iscc.c: add missing includes
[barvinok.git] / iscc.c
blobb33c82a2bac9dd03c7b0f04259cefd5656ae3fa0
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_obj_list.h>
15 #include <isl_obj_str.h>
16 #include <barvinok/isl.h>
17 #include <barvinok/options.h>
18 #include "lattice_width.h"
20 #include "config.h"
22 #ifdef HAVE_SIGACTION
23 #include <signal.h>
25 static isl_ctx *main_ctx;
27 static void handler(int signum)
29 if (isl_ctx_aborted(main_ctx))
30 exit(EXIT_FAILURE);
31 isl_ctx_abort(main_ctx);
34 static struct sigaction sa_old;
36 static void install_signal_handler(isl_ctx *ctx)
38 struct sigaction sa;
40 main_ctx = ctx;
42 memset(&sa, 0, sizeof(struct sigaction));
43 sa.sa_handler = &handler;
44 sa.sa_flags = SA_RESTART;
45 sigaction(SIGINT, &sa, &sa_old);
48 static void remove_signal_handler(isl_ctx *ctx)
50 sigaction(SIGINT, &sa_old, NULL);
53 #else
55 static void install_signal_handler(isl_ctx *ctx)
59 static void remove_signal_handler(isl_ctx *ctx)
63 #endif
65 #ifdef HAVE_CLOOG
66 #include <cloog/isl/cloog.h>
67 #endif
69 #ifdef HAVE_PET
70 #include <pet.h>
71 #else
72 struct pet_options;
73 int pet_options_set_autodetect(isl_ctx *ctx, int val)
75 return -1;
77 #endif
79 static int isl_bool_false = 0;
80 static int isl_bool_true = 1;
81 static int isl_bool_error = -1;
83 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
84 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
85 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
86 ISCC_MINIMIZING, ISCC_RESPECTING,
87 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
88 ISCC_N_OP };
89 static const char *op_name[ISCC_N_OP] = {
90 [ISCC_ASSERT] = "assert",
91 [ISCC_READ] = "read",
92 [ISCC_WRITE] = "write",
93 [ISCC_PRINT] = "print",
94 [ISCC_SOURCE] = "source",
95 [ISCC_VERTICES] = "vertices",
96 [ISCC_LAST] = "last",
97 [ISCC_ANY] = "any",
98 [ISCC_BEFORE] = "before",
99 [ISCC_UNDER] = "under",
100 [ISCC_SCHEDULE] = "schedule",
101 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
102 [ISCC_MINIMIZING] = "minimizing",
103 [ISCC_RESPECTING] = "respecting",
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_CLOOG
703 void *map_codegen(void *arg)
705 isl_space *dim;
706 isl_union_map *umap = (isl_union_map *)arg;
707 isl_ctx *ctx = isl_union_map_get_ctx(umap);
708 CloogState *state;
709 CloogOptions *options;
710 CloogDomain *context;
711 CloogUnionDomain *ud;
712 CloogInput *input;
713 struct clast_stmt *stmt;
715 state = cloog_isl_state_malloc(ctx);
716 options = cloog_options_malloc(state);
717 options->language = CLOOG_LANGUAGE_C;
718 options->strides = 1;
719 options->sh = 1;
721 ud = cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap));
723 dim = isl_union_map_get_space(umap);
724 context = cloog_domain_from_isl_set(isl_set_universe(dim));
726 input = cloog_input_alloc(context, ud);
728 stmt = cloog_clast_create_from_input(input, options);
729 clast_pprint(stdout, stmt, 0, options);
730 cloog_clast_free(stmt);
732 error:
733 cloog_options_free(options);
734 cloog_state_free(state);
735 isl_union_map_free(umap);
736 return NULL;
739 void *set_codegen(void *arg)
741 isl_space *dim;
742 isl_union_set *uset = (isl_union_set *)arg;
743 isl_set *set;
744 isl_ctx *ctx = isl_union_set_get_ctx(uset);
745 CloogState *state;
746 CloogOptions *options;
747 CloogDomain *context;
748 CloogUnionDomain *ud;
749 CloogInput *input;
750 struct clast_stmt *stmt;
752 if (isl_union_set_n_set(uset) > 1)
753 isl_die(ctx, isl_error_invalid,
754 "code generation for more than one domain "
755 "requires a schedule", goto error);
757 state = cloog_isl_state_malloc(ctx);
758 options = cloog_options_malloc(state);
759 options->language = CLOOG_LANGUAGE_C;
760 options->strides = 1;
761 options->sh = 1;
763 set = isl_set_from_union_set(isl_union_set_copy(uset));
764 ud = cloog_union_domain_from_isl_set(set);
766 dim = isl_union_set_get_space(uset);
767 context = cloog_domain_from_isl_set(isl_set_universe(dim));
769 input = cloog_input_alloc(context, ud);
771 stmt = cloog_clast_create_from_input(input, options);
772 clast_pprint(stdout, stmt, 0, options);
773 cloog_clast_free(stmt);
775 cloog_options_free(options);
776 cloog_state_free(state);
777 error:
778 isl_union_set_free(uset);
779 return NULL;
781 #endif
783 #ifdef HAVE_PET
784 static __isl_give isl_list *parse(__isl_take isl_str *str)
786 isl_ctx *ctx;
787 struct isl_list *list;
788 struct pet_scop *scop;
789 isl_union_map *sched, *reads, *writes;
790 isl_union_set *domain;
791 struct iscc_options *options;
793 if (!str)
794 return NULL;
795 ctx = str->ctx;
797 options = isl_ctx_peek_iscc_options(ctx);
798 if (!options || !options->io) {
799 isl_str_free(str);
800 isl_die(ctx, isl_error_invalid,
801 "parse_file operation not allowed", return NULL);
804 list = isl_list_alloc(ctx, 4);
805 if (!list)
806 goto error;
808 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
809 domain = pet_scop_collect_domains(scop);
810 sched = pet_scop_collect_schedule(scop);
811 reads = pet_scop_collect_reads(scop);
812 writes = pet_scop_collect_writes(scop);
813 pet_scop_free(scop);
815 list->obj[0].type = isl_obj_union_set;
816 list->obj[0].v = domain;
817 list->obj[1].type = isl_obj_union_map;
818 list->obj[1].v = writes;
819 list->obj[2].type = isl_obj_union_map;
820 list->obj[2].v = reads;
821 list->obj[3].type = isl_obj_union_map;
822 list->obj[3].v = sched;
824 if (!list->obj[0].v || !list->obj[1].v ||
825 !list->obj[2].v || !list->obj[3].v)
826 goto error;
828 isl_str_free(str);
829 return list;
830 error:
831 isl_list_free(list);
832 isl_str_free(str);
833 return NULL;
835 #endif
837 static int add_point(__isl_take isl_point *pnt, void *user)
839 isl_union_set **scan = (isl_union_set **) user;
841 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
843 return 0;
846 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
848 isl_union_set *scan;
850 scan = isl_union_set_empty(isl_union_set_get_space(uset));
852 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
853 isl_union_set_free(scan);
854 return uset;
857 isl_union_set_free(uset);
858 return scan;
861 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
863 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
866 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
867 __isl_take isl_union_pw_qpolynomial *upwqp)
869 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
872 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
873 __isl_take isl_union_pw_qpolynomial *upwqp)
875 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
878 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
879 __isl_take isl_union_pw_qpolynomial *upwqp)
881 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
884 typedef void *(*isc_un_op_fn)(void *arg);
885 struct isc_un_op {
886 enum isl_token_type op;
887 isl_obj_type arg;
888 isl_obj_type res;
889 isc_un_op_fn fn;
891 struct isc_named_un_op {
892 char *name;
893 struct isc_un_op op;
895 struct isc_named_un_op named_un_ops[] = {
896 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
897 (isc_un_op_fn) &isl_union_map_affine_hull } },
898 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
899 (isc_un_op_fn) &isl_union_set_affine_hull } },
900 {"card", { -1, isl_obj_union_set,
901 isl_obj_union_pw_qpolynomial,
902 (isc_un_op_fn) &isl_union_set_card } },
903 {"card", { -1, isl_obj_union_map,
904 isl_obj_union_pw_qpolynomial,
905 (isc_un_op_fn) &isl_union_map_card } },
906 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
907 (isc_un_op_fn) &isl_union_set_coalesce } },
908 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
909 (isc_un_op_fn) &isl_union_map_coalesce } },
910 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
911 isl_obj_union_pw_qpolynomial,
912 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
913 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
914 isl_obj_union_pw_qpolynomial_fold,
915 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
916 #ifdef HAVE_CLOOG
917 {"codegen", { -1, isl_obj_union_set, isl_obj_none,
918 &set_codegen } },
919 {"codegen", { -1, isl_obj_union_map, isl_obj_none,
920 &map_codegen } },
921 #endif
922 {"coefficients", { -1, isl_obj_union_set,
923 isl_obj_union_set,
924 (isc_un_op_fn) &isl_union_set_coefficients } },
925 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
926 (isc_un_op_fn) &isl_union_set_solutions } },
927 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
928 (isc_un_op_fn) &isl_union_map_deltas } },
929 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
930 (isc_un_op_fn) &isl_union_map_deltas_map } },
931 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
932 (isc_un_op_fn) &isl_union_map_domain } },
933 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
934 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
935 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
936 isl_obj_union_set,
937 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
938 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
939 (isc_un_op_fn) &isl_union_map_domain } },
940 {"domain", { -1, isl_obj_union_pw_qpolynomial,
941 isl_obj_union_set,
942 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
943 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
944 isl_obj_union_set,
945 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
946 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
947 (isc_un_op_fn) &isl_union_map_domain_map } },
948 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
949 (isc_un_op_fn) &isl_union_map_range } },
950 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
951 (isc_un_op_fn) &isl_union_map_range } },
952 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
953 (isc_un_op_fn) &isl_union_map_range_map } },
954 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
955 (isc_un_op_fn) &isl_union_set_identity } },
956 {"lattice_width", { -1, isl_obj_union_set,
957 isl_obj_union_pw_qpolynomial,
958 (isc_un_op_fn) &isl_union_set_lattice_width } },
959 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
960 (isc_un_op_fn) &isl_union_map_lexmin } },
961 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
962 (isc_un_op_fn) &isl_union_map_lexmax } },
963 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
964 (isc_un_op_fn) &isl_union_set_lexmin } },
965 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
966 (isc_un_op_fn) &isl_union_set_lexmax } },
967 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
968 (isc_un_op_fn) &isl_union_set_lift } },
969 {"params", { -1, isl_obj_union_map, isl_obj_set,
970 (isc_un_op_fn) &isl_union_map_params } },
971 {"params", { -1, isl_obj_union_set, isl_obj_set,
972 (isc_un_op_fn) &isl_union_set_params } },
973 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
974 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
975 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
976 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
977 {"poly", { -1, isl_obj_union_pw_qpolynomial,
978 isl_obj_union_pw_qpolynomial,
979 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
980 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
981 isl_obj_union_pw_qpolynomial,
982 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
983 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
984 isl_obj_union_pw_qpolynomial,
985 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
986 #ifdef HAVE_PET
987 {"parse_file", { -1, isl_obj_str, isl_obj_list,
988 (isc_un_op_fn) &parse } },
989 #endif
990 {"pow", { -1, isl_obj_union_map, isl_obj_list,
991 (isc_un_op_fn) &union_map_power } },
992 {"sample", { -1, isl_obj_union_set, isl_obj_set,
993 (isc_un_op_fn) &union_set_sample } },
994 {"sample", { -1, isl_obj_union_map, isl_obj_map,
995 (isc_un_op_fn) &union_map_sample } },
996 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
997 (isc_un_op_fn) &union_set_scan } },
998 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
999 (isc_un_op_fn) &union_map_scan } },
1000 {"sum", { -1, isl_obj_union_pw_qpolynomial,
1001 isl_obj_union_pw_qpolynomial,
1002 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
1003 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
1004 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
1005 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
1006 (isc_un_op_fn) &isl_union_set_unwrap } },
1007 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
1008 (isc_un_op_fn) &isl_union_map_wrap } },
1009 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
1010 (isc_un_op_fn) &isl_union_map_zip } },
1011 NULL
1014 struct isl_named_obj {
1015 char *name;
1016 struct isl_obj obj;
1019 static void free_obj(struct isl_obj obj)
1021 obj.type->free(obj.v);
1024 static int same_name(const void *entry, const void *val)
1026 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1028 return !strcmp(named->name, val);
1031 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1032 char *name, struct isl_obj obj)
1034 struct isl_hash_table_entry *entry;
1035 uint32_t name_hash;
1036 struct isl_named_obj *named;
1038 name_hash = isl_hash_string(isl_hash_init(), name);
1039 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1040 if (!entry)
1041 goto error;
1042 if (entry->data) {
1043 named = entry->data;
1044 free_obj(named->obj);
1045 free(name);
1046 } else {
1047 named = isl_alloc_type(ctx, struct isl_named_obj);
1048 if (!named)
1049 goto error;
1050 named->name = name;
1051 entry->data = named;
1053 named->obj = obj;
1055 return 0;
1056 error:
1057 free_obj(obj);
1058 free(name);
1059 return -1;
1062 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1063 struct isl_hash_table *table, char *name)
1065 struct isl_obj obj = { isl_obj_none, NULL };
1066 struct isl_hash_table_entry *entry;
1067 uint32_t name_hash;
1069 name_hash = isl_hash_string(isl_hash_init(), name);
1070 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1071 if (entry) {
1072 struct isl_named_obj *named;
1073 named = entry->data;
1074 obj = named->obj;
1075 } else if (isdigit(name[0]))
1076 fprintf(stderr, "unknown identifier '$%s'\n", name);
1077 else
1078 fprintf(stderr, "unknown identifier '%s'\n", name);
1080 free(name);
1081 obj.v = obj.type->copy(obj.v);
1082 return obj;
1085 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1087 if (obj.type == super)
1088 return 1;
1089 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1090 return 1;
1091 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1092 return 1;
1093 if (obj.type == isl_obj_pw_qpolynomial &&
1094 super == isl_obj_union_pw_qpolynomial)
1095 return 1;
1096 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1097 super == isl_obj_union_pw_qpolynomial_fold)
1098 return 1;
1099 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1100 return 1;
1101 if (obj.type == isl_obj_list) {
1102 struct isl_list *list = obj.v;
1103 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1104 return is_subtype(list->obj[0], super);
1106 if (super == isl_obj_str)
1107 return 1;
1108 return 0;
1111 static struct isl_obj obj_at(struct isl_obj obj, int i)
1113 struct isl_list *list = obj.v;
1115 obj = list->obj[i];
1116 obj.v = obj.type->copy(obj.v);
1118 isl_list_free(list);
1120 return obj;
1123 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1124 isl_obj_type type)
1126 if (obj.type == type)
1127 return obj;
1128 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1129 obj.type = isl_obj_union_map;
1130 obj.v = isl_union_map_from_map(obj.v);
1131 return obj;
1133 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1134 obj.type = isl_obj_union_set;
1135 obj.v = isl_union_set_from_set(obj.v);
1136 return obj;
1138 if (obj.type == isl_obj_pw_qpolynomial &&
1139 type == isl_obj_union_pw_qpolynomial) {
1140 obj.type = isl_obj_union_pw_qpolynomial;
1141 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1142 return obj;
1144 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1145 type == isl_obj_union_pw_qpolynomial_fold) {
1146 obj.type = isl_obj_union_pw_qpolynomial_fold;
1147 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1148 return obj;
1150 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1151 if (type == isl_obj_union_map) {
1152 obj.type = isl_obj_union_map;
1153 return obj;
1155 if (type == isl_obj_union_pw_qpolynomial) {
1156 isl_space *dim = isl_union_set_get_space(obj.v);
1157 isl_union_set_free(obj.v);
1158 obj.v = isl_union_pw_qpolynomial_zero(dim);
1159 obj.type = isl_obj_union_pw_qpolynomial;
1160 return obj;
1162 if (type == isl_obj_union_pw_qpolynomial_fold) {
1163 isl_space *dim = isl_union_set_get_space(obj.v);
1164 isl_union_set_free(obj.v);
1165 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1166 isl_fold_list);
1167 obj.type = isl_obj_union_pw_qpolynomial_fold;
1168 return obj;
1171 if (obj.type == isl_obj_list) {
1172 struct isl_list *list = obj.v;
1173 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1174 return convert(ctx, obj_at(obj, 0), type);
1176 if (type == isl_obj_str) {
1177 isl_str *str;
1178 isl_printer *p;
1179 char *s;
1181 p = isl_printer_to_str(ctx);
1182 if (!p)
1183 goto error;
1184 p = obj.type->print(p, obj.v);
1185 s = isl_printer_get_str(p);
1186 isl_printer_free(p);
1188 str = isl_str_from_string(ctx, s);
1189 if (!str)
1190 goto error;
1191 free_obj(obj);
1192 obj.v = str;
1193 obj.type = isl_obj_str;
1194 return obj;
1197 error:
1198 free_obj(obj);
1199 obj.type = isl_obj_none;
1200 obj.v = NULL;
1201 return obj;
1204 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1205 struct isl_obj lhs)
1207 int i;
1208 struct isl_token *tok;
1210 tok = isl_stream_next_token(s);
1211 if (!tok)
1212 return NULL;
1214 for (i = 0; ; ++i) {
1215 if (!bin_ops[i].op)
1216 break;
1217 if (bin_ops[i].op != tok->type)
1218 continue;
1219 if (!is_subtype(lhs, bin_ops[i].lhs))
1220 continue;
1222 isl_token_free(tok);
1223 return &bin_ops[i];
1226 for (i = 0; ; ++i) {
1227 if (!named_bin_ops[i].name)
1228 break;
1229 if (named_bin_ops[i].op.op != tok->type)
1230 continue;
1231 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1232 continue;
1234 isl_token_free(tok);
1235 return &named_bin_ops[i].op;
1238 isl_stream_push_token(s, tok);
1240 return NULL;
1243 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1245 int i;
1246 struct isl_token *tok;
1248 tok = isl_stream_next_token(s);
1249 if (!tok)
1250 return NULL;
1252 for (i = 0; ; ++i) {
1253 if (!named_un_ops[i].name)
1254 break;
1255 if (named_un_ops[i].op.op != tok->type)
1256 continue;
1258 isl_token_free(tok);
1259 return &named_un_ops[i].op;
1262 isl_stream_push_token(s, tok);
1264 return NULL;
1267 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1268 struct isl_obj arg)
1270 int i;
1272 for (i = 0; ; ++i) {
1273 if (!named_un_ops[i].name)
1274 break;
1275 if (named_un_ops[i].op.op != like->op)
1276 continue;
1277 if (!is_subtype(arg, named_un_ops[i].op.arg))
1278 continue;
1280 return &named_un_ops[i].op;
1283 return NULL;
1286 static int is_assign(struct isl_stream *s)
1288 struct isl_token *tok;
1289 struct isl_token *tok2;
1290 int assign;
1292 tok = isl_stream_next_token(s);
1293 if (!tok)
1294 return 0;
1295 if (tok->type != ISL_TOKEN_IDENT) {
1296 isl_stream_push_token(s, tok);
1297 return 0;
1300 tok2 = isl_stream_next_token(s);
1301 if (!tok2) {
1302 isl_stream_push_token(s, tok);
1303 return 0;
1305 assign = tok2->type == ISL_TOKEN_DEF;
1306 isl_stream_push_token(s, tok2);
1307 isl_stream_push_token(s, tok);
1309 return assign;
1312 static struct isl_obj read_obj(struct isl_stream *s,
1313 struct isl_hash_table *table);
1314 static struct isl_obj read_expr(struct isl_stream *s,
1315 struct isl_hash_table *table);
1317 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1318 struct isl_hash_table *table, struct isc_un_op *op)
1320 struct isl_obj obj = { isl_obj_none, NULL };
1322 obj = read_obj(s, table);
1323 if (!obj.v)
1324 goto error;
1326 op = find_matching_un_op(op, obj);
1328 if (!op)
1329 isl_die(s->ctx, isl_error_invalid,
1330 "no such unary operator defined on given operand",
1331 goto error);
1333 obj = convert(s->ctx, obj, op->arg);
1334 obj.v = op->fn(obj.v);
1335 obj.type = op->res;
1337 return obj;
1338 error:
1339 free_obj(obj);
1340 obj.type = isl_obj_none;
1341 obj.v = NULL;
1342 return obj;
1345 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1347 struct isl_list *list;
1348 int exact;
1350 if (obj.type != isl_obj_union_map)
1351 obj = convert(ctx, obj, isl_obj_union_map);
1352 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1353 list = isl_list_alloc(ctx, 2);
1354 if (!list)
1355 goto error;
1357 list->obj[0].type = isl_obj_union_map;
1358 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1359 list->obj[1].type = isl_obj_bool;
1360 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1361 obj.v = list;
1362 obj.type = isl_obj_list;
1363 if (exact < 0 || !list->obj[0].v)
1364 goto error;
1366 return obj;
1367 error:
1368 free_obj(obj);
1369 obj.type = isl_obj_none;
1370 obj.v = NULL;
1371 return obj;
1374 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1376 struct isl_list *list = obj.v;
1377 struct isl_token *tok;
1378 int i;
1380 tok = isl_stream_next_token(s);
1381 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1382 isl_stream_error(s, tok, "expecting index");
1383 if (tok)
1384 isl_stream_push_token(s, tok);
1385 goto error;
1387 i = isl_int_get_si(tok->u.v);
1388 isl_token_free(tok);
1389 isl_assert(s->ctx, i < list->n, goto error);
1390 if (isl_stream_eat(s, ']'))
1391 goto error;
1393 return obj_at(obj, i);
1394 error:
1395 free_obj(obj);
1396 obj.type = isl_obj_none;
1397 obj.v = NULL;
1398 return obj;
1401 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1402 struct isl_hash_table *table)
1404 struct isl_obj obj;
1406 obj = read_expr(s, table);
1407 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1408 is_subtype(obj, isl_obj_union_map), goto error);
1410 if (obj.type == isl_obj_list) {
1411 struct isl_list *list = obj.v;
1412 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1413 obj = obj_at(obj, 0);
1415 if (obj.type == isl_obj_set)
1416 obj = convert(s->ctx, obj, isl_obj_union_set);
1417 else if (obj.type == isl_obj_map)
1418 obj = convert(s->ctx, obj, isl_obj_union_map);
1419 if (obj.type == isl_obj_union_set) {
1420 obj.v = isl_union_set_apply(obj.v, umap);
1421 } else
1422 obj.v = isl_union_map_apply_range(obj.v, umap);
1423 if (!obj.v)
1424 goto error2;
1426 if (isl_stream_eat(s, ')'))
1427 goto error2;
1429 return obj;
1430 error:
1431 isl_union_map_free(umap);
1432 error2:
1433 free_obj(obj);
1434 obj.type = isl_obj_none;
1435 obj.v = NULL;
1436 return obj;
1439 static struct isl_obj apply_fun_set(struct isl_obj obj,
1440 __isl_take isl_union_set *uset)
1442 if (obj.type == isl_obj_union_pw_qpolynomial) {
1443 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1444 } else {
1445 obj.type = isl_obj_list;
1446 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1448 return obj;
1451 static struct isl_obj apply_fun_map(struct isl_obj obj,
1452 __isl_take isl_union_map *umap)
1454 if (obj.type == isl_obj_union_pw_qpolynomial) {
1455 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1456 } else {
1457 obj.type = isl_obj_list;
1458 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1460 return obj;
1463 static struct isl_obj apply_fun(struct isl_stream *s,
1464 struct isl_obj obj, struct isl_hash_table *table)
1466 struct isl_obj arg;
1468 arg = read_expr(s, table);
1469 if (!is_subtype(arg, isl_obj_union_map) &&
1470 !is_subtype(arg, isl_obj_union_set))
1471 isl_die(s->ctx, isl_error_invalid,
1472 "expecting set of map argument", goto error);
1474 if (arg.type == isl_obj_list) {
1475 struct isl_list *list = arg.v;
1476 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1477 arg = obj_at(arg, 0);
1479 if (arg.type == isl_obj_set)
1480 arg = convert(s->ctx, arg, isl_obj_union_set);
1481 else if (arg.type == isl_obj_map)
1482 arg = convert(s->ctx, arg, isl_obj_union_map);
1483 if (arg.type == isl_obj_union_set)
1484 obj = apply_fun_set(obj, arg.v);
1485 else
1486 obj = apply_fun_map(obj, arg.v);
1487 if (!obj.v)
1488 goto error2;
1490 if (isl_stream_eat(s, ')'))
1491 goto error2;
1493 return obj;
1494 error:
1495 free_obj(arg);
1496 error2:
1497 free_obj(obj);
1498 obj.type = isl_obj_none;
1499 obj.v = NULL;
1500 return obj;
1503 struct add_vertex_data {
1504 struct isl_list *list;
1505 int i;
1508 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1510 struct add_vertex_data *data = (struct add_vertex_data *)user;
1511 isl_basic_set *expr;
1513 expr = isl_vertex_get_expr(vertex);
1515 data->list->obj[data->i].type = isl_obj_set;
1516 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1517 data->i++;
1519 isl_vertex_free(vertex);
1521 return 0;
1524 static int set_vertices(__isl_take isl_set *set, void *user)
1526 isl_ctx *ctx;
1527 isl_basic_set *hull;
1528 isl_vertices *vertices = NULL;
1529 struct isl_list *list = NULL;
1530 int r;
1531 struct add_vertex_data *data = (struct add_vertex_data *)user;
1533 set = isl_set_remove_divs(set);
1534 hull = isl_set_convex_hull(set);
1535 vertices = isl_basic_set_compute_vertices(hull);
1536 isl_basic_set_free(hull);
1538 list = data->list;
1540 ctx = isl_vertices_get_ctx(vertices);
1541 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1542 if (!data->list)
1543 goto error;
1545 data->i = 0;
1546 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1548 data->list = isl_list_concat(list, data->list);
1550 isl_vertices_free(vertices);
1552 return r;
1553 error:
1554 data->list = list;
1555 isl_vertices_free(vertices);
1556 return -1;
1559 static struct isl_obj vertices(struct isl_stream *s,
1560 struct isl_hash_table *table)
1562 isl_ctx *ctx;
1563 struct isl_obj obj;
1564 struct isl_list *list = NULL;
1565 isl_union_set *uset;
1566 struct add_vertex_data data = { NULL };
1568 obj = read_expr(s, table);
1569 obj = convert(s->ctx, obj, isl_obj_union_set);
1570 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1571 uset = obj.v;
1572 obj.v = NULL;
1574 ctx = isl_union_set_get_ctx(uset);
1575 list = isl_list_alloc(ctx, 0);
1576 if (!list)
1577 goto error;
1579 data.list = list;
1581 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1582 goto error;
1584 isl_union_set_free(uset);
1586 obj.type = isl_obj_list;
1587 obj.v = data.list;
1589 return obj;
1590 error:
1591 isl_union_set_free(uset);
1592 isl_list_free(data.list);
1593 free_obj(obj);
1594 obj.type = isl_obj_none;
1595 obj.v = NULL;
1596 return obj;
1599 static struct isl_obj type_of(struct isl_stream *s,
1600 struct isl_hash_table *table)
1602 isl_ctx *ctx;
1603 struct isl_obj obj;
1604 const char *type = "unknown";
1606 obj = read_expr(s, table);
1608 if (obj.type == isl_obj_map ||
1609 obj.type == isl_obj_union_map)
1610 type = "map";
1611 if (obj.type == isl_obj_set ||
1612 obj.type == isl_obj_union_set)
1613 type = "set";
1614 if (obj.type == isl_obj_pw_qpolynomial ||
1615 obj.type == isl_obj_union_pw_qpolynomial)
1616 type = "piecewise quasipolynomial";
1617 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1618 obj.type == isl_obj_union_pw_qpolynomial_fold)
1619 type = "piecewise quasipolynomial fold";
1620 if (obj.type == isl_obj_list)
1621 type = "list";
1622 if (obj.type == isl_obj_bool)
1623 type = "boolean";
1624 if (obj.type == isl_obj_str)
1625 type = "string";
1626 if (obj.type == isl_obj_int)
1627 type = "int";
1629 free_obj(obj);
1630 obj.type = isl_obj_str;
1631 obj.v = isl_str_from_string(s->ctx, strdup(type));
1633 return obj;
1636 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1637 struct isl_hash_table *table)
1639 struct isl_obj obj;
1641 obj = read_obj(s, table);
1642 obj = convert(s->ctx, obj, isl_obj_union_set);
1643 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1644 return obj.v;
1645 error:
1646 free_obj(obj);
1647 return NULL;
1650 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1651 struct isl_hash_table *table)
1653 struct isl_obj obj;
1655 obj = read_obj(s, table);
1656 obj = convert(s->ctx, obj, isl_obj_union_map);
1657 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1658 return obj.v;
1659 error:
1660 free_obj(obj);
1661 return NULL;
1664 static struct isl_obj last_any(struct isl_stream *s,
1665 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1666 __isl_take isl_union_map *may_source)
1668 struct isl_obj obj = { isl_obj_none, NULL };
1669 isl_union_map *sink = NULL;
1670 isl_union_map *schedule = NULL;
1671 isl_union_map *may_dep;
1672 isl_union_map *must_dep;
1674 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1675 goto error;
1677 sink = read_map(s, table);
1678 if (!sink)
1679 goto error;
1681 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1682 goto error;
1684 schedule = read_map(s, table);
1685 if (!schedule)
1686 goto error;
1688 if (isl_union_map_compute_flow(sink, must_source, may_source,
1689 schedule, &must_dep, &may_dep,
1690 NULL, NULL) < 0)
1691 return obj;
1693 obj.type = isl_obj_union_map;
1694 obj.v = isl_union_map_union(must_dep, may_dep);
1696 return obj;
1697 error:
1698 isl_union_map_free(may_source);
1699 isl_union_map_free(must_source);
1700 isl_union_map_free(sink);
1701 isl_union_map_free(schedule);
1702 free_obj(obj);
1703 obj.type = isl_obj_none;
1704 obj.v = NULL;
1705 return obj;
1708 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1710 struct isl_obj obj = { isl_obj_none, NULL };
1711 isl_union_map *must_source = NULL;
1712 isl_union_map *may_source = NULL;
1713 isl_union_map *sink = NULL;
1714 isl_union_map *schedule = NULL;
1715 isl_union_map *may_dep;
1717 may_source = read_map(s, table);
1718 if (!may_source)
1719 goto error;
1721 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1722 must_source = read_map(s, table);
1723 if (!must_source)
1724 goto error;
1725 return last_any(s, table, must_source, may_source);
1728 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1729 goto error;
1731 sink = read_map(s, table);
1732 if (!sink)
1733 goto error;
1735 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1736 goto error;
1738 schedule = read_map(s, table);
1739 if (!schedule)
1740 goto error;
1742 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1743 if (isl_union_map_compute_flow(sink, must_source, may_source,
1744 schedule, NULL, &may_dep,
1745 NULL, NULL) < 0)
1746 return obj;
1748 obj.type = isl_obj_union_map;
1749 obj.v = may_dep;
1751 return obj;
1752 error:
1753 isl_union_map_free(may_source);
1754 isl_union_map_free(must_source);
1755 isl_union_map_free(sink);
1756 isl_union_map_free(schedule);
1757 free_obj(obj);
1758 obj.type = isl_obj_none;
1759 obj.v = NULL;
1760 return obj;
1763 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1765 struct isl_obj obj = { isl_obj_none, NULL };
1766 struct isl_list *list = NULL;
1767 isl_union_map *must_source = NULL;
1768 isl_union_map *may_source = NULL;
1769 isl_union_map *sink = NULL;
1770 isl_union_map *schedule = NULL;
1771 isl_union_map *must_dep;
1772 isl_union_map *must_no_source;
1774 must_source = read_map(s, table);
1775 if (!must_source)
1776 goto error;
1778 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1779 may_source = read_map(s, table);
1780 if (!may_source)
1781 goto error;
1782 return last_any(s, table, must_source, may_source);
1785 list = isl_list_alloc(s->ctx, 2);
1786 if (!list)
1787 goto error;
1789 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1790 goto error;
1792 sink = read_map(s, table);
1793 if (!sink)
1794 goto error;
1796 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1797 goto error;
1799 schedule = read_map(s, table);
1800 if (!schedule)
1801 goto error;
1803 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1804 if (isl_union_map_compute_flow(sink, must_source, may_source,
1805 schedule, &must_dep, NULL,
1806 &must_no_source, NULL) < 0) {
1807 isl_list_free(list);
1808 return obj;
1811 list->obj[0].type = isl_obj_union_map;
1812 list->obj[0].v = must_dep;
1813 list->obj[1].type = isl_obj_union_map;
1814 list->obj[1].v = must_no_source;
1816 obj.v = list;
1817 obj.type = isl_obj_list;
1819 return obj;
1820 error:
1821 isl_list_free(list);
1822 isl_union_map_free(may_source);
1823 isl_union_map_free(must_source);
1824 isl_union_map_free(sink);
1825 isl_union_map_free(schedule);
1826 free_obj(obj);
1827 obj.type = isl_obj_none;
1828 obj.v = NULL;
1829 return obj;
1832 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1833 struct isl_hash_table *table)
1835 isl_union_set *domain;
1836 isl_union_map *validity;
1837 isl_union_map *proximity;
1839 domain = read_set(s, table);
1840 if (!domain)
1841 return NULL;
1843 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1844 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1846 for (;;) {
1847 isl_union_map *umap;
1848 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1849 umap = read_map(s, table);
1850 validity = isl_union_map_union(validity, umap);
1851 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1852 umap = read_map(s, table);
1853 proximity = isl_union_map_union(proximity, umap);
1854 } else
1855 break;
1858 return isl_union_set_compute_schedule(domain, validity, proximity);
1861 static struct isl_obj schedule(struct isl_stream *s,
1862 struct isl_hash_table *table)
1864 struct isl_obj obj = { isl_obj_none, NULL };
1865 isl_schedule *schedule;
1867 schedule = get_schedule(s, table);
1869 obj.v = isl_schedule_get_map(schedule);
1870 obj.type = isl_obj_union_map;
1872 isl_schedule_free(schedule);
1874 return obj;
1877 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1879 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1881 struct isl_obj obj = { isl_obj_none, NULL };
1882 isl_ctx *ctx = isl_band_get_ctx(band);
1883 struct isl_list *list;
1885 list = isl_list_alloc(ctx, 2);
1886 if (!list)
1887 goto error;
1889 obj.v = list;
1890 obj.type = isl_obj_list;
1892 list->obj[0].type = isl_obj_union_map;
1893 list->obj[0].v = isl_band_get_partial_schedule(band);
1895 if (isl_band_has_children(band)) {
1896 isl_band_list *children;
1898 children = isl_band_get_children(band);
1899 list->obj[1] = band_list_to_obj_list(children);
1900 } else {
1901 list->obj[1].type = isl_obj_list;
1902 list->obj[1].v = isl_list_alloc(ctx, 0);
1905 if (!list->obj[0].v || !list->obj[1].v)
1906 goto error;
1908 isl_band_free(band);
1910 return obj;
1911 error:
1912 isl_band_free(band);
1913 free_obj(obj);
1914 obj.type = isl_obj_none;
1915 obj.v = NULL;
1916 return obj;
1919 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1921 struct isl_obj obj = { isl_obj_none, NULL };
1922 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1923 struct isl_list *list;
1924 int i, n;
1926 n = isl_band_list_n_band(bands);
1927 list = isl_list_alloc(ctx, n);
1928 if (!list)
1929 goto error;
1931 obj.v = list;
1932 obj.type = isl_obj_list;
1934 for (i = 0; i < n; ++i) {
1935 isl_band *band;
1937 band = isl_band_list_get_band(bands, i);
1938 list->obj[i] = band_to_obj_list(band);
1939 if (!list->obj[i].v)
1940 goto error;
1943 isl_band_list_free(bands);
1945 return obj;
1946 error:
1947 isl_band_list_free(bands);
1948 free_obj(obj);
1949 obj.type = isl_obj_none;
1950 obj.v = NULL;
1951 return obj;
1954 static struct isl_obj schedule_forest(struct isl_stream *s,
1955 struct isl_hash_table *table)
1957 struct isl_obj obj = { isl_obj_none, NULL };
1958 isl_schedule *schedule;
1959 isl_band_list *roots;
1961 schedule = get_schedule(s, table);
1962 if (!schedule)
1963 return obj;
1965 roots = isl_schedule_get_band_forest(schedule);
1966 isl_schedule_free(schedule);
1968 return band_list_to_obj_list(roots);
1971 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1973 struct isl_token *tok;
1975 if (isl_stream_eat_if_available(s, '+'))
1976 return transitive_closure(s->ctx, obj);
1978 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1979 if (obj.type != isl_obj_union_map)
1980 obj = convert(s->ctx, obj, isl_obj_union_map);
1982 tok = isl_stream_next_token(s);
1983 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_is_zero(tok->u.v)) {
1984 isl_stream_error(s, tok, "expecting non-zero integer exponent");
1985 if (tok)
1986 isl_stream_push_token(s, tok);
1987 goto error;
1990 obj.v = isl_union_map_fixed_power(obj.v, tok->u.v);
1991 isl_token_free(tok);
1992 if (!obj.v)
1993 goto error;
1995 return obj;
1996 error:
1997 free_obj(obj);
1998 obj.type = isl_obj_none;
1999 obj.v = NULL;
2000 return obj;
2003 static struct isl_obj check_assert(struct isl_stream *s,
2004 struct isl_hash_table *table)
2006 struct isl_obj obj;
2008 obj = read_expr(s, table);
2009 if (obj.type != isl_obj_bool)
2010 isl_die(s->ctx, isl_error_invalid,
2011 "expecting boolean expression", goto error);
2012 if (obj.v != &isl_bool_true)
2013 isl_die(s->ctx, isl_error_unknown,
2014 "assertion failed", abort());
2015 error:
2016 free_obj(obj);
2017 obj.type = isl_obj_none;
2018 obj.v = NULL;
2019 return obj;
2022 static struct isl_obj read_from_file(struct isl_stream *s)
2024 struct isl_obj obj;
2025 struct isl_token *tok;
2026 struct isl_stream *s_file;
2027 struct iscc_options *options;
2028 FILE *file;
2030 tok = isl_stream_next_token(s);
2031 if (!tok || tok->type != ISL_TOKEN_STRING) {
2032 isl_stream_error(s, tok, "expecting filename");
2033 isl_token_free(tok);
2034 goto error;
2037 options = isl_ctx_peek_iscc_options(s->ctx);
2038 if (!options || !options->io) {
2039 isl_token_free(tok);
2040 isl_die(s->ctx, isl_error_invalid,
2041 "read operation not allowed", goto error);
2044 file = fopen(tok->u.s, "r");
2045 isl_token_free(tok);
2046 isl_assert(s->ctx, file, goto error);
2048 s_file = isl_stream_new_file(s->ctx, file);
2049 if (!s_file) {
2050 fclose(file);
2051 goto error;
2054 obj = isl_stream_read_obj(s_file);
2056 isl_stream_free(s_file);
2057 fclose(file);
2059 return obj;
2060 error:
2061 obj.type = isl_obj_none;
2062 obj.v = NULL;
2063 return obj;
2066 static struct isl_obj write_to_file(struct isl_stream *s,
2067 struct isl_hash_table *table)
2069 struct isl_obj obj;
2070 struct isl_token *tok;
2071 struct isl_stream *s_file;
2072 struct iscc_options *options;
2073 FILE *file;
2074 isl_printer *p;
2076 tok = isl_stream_next_token(s);
2077 if (!tok || tok->type != ISL_TOKEN_STRING) {
2078 isl_stream_error(s, tok, "expecting filename");
2079 isl_token_free(tok);
2080 goto error;
2083 obj = read_expr(s, table);
2085 options = isl_ctx_peek_iscc_options(s->ctx);
2086 if (!options || !options->io) {
2087 isl_token_free(tok);
2088 isl_die(s->ctx, isl_error_invalid,
2089 "write operation not allowed", goto error);
2092 file = fopen(tok->u.s, "w");
2093 isl_token_free(tok);
2094 if (!file)
2095 isl_die(s->ctx, isl_error_unknown,
2096 "could not open file for writing", goto error);
2098 p = isl_printer_to_file(s->ctx, file);
2099 p = isl_printer_set_output_format(p, options->format);
2100 p = obj.type->print(p, obj.v);
2101 p = isl_printer_end_line(p);
2102 isl_printer_free(p);
2104 fclose(file);
2105 error:
2106 free_obj(obj);
2107 obj.type = isl_obj_none;
2108 obj.v = NULL;
2109 return obj;
2112 static struct isl_obj read_string_if_available(struct isl_stream *s)
2114 struct isl_token *tok;
2115 struct isl_obj obj = { isl_obj_none, NULL };
2117 tok = isl_stream_next_token(s);
2118 if (!tok)
2119 return obj;
2120 if (tok->type == ISL_TOKEN_STRING) {
2121 isl_str *str;
2122 str = isl_str_alloc(s->ctx);
2123 if (!str)
2124 goto error;
2125 str->s = strdup(tok->u.s);
2126 isl_token_free(tok);
2127 obj.v = str;
2128 obj.type = isl_obj_str;
2129 } else
2130 isl_stream_push_token(s, tok);
2131 return obj;
2132 error:
2133 isl_token_free(tok);
2134 return obj;
2137 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2139 struct isl_token *tok;
2140 struct isl_obj obj = { isl_obj_none, NULL };
2142 tok = isl_stream_next_token(s);
2143 if (!tok)
2144 return obj;
2145 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2146 int is_true = tok->type == ISL_TOKEN_TRUE;
2147 isl_token_free(tok);
2148 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2149 obj.type = isl_obj_bool;
2150 } else
2151 isl_stream_push_token(s, tok);
2152 return obj;
2153 error:
2154 isl_token_free(tok);
2155 return obj;
2158 static __isl_give char *read_ident(struct isl_stream *s)
2160 char *name;
2161 struct isl_token *tok, *tok2;
2163 name = isl_stream_read_ident_if_available(s);
2164 if (name)
2165 return name;
2167 tok = isl_stream_next_token(s);
2168 if (!tok)
2169 return NULL;
2170 if (tok->type != '$') {
2171 isl_stream_push_token(s, tok);
2172 return NULL;
2174 tok2 = isl_stream_next_token(s);
2175 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2176 if (tok2)
2177 isl_stream_push_token(s, tok2);
2178 isl_stream_push_token(s, tok);
2179 return NULL;
2182 name = isl_int_get_str(tok2->u.v);
2183 isl_token_free(tok);
2184 isl_token_free(tok2);
2186 return name;
2189 static struct isl_obj read_list(struct isl_stream *s,
2190 struct isl_hash_table *table, struct isl_obj obj)
2192 struct isl_list *list;
2194 list = isl_list_alloc(s->ctx, 2);
2195 if (!list)
2196 goto error;
2197 list->obj[0] = obj;
2198 list->obj[1] = read_obj(s, table);
2199 obj.v = list;
2200 obj.type = isl_obj_list;
2202 if (!list->obj[1].v)
2203 goto error;
2205 while (isl_stream_eat_if_available(s, ',')) {
2206 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2207 if (!obj.v)
2208 goto error;
2211 return obj;
2212 error:
2213 free_obj(obj);
2214 obj.type = isl_obj_none;
2215 obj.v = NULL;
2216 return obj;
2219 static struct isl_obj read_obj(struct isl_stream *s,
2220 struct isl_hash_table *table)
2222 struct isl_obj obj = { isl_obj_none, NULL };
2223 char *name = NULL;
2224 struct isc_un_op *op = NULL;
2226 obj = read_string_if_available(s);
2227 if (obj.v)
2228 return obj;
2229 obj = read_bool_if_available(s);
2230 if (obj.v)
2231 return obj;
2232 if (isl_stream_eat_if_available(s, '(')) {
2233 if (isl_stream_next_token_is(s, ')')) {
2234 obj.type = isl_obj_list;
2235 obj.v = isl_list_alloc(s->ctx, 0);
2236 } else {
2237 obj = read_expr(s, table);
2238 if (obj.v && isl_stream_eat_if_available(s, ','))
2239 obj = read_list(s, table, obj);
2241 if (!obj.v || isl_stream_eat(s, ')'))
2242 goto error;
2243 } else {
2244 op = read_prefix_un_op_if_available(s);
2245 if (op)
2246 return read_un_op_expr(s, table, op);
2248 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2249 return check_assert(s, table);
2250 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2251 return read_from_file(s);
2252 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2253 return write_to_file(s, table);
2254 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2255 return vertices(s, table);
2256 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2257 return any(s, table);
2258 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2259 return last(s, table);
2260 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2261 return schedule(s, table);
2262 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2263 return schedule_forest(s, table);
2264 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2265 return type_of(s, table);
2267 name = read_ident(s);
2268 if (name)
2269 obj = stored_obj(s->ctx, table, name);
2270 else
2271 obj = isl_stream_read_obj(s);
2272 if (!obj.v)
2273 goto error;
2276 if (isl_stream_eat_if_available(s, '^'))
2277 obj = power(s, obj);
2278 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2279 obj = obj_at_index(s, obj);
2280 else if (is_subtype(obj, isl_obj_union_map) &&
2281 isl_stream_eat_if_available(s, '(')) {
2282 obj = convert(s->ctx, obj, isl_obj_union_map);
2283 obj = apply(s, obj.v, table);
2284 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2285 isl_stream_eat_if_available(s, '(')) {
2286 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2287 obj = apply_fun(s, obj, table);
2288 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2289 isl_stream_eat_if_available(s, '(')) {
2290 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2291 obj = apply_fun(s, obj, table);
2294 return obj;
2295 error:
2296 free_obj(obj);
2297 obj.type = isl_obj_none;
2298 obj.v = NULL;
2299 return obj;
2302 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2303 struct isl_obj lhs, struct isl_obj rhs)
2305 int i;
2307 for (i = 0; ; ++i) {
2308 if (!bin_ops[i].op)
2309 break;
2310 if (bin_ops[i].op != like->op)
2311 continue;
2312 if (!is_subtype(lhs, bin_ops[i].lhs))
2313 continue;
2314 if (!is_subtype(rhs, bin_ops[i].rhs))
2315 continue;
2317 return &bin_ops[i];
2320 for (i = 0; ; ++i) {
2321 if (!named_bin_ops[i].name)
2322 break;
2323 if (named_bin_ops[i].op.op != like->op)
2324 continue;
2325 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2326 continue;
2327 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2328 continue;
2330 return &named_bin_ops[i].op;
2333 return NULL;
2336 static int next_is_neg_int(struct isl_stream *s)
2338 struct isl_token *tok;
2339 int ret;
2341 tok = isl_stream_next_token(s);
2342 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2343 isl_stream_push_token(s, tok);
2345 return ret;
2348 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2349 struct isl_obj lhs, struct isl_obj rhs)
2351 struct isl_obj obj;
2353 lhs = convert(ctx, lhs, op->lhs);
2354 rhs = convert(ctx, rhs, op->rhs);
2355 if (op->res != isl_obj_bool)
2356 obj.v = op->o.fn(lhs.v, rhs.v);
2357 else {
2358 int res = op->o.test(lhs.v, rhs.v);
2359 free_obj(lhs);
2360 free_obj(rhs);
2361 obj.v = isl_bool_from_int(res);
2363 obj.type = op->res;
2365 return obj;
2368 static struct isl_obj read_expr(struct isl_stream *s,
2369 struct isl_hash_table *table)
2371 struct isl_obj obj = { isl_obj_none, NULL };
2372 struct isl_obj right_obj = { isl_obj_none, NULL };
2374 obj = read_obj(s, table);
2375 for (; obj.v;) {
2376 struct isc_bin_op *op = NULL;
2378 op = read_bin_op_if_available(s, obj);
2379 if (!op)
2380 break;
2382 right_obj = read_obj(s, table);
2384 op = find_matching_bin_op(op, obj, right_obj);
2386 if (!op)
2387 isl_die(s->ctx, isl_error_invalid,
2388 "no such binary operator defined on given operands",
2389 goto error);
2391 obj = call_bin_op(s->ctx, op, obj, right_obj);
2394 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2395 right_obj = read_obj(s, table);
2396 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2399 return obj;
2400 error:
2401 free_obj(right_obj);
2402 free_obj(obj);
2403 obj.type = isl_obj_none;
2404 obj.v = NULL;
2405 return obj;
2408 static __isl_give isl_printer *source_file(struct isl_stream *s,
2409 struct isl_hash_table *table, __isl_take isl_printer *p);
2411 static __isl_give isl_printer *read_line(struct isl_stream *s,
2412 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2414 struct isl_obj obj = { isl_obj_none, NULL };
2415 char *lhs = NULL;
2416 int assign = 0;
2417 int only_print = 0;
2418 struct isc_bin_op *op = NULL;
2419 char buf[30];
2421 if (!p)
2422 return NULL;
2423 if (isl_stream_is_empty(s))
2424 return p;
2426 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2427 return source_file(s, table, p);
2429 assign = is_assign(s);
2430 if (assign) {
2431 lhs = isl_stream_read_ident_if_available(s);
2432 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2433 goto error;
2434 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2435 only_print = 1;
2436 else if (!tty)
2437 only_print = 1;
2439 obj = read_expr(s, table);
2440 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2441 fprintf(stderr, "Interrupted\n");
2442 isl_ctx_reset_error(s->ctx);
2444 if (isl_stream_eat(s, ';'))
2445 goto error;
2447 if (only_print) {
2448 if (obj.type != isl_obj_none && obj.v != NULL) {
2449 p = obj.type->print(p, obj.v);
2450 p = isl_printer_end_line(p);
2452 free_obj(obj);
2453 return p;
2455 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2456 static int count = 0;
2457 snprintf(buf, sizeof(buf), "$%d", count++);
2458 lhs = strdup(buf + 1);
2460 p = isl_printer_print_str(p, buf);
2461 p = isl_printer_print_str(p, " := ");
2462 p = obj.type->print(p, obj.v);
2463 p = isl_printer_end_line(p);
2465 if (lhs && do_assign(s->ctx, table, lhs, obj))
2466 return p;
2468 return p;
2469 error:
2470 isl_stream_flush_tokens(s);
2471 isl_stream_skip_line(s);
2472 free(lhs);
2473 free_obj(obj);
2474 return p;
2477 int free_cb(void **entry, void *user)
2479 struct isl_named_obj *named = *entry;
2481 free_obj(named->obj);
2482 free(named->name);
2483 free(named);
2485 return 0;
2488 static void register_named_ops(struct isl_stream *s)
2490 int i;
2492 for (i = 0; i < ISCC_N_OP; ++i) {
2493 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2494 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2497 for (i = 0; ; ++i) {
2498 if (!named_un_ops[i].name)
2499 break;
2500 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2501 named_un_ops[i].name);
2502 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2505 for (i = 0; ; ++i) {
2506 if (!named_bin_ops[i].name)
2507 break;
2508 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2509 named_bin_ops[i].name);
2510 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2514 static __isl_give isl_printer *source_file(struct isl_stream *s,
2515 struct isl_hash_table *table, __isl_take isl_printer *p)
2517 struct isl_token *tok;
2518 struct isl_stream *s_file;
2519 FILE *file;
2521 tok = isl_stream_next_token(s);
2522 if (!tok || tok->type != ISL_TOKEN_STRING) {
2523 isl_stream_error(s, tok, "expecting filename");
2524 isl_token_free(tok);
2525 return p;
2528 file = fopen(tok->u.s, "r");
2529 isl_token_free(tok);
2530 isl_assert(s->ctx, file, return p);
2532 s_file = isl_stream_new_file(s->ctx, file);
2533 if (!s_file) {
2534 fclose(file);
2535 return p;
2538 register_named_ops(s_file);
2540 while (!s_file->eof)
2541 p = read_line(s_file, table, p, 0);
2543 isl_stream_free(s_file);
2544 fclose(file);
2546 isl_stream_eat(s, ';');
2548 return p;
2551 int main(int argc, char **argv)
2553 struct isl_ctx *ctx;
2554 struct isl_stream *s;
2555 struct isl_hash_table *table;
2556 struct iscc_options *options;
2557 isl_printer *p;
2558 int tty = isatty(0);
2560 options = iscc_options_new_with_defaults();
2561 assert(options);
2563 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2564 pet_options_set_autodetect(ctx, 1);
2565 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2566 s = isl_stream_new_file(ctx, stdin);
2567 assert(s);
2568 table = isl_hash_table_alloc(ctx, 10);
2569 assert(table);
2570 p = isl_printer_to_file(ctx, stdout);
2571 p = isl_printer_set_output_format(p, options->format);
2572 assert(p);
2574 register_named_ops(s);
2576 install_signal_handler(ctx);
2578 while (p && !s->eof) {
2579 isl_ctx_resume(ctx);
2580 p = read_line(s, table, p, tty);
2583 remove_signal_handler(ctx);
2585 isl_printer_free(p);
2586 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2587 isl_hash_table_free(ctx, table);
2588 isl_stream_free(s);
2589 isl_ctx_free(ctx);
2591 return 0;