avoid use of isl_dim internals
[barvinok.git] / iscc.c
blob79b1a55d1e7b4899f5e2a8c3dfb844e9365d7bd9
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <isl_obj.h>
5 #include <isl_stream.h>
6 #include <isl_vertices.h>
7 #include <isl_obj_list.h>
8 #include <barvinok/barvinok.h>
10 #include "config.h"
12 #ifdef HAVE_CLOOG
13 #include <cloog/isl/cloog.h>
14 #endif
16 static int isl_bool_false = 0;
17 static int isl_bool_true = 1;
18 static int isl_bool_error = -1;
20 static enum isl_token_type read_op;
21 static enum isl_token_type vertices_op;
23 struct isl_arg_choice iscc_format[] = {
24 {"isl", ISL_FORMAT_ISL},
25 {"omega", ISL_FORMAT_OMEGA},
26 {"polylib", ISL_FORMAT_POLYLIB},
27 {"latex", ISL_FORMAT_LATEX},
28 {"C", ISL_FORMAT_C},
29 {0}
32 struct iscc_options {
33 struct barvinok_options *barvinok;
34 unsigned format;
37 struct isl_arg iscc_options_arg[] = {
38 ISL_ARG_CHILD(struct iscc_options, barvinok, "barvinok", barvinok_options_arg,
39 "barvinok options")
40 ISL_ARG_CHOICE(struct iscc_options, format, 0, "format", \
41 iscc_format, ISL_FORMAT_ISL, "output format")
42 ISL_ARG_END
45 ISL_ARG_DEF(iscc_options, struct iscc_options, iscc_options_arg)
47 static void *isl_obj_bool_copy(void *v)
49 return v;
52 static void isl_obj_bool_free(void *v)
56 static __isl_give isl_printer *isl_obj_bool_print(__isl_take isl_printer *p,
57 void *v)
59 if (v == &isl_bool_true)
60 return isl_printer_print_str(p, "True");
61 else if (v == &isl_bool_false)
62 return isl_printer_print_str(p, "False");
63 else
64 return isl_printer_print_str(p, "Error");
67 static void *isl_obj_bool_add(void *v1, void *v2)
69 return v1;
72 struct isl_obj_vtable isl_obj_bool_vtable = {
73 isl_obj_bool_copy,
74 isl_obj_bool_add,
75 isl_obj_bool_print,
76 isl_obj_bool_free
78 #define isl_obj_bool (&isl_obj_bool_vtable)
80 int *isl_bool_from_int(int res)
82 return res < 0 ? &isl_bool_error : res ? &isl_bool_true : &isl_bool_false;
85 int *union_map_is_equal(__isl_take isl_union_map *map1,
86 __isl_take isl_union_map *map2)
88 int res = isl_union_map_is_equal(map1, map2);
89 isl_union_map_free(map1);
90 isl_union_map_free(map2);
91 return isl_bool_from_int(res);
93 int *union_set_is_equal(__isl_take isl_union_set *set1,
94 __isl_take isl_union_set *set2)
96 return union_map_is_equal((isl_union_map *)set1, (isl_union_map *)set2);
99 int *union_map_is_subset(__isl_take isl_union_map *map1,
100 __isl_take isl_union_map *map2)
102 int res = isl_union_map_is_subset(map1, map2);
103 isl_union_map_free(map1);
104 isl_union_map_free(map2);
105 return isl_bool_from_int(res);
107 int *union_set_is_subset(__isl_take isl_union_set *set1,
108 __isl_take isl_union_set *set2)
110 return union_map_is_subset((isl_union_map *)set1, (isl_union_map *)set2);
113 int *union_map_is_strict_subset(__isl_take isl_union_map *map1,
114 __isl_take isl_union_map *map2)
116 int res = isl_union_map_is_strict_subset(map1, map2);
117 isl_union_map_free(map1);
118 isl_union_map_free(map2);
119 return isl_bool_from_int(res);
121 int *union_set_is_strict_subset(__isl_take isl_union_set *set1,
122 __isl_take isl_union_set *set2)
124 return union_map_is_strict_subset((isl_union_map *)set1,
125 (isl_union_map *)set2);
128 int *union_map_is_superset(__isl_take isl_union_map *map1,
129 __isl_take isl_union_map *map2)
131 return union_map_is_subset(map2, map1);
133 int *union_set_is_superset(__isl_take isl_union_set *set1,
134 __isl_take isl_union_set *set2)
136 return union_set_is_subset(set2, set1);
139 int *union_map_is_strict_superset(__isl_take isl_union_map *map1,
140 __isl_take isl_union_map *map2)
142 return union_map_is_strict_subset(map2, map1);
144 int *union_set_is_strict_superset(__isl_take isl_union_set *set1,
145 __isl_take isl_union_set *set2)
147 return union_set_is_strict_subset(set2, set1);
150 extern struct isl_obj_vtable isl_obj_list_vtable;
151 #define isl_obj_list (&isl_obj_list_vtable)
153 typedef void *(*isc_bin_op_fn)(void *lhs, void *rhs);
154 struct isc_bin_op {
155 enum isl_token_type op;
156 isl_obj_type lhs;
157 isl_obj_type rhs;
158 isl_obj_type res;
159 isc_bin_op_fn fn;
161 struct isc_named_bin_op {
162 char *name;
163 struct isc_bin_op op;
166 struct iscc_at {
167 isl_union_pw_qpolynomial *upwqp;
168 isl_union_pw_qpolynomial *res;
171 static int eval_at(__isl_take isl_point *pnt, void *user)
173 struct iscc_at *at = (struct iscc_at *) user;
174 isl_qpolynomial *qp;
175 isl_set *set;
177 set = isl_set_from_point(isl_point_copy(pnt));
178 qp = isl_union_pw_qpolynomial_eval(
179 isl_union_pw_qpolynomial_copy(at->upwqp), pnt);
181 at->res = isl_union_pw_qpolynomial_add(at->res,
182 isl_union_pw_qpolynomial_from_pw_qpolynomial(
183 isl_pw_qpolynomial_alloc(set, qp)));
185 return 0;
188 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_at(
189 __isl_take isl_union_pw_qpolynomial *upwqp,
190 __isl_take isl_union_set *uset)
192 struct iscc_at at;
194 at.upwqp = upwqp;
195 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset));
197 isl_union_set_foreach_point(uset, eval_at, &at);
199 isl_union_pw_qpolynomial_free(upwqp);
200 isl_union_set_free(uset);
202 return at.res;
205 struct iscc_fold_at {
206 isl_union_pw_qpolynomial_fold *upwf;
207 isl_union_pw_qpolynomial *res;
210 static int eval_fold_at(__isl_take isl_point *pnt, void *user)
212 struct iscc_fold_at *at = (struct iscc_fold_at *) user;
213 isl_qpolynomial *qp;
214 isl_set *set;
216 set = isl_set_from_point(isl_point_copy(pnt));
217 qp = isl_union_pw_qpolynomial_fold_eval(
218 isl_union_pw_qpolynomial_fold_copy(at->upwf), pnt);
220 at->res = isl_union_pw_qpolynomial_add(at->res,
221 isl_union_pw_qpolynomial_from_pw_qpolynomial(
222 isl_pw_qpolynomial_alloc(set, qp)));
224 return 0;
227 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_fold_at(
228 __isl_take isl_union_pw_qpolynomial_fold *upwf,
229 __isl_take isl_union_set *uset)
231 struct iscc_fold_at at;
233 at.upwf = upwf;
234 at.res = isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset));
236 isl_union_set_foreach_point(uset, eval_fold_at, &at);
238 isl_union_pw_qpolynomial_fold_free(upwf);
239 isl_union_set_free(uset);
241 return at.res;
244 struct isc_bin_op bin_ops[] = {
245 { '+', isl_obj_union_set, isl_obj_union_set,
246 isl_obj_union_set,
247 (isc_bin_op_fn) &isl_union_set_union },
248 { '+', isl_obj_union_map, isl_obj_union_map,
249 isl_obj_union_map,
250 (isc_bin_op_fn) &isl_union_map_union },
251 { '-', isl_obj_union_set, isl_obj_union_set,
252 isl_obj_union_set,
253 (isc_bin_op_fn) &isl_union_set_subtract },
254 { '-', isl_obj_union_map, isl_obj_union_map,
255 isl_obj_union_map,
256 (isc_bin_op_fn) &isl_union_map_subtract },
257 { '*', isl_obj_union_set, isl_obj_union_set,
258 isl_obj_union_set,
259 (isc_bin_op_fn) &isl_union_set_intersect },
260 { '*', isl_obj_union_map, isl_obj_union_map,
261 isl_obj_union_map,
262 (isc_bin_op_fn) &isl_union_map_intersect },
263 { '*', isl_obj_union_map, isl_obj_union_set,
264 isl_obj_union_map,
265 (isc_bin_op_fn) &isl_union_map_intersect_domain },
266 { '.', isl_obj_union_map, isl_obj_union_map,
267 isl_obj_union_map,
268 (isc_bin_op_fn) &isl_union_map_apply_range },
269 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
270 isl_obj_union_map,
271 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
272 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
273 (isc_bin_op_fn) &union_set_is_equal },
274 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
275 (isc_bin_op_fn) &union_map_is_equal },
276 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
277 isl_obj_bool, (isc_bin_op_fn) &union_set_is_subset },
278 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
279 isl_obj_bool, (isc_bin_op_fn) &union_map_is_subset },
280 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
281 isl_obj_bool, (isc_bin_op_fn) &union_set_is_strict_subset },
282 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
283 isl_obj_bool, (isc_bin_op_fn) &union_map_is_strict_subset },
284 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
285 isl_obj_bool, (isc_bin_op_fn) &union_set_is_superset },
286 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
287 isl_obj_bool, (isc_bin_op_fn) &union_map_is_superset },
288 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
289 isl_obj_bool, (isc_bin_op_fn) &union_set_is_strict_superset },
290 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
291 isl_obj_bool, (isc_bin_op_fn) &union_map_is_strict_superset },
292 { '+', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
293 isl_obj_union_pw_qpolynomial,
294 (isc_bin_op_fn) &isl_union_pw_qpolynomial_add },
295 { '-', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
296 isl_obj_union_pw_qpolynomial,
297 (isc_bin_op_fn) &isl_union_pw_qpolynomial_sub },
298 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
299 isl_obj_union_pw_qpolynomial,
300 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
301 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
302 isl_obj_union_pw_qpolynomial,
303 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
304 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
305 isl_obj_union_pw_qpolynomial_fold,
306 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
307 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
308 isl_obj_union_pw_qpolynomial,
309 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
310 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
311 isl_obj_union_pw_qpolynomial,
312 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
313 { '%', isl_obj_union_set, isl_obj_union_set,
314 isl_obj_union_set,
315 (isc_bin_op_fn) &isl_union_set_gist },
316 { '%', isl_obj_union_map, isl_obj_union_map,
317 isl_obj_union_map,
318 (isc_bin_op_fn) &isl_union_map_gist },
319 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
320 isl_obj_union_pw_qpolynomial,
321 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
322 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
323 isl_obj_union_pw_qpolynomial_fold,
324 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
327 struct isc_named_bin_op named_bin_ops[] = {
328 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
329 isl_obj_union_set,
330 (isc_bin_op_fn) &isl_union_set_product } },
331 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
332 isl_obj_union_map,
333 (isc_bin_op_fn) &isl_union_map_product } },
334 NULL
337 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
339 return isl_set_from_basic_set(isl_union_set_sample(uset));
342 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
344 return isl_map_from_basic_map(isl_union_map_sample(umap));
347 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
348 __isl_take isl_union_pw_qpolynomial *upwqp)
350 isl_ctx *ctx;
351 struct isl_list *list;
352 int tight;
354 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
355 list = isl_list_alloc(ctx, 2);
356 if (!list)
357 goto error;
359 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
360 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
361 isl_fold_max, &tight);
362 list->obj[1].type = isl_obj_bool;
363 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
364 if (tight < 0 || !list->obj[0].v)
365 goto error;
367 return list;
368 error:
369 isl_list_free(list);
370 return NULL;
373 #ifdef HAVE_CLOOG
374 struct iscc_codegen_data {
375 int nr;
376 int nparam;
377 int max_iter;
378 int n_scat;
379 isl_set *context;
380 CloogState *state;
381 CloogLoop **next;
382 CloogBlockList **nextBL;
383 CloogScatteringList *scatterings;
384 CloogScatteringList **nextScat;
387 static int add_domain(__isl_take isl_map *map, void *user)
389 struct iscc_codegen_data *data = (struct iscc_codegen_data *)user;
390 isl_ctx *ctx = isl_map_get_ctx(map);
391 isl_dim *dim;
392 isl_set *domain;
393 CloogStatement *statement;
394 CloogLoop *l;
395 CloogScatteringList *s;
396 int n_in;
398 n_in = isl_map_dim(map, isl_dim_in);
399 if (data->nr == 0) {
400 isl_dim *dim = isl_map_get_dim(map);
401 data->nparam = isl_map_dim(map, isl_dim_param);
402 data->n_scat = isl_map_dim(map, isl_dim_out);
403 dim = isl_dim_drop_inputs(dim, 0, n_in);
404 dim = isl_dim_drop_outputs(dim, 0, data->n_scat);
405 data->context = isl_set_universe(dim);
406 } else {
407 isl_assert(ctx, data->nparam == isl_map_dim(map, isl_dim_param),
408 goto error);
409 isl_assert(ctx, data->n_scat == isl_map_dim(map, isl_dim_out),
410 goto error);
412 if (n_in > data->max_iter)
413 data->max_iter = n_in;
415 l = cloog_loop_malloc(data->state);
416 if (!l)
417 goto error;
418 l->next = NULL;
419 l->inner = NULL;
420 domain = isl_map_domain(isl_map_copy(map));
421 l->domain = cloog_domain_from_isl_set(domain);
423 statement = cloog_statement_alloc(data->state, data->nr);
424 statement->usr = NULL;
425 dim = isl_map_get_dim(map);
426 statement->name = strdup(isl_dim_get_tuple_name(dim, isl_dim_in));
427 isl_dim_free(dim);
428 l->block = cloog_block_alloc(statement, 0, NULL, n_in);
430 s = isl_alloc_type(ctx, CloogScatteringList);
431 s->scatt = cloog_scattering_from_isl_map(map);
432 s->next = NULL;
434 *data->next = l;
435 *data->nextBL = cloog_block_list_alloc((*data->next)->block);
436 data->next = &(*data->next)->next;
437 data->nextBL = &(*data->nextBL)->next;
438 *data->nextScat = s;
439 data->nextScat = &(s->next);
441 data->nr++;
443 return 0;
444 error:
445 return -1;
448 char **generate_names(isl_ctx *ctx, int n, const char *prefix)
450 int i;
451 char **names;
453 names = isl_alloc_array(ctx, char *, n);
454 for (i = 0; i < n; i++) {
455 names[i] = isl_alloc_array(ctx, char, 50);
456 sprintf(names[i], "%s%d", prefix, i);
459 return names;
462 void *codegen(void *arg)
464 int i;
465 struct iscc_codegen_data data;
466 isl_dim *dim;
467 isl_union_map *umap = (isl_union_map *)arg;
468 isl_ctx *ctx = isl_union_map_get_ctx(umap);
469 CloogOptions *options;
470 CloogProgram *p;
471 struct clast_stmt *stmt;
473 data.state = cloog_isl_state_malloc(ctx);
474 options = cloog_options_malloc(data.state);
475 options->language = LANGUAGE_C;
476 p = cloog_program_malloc();
478 isl_assert(ctx, p, goto error);
479 p->names = cloog_names_malloc();
480 isl_assert(ctx, p->names, goto error2);
481 p->language = 'c';
483 data.context = NULL;
484 data.nr = 0;
485 data.nparam = -1;
486 data.max_iter = 0;
487 data.n_scat = -1;
488 data.next = &p->loop;
489 data.nextBL = &p->blocklist;
490 data.scatterings = NULL;
491 data.nextScat = &data.scatterings;
493 if (isl_union_map_foreach_map(umap, &add_domain, &data) < 0)
494 goto error3;
496 if (data.nr == 0)
497 goto error3;
499 p->names->nb_parameters = data.nparam;
500 p->names->parameters = isl_alloc_array(ctx, char *, data.nparam);
501 dim = isl_set_get_dim(data.context);
502 p->context = cloog_domain_from_isl_set(data.context);
503 for (i = 0; i < data.nparam; ++i) {
504 const char *s = isl_dim_get_name(dim, isl_dim_param, i);
505 p->names->parameters[i] = strdup(s);
507 isl_dim_free(dim);
509 p->names->nb_iterators = data.max_iter;
510 p->names->iterators = generate_names(ctx, data.max_iter, "i");
512 p->names->nb_scattering = data.n_scat;
513 p->names->scattering = generate_names(ctx, data.n_scat, "c");
514 p->names->nb_scalars = 0;
515 p->nb_scattdims = data.n_scat;
516 p->scaldims = (int *)malloc(p->nb_scattdims*(sizeof(int)));
517 for (i = 0; i < p->nb_scattdims; i++)
518 p->scaldims[i] = 0;
520 cloog_program_scatter(p, data.scatterings, options);
521 p = cloog_program_generate(p, options);
523 stmt = cloog_clast_create(p, options);
524 clast_pprint(stdout, stmt, 0, options);
525 cloog_clast_free(stmt);
527 error3:
528 cloog_scattering_list_free(data.scatterings);
529 error2:
530 cloog_program_free(p);
531 error:
532 cloog_options_free(options);
533 cloog_state_free(data.state);
534 isl_union_map_free(umap);
535 return NULL;
537 #endif
539 typedef void *(*isc_un_op_fn)(void *arg);
540 struct isc_un_op {
541 enum isl_token_type op;
542 isl_obj_type arg;
543 isl_obj_type res;
544 isc_un_op_fn fn;
546 struct isc_named_un_op {
547 char *name;
548 struct isc_un_op op;
550 struct isc_named_un_op named_un_ops[] = {
551 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
552 (isc_un_op_fn) &isl_union_map_affine_hull } },
553 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
554 (isc_un_op_fn) &isl_union_set_affine_hull } },
555 {"card", { -1, isl_obj_union_set,
556 isl_obj_union_pw_qpolynomial,
557 (isc_un_op_fn) &isl_union_set_card } },
558 {"card", { -1, isl_obj_union_map,
559 isl_obj_union_pw_qpolynomial,
560 (isc_un_op_fn) &isl_union_map_card } },
561 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
562 (isc_un_op_fn) &isl_union_set_coalesce } },
563 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
564 (isc_un_op_fn) &isl_union_map_coalesce } },
565 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
566 isl_obj_union_pw_qpolynomial,
567 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
568 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
569 isl_obj_union_pw_qpolynomial_fold,
570 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
571 #ifdef HAVE_CLOOG
572 {"codegen", { -1, isl_obj_union_map, isl_obj_none, &codegen } },
573 #endif
574 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
575 (isc_un_op_fn) &isl_union_map_deltas } },
576 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
577 (isc_un_op_fn) &isl_union_map_domain } },
578 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
579 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
580 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
581 isl_obj_union_set,
582 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
583 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
584 (isc_un_op_fn) &isl_union_map_range } },
585 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
586 (isc_un_op_fn) &isl_union_map_lexmin } },
587 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
588 (isc_un_op_fn) &isl_union_map_lexmax } },
589 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
590 (isc_un_op_fn) &isl_union_set_lexmin } },
591 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
592 (isc_un_op_fn) &isl_union_set_lexmax } },
593 {"sample", { -1, isl_obj_union_set, isl_obj_set,
594 (isc_un_op_fn) &union_set_sample } },
595 {"sample", { -1, isl_obj_union_map, isl_obj_map,
596 (isc_un_op_fn) &union_map_sample } },
597 {"sum", { -1, isl_obj_union_pw_qpolynomial,
598 isl_obj_union_pw_qpolynomial,
599 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
600 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
601 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
602 NULL
605 struct isl_named_obj {
606 char *name;
607 struct isl_obj obj;
610 static void free_obj(struct isl_obj obj)
612 obj.type->free(obj.v);
615 static int same_name(const void *entry, const void *val)
617 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
619 return !strcmp(named->name, val);
622 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
623 char *name, struct isl_obj obj)
625 struct isl_hash_table_entry *entry;
626 uint32_t name_hash;
627 struct isl_named_obj *named;
629 name_hash = isl_hash_string(isl_hash_init(), name);
630 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
631 if (!entry)
632 goto error;
633 if (entry->data) {
634 named = entry->data;
635 free_obj(named->obj);
636 free(name);
637 } else {
638 named = isl_alloc_type(ctx, struct isl_named_obj);
639 if (!named)
640 goto error;
641 named->name = name;
642 entry->data = named;
644 named->obj = obj;
646 return 0;
647 error:
648 free_obj(obj);
649 free(name);
650 return -1;
653 static struct isl_obj stored_obj(struct isl_ctx *ctx,
654 struct isl_hash_table *table, char *name)
656 struct isl_obj obj = { isl_obj_none, NULL };
657 struct isl_hash_table_entry *entry;
658 uint32_t name_hash;
660 name_hash = isl_hash_string(isl_hash_init(), name);
661 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
662 if (entry) {
663 struct isl_named_obj *named;
664 named = entry->data;
665 obj = named->obj;
668 free(name);
669 obj.v = obj.type->copy(obj.v);
670 return obj;
673 static int is_subtype(struct isl_obj obj, isl_obj_type super)
675 if (obj.type == super)
676 return 1;
677 if (obj.type == isl_obj_map && super == isl_obj_union_map)
678 return 1;
679 if (obj.type == isl_obj_set && super == isl_obj_union_set)
680 return 1;
681 if (obj.type == isl_obj_pw_qpolynomial &&
682 super == isl_obj_union_pw_qpolynomial)
683 return 1;
684 if (obj.type == isl_obj_pw_qpolynomial_fold &&
685 super == isl_obj_union_pw_qpolynomial_fold)
686 return 1;
687 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
688 return 1;
689 return 0;
692 static struct isl_obj convert(struct isl_obj obj, isl_obj_type type)
694 if (obj.type == type)
695 return obj;
696 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
697 obj.type = isl_obj_union_map;
698 obj.v = isl_union_map_from_map(obj.v);
699 return obj;
701 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
702 obj.type = isl_obj_union_set;
703 obj.v = isl_union_set_from_set(obj.v);
704 return obj;
706 if (obj.type == isl_obj_pw_qpolynomial &&
707 type == isl_obj_union_pw_qpolynomial) {
708 obj.type = isl_obj_union_pw_qpolynomial;
709 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
710 return obj;
712 if (obj.type == isl_obj_pw_qpolynomial_fold &&
713 type == isl_obj_union_pw_qpolynomial_fold) {
714 obj.type = isl_obj_union_pw_qpolynomial_fold;
715 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
716 return obj;
718 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
719 if (type == isl_obj_union_map) {
720 obj.type = isl_obj_union_map;
721 return obj;
723 if (type == isl_obj_union_pw_qpolynomial) {
724 isl_dim *dim = isl_union_set_get_dim(obj.v);
725 isl_union_set_free(obj.v);
726 obj.v = isl_union_pw_qpolynomial_zero(dim);
727 obj.type = isl_obj_union_pw_qpolynomial;
728 return obj;
730 if (type == isl_obj_union_pw_qpolynomial_fold) {
731 isl_dim *dim = isl_union_set_get_dim(obj.v);
732 isl_union_set_free(obj.v);
733 obj.v = isl_union_pw_qpolynomial_fold_zero(dim);
734 obj.type = isl_obj_union_pw_qpolynomial_fold;
735 return obj;
738 free_obj(obj);
739 obj.type = isl_obj_none;
740 obj.v = NULL;
741 return obj;
744 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
745 struct isl_obj lhs)
747 int i;
748 struct isl_token *tok;
750 tok = isl_stream_next_token(s);
751 if (!tok)
752 return NULL;
754 for (i = 0; ; ++i) {
755 if (!bin_ops[i].op)
756 break;
757 if (bin_ops[i].op != tok->type)
758 continue;
759 if (!is_subtype(lhs, bin_ops[i].lhs))
760 continue;
762 isl_token_free(tok);
763 return &bin_ops[i];
766 for (i = 0; ; ++i) {
767 if (!named_bin_ops[i].name)
768 break;
769 if (named_bin_ops[i].op.op != tok->type)
770 continue;
771 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
772 continue;
774 isl_token_free(tok);
775 return &named_bin_ops[i].op;
778 isl_stream_push_token(s, tok);
780 return NULL;
783 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
785 int i;
786 struct isl_token *tok;
788 tok = isl_stream_next_token(s);
789 if (!tok)
790 return NULL;
792 for (i = 0; ; ++i) {
793 if (!named_un_ops[i].name)
794 break;
795 if (named_un_ops[i].op.op != tok->type)
796 continue;
798 isl_token_free(tok);
799 return &named_un_ops[i].op;
802 isl_stream_push_token(s, tok);
804 return NULL;
807 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
808 struct isl_obj arg)
810 int i;
812 for (i = 0; ; ++i) {
813 if (!named_un_ops[i].name)
814 break;
815 if (named_un_ops[i].op.op != like->op)
816 continue;
817 if (!is_subtype(arg, named_un_ops[i].op.arg))
818 continue;
820 return &named_un_ops[i].op;
823 return NULL;
826 static int is_assign(struct isl_stream *s)
828 struct isl_token *tok;
829 struct isl_token *tok2;
830 int assign;
832 tok = isl_stream_next_token(s);
833 if (!tok)
834 return 0;
835 if (tok->type != ISL_TOKEN_IDENT) {
836 isl_stream_push_token(s, tok);
837 return 0;
840 tok2 = isl_stream_next_token(s);
841 if (!tok2) {
842 isl_stream_push_token(s, tok);
843 return 0;
845 assign = tok2->type == ISL_TOKEN_DEF;
846 isl_stream_push_token(s, tok2);
847 isl_stream_push_token(s, tok);
849 return assign;
852 static struct isl_obj read_obj(struct isl_stream *s,
853 struct isl_hash_table *table);
854 static struct isl_obj read_expr(struct isl_stream *s,
855 struct isl_hash_table *table);
857 static struct isl_obj read_un_op_expr(struct isl_stream *s,
858 struct isl_hash_table *table, struct isc_un_op *op)
860 struct isl_obj obj = { isl_obj_none, NULL };
862 obj = read_obj(s, table);
863 if (!obj.v)
864 goto error;
866 op = find_matching_un_op(op, obj);
868 isl_assert(s->ctx, op, goto error);
869 obj = convert(obj, op->arg);
870 obj.v = op->fn(obj.v);
871 obj.type = op->res;
873 return obj;
874 error:
875 free_obj(obj);
876 obj.type = isl_obj_none;
877 obj.v = NULL;
878 return obj;
881 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
883 struct isl_list *list;
884 int exact;
886 if (obj.type == isl_obj_map)
887 obj = convert(obj, isl_obj_union_map);
888 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
889 list = isl_list_alloc(ctx, 2);
890 if (!list)
891 goto error;
893 list->obj[0].type = isl_obj_union_map;
894 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
895 list->obj[1].type = isl_obj_bool;
896 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
897 obj.v = list;
898 obj.type = isl_obj_list;
899 if (exact < 0 || !list->obj[0].v)
900 goto error;
902 return obj;
903 error:
904 free_obj(obj);
905 obj.type = isl_obj_none;
906 obj.v = NULL;
907 return obj;
910 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
912 struct isl_list *list = obj.v;
913 struct isl_token *tok;
914 int i;
916 tok = isl_stream_next_token(s);
917 if (!tok || tok->type != ISL_TOKEN_VALUE) {
918 isl_stream_error(s, tok, "expecting index");
919 if (tok)
920 isl_stream_push_token(s, tok);
921 goto error;
923 i = isl_int_get_si(tok->u.v);
924 isl_token_free(tok);
925 isl_assert(s->ctx, i < list->n, goto error);
926 if (isl_stream_eat(s, ']'))
927 goto error;
929 obj = list->obj[i];
930 obj.v = obj.type->copy(obj.v);
932 isl_list_free(list);
934 return obj;
935 error:
936 free_obj(obj);
937 obj.type = isl_obj_none;
938 obj.v = NULL;
939 return obj;
942 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
943 struct isl_hash_table *table)
945 struct isl_obj obj;
947 obj = read_expr(s, table);
948 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
949 is_subtype(obj, isl_obj_union_map), goto error);
951 if (obj.type == isl_obj_set)
952 obj = convert(obj, isl_obj_union_set);
953 else if (obj.type == isl_obj_map)
954 obj = convert(obj, isl_obj_union_map);
955 if (obj.type == isl_obj_union_set) {
956 obj.v = isl_union_set_apply(obj.v, umap);
957 } else
958 obj.v = isl_union_map_apply_range(obj.v, umap);
959 if (!obj.v)
960 goto error2;
962 if (isl_stream_eat(s, ')'))
963 goto error2;
965 return obj;
966 error:
967 isl_union_map_free(umap);
968 error2:
969 free_obj(obj);
970 obj.type = isl_obj_none;
971 obj.v = NULL;
972 return obj;
975 struct add_vertex_data {
976 struct isl_list *list;
977 int i;
980 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
982 struct add_vertex_data *data = (struct add_vertex_data *)user;
983 isl_basic_set *expr;
985 expr = isl_vertex_get_expr(vertex);
987 data->list->obj[data->i].type = isl_obj_set;
988 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
989 data->i++;
991 isl_vertex_free(vertex);
993 return 0;
996 static struct isl_obj vertices(struct isl_stream *s,
997 struct isl_hash_table *table)
999 isl_ctx *ctx;
1000 struct isl_obj obj;
1001 isl_set *set;
1002 isl_basic_set *hull;
1003 isl_vertices *vertices = NULL;
1004 struct isl_list *list = NULL;
1005 struct add_vertex_data data;
1007 obj = read_expr(s, table);
1008 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1009 set = obj.v;
1010 obj.v = NULL;
1011 set = isl_set_remove_divs(set);
1012 hull = isl_set_convex_hull(set);
1013 vertices = isl_basic_set_compute_vertices(hull);
1014 isl_basic_set_free(hull);
1016 ctx = isl_vertices_get_ctx(vertices);
1017 list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1018 if (!list)
1019 goto error;
1021 data.list = list;
1022 data.i = 0;
1024 if (isl_vertices_foreach_vertex(vertices, &add_vertex, &data) < 0)
1025 goto error;
1027 isl_vertices_free(vertices);
1029 obj.type = isl_obj_list;
1030 obj.v = list;
1032 return obj;
1033 error:
1034 isl_vertices_free(vertices);
1035 isl_list_free(list);
1036 free_obj(obj);
1037 obj.type = isl_obj_none;
1038 obj.v = NULL;
1039 return obj;
1042 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1044 struct isl_token *tok;
1046 if (isl_stream_eat_if_available(s, '+'))
1047 return transitive_closure(s->ctx, obj);
1049 tok = isl_stream_next_token(s);
1050 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1051 isl_stream_error(s, tok, "expecting -1");
1052 if (tok)
1053 isl_stream_push_token(s, tok);
1054 goto error;
1056 isl_token_free(tok);
1057 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1059 obj.v = isl_union_map_reverse(obj.v);
1060 if (!obj.v)
1061 goto error;
1063 return obj;
1064 error:
1065 free_obj(obj);
1066 obj.type = isl_obj_none;
1067 obj.v = NULL;
1068 return obj;
1071 static struct isl_obj read_from_file(struct isl_stream *s)
1073 struct isl_obj obj;
1074 struct isl_token *tok;
1075 struct isl_stream *s_file;
1076 FILE *file;
1078 tok = isl_stream_next_token(s);
1079 if (!tok || tok->type != ISL_TOKEN_STRING) {
1080 isl_stream_error(s, tok, "expecting filename");
1081 isl_token_free(tok);
1082 goto error;
1085 file = fopen(tok->u.s, "r");
1086 isl_token_free(tok);
1087 isl_assert(s->ctx, file, goto error);
1089 s_file = isl_stream_new_file(s->ctx, file);
1090 if (!s_file) {
1091 fclose(file);
1092 goto error;
1095 obj = isl_stream_read_obj(s_file);
1097 isl_stream_free(s_file);
1098 fclose(file);
1100 return obj;
1101 error:
1102 obj.type = isl_obj_none;
1103 obj.v = NULL;
1104 return obj;
1107 static struct isl_obj read_obj(struct isl_stream *s,
1108 struct isl_hash_table *table)
1110 struct isl_obj obj = { isl_obj_none, NULL };
1111 char *name = NULL;
1112 struct isc_un_op *op = NULL;
1114 if (isl_stream_eat_if_available(s, '(')) {
1115 obj = read_expr(s, table);
1116 if (!obj.v || isl_stream_eat(s, ')'))
1117 goto error;
1118 } else {
1119 op = read_prefix_un_op_if_available(s);
1120 if (op)
1121 return read_un_op_expr(s, table, op);
1123 if (isl_stream_eat_if_available(s, read_op))
1124 return read_from_file(s);
1125 if (isl_stream_eat_if_available(s, vertices_op))
1126 return vertices(s, table);
1128 name = isl_stream_read_ident_if_available(s);
1129 if (name)
1130 obj = stored_obj(s->ctx, table, name);
1131 else
1132 obj = isl_stream_read_obj(s);
1133 if (!obj.v)
1134 goto error;
1137 if (isl_stream_eat_if_available(s, '^'))
1138 obj = power(s, obj);
1139 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
1140 obj = obj_at_index(s, obj);
1141 else if (is_subtype(obj, isl_obj_union_map) &&
1142 isl_stream_eat_if_available(s, '(')) {
1143 obj = convert(obj, isl_obj_union_map);
1144 obj = apply(s, obj.v, table);
1147 return obj;
1148 error:
1149 free_obj(obj);
1150 obj.type = isl_obj_none;
1151 obj.v = NULL;
1152 return obj;
1155 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
1156 struct isl_obj lhs, struct isl_obj rhs)
1158 int i;
1160 for (i = 0; ; ++i) {
1161 if (!bin_ops[i].op)
1162 break;
1163 if (bin_ops[i].op != like->op)
1164 continue;
1165 if (!is_subtype(lhs, bin_ops[i].lhs))
1166 continue;
1167 if (!is_subtype(rhs, bin_ops[i].rhs))
1168 continue;
1170 return &bin_ops[i];
1173 for (i = 0; ; ++i) {
1174 if (!named_bin_ops[i].name)
1175 break;
1176 if (named_bin_ops[i].op.op != like->op)
1177 continue;
1178 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1179 continue;
1180 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
1181 continue;
1183 return &named_bin_ops[i].op;
1186 return NULL;
1189 static struct isl_obj read_expr(struct isl_stream *s,
1190 struct isl_hash_table *table)
1192 struct isl_obj obj = { isl_obj_none, NULL };
1193 struct isl_obj right_obj = { isl_obj_none, NULL };
1195 obj = read_obj(s, table);
1196 for (; obj.v;) {
1197 struct isc_bin_op *op = NULL;
1199 op = read_bin_op_if_available(s, obj);
1200 if (!op)
1201 break;
1203 right_obj = read_obj(s, table);
1205 op = find_matching_bin_op(op, obj, right_obj);
1207 isl_assert(s->ctx, op, goto error);
1208 obj = convert(obj, op->lhs);
1209 right_obj = convert(right_obj, op->rhs);
1210 obj.v = op->fn(obj.v, right_obj.v);
1211 obj.type = op->res;
1214 return obj;
1215 error:
1216 free_obj(right_obj);
1217 free_obj(obj);
1218 obj.type = isl_obj_none;
1219 obj.v = NULL;
1220 return obj;
1223 static __isl_give isl_printer *read_line(struct isl_stream *s,
1224 struct isl_hash_table *table, __isl_take isl_printer *p)
1226 struct isl_obj obj = { isl_obj_none, NULL };
1227 char *lhs = NULL;
1228 int assign = 0;
1229 struct isc_bin_op *op = NULL;
1231 if (!p)
1232 return NULL;
1233 if (isl_stream_is_empty(s))
1234 return p;
1236 assign = is_assign(s);
1237 if (assign) {
1238 lhs = isl_stream_read_ident_if_available(s);
1239 if (isl_stream_eat(s, ISL_TOKEN_DEF))
1240 goto error;
1243 obj = read_expr(s, table);
1244 if (obj.type == isl_obj_none || obj.v == NULL)
1245 goto error;
1246 if (isl_stream_eat(s, ';'))
1247 goto error;
1249 if (assign) {
1250 if (do_assign(s->ctx, table, lhs, obj))
1251 return;
1252 } else {
1253 p = obj.type->print(p, obj.v);
1254 p = isl_printer_end_line(p);
1255 free_obj(obj);
1258 return p;
1259 error:
1260 isl_stream_flush_tokens(s);
1261 isl_stream_skip_line(s);
1262 free(lhs);
1263 free_obj(obj);
1264 return p;
1267 int free_cb(void **entry, void *user)
1269 struct isl_named_obj *named = *entry;
1271 free_obj(named->obj);
1272 free(named->name);
1273 free(named);
1275 return 0;
1278 static void register_named_ops(struct isl_stream *s)
1280 int i;
1282 read_op = isl_stream_register_keyword(s, "read");
1283 assert(read_op != ISL_TOKEN_ERROR);
1284 vertices_op = isl_stream_register_keyword(s, "vertices");
1285 assert(vertices_op != ISL_TOKEN_ERROR);
1287 for (i = 0; ; ++i) {
1288 if (!named_un_ops[i].name)
1289 break;
1290 named_un_ops[i].op.op = isl_stream_register_keyword(s,
1291 named_un_ops[i].name);
1292 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
1295 for (i = 0; ; ++i) {
1296 if (!named_bin_ops[i].name)
1297 break;
1298 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
1299 named_bin_ops[i].name);
1300 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
1304 int main(int argc, char **argv)
1306 struct isl_ctx *ctx;
1307 struct isl_stream *s;
1308 struct isl_hash_table *table;
1309 struct iscc_options *options;
1310 isl_printer *p;
1312 options = iscc_options_new_with_defaults();
1313 assert(options);
1314 argc = iscc_options_parse(options, argc, argv, ISL_ARG_ALL);
1316 ctx = isl_ctx_alloc_with_options(iscc_options_arg, options);
1317 s = isl_stream_new_file(ctx, stdin);
1318 assert(s);
1319 table = isl_hash_table_alloc(ctx, 10);
1320 assert(table);
1321 p = isl_printer_to_file(ctx, stdout);
1322 p = isl_printer_set_output_format(p, options->format);
1323 assert(p);
1325 register_named_ops(s);
1327 while (!s->eof) {
1328 p = read_line(s, table, p);
1331 isl_printer_free(p);
1332 isl_hash_table_foreach(ctx, table, free_cb, NULL);
1333 isl_hash_table_free(ctx, table);
1334 isl_stream_free(s);
1335 isl_ctx_free(ctx);
1337 return 0;