update pet for support for recent clangs
[barvinok.git] / iscc.c
blob44a8384e4a4450be1a901fcd0fe8f0ae307317fc
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/vertices.h>
9 #include <isl/flow.h>
10 #include <isl/band.h>
11 #include <isl/schedule.h>
12 #include <isl_obj_list.h>
13 #include <isl_obj_str.h>
14 #include <barvinok/isl.h>
15 #include <barvinok/options.h>
16 #include "lattice_width.h"
18 #include "config.h"
20 #ifdef HAVE_SIGACTION
21 #include <signal.h>
23 static isl_ctx *main_ctx;
25 static void handler(int signum)
27 if (isl_ctx_aborted(main_ctx))
28 exit(EXIT_FAILURE);
29 isl_ctx_abort(main_ctx);
32 static struct sigaction sa_old;
34 static void install_signal_handler(isl_ctx *ctx)
36 struct sigaction sa;
38 main_ctx = ctx;
40 memset(&sa, 0, sizeof(struct sigaction));
41 sa.sa_handler = &handler;
42 sa.sa_flags = SA_RESTART;
43 sigaction(SIGINT, &sa, &sa_old);
46 static void remove_signal_handler(isl_ctx *ctx)
48 sigaction(SIGINT, &sa_old, NULL);
51 #else
53 static void install_signal_handler(isl_ctx *ctx)
57 static void remove_signal_handler(isl_ctx *ctx)
61 #endif
63 #ifdef HAVE_CLOOG
64 #include <cloog/isl/cloog.h>
65 #endif
67 #ifdef HAVE_PET
68 #include <pet.h>
69 #endif
71 static int isl_bool_false = 0;
72 static int isl_bool_true = 1;
73 static int isl_bool_error = -1;
75 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
76 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
77 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
78 ISCC_MINIMIZING, ISCC_RESPECTING,
79 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
80 ISCC_N_OP };
81 static const char *op_name[ISCC_N_OP] = {
82 [ISCC_ASSERT] = "assert",
83 [ISCC_READ] = "read",
84 [ISCC_WRITE] = "write",
85 [ISCC_PRINT] = "print",
86 [ISCC_SOURCE] = "source",
87 [ISCC_VERTICES] = "vertices",
88 [ISCC_LAST] = "last",
89 [ISCC_ANY] = "any",
90 [ISCC_BEFORE] = "before",
91 [ISCC_UNDER] = "under",
92 [ISCC_SCHEDULE] = "schedule",
93 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
94 [ISCC_MINIMIZING] = "minimizing",
95 [ISCC_RESPECTING] = "respecting",
96 [ISCC_TYPEOF] = "typeof"
98 static enum isl_token_type iscc_op[ISCC_N_OP];
100 struct isl_arg_choice iscc_format[] = {
101 {"isl", ISL_FORMAT_ISL},
102 {"omega", ISL_FORMAT_OMEGA},
103 {"polylib", ISL_FORMAT_POLYLIB},
104 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
105 {"latex", ISL_FORMAT_LATEX},
106 {"C", ISL_FORMAT_C},
110 struct iscc_options {
111 struct barvinok_options *barvinok;
112 unsigned format;
113 int io;
116 struct isl_arg iscc_options_arg[] = {
117 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", barvinok_options_arg,
118 "barvinok options")
119 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
120 iscc_format, ISL_FORMAT_ISL, "output format")
121 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
122 "allow read and write operations")
123 ISL_ARG_END
126 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_arg)
127 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_arg)
129 static void *isl_obj_bool_copy(void *v)
131 return v;
134 static void isl_obj_bool_free(void *v)
138 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
139 void *v)
141 if (v == &isl_bool_true)
142 return isl_printer_print_str(p, "True");
143 else if (v == &isl_bool_false)
144 return isl_printer_print_str(p, "False");
145 else
146 return isl_printer_print_str(p, "Error");
149 static void *isl_obj_bool_add(void *v1, void *v2)
151 return v1;
154 struct isl_obj_vtable isl_obj_bool_vtable = {
155 isl_obj_bool_copy,
156 isl_obj_bool_add,
157 isl_obj_bool_print,
158 isl_obj_bool_free
160 #define isl_obj_bool (&isl_obj_bool_vtable)
162 int *isl_bool_from_int(int res)
164 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
167 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
168 __isl_take isl_union_map *map2)
170 return isl_union_map_is_subset(map2, map1);
172 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
173 __isl_take isl_union_set *set2)
175 return isl_union_set_is_subset(set2, set1);
178 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
179 __isl_take isl_union_map *map2)
181 return isl_union_map_is_strict_subset(map2, map1);
183 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
184 __isl_take isl_union_set *set2)
186 return isl_union_set_is_strict_subset(set2, set1);
189 extern struct isl_obj_vtable isl_obj_list_vtable;
190 #define isl_obj_list (&isl_obj_list_vtable)
192 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
193 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
194 struct isc_bin_op {
195 enum isl_token_type op;
196 isl_obj_type lhs;
197 isl_obj_type rhs;
198 isl_obj_type res;
199 union {
200 isc_bin_op_fn fn;
201 isc_bin_test_fn test;
202 } o;
204 struct isc_named_bin_op {
205 char *name;
206 struct isc_bin_op op;
209 struct iscc_at {
210 isl_union_pw_qpolynomial *upwqp;
211 isl_union_pw_qpolynomial *res;
214 static int eval_at(__isl_take isl_point *pnt, void *user)
216 struct iscc_at *at = (struct iscc_at *) user;
217 isl_qpolynomial *qp;
218 isl_set *set;
220 set = isl_set_from_point(isl_point_copy(pnt));
221 qp = isl_union_pw_qpolynomial_eval(
222 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
224 at->res = isl_union_pw_qpolynomial_add(at->res,
225 isl_union_pw_qpolynomial_from_pw_qpolynomial(
226 isl_pw_qpolynomial_alloc(set, qp)));
228 return 0;
231 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
232 __isl_take isl_union_pw_qpolynomial *upwqp,
233 __isl_take isl_union_set *uset)
235 struct iscc_at at;
237 at.upwqp = upwqp;
238 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
240 isl_union_set_foreach_point(uset, eval_at, &at);
242 isl_union_pw_qpolynomial_free(upwqp);
243 isl_union_set_free(uset);
245 return at.res;
248 struct iscc_fold_at {
249 isl_union_pw_qpolynomial_fold *upwf;
250 isl_union_pw_qpolynomial *res;
253 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
255 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
256 isl_qpolynomial *qp;
257 isl_set *set;
259 set = isl_set_from_point(isl_point_copy(pnt));
260 qp = isl_union_pw_qpolynomial_fold_eval(
261 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
263 at->res = isl_union_pw_qpolynomial_add(at->res,
264 isl_union_pw_qpolynomial_from_pw_qpolynomial(
265 isl_pw_qpolynomial_alloc(set, qp)));
267 return 0;
270 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
271 __isl_take isl_union_pw_qpolynomial_fold *upwf,
272 __isl_take isl_union_set *uset)
274 struct iscc_fold_at at;
276 at.upwf = upwf;
277 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
279 isl_union_set_foreach_point(uset, eval_fold_at, &at);
281 isl_union_pw_qpolynomial_fold_free(upwf);
282 isl_union_set_free(uset);
284 return at.res;
287 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
288 __isl_take isl_union_pw_qpolynomial *upwqp,
289 __isl_take isl_union_pw_qpolynomial_fold *upwf)
291 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
292 upwqp);
295 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
296 __isl_take isl_union_map *umap,
297 __isl_take isl_union_pw_qpolynomial_fold *upwf)
299 isl_ctx *ctx;
300 struct isl_list *list;
301 int tight;
303 ctx = isl_union_map_get_ctx(umap);
304 list = isl_list_alloc(ctx, 2);
305 if (!list)
306 goto error2;
308 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
309 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
310 upwf, &tight);
311 list->obj[1].type = isl_obj_bool;
312 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
313 if (tight < 0 || !list->obj[0].v)
314 goto error;
316 return list;
317 error2:
318 isl_union_map_free(umap);
319 isl_union_pw_qpolynomial_fold_free(upwf);
320 error:
321 isl_list_free(list);
322 return NULL;
325 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
326 __isl_take isl_union_set *uset,
327 __isl_take isl_union_pw_qpolynomial_fold *upwf)
329 isl_ctx *ctx;
330 struct isl_list *list;
331 int tight;
333 ctx = isl_union_set_get_ctx(uset);
334 list = isl_list_alloc(ctx, 2);
335 if (!list)
336 goto error2;
338 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
339 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
340 upwf, &tight);
341 list->obj[1].type = isl_obj_bool;
342 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
343 if (tight < 0 || !list->obj[0].v)
344 goto error;
346 return list;
347 error2:
348 isl_union_set_free(uset);
349 isl_union_pw_qpolynomial_fold_free(upwf);
350 error:
351 isl_list_free(list);
352 return NULL;
355 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_int_mul(
356 __isl_take isl_union_pw_qpolynomial *upwqp, __isl_take isl_int_obj *i)
358 isl_int v;
360 if (!i)
361 goto error;
363 isl_int_init(v);
364 isl_int_obj_get_int(i, &v);
365 upwqp = isl_union_pw_qpolynomial_mul_isl_int(upwqp, v);
366 isl_int_clear(v);
368 isl_int_obj_free(i);
370 return upwqp;
371 error:
372 isl_union_pw_qpolynomial_free(upwqp);
373 return NULL;
376 static __isl_give isl_union_pw_qpolynomial *int_union_pw_qpolynomial_mul(
377 __isl_take isl_int_obj *i, __isl_take isl_union_pw_qpolynomial *upwqp)
379 return union_pw_qpolynomial_int_mul(upwqp, i);
382 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_fold_int_mul(
383 __isl_take isl_union_pw_qpolynomial_fold *upwf,
384 __isl_take isl_int_obj *i)
386 isl_int v;
388 if (!i)
389 goto error;
391 isl_int_init(v);
392 isl_int_obj_get_int(i, &v);
393 upwf = isl_union_pw_qpolynomial_fold_mul_isl_int(upwf, v);
394 isl_int_clear(v);
396 isl_int_obj_free(i);
398 return upwf;
399 error:
400 isl_union_pw_qpolynomial_fold_free(upwf);
401 return NULL;
404 static __isl_give isl_union_pw_qpolynomial_fold *int_union_pw_qpolynomial_fold_mul(
405 __isl_take isl_int_obj *i,
406 __isl_take isl_union_pw_qpolynomial_fold *upwf)
408 return union_pw_qpolynomial_fold_int_mul(upwf, i);
411 struct isc_bin_op bin_ops[] = {
412 { '+', isl_obj_int, isl_obj_int, isl_obj_int,
413 (isc_bin_op_fn) &isl_int_obj_add },
414 { '-', isl_obj_int, isl_obj_int, isl_obj_int,
415 (isc_bin_op_fn) &isl_int_obj_sub },
416 { '*', isl_obj_int, isl_obj_int, isl_obj_int,
417 (isc_bin_op_fn) &isl_int_obj_mul },
418 { '+', isl_obj_union_set, isl_obj_union_set,
419 isl_obj_union_set,
420 (isc_bin_op_fn) &isl_union_set_union },
421 { '+', isl_obj_union_map, isl_obj_union_map,
422 isl_obj_union_map,
423 (isc_bin_op_fn) &isl_union_map_union },
424 { '-', isl_obj_union_set, isl_obj_union_set,
425 isl_obj_union_set,
426 (isc_bin_op_fn) &isl_union_set_subtract },
427 { '-', isl_obj_union_map, isl_obj_union_map,
428 isl_obj_union_map,
429 (isc_bin_op_fn) &isl_union_map_subtract },
430 { '*', isl_obj_union_set, isl_obj_union_set,
431 isl_obj_union_set,
432 (isc_bin_op_fn) &isl_union_set_intersect },
433 { '*', isl_obj_union_map, isl_obj_union_map,
434 isl_obj_union_map,
435 (isc_bin_op_fn) &isl_union_map_intersect },
436 { '*', isl_obj_union_map, isl_obj_union_set,
437 isl_obj_union_map,
438 (isc_bin_op_fn) &isl_union_map_intersect_domain },
439 { '.', isl_obj_union_map, isl_obj_union_map,
440 isl_obj_union_map,
441 (isc_bin_op_fn) &isl_union_map_apply_range },
442 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
443 isl_obj_union_pw_qpolynomial,
444 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
445 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
446 isl_obj_list,
447 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
448 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
449 isl_obj_union_map,
450 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
451 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
452 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
453 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
454 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
455 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
456 isl_obj_bool,
457 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
458 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
459 isl_obj_bool,
460 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
461 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
462 isl_obj_bool,
463 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
464 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
465 isl_obj_bool,
466 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
467 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
468 isl_obj_bool,
469 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
470 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
471 isl_obj_bool,
472 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
473 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
474 isl_obj_bool,
475 { .test =
476 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
477 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
478 isl_obj_bool,
479 { .test =
480 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
481 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
482 isl_obj_union_map,
483 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
484 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
485 isl_obj_union_map,
486 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
487 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
488 isl_obj_union_map,
489 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
490 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
491 isl_obj_union_map,
492 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
493 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
494 isl_obj_union_map,
495 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
496 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
497 isl_obj_union_map,
498 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
499 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
500 isl_obj_union_map,
501 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
502 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
503 isl_obj_union_map,
504 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
505 { '.', isl_obj_union_pw_qpolynomial_fold,
506 isl_obj_union_pw_qpolynomial_fold,
507 isl_obj_union_pw_qpolynomial_fold,
508 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
509 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
510 isl_obj_union_pw_qpolynomial,
511 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
512 { '+', isl_obj_union_pw_qpolynomial,
513 isl_obj_union_pw_qpolynomial_fold,
514 isl_obj_union_pw_qpolynomial_fold,
515 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
516 { '+', isl_obj_union_pw_qpolynomial_fold,
517 isl_obj_union_pw_qpolynomial,
518 isl_obj_union_pw_qpolynomial_fold,
519 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
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_sub },
523 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial,
524 isl_obj_union_pw_qpolynomial,
525 (isc_bin_op_fn) &int_union_pw_qpolynomial_mul },
526 { '*', isl_obj_union_pw_qpolynomial, isl_obj_int,
527 isl_obj_union_pw_qpolynomial,
528 (isc_bin_op_fn) &union_pw_qpolynomial_int_mul },
529 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial_fold,
530 isl_obj_union_pw_qpolynomial_fold,
531 (isc_bin_op_fn) &int_union_pw_qpolynomial_fold_mul },
532 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_int,
533 isl_obj_union_pw_qpolynomial_fold,
534 (isc_bin_op_fn) &union_pw_qpolynomial_fold_int_mul },
535 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
536 isl_obj_union_pw_qpolynomial,
537 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
538 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
539 isl_obj_union_pw_qpolynomial,
540 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
541 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
542 isl_obj_union_pw_qpolynomial_fold,
543 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
544 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
545 isl_obj_union_pw_qpolynomial,
546 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
547 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
548 isl_obj_union_pw_qpolynomial,
549 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
550 { '%', isl_obj_union_set, isl_obj_union_set,
551 isl_obj_union_set,
552 (isc_bin_op_fn) &isl_union_set_gist },
553 { '%', isl_obj_union_map, isl_obj_union_map,
554 isl_obj_union_map,
555 (isc_bin_op_fn) &isl_union_map_gist },
556 { '%', isl_obj_union_map, isl_obj_union_set,
557 isl_obj_union_map,
558 (isc_bin_op_fn) &isl_union_map_gist_domain },
559 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
560 isl_obj_union_pw_qpolynomial,
561 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
562 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
563 isl_obj_union_pw_qpolynomial_fold,
564 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
565 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
566 isl_obj_union_pw_qpolynomial, isl_obj_bool,
567 { .test = (isc_bin_test_fn)
568 &isl_union_pw_qpolynomial_plain_is_equal } },
569 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
570 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
571 { .test = (isc_bin_test_fn)
572 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
573 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
574 (isc_bin_op_fn) &isl_str_concat },
578 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
579 __isl_take isl_union_map *umap2)
581 return isl_union_map_apply_range(umap2, umap1);
584 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
585 __isl_take isl_union_pw_qpolynomial *upwqp,
586 __isl_take isl_union_map *umap)
588 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
591 static __isl_give struct isl_list *qpolynomial_fold_after_map(
592 __isl_take isl_union_pw_qpolynomial_fold *upwf,
593 __isl_take isl_union_map *umap)
595 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
598 struct isc_named_bin_op named_bin_ops[] = {
599 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
600 isl_obj_union_map,
601 (isc_bin_op_fn) &map_after_map } },
602 { "after", { -1, isl_obj_union_pw_qpolynomial,
603 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
604 (isc_bin_op_fn) &qpolynomial_after_map } },
605 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
606 isl_obj_union_map, isl_obj_list,
607 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
608 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
609 isl_obj_union_map,
610 (isc_bin_op_fn) &isl_union_map_apply_range } },
611 { "before", { -1, isl_obj_union_map,
612 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
613 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
614 { "before", { -1, isl_obj_union_map,
615 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
616 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
617 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
618 isl_obj_union_set,
619 (isc_bin_op_fn) &isl_union_set_product } },
620 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
621 isl_obj_union_map,
622 (isc_bin_op_fn) &isl_union_map_product } },
623 NULL
626 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
628 return isl_set_from_basic_set(isl_union_set_sample(uset));
631 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
633 return isl_map_from_basic_map(isl_union_map_sample(umap));
636 static __isl_give struct isl_list *union_map_power(
637 __isl_take isl_union_map *umap)
639 isl_ctx *ctx;
640 struct isl_list *list;
641 int exact;
643 ctx = isl_union_map_get_ctx(umap);
644 list = isl_list_alloc(ctx, 2);
645 if (!list)
646 goto error2;
648 list->obj[0].type = isl_obj_union_map;
649 list->obj[0].v = isl_union_map_power(umap, &exact);
650 list->obj[1].type = isl_obj_bool;
651 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
652 if (exact < 0 || !list->obj[0].v)
653 goto error;
655 return list;
656 error2:
657 isl_union_map_free(umap);
658 error:
659 isl_list_free(list);
660 return NULL;
663 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
664 __isl_take isl_union_pw_qpolynomial *upwqp)
666 isl_ctx *ctx;
667 struct isl_list *list;
668 int tight;
670 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
671 list = isl_list_alloc(ctx, 2);
672 if (!list)
673 goto error2;
675 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
676 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
677 isl_fold_max, &tight);
678 list->obj[1].type = isl_obj_bool;
679 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
680 if (tight < 0 || !list->obj[0].v)
681 goto error;
683 return list;
684 error2:
685 isl_union_pw_qpolynomial_free(upwqp);
686 error:
687 isl_list_free(list);
688 return NULL;
691 #ifdef HAVE_CLOOG
692 void *map_codegen(void *arg)
694 isl_space *dim;
695 isl_union_map *umap = (isl_union_map *)arg;
696 isl_ctx *ctx = isl_union_map_get_ctx(umap);
697 CloogState *state;
698 CloogOptions *options;
699 CloogDomain *context;
700 CloogUnionDomain *ud;
701 CloogInput *input;
702 struct clast_stmt *stmt;
704 state = cloog_isl_state_malloc(ctx);
705 options = cloog_options_malloc(state);
706 options->language = CLOOG_LANGUAGE_C;
707 options->strides = 1;
708 options->sh = 1;
710 ud = cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap));
712 dim = isl_union_map_get_space(umap);
713 context = cloog_domain_from_isl_set(isl_set_universe(dim));
715 input = cloog_input_alloc(context, ud);
717 stmt = cloog_clast_create_from_input(input, options);
718 clast_pprint(stdout, stmt, 0, options);
719 cloog_clast_free(stmt);
721 error:
722 cloog_options_free(options);
723 cloog_state_free(state);
724 isl_union_map_free(umap);
725 return NULL;
728 void *set_codegen(void *arg)
730 isl_space *dim;
731 isl_union_set *uset = (isl_union_set *)arg;
732 isl_set *set;
733 isl_ctx *ctx = isl_union_set_get_ctx(uset);
734 CloogState *state;
735 CloogOptions *options;
736 CloogDomain *context;
737 CloogUnionDomain *ud;
738 CloogInput *input;
739 struct clast_stmt *stmt;
741 if (isl_union_set_n_set(uset) > 1)
742 isl_die(ctx, isl_error_invalid,
743 "code generation for more than one domain "
744 "requires a schedule", goto error);
746 state = cloog_isl_state_malloc(ctx);
747 options = cloog_options_malloc(state);
748 options->language = CLOOG_LANGUAGE_C;
749 options->strides = 1;
750 options->sh = 1;
752 set = isl_set_from_union_set(isl_union_set_copy(uset));
753 ud = cloog_union_domain_from_isl_set(set);
755 dim = isl_union_set_get_space(uset);
756 context = cloog_domain_from_isl_set(isl_set_universe(dim));
758 input = cloog_input_alloc(context, ud);
760 stmt = cloog_clast_create_from_input(input, options);
761 clast_pprint(stdout, stmt, 0, options);
762 cloog_clast_free(stmt);
764 cloog_options_free(options);
765 cloog_state_free(state);
766 error:
767 isl_union_set_free(uset);
768 return NULL;
770 #endif
772 #ifdef HAVE_PET
773 static __isl_give isl_list *parse(__isl_take isl_str *str)
775 isl_ctx *ctx;
776 struct isl_list *list;
777 struct pet_scop *scop;
778 isl_union_map *sched, *reads, *writes;
779 isl_union_set *domain;
780 struct iscc_options *options;
782 if (!str)
783 return NULL;
784 ctx = str->ctx;
786 options = isl_ctx_peek_iscc_options(ctx);
787 if (!options || !options->io) {
788 isl_str_free(str);
789 isl_die(ctx, isl_error_invalid,
790 "parse_file operation not allowed", return NULL);
793 list = isl_list_alloc(ctx, 4);
794 if (!list)
795 goto error;
797 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL, 1);
798 domain = pet_scop_collect_domains(scop);
799 sched = pet_scop_collect_schedule(scop);
800 reads = pet_scop_collect_reads(scop);
801 writes = pet_scop_collect_writes(scop);
802 pet_scop_free(scop);
804 list->obj[0].type = isl_obj_union_set;
805 list->obj[0].v = domain;
806 list->obj[1].type = isl_obj_union_map;
807 list->obj[1].v = writes;
808 list->obj[2].type = isl_obj_union_map;
809 list->obj[2].v = reads;
810 list->obj[3].type = isl_obj_union_map;
811 list->obj[3].v = sched;
813 if (!list->obj[0].v || !list->obj[1].v ||
814 !list->obj[2].v || !list->obj[3].v)
815 goto error;
817 isl_str_free(str);
818 return list;
819 error:
820 isl_list_free(list);
821 isl_str_free(str);
822 return NULL;
824 #endif
826 static int add_point(__isl_take isl_point *pnt, void *user)
828 isl_union_set **scan = (isl_union_set **) user;
830 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
832 return 0;
835 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
837 isl_union_set *scan;
839 scan = isl_union_set_empty(isl_union_set_get_space(uset));
841 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
842 isl_union_set_free(scan);
843 return uset;
846 isl_union_set_free(uset);
847 return scan;
850 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
852 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
855 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
856 __isl_take isl_union_pw_qpolynomial *upwqp)
858 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
861 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
862 __isl_take isl_union_pw_qpolynomial *upwqp)
864 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
867 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
868 __isl_take isl_union_pw_qpolynomial *upwqp)
870 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
873 typedef void *(*isc_un_op_fn)(void *arg);
874 struct isc_un_op {
875 enum isl_token_type op;
876 isl_obj_type arg;
877 isl_obj_type res;
878 isc_un_op_fn fn;
880 struct isc_named_un_op {
881 char *name;
882 struct isc_un_op op;
884 struct isc_named_un_op named_un_ops[] = {
885 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
886 (isc_un_op_fn) &isl_union_map_affine_hull } },
887 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
888 (isc_un_op_fn) &isl_union_set_affine_hull } },
889 {"card", { -1, isl_obj_union_set,
890 isl_obj_union_pw_qpolynomial,
891 (isc_un_op_fn) &isl_union_set_card } },
892 {"card", { -1, isl_obj_union_map,
893 isl_obj_union_pw_qpolynomial,
894 (isc_un_op_fn) &isl_union_map_card } },
895 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
896 (isc_un_op_fn) &isl_union_set_coalesce } },
897 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
898 (isc_un_op_fn) &isl_union_map_coalesce } },
899 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
900 isl_obj_union_pw_qpolynomial,
901 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
902 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
903 isl_obj_union_pw_qpolynomial_fold,
904 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
905 #ifdef HAVE_CLOOG
906 {"codegen", { -1, isl_obj_union_set, isl_obj_none,
907 &set_codegen } },
908 {"codegen", { -1, isl_obj_union_map, isl_obj_none,
909 &map_codegen } },
910 #endif
911 {"coefficients", { -1, isl_obj_union_set,
912 isl_obj_union_set,
913 (isc_un_op_fn) &isl_union_set_coefficients } },
914 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
915 (isc_un_op_fn) &isl_union_set_solutions } },
916 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
917 (isc_un_op_fn) &isl_union_map_deltas } },
918 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
919 (isc_un_op_fn) &isl_union_map_deltas_map } },
920 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
921 (isc_un_op_fn) &isl_union_map_domain } },
922 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
923 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
924 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
925 isl_obj_union_set,
926 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
927 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
928 (isc_un_op_fn) &isl_union_map_domain } },
929 {"domain", { -1, isl_obj_union_pw_qpolynomial,
930 isl_obj_union_set,
931 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
932 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
933 isl_obj_union_set,
934 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
935 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
936 (isc_un_op_fn) &isl_union_map_domain_map } },
937 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
938 (isc_un_op_fn) &isl_union_map_range } },
939 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
940 (isc_un_op_fn) &isl_union_map_range } },
941 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
942 (isc_un_op_fn) &isl_union_map_range_map } },
943 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
944 (isc_un_op_fn) &isl_union_set_identity } },
945 {"lattice_width", { -1, isl_obj_union_set,
946 isl_obj_union_pw_qpolynomial,
947 (isc_un_op_fn) &isl_union_set_lattice_width } },
948 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
949 (isc_un_op_fn) &isl_union_map_lexmin } },
950 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
951 (isc_un_op_fn) &isl_union_map_lexmax } },
952 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
953 (isc_un_op_fn) &isl_union_set_lexmin } },
954 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
955 (isc_un_op_fn) &isl_union_set_lexmax } },
956 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
957 (isc_un_op_fn) &isl_union_set_lift } },
958 {"params", { -1, isl_obj_union_map, isl_obj_set,
959 (isc_un_op_fn) &isl_union_map_params } },
960 {"params", { -1, isl_obj_union_set, isl_obj_set,
961 (isc_un_op_fn) &isl_union_set_params } },
962 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
963 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
964 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
965 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
966 {"poly", { -1, isl_obj_union_pw_qpolynomial,
967 isl_obj_union_pw_qpolynomial,
968 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
969 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
970 isl_obj_union_pw_qpolynomial,
971 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
972 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
973 isl_obj_union_pw_qpolynomial,
974 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
975 #ifdef HAVE_PET
976 {"parse_file", { -1, isl_obj_str, isl_obj_list,
977 (isc_un_op_fn) &parse } },
978 #endif
979 {"pow", { -1, isl_obj_union_map, isl_obj_list,
980 (isc_un_op_fn) &union_map_power } },
981 {"sample", { -1, isl_obj_union_set, isl_obj_set,
982 (isc_un_op_fn) &union_set_sample } },
983 {"sample", { -1, isl_obj_union_map, isl_obj_map,
984 (isc_un_op_fn) &union_map_sample } },
985 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
986 (isc_un_op_fn) &union_set_scan } },
987 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
988 (isc_un_op_fn) &union_map_scan } },
989 {"sum", { -1, isl_obj_union_pw_qpolynomial,
990 isl_obj_union_pw_qpolynomial,
991 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
992 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
993 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
994 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
995 (isc_un_op_fn) &isl_union_set_unwrap } },
996 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
997 (isc_un_op_fn) &isl_union_map_wrap } },
998 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
999 (isc_un_op_fn) &isl_union_map_zip } },
1000 NULL
1003 struct isl_named_obj {
1004 char *name;
1005 struct isl_obj obj;
1008 static void free_obj(struct isl_obj obj)
1010 obj.type->free(obj.v);
1013 static int same_name(const void *entry, const void *val)
1015 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1017 return !strcmp(named->name, val);
1020 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1021 char *name, struct isl_obj obj)
1023 struct isl_hash_table_entry *entry;
1024 uint32_t name_hash;
1025 struct isl_named_obj *named;
1027 name_hash = isl_hash_string(isl_hash_init(), name);
1028 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1029 if (!entry)
1030 goto error;
1031 if (entry->data) {
1032 named = entry->data;
1033 free_obj(named->obj);
1034 free(name);
1035 } else {
1036 named = isl_alloc_type(ctx, struct isl_named_obj);
1037 if (!named)
1038 goto error;
1039 named->name = name;
1040 entry->data = named;
1042 named->obj = obj;
1044 return 0;
1045 error:
1046 free_obj(obj);
1047 free(name);
1048 return -1;
1051 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1052 struct isl_hash_table *table, char *name)
1054 struct isl_obj obj = { isl_obj_none, NULL };
1055 struct isl_hash_table_entry *entry;
1056 uint32_t name_hash;
1058 name_hash = isl_hash_string(isl_hash_init(), name);
1059 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1060 if (entry) {
1061 struct isl_named_obj *named;
1062 named = entry->data;
1063 obj = named->obj;
1064 } else if (isdigit(name[0]))
1065 fprintf(stderr, "unknown identifier '$%s'\n", name);
1066 else
1067 fprintf(stderr, "unknown identifier '%s'\n", name);
1069 free(name);
1070 obj.v = obj.type->copy(obj.v);
1071 return obj;
1074 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1076 if (obj.type == super)
1077 return 1;
1078 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1079 return 1;
1080 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1081 return 1;
1082 if (obj.type == isl_obj_pw_qpolynomial &&
1083 super == isl_obj_union_pw_qpolynomial)
1084 return 1;
1085 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1086 super == isl_obj_union_pw_qpolynomial_fold)
1087 return 1;
1088 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1089 return 1;
1090 if (obj.type == isl_obj_list) {
1091 struct isl_list *list = obj.v;
1092 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1093 return is_subtype(list->obj[0], super);
1095 if (super == isl_obj_str)
1096 return 1;
1097 return 0;
1100 static struct isl_obj obj_at(struct isl_obj obj, int i)
1102 struct isl_list *list = obj.v;
1104 obj = list->obj[i];
1105 obj.v = obj.type->copy(obj.v);
1107 isl_list_free(list);
1109 return obj;
1112 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1113 isl_obj_type type)
1115 if (obj.type == type)
1116 return obj;
1117 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1118 obj.type = isl_obj_union_map;
1119 obj.v = isl_union_map_from_map(obj.v);
1120 return obj;
1122 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1123 obj.type = isl_obj_union_set;
1124 obj.v = isl_union_set_from_set(obj.v);
1125 return obj;
1127 if (obj.type == isl_obj_pw_qpolynomial &&
1128 type == isl_obj_union_pw_qpolynomial) {
1129 obj.type = isl_obj_union_pw_qpolynomial;
1130 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1131 return obj;
1133 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1134 type == isl_obj_union_pw_qpolynomial_fold) {
1135 obj.type = isl_obj_union_pw_qpolynomial_fold;
1136 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1137 return obj;
1139 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1140 if (type == isl_obj_union_map) {
1141 obj.type = isl_obj_union_map;
1142 return obj;
1144 if (type == isl_obj_union_pw_qpolynomial) {
1145 isl_space *dim = isl_union_set_get_space(obj.v);
1146 isl_union_set_free(obj.v);
1147 obj.v = isl_union_pw_qpolynomial_zero(dim);
1148 obj.type = isl_obj_union_pw_qpolynomial;
1149 return obj;
1151 if (type == isl_obj_union_pw_qpolynomial_fold) {
1152 isl_space *dim = isl_union_set_get_space(obj.v);
1153 isl_union_set_free(obj.v);
1154 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1155 isl_fold_list);
1156 obj.type = isl_obj_union_pw_qpolynomial_fold;
1157 return obj;
1160 if (obj.type == isl_obj_list) {
1161 struct isl_list *list = obj.v;
1162 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1163 return convert(ctx, obj_at(obj, 0), type);
1165 if (type == isl_obj_str) {
1166 isl_str *str;
1167 isl_printer *p;
1168 char *s;
1170 p = isl_printer_to_str(ctx);
1171 if (!p)
1172 goto error;
1173 p = obj.type->print(p, obj.v);
1174 s = isl_printer_get_str(p);
1175 isl_printer_free(p);
1177 str = isl_str_from_string(ctx, s);
1178 if (!str)
1179 goto error;
1180 free_obj(obj);
1181 obj.v = str;
1182 obj.type = isl_obj_str;
1183 return obj;
1186 error:
1187 free_obj(obj);
1188 obj.type = isl_obj_none;
1189 obj.v = NULL;
1190 return obj;
1193 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1194 struct isl_obj lhs)
1196 int i;
1197 struct isl_token *tok;
1199 tok = isl_stream_next_token(s);
1200 if (!tok)
1201 return NULL;
1203 for (i = 0; ; ++i) {
1204 if (!bin_ops[i].op)
1205 break;
1206 if (bin_ops[i].op != tok->type)
1207 continue;
1208 if (!is_subtype(lhs, bin_ops[i].lhs))
1209 continue;
1211 isl_token_free(tok);
1212 return &bin_ops[i];
1215 for (i = 0; ; ++i) {
1216 if (!named_bin_ops[i].name)
1217 break;
1218 if (named_bin_ops[i].op.op != tok->type)
1219 continue;
1220 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1221 continue;
1223 isl_token_free(tok);
1224 return &named_bin_ops[i].op;
1227 isl_stream_push_token(s, tok);
1229 return NULL;
1232 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1234 int i;
1235 struct isl_token *tok;
1237 tok = isl_stream_next_token(s);
1238 if (!tok)
1239 return NULL;
1241 for (i = 0; ; ++i) {
1242 if (!named_un_ops[i].name)
1243 break;
1244 if (named_un_ops[i].op.op != tok->type)
1245 continue;
1247 isl_token_free(tok);
1248 return &named_un_ops[i].op;
1251 isl_stream_push_token(s, tok);
1253 return NULL;
1256 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1257 struct isl_obj arg)
1259 int i;
1261 for (i = 0; ; ++i) {
1262 if (!named_un_ops[i].name)
1263 break;
1264 if (named_un_ops[i].op.op != like->op)
1265 continue;
1266 if (!is_subtype(arg, named_un_ops[i].op.arg))
1267 continue;
1269 return &named_un_ops[i].op;
1272 return NULL;
1275 static int is_assign(struct isl_stream *s)
1277 struct isl_token *tok;
1278 struct isl_token *tok2;
1279 int assign;
1281 tok = isl_stream_next_token(s);
1282 if (!tok)
1283 return 0;
1284 if (tok->type != ISL_TOKEN_IDENT) {
1285 isl_stream_push_token(s, tok);
1286 return 0;
1289 tok2 = isl_stream_next_token(s);
1290 if (!tok2) {
1291 isl_stream_push_token(s, tok);
1292 return 0;
1294 assign = tok2->type == ISL_TOKEN_DEF;
1295 isl_stream_push_token(s, tok2);
1296 isl_stream_push_token(s, tok);
1298 return assign;
1301 static struct isl_obj read_obj(struct isl_stream *s,
1302 struct isl_hash_table *table);
1303 static struct isl_obj read_expr(struct isl_stream *s,
1304 struct isl_hash_table *table);
1306 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1307 struct isl_hash_table *table, struct isc_un_op *op)
1309 struct isl_obj obj = { isl_obj_none, NULL };
1311 obj = read_obj(s, table);
1312 if (!obj.v)
1313 goto error;
1315 op = find_matching_un_op(op, obj);
1317 if (!op)
1318 isl_die(s->ctx, isl_error_invalid,
1319 "no such unary operator defined on given operand",
1320 goto error);
1322 obj = convert(s->ctx, obj, op->arg);
1323 obj.v = op->fn(obj.v);
1324 obj.type = op->res;
1326 return obj;
1327 error:
1328 free_obj(obj);
1329 obj.type = isl_obj_none;
1330 obj.v = NULL;
1331 return obj;
1334 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1336 struct isl_list *list;
1337 int exact;
1339 if (obj.type != isl_obj_union_map)
1340 obj = convert(ctx, obj, isl_obj_union_map);
1341 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1342 list = isl_list_alloc(ctx, 2);
1343 if (!list)
1344 goto error;
1346 list->obj[0].type = isl_obj_union_map;
1347 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1348 list->obj[1].type = isl_obj_bool;
1349 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1350 obj.v = list;
1351 obj.type = isl_obj_list;
1352 if (exact < 0 || !list->obj[0].v)
1353 goto error;
1355 return obj;
1356 error:
1357 free_obj(obj);
1358 obj.type = isl_obj_none;
1359 obj.v = NULL;
1360 return obj;
1363 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1365 struct isl_list *list = obj.v;
1366 struct isl_token *tok;
1367 int i;
1369 tok = isl_stream_next_token(s);
1370 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1371 isl_stream_error(s, tok, "expecting index");
1372 if (tok)
1373 isl_stream_push_token(s, tok);
1374 goto error;
1376 i = isl_int_get_si(tok->u.v);
1377 isl_token_free(tok);
1378 isl_assert(s->ctx, i < list->n, goto error);
1379 if (isl_stream_eat(s, ']'))
1380 goto error;
1382 return obj_at(obj, i);
1383 error:
1384 free_obj(obj);
1385 obj.type = isl_obj_none;
1386 obj.v = NULL;
1387 return obj;
1390 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1391 struct isl_hash_table *table)
1393 struct isl_obj obj;
1395 obj = read_expr(s, table);
1396 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1397 is_subtype(obj, isl_obj_union_map), goto error);
1399 if (obj.type == isl_obj_list) {
1400 struct isl_list *list = obj.v;
1401 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1402 obj = obj_at(obj, 0);
1404 if (obj.type == isl_obj_set)
1405 obj = convert(s->ctx, obj, isl_obj_union_set);
1406 else if (obj.type == isl_obj_map)
1407 obj = convert(s->ctx, obj, isl_obj_union_map);
1408 if (obj.type == isl_obj_union_set) {
1409 obj.v = isl_union_set_apply(obj.v, umap);
1410 } else
1411 obj.v = isl_union_map_apply_range(obj.v, umap);
1412 if (!obj.v)
1413 goto error2;
1415 if (isl_stream_eat(s, ')'))
1416 goto error2;
1418 return obj;
1419 error:
1420 isl_union_map_free(umap);
1421 error2:
1422 free_obj(obj);
1423 obj.type = isl_obj_none;
1424 obj.v = NULL;
1425 return obj;
1428 static struct isl_obj apply_fun_set(struct isl_obj obj,
1429 __isl_take isl_union_set *uset)
1431 if (obj.type == isl_obj_union_pw_qpolynomial) {
1432 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1433 } else {
1434 obj.type = isl_obj_list;
1435 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1437 return obj;
1440 static struct isl_obj apply_fun_map(struct isl_obj obj,
1441 __isl_take isl_union_map *umap)
1443 if (obj.type == isl_obj_union_pw_qpolynomial) {
1444 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1445 } else {
1446 obj.type = isl_obj_list;
1447 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1449 return obj;
1452 static struct isl_obj apply_fun(struct isl_stream *s,
1453 struct isl_obj obj, struct isl_hash_table *table)
1455 struct isl_obj arg;
1457 arg = read_expr(s, table);
1458 if (!is_subtype(arg, isl_obj_union_map) &&
1459 !is_subtype(arg, isl_obj_union_set))
1460 isl_die(s->ctx, isl_error_invalid,
1461 "expecting set of map argument", goto error);
1463 if (arg.type == isl_obj_list) {
1464 struct isl_list *list = arg.v;
1465 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1466 arg = obj_at(arg, 0);
1468 if (arg.type == isl_obj_set)
1469 arg = convert(s->ctx, arg, isl_obj_union_set);
1470 else if (arg.type == isl_obj_map)
1471 arg = convert(s->ctx, arg, isl_obj_union_map);
1472 if (arg.type == isl_obj_union_set)
1473 obj = apply_fun_set(obj, arg.v);
1474 else
1475 obj = apply_fun_map(obj, arg.v);
1476 if (!obj.v)
1477 goto error2;
1479 if (isl_stream_eat(s, ')'))
1480 goto error2;
1482 return obj;
1483 error:
1484 free_obj(arg);
1485 error2:
1486 free_obj(obj);
1487 obj.type = isl_obj_none;
1488 obj.v = NULL;
1489 return obj;
1492 struct add_vertex_data {
1493 struct isl_list *list;
1494 int i;
1497 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1499 struct add_vertex_data *data = (struct add_vertex_data *)user;
1500 isl_basic_set *expr;
1502 expr = isl_vertex_get_expr(vertex);
1504 data->list->obj[data->i].type = isl_obj_set;
1505 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1506 data->i++;
1508 isl_vertex_free(vertex);
1510 return 0;
1513 static int set_vertices(__isl_take isl_set *set, void *user)
1515 isl_ctx *ctx;
1516 isl_basic_set *hull;
1517 isl_vertices *vertices = NULL;
1518 struct isl_list *list = NULL;
1519 int r;
1520 struct add_vertex_data *data = (struct add_vertex_data *)user;
1522 set = isl_set_remove_divs(set);
1523 hull = isl_set_convex_hull(set);
1524 vertices = isl_basic_set_compute_vertices(hull);
1525 isl_basic_set_free(hull);
1527 list = data->list;
1529 ctx = isl_vertices_get_ctx(vertices);
1530 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1531 if (!data->list)
1532 goto error;
1534 data->i = 0;
1535 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1537 data->list = isl_list_concat(list, data->list);
1539 isl_vertices_free(vertices);
1541 return r;
1542 error:
1543 data->list = list;
1544 isl_vertices_free(vertices);
1545 return -1;
1548 static struct isl_obj vertices(struct isl_stream *s,
1549 struct isl_hash_table *table)
1551 isl_ctx *ctx;
1552 struct isl_obj obj;
1553 struct isl_list *list = NULL;
1554 isl_union_set *uset;
1555 struct add_vertex_data data = { NULL };
1557 obj = read_expr(s, table);
1558 obj = convert(s->ctx, obj, isl_obj_union_set);
1559 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1560 uset = obj.v;
1561 obj.v = NULL;
1563 ctx = isl_union_set_get_ctx(uset);
1564 list = isl_list_alloc(ctx, 0);
1565 if (!list)
1566 goto error;
1568 data.list = list;
1570 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1571 goto error;
1573 isl_union_set_free(uset);
1575 obj.type = isl_obj_list;
1576 obj.v = data.list;
1578 return obj;
1579 error:
1580 isl_union_set_free(uset);
1581 isl_list_free(data.list);
1582 free_obj(obj);
1583 obj.type = isl_obj_none;
1584 obj.v = NULL;
1585 return obj;
1588 static struct isl_obj type_of(struct isl_stream *s,
1589 struct isl_hash_table *table)
1591 isl_ctx *ctx;
1592 struct isl_obj obj;
1593 const char *type = "unknown";
1595 obj = read_expr(s, table);
1597 if (obj.type == isl_obj_map ||
1598 obj.type == isl_obj_union_map)
1599 type = "map";
1600 if (obj.type == isl_obj_set ||
1601 obj.type == isl_obj_union_set)
1602 type = "set";
1603 if (obj.type == isl_obj_pw_qpolynomial ||
1604 obj.type == isl_obj_union_pw_qpolynomial)
1605 type = "piecewise quasipolynomial";
1606 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1607 obj.type == isl_obj_union_pw_qpolynomial_fold)
1608 type = "piecewise quasipolynomial fold";
1609 if (obj.type == isl_obj_list)
1610 type = "list";
1611 if (obj.type == isl_obj_bool)
1612 type = "boolean";
1613 if (obj.type == isl_obj_str)
1614 type = "string";
1615 if (obj.type == isl_obj_int)
1616 type = "int";
1618 free_obj(obj);
1619 obj.type = isl_obj_str;
1620 obj.v = isl_str_from_string(s->ctx, strdup(type));
1622 return obj;
1625 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1626 struct isl_hash_table *table)
1628 struct isl_obj obj;
1630 obj = read_obj(s, table);
1631 obj = convert(s->ctx, obj, isl_obj_union_set);
1632 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1633 return obj.v;
1634 error:
1635 free_obj(obj);
1636 return NULL;
1639 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1640 struct isl_hash_table *table)
1642 struct isl_obj obj;
1644 obj = read_obj(s, table);
1645 obj = convert(s->ctx, obj, isl_obj_union_map);
1646 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1647 return obj.v;
1648 error:
1649 free_obj(obj);
1650 return NULL;
1653 static struct isl_obj last_any(struct isl_stream *s,
1654 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1655 __isl_take isl_union_map *may_source)
1657 struct isl_obj obj = { isl_obj_none, NULL };
1658 isl_union_map *sink = NULL;
1659 isl_union_map *schedule = NULL;
1660 isl_union_map *may_dep;
1661 isl_union_map *must_dep;
1663 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1664 goto error;
1666 sink = read_map(s, table);
1667 if (!sink)
1668 goto error;
1670 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1671 goto error;
1673 schedule = read_map(s, table);
1674 if (!schedule)
1675 goto error;
1677 if (isl_union_map_compute_flow(sink, must_source, may_source,
1678 schedule, &must_dep, &may_dep,
1679 NULL, NULL) < 0)
1680 return obj;
1682 obj.type = isl_obj_union_map;
1683 obj.v = isl_union_map_union(must_dep, may_dep);
1685 return obj;
1686 error:
1687 isl_union_map_free(may_source);
1688 isl_union_map_free(must_source);
1689 isl_union_map_free(sink);
1690 isl_union_map_free(schedule);
1691 free_obj(obj);
1692 obj.type = isl_obj_none;
1693 obj.v = NULL;
1694 return obj;
1697 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1699 struct isl_obj obj = { isl_obj_none, NULL };
1700 isl_union_map *must_source = NULL;
1701 isl_union_map *may_source = NULL;
1702 isl_union_map *sink = NULL;
1703 isl_union_map *schedule = NULL;
1704 isl_union_map *may_dep;
1706 may_source = read_map(s, table);
1707 if (!may_source)
1708 goto error;
1710 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1711 must_source = read_map(s, table);
1712 if (!must_source)
1713 goto error;
1714 return last_any(s, table, must_source, may_source);
1717 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1718 goto error;
1720 sink = read_map(s, table);
1721 if (!sink)
1722 goto error;
1724 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1725 goto error;
1727 schedule = read_map(s, table);
1728 if (!schedule)
1729 goto error;
1731 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1732 if (isl_union_map_compute_flow(sink, must_source, may_source,
1733 schedule, NULL, &may_dep,
1734 NULL, NULL) < 0)
1735 return obj;
1737 obj.type = isl_obj_union_map;
1738 obj.v = may_dep;
1740 return obj;
1741 error:
1742 isl_union_map_free(may_source);
1743 isl_union_map_free(must_source);
1744 isl_union_map_free(sink);
1745 isl_union_map_free(schedule);
1746 free_obj(obj);
1747 obj.type = isl_obj_none;
1748 obj.v = NULL;
1749 return obj;
1752 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1754 struct isl_obj obj = { isl_obj_none, NULL };
1755 struct isl_list *list = NULL;
1756 isl_union_map *must_source = NULL;
1757 isl_union_map *may_source = NULL;
1758 isl_union_map *sink = NULL;
1759 isl_union_map *schedule = NULL;
1760 isl_union_map *must_dep;
1761 isl_union_map *must_no_source;
1763 must_source = read_map(s, table);
1764 if (!must_source)
1765 goto error;
1767 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1768 may_source = read_map(s, table);
1769 if (!may_source)
1770 goto error;
1771 return last_any(s, table, must_source, may_source);
1774 list = isl_list_alloc(s->ctx, 2);
1775 if (!list)
1776 goto error;
1778 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1779 goto error;
1781 sink = read_map(s, table);
1782 if (!sink)
1783 goto error;
1785 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1786 goto error;
1788 schedule = read_map(s, table);
1789 if (!schedule)
1790 goto error;
1792 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1793 if (isl_union_map_compute_flow(sink, must_source, may_source,
1794 schedule, &must_dep, NULL,
1795 &must_no_source, NULL) < 0) {
1796 isl_list_free(list);
1797 return obj;
1800 list->obj[0].type = isl_obj_union_map;
1801 list->obj[0].v = must_dep;
1802 list->obj[1].type = isl_obj_union_map;
1803 list->obj[1].v = must_no_source;
1805 obj.v = list;
1806 obj.type = isl_obj_list;
1808 return obj;
1809 error:
1810 isl_list_free(list);
1811 isl_union_map_free(may_source);
1812 isl_union_map_free(must_source);
1813 isl_union_map_free(sink);
1814 isl_union_map_free(schedule);
1815 free_obj(obj);
1816 obj.type = isl_obj_none;
1817 obj.v = NULL;
1818 return obj;
1821 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1822 struct isl_hash_table *table)
1824 isl_union_set *domain;
1825 isl_union_map *validity;
1826 isl_union_map *proximity;
1828 domain = read_set(s, table);
1829 if (!domain)
1830 return NULL;
1832 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1833 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1835 for (;;) {
1836 isl_union_map *umap;
1837 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1838 umap = read_map(s, table);
1839 validity = isl_union_map_union(validity, umap);
1840 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1841 umap = read_map(s, table);
1842 proximity = isl_union_map_union(proximity, umap);
1843 } else
1844 break;
1847 return isl_union_set_compute_schedule(domain, validity, proximity);
1850 static struct isl_obj schedule(struct isl_stream *s,
1851 struct isl_hash_table *table)
1853 struct isl_obj obj = { isl_obj_none, NULL };
1854 isl_schedule *schedule;
1856 schedule = get_schedule(s, table);
1858 obj.v = isl_schedule_get_map(schedule);
1859 obj.type = isl_obj_union_map;
1861 isl_schedule_free(schedule);
1863 return obj;
1866 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1868 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1870 struct isl_obj obj = { isl_obj_none, NULL };
1871 isl_ctx *ctx = isl_band_get_ctx(band);
1872 struct isl_list *list;
1874 list = isl_list_alloc(ctx, 2);
1875 if (!list)
1876 goto error;
1878 obj.v = list;
1879 obj.type = isl_obj_list;
1881 list->obj[0].type = isl_obj_union_map;
1882 list->obj[0].v = isl_band_get_partial_schedule(band);
1884 if (isl_band_has_children(band)) {
1885 isl_band_list *children;
1887 children = isl_band_get_children(band);
1888 list->obj[1] = band_list_to_obj_list(children);
1889 } else {
1890 list->obj[1].type = isl_obj_list;
1891 list->obj[1].v = isl_list_alloc(ctx, 0);
1894 if (!list->obj[0].v || !list->obj[1].v)
1895 goto error;
1897 isl_band_free(band);
1899 return obj;
1900 error:
1901 isl_band_free(band);
1902 free_obj(obj);
1903 obj.type = isl_obj_none;
1904 obj.v = NULL;
1905 return obj;
1908 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1910 struct isl_obj obj = { isl_obj_none, NULL };
1911 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1912 struct isl_list *list;
1913 int i, n;
1915 n = isl_band_list_n_band(bands);
1916 list = isl_list_alloc(ctx, n);
1917 if (!list)
1918 goto error;
1920 obj.v = list;
1921 obj.type = isl_obj_list;
1923 for (i = 0; i < n; ++i) {
1924 isl_band *band;
1926 band = isl_band_list_get_band(bands, i);
1927 list->obj[i] = band_to_obj_list(band);
1928 if (!list->obj[i].v)
1929 goto error;
1932 isl_band_list_free(bands);
1934 return obj;
1935 error:
1936 isl_band_list_free(bands);
1937 free_obj(obj);
1938 obj.type = isl_obj_none;
1939 obj.v = NULL;
1940 return obj;
1943 static struct isl_obj schedule_forest(struct isl_stream *s,
1944 struct isl_hash_table *table)
1946 struct isl_obj obj = { isl_obj_none, NULL };
1947 isl_schedule *schedule;
1948 isl_band_list *roots;
1950 schedule = get_schedule(s, table);
1951 if (!schedule)
1952 return obj;
1954 roots = isl_schedule_get_band_forest(schedule);
1955 isl_schedule_free(schedule);
1957 return band_list_to_obj_list(roots);
1960 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1962 struct isl_token *tok;
1964 if (isl_stream_eat_if_available(s, '+'))
1965 return transitive_closure(s->ctx, obj);
1967 tok = isl_stream_next_token(s);
1968 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1969 isl_stream_error(s, tok, "expecting -1");
1970 if (tok)
1971 isl_stream_push_token(s, tok);
1972 goto error;
1974 isl_token_free(tok);
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 obj.v = isl_union_map_reverse(obj.v);
1980 if (!obj.v)
1981 goto error;
1983 return obj;
1984 error:
1985 free_obj(obj);
1986 obj.type = isl_obj_none;
1987 obj.v = NULL;
1988 return obj;
1991 static struct isl_obj check_assert(struct isl_stream *s,
1992 struct isl_hash_table *table)
1994 struct isl_obj obj;
1996 obj = read_expr(s, table);
1997 if (obj.type != isl_obj_bool)
1998 isl_die(s->ctx, isl_error_invalid,
1999 "expecting boolean expression", goto error);
2000 if (obj.v != &isl_bool_true)
2001 isl_die(s->ctx, isl_error_unknown,
2002 "assertion failed", abort());
2003 error:
2004 free_obj(obj);
2005 obj.type = isl_obj_none;
2006 obj.v = NULL;
2007 return obj;
2010 static struct isl_obj read_from_file(struct isl_stream *s)
2012 struct isl_obj obj;
2013 struct isl_token *tok;
2014 struct isl_stream *s_file;
2015 struct iscc_options *options;
2016 FILE *file;
2018 tok = isl_stream_next_token(s);
2019 if (!tok || tok->type != ISL_TOKEN_STRING) {
2020 isl_stream_error(s, tok, "expecting filename");
2021 isl_token_free(tok);
2022 goto error;
2025 options = isl_ctx_peek_iscc_options(s->ctx);
2026 if (!options || !options->io) {
2027 isl_token_free(tok);
2028 isl_die(s->ctx, isl_error_invalid,
2029 "read operation not allowed", goto error);
2032 file = fopen(tok->u.s, "r");
2033 isl_token_free(tok);
2034 isl_assert(s->ctx, file, goto error);
2036 s_file = isl_stream_new_file(s->ctx, file);
2037 if (!s_file) {
2038 fclose(file);
2039 goto error;
2042 obj = isl_stream_read_obj(s_file);
2044 isl_stream_free(s_file);
2045 fclose(file);
2047 return obj;
2048 error:
2049 obj.type = isl_obj_none;
2050 obj.v = NULL;
2051 return obj;
2054 static struct isl_obj write_to_file(struct isl_stream *s,
2055 struct isl_hash_table *table)
2057 struct isl_obj obj;
2058 struct isl_token *tok;
2059 struct isl_stream *s_file;
2060 struct iscc_options *options;
2061 FILE *file;
2062 isl_printer *p;
2064 tok = isl_stream_next_token(s);
2065 if (!tok || tok->type != ISL_TOKEN_STRING) {
2066 isl_stream_error(s, tok, "expecting filename");
2067 isl_token_free(tok);
2068 goto error;
2071 obj = read_expr(s, table);
2073 options = isl_ctx_peek_iscc_options(s->ctx);
2074 if (!options || !options->io) {
2075 isl_token_free(tok);
2076 isl_die(s->ctx, isl_error_invalid,
2077 "write operation not allowed", goto error);
2080 file = fopen(tok->u.s, "w");
2081 isl_token_free(tok);
2082 if (!file)
2083 isl_die(s->ctx, isl_error_unknown,
2084 "could not open file for writing", goto error);
2086 p = isl_printer_to_file(s->ctx, file);
2087 p = isl_printer_set_output_format(p, options->format);
2088 p = obj.type->print(p, obj.v);
2089 p = isl_printer_end_line(p);
2090 isl_printer_free(p);
2092 fclose(file);
2093 error:
2094 free_obj(obj);
2095 obj.type = isl_obj_none;
2096 obj.v = NULL;
2097 return obj;
2100 static struct isl_obj read_string_if_available(struct isl_stream *s)
2102 struct isl_token *tok;
2103 struct isl_obj obj = { isl_obj_none, NULL };
2105 tok = isl_stream_next_token(s);
2106 if (!tok)
2107 return obj;
2108 if (tok->type == ISL_TOKEN_STRING) {
2109 isl_str *str;
2110 str = isl_str_alloc(s->ctx);
2111 if (!str)
2112 goto error;
2113 str->s = strdup(tok->u.s);
2114 isl_token_free(tok);
2115 obj.v = str;
2116 obj.type = isl_obj_str;
2117 } else
2118 isl_stream_push_token(s, tok);
2119 return obj;
2120 error:
2121 isl_token_free(tok);
2122 return obj;
2125 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2127 struct isl_token *tok;
2128 struct isl_obj obj = { isl_obj_none, NULL };
2130 tok = isl_stream_next_token(s);
2131 if (!tok)
2132 return obj;
2133 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2134 int is_true = tok->type == ISL_TOKEN_TRUE;
2135 isl_token_free(tok);
2136 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2137 obj.type = isl_obj_bool;
2138 } else
2139 isl_stream_push_token(s, tok);
2140 return obj;
2141 error:
2142 isl_token_free(tok);
2143 return obj;
2146 static __isl_give char *read_ident(struct isl_stream *s)
2148 char *name;
2149 struct isl_token *tok, *tok2;
2151 name = isl_stream_read_ident_if_available(s);
2152 if (name)
2153 return name;
2155 tok = isl_stream_next_token(s);
2156 if (!tok)
2157 return NULL;
2158 if (tok->type != '$') {
2159 isl_stream_push_token(s, tok);
2160 return NULL;
2162 tok2 = isl_stream_next_token(s);
2163 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2164 if (tok2)
2165 isl_stream_push_token(s, tok2);
2166 isl_stream_push_token(s, tok);
2167 return NULL;
2170 name = isl_int_get_str(tok2->u.v);
2171 isl_token_free(tok);
2172 isl_token_free(tok2);
2174 return name;
2177 static struct isl_obj read_list(struct isl_stream *s,
2178 struct isl_hash_table *table, struct isl_obj obj)
2180 struct isl_list *list;
2182 list = isl_list_alloc(s->ctx, 2);
2183 if (!list)
2184 goto error;
2185 list->obj[0] = obj;
2186 list->obj[1] = read_obj(s, table);
2187 obj.v = list;
2188 obj.type = isl_obj_list;
2190 if (!list->obj[1].v)
2191 goto error;
2193 while (isl_stream_eat_if_available(s, ',')) {
2194 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2195 if (!obj.v)
2196 goto error;
2199 return obj;
2200 error:
2201 free_obj(obj);
2202 obj.type = isl_obj_none;
2203 obj.v = NULL;
2204 return obj;
2207 static struct isl_obj read_obj(struct isl_stream *s,
2208 struct isl_hash_table *table)
2210 struct isl_obj obj = { isl_obj_none, NULL };
2211 char *name = NULL;
2212 struct isc_un_op *op = NULL;
2214 obj = read_string_if_available(s);
2215 if (obj.v)
2216 return obj;
2217 obj = read_bool_if_available(s);
2218 if (obj.v)
2219 return obj;
2220 if (isl_stream_eat_if_available(s, '(')) {
2221 if (isl_stream_next_token_is(s, ')')) {
2222 obj.type = isl_obj_list;
2223 obj.v = isl_list_alloc(s->ctx, 0);
2224 } else {
2225 obj = read_expr(s, table);
2226 if (obj.v && isl_stream_eat_if_available(s, ','))
2227 obj = read_list(s, table, obj);
2229 if (!obj.v || isl_stream_eat(s, ')'))
2230 goto error;
2231 } else {
2232 op = read_prefix_un_op_if_available(s);
2233 if (op)
2234 return read_un_op_expr(s, table, op);
2236 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2237 return check_assert(s, table);
2238 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2239 return read_from_file(s);
2240 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2241 return write_to_file(s, table);
2242 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2243 return vertices(s, table);
2244 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2245 return any(s, table);
2246 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2247 return last(s, table);
2248 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2249 return schedule(s, table);
2250 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2251 return schedule_forest(s, table);
2252 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2253 return type_of(s, table);
2255 name = read_ident(s);
2256 if (name)
2257 obj = stored_obj(s->ctx, table, name);
2258 else
2259 obj = isl_stream_read_obj(s);
2260 if (!obj.v)
2261 goto error;
2264 if (isl_stream_eat_if_available(s, '^'))
2265 obj = power(s, obj);
2266 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2267 obj = obj_at_index(s, obj);
2268 else if (is_subtype(obj, isl_obj_union_map) &&
2269 isl_stream_eat_if_available(s, '(')) {
2270 obj = convert(s->ctx, obj, isl_obj_union_map);
2271 obj = apply(s, obj.v, table);
2272 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2273 isl_stream_eat_if_available(s, '(')) {
2274 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2275 obj = apply_fun(s, obj, table);
2276 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2277 isl_stream_eat_if_available(s, '(')) {
2278 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2279 obj = apply_fun(s, obj, table);
2282 return obj;
2283 error:
2284 free_obj(obj);
2285 obj.type = isl_obj_none;
2286 obj.v = NULL;
2287 return obj;
2290 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2291 struct isl_obj lhs, struct isl_obj rhs)
2293 int i;
2295 for (i = 0; ; ++i) {
2296 if (!bin_ops[i].op)
2297 break;
2298 if (bin_ops[i].op != like->op)
2299 continue;
2300 if (!is_subtype(lhs, bin_ops[i].lhs))
2301 continue;
2302 if (!is_subtype(rhs, bin_ops[i].rhs))
2303 continue;
2305 return &bin_ops[i];
2308 for (i = 0; ; ++i) {
2309 if (!named_bin_ops[i].name)
2310 break;
2311 if (named_bin_ops[i].op.op != like->op)
2312 continue;
2313 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2314 continue;
2315 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2316 continue;
2318 return &named_bin_ops[i].op;
2321 return NULL;
2324 static int next_is_neg_int(struct isl_stream *s)
2326 struct isl_token *tok;
2327 int ret;
2329 tok = isl_stream_next_token(s);
2330 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2331 isl_stream_push_token(s, tok);
2333 return ret;
2336 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2337 struct isl_obj lhs, struct isl_obj rhs)
2339 struct isl_obj obj;
2341 lhs = convert(ctx, lhs, op->lhs);
2342 rhs = convert(ctx, rhs, op->rhs);
2343 if (op->res != isl_obj_bool)
2344 obj.v = op->o.fn(lhs.v, rhs.v);
2345 else {
2346 int res = op->o.test(lhs.v, rhs.v);
2347 free_obj(lhs);
2348 free_obj(rhs);
2349 obj.v = isl_bool_from_int(res);
2351 obj.type = op->res;
2353 return obj;
2356 static struct isl_obj read_expr(struct isl_stream *s,
2357 struct isl_hash_table *table)
2359 struct isl_obj obj = { isl_obj_none, NULL };
2360 struct isl_obj right_obj = { isl_obj_none, NULL };
2362 obj = read_obj(s, table);
2363 for (; obj.v;) {
2364 struct isc_bin_op *op = NULL;
2366 op = read_bin_op_if_available(s, obj);
2367 if (!op)
2368 break;
2370 right_obj = read_obj(s, table);
2372 op = find_matching_bin_op(op, obj, right_obj);
2374 if (!op)
2375 isl_die(s->ctx, isl_error_invalid,
2376 "no such binary operator defined on given operands",
2377 goto error);
2379 obj = call_bin_op(s->ctx, op, obj, right_obj);
2382 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2383 right_obj = read_obj(s, table);
2384 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2387 return obj;
2388 error:
2389 free_obj(right_obj);
2390 free_obj(obj);
2391 obj.type = isl_obj_none;
2392 obj.v = NULL;
2393 return obj;
2396 static __isl_give isl_printer *source_file(struct isl_stream *s,
2397 struct isl_hash_table *table, __isl_take isl_printer *p);
2399 static __isl_give isl_printer *read_line(struct isl_stream *s,
2400 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2402 struct isl_obj obj = { isl_obj_none, NULL };
2403 char *lhs = NULL;
2404 int assign = 0;
2405 int only_print = 0;
2406 struct isc_bin_op *op = NULL;
2407 char buf[30];
2409 if (!p)
2410 return NULL;
2411 if (isl_stream_is_empty(s))
2412 return p;
2414 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2415 return source_file(s, table, p);
2417 assign = is_assign(s);
2418 if (assign) {
2419 lhs = isl_stream_read_ident_if_available(s);
2420 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2421 goto error;
2422 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2423 only_print = 1;
2424 else if (!tty)
2425 only_print = 1;
2427 obj = read_expr(s, table);
2428 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2429 fprintf(stderr, "Interrupted\n");
2430 isl_ctx_reset_error(s->ctx);
2432 if (isl_stream_eat(s, ';'))
2433 goto error;
2435 if (only_print) {
2436 if (obj.type != isl_obj_none && obj.v != NULL) {
2437 p = obj.type->print(p, obj.v);
2438 p = isl_printer_end_line(p);
2440 free_obj(obj);
2441 return p;
2443 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2444 static int count = 0;
2445 snprintf(buf, sizeof(buf), "$%d", count++);
2446 lhs = strdup(buf + 1);
2448 p = isl_printer_print_str(p, buf);
2449 p = isl_printer_print_str(p, " := ");
2450 p = obj.type->print(p, obj.v);
2451 p = isl_printer_end_line(p);
2453 if (lhs && do_assign(s->ctx, table, lhs, obj))
2454 return p;
2456 return p;
2457 error:
2458 isl_stream_flush_tokens(s);
2459 isl_stream_skip_line(s);
2460 free(lhs);
2461 free_obj(obj);
2462 return p;
2465 int free_cb(void **entry, void *user)
2467 struct isl_named_obj *named = *entry;
2469 free_obj(named->obj);
2470 free(named->name);
2471 free(named);
2473 return 0;
2476 static void register_named_ops(struct isl_stream *s)
2478 int i;
2480 for (i = 0; i < ISCC_N_OP; ++i) {
2481 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2482 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2485 for (i = 0; ; ++i) {
2486 if (!named_un_ops[i].name)
2487 break;
2488 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2489 named_un_ops[i].name);
2490 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2493 for (i = 0; ; ++i) {
2494 if (!named_bin_ops[i].name)
2495 break;
2496 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2497 named_bin_ops[i].name);
2498 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2502 static __isl_give isl_printer *source_file(struct isl_stream *s,
2503 struct isl_hash_table *table, __isl_take isl_printer *p)
2505 struct isl_token *tok;
2506 struct isl_stream *s_file;
2507 FILE *file;
2509 tok = isl_stream_next_token(s);
2510 if (!tok || tok->type != ISL_TOKEN_STRING) {
2511 isl_stream_error(s, tok, "expecting filename");
2512 isl_token_free(tok);
2513 return p;
2516 file = fopen(tok->u.s, "r");
2517 isl_token_free(tok);
2518 isl_assert(s->ctx, file, return p);
2520 s_file = isl_stream_new_file(s->ctx, file);
2521 if (!s_file) {
2522 fclose(file);
2523 return p;
2526 register_named_ops(s_file);
2528 while (!s_file->eof)
2529 p = read_line(s_file, table, p, 0);
2531 isl_stream_free(s_file);
2532 fclose(file);
2534 isl_stream_eat(s, ';');
2536 return p;
2539 int main(int argc, char **argv)
2541 struct isl_ctx *ctx;
2542 struct isl_stream *s;
2543 struct isl_hash_table *table;
2544 struct iscc_options *options;
2545 isl_printer *p;
2546 int tty = isatty(0);
2548 options = iscc_options_new_with_defaults();
2549 assert(options);
2550 argc = iscc_options_parse(options, argc, argv, ISL_ARG_ALL);
2552 ctx = isl_ctx_alloc_with_options(iscc_options_arg, options);
2553 s = isl_stream_new_file(ctx, stdin);
2554 assert(s);
2555 table = isl_hash_table_alloc(ctx, 10);
2556 assert(table);
2557 p = isl_printer_to_file(ctx, stdout);
2558 p = isl_printer_set_output_format(p, options->format);
2559 assert(p);
2561 register_named_ops(s);
2563 install_signal_handler(ctx);
2565 while (p && !s->eof) {
2566 isl_ctx_resume(ctx);
2567 p = read_line(s, table, p, tty);
2570 remove_signal_handler(ctx);
2572 isl_printer_free(p);
2573 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2574 isl_hash_table_free(ctx, table);
2575 isl_stream_free(s);
2576 isl_ctx_free(ctx);
2578 return 0;