iscc: codegen: accept schedule as input
[barvinok.git] / iscc.c
blob4c013f6dc80b62db0617b02610db58105d195c46
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/aff.h>
7 #include <isl/obj.h>
8 #include <isl/stream.h>
9 #include <isl/set.h>
10 #include <isl/map.h>
11 #include <isl/vertices.h>
12 #include <isl/flow.h>
13 #include <isl/band.h>
14 #include <isl/schedule.h>
15 #include <isl/ast_build.h>
16 #include <isl_obj_list.h>
17 #include <isl_obj_str.h>
18 #include <barvinok/isl.h>
19 #include <barvinok/options.h>
20 #include "lattice_width.h"
22 #include "config.h"
24 #ifdef HAVE_SIGACTION
25 #include <signal.h>
27 static isl_ctx *main_ctx;
29 static void handler(int signum)
31 if (isl_ctx_aborted(main_ctx))
32 exit(EXIT_FAILURE);
33 isl_ctx_abort(main_ctx);
36 static struct sigaction sa_old;
38 static void install_signal_handler(isl_ctx *ctx)
40 struct sigaction sa;
42 main_ctx = ctx;
44 memset(&sa, 0, sizeof(struct sigaction));
45 sa.sa_handler = &handler;
46 sa.sa_flags = SA_RESTART;
47 sigaction(SIGINT, &sa, &sa_old);
50 static void remove_signal_handler(isl_ctx *ctx)
52 sigaction(SIGINT, &sa_old, NULL);
55 #else
57 static void install_signal_handler(isl_ctx *ctx)
61 static void remove_signal_handler(isl_ctx *ctx)
65 #endif
67 #ifdef HAVE_PET
68 #include <pet.h>
69 #else
70 struct pet_options;
71 int pet_options_set_autodetect(isl_ctx *ctx, int val)
73 return -1;
75 #endif
77 static int iscc_bool_false = 0;
78 static int iscc_bool_true = 1;
79 static int iscc_bool_error = -1;
81 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
82 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
83 ISCC_SCHEDULE, ISCC_SCHEDULE_FOREST,
84 ISCC_MINIMIZING, ISCC_RESPECTING,
85 ISCC_CODEGEN, ISCC_USING,
86 ISCC_TYPEOF, ISCC_PRINT, ISCC_ASSERT,
87 ISCC_N_OP };
88 static const char *op_name[ISCC_N_OP] = {
89 [ISCC_ASSERT] = "assert",
90 [ISCC_READ] = "read",
91 [ISCC_WRITE] = "write",
92 [ISCC_PRINT] = "print",
93 [ISCC_SOURCE] = "source",
94 [ISCC_VERTICES] = "vertices",
95 [ISCC_LAST] = "last",
96 [ISCC_ANY] = "any",
97 [ISCC_BEFORE] = "before",
98 [ISCC_UNDER] = "under",
99 [ISCC_SCHEDULE] = "schedule",
100 [ISCC_SCHEDULE_FOREST] = "schedule_forest",
101 [ISCC_MINIMIZING] = "minimizing",
102 [ISCC_RESPECTING] = "respecting",
103 [ISCC_CODEGEN] = "codegen",
104 [ISCC_USING] = "using",
105 [ISCC_TYPEOF] = "typeof"
107 static enum isl_token_type iscc_op[ISCC_N_OP];
109 struct isl_arg_choice iscc_format[] = {
110 {"isl", ISL_FORMAT_ISL},
111 {"omega", ISL_FORMAT_OMEGA},
112 {"polylib", ISL_FORMAT_POLYLIB},
113 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
114 {"latex", ISL_FORMAT_LATEX},
115 {"C", ISL_FORMAT_C},
119 struct iscc_options {
120 struct barvinok_options *barvinok;
121 struct pet_options *pet;
122 unsigned format;
123 int io;
126 ISL_ARGS_START(struct iscc_options, iscc_options_args)
127 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", &barvinok_options_args,
128 "barvinok options")
129 #ifdef HAVE_PET
130 ISL_ARG_CHILD(struct iscc_options, pet, "pet", &pet_options_args, "pet options")
131 #endif
132 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
133 iscc_format, ISL_FORMAT_ISL, "output format")
134 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
135 "allow read and write operations")
136 ISL_ARGS_END
138 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_args)
139 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_args)
141 static void *isl_obj_bool_copy(void *v)
143 return v;
146 static void isl_obj_bool_free(void *v)
150 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
151 void *v)
153 if (v == &iscc_bool_true)
154 return isl_printer_print_str(p, "True");
155 else if (v == &iscc_bool_false)
156 return isl_printer_print_str(p, "False");
157 else
158 return isl_printer_print_str(p, "Error");
161 static void *isl_obj_bool_add(void *v1, void *v2)
163 return v1;
166 struct isl_obj_vtable isl_obj_bool_vtable = {
167 isl_obj_bool_copy,
168 isl_obj_bool_add,
169 isl_obj_bool_print,
170 isl_obj_bool_free
172 #define isl_obj_bool (&isl_obj_bool_vtable)
174 int *iscc_bool_from_int(int res)
176 return res < 0 ? &iscc_bool_error :
177 res ? &iscc_bool_true : &iscc_bool_false;
180 static int isl_union_map_is_superset(__isl_take isl_union_map *map1,
181 __isl_take isl_union_map *map2)
183 return isl_union_map_is_subset(map2, map1);
185 static int isl_union_set_is_superset(__isl_take isl_union_set *set1,
186 __isl_take isl_union_set *set2)
188 return isl_union_set_is_subset(set2, set1);
191 static int isl_union_map_is_strict_superset(__isl_take isl_union_map *map1,
192 __isl_take isl_union_map *map2)
194 return isl_union_map_is_strict_subset(map2, map1);
196 static int isl_union_set_is_strict_superset(__isl_take isl_union_set *set1,
197 __isl_take isl_union_set *set2)
199 return isl_union_set_is_strict_subset(set2, set1);
202 extern struct isl_obj_vtable isl_obj_list_vtable;
203 #define isl_obj_list (&isl_obj_list_vtable)
205 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
206 typedef int (*isc_bin_test_fn)(void *lhs, void *rhs);
207 struct isc_bin_op {
208 enum isl_token_type op;
209 isl_obj_type lhs;
210 isl_obj_type rhs;
211 isl_obj_type res;
212 union {
213 isc_bin_op_fn fn;
214 isc_bin_test_fn test;
215 } o;
217 struct isc_named_bin_op {
218 char *name;
219 struct isc_bin_op op;
222 struct iscc_at {
223 isl_union_pw_qpolynomial *upwqp;
224 isl_union_pw_qpolynomial *res;
227 static isl_stat eval_at(__isl_take isl_point *pnt, void *user)
229 struct iscc_at *at = (struct iscc_at *) user;
230 isl_val *v;
231 isl_qpolynomial *qp;
232 isl_set *set;
234 set = isl_set_from_point(isl_point_copy(pnt));
235 v = isl_union_pw_qpolynomial_eval(
236 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
237 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
239 at->res = isl_union_pw_qpolynomial_add(at->res,
240 isl_union_pw_qpolynomial_from_pw_qpolynomial(
241 isl_pw_qpolynomial_alloc(set, qp)));
243 return isl_stat_ok;
246 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
247 __isl_take isl_union_pw_qpolynomial *upwqp,
248 __isl_take isl_union_set *uset)
250 struct iscc_at at;
252 at.upwqp = upwqp;
253 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
255 isl_union_set_foreach_point(uset, eval_at, &at);
257 isl_union_pw_qpolynomial_free(upwqp);
258 isl_union_set_free(uset);
260 return at.res;
263 struct iscc_fold_at {
264 isl_union_pw_qpolynomial_fold *upwf;
265 isl_union_pw_qpolynomial *res;
268 static isl_stat eval_fold_at(__isl_take isl_point *pnt, void *user)
270 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
271 isl_val *v;
272 isl_qpolynomial *qp;
273 isl_set *set;
275 set = isl_set_from_point(isl_point_copy(pnt));
276 v = isl_union_pw_qpolynomial_fold_eval(
277 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
278 qp = isl_qpolynomial_val_on_domain(isl_set_get_space(set), v);
280 at->res = isl_union_pw_qpolynomial_add(at->res,
281 isl_union_pw_qpolynomial_from_pw_qpolynomial(
282 isl_pw_qpolynomial_alloc(set, qp)));
284 return isl_stat_ok;
287 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
288 __isl_take isl_union_pw_qpolynomial_fold *upwf,
289 __isl_take isl_union_set *uset)
291 struct iscc_fold_at at;
293 at.upwf = upwf;
294 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset));
296 isl_union_set_foreach_point(uset, eval_fold_at, &at);
298 isl_union_pw_qpolynomial_fold_free(upwf);
299 isl_union_set_free(uset);
301 return at.res;
304 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
305 __isl_take isl_union_pw_qpolynomial *upwqp,
306 __isl_take isl_union_pw_qpolynomial_fold *upwf)
308 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
309 upwqp);
312 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
313 __isl_take isl_union_map *umap,
314 __isl_take isl_union_pw_qpolynomial_fold *upwf)
316 isl_ctx *ctx;
317 struct isl_list *list;
318 int tight;
320 ctx = isl_union_map_get_ctx(umap);
321 list = isl_list_alloc(ctx, 2);
322 if (!list)
323 goto error2;
325 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
326 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
327 upwf, &tight);
328 list->obj[1].type = isl_obj_bool;
329 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
330 if (tight < 0 || !list->obj[0].v)
331 goto error;
333 return list;
334 error2:
335 isl_union_map_free(umap);
336 isl_union_pw_qpolynomial_fold_free(upwf);
337 error:
338 isl_list_free(list);
339 return NULL;
342 static __isl_give struct isl_list *union_set_apply_union_pw_qpolynomial_fold(
343 __isl_take isl_union_set *uset,
344 __isl_take isl_union_pw_qpolynomial_fold *upwf)
346 isl_ctx *ctx;
347 struct isl_list *list;
348 int tight;
350 ctx = isl_union_set_get_ctx(uset);
351 list = isl_list_alloc(ctx, 2);
352 if (!list)
353 goto error2;
355 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
356 list->obj[0].v = isl_union_set_apply_union_pw_qpolynomial_fold(uset,
357 upwf, &tight);
358 list->obj[1].type = isl_obj_bool;
359 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
360 if (tight < 0 || !list->obj[0].v)
361 goto error;
363 return list;
364 error2:
365 isl_union_set_free(uset);
366 isl_union_pw_qpolynomial_fold_free(upwf);
367 error:
368 isl_list_free(list);
369 return NULL;
372 static __isl_give isl_union_pw_qpolynomial *isl_val_mul_union_pw_qpolynomial(
373 __isl_take isl_val *v, __isl_take isl_union_pw_qpolynomial *upwqp)
375 return isl_union_pw_qpolynomial_scale_val(upwqp, v);
378 static __isl_give isl_union_pw_qpolynomial_fold *
379 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val *v,
380 __isl_take isl_union_pw_qpolynomial_fold *upwf)
382 return isl_union_pw_qpolynomial_fold_scale_val(upwf, v);
385 /* Are the two strings "str1" and "str2" equal to each other?
387 static int str_eq(__isl_keep isl_str *str1, __isl_keep isl_str *str2)
389 if (!str1 || !str2)
390 return -1;
392 return !strcmp(str1->s, str2->s);
395 struct isc_bin_op bin_ops[] = {
396 { '+', isl_obj_val, isl_obj_val, isl_obj_val,
397 (isc_bin_op_fn) &isl_val_add },
398 { '-', isl_obj_val, isl_obj_val, isl_obj_val,
399 (isc_bin_op_fn) &isl_val_sub },
400 { '*', isl_obj_val, isl_obj_val, isl_obj_val,
401 (isc_bin_op_fn) &isl_val_mul },
402 { '+', isl_obj_pw_multi_aff, isl_obj_pw_multi_aff,
403 isl_obj_pw_multi_aff,
404 (isc_bin_op_fn) &isl_pw_multi_aff_add },
405 { '+', isl_obj_union_set, isl_obj_union_set,
406 isl_obj_union_set,
407 (isc_bin_op_fn) &isl_union_set_union },
408 { '+', isl_obj_union_map, isl_obj_union_map,
409 isl_obj_union_map,
410 (isc_bin_op_fn) &isl_union_map_union },
411 { '-', isl_obj_union_set, isl_obj_union_set,
412 isl_obj_union_set,
413 (isc_bin_op_fn) &isl_union_set_subtract },
414 { '-', isl_obj_union_map, isl_obj_union_map,
415 isl_obj_union_map,
416 (isc_bin_op_fn) &isl_union_map_subtract },
417 { '*', isl_obj_union_set, isl_obj_union_set,
418 isl_obj_union_set,
419 (isc_bin_op_fn) &isl_union_set_intersect },
420 { '*', isl_obj_union_map, isl_obj_union_map,
421 isl_obj_union_map,
422 (isc_bin_op_fn) &isl_union_map_intersect },
423 { '*', isl_obj_union_map, isl_obj_union_set,
424 isl_obj_union_map,
425 (isc_bin_op_fn) &isl_union_map_intersect_domain },
426 { '.', isl_obj_union_map, isl_obj_union_map,
427 isl_obj_union_map,
428 (isc_bin_op_fn) &isl_union_map_apply_range },
429 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
430 isl_obj_union_pw_qpolynomial,
431 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
432 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
433 isl_obj_list,
434 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
435 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
436 isl_obj_union_map,
437 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
438 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
439 { .test = (isc_bin_test_fn) &isl_union_set_is_equal } },
440 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
441 { .test = (isc_bin_test_fn) &isl_union_map_is_equal } },
442 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
443 isl_obj_bool,
444 { .test = (isc_bin_test_fn) &isl_union_set_is_subset } },
445 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
446 isl_obj_bool,
447 { .test = (isc_bin_test_fn) &isl_union_map_is_subset } },
448 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
449 isl_obj_bool,
450 { .test = (isc_bin_test_fn) &isl_union_set_is_strict_subset } },
451 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
452 isl_obj_bool,
453 { .test = (isc_bin_test_fn) &isl_union_map_is_strict_subset } },
454 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
455 isl_obj_bool,
456 { .test = (isc_bin_test_fn) &isl_union_set_is_superset } },
457 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
458 isl_obj_bool,
459 { .test = (isc_bin_test_fn) &isl_union_map_is_superset } },
460 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
461 isl_obj_bool,
462 { .test =
463 (isc_bin_test_fn) &isl_union_set_is_strict_superset } },
464 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
465 isl_obj_bool,
466 { .test =
467 (isc_bin_test_fn) &isl_union_map_is_strict_superset } },
468 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
469 isl_obj_union_map,
470 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
471 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
472 isl_obj_union_map,
473 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
474 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
475 isl_obj_union_map,
476 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
477 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
478 isl_obj_union_map,
479 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
480 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
481 isl_obj_union_map,
482 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
483 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
484 isl_obj_union_map,
485 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
486 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
487 isl_obj_union_map,
488 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
489 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
490 isl_obj_union_map,
491 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
492 { '.', isl_obj_union_pw_qpolynomial_fold,
493 isl_obj_union_pw_qpolynomial_fold,
494 isl_obj_union_pw_qpolynomial_fold,
495 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
496 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
497 isl_obj_union_pw_qpolynomial,
498 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
499 { '+', isl_obj_union_pw_qpolynomial,
500 isl_obj_union_pw_qpolynomial_fold,
501 isl_obj_union_pw_qpolynomial_fold,
502 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
503 { '+', isl_obj_union_pw_qpolynomial_fold,
504 isl_obj_union_pw_qpolynomial,
505 isl_obj_union_pw_qpolynomial_fold,
506 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
507 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
508 isl_obj_union_pw_qpolynomial,
509 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
510 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial,
511 isl_obj_union_pw_qpolynomial,
512 (isc_bin_op_fn) &isl_val_mul_union_pw_qpolynomial },
513 { '*', isl_obj_union_pw_qpolynomial, isl_obj_val,
514 isl_obj_union_pw_qpolynomial,
515 (isc_bin_op_fn) &isl_union_pw_qpolynomial_scale_val },
516 { '*', isl_obj_val, isl_obj_union_pw_qpolynomial_fold,
517 isl_obj_union_pw_qpolynomial_fold,
518 (isc_bin_op_fn) &int_val_mul_union_pw_qpolynomial_fold },
519 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_val,
520 isl_obj_union_pw_qpolynomial_fold,
521 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_scale_val },
522 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
523 isl_obj_union_pw_qpolynomial,
524 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
525 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
526 isl_obj_union_pw_qpolynomial,
527 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
528 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
529 isl_obj_union_pw_qpolynomial_fold,
530 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
531 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
532 isl_obj_union_pw_qpolynomial,
533 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
534 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
535 isl_obj_union_pw_qpolynomial,
536 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
537 { '%', isl_obj_union_set, isl_obj_union_set,
538 isl_obj_union_set,
539 (isc_bin_op_fn) &isl_union_set_gist },
540 { '%', isl_obj_union_map, isl_obj_union_map,
541 isl_obj_union_map,
542 (isc_bin_op_fn) &isl_union_map_gist },
543 { '%', isl_obj_union_map, isl_obj_union_set,
544 isl_obj_union_map,
545 (isc_bin_op_fn) &isl_union_map_gist_domain },
546 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
547 isl_obj_union_pw_qpolynomial,
548 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
549 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
550 isl_obj_union_pw_qpolynomial_fold,
551 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
552 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial,
553 isl_obj_union_pw_qpolynomial, isl_obj_bool,
554 { .test = (isc_bin_test_fn)
555 &isl_union_pw_qpolynomial_plain_is_equal } },
556 { ISL_TOKEN_EQ_EQ, isl_obj_union_pw_qpolynomial_fold,
557 isl_obj_union_pw_qpolynomial_fold, isl_obj_bool,
558 { .test = (isc_bin_test_fn)
559 &isl_union_pw_qpolynomial_fold_plain_is_equal } },
560 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
561 (isc_bin_op_fn) &isl_str_concat },
562 { '=', isl_obj_str, isl_obj_str, isl_obj_bool,
563 { .test = (isc_bin_test_fn) &str_eq } },
567 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
568 __isl_take isl_union_map *umap2)
570 return isl_union_map_apply_range(umap2, umap1);
573 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
574 __isl_take isl_union_pw_qpolynomial *upwqp,
575 __isl_take isl_union_map *umap)
577 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
580 static __isl_give struct isl_list *qpolynomial_fold_after_map(
581 __isl_take isl_union_pw_qpolynomial_fold *upwf,
582 __isl_take isl_union_map *umap)
584 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
587 struct isc_named_bin_op named_bin_ops[] = {
588 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
589 isl_obj_union_map,
590 (isc_bin_op_fn) &map_after_map } },
591 { "after", { -1, isl_obj_union_pw_qpolynomial,
592 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
593 (isc_bin_op_fn) &qpolynomial_after_map } },
594 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
595 isl_obj_union_map, isl_obj_list,
596 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
597 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
598 isl_obj_union_map,
599 (isc_bin_op_fn) &isl_union_map_apply_range } },
600 { "before", { -1, isl_obj_union_map,
601 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
602 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
603 { "before", { -1, isl_obj_union_map,
604 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
605 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
606 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
607 isl_obj_union_set,
608 (isc_bin_op_fn) &isl_union_set_product } },
609 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
610 isl_obj_union_map,
611 (isc_bin_op_fn) &isl_union_map_product } },
612 NULL
615 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
617 return isl_set_from_basic_set(isl_union_set_sample(uset));
620 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
622 return isl_map_from_basic_map(isl_union_map_sample(umap));
625 static __isl_give struct isl_list *union_map_power(
626 __isl_take isl_union_map *umap)
628 isl_ctx *ctx;
629 struct isl_list *list;
630 int exact;
632 ctx = isl_union_map_get_ctx(umap);
633 list = isl_list_alloc(ctx, 2);
634 if (!list)
635 goto error2;
637 list->obj[0].type = isl_obj_union_map;
638 list->obj[0].v = isl_union_map_power(umap, &exact);
639 list->obj[1].type = isl_obj_bool;
640 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
641 if (exact < 0 || !list->obj[0].v)
642 goto error;
644 return list;
645 error2:
646 isl_union_map_free(umap);
647 error:
648 isl_list_free(list);
649 return NULL;
652 /* Compute a lower or upper bound on "upwqp" depending on "type" and
653 * return a list containing two elements, the bound and a boolean
654 * indicating whether the result is tight.
656 static __isl_give struct isl_list *union_pw_qpolynomial_bound(
657 __isl_take isl_union_pw_qpolynomial *upwqp, enum isl_fold type)
659 isl_ctx *ctx;
660 struct isl_list *list;
661 int tight;
663 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
664 list = isl_list_alloc(ctx, 2);
665 if (!list)
666 goto error2;
668 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
669 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp, type, &tight);
670 list->obj[1].type = isl_obj_bool;
671 list->obj[1].v = tight ? &iscc_bool_true : &iscc_bool_false;
672 if (tight < 0 || !list->obj[0].v)
673 goto error;
675 return list;
676 error2:
677 isl_union_pw_qpolynomial_free(upwqp);
678 error:
679 isl_list_free(list);
680 return NULL;
683 /* Compute a lower bound on "upwqp" and return a list containing
684 * two elements, the bound and a booleanindicating whether
685 * the result is tight.
687 static __isl_give struct isl_list *union_pw_qpolynomial_lower_bound(
688 __isl_take isl_union_pw_qpolynomial *upwqp)
690 return union_pw_qpolynomial_bound(upwqp, isl_fold_min);
693 /* Compute a upper bound on "upwqp" and return a list containing
694 * two elements, the bound and a booleanindicating whether
695 * the result is tight.
697 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
698 __isl_take isl_union_pw_qpolynomial *upwqp)
700 return union_pw_qpolynomial_bound(upwqp, isl_fold_max);
703 #ifdef HAVE_PET
704 static __isl_give isl_list *parse(__isl_take isl_str *str)
706 isl_ctx *ctx;
707 struct isl_list *list;
708 struct pet_scop *scop;
709 isl_union_map *sched, *may_reads, *must_writes, *may_writes;
710 isl_union_set *domain;
711 struct iscc_options *options;
713 if (!str)
714 return NULL;
715 ctx = str->ctx;
717 options = isl_ctx_peek_iscc_options(ctx);
718 if (!options || !options->io) {
719 isl_str_free(str);
720 isl_die(ctx, isl_error_invalid,
721 "parse_file operation not allowed", return NULL);
724 list = isl_list_alloc(ctx, 5);
725 if (!list)
726 goto error;
728 scop = pet_scop_extract_from_C_source(ctx, str->s, NULL);
729 domain = pet_scop_collect_domains(scop);
730 sched = scop ? isl_schedule_get_map(scop->schedule) : NULL;
731 may_reads = pet_scop_collect_may_reads(scop);
732 may_writes = pet_scop_collect_may_writes(scop);
733 must_writes = pet_scop_collect_must_writes(scop);
734 pet_scop_free(scop);
736 list->obj[0].type = isl_obj_union_set;
737 list->obj[0].v = domain;
738 list->obj[1].type = isl_obj_union_map;
739 list->obj[1].v = must_writes;
740 list->obj[2].type = isl_obj_union_map;
741 list->obj[2].v = may_writes;
742 list->obj[3].type = isl_obj_union_map;
743 list->obj[3].v = may_reads;
744 list->obj[4].type = isl_obj_union_map;
745 list->obj[4].v = sched;
747 if (!list->obj[0].v || !list->obj[1].v ||
748 !list->obj[2].v || !list->obj[3].v || !list->obj[4].v)
749 goto error;
751 isl_str_free(str);
752 return list;
753 error:
754 isl_list_free(list);
755 isl_str_free(str);
756 return NULL;
758 #endif
760 static isl_stat add_point(__isl_take isl_point *pnt, void *user)
762 isl_union_set **scan = (isl_union_set **) user;
764 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
766 return isl_stat_ok;
769 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
771 isl_union_set *scan;
773 scan = isl_union_set_empty(isl_union_set_get_space(uset));
775 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
776 isl_union_set_free(scan);
777 return uset;
780 isl_union_set_free(uset);
781 return scan;
784 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
786 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
789 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
790 __isl_take isl_union_pw_qpolynomial *upwqp)
792 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
795 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
796 __isl_take isl_union_pw_qpolynomial *upwqp)
798 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
801 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
802 __isl_take isl_union_pw_qpolynomial *upwqp)
804 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
807 typedef void *(*isc_un_op_fn)(void *arg);
808 struct isc_un_op {
809 enum isl_token_type op;
810 isl_obj_type arg;
811 isl_obj_type res;
812 isc_un_op_fn fn;
814 struct isc_named_un_op {
815 char *name;
816 struct isc_un_op op;
818 struct isc_named_un_op named_un_ops[] = {
819 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
820 (isc_un_op_fn) &isl_union_map_affine_hull } },
821 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
822 (isc_un_op_fn) &isl_union_set_affine_hull } },
823 {"card", { -1, isl_obj_union_set,
824 isl_obj_union_pw_qpolynomial,
825 (isc_un_op_fn) &isl_union_set_card } },
826 {"card", { -1, isl_obj_union_map,
827 isl_obj_union_pw_qpolynomial,
828 (isc_un_op_fn) &isl_union_map_card } },
829 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
830 (isc_un_op_fn) &isl_union_set_coalesce } },
831 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
832 (isc_un_op_fn) &isl_union_map_coalesce } },
833 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
834 isl_obj_union_pw_qpolynomial,
835 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
836 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
837 isl_obj_union_pw_qpolynomial_fold,
838 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
839 {"coefficients", { -1, isl_obj_union_set,
840 isl_obj_union_set,
841 (isc_un_op_fn) &isl_union_set_coefficients } },
842 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
843 (isc_un_op_fn) &isl_union_set_solutions } },
844 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
845 (isc_un_op_fn) &isl_union_map_deltas } },
846 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
847 (isc_un_op_fn) &isl_union_map_deltas_map } },
848 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
849 (isc_un_op_fn) &isl_union_map_domain } },
850 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
851 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
852 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
853 isl_obj_union_set,
854 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
855 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
856 (isc_un_op_fn) &isl_union_map_domain } },
857 {"domain", { -1, isl_obj_union_pw_qpolynomial,
858 isl_obj_union_set,
859 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
860 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
861 isl_obj_union_set,
862 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
863 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
864 (isc_un_op_fn) &isl_union_map_domain_map } },
865 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
866 (isc_un_op_fn) &isl_union_map_range } },
867 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
868 (isc_un_op_fn) &isl_union_map_range } },
869 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
870 (isc_un_op_fn) &isl_union_map_range_map } },
871 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
872 (isc_un_op_fn) &isl_union_set_identity } },
873 {"lattice_width", { -1, isl_obj_union_set,
874 isl_obj_union_pw_qpolynomial,
875 (isc_un_op_fn) &isl_union_set_lattice_width } },
876 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
877 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
878 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
879 (isc_un_op_fn) &isl_union_map_lexmin } },
880 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
881 (isc_un_op_fn) &isl_union_map_lexmax } },
882 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
883 (isc_un_op_fn) &isl_union_set_lexmin } },
884 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
885 (isc_un_op_fn) &isl_union_set_lexmax } },
886 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
887 (isc_un_op_fn) &isl_union_set_lift } },
888 {"params", { -1, isl_obj_union_map, isl_obj_set,
889 (isc_un_op_fn) &isl_union_map_params } },
890 {"params", { -1, isl_obj_union_set, isl_obj_set,
891 (isc_un_op_fn) &isl_union_set_params } },
892 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
893 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
894 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
895 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
896 {"poly", { -1, isl_obj_union_pw_qpolynomial,
897 isl_obj_union_pw_qpolynomial,
898 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
899 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
900 isl_obj_union_pw_qpolynomial,
901 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
902 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
903 isl_obj_union_pw_qpolynomial,
904 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
905 #ifdef HAVE_PET
906 {"parse_file", { -1, isl_obj_str, isl_obj_list,
907 (isc_un_op_fn) &parse } },
908 #endif
909 {"pow", { -1, isl_obj_union_map, isl_obj_list,
910 (isc_un_op_fn) &union_map_power } },
911 {"sample", { -1, isl_obj_union_set, isl_obj_set,
912 (isc_un_op_fn) &union_set_sample } },
913 {"sample", { -1, isl_obj_union_map, isl_obj_map,
914 (isc_un_op_fn) &union_map_sample } },
915 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
916 (isc_un_op_fn) &union_set_scan } },
917 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
918 (isc_un_op_fn) &union_map_scan } },
919 {"sum", { -1, isl_obj_union_pw_qpolynomial,
920 isl_obj_union_pw_qpolynomial,
921 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
922 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
923 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
924 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
925 (isc_un_op_fn) &isl_union_set_unwrap } },
926 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
927 (isc_un_op_fn) &isl_union_map_wrap } },
928 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
929 (isc_un_op_fn) &isl_union_map_zip } },
930 NULL
933 struct isl_named_obj {
934 char *name;
935 struct isl_obj obj;
938 static void free_obj(struct isl_obj obj)
940 obj.type->free(obj.v);
943 static int same_name(const void *entry, const void *val)
945 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
947 return !strcmp(named->name, val);
950 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
951 char *name, struct isl_obj obj)
953 struct isl_hash_table_entry *entry;
954 uint32_t name_hash;
955 struct isl_named_obj *named;
957 name_hash = isl_hash_string(isl_hash_init(), name);
958 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
959 if (!entry)
960 goto error;
961 if (entry->data) {
962 named = entry->data;
963 free_obj(named->obj);
964 free(name);
965 } else {
966 named = isl_alloc_type(ctx, struct isl_named_obj);
967 if (!named)
968 goto error;
969 named->name = name;
970 entry->data = named;
972 named->obj = obj;
974 return 0;
975 error:
976 free_obj(obj);
977 free(name);
978 return -1;
981 static struct isl_obj stored_obj(struct isl_ctx *ctx,
982 struct isl_hash_table *table, char *name)
984 struct isl_obj obj = { isl_obj_none, NULL };
985 struct isl_hash_table_entry *entry;
986 uint32_t name_hash;
988 name_hash = isl_hash_string(isl_hash_init(), name);
989 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
990 if (entry) {
991 struct isl_named_obj *named;
992 named = entry->data;
993 obj = named->obj;
994 } else if (isdigit(name[0]))
995 fprintf(stderr, "unknown identifier '$%s'\n", name);
996 else
997 fprintf(stderr, "unknown identifier '%s'\n", name);
999 free(name);
1000 obj.v = obj.type->copy(obj.v);
1001 return obj;
1004 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1006 if (obj.type == super)
1007 return 1;
1008 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1009 return 1;
1010 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1011 return 1;
1012 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1013 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1014 int is_set = isl_space_is_set(space);
1015 isl_space_free(space);
1016 return is_set;
1018 if (obj.type == isl_obj_pw_qpolynomial &&
1019 super == isl_obj_union_pw_qpolynomial)
1020 return 1;
1021 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1022 super == isl_obj_union_pw_qpolynomial_fold)
1023 return 1;
1024 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1025 return 1;
1026 if (obj.type == isl_obj_list) {
1027 struct isl_list *list = obj.v;
1028 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1029 return is_subtype(list->obj[0], super);
1031 if (super == isl_obj_str)
1032 return 1;
1033 return 0;
1036 static struct isl_obj obj_at(struct isl_obj obj, int i)
1038 struct isl_list *list = obj.v;
1040 obj = list->obj[i];
1041 obj.v = obj.type->copy(obj.v);
1043 isl_list_free(list);
1045 return obj;
1048 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1049 isl_obj_type type)
1051 if (obj.type == type)
1052 return obj;
1053 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1054 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1055 obj.type = isl_obj_union_set;
1056 obj.v = isl_union_set_from_set(set);
1057 return obj;
1059 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1060 obj.type = isl_obj_union_map;
1061 obj.v = isl_union_map_from_map(obj.v);
1062 return obj;
1064 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1065 obj.type = isl_obj_union_set;
1066 obj.v = isl_union_set_from_set(obj.v);
1067 return obj;
1069 if (obj.type == isl_obj_pw_qpolynomial &&
1070 type == isl_obj_union_pw_qpolynomial) {
1071 obj.type = isl_obj_union_pw_qpolynomial;
1072 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1073 return obj;
1075 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1076 type == isl_obj_union_pw_qpolynomial_fold) {
1077 obj.type = isl_obj_union_pw_qpolynomial_fold;
1078 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1079 return obj;
1081 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1082 if (type == isl_obj_union_map) {
1083 obj.type = isl_obj_union_map;
1084 return obj;
1086 if (type == isl_obj_union_pw_qpolynomial) {
1087 isl_space *dim = isl_union_set_get_space(obj.v);
1088 isl_union_set_free(obj.v);
1089 obj.v = isl_union_pw_qpolynomial_zero(dim);
1090 obj.type = isl_obj_union_pw_qpolynomial;
1091 return obj;
1093 if (type == isl_obj_union_pw_qpolynomial_fold) {
1094 isl_space *dim = isl_union_set_get_space(obj.v);
1095 isl_union_set_free(obj.v);
1096 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1097 isl_fold_list);
1098 obj.type = isl_obj_union_pw_qpolynomial_fold;
1099 return obj;
1102 if (obj.type == isl_obj_list) {
1103 struct isl_list *list = obj.v;
1104 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1105 return convert(ctx, obj_at(obj, 0), type);
1107 if (type == isl_obj_str) {
1108 isl_str *str;
1109 isl_printer *p;
1110 char *s;
1112 p = isl_printer_to_str(ctx);
1113 if (!p)
1114 goto error;
1115 p = obj.type->print(p, obj.v);
1116 s = isl_printer_get_str(p);
1117 isl_printer_free(p);
1119 str = isl_str_from_string(ctx, s);
1120 if (!str)
1121 goto error;
1122 free_obj(obj);
1123 obj.v = str;
1124 obj.type = isl_obj_str;
1125 return obj;
1128 error:
1129 free_obj(obj);
1130 obj.type = isl_obj_none;
1131 obj.v = NULL;
1132 return obj;
1135 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1136 struct isl_obj lhs)
1138 int i;
1139 struct isl_token *tok;
1141 tok = isl_stream_next_token(s);
1142 if (!tok)
1143 return NULL;
1145 for (i = 0; ; ++i) {
1146 if (!bin_ops[i].op)
1147 break;
1148 if (bin_ops[i].op != isl_token_get_type(tok))
1149 continue;
1150 if (!is_subtype(lhs, bin_ops[i].lhs))
1151 continue;
1153 isl_token_free(tok);
1154 return &bin_ops[i];
1157 for (i = 0; ; ++i) {
1158 if (!named_bin_ops[i].name)
1159 break;
1160 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1161 continue;
1162 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1163 continue;
1165 isl_token_free(tok);
1166 return &named_bin_ops[i].op;
1169 isl_stream_push_token(s, tok);
1171 return NULL;
1174 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1176 int i;
1177 struct isl_token *tok;
1179 tok = isl_stream_next_token(s);
1180 if (!tok)
1181 return NULL;
1183 for (i = 0; ; ++i) {
1184 if (!named_un_ops[i].name)
1185 break;
1186 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1187 continue;
1189 isl_token_free(tok);
1190 return &named_un_ops[i].op;
1193 isl_stream_push_token(s, tok);
1195 return NULL;
1198 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1199 struct isl_obj arg)
1201 int i;
1203 for (i = 0; ; ++i) {
1204 if (!named_un_ops[i].name)
1205 break;
1206 if (named_un_ops[i].op.op != like->op)
1207 continue;
1208 if (!is_subtype(arg, named_un_ops[i].op.arg))
1209 continue;
1211 return &named_un_ops[i].op;
1214 return NULL;
1217 static int is_assign(struct isl_stream *s)
1219 struct isl_token *tok;
1220 struct isl_token *tok2;
1221 int assign;
1223 tok = isl_stream_next_token(s);
1224 if (!tok)
1225 return 0;
1226 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1227 isl_stream_push_token(s, tok);
1228 return 0;
1231 tok2 = isl_stream_next_token(s);
1232 if (!tok2) {
1233 isl_stream_push_token(s, tok);
1234 return 0;
1236 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1237 isl_stream_push_token(s, tok2);
1238 isl_stream_push_token(s, tok);
1240 return assign;
1243 static struct isl_obj read_obj(struct isl_stream *s,
1244 struct isl_hash_table *table);
1245 static struct isl_obj read_expr(struct isl_stream *s,
1246 struct isl_hash_table *table);
1248 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1249 struct isl_hash_table *table, struct isc_un_op *op)
1251 isl_ctx *ctx;
1252 struct isl_obj obj = { isl_obj_none, NULL };
1254 obj = read_obj(s, table);
1255 if (!obj.v)
1256 goto error;
1258 op = find_matching_un_op(op, obj);
1260 ctx = isl_stream_get_ctx(s);
1261 if (!op)
1262 isl_die(ctx, isl_error_invalid,
1263 "no such unary operator defined on given operand",
1264 goto error);
1266 obj = convert(ctx, obj, op->arg);
1267 obj.v = op->fn(obj.v);
1268 obj.type = op->res;
1270 return obj;
1271 error:
1272 free_obj(obj);
1273 obj.type = isl_obj_none;
1274 obj.v = NULL;
1275 return obj;
1278 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1280 struct isl_list *list;
1281 int exact;
1283 if (obj.type != isl_obj_union_map)
1284 obj = convert(ctx, obj, isl_obj_union_map);
1285 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1286 list = isl_list_alloc(ctx, 2);
1287 if (!list)
1288 goto error;
1290 list->obj[0].type = isl_obj_union_map;
1291 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1292 list->obj[1].type = isl_obj_bool;
1293 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1294 obj.v = list;
1295 obj.type = isl_obj_list;
1296 if (exact < 0 || !list->obj[0].v)
1297 goto error;
1299 return obj;
1300 error:
1301 free_obj(obj);
1302 obj.type = isl_obj_none;
1303 obj.v = NULL;
1304 return obj;
1307 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1309 struct isl_list *list = obj.v;
1310 struct isl_token *tok;
1311 isl_ctx *ctx;
1312 isl_val *v;
1313 int i;
1315 tok = isl_stream_next_token(s);
1316 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1317 isl_stream_error(s, tok, "expecting index");
1318 if (tok)
1319 isl_stream_push_token(s, tok);
1320 goto error;
1322 ctx = isl_stream_get_ctx(s);
1323 v = isl_token_get_val(ctx, tok);
1324 i = isl_val_get_num_si(v);
1325 isl_val_free(v);
1326 isl_token_free(tok);
1327 isl_assert(ctx, i < list->n, goto error);
1328 if (isl_stream_eat(s, ']'))
1329 goto error;
1331 return obj_at(obj, i);
1332 error:
1333 free_obj(obj);
1334 obj.type = isl_obj_none;
1335 obj.v = NULL;
1336 return obj;
1339 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1340 struct isl_hash_table *table)
1342 isl_ctx *ctx;
1343 struct isl_obj obj;
1345 obj = read_expr(s, table);
1346 ctx = isl_stream_get_ctx(s);
1347 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1348 is_subtype(obj, isl_obj_union_map), goto error);
1350 if (obj.type == isl_obj_list) {
1351 struct isl_list *list = obj.v;
1352 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1353 obj = obj_at(obj, 0);
1355 if (obj.type == isl_obj_set)
1356 obj = convert(ctx, obj, isl_obj_union_set);
1357 else if (obj.type == isl_obj_map)
1358 obj = convert(ctx, obj, isl_obj_union_map);
1359 if (obj.type == isl_obj_union_set) {
1360 obj.v = isl_union_set_apply(obj.v, umap);
1361 } else
1362 obj.v = isl_union_map_apply_range(obj.v, umap);
1363 if (!obj.v)
1364 goto error2;
1366 if (isl_stream_eat(s, ')'))
1367 goto error2;
1369 return obj;
1370 error:
1371 isl_union_map_free(umap);
1372 error2:
1373 free_obj(obj);
1374 obj.type = isl_obj_none;
1375 obj.v = NULL;
1376 return obj;
1379 static struct isl_obj apply_fun_set(struct isl_obj obj,
1380 __isl_take isl_union_set *uset)
1382 if (obj.type == isl_obj_union_pw_qpolynomial) {
1383 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1384 } else {
1385 obj.type = isl_obj_list;
1386 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1388 return obj;
1391 static struct isl_obj apply_fun_map(struct isl_obj obj,
1392 __isl_take isl_union_map *umap)
1394 if (obj.type == isl_obj_union_pw_qpolynomial) {
1395 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1396 } else {
1397 obj.type = isl_obj_list;
1398 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1400 return obj;
1403 static struct isl_obj apply_fun(struct isl_stream *s,
1404 struct isl_obj obj, struct isl_hash_table *table)
1406 struct isl_obj arg;
1407 isl_ctx *ctx;
1409 arg = read_expr(s, table);
1410 ctx = isl_stream_get_ctx(s);
1411 if (!is_subtype(arg, isl_obj_union_map) &&
1412 !is_subtype(arg, isl_obj_union_set))
1413 isl_die(ctx, isl_error_invalid,
1414 "expecting set of map argument", goto error);
1416 if (arg.type == isl_obj_list) {
1417 struct isl_list *list = arg.v;
1418 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1419 arg = obj_at(arg, 0);
1421 if (arg.type == isl_obj_set)
1422 arg = convert(ctx, arg, isl_obj_union_set);
1423 else if (arg.type == isl_obj_map)
1424 arg = convert(ctx, arg, isl_obj_union_map);
1425 if (arg.type == isl_obj_union_set)
1426 obj = apply_fun_set(obj, arg.v);
1427 else
1428 obj = apply_fun_map(obj, arg.v);
1429 if (!obj.v)
1430 goto error2;
1432 if (isl_stream_eat(s, ')'))
1433 goto error2;
1435 return obj;
1436 error:
1437 free_obj(arg);
1438 error2:
1439 free_obj(obj);
1440 obj.type = isl_obj_none;
1441 obj.v = NULL;
1442 return obj;
1445 struct add_vertex_data {
1446 struct isl_list *list;
1447 int i;
1450 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1452 struct add_vertex_data *data = (struct add_vertex_data *)user;
1453 isl_multi_aff *ma;
1454 isl_set *dom;
1456 ma = isl_vertex_get_expr(vertex);
1457 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1459 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1460 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1461 data->i++;
1463 isl_vertex_free(vertex);
1465 return isl_stat_ok;
1468 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1470 isl_ctx *ctx;
1471 isl_basic_set *hull;
1472 isl_vertices *vertices = NULL;
1473 struct isl_list *list = NULL;
1474 isl_stat r;
1475 struct add_vertex_data *data = (struct add_vertex_data *)user;
1477 set = isl_set_remove_divs(set);
1478 hull = isl_set_convex_hull(set);
1479 vertices = isl_basic_set_compute_vertices(hull);
1480 isl_basic_set_free(hull);
1482 list = data->list;
1484 ctx = isl_vertices_get_ctx(vertices);
1485 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1486 if (!data->list)
1487 goto error;
1489 data->i = 0;
1490 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1492 data->list = isl_list_concat(list, data->list);
1494 isl_vertices_free(vertices);
1496 return r;
1497 error:
1498 data->list = list;
1499 isl_vertices_free(vertices);
1500 return isl_stat_error;
1503 static struct isl_obj vertices(struct isl_stream *s,
1504 struct isl_hash_table *table)
1506 isl_ctx *ctx;
1507 struct isl_obj obj;
1508 struct isl_list *list = NULL;
1509 isl_union_set *uset = NULL;
1510 struct add_vertex_data data = { NULL };
1512 obj = read_expr(s, table);
1513 ctx = isl_stream_get_ctx(s);
1514 obj = convert(ctx, obj, isl_obj_union_set);
1515 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1516 uset = obj.v;
1517 obj.v = NULL;
1519 list = isl_list_alloc(ctx, 0);
1520 if (!list)
1521 goto error;
1523 data.list = list;
1525 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1526 goto error;
1528 isl_union_set_free(uset);
1530 obj.type = isl_obj_list;
1531 obj.v = data.list;
1533 return obj;
1534 error:
1535 isl_union_set_free(uset);
1536 isl_list_free(data.list);
1537 free_obj(obj);
1538 obj.type = isl_obj_none;
1539 obj.v = NULL;
1540 return obj;
1543 static struct isl_obj type_of(struct isl_stream *s,
1544 struct isl_hash_table *table)
1546 isl_ctx *ctx;
1547 struct isl_obj obj;
1548 const char *type = "unknown";
1550 obj = read_expr(s, table);
1552 if (obj.type == isl_obj_map ||
1553 obj.type == isl_obj_union_map)
1554 type = "map";
1555 if (obj.type == isl_obj_set ||
1556 obj.type == isl_obj_union_set)
1557 type = "set";
1558 if (obj.type == isl_obj_pw_multi_aff)
1559 type = "piecewise multi-quasiaffine expression";
1560 if (obj.type == isl_obj_pw_qpolynomial ||
1561 obj.type == isl_obj_union_pw_qpolynomial)
1562 type = "piecewise quasipolynomial";
1563 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1564 obj.type == isl_obj_union_pw_qpolynomial_fold)
1565 type = "piecewise quasipolynomial fold";
1566 if (obj.type == isl_obj_list)
1567 type = "list";
1568 if (obj.type == isl_obj_bool)
1569 type = "boolean";
1570 if (obj.type == isl_obj_str)
1571 type = "string";
1572 if (obj.type == isl_obj_val)
1573 type = "value";
1574 if (obj.type == isl_obj_schedule)
1575 type = "schedule";
1577 free_obj(obj);
1578 obj.type = isl_obj_str;
1579 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1581 return obj;
1584 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1585 struct isl_hash_table *table)
1587 struct isl_obj obj;
1588 isl_ctx *ctx;
1590 obj = read_obj(s, table);
1591 ctx = isl_stream_get_ctx(s);
1592 obj = convert(ctx, obj, isl_obj_union_set);
1593 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1594 return obj.v;
1595 error:
1596 free_obj(obj);
1597 return NULL;
1600 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1601 struct isl_hash_table *table)
1603 struct isl_obj obj;
1604 isl_ctx *ctx;
1606 obj = read_obj(s, table);
1607 ctx = isl_stream_get_ctx(s);
1608 obj = convert(ctx, obj, isl_obj_union_map);
1609 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1610 return obj.v;
1611 error:
1612 free_obj(obj);
1613 return NULL;
1616 /* Read a schedule in the form of either a schedule (tree) or a union map
1617 * from "s" and store the schedule in "access".
1619 static __isl_give isl_union_access_info *access_info_set_schedule(
1620 __isl_take isl_union_access_info *access, struct isl_stream *s,
1621 struct isl_hash_table *table)
1623 struct isl_obj obj;
1624 isl_ctx *ctx;
1626 obj = read_obj(s, table);
1627 if (obj.type == isl_obj_schedule)
1628 return isl_union_access_info_set_schedule(access, obj.v);
1629 ctx = isl_stream_get_ctx(s);
1630 obj = convert(ctx, obj, isl_obj_union_map);
1632 return isl_union_access_info_set_schedule_map(access, obj.v);
1635 static struct isl_obj last_any(struct isl_stream *s,
1636 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1637 __isl_take isl_union_map *may_source)
1639 struct isl_obj obj = { isl_obj_none, NULL };
1640 isl_union_access_info *access;
1641 isl_union_flow *flow;
1642 isl_union_map *sink = NULL;
1643 isl_union_map *may_dep;
1645 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1646 goto error;
1648 sink = read_map(s, table);
1649 if (!sink)
1650 goto error;
1652 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1653 goto error;
1655 access = isl_union_access_info_from_sink(sink);
1656 access = isl_union_access_info_set_must_source(access, must_source);
1657 access = isl_union_access_info_set_may_source(access, may_source);
1658 access = access_info_set_schedule(access, s, table);
1659 flow = isl_union_access_info_compute_flow(access);
1660 may_dep = isl_union_flow_get_may_dependence(flow);
1661 isl_union_flow_free(flow);
1663 if (!may_dep)
1664 return obj;
1666 obj.type = isl_obj_union_map;
1667 obj.v = may_dep;
1669 return obj;
1670 error:
1671 isl_union_map_free(may_source);
1672 isl_union_map_free(must_source);
1673 isl_union_map_free(sink);
1674 free_obj(obj);
1675 obj.type = isl_obj_none;
1676 obj.v = NULL;
1677 return obj;
1680 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1682 struct isl_obj obj = { isl_obj_none, NULL };
1683 isl_union_access_info *access;
1684 isl_union_flow *flow;
1685 isl_union_map *may_source = NULL;
1686 isl_union_map *sink = NULL;
1687 isl_union_map *may_dep;
1689 may_source = read_map(s, table);
1690 if (!may_source)
1691 goto error;
1693 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1694 isl_union_map *must_source;
1695 must_source = read_map(s, table);
1696 if (!must_source)
1697 goto error;
1698 return last_any(s, table, must_source, may_source);
1701 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1702 goto error;
1704 sink = read_map(s, table);
1705 if (!sink)
1706 goto error;
1708 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1709 goto error;
1711 access = isl_union_access_info_from_sink(sink);
1712 access = isl_union_access_info_set_may_source(access, may_source);
1713 access = access_info_set_schedule(access, s, table);
1714 flow = isl_union_access_info_compute_flow(access);
1715 may_dep = isl_union_flow_get_may_dependence(flow);
1716 isl_union_flow_free(flow);
1718 if (!may_dep)
1719 return obj;
1721 obj.type = isl_obj_union_map;
1722 obj.v = may_dep;
1724 return obj;
1725 error:
1726 isl_union_map_free(may_source);
1727 isl_union_map_free(sink);
1728 free_obj(obj);
1729 obj.type = isl_obj_none;
1730 obj.v = NULL;
1731 return obj;
1734 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1736 struct isl_obj obj = { isl_obj_none, NULL };
1737 struct isl_list *list = NULL;
1738 isl_union_access_info *access;
1739 isl_union_flow *flow;
1740 isl_union_map *must_source = NULL;
1741 isl_union_map *sink = NULL;
1742 isl_union_map *must_dep;
1743 isl_union_map *must_no_source;
1745 must_source = read_map(s, table);
1746 if (!must_source)
1747 goto error;
1749 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1750 isl_union_map *may_source;
1751 may_source = read_map(s, table);
1752 if (!may_source)
1753 goto error;
1754 return last_any(s, table, must_source, may_source);
1757 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1758 if (!list)
1759 goto error;
1761 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1762 goto error;
1764 sink = read_map(s, table);
1765 if (!sink)
1766 goto error;
1768 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1769 goto error;
1771 access = isl_union_access_info_from_sink(sink);
1772 access = isl_union_access_info_set_must_source(access, must_source);
1773 access = access_info_set_schedule(access, s, table);
1774 flow = isl_union_access_info_compute_flow(access);
1775 must_dep = isl_union_flow_get_must_dependence(flow);
1776 must_no_source = isl_union_flow_get_must_no_source(flow);
1777 isl_union_flow_free(flow);
1779 list->obj[0].type = isl_obj_union_map;
1780 list->obj[0].v = must_dep;
1781 list->obj[1].type = isl_obj_union_map;
1782 list->obj[1].v = must_no_source;
1784 if (!must_dep || !must_no_source) {
1785 isl_list_free(list);
1786 return obj;
1789 obj.v = list;
1790 obj.type = isl_obj_list;
1792 return obj;
1793 error:
1794 isl_list_free(list);
1795 isl_union_map_free(must_source);
1796 isl_union_map_free(sink);
1797 free_obj(obj);
1798 obj.type = isl_obj_none;
1799 obj.v = NULL;
1800 return obj;
1803 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1804 struct isl_hash_table *table)
1806 isl_union_set *domain;
1807 isl_union_map *validity;
1808 isl_union_map *proximity;
1810 domain = read_set(s, table);
1811 if (!domain)
1812 return NULL;
1814 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1815 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1817 for (;;) {
1818 isl_union_map *umap;
1819 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1820 umap = read_map(s, table);
1821 validity = isl_union_map_union(validity, umap);
1822 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1823 umap = read_map(s, table);
1824 proximity = isl_union_map_union(proximity, umap);
1825 } else
1826 break;
1829 return isl_union_set_compute_schedule(domain, validity, proximity);
1832 static struct isl_obj schedule(struct isl_stream *s,
1833 struct isl_hash_table *table)
1835 struct isl_obj obj = { isl_obj_none, NULL };
1836 isl_schedule *schedule;
1838 schedule = get_schedule(s, table);
1840 obj.v = isl_schedule_get_map(schedule);
1841 obj.type = isl_obj_union_map;
1843 isl_schedule_free(schedule);
1845 return obj;
1848 /* Read a schedule for code generation in the form of either
1849 * a schedule tree or a union map.
1850 * If the input is a set rather than a map, then we construct
1851 * an identity union map schedule on the given set.
1853 static struct isl_obj get_codegen_schedule(struct isl_stream *s,
1854 struct isl_hash_table *table)
1856 struct isl_obj obj;
1857 isl_ctx *ctx;
1859 obj = read_obj(s, table);
1860 ctx = isl_stream_get_ctx(s);
1862 if (obj.type == isl_obj_schedule)
1863 return obj;
1864 if (is_subtype(obj, isl_obj_union_set)) {
1865 obj = convert(ctx, obj, isl_obj_union_set);
1866 obj.v = isl_union_set_identity(obj.v);
1867 obj.type = isl_obj_union_map;
1869 if (is_subtype(obj, isl_obj_union_map))
1870 return convert(ctx, obj, isl_obj_union_map);
1872 free_obj(obj);
1873 obj.v = NULL;
1874 obj.type = isl_obj_none;
1875 isl_die(ctx, isl_error_invalid, "expecting schedule, set or map",
1876 return obj);
1879 /* Generate an AST for the given schedule and options and return the AST.
1881 static __isl_give isl_ast_node *get_ast_from_union_map(
1882 __isl_take isl_union_map *schedule, __isl_take isl_union_map *options)
1884 isl_space *space;
1885 isl_set *context;
1886 isl_ast_build *build;
1887 isl_ast_node *tree;
1889 space = isl_union_map_get_space(schedule);
1890 context = isl_set_universe(isl_space_params(space));
1892 build = isl_ast_build_from_context(context);
1893 build = isl_ast_build_set_options(build, options);
1894 tree = isl_ast_build_ast_from_schedule(build, schedule);
1895 isl_ast_build_free(build);
1897 return tree;
1900 /* Generate an AST for the given schedule and return the AST.
1902 static __isl_give isl_ast_node *get_ast_from_schedule(
1903 __isl_take isl_schedule *schedule)
1905 isl_ast_build *build;
1906 isl_ast_node *tree;
1908 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
1909 tree = isl_ast_build_node_from_schedule(build, schedule);
1910 isl_ast_build_free(build);
1912 return tree;
1915 /* Print the AST "tree" on the printer "p".
1917 static __isl_give isl_printer *print_ast(__isl_take isl_printer *p,
1918 __isl_take isl_ast_node *tree)
1920 int format;
1922 format = isl_printer_get_output_format(p);
1923 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1924 p = isl_printer_print_ast_node(p, tree);
1925 p = isl_printer_set_output_format(p, format);
1927 isl_ast_node_free(tree);
1929 return p;
1932 /* Perform the codegen operation.
1933 * In particular, read a schedule, check if the user has specified any options
1934 * and then generate an AST from the schedule (and options) and print it.
1935 * In case the schedule is specified as a schedule tree, the AST generation
1936 * options are embedded in the schedule, so they are not read in separately.
1938 static __isl_give isl_printer *codegen(struct isl_stream *s,
1939 struct isl_hash_table *table, __isl_take isl_printer *p)
1941 struct isl_obj obj;
1942 isl_ast_node *tree;
1944 obj = get_codegen_schedule(s, table);
1945 if (!obj.v)
1946 return p;
1948 if (obj.type == isl_obj_schedule) {
1949 isl_schedule *schedule = obj.v;
1951 tree = get_ast_from_schedule(schedule);
1952 } else {
1953 isl_union_map *schedule = obj.v;
1954 isl_union_map *options;
1956 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1957 options = read_map(s, table);
1958 else
1959 options = isl_union_map_empty(
1960 isl_union_map_get_space(schedule));
1962 tree = get_ast_from_union_map(schedule, options);
1965 p = print_ast(p, tree);
1967 isl_stream_eat(s, ';');
1969 return p;
1972 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
1974 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
1976 struct isl_obj obj = { isl_obj_none, NULL };
1977 isl_ctx *ctx = isl_band_get_ctx(band);
1978 struct isl_list *list;
1980 list = isl_list_alloc(ctx, 2);
1981 if (!list)
1982 goto error;
1984 obj.v = list;
1985 obj.type = isl_obj_list;
1987 list->obj[0].type = isl_obj_union_map;
1988 list->obj[0].v = isl_band_get_partial_schedule(band);
1990 if (isl_band_has_children(band)) {
1991 isl_band_list *children;
1993 children = isl_band_get_children(band);
1994 list->obj[1] = band_list_to_obj_list(children);
1995 } else {
1996 list->obj[1].type = isl_obj_list;
1997 list->obj[1].v = isl_list_alloc(ctx, 0);
2000 if (!list->obj[0].v || !list->obj[1].v)
2001 goto error;
2003 isl_band_free(band);
2005 return obj;
2006 error:
2007 isl_band_free(band);
2008 free_obj(obj);
2009 obj.type = isl_obj_none;
2010 obj.v = NULL;
2011 return obj;
2014 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
2016 struct isl_obj obj = { isl_obj_none, NULL };
2017 isl_ctx *ctx = isl_band_list_get_ctx(bands);
2018 struct isl_list *list;
2019 int i, n;
2021 n = isl_band_list_n_band(bands);
2022 list = isl_list_alloc(ctx, n);
2023 if (!list)
2024 goto error;
2026 obj.v = list;
2027 obj.type = isl_obj_list;
2029 for (i = 0; i < n; ++i) {
2030 isl_band *band;
2032 band = isl_band_list_get_band(bands, i);
2033 list->obj[i] = band_to_obj_list(band);
2034 if (!list->obj[i].v)
2035 goto error;
2038 isl_band_list_free(bands);
2040 return obj;
2041 error:
2042 isl_band_list_free(bands);
2043 free_obj(obj);
2044 obj.type = isl_obj_none;
2045 obj.v = NULL;
2046 return obj;
2049 static struct isl_obj schedule_forest(struct isl_stream *s,
2050 struct isl_hash_table *table)
2052 struct isl_obj obj = { isl_obj_none, NULL };
2053 isl_schedule *schedule;
2054 isl_band_list *roots;
2056 schedule = get_schedule(s, table);
2057 if (!schedule)
2058 return obj;
2060 roots = isl_schedule_get_band_forest(schedule);
2061 isl_schedule_free(schedule);
2063 return band_list_to_obj_list(roots);
2066 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2068 struct isl_token *tok;
2069 isl_ctx *ctx;
2070 isl_val *v;
2072 ctx = isl_stream_get_ctx(s);
2073 if (isl_stream_eat_if_available(s, '+'))
2074 return transitive_closure(ctx, obj);
2076 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2077 if (obj.type != isl_obj_union_map)
2078 obj = convert(ctx, obj, isl_obj_union_map);
2080 tok = isl_stream_next_token(s);
2081 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2082 isl_stream_error(s, tok, "expecting integer exponent");
2083 if (tok)
2084 isl_stream_push_token(s, tok);
2085 goto error;
2088 v = isl_token_get_val(ctx, tok);
2089 if (isl_val_is_zero(v)) {
2090 isl_stream_error(s, tok, "expecting non-zero exponent");
2091 isl_val_free(v);
2092 if (tok)
2093 isl_stream_push_token(s, tok);
2094 goto error;
2097 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2098 isl_token_free(tok);
2099 if (!obj.v)
2100 goto error;
2102 return obj;
2103 error:
2104 free_obj(obj);
2105 obj.type = isl_obj_none;
2106 obj.v = NULL;
2107 return obj;
2110 static struct isl_obj check_assert(struct isl_stream *s,
2111 struct isl_hash_table *table)
2113 struct isl_obj obj;
2114 isl_ctx *ctx;
2116 obj = read_expr(s, table);
2117 ctx = isl_stream_get_ctx(s);
2118 if (obj.type != isl_obj_bool)
2119 isl_die(ctx, isl_error_invalid,
2120 "expecting boolean expression", goto error);
2121 if (obj.v != &iscc_bool_true)
2122 isl_die(ctx, isl_error_unknown,
2123 "assertion failed", abort());
2124 error:
2125 free_obj(obj);
2126 obj.type = isl_obj_none;
2127 obj.v = NULL;
2128 return obj;
2131 static struct isl_obj read_from_file(struct isl_stream *s)
2133 isl_ctx *ctx;
2134 struct isl_obj obj;
2135 struct isl_token *tok;
2136 struct isl_stream *s_file;
2137 struct iscc_options *options;
2138 char *name;
2139 FILE *file;
2141 tok = isl_stream_next_token(s);
2142 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2143 isl_stream_error(s, tok, "expecting filename");
2144 isl_token_free(tok);
2145 goto error;
2148 ctx = isl_stream_get_ctx(s);
2149 options = isl_ctx_peek_iscc_options(ctx);
2150 if (!options || !options->io) {
2151 isl_token_free(tok);
2152 isl_die(ctx, isl_error_invalid,
2153 "read operation not allowed", goto error);
2156 name = isl_token_get_str(ctx, tok);
2157 isl_token_free(tok);
2158 file = fopen(name, "r");
2159 free(name);
2160 isl_assert(ctx, file, goto error);
2162 s_file = isl_stream_new_file(ctx, file);
2163 if (!s_file) {
2164 fclose(file);
2165 goto error;
2168 obj = isl_stream_read_obj(s_file);
2170 isl_stream_free(s_file);
2171 fclose(file);
2173 return obj;
2174 error:
2175 obj.type = isl_obj_none;
2176 obj.v = NULL;
2177 return obj;
2180 static struct isl_obj write_to_file(struct isl_stream *s,
2181 struct isl_hash_table *table)
2183 struct isl_obj obj;
2184 struct isl_token *tok;
2185 struct isl_stream *s_file;
2186 struct iscc_options *options;
2187 char *name;
2188 FILE *file;
2189 isl_ctx *ctx;
2190 isl_printer *p;
2192 tok = isl_stream_next_token(s);
2193 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2194 isl_stream_error(s, tok, "expecting filename");
2195 isl_token_free(tok);
2196 goto error;
2199 obj = read_expr(s, table);
2201 ctx = isl_stream_get_ctx(s);
2202 options = isl_ctx_peek_iscc_options(ctx);
2203 if (!options || !options->io) {
2204 isl_token_free(tok);
2205 isl_die(ctx, isl_error_invalid,
2206 "write operation not allowed", goto error);
2209 name = isl_token_get_str(ctx, tok);
2210 isl_token_free(tok);
2211 file = fopen(name, "w");
2212 free(name);
2213 if (!file)
2214 isl_die(ctx, isl_error_unknown,
2215 "could not open file for writing", goto error);
2217 p = isl_printer_to_file(ctx, file);
2218 p = isl_printer_set_output_format(p, options->format);
2219 p = obj.type->print(p, obj.v);
2220 p = isl_printer_end_line(p);
2221 isl_printer_free(p);
2223 fclose(file);
2224 error:
2225 free_obj(obj);
2226 obj.type = isl_obj_none;
2227 obj.v = NULL;
2228 return obj;
2231 static struct isl_obj read_string_if_available(struct isl_stream *s)
2233 struct isl_token *tok;
2234 struct isl_obj obj = { isl_obj_none, NULL };
2236 tok = isl_stream_next_token(s);
2237 if (!tok)
2238 return obj;
2239 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2240 isl_str *str;
2241 str = isl_str_alloc(isl_stream_get_ctx(s));
2242 if (!str)
2243 goto error;
2244 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2245 isl_token_free(tok);
2246 obj.v = str;
2247 obj.type = isl_obj_str;
2248 } else
2249 isl_stream_push_token(s, tok);
2250 return obj;
2251 error:
2252 isl_token_free(tok);
2253 return obj;
2256 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2258 struct isl_token *tok;
2259 struct isl_obj obj = { isl_obj_none, NULL };
2260 int type;
2262 tok = isl_stream_next_token(s);
2263 if (!tok)
2264 return obj;
2265 type = isl_token_get_type(tok);
2266 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2267 int is_true = type == ISL_TOKEN_TRUE;
2268 isl_token_free(tok);
2269 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2270 obj.type = isl_obj_bool;
2271 } else
2272 isl_stream_push_token(s, tok);
2273 return obj;
2276 static __isl_give char *read_ident(struct isl_stream *s)
2278 char *name;
2279 isl_val *v;
2280 struct isl_token *tok, *tok2;
2282 name = isl_stream_read_ident_if_available(s);
2283 if (name)
2284 return name;
2286 tok = isl_stream_next_token(s);
2287 if (!tok)
2288 return NULL;
2289 if (isl_token_get_type(tok) != '$') {
2290 isl_stream_push_token(s, tok);
2291 return NULL;
2293 tok2 = isl_stream_next_token(s);
2294 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2295 if (tok2)
2296 isl_stream_push_token(s, tok2);
2297 isl_stream_push_token(s, tok);
2298 return NULL;
2301 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2302 name = isl_val_to_str(v);
2303 isl_val_free(v);
2304 isl_token_free(tok);
2305 isl_token_free(tok2);
2307 return name;
2310 static struct isl_obj read_list(struct isl_stream *s,
2311 struct isl_hash_table *table, struct isl_obj obj)
2313 struct isl_list *list;
2315 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2316 if (!list)
2317 goto error;
2318 list->obj[0] = obj;
2319 list->obj[1] = read_obj(s, table);
2320 obj.v = list;
2321 obj.type = isl_obj_list;
2323 if (!list->obj[1].v)
2324 goto error;
2326 while (isl_stream_eat_if_available(s, ',')) {
2327 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2328 if (!obj.v)
2329 goto error;
2332 return obj;
2333 error:
2334 free_obj(obj);
2335 obj.type = isl_obj_none;
2336 obj.v = NULL;
2337 return obj;
2340 static struct isl_obj read_obj(struct isl_stream *s,
2341 struct isl_hash_table *table)
2343 isl_ctx *ctx;
2344 struct isl_obj obj = { isl_obj_none, NULL };
2345 char *name = NULL;
2346 struct isc_un_op *op = NULL;
2348 obj = read_string_if_available(s);
2349 if (obj.v)
2350 return obj;
2351 obj = read_bool_if_available(s);
2352 if (obj.v)
2353 return obj;
2354 ctx = isl_stream_get_ctx(s);
2355 if (isl_stream_eat_if_available(s, '(')) {
2356 if (isl_stream_next_token_is(s, ')')) {
2357 obj.type = isl_obj_list;
2358 obj.v = isl_list_alloc(ctx, 0);
2359 } else {
2360 obj = read_expr(s, table);
2361 if (obj.v && isl_stream_eat_if_available(s, ','))
2362 obj = read_list(s, table, obj);
2364 if (!obj.v || isl_stream_eat(s, ')'))
2365 goto error;
2366 } else {
2367 op = read_prefix_un_op_if_available(s);
2368 if (op)
2369 return read_un_op_expr(s, table, op);
2371 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2372 return check_assert(s, table);
2373 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2374 return read_from_file(s);
2375 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2376 return write_to_file(s, table);
2377 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2378 return vertices(s, table);
2379 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2380 return any(s, table);
2381 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2382 return last(s, table);
2383 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2384 return schedule(s, table);
2385 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2386 return schedule_forest(s, table);
2387 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2388 return type_of(s, table);
2390 name = read_ident(s);
2391 if (name)
2392 obj = stored_obj(ctx, table, name);
2393 else
2394 obj = isl_stream_read_obj(s);
2395 if (!obj.v)
2396 goto error;
2399 if (isl_stream_eat_if_available(s, '^'))
2400 obj = power(s, obj);
2401 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2402 obj = obj_at_index(s, obj);
2403 else if (is_subtype(obj, isl_obj_union_map) &&
2404 isl_stream_eat_if_available(s, '(')) {
2405 obj = convert(ctx, obj, isl_obj_union_map);
2406 obj = apply(s, obj.v, table);
2407 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2408 isl_stream_eat_if_available(s, '(')) {
2409 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2410 obj = apply_fun(s, obj, table);
2411 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2412 isl_stream_eat_if_available(s, '(')) {
2413 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2414 obj = apply_fun(s, obj, table);
2417 return obj;
2418 error:
2419 free_obj(obj);
2420 obj.type = isl_obj_none;
2421 obj.v = NULL;
2422 return obj;
2425 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2426 struct isl_obj lhs, struct isl_obj rhs)
2428 int i;
2430 for (i = 0; ; ++i) {
2431 if (!bin_ops[i].op)
2432 break;
2433 if (bin_ops[i].op != like->op)
2434 continue;
2435 if (!is_subtype(lhs, bin_ops[i].lhs))
2436 continue;
2437 if (!is_subtype(rhs, bin_ops[i].rhs))
2438 continue;
2440 return &bin_ops[i];
2443 for (i = 0; ; ++i) {
2444 if (!named_bin_ops[i].name)
2445 break;
2446 if (named_bin_ops[i].op.op != like->op)
2447 continue;
2448 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2449 continue;
2450 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2451 continue;
2453 return &named_bin_ops[i].op;
2456 return NULL;
2459 static int next_is_neg_int(struct isl_stream *s)
2461 struct isl_token *tok;
2462 int ret;
2464 tok = isl_stream_next_token(s);
2465 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2466 isl_val *v;
2467 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2468 ret = isl_val_is_neg(v);
2469 isl_val_free(v);
2470 } else
2471 ret = 0;
2472 isl_stream_push_token(s, tok);
2474 return ret;
2477 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2478 struct isl_obj lhs, struct isl_obj rhs)
2480 struct isl_obj obj;
2482 lhs = convert(ctx, lhs, op->lhs);
2483 rhs = convert(ctx, rhs, op->rhs);
2484 if (op->res != isl_obj_bool)
2485 obj.v = op->o.fn(lhs.v, rhs.v);
2486 else {
2487 int res = op->o.test(lhs.v, rhs.v);
2488 free_obj(lhs);
2489 free_obj(rhs);
2490 obj.v = iscc_bool_from_int(res);
2492 obj.type = op->res;
2494 return obj;
2497 static struct isl_obj read_expr(struct isl_stream *s,
2498 struct isl_hash_table *table)
2500 isl_ctx *ctx;
2501 struct isl_obj obj = { isl_obj_none, NULL };
2502 struct isl_obj right_obj = { isl_obj_none, NULL };
2504 obj = read_obj(s, table);
2505 ctx = isl_stream_get_ctx(s);
2506 for (; obj.v;) {
2507 struct isc_bin_op *op = NULL;
2509 op = read_bin_op_if_available(s, obj);
2510 if (!op)
2511 break;
2513 right_obj = read_obj(s, table);
2515 op = find_matching_bin_op(op, obj, right_obj);
2517 if (!op)
2518 isl_die(ctx, isl_error_invalid,
2519 "no such binary operator defined on given operands",
2520 goto error);
2522 obj = call_bin_op(ctx, op, obj, right_obj);
2525 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2526 right_obj = read_obj(s, table);
2527 obj.v = isl_val_add(obj.v, right_obj.v);
2530 return obj;
2531 error:
2532 free_obj(right_obj);
2533 free_obj(obj);
2534 obj.type = isl_obj_none;
2535 obj.v = NULL;
2536 return obj;
2539 static __isl_give isl_printer *source_file(struct isl_stream *s,
2540 struct isl_hash_table *table, __isl_take isl_printer *p);
2542 static __isl_give isl_printer *read_line(struct isl_stream *s,
2543 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2545 isl_ctx *ctx;
2546 struct isl_obj obj = { isl_obj_none, NULL };
2547 char *lhs = NULL;
2548 int assign = 0;
2549 int only_print = 0;
2550 struct isc_bin_op *op = NULL;
2551 char buf[30];
2553 if (!p)
2554 return NULL;
2555 if (isl_stream_is_empty(s))
2556 return p;
2558 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2559 return source_file(s, table, p);
2560 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2561 return codegen(s, table, p);
2563 assign = is_assign(s);
2564 if (assign) {
2565 lhs = isl_stream_read_ident_if_available(s);
2566 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2567 goto error;
2568 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2569 only_print = 1;
2570 else if (!tty)
2571 only_print = 1;
2573 obj = read_expr(s, table);
2574 ctx = isl_stream_get_ctx(s);
2575 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2576 fprintf(stderr, "Interrupted\n");
2577 isl_ctx_reset_error(ctx);
2579 if (isl_stream_eat(s, ';'))
2580 goto error;
2582 if (only_print) {
2583 if (obj.type != isl_obj_none && obj.v != NULL) {
2584 p = obj.type->print(p, obj.v);
2585 p = isl_printer_end_line(p);
2587 free_obj(obj);
2588 return p;
2590 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2591 static int count = 0;
2592 snprintf(buf, sizeof(buf), "$%d", count++);
2593 lhs = strdup(buf + 1);
2595 p = isl_printer_print_str(p, buf);
2596 p = isl_printer_print_str(p, " := ");
2597 p = obj.type->print(p, obj.v);
2598 p = isl_printer_end_line(p);
2600 if (lhs && do_assign(ctx, table, lhs, obj))
2601 return p;
2603 return p;
2604 error:
2605 isl_stream_flush_tokens(s);
2606 isl_stream_skip_line(s);
2607 free(lhs);
2608 free_obj(obj);
2609 return p;
2612 static isl_stat free_cb(void **entry, void *user)
2614 struct isl_named_obj *named = *entry;
2616 free_obj(named->obj);
2617 free(named->name);
2618 free(named);
2620 return isl_stat_ok;
2623 static void register_named_ops(struct isl_stream *s)
2625 int i;
2627 for (i = 0; i < ISCC_N_OP; ++i) {
2628 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2629 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2632 for (i = 0; ; ++i) {
2633 if (!named_un_ops[i].name)
2634 break;
2635 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2636 named_un_ops[i].name);
2637 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2640 for (i = 0; ; ++i) {
2641 if (!named_bin_ops[i].name)
2642 break;
2643 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2644 named_bin_ops[i].name);
2645 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2649 static __isl_give isl_printer *source_file(struct isl_stream *s,
2650 struct isl_hash_table *table, __isl_take isl_printer *p)
2652 isl_ctx *ctx;
2653 struct isl_token *tok;
2654 struct isl_stream *s_file;
2655 struct iscc_options *options;
2656 char *name;
2657 FILE *file;
2659 tok = isl_stream_next_token(s);
2660 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2661 isl_stream_error(s, tok, "expecting filename");
2662 isl_token_free(tok);
2663 return p;
2666 isl_stream_eat(s, ';');
2668 ctx = isl_stream_get_ctx(s);
2669 options = isl_ctx_peek_iscc_options(ctx);
2670 if (!options || !options->io) {
2671 isl_token_free(tok);
2672 isl_die(ctx, isl_error_invalid,
2673 "source operation not allowed", return p);
2676 name = isl_token_get_str(ctx, tok);
2677 isl_token_free(tok);
2678 file = fopen(name, "r");
2679 free(name);
2680 isl_assert(ctx, file, return p);
2682 s_file = isl_stream_new_file(ctx, file);
2683 if (!s_file) {
2684 fclose(file);
2685 return p;
2688 register_named_ops(s_file);
2690 while (!isl_stream_is_empty(s_file))
2691 p = read_line(s_file, table, p, 0);
2693 isl_stream_free(s_file);
2694 fclose(file);
2696 return p;
2699 int main(int argc, char **argv)
2701 struct isl_ctx *ctx;
2702 struct isl_stream *s;
2703 struct isl_hash_table *table;
2704 struct iscc_options *options;
2705 isl_printer *p;
2706 int tty = isatty(0);
2708 options = iscc_options_new_with_defaults();
2709 assert(options);
2711 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2712 pet_options_set_autodetect(ctx, 1);
2713 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2714 s = isl_stream_new_file(ctx, stdin);
2715 assert(s);
2716 table = isl_hash_table_alloc(ctx, 10);
2717 assert(table);
2718 p = isl_printer_to_file(ctx, stdout);
2719 p = isl_printer_set_output_format(p, options->format);
2720 assert(p);
2722 register_named_ops(s);
2724 install_signal_handler(ctx);
2726 while (p && !isl_stream_is_empty(s)) {
2727 isl_ctx_resume(ctx);
2728 p = read_line(s, table, p, tty);
2731 remove_signal_handler(ctx);
2733 isl_printer_free(p);
2734 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2735 isl_hash_table_free(ctx, table);
2736 isl_stream_free(s);
2737 isl_ctx_free(ctx);
2739 return 0;