barvinok_enumerate.cc: series: drop unused variables
[barvinok.git] / iscc.c
blobebec8cb2d98e3994f2ae58420d4ea1346810691d
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/aff.h>
7 #include <isl/obj.h>
8 #include <isl/stream.h>
9 #include <isl/set.h>
10 #include <isl/map.h>
11 #include <isl/vertices.h>
12 #include <isl/flow.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 int pet_options_set_encapsulate_dynamic_control(isl_ctx *ctx, int val)
76 return -1;
78 #endif
80 static int iscc_bool_false = 0;
81 static int iscc_bool_true = 1;
82 static int iscc_bool_error = -1;
84 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
85 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
86 ISCC_SCHEDULE,
87 ISCC_MINIMIZING, ISCC_RESPECTING,
88 ISCC_CODEGEN, ISCC_USING,
89 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
90 ISCC_N_OP };
91 static const char *op_name[ISCC_N_OP] = {
92 [ISCC_ASSERT] = "assert",
93 [ISCC_READ] = "read",
94 [ISCC_WRITE] = "write",
95 [ISCC_PRINT] = "print",
96 [ISCC_SOURCE] = "source",
97 [ISCC_VERTICES] = "vertices",
98 [ISCC_LAST] = "last",
99 [ISCC_ANY] = "any",
100 [ISCC_BEFORE] = "before",
101 [ISCC_UNDER] = "under",
102 [ISCC_SCHEDULE] = "schedule",
103 [ISCC_MINIMIZING] = "minimizing",
104 [ISCC_RESPECTING] = "respecting",
105 [ISCC_CODEGEN] = "codegen",
106 [ISCC_USING] = "using",
107 [ISCC_TYPEOF] = "typeof"
109 static enum isl_token_type iscc_op[ISCC_N_OP];
111 struct isl_arg_choice iscc_format[] = {
112 {"isl", ISL_FORMAT_ISL},
113 {"omega", ISL_FORMAT_OMEGA},
114 {"polylib", ISL_FORMAT_POLYLIB},
115 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
116 {"latex", ISL_FORMAT_LATEX},
117 {"C", ISL_FORMAT_C},
121 struct iscc_options {
122 struct barvinok_options *barvinok;
123 struct pet_options *pet;
124 unsigned format;
125 int io;
128 ISL_ARGS_START(struct iscc_options, iscc_options_args)
129 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
130 "barvinok options")
131 #ifdef HAVE_PET
132 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
133 #endif
134 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
135 iscc_format, ISL_FORMAT_ISL, "output format")
136 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
137 "allow read and write operations")
138 ISL_ARGS_END
140 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
141 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
143 static void *isl_obj_bool_copy(void *v)
145 return v;
148 static void isl_obj_bool_free(void *v)
152 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
153 void *v)
155 if (v == &iscc_bool_true)
156 return isl_printer_print_str(p, "True");
157 else if (v == &iscc_bool_false)
158 return isl_printer_print_str(p, "False");
159 else
160 return isl_printer_print_str(p, "Error");
163 static void *isl_obj_bool_add(void *v1, void *v2)
165 return v1;
168 struct isl_obj_vtable isl_obj_bool_vtable = {
169 isl_obj_bool_copy,
170 isl_obj_bool_add,
171 isl_obj_bool_print,
172 isl_obj_bool_free
174 #define isl_obj_bool (&isl_obj_bool_vtable)
176 int *iscc_bool_from_int(int res)
178 return res < 0 ? &iscc_bool_error :
179 res ? &iscc_bool_true : &iscc_bool_false;
182 /* Conjunction of "b1" and "b2".
183 * The result is returned as an integer because it is post-processed by
184 * iscc_bool_from_int.
186 static int isl_bool_and(isl_bool *b1, __isl_take isl_bool *b2)
188 if (b1 == &iscc_bool_error || b2 == &iscc_bool_error)
189 return -1;
190 return b1 == &iscc_bool_true && b2 == &iscc_bool_true;
193 /* Disjunction of "b1" and "b2".
194 * The result is returned as an integer because it is post-processed by
195 * iscc_bool_from_int.
197 static int isl_bool_or(isl_bool *b1, __isl_take isl_bool *b2)
199 if (b1 == &iscc_bool_error || b2 == &iscc_bool_error)
200 return -1;
201 return b1 == &iscc_bool_true || b2 == &iscc_bool_true;
204 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
205 __isl_take isl_union_map *map2)
207 return isl_union_map_is_subset(map2, map1);
209 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
210 __isl_take isl_union_set *set2)
212 return isl_union_set_is_subset(set2, set1);
215 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
216 __isl_take isl_union_map *map2)
218 return isl_union_map_is_strict_subset(map2, map1);
220 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
221 __isl_take isl_union_set *set2)
223 return isl_union_set_is_strict_subset(set2, set1);
226 extern struct isl_obj_vtable isl_obj_list_vtable;
227 #define isl_obj_list (&isl_obj_list_vtable)
229 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
230 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
231 struct isc_bin_op {
232 enum isl_token_type op;
233 isl_obj_type lhs;
234 isl_obj_type rhs;
235 isl_obj_type res;
236 union {
237 isc_bin_op_fn fn;
238 isc_bin_test_fn test;
239 } o;
241 struct isc_named_bin_op {
242 char *name;
243 struct isc_bin_op op;
245 /* Compound binary operator.
246 * "full" is only used to generate a unique token number for op.op
247 * in register_named_ops.
248 * "op1" is the first part of the compound operator.
249 * "op2" is the second part of the compound operator.
251 struct iscc_compound_bin_op {
252 char *full;
253 enum isl_token_type op1;
254 enum isl_token_type op2;
255 struct isc_bin_op op;
258 struct iscc_at {
259 isl_union_pw_qpolynomial *upwqp;
260 isl_union_pw_qpolynomial *res;
263 static isl_stat eval_at(__isl_take isl_point *pnt, void *user)
265 struct iscc_at *at = (struct iscc_at *) user;
266 isl_val *v;
267 isl_qpolynomial *qp;
268 isl_set *set;
270 set = isl_set_from_point(isl_point_copy(pnt));
271 v = isl_union_pw_qpolynomial_eval(
272 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
273 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
275 at->res = isl_union_pw_qpolynomial_add(at->res,
276 isl_union_pw_qpolynomial_from_pw_qpolynomial(
277 isl_pw_qpolynomial_alloc(set, qp)));
279 return isl_stat_ok;
282 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
283 __isl_take isl_union_pw_qpolynomial *upwqp,
284 __isl_take isl_union_set *uset)
286 struct iscc_at at;
288 at.upwqp = upwqp;
289 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
291 isl_union_set_foreach_point(uset, eval_at, &at);
293 isl_union_pw_qpolynomial_free(upwqp);
294 isl_union_set_free(uset);
296 return at.res;
299 struct iscc_fold_at {
300 isl_union_pw_qpolynomial_fold *upwf;
301 isl_union_pw_qpolynomial *res;
304 static isl_stat eval_fold_at(__isl_take isl_point *pnt, void *user)
306 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
307 isl_val *v;
308 isl_qpolynomial *qp;
309 isl_set *set;
311 set = isl_set_from_point(isl_point_copy(pnt));
312 v = isl_union_pw_qpolynomial_fold_eval(
313 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
314 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
316 at->res = isl_union_pw_qpolynomial_add(at->res,
317 isl_union_pw_qpolynomial_from_pw_qpolynomial(
318 isl_pw_qpolynomial_alloc(set, qp)));
320 return isl_stat_ok;
323 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
324 __isl_take isl_union_pw_qpolynomial_fold *upwf,
325 __isl_take isl_union_set *uset)
327 struct iscc_fold_at at;
329 at.upwf = upwf;
330 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
332 isl_union_set_foreach_point(uset, eval_fold_at, &at);
334 isl_union_pw_qpolynomial_fold_free(upwf);
335 isl_union_set_free(uset);
337 return at.res;
340 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
341 __isl_take isl_union_pw_qpolynomial *upwqp,
342 __isl_take isl_union_pw_qpolynomial_fold *upwf)
344 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
345 upwqp);
348 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
349 __isl_take isl_union_map *umap,
350 __isl_take isl_union_pw_qpolynomial_fold *upwf)
352 isl_ctx *ctx;
353 struct isl_list *list;
354 int tight;
356 ctx = isl_union_map_get_ctx(umap);
357 list = isl_list_alloc(ctx, 2);
358 if (!list)
359 goto error2;
361 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
362 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
363 upwf, &tight);
364 list->obj[1].type = isl_obj_bool;
365 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
366 if (tight < 0 || !list->obj[0].v)
367 goto error;
369 return list;
370 error2:
371 isl_union_map_free(umap);
372 isl_union_pw_qpolynomial_fold_free(upwf);
373 error:
374 isl_list_free(list);
375 return NULL;
378 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
379 __isl_take isl_union_set *uset,
380 __isl_take isl_union_pw_qpolynomial_fold *upwf)
382 isl_ctx *ctx;
383 struct isl_list *list;
384 int tight;
386 ctx = isl_union_set_get_ctx(uset);
387 list = isl_list_alloc(ctx, 2);
388 if (!list)
389 goto error2;
391 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
392 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
393 upwf, &tight);
394 list->obj[1].type = isl_obj_bool;
395 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
396 if (tight < 0 || !list->obj[0].v)
397 goto error;
399 return list;
400 error2:
401 isl_union_set_free(uset);
402 isl_union_pw_qpolynomial_fold_free(upwf);
403 error:
404 isl_list_free(list);
405 return NULL;
408 static __isl_give isl_union_pw_qpolynomial *isl_val_mul_union_pw_qpolynomial(
409 __isl_take isl_val *v, __isl_take isl_union_pw_qpolynomial *upwqp)
411 return isl_union_pw_qpolynomial_scale_val(upwqp, v);
414 static __isl_give isl_union_pw_qpolynomial_fold *
415 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val *v,
416 __isl_take isl_union_pw_qpolynomial_fold *upwf)
418 return isl_union_pw_qpolynomial_fold_scale_val(upwf, v);
421 /* Are the two strings "str1" and "str2" equal to each other?
423 static int str_eq(__isl_keep isl_str *str1, __isl_keep isl_str *str2)
425 if (!str1 || !str2)
426 return -1;
428 return !strcmp(str1->s, str2->s);
431 struct isc_bin_op bin_ops[] = {
432 { '+', isl_obj_bool, isl_obj_bool, isl_obj_bool,
433 (isc_bin_op_fn) &isl_bool_or },
434 { '*', isl_obj_bool, isl_obj_bool, isl_obj_bool,
435 (isc_bin_op_fn) &isl_bool_and },
436 { '+', isl_obj_val, isl_obj_val, isl_obj_val,
437 (isc_bin_op_fn) &isl_val_add },
438 { '-', isl_obj_val, isl_obj_val, isl_obj_val,
439 (isc_bin_op_fn) &isl_val_sub },
440 { '*', isl_obj_val, isl_obj_val, isl_obj_val,
441 (isc_bin_op_fn) &isl_val_mul },
442 { '+', isl_obj_pw_multi_aff, isl_obj_pw_multi_aff,
443 isl_obj_pw_multi_aff,
444 (isc_bin_op_fn) &isl_pw_multi_aff_add },
445 { '+', isl_obj_union_set, isl_obj_union_set,
446 isl_obj_union_set,
447 (isc_bin_op_fn) &isl_union_set_union },
448 { '+', isl_obj_union_map, isl_obj_union_map,
449 isl_obj_union_map,
450 (isc_bin_op_fn) &isl_union_map_union },
451 { '-', isl_obj_union_set, isl_obj_union_set,
452 isl_obj_union_set,
453 (isc_bin_op_fn) &isl_union_set_subtract },
454 { '-', isl_obj_union_map, isl_obj_union_map,
455 isl_obj_union_map,
456 (isc_bin_op_fn) &isl_union_map_subtract },
457 { '-', isl_obj_union_map, isl_obj_union_set,
458 isl_obj_union_map,
459 (isc_bin_op_fn) &isl_union_map_subtract_domain },
460 { '*', isl_obj_union_set, isl_obj_union_set,
461 isl_obj_union_set,
462 (isc_bin_op_fn) &isl_union_set_intersect },
463 { '*', isl_obj_union_map, isl_obj_union_map,
464 isl_obj_union_map,
465 (isc_bin_op_fn) &isl_union_map_intersect },
466 { '*', isl_obj_union_map, isl_obj_union_set,
467 isl_obj_union_map,
468 (isc_bin_op_fn) &isl_union_map_intersect_domain },
469 { '.', isl_obj_union_map, isl_obj_union_map,
470 isl_obj_union_map,
471 (isc_bin_op_fn) &isl_union_map_apply_range },
472 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
473 isl_obj_union_pw_qpolynomial,
474 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
475 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
476 isl_obj_list,
477 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
478 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
479 isl_obj_union_map,
480 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
481 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
482 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
483 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
484 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
485 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
486 isl_obj_bool,
487 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
488 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
489 isl_obj_bool,
490 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
491 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
492 isl_obj_bool,
493 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
494 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
495 isl_obj_bool,
496 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
497 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
498 isl_obj_bool,
499 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
500 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
501 isl_obj_bool,
502 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
503 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
504 isl_obj_bool,
505 { .test =
506 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
507 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
508 isl_obj_bool,
509 { .test =
510 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
511 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
512 isl_obj_union_map,
513 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
514 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
515 isl_obj_union_map,
516 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
517 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
518 isl_obj_union_map,
519 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
520 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
521 isl_obj_union_map,
522 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
523 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
524 isl_obj_union_map,
525 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
526 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
527 isl_obj_union_map,
528 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
529 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
530 isl_obj_union_map,
531 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
532 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
533 isl_obj_union_map,
534 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
535 { '.', isl_obj_union_pw_qpolynomial_fold,
536 isl_obj_union_pw_qpolynomial_fold,
537 isl_obj_union_pw_qpolynomial_fold,
538 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
539 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
540 isl_obj_union_pw_qpolynomial,
541 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
542 { '+', isl_obj_union_pw_qpolynomial,
543 isl_obj_union_pw_qpolynomial_fold,
544 isl_obj_union_pw_qpolynomial_fold,
545 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
546 { '+', isl_obj_union_pw_qpolynomial_fold,
547 isl_obj_union_pw_qpolynomial,
548 isl_obj_union_pw_qpolynomial_fold,
549 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
550 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
551 isl_obj_union_pw_qpolynomial,
552 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
553 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
554 isl_obj_union_pw_qpolynomial,
555 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
556 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
557 isl_obj_union_pw_qpolynomial,
558 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
559 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
560 isl_obj_union_pw_qpolynomial_fold,
561 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
562 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
563 isl_obj_union_pw_qpolynomial_fold,
564 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
565 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
566 isl_obj_union_pw_qpolynomial,
567 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
568 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
569 isl_obj_union_pw_qpolynomial,
570 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
571 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
572 isl_obj_union_pw_qpolynomial_fold,
573 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
574 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
575 isl_obj_union_pw_qpolynomial,
576 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
577 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
578 isl_obj_union_pw_qpolynomial,
579 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
580 { '%', isl_obj_union_set, isl_obj_union_set,
581 isl_obj_union_set,
582 (isc_bin_op_fn) &isl_union_set_gist },
583 { '%', isl_obj_union_map, isl_obj_union_map,
584 isl_obj_union_map,
585 (isc_bin_op_fn) &isl_union_map_gist },
586 { '%', isl_obj_union_map, isl_obj_union_set,
587 isl_obj_union_map,
588 (isc_bin_op_fn) &isl_union_map_gist_domain },
589 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
590 isl_obj_union_pw_qpolynomial,
591 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
592 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
593 isl_obj_union_pw_qpolynomial_fold,
594 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
595 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
596 isl_obj_union_pw_qpolynomial, isl_obj_bool,
597 { .test = (isc_bin_test_fn)
598 &isl_union_pw_qpolynomial_plain_is_equal } },
599 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
600 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
601 { .test = (isc_bin_test_fn)
602 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
603 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
604 (isc_bin_op_fn) &isl_str_concat },
605 { '=', isl_obj_str, isl_obj_str, isl_obj_bool,
606 { .test = (isc_bin_test_fn) &str_eq } },
610 struct iscc_compound_bin_op compound_bin_ops[] = {
611 { "->*", ISL_TOKEN_TO, '*',
612 { -1, isl_obj_union_map, isl_obj_union_set,
613 isl_obj_union_map,
614 (isc_bin_op_fn) &isl_union_map_intersect_range } },
615 { "->-", ISL_TOKEN_TO, '-',
616 { -1, isl_obj_union_map, isl_obj_union_set,
617 isl_obj_union_map,
618 (isc_bin_op_fn) &isl_union_map_subtract_range } },
622 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
623 __isl_take isl_union_map *umap2)
625 return isl_union_map_apply_range(umap2, umap1);
628 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
629 __isl_take isl_union_pw_qpolynomial *upwqp,
630 __isl_take isl_union_map *umap)
632 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
635 static __isl_give struct isl_list *qpolynomial_fold_after_map(
636 __isl_take isl_union_pw_qpolynomial_fold *upwf,
637 __isl_take isl_union_map *umap)
639 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
642 struct isc_named_bin_op named_bin_ops[] = {
643 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
644 isl_obj_union_map,
645 (isc_bin_op_fn) &map_after_map } },
646 { "after", { -1, isl_obj_union_pw_qpolynomial,
647 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
648 (isc_bin_op_fn) &qpolynomial_after_map } },
649 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
650 isl_obj_union_map, isl_obj_list,
651 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
652 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
653 isl_obj_union_map,
654 (isc_bin_op_fn) &isl_union_map_apply_range } },
655 { "before", { -1, isl_obj_union_map,
656 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
657 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
658 { "before", { -1, isl_obj_union_map,
659 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
660 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
661 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
662 isl_obj_union_set,
663 (isc_bin_op_fn) &isl_union_set_product } },
664 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
665 isl_obj_union_map,
666 (isc_bin_op_fn) &isl_union_map_product } },
667 NULL
670 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
672 return isl_set_from_basic_set(isl_union_set_sample(uset));
675 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
677 return isl_map_from_basic_map(isl_union_map_sample(umap));
680 static __isl_give struct isl_list *union_map_power(
681 __isl_take isl_union_map *umap)
683 isl_ctx *ctx;
684 struct isl_list *list;
685 int exact;
687 ctx = isl_union_map_get_ctx(umap);
688 list = isl_list_alloc(ctx, 2);
689 if (!list)
690 goto error2;
692 list->obj[0].type = isl_obj_union_map;
693 list->obj[0].v = isl_union_map_power(umap, &exact);
694 list->obj[1].type = isl_obj_bool;
695 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
696 if (exact < 0 || !list->obj[0].v)
697 goto error;
699 return list;
700 error2:
701 isl_union_map_free(umap);
702 error:
703 isl_list_free(list);
704 return NULL;
707 /* Compute a lower or upper bound on "upwqp" depending on "type" and
708 * return a list containing two elements, the bound and a boolean
709 * indicating whether the result is tight.
711 static __isl_give struct isl_list *union_pw_qpolynomial_bound(
712 __isl_take isl_union_pw_qpolynomial *upwqp, enum isl_fold type)
714 isl_ctx *ctx;
715 struct isl_list *list;
716 int tight;
718 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
719 list = isl_list_alloc(ctx, 2);
720 if (!list)
721 goto error2;
723 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
724 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp, type, &tight);
725 list->obj[1].type = isl_obj_bool;
726 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
727 if (tight < 0 || !list->obj[0].v)
728 goto error;
730 return list;
731 error2:
732 isl_union_pw_qpolynomial_free(upwqp);
733 error:
734 isl_list_free(list);
735 return NULL;
738 /* Compute a lower bound on "upwqp" and return a list containing
739 * two elements, the bound and a booleanindicating whether
740 * the result is tight.
742 static __isl_give struct isl_list *union_pw_qpolynomial_lower_bound(
743 __isl_take isl_union_pw_qpolynomial *upwqp)
745 return union_pw_qpolynomial_bound(upwqp, isl_fold_min);
748 /* Compute a upper bound on "upwqp" and return a list containing
749 * two elements, the bound and a booleanindicating whether
750 * the result is tight.
752 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
753 __isl_take isl_union_pw_qpolynomial *upwqp)
755 return union_pw_qpolynomial_bound(upwqp, isl_fold_max);
758 #ifdef HAVE_PET
759 /* Collect all statement instances, except those of kill statements.
761 static __isl_give isl_union_set *collect_non_kill_instances(
762 struct pet_scop *scop)
764 int i;
765 isl_set *domain_i;
766 isl_union_set *domain;
768 if (!scop)
769 return NULL;
771 domain = isl_union_set_empty(isl_set_get_space(scop->context));
773 for (i = 0; i < scop->n_stmt; ++i) {
774 struct pet_stmt *stmt = scop->stmts[i];
776 if (pet_stmt_is_kill(stmt))
777 continue;
778 domain_i = isl_set_copy(stmt->domain);
779 if (scop->stmts[i]->n_arg > 0)
780 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
781 domain = isl_union_set_add_set(domain, domain_i);
784 return domain;
787 static __isl_give isl_list *parse(__isl_take isl_str *str)
789 isl_ctx *ctx;
790 struct isl_list *list;
791 struct pet_scop *scop;
792 isl_schedule *sched;
793 isl_union_map *may_reads, *must_writes, *may_writes;
794 isl_union_set *domain;
795 struct iscc_options *options;
797 if (!str)
798 return NULL;
799 ctx = str->ctx;
801 options = isl_ctx_peek_iscc_options(ctx);
802 if (!options || !options->io) {
803 isl_str_free(str);
804 isl_die(ctx, isl_error_invalid,
805 "parse_file operation not allowed", return NULL);
808 list = isl_list_alloc(ctx, 5);
809 if (!list)
810 goto error;
812 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
813 domain = collect_non_kill_instances(scop);
814 sched = pet_scop_get_schedule(scop);
815 sched = isl_schedule_intersect_domain(sched,
816 isl_union_set_copy(domain));
817 may_reads = pet_scop_get_may_reads(scop);
818 may_writes = pet_scop_get_may_writes(scop);
819 must_writes = pet_scop_get_must_writes(scop);
820 pet_scop_free(scop);
822 list->obj[0].type = isl_obj_union_set;
823 list->obj[0].v = domain;
824 list->obj[1].type = isl_obj_union_map;
825 list->obj[1].v = must_writes;
826 list->obj[2].type = isl_obj_union_map;
827 list->obj[2].v = may_writes;
828 list->obj[3].type = isl_obj_union_map;
829 list->obj[3].v = may_reads;
830 list->obj[4].type = isl_obj_schedule;
831 list->obj[4].v = sched;
833 if (!list->obj[0].v || !list->obj[1].v ||
834 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
835 goto error;
837 isl_str_free(str);
838 return list;
839 error:
840 isl_list_free(list);
841 isl_str_free(str);
842 return NULL;
844 #endif
846 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
848 isl_union_set **scan = (isl_union_set **) user;
850 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
852 return isl_stat_ok;
855 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
857 isl_union_set *scan;
859 scan = isl_union_set_empty(isl_union_set_get_space(uset));
861 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
862 isl_union_set_free(scan);
863 return uset;
866 isl_union_set_free(uset);
867 return scan;
870 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
872 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
875 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
876 __isl_take isl_union_pw_qpolynomial *upwqp)
878 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
881 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
882 __isl_take isl_union_pw_qpolynomial *upwqp)
884 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
887 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
888 __isl_take isl_union_pw_qpolynomial *upwqp)
890 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
893 /* Return the domain of "schedule".
895 static __isl_give isl_union_set *schedule_domain(
896 __isl_take isl_schedule *schedule)
898 isl_union_set *domain;
900 domain = isl_schedule_get_domain(schedule);
901 isl_schedule_free(schedule);
903 return domain;
906 /* Convert "schedule" to a union map representation.
908 static __isl_give isl_union_map *schedule_map(__isl_take isl_schedule *schedule)
910 isl_union_map *map;
912 map = isl_schedule_get_map(schedule);
913 isl_schedule_free(schedule);
915 return map;
918 typedef void *(*isc_un_op_fn)(void *arg);
919 struct isc_un_op {
920 enum isl_token_type op;
921 isl_obj_type arg;
922 isl_obj_type res;
923 isc_un_op_fn fn;
925 struct isc_named_un_op {
926 char *name;
927 struct isc_un_op op;
929 struct isc_named_un_op named_un_ops[] = {
930 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
931 (isc_un_op_fn) &isl_union_map_affine_hull } },
932 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
933 (isc_un_op_fn) &isl_union_set_affine_hull } },
934 {"card", { -1, isl_obj_union_set,
935 isl_obj_union_pw_qpolynomial,
936 (isc_un_op_fn) &isl_union_set_card } },
937 {"card", { -1, isl_obj_union_map,
938 isl_obj_union_pw_qpolynomial,
939 (isc_un_op_fn) &isl_union_map_card } },
940 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
941 (isc_un_op_fn) &isl_union_set_coalesce } },
942 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
943 (isc_un_op_fn) &isl_union_map_coalesce } },
944 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
945 isl_obj_union_pw_qpolynomial,
946 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
947 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
948 isl_obj_union_pw_qpolynomial_fold,
949 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
950 {"coefficients", { -1, isl_obj_union_set,
951 isl_obj_union_set,
952 (isc_un_op_fn) &isl_union_set_coefficients } },
953 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
954 (isc_un_op_fn) &isl_union_set_solutions } },
955 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
956 (isc_un_op_fn) &isl_union_map_deltas } },
957 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
958 (isc_un_op_fn) &isl_union_map_deltas_map } },
959 {"dom", { -1, isl_obj_schedule, isl_obj_union_set,
960 (isc_un_op_fn) &schedule_domain } },
961 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
962 (isc_un_op_fn) &isl_union_map_domain } },
963 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
964 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
965 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
966 isl_obj_union_set,
967 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
968 {"domain", { -1, isl_obj_schedule, isl_obj_union_set,
969 (isc_un_op_fn) &schedule_domain } },
970 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
971 (isc_un_op_fn) &isl_union_map_domain } },
972 {"domain", { -1, isl_obj_union_pw_qpolynomial,
973 isl_obj_union_set,
974 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
975 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
976 isl_obj_union_set,
977 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
978 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
979 (isc_un_op_fn) &isl_union_map_domain_map } },
980 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
981 (isc_un_op_fn) &isl_union_map_range } },
982 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
983 (isc_un_op_fn) &isl_union_map_range } },
984 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
985 (isc_un_op_fn) &isl_union_map_range_map } },
986 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
987 (isc_un_op_fn) &isl_union_set_identity } },
988 {"lattice_width", { -1, isl_obj_union_set,
989 isl_obj_union_pw_qpolynomial,
990 (isc_un_op_fn) &isl_union_set_lattice_width } },
991 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
992 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
993 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
994 (isc_un_op_fn) &isl_union_map_lexmin } },
995 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
996 (isc_un_op_fn) &isl_union_map_lexmax } },
997 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
998 (isc_un_op_fn) &isl_union_set_lexmin } },
999 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
1000 (isc_un_op_fn) &isl_union_set_lexmax } },
1001 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
1002 (isc_un_op_fn) &isl_union_set_lift } },
1003 {"map", { -1, isl_obj_schedule, isl_obj_union_map,
1004 (isc_un_op_fn) &schedule_map } },
1005 {"params", { -1, isl_obj_union_map, isl_obj_set,
1006 (isc_un_op_fn) &isl_union_map_params } },
1007 {"params", { -1, isl_obj_union_set, isl_obj_set,
1008 (isc_un_op_fn) &isl_union_set_params } },
1009 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
1010 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
1011 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
1012 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
1013 {"poly", { -1, isl_obj_union_pw_qpolynomial,
1014 isl_obj_union_pw_qpolynomial,
1015 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
1016 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
1017 isl_obj_union_pw_qpolynomial,
1018 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
1019 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
1020 isl_obj_union_pw_qpolynomial,
1021 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
1022 #ifdef HAVE_PET
1023 {"parse_file", { -1, isl_obj_str, isl_obj_list,
1024 (isc_un_op_fn) &parse } },
1025 #endif
1026 {"pow", { -1, isl_obj_union_map, isl_obj_list,
1027 (isc_un_op_fn) &union_map_power } },
1028 {"sample", { -1, isl_obj_union_set, isl_obj_set,
1029 (isc_un_op_fn) &union_set_sample } },
1030 {"sample", { -1, isl_obj_union_map, isl_obj_map,
1031 (isc_un_op_fn) &union_map_sample } },
1032 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
1033 (isc_un_op_fn) &union_set_scan } },
1034 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
1035 (isc_un_op_fn) &union_map_scan } },
1036 {"sum", { -1, isl_obj_union_pw_qpolynomial,
1037 isl_obj_union_pw_qpolynomial,
1038 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
1039 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
1040 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
1041 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
1042 (isc_un_op_fn) &isl_union_set_unwrap } },
1043 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
1044 (isc_un_op_fn) &isl_union_map_wrap } },
1045 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
1046 (isc_un_op_fn) &isl_union_map_zip } },
1047 NULL
1050 struct isl_named_obj {
1051 char *name;
1052 struct isl_obj obj;
1055 static void free_obj(struct isl_obj obj)
1057 obj.type->free(obj.v);
1060 static int same_name(const void *entry, const void *val)
1062 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1064 return !strcmp(named->name, val);
1067 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1068 char *name, struct isl_obj obj)
1070 struct isl_hash_table_entry *entry;
1071 uint32_t name_hash;
1072 struct isl_named_obj *named;
1074 name_hash = isl_hash_string(isl_hash_init(), name);
1075 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1076 if (!entry)
1077 goto error;
1078 if (entry->data) {
1079 named = entry->data;
1080 free_obj(named->obj);
1081 free(name);
1082 } else {
1083 named = isl_alloc_type(ctx, struct isl_named_obj);
1084 if (!named)
1085 goto error;
1086 named->name = name;
1087 entry->data = named;
1089 named->obj = obj;
1091 return 0;
1092 error:
1093 free_obj(obj);
1094 free(name);
1095 return -1;
1098 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1099 struct isl_hash_table *table, char *name)
1101 struct isl_obj obj = { isl_obj_none, NULL };
1102 struct isl_hash_table_entry *entry;
1103 uint32_t name_hash;
1105 name_hash = isl_hash_string(isl_hash_init(), name);
1106 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1107 if (entry) {
1108 struct isl_named_obj *named;
1109 named = entry->data;
1110 obj = named->obj;
1111 } else if (isdigit(name[0]))
1112 fprintf(stderr, "unknown identifier '$%s'\n", name);
1113 else
1114 fprintf(stderr, "unknown identifier '%s'\n", name);
1116 free(name);
1117 obj.v = obj.type->copy(obj.v);
1118 return obj;
1121 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1123 if (obj.type == super)
1124 return 1;
1125 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1126 return 1;
1127 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1128 return 1;
1129 if (obj.type == isl_obj_schedule && super == isl_obj_union_map)
1130 return 1;
1131 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1132 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1133 int is_set = isl_space_is_set(space);
1134 isl_space_free(space);
1135 return is_set;
1137 if (obj.type == isl_obj_pw_qpolynomial &&
1138 super == isl_obj_union_pw_qpolynomial)
1139 return 1;
1140 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1141 super == isl_obj_union_pw_qpolynomial_fold)
1142 return 1;
1143 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1144 return 1;
1145 if (obj.type == isl_obj_list) {
1146 struct isl_list *list = obj.v;
1147 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1148 return is_subtype(list->obj[0], super);
1150 if (super == isl_obj_str)
1151 return 1;
1152 return 0;
1155 static struct isl_obj obj_at(struct isl_obj obj, int i)
1157 struct isl_list *list = obj.v;
1159 obj = list->obj[i];
1160 obj.v = obj.type->copy(obj.v);
1162 isl_list_free(list);
1164 return obj;
1167 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1168 isl_obj_type type)
1170 if (obj.type == type)
1171 return obj;
1172 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1173 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1174 obj.type = isl_obj_union_set;
1175 obj.v = isl_union_set_from_set(set);
1176 return obj;
1178 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1179 obj.type = isl_obj_union_map;
1180 obj.v = isl_union_map_from_map(obj.v);
1181 return obj;
1183 if (obj.type == isl_obj_schedule && type == isl_obj_union_map) {
1184 obj.type = isl_obj_union_map;
1185 obj.v = schedule_map(obj.v);
1186 return obj;
1188 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1189 obj.type = isl_obj_union_set;
1190 obj.v = isl_union_set_from_set(obj.v);
1191 return obj;
1193 if (obj.type == isl_obj_pw_qpolynomial &&
1194 type == isl_obj_union_pw_qpolynomial) {
1195 obj.type = isl_obj_union_pw_qpolynomial;
1196 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1197 return obj;
1199 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1200 type == isl_obj_union_pw_qpolynomial_fold) {
1201 obj.type = isl_obj_union_pw_qpolynomial_fold;
1202 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1203 return obj;
1205 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1206 if (type == isl_obj_union_map) {
1207 obj.type = isl_obj_union_map;
1208 return obj;
1210 if (type == isl_obj_union_pw_qpolynomial) {
1211 isl_space *dim = isl_union_set_get_space(obj.v);
1212 isl_union_set_free(obj.v);
1213 obj.v = isl_union_pw_qpolynomial_zero(dim);
1214 obj.type = isl_obj_union_pw_qpolynomial;
1215 return obj;
1217 if (type == isl_obj_union_pw_qpolynomial_fold) {
1218 isl_space *dim = isl_union_set_get_space(obj.v);
1219 isl_union_set_free(obj.v);
1220 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1221 isl_fold_list);
1222 obj.type = isl_obj_union_pw_qpolynomial_fold;
1223 return obj;
1226 if (obj.type == isl_obj_list) {
1227 struct isl_list *list = obj.v;
1228 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1229 return convert(ctx, obj_at(obj, 0), type);
1231 if (type == isl_obj_str) {
1232 isl_str *str;
1233 isl_printer *p;
1234 char *s;
1236 p = isl_printer_to_str(ctx);
1237 if (!p)
1238 goto error;
1239 p = obj.type->print(p, obj.v);
1240 s = isl_printer_get_str(p);
1241 isl_printer_free(p);
1243 str = isl_str_from_string(ctx, s);
1244 if (!str)
1245 goto error;
1246 free_obj(obj);
1247 obj.v = str;
1248 obj.type = isl_obj_str;
1249 return obj;
1252 error:
1253 free_obj(obj);
1254 obj.type = isl_obj_none;
1255 obj.v = NULL;
1256 return obj;
1259 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1260 struct isl_obj lhs)
1262 int i;
1263 int read_tok2 = 0;
1264 struct isl_token *tok, *tok2;
1266 tok = isl_stream_next_token(s);
1267 if (!tok)
1268 return NULL;
1270 for (i = 0; ; ++i) {
1271 if (!bin_ops[i].op)
1272 break;
1273 if (bin_ops[i].op != isl_token_get_type(tok))
1274 continue;
1275 if (!is_subtype(lhs, bin_ops[i].lhs))
1276 continue;
1278 isl_token_free(tok);
1279 return &bin_ops[i];
1282 for (i = 0; ; ++i) {
1283 if (!named_bin_ops[i].name)
1284 break;
1285 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1286 continue;
1287 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1288 continue;
1290 isl_token_free(tok);
1291 return &named_bin_ops[i].op;
1294 for (i = 0; ; ++i) {
1295 if (!compound_bin_ops[i].full)
1296 break;
1297 if (compound_bin_ops[i].op1 != isl_token_get_type(tok))
1298 continue;
1299 if (!read_tok2)
1300 tok2 = isl_stream_next_token(s);
1301 read_tok2 = 1;
1302 if (compound_bin_ops[i].op2 != isl_token_get_type(tok2))
1303 continue;
1304 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
1305 continue;
1307 isl_token_free(tok2);
1308 isl_token_free(tok);
1309 return &compound_bin_ops[i].op;
1312 if (read_tok2)
1313 isl_stream_push_token(s, tok2);
1314 isl_stream_push_token(s, tok);
1316 return NULL;
1319 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1321 int i;
1322 struct isl_token *tok;
1324 tok = isl_stream_next_token(s);
1325 if (!tok)
1326 return NULL;
1328 for (i = 0; ; ++i) {
1329 if (!named_un_ops[i].name)
1330 break;
1331 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1332 continue;
1334 isl_token_free(tok);
1335 return &named_un_ops[i].op;
1338 isl_stream_push_token(s, tok);
1340 return NULL;
1343 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1344 struct isl_obj arg)
1346 int i;
1348 for (i = 0; ; ++i) {
1349 if (!named_un_ops[i].name)
1350 break;
1351 if (named_un_ops[i].op.op != like->op)
1352 continue;
1353 if (!is_subtype(arg, named_un_ops[i].op.arg))
1354 continue;
1356 return &named_un_ops[i].op;
1359 return NULL;
1362 static int is_assign(struct isl_stream *s)
1364 struct isl_token *tok;
1365 struct isl_token *tok2;
1366 int assign;
1368 tok = isl_stream_next_token(s);
1369 if (!tok)
1370 return 0;
1371 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1372 isl_stream_push_token(s, tok);
1373 return 0;
1376 tok2 = isl_stream_next_token(s);
1377 if (!tok2) {
1378 isl_stream_push_token(s, tok);
1379 return 0;
1381 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1382 isl_stream_push_token(s, tok2);
1383 isl_stream_push_token(s, tok);
1385 return assign;
1388 static struct isl_obj read_obj(struct isl_stream *s,
1389 struct isl_hash_table *table);
1390 static struct isl_obj read_expr(struct isl_stream *s,
1391 struct isl_hash_table *table);
1393 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1394 struct isl_hash_table *table, struct isc_un_op *op)
1396 isl_ctx *ctx;
1397 struct isl_obj obj = { isl_obj_none, NULL };
1399 obj = read_obj(s, table);
1400 if (!obj.v)
1401 goto error;
1403 op = find_matching_un_op(op, obj);
1405 ctx = isl_stream_get_ctx(s);
1406 if (!op)
1407 isl_die(ctx, isl_error_invalid,
1408 "no such unary operator defined on given operand",
1409 goto error);
1411 obj = convert(ctx, obj, op->arg);
1412 obj.v = op->fn(obj.v);
1413 obj.type = op->res;
1415 return obj;
1416 error:
1417 free_obj(obj);
1418 obj.type = isl_obj_none;
1419 obj.v = NULL;
1420 return obj;
1423 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1425 struct isl_list *list;
1426 int exact;
1428 if (obj.type != isl_obj_union_map)
1429 obj = convert(ctx, obj, isl_obj_union_map);
1430 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1431 list = isl_list_alloc(ctx, 2);
1432 if (!list)
1433 goto error;
1435 list->obj[0].type = isl_obj_union_map;
1436 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1437 list->obj[1].type = isl_obj_bool;
1438 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1439 obj.v = list;
1440 obj.type = isl_obj_list;
1441 if (exact < 0 || !list->obj[0].v)
1442 goto error;
1444 return obj;
1445 error:
1446 free_obj(obj);
1447 obj.type = isl_obj_none;
1448 obj.v = NULL;
1449 return obj;
1452 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1454 struct isl_list *list = obj.v;
1455 struct isl_token *tok;
1456 isl_ctx *ctx;
1457 isl_val *v;
1458 int i;
1460 tok = isl_stream_next_token(s);
1461 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1462 isl_stream_error(s, tok, "expecting index");
1463 if (tok)
1464 isl_stream_push_token(s, tok);
1465 goto error;
1467 ctx = isl_stream_get_ctx(s);
1468 v = isl_token_get_val(ctx, tok);
1469 i = isl_val_get_num_si(v);
1470 isl_val_free(v);
1471 isl_token_free(tok);
1472 isl_assert(ctx, i < list->n, goto error);
1473 if (isl_stream_eat(s, ']'))
1474 goto error;
1476 return obj_at(obj, i);
1477 error:
1478 free_obj(obj);
1479 obj.type = isl_obj_none;
1480 obj.v = NULL;
1481 return obj;
1484 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1485 struct isl_hash_table *table)
1487 isl_ctx *ctx;
1488 struct isl_obj obj;
1490 obj = read_expr(s, table);
1491 ctx = isl_stream_get_ctx(s);
1492 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1493 is_subtype(obj, isl_obj_union_map), goto error);
1495 if (obj.type == isl_obj_list) {
1496 struct isl_list *list = obj.v;
1497 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1498 obj = obj_at(obj, 0);
1500 if (obj.type == isl_obj_set)
1501 obj = convert(ctx, obj, isl_obj_union_set);
1502 else if (obj.type == isl_obj_map)
1503 obj = convert(ctx, obj, isl_obj_union_map);
1504 if (obj.type == isl_obj_union_set) {
1505 obj.v = isl_union_set_apply(obj.v, umap);
1506 } else
1507 obj.v = isl_union_map_apply_range(obj.v, umap);
1508 if (!obj.v)
1509 goto error2;
1511 if (isl_stream_eat(s, ')'))
1512 goto error2;
1514 return obj;
1515 error:
1516 isl_union_map_free(umap);
1517 error2:
1518 free_obj(obj);
1519 obj.type = isl_obj_none;
1520 obj.v = NULL;
1521 return obj;
1524 static struct isl_obj apply_fun_set(struct isl_obj obj,
1525 __isl_take isl_union_set *uset)
1527 if (obj.type == isl_obj_union_pw_qpolynomial) {
1528 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1529 } else {
1530 obj.type = isl_obj_list;
1531 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1533 return obj;
1536 static struct isl_obj apply_fun_map(struct isl_obj obj,
1537 __isl_take isl_union_map *umap)
1539 if (obj.type == isl_obj_union_pw_qpolynomial) {
1540 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1541 } else {
1542 obj.type = isl_obj_list;
1543 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1545 return obj;
1548 static struct isl_obj apply_fun(struct isl_stream *s,
1549 struct isl_obj obj, struct isl_hash_table *table)
1551 struct isl_obj arg;
1552 isl_ctx *ctx;
1554 arg = read_expr(s, table);
1555 ctx = isl_stream_get_ctx(s);
1556 if (!is_subtype(arg, isl_obj_union_map) &&
1557 !is_subtype(arg, isl_obj_union_set))
1558 isl_die(ctx, isl_error_invalid,
1559 "expecting set of map argument", goto error);
1561 if (arg.type == isl_obj_list) {
1562 struct isl_list *list = arg.v;
1563 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1564 arg = obj_at(arg, 0);
1566 if (arg.type == isl_obj_set)
1567 arg = convert(ctx, arg, isl_obj_union_set);
1568 else if (arg.type == isl_obj_map)
1569 arg = convert(ctx, arg, isl_obj_union_map);
1570 if (arg.type == isl_obj_union_set)
1571 obj = apply_fun_set(obj, arg.v);
1572 else
1573 obj = apply_fun_map(obj, arg.v);
1574 if (!obj.v)
1575 goto error2;
1577 if (isl_stream_eat(s, ')'))
1578 goto error2;
1580 return obj;
1581 error:
1582 free_obj(arg);
1583 error2:
1584 free_obj(obj);
1585 obj.type = isl_obj_none;
1586 obj.v = NULL;
1587 return obj;
1590 struct add_vertex_data {
1591 struct isl_list *list;
1592 int i;
1595 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1597 struct add_vertex_data *data = (struct add_vertex_data *)user;
1598 isl_multi_aff *ma;
1599 isl_set *dom;
1601 ma = isl_vertex_get_expr(vertex);
1602 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1604 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1605 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1606 data->i++;
1608 isl_vertex_free(vertex);
1610 return isl_stat_ok;
1613 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1615 isl_ctx *ctx;
1616 isl_basic_set *hull;
1617 isl_vertices *vertices = NULL;
1618 struct isl_list *list = NULL;
1619 isl_stat r;
1620 struct add_vertex_data *data = (struct add_vertex_data *)user;
1622 set = isl_set_remove_divs(set);
1623 hull = isl_set_convex_hull(set);
1624 vertices = isl_basic_set_compute_vertices(hull);
1625 isl_basic_set_free(hull);
1627 list = data->list;
1629 ctx = isl_vertices_get_ctx(vertices);
1630 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1631 if (!data->list)
1632 goto error;
1634 data->i = 0;
1635 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1637 data->list = isl_list_concat(list, data->list);
1639 isl_vertices_free(vertices);
1641 return r;
1642 error:
1643 data->list = list;
1644 isl_vertices_free(vertices);
1645 return isl_stat_error;
1648 static struct isl_obj vertices(struct isl_stream *s,
1649 struct isl_hash_table *table)
1651 isl_ctx *ctx;
1652 struct isl_obj obj;
1653 struct isl_list *list = NULL;
1654 isl_union_set *uset = NULL;
1655 struct add_vertex_data data = { NULL };
1657 obj = read_expr(s, table);
1658 ctx = isl_stream_get_ctx(s);
1659 obj = convert(ctx, obj, isl_obj_union_set);
1660 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1661 uset = obj.v;
1662 obj.v = NULL;
1664 list = isl_list_alloc(ctx, 0);
1665 if (!list)
1666 goto error;
1668 data.list = list;
1670 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1671 goto error;
1673 isl_union_set_free(uset);
1675 obj.type = isl_obj_list;
1676 obj.v = data.list;
1678 return obj;
1679 error:
1680 isl_union_set_free(uset);
1681 isl_list_free(data.list);
1682 free_obj(obj);
1683 obj.type = isl_obj_none;
1684 obj.v = NULL;
1685 return obj;
1688 static struct isl_obj type_of(struct isl_stream *s,
1689 struct isl_hash_table *table)
1691 isl_ctx *ctx;
1692 struct isl_obj obj;
1693 const char *type = "unknown";
1695 obj = read_expr(s, table);
1697 if (obj.type == isl_obj_map ||
1698 obj.type == isl_obj_union_map)
1699 type = "map";
1700 if (obj.type == isl_obj_set ||
1701 obj.type == isl_obj_union_set)
1702 type = "set";
1703 if (obj.type == isl_obj_pw_multi_aff)
1704 type = "piecewise multi-quasiaffine expression";
1705 if (obj.type == isl_obj_pw_qpolynomial ||
1706 obj.type == isl_obj_union_pw_qpolynomial)
1707 type = "piecewise quasipolynomial";
1708 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1709 obj.type == isl_obj_union_pw_qpolynomial_fold)
1710 type = "piecewise quasipolynomial fold";
1711 if (obj.type == isl_obj_list)
1712 type = "list";
1713 if (obj.type == isl_obj_bool)
1714 type = "boolean";
1715 if (obj.type == isl_obj_str)
1716 type = "string";
1717 if (obj.type == isl_obj_val)
1718 type = "value";
1719 if (obj.type == isl_obj_schedule)
1720 type = "schedule";
1722 free_obj(obj);
1723 obj.type = isl_obj_str;
1724 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1726 return obj;
1729 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1730 struct isl_hash_table *table)
1732 struct isl_obj obj;
1733 isl_ctx *ctx;
1735 obj = read_obj(s, table);
1736 ctx = isl_stream_get_ctx(s);
1737 obj = convert(ctx, obj, isl_obj_union_set);
1738 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1739 return obj.v;
1740 error:
1741 free_obj(obj);
1742 return NULL;
1745 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1746 struct isl_hash_table *table)
1748 struct isl_obj obj;
1749 isl_ctx *ctx;
1751 obj = read_obj(s, table);
1752 ctx = isl_stream_get_ctx(s);
1753 obj = convert(ctx, obj, isl_obj_union_map);
1754 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1755 return obj.v;
1756 error:
1757 free_obj(obj);
1758 return NULL;
1761 /* Read a schedule in the form of either a schedule (tree) or a union map
1762 * from "s" and store the schedule in "access".
1764 static __isl_give isl_union_access_info *access_info_set_schedule(
1765 __isl_take isl_union_access_info *access, struct isl_stream *s,
1766 struct isl_hash_table *table)
1768 struct isl_obj obj;
1769 isl_ctx *ctx;
1771 obj = read_obj(s, table);
1772 if (obj.type == isl_obj_schedule)
1773 return isl_union_access_info_set_schedule(access, obj.v);
1774 ctx = isl_stream_get_ctx(s);
1775 obj = convert(ctx, obj, isl_obj_union_map);
1777 return isl_union_access_info_set_schedule_map(access, obj.v);
1780 static struct isl_obj last_any(struct isl_stream *s,
1781 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1782 __isl_take isl_union_map *may_source)
1784 struct isl_obj obj = { isl_obj_none, NULL };
1785 isl_union_access_info *access;
1786 isl_union_flow *flow;
1787 isl_union_map *sink = NULL;
1788 isl_union_map *may_dep;
1790 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1791 goto error;
1793 sink = read_map(s, table);
1794 if (!sink)
1795 goto error;
1797 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1798 goto error;
1800 access = isl_union_access_info_from_sink(sink);
1801 access = isl_union_access_info_set_must_source(access, must_source);
1802 access = isl_union_access_info_set_may_source(access, may_source);
1803 access = access_info_set_schedule(access, s, table);
1804 flow = isl_union_access_info_compute_flow(access);
1805 may_dep = isl_union_flow_get_may_dependence(flow);
1806 isl_union_flow_free(flow);
1808 if (!may_dep)
1809 return obj;
1811 obj.type = isl_obj_union_map;
1812 obj.v = may_dep;
1814 return obj;
1815 error:
1816 isl_union_map_free(may_source);
1817 isl_union_map_free(must_source);
1818 isl_union_map_free(sink);
1819 free_obj(obj);
1820 obj.type = isl_obj_none;
1821 obj.v = NULL;
1822 return obj;
1825 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1827 struct isl_obj obj = { isl_obj_none, NULL };
1828 isl_union_access_info *access;
1829 isl_union_flow *flow;
1830 isl_union_map *may_source = NULL;
1831 isl_union_map *sink = NULL;
1832 isl_union_map *may_dep;
1834 may_source = read_map(s, table);
1835 if (!may_source)
1836 goto error;
1838 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1839 isl_union_map *must_source;
1840 must_source = read_map(s, table);
1841 if (!must_source)
1842 goto error;
1843 return last_any(s, table, must_source, may_source);
1846 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1847 goto error;
1849 sink = read_map(s, table);
1850 if (!sink)
1851 goto error;
1853 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1854 goto error;
1856 access = isl_union_access_info_from_sink(sink);
1857 access = isl_union_access_info_set_may_source(access, may_source);
1858 access = access_info_set_schedule(access, s, table);
1859 flow = isl_union_access_info_compute_flow(access);
1860 may_dep = isl_union_flow_get_may_dependence(flow);
1861 isl_union_flow_free(flow);
1863 if (!may_dep)
1864 return obj;
1866 obj.type = isl_obj_union_map;
1867 obj.v = may_dep;
1869 return obj;
1870 error:
1871 isl_union_map_free(may_source);
1872 isl_union_map_free(sink);
1873 free_obj(obj);
1874 obj.type = isl_obj_none;
1875 obj.v = NULL;
1876 return obj;
1879 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1881 struct isl_obj obj = { isl_obj_none, NULL };
1882 struct isl_list *list = NULL;
1883 isl_union_access_info *access;
1884 isl_union_flow *flow;
1885 isl_union_map *must_source = NULL;
1886 isl_union_map *sink = NULL;
1887 isl_union_map *must_dep;
1888 isl_union_map *must_no_source;
1890 must_source = read_map(s, table);
1891 if (!must_source)
1892 goto error;
1894 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1895 isl_union_map *may_source;
1896 may_source = read_map(s, table);
1897 if (!may_source)
1898 goto error;
1899 return last_any(s, table, must_source, may_source);
1902 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1903 if (!list)
1904 goto error;
1906 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1907 goto error;
1909 sink = read_map(s, table);
1910 if (!sink)
1911 goto error;
1913 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1914 goto error;
1916 access = isl_union_access_info_from_sink(sink);
1917 access = isl_union_access_info_set_must_source(access, must_source);
1918 access = access_info_set_schedule(access, s, table);
1919 flow = isl_union_access_info_compute_flow(access);
1920 must_dep = isl_union_flow_get_must_dependence(flow);
1921 must_no_source = isl_union_flow_get_must_no_source(flow);
1922 isl_union_flow_free(flow);
1924 list->obj[0].type = isl_obj_union_map;
1925 list->obj[0].v = must_dep;
1926 list->obj[1].type = isl_obj_union_map;
1927 list->obj[1].v = must_no_source;
1929 if (!must_dep || !must_no_source) {
1930 isl_list_free(list);
1931 return obj;
1934 obj.v = list;
1935 obj.type = isl_obj_list;
1937 return obj;
1938 error:
1939 isl_list_free(list);
1940 isl_union_map_free(must_source);
1941 isl_union_map_free(sink);
1942 free_obj(obj);
1943 obj.type = isl_obj_none;
1944 obj.v = NULL;
1945 return obj;
1948 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1949 struct isl_hash_table *table)
1951 isl_union_set *domain;
1952 isl_union_map *validity;
1953 isl_union_map *proximity;
1955 domain = read_set(s, table);
1956 if (!domain)
1957 return NULL;
1959 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1960 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1962 for (;;) {
1963 isl_union_map *umap;
1964 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1965 umap = read_map(s, table);
1966 validity = isl_union_map_union(validity, umap);
1967 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1968 umap = read_map(s, table);
1969 proximity = isl_union_map_union(proximity, umap);
1970 } else
1971 break;
1974 return isl_union_set_compute_schedule(domain, validity, proximity);
1977 static struct isl_obj schedule(struct isl_stream *s,
1978 struct isl_hash_table *table)
1980 struct isl_obj obj = { isl_obj_none, NULL };
1981 isl_schedule *schedule;
1983 schedule = get_schedule(s, table);
1985 obj.v = schedule;
1986 obj.type = isl_obj_schedule;
1988 return obj;
1991 /* Read a schedule for code generation in the form of either
1992 * a schedule tree or a union map.
1993 * If the input is a set rather than a map, then we construct
1994 * an identity union map schedule on the given set.
1996 static struct isl_obj get_codegen_schedule(struct isl_stream *s,
1997 struct isl_hash_table *table)
1999 struct isl_obj obj;
2000 isl_ctx *ctx;
2002 obj = read_obj(s, table);
2003 ctx = isl_stream_get_ctx(s);
2005 if (obj.type == isl_obj_schedule)
2006 return obj;
2007 if (is_subtype(obj, isl_obj_union_set)) {
2008 obj = convert(ctx, obj, isl_obj_union_set);
2009 obj.v = isl_union_set_identity(obj.v);
2010 obj.type = isl_obj_union_map;
2012 if (is_subtype(obj, isl_obj_union_map))
2013 return convert(ctx, obj, isl_obj_union_map);
2015 free_obj(obj);
2016 obj.v = NULL;
2017 obj.type = isl_obj_none;
2018 isl_die(ctx, isl_error_invalid, "expecting schedule, set or map",
2019 return obj);
2022 /* Generate an AST for the given schedule and options and return the AST.
2024 static __isl_give isl_ast_node *get_ast_from_union_map(
2025 __isl_take isl_union_map *schedule, __isl_take isl_union_map *options)
2027 isl_space *space;
2028 isl_set *context;
2029 isl_ast_build *build;
2030 isl_ast_node *tree;
2032 space = isl_union_map_get_space(schedule);
2033 context = isl_set_universe(isl_space_params(space));
2035 build = isl_ast_build_from_context(context);
2036 build = isl_ast_build_set_options(build, options);
2037 tree = isl_ast_build_ast_from_schedule(build, schedule);
2038 isl_ast_build_free(build);
2040 return tree;
2043 /* Generate an AST for the given schedule and return the AST.
2045 static __isl_give isl_ast_node *get_ast_from_schedule(
2046 __isl_take isl_schedule *schedule)
2048 isl_ast_build *build;
2049 isl_ast_node *tree;
2051 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
2052 tree = isl_ast_build_node_from_schedule(build, schedule);
2053 isl_ast_build_free(build);
2055 return tree;
2058 /* Print the AST "tree" on the printer "p".
2060 static __isl_give isl_printer *print_ast(__isl_take isl_printer *p,
2061 __isl_take isl_ast_node *tree)
2063 int format;
2065 format = isl_printer_get_output_format(p);
2066 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2067 p = isl_printer_print_ast_node(p, tree);
2068 p = isl_printer_set_output_format(p, format);
2070 isl_ast_node_free(tree);
2072 return p;
2075 /* Perform the codegen operation.
2076 * In particular, read a schedule, check if the user has specified any options
2077 * and then generate an AST from the schedule (and options) and print it.
2078 * In case the schedule is specified as a schedule tree, the AST generation
2079 * options are embedded in the schedule, so they are not read in separately.
2081 static __isl_give isl_printer *codegen(struct isl_stream *s,
2082 struct isl_hash_table *table, __isl_take isl_printer *p)
2084 struct isl_obj obj;
2085 isl_ast_node *tree;
2087 obj = get_codegen_schedule(s, table);
2088 if (!obj.v)
2089 return p;
2091 if (obj.type == isl_obj_schedule) {
2092 isl_schedule *schedule = obj.v;
2094 tree = get_ast_from_schedule(schedule);
2095 } else {
2096 isl_union_map *schedule = obj.v;
2097 isl_union_map *options;
2099 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
2100 options = read_map(s, table);
2101 else
2102 options = isl_union_map_empty(
2103 isl_union_map_get_space(schedule));
2105 tree = get_ast_from_union_map(schedule, options);
2108 if (tree)
2109 p = print_ast(p, tree);
2111 isl_stream_eat(s, ';');
2113 return p;
2116 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2118 struct isl_token *tok;
2119 isl_ctx *ctx;
2120 isl_val *v;
2122 ctx = isl_stream_get_ctx(s);
2123 if (isl_stream_eat_if_available(s, '+'))
2124 return transitive_closure(ctx, obj);
2126 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2127 if (obj.type != isl_obj_union_map)
2128 obj = convert(ctx, obj, isl_obj_union_map);
2130 tok = isl_stream_next_token(s);
2131 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2132 isl_stream_error(s, tok, "expecting integer exponent");
2133 if (tok)
2134 isl_stream_push_token(s, tok);
2135 goto error;
2138 v = isl_token_get_val(ctx, tok);
2139 if (isl_val_is_zero(v)) {
2140 isl_stream_error(s, tok, "expecting non-zero exponent");
2141 isl_val_free(v);
2142 if (tok)
2143 isl_stream_push_token(s, tok);
2144 goto error;
2147 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2148 isl_token_free(tok);
2149 if (!obj.v)
2150 goto error;
2152 return obj;
2153 error:
2154 free_obj(obj);
2155 obj.type = isl_obj_none;
2156 obj.v = NULL;
2157 return obj;
2160 static struct isl_obj check_assert(struct isl_stream *s,
2161 struct isl_hash_table *table)
2163 struct isl_obj obj;
2164 isl_ctx *ctx;
2166 obj = read_expr(s, table);
2167 ctx = isl_stream_get_ctx(s);
2168 if (obj.type != isl_obj_bool)
2169 isl_die(ctx, isl_error_invalid,
2170 "expecting boolean expression", goto error);
2171 if (obj.v != &iscc_bool_true)
2172 isl_die(ctx, isl_error_unknown,
2173 "assertion failed", abort());
2174 error:
2175 free_obj(obj);
2176 obj.type = isl_obj_none;
2177 obj.v = NULL;
2178 return obj;
2181 static struct isl_obj read_from_file(struct isl_stream *s)
2183 isl_ctx *ctx;
2184 struct isl_obj obj;
2185 struct isl_token *tok;
2186 struct isl_stream *s_file;
2187 struct iscc_options *options;
2188 char *name;
2189 FILE *file;
2191 tok = isl_stream_next_token(s);
2192 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2193 isl_stream_error(s, tok, "expecting filename");
2194 isl_token_free(tok);
2195 goto error;
2198 ctx = isl_stream_get_ctx(s);
2199 options = isl_ctx_peek_iscc_options(ctx);
2200 if (!options || !options->io) {
2201 isl_token_free(tok);
2202 isl_die(ctx, isl_error_invalid,
2203 "read operation not allowed", goto error);
2206 name = isl_token_get_str(ctx, tok);
2207 isl_token_free(tok);
2208 file = fopen(name, "r");
2209 free(name);
2210 isl_assert(ctx, file, goto error);
2212 s_file = isl_stream_new_file(ctx, file);
2213 if (!s_file) {
2214 fclose(file);
2215 goto error;
2218 obj = isl_stream_read_obj(s_file);
2220 isl_stream_free(s_file);
2221 fclose(file);
2223 return obj;
2224 error:
2225 obj.type = isl_obj_none;
2226 obj.v = NULL;
2227 return obj;
2230 static struct isl_obj write_to_file(struct isl_stream *s,
2231 struct isl_hash_table *table)
2233 struct isl_obj obj = { isl_obj_none, NULL };
2234 struct isl_token *tok;
2235 struct isl_stream *s_file;
2236 struct iscc_options *options;
2237 char *name;
2238 FILE *file;
2239 isl_ctx *ctx;
2240 isl_printer *p;
2242 tok = isl_stream_next_token(s);
2243 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2244 isl_stream_error(s, tok, "expecting filename");
2245 isl_token_free(tok);
2246 goto error;
2249 obj = read_expr(s, table);
2251 ctx = isl_stream_get_ctx(s);
2252 options = isl_ctx_peek_iscc_options(ctx);
2253 if (!options || !options->io) {
2254 isl_token_free(tok);
2255 isl_die(ctx, isl_error_invalid,
2256 "write operation not allowed", goto error);
2259 name = isl_token_get_str(ctx, tok);
2260 isl_token_free(tok);
2261 file = fopen(name, "w");
2262 free(name);
2263 if (!file)
2264 isl_die(ctx, isl_error_unknown,
2265 "could not open file for writing", goto error);
2267 p = isl_printer_to_file(ctx, file);
2268 p = isl_printer_set_output_format(p, options->format);
2269 p = obj.type->print(p, obj.v);
2270 p = isl_printer_end_line(p);
2271 isl_printer_free(p);
2273 fclose(file);
2274 error:
2275 free_obj(obj);
2276 obj.type = isl_obj_none;
2277 obj.v = NULL;
2278 return obj;
2281 static struct isl_obj read_string_if_available(struct isl_stream *s)
2283 struct isl_token *tok;
2284 struct isl_obj obj = { isl_obj_none, NULL };
2286 tok = isl_stream_next_token(s);
2287 if (!tok)
2288 return obj;
2289 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2290 isl_str *str;
2291 str = isl_str_alloc(isl_stream_get_ctx(s));
2292 if (!str)
2293 goto error;
2294 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2295 isl_token_free(tok);
2296 obj.v = str;
2297 obj.type = isl_obj_str;
2298 } else
2299 isl_stream_push_token(s, tok);
2300 return obj;
2301 error:
2302 isl_token_free(tok);
2303 return obj;
2306 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2308 struct isl_token *tok;
2309 struct isl_obj obj = { isl_obj_none, NULL };
2310 int type;
2312 tok = isl_stream_next_token(s);
2313 if (!tok)
2314 return obj;
2315 type = isl_token_get_type(tok);
2316 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2317 int is_true = type == ISL_TOKEN_TRUE;
2318 isl_token_free(tok);
2319 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2320 obj.type = isl_obj_bool;
2321 } else
2322 isl_stream_push_token(s, tok);
2323 return obj;
2326 static __isl_give char *read_ident(struct isl_stream *s)
2328 char *name;
2329 isl_val *v;
2330 struct isl_token *tok, *tok2;
2332 name = isl_stream_read_ident_if_available(s);
2333 if (name)
2334 return name;
2336 tok = isl_stream_next_token(s);
2337 if (!tok)
2338 return NULL;
2339 if (isl_token_get_type(tok) != '$') {
2340 isl_stream_push_token(s, tok);
2341 return NULL;
2343 tok2 = isl_stream_next_token(s);
2344 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2345 if (tok2)
2346 isl_stream_push_token(s, tok2);
2347 isl_stream_push_token(s, tok);
2348 return NULL;
2351 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2352 name = isl_val_to_str(v);
2353 isl_val_free(v);
2354 isl_token_free(tok);
2355 isl_token_free(tok2);
2357 return name;
2360 static struct isl_obj read_list(struct isl_stream *s,
2361 struct isl_hash_table *table, struct isl_obj obj)
2363 struct isl_list *list;
2365 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2366 if (!list)
2367 goto error;
2368 list->obj[0] = obj;
2369 list->obj[1] = read_obj(s, table);
2370 obj.v = list;
2371 obj.type = isl_obj_list;
2373 if (!list->obj[1].v)
2374 goto error;
2376 while (isl_stream_eat_if_available(s, ',')) {
2377 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2378 if (!obj.v)
2379 goto error;
2382 return obj;
2383 error:
2384 free_obj(obj);
2385 obj.type = isl_obj_none;
2386 obj.v = NULL;
2387 return obj;
2390 static struct isl_obj read_obj(struct isl_stream *s,
2391 struct isl_hash_table *table)
2393 isl_ctx *ctx;
2394 struct isl_obj obj = { isl_obj_none, NULL };
2395 char *name = NULL;
2396 struct isc_un_op *op = NULL;
2398 obj = read_string_if_available(s);
2399 if (obj.v)
2400 return obj;
2401 obj = read_bool_if_available(s);
2402 if (obj.v)
2403 return obj;
2404 ctx = isl_stream_get_ctx(s);
2405 if (isl_stream_eat_if_available(s, '(')) {
2406 if (isl_stream_next_token_is(s, ')')) {
2407 obj.type = isl_obj_list;
2408 obj.v = isl_list_alloc(ctx, 0);
2409 } else {
2410 obj = read_expr(s, table);
2411 if (obj.v && isl_stream_eat_if_available(s, ','))
2412 obj = read_list(s, table, obj);
2414 if (!obj.v || isl_stream_eat(s, ')'))
2415 goto error;
2416 } else {
2417 op = read_prefix_un_op_if_available(s);
2418 if (op)
2419 return read_un_op_expr(s, table, op);
2421 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2422 return check_assert(s, table);
2423 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2424 return read_from_file(s);
2425 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2426 return write_to_file(s, table);
2427 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2428 return vertices(s, table);
2429 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2430 return any(s, table);
2431 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2432 return last(s, table);
2433 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2434 return schedule(s, table);
2435 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2436 return type_of(s, table);
2438 name = read_ident(s);
2439 if (name)
2440 obj = stored_obj(ctx, table, name);
2441 else
2442 obj = isl_stream_read_obj(s);
2443 if (!obj.v)
2444 goto error;
2447 if (isl_stream_eat_if_available(s, '^'))
2448 obj = power(s, obj);
2449 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2450 obj = obj_at_index(s, obj);
2451 else if (is_subtype(obj, isl_obj_union_map) &&
2452 isl_stream_eat_if_available(s, '(')) {
2453 obj = convert(ctx, obj, isl_obj_union_map);
2454 obj = apply(s, obj.v, table);
2455 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2456 isl_stream_eat_if_available(s, '(')) {
2457 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2458 obj = apply_fun(s, obj, table);
2459 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2460 isl_stream_eat_if_available(s, '(')) {
2461 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2462 obj = apply_fun(s, obj, table);
2465 return obj;
2466 error:
2467 free_obj(obj);
2468 obj.type = isl_obj_none;
2469 obj.v = NULL;
2470 return obj;
2473 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2474 struct isl_obj lhs, struct isl_obj rhs)
2476 int i;
2478 for (i = 0; ; ++i) {
2479 if (!bin_ops[i].op)
2480 break;
2481 if (bin_ops[i].op != like->op)
2482 continue;
2483 if (!is_subtype(lhs, bin_ops[i].lhs))
2484 continue;
2485 if (!is_subtype(rhs, bin_ops[i].rhs))
2486 continue;
2488 return &bin_ops[i];
2491 for (i = 0; ; ++i) {
2492 if (!named_bin_ops[i].name)
2493 break;
2494 if (named_bin_ops[i].op.op != like->op)
2495 continue;
2496 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2497 continue;
2498 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2499 continue;
2501 return &named_bin_ops[i].op;
2504 for (i = 0; ; ++i) {
2505 if (!compound_bin_ops[i].full)
2506 break;
2507 if (compound_bin_ops[i].op.op != like->op)
2508 continue;
2509 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
2510 continue;
2511 if (!is_subtype(rhs, compound_bin_ops[i].op.rhs))
2512 continue;
2514 return &compound_bin_ops[i].op;
2517 return NULL;
2520 static int next_is_neg_int(struct isl_stream *s)
2522 struct isl_token *tok;
2523 int ret;
2525 tok = isl_stream_next_token(s);
2526 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2527 isl_val *v;
2528 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2529 ret = isl_val_is_neg(v);
2530 isl_val_free(v);
2531 } else
2532 ret = 0;
2533 isl_stream_push_token(s, tok);
2535 return ret;
2538 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2539 struct isl_obj lhs, struct isl_obj rhs)
2541 struct isl_obj obj;
2543 lhs = convert(ctx, lhs, op->lhs);
2544 rhs = convert(ctx, rhs, op->rhs);
2545 if (op->res != isl_obj_bool)
2546 obj.v = op->o.fn(lhs.v, rhs.v);
2547 else {
2548 int res = op->o.test(lhs.v, rhs.v);
2549 free_obj(lhs);
2550 free_obj(rhs);
2551 obj.v = iscc_bool_from_int(res);
2553 obj.type = op->res;
2555 return obj;
2558 static struct isl_obj read_expr(struct isl_stream *s,
2559 struct isl_hash_table *table)
2561 isl_ctx *ctx;
2562 struct isl_obj obj = { isl_obj_none, NULL };
2563 struct isl_obj right_obj = { isl_obj_none, NULL };
2565 obj = read_obj(s, table);
2566 ctx = isl_stream_get_ctx(s);
2567 for (; obj.v;) {
2568 struct isc_bin_op *op = NULL;
2570 op = read_bin_op_if_available(s, obj);
2571 if (!op)
2572 break;
2574 right_obj = read_obj(s, table);
2576 op = find_matching_bin_op(op, obj, right_obj);
2578 if (!op)
2579 isl_die(ctx, isl_error_invalid,
2580 "no such binary operator defined on given operands",
2581 goto error);
2583 obj = call_bin_op(ctx, op, obj, right_obj);
2586 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2587 right_obj = read_obj(s, table);
2588 obj.v = isl_val_add(obj.v, right_obj.v);
2591 return obj;
2592 error:
2593 free_obj(right_obj);
2594 free_obj(obj);
2595 obj.type = isl_obj_none;
2596 obj.v = NULL;
2597 return obj;
2600 static __isl_give isl_printer *source_file(struct isl_stream *s,
2601 struct isl_hash_table *table, __isl_take isl_printer *p);
2603 /* Print "obj" to the printer "p".
2604 * If the object is a schedule, then print it in block format.
2606 static __isl_give isl_printer *print_obj(__isl_take isl_printer *p,
2607 struct isl_obj obj)
2609 if (obj.type != isl_obj_schedule)
2610 return obj.type->print(p, obj.v);
2612 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
2613 p = obj.type->print(p, obj.v);
2614 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2616 return p;
2619 static __isl_give isl_printer *read_line(struct isl_stream *s,
2620 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2622 isl_ctx *ctx;
2623 struct isl_obj obj = { isl_obj_none, NULL };
2624 char *lhs = NULL;
2625 int assign = 0;
2626 int only_print = 0;
2627 struct isc_bin_op *op = NULL;
2628 char buf[30];
2630 if (!p)
2631 return NULL;
2632 if (isl_stream_is_empty(s))
2633 return p;
2635 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2636 return source_file(s, table, p);
2637 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2638 return codegen(s, table, p);
2640 assign = is_assign(s);
2641 if (assign) {
2642 lhs = isl_stream_read_ident_if_available(s);
2643 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2644 goto error;
2645 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2646 only_print = 1;
2647 else if (!tty)
2648 only_print = 1;
2650 obj = read_expr(s, table);
2651 ctx = isl_stream_get_ctx(s);
2652 if (isl_stream_eat(s, ';'))
2653 goto error;
2655 if (only_print) {
2656 if (obj.type != isl_obj_none && obj.v != NULL) {
2657 p = print_obj(p, obj);
2658 p = isl_printer_end_line(p);
2660 free_obj(obj);
2661 return p;
2663 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2664 static int count = 0;
2665 snprintf(buf, sizeof(buf), "$%d", count++);
2666 lhs = strdup(buf + 1);
2668 p = isl_printer_print_str(p, buf);
2669 p = isl_printer_print_str(p, " := ");
2670 p = obj.type->print(p, obj.v);
2671 p = isl_printer_end_line(p);
2673 if (lhs && do_assign(ctx, table, lhs, obj))
2674 return p;
2676 return p;
2677 error:
2678 isl_stream_flush_tokens(s);
2679 isl_stream_skip_line(s);
2680 free(lhs);
2681 free_obj(obj);
2682 return p;
2685 static isl_stat free_cb(void **entry, void *user)
2687 struct isl_named_obj *named = *entry;
2689 free_obj(named->obj);
2690 free(named->name);
2691 free(named);
2693 return isl_stat_ok;
2696 static void register_named_ops(struct isl_stream *s)
2698 int i;
2700 for (i = 0; i < ISCC_N_OP; ++i) {
2701 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2702 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2705 for (i = 0; ; ++i) {
2706 if (!named_un_ops[i].name)
2707 break;
2708 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2709 named_un_ops[i].name);
2710 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2713 for (i = 0; ; ++i) {
2714 if (!named_bin_ops[i].name)
2715 break;
2716 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2717 named_bin_ops[i].name);
2718 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2721 for (i = 0; ; ++i) {
2722 if (!compound_bin_ops[i].full)
2723 break;
2724 compound_bin_ops[i].op.op = isl_stream_register_keyword(s,
2725 compound_bin_ops[i].full);
2726 assert(compound_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2730 static __isl_give isl_printer *source_file(struct isl_stream *s,
2731 struct isl_hash_table *table, __isl_take isl_printer *p)
2733 isl_ctx *ctx;
2734 struct isl_token *tok;
2735 struct isl_stream *s_file;
2736 struct iscc_options *options;
2737 char *name;
2738 FILE *file;
2740 tok = isl_stream_next_token(s);
2741 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2742 isl_stream_error(s, tok, "expecting filename");
2743 isl_token_free(tok);
2744 return p;
2747 isl_stream_eat(s, ';');
2749 ctx = isl_stream_get_ctx(s);
2750 options = isl_ctx_peek_iscc_options(ctx);
2751 if (!options || !options->io) {
2752 isl_token_free(tok);
2753 isl_die(ctx, isl_error_invalid,
2754 "source operation not allowed", return p);
2757 name = isl_token_get_str(ctx, tok);
2758 isl_token_free(tok);
2759 file = fopen(name, "r");
2760 free(name);
2761 isl_assert(ctx, file, return p);
2763 s_file = isl_stream_new_file(ctx, file);
2764 if (!s_file) {
2765 fclose(file);
2766 return p;
2769 register_named_ops(s_file);
2771 while (!isl_stream_is_empty(s_file))
2772 p = read_line(s_file, table, p, 0);
2774 isl_stream_free(s_file);
2775 fclose(file);
2777 return p;
2780 int main(int argc, char **argv)
2782 struct isl_ctx *ctx;
2783 struct isl_stream *s;
2784 struct isl_hash_table *table;
2785 struct iscc_options *options;
2786 isl_printer *p;
2787 int tty = isatty(0);
2789 options = iscc_options_new_with_defaults();
2790 assert(options);
2792 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2793 pet_options_set_autodetect(ctx, 1);
2794 pet_options_set_encapsulate_dynamic_control(ctx, 1);
2795 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2796 s = isl_stream_new_file(ctx, stdin);
2797 assert(s);
2798 table = isl_hash_table_alloc(ctx, 10);
2799 assert(table);
2800 p = isl_printer_to_file(ctx, stdout);
2801 p = isl_printer_set_output_format(p, options->format);
2802 assert(p);
2804 register_named_ops(s);
2806 install_signal_handler(ctx);
2808 while (p) {
2809 int empty;
2811 empty = isl_stream_is_empty(s);
2812 if (empty && isl_ctx_last_error(ctx) != isl_error_abort)
2813 break;
2814 if (!empty)
2815 p = read_line(s, table, p, tty);
2816 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2817 fprintf(stderr, "Interrupted\n");
2818 isl_ctx_resume(ctx);
2819 isl_ctx_reset_error(ctx);
2823 remove_signal_handler(ctx);
2825 isl_printer_free(p);
2826 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2827 isl_hash_table_free(ctx, table);
2828 isl_stream_free(s);
2829 isl_ctx_free(ctx);
2831 return 0;