2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 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
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
22 #include <isl_stream_private.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_set.h>
26 #include <isl/union_map.h>
27 #include <isl_mat_private.h>
28 #include <isl_aff_private.h>
29 #include <isl_vec_private.h>
31 #include <isl_val_private.h>
36 struct variable
*next
;
45 static struct vars
*vars_new(struct isl_ctx
*ctx
)
48 v
= isl_alloc_type(ctx
, struct vars
);
57 static void variable_free(struct variable
*var
)
60 struct variable
*next
= var
->next
;
67 static void vars_free(struct vars
*v
)
75 static void vars_drop(struct vars
*v
, int n
)
86 struct variable
*next
= var
->next
;
94 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
98 var
= isl_calloc_type(v
->ctx
, struct variable
);
101 var
->name
= strdup(name
);
102 var
->name
[len
] = '\0';
111 static int vars_pos(struct vars
*v
, const char *s
, int len
)
118 for (q
= v
->v
; q
; q
= q
->next
) {
119 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
126 v
->v
= variable_new(v
, s
, len
, v
->n
);
134 static int vars_add_anon(struct vars
*v
)
136 v
->v
= variable_new(v
, "", 0, v
->n
);
145 /* Obtain next token, with some preprocessing.
146 * In particular, evaluate expressions of the form x^y,
147 * with x and y values.
149 static struct isl_token
*next_token(__isl_keep isl_stream
*s
)
151 struct isl_token
*tok
, *tok2
;
153 tok
= isl_stream_next_token(s
);
154 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
156 if (!isl_stream_eat_if_available(s
, '^'))
158 tok2
= isl_stream_next_token(s
);
159 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
160 isl_stream_error(s
, tok2
, "expecting constant value");
164 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
166 isl_token_free(tok2
);
170 isl_token_free(tok2
);
174 /* Read an isl_val from "s".
176 * The following token sequences are recognized
179 * "-" "infty" -> -infty
184 * where n, d and v are integer constants.
186 __isl_give isl_val
*isl_stream_read_val(__isl_keep isl_stream
*s
)
188 struct isl_token
*tok
= NULL
;
189 struct isl_token
*tok2
= NULL
;
194 isl_stream_error(s
, NULL
, "unexpected EOF");
197 if (tok
->type
== ISL_TOKEN_INFTY
) {
199 return isl_val_infty(s
->ctx
);
201 if (tok
->type
== '-' &&
202 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
204 return isl_val_neginfty(s
->ctx
);
206 if (tok
->type
== ISL_TOKEN_NAN
) {
208 return isl_val_nan(s
->ctx
);
210 if (tok
->type
!= ISL_TOKEN_VALUE
) {
211 isl_stream_error(s
, tok
, "expecting value");
215 if (isl_stream_eat_if_available(s
, '/')) {
216 tok2
= next_token(s
);
218 isl_stream_error(s
, NULL
, "unexpected EOF");
221 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
222 isl_stream_error(s
, tok2
, "expecting value");
225 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
226 val
= isl_val_normalize(val
);
228 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
232 isl_token_free(tok2
);
236 isl_token_free(tok2
);
240 /* Read an isl_val from "str".
242 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
246 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
249 val
= isl_stream_read_val(s
);
254 static int accept_cst_factor(__isl_keep isl_stream
*s
, isl_int
*f
)
256 struct isl_token
*tok
;
259 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
260 isl_stream_error(s
, tok
, "expecting constant value");
264 isl_int_mul(*f
, *f
, tok
->u
.v
);
268 if (isl_stream_eat_if_available(s
, '*'))
269 return accept_cst_factor(s
, f
);
277 /* Given an affine expression aff, return an affine expression
278 * for aff % d, with d the next token on the stream, which is
279 * assumed to be a constant.
281 * We introduce an integer division q = [aff/d] and the result
282 * is set to aff - d q.
284 static __isl_give isl_pw_aff
*affine_mod(__isl_keep isl_stream
*s
,
285 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
287 struct isl_token
*tok
;
291 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
292 isl_stream_error(s
, tok
, "expecting constant value");
296 q
= isl_pw_aff_copy(aff
);
297 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
298 q
= isl_pw_aff_floor(q
);
299 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
301 aff
= isl_pw_aff_sub(aff
, q
);
306 isl_pw_aff_free(aff
);
311 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
312 __isl_take isl_space
*space
, struct vars
*v
);
313 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
314 __isl_take isl_space
*dim
, struct vars
*v
);
316 static __isl_give isl_pw_aff
*accept_minmax(__isl_keep isl_stream
*s
,
317 __isl_take isl_space
*dim
, struct vars
*v
)
319 struct isl_token
*tok
;
320 isl_pw_aff_list
*list
= NULL
;
323 tok
= isl_stream_next_token(s
);
326 min
= tok
->type
== ISL_TOKEN_MIN
;
329 if (isl_stream_eat(s
, '('))
332 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
336 if (isl_stream_eat(s
, ')'))
340 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
343 isl_pw_aff_list_free(list
);
347 /* Is "tok" the start of an integer division?
349 static int is_start_of_div(struct isl_token
*tok
)
353 if (tok
->type
== '[')
355 if (tok
->type
== ISL_TOKEN_FLOOR
)
357 if (tok
->type
== ISL_TOKEN_CEIL
)
359 if (tok
->type
== ISL_TOKEN_FLOORD
)
361 if (tok
->type
== ISL_TOKEN_CEILD
)
366 /* Read an integer division from "s" and return it as an isl_pw_aff.
368 * The integer division can be of the form
370 * [<affine expression>]
371 * floor(<affine expression>)
372 * ceil(<affine expression>)
373 * floord(<affine expression>,<denominator>)
374 * ceild(<affine expression>,<denominator>)
376 static __isl_give isl_pw_aff
*accept_div(__isl_keep isl_stream
*s
,
377 __isl_take isl_space
*dim
, struct vars
*v
)
379 struct isl_token
*tok
;
383 isl_pw_aff
*pwaff
= NULL
;
385 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
387 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
389 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
391 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
394 if (isl_stream_eat(s
, '('))
397 if (isl_stream_eat(s
, '['))
401 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
404 if (isl_stream_eat(s
, ','))
410 if (tok
->type
!= ISL_TOKEN_VALUE
) {
411 isl_stream_error(s
, tok
, "expected denominator");
412 isl_stream_push_token(s
, tok
);
415 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
420 pwaff
= isl_pw_aff_ceil(pwaff
);
422 pwaff
= isl_pw_aff_floor(pwaff
);
425 if (isl_stream_eat(s
, ')'))
428 if (isl_stream_eat(s
, ']'))
436 isl_pw_aff_free(pwaff
);
440 static __isl_give isl_pw_aff
*accept_affine_factor(__isl_keep isl_stream
*s
,
441 __isl_take isl_space
*dim
, struct vars
*v
)
443 struct isl_token
*tok
= NULL
;
444 isl_pw_aff
*res
= NULL
;
448 isl_stream_error(s
, NULL
, "unexpected EOF");
452 if (tok
->type
== ISL_TOKEN_AFF
) {
453 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
455 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
457 int pos
= vars_pos(v
, tok
->u
.s
, -1);
463 vars_drop(v
, v
->n
- n
);
464 isl_stream_error(s
, tok
, "unknown identifier");
468 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
471 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
472 res
= isl_pw_aff_from_aff(aff
);
474 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
475 if (isl_stream_eat_if_available(s
, '*')) {
476 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
477 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
481 ls
= isl_local_space_from_space(isl_space_copy(dim
));
482 aff
= isl_aff_zero_on_domain(ls
);
483 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
484 res
= isl_pw_aff_from_aff(aff
);
487 } else if (tok
->type
== '(') {
490 res
= accept_affine(s
, isl_space_copy(dim
), v
);
493 if (isl_stream_eat(s
, ')'))
495 } else if (is_start_of_div(tok
)) {
496 isl_stream_push_token(s
, tok
);
498 res
= accept_div(s
, isl_space_copy(dim
), v
);
499 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
500 isl_stream_push_token(s
, tok
);
502 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
504 isl_stream_error(s
, tok
, "expecting factor");
507 if (isl_stream_eat_if_available(s
, '%') ||
508 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
510 return affine_mod(s
, v
, res
);
512 if (isl_stream_eat_if_available(s
, '*')) {
515 isl_int_set_si(f
, 1);
516 if (accept_cst_factor(s
, &f
) < 0) {
520 res
= isl_pw_aff_scale(res
, f
);
523 if (isl_stream_eat_if_available(s
, '/')) {
526 isl_int_set_si(f
, 1);
527 if (accept_cst_factor(s
, &f
) < 0) {
531 res
= isl_pw_aff_scale_down(res
, f
);
540 isl_pw_aff_free(res
);
545 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
550 space
= isl_pw_aff_get_domain_space(pwaff
);
551 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
552 aff
= isl_aff_add_constant(aff
, v
);
554 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
557 /* Return a piecewise affine expression defined on the specified domain
558 * that represents NaN.
560 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
564 ls
= isl_local_space_from_space(isl_space_copy(space
));
565 return isl_pw_aff_nan_on_domain(ls
);
568 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
569 __isl_take isl_space
*space
, struct vars
*v
)
571 struct isl_token
*tok
= NULL
;
576 ls
= isl_local_space_from_space(isl_space_copy(space
));
577 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
584 isl_stream_error(s
, NULL
, "unexpected EOF");
587 if (tok
->type
== '-') {
592 if (tok
->type
== '(' || is_start_of_div(tok
) ||
593 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
594 tok
->type
== ISL_TOKEN_IDENT
||
595 tok
->type
== ISL_TOKEN_AFF
) {
597 isl_stream_push_token(s
, tok
);
599 term
= accept_affine_factor(s
,
600 isl_space_copy(space
), v
);
602 res
= isl_pw_aff_sub(res
, term
);
604 res
= isl_pw_aff_add(res
, term
);
608 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
610 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
611 if (isl_stream_eat_if_available(s
, '*') ||
612 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
614 term
= accept_affine_factor(s
,
615 isl_space_copy(space
), v
);
616 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
617 res
= isl_pw_aff_add(res
, term
);
621 res
= add_cst(res
, tok
->u
.v
);
624 } else if (tok
->type
== ISL_TOKEN_NAN
) {
625 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
627 isl_stream_error(s
, tok
, "unexpected isl_token");
628 isl_stream_push_token(s
, tok
);
629 isl_pw_aff_free(res
);
630 isl_space_free(space
);
636 if (tok
&& tok
->type
== '-') {
639 } else if (tok
&& tok
->type
== '+') {
642 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
643 isl_int_is_neg(tok
->u
.v
)) {
644 isl_stream_push_token(s
, tok
);
647 isl_stream_push_token(s
, tok
);
652 isl_space_free(space
);
655 isl_space_free(space
);
657 isl_pw_aff_free(res
);
661 static int is_comparator(struct isl_token
*tok
)
679 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
680 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
681 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
682 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
684 /* Accept a ternary operator, given the first argument.
686 static __isl_give isl_pw_aff
*accept_ternary(__isl_keep isl_stream
*s
,
687 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
690 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
695 if (isl_stream_eat(s
, '?'))
698 dim
= isl_space_wrap(isl_map_get_space(cond
));
699 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
703 if (isl_stream_eat(s
, ':'))
706 dim
= isl_pw_aff_get_domain_space(pwaff1
);
707 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
711 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
712 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
715 isl_pw_aff_free(pwaff1
);
716 isl_pw_aff_free(pwaff2
);
720 /* Set *line and *col to those of the next token, if any.
722 static void set_current_line_col(__isl_keep isl_stream
*s
, int *line
, int *col
)
724 struct isl_token
*tok
;
726 tok
= isl_stream_next_token(s
);
732 isl_stream_push_token(s
, tok
);
735 /* Push a token encapsulating "pa" onto "s", with the given
738 static int push_aff(__isl_keep isl_stream
*s
, int line
, int col
,
739 __isl_take isl_pw_aff
*pa
)
741 struct isl_token
*tok
;
743 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
746 tok
->type
= ISL_TOKEN_AFF
;
748 isl_stream_push_token(s
, tok
);
756 /* Accept an affine expression that may involve ternary operators.
757 * We first read an affine expression.
758 * If it is not followed by a comparison operator, we simply return it.
759 * Otherwise, we assume the affine expression is part of the first
760 * argument of a ternary operator and try to parse that.
762 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
763 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
768 struct isl_token
*tok
;
769 int line
= -1, col
= -1;
772 set_current_line_col(s
, &line
, &col
);
774 pwaff
= accept_affine(s
, dim
, v
);
776 pwaff
= isl_pw_aff_set_rational(pwaff
);
780 tok
= isl_stream_next_token(s
);
782 return isl_pw_aff_free(pwaff
);
784 is_comp
= is_comparator(tok
);
785 isl_stream_push_token(s
, tok
);
789 space
= isl_pw_aff_get_domain_space(pwaff
);
790 cond
= isl_map_universe(isl_space_unwrap(space
));
792 if (push_aff(s
, line
, col
, pwaff
) < 0)
793 cond
= isl_map_free(cond
);
797 cond
= read_formula(s
, v
, cond
, rational
);
799 return accept_ternary(s
, cond
, v
, rational
);
802 static __isl_give isl_map
*read_var_def(__isl_keep isl_stream
*s
,
803 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
810 if (type
== isl_dim_param
)
811 pos
= isl_map_dim(map
, isl_dim_param
);
813 pos
= isl_map_dim(map
, isl_dim_in
);
814 if (type
== isl_dim_out
)
815 pos
+= isl_map_dim(map
, isl_dim_out
);
820 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
822 def_map
= isl_map_from_pw_aff(def
);
823 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
824 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
826 map
= isl_map_intersect(map
, def_map
);
831 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
832 __isl_take isl_space
*dim
, struct vars
*v
)
835 isl_pw_aff_list
*list
;
836 struct isl_token
*tok
= NULL
;
838 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
839 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
844 tok
= isl_stream_next_token(s
);
846 isl_stream_error(s
, NULL
, "unexpected EOF");
849 if (tok
->type
!= ',') {
850 isl_stream_push_token(s
, tok
);
855 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
856 list
= isl_pw_aff_list_concat(list
,
857 isl_pw_aff_list_from_pw_aff(pwaff
));
866 isl_pw_aff_list_free(list
);
870 static __isl_give isl_map
*read_defined_var_list(__isl_keep isl_stream
*s
,
871 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
873 struct isl_token
*tok
;
875 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
879 if (tok
->type
!= ISL_TOKEN_IDENT
)
882 p
= vars_pos(v
, tok
->u
.s
, -1);
886 isl_stream_error(s
, tok
, "expecting unique identifier");
890 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
893 tok
= isl_stream_next_token(s
);
894 if (tok
&& tok
->type
== '=') {
896 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
897 tok
= isl_stream_next_token(s
);
900 if (!tok
|| tok
->type
!= ',')
906 isl_stream_push_token(s
, tok
);
915 static int next_is_tuple(__isl_keep isl_stream
*s
)
917 struct isl_token
*tok
;
920 tok
= isl_stream_next_token(s
);
923 if (tok
->type
== '[') {
924 isl_stream_push_token(s
, tok
);
927 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
928 isl_stream_push_token(s
, tok
);
932 is_tuple
= isl_stream_next_token_is(s
, '[');
934 isl_stream_push_token(s
, tok
);
939 /* Is "pa" an expression in term of earlier dimensions?
940 * The alternative is that the dimension is defined to be equal to itself,
941 * meaning that it has a universe domain and an expression that depends
942 * on itself. "i" is the position of the expression in a sequence
943 * of "n" expressions. The final dimensions of "pa" correspond to
944 * these "n" expressions.
946 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
954 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
958 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
963 /* Does the tuple contain any dimensions that are defined
964 * in terms of earlier dimensions?
966 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
974 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
975 for (i
= 0; i
< n
; ++i
) {
976 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
977 has_expr
= pw_aff_is_expr(pa
, i
, n
);
979 if (has_expr
< 0 || has_expr
)
986 /* Set the name of dimension "pos" in "space" to "name".
987 * During printing, we add primes if the same name appears more than once
988 * to distinguish the occurrences. Here, we remove those primes from "name"
989 * before setting the name of the dimension.
991 static __isl_give isl_space
*space_set_dim_name(__isl_take isl_space
*space
,
999 prime
= strchr(name
, '\'');
1002 space
= isl_space_set_dim_name(space
, isl_dim_out
, pos
, name
);
1009 /* Accept a piecewise affine expression.
1011 * At the outer level, the piecewise affine expression may be of the form
1013 * aff1 : condition1; aff2 : conditions2; ...
1019 * each of the affine expressions may in turn include ternary operators.
1021 * There may be parentheses around some subexpression of "aff1"
1022 * around "aff1" itself, around "aff1 : condition1" and/or
1023 * around the entire piecewise affine expression.
1024 * We therefore remove the opening parenthesis (if any) from the stream
1025 * in case the closing parenthesis follows the colon, but if the closing
1026 * parenthesis is the first thing in the stream after the parsed affine
1027 * expression, we push the parsed expression onto the stream and parse
1028 * again in case the parentheses enclose some subexpression of "aff1".
1030 static __isl_give isl_pw_aff
*accept_piecewise_affine(__isl_keep isl_stream
*s
,
1031 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1034 isl_space
*res_space
;
1036 res_space
= isl_space_from_domain(isl_space_copy(space
));
1037 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1038 res
= isl_pw_aff_empty(res_space
);
1042 int line
= -1, col
= -1;
1044 set_current_line_col(s
, &line
, &col
);
1045 seen_paren
= isl_stream_eat_if_available(s
, '(');
1047 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1050 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1052 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1054 if (push_aff(s
, line
, col
, pa
) < 0)
1056 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1059 if (isl_stream_eat_if_available(s
, ':')) {
1060 isl_space
*dom_space
;
1063 dom_space
= isl_pw_aff_get_domain_space(pa
);
1064 dom
= isl_set_universe(dom_space
);
1065 dom
= read_formula(s
, v
, dom
, rational
);
1066 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1069 res
= isl_pw_aff_union_add(res
, pa
);
1071 if (seen_paren
&& isl_stream_eat(s
, ')'))
1073 } while (isl_stream_eat_if_available(s
, ';'));
1075 isl_space_free(space
);
1079 isl_space_free(space
);
1080 return isl_pw_aff_free(res
);
1083 /* Read an affine expression from "s" for use in read_tuple.
1085 * accept_extended_affine requires a wrapped space as input.
1086 * read_tuple on the other hand expects each isl_pw_aff
1087 * to have an anonymous space. We therefore adjust the space
1088 * of the isl_pw_aff before returning it.
1090 static __isl_give isl_pw_aff
*read_tuple_var_def(__isl_keep isl_stream
*s
,
1091 struct vars
*v
, int rational
)
1096 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1098 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1100 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1101 def
= isl_pw_aff_reset_domain_space(def
, space
);
1106 /* Read a list of tuple elements by calling "read_el" on each of them and
1107 * return a space with the same number of set dimensions derived from
1108 * the parameter space "space" and possibly updated by "read_el".
1109 * The elements in the list are separated by either "," or "][".
1110 * If "comma" is set then only "," is allowed.
1112 static __isl_give isl_space
*read_tuple_list(__isl_keep isl_stream
*s
,
1113 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1114 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1115 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1122 space
= isl_space_set_from_params(space
);
1124 if (isl_stream_next_token_is(s
, ']'))
1128 struct isl_token
*tok
;
1130 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1132 space
= read_el(s
, v
, space
, rational
, user
);
1136 tok
= isl_stream_next_token(s
);
1137 if (!comma
&& tok
&& tok
->type
== ']' &&
1138 isl_stream_next_token_is(s
, '[')) {
1139 isl_token_free(tok
);
1140 tok
= isl_stream_next_token(s
);
1141 } else if (!tok
|| tok
->type
!= ',') {
1143 isl_stream_push_token(s
, tok
);
1147 isl_token_free(tok
);
1153 /* Read a tuple space from "s" derived from the parameter space "space".
1154 * Call "read_el" on each element in the tuples.
1156 static __isl_give isl_space
*read_tuple_space(__isl_keep isl_stream
*s
,
1157 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1158 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1159 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1163 struct isl_token
*tok
;
1165 isl_space
*res
= NULL
;
1167 tok
= isl_stream_next_token(s
);
1170 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1171 name
= strdup(tok
->u
.s
);
1172 isl_token_free(tok
);
1176 isl_stream_push_token(s
, tok
);
1177 if (isl_stream_eat(s
, '['))
1179 if (next_is_tuple(s
)) {
1181 res
= read_tuple_space(s
, v
, isl_space_copy(space
),
1182 rational
, comma
, read_el
, user
);
1183 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1185 out
= read_tuple_space(s
, v
, isl_space_copy(space
),
1186 rational
, comma
, read_el
, user
);
1187 res
= isl_space_range_product(res
, out
);
1189 res
= read_tuple_list(s
, v
, isl_space_copy(space
),
1190 rational
, comma
, read_el
, user
);
1191 if (isl_stream_eat(s
, ']'))
1195 res
= isl_space_set_tuple_name(res
, isl_dim_set
, name
);
1199 isl_space_free(space
);
1203 isl_space_free(res
);
1204 isl_space_free(space
);
1208 /* Construct an isl_pw_aff defined on a space with v->n variables
1209 * that is equal to the last of those variables.
1211 static __isl_give isl_pw_aff
*identity_tuple_el(struct vars
*v
)
1216 space
= isl_space_set_alloc(v
->ctx
, 0, v
->n
);
1217 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1218 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
- 1, 1);
1219 return isl_pw_aff_from_aff(aff
);
1222 /* This function is called for each element in a tuple inside read_tuple.
1223 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1224 * over a space containing all variables in "v" defined so far.
1225 * The isl_pw_aff expresses the new variable in terms of earlier variables
1226 * if a definition is provided. Otherwise, it is represented as being
1228 * Add the isl_pw_aff to *list.
1229 * If the new variable was named, then adjust "space" accordingly and
1230 * return the updated space.
1232 static __isl_give isl_space
*read_tuple_pw_aff_el(__isl_keep isl_stream
*s
,
1233 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1235 isl_pw_aff_list
**list
= (isl_pw_aff_list
**) user
;
1237 struct isl_token
*tok
;
1240 tok
= next_token(s
);
1242 isl_stream_error(s
, NULL
, "unexpected EOF");
1243 return isl_space_free(space
);
1246 if (tok
->type
== ISL_TOKEN_IDENT
) {
1248 int p
= vars_pos(v
, tok
->u
.s
, -1);
1254 if (tok
->type
== '*') {
1255 if (vars_add_anon(v
) < 0)
1257 isl_token_free(tok
);
1258 pa
= identity_tuple_el(v
);
1259 } else if (new_name
) {
1260 int pos
= isl_space_dim(space
, isl_dim_out
) - 1;
1261 space
= space_set_dim_name(space
, pos
, v
->v
->name
);
1262 isl_token_free(tok
);
1263 if (isl_stream_eat_if_available(s
, '='))
1264 pa
= read_tuple_var_def(s
, v
, rational
);
1266 pa
= identity_tuple_el(v
);
1268 isl_stream_push_token(s
, tok
);
1270 if (vars_add_anon(v
) < 0)
1272 pa
= read_tuple_var_def(s
, v
, rational
);
1275 *list
= isl_pw_aff_list_add(*list
, pa
);
1277 return isl_space_free(space
);
1281 isl_token_free(tok
);
1282 return isl_space_free(space
);
1285 /* Read a tuple and represent it as an isl_multi_pw_aff.
1286 * The range space of the isl_multi_pw_aff is the space of the tuple.
1287 * The domain space is an anonymous space
1288 * with a dimension for each variable in the set of variables in "v",
1289 * including the variables in the range.
1290 * If a given dimension is not defined in terms of earlier dimensions in
1291 * the input, then the corresponding isl_pw_aff is set equal to one time
1292 * the variable corresponding to the dimension being defined.
1294 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1295 * Each element in this list is defined over a space representing
1296 * the variables defined so far. We need to adjust the earlier
1297 * elements to have as many variables in the domain as the final
1298 * element in the list.
1300 static __isl_give isl_multi_pw_aff
*read_tuple(__isl_keep isl_stream
*s
,
1301 struct vars
*v
, int rational
, int comma
)
1305 isl_pw_aff_list
*list
;
1307 space
= isl_space_params_alloc(v
->ctx
, 0);
1308 list
= isl_pw_aff_list_alloc(s
->ctx
, 0);
1309 space
= read_tuple_space(s
, v
, space
, rational
, comma
,
1310 &read_tuple_pw_aff_el
, &list
);
1311 n
= isl_space_dim(space
, isl_dim_set
);
1312 for (i
= 0; i
+ 1 < n
; ++i
) {
1315 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
1316 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, n
- (i
+ 1));
1317 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
1320 space
= isl_space_from_range(space
);
1321 space
= isl_space_add_dims(space
, isl_dim_in
, v
->n
);
1322 return isl_multi_pw_aff_from_pw_aff_list(space
, list
);
1325 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1326 * We first create the appropriate space in "map" based on the range
1327 * space of this isl_multi_pw_aff. Then, we add equalities based
1328 * on the affine expressions. These live in an anonymous space,
1329 * however, so we first need to reset the space to that of "map".
1331 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1332 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1337 isl_space
*space
= NULL
;
1341 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1342 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1343 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1347 if (type
== isl_dim_param
) {
1348 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1349 isl_space_is_wrapping(space
)) {
1350 isl_die(ctx
, isl_error_invalid
,
1351 "parameter tuples cannot be named or nested",
1354 map
= isl_map_add_dims(map
, type
, n
);
1355 for (i
= 0; i
< n
; ++i
) {
1357 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1358 isl_die(ctx
, isl_error_invalid
,
1359 "parameters must be named",
1361 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1362 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1364 } else if (type
== isl_dim_in
) {
1367 set
= isl_set_universe(isl_space_copy(space
));
1369 set
= isl_set_set_rational(set
);
1370 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1371 map
= isl_map_from_domain(set
);
1375 set
= isl_set_universe(isl_space_copy(space
));
1377 set
= isl_set_set_rational(set
);
1378 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1381 for (i
= 0; i
< n
; ++i
) {
1388 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1389 space
= isl_pw_aff_get_domain_space(pa
);
1390 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1391 aff
= isl_aff_add_coefficient_si(aff
,
1392 isl_dim_in
, v
->n
- n
+ i
, -1);
1393 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1395 pa
= isl_pw_aff_set_rational(pa
);
1396 set
= isl_pw_aff_zero_set(pa
);
1397 map_i
= isl_map_from_range(set
);
1398 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1399 map
= isl_map_intersect(map
, map_i
);
1402 isl_space_free(space
);
1403 isl_multi_pw_aff_free(tuple
);
1406 isl_space_free(space
);
1407 isl_multi_pw_aff_free(tuple
);
1412 /* Read a tuple from "s" and add it to "map".
1413 * The tuple is initially represented as an isl_multi_pw_aff and
1414 * then added to "map".
1416 static __isl_give isl_map
*read_map_tuple(__isl_keep isl_stream
*s
,
1417 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1418 int rational
, int comma
)
1420 isl_multi_pw_aff
*tuple
;
1422 tuple
= read_tuple(s
, v
, rational
, comma
);
1424 return isl_map_free(map
);
1426 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1429 static __isl_give isl_set
*construct_constraints(
1430 __isl_take isl_set
*set
, int type
,
1431 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1436 left
= isl_pw_aff_list_copy(left
);
1437 right
= isl_pw_aff_list_copy(right
);
1439 left
= isl_pw_aff_list_set_rational(left
);
1440 right
= isl_pw_aff_list_set_rational(right
);
1442 if (type
== ISL_TOKEN_LE
)
1443 cond
= isl_pw_aff_list_le_set(left
, right
);
1444 else if (type
== ISL_TOKEN_GE
)
1445 cond
= isl_pw_aff_list_ge_set(left
, right
);
1446 else if (type
== ISL_TOKEN_LT
)
1447 cond
= isl_pw_aff_list_lt_set(left
, right
);
1448 else if (type
== ISL_TOKEN_GT
)
1449 cond
= isl_pw_aff_list_gt_set(left
, right
);
1450 else if (type
== ISL_TOKEN_NE
)
1451 cond
= isl_pw_aff_list_ne_set(left
, right
);
1453 cond
= isl_pw_aff_list_eq_set(left
, right
);
1455 return isl_set_intersect(set
, cond
);
1458 static __isl_give isl_map
*add_constraint(__isl_keep isl_stream
*s
,
1459 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1461 struct isl_token
*tok
= NULL
;
1462 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1465 set
= isl_map_wrap(map
);
1466 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1469 tok
= isl_stream_next_token(s
);
1470 if (!is_comparator(tok
)) {
1471 isl_stream_error(s
, tok
, "missing operator");
1473 isl_stream_push_token(s
, tok
);
1478 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1482 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1484 isl_token_free(tok
);
1485 isl_pw_aff_list_free(list1
);
1488 tok
= isl_stream_next_token(s
);
1489 if (!is_comparator(tok
)) {
1491 isl_stream_push_token(s
, tok
);
1495 isl_pw_aff_list_free(list1
);
1497 return isl_set_unwrap(set
);
1500 isl_token_free(tok
);
1501 isl_pw_aff_list_free(list1
);
1502 isl_pw_aff_list_free(list2
);
1507 static __isl_give isl_map
*read_exists(__isl_keep isl_stream
*s
,
1508 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1511 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1513 map
= isl_map_from_domain(isl_map_wrap(map
));
1514 map
= read_defined_var_list(s
, v
, map
, rational
);
1516 if (isl_stream_eat(s
, ':'))
1519 map
= read_formula(s
, v
, map
, rational
);
1520 map
= isl_set_unwrap(isl_map_domain(map
));
1522 vars_drop(v
, v
->n
- n
);
1523 if (seen_paren
&& isl_stream_eat(s
, ')'))
1532 /* Parse an expression between parentheses and push the result
1533 * back on the stream.
1535 * The parsed expression may be either an affine expression
1536 * or a condition. The first type is pushed onto the stream
1537 * as an isl_pw_aff, while the second is pushed as an isl_map.
1539 * If the initial token indicates the start of a condition,
1540 * we parse it as such.
1541 * Otherwise, we first parse an affine expression and push
1542 * that onto the stream. If the affine expression covers the
1543 * entire expression between parentheses, we return.
1544 * Otherwise, we assume that the affine expression is the
1545 * start of a condition and continue parsing.
1547 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
1548 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1550 struct isl_token
*tok
, *tok2
;
1554 tok
= isl_stream_next_token(s
);
1555 if (!tok
|| tok
->type
!= '(')
1558 if (isl_stream_next_token_is(s
, '('))
1559 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1562 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1563 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1564 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1565 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1566 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1567 map
= read_formula(s
, v
, map
, rational
);
1568 if (isl_stream_eat(s
, ')'))
1570 tok
->type
= ISL_TOKEN_MAP
;
1572 isl_stream_push_token(s
, tok
);
1576 tok2
= isl_stream_next_token(s
);
1581 isl_stream_push_token(s
, tok2
);
1583 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1587 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1590 tok2
->type
= ISL_TOKEN_AFF
;
1591 tok2
->u
.pwaff
= pwaff
;
1593 if (isl_stream_eat_if_available(s
, ')')) {
1594 isl_stream_push_token(s
, tok2
);
1595 isl_token_free(tok
);
1600 isl_stream_push_token(s
, tok2
);
1602 map
= read_formula(s
, v
, map
, rational
);
1603 if (isl_stream_eat(s
, ')'))
1606 tok
->type
= ISL_TOKEN_MAP
;
1608 isl_stream_push_token(s
, tok
);
1612 isl_pw_aff_free(pwaff
);
1614 isl_token_free(tok
);
1619 static __isl_give isl_map
*read_conjunct(__isl_keep isl_stream
*s
,
1620 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1622 if (isl_stream_next_token_is(s
, '('))
1623 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1626 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1627 struct isl_token
*tok
;
1628 tok
= isl_stream_next_token(s
);
1632 map
= isl_map_copy(tok
->u
.map
);
1633 isl_token_free(tok
);
1637 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1638 return read_exists(s
, v
, map
, rational
);
1640 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1643 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1644 isl_space
*dim
= isl_map_get_space(map
);
1646 return isl_map_empty(dim
);
1649 return add_constraint(s
, v
, map
, rational
);
1655 static __isl_give isl_map
*read_conjuncts(__isl_keep isl_stream
*s
,
1656 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1661 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1662 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1664 res
= isl_map_subtract(isl_map_copy(map
), res
);
1666 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1669 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1670 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1672 res
= isl_map_subtract(res
, res_i
);
1674 res
= isl_map_intersect(res
, res_i
);
1681 static struct isl_map
*read_disjuncts(__isl_keep isl_stream
*s
,
1682 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1686 if (isl_stream_next_token_is(s
, '}')) {
1687 isl_space
*dim
= isl_map_get_space(map
);
1689 return isl_map_universe(dim
);
1692 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1693 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1696 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1697 res
= isl_map_union(res
, res_i
);
1704 /* Read a first order formula from "s", add the corresponding
1705 * constraints to "map" and return the result.
1707 * In particular, read a formula of the form
1715 * where a and b are disjunctions.
1717 * In the first case, map is replaced by
1719 * map \cap { [..] : a }
1721 * In the second case, it is replaced by
1723 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1725 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
1726 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1730 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1732 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1735 res
= isl_map_subtract(isl_map_copy(map
), res
);
1736 res2
= read_disjuncts(s
, v
, map
, rational
);
1737 res
= isl_map_union(res
, res2
);
1744 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1746 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1747 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1748 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1749 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1751 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1752 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1753 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1755 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1756 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1757 isl_basic_map_dim(bmap
, isl_dim_in
) +
1758 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1759 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1761 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1767 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1768 __isl_keep isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1771 struct isl_token
*tok
;
1779 tok
= isl_stream_next_token(s
);
1780 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1781 isl_stream_error(s
, tok
, "expecting coefficient");
1783 isl_stream_push_token(s
, tok
);
1786 if (!tok
->on_new_line
) {
1787 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1788 isl_stream_push_token(s
, tok
);
1792 type
= isl_int_get_si(tok
->u
.v
);
1793 isl_token_free(tok
);
1795 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1797 k
= isl_basic_map_alloc_equality(bmap
);
1800 k
= isl_basic_map_alloc_inequality(bmap
);
1806 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1808 tok
= isl_stream_next_token(s
);
1809 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1810 isl_stream_error(s
, tok
, "expecting coefficient");
1812 isl_stream_push_token(s
, tok
);
1815 if (tok
->on_new_line
) {
1816 isl_stream_error(s
, tok
,
1817 "coefficient should not appear on new line");
1818 isl_stream_push_token(s
, tok
);
1821 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1822 isl_int_set(c
[pos
], tok
->u
.v
);
1823 isl_token_free(tok
);
1828 isl_basic_map_free(bmap
);
1832 static __isl_give isl_basic_map
*basic_map_read_polylib(
1833 __isl_keep isl_stream
*s
)
1836 struct isl_token
*tok
;
1837 struct isl_token
*tok2
;
1840 unsigned in
= 0, out
, local
= 0;
1841 struct isl_basic_map
*bmap
= NULL
;
1844 tok
= isl_stream_next_token(s
);
1846 isl_stream_error(s
, NULL
, "unexpected EOF");
1849 tok2
= isl_stream_next_token(s
);
1851 isl_token_free(tok
);
1852 isl_stream_error(s
, NULL
, "unexpected EOF");
1855 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1856 isl_stream_push_token(s
, tok2
);
1857 isl_stream_push_token(s
, tok
);
1858 isl_stream_error(s
, NULL
,
1859 "expecting constraint matrix dimensions");
1862 n_row
= isl_int_get_si(tok
->u
.v
);
1863 n_col
= isl_int_get_si(tok2
->u
.v
);
1864 on_new_line
= tok2
->on_new_line
;
1865 isl_token_free(tok2
);
1866 isl_token_free(tok
);
1867 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1868 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1869 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1870 tok
= isl_stream_next_token_on_same_line(s
);
1872 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1873 isl_stream_error(s
, tok
,
1874 "expecting number of output dimensions");
1875 isl_stream_push_token(s
, tok
);
1878 out
= isl_int_get_si(tok
->u
.v
);
1879 isl_token_free(tok
);
1881 tok
= isl_stream_next_token_on_same_line(s
);
1882 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1883 isl_stream_error(s
, tok
,
1884 "expecting number of input dimensions");
1886 isl_stream_push_token(s
, tok
);
1889 in
= isl_int_get_si(tok
->u
.v
);
1890 isl_token_free(tok
);
1892 tok
= isl_stream_next_token_on_same_line(s
);
1893 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1894 isl_stream_error(s
, tok
,
1895 "expecting number of existentials");
1897 isl_stream_push_token(s
, tok
);
1900 local
= isl_int_get_si(tok
->u
.v
);
1901 isl_token_free(tok
);
1903 tok
= isl_stream_next_token_on_same_line(s
);
1904 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1905 isl_stream_error(s
, tok
,
1906 "expecting number of parameters");
1908 isl_stream_push_token(s
, tok
);
1911 nparam
= isl_int_get_si(tok
->u
.v
);
1912 isl_token_free(tok
);
1913 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1914 isl_stream_error(s
, NULL
,
1915 "dimensions don't match");
1919 out
= n_col
- 2 - nparam
;
1920 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1924 for (i
= 0; i
< local
; ++i
) {
1925 int k
= isl_basic_map_alloc_div(bmap
);
1928 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1931 for (i
= 0; i
< n_row
; ++i
)
1932 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1934 tok
= isl_stream_next_token_on_same_line(s
);
1936 isl_stream_error(s
, tok
, "unexpected extra token on line");
1937 isl_stream_push_token(s
, tok
);
1941 bmap
= isl_basic_map_simplify(bmap
);
1942 bmap
= isl_basic_map_finalize(bmap
);
1945 isl_basic_map_free(bmap
);
1949 static struct isl_map
*map_read_polylib(__isl_keep isl_stream
*s
)
1951 struct isl_token
*tok
;
1952 struct isl_token
*tok2
;
1954 struct isl_map
*map
;
1956 tok
= isl_stream_next_token(s
);
1958 isl_stream_error(s
, NULL
, "unexpected EOF");
1961 tok2
= isl_stream_next_token_on_same_line(s
);
1962 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1963 isl_stream_push_token(s
, tok2
);
1964 isl_stream_push_token(s
, tok
);
1965 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1968 isl_stream_error(s
, tok2
, "unexpected token");
1969 isl_stream_push_token(s
, tok2
);
1970 isl_stream_push_token(s
, tok
);
1973 n
= isl_int_get_si(tok
->u
.v
);
1974 isl_token_free(tok
);
1976 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1978 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1980 for (i
= 1; map
&& i
< n
; ++i
)
1981 map
= isl_map_union(map
,
1982 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1987 static int optional_power(__isl_keep isl_stream
*s
)
1990 struct isl_token
*tok
;
1992 tok
= isl_stream_next_token(s
);
1995 if (tok
->type
!= '^') {
1996 isl_stream_push_token(s
, tok
);
1999 isl_token_free(tok
);
2000 tok
= isl_stream_next_token(s
);
2001 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2002 isl_stream_error(s
, tok
, "expecting exponent");
2004 isl_stream_push_token(s
, tok
);
2007 pow
= isl_int_get_si(tok
->u
.v
);
2008 isl_token_free(tok
);
2012 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2013 __isl_keep isl_map
*map
, struct vars
*v
);
2015 static __isl_give isl_pw_qpolynomial
*read_factor(__isl_keep isl_stream
*s
,
2016 __isl_keep isl_map
*map
, struct vars
*v
)
2018 isl_pw_qpolynomial
*pwqp
;
2019 struct isl_token
*tok
;
2021 tok
= next_token(s
);
2023 isl_stream_error(s
, NULL
, "unexpected EOF");
2026 if (tok
->type
== '(') {
2029 isl_token_free(tok
);
2030 pwqp
= read_term(s
, map
, v
);
2033 if (isl_stream_eat(s
, ')'))
2035 pow
= optional_power(s
);
2036 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2037 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
2038 struct isl_token
*tok2
;
2039 isl_qpolynomial
*qp
;
2041 tok2
= isl_stream_next_token(s
);
2042 if (tok2
&& tok2
->type
== '/') {
2043 isl_token_free(tok2
);
2044 tok2
= next_token(s
);
2045 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2046 isl_stream_error(s
, tok2
, "expected denominator");
2047 isl_token_free(tok
);
2048 isl_token_free(tok2
);
2051 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
2052 tok
->u
.v
, tok2
->u
.v
);
2053 isl_token_free(tok2
);
2055 isl_stream_push_token(s
, tok2
);
2056 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
2059 isl_token_free(tok
);
2060 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2061 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2062 isl_qpolynomial
*qp
;
2063 isl_token_free(tok
);
2064 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2065 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2066 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2067 isl_qpolynomial
*qp
;
2068 isl_token_free(tok
);
2069 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2070 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2071 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2073 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2075 isl_qpolynomial
*qp
;
2077 isl_token_free(tok
);
2081 vars_drop(v
, v
->n
- n
);
2082 isl_stream_error(s
, tok
, "unknown identifier");
2083 isl_token_free(tok
);
2086 isl_token_free(tok
);
2087 pow
= optional_power(s
);
2088 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2089 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2090 } else if (is_start_of_div(tok
)) {
2094 isl_stream_push_token(s
, tok
);
2095 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2096 pow
= optional_power(s
);
2097 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2098 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2099 } else if (tok
->type
== '-') {
2100 isl_token_free(tok
);
2101 pwqp
= read_factor(s
, map
, v
);
2102 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2104 isl_stream_error(s
, tok
, "unexpected isl_token");
2105 isl_stream_push_token(s
, tok
);
2109 if (isl_stream_eat_if_available(s
, '*') ||
2110 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2111 isl_pw_qpolynomial
*pwqp2
;
2113 pwqp2
= read_factor(s
, map
, v
);
2114 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2119 isl_pw_qpolynomial_free(pwqp
);
2123 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2124 __isl_keep isl_map
*map
, struct vars
*v
)
2126 struct isl_token
*tok
;
2127 isl_pw_qpolynomial
*pwqp
;
2129 pwqp
= read_factor(s
, map
, v
);
2132 tok
= next_token(s
);
2136 if (tok
->type
== '+') {
2137 isl_pw_qpolynomial
*pwqp2
;
2139 isl_token_free(tok
);
2140 pwqp2
= read_factor(s
, map
, v
);
2141 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2142 } else if (tok
->type
== '-') {
2143 isl_pw_qpolynomial
*pwqp2
;
2145 isl_token_free(tok
);
2146 pwqp2
= read_factor(s
, map
, v
);
2147 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2148 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2149 isl_int_is_neg(tok
->u
.v
)) {
2150 isl_pw_qpolynomial
*pwqp2
;
2152 isl_stream_push_token(s
, tok
);
2153 pwqp2
= read_factor(s
, map
, v
);
2154 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2156 isl_stream_push_token(s
, tok
);
2164 static __isl_give isl_map
*read_optional_formula(__isl_keep isl_stream
*s
,
2165 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2167 struct isl_token
*tok
;
2169 tok
= isl_stream_next_token(s
);
2171 isl_stream_error(s
, NULL
, "unexpected EOF");
2174 if (tok
->type
== ':' ||
2175 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2176 isl_token_free(tok
);
2177 map
= read_formula(s
, v
, map
, rational
);
2179 isl_stream_push_token(s
, tok
);
2187 static struct isl_obj
obj_read_poly(__isl_keep isl_stream
*s
,
2188 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2190 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2191 isl_pw_qpolynomial
*pwqp
;
2192 struct isl_set
*set
;
2194 pwqp
= read_term(s
, map
, v
);
2195 map
= read_optional_formula(s
, map
, v
, 0);
2196 set
= isl_map_range(map
);
2198 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2200 vars_drop(v
, v
->n
- n
);
2206 static struct isl_obj
obj_read_poly_or_fold(__isl_keep isl_stream
*s
,
2207 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2209 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2210 isl_pw_qpolynomial
*pwqp
;
2211 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2213 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2214 return obj_read_poly(s
, set
, v
, n
);
2216 if (isl_stream_eat(s
, '('))
2219 pwqp
= read_term(s
, set
, v
);
2220 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2222 while (isl_stream_eat_if_available(s
, ',')) {
2223 isl_pw_qpolynomial_fold
*pwf_i
;
2224 pwqp
= read_term(s
, set
, v
);
2225 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2227 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2230 if (isl_stream_eat(s
, ')'))
2233 set
= read_optional_formula(s
, set
, v
, 0);
2234 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2236 vars_drop(v
, v
->n
- n
);
2242 isl_pw_qpolynomial_fold_free(pwf
);
2243 obj
.type
= isl_obj_none
;
2247 static int is_rational(__isl_keep isl_stream
*s
)
2249 struct isl_token
*tok
;
2251 tok
= isl_stream_next_token(s
);
2254 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2255 isl_token_free(tok
);
2256 isl_stream_eat(s
, ':');
2260 isl_stream_push_token(s
, tok
);
2265 static struct isl_obj
obj_read_body(__isl_keep isl_stream
*s
,
2266 __isl_take isl_map
*map
, struct vars
*v
)
2268 struct isl_token
*tok
;
2269 struct isl_obj obj
= { isl_obj_set
, NULL
};
2273 rational
= is_rational(s
);
2275 map
= isl_map_set_rational(map
);
2277 if (isl_stream_next_token_is(s
, ':')) {
2278 obj
.type
= isl_obj_set
;
2279 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2283 if (!next_is_tuple(s
))
2284 return obj_read_poly_or_fold(s
, map
, v
, n
);
2286 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2289 tok
= isl_stream_next_token(s
);
2292 if (tok
->type
== ISL_TOKEN_TO
) {
2293 obj
.type
= isl_obj_map
;
2294 isl_token_free(tok
);
2295 if (!next_is_tuple(s
)) {
2296 isl_set
*set
= isl_map_domain(map
);
2297 return obj_read_poly_or_fold(s
, set
, v
, n
);
2299 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2303 map
= isl_map_domain(map
);
2304 isl_stream_push_token(s
, tok
);
2307 map
= read_optional_formula(s
, map
, v
, rational
);
2309 vars_drop(v
, v
->n
- n
);
2315 obj
.type
= isl_obj_none
;
2319 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2321 if (obj
.type
== isl_obj_map
) {
2322 obj
.v
= isl_union_map_from_map(obj
.v
);
2323 obj
.type
= isl_obj_union_map
;
2324 } else if (obj
.type
== isl_obj_set
) {
2325 obj
.v
= isl_union_set_from_set(obj
.v
);
2326 obj
.type
= isl_obj_union_set
;
2327 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2328 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2329 obj
.type
= isl_obj_union_pw_qpolynomial
;
2330 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2331 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2332 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2334 isl_assert(ctx
, 0, goto error
);
2337 obj
.type
->free(obj
.v
);
2338 obj
.type
= isl_obj_none
;
2342 static struct isl_obj
obj_add(__isl_keep isl_stream
*s
,
2343 struct isl_obj obj1
, struct isl_obj obj2
)
2345 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2346 obj1
= to_union(s
->ctx
, obj1
);
2347 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2348 obj2
= to_union(s
->ctx
, obj2
);
2349 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2350 obj1
= to_union(s
->ctx
, obj1
);
2351 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2352 obj2
= to_union(s
->ctx
, obj2
);
2353 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2354 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2355 obj1
= to_union(s
->ctx
, obj1
);
2356 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2357 obj2
.type
== isl_obj_pw_qpolynomial
)
2358 obj2
= to_union(s
->ctx
, obj2
);
2359 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2360 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2361 obj1
= to_union(s
->ctx
, obj1
);
2362 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2363 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2364 obj2
= to_union(s
->ctx
, obj2
);
2365 if (obj1
.type
!= obj2
.type
) {
2366 isl_stream_error(s
, NULL
,
2367 "attempt to combine incompatible objects");
2370 if (!obj1
.type
->add
)
2371 isl_die(s
->ctx
, isl_error_internal
,
2372 "combination not supported on object type", goto error
);
2373 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2374 obj1
= to_union(s
->ctx
, obj1
);
2375 obj2
= to_union(s
->ctx
, obj2
);
2377 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2378 obj1
= to_union(s
->ctx
, obj1
);
2379 obj2
= to_union(s
->ctx
, obj2
);
2381 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2382 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2383 obj1
= to_union(s
->ctx
, obj1
);
2384 obj2
= to_union(s
->ctx
, obj2
);
2386 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2387 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2388 obj1
= to_union(s
->ctx
, obj1
);
2389 obj2
= to_union(s
->ctx
, obj2
);
2391 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2394 obj1
.type
->free(obj1
.v
);
2395 obj2
.type
->free(obj2
.v
);
2396 obj1
.type
= isl_obj_none
;
2401 /* Are the first two tokens on "s", "domain" (either as a string
2402 * or as an identifier) followed by ":"?
2404 static int next_is_domain_colon(__isl_keep isl_stream
*s
)
2406 struct isl_token
*tok
;
2410 tok
= isl_stream_next_token(s
);
2413 if (tok
->type
!= ISL_TOKEN_IDENT
&& tok
->type
!= ISL_TOKEN_STRING
) {
2414 isl_stream_push_token(s
, tok
);
2418 name
= isl_token_get_str(s
->ctx
, tok
);
2419 res
= !strcmp(name
, "domain") && isl_stream_next_token_is(s
, ':');
2422 isl_stream_push_token(s
, tok
);
2427 /* Do the first tokens on "s" look like a schedule?
2429 * The root of a schedule is always a domain node, so the first thing
2430 * we expect in the stream is a domain key, i.e., "domain" followed
2431 * by ":". If the schedule was printed in YAML flow style, then
2432 * we additionally expect a "{" to open the outer mapping.
2434 static int next_is_schedule(__isl_keep isl_stream
*s
)
2436 struct isl_token
*tok
;
2439 tok
= isl_stream_next_token(s
);
2442 if (tok
->type
!= '{') {
2443 isl_stream_push_token(s
, tok
);
2444 return next_is_domain_colon(s
);
2447 is_schedule
= next_is_domain_colon(s
);
2448 isl_stream_push_token(s
, tok
);
2453 /* Read an isl_schedule from "s" and store it in an isl_obj.
2455 static struct isl_obj
schedule_read(__isl_keep isl_stream
*s
)
2459 obj
.type
= isl_obj_schedule
;
2460 obj
.v
= isl_stream_read_schedule(s
);
2465 static struct isl_obj
obj_read(__isl_keep isl_stream
*s
)
2467 isl_map
*map
= NULL
;
2468 struct isl_token
*tok
;
2469 struct vars
*v
= NULL
;
2470 struct isl_obj obj
= { isl_obj_set
, NULL
};
2472 if (next_is_schedule(s
))
2473 return schedule_read(s
);
2475 tok
= next_token(s
);
2477 isl_stream_error(s
, NULL
, "unexpected EOF");
2480 if (tok
->type
== ISL_TOKEN_VALUE
) {
2481 struct isl_token
*tok2
;
2482 struct isl_map
*map
;
2484 tok2
= isl_stream_next_token(s
);
2485 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2486 isl_int_is_neg(tok2
->u
.v
)) {
2488 isl_stream_push_token(s
, tok2
);
2489 obj
.type
= isl_obj_val
;
2490 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2491 isl_token_free(tok
);
2494 isl_stream_push_token(s
, tok2
);
2495 isl_stream_push_token(s
, tok
);
2496 map
= map_read_polylib(s
);
2499 if (isl_map_may_be_set(map
))
2500 obj
.v
= isl_map_range(map
);
2502 obj
.type
= isl_obj_map
;
2507 v
= vars_new(s
->ctx
);
2509 isl_stream_push_token(s
, tok
);
2512 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2513 if (tok
->type
== '[') {
2514 isl_stream_push_token(s
, tok
);
2515 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2518 tok
= isl_stream_next_token(s
);
2519 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2520 isl_stream_error(s
, tok
, "expecting '->'");
2522 isl_stream_push_token(s
, tok
);
2525 isl_token_free(tok
);
2526 tok
= isl_stream_next_token(s
);
2528 if (!tok
|| tok
->type
!= '{') {
2529 isl_stream_error(s
, tok
, "expecting '{'");
2531 isl_stream_push_token(s
, tok
);
2534 isl_token_free(tok
);
2536 tok
= isl_stream_next_token(s
);
2539 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2540 isl_token_free(tok
);
2541 if (isl_stream_eat(s
, '='))
2543 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2546 } else if (tok
->type
== '}') {
2547 obj
.type
= isl_obj_union_set
;
2548 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2549 isl_token_free(tok
);
2552 isl_stream_push_token(s
, tok
);
2557 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2558 if (o
.type
== isl_obj_none
|| !o
.v
)
2563 obj
= obj_add(s
, obj
, o
);
2564 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2567 tok
= isl_stream_next_token(s
);
2568 if (!tok
|| tok
->type
!= ';')
2570 isl_token_free(tok
);
2571 if (isl_stream_next_token_is(s
, '}')) {
2572 tok
= isl_stream_next_token(s
);
2577 if (tok
&& tok
->type
== '}') {
2578 isl_token_free(tok
);
2580 isl_stream_error(s
, tok
, "unexpected isl_token");
2582 isl_token_free(tok
);
2592 obj
.type
->free(obj
.v
);
2599 struct isl_obj
isl_stream_read_obj(__isl_keep isl_stream
*s
)
2604 __isl_give isl_map
*isl_stream_read_map(__isl_keep isl_stream
*s
)
2610 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2611 obj
.type
== isl_obj_set
, goto error
);
2613 if (obj
.type
== isl_obj_set
)
2614 obj
.v
= isl_map_from_range(obj
.v
);
2618 obj
.type
->free(obj
.v
);
2622 __isl_give isl_set
*isl_stream_read_set(__isl_keep isl_stream
*s
)
2628 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2629 obj
.v
= isl_map_range(obj
.v
);
2630 obj
.type
= isl_obj_set
;
2632 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2637 obj
.type
->free(obj
.v
);
2641 __isl_give isl_union_map
*isl_stream_read_union_map(__isl_keep isl_stream
*s
)
2646 if (obj
.type
== isl_obj_map
) {
2647 obj
.type
= isl_obj_union_map
;
2648 obj
.v
= isl_union_map_from_map(obj
.v
);
2650 if (obj
.type
== isl_obj_set
) {
2651 obj
.type
= isl_obj_union_set
;
2652 obj
.v
= isl_union_set_from_set(obj
.v
);
2654 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2655 isl_union_set_is_empty(obj
.v
))
2656 obj
.type
= isl_obj_union_map
;
2657 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2658 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2662 obj
.type
->free(obj
.v
);
2666 __isl_give isl_union_set
*isl_stream_read_union_set(__isl_keep isl_stream
*s
)
2671 if (obj
.type
== isl_obj_set
) {
2672 obj
.type
= isl_obj_union_set
;
2673 obj
.v
= isl_union_set_from_set(obj
.v
);
2676 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2680 obj
.type
->free(obj
.v
);
2684 static __isl_give isl_basic_map
*basic_map_read(__isl_keep isl_stream
*s
)
2687 struct isl_map
*map
;
2688 struct isl_basic_map
*bmap
;
2691 if (obj
.v
&& (obj
.type
!= isl_obj_map
&& obj
.type
!= isl_obj_set
))
2692 isl_die(s
->ctx
, isl_error_invalid
, "not a (basic) set or map",
2699 isl_die(s
->ctx
, isl_error_invalid
,
2700 "set or map description involves "
2701 "more than one disjunct", goto error
);
2704 bmap
= isl_basic_map_empty(isl_map_get_space(map
));
2706 bmap
= isl_basic_map_copy(map
->p
[0]);
2712 obj
.type
->free(obj
.v
);
2716 static __isl_give isl_basic_set
*basic_set_read(__isl_keep isl_stream
*s
)
2718 isl_basic_map
*bmap
;
2719 bmap
= basic_map_read(s
);
2722 if (!isl_basic_map_may_be_set(bmap
))
2723 isl_die(s
->ctx
, isl_error_invalid
,
2724 "input is not a set", goto error
);
2725 return isl_basic_map_range(bmap
);
2727 isl_basic_map_free(bmap
);
2731 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2734 struct isl_basic_map
*bmap
;
2735 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2738 bmap
= basic_map_read(s
);
2743 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2746 isl_basic_set
*bset
;
2747 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2750 bset
= basic_set_read(s
);
2755 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2758 struct isl_basic_map
*bmap
;
2759 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2762 bmap
= basic_map_read(s
);
2767 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2770 isl_basic_set
*bset
;
2771 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2774 bset
= basic_set_read(s
);
2779 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2782 struct isl_map
*map
;
2783 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2786 map
= isl_stream_read_map(s
);
2791 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2794 struct isl_map
*map
;
2795 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2798 map
= isl_stream_read_map(s
);
2803 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2807 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2810 set
= isl_stream_read_set(s
);
2815 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2819 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2822 set
= isl_stream_read_set(s
);
2827 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2830 isl_union_map
*umap
;
2831 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2834 umap
= isl_stream_read_union_map(s
);
2839 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2842 isl_union_map
*umap
;
2843 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2846 umap
= isl_stream_read_union_map(s
);
2851 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2854 isl_union_set
*uset
;
2855 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2858 uset
= isl_stream_read_union_set(s
);
2863 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2866 isl_union_set
*uset
;
2867 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2870 uset
= isl_stream_read_union_set(s
);
2875 static __isl_give isl_vec
*isl_vec_read_polylib(__isl_keep isl_stream
*s
)
2877 struct isl_vec
*vec
= NULL
;
2878 struct isl_token
*tok
;
2882 tok
= isl_stream_next_token(s
);
2883 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2884 isl_stream_error(s
, tok
, "expecting vector length");
2888 size
= isl_int_get_si(tok
->u
.v
);
2889 isl_token_free(tok
);
2891 vec
= isl_vec_alloc(s
->ctx
, size
);
2893 for (j
= 0; j
< size
; ++j
) {
2894 tok
= isl_stream_next_token(s
);
2895 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2896 isl_stream_error(s
, tok
, "expecting constant value");
2899 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2900 isl_token_free(tok
);
2905 isl_token_free(tok
);
2910 static __isl_give isl_vec
*vec_read(__isl_keep isl_stream
*s
)
2912 return isl_vec_read_polylib(s
);
2915 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2918 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2926 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2927 __isl_keep isl_stream
*s
)
2933 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2938 obj
.type
->free(obj
.v
);
2942 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2945 isl_pw_qpolynomial
*pwqp
;
2946 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2949 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2954 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2957 isl_pw_qpolynomial
*pwqp
;
2958 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2961 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2966 /* Is the next token an identifer not in "v"?
2968 static int next_is_fresh_ident(__isl_keep isl_stream
*s
, struct vars
*v
)
2972 struct isl_token
*tok
;
2974 tok
= isl_stream_next_token(s
);
2977 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2978 isl_stream_push_token(s
, tok
);
2980 vars_drop(v
, v
->n
- n
);
2985 /* First read the domain of the affine expression, which may be
2986 * a parameter space or a set.
2987 * The tricky part is that we don't know if the domain is a set or not,
2988 * so when we are trying to read the domain, we may actually be reading
2989 * the affine expression itself (defined on a parameter domains)
2990 * If the tuple we are reading is named, we assume it's the domain.
2991 * Also, if inside the tuple, the first thing we find is a nested tuple
2992 * or a new identifier, we again assume it's the domain.
2993 * Otherwise, we assume we are reading an affine expression.
2995 static __isl_give isl_set
*read_aff_domain(__isl_keep isl_stream
*s
,
2996 __isl_take isl_set
*dom
, struct vars
*v
)
2998 struct isl_token
*tok
;
3000 tok
= isl_stream_next_token(s
);
3001 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
3002 isl_stream_push_token(s
, tok
);
3003 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
3005 if (!tok
|| tok
->type
!= '[') {
3006 isl_stream_error(s
, tok
, "expecting '['");
3009 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
3010 isl_stream_push_token(s
, tok
);
3011 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
3013 isl_stream_push_token(s
, tok
);
3018 isl_stream_push_token(s
, tok
);
3023 /* Read an affine expression from "s".
3025 __isl_give isl_aff
*isl_stream_read_aff(__isl_keep isl_stream
*s
)
3030 ma
= isl_stream_read_multi_aff(s
);
3033 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
3034 isl_die(s
->ctx
, isl_error_invalid
,
3035 "expecting single affine expression",
3038 aff
= isl_multi_aff_get_aff(ma
, 0);
3039 isl_multi_aff_free(ma
);
3042 isl_multi_aff_free(ma
);
3046 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3048 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(__isl_keep isl_stream
*s
,
3049 __isl_take isl_set
*dom
, struct vars
*v
)
3051 isl_pw_aff
*pwaff
= NULL
;
3053 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
3056 if (isl_stream_eat(s
, '['))
3059 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
3061 if (isl_stream_eat(s
, ']'))
3064 dom
= read_optional_formula(s
, dom
, v
, 0);
3065 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3070 isl_pw_aff_free(pwaff
);
3074 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(__isl_keep isl_stream
*s
)
3077 isl_set
*dom
= NULL
;
3079 isl_pw_aff
*pa
= NULL
;
3082 v
= vars_new(s
->ctx
);
3086 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3087 if (next_is_tuple(s
)) {
3088 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3089 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3092 if (isl_stream_eat(s
, '{'))
3096 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3097 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3098 vars_drop(v
, v
->n
- n
);
3100 while (isl_stream_eat_if_available(s
, ';')) {
3104 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3105 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3106 vars_drop(v
, v
->n
- n
);
3108 pa
= isl_pw_aff_union_add(pa
, pa_i
);
3111 if (isl_stream_eat(s
, '}'))
3120 isl_pw_aff_free(pa
);
3124 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3127 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3130 aff
= isl_stream_read_aff(s
);
3135 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3138 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3141 pa
= isl_stream_read_pw_aff(s
);
3146 /* Read an isl_pw_multi_aff from "s".
3147 * We currently read a generic object and if it turns out to be a set or
3148 * a map, we convert that to an isl_pw_multi_aff.
3149 * It would be more efficient if we were to construct the isl_pw_multi_aff
3152 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(
3153 __isl_keep isl_stream
*s
)
3161 if (obj
.type
== isl_obj_map
)
3162 return isl_pw_multi_aff_from_map(obj
.v
);
3163 if (obj
.type
== isl_obj_set
)
3164 return isl_pw_multi_aff_from_set(obj
.v
);
3166 obj
.type
->free(obj
.v
);
3167 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3171 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3174 isl_pw_multi_aff
*pma
;
3175 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3178 pma
= isl_stream_read_pw_multi_aff(s
);
3183 /* Read an isl_union_pw_multi_aff from "s".
3184 * We currently read a generic object and if it turns out to be a set or
3185 * a map, we convert that to an isl_union_pw_multi_aff.
3186 * It would be more efficient if we were to construct
3187 * the isl_union_pw_multi_aff directly.
3189 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3190 __isl_keep isl_stream
*s
)
3198 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3199 obj
= to_union(s
->ctx
, obj
);
3200 if (obj
.type
== isl_obj_union_map
)
3201 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3202 if (obj
.type
== isl_obj_union_set
)
3203 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3205 obj
.type
->free(obj
.v
);
3206 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3210 /* Read an isl_union_pw_multi_aff from "str".
3212 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3213 isl_ctx
*ctx
, const char *str
)
3215 isl_union_pw_multi_aff
*upma
;
3216 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3219 upma
= isl_stream_read_union_pw_multi_aff(s
);
3224 /* Assuming "pa" represents a single affine expression defined on a universe
3225 * domain, extract this affine expression.
3227 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3234 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3235 "expecting single affine expression",
3237 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3238 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3239 "expecting universe domain",
3242 aff
= isl_aff_copy(pa
->p
[0].aff
);
3243 isl_pw_aff_free(pa
);
3246 isl_pw_aff_free(pa
);
3250 /* This function is called for each element in a tuple inside
3251 * isl_stream_read_multi_val.
3252 * Read an isl_val from "s" and add it to *list.
3254 static __isl_give isl_space
*read_val_el(__isl_keep isl_stream
*s
,
3255 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3257 isl_val_list
**list
= (isl_val_list
**) user
;
3260 val
= isl_stream_read_val(s
);
3261 *list
= isl_val_list_add(*list
, val
);
3263 return isl_space_free(space
);
3268 /* Read an isl_multi_val from "s".
3270 * We first read a tuple space, collecting the element values in a list.
3271 * Then we create an isl_multi_val from the space and the isl_val_list.
3273 __isl_give isl_multi_val
*isl_stream_read_multi_val(__isl_keep isl_stream
*s
)
3276 isl_set
*dom
= NULL
;
3278 isl_multi_val
*mv
= NULL
;
3281 v
= vars_new(s
->ctx
);
3285 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3286 if (next_is_tuple(s
)) {
3287 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3288 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3291 if (!isl_set_plain_is_universe(dom
))
3292 isl_die(s
->ctx
, isl_error_invalid
,
3293 "expecting universe parameter domain", goto error
);
3294 if (isl_stream_eat(s
, '{'))
3297 space
= isl_set_get_space(dom
);
3299 list
= isl_val_list_alloc(s
->ctx
, 0);
3300 space
= read_tuple_space(s
, v
, space
, 1, 0, &read_val_el
, &list
);
3301 mv
= isl_multi_val_from_val_list(space
, list
);
3303 if (isl_stream_eat(s
, '}'))
3312 isl_multi_val_free(mv
);
3316 /* Read an isl_multi_val from "str".
3318 __isl_give isl_multi_val
*isl_multi_val_read_from_str(isl_ctx
*ctx
,
3322 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3325 mv
= isl_stream_read_multi_val(s
);
3330 /* Read a multi-affine expression from "s".
3331 * If the multi-affine expression has a domain, then the tuple
3332 * representing this domain cannot involve any affine expressions.
3333 * The tuple representing the actual expressions needs to consist
3334 * of only affine expressions. Moreover, these expressions can
3335 * only depend on parameters and input dimensions and not on other
3336 * output dimensions.
3338 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(__isl_keep isl_stream
*s
)
3341 isl_set
*dom
= NULL
;
3342 isl_multi_pw_aff
*tuple
= NULL
;
3344 isl_space
*space
, *dom_space
;
3345 isl_multi_aff
*ma
= NULL
;
3347 v
= vars_new(s
->ctx
);
3351 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3352 if (next_is_tuple(s
)) {
3353 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3354 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3357 if (!isl_set_plain_is_universe(dom
))
3358 isl_die(s
->ctx
, isl_error_invalid
,
3359 "expecting universe parameter domain", goto error
);
3360 if (isl_stream_eat(s
, '{'))
3363 tuple
= read_tuple(s
, v
, 0, 0);
3366 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3371 has_expr
= tuple_has_expr(tuple
);
3375 isl_die(s
->ctx
, isl_error_invalid
,
3376 "expecting universe domain", goto error
);
3377 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3378 set
= isl_set_universe(space
);
3379 dom
= isl_set_intersect_params(set
, dom
);
3380 isl_multi_pw_aff_free(tuple
);
3381 tuple
= read_tuple(s
, v
, 0, 0);
3386 if (isl_stream_eat(s
, '}'))
3389 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3390 dim
= isl_set_dim(dom
, isl_dim_all
);
3391 dom_space
= isl_set_get_space(dom
);
3392 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3393 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3394 if (!isl_space_is_params(dom_space
))
3395 space
= isl_space_map_from_domain_and_range(
3396 isl_space_copy(dom_space
), space
);
3397 isl_space_free(dom_space
);
3398 ma
= isl_multi_aff_alloc(space
);
3400 for (i
= 0; i
< n
; ++i
) {
3403 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3404 aff
= aff_from_pw_aff(pa
);
3407 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3409 isl_die(s
->ctx
, isl_error_invalid
,
3410 "not an affine expression", goto error
);
3412 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3413 space
= isl_multi_aff_get_domain_space(ma
);
3414 aff
= isl_aff_reset_domain_space(aff
, space
);
3415 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3418 isl_multi_pw_aff_free(tuple
);
3423 isl_multi_pw_aff_free(tuple
);
3426 isl_multi_aff_free(ma
);
3430 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3433 isl_multi_aff
*maff
;
3434 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3437 maff
= isl_stream_read_multi_aff(s
);
3442 /* Read an isl_multi_pw_aff from "s".
3444 * The input format is similar to that of map, except that any conditions
3445 * on the domains should be specified inside the tuple since each
3446 * piecewise affine expression may have a different domain.
3448 * Since we do not know in advance if the isl_multi_pw_aff lives
3449 * in a set or a map space, we first read the first tuple and check
3450 * if it is followed by a "->". If so, we convert the tuple into
3451 * the domain of the isl_multi_pw_aff and read in the next tuple.
3452 * This tuple (or the first tuple if it was not followed by a "->")
3453 * is then converted into the isl_multi_pw_aff.
3455 * Note that the function read_tuple accepts tuples where some output or
3456 * set dimensions are defined in terms of other output or set dimensions
3457 * since this function is also used to read maps. As a special case,
3458 * read_tuple also accept dimensions that are defined in terms of themselves
3459 * (i.e., that are not defined).
3460 * These cases are not allowed when reading am isl_multi_pw_aff so we check
3461 * that the definition of the output/set dimensions does not involve any
3462 * output/set dimensions.
3463 * We then drop the output dimensions from the domain of the result
3464 * of read_tuple (which is of the form [input, output] -> [output],
3465 * with anonymous domain) and reset the space.
3467 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(
3468 __isl_keep isl_stream
*s
)
3471 isl_set
*dom
= NULL
;
3472 isl_multi_pw_aff
*tuple
= NULL
;
3474 isl_space
*space
, *dom_space
;
3475 isl_multi_pw_aff
*mpa
= NULL
;
3477 v
= vars_new(s
->ctx
);
3481 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3482 if (next_is_tuple(s
)) {
3483 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3484 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3487 if (isl_stream_eat(s
, '{'))
3490 tuple
= read_tuple(s
, v
, 0, 0);
3493 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3494 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3495 dom
= isl_map_domain(map
);
3496 tuple
= read_tuple(s
, v
, 0, 0);
3501 if (isl_stream_eat(s
, '}'))
3504 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3505 dim
= isl_set_dim(dom
, isl_dim_all
);
3506 dom_space
= isl_set_get_space(dom
);
3507 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3508 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3509 if (!isl_space_is_params(dom_space
))
3510 space
= isl_space_map_from_domain_and_range(
3511 isl_space_copy(dom_space
), space
);
3512 isl_space_free(dom_space
);
3513 mpa
= isl_multi_pw_aff_alloc(space
);
3515 for (i
= 0; i
< n
; ++i
) {
3517 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3520 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3521 isl_pw_aff_free(pa
);
3522 isl_die(s
->ctx
, isl_error_invalid
,
3523 "not an affine expression", goto error
);
3525 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3526 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3527 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3528 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3531 isl_multi_pw_aff_free(tuple
);
3533 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3536 isl_multi_pw_aff_free(tuple
);
3539 isl_multi_pw_aff_free(mpa
);
3543 /* Read an isl_multi_pw_aff from "str".
3545 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3548 isl_multi_pw_aff
*mpa
;
3549 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3552 mpa
= isl_stream_read_multi_pw_aff(s
);
3557 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3559 static __isl_give isl_union_pw_aff
*read_union_pw_aff_with_dom(
3560 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3563 isl_union_pw_aff
*upa
= NULL
;
3568 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3569 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3570 vars_drop(v
, v
->n
- n
);
3572 upa
= isl_union_pw_aff_from_pw_aff(pa
);
3574 while (isl_stream_eat_if_available(s
, ';')) {
3576 isl_union_pw_aff
*upa_i
;
3579 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3580 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3581 vars_drop(v
, v
->n
- n
);
3583 upa_i
= isl_union_pw_aff_from_pw_aff(pa_i
);
3584 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
3591 /* This function is called for each element in a tuple inside
3592 * isl_stream_read_multi_union_pw_aff.
3594 * Read a '{', the union piecewise affine expression body and a '}' and
3595 * add the isl_union_pw_aff to *list.
3597 static __isl_give isl_space
*read_union_pw_aff_el(__isl_keep isl_stream
*s
,
3598 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3601 isl_union_pw_aff
*upa
;
3602 isl_union_pw_aff_list
**list
= (isl_union_pw_aff_list
**) user
;
3604 dom
= isl_set_universe(isl_space_params(isl_space_copy(space
)));
3605 if (isl_stream_eat(s
, '{'))
3607 upa
= read_union_pw_aff_with_dom(s
, dom
, v
);
3608 *list
= isl_union_pw_aff_list_add(*list
, upa
);
3609 if (isl_stream_eat(s
, '}'))
3610 return isl_space_free(space
);
3612 return isl_space_free(space
);
3616 return isl_space_free(space
);
3619 /* Do the next tokens in "s" correspond to an empty tuple?
3620 * In particular, does the stream start with a '[', followed by a ']',
3621 * not followed by a "->"?
3623 static int next_is_empty_tuple(__isl_keep isl_stream
*s
)
3625 struct isl_token
*tok
, *tok2
, *tok3
;
3626 int is_empty_tuple
= 0;
3628 tok
= isl_stream_next_token(s
);
3631 if (tok
->type
!= '[') {
3632 isl_stream_push_token(s
, tok
);
3636 tok2
= isl_stream_next_token(s
);
3637 if (tok2
&& tok2
->type
== ']') {
3638 tok3
= isl_stream_next_token(s
);
3639 is_empty_tuple
= !tok
|| tok
->type
!= ISL_TOKEN_TO
;
3641 isl_stream_push_token(s
, tok3
);
3644 isl_stream_push_token(s
, tok2
);
3645 isl_stream_push_token(s
, tok
);
3647 return is_empty_tuple
;
3650 /* Do the next tokens in "s" correspond to a tuple of parameters?
3651 * In particular, does the stream start with a '[' that is not
3652 * followed by a '{' or a nested tuple?
3654 static int next_is_param_tuple(__isl_keep isl_stream
*s
)
3656 struct isl_token
*tok
, *tok2
;
3659 tok
= isl_stream_next_token(s
);
3662 if (tok
->type
!= '[' || next_is_tuple(s
)) {
3663 isl_stream_push_token(s
, tok
);
3667 tok2
= isl_stream_next_token(s
);
3668 is_tuple
= tok2
&& tok2
->type
!= '{';
3670 isl_stream_push_token(s
, tok2
);
3671 isl_stream_push_token(s
, tok
);
3676 /* Read an isl_multi_union_pw_aff from "s".
3678 * The input has the form
3680 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3684 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3686 * We first check for the special case of an empty tuple "[]".
3687 * Then we check if there are any parameters.
3688 * Finally, we read the tuple, collecting the individual isl_union_pw_aff
3689 * elements in a list and construct the result from the tuple space and
3692 __isl_give isl_multi_union_pw_aff
*isl_stream_read_multi_union_pw_aff(
3693 __isl_keep isl_stream
*s
)
3696 isl_set
*dom
= NULL
;
3698 isl_multi_union_pw_aff
*mupa
= NULL
;
3699 isl_union_pw_aff_list
*list
;
3701 if (next_is_empty_tuple(s
)) {
3702 if (isl_stream_eat(s
, '['))
3704 if (isl_stream_eat(s
, ']'))
3706 space
= isl_space_set_alloc(s
->ctx
, 0, 0);
3707 return isl_multi_union_pw_aff_zero(space
);
3710 v
= vars_new(s
->ctx
);
3714 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3715 if (next_is_param_tuple(s
)) {
3716 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3717 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3720 space
= isl_set_get_space(dom
);
3722 list
= isl_union_pw_aff_list_alloc(s
->ctx
, 0);
3723 space
= read_tuple_space(s
, v
, space
, 1, 0,
3724 &read_union_pw_aff_el
, &list
);
3725 mupa
= isl_multi_union_pw_aff_from_union_pw_aff_list(space
, list
);
3733 isl_multi_union_pw_aff_free(mupa
);
3737 /* Read an isl_multi_union_pw_aff from "str".
3739 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_read_from_str(
3740 isl_ctx
*ctx
, const char *str
)
3742 isl_multi_union_pw_aff
*mupa
;
3743 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3746 mupa
= isl_stream_read_multi_union_pw_aff(s
);
3751 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3752 __isl_keep isl_stream
*s
)
3757 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3758 obj
.type
= isl_obj_union_pw_qpolynomial
;
3759 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3762 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3767 obj
.type
->free(obj
.v
);
3771 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3772 isl_ctx
*ctx
, const char *str
)
3774 isl_union_pw_qpolynomial
*upwqp
;
3775 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3778 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);