update pet for fix in distribution
[barvinok.git] / iscc.c
blobd949f3cec2d3b5fd2a9f97b07f597adbedc2ee63
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_pw_qpolynomial, isl_obj_union_set,
557 isl_obj_union_pw_qpolynomial,
558 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
559 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
560 isl_obj_union_pw_qpolynomial_fold,
561 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
562 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
563 isl_obj_union_pw_qpolynomial, isl_obj_bool,
564 { .test = (isc_bin_test_fn)
565 &isl_union_pw_qpolynomial_plain_is_equal } },
566 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
567 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
568 { .test = (isc_bin_test_fn)
569 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
570 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
571 (isc_bin_op_fn) &isl_str_concat },
575 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
576 __isl_take isl_union_map *umap2)
578 return isl_union_map_apply_range(umap2, umap1);
581 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
582 __isl_take isl_union_pw_qpolynomial *upwqp,
583 __isl_take isl_union_map *umap)
585 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
588 static __isl_give struct isl_list *qpolynomial_fold_after_map(
589 __isl_take isl_union_pw_qpolynomial_fold *upwf,
590 __isl_take isl_union_map *umap)
592 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
595 struct isc_named_bin_op named_bin_ops[] = {
596 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
597 isl_obj_union_map,
598 (isc_bin_op_fn) &map_after_map } },
599 { "after", { -1, isl_obj_union_pw_qpolynomial,
600 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
601 (isc_bin_op_fn) &qpolynomial_after_map } },
602 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
603 isl_obj_union_map, isl_obj_list,
604 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
605 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
606 isl_obj_union_map,
607 (isc_bin_op_fn) &isl_union_map_apply_range } },
608 { "before", { -1, isl_obj_union_map,
609 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
610 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
611 { "before", { -1, isl_obj_union_map,
612 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
613 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
614 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
615 isl_obj_union_set,
616 (isc_bin_op_fn) &isl_union_set_product } },
617 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
618 isl_obj_union_map,
619 (isc_bin_op_fn) &isl_union_map_product } },
620 NULL
623 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
625 return isl_set_from_basic_set(isl_union_set_sample(uset));
628 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
630 return isl_map_from_basic_map(isl_union_map_sample(umap));
633 static __isl_give struct isl_list *union_map_power(
634 __isl_take isl_union_map *umap)
636 isl_ctx *ctx;
637 struct isl_list *list;
638 int exact;
640 ctx = isl_union_map_get_ctx(umap);
641 list = isl_list_alloc(ctx, 2);
642 if (!list)
643 goto error2;
645 list->obj[0].type = isl_obj_union_map;
646 list->obj[0].v = isl_union_map_power(umap, &exact);
647 list->obj[1].type = isl_obj_bool;
648 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
649 if (exact < 0 || !list->obj[0].v)
650 goto error;
652 return list;
653 error2:
654 isl_union_map_free(umap);
655 error:
656 isl_list_free(list);
657 return NULL;
660 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
661 __isl_take isl_union_pw_qpolynomial *upwqp)
663 isl_ctx *ctx;
664 struct isl_list *list;
665 int tight;
667 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
668 list = isl_list_alloc(ctx, 2);
669 if (!list)
670 goto error2;
672 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
673 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
674 isl_fold_max, &tight);
675 list->obj[1].type = isl_obj_bool;
676 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
677 if (tight < 0 || !list->obj[0].v)
678 goto error;
680 return list;
681 error2:
682 isl_union_pw_qpolynomial_free(upwqp);
683 error:
684 isl_list_free(list);
685 return NULL;
688 #ifdef HAVE_CLOOG
689 void *map_codegen(void *arg)
691 isl_space *dim;
692 isl_union_map *umap = (isl_union_map *)arg;
693 isl_ctx *ctx = isl_union_map_get_ctx(umap);
694 CloogState *state;
695 CloogOptions *options;
696 CloogDomain *context;
697 CloogUnionDomain *ud;
698 CloogInput *input;
699 struct clast_stmt *stmt;
701 state = cloog_isl_state_malloc(ctx);
702 options = cloog_options_malloc(state);
703 options->language = LANGUAGE_C;
704 options->strides = 1;
705 options->sh = 1;
707 ud = cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap));
709 dim = isl_union_map_get_space(umap);
710 context = cloog_domain_from_isl_set(isl_set_universe(dim));
712 input = cloog_input_alloc(context, ud);
714 stmt = cloog_clast_create_from_input(input, options);
715 clast_pprint(stdout, stmt, 0, options);
716 cloog_clast_free(stmt);
718 error:
719 cloog_options_free(options);
720 cloog_state_free(state);
721 isl_union_map_free(umap);
722 return NULL;
725 void *set_codegen(void *arg)
727 isl_space *dim;
728 isl_union_set *uset = (isl_union_set *)arg;
729 isl_ctx *ctx = isl_union_set_get_ctx(uset);
730 CloogState *state;
731 CloogOptions *options;
732 CloogDomain *context;
733 CloogUnionDomain *ud;
734 CloogInput *input;
735 struct clast_stmt *stmt;
737 if (isl_union_set_n_set(uset) > 1)
738 isl_die(ctx, isl_error_invalid,
739 "code generation for more than one domain "
740 "requires a schedule", goto error);
742 state = cloog_isl_state_malloc(ctx);
743 options = cloog_options_malloc(state);
744 options->language = LANGUAGE_C;
745 options->strides = 1;
746 options->sh = 1;
748 ud = cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset));
750 dim = isl_union_set_get_space(uset);
751 context = cloog_domain_from_isl_set(isl_set_universe(dim));
753 input = cloog_input_alloc(context, ud);
755 stmt = cloog_clast_create_from_input(input, options);
756 clast_pprint(stdout, stmt, 0, options);
757 cloog_clast_free(stmt);
759 cloog_options_free(options);
760 cloog_state_free(state);
761 error:
762 isl_union_set_free(uset);
763 return NULL;
765 #endif
767 #ifdef HAVE_PET
768 static __isl_give isl_list *parse(__isl_take isl_str *str)
770 isl_ctx *ctx;
771 struct isl_list *list;
772 struct pet_scop *scop;
773 isl_union_map *sched, *reads, *writes;
774 isl_union_set *domain;
775 struct iscc_options *options;
777 if (!str)
778 return NULL;
779 ctx = str->ctx;
781 options = isl_ctx_peek_iscc_options(ctx);
782 if (!options || !options->io) {
783 isl_str_free(str);
784 isl_die(ctx, isl_error_invalid,
785 "parse_file operation not allowed", return NULL);
788 list = isl_list_alloc(ctx, 4);
789 if (!list)
790 goto error;
792 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL, 1);
793 domain = pet_scop_collect_domains(scop);
794 sched = pet_scop_collect_schedule(scop);
795 reads = pet_scop_collect_reads(scop);
796 writes = pet_scop_collect_writes(scop);
797 pet_scop_free(scop);
799 list->obj[0].type = isl_obj_union_set;
800 list->obj[0].v = domain;
801 list->obj[1].type = isl_obj_union_map;
802 list->obj[1].v = writes;
803 list->obj[2].type = isl_obj_union_map;
804 list->obj[2].v = reads;
805 list->obj[3].type = isl_obj_union_map;
806 list->obj[3].v = sched;
808 if (!list->obj[0].v || !list->obj[1].v ||
809 !list->obj[2].v || !list->obj[3].v)
810 goto error;
812 isl_str_free(str);
813 return list;
814 error:
815 isl_list_free(list);
816 isl_str_free(str);
817 return NULL;
819 #endif
821 static int add_point(__isl_take isl_point *pnt, void *user)
823 isl_union_set **scan = (isl_union_set **) user;
825 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
827 return 0;
830 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
832 isl_union_set *scan;
834 scan = isl_union_set_empty(isl_union_set_get_space(uset));
836 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
837 isl_union_set_free(scan);
838 return uset;
841 isl_union_set_free(uset);
842 return scan;
845 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
847 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
850 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
851 __isl_take isl_union_pw_qpolynomial *upwqp)
853 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
856 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
857 __isl_take isl_union_pw_qpolynomial *upwqp)
859 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
862 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
863 __isl_take isl_union_pw_qpolynomial *upwqp)
865 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
868 typedef void *(*isc_un_op_fn)(void *arg);
869 struct isc_un_op {
870 enum isl_token_type op;
871 isl_obj_type arg;
872 isl_obj_type res;
873 isc_un_op_fn fn;
875 struct isc_named_un_op {
876 char *name;
877 struct isc_un_op op;
879 struct isc_named_un_op named_un_ops[] = {
880 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
881 (isc_un_op_fn) &isl_union_map_affine_hull } },
882 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
883 (isc_un_op_fn) &isl_union_set_affine_hull } },
884 {"card", { -1, isl_obj_union_set,
885 isl_obj_union_pw_qpolynomial,
886 (isc_un_op_fn) &isl_union_set_card } },
887 {"card", { -1, isl_obj_union_map,
888 isl_obj_union_pw_qpolynomial,
889 (isc_un_op_fn) &isl_union_map_card } },
890 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
891 (isc_un_op_fn) &isl_union_set_coalesce } },
892 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
893 (isc_un_op_fn) &isl_union_map_coalesce } },
894 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
895 isl_obj_union_pw_qpolynomial,
896 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
897 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
898 isl_obj_union_pw_qpolynomial_fold,
899 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
900 #ifdef HAVE_CLOOG
901 {"codegen", { -1, isl_obj_union_set, isl_obj_none,
902 &set_codegen } },
903 {"codegen", { -1, isl_obj_union_map, isl_obj_none,
904 &map_codegen } },
905 #endif
906 {"coefficients", { -1, isl_obj_union_set,
907 isl_obj_union_set,
908 (isc_un_op_fn) &isl_union_set_coefficients } },
909 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
910 (isc_un_op_fn) &isl_union_set_solutions } },
911 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
912 (isc_un_op_fn) &isl_union_map_deltas } },
913 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
914 (isc_un_op_fn) &isl_union_map_deltas_map } },
915 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
916 (isc_un_op_fn) &isl_union_map_domain } },
917 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
918 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
919 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
920 isl_obj_union_set,
921 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
922 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
923 (isc_un_op_fn) &isl_union_map_domain } },
924 {"domain", { -1, isl_obj_union_pw_qpolynomial,
925 isl_obj_union_set,
926 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
927 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
928 isl_obj_union_set,
929 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
930 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
931 (isc_un_op_fn) &isl_union_map_domain_map } },
932 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
933 (isc_un_op_fn) &isl_union_map_range } },
934 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
935 (isc_un_op_fn) &isl_union_map_range } },
936 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
937 (isc_un_op_fn) &isl_union_map_range_map } },
938 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
939 (isc_un_op_fn) &isl_union_set_identity } },
940 {"lattice_width", { -1, isl_obj_union_set,
941 isl_obj_union_pw_qpolynomial,
942 (isc_un_op_fn) &isl_union_set_lattice_width } },
943 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
944 (isc_un_op_fn) &isl_union_map_lexmin } },
945 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
946 (isc_un_op_fn) &isl_union_map_lexmax } },
947 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
948 (isc_un_op_fn) &isl_union_set_lexmin } },
949 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
950 (isc_un_op_fn) &isl_union_set_lexmax } },
951 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
952 (isc_un_op_fn) &isl_union_set_lift } },
953 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
954 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
955 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
956 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
957 {"poly", { -1, isl_obj_union_pw_qpolynomial,
958 isl_obj_union_pw_qpolynomial,
959 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
960 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
961 isl_obj_union_pw_qpolynomial,
962 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
963 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
964 isl_obj_union_pw_qpolynomial,
965 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
966 #ifdef HAVE_PET
967 {"parse_file", { -1, isl_obj_str, isl_obj_list,
968 (isc_un_op_fn) &parse } },
969 #endif
970 {"pow", { -1, isl_obj_union_map, isl_obj_list,
971 (isc_un_op_fn) &union_map_power } },
972 {"sample", { -1, isl_obj_union_set, isl_obj_set,
973 (isc_un_op_fn) &union_set_sample } },
974 {"sample", { -1, isl_obj_union_map, isl_obj_map,
975 (isc_un_op_fn) &union_map_sample } },
976 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
977 (isc_un_op_fn) &union_set_scan } },
978 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
979 (isc_un_op_fn) &union_map_scan } },
980 {"sum", { -1, isl_obj_union_pw_qpolynomial,
981 isl_obj_union_pw_qpolynomial,
982 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
983 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
984 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
985 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
986 (isc_un_op_fn) &isl_union_set_unwrap } },
987 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
988 (isc_un_op_fn) &isl_union_map_wrap } },
989 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
990 (isc_un_op_fn) &isl_union_map_zip } },
991 NULL
994 struct isl_named_obj {
995 char *name;
996 struct isl_obj obj;
999 static void free_obj(struct isl_obj obj)
1001 obj.type->free(obj.v);
1004 static int same_name(const void *entry, const void *val)
1006 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1008 return !strcmp(named->name, val);
1011 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1012 char *name, struct isl_obj obj)
1014 struct isl_hash_table_entry *entry;
1015 uint32_t name_hash;
1016 struct isl_named_obj *named;
1018 name_hash = isl_hash_string(isl_hash_init(), name);
1019 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1020 if (!entry)
1021 goto error;
1022 if (entry->data) {
1023 named = entry->data;
1024 free_obj(named->obj);
1025 free(name);
1026 } else {
1027 named = isl_alloc_type(ctx, struct isl_named_obj);
1028 if (!named)
1029 goto error;
1030 named->name = name;
1031 entry->data = named;
1033 named->obj = obj;
1035 return 0;
1036 error:
1037 free_obj(obj);
1038 free(name);
1039 return -1;
1042 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1043 struct isl_hash_table *table, char *name)
1045 struct isl_obj obj = { isl_obj_none, NULL };
1046 struct isl_hash_table_entry *entry;
1047 uint32_t name_hash;
1049 name_hash = isl_hash_string(isl_hash_init(), name);
1050 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1051 if (entry) {
1052 struct isl_named_obj *named;
1053 named = entry->data;
1054 obj = named->obj;
1055 } else if (isdigit(name[0]))
1056 fprintf(stderr, "unknown identifier '$%s'\n", name);
1057 else
1058 fprintf(stderr, "unknown identifier '%s'\n", name);
1060 free(name);
1061 obj.v = obj.type->copy(obj.v);
1062 return obj;
1065 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1067 if (obj.type == super)
1068 return 1;
1069 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1070 return 1;
1071 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1072 return 1;
1073 if (obj.type == isl_obj_pw_qpolynomial &&
1074 super == isl_obj_union_pw_qpolynomial)
1075 return 1;
1076 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1077 super == isl_obj_union_pw_qpolynomial_fold)
1078 return 1;
1079 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1080 return 1;
1081 if (obj.type == isl_obj_list) {
1082 struct isl_list *list = obj.v;
1083 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1084 return is_subtype(list->obj[0], super);
1086 if (super == isl_obj_str)
1087 return 1;
1088 return 0;
1091 static struct isl_obj obj_at(struct isl_obj obj, int i)
1093 struct isl_list *list = obj.v;
1095 obj = list->obj[i];
1096 obj.v = obj.type->copy(obj.v);
1098 isl_list_free(list);
1100 return obj;
1103 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1104 isl_obj_type type)
1106 if (obj.type == type)
1107 return obj;
1108 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1109 obj.type = isl_obj_union_map;
1110 obj.v = isl_union_map_from_map(obj.v);
1111 return obj;
1113 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1114 obj.type = isl_obj_union_set;
1115 obj.v = isl_union_set_from_set(obj.v);
1116 return obj;
1118 if (obj.type == isl_obj_pw_qpolynomial &&
1119 type == isl_obj_union_pw_qpolynomial) {
1120 obj.type = isl_obj_union_pw_qpolynomial;
1121 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1122 return obj;
1124 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1125 type == isl_obj_union_pw_qpolynomial_fold) {
1126 obj.type = isl_obj_union_pw_qpolynomial_fold;
1127 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1128 return obj;
1130 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1131 if (type == isl_obj_union_map) {
1132 obj.type = isl_obj_union_map;
1133 return obj;
1135 if (type == isl_obj_union_pw_qpolynomial) {
1136 isl_space *dim = isl_union_set_get_space(obj.v);
1137 isl_union_set_free(obj.v);
1138 obj.v = isl_union_pw_qpolynomial_zero(dim);
1139 obj.type = isl_obj_union_pw_qpolynomial;
1140 return obj;
1142 if (type == isl_obj_union_pw_qpolynomial_fold) {
1143 isl_space *dim = isl_union_set_get_space(obj.v);
1144 isl_union_set_free(obj.v);
1145 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1146 isl_fold_list);
1147 obj.type = isl_obj_union_pw_qpolynomial_fold;
1148 return obj;
1151 if (obj.type == isl_obj_list) {
1152 struct isl_list *list = obj.v;
1153 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1154 return convert(ctx, obj_at(obj, 0), type);
1156 if (type == isl_obj_str) {
1157 isl_str *str;
1158 isl_printer *p;
1159 char *s;
1161 p = isl_printer_to_str(ctx);
1162 if (!p)
1163 goto error;
1164 p = obj.type->print(p, obj.v);
1165 s = isl_printer_get_str(p);
1166 isl_printer_free(p);
1168 str = isl_str_from_string(ctx, s);
1169 if (!str)
1170 goto error;
1171 free_obj(obj);
1172 obj.v = str;
1173 obj.type = isl_obj_str;
1174 return obj;
1177 error:
1178 free_obj(obj);
1179 obj.type = isl_obj_none;
1180 obj.v = NULL;
1181 return obj;
1184 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1185 struct isl_obj lhs)
1187 int i;
1188 struct isl_token *tok;
1190 tok = isl_stream_next_token(s);
1191 if (!tok)
1192 return NULL;
1194 for (i = 0; ; ++i) {
1195 if (!bin_ops[i].op)
1196 break;
1197 if (bin_ops[i].op != tok->type)
1198 continue;
1199 if (!is_subtype(lhs, bin_ops[i].lhs))
1200 continue;
1202 isl_token_free(tok);
1203 return &bin_ops[i];
1206 for (i = 0; ; ++i) {
1207 if (!named_bin_ops[i].name)
1208 break;
1209 if (named_bin_ops[i].op.op != tok->type)
1210 continue;
1211 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1212 continue;
1214 isl_token_free(tok);
1215 return &named_bin_ops[i].op;
1218 isl_stream_push_token(s, tok);
1220 return NULL;
1223 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1225 int i;
1226 struct isl_token *tok;
1228 tok = isl_stream_next_token(s);
1229 if (!tok)
1230 return NULL;
1232 for (i = 0; ; ++i) {
1233 if (!named_un_ops[i].name)
1234 break;
1235 if (named_un_ops[i].op.op != tok->type)
1236 continue;
1238 isl_token_free(tok);
1239 return &named_un_ops[i].op;
1242 isl_stream_push_token(s, tok);
1244 return NULL;
1247 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1248 struct isl_obj arg)
1250 int i;
1252 for (i = 0; ; ++i) {
1253 if (!named_un_ops[i].name)
1254 break;
1255 if (named_un_ops[i].op.op != like->op)
1256 continue;
1257 if (!is_subtype(arg, named_un_ops[i].op.arg))
1258 continue;
1260 return &named_un_ops[i].op;
1263 return NULL;
1266 static int is_assign(struct isl_stream *s)
1268 struct isl_token *tok;
1269 struct isl_token *tok2;
1270 int assign;
1272 tok = isl_stream_next_token(s);
1273 if (!tok)
1274 return 0;
1275 if (tok->type != ISL_TOKEN_IDENT) {
1276 isl_stream_push_token(s, tok);
1277 return 0;
1280 tok2 = isl_stream_next_token(s);
1281 if (!tok2) {
1282 isl_stream_push_token(s, tok);
1283 return 0;
1285 assign = tok2->type == ISL_TOKEN_DEF;
1286 isl_stream_push_token(s, tok2);
1287 isl_stream_push_token(s, tok);
1289 return assign;
1292 static struct isl_obj read_obj(struct isl_stream *s,
1293 struct isl_hash_table *table);
1294 static struct isl_obj read_expr(struct isl_stream *s,
1295 struct isl_hash_table *table);
1297 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1298 struct isl_hash_table *table, struct isc_un_op *op)
1300 struct isl_obj obj = { isl_obj_none, NULL };
1302 obj = read_obj(s, table);
1303 if (!obj.v)
1304 goto error;
1306 op = find_matching_un_op(op, obj);
1308 if (!op)
1309 isl_die(s->ctx, isl_error_invalid,
1310 "no such unary operator defined on given operand",
1311 goto error);
1313 obj = convert(s->ctx, obj, op->arg);
1314 obj.v = op->fn(obj.v);
1315 obj.type = op->res;
1317 return obj;
1318 error:
1319 free_obj(obj);
1320 obj.type = isl_obj_none;
1321 obj.v = NULL;
1322 return obj;
1325 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1327 struct isl_list *list;
1328 int exact;
1330 if (obj.type != isl_obj_union_map)
1331 obj = convert(ctx, obj, isl_obj_union_map);
1332 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1333 list = isl_list_alloc(ctx, 2);
1334 if (!list)
1335 goto error;
1337 list->obj[0].type = isl_obj_union_map;
1338 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1339 list->obj[1].type = isl_obj_bool;
1340 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1341 obj.v = list;
1342 obj.type = isl_obj_list;
1343 if (exact < 0 || !list->obj[0].v)
1344 goto error;
1346 return obj;
1347 error:
1348 free_obj(obj);
1349 obj.type = isl_obj_none;
1350 obj.v = NULL;
1351 return obj;
1354 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1356 struct isl_list *list = obj.v;
1357 struct isl_token *tok;
1358 int i;
1360 tok = isl_stream_next_token(s);
1361 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1362 isl_stream_error(s, tok, "expecting index");
1363 if (tok)
1364 isl_stream_push_token(s, tok);
1365 goto error;
1367 i = isl_int_get_si(tok->u.v);
1368 isl_token_free(tok);
1369 isl_assert(s->ctx, i < list->n, goto error);
1370 if (isl_stream_eat(s, ']'))
1371 goto error;
1373 return obj_at(obj, i);
1374 error:
1375 free_obj(obj);
1376 obj.type = isl_obj_none;
1377 obj.v = NULL;
1378 return obj;
1381 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1382 struct isl_hash_table *table)
1384 struct isl_obj obj;
1386 obj = read_expr(s, table);
1387 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1388 is_subtype(obj, isl_obj_union_map), goto error);
1390 if (obj.type == isl_obj_list) {
1391 struct isl_list *list = obj.v;
1392 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1393 obj = obj_at(obj, 0);
1395 if (obj.type == isl_obj_set)
1396 obj = convert(s->ctx, obj, isl_obj_union_set);
1397 else if (obj.type == isl_obj_map)
1398 obj = convert(s->ctx, obj, isl_obj_union_map);
1399 if (obj.type == isl_obj_union_set) {
1400 obj.v = isl_union_set_apply(obj.v, umap);
1401 } else
1402 obj.v = isl_union_map_apply_range(obj.v, umap);
1403 if (!obj.v)
1404 goto error2;
1406 if (isl_stream_eat(s, ')'))
1407 goto error2;
1409 return obj;
1410 error:
1411 isl_union_map_free(umap);
1412 error2:
1413 free_obj(obj);
1414 obj.type = isl_obj_none;
1415 obj.v = NULL;
1416 return obj;
1419 static struct isl_obj apply_fun_set(struct isl_obj obj,
1420 __isl_take isl_union_set *uset)
1422 if (obj.type == isl_obj_union_pw_qpolynomial) {
1423 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1424 } else {
1425 obj.type = isl_obj_list;
1426 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1428 return obj;
1431 static struct isl_obj apply_fun_map(struct isl_obj obj,
1432 __isl_take isl_union_map *umap)
1434 if (obj.type == isl_obj_union_pw_qpolynomial) {
1435 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1436 } else {
1437 obj.type = isl_obj_list;
1438 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1440 return obj;
1443 static struct isl_obj apply_fun(struct isl_stream *s,
1444 struct isl_obj obj, struct isl_hash_table *table)
1446 struct isl_obj arg;
1448 arg = read_expr(s, table);
1449 if (!is_subtype(arg, isl_obj_union_map) &&
1450 !is_subtype(arg, isl_obj_union_set))
1451 isl_die(s->ctx, isl_error_invalid,
1452 "expecting set of map argument", goto error);
1454 if (arg.type == isl_obj_list) {
1455 struct isl_list *list = arg.v;
1456 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1457 arg = obj_at(arg, 0);
1459 if (arg.type == isl_obj_set)
1460 arg = convert(s->ctx, arg, isl_obj_union_set);
1461 else if (arg.type == isl_obj_map)
1462 arg = convert(s->ctx, arg, isl_obj_union_map);
1463 if (arg.type == isl_obj_union_set)
1464 obj = apply_fun_set(obj, arg.v);
1465 else
1466 obj = apply_fun_map(obj, arg.v);
1467 if (!obj.v)
1468 goto error2;
1470 if (isl_stream_eat(s, ')'))
1471 goto error2;
1473 return obj;
1474 error:
1475 free_obj(arg);
1476 error2:
1477 free_obj(obj);
1478 obj.type = isl_obj_none;
1479 obj.v = NULL;
1480 return obj;
1483 struct add_vertex_data {
1484 struct isl_list *list;
1485 int i;
1488 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1490 struct add_vertex_data *data = (struct add_vertex_data *)user;
1491 isl_basic_set *expr;
1493 expr = isl_vertex_get_expr(vertex);
1495 data->list->obj[data->i].type = isl_obj_set;
1496 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1497 data->i++;
1499 isl_vertex_free(vertex);
1501 return 0;
1504 static int set_vertices(__isl_take isl_set *set, void *user)
1506 isl_ctx *ctx;
1507 isl_basic_set *hull;
1508 isl_vertices *vertices = NULL;
1509 struct isl_list *list = NULL;
1510 int r;
1511 struct add_vertex_data *data = (struct add_vertex_data *)user;
1513 set = isl_set_remove_divs(set);
1514 hull = isl_set_convex_hull(set);
1515 vertices = isl_basic_set_compute_vertices(hull);
1516 isl_basic_set_free(hull);
1518 list = data->list;
1520 ctx = isl_vertices_get_ctx(vertices);
1521 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1522 if (!data->list)
1523 goto error;
1525 data->i = 0;
1526 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1528 data->list = isl_list_concat(list, data->list);
1530 isl_vertices_free(vertices);
1532 return r;
1533 error:
1534 data->list = list;
1535 isl_vertices_free(vertices);
1536 return -1;
1539 static struct isl_obj vertices(struct isl_stream *s,
1540 struct isl_hash_table *table)
1542 isl_ctx *ctx;
1543 struct isl_obj obj;
1544 struct isl_list *list = NULL;
1545 isl_union_set *uset;
1546 struct add_vertex_data data = { NULL };
1548 obj = read_expr(s, table);
1549 obj = convert(s->ctx, obj, isl_obj_union_set);
1550 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1551 uset = obj.v;
1552 obj.v = NULL;
1554 ctx = isl_union_set_get_ctx(uset);
1555 list = isl_list_alloc(ctx, 0);
1556 if (!list)
1557 goto error;
1559 data.list = list;
1561 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1562 goto error;
1564 isl_union_set_free(uset);
1566 obj.type = isl_obj_list;
1567 obj.v = data.list;
1569 return obj;
1570 error:
1571 isl_union_set_free(uset);
1572 isl_list_free(data.list);
1573 free_obj(obj);
1574 obj.type = isl_obj_none;
1575 obj.v = NULL;
1576 return obj;
1579 static struct isl_obj type_of(struct isl_stream *s,
1580 struct isl_hash_table *table)
1582 isl_ctx *ctx;
1583 struct isl_obj obj;
1584 const char *type = "unknown";
1586 obj = read_expr(s, table);
1588 if (obj.type == isl_obj_map ||
1589 obj.type == isl_obj_union_map)
1590 type = "map";
1591 if (obj.type == isl_obj_set ||
1592 obj.type == isl_obj_union_set)
1593 type = "set";
1594 if (obj.type == isl_obj_pw_qpolynomial ||
1595 obj.type == isl_obj_union_pw_qpolynomial)
1596 type = "piecewise quasipolynomial";
1597 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1598 obj.type == isl_obj_union_pw_qpolynomial_fold)
1599 type = "piecewise quasipolynomial fold";
1600 if (obj.type == isl_obj_list)
1601 type = "list";
1602 if (obj.type == isl_obj_bool)
1603 type = "boolean";
1604 if (obj.type == isl_obj_str)
1605 type = "string";
1606 if (obj.type == isl_obj_int)
1607 type = "int";
1609 free_obj(obj);
1610 obj.type = isl_obj_str;
1611 obj.v = isl_str_from_string(s->ctx, strdup(type));
1613 return obj;
1616 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1617 struct isl_hash_table *table)
1619 struct isl_obj obj;
1621 obj = read_obj(s, table);
1622 obj = convert(s->ctx, obj, isl_obj_union_set);
1623 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1624 return obj.v;
1625 error:
1626 free_obj(obj);
1627 return NULL;
1630 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1631 struct isl_hash_table *table)
1633 struct isl_obj obj;
1635 obj = read_obj(s, table);
1636 obj = convert(s->ctx, obj, isl_obj_union_map);
1637 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1638 return obj.v;
1639 error:
1640 free_obj(obj);
1641 return NULL;
1644 static struct isl_obj last_any(struct isl_stream *s,
1645 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1646 __isl_take isl_union_map *may_source)
1648 struct isl_obj obj = { isl_obj_none, NULL };
1649 isl_union_map *sink = NULL;
1650 isl_union_map *schedule = NULL;
1651 isl_union_map *may_dep;
1652 isl_union_map *must_dep;
1654 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1655 goto error;
1657 sink = read_map(s, table);
1658 if (!sink)
1659 goto error;
1661 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1662 goto error;
1664 schedule = read_map(s, table);
1665 if (!schedule)
1666 goto error;
1668 if (isl_union_map_compute_flow(sink, must_source, may_source,
1669 schedule, &must_dep, &may_dep,
1670 NULL, NULL) < 0)
1671 return obj;
1673 obj.type = isl_obj_union_map;
1674 obj.v = isl_union_map_union(must_dep, may_dep);
1676 return obj;
1677 error:
1678 isl_union_map_free(may_source);
1679 isl_union_map_free(must_source);
1680 isl_union_map_free(sink);
1681 isl_union_map_free(schedule);
1682 free_obj(obj);
1683 obj.type = isl_obj_none;
1684 obj.v = NULL;
1685 return obj;
1688 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1690 struct isl_obj obj = { isl_obj_none, NULL };
1691 isl_union_map *must_source = NULL;
1692 isl_union_map *may_source = NULL;
1693 isl_union_map *sink = NULL;
1694 isl_union_map *schedule = NULL;
1695 isl_union_map *may_dep;
1697 may_source = read_map(s, table);
1698 if (!may_source)
1699 goto error;
1701 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1702 must_source = read_map(s, table);
1703 if (!must_source)
1704 goto error;
1705 return last_any(s, table, must_source, may_source);
1708 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1709 goto error;
1711 sink = read_map(s, table);
1712 if (!sink)
1713 goto error;
1715 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1716 goto error;
1718 schedule = read_map(s, table);
1719 if (!schedule)
1720 goto error;
1722 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1723 if (isl_union_map_compute_flow(sink, must_source, may_source,
1724 schedule, NULL, &may_dep,
1725 NULL, NULL) < 0)
1726 return obj;
1728 obj.type = isl_obj_union_map;
1729 obj.v = may_dep;
1731 return obj;
1732 error:
1733 isl_union_map_free(may_source);
1734 isl_union_map_free(must_source);
1735 isl_union_map_free(sink);
1736 isl_union_map_free(schedule);
1737 free_obj(obj);
1738 obj.type = isl_obj_none;
1739 obj.v = NULL;
1740 return obj;
1743 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1745 struct isl_obj obj = { isl_obj_none, NULL };
1746 struct isl_list *list = NULL;
1747 isl_union_map *must_source = NULL;
1748 isl_union_map *may_source = NULL;
1749 isl_union_map *sink = NULL;
1750 isl_union_map *schedule = NULL;
1751 isl_union_map *must_dep;
1752 isl_union_map *must_no_source;
1754 must_source = read_map(s, table);
1755 if (!must_source)
1756 goto error;
1758 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1759 may_source = read_map(s, table);
1760 if (!may_source)
1761 goto error;
1762 return last_any(s, table, must_source, may_source);
1765 list = isl_list_alloc(s->ctx, 2);
1766 if (!list)
1767 goto error;
1769 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1770 goto error;
1772 sink = read_map(s, table);
1773 if (!sink)
1774 goto error;
1776 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1777 goto error;
1779 schedule = read_map(s, table);
1780 if (!schedule)
1781 goto error;
1783 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1784 if (isl_union_map_compute_flow(sink, must_source, may_source,
1785 schedule, &must_dep, NULL,
1786 &must_no_source, NULL) < 0) {
1787 isl_list_free(list);
1788 return obj;
1791 list->obj[0].type = isl_obj_union_map;
1792 list->obj[0].v = must_dep;
1793 list->obj[1].type = isl_obj_union_map;
1794 list->obj[1].v = must_no_source;
1796 obj.v = list;
1797 obj.type = isl_obj_list;
1799 return obj;
1800 error:
1801 isl_list_free(list);
1802 isl_union_map_free(may_source);
1803 isl_union_map_free(must_source);
1804 isl_union_map_free(sink);
1805 isl_union_map_free(schedule);
1806 free_obj(obj);
1807 obj.type = isl_obj_none;
1808 obj.v = NULL;
1809 return obj;
1812 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1813 struct isl_hash_table *table)
1815 isl_union_set *domain;
1816 isl_union_map *validity;
1817 isl_union_map *proximity;
1819 domain = read_set(s, table);
1820 if (!domain)
1821 return NULL;
1823 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1824 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1826 for (;;) {
1827 isl_union_map *umap;
1828 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1829 umap = read_map(s, table);
1830 validity = isl_union_map_union(validity, umap);
1831 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1832 umap = read_map(s, table);
1833 proximity = isl_union_map_union(proximity, umap);
1834 } else
1835 break;
1838 return isl_union_set_compute_schedule(domain, validity, proximity);
1841 static struct isl_obj schedule(struct isl_stream *s,
1842 struct isl_hash_table *table)
1844 struct isl_obj obj = { isl_obj_none, NULL };
1845 isl_schedule *schedule;
1847 schedule = get_schedule(s, table);
1849 obj.v = isl_schedule_get_map(schedule);
1850 obj.type = isl_obj_union_map;
1852 isl_schedule_free(schedule);
1854 return obj;
1857 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1859 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1861 struct isl_obj obj = { isl_obj_none, NULL };
1862 isl_ctx *ctx = isl_band_get_ctx(band);
1863 struct isl_list *list;
1865 list = isl_list_alloc(ctx, 2);
1866 if (!list)
1867 goto error;
1869 obj.v = list;
1870 obj.type = isl_obj_list;
1872 list->obj[0].type = isl_obj_union_map;
1873 list->obj[0].v = isl_band_get_partial_schedule(band);
1875 if (isl_band_has_children(band)) {
1876 isl_band_list *children;
1878 children = isl_band_get_children(band);
1879 list->obj[1] = band_list_to_obj_list(children);
1880 } else {
1881 list->obj[1].type = isl_obj_list;
1882 list->obj[1].v = isl_list_alloc(ctx, 0);
1885 if (!list->obj[0].v || !list->obj[1].v)
1886 goto error;
1888 isl_band_free(band);
1890 return obj;
1891 error:
1892 isl_band_free(band);
1893 free_obj(obj);
1894 obj.type = isl_obj_none;
1895 obj.v = NULL;
1896 return obj;
1899 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1901 struct isl_obj obj = { isl_obj_none, NULL };
1902 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1903 struct isl_list *list;
1904 int i, n;
1906 n = isl_band_list_n_band(bands);
1907 list = isl_list_alloc(ctx, n);
1908 if (!list)
1909 goto error;
1911 obj.v = list;
1912 obj.type = isl_obj_list;
1914 for (i = 0; i < n; ++i) {
1915 isl_band *band;
1917 band = isl_band_list_get_band(bands, i);
1918 list->obj[i] = band_to_obj_list(band);
1919 if (!list->obj[i].v)
1920 goto error;
1923 isl_band_list_free(bands);
1925 return obj;
1926 error:
1927 isl_band_list_free(bands);
1928 free_obj(obj);
1929 obj.type = isl_obj_none;
1930 obj.v = NULL;
1931 return obj;
1934 static struct isl_obj schedule_forest(struct isl_stream *s,
1935 struct isl_hash_table *table)
1937 struct isl_obj obj = { isl_obj_none, NULL };
1938 isl_schedule *schedule;
1939 isl_band_list *roots;
1941 schedule = get_schedule(s, table);
1942 if (!schedule)
1943 return obj;
1945 roots = isl_schedule_get_band_forest(schedule);
1946 isl_schedule_free(schedule);
1948 return band_list_to_obj_list(roots);
1951 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1953 struct isl_token *tok;
1955 if (isl_stream_eat_if_available(s, '+'))
1956 return transitive_closure(s->ctx, obj);
1958 tok = isl_stream_next_token(s);
1959 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1960 isl_stream_error(s, tok, "expecting -1");
1961 if (tok)
1962 isl_stream_push_token(s, tok);
1963 goto error;
1965 isl_token_free(tok);
1966 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1967 if (obj.type != isl_obj_union_map)
1968 obj = convert(s->ctx, obj, isl_obj_union_map);
1970 obj.v = isl_union_map_reverse(obj.v);
1971 if (!obj.v)
1972 goto error;
1974 return obj;
1975 error:
1976 free_obj(obj);
1977 obj.type = isl_obj_none;
1978 obj.v = NULL;
1979 return obj;
1982 static struct isl_obj check_assert(struct isl_stream *s,
1983 struct isl_hash_table *table)
1985 struct isl_obj obj;
1987 obj = read_expr(s, table);
1988 if (obj.type != isl_obj_bool)
1989 isl_die(s->ctx, isl_error_invalid,
1990 "expecting boolean expression", goto error);
1991 if (obj.v != &isl_bool_true)
1992 isl_die(s->ctx, isl_error_unknown,
1993 "assertion failed", abort());
1994 error:
1995 free_obj(obj);
1996 obj.type = isl_obj_none;
1997 obj.v = NULL;
1998 return obj;
2001 static struct isl_obj read_from_file(struct isl_stream *s)
2003 struct isl_obj obj;
2004 struct isl_token *tok;
2005 struct isl_stream *s_file;
2006 struct iscc_options *options;
2007 FILE *file;
2009 tok = isl_stream_next_token(s);
2010 if (!tok || tok->type != ISL_TOKEN_STRING) {
2011 isl_stream_error(s, tok, "expecting filename");
2012 isl_token_free(tok);
2013 goto error;
2016 options = isl_ctx_peek_iscc_options(s->ctx);
2017 if (!options || !options->io) {
2018 isl_token_free(tok);
2019 isl_die(s->ctx, isl_error_invalid,
2020 "read operation not allowed", goto error);
2023 file = fopen(tok->u.s, "r");
2024 isl_token_free(tok);
2025 isl_assert(s->ctx, file, goto error);
2027 s_file = isl_stream_new_file(s->ctx, file);
2028 if (!s_file) {
2029 fclose(file);
2030 goto error;
2033 obj = isl_stream_read_obj(s_file);
2035 isl_stream_free(s_file);
2036 fclose(file);
2038 return obj;
2039 error:
2040 obj.type = isl_obj_none;
2041 obj.v = NULL;
2042 return obj;
2045 static struct isl_obj write_to_file(struct isl_stream *s,
2046 struct isl_hash_table *table)
2048 struct isl_obj obj;
2049 struct isl_token *tok;
2050 struct isl_stream *s_file;
2051 struct iscc_options *options;
2052 FILE *file;
2053 isl_printer *p;
2055 tok = isl_stream_next_token(s);
2056 if (!tok || tok->type != ISL_TOKEN_STRING) {
2057 isl_stream_error(s, tok, "expecting filename");
2058 isl_token_free(tok);
2059 goto error;
2062 obj = read_expr(s, table);
2064 options = isl_ctx_peek_iscc_options(s->ctx);
2065 if (!options || !options->io) {
2066 isl_token_free(tok);
2067 isl_die(s->ctx, isl_error_invalid,
2068 "write operation not allowed", goto error);
2071 file = fopen(tok->u.s, "w");
2072 isl_token_free(tok);
2073 if (!file)
2074 isl_die(s->ctx, isl_error_unknown,
2075 "could not open file for writing", goto error);
2077 p = isl_printer_to_file(s->ctx, file);
2078 p = isl_printer_set_output_format(p, options->format);
2079 p = obj.type->print(p, obj.v);
2080 p = isl_printer_end_line(p);
2081 isl_printer_free(p);
2083 fclose(file);
2084 error:
2085 free_obj(obj);
2086 obj.type = isl_obj_none;
2087 obj.v = NULL;
2088 return obj;
2091 static struct isl_obj read_string_if_available(struct isl_stream *s)
2093 struct isl_token *tok;
2094 struct isl_obj obj = { isl_obj_none, NULL };
2096 tok = isl_stream_next_token(s);
2097 if (!tok)
2098 return obj;
2099 if (tok->type == ISL_TOKEN_STRING) {
2100 isl_str *str;
2101 str = isl_str_alloc(s->ctx);
2102 if (!str)
2103 goto error;
2104 str->s = strdup(tok->u.s);
2105 isl_token_free(tok);
2106 obj.v = str;
2107 obj.type = isl_obj_str;
2108 } else
2109 isl_stream_push_token(s, tok);
2110 return obj;
2111 error:
2112 isl_token_free(tok);
2113 return obj;
2116 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2118 struct isl_token *tok;
2119 struct isl_obj obj = { isl_obj_none, NULL };
2121 tok = isl_stream_next_token(s);
2122 if (!tok)
2123 return obj;
2124 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2125 int is_true = tok->type == ISL_TOKEN_TRUE;
2126 isl_token_free(tok);
2127 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2128 obj.type = isl_obj_bool;
2129 } else
2130 isl_stream_push_token(s, tok);
2131 return obj;
2132 error:
2133 isl_token_free(tok);
2134 return obj;
2137 static __isl_give char *read_ident(struct isl_stream *s)
2139 char *name;
2140 struct isl_token *tok, *tok2;
2142 name = isl_stream_read_ident_if_available(s);
2143 if (name)
2144 return name;
2146 tok = isl_stream_next_token(s);
2147 if (!tok)
2148 return NULL;
2149 if (tok->type != '$') {
2150 isl_stream_push_token(s, tok);
2151 return NULL;
2153 tok2 = isl_stream_next_token(s);
2154 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2155 if (tok2)
2156 isl_stream_push_token(s, tok2);
2157 isl_stream_push_token(s, tok);
2158 return NULL;
2161 name = isl_int_get_str(tok2->u.v);
2162 isl_token_free(tok);
2163 isl_token_free(tok2);
2165 return name;
2168 static struct isl_obj read_list(struct isl_stream *s,
2169 struct isl_hash_table *table, struct isl_obj obj)
2171 struct isl_list *list;
2173 list = isl_list_alloc(s->ctx, 2);
2174 if (!list)
2175 goto error;
2176 list->obj[0] = obj;
2177 list->obj[1] = read_obj(s, table);
2178 obj.v = list;
2179 obj.type = isl_obj_list;
2181 if (!list->obj[1].v)
2182 goto error;
2184 while (isl_stream_eat_if_available(s, ',')) {
2185 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2186 if (!obj.v)
2187 goto error;
2190 return obj;
2191 error:
2192 free_obj(obj);
2193 obj.type = isl_obj_none;
2194 obj.v = NULL;
2195 return obj;
2198 static struct isl_obj read_obj(struct isl_stream *s,
2199 struct isl_hash_table *table)
2201 struct isl_obj obj = { isl_obj_none, NULL };
2202 char *name = NULL;
2203 struct isc_un_op *op = NULL;
2205 obj = read_string_if_available(s);
2206 if (obj.v)
2207 return obj;
2208 obj = read_bool_if_available(s);
2209 if (obj.v)
2210 return obj;
2211 if (isl_stream_eat_if_available(s, '(')) {
2212 if (isl_stream_next_token_is(s, ')')) {
2213 obj.type = isl_obj_list;
2214 obj.v = isl_list_alloc(s->ctx, 0);
2215 } else {
2216 obj = read_expr(s, table);
2217 if (obj.v && isl_stream_eat_if_available(s, ','))
2218 obj = read_list(s, table, obj);
2220 if (!obj.v || isl_stream_eat(s, ')'))
2221 goto error;
2222 } else {
2223 op = read_prefix_un_op_if_available(s);
2224 if (op)
2225 return read_un_op_expr(s, table, op);
2227 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2228 return check_assert(s, table);
2229 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2230 return read_from_file(s);
2231 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2232 return write_to_file(s, table);
2233 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2234 return vertices(s, table);
2235 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2236 return any(s, table);
2237 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2238 return last(s, table);
2239 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2240 return schedule(s, table);
2241 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2242 return schedule_forest(s, table);
2243 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2244 return type_of(s, table);
2246 name = read_ident(s);
2247 if (name)
2248 obj = stored_obj(s->ctx, table, name);
2249 else
2250 obj = isl_stream_read_obj(s);
2251 if (!obj.v)
2252 goto error;
2255 if (isl_stream_eat_if_available(s, '^'))
2256 obj = power(s, obj);
2257 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2258 obj = obj_at_index(s, obj);
2259 else if (is_subtype(obj, isl_obj_union_map) &&
2260 isl_stream_eat_if_available(s, '(')) {
2261 obj = convert(s->ctx, obj, isl_obj_union_map);
2262 obj = apply(s, obj.v, table);
2263 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2264 isl_stream_eat_if_available(s, '(')) {
2265 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2266 obj = apply_fun(s, obj, table);
2267 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2268 isl_stream_eat_if_available(s, '(')) {
2269 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2270 obj = apply_fun(s, obj, table);
2273 return obj;
2274 error:
2275 free_obj(obj);
2276 obj.type = isl_obj_none;
2277 obj.v = NULL;
2278 return obj;
2281 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2282 struct isl_obj lhs, struct isl_obj rhs)
2284 int i;
2286 for (i = 0; ; ++i) {
2287 if (!bin_ops[i].op)
2288 break;
2289 if (bin_ops[i].op != like->op)
2290 continue;
2291 if (!is_subtype(lhs, bin_ops[i].lhs))
2292 continue;
2293 if (!is_subtype(rhs, bin_ops[i].rhs))
2294 continue;
2296 return &bin_ops[i];
2299 for (i = 0; ; ++i) {
2300 if (!named_bin_ops[i].name)
2301 break;
2302 if (named_bin_ops[i].op.op != like->op)
2303 continue;
2304 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2305 continue;
2306 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2307 continue;
2309 return &named_bin_ops[i].op;
2312 return NULL;
2315 static int next_is_neg_int(struct isl_stream *s)
2317 struct isl_token *tok;
2318 int ret;
2320 tok = isl_stream_next_token(s);
2321 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2322 isl_stream_push_token(s, tok);
2324 return ret;
2327 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2328 struct isl_obj lhs, struct isl_obj rhs)
2330 struct isl_obj obj;
2332 lhs = convert(ctx, lhs, op->lhs);
2333 rhs = convert(ctx, rhs, op->rhs);
2334 if (op->res != isl_obj_bool)
2335 obj.v = op->o.fn(lhs.v, rhs.v);
2336 else {
2337 int res = op->o.test(lhs.v, rhs.v);
2338 free_obj(lhs);
2339 free_obj(rhs);
2340 obj.v = isl_bool_from_int(res);
2342 obj.type = op->res;
2344 return obj;
2347 static struct isl_obj read_expr(struct isl_stream *s,
2348 struct isl_hash_table *table)
2350 struct isl_obj obj = { isl_obj_none, NULL };
2351 struct isl_obj right_obj = { isl_obj_none, NULL };
2353 obj = read_obj(s, table);
2354 for (; obj.v;) {
2355 struct isc_bin_op *op = NULL;
2357 op = read_bin_op_if_available(s, obj);
2358 if (!op)
2359 break;
2361 right_obj = read_obj(s, table);
2363 op = find_matching_bin_op(op, obj, right_obj);
2365 if (!op)
2366 isl_die(s->ctx, isl_error_invalid,
2367 "no such binary operator defined on given operands",
2368 goto error);
2370 obj = call_bin_op(s->ctx, op, obj, right_obj);
2373 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2374 right_obj = read_obj(s, table);
2375 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2378 return obj;
2379 error:
2380 free_obj(right_obj);
2381 free_obj(obj);
2382 obj.type = isl_obj_none;
2383 obj.v = NULL;
2384 return obj;
2387 static __isl_give isl_printer *source_file(struct isl_stream *s,
2388 struct isl_hash_table *table, __isl_take isl_printer *p);
2390 static __isl_give isl_printer *read_line(struct isl_stream *s,
2391 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2393 struct isl_obj obj = { isl_obj_none, NULL };
2394 char *lhs = NULL;
2395 int assign = 0;
2396 int only_print = 0;
2397 struct isc_bin_op *op = NULL;
2398 char buf[30];
2400 if (!p)
2401 return NULL;
2402 if (isl_stream_is_empty(s))
2403 return p;
2405 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2406 return source_file(s, table, p);
2408 assign = is_assign(s);
2409 if (assign) {
2410 lhs = isl_stream_read_ident_if_available(s);
2411 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2412 goto error;
2413 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2414 only_print = 1;
2415 else if (!tty)
2416 only_print = 1;
2418 obj = read_expr(s, table);
2419 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2420 fprintf(stderr, "Interrupted\n");
2421 isl_ctx_reset_error(s->ctx);
2423 if (isl_stream_eat(s, ';'))
2424 goto error;
2426 if (only_print) {
2427 if (obj.type != isl_obj_none && obj.v != NULL) {
2428 p = obj.type->print(p, obj.v);
2429 p = isl_printer_end_line(p);
2431 free_obj(obj);
2432 return p;
2434 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2435 static int count = 0;
2436 snprintf(buf, sizeof(buf), "$%d", count++);
2437 lhs = strdup(buf + 1);
2439 p = isl_printer_print_str(p, buf);
2440 p = isl_printer_print_str(p, " := ");
2441 p = obj.type->print(p, obj.v);
2442 p = isl_printer_end_line(p);
2444 if (lhs && do_assign(s->ctx, table, lhs, obj))
2445 return p;
2447 return p;
2448 error:
2449 isl_stream_flush_tokens(s);
2450 isl_stream_skip_line(s);
2451 free(lhs);
2452 free_obj(obj);
2453 return p;
2456 int free_cb(void **entry, void *user)
2458 struct isl_named_obj *named = *entry;
2460 free_obj(named->obj);
2461 free(named->name);
2462 free(named);
2464 return 0;
2467 static void register_named_ops(struct isl_stream *s)
2469 int i;
2471 for (i = 0; i < ISCC_N_OP; ++i) {
2472 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2473 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2476 for (i = 0; ; ++i) {
2477 if (!named_un_ops[i].name)
2478 break;
2479 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2480 named_un_ops[i].name);
2481 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2484 for (i = 0; ; ++i) {
2485 if (!named_bin_ops[i].name)
2486 break;
2487 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2488 named_bin_ops[i].name);
2489 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2493 static __isl_give isl_printer *source_file(struct isl_stream *s,
2494 struct isl_hash_table *table, __isl_take isl_printer *p)
2496 struct isl_token *tok;
2497 struct isl_stream *s_file;
2498 FILE *file;
2500 tok = isl_stream_next_token(s);
2501 if (!tok || tok->type != ISL_TOKEN_STRING) {
2502 isl_stream_error(s, tok, "expecting filename");
2503 isl_token_free(tok);
2504 return p;
2507 file = fopen(tok->u.s, "r");
2508 isl_token_free(tok);
2509 isl_assert(s->ctx, file, return p);
2511 s_file = isl_stream_new_file(s->ctx, file);
2512 if (!s_file) {
2513 fclose(file);
2514 return p;
2517 register_named_ops(s_file);
2519 while (!s_file->eof)
2520 p = read_line(s_file, table, p, 0);
2522 isl_stream_free(s_file);
2523 fclose(file);
2525 isl_stream_eat(s, ';');
2527 return p;
2530 int main(int argc, char **argv)
2532 struct isl_ctx *ctx;
2533 struct isl_stream *s;
2534 struct isl_hash_table *table;
2535 struct iscc_options *options;
2536 isl_printer *p;
2537 int tty = isatty(0);
2539 options = iscc_options_new_with_defaults();
2540 assert(options);
2541 argc = iscc_options_parse(options, argc, argv, ISL_ARG_ALL);
2543 ctx = isl_ctx_alloc_with_options(iscc_options_arg, options);
2544 s = isl_stream_new_file(ctx, stdin);
2545 assert(s);
2546 table = isl_hash_table_alloc(ctx, 10);
2547 assert(table);
2548 p = isl_printer_to_file(ctx, stdout);
2549 p = isl_printer_set_output_format(p, options->format);
2550 assert(p);
2552 register_named_ops(s);
2554 install_signal_handler(ctx);
2556 while (p && !s->eof) {
2557 isl_ctx_resume(ctx);
2558 p = read_line(s, table, p, tty);
2561 remove_signal_handler(ctx);
2563 isl_printer_free(p);
2564 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2565 isl_hash_table_free(ctx, table);
2566 isl_stream_free(s);
2567 isl_ctx_free(ctx);
2569 return 0;