update isl for introduction of isl_stat
[barvinok.git] / iscc.c
blob8998c21cfde28d1bea5d921701af686cb8719807
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/aff.h>
7 #include <isl/obj.h>
8 #include <isl/stream.h>
9 #include <isl/set.h>
10 #include <isl/map.h>
11 #include <isl/vertices.h>
12 #include <isl/flow.h>
13 #include <isl/band.h>
14 #include <isl/schedule.h>
15 #include <isl/ast_build.h>
16 #include <isl_obj_list.h>
17 #include <isl_obj_str.h>
18 #include <barvinok/isl.h>
19 #include <barvinok/options.h>
20 #include "lattice_width.h"
22 #include "config.h"
24 #ifdef HAVE_SIGACTION
25 #include <signal.h>
27 static isl_ctx *main_ctx;
29 static void handler(int signum)
31 if (isl_ctx_aborted(main_ctx))
32 exit(EXIT_FAILURE);
33 isl_ctx_abort(main_ctx);
36 static struct sigaction sa_old;
38 static void install_signal_handler(isl_ctx *ctx)
40 struct sigaction sa;
42 main_ctx = ctx;
44 memset(&sa, 0, sizeof(struct sigaction));
45 sa.sa_handler = &handler;
46 sa.sa_flags = SA_RESTART;
47 sigaction(SIGINT, &sa, &sa_old);
50 static void remove_signal_handler(isl_ctx *ctx)
52 sigaction(SIGINT, &sa_old, NULL);
55 #else
57 static void install_signal_handler(isl_ctx *ctx)
61 static void remove_signal_handler(isl_ctx *ctx)
65 #endif
67 #ifdef HAVE_PET
68 #include <pet.h>
69 #else
70 struct pet_options;
71 int pet_options_set_autodetect(isl_ctx *ctx, int val)
73 return -1;
75 #endif
77 static int iscc_bool_false = 0;
78 static int iscc_bool_true = 1;
79 static int iscc_bool_error = -1;
81 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
82 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
83 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
84 ISCC_MINIMIZING, ISCC_RESPECTING,
85 ISCC_CODEGEN, ISCC_USING,
86 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
87 ISCC_N_OP };
88 static const char *op_name[ISCC_N_OP] = {
89 [ISCC_ASSERT] = "assert",
90 [ISCC_READ] = "read",
91 [ISCC_WRITE] = "write",
92 [ISCC_PRINT] = "print",
93 [ISCC_SOURCE] = "source",
94 [ISCC_VERTICES] = "vertices",
95 [ISCC_LAST] = "last",
96 [ISCC_ANY] = "any",
97 [ISCC_BEFORE] = "before",
98 [ISCC_UNDER] = "under",
99 [ISCC_SCHEDULE] = "schedule",
100 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
101 [ISCC_MINIMIZING] = "minimizing",
102 [ISCC_RESPECTING] = "respecting",
103 [ISCC_CODEGEN] = "codegen",
104 [ISCC_USING] = "using",
105 [ISCC_TYPEOF] = "typeof"
107 static enum isl_token_type iscc_op[ISCC_N_OP];
109 struct isl_arg_choice iscc_format[] = {
110 {"isl", ISL_FORMAT_ISL},
111 {"omega", ISL_FORMAT_OMEGA},
112 {"polylib", ISL_FORMAT_POLYLIB},
113 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
114 {"latex", ISL_FORMAT_LATEX},
115 {"C", ISL_FORMAT_C},
119 struct iscc_options {
120 struct barvinok_options *barvinok;
121 struct pet_options *pet;
122 unsigned format;
123 int io;
126 ISL_ARGS_START(struct iscc_options, iscc_options_args)
127 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
128 "barvinok options")
129 #ifdef HAVE_PET
130 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
131 #endif
132 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
133 iscc_format, ISL_FORMAT_ISL, "output format")
134 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
135 "allow read and write operations")
136 ISL_ARGS_END
138 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
139 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
141 static void *isl_obj_bool_copy(void *v)
143 return v;
146 static void isl_obj_bool_free(void *v)
150 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
151 void *v)
153 if (v == &iscc_bool_true)
154 return isl_printer_print_str(p, "True");
155 else if (v == &iscc_bool_false)
156 return isl_printer_print_str(p, "False");
157 else
158 return isl_printer_print_str(p, "Error");
161 static void *isl_obj_bool_add(void *v1, void *v2)
163 return v1;
166 struct isl_obj_vtable isl_obj_bool_vtable = {
167 isl_obj_bool_copy,
168 isl_obj_bool_add,
169 isl_obj_bool_print,
170 isl_obj_bool_free
172 #define isl_obj_bool (&isl_obj_bool_vtable)
174 int *iscc_bool_from_int(int res)
176 return res < 0 ? &iscc_bool_error :
177 res ? &iscc_bool_true : &iscc_bool_false;
180 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
181 __isl_take isl_union_map *map2)
183 return isl_union_map_is_subset(map2, map1);
185 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
186 __isl_take isl_union_set *set2)
188 return isl_union_set_is_subset(set2, set1);
191 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
192 __isl_take isl_union_map *map2)
194 return isl_union_map_is_strict_subset(map2, map1);
196 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
197 __isl_take isl_union_set *set2)
199 return isl_union_set_is_strict_subset(set2, set1);
202 extern struct isl_obj_vtable isl_obj_list_vtable;
203 #define isl_obj_list (&isl_obj_list_vtable)
205 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
206 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
207 struct isc_bin_op {
208 enum isl_token_type op;
209 isl_obj_type lhs;
210 isl_obj_type rhs;
211 isl_obj_type res;
212 union {
213 isc_bin_op_fn fn;
214 isc_bin_test_fn test;
215 } o;
217 struct isc_named_bin_op {
218 char *name;
219 struct isc_bin_op op;
222 struct iscc_at {
223 isl_union_pw_qpolynomial *upwqp;
224 isl_union_pw_qpolynomial *res;
227 static isl_stat eval_at(__isl_take isl_point *pnt, void *user)
229 struct iscc_at *at = (struct iscc_at *) user;
230 isl_val *v;
231 isl_qpolynomial *qp;
232 isl_set *set;
234 set = isl_set_from_point(isl_point_copy(pnt));
235 v = isl_union_pw_qpolynomial_eval(
236 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
237 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
239 at->res = isl_union_pw_qpolynomial_add(at->res,
240 isl_union_pw_qpolynomial_from_pw_qpolynomial(
241 isl_pw_qpolynomial_alloc(set, qp)));
243 return isl_stat_ok;
246 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
247 __isl_take isl_union_pw_qpolynomial *upwqp,
248 __isl_take isl_union_set *uset)
250 struct iscc_at at;
252 at.upwqp = upwqp;
253 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
255 isl_union_set_foreach_point(uset, eval_at, &at);
257 isl_union_pw_qpolynomial_free(upwqp);
258 isl_union_set_free(uset);
260 return at.res;
263 struct iscc_fold_at {
264 isl_union_pw_qpolynomial_fold *upwf;
265 isl_union_pw_qpolynomial *res;
268 static isl_stat eval_fold_at(__isl_take isl_point *pnt, void *user)
270 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
271 isl_val *v;
272 isl_qpolynomial *qp;
273 isl_set *set;
275 set = isl_set_from_point(isl_point_copy(pnt));
276 v = isl_union_pw_qpolynomial_fold_eval(
277 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
278 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
280 at->res = isl_union_pw_qpolynomial_add(at->res,
281 isl_union_pw_qpolynomial_from_pw_qpolynomial(
282 isl_pw_qpolynomial_alloc(set, qp)));
284 return isl_stat_ok;
287 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
288 __isl_take isl_union_pw_qpolynomial_fold *upwf,
289 __isl_take isl_union_set *uset)
291 struct iscc_fold_at at;
293 at.upwf = upwf;
294 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
296 isl_union_set_foreach_point(uset, eval_fold_at, &at);
298 isl_union_pw_qpolynomial_fold_free(upwf);
299 isl_union_set_free(uset);
301 return at.res;
304 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
305 __isl_take isl_union_pw_qpolynomial *upwqp,
306 __isl_take isl_union_pw_qpolynomial_fold *upwf)
308 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
309 upwqp);
312 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
313 __isl_take isl_union_map *umap,
314 __isl_take isl_union_pw_qpolynomial_fold *upwf)
316 isl_ctx *ctx;
317 struct isl_list *list;
318 int tight;
320 ctx = isl_union_map_get_ctx(umap);
321 list = isl_list_alloc(ctx, 2);
322 if (!list)
323 goto error2;
325 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
326 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
327 upwf, &tight);
328 list->obj[1].type = isl_obj_bool;
329 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
330 if (tight < 0 || !list->obj[0].v)
331 goto error;
333 return list;
334 error2:
335 isl_union_map_free(umap);
336 isl_union_pw_qpolynomial_fold_free(upwf);
337 error:
338 isl_list_free(list);
339 return NULL;
342 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
343 __isl_take isl_union_set *uset,
344 __isl_take isl_union_pw_qpolynomial_fold *upwf)
346 isl_ctx *ctx;
347 struct isl_list *list;
348 int tight;
350 ctx = isl_union_set_get_ctx(uset);
351 list = isl_list_alloc(ctx, 2);
352 if (!list)
353 goto error2;
355 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
356 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
357 upwf, &tight);
358 list->obj[1].type = isl_obj_bool;
359 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
360 if (tight < 0 || !list->obj[0].v)
361 goto error;
363 return list;
364 error2:
365 isl_union_set_free(uset);
366 isl_union_pw_qpolynomial_fold_free(upwf);
367 error:
368 isl_list_free(list);
369 return NULL;
372 static __isl_give isl_union_pw_qpolynomial *isl_val_mul_union_pw_qpolynomial(
373 __isl_take isl_val *v, __isl_take isl_union_pw_qpolynomial *upwqp)
375 return isl_union_pw_qpolynomial_scale_val(upwqp, v);
378 static __isl_give isl_union_pw_qpolynomial_fold *
379 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val *v,
380 __isl_take isl_union_pw_qpolynomial_fold *upwf)
382 return isl_union_pw_qpolynomial_fold_scale_val(upwf, v);
385 /* Are the two strings "str1" and "str2" equal to each other?
387 static int str_eq(__isl_keep isl_str *str1, __isl_keep isl_str *str2)
389 if (!str1 || !str2)
390 return -1;
392 return !strcmp(str1->s, str2->s);
395 struct isc_bin_op bin_ops[] = {
396 { '+', isl_obj_val, isl_obj_val, isl_obj_val,
397 (isc_bin_op_fn) &isl_val_add },
398 { '-', isl_obj_val, isl_obj_val, isl_obj_val,
399 (isc_bin_op_fn) &isl_val_sub },
400 { '*', isl_obj_val, isl_obj_val, isl_obj_val,
401 (isc_bin_op_fn) &isl_val_mul },
402 { '+', isl_obj_pw_multi_aff, isl_obj_pw_multi_aff,
403 isl_obj_pw_multi_aff,
404 (isc_bin_op_fn) &isl_pw_multi_aff_add },
405 { '+', isl_obj_union_set, isl_obj_union_set,
406 isl_obj_union_set,
407 (isc_bin_op_fn) &isl_union_set_union },
408 { '+', isl_obj_union_map, isl_obj_union_map,
409 isl_obj_union_map,
410 (isc_bin_op_fn) &isl_union_map_union },
411 { '-', isl_obj_union_set, isl_obj_union_set,
412 isl_obj_union_set,
413 (isc_bin_op_fn) &isl_union_set_subtract },
414 { '-', isl_obj_union_map, isl_obj_union_map,
415 isl_obj_union_map,
416 (isc_bin_op_fn) &isl_union_map_subtract },
417 { '*', isl_obj_union_set, isl_obj_union_set,
418 isl_obj_union_set,
419 (isc_bin_op_fn) &isl_union_set_intersect },
420 { '*', isl_obj_union_map, isl_obj_union_map,
421 isl_obj_union_map,
422 (isc_bin_op_fn) &isl_union_map_intersect },
423 { '*', isl_obj_union_map, isl_obj_union_set,
424 isl_obj_union_map,
425 (isc_bin_op_fn) &isl_union_map_intersect_domain },
426 { '.', isl_obj_union_map, isl_obj_union_map,
427 isl_obj_union_map,
428 (isc_bin_op_fn) &isl_union_map_apply_range },
429 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
430 isl_obj_union_pw_qpolynomial,
431 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
432 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
433 isl_obj_list,
434 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
435 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
436 isl_obj_union_map,
437 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
438 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
439 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
440 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
441 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
442 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
443 isl_obj_bool,
444 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
445 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
446 isl_obj_bool,
447 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
448 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
449 isl_obj_bool,
450 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
451 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
452 isl_obj_bool,
453 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
454 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
455 isl_obj_bool,
456 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
457 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
458 isl_obj_bool,
459 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
460 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
461 isl_obj_bool,
462 { .test =
463 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
464 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
465 isl_obj_bool,
466 { .test =
467 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
468 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
469 isl_obj_union_map,
470 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
471 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
472 isl_obj_union_map,
473 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
474 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
475 isl_obj_union_map,
476 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
477 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
478 isl_obj_union_map,
479 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
480 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
481 isl_obj_union_map,
482 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
483 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
484 isl_obj_union_map,
485 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
486 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
487 isl_obj_union_map,
488 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
489 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
490 isl_obj_union_map,
491 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
492 { '.', isl_obj_union_pw_qpolynomial_fold,
493 isl_obj_union_pw_qpolynomial_fold,
494 isl_obj_union_pw_qpolynomial_fold,
495 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
496 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
497 isl_obj_union_pw_qpolynomial,
498 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
499 { '+', isl_obj_union_pw_qpolynomial,
500 isl_obj_union_pw_qpolynomial_fold,
501 isl_obj_union_pw_qpolynomial_fold,
502 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
503 { '+', isl_obj_union_pw_qpolynomial_fold,
504 isl_obj_union_pw_qpolynomial,
505 isl_obj_union_pw_qpolynomial_fold,
506 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
507 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
508 isl_obj_union_pw_qpolynomial,
509 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
510 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
511 isl_obj_union_pw_qpolynomial,
512 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
513 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
514 isl_obj_union_pw_qpolynomial,
515 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
516 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
517 isl_obj_union_pw_qpolynomial_fold,
518 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
519 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
520 isl_obj_union_pw_qpolynomial_fold,
521 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
522 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
523 isl_obj_union_pw_qpolynomial,
524 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
525 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
526 isl_obj_union_pw_qpolynomial,
527 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
528 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
529 isl_obj_union_pw_qpolynomial_fold,
530 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
531 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
532 isl_obj_union_pw_qpolynomial,
533 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
534 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
535 isl_obj_union_pw_qpolynomial,
536 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
537 { '%', isl_obj_union_set, isl_obj_union_set,
538 isl_obj_union_set,
539 (isc_bin_op_fn) &isl_union_set_gist },
540 { '%', isl_obj_union_map, isl_obj_union_map,
541 isl_obj_union_map,
542 (isc_bin_op_fn) &isl_union_map_gist },
543 { '%', isl_obj_union_map, isl_obj_union_set,
544 isl_obj_union_map,
545 (isc_bin_op_fn) &isl_union_map_gist_domain },
546 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
547 isl_obj_union_pw_qpolynomial,
548 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
549 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
550 isl_obj_union_pw_qpolynomial_fold,
551 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
552 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
553 isl_obj_union_pw_qpolynomial, isl_obj_bool,
554 { .test = (isc_bin_test_fn)
555 &isl_union_pw_qpolynomial_plain_is_equal } },
556 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
557 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
558 { .test = (isc_bin_test_fn)
559 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
560 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
561 (isc_bin_op_fn) &isl_str_concat },
562 { '=', isl_obj_str, isl_obj_str, isl_obj_bool,
563 { .test = (isc_bin_test_fn) &str_eq } },
567 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
568 __isl_take isl_union_map *umap2)
570 return isl_union_map_apply_range(umap2, umap1);
573 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
574 __isl_take isl_union_pw_qpolynomial *upwqp,
575 __isl_take isl_union_map *umap)
577 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
580 static __isl_give struct isl_list *qpolynomial_fold_after_map(
581 __isl_take isl_union_pw_qpolynomial_fold *upwf,
582 __isl_take isl_union_map *umap)
584 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
587 struct isc_named_bin_op named_bin_ops[] = {
588 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
589 isl_obj_union_map,
590 (isc_bin_op_fn) &map_after_map } },
591 { "after", { -1, isl_obj_union_pw_qpolynomial,
592 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
593 (isc_bin_op_fn) &qpolynomial_after_map } },
594 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
595 isl_obj_union_map, isl_obj_list,
596 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
597 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
598 isl_obj_union_map,
599 (isc_bin_op_fn) &isl_union_map_apply_range } },
600 { "before", { -1, isl_obj_union_map,
601 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
602 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
603 { "before", { -1, isl_obj_union_map,
604 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
605 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
606 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
607 isl_obj_union_set,
608 (isc_bin_op_fn) &isl_union_set_product } },
609 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
610 isl_obj_union_map,
611 (isc_bin_op_fn) &isl_union_map_product } },
612 NULL
615 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
617 return isl_set_from_basic_set(isl_union_set_sample(uset));
620 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
622 return isl_map_from_basic_map(isl_union_map_sample(umap));
625 static __isl_give struct isl_list *union_map_power(
626 __isl_take isl_union_map *umap)
628 isl_ctx *ctx;
629 struct isl_list *list;
630 int exact;
632 ctx = isl_union_map_get_ctx(umap);
633 list = isl_list_alloc(ctx, 2);
634 if (!list)
635 goto error2;
637 list->obj[0].type = isl_obj_union_map;
638 list->obj[0].v = isl_union_map_power(umap, &exact);
639 list->obj[1].type = isl_obj_bool;
640 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
641 if (exact < 0 || !list->obj[0].v)
642 goto error;
644 return list;
645 error2:
646 isl_union_map_free(umap);
647 error:
648 isl_list_free(list);
649 return NULL;
652 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
653 __isl_take isl_union_pw_qpolynomial *upwqp)
655 isl_ctx *ctx;
656 struct isl_list *list;
657 int tight;
659 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
660 list = isl_list_alloc(ctx, 2);
661 if (!list)
662 goto error2;
664 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
665 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
666 isl_fold_max, &tight);
667 list->obj[1].type = isl_obj_bool;
668 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
669 if (tight < 0 || !list->obj[0].v)
670 goto error;
672 return list;
673 error2:
674 isl_union_pw_qpolynomial_free(upwqp);
675 error:
676 isl_list_free(list);
677 return NULL;
680 #ifdef HAVE_PET
681 static __isl_give isl_list *parse(__isl_take isl_str *str)
683 isl_ctx *ctx;
684 struct isl_list *list;
685 struct pet_scop *scop;
686 isl_union_map *sched, *may_reads, *must_writes, *may_writes;
687 isl_union_set *domain;
688 struct iscc_options *options;
690 if (!str)
691 return NULL;
692 ctx = str->ctx;
694 options = isl_ctx_peek_iscc_options(ctx);
695 if (!options || !options->io) {
696 isl_str_free(str);
697 isl_die(ctx, isl_error_invalid,
698 "parse_file operation not allowed", return NULL);
701 list = isl_list_alloc(ctx, 5);
702 if (!list)
703 goto error;
705 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
706 domain = pet_scop_collect_domains(scop);
707 sched = scop ? isl_schedule_get_map(scop->schedule) : NULL;
708 may_reads = pet_scop_collect_may_reads(scop);
709 may_writes = pet_scop_collect_may_writes(scop);
710 must_writes = pet_scop_collect_must_writes(scop);
711 pet_scop_free(scop);
713 list->obj[0].type = isl_obj_union_set;
714 list->obj[0].v = domain;
715 list->obj[1].type = isl_obj_union_map;
716 list->obj[1].v = must_writes;
717 list->obj[2].type = isl_obj_union_map;
718 list->obj[2].v = may_writes;
719 list->obj[3].type = isl_obj_union_map;
720 list->obj[3].v = may_reads;
721 list->obj[4].type = isl_obj_union_map;
722 list->obj[4].v = sched;
724 if (!list->obj[0].v || !list->obj[1].v ||
725 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
726 goto error;
728 isl_str_free(str);
729 return list;
730 error:
731 isl_list_free(list);
732 isl_str_free(str);
733 return NULL;
735 #endif
737 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
739 isl_union_set **scan = (isl_union_set **) user;
741 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
743 return isl_stat_ok;
746 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
748 isl_union_set *scan;
750 scan = isl_union_set_empty(isl_union_set_get_space(uset));
752 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
753 isl_union_set_free(scan);
754 return uset;
757 isl_union_set_free(uset);
758 return scan;
761 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
763 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
766 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
767 __isl_take isl_union_pw_qpolynomial *upwqp)
769 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
772 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
773 __isl_take isl_union_pw_qpolynomial *upwqp)
775 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
778 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
779 __isl_take isl_union_pw_qpolynomial *upwqp)
781 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
784 typedef void *(*isc_un_op_fn)(void *arg);
785 struct isc_un_op {
786 enum isl_token_type op;
787 isl_obj_type arg;
788 isl_obj_type res;
789 isc_un_op_fn fn;
791 struct isc_named_un_op {
792 char *name;
793 struct isc_un_op op;
795 struct isc_named_un_op named_un_ops[] = {
796 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
797 (isc_un_op_fn) &isl_union_map_affine_hull } },
798 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
799 (isc_un_op_fn) &isl_union_set_affine_hull } },
800 {"card", { -1, isl_obj_union_set,
801 isl_obj_union_pw_qpolynomial,
802 (isc_un_op_fn) &isl_union_set_card } },
803 {"card", { -1, isl_obj_union_map,
804 isl_obj_union_pw_qpolynomial,
805 (isc_un_op_fn) &isl_union_map_card } },
806 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
807 (isc_un_op_fn) &isl_union_set_coalesce } },
808 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
809 (isc_un_op_fn) &isl_union_map_coalesce } },
810 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
811 isl_obj_union_pw_qpolynomial,
812 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
813 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
814 isl_obj_union_pw_qpolynomial_fold,
815 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
816 {"coefficients", { -1, isl_obj_union_set,
817 isl_obj_union_set,
818 (isc_un_op_fn) &isl_union_set_coefficients } },
819 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
820 (isc_un_op_fn) &isl_union_set_solutions } },
821 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
822 (isc_un_op_fn) &isl_union_map_deltas } },
823 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
824 (isc_un_op_fn) &isl_union_map_deltas_map } },
825 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
826 (isc_un_op_fn) &isl_union_map_domain } },
827 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
828 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
829 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
830 isl_obj_union_set,
831 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
832 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
833 (isc_un_op_fn) &isl_union_map_domain } },
834 {"domain", { -1, isl_obj_union_pw_qpolynomial,
835 isl_obj_union_set,
836 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
837 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
838 isl_obj_union_set,
839 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
840 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
841 (isc_un_op_fn) &isl_union_map_domain_map } },
842 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
843 (isc_un_op_fn) &isl_union_map_range } },
844 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
845 (isc_un_op_fn) &isl_union_map_range } },
846 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
847 (isc_un_op_fn) &isl_union_map_range_map } },
848 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
849 (isc_un_op_fn) &isl_union_set_identity } },
850 {"lattice_width", { -1, isl_obj_union_set,
851 isl_obj_union_pw_qpolynomial,
852 (isc_un_op_fn) &isl_union_set_lattice_width } },
853 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
854 (isc_un_op_fn) &isl_union_map_lexmin } },
855 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
856 (isc_un_op_fn) &isl_union_map_lexmax } },
857 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
858 (isc_un_op_fn) &isl_union_set_lexmin } },
859 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
860 (isc_un_op_fn) &isl_union_set_lexmax } },
861 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
862 (isc_un_op_fn) &isl_union_set_lift } },
863 {"params", { -1, isl_obj_union_map, isl_obj_set,
864 (isc_un_op_fn) &isl_union_map_params } },
865 {"params", { -1, isl_obj_union_set, isl_obj_set,
866 (isc_un_op_fn) &isl_union_set_params } },
867 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
868 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
869 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
870 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
871 {"poly", { -1, isl_obj_union_pw_qpolynomial,
872 isl_obj_union_pw_qpolynomial,
873 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
874 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
875 isl_obj_union_pw_qpolynomial,
876 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
877 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
878 isl_obj_union_pw_qpolynomial,
879 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
880 #ifdef HAVE_PET
881 {"parse_file", { -1, isl_obj_str, isl_obj_list,
882 (isc_un_op_fn) &parse } },
883 #endif
884 {"pow", { -1, isl_obj_union_map, isl_obj_list,
885 (isc_un_op_fn) &union_map_power } },
886 {"sample", { -1, isl_obj_union_set, isl_obj_set,
887 (isc_un_op_fn) &union_set_sample } },
888 {"sample", { -1, isl_obj_union_map, isl_obj_map,
889 (isc_un_op_fn) &union_map_sample } },
890 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
891 (isc_un_op_fn) &union_set_scan } },
892 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
893 (isc_un_op_fn) &union_map_scan } },
894 {"sum", { -1, isl_obj_union_pw_qpolynomial,
895 isl_obj_union_pw_qpolynomial,
896 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
897 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
898 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
899 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
900 (isc_un_op_fn) &isl_union_set_unwrap } },
901 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
902 (isc_un_op_fn) &isl_union_map_wrap } },
903 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
904 (isc_un_op_fn) &isl_union_map_zip } },
905 NULL
908 struct isl_named_obj {
909 char *name;
910 struct isl_obj obj;
913 static void free_obj(struct isl_obj obj)
915 obj.type->free(obj.v);
918 static int same_name(const void *entry, const void *val)
920 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
922 return !strcmp(named->name, val);
925 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
926 char *name, struct isl_obj obj)
928 struct isl_hash_table_entry *entry;
929 uint32_t name_hash;
930 struct isl_named_obj *named;
932 name_hash = isl_hash_string(isl_hash_init(), name);
933 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
934 if (!entry)
935 goto error;
936 if (entry->data) {
937 named = entry->data;
938 free_obj(named->obj);
939 free(name);
940 } else {
941 named = isl_alloc_type(ctx, struct isl_named_obj);
942 if (!named)
943 goto error;
944 named->name = name;
945 entry->data = named;
947 named->obj = obj;
949 return 0;
950 error:
951 free_obj(obj);
952 free(name);
953 return -1;
956 static struct isl_obj stored_obj(struct isl_ctx *ctx,
957 struct isl_hash_table *table, char *name)
959 struct isl_obj obj = { isl_obj_none, NULL };
960 struct isl_hash_table_entry *entry;
961 uint32_t name_hash;
963 name_hash = isl_hash_string(isl_hash_init(), name);
964 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
965 if (entry) {
966 struct isl_named_obj *named;
967 named = entry->data;
968 obj = named->obj;
969 } else if (isdigit(name[0]))
970 fprintf(stderr, "unknown identifier '$%s'\n", name);
971 else
972 fprintf(stderr, "unknown identifier '%s'\n", name);
974 free(name);
975 obj.v = obj.type->copy(obj.v);
976 return obj;
979 static int is_subtype(struct isl_obj obj, isl_obj_type super)
981 if (obj.type == super)
982 return 1;
983 if (obj.type == isl_obj_map && super == isl_obj_union_map)
984 return 1;
985 if (obj.type == isl_obj_set && super == isl_obj_union_set)
986 return 1;
987 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
988 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
989 int is_set = isl_space_is_set(space);
990 isl_space_free(space);
991 return is_set;
993 if (obj.type == isl_obj_pw_qpolynomial &&
994 super == isl_obj_union_pw_qpolynomial)
995 return 1;
996 if (obj.type == isl_obj_pw_qpolynomial_fold &&
997 super == isl_obj_union_pw_qpolynomial_fold)
998 return 1;
999 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1000 return 1;
1001 if (obj.type == isl_obj_list) {
1002 struct isl_list *list = obj.v;
1003 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1004 return is_subtype(list->obj[0], super);
1006 if (super == isl_obj_str)
1007 return 1;
1008 return 0;
1011 static struct isl_obj obj_at(struct isl_obj obj, int i)
1013 struct isl_list *list = obj.v;
1015 obj = list->obj[i];
1016 obj.v = obj.type->copy(obj.v);
1018 isl_list_free(list);
1020 return obj;
1023 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1024 isl_obj_type type)
1026 if (obj.type == type)
1027 return obj;
1028 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1029 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1030 obj.type = isl_obj_union_set;
1031 obj.v = isl_union_set_from_set(set);
1032 return obj;
1034 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1035 obj.type = isl_obj_union_map;
1036 obj.v = isl_union_map_from_map(obj.v);
1037 return obj;
1039 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1040 obj.type = isl_obj_union_set;
1041 obj.v = isl_union_set_from_set(obj.v);
1042 return obj;
1044 if (obj.type == isl_obj_pw_qpolynomial &&
1045 type == isl_obj_union_pw_qpolynomial) {
1046 obj.type = isl_obj_union_pw_qpolynomial;
1047 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1048 return obj;
1050 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1051 type == isl_obj_union_pw_qpolynomial_fold) {
1052 obj.type = isl_obj_union_pw_qpolynomial_fold;
1053 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1054 return obj;
1056 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1057 if (type == isl_obj_union_map) {
1058 obj.type = isl_obj_union_map;
1059 return obj;
1061 if (type == isl_obj_union_pw_qpolynomial) {
1062 isl_space *dim = isl_union_set_get_space(obj.v);
1063 isl_union_set_free(obj.v);
1064 obj.v = isl_union_pw_qpolynomial_zero(dim);
1065 obj.type = isl_obj_union_pw_qpolynomial;
1066 return obj;
1068 if (type == isl_obj_union_pw_qpolynomial_fold) {
1069 isl_space *dim = isl_union_set_get_space(obj.v);
1070 isl_union_set_free(obj.v);
1071 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1072 isl_fold_list);
1073 obj.type = isl_obj_union_pw_qpolynomial_fold;
1074 return obj;
1077 if (obj.type == isl_obj_list) {
1078 struct isl_list *list = obj.v;
1079 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1080 return convert(ctx, obj_at(obj, 0), type);
1082 if (type == isl_obj_str) {
1083 isl_str *str;
1084 isl_printer *p;
1085 char *s;
1087 p = isl_printer_to_str(ctx);
1088 if (!p)
1089 goto error;
1090 p = obj.type->print(p, obj.v);
1091 s = isl_printer_get_str(p);
1092 isl_printer_free(p);
1094 str = isl_str_from_string(ctx, s);
1095 if (!str)
1096 goto error;
1097 free_obj(obj);
1098 obj.v = str;
1099 obj.type = isl_obj_str;
1100 return obj;
1103 error:
1104 free_obj(obj);
1105 obj.type = isl_obj_none;
1106 obj.v = NULL;
1107 return obj;
1110 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1111 struct isl_obj lhs)
1113 int i;
1114 struct isl_token *tok;
1116 tok = isl_stream_next_token(s);
1117 if (!tok)
1118 return NULL;
1120 for (i = 0; ; ++i) {
1121 if (!bin_ops[i].op)
1122 break;
1123 if (bin_ops[i].op != isl_token_get_type(tok))
1124 continue;
1125 if (!is_subtype(lhs, bin_ops[i].lhs))
1126 continue;
1128 isl_token_free(tok);
1129 return &bin_ops[i];
1132 for (i = 0; ; ++i) {
1133 if (!named_bin_ops[i].name)
1134 break;
1135 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1136 continue;
1137 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1138 continue;
1140 isl_token_free(tok);
1141 return &named_bin_ops[i].op;
1144 isl_stream_push_token(s, tok);
1146 return NULL;
1149 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1151 int i;
1152 struct isl_token *tok;
1154 tok = isl_stream_next_token(s);
1155 if (!tok)
1156 return NULL;
1158 for (i = 0; ; ++i) {
1159 if (!named_un_ops[i].name)
1160 break;
1161 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1162 continue;
1164 isl_token_free(tok);
1165 return &named_un_ops[i].op;
1168 isl_stream_push_token(s, tok);
1170 return NULL;
1173 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1174 struct isl_obj arg)
1176 int i;
1178 for (i = 0; ; ++i) {
1179 if (!named_un_ops[i].name)
1180 break;
1181 if (named_un_ops[i].op.op != like->op)
1182 continue;
1183 if (!is_subtype(arg, named_un_ops[i].op.arg))
1184 continue;
1186 return &named_un_ops[i].op;
1189 return NULL;
1192 static int is_assign(struct isl_stream *s)
1194 struct isl_token *tok;
1195 struct isl_token *tok2;
1196 int assign;
1198 tok = isl_stream_next_token(s);
1199 if (!tok)
1200 return 0;
1201 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1202 isl_stream_push_token(s, tok);
1203 return 0;
1206 tok2 = isl_stream_next_token(s);
1207 if (!tok2) {
1208 isl_stream_push_token(s, tok);
1209 return 0;
1211 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1212 isl_stream_push_token(s, tok2);
1213 isl_stream_push_token(s, tok);
1215 return assign;
1218 static struct isl_obj read_obj(struct isl_stream *s,
1219 struct isl_hash_table *table);
1220 static struct isl_obj read_expr(struct isl_stream *s,
1221 struct isl_hash_table *table);
1223 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1224 struct isl_hash_table *table, struct isc_un_op *op)
1226 isl_ctx *ctx;
1227 struct isl_obj obj = { isl_obj_none, NULL };
1229 obj = read_obj(s, table);
1230 if (!obj.v)
1231 goto error;
1233 op = find_matching_un_op(op, obj);
1235 ctx = isl_stream_get_ctx(s);
1236 if (!op)
1237 isl_die(ctx, isl_error_invalid,
1238 "no such unary operator defined on given operand",
1239 goto error);
1241 obj = convert(ctx, obj, op->arg);
1242 obj.v = op->fn(obj.v);
1243 obj.type = op->res;
1245 return obj;
1246 error:
1247 free_obj(obj);
1248 obj.type = isl_obj_none;
1249 obj.v = NULL;
1250 return obj;
1253 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1255 struct isl_list *list;
1256 int exact;
1258 if (obj.type != isl_obj_union_map)
1259 obj = convert(ctx, obj, isl_obj_union_map);
1260 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1261 list = isl_list_alloc(ctx, 2);
1262 if (!list)
1263 goto error;
1265 list->obj[0].type = isl_obj_union_map;
1266 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1267 list->obj[1].type = isl_obj_bool;
1268 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1269 obj.v = list;
1270 obj.type = isl_obj_list;
1271 if (exact < 0 || !list->obj[0].v)
1272 goto error;
1274 return obj;
1275 error:
1276 free_obj(obj);
1277 obj.type = isl_obj_none;
1278 obj.v = NULL;
1279 return obj;
1282 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1284 struct isl_list *list = obj.v;
1285 struct isl_token *tok;
1286 isl_ctx *ctx;
1287 isl_val *v;
1288 int i;
1290 tok = isl_stream_next_token(s);
1291 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1292 isl_stream_error(s, tok, "expecting index");
1293 if (tok)
1294 isl_stream_push_token(s, tok);
1295 goto error;
1297 ctx = isl_stream_get_ctx(s);
1298 v = isl_token_get_val(ctx, tok);
1299 i = isl_val_get_num_si(v);
1300 isl_val_free(v);
1301 isl_token_free(tok);
1302 isl_assert(ctx, i < list->n, goto error);
1303 if (isl_stream_eat(s, ']'))
1304 goto error;
1306 return obj_at(obj, i);
1307 error:
1308 free_obj(obj);
1309 obj.type = isl_obj_none;
1310 obj.v = NULL;
1311 return obj;
1314 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1315 struct isl_hash_table *table)
1317 isl_ctx *ctx;
1318 struct isl_obj obj;
1320 obj = read_expr(s, table);
1321 ctx = isl_stream_get_ctx(s);
1322 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1323 is_subtype(obj, isl_obj_union_map), goto error);
1325 if (obj.type == isl_obj_list) {
1326 struct isl_list *list = obj.v;
1327 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1328 obj = obj_at(obj, 0);
1330 if (obj.type == isl_obj_set)
1331 obj = convert(ctx, obj, isl_obj_union_set);
1332 else if (obj.type == isl_obj_map)
1333 obj = convert(ctx, obj, isl_obj_union_map);
1334 if (obj.type == isl_obj_union_set) {
1335 obj.v = isl_union_set_apply(obj.v, umap);
1336 } else
1337 obj.v = isl_union_map_apply_range(obj.v, umap);
1338 if (!obj.v)
1339 goto error2;
1341 if (isl_stream_eat(s, ')'))
1342 goto error2;
1344 return obj;
1345 error:
1346 isl_union_map_free(umap);
1347 error2:
1348 free_obj(obj);
1349 obj.type = isl_obj_none;
1350 obj.v = NULL;
1351 return obj;
1354 static struct isl_obj apply_fun_set(struct isl_obj obj,
1355 __isl_take isl_union_set *uset)
1357 if (obj.type == isl_obj_union_pw_qpolynomial) {
1358 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1359 } else {
1360 obj.type = isl_obj_list;
1361 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1363 return obj;
1366 static struct isl_obj apply_fun_map(struct isl_obj obj,
1367 __isl_take isl_union_map *umap)
1369 if (obj.type == isl_obj_union_pw_qpolynomial) {
1370 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1371 } else {
1372 obj.type = isl_obj_list;
1373 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1375 return obj;
1378 static struct isl_obj apply_fun(struct isl_stream *s,
1379 struct isl_obj obj, struct isl_hash_table *table)
1381 struct isl_obj arg;
1382 isl_ctx *ctx;
1384 arg = read_expr(s, table);
1385 ctx = isl_stream_get_ctx(s);
1386 if (!is_subtype(arg, isl_obj_union_map) &&
1387 !is_subtype(arg, isl_obj_union_set))
1388 isl_die(ctx, isl_error_invalid,
1389 "expecting set of map argument", goto error);
1391 if (arg.type == isl_obj_list) {
1392 struct isl_list *list = arg.v;
1393 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1394 arg = obj_at(arg, 0);
1396 if (arg.type == isl_obj_set)
1397 arg = convert(ctx, arg, isl_obj_union_set);
1398 else if (arg.type == isl_obj_map)
1399 arg = convert(ctx, arg, isl_obj_union_map);
1400 if (arg.type == isl_obj_union_set)
1401 obj = apply_fun_set(obj, arg.v);
1402 else
1403 obj = apply_fun_map(obj, arg.v);
1404 if (!obj.v)
1405 goto error2;
1407 if (isl_stream_eat(s, ')'))
1408 goto error2;
1410 return obj;
1411 error:
1412 free_obj(arg);
1413 error2:
1414 free_obj(obj);
1415 obj.type = isl_obj_none;
1416 obj.v = NULL;
1417 return obj;
1420 struct add_vertex_data {
1421 struct isl_list *list;
1422 int i;
1425 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1427 struct add_vertex_data *data = (struct add_vertex_data *)user;
1428 isl_multi_aff *ma;
1429 isl_set *dom;
1431 ma = isl_vertex_get_expr(vertex);
1432 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1434 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1435 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1436 data->i++;
1438 isl_vertex_free(vertex);
1440 return isl_stat_ok;
1443 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1445 isl_ctx *ctx;
1446 isl_basic_set *hull;
1447 isl_vertices *vertices = NULL;
1448 struct isl_list *list = NULL;
1449 isl_stat r;
1450 struct add_vertex_data *data = (struct add_vertex_data *)user;
1452 set = isl_set_remove_divs(set);
1453 hull = isl_set_convex_hull(set);
1454 vertices = isl_basic_set_compute_vertices(hull);
1455 isl_basic_set_free(hull);
1457 list = data->list;
1459 ctx = isl_vertices_get_ctx(vertices);
1460 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1461 if (!data->list)
1462 goto error;
1464 data->i = 0;
1465 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1467 data->list = isl_list_concat(list, data->list);
1469 isl_vertices_free(vertices);
1471 return r;
1472 error:
1473 data->list = list;
1474 isl_vertices_free(vertices);
1475 return isl_stat_error;
1478 static struct isl_obj vertices(struct isl_stream *s,
1479 struct isl_hash_table *table)
1481 isl_ctx *ctx;
1482 struct isl_obj obj;
1483 struct isl_list *list = NULL;
1484 isl_union_set *uset = NULL;
1485 struct add_vertex_data data = { NULL };
1487 obj = read_expr(s, table);
1488 ctx = isl_stream_get_ctx(s);
1489 obj = convert(ctx, obj, isl_obj_union_set);
1490 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1491 uset = obj.v;
1492 obj.v = NULL;
1494 list = isl_list_alloc(ctx, 0);
1495 if (!list)
1496 goto error;
1498 data.list = list;
1500 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1501 goto error;
1503 isl_union_set_free(uset);
1505 obj.type = isl_obj_list;
1506 obj.v = data.list;
1508 return obj;
1509 error:
1510 isl_union_set_free(uset);
1511 isl_list_free(data.list);
1512 free_obj(obj);
1513 obj.type = isl_obj_none;
1514 obj.v = NULL;
1515 return obj;
1518 static struct isl_obj type_of(struct isl_stream *s,
1519 struct isl_hash_table *table)
1521 isl_ctx *ctx;
1522 struct isl_obj obj;
1523 const char *type = "unknown";
1525 obj = read_expr(s, table);
1527 if (obj.type == isl_obj_map ||
1528 obj.type == isl_obj_union_map)
1529 type = "map";
1530 if (obj.type == isl_obj_set ||
1531 obj.type == isl_obj_union_set)
1532 type = "set";
1533 if (obj.type == isl_obj_pw_multi_aff)
1534 type = "piecewise multi-quasiaffine expression";
1535 if (obj.type == isl_obj_pw_qpolynomial ||
1536 obj.type == isl_obj_union_pw_qpolynomial)
1537 type = "piecewise quasipolynomial";
1538 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1539 obj.type == isl_obj_union_pw_qpolynomial_fold)
1540 type = "piecewise quasipolynomial fold";
1541 if (obj.type == isl_obj_list)
1542 type = "list";
1543 if (obj.type == isl_obj_bool)
1544 type = "boolean";
1545 if (obj.type == isl_obj_str)
1546 type = "string";
1547 if (obj.type == isl_obj_val)
1548 type = "value";
1550 free_obj(obj);
1551 obj.type = isl_obj_str;
1552 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1554 return obj;
1557 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1558 struct isl_hash_table *table)
1560 struct isl_obj obj;
1561 isl_ctx *ctx;
1563 obj = read_obj(s, table);
1564 ctx = isl_stream_get_ctx(s);
1565 obj = convert(ctx, obj, isl_obj_union_set);
1566 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1567 return obj.v;
1568 error:
1569 free_obj(obj);
1570 return NULL;
1573 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1574 struct isl_hash_table *table)
1576 struct isl_obj obj;
1577 isl_ctx *ctx;
1579 obj = read_obj(s, table);
1580 ctx = isl_stream_get_ctx(s);
1581 obj = convert(ctx, obj, isl_obj_union_map);
1582 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1583 return obj.v;
1584 error:
1585 free_obj(obj);
1586 return NULL;
1589 static struct isl_obj last_any(struct isl_stream *s,
1590 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1591 __isl_take isl_union_map *may_source)
1593 struct isl_obj obj = { isl_obj_none, NULL };
1594 isl_union_map *sink = NULL;
1595 isl_union_map *schedule = NULL;
1596 isl_union_map *may_dep;
1597 isl_union_map *must_dep;
1599 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1600 goto error;
1602 sink = read_map(s, table);
1603 if (!sink)
1604 goto error;
1606 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1607 goto error;
1609 schedule = read_map(s, table);
1610 if (!schedule)
1611 goto error;
1613 if (isl_union_map_compute_flow(sink, must_source, may_source,
1614 schedule, &must_dep, &may_dep,
1615 NULL, NULL) < 0)
1616 return obj;
1618 obj.type = isl_obj_union_map;
1619 obj.v = isl_union_map_union(must_dep, may_dep);
1621 return obj;
1622 error:
1623 isl_union_map_free(may_source);
1624 isl_union_map_free(must_source);
1625 isl_union_map_free(sink);
1626 isl_union_map_free(schedule);
1627 free_obj(obj);
1628 obj.type = isl_obj_none;
1629 obj.v = NULL;
1630 return obj;
1633 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1635 struct isl_obj obj = { isl_obj_none, NULL };
1636 isl_union_map *must_source = NULL;
1637 isl_union_map *may_source = NULL;
1638 isl_union_map *sink = NULL;
1639 isl_union_map *schedule = NULL;
1640 isl_union_map *may_dep;
1642 may_source = read_map(s, table);
1643 if (!may_source)
1644 goto error;
1646 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1647 must_source = read_map(s, table);
1648 if (!must_source)
1649 goto error;
1650 return last_any(s, table, must_source, may_source);
1653 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1654 goto error;
1656 sink = read_map(s, table);
1657 if (!sink)
1658 goto error;
1660 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1661 goto error;
1663 schedule = read_map(s, table);
1664 if (!schedule)
1665 goto error;
1667 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1668 if (isl_union_map_compute_flow(sink, must_source, may_source,
1669 schedule, NULL, &may_dep,
1670 NULL, NULL) < 0)
1671 return obj;
1673 obj.type = isl_obj_union_map;
1674 obj.v = may_dep;
1676 return obj;
1677 error:
1678 isl_union_map_free(may_source);
1679 isl_union_map_free(must_source);
1680 isl_union_map_free(sink);
1681 isl_union_map_free(schedule);
1682 free_obj(obj);
1683 obj.type = isl_obj_none;
1684 obj.v = NULL;
1685 return obj;
1688 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1690 struct isl_obj obj = { isl_obj_none, NULL };
1691 struct isl_list *list = NULL;
1692 isl_union_map *must_source = NULL;
1693 isl_union_map *may_source = NULL;
1694 isl_union_map *sink = NULL;
1695 isl_union_map *schedule = NULL;
1696 isl_union_map *must_dep;
1697 isl_union_map *must_no_source;
1699 must_source = read_map(s, table);
1700 if (!must_source)
1701 goto error;
1703 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1704 may_source = read_map(s, table);
1705 if (!may_source)
1706 goto error;
1707 return last_any(s, table, must_source, may_source);
1710 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1711 if (!list)
1712 goto error;
1714 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1715 goto error;
1717 sink = read_map(s, table);
1718 if (!sink)
1719 goto error;
1721 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1722 goto error;
1724 schedule = read_map(s, table);
1725 if (!schedule)
1726 goto error;
1728 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1729 if (isl_union_map_compute_flow(sink, must_source, may_source,
1730 schedule, &must_dep, NULL,
1731 &must_no_source, NULL) < 0) {
1732 isl_list_free(list);
1733 return obj;
1736 list->obj[0].type = isl_obj_union_map;
1737 list->obj[0].v = must_dep;
1738 list->obj[1].type = isl_obj_union_map;
1739 list->obj[1].v = must_no_source;
1741 obj.v = list;
1742 obj.type = isl_obj_list;
1744 return obj;
1745 error:
1746 isl_list_free(list);
1747 isl_union_map_free(may_source);
1748 isl_union_map_free(must_source);
1749 isl_union_map_free(sink);
1750 isl_union_map_free(schedule);
1751 free_obj(obj);
1752 obj.type = isl_obj_none;
1753 obj.v = NULL;
1754 return obj;
1757 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1758 struct isl_hash_table *table)
1760 isl_union_set *domain;
1761 isl_union_map *validity;
1762 isl_union_map *proximity;
1764 domain = read_set(s, table);
1765 if (!domain)
1766 return NULL;
1768 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1769 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1771 for (;;) {
1772 isl_union_map *umap;
1773 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1774 umap = read_map(s, table);
1775 validity = isl_union_map_union(validity, umap);
1776 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1777 umap = read_map(s, table);
1778 proximity = isl_union_map_union(proximity, umap);
1779 } else
1780 break;
1783 return isl_union_set_compute_schedule(domain, validity, proximity);
1786 static struct isl_obj schedule(struct isl_stream *s,
1787 struct isl_hash_table *table)
1789 struct isl_obj obj = { isl_obj_none, NULL };
1790 isl_schedule *schedule;
1792 schedule = get_schedule(s, table);
1794 obj.v = isl_schedule_get_map(schedule);
1795 obj.type = isl_obj_union_map;
1797 isl_schedule_free(schedule);
1799 return obj;
1802 /* Read a schedule for code generation.
1803 * If the input is a set rather than a map, then we construct
1804 * an identity schedule on the given set.
1806 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1807 struct isl_hash_table *table)
1809 struct isl_obj obj;
1810 isl_ctx *ctx;
1812 obj = read_obj(s, table);
1813 ctx = isl_stream_get_ctx(s);
1815 if (is_subtype(obj, isl_obj_union_map)) {
1816 obj = convert(ctx, obj, isl_obj_union_map);
1817 return obj.v;
1820 if (is_subtype(obj, isl_obj_union_set)) {
1821 obj = convert(ctx, obj, isl_obj_union_set);
1822 return isl_union_set_identity(obj.v);
1825 free_obj(obj);
1826 isl_die(ctx, isl_error_invalid, "expecting set or map", return NULL);
1829 /* Generate an AST for the given schedule and options and print
1830 * the AST on the printer.
1832 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1833 __isl_take isl_union_map *schedule,
1834 __isl_take isl_union_map *options)
1836 isl_space *space;
1837 isl_set *context;
1838 isl_ast_build *build;
1839 isl_ast_node *tree;
1840 int format;
1842 space = isl_union_map_get_space(schedule);
1843 context = isl_set_universe(isl_space_params(space));
1845 build = isl_ast_build_from_context(context);
1846 build = isl_ast_build_set_options(build, options);
1847 tree = isl_ast_build_ast_from_schedule(build, schedule);
1848 isl_ast_build_free(build);
1850 if (!tree)
1851 return p;
1853 format = isl_printer_get_output_format(p);
1854 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1855 p = isl_printer_print_ast_node(p, tree);
1856 p = isl_printer_set_output_format(p, format);
1858 isl_ast_node_free(tree);
1860 return p;
1863 /* Perform the codegen operation.
1864 * In particular, read a schedule, check if the user has specified any options
1865 * and then generate an AST from the schedule (and options) and print it.
1867 static __isl_give isl_printer *codegen(struct isl_stream *s,
1868 struct isl_hash_table *table, __isl_take isl_printer *p)
1870 isl_union_map *schedule;
1871 isl_union_map *options;
1873 schedule = get_codegen_schedule(s, table);
1874 if (!schedule)
1875 return p;
1877 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1878 options = read_map(s, table);
1879 else
1880 options = isl_union_map_empty(
1881 isl_union_map_get_space(schedule));
1883 p = print_code(p, schedule, options);
1885 isl_stream_eat(s, ';');
1887 return p;
1890 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1892 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1894 struct isl_obj obj = { isl_obj_none, NULL };
1895 isl_ctx *ctx = isl_band_get_ctx(band);
1896 struct isl_list *list;
1898 list = isl_list_alloc(ctx, 2);
1899 if (!list)
1900 goto error;
1902 obj.v = list;
1903 obj.type = isl_obj_list;
1905 list->obj[0].type = isl_obj_union_map;
1906 list->obj[0].v = isl_band_get_partial_schedule(band);
1908 if (isl_band_has_children(band)) {
1909 isl_band_list *children;
1911 children = isl_band_get_children(band);
1912 list->obj[1] = band_list_to_obj_list(children);
1913 } else {
1914 list->obj[1].type = isl_obj_list;
1915 list->obj[1].v = isl_list_alloc(ctx, 0);
1918 if (!list->obj[0].v || !list->obj[1].v)
1919 goto error;
1921 isl_band_free(band);
1923 return obj;
1924 error:
1925 isl_band_free(band);
1926 free_obj(obj);
1927 obj.type = isl_obj_none;
1928 obj.v = NULL;
1929 return obj;
1932 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1934 struct isl_obj obj = { isl_obj_none, NULL };
1935 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1936 struct isl_list *list;
1937 int i, n;
1939 n = isl_band_list_n_band(bands);
1940 list = isl_list_alloc(ctx, n);
1941 if (!list)
1942 goto error;
1944 obj.v = list;
1945 obj.type = isl_obj_list;
1947 for (i = 0; i < n; ++i) {
1948 isl_band *band;
1950 band = isl_band_list_get_band(bands, i);
1951 list->obj[i] = band_to_obj_list(band);
1952 if (!list->obj[i].v)
1953 goto error;
1956 isl_band_list_free(bands);
1958 return obj;
1959 error:
1960 isl_band_list_free(bands);
1961 free_obj(obj);
1962 obj.type = isl_obj_none;
1963 obj.v = NULL;
1964 return obj;
1967 static struct isl_obj schedule_forest(struct isl_stream *s,
1968 struct isl_hash_table *table)
1970 struct isl_obj obj = { isl_obj_none, NULL };
1971 isl_schedule *schedule;
1972 isl_band_list *roots;
1974 schedule = get_schedule(s, table);
1975 if (!schedule)
1976 return obj;
1978 roots = isl_schedule_get_band_forest(schedule);
1979 isl_schedule_free(schedule);
1981 return band_list_to_obj_list(roots);
1984 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1986 struct isl_token *tok;
1987 isl_ctx *ctx;
1988 isl_val *v;
1990 ctx = isl_stream_get_ctx(s);
1991 if (isl_stream_eat_if_available(s, '+'))
1992 return transitive_closure(ctx, obj);
1994 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
1995 if (obj.type != isl_obj_union_map)
1996 obj = convert(ctx, obj, isl_obj_union_map);
1998 tok = isl_stream_next_token(s);
1999 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2000 isl_stream_error(s, tok, "expecting integer exponent");
2001 if (tok)
2002 isl_stream_push_token(s, tok);
2003 goto error;
2006 v = isl_token_get_val(ctx, tok);
2007 if (isl_val_is_zero(v)) {
2008 isl_stream_error(s, tok, "expecting non-zero exponent");
2009 isl_val_free(v);
2010 if (tok)
2011 isl_stream_push_token(s, tok);
2012 goto error;
2015 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2016 isl_token_free(tok);
2017 if (!obj.v)
2018 goto error;
2020 return obj;
2021 error:
2022 free_obj(obj);
2023 obj.type = isl_obj_none;
2024 obj.v = NULL;
2025 return obj;
2028 static struct isl_obj check_assert(struct isl_stream *s,
2029 struct isl_hash_table *table)
2031 struct isl_obj obj;
2032 isl_ctx *ctx;
2034 obj = read_expr(s, table);
2035 ctx = isl_stream_get_ctx(s);
2036 if (obj.type != isl_obj_bool)
2037 isl_die(ctx, isl_error_invalid,
2038 "expecting boolean expression", goto error);
2039 if (obj.v != &iscc_bool_true)
2040 isl_die(ctx, isl_error_unknown,
2041 "assertion failed", abort());
2042 error:
2043 free_obj(obj);
2044 obj.type = isl_obj_none;
2045 obj.v = NULL;
2046 return obj;
2049 static struct isl_obj read_from_file(struct isl_stream *s)
2051 isl_ctx *ctx;
2052 struct isl_obj obj;
2053 struct isl_token *tok;
2054 struct isl_stream *s_file;
2055 struct iscc_options *options;
2056 char *name;
2057 FILE *file;
2059 tok = isl_stream_next_token(s);
2060 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2061 isl_stream_error(s, tok, "expecting filename");
2062 isl_token_free(tok);
2063 goto error;
2066 ctx = isl_stream_get_ctx(s);
2067 options = isl_ctx_peek_iscc_options(ctx);
2068 if (!options || !options->io) {
2069 isl_token_free(tok);
2070 isl_die(ctx, isl_error_invalid,
2071 "read operation not allowed", goto error);
2074 name = isl_token_get_str(ctx, tok);
2075 isl_token_free(tok);
2076 file = fopen(name, "r");
2077 free(name);
2078 isl_assert(ctx, file, goto error);
2080 s_file = isl_stream_new_file(ctx, file);
2081 if (!s_file) {
2082 fclose(file);
2083 goto error;
2086 obj = isl_stream_read_obj(s_file);
2088 isl_stream_free(s_file);
2089 fclose(file);
2091 return obj;
2092 error:
2093 obj.type = isl_obj_none;
2094 obj.v = NULL;
2095 return obj;
2098 static struct isl_obj write_to_file(struct isl_stream *s,
2099 struct isl_hash_table *table)
2101 struct isl_obj obj;
2102 struct isl_token *tok;
2103 struct isl_stream *s_file;
2104 struct iscc_options *options;
2105 char *name;
2106 FILE *file;
2107 isl_ctx *ctx;
2108 isl_printer *p;
2110 tok = isl_stream_next_token(s);
2111 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2112 isl_stream_error(s, tok, "expecting filename");
2113 isl_token_free(tok);
2114 goto error;
2117 obj = read_expr(s, table);
2119 ctx = isl_stream_get_ctx(s);
2120 options = isl_ctx_peek_iscc_options(ctx);
2121 if (!options || !options->io) {
2122 isl_token_free(tok);
2123 isl_die(ctx, isl_error_invalid,
2124 "write operation not allowed", goto error);
2127 name = isl_token_get_str(ctx, tok);
2128 isl_token_free(tok);
2129 file = fopen(name, "w");
2130 free(name);
2131 if (!file)
2132 isl_die(ctx, isl_error_unknown,
2133 "could not open file for writing", goto error);
2135 p = isl_printer_to_file(ctx, file);
2136 p = isl_printer_set_output_format(p, options->format);
2137 p = obj.type->print(p, obj.v);
2138 p = isl_printer_end_line(p);
2139 isl_printer_free(p);
2141 fclose(file);
2142 error:
2143 free_obj(obj);
2144 obj.type = isl_obj_none;
2145 obj.v = NULL;
2146 return obj;
2149 static struct isl_obj read_string_if_available(struct isl_stream *s)
2151 struct isl_token *tok;
2152 struct isl_obj obj = { isl_obj_none, NULL };
2154 tok = isl_stream_next_token(s);
2155 if (!tok)
2156 return obj;
2157 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2158 isl_str *str;
2159 str = isl_str_alloc(isl_stream_get_ctx(s));
2160 if (!str)
2161 goto error;
2162 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2163 isl_token_free(tok);
2164 obj.v = str;
2165 obj.type = isl_obj_str;
2166 } else
2167 isl_stream_push_token(s, tok);
2168 return obj;
2169 error:
2170 isl_token_free(tok);
2171 return obj;
2174 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2176 struct isl_token *tok;
2177 struct isl_obj obj = { isl_obj_none, NULL };
2178 int type;
2180 tok = isl_stream_next_token(s);
2181 if (!tok)
2182 return obj;
2183 type = isl_token_get_type(tok);
2184 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2185 int is_true = type == ISL_TOKEN_TRUE;
2186 isl_token_free(tok);
2187 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2188 obj.type = isl_obj_bool;
2189 } else
2190 isl_stream_push_token(s, tok);
2191 return obj;
2194 static __isl_give char *read_ident(struct isl_stream *s)
2196 char *name;
2197 isl_val *v;
2198 struct isl_token *tok, *tok2;
2200 name = isl_stream_read_ident_if_available(s);
2201 if (name)
2202 return name;
2204 tok = isl_stream_next_token(s);
2205 if (!tok)
2206 return NULL;
2207 if (isl_token_get_type(tok) != '$') {
2208 isl_stream_push_token(s, tok);
2209 return NULL;
2211 tok2 = isl_stream_next_token(s);
2212 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2213 if (tok2)
2214 isl_stream_push_token(s, tok2);
2215 isl_stream_push_token(s, tok);
2216 return NULL;
2219 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2220 name = isl_val_to_str(v);
2221 isl_val_free(v);
2222 isl_token_free(tok);
2223 isl_token_free(tok2);
2225 return name;
2228 static struct isl_obj read_list(struct isl_stream *s,
2229 struct isl_hash_table *table, struct isl_obj obj)
2231 struct isl_list *list;
2233 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2234 if (!list)
2235 goto error;
2236 list->obj[0] = obj;
2237 list->obj[1] = read_obj(s, table);
2238 obj.v = list;
2239 obj.type = isl_obj_list;
2241 if (!list->obj[1].v)
2242 goto error;
2244 while (isl_stream_eat_if_available(s, ',')) {
2245 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2246 if (!obj.v)
2247 goto error;
2250 return obj;
2251 error:
2252 free_obj(obj);
2253 obj.type = isl_obj_none;
2254 obj.v = NULL;
2255 return obj;
2258 static struct isl_obj read_obj(struct isl_stream *s,
2259 struct isl_hash_table *table)
2261 isl_ctx *ctx;
2262 struct isl_obj obj = { isl_obj_none, NULL };
2263 char *name = NULL;
2264 struct isc_un_op *op = NULL;
2266 obj = read_string_if_available(s);
2267 if (obj.v)
2268 return obj;
2269 obj = read_bool_if_available(s);
2270 if (obj.v)
2271 return obj;
2272 ctx = isl_stream_get_ctx(s);
2273 if (isl_stream_eat_if_available(s, '(')) {
2274 if (isl_stream_next_token_is(s, ')')) {
2275 obj.type = isl_obj_list;
2276 obj.v = isl_list_alloc(ctx, 0);
2277 } else {
2278 obj = read_expr(s, table);
2279 if (obj.v && isl_stream_eat_if_available(s, ','))
2280 obj = read_list(s, table, obj);
2282 if (!obj.v || isl_stream_eat(s, ')'))
2283 goto error;
2284 } else {
2285 op = read_prefix_un_op_if_available(s);
2286 if (op)
2287 return read_un_op_expr(s, table, op);
2289 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2290 return check_assert(s, table);
2291 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2292 return read_from_file(s);
2293 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2294 return write_to_file(s, table);
2295 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2296 return vertices(s, table);
2297 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2298 return any(s, table);
2299 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2300 return last(s, table);
2301 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2302 return schedule(s, table);
2303 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2304 return schedule_forest(s, table);
2305 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2306 return type_of(s, table);
2308 name = read_ident(s);
2309 if (name)
2310 obj = stored_obj(ctx, table, name);
2311 else
2312 obj = isl_stream_read_obj(s);
2313 if (!obj.v)
2314 goto error;
2317 if (isl_stream_eat_if_available(s, '^'))
2318 obj = power(s, obj);
2319 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2320 obj = obj_at_index(s, obj);
2321 else if (is_subtype(obj, isl_obj_union_map) &&
2322 isl_stream_eat_if_available(s, '(')) {
2323 obj = convert(ctx, obj, isl_obj_union_map);
2324 obj = apply(s, obj.v, table);
2325 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2326 isl_stream_eat_if_available(s, '(')) {
2327 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2328 obj = apply_fun(s, obj, table);
2329 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2330 isl_stream_eat_if_available(s, '(')) {
2331 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2332 obj = apply_fun(s, obj, table);
2335 return obj;
2336 error:
2337 free_obj(obj);
2338 obj.type = isl_obj_none;
2339 obj.v = NULL;
2340 return obj;
2343 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2344 struct isl_obj lhs, struct isl_obj rhs)
2346 int i;
2348 for (i = 0; ; ++i) {
2349 if (!bin_ops[i].op)
2350 break;
2351 if (bin_ops[i].op != like->op)
2352 continue;
2353 if (!is_subtype(lhs, bin_ops[i].lhs))
2354 continue;
2355 if (!is_subtype(rhs, bin_ops[i].rhs))
2356 continue;
2358 return &bin_ops[i];
2361 for (i = 0; ; ++i) {
2362 if (!named_bin_ops[i].name)
2363 break;
2364 if (named_bin_ops[i].op.op != like->op)
2365 continue;
2366 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2367 continue;
2368 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2369 continue;
2371 return &named_bin_ops[i].op;
2374 return NULL;
2377 static int next_is_neg_int(struct isl_stream *s)
2379 struct isl_token *tok;
2380 int ret;
2382 tok = isl_stream_next_token(s);
2383 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2384 isl_val *v;
2385 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2386 ret = isl_val_is_neg(v);
2387 isl_val_free(v);
2388 } else
2389 ret = 0;
2390 isl_stream_push_token(s, tok);
2392 return ret;
2395 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2396 struct isl_obj lhs, struct isl_obj rhs)
2398 struct isl_obj obj;
2400 lhs = convert(ctx, lhs, op->lhs);
2401 rhs = convert(ctx, rhs, op->rhs);
2402 if (op->res != isl_obj_bool)
2403 obj.v = op->o.fn(lhs.v, rhs.v);
2404 else {
2405 int res = op->o.test(lhs.v, rhs.v);
2406 free_obj(lhs);
2407 free_obj(rhs);
2408 obj.v = iscc_bool_from_int(res);
2410 obj.type = op->res;
2412 return obj;
2415 static struct isl_obj read_expr(struct isl_stream *s,
2416 struct isl_hash_table *table)
2418 isl_ctx *ctx;
2419 struct isl_obj obj = { isl_obj_none, NULL };
2420 struct isl_obj right_obj = { isl_obj_none, NULL };
2422 obj = read_obj(s, table);
2423 ctx = isl_stream_get_ctx(s);
2424 for (; obj.v;) {
2425 struct isc_bin_op *op = NULL;
2427 op = read_bin_op_if_available(s, obj);
2428 if (!op)
2429 break;
2431 right_obj = read_obj(s, table);
2433 op = find_matching_bin_op(op, obj, right_obj);
2435 if (!op)
2436 isl_die(ctx, isl_error_invalid,
2437 "no such binary operator defined on given operands",
2438 goto error);
2440 obj = call_bin_op(ctx, op, obj, right_obj);
2443 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2444 right_obj = read_obj(s, table);
2445 obj.v = isl_val_add(obj.v, right_obj.v);
2448 return obj;
2449 error:
2450 free_obj(right_obj);
2451 free_obj(obj);
2452 obj.type = isl_obj_none;
2453 obj.v = NULL;
2454 return obj;
2457 static __isl_give isl_printer *source_file(struct isl_stream *s,
2458 struct isl_hash_table *table, __isl_take isl_printer *p);
2460 static __isl_give isl_printer *read_line(struct isl_stream *s,
2461 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2463 isl_ctx *ctx;
2464 struct isl_obj obj = { isl_obj_none, NULL };
2465 char *lhs = NULL;
2466 int assign = 0;
2467 int only_print = 0;
2468 struct isc_bin_op *op = NULL;
2469 char buf[30];
2471 if (!p)
2472 return NULL;
2473 if (isl_stream_is_empty(s))
2474 return p;
2476 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2477 return source_file(s, table, p);
2478 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2479 return codegen(s, table, p);
2481 assign = is_assign(s);
2482 if (assign) {
2483 lhs = isl_stream_read_ident_if_available(s);
2484 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2485 goto error;
2486 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2487 only_print = 1;
2488 else if (!tty)
2489 only_print = 1;
2491 obj = read_expr(s, table);
2492 ctx = isl_stream_get_ctx(s);
2493 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2494 fprintf(stderr, "Interrupted\n");
2495 isl_ctx_reset_error(ctx);
2497 if (isl_stream_eat(s, ';'))
2498 goto error;
2500 if (only_print) {
2501 if (obj.type != isl_obj_none && obj.v != NULL) {
2502 p = obj.type->print(p, obj.v);
2503 p = isl_printer_end_line(p);
2505 free_obj(obj);
2506 return p;
2508 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2509 static int count = 0;
2510 snprintf(buf, sizeof(buf), "$%d", count++);
2511 lhs = strdup(buf + 1);
2513 p = isl_printer_print_str(p, buf);
2514 p = isl_printer_print_str(p, " := ");
2515 p = obj.type->print(p, obj.v);
2516 p = isl_printer_end_line(p);
2518 if (lhs && do_assign(ctx, table, lhs, obj))
2519 return p;
2521 return p;
2522 error:
2523 isl_stream_flush_tokens(s);
2524 isl_stream_skip_line(s);
2525 free(lhs);
2526 free_obj(obj);
2527 return p;
2530 static isl_stat free_cb(void **entry, void *user)
2532 struct isl_named_obj *named = *entry;
2534 free_obj(named->obj);
2535 free(named->name);
2536 free(named);
2538 return isl_stat_ok;
2541 static void register_named_ops(struct isl_stream *s)
2543 int i;
2545 for (i = 0; i < ISCC_N_OP; ++i) {
2546 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2547 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2550 for (i = 0; ; ++i) {
2551 if (!named_un_ops[i].name)
2552 break;
2553 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2554 named_un_ops[i].name);
2555 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2558 for (i = 0; ; ++i) {
2559 if (!named_bin_ops[i].name)
2560 break;
2561 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2562 named_bin_ops[i].name);
2563 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2567 static __isl_give isl_printer *source_file(struct isl_stream *s,
2568 struct isl_hash_table *table, __isl_take isl_printer *p)
2570 isl_ctx *ctx;
2571 struct isl_token *tok;
2572 struct isl_stream *s_file;
2573 struct iscc_options *options;
2574 char *name;
2575 FILE *file;
2577 tok = isl_stream_next_token(s);
2578 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2579 isl_stream_error(s, tok, "expecting filename");
2580 isl_token_free(tok);
2581 return p;
2584 isl_stream_eat(s, ';');
2586 ctx = isl_stream_get_ctx(s);
2587 options = isl_ctx_peek_iscc_options(ctx);
2588 if (!options || !options->io) {
2589 isl_token_free(tok);
2590 isl_die(ctx, isl_error_invalid,
2591 "source operation not allowed", return p);
2594 name = isl_token_get_str(ctx, tok);
2595 isl_token_free(tok);
2596 file = fopen(name, "r");
2597 free(name);
2598 isl_assert(ctx, file, return p);
2600 s_file = isl_stream_new_file(ctx, file);
2601 if (!s_file) {
2602 fclose(file);
2603 return p;
2606 register_named_ops(s_file);
2608 while (!isl_stream_is_empty(s_file))
2609 p = read_line(s_file, table, p, 0);
2611 isl_stream_free(s_file);
2612 fclose(file);
2614 return p;
2617 int main(int argc, char **argv)
2619 struct isl_ctx *ctx;
2620 struct isl_stream *s;
2621 struct isl_hash_table *table;
2622 struct iscc_options *options;
2623 isl_printer *p;
2624 int tty = isatty(0);
2626 options = iscc_options_new_with_defaults();
2627 assert(options);
2629 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2630 pet_options_set_autodetect(ctx, 1);
2631 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2632 s = isl_stream_new_file(ctx, stdin);
2633 assert(s);
2634 table = isl_hash_table_alloc(ctx, 10);
2635 assert(table);
2636 p = isl_printer_to_file(ctx, stdout);
2637 p = isl_printer_set_output_format(p, options->format);
2638 assert(p);
2640 register_named_ops(s);
2642 install_signal_handler(ctx);
2644 while (p && !isl_stream_is_empty(s)) {
2645 isl_ctx_resume(ctx);
2646 p = read_line(s, table, p, tty);
2649 remove_signal_handler(ctx);
2651 isl_printer_free(p);
2652 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2653 isl_hash_table_free(ctx, table);
2654 isl_stream_free(s);
2655 isl_ctx_free(ctx);
2657 return 0;