isl_param_util.c: drop expr2vertex
[barvinok.git] / iscc.c
blobaa6c73a270d20bd0016f858bbc54a78a2c2d3ce4
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/obj.h>
7 #include <isl/stream.h>
8 #include <isl/set.h>
9 #include <isl/map.h>
10 #include <isl/vertices.h>
11 #include <isl/flow.h>
12 #include <isl/band.h>
13 #include <isl/schedule.h>
14 #include <isl/ast_build.h>
15 #include <isl_obj_list.h>
16 #include <isl_obj_str.h>
17 #include <barvinok/isl.h>
18 #include <barvinok/options.h>
19 #include "lattice_width.h"
21 #include "config.h"
23 #ifdef HAVE_SIGACTION
24 #include <signal.h>
26 static isl_ctx *main_ctx;
28 static void handler(int signum)
30 if (isl_ctx_aborted(main_ctx))
31 exit(EXIT_FAILURE);
32 isl_ctx_abort(main_ctx);
35 static struct sigaction sa_old;
37 static void install_signal_handler(isl_ctx *ctx)
39 struct sigaction sa;
41 main_ctx = ctx;
43 memset(&sa, 0, sizeof(struct sigaction));
44 sa.sa_handler = &handler;
45 sa.sa_flags = SA_RESTART;
46 sigaction(SIGINT, &sa, &sa_old);
49 static void remove_signal_handler(isl_ctx *ctx)
51 sigaction(SIGINT, &sa_old, NULL);
54 #else
56 static void install_signal_handler(isl_ctx *ctx)
60 static void remove_signal_handler(isl_ctx *ctx)
64 #endif
66 #ifdef HAVE_PET
67 #include <pet.h>
68 #else
69 struct pet_options;
70 int pet_options_set_autodetect(isl_ctx *ctx, int val)
72 return -1;
74 #endif
76 static int isl_bool_false = 0;
77 static int isl_bool_true = 1;
78 static int isl_bool_error = -1;
80 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
81 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
82 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
83 ISCC_MINIMIZING, ISCC_RESPECTING,
84 ISCC_CODEGEN, ISCC_USING,
85 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
86 ISCC_N_OP };
87 static const char *op_name[ISCC_N_OP] = {
88 [ISCC_ASSERT] = "assert",
89 [ISCC_READ] = "read",
90 [ISCC_WRITE] = "write",
91 [ISCC_PRINT] = "print",
92 [ISCC_SOURCE] = "source",
93 [ISCC_VERTICES] = "vertices",
94 [ISCC_LAST] = "last",
95 [ISCC_ANY] = "any",
96 [ISCC_BEFORE] = "before",
97 [ISCC_UNDER] = "under",
98 [ISCC_SCHEDULE] = "schedule",
99 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
100 [ISCC_MINIMIZING] = "minimizing",
101 [ISCC_RESPECTING] = "respecting",
102 [ISCC_CODEGEN] = "codegen",
103 [ISCC_USING] = "using",
104 [ISCC_TYPEOF] = "typeof"
106 static enum isl_token_type iscc_op[ISCC_N_OP];
108 struct isl_arg_choice iscc_format[] = {
109 {"isl", ISL_FORMAT_ISL},
110 {"omega", ISL_FORMAT_OMEGA},
111 {"polylib", ISL_FORMAT_POLYLIB},
112 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
113 {"latex", ISL_FORMAT_LATEX},
114 {"C", ISL_FORMAT_C},
118 struct iscc_options {
119 struct barvinok_options *barvinok;
120 struct pet_options *pet;
121 unsigned format;
122 int io;
125 ISL_ARGS_START(struct iscc_options, iscc_options_args)
126 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
127 "barvinok options")
128 #ifdef HAVE_PET
129 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
130 #endif
131 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
132 iscc_format, ISL_FORMAT_ISL, "output format")
133 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
134 "allow read and write operations")
135 ISL_ARGS_END
137 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
138 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
140 static void *isl_obj_bool_copy(void *v)
142 return v;
145 static void isl_obj_bool_free(void *v)
149 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
150 void *v)
152 if (v == &isl_bool_true)
153 return isl_printer_print_str(p, "True");
154 else if (v == &isl_bool_false)
155 return isl_printer_print_str(p, "False");
156 else
157 return isl_printer_print_str(p, "Error");
160 static void *isl_obj_bool_add(void *v1, void *v2)
162 return v1;
165 struct isl_obj_vtable isl_obj_bool_vtable = {
166 isl_obj_bool_copy,
167 isl_obj_bool_add,
168 isl_obj_bool_print,
169 isl_obj_bool_free
171 #define isl_obj_bool (&isl_obj_bool_vtable)
173 int *isl_bool_from_int(int res)
175 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
178 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
179 __isl_take isl_union_map *map2)
181 return isl_union_map_is_subset(map2, map1);
183 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
184 __isl_take isl_union_set *set2)
186 return isl_union_set_is_subset(set2, set1);
189 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
190 __isl_take isl_union_map *map2)
192 return isl_union_map_is_strict_subset(map2, map1);
194 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
195 __isl_take isl_union_set *set2)
197 return isl_union_set_is_strict_subset(set2, set1);
200 extern struct isl_obj_vtable isl_obj_list_vtable;
201 #define isl_obj_list (&isl_obj_list_vtable)
203 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
204 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
205 struct isc_bin_op {
206 enum isl_token_type op;
207 isl_obj_type lhs;
208 isl_obj_type rhs;
209 isl_obj_type res;
210 union {
211 isc_bin_op_fn fn;
212 isc_bin_test_fn test;
213 } o;
215 struct isc_named_bin_op {
216 char *name;
217 struct isc_bin_op op;
220 struct iscc_at {
221 isl_union_pw_qpolynomial *upwqp;
222 isl_union_pw_qpolynomial *res;
225 static int eval_at(__isl_take isl_point *pnt, void *user)
227 struct iscc_at *at = (struct iscc_at *) user;
228 isl_val *v;
229 isl_qpolynomial *qp;
230 isl_set *set;
232 set = isl_set_from_point(isl_point_copy(pnt));
233 v = isl_union_pw_qpolynomial_eval(
234 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
235 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
237 at->res = isl_union_pw_qpolynomial_add(at->res,
238 isl_union_pw_qpolynomial_from_pw_qpolynomial(
239 isl_pw_qpolynomial_alloc(set, qp)));
241 return 0;
244 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
245 __isl_take isl_union_pw_qpolynomial *upwqp,
246 __isl_take isl_union_set *uset)
248 struct iscc_at at;
250 at.upwqp = upwqp;
251 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
253 isl_union_set_foreach_point(uset, eval_at, &at);
255 isl_union_pw_qpolynomial_free(upwqp);
256 isl_union_set_free(uset);
258 return at.res;
261 struct iscc_fold_at {
262 isl_union_pw_qpolynomial_fold *upwf;
263 isl_union_pw_qpolynomial *res;
266 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
268 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
269 isl_val *v;
270 isl_qpolynomial *qp;
271 isl_set *set;
273 set = isl_set_from_point(isl_point_copy(pnt));
274 v = isl_union_pw_qpolynomial_fold_eval(
275 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
276 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
278 at->res = isl_union_pw_qpolynomial_add(at->res,
279 isl_union_pw_qpolynomial_from_pw_qpolynomial(
280 isl_pw_qpolynomial_alloc(set, qp)));
282 return 0;
285 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
286 __isl_take isl_union_pw_qpolynomial_fold *upwf,
287 __isl_take isl_union_set *uset)
289 struct iscc_fold_at at;
291 at.upwf = upwf;
292 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
294 isl_union_set_foreach_point(uset, eval_fold_at, &at);
296 isl_union_pw_qpolynomial_fold_free(upwf);
297 isl_union_set_free(uset);
299 return at.res;
302 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
303 __isl_take isl_union_pw_qpolynomial *upwqp,
304 __isl_take isl_union_pw_qpolynomial_fold *upwf)
306 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
307 upwqp);
310 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
311 __isl_take isl_union_map *umap,
312 __isl_take isl_union_pw_qpolynomial_fold *upwf)
314 isl_ctx *ctx;
315 struct isl_list *list;
316 int tight;
318 ctx = isl_union_map_get_ctx(umap);
319 list = isl_list_alloc(ctx, 2);
320 if (!list)
321 goto error2;
323 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
324 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
325 upwf, &tight);
326 list->obj[1].type = isl_obj_bool;
327 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
328 if (tight < 0 || !list->obj[0].v)
329 goto error;
331 return list;
332 error2:
333 isl_union_map_free(umap);
334 isl_union_pw_qpolynomial_fold_free(upwf);
335 error:
336 isl_list_free(list);
337 return NULL;
340 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
341 __isl_take isl_union_set *uset,
342 __isl_take isl_union_pw_qpolynomial_fold *upwf)
344 isl_ctx *ctx;
345 struct isl_list *list;
346 int tight;
348 ctx = isl_union_set_get_ctx(uset);
349 list = isl_list_alloc(ctx, 2);
350 if (!list)
351 goto error2;
353 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
354 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
355 upwf, &tight);
356 list->obj[1].type = isl_obj_bool;
357 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
358 if (tight < 0 || !list->obj[0].v)
359 goto error;
361 return list;
362 error2:
363 isl_union_set_free(uset);
364 isl_union_pw_qpolynomial_fold_free(upwf);
365 error:
366 isl_list_free(list);
367 return NULL;
370 static __isl_give isl_union_pw_qpolynomial *isl_val_mul_union_pw_qpolynomial(
371 __isl_take isl_val *v, __isl_take isl_union_pw_qpolynomial *upwqp)
373 return isl_union_pw_qpolynomial_scale_val(upwqp, v);
376 static __isl_give isl_union_pw_qpolynomial_fold *
377 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val *v,
378 __isl_take isl_union_pw_qpolynomial_fold *upwf)
380 return isl_union_pw_qpolynomial_fold_scale_val(upwf, v);
383 struct isc_bin_op bin_ops[] = {
384 { '+', isl_obj_val, isl_obj_val, isl_obj_val,
385 (isc_bin_op_fn) &isl_val_add },
386 { '-', isl_obj_val, isl_obj_val, isl_obj_val,
387 (isc_bin_op_fn) &isl_val_sub },
388 { '*', isl_obj_val, isl_obj_val, isl_obj_val,
389 (isc_bin_op_fn) &isl_val_mul },
390 { '+', isl_obj_pw_multi_aff, isl_obj_pw_multi_aff,
391 isl_obj_pw_multi_aff,
392 (isc_bin_op_fn) &isl_pw_multi_aff_add },
393 { '+', isl_obj_union_set, isl_obj_union_set,
394 isl_obj_union_set,
395 (isc_bin_op_fn) &isl_union_set_union },
396 { '+', isl_obj_union_map, isl_obj_union_map,
397 isl_obj_union_map,
398 (isc_bin_op_fn) &isl_union_map_union },
399 { '-', isl_obj_union_set, isl_obj_union_set,
400 isl_obj_union_set,
401 (isc_bin_op_fn) &isl_union_set_subtract },
402 { '-', isl_obj_union_map, isl_obj_union_map,
403 isl_obj_union_map,
404 (isc_bin_op_fn) &isl_union_map_subtract },
405 { '*', isl_obj_union_set, isl_obj_union_set,
406 isl_obj_union_set,
407 (isc_bin_op_fn) &isl_union_set_intersect },
408 { '*', isl_obj_union_map, isl_obj_union_map,
409 isl_obj_union_map,
410 (isc_bin_op_fn) &isl_union_map_intersect },
411 { '*', isl_obj_union_map, isl_obj_union_set,
412 isl_obj_union_map,
413 (isc_bin_op_fn) &isl_union_map_intersect_domain },
414 { '.', isl_obj_union_map, isl_obj_union_map,
415 isl_obj_union_map,
416 (isc_bin_op_fn) &isl_union_map_apply_range },
417 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
418 isl_obj_union_pw_qpolynomial,
419 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
420 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
421 isl_obj_list,
422 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
423 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
424 isl_obj_union_map,
425 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
426 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
427 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
428 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
429 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
430 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
431 isl_obj_bool,
432 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
433 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
434 isl_obj_bool,
435 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
436 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
437 isl_obj_bool,
438 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
439 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
440 isl_obj_bool,
441 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
442 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
443 isl_obj_bool,
444 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
445 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
446 isl_obj_bool,
447 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
448 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
449 isl_obj_bool,
450 { .test =
451 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
452 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
453 isl_obj_bool,
454 { .test =
455 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
456 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
457 isl_obj_union_map,
458 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
459 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
460 isl_obj_union_map,
461 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
462 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
463 isl_obj_union_map,
464 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
465 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
466 isl_obj_union_map,
467 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
468 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
469 isl_obj_union_map,
470 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
471 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
472 isl_obj_union_map,
473 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
474 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
475 isl_obj_union_map,
476 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
477 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
478 isl_obj_union_map,
479 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
480 { '.', isl_obj_union_pw_qpolynomial_fold,
481 isl_obj_union_pw_qpolynomial_fold,
482 isl_obj_union_pw_qpolynomial_fold,
483 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
484 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
485 isl_obj_union_pw_qpolynomial,
486 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
487 { '+', isl_obj_union_pw_qpolynomial,
488 isl_obj_union_pw_qpolynomial_fold,
489 isl_obj_union_pw_qpolynomial_fold,
490 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
491 { '+', isl_obj_union_pw_qpolynomial_fold,
492 isl_obj_union_pw_qpolynomial,
493 isl_obj_union_pw_qpolynomial_fold,
494 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
495 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
496 isl_obj_union_pw_qpolynomial,
497 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
498 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
499 isl_obj_union_pw_qpolynomial,
500 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
501 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
502 isl_obj_union_pw_qpolynomial,
503 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
504 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
505 isl_obj_union_pw_qpolynomial_fold,
506 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
507 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
508 isl_obj_union_pw_qpolynomial_fold,
509 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
510 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
511 isl_obj_union_pw_qpolynomial,
512 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
513 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
514 isl_obj_union_pw_qpolynomial,
515 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
516 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
517 isl_obj_union_pw_qpolynomial_fold,
518 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
519 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
520 isl_obj_union_pw_qpolynomial,
521 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
522 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
523 isl_obj_union_pw_qpolynomial,
524 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
525 { '%', isl_obj_union_set, isl_obj_union_set,
526 isl_obj_union_set,
527 (isc_bin_op_fn) &isl_union_set_gist },
528 { '%', isl_obj_union_map, isl_obj_union_map,
529 isl_obj_union_map,
530 (isc_bin_op_fn) &isl_union_map_gist },
531 { '%', isl_obj_union_map, isl_obj_union_set,
532 isl_obj_union_map,
533 (isc_bin_op_fn) &isl_union_map_gist_domain },
534 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
535 isl_obj_union_pw_qpolynomial,
536 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
537 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
538 isl_obj_union_pw_qpolynomial_fold,
539 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
540 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
541 isl_obj_union_pw_qpolynomial, isl_obj_bool,
542 { .test = (isc_bin_test_fn)
543 &isl_union_pw_qpolynomial_plain_is_equal } },
544 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
545 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
546 { .test = (isc_bin_test_fn)
547 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
548 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
549 (isc_bin_op_fn) &isl_str_concat },
553 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
554 __isl_take isl_union_map *umap2)
556 return isl_union_map_apply_range(umap2, umap1);
559 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
560 __isl_take isl_union_pw_qpolynomial *upwqp,
561 __isl_take isl_union_map *umap)
563 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
566 static __isl_give struct isl_list *qpolynomial_fold_after_map(
567 __isl_take isl_union_pw_qpolynomial_fold *upwf,
568 __isl_take isl_union_map *umap)
570 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
573 struct isc_named_bin_op named_bin_ops[] = {
574 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
575 isl_obj_union_map,
576 (isc_bin_op_fn) &map_after_map } },
577 { "after", { -1, isl_obj_union_pw_qpolynomial,
578 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
579 (isc_bin_op_fn) &qpolynomial_after_map } },
580 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
581 isl_obj_union_map, isl_obj_list,
582 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
583 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
584 isl_obj_union_map,
585 (isc_bin_op_fn) &isl_union_map_apply_range } },
586 { "before", { -1, isl_obj_union_map,
587 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
588 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
589 { "before", { -1, isl_obj_union_map,
590 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
591 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
592 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
593 isl_obj_union_set,
594 (isc_bin_op_fn) &isl_union_set_product } },
595 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
596 isl_obj_union_map,
597 (isc_bin_op_fn) &isl_union_map_product } },
598 NULL
601 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
603 return isl_set_from_basic_set(isl_union_set_sample(uset));
606 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
608 return isl_map_from_basic_map(isl_union_map_sample(umap));
611 static __isl_give struct isl_list *union_map_power(
612 __isl_take isl_union_map *umap)
614 isl_ctx *ctx;
615 struct isl_list *list;
616 int exact;
618 ctx = isl_union_map_get_ctx(umap);
619 list = isl_list_alloc(ctx, 2);
620 if (!list)
621 goto error2;
623 list->obj[0].type = isl_obj_union_map;
624 list->obj[0].v = isl_union_map_power(umap, &exact);
625 list->obj[1].type = isl_obj_bool;
626 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
627 if (exact < 0 || !list->obj[0].v)
628 goto error;
630 return list;
631 error2:
632 isl_union_map_free(umap);
633 error:
634 isl_list_free(list);
635 return NULL;
638 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
639 __isl_take isl_union_pw_qpolynomial *upwqp)
641 isl_ctx *ctx;
642 struct isl_list *list;
643 int tight;
645 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
646 list = isl_list_alloc(ctx, 2);
647 if (!list)
648 goto error2;
650 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
651 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
652 isl_fold_max, &tight);
653 list->obj[1].type = isl_obj_bool;
654 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
655 if (tight < 0 || !list->obj[0].v)
656 goto error;
658 return list;
659 error2:
660 isl_union_pw_qpolynomial_free(upwqp);
661 error:
662 isl_list_free(list);
663 return NULL;
666 #ifdef HAVE_PET
667 static __isl_give isl_list *parse(__isl_take isl_str *str)
669 isl_ctx *ctx;
670 struct isl_list *list;
671 struct pet_scop *scop;
672 isl_union_map *sched, *may_reads, *must_writes, *may_writes;
673 isl_union_set *domain;
674 struct iscc_options *options;
676 if (!str)
677 return NULL;
678 ctx = str->ctx;
680 options = isl_ctx_peek_iscc_options(ctx);
681 if (!options || !options->io) {
682 isl_str_free(str);
683 isl_die(ctx, isl_error_invalid,
684 "parse_file operation not allowed", return NULL);
687 list = isl_list_alloc(ctx, 5);
688 if (!list)
689 goto error;
691 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
692 domain = pet_scop_collect_domains(scop);
693 sched = pet_scop_collect_schedule(scop);
694 may_reads = pet_scop_collect_may_reads(scop);
695 may_writes = pet_scop_collect_may_writes(scop);
696 must_writes = pet_scop_collect_must_writes(scop);
697 pet_scop_free(scop);
699 list->obj[0].type = isl_obj_union_set;
700 list->obj[0].v = domain;
701 list->obj[1].type = isl_obj_union_map;
702 list->obj[1].v = must_writes;
703 list->obj[2].type = isl_obj_union_map;
704 list->obj[2].v = may_writes;
705 list->obj[3].type = isl_obj_union_map;
706 list->obj[3].v = may_reads;
707 list->obj[4].type = isl_obj_union_map;
708 list->obj[4].v = sched;
710 if (!list->obj[0].v || !list->obj[1].v ||
711 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
712 goto error;
714 isl_str_free(str);
715 return list;
716 error:
717 isl_list_free(list);
718 isl_str_free(str);
719 return NULL;
721 #endif
723 static int add_point(__isl_take isl_point *pnt, void *user)
725 isl_union_set **scan = (isl_union_set **) user;
727 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
729 return 0;
732 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
734 isl_union_set *scan;
736 scan = isl_union_set_empty(isl_union_set_get_space(uset));
738 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
739 isl_union_set_free(scan);
740 return uset;
743 isl_union_set_free(uset);
744 return scan;
747 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
749 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
752 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
753 __isl_take isl_union_pw_qpolynomial *upwqp)
755 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
758 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
759 __isl_take isl_union_pw_qpolynomial *upwqp)
761 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
764 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
765 __isl_take isl_union_pw_qpolynomial *upwqp)
767 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
770 typedef void *(*isc_un_op_fn)(void *arg);
771 struct isc_un_op {
772 enum isl_token_type op;
773 isl_obj_type arg;
774 isl_obj_type res;
775 isc_un_op_fn fn;
777 struct isc_named_un_op {
778 char *name;
779 struct isc_un_op op;
781 struct isc_named_un_op named_un_ops[] = {
782 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
783 (isc_un_op_fn) &isl_union_map_affine_hull } },
784 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
785 (isc_un_op_fn) &isl_union_set_affine_hull } },
786 {"card", { -1, isl_obj_union_set,
787 isl_obj_union_pw_qpolynomial,
788 (isc_un_op_fn) &isl_union_set_card } },
789 {"card", { -1, isl_obj_union_map,
790 isl_obj_union_pw_qpolynomial,
791 (isc_un_op_fn) &isl_union_map_card } },
792 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
793 (isc_un_op_fn) &isl_union_set_coalesce } },
794 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
795 (isc_un_op_fn) &isl_union_map_coalesce } },
796 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
797 isl_obj_union_pw_qpolynomial,
798 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
799 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
800 isl_obj_union_pw_qpolynomial_fold,
801 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
802 {"coefficients", { -1, isl_obj_union_set,
803 isl_obj_union_set,
804 (isc_un_op_fn) &isl_union_set_coefficients } },
805 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
806 (isc_un_op_fn) &isl_union_set_solutions } },
807 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
808 (isc_un_op_fn) &isl_union_map_deltas } },
809 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
810 (isc_un_op_fn) &isl_union_map_deltas_map } },
811 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
812 (isc_un_op_fn) &isl_union_map_domain } },
813 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
814 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
815 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
816 isl_obj_union_set,
817 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
818 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
819 (isc_un_op_fn) &isl_union_map_domain } },
820 {"domain", { -1, isl_obj_union_pw_qpolynomial,
821 isl_obj_union_set,
822 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
823 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
824 isl_obj_union_set,
825 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
826 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
827 (isc_un_op_fn) &isl_union_map_domain_map } },
828 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
829 (isc_un_op_fn) &isl_union_map_range } },
830 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
831 (isc_un_op_fn) &isl_union_map_range } },
832 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
833 (isc_un_op_fn) &isl_union_map_range_map } },
834 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
835 (isc_un_op_fn) &isl_union_set_identity } },
836 {"lattice_width", { -1, isl_obj_union_set,
837 isl_obj_union_pw_qpolynomial,
838 (isc_un_op_fn) &isl_union_set_lattice_width } },
839 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
840 (isc_un_op_fn) &isl_union_map_lexmin } },
841 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
842 (isc_un_op_fn) &isl_union_map_lexmax } },
843 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
844 (isc_un_op_fn) &isl_union_set_lexmin } },
845 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
846 (isc_un_op_fn) &isl_union_set_lexmax } },
847 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
848 (isc_un_op_fn) &isl_union_set_lift } },
849 {"params", { -1, isl_obj_union_map, isl_obj_set,
850 (isc_un_op_fn) &isl_union_map_params } },
851 {"params", { -1, isl_obj_union_set, isl_obj_set,
852 (isc_un_op_fn) &isl_union_set_params } },
853 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
854 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
855 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
856 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
857 {"poly", { -1, isl_obj_union_pw_qpolynomial,
858 isl_obj_union_pw_qpolynomial,
859 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
860 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
861 isl_obj_union_pw_qpolynomial,
862 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
863 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
864 isl_obj_union_pw_qpolynomial,
865 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
866 #ifdef HAVE_PET
867 {"parse_file", { -1, isl_obj_str, isl_obj_list,
868 (isc_un_op_fn) &parse } },
869 #endif
870 {"pow", { -1, isl_obj_union_map, isl_obj_list,
871 (isc_un_op_fn) &union_map_power } },
872 {"sample", { -1, isl_obj_union_set, isl_obj_set,
873 (isc_un_op_fn) &union_set_sample } },
874 {"sample", { -1, isl_obj_union_map, isl_obj_map,
875 (isc_un_op_fn) &union_map_sample } },
876 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
877 (isc_un_op_fn) &union_set_scan } },
878 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
879 (isc_un_op_fn) &union_map_scan } },
880 {"sum", { -1, isl_obj_union_pw_qpolynomial,
881 isl_obj_union_pw_qpolynomial,
882 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
883 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
884 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
885 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
886 (isc_un_op_fn) &isl_union_set_unwrap } },
887 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
888 (isc_un_op_fn) &isl_union_map_wrap } },
889 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
890 (isc_un_op_fn) &isl_union_map_zip } },
891 NULL
894 struct isl_named_obj {
895 char *name;
896 struct isl_obj obj;
899 static void free_obj(struct isl_obj obj)
901 obj.type->free(obj.v);
904 static int same_name(const void *entry, const void *val)
906 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
908 return !strcmp(named->name, val);
911 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
912 char *name, struct isl_obj obj)
914 struct isl_hash_table_entry *entry;
915 uint32_t name_hash;
916 struct isl_named_obj *named;
918 name_hash = isl_hash_string(isl_hash_init(), name);
919 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
920 if (!entry)
921 goto error;
922 if (entry->data) {
923 named = entry->data;
924 free_obj(named->obj);
925 free(name);
926 } else {
927 named = isl_alloc_type(ctx, struct isl_named_obj);
928 if (!named)
929 goto error;
930 named->name = name;
931 entry->data = named;
933 named->obj = obj;
935 return 0;
936 error:
937 free_obj(obj);
938 free(name);
939 return -1;
942 static struct isl_obj stored_obj(struct isl_ctx *ctx,
943 struct isl_hash_table *table, char *name)
945 struct isl_obj obj = { isl_obj_none, NULL };
946 struct isl_hash_table_entry *entry;
947 uint32_t name_hash;
949 name_hash = isl_hash_string(isl_hash_init(), name);
950 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
951 if (entry) {
952 struct isl_named_obj *named;
953 named = entry->data;
954 obj = named->obj;
955 } else if (isdigit(name[0]))
956 fprintf(stderr, "unknown identifier '$%s'\n", name);
957 else
958 fprintf(stderr, "unknown identifier '%s'\n", name);
960 free(name);
961 obj.v = obj.type->copy(obj.v);
962 return obj;
965 static int is_subtype(struct isl_obj obj, isl_obj_type super)
967 if (obj.type == super)
968 return 1;
969 if (obj.type == isl_obj_map && super == isl_obj_union_map)
970 return 1;
971 if (obj.type == isl_obj_set && super == isl_obj_union_set)
972 return 1;
973 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
974 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
975 int is_set = isl_space_is_set(space);
976 isl_space_free(space);
977 return is_set;
979 if (obj.type == isl_obj_pw_qpolynomial &&
980 super == isl_obj_union_pw_qpolynomial)
981 return 1;
982 if (obj.type == isl_obj_pw_qpolynomial_fold &&
983 super == isl_obj_union_pw_qpolynomial_fold)
984 return 1;
985 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
986 return 1;
987 if (obj.type == isl_obj_list) {
988 struct isl_list *list = obj.v;
989 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
990 return is_subtype(list->obj[0], super);
992 if (super == isl_obj_str)
993 return 1;
994 return 0;
997 static struct isl_obj obj_at(struct isl_obj obj, int i)
999 struct isl_list *list = obj.v;
1001 obj = list->obj[i];
1002 obj.v = obj.type->copy(obj.v);
1004 isl_list_free(list);
1006 return obj;
1009 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1010 isl_obj_type type)
1012 if (obj.type == type)
1013 return obj;
1014 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1015 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1016 obj.type = isl_obj_union_set;
1017 obj.v = isl_union_set_from_set(set);
1018 return obj;
1020 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1021 obj.type = isl_obj_union_map;
1022 obj.v = isl_union_map_from_map(obj.v);
1023 return obj;
1025 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1026 obj.type = isl_obj_union_set;
1027 obj.v = isl_union_set_from_set(obj.v);
1028 return obj;
1030 if (obj.type == isl_obj_pw_qpolynomial &&
1031 type == isl_obj_union_pw_qpolynomial) {
1032 obj.type = isl_obj_union_pw_qpolynomial;
1033 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1034 return obj;
1036 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1037 type == isl_obj_union_pw_qpolynomial_fold) {
1038 obj.type = isl_obj_union_pw_qpolynomial_fold;
1039 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1040 return obj;
1042 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1043 if (type == isl_obj_union_map) {
1044 obj.type = isl_obj_union_map;
1045 return obj;
1047 if (type == isl_obj_union_pw_qpolynomial) {
1048 isl_space *dim = isl_union_set_get_space(obj.v);
1049 isl_union_set_free(obj.v);
1050 obj.v = isl_union_pw_qpolynomial_zero(dim);
1051 obj.type = isl_obj_union_pw_qpolynomial;
1052 return obj;
1054 if (type == isl_obj_union_pw_qpolynomial_fold) {
1055 isl_space *dim = isl_union_set_get_space(obj.v);
1056 isl_union_set_free(obj.v);
1057 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1058 isl_fold_list);
1059 obj.type = isl_obj_union_pw_qpolynomial_fold;
1060 return obj;
1063 if (obj.type == isl_obj_list) {
1064 struct isl_list *list = obj.v;
1065 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1066 return convert(ctx, obj_at(obj, 0), type);
1068 if (type == isl_obj_str) {
1069 isl_str *str;
1070 isl_printer *p;
1071 char *s;
1073 p = isl_printer_to_str(ctx);
1074 if (!p)
1075 goto error;
1076 p = obj.type->print(p, obj.v);
1077 s = isl_printer_get_str(p);
1078 isl_printer_free(p);
1080 str = isl_str_from_string(ctx, s);
1081 if (!str)
1082 goto error;
1083 free_obj(obj);
1084 obj.v = str;
1085 obj.type = isl_obj_str;
1086 return obj;
1089 error:
1090 free_obj(obj);
1091 obj.type = isl_obj_none;
1092 obj.v = NULL;
1093 return obj;
1096 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1097 struct isl_obj lhs)
1099 int i;
1100 struct isl_token *tok;
1102 tok = isl_stream_next_token(s);
1103 if (!tok)
1104 return NULL;
1106 for (i = 0; ; ++i) {
1107 if (!bin_ops[i].op)
1108 break;
1109 if (bin_ops[i].op != isl_token_get_type(tok))
1110 continue;
1111 if (!is_subtype(lhs, bin_ops[i].lhs))
1112 continue;
1114 isl_token_free(tok);
1115 return &bin_ops[i];
1118 for (i = 0; ; ++i) {
1119 if (!named_bin_ops[i].name)
1120 break;
1121 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1122 continue;
1123 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1124 continue;
1126 isl_token_free(tok);
1127 return &named_bin_ops[i].op;
1130 isl_stream_push_token(s, tok);
1132 return NULL;
1135 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1137 int i;
1138 struct isl_token *tok;
1140 tok = isl_stream_next_token(s);
1141 if (!tok)
1142 return NULL;
1144 for (i = 0; ; ++i) {
1145 if (!named_un_ops[i].name)
1146 break;
1147 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1148 continue;
1150 isl_token_free(tok);
1151 return &named_un_ops[i].op;
1154 isl_stream_push_token(s, tok);
1156 return NULL;
1159 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1160 struct isl_obj arg)
1162 int i;
1164 for (i = 0; ; ++i) {
1165 if (!named_un_ops[i].name)
1166 break;
1167 if (named_un_ops[i].op.op != like->op)
1168 continue;
1169 if (!is_subtype(arg, named_un_ops[i].op.arg))
1170 continue;
1172 return &named_un_ops[i].op;
1175 return NULL;
1178 static int is_assign(struct isl_stream *s)
1180 struct isl_token *tok;
1181 struct isl_token *tok2;
1182 int assign;
1184 tok = isl_stream_next_token(s);
1185 if (!tok)
1186 return 0;
1187 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1188 isl_stream_push_token(s, tok);
1189 return 0;
1192 tok2 = isl_stream_next_token(s);
1193 if (!tok2) {
1194 isl_stream_push_token(s, tok);
1195 return 0;
1197 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1198 isl_stream_push_token(s, tok2);
1199 isl_stream_push_token(s, tok);
1201 return assign;
1204 static struct isl_obj read_obj(struct isl_stream *s,
1205 struct isl_hash_table *table);
1206 static struct isl_obj read_expr(struct isl_stream *s,
1207 struct isl_hash_table *table);
1209 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1210 struct isl_hash_table *table, struct isc_un_op *op)
1212 struct isl_obj obj = { isl_obj_none, NULL };
1214 obj = read_obj(s, table);
1215 if (!obj.v)
1216 goto error;
1218 op = find_matching_un_op(op, obj);
1220 if (!op)
1221 isl_die(s->ctx, isl_error_invalid,
1222 "no such unary operator defined on given operand",
1223 goto error);
1225 obj = convert(s->ctx, obj, op->arg);
1226 obj.v = op->fn(obj.v);
1227 obj.type = op->res;
1229 return obj;
1230 error:
1231 free_obj(obj);
1232 obj.type = isl_obj_none;
1233 obj.v = NULL;
1234 return obj;
1237 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1239 struct isl_list *list;
1240 int exact;
1242 if (obj.type != isl_obj_union_map)
1243 obj = convert(ctx, obj, isl_obj_union_map);
1244 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1245 list = isl_list_alloc(ctx, 2);
1246 if (!list)
1247 goto error;
1249 list->obj[0].type = isl_obj_union_map;
1250 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1251 list->obj[1].type = isl_obj_bool;
1252 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1253 obj.v = list;
1254 obj.type = isl_obj_list;
1255 if (exact < 0 || !list->obj[0].v)
1256 goto error;
1258 return obj;
1259 error:
1260 free_obj(obj);
1261 obj.type = isl_obj_none;
1262 obj.v = NULL;
1263 return obj;
1266 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1268 struct isl_list *list = obj.v;
1269 struct isl_token *tok;
1270 isl_val *v;
1271 int i;
1273 tok = isl_stream_next_token(s);
1274 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1275 isl_stream_error(s, tok, "expecting index");
1276 if (tok)
1277 isl_stream_push_token(s, tok);
1278 goto error;
1280 v = isl_token_get_val(s->ctx, tok);
1281 i = isl_val_get_num_si(v);
1282 isl_val_free(v);
1283 isl_token_free(tok);
1284 isl_assert(s->ctx, i < list->n, goto error);
1285 if (isl_stream_eat(s, ']'))
1286 goto error;
1288 return obj_at(obj, i);
1289 error:
1290 free_obj(obj);
1291 obj.type = isl_obj_none;
1292 obj.v = NULL;
1293 return obj;
1296 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1297 struct isl_hash_table *table)
1299 struct isl_obj obj;
1301 obj = read_expr(s, table);
1302 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1303 is_subtype(obj, isl_obj_union_map), goto error);
1305 if (obj.type == isl_obj_list) {
1306 struct isl_list *list = obj.v;
1307 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1308 obj = obj_at(obj, 0);
1310 if (obj.type == isl_obj_set)
1311 obj = convert(s->ctx, obj, isl_obj_union_set);
1312 else if (obj.type == isl_obj_map)
1313 obj = convert(s->ctx, obj, isl_obj_union_map);
1314 if (obj.type == isl_obj_union_set) {
1315 obj.v = isl_union_set_apply(obj.v, umap);
1316 } else
1317 obj.v = isl_union_map_apply_range(obj.v, umap);
1318 if (!obj.v)
1319 goto error2;
1321 if (isl_stream_eat(s, ')'))
1322 goto error2;
1324 return obj;
1325 error:
1326 isl_union_map_free(umap);
1327 error2:
1328 free_obj(obj);
1329 obj.type = isl_obj_none;
1330 obj.v = NULL;
1331 return obj;
1334 static struct isl_obj apply_fun_set(struct isl_obj obj,
1335 __isl_take isl_union_set *uset)
1337 if (obj.type == isl_obj_union_pw_qpolynomial) {
1338 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1339 } else {
1340 obj.type = isl_obj_list;
1341 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1343 return obj;
1346 static struct isl_obj apply_fun_map(struct isl_obj obj,
1347 __isl_take isl_union_map *umap)
1349 if (obj.type == isl_obj_union_pw_qpolynomial) {
1350 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1351 } else {
1352 obj.type = isl_obj_list;
1353 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1355 return obj;
1358 static struct isl_obj apply_fun(struct isl_stream *s,
1359 struct isl_obj obj, struct isl_hash_table *table)
1361 struct isl_obj arg;
1363 arg = read_expr(s, table);
1364 if (!is_subtype(arg, isl_obj_union_map) &&
1365 !is_subtype(arg, isl_obj_union_set))
1366 isl_die(s->ctx, isl_error_invalid,
1367 "expecting set of map argument", goto error);
1369 if (arg.type == isl_obj_list) {
1370 struct isl_list *list = arg.v;
1371 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1372 arg = obj_at(arg, 0);
1374 if (arg.type == isl_obj_set)
1375 arg = convert(s->ctx, arg, isl_obj_union_set);
1376 else if (arg.type == isl_obj_map)
1377 arg = convert(s->ctx, arg, isl_obj_union_map);
1378 if (arg.type == isl_obj_union_set)
1379 obj = apply_fun_set(obj, arg.v);
1380 else
1381 obj = apply_fun_map(obj, arg.v);
1382 if (!obj.v)
1383 goto error2;
1385 if (isl_stream_eat(s, ')'))
1386 goto error2;
1388 return obj;
1389 error:
1390 free_obj(arg);
1391 error2:
1392 free_obj(obj);
1393 obj.type = isl_obj_none;
1394 obj.v = NULL;
1395 return obj;
1398 struct add_vertex_data {
1399 struct isl_list *list;
1400 int i;
1403 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1405 struct add_vertex_data *data = (struct add_vertex_data *)user;
1406 isl_multi_aff *ma;
1407 isl_set *dom;
1409 ma = isl_vertex_get_expr(vertex);
1410 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1412 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1413 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1414 data->i++;
1416 isl_vertex_free(vertex);
1418 return 0;
1421 static int set_vertices(__isl_take isl_set *set, void *user)
1423 isl_ctx *ctx;
1424 isl_basic_set *hull;
1425 isl_vertices *vertices = NULL;
1426 struct isl_list *list = NULL;
1427 int r;
1428 struct add_vertex_data *data = (struct add_vertex_data *)user;
1430 set = isl_set_remove_divs(set);
1431 hull = isl_set_convex_hull(set);
1432 vertices = isl_basic_set_compute_vertices(hull);
1433 isl_basic_set_free(hull);
1435 list = data->list;
1437 ctx = isl_vertices_get_ctx(vertices);
1438 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1439 if (!data->list)
1440 goto error;
1442 data->i = 0;
1443 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1445 data->list = isl_list_concat(list, data->list);
1447 isl_vertices_free(vertices);
1449 return r;
1450 error:
1451 data->list = list;
1452 isl_vertices_free(vertices);
1453 return -1;
1456 static struct isl_obj vertices(struct isl_stream *s,
1457 struct isl_hash_table *table)
1459 isl_ctx *ctx;
1460 struct isl_obj obj;
1461 struct isl_list *list = NULL;
1462 isl_union_set *uset;
1463 struct add_vertex_data data = { NULL };
1465 obj = read_expr(s, table);
1466 obj = convert(s->ctx, obj, isl_obj_union_set);
1467 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1468 uset = obj.v;
1469 obj.v = NULL;
1471 ctx = isl_union_set_get_ctx(uset);
1472 list = isl_list_alloc(ctx, 0);
1473 if (!list)
1474 goto error;
1476 data.list = list;
1478 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1479 goto error;
1481 isl_union_set_free(uset);
1483 obj.type = isl_obj_list;
1484 obj.v = data.list;
1486 return obj;
1487 error:
1488 isl_union_set_free(uset);
1489 isl_list_free(data.list);
1490 free_obj(obj);
1491 obj.type = isl_obj_none;
1492 obj.v = NULL;
1493 return obj;
1496 static struct isl_obj type_of(struct isl_stream *s,
1497 struct isl_hash_table *table)
1499 isl_ctx *ctx;
1500 struct isl_obj obj;
1501 const char *type = "unknown";
1503 obj = read_expr(s, table);
1505 if (obj.type == isl_obj_map ||
1506 obj.type == isl_obj_union_map)
1507 type = "map";
1508 if (obj.type == isl_obj_set ||
1509 obj.type == isl_obj_union_set)
1510 type = "set";
1511 if (obj.type == isl_obj_pw_multi_aff)
1512 type = "piecewise multi-quasiaffine expression";
1513 if (obj.type == isl_obj_pw_qpolynomial ||
1514 obj.type == isl_obj_union_pw_qpolynomial)
1515 type = "piecewise quasipolynomial";
1516 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1517 obj.type == isl_obj_union_pw_qpolynomial_fold)
1518 type = "piecewise quasipolynomial fold";
1519 if (obj.type == isl_obj_list)
1520 type = "list";
1521 if (obj.type == isl_obj_bool)
1522 type = "boolean";
1523 if (obj.type == isl_obj_str)
1524 type = "string";
1525 if (obj.type == isl_obj_val)
1526 type = "value";
1528 free_obj(obj);
1529 obj.type = isl_obj_str;
1530 obj.v = isl_str_from_string(s->ctx, strdup(type));
1532 return obj;
1535 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1536 struct isl_hash_table *table)
1538 struct isl_obj obj;
1540 obj = read_obj(s, table);
1541 obj = convert(s->ctx, obj, isl_obj_union_set);
1542 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1543 return obj.v;
1544 error:
1545 free_obj(obj);
1546 return NULL;
1549 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1550 struct isl_hash_table *table)
1552 struct isl_obj obj;
1554 obj = read_obj(s, table);
1555 obj = convert(s->ctx, obj, isl_obj_union_map);
1556 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1557 return obj.v;
1558 error:
1559 free_obj(obj);
1560 return NULL;
1563 static struct isl_obj last_any(struct isl_stream *s,
1564 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1565 __isl_take isl_union_map *may_source)
1567 struct isl_obj obj = { isl_obj_none, NULL };
1568 isl_union_map *sink = NULL;
1569 isl_union_map *schedule = NULL;
1570 isl_union_map *may_dep;
1571 isl_union_map *must_dep;
1573 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1574 goto error;
1576 sink = read_map(s, table);
1577 if (!sink)
1578 goto error;
1580 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1581 goto error;
1583 schedule = read_map(s, table);
1584 if (!schedule)
1585 goto error;
1587 if (isl_union_map_compute_flow(sink, must_source, may_source,
1588 schedule, &must_dep, &may_dep,
1589 NULL, NULL) < 0)
1590 return obj;
1592 obj.type = isl_obj_union_map;
1593 obj.v = isl_union_map_union(must_dep, may_dep);
1595 return obj;
1596 error:
1597 isl_union_map_free(may_source);
1598 isl_union_map_free(must_source);
1599 isl_union_map_free(sink);
1600 isl_union_map_free(schedule);
1601 free_obj(obj);
1602 obj.type = isl_obj_none;
1603 obj.v = NULL;
1604 return obj;
1607 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1609 struct isl_obj obj = { isl_obj_none, NULL };
1610 isl_union_map *must_source = NULL;
1611 isl_union_map *may_source = NULL;
1612 isl_union_map *sink = NULL;
1613 isl_union_map *schedule = NULL;
1614 isl_union_map *may_dep;
1616 may_source = read_map(s, table);
1617 if (!may_source)
1618 goto error;
1620 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1621 must_source = read_map(s, table);
1622 if (!must_source)
1623 goto error;
1624 return last_any(s, table, must_source, may_source);
1627 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1628 goto error;
1630 sink = read_map(s, table);
1631 if (!sink)
1632 goto error;
1634 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1635 goto error;
1637 schedule = read_map(s, table);
1638 if (!schedule)
1639 goto error;
1641 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1642 if (isl_union_map_compute_flow(sink, must_source, may_source,
1643 schedule, NULL, &may_dep,
1644 NULL, NULL) < 0)
1645 return obj;
1647 obj.type = isl_obj_union_map;
1648 obj.v = may_dep;
1650 return obj;
1651 error:
1652 isl_union_map_free(may_source);
1653 isl_union_map_free(must_source);
1654 isl_union_map_free(sink);
1655 isl_union_map_free(schedule);
1656 free_obj(obj);
1657 obj.type = isl_obj_none;
1658 obj.v = NULL;
1659 return obj;
1662 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1664 struct isl_obj obj = { isl_obj_none, NULL };
1665 struct isl_list *list = NULL;
1666 isl_union_map *must_source = NULL;
1667 isl_union_map *may_source = NULL;
1668 isl_union_map *sink = NULL;
1669 isl_union_map *schedule = NULL;
1670 isl_union_map *must_dep;
1671 isl_union_map *must_no_source;
1673 must_source = read_map(s, table);
1674 if (!must_source)
1675 goto error;
1677 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1678 may_source = read_map(s, table);
1679 if (!may_source)
1680 goto error;
1681 return last_any(s, table, must_source, may_source);
1684 list = isl_list_alloc(s->ctx, 2);
1685 if (!list)
1686 goto error;
1688 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1689 goto error;
1691 sink = read_map(s, table);
1692 if (!sink)
1693 goto error;
1695 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1696 goto error;
1698 schedule = read_map(s, table);
1699 if (!schedule)
1700 goto error;
1702 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1703 if (isl_union_map_compute_flow(sink, must_source, may_source,
1704 schedule, &must_dep, NULL,
1705 &must_no_source, NULL) < 0) {
1706 isl_list_free(list);
1707 return obj;
1710 list->obj[0].type = isl_obj_union_map;
1711 list->obj[0].v = must_dep;
1712 list->obj[1].type = isl_obj_union_map;
1713 list->obj[1].v = must_no_source;
1715 obj.v = list;
1716 obj.type = isl_obj_list;
1718 return obj;
1719 error:
1720 isl_list_free(list);
1721 isl_union_map_free(may_source);
1722 isl_union_map_free(must_source);
1723 isl_union_map_free(sink);
1724 isl_union_map_free(schedule);
1725 free_obj(obj);
1726 obj.type = isl_obj_none;
1727 obj.v = NULL;
1728 return obj;
1731 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1732 struct isl_hash_table *table)
1734 isl_union_set *domain;
1735 isl_union_map *validity;
1736 isl_union_map *proximity;
1738 domain = read_set(s, table);
1739 if (!domain)
1740 return NULL;
1742 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1743 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1745 for (;;) {
1746 isl_union_map *umap;
1747 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1748 umap = read_map(s, table);
1749 validity = isl_union_map_union(validity, umap);
1750 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1751 umap = read_map(s, table);
1752 proximity = isl_union_map_union(proximity, umap);
1753 } else
1754 break;
1757 return isl_union_set_compute_schedule(domain, validity, proximity);
1760 static struct isl_obj schedule(struct isl_stream *s,
1761 struct isl_hash_table *table)
1763 struct isl_obj obj = { isl_obj_none, NULL };
1764 isl_schedule *schedule;
1766 schedule = get_schedule(s, table);
1768 obj.v = isl_schedule_get_map(schedule);
1769 obj.type = isl_obj_union_map;
1771 isl_schedule_free(schedule);
1773 return obj;
1776 /* Read a schedule for code generation.
1777 * If the input is a set rather than a map, then we construct
1778 * an identity schedule on the given set.
1780 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1781 struct isl_hash_table *table)
1783 struct isl_obj obj;
1785 obj = read_obj(s, table);
1787 if (is_subtype(obj, isl_obj_union_map)) {
1788 obj = convert(s->ctx, obj, isl_obj_union_map);
1789 return obj.v;
1792 if (is_subtype(obj, isl_obj_union_set)) {
1793 obj = convert(s->ctx, obj, isl_obj_union_set);
1794 return isl_union_set_identity(obj.v);
1797 free_obj(obj);
1798 isl_die(s->ctx, isl_error_invalid, "expecting set or map", return NULL);
1801 /* Generate an AST for the given schedule and options and print
1802 * the AST on the printer.
1804 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1805 __isl_take isl_union_map *schedule,
1806 __isl_take isl_union_map *options)
1808 isl_space *space;
1809 isl_set *context;
1810 isl_ast_build *build;
1811 isl_ast_node *tree;
1812 int format;
1814 space = isl_union_map_get_space(schedule);
1815 context = isl_set_universe(isl_space_params(space));
1817 build = isl_ast_build_from_context(context);
1818 build = isl_ast_build_set_options(build, options);
1819 tree = isl_ast_build_ast_from_schedule(build, schedule);
1820 isl_ast_build_free(build);
1822 if (!tree)
1823 return p;
1825 format = isl_printer_get_output_format(p);
1826 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1827 p = isl_printer_print_ast_node(p, tree);
1828 p = isl_printer_set_output_format(p, format);
1830 isl_ast_node_free(tree);
1832 return p;
1835 /* Perform the codegen operation.
1836 * In particular, read a schedule, check if the user has specified any options
1837 * and then generate an AST from the schedule (and options) and print it.
1839 static __isl_give isl_printer *codegen(struct isl_stream *s,
1840 struct isl_hash_table *table, __isl_take isl_printer *p)
1842 isl_union_map *schedule;
1843 isl_union_map *options;
1845 schedule = get_codegen_schedule(s, table);
1846 if (!schedule)
1847 return p;
1849 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1850 options = read_map(s, table);
1851 else
1852 options = isl_union_map_empty(
1853 isl_union_map_get_space(schedule));
1855 p = print_code(p, schedule, options);
1857 isl_stream_eat(s, ';');
1859 return p;
1862 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1864 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1866 struct isl_obj obj = { isl_obj_none, NULL };
1867 isl_ctx *ctx = isl_band_get_ctx(band);
1868 struct isl_list *list;
1870 list = isl_list_alloc(ctx, 2);
1871 if (!list)
1872 goto error;
1874 obj.v = list;
1875 obj.type = isl_obj_list;
1877 list->obj[0].type = isl_obj_union_map;
1878 list->obj[0].v = isl_band_get_partial_schedule(band);
1880 if (isl_band_has_children(band)) {
1881 isl_band_list *children;
1883 children = isl_band_get_children(band);
1884 list->obj[1] = band_list_to_obj_list(children);
1885 } else {
1886 list->obj[1].type = isl_obj_list;
1887 list->obj[1].v = isl_list_alloc(ctx, 0);
1890 if (!list->obj[0].v || !list->obj[1].v)
1891 goto error;
1893 isl_band_free(band);
1895 return obj;
1896 error:
1897 isl_band_free(band);
1898 free_obj(obj);
1899 obj.type = isl_obj_none;
1900 obj.v = NULL;
1901 return obj;
1904 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1906 struct isl_obj obj = { isl_obj_none, NULL };
1907 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1908 struct isl_list *list;
1909 int i, n;
1911 n = isl_band_list_n_band(bands);
1912 list = isl_list_alloc(ctx, n);
1913 if (!list)
1914 goto error;
1916 obj.v = list;
1917 obj.type = isl_obj_list;
1919 for (i = 0; i < n; ++i) {
1920 isl_band *band;
1922 band = isl_band_list_get_band(bands, i);
1923 list->obj[i] = band_to_obj_list(band);
1924 if (!list->obj[i].v)
1925 goto error;
1928 isl_band_list_free(bands);
1930 return obj;
1931 error:
1932 isl_band_list_free(bands);
1933 free_obj(obj);
1934 obj.type = isl_obj_none;
1935 obj.v = NULL;
1936 return obj;
1939 static struct isl_obj schedule_forest(struct isl_stream *s,
1940 struct isl_hash_table *table)
1942 struct isl_obj obj = { isl_obj_none, NULL };
1943 isl_schedule *schedule;
1944 isl_band_list *roots;
1946 schedule = get_schedule(s, table);
1947 if (!schedule)
1948 return obj;
1950 roots = isl_schedule_get_band_forest(schedule);
1951 isl_schedule_free(schedule);
1953 return band_list_to_obj_list(roots);
1956 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1958 struct isl_token *tok;
1959 isl_val *v;
1961 if (isl_stream_eat_if_available(s, '+'))
1962 return transitive_closure(s->ctx, obj);
1964 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1965 if (obj.type != isl_obj_union_map)
1966 obj = convert(s->ctx, obj, isl_obj_union_map);
1968 tok = isl_stream_next_token(s);
1969 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1970 isl_stream_error(s, tok, "expecting integer exponent");
1971 if (tok)
1972 isl_stream_push_token(s, tok);
1973 goto error;
1976 v = isl_token_get_val(s->ctx, tok);
1977 if (isl_val_is_zero(v)) {
1978 isl_stream_error(s, tok, "expecting non-zero exponent");
1979 isl_val_free(v);
1980 if (tok)
1981 isl_stream_push_token(s, tok);
1982 goto error;
1985 obj.v = isl_union_map_fixed_power_val(obj.v, v);
1986 isl_token_free(tok);
1987 if (!obj.v)
1988 goto error;
1990 return obj;
1991 error:
1992 free_obj(obj);
1993 obj.type = isl_obj_none;
1994 obj.v = NULL;
1995 return obj;
1998 static struct isl_obj check_assert(struct isl_stream *s,
1999 struct isl_hash_table *table)
2001 struct isl_obj obj;
2003 obj = read_expr(s, table);
2004 if (obj.type != isl_obj_bool)
2005 isl_die(s->ctx, isl_error_invalid,
2006 "expecting boolean expression", goto error);
2007 if (obj.v != &isl_bool_true)
2008 isl_die(s->ctx, isl_error_unknown,
2009 "assertion failed", abort());
2010 error:
2011 free_obj(obj);
2012 obj.type = isl_obj_none;
2013 obj.v = NULL;
2014 return obj;
2017 static struct isl_obj read_from_file(struct isl_stream *s)
2019 struct isl_obj obj;
2020 struct isl_token *tok;
2021 struct isl_stream *s_file;
2022 struct iscc_options *options;
2023 char *name;
2024 FILE *file;
2026 tok = isl_stream_next_token(s);
2027 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2028 isl_stream_error(s, tok, "expecting filename");
2029 isl_token_free(tok);
2030 goto error;
2033 options = isl_ctx_peek_iscc_options(s->ctx);
2034 if (!options || !options->io) {
2035 isl_token_free(tok);
2036 isl_die(s->ctx, isl_error_invalid,
2037 "read operation not allowed", goto error);
2040 name = isl_token_get_str(s->ctx, tok);
2041 isl_token_free(tok);
2042 file = fopen(name, "r");
2043 free(name);
2044 isl_assert(s->ctx, file, goto error);
2046 s_file = isl_stream_new_file(s->ctx, file);
2047 if (!s_file) {
2048 fclose(file);
2049 goto error;
2052 obj = isl_stream_read_obj(s_file);
2054 isl_stream_free(s_file);
2055 fclose(file);
2057 return obj;
2058 error:
2059 obj.type = isl_obj_none;
2060 obj.v = NULL;
2061 return obj;
2064 static struct isl_obj write_to_file(struct isl_stream *s,
2065 struct isl_hash_table *table)
2067 struct isl_obj obj;
2068 struct isl_token *tok;
2069 struct isl_stream *s_file;
2070 struct iscc_options *options;
2071 char *name;
2072 FILE *file;
2073 isl_printer *p;
2075 tok = isl_stream_next_token(s);
2076 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2077 isl_stream_error(s, tok, "expecting filename");
2078 isl_token_free(tok);
2079 goto error;
2082 obj = read_expr(s, table);
2084 options = isl_ctx_peek_iscc_options(s->ctx);
2085 if (!options || !options->io) {
2086 isl_token_free(tok);
2087 isl_die(s->ctx, isl_error_invalid,
2088 "write operation not allowed", goto error);
2091 name = isl_token_get_str(s->ctx, tok);
2092 isl_token_free(tok);
2093 file = fopen(name, "w");
2094 free(name);
2095 if (!file)
2096 isl_die(s->ctx, isl_error_unknown,
2097 "could not open file for writing", goto error);
2099 p = isl_printer_to_file(s->ctx, file);
2100 p = isl_printer_set_output_format(p, options->format);
2101 p = obj.type->print(p, obj.v);
2102 p = isl_printer_end_line(p);
2103 isl_printer_free(p);
2105 fclose(file);
2106 error:
2107 free_obj(obj);
2108 obj.type = isl_obj_none;
2109 obj.v = NULL;
2110 return obj;
2113 static struct isl_obj read_string_if_available(struct isl_stream *s)
2115 struct isl_token *tok;
2116 struct isl_obj obj = { isl_obj_none, NULL };
2118 tok = isl_stream_next_token(s);
2119 if (!tok)
2120 return obj;
2121 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2122 isl_str *str;
2123 str = isl_str_alloc(s->ctx);
2124 if (!str)
2125 goto error;
2126 str->s = isl_token_get_str(s->ctx, tok);
2127 isl_token_free(tok);
2128 obj.v = str;
2129 obj.type = isl_obj_str;
2130 } else
2131 isl_stream_push_token(s, tok);
2132 return obj;
2133 error:
2134 isl_token_free(tok);
2135 return obj;
2138 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2140 struct isl_token *tok;
2141 struct isl_obj obj = { isl_obj_none, NULL };
2142 int type;
2144 tok = isl_stream_next_token(s);
2145 if (!tok)
2146 return obj;
2147 type = isl_token_get_type(tok);
2148 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2149 int is_true = type == ISL_TOKEN_TRUE;
2150 isl_token_free(tok);
2151 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2152 obj.type = isl_obj_bool;
2153 } else
2154 isl_stream_push_token(s, tok);
2155 return obj;
2158 static __isl_give char *read_ident(struct isl_stream *s)
2160 char *name;
2161 isl_val *v;
2162 struct isl_token *tok, *tok2;
2164 name = isl_stream_read_ident_if_available(s);
2165 if (name)
2166 return name;
2168 tok = isl_stream_next_token(s);
2169 if (!tok)
2170 return NULL;
2171 if (isl_token_get_type(tok) != '$') {
2172 isl_stream_push_token(s, tok);
2173 return NULL;
2175 tok2 = isl_stream_next_token(s);
2176 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2177 if (tok2)
2178 isl_stream_push_token(s, tok2);
2179 isl_stream_push_token(s, tok);
2180 return NULL;
2183 v = isl_token_get_val(s->ctx, tok2);
2184 name = isl_val_to_str(v);
2185 isl_val_free(v);
2186 isl_token_free(tok);
2187 isl_token_free(tok2);
2189 return name;
2192 static struct isl_obj read_list(struct isl_stream *s,
2193 struct isl_hash_table *table, struct isl_obj obj)
2195 struct isl_list *list;
2197 list = isl_list_alloc(s->ctx, 2);
2198 if (!list)
2199 goto error;
2200 list->obj[0] = obj;
2201 list->obj[1] = read_obj(s, table);
2202 obj.v = list;
2203 obj.type = isl_obj_list;
2205 if (!list->obj[1].v)
2206 goto error;
2208 while (isl_stream_eat_if_available(s, ',')) {
2209 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2210 if (!obj.v)
2211 goto error;
2214 return obj;
2215 error:
2216 free_obj(obj);
2217 obj.type = isl_obj_none;
2218 obj.v = NULL;
2219 return obj;
2222 static struct isl_obj read_obj(struct isl_stream *s,
2223 struct isl_hash_table *table)
2225 struct isl_obj obj = { isl_obj_none, NULL };
2226 char *name = NULL;
2227 struct isc_un_op *op = NULL;
2229 obj = read_string_if_available(s);
2230 if (obj.v)
2231 return obj;
2232 obj = read_bool_if_available(s);
2233 if (obj.v)
2234 return obj;
2235 if (isl_stream_eat_if_available(s, '(')) {
2236 if (isl_stream_next_token_is(s, ')')) {
2237 obj.type = isl_obj_list;
2238 obj.v = isl_list_alloc(s->ctx, 0);
2239 } else {
2240 obj = read_expr(s, table);
2241 if (obj.v && isl_stream_eat_if_available(s, ','))
2242 obj = read_list(s, table, obj);
2244 if (!obj.v || isl_stream_eat(s, ')'))
2245 goto error;
2246 } else {
2247 op = read_prefix_un_op_if_available(s);
2248 if (op)
2249 return read_un_op_expr(s, table, op);
2251 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2252 return check_assert(s, table);
2253 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2254 return read_from_file(s);
2255 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2256 return write_to_file(s, table);
2257 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2258 return vertices(s, table);
2259 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2260 return any(s, table);
2261 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2262 return last(s, table);
2263 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2264 return schedule(s, table);
2265 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2266 return schedule_forest(s, table);
2267 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2268 return type_of(s, table);
2270 name = read_ident(s);
2271 if (name)
2272 obj = stored_obj(s->ctx, table, name);
2273 else
2274 obj = isl_stream_read_obj(s);
2275 if (!obj.v)
2276 goto error;
2279 if (isl_stream_eat_if_available(s, '^'))
2280 obj = power(s, obj);
2281 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2282 obj = obj_at_index(s, obj);
2283 else if (is_subtype(obj, isl_obj_union_map) &&
2284 isl_stream_eat_if_available(s, '(')) {
2285 obj = convert(s->ctx, obj, isl_obj_union_map);
2286 obj = apply(s, obj.v, table);
2287 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2288 isl_stream_eat_if_available(s, '(')) {
2289 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2290 obj = apply_fun(s, obj, table);
2291 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2292 isl_stream_eat_if_available(s, '(')) {
2293 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2294 obj = apply_fun(s, obj, table);
2297 return obj;
2298 error:
2299 free_obj(obj);
2300 obj.type = isl_obj_none;
2301 obj.v = NULL;
2302 return obj;
2305 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2306 struct isl_obj lhs, struct isl_obj rhs)
2308 int i;
2310 for (i = 0; ; ++i) {
2311 if (!bin_ops[i].op)
2312 break;
2313 if (bin_ops[i].op != like->op)
2314 continue;
2315 if (!is_subtype(lhs, bin_ops[i].lhs))
2316 continue;
2317 if (!is_subtype(rhs, bin_ops[i].rhs))
2318 continue;
2320 return &bin_ops[i];
2323 for (i = 0; ; ++i) {
2324 if (!named_bin_ops[i].name)
2325 break;
2326 if (named_bin_ops[i].op.op != like->op)
2327 continue;
2328 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2329 continue;
2330 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2331 continue;
2333 return &named_bin_ops[i].op;
2336 return NULL;
2339 static int next_is_neg_int(struct isl_stream *s)
2341 struct isl_token *tok;
2342 int ret;
2344 tok = isl_stream_next_token(s);
2345 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2346 isl_val *v;
2347 v = isl_token_get_val(s->ctx, tok);
2348 ret = isl_val_is_neg(v);
2349 isl_val_free(v);
2350 } else
2351 ret = 0;
2352 isl_stream_push_token(s, tok);
2354 return ret;
2357 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2358 struct isl_obj lhs, struct isl_obj rhs)
2360 struct isl_obj obj;
2362 lhs = convert(ctx, lhs, op->lhs);
2363 rhs = convert(ctx, rhs, op->rhs);
2364 if (op->res != isl_obj_bool)
2365 obj.v = op->o.fn(lhs.v, rhs.v);
2366 else {
2367 int res = op->o.test(lhs.v, rhs.v);
2368 free_obj(lhs);
2369 free_obj(rhs);
2370 obj.v = isl_bool_from_int(res);
2372 obj.type = op->res;
2374 return obj;
2377 static struct isl_obj read_expr(struct isl_stream *s,
2378 struct isl_hash_table *table)
2380 struct isl_obj obj = { isl_obj_none, NULL };
2381 struct isl_obj right_obj = { isl_obj_none, NULL };
2383 obj = read_obj(s, table);
2384 for (; obj.v;) {
2385 struct isc_bin_op *op = NULL;
2387 op = read_bin_op_if_available(s, obj);
2388 if (!op)
2389 break;
2391 right_obj = read_obj(s, table);
2393 op = find_matching_bin_op(op, obj, right_obj);
2395 if (!op)
2396 isl_die(s->ctx, isl_error_invalid,
2397 "no such binary operator defined on given operands",
2398 goto error);
2400 obj = call_bin_op(s->ctx, op, obj, right_obj);
2403 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2404 right_obj = read_obj(s, table);
2405 obj.v = isl_val_add(obj.v, right_obj.v);
2408 return obj;
2409 error:
2410 free_obj(right_obj);
2411 free_obj(obj);
2412 obj.type = isl_obj_none;
2413 obj.v = NULL;
2414 return obj;
2417 static __isl_give isl_printer *source_file(struct isl_stream *s,
2418 struct isl_hash_table *table, __isl_take isl_printer *p);
2420 static __isl_give isl_printer *read_line(struct isl_stream *s,
2421 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2423 struct isl_obj obj = { isl_obj_none, NULL };
2424 char *lhs = NULL;
2425 int assign = 0;
2426 int only_print = 0;
2427 struct isc_bin_op *op = NULL;
2428 char buf[30];
2430 if (!p)
2431 return NULL;
2432 if (isl_stream_is_empty(s))
2433 return p;
2435 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2436 return source_file(s, table, p);
2437 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2438 return codegen(s, table, p);
2440 assign = is_assign(s);
2441 if (assign) {
2442 lhs = isl_stream_read_ident_if_available(s);
2443 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2444 goto error;
2445 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2446 only_print = 1;
2447 else if (!tty)
2448 only_print = 1;
2450 obj = read_expr(s, table);
2451 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2452 fprintf(stderr, "Interrupted\n");
2453 isl_ctx_reset_error(s->ctx);
2455 if (isl_stream_eat(s, ';'))
2456 goto error;
2458 if (only_print) {
2459 if (obj.type != isl_obj_none && obj.v != NULL) {
2460 p = obj.type->print(p, obj.v);
2461 p = isl_printer_end_line(p);
2463 free_obj(obj);
2464 return p;
2466 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2467 static int count = 0;
2468 snprintf(buf, sizeof(buf), "$%d", count++);
2469 lhs = strdup(buf + 1);
2471 p = isl_printer_print_str(p, buf);
2472 p = isl_printer_print_str(p, " := ");
2473 p = obj.type->print(p, obj.v);
2474 p = isl_printer_end_line(p);
2476 if (lhs && do_assign(s->ctx, table, lhs, obj))
2477 return p;
2479 return p;
2480 error:
2481 isl_stream_flush_tokens(s);
2482 isl_stream_skip_line(s);
2483 free(lhs);
2484 free_obj(obj);
2485 return p;
2488 int free_cb(void **entry, void *user)
2490 struct isl_named_obj *named = *entry;
2492 free_obj(named->obj);
2493 free(named->name);
2494 free(named);
2496 return 0;
2499 static void register_named_ops(struct isl_stream *s)
2501 int i;
2503 for (i = 0; i < ISCC_N_OP; ++i) {
2504 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2505 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2508 for (i = 0; ; ++i) {
2509 if (!named_un_ops[i].name)
2510 break;
2511 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2512 named_un_ops[i].name);
2513 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2516 for (i = 0; ; ++i) {
2517 if (!named_bin_ops[i].name)
2518 break;
2519 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2520 named_bin_ops[i].name);
2521 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2525 static __isl_give isl_printer *source_file(struct isl_stream *s,
2526 struct isl_hash_table *table, __isl_take isl_printer *p)
2528 struct isl_token *tok;
2529 struct isl_stream *s_file;
2530 char *name;
2531 FILE *file;
2533 tok = isl_stream_next_token(s);
2534 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2535 isl_stream_error(s, tok, "expecting filename");
2536 isl_token_free(tok);
2537 return p;
2540 name = isl_token_get_str(s->ctx, tok);
2541 isl_token_free(tok);
2542 file = fopen(name, "r");
2543 free(name);
2544 isl_assert(s->ctx, file, return p);
2546 s_file = isl_stream_new_file(s->ctx, file);
2547 if (!s_file) {
2548 fclose(file);
2549 return p;
2552 register_named_ops(s_file);
2554 while (!s_file->eof)
2555 p = read_line(s_file, table, p, 0);
2557 isl_stream_free(s_file);
2558 fclose(file);
2560 isl_stream_eat(s, ';');
2562 return p;
2565 int main(int argc, char **argv)
2567 struct isl_ctx *ctx;
2568 struct isl_stream *s;
2569 struct isl_hash_table *table;
2570 struct iscc_options *options;
2571 isl_printer *p;
2572 int tty = isatty(0);
2574 options = iscc_options_new_with_defaults();
2575 assert(options);
2577 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2578 pet_options_set_autodetect(ctx, 1);
2579 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2580 s = isl_stream_new_file(ctx, stdin);
2581 assert(s);
2582 table = isl_hash_table_alloc(ctx, 10);
2583 assert(table);
2584 p = isl_printer_to_file(ctx, stdout);
2585 p = isl_printer_set_output_format(p, options->format);
2586 assert(p);
2588 register_named_ops(s);
2590 install_signal_handler(ctx);
2592 while (p && !s->eof) {
2593 isl_ctx_resume(ctx);
2594 p = read_line(s, table, p, tty);
2597 remove_signal_handler(ctx);
2599 isl_printer_free(p);
2600 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2601 isl_hash_table_free(ctx, table);
2602 isl_stream_free(s);
2603 isl_ctx_free(ctx);
2605 return 0;