iscc: add "lb" operation
[barvinok/uuh.git] / iscc.c
blobbc3f6c1a5b3d288f9fda64cedab2c4208d67a370
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 /* Compute a lower or upper bound on "upwqp" depending on "type" and
653 * return a list containing two elements, the bound and a boolean
654 * indicating whether the result is tight.
656 static __isl_give struct isl_list *union_pw_qpolynomial_bound(
657 __isl_take isl_union_pw_qpolynomial *upwqp, enum isl_fold type)
659 isl_ctx *ctx;
660 struct isl_list *list;
661 int tight;
663 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
664 list = isl_list_alloc(ctx, 2);
665 if (!list)
666 goto error2;
668 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
669 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp, type, &tight);
670 list->obj[1].type = isl_obj_bool;
671 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
672 if (tight < 0 || !list->obj[0].v)
673 goto error;
675 return list;
676 error2:
677 isl_union_pw_qpolynomial_free(upwqp);
678 error:
679 isl_list_free(list);
680 return NULL;
683 /* Compute a lower bound on "upwqp" and return a list containing
684 * two elements, the bound and a booleanindicating whether
685 * the result is tight.
687 static __isl_give struct isl_list *union_pw_qpolynomial_lower_bound(
688 __isl_take isl_union_pw_qpolynomial *upwqp)
690 return union_pw_qpolynomial_bound(upwqp, isl_fold_min);
693 /* Compute a upper bound on "upwqp" and return a list containing
694 * two elements, the bound and a booleanindicating whether
695 * the result is tight.
697 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
698 __isl_take isl_union_pw_qpolynomial *upwqp)
700 return union_pw_qpolynomial_bound(upwqp, isl_fold_max);
703 #ifdef HAVE_PET
704 static __isl_give isl_list *parse(__isl_take isl_str *str)
706 isl_ctx *ctx;
707 struct isl_list *list;
708 struct pet_scop *scop;
709 isl_union_map *sched, *may_reads, *must_writes, *may_writes;
710 isl_union_set *domain;
711 struct iscc_options *options;
713 if (!str)
714 return NULL;
715 ctx = str->ctx;
717 options = isl_ctx_peek_iscc_options(ctx);
718 if (!options || !options->io) {
719 isl_str_free(str);
720 isl_die(ctx, isl_error_invalid,
721 "parse_file operation not allowed", return NULL);
724 list = isl_list_alloc(ctx, 5);
725 if (!list)
726 goto error;
728 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
729 domain = pet_scop_collect_domains(scop);
730 sched = scop ? isl_schedule_get_map(scop->schedule) : NULL;
731 may_reads = pet_scop_collect_may_reads(scop);
732 may_writes = pet_scop_collect_may_writes(scop);
733 must_writes = pet_scop_collect_must_writes(scop);
734 pet_scop_free(scop);
736 list->obj[0].type = isl_obj_union_set;
737 list->obj[0].v = domain;
738 list->obj[1].type = isl_obj_union_map;
739 list->obj[1].v = must_writes;
740 list->obj[2].type = isl_obj_union_map;
741 list->obj[2].v = may_writes;
742 list->obj[3].type = isl_obj_union_map;
743 list->obj[3].v = may_reads;
744 list->obj[4].type = isl_obj_union_map;
745 list->obj[4].v = sched;
747 if (!list->obj[0].v || !list->obj[1].v ||
748 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
749 goto error;
751 isl_str_free(str);
752 return list;
753 error:
754 isl_list_free(list);
755 isl_str_free(str);
756 return NULL;
758 #endif
760 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
762 isl_union_set **scan = (isl_union_set **) user;
764 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
766 return isl_stat_ok;
769 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
771 isl_union_set *scan;
773 scan = isl_union_set_empty(isl_union_set_get_space(uset));
775 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
776 isl_union_set_free(scan);
777 return uset;
780 isl_union_set_free(uset);
781 return scan;
784 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
786 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
789 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
790 __isl_take isl_union_pw_qpolynomial *upwqp)
792 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
795 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
796 __isl_take isl_union_pw_qpolynomial *upwqp)
798 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
801 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
802 __isl_take isl_union_pw_qpolynomial *upwqp)
804 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
807 typedef void *(*isc_un_op_fn)(void *arg);
808 struct isc_un_op {
809 enum isl_token_type op;
810 isl_obj_type arg;
811 isl_obj_type res;
812 isc_un_op_fn fn;
814 struct isc_named_un_op {
815 char *name;
816 struct isc_un_op op;
818 struct isc_named_un_op named_un_ops[] = {
819 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
820 (isc_un_op_fn) &isl_union_map_affine_hull } },
821 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
822 (isc_un_op_fn) &isl_union_set_affine_hull } },
823 {"card", { -1, isl_obj_union_set,
824 isl_obj_union_pw_qpolynomial,
825 (isc_un_op_fn) &isl_union_set_card } },
826 {"card", { -1, isl_obj_union_map,
827 isl_obj_union_pw_qpolynomial,
828 (isc_un_op_fn) &isl_union_map_card } },
829 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
830 (isc_un_op_fn) &isl_union_set_coalesce } },
831 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
832 (isc_un_op_fn) &isl_union_map_coalesce } },
833 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
834 isl_obj_union_pw_qpolynomial,
835 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
836 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
837 isl_obj_union_pw_qpolynomial_fold,
838 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
839 {"coefficients", { -1, isl_obj_union_set,
840 isl_obj_union_set,
841 (isc_un_op_fn) &isl_union_set_coefficients } },
842 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
843 (isc_un_op_fn) &isl_union_set_solutions } },
844 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
845 (isc_un_op_fn) &isl_union_map_deltas } },
846 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
847 (isc_un_op_fn) &isl_union_map_deltas_map } },
848 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
849 (isc_un_op_fn) &isl_union_map_domain } },
850 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
851 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
852 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
853 isl_obj_union_set,
854 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
855 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
856 (isc_un_op_fn) &isl_union_map_domain } },
857 {"domain", { -1, isl_obj_union_pw_qpolynomial,
858 isl_obj_union_set,
859 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
860 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
861 isl_obj_union_set,
862 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
863 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
864 (isc_un_op_fn) &isl_union_map_domain_map } },
865 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
866 (isc_un_op_fn) &isl_union_map_range } },
867 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
868 (isc_un_op_fn) &isl_union_map_range } },
869 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
870 (isc_un_op_fn) &isl_union_map_range_map } },
871 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
872 (isc_un_op_fn) &isl_union_set_identity } },
873 {"lattice_width", { -1, isl_obj_union_set,
874 isl_obj_union_pw_qpolynomial,
875 (isc_un_op_fn) &isl_union_set_lattice_width } },
876 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
877 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
878 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
879 (isc_un_op_fn) &isl_union_map_lexmin } },
880 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
881 (isc_un_op_fn) &isl_union_map_lexmax } },
882 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
883 (isc_un_op_fn) &isl_union_set_lexmin } },
884 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
885 (isc_un_op_fn) &isl_union_set_lexmax } },
886 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
887 (isc_un_op_fn) &isl_union_set_lift } },
888 {"params", { -1, isl_obj_union_map, isl_obj_set,
889 (isc_un_op_fn) &isl_union_map_params } },
890 {"params", { -1, isl_obj_union_set, isl_obj_set,
891 (isc_un_op_fn) &isl_union_set_params } },
892 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
893 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
894 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
895 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
896 {"poly", { -1, isl_obj_union_pw_qpolynomial,
897 isl_obj_union_pw_qpolynomial,
898 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
899 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
900 isl_obj_union_pw_qpolynomial,
901 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
902 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
903 isl_obj_union_pw_qpolynomial,
904 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
905 #ifdef HAVE_PET
906 {"parse_file", { -1, isl_obj_str, isl_obj_list,
907 (isc_un_op_fn) &parse } },
908 #endif
909 {"pow", { -1, isl_obj_union_map, isl_obj_list,
910 (isc_un_op_fn) &union_map_power } },
911 {"sample", { -1, isl_obj_union_set, isl_obj_set,
912 (isc_un_op_fn) &union_set_sample } },
913 {"sample", { -1, isl_obj_union_map, isl_obj_map,
914 (isc_un_op_fn) &union_map_sample } },
915 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
916 (isc_un_op_fn) &union_set_scan } },
917 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
918 (isc_un_op_fn) &union_map_scan } },
919 {"sum", { -1, isl_obj_union_pw_qpolynomial,
920 isl_obj_union_pw_qpolynomial,
921 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
922 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
923 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
924 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
925 (isc_un_op_fn) &isl_union_set_unwrap } },
926 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
927 (isc_un_op_fn) &isl_union_map_wrap } },
928 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
929 (isc_un_op_fn) &isl_union_map_zip } },
930 NULL
933 struct isl_named_obj {
934 char *name;
935 struct isl_obj obj;
938 static void free_obj(struct isl_obj obj)
940 obj.type->free(obj.v);
943 static int same_name(const void *entry, const void *val)
945 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
947 return !strcmp(named->name, val);
950 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
951 char *name, struct isl_obj obj)
953 struct isl_hash_table_entry *entry;
954 uint32_t name_hash;
955 struct isl_named_obj *named;
957 name_hash = isl_hash_string(isl_hash_init(), name);
958 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
959 if (!entry)
960 goto error;
961 if (entry->data) {
962 named = entry->data;
963 free_obj(named->obj);
964 free(name);
965 } else {
966 named = isl_alloc_type(ctx, struct isl_named_obj);
967 if (!named)
968 goto error;
969 named->name = name;
970 entry->data = named;
972 named->obj = obj;
974 return 0;
975 error:
976 free_obj(obj);
977 free(name);
978 return -1;
981 static struct isl_obj stored_obj(struct isl_ctx *ctx,
982 struct isl_hash_table *table, char *name)
984 struct isl_obj obj = { isl_obj_none, NULL };
985 struct isl_hash_table_entry *entry;
986 uint32_t name_hash;
988 name_hash = isl_hash_string(isl_hash_init(), name);
989 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
990 if (entry) {
991 struct isl_named_obj *named;
992 named = entry->data;
993 obj = named->obj;
994 } else if (isdigit(name[0]))
995 fprintf(stderr, "unknown identifier '$%s'\n", name);
996 else
997 fprintf(stderr, "unknown identifier '%s'\n", name);
999 free(name);
1000 obj.v = obj.type->copy(obj.v);
1001 return obj;
1004 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1006 if (obj.type == super)
1007 return 1;
1008 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1009 return 1;
1010 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1011 return 1;
1012 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1013 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1014 int is_set = isl_space_is_set(space);
1015 isl_space_free(space);
1016 return is_set;
1018 if (obj.type == isl_obj_pw_qpolynomial &&
1019 super == isl_obj_union_pw_qpolynomial)
1020 return 1;
1021 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1022 super == isl_obj_union_pw_qpolynomial_fold)
1023 return 1;
1024 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1025 return 1;
1026 if (obj.type == isl_obj_list) {
1027 struct isl_list *list = obj.v;
1028 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1029 return is_subtype(list->obj[0], super);
1031 if (super == isl_obj_str)
1032 return 1;
1033 return 0;
1036 static struct isl_obj obj_at(struct isl_obj obj, int i)
1038 struct isl_list *list = obj.v;
1040 obj = list->obj[i];
1041 obj.v = obj.type->copy(obj.v);
1043 isl_list_free(list);
1045 return obj;
1048 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1049 isl_obj_type type)
1051 if (obj.type == type)
1052 return obj;
1053 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1054 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1055 obj.type = isl_obj_union_set;
1056 obj.v = isl_union_set_from_set(set);
1057 return obj;
1059 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1060 obj.type = isl_obj_union_map;
1061 obj.v = isl_union_map_from_map(obj.v);
1062 return obj;
1064 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1065 obj.type = isl_obj_union_set;
1066 obj.v = isl_union_set_from_set(obj.v);
1067 return obj;
1069 if (obj.type == isl_obj_pw_qpolynomial &&
1070 type == isl_obj_union_pw_qpolynomial) {
1071 obj.type = isl_obj_union_pw_qpolynomial;
1072 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1073 return obj;
1075 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1076 type == isl_obj_union_pw_qpolynomial_fold) {
1077 obj.type = isl_obj_union_pw_qpolynomial_fold;
1078 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1079 return obj;
1081 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1082 if (type == isl_obj_union_map) {
1083 obj.type = isl_obj_union_map;
1084 return obj;
1086 if (type == isl_obj_union_pw_qpolynomial) {
1087 isl_space *dim = isl_union_set_get_space(obj.v);
1088 isl_union_set_free(obj.v);
1089 obj.v = isl_union_pw_qpolynomial_zero(dim);
1090 obj.type = isl_obj_union_pw_qpolynomial;
1091 return obj;
1093 if (type == isl_obj_union_pw_qpolynomial_fold) {
1094 isl_space *dim = isl_union_set_get_space(obj.v);
1095 isl_union_set_free(obj.v);
1096 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1097 isl_fold_list);
1098 obj.type = isl_obj_union_pw_qpolynomial_fold;
1099 return obj;
1102 if (obj.type == isl_obj_list) {
1103 struct isl_list *list = obj.v;
1104 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1105 return convert(ctx, obj_at(obj, 0), type);
1107 if (type == isl_obj_str) {
1108 isl_str *str;
1109 isl_printer *p;
1110 char *s;
1112 p = isl_printer_to_str(ctx);
1113 if (!p)
1114 goto error;
1115 p = obj.type->print(p, obj.v);
1116 s = isl_printer_get_str(p);
1117 isl_printer_free(p);
1119 str = isl_str_from_string(ctx, s);
1120 if (!str)
1121 goto error;
1122 free_obj(obj);
1123 obj.v = str;
1124 obj.type = isl_obj_str;
1125 return obj;
1128 error:
1129 free_obj(obj);
1130 obj.type = isl_obj_none;
1131 obj.v = NULL;
1132 return obj;
1135 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1136 struct isl_obj lhs)
1138 int i;
1139 struct isl_token *tok;
1141 tok = isl_stream_next_token(s);
1142 if (!tok)
1143 return NULL;
1145 for (i = 0; ; ++i) {
1146 if (!bin_ops[i].op)
1147 break;
1148 if (bin_ops[i].op != isl_token_get_type(tok))
1149 continue;
1150 if (!is_subtype(lhs, bin_ops[i].lhs))
1151 continue;
1153 isl_token_free(tok);
1154 return &bin_ops[i];
1157 for (i = 0; ; ++i) {
1158 if (!named_bin_ops[i].name)
1159 break;
1160 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1161 continue;
1162 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1163 continue;
1165 isl_token_free(tok);
1166 return &named_bin_ops[i].op;
1169 isl_stream_push_token(s, tok);
1171 return NULL;
1174 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1176 int i;
1177 struct isl_token *tok;
1179 tok = isl_stream_next_token(s);
1180 if (!tok)
1181 return NULL;
1183 for (i = 0; ; ++i) {
1184 if (!named_un_ops[i].name)
1185 break;
1186 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1187 continue;
1189 isl_token_free(tok);
1190 return &named_un_ops[i].op;
1193 isl_stream_push_token(s, tok);
1195 return NULL;
1198 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1199 struct isl_obj arg)
1201 int i;
1203 for (i = 0; ; ++i) {
1204 if (!named_un_ops[i].name)
1205 break;
1206 if (named_un_ops[i].op.op != like->op)
1207 continue;
1208 if (!is_subtype(arg, named_un_ops[i].op.arg))
1209 continue;
1211 return &named_un_ops[i].op;
1214 return NULL;
1217 static int is_assign(struct isl_stream *s)
1219 struct isl_token *tok;
1220 struct isl_token *tok2;
1221 int assign;
1223 tok = isl_stream_next_token(s);
1224 if (!tok)
1225 return 0;
1226 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1227 isl_stream_push_token(s, tok);
1228 return 0;
1231 tok2 = isl_stream_next_token(s);
1232 if (!tok2) {
1233 isl_stream_push_token(s, tok);
1234 return 0;
1236 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1237 isl_stream_push_token(s, tok2);
1238 isl_stream_push_token(s, tok);
1240 return assign;
1243 static struct isl_obj read_obj(struct isl_stream *s,
1244 struct isl_hash_table *table);
1245 static struct isl_obj read_expr(struct isl_stream *s,
1246 struct isl_hash_table *table);
1248 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1249 struct isl_hash_table *table, struct isc_un_op *op)
1251 isl_ctx *ctx;
1252 struct isl_obj obj = { isl_obj_none, NULL };
1254 obj = read_obj(s, table);
1255 if (!obj.v)
1256 goto error;
1258 op = find_matching_un_op(op, obj);
1260 ctx = isl_stream_get_ctx(s);
1261 if (!op)
1262 isl_die(ctx, isl_error_invalid,
1263 "no such unary operator defined on given operand",
1264 goto error);
1266 obj = convert(ctx, obj, op->arg);
1267 obj.v = op->fn(obj.v);
1268 obj.type = op->res;
1270 return obj;
1271 error:
1272 free_obj(obj);
1273 obj.type = isl_obj_none;
1274 obj.v = NULL;
1275 return obj;
1278 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1280 struct isl_list *list;
1281 int exact;
1283 if (obj.type != isl_obj_union_map)
1284 obj = convert(ctx, obj, isl_obj_union_map);
1285 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1286 list = isl_list_alloc(ctx, 2);
1287 if (!list)
1288 goto error;
1290 list->obj[0].type = isl_obj_union_map;
1291 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1292 list->obj[1].type = isl_obj_bool;
1293 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1294 obj.v = list;
1295 obj.type = isl_obj_list;
1296 if (exact < 0 || !list->obj[0].v)
1297 goto error;
1299 return obj;
1300 error:
1301 free_obj(obj);
1302 obj.type = isl_obj_none;
1303 obj.v = NULL;
1304 return obj;
1307 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1309 struct isl_list *list = obj.v;
1310 struct isl_token *tok;
1311 isl_ctx *ctx;
1312 isl_val *v;
1313 int i;
1315 tok = isl_stream_next_token(s);
1316 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1317 isl_stream_error(s, tok, "expecting index");
1318 if (tok)
1319 isl_stream_push_token(s, tok);
1320 goto error;
1322 ctx = isl_stream_get_ctx(s);
1323 v = isl_token_get_val(ctx, tok);
1324 i = isl_val_get_num_si(v);
1325 isl_val_free(v);
1326 isl_token_free(tok);
1327 isl_assert(ctx, i < list->n, goto error);
1328 if (isl_stream_eat(s, ']'))
1329 goto error;
1331 return obj_at(obj, i);
1332 error:
1333 free_obj(obj);
1334 obj.type = isl_obj_none;
1335 obj.v = NULL;
1336 return obj;
1339 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1340 struct isl_hash_table *table)
1342 isl_ctx *ctx;
1343 struct isl_obj obj;
1345 obj = read_expr(s, table);
1346 ctx = isl_stream_get_ctx(s);
1347 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1348 is_subtype(obj, isl_obj_union_map), goto error);
1350 if (obj.type == isl_obj_list) {
1351 struct isl_list *list = obj.v;
1352 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1353 obj = obj_at(obj, 0);
1355 if (obj.type == isl_obj_set)
1356 obj = convert(ctx, obj, isl_obj_union_set);
1357 else if (obj.type == isl_obj_map)
1358 obj = convert(ctx, obj, isl_obj_union_map);
1359 if (obj.type == isl_obj_union_set) {
1360 obj.v = isl_union_set_apply(obj.v, umap);
1361 } else
1362 obj.v = isl_union_map_apply_range(obj.v, umap);
1363 if (!obj.v)
1364 goto error2;
1366 if (isl_stream_eat(s, ')'))
1367 goto error2;
1369 return obj;
1370 error:
1371 isl_union_map_free(umap);
1372 error2:
1373 free_obj(obj);
1374 obj.type = isl_obj_none;
1375 obj.v = NULL;
1376 return obj;
1379 static struct isl_obj apply_fun_set(struct isl_obj obj,
1380 __isl_take isl_union_set *uset)
1382 if (obj.type == isl_obj_union_pw_qpolynomial) {
1383 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1384 } else {
1385 obj.type = isl_obj_list;
1386 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1388 return obj;
1391 static struct isl_obj apply_fun_map(struct isl_obj obj,
1392 __isl_take isl_union_map *umap)
1394 if (obj.type == isl_obj_union_pw_qpolynomial) {
1395 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1396 } else {
1397 obj.type = isl_obj_list;
1398 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1400 return obj;
1403 static struct isl_obj apply_fun(struct isl_stream *s,
1404 struct isl_obj obj, struct isl_hash_table *table)
1406 struct isl_obj arg;
1407 isl_ctx *ctx;
1409 arg = read_expr(s, table);
1410 ctx = isl_stream_get_ctx(s);
1411 if (!is_subtype(arg, isl_obj_union_map) &&
1412 !is_subtype(arg, isl_obj_union_set))
1413 isl_die(ctx, isl_error_invalid,
1414 "expecting set of map argument", goto error);
1416 if (arg.type == isl_obj_list) {
1417 struct isl_list *list = arg.v;
1418 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1419 arg = obj_at(arg, 0);
1421 if (arg.type == isl_obj_set)
1422 arg = convert(ctx, arg, isl_obj_union_set);
1423 else if (arg.type == isl_obj_map)
1424 arg = convert(ctx, arg, isl_obj_union_map);
1425 if (arg.type == isl_obj_union_set)
1426 obj = apply_fun_set(obj, arg.v);
1427 else
1428 obj = apply_fun_map(obj, arg.v);
1429 if (!obj.v)
1430 goto error2;
1432 if (isl_stream_eat(s, ')'))
1433 goto error2;
1435 return obj;
1436 error:
1437 free_obj(arg);
1438 error2:
1439 free_obj(obj);
1440 obj.type = isl_obj_none;
1441 obj.v = NULL;
1442 return obj;
1445 struct add_vertex_data {
1446 struct isl_list *list;
1447 int i;
1450 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1452 struct add_vertex_data *data = (struct add_vertex_data *)user;
1453 isl_multi_aff *ma;
1454 isl_set *dom;
1456 ma = isl_vertex_get_expr(vertex);
1457 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1459 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1460 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1461 data->i++;
1463 isl_vertex_free(vertex);
1465 return isl_stat_ok;
1468 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1470 isl_ctx *ctx;
1471 isl_basic_set *hull;
1472 isl_vertices *vertices = NULL;
1473 struct isl_list *list = NULL;
1474 isl_stat r;
1475 struct add_vertex_data *data = (struct add_vertex_data *)user;
1477 set = isl_set_remove_divs(set);
1478 hull = isl_set_convex_hull(set);
1479 vertices = isl_basic_set_compute_vertices(hull);
1480 isl_basic_set_free(hull);
1482 list = data->list;
1484 ctx = isl_vertices_get_ctx(vertices);
1485 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1486 if (!data->list)
1487 goto error;
1489 data->i = 0;
1490 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1492 data->list = isl_list_concat(list, data->list);
1494 isl_vertices_free(vertices);
1496 return r;
1497 error:
1498 data->list = list;
1499 isl_vertices_free(vertices);
1500 return isl_stat_error;
1503 static struct isl_obj vertices(struct isl_stream *s,
1504 struct isl_hash_table *table)
1506 isl_ctx *ctx;
1507 struct isl_obj obj;
1508 struct isl_list *list = NULL;
1509 isl_union_set *uset = NULL;
1510 struct add_vertex_data data = { NULL };
1512 obj = read_expr(s, table);
1513 ctx = isl_stream_get_ctx(s);
1514 obj = convert(ctx, obj, isl_obj_union_set);
1515 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1516 uset = obj.v;
1517 obj.v = NULL;
1519 list = isl_list_alloc(ctx, 0);
1520 if (!list)
1521 goto error;
1523 data.list = list;
1525 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1526 goto error;
1528 isl_union_set_free(uset);
1530 obj.type = isl_obj_list;
1531 obj.v = data.list;
1533 return obj;
1534 error:
1535 isl_union_set_free(uset);
1536 isl_list_free(data.list);
1537 free_obj(obj);
1538 obj.type = isl_obj_none;
1539 obj.v = NULL;
1540 return obj;
1543 static struct isl_obj type_of(struct isl_stream *s,
1544 struct isl_hash_table *table)
1546 isl_ctx *ctx;
1547 struct isl_obj obj;
1548 const char *type = "unknown";
1550 obj = read_expr(s, table);
1552 if (obj.type == isl_obj_map ||
1553 obj.type == isl_obj_union_map)
1554 type = "map";
1555 if (obj.type == isl_obj_set ||
1556 obj.type == isl_obj_union_set)
1557 type = "set";
1558 if (obj.type == isl_obj_pw_multi_aff)
1559 type = "piecewise multi-quasiaffine expression";
1560 if (obj.type == isl_obj_pw_qpolynomial ||
1561 obj.type == isl_obj_union_pw_qpolynomial)
1562 type = "piecewise quasipolynomial";
1563 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1564 obj.type == isl_obj_union_pw_qpolynomial_fold)
1565 type = "piecewise quasipolynomial fold";
1566 if (obj.type == isl_obj_list)
1567 type = "list";
1568 if (obj.type == isl_obj_bool)
1569 type = "boolean";
1570 if (obj.type == isl_obj_str)
1571 type = "string";
1572 if (obj.type == isl_obj_val)
1573 type = "value";
1575 free_obj(obj);
1576 obj.type = isl_obj_str;
1577 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1579 return obj;
1582 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1583 struct isl_hash_table *table)
1585 struct isl_obj obj;
1586 isl_ctx *ctx;
1588 obj = read_obj(s, table);
1589 ctx = isl_stream_get_ctx(s);
1590 obj = convert(ctx, obj, isl_obj_union_set);
1591 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1592 return obj.v;
1593 error:
1594 free_obj(obj);
1595 return NULL;
1598 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1599 struct isl_hash_table *table)
1601 struct isl_obj obj;
1602 isl_ctx *ctx;
1604 obj = read_obj(s, table);
1605 ctx = isl_stream_get_ctx(s);
1606 obj = convert(ctx, obj, isl_obj_union_map);
1607 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1608 return obj.v;
1609 error:
1610 free_obj(obj);
1611 return NULL;
1614 static struct isl_obj last_any(struct isl_stream *s,
1615 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1616 __isl_take isl_union_map *may_source)
1618 struct isl_obj obj = { isl_obj_none, NULL };
1619 isl_union_map *sink = NULL;
1620 isl_union_map *schedule = NULL;
1621 isl_union_map *may_dep;
1622 isl_union_map *must_dep;
1624 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1625 goto error;
1627 sink = read_map(s, table);
1628 if (!sink)
1629 goto error;
1631 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1632 goto error;
1634 schedule = read_map(s, table);
1635 if (!schedule)
1636 goto error;
1638 if (isl_union_map_compute_flow(sink, must_source, may_source,
1639 schedule, &must_dep, &may_dep,
1640 NULL, NULL) < 0)
1641 return obj;
1643 obj.type = isl_obj_union_map;
1644 obj.v = isl_union_map_union(must_dep, may_dep);
1646 return obj;
1647 error:
1648 isl_union_map_free(may_source);
1649 isl_union_map_free(must_source);
1650 isl_union_map_free(sink);
1651 isl_union_map_free(schedule);
1652 free_obj(obj);
1653 obj.type = isl_obj_none;
1654 obj.v = NULL;
1655 return obj;
1658 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1660 struct isl_obj obj = { isl_obj_none, NULL };
1661 isl_union_map *must_source = NULL;
1662 isl_union_map *may_source = NULL;
1663 isl_union_map *sink = NULL;
1664 isl_union_map *schedule = NULL;
1665 isl_union_map *may_dep;
1667 may_source = read_map(s, table);
1668 if (!may_source)
1669 goto error;
1671 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1672 must_source = read_map(s, table);
1673 if (!must_source)
1674 goto error;
1675 return last_any(s, table, must_source, may_source);
1678 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1679 goto error;
1681 sink = read_map(s, table);
1682 if (!sink)
1683 goto error;
1685 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1686 goto error;
1688 schedule = read_map(s, table);
1689 if (!schedule)
1690 goto error;
1692 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1693 if (isl_union_map_compute_flow(sink, must_source, may_source,
1694 schedule, NULL, &may_dep,
1695 NULL, NULL) < 0)
1696 return obj;
1698 obj.type = isl_obj_union_map;
1699 obj.v = may_dep;
1701 return obj;
1702 error:
1703 isl_union_map_free(may_source);
1704 isl_union_map_free(must_source);
1705 isl_union_map_free(sink);
1706 isl_union_map_free(schedule);
1707 free_obj(obj);
1708 obj.type = isl_obj_none;
1709 obj.v = NULL;
1710 return obj;
1713 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1715 struct isl_obj obj = { isl_obj_none, NULL };
1716 struct isl_list *list = NULL;
1717 isl_union_map *must_source = NULL;
1718 isl_union_map *may_source = NULL;
1719 isl_union_map *sink = NULL;
1720 isl_union_map *schedule = NULL;
1721 isl_union_map *must_dep;
1722 isl_union_map *must_no_source;
1724 must_source = read_map(s, table);
1725 if (!must_source)
1726 goto error;
1728 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1729 may_source = read_map(s, table);
1730 if (!may_source)
1731 goto error;
1732 return last_any(s, table, must_source, may_source);
1735 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1736 if (!list)
1737 goto error;
1739 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1740 goto error;
1742 sink = read_map(s, table);
1743 if (!sink)
1744 goto error;
1746 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1747 goto error;
1749 schedule = read_map(s, table);
1750 if (!schedule)
1751 goto error;
1753 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1754 if (isl_union_map_compute_flow(sink, must_source, may_source,
1755 schedule, &must_dep, NULL,
1756 &must_no_source, NULL) < 0) {
1757 isl_list_free(list);
1758 return obj;
1761 list->obj[0].type = isl_obj_union_map;
1762 list->obj[0].v = must_dep;
1763 list->obj[1].type = isl_obj_union_map;
1764 list->obj[1].v = must_no_source;
1766 obj.v = list;
1767 obj.type = isl_obj_list;
1769 return obj;
1770 error:
1771 isl_list_free(list);
1772 isl_union_map_free(may_source);
1773 isl_union_map_free(must_source);
1774 isl_union_map_free(sink);
1775 isl_union_map_free(schedule);
1776 free_obj(obj);
1777 obj.type = isl_obj_none;
1778 obj.v = NULL;
1779 return obj;
1782 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1783 struct isl_hash_table *table)
1785 isl_union_set *domain;
1786 isl_union_map *validity;
1787 isl_union_map *proximity;
1789 domain = read_set(s, table);
1790 if (!domain)
1791 return NULL;
1793 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1794 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1796 for (;;) {
1797 isl_union_map *umap;
1798 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1799 umap = read_map(s, table);
1800 validity = isl_union_map_union(validity, umap);
1801 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1802 umap = read_map(s, table);
1803 proximity = isl_union_map_union(proximity, umap);
1804 } else
1805 break;
1808 return isl_union_set_compute_schedule(domain, validity, proximity);
1811 static struct isl_obj schedule(struct isl_stream *s,
1812 struct isl_hash_table *table)
1814 struct isl_obj obj = { isl_obj_none, NULL };
1815 isl_schedule *schedule;
1817 schedule = get_schedule(s, table);
1819 obj.v = isl_schedule_get_map(schedule);
1820 obj.type = isl_obj_union_map;
1822 isl_schedule_free(schedule);
1824 return obj;
1827 /* Read a schedule for code generation.
1828 * If the input is a set rather than a map, then we construct
1829 * an identity schedule on the given set.
1831 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1832 struct isl_hash_table *table)
1834 struct isl_obj obj;
1835 isl_ctx *ctx;
1837 obj = read_obj(s, table);
1838 ctx = isl_stream_get_ctx(s);
1840 if (is_subtype(obj, isl_obj_union_map)) {
1841 obj = convert(ctx, obj, isl_obj_union_map);
1842 return obj.v;
1845 if (is_subtype(obj, isl_obj_union_set)) {
1846 obj = convert(ctx, obj, isl_obj_union_set);
1847 return isl_union_set_identity(obj.v);
1850 free_obj(obj);
1851 isl_die(ctx, isl_error_invalid, "expecting set or map", return NULL);
1854 /* Generate an AST for the given schedule and options and print
1855 * the AST on the printer.
1857 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1858 __isl_take isl_union_map *schedule,
1859 __isl_take isl_union_map *options)
1861 isl_space *space;
1862 isl_set *context;
1863 isl_ast_build *build;
1864 isl_ast_node *tree;
1865 int format;
1867 space = isl_union_map_get_space(schedule);
1868 context = isl_set_universe(isl_space_params(space));
1870 build = isl_ast_build_from_context(context);
1871 build = isl_ast_build_set_options(build, options);
1872 tree = isl_ast_build_ast_from_schedule(build, schedule);
1873 isl_ast_build_free(build);
1875 if (!tree)
1876 return p;
1878 format = isl_printer_get_output_format(p);
1879 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1880 p = isl_printer_print_ast_node(p, tree);
1881 p = isl_printer_set_output_format(p, format);
1883 isl_ast_node_free(tree);
1885 return p;
1888 /* Perform the codegen operation.
1889 * In particular, read a schedule, check if the user has specified any options
1890 * and then generate an AST from the schedule (and options) and print it.
1892 static __isl_give isl_printer *codegen(struct isl_stream *s,
1893 struct isl_hash_table *table, __isl_take isl_printer *p)
1895 isl_union_map *schedule;
1896 isl_union_map *options;
1898 schedule = get_codegen_schedule(s, table);
1899 if (!schedule)
1900 return p;
1902 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1903 options = read_map(s, table);
1904 else
1905 options = isl_union_map_empty(
1906 isl_union_map_get_space(schedule));
1908 p = print_code(p, schedule, options);
1910 isl_stream_eat(s, ';');
1912 return p;
1915 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1917 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1919 struct isl_obj obj = { isl_obj_none, NULL };
1920 isl_ctx *ctx = isl_band_get_ctx(band);
1921 struct isl_list *list;
1923 list = isl_list_alloc(ctx, 2);
1924 if (!list)
1925 goto error;
1927 obj.v = list;
1928 obj.type = isl_obj_list;
1930 list->obj[0].type = isl_obj_union_map;
1931 list->obj[0].v = isl_band_get_partial_schedule(band);
1933 if (isl_band_has_children(band)) {
1934 isl_band_list *children;
1936 children = isl_band_get_children(band);
1937 list->obj[1] = band_list_to_obj_list(children);
1938 } else {
1939 list->obj[1].type = isl_obj_list;
1940 list->obj[1].v = isl_list_alloc(ctx, 0);
1943 if (!list->obj[0].v || !list->obj[1].v)
1944 goto error;
1946 isl_band_free(band);
1948 return obj;
1949 error:
1950 isl_band_free(band);
1951 free_obj(obj);
1952 obj.type = isl_obj_none;
1953 obj.v = NULL;
1954 return obj;
1957 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1959 struct isl_obj obj = { isl_obj_none, NULL };
1960 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1961 struct isl_list *list;
1962 int i, n;
1964 n = isl_band_list_n_band(bands);
1965 list = isl_list_alloc(ctx, n);
1966 if (!list)
1967 goto error;
1969 obj.v = list;
1970 obj.type = isl_obj_list;
1972 for (i = 0; i < n; ++i) {
1973 isl_band *band;
1975 band = isl_band_list_get_band(bands, i);
1976 list->obj[i] = band_to_obj_list(band);
1977 if (!list->obj[i].v)
1978 goto error;
1981 isl_band_list_free(bands);
1983 return obj;
1984 error:
1985 isl_band_list_free(bands);
1986 free_obj(obj);
1987 obj.type = isl_obj_none;
1988 obj.v = NULL;
1989 return obj;
1992 static struct isl_obj schedule_forest(struct isl_stream *s,
1993 struct isl_hash_table *table)
1995 struct isl_obj obj = { isl_obj_none, NULL };
1996 isl_schedule *schedule;
1997 isl_band_list *roots;
1999 schedule = get_schedule(s, table);
2000 if (!schedule)
2001 return obj;
2003 roots = isl_schedule_get_band_forest(schedule);
2004 isl_schedule_free(schedule);
2006 return band_list_to_obj_list(roots);
2009 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2011 struct isl_token *tok;
2012 isl_ctx *ctx;
2013 isl_val *v;
2015 ctx = isl_stream_get_ctx(s);
2016 if (isl_stream_eat_if_available(s, '+'))
2017 return transitive_closure(ctx, obj);
2019 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2020 if (obj.type != isl_obj_union_map)
2021 obj = convert(ctx, obj, isl_obj_union_map);
2023 tok = isl_stream_next_token(s);
2024 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2025 isl_stream_error(s, tok, "expecting integer exponent");
2026 if (tok)
2027 isl_stream_push_token(s, tok);
2028 goto error;
2031 v = isl_token_get_val(ctx, tok);
2032 if (isl_val_is_zero(v)) {
2033 isl_stream_error(s, tok, "expecting non-zero exponent");
2034 isl_val_free(v);
2035 if (tok)
2036 isl_stream_push_token(s, tok);
2037 goto error;
2040 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2041 isl_token_free(tok);
2042 if (!obj.v)
2043 goto error;
2045 return obj;
2046 error:
2047 free_obj(obj);
2048 obj.type = isl_obj_none;
2049 obj.v = NULL;
2050 return obj;
2053 static struct isl_obj check_assert(struct isl_stream *s,
2054 struct isl_hash_table *table)
2056 struct isl_obj obj;
2057 isl_ctx *ctx;
2059 obj = read_expr(s, table);
2060 ctx = isl_stream_get_ctx(s);
2061 if (obj.type != isl_obj_bool)
2062 isl_die(ctx, isl_error_invalid,
2063 "expecting boolean expression", goto error);
2064 if (obj.v != &iscc_bool_true)
2065 isl_die(ctx, isl_error_unknown,
2066 "assertion failed", abort());
2067 error:
2068 free_obj(obj);
2069 obj.type = isl_obj_none;
2070 obj.v = NULL;
2071 return obj;
2074 static struct isl_obj read_from_file(struct isl_stream *s)
2076 isl_ctx *ctx;
2077 struct isl_obj obj;
2078 struct isl_token *tok;
2079 struct isl_stream *s_file;
2080 struct iscc_options *options;
2081 char *name;
2082 FILE *file;
2084 tok = isl_stream_next_token(s);
2085 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2086 isl_stream_error(s, tok, "expecting filename");
2087 isl_token_free(tok);
2088 goto error;
2091 ctx = isl_stream_get_ctx(s);
2092 options = isl_ctx_peek_iscc_options(ctx);
2093 if (!options || !options->io) {
2094 isl_token_free(tok);
2095 isl_die(ctx, isl_error_invalid,
2096 "read operation not allowed", goto error);
2099 name = isl_token_get_str(ctx, tok);
2100 isl_token_free(tok);
2101 file = fopen(name, "r");
2102 free(name);
2103 isl_assert(ctx, file, goto error);
2105 s_file = isl_stream_new_file(ctx, file);
2106 if (!s_file) {
2107 fclose(file);
2108 goto error;
2111 obj = isl_stream_read_obj(s_file);
2113 isl_stream_free(s_file);
2114 fclose(file);
2116 return obj;
2117 error:
2118 obj.type = isl_obj_none;
2119 obj.v = NULL;
2120 return obj;
2123 static struct isl_obj write_to_file(struct isl_stream *s,
2124 struct isl_hash_table *table)
2126 struct isl_obj obj;
2127 struct isl_token *tok;
2128 struct isl_stream *s_file;
2129 struct iscc_options *options;
2130 char *name;
2131 FILE *file;
2132 isl_ctx *ctx;
2133 isl_printer *p;
2135 tok = isl_stream_next_token(s);
2136 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2137 isl_stream_error(s, tok, "expecting filename");
2138 isl_token_free(tok);
2139 goto error;
2142 obj = read_expr(s, table);
2144 ctx = isl_stream_get_ctx(s);
2145 options = isl_ctx_peek_iscc_options(ctx);
2146 if (!options || !options->io) {
2147 isl_token_free(tok);
2148 isl_die(ctx, isl_error_invalid,
2149 "write operation not allowed", goto error);
2152 name = isl_token_get_str(ctx, tok);
2153 isl_token_free(tok);
2154 file = fopen(name, "w");
2155 free(name);
2156 if (!file)
2157 isl_die(ctx, isl_error_unknown,
2158 "could not open file for writing", goto error);
2160 p = isl_printer_to_file(ctx, file);
2161 p = isl_printer_set_output_format(p, options->format);
2162 p = obj.type->print(p, obj.v);
2163 p = isl_printer_end_line(p);
2164 isl_printer_free(p);
2166 fclose(file);
2167 error:
2168 free_obj(obj);
2169 obj.type = isl_obj_none;
2170 obj.v = NULL;
2171 return obj;
2174 static struct isl_obj read_string_if_available(struct isl_stream *s)
2176 struct isl_token *tok;
2177 struct isl_obj obj = { isl_obj_none, NULL };
2179 tok = isl_stream_next_token(s);
2180 if (!tok)
2181 return obj;
2182 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2183 isl_str *str;
2184 str = isl_str_alloc(isl_stream_get_ctx(s));
2185 if (!str)
2186 goto error;
2187 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2188 isl_token_free(tok);
2189 obj.v = str;
2190 obj.type = isl_obj_str;
2191 } else
2192 isl_stream_push_token(s, tok);
2193 return obj;
2194 error:
2195 isl_token_free(tok);
2196 return obj;
2199 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2201 struct isl_token *tok;
2202 struct isl_obj obj = { isl_obj_none, NULL };
2203 int type;
2205 tok = isl_stream_next_token(s);
2206 if (!tok)
2207 return obj;
2208 type = isl_token_get_type(tok);
2209 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2210 int is_true = type == ISL_TOKEN_TRUE;
2211 isl_token_free(tok);
2212 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2213 obj.type = isl_obj_bool;
2214 } else
2215 isl_stream_push_token(s, tok);
2216 return obj;
2219 static __isl_give char *read_ident(struct isl_stream *s)
2221 char *name;
2222 isl_val *v;
2223 struct isl_token *tok, *tok2;
2225 name = isl_stream_read_ident_if_available(s);
2226 if (name)
2227 return name;
2229 tok = isl_stream_next_token(s);
2230 if (!tok)
2231 return NULL;
2232 if (isl_token_get_type(tok) != '$') {
2233 isl_stream_push_token(s, tok);
2234 return NULL;
2236 tok2 = isl_stream_next_token(s);
2237 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2238 if (tok2)
2239 isl_stream_push_token(s, tok2);
2240 isl_stream_push_token(s, tok);
2241 return NULL;
2244 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2245 name = isl_val_to_str(v);
2246 isl_val_free(v);
2247 isl_token_free(tok);
2248 isl_token_free(tok2);
2250 return name;
2253 static struct isl_obj read_list(struct isl_stream *s,
2254 struct isl_hash_table *table, struct isl_obj obj)
2256 struct isl_list *list;
2258 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2259 if (!list)
2260 goto error;
2261 list->obj[0] = obj;
2262 list->obj[1] = read_obj(s, table);
2263 obj.v = list;
2264 obj.type = isl_obj_list;
2266 if (!list->obj[1].v)
2267 goto error;
2269 while (isl_stream_eat_if_available(s, ',')) {
2270 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2271 if (!obj.v)
2272 goto error;
2275 return obj;
2276 error:
2277 free_obj(obj);
2278 obj.type = isl_obj_none;
2279 obj.v = NULL;
2280 return obj;
2283 static struct isl_obj read_obj(struct isl_stream *s,
2284 struct isl_hash_table *table)
2286 isl_ctx *ctx;
2287 struct isl_obj obj = { isl_obj_none, NULL };
2288 char *name = NULL;
2289 struct isc_un_op *op = NULL;
2291 obj = read_string_if_available(s);
2292 if (obj.v)
2293 return obj;
2294 obj = read_bool_if_available(s);
2295 if (obj.v)
2296 return obj;
2297 ctx = isl_stream_get_ctx(s);
2298 if (isl_stream_eat_if_available(s, '(')) {
2299 if (isl_stream_next_token_is(s, ')')) {
2300 obj.type = isl_obj_list;
2301 obj.v = isl_list_alloc(ctx, 0);
2302 } else {
2303 obj = read_expr(s, table);
2304 if (obj.v && isl_stream_eat_if_available(s, ','))
2305 obj = read_list(s, table, obj);
2307 if (!obj.v || isl_stream_eat(s, ')'))
2308 goto error;
2309 } else {
2310 op = read_prefix_un_op_if_available(s);
2311 if (op)
2312 return read_un_op_expr(s, table, op);
2314 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2315 return check_assert(s, table);
2316 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2317 return read_from_file(s);
2318 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2319 return write_to_file(s, table);
2320 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2321 return vertices(s, table);
2322 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2323 return any(s, table);
2324 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2325 return last(s, table);
2326 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2327 return schedule(s, table);
2328 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2329 return schedule_forest(s, table);
2330 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2331 return type_of(s, table);
2333 name = read_ident(s);
2334 if (name)
2335 obj = stored_obj(ctx, table, name);
2336 else
2337 obj = isl_stream_read_obj(s);
2338 if (!obj.v)
2339 goto error;
2342 if (isl_stream_eat_if_available(s, '^'))
2343 obj = power(s, obj);
2344 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2345 obj = obj_at_index(s, obj);
2346 else if (is_subtype(obj, isl_obj_union_map) &&
2347 isl_stream_eat_if_available(s, '(')) {
2348 obj = convert(ctx, obj, isl_obj_union_map);
2349 obj = apply(s, obj.v, table);
2350 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2351 isl_stream_eat_if_available(s, '(')) {
2352 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2353 obj = apply_fun(s, obj, table);
2354 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2355 isl_stream_eat_if_available(s, '(')) {
2356 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2357 obj = apply_fun(s, obj, table);
2360 return obj;
2361 error:
2362 free_obj(obj);
2363 obj.type = isl_obj_none;
2364 obj.v = NULL;
2365 return obj;
2368 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2369 struct isl_obj lhs, struct isl_obj rhs)
2371 int i;
2373 for (i = 0; ; ++i) {
2374 if (!bin_ops[i].op)
2375 break;
2376 if (bin_ops[i].op != like->op)
2377 continue;
2378 if (!is_subtype(lhs, bin_ops[i].lhs))
2379 continue;
2380 if (!is_subtype(rhs, bin_ops[i].rhs))
2381 continue;
2383 return &bin_ops[i];
2386 for (i = 0; ; ++i) {
2387 if (!named_bin_ops[i].name)
2388 break;
2389 if (named_bin_ops[i].op.op != like->op)
2390 continue;
2391 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2392 continue;
2393 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2394 continue;
2396 return &named_bin_ops[i].op;
2399 return NULL;
2402 static int next_is_neg_int(struct isl_stream *s)
2404 struct isl_token *tok;
2405 int ret;
2407 tok = isl_stream_next_token(s);
2408 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2409 isl_val *v;
2410 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2411 ret = isl_val_is_neg(v);
2412 isl_val_free(v);
2413 } else
2414 ret = 0;
2415 isl_stream_push_token(s, tok);
2417 return ret;
2420 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2421 struct isl_obj lhs, struct isl_obj rhs)
2423 struct isl_obj obj;
2425 lhs = convert(ctx, lhs, op->lhs);
2426 rhs = convert(ctx, rhs, op->rhs);
2427 if (op->res != isl_obj_bool)
2428 obj.v = op->o.fn(lhs.v, rhs.v);
2429 else {
2430 int res = op->o.test(lhs.v, rhs.v);
2431 free_obj(lhs);
2432 free_obj(rhs);
2433 obj.v = iscc_bool_from_int(res);
2435 obj.type = op->res;
2437 return obj;
2440 static struct isl_obj read_expr(struct isl_stream *s,
2441 struct isl_hash_table *table)
2443 isl_ctx *ctx;
2444 struct isl_obj obj = { isl_obj_none, NULL };
2445 struct isl_obj right_obj = { isl_obj_none, NULL };
2447 obj = read_obj(s, table);
2448 ctx = isl_stream_get_ctx(s);
2449 for (; obj.v;) {
2450 struct isc_bin_op *op = NULL;
2452 op = read_bin_op_if_available(s, obj);
2453 if (!op)
2454 break;
2456 right_obj = read_obj(s, table);
2458 op = find_matching_bin_op(op, obj, right_obj);
2460 if (!op)
2461 isl_die(ctx, isl_error_invalid,
2462 "no such binary operator defined on given operands",
2463 goto error);
2465 obj = call_bin_op(ctx, op, obj, right_obj);
2468 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2469 right_obj = read_obj(s, table);
2470 obj.v = isl_val_add(obj.v, right_obj.v);
2473 return obj;
2474 error:
2475 free_obj(right_obj);
2476 free_obj(obj);
2477 obj.type = isl_obj_none;
2478 obj.v = NULL;
2479 return obj;
2482 static __isl_give isl_printer *source_file(struct isl_stream *s,
2483 struct isl_hash_table *table, __isl_take isl_printer *p);
2485 static __isl_give isl_printer *read_line(struct isl_stream *s,
2486 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2488 isl_ctx *ctx;
2489 struct isl_obj obj = { isl_obj_none, NULL };
2490 char *lhs = NULL;
2491 int assign = 0;
2492 int only_print = 0;
2493 struct isc_bin_op *op = NULL;
2494 char buf[30];
2496 if (!p)
2497 return NULL;
2498 if (isl_stream_is_empty(s))
2499 return p;
2501 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2502 return source_file(s, table, p);
2503 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2504 return codegen(s, table, p);
2506 assign = is_assign(s);
2507 if (assign) {
2508 lhs = isl_stream_read_ident_if_available(s);
2509 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2510 goto error;
2511 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2512 only_print = 1;
2513 else if (!tty)
2514 only_print = 1;
2516 obj = read_expr(s, table);
2517 ctx = isl_stream_get_ctx(s);
2518 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2519 fprintf(stderr, "Interrupted\n");
2520 isl_ctx_reset_error(ctx);
2522 if (isl_stream_eat(s, ';'))
2523 goto error;
2525 if (only_print) {
2526 if (obj.type != isl_obj_none && obj.v != NULL) {
2527 p = obj.type->print(p, obj.v);
2528 p = isl_printer_end_line(p);
2530 free_obj(obj);
2531 return p;
2533 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2534 static int count = 0;
2535 snprintf(buf, sizeof(buf), "$%d", count++);
2536 lhs = strdup(buf + 1);
2538 p = isl_printer_print_str(p, buf);
2539 p = isl_printer_print_str(p, " := ");
2540 p = obj.type->print(p, obj.v);
2541 p = isl_printer_end_line(p);
2543 if (lhs && do_assign(ctx, table, lhs, obj))
2544 return p;
2546 return p;
2547 error:
2548 isl_stream_flush_tokens(s);
2549 isl_stream_skip_line(s);
2550 free(lhs);
2551 free_obj(obj);
2552 return p;
2555 static isl_stat free_cb(void **entry, void *user)
2557 struct isl_named_obj *named = *entry;
2559 free_obj(named->obj);
2560 free(named->name);
2561 free(named);
2563 return isl_stat_ok;
2566 static void register_named_ops(struct isl_stream *s)
2568 int i;
2570 for (i = 0; i < ISCC_N_OP; ++i) {
2571 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2572 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2575 for (i = 0; ; ++i) {
2576 if (!named_un_ops[i].name)
2577 break;
2578 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2579 named_un_ops[i].name);
2580 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2583 for (i = 0; ; ++i) {
2584 if (!named_bin_ops[i].name)
2585 break;
2586 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2587 named_bin_ops[i].name);
2588 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2592 static __isl_give isl_printer *source_file(struct isl_stream *s,
2593 struct isl_hash_table *table, __isl_take isl_printer *p)
2595 isl_ctx *ctx;
2596 struct isl_token *tok;
2597 struct isl_stream *s_file;
2598 struct iscc_options *options;
2599 char *name;
2600 FILE *file;
2602 tok = isl_stream_next_token(s);
2603 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2604 isl_stream_error(s, tok, "expecting filename");
2605 isl_token_free(tok);
2606 return p;
2609 isl_stream_eat(s, ';');
2611 ctx = isl_stream_get_ctx(s);
2612 options = isl_ctx_peek_iscc_options(ctx);
2613 if (!options || !options->io) {
2614 isl_token_free(tok);
2615 isl_die(ctx, isl_error_invalid,
2616 "source operation not allowed", return p);
2619 name = isl_token_get_str(ctx, tok);
2620 isl_token_free(tok);
2621 file = fopen(name, "r");
2622 free(name);
2623 isl_assert(ctx, file, return p);
2625 s_file = isl_stream_new_file(ctx, file);
2626 if (!s_file) {
2627 fclose(file);
2628 return p;
2631 register_named_ops(s_file);
2633 while (!isl_stream_is_empty(s_file))
2634 p = read_line(s_file, table, p, 0);
2636 isl_stream_free(s_file);
2637 fclose(file);
2639 return p;
2642 int main(int argc, char **argv)
2644 struct isl_ctx *ctx;
2645 struct isl_stream *s;
2646 struct isl_hash_table *table;
2647 struct iscc_options *options;
2648 isl_printer *p;
2649 int tty = isatty(0);
2651 options = iscc_options_new_with_defaults();
2652 assert(options);
2654 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2655 pet_options_set_autodetect(ctx, 1);
2656 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2657 s = isl_stream_new_file(ctx, stdin);
2658 assert(s);
2659 table = isl_hash_table_alloc(ctx, 10);
2660 assert(table);
2661 p = isl_printer_to_file(ctx, stdout);
2662 p = isl_printer_set_output_format(p, options->format);
2663 assert(p);
2665 register_named_ops(s);
2667 install_signal_handler(ctx);
2669 while (p && !isl_stream_is_empty(s)) {
2670 isl_ctx_resume(ctx);
2671 p = read_line(s, table, p, tty);
2674 remove_signal_handler(ctx);
2676 isl_printer_free(p);
2677 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2678 isl_hash_table_free(ctx, table);
2679 isl_stream_free(s);
2680 isl_ctx_free(ctx);
2682 return 0;