add isl_union_map_uncurry
[isl.git] / isl_input.c
blob3450d8741d82b3956ef15cf2965218f2ff63bc3b
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl/set.h>
21 #include <isl/seq.h>
22 #include <isl_stream_private.h>
23 #include <isl/obj.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
28 #include <isl/list.h>
30 struct variable {
31 char *name;
32 int pos;
33 struct variable *next;
36 struct vars {
37 struct isl_ctx *ctx;
38 int n;
39 struct variable *v;
42 static struct vars *vars_new(struct isl_ctx *ctx)
44 struct vars *v;
45 v = isl_alloc_type(ctx, struct vars);
46 if (!v)
47 return NULL;
48 v->ctx = ctx;
49 v->n = 0;
50 v->v = NULL;
51 return v;
54 static void variable_free(struct variable *var)
56 while (var) {
57 struct variable *next = var->next;
58 free(var->name);
59 free(var);
60 var = next;
64 static void vars_free(struct vars *v)
66 if (!v)
67 return;
68 variable_free(v->v);
69 free(v);
72 static void vars_drop(struct vars *v, int n)
74 struct variable *var;
76 if (!v || !v->v)
77 return;
79 v->n -= n;
81 var = v->v;
82 while (--n >= 0) {
83 struct variable *next = var->next;
84 free(var->name);
85 free(var);
86 var = next;
88 v->v = var;
91 static struct variable *variable_new(struct vars *v, const char *name, int len,
92 int pos)
94 struct variable *var;
95 var = isl_calloc_type(v->ctx, struct variable);
96 if (!var)
97 goto error;
98 var->name = strdup(name);
99 var->name[len] = '\0';
100 var->pos = pos;
101 var->next = v->v;
102 return var;
103 error:
104 variable_free(v->v);
105 return NULL;
108 static int vars_pos(struct vars *v, const char *s, int len)
110 int pos;
111 struct variable *q;
113 if (len == -1)
114 len = strlen(s);
115 for (q = v->v; q; q = q->next) {
116 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
117 break;
119 if (q)
120 pos = q->pos;
121 else {
122 pos = v->n;
123 v->v = variable_new(v, s, len, v->n);
124 if (!v->v)
125 return -1;
126 v->n++;
128 return pos;
131 static int vars_add_anon(struct vars *v)
133 v->v = variable_new(v, "", 0, v->n);
135 if (!v->v)
136 return -1;
137 v->n++;
139 return 0;
142 /* Obtain next token, with some preprocessing.
143 * In particular, evaluate expressions of the form x^y,
144 * with x and y values.
146 static struct isl_token *next_token(struct isl_stream *s)
148 struct isl_token *tok, *tok2;
150 tok = isl_stream_next_token(s);
151 if (!tok || tok->type != ISL_TOKEN_VALUE)
152 return tok;
153 if (!isl_stream_eat_if_available(s, '^'))
154 return tok;
155 tok2 = isl_stream_next_token(s);
156 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
157 isl_stream_error(s, tok2, "expecting constant value");
158 goto error;
161 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
163 isl_token_free(tok2);
164 return tok;
165 error:
166 isl_token_free(tok);
167 isl_token_free(tok2);
168 return NULL;
171 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
173 struct isl_token *tok;
175 tok = next_token(s);
176 if (!tok || tok->type != ISL_TOKEN_VALUE) {
177 isl_stream_error(s, tok, "expecting constant value");
178 goto error;
181 isl_int_mul(*f, *f, tok->u.v);
183 isl_token_free(tok);
185 if (isl_stream_eat_if_available(s, '*'))
186 return accept_cst_factor(s, f);
188 return 0;
189 error:
190 isl_token_free(tok);
191 return -1;
194 /* Given an affine expression aff, return an affine expression
195 * for aff % d, with d the next token on the stream, which is
196 * assumed to be a constant.
198 * We introduce an integer division q = [aff/d] and the result
199 * is set to aff - d q.
201 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
202 struct vars *v, __isl_take isl_pw_aff *aff)
204 struct isl_token *tok;
205 isl_pw_aff *q;
207 tok = next_token(s);
208 if (!tok || tok->type != ISL_TOKEN_VALUE) {
209 isl_stream_error(s, tok, "expecting constant value");
210 goto error;
213 q = isl_pw_aff_copy(aff);
214 q = isl_pw_aff_scale_down(q, tok->u.v);
215 q = isl_pw_aff_floor(q);
216 q = isl_pw_aff_scale(q, tok->u.v);
218 aff = isl_pw_aff_sub(aff, q);
220 isl_token_free(tok);
221 return aff;
222 error:
223 isl_pw_aff_free(aff);
224 isl_token_free(tok);
225 return NULL;
228 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
229 __isl_take isl_space *dim, struct vars *v);
230 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
231 __isl_take isl_space *dim, struct vars *v);
233 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
234 __isl_take isl_space *dim, struct vars *v)
236 struct isl_token *tok;
237 isl_pw_aff_list *list = NULL;
238 int min;
240 tok = isl_stream_next_token(s);
241 if (!tok)
242 goto error;
243 min = tok->type == ISL_TOKEN_MIN;
244 isl_token_free(tok);
246 if (isl_stream_eat(s, '('))
247 goto error;
249 list = accept_affine_list(s, isl_space_copy(dim), v);
250 if (!list)
251 goto error;
253 if (isl_stream_eat(s, ')'))
254 goto error;
256 isl_space_free(dim);
257 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
258 error:
259 isl_space_free(dim);
260 isl_pw_aff_list_free(list);
261 return NULL;
264 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
265 __isl_take isl_space *dim, struct vars *v)
267 struct isl_token *tok;
268 int f = 0;
269 int c = 0;
270 isl_pw_aff *pwaff = NULL;
272 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
273 f = 1;
274 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
275 c = 1;
276 if (f || c) {
277 if (isl_stream_eat(s, '('))
278 goto error;
279 } else {
280 if (isl_stream_eat(s, '['))
281 goto error;
284 pwaff = accept_affine(s, isl_space_copy(dim), v);
286 if (f || c) {
287 if (isl_stream_eat(s, ','))
288 goto error;
290 tok = next_token(s);
291 if (!tok)
292 goto error;
293 if (tok->type != ISL_TOKEN_VALUE) {
294 isl_stream_error(s, tok, "expected denominator");
295 isl_stream_push_token(s, tok);
296 goto error;
298 isl_pw_aff_scale_down(pwaff, tok->u.v);
299 isl_token_free(tok);
302 if (c)
303 pwaff = isl_pw_aff_ceil(pwaff);
304 else
305 pwaff = isl_pw_aff_floor(pwaff);
307 if (f || c) {
308 if (isl_stream_eat(s, ')'))
309 goto error;
310 } else {
311 if (isl_stream_eat(s, ']'))
312 goto error;
315 isl_space_free(dim);
316 return pwaff;
317 error:
318 isl_space_free(dim);
319 isl_pw_aff_free(pwaff);
320 return NULL;
323 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
324 __isl_take isl_space *dim, struct vars *v)
326 struct isl_token *tok = NULL;
327 isl_pw_aff *res = NULL;
329 tok = next_token(s);
330 if (!tok) {
331 isl_stream_error(s, NULL, "unexpected EOF");
332 goto error;
335 if (tok->type == ISL_TOKEN_AFF) {
336 res = isl_pw_aff_copy(tok->u.pwaff);
337 isl_token_free(tok);
338 } else if (tok->type == ISL_TOKEN_IDENT) {
339 int n = v->n;
340 int pos = vars_pos(v, tok->u.s, -1);
341 isl_aff *aff;
343 if (pos < 0)
344 goto error;
345 if (pos >= n) {
346 isl_stream_error(s, tok, "unknown identifier");
347 goto error;
350 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
351 if (!aff)
352 goto error;
353 isl_int_set_si(aff->v->el[2 + pos], 1);
354 res = isl_pw_aff_from_aff(aff);
355 isl_token_free(tok);
356 } else if (tok->type == ISL_TOKEN_VALUE) {
357 if (isl_stream_eat_if_available(s, '*')) {
358 res = accept_affine_factor(s, isl_space_copy(dim), v);
359 res = isl_pw_aff_scale(res, tok->u.v);
360 } else {
361 isl_local_space *ls;
362 isl_aff *aff;
363 ls = isl_local_space_from_space(isl_space_copy(dim));
364 aff = isl_aff_zero_on_domain(ls);
365 aff = isl_aff_add_constant(aff, tok->u.v);
366 res = isl_pw_aff_from_aff(aff);
368 isl_token_free(tok);
369 } else if (tok->type == '(') {
370 isl_token_free(tok);
371 tok = NULL;
372 res = accept_affine(s, isl_space_copy(dim), v);
373 if (!res)
374 goto error;
375 if (isl_stream_eat(s, ')'))
376 goto error;
377 } else if (tok->type == '[' ||
378 tok->type == ISL_TOKEN_FLOORD ||
379 tok->type == ISL_TOKEN_CEILD) {
380 isl_stream_push_token(s, tok);
381 tok = NULL;
382 res = accept_div(s, isl_space_copy(dim), v);
383 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
384 isl_stream_push_token(s, tok);
385 tok = NULL;
386 res = accept_minmax(s, isl_space_copy(dim), v);
387 } else {
388 isl_stream_error(s, tok, "expecting factor");
389 goto error;
391 if (isl_stream_eat_if_available(s, '%') ||
392 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
393 isl_space_free(dim);
394 return affine_mod(s, v, res);
396 if (isl_stream_eat_if_available(s, '*')) {
397 isl_int f;
398 isl_int_init(f);
399 isl_int_set_si(f, 1);
400 if (accept_cst_factor(s, &f) < 0) {
401 isl_int_clear(f);
402 goto error2;
404 res = isl_pw_aff_scale(res, f);
405 isl_int_clear(f);
407 if (isl_stream_eat_if_available(s, '/')) {
408 isl_int f;
409 isl_int_init(f);
410 isl_int_set_si(f, 1);
411 if (accept_cst_factor(s, &f) < 0) {
412 isl_int_clear(f);
413 goto error2;
415 res = isl_pw_aff_scale_down(res, f);
416 isl_int_clear(f);
419 isl_space_free(dim);
420 return res;
421 error:
422 isl_token_free(tok);
423 error2:
424 isl_pw_aff_free(res);
425 isl_space_free(dim);
426 return NULL;
429 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
431 isl_aff *aff;
432 isl_space *space;
434 space = isl_pw_aff_get_domain_space(pwaff);
435 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
436 aff = isl_aff_add_constant(aff, v);
438 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
441 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
442 __isl_take isl_space *dim, struct vars *v)
444 struct isl_token *tok = NULL;
445 isl_local_space *ls;
446 isl_pw_aff *res;
447 int sign = 1;
449 ls = isl_local_space_from_space(isl_space_copy(dim));
450 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
451 if (!res)
452 goto error;
454 for (;;) {
455 tok = next_token(s);
456 if (!tok) {
457 isl_stream_error(s, NULL, "unexpected EOF");
458 goto error;
460 if (tok->type == '-') {
461 sign = -sign;
462 isl_token_free(tok);
463 continue;
465 if (tok->type == '(' || tok->type == '[' ||
466 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
467 tok->type == ISL_TOKEN_FLOORD ||
468 tok->type == ISL_TOKEN_CEILD ||
469 tok->type == ISL_TOKEN_IDENT ||
470 tok->type == ISL_TOKEN_AFF) {
471 isl_pw_aff *term;
472 isl_stream_push_token(s, tok);
473 tok = NULL;
474 term = accept_affine_factor(s, isl_space_copy(dim), v);
475 if (sign < 0)
476 res = isl_pw_aff_sub(res, term);
477 else
478 res = isl_pw_aff_add(res, term);
479 if (!res)
480 goto error;
481 sign = 1;
482 } else if (tok->type == ISL_TOKEN_VALUE) {
483 if (sign < 0)
484 isl_int_neg(tok->u.v, tok->u.v);
485 if (isl_stream_eat_if_available(s, '*') ||
486 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
487 isl_pw_aff *term;
488 term = accept_affine_factor(s,
489 isl_space_copy(dim), v);
490 term = isl_pw_aff_scale(term, tok->u.v);
491 res = isl_pw_aff_add(res, term);
492 if (!res)
493 goto error;
494 } else {
495 res = add_cst(res, tok->u.v);
497 sign = 1;
498 } else {
499 isl_stream_error(s, tok, "unexpected isl_token");
500 isl_stream_push_token(s, tok);
501 isl_pw_aff_free(res);
502 isl_space_free(dim);
503 return NULL;
505 isl_token_free(tok);
507 tok = next_token(s);
508 if (tok && tok->type == '-') {
509 sign = -sign;
510 isl_token_free(tok);
511 } else if (tok && tok->type == '+') {
512 /* nothing */
513 isl_token_free(tok);
514 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
515 isl_int_is_neg(tok->u.v)) {
516 isl_stream_push_token(s, tok);
517 } else {
518 if (tok)
519 isl_stream_push_token(s, tok);
520 break;
524 isl_space_free(dim);
525 return res;
526 error:
527 isl_space_free(dim);
528 isl_token_free(tok);
529 isl_pw_aff_free(res);
530 return NULL;
533 static int is_comparator(struct isl_token *tok)
535 if (!tok)
536 return 0;
538 switch (tok->type) {
539 case ISL_TOKEN_LT:
540 case ISL_TOKEN_GT:
541 case ISL_TOKEN_LE:
542 case ISL_TOKEN_GE:
543 case ISL_TOKEN_NE:
544 case '=':
545 return 1;
546 default:
547 return 0;
551 static struct isl_map *read_disjuncts(struct isl_stream *s,
552 struct vars *v, __isl_take isl_map *map, int rational);
553 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
554 __isl_take isl_space *dim, struct vars *v, int rational);
556 /* Accept a ternary operator, given the first argument.
558 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
559 __isl_take isl_map *cond, struct vars *v, int rational)
561 isl_space *dim;
562 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
564 if (!cond)
565 return NULL;
567 if (isl_stream_eat(s, '?'))
568 goto error;
570 dim = isl_space_wrap(isl_map_get_space(cond));
571 pwaff1 = accept_extended_affine(s, dim, v, rational);
572 if (!pwaff1)
573 goto error;
575 if (isl_stream_eat(s, ':'))
576 goto error;
578 dim = isl_pw_aff_get_domain_space(pwaff1);
579 pwaff2 = accept_extended_affine(s, dim, v, rational);
580 if (!pwaff1)
581 goto error;
583 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
584 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
585 error:
586 isl_map_free(cond);
587 isl_pw_aff_free(pwaff1);
588 isl_pw_aff_free(pwaff2);
589 return NULL;
592 /* Accept an affine expression that may involve ternary operators.
593 * We first read an affine expression.
594 * If it is not followed by a comparison operator, we simply return it.
595 * Otherwise, we assume the affine epxression is part of the first
596 * argument of a ternary operator and try to parse that.
598 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
599 __isl_take isl_space *dim, struct vars *v, int rational)
601 isl_space *space;
602 isl_map *cond;
603 isl_pw_aff *pwaff;
604 struct isl_token *tok;
605 int line = -1, col = -1;
606 int is_comp;
608 tok = isl_stream_next_token(s);
609 if (tok) {
610 line = tok->line;
611 col = tok->col;
612 isl_stream_push_token(s, tok);
615 pwaff = accept_affine(s, dim, v);
616 if (rational)
617 pwaff = isl_pw_aff_set_rational(pwaff);
618 if (!pwaff)
619 return NULL;
621 tok = isl_stream_next_token(s);
622 if (!tok)
623 return isl_pw_aff_free(pwaff);
625 is_comp = is_comparator(tok);
626 isl_stream_push_token(s, tok);
627 if (!is_comp)
628 return pwaff;
630 tok = isl_token_new(s->ctx, line, col, 0);
631 if (!tok)
632 return isl_pw_aff_free(pwaff);
633 tok->type = ISL_TOKEN_AFF;
634 tok->u.pwaff = pwaff;
636 space = isl_pw_aff_get_domain_space(pwaff);
637 cond = isl_map_universe(isl_space_unwrap(space));
639 isl_stream_push_token(s, tok);
641 cond = read_disjuncts(s, v, cond, rational);
643 return accept_ternary(s, cond, v, rational);
646 static __isl_give isl_map *read_var_def(struct isl_stream *s,
647 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
648 int rational)
650 isl_pw_aff *def;
651 int pos;
652 isl_map *def_map;
654 if (type == isl_dim_param)
655 pos = isl_map_dim(map, isl_dim_param);
656 else {
657 pos = isl_map_dim(map, isl_dim_in);
658 if (type == isl_dim_out)
659 pos += isl_map_dim(map, isl_dim_out);
660 type = isl_dim_in;
662 --pos;
664 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
665 v, rational);
666 def_map = isl_map_from_pw_aff(def);
667 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
668 def_map = isl_set_unwrap(isl_map_domain(def_map));
670 map = isl_map_intersect(map, def_map);
672 return map;
675 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
676 __isl_take isl_space *dim, struct vars *v)
678 isl_pw_aff *pwaff;
679 isl_pw_aff_list *list;
680 struct isl_token *tok = NULL;
682 pwaff = accept_affine(s, isl_space_copy(dim), v);
683 list = isl_pw_aff_list_from_pw_aff(pwaff);
684 if (!list)
685 goto error;
687 for (;;) {
688 tok = isl_stream_next_token(s);
689 if (!tok) {
690 isl_stream_error(s, NULL, "unexpected EOF");
691 goto error;
693 if (tok->type != ',') {
694 isl_stream_push_token(s, tok);
695 break;
697 isl_token_free(tok);
699 pwaff = accept_affine(s, isl_space_copy(dim), v);
700 list = isl_pw_aff_list_concat(list,
701 isl_pw_aff_list_from_pw_aff(pwaff));
702 if (!list)
703 return NULL;
706 isl_space_free(dim);
707 return list;
708 error:
709 isl_space_free(dim);
710 isl_pw_aff_list_free(list);
711 return NULL;
714 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
715 struct vars *v, __isl_take isl_map *map, int rational)
717 struct isl_token *tok;
719 while ((tok = isl_stream_next_token(s)) != NULL) {
720 int p;
721 int n = v->n;
723 if (tok->type != ISL_TOKEN_IDENT)
724 break;
726 p = vars_pos(v, tok->u.s, -1);
727 if (p < 0)
728 goto error;
729 if (p < n) {
730 isl_stream_error(s, tok, "expecting unique identifier");
731 goto error;
734 map = isl_map_add_dims(map, isl_dim_out, 1);
736 isl_token_free(tok);
737 tok = isl_stream_next_token(s);
738 if (tok && tok->type == '=') {
739 isl_token_free(tok);
740 map = read_var_def(s, map, isl_dim_out, v, rational);
741 tok = isl_stream_next_token(s);
744 if (!tok || tok->type != ',')
745 break;
747 isl_token_free(tok);
749 if (tok)
750 isl_stream_push_token(s, tok);
752 return map;
753 error:
754 isl_token_free(tok);
755 isl_map_free(map);
756 return NULL;
759 static int next_is_tuple(struct isl_stream *s)
761 struct isl_token *tok;
762 int is_tuple;
764 tok = isl_stream_next_token(s);
765 if (!tok)
766 return 0;
767 if (tok->type == '[') {
768 isl_stream_push_token(s, tok);
769 return 1;
771 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
772 isl_stream_push_token(s, tok);
773 return 0;
776 is_tuple = isl_stream_next_token_is(s, '[');
778 isl_stream_push_token(s, tok);
780 return is_tuple;
783 /* Allocate an initial tuple with zero dimensions and an anonymous,
784 * unstructured space.
785 * A tuple is represented as an isl_multi_pw_aff.
786 * The range space is the space of the tuple.
787 * The domain space is an anonymous space
788 * with a dimension for each variable in the set of variables in "v".
789 * If a given dimension is not defined in terms of earlier dimensions in
790 * the input, then the corresponding isl_pw_aff is set equal to one time
791 * the variable corresponding to the dimension being defined.
793 static __isl_give isl_multi_pw_aff *tuple_alloc(struct vars *v)
795 return isl_multi_pw_aff_alloc(isl_space_alloc(v->ctx, 0, v->n, 0));
798 /* Is "pa" an expression in term of earlier dimensions?
799 * The alternative is that the dimension is defined to be equal to itself,
800 * meaning that it has a universe domain and an expression that depends
801 * on itself. "i" is the position of the expression in a sequence
802 * of "n" expressions. The final dimensions of "pa" correspond to
803 * these "n" expressions.
805 static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
807 isl_aff *aff;
809 if (!pa)
810 return -1;
811 if (pa->n != 1)
812 return 1;
813 if (!isl_set_plain_is_universe(pa->p[0].set))
814 return 1;
816 aff = pa->p[0].aff;
817 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
818 return 1;
819 return 0;
822 /* Does the tuple contain any dimensions that are defined
823 * in terms of earlier dimensions?
825 static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
827 int i, n;
828 int has_expr = 0;
829 isl_pw_aff *pa;
831 if (!tuple)
832 return -1;
833 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
834 for (i = 0; i < n; ++i) {
835 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
836 has_expr = pw_aff_is_expr(pa, i, n);
837 isl_pw_aff_free(pa);
838 if (has_expr < 0 || has_expr)
839 break;
842 return has_expr;
845 /* Add a dimension to the given tuple.
846 * The dimension is initially undefined, so it is encoded
847 * as one times itself.
849 static __isl_give isl_multi_pw_aff *tuple_add_dim(
850 __isl_take isl_multi_pw_aff *tuple, struct vars *v)
852 isl_space *space;
853 isl_aff *aff;
854 isl_pw_aff *pa;
856 tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
857 space = isl_multi_pw_aff_get_domain_space(tuple);
858 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
859 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
860 pa = isl_pw_aff_from_aff(aff);
861 tuple = isl_multi_pw_aff_flat_range_product(tuple,
862 isl_multi_pw_aff_from_pw_aff(pa));
864 return tuple;
867 /* Set the name of dimension "pos" in "tuple" to "name".
868 * During printing, we add primes if the same name appears more than once
869 * to distinguish the occurrences. Here, we remove those primes from "name"
870 * before setting the name of the dimension.
872 static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
873 __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
875 char *prime;
877 if (!name)
878 return tuple;
880 prime = strchr(name, '\'');
881 if (prime)
882 *prime = '\0';
883 tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
884 if (prime)
885 *prime = '\'';
887 return tuple;
890 /* Read an affine expression from "s" and replace the definition
891 * of dimension "pos" in "tuple" by this expression.
893 * accept_extended_affine requires a wrapped space as input.
894 * The domain space of "tuple", on the other hand is an anonymous space,
895 * so we have to adjust the space of the isl_pw_aff before adding it
896 * to "tuple".
898 static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
899 __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
900 int rational)
902 isl_space *space;
903 isl_pw_aff *def;
905 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
906 def = accept_extended_affine(s, space, v, rational);
907 space = isl_space_set_alloc(s->ctx, 0, v->n);
908 def = isl_pw_aff_reset_domain_space(def, space);
909 tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
911 return tuple;
914 /* Read a list of variables and/or affine expressions and return the list
915 * as an isl_multi_pw_aff.
916 * The elements in the list are separated by either "," or "][".
917 * If "comma" is set then only "," is allowed.
919 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
920 struct vars *v, int rational, int comma)
922 int i = 0;
923 struct isl_token *tok;
924 isl_multi_pw_aff *res;
926 res = tuple_alloc(v);
928 if (isl_stream_next_token_is(s, ']'))
929 return res;
931 while ((tok = next_token(s)) != NULL) {
932 int new_name = 0;
934 res = tuple_add_dim(res, v);
936 if (tok->type == ISL_TOKEN_IDENT) {
937 int n = v->n;
938 int p = vars_pos(v, tok->u.s, -1);
939 if (p < 0)
940 goto error;
941 new_name = p >= n;
944 if (new_name) {
945 res = tuple_set_dim_name(res, i, v->v->name);
946 isl_token_free(tok);
947 if (isl_stream_eat_if_available(s, '='))
948 res = read_tuple_var_def(s, res, i, v,
949 rational);
950 } else {
951 isl_stream_push_token(s, tok);
952 tok = NULL;
953 if (vars_add_anon(v) < 0)
954 goto error;
955 res = read_tuple_var_def(s, res, i, v, rational);
958 tok = isl_stream_next_token(s);
959 if (!comma && tok && tok->type == ']' &&
960 isl_stream_next_token_is(s, '[')) {
961 isl_token_free(tok);
962 tok = isl_stream_next_token(s);
963 } else if (!tok || tok->type != ',')
964 break;
966 isl_token_free(tok);
967 i++;
969 if (tok)
970 isl_stream_push_token(s, tok);
972 return res;
973 error:
974 isl_token_free(tok);
975 return isl_multi_pw_aff_free(res);
978 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
980 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
981 struct vars *v, int rational, int comma)
983 struct isl_token *tok;
984 char *name = NULL;
985 isl_multi_pw_aff *res = NULL;
987 tok = isl_stream_next_token(s);
988 if (!tok)
989 goto error;
990 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
991 name = strdup(tok->u.s);
992 isl_token_free(tok);
993 if (!name)
994 goto error;
995 } else
996 isl_stream_push_token(s, tok);
997 if (isl_stream_eat(s, '['))
998 goto error;
999 if (next_is_tuple(s)) {
1000 isl_multi_pw_aff *out;
1001 int n;
1002 res = read_tuple(s, v, rational, comma);
1003 if (isl_stream_eat(s, ISL_TOKEN_TO))
1004 goto error;
1005 out = read_tuple(s, v, rational, comma);
1006 n = isl_multi_pw_aff_dim(out, isl_dim_out);
1007 res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
1008 res = isl_multi_pw_aff_range_product(res, out);
1009 } else
1010 res = read_tuple_var_list(s, v, rational, comma);
1011 if (isl_stream_eat(s, ']'))
1012 goto error;
1014 if (name) {
1015 res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
1016 free(name);
1019 return res;
1020 error:
1021 free(name);
1022 return isl_multi_pw_aff_free(res);
1025 /* Read a tuple from "s" and add it to "map".
1026 * The tuple is initially represented as an isl_multi_pw_aff.
1027 * We first create the appropriate space in "map" based on the range
1028 * space of this isl_multi_pw_aff. Then, we add equalities based
1029 * on the affine expressions. These live in an anonymous space,
1030 * however, so we first need to reset the space to that of "map".
1032 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
1033 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1034 int rational, int comma)
1036 int i, n;
1037 isl_multi_pw_aff *tuple;
1038 isl_space *space = NULL;
1040 tuple = read_tuple(s, v, rational, comma);
1041 if (!tuple)
1042 goto error;
1044 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1045 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1047 if (type == isl_dim_param) {
1048 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1049 isl_space_is_wrapping(space)) {
1050 isl_die(s->ctx, isl_error_invalid,
1051 "parameter tuples cannot be named or nested",
1052 goto error);
1054 map = isl_map_add_dims(map, type, n);
1055 for (i = 0; i < n; ++i) {
1056 isl_id *id;
1057 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1058 isl_die(s->ctx, isl_error_invalid,
1059 "parameters must be named",
1060 goto error);
1061 id = isl_space_get_dim_id(space, isl_dim_set, i);
1062 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1064 } else if (type == isl_dim_in) {
1065 isl_set *set;
1067 set = isl_set_universe(isl_space_copy(space));
1068 if (rational)
1069 set = isl_set_set_rational(set);
1070 set = isl_set_intersect_params(set, isl_map_params(map));
1071 map = isl_map_from_domain(set);
1072 } else {
1073 isl_set *set;
1075 set = isl_set_universe(isl_space_copy(space));
1076 if (rational)
1077 set = isl_set_set_rational(set);
1078 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1081 for (i = 0; i < n; ++i) {
1082 isl_pw_aff *pa;
1083 isl_space *space;
1084 isl_aff *aff;
1085 isl_set *set;
1086 isl_map *map_i;
1088 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1089 space = isl_pw_aff_get_domain_space(pa);
1090 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1091 aff = isl_aff_add_coefficient_si(aff,
1092 isl_dim_in, v->n - n + i, -1);
1093 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1094 if (rational)
1095 pa = isl_pw_aff_set_rational(pa);
1096 set = isl_pw_aff_zero_set(pa);
1097 map_i = isl_map_from_range(set);
1098 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1099 map = isl_map_intersect(map, map_i);
1102 isl_space_free(space);
1103 isl_multi_pw_aff_free(tuple);
1104 return map;
1105 error:
1106 isl_space_free(space);
1107 isl_multi_pw_aff_free(tuple);
1108 isl_map_free(map);
1109 return NULL;
1112 static __isl_give isl_set *construct_constraints(
1113 __isl_take isl_set *set, int type,
1114 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1115 int rational)
1117 isl_set *cond;
1119 if (rational) {
1120 left = isl_pw_aff_list_set_rational(left);
1121 right = isl_pw_aff_list_set_rational(right);
1123 if (type == ISL_TOKEN_LE)
1124 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
1125 isl_pw_aff_list_copy(right));
1126 else if (type == ISL_TOKEN_GE)
1127 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
1128 isl_pw_aff_list_copy(right));
1129 else if (type == ISL_TOKEN_LT)
1130 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
1131 isl_pw_aff_list_copy(right));
1132 else if (type == ISL_TOKEN_GT)
1133 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
1134 isl_pw_aff_list_copy(right));
1135 else if (type == ISL_TOKEN_NE)
1136 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
1137 isl_pw_aff_list_copy(right));
1138 else
1139 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
1140 isl_pw_aff_list_copy(right));
1142 return isl_set_intersect(set, cond);
1145 static __isl_give isl_map *add_constraint(struct isl_stream *s,
1146 struct vars *v, __isl_take isl_map *map, int rational)
1148 struct isl_token *tok = NULL;
1149 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1150 isl_set *set;
1152 set = isl_map_wrap(map);
1153 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1154 if (!list1)
1155 goto error;
1156 tok = isl_stream_next_token(s);
1157 if (!is_comparator(tok)) {
1158 isl_stream_error(s, tok, "missing operator");
1159 if (tok)
1160 isl_stream_push_token(s, tok);
1161 tok = NULL;
1162 goto error;
1164 for (;;) {
1165 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1166 if (!list2)
1167 goto error;
1169 set = construct_constraints(set, tok->type, list1, list2,
1170 rational);
1171 isl_token_free(tok);
1172 isl_pw_aff_list_free(list1);
1173 list1 = list2;
1175 tok = isl_stream_next_token(s);
1176 if (!is_comparator(tok)) {
1177 if (tok)
1178 isl_stream_push_token(s, tok);
1179 break;
1182 isl_pw_aff_list_free(list1);
1184 return isl_set_unwrap(set);
1185 error:
1186 if (tok)
1187 isl_token_free(tok);
1188 isl_pw_aff_list_free(list1);
1189 isl_pw_aff_list_free(list2);
1190 isl_set_free(set);
1191 return NULL;
1194 static __isl_give isl_map *read_exists(struct isl_stream *s,
1195 struct vars *v, __isl_take isl_map *map, int rational)
1197 int n = v->n;
1198 int seen_paren = isl_stream_eat_if_available(s, '(');
1200 map = isl_map_from_domain(isl_map_wrap(map));
1201 map = read_defined_var_list(s, v, map, rational);
1203 if (isl_stream_eat(s, ':'))
1204 goto error;
1206 map = read_disjuncts(s, v, map, rational);
1207 map = isl_set_unwrap(isl_map_domain(map));
1209 vars_drop(v, v->n - n);
1210 if (seen_paren && isl_stream_eat(s, ')'))
1211 goto error;
1213 return map;
1214 error:
1215 isl_map_free(map);
1216 return NULL;
1219 /* Parse an expression between parentheses and push the result
1220 * back on the stream.
1222 * The parsed expression may be either an affine expression
1223 * or a condition. The first type is pushed onto the stream
1224 * as an isl_pw_aff, while the second is pushed as an isl_map.
1226 * If the initial token indicates the start of a condition,
1227 * we parse it as such.
1228 * Otherwise, we first parse an affine expression and push
1229 * that onto the stream. If the affine expression covers the
1230 * entire expression between parentheses, we return.
1231 * Otherwise, we assume that the affine expression is the
1232 * start of a condition and continue parsing.
1234 static int resolve_paren_expr(struct isl_stream *s,
1235 struct vars *v, __isl_take isl_map *map, int rational)
1237 struct isl_token *tok, *tok2;
1238 int line, col;
1239 isl_pw_aff *pwaff;
1241 tok = isl_stream_next_token(s);
1242 if (!tok || tok->type != '(')
1243 goto error;
1245 if (isl_stream_next_token_is(s, '('))
1246 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1247 goto error;
1249 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1250 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1251 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1252 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1253 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1254 map = read_disjuncts(s, v, map, rational);
1255 if (isl_stream_eat(s, ')'))
1256 goto error;
1257 tok->type = ISL_TOKEN_MAP;
1258 tok->u.map = map;
1259 isl_stream_push_token(s, tok);
1260 return 0;
1263 tok2 = isl_stream_next_token(s);
1264 if (!tok2)
1265 goto error;
1266 line = tok2->line;
1267 col = tok2->col;
1268 isl_stream_push_token(s, tok2);
1270 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1271 if (!pwaff)
1272 goto error;
1274 tok2 = isl_token_new(s->ctx, line, col, 0);
1275 if (!tok2)
1276 goto error2;
1277 tok2->type = ISL_TOKEN_AFF;
1278 tok2->u.pwaff = pwaff;
1280 if (isl_stream_eat_if_available(s, ')')) {
1281 isl_stream_push_token(s, tok2);
1282 isl_token_free(tok);
1283 isl_map_free(map);
1284 return 0;
1287 isl_stream_push_token(s, tok2);
1289 map = read_disjuncts(s, v, map, rational);
1290 if (isl_stream_eat(s, ')'))
1291 goto error;
1293 tok->type = ISL_TOKEN_MAP;
1294 tok->u.map = map;
1295 isl_stream_push_token(s, tok);
1297 return 0;
1298 error2:
1299 isl_pw_aff_free(pwaff);
1300 error:
1301 isl_token_free(tok);
1302 isl_map_free(map);
1303 return -1;
1306 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1307 struct vars *v, __isl_take isl_map *map, int rational)
1309 if (isl_stream_next_token_is(s, '('))
1310 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1311 goto error;
1313 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1314 struct isl_token *tok;
1315 tok = isl_stream_next_token(s);
1316 if (!tok)
1317 goto error;
1318 isl_map_free(map);
1319 map = isl_map_copy(tok->u.map);
1320 isl_token_free(tok);
1321 return map;
1324 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1325 return read_exists(s, v, map, rational);
1327 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1328 return map;
1330 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1331 isl_space *dim = isl_map_get_space(map);
1332 isl_map_free(map);
1333 return isl_map_empty(dim);
1336 return add_constraint(s, v, map, rational);
1337 error:
1338 isl_map_free(map);
1339 return NULL;
1342 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1343 struct vars *v, __isl_take isl_map *map, int rational)
1345 isl_map *res;
1346 int negate;
1348 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1349 res = read_conjunct(s, v, isl_map_copy(map), rational);
1350 if (negate)
1351 res = isl_map_subtract(isl_map_copy(map), res);
1353 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1354 isl_map *res_i;
1356 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1357 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1358 if (negate)
1359 res = isl_map_subtract(res, res_i);
1360 else
1361 res = isl_map_intersect(res, res_i);
1364 isl_map_free(map);
1365 return res;
1368 static struct isl_map *read_disjuncts(struct isl_stream *s,
1369 struct vars *v, __isl_take isl_map *map, int rational)
1371 isl_map *res;
1373 if (isl_stream_next_token_is(s, '}')) {
1374 isl_space *dim = isl_map_get_space(map);
1375 isl_map_free(map);
1376 return isl_map_universe(dim);
1379 res = read_conjuncts(s, v, isl_map_copy(map), rational);
1380 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1381 isl_map *res_i;
1383 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1384 res = isl_map_union(res, res_i);
1387 isl_map_free(map);
1388 return res;
1391 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1393 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1394 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1395 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1396 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1398 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1399 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1400 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1402 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1403 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1404 isl_basic_map_dim(bmap, isl_dim_in) +
1405 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1406 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1408 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1409 return 1 + pos;
1411 return 0;
1414 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1415 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1417 int j;
1418 struct isl_token *tok;
1419 int type;
1420 int k;
1421 isl_int *c;
1422 unsigned nparam;
1423 unsigned dim;
1425 if (!bmap)
1426 return NULL;
1428 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1429 dim = isl_basic_map_dim(bmap, isl_dim_out);
1431 tok = isl_stream_next_token(s);
1432 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1433 isl_stream_error(s, tok, "expecting coefficient");
1434 if (tok)
1435 isl_stream_push_token(s, tok);
1436 goto error;
1438 if (!tok->on_new_line) {
1439 isl_stream_error(s, tok, "coefficient should appear on new line");
1440 isl_stream_push_token(s, tok);
1441 goto error;
1444 type = isl_int_get_si(tok->u.v);
1445 isl_token_free(tok);
1447 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1448 if (type == 0) {
1449 k = isl_basic_map_alloc_equality(bmap);
1450 c = bmap->eq[k];
1451 } else {
1452 k = isl_basic_map_alloc_inequality(bmap);
1453 c = bmap->ineq[k];
1455 if (k < 0)
1456 goto error;
1458 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1459 int pos;
1460 tok = isl_stream_next_token(s);
1461 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1462 isl_stream_error(s, tok, "expecting coefficient");
1463 if (tok)
1464 isl_stream_push_token(s, tok);
1465 goto error;
1467 if (tok->on_new_line) {
1468 isl_stream_error(s, tok,
1469 "coefficient should not appear on new line");
1470 isl_stream_push_token(s, tok);
1471 goto error;
1473 pos = polylib_pos_to_isl_pos(bmap, j);
1474 isl_int_set(c[pos], tok->u.v);
1475 isl_token_free(tok);
1478 return bmap;
1479 error:
1480 isl_basic_map_free(bmap);
1481 return NULL;
1484 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
1486 int i;
1487 struct isl_token *tok;
1488 struct isl_token *tok2;
1489 int n_row, n_col;
1490 int on_new_line;
1491 unsigned in = 0, out, local = 0;
1492 struct isl_basic_map *bmap = NULL;
1493 int nparam = 0;
1495 tok = isl_stream_next_token(s);
1496 if (!tok) {
1497 isl_stream_error(s, NULL, "unexpected EOF");
1498 return NULL;
1500 tok2 = isl_stream_next_token(s);
1501 if (!tok2) {
1502 isl_token_free(tok);
1503 isl_stream_error(s, NULL, "unexpected EOF");
1504 return NULL;
1506 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1507 isl_stream_push_token(s, tok2);
1508 isl_stream_push_token(s, tok);
1509 isl_stream_error(s, NULL,
1510 "expecting constraint matrix dimensions");
1511 return NULL;
1513 n_row = isl_int_get_si(tok->u.v);
1514 n_col = isl_int_get_si(tok2->u.v);
1515 on_new_line = tok2->on_new_line;
1516 isl_token_free(tok2);
1517 isl_token_free(tok);
1518 isl_assert(s->ctx, !on_new_line, return NULL);
1519 isl_assert(s->ctx, n_row >= 0, return NULL);
1520 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1521 tok = isl_stream_next_token_on_same_line(s);
1522 if (tok) {
1523 if (tok->type != ISL_TOKEN_VALUE) {
1524 isl_stream_error(s, tok,
1525 "expecting number of output dimensions");
1526 isl_stream_push_token(s, tok);
1527 goto error;
1529 out = isl_int_get_si(tok->u.v);
1530 isl_token_free(tok);
1532 tok = isl_stream_next_token_on_same_line(s);
1533 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1534 isl_stream_error(s, tok,
1535 "expecting number of input dimensions");
1536 if (tok)
1537 isl_stream_push_token(s, tok);
1538 goto error;
1540 in = isl_int_get_si(tok->u.v);
1541 isl_token_free(tok);
1543 tok = isl_stream_next_token_on_same_line(s);
1544 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1545 isl_stream_error(s, tok,
1546 "expecting number of existentials");
1547 if (tok)
1548 isl_stream_push_token(s, tok);
1549 goto error;
1551 local = isl_int_get_si(tok->u.v);
1552 isl_token_free(tok);
1554 tok = isl_stream_next_token_on_same_line(s);
1555 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1556 isl_stream_error(s, tok,
1557 "expecting number of parameters");
1558 if (tok)
1559 isl_stream_push_token(s, tok);
1560 goto error;
1562 nparam = isl_int_get_si(tok->u.v);
1563 isl_token_free(tok);
1564 if (n_col != 1 + out + in + local + nparam + 1) {
1565 isl_stream_error(s, NULL,
1566 "dimensions don't match");
1567 goto error;
1569 } else
1570 out = n_col - 2 - nparam;
1571 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1572 if (!bmap)
1573 return NULL;
1575 for (i = 0; i < local; ++i) {
1576 int k = isl_basic_map_alloc_div(bmap);
1577 if (k < 0)
1578 goto error;
1579 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1582 for (i = 0; i < n_row; ++i)
1583 bmap = basic_map_read_polylib_constraint(s, bmap);
1585 tok = isl_stream_next_token_on_same_line(s);
1586 if (tok) {
1587 isl_stream_error(s, tok, "unexpected extra token on line");
1588 isl_stream_push_token(s, tok);
1589 goto error;
1592 bmap = isl_basic_map_simplify(bmap);
1593 bmap = isl_basic_map_finalize(bmap);
1594 return bmap;
1595 error:
1596 isl_basic_map_free(bmap);
1597 return NULL;
1600 static struct isl_map *map_read_polylib(struct isl_stream *s)
1602 struct isl_token *tok;
1603 struct isl_token *tok2;
1604 int i, n;
1605 struct isl_map *map;
1607 tok = isl_stream_next_token(s);
1608 if (!tok) {
1609 isl_stream_error(s, NULL, "unexpected EOF");
1610 return NULL;
1612 tok2 = isl_stream_next_token_on_same_line(s);
1613 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1614 isl_stream_push_token(s, tok2);
1615 isl_stream_push_token(s, tok);
1616 return isl_map_from_basic_map(basic_map_read_polylib(s));
1618 if (tok2) {
1619 isl_stream_error(s, tok2, "unexpected token");
1620 isl_stream_push_token(s, tok2);
1621 isl_stream_push_token(s, tok);
1622 return NULL;
1624 n = isl_int_get_si(tok->u.v);
1625 isl_token_free(tok);
1627 isl_assert(s->ctx, n >= 1, return NULL);
1629 map = isl_map_from_basic_map(basic_map_read_polylib(s));
1631 for (i = 1; map && i < n; ++i)
1632 map = isl_map_union(map,
1633 isl_map_from_basic_map(basic_map_read_polylib(s)));
1635 return map;
1638 static int optional_power(struct isl_stream *s)
1640 int pow;
1641 struct isl_token *tok;
1643 tok = isl_stream_next_token(s);
1644 if (!tok)
1645 return 1;
1646 if (tok->type != '^') {
1647 isl_stream_push_token(s, tok);
1648 return 1;
1650 isl_token_free(tok);
1651 tok = isl_stream_next_token(s);
1652 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1653 isl_stream_error(s, tok, "expecting exponent");
1654 if (tok)
1655 isl_stream_push_token(s, tok);
1656 return 1;
1658 pow = isl_int_get_si(tok->u.v);
1659 isl_token_free(tok);
1660 return pow;
1663 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1664 __isl_keep isl_map *map, struct vars *v);
1666 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1667 __isl_keep isl_map *map, struct vars *v)
1669 isl_pw_qpolynomial *pwqp;
1670 struct isl_token *tok;
1672 tok = next_token(s);
1673 if (!tok) {
1674 isl_stream_error(s, NULL, "unexpected EOF");
1675 return NULL;
1677 if (tok->type == '(') {
1678 int pow;
1680 isl_token_free(tok);
1681 pwqp = read_term(s, map, v);
1682 if (!pwqp)
1683 return NULL;
1684 if (isl_stream_eat(s, ')'))
1685 goto error;
1686 pow = optional_power(s);
1687 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1688 } else if (tok->type == ISL_TOKEN_VALUE) {
1689 struct isl_token *tok2;
1690 tok2 = isl_stream_next_token(s);
1691 isl_qpolynomial *qp;
1692 if (tok2 && tok2->type == '/') {
1693 isl_token_free(tok2);
1694 tok2 = next_token(s);
1695 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1696 isl_stream_error(s, tok2, "expected denominator");
1697 isl_token_free(tok);
1698 isl_token_free(tok2);
1699 return NULL;
1701 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1702 tok->u.v, tok2->u.v);
1703 isl_token_free(tok2);
1704 } else {
1705 isl_stream_push_token(s, tok2);
1706 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1707 tok->u.v);
1709 isl_token_free(tok);
1710 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1711 } else if (tok->type == ISL_TOKEN_INFTY) {
1712 isl_qpolynomial *qp;
1713 isl_token_free(tok);
1714 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1715 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1716 } else if (tok->type == ISL_TOKEN_NAN) {
1717 isl_qpolynomial *qp;
1718 isl_token_free(tok);
1719 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1720 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1721 } else if (tok->type == ISL_TOKEN_IDENT) {
1722 int n = v->n;
1723 int pos = vars_pos(v, tok->u.s, -1);
1724 int pow;
1725 isl_qpolynomial *qp;
1726 if (pos < 0) {
1727 isl_token_free(tok);
1728 return NULL;
1730 if (pos >= n) {
1731 vars_drop(v, v->n - n);
1732 isl_stream_error(s, tok, "unknown identifier");
1733 isl_token_free(tok);
1734 return NULL;
1736 isl_token_free(tok);
1737 pow = optional_power(s);
1738 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1739 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1740 } else if (tok->type == '[') {
1741 isl_pw_aff *pwaff;
1742 int pow;
1744 isl_stream_push_token(s, tok);
1745 pwaff = accept_div(s, isl_map_get_space(map), v);
1746 pow = optional_power(s);
1747 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1748 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1749 } else if (tok->type == '-') {
1750 isl_token_free(tok);
1751 pwqp = read_factor(s, map, v);
1752 pwqp = isl_pw_qpolynomial_neg(pwqp);
1753 } else {
1754 isl_stream_error(s, tok, "unexpected isl_token");
1755 isl_stream_push_token(s, tok);
1756 return NULL;
1759 if (isl_stream_eat_if_available(s, '*') ||
1760 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1761 isl_pw_qpolynomial *pwqp2;
1763 pwqp2 = read_factor(s, map, v);
1764 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1767 return pwqp;
1768 error:
1769 isl_pw_qpolynomial_free(pwqp);
1770 return NULL;
1773 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1774 __isl_keep isl_map *map, struct vars *v)
1776 struct isl_token *tok;
1777 isl_pw_qpolynomial *pwqp;
1779 pwqp = read_factor(s, map, v);
1781 for (;;) {
1782 tok = next_token(s);
1783 if (!tok)
1784 return pwqp;
1786 if (tok->type == '+') {
1787 isl_pw_qpolynomial *pwqp2;
1789 isl_token_free(tok);
1790 pwqp2 = read_factor(s, map, v);
1791 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1792 } else if (tok->type == '-') {
1793 isl_pw_qpolynomial *pwqp2;
1795 isl_token_free(tok);
1796 pwqp2 = read_factor(s, map, v);
1797 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1798 } else if (tok->type == ISL_TOKEN_VALUE &&
1799 isl_int_is_neg(tok->u.v)) {
1800 isl_pw_qpolynomial *pwqp2;
1802 isl_stream_push_token(s, tok);
1803 pwqp2 = read_factor(s, map, v);
1804 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1805 } else {
1806 isl_stream_push_token(s, tok);
1807 break;
1811 return pwqp;
1814 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1815 __isl_take isl_map *map, struct vars *v, int rational)
1817 struct isl_token *tok;
1819 tok = isl_stream_next_token(s);
1820 if (!tok) {
1821 isl_stream_error(s, NULL, "unexpected EOF");
1822 goto error;
1824 if (tok->type == ':' ||
1825 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1826 isl_token_free(tok);
1827 map = read_disjuncts(s, v, map, rational);
1828 } else
1829 isl_stream_push_token(s, tok);
1831 return map;
1832 error:
1833 isl_map_free(map);
1834 return NULL;
1837 static struct isl_obj obj_read_poly(struct isl_stream *s,
1838 __isl_take isl_map *map, struct vars *v, int n)
1840 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1841 isl_pw_qpolynomial *pwqp;
1842 struct isl_set *set;
1844 pwqp = read_term(s, map, v);
1845 map = read_optional_disjuncts(s, map, v, 0);
1846 set = isl_map_range(map);
1848 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1850 vars_drop(v, v->n - n);
1852 obj.v = pwqp;
1853 return obj;
1856 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1857 __isl_take isl_set *set, struct vars *v, int n)
1859 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1860 isl_pw_qpolynomial *pwqp;
1861 isl_pw_qpolynomial_fold *pwf = NULL;
1863 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1864 return obj_read_poly(s, set, v, n);
1866 if (isl_stream_eat(s, '('))
1867 goto error;
1869 pwqp = read_term(s, set, v);
1870 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1872 while (isl_stream_eat_if_available(s, ',')) {
1873 isl_pw_qpolynomial_fold *pwf_i;
1874 pwqp = read_term(s, set, v);
1875 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1876 pwqp);
1877 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1880 if (isl_stream_eat(s, ')'))
1881 goto error;
1883 set = read_optional_disjuncts(s, set, v, 0);
1884 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1886 vars_drop(v, v->n - n);
1888 obj.v = pwf;
1889 return obj;
1890 error:
1891 isl_set_free(set);
1892 isl_pw_qpolynomial_fold_free(pwf);
1893 obj.type = isl_obj_none;
1894 return obj;
1897 static int is_rational(struct isl_stream *s)
1899 struct isl_token *tok;
1901 tok = isl_stream_next_token(s);
1902 if (!tok)
1903 return 0;
1904 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1905 isl_token_free(tok);
1906 isl_stream_eat(s, ':');
1907 return 1;
1910 isl_stream_push_token(s, tok);
1912 return 0;
1915 static struct isl_obj obj_read_body(struct isl_stream *s,
1916 __isl_take isl_map *map, struct vars *v)
1918 struct isl_token *tok;
1919 struct isl_obj obj = { isl_obj_set, NULL };
1920 int n = v->n;
1921 int rational;
1923 rational = is_rational(s);
1924 if (rational)
1925 map = isl_map_set_rational(map);
1927 if (isl_stream_next_token_is(s, ':')) {
1928 obj.type = isl_obj_set;
1929 obj.v = read_optional_disjuncts(s, map, v, rational);
1930 return obj;
1933 if (!next_is_tuple(s))
1934 return obj_read_poly_or_fold(s, map, v, n);
1936 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
1937 if (!map)
1938 goto error;
1939 tok = isl_stream_next_token(s);
1940 if (!tok)
1941 goto error;
1942 if (tok->type == ISL_TOKEN_TO) {
1943 obj.type = isl_obj_map;
1944 isl_token_free(tok);
1945 if (!next_is_tuple(s)) {
1946 isl_set *set = isl_map_domain(map);
1947 return obj_read_poly_or_fold(s, set, v, n);
1949 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
1950 if (!map)
1951 goto error;
1952 } else {
1953 map = isl_map_domain(map);
1954 isl_stream_push_token(s, tok);
1957 map = read_optional_disjuncts(s, map, v, rational);
1959 vars_drop(v, v->n - n);
1961 obj.v = map;
1962 return obj;
1963 error:
1964 isl_map_free(map);
1965 obj.type = isl_obj_none;
1966 return obj;
1969 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1971 if (obj.type == isl_obj_map) {
1972 obj.v = isl_union_map_from_map(obj.v);
1973 obj.type = isl_obj_union_map;
1974 } else if (obj.type == isl_obj_set) {
1975 obj.v = isl_union_set_from_set(obj.v);
1976 obj.type = isl_obj_union_set;
1977 } else if (obj.type == isl_obj_pw_qpolynomial) {
1978 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1979 obj.type = isl_obj_union_pw_qpolynomial;
1980 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1981 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1982 obj.type = isl_obj_union_pw_qpolynomial_fold;
1983 } else
1984 isl_assert(ctx, 0, goto error);
1985 return obj;
1986 error:
1987 obj.type->free(obj.v);
1988 obj.type = isl_obj_none;
1989 return obj;
1992 static struct isl_obj obj_add(struct isl_ctx *ctx,
1993 struct isl_obj obj1, struct isl_obj obj2)
1995 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1996 obj1 = to_union(ctx, obj1);
1997 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1998 obj2 = to_union(ctx, obj2);
1999 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2000 obj1 = to_union(ctx, obj1);
2001 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2002 obj2 = to_union(ctx, obj2);
2003 if (obj1.type == isl_obj_pw_qpolynomial &&
2004 obj2.type == isl_obj_union_pw_qpolynomial)
2005 obj1 = to_union(ctx, obj1);
2006 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2007 obj2.type == isl_obj_pw_qpolynomial)
2008 obj2 = to_union(ctx, obj2);
2009 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2010 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2011 obj1 = to_union(ctx, obj1);
2012 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2013 obj2.type == isl_obj_pw_qpolynomial_fold)
2014 obj2 = to_union(ctx, obj2);
2015 isl_assert(ctx, obj1.type == obj2.type, goto error);
2016 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2017 obj1 = to_union(ctx, obj1);
2018 obj2 = to_union(ctx, obj2);
2020 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2021 obj1 = to_union(ctx, obj1);
2022 obj2 = to_union(ctx, obj2);
2024 if (obj1.type == isl_obj_pw_qpolynomial &&
2025 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2026 obj1 = to_union(ctx, obj1);
2027 obj2 = to_union(ctx, obj2);
2029 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2030 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2031 obj1 = to_union(ctx, obj1);
2032 obj2 = to_union(ctx, obj2);
2034 obj1.v = obj1.type->add(obj1.v, obj2.v);
2035 return obj1;
2036 error:
2037 obj1.type->free(obj1.v);
2038 obj2.type->free(obj2.v);
2039 obj1.type = isl_obj_none;
2040 obj1.v = NULL;
2041 return obj1;
2044 static struct isl_obj obj_read(struct isl_stream *s)
2046 isl_map *map = NULL;
2047 struct isl_token *tok;
2048 struct vars *v = NULL;
2049 struct isl_obj obj = { isl_obj_set, NULL };
2051 tok = next_token(s);
2052 if (!tok) {
2053 isl_stream_error(s, NULL, "unexpected EOF");
2054 goto error;
2056 if (tok->type == ISL_TOKEN_VALUE) {
2057 struct isl_token *tok2;
2058 struct isl_map *map;
2060 tok2 = isl_stream_next_token(s);
2061 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2062 isl_int_is_neg(tok2->u.v)) {
2063 if (tok2)
2064 isl_stream_push_token(s, tok2);
2065 obj.type = isl_obj_int;
2066 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
2067 isl_token_free(tok);
2068 return obj;
2070 isl_stream_push_token(s, tok2);
2071 isl_stream_push_token(s, tok);
2072 map = map_read_polylib(s);
2073 if (!map)
2074 goto error;
2075 if (isl_map_may_be_set(map))
2076 obj.v = isl_map_range(map);
2077 else {
2078 obj.type = isl_obj_map;
2079 obj.v = map;
2081 return obj;
2083 v = vars_new(s->ctx);
2084 if (!v) {
2085 isl_stream_push_token(s, tok);
2086 goto error;
2088 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2089 if (tok->type == '[') {
2090 isl_stream_push_token(s, tok);
2091 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2092 if (!map)
2093 goto error;
2094 tok = isl_stream_next_token(s);
2095 if (!tok || tok->type != ISL_TOKEN_TO) {
2096 isl_stream_error(s, tok, "expecting '->'");
2097 if (tok)
2098 isl_stream_push_token(s, tok);
2099 goto error;
2101 isl_token_free(tok);
2102 tok = isl_stream_next_token(s);
2104 if (!tok || tok->type != '{') {
2105 isl_stream_error(s, tok, "expecting '{'");
2106 if (tok)
2107 isl_stream_push_token(s, tok);
2108 goto error;
2110 isl_token_free(tok);
2112 tok = isl_stream_next_token(s);
2113 if (!tok)
2115 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2116 isl_token_free(tok);
2117 if (isl_stream_eat(s, '='))
2118 goto error;
2119 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2120 if (!map)
2121 goto error;
2122 } else if (tok->type == '}') {
2123 obj.type = isl_obj_union_set;
2124 obj.v = isl_union_set_empty(isl_map_get_space(map));
2125 isl_token_free(tok);
2126 goto done;
2127 } else
2128 isl_stream_push_token(s, tok);
2130 for (;;) {
2131 struct isl_obj o;
2132 tok = NULL;
2133 o = obj_read_body(s, isl_map_copy(map), v);
2134 if (o.type == isl_obj_none || !o.v)
2135 goto error;
2136 if (!obj.v)
2137 obj = o;
2138 else {
2139 obj = obj_add(s->ctx, obj, o);
2140 if (obj.type == isl_obj_none || !obj.v)
2141 goto error;
2143 tok = isl_stream_next_token(s);
2144 if (!tok || tok->type != ';')
2145 break;
2146 isl_token_free(tok);
2147 if (isl_stream_next_token_is(s, '}')) {
2148 tok = isl_stream_next_token(s);
2149 break;
2153 if (tok && tok->type == '}') {
2154 isl_token_free(tok);
2155 } else {
2156 isl_stream_error(s, tok, "unexpected isl_token");
2157 if (tok)
2158 isl_token_free(tok);
2159 goto error;
2161 done:
2162 vars_free(v);
2163 isl_map_free(map);
2165 return obj;
2166 error:
2167 isl_map_free(map);
2168 obj.type->free(obj.v);
2169 if (v)
2170 vars_free(v);
2171 obj.v = NULL;
2172 return obj;
2175 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
2177 return obj_read(s);
2180 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
2182 struct isl_obj obj;
2184 obj = obj_read(s);
2185 if (obj.v)
2186 isl_assert(s->ctx, obj.type == isl_obj_map ||
2187 obj.type == isl_obj_set, goto error);
2189 if (obj.type == isl_obj_set)
2190 obj.v = isl_map_from_range(obj.v);
2192 return obj.v;
2193 error:
2194 obj.type->free(obj.v);
2195 return NULL;
2198 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
2200 struct isl_obj obj;
2202 obj = obj_read(s);
2203 if (obj.v) {
2204 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2205 obj.v = isl_map_range(obj.v);
2206 obj.type = isl_obj_set;
2208 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2211 return obj.v;
2212 error:
2213 obj.type->free(obj.v);
2214 return NULL;
2217 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2219 struct isl_obj obj;
2221 obj = obj_read(s);
2222 if (obj.type == isl_obj_map) {
2223 obj.type = isl_obj_union_map;
2224 obj.v = isl_union_map_from_map(obj.v);
2226 if (obj.type == isl_obj_set) {
2227 obj.type = isl_obj_union_set;
2228 obj.v = isl_union_set_from_set(obj.v);
2230 if (obj.v && obj.type == isl_obj_union_set &&
2231 isl_union_set_is_empty(obj.v))
2232 obj.type = isl_obj_union_map;
2233 if (obj.v && obj.type != isl_obj_union_map)
2234 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2236 return obj.v;
2237 error:
2238 obj.type->free(obj.v);
2239 return NULL;
2242 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2244 struct isl_obj obj;
2246 obj = obj_read(s);
2247 if (obj.type == isl_obj_set) {
2248 obj.type = isl_obj_union_set;
2249 obj.v = isl_union_set_from_set(obj.v);
2251 if (obj.v)
2252 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2254 return obj.v;
2255 error:
2256 obj.type->free(obj.v);
2257 return NULL;
2260 static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
2262 struct isl_obj obj;
2263 struct isl_map *map;
2264 struct isl_basic_map *bmap;
2266 obj = obj_read(s);
2267 map = obj.v;
2268 if (!map)
2269 return NULL;
2271 isl_assert(map->ctx, map->n <= 1, goto error);
2273 if (map->n == 0)
2274 bmap = isl_basic_map_empty_like_map(map);
2275 else
2276 bmap = isl_basic_map_copy(map->p[0]);
2278 isl_map_free(map);
2280 return bmap;
2281 error:
2282 isl_map_free(map);
2283 return NULL;
2286 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
2288 isl_basic_map *bmap;
2289 bmap = basic_map_read(s);
2290 if (!bmap)
2291 return NULL;
2292 if (!isl_basic_map_may_be_set(bmap))
2293 isl_die(s->ctx, isl_error_invalid,
2294 "input is not a set", goto error);
2295 return isl_basic_map_range(bmap);
2296 error:
2297 isl_basic_map_free(bmap);
2298 return NULL;
2301 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2302 FILE *input)
2304 struct isl_basic_map *bmap;
2305 struct isl_stream *s = isl_stream_new_file(ctx, input);
2306 if (!s)
2307 return NULL;
2308 bmap = basic_map_read(s);
2309 isl_stream_free(s);
2310 return bmap;
2313 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2314 FILE *input)
2316 isl_basic_set *bset;
2317 struct isl_stream *s = isl_stream_new_file(ctx, input);
2318 if (!s)
2319 return NULL;
2320 bset = basic_set_read(s);
2321 isl_stream_free(s);
2322 return bset;
2325 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2326 const char *str)
2328 struct isl_basic_map *bmap;
2329 struct isl_stream *s = isl_stream_new_str(ctx, str);
2330 if (!s)
2331 return NULL;
2332 bmap = basic_map_read(s);
2333 isl_stream_free(s);
2334 return bmap;
2337 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2338 const char *str)
2340 isl_basic_set *bset;
2341 struct isl_stream *s = isl_stream_new_str(ctx, str);
2342 if (!s)
2343 return NULL;
2344 bset = basic_set_read(s);
2345 isl_stream_free(s);
2346 return bset;
2349 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2350 FILE *input)
2352 struct isl_map *map;
2353 struct isl_stream *s = isl_stream_new_file(ctx, input);
2354 if (!s)
2355 return NULL;
2356 map = isl_stream_read_map(s);
2357 isl_stream_free(s);
2358 return map;
2361 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2362 const char *str)
2364 struct isl_map *map;
2365 struct isl_stream *s = isl_stream_new_str(ctx, str);
2366 if (!s)
2367 return NULL;
2368 map = isl_stream_read_map(s);
2369 isl_stream_free(s);
2370 return map;
2373 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2374 FILE *input)
2376 isl_set *set;
2377 struct isl_stream *s = isl_stream_new_file(ctx, input);
2378 if (!s)
2379 return NULL;
2380 set = isl_stream_read_set(s);
2381 isl_stream_free(s);
2382 return set;
2385 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2386 const char *str)
2388 isl_set *set;
2389 struct isl_stream *s = isl_stream_new_str(ctx, str);
2390 if (!s)
2391 return NULL;
2392 set = isl_stream_read_set(s);
2393 isl_stream_free(s);
2394 return set;
2397 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2398 FILE *input)
2400 isl_union_map *umap;
2401 struct isl_stream *s = isl_stream_new_file(ctx, input);
2402 if (!s)
2403 return NULL;
2404 umap = isl_stream_read_union_map(s);
2405 isl_stream_free(s);
2406 return umap;
2409 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2410 const char *str)
2412 isl_union_map *umap;
2413 struct isl_stream *s = isl_stream_new_str(ctx, str);
2414 if (!s)
2415 return NULL;
2416 umap = isl_stream_read_union_map(s);
2417 isl_stream_free(s);
2418 return umap;
2421 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2422 FILE *input)
2424 isl_union_set *uset;
2425 struct isl_stream *s = isl_stream_new_file(ctx, input);
2426 if (!s)
2427 return NULL;
2428 uset = isl_stream_read_union_set(s);
2429 isl_stream_free(s);
2430 return uset;
2433 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2434 const char *str)
2436 isl_union_set *uset;
2437 struct isl_stream *s = isl_stream_new_str(ctx, str);
2438 if (!s)
2439 return NULL;
2440 uset = isl_stream_read_union_set(s);
2441 isl_stream_free(s);
2442 return uset;
2445 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2447 struct isl_vec *vec = NULL;
2448 struct isl_token *tok;
2449 unsigned size;
2450 int j;
2452 tok = isl_stream_next_token(s);
2453 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2454 isl_stream_error(s, tok, "expecting vector length");
2455 goto error;
2458 size = isl_int_get_si(tok->u.v);
2459 isl_token_free(tok);
2461 vec = isl_vec_alloc(s->ctx, size);
2463 for (j = 0; j < size; ++j) {
2464 tok = isl_stream_next_token(s);
2465 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2466 isl_stream_error(s, tok, "expecting constant value");
2467 goto error;
2469 isl_int_set(vec->el[j], tok->u.v);
2470 isl_token_free(tok);
2473 return vec;
2474 error:
2475 isl_token_free(tok);
2476 isl_vec_free(vec);
2477 return NULL;
2480 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2482 return isl_vec_read_polylib(s);
2485 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2487 isl_vec *v;
2488 struct isl_stream *s = isl_stream_new_file(ctx, input);
2489 if (!s)
2490 return NULL;
2491 v = vec_read(s);
2492 isl_stream_free(s);
2493 return v;
2496 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2497 struct isl_stream *s)
2499 struct isl_obj obj;
2501 obj = obj_read(s);
2502 if (obj.v)
2503 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2504 goto error);
2506 return obj.v;
2507 error:
2508 obj.type->free(obj.v);
2509 return NULL;
2512 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2513 const char *str)
2515 isl_pw_qpolynomial *pwqp;
2516 struct isl_stream *s = isl_stream_new_str(ctx, str);
2517 if (!s)
2518 return NULL;
2519 pwqp = isl_stream_read_pw_qpolynomial(s);
2520 isl_stream_free(s);
2521 return pwqp;
2524 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2525 FILE *input)
2527 isl_pw_qpolynomial *pwqp;
2528 struct isl_stream *s = isl_stream_new_file(ctx, input);
2529 if (!s)
2530 return NULL;
2531 pwqp = isl_stream_read_pw_qpolynomial(s);
2532 isl_stream_free(s);
2533 return pwqp;
2536 /* Is the next token an identifer not in "v"?
2538 static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
2540 int n = v->n;
2541 int fresh;
2542 struct isl_token *tok;
2544 tok = isl_stream_next_token(s);
2545 if (!tok)
2546 return 0;
2547 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
2548 isl_stream_push_token(s, tok);
2550 vars_drop(v, v->n - n);
2552 return fresh;
2555 /* First read the domain of the affine expression, which may be
2556 * a parameter space or a set.
2557 * The tricky part is that we don't know if the domain is a set or not,
2558 * so when we are trying to read the domain, we may actually be reading
2559 * the affine expression itself (defined on a parameter domains)
2560 * If the tuple we are reading is named, we assume it's the domain.
2561 * Also, if inside the tuple, the first thing we find is a nested tuple
2562 * or a new identifier, we again assume it's the domain.
2563 * Otherwise, we assume we are reading an affine expression.
2565 static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
2566 __isl_take isl_set *dom, struct vars *v)
2568 struct isl_token *tok;
2570 tok = isl_stream_next_token(s);
2571 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
2572 isl_stream_push_token(s, tok);
2573 return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2575 if (!tok || tok->type != '[') {
2576 isl_stream_error(s, tok, "expecting '['");
2577 goto error;
2579 if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
2580 isl_stream_push_token(s, tok);
2581 dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2582 } else
2583 isl_stream_push_token(s, tok);
2585 return dom;
2586 error:
2587 if (tok)
2588 isl_stream_push_token(s, tok);
2589 vars_free(v);
2590 isl_set_free(dom);
2591 return NULL;
2594 /* Read an affine expression from "s".
2596 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2598 isl_aff *aff;
2599 isl_multi_aff *ma;
2601 ma = isl_stream_read_multi_aff(s);
2602 if (!ma)
2603 return NULL;
2604 if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
2605 isl_die(s->ctx, isl_error_invalid,
2606 "expecting single affine expression",
2607 goto error);
2609 aff = isl_multi_aff_get_aff(ma, 0);
2610 isl_multi_aff_free(ma);
2611 return aff;
2612 error:
2613 isl_multi_aff_free(ma);
2614 return NULL;
2617 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2619 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2620 __isl_take isl_set *dom, struct vars *v)
2622 isl_pw_aff *pwaff = NULL;
2624 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2625 goto error;
2627 if (isl_stream_eat(s, '['))
2628 goto error;
2630 pwaff = accept_affine(s, isl_set_get_space(dom), v);
2632 if (isl_stream_eat(s, ']'))
2633 goto error;
2635 dom = read_optional_disjuncts(s, dom, v, 0);
2636 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2638 return pwaff;
2639 error:
2640 isl_set_free(dom);
2641 isl_pw_aff_free(pwaff);
2642 return NULL;
2645 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2647 struct vars *v;
2648 isl_set *dom = NULL;
2649 isl_set *aff_dom;
2650 isl_pw_aff *pa = NULL;
2651 int n;
2653 v = vars_new(s->ctx);
2654 if (!v)
2655 return NULL;
2657 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2658 if (next_is_tuple(s)) {
2659 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2660 if (isl_stream_eat(s, ISL_TOKEN_TO))
2661 goto error;
2663 if (isl_stream_eat(s, '{'))
2664 goto error;
2666 n = v->n;
2667 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2668 pa = read_pw_aff_with_dom(s, aff_dom, v);
2669 vars_drop(v, v->n - n);
2671 while (isl_stream_eat_if_available(s, ';')) {
2672 isl_pw_aff *pa_i;
2674 n = v->n;
2675 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2676 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2677 vars_drop(v, v->n - n);
2679 pa = isl_pw_aff_union_add(pa, pa_i);
2682 if (isl_stream_eat(s, '}'))
2683 goto error;
2685 vars_free(v);
2686 isl_set_free(dom);
2687 return pa;
2688 error:
2689 vars_free(v);
2690 isl_set_free(dom);
2691 isl_pw_aff_free(pa);
2692 return NULL;
2695 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2697 isl_aff *aff;
2698 struct isl_stream *s = isl_stream_new_str(ctx, str);
2699 if (!s)
2700 return NULL;
2701 aff = isl_stream_read_aff(s);
2702 isl_stream_free(s);
2703 return aff;
2706 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2708 isl_pw_aff *pa;
2709 struct isl_stream *s = isl_stream_new_str(ctx, str);
2710 if (!s)
2711 return NULL;
2712 pa = isl_stream_read_pw_aff(s);
2713 isl_stream_free(s);
2714 return pa;
2717 /* Read an isl_pw_multi_aff from "s".
2718 * We currently read a generic object and if it turns out to be a set or
2719 * a map, we convert that to an isl_pw_multi_aff.
2720 * It would be more efficient if we were to construct the isl_pw_multi_aff
2721 * directly.
2723 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2725 struct isl_obj obj;
2727 obj = obj_read(s);
2728 if (!obj.v)
2729 return NULL;
2731 if (obj.type == isl_obj_map)
2732 return isl_pw_multi_aff_from_map(obj.v);
2733 if (obj.type == isl_obj_set)
2734 return isl_pw_multi_aff_from_set(obj.v);
2736 obj.type->free(obj.v);
2737 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2738 return NULL);
2741 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2742 const char *str)
2744 isl_pw_multi_aff *pma;
2745 struct isl_stream *s = isl_stream_new_str(ctx, str);
2746 if (!s)
2747 return NULL;
2748 pma = isl_stream_read_pw_multi_aff(s);
2749 isl_stream_free(s);
2750 return pma;
2753 /* Assuming "pa" represents a single affine expression defined on a universe
2754 * domain, extract this affine expression.
2756 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
2758 isl_aff *aff;
2760 if (!pa)
2761 return NULL;
2762 if (pa->n != 1)
2763 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2764 "expecting single affine expression",
2765 goto error);
2766 if (!isl_set_plain_is_universe(pa->p[0].set))
2767 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2768 "expecting universe domain",
2769 goto error);
2771 aff = isl_aff_copy(pa->p[0].aff);
2772 isl_pw_aff_free(pa);
2773 return aff;
2774 error:
2775 isl_pw_aff_free(pa);
2776 return NULL;
2779 /* Read a multi-affine expression from "s".
2780 * If the multi-affine expression has a domain, then then tuple
2781 * representing this domain cannot involve any affine expressions.
2782 * The tuple representing the actual expressions needs to consist
2783 * of only affine expressions. Moreover, these expressions can
2784 * only depend on parameters and input dimensions and not on other
2785 * output dimensions.
2787 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2789 struct vars *v;
2790 isl_set *dom = NULL;
2791 isl_multi_pw_aff *tuple = NULL;
2792 int dim, i, n;
2793 isl_space *space, *dom_space;
2794 isl_multi_aff *ma = NULL;
2796 v = vars_new(s->ctx);
2797 if (!v)
2798 return NULL;
2800 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2801 if (next_is_tuple(s)) {
2802 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2803 if (isl_stream_eat(s, ISL_TOKEN_TO))
2804 goto error;
2806 if (!isl_set_plain_is_universe(dom))
2807 isl_die(s->ctx, isl_error_invalid,
2808 "expecting universe parameter domain", goto error);
2809 if (isl_stream_eat(s, '{'))
2810 goto error;
2812 tuple = read_tuple(s, v, 0, 0);
2813 if (!tuple)
2814 goto error;
2815 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
2816 isl_set *set;
2817 isl_space *space;
2818 int has_expr;
2820 has_expr = tuple_has_expr(tuple);
2821 if (has_expr < 0)
2822 goto error;
2823 if (has_expr)
2824 isl_die(s->ctx, isl_error_invalid,
2825 "expecting universe domain", goto error);
2826 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2827 set = isl_set_universe(space);
2828 dom = isl_set_intersect_params(set, dom);
2829 isl_multi_pw_aff_free(tuple);
2830 tuple = read_tuple(s, v, 0, 0);
2831 if (!tuple)
2832 goto error;
2835 if (isl_stream_eat(s, '}'))
2836 goto error;
2838 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
2839 dim = isl_set_dim(dom, isl_dim_all);
2840 dom_space = isl_set_get_space(dom);
2841 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2842 space = isl_space_align_params(space, isl_space_copy(dom_space));
2843 if (!isl_space_is_params(dom_space))
2844 space = isl_space_map_from_domain_and_range(
2845 isl_space_copy(dom_space), space);
2846 isl_space_free(dom_space);
2847 ma = isl_multi_aff_alloc(space);
2849 for (i = 0; i < n; ++i) {
2850 isl_pw_aff *pa;
2851 isl_aff *aff;
2852 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
2853 aff = aff_from_pw_aff(pa);
2854 if (!aff)
2855 goto error;
2856 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
2857 isl_aff_free(aff);
2858 isl_die(s->ctx, isl_error_invalid,
2859 "not an affine expression", goto error);
2861 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
2862 space = isl_multi_aff_get_domain_space(ma);
2863 aff = isl_aff_reset_domain_space(aff, space);
2864 ma = isl_multi_aff_set_aff(ma, i, aff);
2867 isl_multi_pw_aff_free(tuple);
2868 vars_free(v);
2869 isl_set_free(dom);
2870 return ma;
2871 error:
2872 isl_multi_pw_aff_free(tuple);
2873 vars_free(v);
2874 isl_set_free(dom);
2875 isl_multi_aff_free(ma);
2876 return NULL;
2879 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
2880 const char *str)
2882 isl_multi_aff *maff;
2883 struct isl_stream *s = isl_stream_new_str(ctx, str);
2884 if (!s)
2885 return NULL;
2886 maff = isl_stream_read_multi_aff(s);
2887 isl_stream_free(s);
2888 return maff;
2891 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
2892 struct isl_stream *s)
2894 struct isl_obj obj;
2896 obj = obj_read(s);
2897 if (obj.type == isl_obj_pw_qpolynomial) {
2898 obj.type = isl_obj_union_pw_qpolynomial;
2899 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2901 if (obj.v)
2902 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
2903 goto error);
2905 return obj.v;
2906 error:
2907 obj.type->free(obj.v);
2908 return NULL;
2911 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
2912 isl_ctx *ctx, const char *str)
2914 isl_union_pw_qpolynomial *upwqp;
2915 struct isl_stream *s = isl_stream_new_str(ctx, str);
2916 if (!s)
2917 return NULL;
2918 upwqp = isl_stream_read_union_pw_qpolynomial(s);
2919 isl_stream_free(s);
2920 return upwqp;