2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
22 #include <isl/stream.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
32 /* non-zero if variable represents a min (-1) or a max (1) */
35 struct variable *next;
44 static struct vars *vars_new(struct isl_ctx *ctx)
47 v = isl_alloc_type(ctx, struct vars);
56 static void variable_free(struct variable *var)
59 struct variable *next = var->next;
60 isl_mat_free(var->list);
61 isl_vec_free(var->def);
68 static void vars_free(struct vars *v)
76 static void vars_drop(struct vars *v, int n)
87 struct variable *next = var->next;
88 isl_mat_free(var->list);
89 isl_vec_free(var->def);
97 static struct variable *variable_new(struct vars *v, const char *name, int len,
100 struct variable *var;
101 var = isl_calloc_type(v->ctx, struct variable);
104 var->name = strdup(name);
105 var->name[len] = '\0';
115 static int vars_pos(struct vars *v, const char *s, int len)
122 for (q = v->v; q; q = q->next) {
123 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
130 v->v = variable_new(v, s, len, v->n);
138 static int vars_add_anon(struct vars *v)
140 v->v = variable_new(v, "", 0, v->n);
149 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
150 enum isl_dim_type type, unsigned pos, char *name)
159 prime = strchr(name, '\'');
162 bmap = isl_basic_map_set_dim_name(bmap, type, pos, name);
169 /* Obtain next token, with some preprocessing.
170 * In particular, evaluate expressions of the form x^y,
171 * with x and y values.
173 static struct isl_token *next_token(struct isl_stream *s)
175 struct isl_token *tok, *tok2;
177 tok = isl_stream_next_token(s);
178 if (!tok || tok->type != ISL_TOKEN_VALUE)
180 if (!isl_stream_eat_if_available(s, '^'))
182 tok2 = isl_stream_next_token(s);
183 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
184 isl_stream_error(s, tok2, "expecting constant value");
188 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
190 isl_token_free(tok2);
194 isl_token_free(tok2);
198 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
200 struct isl_token *tok;
203 if (!tok || tok->type != ISL_TOKEN_VALUE) {
204 isl_stream_error(s, tok, "expecting constant value");
208 isl_int_mul(*f, *f, tok->u.v);
212 if (isl_stream_eat_if_available(s, '*'))
213 return accept_cst_factor(s, f);
221 /* Given an affine expression aff, return an affine expression
222 * for aff % d, with d the next token on the stream, which is
223 * assumed to be a constant.
225 * We introduce an integer division q = [aff/d] and the result
226 * is set to aff - d q.
228 static __isl_give isl_vec *affine_mod(struct isl_stream *s,
229 struct vars *v, __isl_take isl_vec *aff)
231 struct isl_token *tok;
232 struct variable *var;
236 if (!tok || tok->type != ISL_TOKEN_VALUE) {
237 isl_stream_error(s, tok, "expecting constant value");
241 if (vars_add_anon(v) < 0)
246 var->def = isl_vec_alloc(s->ctx, 2 + v->n);
249 isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
250 isl_int_set_si(var->def->el[1 + aff->size], 0);
251 isl_int_set(var->def->el[0], tok->u.v);
253 mod = isl_vec_alloc(v->ctx, 1 + v->n);
257 isl_seq_cpy(mod->el, aff->el, aff->size);
258 isl_int_neg(mod->el[aff->size], tok->u.v);
269 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v);
270 static int read_div_definition(struct isl_stream *s, struct vars *v);
271 static int read_minmax_definition(struct isl_stream *s, struct vars *v);
273 static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
276 struct isl_token *tok = NULL;
281 isl_stream_error(s, NULL, "unexpected EOF");
284 if (tok->type == ISL_TOKEN_IDENT) {
286 int pos = vars_pos(v, tok->u.s, -1);
290 isl_stream_error(s, tok, "unknown identifier");
294 aff = isl_vec_alloc(v->ctx, 1 + v->n);
297 isl_seq_clr(aff->el, aff->size);
298 isl_int_set_si(aff->el[1 + pos], 1);
300 } else if (tok->type == ISL_TOKEN_VALUE) {
301 if (isl_stream_eat_if_available(s, '*')) {
302 aff = accept_affine_factor(s, v);
303 aff = isl_vec_scale(aff, tok->u.v);
305 aff = isl_vec_alloc(v->ctx, 1 + v->n);
308 isl_seq_clr(aff->el, aff->size);
309 isl_int_set(aff->el[0], tok->u.v);
312 } else if (tok->type == '(') {
315 aff = accept_affine(s, v);
318 if (isl_stream_eat(s, ')'))
320 } else if (tok->type == '[') {
321 if (vars_add_anon(v) < 0)
323 aff = isl_vec_alloc(v->ctx, 1 + v->n);
326 isl_seq_clr(aff->el, aff->size);
327 isl_int_set_si(aff->el[1 + v->n - 1], 1);
328 isl_stream_push_token(s, tok);
330 if (read_div_definition(s, v) < 0)
332 aff = isl_vec_zero_extend(aff, 1 + v->n);
333 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
334 if (vars_add_anon(v) < 0)
336 aff = isl_vec_alloc(v->ctx, 1 + v->n);
339 isl_seq_clr(aff->el, aff->size);
340 isl_int_set_si(aff->el[1 + v->n - 1], 1);
341 isl_stream_push_token(s, tok);
343 if (read_minmax_definition(s, v) < 0)
345 aff = isl_vec_zero_extend(aff, 1 + v->n);
347 isl_stream_error(s, tok, "expecting factor");
350 if (isl_stream_eat_if_available(s, '%'))
351 return affine_mod(s, v, aff);
352 if (isl_stream_eat_if_available(s, '*')) {
355 isl_int_set_si(f, 1);
356 if (accept_cst_factor(s, &f) < 0) {
360 aff = isl_vec_scale(aff, f);
372 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
374 struct isl_token *tok = NULL;
378 aff = isl_vec_alloc(v->ctx, 1 + v->n);
381 isl_seq_clr(aff->el, aff->size);
386 isl_stream_error(s, NULL, "unexpected EOF");
389 if (tok->type == '-') {
394 if (tok->type == '(' || tok->type == '[' ||
395 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
396 tok->type == ISL_TOKEN_IDENT) {
398 isl_stream_push_token(s, tok);
400 aff2 = accept_affine_factor(s, v);
402 aff2 = isl_vec_scale(aff2, s->ctx->negone);
403 aff = isl_vec_zero_extend(aff, 1 + v->n);
404 aff = isl_vec_add(aff, aff2);
408 } else if (tok->type == ISL_TOKEN_VALUE) {
410 isl_int_neg(tok->u.v, tok->u.v);
411 if (isl_stream_eat_if_available(s, '*') ||
412 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
414 aff2 = accept_affine_factor(s, v);
415 aff2 = isl_vec_scale(aff2, tok->u.v);
416 aff = isl_vec_zero_extend(aff, 1 + v->n);
417 aff = isl_vec_add(aff, aff2);
421 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
425 isl_stream_error(s, tok, "unexpected isl_token");
426 isl_stream_push_token(s, tok);
433 if (tok && tok->type == '-') {
436 } else if (tok && tok->type == '+') {
439 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
440 isl_int_is_neg(tok->u.v)) {
441 isl_stream_push_token(s, tok);
444 isl_stream_push_token(s, tok);
456 /* Add any variables in the variable list "v" that are not already in "bmap"
457 * as existentially quantified variables in "bmap".
459 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
464 struct variable *var;
466 extra = v->n - isl_basic_map_total_dim(bmap);
471 bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
472 extra, 0, 2 * extra);
474 for (i = 0; i < extra; ++i)
475 if (isl_basic_map_alloc_div(bmap) < 0)
478 for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
479 int k = bmap->n_div - 1 - i;
481 isl_seq_cpy(bmap->div[k], var->def->el, var->def->size);
482 isl_seq_clr(bmap->div[k] + var->def->size,
483 2 + v->n - var->def->size);
485 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
491 isl_basic_map_free(bmap);
495 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
496 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
499 isl_basic_map *def = NULL;
504 if (vars_add_anon(v) < 0)
508 vec = accept_affine(s, v);
512 dim = isl_basic_map_get_dim(bmap);
513 def = isl_basic_map_universe(dim);
514 def = add_divs(def, v);
515 def = isl_basic_map_extend_constraints(def, 1, 0);
516 k = isl_basic_map_alloc_equality(def);
518 isl_seq_cpy(def->eq[k], vec->el, vec->size);
519 isl_int_set_si(def->eq[k][1 + n - 1], -1);
525 vars_drop(v, v->n - n);
527 def = isl_basic_map_simplify(def);
528 def = isl_basic_map_finalize(def);
529 bmap = isl_basic_map_intersect(bmap, def);
532 isl_basic_map_free(bmap);
533 isl_basic_map_free(def);
537 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
538 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
541 struct isl_token *tok;
543 while ((tok = next_token(s)) != NULL) {
546 if (tok->type == ISL_TOKEN_IDENT) {
548 int p = vars_pos(v, tok->u.s, -1);
555 bmap = isl_basic_map_add(bmap, type, 1);
556 bmap = set_name(bmap, type, i, v->v->name);
558 } else if (tok->type == ISL_TOKEN_IDENT ||
559 tok->type == ISL_TOKEN_VALUE ||
562 if (type == isl_dim_param) {
563 isl_stream_error(s, tok,
564 "expecting unique identifier");
567 isl_stream_push_token(s, tok);
569 bmap = isl_basic_map_add(bmap, type, 1);
570 bmap = read_var_def(s, bmap, type, v);
574 tok = isl_stream_next_token(s);
575 if (!tok || tok->type != ',')
582 isl_stream_push_token(s, tok);
587 isl_basic_map_free(bmap);
591 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
596 struct isl_token *tok = NULL;
598 vec = accept_affine(s, v);
599 mat = isl_mat_from_row_vec(vec);
604 tok = isl_stream_next_token(s);
606 isl_stream_error(s, NULL, "unexpected EOF");
609 if (tok->type != ',') {
610 isl_stream_push_token(s, tok);
615 vec = accept_affine(s, v);
616 mat = isl_mat_add_zero_cols(mat, 1 + v->n - isl_mat_cols(mat));
617 mat = isl_mat_vec_concat(mat, vec);
628 static int read_minmax_definition(struct isl_stream *s, struct vars *v)
630 struct isl_token *tok;
631 struct variable *var;
635 tok = isl_stream_next_token(s);
638 var->sign = tok->type == ISL_TOKEN_MIN ? -1 : 1;
641 if (isl_stream_eat(s, '('))
644 var->list = accept_affine_list(s, v);
648 if (isl_stream_eat(s, ')'))
654 static int read_div_definition(struct isl_stream *s, struct vars *v)
656 struct isl_token *tok;
659 struct variable *var;
661 if (isl_stream_eat(s, '['))
664 tok = isl_stream_next_token(s);
667 if (tok->type == '(') {
671 isl_stream_push_token(s, tok);
675 aff = accept_affine(s, v);
679 var->def = isl_vec_alloc(s->ctx, 2 + v->n);
685 isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
689 if (seen_paren && isl_stream_eat(s, ')'))
691 if (isl_stream_eat(s, '/'))
697 if (tok->type != ISL_TOKEN_VALUE) {
698 isl_stream_error(s, tok, "expected denominator");
699 isl_stream_push_token(s, tok);
702 isl_int_set(var->def->el[0], tok->u.v);
705 if (isl_stream_eat(s, ']'))
711 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
712 struct vars *v, struct isl_basic_map *bmap, int pos)
714 struct variable *var = v->v;
715 unsigned o_out = isl_basic_map_offset(bmap, isl_dim_out) - 1;
717 if (read_div_definition(s, v) < 0)
720 if (isl_basic_map_add_div_constraints_var(bmap, o_out + pos,
726 isl_basic_map_free(bmap);
730 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
731 struct vars *v, struct isl_basic_map *bmap)
733 struct isl_token *tok;
735 while ((tok = isl_stream_next_token(s)) != NULL) {
738 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
740 if (tok->type != ISL_TOKEN_IDENT)
743 p = vars_pos(v, tok->u.s, -1);
747 isl_stream_error(s, tok, "expecting unique identifier");
751 bmap = isl_basic_map_cow(bmap);
752 bmap = isl_basic_map_add(bmap, isl_dim_out, 1);
753 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
757 tok = isl_stream_next_token(s);
758 if (tok && tok->type == '=') {
760 bmap = add_div_definition(s, v, bmap, n_out);
761 tok = isl_stream_next_token(s);
764 if (!tok || tok->type != ',')
770 isl_stream_push_token(s, tok);
775 isl_basic_map_free(bmap);
779 static int next_is_tuple(struct isl_stream *s)
781 struct isl_token *tok;
784 tok = isl_stream_next_token(s);
787 if (tok->type == '[') {
788 isl_stream_push_token(s, tok);
791 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
792 isl_stream_push_token(s, tok);
796 is_tuple = isl_stream_next_token_is(s, '[');
798 isl_stream_push_token(s, tok);
803 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
804 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v);
806 static __isl_give isl_basic_map *read_nested_tuple(struct isl_stream *s,
807 __isl_take isl_basic_map *bmap, struct vars *v)
809 bmap = read_tuple(s, bmap, isl_dim_in, v);
810 if (isl_stream_eat(s, ISL_TOKEN_TO))
812 bmap = read_tuple(s, bmap, isl_dim_out, v);
813 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
816 isl_basic_map_free(bmap);
820 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
821 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
823 struct isl_token *tok;
826 tok = isl_stream_next_token(s);
827 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
828 name = strdup(tok->u.s);
832 tok = isl_stream_next_token(s);
834 if (!tok || tok->type != '[') {
835 isl_stream_error(s, tok, "expecting '['");
839 if (type != isl_dim_param && next_is_tuple(s)) {
840 isl_dim *dim = isl_basic_map_get_dim(bmap);
841 int nparam = isl_dim_size(dim, isl_dim_param);
842 int n_in = isl_dim_size(dim, isl_dim_in);
843 isl_basic_map *nested;
844 if (type == isl_dim_out)
845 dim = isl_dim_move(dim, isl_dim_param, nparam,
846 isl_dim_in, 0, n_in);
847 nested = isl_basic_map_alloc_dim(dim, 0, 0, 0);
848 nested = read_nested_tuple(s, nested, v);
849 if (type == isl_dim_in) {
850 nested = isl_basic_map_reverse(nested);
851 bmap = isl_basic_map_intersect(nested, bmap);
854 dim = isl_dim_range(isl_basic_map_get_dim(nested));
855 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
856 dim = isl_dim_join(isl_basic_map_get_dim(bmap), dim);
857 bset = isl_basic_map_domain(bmap);
858 nested = isl_basic_map_reset_dim(nested, dim);
859 bmap = isl_basic_map_intersect_domain(nested, bset);
862 bmap = read_var_list(s, bmap, type, v);
863 tok = isl_stream_next_token(s);
864 if (!tok || tok->type != ']') {
865 isl_stream_error(s, tok, "expecting ']'");
871 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
879 isl_basic_map_free(bmap);
883 static __isl_give isl_basic_map *construct_constraint(
884 __isl_take isl_basic_map *bmap, enum isl_token_type type,
885 isl_int *left, isl_int *right)
893 len = 1 + isl_basic_map_total_dim(bmap);
896 k = isl_basic_map_alloc_inequality(bmap);
899 if (type == ISL_TOKEN_LE)
900 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
903 else if (type == ISL_TOKEN_GE)
904 isl_seq_combine(bmap->ineq[k], ctx->one, left,
907 else if (type == ISL_TOKEN_LT) {
908 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
911 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
912 } else if (type == ISL_TOKEN_GT) {
913 isl_seq_combine(bmap->ineq[k], ctx->one, left,
916 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
918 isl_seq_combine(bmap->ineq[k], ctx->one, left,
921 isl_basic_map_inequality_to_equality(bmap, k);
926 isl_basic_map_free(bmap);
930 static int is_comparator(struct isl_token *tok)
947 /* Add any variables in the variable list "v" that are not already in "bmap"
948 * as output variables in "bmap".
950 static __isl_give isl_basic_map *add_lifted_divs(__isl_take isl_basic_map *bmap,
955 struct variable *var;
957 extra = v->n - isl_basic_map_total_dim(bmap);
962 bmap = isl_basic_map_add(bmap, isl_dim_out, extra);
963 bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
966 for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
969 var->def = isl_vec_zero_extend(var->def, 2 + v->n);
972 if (isl_basic_map_add_div_constraints_var(bmap, var->pos,
979 isl_basic_map_free(bmap);
983 static struct isl_basic_map *add_constraint(struct isl_stream *s,
984 struct vars *v, struct isl_basic_map *bmap)
987 struct isl_token *tok = NULL;
988 struct isl_mat *aff1 = NULL, *aff2 = NULL;
990 bmap = isl_basic_map_cow(bmap);
992 aff1 = accept_affine_list(s, v);
995 tok = isl_stream_next_token(s);
996 if (!is_comparator(tok)) {
997 isl_stream_error(s, tok, "missing operator");
999 isl_stream_push_token(s, tok);
1004 aff2 = accept_affine_list(s, v);
1008 aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
1011 bmap = add_lifted_divs(bmap, v);
1012 bmap = isl_basic_map_extend_constraints(bmap, 0,
1013 aff1->n_row * aff2->n_row);
1014 for (i = 0; i < aff1->n_row; ++i)
1015 for (j = 0; j < aff2->n_row; ++j)
1016 bmap = construct_constraint(bmap, tok->type,
1017 aff1->row[i], aff2->row[j]);
1018 isl_token_free(tok);
1022 tok = isl_stream_next_token(s);
1023 if (!is_comparator(tok)) {
1025 isl_stream_push_token(s, tok);
1034 isl_token_free(tok);
1037 isl_basic_map_free(bmap);
1041 /* Return first variable, starting at n, representing a min or max,
1042 * or NULL if there is no such variable.
1044 static struct variable *first_minmax(struct vars *v, int n)
1046 struct variable *first = NULL;
1047 struct variable *var;
1049 for (var = v->v; var && var->pos >= n; var = var->next)
1056 /* Check whether the variable at the given position only occurs in
1057 * inequalities and only with the given sign.
1059 static int all_coefficients_of_sign(__isl_keep isl_map *map, int pos, int sign)
1066 for (i = 0; i < map->n; ++i) {
1067 isl_basic_map *bmap = map->p[i];
1069 for (j = 0; j < bmap->n_eq; ++j)
1070 if (!isl_int_is_zero(bmap->eq[j][1 + pos]))
1072 for (j = 0; j < bmap->n_ineq; ++j) {
1073 int s = isl_int_sgn(bmap->ineq[j][1 + pos]);
1084 /* Given a variable m which represents a min or a max of n expressions
1085 * b_i, add the constraints
1089 * in case of a min (var->sign < 0) and m >= b_i in case of a max.
1091 static __isl_give isl_map *bound_minmax(__isl_take isl_map *map,
1092 struct variable *var)
1095 isl_basic_map *bound;
1098 total = isl_map_dim(map, isl_dim_all);
1099 bound = isl_basic_map_alloc_dim(isl_map_get_dim(map),
1100 0, 0, var->list->n_row);
1102 for (i = 0; i < var->list->n_row; ++i) {
1103 k = isl_basic_map_alloc_inequality(bound);
1107 isl_seq_cpy(bound->ineq[k], var->list->row[i],
1110 isl_seq_neg(bound->ineq[k], var->list->row[i],
1112 isl_int_set_si(bound->ineq[k][1 + var->pos], var->sign);
1113 isl_seq_clr(bound->ineq[k] + var->list->n_col,
1114 1 + total - var->list->n_col);
1117 map = isl_map_intersect(map, isl_map_from_basic_map(bound));
1121 isl_basic_map_free(bound);
1126 /* Given a variable m which represents a min (or max) of n expressions
1127 * b_i, add constraints that assigns the minimal upper bound to m, i.e.,
1128 * divide the space into cells where one
1129 * of the upper bounds is smaller than all the others and assign
1130 * this upper bound to m.
1132 * In particular, if there are n bounds b_i, then the input map
1133 * is split into n pieces, each with the extra constraints
1136 * b_i <= b_j for j > i
1137 * b_i < b_j for j < i
1139 * in case of a min (var->sign < 0) and similarly in case of a max.
1141 * Note: this function is very similar to set_minimum in isl_tab_pip.c
1142 * Perhaps we should try to merge the two.
1144 static __isl_give isl_map *set_minmax(__isl_take isl_map *map,
1145 struct variable *var)
1148 isl_basic_map *bmap = NULL;
1150 isl_map *split = NULL;
1153 ctx = isl_map_get_ctx(map);
1154 total = isl_map_dim(map, isl_dim_all);
1155 split = isl_map_alloc_dim(isl_map_get_dim(map),
1156 var->list->n_row, ISL_SET_DISJOINT);
1158 for (i = 0; i < var->list->n_row; ++i) {
1159 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(map), 0,
1160 1, var->list->n_row - 1);
1161 k = isl_basic_map_alloc_equality(bmap);
1164 isl_seq_cpy(bmap->eq[k], var->list->row[i], var->list->n_col);
1165 isl_int_set_si(bmap->eq[k][1 + var->pos], -1);
1166 for (j = 0; j < var->list->n_row; ++j) {
1169 k = isl_basic_map_alloc_inequality(bmap);
1173 isl_seq_combine(bmap->ineq[k],
1174 ctx->one, var->list->row[j],
1175 ctx->negone, var->list->row[i],
1178 isl_seq_combine(bmap->ineq[k],
1179 ctx->negone, var->list->row[j],
1180 ctx->one, var->list->row[i],
1182 isl_seq_clr(bmap->ineq[k] + var->list->n_col,
1183 1 + total - var->list->n_col);
1185 isl_int_sub_ui(bmap->ineq[k][0],
1186 bmap->ineq[k][0], 1);
1188 bmap = isl_basic_map_finalize(bmap);
1189 split = isl_map_add_basic_map(split, bmap);
1192 map = isl_map_intersect(map, split);
1196 isl_basic_map_free(bmap);
1197 isl_map_free(split);
1202 /* Plug in the definitions of all min and max expressions.
1203 * If a min expression only appears in inequalities and only
1204 * with a positive coefficient, then we can simply bound
1205 * the variable representing the min by its defining terms
1206 * and similarly for a max expression.
1207 * Otherwise, we have to assign the different terms to the
1208 * variable under the condition that the assigned term is smaller
1209 * than the other terms.
1211 static __isl_give isl_map *add_minmax(__isl_take isl_map *map,
1212 struct vars *v, int n)
1215 struct variable *var;
1218 var = first_minmax(v, n);
1221 if (all_coefficients_of_sign(map, var->pos, -var->sign))
1222 map = bound_minmax(map, var);
1224 map = set_minmax(map, var);
1231 static isl_map *read_constraint(struct isl_stream *s,
1232 struct vars *v, __isl_take isl_basic_map *bmap)
1240 bmap = isl_basic_set_unwrap(isl_basic_set_lift(isl_basic_map_wrap(bmap)));
1242 bmap = add_constraint(s, v, bmap);
1243 bmap = isl_basic_map_simplify(bmap);
1244 bmap = isl_basic_map_finalize(bmap);
1246 map = isl_map_from_basic_map(bmap);
1248 map = add_minmax(map, v, n);
1250 map = isl_set_unwrap(isl_map_domain(map));
1252 vars_drop(v, v->n - n);
1257 static struct isl_map *read_disjuncts(struct isl_stream *s,
1258 struct vars *v, __isl_take isl_basic_map *bmap);
1260 static __isl_give isl_map *read_exists(struct isl_stream *s,
1261 struct vars *v, __isl_take isl_basic_map *bmap)
1264 int seen_paren = isl_stream_eat_if_available(s, '(');
1265 isl_map *map = NULL;
1267 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
1268 bmap = read_defined_var_list(s, v, bmap);
1270 if (isl_stream_eat(s, ':'))
1273 map = read_disjuncts(s, v, bmap);
1274 map = isl_set_unwrap(isl_map_domain(map));
1277 vars_drop(v, v->n - n);
1278 if (seen_paren && isl_stream_eat(s, ')'))
1283 isl_basic_map_free(bmap);
1288 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1289 struct vars *v, __isl_take isl_basic_map *bmap)
1293 if (isl_stream_eat_if_available(s, '(')) {
1294 map = read_disjuncts(s, v, bmap);
1295 if (isl_stream_eat(s, ')'))
1300 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1301 return read_exists(s, v, bmap);
1303 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1304 return isl_map_from_basic_map(bmap);
1306 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1307 isl_dim *dim = isl_basic_map_get_dim(bmap);
1308 isl_basic_map_free(bmap);
1309 return isl_map_empty(dim);
1312 return read_constraint(s, v, bmap);
1318 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1319 struct vars *v, __isl_take isl_basic_map *bmap)
1324 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1325 map = read_conjunct(s, v, isl_basic_map_copy(bmap));
1328 t = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1329 map = isl_map_subtract(t, map);
1332 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1335 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1336 map_i = read_conjunct(s, v, isl_basic_map_copy(bmap));
1338 map = isl_map_subtract(map, map_i);
1340 map = isl_map_intersect(map, map_i);
1343 isl_basic_map_free(bmap);
1347 static struct isl_map *read_disjuncts(struct isl_stream *s,
1348 struct vars *v, __isl_take isl_basic_map *bmap)
1350 struct isl_map *map;
1352 if (isl_stream_next_token_is(s, '}')) {
1353 isl_dim *dim = isl_basic_map_get_dim(bmap);
1354 isl_basic_map_free(bmap);
1355 return isl_map_universe(dim);
1358 map = read_conjuncts(s, v, isl_basic_map_copy(bmap));
1359 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1362 map_i = read_conjuncts(s, v, isl_basic_map_copy(bmap));
1363 map = isl_map_union(map, map_i);
1366 isl_basic_map_free(bmap);
1370 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1372 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1373 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1374 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1375 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1377 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1378 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1379 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1381 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1382 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1383 isl_basic_map_dim(bmap, isl_dim_in) +
1384 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1385 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1387 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1393 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1394 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1397 struct isl_token *tok;
1407 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1408 dim = isl_basic_map_dim(bmap, isl_dim_out);
1410 tok = isl_stream_next_token(s);
1411 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1412 isl_stream_error(s, tok, "expecting coefficient");
1414 isl_stream_push_token(s, tok);
1417 if (!tok->on_new_line) {
1418 isl_stream_error(s, tok, "coefficient should appear on new line");
1419 isl_stream_push_token(s, tok);
1423 type = isl_int_get_si(tok->u.v);
1424 isl_token_free(tok);
1426 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1428 k = isl_basic_map_alloc_equality(bmap);
1431 k = isl_basic_map_alloc_inequality(bmap);
1437 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1439 tok = isl_stream_next_token(s);
1440 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1441 isl_stream_error(s, tok, "expecting coefficient");
1443 isl_stream_push_token(s, tok);
1446 if (tok->on_new_line) {
1447 isl_stream_error(s, tok,
1448 "coefficient should not appear on new line");
1449 isl_stream_push_token(s, tok);
1452 pos = polylib_pos_to_isl_pos(bmap, j);
1453 isl_int_set(c[pos], tok->u.v);
1454 isl_token_free(tok);
1459 isl_basic_map_free(bmap);
1463 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1467 struct isl_token *tok;
1468 struct isl_token *tok2;
1471 unsigned in = 0, out, local = 0;
1472 struct isl_basic_map *bmap = NULL;
1477 tok = isl_stream_next_token(s);
1479 isl_stream_error(s, NULL, "unexpected EOF");
1482 tok2 = isl_stream_next_token(s);
1484 isl_token_free(tok);
1485 isl_stream_error(s, NULL, "unexpected EOF");
1488 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1489 isl_stream_push_token(s, tok2);
1490 isl_stream_push_token(s, tok);
1491 isl_stream_error(s, NULL,
1492 "expecting constraint matrix dimensions");
1495 n_row = isl_int_get_si(tok->u.v);
1496 n_col = isl_int_get_si(tok2->u.v);
1497 on_new_line = tok2->on_new_line;
1498 isl_token_free(tok2);
1499 isl_token_free(tok);
1500 isl_assert(s->ctx, !on_new_line, return NULL);
1501 isl_assert(s->ctx, n_row >= 0, return NULL);
1502 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1503 tok = isl_stream_next_token_on_same_line(s);
1505 if (tok->type != ISL_TOKEN_VALUE) {
1506 isl_stream_error(s, tok,
1507 "expecting number of output dimensions");
1508 isl_stream_push_token(s, tok);
1511 out = isl_int_get_si(tok->u.v);
1512 isl_token_free(tok);
1514 tok = isl_stream_next_token_on_same_line(s);
1515 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1516 isl_stream_error(s, tok,
1517 "expecting number of input dimensions");
1519 isl_stream_push_token(s, tok);
1522 in = isl_int_get_si(tok->u.v);
1523 isl_token_free(tok);
1525 tok = isl_stream_next_token_on_same_line(s);
1526 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1527 isl_stream_error(s, tok,
1528 "expecting number of existentials");
1530 isl_stream_push_token(s, tok);
1533 local = isl_int_get_si(tok->u.v);
1534 isl_token_free(tok);
1536 tok = isl_stream_next_token_on_same_line(s);
1537 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1538 isl_stream_error(s, tok,
1539 "expecting number of parameters");
1541 isl_stream_push_token(s, tok);
1544 nparam = isl_int_get_si(tok->u.v);
1545 isl_token_free(tok);
1546 if (n_col != 1 + out + in + local + nparam + 1) {
1547 isl_stream_error(s, NULL,
1548 "dimensions don't match");
1552 out = n_col - 2 - nparam;
1553 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1557 for (i = 0; i < local; ++i) {
1558 int k = isl_basic_map_alloc_div(bmap);
1561 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1564 for (i = 0; i < n_row; ++i)
1565 bmap = basic_map_read_polylib_constraint(s, bmap);
1567 tok = isl_stream_next_token_on_same_line(s);
1569 isl_stream_error(s, tok, "unexpected extra token on line");
1570 isl_stream_push_token(s, tok);
1574 bmap = isl_basic_map_simplify(bmap);
1575 bmap = isl_basic_map_finalize(bmap);
1578 isl_basic_map_free(bmap);
1582 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1584 struct isl_token *tok;
1585 struct isl_token *tok2;
1587 struct isl_map *map;
1589 tok = isl_stream_next_token(s);
1591 isl_stream_error(s, NULL, "unexpected EOF");
1594 tok2 = isl_stream_next_token_on_same_line(s);
1595 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1596 isl_stream_push_token(s, tok2);
1597 isl_stream_push_token(s, tok);
1598 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1601 isl_stream_error(s, tok2, "unexpected token");
1602 isl_stream_push_token(s, tok2);
1603 isl_stream_push_token(s, tok);
1606 n = isl_int_get_si(tok->u.v);
1607 isl_token_free(tok);
1609 isl_assert(s->ctx, n >= 1, return NULL);
1611 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1613 for (i = 1; map && i < n; ++i)
1614 map = isl_map_union(map,
1615 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1620 static int optional_power(struct isl_stream *s)
1623 struct isl_token *tok;
1625 tok = isl_stream_next_token(s);
1628 if (tok->type != '^') {
1629 isl_stream_push_token(s, tok);
1632 isl_token_free(tok);
1633 tok = isl_stream_next_token(s);
1634 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1635 isl_stream_error(s, tok, "expecting exponent");
1637 isl_stream_push_token(s, tok);
1640 pow = isl_int_get_si(tok->u.v);
1641 isl_token_free(tok);
1645 static __isl_give isl_div *read_div(struct isl_stream *s,
1646 __isl_take isl_dim *dim, struct vars *v)
1649 isl_basic_map *bmap;
1652 bmap = isl_basic_map_universe(dim);
1654 if (vars_add_anon(v) < 0)
1656 if (read_div_definition(s, v) < 0)
1658 bmap = add_divs(bmap, v);
1659 bmap = isl_basic_map_order_divs(bmap);
1662 vars_drop(v, v->n - n);
1664 return isl_basic_map_div(bmap, bmap->n_div - 1);
1666 isl_basic_map_free(bmap);
1670 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1671 __isl_keep isl_basic_map *bmap, struct vars *v);
1673 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1674 __isl_keep isl_basic_map *bmap, struct vars *v)
1676 struct isl_qpolynomial *qp;
1677 struct isl_token *tok;
1679 tok = next_token(s);
1681 isl_stream_error(s, NULL, "unexpected EOF");
1684 if (tok->type == '(') {
1687 isl_token_free(tok);
1688 qp = read_term(s, bmap, v);
1691 if (isl_stream_eat(s, ')'))
1693 pow = optional_power(s);
1694 qp = isl_qpolynomial_pow(qp, pow);
1695 } else if (tok->type == ISL_TOKEN_VALUE) {
1696 struct isl_token *tok2;
1697 tok2 = isl_stream_next_token(s);
1698 if (tok2 && tok2->type == '/') {
1699 isl_token_free(tok2);
1700 tok2 = next_token(s);
1701 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1702 isl_stream_error(s, tok2, "expected denominator");
1703 isl_token_free(tok);
1704 isl_token_free(tok2);
1707 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1708 tok->u.v, tok2->u.v);
1709 isl_token_free(tok2);
1711 isl_stream_push_token(s, tok2);
1712 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1715 isl_token_free(tok);
1716 } else if (tok->type == ISL_TOKEN_INFTY) {
1717 isl_token_free(tok);
1718 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1719 } else if (tok->type == ISL_TOKEN_NAN) {
1720 isl_token_free(tok);
1721 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1722 } else if (tok->type == ISL_TOKEN_IDENT) {
1724 int pos = vars_pos(v, tok->u.s, -1);
1727 isl_token_free(tok);
1731 vars_drop(v, v->n - n);
1732 isl_stream_error(s, tok, "unknown identifier");
1733 isl_token_free(tok);
1736 isl_token_free(tok);
1737 pow = optional_power(s);
1738 qp = isl_qpolynomial_var_pow(isl_basic_map_get_dim(bmap), pos, pow);
1739 } else if (tok->type == '[') {
1743 isl_stream_push_token(s, tok);
1744 div = read_div(s, isl_basic_map_get_dim(bmap), v);
1745 pow = optional_power(s);
1746 qp = isl_qpolynomial_div_pow(div, pow);
1747 } else if (tok->type == '-') {
1748 struct isl_qpolynomial *qp2;
1750 isl_token_free(tok);
1751 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1753 qp2 = read_factor(s, bmap, v);
1754 qp = isl_qpolynomial_mul(qp, qp2);
1756 isl_stream_error(s, tok, "unexpected isl_token");
1757 isl_stream_push_token(s, tok);
1761 if (isl_stream_eat_if_available(s, '*') ||
1762 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1763 struct isl_qpolynomial *qp2;
1765 qp2 = read_factor(s, bmap, v);
1766 qp = isl_qpolynomial_mul(qp, qp2);
1771 isl_qpolynomial_free(qp);
1775 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1776 __isl_keep isl_basic_map *bmap, struct vars *v)
1778 struct isl_token *tok;
1779 struct isl_qpolynomial *qp;
1781 qp = read_factor(s, bmap, v);
1784 tok = next_token(s);
1788 if (tok->type == '+') {
1789 struct isl_qpolynomial *qp2;
1791 isl_token_free(tok);
1792 qp2 = read_factor(s, bmap, v);
1793 qp = isl_qpolynomial_add(qp, qp2);
1794 } else if (tok->type == '-') {
1795 struct isl_qpolynomial *qp2;
1797 isl_token_free(tok);
1798 qp2 = read_factor(s, bmap, v);
1799 qp = isl_qpolynomial_sub(qp, qp2);
1800 } else if (tok->type == ISL_TOKEN_VALUE &&
1801 isl_int_is_neg(tok->u.v)) {
1802 struct isl_qpolynomial *qp2;
1804 isl_stream_push_token(s, tok);
1805 qp2 = read_factor(s, bmap, v);
1806 qp = isl_qpolynomial_add(qp, qp2);
1808 isl_stream_push_token(s, tok);
1816 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1817 __isl_take isl_basic_map *bmap, struct vars *v)
1819 struct isl_token *tok;
1820 struct isl_map *map;
1822 tok = isl_stream_next_token(s);
1824 isl_stream_error(s, NULL, "unexpected EOF");
1827 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1828 if (tok->type == ':' ||
1829 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1830 isl_token_free(tok);
1831 map = isl_map_intersect(map,
1832 read_disjuncts(s, v, isl_basic_map_copy(bmap)));
1834 isl_stream_push_token(s, tok);
1836 isl_basic_map_free(bmap);
1840 isl_basic_map_free(bmap);
1844 static struct isl_obj obj_read_poly(struct isl_stream *s,
1845 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1847 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1848 struct isl_pw_qpolynomial *pwqp;
1849 struct isl_qpolynomial *qp;
1850 struct isl_map *map;
1851 struct isl_set *set;
1853 qp = read_term(s, bmap, v);
1854 map = read_optional_disjuncts(s, bmap, v);
1855 set = isl_map_range(map);
1857 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1859 vars_drop(v, v->n - n);
1865 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1866 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1868 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1869 struct isl_obj obj_p;
1870 isl_qpolynomial *qp;
1871 isl_qpolynomial_fold *fold = NULL;
1872 isl_pw_qpolynomial_fold *pwf;
1876 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1877 return obj_read_poly(s, bmap, v, n);
1879 if (isl_stream_eat(s, '('))
1882 qp = read_term(s, bmap, v);
1883 fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1885 while (isl_stream_eat_if_available(s, ',')) {
1886 isl_qpolynomial_fold *fold_i;
1887 qp = read_term(s, bmap, v);
1888 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1889 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1892 if (isl_stream_eat(s, ')'))
1895 map = read_optional_disjuncts(s, bmap, v);
1896 set = isl_map_range(map);
1897 pwf = isl_pw_qpolynomial_fold_alloc(isl_fold_max, set, fold);
1899 vars_drop(v, v->n - n);
1904 isl_basic_map_free(bmap);
1905 isl_qpolynomial_fold_free(fold);
1906 obj.type = isl_obj_none;
1910 static int is_rational(struct isl_stream *s)
1912 struct isl_token *tok;
1914 tok = isl_stream_next_token(s);
1917 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1918 isl_token_free(tok);
1919 isl_stream_eat(s, ':');
1923 isl_stream_push_token(s, tok);
1928 static struct isl_obj obj_read_body(struct isl_stream *s,
1929 __isl_take isl_basic_map *bmap, struct vars *v)
1931 struct isl_map *map = NULL;
1932 struct isl_token *tok;
1933 struct isl_obj obj = { isl_obj_set, NULL };
1937 bmap = isl_basic_map_set_rational(bmap);
1939 if (!next_is_tuple(s))
1940 return obj_read_poly_or_fold(s, bmap, v, n);
1942 bmap = read_tuple(s, bmap, isl_dim_in, v);
1945 tok = isl_stream_next_token(s);
1946 if (tok && tok->type == ISL_TOKEN_TO) {
1947 obj.type = isl_obj_map;
1948 isl_token_free(tok);
1949 if (!next_is_tuple(s)) {
1950 bmap = isl_basic_map_reverse(bmap);
1951 return obj_read_poly_or_fold(s, bmap, v, n);
1953 bmap = read_tuple(s, bmap, isl_dim_out, v);
1957 bmap = isl_basic_map_reverse(bmap);
1959 isl_stream_push_token(s, tok);
1962 map = read_optional_disjuncts(s, bmap, v);
1964 vars_drop(v, v->n - n);
1969 isl_basic_map_free(bmap);
1970 obj.type = isl_obj_none;
1974 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1976 if (obj.type == isl_obj_map) {
1977 obj.v = isl_union_map_from_map(obj.v);
1978 obj.type = isl_obj_union_map;
1979 } else if (obj.type == isl_obj_set) {
1980 obj.v = isl_union_set_from_set(obj.v);
1981 obj.type = isl_obj_union_set;
1982 } else if (obj.type == isl_obj_pw_qpolynomial) {
1983 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1984 obj.type = isl_obj_union_pw_qpolynomial;
1985 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1986 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1987 obj.type = isl_obj_union_pw_qpolynomial_fold;
1989 isl_assert(ctx, 0, goto error);
1992 obj.type->free(obj.v);
1993 obj.type = isl_obj_none;
1997 static struct isl_obj obj_add(struct isl_ctx *ctx,
1998 struct isl_obj obj1, struct isl_obj obj2)
2000 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2001 obj1 = to_union(ctx, obj1);
2002 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2003 obj2 = to_union(ctx, obj2);
2004 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2005 obj1 = to_union(ctx, obj1);
2006 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2007 obj2 = to_union(ctx, obj2);
2008 if (obj1.type == isl_obj_pw_qpolynomial &&
2009 obj2.type == isl_obj_union_pw_qpolynomial)
2010 obj1 = to_union(ctx, obj1);
2011 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2012 obj2.type == isl_obj_pw_qpolynomial)
2013 obj2 = to_union(ctx, obj2);
2014 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2015 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2016 obj1 = to_union(ctx, obj1);
2017 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2018 obj2.type == isl_obj_pw_qpolynomial_fold)
2019 obj2 = to_union(ctx, obj2);
2020 isl_assert(ctx, obj1.type == obj2.type, goto error);
2021 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
2022 obj1 = to_union(ctx, obj1);
2023 obj2 = to_union(ctx, obj2);
2025 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
2026 obj1 = to_union(ctx, obj1);
2027 obj2 = to_union(ctx, obj2);
2029 if (obj1.type == isl_obj_pw_qpolynomial &&
2030 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
2031 obj1 = to_union(ctx, obj1);
2032 obj2 = to_union(ctx, obj2);
2034 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2035 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
2036 obj1 = to_union(ctx, obj1);
2037 obj2 = to_union(ctx, obj2);
2039 obj1.v = obj1.type->add(obj1.v, obj2.v);
2042 obj1.type->free(obj1.v);
2043 obj2.type->free(obj2.v);
2044 obj1.type = isl_obj_none;
2049 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
2051 isl_basic_map *bmap = NULL;
2052 struct isl_token *tok;
2053 struct vars *v = NULL;
2054 struct isl_obj obj = { isl_obj_set, NULL };
2056 tok = next_token(s);
2058 isl_stream_error(s, NULL, "unexpected EOF");
2061 if (tok->type == ISL_TOKEN_VALUE) {
2062 struct isl_token *tok2;
2063 struct isl_map *map;
2065 tok2 = isl_stream_next_token(s);
2066 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2067 isl_int_is_neg(tok2->u.v)) {
2069 isl_stream_push_token(s, tok2);
2070 obj.type = isl_obj_int;
2071 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
2072 isl_token_free(tok);
2075 isl_stream_push_token(s, tok2);
2076 isl_stream_push_token(s, tok);
2077 map = map_read_polylib(s, nparam);
2080 if (isl_map_dim(map, isl_dim_in) > 0)
2081 obj.type = isl_obj_map;
2085 v = vars_new(s->ctx);
2087 isl_stream_push_token(s, tok);
2090 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
2091 if (tok->type == '[') {
2092 isl_stream_push_token(s, tok);
2093 bmap = read_tuple(s, bmap, isl_dim_param, v);
2097 isl_assert(s->ctx, nparam == v->n, goto error);
2098 tok = isl_stream_next_token(s);
2099 if (!tok || tok->type != ISL_TOKEN_TO) {
2100 isl_stream_error(s, tok, "expecting '->'");
2102 isl_stream_push_token(s, tok);
2105 isl_token_free(tok);
2106 tok = isl_stream_next_token(s);
2107 } else if (nparam > 0)
2108 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
2109 if (!tok || tok->type != '{') {
2110 isl_stream_error(s, tok, "expecting '{'");
2112 isl_stream_push_token(s, tok);
2115 isl_token_free(tok);
2117 tok = isl_stream_next_token(s);
2120 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2121 isl_token_free(tok);
2122 if (isl_stream_eat(s, '='))
2124 bmap = read_tuple(s, bmap, isl_dim_param, v);
2128 isl_assert(s->ctx, nparam == v->n, goto error);
2129 } else if (tok->type == '}') {
2130 obj.type = isl_obj_union_set;
2131 obj.v = isl_union_set_empty(isl_basic_map_get_dim(bmap));
2132 isl_token_free(tok);
2135 isl_stream_push_token(s, tok);
2140 o = obj_read_body(s, isl_basic_map_copy(bmap), v);
2141 if (o.type == isl_obj_none || !o.v)
2146 obj = obj_add(s->ctx, obj, o);
2147 if (obj.type == isl_obj_none || !obj.v)
2150 tok = isl_stream_next_token(s);
2151 if (!tok || tok->type != ';')
2153 isl_token_free(tok);
2154 if (isl_stream_next_token_is(s, '}')) {
2155 tok = isl_stream_next_token(s);
2160 if (tok && tok->type == '}') {
2161 isl_token_free(tok);
2163 isl_stream_error(s, tok, "unexpected isl_token");
2165 isl_token_free(tok);
2170 isl_basic_map_free(bmap);
2174 isl_basic_map_free(bmap);
2175 obj.type->free(obj.v);
2182 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
2184 return obj_read(s, -1);
2187 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
2190 struct isl_map *map;
2192 obj = obj_read(s, nparam);
2194 isl_assert(s->ctx, obj.type == isl_obj_map ||
2195 obj.type == isl_obj_set, goto error);
2199 obj.type->free(obj.v);
2203 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
2206 struct isl_set *set;
2208 obj = obj_read(s, nparam);
2210 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2214 obj.type->free(obj.v);
2218 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2221 isl_union_map *umap;
2223 obj = obj_read(s, -1);
2224 if (obj.type == isl_obj_map) {
2225 obj.type = isl_obj_union_map;
2226 obj.v = isl_union_map_from_map(obj.v);
2228 if (obj.type == isl_obj_set) {
2229 obj.type = isl_obj_union_set;
2230 obj.v = isl_union_set_from_set(obj.v);
2233 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2234 obj.type == isl_obj_union_set, goto error);
2238 obj.type->free(obj.v);
2242 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2245 isl_union_set *uset;
2247 obj = obj_read(s, -1);
2248 if (obj.type == isl_obj_set) {
2249 obj.type = isl_obj_union_set;
2250 obj.v = isl_union_set_from_set(obj.v);
2253 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2257 obj.type->free(obj.v);
2261 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2264 struct isl_map *map;
2265 struct isl_basic_map *bmap;
2267 obj = obj_read(s, nparam);
2272 isl_assert(map->ctx, map->n <= 1, goto error);
2275 bmap = isl_basic_map_empty_like_map(map);
2277 bmap = isl_basic_map_copy(map->p[0]);
2287 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2288 FILE *input, int nparam)
2290 struct isl_basic_map *bmap;
2291 struct isl_stream *s = isl_stream_new_file(ctx, input);
2294 bmap = basic_map_read(s, nparam);
2299 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2300 FILE *input, int nparam)
2302 struct isl_basic_map *bmap;
2303 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
2306 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2307 return (struct isl_basic_set *)bmap;
2309 isl_basic_map_free(bmap);
2313 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2314 const char *str, int nparam)
2316 struct isl_basic_map *bmap;
2317 struct isl_stream *s = isl_stream_new_str(ctx, str);
2320 bmap = basic_map_read(s, nparam);
2325 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2326 const char *str, int nparam)
2328 struct isl_basic_map *bmap;
2329 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
2332 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2333 return (struct isl_basic_set *)bmap;
2335 isl_basic_map_free(bmap);
2339 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2340 FILE *input, int nparam)
2342 struct isl_map *map;
2343 struct isl_stream *s = isl_stream_new_file(ctx, input);
2346 map = isl_stream_read_map(s, nparam);
2351 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2352 const char *str, int nparam)
2354 struct isl_map *map;
2355 struct isl_stream *s = isl_stream_new_str(ctx, str);
2358 map = isl_stream_read_map(s, nparam);
2363 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2364 FILE *input, int nparam)
2366 struct isl_map *map;
2367 map = isl_map_read_from_file(ctx, input, nparam);
2370 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2371 return (struct isl_set *)map;
2377 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2378 const char *str, int nparam)
2380 struct isl_map *map;
2381 map = isl_map_read_from_str(ctx, str, nparam);
2384 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2385 return (struct isl_set *)map;
2391 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2394 isl_union_map *umap;
2395 struct isl_stream *s = isl_stream_new_file(ctx, input);
2398 umap = isl_stream_read_union_map(s);
2403 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2406 isl_union_map *umap;
2407 struct isl_stream *s = isl_stream_new_str(ctx, str);
2410 umap = isl_stream_read_union_map(s);
2415 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2418 isl_union_set *uset;
2419 struct isl_stream *s = isl_stream_new_file(ctx, input);
2422 uset = isl_stream_read_union_set(s);
2427 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2430 isl_union_set *uset;
2431 struct isl_stream *s = isl_stream_new_str(ctx, str);
2434 uset = isl_stream_read_union_set(s);
2439 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2441 struct isl_vec *vec = NULL;
2442 struct isl_token *tok;
2446 tok = isl_stream_next_token(s);
2447 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2448 isl_stream_error(s, tok, "expecting vector length");
2452 size = isl_int_get_si(tok->u.v);
2453 isl_token_free(tok);
2455 vec = isl_vec_alloc(s->ctx, size);
2457 for (j = 0; j < size; ++j) {
2458 tok = isl_stream_next_token(s);
2459 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2460 isl_stream_error(s, tok, "expecting constant value");
2463 isl_int_set(vec->el[j], tok->u.v);
2464 isl_token_free(tok);
2469 isl_token_free(tok);
2474 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2476 return isl_vec_read_polylib(s);
2479 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2482 struct isl_stream *s = isl_stream_new_file(ctx, input);
2490 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2491 struct isl_stream *s)
2494 struct isl_pw_qpolynomial *pwqp;
2496 obj = obj_read(s, -1);
2498 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2503 obj.type->free(obj.v);
2507 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2510 isl_pw_qpolynomial *pwqp;
2511 struct isl_stream *s = isl_stream_new_str(ctx, str);
2514 pwqp = isl_stream_read_pw_qpolynomial(s);