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);
409 aff2 = accept_affine(s, *v);
412 isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
413 isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
415 if (tok->type == ISL_TOKEN_LE)
416 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
417 (*v)->ctx->one, aff2->el,
419 else if (tok->type == ISL_TOKEN_GE)
420 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
421 (*v)->ctx->negone, aff2->el,
424 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
425 (*v)->ctx->negone, aff2->el,
427 isl_basic_map_inequality_to_equality(bmap, k);
439 isl_basic_map_free(bmap);
443 static struct isl_basic_map *add_constraints(struct isl_stream *s,
444 struct vars **v, struct isl_basic_map *bmap)
446 struct isl_token *tok;
449 bmap = add_constraint(s, v, bmap);
452 tok = isl_stream_next_token(s);
454 isl_stream_error(s, NULL, "unexpected EOF");
457 if (tok->type != ISL_TOKEN_AND)
461 isl_stream_push_token(s, tok);
467 isl_basic_map_free(bmap);
471 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
472 struct vars **v, __isl_take isl_dim *dim)
474 struct isl_basic_map *bmap;
476 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
480 bmap = add_constraints(s, v, bmap);
481 bmap = isl_basic_map_simplify(bmap);
482 bmap = isl_basic_map_finalize(bmap);
486 static struct isl_map *read_disjuncts(struct isl_stream *s,
487 struct vars **v, __isl_take isl_dim *dim)
489 struct isl_token *tok;
492 tok = isl_stream_next_token(s);
494 isl_stream_error(s, NULL, "unexpected EOF");
497 if (tok->type == '}') {
498 isl_stream_push_token(s, tok);
499 return isl_map_universe(dim);
501 isl_stream_push_token(s, tok);
503 map = isl_map_empty(isl_dim_copy(dim));
505 struct isl_basic_map *bmap;
507 bmap = read_disjunct(s, v, isl_dim_copy(dim));
508 map = isl_map_union(map, isl_map_from_basic_map(bmap));
510 tok = isl_stream_next_token(s);
511 if (!tok || tok->type != ISL_TOKEN_OR)
516 isl_stream_push_token(s, tok);
525 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
526 struct isl_stream *s, __isl_take isl_basic_map *bmap)
529 struct isl_token *tok;
539 nparam = isl_basic_map_dim(bmap, isl_dim_param);
540 dim = isl_basic_map_dim(bmap, isl_dim_out);
542 tok = isl_stream_next_token(s);
543 if (!tok || tok->type != ISL_TOKEN_VALUE) {
544 isl_stream_error(s, tok, "expecting coefficient");
546 isl_stream_push_token(s, tok);
549 if (!tok->on_new_line) {
550 isl_stream_error(s, tok, "coefficient should appear on new line");
551 isl_stream_push_token(s, tok);
555 type = isl_int_get_si(tok->u.v);
558 isl_assert(s->ctx, type == 0 || type == 1, goto error);
560 k = isl_basic_map_alloc_equality(bmap);
563 k = isl_basic_map_alloc_inequality(bmap);
569 for (j = 0; j < dim; ++j) {
570 tok = isl_stream_next_token(s);
571 if (!tok || tok->type != ISL_TOKEN_VALUE) {
572 isl_stream_error(s, tok, "expecting coefficient");
574 isl_stream_push_token(s, tok);
577 isl_int_set(c[1 + nparam + j], tok->u.v);
580 for (j = 0; j < nparam; ++j) {
581 tok = isl_stream_next_token(s);
582 if (!tok || tok->type != ISL_TOKEN_VALUE) {
583 isl_stream_error(s, tok, "expecting coefficient");
585 isl_stream_push_token(s, tok);
588 isl_int_set(c[1 + j], tok->u.v);
591 tok = isl_stream_next_token(s);
592 if (!tok || tok->type != ISL_TOKEN_VALUE) {
593 isl_stream_error(s, tok, "expecting coefficient");
595 isl_stream_push_token(s, tok);
598 isl_int_set(c[0], tok->u.v);
603 isl_basic_map_free(bmap);
607 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
611 struct isl_token *tok;
612 struct isl_token *tok2;
616 struct isl_basic_map *bmap = NULL;
621 tok = isl_stream_next_token(s);
623 isl_stream_error(s, NULL, "unexpected EOF");
626 tok2 = isl_stream_next_token(s);
629 isl_stream_error(s, NULL, "unexpected EOF");
632 n_row = isl_int_get_si(tok->u.v);
633 n_col = isl_int_get_si(tok2->u.v);
634 on_new_line = tok2->on_new_line;
635 isl_token_free(tok2);
637 isl_assert(s->ctx, !on_new_line, return NULL);
638 isl_assert(s->ctx, n_row >= 0, return NULL);
639 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
640 dim = n_col - 2 - nparam;
641 bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
645 for (i = 0; i < n_row; ++i)
646 bmap = basic_map_read_polylib_constraint(s, bmap);
648 bmap = isl_basic_map_simplify(bmap);
649 bmap = isl_basic_map_finalize(bmap);
653 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
655 struct isl_token *tok;
656 struct isl_token *tok2;
660 tok = isl_stream_next_token(s);
662 isl_stream_error(s, NULL, "unexpected EOF");
665 tok2 = isl_stream_next_token(s);
668 isl_stream_error(s, NULL, "unexpected EOF");
671 if (!tok2->on_new_line) {
672 isl_stream_push_token(s, tok2);
673 isl_stream_push_token(s, tok);
674 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
676 isl_stream_push_token(s, tok2);
677 n = isl_int_get_si(tok->u.v);
680 isl_assert(s->ctx, n >= 1, return NULL);
682 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
684 for (i = 1; i < n; ++i)
685 map = isl_map_union(map,
686 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
691 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
692 enum isl_dim_type type, int offset, int n)
697 for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
699 for (i = n - 1; i >= 0; --i, v = v->next)
700 dim = isl_dim_set_name(dim, type, i, v->name);
705 static struct isl_dim *dim_from_vars(struct vars *vars,
706 int nparam, int n_in, int n_out)
710 dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
711 dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
712 dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
713 dim = set_names(dim, vars, isl_dim_out, 0, n_out);
718 static struct isl_map *map_read(struct isl_stream *s, int nparam)
720 struct isl_dim *dim = NULL;
721 struct isl_map *map = NULL;
722 struct isl_token *tok;
723 struct vars *v = NULL;
727 tok = isl_stream_next_token(s);
729 isl_stream_error(s, NULL, "unexpected EOF");
732 if (tok->type == ISL_TOKEN_VALUE) {
733 isl_stream_push_token(s, tok);
734 return map_read_polylib(s, nparam);
736 v = vars_new(s->ctx);
737 if (tok->type == '[') {
738 isl_stream_push_token(s, tok);
739 v = read_tuple(s, v);
743 isl_assert(s->ctx, nparam == v->n, goto error);
745 tok = isl_stream_next_token(s);
746 if (!tok || tok->type != ISL_TOKEN_TO) {
747 isl_stream_error(s, tok, "expecting '->'");
749 isl_stream_push_token(s, tok);
753 tok = isl_stream_next_token(s);
757 if (!tok || tok->type != '{') {
758 isl_stream_error(s, tok, "expecting '{'");
760 isl_stream_push_token(s, tok);
764 v = read_tuple(s, v);
768 tok = isl_stream_next_token(s);
769 if (tok && tok->type == ISL_TOKEN_TO) {
771 v = read_tuple(s, v);
774 n2 = v->n - n1 - nparam;
777 isl_stream_push_token(s, tok);
781 dim = dim_from_vars(v, nparam, n1, n2);
782 tok = isl_stream_next_token(s);
784 isl_stream_error(s, NULL, "unexpected EOF");
787 if (tok->type == ':') {
789 map = read_disjuncts(s, &v, isl_dim_copy(dim));
790 tok = isl_stream_next_token(s);
792 map = isl_map_universe(isl_dim_copy(dim));
793 if (tok && tok->type == '}') {
796 isl_stream_error(s, tok, "unexpected isl_token");
813 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
816 struct isl_basic_map *bmap;
818 map = map_read(s, nparam);
822 isl_assert(map->ctx, map->n <= 1, goto error);
825 bmap = isl_basic_map_empty_like_map(map);
827 bmap = isl_basic_map_copy(map->p[0]);
837 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
838 FILE *input, int nparam)
840 struct isl_basic_map *bmap;
841 struct isl_stream *s = isl_stream_new_file(ctx, input);
844 bmap = basic_map_read(s, nparam);
849 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
850 FILE *input, int nparam)
852 struct isl_basic_map *bmap;
853 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
856 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
857 return (struct isl_basic_set *)bmap;
859 isl_basic_map_free(bmap);
863 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
864 const char *str, int nparam)
866 struct isl_basic_map *bmap;
867 struct isl_stream *s = isl_stream_new_str(ctx, str);
870 bmap = basic_map_read(s, nparam);
875 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
876 const char *str, int nparam)
878 struct isl_basic_map *bmap;
879 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
882 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
883 return (struct isl_basic_set *)bmap;
885 isl_basic_map_free(bmap);
889 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
890 FILE *input, int nparam)
893 struct isl_stream *s = isl_stream_new_file(ctx, input);
896 map = map_read(s, nparam);
901 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
902 FILE *input, int nparam)
905 map = isl_map_read_from_file(ctx, input, nparam);
908 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
909 return (struct isl_set *)map;
915 static char *next_line(FILE *input, char *line, unsigned len)
920 if (!(p = fgets(line, len, input)))
922 while (isspace(*p) && *p != '\n')
924 } while (*p == '#' || *p == '\n');
929 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
932 struct isl_vec *vec = NULL;
941 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
942 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
944 vec = isl_vec_alloc(ctx, size);
946 p = next_line(input, line, sizeof(line));
947 isl_assert(ctx, p, goto error);
949 for (j = 0; j < size; ++j) {
950 n = sscanf(p, "%s%n", val, &offset);
951 isl_assert(ctx, n != 0, goto error);
952 isl_int_read(vec->el[j], val);
962 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
963 FILE *input, unsigned input_format)
965 if (input_format == ISL_FORMAT_POLYLIB)
966 return isl_vec_read_from_file_polylib(ctx, input);
968 isl_assert(ctx, 0, return NULL);