test_bound.c: result_data: use isl_val
[barvinok.git] / iscc.c
blob214a85468125e2d3f72bd1bdecc629a9924a53f9
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 isl_val *v;
1292 int i;
1294 tok = isl_stream_next_token(s);
1295 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1296 isl_stream_error(s, tok, "expecting index");
1297 if (tok)
1298 isl_stream_push_token(s, tok);
1299 goto error;
1301 v = isl_token_get_val(s->ctx, tok);
1302 i = isl_val_get_num_si(v);
1303 isl_val_free(v);
1304 isl_token_free(tok);
1305 isl_assert(s->ctx, i < list->n, goto error);
1306 if (isl_stream_eat(s, ']'))
1307 goto error;
1309 return obj_at(obj, i);
1310 error:
1311 free_obj(obj);
1312 obj.type = isl_obj_none;
1313 obj.v = NULL;
1314 return obj;
1317 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1318 struct isl_hash_table *table)
1320 struct isl_obj obj;
1322 obj = read_expr(s, table);
1323 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1324 is_subtype(obj, isl_obj_union_map), goto error);
1326 if (obj.type == isl_obj_list) {
1327 struct isl_list *list = obj.v;
1328 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1329 obj = obj_at(obj, 0);
1331 if (obj.type == isl_obj_set)
1332 obj = convert(s->ctx, obj, isl_obj_union_set);
1333 else if (obj.type == isl_obj_map)
1334 obj = convert(s->ctx, obj, isl_obj_union_map);
1335 if (obj.type == isl_obj_union_set) {
1336 obj.v = isl_union_set_apply(obj.v, umap);
1337 } else
1338 obj.v = isl_union_map_apply_range(obj.v, umap);
1339 if (!obj.v)
1340 goto error2;
1342 if (isl_stream_eat(s, ')'))
1343 goto error2;
1345 return obj;
1346 error:
1347 isl_union_map_free(umap);
1348 error2:
1349 free_obj(obj);
1350 obj.type = isl_obj_none;
1351 obj.v = NULL;
1352 return obj;
1355 static struct isl_obj apply_fun_set(struct isl_obj obj,
1356 __isl_take isl_union_set *uset)
1358 if (obj.type == isl_obj_union_pw_qpolynomial) {
1359 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1360 } else {
1361 obj.type = isl_obj_list;
1362 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1364 return obj;
1367 static struct isl_obj apply_fun_map(struct isl_obj obj,
1368 __isl_take isl_union_map *umap)
1370 if (obj.type == isl_obj_union_pw_qpolynomial) {
1371 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1372 } else {
1373 obj.type = isl_obj_list;
1374 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1376 return obj;
1379 static struct isl_obj apply_fun(struct isl_stream *s,
1380 struct isl_obj obj, struct isl_hash_table *table)
1382 struct isl_obj arg;
1384 arg = read_expr(s, table);
1385 if (!is_subtype(arg, isl_obj_union_map) &&
1386 !is_subtype(arg, isl_obj_union_set))
1387 isl_die(s->ctx, isl_error_invalid,
1388 "expecting set of map argument", goto error);
1390 if (arg.type == isl_obj_list) {
1391 struct isl_list *list = arg.v;
1392 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1393 arg = obj_at(arg, 0);
1395 if (arg.type == isl_obj_set)
1396 arg = convert(s->ctx, arg, isl_obj_union_set);
1397 else if (arg.type == isl_obj_map)
1398 arg = convert(s->ctx, arg, isl_obj_union_map);
1399 if (arg.type == isl_obj_union_set)
1400 obj = apply_fun_set(obj, arg.v);
1401 else
1402 obj = apply_fun_map(obj, arg.v);
1403 if (!obj.v)
1404 goto error2;
1406 if (isl_stream_eat(s, ')'))
1407 goto error2;
1409 return obj;
1410 error:
1411 free_obj(arg);
1412 error2:
1413 free_obj(obj);
1414 obj.type = isl_obj_none;
1415 obj.v = NULL;
1416 return obj;
1419 struct add_vertex_data {
1420 struct isl_list *list;
1421 int i;
1424 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1426 struct add_vertex_data *data = (struct add_vertex_data *)user;
1427 isl_basic_set *expr;
1429 expr = isl_vertex_get_expr(vertex);
1431 data->list->obj[data->i].type = isl_obj_set;
1432 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1433 data->i++;
1435 isl_vertex_free(vertex);
1437 return 0;
1440 static int set_vertices(__isl_take isl_set *set, void *user)
1442 isl_ctx *ctx;
1443 isl_basic_set *hull;
1444 isl_vertices *vertices = NULL;
1445 struct isl_list *list = NULL;
1446 int r;
1447 struct add_vertex_data *data = (struct add_vertex_data *)user;
1449 set = isl_set_remove_divs(set);
1450 hull = isl_set_convex_hull(set);
1451 vertices = isl_basic_set_compute_vertices(hull);
1452 isl_basic_set_free(hull);
1454 list = data->list;
1456 ctx = isl_vertices_get_ctx(vertices);
1457 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1458 if (!data->list)
1459 goto error;
1461 data->i = 0;
1462 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1464 data->list = isl_list_concat(list, data->list);
1466 isl_vertices_free(vertices);
1468 return r;
1469 error:
1470 data->list = list;
1471 isl_vertices_free(vertices);
1472 return -1;
1475 static struct isl_obj vertices(struct isl_stream *s,
1476 struct isl_hash_table *table)
1478 isl_ctx *ctx;
1479 struct isl_obj obj;
1480 struct isl_list *list = NULL;
1481 isl_union_set *uset;
1482 struct add_vertex_data data = { NULL };
1484 obj = read_expr(s, table);
1485 obj = convert(s->ctx, obj, isl_obj_union_set);
1486 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1487 uset = obj.v;
1488 obj.v = NULL;
1490 ctx = isl_union_set_get_ctx(uset);
1491 list = isl_list_alloc(ctx, 0);
1492 if (!list)
1493 goto error;
1495 data.list = list;
1497 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1498 goto error;
1500 isl_union_set_free(uset);
1502 obj.type = isl_obj_list;
1503 obj.v = data.list;
1505 return obj;
1506 error:
1507 isl_union_set_free(uset);
1508 isl_list_free(data.list);
1509 free_obj(obj);
1510 obj.type = isl_obj_none;
1511 obj.v = NULL;
1512 return obj;
1515 static struct isl_obj type_of(struct isl_stream *s,
1516 struct isl_hash_table *table)
1518 isl_ctx *ctx;
1519 struct isl_obj obj;
1520 const char *type = "unknown";
1522 obj = read_expr(s, table);
1524 if (obj.type == isl_obj_map ||
1525 obj.type == isl_obj_union_map)
1526 type = "map";
1527 if (obj.type == isl_obj_set ||
1528 obj.type == isl_obj_union_set)
1529 type = "set";
1530 if (obj.type == isl_obj_pw_qpolynomial ||
1531 obj.type == isl_obj_union_pw_qpolynomial)
1532 type = "piecewise quasipolynomial";
1533 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1534 obj.type == isl_obj_union_pw_qpolynomial_fold)
1535 type = "piecewise quasipolynomial fold";
1536 if (obj.type == isl_obj_list)
1537 type = "list";
1538 if (obj.type == isl_obj_bool)
1539 type = "boolean";
1540 if (obj.type == isl_obj_str)
1541 type = "string";
1542 if (obj.type == isl_obj_int)
1543 type = "int";
1545 free_obj(obj);
1546 obj.type = isl_obj_str;
1547 obj.v = isl_str_from_string(s->ctx, strdup(type));
1549 return obj;
1552 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1553 struct isl_hash_table *table)
1555 struct isl_obj obj;
1557 obj = read_obj(s, table);
1558 obj = convert(s->ctx, obj, isl_obj_union_set);
1559 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1560 return obj.v;
1561 error:
1562 free_obj(obj);
1563 return NULL;
1566 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1567 struct isl_hash_table *table)
1569 struct isl_obj obj;
1571 obj = read_obj(s, table);
1572 obj = convert(s->ctx, obj, isl_obj_union_map);
1573 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1574 return obj.v;
1575 error:
1576 free_obj(obj);
1577 return NULL;
1580 static struct isl_obj last_any(struct isl_stream *s,
1581 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1582 __isl_take isl_union_map *may_source)
1584 struct isl_obj obj = { isl_obj_none, NULL };
1585 isl_union_map *sink = NULL;
1586 isl_union_map *schedule = NULL;
1587 isl_union_map *may_dep;
1588 isl_union_map *must_dep;
1590 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1591 goto error;
1593 sink = read_map(s, table);
1594 if (!sink)
1595 goto error;
1597 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1598 goto error;
1600 schedule = read_map(s, table);
1601 if (!schedule)
1602 goto error;
1604 if (isl_union_map_compute_flow(sink, must_source, may_source,
1605 schedule, &must_dep, &may_dep,
1606 NULL, NULL) < 0)
1607 return obj;
1609 obj.type = isl_obj_union_map;
1610 obj.v = isl_union_map_union(must_dep, may_dep);
1612 return obj;
1613 error:
1614 isl_union_map_free(may_source);
1615 isl_union_map_free(must_source);
1616 isl_union_map_free(sink);
1617 isl_union_map_free(schedule);
1618 free_obj(obj);
1619 obj.type = isl_obj_none;
1620 obj.v = NULL;
1621 return obj;
1624 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1626 struct isl_obj obj = { isl_obj_none, NULL };
1627 isl_union_map *must_source = NULL;
1628 isl_union_map *may_source = NULL;
1629 isl_union_map *sink = NULL;
1630 isl_union_map *schedule = NULL;
1631 isl_union_map *may_dep;
1633 may_source = read_map(s, table);
1634 if (!may_source)
1635 goto error;
1637 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1638 must_source = read_map(s, table);
1639 if (!must_source)
1640 goto error;
1641 return last_any(s, table, must_source, may_source);
1644 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1645 goto error;
1647 sink = read_map(s, table);
1648 if (!sink)
1649 goto error;
1651 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1652 goto error;
1654 schedule = read_map(s, table);
1655 if (!schedule)
1656 goto error;
1658 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1659 if (isl_union_map_compute_flow(sink, must_source, may_source,
1660 schedule, NULL, &may_dep,
1661 NULL, NULL) < 0)
1662 return obj;
1664 obj.type = isl_obj_union_map;
1665 obj.v = may_dep;
1667 return obj;
1668 error:
1669 isl_union_map_free(may_source);
1670 isl_union_map_free(must_source);
1671 isl_union_map_free(sink);
1672 isl_union_map_free(schedule);
1673 free_obj(obj);
1674 obj.type = isl_obj_none;
1675 obj.v = NULL;
1676 return obj;
1679 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1681 struct isl_obj obj = { isl_obj_none, NULL };
1682 struct isl_list *list = NULL;
1683 isl_union_map *must_source = NULL;
1684 isl_union_map *may_source = NULL;
1685 isl_union_map *sink = NULL;
1686 isl_union_map *schedule = NULL;
1687 isl_union_map *must_dep;
1688 isl_union_map *must_no_source;
1690 must_source = read_map(s, table);
1691 if (!must_source)
1692 goto error;
1694 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1695 may_source = read_map(s, table);
1696 if (!may_source)
1697 goto error;
1698 return last_any(s, table, must_source, may_source);
1701 list = isl_list_alloc(s->ctx, 2);
1702 if (!list)
1703 goto error;
1705 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1706 goto error;
1708 sink = read_map(s, table);
1709 if (!sink)
1710 goto error;
1712 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1713 goto error;
1715 schedule = read_map(s, table);
1716 if (!schedule)
1717 goto error;
1719 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1720 if (isl_union_map_compute_flow(sink, must_source, may_source,
1721 schedule, &must_dep, NULL,
1722 &must_no_source, NULL) < 0) {
1723 isl_list_free(list);
1724 return obj;
1727 list->obj[0].type = isl_obj_union_map;
1728 list->obj[0].v = must_dep;
1729 list->obj[1].type = isl_obj_union_map;
1730 list->obj[1].v = must_no_source;
1732 obj.v = list;
1733 obj.type = isl_obj_list;
1735 return obj;
1736 error:
1737 isl_list_free(list);
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 __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1749 struct isl_hash_table *table)
1751 isl_union_set *domain;
1752 isl_union_map *validity;
1753 isl_union_map *proximity;
1755 domain = read_set(s, table);
1756 if (!domain)
1757 return NULL;
1759 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1760 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1762 for (;;) {
1763 isl_union_map *umap;
1764 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1765 umap = read_map(s, table);
1766 validity = isl_union_map_union(validity, umap);
1767 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1768 umap = read_map(s, table);
1769 proximity = isl_union_map_union(proximity, umap);
1770 } else
1771 break;
1774 return isl_union_set_compute_schedule(domain, validity, proximity);
1777 static struct isl_obj schedule(struct isl_stream *s,
1778 struct isl_hash_table *table)
1780 struct isl_obj obj = { isl_obj_none, NULL };
1781 isl_schedule *schedule;
1783 schedule = get_schedule(s, table);
1785 obj.v = isl_schedule_get_map(schedule);
1786 obj.type = isl_obj_union_map;
1788 isl_schedule_free(schedule);
1790 return obj;
1793 /* Read a schedule for code generation.
1794 * If the input is a set rather than a map, then we construct
1795 * an identity schedule on the given set.
1797 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1798 struct isl_hash_table *table)
1800 struct isl_obj obj;
1802 obj = read_obj(s, table);
1804 if (is_subtype(obj, isl_obj_union_map)) {
1805 obj = convert(s->ctx, obj, isl_obj_union_map);
1806 return obj.v;
1809 if (is_subtype(obj, isl_obj_union_set)) {
1810 obj = convert(s->ctx, obj, isl_obj_union_set);
1811 return isl_union_set_identity(obj.v);
1814 free_obj(obj);
1815 isl_die(s->ctx, isl_error_invalid, "expecting set or map", return NULL);
1818 /* Generate an AST for the given schedule and options and print
1819 * the AST on the printer.
1821 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1822 __isl_take isl_union_map *schedule,
1823 __isl_take isl_union_map *options)
1825 isl_space *space;
1826 isl_set *context;
1827 isl_ast_build *build;
1828 isl_ast_node *tree;
1829 int format;
1831 space = isl_union_map_get_space(schedule);
1832 context = isl_set_universe(isl_space_params(space));
1834 build = isl_ast_build_from_context(context);
1835 build = isl_ast_build_set_options(build, options);
1836 tree = isl_ast_build_ast_from_schedule(build, schedule);
1837 isl_ast_build_free(build);
1839 if (!tree)
1840 return p;
1842 format = isl_printer_get_output_format(p);
1843 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1844 p = isl_printer_print_ast_node(p, tree);
1845 p = isl_printer_set_output_format(p, format);
1847 isl_ast_node_free(tree);
1849 return p;
1852 /* Perform the codegen operation.
1853 * In particular, read a schedule, check if the user has specified any options
1854 * and then generate an AST from the schedule (and options) and print it.
1856 static __isl_give isl_printer *codegen(struct isl_stream *s,
1857 struct isl_hash_table *table, __isl_take isl_printer *p)
1859 isl_union_map *schedule;
1860 isl_union_map *options;
1862 schedule = get_codegen_schedule(s, table);
1863 if (!schedule)
1864 return p;
1866 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1867 options = read_map(s, table);
1868 else
1869 options = isl_union_map_empty(
1870 isl_union_map_get_space(schedule));
1872 p = print_code(p, schedule, options);
1874 isl_stream_eat(s, ';');
1876 return p;
1879 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1881 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1883 struct isl_obj obj = { isl_obj_none, NULL };
1884 isl_ctx *ctx = isl_band_get_ctx(band);
1885 struct isl_list *list;
1887 list = isl_list_alloc(ctx, 2);
1888 if (!list)
1889 goto error;
1891 obj.v = list;
1892 obj.type = isl_obj_list;
1894 list->obj[0].type = isl_obj_union_map;
1895 list->obj[0].v = isl_band_get_partial_schedule(band);
1897 if (isl_band_has_children(band)) {
1898 isl_band_list *children;
1900 children = isl_band_get_children(band);
1901 list->obj[1] = band_list_to_obj_list(children);
1902 } else {
1903 list->obj[1].type = isl_obj_list;
1904 list->obj[1].v = isl_list_alloc(ctx, 0);
1907 if (!list->obj[0].v || !list->obj[1].v)
1908 goto error;
1910 isl_band_free(band);
1912 return obj;
1913 error:
1914 isl_band_free(band);
1915 free_obj(obj);
1916 obj.type = isl_obj_none;
1917 obj.v = NULL;
1918 return obj;
1921 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1923 struct isl_obj obj = { isl_obj_none, NULL };
1924 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1925 struct isl_list *list;
1926 int i, n;
1928 n = isl_band_list_n_band(bands);
1929 list = isl_list_alloc(ctx, n);
1930 if (!list)
1931 goto error;
1933 obj.v = list;
1934 obj.type = isl_obj_list;
1936 for (i = 0; i < n; ++i) {
1937 isl_band *band;
1939 band = isl_band_list_get_band(bands, i);
1940 list->obj[i] = band_to_obj_list(band);
1941 if (!list->obj[i].v)
1942 goto error;
1945 isl_band_list_free(bands);
1947 return obj;
1948 error:
1949 isl_band_list_free(bands);
1950 free_obj(obj);
1951 obj.type = isl_obj_none;
1952 obj.v = NULL;
1953 return obj;
1956 static struct isl_obj schedule_forest(struct isl_stream *s,
1957 struct isl_hash_table *table)
1959 struct isl_obj obj = { isl_obj_none, NULL };
1960 isl_schedule *schedule;
1961 isl_band_list *roots;
1963 schedule = get_schedule(s, table);
1964 if (!schedule)
1965 return obj;
1967 roots = isl_schedule_get_band_forest(schedule);
1968 isl_schedule_free(schedule);
1970 return band_list_to_obj_list(roots);
1973 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1975 struct isl_token *tok;
1976 isl_val *v;
1978 if (isl_stream_eat_if_available(s, '+'))
1979 return transitive_closure(s->ctx, obj);
1981 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1982 if (obj.type != isl_obj_union_map)
1983 obj = convert(s->ctx, obj, isl_obj_union_map);
1985 tok = isl_stream_next_token(s);
1986 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1987 isl_stream_error(s, tok, "expecting integer exponent");
1988 if (tok)
1989 isl_stream_push_token(s, tok);
1990 goto error;
1993 v = isl_token_get_val(s->ctx, tok);
1994 if (isl_val_is_zero(v)) {
1995 isl_stream_error(s, tok, "expecting non-zero exponent");
1996 isl_val_free(v);
1997 if (tok)
1998 isl_stream_push_token(s, tok);
1999 goto error;
2002 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2003 isl_token_free(tok);
2004 if (!obj.v)
2005 goto error;
2007 return obj;
2008 error:
2009 free_obj(obj);
2010 obj.type = isl_obj_none;
2011 obj.v = NULL;
2012 return obj;
2015 static struct isl_obj check_assert(struct isl_stream *s,
2016 struct isl_hash_table *table)
2018 struct isl_obj obj;
2020 obj = read_expr(s, table);
2021 if (obj.type != isl_obj_bool)
2022 isl_die(s->ctx, isl_error_invalid,
2023 "expecting boolean expression", goto error);
2024 if (obj.v != &isl_bool_true)
2025 isl_die(s->ctx, isl_error_unknown,
2026 "assertion failed", abort());
2027 error:
2028 free_obj(obj);
2029 obj.type = isl_obj_none;
2030 obj.v = NULL;
2031 return obj;
2034 static struct isl_obj read_from_file(struct isl_stream *s)
2036 struct isl_obj obj;
2037 struct isl_token *tok;
2038 struct isl_stream *s_file;
2039 struct iscc_options *options;
2040 FILE *file;
2042 tok = isl_stream_next_token(s);
2043 if (!tok || tok->type != ISL_TOKEN_STRING) {
2044 isl_stream_error(s, tok, "expecting filename");
2045 isl_token_free(tok);
2046 goto error;
2049 options = isl_ctx_peek_iscc_options(s->ctx);
2050 if (!options || !options->io) {
2051 isl_token_free(tok);
2052 isl_die(s->ctx, isl_error_invalid,
2053 "read operation not allowed", goto error);
2056 file = fopen(tok->u.s, "r");
2057 isl_token_free(tok);
2058 isl_assert(s->ctx, file, goto error);
2060 s_file = isl_stream_new_file(s->ctx, file);
2061 if (!s_file) {
2062 fclose(file);
2063 goto error;
2066 obj = isl_stream_read_obj(s_file);
2068 isl_stream_free(s_file);
2069 fclose(file);
2071 return obj;
2072 error:
2073 obj.type = isl_obj_none;
2074 obj.v = NULL;
2075 return obj;
2078 static struct isl_obj write_to_file(struct isl_stream *s,
2079 struct isl_hash_table *table)
2081 struct isl_obj obj;
2082 struct isl_token *tok;
2083 struct isl_stream *s_file;
2084 struct iscc_options *options;
2085 FILE *file;
2086 isl_printer *p;
2088 tok = isl_stream_next_token(s);
2089 if (!tok || tok->type != ISL_TOKEN_STRING) {
2090 isl_stream_error(s, tok, "expecting filename");
2091 isl_token_free(tok);
2092 goto error;
2095 obj = read_expr(s, table);
2097 options = isl_ctx_peek_iscc_options(s->ctx);
2098 if (!options || !options->io) {
2099 isl_token_free(tok);
2100 isl_die(s->ctx, isl_error_invalid,
2101 "write operation not allowed", goto error);
2104 file = fopen(tok->u.s, "w");
2105 isl_token_free(tok);
2106 if (!file)
2107 isl_die(s->ctx, isl_error_unknown,
2108 "could not open file for writing", goto error);
2110 p = isl_printer_to_file(s->ctx, file);
2111 p = isl_printer_set_output_format(p, options->format);
2112 p = obj.type->print(p, obj.v);
2113 p = isl_printer_end_line(p);
2114 isl_printer_free(p);
2116 fclose(file);
2117 error:
2118 free_obj(obj);
2119 obj.type = isl_obj_none;
2120 obj.v = NULL;
2121 return obj;
2124 static struct isl_obj read_string_if_available(struct isl_stream *s)
2126 struct isl_token *tok;
2127 struct isl_obj obj = { isl_obj_none, NULL };
2129 tok = isl_stream_next_token(s);
2130 if (!tok)
2131 return obj;
2132 if (tok->type == ISL_TOKEN_STRING) {
2133 isl_str *str;
2134 str = isl_str_alloc(s->ctx);
2135 if (!str)
2136 goto error;
2137 str->s = strdup(tok->u.s);
2138 isl_token_free(tok);
2139 obj.v = str;
2140 obj.type = isl_obj_str;
2141 } else
2142 isl_stream_push_token(s, tok);
2143 return obj;
2144 error:
2145 isl_token_free(tok);
2146 return obj;
2149 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2151 struct isl_token *tok;
2152 struct isl_obj obj = { isl_obj_none, NULL };
2154 tok = isl_stream_next_token(s);
2155 if (!tok)
2156 return obj;
2157 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2158 int is_true = tok->type == ISL_TOKEN_TRUE;
2159 isl_token_free(tok);
2160 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2161 obj.type = isl_obj_bool;
2162 } else
2163 isl_stream_push_token(s, tok);
2164 return obj;
2167 static __isl_give char *read_ident(struct isl_stream *s)
2169 char *name;
2170 isl_val *v;
2171 struct isl_token *tok, *tok2;
2173 name = isl_stream_read_ident_if_available(s);
2174 if (name)
2175 return name;
2177 tok = isl_stream_next_token(s);
2178 if (!tok)
2179 return NULL;
2180 if (tok->type != '$') {
2181 isl_stream_push_token(s, tok);
2182 return NULL;
2184 tok2 = isl_stream_next_token(s);
2185 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2186 if (tok2)
2187 isl_stream_push_token(s, tok2);
2188 isl_stream_push_token(s, tok);
2189 return NULL;
2192 v = isl_token_get_val(s->ctx, tok2);
2193 name = isl_val_to_str(v);
2194 isl_val_free(v);
2195 isl_token_free(tok);
2196 isl_token_free(tok2);
2198 return name;
2201 static struct isl_obj read_list(struct isl_stream *s,
2202 struct isl_hash_table *table, struct isl_obj obj)
2204 struct isl_list *list;
2206 list = isl_list_alloc(s->ctx, 2);
2207 if (!list)
2208 goto error;
2209 list->obj[0] = obj;
2210 list->obj[1] = read_obj(s, table);
2211 obj.v = list;
2212 obj.type = isl_obj_list;
2214 if (!list->obj[1].v)
2215 goto error;
2217 while (isl_stream_eat_if_available(s, ',')) {
2218 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2219 if (!obj.v)
2220 goto error;
2223 return obj;
2224 error:
2225 free_obj(obj);
2226 obj.type = isl_obj_none;
2227 obj.v = NULL;
2228 return obj;
2231 static struct isl_obj read_obj(struct isl_stream *s,
2232 struct isl_hash_table *table)
2234 struct isl_obj obj = { isl_obj_none, NULL };
2235 char *name = NULL;
2236 struct isc_un_op *op = NULL;
2238 obj = read_string_if_available(s);
2239 if (obj.v)
2240 return obj;
2241 obj = read_bool_if_available(s);
2242 if (obj.v)
2243 return obj;
2244 if (isl_stream_eat_if_available(s, '(')) {
2245 if (isl_stream_next_token_is(s, ')')) {
2246 obj.type = isl_obj_list;
2247 obj.v = isl_list_alloc(s->ctx, 0);
2248 } else {
2249 obj = read_expr(s, table);
2250 if (obj.v && isl_stream_eat_if_available(s, ','))
2251 obj = read_list(s, table, obj);
2253 if (!obj.v || isl_stream_eat(s, ')'))
2254 goto error;
2255 } else {
2256 op = read_prefix_un_op_if_available(s);
2257 if (op)
2258 return read_un_op_expr(s, table, op);
2260 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2261 return check_assert(s, table);
2262 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2263 return read_from_file(s);
2264 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2265 return write_to_file(s, table);
2266 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2267 return vertices(s, table);
2268 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2269 return any(s, table);
2270 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2271 return last(s, table);
2272 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2273 return schedule(s, table);
2274 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2275 return schedule_forest(s, table);
2276 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2277 return type_of(s, table);
2279 name = read_ident(s);
2280 if (name)
2281 obj = stored_obj(s->ctx, table, name);
2282 else
2283 obj = isl_stream_read_obj(s);
2284 if (!obj.v)
2285 goto error;
2288 if (isl_stream_eat_if_available(s, '^'))
2289 obj = power(s, obj);
2290 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2291 obj = obj_at_index(s, obj);
2292 else if (is_subtype(obj, isl_obj_union_map) &&
2293 isl_stream_eat_if_available(s, '(')) {
2294 obj = convert(s->ctx, obj, isl_obj_union_map);
2295 obj = apply(s, obj.v, table);
2296 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2297 isl_stream_eat_if_available(s, '(')) {
2298 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2299 obj = apply_fun(s, obj, table);
2300 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2301 isl_stream_eat_if_available(s, '(')) {
2302 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2303 obj = apply_fun(s, obj, table);
2306 return obj;
2307 error:
2308 free_obj(obj);
2309 obj.type = isl_obj_none;
2310 obj.v = NULL;
2311 return obj;
2314 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2315 struct isl_obj lhs, struct isl_obj rhs)
2317 int i;
2319 for (i = 0; ; ++i) {
2320 if (!bin_ops[i].op)
2321 break;
2322 if (bin_ops[i].op != like->op)
2323 continue;
2324 if (!is_subtype(lhs, bin_ops[i].lhs))
2325 continue;
2326 if (!is_subtype(rhs, bin_ops[i].rhs))
2327 continue;
2329 return &bin_ops[i];
2332 for (i = 0; ; ++i) {
2333 if (!named_bin_ops[i].name)
2334 break;
2335 if (named_bin_ops[i].op.op != like->op)
2336 continue;
2337 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2338 continue;
2339 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2340 continue;
2342 return &named_bin_ops[i].op;
2345 return NULL;
2348 static int next_is_neg_int(struct isl_stream *s)
2350 struct isl_token *tok;
2351 int ret;
2353 tok = isl_stream_next_token(s);
2354 if (tok && tok->type == ISL_TOKEN_VALUE) {
2355 isl_val *v;
2356 v = isl_token_get_val(s->ctx, tok);
2357 ret = isl_val_is_neg(v);
2358 isl_val_free(v);
2359 } else
2360 ret = 0;
2361 isl_stream_push_token(s, tok);
2363 return ret;
2366 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2367 struct isl_obj lhs, struct isl_obj rhs)
2369 struct isl_obj obj;
2371 lhs = convert(ctx, lhs, op->lhs);
2372 rhs = convert(ctx, rhs, op->rhs);
2373 if (op->res != isl_obj_bool)
2374 obj.v = op->o.fn(lhs.v, rhs.v);
2375 else {
2376 int res = op->o.test(lhs.v, rhs.v);
2377 free_obj(lhs);
2378 free_obj(rhs);
2379 obj.v = isl_bool_from_int(res);
2381 obj.type = op->res;
2383 return obj;
2386 static struct isl_obj read_expr(struct isl_stream *s,
2387 struct isl_hash_table *table)
2389 struct isl_obj obj = { isl_obj_none, NULL };
2390 struct isl_obj right_obj = { isl_obj_none, NULL };
2392 obj = read_obj(s, table);
2393 for (; obj.v;) {
2394 struct isc_bin_op *op = NULL;
2396 op = read_bin_op_if_available(s, obj);
2397 if (!op)
2398 break;
2400 right_obj = read_obj(s, table);
2402 op = find_matching_bin_op(op, obj, right_obj);
2404 if (!op)
2405 isl_die(s->ctx, isl_error_invalid,
2406 "no such binary operator defined on given operands",
2407 goto error);
2409 obj = call_bin_op(s->ctx, op, obj, right_obj);
2412 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2413 right_obj = read_obj(s, table);
2414 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2417 return obj;
2418 error:
2419 free_obj(right_obj);
2420 free_obj(obj);
2421 obj.type = isl_obj_none;
2422 obj.v = NULL;
2423 return obj;
2426 static __isl_give isl_printer *source_file(struct isl_stream *s,
2427 struct isl_hash_table *table, __isl_take isl_printer *p);
2429 static __isl_give isl_printer *read_line(struct isl_stream *s,
2430 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2432 struct isl_obj obj = { isl_obj_none, NULL };
2433 char *lhs = NULL;
2434 int assign = 0;
2435 int only_print = 0;
2436 struct isc_bin_op *op = NULL;
2437 char buf[30];
2439 if (!p)
2440 return NULL;
2441 if (isl_stream_is_empty(s))
2442 return p;
2444 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2445 return source_file(s, table, p);
2446 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2447 return codegen(s, table, p);
2449 assign = is_assign(s);
2450 if (assign) {
2451 lhs = isl_stream_read_ident_if_available(s);
2452 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2453 goto error;
2454 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2455 only_print = 1;
2456 else if (!tty)
2457 only_print = 1;
2459 obj = read_expr(s, table);
2460 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2461 fprintf(stderr, "Interrupted\n");
2462 isl_ctx_reset_error(s->ctx);
2464 if (isl_stream_eat(s, ';'))
2465 goto error;
2467 if (only_print) {
2468 if (obj.type != isl_obj_none && obj.v != NULL) {
2469 p = obj.type->print(p, obj.v);
2470 p = isl_printer_end_line(p);
2472 free_obj(obj);
2473 return p;
2475 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2476 static int count = 0;
2477 snprintf(buf, sizeof(buf), "$%d", count++);
2478 lhs = strdup(buf + 1);
2480 p = isl_printer_print_str(p, buf);
2481 p = isl_printer_print_str(p, " := ");
2482 p = obj.type->print(p, obj.v);
2483 p = isl_printer_end_line(p);
2485 if (lhs && do_assign(s->ctx, table, lhs, obj))
2486 return p;
2488 return p;
2489 error:
2490 isl_stream_flush_tokens(s);
2491 isl_stream_skip_line(s);
2492 free(lhs);
2493 free_obj(obj);
2494 return p;
2497 int free_cb(void **entry, void *user)
2499 struct isl_named_obj *named = *entry;
2501 free_obj(named->obj);
2502 free(named->name);
2503 free(named);
2505 return 0;
2508 static void register_named_ops(struct isl_stream *s)
2510 int i;
2512 for (i = 0; i < ISCC_N_OP; ++i) {
2513 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2514 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2517 for (i = 0; ; ++i) {
2518 if (!named_un_ops[i].name)
2519 break;
2520 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2521 named_un_ops[i].name);
2522 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2525 for (i = 0; ; ++i) {
2526 if (!named_bin_ops[i].name)
2527 break;
2528 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2529 named_bin_ops[i].name);
2530 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2534 static __isl_give isl_printer *source_file(struct isl_stream *s,
2535 struct isl_hash_table *table, __isl_take isl_printer *p)
2537 struct isl_token *tok;
2538 struct isl_stream *s_file;
2539 FILE *file;
2541 tok = isl_stream_next_token(s);
2542 if (!tok || tok->type != ISL_TOKEN_STRING) {
2543 isl_stream_error(s, tok, "expecting filename");
2544 isl_token_free(tok);
2545 return p;
2548 file = fopen(tok->u.s, "r");
2549 isl_token_free(tok);
2550 isl_assert(s->ctx, file, return p);
2552 s_file = isl_stream_new_file(s->ctx, file);
2553 if (!s_file) {
2554 fclose(file);
2555 return p;
2558 register_named_ops(s_file);
2560 while (!s_file->eof)
2561 p = read_line(s_file, table, p, 0);
2563 isl_stream_free(s_file);
2564 fclose(file);
2566 isl_stream_eat(s, ';');
2568 return p;
2571 int main(int argc, char **argv)
2573 struct isl_ctx *ctx;
2574 struct isl_stream *s;
2575 struct isl_hash_table *table;
2576 struct iscc_options *options;
2577 isl_printer *p;
2578 int tty = isatty(0);
2580 options = iscc_options_new_with_defaults();
2581 assert(options);
2583 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2584 pet_options_set_autodetect(ctx, 1);
2585 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2586 s = isl_stream_new_file(ctx, stdin);
2587 assert(s);
2588 table = isl_hash_table_alloc(ctx, 10);
2589 assert(table);
2590 p = isl_printer_to_file(ctx, stdout);
2591 p = isl_printer_set_output_format(p, options->format);
2592 assert(p);
2594 register_named_ops(s);
2596 install_signal_handler(ctx);
2598 while (p && !s->eof) {
2599 isl_ctx_resume(ctx);
2600 p = read_line(s, table, p, tty);
2603 remove_signal_handler(ctx);
2605 isl_printer_free(p);
2606 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2607 isl_hash_table_free(ctx, table);
2608 isl_stream_free(s);
2609 isl_ctx_free(ctx);
2611 return 0;