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 vars *read_tuple(struct isl_stream *s, struct vars *v)
141 struct isl_token *tok;
143 tok = isl_stream_next_token(s);
144 if (!tok || tok->type != '[') {
145 isl_stream_error(s, tok, "expecting '['");
149 v = read_var_list(s, v);
150 tok = isl_stream_next_token(s);
151 if (!tok || tok->type != ']') {
152 isl_stream_error(s, tok, "expecting ']'");
165 static struct isl_basic_map *add_constraints(struct isl_stream *s,
166 struct vars **v, struct isl_basic_map *bmap);
168 static struct isl_basic_map *add_exists(struct isl_stream *s,
169 struct vars **v, struct isl_basic_map *bmap)
171 struct isl_token *tok;
178 tok = isl_stream_next_token(s);
181 if (tok->type == '(') {
185 isl_stream_push_token(s, tok);
186 *v = read_var_list(s, *v);
190 bmap = isl_basic_map_cow(bmap);
191 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
193 total = isl_basic_map_total_dim(bmap);
194 for (i = 0; i < extra; ++i) {
196 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
198 isl_seq_clr(bmap->div[k], 1+1+total);
202 if (isl_stream_eat(s, ':'))
204 bmap = add_constraints(s, v, bmap);
205 if (seen_paren && isl_stream_eat(s, ')'))
209 isl_basic_map_free(bmap);
213 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
215 struct isl_token *tok = NULL;
218 aff = isl_vec_alloc(v->ctx, 1 + v->n);
219 isl_seq_clr(aff->el, aff->size);
224 tok = isl_stream_next_token(s);
226 isl_stream_error(s, NULL, "unexpected EOF");
229 if (tok->type == ISL_TOKEN_IDENT) {
231 int pos = vars_pos(v, tok->u.s, -1);
235 isl_stream_error(s, tok, "unknown identifier");
238 isl_int_add_ui(aff->el[1 + pos], aff->el[1 + pos], 1);
239 } else if (tok->type == ISL_TOKEN_VALUE) {
240 struct isl_token *tok2;
243 tok2 = isl_stream_next_token(s);
244 if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
245 pos = vars_pos(v, tok2->u.s, -1);
249 isl_stream_error(s, tok2,
250 "unknown identifier");
251 isl_token_free(tok2);
254 isl_token_free(tok2);
256 isl_stream_push_token(s, tok2);
257 isl_int_add(aff->el[1 + pos],
258 aff->el[1 + pos], tok->u.v);
259 } else if (tok->type == '+') {
262 isl_stream_push_token(s, tok);
274 static struct isl_basic_map *add_constraint(struct isl_stream *s,
275 struct vars **v, struct isl_basic_map *bmap)
277 unsigned total = isl_basic_map_total_dim(bmap);
279 struct isl_token *tok = NULL;
280 struct isl_vec *aff1 = NULL, *aff2 = NULL;
282 tok = isl_stream_next_token(s);
285 if (tok->type == ISL_TOKEN_EXISTS) {
287 return add_exists(s, v, bmap);
289 isl_stream_push_token(s, tok);
292 bmap = isl_basic_map_cow(bmap);
293 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
294 k = isl_basic_map_alloc_inequality(bmap);
298 aff1 = accept_affine(s, *v);
301 tok = isl_stream_next_token(s);
308 isl_stream_error(s, tok, "missing operator");
309 isl_stream_push_token(s, tok);
312 aff2 = accept_affine(s, *v);
315 isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
316 isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
318 if (tok->type == ISL_TOKEN_LE)
319 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
320 (*v)->ctx->one, aff2->el,
322 else if (tok->type == ISL_TOKEN_GE)
323 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
324 (*v)->ctx->negone, aff2->el,
327 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
328 (*v)->ctx->negone, aff2->el,
330 isl_basic_map_inequality_to_equality(bmap, k);
342 isl_basic_map_free(bmap);
346 static struct isl_basic_map *add_constraints(struct isl_stream *s,
347 struct vars **v, struct isl_basic_map *bmap)
349 struct isl_token *tok;
352 bmap = add_constraint(s, v, bmap);
355 tok = isl_stream_next_token(s);
357 isl_stream_error(s, NULL, "unexpected EOF");
360 if (tok->type != ISL_TOKEN_AND)
364 isl_stream_push_token(s, tok);
370 isl_basic_map_free(bmap);
374 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
375 struct isl_stream *s, __isl_take isl_basic_map *bmap)
378 struct isl_token *tok;
388 nparam = isl_basic_map_dim(bmap, isl_dim_param);
389 dim = isl_basic_map_dim(bmap, isl_dim_out);
391 tok = isl_stream_next_token(s);
392 if (!tok || tok->type != ISL_TOKEN_VALUE) {
393 isl_stream_error(s, tok, "expecting coefficient");
395 isl_stream_push_token(s, tok);
398 if (!tok->on_new_line) {
399 isl_stream_error(s, tok, "coefficient should appear on new line");
400 isl_stream_push_token(s, tok);
404 type = isl_int_get_si(tok->u.v);
407 isl_assert(s->ctx, type == 0 || type == 1, goto error);
409 k = isl_basic_map_alloc_equality(bmap);
412 k = isl_basic_map_alloc_inequality(bmap);
418 for (j = 0; j < dim; ++j) {
419 tok = isl_stream_next_token(s);
420 if (!tok || tok->type != ISL_TOKEN_VALUE) {
421 isl_stream_error(s, tok, "expecting coefficient");
423 isl_stream_push_token(s, tok);
426 isl_int_set(c[1 + nparam + j], tok->u.v);
429 for (j = 0; j < nparam; ++j) {
430 tok = isl_stream_next_token(s);
431 if (!tok || tok->type != ISL_TOKEN_VALUE) {
432 isl_stream_error(s, tok, "expecting coefficient");
434 isl_stream_push_token(s, tok);
437 isl_int_set(c[1 + j], tok->u.v);
440 tok = isl_stream_next_token(s);
441 if (!tok || tok->type != ISL_TOKEN_VALUE) {
442 isl_stream_error(s, tok, "expecting coefficient");
444 isl_stream_push_token(s, tok);
447 isl_int_set(c[0], tok->u.v);
452 isl_basic_map_free(bmap);
456 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
460 struct isl_token *tok;
461 struct isl_token *tok2;
465 struct isl_basic_map *bmap = NULL;
470 tok = isl_stream_next_token(s);
472 isl_stream_error(s, NULL, "unexpected EOF");
475 tok2 = isl_stream_next_token(s);
478 isl_stream_error(s, NULL, "unexpected EOF");
481 n_row = isl_int_get_si(tok->u.v);
482 n_col = isl_int_get_si(tok2->u.v);
483 on_new_line = tok2->on_new_line;
484 isl_token_free(tok2);
486 isl_assert(s->ctx, !on_new_line, return NULL);
487 isl_assert(s->ctx, n_row >= 0, return NULL);
488 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
489 dim = n_col - 2 - nparam;
490 bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
494 for (i = 0; i < n_row; ++i)
495 bmap = basic_map_read_polylib_constraint(s, bmap);
497 bmap = isl_basic_map_simplify(bmap);
498 bmap = isl_basic_map_finalize(bmap);
502 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
504 struct isl_token *tok;
505 struct isl_token *tok2;
509 tok = isl_stream_next_token(s);
511 isl_stream_error(s, NULL, "unexpected EOF");
514 tok2 = isl_stream_next_token(s);
517 isl_stream_error(s, NULL, "unexpected EOF");
520 if (!tok2->on_new_line) {
521 isl_stream_push_token(s, tok2);
522 isl_stream_push_token(s, tok);
523 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
525 isl_stream_push_token(s, tok2);
526 n = isl_int_get_si(tok->u.v);
529 isl_assert(s->ctx, n >= 1, return NULL);
531 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
533 for (i = 1; i < n; ++i)
534 map = isl_map_union(map,
535 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
540 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
541 enum isl_dim_type type, int offset, int n)
546 for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
548 for (i = n - 1; i >= 0; --i, v = v->next)
549 dim = isl_dim_set_name(dim, type, i, v->name);
554 static struct isl_dim *dim_from_vars(struct vars *vars,
555 int nparam, int n_in, int n_out)
559 dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
560 dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
561 dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
562 dim = set_names(dim, vars, isl_dim_out, 0, n_out);
567 static struct isl_map *map_read(struct isl_stream *s, int nparam)
570 struct isl_basic_map *bmap = NULL;
571 struct isl_token *tok;
572 struct vars *v = NULL;
576 tok = isl_stream_next_token(s);
578 isl_stream_error(s, NULL, "unexpected EOF");
581 if (tok->type == ISL_TOKEN_VALUE) {
582 isl_stream_push_token(s, tok);
583 return map_read_polylib(s, nparam);
585 v = vars_new(s->ctx);
586 if (tok->type == '[') {
587 isl_stream_push_token(s, tok);
588 v = read_tuple(s, v);
592 isl_assert(s->ctx, nparam == v->n, goto error);
594 tok = isl_stream_next_token(s);
595 if (!tok || tok->type != ISL_TOKEN_TO) {
596 isl_stream_error(s, tok, "expecting '->'");
598 isl_stream_push_token(s, tok);
602 tok = isl_stream_next_token(s);
606 if (!tok || tok->type != '{') {
607 isl_stream_error(s, tok, "expecting '{'");
609 isl_stream_push_token(s, tok);
613 v = read_tuple(s, v);
617 tok = isl_stream_next_token(s);
618 if (tok && tok->type == ISL_TOKEN_TO) {
620 v = read_tuple(s, v);
623 n2 = v->n - n1 - nparam;
626 isl_stream_push_token(s, tok);
630 dim = dim_from_vars(v, nparam, n1, n2);
631 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
634 tok = isl_stream_next_token(s);
635 if (tok && tok->type == ':') {
637 bmap = add_constraints(s, &v, bmap);
638 tok = isl_stream_next_token(s);
640 if (tok && tok->type == '}') {
643 isl_stream_error(s, tok, "unexpected isl_token");
650 bmap = isl_basic_map_simplify(bmap);
651 bmap = isl_basic_map_finalize(bmap);
652 return isl_map_from_basic_map(bmap);
654 isl_basic_map_free(bmap);
660 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
663 struct isl_basic_map *bmap;
665 map = map_read(s, nparam);
669 isl_assert(map->ctx, map->n <= 1, goto error);
672 bmap = isl_basic_map_empty_like_map(map);
674 bmap = isl_basic_map_copy(map->p[0]);
684 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
685 FILE *input, int nparam)
687 struct isl_basic_map *bmap;
688 struct isl_stream *s = isl_stream_new_file(ctx, input);
691 bmap = basic_map_read(s, nparam);
696 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
697 FILE *input, int nparam)
699 struct isl_basic_map *bmap;
700 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
703 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
704 return (struct isl_basic_set *)bmap;
706 isl_basic_map_free(bmap);
710 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
711 const char *str, int nparam)
713 struct isl_basic_map *bmap;
714 struct isl_stream *s = isl_stream_new_str(ctx, str);
717 bmap = basic_map_read(s, nparam);
722 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
723 const char *str, int nparam)
725 struct isl_basic_map *bmap;
726 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
729 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
730 return (struct isl_basic_set *)bmap;
732 isl_basic_map_free(bmap);
736 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
737 FILE *input, int nparam)
740 struct isl_stream *s = isl_stream_new_file(ctx, input);
743 map = map_read(s, nparam);
748 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
749 FILE *input, int nparam)
752 map = isl_map_read_from_file(ctx, input, nparam);
755 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
756 return (struct isl_set *)map;
762 static char *next_line(FILE *input, char *line, unsigned len)
767 if (!(p = fgets(line, len, input)))
769 while (isspace(*p) && *p != '\n')
771 } while (*p == '#' || *p == '\n');
776 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
779 struct isl_vec *vec = NULL;
788 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
789 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
791 vec = isl_vec_alloc(ctx, size);
793 p = next_line(input, line, sizeof(line));
794 isl_assert(ctx, p, goto error);
796 for (j = 0; j < size; ++j) {
797 n = sscanf(p, "%s%n", val, &offset);
798 isl_assert(ctx, n != 0, goto error);
799 isl_int_read(vec->el[j], val);
809 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
810 FILE *input, unsigned input_format)
812 if (input_format == ISL_FORMAT_POLYLIB)
813 return isl_vec_read_from_file_polylib(ctx, input);
815 isl_assert(ctx, 0, return NULL);