iscc: schedule: return schedule in schedule tree representation
[barvinok.git] / iscc.c
blob4adf04ad50b11afc7edca1509c758f73413fb2d6
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 /* Return the domain of "schedule".
809 static __isl_give isl_union_set *schedule_domain(
810 __isl_take isl_schedule *schedule)
812 isl_union_set *domain;
814 domain = isl_schedule_get_domain(schedule);
815 isl_schedule_free(schedule);
817 return domain;
820 /* Convert "schedule" to a union map representation.
822 static __isl_give isl_union_map *schedule_map(__isl_take isl_schedule *schedule)
824 isl_union_map *map;
826 map = isl_schedule_get_map(schedule);
827 isl_schedule_free(schedule);
829 return map;
832 typedef void *(*isc_un_op_fn)(void *arg);
833 struct isc_un_op {
834 enum isl_token_type op;
835 isl_obj_type arg;
836 isl_obj_type res;
837 isc_un_op_fn fn;
839 struct isc_named_un_op {
840 char *name;
841 struct isc_un_op op;
843 struct isc_named_un_op named_un_ops[] = {
844 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
845 (isc_un_op_fn) &isl_union_map_affine_hull } },
846 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
847 (isc_un_op_fn) &isl_union_set_affine_hull } },
848 {"card", { -1, isl_obj_union_set,
849 isl_obj_union_pw_qpolynomial,
850 (isc_un_op_fn) &isl_union_set_card } },
851 {"card", { -1, isl_obj_union_map,
852 isl_obj_union_pw_qpolynomial,
853 (isc_un_op_fn) &isl_union_map_card } },
854 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
855 (isc_un_op_fn) &isl_union_set_coalesce } },
856 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
857 (isc_un_op_fn) &isl_union_map_coalesce } },
858 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
859 isl_obj_union_pw_qpolynomial,
860 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
861 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
862 isl_obj_union_pw_qpolynomial_fold,
863 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
864 {"coefficients", { -1, isl_obj_union_set,
865 isl_obj_union_set,
866 (isc_un_op_fn) &isl_union_set_coefficients } },
867 {"solutions", { -1, isl_obj_union_set, isl_obj_union_set,
868 (isc_un_op_fn) &isl_union_set_solutions } },
869 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
870 (isc_un_op_fn) &isl_union_map_deltas } },
871 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
872 (isc_un_op_fn) &isl_union_map_deltas_map } },
873 {"dom", { -1, isl_obj_schedule, isl_obj_union_set,
874 (isc_un_op_fn) &schedule_domain } },
875 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
876 (isc_un_op_fn) &isl_union_map_domain } },
877 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
878 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
879 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
880 isl_obj_union_set,
881 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
882 {"domain", { -1, isl_obj_schedule, isl_obj_union_set,
883 (isc_un_op_fn) &schedule_domain } },
884 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
885 (isc_un_op_fn) &isl_union_map_domain } },
886 {"domain", { -1, isl_obj_union_pw_qpolynomial,
887 isl_obj_union_set,
888 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
889 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
890 isl_obj_union_set,
891 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
892 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
893 (isc_un_op_fn) &isl_union_map_domain_map } },
894 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
895 (isc_un_op_fn) &isl_union_map_range } },
896 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
897 (isc_un_op_fn) &isl_union_map_range } },
898 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
899 (isc_un_op_fn) &isl_union_map_range_map } },
900 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
901 (isc_un_op_fn) &isl_union_set_identity } },
902 {"lattice_width", { -1, isl_obj_union_set,
903 isl_obj_union_pw_qpolynomial,
904 (isc_un_op_fn) &isl_union_set_lattice_width } },
905 {"lb", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
906 (isc_un_op_fn) &union_pw_qpolynomial_lower_bound } },
907 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
908 (isc_un_op_fn) &isl_union_map_lexmin } },
909 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
910 (isc_un_op_fn) &isl_union_map_lexmax } },
911 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
912 (isc_un_op_fn) &isl_union_set_lexmin } },
913 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
914 (isc_un_op_fn) &isl_union_set_lexmax } },
915 {"lift", { -1, isl_obj_union_set, isl_obj_union_set,
916 (isc_un_op_fn) &isl_union_set_lift } },
917 {"map", { -1, isl_obj_schedule, isl_obj_union_map,
918 (isc_un_op_fn) &schedule_map } },
919 {"params", { -1, isl_obj_union_map, isl_obj_set,
920 (isc_un_op_fn) &isl_union_map_params } },
921 {"params", { -1, isl_obj_union_set, isl_obj_set,
922 (isc_un_op_fn) &isl_union_set_params } },
923 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
924 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
925 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
926 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
927 {"poly", { -1, isl_obj_union_pw_qpolynomial,
928 isl_obj_union_pw_qpolynomial,
929 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
930 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
931 isl_obj_union_pw_qpolynomial,
932 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
933 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
934 isl_obj_union_pw_qpolynomial,
935 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
936 #ifdef HAVE_PET
937 {"parse_file", { -1, isl_obj_str, isl_obj_list,
938 (isc_un_op_fn) &parse } },
939 #endif
940 {"pow", { -1, isl_obj_union_map, isl_obj_list,
941 (isc_un_op_fn) &union_map_power } },
942 {"sample", { -1, isl_obj_union_set, isl_obj_set,
943 (isc_un_op_fn) &union_set_sample } },
944 {"sample", { -1, isl_obj_union_map, isl_obj_map,
945 (isc_un_op_fn) &union_map_sample } },
946 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
947 (isc_un_op_fn) &union_set_scan } },
948 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
949 (isc_un_op_fn) &union_map_scan } },
950 {"sum", { -1, isl_obj_union_pw_qpolynomial,
951 isl_obj_union_pw_qpolynomial,
952 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
953 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
954 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
955 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
956 (isc_un_op_fn) &isl_union_set_unwrap } },
957 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
958 (isc_un_op_fn) &isl_union_map_wrap } },
959 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
960 (isc_un_op_fn) &isl_union_map_zip } },
961 NULL
964 struct isl_named_obj {
965 char *name;
966 struct isl_obj obj;
969 static void free_obj(struct isl_obj obj)
971 obj.type->free(obj.v);
974 static int same_name(const void *entry, const void *val)
976 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
978 return !strcmp(named->name, val);
981 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
982 char *name, struct isl_obj obj)
984 struct isl_hash_table_entry *entry;
985 uint32_t name_hash;
986 struct isl_named_obj *named;
988 name_hash = isl_hash_string(isl_hash_init(), name);
989 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
990 if (!entry)
991 goto error;
992 if (entry->data) {
993 named = entry->data;
994 free_obj(named->obj);
995 free(name);
996 } else {
997 named = isl_alloc_type(ctx, struct isl_named_obj);
998 if (!named)
999 goto error;
1000 named->name = name;
1001 entry->data = named;
1003 named->obj = obj;
1005 return 0;
1006 error:
1007 free_obj(obj);
1008 free(name);
1009 return -1;
1012 static struct isl_obj stored_obj(struct isl_ctx *ctx,
1013 struct isl_hash_table *table, char *name)
1015 struct isl_obj obj = { isl_obj_none, NULL };
1016 struct isl_hash_table_entry *entry;
1017 uint32_t name_hash;
1019 name_hash = isl_hash_string(isl_hash_init(), name);
1020 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
1021 if (entry) {
1022 struct isl_named_obj *named;
1023 named = entry->data;
1024 obj = named->obj;
1025 } else if (isdigit(name[0]))
1026 fprintf(stderr, "unknown identifier '$%s'\n", name);
1027 else
1028 fprintf(stderr, "unknown identifier '%s'\n", name);
1030 free(name);
1031 obj.v = obj.type->copy(obj.v);
1032 return obj;
1035 static int is_subtype(struct isl_obj obj, isl_obj_type super)
1037 if (obj.type == super)
1038 return 1;
1039 if (obj.type == isl_obj_map && super == isl_obj_union_map)
1040 return 1;
1041 if (obj.type == isl_obj_set && super == isl_obj_union_set)
1042 return 1;
1043 if (obj.type == isl_obj_schedule && super == isl_obj_union_map)
1044 return 1;
1045 if (obj.type == isl_obj_pw_multi_aff && super == isl_obj_union_set) {
1046 isl_space *space = isl_pw_multi_aff_get_space(obj.v);
1047 int is_set = isl_space_is_set(space);
1048 isl_space_free(space);
1049 return is_set;
1051 if (obj.type == isl_obj_pw_qpolynomial &&
1052 super == isl_obj_union_pw_qpolynomial)
1053 return 1;
1054 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1055 super == isl_obj_union_pw_qpolynomial_fold)
1056 return 1;
1057 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
1058 return 1;
1059 if (obj.type == isl_obj_list) {
1060 struct isl_list *list = obj.v;
1061 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1062 return is_subtype(list->obj[0], super);
1064 if (super == isl_obj_str)
1065 return 1;
1066 return 0;
1069 static struct isl_obj obj_at(struct isl_obj obj, int i)
1071 struct isl_list *list = obj.v;
1073 obj = list->obj[i];
1074 obj.v = obj.type->copy(obj.v);
1076 isl_list_free(list);
1078 return obj;
1081 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
1082 isl_obj_type type)
1084 if (obj.type == type)
1085 return obj;
1086 if (obj.type == isl_obj_pw_multi_aff && type == isl_obj_union_set) {
1087 isl_set *set = isl_set_from_pw_multi_aff(obj.v);
1088 obj.type = isl_obj_union_set;
1089 obj.v = isl_union_set_from_set(set);
1090 return obj;
1092 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
1093 obj.type = isl_obj_union_map;
1094 obj.v = isl_union_map_from_map(obj.v);
1095 return obj;
1097 if (obj.type == isl_obj_schedule && type == isl_obj_union_map) {
1098 obj.type = isl_obj_union_map;
1099 obj.v = schedule_map(obj.v);
1100 return obj;
1102 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
1103 obj.type = isl_obj_union_set;
1104 obj.v = isl_union_set_from_set(obj.v);
1105 return obj;
1107 if (obj.type == isl_obj_pw_qpolynomial &&
1108 type == isl_obj_union_pw_qpolynomial) {
1109 obj.type = isl_obj_union_pw_qpolynomial;
1110 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1111 return obj;
1113 if (obj.type == isl_obj_pw_qpolynomial_fold &&
1114 type == isl_obj_union_pw_qpolynomial_fold) {
1115 obj.type = isl_obj_union_pw_qpolynomial_fold;
1116 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1117 return obj;
1119 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
1120 if (type == isl_obj_union_map) {
1121 obj.type = isl_obj_union_map;
1122 return obj;
1124 if (type == isl_obj_union_pw_qpolynomial) {
1125 isl_space *dim = isl_union_set_get_space(obj.v);
1126 isl_union_set_free(obj.v);
1127 obj.v = isl_union_pw_qpolynomial_zero(dim);
1128 obj.type = isl_obj_union_pw_qpolynomial;
1129 return obj;
1131 if (type == isl_obj_union_pw_qpolynomial_fold) {
1132 isl_space *dim = isl_union_set_get_space(obj.v);
1133 isl_union_set_free(obj.v);
1134 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1135 isl_fold_list);
1136 obj.type = isl_obj_union_pw_qpolynomial_fold;
1137 return obj;
1140 if (obj.type == isl_obj_list) {
1141 struct isl_list *list = obj.v;
1142 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1143 return convert(ctx, obj_at(obj, 0), type);
1145 if (type == isl_obj_str) {
1146 isl_str *str;
1147 isl_printer *p;
1148 char *s;
1150 p = isl_printer_to_str(ctx);
1151 if (!p)
1152 goto error;
1153 p = obj.type->print(p, obj.v);
1154 s = isl_printer_get_str(p);
1155 isl_printer_free(p);
1157 str = isl_str_from_string(ctx, s);
1158 if (!str)
1159 goto error;
1160 free_obj(obj);
1161 obj.v = str;
1162 obj.type = isl_obj_str;
1163 return obj;
1166 error:
1167 free_obj(obj);
1168 obj.type = isl_obj_none;
1169 obj.v = NULL;
1170 return obj;
1173 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1174 struct isl_obj lhs)
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 (!bin_ops[i].op)
1185 break;
1186 if (bin_ops[i].op != isl_token_get_type(tok))
1187 continue;
1188 if (!is_subtype(lhs, bin_ops[i].lhs))
1189 continue;
1191 isl_token_free(tok);
1192 return &bin_ops[i];
1195 for (i = 0; ; ++i) {
1196 if (!named_bin_ops[i].name)
1197 break;
1198 if (named_bin_ops[i].op.op != isl_token_get_type(tok))
1199 continue;
1200 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1201 continue;
1203 isl_token_free(tok);
1204 return &named_bin_ops[i].op;
1207 isl_stream_push_token(s, tok);
1209 return NULL;
1212 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1214 int i;
1215 struct isl_token *tok;
1217 tok = isl_stream_next_token(s);
1218 if (!tok)
1219 return NULL;
1221 for (i = 0; ; ++i) {
1222 if (!named_un_ops[i].name)
1223 break;
1224 if (named_un_ops[i].op.op != isl_token_get_type(tok))
1225 continue;
1227 isl_token_free(tok);
1228 return &named_un_ops[i].op;
1231 isl_stream_push_token(s, tok);
1233 return NULL;
1236 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1237 struct isl_obj arg)
1239 int i;
1241 for (i = 0; ; ++i) {
1242 if (!named_un_ops[i].name)
1243 break;
1244 if (named_un_ops[i].op.op != like->op)
1245 continue;
1246 if (!is_subtype(arg, named_un_ops[i].op.arg))
1247 continue;
1249 return &named_un_ops[i].op;
1252 return NULL;
1255 static int is_assign(struct isl_stream *s)
1257 struct isl_token *tok;
1258 struct isl_token *tok2;
1259 int assign;
1261 tok = isl_stream_next_token(s);
1262 if (!tok)
1263 return 0;
1264 if (isl_token_get_type(tok) != ISL_TOKEN_IDENT) {
1265 isl_stream_push_token(s, tok);
1266 return 0;
1269 tok2 = isl_stream_next_token(s);
1270 if (!tok2) {
1271 isl_stream_push_token(s, tok);
1272 return 0;
1274 assign = isl_token_get_type(tok2) == ISL_TOKEN_DEF;
1275 isl_stream_push_token(s, tok2);
1276 isl_stream_push_token(s, tok);
1278 return assign;
1281 static struct isl_obj read_obj(struct isl_stream *s,
1282 struct isl_hash_table *table);
1283 static struct isl_obj read_expr(struct isl_stream *s,
1284 struct isl_hash_table *table);
1286 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1287 struct isl_hash_table *table, struct isc_un_op *op)
1289 isl_ctx *ctx;
1290 struct isl_obj obj = { isl_obj_none, NULL };
1292 obj = read_obj(s, table);
1293 if (!obj.v)
1294 goto error;
1296 op = find_matching_un_op(op, obj);
1298 ctx = isl_stream_get_ctx(s);
1299 if (!op)
1300 isl_die(ctx, isl_error_invalid,
1301 "no such unary operator defined on given operand",
1302 goto error);
1304 obj = convert(ctx, obj, op->arg);
1305 obj.v = op->fn(obj.v);
1306 obj.type = op->res;
1308 return obj;
1309 error:
1310 free_obj(obj);
1311 obj.type = isl_obj_none;
1312 obj.v = NULL;
1313 return obj;
1316 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1318 struct isl_list *list;
1319 int exact;
1321 if (obj.type != isl_obj_union_map)
1322 obj = convert(ctx, obj, isl_obj_union_map);
1323 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1324 list = isl_list_alloc(ctx, 2);
1325 if (!list)
1326 goto error;
1328 list->obj[0].type = isl_obj_union_map;
1329 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1330 list->obj[1].type = isl_obj_bool;
1331 list->obj[1].v = exact ? &iscc_bool_true : &iscc_bool_false;
1332 obj.v = list;
1333 obj.type = isl_obj_list;
1334 if (exact < 0 || !list->obj[0].v)
1335 goto error;
1337 return obj;
1338 error:
1339 free_obj(obj);
1340 obj.type = isl_obj_none;
1341 obj.v = NULL;
1342 return obj;
1345 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1347 struct isl_list *list = obj.v;
1348 struct isl_token *tok;
1349 isl_ctx *ctx;
1350 isl_val *v;
1351 int i;
1353 tok = isl_stream_next_token(s);
1354 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
1355 isl_stream_error(s, tok, "expecting index");
1356 if (tok)
1357 isl_stream_push_token(s, tok);
1358 goto error;
1360 ctx = isl_stream_get_ctx(s);
1361 v = isl_token_get_val(ctx, tok);
1362 i = isl_val_get_num_si(v);
1363 isl_val_free(v);
1364 isl_token_free(tok);
1365 isl_assert(ctx, i < list->n, goto error);
1366 if (isl_stream_eat(s, ']'))
1367 goto error;
1369 return obj_at(obj, i);
1370 error:
1371 free_obj(obj);
1372 obj.type = isl_obj_none;
1373 obj.v = NULL;
1374 return obj;
1377 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1378 struct isl_hash_table *table)
1380 isl_ctx *ctx;
1381 struct isl_obj obj;
1383 obj = read_expr(s, table);
1384 ctx = isl_stream_get_ctx(s);
1385 isl_assert(ctx, is_subtype(obj, isl_obj_union_set) ||
1386 is_subtype(obj, isl_obj_union_map), goto error);
1388 if (obj.type == isl_obj_list) {
1389 struct isl_list *list = obj.v;
1390 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1391 obj = obj_at(obj, 0);
1393 if (obj.type == isl_obj_set)
1394 obj = convert(ctx, obj, isl_obj_union_set);
1395 else if (obj.type == isl_obj_map)
1396 obj = convert(ctx, obj, isl_obj_union_map);
1397 if (obj.type == isl_obj_union_set) {
1398 obj.v = isl_union_set_apply(obj.v, umap);
1399 } else
1400 obj.v = isl_union_map_apply_range(obj.v, umap);
1401 if (!obj.v)
1402 goto error2;
1404 if (isl_stream_eat(s, ')'))
1405 goto error2;
1407 return obj;
1408 error:
1409 isl_union_map_free(umap);
1410 error2:
1411 free_obj(obj);
1412 obj.type = isl_obj_none;
1413 obj.v = NULL;
1414 return obj;
1417 static struct isl_obj apply_fun_set(struct isl_obj obj,
1418 __isl_take isl_union_set *uset)
1420 if (obj.type == isl_obj_union_pw_qpolynomial) {
1421 obj.v = isl_union_set_apply_union_pw_qpolynomial(uset, obj.v);
1422 } else {
1423 obj.type = isl_obj_list;
1424 obj.v = union_set_apply_union_pw_qpolynomial_fold(uset, obj.v);
1426 return obj;
1429 static struct isl_obj apply_fun_map(struct isl_obj obj,
1430 __isl_take isl_union_map *umap)
1432 if (obj.type == isl_obj_union_pw_qpolynomial) {
1433 obj.v = isl_union_map_apply_union_pw_qpolynomial(umap, obj.v);
1434 } else {
1435 obj.type = isl_obj_list;
1436 obj.v = union_map_apply_union_pw_qpolynomial_fold(umap, obj.v);
1438 return obj;
1441 static struct isl_obj apply_fun(struct isl_stream *s,
1442 struct isl_obj obj, struct isl_hash_table *table)
1444 struct isl_obj arg;
1445 isl_ctx *ctx;
1447 arg = read_expr(s, table);
1448 ctx = isl_stream_get_ctx(s);
1449 if (!is_subtype(arg, isl_obj_union_map) &&
1450 !is_subtype(arg, isl_obj_union_set))
1451 isl_die(ctx, isl_error_invalid,
1452 "expecting set of map argument", goto error);
1454 if (arg.type == isl_obj_list) {
1455 struct isl_list *list = arg.v;
1456 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1457 arg = obj_at(arg, 0);
1459 if (arg.type == isl_obj_set)
1460 arg = convert(ctx, arg, isl_obj_union_set);
1461 else if (arg.type == isl_obj_map)
1462 arg = convert(ctx, arg, isl_obj_union_map);
1463 if (arg.type == isl_obj_union_set)
1464 obj = apply_fun_set(obj, arg.v);
1465 else
1466 obj = apply_fun_map(obj, arg.v);
1467 if (!obj.v)
1468 goto error2;
1470 if (isl_stream_eat(s, ')'))
1471 goto error2;
1473 return obj;
1474 error:
1475 free_obj(arg);
1476 error2:
1477 free_obj(obj);
1478 obj.type = isl_obj_none;
1479 obj.v = NULL;
1480 return obj;
1483 struct add_vertex_data {
1484 struct isl_list *list;
1485 int i;
1488 static isl_stat add_vertex(__isl_take isl_vertex *vertex, void *user)
1490 struct add_vertex_data *data = (struct add_vertex_data *)user;
1491 isl_multi_aff *ma;
1492 isl_set *dom;
1494 ma = isl_vertex_get_expr(vertex);
1495 dom = isl_set_from_basic_set(isl_vertex_get_domain(vertex));
1497 data->list->obj[data->i].type = isl_obj_pw_multi_aff;
1498 data->list->obj[data->i].v = isl_pw_multi_aff_alloc(dom, ma);
1499 data->i++;
1501 isl_vertex_free(vertex);
1503 return isl_stat_ok;
1506 static isl_stat set_vertices(__isl_take isl_set *set, void *user)
1508 isl_ctx *ctx;
1509 isl_basic_set *hull;
1510 isl_vertices *vertices = NULL;
1511 struct isl_list *list = NULL;
1512 isl_stat r;
1513 struct add_vertex_data *data = (struct add_vertex_data *)user;
1515 set = isl_set_remove_divs(set);
1516 hull = isl_set_convex_hull(set);
1517 vertices = isl_basic_set_compute_vertices(hull);
1518 isl_basic_set_free(hull);
1520 list = data->list;
1522 ctx = isl_vertices_get_ctx(vertices);
1523 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1524 if (!data->list)
1525 goto error;
1527 data->i = 0;
1528 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1530 data->list = isl_list_concat(list, data->list);
1532 isl_vertices_free(vertices);
1534 return r;
1535 error:
1536 data->list = list;
1537 isl_vertices_free(vertices);
1538 return isl_stat_error;
1541 static struct isl_obj vertices(struct isl_stream *s,
1542 struct isl_hash_table *table)
1544 isl_ctx *ctx;
1545 struct isl_obj obj;
1546 struct isl_list *list = NULL;
1547 isl_union_set *uset = NULL;
1548 struct add_vertex_data data = { NULL };
1550 obj = read_expr(s, table);
1551 ctx = isl_stream_get_ctx(s);
1552 obj = convert(ctx, obj, isl_obj_union_set);
1553 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1554 uset = obj.v;
1555 obj.v = NULL;
1557 list = isl_list_alloc(ctx, 0);
1558 if (!list)
1559 goto error;
1561 data.list = list;
1563 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1564 goto error;
1566 isl_union_set_free(uset);
1568 obj.type = isl_obj_list;
1569 obj.v = data.list;
1571 return obj;
1572 error:
1573 isl_union_set_free(uset);
1574 isl_list_free(data.list);
1575 free_obj(obj);
1576 obj.type = isl_obj_none;
1577 obj.v = NULL;
1578 return obj;
1581 static struct isl_obj type_of(struct isl_stream *s,
1582 struct isl_hash_table *table)
1584 isl_ctx *ctx;
1585 struct isl_obj obj;
1586 const char *type = "unknown";
1588 obj = read_expr(s, table);
1590 if (obj.type == isl_obj_map ||
1591 obj.type == isl_obj_union_map)
1592 type = "map";
1593 if (obj.type == isl_obj_set ||
1594 obj.type == isl_obj_union_set)
1595 type = "set";
1596 if (obj.type == isl_obj_pw_multi_aff)
1597 type = "piecewise multi-quasiaffine expression";
1598 if (obj.type == isl_obj_pw_qpolynomial ||
1599 obj.type == isl_obj_union_pw_qpolynomial)
1600 type = "piecewise quasipolynomial";
1601 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1602 obj.type == isl_obj_union_pw_qpolynomial_fold)
1603 type = "piecewise quasipolynomial fold";
1604 if (obj.type == isl_obj_list)
1605 type = "list";
1606 if (obj.type == isl_obj_bool)
1607 type = "boolean";
1608 if (obj.type == isl_obj_str)
1609 type = "string";
1610 if (obj.type == isl_obj_val)
1611 type = "value";
1612 if (obj.type == isl_obj_schedule)
1613 type = "schedule";
1615 free_obj(obj);
1616 obj.type = isl_obj_str;
1617 obj.v = isl_str_from_string(isl_stream_get_ctx(s), strdup(type));
1619 return obj;
1622 static __isl_give isl_union_set *read_set(struct isl_stream *s,
1623 struct isl_hash_table *table)
1625 struct isl_obj obj;
1626 isl_ctx *ctx;
1628 obj = read_obj(s, table);
1629 ctx = isl_stream_get_ctx(s);
1630 obj = convert(ctx, obj, isl_obj_union_set);
1631 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
1632 return obj.v;
1633 error:
1634 free_obj(obj);
1635 return NULL;
1638 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1639 struct isl_hash_table *table)
1641 struct isl_obj obj;
1642 isl_ctx *ctx;
1644 obj = read_obj(s, table);
1645 ctx = isl_stream_get_ctx(s);
1646 obj = convert(ctx, obj, isl_obj_union_map);
1647 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1648 return obj.v;
1649 error:
1650 free_obj(obj);
1651 return NULL;
1654 /* Read a schedule in the form of either a schedule (tree) or a union map
1655 * from "s" and store the schedule in "access".
1657 static __isl_give isl_union_access_info *access_info_set_schedule(
1658 __isl_take isl_union_access_info *access, struct isl_stream *s,
1659 struct isl_hash_table *table)
1661 struct isl_obj obj;
1662 isl_ctx *ctx;
1664 obj = read_obj(s, table);
1665 if (obj.type == isl_obj_schedule)
1666 return isl_union_access_info_set_schedule(access, obj.v);
1667 ctx = isl_stream_get_ctx(s);
1668 obj = convert(ctx, obj, isl_obj_union_map);
1670 return isl_union_access_info_set_schedule_map(access, obj.v);
1673 static struct isl_obj last_any(struct isl_stream *s,
1674 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1675 __isl_take isl_union_map *may_source)
1677 struct isl_obj obj = { isl_obj_none, NULL };
1678 isl_union_access_info *access;
1679 isl_union_flow *flow;
1680 isl_union_map *sink = NULL;
1681 isl_union_map *may_dep;
1683 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1684 goto error;
1686 sink = read_map(s, table);
1687 if (!sink)
1688 goto error;
1690 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1691 goto error;
1693 access = isl_union_access_info_from_sink(sink);
1694 access = isl_union_access_info_set_must_source(access, must_source);
1695 access = isl_union_access_info_set_may_source(access, may_source);
1696 access = access_info_set_schedule(access, s, table);
1697 flow = isl_union_access_info_compute_flow(access);
1698 may_dep = isl_union_flow_get_may_dependence(flow);
1699 isl_union_flow_free(flow);
1701 if (!may_dep)
1702 return obj;
1704 obj.type = isl_obj_union_map;
1705 obj.v = may_dep;
1707 return obj;
1708 error:
1709 isl_union_map_free(may_source);
1710 isl_union_map_free(must_source);
1711 isl_union_map_free(sink);
1712 free_obj(obj);
1713 obj.type = isl_obj_none;
1714 obj.v = NULL;
1715 return obj;
1718 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1720 struct isl_obj obj = { isl_obj_none, NULL };
1721 isl_union_access_info *access;
1722 isl_union_flow *flow;
1723 isl_union_map *may_source = NULL;
1724 isl_union_map *sink = NULL;
1725 isl_union_map *may_dep;
1727 may_source = read_map(s, table);
1728 if (!may_source)
1729 goto error;
1731 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1732 isl_union_map *must_source;
1733 must_source = read_map(s, table);
1734 if (!must_source)
1735 goto error;
1736 return last_any(s, table, must_source, may_source);
1739 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1740 goto error;
1742 sink = read_map(s, table);
1743 if (!sink)
1744 goto error;
1746 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1747 goto error;
1749 access = isl_union_access_info_from_sink(sink);
1750 access = isl_union_access_info_set_may_source(access, may_source);
1751 access = access_info_set_schedule(access, s, table);
1752 flow = isl_union_access_info_compute_flow(access);
1753 may_dep = isl_union_flow_get_may_dependence(flow);
1754 isl_union_flow_free(flow);
1756 if (!may_dep)
1757 return obj;
1759 obj.type = isl_obj_union_map;
1760 obj.v = may_dep;
1762 return obj;
1763 error:
1764 isl_union_map_free(may_source);
1765 isl_union_map_free(sink);
1766 free_obj(obj);
1767 obj.type = isl_obj_none;
1768 obj.v = NULL;
1769 return obj;
1772 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1774 struct isl_obj obj = { isl_obj_none, NULL };
1775 struct isl_list *list = NULL;
1776 isl_union_access_info *access;
1777 isl_union_flow *flow;
1778 isl_union_map *must_source = NULL;
1779 isl_union_map *sink = NULL;
1780 isl_union_map *must_dep;
1781 isl_union_map *must_no_source;
1783 must_source = read_map(s, table);
1784 if (!must_source)
1785 goto error;
1787 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1788 isl_union_map *may_source;
1789 may_source = read_map(s, table);
1790 if (!may_source)
1791 goto error;
1792 return last_any(s, table, must_source, may_source);
1795 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
1796 if (!list)
1797 goto error;
1799 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1800 goto error;
1802 sink = read_map(s, table);
1803 if (!sink)
1804 goto error;
1806 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1807 goto error;
1809 access = isl_union_access_info_from_sink(sink);
1810 access = isl_union_access_info_set_must_source(access, must_source);
1811 access = access_info_set_schedule(access, s, table);
1812 flow = isl_union_access_info_compute_flow(access);
1813 must_dep = isl_union_flow_get_must_dependence(flow);
1814 must_no_source = isl_union_flow_get_must_no_source(flow);
1815 isl_union_flow_free(flow);
1817 list->obj[0].type = isl_obj_union_map;
1818 list->obj[0].v = must_dep;
1819 list->obj[1].type = isl_obj_union_map;
1820 list->obj[1].v = must_no_source;
1822 if (!must_dep || !must_no_source) {
1823 isl_list_free(list);
1824 return obj;
1827 obj.v = list;
1828 obj.type = isl_obj_list;
1830 return obj;
1831 error:
1832 isl_list_free(list);
1833 isl_union_map_free(must_source);
1834 isl_union_map_free(sink);
1835 free_obj(obj);
1836 obj.type = isl_obj_none;
1837 obj.v = NULL;
1838 return obj;
1841 static __isl_give isl_schedule *get_schedule(struct isl_stream *s,
1842 struct isl_hash_table *table)
1844 isl_union_set *domain;
1845 isl_union_map *validity;
1846 isl_union_map *proximity;
1848 domain = read_set(s, table);
1849 if (!domain)
1850 return NULL;
1852 validity = isl_union_map_empty(isl_union_set_get_space(domain));
1853 proximity = isl_union_map_empty(isl_union_set_get_space(domain));
1855 for (;;) {
1856 isl_union_map *umap;
1857 if (isl_stream_eat_if_available(s, iscc_op[ISCC_RESPECTING])) {
1858 umap = read_map(s, table);
1859 validity = isl_union_map_union(validity, umap);
1860 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_MINIMIZING])) {
1861 umap = read_map(s, table);
1862 proximity = isl_union_map_union(proximity, umap);
1863 } else
1864 break;
1867 return isl_union_set_compute_schedule(domain, validity, proximity);
1870 static struct isl_obj schedule(struct isl_stream *s,
1871 struct isl_hash_table *table)
1873 struct isl_obj obj = { isl_obj_none, NULL };
1874 isl_schedule *schedule;
1876 schedule = get_schedule(s, table);
1878 obj.v = schedule;
1879 obj.type = isl_obj_schedule;
1881 return obj;
1884 /* Read a schedule for code generation in the form of either
1885 * a schedule tree or a union map.
1886 * If the input is a set rather than a map, then we construct
1887 * an identity union map schedule on the given set.
1889 static struct isl_obj get_codegen_schedule(struct isl_stream *s,
1890 struct isl_hash_table *table)
1892 struct isl_obj obj;
1893 isl_ctx *ctx;
1895 obj = read_obj(s, table);
1896 ctx = isl_stream_get_ctx(s);
1898 if (obj.type == isl_obj_schedule)
1899 return obj;
1900 if (is_subtype(obj, isl_obj_union_set)) {
1901 obj = convert(ctx, obj, isl_obj_union_set);
1902 obj.v = isl_union_set_identity(obj.v);
1903 obj.type = isl_obj_union_map;
1905 if (is_subtype(obj, isl_obj_union_map))
1906 return convert(ctx, obj, isl_obj_union_map);
1908 free_obj(obj);
1909 obj.v = NULL;
1910 obj.type = isl_obj_none;
1911 isl_die(ctx, isl_error_invalid, "expecting schedule, set or map",
1912 return obj);
1915 /* Generate an AST for the given schedule and options and return the AST.
1917 static __isl_give isl_ast_node *get_ast_from_union_map(
1918 __isl_take isl_union_map *schedule, __isl_take isl_union_map *options)
1920 isl_space *space;
1921 isl_set *context;
1922 isl_ast_build *build;
1923 isl_ast_node *tree;
1925 space = isl_union_map_get_space(schedule);
1926 context = isl_set_universe(isl_space_params(space));
1928 build = isl_ast_build_from_context(context);
1929 build = isl_ast_build_set_options(build, options);
1930 tree = isl_ast_build_ast_from_schedule(build, schedule);
1931 isl_ast_build_free(build);
1933 return tree;
1936 /* Generate an AST for the given schedule and return the AST.
1938 static __isl_give isl_ast_node *get_ast_from_schedule(
1939 __isl_take isl_schedule *schedule)
1941 isl_ast_build *build;
1942 isl_ast_node *tree;
1944 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
1945 tree = isl_ast_build_node_from_schedule(build, schedule);
1946 isl_ast_build_free(build);
1948 return tree;
1951 /* Print the AST "tree" on the printer "p".
1953 static __isl_give isl_printer *print_ast(__isl_take isl_printer *p,
1954 __isl_take isl_ast_node *tree)
1956 int format;
1958 format = isl_printer_get_output_format(p);
1959 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
1960 p = isl_printer_print_ast_node(p, tree);
1961 p = isl_printer_set_output_format(p, format);
1963 isl_ast_node_free(tree);
1965 return p;
1968 /* Perform the codegen operation.
1969 * In particular, read a schedule, check if the user has specified any options
1970 * and then generate an AST from the schedule (and options) and print it.
1971 * In case the schedule is specified as a schedule tree, the AST generation
1972 * options are embedded in the schedule, so they are not read in separately.
1974 static __isl_give isl_printer *codegen(struct isl_stream *s,
1975 struct isl_hash_table *table, __isl_take isl_printer *p)
1977 struct isl_obj obj;
1978 isl_ast_node *tree;
1980 obj = get_codegen_schedule(s, table);
1981 if (!obj.v)
1982 return p;
1984 if (obj.type == isl_obj_schedule) {
1985 isl_schedule *schedule = obj.v;
1987 tree = get_ast_from_schedule(schedule);
1988 } else {
1989 isl_union_map *schedule = obj.v;
1990 isl_union_map *options;
1992 if (isl_stream_eat_if_available(s, iscc_op[ISCC_USING]))
1993 options = read_map(s, table);
1994 else
1995 options = isl_union_map_empty(
1996 isl_union_map_get_space(schedule));
1998 tree = get_ast_from_union_map(schedule, options);
2001 p = print_ast(p, tree);
2003 isl_stream_eat(s, ';');
2005 return p;
2008 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands);
2010 static struct isl_obj band_to_obj_list(__isl_take isl_band *band)
2012 struct isl_obj obj = { isl_obj_none, NULL };
2013 isl_ctx *ctx = isl_band_get_ctx(band);
2014 struct isl_list *list;
2016 list = isl_list_alloc(ctx, 2);
2017 if (!list)
2018 goto error;
2020 obj.v = list;
2021 obj.type = isl_obj_list;
2023 list->obj[0].type = isl_obj_union_map;
2024 list->obj[0].v = isl_band_get_partial_schedule(band);
2026 if (isl_band_has_children(band)) {
2027 isl_band_list *children;
2029 children = isl_band_get_children(band);
2030 list->obj[1] = band_list_to_obj_list(children);
2031 } else {
2032 list->obj[1].type = isl_obj_list;
2033 list->obj[1].v = isl_list_alloc(ctx, 0);
2036 if (!list->obj[0].v || !list->obj[1].v)
2037 goto error;
2039 isl_band_free(band);
2041 return obj;
2042 error:
2043 isl_band_free(band);
2044 free_obj(obj);
2045 obj.type = isl_obj_none;
2046 obj.v = NULL;
2047 return obj;
2050 static struct isl_obj band_list_to_obj_list(__isl_take isl_band_list *bands)
2052 struct isl_obj obj = { isl_obj_none, NULL };
2053 isl_ctx *ctx = isl_band_list_get_ctx(bands);
2054 struct isl_list *list;
2055 int i, n;
2057 n = isl_band_list_n_band(bands);
2058 list = isl_list_alloc(ctx, n);
2059 if (!list)
2060 goto error;
2062 obj.v = list;
2063 obj.type = isl_obj_list;
2065 for (i = 0; i < n; ++i) {
2066 isl_band *band;
2068 band = isl_band_list_get_band(bands, i);
2069 list->obj[i] = band_to_obj_list(band);
2070 if (!list->obj[i].v)
2071 goto error;
2074 isl_band_list_free(bands);
2076 return obj;
2077 error:
2078 isl_band_list_free(bands);
2079 free_obj(obj);
2080 obj.type = isl_obj_none;
2081 obj.v = NULL;
2082 return obj;
2085 static struct isl_obj schedule_forest(struct isl_stream *s,
2086 struct isl_hash_table *table)
2088 struct isl_obj obj = { isl_obj_none, NULL };
2089 isl_schedule *schedule;
2090 isl_band_list *roots;
2092 schedule = get_schedule(s, table);
2093 if (!schedule)
2094 return obj;
2096 roots = isl_schedule_get_band_forest(schedule);
2097 isl_schedule_free(schedule);
2099 return band_list_to_obj_list(roots);
2102 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
2104 struct isl_token *tok;
2105 isl_ctx *ctx;
2106 isl_val *v;
2108 ctx = isl_stream_get_ctx(s);
2109 if (isl_stream_eat_if_available(s, '+'))
2110 return transitive_closure(ctx, obj);
2112 isl_assert(ctx, is_subtype(obj, isl_obj_union_map), goto error);
2113 if (obj.type != isl_obj_union_map)
2114 obj = convert(ctx, obj, isl_obj_union_map);
2116 tok = isl_stream_next_token(s);
2117 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_VALUE) {
2118 isl_stream_error(s, tok, "expecting integer exponent");
2119 if (tok)
2120 isl_stream_push_token(s, tok);
2121 goto error;
2124 v = isl_token_get_val(ctx, tok);
2125 if (isl_val_is_zero(v)) {
2126 isl_stream_error(s, tok, "expecting non-zero exponent");
2127 isl_val_free(v);
2128 if (tok)
2129 isl_stream_push_token(s, tok);
2130 goto error;
2133 obj.v = isl_union_map_fixed_power_val(obj.v, v);
2134 isl_token_free(tok);
2135 if (!obj.v)
2136 goto error;
2138 return obj;
2139 error:
2140 free_obj(obj);
2141 obj.type = isl_obj_none;
2142 obj.v = NULL;
2143 return obj;
2146 static struct isl_obj check_assert(struct isl_stream *s,
2147 struct isl_hash_table *table)
2149 struct isl_obj obj;
2150 isl_ctx *ctx;
2152 obj = read_expr(s, table);
2153 ctx = isl_stream_get_ctx(s);
2154 if (obj.type != isl_obj_bool)
2155 isl_die(ctx, isl_error_invalid,
2156 "expecting boolean expression", goto error);
2157 if (obj.v != &iscc_bool_true)
2158 isl_die(ctx, isl_error_unknown,
2159 "assertion failed", abort());
2160 error:
2161 free_obj(obj);
2162 obj.type = isl_obj_none;
2163 obj.v = NULL;
2164 return obj;
2167 static struct isl_obj read_from_file(struct isl_stream *s)
2169 isl_ctx *ctx;
2170 struct isl_obj obj;
2171 struct isl_token *tok;
2172 struct isl_stream *s_file;
2173 struct iscc_options *options;
2174 char *name;
2175 FILE *file;
2177 tok = isl_stream_next_token(s);
2178 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2179 isl_stream_error(s, tok, "expecting filename");
2180 isl_token_free(tok);
2181 goto error;
2184 ctx = isl_stream_get_ctx(s);
2185 options = isl_ctx_peek_iscc_options(ctx);
2186 if (!options || !options->io) {
2187 isl_token_free(tok);
2188 isl_die(ctx, isl_error_invalid,
2189 "read operation not allowed", goto error);
2192 name = isl_token_get_str(ctx, tok);
2193 isl_token_free(tok);
2194 file = fopen(name, "r");
2195 free(name);
2196 isl_assert(ctx, file, goto error);
2198 s_file = isl_stream_new_file(ctx, file);
2199 if (!s_file) {
2200 fclose(file);
2201 goto error;
2204 obj = isl_stream_read_obj(s_file);
2206 isl_stream_free(s_file);
2207 fclose(file);
2209 return obj;
2210 error:
2211 obj.type = isl_obj_none;
2212 obj.v = NULL;
2213 return obj;
2216 static struct isl_obj write_to_file(struct isl_stream *s,
2217 struct isl_hash_table *table)
2219 struct isl_obj obj;
2220 struct isl_token *tok;
2221 struct isl_stream *s_file;
2222 struct iscc_options *options;
2223 char *name;
2224 FILE *file;
2225 isl_ctx *ctx;
2226 isl_printer *p;
2228 tok = isl_stream_next_token(s);
2229 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2230 isl_stream_error(s, tok, "expecting filename");
2231 isl_token_free(tok);
2232 goto error;
2235 obj = read_expr(s, table);
2237 ctx = isl_stream_get_ctx(s);
2238 options = isl_ctx_peek_iscc_options(ctx);
2239 if (!options || !options->io) {
2240 isl_token_free(tok);
2241 isl_die(ctx, isl_error_invalid,
2242 "write operation not allowed", goto error);
2245 name = isl_token_get_str(ctx, tok);
2246 isl_token_free(tok);
2247 file = fopen(name, "w");
2248 free(name);
2249 if (!file)
2250 isl_die(ctx, isl_error_unknown,
2251 "could not open file for writing", goto error);
2253 p = isl_printer_to_file(ctx, file);
2254 p = isl_printer_set_output_format(p, options->format);
2255 p = obj.type->print(p, obj.v);
2256 p = isl_printer_end_line(p);
2257 isl_printer_free(p);
2259 fclose(file);
2260 error:
2261 free_obj(obj);
2262 obj.type = isl_obj_none;
2263 obj.v = NULL;
2264 return obj;
2267 static struct isl_obj read_string_if_available(struct isl_stream *s)
2269 struct isl_token *tok;
2270 struct isl_obj obj = { isl_obj_none, NULL };
2272 tok = isl_stream_next_token(s);
2273 if (!tok)
2274 return obj;
2275 if (isl_token_get_type(tok) == ISL_TOKEN_STRING) {
2276 isl_str *str;
2277 str = isl_str_alloc(isl_stream_get_ctx(s));
2278 if (!str)
2279 goto error;
2280 str->s = isl_token_get_str(isl_stream_get_ctx(s), tok);
2281 isl_token_free(tok);
2282 obj.v = str;
2283 obj.type = isl_obj_str;
2284 } else
2285 isl_stream_push_token(s, tok);
2286 return obj;
2287 error:
2288 isl_token_free(tok);
2289 return obj;
2292 static struct isl_obj read_bool_if_available(struct isl_stream *s)
2294 struct isl_token *tok;
2295 struct isl_obj obj = { isl_obj_none, NULL };
2296 int type;
2298 tok = isl_stream_next_token(s);
2299 if (!tok)
2300 return obj;
2301 type = isl_token_get_type(tok);
2302 if (type == ISL_TOKEN_FALSE || type == ISL_TOKEN_TRUE) {
2303 int is_true = type == ISL_TOKEN_TRUE;
2304 isl_token_free(tok);
2305 obj.v = is_true ? &iscc_bool_true : &iscc_bool_false;
2306 obj.type = isl_obj_bool;
2307 } else
2308 isl_stream_push_token(s, tok);
2309 return obj;
2312 static __isl_give char *read_ident(struct isl_stream *s)
2314 char *name;
2315 isl_val *v;
2316 struct isl_token *tok, *tok2;
2318 name = isl_stream_read_ident_if_available(s);
2319 if (name)
2320 return name;
2322 tok = isl_stream_next_token(s);
2323 if (!tok)
2324 return NULL;
2325 if (isl_token_get_type(tok) != '$') {
2326 isl_stream_push_token(s, tok);
2327 return NULL;
2329 tok2 = isl_stream_next_token(s);
2330 if (!tok2 || isl_token_get_type(tok2) != ISL_TOKEN_VALUE) {
2331 if (tok2)
2332 isl_stream_push_token(s, tok2);
2333 isl_stream_push_token(s, tok);
2334 return NULL;
2337 v = isl_token_get_val(isl_stream_get_ctx(s), tok2);
2338 name = isl_val_to_str(v);
2339 isl_val_free(v);
2340 isl_token_free(tok);
2341 isl_token_free(tok2);
2343 return name;
2346 static struct isl_obj read_list(struct isl_stream *s,
2347 struct isl_hash_table *table, struct isl_obj obj)
2349 struct isl_list *list;
2351 list = isl_list_alloc(isl_stream_get_ctx(s), 2);
2352 if (!list)
2353 goto error;
2354 list->obj[0] = obj;
2355 list->obj[1] = read_obj(s, table);
2356 obj.v = list;
2357 obj.type = isl_obj_list;
2359 if (!list->obj[1].v)
2360 goto error;
2362 while (isl_stream_eat_if_available(s, ',')) {
2363 obj.v = list = isl_list_add_obj(list, read_obj(s, table));
2364 if (!obj.v)
2365 goto error;
2368 return obj;
2369 error:
2370 free_obj(obj);
2371 obj.type = isl_obj_none;
2372 obj.v = NULL;
2373 return obj;
2376 static struct isl_obj read_obj(struct isl_stream *s,
2377 struct isl_hash_table *table)
2379 isl_ctx *ctx;
2380 struct isl_obj obj = { isl_obj_none, NULL };
2381 char *name = NULL;
2382 struct isc_un_op *op = NULL;
2384 obj = read_string_if_available(s);
2385 if (obj.v)
2386 return obj;
2387 obj = read_bool_if_available(s);
2388 if (obj.v)
2389 return obj;
2390 ctx = isl_stream_get_ctx(s);
2391 if (isl_stream_eat_if_available(s, '(')) {
2392 if (isl_stream_next_token_is(s, ')')) {
2393 obj.type = isl_obj_list;
2394 obj.v = isl_list_alloc(ctx, 0);
2395 } else {
2396 obj = read_expr(s, table);
2397 if (obj.v && isl_stream_eat_if_available(s, ','))
2398 obj = read_list(s, table, obj);
2400 if (!obj.v || isl_stream_eat(s, ')'))
2401 goto error;
2402 } else {
2403 op = read_prefix_un_op_if_available(s);
2404 if (op)
2405 return read_un_op_expr(s, table, op);
2407 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ASSERT]))
2408 return check_assert(s, table);
2409 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
2410 return read_from_file(s);
2411 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
2412 return write_to_file(s, table);
2413 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
2414 return vertices(s, table);
2415 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
2416 return any(s, table);
2417 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
2418 return last(s, table);
2419 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE]))
2420 return schedule(s, table);
2421 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SCHEDULE_FOREST]))
2422 return schedule_forest(s, table);
2423 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
2424 return type_of(s, table);
2426 name = read_ident(s);
2427 if (name)
2428 obj = stored_obj(ctx, table, name);
2429 else
2430 obj = isl_stream_read_obj(s);
2431 if (!obj.v)
2432 goto error;
2435 if (isl_stream_eat_if_available(s, '^'))
2436 obj = power(s, obj);
2437 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
2438 obj = obj_at_index(s, obj);
2439 else if (is_subtype(obj, isl_obj_union_map) &&
2440 isl_stream_eat_if_available(s, '(')) {
2441 obj = convert(ctx, obj, isl_obj_union_map);
2442 obj = apply(s, obj.v, table);
2443 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
2444 isl_stream_eat_if_available(s, '(')) {
2445 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial);
2446 obj = apply_fun(s, obj, table);
2447 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
2448 isl_stream_eat_if_available(s, '(')) {
2449 obj = convert(ctx, obj, isl_obj_union_pw_qpolynomial_fold);
2450 obj = apply_fun(s, obj, table);
2453 return obj;
2454 error:
2455 free_obj(obj);
2456 obj.type = isl_obj_none;
2457 obj.v = NULL;
2458 return obj;
2461 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
2462 struct isl_obj lhs, struct isl_obj rhs)
2464 int i;
2466 for (i = 0; ; ++i) {
2467 if (!bin_ops[i].op)
2468 break;
2469 if (bin_ops[i].op != like->op)
2470 continue;
2471 if (!is_subtype(lhs, bin_ops[i].lhs))
2472 continue;
2473 if (!is_subtype(rhs, bin_ops[i].rhs))
2474 continue;
2476 return &bin_ops[i];
2479 for (i = 0; ; ++i) {
2480 if (!named_bin_ops[i].name)
2481 break;
2482 if (named_bin_ops[i].op.op != like->op)
2483 continue;
2484 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
2485 continue;
2486 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
2487 continue;
2489 return &named_bin_ops[i].op;
2492 return NULL;
2495 static int next_is_neg_int(struct isl_stream *s)
2497 struct isl_token *tok;
2498 int ret;
2500 tok = isl_stream_next_token(s);
2501 if (tok && isl_token_get_type(tok) == ISL_TOKEN_VALUE) {
2502 isl_val *v;
2503 v = isl_token_get_val(isl_stream_get_ctx(s), tok);
2504 ret = isl_val_is_neg(v);
2505 isl_val_free(v);
2506 } else
2507 ret = 0;
2508 isl_stream_push_token(s, tok);
2510 return ret;
2513 static struct isl_obj call_bin_op(isl_ctx *ctx, struct isc_bin_op *op,
2514 struct isl_obj lhs, struct isl_obj rhs)
2516 struct isl_obj obj;
2518 lhs = convert(ctx, lhs, op->lhs);
2519 rhs = convert(ctx, rhs, op->rhs);
2520 if (op->res != isl_obj_bool)
2521 obj.v = op->o.fn(lhs.v, rhs.v);
2522 else {
2523 int res = op->o.test(lhs.v, rhs.v);
2524 free_obj(lhs);
2525 free_obj(rhs);
2526 obj.v = iscc_bool_from_int(res);
2528 obj.type = op->res;
2530 return obj;
2533 static struct isl_obj read_expr(struct isl_stream *s,
2534 struct isl_hash_table *table)
2536 isl_ctx *ctx;
2537 struct isl_obj obj = { isl_obj_none, NULL };
2538 struct isl_obj right_obj = { isl_obj_none, NULL };
2540 obj = read_obj(s, table);
2541 ctx = isl_stream_get_ctx(s);
2542 for (; obj.v;) {
2543 struct isc_bin_op *op = NULL;
2545 op = read_bin_op_if_available(s, obj);
2546 if (!op)
2547 break;
2549 right_obj = read_obj(s, table);
2551 op = find_matching_bin_op(op, obj, right_obj);
2553 if (!op)
2554 isl_die(ctx, isl_error_invalid,
2555 "no such binary operator defined on given operands",
2556 goto error);
2558 obj = call_bin_op(ctx, op, obj, right_obj);
2561 if (obj.type == isl_obj_val && next_is_neg_int(s)) {
2562 right_obj = read_obj(s, table);
2563 obj.v = isl_val_add(obj.v, right_obj.v);
2566 return obj;
2567 error:
2568 free_obj(right_obj);
2569 free_obj(obj);
2570 obj.type = isl_obj_none;
2571 obj.v = NULL;
2572 return obj;
2575 static __isl_give isl_printer *source_file(struct isl_stream *s,
2576 struct isl_hash_table *table, __isl_take isl_printer *p);
2578 /* Print "obj" to the printer "p".
2579 * If the object is a schedule, then print it in block format.
2581 static __isl_give isl_printer *print_obj(__isl_take isl_printer *p,
2582 struct isl_obj obj)
2584 if (obj.type != isl_obj_schedule)
2585 return obj.type->print(p, obj.v);
2587 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
2588 p = obj.type->print(p, obj.v);
2589 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2591 return p;
2594 static __isl_give isl_printer *read_line(struct isl_stream *s,
2595 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
2597 isl_ctx *ctx;
2598 struct isl_obj obj = { isl_obj_none, NULL };
2599 char *lhs = NULL;
2600 int assign = 0;
2601 int only_print = 0;
2602 struct isc_bin_op *op = NULL;
2603 char buf[30];
2605 if (!p)
2606 return NULL;
2607 if (isl_stream_is_empty(s))
2608 return p;
2610 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
2611 return source_file(s, table, p);
2612 if (isl_stream_eat_if_available(s, iscc_op[ISCC_CODEGEN]))
2613 return codegen(s, table, p);
2615 assign = is_assign(s);
2616 if (assign) {
2617 lhs = isl_stream_read_ident_if_available(s);
2618 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2619 goto error;
2620 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2621 only_print = 1;
2622 else if (!tty)
2623 only_print = 1;
2625 obj = read_expr(s, table);
2626 ctx = isl_stream_get_ctx(s);
2627 if (isl_ctx_last_error(ctx) == isl_error_abort) {
2628 fprintf(stderr, "Interrupted\n");
2629 isl_ctx_reset_error(ctx);
2631 if (isl_stream_eat(s, ';'))
2632 goto error;
2634 if (only_print) {
2635 if (obj.type != isl_obj_none && obj.v != NULL) {
2636 p = print_obj(p, obj);
2637 p = isl_printer_end_line(p);
2639 free_obj(obj);
2640 return p;
2642 if (!assign && obj.type != isl_obj_none && obj.v != NULL) {
2643 static int count = 0;
2644 snprintf(buf, sizeof(buf), "$%d", count++);
2645 lhs = strdup(buf + 1);
2647 p = isl_printer_print_str(p, buf);
2648 p = isl_printer_print_str(p, " := ");
2649 p = obj.type->print(p, obj.v);
2650 p = isl_printer_end_line(p);
2652 if (lhs && do_assign(ctx, table, lhs, obj))
2653 return p;
2655 return p;
2656 error:
2657 isl_stream_flush_tokens(s);
2658 isl_stream_skip_line(s);
2659 free(lhs);
2660 free_obj(obj);
2661 return p;
2664 static isl_stat free_cb(void **entry, void *user)
2666 struct isl_named_obj *named = *entry;
2668 free_obj(named->obj);
2669 free(named->name);
2670 free(named);
2672 return isl_stat_ok;
2675 static void register_named_ops(struct isl_stream *s)
2677 int i;
2679 for (i = 0; i < ISCC_N_OP; ++i) {
2680 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2681 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2684 for (i = 0; ; ++i) {
2685 if (!named_un_ops[i].name)
2686 break;
2687 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2688 named_un_ops[i].name);
2689 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2692 for (i = 0; ; ++i) {
2693 if (!named_bin_ops[i].name)
2694 break;
2695 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2696 named_bin_ops[i].name);
2697 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2701 static __isl_give isl_printer *source_file(struct isl_stream *s,
2702 struct isl_hash_table *table, __isl_take isl_printer *p)
2704 isl_ctx *ctx;
2705 struct isl_token *tok;
2706 struct isl_stream *s_file;
2707 struct iscc_options *options;
2708 char *name;
2709 FILE *file;
2711 tok = isl_stream_next_token(s);
2712 if (!tok || isl_token_get_type(tok) != ISL_TOKEN_STRING) {
2713 isl_stream_error(s, tok, "expecting filename");
2714 isl_token_free(tok);
2715 return p;
2718 isl_stream_eat(s, ';');
2720 ctx = isl_stream_get_ctx(s);
2721 options = isl_ctx_peek_iscc_options(ctx);
2722 if (!options || !options->io) {
2723 isl_token_free(tok);
2724 isl_die(ctx, isl_error_invalid,
2725 "source operation not allowed", return p);
2728 name = isl_token_get_str(ctx, tok);
2729 isl_token_free(tok);
2730 file = fopen(name, "r");
2731 free(name);
2732 isl_assert(ctx, file, return p);
2734 s_file = isl_stream_new_file(ctx, file);
2735 if (!s_file) {
2736 fclose(file);
2737 return p;
2740 register_named_ops(s_file);
2742 while (!isl_stream_is_empty(s_file))
2743 p = read_line(s_file, table, p, 0);
2745 isl_stream_free(s_file);
2746 fclose(file);
2748 return p;
2751 int main(int argc, char **argv)
2753 struct isl_ctx *ctx;
2754 struct isl_stream *s;
2755 struct isl_hash_table *table;
2756 struct iscc_options *options;
2757 isl_printer *p;
2758 int tty = isatty(0);
2760 options = iscc_options_new_with_defaults();
2761 assert(options);
2763 ctx = isl_ctx_alloc_with_options(&iscc_options_args, options);
2764 pet_options_set_autodetect(ctx, 1);
2765 argc = isl_ctx_parse_options(ctx, argc, argv, ISL_ARG_ALL);
2766 s = isl_stream_new_file(ctx, stdin);
2767 assert(s);
2768 table = isl_hash_table_alloc(ctx, 10);
2769 assert(table);
2770 p = isl_printer_to_file(ctx, stdout);
2771 p = isl_printer_set_output_format(p, options->format);
2772 assert(p);
2774 register_named_ops(s);
2776 install_signal_handler(ctx);
2778 while (p && !isl_stream_is_empty(s)) {
2779 isl_ctx_resume(ctx);
2780 p = read_line(s, table, p, tty);
2783 remove_signal_handler(ctx);
2785 isl_printer_free(p);
2786 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2787 isl_hash_table_free(ctx, table);
2788 isl_stream_free(s);
2789 isl_ctx_free(ctx);
2791 return 0;