summate.c: join_compatible: use isl_space_has_equal_params
[barvinok.git] / iscc.c
blob03938f8b1bfaf8ed5a0d845692fc46f6d3bdb3ad
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 { "cross_range", { -1, isl_obj_union_map,
674 isl_obj_union_map, isl_obj_union_map,
675 (isc_bin_op_fn) &isl_union_map_range_product } },
676 NULL
679 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
681 return isl_set_from_basic_set(isl_union_set_sample(uset));
684 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
686 return isl_map_from_basic_map(isl_union_map_sample(umap));
689 static __isl_give struct isl_list *union_map_power(
690 __isl_take isl_union_map *umap)
692 isl_ctx *ctx;
693 struct isl_list *list;
694 int exact;
696 ctx = isl_union_map_get_ctx(umap);
697 list = isl_list_alloc(ctx, 2);
698 if (!list)
699 goto error2;
701 list->obj[0].type = isl_obj_union_map;
702 list->obj[0].v = isl_union_map_power(umap, &exact);
703 list->obj[1].type = isl_obj_bool;
704 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
705 if (exact < 0 || !list->obj[0].v)
706 goto error;
708 return list;
709 error2:
710 isl_union_map_free(umap);
711 error:
712 isl_list_free(list);
713 return NULL;
716 /* Compute a lower or upper bound on "upwqp" depending on "type" and
717 * return a list containing two elements, the bound and a boolean
718 * indicating whether the result is tight.
720 static __isl_give struct isl_list *union_pw_qpolynomial_bound(
721 __isl_take isl_union_pw_qpolynomial *upwqp, enum isl_fold type)
723 isl_ctx *ctx;
724 struct isl_list *list;
725 int tight;
727 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
728 list = isl_list_alloc(ctx, 2);
729 if (!list)
730 goto error2;
732 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
733 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp, type, &tight);
734 list->obj[1].type = isl_obj_bool;
735 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
736 if (tight < 0 || !list->obj[0].v)
737 goto error;
739 return list;
740 error2:
741 isl_union_pw_qpolynomial_free(upwqp);
742 error:
743 isl_list_free(list);
744 return NULL;
747 /* Compute a lower bound on "upwqp" and return a list containing
748 * two elements, the bound and a booleanindicating whether
749 * the result is tight.
751 static __isl_give struct isl_list *union_pw_qpolynomial_lower_bound(
752 __isl_take isl_union_pw_qpolynomial *upwqp)
754 return union_pw_qpolynomial_bound(upwqp, isl_fold_min);
757 /* Compute a upper bound on "upwqp" and return a list containing
758 * two elements, the bound and a booleanindicating whether
759 * the result is tight.
761 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
762 __isl_take isl_union_pw_qpolynomial *upwqp)
764 return union_pw_qpolynomial_bound(upwqp, isl_fold_max);
767 #ifdef HAVE_PET
768 /* Collect all statement instances, except those of kill statements.
770 static __isl_give isl_union_set *collect_non_kill_instances(
771 struct pet_scop *scop)
773 int i;
774 isl_set *domain_i;
775 isl_union_set *domain;
777 if (!scop)
778 return NULL;
780 domain = isl_union_set_empty(isl_set_get_space(scop->context));
782 for (i = 0; i < scop->n_stmt; ++i) {
783 struct pet_stmt *stmt = scop->stmts[i];
785 if (pet_stmt_is_kill(stmt))
786 continue;
787 domain_i = isl_set_copy(stmt->domain);
788 if (scop->stmts[i]->n_arg > 0)
789 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
790 domain = isl_union_set_add_set(domain, domain_i);
793 return domain;
796 static __isl_give isl_list *parse(__isl_take isl_str *str)
798 isl_ctx *ctx;
799 struct isl_list *list;
800 struct pet_scop *scop;
801 isl_schedule *sched;
802 isl_union_map *may_reads, *must_writes, *may_writes;
803 isl_union_set *domain;
804 struct iscc_options *options;
806 if (!str)
807 return NULL;
808 ctx = str->ctx;
810 options = isl_ctx_peek_iscc_options(ctx);
811 if (!options || !options->io) {
812 isl_str_free(str);
813 isl_die(ctx, isl_error_invalid,
814 "parse_file operation not allowed", return NULL);
817 list = isl_list_alloc(ctx, 5);
818 if (!list)
819 goto error;
821 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
822 domain = collect_non_kill_instances(scop);
823 sched = pet_scop_get_schedule(scop);
824 sched = isl_schedule_intersect_domain(sched,
825 isl_union_set_copy(domain));
826 may_reads = pet_scop_get_may_reads(scop);
827 may_writes = pet_scop_get_may_writes(scop);
828 must_writes = pet_scop_get_must_writes(scop);
829 pet_scop_free(scop);
831 list->obj[0].type = isl_obj_union_set;
832 list->obj[0].v = domain;
833 list->obj[1].type = isl_obj_union_map;
834 list->obj[1].v = must_writes;
835 list->obj[2].type = isl_obj_union_map;
836 list->obj[2].v = may_writes;
837 list->obj[3].type = isl_obj_union_map;
838 list->obj[3].v = may_reads;
839 list->obj[4].type = isl_obj_schedule;
840 list->obj[4].v = sched;
842 if (!list->obj[0].v || !list->obj[1].v ||
843 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
844 goto error;
846 isl_str_free(str);
847 return list;
848 error:
849 isl_list_free(list);
850 isl_str_free(str);
851 return NULL;
853 #endif
855 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
857 isl_union_set **scan = (isl_union_set **) user;
859 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
861 return isl_stat_ok;
864 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
866 isl_union_set *scan;
868 scan = isl_union_set_empty(isl_union_set_get_space(uset));
870 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
871 isl_union_set_free(scan);
872 return uset;
875 isl_union_set_free(uset);
876 return scan;
879 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
881 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
884 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
885 __isl_take isl_union_pw_qpolynomial *upwqp)
887 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
890 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
891 __isl_take isl_union_pw_qpolynomial *upwqp)
893 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
896 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
897 __isl_take isl_union_pw_qpolynomial *upwqp)
899 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
902 /* Return the domain of "schedule".
904 static __isl_give isl_union_set *schedule_domain(
905 __isl_take isl_schedule *schedule)
907 isl_union_set *domain;
909 domain = isl_schedule_get_domain(schedule);
910 isl_schedule_free(schedule);
912 return domain;
915 /* Convert "schedule" to a union map representation.
917 static __isl_give isl_union_map *schedule_map(__isl_take isl_schedule *schedule)
919 isl_union_map *map;
921 map = isl_schedule_get_map(schedule);
922 isl_schedule_free(schedule);
924 return map;
927 typedef void *(*isc_un_op_fn)(void *arg);
928 struct isc_un_op {
929 enum isl_token_type op;
930 isl_obj_type arg;
931 isl_obj_type res;
932 isc_un_op_fn fn;
934 struct isc_named_un_op {
935 char *name;
936 struct isc_un_op op;
938 struct isc_named_un_op named_un_ops[] = {
939 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
940 (isc_un_op_fn) &isl_union_map_affine_hull } },
941 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
942 (isc_un_op_fn) &isl_union_set_affine_hull } },
943 {"card", { -1, isl_obj_union_set,
944 isl_obj_union_pw_qpolynomial,
945 (isc_un_op_fn) &isl_union_set_card } },
946 {"card", { -1, isl_obj_union_map,
947 isl_obj_union_pw_qpolynomial,
948 (isc_un_op_fn) &isl_union_map_card } },
949 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
950 (isc_un_op_fn) &isl_union_set_coalesce } },
951 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
952 (isc_un_op_fn) &isl_union_map_coalesce } },
953 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
954 isl_obj_union_pw_qpolynomial,
955 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
956 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
957 isl_obj_union_pw_qpolynomial_fold,
958 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
959 {"coefficients", { -1, isl_obj_union_set,
960 isl_obj_union_set,
961 (isc_un_op_fn) &isl_union_set_coefficients } },
962 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
963 (isc_un_op_fn) &isl_union_set_solutions } },
964 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
965 (isc_un_op_fn) &isl_union_map_deltas } },
966 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
967 (isc_un_op_fn) &isl_union_map_deltas_map } },
968 {"dom", { -1, isl_obj_schedule, isl_obj_union_set,
969 (isc_un_op_fn) &schedule_domain } },
970 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
971 (isc_un_op_fn) &isl_union_map_domain } },
972 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
973 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
974 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
975 isl_obj_union_set,
976 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
977 {"domain", { -1, isl_obj_schedule, isl_obj_union_set,
978 (isc_un_op_fn) &schedule_domain } },
979 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
980 (isc_un_op_fn) &isl_union_map_domain } },
981 {"domain", { -1, isl_obj_union_pw_qpolynomial,
982 isl_obj_union_set,
983 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
984 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
985 isl_obj_union_set,
986 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
987 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
988 (isc_un_op_fn) &isl_union_map_domain_map } },
989 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
990 (isc_un_op_fn) &isl_union_map_range } },
991 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
992 (isc_un_op_fn) &isl_union_map_range } },
993 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
994 (isc_un_op_fn) &isl_union_map_range_map } },
995 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
996 (isc_un_op_fn) &isl_union_set_identity } },
997 {"lattice_width", { -1, isl_obj_union_set,
998 isl_obj_union_pw_qpolynomial,
999 (isc_un_op_fn) &isl_union_set_lattice_width } },
1000 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
1001 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
1002 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
1003 (isc_un_op_fn) &isl_union_map_lexmin } },
1004 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
1005 (isc_un_op_fn) &isl_union_map_lexmax } },
1006 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
1007 (isc_un_op_fn) &isl_union_set_lexmin } },
1008 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
1009 (isc_un_op_fn) &isl_union_set_lexmax } },
1010 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
1011 (isc_un_op_fn) &isl_union_set_lift } },
1012 {"map", { -1, isl_obj_schedule, isl_obj_union_map,
1013 (isc_un_op_fn) &schedule_map } },
1014 {"params", { -1, isl_obj_union_map, isl_obj_set,
1015 (isc_un_op_fn) &isl_union_map_params } },
1016 {"params", { -1, isl_obj_union_set, isl_obj_set,
1017 (isc_un_op_fn) &isl_union_set_params } },
1018 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
1019 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
1020 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
1021 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
1022 {"poly", { -1, isl_obj_union_pw_qpolynomial,
1023 isl_obj_union_pw_qpolynomial,
1024 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
1025 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
1026 isl_obj_union_pw_qpolynomial,
1027 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
1028 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
1029 isl_obj_union_pw_qpolynomial,
1030 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
1031 #ifdef HAVE_PET
1032 {"parse_file", { -1, isl_obj_str, isl_obj_list,
1033 (isc_un_op_fn) &parse } },
1034 #endif
1035 {"pow", { -1, isl_obj_union_map, isl_obj_list,
1036 (isc_un_op_fn) &union_map_power } },
1037 {"sample", { -1, isl_obj_union_set, isl_obj_set,
1038 (isc_un_op_fn) &union_set_sample } },
1039 {"sample", { -1, isl_obj_union_map, isl_obj_map,
1040 (isc_un_op_fn) &union_map_sample } },
1041 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
1042 (isc_un_op_fn) &union_set_scan } },
1043 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
1044 (isc_un_op_fn) &union_map_scan } },
1045 {"sum", { -1, isl_obj_union_pw_qpolynomial,
1046 isl_obj_union_pw_qpolynomial,
1047 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
1048 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
1049 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
1050 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
1051 (isc_un_op_fn) &isl_union_set_unwrap } },
1052 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
1053 (isc_un_op_fn) &isl_union_map_wrap } },
1054 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
1055 (isc_un_op_fn) &isl_union_map_zip } },
1056 NULL
1059 struct isl_named_obj {
1060 char *name;
1061 struct isl_obj obj;
1064 static void free_obj(struct isl_obj obj)
1066 obj.type->free(obj.v);
1069 static int same_name(const void *entry, const void *val)
1071 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1073 return !strcmp(named->name, val);
1076 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1077 char *name, struct isl_obj obj)
1079 struct isl_hash_table_entry *entry;
1080 uint32_t name_hash;
1081 struct isl_named_obj *named;
1083 name_hash = isl_hash_string(isl_hash_init(), name);
1084 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1085 if (!entry)
1086 goto error;
1087 if (entry->data) {
1088 named = entry->data;
1089 free_obj(named->obj);
1090 free(name);
1091 } else {
1092 named = isl_alloc_type(ctx, struct isl_named_obj);
1093 if (!named)
1094 goto error;
1095 named->name = name;
1096 entry->data = named;
1098 named->obj = obj;
1100 return 0;
1101 error:
1102 free_obj(obj);
1103 free(name);
1104 return -1;
1107 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1108 struct isl_hash_table *table, char *name)
1110 struct isl_obj obj = { isl_obj_none, NULL };
1111 struct isl_hash_table_entry *entry;
1112 uint32_t name_hash;
1114 name_hash = isl_hash_string(isl_hash_init(), name);
1115 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1116 if (entry) {
1117 struct isl_named_obj *named;
1118 named = entry->data;
1119 obj = named->obj;
1120 } else if (isdigit(name[0]))
1121 fprintf(stderr, "unknown identifier '$%s'\n", name);
1122 else
1123 fprintf(stderr, "unknown identifier '%s'\n", name);
1125 free(name);
1126 obj.v = obj.type->copy(obj.v);
1127 return obj;
1130 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1132 if (obj.type == super)
1133 return 1;
1134 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1135 return 1;
1136 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1137 return 1;
1138 if (obj.type == isl_obj_schedule && super == isl_obj_union_map)
1139 return 1;
1140 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1141 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1142 int is_set = isl_space_is_set(space);
1143 isl_space_free(space);
1144 return is_set;
1146 if (obj.type == isl_obj_pw_qpolynomial &&
1147 super == isl_obj_union_pw_qpolynomial)
1148 return 1;
1149 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1150 super == isl_obj_union_pw_qpolynomial_fold)
1151 return 1;
1152 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1153 return 1;
1154 if (obj.type == isl_obj_list) {
1155 struct isl_list *list = obj.v;
1156 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1157 return is_subtype(list->obj[0], super);
1159 if (super == isl_obj_str)
1160 return 1;
1161 return 0;
1164 static struct isl_obj obj_at(struct isl_obj obj, int i)
1166 struct isl_list *list = obj.v;
1168 obj = list->obj[i];
1169 obj.v = obj.type->copy(obj.v);
1171 isl_list_free(list);
1173 return obj;
1176 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1177 isl_obj_type type)
1179 if (obj.type == type)
1180 return obj;
1181 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1182 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1183 obj.type = isl_obj_union_set;
1184 obj.v = isl_union_set_from_set(set);
1185 return obj;
1187 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1188 obj.type = isl_obj_union_map;
1189 obj.v = isl_union_map_from_map(obj.v);
1190 return obj;
1192 if (obj.type == isl_obj_schedule && type == isl_obj_union_map) {
1193 obj.type = isl_obj_union_map;
1194 obj.v = schedule_map(obj.v);
1195 return obj;
1197 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1198 obj.type = isl_obj_union_set;
1199 obj.v = isl_union_set_from_set(obj.v);
1200 return obj;
1202 if (obj.type == isl_obj_pw_qpolynomial &&
1203 type == isl_obj_union_pw_qpolynomial) {
1204 obj.type = isl_obj_union_pw_qpolynomial;
1205 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1206 return obj;
1208 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1209 type == isl_obj_union_pw_qpolynomial_fold) {
1210 obj.type = isl_obj_union_pw_qpolynomial_fold;
1211 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1212 return obj;
1214 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1215 if (type == isl_obj_union_map) {
1216 obj.type = isl_obj_union_map;
1217 return obj;
1219 if (type == isl_obj_union_pw_qpolynomial) {
1220 isl_space *dim = isl_union_set_get_space(obj.v);
1221 isl_union_set_free(obj.v);
1222 obj.v = isl_union_pw_qpolynomial_zero(dim);
1223 obj.type = isl_obj_union_pw_qpolynomial;
1224 return obj;
1226 if (type == isl_obj_union_pw_qpolynomial_fold) {
1227 isl_space *dim = isl_union_set_get_space(obj.v);
1228 isl_union_set_free(obj.v);
1229 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1230 isl_fold_list);
1231 obj.type = isl_obj_union_pw_qpolynomial_fold;
1232 return obj;
1235 if (obj.type == isl_obj_list) {
1236 struct isl_list *list = obj.v;
1237 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1238 return convert(ctx, obj_at(obj, 0), type);
1240 if (type == isl_obj_str) {
1241 isl_str *str;
1242 isl_printer *p;
1243 char *s;
1245 p = isl_printer_to_str(ctx);
1246 if (!p)
1247 goto error;
1248 p = obj.type->print(p, obj.v);
1249 s = isl_printer_get_str(p);
1250 isl_printer_free(p);
1252 str = isl_str_from_string(ctx, s);
1253 if (!str)
1254 goto error;
1255 free_obj(obj);
1256 obj.v = str;
1257 obj.type = isl_obj_str;
1258 return obj;
1261 error:
1262 free_obj(obj);
1263 obj.type = isl_obj_none;
1264 obj.v = NULL;
1265 return obj;
1268 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1269 struct isl_obj lhs)
1271 int i;
1272 int read_tok2 = 0;
1273 struct isl_token *tok, *tok2;
1275 tok = isl_stream_next_token(s);
1276 if (!tok)
1277 return NULL;
1279 for (i = 0; ; ++i) {
1280 if (!bin_ops[i].op)
1281 break;
1282 if (bin_ops[i].op != isl_token_get_type(tok))
1283 continue;
1284 if (!is_subtype(lhs, bin_ops[i].lhs))
1285 continue;
1287 isl_token_free(tok);
1288 return &bin_ops[i];
1291 for (i = 0; ; ++i) {
1292 if (!named_bin_ops[i].name)
1293 break;
1294 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1295 continue;
1296 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1297 continue;
1299 isl_token_free(tok);
1300 return &named_bin_ops[i].op;
1303 for (i = 0; ; ++i) {
1304 if (!compound_bin_ops[i].full)
1305 break;
1306 if (compound_bin_ops[i].op1 != isl_token_get_type(tok))
1307 continue;
1308 if (!read_tok2)
1309 tok2 = isl_stream_next_token(s);
1310 read_tok2 = 1;
1311 if (compound_bin_ops[i].op2 != isl_token_get_type(tok2))
1312 continue;
1313 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
1314 continue;
1316 isl_token_free(tok2);
1317 isl_token_free(tok);
1318 return &compound_bin_ops[i].op;
1321 if (read_tok2)
1322 isl_stream_push_token(s, tok2);
1323 isl_stream_push_token(s, tok);
1325 return NULL;
1328 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1330 int i;
1331 struct isl_token *tok;
1333 tok = isl_stream_next_token(s);
1334 if (!tok)
1335 return NULL;
1337 for (i = 0; ; ++i) {
1338 if (!named_un_ops[i].name)
1339 break;
1340 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1341 continue;
1343 isl_token_free(tok);
1344 return &named_un_ops[i].op;
1347 isl_stream_push_token(s, tok);
1349 return NULL;
1352 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1353 struct isl_obj arg)
1355 int i;
1357 for (i = 0; ; ++i) {
1358 if (!named_un_ops[i].name)
1359 break;
1360 if (named_un_ops[i].op.op != like->op)
1361 continue;
1362 if (!is_subtype(arg, named_un_ops[i].op.arg))
1363 continue;
1365 return &named_un_ops[i].op;
1368 return NULL;
1371 static int is_assign(struct isl_stream *s)
1373 struct isl_token *tok;
1374 struct isl_token *tok2;
1375 int assign;
1377 tok = isl_stream_next_token(s);
1378 if (!tok)
1379 return 0;
1380 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1381 isl_stream_push_token(s, tok);
1382 return 0;
1385 tok2 = isl_stream_next_token(s);
1386 if (!tok2) {
1387 isl_stream_push_token(s, tok);
1388 return 0;
1390 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1391 isl_stream_push_token(s, tok2);
1392 isl_stream_push_token(s, tok);
1394 return assign;
1397 static struct isl_obj read_obj(struct isl_stream *s,
1398 struct isl_hash_table *table);
1399 static struct isl_obj read_expr(struct isl_stream *s,
1400 struct isl_hash_table *table);
1402 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1403 struct isl_hash_table *table, struct isc_un_op *op)
1405 isl_ctx *ctx;
1406 struct isl_obj obj = { isl_obj_none, NULL };
1408 obj = read_obj(s, table);
1409 if (!obj.v)
1410 goto error;
1412 op = find_matching_un_op(op, obj);
1414 ctx = isl_stream_get_ctx(s);
1415 if (!op)
1416 isl_die(ctx, isl_error_invalid,
1417 "no such unary operator defined on given operand",
1418 goto error);
1420 obj = convert(ctx, obj, op->arg);
1421 obj.v = op->fn(obj.v);
1422 obj.type = op->res;
1424 return obj;
1425 error:
1426 free_obj(obj);
1427 obj.type = isl_obj_none;
1428 obj.v = NULL;
1429 return obj;
1432 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1434 struct isl_list *list;
1435 int exact;
1437 if (obj.type != isl_obj_union_map)
1438 obj = convert(ctx, obj, isl_obj_union_map);
1439 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1440 list = isl_list_alloc(ctx, 2);
1441 if (!list)
1442 goto error;
1444 list->obj[0].type = isl_obj_union_map;
1445 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1446 list->obj[1].type = isl_obj_bool;
1447 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1448 obj.v = list;
1449 obj.type = isl_obj_list;
1450 if (exact < 0 || !list->obj[0].v)
1451 goto error;
1453 return obj;
1454 error:
1455 free_obj(obj);
1456 obj.type = isl_obj_none;
1457 obj.v = NULL;
1458 return obj;
1461 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1463 struct isl_list *list = obj.v;
1464 struct isl_token *tok;
1465 isl_ctx *ctx;
1466 isl_val *v;
1467 int i;
1469 tok = isl_stream_next_token(s);
1470 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1471 isl_stream_error(s, tok, "expecting index");
1472 if (tok)
1473 isl_stream_push_token(s, tok);
1474 goto error;
1476 ctx = isl_stream_get_ctx(s);
1477 v = isl_token_get_val(ctx, tok);
1478 i = isl_val_get_num_si(v);
1479 isl_val_free(v);
1480 isl_token_free(tok);
1481 isl_assert(ctx, i < list->n, goto error);
1482 if (isl_stream_eat(s, ']'))
1483 goto error;
1485 return obj_at(obj, i);
1486 error:
1487 free_obj(obj);
1488 obj.type = isl_obj_none;
1489 obj.v = NULL;
1490 return obj;
1493 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1494 struct isl_hash_table *table)
1496 isl_ctx *ctx;
1497 struct isl_obj obj;
1499 obj = read_expr(s, table);
1500 ctx = isl_stream_get_ctx(s);
1501 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1502 is_subtype(obj, isl_obj_union_map), goto error);
1504 if (obj.type == isl_obj_list) {
1505 struct isl_list *list = obj.v;
1506 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1507 obj = obj_at(obj, 0);
1509 if (obj.type == isl_obj_set)
1510 obj = convert(ctx, obj, isl_obj_union_set);
1511 else if (obj.type == isl_obj_map)
1512 obj = convert(ctx, obj, isl_obj_union_map);
1513 if (obj.type == isl_obj_union_set) {
1514 obj.v = isl_union_set_apply(obj.v, umap);
1515 } else
1516 obj.v = isl_union_map_apply_range(obj.v, umap);
1517 if (!obj.v)
1518 goto error2;
1520 if (isl_stream_eat(s, ')'))
1521 goto error2;
1523 return obj;
1524 error:
1525 isl_union_map_free(umap);
1526 error2:
1527 free_obj(obj);
1528 obj.type = isl_obj_none;
1529 obj.v = NULL;
1530 return obj;
1533 static struct isl_obj apply_fun_set(struct isl_obj obj,
1534 __isl_take isl_union_set *uset)
1536 if (obj.type == isl_obj_union_pw_qpolynomial) {
1537 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1538 } else {
1539 obj.type = isl_obj_list;
1540 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1542 return obj;
1545 static struct isl_obj apply_fun_map(struct isl_obj obj,
1546 __isl_take isl_union_map *umap)
1548 if (obj.type == isl_obj_union_pw_qpolynomial) {
1549 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1550 } else {
1551 obj.type = isl_obj_list;
1552 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1554 return obj;
1557 static struct isl_obj apply_fun(struct isl_stream *s,
1558 struct isl_obj obj, struct isl_hash_table *table)
1560 struct isl_obj arg;
1561 isl_ctx *ctx;
1563 arg = read_expr(s, table);
1564 ctx = isl_stream_get_ctx(s);
1565 if (!is_subtype(arg, isl_obj_union_map) &&
1566 !is_subtype(arg, isl_obj_union_set))
1567 isl_die(ctx, isl_error_invalid,
1568 "expecting set of map argument", goto error);
1570 if (arg.type == isl_obj_list) {
1571 struct isl_list *list = arg.v;
1572 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1573 arg = obj_at(arg, 0);
1575 if (arg.type == isl_obj_set)
1576 arg = convert(ctx, arg, isl_obj_union_set);
1577 else if (arg.type == isl_obj_map)
1578 arg = convert(ctx, arg, isl_obj_union_map);
1579 if (arg.type == isl_obj_union_set)
1580 obj = apply_fun_set(obj, arg.v);
1581 else
1582 obj = apply_fun_map(obj, arg.v);
1583 if (!obj.v)
1584 goto error2;
1586 if (isl_stream_eat(s, ')'))
1587 goto error2;
1589 return obj;
1590 error:
1591 free_obj(arg);
1592 error2:
1593 free_obj(obj);
1594 obj.type = isl_obj_none;
1595 obj.v = NULL;
1596 return obj;
1599 struct add_vertex_data {
1600 struct isl_list *list;
1601 int i;
1604 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1606 struct add_vertex_data *data = (struct add_vertex_data *)user;
1607 isl_multi_aff *ma;
1608 isl_set *dom;
1610 ma = isl_vertex_get_expr(vertex);
1611 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1613 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1614 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1615 data->i++;
1617 isl_vertex_free(vertex);
1619 return isl_stat_ok;
1622 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1624 isl_ctx *ctx;
1625 isl_basic_set *hull;
1626 isl_vertices *vertices = NULL;
1627 struct isl_list *list = NULL;
1628 isl_stat r;
1629 struct add_vertex_data *data = (struct add_vertex_data *)user;
1631 set = isl_set_remove_divs(set);
1632 hull = isl_set_convex_hull(set);
1633 vertices = isl_basic_set_compute_vertices(hull);
1634 isl_basic_set_free(hull);
1636 list = data->list;
1638 ctx = isl_vertices_get_ctx(vertices);
1639 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1640 if (!data->list)
1641 goto error;
1643 data->i = 0;
1644 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1646 data->list = isl_list_concat(list, data->list);
1648 isl_vertices_free(vertices);
1650 return r;
1651 error:
1652 data->list = list;
1653 isl_vertices_free(vertices);
1654 return isl_stat_error;
1657 static struct isl_obj vertices(struct isl_stream *s,
1658 struct isl_hash_table *table)
1660 isl_ctx *ctx;
1661 struct isl_obj obj;
1662 struct isl_list *list = NULL;
1663 isl_union_set *uset = NULL;
1664 struct add_vertex_data data = { NULL };
1666 obj = read_expr(s, table);
1667 ctx = isl_stream_get_ctx(s);
1668 obj = convert(ctx, obj, isl_obj_union_set);
1669 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1670 uset = obj.v;
1671 obj.v = NULL;
1673 list = isl_list_alloc(ctx, 0);
1674 if (!list)
1675 goto error;
1677 data.list = list;
1679 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1680 goto error;
1682 isl_union_set_free(uset);
1684 obj.type = isl_obj_list;
1685 obj.v = data.list;
1687 return obj;
1688 error:
1689 isl_union_set_free(uset);
1690 isl_list_free(data.list);
1691 free_obj(obj);
1692 obj.type = isl_obj_none;
1693 obj.v = NULL;
1694 return obj;
1697 static struct isl_obj type_of(struct isl_stream *s,
1698 struct isl_hash_table *table)
1700 struct isl_obj obj;
1701 const char *type = "unknown";
1703 obj = read_expr(s, table);
1705 if (obj.type == isl_obj_map ||
1706 obj.type == isl_obj_union_map)
1707 type = "map";
1708 if (obj.type == isl_obj_set ||
1709 obj.type == isl_obj_union_set)
1710 type = "set";
1711 if (obj.type == isl_obj_pw_multi_aff)
1712 type = "piecewise multi-quasiaffine expression";
1713 if (obj.type == isl_obj_pw_qpolynomial ||
1714 obj.type == isl_obj_union_pw_qpolynomial)
1715 type = "piecewise quasipolynomial";
1716 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1717 obj.type == isl_obj_union_pw_qpolynomial_fold)
1718 type = "piecewise quasipolynomial fold";
1719 if (obj.type == isl_obj_list)
1720 type = "list";
1721 if (obj.type == isl_obj_bool)
1722 type = "boolean";
1723 if (obj.type == isl_obj_str)
1724 type = "string";
1725 if (obj.type == isl_obj_val)
1726 type = "value";
1727 if (obj.type == isl_obj_schedule)
1728 type = "schedule";
1730 free_obj(obj);
1731 obj.type = isl_obj_str;
1732 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1734 return obj;
1737 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1738 struct isl_hash_table *table)
1740 struct isl_obj obj;
1741 isl_ctx *ctx;
1743 obj = read_obj(s, table);
1744 ctx = isl_stream_get_ctx(s);
1745 obj = convert(ctx, obj, isl_obj_union_set);
1746 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1747 return obj.v;
1748 error:
1749 free_obj(obj);
1750 return NULL;
1753 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1754 struct isl_hash_table *table)
1756 struct isl_obj obj;
1757 isl_ctx *ctx;
1759 obj = read_obj(s, table);
1760 ctx = isl_stream_get_ctx(s);
1761 obj = convert(ctx, obj, isl_obj_union_map);
1762 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1763 return obj.v;
1764 error:
1765 free_obj(obj);
1766 return NULL;
1769 /* Read a schedule in the form of either a schedule (tree) or a union map
1770 * from "s" and store the schedule in "access".
1772 static __isl_give isl_union_access_info *access_info_set_schedule(
1773 __isl_take isl_union_access_info *access, struct isl_stream *s,
1774 struct isl_hash_table *table)
1776 struct isl_obj obj;
1777 isl_ctx *ctx;
1779 obj = read_obj(s, table);
1780 if (obj.type == isl_obj_schedule)
1781 return isl_union_access_info_set_schedule(access, obj.v);
1782 ctx = isl_stream_get_ctx(s);
1783 obj = convert(ctx, obj, isl_obj_union_map);
1785 return isl_union_access_info_set_schedule_map(access, obj.v);
1788 static struct isl_obj last_any(struct isl_stream *s,
1789 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1790 __isl_take isl_union_map *may_source)
1792 struct isl_obj obj = { isl_obj_none, NULL };
1793 isl_union_access_info *access;
1794 isl_union_flow *flow;
1795 isl_union_map *sink = NULL;
1796 isl_union_map *may_dep;
1798 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1799 goto error;
1801 sink = read_map(s, table);
1802 if (!sink)
1803 goto error;
1805 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1806 goto error;
1808 access = isl_union_access_info_from_sink(sink);
1809 access = isl_union_access_info_set_must_source(access, must_source);
1810 access = isl_union_access_info_set_may_source(access, may_source);
1811 access = access_info_set_schedule(access, s, table);
1812 flow = isl_union_access_info_compute_flow(access);
1813 may_dep = isl_union_flow_get_may_dependence(flow);
1814 isl_union_flow_free(flow);
1816 if (!may_dep)
1817 return obj;
1819 obj.type = isl_obj_union_map;
1820 obj.v = may_dep;
1822 return obj;
1823 error:
1824 isl_union_map_free(may_source);
1825 isl_union_map_free(must_source);
1826 isl_union_map_free(sink);
1827 free_obj(obj);
1828 obj.type = isl_obj_none;
1829 obj.v = NULL;
1830 return obj;
1833 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1835 struct isl_obj obj = { isl_obj_none, NULL };
1836 isl_union_access_info *access;
1837 isl_union_flow *flow;
1838 isl_union_map *may_source = NULL;
1839 isl_union_map *sink = NULL;
1840 isl_union_map *may_dep;
1842 may_source = read_map(s, table);
1843 if (!may_source)
1844 goto error;
1846 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1847 isl_union_map *must_source;
1848 must_source = read_map(s, table);
1849 if (!must_source)
1850 goto error;
1851 return last_any(s, table, must_source, may_source);
1854 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1855 goto error;
1857 sink = read_map(s, table);
1858 if (!sink)
1859 goto error;
1861 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1862 goto error;
1864 access = isl_union_access_info_from_sink(sink);
1865 access = isl_union_access_info_set_may_source(access, may_source);
1866 access = access_info_set_schedule(access, s, table);
1867 flow = isl_union_access_info_compute_flow(access);
1868 may_dep = isl_union_flow_get_may_dependence(flow);
1869 isl_union_flow_free(flow);
1871 if (!may_dep)
1872 return obj;
1874 obj.type = isl_obj_union_map;
1875 obj.v = may_dep;
1877 return obj;
1878 error:
1879 isl_union_map_free(may_source);
1880 isl_union_map_free(sink);
1881 free_obj(obj);
1882 obj.type = isl_obj_none;
1883 obj.v = NULL;
1884 return obj;
1887 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1889 struct isl_obj obj = { isl_obj_none, NULL };
1890 struct isl_list *list = NULL;
1891 isl_union_access_info *access;
1892 isl_union_flow *flow;
1893 isl_union_map *must_source = NULL;
1894 isl_union_map *sink = NULL;
1895 isl_union_map *must_dep;
1896 isl_union_map *must_no_source;
1898 must_source = read_map(s, table);
1899 if (!must_source)
1900 goto error;
1902 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1903 isl_union_map *may_source;
1904 may_source = read_map(s, table);
1905 if (!may_source)
1906 goto error;
1907 return last_any(s, table, must_source, may_source);
1910 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1911 if (!list)
1912 goto error;
1914 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1915 goto error;
1917 sink = read_map(s, table);
1918 if (!sink)
1919 goto error;
1921 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1922 goto error;
1924 access = isl_union_access_info_from_sink(sink);
1925 access = isl_union_access_info_set_must_source(access, must_source);
1926 access = access_info_set_schedule(access, s, table);
1927 flow = isl_union_access_info_compute_flow(access);
1928 must_dep = isl_union_flow_get_must_dependence(flow);
1929 must_no_source = isl_union_flow_get_must_no_source(flow);
1930 isl_union_flow_free(flow);
1932 list->obj[0].type = isl_obj_union_map;
1933 list->obj[0].v = must_dep;
1934 list->obj[1].type = isl_obj_union_map;
1935 list->obj[1].v = must_no_source;
1937 if (!must_dep || !must_no_source) {
1938 isl_list_free(list);
1939 return obj;
1942 obj.v = list;
1943 obj.type = isl_obj_list;
1945 return obj;
1946 error:
1947 isl_list_free(list);
1948 isl_union_map_free(must_source);
1949 isl_union_map_free(sink);
1950 free_obj(obj);
1951 obj.type = isl_obj_none;
1952 obj.v = NULL;
1953 return obj;
1956 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1957 struct isl_hash_table *table)
1959 isl_union_set *domain;
1960 isl_union_map *validity;
1961 isl_union_map *proximity;
1963 domain = read_set(s, table);
1964 if (!domain)
1965 return NULL;
1967 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1968 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1970 for (;;) {
1971 isl_union_map *umap;
1972 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1973 umap = read_map(s, table);
1974 validity = isl_union_map_union(validity, umap);
1975 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1976 umap = read_map(s, table);
1977 proximity = isl_union_map_union(proximity, umap);
1978 } else
1979 break;
1982 return isl_union_set_compute_schedule(domain, validity, proximity);
1985 static struct isl_obj schedule(struct isl_stream *s,
1986 struct isl_hash_table *table)
1988 struct isl_obj obj = { isl_obj_none, NULL };
1989 isl_schedule *schedule;
1991 schedule = get_schedule(s, table);
1993 obj.v = schedule;
1994 obj.type = isl_obj_schedule;
1996 return obj;
1999 /* Read a schedule for code generation in the form of either
2000 * a schedule tree or a union map.
2001 * If the input is a set rather than a map, then we construct
2002 * an identity union map schedule on the given set.
2004 static struct isl_obj get_codegen_schedule(struct isl_stream *s,
2005 struct isl_hash_table *table)
2007 struct isl_obj obj;
2008 isl_ctx *ctx;
2010 obj = read_obj(s, table);
2011 ctx = isl_stream_get_ctx(s);
2013 if (obj.type == isl_obj_schedule)
2014 return obj;
2015 if (is_subtype(obj, isl_obj_union_set)) {
2016 obj = convert(ctx, obj, isl_obj_union_set);
2017 obj.v = isl_union_set_identity(obj.v);
2018 obj.type = isl_obj_union_map;
2020 if (is_subtype(obj, isl_obj_union_map))
2021 return convert(ctx, obj, isl_obj_union_map);
2023 free_obj(obj);
2024 obj.v = NULL;
2025 obj.type = isl_obj_none;
2026 isl_die(ctx, isl_error_invalid, "expecting schedule, set or map",
2027 return obj);
2030 /* Generate an AST for the given schedule and options and return the AST.
2032 static __isl_give isl_ast_node *get_ast_from_union_map(
2033 __isl_take isl_union_map *schedule, __isl_take isl_union_map *options)
2035 isl_space *space;
2036 isl_set *context;
2037 isl_ast_build *build;
2038 isl_ast_node *tree;
2040 space = isl_union_map_get_space(schedule);
2041 context = isl_set_universe(isl_space_params(space));
2043 build = isl_ast_build_from_context(context);
2044 build = isl_ast_build_set_options(build, options);
2045 tree = isl_ast_build_ast_from_schedule(build, schedule);
2046 isl_ast_build_free(build);
2048 return tree;
2051 /* Generate an AST for the given schedule and return the AST.
2053 static __isl_give isl_ast_node *get_ast_from_schedule(
2054 __isl_take isl_schedule *schedule)
2056 isl_ast_build *build;
2057 isl_ast_node *tree;
2059 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
2060 tree = isl_ast_build_node_from_schedule(build, schedule);
2061 isl_ast_build_free(build);
2063 return tree;
2066 /* Print the AST "tree" on the printer "p".
2068 static __isl_give isl_printer *print_ast(__isl_take isl_printer *p,
2069 __isl_take isl_ast_node *tree)
2071 int format;
2073 format = isl_printer_get_output_format(p);
2074 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2075 p = isl_printer_print_ast_node(p, tree);
2076 p = isl_printer_set_output_format(p, format);
2078 isl_ast_node_free(tree);
2080 return p;
2083 /* Perform the codegen operation.
2084 * In particular, read a schedule, check if the user has specified any options
2085 * and then generate an AST from the schedule (and options) and print it.
2086 * In case the schedule is specified as a schedule tree, the AST generation
2087 * options are embedded in the schedule, so they are not read in separately.
2089 static __isl_give isl_printer *codegen(struct isl_stream *s,
2090 struct isl_hash_table *table, __isl_take isl_printer *p)
2092 struct isl_obj obj;
2093 isl_ast_node *tree;
2095 obj = get_codegen_schedule(s, table);
2096 if (!obj.v)
2097 return p;
2099 if (obj.type == isl_obj_schedule) {
2100 isl_schedule *schedule = obj.v;
2102 tree = get_ast_from_schedule(schedule);
2103 } else {
2104 isl_union_map *schedule = obj.v;
2105 isl_union_map *options;
2107 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
2108 options = read_map(s, table);
2109 else
2110 options = isl_union_map_empty(
2111 isl_union_map_get_space(schedule));
2113 tree = get_ast_from_union_map(schedule, options);
2116 if (tree)
2117 p = print_ast(p, tree);
2119 isl_stream_eat(s, ';');
2121 return p;
2124 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2126 struct isl_token *tok;
2127 isl_ctx *ctx;
2128 isl_val *v;
2130 ctx = isl_stream_get_ctx(s);
2131 if (isl_stream_eat_if_available(s, '+'))
2132 return transitive_closure(ctx, obj);
2134 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2135 if (obj.type != isl_obj_union_map)
2136 obj = convert(ctx, obj, isl_obj_union_map);
2138 tok = isl_stream_next_token(s);
2139 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2140 isl_stream_error(s, tok, "expecting integer exponent");
2141 if (tok)
2142 isl_stream_push_token(s, tok);
2143 goto error;
2146 v = isl_token_get_val(ctx, tok);
2147 if (isl_val_is_zero(v)) {
2148 isl_stream_error(s, tok, "expecting non-zero exponent");
2149 isl_val_free(v);
2150 if (tok)
2151 isl_stream_push_token(s, tok);
2152 goto error;
2155 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2156 isl_token_free(tok);
2157 if (!obj.v)
2158 goto error;
2160 return obj;
2161 error:
2162 free_obj(obj);
2163 obj.type = isl_obj_none;
2164 obj.v = NULL;
2165 return obj;
2168 static struct isl_obj check_assert(struct isl_stream *s,
2169 struct isl_hash_table *table)
2171 struct isl_obj obj;
2172 isl_ctx *ctx;
2174 obj = read_expr(s, table);
2175 ctx = isl_stream_get_ctx(s);
2176 if (obj.type != isl_obj_bool)
2177 isl_die(ctx, isl_error_invalid,
2178 "expecting boolean expression", goto error);
2179 if (obj.v != &iscc_bool_true)
2180 isl_die(ctx, isl_error_unknown,
2181 "assertion failed", abort());
2182 error:
2183 free_obj(obj);
2184 obj.type = isl_obj_none;
2185 obj.v = NULL;
2186 return obj;
2189 static struct isl_obj read_from_file(struct isl_stream *s)
2191 isl_ctx *ctx;
2192 struct isl_obj obj;
2193 struct isl_token *tok;
2194 struct isl_stream *s_file;
2195 struct iscc_options *options;
2196 char *name;
2197 FILE *file;
2199 tok = isl_stream_next_token(s);
2200 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2201 isl_stream_error(s, tok, "expecting filename");
2202 isl_token_free(tok);
2203 goto error;
2206 ctx = isl_stream_get_ctx(s);
2207 options = isl_ctx_peek_iscc_options(ctx);
2208 if (!options || !options->io) {
2209 isl_token_free(tok);
2210 isl_die(ctx, isl_error_invalid,
2211 "read operation not allowed", goto error);
2214 name = isl_token_get_str(ctx, tok);
2215 isl_token_free(tok);
2216 file = fopen(name, "r");
2217 free(name);
2218 isl_assert(ctx, file, goto error);
2220 s_file = isl_stream_new_file(ctx, file);
2221 if (!s_file) {
2222 fclose(file);
2223 goto error;
2226 obj = isl_stream_read_obj(s_file);
2228 isl_stream_free(s_file);
2229 fclose(file);
2231 return obj;
2232 error:
2233 obj.type = isl_obj_none;
2234 obj.v = NULL;
2235 return obj;
2238 static struct isl_obj write_to_file(struct isl_stream *s,
2239 struct isl_hash_table *table)
2241 struct isl_obj obj = { isl_obj_none, NULL };
2242 struct isl_token *tok;
2243 struct iscc_options *options;
2244 char *name;
2245 FILE *file;
2246 isl_ctx *ctx;
2247 isl_printer *p;
2249 tok = isl_stream_next_token(s);
2250 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2251 isl_stream_error(s, tok, "expecting filename");
2252 isl_token_free(tok);
2253 goto error;
2256 obj = read_expr(s, table);
2258 ctx = isl_stream_get_ctx(s);
2259 options = isl_ctx_peek_iscc_options(ctx);
2260 if (!options || !options->io) {
2261 isl_token_free(tok);
2262 isl_die(ctx, isl_error_invalid,
2263 "write operation not allowed", goto error);
2266 name = isl_token_get_str(ctx, tok);
2267 isl_token_free(tok);
2268 file = fopen(name, "w");
2269 free(name);
2270 if (!file)
2271 isl_die(ctx, isl_error_unknown,
2272 "could not open file for writing", goto error);
2274 p = isl_printer_to_file(ctx, file);
2275 p = isl_printer_set_output_format(p, options->format);
2276 p = obj.type->print(p, obj.v);
2277 p = isl_printer_end_line(p);
2278 isl_printer_free(p);
2280 fclose(file);
2281 error:
2282 free_obj(obj);
2283 obj.type = isl_obj_none;
2284 obj.v = NULL;
2285 return obj;
2288 static struct isl_obj read_string_if_available(struct isl_stream *s)
2290 struct isl_token *tok;
2291 struct isl_obj obj = { isl_obj_none, NULL };
2293 tok = isl_stream_next_token(s);
2294 if (!tok)
2295 return obj;
2296 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2297 isl_str *str;
2298 str = isl_str_alloc(isl_stream_get_ctx(s));
2299 if (!str)
2300 goto error;
2301 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2302 isl_token_free(tok);
2303 obj.v = str;
2304 obj.type = isl_obj_str;
2305 } else
2306 isl_stream_push_token(s, tok);
2307 return obj;
2308 error:
2309 isl_token_free(tok);
2310 return obj;
2313 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2315 struct isl_token *tok;
2316 struct isl_obj obj = { isl_obj_none, NULL };
2317 int type;
2319 tok = isl_stream_next_token(s);
2320 if (!tok)
2321 return obj;
2322 type = isl_token_get_type(tok);
2323 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2324 int is_true = type == ISL_TOKEN_TRUE;
2325 isl_token_free(tok);
2326 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2327 obj.type = isl_obj_bool;
2328 } else
2329 isl_stream_push_token(s, tok);
2330 return obj;
2333 static __isl_give char *read_ident(struct isl_stream *s)
2335 char *name;
2336 isl_val *v;
2337 struct isl_token *tok, *tok2;
2339 name = isl_stream_read_ident_if_available(s);
2340 if (name)
2341 return name;
2343 tok = isl_stream_next_token(s);
2344 if (!tok)
2345 return NULL;
2346 if (isl_token_get_type(tok) != '$') {
2347 isl_stream_push_token(s, tok);
2348 return NULL;
2350 tok2 = isl_stream_next_token(s);
2351 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2352 if (tok2)
2353 isl_stream_push_token(s, tok2);
2354 isl_stream_push_token(s, tok);
2355 return NULL;
2358 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2359 name = isl_val_to_str(v);
2360 isl_val_free(v);
2361 isl_token_free(tok);
2362 isl_token_free(tok2);
2364 return name;
2367 static struct isl_obj read_list(struct isl_stream *s,
2368 struct isl_hash_table *table, struct isl_obj obj)
2370 struct isl_list *list;
2372 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2373 if (!list)
2374 goto error;
2375 list->obj[0] = obj;
2376 list->obj[1] = read_obj(s, table);
2377 obj.v = list;
2378 obj.type = isl_obj_list;
2380 if (!list->obj[1].v)
2381 goto error;
2383 while (isl_stream_eat_if_available(s, ',')) {
2384 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2385 if (!obj.v)
2386 goto error;
2389 return obj;
2390 error:
2391 free_obj(obj);
2392 obj.type = isl_obj_none;
2393 obj.v = NULL;
2394 return obj;
2397 static struct isl_obj read_obj(struct isl_stream *s,
2398 struct isl_hash_table *table)
2400 isl_ctx *ctx;
2401 struct isl_obj obj = { isl_obj_none, NULL };
2402 char *name = NULL;
2403 struct isc_un_op *op = NULL;
2405 obj = read_string_if_available(s);
2406 if (obj.v)
2407 return obj;
2408 obj = read_bool_if_available(s);
2409 if (obj.v)
2410 return obj;
2411 ctx = isl_stream_get_ctx(s);
2412 if (isl_stream_eat_if_available(s, '(')) {
2413 if (isl_stream_next_token_is(s, ')')) {
2414 obj.type = isl_obj_list;
2415 obj.v = isl_list_alloc(ctx, 0);
2416 } else {
2417 obj = read_expr(s, table);
2418 if (obj.v && isl_stream_eat_if_available(s, ','))
2419 obj = read_list(s, table, obj);
2421 if (!obj.v || isl_stream_eat(s, ')'))
2422 goto error;
2423 } else {
2424 op = read_prefix_un_op_if_available(s);
2425 if (op)
2426 return read_un_op_expr(s, table, op);
2428 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2429 return check_assert(s, table);
2430 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2431 return read_from_file(s);
2432 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2433 return write_to_file(s, table);
2434 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2435 return vertices(s, table);
2436 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2437 return any(s, table);
2438 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2439 return last(s, table);
2440 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2441 return schedule(s, table);
2442 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2443 return type_of(s, table);
2445 name = read_ident(s);
2446 if (name)
2447 obj = stored_obj(ctx, table, name);
2448 else
2449 obj = isl_stream_read_obj(s);
2450 if (!obj.v)
2451 goto error;
2454 if (isl_stream_eat_if_available(s, '^'))
2455 obj = power(s, obj);
2456 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2457 obj = obj_at_index(s, obj);
2458 else if (is_subtype(obj, isl_obj_union_map) &&
2459 isl_stream_eat_if_available(s, '(')) {
2460 obj = convert(ctx, obj, isl_obj_union_map);
2461 obj = apply(s, obj.v, table);
2462 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2463 isl_stream_eat_if_available(s, '(')) {
2464 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2465 obj = apply_fun(s, obj, table);
2466 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2467 isl_stream_eat_if_available(s, '(')) {
2468 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2469 obj = apply_fun(s, obj, table);
2472 return obj;
2473 error:
2474 free_obj(obj);
2475 obj.type = isl_obj_none;
2476 obj.v = NULL;
2477 return obj;
2480 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2481 struct isl_obj lhs, struct isl_obj rhs)
2483 int i;
2485 for (i = 0; ; ++i) {
2486 if (!bin_ops[i].op)
2487 break;
2488 if (bin_ops[i].op != like->op)
2489 continue;
2490 if (!is_subtype(lhs, bin_ops[i].lhs))
2491 continue;
2492 if (!is_subtype(rhs, bin_ops[i].rhs))
2493 continue;
2495 return &bin_ops[i];
2498 for (i = 0; ; ++i) {
2499 if (!named_bin_ops[i].name)
2500 break;
2501 if (named_bin_ops[i].op.op != like->op)
2502 continue;
2503 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2504 continue;
2505 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2506 continue;
2508 return &named_bin_ops[i].op;
2511 for (i = 0; ; ++i) {
2512 if (!compound_bin_ops[i].full)
2513 break;
2514 if (compound_bin_ops[i].op.op != like->op)
2515 continue;
2516 if (!is_subtype(lhs, compound_bin_ops[i].op.lhs))
2517 continue;
2518 if (!is_subtype(rhs, compound_bin_ops[i].op.rhs))
2519 continue;
2521 return &compound_bin_ops[i].op;
2524 return NULL;
2527 static int next_is_neg_int(struct isl_stream *s)
2529 struct isl_token *tok;
2530 int ret;
2532 tok = isl_stream_next_token(s);
2533 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2534 isl_val *v;
2535 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2536 ret = isl_val_is_neg(v);
2537 isl_val_free(v);
2538 } else
2539 ret = 0;
2540 isl_stream_push_token(s, tok);
2542 return ret;
2545 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2546 struct isl_obj lhs, struct isl_obj rhs)
2548 struct isl_obj obj;
2550 lhs = convert(ctx, lhs, op->lhs);
2551 rhs = convert(ctx, rhs, op->rhs);
2552 if (op->res != isl_obj_bool)
2553 obj.v = op->o.fn(lhs.v, rhs.v);
2554 else {
2555 int res = op->o.test(lhs.v, rhs.v);
2556 free_obj(lhs);
2557 free_obj(rhs);
2558 obj.v = iscc_bool_from_int(res);
2560 obj.type = op->res;
2562 return obj;
2565 static struct isl_obj read_expr(struct isl_stream *s,
2566 struct isl_hash_table *table)
2568 isl_ctx *ctx;
2569 struct isl_obj obj = { isl_obj_none, NULL };
2570 struct isl_obj right_obj = { isl_obj_none, NULL };
2572 obj = read_obj(s, table);
2573 ctx = isl_stream_get_ctx(s);
2574 for (; obj.v;) {
2575 struct isc_bin_op *op = NULL;
2577 op = read_bin_op_if_available(s, obj);
2578 if (!op)
2579 break;
2581 right_obj = read_obj(s, table);
2583 op = find_matching_bin_op(op, obj, right_obj);
2585 if (!op)
2586 isl_die(ctx, isl_error_invalid,
2587 "no such binary operator defined on given operands",
2588 goto error);
2590 obj = call_bin_op(ctx, op, obj, right_obj);
2593 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2594 right_obj = read_obj(s, table);
2595 obj.v = isl_val_add(obj.v, right_obj.v);
2598 return obj;
2599 error:
2600 free_obj(right_obj);
2601 free_obj(obj);
2602 obj.type = isl_obj_none;
2603 obj.v = NULL;
2604 return obj;
2607 static __isl_give isl_printer *source_file(struct isl_stream *s,
2608 struct isl_hash_table *table, __isl_take isl_printer *p);
2610 /* Print "obj" to the printer "p".
2611 * If the object is a schedule, then print it in block format.
2613 static __isl_give isl_printer *print_obj(__isl_take isl_printer *p,
2614 struct isl_obj obj)
2616 if (obj.type != isl_obj_schedule)
2617 return obj.type->print(p, obj.v);
2619 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
2620 p = obj.type->print(p, obj.v);
2621 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2623 return p;
2626 static __isl_give isl_printer *read_line(struct isl_stream *s,
2627 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2629 isl_ctx *ctx;
2630 struct isl_obj obj = { isl_obj_none, NULL };
2631 char *lhs = NULL;
2632 int assign = 0;
2633 int only_print = 0;
2634 char buf[30];
2636 if (!p)
2637 return NULL;
2638 if (isl_stream_is_empty(s))
2639 return p;
2641 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2642 return source_file(s, table, p);
2643 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2644 return codegen(s, table, p);
2646 assign = is_assign(s);
2647 if (assign) {
2648 lhs = isl_stream_read_ident_if_available(s);
2649 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2650 goto error;
2651 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2652 only_print = 1;
2653 else if (!tty)
2654 only_print = 1;
2656 obj = read_expr(s, table);
2657 ctx = isl_stream_get_ctx(s);
2658 if (isl_stream_eat(s, ';'))
2659 goto error;
2661 if (only_print) {
2662 if (obj.type != isl_obj_none && obj.v != NULL) {
2663 p = print_obj(p, obj);
2664 p = isl_printer_end_line(p);
2666 free_obj(obj);
2667 return p;
2669 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2670 static int count = 0;
2671 snprintf(buf, sizeof(buf), "$%d", count++);
2672 lhs = strdup(buf + 1);
2674 p = isl_printer_print_str(p, buf);
2675 p = isl_printer_print_str(p, " := ");
2676 p = obj.type->print(p, obj.v);
2677 p = isl_printer_end_line(p);
2679 if (lhs && do_assign(ctx, table, lhs, obj))
2680 return p;
2682 return p;
2683 error:
2684 isl_stream_flush_tokens(s);
2685 isl_stream_skip_line(s);
2686 free(lhs);
2687 free_obj(obj);
2688 return p;
2691 static isl_stat free_cb(void **entry, void *user)
2693 struct isl_named_obj *named = *entry;
2695 free_obj(named->obj);
2696 free(named->name);
2697 free(named);
2699 return isl_stat_ok;
2702 static void register_named_ops(struct isl_stream *s)
2704 int i;
2706 for (i = 0; i < ISCC_N_OP; ++i) {
2707 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2708 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2711 for (i = 0; ; ++i) {
2712 if (!named_un_ops[i].name)
2713 break;
2714 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2715 named_un_ops[i].name);
2716 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2719 for (i = 0; ; ++i) {
2720 if (!named_bin_ops[i].name)
2721 break;
2722 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2723 named_bin_ops[i].name);
2724 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2727 for (i = 0; ; ++i) {
2728 if (!compound_bin_ops[i].full)
2729 break;
2730 compound_bin_ops[i].op.op = isl_stream_register_keyword(s,
2731 compound_bin_ops[i].full);
2732 assert(compound_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2736 static __isl_give isl_printer *source_file(struct isl_stream *s,
2737 struct isl_hash_table *table, __isl_take isl_printer *p)
2739 isl_ctx *ctx;
2740 struct isl_token *tok;
2741 struct isl_stream *s_file;
2742 struct iscc_options *options;
2743 char *name;
2744 FILE *file;
2746 tok = isl_stream_next_token(s);
2747 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2748 isl_stream_error(s, tok, "expecting filename");
2749 isl_token_free(tok);
2750 return p;
2753 isl_stream_eat(s, ';');
2755 ctx = isl_stream_get_ctx(s);
2756 options = isl_ctx_peek_iscc_options(ctx);
2757 if (!options || !options->io) {
2758 isl_token_free(tok);
2759 isl_die(ctx, isl_error_invalid,
2760 "source operation not allowed", return p);
2763 name = isl_token_get_str(ctx, tok);
2764 isl_token_free(tok);
2765 file = fopen(name, "r");
2766 free(name);
2767 isl_assert(ctx, file, return p);
2769 s_file = isl_stream_new_file(ctx, file);
2770 if (!s_file) {
2771 fclose(file);
2772 return p;
2775 register_named_ops(s_file);
2777 while (!isl_stream_is_empty(s_file))
2778 p = read_line(s_file, table, p, 0);
2780 isl_stream_free(s_file);
2781 fclose(file);
2783 return p;
2786 int main(int argc, char **argv)
2788 struct isl_ctx *ctx;
2789 struct isl_stream *s;
2790 struct isl_hash_table *table;
2791 struct iscc_options *options;
2792 isl_printer *p;
2793 int tty = isatty(0);
2795 options = iscc_options_new_with_defaults();
2796 assert(options);
2798 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2799 pet_options_set_autodetect(ctx, 1);
2800 pet_options_set_encapsulate_dynamic_control(ctx, 1);
2801 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2802 s = isl_stream_new_file(ctx, stdin);
2803 assert(s);
2804 table = isl_hash_table_alloc(ctx, 10);
2805 assert(table);
2806 p = isl_printer_to_file(ctx, stdout);
2807 p = isl_printer_set_output_format(p, options->format);
2808 assert(p);
2810 register_named_ops(s);
2812 install_signal_handler(ctx);
2814 while (p) {
2815 int empty;
2817 empty = isl_stream_is_empty(s);
2818 if (empty && isl_ctx_last_error(ctx) != isl_error_abort)
2819 break;
2820 if (!empty)
2821 p = read_line(s, table, p, tty);
2822 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2823 fprintf(stderr, "Interrupted\n");
2824 isl_ctx_resume(ctx);
2825 isl_ctx_reset_error(ctx);
2829 remove_signal_handler(ctx);
2831 isl_printer_free(p);
2832 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2833 isl_hash_table_free(ctx, table);
2834 isl_stream_free(s);
2835 isl_ctx_free(ctx);
2837 return 0;