gen_fun::add_union: context of result should be the union of the input contexts
[barvinok.git] / iscc.c
blob3ae063ae711bf6f80736d8c7e54fbb22a1b51de7
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_union_set, isl_obj_union_set,
391 isl_obj_union_set,
392 (isc_bin_op_fn) &isl_union_set_union },
393 { '+', isl_obj_union_map, isl_obj_union_map,
394 isl_obj_union_map,
395 (isc_bin_op_fn) &isl_union_map_union },
396 { '-', isl_obj_union_set, isl_obj_union_set,
397 isl_obj_union_set,
398 (isc_bin_op_fn) &isl_union_set_subtract },
399 { '-', isl_obj_union_map, isl_obj_union_map,
400 isl_obj_union_map,
401 (isc_bin_op_fn) &isl_union_map_subtract },
402 { '*', isl_obj_union_set, isl_obj_union_set,
403 isl_obj_union_set,
404 (isc_bin_op_fn) &isl_union_set_intersect },
405 { '*', isl_obj_union_map, isl_obj_union_map,
406 isl_obj_union_map,
407 (isc_bin_op_fn) &isl_union_map_intersect },
408 { '*', isl_obj_union_map, isl_obj_union_set,
409 isl_obj_union_map,
410 (isc_bin_op_fn) &isl_union_map_intersect_domain },
411 { '.', isl_obj_union_map, isl_obj_union_map,
412 isl_obj_union_map,
413 (isc_bin_op_fn) &isl_union_map_apply_range },
414 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
415 isl_obj_union_pw_qpolynomial,
416 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
417 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
418 isl_obj_list,
419 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
420 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
421 isl_obj_union_map,
422 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
423 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
424 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
425 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
426 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
427 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
428 isl_obj_bool,
429 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
430 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
431 isl_obj_bool,
432 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
433 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
434 isl_obj_bool,
435 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
436 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
437 isl_obj_bool,
438 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
439 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
440 isl_obj_bool,
441 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
442 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
443 isl_obj_bool,
444 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
445 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
446 isl_obj_bool,
447 { .test =
448 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
449 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
450 isl_obj_bool,
451 { .test =
452 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
453 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
454 isl_obj_union_map,
455 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
456 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
457 isl_obj_union_map,
458 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
459 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
460 isl_obj_union_map,
461 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
462 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
463 isl_obj_union_map,
464 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
465 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
466 isl_obj_union_map,
467 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
468 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
469 isl_obj_union_map,
470 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
471 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
472 isl_obj_union_map,
473 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
474 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
475 isl_obj_union_map,
476 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
477 { '.', isl_obj_union_pw_qpolynomial_fold,
478 isl_obj_union_pw_qpolynomial_fold,
479 isl_obj_union_pw_qpolynomial_fold,
480 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
481 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
482 isl_obj_union_pw_qpolynomial,
483 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
484 { '+', isl_obj_union_pw_qpolynomial,
485 isl_obj_union_pw_qpolynomial_fold,
486 isl_obj_union_pw_qpolynomial_fold,
487 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
488 { '+', isl_obj_union_pw_qpolynomial_fold,
489 isl_obj_union_pw_qpolynomial,
490 isl_obj_union_pw_qpolynomial_fold,
491 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
492 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
493 isl_obj_union_pw_qpolynomial,
494 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
495 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
496 isl_obj_union_pw_qpolynomial,
497 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
498 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
499 isl_obj_union_pw_qpolynomial,
500 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
501 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
502 isl_obj_union_pw_qpolynomial_fold,
503 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
504 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
505 isl_obj_union_pw_qpolynomial_fold,
506 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
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_mul },
510 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
511 isl_obj_union_pw_qpolynomial,
512 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
513 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
514 isl_obj_union_pw_qpolynomial_fold,
515 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
516 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
517 isl_obj_union_pw_qpolynomial,
518 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
519 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
520 isl_obj_union_pw_qpolynomial,
521 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
522 { '%', isl_obj_union_set, isl_obj_union_set,
523 isl_obj_union_set,
524 (isc_bin_op_fn) &isl_union_set_gist },
525 { '%', isl_obj_union_map, isl_obj_union_map,
526 isl_obj_union_map,
527 (isc_bin_op_fn) &isl_union_map_gist },
528 { '%', isl_obj_union_map, isl_obj_union_set,
529 isl_obj_union_map,
530 (isc_bin_op_fn) &isl_union_map_gist_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_gist },
534 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
535 isl_obj_union_pw_qpolynomial_fold,
536 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
537 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
538 isl_obj_union_pw_qpolynomial, isl_obj_bool,
539 { .test = (isc_bin_test_fn)
540 &isl_union_pw_qpolynomial_plain_is_equal } },
541 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
542 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
543 { .test = (isc_bin_test_fn)
544 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
545 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
546 (isc_bin_op_fn) &isl_str_concat },
550 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
551 __isl_take isl_union_map *umap2)
553 return isl_union_map_apply_range(umap2, umap1);
556 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
557 __isl_take isl_union_pw_qpolynomial *upwqp,
558 __isl_take isl_union_map *umap)
560 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
563 static __isl_give struct isl_list *qpolynomial_fold_after_map(
564 __isl_take isl_union_pw_qpolynomial_fold *upwf,
565 __isl_take isl_union_map *umap)
567 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
570 struct isc_named_bin_op named_bin_ops[] = {
571 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
572 isl_obj_union_map,
573 (isc_bin_op_fn) &map_after_map } },
574 { "after", { -1, isl_obj_union_pw_qpolynomial,
575 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
576 (isc_bin_op_fn) &qpolynomial_after_map } },
577 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
578 isl_obj_union_map, isl_obj_list,
579 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
580 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
581 isl_obj_union_map,
582 (isc_bin_op_fn) &isl_union_map_apply_range } },
583 { "before", { -1, isl_obj_union_map,
584 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
585 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
586 { "before", { -1, isl_obj_union_map,
587 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
588 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
589 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
590 isl_obj_union_set,
591 (isc_bin_op_fn) &isl_union_set_product } },
592 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
593 isl_obj_union_map,
594 (isc_bin_op_fn) &isl_union_map_product } },
595 NULL
598 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
600 return isl_set_from_basic_set(isl_union_set_sample(uset));
603 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
605 return isl_map_from_basic_map(isl_union_map_sample(umap));
608 static __isl_give struct isl_list *union_map_power(
609 __isl_take isl_union_map *umap)
611 isl_ctx *ctx;
612 struct isl_list *list;
613 int exact;
615 ctx = isl_union_map_get_ctx(umap);
616 list = isl_list_alloc(ctx, 2);
617 if (!list)
618 goto error2;
620 list->obj[0].type = isl_obj_union_map;
621 list->obj[0].v = isl_union_map_power(umap, &exact);
622 list->obj[1].type = isl_obj_bool;
623 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
624 if (exact < 0 || !list->obj[0].v)
625 goto error;
627 return list;
628 error2:
629 isl_union_map_free(umap);
630 error:
631 isl_list_free(list);
632 return NULL;
635 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
636 __isl_take isl_union_pw_qpolynomial *upwqp)
638 isl_ctx *ctx;
639 struct isl_list *list;
640 int tight;
642 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
643 list = isl_list_alloc(ctx, 2);
644 if (!list)
645 goto error2;
647 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
648 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
649 isl_fold_max, &tight);
650 list->obj[1].type = isl_obj_bool;
651 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
652 if (tight < 0 || !list->obj[0].v)
653 goto error;
655 return list;
656 error2:
657 isl_union_pw_qpolynomial_free(upwqp);
658 error:
659 isl_list_free(list);
660 return NULL;
663 #ifdef HAVE_PET
664 static __isl_give isl_list *parse(__isl_take isl_str *str)
666 isl_ctx *ctx;
667 struct isl_list *list;
668 struct pet_scop *scop;
669 isl_union_map *sched, *may_reads, *must_writes, *may_writes;
670 isl_union_set *domain;
671 struct iscc_options *options;
673 if (!str)
674 return NULL;
675 ctx = str->ctx;
677 options = isl_ctx_peek_iscc_options(ctx);
678 if (!options || !options->io) {
679 isl_str_free(str);
680 isl_die(ctx, isl_error_invalid,
681 "parse_file operation not allowed", return NULL);
684 list = isl_list_alloc(ctx, 5);
685 if (!list)
686 goto error;
688 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
689 domain = pet_scop_collect_domains(scop);
690 sched = pet_scop_collect_schedule(scop);
691 may_reads = pet_scop_collect_may_reads(scop);
692 may_writes = pet_scop_collect_may_writes(scop);
693 must_writes = pet_scop_collect_must_writes(scop);
694 pet_scop_free(scop);
696 list->obj[0].type = isl_obj_union_set;
697 list->obj[0].v = domain;
698 list->obj[1].type = isl_obj_union_map;
699 list->obj[1].v = must_writes;
700 list->obj[2].type = isl_obj_union_map;
701 list->obj[2].v = may_writes;
702 list->obj[3].type = isl_obj_union_map;
703 list->obj[3].v = may_reads;
704 list->obj[4].type = isl_obj_union_map;
705 list->obj[4].v = sched;
707 if (!list->obj[0].v || !list->obj[1].v ||
708 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
709 goto error;
711 isl_str_free(str);
712 return list;
713 error:
714 isl_list_free(list);
715 isl_str_free(str);
716 return NULL;
718 #endif
720 static int add_point(__isl_take isl_point *pnt, void *user)
722 isl_union_set **scan = (isl_union_set **) user;
724 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
726 return 0;
729 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
731 isl_union_set *scan;
733 scan = isl_union_set_empty(isl_union_set_get_space(uset));
735 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
736 isl_union_set_free(scan);
737 return uset;
740 isl_union_set_free(uset);
741 return scan;
744 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
746 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
749 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
750 __isl_take isl_union_pw_qpolynomial *upwqp)
752 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
755 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
756 __isl_take isl_union_pw_qpolynomial *upwqp)
758 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
761 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
762 __isl_take isl_union_pw_qpolynomial *upwqp)
764 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
767 typedef void *(*isc_un_op_fn)(void *arg);
768 struct isc_un_op {
769 enum isl_token_type op;
770 isl_obj_type arg;
771 isl_obj_type res;
772 isc_un_op_fn fn;
774 struct isc_named_un_op {
775 char *name;
776 struct isc_un_op op;
778 struct isc_named_un_op named_un_ops[] = {
779 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
780 (isc_un_op_fn) &isl_union_map_affine_hull } },
781 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
782 (isc_un_op_fn) &isl_union_set_affine_hull } },
783 {"card", { -1, isl_obj_union_set,
784 isl_obj_union_pw_qpolynomial,
785 (isc_un_op_fn) &isl_union_set_card } },
786 {"card", { -1, isl_obj_union_map,
787 isl_obj_union_pw_qpolynomial,
788 (isc_un_op_fn) &isl_union_map_card } },
789 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
790 (isc_un_op_fn) &isl_union_set_coalesce } },
791 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
792 (isc_un_op_fn) &isl_union_map_coalesce } },
793 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
794 isl_obj_union_pw_qpolynomial,
795 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
796 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
797 isl_obj_union_pw_qpolynomial_fold,
798 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
799 {"coefficients", { -1, isl_obj_union_set,
800 isl_obj_union_set,
801 (isc_un_op_fn) &isl_union_set_coefficients } },
802 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
803 (isc_un_op_fn) &isl_union_set_solutions } },
804 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
805 (isc_un_op_fn) &isl_union_map_deltas } },
806 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
807 (isc_un_op_fn) &isl_union_map_deltas_map } },
808 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
809 (isc_un_op_fn) &isl_union_map_domain } },
810 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
811 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
812 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
813 isl_obj_union_set,
814 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
815 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
816 (isc_un_op_fn) &isl_union_map_domain } },
817 {"domain", { -1, isl_obj_union_pw_qpolynomial,
818 isl_obj_union_set,
819 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
820 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
821 isl_obj_union_set,
822 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
823 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
824 (isc_un_op_fn) &isl_union_map_domain_map } },
825 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
826 (isc_un_op_fn) &isl_union_map_range } },
827 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
828 (isc_un_op_fn) &isl_union_map_range } },
829 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
830 (isc_un_op_fn) &isl_union_map_range_map } },
831 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
832 (isc_un_op_fn) &isl_union_set_identity } },
833 {"lattice_width", { -1, isl_obj_union_set,
834 isl_obj_union_pw_qpolynomial,
835 (isc_un_op_fn) &isl_union_set_lattice_width } },
836 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
837 (isc_un_op_fn) &isl_union_map_lexmin } },
838 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
839 (isc_un_op_fn) &isl_union_map_lexmax } },
840 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
841 (isc_un_op_fn) &isl_union_set_lexmin } },
842 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
843 (isc_un_op_fn) &isl_union_set_lexmax } },
844 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
845 (isc_un_op_fn) &isl_union_set_lift } },
846 {"params", { -1, isl_obj_union_map, isl_obj_set,
847 (isc_un_op_fn) &isl_union_map_params } },
848 {"params", { -1, isl_obj_union_set, isl_obj_set,
849 (isc_un_op_fn) &isl_union_set_params } },
850 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
851 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
852 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
853 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
854 {"poly", { -1, isl_obj_union_pw_qpolynomial,
855 isl_obj_union_pw_qpolynomial,
856 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
857 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
858 isl_obj_union_pw_qpolynomial,
859 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
860 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
861 isl_obj_union_pw_qpolynomial,
862 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
863 #ifdef HAVE_PET
864 {"parse_file", { -1, isl_obj_str, isl_obj_list,
865 (isc_un_op_fn) &parse } },
866 #endif
867 {"pow", { -1, isl_obj_union_map, isl_obj_list,
868 (isc_un_op_fn) &union_map_power } },
869 {"sample", { -1, isl_obj_union_set, isl_obj_set,
870 (isc_un_op_fn) &union_set_sample } },
871 {"sample", { -1, isl_obj_union_map, isl_obj_map,
872 (isc_un_op_fn) &union_map_sample } },
873 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
874 (isc_un_op_fn) &union_set_scan } },
875 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
876 (isc_un_op_fn) &union_map_scan } },
877 {"sum", { -1, isl_obj_union_pw_qpolynomial,
878 isl_obj_union_pw_qpolynomial,
879 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
880 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
881 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
882 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
883 (isc_un_op_fn) &isl_union_set_unwrap } },
884 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
885 (isc_un_op_fn) &isl_union_map_wrap } },
886 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
887 (isc_un_op_fn) &isl_union_map_zip } },
888 NULL
891 struct isl_named_obj {
892 char *name;
893 struct isl_obj obj;
896 static void free_obj(struct isl_obj obj)
898 obj.type->free(obj.v);
901 static int same_name(const void *entry, const void *val)
903 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
905 return !strcmp(named->name, val);
908 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
909 char *name, struct isl_obj obj)
911 struct isl_hash_table_entry *entry;
912 uint32_t name_hash;
913 struct isl_named_obj *named;
915 name_hash = isl_hash_string(isl_hash_init(), name);
916 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
917 if (!entry)
918 goto error;
919 if (entry->data) {
920 named = entry->data;
921 free_obj(named->obj);
922 free(name);
923 } else {
924 named = isl_alloc_type(ctx, struct isl_named_obj);
925 if (!named)
926 goto error;
927 named->name = name;
928 entry->data = named;
930 named->obj = obj;
932 return 0;
933 error:
934 free_obj(obj);
935 free(name);
936 return -1;
939 static struct isl_obj stored_obj(struct isl_ctx *ctx,
940 struct isl_hash_table *table, char *name)
942 struct isl_obj obj = { isl_obj_none, NULL };
943 struct isl_hash_table_entry *entry;
944 uint32_t name_hash;
946 name_hash = isl_hash_string(isl_hash_init(), name);
947 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
948 if (entry) {
949 struct isl_named_obj *named;
950 named = entry->data;
951 obj = named->obj;
952 } else if (isdigit(name[0]))
953 fprintf(stderr, "unknown identifier '$%s'\n", name);
954 else
955 fprintf(stderr, "unknown identifier '%s'\n", name);
957 free(name);
958 obj.v = obj.type->copy(obj.v);
959 return obj;
962 static int is_subtype(struct isl_obj obj, isl_obj_type super)
964 if (obj.type == super)
965 return 1;
966 if (obj.type == isl_obj_map && super == isl_obj_union_map)
967 return 1;
968 if (obj.type == isl_obj_set && super == isl_obj_union_set)
969 return 1;
970 if (obj.type == isl_obj_pw_qpolynomial &&
971 super == isl_obj_union_pw_qpolynomial)
972 return 1;
973 if (obj.type == isl_obj_pw_qpolynomial_fold &&
974 super == isl_obj_union_pw_qpolynomial_fold)
975 return 1;
976 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
977 return 1;
978 if (obj.type == isl_obj_list) {
979 struct isl_list *list = obj.v;
980 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
981 return is_subtype(list->obj[0], super);
983 if (super == isl_obj_str)
984 return 1;
985 return 0;
988 static struct isl_obj obj_at(struct isl_obj obj, int i)
990 struct isl_list *list = obj.v;
992 obj = list->obj[i];
993 obj.v = obj.type->copy(obj.v);
995 isl_list_free(list);
997 return obj;
1000 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1001 isl_obj_type type)
1003 if (obj.type == type)
1004 return obj;
1005 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1006 obj.type = isl_obj_union_map;
1007 obj.v = isl_union_map_from_map(obj.v);
1008 return obj;
1010 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1011 obj.type = isl_obj_union_set;
1012 obj.v = isl_union_set_from_set(obj.v);
1013 return obj;
1015 if (obj.type == isl_obj_pw_qpolynomial &&
1016 type == isl_obj_union_pw_qpolynomial) {
1017 obj.type = isl_obj_union_pw_qpolynomial;
1018 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1019 return obj;
1021 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1022 type == isl_obj_union_pw_qpolynomial_fold) {
1023 obj.type = isl_obj_union_pw_qpolynomial_fold;
1024 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1025 return obj;
1027 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1028 if (type == isl_obj_union_map) {
1029 obj.type = isl_obj_union_map;
1030 return obj;
1032 if (type == isl_obj_union_pw_qpolynomial) {
1033 isl_space *dim = isl_union_set_get_space(obj.v);
1034 isl_union_set_free(obj.v);
1035 obj.v = isl_union_pw_qpolynomial_zero(dim);
1036 obj.type = isl_obj_union_pw_qpolynomial;
1037 return obj;
1039 if (type == isl_obj_union_pw_qpolynomial_fold) {
1040 isl_space *dim = isl_union_set_get_space(obj.v);
1041 isl_union_set_free(obj.v);
1042 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1043 isl_fold_list);
1044 obj.type = isl_obj_union_pw_qpolynomial_fold;
1045 return obj;
1048 if (obj.type == isl_obj_list) {
1049 struct isl_list *list = obj.v;
1050 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1051 return convert(ctx, obj_at(obj, 0), type);
1053 if (type == isl_obj_str) {
1054 isl_str *str;
1055 isl_printer *p;
1056 char *s;
1058 p = isl_printer_to_str(ctx);
1059 if (!p)
1060 goto error;
1061 p = obj.type->print(p, obj.v);
1062 s = isl_printer_get_str(p);
1063 isl_printer_free(p);
1065 str = isl_str_from_string(ctx, s);
1066 if (!str)
1067 goto error;
1068 free_obj(obj);
1069 obj.v = str;
1070 obj.type = isl_obj_str;
1071 return obj;
1074 error:
1075 free_obj(obj);
1076 obj.type = isl_obj_none;
1077 obj.v = NULL;
1078 return obj;
1081 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1082 struct isl_obj lhs)
1084 int i;
1085 struct isl_token *tok;
1087 tok = isl_stream_next_token(s);
1088 if (!tok)
1089 return NULL;
1091 for (i = 0; ; ++i) {
1092 if (!bin_ops[i].op)
1093 break;
1094 if (bin_ops[i].op != isl_token_get_type(tok))
1095 continue;
1096 if (!is_subtype(lhs, bin_ops[i].lhs))
1097 continue;
1099 isl_token_free(tok);
1100 return &bin_ops[i];
1103 for (i = 0; ; ++i) {
1104 if (!named_bin_ops[i].name)
1105 break;
1106 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1107 continue;
1108 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1109 continue;
1111 isl_token_free(tok);
1112 return &named_bin_ops[i].op;
1115 isl_stream_push_token(s, tok);
1117 return NULL;
1120 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1122 int i;
1123 struct isl_token *tok;
1125 tok = isl_stream_next_token(s);
1126 if (!tok)
1127 return NULL;
1129 for (i = 0; ; ++i) {
1130 if (!named_un_ops[i].name)
1131 break;
1132 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1133 continue;
1135 isl_token_free(tok);
1136 return &named_un_ops[i].op;
1139 isl_stream_push_token(s, tok);
1141 return NULL;
1144 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1145 struct isl_obj arg)
1147 int i;
1149 for (i = 0; ; ++i) {
1150 if (!named_un_ops[i].name)
1151 break;
1152 if (named_un_ops[i].op.op != like->op)
1153 continue;
1154 if (!is_subtype(arg, named_un_ops[i].op.arg))
1155 continue;
1157 return &named_un_ops[i].op;
1160 return NULL;
1163 static int is_assign(struct isl_stream *s)
1165 struct isl_token *tok;
1166 struct isl_token *tok2;
1167 int assign;
1169 tok = isl_stream_next_token(s);
1170 if (!tok)
1171 return 0;
1172 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1173 isl_stream_push_token(s, tok);
1174 return 0;
1177 tok2 = isl_stream_next_token(s);
1178 if (!tok2) {
1179 isl_stream_push_token(s, tok);
1180 return 0;
1182 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1183 isl_stream_push_token(s, tok2);
1184 isl_stream_push_token(s, tok);
1186 return assign;
1189 static struct isl_obj read_obj(struct isl_stream *s,
1190 struct isl_hash_table *table);
1191 static struct isl_obj read_expr(struct isl_stream *s,
1192 struct isl_hash_table *table);
1194 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1195 struct isl_hash_table *table, struct isc_un_op *op)
1197 struct isl_obj obj = { isl_obj_none, NULL };
1199 obj = read_obj(s, table);
1200 if (!obj.v)
1201 goto error;
1203 op = find_matching_un_op(op, obj);
1205 if (!op)
1206 isl_die(s->ctx, isl_error_invalid,
1207 "no such unary operator defined on given operand",
1208 goto error);
1210 obj = convert(s->ctx, obj, op->arg);
1211 obj.v = op->fn(obj.v);
1212 obj.type = op->res;
1214 return obj;
1215 error:
1216 free_obj(obj);
1217 obj.type = isl_obj_none;
1218 obj.v = NULL;
1219 return obj;
1222 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1224 struct isl_list *list;
1225 int exact;
1227 if (obj.type != isl_obj_union_map)
1228 obj = convert(ctx, obj, isl_obj_union_map);
1229 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1230 list = isl_list_alloc(ctx, 2);
1231 if (!list)
1232 goto error;
1234 list->obj[0].type = isl_obj_union_map;
1235 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1236 list->obj[1].type = isl_obj_bool;
1237 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1238 obj.v = list;
1239 obj.type = isl_obj_list;
1240 if (exact < 0 || !list->obj[0].v)
1241 goto error;
1243 return obj;
1244 error:
1245 free_obj(obj);
1246 obj.type = isl_obj_none;
1247 obj.v = NULL;
1248 return obj;
1251 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1253 struct isl_list *list = obj.v;
1254 struct isl_token *tok;
1255 isl_val *v;
1256 int i;
1258 tok = isl_stream_next_token(s);
1259 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1260 isl_stream_error(s, tok, "expecting index");
1261 if (tok)
1262 isl_stream_push_token(s, tok);
1263 goto error;
1265 v = isl_token_get_val(s->ctx, tok);
1266 i = isl_val_get_num_si(v);
1267 isl_val_free(v);
1268 isl_token_free(tok);
1269 isl_assert(s->ctx, i < list->n, goto error);
1270 if (isl_stream_eat(s, ']'))
1271 goto error;
1273 return obj_at(obj, i);
1274 error:
1275 free_obj(obj);
1276 obj.type = isl_obj_none;
1277 obj.v = NULL;
1278 return obj;
1281 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1282 struct isl_hash_table *table)
1284 struct isl_obj obj;
1286 obj = read_expr(s, table);
1287 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1288 is_subtype(obj, isl_obj_union_map), goto error);
1290 if (obj.type == isl_obj_list) {
1291 struct isl_list *list = obj.v;
1292 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1293 obj = obj_at(obj, 0);
1295 if (obj.type == isl_obj_set)
1296 obj = convert(s->ctx, obj, isl_obj_union_set);
1297 else if (obj.type == isl_obj_map)
1298 obj = convert(s->ctx, obj, isl_obj_union_map);
1299 if (obj.type == isl_obj_union_set) {
1300 obj.v = isl_union_set_apply(obj.v, umap);
1301 } else
1302 obj.v = isl_union_map_apply_range(obj.v, umap);
1303 if (!obj.v)
1304 goto error2;
1306 if (isl_stream_eat(s, ')'))
1307 goto error2;
1309 return obj;
1310 error:
1311 isl_union_map_free(umap);
1312 error2:
1313 free_obj(obj);
1314 obj.type = isl_obj_none;
1315 obj.v = NULL;
1316 return obj;
1319 static struct isl_obj apply_fun_set(struct isl_obj obj,
1320 __isl_take isl_union_set *uset)
1322 if (obj.type == isl_obj_union_pw_qpolynomial) {
1323 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1324 } else {
1325 obj.type = isl_obj_list;
1326 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1328 return obj;
1331 static struct isl_obj apply_fun_map(struct isl_obj obj,
1332 __isl_take isl_union_map *umap)
1334 if (obj.type == isl_obj_union_pw_qpolynomial) {
1335 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1336 } else {
1337 obj.type = isl_obj_list;
1338 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1340 return obj;
1343 static struct isl_obj apply_fun(struct isl_stream *s,
1344 struct isl_obj obj, struct isl_hash_table *table)
1346 struct isl_obj arg;
1348 arg = read_expr(s, table);
1349 if (!is_subtype(arg, isl_obj_union_map) &&
1350 !is_subtype(arg, isl_obj_union_set))
1351 isl_die(s->ctx, isl_error_invalid,
1352 "expecting set of map argument", goto error);
1354 if (arg.type == isl_obj_list) {
1355 struct isl_list *list = arg.v;
1356 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1357 arg = obj_at(arg, 0);
1359 if (arg.type == isl_obj_set)
1360 arg = convert(s->ctx, arg, isl_obj_union_set);
1361 else if (arg.type == isl_obj_map)
1362 arg = convert(s->ctx, arg, isl_obj_union_map);
1363 if (arg.type == isl_obj_union_set)
1364 obj = apply_fun_set(obj, arg.v);
1365 else
1366 obj = apply_fun_map(obj, arg.v);
1367 if (!obj.v)
1368 goto error2;
1370 if (isl_stream_eat(s, ')'))
1371 goto error2;
1373 return obj;
1374 error:
1375 free_obj(arg);
1376 error2:
1377 free_obj(obj);
1378 obj.type = isl_obj_none;
1379 obj.v = NULL;
1380 return obj;
1383 struct add_vertex_data {
1384 struct isl_list *list;
1385 int i;
1388 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1390 struct add_vertex_data *data = (struct add_vertex_data *)user;
1391 isl_basic_set *expr;
1393 expr = isl_vertex_get_expr(vertex);
1395 data->list->obj[data->i].type = isl_obj_set;
1396 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1397 data->i++;
1399 isl_vertex_free(vertex);
1401 return 0;
1404 static int set_vertices(__isl_take isl_set *set, void *user)
1406 isl_ctx *ctx;
1407 isl_basic_set *hull;
1408 isl_vertices *vertices = NULL;
1409 struct isl_list *list = NULL;
1410 int r;
1411 struct add_vertex_data *data = (struct add_vertex_data *)user;
1413 set = isl_set_remove_divs(set);
1414 hull = isl_set_convex_hull(set);
1415 vertices = isl_basic_set_compute_vertices(hull);
1416 isl_basic_set_free(hull);
1418 list = data->list;
1420 ctx = isl_vertices_get_ctx(vertices);
1421 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1422 if (!data->list)
1423 goto error;
1425 data->i = 0;
1426 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1428 data->list = isl_list_concat(list, data->list);
1430 isl_vertices_free(vertices);
1432 return r;
1433 error:
1434 data->list = list;
1435 isl_vertices_free(vertices);
1436 return -1;
1439 static struct isl_obj vertices(struct isl_stream *s,
1440 struct isl_hash_table *table)
1442 isl_ctx *ctx;
1443 struct isl_obj obj;
1444 struct isl_list *list = NULL;
1445 isl_union_set *uset;
1446 struct add_vertex_data data = { NULL };
1448 obj = read_expr(s, table);
1449 obj = convert(s->ctx, obj, isl_obj_union_set);
1450 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1451 uset = obj.v;
1452 obj.v = NULL;
1454 ctx = isl_union_set_get_ctx(uset);
1455 list = isl_list_alloc(ctx, 0);
1456 if (!list)
1457 goto error;
1459 data.list = list;
1461 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1462 goto error;
1464 isl_union_set_free(uset);
1466 obj.type = isl_obj_list;
1467 obj.v = data.list;
1469 return obj;
1470 error:
1471 isl_union_set_free(uset);
1472 isl_list_free(data.list);
1473 free_obj(obj);
1474 obj.type = isl_obj_none;
1475 obj.v = NULL;
1476 return obj;
1479 static struct isl_obj type_of(struct isl_stream *s,
1480 struct isl_hash_table *table)
1482 isl_ctx *ctx;
1483 struct isl_obj obj;
1484 const char *type = "unknown";
1486 obj = read_expr(s, table);
1488 if (obj.type == isl_obj_map ||
1489 obj.type == isl_obj_union_map)
1490 type = "map";
1491 if (obj.type == isl_obj_set ||
1492 obj.type == isl_obj_union_set)
1493 type = "set";
1494 if (obj.type == isl_obj_pw_qpolynomial ||
1495 obj.type == isl_obj_union_pw_qpolynomial)
1496 type = "piecewise quasipolynomial";
1497 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1498 obj.type == isl_obj_union_pw_qpolynomial_fold)
1499 type = "piecewise quasipolynomial fold";
1500 if (obj.type == isl_obj_list)
1501 type = "list";
1502 if (obj.type == isl_obj_bool)
1503 type = "boolean";
1504 if (obj.type == isl_obj_str)
1505 type = "string";
1506 if (obj.type == isl_obj_val)
1507 type = "value";
1509 free_obj(obj);
1510 obj.type = isl_obj_str;
1511 obj.v = isl_str_from_string(s->ctx, strdup(type));
1513 return obj;
1516 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1517 struct isl_hash_table *table)
1519 struct isl_obj obj;
1521 obj = read_obj(s, table);
1522 obj = convert(s->ctx, obj, isl_obj_union_set);
1523 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1524 return obj.v;
1525 error:
1526 free_obj(obj);
1527 return NULL;
1530 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1531 struct isl_hash_table *table)
1533 struct isl_obj obj;
1535 obj = read_obj(s, table);
1536 obj = convert(s->ctx, obj, isl_obj_union_map);
1537 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1538 return obj.v;
1539 error:
1540 free_obj(obj);
1541 return NULL;
1544 static struct isl_obj last_any(struct isl_stream *s,
1545 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1546 __isl_take isl_union_map *may_source)
1548 struct isl_obj obj = { isl_obj_none, NULL };
1549 isl_union_map *sink = NULL;
1550 isl_union_map *schedule = NULL;
1551 isl_union_map *may_dep;
1552 isl_union_map *must_dep;
1554 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1555 goto error;
1557 sink = read_map(s, table);
1558 if (!sink)
1559 goto error;
1561 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1562 goto error;
1564 schedule = read_map(s, table);
1565 if (!schedule)
1566 goto error;
1568 if (isl_union_map_compute_flow(sink, must_source, may_source,
1569 schedule, &must_dep, &may_dep,
1570 NULL, NULL) < 0)
1571 return obj;
1573 obj.type = isl_obj_union_map;
1574 obj.v = isl_union_map_union(must_dep, may_dep);
1576 return obj;
1577 error:
1578 isl_union_map_free(may_source);
1579 isl_union_map_free(must_source);
1580 isl_union_map_free(sink);
1581 isl_union_map_free(schedule);
1582 free_obj(obj);
1583 obj.type = isl_obj_none;
1584 obj.v = NULL;
1585 return obj;
1588 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1590 struct isl_obj obj = { isl_obj_none, NULL };
1591 isl_union_map *must_source = NULL;
1592 isl_union_map *may_source = NULL;
1593 isl_union_map *sink = NULL;
1594 isl_union_map *schedule = NULL;
1595 isl_union_map *may_dep;
1597 may_source = read_map(s, table);
1598 if (!may_source)
1599 goto error;
1601 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1602 must_source = read_map(s, table);
1603 if (!must_source)
1604 goto error;
1605 return last_any(s, table, must_source, may_source);
1608 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1609 goto error;
1611 sink = read_map(s, table);
1612 if (!sink)
1613 goto error;
1615 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1616 goto error;
1618 schedule = read_map(s, table);
1619 if (!schedule)
1620 goto error;
1622 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1623 if (isl_union_map_compute_flow(sink, must_source, may_source,
1624 schedule, NULL, &may_dep,
1625 NULL, NULL) < 0)
1626 return obj;
1628 obj.type = isl_obj_union_map;
1629 obj.v = may_dep;
1631 return obj;
1632 error:
1633 isl_union_map_free(may_source);
1634 isl_union_map_free(must_source);
1635 isl_union_map_free(sink);
1636 isl_union_map_free(schedule);
1637 free_obj(obj);
1638 obj.type = isl_obj_none;
1639 obj.v = NULL;
1640 return obj;
1643 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1645 struct isl_obj obj = { isl_obj_none, NULL };
1646 struct isl_list *list = NULL;
1647 isl_union_map *must_source = NULL;
1648 isl_union_map *may_source = NULL;
1649 isl_union_map *sink = NULL;
1650 isl_union_map *schedule = NULL;
1651 isl_union_map *must_dep;
1652 isl_union_map *must_no_source;
1654 must_source = read_map(s, table);
1655 if (!must_source)
1656 goto error;
1658 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1659 may_source = read_map(s, table);
1660 if (!may_source)
1661 goto error;
1662 return last_any(s, table, must_source, may_source);
1665 list = isl_list_alloc(s->ctx, 2);
1666 if (!list)
1667 goto error;
1669 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1670 goto error;
1672 sink = read_map(s, table);
1673 if (!sink)
1674 goto error;
1676 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1677 goto error;
1679 schedule = read_map(s, table);
1680 if (!schedule)
1681 goto error;
1683 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1684 if (isl_union_map_compute_flow(sink, must_source, may_source,
1685 schedule, &must_dep, NULL,
1686 &must_no_source, NULL) < 0) {
1687 isl_list_free(list);
1688 return obj;
1691 list->obj[0].type = isl_obj_union_map;
1692 list->obj[0].v = must_dep;
1693 list->obj[1].type = isl_obj_union_map;
1694 list->obj[1].v = must_no_source;
1696 obj.v = list;
1697 obj.type = isl_obj_list;
1699 return obj;
1700 error:
1701 isl_list_free(list);
1702 isl_union_map_free(may_source);
1703 isl_union_map_free(must_source);
1704 isl_union_map_free(sink);
1705 isl_union_map_free(schedule);
1706 free_obj(obj);
1707 obj.type = isl_obj_none;
1708 obj.v = NULL;
1709 return obj;
1712 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1713 struct isl_hash_table *table)
1715 isl_union_set *domain;
1716 isl_union_map *validity;
1717 isl_union_map *proximity;
1719 domain = read_set(s, table);
1720 if (!domain)
1721 return NULL;
1723 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1724 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1726 for (;;) {
1727 isl_union_map *umap;
1728 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1729 umap = read_map(s, table);
1730 validity = isl_union_map_union(validity, umap);
1731 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1732 umap = read_map(s, table);
1733 proximity = isl_union_map_union(proximity, umap);
1734 } else
1735 break;
1738 return isl_union_set_compute_schedule(domain, validity, proximity);
1741 static struct isl_obj schedule(struct isl_stream *s,
1742 struct isl_hash_table *table)
1744 struct isl_obj obj = { isl_obj_none, NULL };
1745 isl_schedule *schedule;
1747 schedule = get_schedule(s, table);
1749 obj.v = isl_schedule_get_map(schedule);
1750 obj.type = isl_obj_union_map;
1752 isl_schedule_free(schedule);
1754 return obj;
1757 /* Read a schedule for code generation.
1758 * If the input is a set rather than a map, then we construct
1759 * an identity schedule on the given set.
1761 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1762 struct isl_hash_table *table)
1764 struct isl_obj obj;
1766 obj = read_obj(s, table);
1768 if (is_subtype(obj, isl_obj_union_map)) {
1769 obj = convert(s->ctx, obj, isl_obj_union_map);
1770 return obj.v;
1773 if (is_subtype(obj, isl_obj_union_set)) {
1774 obj = convert(s->ctx, obj, isl_obj_union_set);
1775 return isl_union_set_identity(obj.v);
1778 free_obj(obj);
1779 isl_die(s->ctx, isl_error_invalid, "expecting set or map", return NULL);
1782 /* Generate an AST for the given schedule and options and print
1783 * the AST on the printer.
1785 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1786 __isl_take isl_union_map *schedule,
1787 __isl_take isl_union_map *options)
1789 isl_space *space;
1790 isl_set *context;
1791 isl_ast_build *build;
1792 isl_ast_node *tree;
1793 int format;
1795 space = isl_union_map_get_space(schedule);
1796 context = isl_set_universe(isl_space_params(space));
1798 build = isl_ast_build_from_context(context);
1799 build = isl_ast_build_set_options(build, options);
1800 tree = isl_ast_build_ast_from_schedule(build, schedule);
1801 isl_ast_build_free(build);
1803 if (!tree)
1804 return p;
1806 format = isl_printer_get_output_format(p);
1807 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1808 p = isl_printer_print_ast_node(p, tree);
1809 p = isl_printer_set_output_format(p, format);
1811 isl_ast_node_free(tree);
1813 return p;
1816 /* Perform the codegen operation.
1817 * In particular, read a schedule, check if the user has specified any options
1818 * and then generate an AST from the schedule (and options) and print it.
1820 static __isl_give isl_printer *codegen(struct isl_stream *s,
1821 struct isl_hash_table *table, __isl_take isl_printer *p)
1823 isl_union_map *schedule;
1824 isl_union_map *options;
1826 schedule = get_codegen_schedule(s, table);
1827 if (!schedule)
1828 return p;
1830 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1831 options = read_map(s, table);
1832 else
1833 options = isl_union_map_empty(
1834 isl_union_map_get_space(schedule));
1836 p = print_code(p, schedule, options);
1838 isl_stream_eat(s, ';');
1840 return p;
1843 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1845 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1847 struct isl_obj obj = { isl_obj_none, NULL };
1848 isl_ctx *ctx = isl_band_get_ctx(band);
1849 struct isl_list *list;
1851 list = isl_list_alloc(ctx, 2);
1852 if (!list)
1853 goto error;
1855 obj.v = list;
1856 obj.type = isl_obj_list;
1858 list->obj[0].type = isl_obj_union_map;
1859 list->obj[0].v = isl_band_get_partial_schedule(band);
1861 if (isl_band_has_children(band)) {
1862 isl_band_list *children;
1864 children = isl_band_get_children(band);
1865 list->obj[1] = band_list_to_obj_list(children);
1866 } else {
1867 list->obj[1].type = isl_obj_list;
1868 list->obj[1].v = isl_list_alloc(ctx, 0);
1871 if (!list->obj[0].v || !list->obj[1].v)
1872 goto error;
1874 isl_band_free(band);
1876 return obj;
1877 error:
1878 isl_band_free(band);
1879 free_obj(obj);
1880 obj.type = isl_obj_none;
1881 obj.v = NULL;
1882 return obj;
1885 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1887 struct isl_obj obj = { isl_obj_none, NULL };
1888 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1889 struct isl_list *list;
1890 int i, n;
1892 n = isl_band_list_n_band(bands);
1893 list = isl_list_alloc(ctx, n);
1894 if (!list)
1895 goto error;
1897 obj.v = list;
1898 obj.type = isl_obj_list;
1900 for (i = 0; i < n; ++i) {
1901 isl_band *band;
1903 band = isl_band_list_get_band(bands, i);
1904 list->obj[i] = band_to_obj_list(band);
1905 if (!list->obj[i].v)
1906 goto error;
1909 isl_band_list_free(bands);
1911 return obj;
1912 error:
1913 isl_band_list_free(bands);
1914 free_obj(obj);
1915 obj.type = isl_obj_none;
1916 obj.v = NULL;
1917 return obj;
1920 static struct isl_obj schedule_forest(struct isl_stream *s,
1921 struct isl_hash_table *table)
1923 struct isl_obj obj = { isl_obj_none, NULL };
1924 isl_schedule *schedule;
1925 isl_band_list *roots;
1927 schedule = get_schedule(s, table);
1928 if (!schedule)
1929 return obj;
1931 roots = isl_schedule_get_band_forest(schedule);
1932 isl_schedule_free(schedule);
1934 return band_list_to_obj_list(roots);
1937 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1939 struct isl_token *tok;
1940 isl_val *v;
1942 if (isl_stream_eat_if_available(s, '+'))
1943 return transitive_closure(s->ctx, obj);
1945 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1946 if (obj.type != isl_obj_union_map)
1947 obj = convert(s->ctx, obj, isl_obj_union_map);
1949 tok = isl_stream_next_token(s);
1950 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1951 isl_stream_error(s, tok, "expecting integer exponent");
1952 if (tok)
1953 isl_stream_push_token(s, tok);
1954 goto error;
1957 v = isl_token_get_val(s->ctx, tok);
1958 if (isl_val_is_zero(v)) {
1959 isl_stream_error(s, tok, "expecting non-zero exponent");
1960 isl_val_free(v);
1961 if (tok)
1962 isl_stream_push_token(s, tok);
1963 goto error;
1966 obj.v = isl_union_map_fixed_power_val(obj.v, v);
1967 isl_token_free(tok);
1968 if (!obj.v)
1969 goto error;
1971 return obj;
1972 error:
1973 free_obj(obj);
1974 obj.type = isl_obj_none;
1975 obj.v = NULL;
1976 return obj;
1979 static struct isl_obj check_assert(struct isl_stream *s,
1980 struct isl_hash_table *table)
1982 struct isl_obj obj;
1984 obj = read_expr(s, table);
1985 if (obj.type != isl_obj_bool)
1986 isl_die(s->ctx, isl_error_invalid,
1987 "expecting boolean expression", goto error);
1988 if (obj.v != &isl_bool_true)
1989 isl_die(s->ctx, isl_error_unknown,
1990 "assertion failed", abort());
1991 error:
1992 free_obj(obj);
1993 obj.type = isl_obj_none;
1994 obj.v = NULL;
1995 return obj;
1998 static struct isl_obj read_from_file(struct isl_stream *s)
2000 struct isl_obj obj;
2001 struct isl_token *tok;
2002 struct isl_stream *s_file;
2003 struct iscc_options *options;
2004 char *name;
2005 FILE *file;
2007 tok = isl_stream_next_token(s);
2008 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2009 isl_stream_error(s, tok, "expecting filename");
2010 isl_token_free(tok);
2011 goto error;
2014 options = isl_ctx_peek_iscc_options(s->ctx);
2015 if (!options || !options->io) {
2016 isl_token_free(tok);
2017 isl_die(s->ctx, isl_error_invalid,
2018 "read operation not allowed", goto error);
2021 name = isl_token_get_str(s->ctx, tok);
2022 isl_token_free(tok);
2023 file = fopen(name, "r");
2024 free(name);
2025 isl_assert(s->ctx, file, goto error);
2027 s_file = isl_stream_new_file(s->ctx, file);
2028 if (!s_file) {
2029 fclose(file);
2030 goto error;
2033 obj = isl_stream_read_obj(s_file);
2035 isl_stream_free(s_file);
2036 fclose(file);
2038 return obj;
2039 error:
2040 obj.type = isl_obj_none;
2041 obj.v = NULL;
2042 return obj;
2045 static struct isl_obj write_to_file(struct isl_stream *s,
2046 struct isl_hash_table *table)
2048 struct isl_obj obj;
2049 struct isl_token *tok;
2050 struct isl_stream *s_file;
2051 struct iscc_options *options;
2052 char *name;
2053 FILE *file;
2054 isl_printer *p;
2056 tok = isl_stream_next_token(s);
2057 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2058 isl_stream_error(s, tok, "expecting filename");
2059 isl_token_free(tok);
2060 goto error;
2063 obj = read_expr(s, table);
2065 options = isl_ctx_peek_iscc_options(s->ctx);
2066 if (!options || !options->io) {
2067 isl_token_free(tok);
2068 isl_die(s->ctx, isl_error_invalid,
2069 "write operation not allowed", goto error);
2072 name = isl_token_get_str(s->ctx, tok);
2073 isl_token_free(tok);
2074 file = fopen(name, "w");
2075 free(name);
2076 if (!file)
2077 isl_die(s->ctx, isl_error_unknown,
2078 "could not open file for writing", goto error);
2080 p = isl_printer_to_file(s->ctx, file);
2081 p = isl_printer_set_output_format(p, options->format);
2082 p = obj.type->print(p, obj.v);
2083 p = isl_printer_end_line(p);
2084 isl_printer_free(p);
2086 fclose(file);
2087 error:
2088 free_obj(obj);
2089 obj.type = isl_obj_none;
2090 obj.v = NULL;
2091 return obj;
2094 static struct isl_obj read_string_if_available(struct isl_stream *s)
2096 struct isl_token *tok;
2097 struct isl_obj obj = { isl_obj_none, NULL };
2099 tok = isl_stream_next_token(s);
2100 if (!tok)
2101 return obj;
2102 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2103 isl_str *str;
2104 str = isl_str_alloc(s->ctx);
2105 if (!str)
2106 goto error;
2107 str->s = isl_token_get_str(s->ctx, tok);
2108 isl_token_free(tok);
2109 obj.v = str;
2110 obj.type = isl_obj_str;
2111 } else
2112 isl_stream_push_token(s, tok);
2113 return obj;
2114 error:
2115 isl_token_free(tok);
2116 return obj;
2119 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2121 struct isl_token *tok;
2122 struct isl_obj obj = { isl_obj_none, NULL };
2123 int type;
2125 tok = isl_stream_next_token(s);
2126 if (!tok)
2127 return obj;
2128 type = isl_token_get_type(tok);
2129 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2130 int is_true = type == ISL_TOKEN_TRUE;
2131 isl_token_free(tok);
2132 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2133 obj.type = isl_obj_bool;
2134 } else
2135 isl_stream_push_token(s, tok);
2136 return obj;
2139 static __isl_give char *read_ident(struct isl_stream *s)
2141 char *name;
2142 isl_val *v;
2143 struct isl_token *tok, *tok2;
2145 name = isl_stream_read_ident_if_available(s);
2146 if (name)
2147 return name;
2149 tok = isl_stream_next_token(s);
2150 if (!tok)
2151 return NULL;
2152 if (isl_token_get_type(tok) != '$') {
2153 isl_stream_push_token(s, tok);
2154 return NULL;
2156 tok2 = isl_stream_next_token(s);
2157 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2158 if (tok2)
2159 isl_stream_push_token(s, tok2);
2160 isl_stream_push_token(s, tok);
2161 return NULL;
2164 v = isl_token_get_val(s->ctx, tok2);
2165 name = isl_val_to_str(v);
2166 isl_val_free(v);
2167 isl_token_free(tok);
2168 isl_token_free(tok2);
2170 return name;
2173 static struct isl_obj read_list(struct isl_stream *s,
2174 struct isl_hash_table *table, struct isl_obj obj)
2176 struct isl_list *list;
2178 list = isl_list_alloc(s->ctx, 2);
2179 if (!list)
2180 goto error;
2181 list->obj[0] = obj;
2182 list->obj[1] = read_obj(s, table);
2183 obj.v = list;
2184 obj.type = isl_obj_list;
2186 if (!list->obj[1].v)
2187 goto error;
2189 while (isl_stream_eat_if_available(s, ',')) {
2190 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2191 if (!obj.v)
2192 goto error;
2195 return obj;
2196 error:
2197 free_obj(obj);
2198 obj.type = isl_obj_none;
2199 obj.v = NULL;
2200 return obj;
2203 static struct isl_obj read_obj(struct isl_stream *s,
2204 struct isl_hash_table *table)
2206 struct isl_obj obj = { isl_obj_none, NULL };
2207 char *name = NULL;
2208 struct isc_un_op *op = NULL;
2210 obj = read_string_if_available(s);
2211 if (obj.v)
2212 return obj;
2213 obj = read_bool_if_available(s);
2214 if (obj.v)
2215 return obj;
2216 if (isl_stream_eat_if_available(s, '(')) {
2217 if (isl_stream_next_token_is(s, ')')) {
2218 obj.type = isl_obj_list;
2219 obj.v = isl_list_alloc(s->ctx, 0);
2220 } else {
2221 obj = read_expr(s, table);
2222 if (obj.v && isl_stream_eat_if_available(s, ','))
2223 obj = read_list(s, table, obj);
2225 if (!obj.v || isl_stream_eat(s, ')'))
2226 goto error;
2227 } else {
2228 op = read_prefix_un_op_if_available(s);
2229 if (op)
2230 return read_un_op_expr(s, table, op);
2232 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2233 return check_assert(s, table);
2234 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2235 return read_from_file(s);
2236 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2237 return write_to_file(s, table);
2238 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2239 return vertices(s, table);
2240 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2241 return any(s, table);
2242 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2243 return last(s, table);
2244 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2245 return schedule(s, table);
2246 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2247 return schedule_forest(s, table);
2248 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2249 return type_of(s, table);
2251 name = read_ident(s);
2252 if (name)
2253 obj = stored_obj(s->ctx, table, name);
2254 else
2255 obj = isl_stream_read_obj(s);
2256 if (!obj.v)
2257 goto error;
2260 if (isl_stream_eat_if_available(s, '^'))
2261 obj = power(s, obj);
2262 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2263 obj = obj_at_index(s, obj);
2264 else if (is_subtype(obj, isl_obj_union_map) &&
2265 isl_stream_eat_if_available(s, '(')) {
2266 obj = convert(s->ctx, obj, isl_obj_union_map);
2267 obj = apply(s, obj.v, table);
2268 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2269 isl_stream_eat_if_available(s, '(')) {
2270 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2271 obj = apply_fun(s, obj, table);
2272 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2273 isl_stream_eat_if_available(s, '(')) {
2274 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2275 obj = apply_fun(s, obj, table);
2278 return obj;
2279 error:
2280 free_obj(obj);
2281 obj.type = isl_obj_none;
2282 obj.v = NULL;
2283 return obj;
2286 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2287 struct isl_obj lhs, struct isl_obj rhs)
2289 int i;
2291 for (i = 0; ; ++i) {
2292 if (!bin_ops[i].op)
2293 break;
2294 if (bin_ops[i].op != like->op)
2295 continue;
2296 if (!is_subtype(lhs, bin_ops[i].lhs))
2297 continue;
2298 if (!is_subtype(rhs, bin_ops[i].rhs))
2299 continue;
2301 return &bin_ops[i];
2304 for (i = 0; ; ++i) {
2305 if (!named_bin_ops[i].name)
2306 break;
2307 if (named_bin_ops[i].op.op != like->op)
2308 continue;
2309 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2310 continue;
2311 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2312 continue;
2314 return &named_bin_ops[i].op;
2317 return NULL;
2320 static int next_is_neg_int(struct isl_stream *s)
2322 struct isl_token *tok;
2323 int ret;
2325 tok = isl_stream_next_token(s);
2326 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2327 isl_val *v;
2328 v = isl_token_get_val(s->ctx, tok);
2329 ret = isl_val_is_neg(v);
2330 isl_val_free(v);
2331 } else
2332 ret = 0;
2333 isl_stream_push_token(s, tok);
2335 return ret;
2338 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2339 struct isl_obj lhs, struct isl_obj rhs)
2341 struct isl_obj obj;
2343 lhs = convert(ctx, lhs, op->lhs);
2344 rhs = convert(ctx, rhs, op->rhs);
2345 if (op->res != isl_obj_bool)
2346 obj.v = op->o.fn(lhs.v, rhs.v);
2347 else {
2348 int res = op->o.test(lhs.v, rhs.v);
2349 free_obj(lhs);
2350 free_obj(rhs);
2351 obj.v = isl_bool_from_int(res);
2353 obj.type = op->res;
2355 return obj;
2358 static struct isl_obj read_expr(struct isl_stream *s,
2359 struct isl_hash_table *table)
2361 struct isl_obj obj = { isl_obj_none, NULL };
2362 struct isl_obj right_obj = { isl_obj_none, NULL };
2364 obj = read_obj(s, table);
2365 for (; obj.v;) {
2366 struct isc_bin_op *op = NULL;
2368 op = read_bin_op_if_available(s, obj);
2369 if (!op)
2370 break;
2372 right_obj = read_obj(s, table);
2374 op = find_matching_bin_op(op, obj, right_obj);
2376 if (!op)
2377 isl_die(s->ctx, isl_error_invalid,
2378 "no such binary operator defined on given operands",
2379 goto error);
2381 obj = call_bin_op(s->ctx, op, obj, right_obj);
2384 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2385 right_obj = read_obj(s, table);
2386 obj.v = isl_val_add(obj.v, right_obj.v);
2389 return obj;
2390 error:
2391 free_obj(right_obj);
2392 free_obj(obj);
2393 obj.type = isl_obj_none;
2394 obj.v = NULL;
2395 return obj;
2398 static __isl_give isl_printer *source_file(struct isl_stream *s,
2399 struct isl_hash_table *table, __isl_take isl_printer *p);
2401 static __isl_give isl_printer *read_line(struct isl_stream *s,
2402 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2404 struct isl_obj obj = { isl_obj_none, NULL };
2405 char *lhs = NULL;
2406 int assign = 0;
2407 int only_print = 0;
2408 struct isc_bin_op *op = NULL;
2409 char buf[30];
2411 if (!p)
2412 return NULL;
2413 if (isl_stream_is_empty(s))
2414 return p;
2416 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2417 return source_file(s, table, p);
2418 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2419 return codegen(s, table, p);
2421 assign = is_assign(s);
2422 if (assign) {
2423 lhs = isl_stream_read_ident_if_available(s);
2424 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2425 goto error;
2426 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2427 only_print = 1;
2428 else if (!tty)
2429 only_print = 1;
2431 obj = read_expr(s, table);
2432 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2433 fprintf(stderr, "Interrupted\n");
2434 isl_ctx_reset_error(s->ctx);
2436 if (isl_stream_eat(s, ';'))
2437 goto error;
2439 if (only_print) {
2440 if (obj.type != isl_obj_none && obj.v != NULL) {
2441 p = obj.type->print(p, obj.v);
2442 p = isl_printer_end_line(p);
2444 free_obj(obj);
2445 return p;
2447 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2448 static int count = 0;
2449 snprintf(buf, sizeof(buf), "$%d", count++);
2450 lhs = strdup(buf + 1);
2452 p = isl_printer_print_str(p, buf);
2453 p = isl_printer_print_str(p, " := ");
2454 p = obj.type->print(p, obj.v);
2455 p = isl_printer_end_line(p);
2457 if (lhs && do_assign(s->ctx, table, lhs, obj))
2458 return p;
2460 return p;
2461 error:
2462 isl_stream_flush_tokens(s);
2463 isl_stream_skip_line(s);
2464 free(lhs);
2465 free_obj(obj);
2466 return p;
2469 int free_cb(void **entry, void *user)
2471 struct isl_named_obj *named = *entry;
2473 free_obj(named->obj);
2474 free(named->name);
2475 free(named);
2477 return 0;
2480 static void register_named_ops(struct isl_stream *s)
2482 int i;
2484 for (i = 0; i < ISCC_N_OP; ++i) {
2485 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2486 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2489 for (i = 0; ; ++i) {
2490 if (!named_un_ops[i].name)
2491 break;
2492 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2493 named_un_ops[i].name);
2494 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2497 for (i = 0; ; ++i) {
2498 if (!named_bin_ops[i].name)
2499 break;
2500 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2501 named_bin_ops[i].name);
2502 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2506 static __isl_give isl_printer *source_file(struct isl_stream *s,
2507 struct isl_hash_table *table, __isl_take isl_printer *p)
2509 struct isl_token *tok;
2510 struct isl_stream *s_file;
2511 char *name;
2512 FILE *file;
2514 tok = isl_stream_next_token(s);
2515 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2516 isl_stream_error(s, tok, "expecting filename");
2517 isl_token_free(tok);
2518 return p;
2521 name = isl_token_get_str(s->ctx, tok);
2522 isl_token_free(tok);
2523 file = fopen(name, "r");
2524 free(name);
2525 isl_assert(s->ctx, file, return p);
2527 s_file = isl_stream_new_file(s->ctx, file);
2528 if (!s_file) {
2529 fclose(file);
2530 return p;
2533 register_named_ops(s_file);
2535 while (!s_file->eof)
2536 p = read_line(s_file, table, p, 0);
2538 isl_stream_free(s_file);
2539 fclose(file);
2541 isl_stream_eat(s, ';');
2543 return p;
2546 int main(int argc, char **argv)
2548 struct isl_ctx *ctx;
2549 struct isl_stream *s;
2550 struct isl_hash_table *table;
2551 struct iscc_options *options;
2552 isl_printer *p;
2553 int tty = isatty(0);
2555 options = iscc_options_new_with_defaults();
2556 assert(options);
2558 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2559 pet_options_set_autodetect(ctx, 1);
2560 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2561 s = isl_stream_new_file(ctx, stdin);
2562 assert(s);
2563 table = isl_hash_table_alloc(ctx, 10);
2564 assert(table);
2565 p = isl_printer_to_file(ctx, stdout);
2566 p = isl_printer_set_output_format(p, options->format);
2567 assert(p);
2569 register_named_ops(s);
2571 install_signal_handler(ctx);
2573 while (p && !s->eof) {
2574 isl_ctx_resume(ctx);
2575 p = read_line(s, table, p, tty);
2578 remove_signal_handler(ctx);
2580 isl_printer_free(p);
2581 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2582 isl_hash_table_free(ctx, table);
2583 isl_stream_free(s);
2584 isl_ctx_free(ctx);
2586 return 0;