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 __isl_give isl_basic_map *basic_map_read_polylib_constraint(
472 struct isl_stream *s, __isl_take isl_basic_map *bmap)
475 struct isl_token *tok;
485 nparam = isl_basic_map_dim(bmap, isl_dim_param);
486 dim = isl_basic_map_dim(bmap, isl_dim_out);
488 tok = isl_stream_next_token(s);
489 if (!tok || tok->type != ISL_TOKEN_VALUE) {
490 isl_stream_error(s, tok, "expecting coefficient");
492 isl_stream_push_token(s, tok);
495 if (!tok->on_new_line) {
496 isl_stream_error(s, tok, "coefficient should appear on new line");
497 isl_stream_push_token(s, tok);
501 type = isl_int_get_si(tok->u.v);
504 isl_assert(s->ctx, type == 0 || type == 1, goto error);
506 k = isl_basic_map_alloc_equality(bmap);
509 k = isl_basic_map_alloc_inequality(bmap);
515 for (j = 0; j < dim; ++j) {
516 tok = isl_stream_next_token(s);
517 if (!tok || tok->type != ISL_TOKEN_VALUE) {
518 isl_stream_error(s, tok, "expecting coefficient");
520 isl_stream_push_token(s, tok);
523 isl_int_set(c[1 + nparam + j], tok->u.v);
526 for (j = 0; j < nparam; ++j) {
527 tok = isl_stream_next_token(s);
528 if (!tok || tok->type != ISL_TOKEN_VALUE) {
529 isl_stream_error(s, tok, "expecting coefficient");
531 isl_stream_push_token(s, tok);
534 isl_int_set(c[1 + j], tok->u.v);
537 tok = isl_stream_next_token(s);
538 if (!tok || tok->type != ISL_TOKEN_VALUE) {
539 isl_stream_error(s, tok, "expecting coefficient");
541 isl_stream_push_token(s, tok);
544 isl_int_set(c[0], tok->u.v);
549 isl_basic_map_free(bmap);
553 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
557 struct isl_token *tok;
558 struct isl_token *tok2;
562 struct isl_basic_map *bmap = NULL;
567 tok = isl_stream_next_token(s);
569 isl_stream_error(s, NULL, "unexpected EOF");
572 tok2 = isl_stream_next_token(s);
575 isl_stream_error(s, NULL, "unexpected EOF");
578 n_row = isl_int_get_si(tok->u.v);
579 n_col = isl_int_get_si(tok2->u.v);
580 on_new_line = tok2->on_new_line;
581 isl_token_free(tok2);
583 isl_assert(s->ctx, !on_new_line, return NULL);
584 isl_assert(s->ctx, n_row >= 0, return NULL);
585 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
586 dim = n_col - 2 - nparam;
587 bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
591 for (i = 0; i < n_row; ++i)
592 bmap = basic_map_read_polylib_constraint(s, bmap);
594 bmap = isl_basic_map_simplify(bmap);
595 bmap = isl_basic_map_finalize(bmap);
599 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
601 struct isl_token *tok;
602 struct isl_token *tok2;
606 tok = isl_stream_next_token(s);
608 isl_stream_error(s, NULL, "unexpected EOF");
611 tok2 = isl_stream_next_token(s);
614 isl_stream_error(s, NULL, "unexpected EOF");
617 if (!tok2->on_new_line) {
618 isl_stream_push_token(s, tok2);
619 isl_stream_push_token(s, tok);
620 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
622 isl_stream_push_token(s, tok2);
623 n = isl_int_get_si(tok->u.v);
626 isl_assert(s->ctx, n >= 1, return NULL);
628 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
630 for (i = 1; i < n; ++i)
631 map = isl_map_union(map,
632 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
637 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
638 enum isl_dim_type type, int offset, int n)
643 for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
645 for (i = n - 1; i >= 0; --i, v = v->next)
646 dim = isl_dim_set_name(dim, type, i, v->name);
651 static struct isl_dim *dim_from_vars(struct vars *vars,
652 int nparam, int n_in, int n_out)
656 dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
657 dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
658 dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
659 dim = set_names(dim, vars, isl_dim_out, 0, n_out);
664 static struct isl_map *map_read(struct isl_stream *s, int nparam)
667 struct isl_basic_map *bmap = NULL;
668 struct isl_token *tok;
669 struct vars *v = NULL;
673 tok = isl_stream_next_token(s);
675 isl_stream_error(s, NULL, "unexpected EOF");
678 if (tok->type == ISL_TOKEN_VALUE) {
679 isl_stream_push_token(s, tok);
680 return map_read_polylib(s, nparam);
682 v = vars_new(s->ctx);
683 if (tok->type == '[') {
684 isl_stream_push_token(s, tok);
685 v = read_tuple(s, v);
689 isl_assert(s->ctx, nparam == v->n, goto error);
691 tok = isl_stream_next_token(s);
692 if (!tok || tok->type != ISL_TOKEN_TO) {
693 isl_stream_error(s, tok, "expecting '->'");
695 isl_stream_push_token(s, tok);
699 tok = isl_stream_next_token(s);
703 if (!tok || tok->type != '{') {
704 isl_stream_error(s, tok, "expecting '{'");
706 isl_stream_push_token(s, tok);
710 v = read_tuple(s, v);
714 tok = isl_stream_next_token(s);
715 if (tok && tok->type == ISL_TOKEN_TO) {
717 v = read_tuple(s, v);
720 n2 = v->n - n1 - nparam;
723 isl_stream_push_token(s, tok);
727 dim = dim_from_vars(v, nparam, n1, n2);
728 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
731 tok = isl_stream_next_token(s);
732 if (tok && tok->type == ':') {
734 bmap = add_constraints(s, &v, bmap);
735 tok = isl_stream_next_token(s);
737 if (tok && tok->type == '}') {
740 isl_stream_error(s, tok, "unexpected isl_token");
747 bmap = isl_basic_map_simplify(bmap);
748 bmap = isl_basic_map_finalize(bmap);
749 return isl_map_from_basic_map(bmap);
751 isl_basic_map_free(bmap);
757 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
760 struct isl_basic_map *bmap;
762 map = map_read(s, nparam);
766 isl_assert(map->ctx, map->n <= 1, goto error);
769 bmap = isl_basic_map_empty_like_map(map);
771 bmap = isl_basic_map_copy(map->p[0]);
781 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
782 FILE *input, int nparam)
784 struct isl_basic_map *bmap;
785 struct isl_stream *s = isl_stream_new_file(ctx, input);
788 bmap = basic_map_read(s, nparam);
793 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
794 FILE *input, int nparam)
796 struct isl_basic_map *bmap;
797 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
800 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
801 return (struct isl_basic_set *)bmap;
803 isl_basic_map_free(bmap);
807 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
808 const char *str, int nparam)
810 struct isl_basic_map *bmap;
811 struct isl_stream *s = isl_stream_new_str(ctx, str);
814 bmap = basic_map_read(s, nparam);
819 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
820 const char *str, int nparam)
822 struct isl_basic_map *bmap;
823 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
826 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
827 return (struct isl_basic_set *)bmap;
829 isl_basic_map_free(bmap);
833 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
834 FILE *input, int nparam)
837 struct isl_stream *s = isl_stream_new_file(ctx, input);
840 map = map_read(s, nparam);
845 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
846 FILE *input, int nparam)
849 map = isl_map_read_from_file(ctx, input, nparam);
852 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
853 return (struct isl_set *)map;
859 static char *next_line(FILE *input, char *line, unsigned len)
864 if (!(p = fgets(line, len, input)))
866 while (isspace(*p) && *p != '\n')
868 } while (*p == '#' || *p == '\n');
873 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
876 struct isl_vec *vec = NULL;
885 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
886 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
888 vec = isl_vec_alloc(ctx, size);
890 p = next_line(input, line, sizeof(line));
891 isl_assert(ctx, p, goto error);
893 for (j = 0; j < size; ++j) {
894 n = sscanf(p, "%s%n", val, &offset);
895 isl_assert(ctx, n != 0, goto error);
896 isl_int_read(vec->el[j], val);
906 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
907 FILE *input, unsigned input_format)
909 if (input_format == ISL_FORMAT_POLYLIB)
910 return isl_vec_read_from_file_polylib(ctx, input);
912 isl_assert(ctx, 0, return NULL);