2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
20 #include <isl/stream.h>
21 #include "isl_map_private.h"
23 #include "isl_polynomial_private.h"
24 #include <isl/union_map.h>
25 #include <isl_mat_private.h>
31 struct variable *next;
40 static struct vars *vars_new(struct isl_ctx *ctx)
43 v = isl_alloc_type(ctx, struct vars);
52 static void variable_free(struct variable *var)
55 struct variable *next = var->next;
56 isl_vec_free(var->def);
63 static void vars_free(struct vars *v)
71 static void vars_drop(struct vars *v, int n)
82 struct variable *next = var->next;
83 isl_vec_free(var->def);
91 static struct variable *variable_new(struct vars *v, const char *name, int len,
95 var = isl_alloc_type(v->ctx, struct variable);
98 var->name = strdup(name);
99 var->name[len] = '\0';
109 static int vars_pos(struct vars *v, const char *s, int len)
116 for (q = v->v; q; q = q->next) {
117 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
124 v->v = variable_new(v, s, len, v->n);
132 static int vars_add_anon(struct vars *v)
134 v->v = variable_new(v, "", 0, v->n);
143 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
144 enum isl_dim_type type, unsigned pos, char *name)
153 prime = strchr(name, '\'');
156 bmap = isl_basic_map_set_dim_name(bmap, type, pos, name);
163 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
165 struct isl_token *tok;
167 tok = isl_stream_next_token(s);
168 if (!tok || tok->type != ISL_TOKEN_VALUE) {
169 isl_stream_error(s, tok, "expecting constant value");
173 isl_int_mul(*f, *f, tok->u.v);
177 if (isl_stream_eat_if_available(s, '*'))
178 return accept_cst_factor(s, f);
186 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v);
187 static int read_div_definition(struct isl_stream *s, struct vars *v);
189 static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
192 struct isl_token *tok = NULL;
195 tok = isl_stream_next_token(s);
197 isl_stream_error(s, NULL, "unexpected EOF");
200 if (tok->type == ISL_TOKEN_IDENT) {
202 int pos = vars_pos(v, tok->u.s, -1);
206 isl_stream_error(s, tok, "unknown identifier");
210 aff = isl_vec_alloc(v->ctx, 1 + v->n);
213 isl_seq_clr(aff->el, aff->size);
214 isl_int_set_si(aff->el[1 + pos], 1);
216 } else if (tok->type == ISL_TOKEN_VALUE) {
217 if (isl_stream_eat_if_available(s, '*')) {
218 aff = accept_affine_factor(s, v);
219 aff = isl_vec_scale(aff, tok->u.v);
221 aff = isl_vec_alloc(v->ctx, 1 + v->n);
224 isl_seq_clr(aff->el, aff->size);
225 isl_int_set(aff->el[0], tok->u.v);
228 } else if (tok->type == '(') {
231 aff = accept_affine(s, v);
234 if (isl_stream_eat(s, ')'))
236 } else if (tok->type == '[') {
237 if (vars_add_anon(v) < 0)
239 aff = isl_vec_alloc(v->ctx, 1 + v->n);
242 isl_seq_clr(aff->el, aff->size);
243 isl_int_set_si(aff->el[1 + v->n - 1], 1);
244 isl_stream_push_token(s, tok);
246 if (read_div_definition(s, v) < 0)
249 isl_stream_error(s, tok, "expecting factor");
252 if (isl_stream_eat_if_available(s, '*')) {
255 isl_int_set_si(f, 1);
256 if (accept_cst_factor(s, &f) < 0) {
260 aff = isl_vec_scale(aff, f);
271 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
273 struct isl_token *tok = NULL;
277 aff = isl_vec_alloc(v->ctx, 1 + v->n);
280 isl_seq_clr(aff->el, aff->size);
283 tok = isl_stream_next_token(s);
285 isl_stream_error(s, NULL, "unexpected EOF");
288 if (tok->type == '-') {
293 if (tok->type == '(' || tok->type == '[' ||
294 tok->type == ISL_TOKEN_IDENT) {
296 isl_stream_push_token(s, tok);
298 aff2 = accept_affine_factor(s, v);
300 aff2 = isl_vec_scale(aff2, s->ctx->negone);
301 aff = isl_vec_zero_extend(aff, 1 + v->n);
302 aff = isl_vec_add(aff, aff2);
306 } else if (tok->type == ISL_TOKEN_VALUE) {
308 isl_int_neg(tok->u.v, tok->u.v);
309 if (isl_stream_eat_if_available(s, '*') ||
310 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
312 aff2 = accept_affine_factor(s, v);
313 aff2 = isl_vec_scale(aff2, tok->u.v);
314 aff = isl_vec_zero_extend(aff, 1 + v->n);
315 aff = isl_vec_add(aff, aff2);
319 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
323 isl_stream_error(s, tok, "unexpected isl_token");
324 isl_stream_push_token(s, tok);
330 tok = isl_stream_next_token(s);
331 if (tok && tok->type == '-') {
334 } else if (tok && tok->type == '+') {
337 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
338 isl_int_is_neg(tok->u.v)) {
339 isl_stream_push_token(s, tok);
342 isl_stream_push_token(s, tok);
354 /* Add any variables in the variable list "v" that are not already in "bmap"
355 * as existentially quantified variables in "bmap".
357 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
362 struct variable *var;
364 extra = v->n - isl_basic_map_total_dim(bmap);
369 bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
370 extra, 0, 2 * extra);
372 for (i = 0; i < extra; ++i)
373 if (isl_basic_map_alloc_div(bmap) < 0)
376 for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
377 int k = bmap->n_div - 1 - i;
379 isl_seq_cpy(bmap->div[k], var->def->el, var->def->size);
380 isl_seq_clr(bmap->div[k] + var->def->size,
381 2 + v->n - var->def->size);
383 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
389 isl_basic_map_free(bmap);
393 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
394 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
397 isl_basic_map *def = NULL;
402 if (vars_add_anon(v) < 0)
406 vec = accept_affine(s, v);
410 dim = isl_basic_map_get_dim(bmap);
411 def = isl_basic_map_universe(dim);
412 def = add_divs(def, v);
413 def = isl_basic_map_extend_constraints(def, 1, 0);
414 k = isl_basic_map_alloc_equality(def);
416 isl_seq_cpy(def->eq[k], vec->el, vec->size);
417 isl_int_set_si(def->eq[k][1 + n - 1], -1);
423 vars_drop(v, v->n - n);
425 def = isl_basic_map_simplify(def);
426 def = isl_basic_map_finalize(def);
427 bmap = isl_basic_map_intersect(bmap, def);
430 isl_basic_map_free(bmap);
431 isl_basic_map_free(def);
435 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
436 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
439 struct isl_token *tok;
441 while ((tok = isl_stream_next_token(s)) != NULL) {
444 if (tok->type == ISL_TOKEN_IDENT) {
446 int p = vars_pos(v, tok->u.s, -1);
453 bmap = isl_basic_map_add(bmap, type, 1);
454 bmap = set_name(bmap, type, i, v->v->name);
456 } else if (tok->type == ISL_TOKEN_IDENT ||
457 tok->type == ISL_TOKEN_VALUE ||
460 if (type == isl_dim_param) {
461 isl_stream_error(s, tok,
462 "expecting unique identifier");
465 isl_stream_push_token(s, tok);
467 bmap = isl_basic_map_add(bmap, type, 1);
468 bmap = read_var_def(s, bmap, type, v);
472 tok = isl_stream_next_token(s);
473 if (!tok || tok->type != ',')
480 isl_stream_push_token(s, tok);
485 isl_basic_map_free(bmap);
489 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
494 struct isl_token *tok = NULL;
496 vec = accept_affine(s, v);
497 mat = isl_mat_from_row_vec(vec);
502 tok = isl_stream_next_token(s);
504 isl_stream_error(s, NULL, "unexpected EOF");
507 if (tok->type != ',') {
508 isl_stream_push_token(s, tok);
513 vec = accept_affine(s, v);
514 mat = isl_mat_add_zero_cols(mat, 1 + v->n - isl_mat_cols(mat));
515 mat = isl_mat_vec_concat(mat, vec);
526 static int read_div_definition(struct isl_stream *s, struct vars *v)
528 struct isl_token *tok;
531 struct variable *var;
533 if (isl_stream_eat(s, '['))
536 tok = isl_stream_next_token(s);
539 if (tok->type == '(') {
543 isl_stream_push_token(s, tok);
547 aff = accept_affine(s, v);
551 var->def = isl_vec_alloc(s->ctx, 2 + v->n);
557 isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
561 if (seen_paren && isl_stream_eat(s, ')'))
563 if (isl_stream_eat(s, '/'))
566 tok = isl_stream_next_token(s);
569 if (tok->type != ISL_TOKEN_VALUE) {
570 isl_stream_error(s, tok, "expected denominator");
571 isl_stream_push_token(s, tok);
574 isl_int_set(var->def->el[0], tok->u.v);
577 if (isl_stream_eat(s, ']'))
583 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
584 struct vars *v, struct isl_basic_map *bmap, int k)
586 struct variable *var = v->v;
588 if (read_div_definition(s, v) < 0)
591 isl_seq_cpy(bmap->div[k], var->def->el, 2 + v->n);
593 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
598 isl_basic_map_free(bmap);
602 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
603 struct vars *v, struct isl_basic_map *bmap)
605 struct isl_token *tok;
607 while ((tok = isl_stream_next_token(s)) != NULL) {
611 unsigned total = isl_basic_map_total_dim(bmap);
613 if (tok->type != ISL_TOKEN_IDENT)
616 p = vars_pos(v, tok->u.s, -1);
620 isl_stream_error(s, tok, "expecting unique identifier");
624 bmap = isl_basic_map_cow(bmap);
625 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
628 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
630 isl_seq_clr(bmap->div[k], 1 + 1 + total);
633 tok = isl_stream_next_token(s);
634 if (tok && tok->type == '=') {
636 bmap = add_div_definition(s, v, bmap, k);
637 tok = isl_stream_next_token(s);
640 if (!tok || tok->type != ',')
646 isl_stream_push_token(s, tok);
651 isl_basic_map_free(bmap);
655 static int next_is_tuple(struct isl_stream *s)
657 struct isl_token *tok;
660 tok = isl_stream_next_token(s);
663 if (tok->type == '[') {
664 isl_stream_push_token(s, tok);
667 if (tok->type != ISL_TOKEN_IDENT) {
668 isl_stream_push_token(s, tok);
672 is_tuple = isl_stream_next_token_is(s, '[');
674 isl_stream_push_token(s, tok);
679 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
680 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v);
682 static __isl_give isl_basic_map *read_nested_tuple(struct isl_stream *s,
683 __isl_take isl_basic_map *bmap, struct vars *v)
685 bmap = read_tuple(s, bmap, isl_dim_in, v);
686 if (isl_stream_eat(s, ISL_TOKEN_TO))
688 bmap = read_tuple(s, bmap, isl_dim_out, v);
689 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
692 isl_basic_map_free(bmap);
696 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
697 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
699 struct isl_token *tok;
702 tok = isl_stream_next_token(s);
703 if (tok && tok->type == ISL_TOKEN_IDENT) {
704 name = strdup(tok->u.s);
708 tok = isl_stream_next_token(s);
710 if (!tok || tok->type != '[') {
711 isl_stream_error(s, tok, "expecting '['");
715 if (type != isl_dim_param && next_is_tuple(s)) {
716 isl_dim *dim = isl_basic_map_get_dim(bmap);
717 int nparam = isl_dim_size(dim, isl_dim_param);
718 int n_in = isl_dim_size(dim, isl_dim_in);
719 isl_basic_map *nested;
720 if (type == isl_dim_out)
721 dim = isl_dim_move(dim, isl_dim_param, nparam,
722 isl_dim_in, 0, n_in);
723 nested = isl_basic_map_alloc_dim(dim, 0, 0, 0);
724 nested = read_nested_tuple(s, nested, v);
725 if (type == isl_dim_in) {
726 nested = isl_basic_map_reverse(nested);
727 bmap = isl_basic_map_intersect(nested, bmap);
730 dim = isl_dim_range(isl_basic_map_get_dim(nested));
731 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
732 dim = isl_dim_join(isl_basic_map_get_dim(bmap), dim);
733 bset = isl_basic_map_domain(bmap);
734 nested = isl_basic_map_reset_dim(nested, dim);
735 bmap = isl_basic_map_intersect_domain(nested, bset);
738 bmap = read_var_list(s, bmap, type, v);
739 tok = isl_stream_next_token(s);
740 if (!tok || tok->type != ']') {
741 isl_stream_error(s, tok, "expecting ']'");
747 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
755 isl_basic_map_free(bmap);
759 static struct isl_basic_map *add_constraints(struct isl_stream *s,
760 struct vars *v, struct isl_basic_map *bmap);
762 static struct isl_basic_map *add_exists(struct isl_stream *s,
763 struct vars *v, struct isl_basic_map *bmap)
765 struct isl_token *tok;
772 tok = isl_stream_next_token(s);
775 if (tok->type == '(') {
779 isl_stream_push_token(s, tok);
781 bmap = read_defined_var_list(s, v, bmap);
785 if (isl_stream_eat(s, ':'))
787 bmap = add_constraints(s, v, bmap);
788 if (seen_paren && isl_stream_eat(s, ')'))
792 isl_basic_map_free(bmap);
796 static __isl_give isl_basic_map *construct_constraint(
797 __isl_take isl_basic_map *bmap, enum isl_token_type type,
798 isl_int *left, isl_int *right)
806 len = 1 + isl_basic_map_total_dim(bmap);
809 k = isl_basic_map_alloc_inequality(bmap);
812 if (type == ISL_TOKEN_LE)
813 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
816 else if (type == ISL_TOKEN_GE)
817 isl_seq_combine(bmap->ineq[k], ctx->one, left,
820 else if (type == ISL_TOKEN_LT) {
821 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
824 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
825 } else if (type == ISL_TOKEN_GT) {
826 isl_seq_combine(bmap->ineq[k], ctx->one, left,
829 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
831 isl_seq_combine(bmap->ineq[k], ctx->one, left,
834 isl_basic_map_inequality_to_equality(bmap, k);
839 isl_basic_map_free(bmap);
843 static int is_comparator(struct isl_token *tok)
860 static struct isl_basic_map *add_constraint(struct isl_stream *s,
861 struct vars *v, struct isl_basic_map *bmap)
864 struct isl_token *tok = NULL;
865 struct isl_mat *aff1 = NULL, *aff2 = NULL;
867 tok = isl_stream_next_token(s);
870 if (tok->type == ISL_TOKEN_EXISTS) {
872 return add_exists(s, v, bmap);
874 isl_stream_push_token(s, tok);
877 bmap = isl_basic_map_cow(bmap);
879 aff1 = accept_affine_list(s, v);
882 tok = isl_stream_next_token(s);
883 if (!is_comparator(tok)) {
884 isl_stream_error(s, tok, "missing operator");
886 isl_stream_push_token(s, tok);
891 aff2 = accept_affine_list(s, v);
895 aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
898 bmap = add_divs(bmap, v);
899 bmap = isl_basic_map_extend_constraints(bmap, 0,
900 aff1->n_row * aff2->n_row);
901 for (i = 0; i < aff1->n_row; ++i)
902 for (j = 0; j < aff2->n_row; ++j)
903 bmap = construct_constraint(bmap, tok->type,
904 aff1->row[i], aff2->row[j]);
909 tok = isl_stream_next_token(s);
910 if (!is_comparator(tok)) {
912 isl_stream_push_token(s, tok);
924 isl_basic_map_free(bmap);
928 static struct isl_basic_map *add_constraints(struct isl_stream *s,
929 struct vars *v, struct isl_basic_map *bmap)
931 struct isl_token *tok;
934 bmap = add_constraint(s, v, bmap);
937 tok = isl_stream_next_token(s);
939 isl_stream_error(s, NULL, "unexpected EOF");
942 if (tok->type != ISL_TOKEN_AND)
946 isl_stream_push_token(s, tok);
952 isl_basic_map_free(bmap);
956 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
957 struct vars *v, __isl_take isl_dim *dim)
960 struct isl_token *tok;
961 struct isl_basic_map *bmap;
963 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
967 tok = isl_stream_next_token(s);
970 if (tok->type == '(') {
974 isl_stream_push_token(s, tok);
976 bmap = add_constraints(s, v, bmap);
977 bmap = isl_basic_map_simplify(bmap);
978 bmap = isl_basic_map_finalize(bmap);
980 if (seen_paren && isl_stream_eat(s, ')'))
985 isl_basic_map_free(bmap);
989 static struct isl_map *read_disjuncts(struct isl_stream *s,
990 struct vars *v, __isl_take isl_dim *dim)
992 struct isl_token *tok;
995 tok = isl_stream_next_token(s);
997 isl_stream_error(s, NULL, "unexpected EOF");
1000 if (tok->type == '}') {
1001 isl_stream_push_token(s, tok);
1002 return isl_map_universe(dim);
1004 isl_stream_push_token(s, tok);
1006 map = isl_map_empty(isl_dim_copy(dim));
1008 struct isl_basic_map *bmap;
1011 bmap = read_disjunct(s, v, isl_dim_copy(dim));
1012 map = isl_map_union(map, isl_map_from_basic_map(bmap));
1014 vars_drop(v, v->n - n);
1016 tok = isl_stream_next_token(s);
1017 if (!tok || tok->type != ISL_TOKEN_OR)
1019 isl_token_free(tok);
1022 isl_stream_push_token(s, tok);
1031 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1033 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1034 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1035 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1036 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1038 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1039 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1040 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1042 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1043 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1044 isl_basic_map_dim(bmap, isl_dim_in) +
1045 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1046 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1048 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1054 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1055 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1058 struct isl_token *tok;
1068 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1069 dim = isl_basic_map_dim(bmap, isl_dim_out);
1071 tok = isl_stream_next_token(s);
1072 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1073 isl_stream_error(s, tok, "expecting coefficient");
1075 isl_stream_push_token(s, tok);
1078 if (!tok->on_new_line) {
1079 isl_stream_error(s, tok, "coefficient should appear on new line");
1080 isl_stream_push_token(s, tok);
1084 type = isl_int_get_si(tok->u.v);
1085 isl_token_free(tok);
1087 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1089 k = isl_basic_map_alloc_equality(bmap);
1092 k = isl_basic_map_alloc_inequality(bmap);
1098 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1100 tok = isl_stream_next_token(s);
1101 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1102 isl_stream_error(s, tok, "expecting coefficient");
1104 isl_stream_push_token(s, tok);
1107 if (tok->on_new_line) {
1108 isl_stream_error(s, tok,
1109 "coefficient should not appear on new line");
1110 isl_stream_push_token(s, tok);
1113 pos = polylib_pos_to_isl_pos(bmap, j);
1114 isl_int_set(c[pos], tok->u.v);
1115 isl_token_free(tok);
1120 isl_basic_map_free(bmap);
1124 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1128 struct isl_token *tok;
1129 struct isl_token *tok2;
1132 unsigned in = 0, out, local = 0;
1133 struct isl_basic_map *bmap = NULL;
1138 tok = isl_stream_next_token(s);
1140 isl_stream_error(s, NULL, "unexpected EOF");
1143 tok2 = isl_stream_next_token(s);
1145 isl_token_free(tok);
1146 isl_stream_error(s, NULL, "unexpected EOF");
1149 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1150 isl_stream_push_token(s, tok2);
1151 isl_stream_push_token(s, tok);
1152 isl_stream_error(s, NULL,
1153 "expecting constraint matrix dimensions");
1156 n_row = isl_int_get_si(tok->u.v);
1157 n_col = isl_int_get_si(tok2->u.v);
1158 on_new_line = tok2->on_new_line;
1159 isl_token_free(tok2);
1160 isl_token_free(tok);
1161 isl_assert(s->ctx, !on_new_line, return NULL);
1162 isl_assert(s->ctx, n_row >= 0, return NULL);
1163 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1164 tok = isl_stream_next_token_on_same_line(s);
1166 if (tok->type != ISL_TOKEN_VALUE) {
1167 isl_stream_error(s, tok,
1168 "expecting number of output dimensions");
1169 isl_stream_push_token(s, tok);
1172 out = isl_int_get_si(tok->u.v);
1173 isl_token_free(tok);
1175 tok = isl_stream_next_token_on_same_line(s);
1176 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1177 isl_stream_error(s, tok,
1178 "expecting number of input dimensions");
1180 isl_stream_push_token(s, tok);
1183 in = isl_int_get_si(tok->u.v);
1184 isl_token_free(tok);
1186 tok = isl_stream_next_token_on_same_line(s);
1187 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1188 isl_stream_error(s, tok,
1189 "expecting number of existentials");
1191 isl_stream_push_token(s, tok);
1194 local = isl_int_get_si(tok->u.v);
1195 isl_token_free(tok);
1197 tok = isl_stream_next_token_on_same_line(s);
1198 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1199 isl_stream_error(s, tok,
1200 "expecting number of parameters");
1202 isl_stream_push_token(s, tok);
1205 nparam = isl_int_get_si(tok->u.v);
1206 isl_token_free(tok);
1207 if (n_col != 1 + out + in + local + nparam + 1) {
1208 isl_stream_error(s, NULL,
1209 "dimensions don't match");
1213 out = n_col - 2 - nparam;
1214 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1218 for (i = 0; i < local; ++i) {
1219 int k = isl_basic_map_alloc_div(bmap);
1222 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1225 for (i = 0; i < n_row; ++i)
1226 bmap = basic_map_read_polylib_constraint(s, bmap);
1228 tok = isl_stream_next_token_on_same_line(s);
1230 isl_stream_error(s, tok, "unexpected extra token on line");
1231 isl_stream_push_token(s, tok);
1235 bmap = isl_basic_map_simplify(bmap);
1236 bmap = isl_basic_map_finalize(bmap);
1239 isl_basic_map_free(bmap);
1243 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1245 struct isl_token *tok;
1246 struct isl_token *tok2;
1248 struct isl_map *map;
1250 tok = isl_stream_next_token(s);
1252 isl_stream_error(s, NULL, "unexpected EOF");
1255 tok2 = isl_stream_next_token_on_same_line(s);
1256 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1257 isl_stream_push_token(s, tok2);
1258 isl_stream_push_token(s, tok);
1259 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1262 isl_stream_error(s, tok2, "unexpected token");
1263 isl_stream_push_token(s, tok2);
1264 isl_stream_push_token(s, tok);
1267 n = isl_int_get_si(tok->u.v);
1268 isl_token_free(tok);
1270 isl_assert(s->ctx, n >= 1, return NULL);
1272 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1274 for (i = 1; map && i < n; ++i)
1275 map = isl_map_union(map,
1276 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1281 static int optional_power(struct isl_stream *s)
1284 struct isl_token *tok;
1286 tok = isl_stream_next_token(s);
1289 if (tok->type != '^') {
1290 isl_stream_push_token(s, tok);
1293 isl_token_free(tok);
1294 tok = isl_stream_next_token(s);
1295 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1296 isl_stream_error(s, tok, "expecting exponent");
1298 isl_stream_push_token(s, tok);
1301 pow = isl_int_get_si(tok->u.v);
1302 isl_token_free(tok);
1306 static __isl_give isl_div *read_div(struct isl_stream *s,
1307 __isl_take isl_dim *dim, struct vars *v)
1310 isl_basic_map *bmap;
1313 bmap = isl_basic_map_universe(dim);
1315 if (vars_add_anon(v) < 0)
1317 if (read_div_definition(s, v) < 0)
1319 bmap = add_divs(bmap, v);
1320 bmap = isl_basic_map_order_divs(bmap);
1323 vars_drop(v, v->n - n);
1325 return isl_basic_map_div(bmap, bmap->n_div - 1);
1327 isl_basic_map_free(bmap);
1331 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1332 __isl_keep isl_basic_map *bmap, struct vars *v);
1334 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1335 __isl_keep isl_basic_map *bmap, struct vars *v)
1337 struct isl_qpolynomial *qp;
1338 struct isl_token *tok;
1340 tok = isl_stream_next_token(s);
1342 isl_stream_error(s, NULL, "unexpected EOF");
1345 if (tok->type == '(') {
1348 isl_token_free(tok);
1349 qp = read_term(s, bmap, v);
1352 if (isl_stream_eat(s, ')'))
1354 pow = optional_power(s);
1355 qp = isl_qpolynomial_pow(qp, pow);
1356 } else if (tok->type == ISL_TOKEN_VALUE) {
1357 struct isl_token *tok2;
1358 tok2 = isl_stream_next_token(s);
1359 if (tok2 && tok2->type == '/') {
1360 isl_token_free(tok2);
1361 tok2 = isl_stream_next_token(s);
1362 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1363 isl_stream_error(s, tok2, "expected denominator");
1364 isl_token_free(tok);
1365 isl_token_free(tok2);
1368 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1369 tok->u.v, tok2->u.v);
1370 isl_token_free(tok2);
1372 isl_stream_push_token(s, tok2);
1373 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1376 isl_token_free(tok);
1377 } else if (tok->type == ISL_TOKEN_INFTY) {
1378 isl_token_free(tok);
1379 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1380 } else if (tok->type == ISL_TOKEN_NAN) {
1381 isl_token_free(tok);
1382 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1383 } else if (tok->type == ISL_TOKEN_IDENT) {
1385 int pos = vars_pos(v, tok->u.s, -1);
1388 isl_token_free(tok);
1392 isl_stream_error(s, tok, "unknown identifier");
1393 isl_token_free(tok);
1396 isl_token_free(tok);
1397 pow = optional_power(s);
1398 qp = isl_qpolynomial_var_pow(isl_basic_map_get_dim(bmap), pos, pow);
1399 } else if (tok->type == '[') {
1403 isl_stream_push_token(s, tok);
1404 div = read_div(s, isl_basic_map_get_dim(bmap), v);
1405 pow = optional_power(s);
1406 qp = isl_qpolynomial_div_pow(div, pow);
1407 } else if (tok->type == '-') {
1408 struct isl_qpolynomial *qp2;
1410 isl_token_free(tok);
1411 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1413 qp2 = read_factor(s, bmap, v);
1414 qp = isl_qpolynomial_mul(qp, qp2);
1416 isl_stream_error(s, tok, "unexpected isl_token");
1417 isl_stream_push_token(s, tok);
1421 if (isl_stream_eat_if_available(s, '*') ||
1422 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1423 struct isl_qpolynomial *qp2;
1425 qp2 = read_factor(s, bmap, v);
1426 qp = isl_qpolynomial_mul(qp, qp2);
1431 isl_qpolynomial_free(qp);
1435 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1436 __isl_keep isl_basic_map *bmap, struct vars *v)
1438 struct isl_token *tok;
1439 struct isl_qpolynomial *qp;
1441 qp = read_factor(s, bmap, v);
1444 tok = isl_stream_next_token(s);
1448 if (tok->type == '+') {
1449 struct isl_qpolynomial *qp2;
1451 isl_token_free(tok);
1452 qp2 = read_factor(s, bmap, v);
1453 qp = isl_qpolynomial_add(qp, qp2);
1454 } else if (tok->type == '-') {
1455 struct isl_qpolynomial *qp2;
1457 isl_token_free(tok);
1458 qp2 = read_factor(s, bmap, v);
1459 qp = isl_qpolynomial_sub(qp, qp2);
1460 } else if (tok->type == ISL_TOKEN_VALUE &&
1461 isl_int_is_neg(tok->u.v)) {
1462 struct isl_qpolynomial *qp2;
1464 isl_stream_push_token(s, tok);
1465 qp2 = read_factor(s, bmap, v);
1466 qp = isl_qpolynomial_add(qp, qp2);
1468 isl_stream_push_token(s, tok);
1476 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1477 __isl_take isl_basic_map *bmap, struct vars *v)
1479 struct isl_token *tok;
1480 struct isl_map *map;
1482 tok = isl_stream_next_token(s);
1484 isl_stream_error(s, NULL, "unexpected EOF");
1487 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1488 if (tok->type == ':') {
1489 isl_token_free(tok);
1490 map = isl_map_intersect(map,
1491 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1493 isl_stream_push_token(s, tok);
1495 isl_basic_map_free(bmap);
1499 isl_basic_map_free(bmap);
1503 static struct isl_obj obj_read_poly(struct isl_stream *s,
1504 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1506 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1507 struct isl_pw_qpolynomial *pwqp;
1508 struct isl_qpolynomial *qp;
1509 struct isl_map *map;
1510 struct isl_set *set;
1512 qp = read_term(s, bmap, v);
1513 map = read_optional_disjuncts(s, bmap, v);
1514 set = isl_map_range(map);
1516 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1518 vars_drop(v, v->n - n);
1524 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1525 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1527 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1528 struct isl_obj obj_p;
1529 isl_qpolynomial *qp;
1530 isl_qpolynomial_fold *fold = NULL;
1531 isl_pw_qpolynomial_fold *pwf;
1535 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1536 return obj_read_poly(s, bmap, v, n);
1538 if (isl_stream_eat(s, '('))
1541 qp = read_term(s, bmap, v);
1542 fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1544 while (isl_stream_eat_if_available(s, ',')) {
1545 isl_qpolynomial_fold *fold_i;
1546 qp = read_term(s, bmap, v);
1547 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1548 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1551 if (isl_stream_eat(s, ')'))
1554 map = read_optional_disjuncts(s, bmap, v);
1555 set = isl_map_range(map);
1556 pwf = isl_pw_qpolynomial_fold_alloc(isl_fold_max, set, fold);
1558 vars_drop(v, v->n - n);
1563 isl_basic_map_free(bmap);
1564 isl_qpolynomial_fold_free(fold);
1565 obj.type = isl_obj_none;
1569 static struct isl_obj obj_read_body(struct isl_stream *s,
1570 __isl_take isl_basic_map *bmap, struct vars *v)
1572 struct isl_map *map = NULL;
1573 struct isl_token *tok;
1574 struct isl_obj obj = { isl_obj_set, NULL };
1577 if (!next_is_tuple(s))
1578 return obj_read_poly_or_fold(s, bmap, v, n);
1580 bmap = read_tuple(s, bmap, isl_dim_in, v);
1583 tok = isl_stream_next_token(s);
1584 if (tok && tok->type == ISL_TOKEN_TO) {
1585 obj.type = isl_obj_map;
1586 isl_token_free(tok);
1587 if (!next_is_tuple(s)) {
1588 bmap = isl_basic_map_reverse(bmap);
1589 return obj_read_poly_or_fold(s, bmap, v, n);
1591 bmap = read_tuple(s, bmap, isl_dim_out, v);
1595 bmap = isl_basic_map_reverse(bmap);
1597 isl_stream_push_token(s, tok);
1600 map = read_optional_disjuncts(s, bmap, v);
1602 vars_drop(v, v->n - n);
1607 isl_basic_map_free(bmap);
1608 obj.type = isl_obj_none;
1612 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1614 if (obj.type == isl_obj_map) {
1615 obj.v = isl_union_map_from_map(obj.v);
1616 obj.type = isl_obj_union_map;
1617 } else if (obj.type == isl_obj_set) {
1618 obj.v = isl_union_set_from_set(obj.v);
1619 obj.type = isl_obj_union_set;
1620 } else if (obj.type == isl_obj_pw_qpolynomial) {
1621 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1622 obj.type = isl_obj_union_pw_qpolynomial;
1623 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1624 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1625 obj.type = isl_obj_union_pw_qpolynomial_fold;
1627 isl_assert(ctx, 0, goto error);
1630 obj.type->free(obj.v);
1631 obj.type = isl_obj_none;
1635 static struct isl_obj obj_add(struct isl_ctx *ctx,
1636 struct isl_obj obj1, struct isl_obj obj2)
1638 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1639 obj1 = to_union(ctx, obj1);
1640 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1641 obj2 = to_union(ctx, obj2);
1642 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1643 obj1 = to_union(ctx, obj1);
1644 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1645 obj2 = to_union(ctx, obj2);
1646 if (obj1.type == isl_obj_pw_qpolynomial &&
1647 obj2.type == isl_obj_union_pw_qpolynomial)
1648 obj1 = to_union(ctx, obj1);
1649 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1650 obj2.type == isl_obj_pw_qpolynomial)
1651 obj2 = to_union(ctx, obj2);
1652 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1653 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1654 obj1 = to_union(ctx, obj1);
1655 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1656 obj2.type == isl_obj_pw_qpolynomial_fold)
1657 obj2 = to_union(ctx, obj2);
1658 isl_assert(ctx, obj1.type == obj2.type, goto error);
1659 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1660 obj1 = to_union(ctx, obj1);
1661 obj2 = to_union(ctx, obj2);
1663 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1664 obj1 = to_union(ctx, obj1);
1665 obj2 = to_union(ctx, obj2);
1667 if (obj1.type == isl_obj_pw_qpolynomial &&
1668 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1669 obj1 = to_union(ctx, obj1);
1670 obj2 = to_union(ctx, obj2);
1672 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1673 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1674 obj1 = to_union(ctx, obj1);
1675 obj2 = to_union(ctx, obj2);
1677 obj1.v = obj1.type->add(obj1.v, obj2.v);
1680 obj1.type->free(obj1.v);
1681 obj2.type->free(obj2.v);
1682 obj1.type = isl_obj_none;
1687 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1689 isl_basic_map *bmap = NULL;
1690 struct isl_token *tok;
1691 struct vars *v = NULL;
1692 struct isl_obj obj = { isl_obj_set, NULL };
1694 tok = isl_stream_next_token(s);
1696 isl_stream_error(s, NULL, "unexpected EOF");
1699 if (tok->type == ISL_TOKEN_VALUE) {
1700 struct isl_map *map;
1701 isl_stream_push_token(s, tok);
1702 map = map_read_polylib(s, nparam);
1705 if (isl_map_dim(map, isl_dim_in) > 0)
1706 obj.type = isl_obj_map;
1710 v = vars_new(s->ctx);
1712 isl_stream_push_token(s, tok);
1715 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1716 if (tok->type == '[') {
1717 isl_stream_push_token(s, tok);
1718 bmap = read_tuple(s, bmap, isl_dim_param, v);
1722 isl_assert(s->ctx, nparam == v->n, goto error);
1723 tok = isl_stream_next_token(s);
1724 if (!tok || tok->type != ISL_TOKEN_TO) {
1725 isl_stream_error(s, tok, "expecting '->'");
1727 isl_stream_push_token(s, tok);
1730 isl_token_free(tok);
1731 tok = isl_stream_next_token(s);
1732 } else if (nparam > 0)
1733 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1734 if (!tok || tok->type != '{') {
1735 isl_stream_error(s, tok, "expecting '{'");
1737 isl_stream_push_token(s, tok);
1740 isl_token_free(tok);
1742 tok = isl_stream_next_token(s);
1745 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1746 isl_token_free(tok);
1747 if (isl_stream_eat(s, '='))
1749 bmap = read_tuple(s, bmap, isl_dim_param, v);
1753 isl_assert(s->ctx, nparam == v->n, goto error);
1754 } else if (tok->type == '}') {
1755 obj.type = isl_obj_union_set;
1756 obj.v = isl_union_set_empty(isl_basic_map_get_dim(bmap));
1757 isl_token_free(tok);
1760 isl_stream_push_token(s, tok);
1765 o = obj_read_body(s, isl_basic_map_copy(bmap), v);
1766 if (o.type == isl_obj_none || !o.v)
1771 obj = obj_add(s->ctx, obj, o);
1772 if (obj.type == isl_obj_none || !obj.v)
1775 tok = isl_stream_next_token(s);
1776 if (!tok || tok->type != ';')
1778 isl_token_free(tok);
1781 if (tok && tok->type == '}') {
1782 isl_token_free(tok);
1784 isl_stream_error(s, tok, "unexpected isl_token");
1786 isl_token_free(tok);
1791 isl_basic_map_free(bmap);
1795 isl_basic_map_free(bmap);
1796 obj.type->free(obj.v);
1803 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1805 return obj_read(s, -1);
1808 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1811 struct isl_map *map;
1813 obj = obj_read(s, nparam);
1815 isl_assert(s->ctx, obj.type == isl_obj_map ||
1816 obj.type == isl_obj_set, goto error);
1820 obj.type->free(obj.v);
1824 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1827 struct isl_set *set;
1829 obj = obj_read(s, nparam);
1831 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1835 obj.type->free(obj.v);
1839 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1842 isl_union_map *umap;
1844 obj = obj_read(s, -1);
1845 if (obj.type == isl_obj_map) {
1846 obj.type = isl_obj_union_map;
1847 obj.v = isl_union_map_from_map(obj.v);
1849 if (obj.type == isl_obj_set) {
1850 obj.type = isl_obj_union_set;
1851 obj.v = isl_union_set_from_set(obj.v);
1854 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1855 obj.type == isl_obj_union_set, goto error);
1859 obj.type->free(obj.v);
1863 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
1866 isl_union_set *uset;
1868 obj = obj_read(s, -1);
1869 if (obj.type == isl_obj_set) {
1870 obj.type = isl_obj_union_set;
1871 obj.v = isl_union_set_from_set(obj.v);
1874 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1878 obj.type->free(obj.v);
1882 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1885 struct isl_map *map;
1886 struct isl_basic_map *bmap;
1888 obj = obj_read(s, nparam);
1893 isl_assert(map->ctx, map->n <= 1, goto error);
1896 bmap = isl_basic_map_empty_like_map(map);
1898 bmap = isl_basic_map_copy(map->p[0]);
1908 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1909 FILE *input, int nparam)
1911 struct isl_basic_map *bmap;
1912 struct isl_stream *s = isl_stream_new_file(ctx, input);
1915 bmap = basic_map_read(s, nparam);
1920 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1921 FILE *input, int nparam)
1923 struct isl_basic_map *bmap;
1924 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1927 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1928 return (struct isl_basic_set *)bmap;
1930 isl_basic_map_free(bmap);
1934 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1935 const char *str, int nparam)
1937 struct isl_basic_map *bmap;
1938 struct isl_stream *s = isl_stream_new_str(ctx, str);
1941 bmap = basic_map_read(s, nparam);
1946 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1947 const char *str, int nparam)
1949 struct isl_basic_map *bmap;
1950 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1953 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1954 return (struct isl_basic_set *)bmap;
1956 isl_basic_map_free(bmap);
1960 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1961 FILE *input, int nparam)
1963 struct isl_map *map;
1964 struct isl_stream *s = isl_stream_new_file(ctx, input);
1967 map = isl_stream_read_map(s, nparam);
1972 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1973 const char *str, int nparam)
1975 struct isl_map *map;
1976 struct isl_stream *s = isl_stream_new_str(ctx, str);
1979 map = isl_stream_read_map(s, nparam);
1984 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1985 FILE *input, int nparam)
1987 struct isl_map *map;
1988 map = isl_map_read_from_file(ctx, input, nparam);
1991 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1992 return (struct isl_set *)map;
1998 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1999 const char *str, int nparam)
2001 struct isl_map *map;
2002 map = isl_map_read_from_str(ctx, str, nparam);
2005 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2006 return (struct isl_set *)map;
2012 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2015 isl_union_map *umap;
2016 struct isl_stream *s = isl_stream_new_str(ctx, str);
2019 umap = isl_stream_read_union_map(s);
2024 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2027 isl_union_set *uset;
2028 struct isl_stream *s = isl_stream_new_str(ctx, str);
2031 uset = isl_stream_read_union_set(s);
2036 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2038 struct isl_vec *vec = NULL;
2039 struct isl_token *tok;
2043 tok = isl_stream_next_token(s);
2044 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2045 isl_stream_error(s, tok, "expecting vector length");
2049 size = isl_int_get_si(tok->u.v);
2050 isl_token_free(tok);
2052 vec = isl_vec_alloc(s->ctx, size);
2054 for (j = 0; j < size; ++j) {
2055 tok = isl_stream_next_token(s);
2056 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2057 isl_stream_error(s, tok, "expecting constant value");
2060 isl_int_set(vec->el[j], tok->u.v);
2061 isl_token_free(tok);
2066 isl_token_free(tok);
2071 static __isl_give isl_vec *vec_read(struct isl_stream *s,
2072 unsigned input_format)
2074 if (input_format == ISL_FORMAT_POLYLIB)
2075 return isl_vec_read_polylib(s);
2076 isl_assert(s->ctx, 0, return NULL);
2079 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
2080 FILE *input, unsigned input_format)
2083 struct isl_stream *s = isl_stream_new_file(ctx, input);
2086 v = vec_read(s, input_format);
2091 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2092 struct isl_stream *s)
2095 struct isl_pw_qpolynomial *pwqp;
2097 obj = obj_read(s, -1);
2099 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2104 obj.type->free(obj.v);
2108 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2111 isl_pw_qpolynomial *pwqp;
2112 struct isl_stream *s = isl_stream_new_str(ctx, str);
2115 pwqp = isl_stream_read_pw_qpolynomial(s);