polysign_isl.c: directly include required headers
[barvinok.git] / iscc.c
blob3a83d5d89a7acc88187ff295ed3a7e0d6d3b0144
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/ctx.h>
7 #include <isl/val.h>
8 #include <isl/space.h>
9 #include <isl/aff.h>
10 #include <isl/obj.h>
11 #include <isl/stream.h>
12 #include <isl/set.h>
13 #include <isl/map.h>
14 #include <isl/union_set.h>
15 #include <isl/union_map.h>
16 #include <isl/vertices.h>
17 #include <isl/flow.h>
18 #include <isl/schedule.h>
19 #include <isl/ast_build.h>
20 #include <isl/printer.h>
21 #include <isl_obj_list.h>
22 #include <isl_obj_str.h>
23 #include <barvinok/isl.h>
24 #include <barvinok/options.h>
25 #include "lattice_width.h"
27 #include "config.h"
29 #ifdef HAVE_SIGACTION
30 #include <signal.h>
32 static isl_ctx *main_ctx;
34 static void handler(int signum)
36 if (isl_ctx_aborted(main_ctx))
37 exit(EXIT_FAILURE);
38 isl_ctx_abort(main_ctx);
41 static struct sigaction sa_old;
43 static void install_signal_handler(isl_ctx *ctx)
45 struct sigaction sa;
47 main_ctx = ctx;
49 memset(&sa, 0, sizeof(struct sigaction));
50 sa.sa_handler = &handler;
51 sa.sa_flags = SA_RESTART;
52 sigaction(SIGINT, &sa, &sa_old);
55 static void remove_signal_handler(isl_ctx *ctx)
57 sigaction(SIGINT, &sa_old, NULL);
60 #else
62 static void install_signal_handler(isl_ctx *ctx)
66 static void remove_signal_handler(isl_ctx *ctx)
70 #endif
72 #ifdef HAVE_PET
73 #include <pet.h>
74 #else
75 struct pet_options;
76 int pet_options_set_autodetect(isl_ctx *ctx, int val)
78 return -1;
80 int pet_options_set_encapsulate_dynamic_control(isl_ctx *ctx, int val)
82 return -1;
84 #endif
86 static int iscc_bool_false = 0;
87 static int iscc_bool_true = 1;
88 static int iscc_bool_error = -1;
90 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
91 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
92 ISCC_SCHEDULE,
93 ISCC_MINIMIZING, ISCC_RESPECTING,
94 ISCC_CODEGEN, ISCC_USING,
95 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
96 ISCC_N_OP };
97 static const char *op_name[ISCC_N_OP] = {
98 [ISCC_ASSERT] = "assert",
99 [ISCC_READ] = "read",
100 [ISCC_WRITE] = "write",
101 [ISCC_PRINT] = "print",
102 [ISCC_SOURCE] = "source",
103 [ISCC_VERTICES] = "vertices",
104 [ISCC_LAST] = "last",
105 [ISCC_ANY] = "any",
106 [ISCC_BEFORE] = "before",
107 [ISCC_UNDER] = "under",
108 [ISCC_SCHEDULE] = "schedule",
109 [ISCC_MINIMIZING] = "minimizing",
110 [ISCC_RESPECTING] = "respecting",
111 [ISCC_CODEGEN] = "codegen",
112 [ISCC_USING] = "using",
113 [ISCC_TYPEOF] = "typeof"
115 static enum isl_token_type iscc_op[ISCC_N_OP];
117 struct isl_arg_choice iscc_format[] = {
118 {"isl", ISL_FORMAT_ISL},
119 {"omega", ISL_FORMAT_OMEGA},
120 {"polylib", ISL_FORMAT_POLYLIB},
121 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
122 {"latex", ISL_FORMAT_LATEX},
123 {"C", ISL_FORMAT_C},
127 struct iscc_options {
128 struct barvinok_options *barvinok;
129 struct pet_options *pet;
130 unsigned format;
131 int io;
134 ISL_ARGS_START(struct iscc_options, iscc_options_args)
135 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
136 "barvinok options")
137 #ifdef HAVE_PET
138 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
139 #endif
140 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
141 iscc_format, ISL_FORMAT_ISL, "output format")
142 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
143 "allow read and write operations")
144 ISL_ARGS_END
146 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
147 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
149 static void *isl_obj_bool_copy(void *v)
151 return v;
154 static void isl_obj_bool_free(void *v)
158 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
159 void *v)
161 if (v == &iscc_bool_true)
162 return isl_printer_print_str(p, "True");
163 else if (v == &iscc_bool_false)
164 return isl_printer_print_str(p, "False");
165 else
166 return isl_printer_print_str(p, "Error");
169 static void *isl_obj_bool_add(void *v1, void *v2)
171 return v1;
174 struct isl_obj_vtable isl_obj_bool_vtable = {
175 isl_obj_bool_copy,
176 isl_obj_bool_add,
177 isl_obj_bool_print,
178 isl_obj_bool_free
180 #define isl_obj_bool (&isl_obj_bool_vtable)
182 int *iscc_bool_from_int(int res)
184 return res < 0 ? &iscc_bool_error :
185 res ? &iscc_bool_true : &iscc_bool_false;
188 /* Conjunction of "b1" and "b2".
189 * The result is returned as an integer because it is post-processed by
190 * iscc_bool_from_int.
192 static int isl_bool_and(isl_bool *b1, __isl_take isl_bool *b2)
194 if (b1 == &iscc_bool_error || b2 == &iscc_bool_error)
195 return -1;
196 return b1 == &iscc_bool_true && b2 == &iscc_bool_true;
199 /* Disjunction of "b1" and "b2".
200 * The result is returned as an integer because it is post-processed by
201 * iscc_bool_from_int.
203 static int isl_bool_or(isl_bool *b1, __isl_take isl_bool *b2)
205 if (b1 == &iscc_bool_error || b2 == &iscc_bool_error)
206 return -1;
207 return b1 == &iscc_bool_true || b2 == &iscc_bool_true;
210 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
211 __isl_take isl_union_map *map2)
213 return isl_union_map_is_subset(map2, map1);
215 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
216 __isl_take isl_union_set *set2)
218 return isl_union_set_is_subset(set2, set1);
221 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
222 __isl_take isl_union_map *map2)
224 return isl_union_map_is_strict_subset(map2, map1);
226 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
227 __isl_take isl_union_set *set2)
229 return isl_union_set_is_strict_subset(set2, set1);
232 extern struct isl_obj_vtable isl_obj_list_vtable;
233 #define isl_obj_list (&isl_obj_list_vtable)
235 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
236 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
237 struct isc_bin_op {
238 enum isl_token_type op;
239 isl_obj_type lhs;
240 isl_obj_type rhs;
241 isl_obj_type res;
242 union {
243 isc_bin_op_fn fn;
244 isc_bin_test_fn test;
245 } o;
247 struct isc_named_bin_op {
248 char *name;
249 struct isc_bin_op op;
251 /* Compound binary operator.
252 * "full" is only used to generate a unique token number for op.op
253 * in register_named_ops.
254 * "op1" is the first part of the compound operator.
255 * "op2" is the second part of the compound operator.
257 struct iscc_compound_bin_op {
258 char *full;
259 enum isl_token_type op1;
260 enum isl_token_type op2;
261 struct isc_bin_op op;
264 struct iscc_at {
265 isl_union_pw_qpolynomial *upwqp;
266 isl_union_pw_qpolynomial *res;
269 static isl_stat eval_at(__isl_take isl_point *pnt, void *user)
271 struct iscc_at *at = (struct iscc_at *) user;
272 isl_val *v;
273 isl_qpolynomial *qp;
274 isl_set *set;
276 set = isl_set_from_point(isl_point_copy(pnt));
277 v = isl_union_pw_qpolynomial_eval(
278 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
279 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
281 at->res = isl_union_pw_qpolynomial_add(at->res,
282 isl_union_pw_qpolynomial_from_pw_qpolynomial(
283 isl_pw_qpolynomial_alloc(set, qp)));
285 return isl_stat_ok;
288 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
289 __isl_take isl_union_pw_qpolynomial *upwqp,
290 __isl_take isl_union_set *uset)
292 struct iscc_at at;
294 at.upwqp = upwqp;
295 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
297 isl_union_set_foreach_point(uset, eval_at, &at);
299 isl_union_pw_qpolynomial_free(upwqp);
300 isl_union_set_free(uset);
302 return at.res;
305 struct iscc_fold_at {
306 isl_union_pw_qpolynomial_fold *upwf;
307 isl_union_pw_qpolynomial *res;
310 static isl_stat eval_fold_at(__isl_take isl_point *pnt, void *user)
312 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
313 isl_val *v;
314 isl_qpolynomial *qp;
315 isl_set *set;
317 set = isl_set_from_point(isl_point_copy(pnt));
318 v = isl_union_pw_qpolynomial_fold_eval(
319 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
320 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
322 at->res = isl_union_pw_qpolynomial_add(at->res,
323 isl_union_pw_qpolynomial_from_pw_qpolynomial(
324 isl_pw_qpolynomial_alloc(set, qp)));
326 return isl_stat_ok;
329 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
330 __isl_take isl_union_pw_qpolynomial_fold *upwf,
331 __isl_take isl_union_set *uset)
333 struct iscc_fold_at at;
335 at.upwf = upwf;
336 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
338 isl_union_set_foreach_point(uset, eval_fold_at, &at);
340 isl_union_pw_qpolynomial_fold_free(upwf);
341 isl_union_set_free(uset);
343 return at.res;
346 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
347 __isl_take isl_union_pw_qpolynomial *upwqp,
348 __isl_take isl_union_pw_qpolynomial_fold *upwf)
350 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
351 upwqp);
354 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
355 __isl_take isl_union_map *umap,
356 __isl_take isl_union_pw_qpolynomial_fold *upwf)
358 isl_ctx *ctx;
359 struct isl_list *list;
360 int tight;
362 ctx = isl_union_map_get_ctx(umap);
363 list = isl_list_alloc(ctx, 2);
364 if (!list)
365 goto error2;
367 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
368 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
369 upwf, &tight);
370 list->obj[1].type = isl_obj_bool;
371 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
372 if (tight < 0 || !list->obj[0].v)
373 goto error;
375 return list;
376 error2:
377 isl_union_map_free(umap);
378 isl_union_pw_qpolynomial_fold_free(upwf);
379 error:
380 isl_list_free(list);
381 return NULL;
384 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
385 __isl_take isl_union_set *uset,
386 __isl_take isl_union_pw_qpolynomial_fold *upwf)
388 isl_ctx *ctx;
389 struct isl_list *list;
390 int tight;
392 ctx = isl_union_set_get_ctx(uset);
393 list = isl_list_alloc(ctx, 2);
394 if (!list)
395 goto error2;
397 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
398 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
399 upwf, &tight);
400 list->obj[1].type = isl_obj_bool;
401 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
402 if (tight < 0 || !list->obj[0].v)
403 goto error;
405 return list;
406 error2:
407 isl_union_set_free(uset);
408 isl_union_pw_qpolynomial_fold_free(upwf);
409 error:
410 isl_list_free(list);
411 return NULL;
414 static __isl_give isl_union_pw_qpolynomial *isl_val_mul_union_pw_qpolynomial(
415 __isl_take isl_val *v, __isl_take isl_union_pw_qpolynomial *upwqp)
417 return isl_union_pw_qpolynomial_scale_val(upwqp, v);
420 static __isl_give isl_union_pw_qpolynomial_fold *
421 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val *v,
422 __isl_take isl_union_pw_qpolynomial_fold *upwf)
424 return isl_union_pw_qpolynomial_fold_scale_val(upwf, v);
427 /* Are the two strings "str1" and "str2" equal to each other?
429 static int str_eq(__isl_keep isl_str *str1, __isl_keep isl_str *str2)
431 if (!str1 || !str2)
432 return -1;
434 return !strcmp(str1->s, str2->s);
437 struct isc_bin_op bin_ops[] = {
438 { '+', isl_obj_bool, isl_obj_bool, isl_obj_bool,
439 (isc_bin_op_fn) &isl_bool_or },
440 { '*', isl_obj_bool, isl_obj_bool, isl_obj_bool,
441 (isc_bin_op_fn) &isl_bool_and },
442 { '+', isl_obj_val, isl_obj_val, isl_obj_val,
443 (isc_bin_op_fn) &isl_val_add },
444 { '-', isl_obj_val, isl_obj_val, isl_obj_val,
445 (isc_bin_op_fn) &isl_val_sub },
446 { '*', isl_obj_val, isl_obj_val, isl_obj_val,
447 (isc_bin_op_fn) &isl_val_mul },
448 { '+', isl_obj_pw_multi_aff, isl_obj_pw_multi_aff,
449 isl_obj_pw_multi_aff,
450 (isc_bin_op_fn) &isl_pw_multi_aff_add },
451 { '+', isl_obj_union_set, isl_obj_union_set,
452 isl_obj_union_set,
453 (isc_bin_op_fn) &isl_union_set_union },
454 { '+', isl_obj_union_map, isl_obj_union_map,
455 isl_obj_union_map,
456 (isc_bin_op_fn) &isl_union_map_union },
457 { '-', isl_obj_union_set, isl_obj_union_set,
458 isl_obj_union_set,
459 (isc_bin_op_fn) &isl_union_set_subtract },
460 { '-', isl_obj_union_map, isl_obj_union_map,
461 isl_obj_union_map,
462 (isc_bin_op_fn) &isl_union_map_subtract },
463 { '-', isl_obj_union_map, isl_obj_union_set,
464 isl_obj_union_map,
465 (isc_bin_op_fn) &isl_union_map_subtract_domain },
466 { '*', isl_obj_union_set, isl_obj_union_set,
467 isl_obj_union_set,
468 (isc_bin_op_fn) &isl_union_set_intersect },
469 { '*', isl_obj_union_map, isl_obj_union_map,
470 isl_obj_union_map,
471 (isc_bin_op_fn) &isl_union_map_intersect },
472 { '*', isl_obj_union_map, isl_obj_union_set,
473 isl_obj_union_map,
474 (isc_bin_op_fn) &isl_union_map_intersect_domain },
475 { '.', isl_obj_union_map, isl_obj_union_map,
476 isl_obj_union_map,
477 (isc_bin_op_fn) &isl_union_map_apply_range },
478 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
479 isl_obj_union_pw_qpolynomial,
480 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
481 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
482 isl_obj_list,
483 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
484 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
485 isl_obj_union_map,
486 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
487 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
488 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
489 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
490 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
491 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
492 isl_obj_bool,
493 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
494 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
495 isl_obj_bool,
496 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
497 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
498 isl_obj_bool,
499 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
500 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
501 isl_obj_bool,
502 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
503 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
504 isl_obj_bool,
505 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
506 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
507 isl_obj_bool,
508 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
509 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
510 isl_obj_bool,
511 { .test =
512 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
513 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
514 isl_obj_bool,
515 { .test =
516 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
517 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
518 isl_obj_union_map,
519 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
520 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
521 isl_obj_union_map,
522 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
523 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
524 isl_obj_union_map,
525 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
526 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
527 isl_obj_union_map,
528 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
529 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
530 isl_obj_union_map,
531 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
532 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
533 isl_obj_union_map,
534 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
535 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
536 isl_obj_union_map,
537 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
538 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
539 isl_obj_union_map,
540 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
541 { '.', isl_obj_union_pw_qpolynomial_fold,
542 isl_obj_union_pw_qpolynomial_fold,
543 isl_obj_union_pw_qpolynomial_fold,
544 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
545 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
546 isl_obj_union_pw_qpolynomial,
547 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
548 { '+', isl_obj_union_pw_qpolynomial,
549 isl_obj_union_pw_qpolynomial_fold,
550 isl_obj_union_pw_qpolynomial_fold,
551 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
552 { '+', isl_obj_union_pw_qpolynomial_fold,
553 isl_obj_union_pw_qpolynomial,
554 isl_obj_union_pw_qpolynomial_fold,
555 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
556 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
557 isl_obj_union_pw_qpolynomial,
558 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
559 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
560 isl_obj_union_pw_qpolynomial,
561 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
562 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
563 isl_obj_union_pw_qpolynomial,
564 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
565 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
566 isl_obj_union_pw_qpolynomial_fold,
567 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
568 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
569 isl_obj_union_pw_qpolynomial_fold,
570 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
571 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
572 isl_obj_union_pw_qpolynomial,
573 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
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_intersect_domain },
577 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
578 isl_obj_union_pw_qpolynomial_fold,
579 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
580 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
581 isl_obj_union_pw_qpolynomial,
582 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
583 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
584 isl_obj_union_pw_qpolynomial,
585 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
586 { '%', isl_obj_union_set, isl_obj_union_set,
587 isl_obj_union_set,
588 (isc_bin_op_fn) &isl_union_set_gist },
589 { '%', isl_obj_union_map, isl_obj_union_map,
590 isl_obj_union_map,
591 (isc_bin_op_fn) &isl_union_map_gist },
592 { '%', isl_obj_union_map, isl_obj_union_set,
593 isl_obj_union_map,
594 (isc_bin_op_fn) &isl_union_map_gist_domain },
595 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
596 isl_obj_union_pw_qpolynomial,
597 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
598 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
599 isl_obj_union_pw_qpolynomial_fold,
600 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
601 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
602 isl_obj_union_pw_qpolynomial, isl_obj_bool,
603 { .test = (isc_bin_test_fn)
604 &isl_union_pw_qpolynomial_plain_is_equal } },
605 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
606 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
607 { .test = (isc_bin_test_fn)
608 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
609 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
610 (isc_bin_op_fn) &isl_str_concat },
611 { '=', isl_obj_str, isl_obj_str, isl_obj_bool,
612 { .test = (isc_bin_test_fn) &str_eq } },
616 struct iscc_compound_bin_op compound_bin_ops[] = {
617 { "->*", ISL_TOKEN_TO, '*',
618 { -1, isl_obj_union_map, isl_obj_union_set,
619 isl_obj_union_map,
620 (isc_bin_op_fn) &isl_union_map_intersect_range } },
621 { "->-", ISL_TOKEN_TO, '-',
622 { -1, isl_obj_union_map, isl_obj_union_set,
623 isl_obj_union_map,
624 (isc_bin_op_fn) &isl_union_map_subtract_range } },
628 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
629 __isl_take isl_union_map *umap2)
631 return isl_union_map_apply_range(umap2, umap1);
634 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
635 __isl_take isl_union_pw_qpolynomial *upwqp,
636 __isl_take isl_union_map *umap)
638 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
641 static __isl_give struct isl_list *qpolynomial_fold_after_map(
642 __isl_take isl_union_pw_qpolynomial_fold *upwf,
643 __isl_take isl_union_map *umap)
645 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
648 struct isc_named_bin_op named_bin_ops[] = {
649 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
650 isl_obj_union_map,
651 (isc_bin_op_fn) &map_after_map } },
652 { "after", { -1, isl_obj_union_pw_qpolynomial,
653 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
654 (isc_bin_op_fn) &qpolynomial_after_map } },
655 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
656 isl_obj_union_map, isl_obj_list,
657 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
658 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
659 isl_obj_union_map,
660 (isc_bin_op_fn) &isl_union_map_apply_range } },
661 { "before", { -1, isl_obj_union_map,
662 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
663 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
664 { "before", { -1, isl_obj_union_map,
665 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
666 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
667 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
668 isl_obj_union_set,
669 (isc_bin_op_fn) &isl_union_set_product } },
670 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
671 isl_obj_union_map,
672 (isc_bin_op_fn) &isl_union_map_product } },
673 NULL
676 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
678 return isl_set_from_basic_set(isl_union_set_sample(uset));
681 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
683 return isl_map_from_basic_map(isl_union_map_sample(umap));
686 static __isl_give struct isl_list *union_map_power(
687 __isl_take isl_union_map *umap)
689 isl_ctx *ctx;
690 struct isl_list *list;
691 int exact;
693 ctx = isl_union_map_get_ctx(umap);
694 list = isl_list_alloc(ctx, 2);
695 if (!list)
696 goto error2;
698 list->obj[0].type = isl_obj_union_map;
699 list->obj[0].v = isl_union_map_power(umap, &exact);
700 list->obj[1].type = isl_obj_bool;
701 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
702 if (exact < 0 || !list->obj[0].v)
703 goto error;
705 return list;
706 error2:
707 isl_union_map_free(umap);
708 error:
709 isl_list_free(list);
710 return NULL;
713 /* Compute a lower or upper bound on "upwqp" depending on "type" and
714 * return a list containing two elements, the bound and a boolean
715 * indicating whether the result is tight.
717 static __isl_give struct isl_list *union_pw_qpolynomial_bound(
718 __isl_take isl_union_pw_qpolynomial *upwqp, enum isl_fold type)
720 isl_ctx *ctx;
721 struct isl_list *list;
722 int tight;
724 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
725 list = isl_list_alloc(ctx, 2);
726 if (!list)
727 goto error2;
729 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
730 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp, type, &tight);
731 list->obj[1].type = isl_obj_bool;
732 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
733 if (tight < 0 || !list->obj[0].v)
734 goto error;
736 return list;
737 error2:
738 isl_union_pw_qpolynomial_free(upwqp);
739 error:
740 isl_list_free(list);
741 return NULL;
744 /* Compute a lower bound on "upwqp" and return a list containing
745 * two elements, the bound and a booleanindicating whether
746 * the result is tight.
748 static __isl_give struct isl_list *union_pw_qpolynomial_lower_bound(
749 __isl_take isl_union_pw_qpolynomial *upwqp)
751 return union_pw_qpolynomial_bound(upwqp, isl_fold_min);
754 /* Compute a upper bound on "upwqp" and return a list containing
755 * two elements, the bound and a booleanindicating whether
756 * the result is tight.
758 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
759 __isl_take isl_union_pw_qpolynomial *upwqp)
761 return union_pw_qpolynomial_bound(upwqp, isl_fold_max);
764 #ifdef HAVE_PET
765 /* Collect all statement instances, except those of kill statements.
767 static __isl_give isl_union_set *collect_non_kill_instances(
768 struct pet_scop *scop)
770 int i;
771 isl_set *domain_i;
772 isl_union_set *domain;
774 if (!scop)
775 return NULL;
777 domain = isl_union_set_empty(isl_set_get_space(scop->context));
779 for (i = 0; i < scop->n_stmt; ++i) {
780 struct pet_stmt *stmt = scop->stmts[i];
782 if (pet_stmt_is_kill(stmt))
783 continue;
784 domain_i = isl_set_copy(stmt->domain);
785 if (scop->stmts[i]->n_arg > 0)
786 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
787 domain = isl_union_set_add_set(domain, domain_i);
790 return domain;
793 static __isl_give isl_list *parse(__isl_take isl_str *str)
795 isl_ctx *ctx;
796 struct isl_list *list;
797 struct pet_scop *scop;
798 isl_schedule *sched;
799 isl_union_map *may_reads, *must_writes, *may_writes;
800 isl_union_set *domain;
801 struct iscc_options *options;
803 if (!str)
804 return NULL;
805 ctx = str->ctx;
807 options = isl_ctx_peek_iscc_options(ctx);
808 if (!options || !options->io) {
809 isl_str_free(str);
810 isl_die(ctx, isl_error_invalid,
811 "parse_file operation not allowed", return NULL);
814 list = isl_list_alloc(ctx, 5);
815 if (!list)
816 goto error;
818 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
819 domain = collect_non_kill_instances(scop);
820 sched = pet_scop_get_schedule(scop);
821 sched = isl_schedule_intersect_domain(sched,
822 isl_union_set_copy(domain));
823 may_reads = pet_scop_get_may_reads(scop);
824 may_writes = pet_scop_get_may_writes(scop);
825 must_writes = pet_scop_get_must_writes(scop);
826 pet_scop_free(scop);
828 list->obj[0].type = isl_obj_union_set;
829 list->obj[0].v = domain;
830 list->obj[1].type = isl_obj_union_map;
831 list->obj[1].v = must_writes;
832 list->obj[2].type = isl_obj_union_map;
833 list->obj[2].v = may_writes;
834 list->obj[3].type = isl_obj_union_map;
835 list->obj[3].v = may_reads;
836 list->obj[4].type = isl_obj_schedule;
837 list->obj[4].v = sched;
839 if (!list->obj[0].v || !list->obj[1].v ||
840 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
841 goto error;
843 isl_str_free(str);
844 return list;
845 error:
846 isl_list_free(list);
847 isl_str_free(str);
848 return NULL;
850 #endif
852 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
854 isl_union_set **scan = (isl_union_set **) user;
856 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
858 return isl_stat_ok;
861 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
863 isl_union_set *scan;
865 scan = isl_union_set_empty(isl_union_set_get_space(uset));
867 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
868 isl_union_set_free(scan);
869 return uset;
872 isl_union_set_free(uset);
873 return scan;
876 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
878 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
881 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
882 __isl_take isl_union_pw_qpolynomial *upwqp)
884 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
887 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
888 __isl_take isl_union_pw_qpolynomial *upwqp)
890 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
893 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
894 __isl_take isl_union_pw_qpolynomial *upwqp)
896 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
899 /* Return the domain of "schedule".
901 static __isl_give isl_union_set *schedule_domain(
902 __isl_take isl_schedule *schedule)
904 isl_union_set *domain;
906 domain = isl_schedule_get_domain(schedule);
907 isl_schedule_free(schedule);
909 return domain;
912 /* Convert "schedule" to a union map representation.
914 static __isl_give isl_union_map *schedule_map(__isl_take isl_schedule *schedule)
916 isl_union_map *map;
918 map = isl_schedule_get_map(schedule);
919 isl_schedule_free(schedule);
921 return map;
924 typedef void *(*isc_un_op_fn)(void *arg);
925 struct isc_un_op {
926 enum isl_token_type op;
927 isl_obj_type arg;
928 isl_obj_type res;
929 isc_un_op_fn fn;
931 struct isc_named_un_op {
932 char *name;
933 struct isc_un_op op;
935 struct isc_named_un_op named_un_ops[] = {
936 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
937 (isc_un_op_fn) &isl_union_map_affine_hull } },
938 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
939 (isc_un_op_fn) &isl_union_set_affine_hull } },
940 {"card", { -1, isl_obj_union_set,
941 isl_obj_union_pw_qpolynomial,
942 (isc_un_op_fn) &isl_union_set_card } },
943 {"card", { -1, isl_obj_union_map,
944 isl_obj_union_pw_qpolynomial,
945 (isc_un_op_fn) &isl_union_map_card } },
946 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
947 (isc_un_op_fn) &isl_union_set_coalesce } },
948 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
949 (isc_un_op_fn) &isl_union_map_coalesce } },
950 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
951 isl_obj_union_pw_qpolynomial,
952 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
953 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
954 isl_obj_union_pw_qpolynomial_fold,
955 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
956 {"coefficients", { -1, isl_obj_union_set,
957 isl_obj_union_set,
958 (isc_un_op_fn) &isl_union_set_coefficients } },
959 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
960 (isc_un_op_fn) &isl_union_set_solutions } },
961 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
962 (isc_un_op_fn) &isl_union_map_deltas } },
963 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
964 (isc_un_op_fn) &isl_union_map_deltas_map } },
965 {"dom", { -1, isl_obj_schedule, isl_obj_union_set,
966 (isc_un_op_fn) &schedule_domain } },
967 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
968 (isc_un_op_fn) &isl_union_map_domain } },
969 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
970 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
971 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
972 isl_obj_union_set,
973 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
974 {"domain", { -1, isl_obj_schedule, isl_obj_union_set,
975 (isc_un_op_fn) &schedule_domain } },
976 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
977 (isc_un_op_fn) &isl_union_map_domain } },
978 {"domain", { -1, isl_obj_union_pw_qpolynomial,
979 isl_obj_union_set,
980 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
981 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
982 isl_obj_union_set,
983 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
984 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
985 (isc_un_op_fn) &isl_union_map_domain_map } },
986 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
987 (isc_un_op_fn) &isl_union_map_range } },
988 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
989 (isc_un_op_fn) &isl_union_map_range } },
990 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
991 (isc_un_op_fn) &isl_union_map_range_map } },
992 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
993 (isc_un_op_fn) &isl_union_set_identity } },
994 {"lattice_width", { -1, isl_obj_union_set,
995 isl_obj_union_pw_qpolynomial,
996 (isc_un_op_fn) &isl_union_set_lattice_width } },
997 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
998 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
999 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
1000 (isc_un_op_fn) &isl_union_map_lexmin } },
1001 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
1002 (isc_un_op_fn) &isl_union_map_lexmax } },
1003 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
1004 (isc_un_op_fn) &isl_union_set_lexmin } },
1005 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
1006 (isc_un_op_fn) &isl_union_set_lexmax } },
1007 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
1008 (isc_un_op_fn) &isl_union_set_lift } },
1009 {"map", { -1, isl_obj_schedule, isl_obj_union_map,
1010 (isc_un_op_fn) &schedule_map } },
1011 {"params", { -1, isl_obj_union_map, isl_obj_set,
1012 (isc_un_op_fn) &isl_union_map_params } },
1013 {"params", { -1, isl_obj_union_set, isl_obj_set,
1014 (isc_un_op_fn) &isl_union_set_params } },
1015 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
1016 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
1017 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
1018 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
1019 {"poly", { -1, isl_obj_union_pw_qpolynomial,
1020 isl_obj_union_pw_qpolynomial,
1021 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
1022 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
1023 isl_obj_union_pw_qpolynomial,
1024 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
1025 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
1026 isl_obj_union_pw_qpolynomial,
1027 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
1028 #ifdef HAVE_PET
1029 {"parse_file", { -1, isl_obj_str, isl_obj_list,
1030 (isc_un_op_fn) &parse } },
1031 #endif
1032 {"pow", { -1, isl_obj_union_map, isl_obj_list,
1033 (isc_un_op_fn) &union_map_power } },
1034 {"sample", { -1, isl_obj_union_set, isl_obj_set,
1035 (isc_un_op_fn) &union_set_sample } },
1036 {"sample", { -1, isl_obj_union_map, isl_obj_map,
1037 (isc_un_op_fn) &union_map_sample } },
1038 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
1039 (isc_un_op_fn) &union_set_scan } },
1040 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
1041 (isc_un_op_fn) &union_map_scan } },
1042 {"sum", { -1, isl_obj_union_pw_qpolynomial,
1043 isl_obj_union_pw_qpolynomial,
1044 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
1045 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
1046 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
1047 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
1048 (isc_un_op_fn) &isl_union_set_unwrap } },
1049 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
1050 (isc_un_op_fn) &isl_union_map_wrap } },
1051 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
1052 (isc_un_op_fn) &isl_union_map_zip } },
1053 NULL
1056 struct isl_named_obj {
1057 char *name;
1058 struct isl_obj obj;
1061 static void free_obj(struct isl_obj obj)
1063 obj.type->free(obj.v);
1066 static int same_name(const void *entry, const void *val)
1068 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1070 return !strcmp(named->name, val);
1073 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1074 char *name, struct isl_obj obj)
1076 struct isl_hash_table_entry *entry;
1077 uint32_t name_hash;
1078 struct isl_named_obj *named;
1080 name_hash = isl_hash_string(isl_hash_init(), name);
1081 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1082 if (!entry)
1083 goto error;
1084 if (entry->data) {
1085 named = entry->data;
1086 free_obj(named->obj);
1087 free(name);
1088 } else {
1089 named = isl_alloc_type(ctx, struct isl_named_obj);
1090 if (!named)
1091 goto error;
1092 named->name = name;
1093 entry->data = named;
1095 named->obj = obj;
1097 return 0;
1098 error:
1099 free_obj(obj);
1100 free(name);
1101 return -1;
1104 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1105 struct isl_hash_table *table, char *name)
1107 struct isl_obj obj = { isl_obj_none, NULL };
1108 struct isl_hash_table_entry *entry;
1109 uint32_t name_hash;
1111 name_hash = isl_hash_string(isl_hash_init(), name);
1112 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1113 if (entry) {
1114 struct isl_named_obj *named;
1115 named = entry->data;
1116 obj = named->obj;
1117 } else if (isdigit(name[0]))
1118 fprintf(stderr, "unknown identifier '$%s'\n", name);
1119 else
1120 fprintf(stderr, "unknown identifier '%s'\n", name);
1122 free(name);
1123 obj.v = obj.type->copy(obj.v);
1124 return obj;
1127 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1129 if (obj.type == super)
1130 return 1;
1131 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1132 return 1;
1133 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1134 return 1;
1135 if (obj.type == isl_obj_schedule && super == isl_obj_union_map)
1136 return 1;
1137 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1138 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1139 int is_set = isl_space_is_set(space);
1140 isl_space_free(space);
1141 return is_set;
1143 if (obj.type == isl_obj_pw_qpolynomial &&
1144 super == isl_obj_union_pw_qpolynomial)
1145 return 1;
1146 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1147 super == isl_obj_union_pw_qpolynomial_fold)
1148 return 1;
1149 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1150 return 1;
1151 if (obj.type == isl_obj_list) {
1152 struct isl_list *list = obj.v;
1153 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1154 return is_subtype(list->obj[0], super);
1156 if (super == isl_obj_str)
1157 return 1;
1158 return 0;
1161 static struct isl_obj obj_at(struct isl_obj obj, int i)
1163 struct isl_list *list = obj.v;
1165 obj = list->obj[i];
1166 obj.v = obj.type->copy(obj.v);
1168 isl_list_free(list);
1170 return obj;
1173 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1174 isl_obj_type type)
1176 if (obj.type == type)
1177 return obj;
1178 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1179 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1180 obj.type = isl_obj_union_set;
1181 obj.v = isl_union_set_from_set(set);
1182 return obj;
1184 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1185 obj.type = isl_obj_union_map;
1186 obj.v = isl_union_map_from_map(obj.v);
1187 return obj;
1189 if (obj.type == isl_obj_schedule && type == isl_obj_union_map) {
1190 obj.type = isl_obj_union_map;
1191 obj.v = schedule_map(obj.v);
1192 return obj;
1194 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1195 obj.type = isl_obj_union_set;
1196 obj.v = isl_union_set_from_set(obj.v);
1197 return obj;
1199 if (obj.type == isl_obj_pw_qpolynomial &&
1200 type == isl_obj_union_pw_qpolynomial) {
1201 obj.type = isl_obj_union_pw_qpolynomial;
1202 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1203 return obj;
1205 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1206 type == isl_obj_union_pw_qpolynomial_fold) {
1207 obj.type = isl_obj_union_pw_qpolynomial_fold;
1208 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1209 return obj;
1211 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1212 if (type == isl_obj_union_map) {
1213 obj.type = isl_obj_union_map;
1214 return obj;
1216 if (type == isl_obj_union_pw_qpolynomial) {
1217 isl_space *dim = isl_union_set_get_space(obj.v);
1218 isl_union_set_free(obj.v);
1219 obj.v = isl_union_pw_qpolynomial_zero(dim);
1220 obj.type = isl_obj_union_pw_qpolynomial;
1221 return obj;
1223 if (type == isl_obj_union_pw_qpolynomial_fold) {
1224 isl_space *dim = isl_union_set_get_space(obj.v);
1225 isl_union_set_free(obj.v);
1226 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1227 isl_fold_list);
1228 obj.type = isl_obj_union_pw_qpolynomial_fold;
1229 return obj;
1232 if (obj.type == isl_obj_list) {
1233 struct isl_list *list = obj.v;
1234 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1235 return convert(ctx, obj_at(obj, 0), type);
1237 if (type == isl_obj_str) {
1238 isl_str *str;
1239 isl_printer *p;
1240 char *s;
1242 p = isl_printer_to_str(ctx);
1243 if (!p)
1244 goto error;
1245 p = obj.type->print(p, obj.v);
1246 s = isl_printer_get_str(p);
1247 isl_printer_free(p);
1249 str = isl_str_from_string(ctx, s);
1250 if (!str)
1251 goto error;
1252 free_obj(obj);
1253 obj.v = str;
1254 obj.type = isl_obj_str;
1255 return obj;
1258 error:
1259 free_obj(obj);
1260 obj.type = isl_obj_none;
1261 obj.v = NULL;
1262 return obj;
1265 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1266 struct isl_obj lhs)
1268 int i;
1269 int read_tok2 = 0;
1270 struct isl_token *tok, *tok2;
1272 tok = isl_stream_next_token(s);
1273 if (!tok)
1274 return NULL;
1276 for (i = 0; ; ++i) {
1277 if (!bin_ops[i].op)
1278 break;
1279 if (bin_ops[i].op != isl_token_get_type(tok))
1280 continue;
1281 if (!is_subtype(lhs, bin_ops[i].lhs))
1282 continue;
1284 isl_token_free(tok);
1285 return &bin_ops[i];
1288 for (i = 0; ; ++i) {
1289 if (!named_bin_ops[i].name)
1290 break;
1291 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1292 continue;
1293 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1294 continue;
1296 isl_token_free(tok);
1297 return &named_bin_ops[i].op;
1300 for (i = 0; ; ++i) {
1301 if (!compound_bin_ops[i].full)
1302 break;
1303 if (compound_bin_ops[i].op1 != isl_token_get_type(tok))
1304 continue;
1305 if (!read_tok2)
1306 tok2 = isl_stream_next_token(s);
1307 read_tok2 = 1;
1308 if (compound_bin_ops[i].op2 != isl_token_get_type(tok2))
1309 continue;
1310 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
1311 continue;
1313 isl_token_free(tok2);
1314 isl_token_free(tok);
1315 return &compound_bin_ops[i].op;
1318 if (read_tok2)
1319 isl_stream_push_token(s, tok2);
1320 isl_stream_push_token(s, tok);
1322 return NULL;
1325 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1327 int i;
1328 struct isl_token *tok;
1330 tok = isl_stream_next_token(s);
1331 if (!tok)
1332 return NULL;
1334 for (i = 0; ; ++i) {
1335 if (!named_un_ops[i].name)
1336 break;
1337 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1338 continue;
1340 isl_token_free(tok);
1341 return &named_un_ops[i].op;
1344 isl_stream_push_token(s, tok);
1346 return NULL;
1349 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1350 struct isl_obj arg)
1352 int i;
1354 for (i = 0; ; ++i) {
1355 if (!named_un_ops[i].name)
1356 break;
1357 if (named_un_ops[i].op.op != like->op)
1358 continue;
1359 if (!is_subtype(arg, named_un_ops[i].op.arg))
1360 continue;
1362 return &named_un_ops[i].op;
1365 return NULL;
1368 static int is_assign(struct isl_stream *s)
1370 struct isl_token *tok;
1371 struct isl_token *tok2;
1372 int assign;
1374 tok = isl_stream_next_token(s);
1375 if (!tok)
1376 return 0;
1377 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1378 isl_stream_push_token(s, tok);
1379 return 0;
1382 tok2 = isl_stream_next_token(s);
1383 if (!tok2) {
1384 isl_stream_push_token(s, tok);
1385 return 0;
1387 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1388 isl_stream_push_token(s, tok2);
1389 isl_stream_push_token(s, tok);
1391 return assign;
1394 static struct isl_obj read_obj(struct isl_stream *s,
1395 struct isl_hash_table *table);
1396 static struct isl_obj read_expr(struct isl_stream *s,
1397 struct isl_hash_table *table);
1399 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1400 struct isl_hash_table *table, struct isc_un_op *op)
1402 isl_ctx *ctx;
1403 struct isl_obj obj = { isl_obj_none, NULL };
1405 obj = read_obj(s, table);
1406 if (!obj.v)
1407 goto error;
1409 op = find_matching_un_op(op, obj);
1411 ctx = isl_stream_get_ctx(s);
1412 if (!op)
1413 isl_die(ctx, isl_error_invalid,
1414 "no such unary operator defined on given operand",
1415 goto error);
1417 obj = convert(ctx, obj, op->arg);
1418 obj.v = op->fn(obj.v);
1419 obj.type = op->res;
1421 return obj;
1422 error:
1423 free_obj(obj);
1424 obj.type = isl_obj_none;
1425 obj.v = NULL;
1426 return obj;
1429 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1431 struct isl_list *list;
1432 int exact;
1434 if (obj.type != isl_obj_union_map)
1435 obj = convert(ctx, obj, isl_obj_union_map);
1436 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1437 list = isl_list_alloc(ctx, 2);
1438 if (!list)
1439 goto error;
1441 list->obj[0].type = isl_obj_union_map;
1442 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1443 list->obj[1].type = isl_obj_bool;
1444 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1445 obj.v = list;
1446 obj.type = isl_obj_list;
1447 if (exact < 0 || !list->obj[0].v)
1448 goto error;
1450 return obj;
1451 error:
1452 free_obj(obj);
1453 obj.type = isl_obj_none;
1454 obj.v = NULL;
1455 return obj;
1458 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1460 struct isl_list *list = obj.v;
1461 struct isl_token *tok;
1462 isl_ctx *ctx;
1463 isl_val *v;
1464 int i;
1466 tok = isl_stream_next_token(s);
1467 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1468 isl_stream_error(s, tok, "expecting index");
1469 if (tok)
1470 isl_stream_push_token(s, tok);
1471 goto error;
1473 ctx = isl_stream_get_ctx(s);
1474 v = isl_token_get_val(ctx, tok);
1475 i = isl_val_get_num_si(v);
1476 isl_val_free(v);
1477 isl_token_free(tok);
1478 isl_assert(ctx, i < list->n, goto error);
1479 if (isl_stream_eat(s, ']'))
1480 goto error;
1482 return obj_at(obj, i);
1483 error:
1484 free_obj(obj);
1485 obj.type = isl_obj_none;
1486 obj.v = NULL;
1487 return obj;
1490 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1491 struct isl_hash_table *table)
1493 isl_ctx *ctx;
1494 struct isl_obj obj;
1496 obj = read_expr(s, table);
1497 ctx = isl_stream_get_ctx(s);
1498 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1499 is_subtype(obj, isl_obj_union_map), goto error);
1501 if (obj.type == isl_obj_list) {
1502 struct isl_list *list = obj.v;
1503 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1504 obj = obj_at(obj, 0);
1506 if (obj.type == isl_obj_set)
1507 obj = convert(ctx, obj, isl_obj_union_set);
1508 else if (obj.type == isl_obj_map)
1509 obj = convert(ctx, obj, isl_obj_union_map);
1510 if (obj.type == isl_obj_union_set) {
1511 obj.v = isl_union_set_apply(obj.v, umap);
1512 } else
1513 obj.v = isl_union_map_apply_range(obj.v, umap);
1514 if (!obj.v)
1515 goto error2;
1517 if (isl_stream_eat(s, ')'))
1518 goto error2;
1520 return obj;
1521 error:
1522 isl_union_map_free(umap);
1523 error2:
1524 free_obj(obj);
1525 obj.type = isl_obj_none;
1526 obj.v = NULL;
1527 return obj;
1530 static struct isl_obj apply_fun_set(struct isl_obj obj,
1531 __isl_take isl_union_set *uset)
1533 if (obj.type == isl_obj_union_pw_qpolynomial) {
1534 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1535 } else {
1536 obj.type = isl_obj_list;
1537 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1539 return obj;
1542 static struct isl_obj apply_fun_map(struct isl_obj obj,
1543 __isl_take isl_union_map *umap)
1545 if (obj.type == isl_obj_union_pw_qpolynomial) {
1546 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1547 } else {
1548 obj.type = isl_obj_list;
1549 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1551 return obj;
1554 static struct isl_obj apply_fun(struct isl_stream *s,
1555 struct isl_obj obj, struct isl_hash_table *table)
1557 struct isl_obj arg;
1558 isl_ctx *ctx;
1560 arg = read_expr(s, table);
1561 ctx = isl_stream_get_ctx(s);
1562 if (!is_subtype(arg, isl_obj_union_map) &&
1563 !is_subtype(arg, isl_obj_union_set))
1564 isl_die(ctx, isl_error_invalid,
1565 "expecting set of map argument", goto error);
1567 if (arg.type == isl_obj_list) {
1568 struct isl_list *list = arg.v;
1569 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1570 arg = obj_at(arg, 0);
1572 if (arg.type == isl_obj_set)
1573 arg = convert(ctx, arg, isl_obj_union_set);
1574 else if (arg.type == isl_obj_map)
1575 arg = convert(ctx, arg, isl_obj_union_map);
1576 if (arg.type == isl_obj_union_set)
1577 obj = apply_fun_set(obj, arg.v);
1578 else
1579 obj = apply_fun_map(obj, arg.v);
1580 if (!obj.v)
1581 goto error2;
1583 if (isl_stream_eat(s, ')'))
1584 goto error2;
1586 return obj;
1587 error:
1588 free_obj(arg);
1589 error2:
1590 free_obj(obj);
1591 obj.type = isl_obj_none;
1592 obj.v = NULL;
1593 return obj;
1596 struct add_vertex_data {
1597 struct isl_list *list;
1598 int i;
1601 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1603 struct add_vertex_data *data = (struct add_vertex_data *)user;
1604 isl_multi_aff *ma;
1605 isl_set *dom;
1607 ma = isl_vertex_get_expr(vertex);
1608 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1610 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1611 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1612 data->i++;
1614 isl_vertex_free(vertex);
1616 return isl_stat_ok;
1619 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1621 isl_ctx *ctx;
1622 isl_basic_set *hull;
1623 isl_vertices *vertices = NULL;
1624 struct isl_list *list = NULL;
1625 isl_stat r;
1626 struct add_vertex_data *data = (struct add_vertex_data *)user;
1628 set = isl_set_remove_divs(set);
1629 hull = isl_set_convex_hull(set);
1630 vertices = isl_basic_set_compute_vertices(hull);
1631 isl_basic_set_free(hull);
1633 list = data->list;
1635 ctx = isl_vertices_get_ctx(vertices);
1636 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1637 if (!data->list)
1638 goto error;
1640 data->i = 0;
1641 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1643 data->list = isl_list_concat(list, data->list);
1645 isl_vertices_free(vertices);
1647 return r;
1648 error:
1649 data->list = list;
1650 isl_vertices_free(vertices);
1651 return isl_stat_error;
1654 static struct isl_obj vertices(struct isl_stream *s,
1655 struct isl_hash_table *table)
1657 isl_ctx *ctx;
1658 struct isl_obj obj;
1659 struct isl_list *list = NULL;
1660 isl_union_set *uset = NULL;
1661 struct add_vertex_data data = { NULL };
1663 obj = read_expr(s, table);
1664 ctx = isl_stream_get_ctx(s);
1665 obj = convert(ctx, obj, isl_obj_union_set);
1666 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1667 uset = obj.v;
1668 obj.v = NULL;
1670 list = isl_list_alloc(ctx, 0);
1671 if (!list)
1672 goto error;
1674 data.list = list;
1676 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1677 goto error;
1679 isl_union_set_free(uset);
1681 obj.type = isl_obj_list;
1682 obj.v = data.list;
1684 return obj;
1685 error:
1686 isl_union_set_free(uset);
1687 isl_list_free(data.list);
1688 free_obj(obj);
1689 obj.type = isl_obj_none;
1690 obj.v = NULL;
1691 return obj;
1694 static struct isl_obj type_of(struct isl_stream *s,
1695 struct isl_hash_table *table)
1697 struct isl_obj obj;
1698 const char *type = "unknown";
1700 obj = read_expr(s, table);
1702 if (obj.type == isl_obj_map ||
1703 obj.type == isl_obj_union_map)
1704 type = "map";
1705 if (obj.type == isl_obj_set ||
1706 obj.type == isl_obj_union_set)
1707 type = "set";
1708 if (obj.type == isl_obj_pw_multi_aff)
1709 type = "piecewise multi-quasiaffine expression";
1710 if (obj.type == isl_obj_pw_qpolynomial ||
1711 obj.type == isl_obj_union_pw_qpolynomial)
1712 type = "piecewise quasipolynomial";
1713 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1714 obj.type == isl_obj_union_pw_qpolynomial_fold)
1715 type = "piecewise quasipolynomial fold";
1716 if (obj.type == isl_obj_list)
1717 type = "list";
1718 if (obj.type == isl_obj_bool)
1719 type = "boolean";
1720 if (obj.type == isl_obj_str)
1721 type = "string";
1722 if (obj.type == isl_obj_val)
1723 type = "value";
1724 if (obj.type == isl_obj_schedule)
1725 type = "schedule";
1727 free_obj(obj);
1728 obj.type = isl_obj_str;
1729 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1731 return obj;
1734 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1735 struct isl_hash_table *table)
1737 struct isl_obj obj;
1738 isl_ctx *ctx;
1740 obj = read_obj(s, table);
1741 ctx = isl_stream_get_ctx(s);
1742 obj = convert(ctx, obj, isl_obj_union_set);
1743 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1744 return obj.v;
1745 error:
1746 free_obj(obj);
1747 return NULL;
1750 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1751 struct isl_hash_table *table)
1753 struct isl_obj obj;
1754 isl_ctx *ctx;
1756 obj = read_obj(s, table);
1757 ctx = isl_stream_get_ctx(s);
1758 obj = convert(ctx, obj, isl_obj_union_map);
1759 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1760 return obj.v;
1761 error:
1762 free_obj(obj);
1763 return NULL;
1766 /* Read a schedule in the form of either a schedule (tree) or a union map
1767 * from "s" and store the schedule in "access".
1769 static __isl_give isl_union_access_info *access_info_set_schedule(
1770 __isl_take isl_union_access_info *access, struct isl_stream *s,
1771 struct isl_hash_table *table)
1773 struct isl_obj obj;
1774 isl_ctx *ctx;
1776 obj = read_obj(s, table);
1777 if (obj.type == isl_obj_schedule)
1778 return isl_union_access_info_set_schedule(access, obj.v);
1779 ctx = isl_stream_get_ctx(s);
1780 obj = convert(ctx, obj, isl_obj_union_map);
1782 return isl_union_access_info_set_schedule_map(access, obj.v);
1785 static struct isl_obj last_any(struct isl_stream *s,
1786 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1787 __isl_take isl_union_map *may_source)
1789 struct isl_obj obj = { isl_obj_none, NULL };
1790 isl_union_access_info *access;
1791 isl_union_flow *flow;
1792 isl_union_map *sink = NULL;
1793 isl_union_map *may_dep;
1795 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1796 goto error;
1798 sink = read_map(s, table);
1799 if (!sink)
1800 goto error;
1802 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1803 goto error;
1805 access = isl_union_access_info_from_sink(sink);
1806 access = isl_union_access_info_set_must_source(access, must_source);
1807 access = isl_union_access_info_set_may_source(access, may_source);
1808 access = access_info_set_schedule(access, s, table);
1809 flow = isl_union_access_info_compute_flow(access);
1810 may_dep = isl_union_flow_get_may_dependence(flow);
1811 isl_union_flow_free(flow);
1813 if (!may_dep)
1814 return obj;
1816 obj.type = isl_obj_union_map;
1817 obj.v = may_dep;
1819 return obj;
1820 error:
1821 isl_union_map_free(may_source);
1822 isl_union_map_free(must_source);
1823 isl_union_map_free(sink);
1824 free_obj(obj);
1825 obj.type = isl_obj_none;
1826 obj.v = NULL;
1827 return obj;
1830 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1832 struct isl_obj obj = { isl_obj_none, NULL };
1833 isl_union_access_info *access;
1834 isl_union_flow *flow;
1835 isl_union_map *may_source = NULL;
1836 isl_union_map *sink = NULL;
1837 isl_union_map *may_dep;
1839 may_source = read_map(s, table);
1840 if (!may_source)
1841 goto error;
1843 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1844 isl_union_map *must_source;
1845 must_source = read_map(s, table);
1846 if (!must_source)
1847 goto error;
1848 return last_any(s, table, must_source, may_source);
1851 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1852 goto error;
1854 sink = read_map(s, table);
1855 if (!sink)
1856 goto error;
1858 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1859 goto error;
1861 access = isl_union_access_info_from_sink(sink);
1862 access = isl_union_access_info_set_may_source(access, may_source);
1863 access = access_info_set_schedule(access, s, table);
1864 flow = isl_union_access_info_compute_flow(access);
1865 may_dep = isl_union_flow_get_may_dependence(flow);
1866 isl_union_flow_free(flow);
1868 if (!may_dep)
1869 return obj;
1871 obj.type = isl_obj_union_map;
1872 obj.v = may_dep;
1874 return obj;
1875 error:
1876 isl_union_map_free(may_source);
1877 isl_union_map_free(sink);
1878 free_obj(obj);
1879 obj.type = isl_obj_none;
1880 obj.v = NULL;
1881 return obj;
1884 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1886 struct isl_obj obj = { isl_obj_none, NULL };
1887 struct isl_list *list = NULL;
1888 isl_union_access_info *access;
1889 isl_union_flow *flow;
1890 isl_union_map *must_source = NULL;
1891 isl_union_map *sink = NULL;
1892 isl_union_map *must_dep;
1893 isl_union_map *must_no_source;
1895 must_source = read_map(s, table);
1896 if (!must_source)
1897 goto error;
1899 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1900 isl_union_map *may_source;
1901 may_source = read_map(s, table);
1902 if (!may_source)
1903 goto error;
1904 return last_any(s, table, must_source, may_source);
1907 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1908 if (!list)
1909 goto error;
1911 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1912 goto error;
1914 sink = read_map(s, table);
1915 if (!sink)
1916 goto error;
1918 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1919 goto error;
1921 access = isl_union_access_info_from_sink(sink);
1922 access = isl_union_access_info_set_must_source(access, must_source);
1923 access = access_info_set_schedule(access, s, table);
1924 flow = isl_union_access_info_compute_flow(access);
1925 must_dep = isl_union_flow_get_must_dependence(flow);
1926 must_no_source = isl_union_flow_get_must_no_source(flow);
1927 isl_union_flow_free(flow);
1929 list->obj[0].type = isl_obj_union_map;
1930 list->obj[0].v = must_dep;
1931 list->obj[1].type = isl_obj_union_map;
1932 list->obj[1].v = must_no_source;
1934 if (!must_dep || !must_no_source) {
1935 isl_list_free(list);
1936 return obj;
1939 obj.v = list;
1940 obj.type = isl_obj_list;
1942 return obj;
1943 error:
1944 isl_list_free(list);
1945 isl_union_map_free(must_source);
1946 isl_union_map_free(sink);
1947 free_obj(obj);
1948 obj.type = isl_obj_none;
1949 obj.v = NULL;
1950 return obj;
1953 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1954 struct isl_hash_table *table)
1956 isl_union_set *domain;
1957 isl_union_map *validity;
1958 isl_union_map *proximity;
1960 domain = read_set(s, table);
1961 if (!domain)
1962 return NULL;
1964 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1965 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1967 for (;;) {
1968 isl_union_map *umap;
1969 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1970 umap = read_map(s, table);
1971 validity = isl_union_map_union(validity, umap);
1972 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1973 umap = read_map(s, table);
1974 proximity = isl_union_map_union(proximity, umap);
1975 } else
1976 break;
1979 return isl_union_set_compute_schedule(domain, validity, proximity);
1982 static struct isl_obj schedule(struct isl_stream *s,
1983 struct isl_hash_table *table)
1985 struct isl_obj obj = { isl_obj_none, NULL };
1986 isl_schedule *schedule;
1988 schedule = get_schedule(s, table);
1990 obj.v = schedule;
1991 obj.type = isl_obj_schedule;
1993 return obj;
1996 /* Read a schedule for code generation in the form of either
1997 * a schedule tree or a union map.
1998 * If the input is a set rather than a map, then we construct
1999 * an identity union map schedule on the given set.
2001 static struct isl_obj get_codegen_schedule(struct isl_stream *s,
2002 struct isl_hash_table *table)
2004 struct isl_obj obj;
2005 isl_ctx *ctx;
2007 obj = read_obj(s, table);
2008 ctx = isl_stream_get_ctx(s);
2010 if (obj.type == isl_obj_schedule)
2011 return obj;
2012 if (is_subtype(obj, isl_obj_union_set)) {
2013 obj = convert(ctx, obj, isl_obj_union_set);
2014 obj.v = isl_union_set_identity(obj.v);
2015 obj.type = isl_obj_union_map;
2017 if (is_subtype(obj, isl_obj_union_map))
2018 return convert(ctx, obj, isl_obj_union_map);
2020 free_obj(obj);
2021 obj.v = NULL;
2022 obj.type = isl_obj_none;
2023 isl_die(ctx, isl_error_invalid, "expecting schedule, set or map",
2024 return obj);
2027 /* Generate an AST for the given schedule and options and return the AST.
2029 static __isl_give isl_ast_node *get_ast_from_union_map(
2030 __isl_take isl_union_map *schedule, __isl_take isl_union_map *options)
2032 isl_space *space;
2033 isl_set *context;
2034 isl_ast_build *build;
2035 isl_ast_node *tree;
2037 space = isl_union_map_get_space(schedule);
2038 context = isl_set_universe(isl_space_params(space));
2040 build = isl_ast_build_from_context(context);
2041 build = isl_ast_build_set_options(build, options);
2042 tree = isl_ast_build_ast_from_schedule(build, schedule);
2043 isl_ast_build_free(build);
2045 return tree;
2048 /* Generate an AST for the given schedule and return the AST.
2050 static __isl_give isl_ast_node *get_ast_from_schedule(
2051 __isl_take isl_schedule *schedule)
2053 isl_ast_build *build;
2054 isl_ast_node *tree;
2056 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
2057 tree = isl_ast_build_node_from_schedule(build, schedule);
2058 isl_ast_build_free(build);
2060 return tree;
2063 /* Print the AST "tree" on the printer "p".
2065 static __isl_give isl_printer *print_ast(__isl_take isl_printer *p,
2066 __isl_take isl_ast_node *tree)
2068 int format;
2070 format = isl_printer_get_output_format(p);
2071 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2072 p = isl_printer_print_ast_node(p, tree);
2073 p = isl_printer_set_output_format(p, format);
2075 isl_ast_node_free(tree);
2077 return p;
2080 /* Perform the codegen operation.
2081 * In particular, read a schedule, check if the user has specified any options
2082 * and then generate an AST from the schedule (and options) and print it.
2083 * In case the schedule is specified as a schedule tree, the AST generation
2084 * options are embedded in the schedule, so they are not read in separately.
2086 static __isl_give isl_printer *codegen(struct isl_stream *s,
2087 struct isl_hash_table *table, __isl_take isl_printer *p)
2089 struct isl_obj obj;
2090 isl_ast_node *tree;
2092 obj = get_codegen_schedule(s, table);
2093 if (!obj.v)
2094 return p;
2096 if (obj.type == isl_obj_schedule) {
2097 isl_schedule *schedule = obj.v;
2099 tree = get_ast_from_schedule(schedule);
2100 } else {
2101 isl_union_map *schedule = obj.v;
2102 isl_union_map *options;
2104 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
2105 options = read_map(s, table);
2106 else
2107 options = isl_union_map_empty(
2108 isl_union_map_get_space(schedule));
2110 tree = get_ast_from_union_map(schedule, options);
2113 if (tree)
2114 p = print_ast(p, tree);
2116 isl_stream_eat(s, ';');
2118 return p;
2121 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2123 struct isl_token *tok;
2124 isl_ctx *ctx;
2125 isl_val *v;
2127 ctx = isl_stream_get_ctx(s);
2128 if (isl_stream_eat_if_available(s, '+'))
2129 return transitive_closure(ctx, obj);
2131 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2132 if (obj.type != isl_obj_union_map)
2133 obj = convert(ctx, obj, isl_obj_union_map);
2135 tok = isl_stream_next_token(s);
2136 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2137 isl_stream_error(s, tok, "expecting integer exponent");
2138 if (tok)
2139 isl_stream_push_token(s, tok);
2140 goto error;
2143 v = isl_token_get_val(ctx, tok);
2144 if (isl_val_is_zero(v)) {
2145 isl_stream_error(s, tok, "expecting non-zero exponent");
2146 isl_val_free(v);
2147 if (tok)
2148 isl_stream_push_token(s, tok);
2149 goto error;
2152 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2153 isl_token_free(tok);
2154 if (!obj.v)
2155 goto error;
2157 return obj;
2158 error:
2159 free_obj(obj);
2160 obj.type = isl_obj_none;
2161 obj.v = NULL;
2162 return obj;
2165 static struct isl_obj check_assert(struct isl_stream *s,
2166 struct isl_hash_table *table)
2168 struct isl_obj obj;
2169 isl_ctx *ctx;
2171 obj = read_expr(s, table);
2172 ctx = isl_stream_get_ctx(s);
2173 if (obj.type != isl_obj_bool)
2174 isl_die(ctx, isl_error_invalid,
2175 "expecting boolean expression", goto error);
2176 if (obj.v != &iscc_bool_true)
2177 isl_die(ctx, isl_error_unknown,
2178 "assertion failed", abort());
2179 error:
2180 free_obj(obj);
2181 obj.type = isl_obj_none;
2182 obj.v = NULL;
2183 return obj;
2186 static struct isl_obj read_from_file(struct isl_stream *s)
2188 isl_ctx *ctx;
2189 struct isl_obj obj;
2190 struct isl_token *tok;
2191 struct isl_stream *s_file;
2192 struct iscc_options *options;
2193 char *name;
2194 FILE *file;
2196 tok = isl_stream_next_token(s);
2197 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2198 isl_stream_error(s, tok, "expecting filename");
2199 isl_token_free(tok);
2200 goto error;
2203 ctx = isl_stream_get_ctx(s);
2204 options = isl_ctx_peek_iscc_options(ctx);
2205 if (!options || !options->io) {
2206 isl_token_free(tok);
2207 isl_die(ctx, isl_error_invalid,
2208 "read operation not allowed", goto error);
2211 name = isl_token_get_str(ctx, tok);
2212 isl_token_free(tok);
2213 file = fopen(name, "r");
2214 free(name);
2215 isl_assert(ctx, file, goto error);
2217 s_file = isl_stream_new_file(ctx, file);
2218 if (!s_file) {
2219 fclose(file);
2220 goto error;
2223 obj = isl_stream_read_obj(s_file);
2225 isl_stream_free(s_file);
2226 fclose(file);
2228 return obj;
2229 error:
2230 obj.type = isl_obj_none;
2231 obj.v = NULL;
2232 return obj;
2235 static struct isl_obj write_to_file(struct isl_stream *s,
2236 struct isl_hash_table *table)
2238 struct isl_obj obj = { isl_obj_none, NULL };
2239 struct isl_token *tok;
2240 struct iscc_options *options;
2241 char *name;
2242 FILE *file;
2243 isl_ctx *ctx;
2244 isl_printer *p;
2246 tok = isl_stream_next_token(s);
2247 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2248 isl_stream_error(s, tok, "expecting filename");
2249 isl_token_free(tok);
2250 goto error;
2253 obj = read_expr(s, table);
2255 ctx = isl_stream_get_ctx(s);
2256 options = isl_ctx_peek_iscc_options(ctx);
2257 if (!options || !options->io) {
2258 isl_token_free(tok);
2259 isl_die(ctx, isl_error_invalid,
2260 "write operation not allowed", goto error);
2263 name = isl_token_get_str(ctx, tok);
2264 isl_token_free(tok);
2265 file = fopen(name, "w");
2266 free(name);
2267 if (!file)
2268 isl_die(ctx, isl_error_unknown,
2269 "could not open file for writing", goto error);
2271 p = isl_printer_to_file(ctx, file);
2272 p = isl_printer_set_output_format(p, options->format);
2273 p = obj.type->print(p, obj.v);
2274 p = isl_printer_end_line(p);
2275 isl_printer_free(p);
2277 fclose(file);
2278 error:
2279 free_obj(obj);
2280 obj.type = isl_obj_none;
2281 obj.v = NULL;
2282 return obj;
2285 static struct isl_obj read_string_if_available(struct isl_stream *s)
2287 struct isl_token *tok;
2288 struct isl_obj obj = { isl_obj_none, NULL };
2290 tok = isl_stream_next_token(s);
2291 if (!tok)
2292 return obj;
2293 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2294 isl_str *str;
2295 str = isl_str_alloc(isl_stream_get_ctx(s));
2296 if (!str)
2297 goto error;
2298 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2299 isl_token_free(tok);
2300 obj.v = str;
2301 obj.type = isl_obj_str;
2302 } else
2303 isl_stream_push_token(s, tok);
2304 return obj;
2305 error:
2306 isl_token_free(tok);
2307 return obj;
2310 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2312 struct isl_token *tok;
2313 struct isl_obj obj = { isl_obj_none, NULL };
2314 int type;
2316 tok = isl_stream_next_token(s);
2317 if (!tok)
2318 return obj;
2319 type = isl_token_get_type(tok);
2320 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2321 int is_true = type == ISL_TOKEN_TRUE;
2322 isl_token_free(tok);
2323 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2324 obj.type = isl_obj_bool;
2325 } else
2326 isl_stream_push_token(s, tok);
2327 return obj;
2330 static __isl_give char *read_ident(struct isl_stream *s)
2332 char *name;
2333 isl_val *v;
2334 struct isl_token *tok, *tok2;
2336 name = isl_stream_read_ident_if_available(s);
2337 if (name)
2338 return name;
2340 tok = isl_stream_next_token(s);
2341 if (!tok)
2342 return NULL;
2343 if (isl_token_get_type(tok) != '$') {
2344 isl_stream_push_token(s, tok);
2345 return NULL;
2347 tok2 = isl_stream_next_token(s);
2348 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2349 if (tok2)
2350 isl_stream_push_token(s, tok2);
2351 isl_stream_push_token(s, tok);
2352 return NULL;
2355 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2356 name = isl_val_to_str(v);
2357 isl_val_free(v);
2358 isl_token_free(tok);
2359 isl_token_free(tok2);
2361 return name;
2364 static struct isl_obj read_list(struct isl_stream *s,
2365 struct isl_hash_table *table, struct isl_obj obj)
2367 struct isl_list *list;
2369 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2370 if (!list)
2371 goto error;
2372 list->obj[0] = obj;
2373 list->obj[1] = read_obj(s, table);
2374 obj.v = list;
2375 obj.type = isl_obj_list;
2377 if (!list->obj[1].v)
2378 goto error;
2380 while (isl_stream_eat_if_available(s, ',')) {
2381 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2382 if (!obj.v)
2383 goto error;
2386 return obj;
2387 error:
2388 free_obj(obj);
2389 obj.type = isl_obj_none;
2390 obj.v = NULL;
2391 return obj;
2394 static struct isl_obj read_obj(struct isl_stream *s,
2395 struct isl_hash_table *table)
2397 isl_ctx *ctx;
2398 struct isl_obj obj = { isl_obj_none, NULL };
2399 char *name = NULL;
2400 struct isc_un_op *op = NULL;
2402 obj = read_string_if_available(s);
2403 if (obj.v)
2404 return obj;
2405 obj = read_bool_if_available(s);
2406 if (obj.v)
2407 return obj;
2408 ctx = isl_stream_get_ctx(s);
2409 if (isl_stream_eat_if_available(s, '(')) {
2410 if (isl_stream_next_token_is(s, ')')) {
2411 obj.type = isl_obj_list;
2412 obj.v = isl_list_alloc(ctx, 0);
2413 } else {
2414 obj = read_expr(s, table);
2415 if (obj.v && isl_stream_eat_if_available(s, ','))
2416 obj = read_list(s, table, obj);
2418 if (!obj.v || isl_stream_eat(s, ')'))
2419 goto error;
2420 } else {
2421 op = read_prefix_un_op_if_available(s);
2422 if (op)
2423 return read_un_op_expr(s, table, op);
2425 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2426 return check_assert(s, table);
2427 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2428 return read_from_file(s);
2429 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2430 return write_to_file(s, table);
2431 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2432 return vertices(s, table);
2433 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2434 return any(s, table);
2435 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2436 return last(s, table);
2437 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2438 return schedule(s, table);
2439 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2440 return type_of(s, table);
2442 name = read_ident(s);
2443 if (name)
2444 obj = stored_obj(ctx, table, name);
2445 else
2446 obj = isl_stream_read_obj(s);
2447 if (!obj.v)
2448 goto error;
2451 if (isl_stream_eat_if_available(s, '^'))
2452 obj = power(s, obj);
2453 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2454 obj = obj_at_index(s, obj);
2455 else if (is_subtype(obj, isl_obj_union_map) &&
2456 isl_stream_eat_if_available(s, '(')) {
2457 obj = convert(ctx, obj, isl_obj_union_map);
2458 obj = apply(s, obj.v, table);
2459 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2460 isl_stream_eat_if_available(s, '(')) {
2461 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2462 obj = apply_fun(s, obj, table);
2463 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2464 isl_stream_eat_if_available(s, '(')) {
2465 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2466 obj = apply_fun(s, obj, table);
2469 return obj;
2470 error:
2471 free_obj(obj);
2472 obj.type = isl_obj_none;
2473 obj.v = NULL;
2474 return obj;
2477 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2478 struct isl_obj lhs, struct isl_obj rhs)
2480 int i;
2482 for (i = 0; ; ++i) {
2483 if (!bin_ops[i].op)
2484 break;
2485 if (bin_ops[i].op != like->op)
2486 continue;
2487 if (!is_subtype(lhs, bin_ops[i].lhs))
2488 continue;
2489 if (!is_subtype(rhs, bin_ops[i].rhs))
2490 continue;
2492 return &bin_ops[i];
2495 for (i = 0; ; ++i) {
2496 if (!named_bin_ops[i].name)
2497 break;
2498 if (named_bin_ops[i].op.op != like->op)
2499 continue;
2500 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2501 continue;
2502 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2503 continue;
2505 return &named_bin_ops[i].op;
2508 for (i = 0; ; ++i) {
2509 if (!compound_bin_ops[i].full)
2510 break;
2511 if (compound_bin_ops[i].op.op != like->op)
2512 continue;
2513 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
2514 continue;
2515 if (!is_subtype(rhs, compound_bin_ops[i].op.rhs))
2516 continue;
2518 return &compound_bin_ops[i].op;
2521 return NULL;
2524 static int next_is_neg_int(struct isl_stream *s)
2526 struct isl_token *tok;
2527 int ret;
2529 tok = isl_stream_next_token(s);
2530 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2531 isl_val *v;
2532 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2533 ret = isl_val_is_neg(v);
2534 isl_val_free(v);
2535 } else
2536 ret = 0;
2537 isl_stream_push_token(s, tok);
2539 return ret;
2542 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2543 struct isl_obj lhs, struct isl_obj rhs)
2545 struct isl_obj obj;
2547 lhs = convert(ctx, lhs, op->lhs);
2548 rhs = convert(ctx, rhs, op->rhs);
2549 if (op->res != isl_obj_bool)
2550 obj.v = op->o.fn(lhs.v, rhs.v);
2551 else {
2552 int res = op->o.test(lhs.v, rhs.v);
2553 free_obj(lhs);
2554 free_obj(rhs);
2555 obj.v = iscc_bool_from_int(res);
2557 obj.type = op->res;
2559 return obj;
2562 static struct isl_obj read_expr(struct isl_stream *s,
2563 struct isl_hash_table *table)
2565 isl_ctx *ctx;
2566 struct isl_obj obj = { isl_obj_none, NULL };
2567 struct isl_obj right_obj = { isl_obj_none, NULL };
2569 obj = read_obj(s, table);
2570 ctx = isl_stream_get_ctx(s);
2571 for (; obj.v;) {
2572 struct isc_bin_op *op = NULL;
2574 op = read_bin_op_if_available(s, obj);
2575 if (!op)
2576 break;
2578 right_obj = read_obj(s, table);
2580 op = find_matching_bin_op(op, obj, right_obj);
2582 if (!op)
2583 isl_die(ctx, isl_error_invalid,
2584 "no such binary operator defined on given operands",
2585 goto error);
2587 obj = call_bin_op(ctx, op, obj, right_obj);
2590 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2591 right_obj = read_obj(s, table);
2592 obj.v = isl_val_add(obj.v, right_obj.v);
2595 return obj;
2596 error:
2597 free_obj(right_obj);
2598 free_obj(obj);
2599 obj.type = isl_obj_none;
2600 obj.v = NULL;
2601 return obj;
2604 static __isl_give isl_printer *source_file(struct isl_stream *s,
2605 struct isl_hash_table *table, __isl_take isl_printer *p);
2607 /* Print "obj" to the printer "p".
2608 * If the object is a schedule, then print it in block format.
2610 static __isl_give isl_printer *print_obj(__isl_take isl_printer *p,
2611 struct isl_obj obj)
2613 if (obj.type != isl_obj_schedule)
2614 return obj.type->print(p, obj.v);
2616 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
2617 p = obj.type->print(p, obj.v);
2618 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2620 return p;
2623 static __isl_give isl_printer *read_line(struct isl_stream *s,
2624 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2626 isl_ctx *ctx;
2627 struct isl_obj obj = { isl_obj_none, NULL };
2628 char *lhs = NULL;
2629 int assign = 0;
2630 int only_print = 0;
2631 char buf[30];
2633 if (!p)
2634 return NULL;
2635 if (isl_stream_is_empty(s))
2636 return p;
2638 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2639 return source_file(s, table, p);
2640 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2641 return codegen(s, table, p);
2643 assign = is_assign(s);
2644 if (assign) {
2645 lhs = isl_stream_read_ident_if_available(s);
2646 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2647 goto error;
2648 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2649 only_print = 1;
2650 else if (!tty)
2651 only_print = 1;
2653 obj = read_expr(s, table);
2654 ctx = isl_stream_get_ctx(s);
2655 if (isl_stream_eat(s, ';'))
2656 goto error;
2658 if (only_print) {
2659 if (obj.type != isl_obj_none && obj.v != NULL) {
2660 p = print_obj(p, obj);
2661 p = isl_printer_end_line(p);
2663 free_obj(obj);
2664 return p;
2666 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2667 static int count = 0;
2668 snprintf(buf, sizeof(buf), "$%d", count++);
2669 lhs = strdup(buf + 1);
2671 p = isl_printer_print_str(p, buf);
2672 p = isl_printer_print_str(p, " := ");
2673 p = obj.type->print(p, obj.v);
2674 p = isl_printer_end_line(p);
2676 if (lhs && do_assign(ctx, table, lhs, obj))
2677 return p;
2679 return p;
2680 error:
2681 isl_stream_flush_tokens(s);
2682 isl_stream_skip_line(s);
2683 free(lhs);
2684 free_obj(obj);
2685 return p;
2688 static isl_stat free_cb(void **entry, void *user)
2690 struct isl_named_obj *named = *entry;
2692 free_obj(named->obj);
2693 free(named->name);
2694 free(named);
2696 return isl_stat_ok;
2699 static void register_named_ops(struct isl_stream *s)
2701 int i;
2703 for (i = 0; i < ISCC_N_OP; ++i) {
2704 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2705 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2708 for (i = 0; ; ++i) {
2709 if (!named_un_ops[i].name)
2710 break;
2711 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2712 named_un_ops[i].name);
2713 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2716 for (i = 0; ; ++i) {
2717 if (!named_bin_ops[i].name)
2718 break;
2719 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2720 named_bin_ops[i].name);
2721 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2724 for (i = 0; ; ++i) {
2725 if (!compound_bin_ops[i].full)
2726 break;
2727 compound_bin_ops[i].op.op = isl_stream_register_keyword(s,
2728 compound_bin_ops[i].full);
2729 assert(compound_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2733 static __isl_give isl_printer *source_file(struct isl_stream *s,
2734 struct isl_hash_table *table, __isl_take isl_printer *p)
2736 isl_ctx *ctx;
2737 struct isl_token *tok;
2738 struct isl_stream *s_file;
2739 struct iscc_options *options;
2740 char *name;
2741 FILE *file;
2743 tok = isl_stream_next_token(s);
2744 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2745 isl_stream_error(s, tok, "expecting filename");
2746 isl_token_free(tok);
2747 return p;
2750 isl_stream_eat(s, ';');
2752 ctx = isl_stream_get_ctx(s);
2753 options = isl_ctx_peek_iscc_options(ctx);
2754 if (!options || !options->io) {
2755 isl_token_free(tok);
2756 isl_die(ctx, isl_error_invalid,
2757 "source operation not allowed", return p);
2760 name = isl_token_get_str(ctx, tok);
2761 isl_token_free(tok);
2762 file = fopen(name, "r");
2763 free(name);
2764 isl_assert(ctx, file, return p);
2766 s_file = isl_stream_new_file(ctx, file);
2767 if (!s_file) {
2768 fclose(file);
2769 return p;
2772 register_named_ops(s_file);
2774 while (!isl_stream_is_empty(s_file))
2775 p = read_line(s_file, table, p, 0);
2777 isl_stream_free(s_file);
2778 fclose(file);
2780 return p;
2783 int main(int argc, char **argv)
2785 struct isl_ctx *ctx;
2786 struct isl_stream *s;
2787 struct isl_hash_table *table;
2788 struct iscc_options *options;
2789 isl_printer *p;
2790 int tty = isatty(0);
2792 options = iscc_options_new_with_defaults();
2793 assert(options);
2795 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2796 pet_options_set_autodetect(ctx, 1);
2797 pet_options_set_encapsulate_dynamic_control(ctx, 1);
2798 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2799 s = isl_stream_new_file(ctx, stdin);
2800 assert(s);
2801 table = isl_hash_table_alloc(ctx, 10);
2802 assert(table);
2803 p = isl_printer_to_file(ctx, stdout);
2804 p = isl_printer_set_output_format(p, options->format);
2805 assert(p);
2807 register_named_ops(s);
2809 install_signal_handler(ctx);
2811 while (p) {
2812 int empty;
2814 empty = isl_stream_is_empty(s);
2815 if (empty && isl_ctx_last_error(ctx) != isl_error_abort)
2816 break;
2817 if (!empty)
2818 p = read_line(s, table, p, tty);
2819 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2820 fprintf(stderr, "Interrupted\n");
2821 isl_ctx_resume(ctx);
2822 isl_ctx_reset_error(ctx);
2826 remove_signal_handler(ctx);
2828 isl_printer_free(p);
2829 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2830 isl_hash_table_free(ctx, table);
2831 isl_stream_free(s);
2832 isl_ctx_free(ctx);
2834 return 0;