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>
25 #include <isl_mat_private.h>
31 struct variable *next;
40 static struct vars *vars_new(struct isl_ctx *ctx)
43 v = isl_alloc_type(ctx, struct vars);
52 static void variable_free(struct variable *var)
55 struct variable *next = var->next;
56 isl_vec_free(var->def);
63 static void vars_free(struct vars *v)
71 static void vars_drop(struct vars *v, int n)
82 struct variable *next = var->next;
83 isl_vec_free(var->def);
91 static struct variable *variable_new(struct vars *v, const char *name, int len,
95 var = isl_alloc_type(v->ctx, struct variable);
98 var->name = strdup(name);
99 var->name[len] = '\0';
109 static int vars_pos(struct vars *v, const char *s, int len)
116 for (q = v->v; q; q = q->next) {
117 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
124 v->v = variable_new(v, s, len, v->n);
132 static int vars_add_anon(struct vars *v)
134 v->v = variable_new(v, "", 0, v->n);
143 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
144 enum isl_dim_type type, unsigned pos, char *name)
153 prime = strchr(name, '\'');
156 bmap = isl_basic_map_set_dim_name(bmap, type, pos, name);
163 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
165 struct isl_token *tok;
167 tok = isl_stream_next_token(s);
168 if (!tok || tok->type != ISL_TOKEN_VALUE) {
169 isl_stream_error(s, tok, "expecting constant value");
173 isl_int_mul(*f, *f, tok->u.v);
177 if (isl_stream_eat_if_available(s, '*'))
178 return accept_cst_factor(s, f);
186 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v);
187 static int read_div_definition(struct isl_stream *s, struct vars *v);
189 static __isl_give isl_vec *accept_affine_factor(struct isl_stream *s,
192 struct isl_token *tok = NULL;
195 tok = isl_stream_next_token(s);
197 isl_stream_error(s, NULL, "unexpected EOF");
200 if (tok->type == ISL_TOKEN_IDENT) {
202 int pos = vars_pos(v, tok->u.s, -1);
206 isl_stream_error(s, tok, "unknown identifier");
210 aff = isl_vec_alloc(v->ctx, 1 + v->n);
213 isl_seq_clr(aff->el, aff->size);
214 isl_int_set_si(aff->el[1 + pos], 1);
216 } else if (tok->type == ISL_TOKEN_VALUE) {
217 if (isl_stream_eat_if_available(s, '*')) {
218 aff = accept_affine_factor(s, v);
219 aff = isl_vec_scale(aff, tok->u.v);
221 aff = isl_vec_alloc(v->ctx, 1 + v->n);
224 isl_seq_clr(aff->el, aff->size);
225 isl_int_set(aff->el[0], tok->u.v);
228 } else if (tok->type == '(') {
231 aff = accept_affine(s, v);
234 if (isl_stream_eat(s, ')'))
236 } else if (tok->type == '[') {
237 if (vars_add_anon(v) < 0)
239 aff = isl_vec_alloc(v->ctx, 1 + v->n);
242 isl_seq_clr(aff->el, aff->size);
243 isl_int_set_si(aff->el[1 + v->n - 1], 1);
244 isl_stream_push_token(s, tok);
246 if (read_div_definition(s, v) < 0)
249 isl_stream_error(s, tok, "expecting factor");
252 if (isl_stream_eat_if_available(s, '*')) {
255 isl_int_set_si(f, 1);
256 if (accept_cst_factor(s, &f) < 0) {
260 aff = isl_vec_scale(aff, f);
271 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
273 struct isl_token *tok = NULL;
277 aff = isl_vec_alloc(v->ctx, 1 + v->n);
280 isl_seq_clr(aff->el, aff->size);
283 tok = isl_stream_next_token(s);
285 isl_stream_error(s, NULL, "unexpected EOF");
288 if (tok->type == '-') {
293 if (tok->type == '(' || tok->type == '[' ||
294 tok->type == ISL_TOKEN_IDENT) {
296 isl_stream_push_token(s, tok);
298 aff2 = accept_affine_factor(s, v);
300 aff2 = isl_vec_scale(aff2, s->ctx->negone);
301 aff = isl_vec_zero_extend(aff, 1 + v->n);
302 aff = isl_vec_add(aff, aff2);
306 } else if (tok->type == ISL_TOKEN_VALUE) {
308 isl_int_neg(tok->u.v, tok->u.v);
309 if (isl_stream_eat_if_available(s, '*') ||
310 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
312 aff2 = accept_affine_factor(s, v);
313 aff2 = isl_vec_scale(aff2, tok->u.v);
314 aff = isl_vec_zero_extend(aff, 1 + v->n);
315 aff = isl_vec_add(aff, aff2);
319 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
323 isl_stream_error(s, tok, "unexpected isl_token");
324 isl_stream_push_token(s, tok);
330 tok = isl_stream_next_token(s);
331 if (tok && tok->type == '-') {
334 } else if (tok && tok->type == '+') {
337 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
338 isl_int_is_neg(tok->u.v)) {
339 isl_stream_push_token(s, tok);
342 isl_stream_push_token(s, tok);
354 /* Add any variables in the variable list "v" that are not already in "bmap"
355 * as existentially quantified variables in "bmap".
357 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
362 struct variable *var;
364 extra = v->n - isl_basic_map_total_dim(bmap);
369 bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
370 extra, 0, 2 * extra);
372 for (i = 0; i < extra; ++i)
373 if (isl_basic_map_alloc_div(bmap) < 0)
376 for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
377 int k = bmap->n_div - 1 - i;
379 isl_seq_cpy(bmap->div[k], var->def->el, var->def->size);
380 isl_seq_clr(bmap->div[k] + var->def->size,
381 2 + v->n - var->def->size);
383 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
389 isl_basic_map_free(bmap);
393 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
394 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
397 isl_basic_map *def = NULL;
402 if (vars_add_anon(v) < 0)
406 vec = accept_affine(s, v);
410 dim = isl_basic_map_get_dim(bmap);
411 def = isl_basic_map_universe(dim);
412 def = add_divs(def, v);
413 def = isl_basic_map_extend_constraints(def, 1, 0);
414 k = isl_basic_map_alloc_equality(def);
416 isl_seq_cpy(def->eq[k], vec->el, vec->size);
417 isl_int_set_si(def->eq[k][1 + n - 1], -1);
423 vars_drop(v, v->n - n);
425 def = isl_basic_map_simplify(def);
426 def = isl_basic_map_finalize(def);
427 bmap = isl_basic_map_intersect(bmap, def);
430 isl_basic_map_free(bmap);
431 isl_basic_map_free(def);
435 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
436 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
439 struct isl_token *tok;
441 while ((tok = isl_stream_next_token(s)) != NULL) {
444 if (tok->type == ISL_TOKEN_IDENT) {
446 int p = vars_pos(v, tok->u.s, -1);
453 bmap = isl_basic_map_add(bmap, type, 1);
454 bmap = set_name(bmap, type, i, v->v->name);
456 } else if (tok->type == ISL_TOKEN_IDENT ||
457 tok->type == ISL_TOKEN_VALUE ||
460 if (type == isl_dim_param) {
461 isl_stream_error(s, tok,
462 "expecting unique identifier");
465 isl_stream_push_token(s, tok);
467 bmap = isl_basic_map_add(bmap, type, 1);
468 bmap = read_var_def(s, bmap, type, v);
472 tok = isl_stream_next_token(s);
473 if (!tok || tok->type != ',')
480 isl_stream_push_token(s, tok);
485 isl_basic_map_free(bmap);
489 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
494 struct isl_token *tok = NULL;
496 vec = accept_affine(s, v);
497 mat = isl_mat_from_row_vec(vec);
502 tok = isl_stream_next_token(s);
504 isl_stream_error(s, NULL, "unexpected EOF");
507 if (tok->type != ',') {
508 isl_stream_push_token(s, tok);
513 vec = accept_affine(s, v);
514 mat = isl_mat_add_zero_cols(mat, 1 + v->n - isl_mat_cols(mat));
515 mat = isl_mat_vec_concat(mat, vec);
526 static int read_div_definition(struct isl_stream *s, struct vars *v)
528 struct isl_token *tok;
531 struct variable *var;
533 if (isl_stream_eat(s, '['))
536 tok = isl_stream_next_token(s);
539 if (tok->type == '(') {
543 isl_stream_push_token(s, tok);
547 aff = accept_affine(s, v);
551 var->def = isl_vec_alloc(s->ctx, 2 + v->n);
557 isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
561 if (seen_paren && isl_stream_eat(s, ')'))
563 if (isl_stream_eat(s, '/'))
566 tok = isl_stream_next_token(s);
569 if (tok->type != ISL_TOKEN_VALUE) {
570 isl_stream_error(s, tok, "expected denominator");
571 isl_stream_push_token(s, tok);
574 isl_int_set(var->def->el[0], tok->u.v);
577 if (isl_stream_eat(s, ']'))
583 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
584 struct vars *v, struct isl_basic_map *bmap, int k)
586 struct variable *var = v->v;
588 if (read_div_definition(s, v) < 0)
591 isl_seq_cpy(bmap->div[k], var->def->el, 2 + v->n);
593 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
598 isl_basic_map_free(bmap);
602 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
603 struct vars *v, struct isl_basic_map *bmap)
605 struct isl_token *tok;
607 while ((tok = isl_stream_next_token(s)) != NULL) {
611 unsigned total = isl_basic_map_total_dim(bmap);
613 if (tok->type != ISL_TOKEN_IDENT)
616 p = vars_pos(v, tok->u.s, -1);
620 isl_stream_error(s, tok, "expecting unique identifier");
624 bmap = isl_basic_map_cow(bmap);
625 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
628 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
630 isl_seq_clr(bmap->div[k], 1 + 1 + total);
633 tok = isl_stream_next_token(s);
634 if (tok && tok->type == '=') {
636 bmap = add_div_definition(s, v, bmap, k);
637 tok = isl_stream_next_token(s);
640 if (!tok || tok->type != ',')
646 isl_stream_push_token(s, tok);
651 isl_basic_map_free(bmap);
655 static int next_is_tuple(struct isl_stream *s)
657 struct isl_token *tok;
660 tok = isl_stream_next_token(s);
663 if (tok->type == '[') {
664 isl_stream_push_token(s, tok);
667 if (tok->type != ISL_TOKEN_IDENT) {
668 isl_stream_push_token(s, tok);
672 is_tuple = isl_stream_next_token_is(s, '[');
674 isl_stream_push_token(s, tok);
679 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
680 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v);
682 static __isl_give isl_basic_map *read_nested_tuple(struct isl_stream *s,
683 __isl_take isl_basic_map *bmap, struct vars *v)
685 bmap = read_tuple(s, bmap, isl_dim_in, v);
686 if (isl_stream_eat(s, ISL_TOKEN_TO))
688 bmap = read_tuple(s, bmap, isl_dim_out, v);
689 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
692 isl_basic_map_free(bmap);
696 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
697 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
699 struct isl_token *tok;
702 tok = isl_stream_next_token(s);
703 if (tok && tok->type == ISL_TOKEN_IDENT) {
704 name = strdup(tok->u.s);
708 tok = isl_stream_next_token(s);
710 if (!tok || tok->type != '[') {
711 isl_stream_error(s, tok, "expecting '['");
715 if (type != isl_dim_param && next_is_tuple(s)) {
716 isl_dim *dim = isl_basic_map_get_dim(bmap);
717 int nparam = isl_dim_size(dim, isl_dim_param);
718 int n_in = isl_dim_size(dim, isl_dim_in);
719 isl_basic_map *nested;
720 if (type == isl_dim_out)
721 dim = isl_dim_move(dim, isl_dim_param, nparam,
722 isl_dim_in, 0, n_in);
723 nested = isl_basic_map_alloc_dim(dim, 0, 0, 0);
724 nested = read_nested_tuple(s, nested, v);
725 if (type == isl_dim_in) {
726 nested = isl_basic_map_reverse(nested);
727 bmap = isl_basic_map_intersect(nested, bmap);
730 dim = isl_dim_range(isl_basic_map_get_dim(nested));
731 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
732 dim = isl_dim_join(isl_basic_map_get_dim(bmap), dim);
733 bset = isl_basic_map_domain(bmap);
734 nested = isl_basic_map_reset_dim(nested, dim);
735 bmap = isl_basic_map_intersect_domain(nested, bset);
738 bmap = read_var_list(s, bmap, type, v);
739 tok = isl_stream_next_token(s);
740 if (!tok || tok->type != ']') {
741 isl_stream_error(s, tok, "expecting ']'");
747 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
755 isl_basic_map_free(bmap);
759 static struct isl_basic_map *add_constraints(struct isl_stream *s,
760 struct vars *v, struct isl_basic_map *bmap);
762 static struct isl_basic_map *add_exists(struct isl_stream *s,
763 struct vars *v, struct isl_basic_map *bmap)
765 struct isl_token *tok;
772 tok = isl_stream_next_token(s);
775 if (tok->type == '(') {
779 isl_stream_push_token(s, tok);
781 bmap = read_defined_var_list(s, v, bmap);
785 if (isl_stream_eat(s, ':'))
787 bmap = add_constraints(s, v, bmap);
788 if (seen_paren && isl_stream_eat(s, ')'))
792 isl_basic_map_free(bmap);
796 static __isl_give isl_basic_map *construct_constraint(
797 __isl_take isl_basic_map *bmap, enum isl_token_type type,
798 isl_int *left, isl_int *right)
806 len = 1 + isl_basic_map_total_dim(bmap);
809 k = isl_basic_map_alloc_inequality(bmap);
812 if (type == ISL_TOKEN_LE)
813 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
816 else if (type == ISL_TOKEN_GE)
817 isl_seq_combine(bmap->ineq[k], ctx->one, left,
820 else if (type == ISL_TOKEN_LT) {
821 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
824 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
825 } else if (type == ISL_TOKEN_GT) {
826 isl_seq_combine(bmap->ineq[k], ctx->one, left,
829 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
831 isl_seq_combine(bmap->ineq[k], ctx->one, left,
834 isl_basic_map_inequality_to_equality(bmap, k);
839 isl_basic_map_free(bmap);
843 static int is_comparator(struct isl_token *tok)
860 static struct isl_basic_map *add_constraint(struct isl_stream *s,
861 struct vars *v, struct isl_basic_map *bmap)
864 struct isl_token *tok = NULL;
865 struct isl_mat *aff1 = NULL, *aff2 = NULL;
867 tok = isl_stream_next_token(s);
870 if (tok->type == ISL_TOKEN_EXISTS) {
872 return add_exists(s, v, bmap);
874 isl_stream_push_token(s, tok);
877 bmap = isl_basic_map_cow(bmap);
879 aff1 = accept_affine_list(s, v);
882 tok = isl_stream_next_token(s);
883 if (!is_comparator(tok)) {
884 isl_stream_error(s, tok, "missing operator");
886 isl_stream_push_token(s, tok);
891 aff2 = accept_affine_list(s, v);
895 aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
898 bmap = add_divs(bmap, v);
899 bmap = isl_basic_map_extend_constraints(bmap, 0,
900 aff1->n_row * aff2->n_row);
901 for (i = 0; i < aff1->n_row; ++i)
902 for (j = 0; j < aff2->n_row; ++j)
903 bmap = construct_constraint(bmap, tok->type,
904 aff1->row[i], aff2->row[j]);
909 tok = isl_stream_next_token(s);
910 if (!is_comparator(tok)) {
912 isl_stream_push_token(s, tok);
924 isl_basic_map_free(bmap);
928 static struct isl_basic_map *add_constraints(struct isl_stream *s,
929 struct vars *v, struct isl_basic_map *bmap)
931 struct isl_token *tok;
934 bmap = add_constraint(s, v, bmap);
937 tok = isl_stream_next_token(s);
939 isl_stream_error(s, NULL, "unexpected EOF");
942 if (tok->type != ISL_TOKEN_AND)
946 isl_stream_push_token(s, tok);
952 isl_basic_map_free(bmap);
956 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
957 struct vars *v, __isl_take isl_dim *dim)
960 struct isl_token *tok;
961 struct isl_basic_map *bmap;
963 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
967 tok = isl_stream_next_token(s);
970 if (tok->type == '(') {
974 isl_stream_push_token(s, tok);
976 bmap = add_constraints(s, v, bmap);
977 bmap = isl_basic_map_simplify(bmap);
978 bmap = isl_basic_map_finalize(bmap);
980 if (seen_paren && isl_stream_eat(s, ')'))
985 isl_basic_map_free(bmap);
989 static struct isl_map *read_disjuncts(struct isl_stream *s,
990 struct vars *v, __isl_take isl_dim *dim)
992 struct isl_token *tok;
995 tok = isl_stream_next_token(s);
997 isl_stream_error(s, NULL, "unexpected EOF");
1000 if (tok->type == '}') {
1001 isl_stream_push_token(s, tok);
1002 return isl_map_universe(dim);
1004 isl_stream_push_token(s, tok);
1006 map = isl_map_empty(isl_dim_copy(dim));
1008 struct isl_basic_map *bmap;
1011 bmap = read_disjunct(s, v, isl_dim_copy(dim));
1012 map = isl_map_union(map, isl_map_from_basic_map(bmap));
1014 vars_drop(v, v->n - n);
1016 tok = isl_stream_next_token(s);
1017 if (!tok || tok->type != ISL_TOKEN_OR)
1019 isl_token_free(tok);
1022 isl_stream_push_token(s, tok);
1031 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1033 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1034 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1035 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1036 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1038 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1039 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1040 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1042 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1043 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1044 isl_basic_map_dim(bmap, isl_dim_in) +
1045 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1046 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1048 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1054 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1055 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1058 struct isl_token *tok;
1068 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1069 dim = isl_basic_map_dim(bmap, isl_dim_out);
1071 tok = isl_stream_next_token(s);
1072 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1073 isl_stream_error(s, tok, "expecting coefficient");
1075 isl_stream_push_token(s, tok);
1078 if (!tok->on_new_line) {
1079 isl_stream_error(s, tok, "coefficient should appear on new line");
1080 isl_stream_push_token(s, tok);
1084 type = isl_int_get_si(tok->u.v);
1085 isl_token_free(tok);
1087 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1089 k = isl_basic_map_alloc_equality(bmap);
1092 k = isl_basic_map_alloc_inequality(bmap);
1098 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1100 tok = isl_stream_next_token(s);
1101 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1102 isl_stream_error(s, tok, "expecting coefficient");
1104 isl_stream_push_token(s, tok);
1107 if (tok->on_new_line) {
1108 isl_stream_error(s, tok,
1109 "coefficient should not appear on new line");
1110 isl_stream_push_token(s, tok);
1113 pos = polylib_pos_to_isl_pos(bmap, j);
1114 isl_int_set(c[pos], tok->u.v);
1115 isl_token_free(tok);
1120 isl_basic_map_free(bmap);
1124 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1128 struct isl_token *tok;
1129 struct isl_token *tok2;
1132 unsigned in = 0, out, local = 0;
1133 struct isl_basic_map *bmap = NULL;
1138 tok = isl_stream_next_token(s);
1140 isl_stream_error(s, NULL, "unexpected EOF");
1143 tok2 = isl_stream_next_token(s);
1145 isl_token_free(tok);
1146 isl_stream_error(s, NULL, "unexpected EOF");
1149 n_row = isl_int_get_si(tok->u.v);
1150 n_col = isl_int_get_si(tok2->u.v);
1151 on_new_line = tok2->on_new_line;
1152 isl_token_free(tok2);
1153 isl_token_free(tok);
1154 isl_assert(s->ctx, !on_new_line, return NULL);
1155 isl_assert(s->ctx, n_row >= 0, return NULL);
1156 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1157 tok = isl_stream_next_token_on_same_line(s);
1159 if (tok->type != ISL_TOKEN_VALUE) {
1160 isl_stream_error(s, tok,
1161 "expecting number of output dimensions");
1162 isl_stream_push_token(s, tok);
1165 out = isl_int_get_si(tok->u.v);
1166 isl_token_free(tok);
1168 tok = isl_stream_next_token_on_same_line(s);
1169 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1170 isl_stream_error(s, tok,
1171 "expecting number of input dimensions");
1173 isl_stream_push_token(s, tok);
1176 in = isl_int_get_si(tok->u.v);
1177 isl_token_free(tok);
1179 tok = isl_stream_next_token_on_same_line(s);
1180 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1181 isl_stream_error(s, tok,
1182 "expecting number of existentials");
1184 isl_stream_push_token(s, tok);
1187 local = isl_int_get_si(tok->u.v);
1188 isl_token_free(tok);
1190 tok = isl_stream_next_token_on_same_line(s);
1191 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1192 isl_stream_error(s, tok,
1193 "expecting number of parameters");
1195 isl_stream_push_token(s, tok);
1198 nparam = isl_int_get_si(tok->u.v);
1199 isl_token_free(tok);
1200 if (n_col != 1 + out + in + local + nparam + 1) {
1201 isl_stream_error(s, NULL,
1202 "dimensions don't match");
1206 out = n_col - 2 - nparam;
1207 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1211 for (i = 0; i < local; ++i) {
1212 int k = isl_basic_map_alloc_div(bmap);
1215 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1218 for (i = 0; i < n_row; ++i)
1219 bmap = basic_map_read_polylib_constraint(s, bmap);
1221 tok = isl_stream_next_token_on_same_line(s);
1223 isl_stream_error(s, tok, "unexpected extra token on line");
1224 isl_stream_push_token(s, tok);
1228 bmap = isl_basic_map_simplify(bmap);
1229 bmap = isl_basic_map_finalize(bmap);
1232 isl_basic_map_free(bmap);
1236 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1238 struct isl_token *tok;
1239 struct isl_token *tok2;
1241 struct isl_map *map;
1243 tok = isl_stream_next_token(s);
1245 isl_stream_error(s, NULL, "unexpected EOF");
1248 tok2 = isl_stream_next_token_on_same_line(s);
1250 isl_stream_push_token(s, tok2);
1251 isl_stream_push_token(s, tok);
1252 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1254 n = isl_int_get_si(tok->u.v);
1255 isl_token_free(tok);
1257 isl_assert(s->ctx, n >= 1, return NULL);
1259 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1261 for (i = 1; i < n; ++i)
1262 map = isl_map_union(map,
1263 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1268 static int optional_power(struct isl_stream *s)
1271 struct isl_token *tok;
1273 tok = isl_stream_next_token(s);
1276 if (tok->type != '^') {
1277 isl_stream_push_token(s, tok);
1280 isl_token_free(tok);
1281 tok = isl_stream_next_token(s);
1282 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1283 isl_stream_error(s, tok, "expecting exponent");
1285 isl_stream_push_token(s, tok);
1288 pow = isl_int_get_si(tok->u.v);
1289 isl_token_free(tok);
1293 static __isl_give isl_div *read_div(struct isl_stream *s,
1294 __isl_take isl_dim *dim, struct vars *v)
1297 isl_basic_map *bmap;
1300 bmap = isl_basic_map_universe(dim);
1302 if (vars_add_anon(v) < 0)
1304 if (read_div_definition(s, v) < 0)
1306 bmap = add_divs(bmap, v);
1307 bmap = isl_basic_map_order_divs(bmap);
1310 vars_drop(v, v->n - n);
1312 return isl_basic_map_div(bmap, bmap->n_div - 1);
1314 isl_basic_map_free(bmap);
1318 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1319 __isl_keep isl_basic_map *bmap, struct vars *v);
1321 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1322 __isl_keep isl_basic_map *bmap, struct vars *v)
1324 struct isl_qpolynomial *qp;
1325 struct isl_token *tok;
1327 tok = isl_stream_next_token(s);
1329 isl_stream_error(s, NULL, "unexpected EOF");
1332 if (tok->type == '(') {
1333 isl_token_free(tok);
1334 qp = read_term(s, bmap, v);
1337 if (isl_stream_eat(s, ')'))
1339 } else if (tok->type == ISL_TOKEN_VALUE) {
1340 struct isl_token *tok2;
1341 tok2 = isl_stream_next_token(s);
1342 if (tok2 && tok2->type == '/') {
1343 isl_token_free(tok2);
1344 tok2 = isl_stream_next_token(s);
1345 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1346 isl_stream_error(s, tok2, "expected denominator");
1347 isl_token_free(tok);
1348 isl_token_free(tok2);
1351 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1352 tok->u.v, tok2->u.v);
1353 isl_token_free(tok2);
1355 isl_stream_push_token(s, tok2);
1356 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1359 isl_token_free(tok);
1360 } else if (tok->type == ISL_TOKEN_INFTY) {
1361 isl_token_free(tok);
1362 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1363 } else if (tok->type == ISL_TOKEN_NAN) {
1364 isl_token_free(tok);
1365 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1366 } else if (tok->type == ISL_TOKEN_IDENT) {
1368 int pos = vars_pos(v, tok->u.s, -1);
1371 isl_token_free(tok);
1375 isl_stream_error(s, tok, "unknown identifier");
1376 isl_token_free(tok);
1379 isl_token_free(tok);
1380 pow = optional_power(s);
1381 qp = isl_qpolynomial_pow(isl_basic_map_get_dim(bmap), pos, pow);
1382 } else if (tok->type == '[') {
1386 isl_stream_push_token(s, tok);
1387 div = read_div(s, isl_basic_map_get_dim(bmap), v);
1388 pow = optional_power(s);
1389 qp = isl_qpolynomial_div_pow(div, pow);
1390 } else if (tok->type == '-') {
1391 struct isl_qpolynomial *qp2;
1393 isl_token_free(tok);
1394 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1396 qp2 = read_factor(s, bmap, v);
1397 qp = isl_qpolynomial_mul(qp, qp2);
1399 isl_stream_error(s, tok, "unexpected isl_token");
1400 isl_stream_push_token(s, tok);
1404 if (isl_stream_eat_if_available(s, '*') ||
1405 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1406 struct isl_qpolynomial *qp2;
1408 qp2 = read_factor(s, bmap, v);
1409 qp = isl_qpolynomial_mul(qp, qp2);
1414 isl_qpolynomial_free(qp);
1418 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1419 __isl_keep isl_basic_map *bmap, struct vars *v)
1421 struct isl_token *tok;
1422 struct isl_qpolynomial *qp;
1424 qp = read_factor(s, bmap, v);
1427 tok = isl_stream_next_token(s);
1431 if (tok->type == '+') {
1432 struct isl_qpolynomial *qp2;
1434 isl_token_free(tok);
1435 qp2 = read_factor(s, bmap, v);
1436 qp = isl_qpolynomial_add(qp, qp2);
1437 } else if (tok->type == '-') {
1438 struct isl_qpolynomial *qp2;
1440 isl_token_free(tok);
1441 qp2 = read_factor(s, bmap, v);
1442 qp = isl_qpolynomial_sub(qp, qp2);
1443 } else if (tok->type == ISL_TOKEN_VALUE &&
1444 isl_int_is_neg(tok->u.v)) {
1445 struct isl_qpolynomial *qp2;
1447 isl_stream_push_token(s, tok);
1448 qp2 = read_factor(s, bmap, v);
1449 qp = isl_qpolynomial_add(qp, qp2);
1451 isl_stream_push_token(s, tok);
1459 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1460 __isl_take isl_basic_map *bmap, struct vars *v)
1462 struct isl_token *tok;
1463 struct isl_map *map;
1465 tok = isl_stream_next_token(s);
1467 isl_stream_error(s, NULL, "unexpected EOF");
1470 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1471 if (tok->type == ':') {
1472 isl_token_free(tok);
1473 map = isl_map_intersect(map,
1474 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1476 isl_stream_push_token(s, tok);
1478 isl_basic_map_free(bmap);
1482 isl_basic_map_free(bmap);
1486 static struct isl_obj obj_read_poly(struct isl_stream *s,
1487 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1489 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1490 struct isl_pw_qpolynomial *pwqp;
1491 struct isl_qpolynomial *qp;
1492 struct isl_map *map;
1493 struct isl_set *set;
1495 qp = read_term(s, bmap, v);
1496 map = read_optional_disjuncts(s, bmap, v);
1497 set = isl_map_range(map);
1499 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1501 vars_drop(v, v->n - n);
1507 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1508 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1510 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1511 struct isl_obj obj_p;
1512 isl_qpolynomial *qp;
1513 isl_qpolynomial_fold *fold = NULL;
1514 isl_pw_qpolynomial_fold *pwf;
1518 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1519 return obj_read_poly(s, bmap, v, n);
1521 if (isl_stream_eat(s, '('))
1524 qp = read_term(s, bmap, v);
1525 fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1527 while (isl_stream_eat_if_available(s, ',')) {
1528 isl_qpolynomial_fold *fold_i;
1529 qp = read_term(s, bmap, v);
1530 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1531 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1534 if (isl_stream_eat(s, ')'))
1537 map = read_optional_disjuncts(s, bmap, v);
1538 set = isl_map_range(map);
1539 pwf = isl_pw_qpolynomial_fold_alloc(isl_fold_max, set, fold);
1541 vars_drop(v, v->n - n);
1546 isl_basic_map_free(bmap);
1547 isl_qpolynomial_fold_free(fold);
1548 obj.type = isl_obj_none;
1552 static struct isl_obj obj_read_body(struct isl_stream *s,
1553 __isl_take isl_basic_map *bmap, struct vars *v)
1555 struct isl_map *map = NULL;
1556 struct isl_token *tok;
1557 struct isl_obj obj = { isl_obj_set, NULL };
1560 if (!next_is_tuple(s))
1561 return obj_read_poly_or_fold(s, bmap, v, n);
1563 bmap = read_tuple(s, bmap, isl_dim_in, v);
1566 tok = isl_stream_next_token(s);
1567 if (tok && tok->type == ISL_TOKEN_TO) {
1568 obj.type = isl_obj_map;
1569 isl_token_free(tok);
1570 if (!next_is_tuple(s)) {
1571 bmap = isl_basic_map_reverse(bmap);
1572 return obj_read_poly_or_fold(s, bmap, v, n);
1574 bmap = read_tuple(s, bmap, isl_dim_out, v);
1578 bmap = isl_basic_map_reverse(bmap);
1580 isl_stream_push_token(s, tok);
1583 map = read_optional_disjuncts(s, bmap, v);
1585 vars_drop(v, v->n - n);
1590 isl_basic_map_free(bmap);
1591 obj.type = isl_obj_none;
1595 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1597 if (obj.type == isl_obj_map) {
1598 obj.v = isl_union_map_from_map(obj.v);
1599 obj.type = isl_obj_union_map;
1600 } else if (obj.type == isl_obj_set) {
1601 obj.v = isl_union_set_from_set(obj.v);
1602 obj.type = isl_obj_union_set;
1603 } else if (obj.type == isl_obj_pw_qpolynomial) {
1604 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1605 obj.type = isl_obj_union_pw_qpolynomial;
1606 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1607 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1608 obj.type = isl_obj_union_pw_qpolynomial_fold;
1610 isl_assert(ctx, 0, goto error);
1613 obj.type->free(obj.v);
1614 obj.type = isl_obj_none;
1618 static struct isl_obj obj_add(struct isl_ctx *ctx,
1619 struct isl_obj obj1, struct isl_obj obj2)
1621 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1622 obj1 = to_union(ctx, obj1);
1623 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1624 obj2 = to_union(ctx, obj2);
1625 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1626 obj1 = to_union(ctx, obj1);
1627 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1628 obj2 = to_union(ctx, obj2);
1629 if (obj1.type == isl_obj_pw_qpolynomial &&
1630 obj2.type == isl_obj_union_pw_qpolynomial)
1631 obj1 = to_union(ctx, obj1);
1632 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1633 obj2.type == isl_obj_pw_qpolynomial)
1634 obj2 = to_union(ctx, obj2);
1635 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1636 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1637 obj1 = to_union(ctx, obj1);
1638 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1639 obj2.type == isl_obj_pw_qpolynomial_fold)
1640 obj2 = to_union(ctx, obj2);
1641 isl_assert(ctx, obj1.type == obj2.type, goto error);
1642 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1643 obj1 = to_union(ctx, obj1);
1644 obj2 = to_union(ctx, obj2);
1646 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1647 obj1 = to_union(ctx, obj1);
1648 obj2 = to_union(ctx, obj2);
1650 if (obj1.type == isl_obj_pw_qpolynomial &&
1651 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1652 obj1 = to_union(ctx, obj1);
1653 obj2 = to_union(ctx, obj2);
1655 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1656 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1657 obj1 = to_union(ctx, obj1);
1658 obj2 = to_union(ctx, obj2);
1660 obj1.v = obj1.type->add(obj1.v, obj2.v);
1663 obj1.type->free(obj1.v);
1664 obj2.type->free(obj2.v);
1665 obj1.type = isl_obj_none;
1670 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1672 isl_basic_map *bmap = NULL;
1673 struct isl_token *tok;
1674 struct vars *v = NULL;
1675 struct isl_obj obj = { isl_obj_set, NULL };
1677 tok = isl_stream_next_token(s);
1679 isl_stream_error(s, NULL, "unexpected EOF");
1682 if (tok->type == ISL_TOKEN_VALUE) {
1683 struct isl_map *map;
1684 isl_stream_push_token(s, tok);
1685 map = map_read_polylib(s, nparam);
1688 if (isl_map_dim(map, isl_dim_in) > 0)
1689 obj.type = isl_obj_map;
1693 v = vars_new(s->ctx);
1695 isl_stream_push_token(s, tok);
1698 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1699 if (tok->type == '[') {
1700 isl_stream_push_token(s, tok);
1701 bmap = read_tuple(s, bmap, isl_dim_param, v);
1705 isl_assert(s->ctx, nparam == v->n, goto error);
1706 tok = isl_stream_next_token(s);
1707 if (!tok || tok->type != ISL_TOKEN_TO) {
1708 isl_stream_error(s, tok, "expecting '->'");
1710 isl_stream_push_token(s, tok);
1713 isl_token_free(tok);
1714 tok = isl_stream_next_token(s);
1715 } else if (nparam > 0)
1716 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1717 if (!tok || tok->type != '{') {
1718 isl_stream_error(s, tok, "expecting '{'");
1720 isl_stream_push_token(s, tok);
1723 isl_token_free(tok);
1725 tok = isl_stream_next_token(s);
1728 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1729 isl_token_free(tok);
1730 if (isl_stream_eat(s, '='))
1732 bmap = read_tuple(s, bmap, isl_dim_param, v);
1736 isl_assert(s->ctx, nparam == v->n, goto error);
1737 } else if (tok->type == '}') {
1738 obj.type = isl_obj_union_set;
1739 obj.v = isl_union_set_empty(isl_basic_map_get_dim(bmap));
1740 isl_token_free(tok);
1743 isl_stream_push_token(s, tok);
1748 o = obj_read_body(s, isl_basic_map_copy(bmap), v);
1749 if (o.type == isl_obj_none || !o.v)
1754 obj = obj_add(s->ctx, obj, o);
1755 if (obj.type == isl_obj_none || !obj.v)
1758 tok = isl_stream_next_token(s);
1759 if (!tok || tok->type != ';')
1761 isl_token_free(tok);
1764 if (tok && tok->type == '}') {
1765 isl_token_free(tok);
1767 isl_stream_error(s, tok, "unexpected isl_token");
1769 isl_token_free(tok);
1774 isl_basic_map_free(bmap);
1778 isl_basic_map_free(bmap);
1779 obj.type->free(obj.v);
1786 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1788 return obj_read(s, -1);
1791 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1794 struct isl_map *map;
1796 obj = obj_read(s, nparam);
1798 isl_assert(s->ctx, obj.type == isl_obj_map ||
1799 obj.type == isl_obj_set, goto error);
1803 obj.type->free(obj.v);
1807 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1810 struct isl_set *set;
1812 obj = obj_read(s, nparam);
1814 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1818 obj.type->free(obj.v);
1822 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1825 isl_union_map *umap;
1827 obj = obj_read(s, -1);
1828 if (obj.type == isl_obj_map) {
1829 obj.type = isl_obj_union_map;
1830 obj.v = isl_union_map_from_map(obj.v);
1832 if (obj.type == isl_obj_set) {
1833 obj.type = isl_obj_union_set;
1834 obj.v = isl_union_set_from_set(obj.v);
1837 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1838 obj.type == isl_obj_union_set, goto error);
1842 obj.type->free(obj.v);
1846 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
1849 isl_union_set *uset;
1851 obj = obj_read(s, -1);
1852 if (obj.type == isl_obj_set) {
1853 obj.type = isl_obj_union_set;
1854 obj.v = isl_union_set_from_set(obj.v);
1857 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1861 obj.type->free(obj.v);
1865 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1868 struct isl_map *map;
1869 struct isl_basic_map *bmap;
1871 obj = obj_read(s, nparam);
1876 isl_assert(map->ctx, map->n <= 1, goto error);
1879 bmap = isl_basic_map_empty_like_map(map);
1881 bmap = isl_basic_map_copy(map->p[0]);
1891 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1892 FILE *input, int nparam)
1894 struct isl_basic_map *bmap;
1895 struct isl_stream *s = isl_stream_new_file(ctx, input);
1898 bmap = basic_map_read(s, nparam);
1903 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1904 FILE *input, int nparam)
1906 struct isl_basic_map *bmap;
1907 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1910 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1911 return (struct isl_basic_set *)bmap;
1913 isl_basic_map_free(bmap);
1917 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1918 const char *str, int nparam)
1920 struct isl_basic_map *bmap;
1921 struct isl_stream *s = isl_stream_new_str(ctx, str);
1924 bmap = basic_map_read(s, nparam);
1929 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1930 const char *str, int nparam)
1932 struct isl_basic_map *bmap;
1933 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1936 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1937 return (struct isl_basic_set *)bmap;
1939 isl_basic_map_free(bmap);
1943 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1944 FILE *input, int nparam)
1946 struct isl_map *map;
1947 struct isl_stream *s = isl_stream_new_file(ctx, input);
1950 map = isl_stream_read_map(s, nparam);
1955 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1956 const char *str, int nparam)
1958 struct isl_map *map;
1959 struct isl_stream *s = isl_stream_new_str(ctx, str);
1962 map = isl_stream_read_map(s, nparam);
1967 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1968 FILE *input, int nparam)
1970 struct isl_map *map;
1971 map = isl_map_read_from_file(ctx, input, nparam);
1974 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1975 return (struct isl_set *)map;
1981 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1982 const char *str, int nparam)
1984 struct isl_map *map;
1985 map = isl_map_read_from_str(ctx, str, nparam);
1988 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1989 return (struct isl_set *)map;
1995 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
1998 isl_union_map *umap;
1999 struct isl_stream *s = isl_stream_new_str(ctx, str);
2002 umap = isl_stream_read_union_map(s);
2007 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2010 isl_union_set *uset;
2011 struct isl_stream *s = isl_stream_new_str(ctx, str);
2014 uset = isl_stream_read_union_set(s);
2019 static char *next_line(FILE *input, char *line, unsigned len)
2024 if (!(p = fgets(line, len, input)))
2026 while (isspace(*p) && *p != '\n')
2028 } while (*p == '#' || *p == '\n');
2033 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
2036 struct isl_vec *vec = NULL;
2045 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
2046 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
2048 vec = isl_vec_alloc(ctx, size);
2050 p = next_line(input, line, sizeof(line));
2051 isl_assert(ctx, p, goto error);
2053 for (j = 0; j < size; ++j) {
2054 n = sscanf(p, "%s%n", val, &offset);
2055 isl_assert(ctx, n != 0, goto error);
2056 isl_int_read(vec->el[j], val);
2066 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
2067 FILE *input, unsigned input_format)
2069 if (input_format == ISL_FORMAT_POLYLIB)
2070 return isl_vec_read_from_file_polylib(ctx, input);
2072 isl_assert(ctx, 0, return NULL);
2075 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2076 struct isl_stream *s)
2079 struct isl_pw_qpolynomial *pwqp;
2081 obj = obj_read(s, -1);
2083 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2088 obj.type->free(obj.v);
2092 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2095 isl_pw_qpolynomial *pwqp;
2096 struct isl_stream *s = isl_stream_new_str(ctx, str);
2099 pwqp = isl_stream_read_pw_qpolynomial(s);