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));
829 else if (type == ISL_TOKEN_NE)
830 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
831 isl_pw_aff_list_copy(right));
833 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
834 isl_pw_aff_list_copy(right));
836 return isl_set_intersect(set, cond);
839 static int is_comparator(struct isl_token *tok)
857 static __isl_give isl_map *add_constraint(struct isl_stream *s,
858 struct vars *v, __isl_take isl_map *map)
860 struct isl_token *tok = NULL;
861 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
864 set = isl_map_wrap(map);
865 list1 = accept_affine_list(s, isl_set_get_dim(set), v);
868 tok = isl_stream_next_token(s);
869 if (!is_comparator(tok)) {
870 isl_stream_error(s, tok, "missing operator");
872 isl_stream_push_token(s, tok);
877 list2 = accept_affine_list(s, isl_set_get_dim(set), v);
881 set = construct_constraints(set, tok->type, list1, list2);
883 isl_pw_aff_list_free(list1);
886 tok = isl_stream_next_token(s);
887 if (!is_comparator(tok)) {
889 isl_stream_push_token(s, tok);
893 isl_pw_aff_list_free(list1);
895 return isl_set_unwrap(set);
899 isl_pw_aff_list_free(list1);
900 isl_pw_aff_list_free(list2);
905 static struct isl_map *read_disjuncts(struct isl_stream *s,
906 struct vars *v, __isl_take isl_map *map);
908 static __isl_give isl_map *read_exists(struct isl_stream *s,
909 struct vars *v, __isl_take isl_map *map)
912 int seen_paren = isl_stream_eat_if_available(s, '(');
914 map = isl_map_from_domain(isl_map_wrap(map));
915 map = read_defined_var_list(s, v, map);
917 if (isl_stream_eat(s, ':'))
920 map = read_disjuncts(s, v, map);
921 map = isl_set_unwrap(isl_map_domain(map));
923 vars_drop(v, v->n - n);
924 if (seen_paren && isl_stream_eat(s, ')'))
933 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
934 struct vars *v, __isl_take isl_map *map)
936 if (isl_stream_eat_if_available(s, '(')) {
937 map = read_disjuncts(s, v, map);
938 if (isl_stream_eat(s, ')'))
943 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
944 return read_exists(s, v, map);
946 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
949 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
950 isl_dim *dim = isl_map_get_dim(map);
952 return isl_map_empty(dim);
955 return add_constraint(s, v, map);
961 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
962 struct vars *v, __isl_take isl_map *map)
967 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
968 res = read_conjunct(s, v, isl_map_copy(map));
970 res = isl_map_subtract(isl_map_copy(map), res);
972 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
975 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
976 res_i = read_conjunct(s, v, isl_map_copy(map));
978 res = isl_map_subtract(res, res_i);
980 res = isl_map_intersect(res, res_i);
987 static struct isl_map *read_disjuncts(struct isl_stream *s,
988 struct vars *v, __isl_take isl_map *map)
992 if (isl_stream_next_token_is(s, '}')) {
993 isl_dim *dim = isl_map_get_dim(map);
995 return isl_map_universe(dim);
998 res = read_conjuncts(s, v, isl_map_copy(map));
999 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1002 res_i = read_conjuncts(s, v, isl_map_copy(map));
1003 res = isl_map_union(res, res_i);
1010 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1012 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1013 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1014 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1015 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1017 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1018 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1019 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1021 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1022 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1023 isl_basic_map_dim(bmap, isl_dim_in) +
1024 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1025 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1027 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1033 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1034 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1037 struct isl_token *tok;
1047 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1048 dim = isl_basic_map_dim(bmap, isl_dim_out);
1050 tok = isl_stream_next_token(s);
1051 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1052 isl_stream_error(s, tok, "expecting coefficient");
1054 isl_stream_push_token(s, tok);
1057 if (!tok->on_new_line) {
1058 isl_stream_error(s, tok, "coefficient should appear on new line");
1059 isl_stream_push_token(s, tok);
1063 type = isl_int_get_si(tok->u.v);
1064 isl_token_free(tok);
1066 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1068 k = isl_basic_map_alloc_equality(bmap);
1071 k = isl_basic_map_alloc_inequality(bmap);
1077 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1079 tok = isl_stream_next_token(s);
1080 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1081 isl_stream_error(s, tok, "expecting coefficient");
1083 isl_stream_push_token(s, tok);
1086 if (tok->on_new_line) {
1087 isl_stream_error(s, tok,
1088 "coefficient should not appear on new line");
1089 isl_stream_push_token(s, tok);
1092 pos = polylib_pos_to_isl_pos(bmap, j);
1093 isl_int_set(c[pos], tok->u.v);
1094 isl_token_free(tok);
1099 isl_basic_map_free(bmap);
1103 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1107 struct isl_token *tok;
1108 struct isl_token *tok2;
1111 unsigned in = 0, out, local = 0;
1112 struct isl_basic_map *bmap = NULL;
1117 tok = isl_stream_next_token(s);
1119 isl_stream_error(s, NULL, "unexpected EOF");
1122 tok2 = isl_stream_next_token(s);
1124 isl_token_free(tok);
1125 isl_stream_error(s, NULL, "unexpected EOF");
1128 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1129 isl_stream_push_token(s, tok2);
1130 isl_stream_push_token(s, tok);
1131 isl_stream_error(s, NULL,
1132 "expecting constraint matrix dimensions");
1135 n_row = isl_int_get_si(tok->u.v);
1136 n_col = isl_int_get_si(tok2->u.v);
1137 on_new_line = tok2->on_new_line;
1138 isl_token_free(tok2);
1139 isl_token_free(tok);
1140 isl_assert(s->ctx, !on_new_line, return NULL);
1141 isl_assert(s->ctx, n_row >= 0, return NULL);
1142 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1143 tok = isl_stream_next_token_on_same_line(s);
1145 if (tok->type != ISL_TOKEN_VALUE) {
1146 isl_stream_error(s, tok,
1147 "expecting number of output dimensions");
1148 isl_stream_push_token(s, tok);
1151 out = isl_int_get_si(tok->u.v);
1152 isl_token_free(tok);
1154 tok = isl_stream_next_token_on_same_line(s);
1155 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1156 isl_stream_error(s, tok,
1157 "expecting number of input dimensions");
1159 isl_stream_push_token(s, tok);
1162 in = isl_int_get_si(tok->u.v);
1163 isl_token_free(tok);
1165 tok = isl_stream_next_token_on_same_line(s);
1166 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1167 isl_stream_error(s, tok,
1168 "expecting number of existentials");
1170 isl_stream_push_token(s, tok);
1173 local = isl_int_get_si(tok->u.v);
1174 isl_token_free(tok);
1176 tok = isl_stream_next_token_on_same_line(s);
1177 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1178 isl_stream_error(s, tok,
1179 "expecting number of parameters");
1181 isl_stream_push_token(s, tok);
1184 nparam = isl_int_get_si(tok->u.v);
1185 isl_token_free(tok);
1186 if (n_col != 1 + out + in + local + nparam + 1) {
1187 isl_stream_error(s, NULL,
1188 "dimensions don't match");
1192 out = n_col - 2 - nparam;
1193 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1197 for (i = 0; i < local; ++i) {
1198 int k = isl_basic_map_alloc_div(bmap);
1201 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1204 for (i = 0; i < n_row; ++i)
1205 bmap = basic_map_read_polylib_constraint(s, bmap);
1207 tok = isl_stream_next_token_on_same_line(s);
1209 isl_stream_error(s, tok, "unexpected extra token on line");
1210 isl_stream_push_token(s, tok);
1214 bmap = isl_basic_map_simplify(bmap);
1215 bmap = isl_basic_map_finalize(bmap);
1218 isl_basic_map_free(bmap);
1222 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1224 struct isl_token *tok;
1225 struct isl_token *tok2;
1227 struct isl_map *map;
1229 tok = isl_stream_next_token(s);
1231 isl_stream_error(s, NULL, "unexpected EOF");
1234 tok2 = isl_stream_next_token_on_same_line(s);
1235 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1236 isl_stream_push_token(s, tok2);
1237 isl_stream_push_token(s, tok);
1238 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1241 isl_stream_error(s, tok2, "unexpected token");
1242 isl_stream_push_token(s, tok2);
1243 isl_stream_push_token(s, tok);
1246 n = isl_int_get_si(tok->u.v);
1247 isl_token_free(tok);
1249 isl_assert(s->ctx, n >= 1, return NULL);
1251 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1253 for (i = 1; map && i < n; ++i)
1254 map = isl_map_union(map,
1255 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1260 static int optional_power(struct isl_stream *s)
1263 struct isl_token *tok;
1265 tok = isl_stream_next_token(s);
1268 if (tok->type != '^') {
1269 isl_stream_push_token(s, tok);
1272 isl_token_free(tok);
1273 tok = isl_stream_next_token(s);
1274 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1275 isl_stream_error(s, tok, "expecting exponent");
1277 isl_stream_push_token(s, tok);
1280 pow = isl_int_get_si(tok->u.v);
1281 isl_token_free(tok);
1285 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1286 __isl_keep isl_map *map, struct vars *v);
1288 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1289 __isl_keep isl_map *map, struct vars *v)
1291 isl_pw_qpolynomial *pwqp;
1292 struct isl_token *tok;
1294 tok = next_token(s);
1296 isl_stream_error(s, NULL, "unexpected EOF");
1299 if (tok->type == '(') {
1302 isl_token_free(tok);
1303 pwqp = read_term(s, map, v);
1306 if (isl_stream_eat(s, ')'))
1308 pow = optional_power(s);
1309 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1310 } else if (tok->type == ISL_TOKEN_VALUE) {
1311 struct isl_token *tok2;
1312 tok2 = isl_stream_next_token(s);
1313 isl_qpolynomial *qp;
1314 if (tok2 && tok2->type == '/') {
1315 isl_token_free(tok2);
1316 tok2 = next_token(s);
1317 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1318 isl_stream_error(s, tok2, "expected denominator");
1319 isl_token_free(tok);
1320 isl_token_free(tok2);
1323 qp = isl_qpolynomial_rat_cst(isl_map_get_dim(map),
1324 tok->u.v, tok2->u.v);
1325 isl_token_free(tok2);
1327 isl_stream_push_token(s, tok2);
1328 qp = isl_qpolynomial_cst(isl_map_get_dim(map),
1331 isl_token_free(tok);
1332 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1333 } else if (tok->type == ISL_TOKEN_INFTY) {
1334 isl_qpolynomial *qp;
1335 isl_token_free(tok);
1336 qp = isl_qpolynomial_infty(isl_map_get_dim(map));
1337 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1338 } else if (tok->type == ISL_TOKEN_NAN) {
1339 isl_qpolynomial *qp;
1340 isl_token_free(tok);
1341 qp = isl_qpolynomial_nan(isl_map_get_dim(map));
1342 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1343 } else if (tok->type == ISL_TOKEN_IDENT) {
1345 int pos = vars_pos(v, tok->u.s, -1);
1347 isl_qpolynomial *qp;
1349 isl_token_free(tok);
1353 vars_drop(v, v->n - n);
1354 isl_stream_error(s, tok, "unknown identifier");
1355 isl_token_free(tok);
1358 isl_token_free(tok);
1359 pow = optional_power(s);
1360 qp = isl_qpolynomial_var_pow(isl_map_get_dim(map), pos, pow);
1361 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1362 } else if (tok->type == '[') {
1366 isl_stream_push_token(s, tok);
1367 pwaff = accept_affine(s, isl_map_get_dim(map), v);
1368 pow = optional_power(s);
1369 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1370 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1371 } else if (tok->type == '-') {
1372 isl_token_free(tok);
1373 pwqp = read_factor(s, map, v);
1374 pwqp = isl_pw_qpolynomial_neg(pwqp);
1376 isl_stream_error(s, tok, "unexpected isl_token");
1377 isl_stream_push_token(s, tok);
1381 if (isl_stream_eat_if_available(s, '*') ||
1382 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1383 isl_pw_qpolynomial *pwqp2;
1385 pwqp2 = read_factor(s, map, v);
1386 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1391 isl_pw_qpolynomial_free(pwqp);
1395 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1396 __isl_keep isl_map *map, struct vars *v)
1398 struct isl_token *tok;
1399 isl_pw_qpolynomial *pwqp;
1401 pwqp = read_factor(s, map, v);
1404 tok = next_token(s);
1408 if (tok->type == '+') {
1409 isl_pw_qpolynomial *pwqp2;
1411 isl_token_free(tok);
1412 pwqp2 = read_factor(s, map, v);
1413 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1414 } else if (tok->type == '-') {
1415 isl_pw_qpolynomial *pwqp2;
1417 isl_token_free(tok);
1418 pwqp2 = read_factor(s, map, v);
1419 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1420 } else if (tok->type == ISL_TOKEN_VALUE &&
1421 isl_int_is_neg(tok->u.v)) {
1422 isl_pw_qpolynomial *pwqp2;
1424 isl_stream_push_token(s, tok);
1425 pwqp2 = read_factor(s, map, v);
1426 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1428 isl_stream_push_token(s, tok);
1436 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1437 __isl_take isl_map *map, struct vars *v)
1439 struct isl_token *tok;
1441 tok = isl_stream_next_token(s);
1443 isl_stream_error(s, NULL, "unexpected EOF");
1446 if (tok->type == ':' ||
1447 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1448 isl_token_free(tok);
1449 map = read_disjuncts(s, v, map);
1451 isl_stream_push_token(s, tok);
1459 static struct isl_obj obj_read_poly(struct isl_stream *s,
1460 __isl_take isl_map *map, struct vars *v, int n)
1462 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1463 isl_pw_qpolynomial *pwqp;
1464 struct isl_set *set;
1466 pwqp = read_term(s, map, v);
1467 map = read_optional_disjuncts(s, map, v);
1468 set = isl_map_range(map);
1470 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1472 vars_drop(v, v->n - n);
1478 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1479 __isl_take isl_map *map, struct vars *v, int n)
1481 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1482 isl_pw_qpolynomial *pwqp;
1483 isl_pw_qpolynomial_fold *pwf = NULL;
1486 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1487 return obj_read_poly(s, map, v, n);
1489 if (isl_stream_eat(s, '('))
1492 pwqp = read_term(s, map, v);
1493 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1495 while (isl_stream_eat_if_available(s, ',')) {
1496 isl_pw_qpolynomial_fold *pwf_i;
1497 pwqp = read_term(s, map, v);
1498 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1500 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1503 if (isl_stream_eat(s, ')'))
1506 map = read_optional_disjuncts(s, map, v);
1507 set = isl_map_range(map);
1508 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1510 vars_drop(v, v->n - n);
1516 isl_pw_qpolynomial_fold_free(pwf);
1517 obj.type = isl_obj_none;
1521 static int is_rational(struct isl_stream *s)
1523 struct isl_token *tok;
1525 tok = isl_stream_next_token(s);
1528 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1529 isl_token_free(tok);
1530 isl_stream_eat(s, ':');
1534 isl_stream_push_token(s, tok);
1539 static struct isl_obj obj_read_body(struct isl_stream *s,
1540 __isl_take isl_map *map, struct vars *v)
1542 struct isl_token *tok;
1543 struct isl_obj obj = { isl_obj_set, NULL };
1547 map = isl_map_set_rational(map);
1549 if (!next_is_tuple(s))
1550 return obj_read_poly_or_fold(s, map, v, n);
1552 map = read_tuple(s, map, isl_dim_in, v);
1555 tok = isl_stream_next_token(s);
1556 if (tok && tok->type == ISL_TOKEN_TO) {
1557 obj.type = isl_obj_map;
1558 isl_token_free(tok);
1559 if (!next_is_tuple(s)) {
1560 map = isl_map_reverse(map);
1561 return obj_read_poly_or_fold(s, map, v, n);
1563 map = read_tuple(s, map, isl_dim_out, v);
1567 map = isl_map_reverse(map);
1569 isl_stream_push_token(s, tok);
1572 map = read_optional_disjuncts(s, map, v);
1574 vars_drop(v, v->n - n);
1580 obj.type = isl_obj_none;
1584 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1586 if (obj.type == isl_obj_map) {
1587 obj.v = isl_union_map_from_map(obj.v);
1588 obj.type = isl_obj_union_map;
1589 } else if (obj.type == isl_obj_set) {
1590 obj.v = isl_union_set_from_set(obj.v);
1591 obj.type = isl_obj_union_set;
1592 } else if (obj.type == isl_obj_pw_qpolynomial) {
1593 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1594 obj.type = isl_obj_union_pw_qpolynomial;
1595 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1596 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1597 obj.type = isl_obj_union_pw_qpolynomial_fold;
1599 isl_assert(ctx, 0, goto error);
1602 obj.type->free(obj.v);
1603 obj.type = isl_obj_none;
1607 static struct isl_obj obj_add(struct isl_ctx *ctx,
1608 struct isl_obj obj1, struct isl_obj obj2)
1610 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1611 obj1 = to_union(ctx, obj1);
1612 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1613 obj2 = to_union(ctx, obj2);
1614 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1615 obj1 = to_union(ctx, obj1);
1616 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1617 obj2 = to_union(ctx, obj2);
1618 if (obj1.type == isl_obj_pw_qpolynomial &&
1619 obj2.type == isl_obj_union_pw_qpolynomial)
1620 obj1 = to_union(ctx, obj1);
1621 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1622 obj2.type == isl_obj_pw_qpolynomial)
1623 obj2 = to_union(ctx, obj2);
1624 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1625 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1626 obj1 = to_union(ctx, obj1);
1627 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1628 obj2.type == isl_obj_pw_qpolynomial_fold)
1629 obj2 = to_union(ctx, obj2);
1630 isl_assert(ctx, obj1.type == obj2.type, goto error);
1631 if (obj1.type == isl_obj_map && !isl_map_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_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1636 obj1 = to_union(ctx, obj1);
1637 obj2 = to_union(ctx, obj2);
1639 if (obj1.type == isl_obj_pw_qpolynomial &&
1640 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1641 obj1 = to_union(ctx, obj1);
1642 obj2 = to_union(ctx, obj2);
1644 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1645 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1646 obj1 = to_union(ctx, obj1);
1647 obj2 = to_union(ctx, obj2);
1649 obj1.v = obj1.type->add(obj1.v, obj2.v);
1652 obj1.type->free(obj1.v);
1653 obj2.type->free(obj2.v);
1654 obj1.type = isl_obj_none;
1659 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1661 isl_map *map = NULL;
1662 struct isl_token *tok;
1663 struct vars *v = NULL;
1664 struct isl_obj obj = { isl_obj_set, NULL };
1666 tok = next_token(s);
1668 isl_stream_error(s, NULL, "unexpected EOF");
1671 if (tok->type == ISL_TOKEN_VALUE) {
1672 struct isl_token *tok2;
1673 struct isl_map *map;
1675 tok2 = isl_stream_next_token(s);
1676 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1677 isl_int_is_neg(tok2->u.v)) {
1679 isl_stream_push_token(s, tok2);
1680 obj.type = isl_obj_int;
1681 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1682 isl_token_free(tok);
1685 isl_stream_push_token(s, tok2);
1686 isl_stream_push_token(s, tok);
1687 map = map_read_polylib(s, nparam);
1690 if (isl_map_dim(map, isl_dim_in) > 0)
1691 obj.type = isl_obj_map;
1695 v = vars_new(s->ctx);
1697 isl_stream_push_token(s, tok);
1700 map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
1701 if (tok->type == '[') {
1702 isl_stream_push_token(s, tok);
1703 map = read_tuple(s, map, isl_dim_param, v);
1707 isl_assert(s->ctx, nparam == v->n, goto error);
1708 tok = isl_stream_next_token(s);
1709 if (!tok || tok->type != ISL_TOKEN_TO) {
1710 isl_stream_error(s, tok, "expecting '->'");
1712 isl_stream_push_token(s, tok);
1715 isl_token_free(tok);
1716 tok = isl_stream_next_token(s);
1717 } else if (nparam > 0)
1718 map = isl_map_add_dims(map, isl_dim_param, nparam);
1719 if (!tok || tok->type != '{') {
1720 isl_stream_error(s, tok, "expecting '{'");
1722 isl_stream_push_token(s, tok);
1725 isl_token_free(tok);
1727 tok = isl_stream_next_token(s);
1730 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1731 isl_token_free(tok);
1732 if (isl_stream_eat(s, '='))
1734 map = read_tuple(s, map, isl_dim_param, v);
1738 isl_assert(s->ctx, nparam == v->n, goto error);
1739 } else if (tok->type == '}') {
1740 obj.type = isl_obj_union_set;
1741 obj.v = isl_union_set_empty(isl_map_get_dim(map));
1742 isl_token_free(tok);
1745 isl_stream_push_token(s, tok);
1750 o = obj_read_body(s, isl_map_copy(map), v);
1751 if (o.type == isl_obj_none || !o.v)
1756 obj = obj_add(s->ctx, obj, o);
1757 if (obj.type == isl_obj_none || !obj.v)
1760 tok = isl_stream_next_token(s);
1761 if (!tok || tok->type != ';')
1763 isl_token_free(tok);
1764 if (isl_stream_next_token_is(s, '}')) {
1765 tok = isl_stream_next_token(s);
1770 if (tok && tok->type == '}') {
1771 isl_token_free(tok);
1773 isl_stream_error(s, tok, "unexpected isl_token");
1775 isl_token_free(tok);
1785 obj.type->free(obj.v);
1792 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1794 return obj_read(s, -1);
1797 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1801 obj = obj_read(s, nparam);
1803 isl_assert(s->ctx, obj.type == isl_obj_map ||
1804 obj.type == isl_obj_set, goto error);
1808 obj.type->free(obj.v);
1812 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1816 obj = obj_read(s, nparam);
1818 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1822 obj.type->free(obj.v);
1826 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1830 obj = obj_read(s, -1);
1831 if (obj.type == isl_obj_map) {
1832 obj.type = isl_obj_union_map;
1833 obj.v = isl_union_map_from_map(obj.v);
1835 if (obj.type == isl_obj_set) {
1836 obj.type = isl_obj_union_set;
1837 obj.v = isl_union_set_from_set(obj.v);
1840 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1841 obj.type == isl_obj_union_set, goto error);
1845 obj.type->free(obj.v);
1849 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
1853 obj = obj_read(s, -1);
1854 if (obj.type == isl_obj_set) {
1855 obj.type = isl_obj_union_set;
1856 obj.v = isl_union_set_from_set(obj.v);
1859 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1863 obj.type->free(obj.v);
1867 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1870 struct isl_map *map;
1871 struct isl_basic_map *bmap;
1873 obj = obj_read(s, nparam);
1878 isl_assert(map->ctx, map->n <= 1, goto error);
1881 bmap = isl_basic_map_empty_like_map(map);
1883 bmap = isl_basic_map_copy(map->p[0]);
1893 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1894 FILE *input, int nparam)
1896 struct isl_basic_map *bmap;
1897 struct isl_stream *s = isl_stream_new_file(ctx, input);
1900 bmap = basic_map_read(s, nparam);
1905 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1906 FILE *input, int nparam)
1908 struct isl_basic_map *bmap;
1909 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1912 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1913 return (struct isl_basic_set *)bmap;
1915 isl_basic_map_free(bmap);
1919 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1920 const char *str, int nparam)
1922 struct isl_basic_map *bmap;
1923 struct isl_stream *s = isl_stream_new_str(ctx, str);
1926 bmap = basic_map_read(s, nparam);
1931 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1932 const char *str, int nparam)
1934 struct isl_basic_map *bmap;
1935 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1938 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1939 return (struct isl_basic_set *)bmap;
1941 isl_basic_map_free(bmap);
1945 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1946 FILE *input, int nparam)
1948 struct isl_map *map;
1949 struct isl_stream *s = isl_stream_new_file(ctx, input);
1952 map = isl_stream_read_map(s, nparam);
1957 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1958 const char *str, int nparam)
1960 struct isl_map *map;
1961 struct isl_stream *s = isl_stream_new_str(ctx, str);
1964 map = isl_stream_read_map(s, nparam);
1969 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1970 FILE *input, int nparam)
1972 struct isl_map *map;
1973 map = isl_map_read_from_file(ctx, input, nparam);
1976 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1977 return (struct isl_set *)map;
1983 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1984 const char *str, int nparam)
1986 struct isl_map *map;
1987 map = isl_map_read_from_str(ctx, str, nparam);
1990 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1991 return (struct isl_set *)map;
1997 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2000 isl_union_map *umap;
2001 struct isl_stream *s = isl_stream_new_file(ctx, input);
2004 umap = isl_stream_read_union_map(s);
2009 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2012 isl_union_map *umap;
2013 struct isl_stream *s = isl_stream_new_str(ctx, str);
2016 umap = isl_stream_read_union_map(s);
2021 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2024 isl_union_set *uset;
2025 struct isl_stream *s = isl_stream_new_file(ctx, input);
2028 uset = isl_stream_read_union_set(s);
2033 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2036 isl_union_set *uset;
2037 struct isl_stream *s = isl_stream_new_str(ctx, str);
2040 uset = isl_stream_read_union_set(s);
2045 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2047 struct isl_vec *vec = NULL;
2048 struct isl_token *tok;
2052 tok = isl_stream_next_token(s);
2053 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2054 isl_stream_error(s, tok, "expecting vector length");
2058 size = isl_int_get_si(tok->u.v);
2059 isl_token_free(tok);
2061 vec = isl_vec_alloc(s->ctx, size);
2063 for (j = 0; j < size; ++j) {
2064 tok = isl_stream_next_token(s);
2065 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2066 isl_stream_error(s, tok, "expecting constant value");
2069 isl_int_set(vec->el[j], tok->u.v);
2070 isl_token_free(tok);
2075 isl_token_free(tok);
2080 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2082 return isl_vec_read_polylib(s);
2085 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2088 struct isl_stream *s = isl_stream_new_file(ctx, input);
2096 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2097 struct isl_stream *s)
2101 obj = obj_read(s, -1);
2103 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2108 obj.type->free(obj.v);
2112 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2115 isl_pw_qpolynomial *pwqp;
2116 struct isl_stream *s = isl_stream_new_str(ctx, str);
2119 pwqp = isl_stream_read_pw_qpolynomial(s);
2124 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2127 isl_pw_qpolynomial *pwqp;
2128 struct isl_stream *s = isl_stream_new_file(ctx, input);
2131 pwqp = isl_stream_read_pw_qpolynomial(s);