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
19 #include "isl_stream.h"
20 #include "isl_map_private.h"
22 #include "isl_polynomial_private.h"
27 struct variable *next;
36 static struct vars *vars_new(struct isl_ctx *ctx)
39 v = isl_alloc_type(ctx, struct vars);
48 static void variable_free(struct variable *var)
51 struct variable *next = var->next;
58 static void vars_free(struct vars *v)
66 static void vars_drop(struct vars *v, int n)
77 struct variable *next = var->next;
85 static struct variable *variable_new(struct vars *v, const char *name, int len,
89 var = isl_alloc_type(v->ctx, struct variable);
92 var->name = strdup(name);
93 var->name[len] = '\0';
102 static int vars_pos(struct vars *v, const char *s, int len)
109 for (q = v->v; q; q = q->next) {
110 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
117 v->v = variable_new(v, s, len, v->n);
125 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
126 enum isl_dim_type type, unsigned pos, char *name)
135 prime = strchr(name, '\'');
138 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, name);
147 isl_basic_map_free(bmap);
151 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
153 struct isl_token *tok = NULL;
157 aff = isl_vec_alloc(v->ctx, 1 + v->n);
158 isl_seq_clr(aff->el, aff->size);
163 tok = isl_stream_next_token(s);
165 isl_stream_error(s, NULL, "unexpected EOF");
168 if (tok->type == ISL_TOKEN_IDENT) {
170 int pos = vars_pos(v, tok->u.s, -1);
174 isl_stream_error(s, tok, "unknown identifier");
179 isl_int_add_ui(aff->el[1 + pos],
180 aff->el[1 + pos], 1);
182 isl_int_sub_ui(aff->el[1 + pos],
183 aff->el[1 + pos], 1);
185 } else if (tok->type == ISL_TOKEN_VALUE) {
186 struct isl_token *tok2;
189 tok2 = isl_stream_next_token(s);
190 if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
191 pos = vars_pos(v, tok2->u.s, -1);
195 isl_stream_error(s, tok2,
196 "unknown identifier");
198 isl_token_free(tok2);
201 isl_token_free(tok2);
203 isl_stream_push_token(s, tok2);
205 isl_int_neg(tok->u.v, tok->u.v);
206 isl_int_add(aff->el[1 + pos],
207 aff->el[1 + pos], tok->u.v);
209 } else if (tok->type == '-') {
211 } else if (tok->type == '+') {
214 isl_stream_push_token(s, tok);
226 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
227 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
230 struct isl_token *tok;
232 while ((tok = isl_stream_next_token(s)) != NULL) {
235 if (tok->type == ISL_TOKEN_IDENT) {
237 int p = vars_pos(v, tok->u.s, -1);
244 bmap = isl_basic_map_add(bmap, type, 1);
245 bmap = set_name(bmap, type, i, v->v->name);
247 } else if (tok->type == ISL_TOKEN_IDENT ||
248 tok->type == ISL_TOKEN_VALUE) {
251 if (type == isl_dim_param) {
252 isl_stream_error(s, tok,
253 "expecting unique identifier");
256 isl_stream_push_token(s, tok);
258 vec = accept_affine(s, v);
261 v->v = variable_new(v, "", 0, v->n);
265 bmap = isl_basic_map_add(bmap, type, 1);
266 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
267 k = isl_basic_map_alloc_equality(bmap);
269 isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
270 isl_int_set_si(bmap->eq[k][vec->size], -1);
278 tok = isl_stream_next_token(s);
279 if (!tok || tok->type != ',')
286 isl_stream_push_token(s, tok);
291 isl_basic_map_free(bmap);
295 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
300 struct isl_token *tok = NULL;
302 vec = accept_affine(s, v);
303 mat = isl_mat_from_row_vec(vec);
308 tok = isl_stream_next_token(s);
310 isl_stream_error(s, NULL, "unexpected EOF");
313 if (tok->type != ',') {
314 isl_stream_push_token(s, tok);
319 vec = accept_affine(s, v);
320 mat = isl_mat_vec_concat(mat, vec);
331 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
332 struct vars *v, struct isl_basic_map *bmap, int k)
334 struct isl_token *tok;
338 if (isl_stream_eat(s, '['))
341 tok = isl_stream_next_token(s);
344 if (tok->type == '(') {
348 isl_stream_push_token(s, tok);
350 aff = accept_affine(s, v);
354 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
358 if (seen_paren && isl_stream_eat(s, ')'))
360 if (isl_stream_eat(s, '/'))
363 tok = isl_stream_next_token(s);
366 if (tok->type != ISL_TOKEN_VALUE) {
367 isl_stream_error(s, tok, "expected denominator");
368 isl_stream_push_token(s, tok);
371 isl_int_set(bmap->div[k][0], tok->u.v);
374 if (isl_stream_eat(s, ']'))
377 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
382 isl_basic_map_free(bmap);
386 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
387 struct vars *v, struct isl_basic_map *bmap)
389 struct isl_token *tok;
391 while ((tok = isl_stream_next_token(s)) != NULL) {
395 unsigned total = isl_basic_map_total_dim(bmap);
397 if (tok->type != ISL_TOKEN_IDENT)
400 p = vars_pos(v, tok->u.s, -1);
404 isl_stream_error(s, tok, "expecting unique identifier");
409 bmap = isl_basic_map_cow(bmap);
410 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
413 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
415 isl_seq_clr(bmap->div[k], 1 + 1 + total);
417 tok = isl_stream_next_token(s);
418 if (tok && tok->type == '=') {
420 bmap = add_div_definition(s, v, bmap, k);
421 tok = isl_stream_next_token(s);
424 if (!tok || tok->type != ',')
430 isl_stream_push_token(s, tok);
435 isl_basic_map_free(bmap);
439 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
440 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
442 struct isl_token *tok;
444 tok = isl_stream_next_token(s);
445 if (!tok || tok->type != '[') {
446 isl_stream_error(s, tok, "expecting '['");
450 bmap = read_var_list(s, bmap, type, v);
451 tok = isl_stream_next_token(s);
452 if (!tok || tok->type != ']') {
453 isl_stream_error(s, tok, "expecting ']'");
462 isl_basic_map_free(bmap);
466 static struct isl_basic_map *add_constraints(struct isl_stream *s,
467 struct vars *v, struct isl_basic_map *bmap);
469 static struct isl_basic_map *add_exists(struct isl_stream *s,
470 struct vars *v, struct isl_basic_map *bmap)
472 struct isl_token *tok;
479 tok = isl_stream_next_token(s);
482 if (tok->type == '(') {
486 isl_stream_push_token(s, tok);
488 bmap = read_defined_var_list(s, v, bmap);
492 if (isl_stream_eat(s, ':'))
494 bmap = add_constraints(s, v, bmap);
495 if (seen_paren && isl_stream_eat(s, ')'))
499 isl_basic_map_free(bmap);
503 static __isl_give isl_basic_map *construct_constraint(
504 __isl_take isl_basic_map *bmap, enum isl_token_type type,
505 isl_int *left, isl_int *right)
513 len = 1 + isl_basic_map_total_dim(bmap);
516 k = isl_basic_map_alloc_inequality(bmap);
519 if (type == ISL_TOKEN_LE)
520 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
523 else if (type == ISL_TOKEN_GE)
524 isl_seq_combine(bmap->ineq[k], ctx->one, left,
527 else if (type == ISL_TOKEN_LT) {
528 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
531 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
532 } else if (type == ISL_TOKEN_GT) {
533 isl_seq_combine(bmap->ineq[k], ctx->one, left,
536 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
538 isl_seq_combine(bmap->ineq[k], ctx->one, left,
541 isl_basic_map_inequality_to_equality(bmap, k);
546 isl_basic_map_free(bmap);
550 static int is_comparator(struct isl_token *tok)
567 static struct isl_basic_map *add_constraint(struct isl_stream *s,
568 struct vars *v, struct isl_basic_map *bmap)
571 unsigned total = isl_basic_map_total_dim(bmap);
572 struct isl_token *tok = NULL;
573 struct isl_mat *aff1 = NULL, *aff2 = NULL;
575 tok = isl_stream_next_token(s);
578 if (tok->type == ISL_TOKEN_EXISTS) {
580 return add_exists(s, v, bmap);
582 isl_stream_push_token(s, tok);
585 bmap = isl_basic_map_cow(bmap);
587 aff1 = accept_affine_list(s, v);
590 tok = isl_stream_next_token(s);
591 if (!is_comparator(tok)) {
592 isl_stream_error(s, tok, "missing operator");
594 isl_stream_push_token(s, tok);
598 isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
600 aff2 = accept_affine_list(s, v);
603 isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
605 bmap = isl_basic_map_extend_constraints(bmap, 0,
606 aff1->n_row * aff2->n_row);
607 for (i = 0; i < aff1->n_row; ++i)
608 for (j = 0; j < aff2->n_row; ++j)
609 bmap = construct_constraint(bmap, tok->type,
610 aff1->row[i], aff2->row[j]);
615 tok = isl_stream_next_token(s);
616 if (!is_comparator(tok)) {
618 isl_stream_push_token(s, tok);
630 isl_basic_map_free(bmap);
634 static struct isl_basic_map *add_constraints(struct isl_stream *s,
635 struct vars *v, struct isl_basic_map *bmap)
637 struct isl_token *tok;
640 bmap = add_constraint(s, v, bmap);
643 tok = isl_stream_next_token(s);
645 isl_stream_error(s, NULL, "unexpected EOF");
648 if (tok->type != ISL_TOKEN_AND)
652 isl_stream_push_token(s, tok);
658 isl_basic_map_free(bmap);
662 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
663 struct vars *v, __isl_take isl_dim *dim)
666 struct isl_token *tok;
667 struct isl_basic_map *bmap;
669 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
673 tok = isl_stream_next_token(s);
676 if (tok->type == '(') {
680 isl_stream_push_token(s, tok);
682 bmap = add_constraints(s, v, bmap);
683 bmap = isl_basic_map_simplify(bmap);
684 bmap = isl_basic_map_finalize(bmap);
686 if (seen_paren && isl_stream_eat(s, ')'))
691 isl_basic_map_free(bmap);
695 static struct isl_map *read_disjuncts(struct isl_stream *s,
696 struct vars *v, __isl_take isl_dim *dim)
698 struct isl_token *tok;
701 tok = isl_stream_next_token(s);
703 isl_stream_error(s, NULL, "unexpected EOF");
706 if (tok->type == '}') {
707 isl_stream_push_token(s, tok);
708 return isl_map_universe(dim);
710 isl_stream_push_token(s, tok);
712 map = isl_map_empty(isl_dim_copy(dim));
714 struct isl_basic_map *bmap;
717 bmap = read_disjunct(s, v, isl_dim_copy(dim));
718 map = isl_map_union(map, isl_map_from_basic_map(bmap));
720 vars_drop(v, v->n - n);
722 tok = isl_stream_next_token(s);
723 if (!tok || tok->type != ISL_TOKEN_OR)
728 isl_stream_push_token(s, tok);
737 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
739 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
740 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
741 isl_basic_map_dim(bmap, isl_dim_in) + pos;
742 pos -= isl_basic_map_dim(bmap, isl_dim_out);
744 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
745 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
746 pos -= isl_basic_map_dim(bmap, isl_dim_in);
748 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
749 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
750 isl_basic_map_dim(bmap, isl_dim_in) +
751 isl_basic_map_dim(bmap, isl_dim_out) + pos;
752 pos -= isl_basic_map_dim(bmap, isl_dim_div);
754 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
760 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
761 struct isl_stream *s, __isl_take isl_basic_map *bmap)
764 struct isl_token *tok;
774 nparam = isl_basic_map_dim(bmap, isl_dim_param);
775 dim = isl_basic_map_dim(bmap, isl_dim_out);
777 tok = isl_stream_next_token(s);
778 if (!tok || tok->type != ISL_TOKEN_VALUE) {
779 isl_stream_error(s, tok, "expecting coefficient");
781 isl_stream_push_token(s, tok);
784 if (!tok->on_new_line) {
785 isl_stream_error(s, tok, "coefficient should appear on new line");
786 isl_stream_push_token(s, tok);
790 type = isl_int_get_si(tok->u.v);
793 isl_assert(s->ctx, type == 0 || type == 1, goto error);
795 k = isl_basic_map_alloc_equality(bmap);
798 k = isl_basic_map_alloc_inequality(bmap);
804 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
806 tok = isl_stream_next_token(s);
807 if (!tok || tok->type != ISL_TOKEN_VALUE) {
808 isl_stream_error(s, tok, "expecting coefficient");
810 isl_stream_push_token(s, tok);
813 if (tok->on_new_line) {
814 isl_stream_error(s, tok,
815 "coefficient should not appear on new line");
816 isl_stream_push_token(s, tok);
819 pos = polylib_pos_to_isl_pos(bmap, j);
820 isl_int_set(c[pos], tok->u.v);
826 isl_basic_map_free(bmap);
830 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
834 struct isl_token *tok;
835 struct isl_token *tok2;
838 unsigned in = 0, out, local = 0;
839 struct isl_basic_map *bmap = NULL;
844 tok = isl_stream_next_token(s);
846 isl_stream_error(s, NULL, "unexpected EOF");
849 tok2 = isl_stream_next_token(s);
852 isl_stream_error(s, NULL, "unexpected EOF");
855 n_row = isl_int_get_si(tok->u.v);
856 n_col = isl_int_get_si(tok2->u.v);
857 on_new_line = tok2->on_new_line;
858 isl_token_free(tok2);
860 isl_assert(s->ctx, !on_new_line, return NULL);
861 isl_assert(s->ctx, n_row >= 0, return NULL);
862 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
863 tok = isl_stream_next_token_on_same_line(s);
865 if (tok->type != ISL_TOKEN_VALUE) {
866 isl_stream_error(s, tok,
867 "expecting number of output dimensions");
868 isl_stream_push_token(s, tok);
871 out = isl_int_get_si(tok->u.v);
874 tok = isl_stream_next_token_on_same_line(s);
875 if (!tok || tok->type != ISL_TOKEN_VALUE) {
876 isl_stream_error(s, tok,
877 "expecting number of input dimensions");
879 isl_stream_push_token(s, tok);
882 in = isl_int_get_si(tok->u.v);
885 tok = isl_stream_next_token_on_same_line(s);
886 if (!tok || tok->type != ISL_TOKEN_VALUE) {
887 isl_stream_error(s, tok,
888 "expecting number of existentials");
890 isl_stream_push_token(s, tok);
893 local = isl_int_get_si(tok->u.v);
896 tok = isl_stream_next_token_on_same_line(s);
897 if (!tok || tok->type != ISL_TOKEN_VALUE) {
898 isl_stream_error(s, tok,
899 "expecting number of parameters");
901 isl_stream_push_token(s, tok);
904 nparam = isl_int_get_si(tok->u.v);
906 if (n_col != 1 + out + in + local + nparam + 1) {
907 isl_stream_error(s, NULL,
908 "dimensions don't match");
912 out = n_col - 2 - nparam;
913 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
917 for (i = 0; i < local; ++i) {
918 int k = isl_basic_map_alloc_div(bmap);
923 for (i = 0; i < n_row; ++i)
924 bmap = basic_map_read_polylib_constraint(s, bmap);
926 tok = isl_stream_next_token_on_same_line(s);
928 isl_stream_error(s, tok, "unexpected extra token on line");
929 isl_stream_push_token(s, tok);
933 bmap = isl_basic_map_simplify(bmap);
934 bmap = isl_basic_map_finalize(bmap);
937 isl_basic_map_free(bmap);
941 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
943 struct isl_token *tok;
944 struct isl_token *tok2;
948 tok = isl_stream_next_token(s);
950 isl_stream_error(s, NULL, "unexpected EOF");
953 tok2 = isl_stream_next_token_on_same_line(s);
955 isl_stream_push_token(s, tok2);
956 isl_stream_push_token(s, tok);
957 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
959 n = isl_int_get_si(tok->u.v);
962 isl_assert(s->ctx, n >= 1, return NULL);
964 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
966 for (i = 1; i < n; ++i)
967 map = isl_map_union(map,
968 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
973 static struct isl_obj obj_read_body(struct isl_stream *s,
974 __isl_take isl_basic_map *bmap, struct vars *v, isl_obj_type type)
976 struct isl_map *map = NULL;
977 struct isl_token *tok;
978 struct isl_obj obj = { type, NULL };
981 bmap = read_tuple(s, bmap, isl_dim_in, v);
984 tok = isl_stream_next_token(s);
985 if (tok && tok->type == ISL_TOKEN_TO) {
986 obj.type = isl_obj_map;
988 bmap = read_tuple(s, bmap, isl_dim_out, v);
992 bmap = isl_basic_map_reverse(bmap);
994 isl_stream_push_token(s, tok);
996 tok = isl_stream_next_token(s);
998 isl_stream_error(s, NULL, "unexpected EOF");
1001 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
1002 if (tok->type == ':') {
1003 isl_token_free(tok);
1004 map = isl_map_intersect(map,
1005 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1007 isl_stream_push_token(s, tok);
1009 vars_drop(v, v->n - n);
1011 isl_basic_map_free(bmap);
1015 isl_basic_map_free(bmap);
1016 obj.type = isl_obj_none;
1020 static struct isl_obj obj_add(struct isl_ctx *ctx,
1021 struct isl_obj obj1, struct isl_obj obj2)
1023 isl_assert(ctx, obj1.type == obj2.type, goto error);
1024 obj1.v = obj1.type->add(obj1.v, obj2.v);
1027 obj1.type->free(obj1.v);
1028 obj2.type->free(obj2.v);
1029 obj1.type = isl_obj_none;
1034 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1036 struct isl_basic_map *bmap = NULL;
1037 struct isl_token *tok;
1038 struct vars *v = NULL;
1039 struct isl_obj obj = { isl_obj_set, NULL };
1041 tok = isl_stream_next_token(s);
1043 isl_stream_error(s, NULL, "unexpected EOF");
1046 if (tok->type == ISL_TOKEN_VALUE) {
1047 struct isl_map *map;
1048 isl_stream_push_token(s, tok);
1049 map = map_read_polylib(s, nparam);
1052 if (isl_map_dim(map, isl_dim_in) > 0)
1053 obj.type = isl_obj_map;
1057 v = vars_new(s->ctx);
1058 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1059 if (tok->type == '[') {
1060 isl_stream_push_token(s, tok);
1061 bmap = read_tuple(s, bmap, isl_dim_param, v);
1065 isl_assert(s->ctx, nparam == v->n, goto error);
1066 tok = isl_stream_next_token(s);
1067 if (!tok || tok->type != ISL_TOKEN_TO) {
1068 isl_stream_error(s, tok, "expecting '->'");
1070 isl_stream_push_token(s, tok);
1073 isl_token_free(tok);
1074 tok = isl_stream_next_token(s);
1076 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1077 if (!tok || tok->type != '{') {
1078 isl_stream_error(s, tok, "expecting '{'");
1080 isl_stream_push_token(s, tok);
1083 isl_token_free(tok);
1088 o = obj_read_body(s, isl_basic_map_copy(bmap), v, obj.type);
1089 if (o.type == isl_obj_none)
1094 obj = obj_add(s->ctx, obj, o);
1095 if (obj.type == isl_obj_none)
1098 tok = isl_stream_next_token(s);
1099 if (!tok || tok->type != ';')
1101 isl_token_free(tok);
1104 if (tok && tok->type == '}') {
1105 isl_token_free(tok);
1107 isl_stream_error(s, tok, "unexpected isl_token");
1109 isl_token_free(tok);
1113 isl_basic_map_free(bmap);
1117 isl_basic_map_free(bmap);
1118 obj.type->free(obj.v);
1125 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1127 return obj_read(s, -1);
1130 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1133 struct isl_map *map;
1134 struct isl_basic_map *bmap;
1136 obj = obj_read(s, nparam);
1141 isl_assert(map->ctx, map->n <= 1, goto error);
1144 bmap = isl_basic_map_empty_like_map(map);
1146 bmap = isl_basic_map_copy(map->p[0]);
1156 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1157 FILE *input, int nparam)
1159 struct isl_basic_map *bmap;
1160 struct isl_stream *s = isl_stream_new_file(ctx, input);
1163 bmap = basic_map_read(s, nparam);
1168 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1169 FILE *input, int nparam)
1171 struct isl_basic_map *bmap;
1172 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1175 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1176 return (struct isl_basic_set *)bmap;
1178 isl_basic_map_free(bmap);
1182 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1183 const char *str, int nparam)
1185 struct isl_basic_map *bmap;
1186 struct isl_stream *s = isl_stream_new_str(ctx, str);
1189 bmap = basic_map_read(s, nparam);
1194 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1195 const char *str, int nparam)
1197 struct isl_basic_map *bmap;
1198 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1201 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1202 return (struct isl_basic_set *)bmap;
1204 isl_basic_map_free(bmap);
1208 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1209 FILE *input, int nparam)
1212 struct isl_map *map;
1213 struct isl_stream *s = isl_stream_new_file(ctx, input);
1216 obj = obj_read(s, nparam);
1221 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1222 const char *str, int nparam)
1225 struct isl_map *map;
1226 struct isl_stream *s = isl_stream_new_str(ctx, str);
1229 obj = obj_read(s, nparam);
1234 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1235 FILE *input, int nparam)
1237 struct isl_map *map;
1238 map = isl_map_read_from_file(ctx, input, nparam);
1241 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1242 return (struct isl_set *)map;
1248 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1249 const char *str, int nparam)
1251 struct isl_map *map;
1252 map = isl_map_read_from_str(ctx, str, nparam);
1255 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1256 return (struct isl_set *)map;
1262 static char *next_line(FILE *input, char *line, unsigned len)
1267 if (!(p = fgets(line, len, input)))
1269 while (isspace(*p) && *p != '\n')
1271 } while (*p == '#' || *p == '\n');
1276 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1279 struct isl_vec *vec = NULL;
1288 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1289 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1291 vec = isl_vec_alloc(ctx, size);
1293 p = next_line(input, line, sizeof(line));
1294 isl_assert(ctx, p, goto error);
1296 for (j = 0; j < size; ++j) {
1297 n = sscanf(p, "%s%n", val, &offset);
1298 isl_assert(ctx, n != 0, goto error);
1299 isl_int_read(vec->el[j], val);
1309 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1310 FILE *input, unsigned input_format)
1312 if (input_format == ISL_FORMAT_POLYLIB)
1313 return isl_vec_read_from_file_polylib(ctx, input);
1315 isl_assert(ctx, 0, return NULL);