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);
272 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
274 struct isl_token *tok = NULL;
278 aff = isl_vec_alloc(v->ctx, 1 + v->n);
281 isl_seq_clr(aff->el, aff->size);
284 tok = isl_stream_next_token(s);
286 isl_stream_error(s, NULL, "unexpected EOF");
289 if (tok->type == '-') {
294 if (tok->type == '(' || tok->type == '[' ||
295 tok->type == ISL_TOKEN_IDENT) {
297 isl_stream_push_token(s, tok);
299 aff2 = accept_affine_factor(s, v);
301 aff2 = isl_vec_scale(aff2, s->ctx->negone);
302 aff = isl_vec_zero_extend(aff, 1 + v->n);
303 aff = isl_vec_add(aff, aff2);
307 } else if (tok->type == ISL_TOKEN_VALUE) {
309 isl_int_neg(tok->u.v, tok->u.v);
310 if (isl_stream_eat_if_available(s, '*') ||
311 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
313 aff2 = accept_affine_factor(s, v);
314 aff2 = isl_vec_scale(aff2, tok->u.v);
315 aff = isl_vec_zero_extend(aff, 1 + v->n);
316 aff = isl_vec_add(aff, aff2);
320 isl_int_add(aff->el[0], aff->el[0], tok->u.v);
324 isl_stream_error(s, tok, "unexpected isl_token");
325 isl_stream_push_token(s, tok);
331 tok = isl_stream_next_token(s);
332 if (tok && tok->type == '-') {
335 } else if (tok && tok->type == '+') {
338 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
339 isl_int_is_neg(tok->u.v)) {
340 isl_stream_push_token(s, tok);
343 isl_stream_push_token(s, tok);
355 /* Add any variables in the variable list "v" that are not already in "bmap"
356 * as existentially quantified variables in "bmap".
358 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
363 struct variable *var;
365 extra = v->n - isl_basic_map_total_dim(bmap);
370 bmap = isl_basic_map_extend_dim(bmap, isl_basic_map_get_dim(bmap),
371 extra, 0, 2 * extra);
373 for (i = 0; i < extra; ++i)
374 if (isl_basic_map_alloc_div(bmap) < 0)
377 for (i = 0, var = v->v; i < extra; ++i, var = var->next) {
378 int k = bmap->n_div - 1 - i;
380 isl_seq_cpy(bmap->div[k], var->def->el, var->def->size);
381 isl_seq_clr(bmap->div[k] + var->def->size,
382 2 + v->n - var->def->size);
384 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
390 isl_basic_map_free(bmap);
394 static __isl_give isl_basic_map *read_var_def(struct isl_stream *s,
395 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
398 isl_basic_map *def = NULL;
403 if (vars_add_anon(v) < 0)
407 vec = accept_affine(s, v);
411 dim = isl_basic_map_get_dim(bmap);
412 def = isl_basic_map_universe(dim);
413 def = add_divs(def, v);
414 def = isl_basic_map_extend_constraints(def, 1, 0);
415 k = isl_basic_map_alloc_equality(def);
417 isl_seq_cpy(def->eq[k], vec->el, vec->size);
418 isl_int_set_si(def->eq[k][1 + n - 1], -1);
424 vars_drop(v, v->n - n);
426 def = isl_basic_map_simplify(def);
427 def = isl_basic_map_finalize(def);
428 bmap = isl_basic_map_intersect(bmap, def);
431 isl_basic_map_free(bmap);
432 isl_basic_map_free(def);
436 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
437 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
440 struct isl_token *tok;
442 while ((tok = isl_stream_next_token(s)) != NULL) {
445 if (tok->type == ISL_TOKEN_IDENT) {
447 int p = vars_pos(v, tok->u.s, -1);
454 bmap = isl_basic_map_add(bmap, type, 1);
455 bmap = set_name(bmap, type, i, v->v->name);
457 } else if (tok->type == ISL_TOKEN_IDENT ||
458 tok->type == ISL_TOKEN_VALUE ||
461 if (type == isl_dim_param) {
462 isl_stream_error(s, tok,
463 "expecting unique identifier");
466 isl_stream_push_token(s, tok);
468 bmap = isl_basic_map_add(bmap, type, 1);
469 bmap = read_var_def(s, bmap, type, v);
473 tok = isl_stream_next_token(s);
474 if (!tok || tok->type != ',')
481 isl_stream_push_token(s, tok);
486 isl_basic_map_free(bmap);
490 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
495 struct isl_token *tok = NULL;
497 vec = accept_affine(s, v);
498 mat = isl_mat_from_row_vec(vec);
503 tok = isl_stream_next_token(s);
505 isl_stream_error(s, NULL, "unexpected EOF");
508 if (tok->type != ',') {
509 isl_stream_push_token(s, tok);
514 vec = accept_affine(s, v);
515 mat = isl_mat_add_zero_cols(mat, 1 + v->n - isl_mat_cols(mat));
516 mat = isl_mat_vec_concat(mat, vec);
527 static int read_div_definition(struct isl_stream *s, struct vars *v)
529 struct isl_token *tok;
532 struct variable *var;
534 if (isl_stream_eat(s, '['))
537 tok = isl_stream_next_token(s);
540 if (tok->type == '(') {
544 isl_stream_push_token(s, tok);
548 aff = accept_affine(s, v);
552 var->def = isl_vec_alloc(s->ctx, 2 + v->n);
558 isl_seq_cpy(var->def->el + 1, aff->el, aff->size);
562 if (seen_paren && isl_stream_eat(s, ')'))
564 if (isl_stream_eat(s, '/'))
567 tok = isl_stream_next_token(s);
570 if (tok->type != ISL_TOKEN_VALUE) {
571 isl_stream_error(s, tok, "expected denominator");
572 isl_stream_push_token(s, tok);
575 isl_int_set(var->def->el[0], tok->u.v);
578 if (isl_stream_eat(s, ']'))
584 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
585 struct vars *v, struct isl_basic_map *bmap, int k)
587 struct variable *var = v->v;
589 if (read_div_definition(s, v) < 0)
592 isl_seq_cpy(bmap->div[k], var->def->el, 2 + v->n);
594 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
599 isl_basic_map_free(bmap);
603 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
604 struct vars *v, struct isl_basic_map *bmap)
606 struct isl_token *tok;
608 while ((tok = isl_stream_next_token(s)) != NULL) {
612 unsigned total = isl_basic_map_total_dim(bmap);
614 if (tok->type != ISL_TOKEN_IDENT)
617 p = vars_pos(v, tok->u.s, -1);
621 isl_stream_error(s, tok, "expecting unique identifier");
625 bmap = isl_basic_map_cow(bmap);
626 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
629 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
631 isl_seq_clr(bmap->div[k], 1 + 1 + total);
634 tok = isl_stream_next_token(s);
635 if (tok && tok->type == '=') {
637 bmap = add_div_definition(s, v, bmap, k);
638 tok = isl_stream_next_token(s);
641 if (!tok || tok->type != ',')
647 isl_stream_push_token(s, tok);
652 isl_basic_map_free(bmap);
656 static int next_is_tuple(struct isl_stream *s)
658 struct isl_token *tok;
661 tok = isl_stream_next_token(s);
664 if (tok->type == '[') {
665 isl_stream_push_token(s, tok);
668 if (tok->type != ISL_TOKEN_IDENT) {
669 isl_stream_push_token(s, tok);
673 is_tuple = isl_stream_next_token_is(s, '[');
675 isl_stream_push_token(s, tok);
680 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
681 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v);
683 static __isl_give isl_basic_map *read_nested_tuple(struct isl_stream *s,
684 __isl_take isl_basic_map *bmap, struct vars *v)
686 bmap = read_tuple(s, bmap, isl_dim_in, v);
687 if (isl_stream_eat(s, ISL_TOKEN_TO))
689 bmap = read_tuple(s, bmap, isl_dim_out, v);
690 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
693 isl_basic_map_free(bmap);
697 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
698 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
700 struct isl_token *tok;
703 tok = isl_stream_next_token(s);
704 if (tok && tok->type == ISL_TOKEN_IDENT) {
705 name = strdup(tok->u.s);
709 tok = isl_stream_next_token(s);
711 if (!tok || tok->type != '[') {
712 isl_stream_error(s, tok, "expecting '['");
716 if (type != isl_dim_param && next_is_tuple(s)) {
717 isl_dim *dim = isl_basic_map_get_dim(bmap);
718 int nparam = isl_dim_size(dim, isl_dim_param);
719 int n_in = isl_dim_size(dim, isl_dim_in);
720 isl_basic_map *nested;
721 if (type == isl_dim_out)
722 dim = isl_dim_move(dim, isl_dim_param, nparam,
723 isl_dim_in, 0, n_in);
724 nested = isl_basic_map_alloc_dim(dim, 0, 0, 0);
725 nested = read_nested_tuple(s, nested, v);
726 if (type == isl_dim_in) {
727 nested = isl_basic_map_reverse(nested);
728 bmap = isl_basic_map_intersect(nested, bmap);
731 dim = isl_dim_range(isl_basic_map_get_dim(nested));
732 dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
733 dim = isl_dim_join(isl_basic_map_get_dim(bmap), dim);
734 bset = isl_basic_map_domain(bmap);
735 nested = isl_basic_map_reset_dim(nested, dim);
736 bmap = isl_basic_map_intersect_domain(nested, bset);
739 bmap = read_var_list(s, bmap, type, v);
740 tok = isl_stream_next_token(s);
741 if (!tok || tok->type != ']') {
742 isl_stream_error(s, tok, "expecting ']'");
748 bmap = isl_basic_map_set_tuple_name(bmap, type, name);
756 isl_basic_map_free(bmap);
760 static struct isl_basic_map *add_constraints(struct isl_stream *s,
761 struct vars *v, struct isl_basic_map *bmap);
763 static struct isl_basic_map *add_exists(struct isl_stream *s,
764 struct vars *v, struct isl_basic_map *bmap)
766 struct isl_token *tok;
773 tok = isl_stream_next_token(s);
776 if (tok->type == '(') {
780 isl_stream_push_token(s, tok);
782 bmap = read_defined_var_list(s, v, bmap);
786 if (isl_stream_eat(s, ':'))
788 bmap = add_constraints(s, v, bmap);
789 if (seen_paren && isl_stream_eat(s, ')'))
793 isl_basic_map_free(bmap);
797 static __isl_give isl_basic_map *construct_constraint(
798 __isl_take isl_basic_map *bmap, enum isl_token_type type,
799 isl_int *left, isl_int *right)
807 len = 1 + isl_basic_map_total_dim(bmap);
810 k = isl_basic_map_alloc_inequality(bmap);
813 if (type == ISL_TOKEN_LE)
814 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
817 else if (type == ISL_TOKEN_GE)
818 isl_seq_combine(bmap->ineq[k], ctx->one, left,
821 else if (type == ISL_TOKEN_LT) {
822 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
825 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
826 } else if (type == ISL_TOKEN_GT) {
827 isl_seq_combine(bmap->ineq[k], ctx->one, left,
830 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
832 isl_seq_combine(bmap->ineq[k], ctx->one, left,
835 isl_basic_map_inequality_to_equality(bmap, k);
840 isl_basic_map_free(bmap);
844 static int is_comparator(struct isl_token *tok)
861 static struct isl_basic_map *add_constraint(struct isl_stream *s,
862 struct vars *v, struct isl_basic_map *bmap)
865 struct isl_token *tok = NULL;
866 struct isl_mat *aff1 = NULL, *aff2 = NULL;
868 tok = isl_stream_next_token(s);
871 if (tok->type == ISL_TOKEN_EXISTS) {
873 return add_exists(s, v, bmap);
875 isl_stream_push_token(s, tok);
878 bmap = isl_basic_map_cow(bmap);
880 aff1 = accept_affine_list(s, v);
883 tok = isl_stream_next_token(s);
884 if (!is_comparator(tok)) {
885 isl_stream_error(s, tok, "missing operator");
887 isl_stream_push_token(s, tok);
892 aff2 = accept_affine_list(s, v);
896 aff1 = isl_mat_add_zero_cols(aff1, aff2->n_col - aff1->n_col);
899 bmap = add_divs(bmap, v);
900 bmap = isl_basic_map_extend_constraints(bmap, 0,
901 aff1->n_row * aff2->n_row);
902 for (i = 0; i < aff1->n_row; ++i)
903 for (j = 0; j < aff2->n_row; ++j)
904 bmap = construct_constraint(bmap, tok->type,
905 aff1->row[i], aff2->row[j]);
910 tok = isl_stream_next_token(s);
911 if (!is_comparator(tok)) {
913 isl_stream_push_token(s, tok);
925 isl_basic_map_free(bmap);
929 static struct isl_basic_map *add_constraints(struct isl_stream *s,
930 struct vars *v, struct isl_basic_map *bmap)
932 struct isl_token *tok;
935 bmap = add_constraint(s, v, bmap);
938 tok = isl_stream_next_token(s);
940 isl_stream_error(s, NULL, "unexpected EOF");
943 if (tok->type != ISL_TOKEN_AND)
947 isl_stream_push_token(s, tok);
953 isl_basic_map_free(bmap);
957 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
958 struct vars *v, __isl_take isl_dim *dim)
961 struct isl_token *tok;
962 struct isl_basic_map *bmap;
964 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
968 tok = isl_stream_next_token(s);
971 if (tok->type == '(') {
975 isl_stream_push_token(s, tok);
977 bmap = add_constraints(s, v, bmap);
978 bmap = isl_basic_map_simplify(bmap);
979 bmap = isl_basic_map_finalize(bmap);
981 if (seen_paren && isl_stream_eat(s, ')'))
986 isl_basic_map_free(bmap);
990 static struct isl_map *read_disjuncts(struct isl_stream *s,
991 struct vars *v, __isl_take isl_dim *dim)
993 struct isl_token *tok;
996 tok = isl_stream_next_token(s);
998 isl_stream_error(s, NULL, "unexpected EOF");
1001 if (tok->type == '}') {
1002 isl_stream_push_token(s, tok);
1003 return isl_map_universe(dim);
1005 isl_stream_push_token(s, tok);
1007 map = isl_map_empty(isl_dim_copy(dim));
1009 struct isl_basic_map *bmap;
1012 bmap = read_disjunct(s, v, isl_dim_copy(dim));
1013 map = isl_map_union(map, isl_map_from_basic_map(bmap));
1015 vars_drop(v, v->n - n);
1017 tok = isl_stream_next_token(s);
1018 if (!tok || tok->type != ISL_TOKEN_OR)
1020 isl_token_free(tok);
1023 isl_stream_push_token(s, tok);
1032 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1034 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1035 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1036 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1037 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1039 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1040 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1041 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1043 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1044 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1045 isl_basic_map_dim(bmap, isl_dim_in) +
1046 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1047 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1049 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1055 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1056 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1059 struct isl_token *tok;
1069 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1070 dim = isl_basic_map_dim(bmap, isl_dim_out);
1072 tok = isl_stream_next_token(s);
1073 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1074 isl_stream_error(s, tok, "expecting coefficient");
1076 isl_stream_push_token(s, tok);
1079 if (!tok->on_new_line) {
1080 isl_stream_error(s, tok, "coefficient should appear on new line");
1081 isl_stream_push_token(s, tok);
1085 type = isl_int_get_si(tok->u.v);
1086 isl_token_free(tok);
1088 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1090 k = isl_basic_map_alloc_equality(bmap);
1093 k = isl_basic_map_alloc_inequality(bmap);
1099 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1101 tok = isl_stream_next_token(s);
1102 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1103 isl_stream_error(s, tok, "expecting coefficient");
1105 isl_stream_push_token(s, tok);
1108 if (tok->on_new_line) {
1109 isl_stream_error(s, tok,
1110 "coefficient should not appear on new line");
1111 isl_stream_push_token(s, tok);
1114 pos = polylib_pos_to_isl_pos(bmap, j);
1115 isl_int_set(c[pos], tok->u.v);
1116 isl_token_free(tok);
1121 isl_basic_map_free(bmap);
1125 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1129 struct isl_token *tok;
1130 struct isl_token *tok2;
1133 unsigned in = 0, out, local = 0;
1134 struct isl_basic_map *bmap = NULL;
1139 tok = isl_stream_next_token(s);
1141 isl_stream_error(s, NULL, "unexpected EOF");
1144 tok2 = isl_stream_next_token(s);
1146 isl_token_free(tok);
1147 isl_stream_error(s, NULL, "unexpected EOF");
1150 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1151 isl_stream_push_token(s, tok2);
1152 isl_stream_push_token(s, tok);
1153 isl_stream_error(s, NULL,
1154 "expecting constraint matrix dimensions");
1157 n_row = isl_int_get_si(tok->u.v);
1158 n_col = isl_int_get_si(tok2->u.v);
1159 on_new_line = tok2->on_new_line;
1160 isl_token_free(tok2);
1161 isl_token_free(tok);
1162 isl_assert(s->ctx, !on_new_line, return NULL);
1163 isl_assert(s->ctx, n_row >= 0, return NULL);
1164 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1165 tok = isl_stream_next_token_on_same_line(s);
1167 if (tok->type != ISL_TOKEN_VALUE) {
1168 isl_stream_error(s, tok,
1169 "expecting number of output dimensions");
1170 isl_stream_push_token(s, tok);
1173 out = isl_int_get_si(tok->u.v);
1174 isl_token_free(tok);
1176 tok = isl_stream_next_token_on_same_line(s);
1177 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1178 isl_stream_error(s, tok,
1179 "expecting number of input dimensions");
1181 isl_stream_push_token(s, tok);
1184 in = isl_int_get_si(tok->u.v);
1185 isl_token_free(tok);
1187 tok = isl_stream_next_token_on_same_line(s);
1188 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1189 isl_stream_error(s, tok,
1190 "expecting number of existentials");
1192 isl_stream_push_token(s, tok);
1195 local = isl_int_get_si(tok->u.v);
1196 isl_token_free(tok);
1198 tok = isl_stream_next_token_on_same_line(s);
1199 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1200 isl_stream_error(s, tok,
1201 "expecting number of parameters");
1203 isl_stream_push_token(s, tok);
1206 nparam = isl_int_get_si(tok->u.v);
1207 isl_token_free(tok);
1208 if (n_col != 1 + out + in + local + nparam + 1) {
1209 isl_stream_error(s, NULL,
1210 "dimensions don't match");
1214 out = n_col - 2 - nparam;
1215 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1219 for (i = 0; i < local; ++i) {
1220 int k = isl_basic_map_alloc_div(bmap);
1223 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1226 for (i = 0; i < n_row; ++i)
1227 bmap = basic_map_read_polylib_constraint(s, bmap);
1229 tok = isl_stream_next_token_on_same_line(s);
1231 isl_stream_error(s, tok, "unexpected extra token on line");
1232 isl_stream_push_token(s, tok);
1236 bmap = isl_basic_map_simplify(bmap);
1237 bmap = isl_basic_map_finalize(bmap);
1240 isl_basic_map_free(bmap);
1244 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1246 struct isl_token *tok;
1247 struct isl_token *tok2;
1249 struct isl_map *map;
1251 tok = isl_stream_next_token(s);
1253 isl_stream_error(s, NULL, "unexpected EOF");
1256 tok2 = isl_stream_next_token_on_same_line(s);
1257 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1258 isl_stream_push_token(s, tok2);
1259 isl_stream_push_token(s, tok);
1260 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1263 isl_stream_error(s, tok2, "unexpected token");
1264 isl_stream_push_token(s, tok2);
1265 isl_stream_push_token(s, tok);
1268 n = isl_int_get_si(tok->u.v);
1269 isl_token_free(tok);
1271 isl_assert(s->ctx, n >= 1, return NULL);
1273 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1275 for (i = 1; map && i < n; ++i)
1276 map = isl_map_union(map,
1277 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1282 static int optional_power(struct isl_stream *s)
1285 struct isl_token *tok;
1287 tok = isl_stream_next_token(s);
1290 if (tok->type != '^') {
1291 isl_stream_push_token(s, tok);
1294 isl_token_free(tok);
1295 tok = isl_stream_next_token(s);
1296 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1297 isl_stream_error(s, tok, "expecting exponent");
1299 isl_stream_push_token(s, tok);
1302 pow = isl_int_get_si(tok->u.v);
1303 isl_token_free(tok);
1307 static __isl_give isl_div *read_div(struct isl_stream *s,
1308 __isl_take isl_dim *dim, struct vars *v)
1311 isl_basic_map *bmap;
1314 bmap = isl_basic_map_universe(dim);
1316 if (vars_add_anon(v) < 0)
1318 if (read_div_definition(s, v) < 0)
1320 bmap = add_divs(bmap, v);
1321 bmap = isl_basic_map_order_divs(bmap);
1324 vars_drop(v, v->n - n);
1326 return isl_basic_map_div(bmap, bmap->n_div - 1);
1328 isl_basic_map_free(bmap);
1332 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1333 __isl_keep isl_basic_map *bmap, struct vars *v);
1335 static __isl_give isl_qpolynomial *read_factor(struct isl_stream *s,
1336 __isl_keep isl_basic_map *bmap, struct vars *v)
1338 struct isl_qpolynomial *qp;
1339 struct isl_token *tok;
1341 tok = isl_stream_next_token(s);
1343 isl_stream_error(s, NULL, "unexpected EOF");
1346 if (tok->type == '(') {
1349 isl_token_free(tok);
1350 qp = read_term(s, bmap, v);
1353 if (isl_stream_eat(s, ')'))
1355 pow = optional_power(s);
1356 qp = isl_qpolynomial_pow(qp, pow);
1357 } else if (tok->type == ISL_TOKEN_VALUE) {
1358 struct isl_token *tok2;
1359 tok2 = isl_stream_next_token(s);
1360 if (tok2 && tok2->type == '/') {
1361 isl_token_free(tok2);
1362 tok2 = isl_stream_next_token(s);
1363 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1364 isl_stream_error(s, tok2, "expected denominator");
1365 isl_token_free(tok);
1366 isl_token_free(tok2);
1369 qp = isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap),
1370 tok->u.v, tok2->u.v);
1371 isl_token_free(tok2);
1373 isl_stream_push_token(s, tok2);
1374 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1377 isl_token_free(tok);
1378 } else if (tok->type == ISL_TOKEN_INFTY) {
1379 isl_token_free(tok);
1380 qp = isl_qpolynomial_infty(isl_basic_map_get_dim(bmap));
1381 } else if (tok->type == ISL_TOKEN_NAN) {
1382 isl_token_free(tok);
1383 qp = isl_qpolynomial_nan(isl_basic_map_get_dim(bmap));
1384 } else if (tok->type == ISL_TOKEN_IDENT) {
1386 int pos = vars_pos(v, tok->u.s, -1);
1389 isl_token_free(tok);
1393 vars_drop(v, v->n - n);
1394 isl_stream_error(s, tok, "unknown identifier");
1395 isl_token_free(tok);
1398 isl_token_free(tok);
1399 pow = optional_power(s);
1400 qp = isl_qpolynomial_var_pow(isl_basic_map_get_dim(bmap), pos, pow);
1401 } else if (tok->type == '[') {
1405 isl_stream_push_token(s, tok);
1406 div = read_div(s, isl_basic_map_get_dim(bmap), v);
1407 pow = optional_power(s);
1408 qp = isl_qpolynomial_div_pow(div, pow);
1409 } else if (tok->type == '-') {
1410 struct isl_qpolynomial *qp2;
1412 isl_token_free(tok);
1413 qp = isl_qpolynomial_cst(isl_basic_map_get_dim(bmap),
1415 qp2 = read_factor(s, bmap, v);
1416 qp = isl_qpolynomial_mul(qp, qp2);
1418 isl_stream_error(s, tok, "unexpected isl_token");
1419 isl_stream_push_token(s, tok);
1423 if (isl_stream_eat_if_available(s, '*') ||
1424 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1425 struct isl_qpolynomial *qp2;
1427 qp2 = read_factor(s, bmap, v);
1428 qp = isl_qpolynomial_mul(qp, qp2);
1433 isl_qpolynomial_free(qp);
1437 static __isl_give isl_qpolynomial *read_term(struct isl_stream *s,
1438 __isl_keep isl_basic_map *bmap, struct vars *v)
1440 struct isl_token *tok;
1441 struct isl_qpolynomial *qp;
1443 qp = read_factor(s, bmap, v);
1446 tok = isl_stream_next_token(s);
1450 if (tok->type == '+') {
1451 struct isl_qpolynomial *qp2;
1453 isl_token_free(tok);
1454 qp2 = read_factor(s, bmap, v);
1455 qp = isl_qpolynomial_add(qp, qp2);
1456 } else if (tok->type == '-') {
1457 struct isl_qpolynomial *qp2;
1459 isl_token_free(tok);
1460 qp2 = read_factor(s, bmap, v);
1461 qp = isl_qpolynomial_sub(qp, qp2);
1462 } else if (tok->type == ISL_TOKEN_VALUE &&
1463 isl_int_is_neg(tok->u.v)) {
1464 struct isl_qpolynomial *qp2;
1466 isl_stream_push_token(s, tok);
1467 qp2 = read_factor(s, bmap, v);
1468 qp = isl_qpolynomial_add(qp, qp2);
1470 isl_stream_push_token(s, tok);
1478 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1479 __isl_take isl_basic_map *bmap, struct vars *v)
1481 struct isl_token *tok;
1482 struct isl_map *map;
1484 tok = isl_stream_next_token(s);
1486 isl_stream_error(s, NULL, "unexpected EOF");
1489 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1490 if (tok->type == ':') {
1491 isl_token_free(tok);
1492 map = isl_map_intersect(map,
1493 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1495 isl_stream_push_token(s, tok);
1497 isl_basic_map_free(bmap);
1501 isl_basic_map_free(bmap);
1505 static struct isl_obj obj_read_poly(struct isl_stream *s,
1506 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1508 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1509 struct isl_pw_qpolynomial *pwqp;
1510 struct isl_qpolynomial *qp;
1511 struct isl_map *map;
1512 struct isl_set *set;
1514 qp = read_term(s, bmap, v);
1515 map = read_optional_disjuncts(s, bmap, v);
1516 set = isl_map_range(map);
1518 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1520 vars_drop(v, v->n - n);
1526 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1527 __isl_take isl_basic_map *bmap, struct vars *v, int n)
1529 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1530 struct isl_obj obj_p;
1531 isl_qpolynomial *qp;
1532 isl_qpolynomial_fold *fold = NULL;
1533 isl_pw_qpolynomial_fold *pwf;
1537 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1538 return obj_read_poly(s, bmap, v, n);
1540 if (isl_stream_eat(s, '('))
1543 qp = read_term(s, bmap, v);
1544 fold = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1546 while (isl_stream_eat_if_available(s, ',')) {
1547 isl_qpolynomial_fold *fold_i;
1548 qp = read_term(s, bmap, v);
1549 fold_i = isl_qpolynomial_fold_alloc(isl_fold_max, qp);
1550 fold = isl_qpolynomial_fold_fold(fold, fold_i);
1553 if (isl_stream_eat(s, ')'))
1556 map = read_optional_disjuncts(s, bmap, v);
1557 set = isl_map_range(map);
1558 pwf = isl_pw_qpolynomial_fold_alloc(isl_fold_max, set, fold);
1560 vars_drop(v, v->n - n);
1565 isl_basic_map_free(bmap);
1566 isl_qpolynomial_fold_free(fold);
1567 obj.type = isl_obj_none;
1571 static struct isl_obj obj_read_body(struct isl_stream *s,
1572 __isl_take isl_basic_map *bmap, struct vars *v)
1574 struct isl_map *map = NULL;
1575 struct isl_token *tok;
1576 struct isl_obj obj = { isl_obj_set, NULL };
1579 if (!next_is_tuple(s))
1580 return obj_read_poly_or_fold(s, bmap, v, n);
1582 bmap = read_tuple(s, bmap, isl_dim_in, v);
1585 tok = isl_stream_next_token(s);
1586 if (tok && tok->type == ISL_TOKEN_TO) {
1587 obj.type = isl_obj_map;
1588 isl_token_free(tok);
1589 if (!next_is_tuple(s)) {
1590 bmap = isl_basic_map_reverse(bmap);
1591 return obj_read_poly_or_fold(s, bmap, v, n);
1593 bmap = read_tuple(s, bmap, isl_dim_out, v);
1597 bmap = isl_basic_map_reverse(bmap);
1599 isl_stream_push_token(s, tok);
1602 map = read_optional_disjuncts(s, bmap, v);
1604 vars_drop(v, v->n - n);
1609 isl_basic_map_free(bmap);
1610 obj.type = isl_obj_none;
1614 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1616 if (obj.type == isl_obj_map) {
1617 obj.v = isl_union_map_from_map(obj.v);
1618 obj.type = isl_obj_union_map;
1619 } else if (obj.type == isl_obj_set) {
1620 obj.v = isl_union_set_from_set(obj.v);
1621 obj.type = isl_obj_union_set;
1622 } else if (obj.type == isl_obj_pw_qpolynomial) {
1623 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1624 obj.type = isl_obj_union_pw_qpolynomial;
1625 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1626 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1627 obj.type = isl_obj_union_pw_qpolynomial_fold;
1629 isl_assert(ctx, 0, goto error);
1632 obj.type->free(obj.v);
1633 obj.type = isl_obj_none;
1637 static struct isl_obj obj_add(struct isl_ctx *ctx,
1638 struct isl_obj obj1, struct isl_obj obj2)
1640 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1641 obj1 = to_union(ctx, obj1);
1642 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1643 obj2 = to_union(ctx, obj2);
1644 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1645 obj1 = to_union(ctx, obj1);
1646 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1647 obj2 = to_union(ctx, obj2);
1648 if (obj1.type == isl_obj_pw_qpolynomial &&
1649 obj2.type == isl_obj_union_pw_qpolynomial)
1650 obj1 = to_union(ctx, obj1);
1651 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1652 obj2.type == isl_obj_pw_qpolynomial)
1653 obj2 = to_union(ctx, obj2);
1654 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1655 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1656 obj1 = to_union(ctx, obj1);
1657 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1658 obj2.type == isl_obj_pw_qpolynomial_fold)
1659 obj2 = to_union(ctx, obj2);
1660 isl_assert(ctx, obj1.type == obj2.type, goto error);
1661 if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1662 obj1 = to_union(ctx, obj1);
1663 obj2 = to_union(ctx, obj2);
1665 if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1666 obj1 = to_union(ctx, obj1);
1667 obj2 = to_union(ctx, obj2);
1669 if (obj1.type == isl_obj_pw_qpolynomial &&
1670 !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1671 obj1 = to_union(ctx, obj1);
1672 obj2 = to_union(ctx, obj2);
1674 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1675 !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1676 obj1 = to_union(ctx, obj1);
1677 obj2 = to_union(ctx, obj2);
1679 obj1.v = obj1.type->add(obj1.v, obj2.v);
1682 obj1.type->free(obj1.v);
1683 obj2.type->free(obj2.v);
1684 obj1.type = isl_obj_none;
1689 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1691 isl_basic_map *bmap = NULL;
1692 struct isl_token *tok;
1693 struct vars *v = NULL;
1694 struct isl_obj obj = { isl_obj_set, NULL };
1696 tok = isl_stream_next_token(s);
1698 isl_stream_error(s, NULL, "unexpected EOF");
1701 if (tok->type == ISL_TOKEN_VALUE) {
1702 struct isl_map *map;
1703 isl_stream_push_token(s, tok);
1704 map = map_read_polylib(s, nparam);
1707 if (isl_map_dim(map, isl_dim_in) > 0)
1708 obj.type = isl_obj_map;
1712 v = vars_new(s->ctx);
1714 isl_stream_push_token(s, tok);
1717 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1718 if (tok->type == '[') {
1719 isl_stream_push_token(s, tok);
1720 bmap = read_tuple(s, bmap, isl_dim_param, v);
1724 isl_assert(s->ctx, nparam == v->n, goto error);
1725 tok = isl_stream_next_token(s);
1726 if (!tok || tok->type != ISL_TOKEN_TO) {
1727 isl_stream_error(s, tok, "expecting '->'");
1729 isl_stream_push_token(s, tok);
1732 isl_token_free(tok);
1733 tok = isl_stream_next_token(s);
1734 } else if (nparam > 0)
1735 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1736 if (!tok || tok->type != '{') {
1737 isl_stream_error(s, tok, "expecting '{'");
1739 isl_stream_push_token(s, tok);
1742 isl_token_free(tok);
1744 tok = isl_stream_next_token(s);
1747 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1748 isl_token_free(tok);
1749 if (isl_stream_eat(s, '='))
1751 bmap = read_tuple(s, bmap, isl_dim_param, v);
1755 isl_assert(s->ctx, nparam == v->n, goto error);
1756 } else if (tok->type == '}') {
1757 obj.type = isl_obj_union_set;
1758 obj.v = isl_union_set_empty(isl_basic_map_get_dim(bmap));
1759 isl_token_free(tok);
1762 isl_stream_push_token(s, tok);
1767 o = obj_read_body(s, isl_basic_map_copy(bmap), v);
1768 if (o.type == isl_obj_none || !o.v)
1773 obj = obj_add(s->ctx, obj, o);
1774 if (obj.type == isl_obj_none || !obj.v)
1777 tok = isl_stream_next_token(s);
1778 if (!tok || tok->type != ';')
1780 isl_token_free(tok);
1783 if (tok && tok->type == '}') {
1784 isl_token_free(tok);
1786 isl_stream_error(s, tok, "unexpected isl_token");
1788 isl_token_free(tok);
1793 isl_basic_map_free(bmap);
1797 isl_basic_map_free(bmap);
1798 obj.type->free(obj.v);
1805 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1807 return obj_read(s, -1);
1810 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1813 struct isl_map *map;
1815 obj = obj_read(s, nparam);
1817 isl_assert(s->ctx, obj.type == isl_obj_map ||
1818 obj.type == isl_obj_set, goto error);
1822 obj.type->free(obj.v);
1826 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
1829 struct isl_set *set;
1831 obj = obj_read(s, nparam);
1833 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
1837 obj.type->free(obj.v);
1841 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
1844 isl_union_map *umap;
1846 obj = obj_read(s, -1);
1847 if (obj.type == isl_obj_map) {
1848 obj.type = isl_obj_union_map;
1849 obj.v = isl_union_map_from_map(obj.v);
1851 if (obj.type == isl_obj_set) {
1852 obj.type = isl_obj_union_set;
1853 obj.v = isl_union_set_from_set(obj.v);
1856 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
1857 obj.type == isl_obj_union_set, goto error);
1861 obj.type->free(obj.v);
1865 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
1868 isl_union_set *uset;
1870 obj = obj_read(s, -1);
1871 if (obj.type == isl_obj_set) {
1872 obj.type = isl_obj_union_set;
1873 obj.v = isl_union_set_from_set(obj.v);
1876 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
1880 obj.type->free(obj.v);
1884 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1887 struct isl_map *map;
1888 struct isl_basic_map *bmap;
1890 obj = obj_read(s, nparam);
1895 isl_assert(map->ctx, map->n <= 1, goto error);
1898 bmap = isl_basic_map_empty_like_map(map);
1900 bmap = isl_basic_map_copy(map->p[0]);
1910 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1911 FILE *input, int nparam)
1913 struct isl_basic_map *bmap;
1914 struct isl_stream *s = isl_stream_new_file(ctx, input);
1917 bmap = basic_map_read(s, nparam);
1922 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1923 FILE *input, int nparam)
1925 struct isl_basic_map *bmap;
1926 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1929 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1930 return (struct isl_basic_set *)bmap;
1932 isl_basic_map_free(bmap);
1936 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1937 const char *str, int nparam)
1939 struct isl_basic_map *bmap;
1940 struct isl_stream *s = isl_stream_new_str(ctx, str);
1943 bmap = basic_map_read(s, nparam);
1948 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1949 const char *str, int nparam)
1951 struct isl_basic_map *bmap;
1952 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1955 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1956 return (struct isl_basic_set *)bmap;
1958 isl_basic_map_free(bmap);
1962 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1963 FILE *input, int nparam)
1965 struct isl_map *map;
1966 struct isl_stream *s = isl_stream_new_file(ctx, input);
1969 map = isl_stream_read_map(s, nparam);
1974 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1975 const char *str, int nparam)
1977 struct isl_map *map;
1978 struct isl_stream *s = isl_stream_new_str(ctx, str);
1981 map = isl_stream_read_map(s, nparam);
1986 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1987 FILE *input, int nparam)
1989 struct isl_map *map;
1990 map = isl_map_read_from_file(ctx, input, nparam);
1993 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1994 return (struct isl_set *)map;
2000 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2001 const char *str, int nparam)
2003 struct isl_map *map;
2004 map = isl_map_read_from_str(ctx, str, nparam);
2007 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2008 return (struct isl_set *)map;
2014 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2017 isl_union_map *umap;
2018 struct isl_stream *s = isl_stream_new_str(ctx, str);
2021 umap = isl_stream_read_union_map(s);
2026 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2029 isl_union_set *uset;
2030 struct isl_stream *s = isl_stream_new_str(ctx, str);
2033 uset = isl_stream_read_union_set(s);
2038 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2040 struct isl_vec *vec = NULL;
2041 struct isl_token *tok;
2045 tok = isl_stream_next_token(s);
2046 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2047 isl_stream_error(s, tok, "expecting vector length");
2051 size = isl_int_get_si(tok->u.v);
2052 isl_token_free(tok);
2054 vec = isl_vec_alloc(s->ctx, size);
2056 for (j = 0; j < size; ++j) {
2057 tok = isl_stream_next_token(s);
2058 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2059 isl_stream_error(s, tok, "expecting constant value");
2062 isl_int_set(vec->el[j], tok->u.v);
2063 isl_token_free(tok);
2068 isl_token_free(tok);
2073 static __isl_give isl_vec *vec_read(struct isl_stream *s,
2074 unsigned input_format)
2076 if (input_format == ISL_FORMAT_POLYLIB)
2077 return isl_vec_read_polylib(s);
2078 isl_assert(s->ctx, 0, return NULL);
2081 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
2082 FILE *input, unsigned input_format)
2085 struct isl_stream *s = isl_stream_new_file(ctx, input);
2088 v = vec_read(s, input_format);
2093 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2094 struct isl_stream *s)
2097 struct isl_pw_qpolynomial *pwqp;
2099 obj = obj_read(s, -1);
2101 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2106 obj.type->free(obj.v);
2110 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2113 isl_pw_qpolynomial *pwqp;
2114 struct isl_stream *s = isl_stream_new_str(ctx, str);
2117 pwqp = isl_stream_read_pw_qpolynomial(s);