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_private.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");
362 if (tok->type == ISL_TOKEN_AFF) {
363 res = isl_pw_aff_copy(tok->u.pwaff);
365 } else if (tok->type == ISL_TOKEN_IDENT) {
367 int pos = vars_pos(v, tok->u.s, -1);
373 isl_stream_error(s, tok, "unknown identifier");
377 aff = isl_aff_zero(isl_local_space_from_dim(isl_dim_copy(dim)));
380 isl_int_set_si(aff->v->el[2 + pos], 1);
381 res = isl_pw_aff_from_aff(aff);
383 } else if (tok->type == ISL_TOKEN_VALUE) {
384 if (isl_stream_eat_if_available(s, '*')) {
385 res = accept_affine_factor(s, isl_dim_copy(dim), v);
386 res = isl_pw_aff_scale(res, tok->u.v);
390 ls = isl_local_space_from_dim(isl_dim_copy(dim));
391 aff = isl_aff_zero(ls);
392 aff = isl_aff_add_constant(aff, tok->u.v);
393 res = isl_pw_aff_from_aff(aff);
396 } else if (tok->type == '(') {
399 res = accept_affine(s, isl_dim_copy(dim), v);
402 if (isl_stream_eat(s, ')'))
404 } else if (tok->type == '[' ||
405 tok->type == ISL_TOKEN_FLOORD ||
406 tok->type == ISL_TOKEN_CEILD) {
407 isl_stream_push_token(s, tok);
409 res = accept_div(s, isl_dim_copy(dim), v);
410 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
411 isl_stream_push_token(s, tok);
413 res = accept_minmax(s, isl_dim_copy(dim), v);
415 isl_stream_error(s, tok, "expecting factor");
418 if (isl_stream_eat_if_available(s, '%') ||
419 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
421 return affine_mod(s, v, res);
423 if (isl_stream_eat_if_available(s, '*')) {
426 isl_int_set_si(f, 1);
427 if (accept_cst_factor(s, &f) < 0) {
431 res = isl_pw_aff_scale(res, f);
440 isl_pw_aff_free(res);
445 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
449 aff = isl_aff_zero(isl_local_space_from_dim(isl_pw_aff_get_dim(pwaff)));
450 aff = isl_aff_add_constant(aff, v);
452 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
455 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
456 __isl_take isl_dim *dim, struct vars *v)
458 struct isl_token *tok = NULL;
463 ls = isl_local_space_from_dim(isl_dim_copy(dim));
464 res = isl_pw_aff_from_aff(isl_aff_zero(ls));
471 isl_stream_error(s, NULL, "unexpected EOF");
474 if (tok->type == '-') {
479 if (tok->type == '(' || tok->type == '[' ||
480 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
481 tok->type == ISL_TOKEN_FLOORD ||
482 tok->type == ISL_TOKEN_CEILD ||
483 tok->type == ISL_TOKEN_IDENT ||
484 tok->type == ISL_TOKEN_AFF) {
486 isl_stream_push_token(s, tok);
488 term = accept_affine_factor(s, isl_dim_copy(dim), v);
490 res = isl_pw_aff_sub(res, term);
492 res = isl_pw_aff_add(res, term);
496 } else if (tok->type == ISL_TOKEN_VALUE) {
498 isl_int_neg(tok->u.v, tok->u.v);
499 if (isl_stream_eat_if_available(s, '*') ||
500 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
502 term = accept_affine_factor(s,
503 isl_dim_copy(dim), v);
504 term = isl_pw_aff_scale(term, tok->u.v);
505 res = isl_pw_aff_add(res, term);
509 res = add_cst(res, tok->u.v);
513 isl_stream_error(s, tok, "unexpected isl_token");
514 isl_stream_push_token(s, tok);
515 isl_pw_aff_free(res);
522 if (tok && tok->type == '-') {
525 } else if (tok && tok->type == '+') {
528 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
529 isl_int_is_neg(tok->u.v)) {
530 isl_stream_push_token(s, tok);
533 isl_stream_push_token(s, tok);
543 isl_pw_aff_free(res);
547 static int is_comparator(struct isl_token *tok)
565 static struct isl_map *read_disjuncts(struct isl_stream *s,
566 struct vars *v, __isl_take isl_map *map);
567 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
568 __isl_take isl_dim *dim, struct vars *v);
570 /* Accept a ternary operator, given the first argument.
572 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
573 __isl_take isl_map *cond, struct vars *v)
576 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL;
581 if (isl_stream_eat(s, '?'))
584 dim = isl_dim_wrap(isl_map_get_dim(cond));
585 pwaff1 = accept_extended_affine(s, dim, v);
589 if (isl_stream_eat(s, ':'))
592 pwaff2 = accept_extended_affine(s, isl_pw_aff_get_dim(pwaff1), v);
596 return isl_pw_aff_cond(isl_map_wrap(cond), pwaff1, pwaff2);
599 isl_pw_aff_free(pwaff1);
600 isl_pw_aff_free(pwaff2);
604 /* Accept an affine expression that may involve ternary operators.
605 * We first read an affine expression.
606 * If it is not followed by a comparison operator, we simply return it.
607 * Otherwise, we assume the affine epxression is part of the first
608 * argument of a ternary operator and try to parse that.
610 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
611 __isl_take isl_dim *dim, struct vars *v)
615 struct isl_token *tok;
616 int line = -1, col = -1;
619 tok = isl_stream_next_token(s);
623 isl_stream_push_token(s, tok);
626 pwaff = accept_affine(s, dim, v);
630 tok = isl_stream_next_token(s);
632 return isl_pw_aff_free(pwaff);
634 is_comp = is_comparator(tok);
635 isl_stream_push_token(s, tok);
639 tok = isl_token_new(s->ctx, line, col, 0);
641 return isl_pw_aff_free(pwaff);
642 tok->type = ISL_TOKEN_AFF;
643 tok->u.pwaff = pwaff;
645 cond = isl_map_universe(isl_dim_unwrap(isl_pw_aff_get_dim(pwaff)));
647 isl_stream_push_token(s, tok);
649 cond = read_disjuncts(s, v, cond);
651 return accept_ternary(s, cond, v);
654 static __isl_give isl_map *read_var_def(struct isl_stream *s,
655 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
661 pos = isl_map_dim(map, isl_dim_in);
662 if (type == isl_dim_out)
663 pos += isl_map_dim(map, isl_dim_out);
666 def = accept_extended_affine(s, isl_dim_wrap(isl_map_get_dim(map)), v);
667 def_map = isl_map_from_pw_aff(def);
668 def_map = isl_map_equate(def_map, isl_dim_in, pos, isl_dim_out, 0);
669 def_map = isl_set_unwrap(isl_map_domain(def_map));
671 map = isl_map_intersect(map, def_map);
676 static __isl_give isl_map *read_var_list(struct isl_stream *s,
677 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
680 struct isl_token *tok;
682 if (isl_stream_next_token_is(s, ']'))
685 while ((tok = next_token(s)) != NULL) {
688 if (tok->type == ISL_TOKEN_IDENT) {
690 int p = vars_pos(v, tok->u.s, -1);
697 map = isl_map_add_dims(map, type, 1);
698 map = set_name(map, type, i, v->v->name);
701 if (type == isl_dim_param) {
702 isl_stream_error(s, tok,
703 "expecting unique identifier");
706 isl_stream_push_token(s, tok);
708 if (vars_add_anon(v) < 0)
710 map = isl_map_add_dims(map, type, 1);
711 map = read_var_def(s, map, type, v);
714 tok = isl_stream_next_token(s);
715 if (tok && tok->type == ']' &&
716 isl_stream_next_token_is(s, '[')) {
718 tok = isl_stream_next_token(s);
719 } else if (!tok || tok->type != ',')
726 isl_stream_push_token(s, tok);
735 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
736 __isl_take isl_dim *dim, struct vars *v)
739 isl_pw_aff_list *list;
740 struct isl_token *tok = NULL;
742 pwaff = accept_affine(s, isl_dim_copy(dim), v);
743 list = isl_pw_aff_list_from_pw_aff(pwaff);
748 tok = isl_stream_next_token(s);
750 isl_stream_error(s, NULL, "unexpected EOF");
753 if (tok->type != ',') {
754 isl_stream_push_token(s, tok);
759 pwaff = accept_affine(s, isl_dim_copy(dim), v);
760 list = isl_pw_aff_list_concat(list,
761 isl_pw_aff_list_from_pw_aff(pwaff));
770 isl_pw_aff_list_free(list);
774 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
775 struct vars *v, __isl_take isl_map *map)
777 struct isl_token *tok;
779 while ((tok = isl_stream_next_token(s)) != NULL) {
783 if (tok->type != ISL_TOKEN_IDENT)
786 p = vars_pos(v, tok->u.s, -1);
790 isl_stream_error(s, tok, "expecting unique identifier");
794 map = isl_map_add_dims(map, isl_dim_out, 1);
797 tok = isl_stream_next_token(s);
798 if (tok && tok->type == '=') {
800 map = read_var_def(s, map, isl_dim_out, v);
801 tok = isl_stream_next_token(s);
804 if (!tok || tok->type != ',')
810 isl_stream_push_token(s, tok);
819 static int next_is_tuple(struct isl_stream *s)
821 struct isl_token *tok;
824 tok = isl_stream_next_token(s);
827 if (tok->type == '[') {
828 isl_stream_push_token(s, tok);
831 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
832 isl_stream_push_token(s, tok);
836 is_tuple = isl_stream_next_token_is(s, '[');
838 isl_stream_push_token(s, tok);
843 static __isl_give isl_map *read_tuple(struct isl_stream *s,
844 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
846 static __isl_give isl_map *read_nested_tuple(struct isl_stream *s,
847 __isl_take isl_map *map, struct vars *v)
849 map = read_tuple(s, map, isl_dim_in, v);
850 if (isl_stream_eat(s, ISL_TOKEN_TO))
852 map = read_tuple(s, map, isl_dim_out, v);
853 map = isl_map_from_range(isl_map_wrap(map));
860 static __isl_give isl_map *read_tuple(struct isl_stream *s,
861 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
863 struct isl_token *tok;
866 tok = isl_stream_next_token(s);
867 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
868 name = strdup(tok->u.s);
872 tok = isl_stream_next_token(s);
874 if (!tok || tok->type != '[') {
875 isl_stream_error(s, tok, "expecting '['");
879 if (type != isl_dim_param && next_is_tuple(s)) {
880 isl_dim *dim = isl_map_get_dim(map);
881 int nparam = isl_dim_size(dim, isl_dim_param);
882 int n_in = isl_dim_size(dim, isl_dim_in);
884 if (type == isl_dim_out)
885 dim = isl_dim_move(dim, isl_dim_param, nparam,
886 isl_dim_in, 0, n_in);
887 nested = isl_map_universe(dim);
888 nested = read_nested_tuple(s, nested, v);
889 if (type == isl_dim_in) {
890 nested = isl_map_reverse(nested);
891 map = isl_map_intersect(nested, map);
894 dim = isl_dim_range(isl_map_get_dim(nested));
895 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
896 dim = isl_dim_join(isl_map_get_dim(map), dim);
897 set = isl_map_domain(map);
898 nested = isl_map_reset_dim(nested, dim);
899 map = isl_map_intersect_domain(nested, set);
902 map = read_var_list(s, map, type, v);
903 tok = isl_stream_next_token(s);
904 if (!tok || tok->type != ']') {
905 isl_stream_error(s, tok, "expecting ']'");
911 map = isl_map_set_tuple_name(map, type, name);
923 static __isl_give isl_set *construct_constraints(
924 __isl_take isl_set *set, enum isl_token_type type,
925 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
929 if (type == ISL_TOKEN_LE)
930 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
931 isl_pw_aff_list_copy(right));
932 else if (type == ISL_TOKEN_GE)
933 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
934 isl_pw_aff_list_copy(right));
935 else if (type == ISL_TOKEN_LT)
936 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
937 isl_pw_aff_list_copy(right));
938 else if (type == ISL_TOKEN_GT)
939 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
940 isl_pw_aff_list_copy(right));
941 else if (type == ISL_TOKEN_NE)
942 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
943 isl_pw_aff_list_copy(right));
945 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
946 isl_pw_aff_list_copy(right));
948 return isl_set_intersect(set, cond);
951 static __isl_give isl_map *add_constraint(struct isl_stream *s,
952 struct vars *v, __isl_take isl_map *map)
954 struct isl_token *tok = NULL;
955 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
958 set = isl_map_wrap(map);
959 list1 = accept_affine_list(s, isl_set_get_dim(set), v);
962 tok = isl_stream_next_token(s);
963 if (!is_comparator(tok)) {
964 isl_stream_error(s, tok, "missing operator");
966 isl_stream_push_token(s, tok);
971 list2 = accept_affine_list(s, isl_set_get_dim(set), v);
975 set = construct_constraints(set, tok->type, list1, list2);
977 isl_pw_aff_list_free(list1);
980 tok = isl_stream_next_token(s);
981 if (!is_comparator(tok)) {
983 isl_stream_push_token(s, tok);
987 isl_pw_aff_list_free(list1);
989 return isl_set_unwrap(set);
993 isl_pw_aff_list_free(list1);
994 isl_pw_aff_list_free(list2);
999 static __isl_give isl_map *read_exists(struct isl_stream *s,
1000 struct vars *v, __isl_take isl_map *map)
1003 int seen_paren = isl_stream_eat_if_available(s, '(');
1005 map = isl_map_from_domain(isl_map_wrap(map));
1006 map = read_defined_var_list(s, v, map);
1008 if (isl_stream_eat(s, ':'))
1011 map = read_disjuncts(s, v, map);
1012 map = isl_set_unwrap(isl_map_domain(map));
1014 vars_drop(v, v->n - n);
1015 if (seen_paren && isl_stream_eat(s, ')'))
1024 /* Parse an expression between parentheses and push the result
1025 * back on the stream.
1027 * The parsed expression may be either an affine expression
1028 * or a condition. The first type is pushed onto the stream
1029 * as an isl_pw_aff, while the second is pushed as an isl_map.
1031 * If the initial token indicates the start of a condition,
1032 * we parse it as such.
1033 * Otherwise, we first parse an affine expression and push
1034 * that onto the stream. If the affine expression covers the
1035 * entire expression between parentheses, we return.
1036 * Otherwise, we assume that the affine expression is the
1037 * start of a condition and continue parsing.
1039 static int resolve_paren_expr(struct isl_stream *s,
1040 struct vars *v, __isl_take isl_map *map)
1042 struct isl_token *tok, *tok2;
1046 tok = isl_stream_next_token(s);
1047 if (!tok || tok->type != '(')
1050 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1051 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1052 isl_stream_next_token_is(s, ISL_TOKEN_FALSE)) {
1053 map = read_disjuncts(s, v, map);
1054 if (isl_stream_eat(s, ')'))
1056 tok->type = ISL_TOKEN_MAP;
1058 isl_stream_push_token(s, tok);
1062 tok2 = isl_stream_next_token(s);
1067 isl_stream_push_token(s, tok2);
1069 pwaff = accept_affine(s, isl_dim_wrap(isl_map_get_dim(map)), v);
1073 tok2 = isl_token_new(s->ctx, line, col, 0);
1076 tok2->type = ISL_TOKEN_AFF;
1077 tok2->u.pwaff = pwaff;
1079 if (isl_stream_eat_if_available(s, ')')) {
1080 isl_stream_push_token(s, tok2);
1081 isl_token_free(tok);
1086 isl_stream_push_token(s, tok2);
1088 map = read_disjuncts(s, v, map);
1089 if (isl_stream_eat(s, ')'))
1092 tok->type = ISL_TOKEN_MAP;
1094 isl_stream_push_token(s, tok);
1098 isl_pw_aff_free(pwaff);
1100 isl_token_free(tok);
1105 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1106 struct vars *v, __isl_take isl_map *map)
1108 if (isl_stream_next_token_is(s, '('))
1109 if (resolve_paren_expr(s, v, isl_map_copy(map)))
1112 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1113 struct isl_token *tok;
1114 tok = isl_stream_next_token(s);
1118 map = isl_map_copy(tok->u.map);
1119 isl_token_free(tok);
1123 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1124 return read_exists(s, v, map);
1126 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1129 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1130 isl_dim *dim = isl_map_get_dim(map);
1132 return isl_map_empty(dim);
1135 return add_constraint(s, v, map);
1141 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1142 struct vars *v, __isl_take isl_map *map)
1147 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1148 res = read_conjunct(s, v, isl_map_copy(map));
1150 res = isl_map_subtract(isl_map_copy(map), res);
1152 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1155 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1156 res_i = read_conjunct(s, v, isl_map_copy(map));
1158 res = isl_map_subtract(res, res_i);
1160 res = isl_map_intersect(res, res_i);
1167 static struct isl_map *read_disjuncts(struct isl_stream *s,
1168 struct vars *v, __isl_take isl_map *map)
1172 if (isl_stream_next_token_is(s, '}')) {
1173 isl_dim *dim = isl_map_get_dim(map);
1175 return isl_map_universe(dim);
1178 res = read_conjuncts(s, v, isl_map_copy(map));
1179 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1182 res_i = read_conjuncts(s, v, isl_map_copy(map));
1183 res = isl_map_union(res, res_i);
1190 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1192 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1193 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1194 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1195 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1197 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1198 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1199 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1201 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1202 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1203 isl_basic_map_dim(bmap, isl_dim_in) +
1204 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1205 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1207 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1213 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1214 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1217 struct isl_token *tok;
1227 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1228 dim = isl_basic_map_dim(bmap, isl_dim_out);
1230 tok = isl_stream_next_token(s);
1231 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1232 isl_stream_error(s, tok, "expecting coefficient");
1234 isl_stream_push_token(s, tok);
1237 if (!tok->on_new_line) {
1238 isl_stream_error(s, tok, "coefficient should appear on new line");
1239 isl_stream_push_token(s, tok);
1243 type = isl_int_get_si(tok->u.v);
1244 isl_token_free(tok);
1246 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1248 k = isl_basic_map_alloc_equality(bmap);
1251 k = isl_basic_map_alloc_inequality(bmap);
1257 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1259 tok = isl_stream_next_token(s);
1260 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1261 isl_stream_error(s, tok, "expecting coefficient");
1263 isl_stream_push_token(s, tok);
1266 if (tok->on_new_line) {
1267 isl_stream_error(s, tok,
1268 "coefficient should not appear on new line");
1269 isl_stream_push_token(s, tok);
1272 pos = polylib_pos_to_isl_pos(bmap, j);
1273 isl_int_set(c[pos], tok->u.v);
1274 isl_token_free(tok);
1279 isl_basic_map_free(bmap);
1283 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1287 struct isl_token *tok;
1288 struct isl_token *tok2;
1291 unsigned in = 0, out, local = 0;
1292 struct isl_basic_map *bmap = NULL;
1297 tok = isl_stream_next_token(s);
1299 isl_stream_error(s, NULL, "unexpected EOF");
1302 tok2 = isl_stream_next_token(s);
1304 isl_token_free(tok);
1305 isl_stream_error(s, NULL, "unexpected EOF");
1308 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1309 isl_stream_push_token(s, tok2);
1310 isl_stream_push_token(s, tok);
1311 isl_stream_error(s, NULL,
1312 "expecting constraint matrix dimensions");
1315 n_row = isl_int_get_si(tok->u.v);
1316 n_col = isl_int_get_si(tok2->u.v);
1317 on_new_line = tok2->on_new_line;
1318 isl_token_free(tok2);
1319 isl_token_free(tok);
1320 isl_assert(s->ctx, !on_new_line, return NULL);
1321 isl_assert(s->ctx, n_row >= 0, return NULL);
1322 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1323 tok = isl_stream_next_token_on_same_line(s);
1325 if (tok->type != ISL_TOKEN_VALUE) {
1326 isl_stream_error(s, tok,
1327 "expecting number of output dimensions");
1328 isl_stream_push_token(s, tok);
1331 out = isl_int_get_si(tok->u.v);
1332 isl_token_free(tok);
1334 tok = isl_stream_next_token_on_same_line(s);
1335 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1336 isl_stream_error(s, tok,
1337 "expecting number of input dimensions");
1339 isl_stream_push_token(s, tok);
1342 in = isl_int_get_si(tok->u.v);
1343 isl_token_free(tok);
1345 tok = isl_stream_next_token_on_same_line(s);
1346 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1347 isl_stream_error(s, tok,
1348 "expecting number of existentials");
1350 isl_stream_push_token(s, tok);
1353 local = isl_int_get_si(tok->u.v);
1354 isl_token_free(tok);
1356 tok = isl_stream_next_token_on_same_line(s);
1357 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1358 isl_stream_error(s, tok,
1359 "expecting number of parameters");
1361 isl_stream_push_token(s, tok);
1364 nparam = isl_int_get_si(tok->u.v);
1365 isl_token_free(tok);
1366 if (n_col != 1 + out + in + local + nparam + 1) {
1367 isl_stream_error(s, NULL,
1368 "dimensions don't match");
1372 out = n_col - 2 - nparam;
1373 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1377 for (i = 0; i < local; ++i) {
1378 int k = isl_basic_map_alloc_div(bmap);
1381 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1384 for (i = 0; i < n_row; ++i)
1385 bmap = basic_map_read_polylib_constraint(s, bmap);
1387 tok = isl_stream_next_token_on_same_line(s);
1389 isl_stream_error(s, tok, "unexpected extra token on line");
1390 isl_stream_push_token(s, tok);
1394 bmap = isl_basic_map_simplify(bmap);
1395 bmap = isl_basic_map_finalize(bmap);
1398 isl_basic_map_free(bmap);
1402 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1404 struct isl_token *tok;
1405 struct isl_token *tok2;
1407 struct isl_map *map;
1409 tok = isl_stream_next_token(s);
1411 isl_stream_error(s, NULL, "unexpected EOF");
1414 tok2 = isl_stream_next_token_on_same_line(s);
1415 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1416 isl_stream_push_token(s, tok2);
1417 isl_stream_push_token(s, tok);
1418 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1421 isl_stream_error(s, tok2, "unexpected token");
1422 isl_stream_push_token(s, tok2);
1423 isl_stream_push_token(s, tok);
1426 n = isl_int_get_si(tok->u.v);
1427 isl_token_free(tok);
1429 isl_assert(s->ctx, n >= 1, return NULL);
1431 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1433 for (i = 1; map && i < n; ++i)
1434 map = isl_map_union(map,
1435 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1440 static int optional_power(struct isl_stream *s)
1443 struct isl_token *tok;
1445 tok = isl_stream_next_token(s);
1448 if (tok->type != '^') {
1449 isl_stream_push_token(s, tok);
1452 isl_token_free(tok);
1453 tok = isl_stream_next_token(s);
1454 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1455 isl_stream_error(s, tok, "expecting exponent");
1457 isl_stream_push_token(s, tok);
1460 pow = isl_int_get_si(tok->u.v);
1461 isl_token_free(tok);
1465 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1466 __isl_keep isl_map *map, struct vars *v);
1468 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1469 __isl_keep isl_map *map, struct vars *v)
1471 isl_pw_qpolynomial *pwqp;
1472 struct isl_token *tok;
1474 tok = next_token(s);
1476 isl_stream_error(s, NULL, "unexpected EOF");
1479 if (tok->type == '(') {
1482 isl_token_free(tok);
1483 pwqp = read_term(s, map, v);
1486 if (isl_stream_eat(s, ')'))
1488 pow = optional_power(s);
1489 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1490 } else if (tok->type == ISL_TOKEN_VALUE) {
1491 struct isl_token *tok2;
1492 tok2 = isl_stream_next_token(s);
1493 isl_qpolynomial *qp;
1494 if (tok2 && tok2->type == '/') {
1495 isl_token_free(tok2);
1496 tok2 = next_token(s);
1497 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1498 isl_stream_error(s, tok2, "expected denominator");
1499 isl_token_free(tok);
1500 isl_token_free(tok2);
1503 qp = isl_qpolynomial_rat_cst(isl_map_get_dim(map),
1504 tok->u.v, tok2->u.v);
1505 isl_token_free(tok2);
1507 isl_stream_push_token(s, tok2);
1508 qp = isl_qpolynomial_cst(isl_map_get_dim(map),
1511 isl_token_free(tok);
1512 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1513 } else if (tok->type == ISL_TOKEN_INFTY) {
1514 isl_qpolynomial *qp;
1515 isl_token_free(tok);
1516 qp = isl_qpolynomial_infty(isl_map_get_dim(map));
1517 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1518 } else if (tok->type == ISL_TOKEN_NAN) {
1519 isl_qpolynomial *qp;
1520 isl_token_free(tok);
1521 qp = isl_qpolynomial_nan(isl_map_get_dim(map));
1522 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1523 } else if (tok->type == ISL_TOKEN_IDENT) {
1525 int pos = vars_pos(v, tok->u.s, -1);
1527 isl_qpolynomial *qp;
1529 isl_token_free(tok);
1533 vars_drop(v, v->n - n);
1534 isl_stream_error(s, tok, "unknown identifier");
1535 isl_token_free(tok);
1538 isl_token_free(tok);
1539 pow = optional_power(s);
1540 qp = isl_qpolynomial_var_pow(isl_map_get_dim(map), pos, pow);
1541 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1542 } else if (tok->type == '[') {
1546 isl_stream_push_token(s, tok);
1547 pwaff = accept_affine(s, isl_map_get_dim(map), v);
1548 pow = optional_power(s);
1549 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1550 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1551 } else if (tok->type == '-') {
1552 isl_token_free(tok);
1553 pwqp = read_factor(s, map, v);
1554 pwqp = isl_pw_qpolynomial_neg(pwqp);
1556 isl_stream_error(s, tok, "unexpected isl_token");
1557 isl_stream_push_token(s, tok);
1561 if (isl_stream_eat_if_available(s, '*') ||
1562 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1563 isl_pw_qpolynomial *pwqp2;
1565 pwqp2 = read_factor(s, map, v);
1566 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1571 isl_pw_qpolynomial_free(pwqp);
1575 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1576 __isl_keep isl_map *map, struct vars *v)
1578 struct isl_token *tok;
1579 isl_pw_qpolynomial *pwqp;
1581 pwqp = read_factor(s, map, v);
1584 tok = next_token(s);
1588 if (tok->type == '+') {
1589 isl_pw_qpolynomial *pwqp2;
1591 isl_token_free(tok);
1592 pwqp2 = read_factor(s, map, v);
1593 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1594 } else if (tok->type == '-') {
1595 isl_pw_qpolynomial *pwqp2;
1597 isl_token_free(tok);
1598 pwqp2 = read_factor(s, map, v);
1599 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1600 } else if (tok->type == ISL_TOKEN_VALUE &&
1601 isl_int_is_neg(tok->u.v)) {
1602 isl_pw_qpolynomial *pwqp2;
1604 isl_stream_push_token(s, tok);
1605 pwqp2 = read_factor(s, map, v);
1606 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1608 isl_stream_push_token(s, tok);
1616 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1617 __isl_take isl_map *map, struct vars *v)
1619 struct isl_token *tok;
1621 tok = isl_stream_next_token(s);
1623 isl_stream_error(s, NULL, "unexpected EOF");
1626 if (tok->type == ':' ||
1627 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1628 isl_token_free(tok);
1629 map = read_disjuncts(s, v, map);
1631 isl_stream_push_token(s, tok);
1639 static struct isl_obj obj_read_poly(struct isl_stream *s,
1640 __isl_take isl_map *map, struct vars *v, int n)
1642 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1643 isl_pw_qpolynomial *pwqp;
1644 struct isl_set *set;
1646 pwqp = read_term(s, map, v);
1647 map = read_optional_disjuncts(s, map, v);
1648 set = isl_map_range(map);
1650 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1652 vars_drop(v, v->n - n);
1658 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1659 __isl_take isl_map *map, struct vars *v, int n)
1661 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1662 isl_pw_qpolynomial *pwqp;
1663 isl_pw_qpolynomial_fold *pwf = NULL;
1666 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1667 return obj_read_poly(s, map, v, n);
1669 if (isl_stream_eat(s, '('))
1672 pwqp = read_term(s, map, v);
1673 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1675 while (isl_stream_eat_if_available(s, ',')) {
1676 isl_pw_qpolynomial_fold *pwf_i;
1677 pwqp = read_term(s, map, v);
1678 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1680 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1683 if (isl_stream_eat(s, ')'))
1686 map = read_optional_disjuncts(s, map, v);
1687 set = isl_map_range(map);
1688 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1690 vars_drop(v, v->n - n);
1696 isl_pw_qpolynomial_fold_free(pwf);
1697 obj.type = isl_obj_none;
1701 static int is_rational(struct isl_stream *s)
1703 struct isl_token *tok;
1705 tok = isl_stream_next_token(s);
1708 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1709 isl_token_free(tok);
1710 isl_stream_eat(s, ':');
1714 isl_stream_push_token(s, tok);
1719 static struct isl_obj obj_read_body(struct isl_stream *s,
1720 __isl_take isl_map *map, struct vars *v)
1722 struct isl_token *tok;
1723 struct isl_obj obj = { isl_obj_set, NULL };
1727 map = isl_map_set_rational(map);
1729 if (!next_is_tuple(s))
1730 return obj_read_poly_or_fold(s, map, v, n);
1732 map = read_tuple(s, map, isl_dim_in, v);
1735 tok = isl_stream_next_token(s);
1736 if (tok && tok->type == ISL_TOKEN_TO) {
1737 obj.type = isl_obj_map;
1738 isl_token_free(tok);
1739 if (!next_is_tuple(s)) {
1740 map = isl_map_reverse(map);
1741 return obj_read_poly_or_fold(s, map, v, n);
1743 map = read_tuple(s, map, isl_dim_out, v);
1747 map = isl_map_reverse(map);
1749 isl_stream_push_token(s, tok);
1752 map = read_optional_disjuncts(s, map, v);
1754 vars_drop(v, v->n - n);
1760 obj.type = isl_obj_none;
1764 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1766 if (obj.type == isl_obj_map) {
1767 obj.v = isl_union_map_from_map(obj.v);
1768 obj.type = isl_obj_union_map;
1769 } else if (obj.type == isl_obj_set) {
1770 obj.v = isl_union_set_from_set(obj.v);
1771 obj.type = isl_obj_union_set;
1772 } else if (obj.type == isl_obj_pw_qpolynomial) {
1773 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1774 obj.type = isl_obj_union_pw_qpolynomial;
1775 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1776 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1777 obj.type = isl_obj_union_pw_qpolynomial_fold;
1779 isl_assert(ctx, 0, goto error);
1782 obj.type->free(obj.v);
1783 obj.type = isl_obj_none;
1787 static struct isl_obj obj_add(struct isl_ctx *ctx,
1788 struct isl_obj obj1, struct isl_obj obj2)
1790 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1791 obj1 = to_union(ctx, obj1);
1792 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1793 obj2 = to_union(ctx, obj2);
1794 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1795 obj1 = to_union(ctx, obj1);
1796 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1797 obj2 = to_union(ctx, obj2);
1798 if (obj1.type == isl_obj_pw_qpolynomial &&
1799 obj2.type == isl_obj_union_pw_qpolynomial)
1800 obj1 = to_union(ctx, obj1);
1801 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1802 obj2.type == isl_obj_pw_qpolynomial)
1803 obj2 = to_union(ctx, obj2);
1804 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1805 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1806 obj1 = to_union(ctx, obj1);
1807 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1808 obj2.type == isl_obj_pw_qpolynomial_fold)
1809 obj2 = to_union(ctx, obj2);
1810 isl_assert(ctx, obj1.type == obj2.type, goto error);
1811 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1812 obj1 = to_union(ctx, obj1);
1813 obj2 = to_union(ctx, obj2);
1815 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1816 obj1 = to_union(ctx, obj1);
1817 obj2 = to_union(ctx, obj2);
1819 if (obj1.type == isl_obj_pw_qpolynomial &&
1820 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1821 obj1 = to_union(ctx, obj1);
1822 obj2 = to_union(ctx, obj2);
1824 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1825 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1826 obj1 = to_union(ctx, obj1);
1827 obj2 = to_union(ctx, obj2);
1829 obj1.v = obj1.type->add(obj1.v, obj2.v);
1832 obj1.type->free(obj1.v);
1833 obj2.type->free(obj2.v);
1834 obj1.type = isl_obj_none;
1839 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1841 isl_map *map = NULL;
1842 struct isl_token *tok;
1843 struct vars *v = NULL;
1844 struct isl_obj obj = { isl_obj_set, NULL };
1846 tok = next_token(s);
1848 isl_stream_error(s, NULL, "unexpected EOF");
1851 if (tok->type == ISL_TOKEN_VALUE) {
1852 struct isl_token *tok2;
1853 struct isl_map *map;
1855 tok2 = isl_stream_next_token(s);
1856 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1857 isl_int_is_neg(tok2->u.v)) {
1859 isl_stream_push_token(s, tok2);
1860 obj.type = isl_obj_int;
1861 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1862 isl_token_free(tok);
1865 isl_stream_push_token(s, tok2);
1866 isl_stream_push_token(s, tok);
1867 map = map_read_polylib(s, nparam);
1870 if (isl_map_dim(map, isl_dim_in) > 0)
1871 obj.type = isl_obj_map;
1875 v = vars_new(s->ctx);
1877 isl_stream_push_token(s, tok);
1880 map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
1881 if (tok->type == '[') {
1882 isl_stream_push_token(s, tok);
1883 map = read_tuple(s, map, isl_dim_param, v);
1887 isl_assert(s->ctx, nparam == v->n, goto error);
1888 tok = isl_stream_next_token(s);
1889 if (!tok || tok->type != ISL_TOKEN_TO) {
1890 isl_stream_error(s, tok, "expecting '->'");
1892 isl_stream_push_token(s, tok);
1895 isl_token_free(tok);
1896 tok = isl_stream_next_token(s);
1897 } else if (nparam > 0)
1898 map = isl_map_add_dims(map, isl_dim_param, nparam);
1899 if (!tok || tok->type != '{') {
1900 isl_stream_error(s, tok, "expecting '{'");
1902 isl_stream_push_token(s, tok);
1905 isl_token_free(tok);
1907 tok = isl_stream_next_token(s);
1910 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1911 isl_token_free(tok);
1912 if (isl_stream_eat(s, '='))
1914 map = read_tuple(s, map, isl_dim_param, v);
1918 isl_assert(s->ctx, nparam == v->n, goto error);
1919 } else if (tok->type == '}') {
1920 obj.type = isl_obj_union_set;
1921 obj.v = isl_union_set_empty(isl_map_get_dim(map));
1922 isl_token_free(tok);
1925 isl_stream_push_token(s, tok);
1930 o = obj_read_body(s, isl_map_copy(map), v);
1931 if (o.type == isl_obj_none || !o.v)
1936 obj = obj_add(s->ctx, obj, o);
1937 if (obj.type == isl_obj_none || !obj.v)
1940 tok = isl_stream_next_token(s);
1941 if (!tok || tok->type != ';')
1943 isl_token_free(tok);
1944 if (isl_stream_next_token_is(s, '}')) {
1945 tok = isl_stream_next_token(s);
1950 if (tok && tok->type == '}') {
1951 isl_token_free(tok);
1953 isl_stream_error(s, tok, "unexpected isl_token");
1955 isl_token_free(tok);
1965 obj.type->free(obj.v);
1972 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1974 return obj_read(s, -1);
1977 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1981 obj = obj_read(s, nparam);
1983 isl_assert(s->ctx, obj.type == isl_obj_map ||
1984 obj.type == isl_obj_set, goto error);
1988 obj.type->free(obj.v);
1992 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1996 obj = obj_read(s, nparam);
1998 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2002 obj.type->free(obj.v);
2006 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2010 obj = obj_read(s, -1);
2011 if (obj.type == isl_obj_map) {
2012 obj.type = isl_obj_union_map;
2013 obj.v = isl_union_map_from_map(obj.v);
2015 if (obj.type == isl_obj_set) {
2016 obj.type = isl_obj_union_set;
2017 obj.v = isl_union_set_from_set(obj.v);
2020 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2021 obj.type == isl_obj_union_set, goto error);
2025 obj.type->free(obj.v);
2029 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2033 obj = obj_read(s, -1);
2034 if (obj.type == isl_obj_set) {
2035 obj.type = isl_obj_union_set;
2036 obj.v = isl_union_set_from_set(obj.v);
2039 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2043 obj.type->free(obj.v);
2047 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2050 struct isl_map *map;
2051 struct isl_basic_map *bmap;
2053 obj = obj_read(s, nparam);
2058 isl_assert(map->ctx, map->n <= 1, goto error);
2061 bmap = isl_basic_map_empty_like_map(map);
2063 bmap = isl_basic_map_copy(map->p[0]);
2073 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2074 FILE *input, int nparam)
2076 struct isl_basic_map *bmap;
2077 struct isl_stream *s = isl_stream_new_file(ctx, input);
2080 bmap = basic_map_read(s, nparam);
2085 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2086 FILE *input, int nparam)
2088 struct isl_basic_map *bmap;
2089 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
2092 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2093 return (struct isl_basic_set *)bmap;
2095 isl_basic_map_free(bmap);
2099 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2100 const char *str, int nparam)
2102 struct isl_basic_map *bmap;
2103 struct isl_stream *s = isl_stream_new_str(ctx, str);
2106 bmap = basic_map_read(s, nparam);
2111 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2112 const char *str, int nparam)
2114 struct isl_basic_map *bmap;
2115 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
2118 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2119 return (struct isl_basic_set *)bmap;
2121 isl_basic_map_free(bmap);
2125 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2126 FILE *input, int nparam)
2128 struct isl_map *map;
2129 struct isl_stream *s = isl_stream_new_file(ctx, input);
2132 map = isl_stream_read_map(s, nparam);
2137 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2138 const char *str, int nparam)
2140 struct isl_map *map;
2141 struct isl_stream *s = isl_stream_new_str(ctx, str);
2144 map = isl_stream_read_map(s, nparam);
2149 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2150 FILE *input, int nparam)
2152 struct isl_map *map;
2153 map = isl_map_read_from_file(ctx, input, nparam);
2156 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2157 return (struct isl_set *)map;
2163 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2164 const char *str, int nparam)
2166 struct isl_map *map;
2167 map = isl_map_read_from_str(ctx, str, nparam);
2170 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2171 return (struct isl_set *)map;
2177 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2180 isl_union_map *umap;
2181 struct isl_stream *s = isl_stream_new_file(ctx, input);
2184 umap = isl_stream_read_union_map(s);
2189 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2192 isl_union_map *umap;
2193 struct isl_stream *s = isl_stream_new_str(ctx, str);
2196 umap = isl_stream_read_union_map(s);
2201 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2204 isl_union_set *uset;
2205 struct isl_stream *s = isl_stream_new_file(ctx, input);
2208 uset = isl_stream_read_union_set(s);
2213 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2216 isl_union_set *uset;
2217 struct isl_stream *s = isl_stream_new_str(ctx, str);
2220 uset = isl_stream_read_union_set(s);
2225 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2227 struct isl_vec *vec = NULL;
2228 struct isl_token *tok;
2232 tok = isl_stream_next_token(s);
2233 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2234 isl_stream_error(s, tok, "expecting vector length");
2238 size = isl_int_get_si(tok->u.v);
2239 isl_token_free(tok);
2241 vec = isl_vec_alloc(s->ctx, size);
2243 for (j = 0; j < size; ++j) {
2244 tok = isl_stream_next_token(s);
2245 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2246 isl_stream_error(s, tok, "expecting constant value");
2249 isl_int_set(vec->el[j], tok->u.v);
2250 isl_token_free(tok);
2255 isl_token_free(tok);
2260 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2262 return isl_vec_read_polylib(s);
2265 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2268 struct isl_stream *s = isl_stream_new_file(ctx, input);
2276 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2277 struct isl_stream *s)
2281 obj = obj_read(s, -1);
2283 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2288 obj.type->free(obj.v);
2292 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2295 isl_pw_qpolynomial *pwqp;
2296 struct isl_stream *s = isl_stream_new_str(ctx, str);
2299 pwqp = isl_stream_read_pw_qpolynomial(s);
2304 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2307 isl_pw_qpolynomial *pwqp;
2308 struct isl_stream *s = isl_stream_new_file(ctx, input);
2311 pwqp = isl_stream_read_pw_qpolynomial(s);