7 #include <isl/stream.h>
8 #include <isl/vertices.h>
10 #include <isl_obj_list.h>
11 #include <isl_obj_str.h>
12 #include <barvinok/isl.h>
13 #include <barvinok/options.h>
18 #include <cloog/isl/cloog.h>
21 static int isl_bool_false
= 0;
22 static int isl_bool_true
= 1;
23 static int isl_bool_error
= -1;
25 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
26 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
27 ISCC_TYPEOF
, ISCC_PRINT
,
29 static const char *op_name
[ISCC_N_OP
] = {
31 [ISCC_WRITE
] = "write",
32 [ISCC_PRINT
] = "print",
33 [ISCC_SOURCE
] = "source",
34 [ISCC_VERTICES
] = "vertices",
37 [ISCC_BEFORE
] = "before",
38 [ISCC_UNDER
] = "under",
39 [ISCC_TYPEOF
] = "typeof"
41 static enum isl_token_type iscc_op
[ISCC_N_OP
];
43 struct isl_arg_choice iscc_format
[] = {
44 {"isl", ISL_FORMAT_ISL
},
45 {"omega", ISL_FORMAT_OMEGA
},
46 {"polylib", ISL_FORMAT_POLYLIB
},
47 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
48 {"latex", ISL_FORMAT_LATEX
},
54 struct barvinok_options
*barvinok
;
59 struct isl_arg iscc_options_arg
[] = {
60 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
62 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
63 iscc_format
, ISL_FORMAT_ISL
, "output format")
64 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
65 "allow read and write operations")
69 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
70 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
72 static void *isl_obj_bool_copy(void *v
)
77 static void isl_obj_bool_free(void *v
)
81 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
84 if (v
== &isl_bool_true
)
85 return isl_printer_print_str(p
, "True");
86 else if (v
== &isl_bool_false
)
87 return isl_printer_print_str(p
, "False");
89 return isl_printer_print_str(p
, "Error");
92 static void *isl_obj_bool_add(void *v1
, void *v2
)
97 struct isl_obj_vtable isl_obj_bool_vtable
= {
103 #define isl_obj_bool (&isl_obj_bool_vtable)
105 int *isl_bool_from_int(int res
)
107 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
110 int *union_map_is_equal(__isl_take isl_union_map
*map1
,
111 __isl_take isl_union_map
*map2
)
113 int res
= isl_union_map_is_equal(map1
, map2
);
114 isl_union_map_free(map1
);
115 isl_union_map_free(map2
);
116 return isl_bool_from_int(res
);
118 int *union_set_is_equal(__isl_take isl_union_set
*set1
,
119 __isl_take isl_union_set
*set2
)
121 return union_map_is_equal((isl_union_map
*)set1
, (isl_union_map
*)set2
);
124 int *union_map_is_subset(__isl_take isl_union_map
*map1
,
125 __isl_take isl_union_map
*map2
)
127 int res
= isl_union_map_is_subset(map1
, map2
);
128 isl_union_map_free(map1
);
129 isl_union_map_free(map2
);
130 return isl_bool_from_int(res
);
132 int *union_set_is_subset(__isl_take isl_union_set
*set1
,
133 __isl_take isl_union_set
*set2
)
135 return union_map_is_subset((isl_union_map
*)set1
, (isl_union_map
*)set2
);
138 int *union_map_is_strict_subset(__isl_take isl_union_map
*map1
,
139 __isl_take isl_union_map
*map2
)
141 int res
= isl_union_map_is_strict_subset(map1
, map2
);
142 isl_union_map_free(map1
);
143 isl_union_map_free(map2
);
144 return isl_bool_from_int(res
);
146 int *union_set_is_strict_subset(__isl_take isl_union_set
*set1
,
147 __isl_take isl_union_set
*set2
)
149 return union_map_is_strict_subset((isl_union_map
*)set1
,
150 (isl_union_map
*)set2
);
153 int *union_map_is_superset(__isl_take isl_union_map
*map1
,
154 __isl_take isl_union_map
*map2
)
156 return union_map_is_subset(map2
, map1
);
158 int *union_set_is_superset(__isl_take isl_union_set
*set1
,
159 __isl_take isl_union_set
*set2
)
161 return union_set_is_subset(set2
, set1
);
164 int *union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
165 __isl_take isl_union_map
*map2
)
167 return union_map_is_strict_subset(map2
, map1
);
169 int *union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
170 __isl_take isl_union_set
*set2
)
172 return union_set_is_strict_subset(set2
, set1
);
175 extern struct isl_obj_vtable isl_obj_list_vtable
;
176 #define isl_obj_list (&isl_obj_list_vtable)
178 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
180 enum isl_token_type op
;
186 struct isc_named_bin_op
{
188 struct isc_bin_op op
;
192 isl_union_pw_qpolynomial
*upwqp
;
193 isl_union_pw_qpolynomial
*res
;
196 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
198 struct iscc_at
*at
= (struct iscc_at
*) user
;
202 set
= isl_set_from_point(isl_point_copy(pnt
));
203 qp
= isl_union_pw_qpolynomial_eval(
204 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
206 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
207 isl_union_pw_qpolynomial_from_pw_qpolynomial(
208 isl_pw_qpolynomial_alloc(set
, qp
)));
213 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
214 __isl_take isl_union_pw_qpolynomial
*upwqp
,
215 __isl_take isl_union_set
*uset
)
220 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
222 isl_union_set_foreach_point(uset
, eval_at
, &at
);
224 isl_union_pw_qpolynomial_free(upwqp
);
225 isl_union_set_free(uset
);
230 struct iscc_fold_at
{
231 isl_union_pw_qpolynomial_fold
*upwf
;
232 isl_union_pw_qpolynomial
*res
;
235 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
237 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
241 set
= isl_set_from_point(isl_point_copy(pnt
));
242 qp
= isl_union_pw_qpolynomial_fold_eval(
243 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
245 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
246 isl_union_pw_qpolynomial_from_pw_qpolynomial(
247 isl_pw_qpolynomial_alloc(set
, qp
)));
252 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
253 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
254 __isl_take isl_union_set
*uset
)
256 struct iscc_fold_at at
;
259 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
261 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
263 isl_union_pw_qpolynomial_fold_free(upwf
);
264 isl_union_set_free(uset
);
269 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
270 __isl_take isl_union_pw_qpolynomial
*upwqp
,
271 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
273 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
277 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
278 __isl_take isl_union_map
*umap
,
279 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
282 struct isl_list
*list
;
285 ctx
= isl_union_map_get_ctx(umap
);
286 list
= isl_list_alloc(ctx
, 2);
290 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
291 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
293 list
->obj
[1].type
= isl_obj_bool
;
294 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
295 if (tight
< 0 || !list
->obj
[0].v
)
300 isl_union_map_free(umap
);
301 isl_union_pw_qpolynomial_fold_free(upwf
);
307 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_int_mul(
308 __isl_take isl_union_pw_qpolynomial
*upwqp
, __isl_take isl_int_obj
*i
)
316 isl_int_obj_get_int(i
, &v
);
317 upwqp
= isl_union_pw_qpolynomial_mul_isl_int(upwqp
, v
);
324 isl_union_pw_qpolynomial_free(upwqp
);
328 static __isl_give isl_union_pw_qpolynomial
*int_union_pw_qpolynomial_mul(
329 __isl_take isl_int_obj
*i
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
331 return union_pw_qpolynomial_int_mul(upwqp
, i
);
334 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_fold_int_mul(
335 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
336 __isl_take isl_int_obj
*i
)
344 isl_int_obj_get_int(i
, &v
);
345 upwf
= isl_union_pw_qpolynomial_fold_mul_isl_int(upwf
, v
);
352 isl_union_pw_qpolynomial_fold_free(upwf
);
356 static __isl_give isl_union_pw_qpolynomial_fold
*int_union_pw_qpolynomial_fold_mul(
357 __isl_take isl_int_obj
*i
,
358 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
360 return union_pw_qpolynomial_fold_int_mul(upwf
, i
);
363 struct isc_bin_op bin_ops
[] = {
364 { '+', isl_obj_int
, isl_obj_int
, isl_obj_int
,
365 (isc_bin_op_fn
) &isl_int_obj_add
},
366 { '-', isl_obj_int
, isl_obj_int
, isl_obj_int
,
367 (isc_bin_op_fn
) &isl_int_obj_sub
},
368 { '*', isl_obj_int
, isl_obj_int
, isl_obj_int
,
369 (isc_bin_op_fn
) &isl_int_obj_mul
},
370 { '+', isl_obj_union_set
, isl_obj_union_set
,
372 (isc_bin_op_fn
) &isl_union_set_union
},
373 { '+', isl_obj_union_map
, isl_obj_union_map
,
375 (isc_bin_op_fn
) &isl_union_map_union
},
376 { '-', isl_obj_union_set
, isl_obj_union_set
,
378 (isc_bin_op_fn
) &isl_union_set_subtract
},
379 { '-', isl_obj_union_map
, isl_obj_union_map
,
381 (isc_bin_op_fn
) &isl_union_map_subtract
},
382 { '*', isl_obj_union_set
, isl_obj_union_set
,
384 (isc_bin_op_fn
) &isl_union_set_intersect
},
385 { '*', isl_obj_union_map
, isl_obj_union_map
,
387 (isc_bin_op_fn
) &isl_union_map_intersect
},
388 { '*', isl_obj_union_map
, isl_obj_union_set
,
390 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
391 { '.', isl_obj_union_map
, isl_obj_union_map
,
393 (isc_bin_op_fn
) &isl_union_map_apply_range
},
394 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
395 isl_obj_union_pw_qpolynomial
,
396 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
397 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
399 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
400 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
402 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
403 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
404 (isc_bin_op_fn
) &union_set_is_equal
},
405 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
406 (isc_bin_op_fn
) &union_map_is_equal
},
407 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
408 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_subset
},
409 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
410 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_subset
},
411 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
412 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_subset
},
413 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
414 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_subset
},
415 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
416 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_superset
},
417 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
418 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_superset
},
419 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
420 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_superset
},
421 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
422 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_superset
},
423 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
425 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
426 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
428 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
429 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
431 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
432 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
434 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
435 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
437 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
438 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
440 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
441 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
443 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
444 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
446 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
447 { '.', isl_obj_union_pw_qpolynomial_fold
,
448 isl_obj_union_pw_qpolynomial_fold
,
449 isl_obj_union_pw_qpolynomial_fold
,
450 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
451 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
452 isl_obj_union_pw_qpolynomial
,
453 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
454 { '+', isl_obj_union_pw_qpolynomial
,
455 isl_obj_union_pw_qpolynomial_fold
,
456 isl_obj_union_pw_qpolynomial_fold
,
457 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
458 { '+', isl_obj_union_pw_qpolynomial_fold
,
459 isl_obj_union_pw_qpolynomial
,
460 isl_obj_union_pw_qpolynomial_fold
,
461 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
462 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
463 isl_obj_union_pw_qpolynomial
,
464 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
465 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial
,
466 isl_obj_union_pw_qpolynomial
,
467 (isc_bin_op_fn
) &int_union_pw_qpolynomial_mul
},
468 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_int
,
469 isl_obj_union_pw_qpolynomial
,
470 (isc_bin_op_fn
) &union_pw_qpolynomial_int_mul
},
471 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial_fold
,
472 isl_obj_union_pw_qpolynomial_fold
,
473 (isc_bin_op_fn
) &int_union_pw_qpolynomial_fold_mul
},
474 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_int
,
475 isl_obj_union_pw_qpolynomial_fold
,
476 (isc_bin_op_fn
) &union_pw_qpolynomial_fold_int_mul
},
477 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
478 isl_obj_union_pw_qpolynomial
,
479 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
480 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
481 isl_obj_union_pw_qpolynomial
,
482 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
483 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
484 isl_obj_union_pw_qpolynomial_fold
,
485 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
486 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
487 isl_obj_union_pw_qpolynomial
,
488 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
489 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
490 isl_obj_union_pw_qpolynomial
,
491 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
492 { '%', isl_obj_union_set
, isl_obj_union_set
,
494 (isc_bin_op_fn
) &isl_union_set_gist
},
495 { '%', isl_obj_union_map
, isl_obj_union_map
,
497 (isc_bin_op_fn
) &isl_union_map_gist
},
498 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
499 isl_obj_union_pw_qpolynomial
,
500 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
501 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
502 isl_obj_union_pw_qpolynomial_fold
,
503 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
504 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
505 (isc_bin_op_fn
) &isl_str_concat
},
509 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
510 __isl_take isl_union_map
*umap2
)
512 return isl_union_map_apply_range(umap2
, umap1
);
515 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
516 __isl_take isl_union_pw_qpolynomial
*upwqp
,
517 __isl_take isl_union_map
*umap
)
519 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
522 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
523 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
524 __isl_take isl_union_map
*umap
)
526 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
529 struct isc_named_bin_op named_bin_ops
[] = {
530 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
532 (isc_bin_op_fn
) &map_after_map
} },
533 { "after", { -1, isl_obj_union_pw_qpolynomial
,
534 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
535 (isc_bin_op_fn
) &qpolynomial_after_map
} },
536 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
537 isl_obj_union_map
, isl_obj_list
,
538 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
539 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
541 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
542 { "before", { -1, isl_obj_union_map
,
543 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
544 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
545 { "before", { -1, isl_obj_union_map
,
546 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
547 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
548 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
550 (isc_bin_op_fn
) &isl_union_set_product
} },
551 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
553 (isc_bin_op_fn
) &isl_union_map_product
} },
557 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
559 return isl_set_from_basic_set(isl_union_set_sample(uset
));
562 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
564 return isl_map_from_basic_map(isl_union_map_sample(umap
));
567 static __isl_give
struct isl_list
*union_map_power(
568 __isl_take isl_union_map
*umap
)
571 struct isl_list
*list
;
574 ctx
= isl_union_map_get_ctx(umap
);
575 list
= isl_list_alloc(ctx
, 2);
579 list
->obj
[0].type
= isl_obj_union_map
;
580 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
581 list
->obj
[1].type
= isl_obj_bool
;
582 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
583 if (exact
< 0 || !list
->obj
[0].v
)
588 isl_union_map_free(umap
);
594 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
595 __isl_take isl_union_pw_qpolynomial
*upwqp
)
598 struct isl_list
*list
;
601 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
602 list
= isl_list_alloc(ctx
, 2);
606 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
607 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
608 isl_fold_max
, &tight
);
609 list
->obj
[1].type
= isl_obj_bool
;
610 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
611 if (tight
< 0 || !list
->obj
[0].v
)
616 isl_union_pw_qpolynomial_free(upwqp
);
623 void *map_codegen(void *arg
)
626 isl_union_map
*umap
= (isl_union_map
*)arg
;
627 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
629 CloogOptions
*options
;
630 CloogDomain
*context
;
631 CloogUnionDomain
*ud
;
633 struct clast_stmt
*stmt
;
635 state
= cloog_isl_state_malloc(ctx
);
636 options
= cloog_options_malloc(state
);
637 options
->language
= LANGUAGE_C
;
638 options
->strides
= 1;
640 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
642 dim
= isl_union_map_get_dim(umap
);
643 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
645 input
= cloog_input_alloc(context
, ud
);
647 stmt
= cloog_clast_create_from_input(input
, options
);
648 clast_pprint(stdout
, stmt
, 0, options
);
649 cloog_clast_free(stmt
);
652 cloog_options_free(options
);
653 cloog_state_free(state
);
654 isl_union_map_free(umap
);
658 void *set_codegen(void *arg
)
661 isl_union_set
*uset
= (isl_union_set
*)arg
;
662 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
664 CloogOptions
*options
;
665 CloogDomain
*context
;
666 CloogUnionDomain
*ud
;
668 struct clast_stmt
*stmt
;
670 if (isl_union_set_n_set(uset
) > 1)
671 isl_die(ctx
, isl_error_invalid
,
672 "code generation for more than one domain "
673 "requires a schedule", goto error
);
675 state
= cloog_isl_state_malloc(ctx
);
676 options
= cloog_options_malloc(state
);
677 options
->language
= LANGUAGE_C
;
678 options
->strides
= 1;
680 ud
= cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset
));
682 dim
= isl_union_set_get_dim(uset
);
683 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
685 input
= cloog_input_alloc(context
, ud
);
687 stmt
= cloog_clast_create_from_input(input
, options
);
688 clast_pprint(stdout
, stmt
, 0, options
);
689 cloog_clast_free(stmt
);
691 cloog_options_free(options
);
692 cloog_state_free(state
);
694 isl_union_set_free(uset
);
699 static int add_point(__isl_take isl_point
*pnt
, void *user
)
701 isl_union_set
**scan
= (isl_union_set
**) user
;
703 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
708 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
712 scan
= isl_union_set_empty(isl_union_set_get_dim(uset
));
714 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
715 isl_union_set_free(scan
);
719 isl_union_set_free(uset
);
723 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
725 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
728 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
729 __isl_take isl_union_pw_qpolynomial
*upwqp
)
731 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
734 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
735 __isl_take isl_union_pw_qpolynomial
*upwqp
)
737 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
740 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
741 __isl_take isl_union_pw_qpolynomial
*upwqp
)
743 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
746 typedef void *(*isc_un_op_fn
)(void *arg
);
748 enum isl_token_type op
;
753 struct isc_named_un_op
{
757 struct isc_named_un_op named_un_ops
[] = {
758 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
759 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
760 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
761 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
762 {"card", { -1, isl_obj_union_set
,
763 isl_obj_union_pw_qpolynomial
,
764 (isc_un_op_fn
) &isl_union_set_card
} },
765 {"card", { -1, isl_obj_union_map
,
766 isl_obj_union_pw_qpolynomial
,
767 (isc_un_op_fn
) &isl_union_map_card
} },
768 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
769 (isc_un_op_fn
) &isl_union_set_coalesce
} },
770 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
771 (isc_un_op_fn
) &isl_union_map_coalesce
} },
772 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
773 isl_obj_union_pw_qpolynomial
,
774 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
775 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
776 isl_obj_union_pw_qpolynomial_fold
,
777 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
779 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
781 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
784 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
785 (isc_un_op_fn
) &isl_union_map_deltas
} },
786 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
787 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
788 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
789 (isc_un_op_fn
) &isl_union_map_domain
} },
790 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
791 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
792 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
794 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
795 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
796 (isc_un_op_fn
) &isl_union_map_domain
} },
797 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
799 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
800 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
802 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
803 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
804 (isc_un_op_fn
) &isl_union_map_domain_map
} },
805 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
806 (isc_un_op_fn
) &isl_union_map_range
} },
807 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
808 (isc_un_op_fn
) &isl_union_map_range
} },
809 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
810 (isc_un_op_fn
) &isl_union_map_range_map
} },
811 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
812 (isc_un_op_fn
) &isl_union_set_identity
} },
813 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
814 (isc_un_op_fn
) &isl_union_map_lexmin
} },
815 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
816 (isc_un_op_fn
) &isl_union_map_lexmax
} },
817 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
818 (isc_un_op_fn
) &isl_union_set_lexmin
} },
819 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
820 (isc_un_op_fn
) &isl_union_set_lexmax
} },
821 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
822 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
823 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
824 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
825 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
826 isl_obj_union_pw_qpolynomial
,
827 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
828 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
829 isl_obj_union_pw_qpolynomial
,
830 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
831 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
832 isl_obj_union_pw_qpolynomial
,
833 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
834 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
835 (isc_un_op_fn
) &union_map_power
} },
836 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
837 (isc_un_op_fn
) &union_set_sample
} },
838 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
839 (isc_un_op_fn
) &union_map_sample
} },
840 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
841 (isc_un_op_fn
) &union_set_scan
} },
842 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
843 (isc_un_op_fn
) &union_map_scan
} },
844 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
845 isl_obj_union_pw_qpolynomial
,
846 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
847 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
848 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
849 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
850 (isc_un_op_fn
) &isl_union_set_unwrap
} },
851 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
852 (isc_un_op_fn
) &isl_union_map_wrap
} },
853 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
854 (isc_un_op_fn
) &isl_union_map_zip
} },
858 struct isl_named_obj
{
863 static void free_obj(struct isl_obj obj
)
865 obj
.type
->free(obj
.v
);
868 static int same_name(const void *entry
, const void *val
)
870 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
872 return !strcmp(named
->name
, val
);
875 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
876 char *name
, struct isl_obj obj
)
878 struct isl_hash_table_entry
*entry
;
880 struct isl_named_obj
*named
;
882 name_hash
= isl_hash_string(isl_hash_init(), name
);
883 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
888 free_obj(named
->obj
);
891 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
906 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
907 struct isl_hash_table
*table
, char *name
)
909 struct isl_obj obj
= { isl_obj_none
, NULL
};
910 struct isl_hash_table_entry
*entry
;
913 name_hash
= isl_hash_string(isl_hash_init(), name
);
914 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
916 struct isl_named_obj
*named
;
919 } else if (isdigit(name
[0]))
920 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
922 fprintf(stderr
, "unknown identifier '%s'\n", name
);
925 obj
.v
= obj
.type
->copy(obj
.v
);
929 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
931 if (obj
.type
== super
)
933 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
935 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
937 if (obj
.type
== isl_obj_pw_qpolynomial
&&
938 super
== isl_obj_union_pw_qpolynomial
)
940 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
941 super
== isl_obj_union_pw_qpolynomial_fold
)
943 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
945 if (obj
.type
== isl_obj_list
) {
946 struct isl_list
*list
= obj
.v
;
947 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
948 return is_subtype(list
->obj
[0], super
);
950 if (super
== isl_obj_str
)
955 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
957 struct isl_list
*list
= obj
.v
;
960 obj
.v
= obj
.type
->copy(obj
.v
);
967 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
970 if (obj
.type
== type
)
972 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
973 obj
.type
= isl_obj_union_map
;
974 obj
.v
= isl_union_map_from_map(obj
.v
);
977 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
978 obj
.type
= isl_obj_union_set
;
979 obj
.v
= isl_union_set_from_set(obj
.v
);
982 if (obj
.type
== isl_obj_pw_qpolynomial
&&
983 type
== isl_obj_union_pw_qpolynomial
) {
984 obj
.type
= isl_obj_union_pw_qpolynomial
;
985 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
988 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
989 type
== isl_obj_union_pw_qpolynomial_fold
) {
990 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
991 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
994 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
995 if (type
== isl_obj_union_map
) {
996 obj
.type
= isl_obj_union_map
;
999 if (type
== isl_obj_union_pw_qpolynomial
) {
1000 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
1001 isl_union_set_free(obj
.v
);
1002 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1003 obj
.type
= isl_obj_union_pw_qpolynomial
;
1006 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1007 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
1008 isl_union_set_free(obj
.v
);
1009 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1011 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1015 if (obj
.type
== isl_obj_list
) {
1016 struct isl_list
*list
= obj
.v
;
1017 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1018 return convert(ctx
, obj_at(obj
, 0), type
);
1020 if (type
== isl_obj_str
) {
1025 p
= isl_printer_to_str(ctx
);
1028 p
= obj
.type
->print(p
, obj
.v
);
1029 s
= isl_printer_get_str(p
);
1030 isl_printer_free(p
);
1032 str
= isl_str_from_string(ctx
, s
);
1037 obj
.type
= isl_obj_str
;
1043 obj
.type
= isl_obj_none
;
1048 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1052 struct isl_token
*tok
;
1054 tok
= isl_stream_next_token(s
);
1058 for (i
= 0; ; ++i
) {
1061 if (bin_ops
[i
].op
!= tok
->type
)
1063 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1066 isl_token_free(tok
);
1070 for (i
= 0; ; ++i
) {
1071 if (!named_bin_ops
[i
].name
)
1073 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1075 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1078 isl_token_free(tok
);
1079 return &named_bin_ops
[i
].op
;
1082 isl_stream_push_token(s
, tok
);
1087 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1090 struct isl_token
*tok
;
1092 tok
= isl_stream_next_token(s
);
1096 for (i
= 0; ; ++i
) {
1097 if (!named_un_ops
[i
].name
)
1099 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1102 isl_token_free(tok
);
1103 return &named_un_ops
[i
].op
;
1106 isl_stream_push_token(s
, tok
);
1111 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1116 for (i
= 0; ; ++i
) {
1117 if (!named_un_ops
[i
].name
)
1119 if (named_un_ops
[i
].op
.op
!= like
->op
)
1121 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1124 return &named_un_ops
[i
].op
;
1130 static int is_assign(struct isl_stream
*s
)
1132 struct isl_token
*tok
;
1133 struct isl_token
*tok2
;
1136 tok
= isl_stream_next_token(s
);
1139 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1140 isl_stream_push_token(s
, tok
);
1144 tok2
= isl_stream_next_token(s
);
1146 isl_stream_push_token(s
, tok
);
1149 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1150 isl_stream_push_token(s
, tok2
);
1151 isl_stream_push_token(s
, tok
);
1156 static struct isl_obj
read_obj(struct isl_stream
*s
,
1157 struct isl_hash_table
*table
);
1158 static struct isl_obj
read_expr(struct isl_stream
*s
,
1159 struct isl_hash_table
*table
);
1161 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1162 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1164 struct isl_obj obj
= { isl_obj_none
, NULL
};
1166 obj
= read_obj(s
, table
);
1170 op
= find_matching_un_op(op
, obj
);
1173 isl_die(s
->ctx
, isl_error_invalid
,
1174 "no such unary operator defined on given operand",
1177 obj
= convert(s
->ctx
, obj
, op
->arg
);
1178 obj
.v
= op
->fn(obj
.v
);
1184 obj
.type
= isl_obj_none
;
1189 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1191 struct isl_list
*list
;
1194 if (obj
.type
!= isl_obj_union_map
)
1195 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1196 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1197 list
= isl_list_alloc(ctx
, 2);
1201 list
->obj
[0].type
= isl_obj_union_map
;
1202 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1203 list
->obj
[1].type
= isl_obj_bool
;
1204 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1206 obj
.type
= isl_obj_list
;
1207 if (exact
< 0 || !list
->obj
[0].v
)
1213 obj
.type
= isl_obj_none
;
1218 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1220 struct isl_list
*list
= obj
.v
;
1221 struct isl_token
*tok
;
1224 tok
= isl_stream_next_token(s
);
1225 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1226 isl_stream_error(s
, tok
, "expecting index");
1228 isl_stream_push_token(s
, tok
);
1231 i
= isl_int_get_si(tok
->u
.v
);
1232 isl_token_free(tok
);
1233 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1234 if (isl_stream_eat(s
, ']'))
1237 return obj_at(obj
, i
);
1240 obj
.type
= isl_obj_none
;
1245 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1246 struct isl_hash_table
*table
)
1250 obj
= read_expr(s
, table
);
1251 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1252 is_subtype(obj
, isl_obj_union_map
), goto error
);
1254 if (obj
.type
== isl_obj_list
) {
1255 struct isl_list
*list
= obj
.v
;
1256 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1257 obj
= obj_at(obj
, 0);
1259 if (obj
.type
== isl_obj_set
)
1260 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1261 else if (obj
.type
== isl_obj_map
)
1262 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1263 if (obj
.type
== isl_obj_union_set
) {
1264 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1266 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1270 if (isl_stream_eat(s
, ')'))
1275 isl_union_map_free(umap
);
1278 obj
.type
= isl_obj_none
;
1283 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1284 struct isl_obj obj
, struct isl_hash_table
*table
)
1288 arg
= read_expr(s
, table
);
1289 if (!is_subtype(arg
, isl_obj_union_map
) &&
1290 !is_subtype(arg
, isl_obj_union_set
))
1291 isl_die(s
->ctx
, isl_error_invalid
,
1292 "expecting set of map argument", goto error
);
1294 if (arg
.type
== isl_obj_list
) {
1295 struct isl_list
*list
= arg
.v
;
1296 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1297 arg
= obj_at(arg
, 0);
1299 if (arg
.type
== isl_obj_set
)
1300 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1301 else if (arg
.type
== isl_obj_map
)
1302 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1303 if (arg
.type
== isl_obj_union_set
) {
1304 arg
.v
= isl_union_map_from_range(arg
.v
);
1305 arg
.type
= isl_obj_union_map
;
1307 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1308 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(arg
.v
, obj
.v
);
1310 obj
.type
= isl_obj_list
;
1311 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(arg
.v
, obj
.v
);
1316 if (isl_stream_eat(s
, ')'))
1324 obj
.type
= isl_obj_none
;
1329 struct add_vertex_data
{
1330 struct isl_list
*list
;
1334 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1336 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1337 isl_basic_set
*expr
;
1339 expr
= isl_vertex_get_expr(vertex
);
1341 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1342 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1345 isl_vertex_free(vertex
);
1350 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1353 isl_basic_set
*hull
;
1354 isl_vertices
*vertices
= NULL
;
1355 struct isl_list
*list
= NULL
;
1357 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1359 set
= isl_set_remove_divs(set
);
1360 hull
= isl_set_convex_hull(set
);
1361 vertices
= isl_basic_set_compute_vertices(hull
);
1362 isl_basic_set_free(hull
);
1366 ctx
= isl_vertices_get_ctx(vertices
);
1367 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1372 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1374 data
->list
= isl_list_concat(list
, data
->list
);
1376 isl_vertices_free(vertices
);
1381 isl_vertices_free(vertices
);
1385 static struct isl_obj
vertices(struct isl_stream
*s
,
1386 struct isl_hash_table
*table
)
1390 struct isl_list
*list
= NULL
;
1391 isl_union_set
*uset
;
1392 struct add_vertex_data data
= { NULL
};
1394 obj
= read_expr(s
, table
);
1395 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1396 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1400 ctx
= isl_union_set_get_ctx(uset
);
1401 list
= isl_list_alloc(ctx
, 0);
1407 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1410 isl_union_set_free(uset
);
1412 obj
.type
= isl_obj_list
;
1417 isl_union_set_free(uset
);
1418 isl_list_free(data
.list
);
1420 obj
.type
= isl_obj_none
;
1425 static struct isl_obj
type_of(struct isl_stream
*s
,
1426 struct isl_hash_table
*table
)
1430 const char *type
= "unknown";
1432 obj
= read_expr(s
, table
);
1434 if (obj
.type
== isl_obj_map
||
1435 obj
.type
== isl_obj_union_map
)
1437 if (obj
.type
== isl_obj_set
||
1438 obj
.type
== isl_obj_union_set
)
1440 if (obj
.type
== isl_obj_pw_qpolynomial
||
1441 obj
.type
== isl_obj_union_pw_qpolynomial
)
1442 type
= "piecewise quasipolynomial";
1443 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1444 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1445 type
= "piecewise quasipolynomial fold";
1446 if (obj
.type
== isl_obj_list
)
1448 if (obj
.type
== isl_obj_bool
)
1450 if (obj
.type
== isl_obj_str
)
1452 if (obj
.type
== isl_obj_int
)
1456 obj
.type
= isl_obj_str
;
1457 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1462 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1463 struct isl_hash_table
*table
)
1467 obj
= read_obj(s
, table
);
1468 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1469 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1476 static struct isl_obj
last_any(struct isl_stream
*s
,
1477 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1478 __isl_take isl_union_map
*may_source
)
1480 struct isl_obj obj
= { isl_obj_none
, NULL
};
1481 isl_union_map
*sink
= NULL
;
1482 isl_union_map
*schedule
= NULL
;
1483 isl_union_map
*may_dep
;
1484 isl_union_map
*must_dep
;
1486 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1489 sink
= read_map(s
, table
);
1493 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1496 schedule
= read_map(s
, table
);
1500 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1501 schedule
, &must_dep
, &may_dep
,
1505 obj
.type
= isl_obj_union_map
;
1506 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1510 isl_union_map_free(may_source
);
1511 isl_union_map_free(must_source
);
1512 isl_union_map_free(sink
);
1513 isl_union_map_free(schedule
);
1515 obj
.type
= isl_obj_none
;
1520 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1522 struct isl_obj obj
= { isl_obj_none
, NULL
};
1523 isl_union_map
*must_source
= NULL
;
1524 isl_union_map
*may_source
= NULL
;
1525 isl_union_map
*sink
= NULL
;
1526 isl_union_map
*schedule
= NULL
;
1527 isl_union_map
*may_dep
;
1529 may_source
= read_map(s
, table
);
1533 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1534 must_source
= read_map(s
, table
);
1537 return last_any(s
, table
, must_source
, may_source
);
1540 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1543 sink
= read_map(s
, table
);
1547 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1550 schedule
= read_map(s
, table
);
1554 must_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1555 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1556 schedule
, NULL
, &may_dep
,
1560 obj
.type
= isl_obj_union_map
;
1565 isl_union_map_free(may_source
);
1566 isl_union_map_free(must_source
);
1567 isl_union_map_free(sink
);
1568 isl_union_map_free(schedule
);
1570 obj
.type
= isl_obj_none
;
1575 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1577 struct isl_obj obj
= { isl_obj_none
, NULL
};
1578 struct isl_list
*list
= NULL
;
1579 isl_union_map
*must_source
= NULL
;
1580 isl_union_map
*may_source
= NULL
;
1581 isl_union_map
*sink
= NULL
;
1582 isl_union_map
*schedule
= NULL
;
1583 isl_union_map
*must_dep
;
1584 isl_union_map
*must_no_source
;
1586 must_source
= read_map(s
, table
);
1590 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1591 may_source
= read_map(s
, table
);
1594 return last_any(s
, table
, must_source
, may_source
);
1597 list
= isl_list_alloc(s
->ctx
, 2);
1601 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1604 sink
= read_map(s
, table
);
1608 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1611 schedule
= read_map(s
, table
);
1615 may_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1616 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1617 schedule
, &must_dep
, NULL
,
1618 &must_no_source
, NULL
) < 0)
1621 list
->obj
[0].type
= isl_obj_union_map
;
1622 list
->obj
[0].v
= must_dep
;
1623 list
->obj
[1].type
= isl_obj_union_map
;
1624 list
->obj
[1].v
= must_no_source
;
1627 obj
.type
= isl_obj_list
;
1631 isl_list_free(list
);
1632 isl_union_map_free(may_source
);
1633 isl_union_map_free(must_source
);
1634 isl_union_map_free(sink
);
1635 isl_union_map_free(schedule
);
1637 obj
.type
= isl_obj_none
;
1642 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1644 struct isl_token
*tok
;
1646 if (isl_stream_eat_if_available(s
, '+'))
1647 return transitive_closure(s
->ctx
, obj
);
1649 tok
= isl_stream_next_token(s
);
1650 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1651 isl_stream_error(s
, tok
, "expecting -1");
1653 isl_stream_push_token(s
, tok
);
1656 isl_token_free(tok
);
1657 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1658 if (obj
.type
!= isl_obj_union_map
)
1659 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1661 obj
.v
= isl_union_map_reverse(obj
.v
);
1668 obj
.type
= isl_obj_none
;
1673 static struct isl_obj
read_from_file(struct isl_stream
*s
)
1676 struct isl_token
*tok
;
1677 struct isl_stream
*s_file
;
1678 struct iscc_options
*options
;
1681 tok
= isl_stream_next_token(s
);
1682 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1683 isl_stream_error(s
, tok
, "expecting filename");
1684 isl_token_free(tok
);
1688 options
= isl_ctx_peek_iscc_options(s
->ctx
);
1689 if (!options
|| !options
->io
) {
1690 isl_token_free(tok
);
1691 isl_die(s
->ctx
, isl_error_invalid
,
1692 "read operation not allowed", goto error
);
1695 file
= fopen(tok
->u
.s
, "r");
1696 isl_token_free(tok
);
1697 isl_assert(s
->ctx
, file
, goto error
);
1699 s_file
= isl_stream_new_file(s
->ctx
, file
);
1705 obj
= isl_stream_read_obj(s_file
);
1707 isl_stream_free(s_file
);
1712 obj
.type
= isl_obj_none
;
1717 static struct isl_obj
write_to_file(struct isl_stream
*s
,
1718 struct isl_hash_table
*table
)
1721 struct isl_token
*tok
;
1722 struct isl_stream
*s_file
;
1723 struct iscc_options
*options
;
1727 tok
= isl_stream_next_token(s
);
1728 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1729 isl_stream_error(s
, tok
, "expecting filename");
1730 isl_token_free(tok
);
1734 obj
= read_expr(s
, table
);
1736 options
= isl_ctx_peek_iscc_options(s
->ctx
);
1737 if (!options
|| !options
->io
) {
1738 isl_token_free(tok
);
1739 isl_die(s
->ctx
, isl_error_invalid
,
1740 "write operation not allowed", goto error
);
1743 file
= fopen(tok
->u
.s
, "w");
1744 isl_token_free(tok
);
1746 isl_die(s
->ctx
, isl_error_unknown
,
1747 "could not open file for writing", goto error
);
1749 p
= isl_printer_to_file(s
->ctx
, file
);
1750 p
= isl_printer_set_output_format(p
, options
->format
);
1751 p
= obj
.type
->print(p
, obj
.v
);
1752 p
= isl_printer_end_line(p
);
1753 isl_printer_free(p
);
1758 obj
.type
= isl_obj_none
;
1763 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
1765 struct isl_token
*tok
;
1766 struct isl_obj obj
= { isl_obj_none
, NULL
};
1768 tok
= isl_stream_next_token(s
);
1771 if (tok
->type
== ISL_TOKEN_STRING
) {
1773 str
= isl_str_alloc(s
->ctx
);
1776 str
->s
= strdup(tok
->u
.s
);
1777 isl_token_free(tok
);
1779 obj
.type
= isl_obj_str
;
1781 isl_stream_push_token(s
, tok
);
1784 isl_token_free(tok
);
1788 static __isl_give
char *read_ident(struct isl_stream
*s
)
1791 struct isl_token
*tok
, *tok2
;
1793 name
= isl_stream_read_ident_if_available(s
);
1797 tok
= isl_stream_next_token(s
);
1800 if (tok
->type
!= '$') {
1801 isl_stream_push_token(s
, tok
);
1804 tok2
= isl_stream_next_token(s
);
1805 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1807 isl_stream_push_token(s
, tok2
);
1808 isl_stream_push_token(s
, tok
);
1812 name
= isl_int_get_str(tok2
->u
.v
);
1813 isl_token_free(tok
);
1814 isl_token_free(tok2
);
1819 static struct isl_obj
read_obj(struct isl_stream
*s
,
1820 struct isl_hash_table
*table
)
1822 struct isl_obj obj
= { isl_obj_none
, NULL
};
1824 struct isc_un_op
*op
= NULL
;
1826 obj
= read_string_if_available(s
);
1829 if (isl_stream_eat_if_available(s
, '(')) {
1830 obj
= read_expr(s
, table
);
1831 if (!obj
.v
|| isl_stream_eat(s
, ')'))
1834 op
= read_prefix_un_op_if_available(s
);
1836 return read_un_op_expr(s
, table
, op
);
1838 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
1839 return read_from_file(s
);
1840 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
1841 return write_to_file(s
, table
);
1842 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
1843 return vertices(s
, table
);
1844 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
1845 return any(s
, table
);
1846 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
1847 return last(s
, table
);
1848 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
1849 return type_of(s
, table
);
1851 name
= read_ident(s
);
1853 obj
= stored_obj(s
->ctx
, table
, name
);
1855 obj
= isl_stream_read_obj(s
);
1860 if (isl_stream_eat_if_available(s
, '^'))
1861 obj
= power(s
, obj
);
1862 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
1863 obj
= obj_at_index(s
, obj
);
1864 else if (is_subtype(obj
, isl_obj_union_map
) &&
1865 isl_stream_eat_if_available(s
, '(')) {
1866 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1867 obj
= apply(s
, obj
.v
, table
);
1868 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
1869 isl_stream_eat_if_available(s
, '(')) {
1870 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
1871 obj
= apply_fun(s
, obj
, table
);
1872 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
1873 isl_stream_eat_if_available(s
, '(')) {
1874 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
1875 obj
= apply_fun(s
, obj
, table
);
1881 obj
.type
= isl_obj_none
;
1886 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
1887 struct isl_obj lhs
, struct isl_obj rhs
)
1891 for (i
= 0; ; ++i
) {
1894 if (bin_ops
[i
].op
!= like
->op
)
1896 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1898 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
1904 for (i
= 0; ; ++i
) {
1905 if (!named_bin_ops
[i
].name
)
1907 if (named_bin_ops
[i
].op
.op
!= like
->op
)
1909 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1911 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
1914 return &named_bin_ops
[i
].op
;
1920 static int next_is_neg_int(struct isl_stream
*s
)
1922 struct isl_token
*tok
;
1925 tok
= isl_stream_next_token(s
);
1926 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
1927 isl_stream_push_token(s
, tok
);
1932 static struct isl_obj
read_expr(struct isl_stream
*s
,
1933 struct isl_hash_table
*table
)
1935 struct isl_obj obj
= { isl_obj_none
, NULL
};
1936 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
1938 obj
= read_obj(s
, table
);
1940 struct isc_bin_op
*op
= NULL
;
1942 op
= read_bin_op_if_available(s
, obj
);
1946 right_obj
= read_obj(s
, table
);
1948 op
= find_matching_bin_op(op
, obj
, right_obj
);
1951 isl_die(s
->ctx
, isl_error_invalid
,
1952 "no such binary operator defined on given operands",
1955 obj
= convert(s
->ctx
, obj
, op
->lhs
);
1956 right_obj
= convert(s
->ctx
, right_obj
, op
->rhs
);
1957 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
1961 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
1962 right_obj
= read_obj(s
, table
);
1963 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
1968 free_obj(right_obj
);
1970 obj
.type
= isl_obj_none
;
1975 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
1976 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
1978 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
1979 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
1981 struct isl_obj obj
= { isl_obj_none
, NULL
};
1985 struct isc_bin_op
*op
= NULL
;
1990 if (isl_stream_is_empty(s
))
1993 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
1994 return source_file(s
, table
, p
);
1996 assign
= is_assign(s
);
1998 lhs
= isl_stream_read_ident_if_available(s
);
1999 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2001 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2006 obj
= read_expr(s
, table
);
2007 if (obj
.type
== isl_obj_none
|| obj
.v
== NULL
)
2009 if (isl_stream_eat(s
, ';'))
2013 p
= obj
.type
->print(p
, obj
.v
);
2014 p
= isl_printer_end_line(p
);
2019 static int count
= 0;
2020 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2021 lhs
= strdup(buf
+ 1);
2023 p
= isl_printer_print_str(p
, buf
);
2024 p
= isl_printer_print_str(p
, " := ");
2025 p
= obj
.type
->print(p
, obj
.v
);
2026 p
= isl_printer_end_line(p
);
2028 if (do_assign(s
->ctx
, table
, lhs
, obj
))
2033 isl_stream_flush_tokens(s
);
2034 isl_stream_skip_line(s
);
2040 int free_cb(void **entry
, void *user
)
2042 struct isl_named_obj
*named
= *entry
;
2044 free_obj(named
->obj
);
2051 static void register_named_ops(struct isl_stream
*s
)
2055 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2056 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2057 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2060 for (i
= 0; ; ++i
) {
2061 if (!named_un_ops
[i
].name
)
2063 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2064 named_un_ops
[i
].name
);
2065 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2068 for (i
= 0; ; ++i
) {
2069 if (!named_bin_ops
[i
].name
)
2071 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2072 named_bin_ops
[i
].name
);
2073 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2077 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2078 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2080 struct isl_token
*tok
;
2081 struct isl_stream
*s_file
;
2084 tok
= isl_stream_next_token(s
);
2085 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2086 isl_stream_error(s
, tok
, "expecting filename");
2087 isl_token_free(tok
);
2091 file
= fopen(tok
->u
.s
, "r");
2092 isl_token_free(tok
);
2093 isl_assert(s
->ctx
, file
, return p
);
2095 s_file
= isl_stream_new_file(s
->ctx
, file
);
2101 register_named_ops(s_file
);
2103 while (!s_file
->eof
)
2104 p
= read_line(s_file
, table
, p
, 0);
2106 isl_stream_free(s_file
);
2109 isl_stream_eat(s
, ';');
2114 int main(int argc
, char **argv
)
2116 struct isl_ctx
*ctx
;
2117 struct isl_stream
*s
;
2118 struct isl_hash_table
*table
;
2119 struct iscc_options
*options
;
2121 int tty
= isatty(0);
2123 options
= iscc_options_new_with_defaults();
2125 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
2127 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
2128 s
= isl_stream_new_file(ctx
, stdin
);
2130 table
= isl_hash_table_alloc(ctx
, 10);
2132 p
= isl_printer_to_file(ctx
, stdout
);
2133 p
= isl_printer_set_output_format(p
, options
->format
);
2136 register_named_ops(s
);
2138 while (p
&& !s
->eof
) {
2139 p
= read_line(s
, table
, p
, tty
);
2142 isl_printer_free(p
);
2143 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2144 isl_hash_table_free(ctx
, table
);