iscc: parse_file: return schedule in schedule tree representation
[barvinok/uuh.git] / iscc.c
blobccadbb3b50f30e6333083efcf2d98067f5c0dbf4
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/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 iscc_bool_false = 0;
77 static int iscc_bool_true = 1;
78 static int iscc_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,
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_MINIMIZING] = "minimizing",
100 [ISCC_RESPECTING] = "respecting",
101 [ISCC_CODEGEN] = "codegen",
102 [ISCC_USING] = "using",
103 [ISCC_TYPEOF] = "typeof"
105 static enum isl_token_type iscc_op[ISCC_N_OP];
107 struct isl_arg_choice iscc_format[] = {
108 {"isl", ISL_FORMAT_ISL},
109 {"omega", ISL_FORMAT_OMEGA},
110 {"polylib", ISL_FORMAT_POLYLIB},
111 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
112 {"latex", ISL_FORMAT_LATEX},
113 {"C", ISL_FORMAT_C},
117 struct iscc_options {
118 struct barvinok_options *barvinok;
119 struct pet_options *pet;
120 unsigned format;
121 int io;
124 ISL_ARGS_START(struct iscc_options, iscc_options_args)
125 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
126 "barvinok options")
127 #ifdef HAVE_PET
128 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
129 #endif
130 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
131 iscc_format, ISL_FORMAT_ISL, "output format")
132 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
133 "allow read and write operations")
134 ISL_ARGS_END
136 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
137 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
139 static void *isl_obj_bool_copy(void *v)
141 return v;
144 static void isl_obj_bool_free(void *v)
148 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
149 void *v)
151 if (v == &iscc_bool_true)
152 return isl_printer_print_str(p, "True");
153 else if (v == &iscc_bool_false)
154 return isl_printer_print_str(p, "False");
155 else
156 return isl_printer_print_str(p, "Error");
159 static void *isl_obj_bool_add(void *v1, void *v2)
161 return v1;
164 struct isl_obj_vtable isl_obj_bool_vtable = {
165 isl_obj_bool_copy,
166 isl_obj_bool_add,
167 isl_obj_bool_print,
168 isl_obj_bool_free
170 #define isl_obj_bool (&isl_obj_bool_vtable)
172 int *iscc_bool_from_int(int res)
174 return res < 0 ? &iscc_bool_error :
175 res ? &iscc_bool_true : &iscc_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 isl_stat 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 isl_stat_ok;
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 isl_stat 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 isl_stat_ok;
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 ? &iscc_bool_true : &iscc_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 ? &iscc_bool_true : &iscc_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 /* Are the two strings "str1" and "str2" equal to each other?
385 static int str_eq(__isl_keep isl_str *str1, __isl_keep isl_str *str2)
387 if (!str1 || !str2)
388 return -1;
390 return !strcmp(str1->s, str2->s);
393 struct isc_bin_op bin_ops[] = {
394 { '+', isl_obj_val, isl_obj_val, isl_obj_val,
395 (isc_bin_op_fn) &isl_val_add },
396 { '-', isl_obj_val, isl_obj_val, isl_obj_val,
397 (isc_bin_op_fn) &isl_val_sub },
398 { '*', isl_obj_val, isl_obj_val, isl_obj_val,
399 (isc_bin_op_fn) &isl_val_mul },
400 { '+', isl_obj_pw_multi_aff, isl_obj_pw_multi_aff,
401 isl_obj_pw_multi_aff,
402 (isc_bin_op_fn) &isl_pw_multi_aff_add },
403 { '+', isl_obj_union_set, isl_obj_union_set,
404 isl_obj_union_set,
405 (isc_bin_op_fn) &isl_union_set_union },
406 { '+', isl_obj_union_map, isl_obj_union_map,
407 isl_obj_union_map,
408 (isc_bin_op_fn) &isl_union_map_union },
409 { '-', isl_obj_union_set, isl_obj_union_set,
410 isl_obj_union_set,
411 (isc_bin_op_fn) &isl_union_set_subtract },
412 { '-', isl_obj_union_map, isl_obj_union_map,
413 isl_obj_union_map,
414 (isc_bin_op_fn) &isl_union_map_subtract },
415 { '*', isl_obj_union_set, isl_obj_union_set,
416 isl_obj_union_set,
417 (isc_bin_op_fn) &isl_union_set_intersect },
418 { '*', isl_obj_union_map, isl_obj_union_map,
419 isl_obj_union_map,
420 (isc_bin_op_fn) &isl_union_map_intersect },
421 { '*', isl_obj_union_map, isl_obj_union_set,
422 isl_obj_union_map,
423 (isc_bin_op_fn) &isl_union_map_intersect_domain },
424 { '.', isl_obj_union_map, isl_obj_union_map,
425 isl_obj_union_map,
426 (isc_bin_op_fn) &isl_union_map_apply_range },
427 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
428 isl_obj_union_pw_qpolynomial,
429 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
430 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
431 isl_obj_list,
432 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
433 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
434 isl_obj_union_map,
435 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
436 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
437 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
438 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
439 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
440 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
441 isl_obj_bool,
442 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
443 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
444 isl_obj_bool,
445 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
446 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
447 isl_obj_bool,
448 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
449 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
450 isl_obj_bool,
451 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
452 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
453 isl_obj_bool,
454 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
455 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
456 isl_obj_bool,
457 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
458 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
459 isl_obj_bool,
460 { .test =
461 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
462 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
463 isl_obj_bool,
464 { .test =
465 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
466 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
467 isl_obj_union_map,
468 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
469 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
470 isl_obj_union_map,
471 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
472 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
473 isl_obj_union_map,
474 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
475 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
476 isl_obj_union_map,
477 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
478 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
479 isl_obj_union_map,
480 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
481 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
482 isl_obj_union_map,
483 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
484 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
485 isl_obj_union_map,
486 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
487 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
488 isl_obj_union_map,
489 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
490 { '.', isl_obj_union_pw_qpolynomial_fold,
491 isl_obj_union_pw_qpolynomial_fold,
492 isl_obj_union_pw_qpolynomial_fold,
493 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
494 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
495 isl_obj_union_pw_qpolynomial,
496 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
497 { '+', isl_obj_union_pw_qpolynomial,
498 isl_obj_union_pw_qpolynomial_fold,
499 isl_obj_union_pw_qpolynomial_fold,
500 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
501 { '+', isl_obj_union_pw_qpolynomial_fold,
502 isl_obj_union_pw_qpolynomial,
503 isl_obj_union_pw_qpolynomial_fold,
504 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
505 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
506 isl_obj_union_pw_qpolynomial,
507 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
508 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
509 isl_obj_union_pw_qpolynomial,
510 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
511 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
512 isl_obj_union_pw_qpolynomial,
513 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
514 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
515 isl_obj_union_pw_qpolynomial_fold,
516 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
517 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
518 isl_obj_union_pw_qpolynomial_fold,
519 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
520 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
521 isl_obj_union_pw_qpolynomial,
522 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
523 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
524 isl_obj_union_pw_qpolynomial,
525 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
526 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
527 isl_obj_union_pw_qpolynomial_fold,
528 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
529 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
530 isl_obj_union_pw_qpolynomial,
531 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
532 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
533 isl_obj_union_pw_qpolynomial,
534 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
535 { '%', isl_obj_union_set, isl_obj_union_set,
536 isl_obj_union_set,
537 (isc_bin_op_fn) &isl_union_set_gist },
538 { '%', isl_obj_union_map, isl_obj_union_map,
539 isl_obj_union_map,
540 (isc_bin_op_fn) &isl_union_map_gist },
541 { '%', isl_obj_union_map, isl_obj_union_set,
542 isl_obj_union_map,
543 (isc_bin_op_fn) &isl_union_map_gist_domain },
544 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
545 isl_obj_union_pw_qpolynomial,
546 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
547 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
548 isl_obj_union_pw_qpolynomial_fold,
549 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
550 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
551 isl_obj_union_pw_qpolynomial, isl_obj_bool,
552 { .test = (isc_bin_test_fn)
553 &isl_union_pw_qpolynomial_plain_is_equal } },
554 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
555 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
556 { .test = (isc_bin_test_fn)
557 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
558 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
559 (isc_bin_op_fn) &isl_str_concat },
560 { '=', isl_obj_str, isl_obj_str, isl_obj_bool,
561 { .test = (isc_bin_test_fn) &str_eq } },
565 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
566 __isl_take isl_union_map *umap2)
568 return isl_union_map_apply_range(umap2, umap1);
571 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
572 __isl_take isl_union_pw_qpolynomial *upwqp,
573 __isl_take isl_union_map *umap)
575 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
578 static __isl_give struct isl_list *qpolynomial_fold_after_map(
579 __isl_take isl_union_pw_qpolynomial_fold *upwf,
580 __isl_take isl_union_map *umap)
582 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
585 struct isc_named_bin_op named_bin_ops[] = {
586 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
587 isl_obj_union_map,
588 (isc_bin_op_fn) &map_after_map } },
589 { "after", { -1, isl_obj_union_pw_qpolynomial,
590 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
591 (isc_bin_op_fn) &qpolynomial_after_map } },
592 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
593 isl_obj_union_map, isl_obj_list,
594 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
595 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
596 isl_obj_union_map,
597 (isc_bin_op_fn) &isl_union_map_apply_range } },
598 { "before", { -1, isl_obj_union_map,
599 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
600 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
601 { "before", { -1, isl_obj_union_map,
602 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
603 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
604 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
605 isl_obj_union_set,
606 (isc_bin_op_fn) &isl_union_set_product } },
607 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
608 isl_obj_union_map,
609 (isc_bin_op_fn) &isl_union_map_product } },
610 NULL
613 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
615 return isl_set_from_basic_set(isl_union_set_sample(uset));
618 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
620 return isl_map_from_basic_map(isl_union_map_sample(umap));
623 static __isl_give struct isl_list *union_map_power(
624 __isl_take isl_union_map *umap)
626 isl_ctx *ctx;
627 struct isl_list *list;
628 int exact;
630 ctx = isl_union_map_get_ctx(umap);
631 list = isl_list_alloc(ctx, 2);
632 if (!list)
633 goto error2;
635 list->obj[0].type = isl_obj_union_map;
636 list->obj[0].v = isl_union_map_power(umap, &exact);
637 list->obj[1].type = isl_obj_bool;
638 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
639 if (exact < 0 || !list->obj[0].v)
640 goto error;
642 return list;
643 error2:
644 isl_union_map_free(umap);
645 error:
646 isl_list_free(list);
647 return NULL;
650 /* Compute a lower or upper bound on "upwqp" depending on "type" and
651 * return a list containing two elements, the bound and a boolean
652 * indicating whether the result is tight.
654 static __isl_give struct isl_list *union_pw_qpolynomial_bound(
655 __isl_take isl_union_pw_qpolynomial *upwqp, enum isl_fold type)
657 isl_ctx *ctx;
658 struct isl_list *list;
659 int tight;
661 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
662 list = isl_list_alloc(ctx, 2);
663 if (!list)
664 goto error2;
666 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
667 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp, type, &tight);
668 list->obj[1].type = isl_obj_bool;
669 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
670 if (tight < 0 || !list->obj[0].v)
671 goto error;
673 return list;
674 error2:
675 isl_union_pw_qpolynomial_free(upwqp);
676 error:
677 isl_list_free(list);
678 return NULL;
681 /* Compute a lower bound on "upwqp" and return a list containing
682 * two elements, the bound and a booleanindicating whether
683 * the result is tight.
685 static __isl_give struct isl_list *union_pw_qpolynomial_lower_bound(
686 __isl_take isl_union_pw_qpolynomial *upwqp)
688 return union_pw_qpolynomial_bound(upwqp, isl_fold_min);
691 /* Compute a upper bound on "upwqp" and return a list containing
692 * two elements, the bound and a booleanindicating whether
693 * the result is tight.
695 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
696 __isl_take isl_union_pw_qpolynomial *upwqp)
698 return union_pw_qpolynomial_bound(upwqp, isl_fold_max);
701 #ifdef HAVE_PET
702 static __isl_give isl_list *parse(__isl_take isl_str *str)
704 isl_ctx *ctx;
705 struct isl_list *list;
706 struct pet_scop *scop;
707 isl_schedule *sched;
708 isl_union_map *may_reads, *must_writes, *may_writes;
709 isl_union_set *domain;
710 struct iscc_options *options;
712 if (!str)
713 return NULL;
714 ctx = str->ctx;
716 options = isl_ctx_peek_iscc_options(ctx);
717 if (!options || !options->io) {
718 isl_str_free(str);
719 isl_die(ctx, isl_error_invalid,
720 "parse_file operation not allowed", return NULL);
723 list = isl_list_alloc(ctx, 5);
724 if (!list)
725 goto error;
727 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
728 domain = pet_scop_collect_domains(scop);
729 sched = scop ? isl_schedule_copy(scop->schedule) : NULL;
730 may_reads = pet_scop_collect_may_reads(scop);
731 may_writes = pet_scop_collect_may_writes(scop);
732 must_writes = pet_scop_collect_must_writes(scop);
733 pet_scop_free(scop);
735 list->obj[0].type = isl_obj_union_set;
736 list->obj[0].v = domain;
737 list->obj[1].type = isl_obj_union_map;
738 list->obj[1].v = must_writes;
739 list->obj[2].type = isl_obj_union_map;
740 list->obj[2].v = may_writes;
741 list->obj[3].type = isl_obj_union_map;
742 list->obj[3].v = may_reads;
743 list->obj[4].type = isl_obj_schedule;
744 list->obj[4].v = sched;
746 if (!list->obj[0].v || !list->obj[1].v ||
747 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
748 goto error;
750 isl_str_free(str);
751 return list;
752 error:
753 isl_list_free(list);
754 isl_str_free(str);
755 return NULL;
757 #endif
759 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
761 isl_union_set **scan = (isl_union_set **) user;
763 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
765 return isl_stat_ok;
768 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
770 isl_union_set *scan;
772 scan = isl_union_set_empty(isl_union_set_get_space(uset));
774 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
775 isl_union_set_free(scan);
776 return uset;
779 isl_union_set_free(uset);
780 return scan;
783 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
785 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
788 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
789 __isl_take isl_union_pw_qpolynomial *upwqp)
791 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
794 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
795 __isl_take isl_union_pw_qpolynomial *upwqp)
797 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
800 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
801 __isl_take isl_union_pw_qpolynomial *upwqp)
803 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
806 /* Return the domain of "schedule".
808 static __isl_give isl_union_set *schedule_domain(
809 __isl_take isl_schedule *schedule)
811 isl_union_set *domain;
813 domain = isl_schedule_get_domain(schedule);
814 isl_schedule_free(schedule);
816 return domain;
819 /* Convert "schedule" to a union map representation.
821 static __isl_give isl_union_map *schedule_map(__isl_take isl_schedule *schedule)
823 isl_union_map *map;
825 map = isl_schedule_get_map(schedule);
826 isl_schedule_free(schedule);
828 return map;
831 typedef void *(*isc_un_op_fn)(void *arg);
832 struct isc_un_op {
833 enum isl_token_type op;
834 isl_obj_type arg;
835 isl_obj_type res;
836 isc_un_op_fn fn;
838 struct isc_named_un_op {
839 char *name;
840 struct isc_un_op op;
842 struct isc_named_un_op named_un_ops[] = {
843 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
844 (isc_un_op_fn) &isl_union_map_affine_hull } },
845 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
846 (isc_un_op_fn) &isl_union_set_affine_hull } },
847 {"card", { -1, isl_obj_union_set,
848 isl_obj_union_pw_qpolynomial,
849 (isc_un_op_fn) &isl_union_set_card } },
850 {"card", { -1, isl_obj_union_map,
851 isl_obj_union_pw_qpolynomial,
852 (isc_un_op_fn) &isl_union_map_card } },
853 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
854 (isc_un_op_fn) &isl_union_set_coalesce } },
855 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
856 (isc_un_op_fn) &isl_union_map_coalesce } },
857 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
858 isl_obj_union_pw_qpolynomial,
859 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
860 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
861 isl_obj_union_pw_qpolynomial_fold,
862 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
863 {"coefficients", { -1, isl_obj_union_set,
864 isl_obj_union_set,
865 (isc_un_op_fn) &isl_union_set_coefficients } },
866 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
867 (isc_un_op_fn) &isl_union_set_solutions } },
868 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
869 (isc_un_op_fn) &isl_union_map_deltas } },
870 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
871 (isc_un_op_fn) &isl_union_map_deltas_map } },
872 {"dom", { -1, isl_obj_schedule, isl_obj_union_set,
873 (isc_un_op_fn) &schedule_domain } },
874 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
875 (isc_un_op_fn) &isl_union_map_domain } },
876 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
877 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
878 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
879 isl_obj_union_set,
880 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
881 {"domain", { -1, isl_obj_schedule, isl_obj_union_set,
882 (isc_un_op_fn) &schedule_domain } },
883 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
884 (isc_un_op_fn) &isl_union_map_domain } },
885 {"domain", { -1, isl_obj_union_pw_qpolynomial,
886 isl_obj_union_set,
887 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
888 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
889 isl_obj_union_set,
890 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
891 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
892 (isc_un_op_fn) &isl_union_map_domain_map } },
893 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
894 (isc_un_op_fn) &isl_union_map_range } },
895 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
896 (isc_un_op_fn) &isl_union_map_range } },
897 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
898 (isc_un_op_fn) &isl_union_map_range_map } },
899 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
900 (isc_un_op_fn) &isl_union_set_identity } },
901 {"lattice_width", { -1, isl_obj_union_set,
902 isl_obj_union_pw_qpolynomial,
903 (isc_un_op_fn) &isl_union_set_lattice_width } },
904 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
905 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
906 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
907 (isc_un_op_fn) &isl_union_map_lexmin } },
908 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
909 (isc_un_op_fn) &isl_union_map_lexmax } },
910 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
911 (isc_un_op_fn) &isl_union_set_lexmin } },
912 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
913 (isc_un_op_fn) &isl_union_set_lexmax } },
914 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
915 (isc_un_op_fn) &isl_union_set_lift } },
916 {"map", { -1, isl_obj_schedule, isl_obj_union_map,
917 (isc_un_op_fn) &schedule_map } },
918 {"params", { -1, isl_obj_union_map, isl_obj_set,
919 (isc_un_op_fn) &isl_union_map_params } },
920 {"params", { -1, isl_obj_union_set, isl_obj_set,
921 (isc_un_op_fn) &isl_union_set_params } },
922 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
923 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
924 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
925 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
926 {"poly", { -1, isl_obj_union_pw_qpolynomial,
927 isl_obj_union_pw_qpolynomial,
928 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
929 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
930 isl_obj_union_pw_qpolynomial,
931 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
932 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
933 isl_obj_union_pw_qpolynomial,
934 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
935 #ifdef HAVE_PET
936 {"parse_file", { -1, isl_obj_str, isl_obj_list,
937 (isc_un_op_fn) &parse } },
938 #endif
939 {"pow", { -1, isl_obj_union_map, isl_obj_list,
940 (isc_un_op_fn) &union_map_power } },
941 {"sample", { -1, isl_obj_union_set, isl_obj_set,
942 (isc_un_op_fn) &union_set_sample } },
943 {"sample", { -1, isl_obj_union_map, isl_obj_map,
944 (isc_un_op_fn) &union_map_sample } },
945 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
946 (isc_un_op_fn) &union_set_scan } },
947 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
948 (isc_un_op_fn) &union_map_scan } },
949 {"sum", { -1, isl_obj_union_pw_qpolynomial,
950 isl_obj_union_pw_qpolynomial,
951 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
952 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
953 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
954 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
955 (isc_un_op_fn) &isl_union_set_unwrap } },
956 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
957 (isc_un_op_fn) &isl_union_map_wrap } },
958 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
959 (isc_un_op_fn) &isl_union_map_zip } },
960 NULL
963 struct isl_named_obj {
964 char *name;
965 struct isl_obj obj;
968 static void free_obj(struct isl_obj obj)
970 obj.type->free(obj.v);
973 static int same_name(const void *entry, const void *val)
975 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
977 return !strcmp(named->name, val);
980 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
981 char *name, struct isl_obj obj)
983 struct isl_hash_table_entry *entry;
984 uint32_t name_hash;
985 struct isl_named_obj *named;
987 name_hash = isl_hash_string(isl_hash_init(), name);
988 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
989 if (!entry)
990 goto error;
991 if (entry->data) {
992 named = entry->data;
993 free_obj(named->obj);
994 free(name);
995 } else {
996 named = isl_alloc_type(ctx, struct isl_named_obj);
997 if (!named)
998 goto error;
999 named->name = name;
1000 entry->data = named;
1002 named->obj = obj;
1004 return 0;
1005 error:
1006 free_obj(obj);
1007 free(name);
1008 return -1;
1011 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1012 struct isl_hash_table *table, char *name)
1014 struct isl_obj obj = { isl_obj_none, NULL };
1015 struct isl_hash_table_entry *entry;
1016 uint32_t name_hash;
1018 name_hash = isl_hash_string(isl_hash_init(), name);
1019 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1020 if (entry) {
1021 struct isl_named_obj *named;
1022 named = entry->data;
1023 obj = named->obj;
1024 } else if (isdigit(name[0]))
1025 fprintf(stderr, "unknown identifier '$%s'\n", name);
1026 else
1027 fprintf(stderr, "unknown identifier '%s'\n", name);
1029 free(name);
1030 obj.v = obj.type->copy(obj.v);
1031 return obj;
1034 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1036 if (obj.type == super)
1037 return 1;
1038 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1039 return 1;
1040 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1041 return 1;
1042 if (obj.type == isl_obj_schedule && super == isl_obj_union_map)
1043 return 1;
1044 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1045 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1046 int is_set = isl_space_is_set(space);
1047 isl_space_free(space);
1048 return is_set;
1050 if (obj.type == isl_obj_pw_qpolynomial &&
1051 super == isl_obj_union_pw_qpolynomial)
1052 return 1;
1053 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1054 super == isl_obj_union_pw_qpolynomial_fold)
1055 return 1;
1056 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1057 return 1;
1058 if (obj.type == isl_obj_list) {
1059 struct isl_list *list = obj.v;
1060 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1061 return is_subtype(list->obj[0], super);
1063 if (super == isl_obj_str)
1064 return 1;
1065 return 0;
1068 static struct isl_obj obj_at(struct isl_obj obj, int i)
1070 struct isl_list *list = obj.v;
1072 obj = list->obj[i];
1073 obj.v = obj.type->copy(obj.v);
1075 isl_list_free(list);
1077 return obj;
1080 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1081 isl_obj_type type)
1083 if (obj.type == type)
1084 return obj;
1085 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1086 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1087 obj.type = isl_obj_union_set;
1088 obj.v = isl_union_set_from_set(set);
1089 return obj;
1091 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1092 obj.type = isl_obj_union_map;
1093 obj.v = isl_union_map_from_map(obj.v);
1094 return obj;
1096 if (obj.type == isl_obj_schedule && type == isl_obj_union_map) {
1097 obj.type = isl_obj_union_map;
1098 obj.v = schedule_map(obj.v);
1099 return obj;
1101 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1102 obj.type = isl_obj_union_set;
1103 obj.v = isl_union_set_from_set(obj.v);
1104 return obj;
1106 if (obj.type == isl_obj_pw_qpolynomial &&
1107 type == isl_obj_union_pw_qpolynomial) {
1108 obj.type = isl_obj_union_pw_qpolynomial;
1109 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1110 return obj;
1112 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1113 type == isl_obj_union_pw_qpolynomial_fold) {
1114 obj.type = isl_obj_union_pw_qpolynomial_fold;
1115 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1116 return obj;
1118 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1119 if (type == isl_obj_union_map) {
1120 obj.type = isl_obj_union_map;
1121 return obj;
1123 if (type == isl_obj_union_pw_qpolynomial) {
1124 isl_space *dim = isl_union_set_get_space(obj.v);
1125 isl_union_set_free(obj.v);
1126 obj.v = isl_union_pw_qpolynomial_zero(dim);
1127 obj.type = isl_obj_union_pw_qpolynomial;
1128 return obj;
1130 if (type == isl_obj_union_pw_qpolynomial_fold) {
1131 isl_space *dim = isl_union_set_get_space(obj.v);
1132 isl_union_set_free(obj.v);
1133 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1134 isl_fold_list);
1135 obj.type = isl_obj_union_pw_qpolynomial_fold;
1136 return obj;
1139 if (obj.type == isl_obj_list) {
1140 struct isl_list *list = obj.v;
1141 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1142 return convert(ctx, obj_at(obj, 0), type);
1144 if (type == isl_obj_str) {
1145 isl_str *str;
1146 isl_printer *p;
1147 char *s;
1149 p = isl_printer_to_str(ctx);
1150 if (!p)
1151 goto error;
1152 p = obj.type->print(p, obj.v);
1153 s = isl_printer_get_str(p);
1154 isl_printer_free(p);
1156 str = isl_str_from_string(ctx, s);
1157 if (!str)
1158 goto error;
1159 free_obj(obj);
1160 obj.v = str;
1161 obj.type = isl_obj_str;
1162 return obj;
1165 error:
1166 free_obj(obj);
1167 obj.type = isl_obj_none;
1168 obj.v = NULL;
1169 return obj;
1172 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1173 struct isl_obj lhs)
1175 int i;
1176 struct isl_token *tok;
1178 tok = isl_stream_next_token(s);
1179 if (!tok)
1180 return NULL;
1182 for (i = 0; ; ++i) {
1183 if (!bin_ops[i].op)
1184 break;
1185 if (bin_ops[i].op != isl_token_get_type(tok))
1186 continue;
1187 if (!is_subtype(lhs, bin_ops[i].lhs))
1188 continue;
1190 isl_token_free(tok);
1191 return &bin_ops[i];
1194 for (i = 0; ; ++i) {
1195 if (!named_bin_ops[i].name)
1196 break;
1197 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1198 continue;
1199 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1200 continue;
1202 isl_token_free(tok);
1203 return &named_bin_ops[i].op;
1206 isl_stream_push_token(s, tok);
1208 return NULL;
1211 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1213 int i;
1214 struct isl_token *tok;
1216 tok = isl_stream_next_token(s);
1217 if (!tok)
1218 return NULL;
1220 for (i = 0; ; ++i) {
1221 if (!named_un_ops[i].name)
1222 break;
1223 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1224 continue;
1226 isl_token_free(tok);
1227 return &named_un_ops[i].op;
1230 isl_stream_push_token(s, tok);
1232 return NULL;
1235 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1236 struct isl_obj arg)
1238 int i;
1240 for (i = 0; ; ++i) {
1241 if (!named_un_ops[i].name)
1242 break;
1243 if (named_un_ops[i].op.op != like->op)
1244 continue;
1245 if (!is_subtype(arg, named_un_ops[i].op.arg))
1246 continue;
1248 return &named_un_ops[i].op;
1251 return NULL;
1254 static int is_assign(struct isl_stream *s)
1256 struct isl_token *tok;
1257 struct isl_token *tok2;
1258 int assign;
1260 tok = isl_stream_next_token(s);
1261 if (!tok)
1262 return 0;
1263 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1264 isl_stream_push_token(s, tok);
1265 return 0;
1268 tok2 = isl_stream_next_token(s);
1269 if (!tok2) {
1270 isl_stream_push_token(s, tok);
1271 return 0;
1273 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1274 isl_stream_push_token(s, tok2);
1275 isl_stream_push_token(s, tok);
1277 return assign;
1280 static struct isl_obj read_obj(struct isl_stream *s,
1281 struct isl_hash_table *table);
1282 static struct isl_obj read_expr(struct isl_stream *s,
1283 struct isl_hash_table *table);
1285 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1286 struct isl_hash_table *table, struct isc_un_op *op)
1288 isl_ctx *ctx;
1289 struct isl_obj obj = { isl_obj_none, NULL };
1291 obj = read_obj(s, table);
1292 if (!obj.v)
1293 goto error;
1295 op = find_matching_un_op(op, obj);
1297 ctx = isl_stream_get_ctx(s);
1298 if (!op)
1299 isl_die(ctx, isl_error_invalid,
1300 "no such unary operator defined on given operand",
1301 goto error);
1303 obj = convert(ctx, obj, op->arg);
1304 obj.v = op->fn(obj.v);
1305 obj.type = op->res;
1307 return obj;
1308 error:
1309 free_obj(obj);
1310 obj.type = isl_obj_none;
1311 obj.v = NULL;
1312 return obj;
1315 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1317 struct isl_list *list;
1318 int exact;
1320 if (obj.type != isl_obj_union_map)
1321 obj = convert(ctx, obj, isl_obj_union_map);
1322 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1323 list = isl_list_alloc(ctx, 2);
1324 if (!list)
1325 goto error;
1327 list->obj[0].type = isl_obj_union_map;
1328 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1329 list->obj[1].type = isl_obj_bool;
1330 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1331 obj.v = list;
1332 obj.type = isl_obj_list;
1333 if (exact < 0 || !list->obj[0].v)
1334 goto error;
1336 return obj;
1337 error:
1338 free_obj(obj);
1339 obj.type = isl_obj_none;
1340 obj.v = NULL;
1341 return obj;
1344 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1346 struct isl_list *list = obj.v;
1347 struct isl_token *tok;
1348 isl_ctx *ctx;
1349 isl_val *v;
1350 int i;
1352 tok = isl_stream_next_token(s);
1353 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1354 isl_stream_error(s, tok, "expecting index");
1355 if (tok)
1356 isl_stream_push_token(s, tok);
1357 goto error;
1359 ctx = isl_stream_get_ctx(s);
1360 v = isl_token_get_val(ctx, tok);
1361 i = isl_val_get_num_si(v);
1362 isl_val_free(v);
1363 isl_token_free(tok);
1364 isl_assert(ctx, i < list->n, goto error);
1365 if (isl_stream_eat(s, ']'))
1366 goto error;
1368 return obj_at(obj, i);
1369 error:
1370 free_obj(obj);
1371 obj.type = isl_obj_none;
1372 obj.v = NULL;
1373 return obj;
1376 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1377 struct isl_hash_table *table)
1379 isl_ctx *ctx;
1380 struct isl_obj obj;
1382 obj = read_expr(s, table);
1383 ctx = isl_stream_get_ctx(s);
1384 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1385 is_subtype(obj, isl_obj_union_map), goto error);
1387 if (obj.type == isl_obj_list) {
1388 struct isl_list *list = obj.v;
1389 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1390 obj = obj_at(obj, 0);
1392 if (obj.type == isl_obj_set)
1393 obj = convert(ctx, obj, isl_obj_union_set);
1394 else if (obj.type == isl_obj_map)
1395 obj = convert(ctx, obj, isl_obj_union_map);
1396 if (obj.type == isl_obj_union_set) {
1397 obj.v = isl_union_set_apply(obj.v, umap);
1398 } else
1399 obj.v = isl_union_map_apply_range(obj.v, umap);
1400 if (!obj.v)
1401 goto error2;
1403 if (isl_stream_eat(s, ')'))
1404 goto error2;
1406 return obj;
1407 error:
1408 isl_union_map_free(umap);
1409 error2:
1410 free_obj(obj);
1411 obj.type = isl_obj_none;
1412 obj.v = NULL;
1413 return obj;
1416 static struct isl_obj apply_fun_set(struct isl_obj obj,
1417 __isl_take isl_union_set *uset)
1419 if (obj.type == isl_obj_union_pw_qpolynomial) {
1420 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1421 } else {
1422 obj.type = isl_obj_list;
1423 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1425 return obj;
1428 static struct isl_obj apply_fun_map(struct isl_obj obj,
1429 __isl_take isl_union_map *umap)
1431 if (obj.type == isl_obj_union_pw_qpolynomial) {
1432 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1433 } else {
1434 obj.type = isl_obj_list;
1435 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1437 return obj;
1440 static struct isl_obj apply_fun(struct isl_stream *s,
1441 struct isl_obj obj, struct isl_hash_table *table)
1443 struct isl_obj arg;
1444 isl_ctx *ctx;
1446 arg = read_expr(s, table);
1447 ctx = isl_stream_get_ctx(s);
1448 if (!is_subtype(arg, isl_obj_union_map) &&
1449 !is_subtype(arg, isl_obj_union_set))
1450 isl_die(ctx, isl_error_invalid,
1451 "expecting set of map argument", goto error);
1453 if (arg.type == isl_obj_list) {
1454 struct isl_list *list = arg.v;
1455 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1456 arg = obj_at(arg, 0);
1458 if (arg.type == isl_obj_set)
1459 arg = convert(ctx, arg, isl_obj_union_set);
1460 else if (arg.type == isl_obj_map)
1461 arg = convert(ctx, arg, isl_obj_union_map);
1462 if (arg.type == isl_obj_union_set)
1463 obj = apply_fun_set(obj, arg.v);
1464 else
1465 obj = apply_fun_map(obj, arg.v);
1466 if (!obj.v)
1467 goto error2;
1469 if (isl_stream_eat(s, ')'))
1470 goto error2;
1472 return obj;
1473 error:
1474 free_obj(arg);
1475 error2:
1476 free_obj(obj);
1477 obj.type = isl_obj_none;
1478 obj.v = NULL;
1479 return obj;
1482 struct add_vertex_data {
1483 struct isl_list *list;
1484 int i;
1487 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1489 struct add_vertex_data *data = (struct add_vertex_data *)user;
1490 isl_multi_aff *ma;
1491 isl_set *dom;
1493 ma = isl_vertex_get_expr(vertex);
1494 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1496 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1497 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1498 data->i++;
1500 isl_vertex_free(vertex);
1502 return isl_stat_ok;
1505 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1507 isl_ctx *ctx;
1508 isl_basic_set *hull;
1509 isl_vertices *vertices = NULL;
1510 struct isl_list *list = NULL;
1511 isl_stat r;
1512 struct add_vertex_data *data = (struct add_vertex_data *)user;
1514 set = isl_set_remove_divs(set);
1515 hull = isl_set_convex_hull(set);
1516 vertices = isl_basic_set_compute_vertices(hull);
1517 isl_basic_set_free(hull);
1519 list = data->list;
1521 ctx = isl_vertices_get_ctx(vertices);
1522 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1523 if (!data->list)
1524 goto error;
1526 data->i = 0;
1527 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1529 data->list = isl_list_concat(list, data->list);
1531 isl_vertices_free(vertices);
1533 return r;
1534 error:
1535 data->list = list;
1536 isl_vertices_free(vertices);
1537 return isl_stat_error;
1540 static struct isl_obj vertices(struct isl_stream *s,
1541 struct isl_hash_table *table)
1543 isl_ctx *ctx;
1544 struct isl_obj obj;
1545 struct isl_list *list = NULL;
1546 isl_union_set *uset = NULL;
1547 struct add_vertex_data data = { NULL };
1549 obj = read_expr(s, table);
1550 ctx = isl_stream_get_ctx(s);
1551 obj = convert(ctx, obj, isl_obj_union_set);
1552 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1553 uset = obj.v;
1554 obj.v = NULL;
1556 list = isl_list_alloc(ctx, 0);
1557 if (!list)
1558 goto error;
1560 data.list = list;
1562 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1563 goto error;
1565 isl_union_set_free(uset);
1567 obj.type = isl_obj_list;
1568 obj.v = data.list;
1570 return obj;
1571 error:
1572 isl_union_set_free(uset);
1573 isl_list_free(data.list);
1574 free_obj(obj);
1575 obj.type = isl_obj_none;
1576 obj.v = NULL;
1577 return obj;
1580 static struct isl_obj type_of(struct isl_stream *s,
1581 struct isl_hash_table *table)
1583 isl_ctx *ctx;
1584 struct isl_obj obj;
1585 const char *type = "unknown";
1587 obj = read_expr(s, table);
1589 if (obj.type == isl_obj_map ||
1590 obj.type == isl_obj_union_map)
1591 type = "map";
1592 if (obj.type == isl_obj_set ||
1593 obj.type == isl_obj_union_set)
1594 type = "set";
1595 if (obj.type == isl_obj_pw_multi_aff)
1596 type = "piecewise multi-quasiaffine expression";
1597 if (obj.type == isl_obj_pw_qpolynomial ||
1598 obj.type == isl_obj_union_pw_qpolynomial)
1599 type = "piecewise quasipolynomial";
1600 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1601 obj.type == isl_obj_union_pw_qpolynomial_fold)
1602 type = "piecewise quasipolynomial fold";
1603 if (obj.type == isl_obj_list)
1604 type = "list";
1605 if (obj.type == isl_obj_bool)
1606 type = "boolean";
1607 if (obj.type == isl_obj_str)
1608 type = "string";
1609 if (obj.type == isl_obj_val)
1610 type = "value";
1611 if (obj.type == isl_obj_schedule)
1612 type = "schedule";
1614 free_obj(obj);
1615 obj.type = isl_obj_str;
1616 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1618 return obj;
1621 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1622 struct isl_hash_table *table)
1624 struct isl_obj obj;
1625 isl_ctx *ctx;
1627 obj = read_obj(s, table);
1628 ctx = isl_stream_get_ctx(s);
1629 obj = convert(ctx, obj, isl_obj_union_set);
1630 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1631 return obj.v;
1632 error:
1633 free_obj(obj);
1634 return NULL;
1637 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1638 struct isl_hash_table *table)
1640 struct isl_obj obj;
1641 isl_ctx *ctx;
1643 obj = read_obj(s, table);
1644 ctx = isl_stream_get_ctx(s);
1645 obj = convert(ctx, obj, isl_obj_union_map);
1646 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1647 return obj.v;
1648 error:
1649 free_obj(obj);
1650 return NULL;
1653 /* Read a schedule in the form of either a schedule (tree) or a union map
1654 * from "s" and store the schedule in "access".
1656 static __isl_give isl_union_access_info *access_info_set_schedule(
1657 __isl_take isl_union_access_info *access, struct isl_stream *s,
1658 struct isl_hash_table *table)
1660 struct isl_obj obj;
1661 isl_ctx *ctx;
1663 obj = read_obj(s, table);
1664 if (obj.type == isl_obj_schedule)
1665 return isl_union_access_info_set_schedule(access, obj.v);
1666 ctx = isl_stream_get_ctx(s);
1667 obj = convert(ctx, obj, isl_obj_union_map);
1669 return isl_union_access_info_set_schedule_map(access, obj.v);
1672 static struct isl_obj last_any(struct isl_stream *s,
1673 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1674 __isl_take isl_union_map *may_source)
1676 struct isl_obj obj = { isl_obj_none, NULL };
1677 isl_union_access_info *access;
1678 isl_union_flow *flow;
1679 isl_union_map *sink = NULL;
1680 isl_union_map *may_dep;
1682 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1683 goto error;
1685 sink = read_map(s, table);
1686 if (!sink)
1687 goto error;
1689 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1690 goto error;
1692 access = isl_union_access_info_from_sink(sink);
1693 access = isl_union_access_info_set_must_source(access, must_source);
1694 access = isl_union_access_info_set_may_source(access, may_source);
1695 access = access_info_set_schedule(access, s, table);
1696 flow = isl_union_access_info_compute_flow(access);
1697 may_dep = isl_union_flow_get_may_dependence(flow);
1698 isl_union_flow_free(flow);
1700 if (!may_dep)
1701 return obj;
1703 obj.type = isl_obj_union_map;
1704 obj.v = may_dep;
1706 return obj;
1707 error:
1708 isl_union_map_free(may_source);
1709 isl_union_map_free(must_source);
1710 isl_union_map_free(sink);
1711 free_obj(obj);
1712 obj.type = isl_obj_none;
1713 obj.v = NULL;
1714 return obj;
1717 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1719 struct isl_obj obj = { isl_obj_none, NULL };
1720 isl_union_access_info *access;
1721 isl_union_flow *flow;
1722 isl_union_map *may_source = NULL;
1723 isl_union_map *sink = NULL;
1724 isl_union_map *may_dep;
1726 may_source = read_map(s, table);
1727 if (!may_source)
1728 goto error;
1730 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1731 isl_union_map *must_source;
1732 must_source = read_map(s, table);
1733 if (!must_source)
1734 goto error;
1735 return last_any(s, table, must_source, may_source);
1738 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1739 goto error;
1741 sink = read_map(s, table);
1742 if (!sink)
1743 goto error;
1745 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1746 goto error;
1748 access = isl_union_access_info_from_sink(sink);
1749 access = isl_union_access_info_set_may_source(access, may_source);
1750 access = access_info_set_schedule(access, s, table);
1751 flow = isl_union_access_info_compute_flow(access);
1752 may_dep = isl_union_flow_get_may_dependence(flow);
1753 isl_union_flow_free(flow);
1755 if (!may_dep)
1756 return obj;
1758 obj.type = isl_obj_union_map;
1759 obj.v = may_dep;
1761 return obj;
1762 error:
1763 isl_union_map_free(may_source);
1764 isl_union_map_free(sink);
1765 free_obj(obj);
1766 obj.type = isl_obj_none;
1767 obj.v = NULL;
1768 return obj;
1771 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1773 struct isl_obj obj = { isl_obj_none, NULL };
1774 struct isl_list *list = NULL;
1775 isl_union_access_info *access;
1776 isl_union_flow *flow;
1777 isl_union_map *must_source = NULL;
1778 isl_union_map *sink = NULL;
1779 isl_union_map *must_dep;
1780 isl_union_map *must_no_source;
1782 must_source = read_map(s, table);
1783 if (!must_source)
1784 goto error;
1786 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1787 isl_union_map *may_source;
1788 may_source = read_map(s, table);
1789 if (!may_source)
1790 goto error;
1791 return last_any(s, table, must_source, may_source);
1794 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1795 if (!list)
1796 goto error;
1798 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1799 goto error;
1801 sink = read_map(s, table);
1802 if (!sink)
1803 goto error;
1805 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1806 goto error;
1808 access = isl_union_access_info_from_sink(sink);
1809 access = isl_union_access_info_set_must_source(access, must_source);
1810 access = access_info_set_schedule(access, s, table);
1811 flow = isl_union_access_info_compute_flow(access);
1812 must_dep = isl_union_flow_get_must_dependence(flow);
1813 must_no_source = isl_union_flow_get_must_no_source(flow);
1814 isl_union_flow_free(flow);
1816 list->obj[0].type = isl_obj_union_map;
1817 list->obj[0].v = must_dep;
1818 list->obj[1].type = isl_obj_union_map;
1819 list->obj[1].v = must_no_source;
1821 if (!must_dep || !must_no_source) {
1822 isl_list_free(list);
1823 return obj;
1826 obj.v = list;
1827 obj.type = isl_obj_list;
1829 return obj;
1830 error:
1831 isl_list_free(list);
1832 isl_union_map_free(must_source);
1833 isl_union_map_free(sink);
1834 free_obj(obj);
1835 obj.type = isl_obj_none;
1836 obj.v = NULL;
1837 return obj;
1840 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1841 struct isl_hash_table *table)
1843 isl_union_set *domain;
1844 isl_union_map *validity;
1845 isl_union_map *proximity;
1847 domain = read_set(s, table);
1848 if (!domain)
1849 return NULL;
1851 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1852 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1854 for (;;) {
1855 isl_union_map *umap;
1856 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1857 umap = read_map(s, table);
1858 validity = isl_union_map_union(validity, umap);
1859 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1860 umap = read_map(s, table);
1861 proximity = isl_union_map_union(proximity, umap);
1862 } else
1863 break;
1866 return isl_union_set_compute_schedule(domain, validity, proximity);
1869 static struct isl_obj schedule(struct isl_stream *s,
1870 struct isl_hash_table *table)
1872 struct isl_obj obj = { isl_obj_none, NULL };
1873 isl_schedule *schedule;
1875 schedule = get_schedule(s, table);
1877 obj.v = schedule;
1878 obj.type = isl_obj_schedule;
1880 return obj;
1883 /* Read a schedule for code generation in the form of either
1884 * a schedule tree or a union map.
1885 * If the input is a set rather than a map, then we construct
1886 * an identity union map schedule on the given set.
1888 static struct isl_obj get_codegen_schedule(struct isl_stream *s,
1889 struct isl_hash_table *table)
1891 struct isl_obj obj;
1892 isl_ctx *ctx;
1894 obj = read_obj(s, table);
1895 ctx = isl_stream_get_ctx(s);
1897 if (obj.type == isl_obj_schedule)
1898 return obj;
1899 if (is_subtype(obj, isl_obj_union_set)) {
1900 obj = convert(ctx, obj, isl_obj_union_set);
1901 obj.v = isl_union_set_identity(obj.v);
1902 obj.type = isl_obj_union_map;
1904 if (is_subtype(obj, isl_obj_union_map))
1905 return convert(ctx, obj, isl_obj_union_map);
1907 free_obj(obj);
1908 obj.v = NULL;
1909 obj.type = isl_obj_none;
1910 isl_die(ctx, isl_error_invalid, "expecting schedule, set or map",
1911 return obj);
1914 /* Generate an AST for the given schedule and options and return the AST.
1916 static __isl_give isl_ast_node *get_ast_from_union_map(
1917 __isl_take isl_union_map *schedule, __isl_take isl_union_map *options)
1919 isl_space *space;
1920 isl_set *context;
1921 isl_ast_build *build;
1922 isl_ast_node *tree;
1924 space = isl_union_map_get_space(schedule);
1925 context = isl_set_universe(isl_space_params(space));
1927 build = isl_ast_build_from_context(context);
1928 build = isl_ast_build_set_options(build, options);
1929 tree = isl_ast_build_ast_from_schedule(build, schedule);
1930 isl_ast_build_free(build);
1932 return tree;
1935 /* Generate an AST for the given schedule and return the AST.
1937 static __isl_give isl_ast_node *get_ast_from_schedule(
1938 __isl_take isl_schedule *schedule)
1940 isl_ast_build *build;
1941 isl_ast_node *tree;
1943 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
1944 tree = isl_ast_build_node_from_schedule(build, schedule);
1945 isl_ast_build_free(build);
1947 return tree;
1950 /* Print the AST "tree" on the printer "p".
1952 static __isl_give isl_printer *print_ast(__isl_take isl_printer *p,
1953 __isl_take isl_ast_node *tree)
1955 int format;
1957 format = isl_printer_get_output_format(p);
1958 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1959 p = isl_printer_print_ast_node(p, tree);
1960 p = isl_printer_set_output_format(p, format);
1962 isl_ast_node_free(tree);
1964 return p;
1967 /* Perform the codegen operation.
1968 * In particular, read a schedule, check if the user has specified any options
1969 * and then generate an AST from the schedule (and options) and print it.
1970 * In case the schedule is specified as a schedule tree, the AST generation
1971 * options are embedded in the schedule, so they are not read in separately.
1973 static __isl_give isl_printer *codegen(struct isl_stream *s,
1974 struct isl_hash_table *table, __isl_take isl_printer *p)
1976 struct isl_obj obj;
1977 isl_ast_node *tree;
1979 obj = get_codegen_schedule(s, table);
1980 if (!obj.v)
1981 return p;
1983 if (obj.type == isl_obj_schedule) {
1984 isl_schedule *schedule = obj.v;
1986 tree = get_ast_from_schedule(schedule);
1987 } else {
1988 isl_union_map *schedule = obj.v;
1989 isl_union_map *options;
1991 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1992 options = read_map(s, table);
1993 else
1994 options = isl_union_map_empty(
1995 isl_union_map_get_space(schedule));
1997 tree = get_ast_from_union_map(schedule, options);
2000 p = print_ast(p, tree);
2002 isl_stream_eat(s, ';');
2004 return p;
2007 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2009 struct isl_token *tok;
2010 isl_ctx *ctx;
2011 isl_val *v;
2013 ctx = isl_stream_get_ctx(s);
2014 if (isl_stream_eat_if_available(s, '+'))
2015 return transitive_closure(ctx, obj);
2017 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2018 if (obj.type != isl_obj_union_map)
2019 obj = convert(ctx, obj, isl_obj_union_map);
2021 tok = isl_stream_next_token(s);
2022 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2023 isl_stream_error(s, tok, "expecting integer exponent");
2024 if (tok)
2025 isl_stream_push_token(s, tok);
2026 goto error;
2029 v = isl_token_get_val(ctx, tok);
2030 if (isl_val_is_zero(v)) {
2031 isl_stream_error(s, tok, "expecting non-zero exponent");
2032 isl_val_free(v);
2033 if (tok)
2034 isl_stream_push_token(s, tok);
2035 goto error;
2038 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2039 isl_token_free(tok);
2040 if (!obj.v)
2041 goto error;
2043 return obj;
2044 error:
2045 free_obj(obj);
2046 obj.type = isl_obj_none;
2047 obj.v = NULL;
2048 return obj;
2051 static struct isl_obj check_assert(struct isl_stream *s,
2052 struct isl_hash_table *table)
2054 struct isl_obj obj;
2055 isl_ctx *ctx;
2057 obj = read_expr(s, table);
2058 ctx = isl_stream_get_ctx(s);
2059 if (obj.type != isl_obj_bool)
2060 isl_die(ctx, isl_error_invalid,
2061 "expecting boolean expression", goto error);
2062 if (obj.v != &iscc_bool_true)
2063 isl_die(ctx, isl_error_unknown,
2064 "assertion failed", abort());
2065 error:
2066 free_obj(obj);
2067 obj.type = isl_obj_none;
2068 obj.v = NULL;
2069 return obj;
2072 static struct isl_obj read_from_file(struct isl_stream *s)
2074 isl_ctx *ctx;
2075 struct isl_obj obj;
2076 struct isl_token *tok;
2077 struct isl_stream *s_file;
2078 struct iscc_options *options;
2079 char *name;
2080 FILE *file;
2082 tok = isl_stream_next_token(s);
2083 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2084 isl_stream_error(s, tok, "expecting filename");
2085 isl_token_free(tok);
2086 goto error;
2089 ctx = isl_stream_get_ctx(s);
2090 options = isl_ctx_peek_iscc_options(ctx);
2091 if (!options || !options->io) {
2092 isl_token_free(tok);
2093 isl_die(ctx, isl_error_invalid,
2094 "read operation not allowed", goto error);
2097 name = isl_token_get_str(ctx, tok);
2098 isl_token_free(tok);
2099 file = fopen(name, "r");
2100 free(name);
2101 isl_assert(ctx, file, goto error);
2103 s_file = isl_stream_new_file(ctx, file);
2104 if (!s_file) {
2105 fclose(file);
2106 goto error;
2109 obj = isl_stream_read_obj(s_file);
2111 isl_stream_free(s_file);
2112 fclose(file);
2114 return obj;
2115 error:
2116 obj.type = isl_obj_none;
2117 obj.v = NULL;
2118 return obj;
2121 static struct isl_obj write_to_file(struct isl_stream *s,
2122 struct isl_hash_table *table)
2124 struct isl_obj obj;
2125 struct isl_token *tok;
2126 struct isl_stream *s_file;
2127 struct iscc_options *options;
2128 char *name;
2129 FILE *file;
2130 isl_ctx *ctx;
2131 isl_printer *p;
2133 tok = isl_stream_next_token(s);
2134 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2135 isl_stream_error(s, tok, "expecting filename");
2136 isl_token_free(tok);
2137 goto error;
2140 obj = read_expr(s, table);
2142 ctx = isl_stream_get_ctx(s);
2143 options = isl_ctx_peek_iscc_options(ctx);
2144 if (!options || !options->io) {
2145 isl_token_free(tok);
2146 isl_die(ctx, isl_error_invalid,
2147 "write operation not allowed", goto error);
2150 name = isl_token_get_str(ctx, tok);
2151 isl_token_free(tok);
2152 file = fopen(name, "w");
2153 free(name);
2154 if (!file)
2155 isl_die(ctx, isl_error_unknown,
2156 "could not open file for writing", goto error);
2158 p = isl_printer_to_file(ctx, file);
2159 p = isl_printer_set_output_format(p, options->format);
2160 p = obj.type->print(p, obj.v);
2161 p = isl_printer_end_line(p);
2162 isl_printer_free(p);
2164 fclose(file);
2165 error:
2166 free_obj(obj);
2167 obj.type = isl_obj_none;
2168 obj.v = NULL;
2169 return obj;
2172 static struct isl_obj read_string_if_available(struct isl_stream *s)
2174 struct isl_token *tok;
2175 struct isl_obj obj = { isl_obj_none, NULL };
2177 tok = isl_stream_next_token(s);
2178 if (!tok)
2179 return obj;
2180 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2181 isl_str *str;
2182 str = isl_str_alloc(isl_stream_get_ctx(s));
2183 if (!str)
2184 goto error;
2185 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2186 isl_token_free(tok);
2187 obj.v = str;
2188 obj.type = isl_obj_str;
2189 } else
2190 isl_stream_push_token(s, tok);
2191 return obj;
2192 error:
2193 isl_token_free(tok);
2194 return obj;
2197 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2199 struct isl_token *tok;
2200 struct isl_obj obj = { isl_obj_none, NULL };
2201 int type;
2203 tok = isl_stream_next_token(s);
2204 if (!tok)
2205 return obj;
2206 type = isl_token_get_type(tok);
2207 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2208 int is_true = type == ISL_TOKEN_TRUE;
2209 isl_token_free(tok);
2210 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2211 obj.type = isl_obj_bool;
2212 } else
2213 isl_stream_push_token(s, tok);
2214 return obj;
2217 static __isl_give char *read_ident(struct isl_stream *s)
2219 char *name;
2220 isl_val *v;
2221 struct isl_token *tok, *tok2;
2223 name = isl_stream_read_ident_if_available(s);
2224 if (name)
2225 return name;
2227 tok = isl_stream_next_token(s);
2228 if (!tok)
2229 return NULL;
2230 if (isl_token_get_type(tok) != '$') {
2231 isl_stream_push_token(s, tok);
2232 return NULL;
2234 tok2 = isl_stream_next_token(s);
2235 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2236 if (tok2)
2237 isl_stream_push_token(s, tok2);
2238 isl_stream_push_token(s, tok);
2239 return NULL;
2242 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2243 name = isl_val_to_str(v);
2244 isl_val_free(v);
2245 isl_token_free(tok);
2246 isl_token_free(tok2);
2248 return name;
2251 static struct isl_obj read_list(struct isl_stream *s,
2252 struct isl_hash_table *table, struct isl_obj obj)
2254 struct isl_list *list;
2256 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2257 if (!list)
2258 goto error;
2259 list->obj[0] = obj;
2260 list->obj[1] = read_obj(s, table);
2261 obj.v = list;
2262 obj.type = isl_obj_list;
2264 if (!list->obj[1].v)
2265 goto error;
2267 while (isl_stream_eat_if_available(s, ',')) {
2268 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2269 if (!obj.v)
2270 goto error;
2273 return obj;
2274 error:
2275 free_obj(obj);
2276 obj.type = isl_obj_none;
2277 obj.v = NULL;
2278 return obj;
2281 static struct isl_obj read_obj(struct isl_stream *s,
2282 struct isl_hash_table *table)
2284 isl_ctx *ctx;
2285 struct isl_obj obj = { isl_obj_none, NULL };
2286 char *name = NULL;
2287 struct isc_un_op *op = NULL;
2289 obj = read_string_if_available(s);
2290 if (obj.v)
2291 return obj;
2292 obj = read_bool_if_available(s);
2293 if (obj.v)
2294 return obj;
2295 ctx = isl_stream_get_ctx(s);
2296 if (isl_stream_eat_if_available(s, '(')) {
2297 if (isl_stream_next_token_is(s, ')')) {
2298 obj.type = isl_obj_list;
2299 obj.v = isl_list_alloc(ctx, 0);
2300 } else {
2301 obj = read_expr(s, table);
2302 if (obj.v && isl_stream_eat_if_available(s, ','))
2303 obj = read_list(s, table, obj);
2305 if (!obj.v || isl_stream_eat(s, ')'))
2306 goto error;
2307 } else {
2308 op = read_prefix_un_op_if_available(s);
2309 if (op)
2310 return read_un_op_expr(s, table, op);
2312 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2313 return check_assert(s, table);
2314 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2315 return read_from_file(s);
2316 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2317 return write_to_file(s, table);
2318 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2319 return vertices(s, table);
2320 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2321 return any(s, table);
2322 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2323 return last(s, table);
2324 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2325 return schedule(s, table);
2326 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2327 return type_of(s, table);
2329 name = read_ident(s);
2330 if (name)
2331 obj = stored_obj(ctx, table, name);
2332 else
2333 obj = isl_stream_read_obj(s);
2334 if (!obj.v)
2335 goto error;
2338 if (isl_stream_eat_if_available(s, '^'))
2339 obj = power(s, obj);
2340 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2341 obj = obj_at_index(s, obj);
2342 else if (is_subtype(obj, isl_obj_union_map) &&
2343 isl_stream_eat_if_available(s, '(')) {
2344 obj = convert(ctx, obj, isl_obj_union_map);
2345 obj = apply(s, obj.v, table);
2346 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2347 isl_stream_eat_if_available(s, '(')) {
2348 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2349 obj = apply_fun(s, obj, table);
2350 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2351 isl_stream_eat_if_available(s, '(')) {
2352 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2353 obj = apply_fun(s, obj, table);
2356 return obj;
2357 error:
2358 free_obj(obj);
2359 obj.type = isl_obj_none;
2360 obj.v = NULL;
2361 return obj;
2364 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2365 struct isl_obj lhs, struct isl_obj rhs)
2367 int i;
2369 for (i = 0; ; ++i) {
2370 if (!bin_ops[i].op)
2371 break;
2372 if (bin_ops[i].op != like->op)
2373 continue;
2374 if (!is_subtype(lhs, bin_ops[i].lhs))
2375 continue;
2376 if (!is_subtype(rhs, bin_ops[i].rhs))
2377 continue;
2379 return &bin_ops[i];
2382 for (i = 0; ; ++i) {
2383 if (!named_bin_ops[i].name)
2384 break;
2385 if (named_bin_ops[i].op.op != like->op)
2386 continue;
2387 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2388 continue;
2389 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2390 continue;
2392 return &named_bin_ops[i].op;
2395 return NULL;
2398 static int next_is_neg_int(struct isl_stream *s)
2400 struct isl_token *tok;
2401 int ret;
2403 tok = isl_stream_next_token(s);
2404 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2405 isl_val *v;
2406 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2407 ret = isl_val_is_neg(v);
2408 isl_val_free(v);
2409 } else
2410 ret = 0;
2411 isl_stream_push_token(s, tok);
2413 return ret;
2416 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2417 struct isl_obj lhs, struct isl_obj rhs)
2419 struct isl_obj obj;
2421 lhs = convert(ctx, lhs, op->lhs);
2422 rhs = convert(ctx, rhs, op->rhs);
2423 if (op->res != isl_obj_bool)
2424 obj.v = op->o.fn(lhs.v, rhs.v);
2425 else {
2426 int res = op->o.test(lhs.v, rhs.v);
2427 free_obj(lhs);
2428 free_obj(rhs);
2429 obj.v = iscc_bool_from_int(res);
2431 obj.type = op->res;
2433 return obj;
2436 static struct isl_obj read_expr(struct isl_stream *s,
2437 struct isl_hash_table *table)
2439 isl_ctx *ctx;
2440 struct isl_obj obj = { isl_obj_none, NULL };
2441 struct isl_obj right_obj = { isl_obj_none, NULL };
2443 obj = read_obj(s, table);
2444 ctx = isl_stream_get_ctx(s);
2445 for (; obj.v;) {
2446 struct isc_bin_op *op = NULL;
2448 op = read_bin_op_if_available(s, obj);
2449 if (!op)
2450 break;
2452 right_obj = read_obj(s, table);
2454 op = find_matching_bin_op(op, obj, right_obj);
2456 if (!op)
2457 isl_die(ctx, isl_error_invalid,
2458 "no such binary operator defined on given operands",
2459 goto error);
2461 obj = call_bin_op(ctx, op, obj, right_obj);
2464 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2465 right_obj = read_obj(s, table);
2466 obj.v = isl_val_add(obj.v, right_obj.v);
2469 return obj;
2470 error:
2471 free_obj(right_obj);
2472 free_obj(obj);
2473 obj.type = isl_obj_none;
2474 obj.v = NULL;
2475 return obj;
2478 static __isl_give isl_printer *source_file(struct isl_stream *s,
2479 struct isl_hash_table *table, __isl_take isl_printer *p);
2481 /* Print "obj" to the printer "p".
2482 * If the object is a schedule, then print it in block format.
2484 static __isl_give isl_printer *print_obj(__isl_take isl_printer *p,
2485 struct isl_obj obj)
2487 if (obj.type != isl_obj_schedule)
2488 return obj.type->print(p, obj.v);
2490 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
2491 p = obj.type->print(p, obj.v);
2492 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2494 return p;
2497 static __isl_give isl_printer *read_line(struct isl_stream *s,
2498 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2500 isl_ctx *ctx;
2501 struct isl_obj obj = { isl_obj_none, NULL };
2502 char *lhs = NULL;
2503 int assign = 0;
2504 int only_print = 0;
2505 struct isc_bin_op *op = NULL;
2506 char buf[30];
2508 if (!p)
2509 return NULL;
2510 if (isl_stream_is_empty(s))
2511 return p;
2513 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2514 return source_file(s, table, p);
2515 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2516 return codegen(s, table, p);
2518 assign = is_assign(s);
2519 if (assign) {
2520 lhs = isl_stream_read_ident_if_available(s);
2521 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2522 goto error;
2523 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2524 only_print = 1;
2525 else if (!tty)
2526 only_print = 1;
2528 obj = read_expr(s, table);
2529 ctx = isl_stream_get_ctx(s);
2530 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2531 fprintf(stderr, "Interrupted\n");
2532 isl_ctx_reset_error(ctx);
2534 if (isl_stream_eat(s, ';'))
2535 goto error;
2537 if (only_print) {
2538 if (obj.type != isl_obj_none && obj.v != NULL) {
2539 p = print_obj(p, obj);
2540 p = isl_printer_end_line(p);
2542 free_obj(obj);
2543 return p;
2545 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2546 static int count = 0;
2547 snprintf(buf, sizeof(buf), "$%d", count++);
2548 lhs = strdup(buf + 1);
2550 p = isl_printer_print_str(p, buf);
2551 p = isl_printer_print_str(p, " := ");
2552 p = obj.type->print(p, obj.v);
2553 p = isl_printer_end_line(p);
2555 if (lhs && do_assign(ctx, table, lhs, obj))
2556 return p;
2558 return p;
2559 error:
2560 isl_stream_flush_tokens(s);
2561 isl_stream_skip_line(s);
2562 free(lhs);
2563 free_obj(obj);
2564 return p;
2567 static isl_stat free_cb(void **entry, void *user)
2569 struct isl_named_obj *named = *entry;
2571 free_obj(named->obj);
2572 free(named->name);
2573 free(named);
2575 return isl_stat_ok;
2578 static void register_named_ops(struct isl_stream *s)
2580 int i;
2582 for (i = 0; i < ISCC_N_OP; ++i) {
2583 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2584 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2587 for (i = 0; ; ++i) {
2588 if (!named_un_ops[i].name)
2589 break;
2590 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2591 named_un_ops[i].name);
2592 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2595 for (i = 0; ; ++i) {
2596 if (!named_bin_ops[i].name)
2597 break;
2598 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2599 named_bin_ops[i].name);
2600 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2604 static __isl_give isl_printer *source_file(struct isl_stream *s,
2605 struct isl_hash_table *table, __isl_take isl_printer *p)
2607 isl_ctx *ctx;
2608 struct isl_token *tok;
2609 struct isl_stream *s_file;
2610 struct iscc_options *options;
2611 char *name;
2612 FILE *file;
2614 tok = isl_stream_next_token(s);
2615 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2616 isl_stream_error(s, tok, "expecting filename");
2617 isl_token_free(tok);
2618 return p;
2621 isl_stream_eat(s, ';');
2623 ctx = isl_stream_get_ctx(s);
2624 options = isl_ctx_peek_iscc_options(ctx);
2625 if (!options || !options->io) {
2626 isl_token_free(tok);
2627 isl_die(ctx, isl_error_invalid,
2628 "source operation not allowed", return p);
2631 name = isl_token_get_str(ctx, tok);
2632 isl_token_free(tok);
2633 file = fopen(name, "r");
2634 free(name);
2635 isl_assert(ctx, file, return p);
2637 s_file = isl_stream_new_file(ctx, file);
2638 if (!s_file) {
2639 fclose(file);
2640 return p;
2643 register_named_ops(s_file);
2645 while (!isl_stream_is_empty(s_file))
2646 p = read_line(s_file, table, p, 0);
2648 isl_stream_free(s_file);
2649 fclose(file);
2651 return p;
2654 int main(int argc, char **argv)
2656 struct isl_ctx *ctx;
2657 struct isl_stream *s;
2658 struct isl_hash_table *table;
2659 struct iscc_options *options;
2660 isl_printer *p;
2661 int tty = isatty(0);
2663 options = iscc_options_new_with_defaults();
2664 assert(options);
2666 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2667 pet_options_set_autodetect(ctx, 1);
2668 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2669 s = isl_stream_new_file(ctx, stdin);
2670 assert(s);
2671 table = isl_hash_table_alloc(ctx, 10);
2672 assert(table);
2673 p = isl_printer_to_file(ctx, stdout);
2674 p = isl_printer_set_output_format(p, options->format);
2675 assert(p);
2677 register_named_ops(s);
2679 install_signal_handler(ctx);
2681 while (p && !isl_stream_is_empty(s)) {
2682 isl_ctx_resume(ctx);
2683 p = read_line(s, table, p, tty);
2686 remove_signal_handler(ctx);
2688 isl_printer_free(p);
2689 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2690 isl_hash_table_free(ctx, table);
2691 isl_stream_free(s);
2692 isl_ctx_free(ctx);
2694 return 0;