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"
25 struct variable *next;
34 static struct vars *vars_new(struct isl_ctx *ctx)
37 v = isl_alloc_type(ctx, struct vars);
46 static void variable_free(struct variable *var)
49 struct variable *next = var->next;
56 static void vars_free(struct vars *v)
64 static struct variable *variable_new(struct vars *v, const char *name, int len,
68 var = isl_alloc_type(v->ctx, struct variable);
71 var->name = strdup(name);
72 var->name[len] = '\0';
81 static int vars_pos(struct vars *v, const char *s, int len)
88 for (q = v->v; q; q = q->next) {
89 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
96 v->v = variable_new(v, s, len, v->n);
104 static struct vars *read_var_list(struct isl_stream *s, struct vars *v)
106 struct isl_token *tok;
108 while ((tok = isl_stream_next_token(s)) != NULL) {
112 if (tok->type != ISL_TOKEN_IDENT)
115 p = vars_pos(v, tok->u.s, -1);
119 isl_stream_error(s, tok, "expecting unique identifier");
123 tok = isl_stream_next_token(s);
124 if (!tok || tok->type != ',')
130 isl_stream_push_token(s, tok);
139 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
141 struct isl_token *tok = NULL;
145 aff = isl_vec_alloc(v->ctx, 1 + v->n);
146 isl_seq_clr(aff->el, aff->size);
151 tok = isl_stream_next_token(s);
153 isl_stream_error(s, NULL, "unexpected EOF");
156 if (tok->type == ISL_TOKEN_IDENT) {
158 int pos = vars_pos(v, tok->u.s, -1);
162 isl_stream_error(s, tok, "unknown identifier");
166 isl_int_add_ui(aff->el[1 + pos],
167 aff->el[1 + pos], 1);
169 isl_int_sub_ui(aff->el[1 + pos],
170 aff->el[1 + pos], 1);
172 } else if (tok->type == ISL_TOKEN_VALUE) {
173 struct isl_token *tok2;
176 tok2 = isl_stream_next_token(s);
177 if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
178 pos = vars_pos(v, tok2->u.s, -1);
182 isl_stream_error(s, tok2,
183 "unknown identifier");
184 isl_token_free(tok2);
187 isl_token_free(tok2);
189 isl_stream_push_token(s, tok2);
191 isl_int_neg(tok->u.v, tok->u.v);
192 isl_int_add(aff->el[1 + pos],
193 aff->el[1 + pos], tok->u.v);
195 } else if (tok->type == '-') {
197 } else if (tok->type == '+') {
200 isl_stream_push_token(s, tok);
212 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
213 struct vars *v, struct isl_basic_map *bmap, int k)
215 struct isl_token *tok;
219 if (isl_stream_eat(s, '['))
222 tok = isl_stream_next_token(s);
225 if (tok->type == '(') {
229 isl_stream_push_token(s, tok);
231 aff = accept_affine(s, v);
235 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
239 if (seen_paren && isl_stream_eat(s, ')'))
241 if (isl_stream_eat(s, '/'))
244 tok = isl_stream_next_token(s);
247 if (tok->type != ISL_TOKEN_VALUE) {
248 isl_stream_error(s, tok, "expected denominator");
249 isl_stream_push_token(s, tok);
252 isl_int_set(bmap->div[k][0], tok->u.v);
255 if (isl_stream_eat(s, ']'))
258 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
263 isl_basic_map_free(bmap);
267 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
268 struct vars *v, struct isl_basic_map *bmap)
270 struct isl_token *tok;
272 while ((tok = isl_stream_next_token(s)) != NULL) {
276 unsigned total = isl_basic_map_total_dim(bmap);
278 if (tok->type != ISL_TOKEN_IDENT)
281 p = vars_pos(v, tok->u.s, -1);
285 isl_stream_error(s, tok, "expecting unique identifier");
290 bmap = isl_basic_map_cow(bmap);
291 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
294 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
296 isl_seq_clr(bmap->div[k], 1 + 1 + total);
298 tok = isl_stream_next_token(s);
299 if (tok && tok->type == '=') {
301 bmap = add_div_definition(s, v, bmap, k);
302 tok = isl_stream_next_token(s);
305 if (!tok || tok->type != ',')
311 isl_stream_push_token(s, tok);
316 isl_basic_map_free(bmap);
320 static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
322 struct isl_token *tok;
324 tok = isl_stream_next_token(s);
325 if (!tok || tok->type != '[') {
326 isl_stream_error(s, tok, "expecting '['");
330 v = read_var_list(s, v);
331 tok = isl_stream_next_token(s);
332 if (!tok || tok->type != ']') {
333 isl_stream_error(s, tok, "expecting ']'");
346 static struct isl_basic_map *add_constraints(struct isl_stream *s,
347 struct vars **v, struct isl_basic_map *bmap);
349 static struct isl_basic_map *add_exists(struct isl_stream *s,
350 struct vars **v, struct isl_basic_map *bmap)
352 struct isl_token *tok;
359 tok = isl_stream_next_token(s);
362 if (tok->type == '(') {
366 isl_stream_push_token(s, tok);
368 bmap = read_defined_var_list(s, *v, bmap);
372 if (isl_stream_eat(s, ':'))
374 bmap = add_constraints(s, v, bmap);
375 if (seen_paren && isl_stream_eat(s, ')'))
379 isl_basic_map_free(bmap);
383 static struct isl_basic_map *add_constraint(struct isl_stream *s,
384 struct vars **v, struct isl_basic_map *bmap)
386 unsigned total = isl_basic_map_total_dim(bmap);
388 struct isl_token *tok = NULL;
389 struct isl_vec *aff1 = NULL, *aff2 = NULL;
391 tok = isl_stream_next_token(s);
394 if (tok->type == ISL_TOKEN_EXISTS) {
396 return add_exists(s, v, bmap);
398 isl_stream_push_token(s, tok);
401 bmap = isl_basic_map_cow(bmap);
402 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
403 k = isl_basic_map_alloc_inequality(bmap);
407 aff1 = accept_affine(s, *v);
410 tok = isl_stream_next_token(s);
419 isl_stream_error(s, tok, "missing operator");
420 isl_stream_push_token(s, tok);
424 aff2 = accept_affine(s, *v);
427 isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
428 isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
430 if (tok->type == ISL_TOKEN_LE)
431 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
432 (*v)->ctx->one, aff2->el,
434 else if (tok->type == ISL_TOKEN_GE)
435 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
436 (*v)->ctx->negone, aff2->el,
438 else if (tok->type == ISL_TOKEN_LT) {
439 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
440 (*v)->ctx->one, aff2->el,
442 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
443 } else if (tok->type == ISL_TOKEN_GT) {
444 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
445 (*v)->ctx->negone, aff2->el,
447 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
449 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
450 (*v)->ctx->negone, aff2->el,
452 isl_basic_map_inequality_to_equality(bmap, k);
464 isl_basic_map_free(bmap);
468 static struct isl_basic_map *add_constraints(struct isl_stream *s,
469 struct vars **v, struct isl_basic_map *bmap)
471 struct isl_token *tok;
474 bmap = add_constraint(s, v, bmap);
477 tok = isl_stream_next_token(s);
479 isl_stream_error(s, NULL, "unexpected EOF");
482 if (tok->type != ISL_TOKEN_AND)
486 isl_stream_push_token(s, tok);
492 isl_basic_map_free(bmap);
496 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
497 struct vars **v, __isl_take isl_dim *dim)
499 struct isl_basic_map *bmap;
501 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
505 bmap = add_constraints(s, v, bmap);
506 bmap = isl_basic_map_simplify(bmap);
507 bmap = isl_basic_map_finalize(bmap);
511 static struct isl_map *read_disjuncts(struct isl_stream *s,
512 struct vars **v, __isl_take isl_dim *dim)
514 struct isl_token *tok;
517 tok = isl_stream_next_token(s);
519 isl_stream_error(s, NULL, "unexpected EOF");
522 if (tok->type == '}') {
523 isl_stream_push_token(s, tok);
524 return isl_map_universe(dim);
526 isl_stream_push_token(s, tok);
528 map = isl_map_empty(isl_dim_copy(dim));
530 struct isl_basic_map *bmap;
532 bmap = read_disjunct(s, v, isl_dim_copy(dim));
533 map = isl_map_union(map, isl_map_from_basic_map(bmap));
535 tok = isl_stream_next_token(s);
536 if (!tok || tok->type != ISL_TOKEN_OR)
541 isl_stream_push_token(s, tok);
550 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
551 struct isl_stream *s, __isl_take isl_basic_map *bmap)
554 struct isl_token *tok;
564 nparam = isl_basic_map_dim(bmap, isl_dim_param);
565 dim = isl_basic_map_dim(bmap, isl_dim_out);
567 tok = isl_stream_next_token(s);
568 if (!tok || tok->type != ISL_TOKEN_VALUE) {
569 isl_stream_error(s, tok, "expecting coefficient");
571 isl_stream_push_token(s, tok);
574 if (!tok->on_new_line) {
575 isl_stream_error(s, tok, "coefficient should appear on new line");
576 isl_stream_push_token(s, tok);
580 type = isl_int_get_si(tok->u.v);
583 isl_assert(s->ctx, type == 0 || type == 1, goto error);
585 k = isl_basic_map_alloc_equality(bmap);
588 k = isl_basic_map_alloc_inequality(bmap);
594 for (j = 0; j < dim; ++j) {
595 tok = isl_stream_next_token(s);
596 if (!tok || tok->type != ISL_TOKEN_VALUE) {
597 isl_stream_error(s, tok, "expecting coefficient");
599 isl_stream_push_token(s, tok);
602 isl_int_set(c[1 + nparam + j], tok->u.v);
605 for (j = 0; j < nparam; ++j) {
606 tok = isl_stream_next_token(s);
607 if (!tok || tok->type != ISL_TOKEN_VALUE) {
608 isl_stream_error(s, tok, "expecting coefficient");
610 isl_stream_push_token(s, tok);
613 isl_int_set(c[1 + j], tok->u.v);
616 tok = isl_stream_next_token(s);
617 if (!tok || tok->type != ISL_TOKEN_VALUE) {
618 isl_stream_error(s, tok, "expecting coefficient");
620 isl_stream_push_token(s, tok);
623 isl_int_set(c[0], tok->u.v);
628 isl_basic_map_free(bmap);
632 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
636 struct isl_token *tok;
637 struct isl_token *tok2;
641 struct isl_basic_map *bmap = NULL;
646 tok = isl_stream_next_token(s);
648 isl_stream_error(s, NULL, "unexpected EOF");
651 tok2 = isl_stream_next_token(s);
654 isl_stream_error(s, NULL, "unexpected EOF");
657 n_row = isl_int_get_si(tok->u.v);
658 n_col = isl_int_get_si(tok2->u.v);
659 on_new_line = tok2->on_new_line;
660 isl_token_free(tok2);
662 isl_assert(s->ctx, !on_new_line, return NULL);
663 isl_assert(s->ctx, n_row >= 0, return NULL);
664 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
665 dim = n_col - 2 - nparam;
666 bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
670 for (i = 0; i < n_row; ++i)
671 bmap = basic_map_read_polylib_constraint(s, bmap);
673 bmap = isl_basic_map_simplify(bmap);
674 bmap = isl_basic_map_finalize(bmap);
678 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
680 struct isl_token *tok;
681 struct isl_token *tok2;
685 tok = isl_stream_next_token(s);
687 isl_stream_error(s, NULL, "unexpected EOF");
690 tok2 = isl_stream_next_token(s);
693 isl_stream_error(s, NULL, "unexpected EOF");
696 if (!tok2->on_new_line) {
697 isl_stream_push_token(s, tok2);
698 isl_stream_push_token(s, tok);
699 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
701 isl_stream_push_token(s, tok2);
702 n = isl_int_get_si(tok->u.v);
705 isl_assert(s->ctx, n >= 1, return NULL);
707 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
709 for (i = 1; i < n; ++i)
710 map = isl_map_union(map,
711 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
716 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
717 enum isl_dim_type type, int offset, int n)
722 for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
724 for (i = n - 1; i >= 0; --i, v = v->next)
725 dim = isl_dim_set_name(dim, type, i, v->name);
730 static struct isl_dim *dim_from_vars(struct vars *vars,
731 int nparam, int n_in, int n_out)
735 dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
736 dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
737 dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
738 dim = set_names(dim, vars, isl_dim_out, 0, n_out);
743 static struct isl_map *map_read(struct isl_stream *s, int nparam)
745 struct isl_dim *dim = NULL;
746 struct isl_map *map = NULL;
747 struct isl_token *tok;
748 struct vars *v = NULL;
752 tok = isl_stream_next_token(s);
754 isl_stream_error(s, NULL, "unexpected EOF");
757 if (tok->type == ISL_TOKEN_VALUE) {
758 isl_stream_push_token(s, tok);
759 return map_read_polylib(s, nparam);
761 v = vars_new(s->ctx);
762 if (tok->type == '[') {
763 isl_stream_push_token(s, tok);
764 v = read_tuple(s, v);
768 isl_assert(s->ctx, nparam == v->n, goto error);
770 tok = isl_stream_next_token(s);
771 if (!tok || tok->type != ISL_TOKEN_TO) {
772 isl_stream_error(s, tok, "expecting '->'");
774 isl_stream_push_token(s, tok);
778 tok = isl_stream_next_token(s);
782 if (!tok || tok->type != '{') {
783 isl_stream_error(s, tok, "expecting '{'");
785 isl_stream_push_token(s, tok);
789 v = read_tuple(s, v);
793 tok = isl_stream_next_token(s);
794 if (tok && tok->type == ISL_TOKEN_TO) {
796 v = read_tuple(s, v);
799 n2 = v->n - n1 - nparam;
802 isl_stream_push_token(s, tok);
806 dim = dim_from_vars(v, nparam, n1, n2);
807 tok = isl_stream_next_token(s);
809 isl_stream_error(s, NULL, "unexpected EOF");
812 if (tok->type == ':') {
814 map = read_disjuncts(s, &v, isl_dim_copy(dim));
815 tok = isl_stream_next_token(s);
817 map = isl_map_universe(isl_dim_copy(dim));
818 if (tok && tok->type == '}') {
821 isl_stream_error(s, tok, "unexpected isl_token");
838 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
841 struct isl_basic_map *bmap;
843 map = map_read(s, nparam);
847 isl_assert(map->ctx, map->n <= 1, goto error);
850 bmap = isl_basic_map_empty_like_map(map);
852 bmap = isl_basic_map_copy(map->p[0]);
862 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
863 FILE *input, int nparam)
865 struct isl_basic_map *bmap;
866 struct isl_stream *s = isl_stream_new_file(ctx, input);
869 bmap = basic_map_read(s, nparam);
874 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
875 FILE *input, int nparam)
877 struct isl_basic_map *bmap;
878 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
881 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
882 return (struct isl_basic_set *)bmap;
884 isl_basic_map_free(bmap);
888 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
889 const char *str, int nparam)
891 struct isl_basic_map *bmap;
892 struct isl_stream *s = isl_stream_new_str(ctx, str);
895 bmap = basic_map_read(s, nparam);
900 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
901 const char *str, int nparam)
903 struct isl_basic_map *bmap;
904 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
907 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
908 return (struct isl_basic_set *)bmap;
910 isl_basic_map_free(bmap);
914 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
915 FILE *input, int nparam)
918 struct isl_stream *s = isl_stream_new_file(ctx, input);
921 map = map_read(s, nparam);
926 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
927 const char *str, int nparam)
930 struct isl_stream *s = isl_stream_new_str(ctx, str);
933 map = map_read(s, nparam);
938 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
939 FILE *input, int nparam)
942 map = isl_map_read_from_file(ctx, input, nparam);
945 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
946 return (struct isl_set *)map;
952 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
953 const char *str, int nparam)
956 map = isl_map_read_from_str(ctx, str, nparam);
959 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
960 return (struct isl_set *)map;
966 static char *next_line(FILE *input, char *line, unsigned len)
971 if (!(p = fgets(line, len, input)))
973 while (isspace(*p) && *p != '\n')
975 } while (*p == '#' || *p == '\n');
980 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
983 struct isl_vec *vec = NULL;
992 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
993 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
995 vec = isl_vec_alloc(ctx, size);
997 p = next_line(input, line, sizeof(line));
998 isl_assert(ctx, p, goto error);
1000 for (j = 0; j < size; ++j) {
1001 n = sscanf(p, "%s%n", val, &offset);
1002 isl_assert(ctx, n != 0, goto error);
1003 isl_int_read(vec->el[j], val);
1013 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1014 FILE *input, unsigned input_format)
1016 if (input_format == ISL_FORMAT_POLYLIB)
1017 return isl_vec_read_from_file_polylib(ctx, input);
1019 isl_assert(ctx, 0, return NULL);