2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT 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>
20 #include <isl_stream_private.h>
22 #include "isl_polynomial_private.h"
23 #include <isl/union_map.h>
24 #include <isl_mat_private.h>
25 #include <isl_aff_private.h>
31 struct variable *next;
40 static struct vars *vars_new(struct isl_ctx *ctx)
43 v = isl_alloc_type(ctx, struct vars);
52 static void variable_free(struct variable *var)
55 struct variable *next = var->next;
62 static void vars_free(struct vars *v)
70 static void vars_drop(struct vars *v, int n)
81 struct variable *next = var->next;
89 static struct variable *variable_new(struct vars *v, const char *name, int len,
93 var = isl_calloc_type(v->ctx, struct variable);
96 var->name = strdup(name);
97 var->name[len] = '\0';
106 static int vars_pos(struct vars *v, const char *s, int len)
113 for (q = v->v; q; q = q->next) {
114 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
121 v->v = variable_new(v, s, len, v->n);
129 static int vars_add_anon(struct vars *v)
131 v->v = variable_new(v, "", 0, v->n);
140 static __isl_give isl_map *set_name(__isl_take isl_map *map,
141 enum isl_dim_type type, unsigned pos, char *name)
150 prime = strchr(name, '\'');
153 map = isl_map_set_dim_name(map, type, pos, name);
160 /* Obtain next token, with some preprocessing.
161 * In particular, evaluate expressions of the form x^y,
162 * with x and y values.
164 static struct isl_token *next_token(struct isl_stream *s)
166 struct isl_token *tok, *tok2;
168 tok = isl_stream_next_token(s);
169 if (!tok || tok->type != ISL_TOKEN_VALUE)
171 if (!isl_stream_eat_if_available(s, '^'))
173 tok2 = isl_stream_next_token(s);
174 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
175 isl_stream_error(s, tok2, "expecting constant value");
179 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
181 isl_token_free(tok2);
185 isl_token_free(tok2);
189 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
191 struct isl_token *tok;
194 if (!tok || tok->type != ISL_TOKEN_VALUE) {
195 isl_stream_error(s, tok, "expecting constant value");
199 isl_int_mul(*f, *f, tok->u.v);
203 if (isl_stream_eat_if_available(s, '*'))
204 return accept_cst_factor(s, f);
212 /* Given an affine expression aff, return an affine expression
213 * for aff % d, with d the next token on the stream, which is
214 * assumed to be a constant.
216 * We introduce an integer division q = [aff/d] and the result
217 * is set to aff - d q.
219 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
220 struct vars *v, __isl_take isl_pw_aff *aff)
222 struct isl_token *tok;
226 if (!tok || tok->type != ISL_TOKEN_VALUE) {
227 isl_stream_error(s, tok, "expecting constant value");
231 q = isl_pw_aff_copy(aff);
232 q = isl_pw_aff_scale_down(q, tok->u.v);
233 q = isl_pw_aff_floor(q);
234 q = isl_pw_aff_scale(q, tok->u.v);
236 aff = isl_pw_aff_sub(aff, q);
241 isl_pw_aff_free(aff);
246 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
247 __isl_take isl_space *dim, struct vars *v);
248 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
249 __isl_take isl_space *dim, struct vars *v);
251 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
252 __isl_take isl_space *dim, struct vars *v)
254 struct isl_token *tok;
255 isl_pw_aff_list *list = NULL;
258 tok = isl_stream_next_token(s);
261 min = tok->type == ISL_TOKEN_MIN;
264 if (isl_stream_eat(s, '('))
267 list = accept_affine_list(s, isl_space_copy(dim), v);
271 if (isl_stream_eat(s, ')'))
275 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
278 isl_pw_aff_list_free(list);
282 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
283 __isl_take isl_space *dim, struct vars *v)
285 struct isl_token *tok;
288 isl_pw_aff *pwaff = NULL;
290 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
292 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
295 if (isl_stream_eat(s, '('))
298 if (isl_stream_eat(s, '['))
302 pwaff = accept_affine(s, isl_space_copy(dim), v);
305 if (isl_stream_eat(s, ','))
311 if (tok->type != ISL_TOKEN_VALUE) {
312 isl_stream_error(s, tok, "expected denominator");
313 isl_stream_push_token(s, tok);
316 isl_pw_aff_scale_down(pwaff, tok->u.v);
321 pwaff = isl_pw_aff_ceil(pwaff);
323 pwaff = isl_pw_aff_floor(pwaff);
326 if (isl_stream_eat(s, ')'))
329 if (isl_stream_eat(s, ']'))
337 isl_pw_aff_free(pwaff);
341 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
342 __isl_take isl_space *dim, struct vars *v)
344 struct isl_token *tok = NULL;
345 isl_pw_aff *res = NULL;
349 isl_stream_error(s, NULL, "unexpected EOF");
353 if (tok->type == ISL_TOKEN_AFF) {
354 res = isl_pw_aff_copy(tok->u.pwaff);
356 } else if (tok->type == ISL_TOKEN_IDENT) {
358 int pos = vars_pos(v, tok->u.s, -1);
364 isl_stream_error(s, tok, "unknown identifier");
368 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
371 isl_int_set_si(aff->v->el[2 + pos], 1);
372 res = isl_pw_aff_from_aff(aff);
374 } else if (tok->type == ISL_TOKEN_VALUE) {
375 if (isl_stream_eat_if_available(s, '*')) {
376 res = accept_affine_factor(s, isl_space_copy(dim), v);
377 res = isl_pw_aff_scale(res, tok->u.v);
381 ls = isl_local_space_from_space(isl_space_copy(dim));
382 aff = isl_aff_zero_on_domain(ls);
383 aff = isl_aff_add_constant(aff, tok->u.v);
384 res = isl_pw_aff_from_aff(aff);
387 } else if (tok->type == '(') {
390 res = accept_affine(s, isl_space_copy(dim), v);
393 if (isl_stream_eat(s, ')'))
395 } else if (tok->type == '[' ||
396 tok->type == ISL_TOKEN_FLOORD ||
397 tok->type == ISL_TOKEN_CEILD) {
398 isl_stream_push_token(s, tok);
400 res = accept_div(s, isl_space_copy(dim), v);
401 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
402 isl_stream_push_token(s, tok);
404 res = accept_minmax(s, isl_space_copy(dim), v);
406 isl_stream_error(s, tok, "expecting factor");
409 if (isl_stream_eat_if_available(s, '%') ||
410 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
412 return affine_mod(s, v, res);
414 if (isl_stream_eat_if_available(s, '*')) {
417 isl_int_set_si(f, 1);
418 if (accept_cst_factor(s, &f) < 0) {
422 res = isl_pw_aff_scale(res, f);
425 if (isl_stream_eat_if_available(s, '/')) {
428 isl_int_set_si(f, 1);
429 if (accept_cst_factor(s, &f) < 0) {
433 res = isl_pw_aff_scale_down(res, f);
442 isl_pw_aff_free(res);
447 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
452 space = isl_pw_aff_get_domain_space(pwaff);
453 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
454 aff = isl_aff_add_constant(aff, v);
456 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
459 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
460 __isl_take isl_space *dim, struct vars *v)
462 struct isl_token *tok = NULL;
467 ls = isl_local_space_from_space(isl_space_copy(dim));
468 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
475 isl_stream_error(s, NULL, "unexpected EOF");
478 if (tok->type == '-') {
483 if (tok->type == '(' || tok->type == '[' ||
484 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
485 tok->type == ISL_TOKEN_FLOORD ||
486 tok->type == ISL_TOKEN_CEILD ||
487 tok->type == ISL_TOKEN_IDENT ||
488 tok->type == ISL_TOKEN_AFF) {
490 isl_stream_push_token(s, tok);
492 term = accept_affine_factor(s, isl_space_copy(dim), v);
494 res = isl_pw_aff_sub(res, term);
496 res = isl_pw_aff_add(res, term);
500 } else if (tok->type == ISL_TOKEN_VALUE) {
502 isl_int_neg(tok->u.v, tok->u.v);
503 if (isl_stream_eat_if_available(s, '*') ||
504 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
506 term = accept_affine_factor(s,
507 isl_space_copy(dim), v);
508 term = isl_pw_aff_scale(term, tok->u.v);
509 res = isl_pw_aff_add(res, term);
513 res = add_cst(res, tok->u.v);
517 isl_stream_error(s, tok, "unexpected isl_token");
518 isl_stream_push_token(s, tok);
519 isl_pw_aff_free(res);
526 if (tok && tok->type == '-') {
529 } else if (tok && tok->type == '+') {
532 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
533 isl_int_is_neg(tok->u.v)) {
534 isl_stream_push_token(s, tok);
537 isl_stream_push_token(s, tok);
547 isl_pw_aff_free(res);
551 static int is_comparator(struct isl_token *tok)
569 static struct isl_map *read_disjuncts(struct isl_stream *s,
570 struct vars *v, __isl_take isl_map *map);
571 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
572 __isl_take isl_space *dim, struct vars *v);
574 /* Accept a ternary operator, given the first argument.
576 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
577 __isl_take isl_map *cond, struct vars *v)
580 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
585 if (isl_stream_eat(s, '?'))
588 dim = isl_space_wrap(isl_map_get_space(cond));
589 pwaff1 = accept_extended_affine(s, dim, v);
593 if (isl_stream_eat(s, ':'))
596 dim = isl_pw_aff_get_domain_space(pwaff1);
597 pwaff2 = accept_extended_affine(s, dim, v);
601 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
602 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
605 isl_pw_aff_free(pwaff1);
606 isl_pw_aff_free(pwaff2);
610 /* Accept an affine expression that may involve ternary operators.
611 * We first read an affine expression.
612 * If it is not followed by a comparison operator, we simply return it.
613 * Otherwise, we assume the affine epxression is part of the first
614 * argument of a ternary operator and try to parse that.
616 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
617 __isl_take isl_space *dim, struct vars *v)
622 struct isl_token *tok;
623 int line = -1, col = -1;
626 tok = isl_stream_next_token(s);
630 isl_stream_push_token(s, tok);
633 pwaff = accept_affine(s, dim, v);
637 tok = isl_stream_next_token(s);
639 return isl_pw_aff_free(pwaff);
641 is_comp = is_comparator(tok);
642 isl_stream_push_token(s, tok);
646 tok = isl_token_new(s->ctx, line, col, 0);
648 return isl_pw_aff_free(pwaff);
649 tok->type = ISL_TOKEN_AFF;
650 tok->u.pwaff = pwaff;
652 space = isl_pw_aff_get_domain_space(pwaff);
653 cond = isl_map_universe(isl_space_unwrap(space));
655 isl_stream_push_token(s, tok);
657 cond = read_disjuncts(s, v, cond);
659 return accept_ternary(s, cond, v);
662 static __isl_give isl_map *read_var_def(struct isl_stream *s,
663 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
669 if (type == isl_dim_param)
670 pos = isl_map_dim(map, isl_dim_param);
672 pos = isl_map_dim(map, isl_dim_in);
673 if (type == isl_dim_out)
674 pos += isl_map_dim(map, isl_dim_out);
679 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
680 def_map = isl_map_from_pw_aff(def);
681 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
682 def_map = isl_set_unwrap(isl_map_domain(def_map));
684 map = isl_map_intersect(map, def_map);
689 static __isl_give isl_map *read_var_list(struct isl_stream *s,
690 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
693 struct isl_token *tok;
695 if (isl_stream_next_token_is(s, ']'))
696 return isl_map_add_dims(map, type, 0);
698 while ((tok = next_token(s)) != NULL) {
701 if (tok->type == ISL_TOKEN_IDENT) {
703 int p = vars_pos(v, tok->u.s, -1);
710 map = isl_map_add_dims(map, type, 1);
711 map = set_name(map, type, i, v->v->name);
713 if (isl_stream_eat_if_available(s, '='))
714 map = read_var_def(s, map, type, v);
716 if (type == isl_dim_param) {
717 isl_stream_error(s, tok,
718 "expecting unique identifier");
721 isl_stream_push_token(s, tok);
723 if (vars_add_anon(v) < 0)
725 map = isl_map_add_dims(map, type, 1);
726 map = read_var_def(s, map, type, v);
729 tok = isl_stream_next_token(s);
730 if (tok && tok->type == ']' &&
731 isl_stream_next_token_is(s, '[')) {
733 tok = isl_stream_next_token(s);
734 } else if (!tok || tok->type != ',')
741 isl_stream_push_token(s, tok);
750 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
751 __isl_take isl_space *dim, struct vars *v)
754 isl_pw_aff_list *list;
755 struct isl_token *tok = NULL;
757 pwaff = accept_affine(s, isl_space_copy(dim), v);
758 list = isl_pw_aff_list_from_pw_aff(pwaff);
763 tok = isl_stream_next_token(s);
765 isl_stream_error(s, NULL, "unexpected EOF");
768 if (tok->type != ',') {
769 isl_stream_push_token(s, tok);
774 pwaff = accept_affine(s, isl_space_copy(dim), v);
775 list = isl_pw_aff_list_concat(list,
776 isl_pw_aff_list_from_pw_aff(pwaff));
785 isl_pw_aff_list_free(list);
789 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
790 struct vars *v, __isl_take isl_map *map)
792 struct isl_token *tok;
794 while ((tok = isl_stream_next_token(s)) != NULL) {
798 if (tok->type != ISL_TOKEN_IDENT)
801 p = vars_pos(v, tok->u.s, -1);
805 isl_stream_error(s, tok, "expecting unique identifier");
809 map = isl_map_add_dims(map, isl_dim_out, 1);
812 tok = isl_stream_next_token(s);
813 if (tok && tok->type == '=') {
815 map = read_var_def(s, map, isl_dim_out, v);
816 tok = isl_stream_next_token(s);
819 if (!tok || tok->type != ',')
825 isl_stream_push_token(s, tok);
834 static int next_is_tuple(struct isl_stream *s)
836 struct isl_token *tok;
839 tok = isl_stream_next_token(s);
842 if (tok->type == '[') {
843 isl_stream_push_token(s, tok);
846 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
847 isl_stream_push_token(s, tok);
851 is_tuple = isl_stream_next_token_is(s, '[');
853 isl_stream_push_token(s, tok);
858 static __isl_give isl_map *read_tuple(struct isl_stream *s,
859 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
861 static __isl_give isl_set *read_nested_tuple(struct isl_stream *s,
862 __isl_take isl_map *map, struct vars *v)
864 map = read_tuple(s, map, isl_dim_in, v);
865 if (isl_stream_eat(s, ISL_TOKEN_TO))
867 map = read_tuple(s, map, isl_dim_out, v);
868 return isl_map_wrap(map);
874 static __isl_give isl_map *read_tuple(struct isl_stream *s,
875 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
877 struct isl_token *tok;
880 tok = isl_stream_next_token(s);
881 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
882 name = strdup(tok->u.s);
886 tok = isl_stream_next_token(s);
888 if (!tok || tok->type != '[') {
889 isl_stream_error(s, tok, "expecting '['");
893 if (type != isl_dim_param && next_is_tuple(s)) {
894 isl_space *dim = isl_map_get_space(map);
895 int nparam = isl_space_dim(dim, isl_dim_param);
896 int n_in = isl_space_dim(dim, isl_dim_in);
898 if (type == isl_dim_out) {
899 dim = isl_space_move_dims(dim, isl_dim_param, nparam,
900 isl_dim_in, 0, n_in);
901 dim = isl_space_params(dim);
903 nested = read_nested_tuple(s, isl_map_universe(dim), v);
904 if (type == isl_dim_in) {
905 nested = isl_map_reverse(nested);
906 map = isl_map_intersect_params(nested, map);
909 dim = isl_set_get_space(nested);
910 dim = isl_space_drop_dims(dim, isl_dim_param, nparam, n_in);
911 dim = isl_space_join(isl_map_get_space(map), dim);
912 set = isl_map_domain(map);
913 nested = isl_map_reset_space(nested, dim);
914 map = isl_map_intersect_domain(nested, set);
917 map = read_var_list(s, map, type, v);
918 tok = isl_stream_next_token(s);
919 if (!tok || tok->type != ']') {
920 isl_stream_error(s, tok, "expecting ']'");
926 map = isl_map_set_tuple_name(map, type, name);
938 static __isl_give isl_set *construct_constraints(
939 __isl_take isl_set *set, int type,
940 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
944 if (type == ISL_TOKEN_LE)
945 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
946 isl_pw_aff_list_copy(right));
947 else if (type == ISL_TOKEN_GE)
948 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
949 isl_pw_aff_list_copy(right));
950 else if (type == ISL_TOKEN_LT)
951 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
952 isl_pw_aff_list_copy(right));
953 else if (type == ISL_TOKEN_GT)
954 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
955 isl_pw_aff_list_copy(right));
956 else if (type == ISL_TOKEN_NE)
957 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
958 isl_pw_aff_list_copy(right));
960 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
961 isl_pw_aff_list_copy(right));
963 return isl_set_intersect(set, cond);
966 static __isl_give isl_map *add_constraint(struct isl_stream *s,
967 struct vars *v, __isl_take isl_map *map)
969 struct isl_token *tok = NULL;
970 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
973 set = isl_map_wrap(map);
974 list1 = accept_affine_list(s, isl_set_get_space(set), v);
977 tok = isl_stream_next_token(s);
978 if (!is_comparator(tok)) {
979 isl_stream_error(s, tok, "missing operator");
981 isl_stream_push_token(s, tok);
986 list2 = accept_affine_list(s, isl_set_get_space(set), v);
990 set = construct_constraints(set, tok->type, list1, list2);
992 isl_pw_aff_list_free(list1);
995 tok = isl_stream_next_token(s);
996 if (!is_comparator(tok)) {
998 isl_stream_push_token(s, tok);
1002 isl_pw_aff_list_free(list1);
1004 return isl_set_unwrap(set);
1007 isl_token_free(tok);
1008 isl_pw_aff_list_free(list1);
1009 isl_pw_aff_list_free(list2);
1014 static __isl_give isl_map *read_exists(struct isl_stream *s,
1015 struct vars *v, __isl_take isl_map *map)
1018 int seen_paren = isl_stream_eat_if_available(s, '(');
1020 map = isl_map_from_domain(isl_map_wrap(map));
1021 map = read_defined_var_list(s, v, map);
1023 if (isl_stream_eat(s, ':'))
1026 map = read_disjuncts(s, v, map);
1027 map = isl_set_unwrap(isl_map_domain(map));
1029 vars_drop(v, v->n - n);
1030 if (seen_paren && isl_stream_eat(s, ')'))
1039 /* Parse an expression between parentheses and push the result
1040 * back on the stream.
1042 * The parsed expression may be either an affine expression
1043 * or a condition. The first type is pushed onto the stream
1044 * as an isl_pw_aff, while the second is pushed as an isl_map.
1046 * If the initial token indicates the start of a condition,
1047 * we parse it as such.
1048 * Otherwise, we first parse an affine expression and push
1049 * that onto the stream. If the affine expression covers the
1050 * entire expression between parentheses, we return.
1051 * Otherwise, we assume that the affine expression is the
1052 * start of a condition and continue parsing.
1054 static int resolve_paren_expr(struct isl_stream *s,
1055 struct vars *v, __isl_take isl_map *map)
1057 struct isl_token *tok, *tok2;
1061 tok = isl_stream_next_token(s);
1062 if (!tok || tok->type != '(')
1065 if (isl_stream_next_token_is(s, '('))
1066 if (resolve_paren_expr(s, v, isl_map_copy(map)))
1069 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1070 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1071 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1072 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1073 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1074 map = read_disjuncts(s, v, map);
1075 if (isl_stream_eat(s, ')'))
1077 tok->type = ISL_TOKEN_MAP;
1079 isl_stream_push_token(s, tok);
1083 tok2 = isl_stream_next_token(s);
1088 isl_stream_push_token(s, tok2);
1090 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1094 tok2 = isl_token_new(s->ctx, line, col, 0);
1097 tok2->type = ISL_TOKEN_AFF;
1098 tok2->u.pwaff = pwaff;
1100 if (isl_stream_eat_if_available(s, ')')) {
1101 isl_stream_push_token(s, tok2);
1102 isl_token_free(tok);
1107 isl_stream_push_token(s, tok2);
1109 map = read_disjuncts(s, v, map);
1110 if (isl_stream_eat(s, ')'))
1113 tok->type = ISL_TOKEN_MAP;
1115 isl_stream_push_token(s, tok);
1119 isl_pw_aff_free(pwaff);
1121 isl_token_free(tok);
1126 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1127 struct vars *v, __isl_take isl_map *map)
1129 if (isl_stream_next_token_is(s, '('))
1130 if (resolve_paren_expr(s, v, isl_map_copy(map)))
1133 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1134 struct isl_token *tok;
1135 tok = isl_stream_next_token(s);
1139 map = isl_map_copy(tok->u.map);
1140 isl_token_free(tok);
1144 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1145 return read_exists(s, v, map);
1147 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1150 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1151 isl_space *dim = isl_map_get_space(map);
1153 return isl_map_empty(dim);
1156 return add_constraint(s, v, map);
1162 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1163 struct vars *v, __isl_take isl_map *map)
1168 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1169 res = read_conjunct(s, v, isl_map_copy(map));
1171 res = isl_map_subtract(isl_map_copy(map), res);
1173 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1176 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1177 res_i = read_conjunct(s, v, isl_map_copy(map));
1179 res = isl_map_subtract(res, res_i);
1181 res = isl_map_intersect(res, res_i);
1188 static struct isl_map *read_disjuncts(struct isl_stream *s,
1189 struct vars *v, __isl_take isl_map *map)
1193 if (isl_stream_next_token_is(s, '}')) {
1194 isl_space *dim = isl_map_get_space(map);
1196 return isl_map_universe(dim);
1199 res = read_conjuncts(s, v, isl_map_copy(map));
1200 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1203 res_i = read_conjuncts(s, v, isl_map_copy(map));
1204 res = isl_map_union(res, res_i);
1211 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1213 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1214 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1215 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1216 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1218 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1219 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1220 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1222 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1223 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1224 isl_basic_map_dim(bmap, isl_dim_in) +
1225 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1226 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1228 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1234 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1235 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1238 struct isl_token *tok;
1248 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1249 dim = isl_basic_map_dim(bmap, isl_dim_out);
1251 tok = isl_stream_next_token(s);
1252 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1253 isl_stream_error(s, tok, "expecting coefficient");
1255 isl_stream_push_token(s, tok);
1258 if (!tok->on_new_line) {
1259 isl_stream_error(s, tok, "coefficient should appear on new line");
1260 isl_stream_push_token(s, tok);
1264 type = isl_int_get_si(tok->u.v);
1265 isl_token_free(tok);
1267 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1269 k = isl_basic_map_alloc_equality(bmap);
1272 k = isl_basic_map_alloc_inequality(bmap);
1278 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1280 tok = isl_stream_next_token(s);
1281 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1282 isl_stream_error(s, tok, "expecting coefficient");
1284 isl_stream_push_token(s, tok);
1287 if (tok->on_new_line) {
1288 isl_stream_error(s, tok,
1289 "coefficient should not appear on new line");
1290 isl_stream_push_token(s, tok);
1293 pos = polylib_pos_to_isl_pos(bmap, j);
1294 isl_int_set(c[pos], tok->u.v);
1295 isl_token_free(tok);
1300 isl_basic_map_free(bmap);
1304 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
1307 struct isl_token *tok;
1308 struct isl_token *tok2;
1311 unsigned in = 0, out, local = 0;
1312 struct isl_basic_map *bmap = NULL;
1315 tok = isl_stream_next_token(s);
1317 isl_stream_error(s, NULL, "unexpected EOF");
1320 tok2 = isl_stream_next_token(s);
1322 isl_token_free(tok);
1323 isl_stream_error(s, NULL, "unexpected EOF");
1326 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1327 isl_stream_push_token(s, tok2);
1328 isl_stream_push_token(s, tok);
1329 isl_stream_error(s, NULL,
1330 "expecting constraint matrix dimensions");
1333 n_row = isl_int_get_si(tok->u.v);
1334 n_col = isl_int_get_si(tok2->u.v);
1335 on_new_line = tok2->on_new_line;
1336 isl_token_free(tok2);
1337 isl_token_free(tok);
1338 isl_assert(s->ctx, !on_new_line, return NULL);
1339 isl_assert(s->ctx, n_row >= 0, return NULL);
1340 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1341 tok = isl_stream_next_token_on_same_line(s);
1343 if (tok->type != ISL_TOKEN_VALUE) {
1344 isl_stream_error(s, tok,
1345 "expecting number of output dimensions");
1346 isl_stream_push_token(s, tok);
1349 out = isl_int_get_si(tok->u.v);
1350 isl_token_free(tok);
1352 tok = isl_stream_next_token_on_same_line(s);
1353 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1354 isl_stream_error(s, tok,
1355 "expecting number of input dimensions");
1357 isl_stream_push_token(s, tok);
1360 in = isl_int_get_si(tok->u.v);
1361 isl_token_free(tok);
1363 tok = isl_stream_next_token_on_same_line(s);
1364 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1365 isl_stream_error(s, tok,
1366 "expecting number of existentials");
1368 isl_stream_push_token(s, tok);
1371 local = isl_int_get_si(tok->u.v);
1372 isl_token_free(tok);
1374 tok = isl_stream_next_token_on_same_line(s);
1375 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1376 isl_stream_error(s, tok,
1377 "expecting number of parameters");
1379 isl_stream_push_token(s, tok);
1382 nparam = isl_int_get_si(tok->u.v);
1383 isl_token_free(tok);
1384 if (n_col != 1 + out + in + local + nparam + 1) {
1385 isl_stream_error(s, NULL,
1386 "dimensions don't match");
1390 out = n_col - 2 - nparam;
1391 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1395 for (i = 0; i < local; ++i) {
1396 int k = isl_basic_map_alloc_div(bmap);
1399 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1402 for (i = 0; i < n_row; ++i)
1403 bmap = basic_map_read_polylib_constraint(s, bmap);
1405 tok = isl_stream_next_token_on_same_line(s);
1407 isl_stream_error(s, tok, "unexpected extra token on line");
1408 isl_stream_push_token(s, tok);
1412 bmap = isl_basic_map_simplify(bmap);
1413 bmap = isl_basic_map_finalize(bmap);
1416 isl_basic_map_free(bmap);
1420 static struct isl_map *map_read_polylib(struct isl_stream *s)
1422 struct isl_token *tok;
1423 struct isl_token *tok2;
1425 struct isl_map *map;
1427 tok = isl_stream_next_token(s);
1429 isl_stream_error(s, NULL, "unexpected EOF");
1432 tok2 = isl_stream_next_token_on_same_line(s);
1433 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1434 isl_stream_push_token(s, tok2);
1435 isl_stream_push_token(s, tok);
1436 return isl_map_from_basic_map(basic_map_read_polylib(s));
1439 isl_stream_error(s, tok2, "unexpected token");
1440 isl_stream_push_token(s, tok2);
1441 isl_stream_push_token(s, tok);
1444 n = isl_int_get_si(tok->u.v);
1445 isl_token_free(tok);
1447 isl_assert(s->ctx, n >= 1, return NULL);
1449 map = isl_map_from_basic_map(basic_map_read_polylib(s));
1451 for (i = 1; map && i < n; ++i)
1452 map = isl_map_union(map,
1453 isl_map_from_basic_map(basic_map_read_polylib(s)));
1458 static int optional_power(struct isl_stream *s)
1461 struct isl_token *tok;
1463 tok = isl_stream_next_token(s);
1466 if (tok->type != '^') {
1467 isl_stream_push_token(s, tok);
1470 isl_token_free(tok);
1471 tok = isl_stream_next_token(s);
1472 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1473 isl_stream_error(s, tok, "expecting exponent");
1475 isl_stream_push_token(s, tok);
1478 pow = isl_int_get_si(tok->u.v);
1479 isl_token_free(tok);
1483 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1484 __isl_keep isl_map *map, struct vars *v);
1486 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1487 __isl_keep isl_map *map, struct vars *v)
1489 isl_pw_qpolynomial *pwqp;
1490 struct isl_token *tok;
1492 tok = next_token(s);
1494 isl_stream_error(s, NULL, "unexpected EOF");
1497 if (tok->type == '(') {
1500 isl_token_free(tok);
1501 pwqp = read_term(s, map, v);
1504 if (isl_stream_eat(s, ')'))
1506 pow = optional_power(s);
1507 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1508 } else if (tok->type == ISL_TOKEN_VALUE) {
1509 struct isl_token *tok2;
1510 tok2 = isl_stream_next_token(s);
1511 isl_qpolynomial *qp;
1512 if (tok2 && tok2->type == '/') {
1513 isl_token_free(tok2);
1514 tok2 = next_token(s);
1515 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1516 isl_stream_error(s, tok2, "expected denominator");
1517 isl_token_free(tok);
1518 isl_token_free(tok2);
1521 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1522 tok->u.v, tok2->u.v);
1523 isl_token_free(tok2);
1525 isl_stream_push_token(s, tok2);
1526 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1529 isl_token_free(tok);
1530 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1531 } else if (tok->type == ISL_TOKEN_INFTY) {
1532 isl_qpolynomial *qp;
1533 isl_token_free(tok);
1534 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1535 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1536 } else if (tok->type == ISL_TOKEN_NAN) {
1537 isl_qpolynomial *qp;
1538 isl_token_free(tok);
1539 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1540 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1541 } else if (tok->type == ISL_TOKEN_IDENT) {
1543 int pos = vars_pos(v, tok->u.s, -1);
1545 isl_qpolynomial *qp;
1547 isl_token_free(tok);
1551 vars_drop(v, v->n - n);
1552 isl_stream_error(s, tok, "unknown identifier");
1553 isl_token_free(tok);
1556 isl_token_free(tok);
1557 pow = optional_power(s);
1558 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1559 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1560 } else if (tok->type == '[') {
1564 isl_stream_push_token(s, tok);
1565 pwaff = accept_div(s, isl_map_get_space(map), v);
1566 pow = optional_power(s);
1567 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1568 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1569 } else if (tok->type == '-') {
1570 isl_token_free(tok);
1571 pwqp = read_factor(s, map, v);
1572 pwqp = isl_pw_qpolynomial_neg(pwqp);
1574 isl_stream_error(s, tok, "unexpected isl_token");
1575 isl_stream_push_token(s, tok);
1579 if (isl_stream_eat_if_available(s, '*') ||
1580 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1581 isl_pw_qpolynomial *pwqp2;
1583 pwqp2 = read_factor(s, map, v);
1584 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1589 isl_pw_qpolynomial_free(pwqp);
1593 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1594 __isl_keep isl_map *map, struct vars *v)
1596 struct isl_token *tok;
1597 isl_pw_qpolynomial *pwqp;
1599 pwqp = read_factor(s, map, v);
1602 tok = next_token(s);
1606 if (tok->type == '+') {
1607 isl_pw_qpolynomial *pwqp2;
1609 isl_token_free(tok);
1610 pwqp2 = read_factor(s, map, v);
1611 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1612 } else if (tok->type == '-') {
1613 isl_pw_qpolynomial *pwqp2;
1615 isl_token_free(tok);
1616 pwqp2 = read_factor(s, map, v);
1617 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1618 } else if (tok->type == ISL_TOKEN_VALUE &&
1619 isl_int_is_neg(tok->u.v)) {
1620 isl_pw_qpolynomial *pwqp2;
1622 isl_stream_push_token(s, tok);
1623 pwqp2 = read_factor(s, map, v);
1624 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1626 isl_stream_push_token(s, tok);
1634 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1635 __isl_take isl_map *map, struct vars *v)
1637 struct isl_token *tok;
1639 tok = isl_stream_next_token(s);
1641 isl_stream_error(s, NULL, "unexpected EOF");
1644 if (tok->type == ':' ||
1645 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1646 isl_token_free(tok);
1647 map = read_disjuncts(s, v, map);
1649 isl_stream_push_token(s, tok);
1657 static struct isl_obj obj_read_poly(struct isl_stream *s,
1658 __isl_take isl_map *map, struct vars *v, int n)
1660 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1661 isl_pw_qpolynomial *pwqp;
1662 struct isl_set *set;
1664 pwqp = read_term(s, map, v);
1665 map = read_optional_disjuncts(s, map, v);
1666 set = isl_map_range(map);
1668 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1670 vars_drop(v, v->n - n);
1676 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1677 __isl_take isl_set *set, struct vars *v, int n)
1679 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1680 isl_pw_qpolynomial *pwqp;
1681 isl_pw_qpolynomial_fold *pwf = NULL;
1683 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1684 return obj_read_poly(s, set, v, n);
1686 if (isl_stream_eat(s, '('))
1689 pwqp = read_term(s, set, v);
1690 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1692 while (isl_stream_eat_if_available(s, ',')) {
1693 isl_pw_qpolynomial_fold *pwf_i;
1694 pwqp = read_term(s, set, v);
1695 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1697 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1700 if (isl_stream_eat(s, ')'))
1703 set = read_optional_disjuncts(s, set, v);
1704 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1706 vars_drop(v, v->n - n);
1712 isl_pw_qpolynomial_fold_free(pwf);
1713 obj.type = isl_obj_none;
1717 static int is_rational(struct isl_stream *s)
1719 struct isl_token *tok;
1721 tok = isl_stream_next_token(s);
1724 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1725 isl_token_free(tok);
1726 isl_stream_eat(s, ':');
1730 isl_stream_push_token(s, tok);
1735 static struct isl_obj obj_read_body(struct isl_stream *s,
1736 __isl_take isl_map *map, struct vars *v)
1738 struct isl_token *tok;
1739 struct isl_obj obj = { isl_obj_set, NULL };
1743 map = isl_map_set_rational(map);
1745 if (isl_stream_next_token_is(s, ':')) {
1746 obj.type = isl_obj_set;
1747 obj.v = read_optional_disjuncts(s, map, v);
1751 if (!next_is_tuple(s))
1752 return obj_read_poly_or_fold(s, map, v, n);
1754 map = read_tuple(s, map, isl_dim_in, v);
1757 tok = isl_stream_next_token(s);
1758 if (tok && tok->type == ISL_TOKEN_TO) {
1759 obj.type = isl_obj_map;
1760 isl_token_free(tok);
1761 if (!next_is_tuple(s)) {
1762 isl_set *set = isl_map_domain(map);
1763 return obj_read_poly_or_fold(s, set, v, n);
1765 map = read_tuple(s, map, isl_dim_out, v);
1769 map = isl_map_reverse(map);
1771 isl_stream_push_token(s, tok);
1774 map = read_optional_disjuncts(s, map, v);
1776 vars_drop(v, v->n - n);
1782 obj.type = isl_obj_none;
1786 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1788 if (obj.type == isl_obj_map) {
1789 obj.v = isl_union_map_from_map(obj.v);
1790 obj.type = isl_obj_union_map;
1791 } else if (obj.type == isl_obj_set) {
1792 obj.v = isl_union_set_from_set(obj.v);
1793 obj.type = isl_obj_union_set;
1794 } else if (obj.type == isl_obj_pw_qpolynomial) {
1795 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1796 obj.type = isl_obj_union_pw_qpolynomial;
1797 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1798 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1799 obj.type = isl_obj_union_pw_qpolynomial_fold;
1801 isl_assert(ctx, 0, goto error);
1804 obj.type->free(obj.v);
1805 obj.type = isl_obj_none;
1809 static struct isl_obj obj_add(struct isl_ctx *ctx,
1810 struct isl_obj obj1, struct isl_obj obj2)
1812 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1813 obj1 = to_union(ctx, obj1);
1814 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1815 obj2 = to_union(ctx, obj2);
1816 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1817 obj1 = to_union(ctx, obj1);
1818 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1819 obj2 = to_union(ctx, obj2);
1820 if (obj1.type == isl_obj_pw_qpolynomial &&
1821 obj2.type == isl_obj_union_pw_qpolynomial)
1822 obj1 = to_union(ctx, obj1);
1823 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1824 obj2.type == isl_obj_pw_qpolynomial)
1825 obj2 = to_union(ctx, obj2);
1826 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1827 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1828 obj1 = to_union(ctx, obj1);
1829 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1830 obj2.type == isl_obj_pw_qpolynomial_fold)
1831 obj2 = to_union(ctx, obj2);
1832 isl_assert(ctx, obj1.type == obj2.type, goto error);
1833 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
1834 obj1 = to_union(ctx, obj1);
1835 obj2 = to_union(ctx, obj2);
1837 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
1838 obj1 = to_union(ctx, obj1);
1839 obj2 = to_union(ctx, obj2);
1841 if (obj1.type == isl_obj_pw_qpolynomial &&
1842 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
1843 obj1 = to_union(ctx, obj1);
1844 obj2 = to_union(ctx, obj2);
1846 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1847 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
1848 obj1 = to_union(ctx, obj1);
1849 obj2 = to_union(ctx, obj2);
1851 obj1.v = obj1.type->add(obj1.v, obj2.v);
1854 obj1.type->free(obj1.v);
1855 obj2.type->free(obj2.v);
1856 obj1.type = isl_obj_none;
1861 static struct isl_obj obj_read(struct isl_stream *s)
1863 isl_map *map = NULL;
1864 struct isl_token *tok;
1865 struct vars *v = NULL;
1866 struct isl_obj obj = { isl_obj_set, NULL };
1868 tok = next_token(s);
1870 isl_stream_error(s, NULL, "unexpected EOF");
1873 if (tok->type == ISL_TOKEN_VALUE) {
1874 struct isl_token *tok2;
1875 struct isl_map *map;
1877 tok2 = isl_stream_next_token(s);
1878 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1879 isl_int_is_neg(tok2->u.v)) {
1881 isl_stream_push_token(s, tok2);
1882 obj.type = isl_obj_int;
1883 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1884 isl_token_free(tok);
1887 isl_stream_push_token(s, tok2);
1888 isl_stream_push_token(s, tok);
1889 map = map_read_polylib(s);
1892 if (isl_map_may_be_set(map))
1893 obj.v = isl_map_range(map);
1895 obj.type = isl_obj_map;
1900 v = vars_new(s->ctx);
1902 isl_stream_push_token(s, tok);
1905 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
1906 if (tok->type == '[') {
1907 isl_stream_push_token(s, tok);
1908 map = read_tuple(s, map, isl_dim_param, v);
1911 tok = isl_stream_next_token(s);
1912 if (!tok || tok->type != ISL_TOKEN_TO) {
1913 isl_stream_error(s, tok, "expecting '->'");
1915 isl_stream_push_token(s, tok);
1918 isl_token_free(tok);
1919 tok = isl_stream_next_token(s);
1921 if (!tok || tok->type != '{') {
1922 isl_stream_error(s, tok, "expecting '{'");
1924 isl_stream_push_token(s, tok);
1927 isl_token_free(tok);
1929 tok = isl_stream_next_token(s);
1932 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1933 isl_token_free(tok);
1934 if (isl_stream_eat(s, '='))
1936 map = read_tuple(s, map, isl_dim_param, v);
1939 } else if (tok->type == '}') {
1940 obj.type = isl_obj_union_set;
1941 obj.v = isl_union_set_empty(isl_map_get_space(map));
1942 isl_token_free(tok);
1945 isl_stream_push_token(s, tok);
1950 o = obj_read_body(s, isl_map_copy(map), v);
1951 if (o.type == isl_obj_none || !o.v)
1956 obj = obj_add(s->ctx, obj, o);
1957 if (obj.type == isl_obj_none || !obj.v)
1960 tok = isl_stream_next_token(s);
1961 if (!tok || tok->type != ';')
1963 isl_token_free(tok);
1964 if (isl_stream_next_token_is(s, '}')) {
1965 tok = isl_stream_next_token(s);
1970 if (tok && tok->type == '}') {
1971 isl_token_free(tok);
1973 isl_stream_error(s, tok, "unexpected isl_token");
1975 isl_token_free(tok);
1985 obj.type->free(obj.v);
1992 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1997 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
2003 isl_assert(s->ctx, obj.type == isl_obj_map ||
2004 obj.type == isl_obj_set, goto error);
2006 if (obj.type == isl_obj_set)
2007 obj.v = isl_map_from_range(obj.v);
2011 obj.type->free(obj.v);
2015 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
2021 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2022 obj.v = isl_map_range(obj.v);
2023 obj.type = isl_obj_set;
2025 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2030 obj.type->free(obj.v);
2034 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2039 if (obj.type == isl_obj_map) {
2040 obj.type = isl_obj_union_map;
2041 obj.v = isl_union_map_from_map(obj.v);
2043 if (obj.type == isl_obj_set) {
2044 obj.type = isl_obj_union_set;
2045 obj.v = isl_union_set_from_set(obj.v);
2048 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2049 obj.type == isl_obj_union_set, goto error);
2053 obj.type->free(obj.v);
2057 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2062 if (obj.type == isl_obj_set) {
2063 obj.type = isl_obj_union_set;
2064 obj.v = isl_union_set_from_set(obj.v);
2067 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2071 obj.type->free(obj.v);
2075 static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
2078 struct isl_map *map;
2079 struct isl_basic_map *bmap;
2086 isl_assert(map->ctx, map->n <= 1, goto error);
2089 bmap = isl_basic_map_empty_like_map(map);
2091 bmap = isl_basic_map_copy(map->p[0]);
2101 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
2103 isl_basic_map *bmap;
2104 bmap = basic_map_read(s);
2107 if (!isl_basic_map_may_be_set(bmap))
2108 isl_die(s->ctx, isl_error_invalid,
2109 "input is not a set", goto error);
2110 return isl_basic_map_range(bmap);
2112 isl_basic_map_free(bmap);
2116 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2119 struct isl_basic_map *bmap;
2120 struct isl_stream *s = isl_stream_new_file(ctx, input);
2123 bmap = basic_map_read(s);
2128 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2131 isl_basic_set *bset;
2132 struct isl_stream *s = isl_stream_new_file(ctx, input);
2135 bset = basic_set_read(s);
2140 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2143 struct isl_basic_map *bmap;
2144 struct isl_stream *s = isl_stream_new_str(ctx, str);
2147 bmap = basic_map_read(s);
2152 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2155 isl_basic_set *bset;
2156 struct isl_stream *s = isl_stream_new_str(ctx, str);
2159 bset = basic_set_read(s);
2164 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2167 struct isl_map *map;
2168 struct isl_stream *s = isl_stream_new_file(ctx, input);
2171 map = isl_stream_read_map(s);
2176 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2179 struct isl_map *map;
2180 struct isl_stream *s = isl_stream_new_str(ctx, str);
2183 map = isl_stream_read_map(s);
2188 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2192 struct isl_stream *s = isl_stream_new_file(ctx, input);
2195 set = isl_stream_read_set(s);
2200 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2204 struct isl_stream *s = isl_stream_new_str(ctx, str);
2207 set = isl_stream_read_set(s);
2212 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2215 isl_union_map *umap;
2216 struct isl_stream *s = isl_stream_new_file(ctx, input);
2219 umap = isl_stream_read_union_map(s);
2224 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2227 isl_union_map *umap;
2228 struct isl_stream *s = isl_stream_new_str(ctx, str);
2231 umap = isl_stream_read_union_map(s);
2236 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2239 isl_union_set *uset;
2240 struct isl_stream *s = isl_stream_new_file(ctx, input);
2243 uset = isl_stream_read_union_set(s);
2248 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2251 isl_union_set *uset;
2252 struct isl_stream *s = isl_stream_new_str(ctx, str);
2255 uset = isl_stream_read_union_set(s);
2260 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2262 struct isl_vec *vec = NULL;
2263 struct isl_token *tok;
2267 tok = isl_stream_next_token(s);
2268 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2269 isl_stream_error(s, tok, "expecting vector length");
2273 size = isl_int_get_si(tok->u.v);
2274 isl_token_free(tok);
2276 vec = isl_vec_alloc(s->ctx, size);
2278 for (j = 0; j < size; ++j) {
2279 tok = isl_stream_next_token(s);
2280 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2281 isl_stream_error(s, tok, "expecting constant value");
2284 isl_int_set(vec->el[j], tok->u.v);
2285 isl_token_free(tok);
2290 isl_token_free(tok);
2295 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2297 return isl_vec_read_polylib(s);
2300 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2303 struct isl_stream *s = isl_stream_new_file(ctx, input);
2311 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2312 struct isl_stream *s)
2318 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2323 obj.type->free(obj.v);
2327 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2330 isl_pw_qpolynomial *pwqp;
2331 struct isl_stream *s = isl_stream_new_str(ctx, str);
2334 pwqp = isl_stream_read_pw_qpolynomial(s);
2339 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2342 isl_pw_qpolynomial *pwqp;
2343 struct isl_stream *s = isl_stream_new_file(ctx, input);
2346 pwqp = isl_stream_read_pw_qpolynomial(s);
2351 /* Read an affine expression from "s" with domain (space) "dom".
2352 * We call accept_affine to parse a possibly piecewise affine expression
2353 * and then check that the result is a single affine expression on
2354 * a universe domain.
2356 static __isl_give isl_aff *read_aff_with_dom(struct isl_stream *s,
2357 __isl_take isl_set *dom, struct vars *v)
2359 isl_aff *aff = NULL;
2360 isl_pw_aff *pwaff = NULL;
2362 if (!isl_set_plain_is_universe(dom))
2363 isl_die(s->ctx, isl_error_invalid,
2364 "expecting universe domain", goto error);
2366 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2369 if (isl_stream_eat(s, '['))
2372 pwaff = accept_affine(s, isl_set_get_space(dom), v);
2374 if (isl_stream_eat(s, ']'))
2376 if (isl_stream_eat(s, '}'))
2383 isl_die(s->ctx, isl_error_invalid,
2384 "expecting single affine expression", goto error);
2385 if (!isl_set_plain_is_universe(pwaff->p[0].set))
2386 isl_die(s->ctx, isl_error_invalid,
2387 "expecting universe domain", goto error);
2389 aff = isl_aff_copy(pwaff->p[0].aff);
2392 isl_pw_aff_free(pwaff);
2397 isl_pw_aff_free(pwaff);
2402 /* Is the next token an identifer not in "v"?
2404 static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
2408 struct isl_token *tok;
2410 tok = isl_stream_next_token(s);
2413 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
2414 isl_stream_push_token(s, tok);
2416 vars_drop(v, v->n - n);
2421 /* First read the domain of the affine expression, which may be
2422 * a parameter space or a set.
2423 * The tricky part is that we don't know if the domain is a set or not,
2424 * so when we are trying to read the domain, we may actually be reading
2425 * the affine expression itself (defined on a parameter domains)
2426 * If the tuple we are reading is named, we assume it's the domain.
2427 * Also, if inside the tuple, the first thing we find is a nested tuple
2428 * or a new identifier, we again assume it's the domain.
2429 * Otherwise, we assume we are reading an affine expression.
2431 static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
2432 __isl_take isl_set *dom, struct vars *v)
2434 struct isl_token *tok;
2436 tok = isl_stream_next_token(s);
2437 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
2438 isl_stream_push_token(s, tok);
2439 return read_tuple(s, dom, isl_dim_set, v);
2441 if (!tok || tok->type != '[') {
2442 isl_stream_error(s, tok, "expecting '['");
2445 if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
2446 isl_stream_push_token(s, tok);
2447 dom = read_tuple(s, dom, isl_dim_set, v);
2449 isl_stream_push_token(s, tok);
2454 isl_stream_push_token(s, tok);
2460 /* Read an affine expression from "s".
2461 * We first read the domain of the affine expression, which may be
2462 * a parameter space or a set, and then call read_aff_with_dom.
2464 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2467 isl_set *dom = NULL;
2469 v = vars_new(s->ctx);
2473 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2474 if (next_is_tuple(s)) {
2475 dom = read_tuple(s, dom, isl_dim_param, v);
2476 if (isl_stream_eat(s, ISL_TOKEN_TO))
2479 if (isl_stream_eat(s, '{'))
2482 dom = read_aff_domain(s, dom, v);
2483 return read_aff_with_dom(s, dom, v);
2490 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2492 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2493 __isl_take isl_set *dom, struct vars *v)
2495 isl_pw_aff *pwaff = NULL;
2497 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2500 if (isl_stream_eat(s, '['))
2503 pwaff = accept_affine(s, isl_set_get_space(dom), v);
2505 if (isl_stream_eat(s, ']'))
2508 dom = read_optional_disjuncts(s, dom, v);
2509 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2514 isl_pw_aff_free(pwaff);
2518 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2521 isl_set *dom = NULL;
2523 isl_pw_aff *pa = NULL;
2526 v = vars_new(s->ctx);
2530 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2531 if (next_is_tuple(s)) {
2532 dom = read_tuple(s, dom, isl_dim_param, v);
2533 if (isl_stream_eat(s, ISL_TOKEN_TO))
2536 if (isl_stream_eat(s, '{'))
2540 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2541 pa = read_pw_aff_with_dom(s, aff_dom, v);
2542 vars_drop(v, v->n - n);
2544 while (isl_stream_eat_if_available(s, ';')) {
2548 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2549 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2550 vars_drop(v, v->n - n);
2552 pa = isl_pw_aff_union_add(pa, pa_i);
2555 if (isl_stream_eat(s, '}'))
2564 isl_pw_aff_free(pa);
2568 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2571 struct isl_stream *s = isl_stream_new_str(ctx, str);
2574 aff = isl_stream_read_aff(s);
2579 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2582 struct isl_stream *s = isl_stream_new_str(ctx, str);
2585 pa = isl_stream_read_pw_aff(s);
2590 /* Read an isl_pw_multi_aff from "s".
2591 * We currently read a generic object and if it turns out to be a set or
2592 * a map, we convert that to an isl_pw_multi_aff.
2593 * It would be more efficient if we were to construct the isl_pw_multi_aff
2596 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2604 if (obj.type == isl_obj_map)
2605 return isl_pw_multi_aff_from_map(obj.v);
2606 if (obj.type == isl_obj_set)
2607 return isl_pw_multi_aff_from_set(obj.v);
2609 obj.type->free(obj.v);
2610 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2614 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2617 isl_pw_multi_aff *pma;
2618 struct isl_stream *s = isl_stream_new_str(ctx, str);
2621 pma = isl_stream_read_pw_multi_aff(s);
2626 /* Read a multi-affine expression from "s".
2627 * We call isl_stream_read_pw_multi_aff to parse a possibly piecewise
2628 * multi-affine expression and then check that the result is
2629 * a single multi-affine expression on a universe domain.
2631 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2633 isl_pw_multi_aff *pma;
2634 isl_multi_aff *maff;
2636 pma = isl_stream_read_pw_multi_aff(s);
2640 isl_die(s->ctx, isl_error_invalid,
2641 "expecting single list of affine expressions",
2642 return isl_pw_multi_aff_free(pma));
2643 if (!isl_set_plain_is_universe(pma->p[0].set))
2644 isl_die(s->ctx, isl_error_invalid, "expecting universe domain",
2645 return isl_pw_multi_aff_free(pma));
2646 maff = isl_multi_aff_copy(pma->p[0].maff);
2647 isl_pw_multi_aff_free(pma);
2651 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
2654 isl_multi_aff *maff;
2655 struct isl_stream *s = isl_stream_new_str(ctx, str);
2658 maff = isl_stream_read_multi_aff(s);
2663 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
2664 struct isl_stream *s)
2669 if (obj.type == isl_obj_pw_qpolynomial) {
2670 obj.type = isl_obj_union_pw_qpolynomial;
2671 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2674 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
2679 obj.type->free(obj.v);
2683 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
2684 isl_ctx *ctx, const char *str)
2686 isl_union_pw_qpolynomial *upwqp;
2687 struct isl_stream *s = isl_stream_new_str(ctx, str);
2690 upwqp = isl_stream_read_union_pw_qpolynomial(s);