iscc.c: print_code: do not assume isl_union_map_get_space returns params space
[barvinok.git] / iscc.c
bloba0d4f90424d38b3b8cb14c80e1e8f422f91609b9
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/obj.h>
7 #include <isl/stream.h>
8 #include <isl/set.h>
9 #include <isl/map.h>
10 #include <isl/vertices.h>
11 #include <isl/flow.h>
12 #include <isl/band.h>
13 #include <isl/schedule.h>
14 #include <isl/ast_build.h>
15 #include <isl_obj_list.h>
16 #include <isl_obj_str.h>
17 #include <barvinok/isl.h>
18 #include <barvinok/options.h>
19 #include "lattice_width.h"
21 #include "config.h"
23 #ifdef HAVE_SIGACTION
24 #include <signal.h>
26 static isl_ctx *main_ctx;
28 static void handler(int signum)
30 if (isl_ctx_aborted(main_ctx))
31 exit(EXIT_FAILURE);
32 isl_ctx_abort(main_ctx);
35 static struct sigaction sa_old;
37 static void install_signal_handler(isl_ctx *ctx)
39 struct sigaction sa;
41 main_ctx = ctx;
43 memset(&sa, 0, sizeof(struct sigaction));
44 sa.sa_handler = &handler;
45 sa.sa_flags = SA_RESTART;
46 sigaction(SIGINT, &sa, &sa_old);
49 static void remove_signal_handler(isl_ctx *ctx)
51 sigaction(SIGINT, &sa_old, NULL);
54 #else
56 static void install_signal_handler(isl_ctx *ctx)
60 static void remove_signal_handler(isl_ctx *ctx)
64 #endif
66 #ifdef HAVE_PET
67 #include <pet.h>
68 #else
69 struct pet_options;
70 int pet_options_set_autodetect(isl_ctx *ctx, int val)
72 return -1;
74 #endif
76 static int isl_bool_false = 0;
77 static int isl_bool_true = 1;
78 static int isl_bool_error = -1;
80 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
81 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
82 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
83 ISCC_MINIMIZING, ISCC_RESPECTING,
84 ISCC_CODEGEN, ISCC_USING,
85 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
86 ISCC_N_OP };
87 static const char *op_name[ISCC_N_OP] = {
88 [ISCC_ASSERT] = "assert",
89 [ISCC_READ] = "read",
90 [ISCC_WRITE] = "write",
91 [ISCC_PRINT] = "print",
92 [ISCC_SOURCE] = "source",
93 [ISCC_VERTICES] = "vertices",
94 [ISCC_LAST] = "last",
95 [ISCC_ANY] = "any",
96 [ISCC_BEFORE] = "before",
97 [ISCC_UNDER] = "under",
98 [ISCC_SCHEDULE] = "schedule",
99 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
100 [ISCC_MINIMIZING] = "minimizing",
101 [ISCC_RESPECTING] = "respecting",
102 [ISCC_CODEGEN] = "codegen",
103 [ISCC_USING] = "using",
104 [ISCC_TYPEOF] = "typeof"
106 static enum isl_token_type iscc_op[ISCC_N_OP];
108 struct isl_arg_choice iscc_format[] = {
109 {"isl", ISL_FORMAT_ISL},
110 {"omega", ISL_FORMAT_OMEGA},
111 {"polylib", ISL_FORMAT_POLYLIB},
112 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
113 {"latex", ISL_FORMAT_LATEX},
114 {"C", ISL_FORMAT_C},
118 struct iscc_options {
119 struct barvinok_options *barvinok;
120 struct pet_options *pet;
121 unsigned format;
122 int io;
125 ISL_ARGS_START(struct iscc_options, iscc_options_args)
126 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
127 "barvinok options")
128 #ifdef HAVE_PET
129 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
130 #endif
131 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
132 iscc_format, ISL_FORMAT_ISL, "output format")
133 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
134 "allow read and write operations")
135 ISL_ARGS_END
137 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
138 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
140 static void *isl_obj_bool_copy(void *v)
142 return v;
145 static void isl_obj_bool_free(void *v)
149 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
150 void *v)
152 if (v == &isl_bool_true)
153 return isl_printer_print_str(p, "True");
154 else if (v == &isl_bool_false)
155 return isl_printer_print_str(p, "False");
156 else
157 return isl_printer_print_str(p, "Error");
160 static void *isl_obj_bool_add(void *v1, void *v2)
162 return v1;
165 struct isl_obj_vtable isl_obj_bool_vtable = {
166 isl_obj_bool_copy,
167 isl_obj_bool_add,
168 isl_obj_bool_print,
169 isl_obj_bool_free
171 #define isl_obj_bool (&isl_obj_bool_vtable)
173 int *isl_bool_from_int(int res)
175 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
178 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
179 __isl_take isl_union_map *map2)
181 return isl_union_map_is_subset(map2, map1);
183 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
184 __isl_take isl_union_set *set2)
186 return isl_union_set_is_subset(set2, set1);
189 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
190 __isl_take isl_union_map *map2)
192 return isl_union_map_is_strict_subset(map2, map1);
194 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
195 __isl_take isl_union_set *set2)
197 return isl_union_set_is_strict_subset(set2, set1);
200 extern struct isl_obj_vtable isl_obj_list_vtable;
201 #define isl_obj_list (&isl_obj_list_vtable)
203 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
204 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
205 struct isc_bin_op {
206 enum isl_token_type op;
207 isl_obj_type lhs;
208 isl_obj_type rhs;
209 isl_obj_type res;
210 union {
211 isc_bin_op_fn fn;
212 isc_bin_test_fn test;
213 } o;
215 struct isc_named_bin_op {
216 char *name;
217 struct isc_bin_op op;
220 struct iscc_at {
221 isl_union_pw_qpolynomial *upwqp;
222 isl_union_pw_qpolynomial *res;
225 static int eval_at(__isl_take isl_point *pnt, void *user)
227 struct iscc_at *at = (struct iscc_at *) user;
228 isl_qpolynomial *qp;
229 isl_set *set;
231 set = isl_set_from_point(isl_point_copy(pnt));
232 qp = isl_union_pw_qpolynomial_eval(
233 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
235 at->res = isl_union_pw_qpolynomial_add(at->res,
236 isl_union_pw_qpolynomial_from_pw_qpolynomial(
237 isl_pw_qpolynomial_alloc(set, qp)));
239 return 0;
242 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
243 __isl_take isl_union_pw_qpolynomial *upwqp,
244 __isl_take isl_union_set *uset)
246 struct iscc_at at;
248 at.upwqp = upwqp;
249 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
251 isl_union_set_foreach_point(uset, eval_at, &at);
253 isl_union_pw_qpolynomial_free(upwqp);
254 isl_union_set_free(uset);
256 return at.res;
259 struct iscc_fold_at {
260 isl_union_pw_qpolynomial_fold *upwf;
261 isl_union_pw_qpolynomial *res;
264 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
266 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
267 isl_qpolynomial *qp;
268 isl_set *set;
270 set = isl_set_from_point(isl_point_copy(pnt));
271 qp = isl_union_pw_qpolynomial_fold_eval(
272 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
274 at->res = isl_union_pw_qpolynomial_add(at->res,
275 isl_union_pw_qpolynomial_from_pw_qpolynomial(
276 isl_pw_qpolynomial_alloc(set, qp)));
278 return 0;
281 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
282 __isl_take isl_union_pw_qpolynomial_fold *upwf,
283 __isl_take isl_union_set *uset)
285 struct iscc_fold_at at;
287 at.upwf = upwf;
288 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
290 isl_union_set_foreach_point(uset, eval_fold_at, &at);
292 isl_union_pw_qpolynomial_fold_free(upwf);
293 isl_union_set_free(uset);
295 return at.res;
298 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
299 __isl_take isl_union_pw_qpolynomial *upwqp,
300 __isl_take isl_union_pw_qpolynomial_fold *upwf)
302 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
303 upwqp);
306 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
307 __isl_take isl_union_map *umap,
308 __isl_take isl_union_pw_qpolynomial_fold *upwf)
310 isl_ctx *ctx;
311 struct isl_list *list;
312 int tight;
314 ctx = isl_union_map_get_ctx(umap);
315 list = isl_list_alloc(ctx, 2);
316 if (!list)
317 goto error2;
319 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
320 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
321 upwf, &tight);
322 list->obj[1].type = isl_obj_bool;
323 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
324 if (tight < 0 || !list->obj[0].v)
325 goto error;
327 return list;
328 error2:
329 isl_union_map_free(umap);
330 isl_union_pw_qpolynomial_fold_free(upwf);
331 error:
332 isl_list_free(list);
333 return NULL;
336 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
337 __isl_take isl_union_set *uset,
338 __isl_take isl_union_pw_qpolynomial_fold *upwf)
340 isl_ctx *ctx;
341 struct isl_list *list;
342 int tight;
344 ctx = isl_union_set_get_ctx(uset);
345 list = isl_list_alloc(ctx, 2);
346 if (!list)
347 goto error2;
349 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
350 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
351 upwf, &tight);
352 list->obj[1].type = isl_obj_bool;
353 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
354 if (tight < 0 || !list->obj[0].v)
355 goto error;
357 return list;
358 error2:
359 isl_union_set_free(uset);
360 isl_union_pw_qpolynomial_fold_free(upwf);
361 error:
362 isl_list_free(list);
363 return NULL;
366 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_int_mul(
367 __isl_take isl_union_pw_qpolynomial *upwqp, __isl_take isl_int_obj *i)
369 isl_int v;
371 if (!i)
372 goto error;
374 isl_int_init(v);
375 isl_int_obj_get_int(i, &v);
376 upwqp = isl_union_pw_qpolynomial_mul_isl_int(upwqp, v);
377 isl_int_clear(v);
379 isl_int_obj_free(i);
381 return upwqp;
382 error:
383 isl_union_pw_qpolynomial_free(upwqp);
384 return NULL;
387 static __isl_give isl_union_pw_qpolynomial *int_union_pw_qpolynomial_mul(
388 __isl_take isl_int_obj *i, __isl_take isl_union_pw_qpolynomial *upwqp)
390 return union_pw_qpolynomial_int_mul(upwqp, i);
393 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_fold_int_mul(
394 __isl_take isl_union_pw_qpolynomial_fold *upwf,
395 __isl_take isl_int_obj *i)
397 isl_int v;
399 if (!i)
400 goto error;
402 isl_int_init(v);
403 isl_int_obj_get_int(i, &v);
404 upwf = isl_union_pw_qpolynomial_fold_mul_isl_int(upwf, v);
405 isl_int_clear(v);
407 isl_int_obj_free(i);
409 return upwf;
410 error:
411 isl_union_pw_qpolynomial_fold_free(upwf);
412 return NULL;
415 static __isl_give isl_union_pw_qpolynomial_fold *int_union_pw_qpolynomial_fold_mul(
416 __isl_take isl_int_obj *i,
417 __isl_take isl_union_pw_qpolynomial_fold *upwf)
419 return union_pw_qpolynomial_fold_int_mul(upwf, i);
422 struct isc_bin_op bin_ops[] = {
423 { '+', isl_obj_int, isl_obj_int, isl_obj_int,
424 (isc_bin_op_fn) &isl_int_obj_add },
425 { '-', isl_obj_int, isl_obj_int, isl_obj_int,
426 (isc_bin_op_fn) &isl_int_obj_sub },
427 { '*', isl_obj_int, isl_obj_int, isl_obj_int,
428 (isc_bin_op_fn) &isl_int_obj_mul },
429 { '+', isl_obj_union_set, isl_obj_union_set,
430 isl_obj_union_set,
431 (isc_bin_op_fn) &isl_union_set_union },
432 { '+', isl_obj_union_map, isl_obj_union_map,
433 isl_obj_union_map,
434 (isc_bin_op_fn) &isl_union_map_union },
435 { '-', isl_obj_union_set, isl_obj_union_set,
436 isl_obj_union_set,
437 (isc_bin_op_fn) &isl_union_set_subtract },
438 { '-', isl_obj_union_map, isl_obj_union_map,
439 isl_obj_union_map,
440 (isc_bin_op_fn) &isl_union_map_subtract },
441 { '*', isl_obj_union_set, isl_obj_union_set,
442 isl_obj_union_set,
443 (isc_bin_op_fn) &isl_union_set_intersect },
444 { '*', isl_obj_union_map, isl_obj_union_map,
445 isl_obj_union_map,
446 (isc_bin_op_fn) &isl_union_map_intersect },
447 { '*', isl_obj_union_map, isl_obj_union_set,
448 isl_obj_union_map,
449 (isc_bin_op_fn) &isl_union_map_intersect_domain },
450 { '.', isl_obj_union_map, isl_obj_union_map,
451 isl_obj_union_map,
452 (isc_bin_op_fn) &isl_union_map_apply_range },
453 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
454 isl_obj_union_pw_qpolynomial,
455 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
456 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
457 isl_obj_list,
458 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
459 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
460 isl_obj_union_map,
461 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
462 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
463 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
464 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
465 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
466 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
467 isl_obj_bool,
468 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
469 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
470 isl_obj_bool,
471 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
472 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
473 isl_obj_bool,
474 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
475 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
476 isl_obj_bool,
477 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
478 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
479 isl_obj_bool,
480 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
481 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
482 isl_obj_bool,
483 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
484 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
485 isl_obj_bool,
486 { .test =
487 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
488 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
489 isl_obj_bool,
490 { .test =
491 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
492 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
493 isl_obj_union_map,
494 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
495 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
496 isl_obj_union_map,
497 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
498 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
499 isl_obj_union_map,
500 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
501 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
502 isl_obj_union_map,
503 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
504 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
505 isl_obj_union_map,
506 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
507 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
508 isl_obj_union_map,
509 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
510 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
511 isl_obj_union_map,
512 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
513 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
514 isl_obj_union_map,
515 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
516 { '.', isl_obj_union_pw_qpolynomial_fold,
517 isl_obj_union_pw_qpolynomial_fold,
518 isl_obj_union_pw_qpolynomial_fold,
519 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
520 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
521 isl_obj_union_pw_qpolynomial,
522 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
523 { '+', isl_obj_union_pw_qpolynomial,
524 isl_obj_union_pw_qpolynomial_fold,
525 isl_obj_union_pw_qpolynomial_fold,
526 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
527 { '+', isl_obj_union_pw_qpolynomial_fold,
528 isl_obj_union_pw_qpolynomial,
529 isl_obj_union_pw_qpolynomial_fold,
530 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
531 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
532 isl_obj_union_pw_qpolynomial,
533 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
534 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial,
535 isl_obj_union_pw_qpolynomial,
536 (isc_bin_op_fn) &int_union_pw_qpolynomial_mul },
537 { '*', isl_obj_union_pw_qpolynomial, isl_obj_int,
538 isl_obj_union_pw_qpolynomial,
539 (isc_bin_op_fn) &union_pw_qpolynomial_int_mul },
540 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial_fold,
541 isl_obj_union_pw_qpolynomial_fold,
542 (isc_bin_op_fn) &int_union_pw_qpolynomial_fold_mul },
543 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_int,
544 isl_obj_union_pw_qpolynomial_fold,
545 (isc_bin_op_fn) &union_pw_qpolynomial_fold_int_mul },
546 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
547 isl_obj_union_pw_qpolynomial,
548 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
549 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
550 isl_obj_union_pw_qpolynomial,
551 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
552 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
553 isl_obj_union_pw_qpolynomial_fold,
554 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
555 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
556 isl_obj_union_pw_qpolynomial,
557 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
558 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
559 isl_obj_union_pw_qpolynomial,
560 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
561 { '%', isl_obj_union_set, isl_obj_union_set,
562 isl_obj_union_set,
563 (isc_bin_op_fn) &isl_union_set_gist },
564 { '%', isl_obj_union_map, isl_obj_union_map,
565 isl_obj_union_map,
566 (isc_bin_op_fn) &isl_union_map_gist },
567 { '%', isl_obj_union_map, isl_obj_union_set,
568 isl_obj_union_map,
569 (isc_bin_op_fn) &isl_union_map_gist_domain },
570 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
571 isl_obj_union_pw_qpolynomial,
572 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
573 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
574 isl_obj_union_pw_qpolynomial_fold,
575 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
576 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
577 isl_obj_union_pw_qpolynomial, isl_obj_bool,
578 { .test = (isc_bin_test_fn)
579 &isl_union_pw_qpolynomial_plain_is_equal } },
580 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
581 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
582 { .test = (isc_bin_test_fn)
583 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
584 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
585 (isc_bin_op_fn) &isl_str_concat },
589 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
590 __isl_take isl_union_map *umap2)
592 return isl_union_map_apply_range(umap2, umap1);
595 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
596 __isl_take isl_union_pw_qpolynomial *upwqp,
597 __isl_take isl_union_map *umap)
599 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
602 static __isl_give struct isl_list *qpolynomial_fold_after_map(
603 __isl_take isl_union_pw_qpolynomial_fold *upwf,
604 __isl_take isl_union_map *umap)
606 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
609 struct isc_named_bin_op named_bin_ops[] = {
610 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
611 isl_obj_union_map,
612 (isc_bin_op_fn) &map_after_map } },
613 { "after", { -1, isl_obj_union_pw_qpolynomial,
614 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
615 (isc_bin_op_fn) &qpolynomial_after_map } },
616 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
617 isl_obj_union_map, isl_obj_list,
618 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
619 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
620 isl_obj_union_map,
621 (isc_bin_op_fn) &isl_union_map_apply_range } },
622 { "before", { -1, isl_obj_union_map,
623 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
624 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
625 { "before", { -1, isl_obj_union_map,
626 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
627 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
628 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
629 isl_obj_union_set,
630 (isc_bin_op_fn) &isl_union_set_product } },
631 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
632 isl_obj_union_map,
633 (isc_bin_op_fn) &isl_union_map_product } },
634 NULL
637 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
639 return isl_set_from_basic_set(isl_union_set_sample(uset));
642 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
644 return isl_map_from_basic_map(isl_union_map_sample(umap));
647 static __isl_give struct isl_list *union_map_power(
648 __isl_take isl_union_map *umap)
650 isl_ctx *ctx;
651 struct isl_list *list;
652 int exact;
654 ctx = isl_union_map_get_ctx(umap);
655 list = isl_list_alloc(ctx, 2);
656 if (!list)
657 goto error2;
659 list->obj[0].type = isl_obj_union_map;
660 list->obj[0].v = isl_union_map_power(umap, &exact);
661 list->obj[1].type = isl_obj_bool;
662 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
663 if (exact < 0 || !list->obj[0].v)
664 goto error;
666 return list;
667 error2:
668 isl_union_map_free(umap);
669 error:
670 isl_list_free(list);
671 return NULL;
674 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
675 __isl_take isl_union_pw_qpolynomial *upwqp)
677 isl_ctx *ctx;
678 struct isl_list *list;
679 int tight;
681 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
682 list = isl_list_alloc(ctx, 2);
683 if (!list)
684 goto error2;
686 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
687 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
688 isl_fold_max, &tight);
689 list->obj[1].type = isl_obj_bool;
690 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
691 if (tight < 0 || !list->obj[0].v)
692 goto error;
694 return list;
695 error2:
696 isl_union_pw_qpolynomial_free(upwqp);
697 error:
698 isl_list_free(list);
699 return NULL;
702 #ifdef HAVE_PET
703 static __isl_give isl_list *parse(__isl_take isl_str *str)
705 isl_ctx *ctx;
706 struct isl_list *list;
707 struct pet_scop *scop;
708 isl_union_map *sched, *reads, *writes;
709 isl_union_set *domain;
710 struct iscc_options *options;
712 if (!str)
713 return NULL;
714 ctx = str->ctx;
716 options = isl_ctx_peek_iscc_options(ctx);
717 if (!options || !options->io) {
718 isl_str_free(str);
719 isl_die(ctx, isl_error_invalid,
720 "parse_file operation not allowed", return NULL);
723 list = isl_list_alloc(ctx, 4);
724 if (!list)
725 goto error;
727 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
728 domain = pet_scop_collect_domains(scop);
729 sched = pet_scop_collect_schedule(scop);
730 reads = pet_scop_collect_reads(scop);
731 writes = pet_scop_collect_writes(scop);
732 pet_scop_free(scop);
734 list->obj[0].type = isl_obj_union_set;
735 list->obj[0].v = domain;
736 list->obj[1].type = isl_obj_union_map;
737 list->obj[1].v = writes;
738 list->obj[2].type = isl_obj_union_map;
739 list->obj[2].v = reads;
740 list->obj[3].type = isl_obj_union_map;
741 list->obj[3].v = sched;
743 if (!list->obj[0].v || !list->obj[1].v ||
744 !list->obj[2].v || !list->obj[3].v)
745 goto error;
747 isl_str_free(str);
748 return list;
749 error:
750 isl_list_free(list);
751 isl_str_free(str);
752 return NULL;
754 #endif
756 static int add_point(__isl_take isl_point *pnt, void *user)
758 isl_union_set **scan = (isl_union_set **) user;
760 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
762 return 0;
765 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
767 isl_union_set *scan;
769 scan = isl_union_set_empty(isl_union_set_get_space(uset));
771 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
772 isl_union_set_free(scan);
773 return uset;
776 isl_union_set_free(uset);
777 return scan;
780 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
782 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
785 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
786 __isl_take isl_union_pw_qpolynomial *upwqp)
788 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
791 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
792 __isl_take isl_union_pw_qpolynomial *upwqp)
794 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
797 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
798 __isl_take isl_union_pw_qpolynomial *upwqp)
800 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
803 typedef void *(*isc_un_op_fn)(void *arg);
804 struct isc_un_op {
805 enum isl_token_type op;
806 isl_obj_type arg;
807 isl_obj_type res;
808 isc_un_op_fn fn;
810 struct isc_named_un_op {
811 char *name;
812 struct isc_un_op op;
814 struct isc_named_un_op named_un_ops[] = {
815 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
816 (isc_un_op_fn) &isl_union_map_affine_hull } },
817 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
818 (isc_un_op_fn) &isl_union_set_affine_hull } },
819 {"card", { -1, isl_obj_union_set,
820 isl_obj_union_pw_qpolynomial,
821 (isc_un_op_fn) &isl_union_set_card } },
822 {"card", { -1, isl_obj_union_map,
823 isl_obj_union_pw_qpolynomial,
824 (isc_un_op_fn) &isl_union_map_card } },
825 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
826 (isc_un_op_fn) &isl_union_set_coalesce } },
827 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
828 (isc_un_op_fn) &isl_union_map_coalesce } },
829 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
830 isl_obj_union_pw_qpolynomial,
831 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
832 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
833 isl_obj_union_pw_qpolynomial_fold,
834 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
835 {"coefficients", { -1, isl_obj_union_set,
836 isl_obj_union_set,
837 (isc_un_op_fn) &isl_union_set_coefficients } },
838 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
839 (isc_un_op_fn) &isl_union_set_solutions } },
840 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
841 (isc_un_op_fn) &isl_union_map_deltas } },
842 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
843 (isc_un_op_fn) &isl_union_map_deltas_map } },
844 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
845 (isc_un_op_fn) &isl_union_map_domain } },
846 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
847 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
848 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
849 isl_obj_union_set,
850 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
851 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
852 (isc_un_op_fn) &isl_union_map_domain } },
853 {"domain", { -1, isl_obj_union_pw_qpolynomial,
854 isl_obj_union_set,
855 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
856 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
857 isl_obj_union_set,
858 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
859 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
860 (isc_un_op_fn) &isl_union_map_domain_map } },
861 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
862 (isc_un_op_fn) &isl_union_map_range } },
863 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
864 (isc_un_op_fn) &isl_union_map_range } },
865 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
866 (isc_un_op_fn) &isl_union_map_range_map } },
867 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
868 (isc_un_op_fn) &isl_union_set_identity } },
869 {"lattice_width", { -1, isl_obj_union_set,
870 isl_obj_union_pw_qpolynomial,
871 (isc_un_op_fn) &isl_union_set_lattice_width } },
872 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
873 (isc_un_op_fn) &isl_union_map_lexmin } },
874 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
875 (isc_un_op_fn) &isl_union_map_lexmax } },
876 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
877 (isc_un_op_fn) &isl_union_set_lexmin } },
878 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
879 (isc_un_op_fn) &isl_union_set_lexmax } },
880 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
881 (isc_un_op_fn) &isl_union_set_lift } },
882 {"params", { -1, isl_obj_union_map, isl_obj_set,
883 (isc_un_op_fn) &isl_union_map_params } },
884 {"params", { -1, isl_obj_union_set, isl_obj_set,
885 (isc_un_op_fn) &isl_union_set_params } },
886 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
887 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
888 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
889 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
890 {"poly", { -1, isl_obj_union_pw_qpolynomial,
891 isl_obj_union_pw_qpolynomial,
892 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
893 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
894 isl_obj_union_pw_qpolynomial,
895 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
896 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
897 isl_obj_union_pw_qpolynomial,
898 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
899 #ifdef HAVE_PET
900 {"parse_file", { -1, isl_obj_str, isl_obj_list,
901 (isc_un_op_fn) &parse } },
902 #endif
903 {"pow", { -1, isl_obj_union_map, isl_obj_list,
904 (isc_un_op_fn) &union_map_power } },
905 {"sample", { -1, isl_obj_union_set, isl_obj_set,
906 (isc_un_op_fn) &union_set_sample } },
907 {"sample", { -1, isl_obj_union_map, isl_obj_map,
908 (isc_un_op_fn) &union_map_sample } },
909 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
910 (isc_un_op_fn) &union_set_scan } },
911 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
912 (isc_un_op_fn) &union_map_scan } },
913 {"sum", { -1, isl_obj_union_pw_qpolynomial,
914 isl_obj_union_pw_qpolynomial,
915 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
916 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
917 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
918 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
919 (isc_un_op_fn) &isl_union_set_unwrap } },
920 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
921 (isc_un_op_fn) &isl_union_map_wrap } },
922 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
923 (isc_un_op_fn) &isl_union_map_zip } },
924 NULL
927 struct isl_named_obj {
928 char *name;
929 struct isl_obj obj;
932 static void free_obj(struct isl_obj obj)
934 obj.type->free(obj.v);
937 static int same_name(const void *entry, const void *val)
939 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
941 return !strcmp(named->name, val);
944 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
945 char *name, struct isl_obj obj)
947 struct isl_hash_table_entry *entry;
948 uint32_t name_hash;
949 struct isl_named_obj *named;
951 name_hash = isl_hash_string(isl_hash_init(), name);
952 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
953 if (!entry)
954 goto error;
955 if (entry->data) {
956 named = entry->data;
957 free_obj(named->obj);
958 free(name);
959 } else {
960 named = isl_alloc_type(ctx, struct isl_named_obj);
961 if (!named)
962 goto error;
963 named->name = name;
964 entry->data = named;
966 named->obj = obj;
968 return 0;
969 error:
970 free_obj(obj);
971 free(name);
972 return -1;
975 static struct isl_obj stored_obj(struct isl_ctx *ctx,
976 struct isl_hash_table *table, char *name)
978 struct isl_obj obj = { isl_obj_none, NULL };
979 struct isl_hash_table_entry *entry;
980 uint32_t name_hash;
982 name_hash = isl_hash_string(isl_hash_init(), name);
983 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
984 if (entry) {
985 struct isl_named_obj *named;
986 named = entry->data;
987 obj = named->obj;
988 } else if (isdigit(name[0]))
989 fprintf(stderr, "unknown identifier '$%s'\n", name);
990 else
991 fprintf(stderr, "unknown identifier '%s'\n", name);
993 free(name);
994 obj.v = obj.type->copy(obj.v);
995 return obj;
998 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1000 if (obj.type == super)
1001 return 1;
1002 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1003 return 1;
1004 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1005 return 1;
1006 if (obj.type == isl_obj_pw_qpolynomial &&
1007 super == isl_obj_union_pw_qpolynomial)
1008 return 1;
1009 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1010 super == isl_obj_union_pw_qpolynomial_fold)
1011 return 1;
1012 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1013 return 1;
1014 if (obj.type == isl_obj_list) {
1015 struct isl_list *list = obj.v;
1016 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1017 return is_subtype(list->obj[0], super);
1019 if (super == isl_obj_str)
1020 return 1;
1021 return 0;
1024 static struct isl_obj obj_at(struct isl_obj obj, int i)
1026 struct isl_list *list = obj.v;
1028 obj = list->obj[i];
1029 obj.v = obj.type->copy(obj.v);
1031 isl_list_free(list);
1033 return obj;
1036 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1037 isl_obj_type type)
1039 if (obj.type == type)
1040 return obj;
1041 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1042 obj.type = isl_obj_union_map;
1043 obj.v = isl_union_map_from_map(obj.v);
1044 return obj;
1046 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1047 obj.type = isl_obj_union_set;
1048 obj.v = isl_union_set_from_set(obj.v);
1049 return obj;
1051 if (obj.type == isl_obj_pw_qpolynomial &&
1052 type == isl_obj_union_pw_qpolynomial) {
1053 obj.type = isl_obj_union_pw_qpolynomial;
1054 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1055 return obj;
1057 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1058 type == isl_obj_union_pw_qpolynomial_fold) {
1059 obj.type = isl_obj_union_pw_qpolynomial_fold;
1060 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1061 return obj;
1063 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1064 if (type == isl_obj_union_map) {
1065 obj.type = isl_obj_union_map;
1066 return obj;
1068 if (type == isl_obj_union_pw_qpolynomial) {
1069 isl_space *dim = isl_union_set_get_space(obj.v);
1070 isl_union_set_free(obj.v);
1071 obj.v = isl_union_pw_qpolynomial_zero(dim);
1072 obj.type = isl_obj_union_pw_qpolynomial;
1073 return obj;
1075 if (type == isl_obj_union_pw_qpolynomial_fold) {
1076 isl_space *dim = isl_union_set_get_space(obj.v);
1077 isl_union_set_free(obj.v);
1078 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1079 isl_fold_list);
1080 obj.type = isl_obj_union_pw_qpolynomial_fold;
1081 return obj;
1084 if (obj.type == isl_obj_list) {
1085 struct isl_list *list = obj.v;
1086 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1087 return convert(ctx, obj_at(obj, 0), type);
1089 if (type == isl_obj_str) {
1090 isl_str *str;
1091 isl_printer *p;
1092 char *s;
1094 p = isl_printer_to_str(ctx);
1095 if (!p)
1096 goto error;
1097 p = obj.type->print(p, obj.v);
1098 s = isl_printer_get_str(p);
1099 isl_printer_free(p);
1101 str = isl_str_from_string(ctx, s);
1102 if (!str)
1103 goto error;
1104 free_obj(obj);
1105 obj.v = str;
1106 obj.type = isl_obj_str;
1107 return obj;
1110 error:
1111 free_obj(obj);
1112 obj.type = isl_obj_none;
1113 obj.v = NULL;
1114 return obj;
1117 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1118 struct isl_obj lhs)
1120 int i;
1121 struct isl_token *tok;
1123 tok = isl_stream_next_token(s);
1124 if (!tok)
1125 return NULL;
1127 for (i = 0; ; ++i) {
1128 if (!bin_ops[i].op)
1129 break;
1130 if (bin_ops[i].op != tok->type)
1131 continue;
1132 if (!is_subtype(lhs, bin_ops[i].lhs))
1133 continue;
1135 isl_token_free(tok);
1136 return &bin_ops[i];
1139 for (i = 0; ; ++i) {
1140 if (!named_bin_ops[i].name)
1141 break;
1142 if (named_bin_ops[i].op.op != tok->type)
1143 continue;
1144 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1145 continue;
1147 isl_token_free(tok);
1148 return &named_bin_ops[i].op;
1151 isl_stream_push_token(s, tok);
1153 return NULL;
1156 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1158 int i;
1159 struct isl_token *tok;
1161 tok = isl_stream_next_token(s);
1162 if (!tok)
1163 return NULL;
1165 for (i = 0; ; ++i) {
1166 if (!named_un_ops[i].name)
1167 break;
1168 if (named_un_ops[i].op.op != tok->type)
1169 continue;
1171 isl_token_free(tok);
1172 return &named_un_ops[i].op;
1175 isl_stream_push_token(s, tok);
1177 return NULL;
1180 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1181 struct isl_obj arg)
1183 int i;
1185 for (i = 0; ; ++i) {
1186 if (!named_un_ops[i].name)
1187 break;
1188 if (named_un_ops[i].op.op != like->op)
1189 continue;
1190 if (!is_subtype(arg, named_un_ops[i].op.arg))
1191 continue;
1193 return &named_un_ops[i].op;
1196 return NULL;
1199 static int is_assign(struct isl_stream *s)
1201 struct isl_token *tok;
1202 struct isl_token *tok2;
1203 int assign;
1205 tok = isl_stream_next_token(s);
1206 if (!tok)
1207 return 0;
1208 if (tok->type != ISL_TOKEN_IDENT) {
1209 isl_stream_push_token(s, tok);
1210 return 0;
1213 tok2 = isl_stream_next_token(s);
1214 if (!tok2) {
1215 isl_stream_push_token(s, tok);
1216 return 0;
1218 assign = tok2->type == ISL_TOKEN_DEF;
1219 isl_stream_push_token(s, tok2);
1220 isl_stream_push_token(s, tok);
1222 return assign;
1225 static struct isl_obj read_obj(struct isl_stream *s,
1226 struct isl_hash_table *table);
1227 static struct isl_obj read_expr(struct isl_stream *s,
1228 struct isl_hash_table *table);
1230 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1231 struct isl_hash_table *table, struct isc_un_op *op)
1233 struct isl_obj obj = { isl_obj_none, NULL };
1235 obj = read_obj(s, table);
1236 if (!obj.v)
1237 goto error;
1239 op = find_matching_un_op(op, obj);
1241 if (!op)
1242 isl_die(s->ctx, isl_error_invalid,
1243 "no such unary operator defined on given operand",
1244 goto error);
1246 obj = convert(s->ctx, obj, op->arg);
1247 obj.v = op->fn(obj.v);
1248 obj.type = op->res;
1250 return obj;
1251 error:
1252 free_obj(obj);
1253 obj.type = isl_obj_none;
1254 obj.v = NULL;
1255 return obj;
1258 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1260 struct isl_list *list;
1261 int exact;
1263 if (obj.type != isl_obj_union_map)
1264 obj = convert(ctx, obj, isl_obj_union_map);
1265 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1266 list = isl_list_alloc(ctx, 2);
1267 if (!list)
1268 goto error;
1270 list->obj[0].type = isl_obj_union_map;
1271 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1272 list->obj[1].type = isl_obj_bool;
1273 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1274 obj.v = list;
1275 obj.type = isl_obj_list;
1276 if (exact < 0 || !list->obj[0].v)
1277 goto error;
1279 return obj;
1280 error:
1281 free_obj(obj);
1282 obj.type = isl_obj_none;
1283 obj.v = NULL;
1284 return obj;
1287 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1289 struct isl_list *list = obj.v;
1290 struct isl_token *tok;
1291 int i;
1293 tok = isl_stream_next_token(s);
1294 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1295 isl_stream_error(s, tok, "expecting index");
1296 if (tok)
1297 isl_stream_push_token(s, tok);
1298 goto error;
1300 i = isl_int_get_si(tok->u.v);
1301 isl_token_free(tok);
1302 isl_assert(s->ctx, i < list->n, goto error);
1303 if (isl_stream_eat(s, ']'))
1304 goto error;
1306 return obj_at(obj, i);
1307 error:
1308 free_obj(obj);
1309 obj.type = isl_obj_none;
1310 obj.v = NULL;
1311 return obj;
1314 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1315 struct isl_hash_table *table)
1317 struct isl_obj obj;
1319 obj = read_expr(s, table);
1320 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1321 is_subtype(obj, isl_obj_union_map), goto error);
1323 if (obj.type == isl_obj_list) {
1324 struct isl_list *list = obj.v;
1325 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1326 obj = obj_at(obj, 0);
1328 if (obj.type == isl_obj_set)
1329 obj = convert(s->ctx, obj, isl_obj_union_set);
1330 else if (obj.type == isl_obj_map)
1331 obj = convert(s->ctx, obj, isl_obj_union_map);
1332 if (obj.type == isl_obj_union_set) {
1333 obj.v = isl_union_set_apply(obj.v, umap);
1334 } else
1335 obj.v = isl_union_map_apply_range(obj.v, umap);
1336 if (!obj.v)
1337 goto error2;
1339 if (isl_stream_eat(s, ')'))
1340 goto error2;
1342 return obj;
1343 error:
1344 isl_union_map_free(umap);
1345 error2:
1346 free_obj(obj);
1347 obj.type = isl_obj_none;
1348 obj.v = NULL;
1349 return obj;
1352 static struct isl_obj apply_fun_set(struct isl_obj obj,
1353 __isl_take isl_union_set *uset)
1355 if (obj.type == isl_obj_union_pw_qpolynomial) {
1356 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1357 } else {
1358 obj.type = isl_obj_list;
1359 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1361 return obj;
1364 static struct isl_obj apply_fun_map(struct isl_obj obj,
1365 __isl_take isl_union_map *umap)
1367 if (obj.type == isl_obj_union_pw_qpolynomial) {
1368 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1369 } else {
1370 obj.type = isl_obj_list;
1371 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1373 return obj;
1376 static struct isl_obj apply_fun(struct isl_stream *s,
1377 struct isl_obj obj, struct isl_hash_table *table)
1379 struct isl_obj arg;
1381 arg = read_expr(s, table);
1382 if (!is_subtype(arg, isl_obj_union_map) &&
1383 !is_subtype(arg, isl_obj_union_set))
1384 isl_die(s->ctx, isl_error_invalid,
1385 "expecting set of map argument", goto error);
1387 if (arg.type == isl_obj_list) {
1388 struct isl_list *list = arg.v;
1389 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1390 arg = obj_at(arg, 0);
1392 if (arg.type == isl_obj_set)
1393 arg = convert(s->ctx, arg, isl_obj_union_set);
1394 else if (arg.type == isl_obj_map)
1395 arg = convert(s->ctx, arg, isl_obj_union_map);
1396 if (arg.type == isl_obj_union_set)
1397 obj = apply_fun_set(obj, arg.v);
1398 else
1399 obj = apply_fun_map(obj, arg.v);
1400 if (!obj.v)
1401 goto error2;
1403 if (isl_stream_eat(s, ')'))
1404 goto error2;
1406 return obj;
1407 error:
1408 free_obj(arg);
1409 error2:
1410 free_obj(obj);
1411 obj.type = isl_obj_none;
1412 obj.v = NULL;
1413 return obj;
1416 struct add_vertex_data {
1417 struct isl_list *list;
1418 int i;
1421 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1423 struct add_vertex_data *data = (struct add_vertex_data *)user;
1424 isl_basic_set *expr;
1426 expr = isl_vertex_get_expr(vertex);
1428 data->list->obj[data->i].type = isl_obj_set;
1429 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1430 data->i++;
1432 isl_vertex_free(vertex);
1434 return 0;
1437 static int set_vertices(__isl_take isl_set *set, void *user)
1439 isl_ctx *ctx;
1440 isl_basic_set *hull;
1441 isl_vertices *vertices = NULL;
1442 struct isl_list *list = NULL;
1443 int r;
1444 struct add_vertex_data *data = (struct add_vertex_data *)user;
1446 set = isl_set_remove_divs(set);
1447 hull = isl_set_convex_hull(set);
1448 vertices = isl_basic_set_compute_vertices(hull);
1449 isl_basic_set_free(hull);
1451 list = data->list;
1453 ctx = isl_vertices_get_ctx(vertices);
1454 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1455 if (!data->list)
1456 goto error;
1458 data->i = 0;
1459 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1461 data->list = isl_list_concat(list, data->list);
1463 isl_vertices_free(vertices);
1465 return r;
1466 error:
1467 data->list = list;
1468 isl_vertices_free(vertices);
1469 return -1;
1472 static struct isl_obj vertices(struct isl_stream *s,
1473 struct isl_hash_table *table)
1475 isl_ctx *ctx;
1476 struct isl_obj obj;
1477 struct isl_list *list = NULL;
1478 isl_union_set *uset;
1479 struct add_vertex_data data = { NULL };
1481 obj = read_expr(s, table);
1482 obj = convert(s->ctx, obj, isl_obj_union_set);
1483 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1484 uset = obj.v;
1485 obj.v = NULL;
1487 ctx = isl_union_set_get_ctx(uset);
1488 list = isl_list_alloc(ctx, 0);
1489 if (!list)
1490 goto error;
1492 data.list = list;
1494 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1495 goto error;
1497 isl_union_set_free(uset);
1499 obj.type = isl_obj_list;
1500 obj.v = data.list;
1502 return obj;
1503 error:
1504 isl_union_set_free(uset);
1505 isl_list_free(data.list);
1506 free_obj(obj);
1507 obj.type = isl_obj_none;
1508 obj.v = NULL;
1509 return obj;
1512 static struct isl_obj type_of(struct isl_stream *s,
1513 struct isl_hash_table *table)
1515 isl_ctx *ctx;
1516 struct isl_obj obj;
1517 const char *type = "unknown";
1519 obj = read_expr(s, table);
1521 if (obj.type == isl_obj_map ||
1522 obj.type == isl_obj_union_map)
1523 type = "map";
1524 if (obj.type == isl_obj_set ||
1525 obj.type == isl_obj_union_set)
1526 type = "set";
1527 if (obj.type == isl_obj_pw_qpolynomial ||
1528 obj.type == isl_obj_union_pw_qpolynomial)
1529 type = "piecewise quasipolynomial";
1530 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1531 obj.type == isl_obj_union_pw_qpolynomial_fold)
1532 type = "piecewise quasipolynomial fold";
1533 if (obj.type == isl_obj_list)
1534 type = "list";
1535 if (obj.type == isl_obj_bool)
1536 type = "boolean";
1537 if (obj.type == isl_obj_str)
1538 type = "string";
1539 if (obj.type == isl_obj_int)
1540 type = "int";
1542 free_obj(obj);
1543 obj.type = isl_obj_str;
1544 obj.v = isl_str_from_string(s->ctx, strdup(type));
1546 return obj;
1549 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1550 struct isl_hash_table *table)
1552 struct isl_obj obj;
1554 obj = read_obj(s, table);
1555 obj = convert(s->ctx, obj, isl_obj_union_set);
1556 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1557 return obj.v;
1558 error:
1559 free_obj(obj);
1560 return NULL;
1563 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1564 struct isl_hash_table *table)
1566 struct isl_obj obj;
1568 obj = read_obj(s, table);
1569 obj = convert(s->ctx, obj, isl_obj_union_map);
1570 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1571 return obj.v;
1572 error:
1573 free_obj(obj);
1574 return NULL;
1577 static struct isl_obj last_any(struct isl_stream *s,
1578 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1579 __isl_take isl_union_map *may_source)
1581 struct isl_obj obj = { isl_obj_none, NULL };
1582 isl_union_map *sink = NULL;
1583 isl_union_map *schedule = NULL;
1584 isl_union_map *may_dep;
1585 isl_union_map *must_dep;
1587 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1588 goto error;
1590 sink = read_map(s, table);
1591 if (!sink)
1592 goto error;
1594 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1595 goto error;
1597 schedule = read_map(s, table);
1598 if (!schedule)
1599 goto error;
1601 if (isl_union_map_compute_flow(sink, must_source, may_source,
1602 schedule, &must_dep, &may_dep,
1603 NULL, NULL) < 0)
1604 return obj;
1606 obj.type = isl_obj_union_map;
1607 obj.v = isl_union_map_union(must_dep, may_dep);
1609 return obj;
1610 error:
1611 isl_union_map_free(may_source);
1612 isl_union_map_free(must_source);
1613 isl_union_map_free(sink);
1614 isl_union_map_free(schedule);
1615 free_obj(obj);
1616 obj.type = isl_obj_none;
1617 obj.v = NULL;
1618 return obj;
1621 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1623 struct isl_obj obj = { isl_obj_none, NULL };
1624 isl_union_map *must_source = NULL;
1625 isl_union_map *may_source = NULL;
1626 isl_union_map *sink = NULL;
1627 isl_union_map *schedule = NULL;
1628 isl_union_map *may_dep;
1630 may_source = read_map(s, table);
1631 if (!may_source)
1632 goto error;
1634 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1635 must_source = read_map(s, table);
1636 if (!must_source)
1637 goto error;
1638 return last_any(s, table, must_source, may_source);
1641 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1642 goto error;
1644 sink = read_map(s, table);
1645 if (!sink)
1646 goto error;
1648 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1649 goto error;
1651 schedule = read_map(s, table);
1652 if (!schedule)
1653 goto error;
1655 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1656 if (isl_union_map_compute_flow(sink, must_source, may_source,
1657 schedule, NULL, &may_dep,
1658 NULL, NULL) < 0)
1659 return obj;
1661 obj.type = isl_obj_union_map;
1662 obj.v = may_dep;
1664 return obj;
1665 error:
1666 isl_union_map_free(may_source);
1667 isl_union_map_free(must_source);
1668 isl_union_map_free(sink);
1669 isl_union_map_free(schedule);
1670 free_obj(obj);
1671 obj.type = isl_obj_none;
1672 obj.v = NULL;
1673 return obj;
1676 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1678 struct isl_obj obj = { isl_obj_none, NULL };
1679 struct isl_list *list = NULL;
1680 isl_union_map *must_source = NULL;
1681 isl_union_map *may_source = NULL;
1682 isl_union_map *sink = NULL;
1683 isl_union_map *schedule = NULL;
1684 isl_union_map *must_dep;
1685 isl_union_map *must_no_source;
1687 must_source = read_map(s, table);
1688 if (!must_source)
1689 goto error;
1691 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1692 may_source = read_map(s, table);
1693 if (!may_source)
1694 goto error;
1695 return last_any(s, table, must_source, may_source);
1698 list = isl_list_alloc(s->ctx, 2);
1699 if (!list)
1700 goto error;
1702 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1703 goto error;
1705 sink = read_map(s, table);
1706 if (!sink)
1707 goto error;
1709 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1710 goto error;
1712 schedule = read_map(s, table);
1713 if (!schedule)
1714 goto error;
1716 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1717 if (isl_union_map_compute_flow(sink, must_source, may_source,
1718 schedule, &must_dep, NULL,
1719 &must_no_source, NULL) < 0) {
1720 isl_list_free(list);
1721 return obj;
1724 list->obj[0].type = isl_obj_union_map;
1725 list->obj[0].v = must_dep;
1726 list->obj[1].type = isl_obj_union_map;
1727 list->obj[1].v = must_no_source;
1729 obj.v = list;
1730 obj.type = isl_obj_list;
1732 return obj;
1733 error:
1734 isl_list_free(list);
1735 isl_union_map_free(may_source);
1736 isl_union_map_free(must_source);
1737 isl_union_map_free(sink);
1738 isl_union_map_free(schedule);
1739 free_obj(obj);
1740 obj.type = isl_obj_none;
1741 obj.v = NULL;
1742 return obj;
1745 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1746 struct isl_hash_table *table)
1748 isl_union_set *domain;
1749 isl_union_map *validity;
1750 isl_union_map *proximity;
1752 domain = read_set(s, table);
1753 if (!domain)
1754 return NULL;
1756 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1757 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1759 for (;;) {
1760 isl_union_map *umap;
1761 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1762 umap = read_map(s, table);
1763 validity = isl_union_map_union(validity, umap);
1764 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1765 umap = read_map(s, table);
1766 proximity = isl_union_map_union(proximity, umap);
1767 } else
1768 break;
1771 return isl_union_set_compute_schedule(domain, validity, proximity);
1774 static struct isl_obj schedule(struct isl_stream *s,
1775 struct isl_hash_table *table)
1777 struct isl_obj obj = { isl_obj_none, NULL };
1778 isl_schedule *schedule;
1780 schedule = get_schedule(s, table);
1782 obj.v = isl_schedule_get_map(schedule);
1783 obj.type = isl_obj_union_map;
1785 isl_schedule_free(schedule);
1787 return obj;
1790 /* Read a schedule for code generation.
1791 * If the input is a set rather than a map, then we construct
1792 * an identity schedule on the given set.
1794 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1795 struct isl_hash_table *table)
1797 struct isl_obj obj;
1799 obj = read_obj(s, table);
1801 if (is_subtype(obj, isl_obj_union_map)) {
1802 obj = convert(s->ctx, obj, isl_obj_union_map);
1803 return obj.v;
1806 if (is_subtype(obj, isl_obj_union_set)) {
1807 obj = convert(s->ctx, obj, isl_obj_union_set);
1808 return isl_union_set_identity(obj.v);
1811 free_obj(obj);
1812 isl_die(s->ctx, isl_error_invalid, "expecting set or map", return NULL);
1815 /* Generate an AST for the given schedule and options and print
1816 * the AST on the printer.
1818 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1819 __isl_take isl_union_map *schedule,
1820 __isl_take isl_union_map *options)
1822 isl_space *space;
1823 isl_set *context;
1824 isl_ast_build *build;
1825 isl_ast_node *tree;
1826 int format;
1828 space = isl_union_map_get_space(schedule);
1829 context = isl_set_universe(isl_space_params(space));
1831 build = isl_ast_build_from_context(context);
1832 build = isl_ast_build_set_options(build, options);
1833 tree = isl_ast_build_ast_from_schedule(build, schedule);
1834 isl_ast_build_free(build);
1836 if (!tree)
1837 return p;
1839 format = isl_printer_get_output_format(p);
1840 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1841 p = isl_printer_print_ast_node(p, tree);
1842 p = isl_printer_set_output_format(p, format);
1844 isl_ast_node_free(tree);
1846 return p;
1849 /* Perform the codegen operation.
1850 * In particular, read a schedule, check if the user has specified any options
1851 * and then generate an AST from the schedule (and options) and print it.
1853 static __isl_give isl_printer *codegen(struct isl_stream *s,
1854 struct isl_hash_table *table, __isl_take isl_printer *p)
1856 isl_union_map *schedule;
1857 isl_union_map *options;
1859 schedule = get_codegen_schedule(s, table);
1860 if (!schedule)
1861 return p;
1863 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1864 options = read_map(s, table);
1865 else
1866 options = isl_union_map_empty(
1867 isl_union_map_get_space(schedule));
1869 p = print_code(p, schedule, options);
1871 isl_stream_eat(s, ';');
1873 return p;
1876 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1878 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1880 struct isl_obj obj = { isl_obj_none, NULL };
1881 isl_ctx *ctx = isl_band_get_ctx(band);
1882 struct isl_list *list;
1884 list = isl_list_alloc(ctx, 2);
1885 if (!list)
1886 goto error;
1888 obj.v = list;
1889 obj.type = isl_obj_list;
1891 list->obj[0].type = isl_obj_union_map;
1892 list->obj[0].v = isl_band_get_partial_schedule(band);
1894 if (isl_band_has_children(band)) {
1895 isl_band_list *children;
1897 children = isl_band_get_children(band);
1898 list->obj[1] = band_list_to_obj_list(children);
1899 } else {
1900 list->obj[1].type = isl_obj_list;
1901 list->obj[1].v = isl_list_alloc(ctx, 0);
1904 if (!list->obj[0].v || !list->obj[1].v)
1905 goto error;
1907 isl_band_free(band);
1909 return obj;
1910 error:
1911 isl_band_free(band);
1912 free_obj(obj);
1913 obj.type = isl_obj_none;
1914 obj.v = NULL;
1915 return obj;
1918 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1920 struct isl_obj obj = { isl_obj_none, NULL };
1921 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1922 struct isl_list *list;
1923 int i, n;
1925 n = isl_band_list_n_band(bands);
1926 list = isl_list_alloc(ctx, n);
1927 if (!list)
1928 goto error;
1930 obj.v = list;
1931 obj.type = isl_obj_list;
1933 for (i = 0; i < n; ++i) {
1934 isl_band *band;
1936 band = isl_band_list_get_band(bands, i);
1937 list->obj[i] = band_to_obj_list(band);
1938 if (!list->obj[i].v)
1939 goto error;
1942 isl_band_list_free(bands);
1944 return obj;
1945 error:
1946 isl_band_list_free(bands);
1947 free_obj(obj);
1948 obj.type = isl_obj_none;
1949 obj.v = NULL;
1950 return obj;
1953 static struct isl_obj schedule_forest(struct isl_stream *s,
1954 struct isl_hash_table *table)
1956 struct isl_obj obj = { isl_obj_none, NULL };
1957 isl_schedule *schedule;
1958 isl_band_list *roots;
1960 schedule = get_schedule(s, table);
1961 if (!schedule)
1962 return obj;
1964 roots = isl_schedule_get_band_forest(schedule);
1965 isl_schedule_free(schedule);
1967 return band_list_to_obj_list(roots);
1970 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1972 struct isl_token *tok;
1974 if (isl_stream_eat_if_available(s, '+'))
1975 return transitive_closure(s->ctx, obj);
1977 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1978 if (obj.type != isl_obj_union_map)
1979 obj = convert(s->ctx, obj, isl_obj_union_map);
1981 tok = isl_stream_next_token(s);
1982 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_is_zero(tok->u.v)) {
1983 isl_stream_error(s, tok, "expecting non-zero integer exponent");
1984 if (tok)
1985 isl_stream_push_token(s, tok);
1986 goto error;
1989 obj.v = isl_union_map_fixed_power(obj.v, tok->u.v);
1990 isl_token_free(tok);
1991 if (!obj.v)
1992 goto error;
1994 return obj;
1995 error:
1996 free_obj(obj);
1997 obj.type = isl_obj_none;
1998 obj.v = NULL;
1999 return obj;
2002 static struct isl_obj check_assert(struct isl_stream *s,
2003 struct isl_hash_table *table)
2005 struct isl_obj obj;
2007 obj = read_expr(s, table);
2008 if (obj.type != isl_obj_bool)
2009 isl_die(s->ctx, isl_error_invalid,
2010 "expecting boolean expression", goto error);
2011 if (obj.v != &isl_bool_true)
2012 isl_die(s->ctx, isl_error_unknown,
2013 "assertion failed", abort());
2014 error:
2015 free_obj(obj);
2016 obj.type = isl_obj_none;
2017 obj.v = NULL;
2018 return obj;
2021 static struct isl_obj read_from_file(struct isl_stream *s)
2023 struct isl_obj obj;
2024 struct isl_token *tok;
2025 struct isl_stream *s_file;
2026 struct iscc_options *options;
2027 FILE *file;
2029 tok = isl_stream_next_token(s);
2030 if (!tok || tok->type != ISL_TOKEN_STRING) {
2031 isl_stream_error(s, tok, "expecting filename");
2032 isl_token_free(tok);
2033 goto error;
2036 options = isl_ctx_peek_iscc_options(s->ctx);
2037 if (!options || !options->io) {
2038 isl_token_free(tok);
2039 isl_die(s->ctx, isl_error_invalid,
2040 "read operation not allowed", goto error);
2043 file = fopen(tok->u.s, "r");
2044 isl_token_free(tok);
2045 isl_assert(s->ctx, file, goto error);
2047 s_file = isl_stream_new_file(s->ctx, file);
2048 if (!s_file) {
2049 fclose(file);
2050 goto error;
2053 obj = isl_stream_read_obj(s_file);
2055 isl_stream_free(s_file);
2056 fclose(file);
2058 return obj;
2059 error:
2060 obj.type = isl_obj_none;
2061 obj.v = NULL;
2062 return obj;
2065 static struct isl_obj write_to_file(struct isl_stream *s,
2066 struct isl_hash_table *table)
2068 struct isl_obj obj;
2069 struct isl_token *tok;
2070 struct isl_stream *s_file;
2071 struct iscc_options *options;
2072 FILE *file;
2073 isl_printer *p;
2075 tok = isl_stream_next_token(s);
2076 if (!tok || tok->type != ISL_TOKEN_STRING) {
2077 isl_stream_error(s, tok, "expecting filename");
2078 isl_token_free(tok);
2079 goto error;
2082 obj = read_expr(s, table);
2084 options = isl_ctx_peek_iscc_options(s->ctx);
2085 if (!options || !options->io) {
2086 isl_token_free(tok);
2087 isl_die(s->ctx, isl_error_invalid,
2088 "write operation not allowed", goto error);
2091 file = fopen(tok->u.s, "w");
2092 isl_token_free(tok);
2093 if (!file)
2094 isl_die(s->ctx, isl_error_unknown,
2095 "could not open file for writing", goto error);
2097 p = isl_printer_to_file(s->ctx, file);
2098 p = isl_printer_set_output_format(p, options->format);
2099 p = obj.type->print(p, obj.v);
2100 p = isl_printer_end_line(p);
2101 isl_printer_free(p);
2103 fclose(file);
2104 error:
2105 free_obj(obj);
2106 obj.type = isl_obj_none;
2107 obj.v = NULL;
2108 return obj;
2111 static struct isl_obj read_string_if_available(struct isl_stream *s)
2113 struct isl_token *tok;
2114 struct isl_obj obj = { isl_obj_none, NULL };
2116 tok = isl_stream_next_token(s);
2117 if (!tok)
2118 return obj;
2119 if (tok->type == ISL_TOKEN_STRING) {
2120 isl_str *str;
2121 str = isl_str_alloc(s->ctx);
2122 if (!str)
2123 goto error;
2124 str->s = strdup(tok->u.s);
2125 isl_token_free(tok);
2126 obj.v = str;
2127 obj.type = isl_obj_str;
2128 } else
2129 isl_stream_push_token(s, tok);
2130 return obj;
2131 error:
2132 isl_token_free(tok);
2133 return obj;
2136 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2138 struct isl_token *tok;
2139 struct isl_obj obj = { isl_obj_none, NULL };
2141 tok = isl_stream_next_token(s);
2142 if (!tok)
2143 return obj;
2144 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2145 int is_true = tok->type == ISL_TOKEN_TRUE;
2146 isl_token_free(tok);
2147 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2148 obj.type = isl_obj_bool;
2149 } else
2150 isl_stream_push_token(s, tok);
2151 return obj;
2154 static __isl_give char *read_ident(struct isl_stream *s)
2156 char *name;
2157 struct isl_token *tok, *tok2;
2159 name = isl_stream_read_ident_if_available(s);
2160 if (name)
2161 return name;
2163 tok = isl_stream_next_token(s);
2164 if (!tok)
2165 return NULL;
2166 if (tok->type != '$') {
2167 isl_stream_push_token(s, tok);
2168 return NULL;
2170 tok2 = isl_stream_next_token(s);
2171 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2172 if (tok2)
2173 isl_stream_push_token(s, tok2);
2174 isl_stream_push_token(s, tok);
2175 return NULL;
2178 name = isl_int_get_str(tok2->u.v);
2179 isl_token_free(tok);
2180 isl_token_free(tok2);
2182 return name;
2185 static struct isl_obj read_list(struct isl_stream *s,
2186 struct isl_hash_table *table, struct isl_obj obj)
2188 struct isl_list *list;
2190 list = isl_list_alloc(s->ctx, 2);
2191 if (!list)
2192 goto error;
2193 list->obj[0] = obj;
2194 list->obj[1] = read_obj(s, table);
2195 obj.v = list;
2196 obj.type = isl_obj_list;
2198 if (!list->obj[1].v)
2199 goto error;
2201 while (isl_stream_eat_if_available(s, ',')) {
2202 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2203 if (!obj.v)
2204 goto error;
2207 return obj;
2208 error:
2209 free_obj(obj);
2210 obj.type = isl_obj_none;
2211 obj.v = NULL;
2212 return obj;
2215 static struct isl_obj read_obj(struct isl_stream *s,
2216 struct isl_hash_table *table)
2218 struct isl_obj obj = { isl_obj_none, NULL };
2219 char *name = NULL;
2220 struct isc_un_op *op = NULL;
2222 obj = read_string_if_available(s);
2223 if (obj.v)
2224 return obj;
2225 obj = read_bool_if_available(s);
2226 if (obj.v)
2227 return obj;
2228 if (isl_stream_eat_if_available(s, '(')) {
2229 if (isl_stream_next_token_is(s, ')')) {
2230 obj.type = isl_obj_list;
2231 obj.v = isl_list_alloc(s->ctx, 0);
2232 } else {
2233 obj = read_expr(s, table);
2234 if (obj.v && isl_stream_eat_if_available(s, ','))
2235 obj = read_list(s, table, obj);
2237 if (!obj.v || isl_stream_eat(s, ')'))
2238 goto error;
2239 } else {
2240 op = read_prefix_un_op_if_available(s);
2241 if (op)
2242 return read_un_op_expr(s, table, op);
2244 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2245 return check_assert(s, table);
2246 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2247 return read_from_file(s);
2248 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2249 return write_to_file(s, table);
2250 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2251 return vertices(s, table);
2252 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2253 return any(s, table);
2254 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2255 return last(s, table);
2256 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2257 return schedule(s, table);
2258 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2259 return schedule_forest(s, table);
2260 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2261 return type_of(s, table);
2263 name = read_ident(s);
2264 if (name)
2265 obj = stored_obj(s->ctx, table, name);
2266 else
2267 obj = isl_stream_read_obj(s);
2268 if (!obj.v)
2269 goto error;
2272 if (isl_stream_eat_if_available(s, '^'))
2273 obj = power(s, obj);
2274 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2275 obj = obj_at_index(s, obj);
2276 else if (is_subtype(obj, isl_obj_union_map) &&
2277 isl_stream_eat_if_available(s, '(')) {
2278 obj = convert(s->ctx, obj, isl_obj_union_map);
2279 obj = apply(s, obj.v, table);
2280 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2281 isl_stream_eat_if_available(s, '(')) {
2282 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2283 obj = apply_fun(s, obj, table);
2284 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2285 isl_stream_eat_if_available(s, '(')) {
2286 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2287 obj = apply_fun(s, obj, table);
2290 return obj;
2291 error:
2292 free_obj(obj);
2293 obj.type = isl_obj_none;
2294 obj.v = NULL;
2295 return obj;
2298 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2299 struct isl_obj lhs, struct isl_obj rhs)
2301 int i;
2303 for (i = 0; ; ++i) {
2304 if (!bin_ops[i].op)
2305 break;
2306 if (bin_ops[i].op != like->op)
2307 continue;
2308 if (!is_subtype(lhs, bin_ops[i].lhs))
2309 continue;
2310 if (!is_subtype(rhs, bin_ops[i].rhs))
2311 continue;
2313 return &bin_ops[i];
2316 for (i = 0; ; ++i) {
2317 if (!named_bin_ops[i].name)
2318 break;
2319 if (named_bin_ops[i].op.op != like->op)
2320 continue;
2321 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2322 continue;
2323 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2324 continue;
2326 return &named_bin_ops[i].op;
2329 return NULL;
2332 static int next_is_neg_int(struct isl_stream *s)
2334 struct isl_token *tok;
2335 int ret;
2337 tok = isl_stream_next_token(s);
2338 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2339 isl_stream_push_token(s, tok);
2341 return ret;
2344 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2345 struct isl_obj lhs, struct isl_obj rhs)
2347 struct isl_obj obj;
2349 lhs = convert(ctx, lhs, op->lhs);
2350 rhs = convert(ctx, rhs, op->rhs);
2351 if (op->res != isl_obj_bool)
2352 obj.v = op->o.fn(lhs.v, rhs.v);
2353 else {
2354 int res = op->o.test(lhs.v, rhs.v);
2355 free_obj(lhs);
2356 free_obj(rhs);
2357 obj.v = isl_bool_from_int(res);
2359 obj.type = op->res;
2361 return obj;
2364 static struct isl_obj read_expr(struct isl_stream *s,
2365 struct isl_hash_table *table)
2367 struct isl_obj obj = { isl_obj_none, NULL };
2368 struct isl_obj right_obj = { isl_obj_none, NULL };
2370 obj = read_obj(s, table);
2371 for (; obj.v;) {
2372 struct isc_bin_op *op = NULL;
2374 op = read_bin_op_if_available(s, obj);
2375 if (!op)
2376 break;
2378 right_obj = read_obj(s, table);
2380 op = find_matching_bin_op(op, obj, right_obj);
2382 if (!op)
2383 isl_die(s->ctx, isl_error_invalid,
2384 "no such binary operator defined on given operands",
2385 goto error);
2387 obj = call_bin_op(s->ctx, op, obj, right_obj);
2390 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2391 right_obj = read_obj(s, table);
2392 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2395 return obj;
2396 error:
2397 free_obj(right_obj);
2398 free_obj(obj);
2399 obj.type = isl_obj_none;
2400 obj.v = NULL;
2401 return obj;
2404 static __isl_give isl_printer *source_file(struct isl_stream *s,
2405 struct isl_hash_table *table, __isl_take isl_printer *p);
2407 static __isl_give isl_printer *read_line(struct isl_stream *s,
2408 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2410 struct isl_obj obj = { isl_obj_none, NULL };
2411 char *lhs = NULL;
2412 int assign = 0;
2413 int only_print = 0;
2414 struct isc_bin_op *op = NULL;
2415 char buf[30];
2417 if (!p)
2418 return NULL;
2419 if (isl_stream_is_empty(s))
2420 return p;
2422 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2423 return source_file(s, table, p);
2424 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2425 return codegen(s, table, p);
2427 assign = is_assign(s);
2428 if (assign) {
2429 lhs = isl_stream_read_ident_if_available(s);
2430 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2431 goto error;
2432 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2433 only_print = 1;
2434 else if (!tty)
2435 only_print = 1;
2437 obj = read_expr(s, table);
2438 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2439 fprintf(stderr, "Interrupted\n");
2440 isl_ctx_reset_error(s->ctx);
2442 if (isl_stream_eat(s, ';'))
2443 goto error;
2445 if (only_print) {
2446 if (obj.type != isl_obj_none && obj.v != NULL) {
2447 p = obj.type->print(p, obj.v);
2448 p = isl_printer_end_line(p);
2450 free_obj(obj);
2451 return p;
2453 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2454 static int count = 0;
2455 snprintf(buf, sizeof(buf), "$%d", count++);
2456 lhs = strdup(buf + 1);
2458 p = isl_printer_print_str(p, buf);
2459 p = isl_printer_print_str(p, " := ");
2460 p = obj.type->print(p, obj.v);
2461 p = isl_printer_end_line(p);
2463 if (lhs && do_assign(s->ctx, table, lhs, obj))
2464 return p;
2466 return p;
2467 error:
2468 isl_stream_flush_tokens(s);
2469 isl_stream_skip_line(s);
2470 free(lhs);
2471 free_obj(obj);
2472 return p;
2475 int free_cb(void **entry, void *user)
2477 struct isl_named_obj *named = *entry;
2479 free_obj(named->obj);
2480 free(named->name);
2481 free(named);
2483 return 0;
2486 static void register_named_ops(struct isl_stream *s)
2488 int i;
2490 for (i = 0; i < ISCC_N_OP; ++i) {
2491 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2492 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2495 for (i = 0; ; ++i) {
2496 if (!named_un_ops[i].name)
2497 break;
2498 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2499 named_un_ops[i].name);
2500 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2503 for (i = 0; ; ++i) {
2504 if (!named_bin_ops[i].name)
2505 break;
2506 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2507 named_bin_ops[i].name);
2508 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2512 static __isl_give isl_printer *source_file(struct isl_stream *s,
2513 struct isl_hash_table *table, __isl_take isl_printer *p)
2515 struct isl_token *tok;
2516 struct isl_stream *s_file;
2517 FILE *file;
2519 tok = isl_stream_next_token(s);
2520 if (!tok || tok->type != ISL_TOKEN_STRING) {
2521 isl_stream_error(s, tok, "expecting filename");
2522 isl_token_free(tok);
2523 return p;
2526 file = fopen(tok->u.s, "r");
2527 isl_token_free(tok);
2528 isl_assert(s->ctx, file, return p);
2530 s_file = isl_stream_new_file(s->ctx, file);
2531 if (!s_file) {
2532 fclose(file);
2533 return p;
2536 register_named_ops(s_file);
2538 while (!s_file->eof)
2539 p = read_line(s_file, table, p, 0);
2541 isl_stream_free(s_file);
2542 fclose(file);
2544 isl_stream_eat(s, ';');
2546 return p;
2549 int main(int argc, char **argv)
2551 struct isl_ctx *ctx;
2552 struct isl_stream *s;
2553 struct isl_hash_table *table;
2554 struct iscc_options *options;
2555 isl_printer *p;
2556 int tty = isatty(0);
2558 options = iscc_options_new_with_defaults();
2559 assert(options);
2561 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2562 pet_options_set_autodetect(ctx, 1);
2563 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2564 s = isl_stream_new_file(ctx, stdin);
2565 assert(s);
2566 table = isl_hash_table_alloc(ctx, 10);
2567 assert(table);
2568 p = isl_printer_to_file(ctx, stdout);
2569 p = isl_printer_set_output_format(p, options->format);
2570 assert(p);
2572 register_named_ops(s);
2574 install_signal_handler(ctx);
2576 while (p && !s->eof) {
2577 isl_ctx_resume(ctx);
2578 p = read_line(s, table, p, tty);
2581 remove_signal_handler(ctx);
2583 isl_printer_free(p);
2584 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2585 isl_hash_table_free(ctx, table);
2586 isl_stream_free(s);
2587 isl_ctx_free(ctx);
2589 return 0;