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
*dim
, struct vars
*v
);
312 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
313 __isl_take isl_space
*dim
, struct vars
*v
);
315 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
316 __isl_take isl_space
*dim
, struct vars
*v
)
318 struct isl_token
*tok
;
319 isl_pw_aff_list
*list
= NULL
;
322 tok
= isl_stream_next_token(s
);
325 min
= tok
->type
== ISL_TOKEN_MIN
;
328 if (isl_stream_eat(s
, '('))
331 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
335 if (isl_stream_eat(s
, ')'))
339 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
342 isl_pw_aff_list_free(list
);
346 /* Is "tok" the start of an integer division?
348 static int is_start_of_div(struct isl_token
*tok
)
352 if (tok
->type
== '[')
354 if (tok
->type
== ISL_TOKEN_FLOOR
)
356 if (tok
->type
== ISL_TOKEN_CEIL
)
358 if (tok
->type
== ISL_TOKEN_FLOORD
)
360 if (tok
->type
== ISL_TOKEN_CEILD
)
365 /* Read an integer division from "s" and return it as an isl_pw_aff.
367 * The integer division can be of the form
369 * [<affine expression>]
370 * floor(<affine expression>)
371 * ceil(<affine expression>)
372 * floord(<affine expression>,<denominator>)
373 * ceild(<affine expression>,<denominator>)
375 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
376 __isl_take isl_space
*dim
, struct vars
*v
)
378 struct isl_token
*tok
;
382 isl_pw_aff
*pwaff
= NULL
;
384 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
386 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
388 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
390 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
393 if (isl_stream_eat(s
, '('))
396 if (isl_stream_eat(s
, '['))
400 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
403 if (isl_stream_eat(s
, ','))
409 if (tok
->type
!= ISL_TOKEN_VALUE
) {
410 isl_stream_error(s
, tok
, "expected denominator");
411 isl_stream_push_token(s
, tok
);
414 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
419 pwaff
= isl_pw_aff_ceil(pwaff
);
421 pwaff
= isl_pw_aff_floor(pwaff
);
424 if (isl_stream_eat(s
, ')'))
427 if (isl_stream_eat(s
, ']'))
435 isl_pw_aff_free(pwaff
);
439 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
440 __isl_take isl_space
*dim
, struct vars
*v
)
442 struct isl_token
*tok
= NULL
;
443 isl_pw_aff
*res
= NULL
;
447 isl_stream_error(s
, NULL
, "unexpected EOF");
451 if (tok
->type
== ISL_TOKEN_AFF
) {
452 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
454 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
456 int pos
= vars_pos(v
, tok
->u
.s
, -1);
462 vars_drop(v
, v
->n
- n
);
463 isl_stream_error(s
, tok
, "unknown identifier");
467 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
470 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
471 res
= isl_pw_aff_from_aff(aff
);
473 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
474 if (isl_stream_eat_if_available(s
, '*')) {
475 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
476 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
480 ls
= isl_local_space_from_space(isl_space_copy(dim
));
481 aff
= isl_aff_zero_on_domain(ls
);
482 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
483 res
= isl_pw_aff_from_aff(aff
);
486 } else if (tok
->type
== '(') {
489 res
= accept_affine(s
, isl_space_copy(dim
), v
);
492 if (isl_stream_eat(s
, ')'))
494 } else if (is_start_of_div(tok
)) {
495 isl_stream_push_token(s
, tok
);
497 res
= accept_div(s
, isl_space_copy(dim
), v
);
498 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
499 isl_stream_push_token(s
, tok
);
501 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
503 isl_stream_error(s
, tok
, "expecting factor");
506 if (isl_stream_eat_if_available(s
, '%') ||
507 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
509 return affine_mod(s
, v
, res
);
511 if (isl_stream_eat_if_available(s
, '*')) {
514 isl_int_set_si(f
, 1);
515 if (accept_cst_factor(s
, &f
) < 0) {
519 res
= isl_pw_aff_scale(res
, f
);
522 if (isl_stream_eat_if_available(s
, '/')) {
525 isl_int_set_si(f
, 1);
526 if (accept_cst_factor(s
, &f
) < 0) {
530 res
= isl_pw_aff_scale_down(res
, f
);
539 isl_pw_aff_free(res
);
544 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
549 space
= isl_pw_aff_get_domain_space(pwaff
);
550 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
551 aff
= isl_aff_add_constant(aff
, v
);
553 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
556 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
557 __isl_take isl_space
*dim
, struct vars
*v
)
559 struct isl_token
*tok
= NULL
;
564 ls
= isl_local_space_from_space(isl_space_copy(dim
));
565 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
572 isl_stream_error(s
, NULL
, "unexpected EOF");
575 if (tok
->type
== '-') {
580 if (tok
->type
== '(' || is_start_of_div(tok
) ||
581 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
582 tok
->type
== ISL_TOKEN_IDENT
||
583 tok
->type
== ISL_TOKEN_AFF
) {
585 isl_stream_push_token(s
, tok
);
587 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
589 res
= isl_pw_aff_sub(res
, term
);
591 res
= isl_pw_aff_add(res
, term
);
595 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
597 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
598 if (isl_stream_eat_if_available(s
, '*') ||
599 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
601 term
= accept_affine_factor(s
,
602 isl_space_copy(dim
), v
);
603 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
604 res
= isl_pw_aff_add(res
, term
);
608 res
= add_cst(res
, tok
->u
.v
);
612 isl_stream_error(s
, tok
, "unexpected isl_token");
613 isl_stream_push_token(s
, tok
);
614 isl_pw_aff_free(res
);
621 if (tok
&& tok
->type
== '-') {
624 } else if (tok
&& tok
->type
== '+') {
627 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
628 isl_int_is_neg(tok
->u
.v
)) {
629 isl_stream_push_token(s
, tok
);
632 isl_stream_push_token(s
, tok
);
642 isl_pw_aff_free(res
);
646 static int is_comparator(struct isl_token
*tok
)
664 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
665 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
666 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
667 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
669 /* Accept a ternary operator, given the first argument.
671 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
672 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
675 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
680 if (isl_stream_eat(s
, '?'))
683 dim
= isl_space_wrap(isl_map_get_space(cond
));
684 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
688 if (isl_stream_eat(s
, ':'))
691 dim
= isl_pw_aff_get_domain_space(pwaff1
);
692 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
696 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
697 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
700 isl_pw_aff_free(pwaff1
);
701 isl_pw_aff_free(pwaff2
);
705 /* Set *line and *col to those of the next token, if any.
707 static void set_current_line_col(struct isl_stream
*s
, int *line
, int *col
)
709 struct isl_token
*tok
;
711 tok
= isl_stream_next_token(s
);
717 isl_stream_push_token(s
, tok
);
720 /* Push a token encapsulating "pa" onto "s", with the given
723 static int push_aff(struct isl_stream
*s
, int line
, int col
,
724 __isl_take isl_pw_aff
*pa
)
726 struct isl_token
*tok
;
728 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
731 tok
->type
= ISL_TOKEN_AFF
;
733 isl_stream_push_token(s
, tok
);
741 /* Accept an affine expression that may involve ternary operators.
742 * We first read an affine expression.
743 * If it is not followed by a comparison operator, we simply return it.
744 * Otherwise, we assume the affine expression is part of the first
745 * argument of a ternary operator and try to parse that.
747 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
748 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
753 struct isl_token
*tok
;
754 int line
= -1, col
= -1;
757 set_current_line_col(s
, &line
, &col
);
759 pwaff
= accept_affine(s
, dim
, v
);
761 pwaff
= isl_pw_aff_set_rational(pwaff
);
765 tok
= isl_stream_next_token(s
);
767 return isl_pw_aff_free(pwaff
);
769 is_comp
= is_comparator(tok
);
770 isl_stream_push_token(s
, tok
);
774 space
= isl_pw_aff_get_domain_space(pwaff
);
775 cond
= isl_map_universe(isl_space_unwrap(space
));
777 if (push_aff(s
, line
, col
, pwaff
) < 0)
780 cond
= read_formula(s
, v
, cond
, rational
);
782 return accept_ternary(s
, cond
, v
, rational
);
785 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
786 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
793 if (type
== isl_dim_param
)
794 pos
= isl_map_dim(map
, isl_dim_param
);
796 pos
= isl_map_dim(map
, isl_dim_in
);
797 if (type
== isl_dim_out
)
798 pos
+= isl_map_dim(map
, isl_dim_out
);
803 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
805 def_map
= isl_map_from_pw_aff(def
);
806 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
807 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
809 map
= isl_map_intersect(map
, def_map
);
814 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
815 __isl_take isl_space
*dim
, struct vars
*v
)
818 isl_pw_aff_list
*list
;
819 struct isl_token
*tok
= NULL
;
821 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
822 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
827 tok
= isl_stream_next_token(s
);
829 isl_stream_error(s
, NULL
, "unexpected EOF");
832 if (tok
->type
!= ',') {
833 isl_stream_push_token(s
, tok
);
838 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
839 list
= isl_pw_aff_list_concat(list
,
840 isl_pw_aff_list_from_pw_aff(pwaff
));
849 isl_pw_aff_list_free(list
);
853 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
854 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
856 struct isl_token
*tok
;
858 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
862 if (tok
->type
!= ISL_TOKEN_IDENT
)
865 p
= vars_pos(v
, tok
->u
.s
, -1);
869 isl_stream_error(s
, tok
, "expecting unique identifier");
873 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
876 tok
= isl_stream_next_token(s
);
877 if (tok
&& tok
->type
== '=') {
879 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
880 tok
= isl_stream_next_token(s
);
883 if (!tok
|| tok
->type
!= ',')
889 isl_stream_push_token(s
, tok
);
898 static int next_is_tuple(struct isl_stream
*s
)
900 struct isl_token
*tok
;
903 tok
= isl_stream_next_token(s
);
906 if (tok
->type
== '[') {
907 isl_stream_push_token(s
, tok
);
910 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
911 isl_stream_push_token(s
, tok
);
915 is_tuple
= isl_stream_next_token_is(s
, '[');
917 isl_stream_push_token(s
, tok
);
922 /* Allocate an initial tuple with zero dimensions and an anonymous,
923 * unstructured space.
924 * A tuple is represented as an isl_multi_pw_aff.
925 * The range space is the space of the tuple.
926 * The domain space is an anonymous space
927 * with a dimension for each variable in the set of variables in "v".
928 * If a given dimension is not defined in terms of earlier dimensions in
929 * the input, then the corresponding isl_pw_aff is set equal to one time
930 * the variable corresponding to the dimension being defined.
932 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
934 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
937 /* Is "pa" an expression in term of earlier dimensions?
938 * The alternative is that the dimension is defined to be equal to itself,
939 * meaning that it has a universe domain and an expression that depends
940 * on itself. "i" is the position of the expression in a sequence
941 * of "n" expressions. The final dimensions of "pa" correspond to
942 * these "n" expressions.
944 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
952 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
956 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
961 /* Does the tuple contain any dimensions that are defined
962 * in terms of earlier dimensions?
964 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
972 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
973 for (i
= 0; i
< n
; ++i
) {
974 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
975 has_expr
= pw_aff_is_expr(pa
, i
, n
);
977 if (has_expr
< 0 || has_expr
)
984 /* Add a dimension to the given tuple.
985 * The dimension is initially undefined, so it is encoded
986 * as one times itself.
988 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
989 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
995 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
996 space
= isl_multi_pw_aff_get_domain_space(tuple
);
997 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
998 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
999 pa
= isl_pw_aff_from_aff(aff
);
1000 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
1001 isl_multi_pw_aff_from_pw_aff(pa
));
1006 /* Set the name of dimension "pos" in "tuple" to "name".
1007 * During printing, we add primes if the same name appears more than once
1008 * to distinguish the occurrences. Here, we remove those primes from "name"
1009 * before setting the name of the dimension.
1011 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
1012 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
1019 prime
= strchr(name
, '\'');
1022 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
1029 /* Accept a piecewise affine expression.
1031 * At the outer level, the piecewise affine expression may be of the form
1033 * aff1 : condition1; aff2 : conditions2; ...
1039 * each of the affine expressions may in turn include ternary operators.
1041 * There may be parentheses around some subexpression of "aff1"
1042 * around "aff1" itself, around "aff1 : condition1" and/or
1043 * around the entire piecewise affine expression.
1044 * We therefore remove the opening parenthesis (if any) from the stream
1045 * in case the closing parenthesis follows the colon, but if the closing
1046 * parenthesis is the first thing in the stream after the parsed affine
1047 * expression, we push the parsed expression onto the stream and parse
1048 * again in case the parentheses enclose some subexpression of "aff1".
1050 static __isl_give isl_pw_aff
*accept_piecewise_affine(struct isl_stream
*s
,
1051 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1054 isl_space
*res_space
;
1056 res_space
= isl_space_from_domain(isl_space_copy(space
));
1057 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1058 res
= isl_pw_aff_empty(res_space
);
1062 int line
= -1, col
= -1;
1064 set_current_line_col(s
, &line
, &col
);
1065 seen_paren
= isl_stream_eat_if_available(s
, '(');
1067 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1070 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1072 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1074 if (push_aff(s
, line
, col
, pa
) < 0)
1076 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1079 if (isl_stream_eat_if_available(s
, ':')) {
1080 isl_space
*dom_space
;
1083 dom_space
= isl_pw_aff_get_domain_space(pa
);
1084 dom
= isl_set_universe(dom_space
);
1085 dom
= read_formula(s
, v
, dom
, rational
);
1086 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1089 res
= isl_pw_aff_union_add(res
, pa
);
1091 if (seen_paren
&& isl_stream_eat(s
, ')'))
1093 } while (isl_stream_eat_if_available(s
, ';'));
1095 isl_space_free(space
);
1099 isl_space_free(space
);
1100 return isl_pw_aff_free(res
);
1103 /* Read an affine expression from "s" and replace the definition
1104 * of dimension "pos" in "tuple" by this expression.
1106 * accept_extended_affine requires a wrapped space as input.
1107 * The domain space of "tuple", on the other hand is an anonymous space,
1108 * so we have to adjust the space of the isl_pw_aff before adding it
1111 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
1112 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
1118 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1120 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1122 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1123 def
= isl_pw_aff_reset_domain_space(def
, space
);
1124 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
1129 /* Read a list of variables and/or affine expressions and return the list
1130 * as an isl_multi_pw_aff.
1131 * The elements in the list are separated by either "," or "][".
1132 * If "comma" is set then only "," is allowed.
1134 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
1135 struct vars
*v
, int rational
, int comma
)
1138 struct isl_token
*tok
;
1139 isl_multi_pw_aff
*res
;
1141 res
= tuple_alloc(v
);
1143 if (isl_stream_next_token_is(s
, ']'))
1146 while ((tok
= next_token(s
)) != NULL
) {
1149 res
= tuple_add_dim(res
, v
);
1151 if (tok
->type
== ISL_TOKEN_IDENT
) {
1153 int p
= vars_pos(v
, tok
->u
.s
, -1);
1159 if (tok
->type
== '*') {
1160 if (vars_add_anon(v
) < 0)
1162 isl_token_free(tok
);
1163 } else if (new_name
) {
1164 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
1165 isl_token_free(tok
);
1166 if (isl_stream_eat_if_available(s
, '='))
1167 res
= read_tuple_var_def(s
, res
, i
, v
,
1170 isl_stream_push_token(s
, tok
);
1172 if (vars_add_anon(v
) < 0)
1174 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
1177 tok
= isl_stream_next_token(s
);
1178 if (!comma
&& tok
&& tok
->type
== ']' &&
1179 isl_stream_next_token_is(s
, '[')) {
1180 isl_token_free(tok
);
1181 tok
= isl_stream_next_token(s
);
1182 } else if (!tok
|| tok
->type
!= ',')
1185 isl_token_free(tok
);
1189 isl_stream_push_token(s
, tok
);
1193 isl_token_free(tok
);
1194 return isl_multi_pw_aff_free(res
);
1197 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1199 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1200 struct vars
*v
, int rational
, int comma
)
1202 struct isl_token
*tok
;
1204 isl_multi_pw_aff
*res
= NULL
;
1206 tok
= isl_stream_next_token(s
);
1209 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1210 name
= strdup(tok
->u
.s
);
1211 isl_token_free(tok
);
1215 isl_stream_push_token(s
, tok
);
1216 if (isl_stream_eat(s
, '['))
1218 if (next_is_tuple(s
)) {
1219 isl_multi_pw_aff
*out
;
1221 res
= read_tuple(s
, v
, rational
, comma
);
1222 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1224 out
= read_tuple(s
, v
, rational
, comma
);
1225 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1226 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1227 res
= isl_multi_pw_aff_range_product(res
, out
);
1229 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1230 if (isl_stream_eat(s
, ']'))
1234 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1241 return isl_multi_pw_aff_free(res
);
1244 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1245 * We first create the appropriate space in "map" based on the range
1246 * space of this isl_multi_pw_aff. Then, we add equalities based
1247 * on the affine expressions. These live in an anonymous space,
1248 * however, so we first need to reset the space to that of "map".
1250 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1251 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1256 isl_space
*space
= NULL
;
1260 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1261 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1262 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1266 if (type
== isl_dim_param
) {
1267 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1268 isl_space_is_wrapping(space
)) {
1269 isl_die(ctx
, isl_error_invalid
,
1270 "parameter tuples cannot be named or nested",
1273 map
= isl_map_add_dims(map
, type
, n
);
1274 for (i
= 0; i
< n
; ++i
) {
1276 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1277 isl_die(ctx
, isl_error_invalid
,
1278 "parameters must be named",
1280 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1281 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1283 } else if (type
== isl_dim_in
) {
1286 set
= isl_set_universe(isl_space_copy(space
));
1288 set
= isl_set_set_rational(set
);
1289 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1290 map
= isl_map_from_domain(set
);
1294 set
= isl_set_universe(isl_space_copy(space
));
1296 set
= isl_set_set_rational(set
);
1297 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1300 for (i
= 0; i
< n
; ++i
) {
1307 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1308 space
= isl_pw_aff_get_domain_space(pa
);
1309 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1310 aff
= isl_aff_add_coefficient_si(aff
,
1311 isl_dim_in
, v
->n
- n
+ i
, -1);
1312 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1314 pa
= isl_pw_aff_set_rational(pa
);
1315 set
= isl_pw_aff_zero_set(pa
);
1316 map_i
= isl_map_from_range(set
);
1317 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1318 map
= isl_map_intersect(map
, map_i
);
1321 isl_space_free(space
);
1322 isl_multi_pw_aff_free(tuple
);
1325 isl_space_free(space
);
1326 isl_multi_pw_aff_free(tuple
);
1331 /* Read a tuple from "s" and add it to "map".
1332 * The tuple is initially represented as an isl_multi_pw_aff and
1333 * then added to "map".
1335 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1336 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1337 int rational
, int comma
)
1339 isl_multi_pw_aff
*tuple
;
1341 tuple
= read_tuple(s
, v
, rational
, comma
);
1343 return isl_map_free(map
);
1345 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1348 static __isl_give isl_set
*construct_constraints(
1349 __isl_take isl_set
*set
, int type
,
1350 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1355 left
= isl_pw_aff_list_copy(left
);
1356 right
= isl_pw_aff_list_copy(right
);
1358 left
= isl_pw_aff_list_set_rational(left
);
1359 right
= isl_pw_aff_list_set_rational(right
);
1361 if (type
== ISL_TOKEN_LE
)
1362 cond
= isl_pw_aff_list_le_set(left
, right
);
1363 else if (type
== ISL_TOKEN_GE
)
1364 cond
= isl_pw_aff_list_ge_set(left
, right
);
1365 else if (type
== ISL_TOKEN_LT
)
1366 cond
= isl_pw_aff_list_lt_set(left
, right
);
1367 else if (type
== ISL_TOKEN_GT
)
1368 cond
= isl_pw_aff_list_gt_set(left
, right
);
1369 else if (type
== ISL_TOKEN_NE
)
1370 cond
= isl_pw_aff_list_ne_set(left
, right
);
1372 cond
= isl_pw_aff_list_eq_set(left
, right
);
1374 return isl_set_intersect(set
, cond
);
1377 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1378 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1380 struct isl_token
*tok
= NULL
;
1381 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1384 set
= isl_map_wrap(map
);
1385 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1388 tok
= isl_stream_next_token(s
);
1389 if (!is_comparator(tok
)) {
1390 isl_stream_error(s
, tok
, "missing operator");
1392 isl_stream_push_token(s
, tok
);
1397 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1401 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1403 isl_token_free(tok
);
1404 isl_pw_aff_list_free(list1
);
1407 tok
= isl_stream_next_token(s
);
1408 if (!is_comparator(tok
)) {
1410 isl_stream_push_token(s
, tok
);
1414 isl_pw_aff_list_free(list1
);
1416 return isl_set_unwrap(set
);
1419 isl_token_free(tok
);
1420 isl_pw_aff_list_free(list1
);
1421 isl_pw_aff_list_free(list2
);
1426 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1427 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1430 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1432 map
= isl_map_from_domain(isl_map_wrap(map
));
1433 map
= read_defined_var_list(s
, v
, map
, rational
);
1435 if (isl_stream_eat(s
, ':'))
1438 map
= read_formula(s
, v
, map
, rational
);
1439 map
= isl_set_unwrap(isl_map_domain(map
));
1441 vars_drop(v
, v
->n
- n
);
1442 if (seen_paren
&& isl_stream_eat(s
, ')'))
1451 /* Parse an expression between parentheses and push the result
1452 * back on the stream.
1454 * The parsed expression may be either an affine expression
1455 * or a condition. The first type is pushed onto the stream
1456 * as an isl_pw_aff, while the second is pushed as an isl_map.
1458 * If the initial token indicates the start of a condition,
1459 * we parse it as such.
1460 * Otherwise, we first parse an affine expression and push
1461 * that onto the stream. If the affine expression covers the
1462 * entire expression between parentheses, we return.
1463 * Otherwise, we assume that the affine expression is the
1464 * start of a condition and continue parsing.
1466 static int resolve_paren_expr(struct isl_stream
*s
,
1467 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1469 struct isl_token
*tok
, *tok2
;
1473 tok
= isl_stream_next_token(s
);
1474 if (!tok
|| tok
->type
!= '(')
1477 if (isl_stream_next_token_is(s
, '('))
1478 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1481 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1482 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1483 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1484 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1485 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1486 map
= read_formula(s
, v
, map
, rational
);
1487 if (isl_stream_eat(s
, ')'))
1489 tok
->type
= ISL_TOKEN_MAP
;
1491 isl_stream_push_token(s
, tok
);
1495 tok2
= isl_stream_next_token(s
);
1500 isl_stream_push_token(s
, tok2
);
1502 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1506 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1509 tok2
->type
= ISL_TOKEN_AFF
;
1510 tok2
->u
.pwaff
= pwaff
;
1512 if (isl_stream_eat_if_available(s
, ')')) {
1513 isl_stream_push_token(s
, tok2
);
1514 isl_token_free(tok
);
1519 isl_stream_push_token(s
, tok2
);
1521 map
= read_formula(s
, v
, map
, rational
);
1522 if (isl_stream_eat(s
, ')'))
1525 tok
->type
= ISL_TOKEN_MAP
;
1527 isl_stream_push_token(s
, tok
);
1531 isl_pw_aff_free(pwaff
);
1533 isl_token_free(tok
);
1538 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1539 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1541 if (isl_stream_next_token_is(s
, '('))
1542 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1545 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1546 struct isl_token
*tok
;
1547 tok
= isl_stream_next_token(s
);
1551 map
= isl_map_copy(tok
->u
.map
);
1552 isl_token_free(tok
);
1556 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1557 return read_exists(s
, v
, map
, rational
);
1559 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1562 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1563 isl_space
*dim
= isl_map_get_space(map
);
1565 return isl_map_empty(dim
);
1568 return add_constraint(s
, v
, map
, rational
);
1574 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1575 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1580 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1581 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1583 res
= isl_map_subtract(isl_map_copy(map
), res
);
1585 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1588 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1589 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1591 res
= isl_map_subtract(res
, res_i
);
1593 res
= isl_map_intersect(res
, res_i
);
1600 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1601 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1605 if (isl_stream_next_token_is(s
, '}')) {
1606 isl_space
*dim
= isl_map_get_space(map
);
1608 return isl_map_universe(dim
);
1611 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1612 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1615 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1616 res
= isl_map_union(res
, res_i
);
1623 /* Read a first order formula from "s", add the corresponding
1624 * constraints to "map" and return the result.
1626 * In particular, read a formula of the form
1634 * where a and b are disjunctions.
1636 * In the first case, map is replaced by
1638 * map \cap { [..] : a }
1640 * In the second case, it is replaced by
1642 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1644 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
1645 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1649 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1651 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1654 res
= isl_map_subtract(isl_map_copy(map
), res
);
1655 res2
= read_disjuncts(s
, v
, map
, rational
);
1656 res
= isl_map_union(res
, res2
);
1663 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1665 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1666 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1667 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1668 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1670 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1671 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1672 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1674 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1675 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1676 isl_basic_map_dim(bmap
, isl_dim_in
) +
1677 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1678 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1680 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1686 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1687 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1690 struct isl_token
*tok
;
1700 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1701 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1703 tok
= isl_stream_next_token(s
);
1704 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1705 isl_stream_error(s
, tok
, "expecting coefficient");
1707 isl_stream_push_token(s
, tok
);
1710 if (!tok
->on_new_line
) {
1711 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1712 isl_stream_push_token(s
, tok
);
1716 type
= isl_int_get_si(tok
->u
.v
);
1717 isl_token_free(tok
);
1719 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1721 k
= isl_basic_map_alloc_equality(bmap
);
1724 k
= isl_basic_map_alloc_inequality(bmap
);
1730 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1732 tok
= isl_stream_next_token(s
);
1733 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1734 isl_stream_error(s
, tok
, "expecting coefficient");
1736 isl_stream_push_token(s
, tok
);
1739 if (tok
->on_new_line
) {
1740 isl_stream_error(s
, tok
,
1741 "coefficient should not appear on new line");
1742 isl_stream_push_token(s
, tok
);
1745 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1746 isl_int_set(c
[pos
], tok
->u
.v
);
1747 isl_token_free(tok
);
1752 isl_basic_map_free(bmap
);
1756 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1759 struct isl_token
*tok
;
1760 struct isl_token
*tok2
;
1763 unsigned in
= 0, out
, local
= 0;
1764 struct isl_basic_map
*bmap
= NULL
;
1767 tok
= isl_stream_next_token(s
);
1769 isl_stream_error(s
, NULL
, "unexpected EOF");
1772 tok2
= isl_stream_next_token(s
);
1774 isl_token_free(tok
);
1775 isl_stream_error(s
, NULL
, "unexpected EOF");
1778 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1779 isl_stream_push_token(s
, tok2
);
1780 isl_stream_push_token(s
, tok
);
1781 isl_stream_error(s
, NULL
,
1782 "expecting constraint matrix dimensions");
1785 n_row
= isl_int_get_si(tok
->u
.v
);
1786 n_col
= isl_int_get_si(tok2
->u
.v
);
1787 on_new_line
= tok2
->on_new_line
;
1788 isl_token_free(tok2
);
1789 isl_token_free(tok
);
1790 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1791 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1792 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1793 tok
= isl_stream_next_token_on_same_line(s
);
1795 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1796 isl_stream_error(s
, tok
,
1797 "expecting number of output dimensions");
1798 isl_stream_push_token(s
, tok
);
1801 out
= isl_int_get_si(tok
->u
.v
);
1802 isl_token_free(tok
);
1804 tok
= isl_stream_next_token_on_same_line(s
);
1805 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1806 isl_stream_error(s
, tok
,
1807 "expecting number of input dimensions");
1809 isl_stream_push_token(s
, tok
);
1812 in
= isl_int_get_si(tok
->u
.v
);
1813 isl_token_free(tok
);
1815 tok
= isl_stream_next_token_on_same_line(s
);
1816 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1817 isl_stream_error(s
, tok
,
1818 "expecting number of existentials");
1820 isl_stream_push_token(s
, tok
);
1823 local
= isl_int_get_si(tok
->u
.v
);
1824 isl_token_free(tok
);
1826 tok
= isl_stream_next_token_on_same_line(s
);
1827 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1828 isl_stream_error(s
, tok
,
1829 "expecting number of parameters");
1831 isl_stream_push_token(s
, tok
);
1834 nparam
= isl_int_get_si(tok
->u
.v
);
1835 isl_token_free(tok
);
1836 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1837 isl_stream_error(s
, NULL
,
1838 "dimensions don't match");
1842 out
= n_col
- 2 - nparam
;
1843 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1847 for (i
= 0; i
< local
; ++i
) {
1848 int k
= isl_basic_map_alloc_div(bmap
);
1851 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1854 for (i
= 0; i
< n_row
; ++i
)
1855 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1857 tok
= isl_stream_next_token_on_same_line(s
);
1859 isl_stream_error(s
, tok
, "unexpected extra token on line");
1860 isl_stream_push_token(s
, tok
);
1864 bmap
= isl_basic_map_simplify(bmap
);
1865 bmap
= isl_basic_map_finalize(bmap
);
1868 isl_basic_map_free(bmap
);
1872 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1874 struct isl_token
*tok
;
1875 struct isl_token
*tok2
;
1877 struct isl_map
*map
;
1879 tok
= isl_stream_next_token(s
);
1881 isl_stream_error(s
, NULL
, "unexpected EOF");
1884 tok2
= isl_stream_next_token_on_same_line(s
);
1885 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1886 isl_stream_push_token(s
, tok2
);
1887 isl_stream_push_token(s
, tok
);
1888 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1891 isl_stream_error(s
, tok2
, "unexpected token");
1892 isl_stream_push_token(s
, tok2
);
1893 isl_stream_push_token(s
, tok
);
1896 n
= isl_int_get_si(tok
->u
.v
);
1897 isl_token_free(tok
);
1899 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1901 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1903 for (i
= 1; map
&& i
< n
; ++i
)
1904 map
= isl_map_union(map
,
1905 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1910 static int optional_power(struct isl_stream
*s
)
1913 struct isl_token
*tok
;
1915 tok
= isl_stream_next_token(s
);
1918 if (tok
->type
!= '^') {
1919 isl_stream_push_token(s
, tok
);
1922 isl_token_free(tok
);
1923 tok
= isl_stream_next_token(s
);
1924 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1925 isl_stream_error(s
, tok
, "expecting exponent");
1927 isl_stream_push_token(s
, tok
);
1930 pow
= isl_int_get_si(tok
->u
.v
);
1931 isl_token_free(tok
);
1935 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1936 __isl_keep isl_map
*map
, struct vars
*v
);
1938 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1939 __isl_keep isl_map
*map
, struct vars
*v
)
1941 isl_pw_qpolynomial
*pwqp
;
1942 struct isl_token
*tok
;
1944 tok
= next_token(s
);
1946 isl_stream_error(s
, NULL
, "unexpected EOF");
1949 if (tok
->type
== '(') {
1952 isl_token_free(tok
);
1953 pwqp
= read_term(s
, map
, v
);
1956 if (isl_stream_eat(s
, ')'))
1958 pow
= optional_power(s
);
1959 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1960 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1961 struct isl_token
*tok2
;
1962 isl_qpolynomial
*qp
;
1964 tok2
= isl_stream_next_token(s
);
1965 if (tok2
&& tok2
->type
== '/') {
1966 isl_token_free(tok2
);
1967 tok2
= next_token(s
);
1968 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1969 isl_stream_error(s
, tok2
, "expected denominator");
1970 isl_token_free(tok
);
1971 isl_token_free(tok2
);
1974 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1975 tok
->u
.v
, tok2
->u
.v
);
1976 isl_token_free(tok2
);
1978 isl_stream_push_token(s
, tok2
);
1979 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1982 isl_token_free(tok
);
1983 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1984 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1985 isl_qpolynomial
*qp
;
1986 isl_token_free(tok
);
1987 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1988 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1989 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1990 isl_qpolynomial
*qp
;
1991 isl_token_free(tok
);
1992 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1993 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1994 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1996 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1998 isl_qpolynomial
*qp
;
2000 isl_token_free(tok
);
2004 vars_drop(v
, v
->n
- n
);
2005 isl_stream_error(s
, tok
, "unknown identifier");
2006 isl_token_free(tok
);
2009 isl_token_free(tok
);
2010 pow
= optional_power(s
);
2011 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2012 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2013 } else if (is_start_of_div(tok
)) {
2017 isl_stream_push_token(s
, tok
);
2018 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2019 pow
= optional_power(s
);
2020 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2021 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2022 } else if (tok
->type
== '-') {
2023 isl_token_free(tok
);
2024 pwqp
= read_factor(s
, map
, v
);
2025 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2027 isl_stream_error(s
, tok
, "unexpected isl_token");
2028 isl_stream_push_token(s
, tok
);
2032 if (isl_stream_eat_if_available(s
, '*') ||
2033 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2034 isl_pw_qpolynomial
*pwqp2
;
2036 pwqp2
= read_factor(s
, map
, v
);
2037 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2042 isl_pw_qpolynomial_free(pwqp
);
2046 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
2047 __isl_keep isl_map
*map
, struct vars
*v
)
2049 struct isl_token
*tok
;
2050 isl_pw_qpolynomial
*pwqp
;
2052 pwqp
= read_factor(s
, map
, v
);
2055 tok
= next_token(s
);
2059 if (tok
->type
== '+') {
2060 isl_pw_qpolynomial
*pwqp2
;
2062 isl_token_free(tok
);
2063 pwqp2
= read_factor(s
, map
, v
);
2064 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2065 } else if (tok
->type
== '-') {
2066 isl_pw_qpolynomial
*pwqp2
;
2068 isl_token_free(tok
);
2069 pwqp2
= read_factor(s
, map
, v
);
2070 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2071 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2072 isl_int_is_neg(tok
->u
.v
)) {
2073 isl_pw_qpolynomial
*pwqp2
;
2075 isl_stream_push_token(s
, tok
);
2076 pwqp2
= read_factor(s
, map
, v
);
2077 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2079 isl_stream_push_token(s
, tok
);
2087 static __isl_give isl_map
*read_optional_formula(struct isl_stream
*s
,
2088 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2090 struct isl_token
*tok
;
2092 tok
= isl_stream_next_token(s
);
2094 isl_stream_error(s
, NULL
, "unexpected EOF");
2097 if (tok
->type
== ':' ||
2098 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2099 isl_token_free(tok
);
2100 map
= read_formula(s
, v
, map
, rational
);
2102 isl_stream_push_token(s
, tok
);
2110 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
2111 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2113 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2114 isl_pw_qpolynomial
*pwqp
;
2115 struct isl_set
*set
;
2117 pwqp
= read_term(s
, map
, v
);
2118 map
= read_optional_formula(s
, map
, v
, 0);
2119 set
= isl_map_range(map
);
2121 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2123 vars_drop(v
, v
->n
- n
);
2129 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
2130 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2132 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2133 isl_pw_qpolynomial
*pwqp
;
2134 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2136 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2137 return obj_read_poly(s
, set
, v
, n
);
2139 if (isl_stream_eat(s
, '('))
2142 pwqp
= read_term(s
, set
, v
);
2143 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2145 while (isl_stream_eat_if_available(s
, ',')) {
2146 isl_pw_qpolynomial_fold
*pwf_i
;
2147 pwqp
= read_term(s
, set
, v
);
2148 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2150 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2153 if (isl_stream_eat(s
, ')'))
2156 set
= read_optional_formula(s
, set
, v
, 0);
2157 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2159 vars_drop(v
, v
->n
- n
);
2165 isl_pw_qpolynomial_fold_free(pwf
);
2166 obj
.type
= isl_obj_none
;
2170 static int is_rational(struct isl_stream
*s
)
2172 struct isl_token
*tok
;
2174 tok
= isl_stream_next_token(s
);
2177 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2178 isl_token_free(tok
);
2179 isl_stream_eat(s
, ':');
2183 isl_stream_push_token(s
, tok
);
2188 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2189 __isl_take isl_map
*map
, struct vars
*v
)
2191 struct isl_token
*tok
;
2192 struct isl_obj obj
= { isl_obj_set
, NULL
};
2196 rational
= is_rational(s
);
2198 map
= isl_map_set_rational(map
);
2200 if (isl_stream_next_token_is(s
, ':')) {
2201 obj
.type
= isl_obj_set
;
2202 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2206 if (!next_is_tuple(s
))
2207 return obj_read_poly_or_fold(s
, map
, v
, n
);
2209 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2212 tok
= isl_stream_next_token(s
);
2215 if (tok
->type
== ISL_TOKEN_TO
) {
2216 obj
.type
= isl_obj_map
;
2217 isl_token_free(tok
);
2218 if (!next_is_tuple(s
)) {
2219 isl_set
*set
= isl_map_domain(map
);
2220 return obj_read_poly_or_fold(s
, set
, v
, n
);
2222 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2226 map
= isl_map_domain(map
);
2227 isl_stream_push_token(s
, tok
);
2230 map
= read_optional_formula(s
, map
, v
, rational
);
2232 vars_drop(v
, v
->n
- n
);
2238 obj
.type
= isl_obj_none
;
2242 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2244 if (obj
.type
== isl_obj_map
) {
2245 obj
.v
= isl_union_map_from_map(obj
.v
);
2246 obj
.type
= isl_obj_union_map
;
2247 } else if (obj
.type
== isl_obj_set
) {
2248 obj
.v
= isl_union_set_from_set(obj
.v
);
2249 obj
.type
= isl_obj_union_set
;
2250 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2251 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2252 obj
.type
= isl_obj_union_pw_qpolynomial
;
2253 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2254 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2255 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2257 isl_assert(ctx
, 0, goto error
);
2260 obj
.type
->free(obj
.v
);
2261 obj
.type
= isl_obj_none
;
2265 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2266 struct isl_obj obj1
, struct isl_obj obj2
)
2268 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2269 obj1
= to_union(ctx
, obj1
);
2270 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2271 obj2
= to_union(ctx
, obj2
);
2272 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2273 obj1
= to_union(ctx
, obj1
);
2274 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2275 obj2
= to_union(ctx
, obj2
);
2276 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2277 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2278 obj1
= to_union(ctx
, obj1
);
2279 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2280 obj2
.type
== isl_obj_pw_qpolynomial
)
2281 obj2
= to_union(ctx
, obj2
);
2282 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2283 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2284 obj1
= to_union(ctx
, obj1
);
2285 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2286 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2287 obj2
= to_union(ctx
, obj2
);
2288 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2289 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2290 obj1
= to_union(ctx
, obj1
);
2291 obj2
= to_union(ctx
, obj2
);
2293 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2294 obj1
= to_union(ctx
, obj1
);
2295 obj2
= to_union(ctx
, obj2
);
2297 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2298 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2299 obj1
= to_union(ctx
, obj1
);
2300 obj2
= to_union(ctx
, obj2
);
2302 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2303 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2304 obj1
= to_union(ctx
, obj1
);
2305 obj2
= to_union(ctx
, obj2
);
2307 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2310 obj1
.type
->free(obj1
.v
);
2311 obj2
.type
->free(obj2
.v
);
2312 obj1
.type
= isl_obj_none
;
2317 static struct isl_obj
obj_read(struct isl_stream
*s
)
2319 isl_map
*map
= NULL
;
2320 struct isl_token
*tok
;
2321 struct vars
*v
= NULL
;
2322 struct isl_obj obj
= { isl_obj_set
, NULL
};
2324 tok
= next_token(s
);
2326 isl_stream_error(s
, NULL
, "unexpected EOF");
2329 if (tok
->type
== ISL_TOKEN_VALUE
) {
2330 struct isl_token
*tok2
;
2331 struct isl_map
*map
;
2333 tok2
= isl_stream_next_token(s
);
2334 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2335 isl_int_is_neg(tok2
->u
.v
)) {
2337 isl_stream_push_token(s
, tok2
);
2338 obj
.type
= isl_obj_val
;
2339 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2340 isl_token_free(tok
);
2343 isl_stream_push_token(s
, tok2
);
2344 isl_stream_push_token(s
, tok
);
2345 map
= map_read_polylib(s
);
2348 if (isl_map_may_be_set(map
))
2349 obj
.v
= isl_map_range(map
);
2351 obj
.type
= isl_obj_map
;
2356 v
= vars_new(s
->ctx
);
2358 isl_stream_push_token(s
, tok
);
2361 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2362 if (tok
->type
== '[') {
2363 isl_stream_push_token(s
, tok
);
2364 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2367 tok
= isl_stream_next_token(s
);
2368 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2369 isl_stream_error(s
, tok
, "expecting '->'");
2371 isl_stream_push_token(s
, tok
);
2374 isl_token_free(tok
);
2375 tok
= isl_stream_next_token(s
);
2377 if (!tok
|| tok
->type
!= '{') {
2378 isl_stream_error(s
, tok
, "expecting '{'");
2380 isl_stream_push_token(s
, tok
);
2383 isl_token_free(tok
);
2385 tok
= isl_stream_next_token(s
);
2388 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2389 isl_token_free(tok
);
2390 if (isl_stream_eat(s
, '='))
2392 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2395 } else if (tok
->type
== '}') {
2396 obj
.type
= isl_obj_union_set
;
2397 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2398 isl_token_free(tok
);
2401 isl_stream_push_token(s
, tok
);
2406 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2407 if (o
.type
== isl_obj_none
|| !o
.v
)
2412 obj
= obj_add(s
->ctx
, obj
, o
);
2413 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2416 tok
= isl_stream_next_token(s
);
2417 if (!tok
|| tok
->type
!= ';')
2419 isl_token_free(tok
);
2420 if (isl_stream_next_token_is(s
, '}')) {
2421 tok
= isl_stream_next_token(s
);
2426 if (tok
&& tok
->type
== '}') {
2427 isl_token_free(tok
);
2429 isl_stream_error(s
, tok
, "unexpected isl_token");
2431 isl_token_free(tok
);
2441 obj
.type
->free(obj
.v
);
2448 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2453 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2459 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2460 obj
.type
== isl_obj_set
, goto error
);
2462 if (obj
.type
== isl_obj_set
)
2463 obj
.v
= isl_map_from_range(obj
.v
);
2467 obj
.type
->free(obj
.v
);
2471 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2477 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2478 obj
.v
= isl_map_range(obj
.v
);
2479 obj
.type
= isl_obj_set
;
2481 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2486 obj
.type
->free(obj
.v
);
2490 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2495 if (obj
.type
== isl_obj_map
) {
2496 obj
.type
= isl_obj_union_map
;
2497 obj
.v
= isl_union_map_from_map(obj
.v
);
2499 if (obj
.type
== isl_obj_set
) {
2500 obj
.type
= isl_obj_union_set
;
2501 obj
.v
= isl_union_set_from_set(obj
.v
);
2503 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2504 isl_union_set_is_empty(obj
.v
))
2505 obj
.type
= isl_obj_union_map
;
2506 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2507 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2511 obj
.type
->free(obj
.v
);
2515 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2520 if (obj
.type
== isl_obj_set
) {
2521 obj
.type
= isl_obj_union_set
;
2522 obj
.v
= isl_union_set_from_set(obj
.v
);
2525 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2529 obj
.type
->free(obj
.v
);
2533 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2536 struct isl_map
*map
;
2537 struct isl_basic_map
*bmap
;
2544 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2547 bmap
= isl_basic_map_empty_like_map(map
);
2549 bmap
= isl_basic_map_copy(map
->p
[0]);
2559 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2561 isl_basic_map
*bmap
;
2562 bmap
= basic_map_read(s
);
2565 if (!isl_basic_map_may_be_set(bmap
))
2566 isl_die(s
->ctx
, isl_error_invalid
,
2567 "input is not a set", goto error
);
2568 return isl_basic_map_range(bmap
);
2570 isl_basic_map_free(bmap
);
2574 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2577 struct isl_basic_map
*bmap
;
2578 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2581 bmap
= basic_map_read(s
);
2586 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2589 isl_basic_set
*bset
;
2590 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2593 bset
= basic_set_read(s
);
2598 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2601 struct isl_basic_map
*bmap
;
2602 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2605 bmap
= basic_map_read(s
);
2610 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2613 isl_basic_set
*bset
;
2614 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2617 bset
= basic_set_read(s
);
2622 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2625 struct isl_map
*map
;
2626 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2629 map
= isl_stream_read_map(s
);
2634 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2637 struct isl_map
*map
;
2638 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2641 map
= isl_stream_read_map(s
);
2646 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2650 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2653 set
= isl_stream_read_set(s
);
2658 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2662 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2665 set
= isl_stream_read_set(s
);
2670 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2673 isl_union_map
*umap
;
2674 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2677 umap
= isl_stream_read_union_map(s
);
2682 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2685 isl_union_map
*umap
;
2686 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2689 umap
= isl_stream_read_union_map(s
);
2694 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2697 isl_union_set
*uset
;
2698 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2701 uset
= isl_stream_read_union_set(s
);
2706 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2709 isl_union_set
*uset
;
2710 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2713 uset
= isl_stream_read_union_set(s
);
2718 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2720 struct isl_vec
*vec
= NULL
;
2721 struct isl_token
*tok
;
2725 tok
= isl_stream_next_token(s
);
2726 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2727 isl_stream_error(s
, tok
, "expecting vector length");
2731 size
= isl_int_get_si(tok
->u
.v
);
2732 isl_token_free(tok
);
2734 vec
= isl_vec_alloc(s
->ctx
, size
);
2736 for (j
= 0; j
< size
; ++j
) {
2737 tok
= isl_stream_next_token(s
);
2738 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2739 isl_stream_error(s
, tok
, "expecting constant value");
2742 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2743 isl_token_free(tok
);
2748 isl_token_free(tok
);
2753 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2755 return isl_vec_read_polylib(s
);
2758 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2761 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2769 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2770 struct isl_stream
*s
)
2776 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2781 obj
.type
->free(obj
.v
);
2785 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2788 isl_pw_qpolynomial
*pwqp
;
2789 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2792 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2797 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2800 isl_pw_qpolynomial
*pwqp
;
2801 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2804 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2809 /* Is the next token an identifer not in "v"?
2811 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2815 struct isl_token
*tok
;
2817 tok
= isl_stream_next_token(s
);
2820 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2821 isl_stream_push_token(s
, tok
);
2823 vars_drop(v
, v
->n
- n
);
2828 /* First read the domain of the affine expression, which may be
2829 * a parameter space or a set.
2830 * The tricky part is that we don't know if the domain is a set or not,
2831 * so when we are trying to read the domain, we may actually be reading
2832 * the affine expression itself (defined on a parameter domains)
2833 * If the tuple we are reading is named, we assume it's the domain.
2834 * Also, if inside the tuple, the first thing we find is a nested tuple
2835 * or a new identifier, we again assume it's the domain.
2836 * Otherwise, we assume we are reading an affine expression.
2838 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2839 __isl_take isl_set
*dom
, struct vars
*v
)
2841 struct isl_token
*tok
;
2843 tok
= isl_stream_next_token(s
);
2844 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2845 isl_stream_push_token(s
, tok
);
2846 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2848 if (!tok
|| tok
->type
!= '[') {
2849 isl_stream_error(s
, tok
, "expecting '['");
2852 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2853 isl_stream_push_token(s
, tok
);
2854 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2856 isl_stream_push_token(s
, tok
);
2861 isl_stream_push_token(s
, tok
);
2866 /* Read an affine expression from "s".
2868 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2873 ma
= isl_stream_read_multi_aff(s
);
2876 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2877 isl_die(s
->ctx
, isl_error_invalid
,
2878 "expecting single affine expression",
2881 aff
= isl_multi_aff_get_aff(ma
, 0);
2882 isl_multi_aff_free(ma
);
2885 isl_multi_aff_free(ma
);
2889 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2891 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2892 __isl_take isl_set
*dom
, struct vars
*v
)
2894 isl_pw_aff
*pwaff
= NULL
;
2896 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2899 if (isl_stream_eat(s
, '['))
2902 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2904 if (isl_stream_eat(s
, ']'))
2907 dom
= read_optional_formula(s
, dom
, v
, 0);
2908 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2913 isl_pw_aff_free(pwaff
);
2917 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2920 isl_set
*dom
= NULL
;
2922 isl_pw_aff
*pa
= NULL
;
2925 v
= vars_new(s
->ctx
);
2929 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2930 if (next_is_tuple(s
)) {
2931 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2932 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2935 if (isl_stream_eat(s
, '{'))
2939 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2940 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2941 vars_drop(v
, v
->n
- n
);
2943 while (isl_stream_eat_if_available(s
, ';')) {
2947 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2948 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2949 vars_drop(v
, v
->n
- n
);
2951 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2954 if (isl_stream_eat(s
, '}'))
2963 isl_pw_aff_free(pa
);
2967 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2970 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2973 aff
= isl_stream_read_aff(s
);
2978 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2981 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2984 pa
= isl_stream_read_pw_aff(s
);
2989 /* Read an isl_pw_multi_aff from "s".
2990 * We currently read a generic object and if it turns out to be a set or
2991 * a map, we convert that to an isl_pw_multi_aff.
2992 * It would be more efficient if we were to construct the isl_pw_multi_aff
2995 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
3003 if (obj
.type
== isl_obj_map
)
3004 return isl_pw_multi_aff_from_map(obj
.v
);
3005 if (obj
.type
== isl_obj_set
)
3006 return isl_pw_multi_aff_from_set(obj
.v
);
3008 obj
.type
->free(obj
.v
);
3009 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3013 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3016 isl_pw_multi_aff
*pma
;
3017 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3020 pma
= isl_stream_read_pw_multi_aff(s
);
3025 /* Read an isl_union_pw_multi_aff from "s".
3026 * We currently read a generic object and if it turns out to be a set or
3027 * a map, we convert that to an isl_union_pw_multi_aff.
3028 * It would be more efficient if we were to construct
3029 * the isl_union_pw_multi_aff directly.
3031 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3032 struct isl_stream
*s
)
3040 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3041 obj
= to_union(s
->ctx
, obj
);
3042 if (obj
.type
== isl_obj_union_map
)
3043 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3044 if (obj
.type
== isl_obj_union_set
)
3045 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3047 obj
.type
->free(obj
.v
);
3048 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3052 /* Read an isl_union_pw_multi_aff from "str".
3054 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3055 isl_ctx
*ctx
, const char *str
)
3057 isl_union_pw_multi_aff
*upma
;
3058 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3061 upma
= isl_stream_read_union_pw_multi_aff(s
);
3066 /* Assuming "pa" represents a single affine expression defined on a universe
3067 * domain, extract this affine expression.
3069 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3076 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3077 "expecting single affine expression",
3079 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3080 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3081 "expecting universe domain",
3084 aff
= isl_aff_copy(pa
->p
[0].aff
);
3085 isl_pw_aff_free(pa
);
3088 isl_pw_aff_free(pa
);
3092 /* Read a multi-affine expression from "s".
3093 * If the multi-affine expression has a domain, then then tuple
3094 * representing this domain cannot involve any affine expressions.
3095 * The tuple representing the actual expressions needs to consist
3096 * of only affine expressions. Moreover, these expressions can
3097 * only depend on parameters and input dimensions and not on other
3098 * output dimensions.
3100 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
3103 isl_set
*dom
= NULL
;
3104 isl_multi_pw_aff
*tuple
= NULL
;
3106 isl_space
*space
, *dom_space
;
3107 isl_multi_aff
*ma
= NULL
;
3109 v
= vars_new(s
->ctx
);
3113 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3114 if (next_is_tuple(s
)) {
3115 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3116 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3119 if (!isl_set_plain_is_universe(dom
))
3120 isl_die(s
->ctx
, isl_error_invalid
,
3121 "expecting universe parameter domain", goto error
);
3122 if (isl_stream_eat(s
, '{'))
3125 tuple
= read_tuple(s
, v
, 0, 0);
3128 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3133 has_expr
= tuple_has_expr(tuple
);
3137 isl_die(s
->ctx
, isl_error_invalid
,
3138 "expecting universe domain", goto error
);
3139 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3140 set
= isl_set_universe(space
);
3141 dom
= isl_set_intersect_params(set
, dom
);
3142 isl_multi_pw_aff_free(tuple
);
3143 tuple
= read_tuple(s
, v
, 0, 0);
3148 if (isl_stream_eat(s
, '}'))
3151 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3152 dim
= isl_set_dim(dom
, isl_dim_all
);
3153 dom_space
= isl_set_get_space(dom
);
3154 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3155 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3156 if (!isl_space_is_params(dom_space
))
3157 space
= isl_space_map_from_domain_and_range(
3158 isl_space_copy(dom_space
), space
);
3159 isl_space_free(dom_space
);
3160 ma
= isl_multi_aff_alloc(space
);
3162 for (i
= 0; i
< n
; ++i
) {
3165 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3166 aff
= aff_from_pw_aff(pa
);
3169 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3171 isl_die(s
->ctx
, isl_error_invalid
,
3172 "not an affine expression", goto error
);
3174 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3175 space
= isl_multi_aff_get_domain_space(ma
);
3176 aff
= isl_aff_reset_domain_space(aff
, space
);
3177 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3180 isl_multi_pw_aff_free(tuple
);
3185 isl_multi_pw_aff_free(tuple
);
3188 isl_multi_aff_free(ma
);
3192 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3195 isl_multi_aff
*maff
;
3196 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3199 maff
= isl_stream_read_multi_aff(s
);
3204 /* Read an isl_multi_pw_aff from "s".
3206 * The input format is similar to that of map, except that any conditions
3207 * on the domains should be specified inside the tuple since each
3208 * piecewise affine expression may have a different domain.
3210 * Since we do not know in advance if the isl_multi_pw_aff lives
3211 * in a set or a map space, we first read the first tuple and check
3212 * if it is followed by a "->". If so, we convert the tuple into
3213 * the domain of the isl_multi_pw_aff and read in the next tuple.
3214 * This tuple (or the first tuple if it was not followed by a "->")
3215 * is then converted into the isl_multi_pw_aff.
3217 * Note that the function read_tuple accepts tuples where some output or
3218 * set dimensions are defined in terms of other output or set dimensions
3219 * since this function is also used to read maps. As a special case,
3220 * read_tuple also accept dimensions that are defined in terms of themselves
3221 * (i.e., that are not defined).
3222 * These cases are not allowed when reading am isl_multi_pw_aff so we check
3223 * that the definition of the output/set dimensions does not involve any
3224 * output/set dimensions.
3225 * We then drop the output dimensions from the domain of the result
3226 * of read_tuple (which is of the form [input, output] -> [output],
3227 * with anonymous domain) and reset the space.
3229 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(struct isl_stream
*s
)
3232 isl_set
*dom
= NULL
;
3233 isl_multi_pw_aff
*tuple
= NULL
;
3235 isl_space
*space
, *dom_space
;
3236 isl_multi_pw_aff
*mpa
= NULL
;
3238 v
= vars_new(s
->ctx
);
3242 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3243 if (next_is_tuple(s
)) {
3244 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3245 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3248 if (isl_stream_eat(s
, '{'))
3251 tuple
= read_tuple(s
, v
, 0, 0);
3254 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3255 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3256 dom
= isl_map_domain(map
);
3257 tuple
= read_tuple(s
, v
, 0, 0);
3262 if (isl_stream_eat(s
, '}'))
3265 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3266 dim
= isl_set_dim(dom
, isl_dim_all
);
3267 dom_space
= isl_set_get_space(dom
);
3268 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3269 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3270 if (!isl_space_is_params(dom_space
))
3271 space
= isl_space_map_from_domain_and_range(
3272 isl_space_copy(dom_space
), space
);
3273 isl_space_free(dom_space
);
3274 mpa
= isl_multi_pw_aff_alloc(space
);
3276 for (i
= 0; i
< n
; ++i
) {
3278 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3281 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3282 isl_pw_aff_free(pa
);
3283 isl_die(s
->ctx
, isl_error_invalid
,
3284 "not an affine expression", goto error
);
3286 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3287 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3288 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3289 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3292 isl_multi_pw_aff_free(tuple
);
3294 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3297 isl_multi_pw_aff_free(tuple
);
3300 isl_multi_pw_aff_free(mpa
);
3304 /* Read an isl_multi_pw_aff from "str".
3306 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3309 isl_multi_pw_aff
*mpa
;
3310 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3313 mpa
= isl_stream_read_multi_pw_aff(s
);
3318 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3319 struct isl_stream
*s
)
3324 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3325 obj
.type
= isl_obj_union_pw_qpolynomial
;
3326 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3329 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3334 obj
.type
->free(obj
.v
);
3338 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3339 isl_ctx
*ctx
, const char *str
)
3341 isl_union_pw_qpolynomial
*upwqp
;
3342 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3345 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);