iscc: add wrap and unwrap operations
[barvinok.git] / iscc.c
blobbbae2e3989ce3938c3ba82343d20e1c24881cd97
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_obj_union_map, isl_obj_union_pw_qpolynomial,
270 isl_obj_union_pw_qpolynomial,
271 (isc_bin_op_fn) &isl_union_map_apply_union_pw_qpolynomial },
272 { ISL_TOKEN_TO, isl_obj_union_set, isl_obj_union_set,
273 isl_obj_union_map,
274 (isc_bin_op_fn) &isl_union_map_from_domain_and_range },
275 { '=', isl_obj_union_set, isl_obj_union_set, isl_obj_bool,
276 (isc_bin_op_fn) &union_set_is_equal },
277 { '=', isl_obj_union_map, isl_obj_union_map, isl_obj_bool,
278 (isc_bin_op_fn) &union_map_is_equal },
279 { ISL_TOKEN_LE, isl_obj_union_set, isl_obj_union_set,
280 isl_obj_bool, (isc_bin_op_fn) &union_set_is_subset },
281 { ISL_TOKEN_LE, isl_obj_union_map, isl_obj_union_map,
282 isl_obj_bool, (isc_bin_op_fn) &union_map_is_subset },
283 { ISL_TOKEN_LT, isl_obj_union_set, isl_obj_union_set,
284 isl_obj_bool, (isc_bin_op_fn) &union_set_is_strict_subset },
285 { ISL_TOKEN_LT, isl_obj_union_map, isl_obj_union_map,
286 isl_obj_bool, (isc_bin_op_fn) &union_map_is_strict_subset },
287 { ISL_TOKEN_GE, isl_obj_union_set, isl_obj_union_set,
288 isl_obj_bool, (isc_bin_op_fn) &union_set_is_superset },
289 { ISL_TOKEN_GE, isl_obj_union_map, isl_obj_union_map,
290 isl_obj_bool, (isc_bin_op_fn) &union_map_is_superset },
291 { ISL_TOKEN_GT, isl_obj_union_set, isl_obj_union_set,
292 isl_obj_bool, (isc_bin_op_fn) &union_set_is_strict_superset },
293 { ISL_TOKEN_GT, isl_obj_union_map, isl_obj_union_map,
294 isl_obj_bool, (isc_bin_op_fn) &union_map_is_strict_superset },
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_add },
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_sub },
301 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_pw_qpolynomial,
302 isl_obj_union_pw_qpolynomial,
303 (isc_bin_op_fn) &isl_union_pw_qpolynomial_mul },
304 { '*', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
305 isl_obj_union_pw_qpolynomial,
306 (isc_bin_op_fn) &isl_union_pw_qpolynomial_intersect_domain },
307 { '*', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
308 isl_obj_union_pw_qpolynomial_fold,
309 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_intersect_domain },
310 { '@', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
311 isl_obj_union_pw_qpolynomial,
312 (isc_bin_op_fn) &isl_union_pw_qpolynomial_at },
313 { '@', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
314 isl_obj_union_pw_qpolynomial,
315 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_at },
316 { '%', isl_obj_union_set, isl_obj_union_set,
317 isl_obj_union_set,
318 (isc_bin_op_fn) &isl_union_set_gist },
319 { '%', isl_obj_union_map, isl_obj_union_map,
320 isl_obj_union_map,
321 (isc_bin_op_fn) &isl_union_map_gist },
322 { '%', isl_obj_union_pw_qpolynomial, isl_obj_union_set,
323 isl_obj_union_pw_qpolynomial,
324 (isc_bin_op_fn) &isl_union_pw_qpolynomial_gist },
325 { '%', isl_obj_union_pw_qpolynomial_fold, isl_obj_union_set,
326 isl_obj_union_pw_qpolynomial_fold,
327 (isc_bin_op_fn) &isl_union_pw_qpolynomial_fold_gist },
330 struct isc_named_bin_op named_bin_ops[] = {
331 { "cross", { -1, isl_obj_union_set, isl_obj_union_set,
332 isl_obj_union_set,
333 (isc_bin_op_fn) &isl_union_set_product } },
334 { "cross", { -1, isl_obj_union_map, isl_obj_union_map,
335 isl_obj_union_map,
336 (isc_bin_op_fn) &isl_union_map_product } },
337 NULL
340 __isl_give isl_set *union_set_sample(__isl_take isl_union_set *uset)
342 return isl_set_from_basic_set(isl_union_set_sample(uset));
345 __isl_give isl_map *union_map_sample(__isl_take isl_union_map *umap)
347 return isl_map_from_basic_map(isl_union_map_sample(umap));
350 static __isl_give struct isl_list *union_pw_qpolynomial_upper_bound(
351 __isl_take isl_union_pw_qpolynomial *upwqp)
353 isl_ctx *ctx;
354 struct isl_list *list;
355 int tight;
357 ctx = isl_union_pw_qpolynomial_get_ctx(upwqp);
358 list = isl_list_alloc(ctx, 2);
359 if (!list)
360 goto error;
362 list->obj[0].type = isl_obj_union_pw_qpolynomial_fold;
363 list->obj[0].v = isl_union_pw_qpolynomial_bound(upwqp,
364 isl_fold_max, &tight);
365 list->obj[1].type = isl_obj_bool;
366 list->obj[1].v = tight ? &isl_bool_true : &isl_bool_false;
367 if (tight < 0 || !list->obj[0].v)
368 goto error;
370 return list;
371 error:
372 isl_list_free(list);
373 return NULL;
376 #ifdef HAVE_CLOOG
377 struct iscc_codegen_data {
378 int nr;
379 int nparam;
380 int max_iter;
381 int n_scat;
382 isl_set *context;
383 CloogState *state;
384 CloogLoop **next;
385 CloogBlockList **nextBL;
386 CloogScatteringList *scatterings;
387 CloogScatteringList **nextScat;
390 static int add_domain(__isl_take isl_map *map, void *user)
392 struct iscc_codegen_data *data = (struct iscc_codegen_data *)user;
393 isl_ctx *ctx = isl_map_get_ctx(map);
394 isl_dim *dim;
395 isl_set *domain;
396 CloogStatement *statement;
397 CloogLoop *l;
398 CloogScatteringList *s;
399 int n_in;
401 n_in = isl_map_dim(map, isl_dim_in);
402 if (data->nr == 0) {
403 isl_dim *dim = isl_map_get_dim(map);
404 data->nparam = isl_map_dim(map, isl_dim_param);
405 data->n_scat = isl_map_dim(map, isl_dim_out);
406 dim = isl_dim_drop_inputs(dim, 0, n_in);
407 dim = isl_dim_drop_outputs(dim, 0, data->n_scat);
408 data->context = isl_set_universe(dim);
409 } else {
410 isl_assert(ctx, data->nparam == isl_map_dim(map, isl_dim_param),
411 goto error);
412 isl_assert(ctx, data->n_scat == isl_map_dim(map, isl_dim_out),
413 goto error);
415 if (n_in > data->max_iter)
416 data->max_iter = n_in;
418 l = cloog_loop_malloc(data->state);
419 if (!l)
420 goto error;
421 l->next = NULL;
422 l->inner = NULL;
423 domain = isl_map_domain(isl_map_copy(map));
424 l->domain = cloog_domain_from_isl_set(domain);
426 statement = cloog_statement_alloc(data->state, data->nr);
427 statement->usr = NULL;
428 dim = isl_map_get_dim(map);
429 statement->name = strdup(isl_dim_get_tuple_name(dim, isl_dim_in));
430 isl_dim_free(dim);
431 l->block = cloog_block_alloc(statement, 0, NULL, n_in);
433 s = isl_alloc_type(ctx, CloogScatteringList);
434 s->scatt = cloog_scattering_from_isl_map(map);
435 s->next = NULL;
437 *data->next = l;
438 *data->nextBL = cloog_block_list_alloc((*data->next)->block);
439 data->next = &(*data->next)->next;
440 data->nextBL = &(*data->nextBL)->next;
441 *data->nextScat = s;
442 data->nextScat = &(s->next);
444 data->nr++;
446 return 0;
447 error:
448 return -1;
451 char **generate_names(isl_ctx *ctx, int n, const char *prefix)
453 int i;
454 char **names;
456 names = isl_alloc_array(ctx, char *, n);
457 for (i = 0; i < n; i++) {
458 names[i] = isl_alloc_array(ctx, char, 50);
459 sprintf(names[i], "%s%d", prefix, i);
462 return names;
465 void *codegen(void *arg)
467 int i;
468 struct iscc_codegen_data data;
469 isl_dim *dim;
470 isl_union_map *umap = (isl_union_map *)arg;
471 isl_ctx *ctx = isl_union_map_get_ctx(umap);
472 CloogOptions *options;
473 CloogProgram *p;
474 struct clast_stmt *stmt;
476 data.state = cloog_isl_state_malloc(ctx);
477 options = cloog_options_malloc(data.state);
478 options->language = LANGUAGE_C;
479 p = cloog_program_malloc();
481 isl_assert(ctx, p, goto error);
482 p->names = cloog_names_malloc();
483 isl_assert(ctx, p->names, goto error2);
484 p->language = 'c';
486 data.context = NULL;
487 data.nr = 0;
488 data.nparam = -1;
489 data.max_iter = 0;
490 data.n_scat = -1;
491 data.next = &p->loop;
492 data.nextBL = &p->blocklist;
493 data.scatterings = NULL;
494 data.nextScat = &data.scatterings;
496 if (isl_union_map_foreach_map(umap, &add_domain, &data) < 0)
497 goto error3;
499 if (data.nr == 0)
500 goto error3;
502 p->names->nb_parameters = data.nparam;
503 p->names->parameters = isl_alloc_array(ctx, char *, data.nparam);
504 dim = isl_set_get_dim(data.context);
505 p->context = cloog_domain_from_isl_set(data.context);
506 for (i = 0; i < data.nparam; ++i) {
507 const char *s = isl_dim_get_name(dim, isl_dim_param, i);
508 p->names->parameters[i] = strdup(s);
510 isl_dim_free(dim);
512 p->names->nb_iterators = data.max_iter;
513 p->names->iterators = generate_names(ctx, data.max_iter, "i");
515 p->names->nb_scattering = data.n_scat;
516 p->names->scattering = generate_names(ctx, data.n_scat, "c");
517 p->names->nb_scalars = 0;
518 p->nb_scattdims = data.n_scat;
519 p->scaldims = (int *)malloc(p->nb_scattdims*(sizeof(int)));
520 for (i = 0; i < p->nb_scattdims; i++)
521 p->scaldims[i] = 0;
523 cloog_program_scatter(p, data.scatterings, options);
524 p = cloog_program_generate(p, options);
526 stmt = cloog_clast_create(p, options);
527 clast_pprint(stdout, stmt, 0, options);
528 cloog_clast_free(stmt);
530 error3:
531 cloog_scattering_list_free(data.scatterings);
532 error2:
533 cloog_program_free(p);
534 error:
535 cloog_options_free(options);
536 cloog_state_free(data.state);
537 isl_union_map_free(umap);
538 return NULL;
540 #endif
542 typedef void *(*isc_un_op_fn)(void *arg);
543 struct isc_un_op {
544 enum isl_token_type op;
545 isl_obj_type arg;
546 isl_obj_type res;
547 isc_un_op_fn fn;
549 struct isc_named_un_op {
550 char *name;
551 struct isc_un_op op;
553 struct isc_named_un_op named_un_ops[] = {
554 {"aff", { -1, isl_obj_union_map, isl_obj_union_map,
555 (isc_un_op_fn) &isl_union_map_affine_hull } },
556 {"aff", { -1, isl_obj_union_set, isl_obj_union_set,
557 (isc_un_op_fn) &isl_union_set_affine_hull } },
558 {"card", { -1, isl_obj_union_set,
559 isl_obj_union_pw_qpolynomial,
560 (isc_un_op_fn) &isl_union_set_card } },
561 {"card", { -1, isl_obj_union_map,
562 isl_obj_union_pw_qpolynomial,
563 (isc_un_op_fn) &isl_union_map_card } },
564 {"coalesce", { -1, isl_obj_union_set, isl_obj_union_set,
565 (isc_un_op_fn) &isl_union_set_coalesce } },
566 {"coalesce", { -1, isl_obj_union_map, isl_obj_union_map,
567 (isc_un_op_fn) &isl_union_map_coalesce } },
568 {"coalesce", { -1, isl_obj_union_pw_qpolynomial,
569 isl_obj_union_pw_qpolynomial,
570 (isc_un_op_fn) &isl_union_pw_qpolynomial_coalesce } },
571 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold,
572 isl_obj_union_pw_qpolynomial_fold,
573 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_coalesce } },
574 #ifdef HAVE_CLOOG
575 {"codegen", { -1, isl_obj_union_map, isl_obj_none, &codegen } },
576 #endif
577 {"deltas", { -1, isl_obj_union_map, isl_obj_union_set,
578 (isc_un_op_fn) &isl_union_map_deltas } },
579 {"dom", { -1, isl_obj_union_map, isl_obj_union_set,
580 (isc_un_op_fn) &isl_union_map_domain } },
581 {"dom", { -1, isl_obj_union_pw_qpolynomial, isl_obj_union_set,
582 (isc_un_op_fn) &isl_union_pw_qpolynomial_domain } },
583 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold,
584 isl_obj_union_set,
585 (isc_un_op_fn) &isl_union_pw_qpolynomial_fold_domain } },
586 {"ran", { -1, isl_obj_union_map, isl_obj_union_set,
587 (isc_un_op_fn) &isl_union_map_range } },
588 {"lexmin", { -1, isl_obj_union_map, isl_obj_union_map,
589 (isc_un_op_fn) &isl_union_map_lexmin } },
590 {"lexmax", { -1, isl_obj_union_map, isl_obj_union_map,
591 (isc_un_op_fn) &isl_union_map_lexmax } },
592 {"lexmin", { -1, isl_obj_union_set, isl_obj_union_set,
593 (isc_un_op_fn) &isl_union_set_lexmin } },
594 {"lexmax", { -1, isl_obj_union_set, isl_obj_union_set,
595 (isc_un_op_fn) &isl_union_set_lexmax } },
596 {"sample", { -1, isl_obj_union_set, isl_obj_set,
597 (isc_un_op_fn) &union_set_sample } },
598 {"sample", { -1, isl_obj_union_map, isl_obj_map,
599 (isc_un_op_fn) &union_map_sample } },
600 {"sum", { -1, isl_obj_union_pw_qpolynomial,
601 isl_obj_union_pw_qpolynomial,
602 (isc_un_op_fn) &isl_union_pw_qpolynomial_sum } },
603 {"ub", { -1, isl_obj_union_pw_qpolynomial, isl_obj_list,
604 (isc_un_op_fn) &union_pw_qpolynomial_upper_bound } },
605 {"unwrap", { -1, isl_obj_union_set, isl_obj_union_map,
606 (isc_un_op_fn) &isl_union_set_unwrap } },
607 {"wrap", { -1, isl_obj_union_map, isl_obj_union_set,
608 (isc_un_op_fn) &isl_union_map_wrap } },
609 NULL
612 struct isl_named_obj {
613 char *name;
614 struct isl_obj obj;
617 static void free_obj(struct isl_obj obj)
619 obj.type->free(obj.v);
622 static int same_name(const void *entry, const void *val)
624 const struct isl_named_obj *named = (const struct isl_named_obj *)entry;
626 return !strcmp(named->name, val);
629 static int do_assign(struct isl_ctx *ctx, struct isl_hash_table *table,
630 char *name, struct isl_obj obj)
632 struct isl_hash_table_entry *entry;
633 uint32_t name_hash;
634 struct isl_named_obj *named;
636 name_hash = isl_hash_string(isl_hash_init(), name);
637 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 1);
638 if (!entry)
639 goto error;
640 if (entry->data) {
641 named = entry->data;
642 free_obj(named->obj);
643 free(name);
644 } else {
645 named = isl_alloc_type(ctx, struct isl_named_obj);
646 if (!named)
647 goto error;
648 named->name = name;
649 entry->data = named;
651 named->obj = obj;
653 return 0;
654 error:
655 free_obj(obj);
656 free(name);
657 return -1;
660 static struct isl_obj stored_obj(struct isl_ctx *ctx,
661 struct isl_hash_table *table, char *name)
663 struct isl_obj obj = { isl_obj_none, NULL };
664 struct isl_hash_table_entry *entry;
665 uint32_t name_hash;
667 name_hash = isl_hash_string(isl_hash_init(), name);
668 entry = isl_hash_table_find(ctx, table, name_hash, same_name, name, 0);
669 if (entry) {
670 struct isl_named_obj *named;
671 named = entry->data;
672 obj = named->obj;
675 free(name);
676 obj.v = obj.type->copy(obj.v);
677 return obj;
680 static int is_subtype(struct isl_obj obj, isl_obj_type super)
682 if (obj.type == super)
683 return 1;
684 if (obj.type == isl_obj_map && super == isl_obj_union_map)
685 return 1;
686 if (obj.type == isl_obj_set && super == isl_obj_union_set)
687 return 1;
688 if (obj.type == isl_obj_pw_qpolynomial &&
689 super == isl_obj_union_pw_qpolynomial)
690 return 1;
691 if (obj.type == isl_obj_pw_qpolynomial_fold &&
692 super == isl_obj_union_pw_qpolynomial_fold)
693 return 1;
694 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v))
695 return 1;
696 return 0;
699 static struct isl_obj convert(struct isl_obj obj, isl_obj_type type)
701 if (obj.type == type)
702 return obj;
703 if (obj.type == isl_obj_map && type == isl_obj_union_map) {
704 obj.type = isl_obj_union_map;
705 obj.v = isl_union_map_from_map(obj.v);
706 return obj;
708 if (obj.type == isl_obj_set && type == isl_obj_union_set) {
709 obj.type = isl_obj_union_set;
710 obj.v = isl_union_set_from_set(obj.v);
711 return obj;
713 if (obj.type == isl_obj_pw_qpolynomial &&
714 type == isl_obj_union_pw_qpolynomial) {
715 obj.type = isl_obj_union_pw_qpolynomial;
716 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
717 return obj;
719 if (obj.type == isl_obj_pw_qpolynomial_fold &&
720 type == isl_obj_union_pw_qpolynomial_fold) {
721 obj.type = isl_obj_union_pw_qpolynomial_fold;
722 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
723 return obj;
725 if (obj.type == isl_obj_union_set && isl_union_set_is_empty(obj.v)) {
726 if (type == isl_obj_union_map) {
727 obj.type = isl_obj_union_map;
728 return obj;
730 if (type == isl_obj_union_pw_qpolynomial) {
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_zero(dim);
734 obj.type = isl_obj_union_pw_qpolynomial;
735 return obj;
737 if (type == isl_obj_union_pw_qpolynomial_fold) {
738 isl_dim *dim = isl_union_set_get_dim(obj.v);
739 isl_union_set_free(obj.v);
740 obj.v = isl_union_pw_qpolynomial_fold_zero(dim);
741 obj.type = isl_obj_union_pw_qpolynomial_fold;
742 return obj;
745 free_obj(obj);
746 obj.type = isl_obj_none;
747 obj.v = NULL;
748 return obj;
751 static struct isc_bin_op *read_bin_op_if_available(struct isl_stream *s,
752 struct isl_obj lhs)
754 int i;
755 struct isl_token *tok;
757 tok = isl_stream_next_token(s);
758 if (!tok)
759 return NULL;
761 for (i = 0; ; ++i) {
762 if (!bin_ops[i].op)
763 break;
764 if (bin_ops[i].op != tok->type)
765 continue;
766 if (!is_subtype(lhs, bin_ops[i].lhs))
767 continue;
769 isl_token_free(tok);
770 return &bin_ops[i];
773 for (i = 0; ; ++i) {
774 if (!named_bin_ops[i].name)
775 break;
776 if (named_bin_ops[i].op.op != tok->type)
777 continue;
778 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
779 continue;
781 isl_token_free(tok);
782 return &named_bin_ops[i].op;
785 isl_stream_push_token(s, tok);
787 return NULL;
790 static struct isc_un_op *read_prefix_un_op_if_available(struct isl_stream *s)
792 int i;
793 struct isl_token *tok;
795 tok = isl_stream_next_token(s);
796 if (!tok)
797 return NULL;
799 for (i = 0; ; ++i) {
800 if (!named_un_ops[i].name)
801 break;
802 if (named_un_ops[i].op.op != tok->type)
803 continue;
805 isl_token_free(tok);
806 return &named_un_ops[i].op;
809 isl_stream_push_token(s, tok);
811 return NULL;
814 static struct isc_un_op *find_matching_un_op(struct isc_un_op *like,
815 struct isl_obj arg)
817 int i;
819 for (i = 0; ; ++i) {
820 if (!named_un_ops[i].name)
821 break;
822 if (named_un_ops[i].op.op != like->op)
823 continue;
824 if (!is_subtype(arg, named_un_ops[i].op.arg))
825 continue;
827 return &named_un_ops[i].op;
830 return NULL;
833 static int is_assign(struct isl_stream *s)
835 struct isl_token *tok;
836 struct isl_token *tok2;
837 int assign;
839 tok = isl_stream_next_token(s);
840 if (!tok)
841 return 0;
842 if (tok->type != ISL_TOKEN_IDENT) {
843 isl_stream_push_token(s, tok);
844 return 0;
847 tok2 = isl_stream_next_token(s);
848 if (!tok2) {
849 isl_stream_push_token(s, tok);
850 return 0;
852 assign = tok2->type == ISL_TOKEN_DEF;
853 isl_stream_push_token(s, tok2);
854 isl_stream_push_token(s, tok);
856 return assign;
859 static struct isl_obj read_obj(struct isl_stream *s,
860 struct isl_hash_table *table);
861 static struct isl_obj read_expr(struct isl_stream *s,
862 struct isl_hash_table *table);
864 static struct isl_obj read_un_op_expr(struct isl_stream *s,
865 struct isl_hash_table *table, struct isc_un_op *op)
867 struct isl_obj obj = { isl_obj_none, NULL };
869 obj = read_obj(s, table);
870 if (!obj.v)
871 goto error;
873 op = find_matching_un_op(op, obj);
875 isl_assert(s->ctx, op, goto error);
876 obj = convert(obj, op->arg);
877 obj.v = op->fn(obj.v);
878 obj.type = op->res;
880 return obj;
881 error:
882 free_obj(obj);
883 obj.type = isl_obj_none;
884 obj.v = NULL;
885 return obj;
888 static struct isl_obj transitive_closure(struct isl_ctx *ctx, struct isl_obj obj)
890 struct isl_list *list;
891 int exact;
893 if (obj.type == isl_obj_map)
894 obj = convert(obj, isl_obj_union_map);
895 isl_assert(ctx, obj.type == isl_obj_union_map, goto error);
896 list = isl_list_alloc(ctx, 2);
897 if (!list)
898 goto error;
900 list->obj[0].type = isl_obj_union_map;
901 list->obj[0].v = isl_union_map_transitive_closure(obj.v, &exact);
902 list->obj[1].type = isl_obj_bool;
903 list->obj[1].v = exact ? &isl_bool_true : &isl_bool_false;
904 obj.v = list;
905 obj.type = isl_obj_list;
906 if (exact < 0 || !list->obj[0].v)
907 goto error;
909 return obj;
910 error:
911 free_obj(obj);
912 obj.type = isl_obj_none;
913 obj.v = NULL;
914 return obj;
917 static struct isl_obj obj_at_index(struct isl_stream *s, struct isl_obj obj)
919 struct isl_list *list = obj.v;
920 struct isl_token *tok;
921 int i;
923 tok = isl_stream_next_token(s);
924 if (!tok || tok->type != ISL_TOKEN_VALUE) {
925 isl_stream_error(s, tok, "expecting index");
926 if (tok)
927 isl_stream_push_token(s, tok);
928 goto error;
930 i = isl_int_get_si(tok->u.v);
931 isl_token_free(tok);
932 isl_assert(s->ctx, i < list->n, goto error);
933 if (isl_stream_eat(s, ']'))
934 goto error;
936 obj = list->obj[i];
937 obj.v = obj.type->copy(obj.v);
939 isl_list_free(list);
941 return obj;
942 error:
943 free_obj(obj);
944 obj.type = isl_obj_none;
945 obj.v = NULL;
946 return obj;
949 static struct isl_obj apply(struct isl_stream *s, __isl_take isl_union_map *umap,
950 struct isl_hash_table *table)
952 struct isl_obj obj;
954 obj = read_expr(s, table);
955 isl_assert(s->ctx, is_subtype(obj, isl_obj_union_set) ||
956 is_subtype(obj, isl_obj_union_map), goto error);
958 if (obj.type == isl_obj_set)
959 obj = convert(obj, isl_obj_union_set);
960 else if (obj.type == isl_obj_map)
961 obj = convert(obj, isl_obj_union_map);
962 if (obj.type == isl_obj_union_set) {
963 obj.v = isl_union_set_apply(obj.v, umap);
964 } else
965 obj.v = isl_union_map_apply_range(obj.v, umap);
966 if (!obj.v)
967 goto error2;
969 if (isl_stream_eat(s, ')'))
970 goto error2;
972 return obj;
973 error:
974 isl_union_map_free(umap);
975 error2:
976 free_obj(obj);
977 obj.type = isl_obj_none;
978 obj.v = NULL;
979 return obj;
982 struct add_vertex_data {
983 struct isl_list *list;
984 int i;
987 static int add_vertex(__isl_take isl_vertex *vertex, void *user)
989 struct add_vertex_data *data = (struct add_vertex_data *)user;
990 isl_basic_set *expr;
992 expr = isl_vertex_get_expr(vertex);
994 data->list->obj[data->i].type = isl_obj_set;
995 data->list->obj[data->i].v = isl_set_from_basic_set(expr);
996 data->i++;
998 isl_vertex_free(vertex);
1000 return 0;
1003 static struct isl_obj vertices(struct isl_stream *s,
1004 struct isl_hash_table *table)
1006 isl_ctx *ctx;
1007 struct isl_obj obj;
1008 isl_set *set;
1009 isl_basic_set *hull;
1010 isl_vertices *vertices = NULL;
1011 struct isl_list *list = NULL;
1012 struct add_vertex_data data;
1014 obj = read_expr(s, table);
1015 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1016 set = obj.v;
1017 obj.v = NULL;
1018 set = isl_set_remove_divs(set);
1019 hull = isl_set_convex_hull(set);
1020 vertices = isl_basic_set_compute_vertices(hull);
1021 isl_basic_set_free(hull);
1023 ctx = isl_vertices_get_ctx(vertices);
1024 list = isl_list_alloc(ctx, isl_vertices_get_n_vertices(vertices));
1025 if (!list)
1026 goto error;
1028 data.list = list;
1029 data.i = 0;
1031 if (isl_vertices_foreach_vertex(vertices, &add_vertex, &data) < 0)
1032 goto error;
1034 isl_vertices_free(vertices);
1036 obj.type = isl_obj_list;
1037 obj.v = list;
1039 return obj;
1040 error:
1041 isl_vertices_free(vertices);
1042 isl_list_free(list);
1043 free_obj(obj);
1044 obj.type = isl_obj_none;
1045 obj.v = NULL;
1046 return obj;
1049 static struct isl_obj power(struct isl_stream *s, struct isl_obj obj)
1051 struct isl_token *tok;
1053 if (isl_stream_eat_if_available(s, '+'))
1054 return transitive_closure(s->ctx, obj);
1056 tok = isl_stream_next_token(s);
1057 if (!tok || tok->type != ISL_TOKEN_VALUE || isl_int_cmp_si(tok->u.v, -1)) {
1058 isl_stream_error(s, tok, "expecting -1");
1059 if (tok)
1060 isl_stream_push_token(s, tok);
1061 goto error;
1063 isl_token_free(tok);
1064 isl_assert(s->ctx, obj.type == isl_obj_union_map, goto error);
1066 obj.v = isl_union_map_reverse(obj.v);
1067 if (!obj.v)
1068 goto error;
1070 return obj;
1071 error:
1072 free_obj(obj);
1073 obj.type = isl_obj_none;
1074 obj.v = NULL;
1075 return obj;
1078 static struct isl_obj read_from_file(struct isl_stream *s)
1080 struct isl_obj obj;
1081 struct isl_token *tok;
1082 struct isl_stream *s_file;
1083 FILE *file;
1085 tok = isl_stream_next_token(s);
1086 if (!tok || tok->type != ISL_TOKEN_STRING) {
1087 isl_stream_error(s, tok, "expecting filename");
1088 isl_token_free(tok);
1089 goto error;
1092 file = fopen(tok->u.s, "r");
1093 isl_token_free(tok);
1094 isl_assert(s->ctx, file, goto error);
1096 s_file = isl_stream_new_file(s->ctx, file);
1097 if (!s_file) {
1098 fclose(file);
1099 goto error;
1102 obj = isl_stream_read_obj(s_file);
1104 isl_stream_free(s_file);
1105 fclose(file);
1107 return obj;
1108 error:
1109 obj.type = isl_obj_none;
1110 obj.v = NULL;
1111 return obj;
1114 static struct isl_obj read_obj(struct isl_stream *s,
1115 struct isl_hash_table *table)
1117 struct isl_obj obj = { isl_obj_none, NULL };
1118 char *name = NULL;
1119 struct isc_un_op *op = NULL;
1121 if (isl_stream_eat_if_available(s, '(')) {
1122 obj = read_expr(s, table);
1123 if (!obj.v || isl_stream_eat(s, ')'))
1124 goto error;
1125 } else {
1126 op = read_prefix_un_op_if_available(s);
1127 if (op)
1128 return read_un_op_expr(s, table, op);
1130 if (isl_stream_eat_if_available(s, read_op))
1131 return read_from_file(s);
1132 if (isl_stream_eat_if_available(s, vertices_op))
1133 return vertices(s, table);
1135 name = isl_stream_read_ident_if_available(s);
1136 if (name)
1137 obj = stored_obj(s->ctx, table, name);
1138 else
1139 obj = isl_stream_read_obj(s);
1140 if (!obj.v)
1141 goto error;
1144 if (isl_stream_eat_if_available(s, '^'))
1145 obj = power(s, obj);
1146 else if (obj.type == isl_obj_list && isl_stream_eat_if_available(s, '['))
1147 obj = obj_at_index(s, obj);
1148 else if (is_subtype(obj, isl_obj_union_map) &&
1149 isl_stream_eat_if_available(s, '(')) {
1150 obj = convert(obj, isl_obj_union_map);
1151 obj = apply(s, obj.v, table);
1154 return obj;
1155 error:
1156 free_obj(obj);
1157 obj.type = isl_obj_none;
1158 obj.v = NULL;
1159 return obj;
1162 static struct isc_bin_op *find_matching_bin_op(struct isc_bin_op *like,
1163 struct isl_obj lhs, struct isl_obj rhs)
1165 int i;
1167 for (i = 0; ; ++i) {
1168 if (!bin_ops[i].op)
1169 break;
1170 if (bin_ops[i].op != like->op)
1171 continue;
1172 if (!is_subtype(lhs, bin_ops[i].lhs))
1173 continue;
1174 if (!is_subtype(rhs, bin_ops[i].rhs))
1175 continue;
1177 return &bin_ops[i];
1180 for (i = 0; ; ++i) {
1181 if (!named_bin_ops[i].name)
1182 break;
1183 if (named_bin_ops[i].op.op != like->op)
1184 continue;
1185 if (!is_subtype(lhs, named_bin_ops[i].op.lhs))
1186 continue;
1187 if (!is_subtype(rhs, named_bin_ops[i].op.rhs))
1188 continue;
1190 return &named_bin_ops[i].op;
1193 return NULL;
1196 static struct isl_obj read_expr(struct isl_stream *s,
1197 struct isl_hash_table *table)
1199 struct isl_obj obj = { isl_obj_none, NULL };
1200 struct isl_obj right_obj = { isl_obj_none, NULL };
1202 obj = read_obj(s, table);
1203 for (; obj.v;) {
1204 struct isc_bin_op *op = NULL;
1206 op = read_bin_op_if_available(s, obj);
1207 if (!op)
1208 break;
1210 right_obj = read_obj(s, table);
1212 op = find_matching_bin_op(op, obj, right_obj);
1214 isl_assert(s->ctx, op, goto error);
1215 obj = convert(obj, op->lhs);
1216 right_obj = convert(right_obj, op->rhs);
1217 obj.v = op->fn(obj.v, right_obj.v);
1218 obj.type = op->res;
1221 return obj;
1222 error:
1223 free_obj(right_obj);
1224 free_obj(obj);
1225 obj.type = isl_obj_none;
1226 obj.v = NULL;
1227 return obj;
1230 static __isl_give isl_printer *read_line(struct isl_stream *s,
1231 struct isl_hash_table *table, __isl_take isl_printer *p)
1233 struct isl_obj obj = { isl_obj_none, NULL };
1234 char *lhs = NULL;
1235 int assign = 0;
1236 struct isc_bin_op *op = NULL;
1238 if (!p)
1239 return NULL;
1240 if (isl_stream_is_empty(s))
1241 return p;
1243 assign = is_assign(s);
1244 if (assign) {
1245 lhs = isl_stream_read_ident_if_available(s);
1246 if (isl_stream_eat(s, ISL_TOKEN_DEF))
1247 goto error;
1250 obj = read_expr(s, table);
1251 if (obj.type == isl_obj_none || obj.v == NULL)
1252 goto error;
1253 if (isl_stream_eat(s, ';'))
1254 goto error;
1256 if (assign) {
1257 if (do_assign(s->ctx, table, lhs, obj))
1258 return;
1259 } else {
1260 p = obj.type->print(p, obj.v);
1261 p = isl_printer_end_line(p);
1262 free_obj(obj);
1265 return p;
1266 error:
1267 isl_stream_flush_tokens(s);
1268 isl_stream_skip_line(s);
1269 free(lhs);
1270 free_obj(obj);
1271 return p;
1274 int free_cb(void **entry, void *user)
1276 struct isl_named_obj *named = *entry;
1278 free_obj(named->obj);
1279 free(named->name);
1280 free(named);
1282 return 0;
1285 static void register_named_ops(struct isl_stream *s)
1287 int i;
1289 read_op = isl_stream_register_keyword(s, "read");
1290 assert(read_op != ISL_TOKEN_ERROR);
1291 vertices_op = isl_stream_register_keyword(s, "vertices");
1292 assert(vertices_op != ISL_TOKEN_ERROR);
1294 for (i = 0; ; ++i) {
1295 if (!named_un_ops[i].name)
1296 break;
1297 named_un_ops[i].op.op = isl_stream_register_keyword(s,
1298 named_un_ops[i].name);
1299 assert(named_un_ops[i].op.op != ISL_TOKEN_ERROR);
1302 for (i = 0; ; ++i) {
1303 if (!named_bin_ops[i].name)
1304 break;
1305 named_bin_ops[i].op.op = isl_stream_register_keyword(s,
1306 named_bin_ops[i].name);
1307 assert(named_bin_ops[i].op.op != ISL_TOKEN_ERROR);
1311 int main(int argc, char **argv)
1313 struct isl_ctx *ctx;
1314 struct isl_stream *s;
1315 struct isl_hash_table *table;
1316 struct iscc_options *options;
1317 isl_printer *p;
1319 options = iscc_options_new_with_defaults();
1320 assert(options);
1321 argc = iscc_options_parse(options, argc, argv, ISL_ARG_ALL);
1323 ctx = isl_ctx_alloc_with_options(iscc_options_arg, options);
1324 s = isl_stream_new_file(ctx, stdin);
1325 assert(s);
1326 table = isl_hash_table_alloc(ctx, 10);
1327 assert(table);
1328 p = isl_printer_to_file(ctx, stdout);
1329 p = isl_printer_set_output_format(p, options->format);
1330 assert(p);
1332 register_named_ops(s);
1334 while (!s->eof) {
1335 p = read_line(s, table, p);
1338 isl_printer_free(p);
1339 isl_hash_table_foreach(ctx, table, free_cb, NULL);
1340 isl_hash_table_free(ctx, table);
1341 isl_stream_free(s);
1342 isl_ctx_free(ctx);
1344 return 0;