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
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
22 #include <isl/stream.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
32 struct variable *next;
41 static struct vars *vars_new(struct isl_ctx *ctx)
44 v = isl_alloc_type(ctx, struct vars);
53 static void variable_free(struct variable *var)
56 struct variable *next = var->next;
57 isl_vec_free(var->def);
64 static void vars_free(struct vars *v)
72 static void vars_drop(struct vars *v, int n)
83 struct variable *next = var->next;
84 isl_vec_free(var->def);
92 static struct variable *variable_new(struct vars *v, const char *name, int len,
96 var = isl_alloc_type(v->ctx, struct variable);
99 var->name = strdup(name);
100 var->name[len] = '\0';
110 static int vars_pos(struct vars *v, const char *s, int len)
117 for (q = v->v; q; q = q->next) {
118 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
125 v->v = variable_new(v, s, len, v->n);
133 static int vars_add_anon(struct vars *v)
135 v->v = variable_new(v, "", 0, v->n);
144 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
145 enum isl_dim_type type, unsigned pos, char *name)
154 prime = strchr(name, '\'');
157 bmap = isl_basic_map_set_dim_name(bmap, type, pos, name);
164 /* Obtain next token, with some preprocessing.
165 * In particular, evaluate expressions of the form x^y,
166 * with x and y values.
168 static struct isl_token *next_token(struct isl_stream *s)
170 struct isl_token *tok, *tok2;
172 tok = isl_stream_next_token(s);
173 if (!tok || tok->type != ISL_TOKEN_VALUE)
175 if (!isl_stream_eat_if_available(s, '^'))
177 tok2 = isl_stream_next_token(s);
178 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
179 isl_stream_error(s, tok2, "expecting constant value");
183 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
185 isl_token_free(tok2);
189 isl_token_free(tok2);
193 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
195 struct isl_token *tok;
198 if (!tok || tok->type != ISL_TOKEN_VALUE) {
199 isl_stream_error(s, tok, "expecting constant value");
203 isl_int_mul(*f, *f, tok->u.v);
207 if (isl_stream_eat_if_available(s, '*'))
208 return accept_cst_factor(s, f);
216 /* Given an affine expression aff, return an affine expression
217 * for aff % d, with d the next token on the stream, which is
218 * assumed to be a constant.
220 * We introduce an integer division q = [aff/d] and the result
221 * is set to aff - d q.
223 static __isl_give isl_vec *affine_mod(struct isl_stream *s,
224 struct vars *v, __isl_take isl_vec *aff)
226 struct isl_token *tok;
227 struct variable *var;
231 if (!tok || tok->type != ISL_TOKEN_VALUE) {
232 isl_stream_error(s, tok, "expecting constant value");
236 if (vars_add_anon(v) < 0)
241 var->def = isl_vec_alloc(s->ctx, 2 + v->n);
244 isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
245 isl_int_set_si(var->def->el[1 + aff->size], 0);
246 isl_int_set(var->def->el[0], tok->u.v);
248 mod = isl_vec_alloc(v->ctx, 1 + v->n);
252 isl_seq_cpy(mod->el, aff->el, aff->size);
253 isl_int_neg(mod->el[aff->size], tok->u.v);
264 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v);
265 static int read_div_definition(struct isl_stream *s, struct vars *v);
267 static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
270 struct isl_token *tok = NULL;
275 isl_stream_error(s, NULL, "unexpected EOF");
278 if (tok->type == ISL_TOKEN_IDENT) {
280 int pos = vars_pos(v, tok->u.s, -1);
284 isl_stream_error(s, tok, "unknown identifier");
288 aff = isl_vec_alloc(v->ctx, 1 + v->n);
291 isl_seq_clr(aff->el, aff->size);
292 isl_int_set_si(aff->el[1 + pos], 1);
294 } else if (tok->type == ISL_TOKEN_VALUE) {
295 if (isl_stream_eat_if_available(s, '*')) {
296 aff = accept_affine_factor(s, v);
297 aff = isl_vec_scale(aff, tok->u.v);
299 aff = isl_vec_alloc(v->ctx, 1 + v->n);
302 isl_seq_clr(aff->el, aff->size);
303 isl_int_set(aff->el[0], tok->u.v);
306 } else if (tok->type == '(') {
309 aff = accept_affine(s, v);
312 if (isl_stream_eat(s, ')'))
314 } else if (tok->type == '[') {
315 if (vars_add_anon(v) < 0)
317 aff = isl_vec_alloc(v->ctx, 1 + v->n);
320 isl_seq_clr(aff->el, aff->size);
321 isl_int_set_si(aff->el[1 + v->n - 1], 1);
322 isl_stream_push_token(s, tok);
324 if (read_div_definition(s, v) < 0)
327 isl_stream_error(s, tok, "expecting factor");
330 if (isl_stream_eat_if_available(s, '%'))
331 return affine_mod(s, v, aff);
332 if (isl_stream_eat_if_available(s, '*')) {
335 isl_int_set_si(f, 1);
336 if (accept_cst_factor(s, &f) < 0) {
340 aff = isl_vec_scale(aff, f);
352 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
354 struct isl_token *tok = NULL;
358 aff = isl_vec_alloc(v->ctx, 1 + v->n);
361 isl_seq_clr(aff->el, aff->size);
366 isl_stream_error(s, NULL, "unexpected EOF");
369 if (tok->type == '-') {
374 if (tok->type == '(' || tok->type == '[' ||
375 tok->type == ISL_TOKEN_IDENT) {
377 isl_stream_push_token(s, tok);
379 aff2 = accept_affine_factor(s, v);
381 aff2 = isl_vec_scale(aff2, s->ctx->negone);
382 aff = isl_vec_zero_extend(aff, 1 + v->n);
383 aff = isl_vec_add(aff, aff2);
387 } else if (tok->type == ISL_TOKEN_VALUE) {
389 isl_int_neg(tok->u.v, tok->u.v);
390 if (isl_stream_eat_if_available(s, '*') ||
391 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
393 aff2 = accept_affine_factor(s, v);
394 aff2 = isl_vec_scale(aff2, tok->u.v);
395 aff = isl_vec_zero_extend(aff, 1 + v->n);
396 aff = isl_vec_add(aff, aff2);
400 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
404 isl_stream_error(s, tok, "unexpected isl_token");
405 isl_stream_push_token(s, tok);
412 if (tok && tok->type == '-') {
415 } else if (tok && tok->type == '+') {
418 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
419 isl_int_is_neg(tok->u.v)) {
420 isl_stream_push_token(s, tok);
423 isl_stream_push_token(s, tok);
435 /* Add any variables in the variable list "v" that are not already in "bmap"
436 * as existentially quantified variables in "bmap".
438 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
443 struct variable *var;
445 extra = v->n - isl_basic_map_total_dim(bmap);
450 bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
451 extra, 0, 2 * extra);
453 for (i = 0; i < extra; ++i)
454 if (isl_basic_map_alloc_div(bmap) < 0)
457 for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
458 int k = bmap->n_div - 1 - i;
460 isl_seq_cpy(bmap->div[k], var->def->el, var->def->size);
461 isl_seq_clr(bmap->div[k] + var->def->size,
462 2 + v->n - var->def->size);
464 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
470 isl_basic_map_free(bmap);
474 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
475 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
478 isl_basic_map *def = NULL;
483 if (vars_add_anon(v) < 0)
487 vec = accept_affine(s, v);
491 dim = isl_basic_map_get_dim(bmap);
492 def = isl_basic_map_universe(dim);
493 def = add_divs(def, v);
494 def = isl_basic_map_extend_constraints(def, 1, 0);
495 k = isl_basic_map_alloc_equality(def);
497 isl_seq_cpy(def->eq[k], vec->el, vec->size);
498 isl_int_set_si(def->eq[k][1 + n - 1], -1);
504 vars_drop(v, v->n - n);
506 def = isl_basic_map_simplify(def);
507 def = isl_basic_map_finalize(def);
508 bmap = isl_basic_map_intersect(bmap, def);
511 isl_basic_map_free(bmap);
512 isl_basic_map_free(def);
516 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
517 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
520 struct isl_token *tok;
522 while ((tok = next_token(s)) != NULL) {
525 if (tok->type == ISL_TOKEN_IDENT) {
527 int p = vars_pos(v, tok->u.s, -1);
534 bmap = isl_basic_map_add(bmap, type, 1);
535 bmap = set_name(bmap, type, i, v->v->name);
537 } else if (tok->type == ISL_TOKEN_IDENT ||
538 tok->type == ISL_TOKEN_VALUE ||
541 if (type == isl_dim_param) {
542 isl_stream_error(s, tok,
543 "expecting unique identifier");
546 isl_stream_push_token(s, tok);
548 bmap = isl_basic_map_add(bmap, type, 1);
549 bmap = read_var_def(s, bmap, type, v);
553 tok = isl_stream_next_token(s);
554 if (!tok || tok->type != ',')
561 isl_stream_push_token(s, tok);
566 isl_basic_map_free(bmap);
570 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
575 struct isl_token *tok = NULL;
577 vec = accept_affine(s, v);
578 mat = isl_mat_from_row_vec(vec);
583 tok = isl_stream_next_token(s);
585 isl_stream_error(s, NULL, "unexpected EOF");
588 if (tok->type != ',') {
589 isl_stream_push_token(s, tok);
594 vec = accept_affine(s, v);
595 mat = isl_mat_add_zero_cols(mat, 1 + v->n - isl_mat_cols(mat));
596 mat = isl_mat_vec_concat(mat, vec);
607 static int read_div_definition(struct isl_stream *s, struct vars *v)
609 struct isl_token *tok;
612 struct variable *var;
614 if (isl_stream_eat(s, '['))
617 tok = isl_stream_next_token(s);
620 if (tok->type == '(') {
624 isl_stream_push_token(s, tok);
628 aff = accept_affine(s, v);
632 var->def = isl_vec_alloc(s->ctx, 2 + v->n);
638 isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
642 if (seen_paren && isl_stream_eat(s, ')'))
644 if (isl_stream_eat(s, '/'))
650 if (tok->type != ISL_TOKEN_VALUE) {
651 isl_stream_error(s, tok, "expected denominator");
652 isl_stream_push_token(s, tok);
655 isl_int_set(var->def->el[0], tok->u.v);
658 if (isl_stream_eat(s, ']'))
664 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
665 struct vars *v, struct isl_basic_map *bmap, int pos)
667 struct variable *var = v->v;
668 unsigned o_out = isl_basic_map_offset(bmap, isl_dim_out) - 1;
670 if (read_div_definition(s, v) < 0)
673 if (isl_basic_map_add_div_constraints_var(bmap, o_out + pos,
679 isl_basic_map_free(bmap);
683 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
684 struct vars *v, struct isl_basic_map *bmap)
686 struct isl_token *tok;
688 while ((tok = isl_stream_next_token(s)) != NULL) {
691 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
693 if (tok->type != ISL_TOKEN_IDENT)
696 p = vars_pos(v, tok->u.s, -1);
700 isl_stream_error(s, tok, "expecting unique identifier");
704 bmap = isl_basic_map_cow(bmap);
705 bmap = isl_basic_map_add(bmap, isl_dim_out, 1);
706 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
710 tok = isl_stream_next_token(s);
711 if (tok && tok->type == '=') {
713 bmap = add_div_definition(s, v, bmap, n_out);
714 tok = isl_stream_next_token(s);
717 if (!tok || tok->type != ',')
723 isl_stream_push_token(s, tok);
728 isl_basic_map_free(bmap);
732 static int next_is_tuple(struct isl_stream *s)
734 struct isl_token *tok;
737 tok = isl_stream_next_token(s);
740 if (tok->type == '[') {
741 isl_stream_push_token(s, tok);
744 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
745 isl_stream_push_token(s, tok);
749 is_tuple = isl_stream_next_token_is(s, '[');
751 isl_stream_push_token(s, tok);
756 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
757 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v);
759 static __isl_give isl_basic_map *read_nested_tuple(struct isl_stream *s,
760 __isl_take isl_basic_map *bmap, struct vars *v)
762 bmap = read_tuple(s, bmap, isl_dim_in, v);
763 if (isl_stream_eat(s, ISL_TOKEN_TO))
765 bmap = read_tuple(s, bmap, isl_dim_out, v);
766 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
769 isl_basic_map_free(bmap);
773 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
774 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
776 struct isl_token *tok;
779 tok = isl_stream_next_token(s);
780 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
781 name = strdup(tok->u.s);
785 tok = isl_stream_next_token(s);
787 if (!tok || tok->type != '[') {
788 isl_stream_error(s, tok, "expecting '['");
792 if (type != isl_dim_param && next_is_tuple(s)) {
793 isl_dim *dim = isl_basic_map_get_dim(bmap);
794 int nparam = isl_dim_size(dim, isl_dim_param);
795 int n_in = isl_dim_size(dim, isl_dim_in);
796 isl_basic_map *nested;
797 if (type == isl_dim_out)
798 dim = isl_dim_move(dim, isl_dim_param, nparam,
799 isl_dim_in, 0, n_in);
800 nested = isl_basic_map_alloc_dim(dim, 0, 0, 0);
801 nested = read_nested_tuple(s, nested, v);
802 if (type == isl_dim_in) {
803 nested = isl_basic_map_reverse(nested);
804 bmap = isl_basic_map_intersect(nested, bmap);
807 dim = isl_dim_range(isl_basic_map_get_dim(nested));
808 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
809 dim = isl_dim_join(isl_basic_map_get_dim(bmap), dim);
810 bset = isl_basic_map_domain(bmap);
811 nested = isl_basic_map_reset_dim(nested, dim);
812 bmap = isl_basic_map_intersect_domain(nested, bset);
815 bmap = read_var_list(s, bmap, type, v);
816 tok = isl_stream_next_token(s);
817 if (!tok || tok->type != ']') {
818 isl_stream_error(s, tok, "expecting ']'");
824 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
832 isl_basic_map_free(bmap);
836 static __isl_give isl_basic_map *construct_constraint(
837 __isl_take isl_basic_map *bmap, enum isl_token_type type,
838 isl_int *left, isl_int *right)
846 len = 1 + isl_basic_map_total_dim(bmap);
849 k = isl_basic_map_alloc_inequality(bmap);
852 if (type == ISL_TOKEN_LE)
853 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
856 else if (type == ISL_TOKEN_GE)
857 isl_seq_combine(bmap->ineq[k], ctx->one, left,
860 else if (type == ISL_TOKEN_LT) {
861 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
864 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
865 } else if (type == ISL_TOKEN_GT) {
866 isl_seq_combine(bmap->ineq[k], ctx->one, left,
869 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
871 isl_seq_combine(bmap->ineq[k], ctx->one, left,
874 isl_basic_map_inequality_to_equality(bmap, k);
879 isl_basic_map_free(bmap);
883 static int is_comparator(struct isl_token *tok)
900 /* Add any variables in the variable list "v" that are not already in "bmap"
901 * as output variables in "bmap".
903 static __isl_give isl_basic_map *add_lifted_divs(__isl_take isl_basic_map *bmap,
908 struct variable *var;
910 extra = v->n - isl_basic_map_total_dim(bmap);
915 bmap = isl_basic_map_add(bmap, isl_dim_out, extra);
916 bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
919 for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
920 var->def = isl_vec_zero_extend(var->def, 2 + v->n);
923 if (isl_basic_map_add_div_constraints_var(bmap, var->pos,
930 isl_basic_map_free(bmap);
934 static struct isl_basic_map *add_constraint(struct isl_stream *s,
935 struct vars *v, struct isl_basic_map *bmap)
938 struct isl_token *tok = NULL;
939 struct isl_mat *aff1 = NULL, *aff2 = NULL;
941 bmap = isl_basic_map_cow(bmap);
943 aff1 = accept_affine_list(s, v);
946 tok = isl_stream_next_token(s);
947 if (!is_comparator(tok)) {
948 isl_stream_error(s, tok, "missing operator");
950 isl_stream_push_token(s, tok);
955 aff2 = accept_affine_list(s, v);
959 aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
962 bmap = add_lifted_divs(bmap, v);
963 bmap = isl_basic_map_extend_constraints(bmap, 0,
964 aff1->n_row * aff2->n_row);
965 for (i = 0; i < aff1->n_row; ++i)
966 for (j = 0; j < aff2->n_row; ++j)
967 bmap = construct_constraint(bmap, tok->type,
968 aff1->row[i], aff2->row[j]);
973 tok = isl_stream_next_token(s);
974 if (!is_comparator(tok)) {
976 isl_stream_push_token(s, tok);
988 isl_basic_map_free(bmap);
992 static isl_map *read_constraint(struct isl_stream *s,
993 struct vars *v, __isl_take isl_basic_map *bmap)
1000 bmap = isl_basic_set_unwrap(isl_basic_set_lift(isl_basic_map_wrap(bmap)));
1002 bmap = add_constraint(s, v, bmap);
1003 bmap = isl_basic_map_simplify(bmap);
1004 bmap = isl_basic_map_finalize(bmap);
1006 bmap = isl_basic_set_unwrap(isl_basic_map_domain(bmap));
1008 vars_drop(v, v->n - n);
1010 return isl_map_from_basic_map(bmap);
1013 static struct isl_map *read_disjuncts(struct isl_stream *s,
1014 struct vars *v, __isl_take isl_basic_map *bmap);
1016 static __isl_give isl_map *read_exists(struct isl_stream *s,
1017 struct vars *v, __isl_take isl_basic_map *bmap)
1020 int seen_paren = isl_stream_eat_if_available(s, '(');
1021 isl_map *map = NULL;
1023 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
1024 bmap = read_defined_var_list(s, v, bmap);
1026 if (isl_stream_eat(s, ':'))
1029 map = read_disjuncts(s, v, bmap);
1030 map = isl_set_unwrap(isl_map_domain(map));
1033 vars_drop(v, v->n - n);
1034 if (seen_paren && isl_stream_eat(s, ')'))
1039 isl_basic_map_free(bmap);
1044 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1045 struct vars *v, __isl_take isl_basic_map *bmap)
1049 if (isl_stream_eat_if_available(s, '(')) {
1050 map = read_disjuncts(s, v, bmap);
1051 if (isl_stream_eat(s, ')'))
1056 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1057 return read_exists(s, v, bmap);
1059 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1060 return isl_map_from_basic_map(bmap);
1062 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1063 isl_dim *dim = isl_basic_map_get_dim(bmap);
1064 isl_basic_map_free(bmap);
1065 return isl_map_empty(dim);
1068 return read_constraint(s, v, bmap);
1074 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1075 struct vars *v, __isl_take isl_basic_map *bmap)
1080 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1081 map = read_conjunct(s, v, isl_basic_map_copy(bmap));
1084 t = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1085 map = isl_map_subtract(t, map);
1088 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1091 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1092 map_i = read_conjunct(s, v, isl_basic_map_copy(bmap));
1094 map = isl_map_subtract(map, map_i);
1096 map = isl_map_intersect(map, map_i);
1099 isl_basic_map_free(bmap);
1103 static struct isl_map *read_disjuncts(struct isl_stream *s,
1104 struct vars *v, __isl_take isl_basic_map *bmap)
1106 struct isl_map *map;
1108 if (isl_stream_next_token_is(s, '}')) {
1109 isl_dim *dim = isl_basic_map_get_dim(bmap);
1110 isl_basic_map_free(bmap);
1111 return isl_map_universe(dim);
1114 map = read_conjuncts(s, v, isl_basic_map_copy(bmap));
1115 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1118 map_i = read_conjuncts(s, v, isl_basic_map_copy(bmap));
1119 map = isl_map_union(map, map_i);
1122 isl_basic_map_free(bmap);
1126 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1128 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1129 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1130 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1131 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1133 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1134 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1135 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1137 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1138 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1139 isl_basic_map_dim(bmap, isl_dim_in) +
1140 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1141 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1143 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1149 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1150 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1153 struct isl_token *tok;
1163 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1164 dim = isl_basic_map_dim(bmap, isl_dim_out);
1166 tok = isl_stream_next_token(s);
1167 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1168 isl_stream_error(s, tok, "expecting coefficient");
1170 isl_stream_push_token(s, tok);
1173 if (!tok->on_new_line) {
1174 isl_stream_error(s, tok, "coefficient should appear on new line");
1175 isl_stream_push_token(s, tok);
1179 type = isl_int_get_si(tok->u.v);
1180 isl_token_free(tok);
1182 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1184 k = isl_basic_map_alloc_equality(bmap);
1187 k = isl_basic_map_alloc_inequality(bmap);
1193 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1195 tok = isl_stream_next_token(s);
1196 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1197 isl_stream_error(s, tok, "expecting coefficient");
1199 isl_stream_push_token(s, tok);
1202 if (tok->on_new_line) {
1203 isl_stream_error(s, tok,
1204 "coefficient should not appear on new line");
1205 isl_stream_push_token(s, tok);
1208 pos = polylib_pos_to_isl_pos(bmap, j);
1209 isl_int_set(c[pos], tok->u.v);
1210 isl_token_free(tok);
1215 isl_basic_map_free(bmap);
1219 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1223 struct isl_token *tok;
1224 struct isl_token *tok2;
1227 unsigned in = 0, out, local = 0;
1228 struct isl_basic_map *bmap = NULL;
1233 tok = isl_stream_next_token(s);
1235 isl_stream_error(s, NULL, "unexpected EOF");
1238 tok2 = isl_stream_next_token(s);
1240 isl_token_free(tok);
1241 isl_stream_error(s, NULL, "unexpected EOF");
1244 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1245 isl_stream_push_token(s, tok2);
1246 isl_stream_push_token(s, tok);
1247 isl_stream_error(s, NULL,
1248 "expecting constraint matrix dimensions");
1251 n_row = isl_int_get_si(tok->u.v);
1252 n_col = isl_int_get_si(tok2->u.v);
1253 on_new_line = tok2->on_new_line;
1254 isl_token_free(tok2);
1255 isl_token_free(tok);
1256 isl_assert(s->ctx, !on_new_line, return NULL);
1257 isl_assert(s->ctx, n_row >= 0, return NULL);
1258 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1259 tok = isl_stream_next_token_on_same_line(s);
1261 if (tok->type != ISL_TOKEN_VALUE) {
1262 isl_stream_error(s, tok,
1263 "expecting number of output dimensions");
1264 isl_stream_push_token(s, tok);
1267 out = isl_int_get_si(tok->u.v);
1268 isl_token_free(tok);
1270 tok = isl_stream_next_token_on_same_line(s);
1271 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1272 isl_stream_error(s, tok,
1273 "expecting number of input dimensions");
1275 isl_stream_push_token(s, tok);
1278 in = isl_int_get_si(tok->u.v);
1279 isl_token_free(tok);
1281 tok = isl_stream_next_token_on_same_line(s);
1282 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1283 isl_stream_error(s, tok,
1284 "expecting number of existentials");
1286 isl_stream_push_token(s, tok);
1289 local = isl_int_get_si(tok->u.v);
1290 isl_token_free(tok);
1292 tok = isl_stream_next_token_on_same_line(s);
1293 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1294 isl_stream_error(s, tok,
1295 "expecting number of parameters");
1297 isl_stream_push_token(s, tok);
1300 nparam = isl_int_get_si(tok->u.v);
1301 isl_token_free(tok);
1302 if (n_col != 1 + out + in + local + nparam + 1) {
1303 isl_stream_error(s, NULL,
1304 "dimensions don't match");
1308 out = n_col - 2 - nparam;
1309 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1313 for (i = 0; i < local; ++i) {
1314 int k = isl_basic_map_alloc_div(bmap);
1317 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1320 for (i = 0; i < n_row; ++i)
1321 bmap = basic_map_read_polylib_constraint(s, bmap);
1323 tok = isl_stream_next_token_on_same_line(s);
1325 isl_stream_error(s, tok, "unexpected extra token on line");
1326 isl_stream_push_token(s, tok);
1330 bmap = isl_basic_map_simplify(bmap);
1331 bmap = isl_basic_map_finalize(bmap);
1334 isl_basic_map_free(bmap);
1338 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1340 struct isl_token *tok;
1341 struct isl_token *tok2;
1343 struct isl_map *map;
1345 tok = isl_stream_next_token(s);
1347 isl_stream_error(s, NULL, "unexpected EOF");
1350 tok2 = isl_stream_next_token_on_same_line(s);
1351 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1352 isl_stream_push_token(s, tok2);
1353 isl_stream_push_token(s, tok);
1354 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1357 isl_stream_error(s, tok2, "unexpected token");
1358 isl_stream_push_token(s, tok2);
1359 isl_stream_push_token(s, tok);
1362 n = isl_int_get_si(tok->u.v);
1363 isl_token_free(tok);
1365 isl_assert(s->ctx, n >= 1, return NULL);
1367 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1369 for (i = 1; map && i < n; ++i)
1370 map = isl_map_union(map,
1371 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1376 static int optional_power(struct isl_stream *s)
1379 struct isl_token *tok;
1381 tok = isl_stream_next_token(s);
1384 if (tok->type != '^') {
1385 isl_stream_push_token(s, tok);
1388 isl_token_free(tok);
1389 tok = isl_stream_next_token(s);
1390 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1391 isl_stream_error(s, tok, "expecting exponent");
1393 isl_stream_push_token(s, tok);
1396 pow = isl_int_get_si(tok->u.v);
1397 isl_token_free(tok);
1401 static __isl_give isl_div *read_div(struct isl_stream *s,
1402 __isl_take isl_dim *dim, struct vars *v)
1405 isl_basic_map *bmap;
1408 bmap = isl_basic_map_universe(dim);
1410 if (vars_add_anon(v) < 0)
1412 if (read_div_definition(s, v) < 0)
1414 bmap = add_divs(bmap, v);
1415 bmap = isl_basic_map_order_divs(bmap);
1418 vars_drop(v, v->n - n);
1420 return isl_basic_map_div(bmap, bmap->n_div - 1);
1422 isl_basic_map_free(bmap);
1426 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1427 __isl_keep isl_basic_map *bmap, struct vars *v);
1429 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1430 __isl_keep isl_basic_map *bmap, struct vars *v)
1432 struct isl_qpolynomial *qp;
1433 struct isl_token *tok;
1435 tok = next_token(s);
1437 isl_stream_error(s, NULL, "unexpected EOF");
1440 if (tok->type == '(') {
1443 isl_token_free(tok);
1444 qp = read_term(s, bmap, v);
1447 if (isl_stream_eat(s, ')'))
1449 pow = optional_power(s);
1450 qp = isl_qpolynomial_pow(qp, pow);
1451 } else if (tok->type == ISL_TOKEN_VALUE) {
1452 struct isl_token *tok2;
1453 tok2 = isl_stream_next_token(s);
1454 if (tok2 && tok2->type == '/') {
1455 isl_token_free(tok2);
1456 tok2 = next_token(s);
1457 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1458 isl_stream_error(s, tok2, "expected denominator");
1459 isl_token_free(tok);
1460 isl_token_free(tok2);
1463 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1464 tok->u.v, tok2->u.v);
1465 isl_token_free(tok2);
1467 isl_stream_push_token(s, tok2);
1468 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1471 isl_token_free(tok);
1472 } else if (tok->type == ISL_TOKEN_INFTY) {
1473 isl_token_free(tok);
1474 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1475 } else if (tok->type == ISL_TOKEN_NAN) {
1476 isl_token_free(tok);
1477 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1478 } else if (tok->type == ISL_TOKEN_IDENT) {
1480 int pos = vars_pos(v, tok->u.s, -1);
1483 isl_token_free(tok);
1487 vars_drop(v, v->n - n);
1488 isl_stream_error(s, tok, "unknown identifier");
1489 isl_token_free(tok);
1492 isl_token_free(tok);
1493 pow = optional_power(s);
1494 qp = isl_qpolynomial_var_pow(isl_basic_map_get_dim(bmap), pos, pow);
1495 } else if (tok->type == '[') {
1499 isl_stream_push_token(s, tok);
1500 div = read_div(s, isl_basic_map_get_dim(bmap), v);
1501 pow = optional_power(s);
1502 qp = isl_qpolynomial_div_pow(div, pow);
1503 } else if (tok->type == '-') {
1504 struct isl_qpolynomial *qp2;
1506 isl_token_free(tok);
1507 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1509 qp2 = read_factor(s, bmap, v);
1510 qp = isl_qpolynomial_mul(qp, qp2);
1512 isl_stream_error(s, tok, "unexpected isl_token");
1513 isl_stream_push_token(s, tok);
1517 if (isl_stream_eat_if_available(s, '*') ||
1518 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1519 struct isl_qpolynomial *qp2;
1521 qp2 = read_factor(s, bmap, v);
1522 qp = isl_qpolynomial_mul(qp, qp2);
1527 isl_qpolynomial_free(qp);
1531 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1532 __isl_keep isl_basic_map *bmap, struct vars *v)
1534 struct isl_token *tok;
1535 struct isl_qpolynomial *qp;
1537 qp = read_factor(s, bmap, v);
1540 tok = next_token(s);
1544 if (tok->type == '+') {
1545 struct isl_qpolynomial *qp2;
1547 isl_token_free(tok);
1548 qp2 = read_factor(s, bmap, v);
1549 qp = isl_qpolynomial_add(qp, qp2);
1550 } else if (tok->type == '-') {
1551 struct isl_qpolynomial *qp2;
1553 isl_token_free(tok);
1554 qp2 = read_factor(s, bmap, v);
1555 qp = isl_qpolynomial_sub(qp, qp2);
1556 } else if (tok->type == ISL_TOKEN_VALUE &&
1557 isl_int_is_neg(tok->u.v)) {
1558 struct isl_qpolynomial *qp2;
1560 isl_stream_push_token(s, tok);
1561 qp2 = read_factor(s, bmap, v);
1562 qp = isl_qpolynomial_add(qp, qp2);
1564 isl_stream_push_token(s, tok);
1572 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1573 __isl_take isl_basic_map *bmap, struct vars *v)
1575 struct isl_token *tok;
1576 struct isl_map *map;
1578 tok = isl_stream_next_token(s);
1580 isl_stream_error(s, NULL, "unexpected EOF");
1583 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1584 if (tok->type == ':' ||
1585 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1586 isl_token_free(tok);
1587 map = isl_map_intersect(map,
1588 read_disjuncts(s, v, isl_basic_map_copy(bmap)));
1590 isl_stream_push_token(s, tok);
1592 isl_basic_map_free(bmap);
1596 isl_basic_map_free(bmap);
1600 static struct isl_obj obj_read_poly(struct isl_stream *s,
1601 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1603 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1604 struct isl_pw_qpolynomial *pwqp;
1605 struct isl_qpolynomial *qp;
1606 struct isl_map *map;
1607 struct isl_set *set;
1609 qp = read_term(s, bmap, v);
1610 map = read_optional_disjuncts(s, bmap, v);
1611 set = isl_map_range(map);
1613 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1615 vars_drop(v, v->n - n);
1621 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1622 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1624 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1625 struct isl_obj obj_p;
1626 isl_qpolynomial *qp;
1627 isl_qpolynomial_fold *fold = NULL;
1628 isl_pw_qpolynomial_fold *pwf;
1632 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1633 return obj_read_poly(s, bmap, v, n);
1635 if (isl_stream_eat(s, '('))
1638 qp = read_term(s, bmap, v);
1639 fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1641 while (isl_stream_eat_if_available(s, ',')) {
1642 isl_qpolynomial_fold *fold_i;
1643 qp = read_term(s, bmap, v);
1644 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1645 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1648 if (isl_stream_eat(s, ')'))
1651 map = read_optional_disjuncts(s, bmap, v);
1652 set = isl_map_range(map);
1653 pwf = isl_pw_qpolynomial_fold_alloc(isl_fold_max, set, fold);
1655 vars_drop(v, v->n - n);
1660 isl_basic_map_free(bmap);
1661 isl_qpolynomial_fold_free(fold);
1662 obj.type = isl_obj_none;
1666 static int is_rational(struct isl_stream *s)
1668 struct isl_token *tok;
1670 tok = isl_stream_next_token(s);
1673 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1674 isl_token_free(tok);
1675 isl_stream_eat(s, ':');
1679 isl_stream_push_token(s, tok);
1684 static struct isl_obj obj_read_body(struct isl_stream *s,
1685 __isl_take isl_basic_map *bmap, struct vars *v)
1687 struct isl_map *map = NULL;
1688 struct isl_token *tok;
1689 struct isl_obj obj = { isl_obj_set, NULL };
1693 bmap = isl_basic_map_set_rational(bmap);
1695 if (!next_is_tuple(s))
1696 return obj_read_poly_or_fold(s, bmap, v, n);
1698 bmap = read_tuple(s, bmap, isl_dim_in, v);
1701 tok = isl_stream_next_token(s);
1702 if (tok && tok->type == ISL_TOKEN_TO) {
1703 obj.type = isl_obj_map;
1704 isl_token_free(tok);
1705 if (!next_is_tuple(s)) {
1706 bmap = isl_basic_map_reverse(bmap);
1707 return obj_read_poly_or_fold(s, bmap, v, n);
1709 bmap = read_tuple(s, bmap, isl_dim_out, v);
1713 bmap = isl_basic_map_reverse(bmap);
1715 isl_stream_push_token(s, tok);
1718 map = read_optional_disjuncts(s, bmap, v);
1720 vars_drop(v, v->n - n);
1725 isl_basic_map_free(bmap);
1726 obj.type = isl_obj_none;
1730 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1732 if (obj.type == isl_obj_map) {
1733 obj.v = isl_union_map_from_map(obj.v);
1734 obj.type = isl_obj_union_map;
1735 } else if (obj.type == isl_obj_set) {
1736 obj.v = isl_union_set_from_set(obj.v);
1737 obj.type = isl_obj_union_set;
1738 } else if (obj.type == isl_obj_pw_qpolynomial) {
1739 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1740 obj.type = isl_obj_union_pw_qpolynomial;
1741 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1742 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1743 obj.type = isl_obj_union_pw_qpolynomial_fold;
1745 isl_assert(ctx, 0, goto error);
1748 obj.type->free(obj.v);
1749 obj.type = isl_obj_none;
1753 static struct isl_obj obj_add(struct isl_ctx *ctx,
1754 struct isl_obj obj1, struct isl_obj obj2)
1756 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1757 obj1 = to_union(ctx, obj1);
1758 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1759 obj2 = to_union(ctx, obj2);
1760 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1761 obj1 = to_union(ctx, obj1);
1762 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1763 obj2 = to_union(ctx, obj2);
1764 if (obj1.type == isl_obj_pw_qpolynomial &&
1765 obj2.type == isl_obj_union_pw_qpolynomial)
1766 obj1 = to_union(ctx, obj1);
1767 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1768 obj2.type == isl_obj_pw_qpolynomial)
1769 obj2 = to_union(ctx, obj2);
1770 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1771 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1772 obj1 = to_union(ctx, obj1);
1773 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1774 obj2.type == isl_obj_pw_qpolynomial_fold)
1775 obj2 = to_union(ctx, obj2);
1776 isl_assert(ctx, obj1.type == obj2.type, goto error);
1777 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1778 obj1 = to_union(ctx, obj1);
1779 obj2 = to_union(ctx, obj2);
1781 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1782 obj1 = to_union(ctx, obj1);
1783 obj2 = to_union(ctx, obj2);
1785 if (obj1.type == isl_obj_pw_qpolynomial &&
1786 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1787 obj1 = to_union(ctx, obj1);
1788 obj2 = to_union(ctx, obj2);
1790 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1791 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1792 obj1 = to_union(ctx, obj1);
1793 obj2 = to_union(ctx, obj2);
1795 obj1.v = obj1.type->add(obj1.v, obj2.v);
1798 obj1.type->free(obj1.v);
1799 obj2.type->free(obj2.v);
1800 obj1.type = isl_obj_none;
1805 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1807 isl_basic_map *bmap = NULL;
1808 struct isl_token *tok;
1809 struct vars *v = NULL;
1810 struct isl_obj obj = { isl_obj_set, NULL };
1812 tok = next_token(s);
1814 isl_stream_error(s, NULL, "unexpected EOF");
1817 if (tok->type == ISL_TOKEN_VALUE) {
1818 struct isl_token *tok2;
1819 struct isl_map *map;
1821 tok2 = isl_stream_next_token(s);
1822 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1823 isl_int_is_neg(tok2->u.v)) {
1825 isl_stream_push_token(s, tok2);
1826 obj.type = isl_obj_int;
1827 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1828 isl_token_free(tok);
1831 isl_stream_push_token(s, tok2);
1832 isl_stream_push_token(s, tok);
1833 map = map_read_polylib(s, nparam);
1836 if (isl_map_dim(map, isl_dim_in) > 0)
1837 obj.type = isl_obj_map;
1841 v = vars_new(s->ctx);
1843 isl_stream_push_token(s, tok);
1846 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1847 if (tok->type == '[') {
1848 isl_stream_push_token(s, tok);
1849 bmap = read_tuple(s, bmap, isl_dim_param, v);
1853 isl_assert(s->ctx, nparam == v->n, goto error);
1854 tok = isl_stream_next_token(s);
1855 if (!tok || tok->type != ISL_TOKEN_TO) {
1856 isl_stream_error(s, tok, "expecting '->'");
1858 isl_stream_push_token(s, tok);
1861 isl_token_free(tok);
1862 tok = isl_stream_next_token(s);
1863 } else if (nparam > 0)
1864 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1865 if (!tok || tok->type != '{') {
1866 isl_stream_error(s, tok, "expecting '{'");
1868 isl_stream_push_token(s, tok);
1871 isl_token_free(tok);
1873 tok = isl_stream_next_token(s);
1876 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1877 isl_token_free(tok);
1878 if (isl_stream_eat(s, '='))
1880 bmap = read_tuple(s, bmap, isl_dim_param, v);
1884 isl_assert(s->ctx, nparam == v->n, goto error);
1885 } else if (tok->type == '}') {
1886 obj.type = isl_obj_union_set;
1887 obj.v = isl_union_set_empty(isl_basic_map_get_dim(bmap));
1888 isl_token_free(tok);
1891 isl_stream_push_token(s, tok);
1896 o = obj_read_body(s, isl_basic_map_copy(bmap), v);
1897 if (o.type == isl_obj_none || !o.v)
1902 obj = obj_add(s->ctx, obj, o);
1903 if (obj.type == isl_obj_none || !obj.v)
1906 tok = isl_stream_next_token(s);
1907 if (!tok || tok->type != ';')
1909 isl_token_free(tok);
1910 if (isl_stream_next_token_is(s, '}')) {
1911 tok = isl_stream_next_token(s);
1916 if (tok && tok->type == '}') {
1917 isl_token_free(tok);
1919 isl_stream_error(s, tok, "unexpected isl_token");
1921 isl_token_free(tok);
1926 isl_basic_map_free(bmap);
1930 isl_basic_map_free(bmap);
1931 obj.type->free(obj.v);
1938 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1940 return obj_read(s, -1);
1943 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1946 struct isl_map *map;
1948 obj = obj_read(s, nparam);
1950 isl_assert(s->ctx, obj.type == isl_obj_map ||
1951 obj.type == isl_obj_set, goto error);
1955 obj.type->free(obj.v);
1959 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1962 struct isl_set *set;
1964 obj = obj_read(s, nparam);
1966 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1970 obj.type->free(obj.v);
1974 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1977 isl_union_map *umap;
1979 obj = obj_read(s, -1);
1980 if (obj.type == isl_obj_map) {
1981 obj.type = isl_obj_union_map;
1982 obj.v = isl_union_map_from_map(obj.v);
1984 if (obj.type == isl_obj_set) {
1985 obj.type = isl_obj_union_set;
1986 obj.v = isl_union_set_from_set(obj.v);
1989 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1990 obj.type == isl_obj_union_set, goto error);
1994 obj.type->free(obj.v);
1998 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2001 isl_union_set *uset;
2003 obj = obj_read(s, -1);
2004 if (obj.type == isl_obj_set) {
2005 obj.type = isl_obj_union_set;
2006 obj.v = isl_union_set_from_set(obj.v);
2009 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2013 obj.type->free(obj.v);
2017 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2020 struct isl_map *map;
2021 struct isl_basic_map *bmap;
2023 obj = obj_read(s, nparam);
2028 isl_assert(map->ctx, map->n <= 1, goto error);
2031 bmap = isl_basic_map_empty_like_map(map);
2033 bmap = isl_basic_map_copy(map->p[0]);
2043 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2044 FILE *input, int nparam)
2046 struct isl_basic_map *bmap;
2047 struct isl_stream *s = isl_stream_new_file(ctx, input);
2050 bmap = basic_map_read(s, nparam);
2055 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2056 FILE *input, int nparam)
2058 struct isl_basic_map *bmap;
2059 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
2062 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2063 return (struct isl_basic_set *)bmap;
2065 isl_basic_map_free(bmap);
2069 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2070 const char *str, int nparam)
2072 struct isl_basic_map *bmap;
2073 struct isl_stream *s = isl_stream_new_str(ctx, str);
2076 bmap = basic_map_read(s, nparam);
2081 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2082 const char *str, int nparam)
2084 struct isl_basic_map *bmap;
2085 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
2088 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2089 return (struct isl_basic_set *)bmap;
2091 isl_basic_map_free(bmap);
2095 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2096 FILE *input, int nparam)
2098 struct isl_map *map;
2099 struct isl_stream *s = isl_stream_new_file(ctx, input);
2102 map = isl_stream_read_map(s, nparam);
2107 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2108 const char *str, int nparam)
2110 struct isl_map *map;
2111 struct isl_stream *s = isl_stream_new_str(ctx, str);
2114 map = isl_stream_read_map(s, nparam);
2119 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2120 FILE *input, int nparam)
2122 struct isl_map *map;
2123 map = isl_map_read_from_file(ctx, input, nparam);
2126 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2127 return (struct isl_set *)map;
2133 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2134 const char *str, int nparam)
2136 struct isl_map *map;
2137 map = isl_map_read_from_str(ctx, str, nparam);
2140 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2141 return (struct isl_set *)map;
2147 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2150 isl_union_map *umap;
2151 struct isl_stream *s = isl_stream_new_file(ctx, input);
2154 umap = isl_stream_read_union_map(s);
2159 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2162 isl_union_map *umap;
2163 struct isl_stream *s = isl_stream_new_str(ctx, str);
2166 umap = isl_stream_read_union_map(s);
2171 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2174 isl_union_set *uset;
2175 struct isl_stream *s = isl_stream_new_file(ctx, input);
2178 uset = isl_stream_read_union_set(s);
2183 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2186 isl_union_set *uset;
2187 struct isl_stream *s = isl_stream_new_str(ctx, str);
2190 uset = isl_stream_read_union_set(s);
2195 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2197 struct isl_vec *vec = NULL;
2198 struct isl_token *tok;
2202 tok = isl_stream_next_token(s);
2203 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2204 isl_stream_error(s, tok, "expecting vector length");
2208 size = isl_int_get_si(tok->u.v);
2209 isl_token_free(tok);
2211 vec = isl_vec_alloc(s->ctx, size);
2213 for (j = 0; j < size; ++j) {
2214 tok = isl_stream_next_token(s);
2215 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2216 isl_stream_error(s, tok, "expecting constant value");
2219 isl_int_set(vec->el[j], tok->u.v);
2220 isl_token_free(tok);
2225 isl_token_free(tok);
2230 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2232 return isl_vec_read_polylib(s);
2235 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2238 struct isl_stream *s = isl_stream_new_file(ctx, input);
2246 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2247 struct isl_stream *s)
2250 struct isl_pw_qpolynomial *pwqp;
2252 obj = obj_read(s, -1);
2254 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2259 obj.type->free(obj.v);
2263 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2266 isl_pw_qpolynomial *pwqp;
2267 struct isl_stream *s = isl_stream_new_str(ctx, str);
2270 pwqp = isl_stream_read_pw_qpolynomial(s);