iscc.c: read_bool_if_available: remove unused label
[barvinok.git] / iscc.c
blob825cbf1dad71c3145e72753f34442ed5f19091ca
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/obj.h>
7 #include <isl/stream.h>
8 #include <isl/set.h>
9 #include <isl/map.h>
10 #include <isl/vertices.h>
11 #include <isl/flow.h>
12 #include <isl/band.h>
13 #include <isl/schedule.h>
14 #include <isl/ast_build.h>
15 #include <isl_obj_list.h>
16 #include <isl_obj_str.h>
17 #include <barvinok/isl.h>
18 #include <barvinok/options.h>
19 #include "lattice_width.h"
21 #include "config.h"
23 #ifdef HAVE_SIGACTION
24 #include <signal.h>
26 static isl_ctx *main_ctx;
28 static void handler(int signum)
30 if (isl_ctx_aborted(main_ctx))
31 exit(EXIT_FAILURE);
32 isl_ctx_abort(main_ctx);
35 static struct sigaction sa_old;
37 static void install_signal_handler(isl_ctx *ctx)
39 struct sigaction sa;
41 main_ctx = ctx;
43 memset(&sa, 0, sizeof(struct sigaction));
44 sa.sa_handler = &handler;
45 sa.sa_flags = SA_RESTART;
46 sigaction(SIGINT, &sa, &sa_old);
49 static void remove_signal_handler(isl_ctx *ctx)
51 sigaction(SIGINT, &sa_old, NULL);
54 #else
56 static void install_signal_handler(isl_ctx *ctx)
60 static void remove_signal_handler(isl_ctx *ctx)
64 #endif
66 #ifdef HAVE_PET
67 #include <pet.h>
68 #else
69 struct pet_options;
70 int pet_options_set_autodetect(isl_ctx *ctx, int val)
72 return -1;
74 #endif
76 static int isl_bool_false = 0;
77 static int isl_bool_true = 1;
78 static int isl_bool_error = -1;
80 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
81 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
82 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
83 ISCC_MINIMIZING, ISCC_RESPECTING,
84 ISCC_CODEGEN, ISCC_USING,
85 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
86 ISCC_N_OP };
87 static const char *op_name[ISCC_N_OP] = {
88 [ISCC_ASSERT] = "assert",
89 [ISCC_READ] = "read",
90 [ISCC_WRITE] = "write",
91 [ISCC_PRINT] = "print",
92 [ISCC_SOURCE] = "source",
93 [ISCC_VERTICES] = "vertices",
94 [ISCC_LAST] = "last",
95 [ISCC_ANY] = "any",
96 [ISCC_BEFORE] = "before",
97 [ISCC_UNDER] = "under",
98 [ISCC_SCHEDULE] = "schedule",
99 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
100 [ISCC_MINIMIZING] = "minimizing",
101 [ISCC_RESPECTING] = "respecting",
102 [ISCC_CODEGEN] = "codegen",
103 [ISCC_USING] = "using",
104 [ISCC_TYPEOF] = "typeof"
106 static enum isl_token_type iscc_op[ISCC_N_OP];
108 struct isl_arg_choice iscc_format[] = {
109 {"isl", ISL_FORMAT_ISL},
110 {"omega", ISL_FORMAT_OMEGA},
111 {"polylib", ISL_FORMAT_POLYLIB},
112 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
113 {"latex", ISL_FORMAT_LATEX},
114 {"C", ISL_FORMAT_C},
118 struct iscc_options {
119 struct barvinok_options *barvinok;
120 struct pet_options *pet;
121 unsigned format;
122 int io;
125 ISL_ARGS_START(struct iscc_options, iscc_options_args)
126 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
127 "barvinok options")
128 #ifdef HAVE_PET
129 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
130 #endif
131 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
132 iscc_format, ISL_FORMAT_ISL, "output format")
133 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
134 "allow read and write operations")
135 ISL_ARGS_END
137 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
138 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
140 static void *isl_obj_bool_copy(void *v)
142 return v;
145 static void isl_obj_bool_free(void *v)
149 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
150 void *v)
152 if (v == &isl_bool_true)
153 return isl_printer_print_str(p, "True");
154 else if (v == &isl_bool_false)
155 return isl_printer_print_str(p, "False");
156 else
157 return isl_printer_print_str(p, "Error");
160 static void *isl_obj_bool_add(void *v1, void *v2)
162 return v1;
165 struct isl_obj_vtable isl_obj_bool_vtable = {
166 isl_obj_bool_copy,
167 isl_obj_bool_add,
168 isl_obj_bool_print,
169 isl_obj_bool_free
171 #define isl_obj_bool (&isl_obj_bool_vtable)
173 int *isl_bool_from_int(int res)
175 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
178 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
179 __isl_take isl_union_map *map2)
181 return isl_union_map_is_subset(map2, map1);
183 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
184 __isl_take isl_union_set *set2)
186 return isl_union_set_is_subset(set2, set1);
189 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
190 __isl_take isl_union_map *map2)
192 return isl_union_map_is_strict_subset(map2, map1);
194 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
195 __isl_take isl_union_set *set2)
197 return isl_union_set_is_strict_subset(set2, set1);
200 extern struct isl_obj_vtable isl_obj_list_vtable;
201 #define isl_obj_list (&isl_obj_list_vtable)
203 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
204 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
205 struct isc_bin_op {
206 enum isl_token_type op;
207 isl_obj_type lhs;
208 isl_obj_type rhs;
209 isl_obj_type res;
210 union {
211 isc_bin_op_fn fn;
212 isc_bin_test_fn test;
213 } o;
215 struct isc_named_bin_op {
216 char *name;
217 struct isc_bin_op op;
220 struct iscc_at {
221 isl_union_pw_qpolynomial *upwqp;
222 isl_union_pw_qpolynomial *res;
225 static int eval_at(__isl_take isl_point *pnt, void *user)
227 struct iscc_at *at = (struct iscc_at *) user;
228 isl_qpolynomial *qp;
229 isl_set *set;
231 set = isl_set_from_point(isl_point_copy(pnt));
232 qp = isl_union_pw_qpolynomial_eval(
233 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
235 at->res = isl_union_pw_qpolynomial_add(at->res,
236 isl_union_pw_qpolynomial_from_pw_qpolynomial(
237 isl_pw_qpolynomial_alloc(set, qp)));
239 return 0;
242 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
243 __isl_take isl_union_pw_qpolynomial *upwqp,
244 __isl_take isl_union_set *uset)
246 struct iscc_at at;
248 at.upwqp = upwqp;
249 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
251 isl_union_set_foreach_point(uset, eval_at, &at);
253 isl_union_pw_qpolynomial_free(upwqp);
254 isl_union_set_free(uset);
256 return at.res;
259 struct iscc_fold_at {
260 isl_union_pw_qpolynomial_fold *upwf;
261 isl_union_pw_qpolynomial *res;
264 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
266 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
267 isl_qpolynomial *qp;
268 isl_set *set;
270 set = isl_set_from_point(isl_point_copy(pnt));
271 qp = isl_union_pw_qpolynomial_fold_eval(
272 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
274 at->res = isl_union_pw_qpolynomial_add(at->res,
275 isl_union_pw_qpolynomial_from_pw_qpolynomial(
276 isl_pw_qpolynomial_alloc(set, qp)));
278 return 0;
281 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
282 __isl_take isl_union_pw_qpolynomial_fold *upwf,
283 __isl_take isl_union_set *uset)
285 struct iscc_fold_at at;
287 at.upwf = upwf;
288 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
290 isl_union_set_foreach_point(uset, eval_fold_at, &at);
292 isl_union_pw_qpolynomial_fold_free(upwf);
293 isl_union_set_free(uset);
295 return at.res;
298 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
299 __isl_take isl_union_pw_qpolynomial *upwqp,
300 __isl_take isl_union_pw_qpolynomial_fold *upwf)
302 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
303 upwqp);
306 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
307 __isl_take isl_union_map *umap,
308 __isl_take isl_union_pw_qpolynomial_fold *upwf)
310 isl_ctx *ctx;
311 struct isl_list *list;
312 int tight;
314 ctx = isl_union_map_get_ctx(umap);
315 list = isl_list_alloc(ctx, 2);
316 if (!list)
317 goto error2;
319 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
320 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
321 upwf, &tight);
322 list->obj[1].type = isl_obj_bool;
323 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
324 if (tight < 0 || !list->obj[0].v)
325 goto error;
327 return list;
328 error2:
329 isl_union_map_free(umap);
330 isl_union_pw_qpolynomial_fold_free(upwf);
331 error:
332 isl_list_free(list);
333 return NULL;
336 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
337 __isl_take isl_union_set *uset,
338 __isl_take isl_union_pw_qpolynomial_fold *upwf)
340 isl_ctx *ctx;
341 struct isl_list *list;
342 int tight;
344 ctx = isl_union_set_get_ctx(uset);
345 list = isl_list_alloc(ctx, 2);
346 if (!list)
347 goto error2;
349 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
350 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
351 upwf, &tight);
352 list->obj[1].type = isl_obj_bool;
353 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
354 if (tight < 0 || !list->obj[0].v)
355 goto error;
357 return list;
358 error2:
359 isl_union_set_free(uset);
360 isl_union_pw_qpolynomial_fold_free(upwf);
361 error:
362 isl_list_free(list);
363 return NULL;
366 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_int_mul(
367 __isl_take isl_union_pw_qpolynomial *upwqp, __isl_take isl_int_obj *i)
369 isl_int v;
371 if (!i)
372 goto error;
374 isl_int_init(v);
375 isl_int_obj_get_int(i, &v);
376 upwqp = isl_union_pw_qpolynomial_mul_isl_int(upwqp, v);
377 isl_int_clear(v);
379 isl_int_obj_free(i);
381 return upwqp;
382 error:
383 isl_union_pw_qpolynomial_free(upwqp);
384 return NULL;
387 static __isl_give isl_union_pw_qpolynomial *int_union_pw_qpolynomial_mul(
388 __isl_take isl_int_obj *i, __isl_take isl_union_pw_qpolynomial *upwqp)
390 return union_pw_qpolynomial_int_mul(upwqp, i);
393 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_fold_int_mul(
394 __isl_take isl_union_pw_qpolynomial_fold *upwf,
395 __isl_take isl_int_obj *i)
397 isl_int v;
399 if (!i)
400 goto error;
402 isl_int_init(v);
403 isl_int_obj_get_int(i, &v);
404 upwf = isl_union_pw_qpolynomial_fold_mul_isl_int(upwf, v);
405 isl_int_clear(v);
407 isl_int_obj_free(i);
409 return upwf;
410 error:
411 isl_union_pw_qpolynomial_fold_free(upwf);
412 return NULL;
415 static __isl_give isl_union_pw_qpolynomial_fold *int_union_pw_qpolynomial_fold_mul(
416 __isl_take isl_int_obj *i,
417 __isl_take isl_union_pw_qpolynomial_fold *upwf)
419 return union_pw_qpolynomial_fold_int_mul(upwf, i);
422 struct isc_bin_op bin_ops[] = {
423 { '+', isl_obj_int, isl_obj_int, isl_obj_int,
424 (isc_bin_op_fn) &isl_int_obj_add },
425 { '-', isl_obj_int, isl_obj_int, isl_obj_int,
426 (isc_bin_op_fn) &isl_int_obj_sub },
427 { '*', isl_obj_int, isl_obj_int, isl_obj_int,
428 (isc_bin_op_fn) &isl_int_obj_mul },
429 { '+', isl_obj_union_set, isl_obj_union_set,
430 isl_obj_union_set,
431 (isc_bin_op_fn) &isl_union_set_union },
432 { '+', isl_obj_union_map, isl_obj_union_map,
433 isl_obj_union_map,
434 (isc_bin_op_fn) &isl_union_map_union },
435 { '-', isl_obj_union_set, isl_obj_union_set,
436 isl_obj_union_set,
437 (isc_bin_op_fn) &isl_union_set_subtract },
438 { '-', isl_obj_union_map, isl_obj_union_map,
439 isl_obj_union_map,
440 (isc_bin_op_fn) &isl_union_map_subtract },
441 { '*', isl_obj_union_set, isl_obj_union_set,
442 isl_obj_union_set,
443 (isc_bin_op_fn) &isl_union_set_intersect },
444 { '*', isl_obj_union_map, isl_obj_union_map,
445 isl_obj_union_map,
446 (isc_bin_op_fn) &isl_union_map_intersect },
447 { '*', isl_obj_union_map, isl_obj_union_set,
448 isl_obj_union_map,
449 (isc_bin_op_fn) &isl_union_map_intersect_domain },
450 { '.', isl_obj_union_map, isl_obj_union_map,
451 isl_obj_union_map,
452 (isc_bin_op_fn) &isl_union_map_apply_range },
453 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
454 isl_obj_union_pw_qpolynomial,
455 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
456 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
457 isl_obj_list,
458 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
459 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
460 isl_obj_union_map,
461 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
462 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
463 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
464 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
465 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
466 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
467 isl_obj_bool,
468 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
469 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
470 isl_obj_bool,
471 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
472 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
473 isl_obj_bool,
474 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
475 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
476 isl_obj_bool,
477 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
478 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
479 isl_obj_bool,
480 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
481 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
482 isl_obj_bool,
483 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
484 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
485 isl_obj_bool,
486 { .test =
487 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
488 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
489 isl_obj_bool,
490 { .test =
491 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
492 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
493 isl_obj_union_map,
494 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
495 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
496 isl_obj_union_map,
497 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
498 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
499 isl_obj_union_map,
500 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
501 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
502 isl_obj_union_map,
503 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
504 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
505 isl_obj_union_map,
506 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
507 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
508 isl_obj_union_map,
509 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
510 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
511 isl_obj_union_map,
512 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
513 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
514 isl_obj_union_map,
515 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
516 { '.', isl_obj_union_pw_qpolynomial_fold,
517 isl_obj_union_pw_qpolynomial_fold,
518 isl_obj_union_pw_qpolynomial_fold,
519 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
520 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
521 isl_obj_union_pw_qpolynomial,
522 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
523 { '+', isl_obj_union_pw_qpolynomial,
524 isl_obj_union_pw_qpolynomial_fold,
525 isl_obj_union_pw_qpolynomial_fold,
526 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
527 { '+', isl_obj_union_pw_qpolynomial_fold,
528 isl_obj_union_pw_qpolynomial,
529 isl_obj_union_pw_qpolynomial_fold,
530 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
531 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
532 isl_obj_union_pw_qpolynomial,
533 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
534 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial,
535 isl_obj_union_pw_qpolynomial,
536 (isc_bin_op_fn) &int_union_pw_qpolynomial_mul },
537 { '*', isl_obj_union_pw_qpolynomial, isl_obj_int,
538 isl_obj_union_pw_qpolynomial,
539 (isc_bin_op_fn) &union_pw_qpolynomial_int_mul },
540 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial_fold,
541 isl_obj_union_pw_qpolynomial_fold,
542 (isc_bin_op_fn) &int_union_pw_qpolynomial_fold_mul },
543 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_int,
544 isl_obj_union_pw_qpolynomial_fold,
545 (isc_bin_op_fn) &union_pw_qpolynomial_fold_int_mul },
546 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
547 isl_obj_union_pw_qpolynomial,
548 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
549 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
550 isl_obj_union_pw_qpolynomial,
551 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
552 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
553 isl_obj_union_pw_qpolynomial_fold,
554 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
555 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
556 isl_obj_union_pw_qpolynomial,
557 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
558 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
559 isl_obj_union_pw_qpolynomial,
560 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
561 { '%', isl_obj_union_set, isl_obj_union_set,
562 isl_obj_union_set,
563 (isc_bin_op_fn) &isl_union_set_gist },
564 { '%', isl_obj_union_map, isl_obj_union_map,
565 isl_obj_union_map,
566 (isc_bin_op_fn) &isl_union_map_gist },
567 { '%', isl_obj_union_map, isl_obj_union_set,
568 isl_obj_union_map,
569 (isc_bin_op_fn) &isl_union_map_gist_domain },
570 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
571 isl_obj_union_pw_qpolynomial,
572 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
573 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
574 isl_obj_union_pw_qpolynomial_fold,
575 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
576 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
577 isl_obj_union_pw_qpolynomial, isl_obj_bool,
578 { .test = (isc_bin_test_fn)
579 &isl_union_pw_qpolynomial_plain_is_equal } },
580 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
581 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
582 { .test = (isc_bin_test_fn)
583 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
584 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
585 (isc_bin_op_fn) &isl_str_concat },
589 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
590 __isl_take isl_union_map *umap2)
592 return isl_union_map_apply_range(umap2, umap1);
595 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
596 __isl_take isl_union_pw_qpolynomial *upwqp,
597 __isl_take isl_union_map *umap)
599 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
602 static __isl_give struct isl_list *qpolynomial_fold_after_map(
603 __isl_take isl_union_pw_qpolynomial_fold *upwf,
604 __isl_take isl_union_map *umap)
606 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
609 struct isc_named_bin_op named_bin_ops[] = {
610 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
611 isl_obj_union_map,
612 (isc_bin_op_fn) &map_after_map } },
613 { "after", { -1, isl_obj_union_pw_qpolynomial,
614 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
615 (isc_bin_op_fn) &qpolynomial_after_map } },
616 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
617 isl_obj_union_map, isl_obj_list,
618 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
619 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
620 isl_obj_union_map,
621 (isc_bin_op_fn) &isl_union_map_apply_range } },
622 { "before", { -1, isl_obj_union_map,
623 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
624 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
625 { "before", { -1, isl_obj_union_map,
626 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
627 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
628 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
629 isl_obj_union_set,
630 (isc_bin_op_fn) &isl_union_set_product } },
631 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
632 isl_obj_union_map,
633 (isc_bin_op_fn) &isl_union_map_product } },
634 NULL
637 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
639 return isl_set_from_basic_set(isl_union_set_sample(uset));
642 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
644 return isl_map_from_basic_map(isl_union_map_sample(umap));
647 static __isl_give struct isl_list *union_map_power(
648 __isl_take isl_union_map *umap)
650 isl_ctx *ctx;
651 struct isl_list *list;
652 int exact;
654 ctx = isl_union_map_get_ctx(umap);
655 list = isl_list_alloc(ctx, 2);
656 if (!list)
657 goto error2;
659 list->obj[0].type = isl_obj_union_map;
660 list->obj[0].v = isl_union_map_power(umap, &exact);
661 list->obj[1].type = isl_obj_bool;
662 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
663 if (exact < 0 || !list->obj[0].v)
664 goto error;
666 return list;
667 error2:
668 isl_union_map_free(umap);
669 error:
670 isl_list_free(list);
671 return NULL;
674 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
675 __isl_take isl_union_pw_qpolynomial *upwqp)
677 isl_ctx *ctx;
678 struct isl_list *list;
679 int tight;
681 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
682 list = isl_list_alloc(ctx, 2);
683 if (!list)
684 goto error2;
686 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
687 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
688 isl_fold_max, &tight);
689 list->obj[1].type = isl_obj_bool;
690 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
691 if (tight < 0 || !list->obj[0].v)
692 goto error;
694 return list;
695 error2:
696 isl_union_pw_qpolynomial_free(upwqp);
697 error:
698 isl_list_free(list);
699 return NULL;
702 #ifdef HAVE_PET
703 static __isl_give isl_list *parse(__isl_take isl_str *str)
705 isl_ctx *ctx;
706 struct isl_list *list;
707 struct pet_scop *scop;
708 isl_union_map *sched, *reads, *writes;
709 isl_union_set *domain;
710 struct iscc_options *options;
712 if (!str)
713 return NULL;
714 ctx = str->ctx;
716 options = isl_ctx_peek_iscc_options(ctx);
717 if (!options || !options->io) {
718 isl_str_free(str);
719 isl_die(ctx, isl_error_invalid,
720 "parse_file operation not allowed", return NULL);
723 list = isl_list_alloc(ctx, 4);
724 if (!list)
725 goto error;
727 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
728 domain = pet_scop_collect_domains(scop);
729 sched = pet_scop_collect_schedule(scop);
730 reads = pet_scop_collect_reads(scop);
731 writes = pet_scop_collect_writes(scop);
732 pet_scop_free(scop);
734 list->obj[0].type = isl_obj_union_set;
735 list->obj[0].v = domain;
736 list->obj[1].type = isl_obj_union_map;
737 list->obj[1].v = writes;
738 list->obj[2].type = isl_obj_union_map;
739 list->obj[2].v = reads;
740 list->obj[3].type = isl_obj_union_map;
741 list->obj[3].v = sched;
743 if (!list->obj[0].v || !list->obj[1].v ||
744 !list->obj[2].v || !list->obj[3].v)
745 goto error;
747 isl_str_free(str);
748 return list;
749 error:
750 isl_list_free(list);
751 isl_str_free(str);
752 return NULL;
754 #endif
756 static int add_point(__isl_take isl_point *pnt, void *user)
758 isl_union_set **scan = (isl_union_set **) user;
760 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
762 return 0;
765 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
767 isl_union_set *scan;
769 scan = isl_union_set_empty(isl_union_set_get_space(uset));
771 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
772 isl_union_set_free(scan);
773 return uset;
776 isl_union_set_free(uset);
777 return scan;
780 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
782 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
785 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
786 __isl_take isl_union_pw_qpolynomial *upwqp)
788 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
791 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
792 __isl_take isl_union_pw_qpolynomial *upwqp)
794 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
797 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
798 __isl_take isl_union_pw_qpolynomial *upwqp)
800 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
803 typedef void *(*isc_un_op_fn)(void *arg);
804 struct isc_un_op {
805 enum isl_token_type op;
806 isl_obj_type arg;
807 isl_obj_type res;
808 isc_un_op_fn fn;
810 struct isc_named_un_op {
811 char *name;
812 struct isc_un_op op;
814 struct isc_named_un_op named_un_ops[] = {
815 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
816 (isc_un_op_fn) &isl_union_map_affine_hull } },
817 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
818 (isc_un_op_fn) &isl_union_set_affine_hull } },
819 {"card", { -1, isl_obj_union_set,
820 isl_obj_union_pw_qpolynomial,
821 (isc_un_op_fn) &isl_union_set_card } },
822 {"card", { -1, isl_obj_union_map,
823 isl_obj_union_pw_qpolynomial,
824 (isc_un_op_fn) &isl_union_map_card } },
825 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
826 (isc_un_op_fn) &isl_union_set_coalesce } },
827 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
828 (isc_un_op_fn) &isl_union_map_coalesce } },
829 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
830 isl_obj_union_pw_qpolynomial,
831 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
832 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
833 isl_obj_union_pw_qpolynomial_fold,
834 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
835 {"coefficients", { -1, isl_obj_union_set,
836 isl_obj_union_set,
837 (isc_un_op_fn) &isl_union_set_coefficients } },
838 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
839 (isc_un_op_fn) &isl_union_set_solutions } },
840 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
841 (isc_un_op_fn) &isl_union_map_deltas } },
842 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
843 (isc_un_op_fn) &isl_union_map_deltas_map } },
844 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
845 (isc_un_op_fn) &isl_union_map_domain } },
846 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
847 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
848 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
849 isl_obj_union_set,
850 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
851 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
852 (isc_un_op_fn) &isl_union_map_domain } },
853 {"domain", { -1, isl_obj_union_pw_qpolynomial,
854 isl_obj_union_set,
855 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
856 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
857 isl_obj_union_set,
858 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
859 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
860 (isc_un_op_fn) &isl_union_map_domain_map } },
861 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
862 (isc_un_op_fn) &isl_union_map_range } },
863 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
864 (isc_un_op_fn) &isl_union_map_range } },
865 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
866 (isc_un_op_fn) &isl_union_map_range_map } },
867 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
868 (isc_un_op_fn) &isl_union_set_identity } },
869 {"lattice_width", { -1, isl_obj_union_set,
870 isl_obj_union_pw_qpolynomial,
871 (isc_un_op_fn) &isl_union_set_lattice_width } },
872 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
873 (isc_un_op_fn) &isl_union_map_lexmin } },
874 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
875 (isc_un_op_fn) &isl_union_map_lexmax } },
876 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
877 (isc_un_op_fn) &isl_union_set_lexmin } },
878 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
879 (isc_un_op_fn) &isl_union_set_lexmax } },
880 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
881 (isc_un_op_fn) &isl_union_set_lift } },
882 {"params", { -1, isl_obj_union_map, isl_obj_set,
883 (isc_un_op_fn) &isl_union_map_params } },
884 {"params", { -1, isl_obj_union_set, isl_obj_set,
885 (isc_un_op_fn) &isl_union_set_params } },
886 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
887 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
888 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
889 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
890 {"poly", { -1, isl_obj_union_pw_qpolynomial,
891 isl_obj_union_pw_qpolynomial,
892 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
893 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
894 isl_obj_union_pw_qpolynomial,
895 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
896 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
897 isl_obj_union_pw_qpolynomial,
898 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
899 #ifdef HAVE_PET
900 {"parse_file", { -1, isl_obj_str, isl_obj_list,
901 (isc_un_op_fn) &parse } },
902 #endif
903 {"pow", { -1, isl_obj_union_map, isl_obj_list,
904 (isc_un_op_fn) &union_map_power } },
905 {"sample", { -1, isl_obj_union_set, isl_obj_set,
906 (isc_un_op_fn) &union_set_sample } },
907 {"sample", { -1, isl_obj_union_map, isl_obj_map,
908 (isc_un_op_fn) &union_map_sample } },
909 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
910 (isc_un_op_fn) &union_set_scan } },
911 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
912 (isc_un_op_fn) &union_map_scan } },
913 {"sum", { -1, isl_obj_union_pw_qpolynomial,
914 isl_obj_union_pw_qpolynomial,
915 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
916 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
917 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
918 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
919 (isc_un_op_fn) &isl_union_set_unwrap } },
920 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
921 (isc_un_op_fn) &isl_union_map_wrap } },
922 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
923 (isc_un_op_fn) &isl_union_map_zip } },
924 NULL
927 struct isl_named_obj {
928 char *name;
929 struct isl_obj obj;
932 static void free_obj(struct isl_obj obj)
934 obj.type->free(obj.v);
937 static int same_name(const void *entry, const void *val)
939 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
941 return !strcmp(named->name, val);
944 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
945 char *name, struct isl_obj obj)
947 struct isl_hash_table_entry *entry;
948 uint32_t name_hash;
949 struct isl_named_obj *named;
951 name_hash = isl_hash_string(isl_hash_init(), name);
952 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
953 if (!entry)
954 goto error;
955 if (entry->data) {
956 named = entry->data;
957 free_obj(named->obj);
958 free(name);
959 } else {
960 named = isl_alloc_type(ctx, struct isl_named_obj);
961 if (!named)
962 goto error;
963 named->name = name;
964 entry->data = named;
966 named->obj = obj;
968 return 0;
969 error:
970 free_obj(obj);
971 free(name);
972 return -1;
975 static struct isl_obj stored_obj(struct isl_ctx *ctx,
976 struct isl_hash_table *table, char *name)
978 struct isl_obj obj = { isl_obj_none, NULL };
979 struct isl_hash_table_entry *entry;
980 uint32_t name_hash;
982 name_hash = isl_hash_string(isl_hash_init(), name);
983 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
984 if (entry) {
985 struct isl_named_obj *named;
986 named = entry->data;
987 obj = named->obj;
988 } else if (isdigit(name[0]))
989 fprintf(stderr, "unknown identifier '$%s'\n", name);
990 else
991 fprintf(stderr, "unknown identifier '%s'\n", name);
993 free(name);
994 obj.v = obj.type->copy(obj.v);
995 return obj;
998 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1000 if (obj.type == super)
1001 return 1;
1002 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1003 return 1;
1004 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1005 return 1;
1006 if (obj.type == isl_obj_pw_qpolynomial &&
1007 super == isl_obj_union_pw_qpolynomial)
1008 return 1;
1009 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1010 super == isl_obj_union_pw_qpolynomial_fold)
1011 return 1;
1012 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1013 return 1;
1014 if (obj.type == isl_obj_list) {
1015 struct isl_list *list = obj.v;
1016 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1017 return is_subtype(list->obj[0], super);
1019 if (super == isl_obj_str)
1020 return 1;
1021 return 0;
1024 static struct isl_obj obj_at(struct isl_obj obj, int i)
1026 struct isl_list *list = obj.v;
1028 obj = list->obj[i];
1029 obj.v = obj.type->copy(obj.v);
1031 isl_list_free(list);
1033 return obj;
1036 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1037 isl_obj_type type)
1039 if (obj.type == type)
1040 return obj;
1041 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1042 obj.type = isl_obj_union_map;
1043 obj.v = isl_union_map_from_map(obj.v);
1044 return obj;
1046 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1047 obj.type = isl_obj_union_set;
1048 obj.v = isl_union_set_from_set(obj.v);
1049 return obj;
1051 if (obj.type == isl_obj_pw_qpolynomial &&
1052 type == isl_obj_union_pw_qpolynomial) {
1053 obj.type = isl_obj_union_pw_qpolynomial;
1054 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1055 return obj;
1057 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1058 type == isl_obj_union_pw_qpolynomial_fold) {
1059 obj.type = isl_obj_union_pw_qpolynomial_fold;
1060 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1061 return obj;
1063 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1064 if (type == isl_obj_union_map) {
1065 obj.type = isl_obj_union_map;
1066 return obj;
1068 if (type == isl_obj_union_pw_qpolynomial) {
1069 isl_space *dim = isl_union_set_get_space(obj.v);
1070 isl_union_set_free(obj.v);
1071 obj.v = isl_union_pw_qpolynomial_zero(dim);
1072 obj.type = isl_obj_union_pw_qpolynomial;
1073 return obj;
1075 if (type == isl_obj_union_pw_qpolynomial_fold) {
1076 isl_space *dim = isl_union_set_get_space(obj.v);
1077 isl_union_set_free(obj.v);
1078 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1079 isl_fold_list);
1080 obj.type = isl_obj_union_pw_qpolynomial_fold;
1081 return obj;
1084 if (obj.type == isl_obj_list) {
1085 struct isl_list *list = obj.v;
1086 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1087 return convert(ctx, obj_at(obj, 0), type);
1089 if (type == isl_obj_str) {
1090 isl_str *str;
1091 isl_printer *p;
1092 char *s;
1094 p = isl_printer_to_str(ctx);
1095 if (!p)
1096 goto error;
1097 p = obj.type->print(p, obj.v);
1098 s = isl_printer_get_str(p);
1099 isl_printer_free(p);
1101 str = isl_str_from_string(ctx, s);
1102 if (!str)
1103 goto error;
1104 free_obj(obj);
1105 obj.v = str;
1106 obj.type = isl_obj_str;
1107 return obj;
1110 error:
1111 free_obj(obj);
1112 obj.type = isl_obj_none;
1113 obj.v = NULL;
1114 return obj;
1117 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1118 struct isl_obj lhs)
1120 int i;
1121 struct isl_token *tok;
1123 tok = isl_stream_next_token(s);
1124 if (!tok)
1125 return NULL;
1127 for (i = 0; ; ++i) {
1128 if (!bin_ops[i].op)
1129 break;
1130 if (bin_ops[i].op != tok->type)
1131 continue;
1132 if (!is_subtype(lhs, bin_ops[i].lhs))
1133 continue;
1135 isl_token_free(tok);
1136 return &bin_ops[i];
1139 for (i = 0; ; ++i) {
1140 if (!named_bin_ops[i].name)
1141 break;
1142 if (named_bin_ops[i].op.op != tok->type)
1143 continue;
1144 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1145 continue;
1147 isl_token_free(tok);
1148 return &named_bin_ops[i].op;
1151 isl_stream_push_token(s, tok);
1153 return NULL;
1156 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1158 int i;
1159 struct isl_token *tok;
1161 tok = isl_stream_next_token(s);
1162 if (!tok)
1163 return NULL;
1165 for (i = 0; ; ++i) {
1166 if (!named_un_ops[i].name)
1167 break;
1168 if (named_un_ops[i].op.op != tok->type)
1169 continue;
1171 isl_token_free(tok);
1172 return &named_un_ops[i].op;
1175 isl_stream_push_token(s, tok);
1177 return NULL;
1180 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1181 struct isl_obj arg)
1183 int i;
1185 for (i = 0; ; ++i) {
1186 if (!named_un_ops[i].name)
1187 break;
1188 if (named_un_ops[i].op.op != like->op)
1189 continue;
1190 if (!is_subtype(arg, named_un_ops[i].op.arg))
1191 continue;
1193 return &named_un_ops[i].op;
1196 return NULL;
1199 static int is_assign(struct isl_stream *s)
1201 struct isl_token *tok;
1202 struct isl_token *tok2;
1203 int assign;
1205 tok = isl_stream_next_token(s);
1206 if (!tok)
1207 return 0;
1208 if (tok->type != ISL_TOKEN_IDENT) {
1209 isl_stream_push_token(s, tok);
1210 return 0;
1213 tok2 = isl_stream_next_token(s);
1214 if (!tok2) {
1215 isl_stream_push_token(s, tok);
1216 return 0;
1218 assign = tok2->type == ISL_TOKEN_DEF;
1219 isl_stream_push_token(s, tok2);
1220 isl_stream_push_token(s, tok);
1222 return assign;
1225 static struct isl_obj read_obj(struct isl_stream *s,
1226 struct isl_hash_table *table);
1227 static struct isl_obj read_expr(struct isl_stream *s,
1228 struct isl_hash_table *table);
1230 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1231 struct isl_hash_table *table, struct isc_un_op *op)
1233 struct isl_obj obj = { isl_obj_none, NULL };
1235 obj = read_obj(s, table);
1236 if (!obj.v)
1237 goto error;
1239 op = find_matching_un_op(op, obj);
1241 if (!op)
1242 isl_die(s->ctx, isl_error_invalid,
1243 "no such unary operator defined on given operand",
1244 goto error);
1246 obj = convert(s->ctx, obj, op->arg);
1247 obj.v = op->fn(obj.v);
1248 obj.type = op->res;
1250 return obj;
1251 error:
1252 free_obj(obj);
1253 obj.type = isl_obj_none;
1254 obj.v = NULL;
1255 return obj;
1258 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1260 struct isl_list *list;
1261 int exact;
1263 if (obj.type != isl_obj_union_map)
1264 obj = convert(ctx, obj, isl_obj_union_map);
1265 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1266 list = isl_list_alloc(ctx, 2);
1267 if (!list)
1268 goto error;
1270 list->obj[0].type = isl_obj_union_map;
1271 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1272 list->obj[1].type = isl_obj_bool;
1273 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1274 obj.v = list;
1275 obj.type = isl_obj_list;
1276 if (exact < 0 || !list->obj[0].v)
1277 goto error;
1279 return obj;
1280 error:
1281 free_obj(obj);
1282 obj.type = isl_obj_none;
1283 obj.v = NULL;
1284 return obj;
1287 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1289 struct isl_list *list = obj.v;
1290 struct isl_token *tok;
1291 int i;
1293 tok = isl_stream_next_token(s);
1294 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1295 isl_stream_error(s, tok, "expecting index");
1296 if (tok)
1297 isl_stream_push_token(s, tok);
1298 goto error;
1300 i = isl_int_get_si(tok->u.v);
1301 isl_token_free(tok);
1302 isl_assert(s->ctx, i < list->n, goto error);
1303 if (isl_stream_eat(s, ']'))
1304 goto error;
1306 return obj_at(obj, i);
1307 error:
1308 free_obj(obj);
1309 obj.type = isl_obj_none;
1310 obj.v = NULL;
1311 return obj;
1314 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1315 struct isl_hash_table *table)
1317 struct isl_obj obj;
1319 obj = read_expr(s, table);
1320 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1321 is_subtype(obj, isl_obj_union_map), goto error);
1323 if (obj.type == isl_obj_list) {
1324 struct isl_list *list = obj.v;
1325 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1326 obj = obj_at(obj, 0);
1328 if (obj.type == isl_obj_set)
1329 obj = convert(s->ctx, obj, isl_obj_union_set);
1330 else if (obj.type == isl_obj_map)
1331 obj = convert(s->ctx, obj, isl_obj_union_map);
1332 if (obj.type == isl_obj_union_set) {
1333 obj.v = isl_union_set_apply(obj.v, umap);
1334 } else
1335 obj.v = isl_union_map_apply_range(obj.v, umap);
1336 if (!obj.v)
1337 goto error2;
1339 if (isl_stream_eat(s, ')'))
1340 goto error2;
1342 return obj;
1343 error:
1344 isl_union_map_free(umap);
1345 error2:
1346 free_obj(obj);
1347 obj.type = isl_obj_none;
1348 obj.v = NULL;
1349 return obj;
1352 static struct isl_obj apply_fun_set(struct isl_obj obj,
1353 __isl_take isl_union_set *uset)
1355 if (obj.type == isl_obj_union_pw_qpolynomial) {
1356 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1357 } else {
1358 obj.type = isl_obj_list;
1359 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1361 return obj;
1364 static struct isl_obj apply_fun_map(struct isl_obj obj,
1365 __isl_take isl_union_map *umap)
1367 if (obj.type == isl_obj_union_pw_qpolynomial) {
1368 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1369 } else {
1370 obj.type = isl_obj_list;
1371 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1373 return obj;
1376 static struct isl_obj apply_fun(struct isl_stream *s,
1377 struct isl_obj obj, struct isl_hash_table *table)
1379 struct isl_obj arg;
1381 arg = read_expr(s, table);
1382 if (!is_subtype(arg, isl_obj_union_map) &&
1383 !is_subtype(arg, isl_obj_union_set))
1384 isl_die(s->ctx, isl_error_invalid,
1385 "expecting set of map argument", goto error);
1387 if (arg.type == isl_obj_list) {
1388 struct isl_list *list = arg.v;
1389 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1390 arg = obj_at(arg, 0);
1392 if (arg.type == isl_obj_set)
1393 arg = convert(s->ctx, arg, isl_obj_union_set);
1394 else if (arg.type == isl_obj_map)
1395 arg = convert(s->ctx, arg, isl_obj_union_map);
1396 if (arg.type == isl_obj_union_set)
1397 obj = apply_fun_set(obj, arg.v);
1398 else
1399 obj = apply_fun_map(obj, arg.v);
1400 if (!obj.v)
1401 goto error2;
1403 if (isl_stream_eat(s, ')'))
1404 goto error2;
1406 return obj;
1407 error:
1408 free_obj(arg);
1409 error2:
1410 free_obj(obj);
1411 obj.type = isl_obj_none;
1412 obj.v = NULL;
1413 return obj;
1416 struct add_vertex_data {
1417 struct isl_list *list;
1418 int i;
1421 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1423 struct add_vertex_data *data = (struct add_vertex_data *)user;
1424 isl_basic_set *expr;
1426 expr = isl_vertex_get_expr(vertex);
1428 data->list->obj[data->i].type = isl_obj_set;
1429 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1430 data->i++;
1432 isl_vertex_free(vertex);
1434 return 0;
1437 static int set_vertices(__isl_take isl_set *set, void *user)
1439 isl_ctx *ctx;
1440 isl_basic_set *hull;
1441 isl_vertices *vertices = NULL;
1442 struct isl_list *list = NULL;
1443 int r;
1444 struct add_vertex_data *data = (struct add_vertex_data *)user;
1446 set = isl_set_remove_divs(set);
1447 hull = isl_set_convex_hull(set);
1448 vertices = isl_basic_set_compute_vertices(hull);
1449 isl_basic_set_free(hull);
1451 list = data->list;
1453 ctx = isl_vertices_get_ctx(vertices);
1454 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1455 if (!data->list)
1456 goto error;
1458 data->i = 0;
1459 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1461 data->list = isl_list_concat(list, data->list);
1463 isl_vertices_free(vertices);
1465 return r;
1466 error:
1467 data->list = list;
1468 isl_vertices_free(vertices);
1469 return -1;
1472 static struct isl_obj vertices(struct isl_stream *s,
1473 struct isl_hash_table *table)
1475 isl_ctx *ctx;
1476 struct isl_obj obj;
1477 struct isl_list *list = NULL;
1478 isl_union_set *uset;
1479 struct add_vertex_data data = { NULL };
1481 obj = read_expr(s, table);
1482 obj = convert(s->ctx, obj, isl_obj_union_set);
1483 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1484 uset = obj.v;
1485 obj.v = NULL;
1487 ctx = isl_union_set_get_ctx(uset);
1488 list = isl_list_alloc(ctx, 0);
1489 if (!list)
1490 goto error;
1492 data.list = list;
1494 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1495 goto error;
1497 isl_union_set_free(uset);
1499 obj.type = isl_obj_list;
1500 obj.v = data.list;
1502 return obj;
1503 error:
1504 isl_union_set_free(uset);
1505 isl_list_free(data.list);
1506 free_obj(obj);
1507 obj.type = isl_obj_none;
1508 obj.v = NULL;
1509 return obj;
1512 static struct isl_obj type_of(struct isl_stream *s,
1513 struct isl_hash_table *table)
1515 isl_ctx *ctx;
1516 struct isl_obj obj;
1517 const char *type = "unknown";
1519 obj = read_expr(s, table);
1521 if (obj.type == isl_obj_map ||
1522 obj.type == isl_obj_union_map)
1523 type = "map";
1524 if (obj.type == isl_obj_set ||
1525 obj.type == isl_obj_union_set)
1526 type = "set";
1527 if (obj.type == isl_obj_pw_qpolynomial ||
1528 obj.type == isl_obj_union_pw_qpolynomial)
1529 type = "piecewise quasipolynomial";
1530 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1531 obj.type == isl_obj_union_pw_qpolynomial_fold)
1532 type = "piecewise quasipolynomial fold";
1533 if (obj.type == isl_obj_list)
1534 type = "list";
1535 if (obj.type == isl_obj_bool)
1536 type = "boolean";
1537 if (obj.type == isl_obj_str)
1538 type = "string";
1539 if (obj.type == isl_obj_int)
1540 type = "int";
1542 free_obj(obj);
1543 obj.type = isl_obj_str;
1544 obj.v = isl_str_from_string(s->ctx, strdup(type));
1546 return obj;
1549 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1550 struct isl_hash_table *table)
1552 struct isl_obj obj;
1554 obj = read_obj(s, table);
1555 obj = convert(s->ctx, obj, isl_obj_union_set);
1556 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1557 return obj.v;
1558 error:
1559 free_obj(obj);
1560 return NULL;
1563 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1564 struct isl_hash_table *table)
1566 struct isl_obj obj;
1568 obj = read_obj(s, table);
1569 obj = convert(s->ctx, obj, isl_obj_union_map);
1570 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1571 return obj.v;
1572 error:
1573 free_obj(obj);
1574 return NULL;
1577 static struct isl_obj last_any(struct isl_stream *s,
1578 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1579 __isl_take isl_union_map *may_source)
1581 struct isl_obj obj = { isl_obj_none, NULL };
1582 isl_union_map *sink = NULL;
1583 isl_union_map *schedule = NULL;
1584 isl_union_map *may_dep;
1585 isl_union_map *must_dep;
1587 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1588 goto error;
1590 sink = read_map(s, table);
1591 if (!sink)
1592 goto error;
1594 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1595 goto error;
1597 schedule = read_map(s, table);
1598 if (!schedule)
1599 goto error;
1601 if (isl_union_map_compute_flow(sink, must_source, may_source,
1602 schedule, &must_dep, &may_dep,
1603 NULL, NULL) < 0)
1604 return obj;
1606 obj.type = isl_obj_union_map;
1607 obj.v = isl_union_map_union(must_dep, may_dep);
1609 return obj;
1610 error:
1611 isl_union_map_free(may_source);
1612 isl_union_map_free(must_source);
1613 isl_union_map_free(sink);
1614 isl_union_map_free(schedule);
1615 free_obj(obj);
1616 obj.type = isl_obj_none;
1617 obj.v = NULL;
1618 return obj;
1621 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1623 struct isl_obj obj = { isl_obj_none, NULL };
1624 isl_union_map *must_source = NULL;
1625 isl_union_map *may_source = NULL;
1626 isl_union_map *sink = NULL;
1627 isl_union_map *schedule = NULL;
1628 isl_union_map *may_dep;
1630 may_source = read_map(s, table);
1631 if (!may_source)
1632 goto error;
1634 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1635 must_source = read_map(s, table);
1636 if (!must_source)
1637 goto error;
1638 return last_any(s, table, must_source, may_source);
1641 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1642 goto error;
1644 sink = read_map(s, table);
1645 if (!sink)
1646 goto error;
1648 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1649 goto error;
1651 schedule = read_map(s, table);
1652 if (!schedule)
1653 goto error;
1655 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1656 if (isl_union_map_compute_flow(sink, must_source, may_source,
1657 schedule, NULL, &may_dep,
1658 NULL, NULL) < 0)
1659 return obj;
1661 obj.type = isl_obj_union_map;
1662 obj.v = may_dep;
1664 return obj;
1665 error:
1666 isl_union_map_free(may_source);
1667 isl_union_map_free(must_source);
1668 isl_union_map_free(sink);
1669 isl_union_map_free(schedule);
1670 free_obj(obj);
1671 obj.type = isl_obj_none;
1672 obj.v = NULL;
1673 return obj;
1676 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1678 struct isl_obj obj = { isl_obj_none, NULL };
1679 struct isl_list *list = NULL;
1680 isl_union_map *must_source = NULL;
1681 isl_union_map *may_source = NULL;
1682 isl_union_map *sink = NULL;
1683 isl_union_map *schedule = NULL;
1684 isl_union_map *must_dep;
1685 isl_union_map *must_no_source;
1687 must_source = read_map(s, table);
1688 if (!must_source)
1689 goto error;
1691 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1692 may_source = read_map(s, table);
1693 if (!may_source)
1694 goto error;
1695 return last_any(s, table, must_source, may_source);
1698 list = isl_list_alloc(s->ctx, 2);
1699 if (!list)
1700 goto error;
1702 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1703 goto error;
1705 sink = read_map(s, table);
1706 if (!sink)
1707 goto error;
1709 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1710 goto error;
1712 schedule = read_map(s, table);
1713 if (!schedule)
1714 goto error;
1716 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1717 if (isl_union_map_compute_flow(sink, must_source, may_source,
1718 schedule, &must_dep, NULL,
1719 &must_no_source, NULL) < 0) {
1720 isl_list_free(list);
1721 return obj;
1724 list->obj[0].type = isl_obj_union_map;
1725 list->obj[0].v = must_dep;
1726 list->obj[1].type = isl_obj_union_map;
1727 list->obj[1].v = must_no_source;
1729 obj.v = list;
1730 obj.type = isl_obj_list;
1732 return obj;
1733 error:
1734 isl_list_free(list);
1735 isl_union_map_free(may_source);
1736 isl_union_map_free(must_source);
1737 isl_union_map_free(sink);
1738 isl_union_map_free(schedule);
1739 free_obj(obj);
1740 obj.type = isl_obj_none;
1741 obj.v = NULL;
1742 return obj;
1745 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1746 struct isl_hash_table *table)
1748 isl_union_set *domain;
1749 isl_union_map *validity;
1750 isl_union_map *proximity;
1752 domain = read_set(s, table);
1753 if (!domain)
1754 return NULL;
1756 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1757 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1759 for (;;) {
1760 isl_union_map *umap;
1761 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1762 umap = read_map(s, table);
1763 validity = isl_union_map_union(validity, umap);
1764 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1765 umap = read_map(s, table);
1766 proximity = isl_union_map_union(proximity, umap);
1767 } else
1768 break;
1771 return isl_union_set_compute_schedule(domain, validity, proximity);
1774 static struct isl_obj schedule(struct isl_stream *s,
1775 struct isl_hash_table *table)
1777 struct isl_obj obj = { isl_obj_none, NULL };
1778 isl_schedule *schedule;
1780 schedule = get_schedule(s, table);
1782 obj.v = isl_schedule_get_map(schedule);
1783 obj.type = isl_obj_union_map;
1785 isl_schedule_free(schedule);
1787 return obj;
1790 /* Read a schedule for code generation.
1791 * If the input is a set rather than a map, then we construct
1792 * an identity schedule on the given set.
1794 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1795 struct isl_hash_table *table)
1797 struct isl_obj obj;
1799 obj = read_obj(s, table);
1801 if (is_subtype(obj, isl_obj_union_map)) {
1802 obj = convert(s->ctx, obj, isl_obj_union_map);
1803 return obj.v;
1806 if (is_subtype(obj, isl_obj_union_set)) {
1807 obj = convert(s->ctx, obj, isl_obj_union_set);
1808 return isl_union_set_identity(obj.v);
1811 free_obj(obj);
1812 isl_die(s->ctx, isl_error_invalid, "expecting set or map", return NULL);
1815 /* Generate an AST for the given schedule and options and print
1816 * the AST on the printer.
1818 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1819 __isl_take isl_union_map *schedule,
1820 __isl_take isl_union_map *options)
1822 isl_set *context;
1823 isl_ast_build *build;
1824 isl_ast_node *tree;
1825 int format;
1827 context = isl_set_universe(isl_union_map_get_space(schedule));
1829 build = isl_ast_build_from_context(context);
1830 build = isl_ast_build_set_options(build, options);
1831 tree = isl_ast_build_ast_from_schedule(build, schedule);
1832 isl_ast_build_free(build);
1834 if (!tree)
1835 return p;
1837 format = isl_printer_get_output_format(p);
1838 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1839 p = isl_printer_print_ast_node(p, tree);
1840 p = isl_printer_set_output_format(p, format);
1842 isl_ast_node_free(tree);
1844 return p;
1847 /* Perform the codegen operation.
1848 * In particular, read a schedule, check if the user has specified any options
1849 * and then generate an AST from the schedule (and options) and print it.
1851 static __isl_give isl_printer *codegen(struct isl_stream *s,
1852 struct isl_hash_table *table, __isl_take isl_printer *p)
1854 isl_union_map *schedule;
1855 isl_union_map *options;
1857 schedule = get_codegen_schedule(s, table);
1858 if (!schedule)
1859 return p;
1861 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1862 options = read_map(s, table);
1863 else
1864 options = isl_union_map_empty(
1865 isl_union_map_get_space(schedule));
1867 p = print_code(p, schedule, options);
1869 isl_stream_eat(s, ';');
1871 return p;
1874 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1876 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1878 struct isl_obj obj = { isl_obj_none, NULL };
1879 isl_ctx *ctx = isl_band_get_ctx(band);
1880 struct isl_list *list;
1882 list = isl_list_alloc(ctx, 2);
1883 if (!list)
1884 goto error;
1886 obj.v = list;
1887 obj.type = isl_obj_list;
1889 list->obj[0].type = isl_obj_union_map;
1890 list->obj[0].v = isl_band_get_partial_schedule(band);
1892 if (isl_band_has_children(band)) {
1893 isl_band_list *children;
1895 children = isl_band_get_children(band);
1896 list->obj[1] = band_list_to_obj_list(children);
1897 } else {
1898 list->obj[1].type = isl_obj_list;
1899 list->obj[1].v = isl_list_alloc(ctx, 0);
1902 if (!list->obj[0].v || !list->obj[1].v)
1903 goto error;
1905 isl_band_free(band);
1907 return obj;
1908 error:
1909 isl_band_free(band);
1910 free_obj(obj);
1911 obj.type = isl_obj_none;
1912 obj.v = NULL;
1913 return obj;
1916 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1918 struct isl_obj obj = { isl_obj_none, NULL };
1919 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1920 struct isl_list *list;
1921 int i, n;
1923 n = isl_band_list_n_band(bands);
1924 list = isl_list_alloc(ctx, n);
1925 if (!list)
1926 goto error;
1928 obj.v = list;
1929 obj.type = isl_obj_list;
1931 for (i = 0; i < n; ++i) {
1932 isl_band *band;
1934 band = isl_band_list_get_band(bands, i);
1935 list->obj[i] = band_to_obj_list(band);
1936 if (!list->obj[i].v)
1937 goto error;
1940 isl_band_list_free(bands);
1942 return obj;
1943 error:
1944 isl_band_list_free(bands);
1945 free_obj(obj);
1946 obj.type = isl_obj_none;
1947 obj.v = NULL;
1948 return obj;
1951 static struct isl_obj schedule_forest(struct isl_stream *s,
1952 struct isl_hash_table *table)
1954 struct isl_obj obj = { isl_obj_none, NULL };
1955 isl_schedule *schedule;
1956 isl_band_list *roots;
1958 schedule = get_schedule(s, table);
1959 if (!schedule)
1960 return obj;
1962 roots = isl_schedule_get_band_forest(schedule);
1963 isl_schedule_free(schedule);
1965 return band_list_to_obj_list(roots);
1968 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1970 struct isl_token *tok;
1972 if (isl_stream_eat_if_available(s, '+'))
1973 return transitive_closure(s->ctx, obj);
1975 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1976 if (obj.type != isl_obj_union_map)
1977 obj = convert(s->ctx, obj, isl_obj_union_map);
1979 tok = isl_stream_next_token(s);
1980 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_is_zero(tok->u.v)) {
1981 isl_stream_error(s, tok, "expecting non-zero integer exponent");
1982 if (tok)
1983 isl_stream_push_token(s, tok);
1984 goto error;
1987 obj.v = isl_union_map_fixed_power(obj.v, tok->u.v);
1988 isl_token_free(tok);
1989 if (!obj.v)
1990 goto error;
1992 return obj;
1993 error:
1994 free_obj(obj);
1995 obj.type = isl_obj_none;
1996 obj.v = NULL;
1997 return obj;
2000 static struct isl_obj check_assert(struct isl_stream *s,
2001 struct isl_hash_table *table)
2003 struct isl_obj obj;
2005 obj = read_expr(s, table);
2006 if (obj.type != isl_obj_bool)
2007 isl_die(s->ctx, isl_error_invalid,
2008 "expecting boolean expression", goto error);
2009 if (obj.v != &isl_bool_true)
2010 isl_die(s->ctx, isl_error_unknown,
2011 "assertion failed", abort());
2012 error:
2013 free_obj(obj);
2014 obj.type = isl_obj_none;
2015 obj.v = NULL;
2016 return obj;
2019 static struct isl_obj read_from_file(struct isl_stream *s)
2021 struct isl_obj obj;
2022 struct isl_token *tok;
2023 struct isl_stream *s_file;
2024 struct iscc_options *options;
2025 FILE *file;
2027 tok = isl_stream_next_token(s);
2028 if (!tok || tok->type != ISL_TOKEN_STRING) {
2029 isl_stream_error(s, tok, "expecting filename");
2030 isl_token_free(tok);
2031 goto error;
2034 options = isl_ctx_peek_iscc_options(s->ctx);
2035 if (!options || !options->io) {
2036 isl_token_free(tok);
2037 isl_die(s->ctx, isl_error_invalid,
2038 "read operation not allowed", goto error);
2041 file = fopen(tok->u.s, "r");
2042 isl_token_free(tok);
2043 isl_assert(s->ctx, file, goto error);
2045 s_file = isl_stream_new_file(s->ctx, file);
2046 if (!s_file) {
2047 fclose(file);
2048 goto error;
2051 obj = isl_stream_read_obj(s_file);
2053 isl_stream_free(s_file);
2054 fclose(file);
2056 return obj;
2057 error:
2058 obj.type = isl_obj_none;
2059 obj.v = NULL;
2060 return obj;
2063 static struct isl_obj write_to_file(struct isl_stream *s,
2064 struct isl_hash_table *table)
2066 struct isl_obj obj;
2067 struct isl_token *tok;
2068 struct isl_stream *s_file;
2069 struct iscc_options *options;
2070 FILE *file;
2071 isl_printer *p;
2073 tok = isl_stream_next_token(s);
2074 if (!tok || tok->type != ISL_TOKEN_STRING) {
2075 isl_stream_error(s, tok, "expecting filename");
2076 isl_token_free(tok);
2077 goto error;
2080 obj = read_expr(s, table);
2082 options = isl_ctx_peek_iscc_options(s->ctx);
2083 if (!options || !options->io) {
2084 isl_token_free(tok);
2085 isl_die(s->ctx, isl_error_invalid,
2086 "write operation not allowed", goto error);
2089 file = fopen(tok->u.s, "w");
2090 isl_token_free(tok);
2091 if (!file)
2092 isl_die(s->ctx, isl_error_unknown,
2093 "could not open file for writing", goto error);
2095 p = isl_printer_to_file(s->ctx, file);
2096 p = isl_printer_set_output_format(p, options->format);
2097 p = obj.type->print(p, obj.v);
2098 p = isl_printer_end_line(p);
2099 isl_printer_free(p);
2101 fclose(file);
2102 error:
2103 free_obj(obj);
2104 obj.type = isl_obj_none;
2105 obj.v = NULL;
2106 return obj;
2109 static struct isl_obj read_string_if_available(struct isl_stream *s)
2111 struct isl_token *tok;
2112 struct isl_obj obj = { isl_obj_none, NULL };
2114 tok = isl_stream_next_token(s);
2115 if (!tok)
2116 return obj;
2117 if (tok->type == ISL_TOKEN_STRING) {
2118 isl_str *str;
2119 str = isl_str_alloc(s->ctx);
2120 if (!str)
2121 goto error;
2122 str->s = strdup(tok->u.s);
2123 isl_token_free(tok);
2124 obj.v = str;
2125 obj.type = isl_obj_str;
2126 } else
2127 isl_stream_push_token(s, tok);
2128 return obj;
2129 error:
2130 isl_token_free(tok);
2131 return obj;
2134 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2136 struct isl_token *tok;
2137 struct isl_obj obj = { isl_obj_none, NULL };
2139 tok = isl_stream_next_token(s);
2140 if (!tok)
2141 return obj;
2142 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2143 int is_true = tok->type == ISL_TOKEN_TRUE;
2144 isl_token_free(tok);
2145 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2146 obj.type = isl_obj_bool;
2147 } else
2148 isl_stream_push_token(s, tok);
2149 return obj;
2152 static __isl_give char *read_ident(struct isl_stream *s)
2154 char *name;
2155 struct isl_token *tok, *tok2;
2157 name = isl_stream_read_ident_if_available(s);
2158 if (name)
2159 return name;
2161 tok = isl_stream_next_token(s);
2162 if (!tok)
2163 return NULL;
2164 if (tok->type != '$') {
2165 isl_stream_push_token(s, tok);
2166 return NULL;
2168 tok2 = isl_stream_next_token(s);
2169 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2170 if (tok2)
2171 isl_stream_push_token(s, tok2);
2172 isl_stream_push_token(s, tok);
2173 return NULL;
2176 name = isl_int_get_str(tok2->u.v);
2177 isl_token_free(tok);
2178 isl_token_free(tok2);
2180 return name;
2183 static struct isl_obj read_list(struct isl_stream *s,
2184 struct isl_hash_table *table, struct isl_obj obj)
2186 struct isl_list *list;
2188 list = isl_list_alloc(s->ctx, 2);
2189 if (!list)
2190 goto error;
2191 list->obj[0] = obj;
2192 list->obj[1] = read_obj(s, table);
2193 obj.v = list;
2194 obj.type = isl_obj_list;
2196 if (!list->obj[1].v)
2197 goto error;
2199 while (isl_stream_eat_if_available(s, ',')) {
2200 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2201 if (!obj.v)
2202 goto error;
2205 return obj;
2206 error:
2207 free_obj(obj);
2208 obj.type = isl_obj_none;
2209 obj.v = NULL;
2210 return obj;
2213 static struct isl_obj read_obj(struct isl_stream *s,
2214 struct isl_hash_table *table)
2216 struct isl_obj obj = { isl_obj_none, NULL };
2217 char *name = NULL;
2218 struct isc_un_op *op = NULL;
2220 obj = read_string_if_available(s);
2221 if (obj.v)
2222 return obj;
2223 obj = read_bool_if_available(s);
2224 if (obj.v)
2225 return obj;
2226 if (isl_stream_eat_if_available(s, '(')) {
2227 if (isl_stream_next_token_is(s, ')')) {
2228 obj.type = isl_obj_list;
2229 obj.v = isl_list_alloc(s->ctx, 0);
2230 } else {
2231 obj = read_expr(s, table);
2232 if (obj.v && isl_stream_eat_if_available(s, ','))
2233 obj = read_list(s, table, obj);
2235 if (!obj.v || isl_stream_eat(s, ')'))
2236 goto error;
2237 } else {
2238 op = read_prefix_un_op_if_available(s);
2239 if (op)
2240 return read_un_op_expr(s, table, op);
2242 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2243 return check_assert(s, table);
2244 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2245 return read_from_file(s);
2246 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2247 return write_to_file(s, table);
2248 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2249 return vertices(s, table);
2250 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2251 return any(s, table);
2252 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2253 return last(s, table);
2254 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2255 return schedule(s, table);
2256 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2257 return schedule_forest(s, table);
2258 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2259 return type_of(s, table);
2261 name = read_ident(s);
2262 if (name)
2263 obj = stored_obj(s->ctx, table, name);
2264 else
2265 obj = isl_stream_read_obj(s);
2266 if (!obj.v)
2267 goto error;
2270 if (isl_stream_eat_if_available(s, '^'))
2271 obj = power(s, obj);
2272 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2273 obj = obj_at_index(s, obj);
2274 else if (is_subtype(obj, isl_obj_union_map) &&
2275 isl_stream_eat_if_available(s, '(')) {
2276 obj = convert(s->ctx, obj, isl_obj_union_map);
2277 obj = apply(s, obj.v, table);
2278 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2279 isl_stream_eat_if_available(s, '(')) {
2280 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2281 obj = apply_fun(s, obj, table);
2282 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2283 isl_stream_eat_if_available(s, '(')) {
2284 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2285 obj = apply_fun(s, obj, table);
2288 return obj;
2289 error:
2290 free_obj(obj);
2291 obj.type = isl_obj_none;
2292 obj.v = NULL;
2293 return obj;
2296 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2297 struct isl_obj lhs, struct isl_obj rhs)
2299 int i;
2301 for (i = 0; ; ++i) {
2302 if (!bin_ops[i].op)
2303 break;
2304 if (bin_ops[i].op != like->op)
2305 continue;
2306 if (!is_subtype(lhs, bin_ops[i].lhs))
2307 continue;
2308 if (!is_subtype(rhs, bin_ops[i].rhs))
2309 continue;
2311 return &bin_ops[i];
2314 for (i = 0; ; ++i) {
2315 if (!named_bin_ops[i].name)
2316 break;
2317 if (named_bin_ops[i].op.op != like->op)
2318 continue;
2319 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2320 continue;
2321 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2322 continue;
2324 return &named_bin_ops[i].op;
2327 return NULL;
2330 static int next_is_neg_int(struct isl_stream *s)
2332 struct isl_token *tok;
2333 int ret;
2335 tok = isl_stream_next_token(s);
2336 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2337 isl_stream_push_token(s, tok);
2339 return ret;
2342 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2343 struct isl_obj lhs, struct isl_obj rhs)
2345 struct isl_obj obj;
2347 lhs = convert(ctx, lhs, op->lhs);
2348 rhs = convert(ctx, rhs, op->rhs);
2349 if (op->res != isl_obj_bool)
2350 obj.v = op->o.fn(lhs.v, rhs.v);
2351 else {
2352 int res = op->o.test(lhs.v, rhs.v);
2353 free_obj(lhs);
2354 free_obj(rhs);
2355 obj.v = isl_bool_from_int(res);
2357 obj.type = op->res;
2359 return obj;
2362 static struct isl_obj read_expr(struct isl_stream *s,
2363 struct isl_hash_table *table)
2365 struct isl_obj obj = { isl_obj_none, NULL };
2366 struct isl_obj right_obj = { isl_obj_none, NULL };
2368 obj = read_obj(s, table);
2369 for (; obj.v;) {
2370 struct isc_bin_op *op = NULL;
2372 op = read_bin_op_if_available(s, obj);
2373 if (!op)
2374 break;
2376 right_obj = read_obj(s, table);
2378 op = find_matching_bin_op(op, obj, right_obj);
2380 if (!op)
2381 isl_die(s->ctx, isl_error_invalid,
2382 "no such binary operator defined on given operands",
2383 goto error);
2385 obj = call_bin_op(s->ctx, op, obj, right_obj);
2388 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2389 right_obj = read_obj(s, table);
2390 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2393 return obj;
2394 error:
2395 free_obj(right_obj);
2396 free_obj(obj);
2397 obj.type = isl_obj_none;
2398 obj.v = NULL;
2399 return obj;
2402 static __isl_give isl_printer *source_file(struct isl_stream *s,
2403 struct isl_hash_table *table, __isl_take isl_printer *p);
2405 static __isl_give isl_printer *read_line(struct isl_stream *s,
2406 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2408 struct isl_obj obj = { isl_obj_none, NULL };
2409 char *lhs = NULL;
2410 int assign = 0;
2411 int only_print = 0;
2412 struct isc_bin_op *op = NULL;
2413 char buf[30];
2415 if (!p)
2416 return NULL;
2417 if (isl_stream_is_empty(s))
2418 return p;
2420 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2421 return source_file(s, table, p);
2422 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2423 return codegen(s, table, p);
2425 assign = is_assign(s);
2426 if (assign) {
2427 lhs = isl_stream_read_ident_if_available(s);
2428 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2429 goto error;
2430 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2431 only_print = 1;
2432 else if (!tty)
2433 only_print = 1;
2435 obj = read_expr(s, table);
2436 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2437 fprintf(stderr, "Interrupted\n");
2438 isl_ctx_reset_error(s->ctx);
2440 if (isl_stream_eat(s, ';'))
2441 goto error;
2443 if (only_print) {
2444 if (obj.type != isl_obj_none && obj.v != NULL) {
2445 p = obj.type->print(p, obj.v);
2446 p = isl_printer_end_line(p);
2448 free_obj(obj);
2449 return p;
2451 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2452 static int count = 0;
2453 snprintf(buf, sizeof(buf), "$%d", count++);
2454 lhs = strdup(buf + 1);
2456 p = isl_printer_print_str(p, buf);
2457 p = isl_printer_print_str(p, " := ");
2458 p = obj.type->print(p, obj.v);
2459 p = isl_printer_end_line(p);
2461 if (lhs && do_assign(s->ctx, table, lhs, obj))
2462 return p;
2464 return p;
2465 error:
2466 isl_stream_flush_tokens(s);
2467 isl_stream_skip_line(s);
2468 free(lhs);
2469 free_obj(obj);
2470 return p;
2473 int free_cb(void **entry, void *user)
2475 struct isl_named_obj *named = *entry;
2477 free_obj(named->obj);
2478 free(named->name);
2479 free(named);
2481 return 0;
2484 static void register_named_ops(struct isl_stream *s)
2486 int i;
2488 for (i = 0; i < ISCC_N_OP; ++i) {
2489 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2490 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2493 for (i = 0; ; ++i) {
2494 if (!named_un_ops[i].name)
2495 break;
2496 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2497 named_un_ops[i].name);
2498 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2501 for (i = 0; ; ++i) {
2502 if (!named_bin_ops[i].name)
2503 break;
2504 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2505 named_bin_ops[i].name);
2506 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2510 static __isl_give isl_printer *source_file(struct isl_stream *s,
2511 struct isl_hash_table *table, __isl_take isl_printer *p)
2513 struct isl_token *tok;
2514 struct isl_stream *s_file;
2515 FILE *file;
2517 tok = isl_stream_next_token(s);
2518 if (!tok || tok->type != ISL_TOKEN_STRING) {
2519 isl_stream_error(s, tok, "expecting filename");
2520 isl_token_free(tok);
2521 return p;
2524 file = fopen(tok->u.s, "r");
2525 isl_token_free(tok);
2526 isl_assert(s->ctx, file, return p);
2528 s_file = isl_stream_new_file(s->ctx, file);
2529 if (!s_file) {
2530 fclose(file);
2531 return p;
2534 register_named_ops(s_file);
2536 while (!s_file->eof)
2537 p = read_line(s_file, table, p, 0);
2539 isl_stream_free(s_file);
2540 fclose(file);
2542 isl_stream_eat(s, ';');
2544 return p;
2547 int main(int argc, char **argv)
2549 struct isl_ctx *ctx;
2550 struct isl_stream *s;
2551 struct isl_hash_table *table;
2552 struct iscc_options *options;
2553 isl_printer *p;
2554 int tty = isatty(0);
2556 options = iscc_options_new_with_defaults();
2557 assert(options);
2559 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2560 pet_options_set_autodetect(ctx, 1);
2561 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2562 s = isl_stream_new_file(ctx, stdin);
2563 assert(s);
2564 table = isl_hash_table_alloc(ctx, 10);
2565 assert(table);
2566 p = isl_printer_to_file(ctx, stdout);
2567 p = isl_printer_set_output_format(p, options->format);
2568 assert(p);
2570 register_named_ops(s);
2572 install_signal_handler(ctx);
2574 while (p && !s->eof) {
2575 isl_ctx_resume(ctx);
2576 p = read_line(s, table, p, tty);
2579 remove_signal_handler(ctx);
2581 isl_printer_free(p);
2582 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2583 isl_hash_table_free(ctx, table);
2584 isl_stream_free(s);
2585 isl_ctx_free(ctx);
2587 return 0;