iscc: turn on --pet-encapsulate-dynamic-control by default
[barvinok.git] / iscc.c
blob89c060921ef7a7a1173a053238f82d0d311af132
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 static __isl_give isl_list *parse(__isl_take isl_str *str)
761 isl_ctx *ctx;
762 struct isl_list *list;
763 struct pet_scop *scop;
764 isl_schedule *sched;
765 isl_union_map *may_reads, *must_writes, *may_writes;
766 isl_union_set *domain;
767 struct iscc_options *options;
769 if (!str)
770 return NULL;
771 ctx = str->ctx;
773 options = isl_ctx_peek_iscc_options(ctx);
774 if (!options || !options->io) {
775 isl_str_free(str);
776 isl_die(ctx, isl_error_invalid,
777 "parse_file operation not allowed", return NULL);
780 list = isl_list_alloc(ctx, 5);
781 if (!list)
782 goto error;
784 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
785 domain = pet_scop_collect_domains(scop);
786 sched = scop ? isl_schedule_copy(scop->schedule) : NULL;
787 may_reads = pet_scop_collect_may_reads(scop);
788 may_writes = pet_scop_collect_may_writes(scop);
789 must_writes = pet_scop_collect_must_writes(scop);
790 pet_scop_free(scop);
792 list->obj[0].type = isl_obj_union_set;
793 list->obj[0].v = domain;
794 list->obj[1].type = isl_obj_union_map;
795 list->obj[1].v = must_writes;
796 list->obj[2].type = isl_obj_union_map;
797 list->obj[2].v = may_writes;
798 list->obj[3].type = isl_obj_union_map;
799 list->obj[3].v = may_reads;
800 list->obj[4].type = isl_obj_schedule;
801 list->obj[4].v = sched;
803 if (!list->obj[0].v || !list->obj[1].v ||
804 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
805 goto error;
807 isl_str_free(str);
808 return list;
809 error:
810 isl_list_free(list);
811 isl_str_free(str);
812 return NULL;
814 #endif
816 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
818 isl_union_set **scan = (isl_union_set **) user;
820 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
822 return isl_stat_ok;
825 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
827 isl_union_set *scan;
829 scan = isl_union_set_empty(isl_union_set_get_space(uset));
831 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
832 isl_union_set_free(scan);
833 return uset;
836 isl_union_set_free(uset);
837 return scan;
840 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
842 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
845 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
846 __isl_take isl_union_pw_qpolynomial *upwqp)
848 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
851 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
852 __isl_take isl_union_pw_qpolynomial *upwqp)
854 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
857 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
858 __isl_take isl_union_pw_qpolynomial *upwqp)
860 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
863 /* Return the domain of "schedule".
865 static __isl_give isl_union_set *schedule_domain(
866 __isl_take isl_schedule *schedule)
868 isl_union_set *domain;
870 domain = isl_schedule_get_domain(schedule);
871 isl_schedule_free(schedule);
873 return domain;
876 /* Convert "schedule" to a union map representation.
878 static __isl_give isl_union_map *schedule_map(__isl_take isl_schedule *schedule)
880 isl_union_map *map;
882 map = isl_schedule_get_map(schedule);
883 isl_schedule_free(schedule);
885 return map;
888 typedef void *(*isc_un_op_fn)(void *arg);
889 struct isc_un_op {
890 enum isl_token_type op;
891 isl_obj_type arg;
892 isl_obj_type res;
893 isc_un_op_fn fn;
895 struct isc_named_un_op {
896 char *name;
897 struct isc_un_op op;
899 struct isc_named_un_op named_un_ops[] = {
900 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
901 (isc_un_op_fn) &isl_union_map_affine_hull } },
902 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
903 (isc_un_op_fn) &isl_union_set_affine_hull } },
904 {"card", { -1, isl_obj_union_set,
905 isl_obj_union_pw_qpolynomial,
906 (isc_un_op_fn) &isl_union_set_card } },
907 {"card", { -1, isl_obj_union_map,
908 isl_obj_union_pw_qpolynomial,
909 (isc_un_op_fn) &isl_union_map_card } },
910 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
911 (isc_un_op_fn) &isl_union_set_coalesce } },
912 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
913 (isc_un_op_fn) &isl_union_map_coalesce } },
914 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
915 isl_obj_union_pw_qpolynomial,
916 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
917 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
918 isl_obj_union_pw_qpolynomial_fold,
919 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
920 {"coefficients", { -1, isl_obj_union_set,
921 isl_obj_union_set,
922 (isc_un_op_fn) &isl_union_set_coefficients } },
923 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
924 (isc_un_op_fn) &isl_union_set_solutions } },
925 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
926 (isc_un_op_fn) &isl_union_map_deltas } },
927 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
928 (isc_un_op_fn) &isl_union_map_deltas_map } },
929 {"dom", { -1, isl_obj_schedule, isl_obj_union_set,
930 (isc_un_op_fn) &schedule_domain } },
931 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
932 (isc_un_op_fn) &isl_union_map_domain } },
933 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
934 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
935 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
936 isl_obj_union_set,
937 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
938 {"domain", { -1, isl_obj_schedule, isl_obj_union_set,
939 (isc_un_op_fn) &schedule_domain } },
940 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
941 (isc_un_op_fn) &isl_union_map_domain } },
942 {"domain", { -1, isl_obj_union_pw_qpolynomial,
943 isl_obj_union_set,
944 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
945 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
946 isl_obj_union_set,
947 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
948 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
949 (isc_un_op_fn) &isl_union_map_domain_map } },
950 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
951 (isc_un_op_fn) &isl_union_map_range } },
952 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
953 (isc_un_op_fn) &isl_union_map_range } },
954 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
955 (isc_un_op_fn) &isl_union_map_range_map } },
956 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
957 (isc_un_op_fn) &isl_union_set_identity } },
958 {"lattice_width", { -1, isl_obj_union_set,
959 isl_obj_union_pw_qpolynomial,
960 (isc_un_op_fn) &isl_union_set_lattice_width } },
961 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
962 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
963 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
964 (isc_un_op_fn) &isl_union_map_lexmin } },
965 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
966 (isc_un_op_fn) &isl_union_map_lexmax } },
967 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
968 (isc_un_op_fn) &isl_union_set_lexmin } },
969 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
970 (isc_un_op_fn) &isl_union_set_lexmax } },
971 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
972 (isc_un_op_fn) &isl_union_set_lift } },
973 {"map", { -1, isl_obj_schedule, isl_obj_union_map,
974 (isc_un_op_fn) &schedule_map } },
975 {"params", { -1, isl_obj_union_map, isl_obj_set,
976 (isc_un_op_fn) &isl_union_map_params } },
977 {"params", { -1, isl_obj_union_set, isl_obj_set,
978 (isc_un_op_fn) &isl_union_set_params } },
979 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
980 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
981 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
982 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
983 {"poly", { -1, isl_obj_union_pw_qpolynomial,
984 isl_obj_union_pw_qpolynomial,
985 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
986 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
987 isl_obj_union_pw_qpolynomial,
988 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
989 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
990 isl_obj_union_pw_qpolynomial,
991 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
992 #ifdef HAVE_PET
993 {"parse_file", { -1, isl_obj_str, isl_obj_list,
994 (isc_un_op_fn) &parse } },
995 #endif
996 {"pow", { -1, isl_obj_union_map, isl_obj_list,
997 (isc_un_op_fn) &union_map_power } },
998 {"sample", { -1, isl_obj_union_set, isl_obj_set,
999 (isc_un_op_fn) &union_set_sample } },
1000 {"sample", { -1, isl_obj_union_map, isl_obj_map,
1001 (isc_un_op_fn) &union_map_sample } },
1002 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
1003 (isc_un_op_fn) &union_set_scan } },
1004 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
1005 (isc_un_op_fn) &union_map_scan } },
1006 {"sum", { -1, isl_obj_union_pw_qpolynomial,
1007 isl_obj_union_pw_qpolynomial,
1008 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
1009 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
1010 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
1011 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
1012 (isc_un_op_fn) &isl_union_set_unwrap } },
1013 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
1014 (isc_un_op_fn) &isl_union_map_wrap } },
1015 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
1016 (isc_un_op_fn) &isl_union_map_zip } },
1017 NULL
1020 struct isl_named_obj {
1021 char *name;
1022 struct isl_obj obj;
1025 static void free_obj(struct isl_obj obj)
1027 obj.type->free(obj.v);
1030 static int same_name(const void *entry, const void *val)
1032 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1034 return !strcmp(named->name, val);
1037 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1038 char *name, struct isl_obj obj)
1040 struct isl_hash_table_entry *entry;
1041 uint32_t name_hash;
1042 struct isl_named_obj *named;
1044 name_hash = isl_hash_string(isl_hash_init(), name);
1045 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1046 if (!entry)
1047 goto error;
1048 if (entry->data) {
1049 named = entry->data;
1050 free_obj(named->obj);
1051 free(name);
1052 } else {
1053 named = isl_alloc_type(ctx, struct isl_named_obj);
1054 if (!named)
1055 goto error;
1056 named->name = name;
1057 entry->data = named;
1059 named->obj = obj;
1061 return 0;
1062 error:
1063 free_obj(obj);
1064 free(name);
1065 return -1;
1068 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1069 struct isl_hash_table *table, char *name)
1071 struct isl_obj obj = { isl_obj_none, NULL };
1072 struct isl_hash_table_entry *entry;
1073 uint32_t name_hash;
1075 name_hash = isl_hash_string(isl_hash_init(), name);
1076 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1077 if (entry) {
1078 struct isl_named_obj *named;
1079 named = entry->data;
1080 obj = named->obj;
1081 } else if (isdigit(name[0]))
1082 fprintf(stderr, "unknown identifier '$%s'\n", name);
1083 else
1084 fprintf(stderr, "unknown identifier '%s'\n", name);
1086 free(name);
1087 obj.v = obj.type->copy(obj.v);
1088 return obj;
1091 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1093 if (obj.type == super)
1094 return 1;
1095 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1096 return 1;
1097 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1098 return 1;
1099 if (obj.type == isl_obj_schedule && super == isl_obj_union_map)
1100 return 1;
1101 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1102 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1103 int is_set = isl_space_is_set(space);
1104 isl_space_free(space);
1105 return is_set;
1107 if (obj.type == isl_obj_pw_qpolynomial &&
1108 super == isl_obj_union_pw_qpolynomial)
1109 return 1;
1110 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1111 super == isl_obj_union_pw_qpolynomial_fold)
1112 return 1;
1113 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1114 return 1;
1115 if (obj.type == isl_obj_list) {
1116 struct isl_list *list = obj.v;
1117 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1118 return is_subtype(list->obj[0], super);
1120 if (super == isl_obj_str)
1121 return 1;
1122 return 0;
1125 static struct isl_obj obj_at(struct isl_obj obj, int i)
1127 struct isl_list *list = obj.v;
1129 obj = list->obj[i];
1130 obj.v = obj.type->copy(obj.v);
1132 isl_list_free(list);
1134 return obj;
1137 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1138 isl_obj_type type)
1140 if (obj.type == type)
1141 return obj;
1142 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1143 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1144 obj.type = isl_obj_union_set;
1145 obj.v = isl_union_set_from_set(set);
1146 return obj;
1148 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1149 obj.type = isl_obj_union_map;
1150 obj.v = isl_union_map_from_map(obj.v);
1151 return obj;
1153 if (obj.type == isl_obj_schedule && type == isl_obj_union_map) {
1154 obj.type = isl_obj_union_map;
1155 obj.v = schedule_map(obj.v);
1156 return obj;
1158 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1159 obj.type = isl_obj_union_set;
1160 obj.v = isl_union_set_from_set(obj.v);
1161 return obj;
1163 if (obj.type == isl_obj_pw_qpolynomial &&
1164 type == isl_obj_union_pw_qpolynomial) {
1165 obj.type = isl_obj_union_pw_qpolynomial;
1166 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1167 return obj;
1169 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1170 type == isl_obj_union_pw_qpolynomial_fold) {
1171 obj.type = isl_obj_union_pw_qpolynomial_fold;
1172 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1173 return obj;
1175 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1176 if (type == isl_obj_union_map) {
1177 obj.type = isl_obj_union_map;
1178 return obj;
1180 if (type == isl_obj_union_pw_qpolynomial) {
1181 isl_space *dim = isl_union_set_get_space(obj.v);
1182 isl_union_set_free(obj.v);
1183 obj.v = isl_union_pw_qpolynomial_zero(dim);
1184 obj.type = isl_obj_union_pw_qpolynomial;
1185 return obj;
1187 if (type == isl_obj_union_pw_qpolynomial_fold) {
1188 isl_space *dim = isl_union_set_get_space(obj.v);
1189 isl_union_set_free(obj.v);
1190 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1191 isl_fold_list);
1192 obj.type = isl_obj_union_pw_qpolynomial_fold;
1193 return obj;
1196 if (obj.type == isl_obj_list) {
1197 struct isl_list *list = obj.v;
1198 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1199 return convert(ctx, obj_at(obj, 0), type);
1201 if (type == isl_obj_str) {
1202 isl_str *str;
1203 isl_printer *p;
1204 char *s;
1206 p = isl_printer_to_str(ctx);
1207 if (!p)
1208 goto error;
1209 p = obj.type->print(p, obj.v);
1210 s = isl_printer_get_str(p);
1211 isl_printer_free(p);
1213 str = isl_str_from_string(ctx, s);
1214 if (!str)
1215 goto error;
1216 free_obj(obj);
1217 obj.v = str;
1218 obj.type = isl_obj_str;
1219 return obj;
1222 error:
1223 free_obj(obj);
1224 obj.type = isl_obj_none;
1225 obj.v = NULL;
1226 return obj;
1229 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1230 struct isl_obj lhs)
1232 int i;
1233 int read_tok2 = 0;
1234 struct isl_token *tok, *tok2;
1236 tok = isl_stream_next_token(s);
1237 if (!tok)
1238 return NULL;
1240 for (i = 0; ; ++i) {
1241 if (!bin_ops[i].op)
1242 break;
1243 if (bin_ops[i].op != isl_token_get_type(tok))
1244 continue;
1245 if (!is_subtype(lhs, bin_ops[i].lhs))
1246 continue;
1248 isl_token_free(tok);
1249 return &bin_ops[i];
1252 for (i = 0; ; ++i) {
1253 if (!named_bin_ops[i].name)
1254 break;
1255 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1256 continue;
1257 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1258 continue;
1260 isl_token_free(tok);
1261 return &named_bin_ops[i].op;
1264 for (i = 0; ; ++i) {
1265 if (!compound_bin_ops[i].full)
1266 break;
1267 if (compound_bin_ops[i].op1 != isl_token_get_type(tok))
1268 continue;
1269 if (!read_tok2)
1270 tok2 = isl_stream_next_token(s);
1271 read_tok2 = 1;
1272 if (compound_bin_ops[i].op2 != isl_token_get_type(tok2))
1273 continue;
1274 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
1275 continue;
1277 isl_token_free(tok2);
1278 isl_token_free(tok);
1279 return &compound_bin_ops[i].op;
1282 if (read_tok2)
1283 isl_stream_push_token(s, tok2);
1284 isl_stream_push_token(s, tok);
1286 return NULL;
1289 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1291 int i;
1292 struct isl_token *tok;
1294 tok = isl_stream_next_token(s);
1295 if (!tok)
1296 return NULL;
1298 for (i = 0; ; ++i) {
1299 if (!named_un_ops[i].name)
1300 break;
1301 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1302 continue;
1304 isl_token_free(tok);
1305 return &named_un_ops[i].op;
1308 isl_stream_push_token(s, tok);
1310 return NULL;
1313 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1314 struct isl_obj arg)
1316 int i;
1318 for (i = 0; ; ++i) {
1319 if (!named_un_ops[i].name)
1320 break;
1321 if (named_un_ops[i].op.op != like->op)
1322 continue;
1323 if (!is_subtype(arg, named_un_ops[i].op.arg))
1324 continue;
1326 return &named_un_ops[i].op;
1329 return NULL;
1332 static int is_assign(struct isl_stream *s)
1334 struct isl_token *tok;
1335 struct isl_token *tok2;
1336 int assign;
1338 tok = isl_stream_next_token(s);
1339 if (!tok)
1340 return 0;
1341 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1342 isl_stream_push_token(s, tok);
1343 return 0;
1346 tok2 = isl_stream_next_token(s);
1347 if (!tok2) {
1348 isl_stream_push_token(s, tok);
1349 return 0;
1351 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1352 isl_stream_push_token(s, tok2);
1353 isl_stream_push_token(s, tok);
1355 return assign;
1358 static struct isl_obj read_obj(struct isl_stream *s,
1359 struct isl_hash_table *table);
1360 static struct isl_obj read_expr(struct isl_stream *s,
1361 struct isl_hash_table *table);
1363 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1364 struct isl_hash_table *table, struct isc_un_op *op)
1366 isl_ctx *ctx;
1367 struct isl_obj obj = { isl_obj_none, NULL };
1369 obj = read_obj(s, table);
1370 if (!obj.v)
1371 goto error;
1373 op = find_matching_un_op(op, obj);
1375 ctx = isl_stream_get_ctx(s);
1376 if (!op)
1377 isl_die(ctx, isl_error_invalid,
1378 "no such unary operator defined on given operand",
1379 goto error);
1381 obj = convert(ctx, obj, op->arg);
1382 obj.v = op->fn(obj.v);
1383 obj.type = op->res;
1385 return obj;
1386 error:
1387 free_obj(obj);
1388 obj.type = isl_obj_none;
1389 obj.v = NULL;
1390 return obj;
1393 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1395 struct isl_list *list;
1396 int exact;
1398 if (obj.type != isl_obj_union_map)
1399 obj = convert(ctx, obj, isl_obj_union_map);
1400 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1401 list = isl_list_alloc(ctx, 2);
1402 if (!list)
1403 goto error;
1405 list->obj[0].type = isl_obj_union_map;
1406 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1407 list->obj[1].type = isl_obj_bool;
1408 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1409 obj.v = list;
1410 obj.type = isl_obj_list;
1411 if (exact < 0 || !list->obj[0].v)
1412 goto error;
1414 return obj;
1415 error:
1416 free_obj(obj);
1417 obj.type = isl_obj_none;
1418 obj.v = NULL;
1419 return obj;
1422 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1424 struct isl_list *list = obj.v;
1425 struct isl_token *tok;
1426 isl_ctx *ctx;
1427 isl_val *v;
1428 int i;
1430 tok = isl_stream_next_token(s);
1431 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1432 isl_stream_error(s, tok, "expecting index");
1433 if (tok)
1434 isl_stream_push_token(s, tok);
1435 goto error;
1437 ctx = isl_stream_get_ctx(s);
1438 v = isl_token_get_val(ctx, tok);
1439 i = isl_val_get_num_si(v);
1440 isl_val_free(v);
1441 isl_token_free(tok);
1442 isl_assert(ctx, i < list->n, goto error);
1443 if (isl_stream_eat(s, ']'))
1444 goto error;
1446 return obj_at(obj, i);
1447 error:
1448 free_obj(obj);
1449 obj.type = isl_obj_none;
1450 obj.v = NULL;
1451 return obj;
1454 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1455 struct isl_hash_table *table)
1457 isl_ctx *ctx;
1458 struct isl_obj obj;
1460 obj = read_expr(s, table);
1461 ctx = isl_stream_get_ctx(s);
1462 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1463 is_subtype(obj, isl_obj_union_map), goto error);
1465 if (obj.type == isl_obj_list) {
1466 struct isl_list *list = obj.v;
1467 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1468 obj = obj_at(obj, 0);
1470 if (obj.type == isl_obj_set)
1471 obj = convert(ctx, obj, isl_obj_union_set);
1472 else if (obj.type == isl_obj_map)
1473 obj = convert(ctx, obj, isl_obj_union_map);
1474 if (obj.type == isl_obj_union_set) {
1475 obj.v = isl_union_set_apply(obj.v, umap);
1476 } else
1477 obj.v = isl_union_map_apply_range(obj.v, umap);
1478 if (!obj.v)
1479 goto error2;
1481 if (isl_stream_eat(s, ')'))
1482 goto error2;
1484 return obj;
1485 error:
1486 isl_union_map_free(umap);
1487 error2:
1488 free_obj(obj);
1489 obj.type = isl_obj_none;
1490 obj.v = NULL;
1491 return obj;
1494 static struct isl_obj apply_fun_set(struct isl_obj obj,
1495 __isl_take isl_union_set *uset)
1497 if (obj.type == isl_obj_union_pw_qpolynomial) {
1498 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1499 } else {
1500 obj.type = isl_obj_list;
1501 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1503 return obj;
1506 static struct isl_obj apply_fun_map(struct isl_obj obj,
1507 __isl_take isl_union_map *umap)
1509 if (obj.type == isl_obj_union_pw_qpolynomial) {
1510 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1511 } else {
1512 obj.type = isl_obj_list;
1513 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1515 return obj;
1518 static struct isl_obj apply_fun(struct isl_stream *s,
1519 struct isl_obj obj, struct isl_hash_table *table)
1521 struct isl_obj arg;
1522 isl_ctx *ctx;
1524 arg = read_expr(s, table);
1525 ctx = isl_stream_get_ctx(s);
1526 if (!is_subtype(arg, isl_obj_union_map) &&
1527 !is_subtype(arg, isl_obj_union_set))
1528 isl_die(ctx, isl_error_invalid,
1529 "expecting set of map argument", goto error);
1531 if (arg.type == isl_obj_list) {
1532 struct isl_list *list = arg.v;
1533 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1534 arg = obj_at(arg, 0);
1536 if (arg.type == isl_obj_set)
1537 arg = convert(ctx, arg, isl_obj_union_set);
1538 else if (arg.type == isl_obj_map)
1539 arg = convert(ctx, arg, isl_obj_union_map);
1540 if (arg.type == isl_obj_union_set)
1541 obj = apply_fun_set(obj, arg.v);
1542 else
1543 obj = apply_fun_map(obj, arg.v);
1544 if (!obj.v)
1545 goto error2;
1547 if (isl_stream_eat(s, ')'))
1548 goto error2;
1550 return obj;
1551 error:
1552 free_obj(arg);
1553 error2:
1554 free_obj(obj);
1555 obj.type = isl_obj_none;
1556 obj.v = NULL;
1557 return obj;
1560 struct add_vertex_data {
1561 struct isl_list *list;
1562 int i;
1565 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1567 struct add_vertex_data *data = (struct add_vertex_data *)user;
1568 isl_multi_aff *ma;
1569 isl_set *dom;
1571 ma = isl_vertex_get_expr(vertex);
1572 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1574 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1575 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1576 data->i++;
1578 isl_vertex_free(vertex);
1580 return isl_stat_ok;
1583 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1585 isl_ctx *ctx;
1586 isl_basic_set *hull;
1587 isl_vertices *vertices = NULL;
1588 struct isl_list *list = NULL;
1589 isl_stat r;
1590 struct add_vertex_data *data = (struct add_vertex_data *)user;
1592 set = isl_set_remove_divs(set);
1593 hull = isl_set_convex_hull(set);
1594 vertices = isl_basic_set_compute_vertices(hull);
1595 isl_basic_set_free(hull);
1597 list = data->list;
1599 ctx = isl_vertices_get_ctx(vertices);
1600 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1601 if (!data->list)
1602 goto error;
1604 data->i = 0;
1605 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1607 data->list = isl_list_concat(list, data->list);
1609 isl_vertices_free(vertices);
1611 return r;
1612 error:
1613 data->list = list;
1614 isl_vertices_free(vertices);
1615 return isl_stat_error;
1618 static struct isl_obj vertices(struct isl_stream *s,
1619 struct isl_hash_table *table)
1621 isl_ctx *ctx;
1622 struct isl_obj obj;
1623 struct isl_list *list = NULL;
1624 isl_union_set *uset = NULL;
1625 struct add_vertex_data data = { NULL };
1627 obj = read_expr(s, table);
1628 ctx = isl_stream_get_ctx(s);
1629 obj = convert(ctx, obj, isl_obj_union_set);
1630 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1631 uset = obj.v;
1632 obj.v = NULL;
1634 list = isl_list_alloc(ctx, 0);
1635 if (!list)
1636 goto error;
1638 data.list = list;
1640 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1641 goto error;
1643 isl_union_set_free(uset);
1645 obj.type = isl_obj_list;
1646 obj.v = data.list;
1648 return obj;
1649 error:
1650 isl_union_set_free(uset);
1651 isl_list_free(data.list);
1652 free_obj(obj);
1653 obj.type = isl_obj_none;
1654 obj.v = NULL;
1655 return obj;
1658 static struct isl_obj type_of(struct isl_stream *s,
1659 struct isl_hash_table *table)
1661 isl_ctx *ctx;
1662 struct isl_obj obj;
1663 const char *type = "unknown";
1665 obj = read_expr(s, table);
1667 if (obj.type == isl_obj_map ||
1668 obj.type == isl_obj_union_map)
1669 type = "map";
1670 if (obj.type == isl_obj_set ||
1671 obj.type == isl_obj_union_set)
1672 type = "set";
1673 if (obj.type == isl_obj_pw_multi_aff)
1674 type = "piecewise multi-quasiaffine expression";
1675 if (obj.type == isl_obj_pw_qpolynomial ||
1676 obj.type == isl_obj_union_pw_qpolynomial)
1677 type = "piecewise quasipolynomial";
1678 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1679 obj.type == isl_obj_union_pw_qpolynomial_fold)
1680 type = "piecewise quasipolynomial fold";
1681 if (obj.type == isl_obj_list)
1682 type = "list";
1683 if (obj.type == isl_obj_bool)
1684 type = "boolean";
1685 if (obj.type == isl_obj_str)
1686 type = "string";
1687 if (obj.type == isl_obj_val)
1688 type = "value";
1689 if (obj.type == isl_obj_schedule)
1690 type = "schedule";
1692 free_obj(obj);
1693 obj.type = isl_obj_str;
1694 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1696 return obj;
1699 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1700 struct isl_hash_table *table)
1702 struct isl_obj obj;
1703 isl_ctx *ctx;
1705 obj = read_obj(s, table);
1706 ctx = isl_stream_get_ctx(s);
1707 obj = convert(ctx, obj, isl_obj_union_set);
1708 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1709 return obj.v;
1710 error:
1711 free_obj(obj);
1712 return NULL;
1715 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1716 struct isl_hash_table *table)
1718 struct isl_obj obj;
1719 isl_ctx *ctx;
1721 obj = read_obj(s, table);
1722 ctx = isl_stream_get_ctx(s);
1723 obj = convert(ctx, obj, isl_obj_union_map);
1724 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1725 return obj.v;
1726 error:
1727 free_obj(obj);
1728 return NULL;
1731 /* Read a schedule in the form of either a schedule (tree) or a union map
1732 * from "s" and store the schedule in "access".
1734 static __isl_give isl_union_access_info *access_info_set_schedule(
1735 __isl_take isl_union_access_info *access, struct isl_stream *s,
1736 struct isl_hash_table *table)
1738 struct isl_obj obj;
1739 isl_ctx *ctx;
1741 obj = read_obj(s, table);
1742 if (obj.type == isl_obj_schedule)
1743 return isl_union_access_info_set_schedule(access, obj.v);
1744 ctx = isl_stream_get_ctx(s);
1745 obj = convert(ctx, obj, isl_obj_union_map);
1747 return isl_union_access_info_set_schedule_map(access, obj.v);
1750 static struct isl_obj last_any(struct isl_stream *s,
1751 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1752 __isl_take isl_union_map *may_source)
1754 struct isl_obj obj = { isl_obj_none, NULL };
1755 isl_union_access_info *access;
1756 isl_union_flow *flow;
1757 isl_union_map *sink = NULL;
1758 isl_union_map *may_dep;
1760 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1761 goto error;
1763 sink = read_map(s, table);
1764 if (!sink)
1765 goto error;
1767 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1768 goto error;
1770 access = isl_union_access_info_from_sink(sink);
1771 access = isl_union_access_info_set_must_source(access, must_source);
1772 access = isl_union_access_info_set_may_source(access, may_source);
1773 access = access_info_set_schedule(access, s, table);
1774 flow = isl_union_access_info_compute_flow(access);
1775 may_dep = isl_union_flow_get_may_dependence(flow);
1776 isl_union_flow_free(flow);
1778 if (!may_dep)
1779 return obj;
1781 obj.type = isl_obj_union_map;
1782 obj.v = may_dep;
1784 return obj;
1785 error:
1786 isl_union_map_free(may_source);
1787 isl_union_map_free(must_source);
1788 isl_union_map_free(sink);
1789 free_obj(obj);
1790 obj.type = isl_obj_none;
1791 obj.v = NULL;
1792 return obj;
1795 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1797 struct isl_obj obj = { isl_obj_none, NULL };
1798 isl_union_access_info *access;
1799 isl_union_flow *flow;
1800 isl_union_map *may_source = NULL;
1801 isl_union_map *sink = NULL;
1802 isl_union_map *may_dep;
1804 may_source = read_map(s, table);
1805 if (!may_source)
1806 goto error;
1808 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1809 isl_union_map *must_source;
1810 must_source = read_map(s, table);
1811 if (!must_source)
1812 goto error;
1813 return last_any(s, table, must_source, may_source);
1816 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1817 goto error;
1819 sink = read_map(s, table);
1820 if (!sink)
1821 goto error;
1823 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1824 goto error;
1826 access = isl_union_access_info_from_sink(sink);
1827 access = isl_union_access_info_set_may_source(access, may_source);
1828 access = access_info_set_schedule(access, s, table);
1829 flow = isl_union_access_info_compute_flow(access);
1830 may_dep = isl_union_flow_get_may_dependence(flow);
1831 isl_union_flow_free(flow);
1833 if (!may_dep)
1834 return obj;
1836 obj.type = isl_obj_union_map;
1837 obj.v = may_dep;
1839 return obj;
1840 error:
1841 isl_union_map_free(may_source);
1842 isl_union_map_free(sink);
1843 free_obj(obj);
1844 obj.type = isl_obj_none;
1845 obj.v = NULL;
1846 return obj;
1849 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1851 struct isl_obj obj = { isl_obj_none, NULL };
1852 struct isl_list *list = NULL;
1853 isl_union_access_info *access;
1854 isl_union_flow *flow;
1855 isl_union_map *must_source = NULL;
1856 isl_union_map *sink = NULL;
1857 isl_union_map *must_dep;
1858 isl_union_map *must_no_source;
1860 must_source = read_map(s, table);
1861 if (!must_source)
1862 goto error;
1864 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1865 isl_union_map *may_source;
1866 may_source = read_map(s, table);
1867 if (!may_source)
1868 goto error;
1869 return last_any(s, table, must_source, may_source);
1872 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1873 if (!list)
1874 goto error;
1876 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1877 goto error;
1879 sink = read_map(s, table);
1880 if (!sink)
1881 goto error;
1883 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1884 goto error;
1886 access = isl_union_access_info_from_sink(sink);
1887 access = isl_union_access_info_set_must_source(access, must_source);
1888 access = access_info_set_schedule(access, s, table);
1889 flow = isl_union_access_info_compute_flow(access);
1890 must_dep = isl_union_flow_get_must_dependence(flow);
1891 must_no_source = isl_union_flow_get_must_no_source(flow);
1892 isl_union_flow_free(flow);
1894 list->obj[0].type = isl_obj_union_map;
1895 list->obj[0].v = must_dep;
1896 list->obj[1].type = isl_obj_union_map;
1897 list->obj[1].v = must_no_source;
1899 if (!must_dep || !must_no_source) {
1900 isl_list_free(list);
1901 return obj;
1904 obj.v = list;
1905 obj.type = isl_obj_list;
1907 return obj;
1908 error:
1909 isl_list_free(list);
1910 isl_union_map_free(must_source);
1911 isl_union_map_free(sink);
1912 free_obj(obj);
1913 obj.type = isl_obj_none;
1914 obj.v = NULL;
1915 return obj;
1918 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1919 struct isl_hash_table *table)
1921 isl_union_set *domain;
1922 isl_union_map *validity;
1923 isl_union_map *proximity;
1925 domain = read_set(s, table);
1926 if (!domain)
1927 return NULL;
1929 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1930 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1932 for (;;) {
1933 isl_union_map *umap;
1934 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1935 umap = read_map(s, table);
1936 validity = isl_union_map_union(validity, umap);
1937 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1938 umap = read_map(s, table);
1939 proximity = isl_union_map_union(proximity, umap);
1940 } else
1941 break;
1944 return isl_union_set_compute_schedule(domain, validity, proximity);
1947 static struct isl_obj schedule(struct isl_stream *s,
1948 struct isl_hash_table *table)
1950 struct isl_obj obj = { isl_obj_none, NULL };
1951 isl_schedule *schedule;
1953 schedule = get_schedule(s, table);
1955 obj.v = schedule;
1956 obj.type = isl_obj_schedule;
1958 return obj;
1961 /* Read a schedule for code generation in the form of either
1962 * a schedule tree or a union map.
1963 * If the input is a set rather than a map, then we construct
1964 * an identity union map schedule on the given set.
1966 static struct isl_obj get_codegen_schedule(struct isl_stream *s,
1967 struct isl_hash_table *table)
1969 struct isl_obj obj;
1970 isl_ctx *ctx;
1972 obj = read_obj(s, table);
1973 ctx = isl_stream_get_ctx(s);
1975 if (obj.type == isl_obj_schedule)
1976 return obj;
1977 if (is_subtype(obj, isl_obj_union_set)) {
1978 obj = convert(ctx, obj, isl_obj_union_set);
1979 obj.v = isl_union_set_identity(obj.v);
1980 obj.type = isl_obj_union_map;
1982 if (is_subtype(obj, isl_obj_union_map))
1983 return convert(ctx, obj, isl_obj_union_map);
1985 free_obj(obj);
1986 obj.v = NULL;
1987 obj.type = isl_obj_none;
1988 isl_die(ctx, isl_error_invalid, "expecting schedule, set or map",
1989 return obj);
1992 /* Generate an AST for the given schedule and options and return the AST.
1994 static __isl_give isl_ast_node *get_ast_from_union_map(
1995 __isl_take isl_union_map *schedule, __isl_take isl_union_map *options)
1997 isl_space *space;
1998 isl_set *context;
1999 isl_ast_build *build;
2000 isl_ast_node *tree;
2002 space = isl_union_map_get_space(schedule);
2003 context = isl_set_universe(isl_space_params(space));
2005 build = isl_ast_build_from_context(context);
2006 build = isl_ast_build_set_options(build, options);
2007 tree = isl_ast_build_ast_from_schedule(build, schedule);
2008 isl_ast_build_free(build);
2010 return tree;
2013 /* Generate an AST for the given schedule and return the AST.
2015 static __isl_give isl_ast_node *get_ast_from_schedule(
2016 __isl_take isl_schedule *schedule)
2018 isl_ast_build *build;
2019 isl_ast_node *tree;
2021 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
2022 tree = isl_ast_build_node_from_schedule(build, schedule);
2023 isl_ast_build_free(build);
2025 return tree;
2028 /* Print the AST "tree" on the printer "p".
2030 static __isl_give isl_printer *print_ast(__isl_take isl_printer *p,
2031 __isl_take isl_ast_node *tree)
2033 int format;
2035 format = isl_printer_get_output_format(p);
2036 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2037 p = isl_printer_print_ast_node(p, tree);
2038 p = isl_printer_set_output_format(p, format);
2040 isl_ast_node_free(tree);
2042 return p;
2045 /* Perform the codegen operation.
2046 * In particular, read a schedule, check if the user has specified any options
2047 * and then generate an AST from the schedule (and options) and print it.
2048 * In case the schedule is specified as a schedule tree, the AST generation
2049 * options are embedded in the schedule, so they are not read in separately.
2051 static __isl_give isl_printer *codegen(struct isl_stream *s,
2052 struct isl_hash_table *table, __isl_take isl_printer *p)
2054 struct isl_obj obj;
2055 isl_ast_node *tree;
2057 obj = get_codegen_schedule(s, table);
2058 if (!obj.v)
2059 return p;
2061 if (obj.type == isl_obj_schedule) {
2062 isl_schedule *schedule = obj.v;
2064 tree = get_ast_from_schedule(schedule);
2065 } else {
2066 isl_union_map *schedule = obj.v;
2067 isl_union_map *options;
2069 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
2070 options = read_map(s, table);
2071 else
2072 options = isl_union_map_empty(
2073 isl_union_map_get_space(schedule));
2075 tree = get_ast_from_union_map(schedule, options);
2078 p = print_ast(p, tree);
2080 isl_stream_eat(s, ';');
2082 return p;
2085 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2087 struct isl_token *tok;
2088 isl_ctx *ctx;
2089 isl_val *v;
2091 ctx = isl_stream_get_ctx(s);
2092 if (isl_stream_eat_if_available(s, '+'))
2093 return transitive_closure(ctx, obj);
2095 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2096 if (obj.type != isl_obj_union_map)
2097 obj = convert(ctx, obj, isl_obj_union_map);
2099 tok = isl_stream_next_token(s);
2100 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2101 isl_stream_error(s, tok, "expecting integer exponent");
2102 if (tok)
2103 isl_stream_push_token(s, tok);
2104 goto error;
2107 v = isl_token_get_val(ctx, tok);
2108 if (isl_val_is_zero(v)) {
2109 isl_stream_error(s, tok, "expecting non-zero exponent");
2110 isl_val_free(v);
2111 if (tok)
2112 isl_stream_push_token(s, tok);
2113 goto error;
2116 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2117 isl_token_free(tok);
2118 if (!obj.v)
2119 goto error;
2121 return obj;
2122 error:
2123 free_obj(obj);
2124 obj.type = isl_obj_none;
2125 obj.v = NULL;
2126 return obj;
2129 static struct isl_obj check_assert(struct isl_stream *s,
2130 struct isl_hash_table *table)
2132 struct isl_obj obj;
2133 isl_ctx *ctx;
2135 obj = read_expr(s, table);
2136 ctx = isl_stream_get_ctx(s);
2137 if (obj.type != isl_obj_bool)
2138 isl_die(ctx, isl_error_invalid,
2139 "expecting boolean expression", goto error);
2140 if (obj.v != &iscc_bool_true)
2141 isl_die(ctx, isl_error_unknown,
2142 "assertion failed", abort());
2143 error:
2144 free_obj(obj);
2145 obj.type = isl_obj_none;
2146 obj.v = NULL;
2147 return obj;
2150 static struct isl_obj read_from_file(struct isl_stream *s)
2152 isl_ctx *ctx;
2153 struct isl_obj obj;
2154 struct isl_token *tok;
2155 struct isl_stream *s_file;
2156 struct iscc_options *options;
2157 char *name;
2158 FILE *file;
2160 tok = isl_stream_next_token(s);
2161 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2162 isl_stream_error(s, tok, "expecting filename");
2163 isl_token_free(tok);
2164 goto error;
2167 ctx = isl_stream_get_ctx(s);
2168 options = isl_ctx_peek_iscc_options(ctx);
2169 if (!options || !options->io) {
2170 isl_token_free(tok);
2171 isl_die(ctx, isl_error_invalid,
2172 "read operation not allowed", goto error);
2175 name = isl_token_get_str(ctx, tok);
2176 isl_token_free(tok);
2177 file = fopen(name, "r");
2178 free(name);
2179 isl_assert(ctx, file, goto error);
2181 s_file = isl_stream_new_file(ctx, file);
2182 if (!s_file) {
2183 fclose(file);
2184 goto error;
2187 obj = isl_stream_read_obj(s_file);
2189 isl_stream_free(s_file);
2190 fclose(file);
2192 return obj;
2193 error:
2194 obj.type = isl_obj_none;
2195 obj.v = NULL;
2196 return obj;
2199 static struct isl_obj write_to_file(struct isl_stream *s,
2200 struct isl_hash_table *table)
2202 struct isl_obj obj;
2203 struct isl_token *tok;
2204 struct isl_stream *s_file;
2205 struct iscc_options *options;
2206 char *name;
2207 FILE *file;
2208 isl_ctx *ctx;
2209 isl_printer *p;
2211 tok = isl_stream_next_token(s);
2212 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2213 isl_stream_error(s, tok, "expecting filename");
2214 isl_token_free(tok);
2215 goto error;
2218 obj = read_expr(s, table);
2220 ctx = isl_stream_get_ctx(s);
2221 options = isl_ctx_peek_iscc_options(ctx);
2222 if (!options || !options->io) {
2223 isl_token_free(tok);
2224 isl_die(ctx, isl_error_invalid,
2225 "write operation not allowed", goto error);
2228 name = isl_token_get_str(ctx, tok);
2229 isl_token_free(tok);
2230 file = fopen(name, "w");
2231 free(name);
2232 if (!file)
2233 isl_die(ctx, isl_error_unknown,
2234 "could not open file for writing", goto error);
2236 p = isl_printer_to_file(ctx, file);
2237 p = isl_printer_set_output_format(p, options->format);
2238 p = obj.type->print(p, obj.v);
2239 p = isl_printer_end_line(p);
2240 isl_printer_free(p);
2242 fclose(file);
2243 error:
2244 free_obj(obj);
2245 obj.type = isl_obj_none;
2246 obj.v = NULL;
2247 return obj;
2250 static struct isl_obj read_string_if_available(struct isl_stream *s)
2252 struct isl_token *tok;
2253 struct isl_obj obj = { isl_obj_none, NULL };
2255 tok = isl_stream_next_token(s);
2256 if (!tok)
2257 return obj;
2258 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2259 isl_str *str;
2260 str = isl_str_alloc(isl_stream_get_ctx(s));
2261 if (!str)
2262 goto error;
2263 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2264 isl_token_free(tok);
2265 obj.v = str;
2266 obj.type = isl_obj_str;
2267 } else
2268 isl_stream_push_token(s, tok);
2269 return obj;
2270 error:
2271 isl_token_free(tok);
2272 return obj;
2275 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2277 struct isl_token *tok;
2278 struct isl_obj obj = { isl_obj_none, NULL };
2279 int type;
2281 tok = isl_stream_next_token(s);
2282 if (!tok)
2283 return obj;
2284 type = isl_token_get_type(tok);
2285 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2286 int is_true = type == ISL_TOKEN_TRUE;
2287 isl_token_free(tok);
2288 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2289 obj.type = isl_obj_bool;
2290 } else
2291 isl_stream_push_token(s, tok);
2292 return obj;
2295 static __isl_give char *read_ident(struct isl_stream *s)
2297 char *name;
2298 isl_val *v;
2299 struct isl_token *tok, *tok2;
2301 name = isl_stream_read_ident_if_available(s);
2302 if (name)
2303 return name;
2305 tok = isl_stream_next_token(s);
2306 if (!tok)
2307 return NULL;
2308 if (isl_token_get_type(tok) != '$') {
2309 isl_stream_push_token(s, tok);
2310 return NULL;
2312 tok2 = isl_stream_next_token(s);
2313 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2314 if (tok2)
2315 isl_stream_push_token(s, tok2);
2316 isl_stream_push_token(s, tok);
2317 return NULL;
2320 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2321 name = isl_val_to_str(v);
2322 isl_val_free(v);
2323 isl_token_free(tok);
2324 isl_token_free(tok2);
2326 return name;
2329 static struct isl_obj read_list(struct isl_stream *s,
2330 struct isl_hash_table *table, struct isl_obj obj)
2332 struct isl_list *list;
2334 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2335 if (!list)
2336 goto error;
2337 list->obj[0] = obj;
2338 list->obj[1] = read_obj(s, table);
2339 obj.v = list;
2340 obj.type = isl_obj_list;
2342 if (!list->obj[1].v)
2343 goto error;
2345 while (isl_stream_eat_if_available(s, ',')) {
2346 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2347 if (!obj.v)
2348 goto error;
2351 return obj;
2352 error:
2353 free_obj(obj);
2354 obj.type = isl_obj_none;
2355 obj.v = NULL;
2356 return obj;
2359 static struct isl_obj read_obj(struct isl_stream *s,
2360 struct isl_hash_table *table)
2362 isl_ctx *ctx;
2363 struct isl_obj obj = { isl_obj_none, NULL };
2364 char *name = NULL;
2365 struct isc_un_op *op = NULL;
2367 obj = read_string_if_available(s);
2368 if (obj.v)
2369 return obj;
2370 obj = read_bool_if_available(s);
2371 if (obj.v)
2372 return obj;
2373 ctx = isl_stream_get_ctx(s);
2374 if (isl_stream_eat_if_available(s, '(')) {
2375 if (isl_stream_next_token_is(s, ')')) {
2376 obj.type = isl_obj_list;
2377 obj.v = isl_list_alloc(ctx, 0);
2378 } else {
2379 obj = read_expr(s, table);
2380 if (obj.v && isl_stream_eat_if_available(s, ','))
2381 obj = read_list(s, table, obj);
2383 if (!obj.v || isl_stream_eat(s, ')'))
2384 goto error;
2385 } else {
2386 op = read_prefix_un_op_if_available(s);
2387 if (op)
2388 return read_un_op_expr(s, table, op);
2390 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2391 return check_assert(s, table);
2392 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2393 return read_from_file(s);
2394 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2395 return write_to_file(s, table);
2396 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2397 return vertices(s, table);
2398 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2399 return any(s, table);
2400 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2401 return last(s, table);
2402 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2403 return schedule(s, table);
2404 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2405 return type_of(s, table);
2407 name = read_ident(s);
2408 if (name)
2409 obj = stored_obj(ctx, table, name);
2410 else
2411 obj = isl_stream_read_obj(s);
2412 if (!obj.v)
2413 goto error;
2416 if (isl_stream_eat_if_available(s, '^'))
2417 obj = power(s, obj);
2418 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2419 obj = obj_at_index(s, obj);
2420 else if (is_subtype(obj, isl_obj_union_map) &&
2421 isl_stream_eat_if_available(s, '(')) {
2422 obj = convert(ctx, obj, isl_obj_union_map);
2423 obj = apply(s, obj.v, table);
2424 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2425 isl_stream_eat_if_available(s, '(')) {
2426 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2427 obj = apply_fun(s, obj, table);
2428 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2429 isl_stream_eat_if_available(s, '(')) {
2430 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2431 obj = apply_fun(s, obj, table);
2434 return obj;
2435 error:
2436 free_obj(obj);
2437 obj.type = isl_obj_none;
2438 obj.v = NULL;
2439 return obj;
2442 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2443 struct isl_obj lhs, struct isl_obj rhs)
2445 int i;
2447 for (i = 0; ; ++i) {
2448 if (!bin_ops[i].op)
2449 break;
2450 if (bin_ops[i].op != like->op)
2451 continue;
2452 if (!is_subtype(lhs, bin_ops[i].lhs))
2453 continue;
2454 if (!is_subtype(rhs, bin_ops[i].rhs))
2455 continue;
2457 return &bin_ops[i];
2460 for (i = 0; ; ++i) {
2461 if (!named_bin_ops[i].name)
2462 break;
2463 if (named_bin_ops[i].op.op != like->op)
2464 continue;
2465 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2466 continue;
2467 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2468 continue;
2470 return &named_bin_ops[i].op;
2473 for (i = 0; ; ++i) {
2474 if (!compound_bin_ops[i].full)
2475 break;
2476 if (compound_bin_ops[i].op.op != like->op)
2477 continue;
2478 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
2479 continue;
2480 if (!is_subtype(rhs, compound_bin_ops[i].op.rhs))
2481 continue;
2483 return &compound_bin_ops[i].op;
2486 return NULL;
2489 static int next_is_neg_int(struct isl_stream *s)
2491 struct isl_token *tok;
2492 int ret;
2494 tok = isl_stream_next_token(s);
2495 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2496 isl_val *v;
2497 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2498 ret = isl_val_is_neg(v);
2499 isl_val_free(v);
2500 } else
2501 ret = 0;
2502 isl_stream_push_token(s, tok);
2504 return ret;
2507 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2508 struct isl_obj lhs, struct isl_obj rhs)
2510 struct isl_obj obj;
2512 lhs = convert(ctx, lhs, op->lhs);
2513 rhs = convert(ctx, rhs, op->rhs);
2514 if (op->res != isl_obj_bool)
2515 obj.v = op->o.fn(lhs.v, rhs.v);
2516 else {
2517 int res = op->o.test(lhs.v, rhs.v);
2518 free_obj(lhs);
2519 free_obj(rhs);
2520 obj.v = iscc_bool_from_int(res);
2522 obj.type = op->res;
2524 return obj;
2527 static struct isl_obj read_expr(struct isl_stream *s,
2528 struct isl_hash_table *table)
2530 isl_ctx *ctx;
2531 struct isl_obj obj = { isl_obj_none, NULL };
2532 struct isl_obj right_obj = { isl_obj_none, NULL };
2534 obj = read_obj(s, table);
2535 ctx = isl_stream_get_ctx(s);
2536 for (; obj.v;) {
2537 struct isc_bin_op *op = NULL;
2539 op = read_bin_op_if_available(s, obj);
2540 if (!op)
2541 break;
2543 right_obj = read_obj(s, table);
2545 op = find_matching_bin_op(op, obj, right_obj);
2547 if (!op)
2548 isl_die(ctx, isl_error_invalid,
2549 "no such binary operator defined on given operands",
2550 goto error);
2552 obj = call_bin_op(ctx, op, obj, right_obj);
2555 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2556 right_obj = read_obj(s, table);
2557 obj.v = isl_val_add(obj.v, right_obj.v);
2560 return obj;
2561 error:
2562 free_obj(right_obj);
2563 free_obj(obj);
2564 obj.type = isl_obj_none;
2565 obj.v = NULL;
2566 return obj;
2569 static __isl_give isl_printer *source_file(struct isl_stream *s,
2570 struct isl_hash_table *table, __isl_take isl_printer *p);
2572 /* Print "obj" to the printer "p".
2573 * If the object is a schedule, then print it in block format.
2575 static __isl_give isl_printer *print_obj(__isl_take isl_printer *p,
2576 struct isl_obj obj)
2578 if (obj.type != isl_obj_schedule)
2579 return obj.type->print(p, obj.v);
2581 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
2582 p = obj.type->print(p, obj.v);
2583 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2585 return p;
2588 static __isl_give isl_printer *read_line(struct isl_stream *s,
2589 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2591 isl_ctx *ctx;
2592 struct isl_obj obj = { isl_obj_none, NULL };
2593 char *lhs = NULL;
2594 int assign = 0;
2595 int only_print = 0;
2596 struct isc_bin_op *op = NULL;
2597 char buf[30];
2599 if (!p)
2600 return NULL;
2601 if (isl_stream_is_empty(s))
2602 return p;
2604 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2605 return source_file(s, table, p);
2606 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2607 return codegen(s, table, p);
2609 assign = is_assign(s);
2610 if (assign) {
2611 lhs = isl_stream_read_ident_if_available(s);
2612 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2613 goto error;
2614 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2615 only_print = 1;
2616 else if (!tty)
2617 only_print = 1;
2619 obj = read_expr(s, table);
2620 ctx = isl_stream_get_ctx(s);
2621 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2622 fprintf(stderr, "Interrupted\n");
2623 isl_ctx_reset_error(ctx);
2625 if (isl_stream_eat(s, ';'))
2626 goto error;
2628 if (only_print) {
2629 if (obj.type != isl_obj_none && obj.v != NULL) {
2630 p = print_obj(p, obj);
2631 p = isl_printer_end_line(p);
2633 free_obj(obj);
2634 return p;
2636 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2637 static int count = 0;
2638 snprintf(buf, sizeof(buf), "$%d", count++);
2639 lhs = strdup(buf + 1);
2641 p = isl_printer_print_str(p, buf);
2642 p = isl_printer_print_str(p, " := ");
2643 p = obj.type->print(p, obj.v);
2644 p = isl_printer_end_line(p);
2646 if (lhs && do_assign(ctx, table, lhs, obj))
2647 return p;
2649 return p;
2650 error:
2651 isl_stream_flush_tokens(s);
2652 isl_stream_skip_line(s);
2653 free(lhs);
2654 free_obj(obj);
2655 return p;
2658 static isl_stat free_cb(void **entry, void *user)
2660 struct isl_named_obj *named = *entry;
2662 free_obj(named->obj);
2663 free(named->name);
2664 free(named);
2666 return isl_stat_ok;
2669 static void register_named_ops(struct isl_stream *s)
2671 int i;
2673 for (i = 0; i < ISCC_N_OP; ++i) {
2674 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2675 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2678 for (i = 0; ; ++i) {
2679 if (!named_un_ops[i].name)
2680 break;
2681 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2682 named_un_ops[i].name);
2683 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2686 for (i = 0; ; ++i) {
2687 if (!named_bin_ops[i].name)
2688 break;
2689 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2690 named_bin_ops[i].name);
2691 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2694 for (i = 0; ; ++i) {
2695 if (!compound_bin_ops[i].full)
2696 break;
2697 compound_bin_ops[i].op.op = isl_stream_register_keyword(s,
2698 compound_bin_ops[i].full);
2699 assert(compound_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2703 static __isl_give isl_printer *source_file(struct isl_stream *s,
2704 struct isl_hash_table *table, __isl_take isl_printer *p)
2706 isl_ctx *ctx;
2707 struct isl_token *tok;
2708 struct isl_stream *s_file;
2709 struct iscc_options *options;
2710 char *name;
2711 FILE *file;
2713 tok = isl_stream_next_token(s);
2714 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2715 isl_stream_error(s, tok, "expecting filename");
2716 isl_token_free(tok);
2717 return p;
2720 isl_stream_eat(s, ';');
2722 ctx = isl_stream_get_ctx(s);
2723 options = isl_ctx_peek_iscc_options(ctx);
2724 if (!options || !options->io) {
2725 isl_token_free(tok);
2726 isl_die(ctx, isl_error_invalid,
2727 "source operation not allowed", return p);
2730 name = isl_token_get_str(ctx, tok);
2731 isl_token_free(tok);
2732 file = fopen(name, "r");
2733 free(name);
2734 isl_assert(ctx, file, return p);
2736 s_file = isl_stream_new_file(ctx, file);
2737 if (!s_file) {
2738 fclose(file);
2739 return p;
2742 register_named_ops(s_file);
2744 while (!isl_stream_is_empty(s_file))
2745 p = read_line(s_file, table, p, 0);
2747 isl_stream_free(s_file);
2748 fclose(file);
2750 return p;
2753 int main(int argc, char **argv)
2755 struct isl_ctx *ctx;
2756 struct isl_stream *s;
2757 struct isl_hash_table *table;
2758 struct iscc_options *options;
2759 isl_printer *p;
2760 int tty = isatty(0);
2762 options = iscc_options_new_with_defaults();
2763 assert(options);
2765 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2766 pet_options_set_autodetect(ctx, 1);
2767 pet_options_set_encapsulate_dynamic_control(ctx, 1);
2768 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2769 s = isl_stream_new_file(ctx, stdin);
2770 assert(s);
2771 table = isl_hash_table_alloc(ctx, 10);
2772 assert(table);
2773 p = isl_printer_to_file(ctx, stdout);
2774 p = isl_printer_set_output_format(p, options->format);
2775 assert(p);
2777 register_named_ops(s);
2779 install_signal_handler(ctx);
2781 while (p && !isl_stream_is_empty(s)) {
2782 isl_ctx_resume(ctx);
2783 p = read_line(s, table, p, tty);
2786 remove_signal_handler(ctx);
2788 isl_printer_free(p);
2789 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2790 isl_hash_table_free(ctx, table);
2791 isl_stream_free(s);
2792 isl_ctx_free(ctx);
2794 return 0;