2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
22 #include <isl_stream_private.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_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
*space
, 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 /* Return a piecewise affine expression defined on the specified domain
557 * that represents NaN.
559 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
563 ls
= isl_local_space_from_space(isl_space_copy(space
));
564 return isl_pw_aff_nan_on_domain(ls
);
567 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
568 __isl_take isl_space
*space
, struct vars
*v
)
570 struct isl_token
*tok
= NULL
;
575 ls
= isl_local_space_from_space(isl_space_copy(space
));
576 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
583 isl_stream_error(s
, NULL
, "unexpected EOF");
586 if (tok
->type
== '-') {
591 if (tok
->type
== '(' || is_start_of_div(tok
) ||
592 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
593 tok
->type
== ISL_TOKEN_IDENT
||
594 tok
->type
== ISL_TOKEN_AFF
) {
596 isl_stream_push_token(s
, tok
);
598 term
= accept_affine_factor(s
,
599 isl_space_copy(space
), v
);
601 res
= isl_pw_aff_sub(res
, term
);
603 res
= isl_pw_aff_add(res
, term
);
607 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
609 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
610 if (isl_stream_eat_if_available(s
, '*') ||
611 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
613 term
= accept_affine_factor(s
,
614 isl_space_copy(space
), v
);
615 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
616 res
= isl_pw_aff_add(res
, term
);
620 res
= add_cst(res
, tok
->u
.v
);
623 } else if (tok
->type
== ISL_TOKEN_NAN
) {
624 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
626 isl_stream_error(s
, tok
, "unexpected isl_token");
627 isl_stream_push_token(s
, tok
);
628 isl_pw_aff_free(res
);
629 isl_space_free(space
);
635 if (tok
&& tok
->type
== '-') {
638 } else if (tok
&& tok
->type
== '+') {
641 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
642 isl_int_is_neg(tok
->u
.v
)) {
643 isl_stream_push_token(s
, tok
);
646 isl_stream_push_token(s
, tok
);
651 isl_space_free(space
);
654 isl_space_free(space
);
656 isl_pw_aff_free(res
);
660 static int is_comparator(struct isl_token
*tok
)
678 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
679 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
680 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
681 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
683 /* Accept a ternary operator, given the first argument.
685 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
686 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
689 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
694 if (isl_stream_eat(s
, '?'))
697 dim
= isl_space_wrap(isl_map_get_space(cond
));
698 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
702 if (isl_stream_eat(s
, ':'))
705 dim
= isl_pw_aff_get_domain_space(pwaff1
);
706 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
710 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
711 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
714 isl_pw_aff_free(pwaff1
);
715 isl_pw_aff_free(pwaff2
);
719 /* Set *line and *col to those of the next token, if any.
721 static void set_current_line_col(struct isl_stream
*s
, int *line
, int *col
)
723 struct isl_token
*tok
;
725 tok
= isl_stream_next_token(s
);
731 isl_stream_push_token(s
, tok
);
734 /* Push a token encapsulating "pa" onto "s", with the given
737 static int push_aff(struct isl_stream
*s
, int line
, int col
,
738 __isl_take isl_pw_aff
*pa
)
740 struct isl_token
*tok
;
742 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
745 tok
->type
= ISL_TOKEN_AFF
;
747 isl_stream_push_token(s
, tok
);
755 /* Accept an affine expression that may involve ternary operators.
756 * We first read an affine expression.
757 * If it is not followed by a comparison operator, we simply return it.
758 * Otherwise, we assume the affine expression is part of the first
759 * argument of a ternary operator and try to parse that.
761 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
762 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
767 struct isl_token
*tok
;
768 int line
= -1, col
= -1;
771 set_current_line_col(s
, &line
, &col
);
773 pwaff
= accept_affine(s
, dim
, v
);
775 pwaff
= isl_pw_aff_set_rational(pwaff
);
779 tok
= isl_stream_next_token(s
);
781 return isl_pw_aff_free(pwaff
);
783 is_comp
= is_comparator(tok
);
784 isl_stream_push_token(s
, tok
);
788 space
= isl_pw_aff_get_domain_space(pwaff
);
789 cond
= isl_map_universe(isl_space_unwrap(space
));
791 if (push_aff(s
, line
, col
, pwaff
) < 0)
792 cond
= isl_map_free(cond
);
796 cond
= read_formula(s
, v
, cond
, rational
);
798 return accept_ternary(s
, cond
, v
, rational
);
801 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
802 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
809 if (type
== isl_dim_param
)
810 pos
= isl_map_dim(map
, isl_dim_param
);
812 pos
= isl_map_dim(map
, isl_dim_in
);
813 if (type
== isl_dim_out
)
814 pos
+= isl_map_dim(map
, isl_dim_out
);
819 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
821 def_map
= isl_map_from_pw_aff(def
);
822 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
823 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
825 map
= isl_map_intersect(map
, def_map
);
830 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
831 __isl_take isl_space
*dim
, struct vars
*v
)
834 isl_pw_aff_list
*list
;
835 struct isl_token
*tok
= NULL
;
837 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
838 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
843 tok
= isl_stream_next_token(s
);
845 isl_stream_error(s
, NULL
, "unexpected EOF");
848 if (tok
->type
!= ',') {
849 isl_stream_push_token(s
, tok
);
854 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
855 list
= isl_pw_aff_list_concat(list
,
856 isl_pw_aff_list_from_pw_aff(pwaff
));
865 isl_pw_aff_list_free(list
);
869 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
870 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
872 struct isl_token
*tok
;
874 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
878 if (tok
->type
!= ISL_TOKEN_IDENT
)
881 p
= vars_pos(v
, tok
->u
.s
, -1);
885 isl_stream_error(s
, tok
, "expecting unique identifier");
889 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
892 tok
= isl_stream_next_token(s
);
893 if (tok
&& tok
->type
== '=') {
895 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
896 tok
= isl_stream_next_token(s
);
899 if (!tok
|| tok
->type
!= ',')
905 isl_stream_push_token(s
, tok
);
914 static int next_is_tuple(struct isl_stream
*s
)
916 struct isl_token
*tok
;
919 tok
= isl_stream_next_token(s
);
922 if (tok
->type
== '[') {
923 isl_stream_push_token(s
, tok
);
926 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
927 isl_stream_push_token(s
, tok
);
931 is_tuple
= isl_stream_next_token_is(s
, '[');
933 isl_stream_push_token(s
, tok
);
938 /* Is "pa" an expression in term of earlier dimensions?
939 * The alternative is that the dimension is defined to be equal to itself,
940 * meaning that it has a universe domain and an expression that depends
941 * on itself. "i" is the position of the expression in a sequence
942 * of "n" expressions. The final dimensions of "pa" correspond to
943 * these "n" expressions.
945 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
953 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
957 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
962 /* Does the tuple contain any dimensions that are defined
963 * in terms of earlier dimensions?
965 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
973 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
974 for (i
= 0; i
< n
; ++i
) {
975 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
976 has_expr
= pw_aff_is_expr(pa
, i
, n
);
978 if (has_expr
< 0 || has_expr
)
985 /* Set the name of dimension "pos" in "space" to "name".
986 * During printing, we add primes if the same name appears more than once
987 * to distinguish the occurrences. Here, we remove those primes from "name"
988 * before setting the name of the dimension.
990 static __isl_give isl_space
*space_set_dim_name(__isl_take isl_space
*space
,
998 prime
= strchr(name
, '\'');
1001 space
= isl_space_set_dim_name(space
, isl_dim_out
, pos
, name
);
1008 /* Accept a piecewise affine expression.
1010 * At the outer level, the piecewise affine expression may be of the form
1012 * aff1 : condition1; aff2 : conditions2; ...
1018 * each of the affine expressions may in turn include ternary operators.
1020 * There may be parentheses around some subexpression of "aff1"
1021 * around "aff1" itself, around "aff1 : condition1" and/or
1022 * around the entire piecewise affine expression.
1023 * We therefore remove the opening parenthesis (if any) from the stream
1024 * in case the closing parenthesis follows the colon, but if the closing
1025 * parenthesis is the first thing in the stream after the parsed affine
1026 * expression, we push the parsed expression onto the stream and parse
1027 * again in case the parentheses enclose some subexpression of "aff1".
1029 static __isl_give isl_pw_aff
*accept_piecewise_affine(struct isl_stream
*s
,
1030 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1033 isl_space
*res_space
;
1035 res_space
= isl_space_from_domain(isl_space_copy(space
));
1036 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1037 res
= isl_pw_aff_empty(res_space
);
1041 int line
= -1, col
= -1;
1043 set_current_line_col(s
, &line
, &col
);
1044 seen_paren
= isl_stream_eat_if_available(s
, '(');
1046 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1049 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1051 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1053 if (push_aff(s
, line
, col
, pa
) < 0)
1055 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1058 if (isl_stream_eat_if_available(s
, ':')) {
1059 isl_space
*dom_space
;
1062 dom_space
= isl_pw_aff_get_domain_space(pa
);
1063 dom
= isl_set_universe(dom_space
);
1064 dom
= read_formula(s
, v
, dom
, rational
);
1065 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1068 res
= isl_pw_aff_union_add(res
, pa
);
1070 if (seen_paren
&& isl_stream_eat(s
, ')'))
1072 } while (isl_stream_eat_if_available(s
, ';'));
1074 isl_space_free(space
);
1078 isl_space_free(space
);
1079 return isl_pw_aff_free(res
);
1082 /* Read an affine expression from "s" for use in read_tuple.
1084 * accept_extended_affine requires a wrapped space as input.
1085 * read_tuple on the other hand expects each isl_pw_aff
1086 * to have an anonymous space. We therefore adjust the space
1087 * of the isl_pw_aff before returning it.
1089 static __isl_give isl_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
1090 struct vars
*v
, int rational
)
1095 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1097 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1099 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1100 def
= isl_pw_aff_reset_domain_space(def
, space
);
1105 /* Read a list of tuple elements by calling "read_el" on each of them and
1106 * return a space with the same number of set dimensions derived from
1107 * the parameter space "space" and possibly updated by "read_el".
1108 * The elements in the list are separated by either "," or "][".
1109 * If "comma" is set then only "," is allowed.
1111 static __isl_give isl_space
*read_tuple_list(struct isl_stream
*s
,
1112 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1113 __isl_give isl_space
*(*read_el
)(struct isl_stream
*s
, struct vars
*v
,
1114 __isl_take isl_space
*space
, int rational
, void *user
),
1120 space
= isl_space_set_from_params(space
);
1122 if (isl_stream_next_token_is(s
, ']'))
1126 struct isl_token
*tok
;
1128 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1130 space
= read_el(s
, v
, space
, rational
, user
);
1134 tok
= isl_stream_next_token(s
);
1135 if (!comma
&& tok
&& tok
->type
== ']' &&
1136 isl_stream_next_token_is(s
, '[')) {
1137 isl_token_free(tok
);
1138 tok
= isl_stream_next_token(s
);
1139 } else if (!tok
|| tok
->type
!= ',') {
1141 isl_stream_push_token(s
, tok
);
1145 isl_token_free(tok
);
1151 /* Read a tuple space from "s" derived from the parameter space "space".
1152 * Call "read_el" on each element in the tuples.
1154 static __isl_give isl_space
*read_tuple_space(struct isl_stream
*s
,
1155 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1156 __isl_give isl_space
*(*read_el
)(struct isl_stream
*s
, struct vars
*v
,
1157 __isl_take isl_space
*space
, int rational
, void *user
),
1160 struct isl_token
*tok
;
1162 isl_space
*res
= NULL
;
1164 tok
= isl_stream_next_token(s
);
1167 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1168 name
= strdup(tok
->u
.s
);
1169 isl_token_free(tok
);
1173 isl_stream_push_token(s
, tok
);
1174 if (isl_stream_eat(s
, '['))
1176 if (next_is_tuple(s
)) {
1178 res
= read_tuple_space(s
, v
, isl_space_copy(space
),
1179 rational
, comma
, read_el
, user
);
1180 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1182 out
= read_tuple_space(s
, v
, isl_space_copy(space
),
1183 rational
, comma
, read_el
, user
);
1184 res
= isl_space_range_product(res
, out
);
1186 res
= read_tuple_list(s
, v
, isl_space_copy(space
),
1187 rational
, comma
, read_el
, user
);
1188 if (isl_stream_eat(s
, ']'))
1192 res
= isl_space_set_tuple_name(res
, isl_dim_set
, name
);
1196 isl_space_free(space
);
1200 isl_space_free(res
);
1201 isl_space_free(space
);
1205 /* Construct an isl_pw_aff defined on a space with v->n variables
1206 * that is equal to the last of those variables.
1208 static __isl_give isl_pw_aff
*identity_tuple_el(struct vars
*v
)
1213 space
= isl_space_set_alloc(v
->ctx
, 0, v
->n
);
1214 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1215 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
- 1, 1);
1216 return isl_pw_aff_from_aff(aff
);
1219 /* This function is called for each element in a tuple inside read_tuple.
1220 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1221 * over a space containing all variables in "v" defined so far.
1222 * The isl_pw_aff expresses the new variable in terms of earlier variables
1223 * if a definition is provided. Otherwise, it is represented as being
1225 * Add the isl_pw_aff to *list.
1226 * If the new variable was named, then adjust "space" accordingly and
1227 * return the updated space.
1229 static __isl_give isl_space
*read_tuple_pw_aff_el(struct isl_stream
*s
,
1230 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1232 isl_pw_aff_list
**list
= (isl_pw_aff_list
**) user
;
1234 struct isl_token
*tok
;
1237 tok
= next_token(s
);
1239 isl_stream_error(s
, NULL
, "unexpected EOF");
1240 return isl_space_free(space
);
1243 if (tok
->type
== ISL_TOKEN_IDENT
) {
1245 int p
= vars_pos(v
, tok
->u
.s
, -1);
1251 if (tok
->type
== '*') {
1252 if (vars_add_anon(v
) < 0)
1254 isl_token_free(tok
);
1255 pa
= identity_tuple_el(v
);
1256 } else if (new_name
) {
1257 int pos
= isl_space_dim(space
, isl_dim_out
) - 1;
1258 space
= space_set_dim_name(space
, pos
, v
->v
->name
);
1259 isl_token_free(tok
);
1260 if (isl_stream_eat_if_available(s
, '='))
1261 pa
= read_tuple_var_def(s
, v
, rational
);
1263 pa
= identity_tuple_el(v
);
1265 isl_stream_push_token(s
, tok
);
1267 if (vars_add_anon(v
) < 0)
1269 pa
= read_tuple_var_def(s
, v
, rational
);
1272 *list
= isl_pw_aff_list_add(*list
, pa
);
1274 return isl_space_free(space
);
1278 isl_token_free(tok
);
1279 return isl_space_free(space
);
1282 /* Read a tuple and represent it as an isl_multi_pw_aff.
1283 * The range space of the isl_multi_pw_aff is the space of the tuple.
1284 * The domain space is an anonymous space
1285 * with a dimension for each variable in the set of variables in "v",
1286 * including the variables in the range.
1287 * If a given dimension is not defined in terms of earlier dimensions in
1288 * the input, then the corresponding isl_pw_aff is set equal to one time
1289 * the variable corresponding to the dimension being defined.
1291 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1292 * Each element in this list is defined over a space representing
1293 * the variables defined so far. We need to adjust the earlier
1294 * elements to have as many variables in the domain as the final
1295 * element in the list.
1297 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1298 struct vars
*v
, int rational
, int comma
)
1302 isl_pw_aff_list
*list
;
1304 space
= isl_space_params_alloc(v
->ctx
, 0);
1305 list
= isl_pw_aff_list_alloc(s
->ctx
, 0);
1306 space
= read_tuple_space(s
, v
, space
, rational
, comma
,
1307 &read_tuple_pw_aff_el
, &list
);
1308 n
= isl_space_dim(space
, isl_dim_set
);
1309 for (i
= 0; i
+ 1 < n
; ++i
) {
1312 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
1313 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, n
- (i
+ 1));
1314 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
1317 space
= isl_space_from_range(space
);
1318 space
= isl_space_add_dims(space
, isl_dim_in
, v
->n
);
1319 return isl_multi_pw_aff_from_pw_aff_list(space
, list
);
1322 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1323 * We first create the appropriate space in "map" based on the range
1324 * space of this isl_multi_pw_aff. Then, we add equalities based
1325 * on the affine expressions. These live in an anonymous space,
1326 * however, so we first need to reset the space to that of "map".
1328 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1329 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1334 isl_space
*space
= NULL
;
1338 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1339 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1340 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1344 if (type
== isl_dim_param
) {
1345 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1346 isl_space_is_wrapping(space
)) {
1347 isl_die(ctx
, isl_error_invalid
,
1348 "parameter tuples cannot be named or nested",
1351 map
= isl_map_add_dims(map
, type
, n
);
1352 for (i
= 0; i
< n
; ++i
) {
1354 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1355 isl_die(ctx
, isl_error_invalid
,
1356 "parameters must be named",
1358 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1359 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1361 } else if (type
== isl_dim_in
) {
1364 set
= isl_set_universe(isl_space_copy(space
));
1366 set
= isl_set_set_rational(set
);
1367 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1368 map
= isl_map_from_domain(set
);
1372 set
= isl_set_universe(isl_space_copy(space
));
1374 set
= isl_set_set_rational(set
);
1375 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1378 for (i
= 0; i
< n
; ++i
) {
1385 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1386 space
= isl_pw_aff_get_domain_space(pa
);
1387 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1388 aff
= isl_aff_add_coefficient_si(aff
,
1389 isl_dim_in
, v
->n
- n
+ i
, -1);
1390 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1392 pa
= isl_pw_aff_set_rational(pa
);
1393 set
= isl_pw_aff_zero_set(pa
);
1394 map_i
= isl_map_from_range(set
);
1395 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1396 map
= isl_map_intersect(map
, map_i
);
1399 isl_space_free(space
);
1400 isl_multi_pw_aff_free(tuple
);
1403 isl_space_free(space
);
1404 isl_multi_pw_aff_free(tuple
);
1409 /* Read a tuple from "s" and add it to "map".
1410 * The tuple is initially represented as an isl_multi_pw_aff and
1411 * then added to "map".
1413 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1414 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1415 int rational
, int comma
)
1417 isl_multi_pw_aff
*tuple
;
1419 tuple
= read_tuple(s
, v
, rational
, comma
);
1421 return isl_map_free(map
);
1423 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1426 static __isl_give isl_set
*construct_constraints(
1427 __isl_take isl_set
*set
, int type
,
1428 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1433 left
= isl_pw_aff_list_copy(left
);
1434 right
= isl_pw_aff_list_copy(right
);
1436 left
= isl_pw_aff_list_set_rational(left
);
1437 right
= isl_pw_aff_list_set_rational(right
);
1439 if (type
== ISL_TOKEN_LE
)
1440 cond
= isl_pw_aff_list_le_set(left
, right
);
1441 else if (type
== ISL_TOKEN_GE
)
1442 cond
= isl_pw_aff_list_ge_set(left
, right
);
1443 else if (type
== ISL_TOKEN_LT
)
1444 cond
= isl_pw_aff_list_lt_set(left
, right
);
1445 else if (type
== ISL_TOKEN_GT
)
1446 cond
= isl_pw_aff_list_gt_set(left
, right
);
1447 else if (type
== ISL_TOKEN_NE
)
1448 cond
= isl_pw_aff_list_ne_set(left
, right
);
1450 cond
= isl_pw_aff_list_eq_set(left
, right
);
1452 return isl_set_intersect(set
, cond
);
1455 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1456 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1458 struct isl_token
*tok
= NULL
;
1459 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1462 set
= isl_map_wrap(map
);
1463 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1466 tok
= isl_stream_next_token(s
);
1467 if (!is_comparator(tok
)) {
1468 isl_stream_error(s
, tok
, "missing operator");
1470 isl_stream_push_token(s
, tok
);
1475 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1479 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1481 isl_token_free(tok
);
1482 isl_pw_aff_list_free(list1
);
1485 tok
= isl_stream_next_token(s
);
1486 if (!is_comparator(tok
)) {
1488 isl_stream_push_token(s
, tok
);
1492 isl_pw_aff_list_free(list1
);
1494 return isl_set_unwrap(set
);
1497 isl_token_free(tok
);
1498 isl_pw_aff_list_free(list1
);
1499 isl_pw_aff_list_free(list2
);
1504 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1505 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1508 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1510 map
= isl_map_from_domain(isl_map_wrap(map
));
1511 map
= read_defined_var_list(s
, v
, map
, rational
);
1513 if (isl_stream_eat(s
, ':'))
1516 map
= read_formula(s
, v
, map
, rational
);
1517 map
= isl_set_unwrap(isl_map_domain(map
));
1519 vars_drop(v
, v
->n
- n
);
1520 if (seen_paren
&& isl_stream_eat(s
, ')'))
1529 /* Parse an expression between parentheses and push the result
1530 * back on the stream.
1532 * The parsed expression may be either an affine expression
1533 * or a condition. The first type is pushed onto the stream
1534 * as an isl_pw_aff, while the second is pushed as an isl_map.
1536 * If the initial token indicates the start of a condition,
1537 * we parse it as such.
1538 * Otherwise, we first parse an affine expression and push
1539 * that onto the stream. If the affine expression covers the
1540 * entire expression between parentheses, we return.
1541 * Otherwise, we assume that the affine expression is the
1542 * start of a condition and continue parsing.
1544 static int resolve_paren_expr(struct isl_stream
*s
,
1545 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1547 struct isl_token
*tok
, *tok2
;
1551 tok
= isl_stream_next_token(s
);
1552 if (!tok
|| tok
->type
!= '(')
1555 if (isl_stream_next_token_is(s
, '('))
1556 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1559 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1560 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1561 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1562 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1563 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1564 map
= read_formula(s
, v
, map
, rational
);
1565 if (isl_stream_eat(s
, ')'))
1567 tok
->type
= ISL_TOKEN_MAP
;
1569 isl_stream_push_token(s
, tok
);
1573 tok2
= isl_stream_next_token(s
);
1578 isl_stream_push_token(s
, tok2
);
1580 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1584 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1587 tok2
->type
= ISL_TOKEN_AFF
;
1588 tok2
->u
.pwaff
= pwaff
;
1590 if (isl_stream_eat_if_available(s
, ')')) {
1591 isl_stream_push_token(s
, tok2
);
1592 isl_token_free(tok
);
1597 isl_stream_push_token(s
, tok2
);
1599 map
= read_formula(s
, v
, map
, rational
);
1600 if (isl_stream_eat(s
, ')'))
1603 tok
->type
= ISL_TOKEN_MAP
;
1605 isl_stream_push_token(s
, tok
);
1609 isl_pw_aff_free(pwaff
);
1611 isl_token_free(tok
);
1616 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1617 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1619 if (isl_stream_next_token_is(s
, '('))
1620 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1623 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1624 struct isl_token
*tok
;
1625 tok
= isl_stream_next_token(s
);
1629 map
= isl_map_copy(tok
->u
.map
);
1630 isl_token_free(tok
);
1634 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1635 return read_exists(s
, v
, map
, rational
);
1637 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1640 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1641 isl_space
*dim
= isl_map_get_space(map
);
1643 return isl_map_empty(dim
);
1646 return add_constraint(s
, v
, map
, rational
);
1652 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1653 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1658 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1659 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1661 res
= isl_map_subtract(isl_map_copy(map
), res
);
1663 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1666 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1667 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1669 res
= isl_map_subtract(res
, res_i
);
1671 res
= isl_map_intersect(res
, res_i
);
1678 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1679 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1683 if (isl_stream_next_token_is(s
, '}')) {
1684 isl_space
*dim
= isl_map_get_space(map
);
1686 return isl_map_universe(dim
);
1689 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1690 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1693 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1694 res
= isl_map_union(res
, res_i
);
1701 /* Read a first order formula from "s", add the corresponding
1702 * constraints to "map" and return the result.
1704 * In particular, read a formula of the form
1712 * where a and b are disjunctions.
1714 * In the first case, map is replaced by
1716 * map \cap { [..] : a }
1718 * In the second case, it is replaced by
1720 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1722 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
1723 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1727 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1729 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1732 res
= isl_map_subtract(isl_map_copy(map
), res
);
1733 res2
= read_disjuncts(s
, v
, map
, rational
);
1734 res
= isl_map_union(res
, res2
);
1741 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1743 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1744 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1745 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1746 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1748 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1749 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1750 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1752 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1753 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1754 isl_basic_map_dim(bmap
, isl_dim_in
) +
1755 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1756 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1758 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1764 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1765 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1768 struct isl_token
*tok
;
1778 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1779 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1781 tok
= isl_stream_next_token(s
);
1782 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1783 isl_stream_error(s
, tok
, "expecting coefficient");
1785 isl_stream_push_token(s
, tok
);
1788 if (!tok
->on_new_line
) {
1789 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1790 isl_stream_push_token(s
, tok
);
1794 type
= isl_int_get_si(tok
->u
.v
);
1795 isl_token_free(tok
);
1797 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1799 k
= isl_basic_map_alloc_equality(bmap
);
1802 k
= isl_basic_map_alloc_inequality(bmap
);
1808 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1810 tok
= isl_stream_next_token(s
);
1811 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1812 isl_stream_error(s
, tok
, "expecting coefficient");
1814 isl_stream_push_token(s
, tok
);
1817 if (tok
->on_new_line
) {
1818 isl_stream_error(s
, tok
,
1819 "coefficient should not appear on new line");
1820 isl_stream_push_token(s
, tok
);
1823 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1824 isl_int_set(c
[pos
], tok
->u
.v
);
1825 isl_token_free(tok
);
1830 isl_basic_map_free(bmap
);
1834 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1837 struct isl_token
*tok
;
1838 struct isl_token
*tok2
;
1841 unsigned in
= 0, out
, local
= 0;
1842 struct isl_basic_map
*bmap
= NULL
;
1845 tok
= isl_stream_next_token(s
);
1847 isl_stream_error(s
, NULL
, "unexpected EOF");
1850 tok2
= isl_stream_next_token(s
);
1852 isl_token_free(tok
);
1853 isl_stream_error(s
, NULL
, "unexpected EOF");
1856 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1857 isl_stream_push_token(s
, tok2
);
1858 isl_stream_push_token(s
, tok
);
1859 isl_stream_error(s
, NULL
,
1860 "expecting constraint matrix dimensions");
1863 n_row
= isl_int_get_si(tok
->u
.v
);
1864 n_col
= isl_int_get_si(tok2
->u
.v
);
1865 on_new_line
= tok2
->on_new_line
;
1866 isl_token_free(tok2
);
1867 isl_token_free(tok
);
1868 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1869 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1870 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1871 tok
= isl_stream_next_token_on_same_line(s
);
1873 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1874 isl_stream_error(s
, tok
,
1875 "expecting number of output dimensions");
1876 isl_stream_push_token(s
, tok
);
1879 out
= isl_int_get_si(tok
->u
.v
);
1880 isl_token_free(tok
);
1882 tok
= isl_stream_next_token_on_same_line(s
);
1883 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1884 isl_stream_error(s
, tok
,
1885 "expecting number of input dimensions");
1887 isl_stream_push_token(s
, tok
);
1890 in
= isl_int_get_si(tok
->u
.v
);
1891 isl_token_free(tok
);
1893 tok
= isl_stream_next_token_on_same_line(s
);
1894 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1895 isl_stream_error(s
, tok
,
1896 "expecting number of existentials");
1898 isl_stream_push_token(s
, tok
);
1901 local
= isl_int_get_si(tok
->u
.v
);
1902 isl_token_free(tok
);
1904 tok
= isl_stream_next_token_on_same_line(s
);
1905 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1906 isl_stream_error(s
, tok
,
1907 "expecting number of parameters");
1909 isl_stream_push_token(s
, tok
);
1912 nparam
= isl_int_get_si(tok
->u
.v
);
1913 isl_token_free(tok
);
1914 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1915 isl_stream_error(s
, NULL
,
1916 "dimensions don't match");
1920 out
= n_col
- 2 - nparam
;
1921 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1925 for (i
= 0; i
< local
; ++i
) {
1926 int k
= isl_basic_map_alloc_div(bmap
);
1929 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1932 for (i
= 0; i
< n_row
; ++i
)
1933 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1935 tok
= isl_stream_next_token_on_same_line(s
);
1937 isl_stream_error(s
, tok
, "unexpected extra token on line");
1938 isl_stream_push_token(s
, tok
);
1942 bmap
= isl_basic_map_simplify(bmap
);
1943 bmap
= isl_basic_map_finalize(bmap
);
1946 isl_basic_map_free(bmap
);
1950 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1952 struct isl_token
*tok
;
1953 struct isl_token
*tok2
;
1955 struct isl_map
*map
;
1957 tok
= isl_stream_next_token(s
);
1959 isl_stream_error(s
, NULL
, "unexpected EOF");
1962 tok2
= isl_stream_next_token_on_same_line(s
);
1963 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1964 isl_stream_push_token(s
, tok2
);
1965 isl_stream_push_token(s
, tok
);
1966 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1969 isl_stream_error(s
, tok2
, "unexpected token");
1970 isl_stream_push_token(s
, tok2
);
1971 isl_stream_push_token(s
, tok
);
1974 n
= isl_int_get_si(tok
->u
.v
);
1975 isl_token_free(tok
);
1977 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1979 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1981 for (i
= 1; map
&& i
< n
; ++i
)
1982 map
= isl_map_union(map
,
1983 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1988 static int optional_power(struct isl_stream
*s
)
1991 struct isl_token
*tok
;
1993 tok
= isl_stream_next_token(s
);
1996 if (tok
->type
!= '^') {
1997 isl_stream_push_token(s
, tok
);
2000 isl_token_free(tok
);
2001 tok
= isl_stream_next_token(s
);
2002 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2003 isl_stream_error(s
, tok
, "expecting exponent");
2005 isl_stream_push_token(s
, tok
);
2008 pow
= isl_int_get_si(tok
->u
.v
);
2009 isl_token_free(tok
);
2013 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
2014 __isl_keep isl_map
*map
, struct vars
*v
);
2016 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
2017 __isl_keep isl_map
*map
, struct vars
*v
)
2019 isl_pw_qpolynomial
*pwqp
;
2020 struct isl_token
*tok
;
2022 tok
= next_token(s
);
2024 isl_stream_error(s
, NULL
, "unexpected EOF");
2027 if (tok
->type
== '(') {
2030 isl_token_free(tok
);
2031 pwqp
= read_term(s
, map
, v
);
2034 if (isl_stream_eat(s
, ')'))
2036 pow
= optional_power(s
);
2037 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2038 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
2039 struct isl_token
*tok2
;
2040 isl_qpolynomial
*qp
;
2042 tok2
= isl_stream_next_token(s
);
2043 if (tok2
&& tok2
->type
== '/') {
2044 isl_token_free(tok2
);
2045 tok2
= next_token(s
);
2046 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2047 isl_stream_error(s
, tok2
, "expected denominator");
2048 isl_token_free(tok
);
2049 isl_token_free(tok2
);
2052 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
2053 tok
->u
.v
, tok2
->u
.v
);
2054 isl_token_free(tok2
);
2056 isl_stream_push_token(s
, tok2
);
2057 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
2060 isl_token_free(tok
);
2061 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2062 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2063 isl_qpolynomial
*qp
;
2064 isl_token_free(tok
);
2065 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2066 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2067 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2068 isl_qpolynomial
*qp
;
2069 isl_token_free(tok
);
2070 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2071 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2072 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2074 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2076 isl_qpolynomial
*qp
;
2078 isl_token_free(tok
);
2082 vars_drop(v
, v
->n
- n
);
2083 isl_stream_error(s
, tok
, "unknown identifier");
2084 isl_token_free(tok
);
2087 isl_token_free(tok
);
2088 pow
= optional_power(s
);
2089 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2090 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2091 } else if (is_start_of_div(tok
)) {
2095 isl_stream_push_token(s
, tok
);
2096 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2097 pow
= optional_power(s
);
2098 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2099 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2100 } else if (tok
->type
== '-') {
2101 isl_token_free(tok
);
2102 pwqp
= read_factor(s
, map
, v
);
2103 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2105 isl_stream_error(s
, tok
, "unexpected isl_token");
2106 isl_stream_push_token(s
, tok
);
2110 if (isl_stream_eat_if_available(s
, '*') ||
2111 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2112 isl_pw_qpolynomial
*pwqp2
;
2114 pwqp2
= read_factor(s
, map
, v
);
2115 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2120 isl_pw_qpolynomial_free(pwqp
);
2124 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
2125 __isl_keep isl_map
*map
, struct vars
*v
)
2127 struct isl_token
*tok
;
2128 isl_pw_qpolynomial
*pwqp
;
2130 pwqp
= read_factor(s
, map
, v
);
2133 tok
= next_token(s
);
2137 if (tok
->type
== '+') {
2138 isl_pw_qpolynomial
*pwqp2
;
2140 isl_token_free(tok
);
2141 pwqp2
= read_factor(s
, map
, v
);
2142 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2143 } else if (tok
->type
== '-') {
2144 isl_pw_qpolynomial
*pwqp2
;
2146 isl_token_free(tok
);
2147 pwqp2
= read_factor(s
, map
, v
);
2148 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2149 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2150 isl_int_is_neg(tok
->u
.v
)) {
2151 isl_pw_qpolynomial
*pwqp2
;
2153 isl_stream_push_token(s
, tok
);
2154 pwqp2
= read_factor(s
, map
, v
);
2155 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2157 isl_stream_push_token(s
, tok
);
2165 static __isl_give isl_map
*read_optional_formula(struct isl_stream
*s
,
2166 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2168 struct isl_token
*tok
;
2170 tok
= isl_stream_next_token(s
);
2172 isl_stream_error(s
, NULL
, "unexpected EOF");
2175 if (tok
->type
== ':' ||
2176 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2177 isl_token_free(tok
);
2178 map
= read_formula(s
, v
, map
, rational
);
2180 isl_stream_push_token(s
, tok
);
2188 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
2189 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2191 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2192 isl_pw_qpolynomial
*pwqp
;
2193 struct isl_set
*set
;
2195 pwqp
= read_term(s
, map
, v
);
2196 map
= read_optional_formula(s
, map
, v
, 0);
2197 set
= isl_map_range(map
);
2199 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2201 vars_drop(v
, v
->n
- n
);
2207 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
2208 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2210 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2211 isl_pw_qpolynomial
*pwqp
;
2212 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2214 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2215 return obj_read_poly(s
, set
, v
, n
);
2217 if (isl_stream_eat(s
, '('))
2220 pwqp
= read_term(s
, set
, v
);
2221 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2223 while (isl_stream_eat_if_available(s
, ',')) {
2224 isl_pw_qpolynomial_fold
*pwf_i
;
2225 pwqp
= read_term(s
, set
, v
);
2226 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2228 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2231 if (isl_stream_eat(s
, ')'))
2234 set
= read_optional_formula(s
, set
, v
, 0);
2235 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2237 vars_drop(v
, v
->n
- n
);
2243 isl_pw_qpolynomial_fold_free(pwf
);
2244 obj
.type
= isl_obj_none
;
2248 static int is_rational(struct isl_stream
*s
)
2250 struct isl_token
*tok
;
2252 tok
= isl_stream_next_token(s
);
2255 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2256 isl_token_free(tok
);
2257 isl_stream_eat(s
, ':');
2261 isl_stream_push_token(s
, tok
);
2266 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2267 __isl_take isl_map
*map
, struct vars
*v
)
2269 struct isl_token
*tok
;
2270 struct isl_obj obj
= { isl_obj_set
, NULL
};
2274 rational
= is_rational(s
);
2276 map
= isl_map_set_rational(map
);
2278 if (isl_stream_next_token_is(s
, ':')) {
2279 obj
.type
= isl_obj_set
;
2280 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2284 if (!next_is_tuple(s
))
2285 return obj_read_poly_or_fold(s
, map
, v
, n
);
2287 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2290 tok
= isl_stream_next_token(s
);
2293 if (tok
->type
== ISL_TOKEN_TO
) {
2294 obj
.type
= isl_obj_map
;
2295 isl_token_free(tok
);
2296 if (!next_is_tuple(s
)) {
2297 isl_set
*set
= isl_map_domain(map
);
2298 return obj_read_poly_or_fold(s
, set
, v
, n
);
2300 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2304 map
= isl_map_domain(map
);
2305 isl_stream_push_token(s
, tok
);
2308 map
= read_optional_formula(s
, map
, v
, rational
);
2310 vars_drop(v
, v
->n
- n
);
2316 obj
.type
= isl_obj_none
;
2320 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2322 if (obj
.type
== isl_obj_map
) {
2323 obj
.v
= isl_union_map_from_map(obj
.v
);
2324 obj
.type
= isl_obj_union_map
;
2325 } else if (obj
.type
== isl_obj_set
) {
2326 obj
.v
= isl_union_set_from_set(obj
.v
);
2327 obj
.type
= isl_obj_union_set
;
2328 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2329 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2330 obj
.type
= isl_obj_union_pw_qpolynomial
;
2331 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2332 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2333 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2335 isl_assert(ctx
, 0, goto error
);
2338 obj
.type
->free(obj
.v
);
2339 obj
.type
= isl_obj_none
;
2343 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2344 struct isl_obj obj1
, struct isl_obj obj2
)
2346 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2347 obj1
= to_union(ctx
, obj1
);
2348 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2349 obj2
= to_union(ctx
, obj2
);
2350 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2351 obj1
= to_union(ctx
, obj1
);
2352 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2353 obj2
= to_union(ctx
, obj2
);
2354 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2355 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2356 obj1
= to_union(ctx
, obj1
);
2357 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2358 obj2
.type
== isl_obj_pw_qpolynomial
)
2359 obj2
= to_union(ctx
, obj2
);
2360 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2361 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2362 obj1
= to_union(ctx
, obj1
);
2363 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2364 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2365 obj2
= to_union(ctx
, obj2
);
2366 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2367 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2368 obj1
= to_union(ctx
, obj1
);
2369 obj2
= to_union(ctx
, obj2
);
2371 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2372 obj1
= to_union(ctx
, obj1
);
2373 obj2
= to_union(ctx
, obj2
);
2375 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2376 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2377 obj1
= to_union(ctx
, obj1
);
2378 obj2
= to_union(ctx
, obj2
);
2380 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2381 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2382 obj1
= to_union(ctx
, obj1
);
2383 obj2
= to_union(ctx
, obj2
);
2385 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2388 obj1
.type
->free(obj1
.v
);
2389 obj2
.type
->free(obj2
.v
);
2390 obj1
.type
= isl_obj_none
;
2395 static struct isl_obj
obj_read(struct isl_stream
*s
)
2397 isl_map
*map
= NULL
;
2398 struct isl_token
*tok
;
2399 struct vars
*v
= NULL
;
2400 struct isl_obj obj
= { isl_obj_set
, NULL
};
2402 tok
= next_token(s
);
2404 isl_stream_error(s
, NULL
, "unexpected EOF");
2407 if (tok
->type
== ISL_TOKEN_VALUE
) {
2408 struct isl_token
*tok2
;
2409 struct isl_map
*map
;
2411 tok2
= isl_stream_next_token(s
);
2412 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2413 isl_int_is_neg(tok2
->u
.v
)) {
2415 isl_stream_push_token(s
, tok2
);
2416 obj
.type
= isl_obj_val
;
2417 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2418 isl_token_free(tok
);
2421 isl_stream_push_token(s
, tok2
);
2422 isl_stream_push_token(s
, tok
);
2423 map
= map_read_polylib(s
);
2426 if (isl_map_may_be_set(map
))
2427 obj
.v
= isl_map_range(map
);
2429 obj
.type
= isl_obj_map
;
2434 v
= vars_new(s
->ctx
);
2436 isl_stream_push_token(s
, tok
);
2439 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2440 if (tok
->type
== '[') {
2441 isl_stream_push_token(s
, tok
);
2442 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2445 tok
= isl_stream_next_token(s
);
2446 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2447 isl_stream_error(s
, tok
, "expecting '->'");
2449 isl_stream_push_token(s
, tok
);
2452 isl_token_free(tok
);
2453 tok
= isl_stream_next_token(s
);
2455 if (!tok
|| tok
->type
!= '{') {
2456 isl_stream_error(s
, tok
, "expecting '{'");
2458 isl_stream_push_token(s
, tok
);
2461 isl_token_free(tok
);
2463 tok
= isl_stream_next_token(s
);
2466 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2467 isl_token_free(tok
);
2468 if (isl_stream_eat(s
, '='))
2470 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2473 } else if (tok
->type
== '}') {
2474 obj
.type
= isl_obj_union_set
;
2475 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2476 isl_token_free(tok
);
2479 isl_stream_push_token(s
, tok
);
2484 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2485 if (o
.type
== isl_obj_none
|| !o
.v
)
2490 obj
= obj_add(s
->ctx
, obj
, o
);
2491 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2494 tok
= isl_stream_next_token(s
);
2495 if (!tok
|| tok
->type
!= ';')
2497 isl_token_free(tok
);
2498 if (isl_stream_next_token_is(s
, '}')) {
2499 tok
= isl_stream_next_token(s
);
2504 if (tok
&& tok
->type
== '}') {
2505 isl_token_free(tok
);
2507 isl_stream_error(s
, tok
, "unexpected isl_token");
2509 isl_token_free(tok
);
2519 obj
.type
->free(obj
.v
);
2526 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2531 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2537 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2538 obj
.type
== isl_obj_set
, goto error
);
2540 if (obj
.type
== isl_obj_set
)
2541 obj
.v
= isl_map_from_range(obj
.v
);
2545 obj
.type
->free(obj
.v
);
2549 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2555 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2556 obj
.v
= isl_map_range(obj
.v
);
2557 obj
.type
= isl_obj_set
;
2559 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2564 obj
.type
->free(obj
.v
);
2568 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2573 if (obj
.type
== isl_obj_map
) {
2574 obj
.type
= isl_obj_union_map
;
2575 obj
.v
= isl_union_map_from_map(obj
.v
);
2577 if (obj
.type
== isl_obj_set
) {
2578 obj
.type
= isl_obj_union_set
;
2579 obj
.v
= isl_union_set_from_set(obj
.v
);
2581 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2582 isl_union_set_is_empty(obj
.v
))
2583 obj
.type
= isl_obj_union_map
;
2584 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2585 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2589 obj
.type
->free(obj
.v
);
2593 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2598 if (obj
.type
== isl_obj_set
) {
2599 obj
.type
= isl_obj_union_set
;
2600 obj
.v
= isl_union_set_from_set(obj
.v
);
2603 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2607 obj
.type
->free(obj
.v
);
2611 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2614 struct isl_map
*map
;
2615 struct isl_basic_map
*bmap
;
2618 if (obj
.v
&& (obj
.type
!= isl_obj_map
&& obj
.type
!= isl_obj_set
))
2619 isl_die(s
->ctx
, isl_error_invalid
, "not a (basic) set or map",
2626 isl_die(s
->ctx
, isl_error_invalid
,
2627 "set or map description involves "
2628 "more than one disjunct", goto error
);
2631 bmap
= isl_basic_map_empty_like_map(map
);
2633 bmap
= isl_basic_map_copy(map
->p
[0]);
2639 obj
.type
->free(obj
.v
);
2643 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2645 isl_basic_map
*bmap
;
2646 bmap
= basic_map_read(s
);
2649 if (!isl_basic_map_may_be_set(bmap
))
2650 isl_die(s
->ctx
, isl_error_invalid
,
2651 "input is not a set", goto error
);
2652 return isl_basic_map_range(bmap
);
2654 isl_basic_map_free(bmap
);
2658 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2661 struct isl_basic_map
*bmap
;
2662 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2665 bmap
= basic_map_read(s
);
2670 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2673 isl_basic_set
*bset
;
2674 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2677 bset
= basic_set_read(s
);
2682 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2685 struct isl_basic_map
*bmap
;
2686 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2689 bmap
= basic_map_read(s
);
2694 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2697 isl_basic_set
*bset
;
2698 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2701 bset
= basic_set_read(s
);
2706 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2709 struct isl_map
*map
;
2710 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2713 map
= isl_stream_read_map(s
);
2718 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2721 struct isl_map
*map
;
2722 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2725 map
= isl_stream_read_map(s
);
2730 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2734 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2737 set
= isl_stream_read_set(s
);
2742 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2746 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2749 set
= isl_stream_read_set(s
);
2754 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2757 isl_union_map
*umap
;
2758 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2761 umap
= isl_stream_read_union_map(s
);
2766 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2769 isl_union_map
*umap
;
2770 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2773 umap
= isl_stream_read_union_map(s
);
2778 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2781 isl_union_set
*uset
;
2782 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2785 uset
= isl_stream_read_union_set(s
);
2790 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2793 isl_union_set
*uset
;
2794 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2797 uset
= isl_stream_read_union_set(s
);
2802 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2804 struct isl_vec
*vec
= NULL
;
2805 struct isl_token
*tok
;
2809 tok
= isl_stream_next_token(s
);
2810 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2811 isl_stream_error(s
, tok
, "expecting vector length");
2815 size
= isl_int_get_si(tok
->u
.v
);
2816 isl_token_free(tok
);
2818 vec
= isl_vec_alloc(s
->ctx
, size
);
2820 for (j
= 0; j
< size
; ++j
) {
2821 tok
= isl_stream_next_token(s
);
2822 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2823 isl_stream_error(s
, tok
, "expecting constant value");
2826 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2827 isl_token_free(tok
);
2832 isl_token_free(tok
);
2837 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2839 return isl_vec_read_polylib(s
);
2842 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2845 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2853 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2854 struct isl_stream
*s
)
2860 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2865 obj
.type
->free(obj
.v
);
2869 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2872 isl_pw_qpolynomial
*pwqp
;
2873 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2876 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2881 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2884 isl_pw_qpolynomial
*pwqp
;
2885 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2888 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2893 /* Is the next token an identifer not in "v"?
2895 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2899 struct isl_token
*tok
;
2901 tok
= isl_stream_next_token(s
);
2904 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2905 isl_stream_push_token(s
, tok
);
2907 vars_drop(v
, v
->n
- n
);
2912 /* First read the domain of the affine expression, which may be
2913 * a parameter space or a set.
2914 * The tricky part is that we don't know if the domain is a set or not,
2915 * so when we are trying to read the domain, we may actually be reading
2916 * the affine expression itself (defined on a parameter domains)
2917 * If the tuple we are reading is named, we assume it's the domain.
2918 * Also, if inside the tuple, the first thing we find is a nested tuple
2919 * or a new identifier, we again assume it's the domain.
2920 * Otherwise, we assume we are reading an affine expression.
2922 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2923 __isl_take isl_set
*dom
, struct vars
*v
)
2925 struct isl_token
*tok
;
2927 tok
= isl_stream_next_token(s
);
2928 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2929 isl_stream_push_token(s
, tok
);
2930 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2932 if (!tok
|| tok
->type
!= '[') {
2933 isl_stream_error(s
, tok
, "expecting '['");
2936 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2937 isl_stream_push_token(s
, tok
);
2938 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2940 isl_stream_push_token(s
, tok
);
2945 isl_stream_push_token(s
, tok
);
2950 /* Read an affine expression from "s".
2952 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2957 ma
= isl_stream_read_multi_aff(s
);
2960 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2961 isl_die(s
->ctx
, isl_error_invalid
,
2962 "expecting single affine expression",
2965 aff
= isl_multi_aff_get_aff(ma
, 0);
2966 isl_multi_aff_free(ma
);
2969 isl_multi_aff_free(ma
);
2973 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2975 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2976 __isl_take isl_set
*dom
, struct vars
*v
)
2978 isl_pw_aff
*pwaff
= NULL
;
2980 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2983 if (isl_stream_eat(s
, '['))
2986 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2988 if (isl_stream_eat(s
, ']'))
2991 dom
= read_optional_formula(s
, dom
, v
, 0);
2992 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2997 isl_pw_aff_free(pwaff
);
3001 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
3004 isl_set
*dom
= NULL
;
3006 isl_pw_aff
*pa
= NULL
;
3009 v
= vars_new(s
->ctx
);
3013 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3014 if (next_is_tuple(s
)) {
3015 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3016 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3019 if (isl_stream_eat(s
, '{'))
3023 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3024 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3025 vars_drop(v
, v
->n
- n
);
3027 while (isl_stream_eat_if_available(s
, ';')) {
3031 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3032 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3033 vars_drop(v
, v
->n
- n
);
3035 pa
= isl_pw_aff_union_add(pa
, pa_i
);
3038 if (isl_stream_eat(s
, '}'))
3047 isl_pw_aff_free(pa
);
3051 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3054 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3057 aff
= isl_stream_read_aff(s
);
3062 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3065 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3068 pa
= isl_stream_read_pw_aff(s
);
3073 /* Read an isl_pw_multi_aff from "s".
3074 * We currently read a generic object and if it turns out to be a set or
3075 * a map, we convert that to an isl_pw_multi_aff.
3076 * It would be more efficient if we were to construct the isl_pw_multi_aff
3079 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
3087 if (obj
.type
== isl_obj_map
)
3088 return isl_pw_multi_aff_from_map(obj
.v
);
3089 if (obj
.type
== isl_obj_set
)
3090 return isl_pw_multi_aff_from_set(obj
.v
);
3092 obj
.type
->free(obj
.v
);
3093 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3097 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3100 isl_pw_multi_aff
*pma
;
3101 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3104 pma
= isl_stream_read_pw_multi_aff(s
);
3109 /* Read an isl_union_pw_multi_aff from "s".
3110 * We currently read a generic object and if it turns out to be a set or
3111 * a map, we convert that to an isl_union_pw_multi_aff.
3112 * It would be more efficient if we were to construct
3113 * the isl_union_pw_multi_aff directly.
3115 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3116 struct isl_stream
*s
)
3124 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3125 obj
= to_union(s
->ctx
, obj
);
3126 if (obj
.type
== isl_obj_union_map
)
3127 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3128 if (obj
.type
== isl_obj_union_set
)
3129 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3131 obj
.type
->free(obj
.v
);
3132 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3136 /* Read an isl_union_pw_multi_aff from "str".
3138 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3139 isl_ctx
*ctx
, const char *str
)
3141 isl_union_pw_multi_aff
*upma
;
3142 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3145 upma
= isl_stream_read_union_pw_multi_aff(s
);
3150 /* Assuming "pa" represents a single affine expression defined on a universe
3151 * domain, extract this affine expression.
3153 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3160 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3161 "expecting single affine expression",
3163 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3164 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3165 "expecting universe domain",
3168 aff
= isl_aff_copy(pa
->p
[0].aff
);
3169 isl_pw_aff_free(pa
);
3172 isl_pw_aff_free(pa
);
3176 /* This function is called for each element in a tuple inside
3177 * isl_stream_read_multi_val.
3178 * Read an isl_val from "s" and add it to *list.
3180 static __isl_give isl_space
*read_val_el(struct isl_stream
*s
,
3181 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3183 isl_val_list
**list
= (isl_val_list
**) user
;
3186 val
= isl_stream_read_val(s
);
3187 *list
= isl_val_list_add(*list
, val
);
3189 return isl_space_free(space
);
3194 /* Read an isl_multi_val from "s".
3196 * We first read a tuple space, collecting the element values in a list.
3197 * Then we create an isl_multi_val from the space and the isl_val_list.
3199 __isl_give isl_multi_val
*isl_stream_read_multi_val(struct isl_stream
*s
)
3202 isl_set
*dom
= NULL
;
3204 isl_multi_val
*mv
= NULL
;
3207 v
= vars_new(s
->ctx
);
3211 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3212 if (next_is_tuple(s
)) {
3213 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3214 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3217 if (!isl_set_plain_is_universe(dom
))
3218 isl_die(s
->ctx
, isl_error_invalid
,
3219 "expecting universe parameter domain", goto error
);
3220 if (isl_stream_eat(s
, '{'))
3223 space
= isl_set_get_space(dom
);
3225 list
= isl_val_list_alloc(s
->ctx
, 0);
3226 space
= read_tuple_space(s
, v
, space
, 1, 0, &read_val_el
, &list
);
3227 mv
= isl_multi_val_from_val_list(space
, list
);
3229 if (isl_stream_eat(s
, '}'))
3238 isl_multi_val_free(mv
);
3242 /* Read an isl_multi_val from "str".
3244 __isl_give isl_multi_val
*isl_multi_val_read_from_str(isl_ctx
*ctx
,
3248 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3251 mv
= isl_stream_read_multi_val(s
);
3256 /* Read a multi-affine expression from "s".
3257 * If the multi-affine expression has a domain, then the tuple
3258 * representing this domain cannot involve any affine expressions.
3259 * The tuple representing the actual expressions needs to consist
3260 * of only affine expressions. Moreover, these expressions can
3261 * only depend on parameters and input dimensions and not on other
3262 * output dimensions.
3264 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
3267 isl_set
*dom
= NULL
;
3268 isl_multi_pw_aff
*tuple
= NULL
;
3270 isl_space
*space
, *dom_space
;
3271 isl_multi_aff
*ma
= NULL
;
3273 v
= vars_new(s
->ctx
);
3277 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3278 if (next_is_tuple(s
)) {
3279 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3280 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3283 if (!isl_set_plain_is_universe(dom
))
3284 isl_die(s
->ctx
, isl_error_invalid
,
3285 "expecting universe parameter domain", goto error
);
3286 if (isl_stream_eat(s
, '{'))
3289 tuple
= read_tuple(s
, v
, 0, 0);
3292 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3297 has_expr
= tuple_has_expr(tuple
);
3301 isl_die(s
->ctx
, isl_error_invalid
,
3302 "expecting universe domain", goto error
);
3303 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3304 set
= isl_set_universe(space
);
3305 dom
= isl_set_intersect_params(set
, dom
);
3306 isl_multi_pw_aff_free(tuple
);
3307 tuple
= read_tuple(s
, v
, 0, 0);
3312 if (isl_stream_eat(s
, '}'))
3315 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3316 dim
= isl_set_dim(dom
, isl_dim_all
);
3317 dom_space
= isl_set_get_space(dom
);
3318 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3319 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3320 if (!isl_space_is_params(dom_space
))
3321 space
= isl_space_map_from_domain_and_range(
3322 isl_space_copy(dom_space
), space
);
3323 isl_space_free(dom_space
);
3324 ma
= isl_multi_aff_alloc(space
);
3326 for (i
= 0; i
< n
; ++i
) {
3329 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3330 aff
= aff_from_pw_aff(pa
);
3333 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3335 isl_die(s
->ctx
, isl_error_invalid
,
3336 "not an affine expression", goto error
);
3338 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3339 space
= isl_multi_aff_get_domain_space(ma
);
3340 aff
= isl_aff_reset_domain_space(aff
, space
);
3341 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3344 isl_multi_pw_aff_free(tuple
);
3349 isl_multi_pw_aff_free(tuple
);
3352 isl_multi_aff_free(ma
);
3356 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3359 isl_multi_aff
*maff
;
3360 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3363 maff
= isl_stream_read_multi_aff(s
);
3368 /* Read an isl_multi_pw_aff from "s".
3370 * The input format is similar to that of map, except that any conditions
3371 * on the domains should be specified inside the tuple since each
3372 * piecewise affine expression may have a different domain.
3374 * Since we do not know in advance if the isl_multi_pw_aff lives
3375 * in a set or a map space, we first read the first tuple and check
3376 * if it is followed by a "->". If so, we convert the tuple into
3377 * the domain of the isl_multi_pw_aff and read in the next tuple.
3378 * This tuple (or the first tuple if it was not followed by a "->")
3379 * is then converted into the isl_multi_pw_aff.
3381 * Note that the function read_tuple accepts tuples where some output or
3382 * set dimensions are defined in terms of other output or set dimensions
3383 * since this function is also used to read maps. As a special case,
3384 * read_tuple also accept dimensions that are defined in terms of themselves
3385 * (i.e., that are not defined).
3386 * These cases are not allowed when reading am isl_multi_pw_aff so we check
3387 * that the definition of the output/set dimensions does not involve any
3388 * output/set dimensions.
3389 * We then drop the output dimensions from the domain of the result
3390 * of read_tuple (which is of the form [input, output] -> [output],
3391 * with anonymous domain) and reset the space.
3393 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(struct isl_stream
*s
)
3396 isl_set
*dom
= NULL
;
3397 isl_multi_pw_aff
*tuple
= NULL
;
3399 isl_space
*space
, *dom_space
;
3400 isl_multi_pw_aff
*mpa
= NULL
;
3402 v
= vars_new(s
->ctx
);
3406 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3407 if (next_is_tuple(s
)) {
3408 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3409 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3412 if (isl_stream_eat(s
, '{'))
3415 tuple
= read_tuple(s
, v
, 0, 0);
3418 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3419 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3420 dom
= isl_map_domain(map
);
3421 tuple
= read_tuple(s
, v
, 0, 0);
3426 if (isl_stream_eat(s
, '}'))
3429 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3430 dim
= isl_set_dim(dom
, isl_dim_all
);
3431 dom_space
= isl_set_get_space(dom
);
3432 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3433 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3434 if (!isl_space_is_params(dom_space
))
3435 space
= isl_space_map_from_domain_and_range(
3436 isl_space_copy(dom_space
), space
);
3437 isl_space_free(dom_space
);
3438 mpa
= isl_multi_pw_aff_alloc(space
);
3440 for (i
= 0; i
< n
; ++i
) {
3442 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3445 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3446 isl_pw_aff_free(pa
);
3447 isl_die(s
->ctx
, isl_error_invalid
,
3448 "not an affine expression", goto error
);
3450 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3451 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3452 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3453 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3456 isl_multi_pw_aff_free(tuple
);
3458 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3461 isl_multi_pw_aff_free(tuple
);
3464 isl_multi_pw_aff_free(mpa
);
3468 /* Read an isl_multi_pw_aff from "str".
3470 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3473 isl_multi_pw_aff
*mpa
;
3474 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3477 mpa
= isl_stream_read_multi_pw_aff(s
);
3482 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3483 struct isl_stream
*s
)
3488 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3489 obj
.type
= isl_obj_union_pw_qpolynomial
;
3490 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3493 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3498 obj
.type
->free(obj
.v
);
3502 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3503 isl_ctx
*ctx
, const char *str
)
3505 isl_union_pw_qpolynomial
*upwqp
;
3506 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3509 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);