update cloog for rename of LANGUAGE_C to CLOOG_LANGUAGE_C
[barvinok.git] / iscc.c
blob6b079b558f8a446599dcd3ae57c7d9c75decf125
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/obj.h>
7 #include <isl/stream.h>
8 #include <isl/vertices.h>
9 #include <isl/flow.h>
10 #include <isl/band.h>
11 #include <isl/schedule.h>
12 #include <isl_obj_list.h>
13 #include <isl_obj_str.h>
14 #include <barvinok/isl.h>
15 #include <barvinok/options.h>
16 #include "lattice_width.h"
18 #include "config.h"
20 #ifdef HAVE_SIGACTION
21 #include <signal.h>
23 static isl_ctx *main_ctx;
25 static void handler(int signum)
27 if (isl_ctx_aborted(main_ctx))
28 exit(EXIT_FAILURE);
29 isl_ctx_abort(main_ctx);
32 static struct sigaction sa_old;
34 static void install_signal_handler(isl_ctx *ctx)
36 struct sigaction sa;
38 main_ctx = ctx;
40 memset(&sa, 0, sizeof(struct sigaction));
41 sa.sa_handler = &handler;
42 sa.sa_flags = SA_RESTART;
43 sigaction(SIGINT, &sa, &sa_old);
46 static void remove_signal_handler(isl_ctx *ctx)
48 sigaction(SIGINT, &sa_old, NULL);
51 #else
53 static void install_signal_handler(isl_ctx *ctx)
57 static void remove_signal_handler(isl_ctx *ctx)
61 #endif
63 #ifdef HAVE_CLOOG
64 #include <cloog/isl/cloog.h>
65 #endif
67 #ifdef HAVE_PET
68 #include <pet.h>
69 #endif
71 static int isl_bool_false = 0;
72 static int isl_bool_true = 1;
73 static int isl_bool_error = -1;
75 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
76 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
77 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
78 ISCC_MINIMIZING, ISCC_RESPECTING,
79 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
80 ISCC_N_OP };
81 static const char *op_name[ISCC_N_OP] = {
82 [ISCC_ASSERT] = "assert",
83 [ISCC_READ] = "read",
84 [ISCC_WRITE] = "write",
85 [ISCC_PRINT] = "print",
86 [ISCC_SOURCE] = "source",
87 [ISCC_VERTICES] = "vertices",
88 [ISCC_LAST] = "last",
89 [ISCC_ANY] = "any",
90 [ISCC_BEFORE] = "before",
91 [ISCC_UNDER] = "under",
92 [ISCC_SCHEDULE] = "schedule",
93 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
94 [ISCC_MINIMIZING] = "minimizing",
95 [ISCC_RESPECTING] = "respecting",
96 [ISCC_TYPEOF] = "typeof"
98 static enum isl_token_type iscc_op[ISCC_N_OP];
100 struct isl_arg_choice iscc_format[] = {
101 {"isl", ISL_FORMAT_ISL},
102 {"omega", ISL_FORMAT_OMEGA},
103 {"polylib", ISL_FORMAT_POLYLIB},
104 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
105 {"latex", ISL_FORMAT_LATEX},
106 {"C", ISL_FORMAT_C},
110 struct iscc_options {
111 struct barvinok_options *barvinok;
112 unsigned format;
113 int io;
116 struct isl_arg iscc_options_arg[] = {
117 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", barvinok_options_arg,
118 "barvinok options")
119 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
120 iscc_format, ISL_FORMAT_ISL, "output format")
121 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
122 "allow read and write operations")
123 ISL_ARG_END
126 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_arg)
127 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_arg)
129 static void *isl_obj_bool_copy(void *v)
131 return v;
134 static void isl_obj_bool_free(void *v)
138 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
139 void *v)
141 if (v == &isl_bool_true)
142 return isl_printer_print_str(p, "True");
143 else if (v == &isl_bool_false)
144 return isl_printer_print_str(p, "False");
145 else
146 return isl_printer_print_str(p, "Error");
149 static void *isl_obj_bool_add(void *v1, void *v2)
151 return v1;
154 struct isl_obj_vtable isl_obj_bool_vtable = {
155 isl_obj_bool_copy,
156 isl_obj_bool_add,
157 isl_obj_bool_print,
158 isl_obj_bool_free
160 #define isl_obj_bool (&isl_obj_bool_vtable)
162 int *isl_bool_from_int(int res)
164 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
167 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
168 __isl_take isl_union_map *map2)
170 return isl_union_map_is_subset(map2, map1);
172 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
173 __isl_take isl_union_set *set2)
175 return isl_union_set_is_subset(set2, set1);
178 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
179 __isl_take isl_union_map *map2)
181 return isl_union_map_is_strict_subset(map2, map1);
183 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
184 __isl_take isl_union_set *set2)
186 return isl_union_set_is_strict_subset(set2, set1);
189 extern struct isl_obj_vtable isl_obj_list_vtable;
190 #define isl_obj_list (&isl_obj_list_vtable)
192 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
193 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
194 struct isc_bin_op {
195 enum isl_token_type op;
196 isl_obj_type lhs;
197 isl_obj_type rhs;
198 isl_obj_type res;
199 union {
200 isc_bin_op_fn fn;
201 isc_bin_test_fn test;
202 } o;
204 struct isc_named_bin_op {
205 char *name;
206 struct isc_bin_op op;
209 struct iscc_at {
210 isl_union_pw_qpolynomial *upwqp;
211 isl_union_pw_qpolynomial *res;
214 static int eval_at(__isl_take isl_point *pnt, void *user)
216 struct iscc_at *at = (struct iscc_at *) user;
217 isl_qpolynomial *qp;
218 isl_set *set;
220 set = isl_set_from_point(isl_point_copy(pnt));
221 qp = isl_union_pw_qpolynomial_eval(
222 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
224 at->res = isl_union_pw_qpolynomial_add(at->res,
225 isl_union_pw_qpolynomial_from_pw_qpolynomial(
226 isl_pw_qpolynomial_alloc(set, qp)));
228 return 0;
231 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
232 __isl_take isl_union_pw_qpolynomial *upwqp,
233 __isl_take isl_union_set *uset)
235 struct iscc_at at;
237 at.upwqp = upwqp;
238 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
240 isl_union_set_foreach_point(uset, eval_at, &at);
242 isl_union_pw_qpolynomial_free(upwqp);
243 isl_union_set_free(uset);
245 return at.res;
248 struct iscc_fold_at {
249 isl_union_pw_qpolynomial_fold *upwf;
250 isl_union_pw_qpolynomial *res;
253 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
255 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
256 isl_qpolynomial *qp;
257 isl_set *set;
259 set = isl_set_from_point(isl_point_copy(pnt));
260 qp = isl_union_pw_qpolynomial_fold_eval(
261 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
263 at->res = isl_union_pw_qpolynomial_add(at->res,
264 isl_union_pw_qpolynomial_from_pw_qpolynomial(
265 isl_pw_qpolynomial_alloc(set, qp)));
267 return 0;
270 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
271 __isl_take isl_union_pw_qpolynomial_fold *upwf,
272 __isl_take isl_union_set *uset)
274 struct iscc_fold_at at;
276 at.upwf = upwf;
277 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
279 isl_union_set_foreach_point(uset, eval_fold_at, &at);
281 isl_union_pw_qpolynomial_fold_free(upwf);
282 isl_union_set_free(uset);
284 return at.res;
287 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
288 __isl_take isl_union_pw_qpolynomial *upwqp,
289 __isl_take isl_union_pw_qpolynomial_fold *upwf)
291 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
292 upwqp);
295 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
296 __isl_take isl_union_map *umap,
297 __isl_take isl_union_pw_qpolynomial_fold *upwf)
299 isl_ctx *ctx;
300 struct isl_list *list;
301 int tight;
303 ctx = isl_union_map_get_ctx(umap);
304 list = isl_list_alloc(ctx, 2);
305 if (!list)
306 goto error2;
308 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
309 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
310 upwf, &tight);
311 list->obj[1].type = isl_obj_bool;
312 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
313 if (tight < 0 || !list->obj[0].v)
314 goto error;
316 return list;
317 error2:
318 isl_union_map_free(umap);
319 isl_union_pw_qpolynomial_fold_free(upwf);
320 error:
321 isl_list_free(list);
322 return NULL;
325 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
326 __isl_take isl_union_set *uset,
327 __isl_take isl_union_pw_qpolynomial_fold *upwf)
329 isl_ctx *ctx;
330 struct isl_list *list;
331 int tight;
333 ctx = isl_union_set_get_ctx(uset);
334 list = isl_list_alloc(ctx, 2);
335 if (!list)
336 goto error2;
338 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
339 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
340 upwf, &tight);
341 list->obj[1].type = isl_obj_bool;
342 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
343 if (tight < 0 || !list->obj[0].v)
344 goto error;
346 return list;
347 error2:
348 isl_union_set_free(uset);
349 isl_union_pw_qpolynomial_fold_free(upwf);
350 error:
351 isl_list_free(list);
352 return NULL;
355 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_int_mul(
356 __isl_take isl_union_pw_qpolynomial *upwqp, __isl_take isl_int_obj *i)
358 isl_int v;
360 if (!i)
361 goto error;
363 isl_int_init(v);
364 isl_int_obj_get_int(i, &v);
365 upwqp = isl_union_pw_qpolynomial_mul_isl_int(upwqp, v);
366 isl_int_clear(v);
368 isl_int_obj_free(i);
370 return upwqp;
371 error:
372 isl_union_pw_qpolynomial_free(upwqp);
373 return NULL;
376 static __isl_give isl_union_pw_qpolynomial *int_union_pw_qpolynomial_mul(
377 __isl_take isl_int_obj *i, __isl_take isl_union_pw_qpolynomial *upwqp)
379 return union_pw_qpolynomial_int_mul(upwqp, i);
382 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_fold_int_mul(
383 __isl_take isl_union_pw_qpolynomial_fold *upwf,
384 __isl_take isl_int_obj *i)
386 isl_int v;
388 if (!i)
389 goto error;
391 isl_int_init(v);
392 isl_int_obj_get_int(i, &v);
393 upwf = isl_union_pw_qpolynomial_fold_mul_isl_int(upwf, v);
394 isl_int_clear(v);
396 isl_int_obj_free(i);
398 return upwf;
399 error:
400 isl_union_pw_qpolynomial_fold_free(upwf);
401 return NULL;
404 static __isl_give isl_union_pw_qpolynomial_fold *int_union_pw_qpolynomial_fold_mul(
405 __isl_take isl_int_obj *i,
406 __isl_take isl_union_pw_qpolynomial_fold *upwf)
408 return union_pw_qpolynomial_fold_int_mul(upwf, i);
411 struct isc_bin_op bin_ops[] = {
412 { '+', isl_obj_int, isl_obj_int, isl_obj_int,
413 (isc_bin_op_fn) &isl_int_obj_add },
414 { '-', isl_obj_int, isl_obj_int, isl_obj_int,
415 (isc_bin_op_fn) &isl_int_obj_sub },
416 { '*', isl_obj_int, isl_obj_int, isl_obj_int,
417 (isc_bin_op_fn) &isl_int_obj_mul },
418 { '+', isl_obj_union_set, isl_obj_union_set,
419 isl_obj_union_set,
420 (isc_bin_op_fn) &isl_union_set_union },
421 { '+', isl_obj_union_map, isl_obj_union_map,
422 isl_obj_union_map,
423 (isc_bin_op_fn) &isl_union_map_union },
424 { '-', isl_obj_union_set, isl_obj_union_set,
425 isl_obj_union_set,
426 (isc_bin_op_fn) &isl_union_set_subtract },
427 { '-', isl_obj_union_map, isl_obj_union_map,
428 isl_obj_union_map,
429 (isc_bin_op_fn) &isl_union_map_subtract },
430 { '*', isl_obj_union_set, isl_obj_union_set,
431 isl_obj_union_set,
432 (isc_bin_op_fn) &isl_union_set_intersect },
433 { '*', isl_obj_union_map, isl_obj_union_map,
434 isl_obj_union_map,
435 (isc_bin_op_fn) &isl_union_map_intersect },
436 { '*', isl_obj_union_map, isl_obj_union_set,
437 isl_obj_union_map,
438 (isc_bin_op_fn) &isl_union_map_intersect_domain },
439 { '.', isl_obj_union_map, isl_obj_union_map,
440 isl_obj_union_map,
441 (isc_bin_op_fn) &isl_union_map_apply_range },
442 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
443 isl_obj_union_pw_qpolynomial,
444 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
445 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
446 isl_obj_list,
447 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
448 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
449 isl_obj_union_map,
450 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
451 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
452 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
453 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
454 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
455 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
456 isl_obj_bool,
457 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
458 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
459 isl_obj_bool,
460 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
461 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
462 isl_obj_bool,
463 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
464 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
465 isl_obj_bool,
466 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
467 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
468 isl_obj_bool,
469 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
470 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
471 isl_obj_bool,
472 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
473 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
474 isl_obj_bool,
475 { .test =
476 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
477 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
478 isl_obj_bool,
479 { .test =
480 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
481 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
482 isl_obj_union_map,
483 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
484 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
485 isl_obj_union_map,
486 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
487 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
488 isl_obj_union_map,
489 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
490 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
491 isl_obj_union_map,
492 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
493 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
494 isl_obj_union_map,
495 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
496 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
497 isl_obj_union_map,
498 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
499 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
500 isl_obj_union_map,
501 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
502 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
503 isl_obj_union_map,
504 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
505 { '.', isl_obj_union_pw_qpolynomial_fold,
506 isl_obj_union_pw_qpolynomial_fold,
507 isl_obj_union_pw_qpolynomial_fold,
508 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
509 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
510 isl_obj_union_pw_qpolynomial,
511 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
512 { '+', isl_obj_union_pw_qpolynomial,
513 isl_obj_union_pw_qpolynomial_fold,
514 isl_obj_union_pw_qpolynomial_fold,
515 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
516 { '+', isl_obj_union_pw_qpolynomial_fold,
517 isl_obj_union_pw_qpolynomial,
518 isl_obj_union_pw_qpolynomial_fold,
519 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
520 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
521 isl_obj_union_pw_qpolynomial,
522 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
523 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial,
524 isl_obj_union_pw_qpolynomial,
525 (isc_bin_op_fn) &int_union_pw_qpolynomial_mul },
526 { '*', isl_obj_union_pw_qpolynomial, isl_obj_int,
527 isl_obj_union_pw_qpolynomial,
528 (isc_bin_op_fn) &union_pw_qpolynomial_int_mul },
529 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial_fold,
530 isl_obj_union_pw_qpolynomial_fold,
531 (isc_bin_op_fn) &int_union_pw_qpolynomial_fold_mul },
532 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_int,
533 isl_obj_union_pw_qpolynomial_fold,
534 (isc_bin_op_fn) &union_pw_qpolynomial_fold_int_mul },
535 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
536 isl_obj_union_pw_qpolynomial,
537 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
538 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
539 isl_obj_union_pw_qpolynomial,
540 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
541 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
542 isl_obj_union_pw_qpolynomial_fold,
543 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
544 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
545 isl_obj_union_pw_qpolynomial,
546 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
547 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
548 isl_obj_union_pw_qpolynomial,
549 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
550 { '%', isl_obj_union_set, isl_obj_union_set,
551 isl_obj_union_set,
552 (isc_bin_op_fn) &isl_union_set_gist },
553 { '%', isl_obj_union_map, isl_obj_union_map,
554 isl_obj_union_map,
555 (isc_bin_op_fn) &isl_union_map_gist },
556 { '%', isl_obj_union_map, isl_obj_union_set,
557 isl_obj_union_map,
558 (isc_bin_op_fn) &isl_union_map_gist_domain },
559 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
560 isl_obj_union_pw_qpolynomial,
561 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
562 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
563 isl_obj_union_pw_qpolynomial_fold,
564 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
565 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
566 isl_obj_union_pw_qpolynomial, isl_obj_bool,
567 { .test = (isc_bin_test_fn)
568 &isl_union_pw_qpolynomial_plain_is_equal } },
569 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
570 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
571 { .test = (isc_bin_test_fn)
572 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
573 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
574 (isc_bin_op_fn) &isl_str_concat },
578 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
579 __isl_take isl_union_map *umap2)
581 return isl_union_map_apply_range(umap2, umap1);
584 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
585 __isl_take isl_union_pw_qpolynomial *upwqp,
586 __isl_take isl_union_map *umap)
588 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
591 static __isl_give struct isl_list *qpolynomial_fold_after_map(
592 __isl_take isl_union_pw_qpolynomial_fold *upwf,
593 __isl_take isl_union_map *umap)
595 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
598 struct isc_named_bin_op named_bin_ops[] = {
599 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
600 isl_obj_union_map,
601 (isc_bin_op_fn) &map_after_map } },
602 { "after", { -1, isl_obj_union_pw_qpolynomial,
603 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
604 (isc_bin_op_fn) &qpolynomial_after_map } },
605 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
606 isl_obj_union_map, isl_obj_list,
607 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
608 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
609 isl_obj_union_map,
610 (isc_bin_op_fn) &isl_union_map_apply_range } },
611 { "before", { -1, isl_obj_union_map,
612 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
613 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
614 { "before", { -1, isl_obj_union_map,
615 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
616 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
617 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
618 isl_obj_union_set,
619 (isc_bin_op_fn) &isl_union_set_product } },
620 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
621 isl_obj_union_map,
622 (isc_bin_op_fn) &isl_union_map_product } },
623 NULL
626 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
628 return isl_set_from_basic_set(isl_union_set_sample(uset));
631 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
633 return isl_map_from_basic_map(isl_union_map_sample(umap));
636 static __isl_give struct isl_list *union_map_power(
637 __isl_take isl_union_map *umap)
639 isl_ctx *ctx;
640 struct isl_list *list;
641 int exact;
643 ctx = isl_union_map_get_ctx(umap);
644 list = isl_list_alloc(ctx, 2);
645 if (!list)
646 goto error2;
648 list->obj[0].type = isl_obj_union_map;
649 list->obj[0].v = isl_union_map_power(umap, &exact);
650 list->obj[1].type = isl_obj_bool;
651 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
652 if (exact < 0 || !list->obj[0].v)
653 goto error;
655 return list;
656 error2:
657 isl_union_map_free(umap);
658 error:
659 isl_list_free(list);
660 return NULL;
663 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
664 __isl_take isl_union_pw_qpolynomial *upwqp)
666 isl_ctx *ctx;
667 struct isl_list *list;
668 int tight;
670 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
671 list = isl_list_alloc(ctx, 2);
672 if (!list)
673 goto error2;
675 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
676 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
677 isl_fold_max, &tight);
678 list->obj[1].type = isl_obj_bool;
679 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
680 if (tight < 0 || !list->obj[0].v)
681 goto error;
683 return list;
684 error2:
685 isl_union_pw_qpolynomial_free(upwqp);
686 error:
687 isl_list_free(list);
688 return NULL;
691 #ifdef HAVE_CLOOG
692 void *map_codegen(void *arg)
694 isl_space *dim;
695 isl_union_map *umap = (isl_union_map *)arg;
696 isl_ctx *ctx = isl_union_map_get_ctx(umap);
697 CloogState *state;
698 CloogOptions *options;
699 CloogDomain *context;
700 CloogUnionDomain *ud;
701 CloogInput *input;
702 struct clast_stmt *stmt;
704 state = cloog_isl_state_malloc(ctx);
705 options = cloog_options_malloc(state);
706 options->language = CLOOG_LANGUAGE_C;
707 options->strides = 1;
708 options->sh = 1;
710 ud = cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap));
712 dim = isl_union_map_get_space(umap);
713 context = cloog_domain_from_isl_set(isl_set_universe(dim));
715 input = cloog_input_alloc(context, ud);
717 stmt = cloog_clast_create_from_input(input, options);
718 clast_pprint(stdout, stmt, 0, options);
719 cloog_clast_free(stmt);
721 error:
722 cloog_options_free(options);
723 cloog_state_free(state);
724 isl_union_map_free(umap);
725 return NULL;
728 void *set_codegen(void *arg)
730 isl_space *dim;
731 isl_union_set *uset = (isl_union_set *)arg;
732 isl_set *set;
733 isl_ctx *ctx = isl_union_set_get_ctx(uset);
734 CloogState *state;
735 CloogOptions *options;
736 CloogDomain *context;
737 CloogUnionDomain *ud;
738 CloogInput *input;
739 struct clast_stmt *stmt;
741 if (isl_union_set_n_set(uset) > 1)
742 isl_die(ctx, isl_error_invalid,
743 "code generation for more than one domain "
744 "requires a schedule", goto error);
746 state = cloog_isl_state_malloc(ctx);
747 options = cloog_options_malloc(state);
748 options->language = CLOOG_LANGUAGE_C;
749 options->strides = 1;
750 options->sh = 1;
752 set = isl_set_from_union_set(isl_union_set_copy(uset));
753 ud = cloog_union_domain_from_isl_set(set);
755 dim = isl_union_set_get_space(uset);
756 context = cloog_domain_from_isl_set(isl_set_universe(dim));
758 input = cloog_input_alloc(context, ud);
760 stmt = cloog_clast_create_from_input(input, options);
761 clast_pprint(stdout, stmt, 0, options);
762 cloog_clast_free(stmt);
764 cloog_options_free(options);
765 cloog_state_free(state);
766 error:
767 isl_union_set_free(uset);
768 return NULL;
770 #endif
772 #ifdef HAVE_PET
773 static __isl_give isl_list *parse(__isl_take isl_str *str)
775 isl_ctx *ctx;
776 struct isl_list *list;
777 struct pet_scop *scop;
778 isl_union_map *sched, *reads, *writes;
779 isl_union_set *domain;
780 struct iscc_options *options;
782 if (!str)
783 return NULL;
784 ctx = str->ctx;
786 options = isl_ctx_peek_iscc_options(ctx);
787 if (!options || !options->io) {
788 isl_str_free(str);
789 isl_die(ctx, isl_error_invalid,
790 "parse_file operation not allowed", return NULL);
793 list = isl_list_alloc(ctx, 4);
794 if (!list)
795 goto error;
797 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL, 1);
798 domain = pet_scop_collect_domains(scop);
799 sched = pet_scop_collect_schedule(scop);
800 reads = pet_scop_collect_reads(scop);
801 writes = pet_scop_collect_writes(scop);
802 pet_scop_free(scop);
804 list->obj[0].type = isl_obj_union_set;
805 list->obj[0].v = domain;
806 list->obj[1].type = isl_obj_union_map;
807 list->obj[1].v = writes;
808 list->obj[2].type = isl_obj_union_map;
809 list->obj[2].v = reads;
810 list->obj[3].type = isl_obj_union_map;
811 list->obj[3].v = sched;
813 if (!list->obj[0].v || !list->obj[1].v ||
814 !list->obj[2].v || !list->obj[3].v)
815 goto error;
817 isl_str_free(str);
818 return list;
819 error:
820 isl_list_free(list);
821 isl_str_free(str);
822 return NULL;
824 #endif
826 static int add_point(__isl_take isl_point *pnt, void *user)
828 isl_union_set **scan = (isl_union_set **) user;
830 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
832 return 0;
835 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
837 isl_union_set *scan;
839 scan = isl_union_set_empty(isl_union_set_get_space(uset));
841 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
842 isl_union_set_free(scan);
843 return uset;
846 isl_union_set_free(uset);
847 return scan;
850 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
852 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
855 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
856 __isl_take isl_union_pw_qpolynomial *upwqp)
858 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
861 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
862 __isl_take isl_union_pw_qpolynomial *upwqp)
864 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
867 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
868 __isl_take isl_union_pw_qpolynomial *upwqp)
870 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
873 typedef void *(*isc_un_op_fn)(void *arg);
874 struct isc_un_op {
875 enum isl_token_type op;
876 isl_obj_type arg;
877 isl_obj_type res;
878 isc_un_op_fn fn;
880 struct isc_named_un_op {
881 char *name;
882 struct isc_un_op op;
884 struct isc_named_un_op named_un_ops[] = {
885 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
886 (isc_un_op_fn) &isl_union_map_affine_hull } },
887 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
888 (isc_un_op_fn) &isl_union_set_affine_hull } },
889 {"card", { -1, isl_obj_union_set,
890 isl_obj_union_pw_qpolynomial,
891 (isc_un_op_fn) &isl_union_set_card } },
892 {"card", { -1, isl_obj_union_map,
893 isl_obj_union_pw_qpolynomial,
894 (isc_un_op_fn) &isl_union_map_card } },
895 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
896 (isc_un_op_fn) &isl_union_set_coalesce } },
897 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
898 (isc_un_op_fn) &isl_union_map_coalesce } },
899 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
900 isl_obj_union_pw_qpolynomial,
901 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
902 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
903 isl_obj_union_pw_qpolynomial_fold,
904 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
905 #ifdef HAVE_CLOOG
906 {"codegen", { -1, isl_obj_union_set, isl_obj_none,
907 &set_codegen } },
908 {"codegen", { -1, isl_obj_union_map, isl_obj_none,
909 &map_codegen } },
910 #endif
911 {"coefficients", { -1, isl_obj_union_set,
912 isl_obj_union_set,
913 (isc_un_op_fn) &isl_union_set_coefficients } },
914 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
915 (isc_un_op_fn) &isl_union_set_solutions } },
916 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
917 (isc_un_op_fn) &isl_union_map_deltas } },
918 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
919 (isc_un_op_fn) &isl_union_map_deltas_map } },
920 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
921 (isc_un_op_fn) &isl_union_map_domain } },
922 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
923 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
924 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
925 isl_obj_union_set,
926 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
927 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
928 (isc_un_op_fn) &isl_union_map_domain } },
929 {"domain", { -1, isl_obj_union_pw_qpolynomial,
930 isl_obj_union_set,
931 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
932 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
933 isl_obj_union_set,
934 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
935 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
936 (isc_un_op_fn) &isl_union_map_domain_map } },
937 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
938 (isc_un_op_fn) &isl_union_map_range } },
939 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
940 (isc_un_op_fn) &isl_union_map_range } },
941 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
942 (isc_un_op_fn) &isl_union_map_range_map } },
943 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
944 (isc_un_op_fn) &isl_union_set_identity } },
945 {"lattice_width", { -1, isl_obj_union_set,
946 isl_obj_union_pw_qpolynomial,
947 (isc_un_op_fn) &isl_union_set_lattice_width } },
948 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
949 (isc_un_op_fn) &isl_union_map_lexmin } },
950 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
951 (isc_un_op_fn) &isl_union_map_lexmax } },
952 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
953 (isc_un_op_fn) &isl_union_set_lexmin } },
954 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
955 (isc_un_op_fn) &isl_union_set_lexmax } },
956 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
957 (isc_un_op_fn) &isl_union_set_lift } },
958 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
959 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
960 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
961 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
962 {"poly", { -1, isl_obj_union_pw_qpolynomial,
963 isl_obj_union_pw_qpolynomial,
964 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
965 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
966 isl_obj_union_pw_qpolynomial,
967 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
968 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
969 isl_obj_union_pw_qpolynomial,
970 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
971 #ifdef HAVE_PET
972 {"parse_file", { -1, isl_obj_str, isl_obj_list,
973 (isc_un_op_fn) &parse } },
974 #endif
975 {"pow", { -1, isl_obj_union_map, isl_obj_list,
976 (isc_un_op_fn) &union_map_power } },
977 {"sample", { -1, isl_obj_union_set, isl_obj_set,
978 (isc_un_op_fn) &union_set_sample } },
979 {"sample", { -1, isl_obj_union_map, isl_obj_map,
980 (isc_un_op_fn) &union_map_sample } },
981 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
982 (isc_un_op_fn) &union_set_scan } },
983 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
984 (isc_un_op_fn) &union_map_scan } },
985 {"sum", { -1, isl_obj_union_pw_qpolynomial,
986 isl_obj_union_pw_qpolynomial,
987 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
988 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
989 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
990 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
991 (isc_un_op_fn) &isl_union_set_unwrap } },
992 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
993 (isc_un_op_fn) &isl_union_map_wrap } },
994 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
995 (isc_un_op_fn) &isl_union_map_zip } },
996 NULL
999 struct isl_named_obj {
1000 char *name;
1001 struct isl_obj obj;
1004 static void free_obj(struct isl_obj obj)
1006 obj.type->free(obj.v);
1009 static int same_name(const void *entry, const void *val)
1011 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1013 return !strcmp(named->name, val);
1016 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1017 char *name, struct isl_obj obj)
1019 struct isl_hash_table_entry *entry;
1020 uint32_t name_hash;
1021 struct isl_named_obj *named;
1023 name_hash = isl_hash_string(isl_hash_init(), name);
1024 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1025 if (!entry)
1026 goto error;
1027 if (entry->data) {
1028 named = entry->data;
1029 free_obj(named->obj);
1030 free(name);
1031 } else {
1032 named = isl_alloc_type(ctx, struct isl_named_obj);
1033 if (!named)
1034 goto error;
1035 named->name = name;
1036 entry->data = named;
1038 named->obj = obj;
1040 return 0;
1041 error:
1042 free_obj(obj);
1043 free(name);
1044 return -1;
1047 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1048 struct isl_hash_table *table, char *name)
1050 struct isl_obj obj = { isl_obj_none, NULL };
1051 struct isl_hash_table_entry *entry;
1052 uint32_t name_hash;
1054 name_hash = isl_hash_string(isl_hash_init(), name);
1055 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1056 if (entry) {
1057 struct isl_named_obj *named;
1058 named = entry->data;
1059 obj = named->obj;
1060 } else if (isdigit(name[0]))
1061 fprintf(stderr, "unknown identifier '$%s'\n", name);
1062 else
1063 fprintf(stderr, "unknown identifier '%s'\n", name);
1065 free(name);
1066 obj.v = obj.type->copy(obj.v);
1067 return obj;
1070 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1072 if (obj.type == super)
1073 return 1;
1074 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1075 return 1;
1076 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1077 return 1;
1078 if (obj.type == isl_obj_pw_qpolynomial &&
1079 super == isl_obj_union_pw_qpolynomial)
1080 return 1;
1081 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1082 super == isl_obj_union_pw_qpolynomial_fold)
1083 return 1;
1084 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1085 return 1;
1086 if (obj.type == isl_obj_list) {
1087 struct isl_list *list = obj.v;
1088 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1089 return is_subtype(list->obj[0], super);
1091 if (super == isl_obj_str)
1092 return 1;
1093 return 0;
1096 static struct isl_obj obj_at(struct isl_obj obj, int i)
1098 struct isl_list *list = obj.v;
1100 obj = list->obj[i];
1101 obj.v = obj.type->copy(obj.v);
1103 isl_list_free(list);
1105 return obj;
1108 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1109 isl_obj_type type)
1111 if (obj.type == type)
1112 return obj;
1113 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1114 obj.type = isl_obj_union_map;
1115 obj.v = isl_union_map_from_map(obj.v);
1116 return obj;
1118 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1119 obj.type = isl_obj_union_set;
1120 obj.v = isl_union_set_from_set(obj.v);
1121 return obj;
1123 if (obj.type == isl_obj_pw_qpolynomial &&
1124 type == isl_obj_union_pw_qpolynomial) {
1125 obj.type = isl_obj_union_pw_qpolynomial;
1126 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1127 return obj;
1129 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1130 type == isl_obj_union_pw_qpolynomial_fold) {
1131 obj.type = isl_obj_union_pw_qpolynomial_fold;
1132 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1133 return obj;
1135 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1136 if (type == isl_obj_union_map) {
1137 obj.type = isl_obj_union_map;
1138 return obj;
1140 if (type == isl_obj_union_pw_qpolynomial) {
1141 isl_space *dim = isl_union_set_get_space(obj.v);
1142 isl_union_set_free(obj.v);
1143 obj.v = isl_union_pw_qpolynomial_zero(dim);
1144 obj.type = isl_obj_union_pw_qpolynomial;
1145 return obj;
1147 if (type == isl_obj_union_pw_qpolynomial_fold) {
1148 isl_space *dim = isl_union_set_get_space(obj.v);
1149 isl_union_set_free(obj.v);
1150 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1151 isl_fold_list);
1152 obj.type = isl_obj_union_pw_qpolynomial_fold;
1153 return obj;
1156 if (obj.type == isl_obj_list) {
1157 struct isl_list *list = obj.v;
1158 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1159 return convert(ctx, obj_at(obj, 0), type);
1161 if (type == isl_obj_str) {
1162 isl_str *str;
1163 isl_printer *p;
1164 char *s;
1166 p = isl_printer_to_str(ctx);
1167 if (!p)
1168 goto error;
1169 p = obj.type->print(p, obj.v);
1170 s = isl_printer_get_str(p);
1171 isl_printer_free(p);
1173 str = isl_str_from_string(ctx, s);
1174 if (!str)
1175 goto error;
1176 free_obj(obj);
1177 obj.v = str;
1178 obj.type = isl_obj_str;
1179 return obj;
1182 error:
1183 free_obj(obj);
1184 obj.type = isl_obj_none;
1185 obj.v = NULL;
1186 return obj;
1189 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1190 struct isl_obj lhs)
1192 int i;
1193 struct isl_token *tok;
1195 tok = isl_stream_next_token(s);
1196 if (!tok)
1197 return NULL;
1199 for (i = 0; ; ++i) {
1200 if (!bin_ops[i].op)
1201 break;
1202 if (bin_ops[i].op != tok->type)
1203 continue;
1204 if (!is_subtype(lhs, bin_ops[i].lhs))
1205 continue;
1207 isl_token_free(tok);
1208 return &bin_ops[i];
1211 for (i = 0; ; ++i) {
1212 if (!named_bin_ops[i].name)
1213 break;
1214 if (named_bin_ops[i].op.op != tok->type)
1215 continue;
1216 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1217 continue;
1219 isl_token_free(tok);
1220 return &named_bin_ops[i].op;
1223 isl_stream_push_token(s, tok);
1225 return NULL;
1228 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1230 int i;
1231 struct isl_token *tok;
1233 tok = isl_stream_next_token(s);
1234 if (!tok)
1235 return NULL;
1237 for (i = 0; ; ++i) {
1238 if (!named_un_ops[i].name)
1239 break;
1240 if (named_un_ops[i].op.op != tok->type)
1241 continue;
1243 isl_token_free(tok);
1244 return &named_un_ops[i].op;
1247 isl_stream_push_token(s, tok);
1249 return NULL;
1252 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1253 struct isl_obj arg)
1255 int i;
1257 for (i = 0; ; ++i) {
1258 if (!named_un_ops[i].name)
1259 break;
1260 if (named_un_ops[i].op.op != like->op)
1261 continue;
1262 if (!is_subtype(arg, named_un_ops[i].op.arg))
1263 continue;
1265 return &named_un_ops[i].op;
1268 return NULL;
1271 static int is_assign(struct isl_stream *s)
1273 struct isl_token *tok;
1274 struct isl_token *tok2;
1275 int assign;
1277 tok = isl_stream_next_token(s);
1278 if (!tok)
1279 return 0;
1280 if (tok->type != ISL_TOKEN_IDENT) {
1281 isl_stream_push_token(s, tok);
1282 return 0;
1285 tok2 = isl_stream_next_token(s);
1286 if (!tok2) {
1287 isl_stream_push_token(s, tok);
1288 return 0;
1290 assign = tok2->type == ISL_TOKEN_DEF;
1291 isl_stream_push_token(s, tok2);
1292 isl_stream_push_token(s, tok);
1294 return assign;
1297 static struct isl_obj read_obj(struct isl_stream *s,
1298 struct isl_hash_table *table);
1299 static struct isl_obj read_expr(struct isl_stream *s,
1300 struct isl_hash_table *table);
1302 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1303 struct isl_hash_table *table, struct isc_un_op *op)
1305 struct isl_obj obj = { isl_obj_none, NULL };
1307 obj = read_obj(s, table);
1308 if (!obj.v)
1309 goto error;
1311 op = find_matching_un_op(op, obj);
1313 if (!op)
1314 isl_die(s->ctx, isl_error_invalid,
1315 "no such unary operator defined on given operand",
1316 goto error);
1318 obj = convert(s->ctx, obj, op->arg);
1319 obj.v = op->fn(obj.v);
1320 obj.type = op->res;
1322 return obj;
1323 error:
1324 free_obj(obj);
1325 obj.type = isl_obj_none;
1326 obj.v = NULL;
1327 return obj;
1330 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1332 struct isl_list *list;
1333 int exact;
1335 if (obj.type != isl_obj_union_map)
1336 obj = convert(ctx, obj, isl_obj_union_map);
1337 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1338 list = isl_list_alloc(ctx, 2);
1339 if (!list)
1340 goto error;
1342 list->obj[0].type = isl_obj_union_map;
1343 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1344 list->obj[1].type = isl_obj_bool;
1345 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1346 obj.v = list;
1347 obj.type = isl_obj_list;
1348 if (exact < 0 || !list->obj[0].v)
1349 goto error;
1351 return obj;
1352 error:
1353 free_obj(obj);
1354 obj.type = isl_obj_none;
1355 obj.v = NULL;
1356 return obj;
1359 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1361 struct isl_list *list = obj.v;
1362 struct isl_token *tok;
1363 int i;
1365 tok = isl_stream_next_token(s);
1366 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1367 isl_stream_error(s, tok, "expecting index");
1368 if (tok)
1369 isl_stream_push_token(s, tok);
1370 goto error;
1372 i = isl_int_get_si(tok->u.v);
1373 isl_token_free(tok);
1374 isl_assert(s->ctx, i < list->n, goto error);
1375 if (isl_stream_eat(s, ']'))
1376 goto error;
1378 return obj_at(obj, i);
1379 error:
1380 free_obj(obj);
1381 obj.type = isl_obj_none;
1382 obj.v = NULL;
1383 return obj;
1386 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1387 struct isl_hash_table *table)
1389 struct isl_obj obj;
1391 obj = read_expr(s, table);
1392 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1393 is_subtype(obj, isl_obj_union_map), goto error);
1395 if (obj.type == isl_obj_list) {
1396 struct isl_list *list = obj.v;
1397 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1398 obj = obj_at(obj, 0);
1400 if (obj.type == isl_obj_set)
1401 obj = convert(s->ctx, obj, isl_obj_union_set);
1402 else if (obj.type == isl_obj_map)
1403 obj = convert(s->ctx, obj, isl_obj_union_map);
1404 if (obj.type == isl_obj_union_set) {
1405 obj.v = isl_union_set_apply(obj.v, umap);
1406 } else
1407 obj.v = isl_union_map_apply_range(obj.v, umap);
1408 if (!obj.v)
1409 goto error2;
1411 if (isl_stream_eat(s, ')'))
1412 goto error2;
1414 return obj;
1415 error:
1416 isl_union_map_free(umap);
1417 error2:
1418 free_obj(obj);
1419 obj.type = isl_obj_none;
1420 obj.v = NULL;
1421 return obj;
1424 static struct isl_obj apply_fun_set(struct isl_obj obj,
1425 __isl_take isl_union_set *uset)
1427 if (obj.type == isl_obj_union_pw_qpolynomial) {
1428 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1429 } else {
1430 obj.type = isl_obj_list;
1431 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1433 return obj;
1436 static struct isl_obj apply_fun_map(struct isl_obj obj,
1437 __isl_take isl_union_map *umap)
1439 if (obj.type == isl_obj_union_pw_qpolynomial) {
1440 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1441 } else {
1442 obj.type = isl_obj_list;
1443 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1445 return obj;
1448 static struct isl_obj apply_fun(struct isl_stream *s,
1449 struct isl_obj obj, struct isl_hash_table *table)
1451 struct isl_obj arg;
1453 arg = read_expr(s, table);
1454 if (!is_subtype(arg, isl_obj_union_map) &&
1455 !is_subtype(arg, isl_obj_union_set))
1456 isl_die(s->ctx, isl_error_invalid,
1457 "expecting set of map argument", goto error);
1459 if (arg.type == isl_obj_list) {
1460 struct isl_list *list = arg.v;
1461 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1462 arg = obj_at(arg, 0);
1464 if (arg.type == isl_obj_set)
1465 arg = convert(s->ctx, arg, isl_obj_union_set);
1466 else if (arg.type == isl_obj_map)
1467 arg = convert(s->ctx, arg, isl_obj_union_map);
1468 if (arg.type == isl_obj_union_set)
1469 obj = apply_fun_set(obj, arg.v);
1470 else
1471 obj = apply_fun_map(obj, arg.v);
1472 if (!obj.v)
1473 goto error2;
1475 if (isl_stream_eat(s, ')'))
1476 goto error2;
1478 return obj;
1479 error:
1480 free_obj(arg);
1481 error2:
1482 free_obj(obj);
1483 obj.type = isl_obj_none;
1484 obj.v = NULL;
1485 return obj;
1488 struct add_vertex_data {
1489 struct isl_list *list;
1490 int i;
1493 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1495 struct add_vertex_data *data = (struct add_vertex_data *)user;
1496 isl_basic_set *expr;
1498 expr = isl_vertex_get_expr(vertex);
1500 data->list->obj[data->i].type = isl_obj_set;
1501 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1502 data->i++;
1504 isl_vertex_free(vertex);
1506 return 0;
1509 static int set_vertices(__isl_take isl_set *set, void *user)
1511 isl_ctx *ctx;
1512 isl_basic_set *hull;
1513 isl_vertices *vertices = NULL;
1514 struct isl_list *list = NULL;
1515 int r;
1516 struct add_vertex_data *data = (struct add_vertex_data *)user;
1518 set = isl_set_remove_divs(set);
1519 hull = isl_set_convex_hull(set);
1520 vertices = isl_basic_set_compute_vertices(hull);
1521 isl_basic_set_free(hull);
1523 list = data->list;
1525 ctx = isl_vertices_get_ctx(vertices);
1526 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1527 if (!data->list)
1528 goto error;
1530 data->i = 0;
1531 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1533 data->list = isl_list_concat(list, data->list);
1535 isl_vertices_free(vertices);
1537 return r;
1538 error:
1539 data->list = list;
1540 isl_vertices_free(vertices);
1541 return -1;
1544 static struct isl_obj vertices(struct isl_stream *s,
1545 struct isl_hash_table *table)
1547 isl_ctx *ctx;
1548 struct isl_obj obj;
1549 struct isl_list *list = NULL;
1550 isl_union_set *uset;
1551 struct add_vertex_data data = { NULL };
1553 obj = read_expr(s, table);
1554 obj = convert(s->ctx, obj, isl_obj_union_set);
1555 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1556 uset = obj.v;
1557 obj.v = NULL;
1559 ctx = isl_union_set_get_ctx(uset);
1560 list = isl_list_alloc(ctx, 0);
1561 if (!list)
1562 goto error;
1564 data.list = list;
1566 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1567 goto error;
1569 isl_union_set_free(uset);
1571 obj.type = isl_obj_list;
1572 obj.v = data.list;
1574 return obj;
1575 error:
1576 isl_union_set_free(uset);
1577 isl_list_free(data.list);
1578 free_obj(obj);
1579 obj.type = isl_obj_none;
1580 obj.v = NULL;
1581 return obj;
1584 static struct isl_obj type_of(struct isl_stream *s,
1585 struct isl_hash_table *table)
1587 isl_ctx *ctx;
1588 struct isl_obj obj;
1589 const char *type = "unknown";
1591 obj = read_expr(s, table);
1593 if (obj.type == isl_obj_map ||
1594 obj.type == isl_obj_union_map)
1595 type = "map";
1596 if (obj.type == isl_obj_set ||
1597 obj.type == isl_obj_union_set)
1598 type = "set";
1599 if (obj.type == isl_obj_pw_qpolynomial ||
1600 obj.type == isl_obj_union_pw_qpolynomial)
1601 type = "piecewise quasipolynomial";
1602 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1603 obj.type == isl_obj_union_pw_qpolynomial_fold)
1604 type = "piecewise quasipolynomial fold";
1605 if (obj.type == isl_obj_list)
1606 type = "list";
1607 if (obj.type == isl_obj_bool)
1608 type = "boolean";
1609 if (obj.type == isl_obj_str)
1610 type = "string";
1611 if (obj.type == isl_obj_int)
1612 type = "int";
1614 free_obj(obj);
1615 obj.type = isl_obj_str;
1616 obj.v = isl_str_from_string(s->ctx, strdup(type));
1618 return obj;
1621 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1622 struct isl_hash_table *table)
1624 struct isl_obj obj;
1626 obj = read_obj(s, table);
1627 obj = convert(s->ctx, obj, isl_obj_union_set);
1628 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1629 return obj.v;
1630 error:
1631 free_obj(obj);
1632 return NULL;
1635 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1636 struct isl_hash_table *table)
1638 struct isl_obj obj;
1640 obj = read_obj(s, table);
1641 obj = convert(s->ctx, obj, isl_obj_union_map);
1642 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1643 return obj.v;
1644 error:
1645 free_obj(obj);
1646 return NULL;
1649 static struct isl_obj last_any(struct isl_stream *s,
1650 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1651 __isl_take isl_union_map *may_source)
1653 struct isl_obj obj = { isl_obj_none, NULL };
1654 isl_union_map *sink = NULL;
1655 isl_union_map *schedule = NULL;
1656 isl_union_map *may_dep;
1657 isl_union_map *must_dep;
1659 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1660 goto error;
1662 sink = read_map(s, table);
1663 if (!sink)
1664 goto error;
1666 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1667 goto error;
1669 schedule = read_map(s, table);
1670 if (!schedule)
1671 goto error;
1673 if (isl_union_map_compute_flow(sink, must_source, may_source,
1674 schedule, &must_dep, &may_dep,
1675 NULL, NULL) < 0)
1676 return obj;
1678 obj.type = isl_obj_union_map;
1679 obj.v = isl_union_map_union(must_dep, may_dep);
1681 return obj;
1682 error:
1683 isl_union_map_free(may_source);
1684 isl_union_map_free(must_source);
1685 isl_union_map_free(sink);
1686 isl_union_map_free(schedule);
1687 free_obj(obj);
1688 obj.type = isl_obj_none;
1689 obj.v = NULL;
1690 return obj;
1693 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1695 struct isl_obj obj = { isl_obj_none, NULL };
1696 isl_union_map *must_source = NULL;
1697 isl_union_map *may_source = NULL;
1698 isl_union_map *sink = NULL;
1699 isl_union_map *schedule = NULL;
1700 isl_union_map *may_dep;
1702 may_source = read_map(s, table);
1703 if (!may_source)
1704 goto error;
1706 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1707 must_source = read_map(s, table);
1708 if (!must_source)
1709 goto error;
1710 return last_any(s, table, must_source, may_source);
1713 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1714 goto error;
1716 sink = read_map(s, table);
1717 if (!sink)
1718 goto error;
1720 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1721 goto error;
1723 schedule = read_map(s, table);
1724 if (!schedule)
1725 goto error;
1727 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1728 if (isl_union_map_compute_flow(sink, must_source, may_source,
1729 schedule, NULL, &may_dep,
1730 NULL, NULL) < 0)
1731 return obj;
1733 obj.type = isl_obj_union_map;
1734 obj.v = may_dep;
1736 return obj;
1737 error:
1738 isl_union_map_free(may_source);
1739 isl_union_map_free(must_source);
1740 isl_union_map_free(sink);
1741 isl_union_map_free(schedule);
1742 free_obj(obj);
1743 obj.type = isl_obj_none;
1744 obj.v = NULL;
1745 return obj;
1748 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1750 struct isl_obj obj = { isl_obj_none, NULL };
1751 struct isl_list *list = NULL;
1752 isl_union_map *must_source = NULL;
1753 isl_union_map *may_source = NULL;
1754 isl_union_map *sink = NULL;
1755 isl_union_map *schedule = NULL;
1756 isl_union_map *must_dep;
1757 isl_union_map *must_no_source;
1759 must_source = read_map(s, table);
1760 if (!must_source)
1761 goto error;
1763 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1764 may_source = read_map(s, table);
1765 if (!may_source)
1766 goto error;
1767 return last_any(s, table, must_source, may_source);
1770 list = isl_list_alloc(s->ctx, 2);
1771 if (!list)
1772 goto error;
1774 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1775 goto error;
1777 sink = read_map(s, table);
1778 if (!sink)
1779 goto error;
1781 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1782 goto error;
1784 schedule = read_map(s, table);
1785 if (!schedule)
1786 goto error;
1788 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1789 if (isl_union_map_compute_flow(sink, must_source, may_source,
1790 schedule, &must_dep, NULL,
1791 &must_no_source, NULL) < 0) {
1792 isl_list_free(list);
1793 return obj;
1796 list->obj[0].type = isl_obj_union_map;
1797 list->obj[0].v = must_dep;
1798 list->obj[1].type = isl_obj_union_map;
1799 list->obj[1].v = must_no_source;
1801 obj.v = list;
1802 obj.type = isl_obj_list;
1804 return obj;
1805 error:
1806 isl_list_free(list);
1807 isl_union_map_free(may_source);
1808 isl_union_map_free(must_source);
1809 isl_union_map_free(sink);
1810 isl_union_map_free(schedule);
1811 free_obj(obj);
1812 obj.type = isl_obj_none;
1813 obj.v = NULL;
1814 return obj;
1817 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1818 struct isl_hash_table *table)
1820 isl_union_set *domain;
1821 isl_union_map *validity;
1822 isl_union_map *proximity;
1824 domain = read_set(s, table);
1825 if (!domain)
1826 return NULL;
1828 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1829 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1831 for (;;) {
1832 isl_union_map *umap;
1833 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1834 umap = read_map(s, table);
1835 validity = isl_union_map_union(validity, umap);
1836 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1837 umap = read_map(s, table);
1838 proximity = isl_union_map_union(proximity, umap);
1839 } else
1840 break;
1843 return isl_union_set_compute_schedule(domain, validity, proximity);
1846 static struct isl_obj schedule(struct isl_stream *s,
1847 struct isl_hash_table *table)
1849 struct isl_obj obj = { isl_obj_none, NULL };
1850 isl_schedule *schedule;
1852 schedule = get_schedule(s, table);
1854 obj.v = isl_schedule_get_map(schedule);
1855 obj.type = isl_obj_union_map;
1857 isl_schedule_free(schedule);
1859 return obj;
1862 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1864 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1866 struct isl_obj obj = { isl_obj_none, NULL };
1867 isl_ctx *ctx = isl_band_get_ctx(band);
1868 struct isl_list *list;
1870 list = isl_list_alloc(ctx, 2);
1871 if (!list)
1872 goto error;
1874 obj.v = list;
1875 obj.type = isl_obj_list;
1877 list->obj[0].type = isl_obj_union_map;
1878 list->obj[0].v = isl_band_get_partial_schedule(band);
1880 if (isl_band_has_children(band)) {
1881 isl_band_list *children;
1883 children = isl_band_get_children(band);
1884 list->obj[1] = band_list_to_obj_list(children);
1885 } else {
1886 list->obj[1].type = isl_obj_list;
1887 list->obj[1].v = isl_list_alloc(ctx, 0);
1890 if (!list->obj[0].v || !list->obj[1].v)
1891 goto error;
1893 isl_band_free(band);
1895 return obj;
1896 error:
1897 isl_band_free(band);
1898 free_obj(obj);
1899 obj.type = isl_obj_none;
1900 obj.v = NULL;
1901 return obj;
1904 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1906 struct isl_obj obj = { isl_obj_none, NULL };
1907 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1908 struct isl_list *list;
1909 int i, n;
1911 n = isl_band_list_n_band(bands);
1912 list = isl_list_alloc(ctx, n);
1913 if (!list)
1914 goto error;
1916 obj.v = list;
1917 obj.type = isl_obj_list;
1919 for (i = 0; i < n; ++i) {
1920 isl_band *band;
1922 band = isl_band_list_get_band(bands, i);
1923 list->obj[i] = band_to_obj_list(band);
1924 if (!list->obj[i].v)
1925 goto error;
1928 isl_band_list_free(bands);
1930 return obj;
1931 error:
1932 isl_band_list_free(bands);
1933 free_obj(obj);
1934 obj.type = isl_obj_none;
1935 obj.v = NULL;
1936 return obj;
1939 static struct isl_obj schedule_forest(struct isl_stream *s,
1940 struct isl_hash_table *table)
1942 struct isl_obj obj = { isl_obj_none, NULL };
1943 isl_schedule *schedule;
1944 isl_band_list *roots;
1946 schedule = get_schedule(s, table);
1947 if (!schedule)
1948 return obj;
1950 roots = isl_schedule_get_band_forest(schedule);
1951 isl_schedule_free(schedule);
1953 return band_list_to_obj_list(roots);
1956 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1958 struct isl_token *tok;
1960 if (isl_stream_eat_if_available(s, '+'))
1961 return transitive_closure(s->ctx, obj);
1963 tok = isl_stream_next_token(s);
1964 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1965 isl_stream_error(s, tok, "expecting -1");
1966 if (tok)
1967 isl_stream_push_token(s, tok);
1968 goto error;
1970 isl_token_free(tok);
1971 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1972 if (obj.type != isl_obj_union_map)
1973 obj = convert(s->ctx, obj, isl_obj_union_map);
1975 obj.v = isl_union_map_reverse(obj.v);
1976 if (!obj.v)
1977 goto error;
1979 return obj;
1980 error:
1981 free_obj(obj);
1982 obj.type = isl_obj_none;
1983 obj.v = NULL;
1984 return obj;
1987 static struct isl_obj check_assert(struct isl_stream *s,
1988 struct isl_hash_table *table)
1990 struct isl_obj obj;
1992 obj = read_expr(s, table);
1993 if (obj.type != isl_obj_bool)
1994 isl_die(s->ctx, isl_error_invalid,
1995 "expecting boolean expression", goto error);
1996 if (obj.v != &isl_bool_true)
1997 isl_die(s->ctx, isl_error_unknown,
1998 "assertion failed", abort());
1999 error:
2000 free_obj(obj);
2001 obj.type = isl_obj_none;
2002 obj.v = NULL;
2003 return obj;
2006 static struct isl_obj read_from_file(struct isl_stream *s)
2008 struct isl_obj obj;
2009 struct isl_token *tok;
2010 struct isl_stream *s_file;
2011 struct iscc_options *options;
2012 FILE *file;
2014 tok = isl_stream_next_token(s);
2015 if (!tok || tok->type != ISL_TOKEN_STRING) {
2016 isl_stream_error(s, tok, "expecting filename");
2017 isl_token_free(tok);
2018 goto error;
2021 options = isl_ctx_peek_iscc_options(s->ctx);
2022 if (!options || !options->io) {
2023 isl_token_free(tok);
2024 isl_die(s->ctx, isl_error_invalid,
2025 "read operation not allowed", goto error);
2028 file = fopen(tok->u.s, "r");
2029 isl_token_free(tok);
2030 isl_assert(s->ctx, file, goto error);
2032 s_file = isl_stream_new_file(s->ctx, file);
2033 if (!s_file) {
2034 fclose(file);
2035 goto error;
2038 obj = isl_stream_read_obj(s_file);
2040 isl_stream_free(s_file);
2041 fclose(file);
2043 return obj;
2044 error:
2045 obj.type = isl_obj_none;
2046 obj.v = NULL;
2047 return obj;
2050 static struct isl_obj write_to_file(struct isl_stream *s,
2051 struct isl_hash_table *table)
2053 struct isl_obj obj;
2054 struct isl_token *tok;
2055 struct isl_stream *s_file;
2056 struct iscc_options *options;
2057 FILE *file;
2058 isl_printer *p;
2060 tok = isl_stream_next_token(s);
2061 if (!tok || tok->type != ISL_TOKEN_STRING) {
2062 isl_stream_error(s, tok, "expecting filename");
2063 isl_token_free(tok);
2064 goto error;
2067 obj = read_expr(s, table);
2069 options = isl_ctx_peek_iscc_options(s->ctx);
2070 if (!options || !options->io) {
2071 isl_token_free(tok);
2072 isl_die(s->ctx, isl_error_invalid,
2073 "write operation not allowed", goto error);
2076 file = fopen(tok->u.s, "w");
2077 isl_token_free(tok);
2078 if (!file)
2079 isl_die(s->ctx, isl_error_unknown,
2080 "could not open file for writing", goto error);
2082 p = isl_printer_to_file(s->ctx, file);
2083 p = isl_printer_set_output_format(p, options->format);
2084 p = obj.type->print(p, obj.v);
2085 p = isl_printer_end_line(p);
2086 isl_printer_free(p);
2088 fclose(file);
2089 error:
2090 free_obj(obj);
2091 obj.type = isl_obj_none;
2092 obj.v = NULL;
2093 return obj;
2096 static struct isl_obj read_string_if_available(struct isl_stream *s)
2098 struct isl_token *tok;
2099 struct isl_obj obj = { isl_obj_none, NULL };
2101 tok = isl_stream_next_token(s);
2102 if (!tok)
2103 return obj;
2104 if (tok->type == ISL_TOKEN_STRING) {
2105 isl_str *str;
2106 str = isl_str_alloc(s->ctx);
2107 if (!str)
2108 goto error;
2109 str->s = strdup(tok->u.s);
2110 isl_token_free(tok);
2111 obj.v = str;
2112 obj.type = isl_obj_str;
2113 } else
2114 isl_stream_push_token(s, tok);
2115 return obj;
2116 error:
2117 isl_token_free(tok);
2118 return obj;
2121 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2123 struct isl_token *tok;
2124 struct isl_obj obj = { isl_obj_none, NULL };
2126 tok = isl_stream_next_token(s);
2127 if (!tok)
2128 return obj;
2129 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2130 int is_true = tok->type == ISL_TOKEN_TRUE;
2131 isl_token_free(tok);
2132 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2133 obj.type = isl_obj_bool;
2134 } else
2135 isl_stream_push_token(s, tok);
2136 return obj;
2137 error:
2138 isl_token_free(tok);
2139 return obj;
2142 static __isl_give char *read_ident(struct isl_stream *s)
2144 char *name;
2145 struct isl_token *tok, *tok2;
2147 name = isl_stream_read_ident_if_available(s);
2148 if (name)
2149 return name;
2151 tok = isl_stream_next_token(s);
2152 if (!tok)
2153 return NULL;
2154 if (tok->type != '$') {
2155 isl_stream_push_token(s, tok);
2156 return NULL;
2158 tok2 = isl_stream_next_token(s);
2159 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2160 if (tok2)
2161 isl_stream_push_token(s, tok2);
2162 isl_stream_push_token(s, tok);
2163 return NULL;
2166 name = isl_int_get_str(tok2->u.v);
2167 isl_token_free(tok);
2168 isl_token_free(tok2);
2170 return name;
2173 static struct isl_obj read_list(struct isl_stream *s,
2174 struct isl_hash_table *table, struct isl_obj obj)
2176 struct isl_list *list;
2178 list = isl_list_alloc(s->ctx, 2);
2179 if (!list)
2180 goto error;
2181 list->obj[0] = obj;
2182 list->obj[1] = read_obj(s, table);
2183 obj.v = list;
2184 obj.type = isl_obj_list;
2186 if (!list->obj[1].v)
2187 goto error;
2189 while (isl_stream_eat_if_available(s, ',')) {
2190 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2191 if (!obj.v)
2192 goto error;
2195 return obj;
2196 error:
2197 free_obj(obj);
2198 obj.type = isl_obj_none;
2199 obj.v = NULL;
2200 return obj;
2203 static struct isl_obj read_obj(struct isl_stream *s,
2204 struct isl_hash_table *table)
2206 struct isl_obj obj = { isl_obj_none, NULL };
2207 char *name = NULL;
2208 struct isc_un_op *op = NULL;
2210 obj = read_string_if_available(s);
2211 if (obj.v)
2212 return obj;
2213 obj = read_bool_if_available(s);
2214 if (obj.v)
2215 return obj;
2216 if (isl_stream_eat_if_available(s, '(')) {
2217 if (isl_stream_next_token_is(s, ')')) {
2218 obj.type = isl_obj_list;
2219 obj.v = isl_list_alloc(s->ctx, 0);
2220 } else {
2221 obj = read_expr(s, table);
2222 if (obj.v && isl_stream_eat_if_available(s, ','))
2223 obj = read_list(s, table, obj);
2225 if (!obj.v || isl_stream_eat(s, ')'))
2226 goto error;
2227 } else {
2228 op = read_prefix_un_op_if_available(s);
2229 if (op)
2230 return read_un_op_expr(s, table, op);
2232 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2233 return check_assert(s, table);
2234 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2235 return read_from_file(s);
2236 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2237 return write_to_file(s, table);
2238 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2239 return vertices(s, table);
2240 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2241 return any(s, table);
2242 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2243 return last(s, table);
2244 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2245 return schedule(s, table);
2246 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2247 return schedule_forest(s, table);
2248 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2249 return type_of(s, table);
2251 name = read_ident(s);
2252 if (name)
2253 obj = stored_obj(s->ctx, table, name);
2254 else
2255 obj = isl_stream_read_obj(s);
2256 if (!obj.v)
2257 goto error;
2260 if (isl_stream_eat_if_available(s, '^'))
2261 obj = power(s, obj);
2262 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2263 obj = obj_at_index(s, obj);
2264 else if (is_subtype(obj, isl_obj_union_map) &&
2265 isl_stream_eat_if_available(s, '(')) {
2266 obj = convert(s->ctx, obj, isl_obj_union_map);
2267 obj = apply(s, obj.v, table);
2268 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2269 isl_stream_eat_if_available(s, '(')) {
2270 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2271 obj = apply_fun(s, obj, table);
2272 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2273 isl_stream_eat_if_available(s, '(')) {
2274 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2275 obj = apply_fun(s, obj, table);
2278 return obj;
2279 error:
2280 free_obj(obj);
2281 obj.type = isl_obj_none;
2282 obj.v = NULL;
2283 return obj;
2286 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2287 struct isl_obj lhs, struct isl_obj rhs)
2289 int i;
2291 for (i = 0; ; ++i) {
2292 if (!bin_ops[i].op)
2293 break;
2294 if (bin_ops[i].op != like->op)
2295 continue;
2296 if (!is_subtype(lhs, bin_ops[i].lhs))
2297 continue;
2298 if (!is_subtype(rhs, bin_ops[i].rhs))
2299 continue;
2301 return &bin_ops[i];
2304 for (i = 0; ; ++i) {
2305 if (!named_bin_ops[i].name)
2306 break;
2307 if (named_bin_ops[i].op.op != like->op)
2308 continue;
2309 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2310 continue;
2311 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2312 continue;
2314 return &named_bin_ops[i].op;
2317 return NULL;
2320 static int next_is_neg_int(struct isl_stream *s)
2322 struct isl_token *tok;
2323 int ret;
2325 tok = isl_stream_next_token(s);
2326 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2327 isl_stream_push_token(s, tok);
2329 return ret;
2332 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2333 struct isl_obj lhs, struct isl_obj rhs)
2335 struct isl_obj obj;
2337 lhs = convert(ctx, lhs, op->lhs);
2338 rhs = convert(ctx, rhs, op->rhs);
2339 if (op->res != isl_obj_bool)
2340 obj.v = op->o.fn(lhs.v, rhs.v);
2341 else {
2342 int res = op->o.test(lhs.v, rhs.v);
2343 free_obj(lhs);
2344 free_obj(rhs);
2345 obj.v = isl_bool_from_int(res);
2347 obj.type = op->res;
2349 return obj;
2352 static struct isl_obj read_expr(struct isl_stream *s,
2353 struct isl_hash_table *table)
2355 struct isl_obj obj = { isl_obj_none, NULL };
2356 struct isl_obj right_obj = { isl_obj_none, NULL };
2358 obj = read_obj(s, table);
2359 for (; obj.v;) {
2360 struct isc_bin_op *op = NULL;
2362 op = read_bin_op_if_available(s, obj);
2363 if (!op)
2364 break;
2366 right_obj = read_obj(s, table);
2368 op = find_matching_bin_op(op, obj, right_obj);
2370 if (!op)
2371 isl_die(s->ctx, isl_error_invalid,
2372 "no such binary operator defined on given operands",
2373 goto error);
2375 obj = call_bin_op(s->ctx, op, obj, right_obj);
2378 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2379 right_obj = read_obj(s, table);
2380 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2383 return obj;
2384 error:
2385 free_obj(right_obj);
2386 free_obj(obj);
2387 obj.type = isl_obj_none;
2388 obj.v = NULL;
2389 return obj;
2392 static __isl_give isl_printer *source_file(struct isl_stream *s,
2393 struct isl_hash_table *table, __isl_take isl_printer *p);
2395 static __isl_give isl_printer *read_line(struct isl_stream *s,
2396 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2398 struct isl_obj obj = { isl_obj_none, NULL };
2399 char *lhs = NULL;
2400 int assign = 0;
2401 int only_print = 0;
2402 struct isc_bin_op *op = NULL;
2403 char buf[30];
2405 if (!p)
2406 return NULL;
2407 if (isl_stream_is_empty(s))
2408 return p;
2410 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2411 return source_file(s, table, p);
2413 assign = is_assign(s);
2414 if (assign) {
2415 lhs = isl_stream_read_ident_if_available(s);
2416 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2417 goto error;
2418 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2419 only_print = 1;
2420 else if (!tty)
2421 only_print = 1;
2423 obj = read_expr(s, table);
2424 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2425 fprintf(stderr, "Interrupted\n");
2426 isl_ctx_reset_error(s->ctx);
2428 if (isl_stream_eat(s, ';'))
2429 goto error;
2431 if (only_print) {
2432 if (obj.type != isl_obj_none && obj.v != NULL) {
2433 p = obj.type->print(p, obj.v);
2434 p = isl_printer_end_line(p);
2436 free_obj(obj);
2437 return p;
2439 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2440 static int count = 0;
2441 snprintf(buf, sizeof(buf), "$%d", count++);
2442 lhs = strdup(buf + 1);
2444 p = isl_printer_print_str(p, buf);
2445 p = isl_printer_print_str(p, " := ");
2446 p = obj.type->print(p, obj.v);
2447 p = isl_printer_end_line(p);
2449 if (lhs && do_assign(s->ctx, table, lhs, obj))
2450 return p;
2452 return p;
2453 error:
2454 isl_stream_flush_tokens(s);
2455 isl_stream_skip_line(s);
2456 free(lhs);
2457 free_obj(obj);
2458 return p;
2461 int free_cb(void **entry, void *user)
2463 struct isl_named_obj *named = *entry;
2465 free_obj(named->obj);
2466 free(named->name);
2467 free(named);
2469 return 0;
2472 static void register_named_ops(struct isl_stream *s)
2474 int i;
2476 for (i = 0; i < ISCC_N_OP; ++i) {
2477 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2478 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2481 for (i = 0; ; ++i) {
2482 if (!named_un_ops[i].name)
2483 break;
2484 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2485 named_un_ops[i].name);
2486 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2489 for (i = 0; ; ++i) {
2490 if (!named_bin_ops[i].name)
2491 break;
2492 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2493 named_bin_ops[i].name);
2494 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2498 static __isl_give isl_printer *source_file(struct isl_stream *s,
2499 struct isl_hash_table *table, __isl_take isl_printer *p)
2501 struct isl_token *tok;
2502 struct isl_stream *s_file;
2503 FILE *file;
2505 tok = isl_stream_next_token(s);
2506 if (!tok || tok->type != ISL_TOKEN_STRING) {
2507 isl_stream_error(s, tok, "expecting filename");
2508 isl_token_free(tok);
2509 return p;
2512 file = fopen(tok->u.s, "r");
2513 isl_token_free(tok);
2514 isl_assert(s->ctx, file, return p);
2516 s_file = isl_stream_new_file(s->ctx, file);
2517 if (!s_file) {
2518 fclose(file);
2519 return p;
2522 register_named_ops(s_file);
2524 while (!s_file->eof)
2525 p = read_line(s_file, table, p, 0);
2527 isl_stream_free(s_file);
2528 fclose(file);
2530 isl_stream_eat(s, ';');
2532 return p;
2535 int main(int argc, char **argv)
2537 struct isl_ctx *ctx;
2538 struct isl_stream *s;
2539 struct isl_hash_table *table;
2540 struct iscc_options *options;
2541 isl_printer *p;
2542 int tty = isatty(0);
2544 options = iscc_options_new_with_defaults();
2545 assert(options);
2546 argc = iscc_options_parse(options, argc, argv, ISL_ARG_ALL);
2548 ctx = isl_ctx_alloc_with_options(iscc_options_arg, options);
2549 s = isl_stream_new_file(ctx, stdin);
2550 assert(s);
2551 table = isl_hash_table_alloc(ctx, 10);
2552 assert(table);
2553 p = isl_printer_to_file(ctx, stdout);
2554 p = isl_printer_set_output_format(p, options->format);
2555 assert(p);
2557 register_named_ops(s);
2559 install_signal_handler(ctx);
2561 while (p && !s->eof) {
2562 isl_ctx_resume(ctx);
2563 p = read_line(s, table, p, tty);
2566 remove_signal_handler(ctx);
2568 isl_printer_free(p);
2569 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2570 isl_hash_table_free(ctx, table);
2571 isl_stream_free(s);
2572 isl_ctx_free(ctx);
2574 return 0;