2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
22 #include <isl_stream_private.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_vec_private.h>
30 #include <isl_val_private.h>
35 struct variable
*next
;
44 static struct vars
*vars_new(struct isl_ctx
*ctx
)
47 v
= isl_alloc_type(ctx
, struct vars
);
56 static void variable_free(struct variable
*var
)
59 struct variable
*next
= var
->next
;
66 static void vars_free(struct vars
*v
)
74 static void vars_drop(struct vars
*v
, int n
)
85 struct variable
*next
= var
->next
;
93 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
97 var
= isl_calloc_type(v
->ctx
, struct variable
);
100 var
->name
= strdup(name
);
101 var
->name
[len
] = '\0';
110 static int vars_pos(struct vars
*v
, const char *s
, int len
)
117 for (q
= v
->v
; q
; q
= q
->next
) {
118 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
125 v
->v
= variable_new(v
, s
, len
, v
->n
);
133 static int vars_add_anon(struct vars
*v
)
135 v
->v
= variable_new(v
, "", 0, v
->n
);
144 /* Obtain next token, with some preprocessing.
145 * In particular, evaluate expressions of the form x^y,
146 * with x and y values.
148 static struct isl_token
*next_token(struct isl_stream
*s
)
150 struct isl_token
*tok
, *tok2
;
152 tok
= isl_stream_next_token(s
);
153 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
155 if (!isl_stream_eat_if_available(s
, '^'))
157 tok2
= isl_stream_next_token(s
);
158 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
159 isl_stream_error(s
, tok2
, "expecting constant value");
163 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
165 isl_token_free(tok2
);
169 isl_token_free(tok2
);
173 /* Read an isl_val from "s".
175 * The following token sequences are recognized
178 * "-" "infty" -> -infty
183 * where n, d and v are integer constants.
185 __isl_give isl_val
*isl_stream_read_val(struct isl_stream
*s
)
187 struct isl_token
*tok
= NULL
;
188 struct isl_token
*tok2
= NULL
;
193 isl_stream_error(s
, NULL
, "unexpected EOF");
196 if (tok
->type
== ISL_TOKEN_INFTY
) {
198 return isl_val_infty(s
->ctx
);
200 if (tok
->type
== '-' &&
201 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
203 return isl_val_neginfty(s
->ctx
);
205 if (tok
->type
== ISL_TOKEN_NAN
) {
207 return isl_val_nan(s
->ctx
);
209 if (tok
->type
!= ISL_TOKEN_VALUE
) {
210 isl_stream_error(s
, tok
, "expecting value");
214 if (isl_stream_eat_if_available(s
, '/')) {
215 tok2
= next_token(s
);
217 isl_stream_error(s
, NULL
, "unexpected EOF");
220 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
221 isl_stream_error(s
, tok2
, "expecting value");
224 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
225 val
= isl_val_normalize(val
);
227 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
231 isl_token_free(tok2
);
235 isl_token_free(tok2
);
239 /* Read an isl_val from "str".
241 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
245 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
248 val
= isl_stream_read_val(s
);
253 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
255 struct isl_token
*tok
;
258 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
259 isl_stream_error(s
, tok
, "expecting constant value");
263 isl_int_mul(*f
, *f
, tok
->u
.v
);
267 if (isl_stream_eat_if_available(s
, '*'))
268 return accept_cst_factor(s
, f
);
276 /* Given an affine expression aff, return an affine expression
277 * for aff % d, with d the next token on the stream, which is
278 * assumed to be a constant.
280 * We introduce an integer division q = [aff/d] and the result
281 * is set to aff - d q.
283 static __isl_give isl_pw_aff
*affine_mod(struct isl_stream
*s
,
284 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
286 struct isl_token
*tok
;
290 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
291 isl_stream_error(s
, tok
, "expecting constant value");
295 q
= isl_pw_aff_copy(aff
);
296 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
297 q
= isl_pw_aff_floor(q
);
298 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
300 aff
= isl_pw_aff_sub(aff
, q
);
305 isl_pw_aff_free(aff
);
310 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
311 __isl_take isl_space
*space
, struct vars
*v
);
312 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
313 __isl_take isl_space
*dim
, struct vars
*v
);
315 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
316 __isl_take isl_space
*dim
, struct vars
*v
)
318 struct isl_token
*tok
;
319 isl_pw_aff_list
*list
= NULL
;
322 tok
= isl_stream_next_token(s
);
325 min
= tok
->type
== ISL_TOKEN_MIN
;
328 if (isl_stream_eat(s
, '('))
331 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
335 if (isl_stream_eat(s
, ')'))
339 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
342 isl_pw_aff_list_free(list
);
346 /* Is "tok" the start of an integer division?
348 static int is_start_of_div(struct isl_token
*tok
)
352 if (tok
->type
== '[')
354 if (tok
->type
== ISL_TOKEN_FLOOR
)
356 if (tok
->type
== ISL_TOKEN_CEIL
)
358 if (tok
->type
== ISL_TOKEN_FLOORD
)
360 if (tok
->type
== ISL_TOKEN_CEILD
)
365 /* Read an integer division from "s" and return it as an isl_pw_aff.
367 * The integer division can be of the form
369 * [<affine expression>]
370 * floor(<affine expression>)
371 * ceil(<affine expression>)
372 * floord(<affine expression>,<denominator>)
373 * ceild(<affine expression>,<denominator>)
375 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
376 __isl_take isl_space
*dim
, struct vars
*v
)
378 struct isl_token
*tok
;
382 isl_pw_aff
*pwaff
= NULL
;
384 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
386 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
388 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
390 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
393 if (isl_stream_eat(s
, '('))
396 if (isl_stream_eat(s
, '['))
400 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
403 if (isl_stream_eat(s
, ','))
409 if (tok
->type
!= ISL_TOKEN_VALUE
) {
410 isl_stream_error(s
, tok
, "expected denominator");
411 isl_stream_push_token(s
, tok
);
414 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
419 pwaff
= isl_pw_aff_ceil(pwaff
);
421 pwaff
= isl_pw_aff_floor(pwaff
);
424 if (isl_stream_eat(s
, ')'))
427 if (isl_stream_eat(s
, ']'))
435 isl_pw_aff_free(pwaff
);
439 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
440 __isl_take isl_space
*dim
, struct vars
*v
)
442 struct isl_token
*tok
= NULL
;
443 isl_pw_aff
*res
= NULL
;
447 isl_stream_error(s
, NULL
, "unexpected EOF");
451 if (tok
->type
== ISL_TOKEN_AFF
) {
452 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
454 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
456 int pos
= vars_pos(v
, tok
->u
.s
, -1);
462 vars_drop(v
, v
->n
- n
);
463 isl_stream_error(s
, tok
, "unknown identifier");
467 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
470 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
471 res
= isl_pw_aff_from_aff(aff
);
473 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
474 if (isl_stream_eat_if_available(s
, '*')) {
475 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
476 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
480 ls
= isl_local_space_from_space(isl_space_copy(dim
));
481 aff
= isl_aff_zero_on_domain(ls
);
482 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
483 res
= isl_pw_aff_from_aff(aff
);
486 } else if (tok
->type
== '(') {
489 res
= accept_affine(s
, isl_space_copy(dim
), v
);
492 if (isl_stream_eat(s
, ')'))
494 } else if (is_start_of_div(tok
)) {
495 isl_stream_push_token(s
, tok
);
497 res
= accept_div(s
, isl_space_copy(dim
), v
);
498 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
499 isl_stream_push_token(s
, tok
);
501 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
503 isl_stream_error(s
, tok
, "expecting factor");
506 if (isl_stream_eat_if_available(s
, '%') ||
507 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
509 return affine_mod(s
, v
, res
);
511 if (isl_stream_eat_if_available(s
, '*')) {
514 isl_int_set_si(f
, 1);
515 if (accept_cst_factor(s
, &f
) < 0) {
519 res
= isl_pw_aff_scale(res
, f
);
522 if (isl_stream_eat_if_available(s
, '/')) {
525 isl_int_set_si(f
, 1);
526 if (accept_cst_factor(s
, &f
) < 0) {
530 res
= isl_pw_aff_scale_down(res
, f
);
539 isl_pw_aff_free(res
);
544 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
549 space
= isl_pw_aff_get_domain_space(pwaff
);
550 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
551 aff
= isl_aff_add_constant(aff
, v
);
553 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
556 /* Return a piecewise affine expression defined on the specified domain
557 * that represents NaN.
559 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
563 ls
= isl_local_space_from_space(isl_space_copy(space
));
564 return isl_pw_aff_nan_on_domain(ls
);
567 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
568 __isl_take isl_space
*space
, struct vars
*v
)
570 struct isl_token
*tok
= NULL
;
575 ls
= isl_local_space_from_space(isl_space_copy(space
));
576 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
583 isl_stream_error(s
, NULL
, "unexpected EOF");
586 if (tok
->type
== '-') {
591 if (tok
->type
== '(' || is_start_of_div(tok
) ||
592 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
593 tok
->type
== ISL_TOKEN_IDENT
||
594 tok
->type
== ISL_TOKEN_AFF
) {
596 isl_stream_push_token(s
, tok
);
598 term
= accept_affine_factor(s
,
599 isl_space_copy(space
), v
);
601 res
= isl_pw_aff_sub(res
, term
);
603 res
= isl_pw_aff_add(res
, term
);
607 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
609 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
610 if (isl_stream_eat_if_available(s
, '*') ||
611 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
613 term
= accept_affine_factor(s
,
614 isl_space_copy(space
), v
);
615 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
616 res
= isl_pw_aff_add(res
, term
);
620 res
= add_cst(res
, tok
->u
.v
);
623 } else if (tok
->type
== ISL_TOKEN_NAN
) {
624 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
626 isl_stream_error(s
, tok
, "unexpected isl_token");
627 isl_stream_push_token(s
, tok
);
628 isl_pw_aff_free(res
);
629 isl_space_free(space
);
635 if (tok
&& tok
->type
== '-') {
638 } else if (tok
&& tok
->type
== '+') {
641 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
642 isl_int_is_neg(tok
->u
.v
)) {
643 isl_stream_push_token(s
, tok
);
646 isl_stream_push_token(s
, tok
);
651 isl_space_free(space
);
654 isl_space_free(space
);
656 isl_pw_aff_free(res
);
660 static int is_comparator(struct isl_token
*tok
)
678 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
679 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
680 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
681 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
683 /* Accept a ternary operator, given the first argument.
685 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
686 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
689 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
694 if (isl_stream_eat(s
, '?'))
697 dim
= isl_space_wrap(isl_map_get_space(cond
));
698 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
702 if (isl_stream_eat(s
, ':'))
705 dim
= isl_pw_aff_get_domain_space(pwaff1
);
706 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
710 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
711 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
714 isl_pw_aff_free(pwaff1
);
715 isl_pw_aff_free(pwaff2
);
719 /* Set *line and *col to those of the next token, if any.
721 static void set_current_line_col(struct isl_stream
*s
, int *line
, int *col
)
723 struct isl_token
*tok
;
725 tok
= isl_stream_next_token(s
);
731 isl_stream_push_token(s
, tok
);
734 /* Push a token encapsulating "pa" onto "s", with the given
737 static int push_aff(struct isl_stream
*s
, int line
, int col
,
738 __isl_take isl_pw_aff
*pa
)
740 struct isl_token
*tok
;
742 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
745 tok
->type
= ISL_TOKEN_AFF
;
747 isl_stream_push_token(s
, tok
);
755 /* Accept an affine expression that may involve ternary operators.
756 * We first read an affine expression.
757 * If it is not followed by a comparison operator, we simply return it.
758 * Otherwise, we assume the affine expression is part of the first
759 * argument of a ternary operator and try to parse that.
761 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
762 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
767 struct isl_token
*tok
;
768 int line
= -1, col
= -1;
771 set_current_line_col(s
, &line
, &col
);
773 pwaff
= accept_affine(s
, dim
, v
);
775 pwaff
= isl_pw_aff_set_rational(pwaff
);
779 tok
= isl_stream_next_token(s
);
781 return isl_pw_aff_free(pwaff
);
783 is_comp
= is_comparator(tok
);
784 isl_stream_push_token(s
, tok
);
788 space
= isl_pw_aff_get_domain_space(pwaff
);
789 cond
= isl_map_universe(isl_space_unwrap(space
));
791 if (push_aff(s
, line
, col
, pwaff
) < 0)
794 cond
= read_formula(s
, v
, cond
, rational
);
796 return accept_ternary(s
, cond
, v
, rational
);
799 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
800 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
807 if (type
== isl_dim_param
)
808 pos
= isl_map_dim(map
, isl_dim_param
);
810 pos
= isl_map_dim(map
, isl_dim_in
);
811 if (type
== isl_dim_out
)
812 pos
+= isl_map_dim(map
, isl_dim_out
);
817 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
819 def_map
= isl_map_from_pw_aff(def
);
820 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
821 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
823 map
= isl_map_intersect(map
, def_map
);
828 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
829 __isl_take isl_space
*dim
, struct vars
*v
)
832 isl_pw_aff_list
*list
;
833 struct isl_token
*tok
= NULL
;
835 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
836 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
841 tok
= isl_stream_next_token(s
);
843 isl_stream_error(s
, NULL
, "unexpected EOF");
846 if (tok
->type
!= ',') {
847 isl_stream_push_token(s
, tok
);
852 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
853 list
= isl_pw_aff_list_concat(list
,
854 isl_pw_aff_list_from_pw_aff(pwaff
));
863 isl_pw_aff_list_free(list
);
867 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
868 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
870 struct isl_token
*tok
;
872 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
876 if (tok
->type
!= ISL_TOKEN_IDENT
)
879 p
= vars_pos(v
, tok
->u
.s
, -1);
883 isl_stream_error(s
, tok
, "expecting unique identifier");
887 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
890 tok
= isl_stream_next_token(s
);
891 if (tok
&& tok
->type
== '=') {
893 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
894 tok
= isl_stream_next_token(s
);
897 if (!tok
|| tok
->type
!= ',')
903 isl_stream_push_token(s
, tok
);
912 static int next_is_tuple(struct isl_stream
*s
)
914 struct isl_token
*tok
;
917 tok
= isl_stream_next_token(s
);
920 if (tok
->type
== '[') {
921 isl_stream_push_token(s
, tok
);
924 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
925 isl_stream_push_token(s
, tok
);
929 is_tuple
= isl_stream_next_token_is(s
, '[');
931 isl_stream_push_token(s
, tok
);
936 /* Allocate an initial tuple with zero dimensions and an anonymous,
937 * unstructured space.
938 * A tuple is represented as an isl_multi_pw_aff.
939 * The range space is the space of the tuple.
940 * The domain space is an anonymous space
941 * with a dimension for each variable in the set of variables in "v".
942 * If a given dimension is not defined in terms of earlier dimensions in
943 * the input, then the corresponding isl_pw_aff is set equal to one time
944 * the variable corresponding to the dimension being defined.
946 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
948 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
951 /* Is "pa" an expression in term of earlier dimensions?
952 * The alternative is that the dimension is defined to be equal to itself,
953 * meaning that it has a universe domain and an expression that depends
954 * on itself. "i" is the position of the expression in a sequence
955 * of "n" expressions. The final dimensions of "pa" correspond to
956 * these "n" expressions.
958 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
966 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
970 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
975 /* Does the tuple contain any dimensions that are defined
976 * in terms of earlier dimensions?
978 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
986 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
987 for (i
= 0; i
< n
; ++i
) {
988 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
989 has_expr
= pw_aff_is_expr(pa
, i
, n
);
991 if (has_expr
< 0 || has_expr
)
998 /* Add a dimension to the given tuple.
999 * The dimension is initially undefined, so it is encoded
1000 * as one times itself.
1002 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
1003 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
1009 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
1010 space
= isl_multi_pw_aff_get_domain_space(tuple
);
1011 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1012 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
1013 pa
= isl_pw_aff_from_aff(aff
);
1014 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
1015 isl_multi_pw_aff_from_pw_aff(pa
));
1020 /* Set the name of dimension "pos" in "tuple" to "name".
1021 * During printing, we add primes if the same name appears more than once
1022 * to distinguish the occurrences. Here, we remove those primes from "name"
1023 * before setting the name of the dimension.
1025 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
1026 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
1033 prime
= strchr(name
, '\'');
1036 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
1043 /* Accept a piecewise affine expression.
1045 * At the outer level, the piecewise affine expression may be of the form
1047 * aff1 : condition1; aff2 : conditions2; ...
1053 * each of the affine expressions may in turn include ternary operators.
1055 * There may be parentheses around some subexpression of "aff1"
1056 * around "aff1" itself, around "aff1 : condition1" and/or
1057 * around the entire piecewise affine expression.
1058 * We therefore remove the opening parenthesis (if any) from the stream
1059 * in case the closing parenthesis follows the colon, but if the closing
1060 * parenthesis is the first thing in the stream after the parsed affine
1061 * expression, we push the parsed expression onto the stream and parse
1062 * again in case the parentheses enclose some subexpression of "aff1".
1064 static __isl_give isl_pw_aff
*accept_piecewise_affine(struct isl_stream
*s
,
1065 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1068 isl_space
*res_space
;
1070 res_space
= isl_space_from_domain(isl_space_copy(space
));
1071 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1072 res
= isl_pw_aff_empty(res_space
);
1076 int line
= -1, col
= -1;
1078 set_current_line_col(s
, &line
, &col
);
1079 seen_paren
= isl_stream_eat_if_available(s
, '(');
1081 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1084 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1086 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1088 if (push_aff(s
, line
, col
, pa
) < 0)
1090 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1093 if (isl_stream_eat_if_available(s
, ':')) {
1094 isl_space
*dom_space
;
1097 dom_space
= isl_pw_aff_get_domain_space(pa
);
1098 dom
= isl_set_universe(dom_space
);
1099 dom
= read_formula(s
, v
, dom
, rational
);
1100 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1103 res
= isl_pw_aff_union_add(res
, pa
);
1105 if (seen_paren
&& isl_stream_eat(s
, ')'))
1107 } while (isl_stream_eat_if_available(s
, ';'));
1109 isl_space_free(space
);
1113 isl_space_free(space
);
1114 return isl_pw_aff_free(res
);
1117 /* Read an affine expression from "s" and replace the definition
1118 * of dimension "pos" in "tuple" by this expression.
1120 * accept_extended_affine requires a wrapped space as input.
1121 * The domain space of "tuple", on the other hand is an anonymous space,
1122 * so we have to adjust the space of the isl_pw_aff before adding it
1125 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
1126 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
1132 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1134 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1136 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1137 def
= isl_pw_aff_reset_domain_space(def
, space
);
1138 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
1143 /* Read a list of variables and/or affine expressions and return the list
1144 * as an isl_multi_pw_aff.
1145 * The elements in the list are separated by either "," or "][".
1146 * If "comma" is set then only "," is allowed.
1148 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
1149 struct vars
*v
, int rational
, int comma
)
1152 struct isl_token
*tok
;
1153 isl_multi_pw_aff
*res
;
1155 res
= tuple_alloc(v
);
1157 if (isl_stream_next_token_is(s
, ']'))
1160 while ((tok
= next_token(s
)) != NULL
) {
1163 res
= tuple_add_dim(res
, v
);
1165 if (tok
->type
== ISL_TOKEN_IDENT
) {
1167 int p
= vars_pos(v
, tok
->u
.s
, -1);
1173 if (tok
->type
== '*') {
1174 if (vars_add_anon(v
) < 0)
1176 isl_token_free(tok
);
1177 } else if (new_name
) {
1178 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
1179 isl_token_free(tok
);
1180 if (isl_stream_eat_if_available(s
, '='))
1181 res
= read_tuple_var_def(s
, res
, i
, v
,
1184 isl_stream_push_token(s
, tok
);
1186 if (vars_add_anon(v
) < 0)
1188 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
1191 tok
= isl_stream_next_token(s
);
1192 if (!comma
&& tok
&& tok
->type
== ']' &&
1193 isl_stream_next_token_is(s
, '[')) {
1194 isl_token_free(tok
);
1195 tok
= isl_stream_next_token(s
);
1196 } else if (!tok
|| tok
->type
!= ',')
1199 isl_token_free(tok
);
1203 isl_stream_push_token(s
, tok
);
1207 isl_token_free(tok
);
1208 return isl_multi_pw_aff_free(res
);
1211 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1213 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1214 struct vars
*v
, int rational
, int comma
)
1216 struct isl_token
*tok
;
1218 isl_multi_pw_aff
*res
= NULL
;
1220 tok
= isl_stream_next_token(s
);
1223 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1224 name
= strdup(tok
->u
.s
);
1225 isl_token_free(tok
);
1229 isl_stream_push_token(s
, tok
);
1230 if (isl_stream_eat(s
, '['))
1232 if (next_is_tuple(s
)) {
1233 isl_multi_pw_aff
*out
;
1235 res
= read_tuple(s
, v
, rational
, comma
);
1236 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1238 out
= read_tuple(s
, v
, rational
, comma
);
1239 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1240 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1241 res
= isl_multi_pw_aff_range_product(res
, out
);
1243 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1244 if (isl_stream_eat(s
, ']'))
1248 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1255 return isl_multi_pw_aff_free(res
);
1258 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1259 * We first create the appropriate space in "map" based on the range
1260 * space of this isl_multi_pw_aff. Then, we add equalities based
1261 * on the affine expressions. These live in an anonymous space,
1262 * however, so we first need to reset the space to that of "map".
1264 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1265 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1270 isl_space
*space
= NULL
;
1274 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1275 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1276 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1280 if (type
== isl_dim_param
) {
1281 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1282 isl_space_is_wrapping(space
)) {
1283 isl_die(ctx
, isl_error_invalid
,
1284 "parameter tuples cannot be named or nested",
1287 map
= isl_map_add_dims(map
, type
, n
);
1288 for (i
= 0; i
< n
; ++i
) {
1290 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1291 isl_die(ctx
, isl_error_invalid
,
1292 "parameters must be named",
1294 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1295 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1297 } else if (type
== isl_dim_in
) {
1300 set
= isl_set_universe(isl_space_copy(space
));
1302 set
= isl_set_set_rational(set
);
1303 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1304 map
= isl_map_from_domain(set
);
1308 set
= isl_set_universe(isl_space_copy(space
));
1310 set
= isl_set_set_rational(set
);
1311 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1314 for (i
= 0; i
< n
; ++i
) {
1321 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1322 space
= isl_pw_aff_get_domain_space(pa
);
1323 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1324 aff
= isl_aff_add_coefficient_si(aff
,
1325 isl_dim_in
, v
->n
- n
+ i
, -1);
1326 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1328 pa
= isl_pw_aff_set_rational(pa
);
1329 set
= isl_pw_aff_zero_set(pa
);
1330 map_i
= isl_map_from_range(set
);
1331 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1332 map
= isl_map_intersect(map
, map_i
);
1335 isl_space_free(space
);
1336 isl_multi_pw_aff_free(tuple
);
1339 isl_space_free(space
);
1340 isl_multi_pw_aff_free(tuple
);
1345 /* Read a tuple from "s" and add it to "map".
1346 * The tuple is initially represented as an isl_multi_pw_aff and
1347 * then added to "map".
1349 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1350 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1351 int rational
, int comma
)
1353 isl_multi_pw_aff
*tuple
;
1355 tuple
= read_tuple(s
, v
, rational
, comma
);
1357 return isl_map_free(map
);
1359 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1362 static __isl_give isl_set
*construct_constraints(
1363 __isl_take isl_set
*set
, int type
,
1364 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1369 left
= isl_pw_aff_list_copy(left
);
1370 right
= isl_pw_aff_list_copy(right
);
1372 left
= isl_pw_aff_list_set_rational(left
);
1373 right
= isl_pw_aff_list_set_rational(right
);
1375 if (type
== ISL_TOKEN_LE
)
1376 cond
= isl_pw_aff_list_le_set(left
, right
);
1377 else if (type
== ISL_TOKEN_GE
)
1378 cond
= isl_pw_aff_list_ge_set(left
, right
);
1379 else if (type
== ISL_TOKEN_LT
)
1380 cond
= isl_pw_aff_list_lt_set(left
, right
);
1381 else if (type
== ISL_TOKEN_GT
)
1382 cond
= isl_pw_aff_list_gt_set(left
, right
);
1383 else if (type
== ISL_TOKEN_NE
)
1384 cond
= isl_pw_aff_list_ne_set(left
, right
);
1386 cond
= isl_pw_aff_list_eq_set(left
, right
);
1388 return isl_set_intersect(set
, cond
);
1391 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1392 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1394 struct isl_token
*tok
= NULL
;
1395 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1398 set
= isl_map_wrap(map
);
1399 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1402 tok
= isl_stream_next_token(s
);
1403 if (!is_comparator(tok
)) {
1404 isl_stream_error(s
, tok
, "missing operator");
1406 isl_stream_push_token(s
, tok
);
1411 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1415 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1417 isl_token_free(tok
);
1418 isl_pw_aff_list_free(list1
);
1421 tok
= isl_stream_next_token(s
);
1422 if (!is_comparator(tok
)) {
1424 isl_stream_push_token(s
, tok
);
1428 isl_pw_aff_list_free(list1
);
1430 return isl_set_unwrap(set
);
1433 isl_token_free(tok
);
1434 isl_pw_aff_list_free(list1
);
1435 isl_pw_aff_list_free(list2
);
1440 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1441 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1444 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1446 map
= isl_map_from_domain(isl_map_wrap(map
));
1447 map
= read_defined_var_list(s
, v
, map
, rational
);
1449 if (isl_stream_eat(s
, ':'))
1452 map
= read_formula(s
, v
, map
, rational
);
1453 map
= isl_set_unwrap(isl_map_domain(map
));
1455 vars_drop(v
, v
->n
- n
);
1456 if (seen_paren
&& isl_stream_eat(s
, ')'))
1465 /* Parse an expression between parentheses and push the result
1466 * back on the stream.
1468 * The parsed expression may be either an affine expression
1469 * or a condition. The first type is pushed onto the stream
1470 * as an isl_pw_aff, while the second is pushed as an isl_map.
1472 * If the initial token indicates the start of a condition,
1473 * we parse it as such.
1474 * Otherwise, we first parse an affine expression and push
1475 * that onto the stream. If the affine expression covers the
1476 * entire expression between parentheses, we return.
1477 * Otherwise, we assume that the affine expression is the
1478 * start of a condition and continue parsing.
1480 static int resolve_paren_expr(struct isl_stream
*s
,
1481 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1483 struct isl_token
*tok
, *tok2
;
1487 tok
= isl_stream_next_token(s
);
1488 if (!tok
|| tok
->type
!= '(')
1491 if (isl_stream_next_token_is(s
, '('))
1492 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1495 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1496 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1497 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1498 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1499 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1500 map
= read_formula(s
, v
, map
, rational
);
1501 if (isl_stream_eat(s
, ')'))
1503 tok
->type
= ISL_TOKEN_MAP
;
1505 isl_stream_push_token(s
, tok
);
1509 tok2
= isl_stream_next_token(s
);
1514 isl_stream_push_token(s
, tok2
);
1516 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1520 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1523 tok2
->type
= ISL_TOKEN_AFF
;
1524 tok2
->u
.pwaff
= pwaff
;
1526 if (isl_stream_eat_if_available(s
, ')')) {
1527 isl_stream_push_token(s
, tok2
);
1528 isl_token_free(tok
);
1533 isl_stream_push_token(s
, tok2
);
1535 map
= read_formula(s
, v
, map
, rational
);
1536 if (isl_stream_eat(s
, ')'))
1539 tok
->type
= ISL_TOKEN_MAP
;
1541 isl_stream_push_token(s
, tok
);
1545 isl_pw_aff_free(pwaff
);
1547 isl_token_free(tok
);
1552 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1553 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1555 if (isl_stream_next_token_is(s
, '('))
1556 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1559 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1560 struct isl_token
*tok
;
1561 tok
= isl_stream_next_token(s
);
1565 map
= isl_map_copy(tok
->u
.map
);
1566 isl_token_free(tok
);
1570 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1571 return read_exists(s
, v
, map
, rational
);
1573 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1576 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1577 isl_space
*dim
= isl_map_get_space(map
);
1579 return isl_map_empty(dim
);
1582 return add_constraint(s
, v
, map
, rational
);
1588 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1589 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1594 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1595 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1597 res
= isl_map_subtract(isl_map_copy(map
), res
);
1599 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1602 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1603 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1605 res
= isl_map_subtract(res
, res_i
);
1607 res
= isl_map_intersect(res
, res_i
);
1614 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1615 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1619 if (isl_stream_next_token_is(s
, '}')) {
1620 isl_space
*dim
= isl_map_get_space(map
);
1622 return isl_map_universe(dim
);
1625 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1626 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1629 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1630 res
= isl_map_union(res
, res_i
);
1637 /* Read a first order formula from "s", add the corresponding
1638 * constraints to "map" and return the result.
1640 * In particular, read a formula of the form
1648 * where a and b are disjunctions.
1650 * In the first case, map is replaced by
1652 * map \cap { [..] : a }
1654 * In the second case, it is replaced by
1656 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1658 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
1659 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1663 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1665 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1668 res
= isl_map_subtract(isl_map_copy(map
), res
);
1669 res2
= read_disjuncts(s
, v
, map
, rational
);
1670 res
= isl_map_union(res
, res2
);
1677 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1679 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1680 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1681 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1682 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1684 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1685 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1686 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1688 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1689 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1690 isl_basic_map_dim(bmap
, isl_dim_in
) +
1691 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1692 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1694 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1700 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1701 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1704 struct isl_token
*tok
;
1714 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1715 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1717 tok
= isl_stream_next_token(s
);
1718 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1719 isl_stream_error(s
, tok
, "expecting coefficient");
1721 isl_stream_push_token(s
, tok
);
1724 if (!tok
->on_new_line
) {
1725 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1726 isl_stream_push_token(s
, tok
);
1730 type
= isl_int_get_si(tok
->u
.v
);
1731 isl_token_free(tok
);
1733 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1735 k
= isl_basic_map_alloc_equality(bmap
);
1738 k
= isl_basic_map_alloc_inequality(bmap
);
1744 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1746 tok
= isl_stream_next_token(s
);
1747 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1748 isl_stream_error(s
, tok
, "expecting coefficient");
1750 isl_stream_push_token(s
, tok
);
1753 if (tok
->on_new_line
) {
1754 isl_stream_error(s
, tok
,
1755 "coefficient should not appear on new line");
1756 isl_stream_push_token(s
, tok
);
1759 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1760 isl_int_set(c
[pos
], tok
->u
.v
);
1761 isl_token_free(tok
);
1766 isl_basic_map_free(bmap
);
1770 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1773 struct isl_token
*tok
;
1774 struct isl_token
*tok2
;
1777 unsigned in
= 0, out
, local
= 0;
1778 struct isl_basic_map
*bmap
= NULL
;
1781 tok
= isl_stream_next_token(s
);
1783 isl_stream_error(s
, NULL
, "unexpected EOF");
1786 tok2
= isl_stream_next_token(s
);
1788 isl_token_free(tok
);
1789 isl_stream_error(s
, NULL
, "unexpected EOF");
1792 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1793 isl_stream_push_token(s
, tok2
);
1794 isl_stream_push_token(s
, tok
);
1795 isl_stream_error(s
, NULL
,
1796 "expecting constraint matrix dimensions");
1799 n_row
= isl_int_get_si(tok
->u
.v
);
1800 n_col
= isl_int_get_si(tok2
->u
.v
);
1801 on_new_line
= tok2
->on_new_line
;
1802 isl_token_free(tok2
);
1803 isl_token_free(tok
);
1804 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1805 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1806 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1807 tok
= isl_stream_next_token_on_same_line(s
);
1809 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1810 isl_stream_error(s
, tok
,
1811 "expecting number of output dimensions");
1812 isl_stream_push_token(s
, tok
);
1815 out
= isl_int_get_si(tok
->u
.v
);
1816 isl_token_free(tok
);
1818 tok
= isl_stream_next_token_on_same_line(s
);
1819 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1820 isl_stream_error(s
, tok
,
1821 "expecting number of input dimensions");
1823 isl_stream_push_token(s
, tok
);
1826 in
= isl_int_get_si(tok
->u
.v
);
1827 isl_token_free(tok
);
1829 tok
= isl_stream_next_token_on_same_line(s
);
1830 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1831 isl_stream_error(s
, tok
,
1832 "expecting number of existentials");
1834 isl_stream_push_token(s
, tok
);
1837 local
= isl_int_get_si(tok
->u
.v
);
1838 isl_token_free(tok
);
1840 tok
= isl_stream_next_token_on_same_line(s
);
1841 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1842 isl_stream_error(s
, tok
,
1843 "expecting number of parameters");
1845 isl_stream_push_token(s
, tok
);
1848 nparam
= isl_int_get_si(tok
->u
.v
);
1849 isl_token_free(tok
);
1850 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1851 isl_stream_error(s
, NULL
,
1852 "dimensions don't match");
1856 out
= n_col
- 2 - nparam
;
1857 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1861 for (i
= 0; i
< local
; ++i
) {
1862 int k
= isl_basic_map_alloc_div(bmap
);
1865 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1868 for (i
= 0; i
< n_row
; ++i
)
1869 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1871 tok
= isl_stream_next_token_on_same_line(s
);
1873 isl_stream_error(s
, tok
, "unexpected extra token on line");
1874 isl_stream_push_token(s
, tok
);
1878 bmap
= isl_basic_map_simplify(bmap
);
1879 bmap
= isl_basic_map_finalize(bmap
);
1882 isl_basic_map_free(bmap
);
1886 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1888 struct isl_token
*tok
;
1889 struct isl_token
*tok2
;
1891 struct isl_map
*map
;
1893 tok
= isl_stream_next_token(s
);
1895 isl_stream_error(s
, NULL
, "unexpected EOF");
1898 tok2
= isl_stream_next_token_on_same_line(s
);
1899 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1900 isl_stream_push_token(s
, tok2
);
1901 isl_stream_push_token(s
, tok
);
1902 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1905 isl_stream_error(s
, tok2
, "unexpected token");
1906 isl_stream_push_token(s
, tok2
);
1907 isl_stream_push_token(s
, tok
);
1910 n
= isl_int_get_si(tok
->u
.v
);
1911 isl_token_free(tok
);
1913 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1915 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1917 for (i
= 1; map
&& i
< n
; ++i
)
1918 map
= isl_map_union(map
,
1919 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1924 static int optional_power(struct isl_stream
*s
)
1927 struct isl_token
*tok
;
1929 tok
= isl_stream_next_token(s
);
1932 if (tok
->type
!= '^') {
1933 isl_stream_push_token(s
, tok
);
1936 isl_token_free(tok
);
1937 tok
= isl_stream_next_token(s
);
1938 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1939 isl_stream_error(s
, tok
, "expecting exponent");
1941 isl_stream_push_token(s
, tok
);
1944 pow
= isl_int_get_si(tok
->u
.v
);
1945 isl_token_free(tok
);
1949 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1950 __isl_keep isl_map
*map
, struct vars
*v
);
1952 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1953 __isl_keep isl_map
*map
, struct vars
*v
)
1955 isl_pw_qpolynomial
*pwqp
;
1956 struct isl_token
*tok
;
1958 tok
= next_token(s
);
1960 isl_stream_error(s
, NULL
, "unexpected EOF");
1963 if (tok
->type
== '(') {
1966 isl_token_free(tok
);
1967 pwqp
= read_term(s
, map
, v
);
1970 if (isl_stream_eat(s
, ')'))
1972 pow
= optional_power(s
);
1973 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1974 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1975 struct isl_token
*tok2
;
1976 isl_qpolynomial
*qp
;
1978 tok2
= isl_stream_next_token(s
);
1979 if (tok2
&& tok2
->type
== '/') {
1980 isl_token_free(tok2
);
1981 tok2
= next_token(s
);
1982 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1983 isl_stream_error(s
, tok2
, "expected denominator");
1984 isl_token_free(tok
);
1985 isl_token_free(tok2
);
1988 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1989 tok
->u
.v
, tok2
->u
.v
);
1990 isl_token_free(tok2
);
1992 isl_stream_push_token(s
, tok2
);
1993 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1996 isl_token_free(tok
);
1997 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1998 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1999 isl_qpolynomial
*qp
;
2000 isl_token_free(tok
);
2001 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2002 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2003 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2004 isl_qpolynomial
*qp
;
2005 isl_token_free(tok
);
2006 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2007 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2008 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2010 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2012 isl_qpolynomial
*qp
;
2014 isl_token_free(tok
);
2018 vars_drop(v
, v
->n
- n
);
2019 isl_stream_error(s
, tok
, "unknown identifier");
2020 isl_token_free(tok
);
2023 isl_token_free(tok
);
2024 pow
= optional_power(s
);
2025 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2026 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2027 } else if (is_start_of_div(tok
)) {
2031 isl_stream_push_token(s
, tok
);
2032 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2033 pow
= optional_power(s
);
2034 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2035 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2036 } else if (tok
->type
== '-') {
2037 isl_token_free(tok
);
2038 pwqp
= read_factor(s
, map
, v
);
2039 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2041 isl_stream_error(s
, tok
, "unexpected isl_token");
2042 isl_stream_push_token(s
, tok
);
2046 if (isl_stream_eat_if_available(s
, '*') ||
2047 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2048 isl_pw_qpolynomial
*pwqp2
;
2050 pwqp2
= read_factor(s
, map
, v
);
2051 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2056 isl_pw_qpolynomial_free(pwqp
);
2060 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
2061 __isl_keep isl_map
*map
, struct vars
*v
)
2063 struct isl_token
*tok
;
2064 isl_pw_qpolynomial
*pwqp
;
2066 pwqp
= read_factor(s
, map
, v
);
2069 tok
= next_token(s
);
2073 if (tok
->type
== '+') {
2074 isl_pw_qpolynomial
*pwqp2
;
2076 isl_token_free(tok
);
2077 pwqp2
= read_factor(s
, map
, v
);
2078 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2079 } else if (tok
->type
== '-') {
2080 isl_pw_qpolynomial
*pwqp2
;
2082 isl_token_free(tok
);
2083 pwqp2
= read_factor(s
, map
, v
);
2084 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2085 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2086 isl_int_is_neg(tok
->u
.v
)) {
2087 isl_pw_qpolynomial
*pwqp2
;
2089 isl_stream_push_token(s
, tok
);
2090 pwqp2
= read_factor(s
, map
, v
);
2091 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2093 isl_stream_push_token(s
, tok
);
2101 static __isl_give isl_map
*read_optional_formula(struct isl_stream
*s
,
2102 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2104 struct isl_token
*tok
;
2106 tok
= isl_stream_next_token(s
);
2108 isl_stream_error(s
, NULL
, "unexpected EOF");
2111 if (tok
->type
== ':' ||
2112 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2113 isl_token_free(tok
);
2114 map
= read_formula(s
, v
, map
, rational
);
2116 isl_stream_push_token(s
, tok
);
2124 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
2125 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2127 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2128 isl_pw_qpolynomial
*pwqp
;
2129 struct isl_set
*set
;
2131 pwqp
= read_term(s
, map
, v
);
2132 map
= read_optional_formula(s
, map
, v
, 0);
2133 set
= isl_map_range(map
);
2135 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2137 vars_drop(v
, v
->n
- n
);
2143 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
2144 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2146 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2147 isl_pw_qpolynomial
*pwqp
;
2148 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2150 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2151 return obj_read_poly(s
, set
, v
, n
);
2153 if (isl_stream_eat(s
, '('))
2156 pwqp
= read_term(s
, set
, v
);
2157 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2159 while (isl_stream_eat_if_available(s
, ',')) {
2160 isl_pw_qpolynomial_fold
*pwf_i
;
2161 pwqp
= read_term(s
, set
, v
);
2162 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2164 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2167 if (isl_stream_eat(s
, ')'))
2170 set
= read_optional_formula(s
, set
, v
, 0);
2171 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2173 vars_drop(v
, v
->n
- n
);
2179 isl_pw_qpolynomial_fold_free(pwf
);
2180 obj
.type
= isl_obj_none
;
2184 static int is_rational(struct isl_stream
*s
)
2186 struct isl_token
*tok
;
2188 tok
= isl_stream_next_token(s
);
2191 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2192 isl_token_free(tok
);
2193 isl_stream_eat(s
, ':');
2197 isl_stream_push_token(s
, tok
);
2202 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2203 __isl_take isl_map
*map
, struct vars
*v
)
2205 struct isl_token
*tok
;
2206 struct isl_obj obj
= { isl_obj_set
, NULL
};
2210 rational
= is_rational(s
);
2212 map
= isl_map_set_rational(map
);
2214 if (isl_stream_next_token_is(s
, ':')) {
2215 obj
.type
= isl_obj_set
;
2216 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2220 if (!next_is_tuple(s
))
2221 return obj_read_poly_or_fold(s
, map
, v
, n
);
2223 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2226 tok
= isl_stream_next_token(s
);
2229 if (tok
->type
== ISL_TOKEN_TO
) {
2230 obj
.type
= isl_obj_map
;
2231 isl_token_free(tok
);
2232 if (!next_is_tuple(s
)) {
2233 isl_set
*set
= isl_map_domain(map
);
2234 return obj_read_poly_or_fold(s
, set
, v
, n
);
2236 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2240 map
= isl_map_domain(map
);
2241 isl_stream_push_token(s
, tok
);
2244 map
= read_optional_formula(s
, map
, v
, rational
);
2246 vars_drop(v
, v
->n
- n
);
2252 obj
.type
= isl_obj_none
;
2256 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2258 if (obj
.type
== isl_obj_map
) {
2259 obj
.v
= isl_union_map_from_map(obj
.v
);
2260 obj
.type
= isl_obj_union_map
;
2261 } else if (obj
.type
== isl_obj_set
) {
2262 obj
.v
= isl_union_set_from_set(obj
.v
);
2263 obj
.type
= isl_obj_union_set
;
2264 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2265 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2266 obj
.type
= isl_obj_union_pw_qpolynomial
;
2267 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2268 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2269 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2271 isl_assert(ctx
, 0, goto error
);
2274 obj
.type
->free(obj
.v
);
2275 obj
.type
= isl_obj_none
;
2279 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2280 struct isl_obj obj1
, struct isl_obj obj2
)
2282 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2283 obj1
= to_union(ctx
, obj1
);
2284 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2285 obj2
= to_union(ctx
, obj2
);
2286 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2287 obj1
= to_union(ctx
, obj1
);
2288 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2289 obj2
= to_union(ctx
, obj2
);
2290 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2291 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2292 obj1
= to_union(ctx
, obj1
);
2293 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2294 obj2
.type
== isl_obj_pw_qpolynomial
)
2295 obj2
= to_union(ctx
, obj2
);
2296 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2297 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2298 obj1
= to_union(ctx
, obj1
);
2299 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2300 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2301 obj2
= to_union(ctx
, obj2
);
2302 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2303 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2304 obj1
= to_union(ctx
, obj1
);
2305 obj2
= to_union(ctx
, obj2
);
2307 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2308 obj1
= to_union(ctx
, obj1
);
2309 obj2
= to_union(ctx
, obj2
);
2311 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2312 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2313 obj1
= to_union(ctx
, obj1
);
2314 obj2
= to_union(ctx
, obj2
);
2316 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2317 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2318 obj1
= to_union(ctx
, obj1
);
2319 obj2
= to_union(ctx
, obj2
);
2321 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2324 obj1
.type
->free(obj1
.v
);
2325 obj2
.type
->free(obj2
.v
);
2326 obj1
.type
= isl_obj_none
;
2331 static struct isl_obj
obj_read(struct isl_stream
*s
)
2333 isl_map
*map
= NULL
;
2334 struct isl_token
*tok
;
2335 struct vars
*v
= NULL
;
2336 struct isl_obj obj
= { isl_obj_set
, NULL
};
2338 tok
= next_token(s
);
2340 isl_stream_error(s
, NULL
, "unexpected EOF");
2343 if (tok
->type
== ISL_TOKEN_VALUE
) {
2344 struct isl_token
*tok2
;
2345 struct isl_map
*map
;
2347 tok2
= isl_stream_next_token(s
);
2348 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2349 isl_int_is_neg(tok2
->u
.v
)) {
2351 isl_stream_push_token(s
, tok2
);
2352 obj
.type
= isl_obj_val
;
2353 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2354 isl_token_free(tok
);
2357 isl_stream_push_token(s
, tok2
);
2358 isl_stream_push_token(s
, tok
);
2359 map
= map_read_polylib(s
);
2362 if (isl_map_may_be_set(map
))
2363 obj
.v
= isl_map_range(map
);
2365 obj
.type
= isl_obj_map
;
2370 v
= vars_new(s
->ctx
);
2372 isl_stream_push_token(s
, tok
);
2375 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2376 if (tok
->type
== '[') {
2377 isl_stream_push_token(s
, tok
);
2378 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2381 tok
= isl_stream_next_token(s
);
2382 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2383 isl_stream_error(s
, tok
, "expecting '->'");
2385 isl_stream_push_token(s
, tok
);
2388 isl_token_free(tok
);
2389 tok
= isl_stream_next_token(s
);
2391 if (!tok
|| tok
->type
!= '{') {
2392 isl_stream_error(s
, tok
, "expecting '{'");
2394 isl_stream_push_token(s
, tok
);
2397 isl_token_free(tok
);
2399 tok
= isl_stream_next_token(s
);
2402 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2403 isl_token_free(tok
);
2404 if (isl_stream_eat(s
, '='))
2406 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2409 } else if (tok
->type
== '}') {
2410 obj
.type
= isl_obj_union_set
;
2411 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2412 isl_token_free(tok
);
2415 isl_stream_push_token(s
, tok
);
2420 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2421 if (o
.type
== isl_obj_none
|| !o
.v
)
2426 obj
= obj_add(s
->ctx
, obj
, o
);
2427 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2430 tok
= isl_stream_next_token(s
);
2431 if (!tok
|| tok
->type
!= ';')
2433 isl_token_free(tok
);
2434 if (isl_stream_next_token_is(s
, '}')) {
2435 tok
= isl_stream_next_token(s
);
2440 if (tok
&& tok
->type
== '}') {
2441 isl_token_free(tok
);
2443 isl_stream_error(s
, tok
, "unexpected isl_token");
2445 isl_token_free(tok
);
2455 obj
.type
->free(obj
.v
);
2462 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2467 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2473 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2474 obj
.type
== isl_obj_set
, goto error
);
2476 if (obj
.type
== isl_obj_set
)
2477 obj
.v
= isl_map_from_range(obj
.v
);
2481 obj
.type
->free(obj
.v
);
2485 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2491 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2492 obj
.v
= isl_map_range(obj
.v
);
2493 obj
.type
= isl_obj_set
;
2495 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2500 obj
.type
->free(obj
.v
);
2504 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2509 if (obj
.type
== isl_obj_map
) {
2510 obj
.type
= isl_obj_union_map
;
2511 obj
.v
= isl_union_map_from_map(obj
.v
);
2513 if (obj
.type
== isl_obj_set
) {
2514 obj
.type
= isl_obj_union_set
;
2515 obj
.v
= isl_union_set_from_set(obj
.v
);
2517 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2518 isl_union_set_is_empty(obj
.v
))
2519 obj
.type
= isl_obj_union_map
;
2520 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2521 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2525 obj
.type
->free(obj
.v
);
2529 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2534 if (obj
.type
== isl_obj_set
) {
2535 obj
.type
= isl_obj_union_set
;
2536 obj
.v
= isl_union_set_from_set(obj
.v
);
2539 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2543 obj
.type
->free(obj
.v
);
2547 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2550 struct isl_map
*map
;
2551 struct isl_basic_map
*bmap
;
2559 isl_die(s
->ctx
, isl_error_invalid
,
2560 "set or map description involves "
2561 "more than one disjunct", goto error
);
2564 bmap
= isl_basic_map_empty_like_map(map
);
2566 bmap
= isl_basic_map_copy(map
->p
[0]);
2576 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2578 isl_basic_map
*bmap
;
2579 bmap
= basic_map_read(s
);
2582 if (!isl_basic_map_may_be_set(bmap
))
2583 isl_die(s
->ctx
, isl_error_invalid
,
2584 "input is not a set", goto error
);
2585 return isl_basic_map_range(bmap
);
2587 isl_basic_map_free(bmap
);
2591 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2594 struct isl_basic_map
*bmap
;
2595 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2598 bmap
= basic_map_read(s
);
2603 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2606 isl_basic_set
*bset
;
2607 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2610 bset
= basic_set_read(s
);
2615 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2618 struct isl_basic_map
*bmap
;
2619 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2622 bmap
= basic_map_read(s
);
2627 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2630 isl_basic_set
*bset
;
2631 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2634 bset
= basic_set_read(s
);
2639 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2642 struct isl_map
*map
;
2643 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2646 map
= isl_stream_read_map(s
);
2651 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2654 struct isl_map
*map
;
2655 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2658 map
= isl_stream_read_map(s
);
2663 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2667 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2670 set
= isl_stream_read_set(s
);
2675 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2679 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2682 set
= isl_stream_read_set(s
);
2687 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2690 isl_union_map
*umap
;
2691 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2694 umap
= isl_stream_read_union_map(s
);
2699 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2702 isl_union_map
*umap
;
2703 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2706 umap
= isl_stream_read_union_map(s
);
2711 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2714 isl_union_set
*uset
;
2715 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2718 uset
= isl_stream_read_union_set(s
);
2723 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2726 isl_union_set
*uset
;
2727 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2730 uset
= isl_stream_read_union_set(s
);
2735 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2737 struct isl_vec
*vec
= NULL
;
2738 struct isl_token
*tok
;
2742 tok
= isl_stream_next_token(s
);
2743 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2744 isl_stream_error(s
, tok
, "expecting vector length");
2748 size
= isl_int_get_si(tok
->u
.v
);
2749 isl_token_free(tok
);
2751 vec
= isl_vec_alloc(s
->ctx
, size
);
2753 for (j
= 0; j
< size
; ++j
) {
2754 tok
= isl_stream_next_token(s
);
2755 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2756 isl_stream_error(s
, tok
, "expecting constant value");
2759 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2760 isl_token_free(tok
);
2765 isl_token_free(tok
);
2770 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2772 return isl_vec_read_polylib(s
);
2775 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2778 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2786 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2787 struct isl_stream
*s
)
2793 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2798 obj
.type
->free(obj
.v
);
2802 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2805 isl_pw_qpolynomial
*pwqp
;
2806 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2809 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2814 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2817 isl_pw_qpolynomial
*pwqp
;
2818 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2821 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2826 /* Is the next token an identifer not in "v"?
2828 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2832 struct isl_token
*tok
;
2834 tok
= isl_stream_next_token(s
);
2837 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2838 isl_stream_push_token(s
, tok
);
2840 vars_drop(v
, v
->n
- n
);
2845 /* First read the domain of the affine expression, which may be
2846 * a parameter space or a set.
2847 * The tricky part is that we don't know if the domain is a set or not,
2848 * so when we are trying to read the domain, we may actually be reading
2849 * the affine expression itself (defined on a parameter domains)
2850 * If the tuple we are reading is named, we assume it's the domain.
2851 * Also, if inside the tuple, the first thing we find is a nested tuple
2852 * or a new identifier, we again assume it's the domain.
2853 * Otherwise, we assume we are reading an affine expression.
2855 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2856 __isl_take isl_set
*dom
, struct vars
*v
)
2858 struct isl_token
*tok
;
2860 tok
= isl_stream_next_token(s
);
2861 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2862 isl_stream_push_token(s
, tok
);
2863 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2865 if (!tok
|| tok
->type
!= '[') {
2866 isl_stream_error(s
, tok
, "expecting '['");
2869 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2870 isl_stream_push_token(s
, tok
);
2871 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2873 isl_stream_push_token(s
, tok
);
2878 isl_stream_push_token(s
, tok
);
2883 /* Read an affine expression from "s".
2885 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2890 ma
= isl_stream_read_multi_aff(s
);
2893 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2894 isl_die(s
->ctx
, isl_error_invalid
,
2895 "expecting single affine expression",
2898 aff
= isl_multi_aff_get_aff(ma
, 0);
2899 isl_multi_aff_free(ma
);
2902 isl_multi_aff_free(ma
);
2906 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2908 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2909 __isl_take isl_set
*dom
, struct vars
*v
)
2911 isl_pw_aff
*pwaff
= NULL
;
2913 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2916 if (isl_stream_eat(s
, '['))
2919 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2921 if (isl_stream_eat(s
, ']'))
2924 dom
= read_optional_formula(s
, dom
, v
, 0);
2925 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2930 isl_pw_aff_free(pwaff
);
2934 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2937 isl_set
*dom
= NULL
;
2939 isl_pw_aff
*pa
= NULL
;
2942 v
= vars_new(s
->ctx
);
2946 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2947 if (next_is_tuple(s
)) {
2948 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2949 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2952 if (isl_stream_eat(s
, '{'))
2956 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2957 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2958 vars_drop(v
, v
->n
- n
);
2960 while (isl_stream_eat_if_available(s
, ';')) {
2964 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2965 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2966 vars_drop(v
, v
->n
- n
);
2968 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2971 if (isl_stream_eat(s
, '}'))
2980 isl_pw_aff_free(pa
);
2984 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2987 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2990 aff
= isl_stream_read_aff(s
);
2995 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2998 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3001 pa
= isl_stream_read_pw_aff(s
);
3006 /* Read an isl_pw_multi_aff from "s".
3007 * We currently read a generic object and if it turns out to be a set or
3008 * a map, we convert that to an isl_pw_multi_aff.
3009 * It would be more efficient if we were to construct the isl_pw_multi_aff
3012 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
3020 if (obj
.type
== isl_obj_map
)
3021 return isl_pw_multi_aff_from_map(obj
.v
);
3022 if (obj
.type
== isl_obj_set
)
3023 return isl_pw_multi_aff_from_set(obj
.v
);
3025 obj
.type
->free(obj
.v
);
3026 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3030 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3033 isl_pw_multi_aff
*pma
;
3034 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3037 pma
= isl_stream_read_pw_multi_aff(s
);
3042 /* Read an isl_union_pw_multi_aff from "s".
3043 * We currently read a generic object and if it turns out to be a set or
3044 * a map, we convert that to an isl_union_pw_multi_aff.
3045 * It would be more efficient if we were to construct
3046 * the isl_union_pw_multi_aff directly.
3048 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3049 struct isl_stream
*s
)
3057 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3058 obj
= to_union(s
->ctx
, obj
);
3059 if (obj
.type
== isl_obj_union_map
)
3060 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3061 if (obj
.type
== isl_obj_union_set
)
3062 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3064 obj
.type
->free(obj
.v
);
3065 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3069 /* Read an isl_union_pw_multi_aff from "str".
3071 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3072 isl_ctx
*ctx
, const char *str
)
3074 isl_union_pw_multi_aff
*upma
;
3075 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3078 upma
= isl_stream_read_union_pw_multi_aff(s
);
3083 /* Assuming "pa" represents a single affine expression defined on a universe
3084 * domain, extract this affine expression.
3086 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3093 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3094 "expecting single affine expression",
3096 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3097 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3098 "expecting universe domain",
3101 aff
= isl_aff_copy(pa
->p
[0].aff
);
3102 isl_pw_aff_free(pa
);
3105 isl_pw_aff_free(pa
);
3109 /* Read a multi-affine expression from "s".
3110 * If the multi-affine expression has a domain, then then tuple
3111 * representing this domain cannot involve any affine expressions.
3112 * The tuple representing the actual expressions needs to consist
3113 * of only affine expressions. Moreover, these expressions can
3114 * only depend on parameters and input dimensions and not on other
3115 * output dimensions.
3117 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
3120 isl_set
*dom
= NULL
;
3121 isl_multi_pw_aff
*tuple
= NULL
;
3123 isl_space
*space
, *dom_space
;
3124 isl_multi_aff
*ma
= NULL
;
3126 v
= vars_new(s
->ctx
);
3130 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3131 if (next_is_tuple(s
)) {
3132 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3133 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3136 if (!isl_set_plain_is_universe(dom
))
3137 isl_die(s
->ctx
, isl_error_invalid
,
3138 "expecting universe parameter domain", goto error
);
3139 if (isl_stream_eat(s
, '{'))
3142 tuple
= read_tuple(s
, v
, 0, 0);
3145 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3150 has_expr
= tuple_has_expr(tuple
);
3154 isl_die(s
->ctx
, isl_error_invalid
,
3155 "expecting universe domain", goto error
);
3156 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3157 set
= isl_set_universe(space
);
3158 dom
= isl_set_intersect_params(set
, dom
);
3159 isl_multi_pw_aff_free(tuple
);
3160 tuple
= read_tuple(s
, v
, 0, 0);
3165 if (isl_stream_eat(s
, '}'))
3168 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3169 dim
= isl_set_dim(dom
, isl_dim_all
);
3170 dom_space
= isl_set_get_space(dom
);
3171 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3172 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3173 if (!isl_space_is_params(dom_space
))
3174 space
= isl_space_map_from_domain_and_range(
3175 isl_space_copy(dom_space
), space
);
3176 isl_space_free(dom_space
);
3177 ma
= isl_multi_aff_alloc(space
);
3179 for (i
= 0; i
< n
; ++i
) {
3182 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3183 aff
= aff_from_pw_aff(pa
);
3186 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3188 isl_die(s
->ctx
, isl_error_invalid
,
3189 "not an affine expression", goto error
);
3191 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3192 space
= isl_multi_aff_get_domain_space(ma
);
3193 aff
= isl_aff_reset_domain_space(aff
, space
);
3194 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3197 isl_multi_pw_aff_free(tuple
);
3202 isl_multi_pw_aff_free(tuple
);
3205 isl_multi_aff_free(ma
);
3209 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3212 isl_multi_aff
*maff
;
3213 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3216 maff
= isl_stream_read_multi_aff(s
);
3221 /* Read an isl_multi_pw_aff from "s".
3223 * The input format is similar to that of map, except that any conditions
3224 * on the domains should be specified inside the tuple since each
3225 * piecewise affine expression may have a different domain.
3227 * Since we do not know in advance if the isl_multi_pw_aff lives
3228 * in a set or a map space, we first read the first tuple and check
3229 * if it is followed by a "->". If so, we convert the tuple into
3230 * the domain of the isl_multi_pw_aff and read in the next tuple.
3231 * This tuple (or the first tuple if it was not followed by a "->")
3232 * is then converted into the isl_multi_pw_aff.
3234 * Note that the function read_tuple accepts tuples where some output or
3235 * set dimensions are defined in terms of other output or set dimensions
3236 * since this function is also used to read maps. As a special case,
3237 * read_tuple also accept dimensions that are defined in terms of themselves
3238 * (i.e., that are not defined).
3239 * These cases are not allowed when reading am isl_multi_pw_aff so we check
3240 * that the definition of the output/set dimensions does not involve any
3241 * output/set dimensions.
3242 * We then drop the output dimensions from the domain of the result
3243 * of read_tuple (which is of the form [input, output] -> [output],
3244 * with anonymous domain) and reset the space.
3246 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(struct isl_stream
*s
)
3249 isl_set
*dom
= NULL
;
3250 isl_multi_pw_aff
*tuple
= NULL
;
3252 isl_space
*space
, *dom_space
;
3253 isl_multi_pw_aff
*mpa
= NULL
;
3255 v
= vars_new(s
->ctx
);
3259 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3260 if (next_is_tuple(s
)) {
3261 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3262 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3265 if (isl_stream_eat(s
, '{'))
3268 tuple
= read_tuple(s
, v
, 0, 0);
3271 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3272 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3273 dom
= isl_map_domain(map
);
3274 tuple
= read_tuple(s
, v
, 0, 0);
3279 if (isl_stream_eat(s
, '}'))
3282 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3283 dim
= isl_set_dim(dom
, isl_dim_all
);
3284 dom_space
= isl_set_get_space(dom
);
3285 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3286 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3287 if (!isl_space_is_params(dom_space
))
3288 space
= isl_space_map_from_domain_and_range(
3289 isl_space_copy(dom_space
), space
);
3290 isl_space_free(dom_space
);
3291 mpa
= isl_multi_pw_aff_alloc(space
);
3293 for (i
= 0; i
< n
; ++i
) {
3295 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3298 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3299 isl_pw_aff_free(pa
);
3300 isl_die(s
->ctx
, isl_error_invalid
,
3301 "not an affine expression", goto error
);
3303 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3304 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3305 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3306 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3309 isl_multi_pw_aff_free(tuple
);
3311 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3314 isl_multi_pw_aff_free(tuple
);
3317 isl_multi_pw_aff_free(mpa
);
3321 /* Read an isl_multi_pw_aff from "str".
3323 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3326 isl_multi_pw_aff
*mpa
;
3327 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3330 mpa
= isl_stream_read_multi_pw_aff(s
);
3335 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3336 struct isl_stream
*s
)
3341 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3342 obj
.type
= isl_obj_union_pw_qpolynomial
;
3343 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3346 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3351 obj
.type
->free(obj
.v
);
3355 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3356 isl_ctx
*ctx
, const char *str
)
3358 isl_union_pw_qpolynomial
*upwqp
;
3359 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3362 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);