isl_obj_str: print quotes around string
[barvinok.git] / iscc.c
bloba1141235bc6d52960df3da8f55d45b81a0bcefa9
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <isl/obj.h>
7 #include <isl/stream.h>
8 #include <isl/vertices.h>
9 #include <isl/flow.h>
10 #include <isl_obj_list.h>
11 #include <isl_obj_str.h>
12 #include <barvinok/isl.h>
13 #include <barvinok/options.h>
15 #include "config.h"
17 #ifdef HAVE_CLOOG
18 #include <cloog/isl/cloog.h>
19 #endif
21 static int isl_bool_false = 0;
22 static int isl_bool_true = 1;
23 static int isl_bool_error = -1;
25 enum iscc_op { ISCC_READ, ISCC_WRITE, ISCC_SOURCE, ISCC_VERTICES,
26 ISCC_LAST, ISCC_ANY, ISCC_BEFORE, ISCC_UNDER,
27 ISCC_TYPEOF, ISCC_PRINT,
28 ISCC_N_OP };
29 static const char *op_name[ISCC_N_OP] = {
30 [ISCC_READ] = "read",
31 [ISCC_WRITE] = "write",
32 [ISCC_PRINT] = "print",
33 [ISCC_SOURCE] = "source",
34 [ISCC_VERTICES] = "vertices",
35 [ISCC_LAST] = "last",
36 [ISCC_ANY] = "any",
37 [ISCC_BEFORE] = "before",
38 [ISCC_UNDER] = "under",
39 [ISCC_TYPEOF] = "typeof"
41 static enum isl_token_type iscc_op[ISCC_N_OP];
43 struct isl_arg_choice iscc_format[] = {
44 {"isl", ISL_FORMAT_ISL},
45 {"omega", ISL_FORMAT_OMEGA},
46 {"polylib", ISL_FORMAT_POLYLIB},
47 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB},
48 {"latex", ISL_FORMAT_LATEX},
49 {"C", ISL_FORMAT_C},
50 {0}
53 struct iscc_options {
54 struct barvinok_options *barvinok;
55 unsigned format;
56 int io;
59 struct isl_arg iscc_options_arg[] = {
60 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", barvinok_options_arg,
61 "barvinok options")
62 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
63 iscc_format, ISL_FORMAT_ISL, "output format")
64 ISL_ARG_BOOL(struct iscc_options, io, 0, "io", 1,
65 "allow read and write operations")
66 ISL_ARG_END
69 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_arg)
70 ISL_ARG_CTX_DEF(iscc_options, struct iscc_options, iscc_options_arg)
72 static void *isl_obj_bool_copy(void *v)
74 return v;
77 static void isl_obj_bool_free(void *v)
81 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
82 void *v)
84 if (v == &isl_bool_true)
85 return isl_printer_print_str(p, "True");
86 else if (v == &isl_bool_false)
87 return isl_printer_print_str(p, "False");
88 else
89 return isl_printer_print_str(p, "Error");
92 static void *isl_obj_bool_add(void *v1, void *v2)
94 return v1;
97 struct isl_obj_vtable isl_obj_bool_vtable = {
98 isl_obj_bool_copy,
99 isl_obj_bool_add,
100 isl_obj_bool_print,
101 isl_obj_bool_free
103 #define isl_obj_bool (&isl_obj_bool_vtable)
105 int *isl_bool_from_int(int res)
107 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
110 int *union_map_is_equal(__isl_take isl_union_map *map1,
111 __isl_take isl_union_map *map2)
113 int res = isl_union_map_is_equal(map1, map2);
114 isl_union_map_free(map1);
115 isl_union_map_free(map2);
116 return isl_bool_from_int(res);
118 int *union_set_is_equal(__isl_take isl_union_set *set1,
119 __isl_take isl_union_set *set2)
121 return union_map_is_equal((isl_union_map *)set1, (isl_union_map *)set2);
124 int *union_map_is_subset(__isl_take isl_union_map *map1,
125 __isl_take isl_union_map *map2)
127 int res = isl_union_map_is_subset(map1, map2);
128 isl_union_map_free(map1);
129 isl_union_map_free(map2);
130 return isl_bool_from_int(res);
132 int *union_set_is_subset(__isl_take isl_union_set *set1,
133 __isl_take isl_union_set *set2)
135 return union_map_is_subset((isl_union_map *)set1, (isl_union_map *)set2);
138 int *union_map_is_strict_subset(__isl_take isl_union_map *map1,
139 __isl_take isl_union_map *map2)
141 int res = isl_union_map_is_strict_subset(map1, map2);
142 isl_union_map_free(map1);
143 isl_union_map_free(map2);
144 return isl_bool_from_int(res);
146 int *union_set_is_strict_subset(__isl_take isl_union_set *set1,
147 __isl_take isl_union_set *set2)
149 return union_map_is_strict_subset((isl_union_map *)set1,
150 (isl_union_map *)set2);
153 int *union_map_is_superset(__isl_take isl_union_map *map1,
154 __isl_take isl_union_map *map2)
156 return union_map_is_subset(map2, map1);
158 int *union_set_is_superset(__isl_take isl_union_set *set1,
159 __isl_take isl_union_set *set2)
161 return union_set_is_subset(set2, set1);
164 int *union_map_is_strict_superset(__isl_take isl_union_map *map1,
165 __isl_take isl_union_map *map2)
167 return union_map_is_strict_subset(map2, map1);
169 int *union_set_is_strict_superset(__isl_take isl_union_set *set1,
170 __isl_take isl_union_set *set2)
172 return union_set_is_strict_subset(set2, set1);
175 extern struct isl_obj_vtable isl_obj_list_vtable;
176 #define isl_obj_list (&isl_obj_list_vtable)
178 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
179 struct isc_bin_op {
180 enum isl_token_type op;
181 isl_obj_type lhs;
182 isl_obj_type rhs;
183 isl_obj_type res;
184 isc_bin_op_fn fn;
186 struct isc_named_bin_op {
187 char *name;
188 struct isc_bin_op op;
191 struct iscc_at {
192 isl_union_pw_qpolynomial *upwqp;
193 isl_union_pw_qpolynomial *res;
196 static int eval_at(__isl_take isl_point *pnt, void *user)
198 struct iscc_at *at = (struct iscc_at *) user;
199 isl_qpolynomial *qp;
200 isl_set *set;
202 set = isl_set_from_point(isl_point_copy(pnt));
203 qp = isl_union_pw_qpolynomial_eval(
204 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
206 at->res = isl_union_pw_qpolynomial_add(at->res,
207 isl_union_pw_qpolynomial_from_pw_qpolynomial(
208 isl_pw_qpolynomial_alloc(set, qp)));
210 return 0;
213 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
214 __isl_take isl_union_pw_qpolynomial *upwqp,
215 __isl_take isl_union_set *uset)
217 struct iscc_at at;
219 at.upwqp = upwqp;
220 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset));
222 isl_union_set_foreach_point(uset, eval_at, &at);
224 isl_union_pw_qpolynomial_free(upwqp);
225 isl_union_set_free(uset);
227 return at.res;
230 struct iscc_fold_at {
231 isl_union_pw_qpolynomial_fold *upwf;
232 isl_union_pw_qpolynomial *res;
235 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
237 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
238 isl_qpolynomial *qp;
239 isl_set *set;
241 set = isl_set_from_point(isl_point_copy(pnt));
242 qp = isl_union_pw_qpolynomial_fold_eval(
243 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
245 at->res = isl_union_pw_qpolynomial_add(at->res,
246 isl_union_pw_qpolynomial_from_pw_qpolynomial(
247 isl_pw_qpolynomial_alloc(set, qp)));
249 return 0;
252 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
253 __isl_take isl_union_pw_qpolynomial_fold *upwf,
254 __isl_take isl_union_set *uset)
256 struct iscc_fold_at at;
258 at.upwf = upwf;
259 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset));
261 isl_union_set_foreach_point(uset, eval_fold_at, &at);
263 isl_union_pw_qpolynomial_fold_free(upwf);
264 isl_union_set_free(uset);
266 return at.res;
269 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
270 __isl_take isl_union_pw_qpolynomial *upwqp,
271 __isl_take isl_union_pw_qpolynomial_fold *upwf)
273 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf,
274 upwqp);
277 static __isl_give struct isl_list *union_map_apply_union_pw_qpolynomial_fold(
278 __isl_take isl_union_map *umap,
279 __isl_take isl_union_pw_qpolynomial_fold *upwf)
281 isl_ctx *ctx;
282 struct isl_list *list;
283 int tight;
285 ctx = isl_union_map_get_ctx(umap);
286 list = isl_list_alloc(ctx, 2);
287 if (!list)
288 goto error2;
290 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
291 list->obj[0].v = isl_union_map_apply_union_pw_qpolynomial_fold(umap,
292 upwf, &tight);
293 list->obj[1].type = isl_obj_bool;
294 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
295 if (tight < 0 || !list->obj[0].v)
296 goto error;
298 return list;
299 error2:
300 isl_union_map_free(umap);
301 isl_union_pw_qpolynomial_fold_free(upwf);
302 error:
303 isl_list_free(list);
304 return NULL;
307 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_int_mul(
308 __isl_take isl_union_pw_qpolynomial *upwqp, __isl_take isl_int_obj *i)
310 isl_int v;
312 if (!i)
313 goto error;
315 isl_int_init(v);
316 isl_int_obj_get_int(i, &v);
317 upwqp = isl_union_pw_qpolynomial_mul_isl_int(upwqp, v);
318 isl_int_clear(v);
320 isl_int_obj_free(i);
322 return upwqp;
323 error:
324 isl_union_pw_qpolynomial_free(upwqp);
325 return NULL;
328 static __isl_give isl_union_pw_qpolynomial *int_union_pw_qpolynomial_mul(
329 __isl_take isl_int_obj *i, __isl_take isl_union_pw_qpolynomial *upwqp)
331 return union_pw_qpolynomial_int_mul(upwqp, i);
334 static __isl_give isl_union_pw_qpolynomial_fold *union_pw_qpolynomial_fold_int_mul(
335 __isl_take isl_union_pw_qpolynomial_fold *upwf,
336 __isl_take isl_int_obj *i)
338 isl_int v;
340 if (!i)
341 goto error;
343 isl_int_init(v);
344 isl_int_obj_get_int(i, &v);
345 upwf = isl_union_pw_qpolynomial_fold_mul_isl_int(upwf, v);
346 isl_int_clear(v);
348 isl_int_obj_free(i);
350 return upwf;
351 error:
352 isl_union_pw_qpolynomial_fold_free(upwf);
353 return NULL;
356 static __isl_give isl_union_pw_qpolynomial_fold *int_union_pw_qpolynomial_fold_mul(
357 __isl_take isl_int_obj *i,
358 __isl_take isl_union_pw_qpolynomial_fold *upwf)
360 return union_pw_qpolynomial_fold_int_mul(upwf, i);
363 struct isc_bin_op bin_ops[] = {
364 { '+', isl_obj_int, isl_obj_int, isl_obj_int,
365 (isc_bin_op_fn) &isl_int_obj_add },
366 { '-', isl_obj_int, isl_obj_int, isl_obj_int,
367 (isc_bin_op_fn) &isl_int_obj_sub },
368 { '*', isl_obj_int, isl_obj_int, isl_obj_int,
369 (isc_bin_op_fn) &isl_int_obj_mul },
370 { '+', isl_obj_union_set, isl_obj_union_set,
371 isl_obj_union_set,
372 (isc_bin_op_fn) &isl_union_set_union },
373 { '+', isl_obj_union_map, isl_obj_union_map,
374 isl_obj_union_map,
375 (isc_bin_op_fn) &isl_union_map_union },
376 { '-', isl_obj_union_set, isl_obj_union_set,
377 isl_obj_union_set,
378 (isc_bin_op_fn) &isl_union_set_subtract },
379 { '-', isl_obj_union_map, isl_obj_union_map,
380 isl_obj_union_map,
381 (isc_bin_op_fn) &isl_union_map_subtract },
382 { '*', isl_obj_union_set, isl_obj_union_set,
383 isl_obj_union_set,
384 (isc_bin_op_fn) &isl_union_set_intersect },
385 { '*', isl_obj_union_map, isl_obj_union_map,
386 isl_obj_union_map,
387 (isc_bin_op_fn) &isl_union_map_intersect },
388 { '*', isl_obj_union_map, isl_obj_union_set,
389 isl_obj_union_map,
390 (isc_bin_op_fn) &isl_union_map_intersect_domain },
391 { '.', isl_obj_union_map, isl_obj_union_map,
392 isl_obj_union_map,
393 (isc_bin_op_fn) &isl_union_map_apply_range },
394 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial,
395 isl_obj_union_pw_qpolynomial,
396 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
397 { '.', isl_obj_union_map, isl_obj_union_pw_qpolynomial_fold,
398 isl_obj_list,
399 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold },
400 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
401 isl_obj_union_map,
402 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
403 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
404 (isc_bin_op_fn) &union_set_is_equal },
405 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
406 (isc_bin_op_fn) &union_map_is_equal },
407 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
408 isl_obj_bool, (isc_bin_op_fn) &union_set_is_subset },
409 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
410 isl_obj_bool, (isc_bin_op_fn) &union_map_is_subset },
411 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
412 isl_obj_bool, (isc_bin_op_fn) &union_set_is_strict_subset },
413 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
414 isl_obj_bool, (isc_bin_op_fn) &union_map_is_strict_subset },
415 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
416 isl_obj_bool, (isc_bin_op_fn) &union_set_is_superset },
417 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
418 isl_obj_bool, (isc_bin_op_fn) &union_map_is_superset },
419 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
420 isl_obj_bool, (isc_bin_op_fn) &union_set_is_strict_superset },
421 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
422 isl_obj_bool, (isc_bin_op_fn) &union_map_is_strict_superset },
423 { ISL_TOKEN_LEX_LE, isl_obj_union_set, isl_obj_union_set,
424 isl_obj_union_map,
425 (isc_bin_op_fn) &isl_union_set_lex_le_union_set },
426 { ISL_TOKEN_LEX_LT, isl_obj_union_set, isl_obj_union_set,
427 isl_obj_union_map,
428 (isc_bin_op_fn) &isl_union_set_lex_lt_union_set },
429 { ISL_TOKEN_LEX_GE, isl_obj_union_set, isl_obj_union_set,
430 isl_obj_union_map,
431 (isc_bin_op_fn) &isl_union_set_lex_ge_union_set },
432 { ISL_TOKEN_LEX_GT, isl_obj_union_set, isl_obj_union_set,
433 isl_obj_union_map,
434 (isc_bin_op_fn) &isl_union_set_lex_gt_union_set },
435 { ISL_TOKEN_LEX_LE, isl_obj_union_map, isl_obj_union_map,
436 isl_obj_union_map,
437 (isc_bin_op_fn) &isl_union_map_lex_le_union_map },
438 { ISL_TOKEN_LEX_LT, isl_obj_union_map, isl_obj_union_map,
439 isl_obj_union_map,
440 (isc_bin_op_fn) &isl_union_map_lex_lt_union_map },
441 { ISL_TOKEN_LEX_GE, isl_obj_union_map, isl_obj_union_map,
442 isl_obj_union_map,
443 (isc_bin_op_fn) &isl_union_map_lex_ge_union_map },
444 { ISL_TOKEN_LEX_GT, isl_obj_union_map, isl_obj_union_map,
445 isl_obj_union_map,
446 (isc_bin_op_fn) &isl_union_map_lex_gt_union_map },
447 { '.', isl_obj_union_pw_qpolynomial_fold,
448 isl_obj_union_pw_qpolynomial_fold,
449 isl_obj_union_pw_qpolynomial_fold,
450 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_fold },
451 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
452 isl_obj_union_pw_qpolynomial,
453 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
454 { '+', isl_obj_union_pw_qpolynomial,
455 isl_obj_union_pw_qpolynomial_fold,
456 isl_obj_union_pw_qpolynomial_fold,
457 (isc_bin_op_fn) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold },
458 { '+', isl_obj_union_pw_qpolynomial_fold,
459 isl_obj_union_pw_qpolynomial,
460 isl_obj_union_pw_qpolynomial_fold,
461 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial },
462 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
463 isl_obj_union_pw_qpolynomial,
464 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
465 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial,
466 isl_obj_union_pw_qpolynomial,
467 (isc_bin_op_fn) &int_union_pw_qpolynomial_mul },
468 { '*', isl_obj_union_pw_qpolynomial, isl_obj_int,
469 isl_obj_union_pw_qpolynomial,
470 (isc_bin_op_fn) &union_pw_qpolynomial_int_mul },
471 { '*', isl_obj_int, isl_obj_union_pw_qpolynomial_fold,
472 isl_obj_union_pw_qpolynomial_fold,
473 (isc_bin_op_fn) &int_union_pw_qpolynomial_fold_mul },
474 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_int,
475 isl_obj_union_pw_qpolynomial_fold,
476 (isc_bin_op_fn) &union_pw_qpolynomial_fold_int_mul },
477 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
478 isl_obj_union_pw_qpolynomial,
479 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
480 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
481 isl_obj_union_pw_qpolynomial,
482 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
483 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
484 isl_obj_union_pw_qpolynomial_fold,
485 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
486 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
487 isl_obj_union_pw_qpolynomial,
488 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
489 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
490 isl_obj_union_pw_qpolynomial,
491 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
492 { '%', isl_obj_union_set, isl_obj_union_set,
493 isl_obj_union_set,
494 (isc_bin_op_fn) &isl_union_set_gist },
495 { '%', isl_obj_union_map, isl_obj_union_map,
496 isl_obj_union_map,
497 (isc_bin_op_fn) &isl_union_map_gist },
498 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
499 isl_obj_union_pw_qpolynomial,
500 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
501 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
502 isl_obj_union_pw_qpolynomial_fold,
503 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
504 { '+', isl_obj_str, isl_obj_str, isl_obj_str,
505 (isc_bin_op_fn) &isl_str_concat },
509 static __isl_give isl_union_map *map_after_map(__isl_take isl_union_map *umap1,
510 __isl_take isl_union_map *umap2)
512 return isl_union_map_apply_range(umap2, umap1);
515 static __isl_give isl_union_pw_qpolynomial *qpolynomial_after_map(
516 __isl_take isl_union_pw_qpolynomial *upwqp,
517 __isl_take isl_union_map *umap)
519 return isl_union_map_apply_union_pw_qpolynomial(umap, upwqp);
522 static __isl_give struct isl_list *qpolynomial_fold_after_map(
523 __isl_take isl_union_pw_qpolynomial_fold *upwf,
524 __isl_take isl_union_map *umap)
526 return union_map_apply_union_pw_qpolynomial_fold(umap, upwf);
529 struct isc_named_bin_op named_bin_ops[] = {
530 { "after", { -1, isl_obj_union_map, isl_obj_union_map,
531 isl_obj_union_map,
532 (isc_bin_op_fn) &map_after_map } },
533 { "after", { -1, isl_obj_union_pw_qpolynomial,
534 isl_obj_union_map, isl_obj_union_pw_qpolynomial,
535 (isc_bin_op_fn) &qpolynomial_after_map } },
536 { "after", { -1, isl_obj_union_pw_qpolynomial_fold,
537 isl_obj_union_map, isl_obj_list,
538 (isc_bin_op_fn) &qpolynomial_fold_after_map } },
539 { "before", { -1, isl_obj_union_map, isl_obj_union_map,
540 isl_obj_union_map,
541 (isc_bin_op_fn) &isl_union_map_apply_range } },
542 { "before", { -1, isl_obj_union_map,
543 isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
544 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial } },
545 { "before", { -1, isl_obj_union_map,
546 isl_obj_union_pw_qpolynomial_fold, isl_obj_list,
547 (isc_bin_op_fn) &union_map_apply_union_pw_qpolynomial_fold } },
548 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
549 isl_obj_union_set,
550 (isc_bin_op_fn) &isl_union_set_product } },
551 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
552 isl_obj_union_map,
553 (isc_bin_op_fn) &isl_union_map_product } },
554 NULL
557 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
559 return isl_set_from_basic_set(isl_union_set_sample(uset));
562 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
564 return isl_map_from_basic_map(isl_union_map_sample(umap));
567 static __isl_give struct isl_list *union_map_power(
568 __isl_take isl_union_map *umap)
570 isl_ctx *ctx;
571 struct isl_list *list;
572 int exact;
574 ctx = isl_union_map_get_ctx(umap);
575 list = isl_list_alloc(ctx, 2);
576 if (!list)
577 goto error2;
579 list->obj[0].type = isl_obj_union_map;
580 list->obj[0].v = isl_union_map_power(umap, &exact);
581 list->obj[1].type = isl_obj_bool;
582 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
583 if (exact < 0 || !list->obj[0].v)
584 goto error;
586 return list;
587 error2:
588 isl_union_map_free(umap);
589 error:
590 isl_list_free(list);
591 return NULL;
594 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
595 __isl_take isl_union_pw_qpolynomial *upwqp)
597 isl_ctx *ctx;
598 struct isl_list *list;
599 int tight;
601 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
602 list = isl_list_alloc(ctx, 2);
603 if (!list)
604 goto error2;
606 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
607 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
608 isl_fold_max, &tight);
609 list->obj[1].type = isl_obj_bool;
610 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
611 if (tight < 0 || !list->obj[0].v)
612 goto error;
614 return list;
615 error2:
616 isl_union_pw_qpolynomial_free(upwqp);
617 error:
618 isl_list_free(list);
619 return NULL;
622 #ifdef HAVE_CLOOG
623 void *map_codegen(void *arg)
625 isl_dim *dim;
626 isl_union_map *umap = (isl_union_map *)arg;
627 isl_ctx *ctx = isl_union_map_get_ctx(umap);
628 CloogState *state;
629 CloogOptions *options;
630 CloogDomain *context;
631 CloogUnionDomain *ud;
632 CloogInput *input;
633 struct clast_stmt *stmt;
635 state = cloog_isl_state_malloc(ctx);
636 options = cloog_options_malloc(state);
637 options->language = LANGUAGE_C;
638 options->strides = 1;
640 ud = cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap));
642 dim = isl_union_map_get_dim(umap);
643 context = cloog_domain_from_isl_set(isl_set_universe(dim));
645 input = cloog_input_alloc(context, ud);
647 stmt = cloog_clast_create_from_input(input, options);
648 clast_pprint(stdout, stmt, 0, options);
649 cloog_clast_free(stmt);
651 error:
652 cloog_options_free(options);
653 cloog_state_free(state);
654 isl_union_map_free(umap);
655 return NULL;
658 void *set_codegen(void *arg)
660 isl_dim *dim;
661 isl_union_set *uset = (isl_union_set *)arg;
662 isl_ctx *ctx = isl_union_set_get_ctx(uset);
663 CloogState *state;
664 CloogOptions *options;
665 CloogDomain *context;
666 CloogUnionDomain *ud;
667 CloogInput *input;
668 struct clast_stmt *stmt;
670 if (isl_union_set_n_set(uset) > 1)
671 isl_die(ctx, isl_error_invalid,
672 "code generation for more than one domain "
673 "requires a schedule", goto error);
675 state = cloog_isl_state_malloc(ctx);
676 options = cloog_options_malloc(state);
677 options->language = LANGUAGE_C;
678 options->strides = 1;
680 ud = cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset));
682 dim = isl_union_set_get_dim(uset);
683 context = cloog_domain_from_isl_set(isl_set_universe(dim));
685 input = cloog_input_alloc(context, ud);
687 stmt = cloog_clast_create_from_input(input, options);
688 clast_pprint(stdout, stmt, 0, options);
689 cloog_clast_free(stmt);
691 cloog_options_free(options);
692 cloog_state_free(state);
693 error:
694 isl_union_set_free(uset);
695 return NULL;
697 #endif
699 static int add_point(__isl_take isl_point *pnt, void *user)
701 isl_union_set **scan = (isl_union_set **) user;
703 *scan = isl_union_set_add_set(*scan, isl_set_from_point(pnt));
705 return 0;
708 static __isl_give isl_union_set *union_set_scan(__isl_take isl_union_set *uset)
710 isl_union_set *scan;
712 scan = isl_union_set_empty(isl_union_set_get_dim(uset));
714 if (isl_union_set_foreach_point(uset, add_point, &scan) < 0) {
715 isl_union_set_free(scan);
716 return uset;
719 isl_union_set_free(uset);
720 return scan;
723 static __isl_give isl_union_map *union_map_scan(__isl_take isl_union_map *umap)
725 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap)));
728 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_poly(
729 __isl_take isl_union_pw_qpolynomial *upwqp)
731 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 0);
734 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_lpoly(
735 __isl_take isl_union_pw_qpolynomial *upwqp)
737 return isl_union_pw_qpolynomial_to_polynomial(upwqp, -1);
740 static __isl_give isl_union_pw_qpolynomial *union_pw_qpolynomial_upoly(
741 __isl_take isl_union_pw_qpolynomial *upwqp)
743 return isl_union_pw_qpolynomial_to_polynomial(upwqp, 1);
746 typedef void *(*isc_un_op_fn)(void *arg);
747 struct isc_un_op {
748 enum isl_token_type op;
749 isl_obj_type arg;
750 isl_obj_type res;
751 isc_un_op_fn fn;
753 struct isc_named_un_op {
754 char *name;
755 struct isc_un_op op;
757 struct isc_named_un_op named_un_ops[] = {
758 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
759 (isc_un_op_fn) &isl_union_map_affine_hull } },
760 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
761 (isc_un_op_fn) &isl_union_set_affine_hull } },
762 {"card", { -1, isl_obj_union_set,
763 isl_obj_union_pw_qpolynomial,
764 (isc_un_op_fn) &isl_union_set_card } },
765 {"card", { -1, isl_obj_union_map,
766 isl_obj_union_pw_qpolynomial,
767 (isc_un_op_fn) &isl_union_map_card } },
768 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
769 (isc_un_op_fn) &isl_union_set_coalesce } },
770 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
771 (isc_un_op_fn) &isl_union_map_coalesce } },
772 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
773 isl_obj_union_pw_qpolynomial,
774 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
775 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
776 isl_obj_union_pw_qpolynomial_fold,
777 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
778 #ifdef HAVE_CLOOG
779 {"codegen", { -1, isl_obj_union_set, isl_obj_none,
780 &set_codegen } },
781 {"codegen", { -1, isl_obj_union_map, isl_obj_none,
782 &map_codegen } },
783 #endif
784 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
785 (isc_un_op_fn) &isl_union_map_deltas } },
786 {"deltas_map", { -1, isl_obj_union_map, isl_obj_union_map,
787 (isc_un_op_fn) &isl_union_map_deltas_map } },
788 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
789 (isc_un_op_fn) &isl_union_map_domain } },
790 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
791 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
792 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
793 isl_obj_union_set,
794 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
795 {"domain", { -1, isl_obj_union_map, isl_obj_union_set,
796 (isc_un_op_fn) &isl_union_map_domain } },
797 {"domain", { -1, isl_obj_union_pw_qpolynomial,
798 isl_obj_union_set,
799 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
800 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold,
801 isl_obj_union_set,
802 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
803 {"domain_map", { -1, isl_obj_union_map, isl_obj_union_map,
804 (isc_un_op_fn) &isl_union_map_domain_map } },
805 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
806 (isc_un_op_fn) &isl_union_map_range } },
807 {"range", { -1, isl_obj_union_map, isl_obj_union_set,
808 (isc_un_op_fn) &isl_union_map_range } },
809 {"range_map", { -1, isl_obj_union_map, isl_obj_union_map,
810 (isc_un_op_fn) &isl_union_map_range_map } },
811 {"identity", { -1, isl_obj_union_set, isl_obj_union_map,
812 (isc_un_op_fn) &isl_union_set_identity } },
813 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
814 (isc_un_op_fn) &isl_union_map_lexmin } },
815 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
816 (isc_un_op_fn) &isl_union_map_lexmax } },
817 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
818 (isc_un_op_fn) &isl_union_set_lexmin } },
819 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
820 (isc_un_op_fn) &isl_union_set_lexmax } },
821 {"poly", { -1, isl_obj_union_map, isl_obj_union_map,
822 (isc_un_op_fn) &isl_union_map_polyhedral_hull } },
823 {"poly", { -1, isl_obj_union_set, isl_obj_union_set,
824 (isc_un_op_fn) &isl_union_set_polyhedral_hull } },
825 {"poly", { -1, isl_obj_union_pw_qpolynomial,
826 isl_obj_union_pw_qpolynomial,
827 (isc_un_op_fn) &union_pw_qpolynomial_poly } },
828 {"lpoly", { -1, isl_obj_union_pw_qpolynomial,
829 isl_obj_union_pw_qpolynomial,
830 (isc_un_op_fn) &union_pw_qpolynomial_lpoly } },
831 {"upoly", { -1, isl_obj_union_pw_qpolynomial,
832 isl_obj_union_pw_qpolynomial,
833 (isc_un_op_fn) &union_pw_qpolynomial_upoly } },
834 {"pow", { -1, isl_obj_union_map, isl_obj_list,
835 (isc_un_op_fn) &union_map_power } },
836 {"sample", { -1, isl_obj_union_set, isl_obj_set,
837 (isc_un_op_fn) &union_set_sample } },
838 {"sample", { -1, isl_obj_union_map, isl_obj_map,
839 (isc_un_op_fn) &union_map_sample } },
840 {"scan", { -1, isl_obj_union_set, isl_obj_union_set,
841 (isc_un_op_fn) &union_set_scan } },
842 {"scan", { -1, isl_obj_union_map, isl_obj_union_map,
843 (isc_un_op_fn) &union_map_scan } },
844 {"sum", { -1, isl_obj_union_pw_qpolynomial,
845 isl_obj_union_pw_qpolynomial,
846 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
847 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
848 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
849 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
850 (isc_un_op_fn) &isl_union_set_unwrap } },
851 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
852 (isc_un_op_fn) &isl_union_map_wrap } },
853 {"zip", { -1, isl_obj_union_map, isl_obj_union_map,
854 (isc_un_op_fn) &isl_union_map_zip } },
855 NULL
858 struct isl_named_obj {
859 char *name;
860 struct isl_obj obj;
863 static void free_obj(struct isl_obj obj)
865 obj.type->free(obj.v);
868 static int same_name(const void *entry, const void *val)
870 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
872 return !strcmp(named->name, val);
875 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
876 char *name, struct isl_obj obj)
878 struct isl_hash_table_entry *entry;
879 uint32_t name_hash;
880 struct isl_named_obj *named;
882 name_hash = isl_hash_string(isl_hash_init(), name);
883 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
884 if (!entry)
885 goto error;
886 if (entry->data) {
887 named = entry->data;
888 free_obj(named->obj);
889 free(name);
890 } else {
891 named = isl_alloc_type(ctx, struct isl_named_obj);
892 if (!named)
893 goto error;
894 named->name = name;
895 entry->data = named;
897 named->obj = obj;
899 return 0;
900 error:
901 free_obj(obj);
902 free(name);
903 return -1;
906 static struct isl_obj stored_obj(struct isl_ctx *ctx,
907 struct isl_hash_table *table, char *name)
909 struct isl_obj obj = { isl_obj_none, NULL };
910 struct isl_hash_table_entry *entry;
911 uint32_t name_hash;
913 name_hash = isl_hash_string(isl_hash_init(), name);
914 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
915 if (entry) {
916 struct isl_named_obj *named;
917 named = entry->data;
918 obj = named->obj;
919 } else if (isdigit(name[0]))
920 fprintf(stderr, "unknown identifier '$%s'\n", name);
921 else
922 fprintf(stderr, "unknown identifier '%s'\n", name);
924 free(name);
925 obj.v = obj.type->copy(obj.v);
926 return obj;
929 static int is_subtype(struct isl_obj obj, isl_obj_type super)
931 if (obj.type == super)
932 return 1;
933 if (obj.type == isl_obj_map && super == isl_obj_union_map)
934 return 1;
935 if (obj.type == isl_obj_set && super == isl_obj_union_set)
936 return 1;
937 if (obj.type == isl_obj_pw_qpolynomial &&
938 super == isl_obj_union_pw_qpolynomial)
939 return 1;
940 if (obj.type == isl_obj_pw_qpolynomial_fold &&
941 super == isl_obj_union_pw_qpolynomial_fold)
942 return 1;
943 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
944 return 1;
945 if (obj.type == isl_obj_list) {
946 struct isl_list *list = obj.v;
947 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
948 return is_subtype(list->obj[0], super);
950 if (super == isl_obj_str)
951 return 1;
952 return 0;
955 static struct isl_obj obj_at(struct isl_obj obj, int i)
957 struct isl_list *list = obj.v;
959 obj = list->obj[i];
960 obj.v = obj.type->copy(obj.v);
962 isl_list_free(list);
964 return obj;
967 static struct isl_obj convert(isl_ctx *ctx, struct isl_obj obj,
968 isl_obj_type type)
970 if (obj.type == type)
971 return obj;
972 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
973 obj.type = isl_obj_union_map;
974 obj.v = isl_union_map_from_map(obj.v);
975 return obj;
977 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
978 obj.type = isl_obj_union_set;
979 obj.v = isl_union_set_from_set(obj.v);
980 return obj;
982 if (obj.type == isl_obj_pw_qpolynomial &&
983 type == isl_obj_union_pw_qpolynomial) {
984 obj.type = isl_obj_union_pw_qpolynomial;
985 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
986 return obj;
988 if (obj.type == isl_obj_pw_qpolynomial_fold &&
989 type == isl_obj_union_pw_qpolynomial_fold) {
990 obj.type = isl_obj_union_pw_qpolynomial_fold;
991 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
992 return obj;
994 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
995 if (type == isl_obj_union_map) {
996 obj.type = isl_obj_union_map;
997 return obj;
999 if (type == isl_obj_union_pw_qpolynomial) {
1000 isl_dim *dim = isl_union_set_get_dim(obj.v);
1001 isl_union_set_free(obj.v);
1002 obj.v = isl_union_pw_qpolynomial_zero(dim);
1003 obj.type = isl_obj_union_pw_qpolynomial;
1004 return obj;
1006 if (type == isl_obj_union_pw_qpolynomial_fold) {
1007 isl_dim *dim = isl_union_set_get_dim(obj.v);
1008 isl_union_set_free(obj.v);
1009 obj.v = isl_union_pw_qpolynomial_fold_zero(dim,
1010 isl_fold_list);
1011 obj.type = isl_obj_union_pw_qpolynomial_fold;
1012 return obj;
1015 if (obj.type == isl_obj_list) {
1016 struct isl_list *list = obj.v;
1017 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1018 return convert(ctx, obj_at(obj, 0), type);
1020 if (type == isl_obj_str) {
1021 isl_str *str;
1022 isl_printer *p;
1023 char *s;
1025 p = isl_printer_to_str(ctx);
1026 if (!p)
1027 goto error;
1028 p = obj.type->print(p, obj.v);
1029 s = isl_printer_get_str(p);
1030 isl_printer_free(p);
1032 str = isl_str_from_string(ctx, s);
1033 if (!str)
1034 goto error;
1035 free_obj(obj);
1036 obj.v = str;
1037 obj.type = isl_obj_str;
1038 return obj;
1041 error:
1042 free_obj(obj);
1043 obj.type = isl_obj_none;
1044 obj.v = NULL;
1045 return obj;
1048 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
1049 struct isl_obj lhs)
1051 int i;
1052 struct isl_token *tok;
1054 tok = isl_stream_next_token(s);
1055 if (!tok)
1056 return NULL;
1058 for (i = 0; ; ++i) {
1059 if (!bin_ops[i].op)
1060 break;
1061 if (bin_ops[i].op != tok->type)
1062 continue;
1063 if (!is_subtype(lhs, bin_ops[i].lhs))
1064 continue;
1066 isl_token_free(tok);
1067 return &bin_ops[i];
1070 for (i = 0; ; ++i) {
1071 if (!named_bin_ops[i].name)
1072 break;
1073 if (named_bin_ops[i].op.op != tok->type)
1074 continue;
1075 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1076 continue;
1078 isl_token_free(tok);
1079 return &named_bin_ops[i].op;
1082 isl_stream_push_token(s, tok);
1084 return NULL;
1087 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
1089 int i;
1090 struct isl_token *tok;
1092 tok = isl_stream_next_token(s);
1093 if (!tok)
1094 return NULL;
1096 for (i = 0; ; ++i) {
1097 if (!named_un_ops[i].name)
1098 break;
1099 if (named_un_ops[i].op.op != tok->type)
1100 continue;
1102 isl_token_free(tok);
1103 return &named_un_ops[i].op;
1106 isl_stream_push_token(s, tok);
1108 return NULL;
1111 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
1112 struct isl_obj arg)
1114 int i;
1116 for (i = 0; ; ++i) {
1117 if (!named_un_ops[i].name)
1118 break;
1119 if (named_un_ops[i].op.op != like->op)
1120 continue;
1121 if (!is_subtype(arg, named_un_ops[i].op.arg))
1122 continue;
1124 return &named_un_ops[i].op;
1127 return NULL;
1130 static int is_assign(struct isl_stream *s)
1132 struct isl_token *tok;
1133 struct isl_token *tok2;
1134 int assign;
1136 tok = isl_stream_next_token(s);
1137 if (!tok)
1138 return 0;
1139 if (tok->type != ISL_TOKEN_IDENT) {
1140 isl_stream_push_token(s, tok);
1141 return 0;
1144 tok2 = isl_stream_next_token(s);
1145 if (!tok2) {
1146 isl_stream_push_token(s, tok);
1147 return 0;
1149 assign = tok2->type == ISL_TOKEN_DEF;
1150 isl_stream_push_token(s, tok2);
1151 isl_stream_push_token(s, tok);
1153 return assign;
1156 static struct isl_obj read_obj(struct isl_stream *s,
1157 struct isl_hash_table *table);
1158 static struct isl_obj read_expr(struct isl_stream *s,
1159 struct isl_hash_table *table);
1161 static struct isl_obj read_un_op_expr(struct isl_stream *s,
1162 struct isl_hash_table *table, struct isc_un_op *op)
1164 struct isl_obj obj = { isl_obj_none, NULL };
1166 obj = read_obj(s, table);
1167 if (!obj.v)
1168 goto error;
1170 op = find_matching_un_op(op, obj);
1172 if (!op)
1173 isl_die(s->ctx, isl_error_invalid,
1174 "no such unary operator defined on given operand",
1175 goto error);
1177 obj = convert(s->ctx, obj, op->arg);
1178 obj.v = op->fn(obj.v);
1179 obj.type = op->res;
1181 return obj;
1182 error:
1183 free_obj(obj);
1184 obj.type = isl_obj_none;
1185 obj.v = NULL;
1186 return obj;
1189 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
1191 struct isl_list *list;
1192 int exact;
1194 if (obj.type != isl_obj_union_map)
1195 obj = convert(ctx, obj, isl_obj_union_map);
1196 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
1197 list = isl_list_alloc(ctx, 2);
1198 if (!list)
1199 goto error;
1201 list->obj[0].type = isl_obj_union_map;
1202 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
1203 list->obj[1].type = isl_obj_bool;
1204 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
1205 obj.v = list;
1206 obj.type = isl_obj_list;
1207 if (exact < 0 || !list->obj[0].v)
1208 goto error;
1210 return obj;
1211 error:
1212 free_obj(obj);
1213 obj.type = isl_obj_none;
1214 obj.v = NULL;
1215 return obj;
1218 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
1220 struct isl_list *list = obj.v;
1221 struct isl_token *tok;
1222 int i;
1224 tok = isl_stream_next_token(s);
1225 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1226 isl_stream_error(s, tok, "expecting index");
1227 if (tok)
1228 isl_stream_push_token(s, tok);
1229 goto error;
1231 i = isl_int_get_si(tok->u.v);
1232 isl_token_free(tok);
1233 isl_assert(s->ctx, i < list->n, goto error);
1234 if (isl_stream_eat(s, ']'))
1235 goto error;
1237 return obj_at(obj, i);
1238 error:
1239 free_obj(obj);
1240 obj.type = isl_obj_none;
1241 obj.v = NULL;
1242 return obj;
1245 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
1246 struct isl_hash_table *table)
1248 struct isl_obj obj;
1250 obj = read_expr(s, table);
1251 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
1252 is_subtype(obj, isl_obj_union_map), goto error);
1254 if (obj.type == isl_obj_list) {
1255 struct isl_list *list = obj.v;
1256 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1257 obj = obj_at(obj, 0);
1259 if (obj.type == isl_obj_set)
1260 obj = convert(s->ctx, obj, isl_obj_union_set);
1261 else if (obj.type == isl_obj_map)
1262 obj = convert(s->ctx, obj, isl_obj_union_map);
1263 if (obj.type == isl_obj_union_set) {
1264 obj.v = isl_union_set_apply(obj.v, umap);
1265 } else
1266 obj.v = isl_union_map_apply_range(obj.v, umap);
1267 if (!obj.v)
1268 goto error2;
1270 if (isl_stream_eat(s, ')'))
1271 goto error2;
1273 return obj;
1274 error:
1275 isl_union_map_free(umap);
1276 error2:
1277 free_obj(obj);
1278 obj.type = isl_obj_none;
1279 obj.v = NULL;
1280 return obj;
1283 static struct isl_obj apply_fun(struct isl_stream *s,
1284 struct isl_obj obj, struct isl_hash_table *table)
1286 struct isl_obj arg;
1288 arg = read_expr(s, table);
1289 if (!is_subtype(arg, isl_obj_union_map) &&
1290 !is_subtype(arg, isl_obj_union_set))
1291 isl_die(s->ctx, isl_error_invalid,
1292 "expecting set of map argument", goto error);
1294 if (arg.type == isl_obj_list) {
1295 struct isl_list *list = arg.v;
1296 if (list->n == 2 && list->obj[1].type == isl_obj_bool)
1297 arg = obj_at(arg, 0);
1299 if (arg.type == isl_obj_set)
1300 arg = convert(s->ctx, arg, isl_obj_union_set);
1301 else if (arg.type == isl_obj_map)
1302 arg = convert(s->ctx, arg, isl_obj_union_map);
1303 if (arg.type == isl_obj_union_set) {
1304 arg.v = isl_union_map_from_range(arg.v);
1305 arg.type = isl_obj_union_map;
1307 if (obj.type == isl_obj_union_pw_qpolynomial) {
1308 obj.v = isl_union_map_apply_union_pw_qpolynomial(arg.v, obj.v);
1309 } else {
1310 obj.type = isl_obj_list;
1311 obj.v = union_map_apply_union_pw_qpolynomial_fold(arg.v, obj.v);
1313 if (!obj.v)
1314 goto error2;
1316 if (isl_stream_eat(s, ')'))
1317 goto error2;
1319 return obj;
1320 error:
1321 free_obj(arg);
1322 error2:
1323 free_obj(obj);
1324 obj.type = isl_obj_none;
1325 obj.v = NULL;
1326 return obj;
1329 struct add_vertex_data {
1330 struct isl_list *list;
1331 int i;
1334 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
1336 struct add_vertex_data *data = (struct add_vertex_data *)user;
1337 isl_basic_set *expr;
1339 expr = isl_vertex_get_expr(vertex);
1341 data->list->obj[data->i].type = isl_obj_set;
1342 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
1343 data->i++;
1345 isl_vertex_free(vertex);
1347 return 0;
1350 static int set_vertices(__isl_take isl_set *set, void *user)
1352 isl_ctx *ctx;
1353 isl_basic_set *hull;
1354 isl_vertices *vertices = NULL;
1355 struct isl_list *list = NULL;
1356 int r;
1357 struct add_vertex_data *data = (struct add_vertex_data *)user;
1359 set = isl_set_remove_divs(set);
1360 hull = isl_set_convex_hull(set);
1361 vertices = isl_basic_set_compute_vertices(hull);
1362 isl_basic_set_free(hull);
1364 list = data->list;
1366 ctx = isl_vertices_get_ctx(vertices);
1367 data->list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1368 if (!data->list)
1369 goto error;
1371 data->i = 0;
1372 r = isl_vertices_foreach_vertex(vertices, &add_vertex, user);
1374 data->list = isl_list_concat(list, data->list);
1376 isl_vertices_free(vertices);
1378 return r;
1379 error:
1380 data->list = list;
1381 isl_vertices_free(vertices);
1382 return -1;
1385 static struct isl_obj vertices(struct isl_stream *s,
1386 struct isl_hash_table *table)
1388 isl_ctx *ctx;
1389 struct isl_obj obj;
1390 struct isl_list *list = NULL;
1391 isl_union_set *uset;
1392 struct add_vertex_data data = { NULL };
1394 obj = read_expr(s, table);
1395 obj = convert(s->ctx, obj, isl_obj_union_set);
1396 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1397 uset = obj.v;
1398 obj.v = NULL;
1400 ctx = isl_union_set_get_ctx(uset);
1401 list = isl_list_alloc(ctx, 0);
1402 if (!list)
1403 goto error;
1405 data.list = list;
1407 if (isl_union_set_foreach_set(uset, &set_vertices, &data) < 0)
1408 goto error;
1410 isl_union_set_free(uset);
1412 obj.type = isl_obj_list;
1413 obj.v = data.list;
1415 return obj;
1416 error:
1417 isl_union_set_free(uset);
1418 isl_list_free(data.list);
1419 free_obj(obj);
1420 obj.type = isl_obj_none;
1421 obj.v = NULL;
1422 return obj;
1425 static struct isl_obj type_of(struct isl_stream *s,
1426 struct isl_hash_table *table)
1428 isl_ctx *ctx;
1429 struct isl_obj obj;
1430 const char *type = "unknown";
1432 obj = read_expr(s, table);
1434 if (obj.type == isl_obj_map ||
1435 obj.type == isl_obj_union_map)
1436 type = "map";
1437 if (obj.type == isl_obj_set ||
1438 obj.type == isl_obj_union_set)
1439 type = "set";
1440 if (obj.type == isl_obj_pw_qpolynomial ||
1441 obj.type == isl_obj_union_pw_qpolynomial)
1442 type = "piecewise quasipolynomial";
1443 if (obj.type == isl_obj_pw_qpolynomial_fold ||
1444 obj.type == isl_obj_union_pw_qpolynomial_fold)
1445 type = "piecewise quasipolynomial fold";
1446 if (obj.type == isl_obj_list)
1447 type = "list";
1448 if (obj.type == isl_obj_bool)
1449 type = "boolean";
1450 if (obj.type == isl_obj_str)
1451 type = "string";
1452 if (obj.type == isl_obj_int)
1453 type = "int";
1455 free_obj(obj);
1456 obj.type = isl_obj_str;
1457 obj.v = isl_str_from_string(s->ctx, strdup(type));
1459 return obj;
1462 static __isl_give isl_union_map *read_map(struct isl_stream *s,
1463 struct isl_hash_table *table)
1465 struct isl_obj obj;
1467 obj = read_obj(s, table);
1468 obj = convert(s->ctx, obj, isl_obj_union_map);
1469 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1470 return obj.v;
1471 error:
1472 free_obj(obj);
1473 return NULL;
1476 static struct isl_obj last_any(struct isl_stream *s,
1477 struct isl_hash_table *table, __isl_take isl_union_map *must_source,
1478 __isl_take isl_union_map *may_source)
1480 struct isl_obj obj = { isl_obj_none, NULL };
1481 isl_union_map *sink = NULL;
1482 isl_union_map *schedule = NULL;
1483 isl_union_map *may_dep;
1484 isl_union_map *must_dep;
1486 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1487 goto error;
1489 sink = read_map(s, table);
1490 if (!sink)
1491 goto error;
1493 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1494 goto error;
1496 schedule = read_map(s, table);
1497 if (!schedule)
1498 goto error;
1500 if (isl_union_map_compute_flow(sink, must_source, may_source,
1501 schedule, &must_dep, &may_dep,
1502 NULL, NULL) < 0)
1503 return obj;
1505 obj.type = isl_obj_union_map;
1506 obj.v = isl_union_map_union(must_dep, may_dep);
1508 return obj;
1509 error:
1510 isl_union_map_free(may_source);
1511 isl_union_map_free(must_source);
1512 isl_union_map_free(sink);
1513 isl_union_map_free(schedule);
1514 free_obj(obj);
1515 obj.type = isl_obj_none;
1516 obj.v = NULL;
1517 return obj;
1520 static struct isl_obj any(struct isl_stream *s, struct isl_hash_table *table)
1522 struct isl_obj obj = { isl_obj_none, NULL };
1523 isl_union_map *must_source = NULL;
1524 isl_union_map *may_source = NULL;
1525 isl_union_map *sink = NULL;
1526 isl_union_map *schedule = NULL;
1527 isl_union_map *may_dep;
1529 may_source = read_map(s, table);
1530 if (!may_source)
1531 goto error;
1533 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST])) {
1534 must_source = read_map(s, table);
1535 if (!must_source)
1536 goto error;
1537 return last_any(s, table, must_source, may_source);
1540 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1541 goto error;
1543 sink = read_map(s, table);
1544 if (!sink)
1545 goto error;
1547 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1548 goto error;
1550 schedule = read_map(s, table);
1551 if (!schedule)
1552 goto error;
1554 must_source = isl_union_map_empty(isl_union_map_get_dim(sink));
1555 if (isl_union_map_compute_flow(sink, must_source, may_source,
1556 schedule, NULL, &may_dep,
1557 NULL, NULL) < 0)
1558 return obj;
1560 obj.type = isl_obj_union_map;
1561 obj.v = may_dep;
1563 return obj;
1564 error:
1565 isl_union_map_free(may_source);
1566 isl_union_map_free(must_source);
1567 isl_union_map_free(sink);
1568 isl_union_map_free(schedule);
1569 free_obj(obj);
1570 obj.type = isl_obj_none;
1571 obj.v = NULL;
1572 return obj;
1575 static struct isl_obj last(struct isl_stream *s, struct isl_hash_table *table)
1577 struct isl_obj obj = { isl_obj_none, NULL };
1578 struct isl_list *list = NULL;
1579 isl_union_map *must_source = NULL;
1580 isl_union_map *may_source = NULL;
1581 isl_union_map *sink = NULL;
1582 isl_union_map *schedule = NULL;
1583 isl_union_map *must_dep;
1584 isl_union_map *must_no_source;
1586 must_source = read_map(s, table);
1587 if (!must_source)
1588 goto error;
1590 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY])) {
1591 may_source = read_map(s, table);
1592 if (!may_source)
1593 goto error;
1594 return last_any(s, table, must_source, may_source);
1597 list = isl_list_alloc(s->ctx, 2);
1598 if (!list)
1599 goto error;
1601 if (isl_stream_eat(s, iscc_op[ISCC_BEFORE]))
1602 goto error;
1604 sink = read_map(s, table);
1605 if (!sink)
1606 goto error;
1608 if (isl_stream_eat(s, iscc_op[ISCC_UNDER]))
1609 goto error;
1611 schedule = read_map(s, table);
1612 if (!schedule)
1613 goto error;
1615 may_source = isl_union_map_empty(isl_union_map_get_dim(sink));
1616 if (isl_union_map_compute_flow(sink, must_source, may_source,
1617 schedule, &must_dep, NULL,
1618 &must_no_source, NULL) < 0)
1619 return obj;
1621 list->obj[0].type = isl_obj_union_map;
1622 list->obj[0].v = must_dep;
1623 list->obj[1].type = isl_obj_union_map;
1624 list->obj[1].v = must_no_source;
1626 obj.v = list;
1627 obj.type = isl_obj_list;
1629 return obj;
1630 error:
1631 isl_list_free(list);
1632 isl_union_map_free(may_source);
1633 isl_union_map_free(must_source);
1634 isl_union_map_free(sink);
1635 isl_union_map_free(schedule);
1636 free_obj(obj);
1637 obj.type = isl_obj_none;
1638 obj.v = NULL;
1639 return obj;
1642 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1644 struct isl_token *tok;
1646 if (isl_stream_eat_if_available(s, '+'))
1647 return transitive_closure(s->ctx, obj);
1649 tok = isl_stream_next_token(s);
1650 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1651 isl_stream_error(s, tok, "expecting -1");
1652 if (tok)
1653 isl_stream_push_token(s, tok);
1654 goto error;
1656 isl_token_free(tok);
1657 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_map), goto error);
1658 if (obj.type != isl_obj_union_map)
1659 obj = convert(s->ctx, obj, isl_obj_union_map);
1661 obj.v = isl_union_map_reverse(obj.v);
1662 if (!obj.v)
1663 goto error;
1665 return obj;
1666 error:
1667 free_obj(obj);
1668 obj.type = isl_obj_none;
1669 obj.v = NULL;
1670 return obj;
1673 static struct isl_obj read_from_file(struct isl_stream *s)
1675 struct isl_obj obj;
1676 struct isl_token *tok;
1677 struct isl_stream *s_file;
1678 struct iscc_options *options;
1679 FILE *file;
1681 tok = isl_stream_next_token(s);
1682 if (!tok || tok->type != ISL_TOKEN_STRING) {
1683 isl_stream_error(s, tok, "expecting filename");
1684 isl_token_free(tok);
1685 goto error;
1688 options = isl_ctx_peek_iscc_options(s->ctx);
1689 if (!options || !options->io) {
1690 isl_token_free(tok);
1691 isl_die(s->ctx, isl_error_invalid,
1692 "read operation not allowed", goto error);
1695 file = fopen(tok->u.s, "r");
1696 isl_token_free(tok);
1697 isl_assert(s->ctx, file, goto error);
1699 s_file = isl_stream_new_file(s->ctx, file);
1700 if (!s_file) {
1701 fclose(file);
1702 goto error;
1705 obj = isl_stream_read_obj(s_file);
1707 isl_stream_free(s_file);
1708 fclose(file);
1710 return obj;
1711 error:
1712 obj.type = isl_obj_none;
1713 obj.v = NULL;
1714 return obj;
1717 static struct isl_obj write_to_file(struct isl_stream *s,
1718 struct isl_hash_table *table)
1720 struct isl_obj obj;
1721 struct isl_token *tok;
1722 struct isl_stream *s_file;
1723 struct iscc_options *options;
1724 FILE *file;
1725 isl_printer *p;
1727 tok = isl_stream_next_token(s);
1728 if (!tok || tok->type != ISL_TOKEN_STRING) {
1729 isl_stream_error(s, tok, "expecting filename");
1730 isl_token_free(tok);
1731 goto error;
1734 obj = read_expr(s, table);
1736 options = isl_ctx_peek_iscc_options(s->ctx);
1737 if (!options || !options->io) {
1738 isl_token_free(tok);
1739 isl_die(s->ctx, isl_error_invalid,
1740 "write operation not allowed", goto error);
1743 file = fopen(tok->u.s, "w");
1744 isl_token_free(tok);
1745 if (!file)
1746 isl_die(s->ctx, isl_error_unknown,
1747 "could not open file for writing", goto error);
1749 p = isl_printer_to_file(s->ctx, file);
1750 p = isl_printer_set_output_format(p, options->format);
1751 p = obj.type->print(p, obj.v);
1752 p = isl_printer_end_line(p);
1753 isl_printer_free(p);
1755 fclose(file);
1756 error:
1757 free_obj(obj);
1758 obj.type = isl_obj_none;
1759 obj.v = NULL;
1760 return obj;
1763 static struct isl_obj read_string_if_available(struct isl_stream *s)
1765 struct isl_token *tok;
1766 struct isl_obj obj = { isl_obj_none, NULL };
1768 tok = isl_stream_next_token(s);
1769 if (!tok)
1770 return obj;
1771 if (tok->type == ISL_TOKEN_STRING) {
1772 isl_str *str;
1773 str = isl_str_alloc(s->ctx);
1774 if (!str)
1775 goto error;
1776 str->s = strdup(tok->u.s);
1777 isl_token_free(tok);
1778 obj.v = str;
1779 obj.type = isl_obj_str;
1780 } else
1781 isl_stream_push_token(s, tok);
1782 return obj;
1783 error:
1784 isl_token_free(tok);
1785 return obj;
1788 static __isl_give char *read_ident(struct isl_stream *s)
1790 char *name;
1791 struct isl_token *tok, *tok2;
1793 name = isl_stream_read_ident_if_available(s);
1794 if (name)
1795 return name;
1797 tok = isl_stream_next_token(s);
1798 if (!tok)
1799 return NULL;
1800 if (tok->type != '$') {
1801 isl_stream_push_token(s, tok);
1802 return NULL;
1804 tok2 = isl_stream_next_token(s);
1805 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1806 if (tok2)
1807 isl_stream_push_token(s, tok2);
1808 isl_stream_push_token(s, tok);
1809 return NULL;
1812 name = isl_int_get_str(tok2->u.v);
1813 isl_token_free(tok);
1814 isl_token_free(tok2);
1816 return name;
1819 static struct isl_obj read_obj(struct isl_stream *s,
1820 struct isl_hash_table *table)
1822 struct isl_obj obj = { isl_obj_none, NULL };
1823 char *name = NULL;
1824 struct isc_un_op *op = NULL;
1826 obj = read_string_if_available(s);
1827 if (obj.v)
1828 return obj;
1829 if (isl_stream_eat_if_available(s, '(')) {
1830 obj = read_expr(s, table);
1831 if (!obj.v || isl_stream_eat(s, ')'))
1832 goto error;
1833 } else {
1834 op = read_prefix_un_op_if_available(s);
1835 if (op)
1836 return read_un_op_expr(s, table, op);
1838 if (isl_stream_eat_if_available(s, iscc_op[ISCC_READ]))
1839 return read_from_file(s);
1840 if (isl_stream_eat_if_available(s, iscc_op[ISCC_WRITE]))
1841 return write_to_file(s, table);
1842 if (isl_stream_eat_if_available(s, iscc_op[ISCC_VERTICES]))
1843 return vertices(s, table);
1844 if (isl_stream_eat_if_available(s, iscc_op[ISCC_ANY]))
1845 return any(s, table);
1846 if (isl_stream_eat_if_available(s, iscc_op[ISCC_LAST]))
1847 return last(s, table);
1848 if (isl_stream_eat_if_available(s, iscc_op[ISCC_TYPEOF]))
1849 return type_of(s, table);
1851 name = read_ident(s);
1852 if (name)
1853 obj = stored_obj(s->ctx, table, name);
1854 else
1855 obj = isl_stream_read_obj(s);
1856 if (!obj.v)
1857 goto error;
1860 if (isl_stream_eat_if_available(s, '^'))
1861 obj = power(s, obj);
1862 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
1863 obj = obj_at_index(s, obj);
1864 else if (is_subtype(obj, isl_obj_union_map) &&
1865 isl_stream_eat_if_available(s, '(')) {
1866 obj = convert(s->ctx, obj, isl_obj_union_map);
1867 obj = apply(s, obj.v, table);
1868 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial) &&
1869 isl_stream_eat_if_available(s, '(')) {
1870 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial);
1871 obj = apply_fun(s, obj, table);
1872 } else if (is_subtype(obj, isl_obj_union_pw_qpolynomial_fold) &&
1873 isl_stream_eat_if_available(s, '(')) {
1874 obj = convert(s->ctx, obj, isl_obj_union_pw_qpolynomial_fold);
1875 obj = apply_fun(s, obj, table);
1878 return obj;
1879 error:
1880 free_obj(obj);
1881 obj.type = isl_obj_none;
1882 obj.v = NULL;
1883 return obj;
1886 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
1887 struct isl_obj lhs, struct isl_obj rhs)
1889 int i;
1891 for (i = 0; ; ++i) {
1892 if (!bin_ops[i].op)
1893 break;
1894 if (bin_ops[i].op != like->op)
1895 continue;
1896 if (!is_subtype(lhs, bin_ops[i].lhs))
1897 continue;
1898 if (!is_subtype(rhs, bin_ops[i].rhs))
1899 continue;
1901 return &bin_ops[i];
1904 for (i = 0; ; ++i) {
1905 if (!named_bin_ops[i].name)
1906 break;
1907 if (named_bin_ops[i].op.op != like->op)
1908 continue;
1909 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1910 continue;
1911 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
1912 continue;
1914 return &named_bin_ops[i].op;
1917 return NULL;
1920 static int next_is_neg_int(struct isl_stream *s)
1922 struct isl_token *tok;
1923 int ret;
1925 tok = isl_stream_next_token(s);
1926 ret = tok && tok->type == ISL_TOKEN_VALUE && isl_int_is_neg(tok->u.v);
1927 isl_stream_push_token(s, tok);
1929 return ret;
1932 static struct isl_obj read_expr(struct isl_stream *s,
1933 struct isl_hash_table *table)
1935 struct isl_obj obj = { isl_obj_none, NULL };
1936 struct isl_obj right_obj = { isl_obj_none, NULL };
1938 obj = read_obj(s, table);
1939 for (; obj.v;) {
1940 struct isc_bin_op *op = NULL;
1942 op = read_bin_op_if_available(s, obj);
1943 if (!op)
1944 break;
1946 right_obj = read_obj(s, table);
1948 op = find_matching_bin_op(op, obj, right_obj);
1950 if (!op)
1951 isl_die(s->ctx, isl_error_invalid,
1952 "no such binary operator defined on given operands",
1953 goto error);
1955 obj = convert(s->ctx, obj, op->lhs);
1956 right_obj = convert(s->ctx, right_obj, op->rhs);
1957 obj.v = op->fn(obj.v, right_obj.v);
1958 obj.type = op->res;
1961 if (obj.type == isl_obj_int && next_is_neg_int(s)) {
1962 right_obj = read_obj(s, table);
1963 obj.v = isl_int_obj_add(obj.v, right_obj.v);
1966 return obj;
1967 error:
1968 free_obj(right_obj);
1969 free_obj(obj);
1970 obj.type = isl_obj_none;
1971 obj.v = NULL;
1972 return obj;
1975 static __isl_give isl_printer *source_file(struct isl_stream *s,
1976 struct isl_hash_table *table, __isl_take isl_printer *p);
1978 static __isl_give isl_printer *read_line(struct isl_stream *s,
1979 struct isl_hash_table *table, __isl_take isl_printer *p, int tty)
1981 struct isl_obj obj = { isl_obj_none, NULL };
1982 char *lhs = NULL;
1983 int assign = 0;
1984 int only_print = 0;
1985 struct isc_bin_op *op = NULL;
1986 char buf[30];
1988 if (!p)
1989 return NULL;
1990 if (isl_stream_is_empty(s))
1991 return p;
1993 if (isl_stream_eat_if_available(s, iscc_op[ISCC_SOURCE]))
1994 return source_file(s, table, p);
1996 assign = is_assign(s);
1997 if (assign) {
1998 lhs = isl_stream_read_ident_if_available(s);
1999 if (isl_stream_eat(s, ISL_TOKEN_DEF))
2000 goto error;
2001 } else if (isl_stream_eat_if_available(s, iscc_op[ISCC_PRINT]))
2002 only_print = 1;
2003 else if (!tty)
2004 only_print = 1;
2006 obj = read_expr(s, table);
2007 if (obj.type == isl_obj_none || obj.v == NULL)
2008 goto error;
2009 if (isl_stream_eat(s, ';'))
2010 goto error;
2012 if (only_print) {
2013 p = obj.type->print(p, obj.v);
2014 p = isl_printer_end_line(p);
2015 free_obj(obj);
2016 return p;
2018 if (!assign) {
2019 static int count = 0;
2020 snprintf(buf, sizeof(buf), "$%d", count++);
2021 lhs = strdup(buf + 1);
2023 p = isl_printer_print_str(p, buf);
2024 p = isl_printer_print_str(p, " := ");
2025 p = obj.type->print(p, obj.v);
2026 p = isl_printer_end_line(p);
2028 if (do_assign(s->ctx, table, lhs, obj))
2029 return p;
2031 return p;
2032 error:
2033 isl_stream_flush_tokens(s);
2034 isl_stream_skip_line(s);
2035 free(lhs);
2036 free_obj(obj);
2037 return p;
2040 int free_cb(void **entry, void *user)
2042 struct isl_named_obj *named = *entry;
2044 free_obj(named->obj);
2045 free(named->name);
2046 free(named);
2048 return 0;
2051 static void register_named_ops(struct isl_stream *s)
2053 int i;
2055 for (i = 0; i < ISCC_N_OP; ++i) {
2056 iscc_op[i] = isl_stream_register_keyword(s, op_name[i]);
2057 assert(iscc_op[i] != ISL_TOKEN_ERROR);
2060 for (i = 0; ; ++i) {
2061 if (!named_un_ops[i].name)
2062 break;
2063 named_un_ops[i].op.op = isl_stream_register_keyword(s,
2064 named_un_ops[i].name);
2065 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
2068 for (i = 0; ; ++i) {
2069 if (!named_bin_ops[i].name)
2070 break;
2071 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
2072 named_bin_ops[i].name);
2073 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
2077 static __isl_give isl_printer *source_file(struct isl_stream *s,
2078 struct isl_hash_table *table, __isl_take isl_printer *p)
2080 struct isl_token *tok;
2081 struct isl_stream *s_file;
2082 FILE *file;
2084 tok = isl_stream_next_token(s);
2085 if (!tok || tok->type != ISL_TOKEN_STRING) {
2086 isl_stream_error(s, tok, "expecting filename");
2087 isl_token_free(tok);
2088 return p;
2091 file = fopen(tok->u.s, "r");
2092 isl_token_free(tok);
2093 isl_assert(s->ctx, file, return p);
2095 s_file = isl_stream_new_file(s->ctx, file);
2096 if (!s_file) {
2097 fclose(file);
2098 return p;
2101 register_named_ops(s_file);
2103 while (!s_file->eof)
2104 p = read_line(s_file, table, p, 0);
2106 isl_stream_free(s_file);
2107 fclose(file);
2109 isl_stream_eat(s, ';');
2111 return p;
2114 int main(int argc, char **argv)
2116 struct isl_ctx *ctx;
2117 struct isl_stream *s;
2118 struct isl_hash_table *table;
2119 struct iscc_options *options;
2120 isl_printer *p;
2121 int tty = isatty(0);
2123 options = iscc_options_new_with_defaults();
2124 assert(options);
2125 argc = iscc_options_parse(options, argc, argv, ISL_ARG_ALL);
2127 ctx = isl_ctx_alloc_with_options(iscc_options_arg, options);
2128 s = isl_stream_new_file(ctx, stdin);
2129 assert(s);
2130 table = isl_hash_table_alloc(ctx, 10);
2131 assert(table);
2132 p = isl_printer_to_file(ctx, stdout);
2133 p = isl_printer_set_output_format(p, options->format);
2134 assert(p);
2136 register_named_ops(s);
2138 while (p && !s->eof) {
2139 p = read_line(s, table, p, tty);
2142 isl_printer_free(p);
2143 isl_hash_table_foreach(ctx, table, free_cb, NULL);
2144 isl_hash_table_free(ctx, table);
2145 isl_stream_free(s);
2146 isl_ctx_free(ctx);
2148 return 0;