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(__isl_keep 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(__isl_keep 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 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
248 val
= isl_stream_read_val(s
);
253 static int accept_cst_factor(__isl_keep 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(__isl_keep 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(__isl_keep isl_stream
*s
,
311 __isl_take isl_space
*space
, struct vars
*v
);
312 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
313 __isl_take isl_space
*dim
, struct vars
*v
);
315 static __isl_give isl_pw_aff
*accept_minmax(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep isl_stream
*s
,
679 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
680 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep 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(__isl_keep isl_stream
*s
,
1112 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1113 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1114 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1121 space
= isl_space_set_from_params(space
);
1123 if (isl_stream_next_token_is(s
, ']'))
1127 struct isl_token
*tok
;
1129 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1131 space
= read_el(s
, v
, space
, rational
, user
);
1135 tok
= isl_stream_next_token(s
);
1136 if (!comma
&& tok
&& tok
->type
== ']' &&
1137 isl_stream_next_token_is(s
, '[')) {
1138 isl_token_free(tok
);
1139 tok
= isl_stream_next_token(s
);
1140 } else if (!tok
|| tok
->type
!= ',') {
1142 isl_stream_push_token(s
, tok
);
1146 isl_token_free(tok
);
1152 /* Read a tuple space from "s" derived from the parameter space "space".
1153 * Call "read_el" on each element in the tuples.
1155 static __isl_give isl_space
*read_tuple_space(__isl_keep isl_stream
*s
,
1156 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1157 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1158 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1162 struct isl_token
*tok
;
1164 isl_space
*res
= NULL
;
1166 tok
= isl_stream_next_token(s
);
1169 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1170 name
= strdup(tok
->u
.s
);
1171 isl_token_free(tok
);
1175 isl_stream_push_token(s
, tok
);
1176 if (isl_stream_eat(s
, '['))
1178 if (next_is_tuple(s
)) {
1180 res
= read_tuple_space(s
, v
, isl_space_copy(space
),
1181 rational
, comma
, read_el
, user
);
1182 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1184 out
= read_tuple_space(s
, v
, isl_space_copy(space
),
1185 rational
, comma
, read_el
, user
);
1186 res
= isl_space_range_product(res
, out
);
1188 res
= read_tuple_list(s
, v
, isl_space_copy(space
),
1189 rational
, comma
, read_el
, user
);
1190 if (isl_stream_eat(s
, ']'))
1194 res
= isl_space_set_tuple_name(res
, isl_dim_set
, name
);
1198 isl_space_free(space
);
1202 isl_space_free(res
);
1203 isl_space_free(space
);
1207 /* Construct an isl_pw_aff defined on a space with v->n variables
1208 * that is equal to the last of those variables.
1210 static __isl_give isl_pw_aff
*identity_tuple_el(struct vars
*v
)
1215 space
= isl_space_set_alloc(v
->ctx
, 0, v
->n
);
1216 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1217 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
- 1, 1);
1218 return isl_pw_aff_from_aff(aff
);
1221 /* This function is called for each element in a tuple inside read_tuple.
1222 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1223 * over a space containing all variables in "v" defined so far.
1224 * The isl_pw_aff expresses the new variable in terms of earlier variables
1225 * if a definition is provided. Otherwise, it is represented as being
1227 * Add the isl_pw_aff to *list.
1228 * If the new variable was named, then adjust "space" accordingly and
1229 * return the updated space.
1231 static __isl_give isl_space
*read_tuple_pw_aff_el(__isl_keep isl_stream
*s
,
1232 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1234 isl_pw_aff_list
**list
= (isl_pw_aff_list
**) user
;
1236 struct isl_token
*tok
;
1239 tok
= next_token(s
);
1241 isl_stream_error(s
, NULL
, "unexpected EOF");
1242 return isl_space_free(space
);
1245 if (tok
->type
== ISL_TOKEN_IDENT
) {
1247 int p
= vars_pos(v
, tok
->u
.s
, -1);
1253 if (tok
->type
== '*') {
1254 if (vars_add_anon(v
) < 0)
1256 isl_token_free(tok
);
1257 pa
= identity_tuple_el(v
);
1258 } else if (new_name
) {
1259 int pos
= isl_space_dim(space
, isl_dim_out
) - 1;
1260 space
= space_set_dim_name(space
, pos
, v
->v
->name
);
1261 isl_token_free(tok
);
1262 if (isl_stream_eat_if_available(s
, '='))
1263 pa
= read_tuple_var_def(s
, v
, rational
);
1265 pa
= identity_tuple_el(v
);
1267 isl_stream_push_token(s
, tok
);
1269 if (vars_add_anon(v
) < 0)
1271 pa
= read_tuple_var_def(s
, v
, rational
);
1274 *list
= isl_pw_aff_list_add(*list
, pa
);
1276 return isl_space_free(space
);
1280 isl_token_free(tok
);
1281 return isl_space_free(space
);
1284 /* Read a tuple and represent it as an isl_multi_pw_aff.
1285 * The range space of the isl_multi_pw_aff is the space of the tuple.
1286 * The domain space is an anonymous space
1287 * with a dimension for each variable in the set of variables in "v",
1288 * including the variables in the range.
1289 * If a given dimension is not defined in terms of earlier dimensions in
1290 * the input, then the corresponding isl_pw_aff is set equal to one time
1291 * the variable corresponding to the dimension being defined.
1293 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1294 * Each element in this list is defined over a space representing
1295 * the variables defined so far. We need to adjust the earlier
1296 * elements to have as many variables in the domain as the final
1297 * element in the list.
1299 static __isl_give isl_multi_pw_aff
*read_tuple(__isl_keep isl_stream
*s
,
1300 struct vars
*v
, int rational
, int comma
)
1304 isl_pw_aff_list
*list
;
1306 space
= isl_space_params_alloc(v
->ctx
, 0);
1307 list
= isl_pw_aff_list_alloc(s
->ctx
, 0);
1308 space
= read_tuple_space(s
, v
, space
, rational
, comma
,
1309 &read_tuple_pw_aff_el
, &list
);
1310 n
= isl_space_dim(space
, isl_dim_set
);
1311 for (i
= 0; i
+ 1 < n
; ++i
) {
1314 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
1315 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, n
- (i
+ 1));
1316 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
1319 space
= isl_space_from_range(space
);
1320 space
= isl_space_add_dims(space
, isl_dim_in
, v
->n
);
1321 return isl_multi_pw_aff_from_pw_aff_list(space
, list
);
1324 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1325 * We first create the appropriate space in "map" based on the range
1326 * space of this isl_multi_pw_aff. Then, we add equalities based
1327 * on the affine expressions. These live in an anonymous space,
1328 * however, so we first need to reset the space to that of "map".
1330 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1331 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1336 isl_space
*space
= NULL
;
1340 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1341 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1342 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1346 if (type
== isl_dim_param
) {
1347 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1348 isl_space_is_wrapping(space
)) {
1349 isl_die(ctx
, isl_error_invalid
,
1350 "parameter tuples cannot be named or nested",
1353 map
= isl_map_add_dims(map
, type
, n
);
1354 for (i
= 0; i
< n
; ++i
) {
1356 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1357 isl_die(ctx
, isl_error_invalid
,
1358 "parameters must be named",
1360 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1361 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1363 } else if (type
== isl_dim_in
) {
1366 set
= isl_set_universe(isl_space_copy(space
));
1368 set
= isl_set_set_rational(set
);
1369 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1370 map
= isl_map_from_domain(set
);
1374 set
= isl_set_universe(isl_space_copy(space
));
1376 set
= isl_set_set_rational(set
);
1377 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1380 for (i
= 0; i
< n
; ++i
) {
1387 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1388 space
= isl_pw_aff_get_domain_space(pa
);
1389 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1390 aff
= isl_aff_add_coefficient_si(aff
,
1391 isl_dim_in
, v
->n
- n
+ i
, -1);
1392 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1394 pa
= isl_pw_aff_set_rational(pa
);
1395 set
= isl_pw_aff_zero_set(pa
);
1396 map_i
= isl_map_from_range(set
);
1397 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1398 map
= isl_map_intersect(map
, map_i
);
1401 isl_space_free(space
);
1402 isl_multi_pw_aff_free(tuple
);
1405 isl_space_free(space
);
1406 isl_multi_pw_aff_free(tuple
);
1411 /* Read a tuple from "s" and add it to "map".
1412 * The tuple is initially represented as an isl_multi_pw_aff and
1413 * then added to "map".
1415 static __isl_give isl_map
*read_map_tuple(__isl_keep isl_stream
*s
,
1416 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1417 int rational
, int comma
)
1419 isl_multi_pw_aff
*tuple
;
1421 tuple
= read_tuple(s
, v
, rational
, comma
);
1423 return isl_map_free(map
);
1425 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1428 static __isl_give isl_set
*construct_constraints(
1429 __isl_take isl_set
*set
, int type
,
1430 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1435 left
= isl_pw_aff_list_copy(left
);
1436 right
= isl_pw_aff_list_copy(right
);
1438 left
= isl_pw_aff_list_set_rational(left
);
1439 right
= isl_pw_aff_list_set_rational(right
);
1441 if (type
== ISL_TOKEN_LE
)
1442 cond
= isl_pw_aff_list_le_set(left
, right
);
1443 else if (type
== ISL_TOKEN_GE
)
1444 cond
= isl_pw_aff_list_ge_set(left
, right
);
1445 else if (type
== ISL_TOKEN_LT
)
1446 cond
= isl_pw_aff_list_lt_set(left
, right
);
1447 else if (type
== ISL_TOKEN_GT
)
1448 cond
= isl_pw_aff_list_gt_set(left
, right
);
1449 else if (type
== ISL_TOKEN_NE
)
1450 cond
= isl_pw_aff_list_ne_set(left
, right
);
1452 cond
= isl_pw_aff_list_eq_set(left
, right
);
1454 return isl_set_intersect(set
, cond
);
1457 static __isl_give isl_map
*add_constraint(__isl_keep isl_stream
*s
,
1458 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1460 struct isl_token
*tok
= NULL
;
1461 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1464 set
= isl_map_wrap(map
);
1465 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1468 tok
= isl_stream_next_token(s
);
1469 if (!is_comparator(tok
)) {
1470 isl_stream_error(s
, tok
, "missing operator");
1472 isl_stream_push_token(s
, tok
);
1477 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1481 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1483 isl_token_free(tok
);
1484 isl_pw_aff_list_free(list1
);
1487 tok
= isl_stream_next_token(s
);
1488 if (!is_comparator(tok
)) {
1490 isl_stream_push_token(s
, tok
);
1494 isl_pw_aff_list_free(list1
);
1496 return isl_set_unwrap(set
);
1499 isl_token_free(tok
);
1500 isl_pw_aff_list_free(list1
);
1501 isl_pw_aff_list_free(list2
);
1506 static __isl_give isl_map
*read_exists(__isl_keep isl_stream
*s
,
1507 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1510 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1512 map
= isl_map_from_domain(isl_map_wrap(map
));
1513 map
= read_defined_var_list(s
, v
, map
, rational
);
1515 if (isl_stream_eat(s
, ':'))
1518 map
= read_formula(s
, v
, map
, rational
);
1519 map
= isl_set_unwrap(isl_map_domain(map
));
1521 vars_drop(v
, v
->n
- n
);
1522 if (seen_paren
&& isl_stream_eat(s
, ')'))
1531 /* Parse an expression between parentheses and push the result
1532 * back on the stream.
1534 * The parsed expression may be either an affine expression
1535 * or a condition. The first type is pushed onto the stream
1536 * as an isl_pw_aff, while the second is pushed as an isl_map.
1538 * If the initial token indicates the start of a condition,
1539 * we parse it as such.
1540 * Otherwise, we first parse an affine expression and push
1541 * that onto the stream. If the affine expression covers the
1542 * entire expression between parentheses, we return.
1543 * Otherwise, we assume that the affine expression is the
1544 * start of a condition and continue parsing.
1546 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
1547 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1549 struct isl_token
*tok
, *tok2
;
1553 tok
= isl_stream_next_token(s
);
1554 if (!tok
|| tok
->type
!= '(')
1557 if (isl_stream_next_token_is(s
, '('))
1558 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1561 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1562 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1563 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1564 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1565 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1566 map
= read_formula(s
, v
, map
, rational
);
1567 if (isl_stream_eat(s
, ')'))
1569 tok
->type
= ISL_TOKEN_MAP
;
1571 isl_stream_push_token(s
, tok
);
1575 tok2
= isl_stream_next_token(s
);
1580 isl_stream_push_token(s
, tok2
);
1582 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1586 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1589 tok2
->type
= ISL_TOKEN_AFF
;
1590 tok2
->u
.pwaff
= pwaff
;
1592 if (isl_stream_eat_if_available(s
, ')')) {
1593 isl_stream_push_token(s
, tok2
);
1594 isl_token_free(tok
);
1599 isl_stream_push_token(s
, tok2
);
1601 map
= read_formula(s
, v
, map
, rational
);
1602 if (isl_stream_eat(s
, ')'))
1605 tok
->type
= ISL_TOKEN_MAP
;
1607 isl_stream_push_token(s
, tok
);
1611 isl_pw_aff_free(pwaff
);
1613 isl_token_free(tok
);
1618 static __isl_give isl_map
*read_conjunct(__isl_keep isl_stream
*s
,
1619 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1621 if (isl_stream_next_token_is(s
, '('))
1622 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1625 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1626 struct isl_token
*tok
;
1627 tok
= isl_stream_next_token(s
);
1631 map
= isl_map_copy(tok
->u
.map
);
1632 isl_token_free(tok
);
1636 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1637 return read_exists(s
, v
, map
, rational
);
1639 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1642 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1643 isl_space
*dim
= isl_map_get_space(map
);
1645 return isl_map_empty(dim
);
1648 return add_constraint(s
, v
, map
, rational
);
1654 static __isl_give isl_map
*read_conjuncts(__isl_keep isl_stream
*s
,
1655 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1660 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1661 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1663 res
= isl_map_subtract(isl_map_copy(map
), res
);
1665 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1668 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1669 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1671 res
= isl_map_subtract(res
, res_i
);
1673 res
= isl_map_intersect(res
, res_i
);
1680 static struct isl_map
*read_disjuncts(__isl_keep isl_stream
*s
,
1681 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1685 if (isl_stream_next_token_is(s
, '}')) {
1686 isl_space
*dim
= isl_map_get_space(map
);
1688 return isl_map_universe(dim
);
1691 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1692 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1695 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1696 res
= isl_map_union(res
, res_i
);
1703 /* Read a first order formula from "s", add the corresponding
1704 * constraints to "map" and return the result.
1706 * In particular, read a formula of the form
1714 * where a and b are disjunctions.
1716 * In the first case, map is replaced by
1718 * map \cap { [..] : a }
1720 * In the second case, it is replaced by
1722 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1724 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
1725 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1729 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1731 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1734 res
= isl_map_subtract(isl_map_copy(map
), res
);
1735 res2
= read_disjuncts(s
, v
, map
, rational
);
1736 res
= isl_map_union(res
, res2
);
1743 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1745 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1746 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1747 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1748 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1750 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1751 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1752 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1754 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1755 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1756 isl_basic_map_dim(bmap
, isl_dim_in
) +
1757 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1758 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1760 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1766 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1767 __isl_keep isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1770 struct isl_token
*tok
;
1778 tok
= isl_stream_next_token(s
);
1779 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1780 isl_stream_error(s
, tok
, "expecting coefficient");
1782 isl_stream_push_token(s
, tok
);
1785 if (!tok
->on_new_line
) {
1786 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1787 isl_stream_push_token(s
, tok
);
1791 type
= isl_int_get_si(tok
->u
.v
);
1792 isl_token_free(tok
);
1794 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1796 k
= isl_basic_map_alloc_equality(bmap
);
1799 k
= isl_basic_map_alloc_inequality(bmap
);
1805 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1807 tok
= isl_stream_next_token(s
);
1808 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1809 isl_stream_error(s
, tok
, "expecting coefficient");
1811 isl_stream_push_token(s
, tok
);
1814 if (tok
->on_new_line
) {
1815 isl_stream_error(s
, tok
,
1816 "coefficient should not appear on new line");
1817 isl_stream_push_token(s
, tok
);
1820 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1821 isl_int_set(c
[pos
], tok
->u
.v
);
1822 isl_token_free(tok
);
1827 isl_basic_map_free(bmap
);
1831 static __isl_give isl_basic_map
*basic_map_read_polylib(
1832 __isl_keep isl_stream
*s
)
1835 struct isl_token
*tok
;
1836 struct isl_token
*tok2
;
1839 unsigned in
= 0, out
, local
= 0;
1840 struct isl_basic_map
*bmap
= NULL
;
1843 tok
= isl_stream_next_token(s
);
1845 isl_stream_error(s
, NULL
, "unexpected EOF");
1848 tok2
= isl_stream_next_token(s
);
1850 isl_token_free(tok
);
1851 isl_stream_error(s
, NULL
, "unexpected EOF");
1854 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1855 isl_stream_push_token(s
, tok2
);
1856 isl_stream_push_token(s
, tok
);
1857 isl_stream_error(s
, NULL
,
1858 "expecting constraint matrix dimensions");
1861 n_row
= isl_int_get_si(tok
->u
.v
);
1862 n_col
= isl_int_get_si(tok2
->u
.v
);
1863 on_new_line
= tok2
->on_new_line
;
1864 isl_token_free(tok2
);
1865 isl_token_free(tok
);
1866 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1867 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1868 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1869 tok
= isl_stream_next_token_on_same_line(s
);
1871 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1872 isl_stream_error(s
, tok
,
1873 "expecting number of output dimensions");
1874 isl_stream_push_token(s
, tok
);
1877 out
= isl_int_get_si(tok
->u
.v
);
1878 isl_token_free(tok
);
1880 tok
= isl_stream_next_token_on_same_line(s
);
1881 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1882 isl_stream_error(s
, tok
,
1883 "expecting number of input dimensions");
1885 isl_stream_push_token(s
, tok
);
1888 in
= isl_int_get_si(tok
->u
.v
);
1889 isl_token_free(tok
);
1891 tok
= isl_stream_next_token_on_same_line(s
);
1892 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1893 isl_stream_error(s
, tok
,
1894 "expecting number of existentials");
1896 isl_stream_push_token(s
, tok
);
1899 local
= isl_int_get_si(tok
->u
.v
);
1900 isl_token_free(tok
);
1902 tok
= isl_stream_next_token_on_same_line(s
);
1903 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1904 isl_stream_error(s
, tok
,
1905 "expecting number of parameters");
1907 isl_stream_push_token(s
, tok
);
1910 nparam
= isl_int_get_si(tok
->u
.v
);
1911 isl_token_free(tok
);
1912 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1913 isl_stream_error(s
, NULL
,
1914 "dimensions don't match");
1918 out
= n_col
- 2 - nparam
;
1919 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1923 for (i
= 0; i
< local
; ++i
) {
1924 int k
= isl_basic_map_alloc_div(bmap
);
1927 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1930 for (i
= 0; i
< n_row
; ++i
)
1931 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1933 tok
= isl_stream_next_token_on_same_line(s
);
1935 isl_stream_error(s
, tok
, "unexpected extra token on line");
1936 isl_stream_push_token(s
, tok
);
1940 bmap
= isl_basic_map_simplify(bmap
);
1941 bmap
= isl_basic_map_finalize(bmap
);
1944 isl_basic_map_free(bmap
);
1948 static struct isl_map
*map_read_polylib(__isl_keep isl_stream
*s
)
1950 struct isl_token
*tok
;
1951 struct isl_token
*tok2
;
1953 struct isl_map
*map
;
1955 tok
= isl_stream_next_token(s
);
1957 isl_stream_error(s
, NULL
, "unexpected EOF");
1960 tok2
= isl_stream_next_token_on_same_line(s
);
1961 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1962 isl_stream_push_token(s
, tok2
);
1963 isl_stream_push_token(s
, tok
);
1964 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1967 isl_stream_error(s
, tok2
, "unexpected token");
1968 isl_stream_push_token(s
, tok2
);
1969 isl_stream_push_token(s
, tok
);
1972 n
= isl_int_get_si(tok
->u
.v
);
1973 isl_token_free(tok
);
1975 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1977 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1979 for (i
= 1; map
&& i
< n
; ++i
)
1980 map
= isl_map_union(map
,
1981 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1986 static int optional_power(__isl_keep isl_stream
*s
)
1989 struct isl_token
*tok
;
1991 tok
= isl_stream_next_token(s
);
1994 if (tok
->type
!= '^') {
1995 isl_stream_push_token(s
, tok
);
1998 isl_token_free(tok
);
1999 tok
= isl_stream_next_token(s
);
2000 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2001 isl_stream_error(s
, tok
, "expecting exponent");
2003 isl_stream_push_token(s
, tok
);
2006 pow
= isl_int_get_si(tok
->u
.v
);
2007 isl_token_free(tok
);
2011 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2012 __isl_keep isl_map
*map
, struct vars
*v
);
2014 static __isl_give isl_pw_qpolynomial
*read_factor(__isl_keep isl_stream
*s
,
2015 __isl_keep isl_map
*map
, struct vars
*v
)
2017 isl_pw_qpolynomial
*pwqp
;
2018 struct isl_token
*tok
;
2020 tok
= next_token(s
);
2022 isl_stream_error(s
, NULL
, "unexpected EOF");
2025 if (tok
->type
== '(') {
2028 isl_token_free(tok
);
2029 pwqp
= read_term(s
, map
, v
);
2032 if (isl_stream_eat(s
, ')'))
2034 pow
= optional_power(s
);
2035 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2036 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
2037 struct isl_token
*tok2
;
2038 isl_qpolynomial
*qp
;
2040 tok2
= isl_stream_next_token(s
);
2041 if (tok2
&& tok2
->type
== '/') {
2042 isl_token_free(tok2
);
2043 tok2
= next_token(s
);
2044 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2045 isl_stream_error(s
, tok2
, "expected denominator");
2046 isl_token_free(tok
);
2047 isl_token_free(tok2
);
2050 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
2051 tok
->u
.v
, tok2
->u
.v
);
2052 isl_token_free(tok2
);
2054 isl_stream_push_token(s
, tok2
);
2055 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
2058 isl_token_free(tok
);
2059 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2060 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2061 isl_qpolynomial
*qp
;
2062 isl_token_free(tok
);
2063 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2064 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2065 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2066 isl_qpolynomial
*qp
;
2067 isl_token_free(tok
);
2068 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2069 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2070 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2072 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2074 isl_qpolynomial
*qp
;
2076 isl_token_free(tok
);
2080 vars_drop(v
, v
->n
- n
);
2081 isl_stream_error(s
, tok
, "unknown identifier");
2082 isl_token_free(tok
);
2085 isl_token_free(tok
);
2086 pow
= optional_power(s
);
2087 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2088 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2089 } else if (is_start_of_div(tok
)) {
2093 isl_stream_push_token(s
, tok
);
2094 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2095 pow
= optional_power(s
);
2096 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2097 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2098 } else if (tok
->type
== '-') {
2099 isl_token_free(tok
);
2100 pwqp
= read_factor(s
, map
, v
);
2101 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2103 isl_stream_error(s
, tok
, "unexpected isl_token");
2104 isl_stream_push_token(s
, tok
);
2108 if (isl_stream_eat_if_available(s
, '*') ||
2109 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2110 isl_pw_qpolynomial
*pwqp2
;
2112 pwqp2
= read_factor(s
, map
, v
);
2113 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2118 isl_pw_qpolynomial_free(pwqp
);
2122 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2123 __isl_keep isl_map
*map
, struct vars
*v
)
2125 struct isl_token
*tok
;
2126 isl_pw_qpolynomial
*pwqp
;
2128 pwqp
= read_factor(s
, map
, v
);
2131 tok
= next_token(s
);
2135 if (tok
->type
== '+') {
2136 isl_pw_qpolynomial
*pwqp2
;
2138 isl_token_free(tok
);
2139 pwqp2
= read_factor(s
, map
, v
);
2140 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2141 } else if (tok
->type
== '-') {
2142 isl_pw_qpolynomial
*pwqp2
;
2144 isl_token_free(tok
);
2145 pwqp2
= read_factor(s
, map
, v
);
2146 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2147 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2148 isl_int_is_neg(tok
->u
.v
)) {
2149 isl_pw_qpolynomial
*pwqp2
;
2151 isl_stream_push_token(s
, tok
);
2152 pwqp2
= read_factor(s
, map
, v
);
2153 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2155 isl_stream_push_token(s
, tok
);
2163 static __isl_give isl_map
*read_optional_formula(__isl_keep isl_stream
*s
,
2164 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2166 struct isl_token
*tok
;
2168 tok
= isl_stream_next_token(s
);
2170 isl_stream_error(s
, NULL
, "unexpected EOF");
2173 if (tok
->type
== ':' ||
2174 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2175 isl_token_free(tok
);
2176 map
= read_formula(s
, v
, map
, rational
);
2178 isl_stream_push_token(s
, tok
);
2186 static struct isl_obj
obj_read_poly(__isl_keep isl_stream
*s
,
2187 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2189 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2190 isl_pw_qpolynomial
*pwqp
;
2191 struct isl_set
*set
;
2193 pwqp
= read_term(s
, map
, v
);
2194 map
= read_optional_formula(s
, map
, v
, 0);
2195 set
= isl_map_range(map
);
2197 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2199 vars_drop(v
, v
->n
- n
);
2205 static struct isl_obj
obj_read_poly_or_fold(__isl_keep isl_stream
*s
,
2206 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2208 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2209 isl_pw_qpolynomial
*pwqp
;
2210 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2212 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2213 return obj_read_poly(s
, set
, v
, n
);
2215 if (isl_stream_eat(s
, '('))
2218 pwqp
= read_term(s
, set
, v
);
2219 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2221 while (isl_stream_eat_if_available(s
, ',')) {
2222 isl_pw_qpolynomial_fold
*pwf_i
;
2223 pwqp
= read_term(s
, set
, v
);
2224 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2226 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2229 if (isl_stream_eat(s
, ')'))
2232 set
= read_optional_formula(s
, set
, v
, 0);
2233 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2235 vars_drop(v
, v
->n
- n
);
2241 isl_pw_qpolynomial_fold_free(pwf
);
2242 obj
.type
= isl_obj_none
;
2246 static int is_rational(__isl_keep isl_stream
*s
)
2248 struct isl_token
*tok
;
2250 tok
= isl_stream_next_token(s
);
2253 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2254 isl_token_free(tok
);
2255 isl_stream_eat(s
, ':');
2259 isl_stream_push_token(s
, tok
);
2264 static struct isl_obj
obj_read_body(__isl_keep isl_stream
*s
,
2265 __isl_take isl_map
*map
, struct vars
*v
)
2267 struct isl_token
*tok
;
2268 struct isl_obj obj
= { isl_obj_set
, NULL
};
2272 rational
= is_rational(s
);
2274 map
= isl_map_set_rational(map
);
2276 if (isl_stream_next_token_is(s
, ':')) {
2277 obj
.type
= isl_obj_set
;
2278 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2282 if (!next_is_tuple(s
))
2283 return obj_read_poly_or_fold(s
, map
, v
, n
);
2285 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2288 tok
= isl_stream_next_token(s
);
2291 if (tok
->type
== ISL_TOKEN_TO
) {
2292 obj
.type
= isl_obj_map
;
2293 isl_token_free(tok
);
2294 if (!next_is_tuple(s
)) {
2295 isl_set
*set
= isl_map_domain(map
);
2296 return obj_read_poly_or_fold(s
, set
, v
, n
);
2298 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2302 map
= isl_map_domain(map
);
2303 isl_stream_push_token(s
, tok
);
2306 map
= read_optional_formula(s
, map
, v
, rational
);
2308 vars_drop(v
, v
->n
- n
);
2314 obj
.type
= isl_obj_none
;
2318 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2320 if (obj
.type
== isl_obj_map
) {
2321 obj
.v
= isl_union_map_from_map(obj
.v
);
2322 obj
.type
= isl_obj_union_map
;
2323 } else if (obj
.type
== isl_obj_set
) {
2324 obj
.v
= isl_union_set_from_set(obj
.v
);
2325 obj
.type
= isl_obj_union_set
;
2326 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2327 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2328 obj
.type
= isl_obj_union_pw_qpolynomial
;
2329 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2330 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2331 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2333 isl_assert(ctx
, 0, goto error
);
2336 obj
.type
->free(obj
.v
);
2337 obj
.type
= isl_obj_none
;
2341 static struct isl_obj
obj_add(__isl_keep isl_stream
*s
,
2342 struct isl_obj obj1
, struct isl_obj obj2
)
2344 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2345 obj1
= to_union(s
->ctx
, obj1
);
2346 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2347 obj2
= to_union(s
->ctx
, obj2
);
2348 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2349 obj1
= to_union(s
->ctx
, obj1
);
2350 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2351 obj2
= to_union(s
->ctx
, obj2
);
2352 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2353 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2354 obj1
= to_union(s
->ctx
, obj1
);
2355 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2356 obj2
.type
== isl_obj_pw_qpolynomial
)
2357 obj2
= to_union(s
->ctx
, obj2
);
2358 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2359 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2360 obj1
= to_union(s
->ctx
, obj1
);
2361 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2362 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2363 obj2
= to_union(s
->ctx
, obj2
);
2364 if (obj1
.type
!= obj2
.type
) {
2365 isl_stream_error(s
, NULL
,
2366 "attempt to combine incompatible objects");
2369 if (!obj1
.type
->add
)
2370 isl_die(s
->ctx
, isl_error_internal
,
2371 "combination not supported on object type", goto error
);
2372 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2373 obj1
= to_union(s
->ctx
, obj1
);
2374 obj2
= to_union(s
->ctx
, obj2
);
2376 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2377 obj1
= to_union(s
->ctx
, obj1
);
2378 obj2
= to_union(s
->ctx
, obj2
);
2380 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2381 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2382 obj1
= to_union(s
->ctx
, obj1
);
2383 obj2
= to_union(s
->ctx
, obj2
);
2385 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2386 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2387 obj1
= to_union(s
->ctx
, obj1
);
2388 obj2
= to_union(s
->ctx
, obj2
);
2390 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2393 obj1
.type
->free(obj1
.v
);
2394 obj2
.type
->free(obj2
.v
);
2395 obj1
.type
= isl_obj_none
;
2400 /* Are the first two tokens on "s", "domain" (either as a string
2401 * or as an identifier) followed by ":"?
2403 static int next_is_domain_colon(__isl_keep isl_stream
*s
)
2405 struct isl_token
*tok
;
2409 tok
= isl_stream_next_token(s
);
2412 if (tok
->type
!= ISL_TOKEN_IDENT
&& tok
->type
!= ISL_TOKEN_STRING
) {
2413 isl_stream_push_token(s
, tok
);
2417 name
= isl_token_get_str(s
->ctx
, tok
);
2418 res
= !strcmp(name
, "domain") && isl_stream_next_token_is(s
, ':');
2421 isl_stream_push_token(s
, tok
);
2426 /* Do the first tokens on "s" look like a schedule?
2428 * The root of a schedule is always a domain node, so the first thing
2429 * we expect in the stream is a domain key, i.e., "domain" followed
2430 * by ":". If the schedule was printed in YAML flow style, then
2431 * we additionally expect a "{" to open the outer mapping.
2433 static int next_is_schedule(__isl_keep isl_stream
*s
)
2435 struct isl_token
*tok
;
2438 tok
= isl_stream_next_token(s
);
2441 if (tok
->type
!= '{') {
2442 isl_stream_push_token(s
, tok
);
2443 return next_is_domain_colon(s
);
2446 is_schedule
= next_is_domain_colon(s
);
2447 isl_stream_push_token(s
, tok
);
2452 /* Read an isl_schedule from "s" and store it in an isl_obj.
2454 static struct isl_obj
schedule_read(__isl_keep isl_stream
*s
)
2458 obj
.type
= isl_obj_schedule
;
2459 obj
.v
= isl_stream_read_schedule(s
);
2464 static struct isl_obj
obj_read(__isl_keep isl_stream
*s
)
2466 isl_map
*map
= NULL
;
2467 struct isl_token
*tok
;
2468 struct vars
*v
= NULL
;
2469 struct isl_obj obj
= { isl_obj_set
, NULL
};
2471 if (next_is_schedule(s
))
2472 return schedule_read(s
);
2474 tok
= next_token(s
);
2476 isl_stream_error(s
, NULL
, "unexpected EOF");
2479 if (tok
->type
== ISL_TOKEN_VALUE
) {
2480 struct isl_token
*tok2
;
2481 struct isl_map
*map
;
2483 tok2
= isl_stream_next_token(s
);
2484 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2485 isl_int_is_neg(tok2
->u
.v
)) {
2487 isl_stream_push_token(s
, tok2
);
2488 obj
.type
= isl_obj_val
;
2489 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2490 isl_token_free(tok
);
2493 isl_stream_push_token(s
, tok2
);
2494 isl_stream_push_token(s
, tok
);
2495 map
= map_read_polylib(s
);
2498 if (isl_map_may_be_set(map
))
2499 obj
.v
= isl_map_range(map
);
2501 obj
.type
= isl_obj_map
;
2506 v
= vars_new(s
->ctx
);
2508 isl_stream_push_token(s
, tok
);
2511 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2512 if (tok
->type
== '[') {
2513 isl_stream_push_token(s
, tok
);
2514 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2517 tok
= isl_stream_next_token(s
);
2518 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2519 isl_stream_error(s
, tok
, "expecting '->'");
2521 isl_stream_push_token(s
, tok
);
2524 isl_token_free(tok
);
2525 tok
= isl_stream_next_token(s
);
2527 if (!tok
|| tok
->type
!= '{') {
2528 isl_stream_error(s
, tok
, "expecting '{'");
2530 isl_stream_push_token(s
, tok
);
2533 isl_token_free(tok
);
2535 tok
= isl_stream_next_token(s
);
2538 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2539 isl_token_free(tok
);
2540 if (isl_stream_eat(s
, '='))
2542 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2545 } else if (tok
->type
== '}') {
2546 obj
.type
= isl_obj_union_set
;
2547 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2548 isl_token_free(tok
);
2551 isl_stream_push_token(s
, tok
);
2556 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2557 if (o
.type
== isl_obj_none
|| !o
.v
)
2562 obj
= obj_add(s
, obj
, o
);
2563 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2566 tok
= isl_stream_next_token(s
);
2567 if (!tok
|| tok
->type
!= ';')
2569 isl_token_free(tok
);
2570 if (isl_stream_next_token_is(s
, '}')) {
2571 tok
= isl_stream_next_token(s
);
2576 if (tok
&& tok
->type
== '}') {
2577 isl_token_free(tok
);
2579 isl_stream_error(s
, tok
, "unexpected isl_token");
2581 isl_token_free(tok
);
2591 obj
.type
->free(obj
.v
);
2598 struct isl_obj
isl_stream_read_obj(__isl_keep isl_stream
*s
)
2603 __isl_give isl_map
*isl_stream_read_map(__isl_keep isl_stream
*s
)
2609 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2610 obj
.type
== isl_obj_set
, goto error
);
2612 if (obj
.type
== isl_obj_set
)
2613 obj
.v
= isl_map_from_range(obj
.v
);
2617 obj
.type
->free(obj
.v
);
2621 __isl_give isl_set
*isl_stream_read_set(__isl_keep isl_stream
*s
)
2627 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2628 obj
.v
= isl_map_range(obj
.v
);
2629 obj
.type
= isl_obj_set
;
2631 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2636 obj
.type
->free(obj
.v
);
2640 __isl_give isl_union_map
*isl_stream_read_union_map(__isl_keep isl_stream
*s
)
2645 if (obj
.type
== isl_obj_map
) {
2646 obj
.type
= isl_obj_union_map
;
2647 obj
.v
= isl_union_map_from_map(obj
.v
);
2649 if (obj
.type
== isl_obj_set
) {
2650 obj
.type
= isl_obj_union_set
;
2651 obj
.v
= isl_union_set_from_set(obj
.v
);
2653 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2654 isl_union_set_is_empty(obj
.v
))
2655 obj
.type
= isl_obj_union_map
;
2656 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2657 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2661 obj
.type
->free(obj
.v
);
2665 __isl_give isl_union_set
*isl_stream_read_union_set(__isl_keep isl_stream
*s
)
2670 if (obj
.type
== isl_obj_set
) {
2671 obj
.type
= isl_obj_union_set
;
2672 obj
.v
= isl_union_set_from_set(obj
.v
);
2675 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2679 obj
.type
->free(obj
.v
);
2683 static __isl_give isl_basic_map
*basic_map_read(__isl_keep isl_stream
*s
)
2686 struct isl_map
*map
;
2687 struct isl_basic_map
*bmap
;
2690 if (obj
.v
&& (obj
.type
!= isl_obj_map
&& obj
.type
!= isl_obj_set
))
2691 isl_die(s
->ctx
, isl_error_invalid
, "not a (basic) set or map",
2698 isl_die(s
->ctx
, isl_error_invalid
,
2699 "set or map description involves "
2700 "more than one disjunct", goto error
);
2703 bmap
= isl_basic_map_empty_like_map(map
);
2705 bmap
= isl_basic_map_copy(map
->p
[0]);
2711 obj
.type
->free(obj
.v
);
2715 static __isl_give isl_basic_set
*basic_set_read(__isl_keep isl_stream
*s
)
2717 isl_basic_map
*bmap
;
2718 bmap
= basic_map_read(s
);
2721 if (!isl_basic_map_may_be_set(bmap
))
2722 isl_die(s
->ctx
, isl_error_invalid
,
2723 "input is not a set", goto error
);
2724 return isl_basic_map_range(bmap
);
2726 isl_basic_map_free(bmap
);
2730 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2733 struct isl_basic_map
*bmap
;
2734 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2737 bmap
= basic_map_read(s
);
2742 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2745 isl_basic_set
*bset
;
2746 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2749 bset
= basic_set_read(s
);
2754 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2757 struct isl_basic_map
*bmap
;
2758 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2761 bmap
= basic_map_read(s
);
2766 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2769 isl_basic_set
*bset
;
2770 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2773 bset
= basic_set_read(s
);
2778 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2781 struct isl_map
*map
;
2782 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2785 map
= isl_stream_read_map(s
);
2790 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2793 struct isl_map
*map
;
2794 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2797 map
= isl_stream_read_map(s
);
2802 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2806 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2809 set
= isl_stream_read_set(s
);
2814 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2818 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2821 set
= isl_stream_read_set(s
);
2826 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2829 isl_union_map
*umap
;
2830 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2833 umap
= isl_stream_read_union_map(s
);
2838 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2841 isl_union_map
*umap
;
2842 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2845 umap
= isl_stream_read_union_map(s
);
2850 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2853 isl_union_set
*uset
;
2854 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2857 uset
= isl_stream_read_union_set(s
);
2862 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2865 isl_union_set
*uset
;
2866 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2869 uset
= isl_stream_read_union_set(s
);
2874 static __isl_give isl_vec
*isl_vec_read_polylib(__isl_keep isl_stream
*s
)
2876 struct isl_vec
*vec
= NULL
;
2877 struct isl_token
*tok
;
2881 tok
= isl_stream_next_token(s
);
2882 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2883 isl_stream_error(s
, tok
, "expecting vector length");
2887 size
= isl_int_get_si(tok
->u
.v
);
2888 isl_token_free(tok
);
2890 vec
= isl_vec_alloc(s
->ctx
, size
);
2892 for (j
= 0; j
< size
; ++j
) {
2893 tok
= isl_stream_next_token(s
);
2894 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2895 isl_stream_error(s
, tok
, "expecting constant value");
2898 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2899 isl_token_free(tok
);
2904 isl_token_free(tok
);
2909 static __isl_give isl_vec
*vec_read(__isl_keep isl_stream
*s
)
2911 return isl_vec_read_polylib(s
);
2914 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2917 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2925 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2926 __isl_keep isl_stream
*s
)
2932 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2937 obj
.type
->free(obj
.v
);
2941 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2944 isl_pw_qpolynomial
*pwqp
;
2945 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2948 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2953 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2956 isl_pw_qpolynomial
*pwqp
;
2957 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2960 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2965 /* Is the next token an identifer not in "v"?
2967 static int next_is_fresh_ident(__isl_keep isl_stream
*s
, struct vars
*v
)
2971 struct isl_token
*tok
;
2973 tok
= isl_stream_next_token(s
);
2976 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2977 isl_stream_push_token(s
, tok
);
2979 vars_drop(v
, v
->n
- n
);
2984 /* First read the domain of the affine expression, which may be
2985 * a parameter space or a set.
2986 * The tricky part is that we don't know if the domain is a set or not,
2987 * so when we are trying to read the domain, we may actually be reading
2988 * the affine expression itself (defined on a parameter domains)
2989 * If the tuple we are reading is named, we assume it's the domain.
2990 * Also, if inside the tuple, the first thing we find is a nested tuple
2991 * or a new identifier, we again assume it's the domain.
2992 * Otherwise, we assume we are reading an affine expression.
2994 static __isl_give isl_set
*read_aff_domain(__isl_keep isl_stream
*s
,
2995 __isl_take isl_set
*dom
, struct vars
*v
)
2997 struct isl_token
*tok
;
2999 tok
= isl_stream_next_token(s
);
3000 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
3001 isl_stream_push_token(s
, tok
);
3002 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
3004 if (!tok
|| tok
->type
!= '[') {
3005 isl_stream_error(s
, tok
, "expecting '['");
3008 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
3009 isl_stream_push_token(s
, tok
);
3010 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
3012 isl_stream_push_token(s
, tok
);
3017 isl_stream_push_token(s
, tok
);
3022 /* Read an affine expression from "s".
3024 __isl_give isl_aff
*isl_stream_read_aff(__isl_keep isl_stream
*s
)
3029 ma
= isl_stream_read_multi_aff(s
);
3032 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
3033 isl_die(s
->ctx
, isl_error_invalid
,
3034 "expecting single affine expression",
3037 aff
= isl_multi_aff_get_aff(ma
, 0);
3038 isl_multi_aff_free(ma
);
3041 isl_multi_aff_free(ma
);
3045 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3047 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(__isl_keep isl_stream
*s
,
3048 __isl_take isl_set
*dom
, struct vars
*v
)
3050 isl_pw_aff
*pwaff
= NULL
;
3052 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
3055 if (isl_stream_eat(s
, '['))
3058 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
3060 if (isl_stream_eat(s
, ']'))
3063 dom
= read_optional_formula(s
, dom
, v
, 0);
3064 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3069 isl_pw_aff_free(pwaff
);
3073 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(__isl_keep isl_stream
*s
)
3076 isl_set
*dom
= NULL
;
3078 isl_pw_aff
*pa
= NULL
;
3081 v
= vars_new(s
->ctx
);
3085 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3086 if (next_is_tuple(s
)) {
3087 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3088 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3091 if (isl_stream_eat(s
, '{'))
3095 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3096 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3097 vars_drop(v
, v
->n
- n
);
3099 while (isl_stream_eat_if_available(s
, ';')) {
3103 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3104 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3105 vars_drop(v
, v
->n
- n
);
3107 pa
= isl_pw_aff_union_add(pa
, pa_i
);
3110 if (isl_stream_eat(s
, '}'))
3119 isl_pw_aff_free(pa
);
3123 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3126 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3129 aff
= isl_stream_read_aff(s
);
3134 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3137 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3140 pa
= isl_stream_read_pw_aff(s
);
3145 /* Read an isl_pw_multi_aff from "s".
3146 * We currently read a generic object and if it turns out to be a set or
3147 * a map, we convert that to an isl_pw_multi_aff.
3148 * It would be more efficient if we were to construct the isl_pw_multi_aff
3151 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(
3152 __isl_keep isl_stream
*s
)
3160 if (obj
.type
== isl_obj_map
)
3161 return isl_pw_multi_aff_from_map(obj
.v
);
3162 if (obj
.type
== isl_obj_set
)
3163 return isl_pw_multi_aff_from_set(obj
.v
);
3165 obj
.type
->free(obj
.v
);
3166 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3170 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3173 isl_pw_multi_aff
*pma
;
3174 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3177 pma
= isl_stream_read_pw_multi_aff(s
);
3182 /* Read an isl_union_pw_multi_aff from "s".
3183 * We currently read a generic object and if it turns out to be a set or
3184 * a map, we convert that to an isl_union_pw_multi_aff.
3185 * It would be more efficient if we were to construct
3186 * the isl_union_pw_multi_aff directly.
3188 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3189 __isl_keep isl_stream
*s
)
3197 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3198 obj
= to_union(s
->ctx
, obj
);
3199 if (obj
.type
== isl_obj_union_map
)
3200 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3201 if (obj
.type
== isl_obj_union_set
)
3202 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3204 obj
.type
->free(obj
.v
);
3205 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3209 /* Read an isl_union_pw_multi_aff from "str".
3211 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3212 isl_ctx
*ctx
, const char *str
)
3214 isl_union_pw_multi_aff
*upma
;
3215 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3218 upma
= isl_stream_read_union_pw_multi_aff(s
);
3223 /* Assuming "pa" represents a single affine expression defined on a universe
3224 * domain, extract this affine expression.
3226 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3233 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3234 "expecting single affine expression",
3236 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3237 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3238 "expecting universe domain",
3241 aff
= isl_aff_copy(pa
->p
[0].aff
);
3242 isl_pw_aff_free(pa
);
3245 isl_pw_aff_free(pa
);
3249 /* This function is called for each element in a tuple inside
3250 * isl_stream_read_multi_val.
3251 * Read an isl_val from "s" and add it to *list.
3253 static __isl_give isl_space
*read_val_el(__isl_keep isl_stream
*s
,
3254 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3256 isl_val_list
**list
= (isl_val_list
**) user
;
3259 val
= isl_stream_read_val(s
);
3260 *list
= isl_val_list_add(*list
, val
);
3262 return isl_space_free(space
);
3267 /* Read an isl_multi_val from "s".
3269 * We first read a tuple space, collecting the element values in a list.
3270 * Then we create an isl_multi_val from the space and the isl_val_list.
3272 __isl_give isl_multi_val
*isl_stream_read_multi_val(__isl_keep isl_stream
*s
)
3275 isl_set
*dom
= NULL
;
3277 isl_multi_val
*mv
= NULL
;
3280 v
= vars_new(s
->ctx
);
3284 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3285 if (next_is_tuple(s
)) {
3286 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3287 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3290 if (!isl_set_plain_is_universe(dom
))
3291 isl_die(s
->ctx
, isl_error_invalid
,
3292 "expecting universe parameter domain", goto error
);
3293 if (isl_stream_eat(s
, '{'))
3296 space
= isl_set_get_space(dom
);
3298 list
= isl_val_list_alloc(s
->ctx
, 0);
3299 space
= read_tuple_space(s
, v
, space
, 1, 0, &read_val_el
, &list
);
3300 mv
= isl_multi_val_from_val_list(space
, list
);
3302 if (isl_stream_eat(s
, '}'))
3311 isl_multi_val_free(mv
);
3315 /* Read an isl_multi_val from "str".
3317 __isl_give isl_multi_val
*isl_multi_val_read_from_str(isl_ctx
*ctx
,
3321 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3324 mv
= isl_stream_read_multi_val(s
);
3329 /* Read a multi-affine expression from "s".
3330 * If the multi-affine expression has a domain, then the tuple
3331 * representing this domain cannot involve any affine expressions.
3332 * The tuple representing the actual expressions needs to consist
3333 * of only affine expressions. Moreover, these expressions can
3334 * only depend on parameters and input dimensions and not on other
3335 * output dimensions.
3337 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(__isl_keep isl_stream
*s
)
3340 isl_set
*dom
= NULL
;
3341 isl_multi_pw_aff
*tuple
= NULL
;
3343 isl_space
*space
, *dom_space
;
3344 isl_multi_aff
*ma
= NULL
;
3346 v
= vars_new(s
->ctx
);
3350 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3351 if (next_is_tuple(s
)) {
3352 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3353 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3356 if (!isl_set_plain_is_universe(dom
))
3357 isl_die(s
->ctx
, isl_error_invalid
,
3358 "expecting universe parameter domain", goto error
);
3359 if (isl_stream_eat(s
, '{'))
3362 tuple
= read_tuple(s
, v
, 0, 0);
3365 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3370 has_expr
= tuple_has_expr(tuple
);
3374 isl_die(s
->ctx
, isl_error_invalid
,
3375 "expecting universe domain", goto error
);
3376 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3377 set
= isl_set_universe(space
);
3378 dom
= isl_set_intersect_params(set
, dom
);
3379 isl_multi_pw_aff_free(tuple
);
3380 tuple
= read_tuple(s
, v
, 0, 0);
3385 if (isl_stream_eat(s
, '}'))
3388 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3389 dim
= isl_set_dim(dom
, isl_dim_all
);
3390 dom_space
= isl_set_get_space(dom
);
3391 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3392 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3393 if (!isl_space_is_params(dom_space
))
3394 space
= isl_space_map_from_domain_and_range(
3395 isl_space_copy(dom_space
), space
);
3396 isl_space_free(dom_space
);
3397 ma
= isl_multi_aff_alloc(space
);
3399 for (i
= 0; i
< n
; ++i
) {
3402 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3403 aff
= aff_from_pw_aff(pa
);
3406 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3408 isl_die(s
->ctx
, isl_error_invalid
,
3409 "not an affine expression", goto error
);
3411 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3412 space
= isl_multi_aff_get_domain_space(ma
);
3413 aff
= isl_aff_reset_domain_space(aff
, space
);
3414 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3417 isl_multi_pw_aff_free(tuple
);
3422 isl_multi_pw_aff_free(tuple
);
3425 isl_multi_aff_free(ma
);
3429 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3432 isl_multi_aff
*maff
;
3433 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3436 maff
= isl_stream_read_multi_aff(s
);
3441 /* Read an isl_multi_pw_aff from "s".
3443 * The input format is similar to that of map, except that any conditions
3444 * on the domains should be specified inside the tuple since each
3445 * piecewise affine expression may have a different domain.
3447 * Since we do not know in advance if the isl_multi_pw_aff lives
3448 * in a set or a map space, we first read the first tuple and check
3449 * if it is followed by a "->". If so, we convert the tuple into
3450 * the domain of the isl_multi_pw_aff and read in the next tuple.
3451 * This tuple (or the first tuple if it was not followed by a "->")
3452 * is then converted into the isl_multi_pw_aff.
3454 * Note that the function read_tuple accepts tuples where some output or
3455 * set dimensions are defined in terms of other output or set dimensions
3456 * since this function is also used to read maps. As a special case,
3457 * read_tuple also accept dimensions that are defined in terms of themselves
3458 * (i.e., that are not defined).
3459 * These cases are not allowed when reading am isl_multi_pw_aff so we check
3460 * that the definition of the output/set dimensions does not involve any
3461 * output/set dimensions.
3462 * We then drop the output dimensions from the domain of the result
3463 * of read_tuple (which is of the form [input, output] -> [output],
3464 * with anonymous domain) and reset the space.
3466 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(
3467 __isl_keep isl_stream
*s
)
3470 isl_set
*dom
= NULL
;
3471 isl_multi_pw_aff
*tuple
= NULL
;
3473 isl_space
*space
, *dom_space
;
3474 isl_multi_pw_aff
*mpa
= NULL
;
3476 v
= vars_new(s
->ctx
);
3480 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3481 if (next_is_tuple(s
)) {
3482 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3483 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3486 if (isl_stream_eat(s
, '{'))
3489 tuple
= read_tuple(s
, v
, 0, 0);
3492 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3493 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3494 dom
= isl_map_domain(map
);
3495 tuple
= read_tuple(s
, v
, 0, 0);
3500 if (isl_stream_eat(s
, '}'))
3503 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3504 dim
= isl_set_dim(dom
, isl_dim_all
);
3505 dom_space
= isl_set_get_space(dom
);
3506 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3507 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3508 if (!isl_space_is_params(dom_space
))
3509 space
= isl_space_map_from_domain_and_range(
3510 isl_space_copy(dom_space
), space
);
3511 isl_space_free(dom_space
);
3512 mpa
= isl_multi_pw_aff_alloc(space
);
3514 for (i
= 0; i
< n
; ++i
) {
3516 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3519 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3520 isl_pw_aff_free(pa
);
3521 isl_die(s
->ctx
, isl_error_invalid
,
3522 "not an affine expression", goto error
);
3524 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3525 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3526 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3527 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3530 isl_multi_pw_aff_free(tuple
);
3532 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3535 isl_multi_pw_aff_free(tuple
);
3538 isl_multi_pw_aff_free(mpa
);
3542 /* Read an isl_multi_pw_aff from "str".
3544 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3547 isl_multi_pw_aff
*mpa
;
3548 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3551 mpa
= isl_stream_read_multi_pw_aff(s
);
3556 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3558 static __isl_give isl_union_pw_aff
*read_union_pw_aff_with_dom(
3559 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3562 isl_union_pw_aff
*upa
= NULL
;
3567 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3568 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3569 vars_drop(v
, v
->n
- n
);
3571 upa
= isl_union_pw_aff_from_pw_aff(pa
);
3573 while (isl_stream_eat_if_available(s
, ';')) {
3575 isl_union_pw_aff
*upa_i
;
3578 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3579 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3580 vars_drop(v
, v
->n
- n
);
3582 upa_i
= isl_union_pw_aff_from_pw_aff(pa_i
);
3583 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
3590 /* This function is called for each element in a tuple inside
3591 * isl_stream_read_multi_union_pw_aff.
3593 * Read a '{', the union piecewise affine expression body and a '}' and
3594 * add the isl_union_pw_aff to *list.
3596 static __isl_give isl_space
*read_union_pw_aff_el(__isl_keep isl_stream
*s
,
3597 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3600 isl_union_pw_aff
*upa
;
3601 isl_union_pw_aff_list
**list
= (isl_union_pw_aff_list
**) user
;
3603 dom
= isl_set_universe(isl_space_params(isl_space_copy(space
)));
3604 if (isl_stream_eat(s
, '{'))
3606 upa
= read_union_pw_aff_with_dom(s
, dom
, v
);
3607 *list
= isl_union_pw_aff_list_add(*list
, upa
);
3608 if (isl_stream_eat(s
, '}'))
3609 return isl_space_free(space
);
3611 return isl_space_free(space
);
3615 return isl_space_free(space
);
3618 /* Do the next tokens in "s" correspond to an empty tuple?
3619 * In particular, does the stream start with a '[', followed by a ']',
3620 * not followed by a "->"?
3622 static int next_is_empty_tuple(__isl_keep isl_stream
*s
)
3624 struct isl_token
*tok
, *tok2
, *tok3
;
3625 int is_empty_tuple
= 0;
3627 tok
= isl_stream_next_token(s
);
3630 if (tok
->type
!= '[') {
3631 isl_stream_push_token(s
, tok
);
3635 tok2
= isl_stream_next_token(s
);
3636 if (tok2
&& tok2
->type
== ']') {
3637 tok3
= isl_stream_next_token(s
);
3638 is_empty_tuple
= !tok
|| tok
->type
!= ISL_TOKEN_TO
;
3640 isl_stream_push_token(s
, tok3
);
3643 isl_stream_push_token(s
, tok2
);
3644 isl_stream_push_token(s
, tok
);
3646 return is_empty_tuple
;
3649 /* Do the next tokens in "s" correspond to a tuple of parameters?
3650 * In particular, does the stream start with a '[' that is not
3651 * followed by a '{' or a nested tuple?
3653 static int next_is_param_tuple(__isl_keep isl_stream
*s
)
3655 struct isl_token
*tok
, *tok2
;
3658 tok
= isl_stream_next_token(s
);
3661 if (tok
->type
!= '[' || next_is_tuple(s
)) {
3662 isl_stream_push_token(s
, tok
);
3666 tok2
= isl_stream_next_token(s
);
3667 is_tuple
= tok2
&& tok2
->type
!= '{';
3669 isl_stream_push_token(s
, tok2
);
3670 isl_stream_push_token(s
, tok
);
3675 /* Read an isl_multi_union_pw_aff from "s".
3677 * The input has the form
3679 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3683 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3685 * We first check for the special case of an empty tuple "[]".
3686 * Then we check if there are any parameters.
3687 * Finally, we read the tuple, collecting the individual isl_union_pw_aff
3688 * elements in a list and construct the result from the tuple space and
3691 __isl_give isl_multi_union_pw_aff
*isl_stream_read_multi_union_pw_aff(
3692 __isl_keep isl_stream
*s
)
3695 isl_set
*dom
= NULL
;
3697 isl_multi_union_pw_aff
*mupa
= NULL
;
3698 isl_union_pw_aff_list
*list
;
3700 if (next_is_empty_tuple(s
)) {
3701 if (isl_stream_eat(s
, '['))
3703 if (isl_stream_eat(s
, ']'))
3705 space
= isl_space_set_alloc(s
->ctx
, 0, 0);
3706 return isl_multi_union_pw_aff_zero(space
);
3709 v
= vars_new(s
->ctx
);
3713 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3714 if (next_is_param_tuple(s
)) {
3715 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3716 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3719 space
= isl_set_get_space(dom
);
3721 list
= isl_union_pw_aff_list_alloc(s
->ctx
, 0);
3722 space
= read_tuple_space(s
, v
, space
, 1, 0,
3723 &read_union_pw_aff_el
, &list
);
3724 mupa
= isl_multi_union_pw_aff_from_union_pw_aff_list(space
, list
);
3732 isl_multi_union_pw_aff_free(mupa
);
3736 /* Read an isl_multi_union_pw_aff from "str".
3738 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_read_from_str(
3739 isl_ctx
*ctx
, const char *str
)
3741 isl_multi_union_pw_aff
*mupa
;
3742 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3745 mupa
= isl_stream_read_multi_union_pw_aff(s
);
3750 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3751 __isl_keep isl_stream
*s
)
3756 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3757 obj
.type
= isl_obj_union_pw_qpolynomial
;
3758 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3761 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3766 obj
.type
->free(obj
.v
);
3770 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3771 isl_ctx
*ctx
, const char *str
)
3773 isl_union_pw_qpolynomial
*upwqp
;
3774 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3777 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);