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
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_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
33 struct variable
*next
;
42 static struct vars
*vars_new(struct isl_ctx
*ctx
)
45 v
= isl_alloc_type(ctx
, struct vars
);
54 static void variable_free(struct variable
*var
)
57 struct variable
*next
= var
->next
;
64 static void vars_free(struct vars
*v
)
72 static void vars_drop(struct vars
*v
, int n
)
83 struct variable
*next
= var
->next
;
91 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
95 var
= isl_calloc_type(v
->ctx
, struct variable
);
98 var
->name
= strdup(name
);
99 var
->name
[len
] = '\0';
108 static int vars_pos(struct vars
*v
, const char *s
, int len
)
115 for (q
= v
->v
; q
; q
= q
->next
) {
116 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
123 v
->v
= variable_new(v
, s
, len
, v
->n
);
131 static int vars_add_anon(struct vars
*v
)
133 v
->v
= variable_new(v
, "", 0, v
->n
);
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
)
153 if (!isl_stream_eat_if_available(s
, '^'))
155 tok2
= isl_stream_next_token(s
);
156 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
157 isl_stream_error(s
, tok2
, "expecting constant value");
161 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
163 isl_token_free(tok2
);
167 isl_token_free(tok2
);
171 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
173 struct isl_token
*tok
;
176 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
177 isl_stream_error(s
, tok
, "expecting constant value");
181 isl_int_mul(*f
, *f
, tok
->u
.v
);
185 if (isl_stream_eat_if_available(s
, '*'))
186 return accept_cst_factor(s
, f
);
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
;
208 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
209 isl_stream_error(s
, tok
, "expecting constant value");
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
);
223 isl_pw_aff_free(aff
);
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
;
240 tok
= isl_stream_next_token(s
);
243 min
= tok
->type
== ISL_TOKEN_MIN
;
246 if (isl_stream_eat(s
, '('))
249 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
253 if (isl_stream_eat(s
, ')'))
257 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
260 isl_pw_aff_list_free(list
);
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
;
270 isl_pw_aff
*pwaff
= NULL
;
272 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
274 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
277 if (isl_stream_eat(s
, '('))
280 if (isl_stream_eat(s
, '['))
284 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
287 if (isl_stream_eat(s
, ','))
293 if (tok
->type
!= ISL_TOKEN_VALUE
) {
294 isl_stream_error(s
, tok
, "expected denominator");
295 isl_stream_push_token(s
, tok
);
298 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
303 pwaff
= isl_pw_aff_ceil(pwaff
);
305 pwaff
= isl_pw_aff_floor(pwaff
);
308 if (isl_stream_eat(s
, ')'))
311 if (isl_stream_eat(s
, ']'))
319 isl_pw_aff_free(pwaff
);
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
;
331 isl_stream_error(s
, NULL
, "unexpected EOF");
335 if (tok
->type
== ISL_TOKEN_AFF
) {
336 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
338 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
340 int pos
= vars_pos(v
, tok
->u
.s
, -1);
346 vars_drop(v
, v
->n
- n
);
347 isl_stream_error(s
, tok
, "unknown identifier");
351 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
354 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
355 res
= isl_pw_aff_from_aff(aff
);
357 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
358 if (isl_stream_eat_if_available(s
, '*')) {
359 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
360 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
364 ls
= isl_local_space_from_space(isl_space_copy(dim
));
365 aff
= isl_aff_zero_on_domain(ls
);
366 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
367 res
= isl_pw_aff_from_aff(aff
);
370 } else if (tok
->type
== '(') {
373 res
= accept_affine(s
, isl_space_copy(dim
), v
);
376 if (isl_stream_eat(s
, ')'))
378 } else if (tok
->type
== '[' ||
379 tok
->type
== ISL_TOKEN_FLOORD
||
380 tok
->type
== ISL_TOKEN_CEILD
) {
381 isl_stream_push_token(s
, tok
);
383 res
= accept_div(s
, isl_space_copy(dim
), v
);
384 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
385 isl_stream_push_token(s
, tok
);
387 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
389 isl_stream_error(s
, tok
, "expecting factor");
392 if (isl_stream_eat_if_available(s
, '%') ||
393 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
395 return affine_mod(s
, v
, res
);
397 if (isl_stream_eat_if_available(s
, '*')) {
400 isl_int_set_si(f
, 1);
401 if (accept_cst_factor(s
, &f
) < 0) {
405 res
= isl_pw_aff_scale(res
, f
);
408 if (isl_stream_eat_if_available(s
, '/')) {
411 isl_int_set_si(f
, 1);
412 if (accept_cst_factor(s
, &f
) < 0) {
416 res
= isl_pw_aff_scale_down(res
, f
);
425 isl_pw_aff_free(res
);
430 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
435 space
= isl_pw_aff_get_domain_space(pwaff
);
436 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
437 aff
= isl_aff_add_constant(aff
, v
);
439 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
442 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
443 __isl_take isl_space
*dim
, struct vars
*v
)
445 struct isl_token
*tok
= NULL
;
450 ls
= isl_local_space_from_space(isl_space_copy(dim
));
451 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
458 isl_stream_error(s
, NULL
, "unexpected EOF");
461 if (tok
->type
== '-') {
466 if (tok
->type
== '(' || tok
->type
== '[' ||
467 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
468 tok
->type
== ISL_TOKEN_FLOORD
||
469 tok
->type
== ISL_TOKEN_CEILD
||
470 tok
->type
== ISL_TOKEN_IDENT
||
471 tok
->type
== ISL_TOKEN_AFF
) {
473 isl_stream_push_token(s
, tok
);
475 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
477 res
= isl_pw_aff_sub(res
, term
);
479 res
= isl_pw_aff_add(res
, term
);
483 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
485 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
486 if (isl_stream_eat_if_available(s
, '*') ||
487 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
489 term
= accept_affine_factor(s
,
490 isl_space_copy(dim
), v
);
491 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
492 res
= isl_pw_aff_add(res
, term
);
496 res
= add_cst(res
, tok
->u
.v
);
500 isl_stream_error(s
, tok
, "unexpected isl_token");
501 isl_stream_push_token(s
, tok
);
502 isl_pw_aff_free(res
);
509 if (tok
&& tok
->type
== '-') {
512 } else if (tok
&& tok
->type
== '+') {
515 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
516 isl_int_is_neg(tok
->u
.v
)) {
517 isl_stream_push_token(s
, tok
);
520 isl_stream_push_token(s
, tok
);
530 isl_pw_aff_free(res
);
534 static int is_comparator(struct isl_token
*tok
)
552 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
553 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
554 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
555 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
557 /* Accept a ternary operator, given the first argument.
559 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
560 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
563 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
568 if (isl_stream_eat(s
, '?'))
571 dim
= isl_space_wrap(isl_map_get_space(cond
));
572 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
576 if (isl_stream_eat(s
, ':'))
579 dim
= isl_pw_aff_get_domain_space(pwaff1
);
580 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
584 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
585 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
588 isl_pw_aff_free(pwaff1
);
589 isl_pw_aff_free(pwaff2
);
593 /* Accept an affine expression that may involve ternary operators.
594 * We first read an affine expression.
595 * If it is not followed by a comparison operator, we simply return it.
596 * Otherwise, we assume the affine epxression is part of the first
597 * argument of a ternary operator and try to parse that.
599 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
600 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
605 struct isl_token
*tok
;
606 int line
= -1, col
= -1;
609 tok
= isl_stream_next_token(s
);
613 isl_stream_push_token(s
, tok
);
616 pwaff
= accept_affine(s
, dim
, v
);
618 pwaff
= isl_pw_aff_set_rational(pwaff
);
622 tok
= isl_stream_next_token(s
);
624 return isl_pw_aff_free(pwaff
);
626 is_comp
= is_comparator(tok
);
627 isl_stream_push_token(s
, tok
);
631 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
633 return isl_pw_aff_free(pwaff
);
634 tok
->type
= ISL_TOKEN_AFF
;
635 tok
->u
.pwaff
= pwaff
;
637 space
= isl_pw_aff_get_domain_space(pwaff
);
638 cond
= isl_map_universe(isl_space_unwrap(space
));
640 isl_stream_push_token(s
, tok
);
642 cond
= read_disjuncts(s
, v
, cond
, rational
);
644 return accept_ternary(s
, cond
, v
, rational
);
647 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
648 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
655 if (type
== isl_dim_param
)
656 pos
= isl_map_dim(map
, isl_dim_param
);
658 pos
= isl_map_dim(map
, isl_dim_in
);
659 if (type
== isl_dim_out
)
660 pos
+= isl_map_dim(map
, isl_dim_out
);
665 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
667 def_map
= isl_map_from_pw_aff(def
);
668 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
669 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
671 map
= isl_map_intersect(map
, def_map
);
676 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
677 __isl_take isl_space
*dim
, struct vars
*v
)
680 isl_pw_aff_list
*list
;
681 struct isl_token
*tok
= NULL
;
683 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
684 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
689 tok
= isl_stream_next_token(s
);
691 isl_stream_error(s
, NULL
, "unexpected EOF");
694 if (tok
->type
!= ',') {
695 isl_stream_push_token(s
, tok
);
700 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
701 list
= isl_pw_aff_list_concat(list
,
702 isl_pw_aff_list_from_pw_aff(pwaff
));
711 isl_pw_aff_list_free(list
);
715 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
716 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
718 struct isl_token
*tok
;
720 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
724 if (tok
->type
!= ISL_TOKEN_IDENT
)
727 p
= vars_pos(v
, tok
->u
.s
, -1);
731 isl_stream_error(s
, tok
, "expecting unique identifier");
735 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
738 tok
= isl_stream_next_token(s
);
739 if (tok
&& tok
->type
== '=') {
741 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
742 tok
= isl_stream_next_token(s
);
745 if (!tok
|| tok
->type
!= ',')
751 isl_stream_push_token(s
, tok
);
760 static int next_is_tuple(struct isl_stream
*s
)
762 struct isl_token
*tok
;
765 tok
= isl_stream_next_token(s
);
768 if (tok
->type
== '[') {
769 isl_stream_push_token(s
, tok
);
772 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
773 isl_stream_push_token(s
, tok
);
777 is_tuple
= isl_stream_next_token_is(s
, '[');
779 isl_stream_push_token(s
, tok
);
784 /* Allocate an initial tuple with zero dimensions and an anonymous,
785 * unstructured space.
786 * A tuple is represented as an isl_multi_pw_aff.
787 * The range space is the space of the tuple.
788 * The domain space is an anonymous space
789 * with a dimension for each variable in the set of variables in "v".
790 * If a given dimension is not defined in terms of earlier dimensions in
791 * the input, then the corresponding isl_pw_aff is set equal to one time
792 * the variable corresponding to the dimension being defined.
794 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
796 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
799 /* Is "pa" an expression in term of earlier dimensions?
800 * The alternative is that the dimension is defined to be equal to itself,
801 * meaning that it has a universe domain and an expression that depends
802 * on itself. "i" is the position of the expression in a sequence
803 * of "n" expressions. The final dimensions of "pa" correspond to
804 * these "n" expressions.
806 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
814 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
818 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
823 /* Does the tuple contain any dimensions that are defined
824 * in terms of earlier dimensions?
826 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
834 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
835 for (i
= 0; i
< n
; ++i
) {
836 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
837 has_expr
= pw_aff_is_expr(pa
, i
, n
);
839 if (has_expr
< 0 || has_expr
)
846 /* Add a dimension to the given tuple.
847 * The dimension is initially undefined, so it is encoded
848 * as one times itself.
850 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
851 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
857 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
858 space
= isl_multi_pw_aff_get_domain_space(tuple
);
859 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
860 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
861 pa
= isl_pw_aff_from_aff(aff
);
862 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
863 isl_multi_pw_aff_from_pw_aff(pa
));
868 /* Set the name of dimension "pos" in "tuple" to "name".
869 * During printing, we add primes if the same name appears more than once
870 * to distinguish the occurrences. Here, we remove those primes from "name"
871 * before setting the name of the dimension.
873 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
874 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
881 prime
= strchr(name
, '\'');
884 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
891 /* Read an affine expression from "s" and replace the definition
892 * of dimension "pos" in "tuple" by this expression.
894 * accept_extended_affine requires a wrapped space as input.
895 * The domain space of "tuple", on the other hand is an anonymous space,
896 * so we have to adjust the space of the isl_pw_aff before adding it
899 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
900 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
906 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
907 def
= accept_extended_affine(s
, space
, v
, rational
);
908 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
909 def
= isl_pw_aff_reset_domain_space(def
, space
);
910 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
915 /* Read a list of variables and/or affine expressions and return the list
916 * as an isl_multi_pw_aff.
917 * The elements in the list are separated by either "," or "][".
918 * If "comma" is set then only "," is allowed.
920 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
921 struct vars
*v
, int rational
, int comma
)
924 struct isl_token
*tok
;
925 isl_multi_pw_aff
*res
;
927 res
= tuple_alloc(v
);
929 if (isl_stream_next_token_is(s
, ']'))
932 while ((tok
= next_token(s
)) != NULL
) {
935 res
= tuple_add_dim(res
, v
);
937 if (tok
->type
== ISL_TOKEN_IDENT
) {
939 int p
= vars_pos(v
, tok
->u
.s
, -1);
946 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
948 if (isl_stream_eat_if_available(s
, '='))
949 res
= read_tuple_var_def(s
, res
, i
, v
,
952 isl_stream_push_token(s
, tok
);
954 if (vars_add_anon(v
) < 0)
956 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
959 tok
= isl_stream_next_token(s
);
960 if (!comma
&& tok
&& tok
->type
== ']' &&
961 isl_stream_next_token_is(s
, '[')) {
963 tok
= isl_stream_next_token(s
);
964 } else if (!tok
|| tok
->type
!= ',')
971 isl_stream_push_token(s
, tok
);
976 return isl_multi_pw_aff_free(res
);
979 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
981 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
982 struct vars
*v
, int rational
, int comma
)
984 struct isl_token
*tok
;
986 isl_multi_pw_aff
*res
= NULL
;
988 tok
= isl_stream_next_token(s
);
991 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
992 name
= strdup(tok
->u
.s
);
997 isl_stream_push_token(s
, tok
);
998 if (isl_stream_eat(s
, '['))
1000 if (next_is_tuple(s
)) {
1001 isl_multi_pw_aff
*out
;
1003 res
= read_tuple(s
, v
, rational
, comma
);
1004 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1006 out
= read_tuple(s
, v
, rational
, comma
);
1007 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1008 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1009 res
= isl_multi_pw_aff_range_product(res
, out
);
1011 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1012 if (isl_stream_eat(s
, ']'))
1016 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1023 return isl_multi_pw_aff_free(res
);
1026 /* Read a tuple from "s" and add it to "map".
1027 * The tuple is initially represented as an isl_multi_pw_aff.
1028 * We first create the appropriate space in "map" based on the range
1029 * space of this isl_multi_pw_aff. Then, we add equalities based
1030 * on the affine expressions. These live in an anonymous space,
1031 * however, so we first need to reset the space to that of "map".
1033 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1034 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1035 int rational
, int comma
)
1038 isl_multi_pw_aff
*tuple
;
1039 isl_space
*space
= NULL
;
1041 tuple
= read_tuple(s
, v
, rational
, comma
);
1045 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1046 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1050 if (type
== isl_dim_param
) {
1051 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1052 isl_space_is_wrapping(space
)) {
1053 isl_die(s
->ctx
, isl_error_invalid
,
1054 "parameter tuples cannot be named or nested",
1057 map
= isl_map_add_dims(map
, type
, n
);
1058 for (i
= 0; i
< n
; ++i
) {
1060 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1061 isl_die(s
->ctx
, isl_error_invalid
,
1062 "parameters must be named",
1064 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1065 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1067 } else if (type
== isl_dim_in
) {
1070 set
= isl_set_universe(isl_space_copy(space
));
1072 set
= isl_set_set_rational(set
);
1073 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1074 map
= isl_map_from_domain(set
);
1078 set
= isl_set_universe(isl_space_copy(space
));
1080 set
= isl_set_set_rational(set
);
1081 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1084 for (i
= 0; i
< n
; ++i
) {
1091 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1092 space
= isl_pw_aff_get_domain_space(pa
);
1093 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1094 aff
= isl_aff_add_coefficient_si(aff
,
1095 isl_dim_in
, v
->n
- n
+ i
, -1);
1096 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1098 pa
= isl_pw_aff_set_rational(pa
);
1099 set
= isl_pw_aff_zero_set(pa
);
1100 map_i
= isl_map_from_range(set
);
1101 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1102 map
= isl_map_intersect(map
, map_i
);
1105 isl_space_free(space
);
1106 isl_multi_pw_aff_free(tuple
);
1109 isl_space_free(space
);
1110 isl_multi_pw_aff_free(tuple
);
1115 static __isl_give isl_set
*construct_constraints(
1116 __isl_take isl_set
*set
, int type
,
1117 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1122 left
= isl_pw_aff_list_copy(left
);
1123 right
= isl_pw_aff_list_copy(right
);
1125 left
= isl_pw_aff_list_set_rational(left
);
1126 right
= isl_pw_aff_list_set_rational(right
);
1128 if (type
== ISL_TOKEN_LE
)
1129 cond
= isl_pw_aff_list_le_set(left
, right
);
1130 else if (type
== ISL_TOKEN_GE
)
1131 cond
= isl_pw_aff_list_ge_set(left
, right
);
1132 else if (type
== ISL_TOKEN_LT
)
1133 cond
= isl_pw_aff_list_lt_set(left
, right
);
1134 else if (type
== ISL_TOKEN_GT
)
1135 cond
= isl_pw_aff_list_gt_set(left
, right
);
1136 else if (type
== ISL_TOKEN_NE
)
1137 cond
= isl_pw_aff_list_ne_set(left
, right
);
1139 cond
= isl_pw_aff_list_eq_set(left
, right
);
1141 return isl_set_intersect(set
, cond
);
1144 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1145 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1147 struct isl_token
*tok
= NULL
;
1148 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1151 set
= isl_map_wrap(map
);
1152 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1155 tok
= isl_stream_next_token(s
);
1156 if (!is_comparator(tok
)) {
1157 isl_stream_error(s
, tok
, "missing operator");
1159 isl_stream_push_token(s
, tok
);
1164 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1168 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1170 isl_token_free(tok
);
1171 isl_pw_aff_list_free(list1
);
1174 tok
= isl_stream_next_token(s
);
1175 if (!is_comparator(tok
)) {
1177 isl_stream_push_token(s
, tok
);
1181 isl_pw_aff_list_free(list1
);
1183 return isl_set_unwrap(set
);
1186 isl_token_free(tok
);
1187 isl_pw_aff_list_free(list1
);
1188 isl_pw_aff_list_free(list2
);
1193 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1194 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1197 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1199 map
= isl_map_from_domain(isl_map_wrap(map
));
1200 map
= read_defined_var_list(s
, v
, map
, rational
);
1202 if (isl_stream_eat(s
, ':'))
1205 map
= read_disjuncts(s
, v
, map
, rational
);
1206 map
= isl_set_unwrap(isl_map_domain(map
));
1208 vars_drop(v
, v
->n
- n
);
1209 if (seen_paren
&& isl_stream_eat(s
, ')'))
1218 /* Parse an expression between parentheses and push the result
1219 * back on the stream.
1221 * The parsed expression may be either an affine expression
1222 * or a condition. The first type is pushed onto the stream
1223 * as an isl_pw_aff, while the second is pushed as an isl_map.
1225 * If the initial token indicates the start of a condition,
1226 * we parse it as such.
1227 * Otherwise, we first parse an affine expression and push
1228 * that onto the stream. If the affine expression covers the
1229 * entire expression between parentheses, we return.
1230 * Otherwise, we assume that the affine expression is the
1231 * start of a condition and continue parsing.
1233 static int resolve_paren_expr(struct isl_stream
*s
,
1234 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1236 struct isl_token
*tok
, *tok2
;
1240 tok
= isl_stream_next_token(s
);
1241 if (!tok
|| tok
->type
!= '(')
1244 if (isl_stream_next_token_is(s
, '('))
1245 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1248 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1249 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1250 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1251 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1252 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1253 map
= read_disjuncts(s
, v
, map
, rational
);
1254 if (isl_stream_eat(s
, ')'))
1256 tok
->type
= ISL_TOKEN_MAP
;
1258 isl_stream_push_token(s
, tok
);
1262 tok2
= isl_stream_next_token(s
);
1267 isl_stream_push_token(s
, tok2
);
1269 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1273 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1276 tok2
->type
= ISL_TOKEN_AFF
;
1277 tok2
->u
.pwaff
= pwaff
;
1279 if (isl_stream_eat_if_available(s
, ')')) {
1280 isl_stream_push_token(s
, tok2
);
1281 isl_token_free(tok
);
1286 isl_stream_push_token(s
, tok2
);
1288 map
= read_disjuncts(s
, v
, map
, rational
);
1289 if (isl_stream_eat(s
, ')'))
1292 tok
->type
= ISL_TOKEN_MAP
;
1294 isl_stream_push_token(s
, tok
);
1298 isl_pw_aff_free(pwaff
);
1300 isl_token_free(tok
);
1305 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1306 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1308 if (isl_stream_next_token_is(s
, '('))
1309 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1312 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1313 struct isl_token
*tok
;
1314 tok
= isl_stream_next_token(s
);
1318 map
= isl_map_copy(tok
->u
.map
);
1319 isl_token_free(tok
);
1323 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1324 return read_exists(s
, v
, map
, rational
);
1326 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1329 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1330 isl_space
*dim
= isl_map_get_space(map
);
1332 return isl_map_empty(dim
);
1335 return add_constraint(s
, v
, map
, rational
);
1341 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1342 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1347 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1348 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1350 res
= isl_map_subtract(isl_map_copy(map
), res
);
1352 while (isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1355 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1356 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1358 res
= isl_map_subtract(res
, res_i
);
1360 res
= isl_map_intersect(res
, res_i
);
1367 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1368 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1372 if (isl_stream_next_token_is(s
, '}')) {
1373 isl_space
*dim
= isl_map_get_space(map
);
1375 return isl_map_universe(dim
);
1378 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1379 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1382 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1383 res
= isl_map_union(res
, res_i
);
1390 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1392 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1393 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1394 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1395 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1397 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1398 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1399 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1401 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1402 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1403 isl_basic_map_dim(bmap
, isl_dim_in
) +
1404 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1405 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1407 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1413 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1414 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1417 struct isl_token
*tok
;
1427 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1428 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1430 tok
= isl_stream_next_token(s
);
1431 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1432 isl_stream_error(s
, tok
, "expecting coefficient");
1434 isl_stream_push_token(s
, tok
);
1437 if (!tok
->on_new_line
) {
1438 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1439 isl_stream_push_token(s
, tok
);
1443 type
= isl_int_get_si(tok
->u
.v
);
1444 isl_token_free(tok
);
1446 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1448 k
= isl_basic_map_alloc_equality(bmap
);
1451 k
= isl_basic_map_alloc_inequality(bmap
);
1457 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1459 tok
= isl_stream_next_token(s
);
1460 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1461 isl_stream_error(s
, tok
, "expecting coefficient");
1463 isl_stream_push_token(s
, tok
);
1466 if (tok
->on_new_line
) {
1467 isl_stream_error(s
, tok
,
1468 "coefficient should not appear on new line");
1469 isl_stream_push_token(s
, tok
);
1472 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1473 isl_int_set(c
[pos
], tok
->u
.v
);
1474 isl_token_free(tok
);
1479 isl_basic_map_free(bmap
);
1483 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1486 struct isl_token
*tok
;
1487 struct isl_token
*tok2
;
1490 unsigned in
= 0, out
, local
= 0;
1491 struct isl_basic_map
*bmap
= NULL
;
1494 tok
= isl_stream_next_token(s
);
1496 isl_stream_error(s
, NULL
, "unexpected EOF");
1499 tok2
= isl_stream_next_token(s
);
1501 isl_token_free(tok
);
1502 isl_stream_error(s
, NULL
, "unexpected EOF");
1505 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1506 isl_stream_push_token(s
, tok2
);
1507 isl_stream_push_token(s
, tok
);
1508 isl_stream_error(s
, NULL
,
1509 "expecting constraint matrix dimensions");
1512 n_row
= isl_int_get_si(tok
->u
.v
);
1513 n_col
= isl_int_get_si(tok2
->u
.v
);
1514 on_new_line
= tok2
->on_new_line
;
1515 isl_token_free(tok2
);
1516 isl_token_free(tok
);
1517 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1518 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1519 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1520 tok
= isl_stream_next_token_on_same_line(s
);
1522 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1523 isl_stream_error(s
, tok
,
1524 "expecting number of output dimensions");
1525 isl_stream_push_token(s
, tok
);
1528 out
= isl_int_get_si(tok
->u
.v
);
1529 isl_token_free(tok
);
1531 tok
= isl_stream_next_token_on_same_line(s
);
1532 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1533 isl_stream_error(s
, tok
,
1534 "expecting number of input dimensions");
1536 isl_stream_push_token(s
, tok
);
1539 in
= isl_int_get_si(tok
->u
.v
);
1540 isl_token_free(tok
);
1542 tok
= isl_stream_next_token_on_same_line(s
);
1543 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1544 isl_stream_error(s
, tok
,
1545 "expecting number of existentials");
1547 isl_stream_push_token(s
, tok
);
1550 local
= isl_int_get_si(tok
->u
.v
);
1551 isl_token_free(tok
);
1553 tok
= isl_stream_next_token_on_same_line(s
);
1554 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1555 isl_stream_error(s
, tok
,
1556 "expecting number of parameters");
1558 isl_stream_push_token(s
, tok
);
1561 nparam
= isl_int_get_si(tok
->u
.v
);
1562 isl_token_free(tok
);
1563 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1564 isl_stream_error(s
, NULL
,
1565 "dimensions don't match");
1569 out
= n_col
- 2 - nparam
;
1570 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1574 for (i
= 0; i
< local
; ++i
) {
1575 int k
= isl_basic_map_alloc_div(bmap
);
1578 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1581 for (i
= 0; i
< n_row
; ++i
)
1582 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1584 tok
= isl_stream_next_token_on_same_line(s
);
1586 isl_stream_error(s
, tok
, "unexpected extra token on line");
1587 isl_stream_push_token(s
, tok
);
1591 bmap
= isl_basic_map_simplify(bmap
);
1592 bmap
= isl_basic_map_finalize(bmap
);
1595 isl_basic_map_free(bmap
);
1599 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1601 struct isl_token
*tok
;
1602 struct isl_token
*tok2
;
1604 struct isl_map
*map
;
1606 tok
= isl_stream_next_token(s
);
1608 isl_stream_error(s
, NULL
, "unexpected EOF");
1611 tok2
= isl_stream_next_token_on_same_line(s
);
1612 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1613 isl_stream_push_token(s
, tok2
);
1614 isl_stream_push_token(s
, tok
);
1615 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1618 isl_stream_error(s
, tok2
, "unexpected token");
1619 isl_stream_push_token(s
, tok2
);
1620 isl_stream_push_token(s
, tok
);
1623 n
= isl_int_get_si(tok
->u
.v
);
1624 isl_token_free(tok
);
1626 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1628 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1630 for (i
= 1; map
&& i
< n
; ++i
)
1631 map
= isl_map_union(map
,
1632 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1637 static int optional_power(struct isl_stream
*s
)
1640 struct isl_token
*tok
;
1642 tok
= isl_stream_next_token(s
);
1645 if (tok
->type
!= '^') {
1646 isl_stream_push_token(s
, tok
);
1649 isl_token_free(tok
);
1650 tok
= isl_stream_next_token(s
);
1651 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1652 isl_stream_error(s
, tok
, "expecting exponent");
1654 isl_stream_push_token(s
, tok
);
1657 pow
= isl_int_get_si(tok
->u
.v
);
1658 isl_token_free(tok
);
1662 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1663 __isl_keep isl_map
*map
, struct vars
*v
);
1665 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1666 __isl_keep isl_map
*map
, struct vars
*v
)
1668 isl_pw_qpolynomial
*pwqp
;
1669 struct isl_token
*tok
;
1671 tok
= next_token(s
);
1673 isl_stream_error(s
, NULL
, "unexpected EOF");
1676 if (tok
->type
== '(') {
1679 isl_token_free(tok
);
1680 pwqp
= read_term(s
, map
, v
);
1683 if (isl_stream_eat(s
, ')'))
1685 pow
= optional_power(s
);
1686 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1687 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1688 struct isl_token
*tok2
;
1689 tok2
= isl_stream_next_token(s
);
1690 isl_qpolynomial
*qp
;
1691 if (tok2
&& tok2
->type
== '/') {
1692 isl_token_free(tok2
);
1693 tok2
= next_token(s
);
1694 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1695 isl_stream_error(s
, tok2
, "expected denominator");
1696 isl_token_free(tok
);
1697 isl_token_free(tok2
);
1700 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1701 tok
->u
.v
, tok2
->u
.v
);
1702 isl_token_free(tok2
);
1704 isl_stream_push_token(s
, tok2
);
1705 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1708 isl_token_free(tok
);
1709 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1710 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1711 isl_qpolynomial
*qp
;
1712 isl_token_free(tok
);
1713 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1714 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1715 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1716 isl_qpolynomial
*qp
;
1717 isl_token_free(tok
);
1718 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1719 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1720 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1722 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1724 isl_qpolynomial
*qp
;
1726 isl_token_free(tok
);
1730 vars_drop(v
, v
->n
- n
);
1731 isl_stream_error(s
, tok
, "unknown identifier");
1732 isl_token_free(tok
);
1735 isl_token_free(tok
);
1736 pow
= optional_power(s
);
1737 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
1738 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1739 } else if (tok
->type
== '[') {
1743 isl_stream_push_token(s
, tok
);
1744 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
1745 pow
= optional_power(s
);
1746 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
1747 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1748 } else if (tok
->type
== '-') {
1749 isl_token_free(tok
);
1750 pwqp
= read_factor(s
, map
, v
);
1751 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
1753 isl_stream_error(s
, tok
, "unexpected isl_token");
1754 isl_stream_push_token(s
, tok
);
1758 if (isl_stream_eat_if_available(s
, '*') ||
1759 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1760 isl_pw_qpolynomial
*pwqp2
;
1762 pwqp2
= read_factor(s
, map
, v
);
1763 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
1768 isl_pw_qpolynomial_free(pwqp
);
1772 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1773 __isl_keep isl_map
*map
, struct vars
*v
)
1775 struct isl_token
*tok
;
1776 isl_pw_qpolynomial
*pwqp
;
1778 pwqp
= read_factor(s
, map
, v
);
1781 tok
= next_token(s
);
1785 if (tok
->type
== '+') {
1786 isl_pw_qpolynomial
*pwqp2
;
1788 isl_token_free(tok
);
1789 pwqp2
= read_factor(s
, map
, v
);
1790 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1791 } else if (tok
->type
== '-') {
1792 isl_pw_qpolynomial
*pwqp2
;
1794 isl_token_free(tok
);
1795 pwqp2
= read_factor(s
, map
, v
);
1796 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
1797 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1798 isl_int_is_neg(tok
->u
.v
)) {
1799 isl_pw_qpolynomial
*pwqp2
;
1801 isl_stream_push_token(s
, tok
);
1802 pwqp2
= read_factor(s
, map
, v
);
1803 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1805 isl_stream_push_token(s
, tok
);
1813 static __isl_give isl_map
*read_optional_disjuncts(struct isl_stream
*s
,
1814 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
1816 struct isl_token
*tok
;
1818 tok
= isl_stream_next_token(s
);
1820 isl_stream_error(s
, NULL
, "unexpected EOF");
1823 if (tok
->type
== ':' ||
1824 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
1825 isl_token_free(tok
);
1826 map
= read_disjuncts(s
, v
, map
, rational
);
1828 isl_stream_push_token(s
, tok
);
1836 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1837 __isl_take isl_map
*map
, struct vars
*v
, int n
)
1839 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1840 isl_pw_qpolynomial
*pwqp
;
1841 struct isl_set
*set
;
1843 pwqp
= read_term(s
, map
, v
);
1844 map
= read_optional_disjuncts(s
, map
, v
, 0);
1845 set
= isl_map_range(map
);
1847 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
1849 vars_drop(v
, v
->n
- n
);
1855 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
1856 __isl_take isl_set
*set
, struct vars
*v
, int n
)
1858 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
1859 isl_pw_qpolynomial
*pwqp
;
1860 isl_pw_qpolynomial_fold
*pwf
= NULL
;
1862 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
1863 return obj_read_poly(s
, set
, v
, n
);
1865 if (isl_stream_eat(s
, '('))
1868 pwqp
= read_term(s
, set
, v
);
1869 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
1871 while (isl_stream_eat_if_available(s
, ',')) {
1872 isl_pw_qpolynomial_fold
*pwf_i
;
1873 pwqp
= read_term(s
, set
, v
);
1874 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
1876 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
1879 if (isl_stream_eat(s
, ')'))
1882 set
= read_optional_disjuncts(s
, set
, v
, 0);
1883 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
1885 vars_drop(v
, v
->n
- n
);
1891 isl_pw_qpolynomial_fold_free(pwf
);
1892 obj
.type
= isl_obj_none
;
1896 static int is_rational(struct isl_stream
*s
)
1898 struct isl_token
*tok
;
1900 tok
= isl_stream_next_token(s
);
1903 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
1904 isl_token_free(tok
);
1905 isl_stream_eat(s
, ':');
1909 isl_stream_push_token(s
, tok
);
1914 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
1915 __isl_take isl_map
*map
, struct vars
*v
)
1917 struct isl_token
*tok
;
1918 struct isl_obj obj
= { isl_obj_set
, NULL
};
1922 rational
= is_rational(s
);
1924 map
= isl_map_set_rational(map
);
1926 if (isl_stream_next_token_is(s
, ':')) {
1927 obj
.type
= isl_obj_set
;
1928 obj
.v
= read_optional_disjuncts(s
, map
, v
, rational
);
1932 if (!next_is_tuple(s
))
1933 return obj_read_poly_or_fold(s
, map
, v
, n
);
1935 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
1938 tok
= isl_stream_next_token(s
);
1941 if (tok
->type
== ISL_TOKEN_TO
) {
1942 obj
.type
= isl_obj_map
;
1943 isl_token_free(tok
);
1944 if (!next_is_tuple(s
)) {
1945 isl_set
*set
= isl_map_domain(map
);
1946 return obj_read_poly_or_fold(s
, set
, v
, n
);
1948 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
1952 map
= isl_map_domain(map
);
1953 isl_stream_push_token(s
, tok
);
1956 map
= read_optional_disjuncts(s
, map
, v
, rational
);
1958 vars_drop(v
, v
->n
- n
);
1964 obj
.type
= isl_obj_none
;
1968 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
1970 if (obj
.type
== isl_obj_map
) {
1971 obj
.v
= isl_union_map_from_map(obj
.v
);
1972 obj
.type
= isl_obj_union_map
;
1973 } else if (obj
.type
== isl_obj_set
) {
1974 obj
.v
= isl_union_set_from_set(obj
.v
);
1975 obj
.type
= isl_obj_union_set
;
1976 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
1977 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1978 obj
.type
= isl_obj_union_pw_qpolynomial
;
1979 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
1980 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1981 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1983 isl_assert(ctx
, 0, goto error
);
1986 obj
.type
->free(obj
.v
);
1987 obj
.type
= isl_obj_none
;
1991 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
1992 struct isl_obj obj1
, struct isl_obj obj2
)
1994 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
1995 obj1
= to_union(ctx
, obj1
);
1996 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
1997 obj2
= to_union(ctx
, obj2
);
1998 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
1999 obj1
= to_union(ctx
, obj1
);
2000 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2001 obj2
= to_union(ctx
, obj2
);
2002 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2003 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2004 obj1
= to_union(ctx
, obj1
);
2005 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2006 obj2
.type
== isl_obj_pw_qpolynomial
)
2007 obj2
= to_union(ctx
, obj2
);
2008 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2009 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2010 obj1
= to_union(ctx
, obj1
);
2011 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2012 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2013 obj2
= to_union(ctx
, obj2
);
2014 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2015 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2016 obj1
= to_union(ctx
, obj1
);
2017 obj2
= to_union(ctx
, obj2
);
2019 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2020 obj1
= to_union(ctx
, obj1
);
2021 obj2
= to_union(ctx
, obj2
);
2023 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2024 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2025 obj1
= to_union(ctx
, obj1
);
2026 obj2
= to_union(ctx
, obj2
);
2028 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2029 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2030 obj1
= to_union(ctx
, obj1
);
2031 obj2
= to_union(ctx
, obj2
);
2033 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2036 obj1
.type
->free(obj1
.v
);
2037 obj2
.type
->free(obj2
.v
);
2038 obj1
.type
= isl_obj_none
;
2043 static struct isl_obj
obj_read(struct isl_stream
*s
)
2045 isl_map
*map
= NULL
;
2046 struct isl_token
*tok
;
2047 struct vars
*v
= NULL
;
2048 struct isl_obj obj
= { isl_obj_set
, NULL
};
2050 tok
= next_token(s
);
2052 isl_stream_error(s
, NULL
, "unexpected EOF");
2055 if (tok
->type
== ISL_TOKEN_VALUE
) {
2056 struct isl_token
*tok2
;
2057 struct isl_map
*map
;
2059 tok2
= isl_stream_next_token(s
);
2060 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2061 isl_int_is_neg(tok2
->u
.v
)) {
2063 isl_stream_push_token(s
, tok2
);
2064 obj
.type
= isl_obj_int
;
2065 obj
.v
= isl_int_obj_alloc(s
->ctx
, tok
->u
.v
);
2066 isl_token_free(tok
);
2069 isl_stream_push_token(s
, tok2
);
2070 isl_stream_push_token(s
, tok
);
2071 map
= map_read_polylib(s
);
2074 if (isl_map_may_be_set(map
))
2075 obj
.v
= isl_map_range(map
);
2077 obj
.type
= isl_obj_map
;
2082 v
= vars_new(s
->ctx
);
2084 isl_stream_push_token(s
, tok
);
2087 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2088 if (tok
->type
== '[') {
2089 isl_stream_push_token(s
, tok
);
2090 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2093 tok
= isl_stream_next_token(s
);
2094 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2095 isl_stream_error(s
, tok
, "expecting '->'");
2097 isl_stream_push_token(s
, tok
);
2100 isl_token_free(tok
);
2101 tok
= isl_stream_next_token(s
);
2103 if (!tok
|| tok
->type
!= '{') {
2104 isl_stream_error(s
, tok
, "expecting '{'");
2106 isl_stream_push_token(s
, tok
);
2109 isl_token_free(tok
);
2111 tok
= isl_stream_next_token(s
);
2114 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2115 isl_token_free(tok
);
2116 if (isl_stream_eat(s
, '='))
2118 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2121 } else if (tok
->type
== '}') {
2122 obj
.type
= isl_obj_union_set
;
2123 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2124 isl_token_free(tok
);
2127 isl_stream_push_token(s
, tok
);
2132 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2133 if (o
.type
== isl_obj_none
|| !o
.v
)
2138 obj
= obj_add(s
->ctx
, obj
, o
);
2139 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2142 tok
= isl_stream_next_token(s
);
2143 if (!tok
|| tok
->type
!= ';')
2145 isl_token_free(tok
);
2146 if (isl_stream_next_token_is(s
, '}')) {
2147 tok
= isl_stream_next_token(s
);
2152 if (tok
&& tok
->type
== '}') {
2153 isl_token_free(tok
);
2155 isl_stream_error(s
, tok
, "unexpected isl_token");
2157 isl_token_free(tok
);
2167 obj
.type
->free(obj
.v
);
2174 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2179 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2185 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2186 obj
.type
== isl_obj_set
, goto error
);
2188 if (obj
.type
== isl_obj_set
)
2189 obj
.v
= isl_map_from_range(obj
.v
);
2193 obj
.type
->free(obj
.v
);
2197 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2203 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2204 obj
.v
= isl_map_range(obj
.v
);
2205 obj
.type
= isl_obj_set
;
2207 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2212 obj
.type
->free(obj
.v
);
2216 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2221 if (obj
.type
== isl_obj_map
) {
2222 obj
.type
= isl_obj_union_map
;
2223 obj
.v
= isl_union_map_from_map(obj
.v
);
2225 if (obj
.type
== isl_obj_set
) {
2226 obj
.type
= isl_obj_union_set
;
2227 obj
.v
= isl_union_set_from_set(obj
.v
);
2229 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2230 isl_union_set_is_empty(obj
.v
))
2231 obj
.type
= isl_obj_union_map
;
2232 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2233 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2237 obj
.type
->free(obj
.v
);
2241 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2246 if (obj
.type
== isl_obj_set
) {
2247 obj
.type
= isl_obj_union_set
;
2248 obj
.v
= isl_union_set_from_set(obj
.v
);
2251 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2255 obj
.type
->free(obj
.v
);
2259 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2262 struct isl_map
*map
;
2263 struct isl_basic_map
*bmap
;
2270 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2273 bmap
= isl_basic_map_empty_like_map(map
);
2275 bmap
= isl_basic_map_copy(map
->p
[0]);
2285 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2287 isl_basic_map
*bmap
;
2288 bmap
= basic_map_read(s
);
2291 if (!isl_basic_map_may_be_set(bmap
))
2292 isl_die(s
->ctx
, isl_error_invalid
,
2293 "input is not a set", goto error
);
2294 return isl_basic_map_range(bmap
);
2296 isl_basic_map_free(bmap
);
2300 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2303 struct isl_basic_map
*bmap
;
2304 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2307 bmap
= basic_map_read(s
);
2312 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2315 isl_basic_set
*bset
;
2316 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2319 bset
= basic_set_read(s
);
2324 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2327 struct isl_basic_map
*bmap
;
2328 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2331 bmap
= basic_map_read(s
);
2336 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2339 isl_basic_set
*bset
;
2340 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2343 bset
= basic_set_read(s
);
2348 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2351 struct isl_map
*map
;
2352 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2355 map
= isl_stream_read_map(s
);
2360 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2363 struct isl_map
*map
;
2364 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2367 map
= isl_stream_read_map(s
);
2372 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2376 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2379 set
= isl_stream_read_set(s
);
2384 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2388 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2391 set
= isl_stream_read_set(s
);
2396 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2399 isl_union_map
*umap
;
2400 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2403 umap
= isl_stream_read_union_map(s
);
2408 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2411 isl_union_map
*umap
;
2412 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2415 umap
= isl_stream_read_union_map(s
);
2420 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2423 isl_union_set
*uset
;
2424 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2427 uset
= isl_stream_read_union_set(s
);
2432 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2435 isl_union_set
*uset
;
2436 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2439 uset
= isl_stream_read_union_set(s
);
2444 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2446 struct isl_vec
*vec
= NULL
;
2447 struct isl_token
*tok
;
2451 tok
= isl_stream_next_token(s
);
2452 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2453 isl_stream_error(s
, tok
, "expecting vector length");
2457 size
= isl_int_get_si(tok
->u
.v
);
2458 isl_token_free(tok
);
2460 vec
= isl_vec_alloc(s
->ctx
, size
);
2462 for (j
= 0; j
< size
; ++j
) {
2463 tok
= isl_stream_next_token(s
);
2464 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2465 isl_stream_error(s
, tok
, "expecting constant value");
2468 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2469 isl_token_free(tok
);
2474 isl_token_free(tok
);
2479 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2481 return isl_vec_read_polylib(s
);
2484 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2487 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2495 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2496 struct isl_stream
*s
)
2502 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2507 obj
.type
->free(obj
.v
);
2511 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2514 isl_pw_qpolynomial
*pwqp
;
2515 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2518 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2523 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2526 isl_pw_qpolynomial
*pwqp
;
2527 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2530 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2535 /* Is the next token an identifer not in "v"?
2537 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2541 struct isl_token
*tok
;
2543 tok
= isl_stream_next_token(s
);
2546 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2547 isl_stream_push_token(s
, tok
);
2549 vars_drop(v
, v
->n
- n
);
2554 /* First read the domain of the affine expression, which may be
2555 * a parameter space or a set.
2556 * The tricky part is that we don't know if the domain is a set or not,
2557 * so when we are trying to read the domain, we may actually be reading
2558 * the affine expression itself (defined on a parameter domains)
2559 * If the tuple we are reading is named, we assume it's the domain.
2560 * Also, if inside the tuple, the first thing we find is a nested tuple
2561 * or a new identifier, we again assume it's the domain.
2562 * Otherwise, we assume we are reading an affine expression.
2564 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2565 __isl_take isl_set
*dom
, struct vars
*v
)
2567 struct isl_token
*tok
;
2569 tok
= isl_stream_next_token(s
);
2570 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2571 isl_stream_push_token(s
, tok
);
2572 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2574 if (!tok
|| tok
->type
!= '[') {
2575 isl_stream_error(s
, tok
, "expecting '['");
2578 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2579 isl_stream_push_token(s
, tok
);
2580 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2582 isl_stream_push_token(s
, tok
);
2587 isl_stream_push_token(s
, tok
);
2592 /* Read an affine expression from "s".
2594 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2599 ma
= isl_stream_read_multi_aff(s
);
2602 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2603 isl_die(s
->ctx
, isl_error_invalid
,
2604 "expecting single affine expression",
2607 aff
= isl_multi_aff_get_aff(ma
, 0);
2608 isl_multi_aff_free(ma
);
2611 isl_multi_aff_free(ma
);
2615 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2617 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2618 __isl_take isl_set
*dom
, struct vars
*v
)
2620 isl_pw_aff
*pwaff
= NULL
;
2622 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2625 if (isl_stream_eat(s
, '['))
2628 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2630 if (isl_stream_eat(s
, ']'))
2633 dom
= read_optional_disjuncts(s
, dom
, v
, 0);
2634 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2639 isl_pw_aff_free(pwaff
);
2643 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2646 isl_set
*dom
= NULL
;
2648 isl_pw_aff
*pa
= NULL
;
2651 v
= vars_new(s
->ctx
);
2655 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2656 if (next_is_tuple(s
)) {
2657 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2658 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2661 if (isl_stream_eat(s
, '{'))
2665 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2666 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2667 vars_drop(v
, v
->n
- n
);
2669 while (isl_stream_eat_if_available(s
, ';')) {
2673 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2674 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2675 vars_drop(v
, v
->n
- n
);
2677 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2680 if (isl_stream_eat(s
, '}'))
2689 isl_pw_aff_free(pa
);
2693 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2696 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2699 aff
= isl_stream_read_aff(s
);
2704 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2707 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2710 pa
= isl_stream_read_pw_aff(s
);
2715 /* Read an isl_pw_multi_aff from "s".
2716 * We currently read a generic object and if it turns out to be a set or
2717 * a map, we convert that to an isl_pw_multi_aff.
2718 * It would be more efficient if we were to construct the isl_pw_multi_aff
2721 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
2729 if (obj
.type
== isl_obj_map
)
2730 return isl_pw_multi_aff_from_map(obj
.v
);
2731 if (obj
.type
== isl_obj_set
)
2732 return isl_pw_multi_aff_from_set(obj
.v
);
2734 obj
.type
->free(obj
.v
);
2735 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2739 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
2742 isl_pw_multi_aff
*pma
;
2743 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2746 pma
= isl_stream_read_pw_multi_aff(s
);
2751 /* Assuming "pa" represents a single affine expression defined on a universe
2752 * domain, extract this affine expression.
2754 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
2761 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2762 "expecting single affine expression",
2764 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
2765 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2766 "expecting universe domain",
2769 aff
= isl_aff_copy(pa
->p
[0].aff
);
2770 isl_pw_aff_free(pa
);
2773 isl_pw_aff_free(pa
);
2777 /* Read a multi-affine expression from "s".
2778 * If the multi-affine expression has a domain, then then tuple
2779 * representing this domain cannot involve any affine expressions.
2780 * The tuple representing the actual expressions needs to consist
2781 * of only affine expressions. Moreover, these expressions can
2782 * only depend on parameters and input dimensions and not on other
2783 * output dimensions.
2785 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
2788 isl_set
*dom
= NULL
;
2789 isl_multi_pw_aff
*tuple
= NULL
;
2791 isl_space
*space
, *dom_space
;
2792 isl_multi_aff
*ma
= NULL
;
2794 v
= vars_new(s
->ctx
);
2798 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2799 if (next_is_tuple(s
)) {
2800 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2801 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2804 if (!isl_set_plain_is_universe(dom
))
2805 isl_die(s
->ctx
, isl_error_invalid
,
2806 "expecting universe parameter domain", goto error
);
2807 if (isl_stream_eat(s
, '{'))
2810 tuple
= read_tuple(s
, v
, 0, 0);
2813 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
2818 has_expr
= tuple_has_expr(tuple
);
2822 isl_die(s
->ctx
, isl_error_invalid
,
2823 "expecting universe domain", goto error
);
2824 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
2825 set
= isl_set_universe(space
);
2826 dom
= isl_set_intersect_params(set
, dom
);
2827 isl_multi_pw_aff_free(tuple
);
2828 tuple
= read_tuple(s
, v
, 0, 0);
2833 if (isl_stream_eat(s
, '}'))
2836 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
2837 dim
= isl_set_dim(dom
, isl_dim_all
);
2838 dom_space
= isl_set_get_space(dom
);
2839 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
2840 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
2841 if (!isl_space_is_params(dom_space
))
2842 space
= isl_space_map_from_domain_and_range(
2843 isl_space_copy(dom_space
), space
);
2844 isl_space_free(dom_space
);
2845 ma
= isl_multi_aff_alloc(space
);
2847 for (i
= 0; i
< n
; ++i
) {
2850 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
2851 aff
= aff_from_pw_aff(pa
);
2854 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
2856 isl_die(s
->ctx
, isl_error_invalid
,
2857 "not an affine expression", goto error
);
2859 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
2860 space
= isl_multi_aff_get_domain_space(ma
);
2861 aff
= isl_aff_reset_domain_space(aff
, space
);
2862 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2865 isl_multi_pw_aff_free(tuple
);
2870 isl_multi_pw_aff_free(tuple
);
2873 isl_multi_aff_free(ma
);
2877 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
2880 isl_multi_aff
*maff
;
2881 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2884 maff
= isl_stream_read_multi_aff(s
);
2889 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
2890 struct isl_stream
*s
)
2895 if (obj
.type
== isl_obj_pw_qpolynomial
) {
2896 obj
.type
= isl_obj_union_pw_qpolynomial
;
2897 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2900 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
2905 obj
.type
->free(obj
.v
);
2909 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
2910 isl_ctx
*ctx
, const char *str
)
2912 isl_union_pw_qpolynomial
*upwqp
;
2913 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2916 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);