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 void vars_drop(struct vars *v, int n)
75 struct variable *next = var->next;
83 static struct variable *variable_new(struct vars *v, const char *name, int len,
87 var = isl_alloc_type(v->ctx, struct variable);
90 var->name = strdup(name);
91 var->name[len] = '\0';
100 static int vars_pos(struct vars *v, const char *s, int len)
107 for (q = v->v; q; q = q->next) {
108 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
115 v->v = variable_new(v, s, len, v->n);
123 static struct vars *read_var_list(struct isl_stream *s, struct vars *v)
125 struct isl_token *tok;
127 while ((tok = isl_stream_next_token(s)) != NULL) {
131 if (tok->type != ISL_TOKEN_IDENT)
134 p = vars_pos(v, tok->u.s, -1);
138 isl_stream_error(s, tok, "expecting unique identifier");
142 tok = isl_stream_next_token(s);
143 if (!tok || tok->type != ',')
149 isl_stream_push_token(s, tok);
158 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
160 struct isl_token *tok = NULL;
164 aff = isl_vec_alloc(v->ctx, 1 + v->n);
165 isl_seq_clr(aff->el, aff->size);
170 tok = isl_stream_next_token(s);
172 isl_stream_error(s, NULL, "unexpected EOF");
175 if (tok->type == ISL_TOKEN_IDENT) {
177 int pos = vars_pos(v, tok->u.s, -1);
181 isl_stream_error(s, tok, "unknown identifier");
185 isl_int_add_ui(aff->el[1 + pos],
186 aff->el[1 + pos], 1);
188 isl_int_sub_ui(aff->el[1 + pos],
189 aff->el[1 + pos], 1);
191 } else if (tok->type == ISL_TOKEN_VALUE) {
192 struct isl_token *tok2;
195 tok2 = isl_stream_next_token(s);
196 if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
197 pos = vars_pos(v, tok2->u.s, -1);
201 isl_stream_error(s, tok2,
202 "unknown identifier");
203 isl_token_free(tok2);
206 isl_token_free(tok2);
208 isl_stream_push_token(s, tok2);
210 isl_int_neg(tok->u.v, tok->u.v);
211 isl_int_add(aff->el[1 + pos],
212 aff->el[1 + pos], tok->u.v);
214 } else if (tok->type == '-') {
216 } else if (tok->type == '+') {
219 isl_stream_push_token(s, tok);
231 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
236 struct isl_token *tok = NULL;
238 vec = accept_affine(s, v);
239 mat = isl_mat_from_row_vec(vec);
244 tok = isl_stream_next_token(s);
246 isl_stream_error(s, NULL, "unexpected EOF");
249 if (tok->type != ',') {
250 isl_stream_push_token(s, tok);
255 vec = accept_affine(s, v);
256 mat = isl_mat_vec_concat(mat, vec);
267 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
268 struct vars *v, struct isl_basic_map *bmap, int k)
270 struct isl_token *tok;
274 if (isl_stream_eat(s, '['))
277 tok = isl_stream_next_token(s);
280 if (tok->type == '(') {
284 isl_stream_push_token(s, tok);
286 aff = accept_affine(s, v);
290 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
294 if (seen_paren && isl_stream_eat(s, ')'))
296 if (isl_stream_eat(s, '/'))
299 tok = isl_stream_next_token(s);
302 if (tok->type != ISL_TOKEN_VALUE) {
303 isl_stream_error(s, tok, "expected denominator");
304 isl_stream_push_token(s, tok);
307 isl_int_set(bmap->div[k][0], tok->u.v);
310 if (isl_stream_eat(s, ']'))
313 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
318 isl_basic_map_free(bmap);
322 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
323 struct vars *v, struct isl_basic_map *bmap)
325 struct isl_token *tok;
327 while ((tok = isl_stream_next_token(s)) != NULL) {
331 unsigned total = isl_basic_map_total_dim(bmap);
333 if (tok->type != ISL_TOKEN_IDENT)
336 p = vars_pos(v, tok->u.s, -1);
340 isl_stream_error(s, tok, "expecting unique identifier");
345 bmap = isl_basic_map_cow(bmap);
346 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
349 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
351 isl_seq_clr(bmap->div[k], 1 + 1 + total);
353 tok = isl_stream_next_token(s);
354 if (tok && tok->type == '=') {
356 bmap = add_div_definition(s, v, bmap, k);
357 tok = isl_stream_next_token(s);
360 if (!tok || tok->type != ',')
366 isl_stream_push_token(s, tok);
371 isl_basic_map_free(bmap);
375 static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
377 struct isl_token *tok;
379 tok = isl_stream_next_token(s);
380 if (!tok || tok->type != '[') {
381 isl_stream_error(s, tok, "expecting '['");
385 v = read_var_list(s, v);
386 tok = isl_stream_next_token(s);
387 if (!tok || tok->type != ']') {
388 isl_stream_error(s, tok, "expecting ']'");
401 static struct isl_basic_map *add_constraints(struct isl_stream *s,
402 struct vars *v, struct isl_basic_map *bmap);
404 static struct isl_basic_map *add_exists(struct isl_stream *s,
405 struct vars *v, struct isl_basic_map *bmap)
407 struct isl_token *tok;
414 tok = isl_stream_next_token(s);
417 if (tok->type == '(') {
421 isl_stream_push_token(s, tok);
423 bmap = read_defined_var_list(s, v, bmap);
427 if (isl_stream_eat(s, ':'))
429 bmap = add_constraints(s, v, bmap);
430 if (seen_paren && isl_stream_eat(s, ')'))
434 isl_basic_map_free(bmap);
438 static __isl_give isl_basic_map *construct_constraint(
439 __isl_take isl_basic_map *bmap, enum isl_token_type type,
440 isl_int *left, isl_int *right)
448 len = 1 + isl_basic_map_total_dim(bmap);
451 k = isl_basic_map_alloc_inequality(bmap);
454 if (type == ISL_TOKEN_LE)
455 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
458 else if (type == ISL_TOKEN_GE)
459 isl_seq_combine(bmap->ineq[k], ctx->one, left,
462 else if (type == ISL_TOKEN_LT) {
463 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
466 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
467 } else if (type == ISL_TOKEN_GT) {
468 isl_seq_combine(bmap->ineq[k], ctx->one, left,
471 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
473 isl_seq_combine(bmap->ineq[k], ctx->one, left,
476 isl_basic_map_inequality_to_equality(bmap, k);
481 isl_basic_map_free(bmap);
485 static struct isl_basic_map *add_constraint(struct isl_stream *s,
486 struct vars *v, struct isl_basic_map *bmap)
489 unsigned total = isl_basic_map_total_dim(bmap);
490 struct isl_token *tok = NULL;
491 struct isl_mat *aff1 = NULL, *aff2 = NULL;
493 tok = isl_stream_next_token(s);
496 if (tok->type == ISL_TOKEN_EXISTS) {
498 return add_exists(s, v, bmap);
500 isl_stream_push_token(s, tok);
503 bmap = isl_basic_map_cow(bmap);
505 aff1 = accept_affine_list(s, v);
508 tok = isl_stream_next_token(s);
517 isl_stream_error(s, tok, "missing operator");
518 isl_stream_push_token(s, tok);
522 aff2 = accept_affine_list(s, v);
525 isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
526 isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
528 bmap = isl_basic_map_extend_constraints(bmap, 0, aff1->n_row * aff2->n_row);
529 for (i = 0; i < aff1->n_row; ++i)
530 for (j = 0; j < aff2->n_row; ++j)
531 bmap = construct_constraint(bmap, tok->type,
532 aff1->row[i], aff2->row[j]);
543 isl_basic_map_free(bmap);
547 static struct isl_basic_map *add_constraints(struct isl_stream *s,
548 struct vars *v, struct isl_basic_map *bmap)
550 struct isl_token *tok;
553 bmap = add_constraint(s, v, bmap);
556 tok = isl_stream_next_token(s);
558 isl_stream_error(s, NULL, "unexpected EOF");
561 if (tok->type != ISL_TOKEN_AND)
565 isl_stream_push_token(s, tok);
571 isl_basic_map_free(bmap);
575 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
576 struct vars *v, __isl_take isl_dim *dim)
578 struct isl_basic_map *bmap;
580 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
584 bmap = add_constraints(s, v, bmap);
585 bmap = isl_basic_map_simplify(bmap);
586 bmap = isl_basic_map_finalize(bmap);
590 static struct isl_map *read_disjuncts(struct isl_stream *s,
591 struct vars *v, __isl_take isl_dim *dim)
593 struct isl_token *tok;
596 tok = isl_stream_next_token(s);
598 isl_stream_error(s, NULL, "unexpected EOF");
601 if (tok->type == '}') {
602 isl_stream_push_token(s, tok);
603 return isl_map_universe(dim);
605 isl_stream_push_token(s, tok);
607 map = isl_map_empty(isl_dim_copy(dim));
609 struct isl_basic_map *bmap;
612 bmap = read_disjunct(s, v, isl_dim_copy(dim));
613 map = isl_map_union(map, isl_map_from_basic_map(bmap));
615 vars_drop(v, v->n - n);
617 tok = isl_stream_next_token(s);
618 if (!tok || tok->type != ISL_TOKEN_OR)
623 isl_stream_push_token(s, tok);
632 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
634 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
635 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
636 isl_basic_map_dim(bmap, isl_dim_in) + pos;
637 pos -= isl_basic_map_dim(bmap, isl_dim_out);
639 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
640 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
641 pos -= isl_basic_map_dim(bmap, isl_dim_in);
643 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
644 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
645 isl_basic_map_dim(bmap, isl_dim_in) +
646 isl_basic_map_dim(bmap, isl_dim_out) + pos;
647 pos -= isl_basic_map_dim(bmap, isl_dim_div);
649 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
655 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
656 struct isl_stream *s, __isl_take isl_basic_map *bmap)
659 struct isl_token *tok;
669 nparam = isl_basic_map_dim(bmap, isl_dim_param);
670 dim = isl_basic_map_dim(bmap, isl_dim_out);
672 tok = isl_stream_next_token(s);
673 if (!tok || tok->type != ISL_TOKEN_VALUE) {
674 isl_stream_error(s, tok, "expecting coefficient");
676 isl_stream_push_token(s, tok);
679 if (!tok->on_new_line) {
680 isl_stream_error(s, tok, "coefficient should appear on new line");
681 isl_stream_push_token(s, tok);
685 type = isl_int_get_si(tok->u.v);
688 isl_assert(s->ctx, type == 0 || type == 1, goto error);
690 k = isl_basic_map_alloc_equality(bmap);
693 k = isl_basic_map_alloc_inequality(bmap);
699 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
701 tok = isl_stream_next_token(s);
702 if (!tok || tok->type != ISL_TOKEN_VALUE) {
703 isl_stream_error(s, tok, "expecting coefficient");
705 isl_stream_push_token(s, tok);
708 if (tok->on_new_line) {
709 isl_stream_error(s, tok,
710 "coefficient should not appear on new line");
711 isl_stream_push_token(s, tok);
714 pos = polylib_pos_to_isl_pos(bmap, j);
715 isl_int_set(c[pos], tok->u.v);
721 isl_basic_map_free(bmap);
725 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
729 struct isl_token *tok;
730 struct isl_token *tok2;
733 unsigned in = 0, out, local = 0;
734 struct isl_basic_map *bmap = NULL;
739 tok = isl_stream_next_token(s);
741 isl_stream_error(s, NULL, "unexpected EOF");
744 tok2 = isl_stream_next_token(s);
747 isl_stream_error(s, NULL, "unexpected EOF");
750 n_row = isl_int_get_si(tok->u.v);
751 n_col = isl_int_get_si(tok2->u.v);
752 on_new_line = tok2->on_new_line;
753 isl_token_free(tok2);
755 isl_assert(s->ctx, !on_new_line, return NULL);
756 isl_assert(s->ctx, n_row >= 0, return NULL);
757 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
758 tok = isl_stream_next_token_on_same_line(s);
760 if (tok->type != ISL_TOKEN_VALUE) {
761 isl_stream_error(s, tok,
762 "expecting number of output dimensions");
763 isl_stream_push_token(s, tok);
766 out = isl_int_get_si(tok->u.v);
769 tok = isl_stream_next_token_on_same_line(s);
770 if (!tok || tok->type != ISL_TOKEN_VALUE) {
771 isl_stream_error(s, tok,
772 "expecting number of input dimensions");
774 isl_stream_push_token(s, tok);
777 in = isl_int_get_si(tok->u.v);
780 tok = isl_stream_next_token_on_same_line(s);
781 if (!tok || tok->type != ISL_TOKEN_VALUE) {
782 isl_stream_error(s, tok,
783 "expecting number of existentials");
785 isl_stream_push_token(s, tok);
788 local = isl_int_get_si(tok->u.v);
791 tok = isl_stream_next_token_on_same_line(s);
792 if (!tok || tok->type != ISL_TOKEN_VALUE) {
793 isl_stream_error(s, tok,
794 "expecting number of parameters");
796 isl_stream_push_token(s, tok);
799 nparam = isl_int_get_si(tok->u.v);
801 if (n_col != 1 + out + in + local + nparam + 1) {
802 isl_stream_error(s, NULL,
803 "dimensions don't match");
807 out = n_col - 2 - nparam;
808 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
812 for (i = 0; i < local; ++i) {
813 int k = isl_basic_map_alloc_div(bmap);
818 for (i = 0; i < n_row; ++i)
819 bmap = basic_map_read_polylib_constraint(s, bmap);
821 bmap = isl_basic_map_simplify(bmap);
822 bmap = isl_basic_map_finalize(bmap);
825 isl_basic_map_free(bmap);
829 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
831 struct isl_token *tok;
832 struct isl_token *tok2;
836 tok = isl_stream_next_token(s);
838 isl_stream_error(s, NULL, "unexpected EOF");
841 tok2 = isl_stream_next_token(s);
844 isl_stream_error(s, NULL, "unexpected EOF");
847 if (!tok2->on_new_line) {
848 isl_stream_push_token(s, tok2);
849 isl_stream_push_token(s, tok);
850 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
852 isl_stream_push_token(s, tok2);
853 n = isl_int_get_si(tok->u.v);
856 isl_assert(s->ctx, n >= 1, return NULL);
858 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
860 for (i = 1; i < n; ++i)
861 map = isl_map_union(map,
862 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
867 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
868 enum isl_dim_type type, int offset, int n)
873 for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
875 for (i = n - 1; i >= 0; --i, v = v->next)
876 dim = isl_dim_set_name(dim, type, i, v->name);
881 static struct isl_dim *dim_from_vars(struct vars *vars,
882 int nparam, int n_in, int n_out)
886 dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
887 dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
888 dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
889 dim = set_names(dim, vars, isl_dim_out, 0, n_out);
894 static struct isl_map *map_read(struct isl_stream *s, int nparam)
896 struct isl_dim *dim = NULL;
897 struct isl_map *map = NULL;
898 struct isl_token *tok;
899 struct vars *v = NULL;
903 tok = isl_stream_next_token(s);
905 isl_stream_error(s, NULL, "unexpected EOF");
908 if (tok->type == ISL_TOKEN_VALUE) {
909 isl_stream_push_token(s, tok);
910 return map_read_polylib(s, nparam);
912 v = vars_new(s->ctx);
913 if (tok->type == '[') {
914 isl_stream_push_token(s, tok);
915 v = read_tuple(s, v);
919 isl_assert(s->ctx, nparam == v->n, goto error);
921 tok = isl_stream_next_token(s);
922 if (!tok || tok->type != ISL_TOKEN_TO) {
923 isl_stream_error(s, tok, "expecting '->'");
925 isl_stream_push_token(s, tok);
929 tok = isl_stream_next_token(s);
933 if (!tok || tok->type != '{') {
934 isl_stream_error(s, tok, "expecting '{'");
936 isl_stream_push_token(s, tok);
940 v = read_tuple(s, v);
944 tok = isl_stream_next_token(s);
945 if (tok && tok->type == ISL_TOKEN_TO) {
947 v = read_tuple(s, v);
950 n2 = v->n - n1 - nparam;
953 isl_stream_push_token(s, tok);
957 dim = dim_from_vars(v, nparam, n1, n2);
958 tok = isl_stream_next_token(s);
960 isl_stream_error(s, NULL, "unexpected EOF");
963 if (tok->type == ':') {
965 map = read_disjuncts(s, v, isl_dim_copy(dim));
966 tok = isl_stream_next_token(s);
968 map = isl_map_universe(isl_dim_copy(dim));
969 if (tok && tok->type == '}') {
972 isl_stream_error(s, tok, "unexpected isl_token");
989 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
992 struct isl_basic_map *bmap;
994 map = map_read(s, nparam);
998 isl_assert(map->ctx, map->n <= 1, goto error);
1001 bmap = isl_basic_map_empty_like_map(map);
1003 bmap = isl_basic_map_copy(map->p[0]);
1013 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1014 FILE *input, int nparam)
1016 struct isl_basic_map *bmap;
1017 struct isl_stream *s = isl_stream_new_file(ctx, input);
1020 bmap = basic_map_read(s, nparam);
1025 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1026 FILE *input, int nparam)
1028 struct isl_basic_map *bmap;
1029 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1032 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1033 return (struct isl_basic_set *)bmap;
1035 isl_basic_map_free(bmap);
1039 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1040 const char *str, int nparam)
1042 struct isl_basic_map *bmap;
1043 struct isl_stream *s = isl_stream_new_str(ctx, str);
1046 bmap = basic_map_read(s, nparam);
1051 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1052 const char *str, int nparam)
1054 struct isl_basic_map *bmap;
1055 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1058 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1059 return (struct isl_basic_set *)bmap;
1061 isl_basic_map_free(bmap);
1065 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1066 FILE *input, int nparam)
1068 struct isl_map *map;
1069 struct isl_stream *s = isl_stream_new_file(ctx, input);
1072 map = map_read(s, nparam);
1077 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1078 const char *str, int nparam)
1080 struct isl_map *map;
1081 struct isl_stream *s = isl_stream_new_str(ctx, str);
1084 map = map_read(s, nparam);
1089 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1090 FILE *input, int nparam)
1092 struct isl_map *map;
1093 map = isl_map_read_from_file(ctx, input, nparam);
1096 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1097 return (struct isl_set *)map;
1103 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1104 const char *str, int nparam)
1106 struct isl_map *map;
1107 map = isl_map_read_from_str(ctx, str, nparam);
1110 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1111 return (struct isl_set *)map;
1117 static char *next_line(FILE *input, char *line, unsigned len)
1122 if (!(p = fgets(line, len, input)))
1124 while (isspace(*p) && *p != '\n')
1126 } while (*p == '#' || *p == '\n');
1131 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1134 struct isl_vec *vec = NULL;
1143 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1144 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1146 vec = isl_vec_alloc(ctx, size);
1148 p = next_line(input, line, sizeof(line));
1149 isl_assert(ctx, p, goto error);
1151 for (j = 0; j < size; ++j) {
1152 n = sscanf(p, "%s%n", val, &offset);
1153 isl_assert(ctx, n != 0, goto error);
1154 isl_int_read(vec->el[j], val);
1164 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1165 FILE *input, unsigned input_format)
1167 if (input_format == ISL_FORMAT_POLYLIB)
1168 return isl_vec_read_from_file_polylib(ctx, input);
1170 isl_assert(ctx, 0, return NULL);