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
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
21 #include <isl/stream.h>
23 #include "isl_polynomial_private.h"
24 #include <isl/union_map.h>
25 #include <isl_mat_private.h>
26 #include <isl_aff_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;
63 static void vars_free(struct vars *v)
71 static void vars_drop(struct vars *v, int n)
82 struct variable *next = var->next;
90 static struct variable *variable_new(struct vars *v, const char *name, int len,
94 var = isl_calloc_type(v->ctx, struct variable);
97 var->name = strdup(name);
98 var->name[len] = '\0';
107 static int vars_pos(struct vars *v, const char *s, int len)
114 for (q = v->v; q; q = q->next) {
115 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
122 v->v = variable_new(v, s, len, v->n);
130 static int vars_add_anon(struct vars *v)
132 v->v = variable_new(v, "", 0, v->n);
141 static __isl_give isl_map *set_name(__isl_take isl_map *map,
142 enum isl_dim_type type, unsigned pos, char *name)
151 prime = strchr(name, '\'');
154 map = isl_map_set_dim_name(map, type, pos, name);
161 /* Obtain next token, with some preprocessing.
162 * In particular, evaluate expressions of the form x^y,
163 * with x and y values.
165 static struct isl_token *next_token(struct isl_stream *s)
167 struct isl_token *tok, *tok2;
169 tok = isl_stream_next_token(s);
170 if (!tok || tok->type != ISL_TOKEN_VALUE)
172 if (!isl_stream_eat_if_available(s, '^'))
174 tok2 = isl_stream_next_token(s);
175 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
176 isl_stream_error(s, tok2, "expecting constant value");
180 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
182 isl_token_free(tok2);
186 isl_token_free(tok2);
190 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
192 struct isl_token *tok;
195 if (!tok || tok->type != ISL_TOKEN_VALUE) {
196 isl_stream_error(s, tok, "expecting constant value");
200 isl_int_mul(*f, *f, tok->u.v);
204 if (isl_stream_eat_if_available(s, '*'))
205 return accept_cst_factor(s, f);
213 /* Given an affine expression aff, return an affine expression
214 * for aff % d, with d the next token on the stream, which is
215 * assumed to be a constant.
217 * We introduce an integer division q = [aff/d] and the result
218 * is set to aff - d q.
220 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
221 struct vars *v, __isl_take isl_pw_aff *aff)
223 struct isl_token *tok;
227 if (!tok || tok->type != ISL_TOKEN_VALUE) {
228 isl_stream_error(s, tok, "expecting constant value");
232 q = isl_pw_aff_copy(aff);
233 q = isl_pw_aff_scale_down(q, tok->u.v);
234 q = isl_pw_aff_floor(q);
235 q = isl_pw_aff_scale(q, tok->u.v);
237 aff = isl_pw_aff_sub(aff, q);
242 isl_pw_aff_free(aff);
247 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
248 __isl_take isl_dim *dim, struct vars *v);
249 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
250 __isl_take isl_dim *dim, struct vars *v);
252 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
253 __isl_take isl_dim *dim, struct vars *v)
255 struct isl_token *tok;
256 isl_pw_aff_list *list = NULL;
259 tok = isl_stream_next_token(s);
262 min = tok->type == ISL_TOKEN_MIN;
265 if (isl_stream_eat(s, '('))
268 list = accept_affine_list(s, isl_dim_copy(dim), v);
272 if (isl_stream_eat(s, ')'))
276 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
279 isl_pw_aff_list_free(list);
283 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
284 __isl_take isl_dim *dim, struct vars *v)
286 struct isl_token *tok;
290 isl_pw_aff *pwaff = NULL;
292 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
294 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
297 if (isl_stream_eat(s, '('))
300 if (isl_stream_eat(s, '['))
302 if (isl_stream_eat_if_available(s, '('))
306 pwaff = accept_affine(s, isl_dim_copy(dim), v);
309 if (isl_stream_eat(s, ','))
312 if (seen_paren && isl_stream_eat(s, ')'))
314 if (isl_stream_eat(s, '/'))
321 if (tok->type != ISL_TOKEN_VALUE) {
322 isl_stream_error(s, tok, "expected denominator");
323 isl_stream_push_token(s, tok);
326 isl_pw_aff_scale_down(pwaff, tok->u.v);
330 pwaff = isl_pw_aff_ceil(pwaff);
332 pwaff = isl_pw_aff_floor(pwaff);
335 if (isl_stream_eat(s, ')'))
338 if (isl_stream_eat(s, ']'))
346 isl_pw_aff_free(pwaff);
350 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
351 __isl_take isl_dim *dim, struct vars *v)
353 struct isl_token *tok = NULL;
354 isl_pw_aff *res = NULL;
358 isl_stream_error(s, NULL, "unexpected EOF");
361 if (tok->type == ISL_TOKEN_IDENT) {
363 int pos = vars_pos(v, tok->u.s, -1);
369 isl_stream_error(s, tok, "unknown identifier");
373 aff = isl_aff_zero(isl_local_space_from_dim(isl_dim_copy(dim)));
376 isl_int_set_si(aff->v->el[2 + pos], 1);
377 res = isl_pw_aff_from_aff(aff);
379 } else if (tok->type == ISL_TOKEN_VALUE) {
380 if (isl_stream_eat_if_available(s, '*')) {
381 res = accept_affine_factor(s, isl_dim_copy(dim), v);
382 res = isl_pw_aff_scale(res, tok->u.v);
386 ls = isl_local_space_from_dim(isl_dim_copy(dim));
387 aff = isl_aff_zero(ls);
388 aff = isl_aff_add_constant(aff, tok->u.v);
389 res = isl_pw_aff_from_aff(aff);
392 } else if (tok->type == '(') {
395 res = accept_affine(s, isl_dim_copy(dim), v);
398 if (isl_stream_eat(s, ')'))
400 } else if (tok->type == '[' ||
401 tok->type == ISL_TOKEN_FLOORD ||
402 tok->type == ISL_TOKEN_CEILD) {
403 isl_stream_push_token(s, tok);
405 res = accept_div(s, isl_dim_copy(dim), v);
406 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
407 isl_stream_push_token(s, tok);
409 res = accept_minmax(s, isl_dim_copy(dim), v);
411 isl_stream_error(s, tok, "expecting factor");
414 if (isl_stream_eat_if_available(s, '%') ||
415 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
417 return affine_mod(s, v, res);
419 if (isl_stream_eat_if_available(s, '*')) {
422 isl_int_set_si(f, 1);
423 if (accept_cst_factor(s, &f) < 0) {
427 res = isl_pw_aff_scale(res, f);
436 isl_pw_aff_free(res);
441 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
445 aff = isl_aff_zero(isl_local_space_from_dim(isl_pw_aff_get_dim(pwaff)));
446 aff = isl_aff_add_constant(aff, v);
448 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
451 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
452 __isl_take isl_dim *dim, struct vars *v)
454 struct isl_token *tok = NULL;
459 ls = isl_local_space_from_dim(isl_dim_copy(dim));
460 res = isl_pw_aff_from_aff(isl_aff_zero(ls));
467 isl_stream_error(s, NULL, "unexpected EOF");
470 if (tok->type == '-') {
475 if (tok->type == '(' || tok->type == '[' ||
476 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
477 tok->type == ISL_TOKEN_FLOORD ||
478 tok->type == ISL_TOKEN_CEILD ||
479 tok->type == ISL_TOKEN_IDENT) {
481 isl_stream_push_token(s, tok);
483 term = accept_affine_factor(s, isl_dim_copy(dim), v);
485 res = isl_pw_aff_sub(res, term);
487 res = isl_pw_aff_add(res, term);
491 } else if (tok->type == ISL_TOKEN_VALUE) {
493 isl_int_neg(tok->u.v, tok->u.v);
494 if (isl_stream_eat_if_available(s, '*') ||
495 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
497 term = accept_affine_factor(s,
498 isl_dim_copy(dim), v);
499 term = isl_pw_aff_scale(term, tok->u.v);
500 res = isl_pw_aff_add(res, term);
504 res = add_cst(res, tok->u.v);
508 isl_stream_error(s, tok, "unexpected isl_token");
509 isl_stream_push_token(s, tok);
510 isl_pw_aff_free(res);
517 if (tok && tok->type == '-') {
520 } else if (tok && tok->type == '+') {
523 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
524 isl_int_is_neg(tok->u.v)) {
525 isl_stream_push_token(s, tok);
528 isl_stream_push_token(s, tok);
538 isl_pw_aff_free(res);
542 static __isl_give isl_map *read_var_def(struct isl_stream *s,
543 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
549 pos = isl_map_dim(map, isl_dim_in);
550 if (type == isl_dim_out)
551 pos += isl_map_dim(map, isl_dim_out);
554 def = accept_affine(s, isl_dim_wrap(isl_map_get_dim(map)), v);
555 def_map = isl_map_from_pw_aff(def);
556 def_map = isl_map_equate(def_map, isl_dim_in, pos, isl_dim_out, 0);
557 def_map = isl_set_unwrap(isl_map_domain(def_map));
559 map = isl_map_intersect(map, def_map);
564 static __isl_give isl_map *read_var_list(struct isl_stream *s,
565 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
568 struct isl_token *tok;
570 if (isl_stream_next_token_is(s, ']'))
573 while ((tok = next_token(s)) != NULL) {
576 if (tok->type == ISL_TOKEN_IDENT) {
578 int p = vars_pos(v, tok->u.s, -1);
585 map = isl_map_add_dims(map, type, 1);
586 map = set_name(map, type, i, v->v->name);
589 if (type == isl_dim_param) {
590 isl_stream_error(s, tok,
591 "expecting unique identifier");
594 isl_stream_push_token(s, tok);
596 if (vars_add_anon(v) < 0)
598 map = isl_map_add_dims(map, type, 1);
599 map = read_var_def(s, map, type, v);
602 tok = isl_stream_next_token(s);
603 if (tok && tok->type == ']' &&
604 isl_stream_next_token_is(s, '[')) {
606 tok = isl_stream_next_token(s);
607 } else if (!tok || tok->type != ',')
614 isl_stream_push_token(s, tok);
623 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
624 __isl_take isl_dim *dim, struct vars *v)
627 isl_pw_aff_list *list;
628 struct isl_token *tok = NULL;
630 pwaff = accept_affine(s, isl_dim_copy(dim), v);
631 list = isl_pw_aff_list_from_pw_aff(pwaff);
636 tok = isl_stream_next_token(s);
638 isl_stream_error(s, NULL, "unexpected EOF");
641 if (tok->type != ',') {
642 isl_stream_push_token(s, tok);
647 pwaff = accept_affine(s, isl_dim_copy(dim), v);
648 list = isl_pw_aff_list_concat(list,
649 isl_pw_aff_list_from_pw_aff(pwaff));
658 isl_pw_aff_list_free(list);
662 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
663 struct vars *v, __isl_take isl_map *map)
665 struct isl_token *tok;
667 while ((tok = isl_stream_next_token(s)) != NULL) {
671 if (tok->type != ISL_TOKEN_IDENT)
674 p = vars_pos(v, tok->u.s, -1);
678 isl_stream_error(s, tok, "expecting unique identifier");
682 map = isl_map_add_dims(map, isl_dim_out, 1);
685 tok = isl_stream_next_token(s);
686 if (tok && tok->type == '=') {
688 map = read_var_def(s, map, isl_dim_out, v);
689 tok = isl_stream_next_token(s);
692 if (!tok || tok->type != ',')
698 isl_stream_push_token(s, tok);
707 static int next_is_tuple(struct isl_stream *s)
709 struct isl_token *tok;
712 tok = isl_stream_next_token(s);
715 if (tok->type == '[') {
716 isl_stream_push_token(s, tok);
719 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
720 isl_stream_push_token(s, tok);
724 is_tuple = isl_stream_next_token_is(s, '[');
726 isl_stream_push_token(s, tok);
731 static __isl_give isl_map *read_tuple(struct isl_stream *s,
732 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
734 static __isl_give isl_map *read_nested_tuple(struct isl_stream *s,
735 __isl_take isl_map *map, struct vars *v)
737 map = read_tuple(s, map, isl_dim_in, v);
738 if (isl_stream_eat(s, ISL_TOKEN_TO))
740 map = read_tuple(s, map, isl_dim_out, v);
741 map = isl_map_from_range(isl_map_wrap(map));
748 static __isl_give isl_map *read_tuple(struct isl_stream *s,
749 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
751 struct isl_token *tok;
754 tok = isl_stream_next_token(s);
755 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
756 name = strdup(tok->u.s);
760 tok = isl_stream_next_token(s);
762 if (!tok || tok->type != '[') {
763 isl_stream_error(s, tok, "expecting '['");
767 if (type != isl_dim_param && next_is_tuple(s)) {
768 isl_dim *dim = isl_map_get_dim(map);
769 int nparam = isl_dim_size(dim, isl_dim_param);
770 int n_in = isl_dim_size(dim, isl_dim_in);
772 if (type == isl_dim_out)
773 dim = isl_dim_move(dim, isl_dim_param, nparam,
774 isl_dim_in, 0, n_in);
775 nested = isl_map_universe(dim);
776 nested = read_nested_tuple(s, nested, v);
777 if (type == isl_dim_in) {
778 nested = isl_map_reverse(nested);
779 map = isl_map_intersect(nested, map);
782 dim = isl_dim_range(isl_map_get_dim(nested));
783 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
784 dim = isl_dim_join(isl_map_get_dim(map), dim);
785 set = isl_map_domain(map);
786 nested = isl_map_reset_dim(nested, dim);
787 map = isl_map_intersect_domain(nested, set);
790 map = read_var_list(s, map, type, v);
791 tok = isl_stream_next_token(s);
792 if (!tok || tok->type != ']') {
793 isl_stream_error(s, tok, "expecting ']'");
799 map = isl_map_set_tuple_name(map, type, name);
811 static __isl_give isl_set *construct_constraints(
812 __isl_take isl_set *set, enum isl_token_type type,
813 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
817 if (type == ISL_TOKEN_LE)
818 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
819 isl_pw_aff_list_copy(right));
820 else if (type == ISL_TOKEN_GE)
821 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
822 isl_pw_aff_list_copy(right));
823 else if (type == ISL_TOKEN_LT)
824 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
825 isl_pw_aff_list_copy(right));
826 else if (type == ISL_TOKEN_GT)
827 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
828 isl_pw_aff_list_copy(right));
830 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
831 isl_pw_aff_list_copy(right));
833 return isl_set_intersect(set, cond);
836 static int is_comparator(struct isl_token *tok)
853 static __isl_give isl_map *add_constraint(struct isl_stream *s,
854 struct vars *v, __isl_take isl_map *map)
856 struct isl_token *tok = NULL;
857 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
860 set = isl_map_wrap(map);
861 list1 = accept_affine_list(s, isl_set_get_dim(set), v);
864 tok = isl_stream_next_token(s);
865 if (!is_comparator(tok)) {
866 isl_stream_error(s, tok, "missing operator");
868 isl_stream_push_token(s, tok);
873 list2 = accept_affine_list(s, isl_set_get_dim(set), v);
877 set = construct_constraints(set, tok->type, list1, list2);
879 isl_pw_aff_list_free(list1);
882 tok = isl_stream_next_token(s);
883 if (!is_comparator(tok)) {
885 isl_stream_push_token(s, tok);
889 isl_pw_aff_list_free(list1);
891 return isl_set_unwrap(set);
895 isl_pw_aff_list_free(list1);
896 isl_pw_aff_list_free(list2);
901 static struct isl_map *read_disjuncts(struct isl_stream *s,
902 struct vars *v, __isl_take isl_map *map);
904 static __isl_give isl_map *read_exists(struct isl_stream *s,
905 struct vars *v, __isl_take isl_map *map)
908 int seen_paren = isl_stream_eat_if_available(s, '(');
910 map = isl_map_from_domain(isl_map_wrap(map));
911 map = read_defined_var_list(s, v, map);
913 if (isl_stream_eat(s, ':'))
916 map = read_disjuncts(s, v, map);
917 map = isl_set_unwrap(isl_map_domain(map));
919 vars_drop(v, v->n - n);
920 if (seen_paren && isl_stream_eat(s, ')'))
929 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
930 struct vars *v, __isl_take isl_map *map)
932 if (isl_stream_eat_if_available(s, '(')) {
933 map = read_disjuncts(s, v, map);
934 if (isl_stream_eat(s, ')'))
939 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
940 return read_exists(s, v, map);
942 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
945 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
946 isl_dim *dim = isl_map_get_dim(map);
948 return isl_map_empty(dim);
951 return add_constraint(s, v, map);
957 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
958 struct vars *v, __isl_take isl_map *map)
963 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
964 res = read_conjunct(s, v, isl_map_copy(map));
966 res = isl_map_subtract(isl_map_copy(map), res);
968 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
971 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
972 res_i = read_conjunct(s, v, isl_map_copy(map));
974 res = isl_map_subtract(res, res_i);
976 res = isl_map_intersect(res, res_i);
983 static struct isl_map *read_disjuncts(struct isl_stream *s,
984 struct vars *v, __isl_take isl_map *map)
988 if (isl_stream_next_token_is(s, '}')) {
989 isl_dim *dim = isl_map_get_dim(map);
991 return isl_map_universe(dim);
994 res = read_conjuncts(s, v, isl_map_copy(map));
995 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
998 res_i = read_conjuncts(s, v, isl_map_copy(map));
999 res = isl_map_union(res, res_i);
1006 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1008 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1009 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1010 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1011 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1013 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1014 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1015 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1017 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1018 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1019 isl_basic_map_dim(bmap, isl_dim_in) +
1020 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1021 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1023 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1029 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1030 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1033 struct isl_token *tok;
1043 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1044 dim = isl_basic_map_dim(bmap, isl_dim_out);
1046 tok = isl_stream_next_token(s);
1047 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1048 isl_stream_error(s, tok, "expecting coefficient");
1050 isl_stream_push_token(s, tok);
1053 if (!tok->on_new_line) {
1054 isl_stream_error(s, tok, "coefficient should appear on new line");
1055 isl_stream_push_token(s, tok);
1059 type = isl_int_get_si(tok->u.v);
1060 isl_token_free(tok);
1062 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1064 k = isl_basic_map_alloc_equality(bmap);
1067 k = isl_basic_map_alloc_inequality(bmap);
1073 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1075 tok = isl_stream_next_token(s);
1076 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1077 isl_stream_error(s, tok, "expecting coefficient");
1079 isl_stream_push_token(s, tok);
1082 if (tok->on_new_line) {
1083 isl_stream_error(s, tok,
1084 "coefficient should not appear on new line");
1085 isl_stream_push_token(s, tok);
1088 pos = polylib_pos_to_isl_pos(bmap, j);
1089 isl_int_set(c[pos], tok->u.v);
1090 isl_token_free(tok);
1095 isl_basic_map_free(bmap);
1099 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1103 struct isl_token *tok;
1104 struct isl_token *tok2;
1107 unsigned in = 0, out, local = 0;
1108 struct isl_basic_map *bmap = NULL;
1113 tok = isl_stream_next_token(s);
1115 isl_stream_error(s, NULL, "unexpected EOF");
1118 tok2 = isl_stream_next_token(s);
1120 isl_token_free(tok);
1121 isl_stream_error(s, NULL, "unexpected EOF");
1124 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1125 isl_stream_push_token(s, tok2);
1126 isl_stream_push_token(s, tok);
1127 isl_stream_error(s, NULL,
1128 "expecting constraint matrix dimensions");
1131 n_row = isl_int_get_si(tok->u.v);
1132 n_col = isl_int_get_si(tok2->u.v);
1133 on_new_line = tok2->on_new_line;
1134 isl_token_free(tok2);
1135 isl_token_free(tok);
1136 isl_assert(s->ctx, !on_new_line, return NULL);
1137 isl_assert(s->ctx, n_row >= 0, return NULL);
1138 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1139 tok = isl_stream_next_token_on_same_line(s);
1141 if (tok->type != ISL_TOKEN_VALUE) {
1142 isl_stream_error(s, tok,
1143 "expecting number of output dimensions");
1144 isl_stream_push_token(s, tok);
1147 out = isl_int_get_si(tok->u.v);
1148 isl_token_free(tok);
1150 tok = isl_stream_next_token_on_same_line(s);
1151 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1152 isl_stream_error(s, tok,
1153 "expecting number of input dimensions");
1155 isl_stream_push_token(s, tok);
1158 in = isl_int_get_si(tok->u.v);
1159 isl_token_free(tok);
1161 tok = isl_stream_next_token_on_same_line(s);
1162 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1163 isl_stream_error(s, tok,
1164 "expecting number of existentials");
1166 isl_stream_push_token(s, tok);
1169 local = isl_int_get_si(tok->u.v);
1170 isl_token_free(tok);
1172 tok = isl_stream_next_token_on_same_line(s);
1173 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1174 isl_stream_error(s, tok,
1175 "expecting number of parameters");
1177 isl_stream_push_token(s, tok);
1180 nparam = isl_int_get_si(tok->u.v);
1181 isl_token_free(tok);
1182 if (n_col != 1 + out + in + local + nparam + 1) {
1183 isl_stream_error(s, NULL,
1184 "dimensions don't match");
1188 out = n_col - 2 - nparam;
1189 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1193 for (i = 0; i < local; ++i) {
1194 int k = isl_basic_map_alloc_div(bmap);
1197 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1200 for (i = 0; i < n_row; ++i)
1201 bmap = basic_map_read_polylib_constraint(s, bmap);
1203 tok = isl_stream_next_token_on_same_line(s);
1205 isl_stream_error(s, tok, "unexpected extra token on line");
1206 isl_stream_push_token(s, tok);
1210 bmap = isl_basic_map_simplify(bmap);
1211 bmap = isl_basic_map_finalize(bmap);
1214 isl_basic_map_free(bmap);
1218 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1220 struct isl_token *tok;
1221 struct isl_token *tok2;
1223 struct isl_map *map;
1225 tok = isl_stream_next_token(s);
1227 isl_stream_error(s, NULL, "unexpected EOF");
1230 tok2 = isl_stream_next_token_on_same_line(s);
1231 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1232 isl_stream_push_token(s, tok2);
1233 isl_stream_push_token(s, tok);
1234 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1237 isl_stream_error(s, tok2, "unexpected token");
1238 isl_stream_push_token(s, tok2);
1239 isl_stream_push_token(s, tok);
1242 n = isl_int_get_si(tok->u.v);
1243 isl_token_free(tok);
1245 isl_assert(s->ctx, n >= 1, return NULL);
1247 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1249 for (i = 1; map && i < n; ++i)
1250 map = isl_map_union(map,
1251 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1256 static int optional_power(struct isl_stream *s)
1259 struct isl_token *tok;
1261 tok = isl_stream_next_token(s);
1264 if (tok->type != '^') {
1265 isl_stream_push_token(s, tok);
1268 isl_token_free(tok);
1269 tok = isl_stream_next_token(s);
1270 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1271 isl_stream_error(s, tok, "expecting exponent");
1273 isl_stream_push_token(s, tok);
1276 pow = isl_int_get_si(tok->u.v);
1277 isl_token_free(tok);
1281 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1282 __isl_keep isl_map *map, struct vars *v);
1284 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1285 __isl_keep isl_map *map, struct vars *v)
1287 isl_pw_qpolynomial *pwqp;
1288 struct isl_token *tok;
1290 tok = next_token(s);
1292 isl_stream_error(s, NULL, "unexpected EOF");
1295 if (tok->type == '(') {
1298 isl_token_free(tok);
1299 pwqp = read_term(s, map, v);
1302 if (isl_stream_eat(s, ')'))
1304 pow = optional_power(s);
1305 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1306 } else if (tok->type == ISL_TOKEN_VALUE) {
1307 struct isl_token *tok2;
1308 tok2 = isl_stream_next_token(s);
1309 isl_qpolynomial *qp;
1310 if (tok2 && tok2->type == '/') {
1311 isl_token_free(tok2);
1312 tok2 = next_token(s);
1313 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1314 isl_stream_error(s, tok2, "expected denominator");
1315 isl_token_free(tok);
1316 isl_token_free(tok2);
1319 qp = isl_qpolynomial_rat_cst(isl_map_get_dim(map),
1320 tok->u.v, tok2->u.v);
1321 isl_token_free(tok2);
1323 isl_stream_push_token(s, tok2);
1324 qp = isl_qpolynomial_cst(isl_map_get_dim(map),
1327 isl_token_free(tok);
1328 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1329 } else if (tok->type == ISL_TOKEN_INFTY) {
1330 isl_qpolynomial *qp;
1331 isl_token_free(tok);
1332 qp = isl_qpolynomial_infty(isl_map_get_dim(map));
1333 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1334 } else if (tok->type == ISL_TOKEN_NAN) {
1335 isl_qpolynomial *qp;
1336 isl_token_free(tok);
1337 qp = isl_qpolynomial_nan(isl_map_get_dim(map));
1338 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1339 } else if (tok->type == ISL_TOKEN_IDENT) {
1341 int pos = vars_pos(v, tok->u.s, -1);
1343 isl_qpolynomial *qp;
1345 isl_token_free(tok);
1349 vars_drop(v, v->n - n);
1350 isl_stream_error(s, tok, "unknown identifier");
1351 isl_token_free(tok);
1354 isl_token_free(tok);
1355 pow = optional_power(s);
1356 qp = isl_qpolynomial_var_pow(isl_map_get_dim(map), pos, pow);
1357 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1358 } else if (tok->type == '[') {
1362 isl_stream_push_token(s, tok);
1363 pwaff = accept_affine(s, isl_map_get_dim(map), v);
1364 pow = optional_power(s);
1365 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1366 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1367 } else if (tok->type == '-') {
1368 isl_token_free(tok);
1369 pwqp = read_factor(s, map, v);
1370 pwqp = isl_pw_qpolynomial_neg(pwqp);
1372 isl_stream_error(s, tok, "unexpected isl_token");
1373 isl_stream_push_token(s, tok);
1377 if (isl_stream_eat_if_available(s, '*') ||
1378 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1379 isl_pw_qpolynomial *pwqp2;
1381 pwqp2 = read_factor(s, map, v);
1382 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1387 isl_pw_qpolynomial_free(pwqp);
1391 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1392 __isl_keep isl_map *map, struct vars *v)
1394 struct isl_token *tok;
1395 isl_pw_qpolynomial *pwqp;
1397 pwqp = read_factor(s, map, v);
1400 tok = next_token(s);
1404 if (tok->type == '+') {
1405 isl_pw_qpolynomial *pwqp2;
1407 isl_token_free(tok);
1408 pwqp2 = read_factor(s, map, v);
1409 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1410 } else if (tok->type == '-') {
1411 isl_pw_qpolynomial *pwqp2;
1413 isl_token_free(tok);
1414 pwqp2 = read_factor(s, map, v);
1415 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1416 } else if (tok->type == ISL_TOKEN_VALUE &&
1417 isl_int_is_neg(tok->u.v)) {
1418 isl_pw_qpolynomial *pwqp2;
1420 isl_stream_push_token(s, tok);
1421 pwqp2 = read_factor(s, map, v);
1422 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1424 isl_stream_push_token(s, tok);
1432 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1433 __isl_take isl_map *map, struct vars *v)
1435 struct isl_token *tok;
1437 tok = isl_stream_next_token(s);
1439 isl_stream_error(s, NULL, "unexpected EOF");
1442 if (tok->type == ':' ||
1443 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1444 isl_token_free(tok);
1445 map = read_disjuncts(s, v, map);
1447 isl_stream_push_token(s, tok);
1455 static struct isl_obj obj_read_poly(struct isl_stream *s,
1456 __isl_take isl_map *map, struct vars *v, int n)
1458 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1459 isl_pw_qpolynomial *pwqp;
1460 struct isl_set *set;
1462 pwqp = read_term(s, map, v);
1463 map = read_optional_disjuncts(s, map, v);
1464 set = isl_map_range(map);
1466 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1468 vars_drop(v, v->n - n);
1474 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1475 __isl_take isl_map *map, struct vars *v, int n)
1477 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1478 isl_pw_qpolynomial *pwqp;
1479 isl_pw_qpolynomial_fold *pwf = NULL;
1482 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1483 return obj_read_poly(s, map, v, n);
1485 if (isl_stream_eat(s, '('))
1488 pwqp = read_term(s, map, v);
1489 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1491 while (isl_stream_eat_if_available(s, ',')) {
1492 isl_pw_qpolynomial_fold *pwf_i;
1493 pwqp = read_term(s, map, v);
1494 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1496 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1499 if (isl_stream_eat(s, ')'))
1502 map = read_optional_disjuncts(s, map, v);
1503 set = isl_map_range(map);
1504 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1506 vars_drop(v, v->n - n);
1512 isl_pw_qpolynomial_fold_free(pwf);
1513 obj.type = isl_obj_none;
1517 static int is_rational(struct isl_stream *s)
1519 struct isl_token *tok;
1521 tok = isl_stream_next_token(s);
1524 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1525 isl_token_free(tok);
1526 isl_stream_eat(s, ':');
1530 isl_stream_push_token(s, tok);
1535 static struct isl_obj obj_read_body(struct isl_stream *s,
1536 __isl_take isl_map *map, struct vars *v)
1538 struct isl_token *tok;
1539 struct isl_obj obj = { isl_obj_set, NULL };
1543 map = isl_map_set_rational(map);
1545 if (!next_is_tuple(s))
1546 return obj_read_poly_or_fold(s, map, v, n);
1548 map = read_tuple(s, map, isl_dim_in, v);
1551 tok = isl_stream_next_token(s);
1552 if (tok && tok->type == ISL_TOKEN_TO) {
1553 obj.type = isl_obj_map;
1554 isl_token_free(tok);
1555 if (!next_is_tuple(s)) {
1556 map = isl_map_reverse(map);
1557 return obj_read_poly_or_fold(s, map, v, n);
1559 map = read_tuple(s, map, isl_dim_out, v);
1563 map = isl_map_reverse(map);
1565 isl_stream_push_token(s, tok);
1568 map = read_optional_disjuncts(s, map, v);
1570 vars_drop(v, v->n - n);
1576 obj.type = isl_obj_none;
1580 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1582 if (obj.type == isl_obj_map) {
1583 obj.v = isl_union_map_from_map(obj.v);
1584 obj.type = isl_obj_union_map;
1585 } else if (obj.type == isl_obj_set) {
1586 obj.v = isl_union_set_from_set(obj.v);
1587 obj.type = isl_obj_union_set;
1588 } else if (obj.type == isl_obj_pw_qpolynomial) {
1589 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1590 obj.type = isl_obj_union_pw_qpolynomial;
1591 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1592 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1593 obj.type = isl_obj_union_pw_qpolynomial_fold;
1595 isl_assert(ctx, 0, goto error);
1598 obj.type->free(obj.v);
1599 obj.type = isl_obj_none;
1603 static struct isl_obj obj_add(struct isl_ctx *ctx,
1604 struct isl_obj obj1, struct isl_obj obj2)
1606 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1607 obj1 = to_union(ctx, obj1);
1608 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1609 obj2 = to_union(ctx, obj2);
1610 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1611 obj1 = to_union(ctx, obj1);
1612 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1613 obj2 = to_union(ctx, obj2);
1614 if (obj1.type == isl_obj_pw_qpolynomial &&
1615 obj2.type == isl_obj_union_pw_qpolynomial)
1616 obj1 = to_union(ctx, obj1);
1617 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1618 obj2.type == isl_obj_pw_qpolynomial)
1619 obj2 = to_union(ctx, obj2);
1620 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1621 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1622 obj1 = to_union(ctx, obj1);
1623 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1624 obj2.type == isl_obj_pw_qpolynomial_fold)
1625 obj2 = to_union(ctx, obj2);
1626 isl_assert(ctx, obj1.type == obj2.type, goto error);
1627 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1628 obj1 = to_union(ctx, obj1);
1629 obj2 = to_union(ctx, obj2);
1631 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1632 obj1 = to_union(ctx, obj1);
1633 obj2 = to_union(ctx, obj2);
1635 if (obj1.type == isl_obj_pw_qpolynomial &&
1636 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1637 obj1 = to_union(ctx, obj1);
1638 obj2 = to_union(ctx, obj2);
1640 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1641 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1642 obj1 = to_union(ctx, obj1);
1643 obj2 = to_union(ctx, obj2);
1645 obj1.v = obj1.type->add(obj1.v, obj2.v);
1648 obj1.type->free(obj1.v);
1649 obj2.type->free(obj2.v);
1650 obj1.type = isl_obj_none;
1655 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1657 isl_map *map = NULL;
1658 struct isl_token *tok;
1659 struct vars *v = NULL;
1660 struct isl_obj obj = { isl_obj_set, NULL };
1662 tok = next_token(s);
1664 isl_stream_error(s, NULL, "unexpected EOF");
1667 if (tok->type == ISL_TOKEN_VALUE) {
1668 struct isl_token *tok2;
1669 struct isl_map *map;
1671 tok2 = isl_stream_next_token(s);
1672 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1673 isl_int_is_neg(tok2->u.v)) {
1675 isl_stream_push_token(s, tok2);
1676 obj.type = isl_obj_int;
1677 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1678 isl_token_free(tok);
1681 isl_stream_push_token(s, tok2);
1682 isl_stream_push_token(s, tok);
1683 map = map_read_polylib(s, nparam);
1686 if (isl_map_dim(map, isl_dim_in) > 0)
1687 obj.type = isl_obj_map;
1691 v = vars_new(s->ctx);
1693 isl_stream_push_token(s, tok);
1696 map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
1697 if (tok->type == '[') {
1698 isl_stream_push_token(s, tok);
1699 map = read_tuple(s, map, isl_dim_param, v);
1703 isl_assert(s->ctx, nparam == v->n, goto error);
1704 tok = isl_stream_next_token(s);
1705 if (!tok || tok->type != ISL_TOKEN_TO) {
1706 isl_stream_error(s, tok, "expecting '->'");
1708 isl_stream_push_token(s, tok);
1711 isl_token_free(tok);
1712 tok = isl_stream_next_token(s);
1713 } else if (nparam > 0)
1714 map = isl_map_add_dims(map, isl_dim_param, nparam);
1715 if (!tok || tok->type != '{') {
1716 isl_stream_error(s, tok, "expecting '{'");
1718 isl_stream_push_token(s, tok);
1721 isl_token_free(tok);
1723 tok = isl_stream_next_token(s);
1726 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1727 isl_token_free(tok);
1728 if (isl_stream_eat(s, '='))
1730 map = read_tuple(s, map, isl_dim_param, v);
1734 isl_assert(s->ctx, nparam == v->n, goto error);
1735 } else if (tok->type == '}') {
1736 obj.type = isl_obj_union_set;
1737 obj.v = isl_union_set_empty(isl_map_get_dim(map));
1738 isl_token_free(tok);
1741 isl_stream_push_token(s, tok);
1746 o = obj_read_body(s, isl_map_copy(map), v);
1747 if (o.type == isl_obj_none || !o.v)
1752 obj = obj_add(s->ctx, obj, o);
1753 if (obj.type == isl_obj_none || !obj.v)
1756 tok = isl_stream_next_token(s);
1757 if (!tok || tok->type != ';')
1759 isl_token_free(tok);
1760 if (isl_stream_next_token_is(s, '}')) {
1761 tok = isl_stream_next_token(s);
1766 if (tok && tok->type == '}') {
1767 isl_token_free(tok);
1769 isl_stream_error(s, tok, "unexpected isl_token");
1771 isl_token_free(tok);
1781 obj.type->free(obj.v);
1788 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1790 return obj_read(s, -1);
1793 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1797 obj = obj_read(s, nparam);
1799 isl_assert(s->ctx, obj.type == isl_obj_map ||
1800 obj.type == isl_obj_set, goto error);
1804 obj.type->free(obj.v);
1808 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1812 obj = obj_read(s, nparam);
1814 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1818 obj.type->free(obj.v);
1822 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1826 obj = obj_read(s, -1);
1827 if (obj.type == isl_obj_map) {
1828 obj.type = isl_obj_union_map;
1829 obj.v = isl_union_map_from_map(obj.v);
1831 if (obj.type == isl_obj_set) {
1832 obj.type = isl_obj_union_set;
1833 obj.v = isl_union_set_from_set(obj.v);
1836 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1837 obj.type == isl_obj_union_set, goto error);
1841 obj.type->free(obj.v);
1845 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
1849 obj = obj_read(s, -1);
1850 if (obj.type == isl_obj_set) {
1851 obj.type = isl_obj_union_set;
1852 obj.v = isl_union_set_from_set(obj.v);
1855 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1859 obj.type->free(obj.v);
1863 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1866 struct isl_map *map;
1867 struct isl_basic_map *bmap;
1869 obj = obj_read(s, nparam);
1874 isl_assert(map->ctx, map->n <= 1, goto error);
1877 bmap = isl_basic_map_empty_like_map(map);
1879 bmap = isl_basic_map_copy(map->p[0]);
1889 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1890 FILE *input, int nparam)
1892 struct isl_basic_map *bmap;
1893 struct isl_stream *s = isl_stream_new_file(ctx, input);
1896 bmap = basic_map_read(s, nparam);
1901 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1902 FILE *input, int nparam)
1904 struct isl_basic_map *bmap;
1905 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1908 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1909 return (struct isl_basic_set *)bmap;
1911 isl_basic_map_free(bmap);
1915 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1916 const char *str, int nparam)
1918 struct isl_basic_map *bmap;
1919 struct isl_stream *s = isl_stream_new_str(ctx, str);
1922 bmap = basic_map_read(s, nparam);
1927 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1928 const char *str, int nparam)
1930 struct isl_basic_map *bmap;
1931 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1934 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1935 return (struct isl_basic_set *)bmap;
1937 isl_basic_map_free(bmap);
1941 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1942 FILE *input, int nparam)
1944 struct isl_map *map;
1945 struct isl_stream *s = isl_stream_new_file(ctx, input);
1948 map = isl_stream_read_map(s, nparam);
1953 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1954 const char *str, int nparam)
1956 struct isl_map *map;
1957 struct isl_stream *s = isl_stream_new_str(ctx, str);
1960 map = isl_stream_read_map(s, nparam);
1965 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1966 FILE *input, int nparam)
1968 struct isl_map *map;
1969 map = isl_map_read_from_file(ctx, input, nparam);
1972 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1973 return (struct isl_set *)map;
1979 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1980 const char *str, int nparam)
1982 struct isl_map *map;
1983 map = isl_map_read_from_str(ctx, str, nparam);
1986 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1987 return (struct isl_set *)map;
1993 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
1996 isl_union_map *umap;
1997 struct isl_stream *s = isl_stream_new_file(ctx, input);
2000 umap = isl_stream_read_union_map(s);
2005 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2008 isl_union_map *umap;
2009 struct isl_stream *s = isl_stream_new_str(ctx, str);
2012 umap = isl_stream_read_union_map(s);
2017 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2020 isl_union_set *uset;
2021 struct isl_stream *s = isl_stream_new_file(ctx, input);
2024 uset = isl_stream_read_union_set(s);
2029 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2032 isl_union_set *uset;
2033 struct isl_stream *s = isl_stream_new_str(ctx, str);
2036 uset = isl_stream_read_union_set(s);
2041 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2043 struct isl_vec *vec = NULL;
2044 struct isl_token *tok;
2048 tok = isl_stream_next_token(s);
2049 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2050 isl_stream_error(s, tok, "expecting vector length");
2054 size = isl_int_get_si(tok->u.v);
2055 isl_token_free(tok);
2057 vec = isl_vec_alloc(s->ctx, size);
2059 for (j = 0; j < size; ++j) {
2060 tok = isl_stream_next_token(s);
2061 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2062 isl_stream_error(s, tok, "expecting constant value");
2065 isl_int_set(vec->el[j], tok->u.v);
2066 isl_token_free(tok);
2071 isl_token_free(tok);
2076 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2078 return isl_vec_read_polylib(s);
2081 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2084 struct isl_stream *s = isl_stream_new_file(ctx, input);
2092 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2093 struct isl_stream *s)
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);
2120 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2123 isl_pw_qpolynomial *pwqp;
2124 struct isl_stream *s = isl_stream_new_file(ctx, input);
2127 pwqp = isl_stream_read_pw_qpolynomial(s);