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 int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
552 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
553 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
554 isl_basic_map_dim(bmap, isl_dim_in) + pos;
555 pos -= isl_basic_map_dim(bmap, isl_dim_out);
557 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
558 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
559 pos -= isl_basic_map_dim(bmap, isl_dim_in);
561 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
562 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
563 isl_basic_map_dim(bmap, isl_dim_in) +
564 isl_basic_map_dim(bmap, isl_dim_out) + pos;
565 pos -= isl_basic_map_dim(bmap, isl_dim_div);
567 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
573 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
574 struct isl_stream *s, __isl_take isl_basic_map *bmap)
577 struct isl_token *tok;
587 nparam = isl_basic_map_dim(bmap, isl_dim_param);
588 dim = isl_basic_map_dim(bmap, isl_dim_out);
590 tok = isl_stream_next_token(s);
591 if (!tok || tok->type != ISL_TOKEN_VALUE) {
592 isl_stream_error(s, tok, "expecting coefficient");
594 isl_stream_push_token(s, tok);
597 if (!tok->on_new_line) {
598 isl_stream_error(s, tok, "coefficient should appear on new line");
599 isl_stream_push_token(s, tok);
603 type = isl_int_get_si(tok->u.v);
606 isl_assert(s->ctx, type == 0 || type == 1, goto error);
608 k = isl_basic_map_alloc_equality(bmap);
611 k = isl_basic_map_alloc_inequality(bmap);
617 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
619 tok = isl_stream_next_token(s);
620 if (!tok || tok->type != ISL_TOKEN_VALUE) {
621 isl_stream_error(s, tok, "expecting coefficient");
623 isl_stream_push_token(s, tok);
626 pos = polylib_pos_to_isl_pos(bmap, j);
627 isl_int_set(c[pos], tok->u.v);
633 isl_basic_map_free(bmap);
637 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
641 struct isl_token *tok;
642 struct isl_token *tok2;
646 struct isl_basic_map *bmap = NULL;
651 tok = isl_stream_next_token(s);
653 isl_stream_error(s, NULL, "unexpected EOF");
656 tok2 = isl_stream_next_token(s);
659 isl_stream_error(s, NULL, "unexpected EOF");
662 n_row = isl_int_get_si(tok->u.v);
663 n_col = isl_int_get_si(tok2->u.v);
664 on_new_line = tok2->on_new_line;
665 isl_token_free(tok2);
667 isl_assert(s->ctx, !on_new_line, return NULL);
668 isl_assert(s->ctx, n_row >= 0, return NULL);
669 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
670 dim = n_col - 2 - nparam;
671 bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
675 for (i = 0; i < n_row; ++i)
676 bmap = basic_map_read_polylib_constraint(s, bmap);
678 bmap = isl_basic_map_simplify(bmap);
679 bmap = isl_basic_map_finalize(bmap);
683 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
685 struct isl_token *tok;
686 struct isl_token *tok2;
690 tok = isl_stream_next_token(s);
692 isl_stream_error(s, NULL, "unexpected EOF");
695 tok2 = isl_stream_next_token(s);
698 isl_stream_error(s, NULL, "unexpected EOF");
701 if (!tok2->on_new_line) {
702 isl_stream_push_token(s, tok2);
703 isl_stream_push_token(s, tok);
704 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
706 isl_stream_push_token(s, tok2);
707 n = isl_int_get_si(tok->u.v);
710 isl_assert(s->ctx, n >= 1, return NULL);
712 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
714 for (i = 1; i < n; ++i)
715 map = isl_map_union(map,
716 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
721 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
722 enum isl_dim_type type, int offset, int n)
727 for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
729 for (i = n - 1; i >= 0; --i, v = v->next)
730 dim = isl_dim_set_name(dim, type, i, v->name);
735 static struct isl_dim *dim_from_vars(struct vars *vars,
736 int nparam, int n_in, int n_out)
740 dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
741 dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
742 dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
743 dim = set_names(dim, vars, isl_dim_out, 0, n_out);
748 static struct isl_map *map_read(struct isl_stream *s, int nparam)
750 struct isl_dim *dim = NULL;
751 struct isl_map *map = NULL;
752 struct isl_token *tok;
753 struct vars *v = NULL;
757 tok = isl_stream_next_token(s);
759 isl_stream_error(s, NULL, "unexpected EOF");
762 if (tok->type == ISL_TOKEN_VALUE) {
763 isl_stream_push_token(s, tok);
764 return map_read_polylib(s, nparam);
766 v = vars_new(s->ctx);
767 if (tok->type == '[') {
768 isl_stream_push_token(s, tok);
769 v = read_tuple(s, v);
773 isl_assert(s->ctx, nparam == v->n, goto error);
775 tok = isl_stream_next_token(s);
776 if (!tok || tok->type != ISL_TOKEN_TO) {
777 isl_stream_error(s, tok, "expecting '->'");
779 isl_stream_push_token(s, tok);
783 tok = isl_stream_next_token(s);
787 if (!tok || tok->type != '{') {
788 isl_stream_error(s, tok, "expecting '{'");
790 isl_stream_push_token(s, tok);
794 v = read_tuple(s, v);
798 tok = isl_stream_next_token(s);
799 if (tok && tok->type == ISL_TOKEN_TO) {
801 v = read_tuple(s, v);
804 n2 = v->n - n1 - nparam;
807 isl_stream_push_token(s, tok);
811 dim = dim_from_vars(v, nparam, n1, n2);
812 tok = isl_stream_next_token(s);
814 isl_stream_error(s, NULL, "unexpected EOF");
817 if (tok->type == ':') {
819 map = read_disjuncts(s, &v, isl_dim_copy(dim));
820 tok = isl_stream_next_token(s);
822 map = isl_map_universe(isl_dim_copy(dim));
823 if (tok && tok->type == '}') {
826 isl_stream_error(s, tok, "unexpected isl_token");
843 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
846 struct isl_basic_map *bmap;
848 map = map_read(s, nparam);
852 isl_assert(map->ctx, map->n <= 1, goto error);
855 bmap = isl_basic_map_empty_like_map(map);
857 bmap = isl_basic_map_copy(map->p[0]);
867 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
868 FILE *input, int nparam)
870 struct isl_basic_map *bmap;
871 struct isl_stream *s = isl_stream_new_file(ctx, input);
874 bmap = basic_map_read(s, nparam);
879 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
880 FILE *input, int nparam)
882 struct isl_basic_map *bmap;
883 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
886 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
887 return (struct isl_basic_set *)bmap;
889 isl_basic_map_free(bmap);
893 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
894 const char *str, int nparam)
896 struct isl_basic_map *bmap;
897 struct isl_stream *s = isl_stream_new_str(ctx, str);
900 bmap = basic_map_read(s, nparam);
905 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
906 const char *str, int nparam)
908 struct isl_basic_map *bmap;
909 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
912 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
913 return (struct isl_basic_set *)bmap;
915 isl_basic_map_free(bmap);
919 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
920 FILE *input, int nparam)
923 struct isl_stream *s = isl_stream_new_file(ctx, input);
926 map = map_read(s, nparam);
931 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
932 const char *str, int nparam)
935 struct isl_stream *s = isl_stream_new_str(ctx, str);
938 map = map_read(s, nparam);
943 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
944 FILE *input, int nparam)
947 map = isl_map_read_from_file(ctx, input, nparam);
950 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
951 return (struct isl_set *)map;
957 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
958 const char *str, int nparam)
961 map = isl_map_read_from_str(ctx, str, nparam);
964 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
965 return (struct isl_set *)map;
971 static char *next_line(FILE *input, char *line, unsigned len)
976 if (!(p = fgets(line, len, input)))
978 while (isspace(*p) && *p != '\n')
980 } while (*p == '#' || *p == '\n');
985 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
988 struct isl_vec *vec = NULL;
997 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
998 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1000 vec = isl_vec_alloc(ctx, size);
1002 p = next_line(input, line, sizeof(line));
1003 isl_assert(ctx, p, goto error);
1005 for (j = 0; j < size; ++j) {
1006 n = sscanf(p, "%s%n", val, &offset);
1007 isl_assert(ctx, n != 0, goto error);
1008 isl_int_read(vec->el[j], val);
1018 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1019 FILE *input, unsigned input_format)
1021 if (input_format == ISL_FORMAT_POLYLIB)
1022 return isl_vec_read_from_file_polylib(ctx, input);
1024 isl_assert(ctx, 0, return NULL);