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