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>
28 #include <isl_vec_private.h>
30 #include <isl_val_private.h>
35 struct variable
*next
;
44 static struct vars
*vars_new(struct isl_ctx
*ctx
)
47 v
= isl_alloc_type(ctx
, struct vars
);
56 static void variable_free(struct variable
*var
)
59 struct variable
*next
= var
->next
;
66 static void vars_free(struct vars
*v
)
74 static void vars_drop(struct vars
*v
, int n
)
85 struct variable
*next
= var
->next
;
93 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
97 var
= isl_calloc_type(v
->ctx
, struct variable
);
100 var
->name
= strdup(name
);
101 var
->name
[len
] = '\0';
110 static int vars_pos(struct vars
*v
, const char *s
, int len
)
117 for (q
= v
->v
; q
; q
= q
->next
) {
118 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
125 v
->v
= variable_new(v
, s
, len
, v
->n
);
133 static int vars_add_anon(struct vars
*v
)
135 v
->v
= variable_new(v
, "", 0, v
->n
);
144 /* Obtain next token, with some preprocessing.
145 * In particular, evaluate expressions of the form x^y,
146 * with x and y values.
148 static struct isl_token
*next_token(struct isl_stream
*s
)
150 struct isl_token
*tok
, *tok2
;
152 tok
= isl_stream_next_token(s
);
153 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
155 if (!isl_stream_eat_if_available(s
, '^'))
157 tok2
= isl_stream_next_token(s
);
158 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
159 isl_stream_error(s
, tok2
, "expecting constant value");
163 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
165 isl_token_free(tok2
);
169 isl_token_free(tok2
);
173 /* Read an isl_val from "s".
175 * The following token sequences are recognized
178 * "-" "infty" -> -infty
183 * where n, d and v are integer constants.
185 __isl_give isl_val
*isl_stream_read_val(struct isl_stream
*s
)
187 struct isl_token
*tok
= NULL
;
188 struct isl_token
*tok2
= NULL
;
193 isl_stream_error(s
, NULL
, "unexpected EOF");
196 if (tok
->type
== ISL_TOKEN_INFTY
) {
198 return isl_val_infty(s
->ctx
);
200 if (tok
->type
== '-' &&
201 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
203 return isl_val_neginfty(s
->ctx
);
205 if (tok
->type
== ISL_TOKEN_NAN
) {
207 return isl_val_nan(s
->ctx
);
209 if (tok
->type
!= ISL_TOKEN_VALUE
) {
210 isl_stream_error(s
, tok
, "expecting value");
214 if (isl_stream_eat_if_available(s
, '/')) {
215 tok2
= next_token(s
);
217 isl_stream_error(s
, NULL
, "unexpected EOF");
220 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
221 isl_stream_error(s
, tok2
, "expecting value");
224 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
225 val
= isl_val_normalize(val
);
227 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
231 isl_token_free(tok2
);
235 isl_token_free(tok2
);
239 /* Read an isl_val from "str".
241 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
245 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
248 val
= isl_stream_read_val(s
);
253 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
255 struct isl_token
*tok
;
258 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
259 isl_stream_error(s
, tok
, "expecting constant value");
263 isl_int_mul(*f
, *f
, tok
->u
.v
);
267 if (isl_stream_eat_if_available(s
, '*'))
268 return accept_cst_factor(s
, f
);
276 /* Given an affine expression aff, return an affine expression
277 * for aff % d, with d the next token on the stream, which is
278 * assumed to be a constant.
280 * We introduce an integer division q = [aff/d] and the result
281 * is set to aff - d q.
283 static __isl_give isl_pw_aff
*affine_mod(struct isl_stream
*s
,
284 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
286 struct isl_token
*tok
;
290 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
291 isl_stream_error(s
, tok
, "expecting constant value");
295 q
= isl_pw_aff_copy(aff
);
296 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
297 q
= isl_pw_aff_floor(q
);
298 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
300 aff
= isl_pw_aff_sub(aff
, q
);
305 isl_pw_aff_free(aff
);
310 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
311 __isl_take isl_space
*dim
, struct vars
*v
);
312 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
313 __isl_take isl_space
*dim
, struct vars
*v
);
315 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
316 __isl_take isl_space
*dim
, struct vars
*v
)
318 struct isl_token
*tok
;
319 isl_pw_aff_list
*list
= NULL
;
322 tok
= isl_stream_next_token(s
);
325 min
= tok
->type
== ISL_TOKEN_MIN
;
328 if (isl_stream_eat(s
, '('))
331 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
335 if (isl_stream_eat(s
, ')'))
339 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
342 isl_pw_aff_list_free(list
);
346 /* Is "tok" the start of an integer division?
348 static int is_start_of_div(struct isl_token
*tok
)
352 if (tok
->type
== '[')
354 if (tok
->type
== ISL_TOKEN_FLOOR
)
356 if (tok
->type
== ISL_TOKEN_CEIL
)
358 if (tok
->type
== ISL_TOKEN_FLOORD
)
360 if (tok
->type
== ISL_TOKEN_CEILD
)
365 /* Read an integer division from "s" and return it as an isl_pw_aff.
367 * The integer division can be of the form
369 * [<affine expression>]
370 * floor(<affine expression>)
371 * ceil(<affine expression>)
372 * floord(<affine expression>,<denominator>)
373 * ceild(<affine expression>,<denominator>)
375 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
376 __isl_take isl_space
*dim
, struct vars
*v
)
378 struct isl_token
*tok
;
382 isl_pw_aff
*pwaff
= NULL
;
384 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
386 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
388 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
390 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
393 if (isl_stream_eat(s
, '('))
396 if (isl_stream_eat(s
, '['))
400 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
403 if (isl_stream_eat(s
, ','))
409 if (tok
->type
!= ISL_TOKEN_VALUE
) {
410 isl_stream_error(s
, tok
, "expected denominator");
411 isl_stream_push_token(s
, tok
);
414 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
419 pwaff
= isl_pw_aff_ceil(pwaff
);
421 pwaff
= isl_pw_aff_floor(pwaff
);
424 if (isl_stream_eat(s
, ')'))
427 if (isl_stream_eat(s
, ']'))
435 isl_pw_aff_free(pwaff
);
439 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
440 __isl_take isl_space
*dim
, struct vars
*v
)
442 struct isl_token
*tok
= NULL
;
443 isl_pw_aff
*res
= NULL
;
447 isl_stream_error(s
, NULL
, "unexpected EOF");
451 if (tok
->type
== ISL_TOKEN_AFF
) {
452 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
454 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
456 int pos
= vars_pos(v
, tok
->u
.s
, -1);
462 vars_drop(v
, v
->n
- n
);
463 isl_stream_error(s
, tok
, "unknown identifier");
467 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
470 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
471 res
= isl_pw_aff_from_aff(aff
);
473 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
474 if (isl_stream_eat_if_available(s
, '*')) {
475 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
476 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
480 ls
= isl_local_space_from_space(isl_space_copy(dim
));
481 aff
= isl_aff_zero_on_domain(ls
);
482 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
483 res
= isl_pw_aff_from_aff(aff
);
486 } else if (tok
->type
== '(') {
489 res
= accept_affine(s
, isl_space_copy(dim
), v
);
492 if (isl_stream_eat(s
, ')'))
494 } else if (is_start_of_div(tok
)) {
495 isl_stream_push_token(s
, tok
);
497 res
= accept_div(s
, isl_space_copy(dim
), v
);
498 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
499 isl_stream_push_token(s
, tok
);
501 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
503 isl_stream_error(s
, tok
, "expecting factor");
506 if (isl_stream_eat_if_available(s
, '%') ||
507 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
509 return affine_mod(s
, v
, res
);
511 if (isl_stream_eat_if_available(s
, '*')) {
514 isl_int_set_si(f
, 1);
515 if (accept_cst_factor(s
, &f
) < 0) {
519 res
= isl_pw_aff_scale(res
, f
);
522 if (isl_stream_eat_if_available(s
, '/')) {
525 isl_int_set_si(f
, 1);
526 if (accept_cst_factor(s
, &f
) < 0) {
530 res
= isl_pw_aff_scale_down(res
, f
);
539 isl_pw_aff_free(res
);
544 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
549 space
= isl_pw_aff_get_domain_space(pwaff
);
550 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
551 aff
= isl_aff_add_constant(aff
, v
);
553 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
556 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
557 __isl_take isl_space
*dim
, struct vars
*v
)
559 struct isl_token
*tok
= NULL
;
564 ls
= isl_local_space_from_space(isl_space_copy(dim
));
565 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
572 isl_stream_error(s
, NULL
, "unexpected EOF");
575 if (tok
->type
== '-') {
580 if (tok
->type
== '(' || is_start_of_div(tok
) ||
581 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
582 tok
->type
== ISL_TOKEN_IDENT
||
583 tok
->type
== ISL_TOKEN_AFF
) {
585 isl_stream_push_token(s
, tok
);
587 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
589 res
= isl_pw_aff_sub(res
, term
);
591 res
= isl_pw_aff_add(res
, term
);
595 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
597 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
598 if (isl_stream_eat_if_available(s
, '*') ||
599 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
601 term
= accept_affine_factor(s
,
602 isl_space_copy(dim
), v
);
603 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
604 res
= isl_pw_aff_add(res
, term
);
608 res
= add_cst(res
, tok
->u
.v
);
612 isl_stream_error(s
, tok
, "unexpected isl_token");
613 isl_stream_push_token(s
, tok
);
614 isl_pw_aff_free(res
);
621 if (tok
&& tok
->type
== '-') {
624 } else if (tok
&& tok
->type
== '+') {
627 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
628 isl_int_is_neg(tok
->u
.v
)) {
629 isl_stream_push_token(s
, tok
);
632 isl_stream_push_token(s
, tok
);
642 isl_pw_aff_free(res
);
646 static int is_comparator(struct isl_token
*tok
)
664 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
665 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
666 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
667 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
669 /* Accept a ternary operator, given the first argument.
671 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
672 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
675 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
680 if (isl_stream_eat(s
, '?'))
683 dim
= isl_space_wrap(isl_map_get_space(cond
));
684 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
688 if (isl_stream_eat(s
, ':'))
691 dim
= isl_pw_aff_get_domain_space(pwaff1
);
692 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
696 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
697 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
700 isl_pw_aff_free(pwaff1
);
701 isl_pw_aff_free(pwaff2
);
705 /* Accept an affine expression that may involve ternary operators.
706 * We first read an affine expression.
707 * If it is not followed by a comparison operator, we simply return it.
708 * Otherwise, we assume the affine expression is part of the first
709 * argument of a ternary operator and try to parse that.
711 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
712 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
717 struct isl_token
*tok
;
718 int line
= -1, col
= -1;
721 tok
= isl_stream_next_token(s
);
725 isl_stream_push_token(s
, tok
);
728 pwaff
= accept_affine(s
, dim
, v
);
730 pwaff
= isl_pw_aff_set_rational(pwaff
);
734 tok
= isl_stream_next_token(s
);
736 return isl_pw_aff_free(pwaff
);
738 is_comp
= is_comparator(tok
);
739 isl_stream_push_token(s
, tok
);
743 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
745 return isl_pw_aff_free(pwaff
);
746 tok
->type
= ISL_TOKEN_AFF
;
747 tok
->u
.pwaff
= pwaff
;
749 space
= isl_pw_aff_get_domain_space(pwaff
);
750 cond
= isl_map_universe(isl_space_unwrap(space
));
752 isl_stream_push_token(s
, tok
);
754 cond
= read_formula(s
, v
, cond
, rational
);
756 return accept_ternary(s
, cond
, v
, rational
);
759 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
760 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
767 if (type
== isl_dim_param
)
768 pos
= isl_map_dim(map
, isl_dim_param
);
770 pos
= isl_map_dim(map
, isl_dim_in
);
771 if (type
== isl_dim_out
)
772 pos
+= isl_map_dim(map
, isl_dim_out
);
777 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
779 def_map
= isl_map_from_pw_aff(def
);
780 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
781 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
783 map
= isl_map_intersect(map
, def_map
);
788 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
789 __isl_take isl_space
*dim
, struct vars
*v
)
792 isl_pw_aff_list
*list
;
793 struct isl_token
*tok
= NULL
;
795 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
796 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
801 tok
= isl_stream_next_token(s
);
803 isl_stream_error(s
, NULL
, "unexpected EOF");
806 if (tok
->type
!= ',') {
807 isl_stream_push_token(s
, tok
);
812 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
813 list
= isl_pw_aff_list_concat(list
,
814 isl_pw_aff_list_from_pw_aff(pwaff
));
823 isl_pw_aff_list_free(list
);
827 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
828 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
830 struct isl_token
*tok
;
832 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
836 if (tok
->type
!= ISL_TOKEN_IDENT
)
839 p
= vars_pos(v
, tok
->u
.s
, -1);
843 isl_stream_error(s
, tok
, "expecting unique identifier");
847 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
850 tok
= isl_stream_next_token(s
);
851 if (tok
&& tok
->type
== '=') {
853 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
854 tok
= isl_stream_next_token(s
);
857 if (!tok
|| tok
->type
!= ',')
863 isl_stream_push_token(s
, tok
);
872 static int next_is_tuple(struct isl_stream
*s
)
874 struct isl_token
*tok
;
877 tok
= isl_stream_next_token(s
);
880 if (tok
->type
== '[') {
881 isl_stream_push_token(s
, tok
);
884 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
885 isl_stream_push_token(s
, tok
);
889 is_tuple
= isl_stream_next_token_is(s
, '[');
891 isl_stream_push_token(s
, tok
);
896 /* Allocate an initial tuple with zero dimensions and an anonymous,
897 * unstructured space.
898 * A tuple is represented as an isl_multi_pw_aff.
899 * The range space is the space of the tuple.
900 * The domain space is an anonymous space
901 * with a dimension for each variable in the set of variables in "v".
902 * If a given dimension is not defined in terms of earlier dimensions in
903 * the input, then the corresponding isl_pw_aff is set equal to one time
904 * the variable corresponding to the dimension being defined.
906 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
908 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
911 /* Is "pa" an expression in term of earlier dimensions?
912 * The alternative is that the dimension is defined to be equal to itself,
913 * meaning that it has a universe domain and an expression that depends
914 * on itself. "i" is the position of the expression in a sequence
915 * of "n" expressions. The final dimensions of "pa" correspond to
916 * these "n" expressions.
918 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
926 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
930 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
935 /* Does the tuple contain any dimensions that are defined
936 * in terms of earlier dimensions?
938 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
946 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
947 for (i
= 0; i
< n
; ++i
) {
948 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
949 has_expr
= pw_aff_is_expr(pa
, i
, n
);
951 if (has_expr
< 0 || has_expr
)
958 /* Add a dimension to the given tuple.
959 * The dimension is initially undefined, so it is encoded
960 * as one times itself.
962 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
963 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
969 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
970 space
= isl_multi_pw_aff_get_domain_space(tuple
);
971 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
972 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
973 pa
= isl_pw_aff_from_aff(aff
);
974 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
975 isl_multi_pw_aff_from_pw_aff(pa
));
980 /* Set the name of dimension "pos" in "tuple" to "name".
981 * During printing, we add primes if the same name appears more than once
982 * to distinguish the occurrences. Here, we remove those primes from "name"
983 * before setting the name of the dimension.
985 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
986 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
993 prime
= strchr(name
, '\'');
996 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
1003 /* Read an affine expression from "s" and replace the definition
1004 * of dimension "pos" in "tuple" by this expression.
1006 * accept_extended_affine requires a wrapped space as input.
1007 * The domain space of "tuple", on the other hand is an anonymous space,
1008 * so we have to adjust the space of the isl_pw_aff before adding it
1011 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
1012 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
1018 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1019 def
= accept_extended_affine(s
, space
, v
, rational
);
1020 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1021 def
= isl_pw_aff_reset_domain_space(def
, space
);
1022 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
1027 /* Read a list of variables and/or affine expressions and return the list
1028 * as an isl_multi_pw_aff.
1029 * The elements in the list are separated by either "," or "][".
1030 * If "comma" is set then only "," is allowed.
1032 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
1033 struct vars
*v
, int rational
, int comma
)
1036 struct isl_token
*tok
;
1037 isl_multi_pw_aff
*res
;
1039 res
= tuple_alloc(v
);
1041 if (isl_stream_next_token_is(s
, ']'))
1044 while ((tok
= next_token(s
)) != NULL
) {
1047 res
= tuple_add_dim(res
, v
);
1049 if (tok
->type
== ISL_TOKEN_IDENT
) {
1051 int p
= vars_pos(v
, tok
->u
.s
, -1);
1057 if (tok
->type
== '*') {
1058 if (vars_add_anon(v
) < 0)
1060 isl_token_free(tok
);
1061 } else if (new_name
) {
1062 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
1063 isl_token_free(tok
);
1064 if (isl_stream_eat_if_available(s
, '='))
1065 res
= read_tuple_var_def(s
, res
, i
, v
,
1068 isl_stream_push_token(s
, tok
);
1070 if (vars_add_anon(v
) < 0)
1072 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
1075 tok
= isl_stream_next_token(s
);
1076 if (!comma
&& tok
&& tok
->type
== ']' &&
1077 isl_stream_next_token_is(s
, '[')) {
1078 isl_token_free(tok
);
1079 tok
= isl_stream_next_token(s
);
1080 } else if (!tok
|| tok
->type
!= ',')
1083 isl_token_free(tok
);
1087 isl_stream_push_token(s
, tok
);
1091 isl_token_free(tok
);
1092 return isl_multi_pw_aff_free(res
);
1095 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1097 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1098 struct vars
*v
, int rational
, int comma
)
1100 struct isl_token
*tok
;
1102 isl_multi_pw_aff
*res
= NULL
;
1104 tok
= isl_stream_next_token(s
);
1107 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1108 name
= strdup(tok
->u
.s
);
1109 isl_token_free(tok
);
1113 isl_stream_push_token(s
, tok
);
1114 if (isl_stream_eat(s
, '['))
1116 if (next_is_tuple(s
)) {
1117 isl_multi_pw_aff
*out
;
1119 res
= read_tuple(s
, v
, rational
, comma
);
1120 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1122 out
= read_tuple(s
, v
, rational
, comma
);
1123 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1124 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1125 res
= isl_multi_pw_aff_range_product(res
, out
);
1127 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1128 if (isl_stream_eat(s
, ']'))
1132 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1139 return isl_multi_pw_aff_free(res
);
1142 /* Read a tuple from "s" and add it to "map".
1143 * The tuple is initially represented as an isl_multi_pw_aff.
1144 * We first create the appropriate space in "map" based on the range
1145 * space of this isl_multi_pw_aff. Then, we add equalities based
1146 * on the affine expressions. These live in an anonymous space,
1147 * however, so we first need to reset the space to that of "map".
1149 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1150 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1151 int rational
, int comma
)
1154 isl_multi_pw_aff
*tuple
;
1155 isl_space
*space
= NULL
;
1157 tuple
= read_tuple(s
, v
, rational
, comma
);
1161 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1162 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1166 if (type
== isl_dim_param
) {
1167 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1168 isl_space_is_wrapping(space
)) {
1169 isl_die(s
->ctx
, isl_error_invalid
,
1170 "parameter tuples cannot be named or nested",
1173 map
= isl_map_add_dims(map
, type
, n
);
1174 for (i
= 0; i
< n
; ++i
) {
1176 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1177 isl_die(s
->ctx
, isl_error_invalid
,
1178 "parameters must be named",
1180 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1181 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1183 } else if (type
== isl_dim_in
) {
1186 set
= isl_set_universe(isl_space_copy(space
));
1188 set
= isl_set_set_rational(set
);
1189 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1190 map
= isl_map_from_domain(set
);
1194 set
= isl_set_universe(isl_space_copy(space
));
1196 set
= isl_set_set_rational(set
);
1197 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1200 for (i
= 0; i
< n
; ++i
) {
1207 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1208 space
= isl_pw_aff_get_domain_space(pa
);
1209 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1210 aff
= isl_aff_add_coefficient_si(aff
,
1211 isl_dim_in
, v
->n
- n
+ i
, -1);
1212 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1214 pa
= isl_pw_aff_set_rational(pa
);
1215 set
= isl_pw_aff_zero_set(pa
);
1216 map_i
= isl_map_from_range(set
);
1217 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1218 map
= isl_map_intersect(map
, map_i
);
1221 isl_space_free(space
);
1222 isl_multi_pw_aff_free(tuple
);
1225 isl_space_free(space
);
1226 isl_multi_pw_aff_free(tuple
);
1231 static __isl_give isl_set
*construct_constraints(
1232 __isl_take isl_set
*set
, int type
,
1233 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1238 left
= isl_pw_aff_list_copy(left
);
1239 right
= isl_pw_aff_list_copy(right
);
1241 left
= isl_pw_aff_list_set_rational(left
);
1242 right
= isl_pw_aff_list_set_rational(right
);
1244 if (type
== ISL_TOKEN_LE
)
1245 cond
= isl_pw_aff_list_le_set(left
, right
);
1246 else if (type
== ISL_TOKEN_GE
)
1247 cond
= isl_pw_aff_list_ge_set(left
, right
);
1248 else if (type
== ISL_TOKEN_LT
)
1249 cond
= isl_pw_aff_list_lt_set(left
, right
);
1250 else if (type
== ISL_TOKEN_GT
)
1251 cond
= isl_pw_aff_list_gt_set(left
, right
);
1252 else if (type
== ISL_TOKEN_NE
)
1253 cond
= isl_pw_aff_list_ne_set(left
, right
);
1255 cond
= isl_pw_aff_list_eq_set(left
, right
);
1257 return isl_set_intersect(set
, cond
);
1260 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1261 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1263 struct isl_token
*tok
= NULL
;
1264 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1267 set
= isl_map_wrap(map
);
1268 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1271 tok
= isl_stream_next_token(s
);
1272 if (!is_comparator(tok
)) {
1273 isl_stream_error(s
, tok
, "missing operator");
1275 isl_stream_push_token(s
, tok
);
1280 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1284 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1286 isl_token_free(tok
);
1287 isl_pw_aff_list_free(list1
);
1290 tok
= isl_stream_next_token(s
);
1291 if (!is_comparator(tok
)) {
1293 isl_stream_push_token(s
, tok
);
1297 isl_pw_aff_list_free(list1
);
1299 return isl_set_unwrap(set
);
1302 isl_token_free(tok
);
1303 isl_pw_aff_list_free(list1
);
1304 isl_pw_aff_list_free(list2
);
1309 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1310 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1313 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1315 map
= isl_map_from_domain(isl_map_wrap(map
));
1316 map
= read_defined_var_list(s
, v
, map
, rational
);
1318 if (isl_stream_eat(s
, ':'))
1321 map
= read_formula(s
, v
, map
, rational
);
1322 map
= isl_set_unwrap(isl_map_domain(map
));
1324 vars_drop(v
, v
->n
- n
);
1325 if (seen_paren
&& isl_stream_eat(s
, ')'))
1334 /* Parse an expression between parentheses and push the result
1335 * back on the stream.
1337 * The parsed expression may be either an affine expression
1338 * or a condition. The first type is pushed onto the stream
1339 * as an isl_pw_aff, while the second is pushed as an isl_map.
1341 * If the initial token indicates the start of a condition,
1342 * we parse it as such.
1343 * Otherwise, we first parse an affine expression and push
1344 * that onto the stream. If the affine expression covers the
1345 * entire expression between parentheses, we return.
1346 * Otherwise, we assume that the affine expression is the
1347 * start of a condition and continue parsing.
1349 static int resolve_paren_expr(struct isl_stream
*s
,
1350 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1352 struct isl_token
*tok
, *tok2
;
1356 tok
= isl_stream_next_token(s
);
1357 if (!tok
|| tok
->type
!= '(')
1360 if (isl_stream_next_token_is(s
, '('))
1361 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1364 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1365 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1366 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1367 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1368 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1369 map
= read_formula(s
, v
, map
, rational
);
1370 if (isl_stream_eat(s
, ')'))
1372 tok
->type
= ISL_TOKEN_MAP
;
1374 isl_stream_push_token(s
, tok
);
1378 tok2
= isl_stream_next_token(s
);
1383 isl_stream_push_token(s
, tok2
);
1385 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1389 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1392 tok2
->type
= ISL_TOKEN_AFF
;
1393 tok2
->u
.pwaff
= pwaff
;
1395 if (isl_stream_eat_if_available(s
, ')')) {
1396 isl_stream_push_token(s
, tok2
);
1397 isl_token_free(tok
);
1402 isl_stream_push_token(s
, tok2
);
1404 map
= read_formula(s
, v
, map
, rational
);
1405 if (isl_stream_eat(s
, ')'))
1408 tok
->type
= ISL_TOKEN_MAP
;
1410 isl_stream_push_token(s
, tok
);
1414 isl_pw_aff_free(pwaff
);
1416 isl_token_free(tok
);
1421 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1422 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1424 if (isl_stream_next_token_is(s
, '('))
1425 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1428 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1429 struct isl_token
*tok
;
1430 tok
= isl_stream_next_token(s
);
1434 map
= isl_map_copy(tok
->u
.map
);
1435 isl_token_free(tok
);
1439 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1440 return read_exists(s
, v
, map
, rational
);
1442 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1445 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1446 isl_space
*dim
= isl_map_get_space(map
);
1448 return isl_map_empty(dim
);
1451 return add_constraint(s
, v
, map
, rational
);
1457 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1458 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1463 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1464 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1466 res
= isl_map_subtract(isl_map_copy(map
), res
);
1468 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1471 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1472 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1474 res
= isl_map_subtract(res
, res_i
);
1476 res
= isl_map_intersect(res
, res_i
);
1483 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1484 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1488 if (isl_stream_next_token_is(s
, '}')) {
1489 isl_space
*dim
= isl_map_get_space(map
);
1491 return isl_map_universe(dim
);
1494 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1495 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1498 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1499 res
= isl_map_union(res
, res_i
);
1506 /* Read a first order formula from "s", add the corresponding
1507 * constraints to "map" and return the result.
1509 * In particular, read a formula of the form
1517 * where a and b are disjunctions.
1519 * In the first case, map is replaced by
1521 * map \cap { [..] : a }
1523 * In the second case, it is replaced by
1525 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1527 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
1528 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1532 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1534 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1537 res
= isl_map_subtract(isl_map_copy(map
), res
);
1538 res2
= read_disjuncts(s
, v
, map
, rational
);
1539 res
= isl_map_union(res
, res2
);
1546 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1548 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1549 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1550 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1551 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1553 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1554 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1555 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1557 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1558 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1559 isl_basic_map_dim(bmap
, isl_dim_in
) +
1560 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1561 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1563 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1569 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1570 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1573 struct isl_token
*tok
;
1583 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1584 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1586 tok
= isl_stream_next_token(s
);
1587 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1588 isl_stream_error(s
, tok
, "expecting coefficient");
1590 isl_stream_push_token(s
, tok
);
1593 if (!tok
->on_new_line
) {
1594 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1595 isl_stream_push_token(s
, tok
);
1599 type
= isl_int_get_si(tok
->u
.v
);
1600 isl_token_free(tok
);
1602 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1604 k
= isl_basic_map_alloc_equality(bmap
);
1607 k
= isl_basic_map_alloc_inequality(bmap
);
1613 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1615 tok
= isl_stream_next_token(s
);
1616 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1617 isl_stream_error(s
, tok
, "expecting coefficient");
1619 isl_stream_push_token(s
, tok
);
1622 if (tok
->on_new_line
) {
1623 isl_stream_error(s
, tok
,
1624 "coefficient should not appear on new line");
1625 isl_stream_push_token(s
, tok
);
1628 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1629 isl_int_set(c
[pos
], tok
->u
.v
);
1630 isl_token_free(tok
);
1635 isl_basic_map_free(bmap
);
1639 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1642 struct isl_token
*tok
;
1643 struct isl_token
*tok2
;
1646 unsigned in
= 0, out
, local
= 0;
1647 struct isl_basic_map
*bmap
= NULL
;
1650 tok
= isl_stream_next_token(s
);
1652 isl_stream_error(s
, NULL
, "unexpected EOF");
1655 tok2
= isl_stream_next_token(s
);
1657 isl_token_free(tok
);
1658 isl_stream_error(s
, NULL
, "unexpected EOF");
1661 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1662 isl_stream_push_token(s
, tok2
);
1663 isl_stream_push_token(s
, tok
);
1664 isl_stream_error(s
, NULL
,
1665 "expecting constraint matrix dimensions");
1668 n_row
= isl_int_get_si(tok
->u
.v
);
1669 n_col
= isl_int_get_si(tok2
->u
.v
);
1670 on_new_line
= tok2
->on_new_line
;
1671 isl_token_free(tok2
);
1672 isl_token_free(tok
);
1673 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1674 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1675 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1676 tok
= isl_stream_next_token_on_same_line(s
);
1678 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1679 isl_stream_error(s
, tok
,
1680 "expecting number of output dimensions");
1681 isl_stream_push_token(s
, tok
);
1684 out
= isl_int_get_si(tok
->u
.v
);
1685 isl_token_free(tok
);
1687 tok
= isl_stream_next_token_on_same_line(s
);
1688 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1689 isl_stream_error(s
, tok
,
1690 "expecting number of input dimensions");
1692 isl_stream_push_token(s
, tok
);
1695 in
= isl_int_get_si(tok
->u
.v
);
1696 isl_token_free(tok
);
1698 tok
= isl_stream_next_token_on_same_line(s
);
1699 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1700 isl_stream_error(s
, tok
,
1701 "expecting number of existentials");
1703 isl_stream_push_token(s
, tok
);
1706 local
= isl_int_get_si(tok
->u
.v
);
1707 isl_token_free(tok
);
1709 tok
= isl_stream_next_token_on_same_line(s
);
1710 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1711 isl_stream_error(s
, tok
,
1712 "expecting number of parameters");
1714 isl_stream_push_token(s
, tok
);
1717 nparam
= isl_int_get_si(tok
->u
.v
);
1718 isl_token_free(tok
);
1719 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1720 isl_stream_error(s
, NULL
,
1721 "dimensions don't match");
1725 out
= n_col
- 2 - nparam
;
1726 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1730 for (i
= 0; i
< local
; ++i
) {
1731 int k
= isl_basic_map_alloc_div(bmap
);
1734 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1737 for (i
= 0; i
< n_row
; ++i
)
1738 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1740 tok
= isl_stream_next_token_on_same_line(s
);
1742 isl_stream_error(s
, tok
, "unexpected extra token on line");
1743 isl_stream_push_token(s
, tok
);
1747 bmap
= isl_basic_map_simplify(bmap
);
1748 bmap
= isl_basic_map_finalize(bmap
);
1751 isl_basic_map_free(bmap
);
1755 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1757 struct isl_token
*tok
;
1758 struct isl_token
*tok2
;
1760 struct isl_map
*map
;
1762 tok
= isl_stream_next_token(s
);
1764 isl_stream_error(s
, NULL
, "unexpected EOF");
1767 tok2
= isl_stream_next_token_on_same_line(s
);
1768 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1769 isl_stream_push_token(s
, tok2
);
1770 isl_stream_push_token(s
, tok
);
1771 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1774 isl_stream_error(s
, tok2
, "unexpected token");
1775 isl_stream_push_token(s
, tok2
);
1776 isl_stream_push_token(s
, tok
);
1779 n
= isl_int_get_si(tok
->u
.v
);
1780 isl_token_free(tok
);
1782 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1784 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1786 for (i
= 1; map
&& i
< n
; ++i
)
1787 map
= isl_map_union(map
,
1788 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1793 static int optional_power(struct isl_stream
*s
)
1796 struct isl_token
*tok
;
1798 tok
= isl_stream_next_token(s
);
1801 if (tok
->type
!= '^') {
1802 isl_stream_push_token(s
, tok
);
1805 isl_token_free(tok
);
1806 tok
= isl_stream_next_token(s
);
1807 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1808 isl_stream_error(s
, tok
, "expecting exponent");
1810 isl_stream_push_token(s
, tok
);
1813 pow
= isl_int_get_si(tok
->u
.v
);
1814 isl_token_free(tok
);
1818 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1819 __isl_keep isl_map
*map
, struct vars
*v
);
1821 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1822 __isl_keep isl_map
*map
, struct vars
*v
)
1824 isl_pw_qpolynomial
*pwqp
;
1825 struct isl_token
*tok
;
1827 tok
= next_token(s
);
1829 isl_stream_error(s
, NULL
, "unexpected EOF");
1832 if (tok
->type
== '(') {
1835 isl_token_free(tok
);
1836 pwqp
= read_term(s
, map
, v
);
1839 if (isl_stream_eat(s
, ')'))
1841 pow
= optional_power(s
);
1842 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1843 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1844 struct isl_token
*tok2
;
1845 isl_qpolynomial
*qp
;
1847 tok2
= isl_stream_next_token(s
);
1848 if (tok2
&& tok2
->type
== '/') {
1849 isl_token_free(tok2
);
1850 tok2
= next_token(s
);
1851 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1852 isl_stream_error(s
, tok2
, "expected denominator");
1853 isl_token_free(tok
);
1854 isl_token_free(tok2
);
1857 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1858 tok
->u
.v
, tok2
->u
.v
);
1859 isl_token_free(tok2
);
1861 isl_stream_push_token(s
, tok2
);
1862 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1865 isl_token_free(tok
);
1866 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1867 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1868 isl_qpolynomial
*qp
;
1869 isl_token_free(tok
);
1870 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1871 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1872 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1873 isl_qpolynomial
*qp
;
1874 isl_token_free(tok
);
1875 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1876 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1877 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1879 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1881 isl_qpolynomial
*qp
;
1883 isl_token_free(tok
);
1887 vars_drop(v
, v
->n
- n
);
1888 isl_stream_error(s
, tok
, "unknown identifier");
1889 isl_token_free(tok
);
1892 isl_token_free(tok
);
1893 pow
= optional_power(s
);
1894 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
1895 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1896 } else if (is_start_of_div(tok
)) {
1900 isl_stream_push_token(s
, tok
);
1901 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
1902 pow
= optional_power(s
);
1903 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
1904 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1905 } else if (tok
->type
== '-') {
1906 isl_token_free(tok
);
1907 pwqp
= read_factor(s
, map
, v
);
1908 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
1910 isl_stream_error(s
, tok
, "unexpected isl_token");
1911 isl_stream_push_token(s
, tok
);
1915 if (isl_stream_eat_if_available(s
, '*') ||
1916 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1917 isl_pw_qpolynomial
*pwqp2
;
1919 pwqp2
= read_factor(s
, map
, v
);
1920 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
1925 isl_pw_qpolynomial_free(pwqp
);
1929 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1930 __isl_keep isl_map
*map
, struct vars
*v
)
1932 struct isl_token
*tok
;
1933 isl_pw_qpolynomial
*pwqp
;
1935 pwqp
= read_factor(s
, map
, v
);
1938 tok
= next_token(s
);
1942 if (tok
->type
== '+') {
1943 isl_pw_qpolynomial
*pwqp2
;
1945 isl_token_free(tok
);
1946 pwqp2
= read_factor(s
, map
, v
);
1947 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1948 } else if (tok
->type
== '-') {
1949 isl_pw_qpolynomial
*pwqp2
;
1951 isl_token_free(tok
);
1952 pwqp2
= read_factor(s
, map
, v
);
1953 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
1954 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1955 isl_int_is_neg(tok
->u
.v
)) {
1956 isl_pw_qpolynomial
*pwqp2
;
1958 isl_stream_push_token(s
, tok
);
1959 pwqp2
= read_factor(s
, map
, v
);
1960 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1962 isl_stream_push_token(s
, tok
);
1970 static __isl_give isl_map
*read_optional_formula(struct isl_stream
*s
,
1971 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
1973 struct isl_token
*tok
;
1975 tok
= isl_stream_next_token(s
);
1977 isl_stream_error(s
, NULL
, "unexpected EOF");
1980 if (tok
->type
== ':' ||
1981 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
1982 isl_token_free(tok
);
1983 map
= read_formula(s
, v
, map
, rational
);
1985 isl_stream_push_token(s
, tok
);
1993 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1994 __isl_take isl_map
*map
, struct vars
*v
, int n
)
1996 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1997 isl_pw_qpolynomial
*pwqp
;
1998 struct isl_set
*set
;
2000 pwqp
= read_term(s
, map
, v
);
2001 map
= read_optional_formula(s
, map
, v
, 0);
2002 set
= isl_map_range(map
);
2004 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2006 vars_drop(v
, v
->n
- n
);
2012 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
2013 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2015 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2016 isl_pw_qpolynomial
*pwqp
;
2017 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2019 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2020 return obj_read_poly(s
, set
, v
, n
);
2022 if (isl_stream_eat(s
, '('))
2025 pwqp
= read_term(s
, set
, v
);
2026 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2028 while (isl_stream_eat_if_available(s
, ',')) {
2029 isl_pw_qpolynomial_fold
*pwf_i
;
2030 pwqp
= read_term(s
, set
, v
);
2031 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2033 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2036 if (isl_stream_eat(s
, ')'))
2039 set
= read_optional_formula(s
, set
, v
, 0);
2040 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2042 vars_drop(v
, v
->n
- n
);
2048 isl_pw_qpolynomial_fold_free(pwf
);
2049 obj
.type
= isl_obj_none
;
2053 static int is_rational(struct isl_stream
*s
)
2055 struct isl_token
*tok
;
2057 tok
= isl_stream_next_token(s
);
2060 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2061 isl_token_free(tok
);
2062 isl_stream_eat(s
, ':');
2066 isl_stream_push_token(s
, tok
);
2071 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2072 __isl_take isl_map
*map
, struct vars
*v
)
2074 struct isl_token
*tok
;
2075 struct isl_obj obj
= { isl_obj_set
, NULL
};
2079 rational
= is_rational(s
);
2081 map
= isl_map_set_rational(map
);
2083 if (isl_stream_next_token_is(s
, ':')) {
2084 obj
.type
= isl_obj_set
;
2085 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2089 if (!next_is_tuple(s
))
2090 return obj_read_poly_or_fold(s
, map
, v
, n
);
2092 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2095 tok
= isl_stream_next_token(s
);
2098 if (tok
->type
== ISL_TOKEN_TO
) {
2099 obj
.type
= isl_obj_map
;
2100 isl_token_free(tok
);
2101 if (!next_is_tuple(s
)) {
2102 isl_set
*set
= isl_map_domain(map
);
2103 return obj_read_poly_or_fold(s
, set
, v
, n
);
2105 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2109 map
= isl_map_domain(map
);
2110 isl_stream_push_token(s
, tok
);
2113 map
= read_optional_formula(s
, map
, v
, rational
);
2115 vars_drop(v
, v
->n
- n
);
2121 obj
.type
= isl_obj_none
;
2125 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2127 if (obj
.type
== isl_obj_map
) {
2128 obj
.v
= isl_union_map_from_map(obj
.v
);
2129 obj
.type
= isl_obj_union_map
;
2130 } else if (obj
.type
== isl_obj_set
) {
2131 obj
.v
= isl_union_set_from_set(obj
.v
);
2132 obj
.type
= isl_obj_union_set
;
2133 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2134 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2135 obj
.type
= isl_obj_union_pw_qpolynomial
;
2136 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2137 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2138 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2140 isl_assert(ctx
, 0, goto error
);
2143 obj
.type
->free(obj
.v
);
2144 obj
.type
= isl_obj_none
;
2148 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2149 struct isl_obj obj1
, struct isl_obj obj2
)
2151 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2152 obj1
= to_union(ctx
, obj1
);
2153 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2154 obj2
= to_union(ctx
, obj2
);
2155 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2156 obj1
= to_union(ctx
, obj1
);
2157 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2158 obj2
= to_union(ctx
, obj2
);
2159 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2160 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2161 obj1
= to_union(ctx
, obj1
);
2162 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2163 obj2
.type
== isl_obj_pw_qpolynomial
)
2164 obj2
= to_union(ctx
, obj2
);
2165 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2166 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2167 obj1
= to_union(ctx
, obj1
);
2168 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2169 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2170 obj2
= to_union(ctx
, obj2
);
2171 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2172 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2173 obj1
= to_union(ctx
, obj1
);
2174 obj2
= to_union(ctx
, obj2
);
2176 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2177 obj1
= to_union(ctx
, obj1
);
2178 obj2
= to_union(ctx
, obj2
);
2180 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2181 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2182 obj1
= to_union(ctx
, obj1
);
2183 obj2
= to_union(ctx
, obj2
);
2185 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2186 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2187 obj1
= to_union(ctx
, obj1
);
2188 obj2
= to_union(ctx
, obj2
);
2190 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2193 obj1
.type
->free(obj1
.v
);
2194 obj2
.type
->free(obj2
.v
);
2195 obj1
.type
= isl_obj_none
;
2200 static struct isl_obj
obj_read(struct isl_stream
*s
)
2202 isl_map
*map
= NULL
;
2203 struct isl_token
*tok
;
2204 struct vars
*v
= NULL
;
2205 struct isl_obj obj
= { isl_obj_set
, NULL
};
2207 tok
= next_token(s
);
2209 isl_stream_error(s
, NULL
, "unexpected EOF");
2212 if (tok
->type
== ISL_TOKEN_VALUE
) {
2213 struct isl_token
*tok2
;
2214 struct isl_map
*map
;
2216 tok2
= isl_stream_next_token(s
);
2217 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2218 isl_int_is_neg(tok2
->u
.v
)) {
2220 isl_stream_push_token(s
, tok2
);
2221 obj
.type
= isl_obj_val
;
2222 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2223 isl_token_free(tok
);
2226 isl_stream_push_token(s
, tok2
);
2227 isl_stream_push_token(s
, tok
);
2228 map
= map_read_polylib(s
);
2231 if (isl_map_may_be_set(map
))
2232 obj
.v
= isl_map_range(map
);
2234 obj
.type
= isl_obj_map
;
2239 v
= vars_new(s
->ctx
);
2241 isl_stream_push_token(s
, tok
);
2244 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2245 if (tok
->type
== '[') {
2246 isl_stream_push_token(s
, tok
);
2247 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2250 tok
= isl_stream_next_token(s
);
2251 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2252 isl_stream_error(s
, tok
, "expecting '->'");
2254 isl_stream_push_token(s
, tok
);
2257 isl_token_free(tok
);
2258 tok
= isl_stream_next_token(s
);
2260 if (!tok
|| tok
->type
!= '{') {
2261 isl_stream_error(s
, tok
, "expecting '{'");
2263 isl_stream_push_token(s
, tok
);
2266 isl_token_free(tok
);
2268 tok
= isl_stream_next_token(s
);
2271 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2272 isl_token_free(tok
);
2273 if (isl_stream_eat(s
, '='))
2275 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2278 } else if (tok
->type
== '}') {
2279 obj
.type
= isl_obj_union_set
;
2280 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2281 isl_token_free(tok
);
2284 isl_stream_push_token(s
, tok
);
2289 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2290 if (o
.type
== isl_obj_none
|| !o
.v
)
2295 obj
= obj_add(s
->ctx
, obj
, o
);
2296 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2299 tok
= isl_stream_next_token(s
);
2300 if (!tok
|| tok
->type
!= ';')
2302 isl_token_free(tok
);
2303 if (isl_stream_next_token_is(s
, '}')) {
2304 tok
= isl_stream_next_token(s
);
2309 if (tok
&& tok
->type
== '}') {
2310 isl_token_free(tok
);
2312 isl_stream_error(s
, tok
, "unexpected isl_token");
2314 isl_token_free(tok
);
2324 obj
.type
->free(obj
.v
);
2331 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2336 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2342 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2343 obj
.type
== isl_obj_set
, goto error
);
2345 if (obj
.type
== isl_obj_set
)
2346 obj
.v
= isl_map_from_range(obj
.v
);
2350 obj
.type
->free(obj
.v
);
2354 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2360 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2361 obj
.v
= isl_map_range(obj
.v
);
2362 obj
.type
= isl_obj_set
;
2364 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2369 obj
.type
->free(obj
.v
);
2373 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2378 if (obj
.type
== isl_obj_map
) {
2379 obj
.type
= isl_obj_union_map
;
2380 obj
.v
= isl_union_map_from_map(obj
.v
);
2382 if (obj
.type
== isl_obj_set
) {
2383 obj
.type
= isl_obj_union_set
;
2384 obj
.v
= isl_union_set_from_set(obj
.v
);
2386 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2387 isl_union_set_is_empty(obj
.v
))
2388 obj
.type
= isl_obj_union_map
;
2389 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2390 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2394 obj
.type
->free(obj
.v
);
2398 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2403 if (obj
.type
== isl_obj_set
) {
2404 obj
.type
= isl_obj_union_set
;
2405 obj
.v
= isl_union_set_from_set(obj
.v
);
2408 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2412 obj
.type
->free(obj
.v
);
2416 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2419 struct isl_map
*map
;
2420 struct isl_basic_map
*bmap
;
2427 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2430 bmap
= isl_basic_map_empty_like_map(map
);
2432 bmap
= isl_basic_map_copy(map
->p
[0]);
2442 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2444 isl_basic_map
*bmap
;
2445 bmap
= basic_map_read(s
);
2448 if (!isl_basic_map_may_be_set(bmap
))
2449 isl_die(s
->ctx
, isl_error_invalid
,
2450 "input is not a set", goto error
);
2451 return isl_basic_map_range(bmap
);
2453 isl_basic_map_free(bmap
);
2457 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2460 struct isl_basic_map
*bmap
;
2461 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2464 bmap
= basic_map_read(s
);
2469 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2472 isl_basic_set
*bset
;
2473 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2476 bset
= basic_set_read(s
);
2481 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2484 struct isl_basic_map
*bmap
;
2485 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2488 bmap
= basic_map_read(s
);
2493 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2496 isl_basic_set
*bset
;
2497 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2500 bset
= basic_set_read(s
);
2505 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2508 struct isl_map
*map
;
2509 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2512 map
= isl_stream_read_map(s
);
2517 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2520 struct isl_map
*map
;
2521 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2524 map
= isl_stream_read_map(s
);
2529 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2533 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2536 set
= isl_stream_read_set(s
);
2541 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2545 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2548 set
= isl_stream_read_set(s
);
2553 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2556 isl_union_map
*umap
;
2557 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2560 umap
= isl_stream_read_union_map(s
);
2565 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2568 isl_union_map
*umap
;
2569 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2572 umap
= isl_stream_read_union_map(s
);
2577 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2580 isl_union_set
*uset
;
2581 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2584 uset
= isl_stream_read_union_set(s
);
2589 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2592 isl_union_set
*uset
;
2593 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2596 uset
= isl_stream_read_union_set(s
);
2601 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2603 struct isl_vec
*vec
= NULL
;
2604 struct isl_token
*tok
;
2608 tok
= isl_stream_next_token(s
);
2609 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2610 isl_stream_error(s
, tok
, "expecting vector length");
2614 size
= isl_int_get_si(tok
->u
.v
);
2615 isl_token_free(tok
);
2617 vec
= isl_vec_alloc(s
->ctx
, size
);
2619 for (j
= 0; j
< size
; ++j
) {
2620 tok
= isl_stream_next_token(s
);
2621 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2622 isl_stream_error(s
, tok
, "expecting constant value");
2625 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2626 isl_token_free(tok
);
2631 isl_token_free(tok
);
2636 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2638 return isl_vec_read_polylib(s
);
2641 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2644 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2652 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2653 struct isl_stream
*s
)
2659 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2664 obj
.type
->free(obj
.v
);
2668 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2671 isl_pw_qpolynomial
*pwqp
;
2672 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2675 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2680 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2683 isl_pw_qpolynomial
*pwqp
;
2684 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2687 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2692 /* Is the next token an identifer not in "v"?
2694 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2698 struct isl_token
*tok
;
2700 tok
= isl_stream_next_token(s
);
2703 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2704 isl_stream_push_token(s
, tok
);
2706 vars_drop(v
, v
->n
- n
);
2711 /* First read the domain of the affine expression, which may be
2712 * a parameter space or a set.
2713 * The tricky part is that we don't know if the domain is a set or not,
2714 * so when we are trying to read the domain, we may actually be reading
2715 * the affine expression itself (defined on a parameter domains)
2716 * If the tuple we are reading is named, we assume it's the domain.
2717 * Also, if inside the tuple, the first thing we find is a nested tuple
2718 * or a new identifier, we again assume it's the domain.
2719 * Otherwise, we assume we are reading an affine expression.
2721 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2722 __isl_take isl_set
*dom
, struct vars
*v
)
2724 struct isl_token
*tok
;
2726 tok
= isl_stream_next_token(s
);
2727 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2728 isl_stream_push_token(s
, tok
);
2729 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2731 if (!tok
|| tok
->type
!= '[') {
2732 isl_stream_error(s
, tok
, "expecting '['");
2735 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2736 isl_stream_push_token(s
, tok
);
2737 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2739 isl_stream_push_token(s
, tok
);
2744 isl_stream_push_token(s
, tok
);
2749 /* Read an affine expression from "s".
2751 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2756 ma
= isl_stream_read_multi_aff(s
);
2759 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2760 isl_die(s
->ctx
, isl_error_invalid
,
2761 "expecting single affine expression",
2764 aff
= isl_multi_aff_get_aff(ma
, 0);
2765 isl_multi_aff_free(ma
);
2768 isl_multi_aff_free(ma
);
2772 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2774 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2775 __isl_take isl_set
*dom
, struct vars
*v
)
2777 isl_pw_aff
*pwaff
= NULL
;
2779 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2782 if (isl_stream_eat(s
, '['))
2785 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2787 if (isl_stream_eat(s
, ']'))
2790 dom
= read_optional_formula(s
, dom
, v
, 0);
2791 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2796 isl_pw_aff_free(pwaff
);
2800 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2803 isl_set
*dom
= NULL
;
2805 isl_pw_aff
*pa
= NULL
;
2808 v
= vars_new(s
->ctx
);
2812 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2813 if (next_is_tuple(s
)) {
2814 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2815 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2818 if (isl_stream_eat(s
, '{'))
2822 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2823 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2824 vars_drop(v
, v
->n
- n
);
2826 while (isl_stream_eat_if_available(s
, ';')) {
2830 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2831 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2832 vars_drop(v
, v
->n
- n
);
2834 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2837 if (isl_stream_eat(s
, '}'))
2846 isl_pw_aff_free(pa
);
2850 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2853 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2856 aff
= isl_stream_read_aff(s
);
2861 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2864 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2867 pa
= isl_stream_read_pw_aff(s
);
2872 /* Read an isl_pw_multi_aff from "s".
2873 * We currently read a generic object and if it turns out to be a set or
2874 * a map, we convert that to an isl_pw_multi_aff.
2875 * It would be more efficient if we were to construct the isl_pw_multi_aff
2878 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
2886 if (obj
.type
== isl_obj_map
)
2887 return isl_pw_multi_aff_from_map(obj
.v
);
2888 if (obj
.type
== isl_obj_set
)
2889 return isl_pw_multi_aff_from_set(obj
.v
);
2891 obj
.type
->free(obj
.v
);
2892 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2896 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
2899 isl_pw_multi_aff
*pma
;
2900 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2903 pma
= isl_stream_read_pw_multi_aff(s
);
2908 /* Read an isl_union_pw_multi_aff from "s".
2909 * We currently read a generic object and if it turns out to be a set or
2910 * a map, we convert that to an isl_union_pw_multi_aff.
2911 * It would be more efficient if we were to construct
2912 * the isl_union_pw_multi_aff directly.
2914 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
2915 struct isl_stream
*s
)
2923 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
2924 obj
= to_union(s
->ctx
, obj
);
2925 if (obj
.type
== isl_obj_union_map
)
2926 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
2927 if (obj
.type
== isl_obj_union_set
)
2928 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
2930 obj
.type
->free(obj
.v
);
2931 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2935 /* Read an isl_union_pw_multi_aff from "str".
2937 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
2938 isl_ctx
*ctx
, const char *str
)
2940 isl_union_pw_multi_aff
*upma
;
2941 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2944 upma
= isl_stream_read_union_pw_multi_aff(s
);
2949 /* Assuming "pa" represents a single affine expression defined on a universe
2950 * domain, extract this affine expression.
2952 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
2959 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2960 "expecting single affine expression",
2962 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
2963 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2964 "expecting universe domain",
2967 aff
= isl_aff_copy(pa
->p
[0].aff
);
2968 isl_pw_aff_free(pa
);
2971 isl_pw_aff_free(pa
);
2975 /* Read a multi-affine expression from "s".
2976 * If the multi-affine expression has a domain, then then tuple
2977 * representing this domain cannot involve any affine expressions.
2978 * The tuple representing the actual expressions needs to consist
2979 * of only affine expressions. Moreover, these expressions can
2980 * only depend on parameters and input dimensions and not on other
2981 * output dimensions.
2983 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
2986 isl_set
*dom
= NULL
;
2987 isl_multi_pw_aff
*tuple
= NULL
;
2989 isl_space
*space
, *dom_space
;
2990 isl_multi_aff
*ma
= NULL
;
2992 v
= vars_new(s
->ctx
);
2996 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2997 if (next_is_tuple(s
)) {
2998 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2999 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3002 if (!isl_set_plain_is_universe(dom
))
3003 isl_die(s
->ctx
, isl_error_invalid
,
3004 "expecting universe parameter domain", goto error
);
3005 if (isl_stream_eat(s
, '{'))
3008 tuple
= read_tuple(s
, v
, 0, 0);
3011 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3016 has_expr
= tuple_has_expr(tuple
);
3020 isl_die(s
->ctx
, isl_error_invalid
,
3021 "expecting universe domain", goto error
);
3022 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3023 set
= isl_set_universe(space
);
3024 dom
= isl_set_intersect_params(set
, dom
);
3025 isl_multi_pw_aff_free(tuple
);
3026 tuple
= read_tuple(s
, v
, 0, 0);
3031 if (isl_stream_eat(s
, '}'))
3034 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3035 dim
= isl_set_dim(dom
, isl_dim_all
);
3036 dom_space
= isl_set_get_space(dom
);
3037 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3038 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3039 if (!isl_space_is_params(dom_space
))
3040 space
= isl_space_map_from_domain_and_range(
3041 isl_space_copy(dom_space
), space
);
3042 isl_space_free(dom_space
);
3043 ma
= isl_multi_aff_alloc(space
);
3045 for (i
= 0; i
< n
; ++i
) {
3048 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3049 aff
= aff_from_pw_aff(pa
);
3052 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3054 isl_die(s
->ctx
, isl_error_invalid
,
3055 "not an affine expression", goto error
);
3057 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3058 space
= isl_multi_aff_get_domain_space(ma
);
3059 aff
= isl_aff_reset_domain_space(aff
, space
);
3060 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3063 isl_multi_pw_aff_free(tuple
);
3068 isl_multi_pw_aff_free(tuple
);
3071 isl_multi_aff_free(ma
);
3075 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3078 isl_multi_aff
*maff
;
3079 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3082 maff
= isl_stream_read_multi_aff(s
);
3087 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3088 struct isl_stream
*s
)
3093 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3094 obj
.type
= isl_obj_union_pw_qpolynomial
;
3095 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3098 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3103 obj
.type
->free(obj
.v
);
3107 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3108 isl_ctx
*ctx
, const char *str
)
3110 isl_union_pw_qpolynomial
*upwqp
;
3111 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3114 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);