barvinok 0.37
[barvinok.git] / iscc.c
blob52ed31556439f94417f9710e71a1840d6ae14a37
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/aff.h>
7 #include <isl/obj.h>
8 #include <isl/stream.h>
9 #include <isl/set.h>
10 #include <isl/map.h>
11 #include <isl/vertices.h>
12 #include <isl/flow.h>
13 #include <isl/band.h>
14 #include <isl/schedule.h>
15 #include <isl/ast_build.h>
16 #include <isl_obj_list.h>
17 #include <isl_obj_str.h>
18 #include <barvinok/isl.h>
19 #include <barvinok/options.h>
20 #include "lattice_width.h"
22 #include "config.h"
24 #ifdef HAVE_SIGACTION
25 #include <signal.h>
27 static isl_ctx *main_ctx;
29 static void handler(int signum)
31 if (isl_ctx_aborted(main_ctx))
32 exit(EXIT_FAILURE);
33 isl_ctx_abort(main_ctx);
36 static struct sigaction sa_old;
38 static void install_signal_handler(isl_ctx *ctx)
40 struct sigaction sa;
42 main_ctx = ctx;
44 memset(&sa, 0, sizeof(struct sigaction));
45 sa.sa_handler = &handler;
46 sa.sa_flags = SA_RESTART;
47 sigaction(SIGINT, &sa, &sa_old);
50 static void remove_signal_handler(isl_ctx *ctx)
52 sigaction(SIGINT, &sa_old, NULL);
55 #else
57 static void install_signal_handler(isl_ctx *ctx)
61 static void remove_signal_handler(isl_ctx *ctx)
65 #endif
67 #ifdef HAVE_PET
68 #include <pet.h>
69 #else
70 struct pet_options;
71 int pet_options_set_autodetect(isl_ctx *ctx, int val)
73 return -1;
75 #endif
77 static int isl_bool_false = 0;
78 static int isl_bool_true = 1;
79 static int isl_bool_error = -1;
81 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
82 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
83 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
84 ISCC_MINIMIZING, ISCC_RESPECTING,
85 ISCC_CODEGEN, ISCC_USING,
86 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
87 ISCC_N_OP };
88 static const char *op_name[ISCC_N_OP] = {
89 [ISCC_ASSERT] = "assert",
90 [ISCC_READ] = "read",
91 [ISCC_WRITE] = "write",
92 [ISCC_PRINT] = "print",
93 [ISCC_SOURCE] = "source",
94 [ISCC_VERTICES] = "vertices",
95 [ISCC_LAST] = "last",
96 [ISCC_ANY] = "any",
97 [ISCC_BEFORE] = "before",
98 [ISCC_UNDER] = "under",
99 [ISCC_SCHEDULE] = "schedule",
100 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
101 [ISCC_MINIMIZING] = "minimizing",
102 [ISCC_RESPECTING] = "respecting",
103 [ISCC_CODEGEN] = "codegen",
104 [ISCC_USING] = "using",
105 [ISCC_TYPEOF] = "typeof"
107 static enum isl_token_type iscc_op[ISCC_N_OP];
109 struct isl_arg_choice iscc_format[] = {
110 {"isl", ISL_FORMAT_ISL},
111 {"omega", ISL_FORMAT_OMEGA},
112 {"polylib", ISL_FORMAT_POLYLIB},
113 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
114 {"latex", ISL_FORMAT_LATEX},
115 {"C", ISL_FORMAT_C},
119 struct iscc_options {
120 struct barvinok_options *barvinok;
121 struct pet_options *pet;
122 unsigned format;
123 int io;
126 ISL_ARGS_START(struct iscc_options, iscc_options_args)
127 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
128 "barvinok options")
129 #ifdef HAVE_PET
130 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
131 #endif
132 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
133 iscc_format, ISL_FORMAT_ISL, "output format")
134 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
135 "allow read and write operations")
136 ISL_ARGS_END
138 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
139 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
141 static void *isl_obj_bool_copy(void *v)
143 return v;
146 static void isl_obj_bool_free(void *v)
150 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
151 void *v)
153 if (v == &isl_bool_true)
154 return isl_printer_print_str(p, "True");
155 else if (v == &isl_bool_false)
156 return isl_printer_print_str(p, "False");
157 else
158 return isl_printer_print_str(p, "Error");
161 static void *isl_obj_bool_add(void *v1, void *v2)
163 return v1;
166 struct isl_obj_vtable isl_obj_bool_vtable = {
167 isl_obj_bool_copy,
168 isl_obj_bool_add,
169 isl_obj_bool_print,
170 isl_obj_bool_free
172 #define isl_obj_bool (&isl_obj_bool_vtable)
174 int *isl_bool_from_int(int res)
176 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
179 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
180 __isl_take isl_union_map *map2)
182 return isl_union_map_is_subset(map2, map1);
184 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
185 __isl_take isl_union_set *set2)
187 return isl_union_set_is_subset(set2, set1);
190 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
191 __isl_take isl_union_map *map2)
193 return isl_union_map_is_strict_subset(map2, map1);
195 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
196 __isl_take isl_union_set *set2)
198 return isl_union_set_is_strict_subset(set2, set1);
201 extern struct isl_obj_vtable isl_obj_list_vtable;
202 #define isl_obj_list (&isl_obj_list_vtable)
204 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
205 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
206 struct isc_bin_op {
207 enum isl_token_type op;
208 isl_obj_type lhs;
209 isl_obj_type rhs;
210 isl_obj_type res;
211 union {
212 isc_bin_op_fn fn;
213 isc_bin_test_fn test;
214 } o;
216 struct isc_named_bin_op {
217 char *name;
218 struct isc_bin_op op;
221 struct iscc_at {
222 isl_union_pw_qpolynomial *upwqp;
223 isl_union_pw_qpolynomial *res;
226 static int eval_at(__isl_take isl_point *pnt, void *user)
228 struct iscc_at *at = (struct iscc_at *) user;
229 isl_val *v;
230 isl_qpolynomial *qp;
231 isl_set *set;
233 set = isl_set_from_point(isl_point_copy(pnt));
234 v = isl_union_pw_qpolynomial_eval(
235 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
236 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
238 at->res = isl_union_pw_qpolynomial_add(at->res,
239 isl_union_pw_qpolynomial_from_pw_qpolynomial(
240 isl_pw_qpolynomial_alloc(set, qp)));
242 return 0;
245 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
246 __isl_take isl_union_pw_qpolynomial *upwqp,
247 __isl_take isl_union_set *uset)
249 struct iscc_at at;
251 at.upwqp = upwqp;
252 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
254 isl_union_set_foreach_point(uset, eval_at, &at);
256 isl_union_pw_qpolynomial_free(upwqp);
257 isl_union_set_free(uset);
259 return at.res;
262 struct iscc_fold_at {
263 isl_union_pw_qpolynomial_fold *upwf;
264 isl_union_pw_qpolynomial *res;
267 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
269 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
270 isl_val *v;
271 isl_qpolynomial *qp;
272 isl_set *set;
274 set = isl_set_from_point(isl_point_copy(pnt));
275 v = isl_union_pw_qpolynomial_fold_eval(
276 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
277 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
279 at->res = isl_union_pw_qpolynomial_add(at->res,
280 isl_union_pw_qpolynomial_from_pw_qpolynomial(
281 isl_pw_qpolynomial_alloc(set, qp)));
283 return 0;
286 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
287 __isl_take isl_union_pw_qpolynomial_fold *upwf,
288 __isl_take isl_union_set *uset)
290 struct iscc_fold_at at;
292 at.upwf = upwf;
293 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
295 isl_union_set_foreach_point(uset, eval_fold_at, &at);
297 isl_union_pw_qpolynomial_fold_free(upwf);
298 isl_union_set_free(uset);
300 return at.res;
303 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
304 __isl_take isl_union_pw_qpolynomial *upwqp,
305 __isl_take isl_union_pw_qpolynomial_fold *upwf)
307 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
308 upwqp);
311 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
312 __isl_take isl_union_map *umap,
313 __isl_take isl_union_pw_qpolynomial_fold *upwf)
315 isl_ctx *ctx;
316 struct isl_list *list;
317 int tight;
319 ctx = isl_union_map_get_ctx(umap);
320 list = isl_list_alloc(ctx, 2);
321 if (!list)
322 goto error2;
324 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
325 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
326 upwf, &tight);
327 list->obj[1].type = isl_obj_bool;
328 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
329 if (tight < 0 || !list->obj[0].v)
330 goto error;
332 return list;
333 error2:
334 isl_union_map_free(umap);
335 isl_union_pw_qpolynomial_fold_free(upwf);
336 error:
337 isl_list_free(list);
338 return NULL;
341 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
342 __isl_take isl_union_set *uset,
343 __isl_take isl_union_pw_qpolynomial_fold *upwf)
345 isl_ctx *ctx;
346 struct isl_list *list;
347 int tight;
349 ctx = isl_union_set_get_ctx(uset);
350 list = isl_list_alloc(ctx, 2);
351 if (!list)
352 goto error2;
354 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
355 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
356 upwf, &tight);
357 list->obj[1].type = isl_obj_bool;
358 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
359 if (tight < 0 || !list->obj[0].v)
360 goto error;
362 return list;
363 error2:
364 isl_union_set_free(uset);
365 isl_union_pw_qpolynomial_fold_free(upwf);
366 error:
367 isl_list_free(list);
368 return NULL;
371 static __isl_give isl_union_pw_qpolynomial *isl_val_mul_union_pw_qpolynomial(
372 __isl_take isl_val *v, __isl_take isl_union_pw_qpolynomial *upwqp)
374 return isl_union_pw_qpolynomial_scale_val(upwqp, v);
377 static __isl_give isl_union_pw_qpolynomial_fold *
378 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val *v,
379 __isl_take isl_union_pw_qpolynomial_fold *upwf)
381 return isl_union_pw_qpolynomial_fold_scale_val(upwf, v);
384 struct isc_bin_op bin_ops[] = {
385 { '+', isl_obj_val, isl_obj_val, isl_obj_val,
386 (isc_bin_op_fn) &isl_val_add },
387 { '-', isl_obj_val, isl_obj_val, isl_obj_val,
388 (isc_bin_op_fn) &isl_val_sub },
389 { '*', isl_obj_val, isl_obj_val, isl_obj_val,
390 (isc_bin_op_fn) &isl_val_mul },
391 { '+', isl_obj_pw_multi_aff, isl_obj_pw_multi_aff,
392 isl_obj_pw_multi_aff,
393 (isc_bin_op_fn) &isl_pw_multi_aff_add },
394 { '+', isl_obj_union_set, isl_obj_union_set,
395 isl_obj_union_set,
396 (isc_bin_op_fn) &isl_union_set_union },
397 { '+', isl_obj_union_map, isl_obj_union_map,
398 isl_obj_union_map,
399 (isc_bin_op_fn) &isl_union_map_union },
400 { '-', isl_obj_union_set, isl_obj_union_set,
401 isl_obj_union_set,
402 (isc_bin_op_fn) &isl_union_set_subtract },
403 { '-', isl_obj_union_map, isl_obj_union_map,
404 isl_obj_union_map,
405 (isc_bin_op_fn) &isl_union_map_subtract },
406 { '*', isl_obj_union_set, isl_obj_union_set,
407 isl_obj_union_set,
408 (isc_bin_op_fn) &isl_union_set_intersect },
409 { '*', isl_obj_union_map, isl_obj_union_map,
410 isl_obj_union_map,
411 (isc_bin_op_fn) &isl_union_map_intersect },
412 { '*', isl_obj_union_map, isl_obj_union_set,
413 isl_obj_union_map,
414 (isc_bin_op_fn) &isl_union_map_intersect_domain },
415 { '.', isl_obj_union_map, isl_obj_union_map,
416 isl_obj_union_map,
417 (isc_bin_op_fn) &isl_union_map_apply_range },
418 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
419 isl_obj_union_pw_qpolynomial,
420 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
421 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
422 isl_obj_list,
423 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
424 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
425 isl_obj_union_map,
426 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
427 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
428 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
429 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
430 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
431 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
432 isl_obj_bool,
433 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
434 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
435 isl_obj_bool,
436 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
437 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
438 isl_obj_bool,
439 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
440 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
441 isl_obj_bool,
442 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
443 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
444 isl_obj_bool,
445 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
446 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
447 isl_obj_bool,
448 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
449 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
450 isl_obj_bool,
451 { .test =
452 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
453 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
454 isl_obj_bool,
455 { .test =
456 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
457 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
458 isl_obj_union_map,
459 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
460 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
461 isl_obj_union_map,
462 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
463 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
464 isl_obj_union_map,
465 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
466 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
467 isl_obj_union_map,
468 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
469 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
470 isl_obj_union_map,
471 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
472 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
473 isl_obj_union_map,
474 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
475 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
476 isl_obj_union_map,
477 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
478 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
479 isl_obj_union_map,
480 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
481 { '.', isl_obj_union_pw_qpolynomial_fold,
482 isl_obj_union_pw_qpolynomial_fold,
483 isl_obj_union_pw_qpolynomial_fold,
484 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
485 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
486 isl_obj_union_pw_qpolynomial,
487 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
488 { '+', isl_obj_union_pw_qpolynomial,
489 isl_obj_union_pw_qpolynomial_fold,
490 isl_obj_union_pw_qpolynomial_fold,
491 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
492 { '+', isl_obj_union_pw_qpolynomial_fold,
493 isl_obj_union_pw_qpolynomial,
494 isl_obj_union_pw_qpolynomial_fold,
495 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
496 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
497 isl_obj_union_pw_qpolynomial,
498 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
499 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
500 isl_obj_union_pw_qpolynomial,
501 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
502 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
503 isl_obj_union_pw_qpolynomial,
504 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
505 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
506 isl_obj_union_pw_qpolynomial_fold,
507 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
508 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
509 isl_obj_union_pw_qpolynomial_fold,
510 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
511 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
512 isl_obj_union_pw_qpolynomial,
513 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
514 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
515 isl_obj_union_pw_qpolynomial,
516 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
517 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
518 isl_obj_union_pw_qpolynomial_fold,
519 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
520 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
521 isl_obj_union_pw_qpolynomial,
522 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
523 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
524 isl_obj_union_pw_qpolynomial,
525 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
526 { '%', isl_obj_union_set, isl_obj_union_set,
527 isl_obj_union_set,
528 (isc_bin_op_fn) &isl_union_set_gist },
529 { '%', isl_obj_union_map, isl_obj_union_map,
530 isl_obj_union_map,
531 (isc_bin_op_fn) &isl_union_map_gist },
532 { '%', isl_obj_union_map, isl_obj_union_set,
533 isl_obj_union_map,
534 (isc_bin_op_fn) &isl_union_map_gist_domain },
535 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
536 isl_obj_union_pw_qpolynomial,
537 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
538 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
539 isl_obj_union_pw_qpolynomial_fold,
540 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
541 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
542 isl_obj_union_pw_qpolynomial, isl_obj_bool,
543 { .test = (isc_bin_test_fn)
544 &isl_union_pw_qpolynomial_plain_is_equal } },
545 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
546 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
547 { .test = (isc_bin_test_fn)
548 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
549 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
550 (isc_bin_op_fn) &isl_str_concat },
554 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
555 __isl_take isl_union_map *umap2)
557 return isl_union_map_apply_range(umap2, umap1);
560 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
561 __isl_take isl_union_pw_qpolynomial *upwqp,
562 __isl_take isl_union_map *umap)
564 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
567 static __isl_give struct isl_list *qpolynomial_fold_after_map(
568 __isl_take isl_union_pw_qpolynomial_fold *upwf,
569 __isl_take isl_union_map *umap)
571 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
574 struct isc_named_bin_op named_bin_ops[] = {
575 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
576 isl_obj_union_map,
577 (isc_bin_op_fn) &map_after_map } },
578 { "after", { -1, isl_obj_union_pw_qpolynomial,
579 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
580 (isc_bin_op_fn) &qpolynomial_after_map } },
581 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
582 isl_obj_union_map, isl_obj_list,
583 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
584 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
585 isl_obj_union_map,
586 (isc_bin_op_fn) &isl_union_map_apply_range } },
587 { "before", { -1, isl_obj_union_map,
588 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
589 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
590 { "before", { -1, isl_obj_union_map,
591 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
592 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
593 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
594 isl_obj_union_set,
595 (isc_bin_op_fn) &isl_union_set_product } },
596 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
597 isl_obj_union_map,
598 (isc_bin_op_fn) &isl_union_map_product } },
599 NULL
602 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
604 return isl_set_from_basic_set(isl_union_set_sample(uset));
607 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
609 return isl_map_from_basic_map(isl_union_map_sample(umap));
612 static __isl_give struct isl_list *union_map_power(
613 __isl_take isl_union_map *umap)
615 isl_ctx *ctx;
616 struct isl_list *list;
617 int exact;
619 ctx = isl_union_map_get_ctx(umap);
620 list = isl_list_alloc(ctx, 2);
621 if (!list)
622 goto error2;
624 list->obj[0].type = isl_obj_union_map;
625 list->obj[0].v = isl_union_map_power(umap, &exact);
626 list->obj[1].type = isl_obj_bool;
627 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
628 if (exact < 0 || !list->obj[0].v)
629 goto error;
631 return list;
632 error2:
633 isl_union_map_free(umap);
634 error:
635 isl_list_free(list);
636 return NULL;
639 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
640 __isl_take isl_union_pw_qpolynomial *upwqp)
642 isl_ctx *ctx;
643 struct isl_list *list;
644 int tight;
646 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
647 list = isl_list_alloc(ctx, 2);
648 if (!list)
649 goto error2;
651 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
652 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
653 isl_fold_max, &tight);
654 list->obj[1].type = isl_obj_bool;
655 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
656 if (tight < 0 || !list->obj[0].v)
657 goto error;
659 return list;
660 error2:
661 isl_union_pw_qpolynomial_free(upwqp);
662 error:
663 isl_list_free(list);
664 return NULL;
667 #ifdef HAVE_PET
668 static __isl_give isl_list *parse(__isl_take isl_str *str)
670 isl_ctx *ctx;
671 struct isl_list *list;
672 struct pet_scop *scop;
673 isl_union_map *sched, *may_reads, *must_writes, *may_writes;
674 isl_union_set *domain;
675 struct iscc_options *options;
677 if (!str)
678 return NULL;
679 ctx = str->ctx;
681 options = isl_ctx_peek_iscc_options(ctx);
682 if (!options || !options->io) {
683 isl_str_free(str);
684 isl_die(ctx, isl_error_invalid,
685 "parse_file operation not allowed", return NULL);
688 list = isl_list_alloc(ctx, 5);
689 if (!list)
690 goto error;
692 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
693 domain = pet_scop_collect_domains(scop);
694 sched = pet_scop_collect_schedule(scop);
695 may_reads = pet_scop_collect_may_reads(scop);
696 may_writes = pet_scop_collect_may_writes(scop);
697 must_writes = pet_scop_collect_must_writes(scop);
698 pet_scop_free(scop);
700 list->obj[0].type = isl_obj_union_set;
701 list->obj[0].v = domain;
702 list->obj[1].type = isl_obj_union_map;
703 list->obj[1].v = must_writes;
704 list->obj[2].type = isl_obj_union_map;
705 list->obj[2].v = may_writes;
706 list->obj[3].type = isl_obj_union_map;
707 list->obj[3].v = may_reads;
708 list->obj[4].type = isl_obj_union_map;
709 list->obj[4].v = sched;
711 if (!list->obj[0].v || !list->obj[1].v ||
712 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
713 goto error;
715 isl_str_free(str);
716 return list;
717 error:
718 isl_list_free(list);
719 isl_str_free(str);
720 return NULL;
722 #endif
724 static int add_point(__isl_take isl_point *pnt, void *user)
726 isl_union_set **scan = (isl_union_set **) user;
728 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
730 return 0;
733 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
735 isl_union_set *scan;
737 scan = isl_union_set_empty(isl_union_set_get_space(uset));
739 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
740 isl_union_set_free(scan);
741 return uset;
744 isl_union_set_free(uset);
745 return scan;
748 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
750 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
753 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
754 __isl_take isl_union_pw_qpolynomial *upwqp)
756 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
759 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
760 __isl_take isl_union_pw_qpolynomial *upwqp)
762 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
765 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
766 __isl_take isl_union_pw_qpolynomial *upwqp)
768 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
771 typedef void *(*isc_un_op_fn)(void *arg);
772 struct isc_un_op {
773 enum isl_token_type op;
774 isl_obj_type arg;
775 isl_obj_type res;
776 isc_un_op_fn fn;
778 struct isc_named_un_op {
779 char *name;
780 struct isc_un_op op;
782 struct isc_named_un_op named_un_ops[] = {
783 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
784 (isc_un_op_fn) &isl_union_map_affine_hull } },
785 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
786 (isc_un_op_fn) &isl_union_set_affine_hull } },
787 {"card", { -1, isl_obj_union_set,
788 isl_obj_union_pw_qpolynomial,
789 (isc_un_op_fn) &isl_union_set_card } },
790 {"card", { -1, isl_obj_union_map,
791 isl_obj_union_pw_qpolynomial,
792 (isc_un_op_fn) &isl_union_map_card } },
793 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
794 (isc_un_op_fn) &isl_union_set_coalesce } },
795 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
796 (isc_un_op_fn) &isl_union_map_coalesce } },
797 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
798 isl_obj_union_pw_qpolynomial,
799 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
800 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
801 isl_obj_union_pw_qpolynomial_fold,
802 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
803 {"coefficients", { -1, isl_obj_union_set,
804 isl_obj_union_set,
805 (isc_un_op_fn) &isl_union_set_coefficients } },
806 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
807 (isc_un_op_fn) &isl_union_set_solutions } },
808 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
809 (isc_un_op_fn) &isl_union_map_deltas } },
810 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
811 (isc_un_op_fn) &isl_union_map_deltas_map } },
812 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
813 (isc_un_op_fn) &isl_union_map_domain } },
814 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
815 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
816 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
817 isl_obj_union_set,
818 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
819 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
820 (isc_un_op_fn) &isl_union_map_domain } },
821 {"domain", { -1, isl_obj_union_pw_qpolynomial,
822 isl_obj_union_set,
823 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
824 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
825 isl_obj_union_set,
826 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
827 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
828 (isc_un_op_fn) &isl_union_map_domain_map } },
829 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
830 (isc_un_op_fn) &isl_union_map_range } },
831 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
832 (isc_un_op_fn) &isl_union_map_range } },
833 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
834 (isc_un_op_fn) &isl_union_map_range_map } },
835 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
836 (isc_un_op_fn) &isl_union_set_identity } },
837 {"lattice_width", { -1, isl_obj_union_set,
838 isl_obj_union_pw_qpolynomial,
839 (isc_un_op_fn) &isl_union_set_lattice_width } },
840 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
841 (isc_un_op_fn) &isl_union_map_lexmin } },
842 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
843 (isc_un_op_fn) &isl_union_map_lexmax } },
844 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
845 (isc_un_op_fn) &isl_union_set_lexmin } },
846 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
847 (isc_un_op_fn) &isl_union_set_lexmax } },
848 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
849 (isc_un_op_fn) &isl_union_set_lift } },
850 {"params", { -1, isl_obj_union_map, isl_obj_set,
851 (isc_un_op_fn) &isl_union_map_params } },
852 {"params", { -1, isl_obj_union_set, isl_obj_set,
853 (isc_un_op_fn) &isl_union_set_params } },
854 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
855 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
856 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
857 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
858 {"poly", { -1, isl_obj_union_pw_qpolynomial,
859 isl_obj_union_pw_qpolynomial,
860 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
861 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
862 isl_obj_union_pw_qpolynomial,
863 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
864 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
865 isl_obj_union_pw_qpolynomial,
866 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
867 #ifdef HAVE_PET
868 {"parse_file", { -1, isl_obj_str, isl_obj_list,
869 (isc_un_op_fn) &parse } },
870 #endif
871 {"pow", { -1, isl_obj_union_map, isl_obj_list,
872 (isc_un_op_fn) &union_map_power } },
873 {"sample", { -1, isl_obj_union_set, isl_obj_set,
874 (isc_un_op_fn) &union_set_sample } },
875 {"sample", { -1, isl_obj_union_map, isl_obj_map,
876 (isc_un_op_fn) &union_map_sample } },
877 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
878 (isc_un_op_fn) &union_set_scan } },
879 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
880 (isc_un_op_fn) &union_map_scan } },
881 {"sum", { -1, isl_obj_union_pw_qpolynomial,
882 isl_obj_union_pw_qpolynomial,
883 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
884 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
885 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
886 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
887 (isc_un_op_fn) &isl_union_set_unwrap } },
888 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
889 (isc_un_op_fn) &isl_union_map_wrap } },
890 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
891 (isc_un_op_fn) &isl_union_map_zip } },
892 NULL
895 struct isl_named_obj {
896 char *name;
897 struct isl_obj obj;
900 static void free_obj(struct isl_obj obj)
902 obj.type->free(obj.v);
905 static int same_name(const void *entry, const void *val)
907 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
909 return !strcmp(named->name, val);
912 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
913 char *name, struct isl_obj obj)
915 struct isl_hash_table_entry *entry;
916 uint32_t name_hash;
917 struct isl_named_obj *named;
919 name_hash = isl_hash_string(isl_hash_init(), name);
920 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
921 if (!entry)
922 goto error;
923 if (entry->data) {
924 named = entry->data;
925 free_obj(named->obj);
926 free(name);
927 } else {
928 named = isl_alloc_type(ctx, struct isl_named_obj);
929 if (!named)
930 goto error;
931 named->name = name;
932 entry->data = named;
934 named->obj = obj;
936 return 0;
937 error:
938 free_obj(obj);
939 free(name);
940 return -1;
943 static struct isl_obj stored_obj(struct isl_ctx *ctx,
944 struct isl_hash_table *table, char *name)
946 struct isl_obj obj = { isl_obj_none, NULL };
947 struct isl_hash_table_entry *entry;
948 uint32_t name_hash;
950 name_hash = isl_hash_string(isl_hash_init(), name);
951 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
952 if (entry) {
953 struct isl_named_obj *named;
954 named = entry->data;
955 obj = named->obj;
956 } else if (isdigit(name[0]))
957 fprintf(stderr, "unknown identifier '$%s'\n", name);
958 else
959 fprintf(stderr, "unknown identifier '%s'\n", name);
961 free(name);
962 obj.v = obj.type->copy(obj.v);
963 return obj;
966 static int is_subtype(struct isl_obj obj, isl_obj_type super)
968 if (obj.type == super)
969 return 1;
970 if (obj.type == isl_obj_map && super == isl_obj_union_map)
971 return 1;
972 if (obj.type == isl_obj_set && super == isl_obj_union_set)
973 return 1;
974 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
975 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
976 int is_set = isl_space_is_set(space);
977 isl_space_free(space);
978 return is_set;
980 if (obj.type == isl_obj_pw_qpolynomial &&
981 super == isl_obj_union_pw_qpolynomial)
982 return 1;
983 if (obj.type == isl_obj_pw_qpolynomial_fold &&
984 super == isl_obj_union_pw_qpolynomial_fold)
985 return 1;
986 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
987 return 1;
988 if (obj.type == isl_obj_list) {
989 struct isl_list *list = obj.v;
990 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
991 return is_subtype(list->obj[0], super);
993 if (super == isl_obj_str)
994 return 1;
995 return 0;
998 static struct isl_obj obj_at(struct isl_obj obj, int i)
1000 struct isl_list *list = obj.v;
1002 obj = list->obj[i];
1003 obj.v = obj.type->copy(obj.v);
1005 isl_list_free(list);
1007 return obj;
1010 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1011 isl_obj_type type)
1013 if (obj.type == type)
1014 return obj;
1015 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1016 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1017 obj.type = isl_obj_union_set;
1018 obj.v = isl_union_set_from_set(set);
1019 return obj;
1021 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1022 obj.type = isl_obj_union_map;
1023 obj.v = isl_union_map_from_map(obj.v);
1024 return obj;
1026 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1027 obj.type = isl_obj_union_set;
1028 obj.v = isl_union_set_from_set(obj.v);
1029 return obj;
1031 if (obj.type == isl_obj_pw_qpolynomial &&
1032 type == isl_obj_union_pw_qpolynomial) {
1033 obj.type = isl_obj_union_pw_qpolynomial;
1034 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1035 return obj;
1037 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1038 type == isl_obj_union_pw_qpolynomial_fold) {
1039 obj.type = isl_obj_union_pw_qpolynomial_fold;
1040 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1041 return obj;
1043 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1044 if (type == isl_obj_union_map) {
1045 obj.type = isl_obj_union_map;
1046 return obj;
1048 if (type == isl_obj_union_pw_qpolynomial) {
1049 isl_space *dim = isl_union_set_get_space(obj.v);
1050 isl_union_set_free(obj.v);
1051 obj.v = isl_union_pw_qpolynomial_zero(dim);
1052 obj.type = isl_obj_union_pw_qpolynomial;
1053 return obj;
1055 if (type == isl_obj_union_pw_qpolynomial_fold) {
1056 isl_space *dim = isl_union_set_get_space(obj.v);
1057 isl_union_set_free(obj.v);
1058 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1059 isl_fold_list);
1060 obj.type = isl_obj_union_pw_qpolynomial_fold;
1061 return obj;
1064 if (obj.type == isl_obj_list) {
1065 struct isl_list *list = obj.v;
1066 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1067 return convert(ctx, obj_at(obj, 0), type);
1069 if (type == isl_obj_str) {
1070 isl_str *str;
1071 isl_printer *p;
1072 char *s;
1074 p = isl_printer_to_str(ctx);
1075 if (!p)
1076 goto error;
1077 p = obj.type->print(p, obj.v);
1078 s = isl_printer_get_str(p);
1079 isl_printer_free(p);
1081 str = isl_str_from_string(ctx, s);
1082 if (!str)
1083 goto error;
1084 free_obj(obj);
1085 obj.v = str;
1086 obj.type = isl_obj_str;
1087 return obj;
1090 error:
1091 free_obj(obj);
1092 obj.type = isl_obj_none;
1093 obj.v = NULL;
1094 return obj;
1097 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1098 struct isl_obj lhs)
1100 int i;
1101 struct isl_token *tok;
1103 tok = isl_stream_next_token(s);
1104 if (!tok)
1105 return NULL;
1107 for (i = 0; ; ++i) {
1108 if (!bin_ops[i].op)
1109 break;
1110 if (bin_ops[i].op != isl_token_get_type(tok))
1111 continue;
1112 if (!is_subtype(lhs, bin_ops[i].lhs))
1113 continue;
1115 isl_token_free(tok);
1116 return &bin_ops[i];
1119 for (i = 0; ; ++i) {
1120 if (!named_bin_ops[i].name)
1121 break;
1122 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1123 continue;
1124 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1125 continue;
1127 isl_token_free(tok);
1128 return &named_bin_ops[i].op;
1131 isl_stream_push_token(s, tok);
1133 return NULL;
1136 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1138 int i;
1139 struct isl_token *tok;
1141 tok = isl_stream_next_token(s);
1142 if (!tok)
1143 return NULL;
1145 for (i = 0; ; ++i) {
1146 if (!named_un_ops[i].name)
1147 break;
1148 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1149 continue;
1151 isl_token_free(tok);
1152 return &named_un_ops[i].op;
1155 isl_stream_push_token(s, tok);
1157 return NULL;
1160 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1161 struct isl_obj arg)
1163 int i;
1165 for (i = 0; ; ++i) {
1166 if (!named_un_ops[i].name)
1167 break;
1168 if (named_un_ops[i].op.op != like->op)
1169 continue;
1170 if (!is_subtype(arg, named_un_ops[i].op.arg))
1171 continue;
1173 return &named_un_ops[i].op;
1176 return NULL;
1179 static int is_assign(struct isl_stream *s)
1181 struct isl_token *tok;
1182 struct isl_token *tok2;
1183 int assign;
1185 tok = isl_stream_next_token(s);
1186 if (!tok)
1187 return 0;
1188 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1189 isl_stream_push_token(s, tok);
1190 return 0;
1193 tok2 = isl_stream_next_token(s);
1194 if (!tok2) {
1195 isl_stream_push_token(s, tok);
1196 return 0;
1198 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1199 isl_stream_push_token(s, tok2);
1200 isl_stream_push_token(s, tok);
1202 return assign;
1205 static struct isl_obj read_obj(struct isl_stream *s,
1206 struct isl_hash_table *table);
1207 static struct isl_obj read_expr(struct isl_stream *s,
1208 struct isl_hash_table *table);
1210 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1211 struct isl_hash_table *table, struct isc_un_op *op)
1213 struct isl_obj obj = { isl_obj_none, NULL };
1215 obj = read_obj(s, table);
1216 if (!obj.v)
1217 goto error;
1219 op = find_matching_un_op(op, obj);
1221 if (!op)
1222 isl_die(s->ctx, isl_error_invalid,
1223 "no such unary operator defined on given operand",
1224 goto error);
1226 obj = convert(s->ctx, obj, op->arg);
1227 obj.v = op->fn(obj.v);
1228 obj.type = op->res;
1230 return obj;
1231 error:
1232 free_obj(obj);
1233 obj.type = isl_obj_none;
1234 obj.v = NULL;
1235 return obj;
1238 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1240 struct isl_list *list;
1241 int exact;
1243 if (obj.type != isl_obj_union_map)
1244 obj = convert(ctx, obj, isl_obj_union_map);
1245 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1246 list = isl_list_alloc(ctx, 2);
1247 if (!list)
1248 goto error;
1250 list->obj[0].type = isl_obj_union_map;
1251 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1252 list->obj[1].type = isl_obj_bool;
1253 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1254 obj.v = list;
1255 obj.type = isl_obj_list;
1256 if (exact < 0 || !list->obj[0].v)
1257 goto error;
1259 return obj;
1260 error:
1261 free_obj(obj);
1262 obj.type = isl_obj_none;
1263 obj.v = NULL;
1264 return obj;
1267 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1269 struct isl_list *list = obj.v;
1270 struct isl_token *tok;
1271 isl_val *v;
1272 int i;
1274 tok = isl_stream_next_token(s);
1275 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1276 isl_stream_error(s, tok, "expecting index");
1277 if (tok)
1278 isl_stream_push_token(s, tok);
1279 goto error;
1281 v = isl_token_get_val(s->ctx, tok);
1282 i = isl_val_get_num_si(v);
1283 isl_val_free(v);
1284 isl_token_free(tok);
1285 isl_assert(s->ctx, i < list->n, goto error);
1286 if (isl_stream_eat(s, ']'))
1287 goto error;
1289 return obj_at(obj, i);
1290 error:
1291 free_obj(obj);
1292 obj.type = isl_obj_none;
1293 obj.v = NULL;
1294 return obj;
1297 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1298 struct isl_hash_table *table)
1300 struct isl_obj obj;
1302 obj = read_expr(s, table);
1303 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1304 is_subtype(obj, isl_obj_union_map), goto error);
1306 if (obj.type == isl_obj_list) {
1307 struct isl_list *list = obj.v;
1308 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1309 obj = obj_at(obj, 0);
1311 if (obj.type == isl_obj_set)
1312 obj = convert(s->ctx, obj, isl_obj_union_set);
1313 else if (obj.type == isl_obj_map)
1314 obj = convert(s->ctx, obj, isl_obj_union_map);
1315 if (obj.type == isl_obj_union_set) {
1316 obj.v = isl_union_set_apply(obj.v, umap);
1317 } else
1318 obj.v = isl_union_map_apply_range(obj.v, umap);
1319 if (!obj.v)
1320 goto error2;
1322 if (isl_stream_eat(s, ')'))
1323 goto error2;
1325 return obj;
1326 error:
1327 isl_union_map_free(umap);
1328 error2:
1329 free_obj(obj);
1330 obj.type = isl_obj_none;
1331 obj.v = NULL;
1332 return obj;
1335 static struct isl_obj apply_fun_set(struct isl_obj obj,
1336 __isl_take isl_union_set *uset)
1338 if (obj.type == isl_obj_union_pw_qpolynomial) {
1339 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1340 } else {
1341 obj.type = isl_obj_list;
1342 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1344 return obj;
1347 static struct isl_obj apply_fun_map(struct isl_obj obj,
1348 __isl_take isl_union_map *umap)
1350 if (obj.type == isl_obj_union_pw_qpolynomial) {
1351 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1352 } else {
1353 obj.type = isl_obj_list;
1354 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1356 return obj;
1359 static struct isl_obj apply_fun(struct isl_stream *s,
1360 struct isl_obj obj, struct isl_hash_table *table)
1362 struct isl_obj arg;
1364 arg = read_expr(s, table);
1365 if (!is_subtype(arg, isl_obj_union_map) &&
1366 !is_subtype(arg, isl_obj_union_set))
1367 isl_die(s->ctx, isl_error_invalid,
1368 "expecting set of map argument", goto error);
1370 if (arg.type == isl_obj_list) {
1371 struct isl_list *list = arg.v;
1372 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1373 arg = obj_at(arg, 0);
1375 if (arg.type == isl_obj_set)
1376 arg = convert(s->ctx, arg, isl_obj_union_set);
1377 else if (arg.type == isl_obj_map)
1378 arg = convert(s->ctx, arg, isl_obj_union_map);
1379 if (arg.type == isl_obj_union_set)
1380 obj = apply_fun_set(obj, arg.v);
1381 else
1382 obj = apply_fun_map(obj, arg.v);
1383 if (!obj.v)
1384 goto error2;
1386 if (isl_stream_eat(s, ')'))
1387 goto error2;
1389 return obj;
1390 error:
1391 free_obj(arg);
1392 error2:
1393 free_obj(obj);
1394 obj.type = isl_obj_none;
1395 obj.v = NULL;
1396 return obj;
1399 struct add_vertex_data {
1400 struct isl_list *list;
1401 int i;
1404 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1406 struct add_vertex_data *data = (struct add_vertex_data *)user;
1407 isl_multi_aff *ma;
1408 isl_set *dom;
1410 ma = isl_vertex_get_expr(vertex);
1411 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1413 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1414 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1415 data->i++;
1417 isl_vertex_free(vertex);
1419 return 0;
1422 static int set_vertices(__isl_take isl_set *set, void *user)
1424 isl_ctx *ctx;
1425 isl_basic_set *hull;
1426 isl_vertices *vertices = NULL;
1427 struct isl_list *list = NULL;
1428 int r;
1429 struct add_vertex_data *data = (struct add_vertex_data *)user;
1431 set = isl_set_remove_divs(set);
1432 hull = isl_set_convex_hull(set);
1433 vertices = isl_basic_set_compute_vertices(hull);
1434 isl_basic_set_free(hull);
1436 list = data->list;
1438 ctx = isl_vertices_get_ctx(vertices);
1439 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1440 if (!data->list)
1441 goto error;
1443 data->i = 0;
1444 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1446 data->list = isl_list_concat(list, data->list);
1448 isl_vertices_free(vertices);
1450 return r;
1451 error:
1452 data->list = list;
1453 isl_vertices_free(vertices);
1454 return -1;
1457 static struct isl_obj vertices(struct isl_stream *s,
1458 struct isl_hash_table *table)
1460 isl_ctx *ctx;
1461 struct isl_obj obj;
1462 struct isl_list *list = NULL;
1463 isl_union_set *uset;
1464 struct add_vertex_data data = { NULL };
1466 obj = read_expr(s, table);
1467 obj = convert(s->ctx, obj, isl_obj_union_set);
1468 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1469 uset = obj.v;
1470 obj.v = NULL;
1472 ctx = isl_union_set_get_ctx(uset);
1473 list = isl_list_alloc(ctx, 0);
1474 if (!list)
1475 goto error;
1477 data.list = list;
1479 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1480 goto error;
1482 isl_union_set_free(uset);
1484 obj.type = isl_obj_list;
1485 obj.v = data.list;
1487 return obj;
1488 error:
1489 isl_union_set_free(uset);
1490 isl_list_free(data.list);
1491 free_obj(obj);
1492 obj.type = isl_obj_none;
1493 obj.v = NULL;
1494 return obj;
1497 static struct isl_obj type_of(struct isl_stream *s,
1498 struct isl_hash_table *table)
1500 isl_ctx *ctx;
1501 struct isl_obj obj;
1502 const char *type = "unknown";
1504 obj = read_expr(s, table);
1506 if (obj.type == isl_obj_map ||
1507 obj.type == isl_obj_union_map)
1508 type = "map";
1509 if (obj.type == isl_obj_set ||
1510 obj.type == isl_obj_union_set)
1511 type = "set";
1512 if (obj.type == isl_obj_pw_multi_aff)
1513 type = "piecewise multi-quasiaffine expression";
1514 if (obj.type == isl_obj_pw_qpolynomial ||
1515 obj.type == isl_obj_union_pw_qpolynomial)
1516 type = "piecewise quasipolynomial";
1517 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1518 obj.type == isl_obj_union_pw_qpolynomial_fold)
1519 type = "piecewise quasipolynomial fold";
1520 if (obj.type == isl_obj_list)
1521 type = "list";
1522 if (obj.type == isl_obj_bool)
1523 type = "boolean";
1524 if (obj.type == isl_obj_str)
1525 type = "string";
1526 if (obj.type == isl_obj_val)
1527 type = "value";
1529 free_obj(obj);
1530 obj.type = isl_obj_str;
1531 obj.v = isl_str_from_string(s->ctx, strdup(type));
1533 return obj;
1536 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1537 struct isl_hash_table *table)
1539 struct isl_obj obj;
1541 obj = read_obj(s, table);
1542 obj = convert(s->ctx, obj, isl_obj_union_set);
1543 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1544 return obj.v;
1545 error:
1546 free_obj(obj);
1547 return NULL;
1550 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1551 struct isl_hash_table *table)
1553 struct isl_obj obj;
1555 obj = read_obj(s, table);
1556 obj = convert(s->ctx, obj, isl_obj_union_map);
1557 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1558 return obj.v;
1559 error:
1560 free_obj(obj);
1561 return NULL;
1564 static struct isl_obj last_any(struct isl_stream *s,
1565 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1566 __isl_take isl_union_map *may_source)
1568 struct isl_obj obj = { isl_obj_none, NULL };
1569 isl_union_map *sink = NULL;
1570 isl_union_map *schedule = NULL;
1571 isl_union_map *may_dep;
1572 isl_union_map *must_dep;
1574 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1575 goto error;
1577 sink = read_map(s, table);
1578 if (!sink)
1579 goto error;
1581 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1582 goto error;
1584 schedule = read_map(s, table);
1585 if (!schedule)
1586 goto error;
1588 if (isl_union_map_compute_flow(sink, must_source, may_source,
1589 schedule, &must_dep, &may_dep,
1590 NULL, NULL) < 0)
1591 return obj;
1593 obj.type = isl_obj_union_map;
1594 obj.v = isl_union_map_union(must_dep, may_dep);
1596 return obj;
1597 error:
1598 isl_union_map_free(may_source);
1599 isl_union_map_free(must_source);
1600 isl_union_map_free(sink);
1601 isl_union_map_free(schedule);
1602 free_obj(obj);
1603 obj.type = isl_obj_none;
1604 obj.v = NULL;
1605 return obj;
1608 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1610 struct isl_obj obj = { isl_obj_none, NULL };
1611 isl_union_map *must_source = NULL;
1612 isl_union_map *may_source = NULL;
1613 isl_union_map *sink = NULL;
1614 isl_union_map *schedule = NULL;
1615 isl_union_map *may_dep;
1617 may_source = read_map(s, table);
1618 if (!may_source)
1619 goto error;
1621 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1622 must_source = read_map(s, table);
1623 if (!must_source)
1624 goto error;
1625 return last_any(s, table, must_source, may_source);
1628 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1629 goto error;
1631 sink = read_map(s, table);
1632 if (!sink)
1633 goto error;
1635 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1636 goto error;
1638 schedule = read_map(s, table);
1639 if (!schedule)
1640 goto error;
1642 must_source = isl_union_map_empty(isl_union_map_get_space(sink));
1643 if (isl_union_map_compute_flow(sink, must_source, may_source,
1644 schedule, NULL, &may_dep,
1645 NULL, NULL) < 0)
1646 return obj;
1648 obj.type = isl_obj_union_map;
1649 obj.v = may_dep;
1651 return obj;
1652 error:
1653 isl_union_map_free(may_source);
1654 isl_union_map_free(must_source);
1655 isl_union_map_free(sink);
1656 isl_union_map_free(schedule);
1657 free_obj(obj);
1658 obj.type = isl_obj_none;
1659 obj.v = NULL;
1660 return obj;
1663 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1665 struct isl_obj obj = { isl_obj_none, NULL };
1666 struct isl_list *list = NULL;
1667 isl_union_map *must_source = NULL;
1668 isl_union_map *may_source = NULL;
1669 isl_union_map *sink = NULL;
1670 isl_union_map *schedule = NULL;
1671 isl_union_map *must_dep;
1672 isl_union_map *must_no_source;
1674 must_source = read_map(s, table);
1675 if (!must_source)
1676 goto error;
1678 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1679 may_source = read_map(s, table);
1680 if (!may_source)
1681 goto error;
1682 return last_any(s, table, must_source, may_source);
1685 list = isl_list_alloc(s->ctx, 2);
1686 if (!list)
1687 goto error;
1689 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1690 goto error;
1692 sink = read_map(s, table);
1693 if (!sink)
1694 goto error;
1696 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1697 goto error;
1699 schedule = read_map(s, table);
1700 if (!schedule)
1701 goto error;
1703 may_source = isl_union_map_empty(isl_union_map_get_space(sink));
1704 if (isl_union_map_compute_flow(sink, must_source, may_source,
1705 schedule, &must_dep, NULL,
1706 &must_no_source, NULL) < 0) {
1707 isl_list_free(list);
1708 return obj;
1711 list->obj[0].type = isl_obj_union_map;
1712 list->obj[0].v = must_dep;
1713 list->obj[1].type = isl_obj_union_map;
1714 list->obj[1].v = must_no_source;
1716 obj.v = list;
1717 obj.type = isl_obj_list;
1719 return obj;
1720 error:
1721 isl_list_free(list);
1722 isl_union_map_free(may_source);
1723 isl_union_map_free(must_source);
1724 isl_union_map_free(sink);
1725 isl_union_map_free(schedule);
1726 free_obj(obj);
1727 obj.type = isl_obj_none;
1728 obj.v = NULL;
1729 return obj;
1732 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1733 struct isl_hash_table *table)
1735 isl_union_set *domain;
1736 isl_union_map *validity;
1737 isl_union_map *proximity;
1739 domain = read_set(s, table);
1740 if (!domain)
1741 return NULL;
1743 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1744 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1746 for (;;) {
1747 isl_union_map *umap;
1748 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1749 umap = read_map(s, table);
1750 validity = isl_union_map_union(validity, umap);
1751 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1752 umap = read_map(s, table);
1753 proximity = isl_union_map_union(proximity, umap);
1754 } else
1755 break;
1758 return isl_union_set_compute_schedule(domain, validity, proximity);
1761 static struct isl_obj schedule(struct isl_stream *s,
1762 struct isl_hash_table *table)
1764 struct isl_obj obj = { isl_obj_none, NULL };
1765 isl_schedule *schedule;
1767 schedule = get_schedule(s, table);
1769 obj.v = isl_schedule_get_map(schedule);
1770 obj.type = isl_obj_union_map;
1772 isl_schedule_free(schedule);
1774 return obj;
1777 /* Read a schedule for code generation.
1778 * If the input is a set rather than a map, then we construct
1779 * an identity schedule on the given set.
1781 static __isl_give isl_union_map *get_codegen_schedule(struct isl_stream *s,
1782 struct isl_hash_table *table)
1784 struct isl_obj obj;
1786 obj = read_obj(s, table);
1788 if (is_subtype(obj, isl_obj_union_map)) {
1789 obj = convert(s->ctx, obj, isl_obj_union_map);
1790 return obj.v;
1793 if (is_subtype(obj, isl_obj_union_set)) {
1794 obj = convert(s->ctx, obj, isl_obj_union_set);
1795 return isl_union_set_identity(obj.v);
1798 free_obj(obj);
1799 isl_die(s->ctx, isl_error_invalid, "expecting set or map", return NULL);
1802 /* Generate an AST for the given schedule and options and print
1803 * the AST on the printer.
1805 static __isl_give isl_printer *print_code(__isl_take isl_printer *p,
1806 __isl_take isl_union_map *schedule,
1807 __isl_take isl_union_map *options)
1809 isl_space *space;
1810 isl_set *context;
1811 isl_ast_build *build;
1812 isl_ast_node *tree;
1813 int format;
1815 space = isl_union_map_get_space(schedule);
1816 context = isl_set_universe(isl_space_params(space));
1818 build = isl_ast_build_from_context(context);
1819 build = isl_ast_build_set_options(build, options);
1820 tree = isl_ast_build_ast_from_schedule(build, schedule);
1821 isl_ast_build_free(build);
1823 if (!tree)
1824 return p;
1826 format = isl_printer_get_output_format(p);
1827 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1828 p = isl_printer_print_ast_node(p, tree);
1829 p = isl_printer_set_output_format(p, format);
1831 isl_ast_node_free(tree);
1833 return p;
1836 /* Perform the codegen operation.
1837 * In particular, read a schedule, check if the user has specified any options
1838 * and then generate an AST from the schedule (and options) and print it.
1840 static __isl_give isl_printer *codegen(struct isl_stream *s,
1841 struct isl_hash_table *table, __isl_take isl_printer *p)
1843 isl_union_map *schedule;
1844 isl_union_map *options;
1846 schedule = get_codegen_schedule(s, table);
1847 if (!schedule)
1848 return p;
1850 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1851 options = read_map(s, table);
1852 else
1853 options = isl_union_map_empty(
1854 isl_union_map_get_space(schedule));
1856 p = print_code(p, schedule, options);
1858 isl_stream_eat(s, ';');
1860 return p;
1863 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1865 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1867 struct isl_obj obj = { isl_obj_none, NULL };
1868 isl_ctx *ctx = isl_band_get_ctx(band);
1869 struct isl_list *list;
1871 list = isl_list_alloc(ctx, 2);
1872 if (!list)
1873 goto error;
1875 obj.v = list;
1876 obj.type = isl_obj_list;
1878 list->obj[0].type = isl_obj_union_map;
1879 list->obj[0].v = isl_band_get_partial_schedule(band);
1881 if (isl_band_has_children(band)) {
1882 isl_band_list *children;
1884 children = isl_band_get_children(band);
1885 list->obj[1] = band_list_to_obj_list(children);
1886 } else {
1887 list->obj[1].type = isl_obj_list;
1888 list->obj[1].v = isl_list_alloc(ctx, 0);
1891 if (!list->obj[0].v || !list->obj[1].v)
1892 goto error;
1894 isl_band_free(band);
1896 return obj;
1897 error:
1898 isl_band_free(band);
1899 free_obj(obj);
1900 obj.type = isl_obj_none;
1901 obj.v = NULL;
1902 return obj;
1905 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
1907 struct isl_obj obj = { isl_obj_none, NULL };
1908 isl_ctx *ctx = isl_band_list_get_ctx(bands);
1909 struct isl_list *list;
1910 int i, n;
1912 n = isl_band_list_n_band(bands);
1913 list = isl_list_alloc(ctx, n);
1914 if (!list)
1915 goto error;
1917 obj.v = list;
1918 obj.type = isl_obj_list;
1920 for (i = 0; i < n; ++i) {
1921 isl_band *band;
1923 band = isl_band_list_get_band(bands, i);
1924 list->obj[i] = band_to_obj_list(band);
1925 if (!list->obj[i].v)
1926 goto error;
1929 isl_band_list_free(bands);
1931 return obj;
1932 error:
1933 isl_band_list_free(bands);
1934 free_obj(obj);
1935 obj.type = isl_obj_none;
1936 obj.v = NULL;
1937 return obj;
1940 static struct isl_obj schedule_forest(struct isl_stream *s,
1941 struct isl_hash_table *table)
1943 struct isl_obj obj = { isl_obj_none, NULL };
1944 isl_schedule *schedule;
1945 isl_band_list *roots;
1947 schedule = get_schedule(s, table);
1948 if (!schedule)
1949 return obj;
1951 roots = isl_schedule_get_band_forest(schedule);
1952 isl_schedule_free(schedule);
1954 return band_list_to_obj_list(roots);
1957 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1959 struct isl_token *tok;
1960 isl_val *v;
1962 if (isl_stream_eat_if_available(s, '+'))
1963 return transitive_closure(s->ctx, obj);
1965 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1966 if (obj.type != isl_obj_union_map)
1967 obj = convert(s->ctx, obj, isl_obj_union_map);
1969 tok = isl_stream_next_token(s);
1970 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1971 isl_stream_error(s, tok, "expecting integer exponent");
1972 if (tok)
1973 isl_stream_push_token(s, tok);
1974 goto error;
1977 v = isl_token_get_val(s->ctx, tok);
1978 if (isl_val_is_zero(v)) {
1979 isl_stream_error(s, tok, "expecting non-zero exponent");
1980 isl_val_free(v);
1981 if (tok)
1982 isl_stream_push_token(s, tok);
1983 goto error;
1986 obj.v = isl_union_map_fixed_power_val(obj.v, v);
1987 isl_token_free(tok);
1988 if (!obj.v)
1989 goto error;
1991 return obj;
1992 error:
1993 free_obj(obj);
1994 obj.type = isl_obj_none;
1995 obj.v = NULL;
1996 return obj;
1999 static struct isl_obj check_assert(struct isl_stream *s,
2000 struct isl_hash_table *table)
2002 struct isl_obj obj;
2004 obj = read_expr(s, table);
2005 if (obj.type != isl_obj_bool)
2006 isl_die(s->ctx, isl_error_invalid,
2007 "expecting boolean expression", goto error);
2008 if (obj.v != &isl_bool_true)
2009 isl_die(s->ctx, isl_error_unknown,
2010 "assertion failed", abort());
2011 error:
2012 free_obj(obj);
2013 obj.type = isl_obj_none;
2014 obj.v = NULL;
2015 return obj;
2018 static struct isl_obj read_from_file(struct isl_stream *s)
2020 struct isl_obj obj;
2021 struct isl_token *tok;
2022 struct isl_stream *s_file;
2023 struct iscc_options *options;
2024 char *name;
2025 FILE *file;
2027 tok = isl_stream_next_token(s);
2028 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2029 isl_stream_error(s, tok, "expecting filename");
2030 isl_token_free(tok);
2031 goto error;
2034 options = isl_ctx_peek_iscc_options(s->ctx);
2035 if (!options || !options->io) {
2036 isl_token_free(tok);
2037 isl_die(s->ctx, isl_error_invalid,
2038 "read operation not allowed", goto error);
2041 name = isl_token_get_str(s->ctx, tok);
2042 isl_token_free(tok);
2043 file = fopen(name, "r");
2044 free(name);
2045 isl_assert(s->ctx, file, goto error);
2047 s_file = isl_stream_new_file(s->ctx, file);
2048 if (!s_file) {
2049 fclose(file);
2050 goto error;
2053 obj = isl_stream_read_obj(s_file);
2055 isl_stream_free(s_file);
2056 fclose(file);
2058 return obj;
2059 error:
2060 obj.type = isl_obj_none;
2061 obj.v = NULL;
2062 return obj;
2065 static struct isl_obj write_to_file(struct isl_stream *s,
2066 struct isl_hash_table *table)
2068 struct isl_obj obj;
2069 struct isl_token *tok;
2070 struct isl_stream *s_file;
2071 struct iscc_options *options;
2072 char *name;
2073 FILE *file;
2074 isl_printer *p;
2076 tok = isl_stream_next_token(s);
2077 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2078 isl_stream_error(s, tok, "expecting filename");
2079 isl_token_free(tok);
2080 goto error;
2083 obj = read_expr(s, table);
2085 options = isl_ctx_peek_iscc_options(s->ctx);
2086 if (!options || !options->io) {
2087 isl_token_free(tok);
2088 isl_die(s->ctx, isl_error_invalid,
2089 "write operation not allowed", goto error);
2092 name = isl_token_get_str(s->ctx, tok);
2093 isl_token_free(tok);
2094 file = fopen(name, "w");
2095 free(name);
2096 if (!file)
2097 isl_die(s->ctx, isl_error_unknown,
2098 "could not open file for writing", goto error);
2100 p = isl_printer_to_file(s->ctx, file);
2101 p = isl_printer_set_output_format(p, options->format);
2102 p = obj.type->print(p, obj.v);
2103 p = isl_printer_end_line(p);
2104 isl_printer_free(p);
2106 fclose(file);
2107 error:
2108 free_obj(obj);
2109 obj.type = isl_obj_none;
2110 obj.v = NULL;
2111 return obj;
2114 static struct isl_obj read_string_if_available(struct isl_stream *s)
2116 struct isl_token *tok;
2117 struct isl_obj obj = { isl_obj_none, NULL };
2119 tok = isl_stream_next_token(s);
2120 if (!tok)
2121 return obj;
2122 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2123 isl_str *str;
2124 str = isl_str_alloc(s->ctx);
2125 if (!str)
2126 goto error;
2127 str->s = isl_token_get_str(s->ctx, tok);
2128 isl_token_free(tok);
2129 obj.v = str;
2130 obj.type = isl_obj_str;
2131 } else
2132 isl_stream_push_token(s, tok);
2133 return obj;
2134 error:
2135 isl_token_free(tok);
2136 return obj;
2139 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2141 struct isl_token *tok;
2142 struct isl_obj obj = { isl_obj_none, NULL };
2143 int type;
2145 tok = isl_stream_next_token(s);
2146 if (!tok)
2147 return obj;
2148 type = isl_token_get_type(tok);
2149 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2150 int is_true = type == ISL_TOKEN_TRUE;
2151 isl_token_free(tok);
2152 obj.v = is_true ? &isl_bool_true : &isl_bool_false;
2153 obj.type = isl_obj_bool;
2154 } else
2155 isl_stream_push_token(s, tok);
2156 return obj;
2159 static __isl_give char *read_ident(struct isl_stream *s)
2161 char *name;
2162 isl_val *v;
2163 struct isl_token *tok, *tok2;
2165 name = isl_stream_read_ident_if_available(s);
2166 if (name)
2167 return name;
2169 tok = isl_stream_next_token(s);
2170 if (!tok)
2171 return NULL;
2172 if (isl_token_get_type(tok) != '$') {
2173 isl_stream_push_token(s, tok);
2174 return NULL;
2176 tok2 = isl_stream_next_token(s);
2177 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2178 if (tok2)
2179 isl_stream_push_token(s, tok2);
2180 isl_stream_push_token(s, tok);
2181 return NULL;
2184 v = isl_token_get_val(s->ctx, tok2);
2185 name = isl_val_to_str(v);
2186 isl_val_free(v);
2187 isl_token_free(tok);
2188 isl_token_free(tok2);
2190 return name;
2193 static struct isl_obj read_list(struct isl_stream *s,
2194 struct isl_hash_table *table, struct isl_obj obj)
2196 struct isl_list *list;
2198 list = isl_list_alloc(s->ctx, 2);
2199 if (!list)
2200 goto error;
2201 list->obj[0] = obj;
2202 list->obj[1] = read_obj(s, table);
2203 obj.v = list;
2204 obj.type = isl_obj_list;
2206 if (!list->obj[1].v)
2207 goto error;
2209 while (isl_stream_eat_if_available(s, ',')) {
2210 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2211 if (!obj.v)
2212 goto error;
2215 return obj;
2216 error:
2217 free_obj(obj);
2218 obj.type = isl_obj_none;
2219 obj.v = NULL;
2220 return obj;
2223 static struct isl_obj read_obj(struct isl_stream *s,
2224 struct isl_hash_table *table)
2226 struct isl_obj obj = { isl_obj_none, NULL };
2227 char *name = NULL;
2228 struct isc_un_op *op = NULL;
2230 obj = read_string_if_available(s);
2231 if (obj.v)
2232 return obj;
2233 obj = read_bool_if_available(s);
2234 if (obj.v)
2235 return obj;
2236 if (isl_stream_eat_if_available(s, '(')) {
2237 if (isl_stream_next_token_is(s, ')')) {
2238 obj.type = isl_obj_list;
2239 obj.v = isl_list_alloc(s->ctx, 0);
2240 } else {
2241 obj = read_expr(s, table);
2242 if (obj.v && isl_stream_eat_if_available(s, ','))
2243 obj = read_list(s, table, obj);
2245 if (!obj.v || isl_stream_eat(s, ')'))
2246 goto error;
2247 } else {
2248 op = read_prefix_un_op_if_available(s);
2249 if (op)
2250 return read_un_op_expr(s, table, op);
2252 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2253 return check_assert(s, table);
2254 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2255 return read_from_file(s);
2256 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2257 return write_to_file(s, table);
2258 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2259 return vertices(s, table);
2260 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2261 return any(s, table);
2262 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2263 return last(s, table);
2264 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2265 return schedule(s, table);
2266 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2267 return schedule_forest(s, table);
2268 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2269 return type_of(s, table);
2271 name = read_ident(s);
2272 if (name)
2273 obj = stored_obj(s->ctx, table, name);
2274 else
2275 obj = isl_stream_read_obj(s);
2276 if (!obj.v)
2277 goto error;
2280 if (isl_stream_eat_if_available(s, '^'))
2281 obj = power(s, obj);
2282 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2283 obj = obj_at_index(s, obj);
2284 else if (is_subtype(obj, isl_obj_union_map) &&
2285 isl_stream_eat_if_available(s, '(')) {
2286 obj = convert(s->ctx, obj, isl_obj_union_map);
2287 obj = apply(s, obj.v, table);
2288 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2289 isl_stream_eat_if_available(s, '(')) {
2290 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
2291 obj = apply_fun(s, obj, table);
2292 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2293 isl_stream_eat_if_available(s, '(')) {
2294 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2295 obj = apply_fun(s, obj, table);
2298 return obj;
2299 error:
2300 free_obj(obj);
2301 obj.type = isl_obj_none;
2302 obj.v = NULL;
2303 return obj;
2306 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2307 struct isl_obj lhs, struct isl_obj rhs)
2309 int i;
2311 for (i = 0; ; ++i) {
2312 if (!bin_ops[i].op)
2313 break;
2314 if (bin_ops[i].op != like->op)
2315 continue;
2316 if (!is_subtype(lhs, bin_ops[i].lhs))
2317 continue;
2318 if (!is_subtype(rhs, bin_ops[i].rhs))
2319 continue;
2321 return &bin_ops[i];
2324 for (i = 0; ; ++i) {
2325 if (!named_bin_ops[i].name)
2326 break;
2327 if (named_bin_ops[i].op.op != like->op)
2328 continue;
2329 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2330 continue;
2331 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2332 continue;
2334 return &named_bin_ops[i].op;
2337 return NULL;
2340 static int next_is_neg_int(struct isl_stream *s)
2342 struct isl_token *tok;
2343 int ret;
2345 tok = isl_stream_next_token(s);
2346 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2347 isl_val *v;
2348 v = isl_token_get_val(s->ctx, tok);
2349 ret = isl_val_is_neg(v);
2350 isl_val_free(v);
2351 } else
2352 ret = 0;
2353 isl_stream_push_token(s, tok);
2355 return ret;
2358 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2359 struct isl_obj lhs, struct isl_obj rhs)
2361 struct isl_obj obj;
2363 lhs = convert(ctx, lhs, op->lhs);
2364 rhs = convert(ctx, rhs, op->rhs);
2365 if (op->res != isl_obj_bool)
2366 obj.v = op->o.fn(lhs.v, rhs.v);
2367 else {
2368 int res = op->o.test(lhs.v, rhs.v);
2369 free_obj(lhs);
2370 free_obj(rhs);
2371 obj.v = isl_bool_from_int(res);
2373 obj.type = op->res;
2375 return obj;
2378 static struct isl_obj read_expr(struct isl_stream *s,
2379 struct isl_hash_table *table)
2381 struct isl_obj obj = { isl_obj_none, NULL };
2382 struct isl_obj right_obj = { isl_obj_none, NULL };
2384 obj = read_obj(s, table);
2385 for (; obj.v;) {
2386 struct isc_bin_op *op = NULL;
2388 op = read_bin_op_if_available(s, obj);
2389 if (!op)
2390 break;
2392 right_obj = read_obj(s, table);
2394 op = find_matching_bin_op(op, obj, right_obj);
2396 if (!op)
2397 isl_die(s->ctx, isl_error_invalid,
2398 "no such binary operator defined on given operands",
2399 goto error);
2401 obj = call_bin_op(s->ctx, op, obj, right_obj);
2404 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2405 right_obj = read_obj(s, table);
2406 obj.v = isl_val_add(obj.v, right_obj.v);
2409 return obj;
2410 error:
2411 free_obj(right_obj);
2412 free_obj(obj);
2413 obj.type = isl_obj_none;
2414 obj.v = NULL;
2415 return obj;
2418 static __isl_give isl_printer *source_file(struct isl_stream *s,
2419 struct isl_hash_table *table, __isl_take isl_printer *p);
2421 static __isl_give isl_printer *read_line(struct isl_stream *s,
2422 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2424 struct isl_obj obj = { isl_obj_none, NULL };
2425 char *lhs = NULL;
2426 int assign = 0;
2427 int only_print = 0;
2428 struct isc_bin_op *op = NULL;
2429 char buf[30];
2431 if (!p)
2432 return NULL;
2433 if (isl_stream_is_empty(s))
2434 return p;
2436 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2437 return source_file(s, table, p);
2438 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2439 return codegen(s, table, p);
2441 assign = is_assign(s);
2442 if (assign) {
2443 lhs = isl_stream_read_ident_if_available(s);
2444 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2445 goto error;
2446 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2447 only_print = 1;
2448 else if (!tty)
2449 only_print = 1;
2451 obj = read_expr(s, table);
2452 if (isl_ctx_last_error(s->ctx) == isl_error_abort) {
2453 fprintf(stderr, "Interrupted\n");
2454 isl_ctx_reset_error(s->ctx);
2456 if (isl_stream_eat(s, ';'))
2457 goto error;
2459 if (only_print) {
2460 if (obj.type != isl_obj_none && obj.v != NULL) {
2461 p = obj.type->print(p, obj.v);
2462 p = isl_printer_end_line(p);
2464 free_obj(obj);
2465 return p;
2467 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2468 static int count = 0;
2469 snprintf(buf, sizeof(buf), "$%d", count++);
2470 lhs = strdup(buf + 1);
2472 p = isl_printer_print_str(p, buf);
2473 p = isl_printer_print_str(p, " := ");
2474 p = obj.type->print(p, obj.v);
2475 p = isl_printer_end_line(p);
2477 if (lhs && do_assign(s->ctx, table, lhs, obj))
2478 return p;
2480 return p;
2481 error:
2482 isl_stream_flush_tokens(s);
2483 isl_stream_skip_line(s);
2484 free(lhs);
2485 free_obj(obj);
2486 return p;
2489 int free_cb(void **entry, void *user)
2491 struct isl_named_obj *named = *entry;
2493 free_obj(named->obj);
2494 free(named->name);
2495 free(named);
2497 return 0;
2500 static void register_named_ops(struct isl_stream *s)
2502 int i;
2504 for (i = 0; i < ISCC_N_OP; ++i) {
2505 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2506 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2509 for (i = 0; ; ++i) {
2510 if (!named_un_ops[i].name)
2511 break;
2512 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2513 named_un_ops[i].name);
2514 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2517 for (i = 0; ; ++i) {
2518 if (!named_bin_ops[i].name)
2519 break;
2520 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2521 named_bin_ops[i].name);
2522 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2526 static __isl_give isl_printer *source_file(struct isl_stream *s,
2527 struct isl_hash_table *table, __isl_take isl_printer *p)
2529 struct isl_token *tok;
2530 struct isl_stream *s_file;
2531 char *name;
2532 FILE *file;
2534 tok = isl_stream_next_token(s);
2535 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2536 isl_stream_error(s, tok, "expecting filename");
2537 isl_token_free(tok);
2538 return p;
2541 name = isl_token_get_str(s->ctx, tok);
2542 isl_token_free(tok);
2543 file = fopen(name, "r");
2544 free(name);
2545 isl_assert(s->ctx, file, return p);
2547 s_file = isl_stream_new_file(s->ctx, file);
2548 if (!s_file) {
2549 fclose(file);
2550 return p;
2553 register_named_ops(s_file);
2555 while (!s_file->eof)
2556 p = read_line(s_file, table, p, 0);
2558 isl_stream_free(s_file);
2559 fclose(file);
2561 isl_stream_eat(s, ';');
2563 return p;
2566 int main(int argc, char **argv)
2568 struct isl_ctx *ctx;
2569 struct isl_stream *s;
2570 struct isl_hash_table *table;
2571 struct iscc_options *options;
2572 isl_printer *p;
2573 int tty = isatty(0);
2575 options = iscc_options_new_with_defaults();
2576 assert(options);
2578 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2579 pet_options_set_autodetect(ctx, 1);
2580 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2581 s = isl_stream_new_file(ctx, stdin);
2582 assert(s);
2583 table = isl_hash_table_alloc(ctx, 10);
2584 assert(table);
2585 p = isl_printer_to_file(ctx, stdout);
2586 p = isl_printer_set_output_format(p, options->format);
2587 assert(p);
2589 register_named_ops(s);
2591 install_signal_handler(ctx);
2593 while (p && !s->eof) {
2594 isl_ctx_resume(ctx);
2595 p = read_line(s, table, p, tty);
2598 remove_signal_handler(ctx);
2600 isl_printer_free(p);
2601 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2602 isl_hash_table_free(ctx, table);
2603 isl_stream_free(s);
2604 isl_ctx_free(ctx);
2606 return 0;