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
20 #include "isl_stream.h"
21 #include "isl_map_private.h"
23 #include "isl_polynomial_private.h"
28 struct variable *next;
37 static struct vars *vars_new(struct isl_ctx *ctx)
40 v = isl_alloc_type(ctx, struct vars);
49 static void variable_free(struct variable *var)
52 struct variable *next = var->next;
59 static void vars_free(struct vars *v)
67 static void vars_drop(struct vars *v, int n)
78 struct variable *next = var->next;
86 static struct variable *variable_new(struct vars *v, const char *name, int len,
90 var = isl_alloc_type(v->ctx, struct variable);
93 var->name = strdup(name);
94 var->name[len] = '\0';
103 static int vars_pos(struct vars *v, const char *s, int len)
110 for (q = v->v; q; q = q->next) {
111 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
118 v->v = variable_new(v, s, len, v->n);
126 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
127 enum isl_dim_type type, unsigned pos, char *name)
136 prime = strchr(name, '\'');
139 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, name);
148 isl_basic_map_free(bmap);
152 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
154 struct isl_token *tok;
156 tok = isl_stream_next_token(s);
157 if (!tok || tok->type != ISL_TOKEN_VALUE) {
158 isl_stream_error(s, tok, "expecting constant value");
162 isl_int_mul(*f, *f, tok->u.v);
166 if (isl_stream_eat_if_available(s, '*'))
167 return accept_cst_factor(s, f);
175 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v);
177 static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
180 struct isl_token *tok = NULL;
183 tok = isl_stream_next_token(s);
185 isl_stream_error(s, NULL, "unexpected EOF");
188 if (tok->type == ISL_TOKEN_IDENT) {
190 int pos = vars_pos(v, tok->u.s, -1);
194 isl_stream_error(s, tok, "unknown identifier");
198 aff = isl_vec_alloc(v->ctx, 1 + v->n);
201 isl_seq_clr(aff->el, aff->size);
202 isl_int_set_si(aff->el[1 + pos], 1);
204 } else if (tok->type == ISL_TOKEN_VALUE) {
205 if (isl_stream_eat_if_available(s, '*')) {
206 aff = accept_affine_factor(s, v);
207 aff = isl_vec_scale(aff, tok->u.v);
209 aff = isl_vec_alloc(v->ctx, 1 + v->n);
212 isl_seq_clr(aff->el, aff->size);
213 isl_int_set(aff->el[0], tok->u.v);
216 } else if (tok->type == '(') {
219 aff = accept_affine(s, v);
222 if (isl_stream_eat(s, ')'))
225 isl_stream_error(s, tok, "expecting factor");
228 if (isl_stream_eat_if_available(s, '*')) {
231 isl_int_set_si(f, 1);
232 if (accept_cst_factor(s, &f) < 0) {
236 aff = isl_vec_scale(aff, f);
247 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
249 struct isl_token *tok = NULL;
253 aff = isl_vec_alloc(v->ctx, 1 + v->n);
256 isl_seq_clr(aff->el, aff->size);
259 tok = isl_stream_next_token(s);
261 isl_stream_error(s, NULL, "unexpected EOF");
264 if (tok->type == '-') {
269 if (tok->type == '(' || tok->type == ISL_TOKEN_IDENT) {
271 isl_stream_push_token(s, tok);
273 aff2 = accept_affine_factor(s, v);
275 aff2 = isl_vec_scale(aff2, s->ctx->negone);
276 aff = isl_vec_add(aff, aff2);
280 } else if (tok->type == ISL_TOKEN_VALUE) {
282 isl_int_neg(tok->u.v, tok->u.v);
283 if (isl_stream_eat_if_available(s, '*') ||
284 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
286 aff2 = accept_affine_factor(s, v);
287 aff2 = isl_vec_scale(aff2, tok->u.v);
288 aff = isl_vec_add(aff, aff2);
292 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
298 tok = isl_stream_next_token(s);
299 if (tok && tok->type == '-') {
302 } else if (tok && tok->type == '+') {
305 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
306 isl_int_is_neg(tok->u.v)) {
307 isl_stream_push_token(s, tok);
310 isl_stream_push_token(s, tok);
322 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
323 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
328 vec = accept_affine(s, v);
331 v->v = variable_new(v, "", 0, v->n);
335 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
336 k = isl_basic_map_alloc_equality(bmap);
338 isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
339 isl_int_set_si(bmap->eq[k][vec->size], -1);
347 isl_basic_map_free(bmap);
351 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
352 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
355 struct isl_token *tok;
357 while ((tok = isl_stream_next_token(s)) != NULL) {
360 if (tok->type == ISL_TOKEN_IDENT) {
362 int p = vars_pos(v, tok->u.s, -1);
369 bmap = isl_basic_map_add(bmap, type, 1);
370 bmap = set_name(bmap, type, i, v->v->name);
372 } else if (tok->type == ISL_TOKEN_IDENT ||
373 tok->type == ISL_TOKEN_VALUE) {
374 if (type == isl_dim_param) {
375 isl_stream_error(s, tok,
376 "expecting unique identifier");
379 isl_stream_push_token(s, tok);
381 bmap = isl_basic_map_add(bmap, type, 1);
382 bmap = read_var_def(s, bmap, type, v);
386 tok = isl_stream_next_token(s);
387 if (!tok || tok->type != ',')
394 isl_stream_push_token(s, tok);
399 isl_basic_map_free(bmap);
403 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
408 struct isl_token *tok = NULL;
410 vec = accept_affine(s, v);
411 mat = isl_mat_from_row_vec(vec);
416 tok = isl_stream_next_token(s);
418 isl_stream_error(s, NULL, "unexpected EOF");
421 if (tok->type != ',') {
422 isl_stream_push_token(s, tok);
427 vec = accept_affine(s, v);
428 mat = isl_mat_vec_concat(mat, vec);
439 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
440 struct vars *v, struct isl_basic_map *bmap, int k)
442 struct isl_token *tok;
446 if (isl_stream_eat(s, '['))
449 tok = isl_stream_next_token(s);
452 if (tok->type == '(') {
456 isl_stream_push_token(s, tok);
458 aff = accept_affine(s, v);
462 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
466 if (seen_paren && isl_stream_eat(s, ')'))
468 if (isl_stream_eat(s, '/'))
471 tok = isl_stream_next_token(s);
474 if (tok->type != ISL_TOKEN_VALUE) {
475 isl_stream_error(s, tok, "expected denominator");
476 isl_stream_push_token(s, tok);
479 isl_int_set(bmap->div[k][0], tok->u.v);
482 if (isl_stream_eat(s, ']'))
485 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
490 isl_basic_map_free(bmap);
494 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
495 struct vars *v, struct isl_basic_map *bmap)
497 struct isl_token *tok;
499 while ((tok = isl_stream_next_token(s)) != NULL) {
503 unsigned total = isl_basic_map_total_dim(bmap);
505 if (tok->type != ISL_TOKEN_IDENT)
508 p = vars_pos(v, tok->u.s, -1);
512 isl_stream_error(s, tok, "expecting unique identifier");
516 bmap = isl_basic_map_cow(bmap);
517 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
520 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
522 isl_seq_clr(bmap->div[k], 1 + 1 + total);
525 tok = isl_stream_next_token(s);
526 if (tok && tok->type == '=') {
528 bmap = add_div_definition(s, v, bmap, k);
529 tok = isl_stream_next_token(s);
532 if (!tok || tok->type != ',')
538 isl_stream_push_token(s, tok);
543 isl_basic_map_free(bmap);
547 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
548 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
550 struct isl_token *tok;
553 tok = isl_stream_next_token(s);
554 if (tok && tok->type == ISL_TOKEN_IDENT) {
555 name = strdup(tok->u.s);
559 tok = isl_stream_next_token(s);
561 if (!tok || tok->type != '[') {
562 isl_stream_error(s, tok, "expecting '['");
566 bmap = read_var_list(s, bmap, type, v);
567 tok = isl_stream_next_token(s);
568 if (!tok || tok->type != ']') {
569 isl_stream_error(s, tok, "expecting ']'");
575 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
583 isl_basic_map_free(bmap);
587 static struct isl_basic_map *add_constraints(struct isl_stream *s,
588 struct vars *v, struct isl_basic_map *bmap);
590 static struct isl_basic_map *add_exists(struct isl_stream *s,
591 struct vars *v, struct isl_basic_map *bmap)
593 struct isl_token *tok;
600 tok = isl_stream_next_token(s);
603 if (tok->type == '(') {
607 isl_stream_push_token(s, tok);
609 bmap = read_defined_var_list(s, v, bmap);
613 if (isl_stream_eat(s, ':'))
615 bmap = add_constraints(s, v, bmap);
616 if (seen_paren && isl_stream_eat(s, ')'))
620 isl_basic_map_free(bmap);
624 static __isl_give isl_basic_map *construct_constraint(
625 __isl_take isl_basic_map *bmap, enum isl_token_type type,
626 isl_int *left, isl_int *right)
634 len = 1 + isl_basic_map_total_dim(bmap);
637 k = isl_basic_map_alloc_inequality(bmap);
640 if (type == ISL_TOKEN_LE)
641 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
644 else if (type == ISL_TOKEN_GE)
645 isl_seq_combine(bmap->ineq[k], ctx->one, left,
648 else if (type == ISL_TOKEN_LT) {
649 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
652 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
653 } else if (type == ISL_TOKEN_GT) {
654 isl_seq_combine(bmap->ineq[k], ctx->one, left,
657 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
659 isl_seq_combine(bmap->ineq[k], ctx->one, left,
662 isl_basic_map_inequality_to_equality(bmap, k);
667 isl_basic_map_free(bmap);
671 static int is_comparator(struct isl_token *tok)
688 static struct isl_basic_map *add_constraint(struct isl_stream *s,
689 struct vars *v, struct isl_basic_map *bmap)
692 unsigned total = isl_basic_map_total_dim(bmap);
693 struct isl_token *tok = NULL;
694 struct isl_mat *aff1 = NULL, *aff2 = NULL;
696 tok = isl_stream_next_token(s);
699 if (tok->type == ISL_TOKEN_EXISTS) {
701 return add_exists(s, v, bmap);
703 isl_stream_push_token(s, tok);
706 bmap = isl_basic_map_cow(bmap);
708 aff1 = accept_affine_list(s, v);
711 tok = isl_stream_next_token(s);
712 if (!is_comparator(tok)) {
713 isl_stream_error(s, tok, "missing operator");
715 isl_stream_push_token(s, tok);
719 isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
721 aff2 = accept_affine_list(s, v);
724 isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
726 bmap = isl_basic_map_extend_constraints(bmap, 0,
727 aff1->n_row * aff2->n_row);
728 for (i = 0; i < aff1->n_row; ++i)
729 for (j = 0; j < aff2->n_row; ++j)
730 bmap = construct_constraint(bmap, tok->type,
731 aff1->row[i], aff2->row[j]);
736 tok = isl_stream_next_token(s);
737 if (!is_comparator(tok)) {
739 isl_stream_push_token(s, tok);
751 isl_basic_map_free(bmap);
755 static struct isl_basic_map *add_constraints(struct isl_stream *s,
756 struct vars *v, struct isl_basic_map *bmap)
758 struct isl_token *tok;
761 bmap = add_constraint(s, v, bmap);
764 tok = isl_stream_next_token(s);
766 isl_stream_error(s, NULL, "unexpected EOF");
769 if (tok->type != ISL_TOKEN_AND)
773 isl_stream_push_token(s, tok);
779 isl_basic_map_free(bmap);
783 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
784 struct vars *v, __isl_take isl_dim *dim)
787 struct isl_token *tok;
788 struct isl_basic_map *bmap;
790 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
794 tok = isl_stream_next_token(s);
797 if (tok->type == '(') {
801 isl_stream_push_token(s, tok);
803 bmap = add_constraints(s, v, bmap);
804 bmap = isl_basic_map_simplify(bmap);
805 bmap = isl_basic_map_finalize(bmap);
807 if (seen_paren && isl_stream_eat(s, ')'))
812 isl_basic_map_free(bmap);
816 static struct isl_map *read_disjuncts(struct isl_stream *s,
817 struct vars *v, __isl_take isl_dim *dim)
819 struct isl_token *tok;
822 tok = isl_stream_next_token(s);
824 isl_stream_error(s, NULL, "unexpected EOF");
827 if (tok->type == '}') {
828 isl_stream_push_token(s, tok);
829 return isl_map_universe(dim);
831 isl_stream_push_token(s, tok);
833 map = isl_map_empty(isl_dim_copy(dim));
835 struct isl_basic_map *bmap;
838 bmap = read_disjunct(s, v, isl_dim_copy(dim));
839 map = isl_map_union(map, isl_map_from_basic_map(bmap));
841 vars_drop(v, v->n - n);
843 tok = isl_stream_next_token(s);
844 if (!tok || tok->type != ISL_TOKEN_OR)
849 isl_stream_push_token(s, tok);
858 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
860 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
861 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
862 isl_basic_map_dim(bmap, isl_dim_in) + pos;
863 pos -= isl_basic_map_dim(bmap, isl_dim_out);
865 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
866 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
867 pos -= isl_basic_map_dim(bmap, isl_dim_in);
869 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
870 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
871 isl_basic_map_dim(bmap, isl_dim_in) +
872 isl_basic_map_dim(bmap, isl_dim_out) + pos;
873 pos -= isl_basic_map_dim(bmap, isl_dim_div);
875 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
881 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
882 struct isl_stream *s, __isl_take isl_basic_map *bmap)
885 struct isl_token *tok;
895 nparam = isl_basic_map_dim(bmap, isl_dim_param);
896 dim = isl_basic_map_dim(bmap, isl_dim_out);
898 tok = isl_stream_next_token(s);
899 if (!tok || tok->type != ISL_TOKEN_VALUE) {
900 isl_stream_error(s, tok, "expecting coefficient");
902 isl_stream_push_token(s, tok);
905 if (!tok->on_new_line) {
906 isl_stream_error(s, tok, "coefficient should appear on new line");
907 isl_stream_push_token(s, tok);
911 type = isl_int_get_si(tok->u.v);
914 isl_assert(s->ctx, type == 0 || type == 1, goto error);
916 k = isl_basic_map_alloc_equality(bmap);
919 k = isl_basic_map_alloc_inequality(bmap);
925 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
927 tok = isl_stream_next_token(s);
928 if (!tok || tok->type != ISL_TOKEN_VALUE) {
929 isl_stream_error(s, tok, "expecting coefficient");
931 isl_stream_push_token(s, tok);
934 if (tok->on_new_line) {
935 isl_stream_error(s, tok,
936 "coefficient should not appear on new line");
937 isl_stream_push_token(s, tok);
940 pos = polylib_pos_to_isl_pos(bmap, j);
941 isl_int_set(c[pos], tok->u.v);
947 isl_basic_map_free(bmap);
951 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
955 struct isl_token *tok;
956 struct isl_token *tok2;
959 unsigned in = 0, out, local = 0;
960 struct isl_basic_map *bmap = NULL;
965 tok = isl_stream_next_token(s);
967 isl_stream_error(s, NULL, "unexpected EOF");
970 tok2 = isl_stream_next_token(s);
973 isl_stream_error(s, NULL, "unexpected EOF");
976 n_row = isl_int_get_si(tok->u.v);
977 n_col = isl_int_get_si(tok2->u.v);
978 on_new_line = tok2->on_new_line;
979 isl_token_free(tok2);
981 isl_assert(s->ctx, !on_new_line, return NULL);
982 isl_assert(s->ctx, n_row >= 0, return NULL);
983 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
984 tok = isl_stream_next_token_on_same_line(s);
986 if (tok->type != ISL_TOKEN_VALUE) {
987 isl_stream_error(s, tok,
988 "expecting number of output dimensions");
989 isl_stream_push_token(s, tok);
992 out = isl_int_get_si(tok->u.v);
995 tok = isl_stream_next_token_on_same_line(s);
996 if (!tok || tok->type != ISL_TOKEN_VALUE) {
997 isl_stream_error(s, tok,
998 "expecting number of input dimensions");
1000 isl_stream_push_token(s, tok);
1003 in = isl_int_get_si(tok->u.v);
1004 isl_token_free(tok);
1006 tok = isl_stream_next_token_on_same_line(s);
1007 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1008 isl_stream_error(s, tok,
1009 "expecting number of existentials");
1011 isl_stream_push_token(s, tok);
1014 local = isl_int_get_si(tok->u.v);
1015 isl_token_free(tok);
1017 tok = isl_stream_next_token_on_same_line(s);
1018 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1019 isl_stream_error(s, tok,
1020 "expecting number of parameters");
1022 isl_stream_push_token(s, tok);
1025 nparam = isl_int_get_si(tok->u.v);
1026 isl_token_free(tok);
1027 if (n_col != 1 + out + in + local + nparam + 1) {
1028 isl_stream_error(s, NULL,
1029 "dimensions don't match");
1033 out = n_col - 2 - nparam;
1034 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1038 for (i = 0; i < local; ++i) {
1039 int k = isl_basic_map_alloc_div(bmap);
1044 for (i = 0; i < n_row; ++i)
1045 bmap = basic_map_read_polylib_constraint(s, bmap);
1047 tok = isl_stream_next_token_on_same_line(s);
1049 isl_stream_error(s, tok, "unexpected extra token on line");
1050 isl_stream_push_token(s, tok);
1054 bmap = isl_basic_map_simplify(bmap);
1055 bmap = isl_basic_map_finalize(bmap);
1058 isl_basic_map_free(bmap);
1062 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1064 struct isl_token *tok;
1065 struct isl_token *tok2;
1067 struct isl_map *map;
1069 tok = isl_stream_next_token(s);
1071 isl_stream_error(s, NULL, "unexpected EOF");
1074 tok2 = isl_stream_next_token_on_same_line(s);
1076 isl_stream_push_token(s, tok2);
1077 isl_stream_push_token(s, tok);
1078 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1080 n = isl_int_get_si(tok->u.v);
1081 isl_token_free(tok);
1083 isl_assert(s->ctx, n >= 1, return NULL);
1085 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1087 for (i = 1; i < n; ++i)
1088 map = isl_map_union(map,
1089 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1094 static int optional_power(struct isl_stream *s)
1097 struct isl_token *tok;
1099 tok = isl_stream_next_token(s);
1102 if (tok->type != '^') {
1103 isl_stream_push_token(s, tok);
1106 isl_token_free(tok);
1107 tok = isl_stream_next_token(s);
1108 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1109 isl_stream_error(s, tok, "expecting exponent");
1111 isl_stream_push_token(s, tok);
1114 pow = isl_int_get_si(tok->u.v);
1115 isl_token_free(tok);
1119 static __isl_give isl_div *read_div(struct isl_stream *s,
1120 __isl_keep isl_basic_map *bmap, struct vars *v)
1122 unsigned total = isl_basic_map_total_dim(bmap);
1125 bmap = isl_basic_map_copy(bmap);
1126 bmap = isl_basic_map_cow(bmap);
1127 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1130 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
1132 isl_seq_clr(bmap->div[k], 1 + 1 + total);
1133 bmap = add_div_definition(s, v, bmap, k);
1135 return isl_basic_map_div(bmap, k);
1137 isl_basic_map_free(bmap);
1141 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1142 __isl_keep isl_basic_map *bmap, struct vars *v);
1144 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1145 __isl_keep isl_basic_map *bmap, struct vars *v)
1147 struct isl_qpolynomial *qp;
1148 struct isl_token *tok;
1150 tok = isl_stream_next_token(s);
1152 isl_stream_error(s, NULL, "unexpected EOF");
1155 if (tok->type == '(') {
1156 isl_token_free(tok);
1157 qp = read_term(s, bmap, v);
1160 if (isl_stream_eat(s, ')'))
1162 } else if (tok->type == ISL_TOKEN_VALUE) {
1163 struct isl_token *tok2;
1164 tok2 = isl_stream_next_token(s);
1165 if (tok2 && tok2->type == '/') {
1166 isl_token_free(tok2);
1167 tok2 = isl_stream_next_token(s);
1168 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1169 isl_stream_error(s, tok2, "expected denominator");
1170 isl_token_free(tok);
1171 isl_token_free(tok2);
1174 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1175 tok->u.v, tok2->u.v);
1176 isl_token_free(tok2);
1178 isl_stream_push_token(s, tok2);
1179 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1182 isl_token_free(tok);
1183 } else if (tok->type == ISL_TOKEN_INFTY) {
1184 isl_token_free(tok);
1185 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1186 } else if (tok->type == ISL_TOKEN_NAN) {
1187 isl_token_free(tok);
1188 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1189 } else if (tok->type == ISL_TOKEN_IDENT) {
1191 int pos = vars_pos(v, tok->u.s, -1);
1193 isl_token_free(tok);
1197 isl_stream_error(s, tok, "unknown identifier");
1200 pow = optional_power(s);
1201 qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
1202 } else if (tok->type == '[') {
1206 isl_stream_push_token(s, tok);
1207 div = read_div(s, bmap, v);
1208 pow = optional_power(s);
1209 qp = isl_qpolynomial_div_pow(div, pow);
1210 } else if (tok->type == '-') {
1211 struct isl_qpolynomial *qp2;
1213 isl_token_free(tok);
1214 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1216 qp2 = read_factor(s, bmap, v);
1217 qp = isl_qpolynomial_mul(qp, qp2);
1219 isl_stream_error(s, tok, "unexpected isl_token");
1220 isl_stream_push_token(s, tok);
1224 if (isl_stream_eat_if_available(s, '*') ||
1225 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1226 struct isl_qpolynomial *qp2;
1228 qp2 = read_factor(s, bmap, v);
1229 qp = isl_qpolynomial_mul(qp, qp2);
1234 isl_qpolynomial_free(qp);
1238 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1239 __isl_keep isl_basic_map *bmap, struct vars *v)
1241 struct isl_token *tok;
1242 struct isl_qpolynomial *qp;
1244 qp = read_factor(s, bmap, v);
1247 tok = isl_stream_next_token(s);
1251 if (tok->type == '+') {
1252 struct isl_qpolynomial *qp2;
1254 isl_token_free(tok);
1255 qp2 = read_factor(s, bmap, v);
1256 qp = isl_qpolynomial_add(qp, qp2);
1257 } else if (tok->type == '-') {
1258 struct isl_qpolynomial *qp2;
1260 isl_token_free(tok);
1261 qp2 = read_factor(s, bmap, v);
1262 qp = isl_qpolynomial_sub(qp, qp2);
1263 } else if (tok->type == ISL_TOKEN_VALUE &&
1264 isl_int_is_neg(tok->u.v)) {
1265 struct isl_qpolynomial *qp2;
1267 qp2 = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1269 isl_token_free(tok);
1270 qp = isl_qpolynomial_add(qp, qp2);
1272 isl_stream_push_token(s, tok);
1280 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1281 __isl_take isl_basic_map *bmap, struct vars *v)
1283 struct isl_token *tok;
1284 struct isl_map *map;
1286 tok = isl_stream_next_token(s);
1288 isl_stream_error(s, NULL, "unexpected EOF");
1291 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1292 if (tok->type == ':') {
1293 isl_token_free(tok);
1294 map = isl_map_intersect(map,
1295 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1297 isl_stream_push_token(s, tok);
1299 isl_basic_map_free(bmap);
1303 isl_basic_map_free(bmap);
1307 static struct isl_obj obj_read_poly(struct isl_stream *s,
1308 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1310 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1311 struct isl_pw_qpolynomial *pwqp;
1312 struct isl_qpolynomial *qp;
1313 struct isl_map *map;
1314 struct isl_set *set;
1316 bmap = isl_basic_map_reverse(bmap);
1318 qp = read_term(s, bmap, v);
1319 map = read_optional_disjuncts(s, bmap, v);
1320 set = isl_map_range(map);
1322 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1324 vars_drop(v, v->n - n);
1330 static int next_is_tuple(struct isl_stream *s)
1332 struct isl_token *tok;
1335 tok = isl_stream_next_token(s);
1338 if (tok->type == '[') {
1339 isl_stream_push_token(s, tok);
1342 if (tok->type != ISL_TOKEN_IDENT) {
1343 isl_stream_push_token(s, tok);
1347 is_tuple = isl_stream_next_token_is(s, '[');
1349 isl_stream_push_token(s, tok);
1354 static struct isl_obj obj_read_body(struct isl_stream *s,
1355 __isl_take isl_basic_map *bmap, struct vars *v, isl_obj_type type)
1357 struct isl_map *map = NULL;
1358 struct isl_token *tok;
1359 struct isl_obj obj = { type, NULL };
1362 if (!next_is_tuple(s))
1363 return obj_read_poly(s, bmap, v, n);
1365 bmap = read_tuple(s, bmap, isl_dim_in, v);
1368 tok = isl_stream_next_token(s);
1369 if (tok && tok->type == ISL_TOKEN_TO) {
1370 obj.type = isl_obj_map;
1371 isl_token_free(tok);
1372 if (!next_is_tuple(s))
1373 return obj_read_poly(s, bmap, v, n);
1374 bmap = read_tuple(s, bmap, isl_dim_out, v);
1378 bmap = isl_basic_map_reverse(bmap);
1380 isl_stream_push_token(s, tok);
1383 map = read_optional_disjuncts(s, bmap, v);
1385 vars_drop(v, v->n - n);
1390 isl_basic_map_free(bmap);
1391 obj.type = isl_obj_none;
1395 static struct isl_obj obj_add(struct isl_ctx *ctx,
1396 struct isl_obj obj1, struct isl_obj obj2)
1398 isl_assert(ctx, obj1.type == obj2.type, goto error);
1399 obj1.v = obj1.type->add(obj1.v, obj2.v);
1402 obj1.type->free(obj1.v);
1403 obj2.type->free(obj2.v);
1404 obj1.type = isl_obj_none;
1409 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1411 struct isl_basic_map *bmap = NULL;
1412 struct isl_token *tok;
1413 struct vars *v = NULL;
1414 struct isl_obj obj = { isl_obj_set, NULL };
1416 tok = isl_stream_next_token(s);
1418 isl_stream_error(s, NULL, "unexpected EOF");
1421 if (tok->type == ISL_TOKEN_VALUE) {
1422 struct isl_map *map;
1423 isl_stream_push_token(s, tok);
1424 map = map_read_polylib(s, nparam);
1427 if (isl_map_dim(map, isl_dim_in) > 0)
1428 obj.type = isl_obj_map;
1432 v = vars_new(s->ctx);
1434 isl_stream_push_token(s, tok);
1437 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1438 if (tok->type == '[') {
1439 isl_stream_push_token(s, tok);
1440 bmap = read_tuple(s, bmap, isl_dim_param, v);
1444 isl_assert(s->ctx, nparam == v->n, goto error);
1445 tok = isl_stream_next_token(s);
1446 if (!tok || tok->type != ISL_TOKEN_TO) {
1447 isl_stream_error(s, tok, "expecting '->'");
1449 isl_stream_push_token(s, tok);
1452 isl_token_free(tok);
1453 tok = isl_stream_next_token(s);
1454 } else if (nparam > 0)
1455 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1456 if (!tok || tok->type != '{') {
1457 isl_stream_error(s, tok, "expecting '{'");
1459 isl_stream_push_token(s, tok);
1462 isl_token_free(tok);
1464 tok = isl_stream_next_token(s);
1467 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1468 isl_token_free(tok);
1469 if (isl_stream_eat(s, '='))
1471 bmap = read_tuple(s, bmap, isl_dim_param, v);
1475 isl_assert(s->ctx, nparam == v->n, goto error);
1477 isl_stream_push_token(s, tok);
1482 o = obj_read_body(s, isl_basic_map_copy(bmap), v, obj.type);
1483 if (o.type == isl_obj_none)
1488 obj = obj_add(s->ctx, obj, o);
1489 if (obj.type == isl_obj_none)
1492 tok = isl_stream_next_token(s);
1493 if (!tok || tok->type != ';')
1495 isl_token_free(tok);
1498 if (tok && tok->type == '}') {
1499 isl_token_free(tok);
1501 isl_stream_error(s, tok, "unexpected isl_token");
1503 isl_token_free(tok);
1507 isl_basic_map_free(bmap);
1511 isl_basic_map_free(bmap);
1512 obj.type->free(obj.v);
1519 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1521 return obj_read(s, -1);
1524 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1527 struct isl_map *map;
1529 obj = obj_read(s, nparam);
1531 isl_assert(s->ctx, obj.type == isl_obj_map ||
1532 obj.type == isl_obj_set, goto error);
1536 obj.type->free(obj.v);
1540 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1543 struct isl_set *set;
1545 obj = obj_read(s, nparam);
1547 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1551 obj.type->free(obj.v);
1555 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1558 struct isl_map *map;
1559 struct isl_basic_map *bmap;
1561 obj = obj_read(s, nparam);
1566 isl_assert(map->ctx, map->n <= 1, goto error);
1569 bmap = isl_basic_map_empty_like_map(map);
1571 bmap = isl_basic_map_copy(map->p[0]);
1581 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1582 FILE *input, int nparam)
1584 struct isl_basic_map *bmap;
1585 struct isl_stream *s = isl_stream_new_file(ctx, input);
1588 bmap = basic_map_read(s, nparam);
1593 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1594 FILE *input, int nparam)
1596 struct isl_basic_map *bmap;
1597 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1600 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1601 return (struct isl_basic_set *)bmap;
1603 isl_basic_map_free(bmap);
1607 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1608 const char *str, int nparam)
1610 struct isl_basic_map *bmap;
1611 struct isl_stream *s = isl_stream_new_str(ctx, str);
1614 bmap = basic_map_read(s, nparam);
1619 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1620 const char *str, int nparam)
1622 struct isl_basic_map *bmap;
1623 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1626 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1627 return (struct isl_basic_set *)bmap;
1629 isl_basic_map_free(bmap);
1633 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1634 FILE *input, int nparam)
1636 struct isl_map *map;
1637 struct isl_stream *s = isl_stream_new_file(ctx, input);
1640 map = isl_stream_read_map(s, nparam);
1645 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1646 const char *str, int nparam)
1648 struct isl_map *map;
1649 struct isl_stream *s = isl_stream_new_str(ctx, str);
1652 map = isl_stream_read_map(s, nparam);
1657 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1658 FILE *input, int nparam)
1660 struct isl_map *map;
1661 map = isl_map_read_from_file(ctx, input, nparam);
1664 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1665 return (struct isl_set *)map;
1671 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1672 const char *str, int nparam)
1674 struct isl_map *map;
1675 map = isl_map_read_from_str(ctx, str, nparam);
1678 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1679 return (struct isl_set *)map;
1685 static char *next_line(FILE *input, char *line, unsigned len)
1690 if (!(p = fgets(line, len, input)))
1692 while (isspace(*p) && *p != '\n')
1694 } while (*p == '#' || *p == '\n');
1699 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1702 struct isl_vec *vec = NULL;
1711 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1712 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1714 vec = isl_vec_alloc(ctx, size);
1716 p = next_line(input, line, sizeof(line));
1717 isl_assert(ctx, p, goto error);
1719 for (j = 0; j < size; ++j) {
1720 n = sscanf(p, "%s%n", val, &offset);
1721 isl_assert(ctx, n != 0, goto error);
1722 isl_int_read(vec->el[j], val);
1732 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1733 FILE *input, unsigned input_format)
1735 if (input_format == ISL_FORMAT_POLYLIB)
1736 return isl_vec_read_from_file_polylib(ctx, input);
1738 isl_assert(ctx, 0, return NULL);
1741 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
1742 struct isl_stream *s)
1745 struct isl_pw_qpolynomial *pwqp;
1747 obj = obj_read(s, -1);
1749 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
1754 obj.type->free(obj.v);