5 #include <isl/stream.h>
6 #include <isl/vertices.h>
7 #include <isl_obj_list.h>
8 #include <isl_obj_str.h>
9 #include <barvinok/isl.h>
10 #include <barvinok/options.h>
15 #include <cloog/isl/cloog.h>
18 static int isl_bool_false
= 0;
19 static int isl_bool_true
= 1;
20 static int isl_bool_error
= -1;
22 enum iscc_op
{ ISCC_READ
, ISCC_SOURCE
, ISCC_VERTICES
,
23 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
, ISCC_N_OP
};
24 static const char *op_name
[ISCC_N_OP
] = {
26 [ISCC_SOURCE
] = "source",
27 [ISCC_VERTICES
] = "vertices",
30 [ISCC_BEFORE
] = "before",
31 [ISCC_UNDER
] = "under"
33 static enum isl_token_type iscc_op
[ISCC_N_OP
];
35 struct isl_arg_choice iscc_format
[] = {
36 {"isl", ISL_FORMAT_ISL
},
37 {"omega", ISL_FORMAT_OMEGA
},
38 {"polylib", ISL_FORMAT_POLYLIB
},
39 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
40 {"latex", ISL_FORMAT_LATEX
},
46 struct barvinok_options
*barvinok
;
50 struct isl_arg iscc_options_arg
[] = {
51 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
53 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
54 iscc_format
, ISL_FORMAT_ISL
, "output format")
58 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
60 static void *isl_obj_bool_copy(void *v
)
65 static void isl_obj_bool_free(void *v
)
69 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
72 if (v
== &isl_bool_true
)
73 return isl_printer_print_str(p
, "True");
74 else if (v
== &isl_bool_false
)
75 return isl_printer_print_str(p
, "False");
77 return isl_printer_print_str(p
, "Error");
80 static void *isl_obj_bool_add(void *v1
, void *v2
)
85 struct isl_obj_vtable isl_obj_bool_vtable
= {
91 #define isl_obj_bool (&isl_obj_bool_vtable)
93 int *isl_bool_from_int(int res
)
95 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
98 int *union_map_is_equal(__isl_take isl_union_map
*map1
,
99 __isl_take isl_union_map
*map2
)
101 int res
= isl_union_map_is_equal(map1
, map2
);
102 isl_union_map_free(map1
);
103 isl_union_map_free(map2
);
104 return isl_bool_from_int(res
);
106 int *union_set_is_equal(__isl_take isl_union_set
*set1
,
107 __isl_take isl_union_set
*set2
)
109 return union_map_is_equal((isl_union_map
*)set1
, (isl_union_map
*)set2
);
112 int *union_map_is_subset(__isl_take isl_union_map
*map1
,
113 __isl_take isl_union_map
*map2
)
115 int res
= isl_union_map_is_subset(map1
, map2
);
116 isl_union_map_free(map1
);
117 isl_union_map_free(map2
);
118 return isl_bool_from_int(res
);
120 int *union_set_is_subset(__isl_take isl_union_set
*set1
,
121 __isl_take isl_union_set
*set2
)
123 return union_map_is_subset((isl_union_map
*)set1
, (isl_union_map
*)set2
);
126 int *union_map_is_strict_subset(__isl_take isl_union_map
*map1
,
127 __isl_take isl_union_map
*map2
)
129 int res
= isl_union_map_is_strict_subset(map1
, map2
);
130 isl_union_map_free(map1
);
131 isl_union_map_free(map2
);
132 return isl_bool_from_int(res
);
134 int *union_set_is_strict_subset(__isl_take isl_union_set
*set1
,
135 __isl_take isl_union_set
*set2
)
137 return union_map_is_strict_subset((isl_union_map
*)set1
,
138 (isl_union_map
*)set2
);
141 int *union_map_is_superset(__isl_take isl_union_map
*map1
,
142 __isl_take isl_union_map
*map2
)
144 return union_map_is_subset(map2
, map1
);
146 int *union_set_is_superset(__isl_take isl_union_set
*set1
,
147 __isl_take isl_union_set
*set2
)
149 return union_set_is_subset(set2
, set1
);
152 int *union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
153 __isl_take isl_union_map
*map2
)
155 return union_map_is_strict_subset(map2
, map1
);
157 int *union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
158 __isl_take isl_union_set
*set2
)
160 return union_set_is_strict_subset(set2
, set1
);
163 extern struct isl_obj_vtable isl_obj_list_vtable
;
164 #define isl_obj_list (&isl_obj_list_vtable)
166 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
168 enum isl_token_type op
;
174 struct isc_named_bin_op
{
176 struct isc_bin_op op
;
180 isl_union_pw_qpolynomial
*upwqp
;
181 isl_union_pw_qpolynomial
*res
;
184 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
186 struct iscc_at
*at
= (struct iscc_at
*) user
;
190 set
= isl_set_from_point(isl_point_copy(pnt
));
191 qp
= isl_union_pw_qpolynomial_eval(
192 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
194 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
195 isl_union_pw_qpolynomial_from_pw_qpolynomial(
196 isl_pw_qpolynomial_alloc(set
, qp
)));
201 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
202 __isl_take isl_union_pw_qpolynomial
*upwqp
,
203 __isl_take isl_union_set
*uset
)
208 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
210 isl_union_set_foreach_point(uset
, eval_at
, &at
);
212 isl_union_pw_qpolynomial_free(upwqp
);
213 isl_union_set_free(uset
);
218 struct iscc_fold_at
{
219 isl_union_pw_qpolynomial_fold
*upwf
;
220 isl_union_pw_qpolynomial
*res
;
223 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
225 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
229 set
= isl_set_from_point(isl_point_copy(pnt
));
230 qp
= isl_union_pw_qpolynomial_fold_eval(
231 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
233 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
234 isl_union_pw_qpolynomial_from_pw_qpolynomial(
235 isl_pw_qpolynomial_alloc(set
, qp
)));
240 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
241 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
242 __isl_take isl_union_set
*uset
)
244 struct iscc_fold_at at
;
247 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
249 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
251 isl_union_pw_qpolynomial_fold_free(upwf
);
252 isl_union_set_free(uset
);
257 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
258 __isl_take isl_union_pw_qpolynomial
*upwqp
,
259 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
261 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
265 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
266 __isl_take isl_union_map
*umap
,
267 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
270 struct isl_list
*list
;
273 ctx
= isl_union_map_get_ctx(umap
);
274 list
= isl_list_alloc(ctx
, 2);
278 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
279 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
281 list
->obj
[1].type
= isl_obj_bool
;
282 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
283 if (tight
< 0 || !list
->obj
[0].v
)
288 isl_union_map_free(umap
);
289 isl_union_pw_qpolynomial_fold_free(upwf
);
295 struct isc_bin_op bin_ops
[] = {
296 { '+', isl_obj_union_set
, isl_obj_union_set
,
298 (isc_bin_op_fn
) &isl_union_set_union
},
299 { '+', isl_obj_union_map
, isl_obj_union_map
,
301 (isc_bin_op_fn
) &isl_union_map_union
},
302 { '-', isl_obj_union_set
, isl_obj_union_set
,
304 (isc_bin_op_fn
) &isl_union_set_subtract
},
305 { '-', isl_obj_union_map
, isl_obj_union_map
,
307 (isc_bin_op_fn
) &isl_union_map_subtract
},
308 { '*', isl_obj_union_set
, isl_obj_union_set
,
310 (isc_bin_op_fn
) &isl_union_set_intersect
},
311 { '*', isl_obj_union_map
, isl_obj_union_map
,
313 (isc_bin_op_fn
) &isl_union_map_intersect
},
314 { '*', isl_obj_union_map
, isl_obj_union_set
,
316 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
317 { '.', isl_obj_union_map
, isl_obj_union_map
,
319 (isc_bin_op_fn
) &isl_union_map_apply_range
},
320 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
321 isl_obj_union_pw_qpolynomial
,
322 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
323 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
325 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
326 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
328 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
329 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
330 (isc_bin_op_fn
) &union_set_is_equal
},
331 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
332 (isc_bin_op_fn
) &union_map_is_equal
},
333 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
334 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_subset
},
335 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
336 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_subset
},
337 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
338 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_subset
},
339 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
340 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_subset
},
341 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
342 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_superset
},
343 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
344 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_superset
},
345 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
346 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_superset
},
347 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
348 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_superset
},
349 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
351 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
352 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
354 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
355 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
357 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
358 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
360 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
361 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
363 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
364 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
366 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
367 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
369 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
370 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
372 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
373 { '.', isl_obj_union_pw_qpolynomial_fold
,
374 isl_obj_union_pw_qpolynomial_fold
,
375 isl_obj_union_pw_qpolynomial_fold
,
376 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
377 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
378 isl_obj_union_pw_qpolynomial
,
379 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
380 { '+', isl_obj_union_pw_qpolynomial
,
381 isl_obj_union_pw_qpolynomial_fold
,
382 isl_obj_union_pw_qpolynomial_fold
,
383 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
384 { '+', isl_obj_union_pw_qpolynomial_fold
,
385 isl_obj_union_pw_qpolynomial
,
386 isl_obj_union_pw_qpolynomial_fold
,
387 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
388 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
389 isl_obj_union_pw_qpolynomial
,
390 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
391 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
392 isl_obj_union_pw_qpolynomial
,
393 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
394 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
395 isl_obj_union_pw_qpolynomial
,
396 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
397 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
398 isl_obj_union_pw_qpolynomial_fold
,
399 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
400 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
401 isl_obj_union_pw_qpolynomial
,
402 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
403 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
404 isl_obj_union_pw_qpolynomial
,
405 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
406 { '%', isl_obj_union_set
, isl_obj_union_set
,
408 (isc_bin_op_fn
) &isl_union_set_gist
},
409 { '%', isl_obj_union_map
, isl_obj_union_map
,
411 (isc_bin_op_fn
) &isl_union_map_gist
},
412 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
413 isl_obj_union_pw_qpolynomial
,
414 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
415 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
416 isl_obj_union_pw_qpolynomial_fold
,
417 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
418 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
419 (isc_bin_op_fn
) &isl_str_concat
},
423 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
424 __isl_take isl_union_map
*umap2
)
426 return isl_union_map_apply_range(umap2
, umap1
);
429 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
430 __isl_take isl_union_pw_qpolynomial
*upwqp
,
431 __isl_take isl_union_map
*umap
)
433 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
436 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
437 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
438 __isl_take isl_union_map
*umap
)
440 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
443 struct isc_named_bin_op named_bin_ops
[] = {
444 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
446 (isc_bin_op_fn
) &map_after_map
} },
447 { "after", { -1, isl_obj_union_pw_qpolynomial
,
448 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
449 (isc_bin_op_fn
) &qpolynomial_after_map
} },
450 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
451 isl_obj_union_map
, isl_obj_list
,
452 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
453 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
455 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
456 { "before", { -1, isl_obj_union_map
,
457 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
458 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
459 { "before", { -1, isl_obj_union_map
,
460 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
461 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
462 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
464 (isc_bin_op_fn
) &isl_union_set_product
} },
465 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
467 (isc_bin_op_fn
) &isl_union_map_product
} },
471 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
473 return isl_set_from_basic_set(isl_union_set_sample(uset
));
476 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
478 return isl_map_from_basic_map(isl_union_map_sample(umap
));
481 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
482 __isl_take isl_union_pw_qpolynomial
*upwqp
)
485 struct isl_list
*list
;
488 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
489 list
= isl_list_alloc(ctx
, 2);
493 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
494 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
495 isl_fold_max
, &tight
);
496 list
->obj
[1].type
= isl_obj_bool
;
497 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
498 if (tight
< 0 || !list
->obj
[0].v
)
503 isl_union_pw_qpolynomial_free(upwqp
);
510 void *map_codegen(void *arg
)
513 isl_union_map
*umap
= (isl_union_map
*)arg
;
514 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
516 CloogOptions
*options
;
517 CloogDomain
*context
;
518 CloogUnionDomain
*ud
;
520 struct clast_stmt
*stmt
;
522 state
= cloog_isl_state_malloc(ctx
);
523 options
= cloog_options_malloc(state
);
524 options
->language
= LANGUAGE_C
;
525 options
->strides
= 1;
527 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
529 dim
= isl_union_map_get_dim(umap
);
530 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
532 input
= cloog_input_alloc(context
, ud
);
534 stmt
= cloog_clast_create_from_input(input
, options
);
535 clast_pprint(stdout
, stmt
, 0, options
);
536 cloog_clast_free(stmt
);
539 cloog_options_free(options
);
540 cloog_state_free(state
);
541 isl_union_map_free(umap
);
545 void *set_codegen(void *arg
)
548 isl_union_set
*uset
= (isl_union_set
*)arg
;
549 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
551 CloogOptions
*options
;
552 CloogDomain
*context
;
553 CloogUnionDomain
*ud
;
555 struct clast_stmt
*stmt
;
557 if (isl_union_set_n_set(uset
) > 1)
558 isl_die(ctx
, isl_error_invalid
,
559 "code generation for more than one domain "
560 "requires a schedule", goto error
);
562 state
= cloog_isl_state_malloc(ctx
);
563 options
= cloog_options_malloc(state
);
564 options
->language
= LANGUAGE_C
;
565 options
->strides
= 1;
567 ud
= cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset
));
569 dim
= isl_union_set_get_dim(uset
);
570 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
572 input
= cloog_input_alloc(context
, ud
);
574 stmt
= cloog_clast_create_from_input(input
, options
);
575 clast_pprint(stdout
, stmt
, 0, options
);
576 cloog_clast_free(stmt
);
578 cloog_options_free(options
);
579 cloog_state_free(state
);
581 isl_union_set_free(uset
);
586 static int add_point(__isl_take isl_point
*pnt
, void *user
)
588 isl_union_set
**scan
= (isl_union_set
**) user
;
590 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
595 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
599 scan
= isl_union_set_empty(isl_union_set_get_dim(uset
));
601 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
602 isl_union_set_free(scan
);
606 isl_union_set_free(uset
);
610 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
612 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
615 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
616 __isl_take isl_union_pw_qpolynomial
*upwqp
)
618 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
621 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
622 __isl_take isl_union_pw_qpolynomial
*upwqp
)
624 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
627 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
628 __isl_take isl_union_pw_qpolynomial
*upwqp
)
630 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
633 typedef void *(*isc_un_op_fn
)(void *arg
);
635 enum isl_token_type op
;
640 struct isc_named_un_op
{
644 struct isc_named_un_op named_un_ops
[] = {
645 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
646 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
647 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
648 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
649 {"card", { -1, isl_obj_union_set
,
650 isl_obj_union_pw_qpolynomial
,
651 (isc_un_op_fn
) &isl_union_set_card
} },
652 {"card", { -1, isl_obj_union_map
,
653 isl_obj_union_pw_qpolynomial
,
654 (isc_un_op_fn
) &isl_union_map_card
} },
655 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
656 (isc_un_op_fn
) &isl_union_set_coalesce
} },
657 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
658 (isc_un_op_fn
) &isl_union_map_coalesce
} },
659 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
660 isl_obj_union_pw_qpolynomial
,
661 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
662 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
663 isl_obj_union_pw_qpolynomial_fold
,
664 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
666 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
668 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
671 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
672 (isc_un_op_fn
) &isl_union_map_deltas
} },
673 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
674 (isc_un_op_fn
) &isl_union_map_domain
} },
675 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
676 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
677 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
679 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
680 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
681 (isc_un_op_fn
) &isl_union_map_domain
} },
682 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
684 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
685 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
687 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
688 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
689 (isc_un_op_fn
) &isl_union_map_domain_map
} },
690 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
691 (isc_un_op_fn
) &isl_union_map_range
} },
692 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
693 (isc_un_op_fn
) &isl_union_map_range
} },
694 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
695 (isc_un_op_fn
) &isl_union_map_range_map
} },
696 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
697 (isc_un_op_fn
) &isl_union_set_identity
} },
698 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
699 (isc_un_op_fn
) &isl_union_map_lexmin
} },
700 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
701 (isc_un_op_fn
) &isl_union_map_lexmax
} },
702 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
703 (isc_un_op_fn
) &isl_union_set_lexmin
} },
704 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
705 (isc_un_op_fn
) &isl_union_set_lexmax
} },
706 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
707 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
708 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
709 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
710 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
711 isl_obj_union_pw_qpolynomial
,
712 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
713 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
714 isl_obj_union_pw_qpolynomial
,
715 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
716 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
717 isl_obj_union_pw_qpolynomial
,
718 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
719 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
720 (isc_un_op_fn
) &union_set_sample
} },
721 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
722 (isc_un_op_fn
) &union_map_sample
} },
723 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
724 (isc_un_op_fn
) &union_set_scan
} },
725 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
726 (isc_un_op_fn
) &union_map_scan
} },
727 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
728 isl_obj_union_pw_qpolynomial
,
729 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
730 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
731 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
732 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
733 (isc_un_op_fn
) &isl_union_set_unwrap
} },
734 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
735 (isc_un_op_fn
) &isl_union_map_wrap
} },
739 struct isl_named_obj
{
744 static void free_obj(struct isl_obj obj
)
746 obj
.type
->free(obj
.v
);
749 static int same_name(const void *entry
, const void *val
)
751 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
753 return !strcmp(named
->name
, val
);
756 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
757 char *name
, struct isl_obj obj
)
759 struct isl_hash_table_entry
*entry
;
761 struct isl_named_obj
*named
;
763 name_hash
= isl_hash_string(isl_hash_init(), name
);
764 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
769 free_obj(named
->obj
);
772 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
787 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
788 struct isl_hash_table
*table
, char *name
)
790 struct isl_obj obj
= { isl_obj_none
, NULL
};
791 struct isl_hash_table_entry
*entry
;
794 name_hash
= isl_hash_string(isl_hash_init(), name
);
795 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
797 struct isl_named_obj
*named
;
801 fprintf(stderr
, "unknown identifier '%s'\n", name
);
804 obj
.v
= obj
.type
->copy(obj
.v
);
808 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
810 if (obj
.type
== super
)
812 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
814 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
816 if (obj
.type
== isl_obj_pw_qpolynomial
&&
817 super
== isl_obj_union_pw_qpolynomial
)
819 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
820 super
== isl_obj_union_pw_qpolynomial_fold
)
822 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
824 if (obj
.type
== isl_obj_list
) {
825 struct isl_list
*list
= obj
.v
;
826 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
827 return is_subtype(list
->obj
[0], super
);
829 if (super
== isl_obj_str
)
834 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
836 struct isl_list
*list
= obj
.v
;
839 obj
.v
= obj
.type
->copy(obj
.v
);
846 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
849 if (obj
.type
== type
)
851 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
852 obj
.type
= isl_obj_union_map
;
853 obj
.v
= isl_union_map_from_map(obj
.v
);
856 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
857 obj
.type
= isl_obj_union_set
;
858 obj
.v
= isl_union_set_from_set(obj
.v
);
861 if (obj
.type
== isl_obj_pw_qpolynomial
&&
862 type
== isl_obj_union_pw_qpolynomial
) {
863 obj
.type
= isl_obj_union_pw_qpolynomial
;
864 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
867 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
868 type
== isl_obj_union_pw_qpolynomial_fold
) {
869 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
870 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
873 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
874 if (type
== isl_obj_union_map
) {
875 obj
.type
= isl_obj_union_map
;
878 if (type
== isl_obj_union_pw_qpolynomial
) {
879 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
880 isl_union_set_free(obj
.v
);
881 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
882 obj
.type
= isl_obj_union_pw_qpolynomial
;
885 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
886 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
887 isl_union_set_free(obj
.v
);
888 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
890 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
894 if (obj
.type
== isl_obj_list
) {
895 struct isl_list
*list
= obj
.v
;
896 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
897 return convert(ctx
, obj_at(obj
, 0), type
);
899 if (type
== isl_obj_str
) {
904 p
= isl_printer_to_str(ctx
);
907 p
= obj
.type
->print(p
, obj
.v
);
908 s
= isl_printer_get_str(p
);
911 str
= isl_str_alloc(ctx
);
919 obj
.type
= isl_obj_str
;
925 obj
.type
= isl_obj_none
;
930 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
934 struct isl_token
*tok
;
936 tok
= isl_stream_next_token(s
);
943 if (bin_ops
[i
].op
!= tok
->type
)
945 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
953 if (!named_bin_ops
[i
].name
)
955 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
957 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
961 return &named_bin_ops
[i
].op
;
964 isl_stream_push_token(s
, tok
);
969 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
972 struct isl_token
*tok
;
974 tok
= isl_stream_next_token(s
);
979 if (!named_un_ops
[i
].name
)
981 if (named_un_ops
[i
].op
.op
!= tok
->type
)
985 return &named_un_ops
[i
].op
;
988 isl_stream_push_token(s
, tok
);
993 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
999 if (!named_un_ops
[i
].name
)
1001 if (named_un_ops
[i
].op
.op
!= like
->op
)
1003 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1006 return &named_un_ops
[i
].op
;
1012 static int is_assign(struct isl_stream
*s
)
1014 struct isl_token
*tok
;
1015 struct isl_token
*tok2
;
1018 tok
= isl_stream_next_token(s
);
1021 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1022 isl_stream_push_token(s
, tok
);
1026 tok2
= isl_stream_next_token(s
);
1028 isl_stream_push_token(s
, tok
);
1031 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1032 isl_stream_push_token(s
, tok2
);
1033 isl_stream_push_token(s
, tok
);
1038 static struct isl_obj
read_obj(struct isl_stream
*s
,
1039 struct isl_hash_table
*table
);
1040 static struct isl_obj
read_expr(struct isl_stream
*s
,
1041 struct isl_hash_table
*table
);
1043 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1044 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1046 struct isl_obj obj
= { isl_obj_none
, NULL
};
1048 obj
= read_obj(s
, table
);
1052 op
= find_matching_un_op(op
, obj
);
1055 isl_die(s
->ctx
, isl_error_invalid
,
1056 "no such unary operator defined on given operand",
1059 obj
= convert(s
->ctx
, obj
, op
->arg
);
1060 obj
.v
= op
->fn(obj
.v
);
1066 obj
.type
= isl_obj_none
;
1071 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1073 struct isl_list
*list
;
1076 if (obj
.type
!= isl_obj_union_map
)
1077 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1078 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1079 list
= isl_list_alloc(ctx
, 2);
1083 list
->obj
[0].type
= isl_obj_union_map
;
1084 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1085 list
->obj
[1].type
= isl_obj_bool
;
1086 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1088 obj
.type
= isl_obj_list
;
1089 if (exact
< 0 || !list
->obj
[0].v
)
1095 obj
.type
= isl_obj_none
;
1100 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1102 struct isl_list
*list
= obj
.v
;
1103 struct isl_token
*tok
;
1106 tok
= isl_stream_next_token(s
);
1107 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1108 isl_stream_error(s
, tok
, "expecting index");
1110 isl_stream_push_token(s
, tok
);
1113 i
= isl_int_get_si(tok
->u
.v
);
1114 isl_token_free(tok
);
1115 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1116 if (isl_stream_eat(s
, ']'))
1119 return obj_at(obj
, i
);
1122 obj
.type
= isl_obj_none
;
1127 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1128 struct isl_hash_table
*table
)
1132 obj
= read_expr(s
, table
);
1133 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1134 is_subtype(obj
, isl_obj_union_map
), goto error
);
1136 if (obj
.type
== isl_obj_list
) {
1137 struct isl_list
*list
= obj
.v
;
1138 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1139 obj
= obj_at(obj
, 0);
1141 if (obj
.type
== isl_obj_set
)
1142 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1143 else if (obj
.type
== isl_obj_map
)
1144 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1145 if (obj
.type
== isl_obj_union_set
) {
1146 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1148 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1152 if (isl_stream_eat(s
, ')'))
1157 isl_union_map_free(umap
);
1160 obj
.type
= isl_obj_none
;
1165 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1166 struct isl_obj obj
, struct isl_hash_table
*table
)
1170 arg
= read_expr(s
, table
);
1171 isl_assert(s
->ctx
, is_subtype(arg
, isl_obj_union_map
), goto error
);
1173 if (arg
.type
== isl_obj_list
) {
1174 struct isl_list
*list
= arg
.v
;
1175 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1176 arg
= obj_at(arg
, 0);
1178 if (arg
.type
== isl_obj_map
)
1179 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1180 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1181 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(arg
.v
, obj
.v
);
1183 obj
.type
= isl_obj_list
;
1184 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(arg
.v
, obj
.v
);
1189 if (isl_stream_eat(s
, ')'))
1197 obj
.type
= isl_obj_none
;
1202 struct add_vertex_data
{
1203 struct isl_list
*list
;
1207 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1209 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1210 isl_basic_set
*expr
;
1212 expr
= isl_vertex_get_expr(vertex
);
1214 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1215 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1218 isl_vertex_free(vertex
);
1223 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1226 isl_basic_set
*hull
;
1227 isl_vertices
*vertices
= NULL
;
1228 struct isl_list
*list
= NULL
;
1230 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1232 set
= isl_set_remove_divs(set
);
1233 hull
= isl_set_convex_hull(set
);
1234 vertices
= isl_basic_set_compute_vertices(hull
);
1235 isl_basic_set_free(hull
);
1239 ctx
= isl_vertices_get_ctx(vertices
);
1240 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1245 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1247 data
->list
= isl_list_concat(list
, data
->list
);
1249 isl_vertices_free(vertices
);
1254 isl_vertices_free(vertices
);
1258 static struct isl_obj
vertices(struct isl_stream
*s
,
1259 struct isl_hash_table
*table
)
1263 struct isl_list
*list
= NULL
;
1264 isl_union_set
*uset
;
1265 struct add_vertex_data data
= { NULL
};
1267 obj
= read_expr(s
, table
);
1268 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1269 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1273 ctx
= isl_union_set_get_ctx(uset
);
1274 list
= isl_list_alloc(ctx
, 0);
1280 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1283 isl_union_set_free(uset
);
1285 obj
.type
= isl_obj_list
;
1290 isl_union_set_free(uset
);
1291 isl_list_free(data
.list
);
1293 obj
.type
= isl_obj_none
;
1298 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1299 struct isl_hash_table
*table
)
1303 obj
= read_obj(s
, table
);
1304 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1305 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1312 static struct isl_obj
last_any(struct isl_stream
*s
,
1313 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1314 __isl_take isl_union_map
*may_source
)
1316 struct isl_obj obj
= { isl_obj_none
, NULL
};
1317 isl_union_map
*sink
= NULL
;
1318 isl_union_map
*schedule
= NULL
;
1319 isl_union_map
*may_dep
;
1320 isl_union_map
*must_dep
;
1322 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1325 sink
= read_map(s
, table
);
1329 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1332 schedule
= read_map(s
, table
);
1336 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1337 schedule
, &must_dep
, &may_dep
,
1341 obj
.type
= isl_obj_union_map
;
1342 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1346 isl_union_map_free(may_source
);
1347 isl_union_map_free(must_source
);
1348 isl_union_map_free(sink
);
1349 isl_union_map_free(schedule
);
1351 obj
.type
= isl_obj_none
;
1356 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1358 struct isl_obj obj
= { isl_obj_none
, NULL
};
1359 isl_union_map
*must_source
= NULL
;
1360 isl_union_map
*may_source
= NULL
;
1361 isl_union_map
*sink
= NULL
;
1362 isl_union_map
*schedule
= NULL
;
1363 isl_union_map
*may_dep
;
1365 may_source
= read_map(s
, table
);
1369 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1370 must_source
= read_map(s
, table
);
1373 return last_any(s
, table
, must_source
, may_source
);
1376 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1379 sink
= read_map(s
, table
);
1383 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1386 schedule
= read_map(s
, table
);
1390 must_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1391 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1392 schedule
, NULL
, &may_dep
,
1396 obj
.type
= isl_obj_union_map
;
1401 isl_union_map_free(may_source
);
1402 isl_union_map_free(must_source
);
1403 isl_union_map_free(sink
);
1404 isl_union_map_free(schedule
);
1406 obj
.type
= isl_obj_none
;
1411 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1413 struct isl_obj obj
= { isl_obj_none
, NULL
};
1414 struct isl_list
*list
= NULL
;
1415 isl_union_map
*must_source
= NULL
;
1416 isl_union_map
*may_source
= NULL
;
1417 isl_union_map
*sink
= NULL
;
1418 isl_union_map
*schedule
= NULL
;
1419 isl_union_map
*must_dep
;
1420 isl_union_set
*must_no_source
;
1422 must_source
= read_map(s
, table
);
1426 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1427 may_source
= read_map(s
, table
);
1430 return last_any(s
, table
, must_source
, may_source
);
1433 list
= isl_list_alloc(s
->ctx
, 2);
1437 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1440 sink
= read_map(s
, table
);
1444 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1447 schedule
= read_map(s
, table
);
1451 may_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1452 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1453 schedule
, &must_dep
, NULL
,
1454 &must_no_source
, NULL
) < 0)
1457 list
->obj
[0].type
= isl_obj_union_map
;
1458 list
->obj
[0].v
= must_dep
;
1459 list
->obj
[1].type
= isl_obj_union_set
;
1460 list
->obj
[1].v
= must_no_source
;
1463 obj
.type
= isl_obj_list
;
1467 isl_list_free(list
);
1468 isl_union_map_free(may_source
);
1469 isl_union_map_free(must_source
);
1470 isl_union_map_free(sink
);
1471 isl_union_map_free(schedule
);
1473 obj
.type
= isl_obj_none
;
1478 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1480 struct isl_token
*tok
;
1482 if (isl_stream_eat_if_available(s
, '+'))
1483 return transitive_closure(s
->ctx
, obj
);
1485 tok
= isl_stream_next_token(s
);
1486 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1487 isl_stream_error(s
, tok
, "expecting -1");
1489 isl_stream_push_token(s
, tok
);
1492 isl_token_free(tok
);
1493 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1494 if (obj
.type
!= isl_obj_union_map
)
1495 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1497 obj
.v
= isl_union_map_reverse(obj
.v
);
1504 obj
.type
= isl_obj_none
;
1509 static struct isl_obj
read_from_file(struct isl_stream
*s
)
1512 struct isl_token
*tok
;
1513 struct isl_stream
*s_file
;
1516 tok
= isl_stream_next_token(s
);
1517 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1518 isl_stream_error(s
, tok
, "expecting filename");
1519 isl_token_free(tok
);
1523 file
= fopen(tok
->u
.s
, "r");
1524 isl_token_free(tok
);
1525 isl_assert(s
->ctx
, file
, goto error
);
1527 s_file
= isl_stream_new_file(s
->ctx
, file
);
1533 obj
= isl_stream_read_obj(s_file
);
1535 isl_stream_free(s_file
);
1540 obj
.type
= isl_obj_none
;
1545 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
1547 struct isl_token
*tok
;
1548 struct isl_obj obj
= { isl_obj_none
, NULL
};
1550 tok
= isl_stream_next_token(s
);
1553 if (tok
->type
== ISL_TOKEN_STRING
) {
1555 str
= isl_str_alloc(s
->ctx
);
1558 str
->s
= strdup(tok
->u
.s
);
1559 isl_token_free(tok
);
1561 obj
.type
= isl_obj_str
;
1563 isl_stream_push_token(s
, tok
);
1566 isl_token_free(tok
);
1570 static struct isl_obj
read_obj(struct isl_stream
*s
,
1571 struct isl_hash_table
*table
)
1573 struct isl_obj obj
= { isl_obj_none
, NULL
};
1575 struct isc_un_op
*op
= NULL
;
1577 obj
= read_string_if_available(s
);
1580 if (isl_stream_eat_if_available(s
, '(')) {
1581 obj
= read_expr(s
, table
);
1582 if (!obj
.v
|| isl_stream_eat(s
, ')'))
1585 op
= read_prefix_un_op_if_available(s
);
1587 return read_un_op_expr(s
, table
, op
);
1589 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
1590 return read_from_file(s
);
1591 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
1592 return vertices(s
, table
);
1593 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
1594 return any(s
, table
);
1595 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
1596 return last(s
, table
);
1598 name
= isl_stream_read_ident_if_available(s
);
1600 obj
= stored_obj(s
->ctx
, table
, name
);
1602 obj
= isl_stream_read_obj(s
);
1607 if (isl_stream_eat_if_available(s
, '^'))
1608 obj
= power(s
, obj
);
1609 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
1610 obj
= obj_at_index(s
, obj
);
1611 else if (is_subtype(obj
, isl_obj_union_map
) &&
1612 isl_stream_eat_if_available(s
, '(')) {
1613 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1614 obj
= apply(s
, obj
.v
, table
);
1615 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
1616 isl_stream_eat_if_available(s
, '(')) {
1617 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
1618 obj
= apply_fun(s
, obj
, table
);
1619 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
1620 isl_stream_eat_if_available(s
, '(')) {
1621 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
1622 obj
= apply_fun(s
, obj
, table
);
1628 obj
.type
= isl_obj_none
;
1633 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
1634 struct isl_obj lhs
, struct isl_obj rhs
)
1638 for (i
= 0; ; ++i
) {
1641 if (bin_ops
[i
].op
!= like
->op
)
1643 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1645 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
1651 for (i
= 0; ; ++i
) {
1652 if (!named_bin_ops
[i
].name
)
1654 if (named_bin_ops
[i
].op
.op
!= like
->op
)
1656 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1658 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
1661 return &named_bin_ops
[i
].op
;
1667 static struct isl_obj
read_expr(struct isl_stream
*s
,
1668 struct isl_hash_table
*table
)
1670 struct isl_obj obj
= { isl_obj_none
, NULL
};
1671 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
1673 obj
= read_obj(s
, table
);
1675 struct isc_bin_op
*op
= NULL
;
1677 op
= read_bin_op_if_available(s
, obj
);
1681 right_obj
= read_obj(s
, table
);
1683 op
= find_matching_bin_op(op
, obj
, right_obj
);
1686 isl_die(s
->ctx
, isl_error_invalid
,
1687 "no such binary operator defined on given operands",
1690 obj
= convert(s
->ctx
, obj
, op
->lhs
);
1691 right_obj
= convert(s
->ctx
, right_obj
, op
->rhs
);
1692 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
1698 free_obj(right_obj
);
1700 obj
.type
= isl_obj_none
;
1705 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
1706 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
1708 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
1709 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1711 struct isl_obj obj
= { isl_obj_none
, NULL
};
1714 struct isc_bin_op
*op
= NULL
;
1718 if (isl_stream_is_empty(s
))
1721 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
1722 return source_file(s
, table
, p
);
1724 assign
= is_assign(s
);
1726 lhs
= isl_stream_read_ident_if_available(s
);
1727 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
1731 obj
= read_expr(s
, table
);
1732 if (obj
.type
== isl_obj_none
|| obj
.v
== NULL
)
1734 if (isl_stream_eat(s
, ';'))
1738 if (do_assign(s
->ctx
, table
, lhs
, obj
))
1741 p
= obj
.type
->print(p
, obj
.v
);
1742 p
= isl_printer_end_line(p
);
1748 isl_stream_flush_tokens(s
);
1749 isl_stream_skip_line(s
);
1755 int free_cb(void **entry
, void *user
)
1757 struct isl_named_obj
*named
= *entry
;
1759 free_obj(named
->obj
);
1766 static void register_named_ops(struct isl_stream
*s
)
1770 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
1771 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
1772 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
1775 for (i
= 0; ; ++i
) {
1776 if (!named_un_ops
[i
].name
)
1778 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
1779 named_un_ops
[i
].name
);
1780 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
1783 for (i
= 0; ; ++i
) {
1784 if (!named_bin_ops
[i
].name
)
1786 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
1787 named_bin_ops
[i
].name
);
1788 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
1792 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
1793 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1795 struct isl_token
*tok
;
1796 struct isl_stream
*s_file
;
1799 tok
= isl_stream_next_token(s
);
1800 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1801 isl_stream_error(s
, tok
, "expecting filename");
1802 isl_token_free(tok
);
1806 file
= fopen(tok
->u
.s
, "r");
1807 isl_token_free(tok
);
1808 isl_assert(s
->ctx
, file
, return p
);
1810 s_file
= isl_stream_new_file(s
->ctx
, file
);
1816 register_named_ops(s_file
);
1818 while (!s_file
->eof
)
1819 p
= read_line(s_file
, table
, p
);
1821 isl_stream_free(s_file
);
1824 isl_stream_eat(s
, ';');
1829 int main(int argc
, char **argv
)
1831 struct isl_ctx
*ctx
;
1832 struct isl_stream
*s
;
1833 struct isl_hash_table
*table
;
1834 struct iscc_options
*options
;
1837 options
= iscc_options_new_with_defaults();
1839 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
1841 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
1842 s
= isl_stream_new_file(ctx
, stdin
);
1844 table
= isl_hash_table_alloc(ctx
, 10);
1846 p
= isl_printer_to_file(ctx
, stdout
);
1847 p
= isl_printer_set_output_format(p
, options
->format
);
1850 register_named_ops(s
);
1852 while (p
&& !s
->eof
) {
1853 p
= read_line(s
, table
, p
);
1856 isl_printer_free(p
);
1857 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
1858 isl_hash_table_free(ctx
, table
);