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"
24 #include <isl_union_map.h>
29 struct variable *next;
38 static struct vars *vars_new(struct isl_ctx *ctx)
41 v = isl_alloc_type(ctx, struct vars);
50 static void variable_free(struct variable *var)
53 struct variable *next = var->next;
60 static void vars_free(struct vars *v)
68 static void vars_drop(struct vars *v, int n)
79 struct variable *next = var->next;
87 static struct variable *variable_new(struct vars *v, const char *name, int len,
91 var = isl_alloc_type(v->ctx, struct variable);
94 var->name = strdup(name);
95 var->name[len] = '\0';
104 static int vars_pos(struct vars *v, const char *s, int len)
111 for (q = v->v; q; q = q->next) {
112 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
119 v->v = variable_new(v, s, len, v->n);
127 static __isl_give isl_dim *set_name(__isl_take isl_dim *dim,
128 enum isl_dim_type type, unsigned pos, char *name)
137 prime = strchr(name, '\'');
140 dim = isl_dim_set_name(dim, type, pos, name);
147 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
149 struct isl_token *tok;
151 tok = isl_stream_next_token(s);
152 if (!tok || tok->type != ISL_TOKEN_VALUE) {
153 isl_stream_error(s, tok, "expecting constant value");
157 isl_int_mul(*f, *f, tok->u.v);
161 if (isl_stream_eat_if_available(s, '*'))
162 return accept_cst_factor(s, f);
170 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v);
172 static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
175 struct isl_token *tok = NULL;
178 tok = isl_stream_next_token(s);
180 isl_stream_error(s, NULL, "unexpected EOF");
183 if (tok->type == ISL_TOKEN_IDENT) {
185 int pos = vars_pos(v, tok->u.s, -1);
189 isl_stream_error(s, tok, "unknown identifier");
193 aff = isl_vec_alloc(v->ctx, 1 + v->n);
196 isl_seq_clr(aff->el, aff->size);
197 isl_int_set_si(aff->el[1 + pos], 1);
199 } else if (tok->type == ISL_TOKEN_VALUE) {
200 if (isl_stream_eat_if_available(s, '*')) {
201 aff = accept_affine_factor(s, v);
202 aff = isl_vec_scale(aff, tok->u.v);
204 aff = isl_vec_alloc(v->ctx, 1 + v->n);
207 isl_seq_clr(aff->el, aff->size);
208 isl_int_set(aff->el[0], tok->u.v);
211 } else if (tok->type == '(') {
214 aff = accept_affine(s, v);
217 if (isl_stream_eat(s, ')'))
220 isl_stream_error(s, tok, "expecting factor");
223 if (isl_stream_eat_if_available(s, '*')) {
226 isl_int_set_si(f, 1);
227 if (accept_cst_factor(s, &f) < 0) {
231 aff = isl_vec_scale(aff, f);
242 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
244 struct isl_token *tok = NULL;
248 aff = isl_vec_alloc(v->ctx, 1 + v->n);
251 isl_seq_clr(aff->el, aff->size);
254 tok = isl_stream_next_token(s);
256 isl_stream_error(s, NULL, "unexpected EOF");
259 if (tok->type == '-') {
264 if (tok->type == '(' || tok->type == ISL_TOKEN_IDENT) {
266 isl_stream_push_token(s, tok);
268 aff2 = accept_affine_factor(s, v);
270 aff2 = isl_vec_scale(aff2, s->ctx->negone);
271 aff = isl_vec_add(aff, aff2);
275 } else if (tok->type == ISL_TOKEN_VALUE) {
277 isl_int_neg(tok->u.v, tok->u.v);
278 if (isl_stream_eat_if_available(s, '*') ||
279 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
281 aff2 = accept_affine_factor(s, v);
282 aff2 = isl_vec_scale(aff2, tok->u.v);
283 aff = isl_vec_add(aff, aff2);
287 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
293 tok = isl_stream_next_token(s);
294 if (tok && tok->type == '-') {
297 } else if (tok && tok->type == '+') {
300 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
301 isl_int_is_neg(tok->u.v)) {
302 isl_stream_push_token(s, tok);
305 isl_stream_push_token(s, tok);
317 static __isl_give isl_mat *read_var_def(struct isl_stream *s,
318 __isl_take isl_mat *eq, enum isl_dim_type type, struct vars *v)
322 vec = accept_affine(s, v);
325 v->v = variable_new(v, "", 0, v->n);
329 eq = isl_mat_add_rows(eq, 1);
331 isl_seq_cpy(eq->row[eq->n_row - 1], vec->el, vec->size);
332 isl_int_set_si(eq->row[eq->n_row - 1][vec->size], -1);
342 static __isl_give isl_dim *read_var_list(struct isl_stream *s,
343 __isl_take isl_dim *dim, enum isl_dim_type type, struct vars *v,
344 __isl_keep isl_mat **eq)
347 struct isl_token *tok;
349 while ((tok = isl_stream_next_token(s)) != NULL) {
352 if (tok->type == ISL_TOKEN_IDENT) {
354 int p = vars_pos(v, tok->u.s, -1);
361 dim = isl_dim_add(dim, type, 1);
363 *eq = isl_mat_add_zero_cols(*eq, 1);
364 dim = set_name(dim, type, i, v->v->name);
366 } else if (tok->type == ISL_TOKEN_IDENT ||
367 tok->type == ISL_TOKEN_VALUE) {
368 if (type == isl_dim_param) {
369 isl_stream_error(s, tok,
370 "expecting unique identifier");
373 isl_stream_push_token(s, tok);
375 dim = isl_dim_add(dim, type, 1);
376 *eq = isl_mat_add_zero_cols(*eq, 1);
377 *eq = read_var_def(s, *eq, type, v);
381 tok = isl_stream_next_token(s);
382 if (!tok || tok->type != ',')
389 isl_stream_push_token(s, tok);
398 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
403 struct isl_token *tok = NULL;
405 vec = accept_affine(s, v);
406 mat = isl_mat_from_row_vec(vec);
411 tok = isl_stream_next_token(s);
413 isl_stream_error(s, NULL, "unexpected EOF");
416 if (tok->type != ',') {
417 isl_stream_push_token(s, tok);
422 vec = accept_affine(s, v);
423 mat = isl_mat_vec_concat(mat, vec);
434 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
435 struct vars *v, struct isl_basic_map *bmap, int k)
437 struct isl_token *tok;
441 if (isl_stream_eat(s, '['))
444 tok = isl_stream_next_token(s);
447 if (tok->type == '(') {
451 isl_stream_push_token(s, tok);
453 aff = accept_affine(s, v);
457 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
461 if (seen_paren && isl_stream_eat(s, ')'))
463 if (isl_stream_eat(s, '/'))
466 tok = isl_stream_next_token(s);
469 if (tok->type != ISL_TOKEN_VALUE) {
470 isl_stream_error(s, tok, "expected denominator");
471 isl_stream_push_token(s, tok);
474 isl_int_set(bmap->div[k][0], tok->u.v);
477 if (isl_stream_eat(s, ']'))
480 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
485 isl_basic_map_free(bmap);
489 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
490 struct vars *v, struct isl_basic_map *bmap)
492 struct isl_token *tok;
494 while ((tok = isl_stream_next_token(s)) != NULL) {
498 unsigned total = isl_basic_map_total_dim(bmap);
500 if (tok->type != ISL_TOKEN_IDENT)
503 p = vars_pos(v, tok->u.s, -1);
507 isl_stream_error(s, tok, "expecting unique identifier");
511 bmap = isl_basic_map_cow(bmap);
512 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
515 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
517 isl_seq_clr(bmap->div[k], 1 + 1 + total);
520 tok = isl_stream_next_token(s);
521 if (tok && tok->type == '=') {
523 bmap = add_div_definition(s, v, bmap, k);
524 tok = isl_stream_next_token(s);
527 if (!tok || tok->type != ',')
533 isl_stream_push_token(s, tok);
538 isl_basic_map_free(bmap);
542 static int next_is_tuple(struct isl_stream *s)
544 struct isl_token *tok;
547 tok = isl_stream_next_token(s);
550 if (tok->type == '[') {
551 isl_stream_push_token(s, tok);
554 if (tok->type != ISL_TOKEN_IDENT) {
555 isl_stream_push_token(s, tok);
559 is_tuple = isl_stream_next_token_is(s, '[');
561 isl_stream_push_token(s, tok);
566 static __isl_give isl_dim *read_tuple(struct isl_stream *s,
567 __isl_take isl_dim *dim, enum isl_dim_type type, struct vars *v,
568 __isl_keep isl_mat **eq);
570 static __isl_give isl_dim *read_nested_tuple(struct isl_stream *s,
571 __isl_take isl_dim *dim, struct vars *v, __isl_keep isl_mat **eq)
573 dim = read_tuple(s, dim, isl_dim_in, v, eq);
574 if (isl_stream_eat(s, ISL_TOKEN_TO))
576 dim = read_tuple(s, dim, isl_dim_out, v, eq);
577 dim = isl_dim_wrap(dim);
584 static __isl_give isl_dim *read_tuple(struct isl_stream *s,
585 __isl_take isl_dim *dim, enum isl_dim_type type, struct vars *v,
586 __isl_keep isl_mat **eq)
588 struct isl_token *tok;
591 tok = isl_stream_next_token(s);
592 if (tok && tok->type == ISL_TOKEN_IDENT) {
593 name = strdup(tok->u.s);
597 tok = isl_stream_next_token(s);
599 if (!tok || tok->type != '[') {
600 isl_stream_error(s, tok, "expecting '['");
604 if (type != isl_dim_param && next_is_tuple(s)) {
605 isl_dim *nested = isl_dim_copy(dim);
606 nested = isl_dim_drop(nested, isl_dim_in, 0,
607 isl_dim_size(nested, isl_dim_in));
608 nested = isl_dim_drop(nested, isl_dim_out, 0,
609 isl_dim_size(nested, isl_dim_out));
610 nested = read_nested_tuple(s, nested, v, eq);
611 if (type == isl_dim_in) {
613 dim = isl_dim_reverse(nested);
615 dim = isl_dim_join(dim, nested);
618 dim = read_var_list(s, dim, type, v, eq);
619 tok = isl_stream_next_token(s);
620 if (!tok || tok->type != ']') {
621 isl_stream_error(s, tok, "expecting ']'");
627 dim = isl_dim_set_tuple_name(dim, type, name);
639 static struct isl_basic_map *add_constraints(struct isl_stream *s,
640 struct vars *v, struct isl_basic_map *bmap);
642 static struct isl_basic_map *add_exists(struct isl_stream *s,
643 struct vars *v, struct isl_basic_map *bmap)
645 struct isl_token *tok;
652 tok = isl_stream_next_token(s);
655 if (tok->type == '(') {
659 isl_stream_push_token(s, tok);
661 bmap = read_defined_var_list(s, v, bmap);
665 if (isl_stream_eat(s, ':'))
667 bmap = add_constraints(s, v, bmap);
668 if (seen_paren && isl_stream_eat(s, ')'))
672 isl_basic_map_free(bmap);
676 static __isl_give isl_basic_map *construct_constraint(
677 __isl_take isl_basic_map *bmap, enum isl_token_type type,
678 isl_int *left, isl_int *right)
686 len = 1 + isl_basic_map_total_dim(bmap);
689 k = isl_basic_map_alloc_inequality(bmap);
692 if (type == ISL_TOKEN_LE)
693 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
696 else if (type == ISL_TOKEN_GE)
697 isl_seq_combine(bmap->ineq[k], ctx->one, left,
700 else if (type == ISL_TOKEN_LT) {
701 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
704 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
705 } else if (type == ISL_TOKEN_GT) {
706 isl_seq_combine(bmap->ineq[k], ctx->one, left,
709 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
711 isl_seq_combine(bmap->ineq[k], ctx->one, left,
714 isl_basic_map_inequality_to_equality(bmap, k);
719 isl_basic_map_free(bmap);
723 static int is_comparator(struct isl_token *tok)
740 static struct isl_basic_map *add_constraint(struct isl_stream *s,
741 struct vars *v, struct isl_basic_map *bmap)
744 unsigned total = isl_basic_map_total_dim(bmap);
745 struct isl_token *tok = NULL;
746 struct isl_mat *aff1 = NULL, *aff2 = NULL;
748 tok = isl_stream_next_token(s);
751 if (tok->type == ISL_TOKEN_EXISTS) {
753 return add_exists(s, v, bmap);
755 isl_stream_push_token(s, tok);
758 bmap = isl_basic_map_cow(bmap);
760 aff1 = accept_affine_list(s, v);
763 tok = isl_stream_next_token(s);
764 if (!is_comparator(tok)) {
765 isl_stream_error(s, tok, "missing operator");
767 isl_stream_push_token(s, tok);
771 isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
773 aff2 = accept_affine_list(s, v);
776 isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
778 bmap = isl_basic_map_extend_constraints(bmap, 0,
779 aff1->n_row * aff2->n_row);
780 for (i = 0; i < aff1->n_row; ++i)
781 for (j = 0; j < aff2->n_row; ++j)
782 bmap = construct_constraint(bmap, tok->type,
783 aff1->row[i], aff2->row[j]);
788 tok = isl_stream_next_token(s);
789 if (!is_comparator(tok)) {
791 isl_stream_push_token(s, tok);
803 isl_basic_map_free(bmap);
807 static struct isl_basic_map *add_constraints(struct isl_stream *s,
808 struct vars *v, struct isl_basic_map *bmap)
810 struct isl_token *tok;
813 bmap = add_constraint(s, v, bmap);
816 tok = isl_stream_next_token(s);
818 isl_stream_error(s, NULL, "unexpected EOF");
821 if (tok->type != ISL_TOKEN_AND)
825 isl_stream_push_token(s, tok);
831 isl_basic_map_free(bmap);
835 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
836 struct vars *v, __isl_take isl_dim *dim)
839 struct isl_token *tok;
840 struct isl_basic_map *bmap;
842 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
846 tok = isl_stream_next_token(s);
849 if (tok->type == '(') {
853 isl_stream_push_token(s, tok);
855 bmap = add_constraints(s, v, bmap);
856 bmap = isl_basic_map_simplify(bmap);
857 bmap = isl_basic_map_finalize(bmap);
859 if (seen_paren && isl_stream_eat(s, ')'))
864 isl_basic_map_free(bmap);
868 static struct isl_map *read_disjuncts(struct isl_stream *s,
869 struct vars *v, __isl_take isl_dim *dim)
871 struct isl_token *tok;
874 tok = isl_stream_next_token(s);
876 isl_stream_error(s, NULL, "unexpected EOF");
879 if (tok->type == '}') {
880 isl_stream_push_token(s, tok);
881 return isl_map_universe(dim);
883 isl_stream_push_token(s, tok);
885 map = isl_map_empty(isl_dim_copy(dim));
887 struct isl_basic_map *bmap;
890 bmap = read_disjunct(s, v, isl_dim_copy(dim));
891 map = isl_map_union(map, isl_map_from_basic_map(bmap));
893 vars_drop(v, v->n - n);
895 tok = isl_stream_next_token(s);
896 if (!tok || tok->type != ISL_TOKEN_OR)
901 isl_stream_push_token(s, tok);
910 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
912 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
913 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
914 isl_basic_map_dim(bmap, isl_dim_in) + pos;
915 pos -= isl_basic_map_dim(bmap, isl_dim_out);
917 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
918 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
919 pos -= isl_basic_map_dim(bmap, isl_dim_in);
921 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
922 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
923 isl_basic_map_dim(bmap, isl_dim_in) +
924 isl_basic_map_dim(bmap, isl_dim_out) + pos;
925 pos -= isl_basic_map_dim(bmap, isl_dim_div);
927 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
933 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
934 struct isl_stream *s, __isl_take isl_basic_map *bmap)
937 struct isl_token *tok;
947 nparam = isl_basic_map_dim(bmap, isl_dim_param);
948 dim = isl_basic_map_dim(bmap, isl_dim_out);
950 tok = isl_stream_next_token(s);
951 if (!tok || tok->type != ISL_TOKEN_VALUE) {
952 isl_stream_error(s, tok, "expecting coefficient");
954 isl_stream_push_token(s, tok);
957 if (!tok->on_new_line) {
958 isl_stream_error(s, tok, "coefficient should appear on new line");
959 isl_stream_push_token(s, tok);
963 type = isl_int_get_si(tok->u.v);
966 isl_assert(s->ctx, type == 0 || type == 1, goto error);
968 k = isl_basic_map_alloc_equality(bmap);
971 k = isl_basic_map_alloc_inequality(bmap);
977 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
979 tok = isl_stream_next_token(s);
980 if (!tok || tok->type != ISL_TOKEN_VALUE) {
981 isl_stream_error(s, tok, "expecting coefficient");
983 isl_stream_push_token(s, tok);
986 if (tok->on_new_line) {
987 isl_stream_error(s, tok,
988 "coefficient should not appear on new line");
989 isl_stream_push_token(s, tok);
992 pos = polylib_pos_to_isl_pos(bmap, j);
993 isl_int_set(c[pos], tok->u.v);
999 isl_basic_map_free(bmap);
1003 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1007 struct isl_token *tok;
1008 struct isl_token *tok2;
1011 unsigned in = 0, out, local = 0;
1012 struct isl_basic_map *bmap = NULL;
1017 tok = isl_stream_next_token(s);
1019 isl_stream_error(s, NULL, "unexpected EOF");
1022 tok2 = isl_stream_next_token(s);
1024 isl_token_free(tok);
1025 isl_stream_error(s, NULL, "unexpected EOF");
1028 n_row = isl_int_get_si(tok->u.v);
1029 n_col = isl_int_get_si(tok2->u.v);
1030 on_new_line = tok2->on_new_line;
1031 isl_token_free(tok2);
1032 isl_token_free(tok);
1033 isl_assert(s->ctx, !on_new_line, return NULL);
1034 isl_assert(s->ctx, n_row >= 0, return NULL);
1035 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1036 tok = isl_stream_next_token_on_same_line(s);
1038 if (tok->type != ISL_TOKEN_VALUE) {
1039 isl_stream_error(s, tok,
1040 "expecting number of output dimensions");
1041 isl_stream_push_token(s, tok);
1044 out = isl_int_get_si(tok->u.v);
1045 isl_token_free(tok);
1047 tok = isl_stream_next_token_on_same_line(s);
1048 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1049 isl_stream_error(s, tok,
1050 "expecting number of input dimensions");
1052 isl_stream_push_token(s, tok);
1055 in = isl_int_get_si(tok->u.v);
1056 isl_token_free(tok);
1058 tok = isl_stream_next_token_on_same_line(s);
1059 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1060 isl_stream_error(s, tok,
1061 "expecting number of existentials");
1063 isl_stream_push_token(s, tok);
1066 local = isl_int_get_si(tok->u.v);
1067 isl_token_free(tok);
1069 tok = isl_stream_next_token_on_same_line(s);
1070 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1071 isl_stream_error(s, tok,
1072 "expecting number of parameters");
1074 isl_stream_push_token(s, tok);
1077 nparam = isl_int_get_si(tok->u.v);
1078 isl_token_free(tok);
1079 if (n_col != 1 + out + in + local + nparam + 1) {
1080 isl_stream_error(s, NULL,
1081 "dimensions don't match");
1085 out = n_col - 2 - nparam;
1086 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1090 for (i = 0; i < local; ++i) {
1091 int k = isl_basic_map_alloc_div(bmap);
1096 for (i = 0; i < n_row; ++i)
1097 bmap = basic_map_read_polylib_constraint(s, bmap);
1099 tok = isl_stream_next_token_on_same_line(s);
1101 isl_stream_error(s, tok, "unexpected extra token on line");
1102 isl_stream_push_token(s, tok);
1106 bmap = isl_basic_map_simplify(bmap);
1107 bmap = isl_basic_map_finalize(bmap);
1110 isl_basic_map_free(bmap);
1114 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1116 struct isl_token *tok;
1117 struct isl_token *tok2;
1119 struct isl_map *map;
1121 tok = isl_stream_next_token(s);
1123 isl_stream_error(s, NULL, "unexpected EOF");
1126 tok2 = isl_stream_next_token_on_same_line(s);
1128 isl_stream_push_token(s, tok2);
1129 isl_stream_push_token(s, tok);
1130 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1132 n = isl_int_get_si(tok->u.v);
1133 isl_token_free(tok);
1135 isl_assert(s->ctx, n >= 1, return NULL);
1137 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1139 for (i = 1; i < n; ++i)
1140 map = isl_map_union(map,
1141 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1146 static int optional_power(struct isl_stream *s)
1149 struct isl_token *tok;
1151 tok = isl_stream_next_token(s);
1154 if (tok->type != '^') {
1155 isl_stream_push_token(s, tok);
1158 isl_token_free(tok);
1159 tok = isl_stream_next_token(s);
1160 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1161 isl_stream_error(s, tok, "expecting exponent");
1163 isl_stream_push_token(s, tok);
1166 pow = isl_int_get_si(tok->u.v);
1167 isl_token_free(tok);
1171 static __isl_give isl_div *read_div(struct isl_stream *s,
1172 __isl_keep isl_basic_map *bmap, struct vars *v)
1174 unsigned total = isl_basic_map_total_dim(bmap);
1177 bmap = isl_basic_map_copy(bmap);
1178 bmap = isl_basic_map_cow(bmap);
1179 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1182 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
1184 isl_seq_clr(bmap->div[k], 1 + 1 + total);
1185 bmap = add_div_definition(s, v, bmap, k);
1187 return isl_basic_map_div(bmap, k);
1189 isl_basic_map_free(bmap);
1193 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1194 __isl_keep isl_basic_map *bmap, struct vars *v);
1196 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1197 __isl_keep isl_basic_map *bmap, struct vars *v)
1199 struct isl_qpolynomial *qp;
1200 struct isl_token *tok;
1202 tok = isl_stream_next_token(s);
1204 isl_stream_error(s, NULL, "unexpected EOF");
1207 if (tok->type == '(') {
1208 isl_token_free(tok);
1209 qp = read_term(s, bmap, v);
1212 if (isl_stream_eat(s, ')'))
1214 } else if (tok->type == ISL_TOKEN_VALUE) {
1215 struct isl_token *tok2;
1216 tok2 = isl_stream_next_token(s);
1217 if (tok2 && tok2->type == '/') {
1218 isl_token_free(tok2);
1219 tok2 = isl_stream_next_token(s);
1220 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1221 isl_stream_error(s, tok2, "expected denominator");
1222 isl_token_free(tok);
1223 isl_token_free(tok2);
1226 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1227 tok->u.v, tok2->u.v);
1228 isl_token_free(tok2);
1230 isl_stream_push_token(s, tok2);
1231 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1234 isl_token_free(tok);
1235 } else if (tok->type == ISL_TOKEN_INFTY) {
1236 isl_token_free(tok);
1237 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1238 } else if (tok->type == ISL_TOKEN_NAN) {
1239 isl_token_free(tok);
1240 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1241 } else if (tok->type == ISL_TOKEN_IDENT) {
1243 int pos = vars_pos(v, tok->u.s, -1);
1246 isl_token_free(tok);
1250 isl_stream_error(s, tok, "unknown identifier");
1251 isl_token_free(tok);
1254 isl_token_free(tok);
1255 pow = optional_power(s);
1256 qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
1257 } else if (tok->type == '[') {
1261 isl_stream_push_token(s, tok);
1262 div = read_div(s, bmap, v);
1263 pow = optional_power(s);
1264 qp = isl_qpolynomial_div_pow(div, pow);
1265 } else if (tok->type == '-') {
1266 struct isl_qpolynomial *qp2;
1268 isl_token_free(tok);
1269 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1271 qp2 = read_factor(s, bmap, v);
1272 qp = isl_qpolynomial_mul(qp, qp2);
1274 isl_stream_error(s, tok, "unexpected isl_token");
1275 isl_stream_push_token(s, tok);
1279 if (isl_stream_eat_if_available(s, '*') ||
1280 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1281 struct isl_qpolynomial *qp2;
1283 qp2 = read_factor(s, bmap, v);
1284 qp = isl_qpolynomial_mul(qp, qp2);
1289 isl_qpolynomial_free(qp);
1293 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1294 __isl_keep isl_basic_map *bmap, struct vars *v)
1296 struct isl_token *tok;
1297 struct isl_qpolynomial *qp;
1299 qp = read_factor(s, bmap, v);
1302 tok = isl_stream_next_token(s);
1306 if (tok->type == '+') {
1307 struct isl_qpolynomial *qp2;
1309 isl_token_free(tok);
1310 qp2 = read_factor(s, bmap, v);
1311 qp = isl_qpolynomial_add(qp, qp2);
1312 } else if (tok->type == '-') {
1313 struct isl_qpolynomial *qp2;
1315 isl_token_free(tok);
1316 qp2 = read_factor(s, bmap, v);
1317 qp = isl_qpolynomial_sub(qp, qp2);
1318 } else if (tok->type == ISL_TOKEN_VALUE &&
1319 isl_int_is_neg(tok->u.v)) {
1320 struct isl_qpolynomial *qp2;
1322 qp2 = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1324 isl_token_free(tok);
1325 qp = isl_qpolynomial_add(qp, qp2);
1327 isl_stream_push_token(s, tok);
1335 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1336 __isl_take isl_basic_map *bmap, struct vars *v)
1338 struct isl_token *tok;
1339 struct isl_map *map;
1341 tok = isl_stream_next_token(s);
1343 isl_stream_error(s, NULL, "unexpected EOF");
1346 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1347 if (tok->type == ':') {
1348 isl_token_free(tok);
1349 map = isl_map_intersect(map,
1350 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1352 isl_stream_push_token(s, tok);
1354 isl_basic_map_free(bmap);
1358 isl_basic_map_free(bmap);
1362 static struct isl_obj obj_read_poly(struct isl_stream *s,
1363 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1365 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1366 struct isl_pw_qpolynomial *pwqp;
1367 struct isl_qpolynomial *qp;
1368 struct isl_map *map;
1369 struct isl_set *set;
1371 qp = read_term(s, bmap, v);
1372 map = read_optional_disjuncts(s, bmap, v);
1373 set = isl_map_range(map);
1375 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1377 vars_drop(v, v->n - n);
1383 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1384 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1386 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1387 struct isl_obj obj_p;
1388 isl_qpolynomial *qp;
1389 isl_qpolynomial_fold *fold = NULL;
1390 isl_pw_qpolynomial_fold *pwf;
1394 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1395 return obj_read_poly(s, bmap, v, n);
1397 if (isl_stream_eat(s, '('))
1400 qp = read_term(s, bmap, v);
1401 fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1403 while (isl_stream_eat_if_available(s, ',')) {
1404 isl_qpolynomial_fold *fold_i;
1405 qp = read_term(s, bmap, v);
1406 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1407 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1410 if (isl_stream_eat(s, ')'))
1413 map = read_optional_disjuncts(s, bmap, v);
1414 set = isl_map_range(map);
1415 pwf = isl_pw_qpolynomial_fold_alloc(set, fold);
1417 vars_drop(v, v->n - n);
1422 isl_basic_map_free(bmap);
1423 isl_qpolynomial_fold_free(fold);
1424 obj.type = isl_obj_none;
1428 static __isl_give isl_basic_map *add_equalities(__isl_take isl_basic_map *bmap,
1432 unsigned total = 1 + isl_basic_map_total_dim(bmap);
1437 bmap = isl_basic_map_extend_constraints(bmap, eq->n_row, 0);
1439 for (i = 0; i < eq->n_row; ++i) {
1440 k = isl_basic_map_alloc_equality(bmap);
1443 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
1444 isl_seq_clr(bmap->eq[k] + eq->n_col, total - eq->n_col);
1451 isl_basic_map_free(bmap);
1455 static struct isl_obj obj_read_body(struct isl_stream *s,
1456 __isl_take isl_dim *dim, struct vars *v)
1458 struct isl_map *map = NULL;
1459 struct isl_token *tok;
1460 struct isl_obj obj = { isl_obj_set, NULL };
1463 isl_basic_map *bmap;
1465 if (!next_is_tuple(s)) {
1466 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
1467 return obj_read_poly_or_fold(s, bmap, v, n);
1470 eq = isl_mat_alloc(s->ctx, 0, 1 + n);
1472 dim = read_tuple(s, dim, isl_dim_in, v, &eq);
1475 tok = isl_stream_next_token(s);
1476 if (tok && tok->type == ISL_TOKEN_TO) {
1477 obj.type = isl_obj_map;
1478 isl_token_free(tok);
1479 if (!next_is_tuple(s)) {
1480 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
1481 bmap = add_equalities(bmap, eq);
1482 bmap = isl_basic_map_reverse(bmap);
1483 return obj_read_poly_or_fold(s, bmap, v, n);
1485 dim = read_tuple(s, dim, isl_dim_out, v, &eq);
1489 dim = isl_dim_reverse(dim);
1491 isl_stream_push_token(s, tok);
1494 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
1495 bmap = add_equalities(bmap, eq);
1497 map = read_optional_disjuncts(s, bmap, v);
1499 vars_drop(v, v->n - n);
1506 obj.type = isl_obj_none;
1510 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1512 if (obj.type == isl_obj_map) {
1513 obj.v = isl_union_map_from_map(obj.v);
1514 obj.type = isl_obj_union_map;
1515 } else if (obj.type == isl_obj_set) {
1516 obj.v = isl_union_set_from_set(obj.v);
1517 obj.type = isl_obj_union_set;
1518 } else if (obj.type == isl_obj_pw_qpolynomial) {
1519 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1520 obj.type = isl_obj_union_pw_qpolynomial;
1521 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1522 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1523 obj.type = isl_obj_union_pw_qpolynomial_fold;
1525 isl_assert(ctx, 0, goto error);
1528 obj.type->free(obj.v);
1529 obj.type = isl_obj_none;
1533 static struct isl_obj obj_add(struct isl_ctx *ctx,
1534 struct isl_obj obj1, struct isl_obj obj2)
1536 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1537 obj1 = to_union(ctx, obj1);
1538 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1539 obj2 = to_union(ctx, obj2);
1540 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1541 obj1 = to_union(ctx, obj1);
1542 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1543 obj2 = to_union(ctx, obj2);
1544 if (obj1.type == isl_obj_pw_qpolynomial &&
1545 obj2.type == isl_obj_union_pw_qpolynomial)
1546 obj1 = to_union(ctx, obj1);
1547 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1548 obj2.type == isl_obj_pw_qpolynomial)
1549 obj2 = to_union(ctx, obj2);
1550 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1551 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1552 obj1 = to_union(ctx, obj1);
1553 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1554 obj2.type == isl_obj_pw_qpolynomial_fold)
1555 obj2 = to_union(ctx, obj2);
1556 isl_assert(ctx, obj1.type == obj2.type, goto error);
1557 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1558 obj1 = to_union(ctx, obj1);
1559 obj2 = to_union(ctx, obj2);
1561 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1562 obj1 = to_union(ctx, obj1);
1563 obj2 = to_union(ctx, obj2);
1565 if (obj1.type == isl_obj_pw_qpolynomial &&
1566 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1567 obj1 = to_union(ctx, obj1);
1568 obj2 = to_union(ctx, obj2);
1570 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1571 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1572 obj1 = to_union(ctx, obj1);
1573 obj2 = to_union(ctx, obj2);
1575 obj1.v = obj1.type->add(obj1.v, obj2.v);
1578 obj1.type->free(obj1.v);
1579 obj2.type->free(obj2.v);
1580 obj1.type = isl_obj_none;
1585 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1587 isl_dim *dim = NULL;
1588 struct isl_token *tok;
1589 struct vars *v = NULL;
1590 struct isl_obj obj = { isl_obj_set, NULL };
1592 tok = isl_stream_next_token(s);
1594 isl_stream_error(s, NULL, "unexpected EOF");
1597 if (tok->type == ISL_TOKEN_VALUE) {
1598 struct isl_map *map;
1599 isl_stream_push_token(s, tok);
1600 map = map_read_polylib(s, nparam);
1603 if (isl_map_dim(map, isl_dim_in) > 0)
1604 obj.type = isl_obj_map;
1608 v = vars_new(s->ctx);
1610 isl_stream_push_token(s, tok);
1613 dim = isl_dim_alloc(s->ctx, 0, 0, 0);
1614 if (tok->type == '[') {
1615 isl_stream_push_token(s, tok);
1616 dim = read_tuple(s, dim, isl_dim_param, v, NULL);
1620 isl_assert(s->ctx, nparam == v->n, goto error);
1621 tok = isl_stream_next_token(s);
1622 if (!tok || tok->type != ISL_TOKEN_TO) {
1623 isl_stream_error(s, tok, "expecting '->'");
1625 isl_stream_push_token(s, tok);
1628 isl_token_free(tok);
1629 tok = isl_stream_next_token(s);
1630 } else if (nparam > 0)
1631 dim = isl_dim_add(dim, isl_dim_param, nparam);
1632 if (!tok || tok->type != '{') {
1633 isl_stream_error(s, tok, "expecting '{'");
1635 isl_stream_push_token(s, tok);
1638 isl_token_free(tok);
1640 tok = isl_stream_next_token(s);
1643 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1644 isl_token_free(tok);
1645 if (isl_stream_eat(s, '='))
1647 dim = read_tuple(s, dim, isl_dim_param, v, NULL);
1651 isl_assert(s->ctx, nparam == v->n, goto error);
1652 } else if (tok->type == '}') {
1653 obj.type = isl_obj_union_set;
1654 obj.v = isl_union_set_empty(isl_dim_copy(dim));
1655 isl_token_free(tok);
1658 isl_stream_push_token(s, tok);
1663 o = obj_read_body(s, isl_dim_copy(dim), v);
1664 if (o.type == isl_obj_none)
1669 obj = obj_add(s->ctx, obj, o);
1670 if (obj.type == isl_obj_none)
1673 tok = isl_stream_next_token(s);
1674 if (!tok || tok->type != ';')
1676 isl_token_free(tok);
1679 if (tok && tok->type == '}') {
1680 isl_token_free(tok);
1682 isl_stream_error(s, tok, "unexpected isl_token");
1684 isl_token_free(tok);
1694 obj.type->free(obj.v);
1701 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1703 return obj_read(s, -1);
1706 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1709 struct isl_map *map;
1711 obj = obj_read(s, nparam);
1713 isl_assert(s->ctx, obj.type == isl_obj_map ||
1714 obj.type == isl_obj_set, goto error);
1718 obj.type->free(obj.v);
1722 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1725 struct isl_set *set;
1727 obj = obj_read(s, nparam);
1729 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1733 obj.type->free(obj.v);
1737 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1740 struct isl_map *map;
1741 struct isl_basic_map *bmap;
1743 obj = obj_read(s, nparam);
1748 isl_assert(map->ctx, map->n <= 1, goto error);
1751 bmap = isl_basic_map_empty_like_map(map);
1753 bmap = isl_basic_map_copy(map->p[0]);
1763 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1764 FILE *input, int nparam)
1766 struct isl_basic_map *bmap;
1767 struct isl_stream *s = isl_stream_new_file(ctx, input);
1770 bmap = basic_map_read(s, nparam);
1775 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1776 FILE *input, int nparam)
1778 struct isl_basic_map *bmap;
1779 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1782 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1783 return (struct isl_basic_set *)bmap;
1785 isl_basic_map_free(bmap);
1789 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1790 const char *str, int nparam)
1792 struct isl_basic_map *bmap;
1793 struct isl_stream *s = isl_stream_new_str(ctx, str);
1796 bmap = basic_map_read(s, nparam);
1801 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1802 const char *str, int nparam)
1804 struct isl_basic_map *bmap;
1805 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1808 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1809 return (struct isl_basic_set *)bmap;
1811 isl_basic_map_free(bmap);
1815 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1816 FILE *input, int nparam)
1818 struct isl_map *map;
1819 struct isl_stream *s = isl_stream_new_file(ctx, input);
1822 map = isl_stream_read_map(s, nparam);
1827 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1828 const char *str, int nparam)
1830 struct isl_map *map;
1831 struct isl_stream *s = isl_stream_new_str(ctx, str);
1834 map = isl_stream_read_map(s, nparam);
1839 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1840 FILE *input, int nparam)
1842 struct isl_map *map;
1843 map = isl_map_read_from_file(ctx, input, nparam);
1846 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1847 return (struct isl_set *)map;
1853 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1854 const char *str, int nparam)
1856 struct isl_map *map;
1857 map = isl_map_read_from_str(ctx, str, nparam);
1860 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1861 return (struct isl_set *)map;
1867 static char *next_line(FILE *input, char *line, unsigned len)
1872 if (!(p = fgets(line, len, input)))
1874 while (isspace(*p) && *p != '\n')
1876 } while (*p == '#' || *p == '\n');
1881 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1884 struct isl_vec *vec = NULL;
1893 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1894 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1896 vec = isl_vec_alloc(ctx, size);
1898 p = next_line(input, line, sizeof(line));
1899 isl_assert(ctx, p, goto error);
1901 for (j = 0; j < size; ++j) {
1902 n = sscanf(p, "%s%n", val, &offset);
1903 isl_assert(ctx, n != 0, goto error);
1904 isl_int_read(vec->el[j], val);
1914 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1915 FILE *input, unsigned input_format)
1917 if (input_format == ISL_FORMAT_POLYLIB)
1918 return isl_vec_read_from_file_polylib(ctx, input);
1920 isl_assert(ctx, 0, return NULL);
1923 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
1924 struct isl_stream *s)
1927 struct isl_pw_qpolynomial *pwqp;
1929 obj = obj_read(s, -1);
1931 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
1936 obj.type->free(obj.v);
1940 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
1943 isl_pw_qpolynomial *pwqp;
1944 struct isl_stream *s = isl_stream_new_str(ctx, str);
1947 pwqp = isl_stream_read_pw_qpolynomial(s);