add isl.py to distribution
[barvinok.git] / iscc.c
blob0b0149afb654d16709941196508524d408ccb619
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 #else
70 struct pet_options;
71 int pet_options_set_autodetect(isl_ctx *ctx, int val)
73 return -1;
75 #endif
77 static int isl_bool_false = 0;
78 static int isl_bool_true = 1;
79 static int isl_bool_error = -1;
81 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
82 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
83 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
84 ISCC_MINIMIZING, ISCC_RESPECTING,
85 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
86 ISCC_N_OP };
87 static const char *op_name[ISCC_N_OP] = {
88 [ISCC_ASSERT] = "assert",
89 [ISCC_READ] = "read",
90 [ISCC_WRITE] = "write",
91 [ISCC_PRINT] = "print",
92 [ISCC_SOURCE] = "source",
93 [ISCC_VERTICES] = "vertices",
94 [ISCC_LAST] = "last",
95 [ISCC_ANY] = "any",
96 [ISCC_BEFORE] = "before",
97 [ISCC_UNDER] = "under",
98 [ISCC_SCHEDULE] = "schedule",
99 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
100 [ISCC_MINIMIZING] = "minimizing",
101 [ISCC_RESPECTING] = "respecting",
102 [ISCC_TYPEOF] = "typeof"
104 static enum isl_token_type iscc_op[ISCC_N_OP];
106 struct isl_arg_choice iscc_format[] = {
107 {"isl", ISL_FORMAT_ISL},
108 {"omega", ISL_FORMAT_OMEGA},
109 {"polylib", ISL_FORMAT_POLYLIB},
110 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
111 {"latex", ISL_FORMAT_LATEX},
112 {"C", ISL_FORMAT_C},
116 struct iscc_options {
117 struct barvinok_options *barvinok;
118 struct pet_options *pet;
119 unsigned format;
120 int io;
123 ISL_ARGS_START(struct iscc_options, iscc_options_args)
124 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
125 "barvinok options")
126 #ifdef HAVE_PET
127 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
128 #endif
129 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
130 iscc_format, ISL_FORMAT_ISL, "output format")
131 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
132 "allow read and write operations")
133 ISL_ARGS_END
135 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
136 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
138 static void *isl_obj_bool_copy(void *v)
140 return v;
143 static void isl_obj_bool_free(void *v)
147 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
148 void *v)
150 if (v == &isl_bool_true)
151 return isl_printer_print_str(p, "True");
152 else if (v == &isl_bool_false)
153 return isl_printer_print_str(p, "False");
154 else
155 return isl_printer_print_str(p, "Error");
158 static void *isl_obj_bool_add(void *v1, void *v2)
160 return v1;
163 struct isl_obj_vtable isl_obj_bool_vtable = {
164 isl_obj_bool_copy,
165 isl_obj_bool_add,
166 isl_obj_bool_print,
167 isl_obj_bool_free
169 #define isl_obj_bool (&isl_obj_bool_vtable)
171 int *isl_bool_from_int(int res)
173 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
176 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
177 __isl_take isl_union_map *map2)
179 return isl_union_map_is_subset(map2, map1);
181 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
182 __isl_take isl_union_set *set2)
184 return isl_union_set_is_subset(set2, set1);
187 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
188 __isl_take isl_union_map *map2)
190 return isl_union_map_is_strict_subset(map2, map1);
192 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
193 __isl_take isl_union_set *set2)
195 return isl_union_set_is_strict_subset(set2, set1);
198 extern struct isl_obj_vtable isl_obj_list_vtable;
199 #define isl_obj_list (&isl_obj_list_vtable)
201 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
202 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
203 struct isc_bin_op {
204 enum isl_token_type op;
205 isl_obj_type lhs;
206 isl_obj_type rhs;
207 isl_obj_type res;
208 union {
209 isc_bin_op_fn fn;
210 isc_bin_test_fn test;
211 } o;
213 struct isc_named_bin_op {
214 char *name;
215 struct isc_bin_op op;
218 struct iscc_at {
219 isl_union_pw_qpolynomial *upwqp;
220 isl_union_pw_qpolynomial *res;
223 static int eval_at(__isl_take isl_point *pnt, void *user)
225 struct iscc_at *at = (struct iscc_at *) user;
226 isl_qpolynomial *qp;
227 isl_set *set;
229 set = isl_set_from_point(isl_point_copy(pnt));
230 qp = isl_union_pw_qpolynomial_eval(
231 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
233 at->res = isl_union_pw_qpolynomial_add(at->res,
234 isl_union_pw_qpolynomial_from_pw_qpolynomial(
235 isl_pw_qpolynomial_alloc(set, qp)));
237 return 0;
240 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
241 __isl_take isl_union_pw_qpolynomial *upwqp,
242 __isl_take isl_union_set *uset)
244 struct iscc_at at;
246 at.upwqp = upwqp;
247 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
249 isl_union_set_foreach_point(uset, eval_at, &at);
251 isl_union_pw_qpolynomial_free(upwqp);
252 isl_union_set_free(uset);
254 return at.res;
257 struct iscc_fold_at {
258 isl_union_pw_qpolynomial_fold *upwf;
259 isl_union_pw_qpolynomial *res;
262 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
264 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
265 isl_qpolynomial *qp;
266 isl_set *set;
268 set = isl_set_from_point(isl_point_copy(pnt));
269 qp = isl_union_pw_qpolynomial_fold_eval(
270 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
272 at->res = isl_union_pw_qpolynomial_add(at->res,
273 isl_union_pw_qpolynomial_from_pw_qpolynomial(
274 isl_pw_qpolynomial_alloc(set, qp)));
276 return 0;
279 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
280 __isl_take isl_union_pw_qpolynomial_fold *upwf,
281 __isl_take isl_union_set *uset)
283 struct iscc_fold_at at;
285 at.upwf = upwf;
286 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
288 isl_union_set_foreach_point(uset, eval_fold_at, &at);
290 isl_union_pw_qpolynomial_fold_free(upwf);
291 isl_union_set_free(uset);
293 return at.res;
296 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
297 __isl_take isl_union_pw_qpolynomial *upwqp,
298 __isl_take isl_union_pw_qpolynomial_fold *upwf)
300 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
301 upwqp);
304 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
305 __isl_take isl_union_map *umap,
306 __isl_take isl_union_pw_qpolynomial_fold *upwf)
308 isl_ctx *ctx;
309 struct isl_list *list;
310 int tight;
312 ctx = isl_union_map_get_ctx(umap);
313 list = isl_list_alloc(ctx, 2);
314 if (!list)
315 goto error2;
317 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
318 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
319 upwf, &tight);
320 list->obj[1].type = isl_obj_bool;
321 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
322 if (tight < 0 || !list->obj[0].v)
323 goto error;
325 return list;
326 error2:
327 isl_union_map_free(umap);
328 isl_union_pw_qpolynomial_fold_free(upwf);
329 error:
330 isl_list_free(list);
331 return NULL;
334 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
335 __isl_take isl_union_set *uset,
336 __isl_take isl_union_pw_qpolynomial_fold *upwf)
338 isl_ctx *ctx;
339 struct isl_list *list;
340 int tight;
342 ctx = isl_union_set_get_ctx(uset);
343 list = isl_list_alloc(ctx, 2);
344 if (!list)
345 goto error2;
347 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
348 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
349 upwf, &tight);
350 list->obj[1].type = isl_obj_bool;
351 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
352 if (tight < 0 || !list->obj[0].v)
353 goto error;
355 return list;
356 error2:
357 isl_union_set_free(uset);
358 isl_union_pw_qpolynomial_fold_free(upwf);
359 error:
360 isl_list_free(list);
361 return NULL;
364 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_int_mul(
365 __isl_take isl_union_pw_qpolynomial *upwqp, __isl_take isl_int_obj *i)
367 isl_int v;
369 if (!i)
370 goto error;
372 isl_int_init(v);
373 isl_int_obj_get_int(i, &v);
374 upwqp = isl_union_pw_qpolynomial_mul_isl_int(upwqp, v);
375 isl_int_clear(v);
377 isl_int_obj_free(i);
379 return upwqp;
380 error:
381 isl_union_pw_qpolynomial_free(upwqp);
382 return NULL;
385 static __isl_give isl_union_pw_qpolynomial *int_union_pw_qpolynomial_mul(
386 __isl_take isl_int_obj *i, __isl_take isl_union_pw_qpolynomial *upwqp)
388 return union_pw_qpolynomial_int_mul(upwqp, i);
391 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_fold_int_mul(
392 __isl_take isl_union_pw_qpolynomial_fold *upwf,
393 __isl_take isl_int_obj *i)
395 isl_int v;
397 if (!i)
398 goto error;
400 isl_int_init(v);
401 isl_int_obj_get_int(i, &v);
402 upwf = isl_union_pw_qpolynomial_fold_mul_isl_int(upwf, v);
403 isl_int_clear(v);
405 isl_int_obj_free(i);
407 return upwf;
408 error:
409 isl_union_pw_qpolynomial_fold_free(upwf);
410 return NULL;
413 static __isl_give isl_union_pw_qpolynomial_fold *int_union_pw_qpolynomial_fold_mul(
414 __isl_take isl_int_obj *i,
415 __isl_take isl_union_pw_qpolynomial_fold *upwf)
417 return union_pw_qpolynomial_fold_int_mul(upwf, i);
420 struct isc_bin_op bin_ops[] = {
421 { '+', isl_obj_int, isl_obj_int, isl_obj_int,
422 (isc_bin_op_fn) &isl_int_obj_add },
423 { '-', isl_obj_int, isl_obj_int, isl_obj_int,
424 (isc_bin_op_fn) &isl_int_obj_sub },
425 { '*', isl_obj_int, isl_obj_int, isl_obj_int,
426 (isc_bin_op_fn) &isl_int_obj_mul },
427 { '+', isl_obj_union_set, isl_obj_union_set,
428 isl_obj_union_set,
429 (isc_bin_op_fn) &isl_union_set_union },
430 { '+', isl_obj_union_map, isl_obj_union_map,
431 isl_obj_union_map,
432 (isc_bin_op_fn) &isl_union_map_union },
433 { '-', isl_obj_union_set, isl_obj_union_set,
434 isl_obj_union_set,
435 (isc_bin_op_fn) &isl_union_set_subtract },
436 { '-', isl_obj_union_map, isl_obj_union_map,
437 isl_obj_union_map,
438 (isc_bin_op_fn) &isl_union_map_subtract },
439 { '*', isl_obj_union_set, isl_obj_union_set,
440 isl_obj_union_set,
441 (isc_bin_op_fn) &isl_union_set_intersect },
442 { '*', isl_obj_union_map, isl_obj_union_map,
443 isl_obj_union_map,
444 (isc_bin_op_fn) &isl_union_map_intersect },
445 { '*', isl_obj_union_map, isl_obj_union_set,
446 isl_obj_union_map,
447 (isc_bin_op_fn) &isl_union_map_intersect_domain },
448 { '.', isl_obj_union_map, isl_obj_union_map,
449 isl_obj_union_map,
450 (isc_bin_op_fn) &isl_union_map_apply_range },
451 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
452 isl_obj_union_pw_qpolynomial,
453 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
454 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
455 isl_obj_list,
456 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
457 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
458 isl_obj_union_map,
459 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
460 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
461 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
462 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
463 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
464 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
465 isl_obj_bool,
466 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
467 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
468 isl_obj_bool,
469 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
470 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
471 isl_obj_bool,
472 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
473 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
474 isl_obj_bool,
475 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
476 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
477 isl_obj_bool,
478 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
479 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
480 isl_obj_bool,
481 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
482 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
483 isl_obj_bool,
484 { .test =
485 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
486 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
487 isl_obj_bool,
488 { .test =
489 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
490 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
491 isl_obj_union_map,
492 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
493 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
494 isl_obj_union_map,
495 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
496 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
497 isl_obj_union_map,
498 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
499 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
500 isl_obj_union_map,
501 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
502 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
503 isl_obj_union_map,
504 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
505 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
506 isl_obj_union_map,
507 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
508 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
509 isl_obj_union_map,
510 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
511 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
512 isl_obj_union_map,
513 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
514 { '.', isl_obj_union_pw_qpolynomial_fold,
515 isl_obj_union_pw_qpolynomial_fold,
516 isl_obj_union_pw_qpolynomial_fold,
517 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
518 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
519 isl_obj_union_pw_qpolynomial,
520 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
521 { '+', isl_obj_union_pw_qpolynomial,
522 isl_obj_union_pw_qpolynomial_fold,
523 isl_obj_union_pw_qpolynomial_fold,
524 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
525 { '+', isl_obj_union_pw_qpolynomial_fold,
526 isl_obj_union_pw_qpolynomial,
527 isl_obj_union_pw_qpolynomial_fold,
528 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
529 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
530 isl_obj_union_pw_qpolynomial,
531 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
532 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial,
533 isl_obj_union_pw_qpolynomial,
534 (isc_bin_op_fn) &int_union_pw_qpolynomial_mul },
535 { '*', isl_obj_union_pw_qpolynomial, isl_obj_int,
536 isl_obj_union_pw_qpolynomial,
537 (isc_bin_op_fn) &union_pw_qpolynomial_int_mul },
538 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial_fold,
539 isl_obj_union_pw_qpolynomial_fold,
540 (isc_bin_op_fn) &int_union_pw_qpolynomial_fold_mul },
541 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_int,
542 isl_obj_union_pw_qpolynomial_fold,
543 (isc_bin_op_fn) &union_pw_qpolynomial_fold_int_mul },
544 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
545 isl_obj_union_pw_qpolynomial,
546 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
547 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
548 isl_obj_union_pw_qpolynomial,
549 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
550 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
551 isl_obj_union_pw_qpolynomial_fold,
552 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
553 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
554 isl_obj_union_pw_qpolynomial,
555 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
556 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
557 isl_obj_union_pw_qpolynomial,
558 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
559 { '%', isl_obj_union_set, isl_obj_union_set,
560 isl_obj_union_set,
561 (isc_bin_op_fn) &isl_union_set_gist },
562 { '%', isl_obj_union_map, isl_obj_union_map,
563 isl_obj_union_map,
564 (isc_bin_op_fn) &isl_union_map_gist },
565 { '%', isl_obj_union_map, isl_obj_union_set,
566 isl_obj_union_map,
567 (isc_bin_op_fn) &isl_union_map_gist_domain },
568 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
569 isl_obj_union_pw_qpolynomial,
570 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
571 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
572 isl_obj_union_pw_qpolynomial_fold,
573 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
574 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
575 isl_obj_union_pw_qpolynomial, isl_obj_bool,
576 { .test = (isc_bin_test_fn)
577 &isl_union_pw_qpolynomial_plain_is_equal } },
578 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
579 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
580 { .test = (isc_bin_test_fn)
581 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
582 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
583 (isc_bin_op_fn) &isl_str_concat },
587 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
588 __isl_take isl_union_map *umap2)
590 return isl_union_map_apply_range(umap2, umap1);
593 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
594 __isl_take isl_union_pw_qpolynomial *upwqp,
595 __isl_take isl_union_map *umap)
597 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
600 static __isl_give struct isl_list *qpolynomial_fold_after_map(
601 __isl_take isl_union_pw_qpolynomial_fold *upwf,
602 __isl_take isl_union_map *umap)
604 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
607 struct isc_named_bin_op named_bin_ops[] = {
608 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
609 isl_obj_union_map,
610 (isc_bin_op_fn) &map_after_map } },
611 { "after", { -1, isl_obj_union_pw_qpolynomial,
612 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
613 (isc_bin_op_fn) &qpolynomial_after_map } },
614 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
615 isl_obj_union_map, isl_obj_list,
616 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
617 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
618 isl_obj_union_map,
619 (isc_bin_op_fn) &isl_union_map_apply_range } },
620 { "before", { -1, isl_obj_union_map,
621 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
622 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
623 { "before", { -1, isl_obj_union_map,
624 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
625 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
626 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
627 isl_obj_union_set,
628 (isc_bin_op_fn) &isl_union_set_product } },
629 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
630 isl_obj_union_map,
631 (isc_bin_op_fn) &isl_union_map_product } },
632 NULL
635 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
637 return isl_set_from_basic_set(isl_union_set_sample(uset));
640 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
642 return isl_map_from_basic_map(isl_union_map_sample(umap));
645 static __isl_give struct isl_list *union_map_power(
646 __isl_take isl_union_map *umap)
648 isl_ctx *ctx;
649 struct isl_list *list;
650 int exact;
652 ctx = isl_union_map_get_ctx(umap);
653 list = isl_list_alloc(ctx, 2);
654 if (!list)
655 goto error2;
657 list->obj[0].type = isl_obj_union_map;
658 list->obj[0].v = isl_union_map_power(umap, &exact);
659 list->obj[1].type = isl_obj_bool;
660 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
661 if (exact < 0 || !list->obj[0].v)
662 goto error;
664 return list;
665 error2:
666 isl_union_map_free(umap);
667 error:
668 isl_list_free(list);
669 return NULL;
672 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
673 __isl_take isl_union_pw_qpolynomial *upwqp)
675 isl_ctx *ctx;
676 struct isl_list *list;
677 int tight;
679 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
680 list = isl_list_alloc(ctx, 2);
681 if (!list)
682 goto error2;
684 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
685 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
686 isl_fold_max, &tight);
687 list->obj[1].type = isl_obj_bool;
688 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
689 if (tight < 0 || !list->obj[0].v)
690 goto error;
692 return list;
693 error2:
694 isl_union_pw_qpolynomial_free(upwqp);
695 error:
696 isl_list_free(list);
697 return NULL;
700 #ifdef HAVE_CLOOG
701 void *map_codegen(void *arg)
703 isl_space *dim;
704 isl_union_map *umap = (isl_union_map *)arg;
705 isl_ctx *ctx = isl_union_map_get_ctx(umap);
706 CloogState *state;
707 CloogOptions *options;
708 CloogDomain *context;
709 CloogUnionDomain *ud;
710 CloogInput *input;
711 struct clast_stmt *stmt;
713 state = cloog_isl_state_malloc(ctx);
714 options = cloog_options_malloc(state);
715 options->language = CLOOG_LANGUAGE_C;
716 options->strides = 1;
717 options->sh = 1;
719 ud = cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap));
721 dim = isl_union_map_get_space(umap);
722 context = cloog_domain_from_isl_set(isl_set_universe(dim));
724 input = cloog_input_alloc(context, ud);
726 stmt = cloog_clast_create_from_input(input, options);
727 clast_pprint(stdout, stmt, 0, options);
728 cloog_clast_free(stmt);
730 error:
731 cloog_options_free(options);
732 cloog_state_free(state);
733 isl_union_map_free(umap);
734 return NULL;
737 void *set_codegen(void *arg)
739 isl_space *dim;
740 isl_union_set *uset = (isl_union_set *)arg;
741 isl_set *set;
742 isl_ctx *ctx = isl_union_set_get_ctx(uset);
743 CloogState *state;
744 CloogOptions *options;
745 CloogDomain *context;
746 CloogUnionDomain *ud;
747 CloogInput *input;
748 struct clast_stmt *stmt;
750 if (isl_union_set_n_set(uset) > 1)
751 isl_die(ctx, isl_error_invalid,
752 "code generation for more than one domain "
753 "requires a schedule", goto error);
755 state = cloog_isl_state_malloc(ctx);
756 options = cloog_options_malloc(state);
757 options->language = CLOOG_LANGUAGE_C;
758 options->strides = 1;
759 options->sh = 1;
761 set = isl_set_from_union_set(isl_union_set_copy(uset));
762 ud = cloog_union_domain_from_isl_set(set);
764 dim = isl_union_set_get_space(uset);
765 context = cloog_domain_from_isl_set(isl_set_universe(dim));
767 input = cloog_input_alloc(context, ud);
769 stmt = cloog_clast_create_from_input(input, options);
770 clast_pprint(stdout, stmt, 0, options);
771 cloog_clast_free(stmt);
773 cloog_options_free(options);
774 cloog_state_free(state);
775 error:
776 isl_union_set_free(uset);
777 return NULL;
779 #endif
781 #ifdef HAVE_PET
782 static __isl_give isl_list *parse(__isl_take isl_str *str)
784 isl_ctx *ctx;
785 struct isl_list *list;
786 struct pet_scop *scop;
787 isl_union_map *sched, *reads, *writes;
788 isl_union_set *domain;
789 struct iscc_options *options;
791 if (!str)
792 return NULL;
793 ctx = str->ctx;
795 options = isl_ctx_peek_iscc_options(ctx);
796 if (!options || !options->io) {
797 isl_str_free(str);
798 isl_die(ctx, isl_error_invalid,
799 "parse_file operation not allowed", return NULL);
802 list = isl_list_alloc(ctx, 4);
803 if (!list)
804 goto error;
806 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
807 domain = pet_scop_collect_domains(scop);
808 sched = pet_scop_collect_schedule(scop);
809 reads = pet_scop_collect_reads(scop);
810 writes = pet_scop_collect_writes(scop);
811 pet_scop_free(scop);
813 list->obj[0].type = isl_obj_union_set;
814 list->obj[0].v = domain;
815 list->obj[1].type = isl_obj_union_map;
816 list->obj[1].v = writes;
817 list->obj[2].type = isl_obj_union_map;
818 list->obj[2].v = reads;
819 list->obj[3].type = isl_obj_union_map;
820 list->obj[3].v = sched;
822 if (!list->obj[0].v || !list->obj[1].v ||
823 !list->obj[2].v || !list->obj[3].v)
824 goto error;
826 isl_str_free(str);
827 return list;
828 error:
829 isl_list_free(list);
830 isl_str_free(str);
831 return NULL;
833 #endif
835 static int add_point(__isl_take isl_point *pnt, void *user)
837 isl_union_set **scan = (isl_union_set **) user;
839 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
841 return 0;
844 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
846 isl_union_set *scan;
848 scan = isl_union_set_empty(isl_union_set_get_space(uset));
850 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
851 isl_union_set_free(scan);
852 return uset;
855 isl_union_set_free(uset);
856 return scan;
859 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
861 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
864 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
865 __isl_take isl_union_pw_qpolynomial *upwqp)
867 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
870 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
871 __isl_take isl_union_pw_qpolynomial *upwqp)
873 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
876 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
877 __isl_take isl_union_pw_qpolynomial *upwqp)
879 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
882 typedef void *(*isc_un_op_fn)(void *arg);
883 struct isc_un_op {
884 enum isl_token_type op;
885 isl_obj_type arg;
886 isl_obj_type res;
887 isc_un_op_fn fn;
889 struct isc_named_un_op {
890 char *name;
891 struct isc_un_op op;
893 struct isc_named_un_op named_un_ops[] = {
894 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
895 (isc_un_op_fn) &isl_union_map_affine_hull } },
896 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
897 (isc_un_op_fn) &isl_union_set_affine_hull } },
898 {"card", { -1, isl_obj_union_set,
899 isl_obj_union_pw_qpolynomial,
900 (isc_un_op_fn) &isl_union_set_card } },
901 {"card", { -1, isl_obj_union_map,
902 isl_obj_union_pw_qpolynomial,
903 (isc_un_op_fn) &isl_union_map_card } },
904 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
905 (isc_un_op_fn) &isl_union_set_coalesce } },
906 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
907 (isc_un_op_fn) &isl_union_map_coalesce } },
908 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
909 isl_obj_union_pw_qpolynomial,
910 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
911 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
912 isl_obj_union_pw_qpolynomial_fold,
913 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
914 #ifdef HAVE_CLOOG
915 {"codegen", { -1, isl_obj_union_set, isl_obj_none,
916 &set_codegen } },
917 {"codegen", { -1, isl_obj_union_map, isl_obj_none,
918 &map_codegen } },
919 #endif
920 {"coefficients", { -1, isl_obj_union_set,
921 isl_obj_union_set,
922 (isc_un_op_fn) &isl_union_set_coefficients } },
923 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
924 (isc_un_op_fn) &isl_union_set_solutions } },
925 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
926 (isc_un_op_fn) &isl_union_map_deltas } },
927 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
928 (isc_un_op_fn) &isl_union_map_deltas_map } },
929 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
930 (isc_un_op_fn) &isl_union_map_domain } },
931 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
932 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
933 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
934 isl_obj_union_set,
935 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
936 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
937 (isc_un_op_fn) &isl_union_map_domain } },
938 {"domain", { -1, isl_obj_union_pw_qpolynomial,
939 isl_obj_union_set,
940 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
941 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
942 isl_obj_union_set,
943 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
944 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
945 (isc_un_op_fn) &isl_union_map_domain_map } },
946 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
947 (isc_un_op_fn) &isl_union_map_range } },
948 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
949 (isc_un_op_fn) &isl_union_map_range } },
950 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
951 (isc_un_op_fn) &isl_union_map_range_map } },
952 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
953 (isc_un_op_fn) &isl_union_set_identity } },
954 {"lattice_width", { -1, isl_obj_union_set,
955 isl_obj_union_pw_qpolynomial,
956 (isc_un_op_fn) &isl_union_set_lattice_width } },
957 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
958 (isc_un_op_fn) &isl_union_map_lexmin } },
959 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
960 (isc_un_op_fn) &isl_union_map_lexmax } },
961 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
962 (isc_un_op_fn) &isl_union_set_lexmin } },
963 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
964 (isc_un_op_fn) &isl_union_set_lexmax } },
965 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
966 (isc_un_op_fn) &isl_union_set_lift } },
967 {"params", { -1, isl_obj_union_map, isl_obj_set,
968 (isc_un_op_fn) &isl_union_map_params } },
969 {"params", { -1, isl_obj_union_set, isl_obj_set,
970 (isc_un_op_fn) &isl_union_set_params } },
971 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
972 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
973 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
974 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
975 {"poly", { -1, isl_obj_union_pw_qpolynomial,
976 isl_obj_union_pw_qpolynomial,
977 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
978 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
979 isl_obj_union_pw_qpolynomial,
980 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
981 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
982 isl_obj_union_pw_qpolynomial,
983 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
984 #ifdef HAVE_PET
985 {"parse_file", { -1, isl_obj_str, isl_obj_list,
986 (isc_un_op_fn) &parse } },
987 #endif
988 {"pow", { -1, isl_obj_union_map, isl_obj_list,
989 (isc_un_op_fn) &union_map_power } },
990 {"sample", { -1, isl_obj_union_set, isl_obj_set,
991 (isc_un_op_fn) &union_set_sample } },
992 {"sample", { -1, isl_obj_union_map, isl_obj_map,
993 (isc_un_op_fn) &union_map_sample } },
994 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
995 (isc_un_op_fn) &union_set_scan } },
996 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
997 (isc_un_op_fn) &union_map_scan } },
998 {"sum", { -1, isl_obj_union_pw_qpolynomial,
999 isl_obj_union_pw_qpolynomial,
1000 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
1001 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
1002 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
1003 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
1004 (isc_un_op_fn) &isl_union_set_unwrap } },
1005 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
1006 (isc_un_op_fn) &isl_union_map_wrap } },
1007 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
1008 (isc_un_op_fn) &isl_union_map_zip } },
1009 NULL
1012 struct isl_named_obj {
1013 char *name;
1014 struct isl_obj obj;
1017 static void free_obj(struct isl_obj obj)
1019 obj.type->free(obj.v);
1022 static int same_name(const void *entry, const void *val)
1024 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1026 return !strcmp(named->name, val);
1029 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1030 char *name, struct isl_obj obj)
1032 struct isl_hash_table_entry *entry;
1033 uint32_t name_hash;
1034 struct isl_named_obj *named;
1036 name_hash = isl_hash_string(isl_hash_init(), name);
1037 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1038 if (!entry)
1039 goto error;
1040 if (entry->data) {
1041 named = entry->data;
1042 free_obj(named->obj);
1043 free(name);
1044 } else {
1045 named = isl_alloc_type(ctx, struct isl_named_obj);
1046 if (!named)
1047 goto error;
1048 named->name = name;
1049 entry->data = named;
1051 named->obj = obj;
1053 return 0;
1054 error:
1055 free_obj(obj);
1056 free(name);
1057 return -1;
1060 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1061 struct isl_hash_table *table, char *name)
1063 struct isl_obj obj = { isl_obj_none, NULL };
1064 struct isl_hash_table_entry *entry;
1065 uint32_t name_hash;
1067 name_hash = isl_hash_string(isl_hash_init(), name);
1068 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1069 if (entry) {
1070 struct isl_named_obj *named;
1071 named = entry->data;
1072 obj = named->obj;
1073 } else if (isdigit(name[0]))
1074 fprintf(stderr, "unknown identifier '$%s'\n", name);
1075 else
1076 fprintf(stderr, "unknown identifier '%s'\n", name);
1078 free(name);
1079 obj.v = obj.type->copy(obj.v);
1080 return obj;
1083 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1085 if (obj.type == super)
1086 return 1;
1087 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1088 return 1;
1089 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1090 return 1;
1091 if (obj.type == isl_obj_pw_qpolynomial &&
1092 super == isl_obj_union_pw_qpolynomial)
1093 return 1;
1094 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1095 super == isl_obj_union_pw_qpolynomial_fold)
1096 return 1;
1097 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1098 return 1;
1099 if (obj.type == isl_obj_list) {
1100 struct isl_list *list = obj.v;
1101 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1102 return is_subtype(list->obj[0], super);
1104 if (super == isl_obj_str)
1105 return 1;
1106 return 0;
1109 static struct isl_obj obj_at(struct isl_obj obj, int i)
1111 struct isl_list *list = obj.v;
1113 obj = list->obj[i];
1114 obj.v = obj.type->copy(obj.v);
1116 isl_list_free(list);
1118 return obj;
1121 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1122 isl_obj_type type)
1124 if (obj.type == type)
1125 return obj;
1126 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1127 obj.type = isl_obj_union_map;
1128 obj.v = isl_union_map_from_map(obj.v);
1129 return obj;
1131 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1132 obj.type = isl_obj_union_set;
1133 obj.v = isl_union_set_from_set(obj.v);
1134 return obj;
1136 if (obj.type == isl_obj_pw_qpolynomial &&
1137 type == isl_obj_union_pw_qpolynomial) {
1138 obj.type = isl_obj_union_pw_qpolynomial;
1139 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1140 return obj;
1142 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1143 type == isl_obj_union_pw_qpolynomial_fold) {
1144 obj.type = isl_obj_union_pw_qpolynomial_fold;
1145 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1146 return obj;
1148 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1149 if (type == isl_obj_union_map) {
1150 obj.type = isl_obj_union_map;
1151 return obj;
1153 if (type == isl_obj_union_pw_qpolynomial) {
1154 isl_space *dim = isl_union_set_get_space(obj.v);
1155 isl_union_set_free(obj.v);
1156 obj.v = isl_union_pw_qpolynomial_zero(dim);
1157 obj.type = isl_obj_union_pw_qpolynomial;
1158 return obj;
1160 if (type == isl_obj_union_pw_qpolynomial_fold) {
1161 isl_space *dim = isl_union_set_get_space(obj.v);
1162 isl_union_set_free(obj.v);
1163 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1164 isl_fold_list);
1165 obj.type = isl_obj_union_pw_qpolynomial_fold;
1166 return obj;
1169 if (obj.type == isl_obj_list) {
1170 struct isl_list *list = obj.v;
1171 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1172 return convert(ctx, obj_at(obj, 0), type);
1174 if (type == isl_obj_str) {
1175 isl_str *str;
1176 isl_printer *p;
1177 char *s;
1179 p = isl_printer_to_str(ctx);
1180 if (!p)
1181 goto error;
1182 p = obj.type->print(p, obj.v);
1183 s = isl_printer_get_str(p);
1184 isl_printer_free(p);
1186 str = isl_str_from_string(ctx, s);
1187 if (!str)
1188 goto error;
1189 free_obj(obj);
1190 obj.v = str;
1191 obj.type = isl_obj_str;
1192 return obj;
1195 error:
1196 free_obj(obj);
1197 obj.type = isl_obj_none;
1198 obj.v = NULL;
1199 return obj;
1202 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1203 struct isl_obj lhs)
1205 int i;
1206 struct isl_token *tok;
1208 tok = isl_stream_next_token(s);
1209 if (!tok)
1210 return NULL;
1212 for (i = 0; ; ++i) {
1213 if (!bin_ops[i].op)
1214 break;
1215 if (bin_ops[i].op != tok->type)
1216 continue;
1217 if (!is_subtype(lhs, bin_ops[i].lhs))
1218 continue;
1220 isl_token_free(tok);
1221 return &bin_ops[i];
1224 for (i = 0; ; ++i) {
1225 if (!named_bin_ops[i].name)
1226 break;
1227 if (named_bin_ops[i].op.op != tok->type)
1228 continue;
1229 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1230 continue;
1232 isl_token_free(tok);
1233 return &named_bin_ops[i].op;
1236 isl_stream_push_token(s, tok);
1238 return NULL;
1241 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1243 int i;
1244 struct isl_token *tok;
1246 tok = isl_stream_next_token(s);
1247 if (!tok)
1248 return NULL;
1250 for (i = 0; ; ++i) {
1251 if (!named_un_ops[i].name)
1252 break;
1253 if (named_un_ops[i].op.op != tok->type)
1254 continue;
1256 isl_token_free(tok);
1257 return &named_un_ops[i].op;
1260 isl_stream_push_token(s, tok);
1262 return NULL;
1265 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1266 struct isl_obj arg)
1268 int i;
1270 for (i = 0; ; ++i) {
1271 if (!named_un_ops[i].name)
1272 break;
1273 if (named_un_ops[i].op.op != like->op)
1274 continue;
1275 if (!is_subtype(arg, named_un_ops[i].op.arg))
1276 continue;
1278 return &named_un_ops[i].op;
1281 return NULL;
1284 static int is_assign(struct isl_stream *s)
1286 struct isl_token *tok;
1287 struct isl_token *tok2;
1288 int assign;
1290 tok = isl_stream_next_token(s);
1291 if (!tok)
1292 return 0;
1293 if (tok->type != ISL_TOKEN_IDENT) {
1294 isl_stream_push_token(s, tok);
1295 return 0;
1298 tok2 = isl_stream_next_token(s);
1299 if (!tok2) {
1300 isl_stream_push_token(s, tok);
1301 return 0;
1303 assign = tok2->type == ISL_TOKEN_DEF;
1304 isl_stream_push_token(s, tok2);
1305 isl_stream_push_token(s, tok);
1307 return assign;
1310 static struct isl_obj read_obj(struct isl_stream *s,
1311 struct isl_hash_table *table);
1312 static struct isl_obj read_expr(struct isl_stream *s,
1313 struct isl_hash_table *table);
1315 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1316 struct isl_hash_table *table, struct isc_un_op *op)
1318 struct isl_obj obj = { isl_obj_none, NULL };
1320 obj = read_obj(s, table);
1321 if (!obj.v)
1322 goto error;
1324 op = find_matching_un_op(op, obj);
1326 if (!op)
1327 isl_die(s->ctx, isl_error_invalid,
1328 "no such unary operator defined on given operand",
1329 goto error);
1331 obj = convert(s->ctx, obj, op->arg);
1332 obj.v = op->fn(obj.v);
1333 obj.type = op->res;
1335 return obj;
1336 error:
1337 free_obj(obj);
1338 obj.type = isl_obj_none;
1339 obj.v = NULL;
1340 return obj;
1343 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1345 struct isl_list *list;
1346 int exact;
1348 if (obj.type != isl_obj_union_map)
1349 obj = convert(ctx, obj, isl_obj_union_map);
1350 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1351 list = isl_list_alloc(ctx, 2);
1352 if (!list)
1353 goto error;
1355 list->obj[0].type = isl_obj_union_map;
1356 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1357 list->obj[1].type = isl_obj_bool;
1358 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1359 obj.v = list;
1360 obj.type = isl_obj_list;
1361 if (exact < 0 || !list->obj[0].v)
1362 goto error;
1364 return obj;
1365 error:
1366 free_obj(obj);
1367 obj.type = isl_obj_none;
1368 obj.v = NULL;
1369 return obj;
1372 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1374 struct isl_list *list = obj.v;
1375 struct isl_token *tok;
1376 int i;
1378 tok = isl_stream_next_token(s);
1379 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1380 isl_stream_error(s, tok, "expecting index");
1381 if (tok)
1382 isl_stream_push_token(s, tok);
1383 goto error;
1385 i = isl_int_get_si(tok->u.v);
1386 isl_token_free(tok);
1387 isl_assert(s->ctx, i < list->n, goto error);
1388 if (isl_stream_eat(s, ']'))
1389 goto error;
1391 return obj_at(obj, i);
1392 error:
1393 free_obj(obj);
1394 obj.type = isl_obj_none;
1395 obj.v = NULL;
1396 return obj;
1399 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1400 struct isl_hash_table *table)
1402 struct isl_obj obj;
1404 obj = read_expr(s, table);
1405 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1406 is_subtype(obj, isl_obj_union_map), goto error);
1408 if (obj.type == isl_obj_list) {
1409 struct isl_list *list = obj.v;
1410 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1411 obj = obj_at(obj, 0);
1413 if (obj.type == isl_obj_set)
1414 obj = convert(s->ctx, obj, isl_obj_union_set);
1415 else if (obj.type == isl_obj_map)
1416 obj = convert(s->ctx, obj, isl_obj_union_map);
1417 if (obj.type == isl_obj_union_set) {
1418 obj.v = isl_union_set_apply(obj.v, umap);
1419 } else
1420 obj.v = isl_union_map_apply_range(obj.v, umap);
1421 if (!obj.v)
1422 goto error2;
1424 if (isl_stream_eat(s, ')'))
1425 goto error2;
1427 return obj;
1428 error:
1429 isl_union_map_free(umap);
1430 error2:
1431 free_obj(obj);
1432 obj.type = isl_obj_none;
1433 obj.v = NULL;
1434 return obj;
1437 static struct isl_obj apply_fun_set(struct isl_obj obj,
1438 __isl_take isl_union_set *uset)
1440 if (obj.type == isl_obj_union_pw_qpolynomial) {
1441 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1442 } else {
1443 obj.type = isl_obj_list;
1444 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1446 return obj;
1449 static struct isl_obj apply_fun_map(struct isl_obj obj,
1450 __isl_take isl_union_map *umap)
1452 if (obj.type == isl_obj_union_pw_qpolynomial) {
1453 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1454 } else {
1455 obj.type = isl_obj_list;
1456 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1458 return obj;
1461 static struct isl_obj apply_fun(struct isl_stream *s,
1462 struct isl_obj obj, struct isl_hash_table *table)
1464 struct isl_obj arg;
1466 arg = read_expr(s, table);
1467 if (!is_subtype(arg, isl_obj_union_map) &&
1468 !is_subtype(arg, isl_obj_union_set))
1469 isl_die(s->ctx, isl_error_invalid,
1470 "expecting set of map argument", goto error);
1472 if (arg.type == isl_obj_list) {
1473 struct isl_list *list = arg.v;
1474 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1475 arg = obj_at(arg, 0);
1477 if (arg.type == isl_obj_set)
1478 arg = convert(s->ctx, arg, isl_obj_union_set);
1479 else if (arg.type == isl_obj_map)
1480 arg = convert(s->ctx, arg, isl_obj_union_map);
1481 if (arg.type == isl_obj_union_set)
1482 obj = apply_fun_set(obj, arg.v);
1483 else
1484 obj = apply_fun_map(obj, arg.v);
1485 if (!obj.v)
1486 goto error2;
1488 if (isl_stream_eat(s, ')'))
1489 goto error2;
1491 return obj;
1492 error:
1493 free_obj(arg);
1494 error2:
1495 free_obj(obj);
1496 obj.type = isl_obj_none;
1497 obj.v = NULL;
1498 return obj;
1501 struct add_vertex_data {
1502 struct isl_list *list;
1503 int i;
1506 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1508 struct add_vertex_data *data = (struct add_vertex_data *)user;
1509 isl_basic_set *expr;
1511 expr = isl_vertex_get_expr(vertex);
1513 data->list->obj[data->i].type = isl_obj_set;
1514 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1515 data->i++;
1517 isl_vertex_free(vertex);
1519 return 0;
1522 static int set_vertices(__isl_take isl_set *set, void *user)
1524 isl_ctx *ctx;
1525 isl_basic_set *hull;
1526 isl_vertices *vertices = NULL;
1527 struct isl_list *list = NULL;
1528 int r;
1529 struct add_vertex_data *data = (struct add_vertex_data *)user;
1531 set = isl_set_remove_divs(set);
1532 hull = isl_set_convex_hull(set);
1533 vertices = isl_basic_set_compute_vertices(hull);
1534 isl_basic_set_free(hull);
1536 list = data->list;
1538 ctx = isl_vertices_get_ctx(vertices);
1539 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1540 if (!data->list)
1541 goto error;
1543 data->i = 0;
1544 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1546 data->list = isl_list_concat(list, data->list);
1548 isl_vertices_free(vertices);
1550 return r;
1551 error:
1552 data->list = list;
1553 isl_vertices_free(vertices);
1554 return -1;
1557 static struct isl_obj vertices(struct isl_stream *s,
1558 struct isl_hash_table *table)
1560 isl_ctx *ctx;
1561 struct isl_obj obj;
1562 struct isl_list *list = NULL;
1563 isl_union_set *uset;
1564 struct add_vertex_data data = { NULL };
1566 obj = read_expr(s, table);
1567 obj = convert(s->ctx, obj, isl_obj_union_set);
1568 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1569 uset = obj.v;
1570 obj.v = NULL;
1572 ctx = isl_union_set_get_ctx(uset);
1573 list = isl_list_alloc(ctx, 0);
1574 if (!list)
1575 goto error;
1577 data.list = list;
1579 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1580 goto error;
1582 isl_union_set_free(uset);
1584 obj.type = isl_obj_list;
1585 obj.v = data.list;
1587 return obj;
1588 error:
1589 isl_union_set_free(uset);
1590 isl_list_free(data.list);
1591 free_obj(obj);
1592 obj.type = isl_obj_none;
1593 obj.v = NULL;
1594 return obj;
1597 static struct isl_obj type_of(struct isl_stream *s,
1598 struct isl_hash_table *table)
1600 isl_ctx *ctx;
1601 struct isl_obj obj;
1602 const char *type = "unknown";
1604 obj = read_expr(s, table);
1606 if (obj.type == isl_obj_map ||
1607 obj.type == isl_obj_union_map)
1608 type = "map";
1609 if (obj.type == isl_obj_set ||
1610 obj.type == isl_obj_union_set)
1611 type = "set";
1612 if (obj.type == isl_obj_pw_qpolynomial ||
1613 obj.type == isl_obj_union_pw_qpolynomial)
1614 type = "piecewise quasipolynomial";
1615 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1616 obj.type == isl_obj_union_pw_qpolynomial_fold)
1617 type = "piecewise quasipolynomial fold";
1618 if (obj.type == isl_obj_list)
1619 type = "list";
1620 if (obj.type == isl_obj_bool)
1621 type = "boolean";
1622 if (obj.type == isl_obj_str)
1623 type = "string";
1624 if (obj.type == isl_obj_int)
1625 type = "int";
1627 free_obj(obj);
1628 obj.type = isl_obj_str;
1629 obj.v = isl_str_from_string(s->ctx, strdup(type));
1631 return obj;
1634 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1635 struct isl_hash_table *table)
1637 struct isl_obj obj;
1639 obj = read_obj(s, table);
1640 obj = convert(s->ctx, obj, isl_obj_union_set);
1641 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1642 return obj.v;
1643 error:
1644 free_obj(obj);
1645 return NULL;
1648 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1649 struct isl_hash_table *table)
1651 struct isl_obj obj;
1653 obj = read_obj(s, table);
1654 obj = convert(s->ctx, obj, isl_obj_union_map);
1655 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1656 return obj.v;
1657 error:
1658 free_obj(obj);
1659 return NULL;
1662 static struct isl_obj last_any(struct isl_stream *s,
1663 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1664 __isl_take isl_union_map *may_source)
1666 struct isl_obj obj = { isl_obj_none, NULL };
1667 isl_union_map *sink = NULL;
1668 isl_union_map *schedule = NULL;
1669 isl_union_map *may_dep;
1670 isl_union_map *must_dep;
1672 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1673 goto error;
1675 sink = read_map(s, table);
1676 if (!sink)
1677 goto error;
1679 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1680 goto error;
1682 schedule = read_map(s, table);
1683 if (!schedule)
1684 goto error;
1686 if (isl_union_map_compute_flow(sink, must_source, may_source,
1687 schedule, &must_dep, &may_dep,
1688 NULL, NULL) < 0)
1689 return obj;
1691 obj.type = isl_obj_union_map;
1692 obj.v = isl_union_map_union(must_dep, may_dep);
1694 return obj;
1695 error:
1696 isl_union_map_free(may_source);
1697 isl_union_map_free(must_source);
1698 isl_union_map_free(sink);
1699 isl_union_map_free(schedule);
1700 free_obj(obj);
1701 obj.type = isl_obj_none;
1702 obj.v = NULL;
1703 return obj;
1706 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1708 struct isl_obj obj = { isl_obj_none, NULL };
1709 isl_union_map *must_source = NULL;
1710 isl_union_map *may_source = NULL;
1711 isl_union_map *sink = NULL;
1712 isl_union_map *schedule = NULL;
1713 isl_union_map *may_dep;
1715 may_source = read_map(s, table);
1716 if (!may_source)
1717 goto error;
1719 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1720 must_source = read_map(s, table);
1721 if (!must_source)
1722 goto error;
1723 return last_any(s, table, must_source, may_source);
1726 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1727 goto error;
1729 sink = read_map(s, table);
1730 if (!sink)
1731 goto error;
1733 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1734 goto error;
1736 schedule = read_map(s, table);
1737 if (!schedule)
1738 goto error;
1740 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1741 if (isl_union_map_compute_flow(sink, must_source, may_source,
1742 schedule, NULL, &may_dep,
1743 NULL, NULL) < 0)
1744 return obj;
1746 obj.type = isl_obj_union_map;
1747 obj.v = may_dep;
1749 return obj;
1750 error:
1751 isl_union_map_free(may_source);
1752 isl_union_map_free(must_source);
1753 isl_union_map_free(sink);
1754 isl_union_map_free(schedule);
1755 free_obj(obj);
1756 obj.type = isl_obj_none;
1757 obj.v = NULL;
1758 return obj;
1761 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1763 struct isl_obj obj = { isl_obj_none, NULL };
1764 struct isl_list *list = NULL;
1765 isl_union_map *must_source = NULL;
1766 isl_union_map *may_source = NULL;
1767 isl_union_map *sink = NULL;
1768 isl_union_map *schedule = NULL;
1769 isl_union_map *must_dep;
1770 isl_union_map *must_no_source;
1772 must_source = read_map(s, table);
1773 if (!must_source)
1774 goto error;
1776 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1777 may_source = read_map(s, table);
1778 if (!may_source)
1779 goto error;
1780 return last_any(s, table, must_source, may_source);
1783 list = isl_list_alloc(s->ctx, 2);
1784 if (!list)
1785 goto error;
1787 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1788 goto error;
1790 sink = read_map(s, table);
1791 if (!sink)
1792 goto error;
1794 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1795 goto error;
1797 schedule = read_map(s, table);
1798 if (!schedule)
1799 goto error;
1801 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1802 if (isl_union_map_compute_flow(sink, must_source, may_source,
1803 schedule, &must_dep, NULL,
1804 &must_no_source, NULL) < 0) {
1805 isl_list_free(list);
1806 return obj;
1809 list->obj[0].type = isl_obj_union_map;
1810 list->obj[0].v = must_dep;
1811 list->obj[1].type = isl_obj_union_map;
1812 list->obj[1].v = must_no_source;
1814 obj.v = list;
1815 obj.type = isl_obj_list;
1817 return obj;
1818 error:
1819 isl_list_free(list);
1820 isl_union_map_free(may_source);
1821 isl_union_map_free(must_source);
1822 isl_union_map_free(sink);
1823 isl_union_map_free(schedule);
1824 free_obj(obj);
1825 obj.type = isl_obj_none;
1826 obj.v = NULL;
1827 return obj;
1830 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1831 struct isl_hash_table *table)
1833 isl_union_set *domain;
1834 isl_union_map *validity;
1835 isl_union_map *proximity;
1837 domain = read_set(s, table);
1838 if (!domain)
1839 return NULL;
1841 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1842 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1844 for (;;) {
1845 isl_union_map *umap;
1846 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1847 umap = read_map(s, table);
1848 validity = isl_union_map_union(validity, umap);
1849 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1850 umap = read_map(s, table);
1851 proximity = isl_union_map_union(proximity, umap);
1852 } else
1853 break;
1856 return isl_union_set_compute_schedule(domain, validity, proximity);
1859 static struct isl_obj schedule(struct isl_stream *s,
1860 struct isl_hash_table *table)
1862 struct isl_obj obj = { isl_obj_none, NULL };
1863 isl_schedule *schedule;
1865 schedule = get_schedule(s, table);
1867 obj.v = isl_schedule_get_map(schedule);
1868 obj.type = isl_obj_union_map;
1870 isl_schedule_free(schedule);
1872 return obj;
1875 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1877 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1879 struct isl_obj obj = { isl_obj_none, NULL };
1880 isl_ctx *ctx = isl_band_get_ctx(band);
1881 struct isl_list *list;
1883 list = isl_list_alloc(ctx, 2);
1884 if (!list)
1885 goto error;
1887 obj.v = list;
1888 obj.type = isl_obj_list;
1890 list->obj[0].type = isl_obj_union_map;
1891 list->obj[0].v = isl_band_get_partial_schedule(band);
1893 if (isl_band_has_children(band)) {
1894 isl_band_list *children;
1896 children = isl_band_get_children(band);
1897 list->obj[1] = band_list_to_obj_list(children);
1898 } else {
1899 list->obj[1].type = isl_obj_list;
1900 list->obj[1].v = isl_list_alloc(ctx, 0);
1903 if (!list->obj[0].v || !list->obj[1].v)
1904 goto error;
1906 isl_band_free(band);
1908 return obj;
1909 error:
1910 isl_band_free(band);
1911 free_obj(obj);
1912 obj.type = isl_obj_none;
1913 obj.v = NULL;
1914 return obj;
1917 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1919 struct isl_obj obj = { isl_obj_none, NULL };
1920 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1921 struct isl_list *list;
1922 int i, n;
1924 n = isl_band_list_n_band(bands);
1925 list = isl_list_alloc(ctx, n);
1926 if (!list)
1927 goto error;
1929 obj.v = list;
1930 obj.type = isl_obj_list;
1932 for (i = 0; i < n; ++i) {
1933 isl_band *band;
1935 band = isl_band_list_get_band(bands, i);
1936 list->obj[i] = band_to_obj_list(band);
1937 if (!list->obj[i].v)
1938 goto error;
1941 isl_band_list_free(bands);
1943 return obj;
1944 error:
1945 isl_band_list_free(bands);
1946 free_obj(obj);
1947 obj.type = isl_obj_none;
1948 obj.v = NULL;
1949 return obj;
1952 static struct isl_obj schedule_forest(struct isl_stream *s,
1953 struct isl_hash_table *table)
1955 struct isl_obj obj = { isl_obj_none, NULL };
1956 isl_schedule *schedule;
1957 isl_band_list *roots;
1959 schedule = get_schedule(s, table);
1960 if (!schedule)
1961 return obj;
1963 roots = isl_schedule_get_band_forest(schedule);
1964 isl_schedule_free(schedule);
1966 return band_list_to_obj_list(roots);
1969 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1971 struct isl_token *tok;
1973 if (isl_stream_eat_if_available(s, '+'))
1974 return transitive_closure(s->ctx, obj);
1976 tok = isl_stream_next_token(s);
1977 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1978 isl_stream_error(s, tok, "expecting -1");
1979 if (tok)
1980 isl_stream_push_token(s, tok);
1981 goto error;
1983 isl_token_free(tok);
1984 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1985 if (obj.type != isl_obj_union_map)
1986 obj = convert(s->ctx, obj, isl_obj_union_map);
1988 obj.v = isl_union_map_reverse(obj.v);
1989 if (!obj.v)
1990 goto error;
1992 return obj;
1993 error:
1994 free_obj(obj);
1995 obj.type = isl_obj_none;
1996 obj.v = NULL;
1997 return obj;
2000 static struct isl_obj check_assert(struct isl_stream *s,
2001 struct isl_hash_table *table)
2003 struct isl_obj obj;
2005 obj = read_expr(s, table);
2006 if (obj.type != isl_obj_bool)
2007 isl_die(s->ctx, isl_error_invalid,
2008 "expecting boolean expression", goto error);
2009 if (obj.v != &isl_bool_true)
2010 isl_die(s->ctx, isl_error_unknown,
2011 "assertion failed", abort());
2012 error:
2013 free_obj(obj);
2014 obj.type = isl_obj_none;
2015 obj.v = NULL;
2016 return obj;
2019 static struct isl_obj read_from_file(struct isl_stream *s)
2021 struct isl_obj obj;
2022 struct isl_token *tok;
2023 struct isl_stream *s_file;
2024 struct iscc_options *options;
2025 FILE *file;
2027 tok = isl_stream_next_token(s);
2028 if (!tok || tok->type != ISL_TOKEN_STRING) {
2029 isl_stream_error(s, tok, "expecting filename");
2030 isl_token_free(tok);
2031 goto error;
2034 options = isl_ctx_peek_iscc_options(s->ctx);
2035 if (!options || !options->io) {
2036 isl_token_free(tok);
2037 isl_die(s->ctx, isl_error_invalid,
2038 "read operation not allowed", goto error);
2041 file = fopen(tok->u.s, "r");
2042 isl_token_free(tok);
2043 isl_assert(s->ctx, file, goto error);
2045 s_file = isl_stream_new_file(s->ctx, file);
2046 if (!s_file) {
2047 fclose(file);
2048 goto error;
2051 obj = isl_stream_read_obj(s_file);
2053 isl_stream_free(s_file);
2054 fclose(file);
2056 return obj;
2057 error:
2058 obj.type = isl_obj_none;
2059 obj.v = NULL;
2060 return obj;
2063 static struct isl_obj write_to_file(struct isl_stream *s,
2064 struct isl_hash_table *table)
2066 struct isl_obj obj;
2067 struct isl_token *tok;
2068 struct isl_stream *s_file;
2069 struct iscc_options *options;
2070 FILE *file;
2071 isl_printer *p;
2073 tok = isl_stream_next_token(s);
2074 if (!tok || tok->type != ISL_TOKEN_STRING) {
2075 isl_stream_error(s, tok, "expecting filename");
2076 isl_token_free(tok);
2077 goto error;
2080 obj = read_expr(s, table);
2082 options = isl_ctx_peek_iscc_options(s->ctx);
2083 if (!options || !options->io) {
2084 isl_token_free(tok);
2085 isl_die(s->ctx, isl_error_invalid,
2086 "write operation not allowed", goto error);
2089 file = fopen(tok->u.s, "w");
2090 isl_token_free(tok);
2091 if (!file)
2092 isl_die(s->ctx, isl_error_unknown,
2093 "could not open file for writing", goto error);
2095 p = isl_printer_to_file(s->ctx, file);
2096 p = isl_printer_set_output_format(p, options->format);
2097 p = obj.type->print(p, obj.v);
2098 p = isl_printer_end_line(p);
2099 isl_printer_free(p);
2101 fclose(file);
2102 error:
2103 free_obj(obj);
2104 obj.type = isl_obj_none;
2105 obj.v = NULL;
2106 return obj;
2109 static struct isl_obj read_string_if_available(struct isl_stream *s)
2111 struct isl_token *tok;
2112 struct isl_obj obj = { isl_obj_none, NULL };
2114 tok = isl_stream_next_token(s);
2115 if (!tok)
2116 return obj;
2117 if (tok->type == ISL_TOKEN_STRING) {
2118 isl_str *str;
2119 str = isl_str_alloc(s->ctx);
2120 if (!str)
2121 goto error;
2122 str->s = strdup(tok->u.s);
2123 isl_token_free(tok);
2124 obj.v = str;
2125 obj.type = isl_obj_str;
2126 } else
2127 isl_stream_push_token(s, tok);
2128 return obj;
2129 error:
2130 isl_token_free(tok);
2131 return obj;
2134 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2136 struct isl_token *tok;
2137 struct isl_obj obj = { isl_obj_none, NULL };
2139 tok = isl_stream_next_token(s);
2140 if (!tok)
2141 return obj;
2142 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2143 int is_true = tok->type == ISL_TOKEN_TRUE;
2144 isl_token_free(tok);
2145 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2146 obj.type = isl_obj_bool;
2147 } else
2148 isl_stream_push_token(s, tok);
2149 return obj;
2150 error:
2151 isl_token_free(tok);
2152 return obj;
2155 static __isl_give char *read_ident(struct isl_stream *s)
2157 char *name;
2158 struct isl_token *tok, *tok2;
2160 name = isl_stream_read_ident_if_available(s);
2161 if (name)
2162 return name;
2164 tok = isl_stream_next_token(s);
2165 if (!tok)
2166 return NULL;
2167 if (tok->type != '$') {
2168 isl_stream_push_token(s, tok);
2169 return NULL;
2171 tok2 = isl_stream_next_token(s);
2172 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2173 if (tok2)
2174 isl_stream_push_token(s, tok2);
2175 isl_stream_push_token(s, tok);
2176 return NULL;
2179 name = isl_int_get_str(tok2->u.v);
2180 isl_token_free(tok);
2181 isl_token_free(tok2);
2183 return name;
2186 static struct isl_obj read_list(struct isl_stream *s,
2187 struct isl_hash_table *table, struct isl_obj obj)
2189 struct isl_list *list;
2191 list = isl_list_alloc(s->ctx, 2);
2192 if (!list)
2193 goto error;
2194 list->obj[0] = obj;
2195 list->obj[1] = read_obj(s, table);
2196 obj.v = list;
2197 obj.type = isl_obj_list;
2199 if (!list->obj[1].v)
2200 goto error;
2202 while (isl_stream_eat_if_available(s, ',')) {
2203 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2204 if (!obj.v)
2205 goto error;
2208 return obj;
2209 error:
2210 free_obj(obj);
2211 obj.type = isl_obj_none;
2212 obj.v = NULL;
2213 return obj;
2216 static struct isl_obj read_obj(struct isl_stream *s,
2217 struct isl_hash_table *table)
2219 struct isl_obj obj = { isl_obj_none, NULL };
2220 char *name = NULL;
2221 struct isc_un_op *op = NULL;
2223 obj = read_string_if_available(s);
2224 if (obj.v)
2225 return obj;
2226 obj = read_bool_if_available(s);
2227 if (obj.v)
2228 return obj;
2229 if (isl_stream_eat_if_available(s, '(')) {
2230 if (isl_stream_next_token_is(s, ')')) {
2231 obj.type = isl_obj_list;
2232 obj.v = isl_list_alloc(s->ctx, 0);
2233 } else {
2234 obj = read_expr(s, table);
2235 if (obj.v && isl_stream_eat_if_available(s, ','))
2236 obj = read_list(s, table, obj);
2238 if (!obj.v || isl_stream_eat(s, ')'))
2239 goto error;
2240 } else {
2241 op = read_prefix_un_op_if_available(s);
2242 if (op)
2243 return read_un_op_expr(s, table, op);
2245 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2246 return check_assert(s, table);
2247 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2248 return read_from_file(s);
2249 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2250 return write_to_file(s, table);
2251 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2252 return vertices(s, table);
2253 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2254 return any(s, table);
2255 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2256 return last(s, table);
2257 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2258 return schedule(s, table);
2259 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2260 return schedule_forest(s, table);
2261 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2262 return type_of(s, table);
2264 name = read_ident(s);
2265 if (name)
2266 obj = stored_obj(s->ctx, table, name);
2267 else
2268 obj = isl_stream_read_obj(s);
2269 if (!obj.v)
2270 goto error;
2273 if (isl_stream_eat_if_available(s, '^'))
2274 obj = power(s, obj);
2275 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2276 obj = obj_at_index(s, obj);
2277 else if (is_subtype(obj, isl_obj_union_map) &&
2278 isl_stream_eat_if_available(s, '(')) {
2279 obj = convert(s->ctx, obj, isl_obj_union_map);
2280 obj = apply(s, obj.v, table);
2281 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2282 isl_stream_eat_if_available(s, '(')) {
2283 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2284 obj = apply_fun(s, obj, table);
2285 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2286 isl_stream_eat_if_available(s, '(')) {
2287 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2288 obj = apply_fun(s, obj, table);
2291 return obj;
2292 error:
2293 free_obj(obj);
2294 obj.type = isl_obj_none;
2295 obj.v = NULL;
2296 return obj;
2299 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2300 struct isl_obj lhs, struct isl_obj rhs)
2302 int i;
2304 for (i = 0; ; ++i) {
2305 if (!bin_ops[i].op)
2306 break;
2307 if (bin_ops[i].op != like->op)
2308 continue;
2309 if (!is_subtype(lhs, bin_ops[i].lhs))
2310 continue;
2311 if (!is_subtype(rhs, bin_ops[i].rhs))
2312 continue;
2314 return &bin_ops[i];
2317 for (i = 0; ; ++i) {
2318 if (!named_bin_ops[i].name)
2319 break;
2320 if (named_bin_ops[i].op.op != like->op)
2321 continue;
2322 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2323 continue;
2324 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2325 continue;
2327 return &named_bin_ops[i].op;
2330 return NULL;
2333 static int next_is_neg_int(struct isl_stream *s)
2335 struct isl_token *tok;
2336 int ret;
2338 tok = isl_stream_next_token(s);
2339 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2340 isl_stream_push_token(s, tok);
2342 return ret;
2345 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2346 struct isl_obj lhs, struct isl_obj rhs)
2348 struct isl_obj obj;
2350 lhs = convert(ctx, lhs, op->lhs);
2351 rhs = convert(ctx, rhs, op->rhs);
2352 if (op->res != isl_obj_bool)
2353 obj.v = op->o.fn(lhs.v, rhs.v);
2354 else {
2355 int res = op->o.test(lhs.v, rhs.v);
2356 free_obj(lhs);
2357 free_obj(rhs);
2358 obj.v = isl_bool_from_int(res);
2360 obj.type = op->res;
2362 return obj;
2365 static struct isl_obj read_expr(struct isl_stream *s,
2366 struct isl_hash_table *table)
2368 struct isl_obj obj = { isl_obj_none, NULL };
2369 struct isl_obj right_obj = { isl_obj_none, NULL };
2371 obj = read_obj(s, table);
2372 for (; obj.v;) {
2373 struct isc_bin_op *op = NULL;
2375 op = read_bin_op_if_available(s, obj);
2376 if (!op)
2377 break;
2379 right_obj = read_obj(s, table);
2381 op = find_matching_bin_op(op, obj, right_obj);
2383 if (!op)
2384 isl_die(s->ctx, isl_error_invalid,
2385 "no such binary operator defined on given operands",
2386 goto error);
2388 obj = call_bin_op(s->ctx, op, obj, right_obj);
2391 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2392 right_obj = read_obj(s, table);
2393 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2396 return obj;
2397 error:
2398 free_obj(right_obj);
2399 free_obj(obj);
2400 obj.type = isl_obj_none;
2401 obj.v = NULL;
2402 return obj;
2405 static __isl_give isl_printer *source_file(struct isl_stream *s,
2406 struct isl_hash_table *table, __isl_take isl_printer *p);
2408 static __isl_give isl_printer *read_line(struct isl_stream *s,
2409 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2411 struct isl_obj obj = { isl_obj_none, NULL };
2412 char *lhs = NULL;
2413 int assign = 0;
2414 int only_print = 0;
2415 struct isc_bin_op *op = NULL;
2416 char buf[30];
2418 if (!p)
2419 return NULL;
2420 if (isl_stream_is_empty(s))
2421 return p;
2423 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2424 return source_file(s, table, p);
2426 assign = is_assign(s);
2427 if (assign) {
2428 lhs = isl_stream_read_ident_if_available(s);
2429 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2430 goto error;
2431 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2432 only_print = 1;
2433 else if (!tty)
2434 only_print = 1;
2436 obj = read_expr(s, table);
2437 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2438 fprintf(stderr, "Interrupted\n");
2439 isl_ctx_reset_error(s->ctx);
2441 if (isl_stream_eat(s, ';'))
2442 goto error;
2444 if (only_print) {
2445 if (obj.type != isl_obj_none && obj.v != NULL) {
2446 p = obj.type->print(p, obj.v);
2447 p = isl_printer_end_line(p);
2449 free_obj(obj);
2450 return p;
2452 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2453 static int count = 0;
2454 snprintf(buf, sizeof(buf), "$%d", count++);
2455 lhs = strdup(buf + 1);
2457 p = isl_printer_print_str(p, buf);
2458 p = isl_printer_print_str(p, " := ");
2459 p = obj.type->print(p, obj.v);
2460 p = isl_printer_end_line(p);
2462 if (lhs && do_assign(s->ctx, table, lhs, obj))
2463 return p;
2465 return p;
2466 error:
2467 isl_stream_flush_tokens(s);
2468 isl_stream_skip_line(s);
2469 free(lhs);
2470 free_obj(obj);
2471 return p;
2474 int free_cb(void **entry, void *user)
2476 struct isl_named_obj *named = *entry;
2478 free_obj(named->obj);
2479 free(named->name);
2480 free(named);
2482 return 0;
2485 static void register_named_ops(struct isl_stream *s)
2487 int i;
2489 for (i = 0; i < ISCC_N_OP; ++i) {
2490 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2491 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2494 for (i = 0; ; ++i) {
2495 if (!named_un_ops[i].name)
2496 break;
2497 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2498 named_un_ops[i].name);
2499 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2502 for (i = 0; ; ++i) {
2503 if (!named_bin_ops[i].name)
2504 break;
2505 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2506 named_bin_ops[i].name);
2507 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2511 static __isl_give isl_printer *source_file(struct isl_stream *s,
2512 struct isl_hash_table *table, __isl_take isl_printer *p)
2514 struct isl_token *tok;
2515 struct isl_stream *s_file;
2516 FILE *file;
2518 tok = isl_stream_next_token(s);
2519 if (!tok || tok->type != ISL_TOKEN_STRING) {
2520 isl_stream_error(s, tok, "expecting filename");
2521 isl_token_free(tok);
2522 return p;
2525 file = fopen(tok->u.s, "r");
2526 isl_token_free(tok);
2527 isl_assert(s->ctx, file, return p);
2529 s_file = isl_stream_new_file(s->ctx, file);
2530 if (!s_file) {
2531 fclose(file);
2532 return p;
2535 register_named_ops(s_file);
2537 while (!s_file->eof)
2538 p = read_line(s_file, table, p, 0);
2540 isl_stream_free(s_file);
2541 fclose(file);
2543 isl_stream_eat(s, ';');
2545 return p;
2548 int main(int argc, char **argv)
2550 struct isl_ctx *ctx;
2551 struct isl_stream *s;
2552 struct isl_hash_table *table;
2553 struct iscc_options *options;
2554 isl_printer *p;
2555 int tty = isatty(0);
2557 options = iscc_options_new_with_defaults();
2558 assert(options);
2560 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2561 pet_options_set_autodetect(ctx, 1);
2562 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2563 s = isl_stream_new_file(ctx, stdin);
2564 assert(s);
2565 table = isl_hash_table_alloc(ctx, 10);
2566 assert(table);
2567 p = isl_printer_to_file(ctx, stdout);
2568 p = isl_printer_set_output_format(p, options->format);
2569 assert(p);
2571 register_named_ops(s);
2573 install_signal_handler(ctx);
2575 while (p && !s->eof) {
2576 isl_ctx_resume(ctx);
2577 p = read_line(s, table, p, tty);
2580 remove_signal_handler(ctx);
2582 isl_printer_free(p);
2583 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2584 isl_hash_table_free(ctx, table);
2585 isl_stream_free(s);
2586 isl_ctx_free(ctx);
2588 return 0;