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 struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
154 struct isl_token *tok = NULL;
158 aff = isl_vec_alloc(v->ctx, 1 + v->n);
159 isl_seq_clr(aff->el, aff->size);
164 tok = isl_stream_next_token(s);
166 isl_stream_error(s, NULL, "unexpected EOF");
169 if (tok->type == ISL_TOKEN_IDENT) {
171 int pos = vars_pos(v, tok->u.s, -1);
175 isl_stream_error(s, tok, "unknown identifier");
180 isl_int_add_ui(aff->el[1 + pos],
181 aff->el[1 + pos], 1);
183 isl_int_sub_ui(aff->el[1 + pos],
184 aff->el[1 + pos], 1);
186 } else if (tok->type == ISL_TOKEN_VALUE) {
187 struct isl_token *tok2;
190 if (isl_stream_eat_if_available(s, '*') &&
191 !isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
192 isl_stream_error(s, NULL, "missing identifier");
195 tok2 = isl_stream_next_token(s);
196 if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
197 pos = vars_pos(v, tok2->u.s, -1);
201 isl_stream_error(s, tok2,
202 "unknown identifier");
204 isl_token_free(tok2);
207 isl_token_free(tok2);
209 isl_stream_push_token(s, tok2);
211 isl_int_neg(tok->u.v, tok->u.v);
212 isl_int_add(aff->el[1 + pos],
213 aff->el[1 + pos], tok->u.v);
215 } else if (tok->type == '-') {
217 } else if (tok->type == '+') {
220 isl_stream_push_token(s, tok);
232 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
233 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
236 struct isl_token *tok;
238 while ((tok = isl_stream_next_token(s)) != NULL) {
241 if (tok->type == ISL_TOKEN_IDENT) {
243 int p = vars_pos(v, tok->u.s, -1);
250 bmap = isl_basic_map_add(bmap, type, 1);
251 bmap = set_name(bmap, type, i, v->v->name);
253 } else if (tok->type == ISL_TOKEN_IDENT ||
254 tok->type == ISL_TOKEN_VALUE) {
257 if (type == isl_dim_param) {
258 isl_stream_error(s, tok,
259 "expecting unique identifier");
262 isl_stream_push_token(s, tok);
264 vec = accept_affine(s, v);
267 v->v = variable_new(v, "", 0, v->n);
271 bmap = isl_basic_map_add(bmap, type, 1);
272 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
273 k = isl_basic_map_alloc_equality(bmap);
275 isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
276 isl_int_set_si(bmap->eq[k][vec->size], -1);
284 tok = isl_stream_next_token(s);
285 if (!tok || tok->type != ',')
292 isl_stream_push_token(s, tok);
297 isl_basic_map_free(bmap);
301 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
306 struct isl_token *tok = NULL;
308 vec = accept_affine(s, v);
309 mat = isl_mat_from_row_vec(vec);
314 tok = isl_stream_next_token(s);
316 isl_stream_error(s, NULL, "unexpected EOF");
319 if (tok->type != ',') {
320 isl_stream_push_token(s, tok);
325 vec = accept_affine(s, v);
326 mat = isl_mat_vec_concat(mat, vec);
337 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
338 struct vars *v, struct isl_basic_map *bmap, int k)
340 struct isl_token *tok;
344 if (isl_stream_eat(s, '['))
347 tok = isl_stream_next_token(s);
350 if (tok->type == '(') {
354 isl_stream_push_token(s, tok);
356 aff = accept_affine(s, v);
360 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
364 if (seen_paren && isl_stream_eat(s, ')'))
366 if (isl_stream_eat(s, '/'))
369 tok = isl_stream_next_token(s);
372 if (tok->type != ISL_TOKEN_VALUE) {
373 isl_stream_error(s, tok, "expected denominator");
374 isl_stream_push_token(s, tok);
377 isl_int_set(bmap->div[k][0], tok->u.v);
380 if (isl_stream_eat(s, ']'))
383 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
388 isl_basic_map_free(bmap);
392 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
393 struct vars *v, struct isl_basic_map *bmap)
395 struct isl_token *tok;
397 while ((tok = isl_stream_next_token(s)) != NULL) {
401 unsigned total = isl_basic_map_total_dim(bmap);
403 if (tok->type != ISL_TOKEN_IDENT)
406 p = vars_pos(v, tok->u.s, -1);
410 isl_stream_error(s, tok, "expecting unique identifier");
415 bmap = isl_basic_map_cow(bmap);
416 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
419 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
421 isl_seq_clr(bmap->div[k], 1 + 1 + total);
423 tok = isl_stream_next_token(s);
424 if (tok && tok->type == '=') {
426 bmap = add_div_definition(s, v, bmap, k);
427 tok = isl_stream_next_token(s);
430 if (!tok || tok->type != ',')
436 isl_stream_push_token(s, tok);
441 isl_basic_map_free(bmap);
445 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
446 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
448 struct isl_token *tok;
450 tok = isl_stream_next_token(s);
451 if (!tok || tok->type != '[') {
452 isl_stream_error(s, tok, "expecting '['");
456 bmap = read_var_list(s, bmap, type, v);
457 tok = isl_stream_next_token(s);
458 if (!tok || tok->type != ']') {
459 isl_stream_error(s, tok, "expecting ']'");
468 isl_basic_map_free(bmap);
472 static struct isl_basic_map *add_constraints(struct isl_stream *s,
473 struct vars *v, struct isl_basic_map *bmap);
475 static struct isl_basic_map *add_exists(struct isl_stream *s,
476 struct vars *v, struct isl_basic_map *bmap)
478 struct isl_token *tok;
485 tok = isl_stream_next_token(s);
488 if (tok->type == '(') {
492 isl_stream_push_token(s, tok);
494 bmap = read_defined_var_list(s, v, bmap);
498 if (isl_stream_eat(s, ':'))
500 bmap = add_constraints(s, v, bmap);
501 if (seen_paren && isl_stream_eat(s, ')'))
505 isl_basic_map_free(bmap);
509 static __isl_give isl_basic_map *construct_constraint(
510 __isl_take isl_basic_map *bmap, enum isl_token_type type,
511 isl_int *left, isl_int *right)
519 len = 1 + isl_basic_map_total_dim(bmap);
522 k = isl_basic_map_alloc_inequality(bmap);
525 if (type == ISL_TOKEN_LE)
526 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
529 else if (type == ISL_TOKEN_GE)
530 isl_seq_combine(bmap->ineq[k], ctx->one, left,
533 else if (type == ISL_TOKEN_LT) {
534 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
537 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
538 } else if (type == ISL_TOKEN_GT) {
539 isl_seq_combine(bmap->ineq[k], ctx->one, left,
542 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
544 isl_seq_combine(bmap->ineq[k], ctx->one, left,
547 isl_basic_map_inequality_to_equality(bmap, k);
552 isl_basic_map_free(bmap);
556 static int is_comparator(struct isl_token *tok)
573 static struct isl_basic_map *add_constraint(struct isl_stream *s,
574 struct vars *v, struct isl_basic_map *bmap)
577 unsigned total = isl_basic_map_total_dim(bmap);
578 struct isl_token *tok = NULL;
579 struct isl_mat *aff1 = NULL, *aff2 = NULL;
581 tok = isl_stream_next_token(s);
584 if (tok->type == ISL_TOKEN_EXISTS) {
586 return add_exists(s, v, bmap);
588 isl_stream_push_token(s, tok);
591 bmap = isl_basic_map_cow(bmap);
593 aff1 = accept_affine_list(s, v);
596 tok = isl_stream_next_token(s);
597 if (!is_comparator(tok)) {
598 isl_stream_error(s, tok, "missing operator");
600 isl_stream_push_token(s, tok);
604 isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
606 aff2 = accept_affine_list(s, v);
609 isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
611 bmap = isl_basic_map_extend_constraints(bmap, 0,
612 aff1->n_row * aff2->n_row);
613 for (i = 0; i < aff1->n_row; ++i)
614 for (j = 0; j < aff2->n_row; ++j)
615 bmap = construct_constraint(bmap, tok->type,
616 aff1->row[i], aff2->row[j]);
621 tok = isl_stream_next_token(s);
622 if (!is_comparator(tok)) {
624 isl_stream_push_token(s, tok);
636 isl_basic_map_free(bmap);
640 static struct isl_basic_map *add_constraints(struct isl_stream *s,
641 struct vars *v, struct isl_basic_map *bmap)
643 struct isl_token *tok;
646 bmap = add_constraint(s, v, bmap);
649 tok = isl_stream_next_token(s);
651 isl_stream_error(s, NULL, "unexpected EOF");
654 if (tok->type != ISL_TOKEN_AND)
658 isl_stream_push_token(s, tok);
664 isl_basic_map_free(bmap);
668 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
669 struct vars *v, __isl_take isl_dim *dim)
672 struct isl_token *tok;
673 struct isl_basic_map *bmap;
675 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
679 tok = isl_stream_next_token(s);
682 if (tok->type == '(') {
686 isl_stream_push_token(s, tok);
688 bmap = add_constraints(s, v, bmap);
689 bmap = isl_basic_map_simplify(bmap);
690 bmap = isl_basic_map_finalize(bmap);
692 if (seen_paren && isl_stream_eat(s, ')'))
697 isl_basic_map_free(bmap);
701 static struct isl_map *read_disjuncts(struct isl_stream *s,
702 struct vars *v, __isl_take isl_dim *dim)
704 struct isl_token *tok;
707 tok = isl_stream_next_token(s);
709 isl_stream_error(s, NULL, "unexpected EOF");
712 if (tok->type == '}') {
713 isl_stream_push_token(s, tok);
714 return isl_map_universe(dim);
716 isl_stream_push_token(s, tok);
718 map = isl_map_empty(isl_dim_copy(dim));
720 struct isl_basic_map *bmap;
723 bmap = read_disjunct(s, v, isl_dim_copy(dim));
724 map = isl_map_union(map, isl_map_from_basic_map(bmap));
726 vars_drop(v, v->n - n);
728 tok = isl_stream_next_token(s);
729 if (!tok || tok->type != ISL_TOKEN_OR)
734 isl_stream_push_token(s, tok);
743 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
745 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
746 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
747 isl_basic_map_dim(bmap, isl_dim_in) + pos;
748 pos -= isl_basic_map_dim(bmap, isl_dim_out);
750 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
751 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
752 pos -= isl_basic_map_dim(bmap, isl_dim_in);
754 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
755 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
756 isl_basic_map_dim(bmap, isl_dim_in) +
757 isl_basic_map_dim(bmap, isl_dim_out) + pos;
758 pos -= isl_basic_map_dim(bmap, isl_dim_div);
760 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
766 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
767 struct isl_stream *s, __isl_take isl_basic_map *bmap)
770 struct isl_token *tok;
780 nparam = isl_basic_map_dim(bmap, isl_dim_param);
781 dim = isl_basic_map_dim(bmap, isl_dim_out);
783 tok = isl_stream_next_token(s);
784 if (!tok || tok->type != ISL_TOKEN_VALUE) {
785 isl_stream_error(s, tok, "expecting coefficient");
787 isl_stream_push_token(s, tok);
790 if (!tok->on_new_line) {
791 isl_stream_error(s, tok, "coefficient should appear on new line");
792 isl_stream_push_token(s, tok);
796 type = isl_int_get_si(tok->u.v);
799 isl_assert(s->ctx, type == 0 || type == 1, goto error);
801 k = isl_basic_map_alloc_equality(bmap);
804 k = isl_basic_map_alloc_inequality(bmap);
810 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
812 tok = isl_stream_next_token(s);
813 if (!tok || tok->type != ISL_TOKEN_VALUE) {
814 isl_stream_error(s, tok, "expecting coefficient");
816 isl_stream_push_token(s, tok);
819 if (tok->on_new_line) {
820 isl_stream_error(s, tok,
821 "coefficient should not appear on new line");
822 isl_stream_push_token(s, tok);
825 pos = polylib_pos_to_isl_pos(bmap, j);
826 isl_int_set(c[pos], tok->u.v);
832 isl_basic_map_free(bmap);
836 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
840 struct isl_token *tok;
841 struct isl_token *tok2;
844 unsigned in = 0, out, local = 0;
845 struct isl_basic_map *bmap = NULL;
850 tok = isl_stream_next_token(s);
852 isl_stream_error(s, NULL, "unexpected EOF");
855 tok2 = isl_stream_next_token(s);
858 isl_stream_error(s, NULL, "unexpected EOF");
861 n_row = isl_int_get_si(tok->u.v);
862 n_col = isl_int_get_si(tok2->u.v);
863 on_new_line = tok2->on_new_line;
864 isl_token_free(tok2);
866 isl_assert(s->ctx, !on_new_line, return NULL);
867 isl_assert(s->ctx, n_row >= 0, return NULL);
868 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
869 tok = isl_stream_next_token_on_same_line(s);
871 if (tok->type != ISL_TOKEN_VALUE) {
872 isl_stream_error(s, tok,
873 "expecting number of output dimensions");
874 isl_stream_push_token(s, tok);
877 out = isl_int_get_si(tok->u.v);
880 tok = isl_stream_next_token_on_same_line(s);
881 if (!tok || tok->type != ISL_TOKEN_VALUE) {
882 isl_stream_error(s, tok,
883 "expecting number of input dimensions");
885 isl_stream_push_token(s, tok);
888 in = isl_int_get_si(tok->u.v);
891 tok = isl_stream_next_token_on_same_line(s);
892 if (!tok || tok->type != ISL_TOKEN_VALUE) {
893 isl_stream_error(s, tok,
894 "expecting number of existentials");
896 isl_stream_push_token(s, tok);
899 local = isl_int_get_si(tok->u.v);
902 tok = isl_stream_next_token_on_same_line(s);
903 if (!tok || tok->type != ISL_TOKEN_VALUE) {
904 isl_stream_error(s, tok,
905 "expecting number of parameters");
907 isl_stream_push_token(s, tok);
910 nparam = isl_int_get_si(tok->u.v);
912 if (n_col != 1 + out + in + local + nparam + 1) {
913 isl_stream_error(s, NULL,
914 "dimensions don't match");
918 out = n_col - 2 - nparam;
919 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
923 for (i = 0; i < local; ++i) {
924 int k = isl_basic_map_alloc_div(bmap);
929 for (i = 0; i < n_row; ++i)
930 bmap = basic_map_read_polylib_constraint(s, bmap);
932 tok = isl_stream_next_token_on_same_line(s);
934 isl_stream_error(s, tok, "unexpected extra token on line");
935 isl_stream_push_token(s, tok);
939 bmap = isl_basic_map_simplify(bmap);
940 bmap = isl_basic_map_finalize(bmap);
943 isl_basic_map_free(bmap);
947 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
949 struct isl_token *tok;
950 struct isl_token *tok2;
954 tok = isl_stream_next_token(s);
956 isl_stream_error(s, NULL, "unexpected EOF");
959 tok2 = isl_stream_next_token_on_same_line(s);
961 isl_stream_push_token(s, tok2);
962 isl_stream_push_token(s, tok);
963 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
965 n = isl_int_get_si(tok->u.v);
968 isl_assert(s->ctx, n >= 1, return NULL);
970 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
972 for (i = 1; i < n; ++i)
973 map = isl_map_union(map,
974 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
979 static int optional_power(struct isl_stream *s)
982 struct isl_token *tok;
984 tok = isl_stream_next_token(s);
987 if (tok->type != '^') {
988 isl_stream_push_token(s, tok);
992 tok = isl_stream_next_token(s);
993 if (!tok || tok->type != ISL_TOKEN_VALUE) {
994 isl_stream_error(s, tok, "expecting exponent");
996 isl_stream_push_token(s, tok);
999 pow = isl_int_get_si(tok->u.v);
1000 isl_token_free(tok);
1004 static __isl_give isl_div *read_div(struct isl_stream *s,
1005 __isl_keep isl_basic_map *bmap, struct vars *v)
1007 unsigned total = isl_basic_map_total_dim(bmap);
1010 bmap = isl_basic_map_copy(bmap);
1011 bmap = isl_basic_map_cow(bmap);
1012 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1015 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
1017 isl_seq_clr(bmap->div[k], 1 + 1 + total);
1018 bmap = add_div_definition(s, v, bmap, k);
1020 return isl_basic_map_div(bmap, k);
1022 isl_basic_map_free(bmap);
1026 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1027 __isl_keep isl_basic_map *bmap, struct vars *v);
1029 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1030 __isl_keep isl_basic_map *bmap, struct vars *v)
1032 struct isl_qpolynomial *qp;
1033 struct isl_token *tok;
1035 tok = isl_stream_next_token(s);
1037 isl_stream_error(s, NULL, "unexpected EOF");
1040 if (tok->type == '(') {
1041 isl_token_free(tok);
1042 qp = read_term(s, bmap, v);
1045 if (isl_stream_eat(s, ')'))
1047 } else if (tok->type == ISL_TOKEN_VALUE) {
1048 struct isl_token *tok2;
1049 tok2 = isl_stream_next_token(s);
1050 if (tok2 && tok2->type == '/') {
1051 isl_token_free(tok2);
1052 tok2 = isl_stream_next_token(s);
1053 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1054 isl_stream_error(s, tok2, "expected denominator");
1055 isl_token_free(tok);
1056 isl_token_free(tok2);
1059 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1060 tok->u.v, tok2->u.v);
1061 isl_token_free(tok2);
1063 isl_stream_push_token(s, tok2);
1064 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1067 isl_token_free(tok);
1068 } else if (tok->type == ISL_TOKEN_INFTY) {
1069 isl_token_free(tok);
1070 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1071 } else if (tok->type == ISL_TOKEN_NAN) {
1072 isl_token_free(tok);
1073 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1074 } else if (tok->type == ISL_TOKEN_IDENT) {
1076 int pos = vars_pos(v, tok->u.s, -1);
1078 isl_token_free(tok);
1082 isl_stream_error(s, tok, "unknown identifier");
1085 pow = optional_power(s);
1086 qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
1087 } else if (tok->type == '[') {
1091 isl_stream_push_token(s, tok);
1092 div = read_div(s, bmap, v);
1093 pow = optional_power(s);
1094 qp = isl_qpolynomial_div_pow(div, pow);
1095 } else if (tok->type == '-') {
1096 struct isl_qpolynomial *qp2;
1098 isl_token_free(tok);
1099 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1101 qp2 = read_factor(s, bmap, v);
1102 qp = isl_qpolynomial_mul(qp, qp2);
1104 isl_stream_error(s, tok, "unexpected isl_token");
1105 isl_stream_push_token(s, tok);
1109 if (isl_stream_eat_if_available(s, '*') ||
1110 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1111 struct isl_qpolynomial *qp2;
1113 qp2 = read_factor(s, bmap, v);
1114 qp = isl_qpolynomial_mul(qp, qp2);
1119 isl_qpolynomial_free(qp);
1123 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1124 __isl_keep isl_basic_map *bmap, struct vars *v)
1126 struct isl_token *tok;
1127 struct isl_qpolynomial *qp;
1129 qp = read_factor(s, bmap, v);
1132 tok = isl_stream_next_token(s);
1136 if (tok->type == '+') {
1137 struct isl_qpolynomial *qp2;
1139 isl_token_free(tok);
1140 qp2 = read_factor(s, bmap, v);
1141 qp = isl_qpolynomial_add(qp, qp2);
1142 } else if (tok->type == '-') {
1143 struct isl_qpolynomial *qp2;
1145 isl_token_free(tok);
1146 qp2 = read_factor(s, bmap, v);
1147 qp = isl_qpolynomial_sub(qp, qp2);
1148 } else if (tok->type == ISL_TOKEN_VALUE &&
1149 isl_int_is_neg(tok->u.v)) {
1150 struct isl_qpolynomial *qp2;
1152 qp2 = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1154 isl_token_free(tok);
1155 qp = isl_qpolynomial_add(qp, qp2);
1157 isl_stream_push_token(s, tok);
1165 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1166 __isl_take isl_basic_map *bmap, struct vars *v)
1168 struct isl_token *tok;
1169 struct isl_map *map;
1171 tok = isl_stream_next_token(s);
1173 isl_stream_error(s, NULL, "unexpected EOF");
1176 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1177 if (tok->type == ':') {
1178 isl_token_free(tok);
1179 map = isl_map_intersect(map,
1180 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1182 isl_stream_push_token(s, tok);
1184 isl_basic_map_free(bmap);
1188 isl_basic_map_free(bmap);
1192 static struct isl_obj obj_read_poly(struct isl_stream *s,
1193 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1195 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1196 struct isl_pw_qpolynomial *pwqp;
1197 struct isl_qpolynomial *qp;
1198 struct isl_map *map;
1199 struct isl_set *set;
1201 bmap = isl_basic_map_reverse(bmap);
1203 qp = read_term(s, bmap, v);
1204 map = read_optional_disjuncts(s, bmap, v);
1205 set = isl_set_from_map(map);
1207 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1209 vars_drop(v, v->n - n);
1215 static struct isl_obj obj_read_body(struct isl_stream *s,
1216 __isl_take isl_basic_map *bmap, struct vars *v, isl_obj_type type)
1218 struct isl_map *map = NULL;
1219 struct isl_token *tok;
1220 struct isl_obj obj = { type, NULL };
1223 if (!isl_stream_next_token_is(s, '['))
1224 return obj_read_poly(s, bmap, v, n);
1226 bmap = read_tuple(s, bmap, isl_dim_in, v);
1229 tok = isl_stream_next_token(s);
1230 if (tok && tok->type == ISL_TOKEN_TO) {
1231 obj.type = isl_obj_map;
1232 isl_token_free(tok);
1233 tok = isl_stream_next_token(s);
1234 if (tok && tok->type != '[') {
1235 isl_stream_push_token(s, tok);
1236 return obj_read_poly(s, bmap, v, n);
1238 isl_stream_push_token(s, tok);
1239 bmap = read_tuple(s, bmap, isl_dim_out, v);
1243 bmap = isl_basic_map_reverse(bmap);
1245 isl_stream_push_token(s, tok);
1248 map = read_optional_disjuncts(s, bmap, v);
1250 vars_drop(v, v->n - n);
1255 isl_basic_map_free(bmap);
1256 obj.type = isl_obj_none;
1260 static struct isl_obj obj_add(struct isl_ctx *ctx,
1261 struct isl_obj obj1, struct isl_obj obj2)
1263 isl_assert(ctx, obj1.type == obj2.type, goto error);
1264 obj1.v = obj1.type->add(obj1.v, obj2.v);
1267 obj1.type->free(obj1.v);
1268 obj2.type->free(obj2.v);
1269 obj1.type = isl_obj_none;
1274 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1276 struct isl_basic_map *bmap = NULL;
1277 struct isl_token *tok;
1278 struct vars *v = NULL;
1279 struct isl_obj obj = { isl_obj_set, NULL };
1281 tok = isl_stream_next_token(s);
1283 isl_stream_error(s, NULL, "unexpected EOF");
1286 if (tok->type == ISL_TOKEN_VALUE) {
1287 struct isl_map *map;
1288 isl_stream_push_token(s, tok);
1289 map = map_read_polylib(s, nparam);
1292 if (isl_map_dim(map, isl_dim_in) > 0)
1293 obj.type = isl_obj_map;
1297 v = vars_new(s->ctx);
1298 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1299 if (tok->type == '[') {
1300 isl_stream_push_token(s, tok);
1301 bmap = read_tuple(s, bmap, isl_dim_param, v);
1305 isl_assert(s->ctx, nparam == v->n, goto error);
1306 tok = isl_stream_next_token(s);
1307 if (!tok || tok->type != ISL_TOKEN_TO) {
1308 isl_stream_error(s, tok, "expecting '->'");
1310 isl_stream_push_token(s, tok);
1313 isl_token_free(tok);
1314 tok = isl_stream_next_token(s);
1316 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1317 if (!tok || tok->type != '{') {
1318 isl_stream_error(s, tok, "expecting '{'");
1320 isl_stream_push_token(s, tok);
1323 isl_token_free(tok);
1328 o = obj_read_body(s, isl_basic_map_copy(bmap), v, obj.type);
1329 if (o.type == isl_obj_none)
1334 obj = obj_add(s->ctx, obj, o);
1335 if (obj.type == isl_obj_none)
1338 tok = isl_stream_next_token(s);
1339 if (!tok || tok->type != ';')
1341 isl_token_free(tok);
1344 if (tok && tok->type == '}') {
1345 isl_token_free(tok);
1347 isl_stream_error(s, tok, "unexpected isl_token");
1349 isl_token_free(tok);
1353 isl_basic_map_free(bmap);
1357 isl_basic_map_free(bmap);
1358 obj.type->free(obj.v);
1365 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1367 return obj_read(s, -1);
1370 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1373 struct isl_map *map;
1374 struct isl_basic_map *bmap;
1376 obj = obj_read(s, nparam);
1381 isl_assert(map->ctx, map->n <= 1, goto error);
1384 bmap = isl_basic_map_empty_like_map(map);
1386 bmap = isl_basic_map_copy(map->p[0]);
1396 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1397 FILE *input, int nparam)
1399 struct isl_basic_map *bmap;
1400 struct isl_stream *s = isl_stream_new_file(ctx, input);
1403 bmap = basic_map_read(s, nparam);
1408 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1409 FILE *input, int nparam)
1411 struct isl_basic_map *bmap;
1412 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1415 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1416 return (struct isl_basic_set *)bmap;
1418 isl_basic_map_free(bmap);
1422 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1423 const char *str, int nparam)
1425 struct isl_basic_map *bmap;
1426 struct isl_stream *s = isl_stream_new_str(ctx, str);
1429 bmap = basic_map_read(s, nparam);
1434 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1435 const char *str, int nparam)
1437 struct isl_basic_map *bmap;
1438 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1441 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1442 return (struct isl_basic_set *)bmap;
1444 isl_basic_map_free(bmap);
1448 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1449 FILE *input, int nparam)
1452 struct isl_map *map;
1453 struct isl_stream *s = isl_stream_new_file(ctx, input);
1456 obj = obj_read(s, nparam);
1461 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1462 const char *str, int nparam)
1465 struct isl_map *map;
1466 struct isl_stream *s = isl_stream_new_str(ctx, str);
1469 obj = obj_read(s, nparam);
1474 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1475 FILE *input, int nparam)
1477 struct isl_map *map;
1478 map = isl_map_read_from_file(ctx, input, nparam);
1481 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1482 return (struct isl_set *)map;
1488 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1489 const char *str, int nparam)
1491 struct isl_map *map;
1492 map = isl_map_read_from_str(ctx, str, nparam);
1495 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1496 return (struct isl_set *)map;
1502 static char *next_line(FILE *input, char *line, unsigned len)
1507 if (!(p = fgets(line, len, input)))
1509 while (isspace(*p) && *p != '\n')
1511 } while (*p == '#' || *p == '\n');
1516 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1519 struct isl_vec *vec = NULL;
1528 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1529 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1531 vec = isl_vec_alloc(ctx, size);
1533 p = next_line(input, line, sizeof(line));
1534 isl_assert(ctx, p, goto error);
1536 for (j = 0; j < size; ++j) {
1537 n = sscanf(p, "%s%n", val, &offset);
1538 isl_assert(ctx, n != 0, goto error);
1539 isl_int_read(vec->el[j], val);
1549 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1550 FILE *input, unsigned input_format)
1552 if (input_format == ISL_FORMAT_POLYLIB)
1553 return isl_vec_read_from_file_polylib(ctx, input);
1555 isl_assert(ctx, 0, return NULL);