2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
22 #include <isl_stream_private.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_vec_private.h>
30 #include <isl_val_private.h>
35 struct variable
*next
;
44 static struct vars
*vars_new(struct isl_ctx
*ctx
)
47 v
= isl_alloc_type(ctx
, struct vars
);
56 static void variable_free(struct variable
*var
)
59 struct variable
*next
= var
->next
;
66 static void vars_free(struct vars
*v
)
74 static void vars_drop(struct vars
*v
, int n
)
85 struct variable
*next
= var
->next
;
93 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
97 var
= isl_calloc_type(v
->ctx
, struct variable
);
100 var
->name
= strdup(name
);
101 var
->name
[len
] = '\0';
110 static int vars_pos(struct vars
*v
, const char *s
, int len
)
117 for (q
= v
->v
; q
; q
= q
->next
) {
118 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
125 v
->v
= variable_new(v
, s
, len
, v
->n
);
133 static int vars_add_anon(struct vars
*v
)
135 v
->v
= variable_new(v
, "", 0, v
->n
);
144 /* Obtain next token, with some preprocessing.
145 * In particular, evaluate expressions of the form x^y,
146 * with x and y values.
148 static struct isl_token
*next_token(struct isl_stream
*s
)
150 struct isl_token
*tok
, *tok2
;
152 tok
= isl_stream_next_token(s
);
153 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
155 if (!isl_stream_eat_if_available(s
, '^'))
157 tok2
= isl_stream_next_token(s
);
158 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
159 isl_stream_error(s
, tok2
, "expecting constant value");
163 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
165 isl_token_free(tok2
);
169 isl_token_free(tok2
);
173 /* Read an isl_val from "s".
175 * The following token sequences are recognized
178 * "-" "infty" -> -infty
183 * where n, d and v are integer constants.
185 __isl_give isl_val
*isl_stream_read_val(struct isl_stream
*s
)
187 struct isl_token
*tok
= NULL
;
188 struct isl_token
*tok2
= NULL
;
193 isl_stream_error(s
, NULL
, "unexpected EOF");
196 if (tok
->type
== ISL_TOKEN_INFTY
) {
198 return isl_val_infty(s
->ctx
);
200 if (tok
->type
== '-' &&
201 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
203 return isl_val_neginfty(s
->ctx
);
205 if (tok
->type
== ISL_TOKEN_NAN
) {
207 return isl_val_nan(s
->ctx
);
209 if (tok
->type
!= ISL_TOKEN_VALUE
) {
210 isl_stream_error(s
, tok
, "expecting value");
214 if (isl_stream_eat_if_available(s
, '/')) {
215 tok2
= next_token(s
);
217 isl_stream_error(s
, NULL
, "unexpected EOF");
220 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
221 isl_stream_error(s
, tok2
, "expecting value");
224 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
225 val
= isl_val_normalize(val
);
227 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
231 isl_token_free(tok2
);
235 isl_token_free(tok2
);
239 /* Read an isl_val from "str".
241 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
245 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
248 val
= isl_stream_read_val(s
);
253 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
255 struct isl_token
*tok
;
258 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
259 isl_stream_error(s
, tok
, "expecting constant value");
263 isl_int_mul(*f
, *f
, tok
->u
.v
);
267 if (isl_stream_eat_if_available(s
, '*'))
268 return accept_cst_factor(s
, f
);
276 /* Given an affine expression aff, return an affine expression
277 * for aff % d, with d the next token on the stream, which is
278 * assumed to be a constant.
280 * We introduce an integer division q = [aff/d] and the result
281 * is set to aff - d q.
283 static __isl_give isl_pw_aff
*affine_mod(struct isl_stream
*s
,
284 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
286 struct isl_token
*tok
;
290 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
291 isl_stream_error(s
, tok
, "expecting constant value");
295 q
= isl_pw_aff_copy(aff
);
296 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
297 q
= isl_pw_aff_floor(q
);
298 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
300 aff
= isl_pw_aff_sub(aff
, q
);
305 isl_pw_aff_free(aff
);
310 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
311 __isl_take isl_space
*space
, struct vars
*v
);
312 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
313 __isl_take isl_space
*dim
, struct vars
*v
);
315 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
316 __isl_take isl_space
*dim
, struct vars
*v
)
318 struct isl_token
*tok
;
319 isl_pw_aff_list
*list
= NULL
;
322 tok
= isl_stream_next_token(s
);
325 min
= tok
->type
== ISL_TOKEN_MIN
;
328 if (isl_stream_eat(s
, '('))
331 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
335 if (isl_stream_eat(s
, ')'))
339 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
342 isl_pw_aff_list_free(list
);
346 /* Is "tok" the start of an integer division?
348 static int is_start_of_div(struct isl_token
*tok
)
352 if (tok
->type
== '[')
354 if (tok
->type
== ISL_TOKEN_FLOOR
)
356 if (tok
->type
== ISL_TOKEN_CEIL
)
358 if (tok
->type
== ISL_TOKEN_FLOORD
)
360 if (tok
->type
== ISL_TOKEN_CEILD
)
365 /* Read an integer division from "s" and return it as an isl_pw_aff.
367 * The integer division can be of the form
369 * [<affine expression>]
370 * floor(<affine expression>)
371 * ceil(<affine expression>)
372 * floord(<affine expression>,<denominator>)
373 * ceild(<affine expression>,<denominator>)
375 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
376 __isl_take isl_space
*dim
, struct vars
*v
)
378 struct isl_token
*tok
;
382 isl_pw_aff
*pwaff
= NULL
;
384 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
386 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
388 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
390 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
393 if (isl_stream_eat(s
, '('))
396 if (isl_stream_eat(s
, '['))
400 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
403 if (isl_stream_eat(s
, ','))
409 if (tok
->type
!= ISL_TOKEN_VALUE
) {
410 isl_stream_error(s
, tok
, "expected denominator");
411 isl_stream_push_token(s
, tok
);
414 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
419 pwaff
= isl_pw_aff_ceil(pwaff
);
421 pwaff
= isl_pw_aff_floor(pwaff
);
424 if (isl_stream_eat(s
, ')'))
427 if (isl_stream_eat(s
, ']'))
435 isl_pw_aff_free(pwaff
);
439 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
440 __isl_take isl_space
*dim
, struct vars
*v
)
442 struct isl_token
*tok
= NULL
;
443 isl_pw_aff
*res
= NULL
;
447 isl_stream_error(s
, NULL
, "unexpected EOF");
451 if (tok
->type
== ISL_TOKEN_AFF
) {
452 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
454 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
456 int pos
= vars_pos(v
, tok
->u
.s
, -1);
462 vars_drop(v
, v
->n
- n
);
463 isl_stream_error(s
, tok
, "unknown identifier");
467 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
470 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
471 res
= isl_pw_aff_from_aff(aff
);
473 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
474 if (isl_stream_eat_if_available(s
, '*')) {
475 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
476 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
480 ls
= isl_local_space_from_space(isl_space_copy(dim
));
481 aff
= isl_aff_zero_on_domain(ls
);
482 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
483 res
= isl_pw_aff_from_aff(aff
);
486 } else if (tok
->type
== '(') {
489 res
= accept_affine(s
, isl_space_copy(dim
), v
);
492 if (isl_stream_eat(s
, ')'))
494 } else if (is_start_of_div(tok
)) {
495 isl_stream_push_token(s
, tok
);
497 res
= accept_div(s
, isl_space_copy(dim
), v
);
498 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
499 isl_stream_push_token(s
, tok
);
501 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
503 isl_stream_error(s
, tok
, "expecting factor");
506 if (isl_stream_eat_if_available(s
, '%') ||
507 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
509 return affine_mod(s
, v
, res
);
511 if (isl_stream_eat_if_available(s
, '*')) {
514 isl_int_set_si(f
, 1);
515 if (accept_cst_factor(s
, &f
) < 0) {
519 res
= isl_pw_aff_scale(res
, f
);
522 if (isl_stream_eat_if_available(s
, '/')) {
525 isl_int_set_si(f
, 1);
526 if (accept_cst_factor(s
, &f
) < 0) {
530 res
= isl_pw_aff_scale_down(res
, f
);
539 isl_pw_aff_free(res
);
544 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
549 space
= isl_pw_aff_get_domain_space(pwaff
);
550 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
551 aff
= isl_aff_add_constant(aff
, v
);
553 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
556 /* Return a piecewise affine expression defined on the specified domain
557 * that represents NaN.
559 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
563 ls
= isl_local_space_from_space(isl_space_copy(space
));
564 return isl_pw_aff_nan_on_domain(ls
);
567 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
568 __isl_take isl_space
*space
, struct vars
*v
)
570 struct isl_token
*tok
= NULL
;
575 ls
= isl_local_space_from_space(isl_space_copy(space
));
576 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
583 isl_stream_error(s
, NULL
, "unexpected EOF");
586 if (tok
->type
== '-') {
591 if (tok
->type
== '(' || is_start_of_div(tok
) ||
592 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
593 tok
->type
== ISL_TOKEN_IDENT
||
594 tok
->type
== ISL_TOKEN_AFF
) {
596 isl_stream_push_token(s
, tok
);
598 term
= accept_affine_factor(s
,
599 isl_space_copy(space
), v
);
601 res
= isl_pw_aff_sub(res
, term
);
603 res
= isl_pw_aff_add(res
, term
);
607 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
609 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
610 if (isl_stream_eat_if_available(s
, '*') ||
611 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
613 term
= accept_affine_factor(s
,
614 isl_space_copy(space
), v
);
615 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
616 res
= isl_pw_aff_add(res
, term
);
620 res
= add_cst(res
, tok
->u
.v
);
623 } else if (tok
->type
== ISL_TOKEN_NAN
) {
624 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
626 isl_stream_error(s
, tok
, "unexpected isl_token");
627 isl_stream_push_token(s
, tok
);
628 isl_pw_aff_free(res
);
629 isl_space_free(space
);
635 if (tok
&& tok
->type
== '-') {
638 } else if (tok
&& tok
->type
== '+') {
641 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
642 isl_int_is_neg(tok
->u
.v
)) {
643 isl_stream_push_token(s
, tok
);
646 isl_stream_push_token(s
, tok
);
651 isl_space_free(space
);
654 isl_space_free(space
);
656 isl_pw_aff_free(res
);
660 static int is_comparator(struct isl_token
*tok
)
678 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
679 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
680 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
681 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
683 /* Accept a ternary operator, given the first argument.
685 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
686 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
689 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
694 if (isl_stream_eat(s
, '?'))
697 dim
= isl_space_wrap(isl_map_get_space(cond
));
698 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
702 if (isl_stream_eat(s
, ':'))
705 dim
= isl_pw_aff_get_domain_space(pwaff1
);
706 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
710 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
711 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
714 isl_pw_aff_free(pwaff1
);
715 isl_pw_aff_free(pwaff2
);
719 /* Set *line and *col to those of the next token, if any.
721 static void set_current_line_col(struct isl_stream
*s
, int *line
, int *col
)
723 struct isl_token
*tok
;
725 tok
= isl_stream_next_token(s
);
731 isl_stream_push_token(s
, tok
);
734 /* Push a token encapsulating "pa" onto "s", with the given
737 static int push_aff(struct isl_stream
*s
, int line
, int col
,
738 __isl_take isl_pw_aff
*pa
)
740 struct isl_token
*tok
;
742 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
745 tok
->type
= ISL_TOKEN_AFF
;
747 isl_stream_push_token(s
, tok
);
755 /* Accept an affine expression that may involve ternary operators.
756 * We first read an affine expression.
757 * If it is not followed by a comparison operator, we simply return it.
758 * Otherwise, we assume the affine expression is part of the first
759 * argument of a ternary operator and try to parse that.
761 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
762 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
767 struct isl_token
*tok
;
768 int line
= -1, col
= -1;
771 set_current_line_col(s
, &line
, &col
);
773 pwaff
= accept_affine(s
, dim
, v
);
775 pwaff
= isl_pw_aff_set_rational(pwaff
);
779 tok
= isl_stream_next_token(s
);
781 return isl_pw_aff_free(pwaff
);
783 is_comp
= is_comparator(tok
);
784 isl_stream_push_token(s
, tok
);
788 space
= isl_pw_aff_get_domain_space(pwaff
);
789 cond
= isl_map_universe(isl_space_unwrap(space
));
791 if (push_aff(s
, line
, col
, pwaff
) < 0)
792 cond
= isl_map_free(cond
);
796 cond
= read_formula(s
, v
, cond
, rational
);
798 return accept_ternary(s
, cond
, v
, rational
);
801 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
802 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
809 if (type
== isl_dim_param
)
810 pos
= isl_map_dim(map
, isl_dim_param
);
812 pos
= isl_map_dim(map
, isl_dim_in
);
813 if (type
== isl_dim_out
)
814 pos
+= isl_map_dim(map
, isl_dim_out
);
819 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
821 def_map
= isl_map_from_pw_aff(def
);
822 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
823 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
825 map
= isl_map_intersect(map
, def_map
);
830 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
831 __isl_take isl_space
*dim
, struct vars
*v
)
834 isl_pw_aff_list
*list
;
835 struct isl_token
*tok
= NULL
;
837 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
838 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
843 tok
= isl_stream_next_token(s
);
845 isl_stream_error(s
, NULL
, "unexpected EOF");
848 if (tok
->type
!= ',') {
849 isl_stream_push_token(s
, tok
);
854 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
855 list
= isl_pw_aff_list_concat(list
,
856 isl_pw_aff_list_from_pw_aff(pwaff
));
865 isl_pw_aff_list_free(list
);
869 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
870 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
872 struct isl_token
*tok
;
874 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
878 if (tok
->type
!= ISL_TOKEN_IDENT
)
881 p
= vars_pos(v
, tok
->u
.s
, -1);
885 isl_stream_error(s
, tok
, "expecting unique identifier");
889 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
892 tok
= isl_stream_next_token(s
);
893 if (tok
&& tok
->type
== '=') {
895 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
896 tok
= isl_stream_next_token(s
);
899 if (!tok
|| tok
->type
!= ',')
905 isl_stream_push_token(s
, tok
);
914 static int next_is_tuple(struct isl_stream
*s
)
916 struct isl_token
*tok
;
919 tok
= isl_stream_next_token(s
);
922 if (tok
->type
== '[') {
923 isl_stream_push_token(s
, tok
);
926 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
927 isl_stream_push_token(s
, tok
);
931 is_tuple
= isl_stream_next_token_is(s
, '[');
933 isl_stream_push_token(s
, tok
);
938 /* Allocate an initial tuple with zero dimensions and an anonymous,
939 * unstructured space.
940 * A tuple is represented as an isl_multi_pw_aff.
941 * The range space is the space of the tuple.
942 * The domain space is an anonymous space
943 * with a dimension for each variable in the set of variables in "v".
944 * If a given dimension is not defined in terms of earlier dimensions in
945 * the input, then the corresponding isl_pw_aff is set equal to one time
946 * the variable corresponding to the dimension being defined.
948 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
950 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
953 /* Is "pa" an expression in term of earlier dimensions?
954 * The alternative is that the dimension is defined to be equal to itself,
955 * meaning that it has a universe domain and an expression that depends
956 * on itself. "i" is the position of the expression in a sequence
957 * of "n" expressions. The final dimensions of "pa" correspond to
958 * these "n" expressions.
960 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
968 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
972 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
977 /* Does the tuple contain any dimensions that are defined
978 * in terms of earlier dimensions?
980 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
988 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
989 for (i
= 0; i
< n
; ++i
) {
990 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
991 has_expr
= pw_aff_is_expr(pa
, i
, n
);
993 if (has_expr
< 0 || has_expr
)
1000 /* Add a dimension to the given tuple.
1001 * The dimension is initially undefined, so it is encoded
1002 * as one times itself.
1004 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
1005 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
1011 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
1012 space
= isl_multi_pw_aff_get_domain_space(tuple
);
1013 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1014 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
1015 pa
= isl_pw_aff_from_aff(aff
);
1016 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
1017 isl_multi_pw_aff_from_pw_aff(pa
));
1022 /* Set the name of dimension "pos" in "tuple" to "name".
1023 * During printing, we add primes if the same name appears more than once
1024 * to distinguish the occurrences. Here, we remove those primes from "name"
1025 * before setting the name of the dimension.
1027 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
1028 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
1035 prime
= strchr(name
, '\'');
1038 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
1045 /* Accept a piecewise affine expression.
1047 * At the outer level, the piecewise affine expression may be of the form
1049 * aff1 : condition1; aff2 : conditions2; ...
1055 * each of the affine expressions may in turn include ternary operators.
1057 * There may be parentheses around some subexpression of "aff1"
1058 * around "aff1" itself, around "aff1 : condition1" and/or
1059 * around the entire piecewise affine expression.
1060 * We therefore remove the opening parenthesis (if any) from the stream
1061 * in case the closing parenthesis follows the colon, but if the closing
1062 * parenthesis is the first thing in the stream after the parsed affine
1063 * expression, we push the parsed expression onto the stream and parse
1064 * again in case the parentheses enclose some subexpression of "aff1".
1066 static __isl_give isl_pw_aff
*accept_piecewise_affine(struct isl_stream
*s
,
1067 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1070 isl_space
*res_space
;
1072 res_space
= isl_space_from_domain(isl_space_copy(space
));
1073 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1074 res
= isl_pw_aff_empty(res_space
);
1078 int line
= -1, col
= -1;
1080 set_current_line_col(s
, &line
, &col
);
1081 seen_paren
= isl_stream_eat_if_available(s
, '(');
1083 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1086 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1088 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1090 if (push_aff(s
, line
, col
, pa
) < 0)
1092 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1095 if (isl_stream_eat_if_available(s
, ':')) {
1096 isl_space
*dom_space
;
1099 dom_space
= isl_pw_aff_get_domain_space(pa
);
1100 dom
= isl_set_universe(dom_space
);
1101 dom
= read_formula(s
, v
, dom
, rational
);
1102 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1105 res
= isl_pw_aff_union_add(res
, pa
);
1107 if (seen_paren
&& isl_stream_eat(s
, ')'))
1109 } while (isl_stream_eat_if_available(s
, ';'));
1111 isl_space_free(space
);
1115 isl_space_free(space
);
1116 return isl_pw_aff_free(res
);
1119 /* Read an affine expression from "s" and replace the definition
1120 * of dimension "pos" in "tuple" by this expression.
1122 * accept_extended_affine requires a wrapped space as input.
1123 * The domain space of "tuple", on the other hand is an anonymous space,
1124 * so we have to adjust the space of the isl_pw_aff before adding it
1127 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
1128 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
1134 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1136 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1138 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1139 def
= isl_pw_aff_reset_domain_space(def
, space
);
1140 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
1145 /* Read a list of variables and/or affine expressions and return the list
1146 * as an isl_multi_pw_aff.
1147 * The elements in the list are separated by either "," or "][".
1148 * If "comma" is set then only "," is allowed.
1150 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
1151 struct vars
*v
, int rational
, int comma
)
1154 struct isl_token
*tok
;
1155 isl_multi_pw_aff
*res
;
1157 res
= tuple_alloc(v
);
1159 if (isl_stream_next_token_is(s
, ']'))
1162 while ((tok
= next_token(s
)) != NULL
) {
1165 res
= tuple_add_dim(res
, v
);
1167 if (tok
->type
== ISL_TOKEN_IDENT
) {
1169 int p
= vars_pos(v
, tok
->u
.s
, -1);
1175 if (tok
->type
== '*') {
1176 if (vars_add_anon(v
) < 0)
1178 isl_token_free(tok
);
1179 } else if (new_name
) {
1180 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
1181 isl_token_free(tok
);
1182 if (isl_stream_eat_if_available(s
, '='))
1183 res
= read_tuple_var_def(s
, res
, i
, v
,
1186 isl_stream_push_token(s
, tok
);
1188 if (vars_add_anon(v
) < 0)
1190 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
1193 tok
= isl_stream_next_token(s
);
1194 if (!comma
&& tok
&& tok
->type
== ']' &&
1195 isl_stream_next_token_is(s
, '[')) {
1196 isl_token_free(tok
);
1197 tok
= isl_stream_next_token(s
);
1198 } else if (!tok
|| tok
->type
!= ',')
1201 isl_token_free(tok
);
1205 isl_stream_push_token(s
, tok
);
1209 isl_token_free(tok
);
1210 return isl_multi_pw_aff_free(res
);
1213 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1215 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1216 struct vars
*v
, int rational
, int comma
)
1218 struct isl_token
*tok
;
1220 isl_multi_pw_aff
*res
= NULL
;
1222 tok
= isl_stream_next_token(s
);
1225 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1226 name
= strdup(tok
->u
.s
);
1227 isl_token_free(tok
);
1231 isl_stream_push_token(s
, tok
);
1232 if (isl_stream_eat(s
, '['))
1234 if (next_is_tuple(s
)) {
1235 isl_multi_pw_aff
*out
;
1237 res
= read_tuple(s
, v
, rational
, comma
);
1238 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1240 out
= read_tuple(s
, v
, rational
, comma
);
1241 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1242 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1243 res
= isl_multi_pw_aff_range_product(res
, out
);
1245 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1246 if (isl_stream_eat(s
, ']'))
1250 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1257 return isl_multi_pw_aff_free(res
);
1260 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1261 * We first create the appropriate space in "map" based on the range
1262 * space of this isl_multi_pw_aff. Then, we add equalities based
1263 * on the affine expressions. These live in an anonymous space,
1264 * however, so we first need to reset the space to that of "map".
1266 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1267 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1272 isl_space
*space
= NULL
;
1276 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1277 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1278 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1282 if (type
== isl_dim_param
) {
1283 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1284 isl_space_is_wrapping(space
)) {
1285 isl_die(ctx
, isl_error_invalid
,
1286 "parameter tuples cannot be named or nested",
1289 map
= isl_map_add_dims(map
, type
, n
);
1290 for (i
= 0; i
< n
; ++i
) {
1292 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1293 isl_die(ctx
, isl_error_invalid
,
1294 "parameters must be named",
1296 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1297 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1299 } else if (type
== isl_dim_in
) {
1302 set
= isl_set_universe(isl_space_copy(space
));
1304 set
= isl_set_set_rational(set
);
1305 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1306 map
= isl_map_from_domain(set
);
1310 set
= isl_set_universe(isl_space_copy(space
));
1312 set
= isl_set_set_rational(set
);
1313 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1316 for (i
= 0; i
< n
; ++i
) {
1323 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1324 space
= isl_pw_aff_get_domain_space(pa
);
1325 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1326 aff
= isl_aff_add_coefficient_si(aff
,
1327 isl_dim_in
, v
->n
- n
+ i
, -1);
1328 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1330 pa
= isl_pw_aff_set_rational(pa
);
1331 set
= isl_pw_aff_zero_set(pa
);
1332 map_i
= isl_map_from_range(set
);
1333 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1334 map
= isl_map_intersect(map
, map_i
);
1337 isl_space_free(space
);
1338 isl_multi_pw_aff_free(tuple
);
1341 isl_space_free(space
);
1342 isl_multi_pw_aff_free(tuple
);
1347 /* Read a tuple from "s" and add it to "map".
1348 * The tuple is initially represented as an isl_multi_pw_aff and
1349 * then added to "map".
1351 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1352 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1353 int rational
, int comma
)
1355 isl_multi_pw_aff
*tuple
;
1357 tuple
= read_tuple(s
, v
, rational
, comma
);
1359 return isl_map_free(map
);
1361 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1364 static __isl_give isl_set
*construct_constraints(
1365 __isl_take isl_set
*set
, int type
,
1366 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1371 left
= isl_pw_aff_list_copy(left
);
1372 right
= isl_pw_aff_list_copy(right
);
1374 left
= isl_pw_aff_list_set_rational(left
);
1375 right
= isl_pw_aff_list_set_rational(right
);
1377 if (type
== ISL_TOKEN_LE
)
1378 cond
= isl_pw_aff_list_le_set(left
, right
);
1379 else if (type
== ISL_TOKEN_GE
)
1380 cond
= isl_pw_aff_list_ge_set(left
, right
);
1381 else if (type
== ISL_TOKEN_LT
)
1382 cond
= isl_pw_aff_list_lt_set(left
, right
);
1383 else if (type
== ISL_TOKEN_GT
)
1384 cond
= isl_pw_aff_list_gt_set(left
, right
);
1385 else if (type
== ISL_TOKEN_NE
)
1386 cond
= isl_pw_aff_list_ne_set(left
, right
);
1388 cond
= isl_pw_aff_list_eq_set(left
, right
);
1390 return isl_set_intersect(set
, cond
);
1393 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1394 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1396 struct isl_token
*tok
= NULL
;
1397 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1400 set
= isl_map_wrap(map
);
1401 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1404 tok
= isl_stream_next_token(s
);
1405 if (!is_comparator(tok
)) {
1406 isl_stream_error(s
, tok
, "missing operator");
1408 isl_stream_push_token(s
, tok
);
1413 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1417 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1419 isl_token_free(tok
);
1420 isl_pw_aff_list_free(list1
);
1423 tok
= isl_stream_next_token(s
);
1424 if (!is_comparator(tok
)) {
1426 isl_stream_push_token(s
, tok
);
1430 isl_pw_aff_list_free(list1
);
1432 return isl_set_unwrap(set
);
1435 isl_token_free(tok
);
1436 isl_pw_aff_list_free(list1
);
1437 isl_pw_aff_list_free(list2
);
1442 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1443 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1446 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1448 map
= isl_map_from_domain(isl_map_wrap(map
));
1449 map
= read_defined_var_list(s
, v
, map
, rational
);
1451 if (isl_stream_eat(s
, ':'))
1454 map
= read_formula(s
, v
, map
, rational
);
1455 map
= isl_set_unwrap(isl_map_domain(map
));
1457 vars_drop(v
, v
->n
- n
);
1458 if (seen_paren
&& isl_stream_eat(s
, ')'))
1467 /* Parse an expression between parentheses and push the result
1468 * back on the stream.
1470 * The parsed expression may be either an affine expression
1471 * or a condition. The first type is pushed onto the stream
1472 * as an isl_pw_aff, while the second is pushed as an isl_map.
1474 * If the initial token indicates the start of a condition,
1475 * we parse it as such.
1476 * Otherwise, we first parse an affine expression and push
1477 * that onto the stream. If the affine expression covers the
1478 * entire expression between parentheses, we return.
1479 * Otherwise, we assume that the affine expression is the
1480 * start of a condition and continue parsing.
1482 static int resolve_paren_expr(struct isl_stream
*s
,
1483 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1485 struct isl_token
*tok
, *tok2
;
1489 tok
= isl_stream_next_token(s
);
1490 if (!tok
|| tok
->type
!= '(')
1493 if (isl_stream_next_token_is(s
, '('))
1494 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1497 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1498 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1499 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1500 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1501 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1502 map
= read_formula(s
, v
, map
, rational
);
1503 if (isl_stream_eat(s
, ')'))
1505 tok
->type
= ISL_TOKEN_MAP
;
1507 isl_stream_push_token(s
, tok
);
1511 tok2
= isl_stream_next_token(s
);
1516 isl_stream_push_token(s
, tok2
);
1518 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1522 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1525 tok2
->type
= ISL_TOKEN_AFF
;
1526 tok2
->u
.pwaff
= pwaff
;
1528 if (isl_stream_eat_if_available(s
, ')')) {
1529 isl_stream_push_token(s
, tok2
);
1530 isl_token_free(tok
);
1535 isl_stream_push_token(s
, tok2
);
1537 map
= read_formula(s
, v
, map
, rational
);
1538 if (isl_stream_eat(s
, ')'))
1541 tok
->type
= ISL_TOKEN_MAP
;
1543 isl_stream_push_token(s
, tok
);
1547 isl_pw_aff_free(pwaff
);
1549 isl_token_free(tok
);
1554 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1555 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1557 if (isl_stream_next_token_is(s
, '('))
1558 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1561 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1562 struct isl_token
*tok
;
1563 tok
= isl_stream_next_token(s
);
1567 map
= isl_map_copy(tok
->u
.map
);
1568 isl_token_free(tok
);
1572 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1573 return read_exists(s
, v
, map
, rational
);
1575 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1578 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1579 isl_space
*dim
= isl_map_get_space(map
);
1581 return isl_map_empty(dim
);
1584 return add_constraint(s
, v
, map
, rational
);
1590 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1591 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1596 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1597 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1599 res
= isl_map_subtract(isl_map_copy(map
), res
);
1601 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1604 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1605 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1607 res
= isl_map_subtract(res
, res_i
);
1609 res
= isl_map_intersect(res
, res_i
);
1616 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1617 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1621 if (isl_stream_next_token_is(s
, '}')) {
1622 isl_space
*dim
= isl_map_get_space(map
);
1624 return isl_map_universe(dim
);
1627 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1628 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1631 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1632 res
= isl_map_union(res
, res_i
);
1639 /* Read a first order formula from "s", add the corresponding
1640 * constraints to "map" and return the result.
1642 * In particular, read a formula of the form
1650 * where a and b are disjunctions.
1652 * In the first case, map is replaced by
1654 * map \cap { [..] : a }
1656 * In the second case, it is replaced by
1658 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1660 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
1661 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1665 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1667 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1670 res
= isl_map_subtract(isl_map_copy(map
), res
);
1671 res2
= read_disjuncts(s
, v
, map
, rational
);
1672 res
= isl_map_union(res
, res2
);
1679 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1681 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1682 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1683 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1684 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1686 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1687 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1688 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1690 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1691 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1692 isl_basic_map_dim(bmap
, isl_dim_in
) +
1693 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1694 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1696 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1702 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1703 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1706 struct isl_token
*tok
;
1716 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1717 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1719 tok
= isl_stream_next_token(s
);
1720 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1721 isl_stream_error(s
, tok
, "expecting coefficient");
1723 isl_stream_push_token(s
, tok
);
1726 if (!tok
->on_new_line
) {
1727 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1728 isl_stream_push_token(s
, tok
);
1732 type
= isl_int_get_si(tok
->u
.v
);
1733 isl_token_free(tok
);
1735 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1737 k
= isl_basic_map_alloc_equality(bmap
);
1740 k
= isl_basic_map_alloc_inequality(bmap
);
1746 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1748 tok
= isl_stream_next_token(s
);
1749 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1750 isl_stream_error(s
, tok
, "expecting coefficient");
1752 isl_stream_push_token(s
, tok
);
1755 if (tok
->on_new_line
) {
1756 isl_stream_error(s
, tok
,
1757 "coefficient should not appear on new line");
1758 isl_stream_push_token(s
, tok
);
1761 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1762 isl_int_set(c
[pos
], tok
->u
.v
);
1763 isl_token_free(tok
);
1768 isl_basic_map_free(bmap
);
1772 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1775 struct isl_token
*tok
;
1776 struct isl_token
*tok2
;
1779 unsigned in
= 0, out
, local
= 0;
1780 struct isl_basic_map
*bmap
= NULL
;
1783 tok
= isl_stream_next_token(s
);
1785 isl_stream_error(s
, NULL
, "unexpected EOF");
1788 tok2
= isl_stream_next_token(s
);
1790 isl_token_free(tok
);
1791 isl_stream_error(s
, NULL
, "unexpected EOF");
1794 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1795 isl_stream_push_token(s
, tok2
);
1796 isl_stream_push_token(s
, tok
);
1797 isl_stream_error(s
, NULL
,
1798 "expecting constraint matrix dimensions");
1801 n_row
= isl_int_get_si(tok
->u
.v
);
1802 n_col
= isl_int_get_si(tok2
->u
.v
);
1803 on_new_line
= tok2
->on_new_line
;
1804 isl_token_free(tok2
);
1805 isl_token_free(tok
);
1806 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1807 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1808 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1809 tok
= isl_stream_next_token_on_same_line(s
);
1811 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1812 isl_stream_error(s
, tok
,
1813 "expecting number of output dimensions");
1814 isl_stream_push_token(s
, tok
);
1817 out
= isl_int_get_si(tok
->u
.v
);
1818 isl_token_free(tok
);
1820 tok
= isl_stream_next_token_on_same_line(s
);
1821 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1822 isl_stream_error(s
, tok
,
1823 "expecting number of input dimensions");
1825 isl_stream_push_token(s
, tok
);
1828 in
= isl_int_get_si(tok
->u
.v
);
1829 isl_token_free(tok
);
1831 tok
= isl_stream_next_token_on_same_line(s
);
1832 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1833 isl_stream_error(s
, tok
,
1834 "expecting number of existentials");
1836 isl_stream_push_token(s
, tok
);
1839 local
= isl_int_get_si(tok
->u
.v
);
1840 isl_token_free(tok
);
1842 tok
= isl_stream_next_token_on_same_line(s
);
1843 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1844 isl_stream_error(s
, tok
,
1845 "expecting number of parameters");
1847 isl_stream_push_token(s
, tok
);
1850 nparam
= isl_int_get_si(tok
->u
.v
);
1851 isl_token_free(tok
);
1852 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1853 isl_stream_error(s
, NULL
,
1854 "dimensions don't match");
1858 out
= n_col
- 2 - nparam
;
1859 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1863 for (i
= 0; i
< local
; ++i
) {
1864 int k
= isl_basic_map_alloc_div(bmap
);
1867 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1870 for (i
= 0; i
< n_row
; ++i
)
1871 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1873 tok
= isl_stream_next_token_on_same_line(s
);
1875 isl_stream_error(s
, tok
, "unexpected extra token on line");
1876 isl_stream_push_token(s
, tok
);
1880 bmap
= isl_basic_map_simplify(bmap
);
1881 bmap
= isl_basic_map_finalize(bmap
);
1884 isl_basic_map_free(bmap
);
1888 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1890 struct isl_token
*tok
;
1891 struct isl_token
*tok2
;
1893 struct isl_map
*map
;
1895 tok
= isl_stream_next_token(s
);
1897 isl_stream_error(s
, NULL
, "unexpected EOF");
1900 tok2
= isl_stream_next_token_on_same_line(s
);
1901 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1902 isl_stream_push_token(s
, tok2
);
1903 isl_stream_push_token(s
, tok
);
1904 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1907 isl_stream_error(s
, tok2
, "unexpected token");
1908 isl_stream_push_token(s
, tok2
);
1909 isl_stream_push_token(s
, tok
);
1912 n
= isl_int_get_si(tok
->u
.v
);
1913 isl_token_free(tok
);
1915 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1917 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1919 for (i
= 1; map
&& i
< n
; ++i
)
1920 map
= isl_map_union(map
,
1921 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1926 static int optional_power(struct isl_stream
*s
)
1929 struct isl_token
*tok
;
1931 tok
= isl_stream_next_token(s
);
1934 if (tok
->type
!= '^') {
1935 isl_stream_push_token(s
, tok
);
1938 isl_token_free(tok
);
1939 tok
= isl_stream_next_token(s
);
1940 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1941 isl_stream_error(s
, tok
, "expecting exponent");
1943 isl_stream_push_token(s
, tok
);
1946 pow
= isl_int_get_si(tok
->u
.v
);
1947 isl_token_free(tok
);
1951 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1952 __isl_keep isl_map
*map
, struct vars
*v
);
1954 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1955 __isl_keep isl_map
*map
, struct vars
*v
)
1957 isl_pw_qpolynomial
*pwqp
;
1958 struct isl_token
*tok
;
1960 tok
= next_token(s
);
1962 isl_stream_error(s
, NULL
, "unexpected EOF");
1965 if (tok
->type
== '(') {
1968 isl_token_free(tok
);
1969 pwqp
= read_term(s
, map
, v
);
1972 if (isl_stream_eat(s
, ')'))
1974 pow
= optional_power(s
);
1975 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1976 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1977 struct isl_token
*tok2
;
1978 isl_qpolynomial
*qp
;
1980 tok2
= isl_stream_next_token(s
);
1981 if (tok2
&& tok2
->type
== '/') {
1982 isl_token_free(tok2
);
1983 tok2
= next_token(s
);
1984 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1985 isl_stream_error(s
, tok2
, "expected denominator");
1986 isl_token_free(tok
);
1987 isl_token_free(tok2
);
1990 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1991 tok
->u
.v
, tok2
->u
.v
);
1992 isl_token_free(tok2
);
1994 isl_stream_push_token(s
, tok2
);
1995 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1998 isl_token_free(tok
);
1999 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2000 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2001 isl_qpolynomial
*qp
;
2002 isl_token_free(tok
);
2003 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2004 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2005 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2006 isl_qpolynomial
*qp
;
2007 isl_token_free(tok
);
2008 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2009 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2010 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2012 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2014 isl_qpolynomial
*qp
;
2016 isl_token_free(tok
);
2020 vars_drop(v
, v
->n
- n
);
2021 isl_stream_error(s
, tok
, "unknown identifier");
2022 isl_token_free(tok
);
2025 isl_token_free(tok
);
2026 pow
= optional_power(s
);
2027 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2028 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2029 } else if (is_start_of_div(tok
)) {
2033 isl_stream_push_token(s
, tok
);
2034 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2035 pow
= optional_power(s
);
2036 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2037 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2038 } else if (tok
->type
== '-') {
2039 isl_token_free(tok
);
2040 pwqp
= read_factor(s
, map
, v
);
2041 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2043 isl_stream_error(s
, tok
, "unexpected isl_token");
2044 isl_stream_push_token(s
, tok
);
2048 if (isl_stream_eat_if_available(s
, '*') ||
2049 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2050 isl_pw_qpolynomial
*pwqp2
;
2052 pwqp2
= read_factor(s
, map
, v
);
2053 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2058 isl_pw_qpolynomial_free(pwqp
);
2062 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
2063 __isl_keep isl_map
*map
, struct vars
*v
)
2065 struct isl_token
*tok
;
2066 isl_pw_qpolynomial
*pwqp
;
2068 pwqp
= read_factor(s
, map
, v
);
2071 tok
= next_token(s
);
2075 if (tok
->type
== '+') {
2076 isl_pw_qpolynomial
*pwqp2
;
2078 isl_token_free(tok
);
2079 pwqp2
= read_factor(s
, map
, v
);
2080 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2081 } else if (tok
->type
== '-') {
2082 isl_pw_qpolynomial
*pwqp2
;
2084 isl_token_free(tok
);
2085 pwqp2
= read_factor(s
, map
, v
);
2086 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2087 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2088 isl_int_is_neg(tok
->u
.v
)) {
2089 isl_pw_qpolynomial
*pwqp2
;
2091 isl_stream_push_token(s
, tok
);
2092 pwqp2
= read_factor(s
, map
, v
);
2093 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2095 isl_stream_push_token(s
, tok
);
2103 static __isl_give isl_map
*read_optional_formula(struct isl_stream
*s
,
2104 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2106 struct isl_token
*tok
;
2108 tok
= isl_stream_next_token(s
);
2110 isl_stream_error(s
, NULL
, "unexpected EOF");
2113 if (tok
->type
== ':' ||
2114 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2115 isl_token_free(tok
);
2116 map
= read_formula(s
, v
, map
, rational
);
2118 isl_stream_push_token(s
, tok
);
2126 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
2127 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2129 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2130 isl_pw_qpolynomial
*pwqp
;
2131 struct isl_set
*set
;
2133 pwqp
= read_term(s
, map
, v
);
2134 map
= read_optional_formula(s
, map
, v
, 0);
2135 set
= isl_map_range(map
);
2137 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2139 vars_drop(v
, v
->n
- n
);
2145 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
2146 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2148 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2149 isl_pw_qpolynomial
*pwqp
;
2150 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2152 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2153 return obj_read_poly(s
, set
, v
, n
);
2155 if (isl_stream_eat(s
, '('))
2158 pwqp
= read_term(s
, set
, v
);
2159 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2161 while (isl_stream_eat_if_available(s
, ',')) {
2162 isl_pw_qpolynomial_fold
*pwf_i
;
2163 pwqp
= read_term(s
, set
, v
);
2164 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2166 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2169 if (isl_stream_eat(s
, ')'))
2172 set
= read_optional_formula(s
, set
, v
, 0);
2173 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2175 vars_drop(v
, v
->n
- n
);
2181 isl_pw_qpolynomial_fold_free(pwf
);
2182 obj
.type
= isl_obj_none
;
2186 static int is_rational(struct isl_stream
*s
)
2188 struct isl_token
*tok
;
2190 tok
= isl_stream_next_token(s
);
2193 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2194 isl_token_free(tok
);
2195 isl_stream_eat(s
, ':');
2199 isl_stream_push_token(s
, tok
);
2204 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2205 __isl_take isl_map
*map
, struct vars
*v
)
2207 struct isl_token
*tok
;
2208 struct isl_obj obj
= { isl_obj_set
, NULL
};
2212 rational
= is_rational(s
);
2214 map
= isl_map_set_rational(map
);
2216 if (isl_stream_next_token_is(s
, ':')) {
2217 obj
.type
= isl_obj_set
;
2218 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2222 if (!next_is_tuple(s
))
2223 return obj_read_poly_or_fold(s
, map
, v
, n
);
2225 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2228 tok
= isl_stream_next_token(s
);
2231 if (tok
->type
== ISL_TOKEN_TO
) {
2232 obj
.type
= isl_obj_map
;
2233 isl_token_free(tok
);
2234 if (!next_is_tuple(s
)) {
2235 isl_set
*set
= isl_map_domain(map
);
2236 return obj_read_poly_or_fold(s
, set
, v
, n
);
2238 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2242 map
= isl_map_domain(map
);
2243 isl_stream_push_token(s
, tok
);
2246 map
= read_optional_formula(s
, map
, v
, rational
);
2248 vars_drop(v
, v
->n
- n
);
2254 obj
.type
= isl_obj_none
;
2258 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2260 if (obj
.type
== isl_obj_map
) {
2261 obj
.v
= isl_union_map_from_map(obj
.v
);
2262 obj
.type
= isl_obj_union_map
;
2263 } else if (obj
.type
== isl_obj_set
) {
2264 obj
.v
= isl_union_set_from_set(obj
.v
);
2265 obj
.type
= isl_obj_union_set
;
2266 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2267 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2268 obj
.type
= isl_obj_union_pw_qpolynomial
;
2269 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2270 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2271 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2273 isl_assert(ctx
, 0, goto error
);
2276 obj
.type
->free(obj
.v
);
2277 obj
.type
= isl_obj_none
;
2281 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2282 struct isl_obj obj1
, struct isl_obj obj2
)
2284 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2285 obj1
= to_union(ctx
, obj1
);
2286 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2287 obj2
= to_union(ctx
, obj2
);
2288 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2289 obj1
= to_union(ctx
, obj1
);
2290 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2291 obj2
= to_union(ctx
, obj2
);
2292 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2293 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2294 obj1
= to_union(ctx
, obj1
);
2295 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2296 obj2
.type
== isl_obj_pw_qpolynomial
)
2297 obj2
= to_union(ctx
, obj2
);
2298 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2299 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2300 obj1
= to_union(ctx
, obj1
);
2301 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2302 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2303 obj2
= to_union(ctx
, obj2
);
2304 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2305 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2306 obj1
= to_union(ctx
, obj1
);
2307 obj2
= to_union(ctx
, obj2
);
2309 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2310 obj1
= to_union(ctx
, obj1
);
2311 obj2
= to_union(ctx
, obj2
);
2313 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2314 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2315 obj1
= to_union(ctx
, obj1
);
2316 obj2
= to_union(ctx
, obj2
);
2318 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2319 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2320 obj1
= to_union(ctx
, obj1
);
2321 obj2
= to_union(ctx
, obj2
);
2323 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2326 obj1
.type
->free(obj1
.v
);
2327 obj2
.type
->free(obj2
.v
);
2328 obj1
.type
= isl_obj_none
;
2333 static struct isl_obj
obj_read(struct isl_stream
*s
)
2335 isl_map
*map
= NULL
;
2336 struct isl_token
*tok
;
2337 struct vars
*v
= NULL
;
2338 struct isl_obj obj
= { isl_obj_set
, NULL
};
2340 tok
= next_token(s
);
2342 isl_stream_error(s
, NULL
, "unexpected EOF");
2345 if (tok
->type
== ISL_TOKEN_VALUE
) {
2346 struct isl_token
*tok2
;
2347 struct isl_map
*map
;
2349 tok2
= isl_stream_next_token(s
);
2350 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2351 isl_int_is_neg(tok2
->u
.v
)) {
2353 isl_stream_push_token(s
, tok2
);
2354 obj
.type
= isl_obj_val
;
2355 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2356 isl_token_free(tok
);
2359 isl_stream_push_token(s
, tok2
);
2360 isl_stream_push_token(s
, tok
);
2361 map
= map_read_polylib(s
);
2364 if (isl_map_may_be_set(map
))
2365 obj
.v
= isl_map_range(map
);
2367 obj
.type
= isl_obj_map
;
2372 v
= vars_new(s
->ctx
);
2374 isl_stream_push_token(s
, tok
);
2377 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2378 if (tok
->type
== '[') {
2379 isl_stream_push_token(s
, tok
);
2380 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2383 tok
= isl_stream_next_token(s
);
2384 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2385 isl_stream_error(s
, tok
, "expecting '->'");
2387 isl_stream_push_token(s
, tok
);
2390 isl_token_free(tok
);
2391 tok
= isl_stream_next_token(s
);
2393 if (!tok
|| tok
->type
!= '{') {
2394 isl_stream_error(s
, tok
, "expecting '{'");
2396 isl_stream_push_token(s
, tok
);
2399 isl_token_free(tok
);
2401 tok
= isl_stream_next_token(s
);
2404 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2405 isl_token_free(tok
);
2406 if (isl_stream_eat(s
, '='))
2408 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2411 } else if (tok
->type
== '}') {
2412 obj
.type
= isl_obj_union_set
;
2413 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2414 isl_token_free(tok
);
2417 isl_stream_push_token(s
, tok
);
2422 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2423 if (o
.type
== isl_obj_none
|| !o
.v
)
2428 obj
= obj_add(s
->ctx
, obj
, o
);
2429 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2432 tok
= isl_stream_next_token(s
);
2433 if (!tok
|| tok
->type
!= ';')
2435 isl_token_free(tok
);
2436 if (isl_stream_next_token_is(s
, '}')) {
2437 tok
= isl_stream_next_token(s
);
2442 if (tok
&& tok
->type
== '}') {
2443 isl_token_free(tok
);
2445 isl_stream_error(s
, tok
, "unexpected isl_token");
2447 isl_token_free(tok
);
2457 obj
.type
->free(obj
.v
);
2464 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2469 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2475 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2476 obj
.type
== isl_obj_set
, goto error
);
2478 if (obj
.type
== isl_obj_set
)
2479 obj
.v
= isl_map_from_range(obj
.v
);
2483 obj
.type
->free(obj
.v
);
2487 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2493 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2494 obj
.v
= isl_map_range(obj
.v
);
2495 obj
.type
= isl_obj_set
;
2497 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2502 obj
.type
->free(obj
.v
);
2506 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2511 if (obj
.type
== isl_obj_map
) {
2512 obj
.type
= isl_obj_union_map
;
2513 obj
.v
= isl_union_map_from_map(obj
.v
);
2515 if (obj
.type
== isl_obj_set
) {
2516 obj
.type
= isl_obj_union_set
;
2517 obj
.v
= isl_union_set_from_set(obj
.v
);
2519 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2520 isl_union_set_is_empty(obj
.v
))
2521 obj
.type
= isl_obj_union_map
;
2522 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2523 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2527 obj
.type
->free(obj
.v
);
2531 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2536 if (obj
.type
== isl_obj_set
) {
2537 obj
.type
= isl_obj_union_set
;
2538 obj
.v
= isl_union_set_from_set(obj
.v
);
2541 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2545 obj
.type
->free(obj
.v
);
2549 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2552 struct isl_map
*map
;
2553 struct isl_basic_map
*bmap
;
2561 isl_die(s
->ctx
, isl_error_invalid
,
2562 "set or map description involves "
2563 "more than one disjunct", goto error
);
2566 bmap
= isl_basic_map_empty_like_map(map
);
2568 bmap
= isl_basic_map_copy(map
->p
[0]);
2578 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2580 isl_basic_map
*bmap
;
2581 bmap
= basic_map_read(s
);
2584 if (!isl_basic_map_may_be_set(bmap
))
2585 isl_die(s
->ctx
, isl_error_invalid
,
2586 "input is not a set", goto error
);
2587 return isl_basic_map_range(bmap
);
2589 isl_basic_map_free(bmap
);
2593 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2596 struct isl_basic_map
*bmap
;
2597 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2600 bmap
= basic_map_read(s
);
2605 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2608 isl_basic_set
*bset
;
2609 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2612 bset
= basic_set_read(s
);
2617 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2620 struct isl_basic_map
*bmap
;
2621 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2624 bmap
= basic_map_read(s
);
2629 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2632 isl_basic_set
*bset
;
2633 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2636 bset
= basic_set_read(s
);
2641 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2644 struct isl_map
*map
;
2645 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2648 map
= isl_stream_read_map(s
);
2653 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2656 struct isl_map
*map
;
2657 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2660 map
= isl_stream_read_map(s
);
2665 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2669 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2672 set
= isl_stream_read_set(s
);
2677 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2681 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2684 set
= isl_stream_read_set(s
);
2689 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2692 isl_union_map
*umap
;
2693 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2696 umap
= isl_stream_read_union_map(s
);
2701 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2704 isl_union_map
*umap
;
2705 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2708 umap
= isl_stream_read_union_map(s
);
2713 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2716 isl_union_set
*uset
;
2717 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2720 uset
= isl_stream_read_union_set(s
);
2725 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2728 isl_union_set
*uset
;
2729 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2732 uset
= isl_stream_read_union_set(s
);
2737 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2739 struct isl_vec
*vec
= NULL
;
2740 struct isl_token
*tok
;
2744 tok
= isl_stream_next_token(s
);
2745 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2746 isl_stream_error(s
, tok
, "expecting vector length");
2750 size
= isl_int_get_si(tok
->u
.v
);
2751 isl_token_free(tok
);
2753 vec
= isl_vec_alloc(s
->ctx
, size
);
2755 for (j
= 0; j
< size
; ++j
) {
2756 tok
= isl_stream_next_token(s
);
2757 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2758 isl_stream_error(s
, tok
, "expecting constant value");
2761 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2762 isl_token_free(tok
);
2767 isl_token_free(tok
);
2772 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2774 return isl_vec_read_polylib(s
);
2777 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2780 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2788 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2789 struct isl_stream
*s
)
2795 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2800 obj
.type
->free(obj
.v
);
2804 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2807 isl_pw_qpolynomial
*pwqp
;
2808 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2811 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2816 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2819 isl_pw_qpolynomial
*pwqp
;
2820 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2823 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2828 /* Is the next token an identifer not in "v"?
2830 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2834 struct isl_token
*tok
;
2836 tok
= isl_stream_next_token(s
);
2839 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2840 isl_stream_push_token(s
, tok
);
2842 vars_drop(v
, v
->n
- n
);
2847 /* First read the domain of the affine expression, which may be
2848 * a parameter space or a set.
2849 * The tricky part is that we don't know if the domain is a set or not,
2850 * so when we are trying to read the domain, we may actually be reading
2851 * the affine expression itself (defined on a parameter domains)
2852 * If the tuple we are reading is named, we assume it's the domain.
2853 * Also, if inside the tuple, the first thing we find is a nested tuple
2854 * or a new identifier, we again assume it's the domain.
2855 * Otherwise, we assume we are reading an affine expression.
2857 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2858 __isl_take isl_set
*dom
, struct vars
*v
)
2860 struct isl_token
*tok
;
2862 tok
= isl_stream_next_token(s
);
2863 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2864 isl_stream_push_token(s
, tok
);
2865 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2867 if (!tok
|| tok
->type
!= '[') {
2868 isl_stream_error(s
, tok
, "expecting '['");
2871 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2872 isl_stream_push_token(s
, tok
);
2873 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2875 isl_stream_push_token(s
, tok
);
2880 isl_stream_push_token(s
, tok
);
2885 /* Read an affine expression from "s".
2887 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2892 ma
= isl_stream_read_multi_aff(s
);
2895 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2896 isl_die(s
->ctx
, isl_error_invalid
,
2897 "expecting single affine expression",
2900 aff
= isl_multi_aff_get_aff(ma
, 0);
2901 isl_multi_aff_free(ma
);
2904 isl_multi_aff_free(ma
);
2908 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2910 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2911 __isl_take isl_set
*dom
, struct vars
*v
)
2913 isl_pw_aff
*pwaff
= NULL
;
2915 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2918 if (isl_stream_eat(s
, '['))
2921 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2923 if (isl_stream_eat(s
, ']'))
2926 dom
= read_optional_formula(s
, dom
, v
, 0);
2927 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2932 isl_pw_aff_free(pwaff
);
2936 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2939 isl_set
*dom
= NULL
;
2941 isl_pw_aff
*pa
= NULL
;
2944 v
= vars_new(s
->ctx
);
2948 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2949 if (next_is_tuple(s
)) {
2950 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2951 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2954 if (isl_stream_eat(s
, '{'))
2958 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2959 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2960 vars_drop(v
, v
->n
- n
);
2962 while (isl_stream_eat_if_available(s
, ';')) {
2966 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2967 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2968 vars_drop(v
, v
->n
- n
);
2970 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2973 if (isl_stream_eat(s
, '}'))
2982 isl_pw_aff_free(pa
);
2986 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2989 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2992 aff
= isl_stream_read_aff(s
);
2997 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3000 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3003 pa
= isl_stream_read_pw_aff(s
);
3008 /* Read an isl_pw_multi_aff from "s".
3009 * We currently read a generic object and if it turns out to be a set or
3010 * a map, we convert that to an isl_pw_multi_aff.
3011 * It would be more efficient if we were to construct the isl_pw_multi_aff
3014 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
3022 if (obj
.type
== isl_obj_map
)
3023 return isl_pw_multi_aff_from_map(obj
.v
);
3024 if (obj
.type
== isl_obj_set
)
3025 return isl_pw_multi_aff_from_set(obj
.v
);
3027 obj
.type
->free(obj
.v
);
3028 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3032 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3035 isl_pw_multi_aff
*pma
;
3036 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3039 pma
= isl_stream_read_pw_multi_aff(s
);
3044 /* Read an isl_union_pw_multi_aff from "s".
3045 * We currently read a generic object and if it turns out to be a set or
3046 * a map, we convert that to an isl_union_pw_multi_aff.
3047 * It would be more efficient if we were to construct
3048 * the isl_union_pw_multi_aff directly.
3050 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3051 struct isl_stream
*s
)
3059 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3060 obj
= to_union(s
->ctx
, obj
);
3061 if (obj
.type
== isl_obj_union_map
)
3062 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3063 if (obj
.type
== isl_obj_union_set
)
3064 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3066 obj
.type
->free(obj
.v
);
3067 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3071 /* Read an isl_union_pw_multi_aff from "str".
3073 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3074 isl_ctx
*ctx
, const char *str
)
3076 isl_union_pw_multi_aff
*upma
;
3077 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3080 upma
= isl_stream_read_union_pw_multi_aff(s
);
3085 /* Assuming "pa" represents a single affine expression defined on a universe
3086 * domain, extract this affine expression.
3088 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3095 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3096 "expecting single affine expression",
3098 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3099 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3100 "expecting universe domain",
3103 aff
= isl_aff_copy(pa
->p
[0].aff
);
3104 isl_pw_aff_free(pa
);
3107 isl_pw_aff_free(pa
);
3111 /* Read a multi-affine expression from "s".
3112 * If the multi-affine expression has a domain, then the tuple
3113 * representing this domain cannot involve any affine expressions.
3114 * The tuple representing the actual expressions needs to consist
3115 * of only affine expressions. Moreover, these expressions can
3116 * only depend on parameters and input dimensions and not on other
3117 * output dimensions.
3119 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
3122 isl_set
*dom
= NULL
;
3123 isl_multi_pw_aff
*tuple
= NULL
;
3125 isl_space
*space
, *dom_space
;
3126 isl_multi_aff
*ma
= NULL
;
3128 v
= vars_new(s
->ctx
);
3132 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3133 if (next_is_tuple(s
)) {
3134 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3135 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3138 if (!isl_set_plain_is_universe(dom
))
3139 isl_die(s
->ctx
, isl_error_invalid
,
3140 "expecting universe parameter domain", goto error
);
3141 if (isl_stream_eat(s
, '{'))
3144 tuple
= read_tuple(s
, v
, 0, 0);
3147 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3152 has_expr
= tuple_has_expr(tuple
);
3156 isl_die(s
->ctx
, isl_error_invalid
,
3157 "expecting universe domain", goto error
);
3158 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3159 set
= isl_set_universe(space
);
3160 dom
= isl_set_intersect_params(set
, dom
);
3161 isl_multi_pw_aff_free(tuple
);
3162 tuple
= read_tuple(s
, v
, 0, 0);
3167 if (isl_stream_eat(s
, '}'))
3170 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3171 dim
= isl_set_dim(dom
, isl_dim_all
);
3172 dom_space
= isl_set_get_space(dom
);
3173 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3174 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3175 if (!isl_space_is_params(dom_space
))
3176 space
= isl_space_map_from_domain_and_range(
3177 isl_space_copy(dom_space
), space
);
3178 isl_space_free(dom_space
);
3179 ma
= isl_multi_aff_alloc(space
);
3181 for (i
= 0; i
< n
; ++i
) {
3184 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3185 aff
= aff_from_pw_aff(pa
);
3188 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3190 isl_die(s
->ctx
, isl_error_invalid
,
3191 "not an affine expression", goto error
);
3193 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3194 space
= isl_multi_aff_get_domain_space(ma
);
3195 aff
= isl_aff_reset_domain_space(aff
, space
);
3196 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3199 isl_multi_pw_aff_free(tuple
);
3204 isl_multi_pw_aff_free(tuple
);
3207 isl_multi_aff_free(ma
);
3211 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3214 isl_multi_aff
*maff
;
3215 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3218 maff
= isl_stream_read_multi_aff(s
);
3223 /* Read an isl_multi_pw_aff from "s".
3225 * The input format is similar to that of map, except that any conditions
3226 * on the domains should be specified inside the tuple since each
3227 * piecewise affine expression may have a different domain.
3229 * Since we do not know in advance if the isl_multi_pw_aff lives
3230 * in a set or a map space, we first read the first tuple and check
3231 * if it is followed by a "->". If so, we convert the tuple into
3232 * the domain of the isl_multi_pw_aff and read in the next tuple.
3233 * This tuple (or the first tuple if it was not followed by a "->")
3234 * is then converted into the isl_multi_pw_aff.
3236 * Note that the function read_tuple accepts tuples where some output or
3237 * set dimensions are defined in terms of other output or set dimensions
3238 * since this function is also used to read maps. As a special case,
3239 * read_tuple also accept dimensions that are defined in terms of themselves
3240 * (i.e., that are not defined).
3241 * These cases are not allowed when reading am isl_multi_pw_aff so we check
3242 * that the definition of the output/set dimensions does not involve any
3243 * output/set dimensions.
3244 * We then drop the output dimensions from the domain of the result
3245 * of read_tuple (which is of the form [input, output] -> [output],
3246 * with anonymous domain) and reset the space.
3248 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(struct isl_stream
*s
)
3251 isl_set
*dom
= NULL
;
3252 isl_multi_pw_aff
*tuple
= NULL
;
3254 isl_space
*space
, *dom_space
;
3255 isl_multi_pw_aff
*mpa
= NULL
;
3257 v
= vars_new(s
->ctx
);
3261 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3262 if (next_is_tuple(s
)) {
3263 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3264 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3267 if (isl_stream_eat(s
, '{'))
3270 tuple
= read_tuple(s
, v
, 0, 0);
3273 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3274 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3275 dom
= isl_map_domain(map
);
3276 tuple
= read_tuple(s
, v
, 0, 0);
3281 if (isl_stream_eat(s
, '}'))
3284 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3285 dim
= isl_set_dim(dom
, isl_dim_all
);
3286 dom_space
= isl_set_get_space(dom
);
3287 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3288 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3289 if (!isl_space_is_params(dom_space
))
3290 space
= isl_space_map_from_domain_and_range(
3291 isl_space_copy(dom_space
), space
);
3292 isl_space_free(dom_space
);
3293 mpa
= isl_multi_pw_aff_alloc(space
);
3295 for (i
= 0; i
< n
; ++i
) {
3297 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3300 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3301 isl_pw_aff_free(pa
);
3302 isl_die(s
->ctx
, isl_error_invalid
,
3303 "not an affine expression", goto error
);
3305 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3306 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3307 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3308 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3311 isl_multi_pw_aff_free(tuple
);
3313 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3316 isl_multi_pw_aff_free(tuple
);
3319 isl_multi_pw_aff_free(mpa
);
3323 /* Read an isl_multi_pw_aff from "str".
3325 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3328 isl_multi_pw_aff
*mpa
;
3329 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3332 mpa
= isl_stream_read_multi_pw_aff(s
);
3337 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3338 struct isl_stream
*s
)
3343 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3344 obj
.type
= isl_obj_union_pw_qpolynomial
;
3345 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3348 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3353 obj
.type
->free(obj
.v
);
3357 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3358 isl_ctx
*ctx
, const char *str
)
3360 isl_union_pw_qpolynomial
*upwqp
;
3361 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3364 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);