iscc: use isl_ctx_parse_options
[barvinok.git] / iscc.c
blob59a45b5e7caf0e13f75994bcbe790889c12993e1
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/obj.h>
7 #include <isl/stream.h>
8 #include <isl/vertices.h>
9 #include <isl/flow.h>
10 #include <isl/band.h>
11 #include <isl/schedule.h>
12 #include <isl_obj_list.h>
13 #include <isl_obj_str.h>
14 #include <barvinok/isl.h>
15 #include <barvinok/options.h>
16 #include "lattice_width.h"
18 #include "config.h"
20 #ifdef HAVE_SIGACTION
21 #include <signal.h>
23 static isl_ctx *main_ctx;
25 static void handler(int signum)
27 if (isl_ctx_aborted(main_ctx))
28 exit(EXIT_FAILURE);
29 isl_ctx_abort(main_ctx);
32 static struct sigaction sa_old;
34 static void install_signal_handler(isl_ctx *ctx)
36 struct sigaction sa;
38 main_ctx = ctx;
40 memset(&sa, 0, sizeof(struct sigaction));
41 sa.sa_handler = &handler;
42 sa.sa_flags = SA_RESTART;
43 sigaction(SIGINT, &sa, &sa_old);
46 static void remove_signal_handler(isl_ctx *ctx)
48 sigaction(SIGINT, &sa_old, NULL);
51 #else
53 static void install_signal_handler(isl_ctx *ctx)
57 static void remove_signal_handler(isl_ctx *ctx)
61 #endif
63 #ifdef HAVE_CLOOG
64 #include <cloog/isl/cloog.h>
65 #endif
67 #ifdef HAVE_PET
68 #include <pet.h>
69 #endif
71 static int isl_bool_false = 0;
72 static int isl_bool_true = 1;
73 static int isl_bool_error = -1;
75 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
76 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
77 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
78 ISCC_MINIMIZING, ISCC_RESPECTING,
79 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
80 ISCC_N_OP };
81 static const char *op_name[ISCC_N_OP] = {
82 [ISCC_ASSERT] = "assert",
83 [ISCC_READ] = "read",
84 [ISCC_WRITE] = "write",
85 [ISCC_PRINT] = "print",
86 [ISCC_SOURCE] = "source",
87 [ISCC_VERTICES] = "vertices",
88 [ISCC_LAST] = "last",
89 [ISCC_ANY] = "any",
90 [ISCC_BEFORE] = "before",
91 [ISCC_UNDER] = "under",
92 [ISCC_SCHEDULE] = "schedule",
93 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
94 [ISCC_MINIMIZING] = "minimizing",
95 [ISCC_RESPECTING] = "respecting",
96 [ISCC_TYPEOF] = "typeof"
98 static enum isl_token_type iscc_op[ISCC_N_OP];
100 struct isl_arg_choice iscc_format[] = {
101 {"isl", ISL_FORMAT_ISL},
102 {"omega", ISL_FORMAT_OMEGA},
103 {"polylib", ISL_FORMAT_POLYLIB},
104 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
105 {"latex", ISL_FORMAT_LATEX},
106 {"C", ISL_FORMAT_C},
110 struct iscc_options {
111 struct barvinok_options *barvinok;
112 unsigned format;
113 int io;
116 ISL_ARGS_START(struct iscc_options, iscc_options_args)
117 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
118 "barvinok options")
119 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
120 iscc_format, ISL_FORMAT_ISL, "output format")
121 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
122 "allow read and write operations")
123 ISL_ARGS_END
125 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
126 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
128 static void *isl_obj_bool_copy(void *v)
130 return v;
133 static void isl_obj_bool_free(void *v)
137 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
138 void *v)
140 if (v == &isl_bool_true)
141 return isl_printer_print_str(p, "True");
142 else if (v == &isl_bool_false)
143 return isl_printer_print_str(p, "False");
144 else
145 return isl_printer_print_str(p, "Error");
148 static void *isl_obj_bool_add(void *v1, void *v2)
150 return v1;
153 struct isl_obj_vtable isl_obj_bool_vtable = {
154 isl_obj_bool_copy,
155 isl_obj_bool_add,
156 isl_obj_bool_print,
157 isl_obj_bool_free
159 #define isl_obj_bool (&isl_obj_bool_vtable)
161 int *isl_bool_from_int(int res)
163 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
166 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
167 __isl_take isl_union_map *map2)
169 return isl_union_map_is_subset(map2, map1);
171 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
172 __isl_take isl_union_set *set2)
174 return isl_union_set_is_subset(set2, set1);
177 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
178 __isl_take isl_union_map *map2)
180 return isl_union_map_is_strict_subset(map2, map1);
182 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
183 __isl_take isl_union_set *set2)
185 return isl_union_set_is_strict_subset(set2, set1);
188 extern struct isl_obj_vtable isl_obj_list_vtable;
189 #define isl_obj_list (&isl_obj_list_vtable)
191 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
192 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
193 struct isc_bin_op {
194 enum isl_token_type op;
195 isl_obj_type lhs;
196 isl_obj_type rhs;
197 isl_obj_type res;
198 union {
199 isc_bin_op_fn fn;
200 isc_bin_test_fn test;
201 } o;
203 struct isc_named_bin_op {
204 char *name;
205 struct isc_bin_op op;
208 struct iscc_at {
209 isl_union_pw_qpolynomial *upwqp;
210 isl_union_pw_qpolynomial *res;
213 static int eval_at(__isl_take isl_point *pnt, void *user)
215 struct iscc_at *at = (struct iscc_at *) user;
216 isl_qpolynomial *qp;
217 isl_set *set;
219 set = isl_set_from_point(isl_point_copy(pnt));
220 qp = isl_union_pw_qpolynomial_eval(
221 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
223 at->res = isl_union_pw_qpolynomial_add(at->res,
224 isl_union_pw_qpolynomial_from_pw_qpolynomial(
225 isl_pw_qpolynomial_alloc(set, qp)));
227 return 0;
230 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
231 __isl_take isl_union_pw_qpolynomial *upwqp,
232 __isl_take isl_union_set *uset)
234 struct iscc_at at;
236 at.upwqp = upwqp;
237 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
239 isl_union_set_foreach_point(uset, eval_at, &at);
241 isl_union_pw_qpolynomial_free(upwqp);
242 isl_union_set_free(uset);
244 return at.res;
247 struct iscc_fold_at {
248 isl_union_pw_qpolynomial_fold *upwf;
249 isl_union_pw_qpolynomial *res;
252 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
254 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
255 isl_qpolynomial *qp;
256 isl_set *set;
258 set = isl_set_from_point(isl_point_copy(pnt));
259 qp = isl_union_pw_qpolynomial_fold_eval(
260 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
262 at->res = isl_union_pw_qpolynomial_add(at->res,
263 isl_union_pw_qpolynomial_from_pw_qpolynomial(
264 isl_pw_qpolynomial_alloc(set, qp)));
266 return 0;
269 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
270 __isl_take isl_union_pw_qpolynomial_fold *upwf,
271 __isl_take isl_union_set *uset)
273 struct iscc_fold_at at;
275 at.upwf = upwf;
276 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
278 isl_union_set_foreach_point(uset, eval_fold_at, &at);
280 isl_union_pw_qpolynomial_fold_free(upwf);
281 isl_union_set_free(uset);
283 return at.res;
286 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
287 __isl_take isl_union_pw_qpolynomial *upwqp,
288 __isl_take isl_union_pw_qpolynomial_fold *upwf)
290 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
291 upwqp);
294 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
295 __isl_take isl_union_map *umap,
296 __isl_take isl_union_pw_qpolynomial_fold *upwf)
298 isl_ctx *ctx;
299 struct isl_list *list;
300 int tight;
302 ctx = isl_union_map_get_ctx(umap);
303 list = isl_list_alloc(ctx, 2);
304 if (!list)
305 goto error2;
307 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
308 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
309 upwf, &tight);
310 list->obj[1].type = isl_obj_bool;
311 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
312 if (tight < 0 || !list->obj[0].v)
313 goto error;
315 return list;
316 error2:
317 isl_union_map_free(umap);
318 isl_union_pw_qpolynomial_fold_free(upwf);
319 error:
320 isl_list_free(list);
321 return NULL;
324 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
325 __isl_take isl_union_set *uset,
326 __isl_take isl_union_pw_qpolynomial_fold *upwf)
328 isl_ctx *ctx;
329 struct isl_list *list;
330 int tight;
332 ctx = isl_union_set_get_ctx(uset);
333 list = isl_list_alloc(ctx, 2);
334 if (!list)
335 goto error2;
337 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
338 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
339 upwf, &tight);
340 list->obj[1].type = isl_obj_bool;
341 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
342 if (tight < 0 || !list->obj[0].v)
343 goto error;
345 return list;
346 error2:
347 isl_union_set_free(uset);
348 isl_union_pw_qpolynomial_fold_free(upwf);
349 error:
350 isl_list_free(list);
351 return NULL;
354 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_int_mul(
355 __isl_take isl_union_pw_qpolynomial *upwqp, __isl_take isl_int_obj *i)
357 isl_int v;
359 if (!i)
360 goto error;
362 isl_int_init(v);
363 isl_int_obj_get_int(i, &v);
364 upwqp = isl_union_pw_qpolynomial_mul_isl_int(upwqp, v);
365 isl_int_clear(v);
367 isl_int_obj_free(i);
369 return upwqp;
370 error:
371 isl_union_pw_qpolynomial_free(upwqp);
372 return NULL;
375 static __isl_give isl_union_pw_qpolynomial *int_union_pw_qpolynomial_mul(
376 __isl_take isl_int_obj *i, __isl_take isl_union_pw_qpolynomial *upwqp)
378 return union_pw_qpolynomial_int_mul(upwqp, i);
381 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_fold_int_mul(
382 __isl_take isl_union_pw_qpolynomial_fold *upwf,
383 __isl_take isl_int_obj *i)
385 isl_int v;
387 if (!i)
388 goto error;
390 isl_int_init(v);
391 isl_int_obj_get_int(i, &v);
392 upwf = isl_union_pw_qpolynomial_fold_mul_isl_int(upwf, v);
393 isl_int_clear(v);
395 isl_int_obj_free(i);
397 return upwf;
398 error:
399 isl_union_pw_qpolynomial_fold_free(upwf);
400 return NULL;
403 static __isl_give isl_union_pw_qpolynomial_fold *int_union_pw_qpolynomial_fold_mul(
404 __isl_take isl_int_obj *i,
405 __isl_take isl_union_pw_qpolynomial_fold *upwf)
407 return union_pw_qpolynomial_fold_int_mul(upwf, i);
410 struct isc_bin_op bin_ops[] = {
411 { '+', isl_obj_int, isl_obj_int, isl_obj_int,
412 (isc_bin_op_fn) &isl_int_obj_add },
413 { '-', isl_obj_int, isl_obj_int, isl_obj_int,
414 (isc_bin_op_fn) &isl_int_obj_sub },
415 { '*', isl_obj_int, isl_obj_int, isl_obj_int,
416 (isc_bin_op_fn) &isl_int_obj_mul },
417 { '+', isl_obj_union_set, isl_obj_union_set,
418 isl_obj_union_set,
419 (isc_bin_op_fn) &isl_union_set_union },
420 { '+', isl_obj_union_map, isl_obj_union_map,
421 isl_obj_union_map,
422 (isc_bin_op_fn) &isl_union_map_union },
423 { '-', isl_obj_union_set, isl_obj_union_set,
424 isl_obj_union_set,
425 (isc_bin_op_fn) &isl_union_set_subtract },
426 { '-', isl_obj_union_map, isl_obj_union_map,
427 isl_obj_union_map,
428 (isc_bin_op_fn) &isl_union_map_subtract },
429 { '*', isl_obj_union_set, isl_obj_union_set,
430 isl_obj_union_set,
431 (isc_bin_op_fn) &isl_union_set_intersect },
432 { '*', isl_obj_union_map, isl_obj_union_map,
433 isl_obj_union_map,
434 (isc_bin_op_fn) &isl_union_map_intersect },
435 { '*', isl_obj_union_map, isl_obj_union_set,
436 isl_obj_union_map,
437 (isc_bin_op_fn) &isl_union_map_intersect_domain },
438 { '.', isl_obj_union_map, isl_obj_union_map,
439 isl_obj_union_map,
440 (isc_bin_op_fn) &isl_union_map_apply_range },
441 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
442 isl_obj_union_pw_qpolynomial,
443 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
444 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
445 isl_obj_list,
446 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
447 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
448 isl_obj_union_map,
449 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
450 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
451 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
452 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
453 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
454 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
455 isl_obj_bool,
456 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
457 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
458 isl_obj_bool,
459 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
460 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
461 isl_obj_bool,
462 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
463 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
464 isl_obj_bool,
465 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
466 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
467 isl_obj_bool,
468 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
469 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
470 isl_obj_bool,
471 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
472 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
473 isl_obj_bool,
474 { .test =
475 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
476 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
477 isl_obj_bool,
478 { .test =
479 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
480 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
481 isl_obj_union_map,
482 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
483 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
484 isl_obj_union_map,
485 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
486 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
487 isl_obj_union_map,
488 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
489 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
490 isl_obj_union_map,
491 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
492 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
493 isl_obj_union_map,
494 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
495 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
496 isl_obj_union_map,
497 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
498 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
499 isl_obj_union_map,
500 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
501 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
502 isl_obj_union_map,
503 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
504 { '.', isl_obj_union_pw_qpolynomial_fold,
505 isl_obj_union_pw_qpolynomial_fold,
506 isl_obj_union_pw_qpolynomial_fold,
507 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
508 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
509 isl_obj_union_pw_qpolynomial,
510 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
511 { '+', isl_obj_union_pw_qpolynomial,
512 isl_obj_union_pw_qpolynomial_fold,
513 isl_obj_union_pw_qpolynomial_fold,
514 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
515 { '+', isl_obj_union_pw_qpolynomial_fold,
516 isl_obj_union_pw_qpolynomial,
517 isl_obj_union_pw_qpolynomial_fold,
518 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
519 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
520 isl_obj_union_pw_qpolynomial,
521 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
522 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial,
523 isl_obj_union_pw_qpolynomial,
524 (isc_bin_op_fn) &int_union_pw_qpolynomial_mul },
525 { '*', isl_obj_union_pw_qpolynomial, isl_obj_int,
526 isl_obj_union_pw_qpolynomial,
527 (isc_bin_op_fn) &union_pw_qpolynomial_int_mul },
528 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial_fold,
529 isl_obj_union_pw_qpolynomial_fold,
530 (isc_bin_op_fn) &int_union_pw_qpolynomial_fold_mul },
531 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_int,
532 isl_obj_union_pw_qpolynomial_fold,
533 (isc_bin_op_fn) &union_pw_qpolynomial_fold_int_mul },
534 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
535 isl_obj_union_pw_qpolynomial,
536 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
537 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
538 isl_obj_union_pw_qpolynomial,
539 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
540 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
541 isl_obj_union_pw_qpolynomial_fold,
542 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
543 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
544 isl_obj_union_pw_qpolynomial,
545 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
546 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
547 isl_obj_union_pw_qpolynomial,
548 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
549 { '%', isl_obj_union_set, isl_obj_union_set,
550 isl_obj_union_set,
551 (isc_bin_op_fn) &isl_union_set_gist },
552 { '%', isl_obj_union_map, isl_obj_union_map,
553 isl_obj_union_map,
554 (isc_bin_op_fn) &isl_union_map_gist },
555 { '%', isl_obj_union_map, isl_obj_union_set,
556 isl_obj_union_map,
557 (isc_bin_op_fn) &isl_union_map_gist_domain },
558 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
559 isl_obj_union_pw_qpolynomial,
560 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
561 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
562 isl_obj_union_pw_qpolynomial_fold,
563 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
564 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
565 isl_obj_union_pw_qpolynomial, isl_obj_bool,
566 { .test = (isc_bin_test_fn)
567 &isl_union_pw_qpolynomial_plain_is_equal } },
568 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
569 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
570 { .test = (isc_bin_test_fn)
571 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
572 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
573 (isc_bin_op_fn) &isl_str_concat },
577 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
578 __isl_take isl_union_map *umap2)
580 return isl_union_map_apply_range(umap2, umap1);
583 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
584 __isl_take isl_union_pw_qpolynomial *upwqp,
585 __isl_take isl_union_map *umap)
587 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
590 static __isl_give struct isl_list *qpolynomial_fold_after_map(
591 __isl_take isl_union_pw_qpolynomial_fold *upwf,
592 __isl_take isl_union_map *umap)
594 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
597 struct isc_named_bin_op named_bin_ops[] = {
598 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
599 isl_obj_union_map,
600 (isc_bin_op_fn) &map_after_map } },
601 { "after", { -1, isl_obj_union_pw_qpolynomial,
602 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
603 (isc_bin_op_fn) &qpolynomial_after_map } },
604 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
605 isl_obj_union_map, isl_obj_list,
606 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
607 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
608 isl_obj_union_map,
609 (isc_bin_op_fn) &isl_union_map_apply_range } },
610 { "before", { -1, isl_obj_union_map,
611 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
612 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
613 { "before", { -1, isl_obj_union_map,
614 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
615 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
616 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
617 isl_obj_union_set,
618 (isc_bin_op_fn) &isl_union_set_product } },
619 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
620 isl_obj_union_map,
621 (isc_bin_op_fn) &isl_union_map_product } },
622 NULL
625 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
627 return isl_set_from_basic_set(isl_union_set_sample(uset));
630 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
632 return isl_map_from_basic_map(isl_union_map_sample(umap));
635 static __isl_give struct isl_list *union_map_power(
636 __isl_take isl_union_map *umap)
638 isl_ctx *ctx;
639 struct isl_list *list;
640 int exact;
642 ctx = isl_union_map_get_ctx(umap);
643 list = isl_list_alloc(ctx, 2);
644 if (!list)
645 goto error2;
647 list->obj[0].type = isl_obj_union_map;
648 list->obj[0].v = isl_union_map_power(umap, &exact);
649 list->obj[1].type = isl_obj_bool;
650 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
651 if (exact < 0 || !list->obj[0].v)
652 goto error;
654 return list;
655 error2:
656 isl_union_map_free(umap);
657 error:
658 isl_list_free(list);
659 return NULL;
662 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
663 __isl_take isl_union_pw_qpolynomial *upwqp)
665 isl_ctx *ctx;
666 struct isl_list *list;
667 int tight;
669 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
670 list = isl_list_alloc(ctx, 2);
671 if (!list)
672 goto error2;
674 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
675 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
676 isl_fold_max, &tight);
677 list->obj[1].type = isl_obj_bool;
678 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
679 if (tight < 0 || !list->obj[0].v)
680 goto error;
682 return list;
683 error2:
684 isl_union_pw_qpolynomial_free(upwqp);
685 error:
686 isl_list_free(list);
687 return NULL;
690 #ifdef HAVE_CLOOG
691 void *map_codegen(void *arg)
693 isl_space *dim;
694 isl_union_map *umap = (isl_union_map *)arg;
695 isl_ctx *ctx = isl_union_map_get_ctx(umap);
696 CloogState *state;
697 CloogOptions *options;
698 CloogDomain *context;
699 CloogUnionDomain *ud;
700 CloogInput *input;
701 struct clast_stmt *stmt;
703 state = cloog_isl_state_malloc(ctx);
704 options = cloog_options_malloc(state);
705 options->language = CLOOG_LANGUAGE_C;
706 options->strides = 1;
707 options->sh = 1;
709 ud = cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap));
711 dim = isl_union_map_get_space(umap);
712 context = cloog_domain_from_isl_set(isl_set_universe(dim));
714 input = cloog_input_alloc(context, ud);
716 stmt = cloog_clast_create_from_input(input, options);
717 clast_pprint(stdout, stmt, 0, options);
718 cloog_clast_free(stmt);
720 error:
721 cloog_options_free(options);
722 cloog_state_free(state);
723 isl_union_map_free(umap);
724 return NULL;
727 void *set_codegen(void *arg)
729 isl_space *dim;
730 isl_union_set *uset = (isl_union_set *)arg;
731 isl_set *set;
732 isl_ctx *ctx = isl_union_set_get_ctx(uset);
733 CloogState *state;
734 CloogOptions *options;
735 CloogDomain *context;
736 CloogUnionDomain *ud;
737 CloogInput *input;
738 struct clast_stmt *stmt;
740 if (isl_union_set_n_set(uset) > 1)
741 isl_die(ctx, isl_error_invalid,
742 "code generation for more than one domain "
743 "requires a schedule", goto error);
745 state = cloog_isl_state_malloc(ctx);
746 options = cloog_options_malloc(state);
747 options->language = CLOOG_LANGUAGE_C;
748 options->strides = 1;
749 options->sh = 1;
751 set = isl_set_from_union_set(isl_union_set_copy(uset));
752 ud = cloog_union_domain_from_isl_set(set);
754 dim = isl_union_set_get_space(uset);
755 context = cloog_domain_from_isl_set(isl_set_universe(dim));
757 input = cloog_input_alloc(context, ud);
759 stmt = cloog_clast_create_from_input(input, options);
760 clast_pprint(stdout, stmt, 0, options);
761 cloog_clast_free(stmt);
763 cloog_options_free(options);
764 cloog_state_free(state);
765 error:
766 isl_union_set_free(uset);
767 return NULL;
769 #endif
771 #ifdef HAVE_PET
772 static __isl_give isl_list *parse(__isl_take isl_str *str)
774 isl_ctx *ctx;
775 struct isl_list *list;
776 struct pet_scop *scop;
777 isl_union_map *sched, *reads, *writes;
778 isl_union_set *domain;
779 struct iscc_options *options;
781 if (!str)
782 return NULL;
783 ctx = str->ctx;
785 options = isl_ctx_peek_iscc_options(ctx);
786 if (!options || !options->io) {
787 isl_str_free(str);
788 isl_die(ctx, isl_error_invalid,
789 "parse_file operation not allowed", return NULL);
792 list = isl_list_alloc(ctx, 4);
793 if (!list)
794 goto error;
796 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL, 1);
797 domain = pet_scop_collect_domains(scop);
798 sched = pet_scop_collect_schedule(scop);
799 reads = pet_scop_collect_reads(scop);
800 writes = pet_scop_collect_writes(scop);
801 pet_scop_free(scop);
803 list->obj[0].type = isl_obj_union_set;
804 list->obj[0].v = domain;
805 list->obj[1].type = isl_obj_union_map;
806 list->obj[1].v = writes;
807 list->obj[2].type = isl_obj_union_map;
808 list->obj[2].v = reads;
809 list->obj[3].type = isl_obj_union_map;
810 list->obj[3].v = sched;
812 if (!list->obj[0].v || !list->obj[1].v ||
813 !list->obj[2].v || !list->obj[3].v)
814 goto error;
816 isl_str_free(str);
817 return list;
818 error:
819 isl_list_free(list);
820 isl_str_free(str);
821 return NULL;
823 #endif
825 static int add_point(__isl_take isl_point *pnt, void *user)
827 isl_union_set **scan = (isl_union_set **) user;
829 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
831 return 0;
834 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
836 isl_union_set *scan;
838 scan = isl_union_set_empty(isl_union_set_get_space(uset));
840 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
841 isl_union_set_free(scan);
842 return uset;
845 isl_union_set_free(uset);
846 return scan;
849 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
851 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
854 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
855 __isl_take isl_union_pw_qpolynomial *upwqp)
857 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
860 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
861 __isl_take isl_union_pw_qpolynomial *upwqp)
863 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
866 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
867 __isl_take isl_union_pw_qpolynomial *upwqp)
869 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
872 typedef void *(*isc_un_op_fn)(void *arg);
873 struct isc_un_op {
874 enum isl_token_type op;
875 isl_obj_type arg;
876 isl_obj_type res;
877 isc_un_op_fn fn;
879 struct isc_named_un_op {
880 char *name;
881 struct isc_un_op op;
883 struct isc_named_un_op named_un_ops[] = {
884 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
885 (isc_un_op_fn) &isl_union_map_affine_hull } },
886 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
887 (isc_un_op_fn) &isl_union_set_affine_hull } },
888 {"card", { -1, isl_obj_union_set,
889 isl_obj_union_pw_qpolynomial,
890 (isc_un_op_fn) &isl_union_set_card } },
891 {"card", { -1, isl_obj_union_map,
892 isl_obj_union_pw_qpolynomial,
893 (isc_un_op_fn) &isl_union_map_card } },
894 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
895 (isc_un_op_fn) &isl_union_set_coalesce } },
896 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
897 (isc_un_op_fn) &isl_union_map_coalesce } },
898 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
899 isl_obj_union_pw_qpolynomial,
900 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
901 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
902 isl_obj_union_pw_qpolynomial_fold,
903 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
904 #ifdef HAVE_CLOOG
905 {"codegen", { -1, isl_obj_union_set, isl_obj_none,
906 &set_codegen } },
907 {"codegen", { -1, isl_obj_union_map, isl_obj_none,
908 &map_codegen } },
909 #endif
910 {"coefficients", { -1, isl_obj_union_set,
911 isl_obj_union_set,
912 (isc_un_op_fn) &isl_union_set_coefficients } },
913 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
914 (isc_un_op_fn) &isl_union_set_solutions } },
915 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
916 (isc_un_op_fn) &isl_union_map_deltas } },
917 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
918 (isc_un_op_fn) &isl_union_map_deltas_map } },
919 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
920 (isc_un_op_fn) &isl_union_map_domain } },
921 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
922 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
923 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
924 isl_obj_union_set,
925 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
926 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
927 (isc_un_op_fn) &isl_union_map_domain } },
928 {"domain", { -1, isl_obj_union_pw_qpolynomial,
929 isl_obj_union_set,
930 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
931 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
932 isl_obj_union_set,
933 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
934 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
935 (isc_un_op_fn) &isl_union_map_domain_map } },
936 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
937 (isc_un_op_fn) &isl_union_map_range } },
938 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
939 (isc_un_op_fn) &isl_union_map_range } },
940 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
941 (isc_un_op_fn) &isl_union_map_range_map } },
942 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
943 (isc_un_op_fn) &isl_union_set_identity } },
944 {"lattice_width", { -1, isl_obj_union_set,
945 isl_obj_union_pw_qpolynomial,
946 (isc_un_op_fn) &isl_union_set_lattice_width } },
947 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
948 (isc_un_op_fn) &isl_union_map_lexmin } },
949 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
950 (isc_un_op_fn) &isl_union_map_lexmax } },
951 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
952 (isc_un_op_fn) &isl_union_set_lexmin } },
953 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
954 (isc_un_op_fn) &isl_union_set_lexmax } },
955 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
956 (isc_un_op_fn) &isl_union_set_lift } },
957 {"params", { -1, isl_obj_union_map, isl_obj_set,
958 (isc_un_op_fn) &isl_union_map_params } },
959 {"params", { -1, isl_obj_union_set, isl_obj_set,
960 (isc_un_op_fn) &isl_union_set_params } },
961 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
962 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
963 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
964 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
965 {"poly", { -1, isl_obj_union_pw_qpolynomial,
966 isl_obj_union_pw_qpolynomial,
967 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
968 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
969 isl_obj_union_pw_qpolynomial,
970 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
971 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
972 isl_obj_union_pw_qpolynomial,
973 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
974 #ifdef HAVE_PET
975 {"parse_file", { -1, isl_obj_str, isl_obj_list,
976 (isc_un_op_fn) &parse } },
977 #endif
978 {"pow", { -1, isl_obj_union_map, isl_obj_list,
979 (isc_un_op_fn) &union_map_power } },
980 {"sample", { -1, isl_obj_union_set, isl_obj_set,
981 (isc_un_op_fn) &union_set_sample } },
982 {"sample", { -1, isl_obj_union_map, isl_obj_map,
983 (isc_un_op_fn) &union_map_sample } },
984 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
985 (isc_un_op_fn) &union_set_scan } },
986 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
987 (isc_un_op_fn) &union_map_scan } },
988 {"sum", { -1, isl_obj_union_pw_qpolynomial,
989 isl_obj_union_pw_qpolynomial,
990 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
991 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
992 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
993 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
994 (isc_un_op_fn) &isl_union_set_unwrap } },
995 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
996 (isc_un_op_fn) &isl_union_map_wrap } },
997 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
998 (isc_un_op_fn) &isl_union_map_zip } },
999 NULL
1002 struct isl_named_obj {
1003 char *name;
1004 struct isl_obj obj;
1007 static void free_obj(struct isl_obj obj)
1009 obj.type->free(obj.v);
1012 static int same_name(const void *entry, const void *val)
1014 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
1016 return !strcmp(named->name, val);
1019 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
1020 char *name, struct isl_obj obj)
1022 struct isl_hash_table_entry *entry;
1023 uint32_t name_hash;
1024 struct isl_named_obj *named;
1026 name_hash = isl_hash_string(isl_hash_init(), name);
1027 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
1028 if (!entry)
1029 goto error;
1030 if (entry->data) {
1031 named = entry->data;
1032 free_obj(named->obj);
1033 free(name);
1034 } else {
1035 named = isl_alloc_type(ctx, struct isl_named_obj);
1036 if (!named)
1037 goto error;
1038 named->name = name;
1039 entry->data = named;
1041 named->obj = obj;
1043 return 0;
1044 error:
1045 free_obj(obj);
1046 free(name);
1047 return -1;
1050 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1051 struct isl_hash_table *table, char *name)
1053 struct isl_obj obj = { isl_obj_none, NULL };
1054 struct isl_hash_table_entry *entry;
1055 uint32_t name_hash;
1057 name_hash = isl_hash_string(isl_hash_init(), name);
1058 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1059 if (entry) {
1060 struct isl_named_obj *named;
1061 named = entry->data;
1062 obj = named->obj;
1063 } else if (isdigit(name[0]))
1064 fprintf(stderr, "unknown identifier '$%s'\n", name);
1065 else
1066 fprintf(stderr, "unknown identifier '%s'\n", name);
1068 free(name);
1069 obj.v = obj.type->copy(obj.v);
1070 return obj;
1073 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1075 if (obj.type == super)
1076 return 1;
1077 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1078 return 1;
1079 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1080 return 1;
1081 if (obj.type == isl_obj_pw_qpolynomial &&
1082 super == isl_obj_union_pw_qpolynomial)
1083 return 1;
1084 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1085 super == isl_obj_union_pw_qpolynomial_fold)
1086 return 1;
1087 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1088 return 1;
1089 if (obj.type == isl_obj_list) {
1090 struct isl_list *list = obj.v;
1091 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1092 return is_subtype(list->obj[0], super);
1094 if (super == isl_obj_str)
1095 return 1;
1096 return 0;
1099 static struct isl_obj obj_at(struct isl_obj obj, int i)
1101 struct isl_list *list = obj.v;
1103 obj = list->obj[i];
1104 obj.v = obj.type->copy(obj.v);
1106 isl_list_free(list);
1108 return obj;
1111 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1112 isl_obj_type type)
1114 if (obj.type == type)
1115 return obj;
1116 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1117 obj.type = isl_obj_union_map;
1118 obj.v = isl_union_map_from_map(obj.v);
1119 return obj;
1121 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1122 obj.type = isl_obj_union_set;
1123 obj.v = isl_union_set_from_set(obj.v);
1124 return obj;
1126 if (obj.type == isl_obj_pw_qpolynomial &&
1127 type == isl_obj_union_pw_qpolynomial) {
1128 obj.type = isl_obj_union_pw_qpolynomial;
1129 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1130 return obj;
1132 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1133 type == isl_obj_union_pw_qpolynomial_fold) {
1134 obj.type = isl_obj_union_pw_qpolynomial_fold;
1135 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1136 return obj;
1138 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1139 if (type == isl_obj_union_map) {
1140 obj.type = isl_obj_union_map;
1141 return obj;
1143 if (type == isl_obj_union_pw_qpolynomial) {
1144 isl_space *dim = isl_union_set_get_space(obj.v);
1145 isl_union_set_free(obj.v);
1146 obj.v = isl_union_pw_qpolynomial_zero(dim);
1147 obj.type = isl_obj_union_pw_qpolynomial;
1148 return obj;
1150 if (type == isl_obj_union_pw_qpolynomial_fold) {
1151 isl_space *dim = isl_union_set_get_space(obj.v);
1152 isl_union_set_free(obj.v);
1153 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1154 isl_fold_list);
1155 obj.type = isl_obj_union_pw_qpolynomial_fold;
1156 return obj;
1159 if (obj.type == isl_obj_list) {
1160 struct isl_list *list = obj.v;
1161 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1162 return convert(ctx, obj_at(obj, 0), type);
1164 if (type == isl_obj_str) {
1165 isl_str *str;
1166 isl_printer *p;
1167 char *s;
1169 p = isl_printer_to_str(ctx);
1170 if (!p)
1171 goto error;
1172 p = obj.type->print(p, obj.v);
1173 s = isl_printer_get_str(p);
1174 isl_printer_free(p);
1176 str = isl_str_from_string(ctx, s);
1177 if (!str)
1178 goto error;
1179 free_obj(obj);
1180 obj.v = str;
1181 obj.type = isl_obj_str;
1182 return obj;
1185 error:
1186 free_obj(obj);
1187 obj.type = isl_obj_none;
1188 obj.v = NULL;
1189 return obj;
1192 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1193 struct isl_obj lhs)
1195 int i;
1196 struct isl_token *tok;
1198 tok = isl_stream_next_token(s);
1199 if (!tok)
1200 return NULL;
1202 for (i = 0; ; ++i) {
1203 if (!bin_ops[i].op)
1204 break;
1205 if (bin_ops[i].op != tok->type)
1206 continue;
1207 if (!is_subtype(lhs, bin_ops[i].lhs))
1208 continue;
1210 isl_token_free(tok);
1211 return &bin_ops[i];
1214 for (i = 0; ; ++i) {
1215 if (!named_bin_ops[i].name)
1216 break;
1217 if (named_bin_ops[i].op.op != tok->type)
1218 continue;
1219 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1220 continue;
1222 isl_token_free(tok);
1223 return &named_bin_ops[i].op;
1226 isl_stream_push_token(s, tok);
1228 return NULL;
1231 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1233 int i;
1234 struct isl_token *tok;
1236 tok = isl_stream_next_token(s);
1237 if (!tok)
1238 return NULL;
1240 for (i = 0; ; ++i) {
1241 if (!named_un_ops[i].name)
1242 break;
1243 if (named_un_ops[i].op.op != tok->type)
1244 continue;
1246 isl_token_free(tok);
1247 return &named_un_ops[i].op;
1250 isl_stream_push_token(s, tok);
1252 return NULL;
1255 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1256 struct isl_obj arg)
1258 int i;
1260 for (i = 0; ; ++i) {
1261 if (!named_un_ops[i].name)
1262 break;
1263 if (named_un_ops[i].op.op != like->op)
1264 continue;
1265 if (!is_subtype(arg, named_un_ops[i].op.arg))
1266 continue;
1268 return &named_un_ops[i].op;
1271 return NULL;
1274 static int is_assign(struct isl_stream *s)
1276 struct isl_token *tok;
1277 struct isl_token *tok2;
1278 int assign;
1280 tok = isl_stream_next_token(s);
1281 if (!tok)
1282 return 0;
1283 if (tok->type != ISL_TOKEN_IDENT) {
1284 isl_stream_push_token(s, tok);
1285 return 0;
1288 tok2 = isl_stream_next_token(s);
1289 if (!tok2) {
1290 isl_stream_push_token(s, tok);
1291 return 0;
1293 assign = tok2->type == ISL_TOKEN_DEF;
1294 isl_stream_push_token(s, tok2);
1295 isl_stream_push_token(s, tok);
1297 return assign;
1300 static struct isl_obj read_obj(struct isl_stream *s,
1301 struct isl_hash_table *table);
1302 static struct isl_obj read_expr(struct isl_stream *s,
1303 struct isl_hash_table *table);
1305 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1306 struct isl_hash_table *table, struct isc_un_op *op)
1308 struct isl_obj obj = { isl_obj_none, NULL };
1310 obj = read_obj(s, table);
1311 if (!obj.v)
1312 goto error;
1314 op = find_matching_un_op(op, obj);
1316 if (!op)
1317 isl_die(s->ctx, isl_error_invalid,
1318 "no such unary operator defined on given operand",
1319 goto error);
1321 obj = convert(s->ctx, obj, op->arg);
1322 obj.v = op->fn(obj.v);
1323 obj.type = op->res;
1325 return obj;
1326 error:
1327 free_obj(obj);
1328 obj.type = isl_obj_none;
1329 obj.v = NULL;
1330 return obj;
1333 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1335 struct isl_list *list;
1336 int exact;
1338 if (obj.type != isl_obj_union_map)
1339 obj = convert(ctx, obj, isl_obj_union_map);
1340 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1341 list = isl_list_alloc(ctx, 2);
1342 if (!list)
1343 goto error;
1345 list->obj[0].type = isl_obj_union_map;
1346 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1347 list->obj[1].type = isl_obj_bool;
1348 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1349 obj.v = list;
1350 obj.type = isl_obj_list;
1351 if (exact < 0 || !list->obj[0].v)
1352 goto error;
1354 return obj;
1355 error:
1356 free_obj(obj);
1357 obj.type = isl_obj_none;
1358 obj.v = NULL;
1359 return obj;
1362 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1364 struct isl_list *list = obj.v;
1365 struct isl_token *tok;
1366 int i;
1368 tok = isl_stream_next_token(s);
1369 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1370 isl_stream_error(s, tok, "expecting index");
1371 if (tok)
1372 isl_stream_push_token(s, tok);
1373 goto error;
1375 i = isl_int_get_si(tok->u.v);
1376 isl_token_free(tok);
1377 isl_assert(s->ctx, i < list->n, goto error);
1378 if (isl_stream_eat(s, ']'))
1379 goto error;
1381 return obj_at(obj, i);
1382 error:
1383 free_obj(obj);
1384 obj.type = isl_obj_none;
1385 obj.v = NULL;
1386 return obj;
1389 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1390 struct isl_hash_table *table)
1392 struct isl_obj obj;
1394 obj = read_expr(s, table);
1395 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1396 is_subtype(obj, isl_obj_union_map), goto error);
1398 if (obj.type == isl_obj_list) {
1399 struct isl_list *list = obj.v;
1400 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1401 obj = obj_at(obj, 0);
1403 if (obj.type == isl_obj_set)
1404 obj = convert(s->ctx, obj, isl_obj_union_set);
1405 else if (obj.type == isl_obj_map)
1406 obj = convert(s->ctx, obj, isl_obj_union_map);
1407 if (obj.type == isl_obj_union_set) {
1408 obj.v = isl_union_set_apply(obj.v, umap);
1409 } else
1410 obj.v = isl_union_map_apply_range(obj.v, umap);
1411 if (!obj.v)
1412 goto error2;
1414 if (isl_stream_eat(s, ')'))
1415 goto error2;
1417 return obj;
1418 error:
1419 isl_union_map_free(umap);
1420 error2:
1421 free_obj(obj);
1422 obj.type = isl_obj_none;
1423 obj.v = NULL;
1424 return obj;
1427 static struct isl_obj apply_fun_set(struct isl_obj obj,
1428 __isl_take isl_union_set *uset)
1430 if (obj.type == isl_obj_union_pw_qpolynomial) {
1431 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1432 } else {
1433 obj.type = isl_obj_list;
1434 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1436 return obj;
1439 static struct isl_obj apply_fun_map(struct isl_obj obj,
1440 __isl_take isl_union_map *umap)
1442 if (obj.type == isl_obj_union_pw_qpolynomial) {
1443 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1444 } else {
1445 obj.type = isl_obj_list;
1446 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1448 return obj;
1451 static struct isl_obj apply_fun(struct isl_stream *s,
1452 struct isl_obj obj, struct isl_hash_table *table)
1454 struct isl_obj arg;
1456 arg = read_expr(s, table);
1457 if (!is_subtype(arg, isl_obj_union_map) &&
1458 !is_subtype(arg, isl_obj_union_set))
1459 isl_die(s->ctx, isl_error_invalid,
1460 "expecting set of map argument", goto error);
1462 if (arg.type == isl_obj_list) {
1463 struct isl_list *list = arg.v;
1464 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1465 arg = obj_at(arg, 0);
1467 if (arg.type == isl_obj_set)
1468 arg = convert(s->ctx, arg, isl_obj_union_set);
1469 else if (arg.type == isl_obj_map)
1470 arg = convert(s->ctx, arg, isl_obj_union_map);
1471 if (arg.type == isl_obj_union_set)
1472 obj = apply_fun_set(obj, arg.v);
1473 else
1474 obj = apply_fun_map(obj, arg.v);
1475 if (!obj.v)
1476 goto error2;
1478 if (isl_stream_eat(s, ')'))
1479 goto error2;
1481 return obj;
1482 error:
1483 free_obj(arg);
1484 error2:
1485 free_obj(obj);
1486 obj.type = isl_obj_none;
1487 obj.v = NULL;
1488 return obj;
1491 struct add_vertex_data {
1492 struct isl_list *list;
1493 int i;
1496 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1498 struct add_vertex_data *data = (struct add_vertex_data *)user;
1499 isl_basic_set *expr;
1501 expr = isl_vertex_get_expr(vertex);
1503 data->list->obj[data->i].type = isl_obj_set;
1504 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1505 data->i++;
1507 isl_vertex_free(vertex);
1509 return 0;
1512 static int set_vertices(__isl_take isl_set *set, void *user)
1514 isl_ctx *ctx;
1515 isl_basic_set *hull;
1516 isl_vertices *vertices = NULL;
1517 struct isl_list *list = NULL;
1518 int r;
1519 struct add_vertex_data *data = (struct add_vertex_data *)user;
1521 set = isl_set_remove_divs(set);
1522 hull = isl_set_convex_hull(set);
1523 vertices = isl_basic_set_compute_vertices(hull);
1524 isl_basic_set_free(hull);
1526 list = data->list;
1528 ctx = isl_vertices_get_ctx(vertices);
1529 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1530 if (!data->list)
1531 goto error;
1533 data->i = 0;
1534 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1536 data->list = isl_list_concat(list, data->list);
1538 isl_vertices_free(vertices);
1540 return r;
1541 error:
1542 data->list = list;
1543 isl_vertices_free(vertices);
1544 return -1;
1547 static struct isl_obj vertices(struct isl_stream *s,
1548 struct isl_hash_table *table)
1550 isl_ctx *ctx;
1551 struct isl_obj obj;
1552 struct isl_list *list = NULL;
1553 isl_union_set *uset;
1554 struct add_vertex_data data = { NULL };
1556 obj = read_expr(s, table);
1557 obj = convert(s->ctx, obj, isl_obj_union_set);
1558 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1559 uset = obj.v;
1560 obj.v = NULL;
1562 ctx = isl_union_set_get_ctx(uset);
1563 list = isl_list_alloc(ctx, 0);
1564 if (!list)
1565 goto error;
1567 data.list = list;
1569 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1570 goto error;
1572 isl_union_set_free(uset);
1574 obj.type = isl_obj_list;
1575 obj.v = data.list;
1577 return obj;
1578 error:
1579 isl_union_set_free(uset);
1580 isl_list_free(data.list);
1581 free_obj(obj);
1582 obj.type = isl_obj_none;
1583 obj.v = NULL;
1584 return obj;
1587 static struct isl_obj type_of(struct isl_stream *s,
1588 struct isl_hash_table *table)
1590 isl_ctx *ctx;
1591 struct isl_obj obj;
1592 const char *type = "unknown";
1594 obj = read_expr(s, table);
1596 if (obj.type == isl_obj_map ||
1597 obj.type == isl_obj_union_map)
1598 type = "map";
1599 if (obj.type == isl_obj_set ||
1600 obj.type == isl_obj_union_set)
1601 type = "set";
1602 if (obj.type == isl_obj_pw_qpolynomial ||
1603 obj.type == isl_obj_union_pw_qpolynomial)
1604 type = "piecewise quasipolynomial";
1605 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1606 obj.type == isl_obj_union_pw_qpolynomial_fold)
1607 type = "piecewise quasipolynomial fold";
1608 if (obj.type == isl_obj_list)
1609 type = "list";
1610 if (obj.type == isl_obj_bool)
1611 type = "boolean";
1612 if (obj.type == isl_obj_str)
1613 type = "string";
1614 if (obj.type == isl_obj_int)
1615 type = "int";
1617 free_obj(obj);
1618 obj.type = isl_obj_str;
1619 obj.v = isl_str_from_string(s->ctx, strdup(type));
1621 return obj;
1624 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1625 struct isl_hash_table *table)
1627 struct isl_obj obj;
1629 obj = read_obj(s, table);
1630 obj = convert(s->ctx, obj, isl_obj_union_set);
1631 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1632 return obj.v;
1633 error:
1634 free_obj(obj);
1635 return NULL;
1638 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1639 struct isl_hash_table *table)
1641 struct isl_obj obj;
1643 obj = read_obj(s, table);
1644 obj = convert(s->ctx, obj, isl_obj_union_map);
1645 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1646 return obj.v;
1647 error:
1648 free_obj(obj);
1649 return NULL;
1652 static struct isl_obj last_any(struct isl_stream *s,
1653 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1654 __isl_take isl_union_map *may_source)
1656 struct isl_obj obj = { isl_obj_none, NULL };
1657 isl_union_map *sink = NULL;
1658 isl_union_map *schedule = NULL;
1659 isl_union_map *may_dep;
1660 isl_union_map *must_dep;
1662 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1663 goto error;
1665 sink = read_map(s, table);
1666 if (!sink)
1667 goto error;
1669 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1670 goto error;
1672 schedule = read_map(s, table);
1673 if (!schedule)
1674 goto error;
1676 if (isl_union_map_compute_flow(sink, must_source, may_source,
1677 schedule, &must_dep, &may_dep,
1678 NULL, NULL) < 0)
1679 return obj;
1681 obj.type = isl_obj_union_map;
1682 obj.v = isl_union_map_union(must_dep, may_dep);
1684 return obj;
1685 error:
1686 isl_union_map_free(may_source);
1687 isl_union_map_free(must_source);
1688 isl_union_map_free(sink);
1689 isl_union_map_free(schedule);
1690 free_obj(obj);
1691 obj.type = isl_obj_none;
1692 obj.v = NULL;
1693 return obj;
1696 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1698 struct isl_obj obj = { isl_obj_none, NULL };
1699 isl_union_map *must_source = NULL;
1700 isl_union_map *may_source = NULL;
1701 isl_union_map *sink = NULL;
1702 isl_union_map *schedule = NULL;
1703 isl_union_map *may_dep;
1705 may_source = read_map(s, table);
1706 if (!may_source)
1707 goto error;
1709 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1710 must_source = read_map(s, table);
1711 if (!must_source)
1712 goto error;
1713 return last_any(s, table, must_source, may_source);
1716 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1717 goto error;
1719 sink = read_map(s, table);
1720 if (!sink)
1721 goto error;
1723 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1724 goto error;
1726 schedule = read_map(s, table);
1727 if (!schedule)
1728 goto error;
1730 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1731 if (isl_union_map_compute_flow(sink, must_source, may_source,
1732 schedule, NULL, &may_dep,
1733 NULL, NULL) < 0)
1734 return obj;
1736 obj.type = isl_obj_union_map;
1737 obj.v = may_dep;
1739 return obj;
1740 error:
1741 isl_union_map_free(may_source);
1742 isl_union_map_free(must_source);
1743 isl_union_map_free(sink);
1744 isl_union_map_free(schedule);
1745 free_obj(obj);
1746 obj.type = isl_obj_none;
1747 obj.v = NULL;
1748 return obj;
1751 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1753 struct isl_obj obj = { isl_obj_none, NULL };
1754 struct isl_list *list = NULL;
1755 isl_union_map *must_source = NULL;
1756 isl_union_map *may_source = NULL;
1757 isl_union_map *sink = NULL;
1758 isl_union_map *schedule = NULL;
1759 isl_union_map *must_dep;
1760 isl_union_map *must_no_source;
1762 must_source = read_map(s, table);
1763 if (!must_source)
1764 goto error;
1766 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1767 may_source = read_map(s, table);
1768 if (!may_source)
1769 goto error;
1770 return last_any(s, table, must_source, may_source);
1773 list = isl_list_alloc(s->ctx, 2);
1774 if (!list)
1775 goto error;
1777 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1778 goto error;
1780 sink = read_map(s, table);
1781 if (!sink)
1782 goto error;
1784 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1785 goto error;
1787 schedule = read_map(s, table);
1788 if (!schedule)
1789 goto error;
1791 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1792 if (isl_union_map_compute_flow(sink, must_source, may_source,
1793 schedule, &must_dep, NULL,
1794 &must_no_source, NULL) < 0) {
1795 isl_list_free(list);
1796 return obj;
1799 list->obj[0].type = isl_obj_union_map;
1800 list->obj[0].v = must_dep;
1801 list->obj[1].type = isl_obj_union_map;
1802 list->obj[1].v = must_no_source;
1804 obj.v = list;
1805 obj.type = isl_obj_list;
1807 return obj;
1808 error:
1809 isl_list_free(list);
1810 isl_union_map_free(may_source);
1811 isl_union_map_free(must_source);
1812 isl_union_map_free(sink);
1813 isl_union_map_free(schedule);
1814 free_obj(obj);
1815 obj.type = isl_obj_none;
1816 obj.v = NULL;
1817 return obj;
1820 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1821 struct isl_hash_table *table)
1823 isl_union_set *domain;
1824 isl_union_map *validity;
1825 isl_union_map *proximity;
1827 domain = read_set(s, table);
1828 if (!domain)
1829 return NULL;
1831 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1832 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1834 for (;;) {
1835 isl_union_map *umap;
1836 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1837 umap = read_map(s, table);
1838 validity = isl_union_map_union(validity, umap);
1839 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1840 umap = read_map(s, table);
1841 proximity = isl_union_map_union(proximity, umap);
1842 } else
1843 break;
1846 return isl_union_set_compute_schedule(domain, validity, proximity);
1849 static struct isl_obj schedule(struct isl_stream *s,
1850 struct isl_hash_table *table)
1852 struct isl_obj obj = { isl_obj_none, NULL };
1853 isl_schedule *schedule;
1855 schedule = get_schedule(s, table);
1857 obj.v = isl_schedule_get_map(schedule);
1858 obj.type = isl_obj_union_map;
1860 isl_schedule_free(schedule);
1862 return obj;
1865 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1867 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1869 struct isl_obj obj = { isl_obj_none, NULL };
1870 isl_ctx *ctx = isl_band_get_ctx(band);
1871 struct isl_list *list;
1873 list = isl_list_alloc(ctx, 2);
1874 if (!list)
1875 goto error;
1877 obj.v = list;
1878 obj.type = isl_obj_list;
1880 list->obj[0].type = isl_obj_union_map;
1881 list->obj[0].v = isl_band_get_partial_schedule(band);
1883 if (isl_band_has_children(band)) {
1884 isl_band_list *children;
1886 children = isl_band_get_children(band);
1887 list->obj[1] = band_list_to_obj_list(children);
1888 } else {
1889 list->obj[1].type = isl_obj_list;
1890 list->obj[1].v = isl_list_alloc(ctx, 0);
1893 if (!list->obj[0].v || !list->obj[1].v)
1894 goto error;
1896 isl_band_free(band);
1898 return obj;
1899 error:
1900 isl_band_free(band);
1901 free_obj(obj);
1902 obj.type = isl_obj_none;
1903 obj.v = NULL;
1904 return obj;
1907 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1909 struct isl_obj obj = { isl_obj_none, NULL };
1910 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1911 struct isl_list *list;
1912 int i, n;
1914 n = isl_band_list_n_band(bands);
1915 list = isl_list_alloc(ctx, n);
1916 if (!list)
1917 goto error;
1919 obj.v = list;
1920 obj.type = isl_obj_list;
1922 for (i = 0; i < n; ++i) {
1923 isl_band *band;
1925 band = isl_band_list_get_band(bands, i);
1926 list->obj[i] = band_to_obj_list(band);
1927 if (!list->obj[i].v)
1928 goto error;
1931 isl_band_list_free(bands);
1933 return obj;
1934 error:
1935 isl_band_list_free(bands);
1936 free_obj(obj);
1937 obj.type = isl_obj_none;
1938 obj.v = NULL;
1939 return obj;
1942 static struct isl_obj schedule_forest(struct isl_stream *s,
1943 struct isl_hash_table *table)
1945 struct isl_obj obj = { isl_obj_none, NULL };
1946 isl_schedule *schedule;
1947 isl_band_list *roots;
1949 schedule = get_schedule(s, table);
1950 if (!schedule)
1951 return obj;
1953 roots = isl_schedule_get_band_forest(schedule);
1954 isl_schedule_free(schedule);
1956 return band_list_to_obj_list(roots);
1959 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1961 struct isl_token *tok;
1963 if (isl_stream_eat_if_available(s, '+'))
1964 return transitive_closure(s->ctx, obj);
1966 tok = isl_stream_next_token(s);
1967 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1968 isl_stream_error(s, tok, "expecting -1");
1969 if (tok)
1970 isl_stream_push_token(s, tok);
1971 goto error;
1973 isl_token_free(tok);
1974 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1975 if (obj.type != isl_obj_union_map)
1976 obj = convert(s->ctx, obj, isl_obj_union_map);
1978 obj.v = isl_union_map_reverse(obj.v);
1979 if (!obj.v)
1980 goto error;
1982 return obj;
1983 error:
1984 free_obj(obj);
1985 obj.type = isl_obj_none;
1986 obj.v = NULL;
1987 return obj;
1990 static struct isl_obj check_assert(struct isl_stream *s,
1991 struct isl_hash_table *table)
1993 struct isl_obj obj;
1995 obj = read_expr(s, table);
1996 if (obj.type != isl_obj_bool)
1997 isl_die(s->ctx, isl_error_invalid,
1998 "expecting boolean expression", goto error);
1999 if (obj.v != &isl_bool_true)
2000 isl_die(s->ctx, isl_error_unknown,
2001 "assertion failed", abort());
2002 error:
2003 free_obj(obj);
2004 obj.type = isl_obj_none;
2005 obj.v = NULL;
2006 return obj;
2009 static struct isl_obj read_from_file(struct isl_stream *s)
2011 struct isl_obj obj;
2012 struct isl_token *tok;
2013 struct isl_stream *s_file;
2014 struct iscc_options *options;
2015 FILE *file;
2017 tok = isl_stream_next_token(s);
2018 if (!tok || tok->type != ISL_TOKEN_STRING) {
2019 isl_stream_error(s, tok, "expecting filename");
2020 isl_token_free(tok);
2021 goto error;
2024 options = isl_ctx_peek_iscc_options(s->ctx);
2025 if (!options || !options->io) {
2026 isl_token_free(tok);
2027 isl_die(s->ctx, isl_error_invalid,
2028 "read operation not allowed", goto error);
2031 file = fopen(tok->u.s, "r");
2032 isl_token_free(tok);
2033 isl_assert(s->ctx, file, goto error);
2035 s_file = isl_stream_new_file(s->ctx, file);
2036 if (!s_file) {
2037 fclose(file);
2038 goto error;
2041 obj = isl_stream_read_obj(s_file);
2043 isl_stream_free(s_file);
2044 fclose(file);
2046 return obj;
2047 error:
2048 obj.type = isl_obj_none;
2049 obj.v = NULL;
2050 return obj;
2053 static struct isl_obj write_to_file(struct isl_stream *s,
2054 struct isl_hash_table *table)
2056 struct isl_obj obj;
2057 struct isl_token *tok;
2058 struct isl_stream *s_file;
2059 struct iscc_options *options;
2060 FILE *file;
2061 isl_printer *p;
2063 tok = isl_stream_next_token(s);
2064 if (!tok || tok->type != ISL_TOKEN_STRING) {
2065 isl_stream_error(s, tok, "expecting filename");
2066 isl_token_free(tok);
2067 goto error;
2070 obj = read_expr(s, table);
2072 options = isl_ctx_peek_iscc_options(s->ctx);
2073 if (!options || !options->io) {
2074 isl_token_free(tok);
2075 isl_die(s->ctx, isl_error_invalid,
2076 "write operation not allowed", goto error);
2079 file = fopen(tok->u.s, "w");
2080 isl_token_free(tok);
2081 if (!file)
2082 isl_die(s->ctx, isl_error_unknown,
2083 "could not open file for writing", goto error);
2085 p = isl_printer_to_file(s->ctx, file);
2086 p = isl_printer_set_output_format(p, options->format);
2087 p = obj.type->print(p, obj.v);
2088 p = isl_printer_end_line(p);
2089 isl_printer_free(p);
2091 fclose(file);
2092 error:
2093 free_obj(obj);
2094 obj.type = isl_obj_none;
2095 obj.v = NULL;
2096 return obj;
2099 static struct isl_obj read_string_if_available(struct isl_stream *s)
2101 struct isl_token *tok;
2102 struct isl_obj obj = { isl_obj_none, NULL };
2104 tok = isl_stream_next_token(s);
2105 if (!tok)
2106 return obj;
2107 if (tok->type == ISL_TOKEN_STRING) {
2108 isl_str *str;
2109 str = isl_str_alloc(s->ctx);
2110 if (!str)
2111 goto error;
2112 str->s = strdup(tok->u.s);
2113 isl_token_free(tok);
2114 obj.v = str;
2115 obj.type = isl_obj_str;
2116 } else
2117 isl_stream_push_token(s, tok);
2118 return obj;
2119 error:
2120 isl_token_free(tok);
2121 return obj;
2124 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2126 struct isl_token *tok;
2127 struct isl_obj obj = { isl_obj_none, NULL };
2129 tok = isl_stream_next_token(s);
2130 if (!tok)
2131 return obj;
2132 if (tok->type == ISL_TOKEN_FALSE || tok->type == ISL_TOKEN_TRUE) {
2133 int is_true = tok->type == ISL_TOKEN_TRUE;
2134 isl_token_free(tok);
2135 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2136 obj.type = isl_obj_bool;
2137 } else
2138 isl_stream_push_token(s, tok);
2139 return obj;
2140 error:
2141 isl_token_free(tok);
2142 return obj;
2145 static __isl_give char *read_ident(struct isl_stream *s)
2147 char *name;
2148 struct isl_token *tok, *tok2;
2150 name = isl_stream_read_ident_if_available(s);
2151 if (name)
2152 return name;
2154 tok = isl_stream_next_token(s);
2155 if (!tok)
2156 return NULL;
2157 if (tok->type != '$') {
2158 isl_stream_push_token(s, tok);
2159 return NULL;
2161 tok2 = isl_stream_next_token(s);
2162 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2163 if (tok2)
2164 isl_stream_push_token(s, tok2);
2165 isl_stream_push_token(s, tok);
2166 return NULL;
2169 name = isl_int_get_str(tok2->u.v);
2170 isl_token_free(tok);
2171 isl_token_free(tok2);
2173 return name;
2176 static struct isl_obj read_list(struct isl_stream *s,
2177 struct isl_hash_table *table, struct isl_obj obj)
2179 struct isl_list *list;
2181 list = isl_list_alloc(s->ctx, 2);
2182 if (!list)
2183 goto error;
2184 list->obj[0] = obj;
2185 list->obj[1] = read_obj(s, table);
2186 obj.v = list;
2187 obj.type = isl_obj_list;
2189 if (!list->obj[1].v)
2190 goto error;
2192 while (isl_stream_eat_if_available(s, ',')) {
2193 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2194 if (!obj.v)
2195 goto error;
2198 return obj;
2199 error:
2200 free_obj(obj);
2201 obj.type = isl_obj_none;
2202 obj.v = NULL;
2203 return obj;
2206 static struct isl_obj read_obj(struct isl_stream *s,
2207 struct isl_hash_table *table)
2209 struct isl_obj obj = { isl_obj_none, NULL };
2210 char *name = NULL;
2211 struct isc_un_op *op = NULL;
2213 obj = read_string_if_available(s);
2214 if (obj.v)
2215 return obj;
2216 obj = read_bool_if_available(s);
2217 if (obj.v)
2218 return obj;
2219 if (isl_stream_eat_if_available(s, '(')) {
2220 if (isl_stream_next_token_is(s, ')')) {
2221 obj.type = isl_obj_list;
2222 obj.v = isl_list_alloc(s->ctx, 0);
2223 } else {
2224 obj = read_expr(s, table);
2225 if (obj.v && isl_stream_eat_if_available(s, ','))
2226 obj = read_list(s, table, obj);
2228 if (!obj.v || isl_stream_eat(s, ')'))
2229 goto error;
2230 } else {
2231 op = read_prefix_un_op_if_available(s);
2232 if (op)
2233 return read_un_op_expr(s, table, op);
2235 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2236 return check_assert(s, table);
2237 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2238 return read_from_file(s);
2239 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2240 return write_to_file(s, table);
2241 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2242 return vertices(s, table);
2243 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2244 return any(s, table);
2245 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2246 return last(s, table);
2247 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2248 return schedule(s, table);
2249 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2250 return schedule_forest(s, table);
2251 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2252 return type_of(s, table);
2254 name = read_ident(s);
2255 if (name)
2256 obj = stored_obj(s->ctx, table, name);
2257 else
2258 obj = isl_stream_read_obj(s);
2259 if (!obj.v)
2260 goto error;
2263 if (isl_stream_eat_if_available(s, '^'))
2264 obj = power(s, obj);
2265 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2266 obj = obj_at_index(s, obj);
2267 else if (is_subtype(obj, isl_obj_union_map) &&
2268 isl_stream_eat_if_available(s, '(')) {
2269 obj = convert(s->ctx, obj, isl_obj_union_map);
2270 obj = apply(s, obj.v, table);
2271 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2272 isl_stream_eat_if_available(s, '(')) {
2273 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2274 obj = apply_fun(s, obj, table);
2275 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2276 isl_stream_eat_if_available(s, '(')) {
2277 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2278 obj = apply_fun(s, obj, table);
2281 return obj;
2282 error:
2283 free_obj(obj);
2284 obj.type = isl_obj_none;
2285 obj.v = NULL;
2286 return obj;
2289 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2290 struct isl_obj lhs, struct isl_obj rhs)
2292 int i;
2294 for (i = 0; ; ++i) {
2295 if (!bin_ops[i].op)
2296 break;
2297 if (bin_ops[i].op != like->op)
2298 continue;
2299 if (!is_subtype(lhs, bin_ops[i].lhs))
2300 continue;
2301 if (!is_subtype(rhs, bin_ops[i].rhs))
2302 continue;
2304 return &bin_ops[i];
2307 for (i = 0; ; ++i) {
2308 if (!named_bin_ops[i].name)
2309 break;
2310 if (named_bin_ops[i].op.op != like->op)
2311 continue;
2312 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2313 continue;
2314 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2315 continue;
2317 return &named_bin_ops[i].op;
2320 return NULL;
2323 static int next_is_neg_int(struct isl_stream *s)
2325 struct isl_token *tok;
2326 int ret;
2328 tok = isl_stream_next_token(s);
2329 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
2330 isl_stream_push_token(s, tok);
2332 return ret;
2335 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2336 struct isl_obj lhs, struct isl_obj rhs)
2338 struct isl_obj obj;
2340 lhs = convert(ctx, lhs, op->lhs);
2341 rhs = convert(ctx, rhs, op->rhs);
2342 if (op->res != isl_obj_bool)
2343 obj.v = op->o.fn(lhs.v, rhs.v);
2344 else {
2345 int res = op->o.test(lhs.v, rhs.v);
2346 free_obj(lhs);
2347 free_obj(rhs);
2348 obj.v = isl_bool_from_int(res);
2350 obj.type = op->res;
2352 return obj;
2355 static struct isl_obj read_expr(struct isl_stream *s,
2356 struct isl_hash_table *table)
2358 struct isl_obj obj = { isl_obj_none, NULL };
2359 struct isl_obj right_obj = { isl_obj_none, NULL };
2361 obj = read_obj(s, table);
2362 for (; obj.v;) {
2363 struct isc_bin_op *op = NULL;
2365 op = read_bin_op_if_available(s, obj);
2366 if (!op)
2367 break;
2369 right_obj = read_obj(s, table);
2371 op = find_matching_bin_op(op, obj, right_obj);
2373 if (!op)
2374 isl_die(s->ctx, isl_error_invalid,
2375 "no such binary operator defined on given operands",
2376 goto error);
2378 obj = call_bin_op(s->ctx, op, obj, right_obj);
2381 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
2382 right_obj = read_obj(s, table);
2383 obj.v = isl_int_obj_add(obj.v, right_obj.v);
2386 return obj;
2387 error:
2388 free_obj(right_obj);
2389 free_obj(obj);
2390 obj.type = isl_obj_none;
2391 obj.v = NULL;
2392 return obj;
2395 static __isl_give isl_printer *source_file(struct isl_stream *s,
2396 struct isl_hash_table *table, __isl_take isl_printer *p);
2398 static __isl_give isl_printer *read_line(struct isl_stream *s,
2399 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2401 struct isl_obj obj = { isl_obj_none, NULL };
2402 char *lhs = NULL;
2403 int assign = 0;
2404 int only_print = 0;
2405 struct isc_bin_op *op = NULL;
2406 char buf[30];
2408 if (!p)
2409 return NULL;
2410 if (isl_stream_is_empty(s))
2411 return p;
2413 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2414 return source_file(s, table, p);
2416 assign = is_assign(s);
2417 if (assign) {
2418 lhs = isl_stream_read_ident_if_available(s);
2419 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2420 goto error;
2421 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2422 only_print = 1;
2423 else if (!tty)
2424 only_print = 1;
2426 obj = read_expr(s, table);
2427 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2428 fprintf(stderr, "Interrupted\n");
2429 isl_ctx_reset_error(s->ctx);
2431 if (isl_stream_eat(s, ';'))
2432 goto error;
2434 if (only_print) {
2435 if (obj.type != isl_obj_none && obj.v != NULL) {
2436 p = obj.type->print(p, obj.v);
2437 p = isl_printer_end_line(p);
2439 free_obj(obj);
2440 return p;
2442 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2443 static int count = 0;
2444 snprintf(buf, sizeof(buf), "$%d", count++);
2445 lhs = strdup(buf + 1);
2447 p = isl_printer_print_str(p, buf);
2448 p = isl_printer_print_str(p, " := ");
2449 p = obj.type->print(p, obj.v);
2450 p = isl_printer_end_line(p);
2452 if (lhs && do_assign(s->ctx, table, lhs, obj))
2453 return p;
2455 return p;
2456 error:
2457 isl_stream_flush_tokens(s);
2458 isl_stream_skip_line(s);
2459 free(lhs);
2460 free_obj(obj);
2461 return p;
2464 int free_cb(void **entry, void *user)
2466 struct isl_named_obj *named = *entry;
2468 free_obj(named->obj);
2469 free(named->name);
2470 free(named);
2472 return 0;
2475 static void register_named_ops(struct isl_stream *s)
2477 int i;
2479 for (i = 0; i < ISCC_N_OP; ++i) {
2480 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2481 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2484 for (i = 0; ; ++i) {
2485 if (!named_un_ops[i].name)
2486 break;
2487 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2488 named_un_ops[i].name);
2489 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2492 for (i = 0; ; ++i) {
2493 if (!named_bin_ops[i].name)
2494 break;
2495 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2496 named_bin_ops[i].name);
2497 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2501 static __isl_give isl_printer *source_file(struct isl_stream *s,
2502 struct isl_hash_table *table, __isl_take isl_printer *p)
2504 struct isl_token *tok;
2505 struct isl_stream *s_file;
2506 FILE *file;
2508 tok = isl_stream_next_token(s);
2509 if (!tok || tok->type != ISL_TOKEN_STRING) {
2510 isl_stream_error(s, tok, "expecting filename");
2511 isl_token_free(tok);
2512 return p;
2515 file = fopen(tok->u.s, "r");
2516 isl_token_free(tok);
2517 isl_assert(s->ctx, file, return p);
2519 s_file = isl_stream_new_file(s->ctx, file);
2520 if (!s_file) {
2521 fclose(file);
2522 return p;
2525 register_named_ops(s_file);
2527 while (!s_file->eof)
2528 p = read_line(s_file, table, p, 0);
2530 isl_stream_free(s_file);
2531 fclose(file);
2533 isl_stream_eat(s, ';');
2535 return p;
2538 int main(int argc, char **argv)
2540 struct isl_ctx *ctx;
2541 struct isl_stream *s;
2542 struct isl_hash_table *table;
2543 struct iscc_options *options;
2544 isl_printer *p;
2545 int tty = isatty(0);
2547 options = iscc_options_new_with_defaults();
2548 assert(options);
2550 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2551 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2552 s = isl_stream_new_file(ctx, stdin);
2553 assert(s);
2554 table = isl_hash_table_alloc(ctx, 10);
2555 assert(table);
2556 p = isl_printer_to_file(ctx, stdout);
2557 p = isl_printer_set_output_format(p, options->format);
2558 assert(p);
2560 register_named_ops(s);
2562 install_signal_handler(ctx);
2564 while (p && !s->eof) {
2565 isl_ctx_resume(ctx);
2566 p = read_line(s, table, p, tty);
2569 remove_signal_handler(ctx);
2571 isl_printer_free(p);
2572 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2573 isl_hash_table_free(ctx, table);
2574 isl_stream_free(s);
2575 isl_ctx_free(ctx);
2577 return 0;