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;
144 aff = isl_vec_alloc(v->ctx, 1 + v->n);
145 isl_seq_clr(aff->el, aff->size);
150 tok = isl_stream_next_token(s);
152 isl_stream_error(s, NULL, "unexpected EOF");
155 if (tok->type == ISL_TOKEN_IDENT) {
157 int pos = vars_pos(v, tok->u.s, -1);
161 isl_stream_error(s, tok, "unknown identifier");
164 isl_int_add_ui(aff->el[1 + pos], aff->el[1 + pos], 1);
165 } else if (tok->type == ISL_TOKEN_VALUE) {
166 struct isl_token *tok2;
169 tok2 = isl_stream_next_token(s);
170 if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
171 pos = vars_pos(v, tok2->u.s, -1);
175 isl_stream_error(s, tok2,
176 "unknown identifier");
177 isl_token_free(tok2);
180 isl_token_free(tok2);
182 isl_stream_push_token(s, tok2);
183 isl_int_add(aff->el[1 + pos],
184 aff->el[1 + pos], tok->u.v);
185 } else if (tok->type == '+') {
188 isl_stream_push_token(s, tok);
200 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
201 struct vars *v, struct isl_basic_map *bmap, int k)
203 struct isl_token *tok;
207 if (isl_stream_eat(s, '['))
210 tok = isl_stream_next_token(s);
213 if (tok->type == '(') {
217 isl_stream_push_token(s, tok);
219 aff = accept_affine(s, v);
223 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
227 if (seen_paren && isl_stream_eat(s, ')'))
229 if (isl_stream_eat(s, '/'))
232 tok = isl_stream_next_token(s);
235 if (tok->type != ISL_TOKEN_VALUE) {
236 isl_stream_error(s, tok, "expected denominator");
237 isl_stream_push_token(s, tok);
240 isl_int_set(bmap->div[k][0], tok->u.v);
243 if (isl_stream_eat(s, ']'))
246 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
251 isl_basic_map_free(bmap);
255 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
256 struct vars *v, struct isl_basic_map *bmap)
258 struct isl_token *tok;
260 while ((tok = isl_stream_next_token(s)) != NULL) {
264 unsigned total = isl_basic_map_total_dim(bmap);
266 if (tok->type != ISL_TOKEN_IDENT)
269 p = vars_pos(v, tok->u.s, -1);
273 isl_stream_error(s, tok, "expecting unique identifier");
278 bmap = isl_basic_map_cow(bmap);
279 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
282 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
284 isl_seq_clr(bmap->div[k], 1 + 1 + total);
286 tok = isl_stream_next_token(s);
287 if (tok && tok->type == '=') {
289 bmap = add_div_definition(s, v, bmap, k);
290 tok = isl_stream_next_token(s);
293 if (!tok || tok->type != ',')
299 isl_stream_push_token(s, tok);
304 isl_basic_map_free(bmap);
308 static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
310 struct isl_token *tok;
312 tok = isl_stream_next_token(s);
313 if (!tok || tok->type != '[') {
314 isl_stream_error(s, tok, "expecting '['");
318 v = read_var_list(s, v);
319 tok = isl_stream_next_token(s);
320 if (!tok || tok->type != ']') {
321 isl_stream_error(s, tok, "expecting ']'");
334 static struct isl_basic_map *add_constraints(struct isl_stream *s,
335 struct vars **v, struct isl_basic_map *bmap);
337 static struct isl_basic_map *add_exists(struct isl_stream *s,
338 struct vars **v, struct isl_basic_map *bmap)
340 struct isl_token *tok;
347 tok = isl_stream_next_token(s);
350 if (tok->type == '(') {
354 isl_stream_push_token(s, tok);
356 bmap = read_defined_var_list(s, *v, bmap);
360 if (isl_stream_eat(s, ':'))
362 bmap = add_constraints(s, v, bmap);
363 if (seen_paren && isl_stream_eat(s, ')'))
367 isl_basic_map_free(bmap);
371 static struct isl_basic_map *add_constraint(struct isl_stream *s,
372 struct vars **v, struct isl_basic_map *bmap)
374 unsigned total = isl_basic_map_total_dim(bmap);
376 struct isl_token *tok = NULL;
377 struct isl_vec *aff1 = NULL, *aff2 = NULL;
379 tok = isl_stream_next_token(s);
382 if (tok->type == ISL_TOKEN_EXISTS) {
384 return add_exists(s, v, bmap);
386 isl_stream_push_token(s, tok);
389 bmap = isl_basic_map_cow(bmap);
390 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
391 k = isl_basic_map_alloc_inequality(bmap);
395 aff1 = accept_affine(s, *v);
398 tok = isl_stream_next_token(s);
405 isl_stream_error(s, tok, "missing operator");
406 isl_stream_push_token(s, tok);
410 aff2 = accept_affine(s, *v);
413 isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
414 isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
416 if (tok->type == ISL_TOKEN_LE)
417 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
418 (*v)->ctx->one, aff2->el,
420 else if (tok->type == ISL_TOKEN_GE)
421 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
422 (*v)->ctx->negone, aff2->el,
425 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
426 (*v)->ctx->negone, aff2->el,
428 isl_basic_map_inequality_to_equality(bmap, k);
440 isl_basic_map_free(bmap);
444 static struct isl_basic_map *add_constraints(struct isl_stream *s,
445 struct vars **v, struct isl_basic_map *bmap)
447 struct isl_token *tok;
450 bmap = add_constraint(s, v, bmap);
453 tok = isl_stream_next_token(s);
455 isl_stream_error(s, NULL, "unexpected EOF");
458 if (tok->type != ISL_TOKEN_AND)
462 isl_stream_push_token(s, tok);
468 isl_basic_map_free(bmap);
472 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
473 struct vars **v, __isl_take isl_dim *dim)
475 struct isl_basic_map *bmap;
477 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
481 bmap = add_constraints(s, v, bmap);
482 bmap = isl_basic_map_simplify(bmap);
483 bmap = isl_basic_map_finalize(bmap);
487 static struct isl_map *read_disjuncts(struct isl_stream *s,
488 struct vars **v, __isl_take isl_dim *dim)
490 struct isl_token *tok;
493 tok = isl_stream_next_token(s);
495 isl_stream_error(s, NULL, "unexpected EOF");
498 if (tok->type == '}') {
499 isl_stream_push_token(s, tok);
500 return isl_map_universe(dim);
502 isl_stream_push_token(s, tok);
504 map = isl_map_empty(isl_dim_copy(dim));
506 struct isl_basic_map *bmap;
508 bmap = read_disjunct(s, v, isl_dim_copy(dim));
509 map = isl_map_union(map, isl_map_from_basic_map(bmap));
511 tok = isl_stream_next_token(s);
512 if (!tok || tok->type != ISL_TOKEN_OR)
517 isl_stream_push_token(s, tok);
526 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
527 struct isl_stream *s, __isl_take isl_basic_map *bmap)
530 struct isl_token *tok;
540 nparam = isl_basic_map_dim(bmap, isl_dim_param);
541 dim = isl_basic_map_dim(bmap, isl_dim_out);
543 tok = isl_stream_next_token(s);
544 if (!tok || tok->type != ISL_TOKEN_VALUE) {
545 isl_stream_error(s, tok, "expecting coefficient");
547 isl_stream_push_token(s, tok);
550 if (!tok->on_new_line) {
551 isl_stream_error(s, tok, "coefficient should appear on new line");
552 isl_stream_push_token(s, tok);
556 type = isl_int_get_si(tok->u.v);
559 isl_assert(s->ctx, type == 0 || type == 1, goto error);
561 k = isl_basic_map_alloc_equality(bmap);
564 k = isl_basic_map_alloc_inequality(bmap);
570 for (j = 0; j < dim; ++j) {
571 tok = isl_stream_next_token(s);
572 if (!tok || tok->type != ISL_TOKEN_VALUE) {
573 isl_stream_error(s, tok, "expecting coefficient");
575 isl_stream_push_token(s, tok);
578 isl_int_set(c[1 + nparam + j], tok->u.v);
581 for (j = 0; j < nparam; ++j) {
582 tok = isl_stream_next_token(s);
583 if (!tok || tok->type != ISL_TOKEN_VALUE) {
584 isl_stream_error(s, tok, "expecting coefficient");
586 isl_stream_push_token(s, tok);
589 isl_int_set(c[1 + j], tok->u.v);
592 tok = isl_stream_next_token(s);
593 if (!tok || tok->type != ISL_TOKEN_VALUE) {
594 isl_stream_error(s, tok, "expecting coefficient");
596 isl_stream_push_token(s, tok);
599 isl_int_set(c[0], tok->u.v);
604 isl_basic_map_free(bmap);
608 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
612 struct isl_token *tok;
613 struct isl_token *tok2;
617 struct isl_basic_map *bmap = NULL;
622 tok = isl_stream_next_token(s);
624 isl_stream_error(s, NULL, "unexpected EOF");
627 tok2 = isl_stream_next_token(s);
630 isl_stream_error(s, NULL, "unexpected EOF");
633 n_row = isl_int_get_si(tok->u.v);
634 n_col = isl_int_get_si(tok2->u.v);
635 on_new_line = tok2->on_new_line;
636 isl_token_free(tok2);
638 isl_assert(s->ctx, !on_new_line, return NULL);
639 isl_assert(s->ctx, n_row >= 0, return NULL);
640 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
641 dim = n_col - 2 - nparam;
642 bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
646 for (i = 0; i < n_row; ++i)
647 bmap = basic_map_read_polylib_constraint(s, bmap);
649 bmap = isl_basic_map_simplify(bmap);
650 bmap = isl_basic_map_finalize(bmap);
654 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
656 struct isl_token *tok;
657 struct isl_token *tok2;
661 tok = isl_stream_next_token(s);
663 isl_stream_error(s, NULL, "unexpected EOF");
666 tok2 = isl_stream_next_token(s);
669 isl_stream_error(s, NULL, "unexpected EOF");
672 if (!tok2->on_new_line) {
673 isl_stream_push_token(s, tok2);
674 isl_stream_push_token(s, tok);
675 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
677 isl_stream_push_token(s, tok2);
678 n = isl_int_get_si(tok->u.v);
681 isl_assert(s->ctx, n >= 1, return NULL);
683 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
685 for (i = 1; i < n; ++i)
686 map = isl_map_union(map,
687 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
692 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
693 enum isl_dim_type type, int offset, int n)
698 for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
700 for (i = n - 1; i >= 0; --i, v = v->next)
701 dim = isl_dim_set_name(dim, type, i, v->name);
706 static struct isl_dim *dim_from_vars(struct vars *vars,
707 int nparam, int n_in, int n_out)
711 dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
712 dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
713 dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
714 dim = set_names(dim, vars, isl_dim_out, 0, n_out);
719 static struct isl_map *map_read(struct isl_stream *s, int nparam)
721 struct isl_dim *dim = NULL;
722 struct isl_map *map = NULL;
723 struct isl_token *tok;
724 struct vars *v = NULL;
728 tok = isl_stream_next_token(s);
730 isl_stream_error(s, NULL, "unexpected EOF");
733 if (tok->type == ISL_TOKEN_VALUE) {
734 isl_stream_push_token(s, tok);
735 return map_read_polylib(s, nparam);
737 v = vars_new(s->ctx);
738 if (tok->type == '[') {
739 isl_stream_push_token(s, tok);
740 v = read_tuple(s, v);
744 isl_assert(s->ctx, nparam == v->n, goto error);
746 tok = isl_stream_next_token(s);
747 if (!tok || tok->type != ISL_TOKEN_TO) {
748 isl_stream_error(s, tok, "expecting '->'");
750 isl_stream_push_token(s, tok);
754 tok = isl_stream_next_token(s);
758 if (!tok || tok->type != '{') {
759 isl_stream_error(s, tok, "expecting '{'");
761 isl_stream_push_token(s, tok);
765 v = read_tuple(s, v);
769 tok = isl_stream_next_token(s);
770 if (tok && tok->type == ISL_TOKEN_TO) {
772 v = read_tuple(s, v);
775 n2 = v->n - n1 - nparam;
778 isl_stream_push_token(s, tok);
782 dim = dim_from_vars(v, nparam, n1, n2);
783 tok = isl_stream_next_token(s);
785 isl_stream_error(s, NULL, "unexpected EOF");
788 if (tok->type == ':') {
790 map = read_disjuncts(s, &v, isl_dim_copy(dim));
791 tok = isl_stream_next_token(s);
793 map = isl_map_universe(isl_dim_copy(dim));
794 if (tok && tok->type == '}') {
797 isl_stream_error(s, tok, "unexpected isl_token");
814 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
817 struct isl_basic_map *bmap;
819 map = map_read(s, nparam);
823 isl_assert(map->ctx, map->n <= 1, goto error);
826 bmap = isl_basic_map_empty_like_map(map);
828 bmap = isl_basic_map_copy(map->p[0]);
838 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
839 FILE *input, int nparam)
841 struct isl_basic_map *bmap;
842 struct isl_stream *s = isl_stream_new_file(ctx, input);
845 bmap = basic_map_read(s, nparam);
850 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
851 FILE *input, int nparam)
853 struct isl_basic_map *bmap;
854 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
857 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
858 return (struct isl_basic_set *)bmap;
860 isl_basic_map_free(bmap);
864 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
865 const char *str, int nparam)
867 struct isl_basic_map *bmap;
868 struct isl_stream *s = isl_stream_new_str(ctx, str);
871 bmap = basic_map_read(s, nparam);
876 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
877 const char *str, int nparam)
879 struct isl_basic_map *bmap;
880 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
883 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
884 return (struct isl_basic_set *)bmap;
886 isl_basic_map_free(bmap);
890 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
891 FILE *input, int nparam)
894 struct isl_stream *s = isl_stream_new_file(ctx, input);
897 map = map_read(s, nparam);
902 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
903 const char *str, int nparam)
906 struct isl_stream *s = isl_stream_new_str(ctx, str);
909 map = map_read(s, nparam);
914 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
915 FILE *input, int nparam)
918 map = isl_map_read_from_file(ctx, input, nparam);
921 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
922 return (struct isl_set *)map;
928 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
929 const char *str, int nparam)
932 map = isl_map_read_from_str(ctx, str, nparam);
935 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
936 return (struct isl_set *)map;
942 static char *next_line(FILE *input, char *line, unsigned len)
947 if (!(p = fgets(line, len, input)))
949 while (isspace(*p) && *p != '\n')
951 } while (*p == '#' || *p == '\n');
956 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
959 struct isl_vec *vec = NULL;
968 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
969 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
971 vec = isl_vec_alloc(ctx, size);
973 p = next_line(input, line, sizeof(line));
974 isl_assert(ctx, p, goto error);
976 for (j = 0; j < size; ++j) {
977 n = sscanf(p, "%s%n", val, &offset);
978 isl_assert(ctx, n != 0, goto error);
979 isl_int_read(vec->el[j], val);
989 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
990 FILE *input, unsigned input_format)
992 if (input_format == ISL_FORMAT_POLYLIB)
993 return isl_vec_read_from_file_polylib(ctx, input);
995 isl_assert(ctx, 0, return NULL);