2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
22 #include <isl_stream_private.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_vec_private.h>
30 #include <isl_val_private.h>
35 struct variable
*next
;
44 static struct vars
*vars_new(struct isl_ctx
*ctx
)
47 v
= isl_alloc_type(ctx
, struct vars
);
56 static void variable_free(struct variable
*var
)
59 struct variable
*next
= var
->next
;
66 static void vars_free(struct vars
*v
)
74 static void vars_drop(struct vars
*v
, int n
)
85 struct variable
*next
= var
->next
;
93 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
97 var
= isl_calloc_type(v
->ctx
, struct variable
);
100 var
->name
= strdup(name
);
101 var
->name
[len
] = '\0';
110 static int vars_pos(struct vars
*v
, const char *s
, int len
)
117 for (q
= v
->v
; q
; q
= q
->next
) {
118 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
125 v
->v
= variable_new(v
, s
, len
, v
->n
);
133 static int vars_add_anon(struct vars
*v
)
135 v
->v
= variable_new(v
, "", 0, v
->n
);
144 /* Obtain next token, with some preprocessing.
145 * In particular, evaluate expressions of the form x^y,
146 * with x and y values.
148 static struct isl_token
*next_token(struct isl_stream
*s
)
150 struct isl_token
*tok
, *tok2
;
152 tok
= isl_stream_next_token(s
);
153 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
155 if (!isl_stream_eat_if_available(s
, '^'))
157 tok2
= isl_stream_next_token(s
);
158 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
159 isl_stream_error(s
, tok2
, "expecting constant value");
163 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
165 isl_token_free(tok2
);
169 isl_token_free(tok2
);
173 /* Read an isl_val from "s".
175 * The following token sequences are recognized
178 * "-" "infty" -> -infty
183 * where n, d and v are integer constants.
185 __isl_give isl_val
*isl_stream_read_val(struct isl_stream
*s
)
187 struct isl_token
*tok
= NULL
;
188 struct isl_token
*tok2
= NULL
;
193 isl_stream_error(s
, NULL
, "unexpected EOF");
196 if (tok
->type
== ISL_TOKEN_INFTY
) {
198 return isl_val_infty(s
->ctx
);
200 if (tok
->type
== '-' &&
201 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
203 return isl_val_neginfty(s
->ctx
);
205 if (tok
->type
== ISL_TOKEN_NAN
) {
207 return isl_val_nan(s
->ctx
);
209 if (tok
->type
!= ISL_TOKEN_VALUE
) {
210 isl_stream_error(s
, tok
, "expecting value");
214 if (isl_stream_eat_if_available(s
, '/')) {
215 tok2
= next_token(s
);
217 isl_stream_error(s
, NULL
, "unexpected EOF");
220 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
221 isl_stream_error(s
, tok2
, "expecting value");
224 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
225 val
= isl_val_normalize(val
);
227 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
231 isl_token_free(tok2
);
235 isl_token_free(tok2
);
239 /* Read an isl_val from "str".
241 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
245 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
248 val
= isl_stream_read_val(s
);
253 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
255 struct isl_token
*tok
;
258 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
259 isl_stream_error(s
, tok
, "expecting constant value");
263 isl_int_mul(*f
, *f
, tok
->u
.v
);
267 if (isl_stream_eat_if_available(s
, '*'))
268 return accept_cst_factor(s
, f
);
276 /* Given an affine expression aff, return an affine expression
277 * for aff % d, with d the next token on the stream, which is
278 * assumed to be a constant.
280 * We introduce an integer division q = [aff/d] and the result
281 * is set to aff - d q.
283 static __isl_give isl_pw_aff
*affine_mod(struct isl_stream
*s
,
284 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
286 struct isl_token
*tok
;
290 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
291 isl_stream_error(s
, tok
, "expecting constant value");
295 q
= isl_pw_aff_copy(aff
);
296 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
297 q
= isl_pw_aff_floor(q
);
298 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
300 aff
= isl_pw_aff_sub(aff
, q
);
305 isl_pw_aff_free(aff
);
310 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
311 __isl_take isl_space
*dim
, struct vars
*v
);
312 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
313 __isl_take isl_space
*dim
, struct vars
*v
);
315 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
316 __isl_take isl_space
*dim
, struct vars
*v
)
318 struct isl_token
*tok
;
319 isl_pw_aff_list
*list
= NULL
;
322 tok
= isl_stream_next_token(s
);
325 min
= tok
->type
== ISL_TOKEN_MIN
;
328 if (isl_stream_eat(s
, '('))
331 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
335 if (isl_stream_eat(s
, ')'))
339 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
342 isl_pw_aff_list_free(list
);
346 /* Is "tok" the start of an integer division?
348 static int is_start_of_div(struct isl_token
*tok
)
352 if (tok
->type
== '[')
354 if (tok
->type
== ISL_TOKEN_FLOOR
)
356 if (tok
->type
== ISL_TOKEN_CEIL
)
358 if (tok
->type
== ISL_TOKEN_FLOORD
)
360 if (tok
->type
== ISL_TOKEN_CEILD
)
365 /* Read an integer division from "s" and return it as an isl_pw_aff.
367 * The integer division can be of the form
369 * [<affine expression>]
370 * floor(<affine expression>)
371 * ceil(<affine expression>)
372 * floord(<affine expression>,<denominator>)
373 * ceild(<affine expression>,<denominator>)
375 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
376 __isl_take isl_space
*dim
, struct vars
*v
)
378 struct isl_token
*tok
;
382 isl_pw_aff
*pwaff
= NULL
;
384 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
386 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
388 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
390 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
393 if (isl_stream_eat(s
, '('))
396 if (isl_stream_eat(s
, '['))
400 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
403 if (isl_stream_eat(s
, ','))
409 if (tok
->type
!= ISL_TOKEN_VALUE
) {
410 isl_stream_error(s
, tok
, "expected denominator");
411 isl_stream_push_token(s
, tok
);
414 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
419 pwaff
= isl_pw_aff_ceil(pwaff
);
421 pwaff
= isl_pw_aff_floor(pwaff
);
424 if (isl_stream_eat(s
, ')'))
427 if (isl_stream_eat(s
, ']'))
435 isl_pw_aff_free(pwaff
);
439 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
440 __isl_take isl_space
*dim
, struct vars
*v
)
442 struct isl_token
*tok
= NULL
;
443 isl_pw_aff
*res
= NULL
;
447 isl_stream_error(s
, NULL
, "unexpected EOF");
451 if (tok
->type
== ISL_TOKEN_AFF
) {
452 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
454 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
456 int pos
= vars_pos(v
, tok
->u
.s
, -1);
462 vars_drop(v
, v
->n
- n
);
463 isl_stream_error(s
, tok
, "unknown identifier");
467 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
470 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
471 res
= isl_pw_aff_from_aff(aff
);
473 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
474 if (isl_stream_eat_if_available(s
, '*')) {
475 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
476 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
480 ls
= isl_local_space_from_space(isl_space_copy(dim
));
481 aff
= isl_aff_zero_on_domain(ls
);
482 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
483 res
= isl_pw_aff_from_aff(aff
);
486 } else if (tok
->type
== '(') {
489 res
= accept_affine(s
, isl_space_copy(dim
), v
);
492 if (isl_stream_eat(s
, ')'))
494 } else if (is_start_of_div(tok
)) {
495 isl_stream_push_token(s
, tok
);
497 res
= accept_div(s
, isl_space_copy(dim
), v
);
498 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
499 isl_stream_push_token(s
, tok
);
501 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
503 isl_stream_error(s
, tok
, "expecting factor");
506 if (isl_stream_eat_if_available(s
, '%') ||
507 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
509 return affine_mod(s
, v
, res
);
511 if (isl_stream_eat_if_available(s
, '*')) {
514 isl_int_set_si(f
, 1);
515 if (accept_cst_factor(s
, &f
) < 0) {
519 res
= isl_pw_aff_scale(res
, f
);
522 if (isl_stream_eat_if_available(s
, '/')) {
525 isl_int_set_si(f
, 1);
526 if (accept_cst_factor(s
, &f
) < 0) {
530 res
= isl_pw_aff_scale_down(res
, f
);
539 isl_pw_aff_free(res
);
544 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
549 space
= isl_pw_aff_get_domain_space(pwaff
);
550 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
551 aff
= isl_aff_add_constant(aff
, v
);
553 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
556 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
557 __isl_take isl_space
*dim
, struct vars
*v
)
559 struct isl_token
*tok
= NULL
;
564 ls
= isl_local_space_from_space(isl_space_copy(dim
));
565 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
572 isl_stream_error(s
, NULL
, "unexpected EOF");
575 if (tok
->type
== '-') {
580 if (tok
->type
== '(' || is_start_of_div(tok
) ||
581 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
582 tok
->type
== ISL_TOKEN_IDENT
||
583 tok
->type
== ISL_TOKEN_AFF
) {
585 isl_stream_push_token(s
, tok
);
587 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
589 res
= isl_pw_aff_sub(res
, term
);
591 res
= isl_pw_aff_add(res
, term
);
595 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
597 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
598 if (isl_stream_eat_if_available(s
, '*') ||
599 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
601 term
= accept_affine_factor(s
,
602 isl_space_copy(dim
), v
);
603 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
604 res
= isl_pw_aff_add(res
, term
);
608 res
= add_cst(res
, tok
->u
.v
);
612 isl_stream_error(s
, tok
, "unexpected isl_token");
613 isl_stream_push_token(s
, tok
);
614 isl_pw_aff_free(res
);
621 if (tok
&& tok
->type
== '-') {
624 } else if (tok
&& tok
->type
== '+') {
627 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
628 isl_int_is_neg(tok
->u
.v
)) {
629 isl_stream_push_token(s
, tok
);
632 isl_stream_push_token(s
, tok
);
642 isl_pw_aff_free(res
);
646 static int is_comparator(struct isl_token
*tok
)
664 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
665 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
666 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
667 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
669 /* Accept a ternary operator, given the first argument.
671 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
672 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
675 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
680 if (isl_stream_eat(s
, '?'))
683 dim
= isl_space_wrap(isl_map_get_space(cond
));
684 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
688 if (isl_stream_eat(s
, ':'))
691 dim
= isl_pw_aff_get_domain_space(pwaff1
);
692 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
696 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
697 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
700 isl_pw_aff_free(pwaff1
);
701 isl_pw_aff_free(pwaff2
);
705 /* Set *line and *col to those of the next token, if any.
707 static void set_current_line_col(struct isl_stream
*s
, int *line
, int *col
)
709 struct isl_token
*tok
;
711 tok
= isl_stream_next_token(s
);
717 isl_stream_push_token(s
, tok
);
720 /* Push a token encapsulating "pa" onto "s", with the given
723 static int push_aff(struct isl_stream
*s
, int line
, int col
,
724 __isl_take isl_pw_aff
*pa
)
726 struct isl_token
*tok
;
728 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
731 tok
->type
= ISL_TOKEN_AFF
;
733 isl_stream_push_token(s
, tok
);
741 /* Accept an affine expression that may involve ternary operators.
742 * We first read an affine expression.
743 * If it is not followed by a comparison operator, we simply return it.
744 * Otherwise, we assume the affine expression is part of the first
745 * argument of a ternary operator and try to parse that.
747 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
748 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
753 struct isl_token
*tok
;
754 int line
= -1, col
= -1;
757 set_current_line_col(s
, &line
, &col
);
759 pwaff
= accept_affine(s
, dim
, v
);
761 pwaff
= isl_pw_aff_set_rational(pwaff
);
765 tok
= isl_stream_next_token(s
);
767 return isl_pw_aff_free(pwaff
);
769 is_comp
= is_comparator(tok
);
770 isl_stream_push_token(s
, tok
);
774 space
= isl_pw_aff_get_domain_space(pwaff
);
775 cond
= isl_map_universe(isl_space_unwrap(space
));
777 if (push_aff(s
, line
, col
, pwaff
) < 0)
780 cond
= read_formula(s
, v
, cond
, rational
);
782 return accept_ternary(s
, cond
, v
, rational
);
785 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
786 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
793 if (type
== isl_dim_param
)
794 pos
= isl_map_dim(map
, isl_dim_param
);
796 pos
= isl_map_dim(map
, isl_dim_in
);
797 if (type
== isl_dim_out
)
798 pos
+= isl_map_dim(map
, isl_dim_out
);
803 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
805 def_map
= isl_map_from_pw_aff(def
);
806 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
807 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
809 map
= isl_map_intersect(map
, def_map
);
814 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
815 __isl_take isl_space
*dim
, struct vars
*v
)
818 isl_pw_aff_list
*list
;
819 struct isl_token
*tok
= NULL
;
821 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
822 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
827 tok
= isl_stream_next_token(s
);
829 isl_stream_error(s
, NULL
, "unexpected EOF");
832 if (tok
->type
!= ',') {
833 isl_stream_push_token(s
, tok
);
838 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
839 list
= isl_pw_aff_list_concat(list
,
840 isl_pw_aff_list_from_pw_aff(pwaff
));
849 isl_pw_aff_list_free(list
);
853 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
854 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
856 struct isl_token
*tok
;
858 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
862 if (tok
->type
!= ISL_TOKEN_IDENT
)
865 p
= vars_pos(v
, tok
->u
.s
, -1);
869 isl_stream_error(s
, tok
, "expecting unique identifier");
873 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
876 tok
= isl_stream_next_token(s
);
877 if (tok
&& tok
->type
== '=') {
879 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
880 tok
= isl_stream_next_token(s
);
883 if (!tok
|| tok
->type
!= ',')
889 isl_stream_push_token(s
, tok
);
898 static int next_is_tuple(struct isl_stream
*s
)
900 struct isl_token
*tok
;
903 tok
= isl_stream_next_token(s
);
906 if (tok
->type
== '[') {
907 isl_stream_push_token(s
, tok
);
910 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
911 isl_stream_push_token(s
, tok
);
915 is_tuple
= isl_stream_next_token_is(s
, '[');
917 isl_stream_push_token(s
, tok
);
922 /* Allocate an initial tuple with zero dimensions and an anonymous,
923 * unstructured space.
924 * A tuple is represented as an isl_multi_pw_aff.
925 * The range space is the space of the tuple.
926 * The domain space is an anonymous space
927 * with a dimension for each variable in the set of variables in "v".
928 * If a given dimension is not defined in terms of earlier dimensions in
929 * the input, then the corresponding isl_pw_aff is set equal to one time
930 * the variable corresponding to the dimension being defined.
932 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
934 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
937 /* Is "pa" an expression in term of earlier dimensions?
938 * The alternative is that the dimension is defined to be equal to itself,
939 * meaning that it has a universe domain and an expression that depends
940 * on itself. "i" is the position of the expression in a sequence
941 * of "n" expressions. The final dimensions of "pa" correspond to
942 * these "n" expressions.
944 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
952 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
956 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
961 /* Does the tuple contain any dimensions that are defined
962 * in terms of earlier dimensions?
964 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
972 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
973 for (i
= 0; i
< n
; ++i
) {
974 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
975 has_expr
= pw_aff_is_expr(pa
, i
, n
);
977 if (has_expr
< 0 || has_expr
)
984 /* Add a dimension to the given tuple.
985 * The dimension is initially undefined, so it is encoded
986 * as one times itself.
988 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
989 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
995 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
996 space
= isl_multi_pw_aff_get_domain_space(tuple
);
997 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
998 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
999 pa
= isl_pw_aff_from_aff(aff
);
1000 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
1001 isl_multi_pw_aff_from_pw_aff(pa
));
1006 /* Set the name of dimension "pos" in "tuple" to "name".
1007 * During printing, we add primes if the same name appears more than once
1008 * to distinguish the occurrences. Here, we remove those primes from "name"
1009 * before setting the name of the dimension.
1011 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
1012 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
1019 prime
= strchr(name
, '\'');
1022 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
1029 /* Accept a piecewise affine expression.
1031 * At the outer level, the piecewise affine expression may be of the form
1033 * aff1 : condition1; aff2 : conditions2; ...
1039 * each of the affine expressions may in turn include ternary operators.
1041 * There may be parentheses around some subexpression of "aff1"
1042 * around "aff1" itself, around "aff1 : condition1" and/or
1043 * around the entire piecewise affine expression.
1044 * We therefore remove the opening parenthesis (if any) from the stream
1045 * in case the closing parenthesis follows the colon, but if the closing
1046 * parenthesis is the first thing in the stream after the parsed affine
1047 * expression, we push the parsed expression onto the stream and parse
1048 * again in case the parentheses enclose some subexpression of "aff1".
1050 static __isl_give isl_pw_aff
*accept_piecewise_affine(struct isl_stream
*s
,
1051 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1054 isl_space
*res_space
;
1056 res_space
= isl_space_from_domain(isl_space_copy(space
));
1057 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1058 res
= isl_pw_aff_empty(res_space
);
1062 int line
= -1, col
= -1;
1064 set_current_line_col(s
, &line
, &col
);
1065 seen_paren
= isl_stream_eat_if_available(s
, '(');
1067 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1070 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1072 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1074 if (push_aff(s
, line
, col
, pa
) < 0)
1076 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1079 if (isl_stream_eat_if_available(s
, ':')) {
1080 isl_space
*dom_space
;
1083 dom_space
= isl_pw_aff_get_domain_space(pa
);
1084 dom
= isl_set_universe(dom_space
);
1085 dom
= read_formula(s
, v
, dom
, rational
);
1086 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1089 res
= isl_pw_aff_union_add(res
, pa
);
1091 if (seen_paren
&& isl_stream_eat(s
, ')'))
1093 } while (isl_stream_eat_if_available(s
, ';'));
1095 isl_space_free(space
);
1099 isl_space_free(space
);
1100 return isl_pw_aff_free(res
);
1103 /* Read an affine expression from "s" and replace the definition
1104 * of dimension "pos" in "tuple" by this expression.
1106 * accept_extended_affine requires a wrapped space as input.
1107 * The domain space of "tuple", on the other hand is an anonymous space,
1108 * so we have to adjust the space of the isl_pw_aff before adding it
1111 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
1112 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
1118 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1120 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1122 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1123 def
= isl_pw_aff_reset_domain_space(def
, space
);
1124 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
1129 /* Read a list of variables and/or affine expressions and return the list
1130 * as an isl_multi_pw_aff.
1131 * The elements in the list are separated by either "," or "][".
1132 * If "comma" is set then only "," is allowed.
1134 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
1135 struct vars
*v
, int rational
, int comma
)
1138 struct isl_token
*tok
;
1139 isl_multi_pw_aff
*res
;
1141 res
= tuple_alloc(v
);
1143 if (isl_stream_next_token_is(s
, ']'))
1146 while ((tok
= next_token(s
)) != NULL
) {
1149 res
= tuple_add_dim(res
, v
);
1151 if (tok
->type
== ISL_TOKEN_IDENT
) {
1153 int p
= vars_pos(v
, tok
->u
.s
, -1);
1159 if (tok
->type
== '*') {
1160 if (vars_add_anon(v
) < 0)
1162 isl_token_free(tok
);
1163 } else if (new_name
) {
1164 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
1165 isl_token_free(tok
);
1166 if (isl_stream_eat_if_available(s
, '='))
1167 res
= read_tuple_var_def(s
, res
, i
, v
,
1170 isl_stream_push_token(s
, tok
);
1172 if (vars_add_anon(v
) < 0)
1174 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
1177 tok
= isl_stream_next_token(s
);
1178 if (!comma
&& tok
&& tok
->type
== ']' &&
1179 isl_stream_next_token_is(s
, '[')) {
1180 isl_token_free(tok
);
1181 tok
= isl_stream_next_token(s
);
1182 } else if (!tok
|| tok
->type
!= ',')
1185 isl_token_free(tok
);
1189 isl_stream_push_token(s
, tok
);
1193 isl_token_free(tok
);
1194 return isl_multi_pw_aff_free(res
);
1197 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1199 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1200 struct vars
*v
, int rational
, int comma
)
1202 struct isl_token
*tok
;
1204 isl_multi_pw_aff
*res
= NULL
;
1206 tok
= isl_stream_next_token(s
);
1209 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1210 name
= strdup(tok
->u
.s
);
1211 isl_token_free(tok
);
1215 isl_stream_push_token(s
, tok
);
1216 if (isl_stream_eat(s
, '['))
1218 if (next_is_tuple(s
)) {
1219 isl_multi_pw_aff
*out
;
1221 res
= read_tuple(s
, v
, rational
, comma
);
1222 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1224 out
= read_tuple(s
, v
, rational
, comma
);
1225 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1226 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1227 res
= isl_multi_pw_aff_range_product(res
, out
);
1229 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1230 if (isl_stream_eat(s
, ']'))
1234 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1241 return isl_multi_pw_aff_free(res
);
1244 /* Read a tuple from "s" and add it to "map".
1245 * The tuple is initially represented as an isl_multi_pw_aff.
1246 * We first create the appropriate space in "map" based on the range
1247 * space of this isl_multi_pw_aff. Then, we add equalities based
1248 * on the affine expressions. These live in an anonymous space,
1249 * however, so we first need to reset the space to that of "map".
1251 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1252 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1253 int rational
, int comma
)
1256 isl_multi_pw_aff
*tuple
;
1257 isl_space
*space
= NULL
;
1259 tuple
= read_tuple(s
, v
, rational
, comma
);
1263 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1264 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1268 if (type
== isl_dim_param
) {
1269 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1270 isl_space_is_wrapping(space
)) {
1271 isl_die(s
->ctx
, isl_error_invalid
,
1272 "parameter tuples cannot be named or nested",
1275 map
= isl_map_add_dims(map
, type
, n
);
1276 for (i
= 0; i
< n
; ++i
) {
1278 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1279 isl_die(s
->ctx
, isl_error_invalid
,
1280 "parameters must be named",
1282 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1283 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1285 } else if (type
== isl_dim_in
) {
1288 set
= isl_set_universe(isl_space_copy(space
));
1290 set
= isl_set_set_rational(set
);
1291 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1292 map
= isl_map_from_domain(set
);
1296 set
= isl_set_universe(isl_space_copy(space
));
1298 set
= isl_set_set_rational(set
);
1299 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1302 for (i
= 0; i
< n
; ++i
) {
1309 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1310 space
= isl_pw_aff_get_domain_space(pa
);
1311 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1312 aff
= isl_aff_add_coefficient_si(aff
,
1313 isl_dim_in
, v
->n
- n
+ i
, -1);
1314 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1316 pa
= isl_pw_aff_set_rational(pa
);
1317 set
= isl_pw_aff_zero_set(pa
);
1318 map_i
= isl_map_from_range(set
);
1319 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1320 map
= isl_map_intersect(map
, map_i
);
1323 isl_space_free(space
);
1324 isl_multi_pw_aff_free(tuple
);
1327 isl_space_free(space
);
1328 isl_multi_pw_aff_free(tuple
);
1333 static __isl_give isl_set
*construct_constraints(
1334 __isl_take isl_set
*set
, int type
,
1335 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1340 left
= isl_pw_aff_list_copy(left
);
1341 right
= isl_pw_aff_list_copy(right
);
1343 left
= isl_pw_aff_list_set_rational(left
);
1344 right
= isl_pw_aff_list_set_rational(right
);
1346 if (type
== ISL_TOKEN_LE
)
1347 cond
= isl_pw_aff_list_le_set(left
, right
);
1348 else if (type
== ISL_TOKEN_GE
)
1349 cond
= isl_pw_aff_list_ge_set(left
, right
);
1350 else if (type
== ISL_TOKEN_LT
)
1351 cond
= isl_pw_aff_list_lt_set(left
, right
);
1352 else if (type
== ISL_TOKEN_GT
)
1353 cond
= isl_pw_aff_list_gt_set(left
, right
);
1354 else if (type
== ISL_TOKEN_NE
)
1355 cond
= isl_pw_aff_list_ne_set(left
, right
);
1357 cond
= isl_pw_aff_list_eq_set(left
, right
);
1359 return isl_set_intersect(set
, cond
);
1362 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1363 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1365 struct isl_token
*tok
= NULL
;
1366 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1369 set
= isl_map_wrap(map
);
1370 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1373 tok
= isl_stream_next_token(s
);
1374 if (!is_comparator(tok
)) {
1375 isl_stream_error(s
, tok
, "missing operator");
1377 isl_stream_push_token(s
, tok
);
1382 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1386 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1388 isl_token_free(tok
);
1389 isl_pw_aff_list_free(list1
);
1392 tok
= isl_stream_next_token(s
);
1393 if (!is_comparator(tok
)) {
1395 isl_stream_push_token(s
, tok
);
1399 isl_pw_aff_list_free(list1
);
1401 return isl_set_unwrap(set
);
1404 isl_token_free(tok
);
1405 isl_pw_aff_list_free(list1
);
1406 isl_pw_aff_list_free(list2
);
1411 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1412 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1415 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1417 map
= isl_map_from_domain(isl_map_wrap(map
));
1418 map
= read_defined_var_list(s
, v
, map
, rational
);
1420 if (isl_stream_eat(s
, ':'))
1423 map
= read_formula(s
, v
, map
, rational
);
1424 map
= isl_set_unwrap(isl_map_domain(map
));
1426 vars_drop(v
, v
->n
- n
);
1427 if (seen_paren
&& isl_stream_eat(s
, ')'))
1436 /* Parse an expression between parentheses and push the result
1437 * back on the stream.
1439 * The parsed expression may be either an affine expression
1440 * or a condition. The first type is pushed onto the stream
1441 * as an isl_pw_aff, while the second is pushed as an isl_map.
1443 * If the initial token indicates the start of a condition,
1444 * we parse it as such.
1445 * Otherwise, we first parse an affine expression and push
1446 * that onto the stream. If the affine expression covers the
1447 * entire expression between parentheses, we return.
1448 * Otherwise, we assume that the affine expression is the
1449 * start of a condition and continue parsing.
1451 static int resolve_paren_expr(struct isl_stream
*s
,
1452 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1454 struct isl_token
*tok
, *tok2
;
1458 tok
= isl_stream_next_token(s
);
1459 if (!tok
|| tok
->type
!= '(')
1462 if (isl_stream_next_token_is(s
, '('))
1463 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1466 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1467 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1468 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1469 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1470 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1471 map
= read_formula(s
, v
, map
, rational
);
1472 if (isl_stream_eat(s
, ')'))
1474 tok
->type
= ISL_TOKEN_MAP
;
1476 isl_stream_push_token(s
, tok
);
1480 tok2
= isl_stream_next_token(s
);
1485 isl_stream_push_token(s
, tok2
);
1487 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1491 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1494 tok2
->type
= ISL_TOKEN_AFF
;
1495 tok2
->u
.pwaff
= pwaff
;
1497 if (isl_stream_eat_if_available(s
, ')')) {
1498 isl_stream_push_token(s
, tok2
);
1499 isl_token_free(tok
);
1504 isl_stream_push_token(s
, tok2
);
1506 map
= read_formula(s
, v
, map
, rational
);
1507 if (isl_stream_eat(s
, ')'))
1510 tok
->type
= ISL_TOKEN_MAP
;
1512 isl_stream_push_token(s
, tok
);
1516 isl_pw_aff_free(pwaff
);
1518 isl_token_free(tok
);
1523 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1524 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1526 if (isl_stream_next_token_is(s
, '('))
1527 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1530 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1531 struct isl_token
*tok
;
1532 tok
= isl_stream_next_token(s
);
1536 map
= isl_map_copy(tok
->u
.map
);
1537 isl_token_free(tok
);
1541 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1542 return read_exists(s
, v
, map
, rational
);
1544 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1547 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1548 isl_space
*dim
= isl_map_get_space(map
);
1550 return isl_map_empty(dim
);
1553 return add_constraint(s
, v
, map
, rational
);
1559 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1560 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1565 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1566 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1568 res
= isl_map_subtract(isl_map_copy(map
), res
);
1570 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1573 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1574 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1576 res
= isl_map_subtract(res
, res_i
);
1578 res
= isl_map_intersect(res
, res_i
);
1585 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1586 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1590 if (isl_stream_next_token_is(s
, '}')) {
1591 isl_space
*dim
= isl_map_get_space(map
);
1593 return isl_map_universe(dim
);
1596 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1597 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1600 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1601 res
= isl_map_union(res
, res_i
);
1608 /* Read a first order formula from "s", add the corresponding
1609 * constraints to "map" and return the result.
1611 * In particular, read a formula of the form
1619 * where a and b are disjunctions.
1621 * In the first case, map is replaced by
1623 * map \cap { [..] : a }
1625 * In the second case, it is replaced by
1627 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1629 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
1630 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1634 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1636 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1639 res
= isl_map_subtract(isl_map_copy(map
), res
);
1640 res2
= read_disjuncts(s
, v
, map
, rational
);
1641 res
= isl_map_union(res
, res2
);
1648 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1650 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1651 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1652 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1653 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1655 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1656 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1657 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1659 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1660 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1661 isl_basic_map_dim(bmap
, isl_dim_in
) +
1662 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1663 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1665 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1671 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1672 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1675 struct isl_token
*tok
;
1685 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1686 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1688 tok
= isl_stream_next_token(s
);
1689 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1690 isl_stream_error(s
, tok
, "expecting coefficient");
1692 isl_stream_push_token(s
, tok
);
1695 if (!tok
->on_new_line
) {
1696 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1697 isl_stream_push_token(s
, tok
);
1701 type
= isl_int_get_si(tok
->u
.v
);
1702 isl_token_free(tok
);
1704 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1706 k
= isl_basic_map_alloc_equality(bmap
);
1709 k
= isl_basic_map_alloc_inequality(bmap
);
1715 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
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
,
1726 "coefficient should not appear on new line");
1727 isl_stream_push_token(s
, tok
);
1730 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1731 isl_int_set(c
[pos
], tok
->u
.v
);
1732 isl_token_free(tok
);
1737 isl_basic_map_free(bmap
);
1741 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1744 struct isl_token
*tok
;
1745 struct isl_token
*tok2
;
1748 unsigned in
= 0, out
, local
= 0;
1749 struct isl_basic_map
*bmap
= NULL
;
1752 tok
= isl_stream_next_token(s
);
1754 isl_stream_error(s
, NULL
, "unexpected EOF");
1757 tok2
= isl_stream_next_token(s
);
1759 isl_token_free(tok
);
1760 isl_stream_error(s
, NULL
, "unexpected EOF");
1763 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1764 isl_stream_push_token(s
, tok2
);
1765 isl_stream_push_token(s
, tok
);
1766 isl_stream_error(s
, NULL
,
1767 "expecting constraint matrix dimensions");
1770 n_row
= isl_int_get_si(tok
->u
.v
);
1771 n_col
= isl_int_get_si(tok2
->u
.v
);
1772 on_new_line
= tok2
->on_new_line
;
1773 isl_token_free(tok2
);
1774 isl_token_free(tok
);
1775 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1776 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1777 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1778 tok
= isl_stream_next_token_on_same_line(s
);
1780 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1781 isl_stream_error(s
, tok
,
1782 "expecting number of output dimensions");
1783 isl_stream_push_token(s
, tok
);
1786 out
= isl_int_get_si(tok
->u
.v
);
1787 isl_token_free(tok
);
1789 tok
= isl_stream_next_token_on_same_line(s
);
1790 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1791 isl_stream_error(s
, tok
,
1792 "expecting number of input dimensions");
1794 isl_stream_push_token(s
, tok
);
1797 in
= isl_int_get_si(tok
->u
.v
);
1798 isl_token_free(tok
);
1800 tok
= isl_stream_next_token_on_same_line(s
);
1801 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1802 isl_stream_error(s
, tok
,
1803 "expecting number of existentials");
1805 isl_stream_push_token(s
, tok
);
1808 local
= isl_int_get_si(tok
->u
.v
);
1809 isl_token_free(tok
);
1811 tok
= isl_stream_next_token_on_same_line(s
);
1812 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1813 isl_stream_error(s
, tok
,
1814 "expecting number of parameters");
1816 isl_stream_push_token(s
, tok
);
1819 nparam
= isl_int_get_si(tok
->u
.v
);
1820 isl_token_free(tok
);
1821 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1822 isl_stream_error(s
, NULL
,
1823 "dimensions don't match");
1827 out
= n_col
- 2 - nparam
;
1828 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1832 for (i
= 0; i
< local
; ++i
) {
1833 int k
= isl_basic_map_alloc_div(bmap
);
1836 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1839 for (i
= 0; i
< n_row
; ++i
)
1840 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1842 tok
= isl_stream_next_token_on_same_line(s
);
1844 isl_stream_error(s
, tok
, "unexpected extra token on line");
1845 isl_stream_push_token(s
, tok
);
1849 bmap
= isl_basic_map_simplify(bmap
);
1850 bmap
= isl_basic_map_finalize(bmap
);
1853 isl_basic_map_free(bmap
);
1857 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1859 struct isl_token
*tok
;
1860 struct isl_token
*tok2
;
1862 struct isl_map
*map
;
1864 tok
= isl_stream_next_token(s
);
1866 isl_stream_error(s
, NULL
, "unexpected EOF");
1869 tok2
= isl_stream_next_token_on_same_line(s
);
1870 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1871 isl_stream_push_token(s
, tok2
);
1872 isl_stream_push_token(s
, tok
);
1873 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1876 isl_stream_error(s
, tok2
, "unexpected token");
1877 isl_stream_push_token(s
, tok2
);
1878 isl_stream_push_token(s
, tok
);
1881 n
= isl_int_get_si(tok
->u
.v
);
1882 isl_token_free(tok
);
1884 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1886 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1888 for (i
= 1; map
&& i
< n
; ++i
)
1889 map
= isl_map_union(map
,
1890 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1895 static int optional_power(struct isl_stream
*s
)
1898 struct isl_token
*tok
;
1900 tok
= isl_stream_next_token(s
);
1903 if (tok
->type
!= '^') {
1904 isl_stream_push_token(s
, tok
);
1907 isl_token_free(tok
);
1908 tok
= isl_stream_next_token(s
);
1909 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1910 isl_stream_error(s
, tok
, "expecting exponent");
1912 isl_stream_push_token(s
, tok
);
1915 pow
= isl_int_get_si(tok
->u
.v
);
1916 isl_token_free(tok
);
1920 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1921 __isl_keep isl_map
*map
, struct vars
*v
);
1923 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1924 __isl_keep isl_map
*map
, struct vars
*v
)
1926 isl_pw_qpolynomial
*pwqp
;
1927 struct isl_token
*tok
;
1929 tok
= next_token(s
);
1931 isl_stream_error(s
, NULL
, "unexpected EOF");
1934 if (tok
->type
== '(') {
1937 isl_token_free(tok
);
1938 pwqp
= read_term(s
, map
, v
);
1941 if (isl_stream_eat(s
, ')'))
1943 pow
= optional_power(s
);
1944 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1945 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1946 struct isl_token
*tok2
;
1947 isl_qpolynomial
*qp
;
1949 tok2
= isl_stream_next_token(s
);
1950 if (tok2
&& tok2
->type
== '/') {
1951 isl_token_free(tok2
);
1952 tok2
= next_token(s
);
1953 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1954 isl_stream_error(s
, tok2
, "expected denominator");
1955 isl_token_free(tok
);
1956 isl_token_free(tok2
);
1959 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1960 tok
->u
.v
, tok2
->u
.v
);
1961 isl_token_free(tok2
);
1963 isl_stream_push_token(s
, tok2
);
1964 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1967 isl_token_free(tok
);
1968 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1969 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1970 isl_qpolynomial
*qp
;
1971 isl_token_free(tok
);
1972 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1973 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1974 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1975 isl_qpolynomial
*qp
;
1976 isl_token_free(tok
);
1977 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1978 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1979 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1981 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1983 isl_qpolynomial
*qp
;
1985 isl_token_free(tok
);
1989 vars_drop(v
, v
->n
- n
);
1990 isl_stream_error(s
, tok
, "unknown identifier");
1991 isl_token_free(tok
);
1994 isl_token_free(tok
);
1995 pow
= optional_power(s
);
1996 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
1997 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1998 } else if (is_start_of_div(tok
)) {
2002 isl_stream_push_token(s
, tok
);
2003 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2004 pow
= optional_power(s
);
2005 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2006 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2007 } else if (tok
->type
== '-') {
2008 isl_token_free(tok
);
2009 pwqp
= read_factor(s
, map
, v
);
2010 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2012 isl_stream_error(s
, tok
, "unexpected isl_token");
2013 isl_stream_push_token(s
, tok
);
2017 if (isl_stream_eat_if_available(s
, '*') ||
2018 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2019 isl_pw_qpolynomial
*pwqp2
;
2021 pwqp2
= read_factor(s
, map
, v
);
2022 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2027 isl_pw_qpolynomial_free(pwqp
);
2031 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
2032 __isl_keep isl_map
*map
, struct vars
*v
)
2034 struct isl_token
*tok
;
2035 isl_pw_qpolynomial
*pwqp
;
2037 pwqp
= read_factor(s
, map
, v
);
2040 tok
= next_token(s
);
2044 if (tok
->type
== '+') {
2045 isl_pw_qpolynomial
*pwqp2
;
2047 isl_token_free(tok
);
2048 pwqp2
= read_factor(s
, map
, v
);
2049 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2050 } else if (tok
->type
== '-') {
2051 isl_pw_qpolynomial
*pwqp2
;
2053 isl_token_free(tok
);
2054 pwqp2
= read_factor(s
, map
, v
);
2055 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2056 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2057 isl_int_is_neg(tok
->u
.v
)) {
2058 isl_pw_qpolynomial
*pwqp2
;
2060 isl_stream_push_token(s
, tok
);
2061 pwqp2
= read_factor(s
, map
, v
);
2062 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2064 isl_stream_push_token(s
, tok
);
2072 static __isl_give isl_map
*read_optional_formula(struct isl_stream
*s
,
2073 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2075 struct isl_token
*tok
;
2077 tok
= isl_stream_next_token(s
);
2079 isl_stream_error(s
, NULL
, "unexpected EOF");
2082 if (tok
->type
== ':' ||
2083 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2084 isl_token_free(tok
);
2085 map
= read_formula(s
, v
, map
, rational
);
2087 isl_stream_push_token(s
, tok
);
2095 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
2096 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2098 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2099 isl_pw_qpolynomial
*pwqp
;
2100 struct isl_set
*set
;
2102 pwqp
= read_term(s
, map
, v
);
2103 map
= read_optional_formula(s
, map
, v
, 0);
2104 set
= isl_map_range(map
);
2106 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2108 vars_drop(v
, v
->n
- n
);
2114 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
2115 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2117 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2118 isl_pw_qpolynomial
*pwqp
;
2119 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2121 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2122 return obj_read_poly(s
, set
, v
, n
);
2124 if (isl_stream_eat(s
, '('))
2127 pwqp
= read_term(s
, set
, v
);
2128 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2130 while (isl_stream_eat_if_available(s
, ',')) {
2131 isl_pw_qpolynomial_fold
*pwf_i
;
2132 pwqp
= read_term(s
, set
, v
);
2133 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2135 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2138 if (isl_stream_eat(s
, ')'))
2141 set
= read_optional_formula(s
, set
, v
, 0);
2142 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2144 vars_drop(v
, v
->n
- n
);
2150 isl_pw_qpolynomial_fold_free(pwf
);
2151 obj
.type
= isl_obj_none
;
2155 static int is_rational(struct isl_stream
*s
)
2157 struct isl_token
*tok
;
2159 tok
= isl_stream_next_token(s
);
2162 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2163 isl_token_free(tok
);
2164 isl_stream_eat(s
, ':');
2168 isl_stream_push_token(s
, tok
);
2173 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2174 __isl_take isl_map
*map
, struct vars
*v
)
2176 struct isl_token
*tok
;
2177 struct isl_obj obj
= { isl_obj_set
, NULL
};
2181 rational
= is_rational(s
);
2183 map
= isl_map_set_rational(map
);
2185 if (isl_stream_next_token_is(s
, ':')) {
2186 obj
.type
= isl_obj_set
;
2187 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2191 if (!next_is_tuple(s
))
2192 return obj_read_poly_or_fold(s
, map
, v
, n
);
2194 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2197 tok
= isl_stream_next_token(s
);
2200 if (tok
->type
== ISL_TOKEN_TO
) {
2201 obj
.type
= isl_obj_map
;
2202 isl_token_free(tok
);
2203 if (!next_is_tuple(s
)) {
2204 isl_set
*set
= isl_map_domain(map
);
2205 return obj_read_poly_or_fold(s
, set
, v
, n
);
2207 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2211 map
= isl_map_domain(map
);
2212 isl_stream_push_token(s
, tok
);
2215 map
= read_optional_formula(s
, map
, v
, rational
);
2217 vars_drop(v
, v
->n
- n
);
2223 obj
.type
= isl_obj_none
;
2227 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2229 if (obj
.type
== isl_obj_map
) {
2230 obj
.v
= isl_union_map_from_map(obj
.v
);
2231 obj
.type
= isl_obj_union_map
;
2232 } else if (obj
.type
== isl_obj_set
) {
2233 obj
.v
= isl_union_set_from_set(obj
.v
);
2234 obj
.type
= isl_obj_union_set
;
2235 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2236 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2237 obj
.type
= isl_obj_union_pw_qpolynomial
;
2238 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2239 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2240 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2242 isl_assert(ctx
, 0, goto error
);
2245 obj
.type
->free(obj
.v
);
2246 obj
.type
= isl_obj_none
;
2250 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2251 struct isl_obj obj1
, struct isl_obj obj2
)
2253 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2254 obj1
= to_union(ctx
, obj1
);
2255 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2256 obj2
= to_union(ctx
, obj2
);
2257 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2258 obj1
= to_union(ctx
, obj1
);
2259 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2260 obj2
= to_union(ctx
, obj2
);
2261 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2262 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2263 obj1
= to_union(ctx
, obj1
);
2264 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2265 obj2
.type
== isl_obj_pw_qpolynomial
)
2266 obj2
= to_union(ctx
, obj2
);
2267 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2268 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2269 obj1
= to_union(ctx
, obj1
);
2270 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2271 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2272 obj2
= to_union(ctx
, obj2
);
2273 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2274 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2275 obj1
= to_union(ctx
, obj1
);
2276 obj2
= to_union(ctx
, obj2
);
2278 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2279 obj1
= to_union(ctx
, obj1
);
2280 obj2
= to_union(ctx
, obj2
);
2282 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2283 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2284 obj1
= to_union(ctx
, obj1
);
2285 obj2
= to_union(ctx
, obj2
);
2287 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2288 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2289 obj1
= to_union(ctx
, obj1
);
2290 obj2
= to_union(ctx
, obj2
);
2292 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2295 obj1
.type
->free(obj1
.v
);
2296 obj2
.type
->free(obj2
.v
);
2297 obj1
.type
= isl_obj_none
;
2302 static struct isl_obj
obj_read(struct isl_stream
*s
)
2304 isl_map
*map
= NULL
;
2305 struct isl_token
*tok
;
2306 struct vars
*v
= NULL
;
2307 struct isl_obj obj
= { isl_obj_set
, NULL
};
2309 tok
= next_token(s
);
2311 isl_stream_error(s
, NULL
, "unexpected EOF");
2314 if (tok
->type
== ISL_TOKEN_VALUE
) {
2315 struct isl_token
*tok2
;
2316 struct isl_map
*map
;
2318 tok2
= isl_stream_next_token(s
);
2319 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2320 isl_int_is_neg(tok2
->u
.v
)) {
2322 isl_stream_push_token(s
, tok2
);
2323 obj
.type
= isl_obj_val
;
2324 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2325 isl_token_free(tok
);
2328 isl_stream_push_token(s
, tok2
);
2329 isl_stream_push_token(s
, tok
);
2330 map
= map_read_polylib(s
);
2333 if (isl_map_may_be_set(map
))
2334 obj
.v
= isl_map_range(map
);
2336 obj
.type
= isl_obj_map
;
2341 v
= vars_new(s
->ctx
);
2343 isl_stream_push_token(s
, tok
);
2346 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2347 if (tok
->type
== '[') {
2348 isl_stream_push_token(s
, tok
);
2349 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2352 tok
= isl_stream_next_token(s
);
2353 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2354 isl_stream_error(s
, tok
, "expecting '->'");
2356 isl_stream_push_token(s
, tok
);
2359 isl_token_free(tok
);
2360 tok
= isl_stream_next_token(s
);
2362 if (!tok
|| tok
->type
!= '{') {
2363 isl_stream_error(s
, tok
, "expecting '{'");
2365 isl_stream_push_token(s
, tok
);
2368 isl_token_free(tok
);
2370 tok
= isl_stream_next_token(s
);
2373 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2374 isl_token_free(tok
);
2375 if (isl_stream_eat(s
, '='))
2377 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2380 } else if (tok
->type
== '}') {
2381 obj
.type
= isl_obj_union_set
;
2382 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2383 isl_token_free(tok
);
2386 isl_stream_push_token(s
, tok
);
2391 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2392 if (o
.type
== isl_obj_none
|| !o
.v
)
2397 obj
= obj_add(s
->ctx
, obj
, o
);
2398 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2401 tok
= isl_stream_next_token(s
);
2402 if (!tok
|| tok
->type
!= ';')
2404 isl_token_free(tok
);
2405 if (isl_stream_next_token_is(s
, '}')) {
2406 tok
= isl_stream_next_token(s
);
2411 if (tok
&& tok
->type
== '}') {
2412 isl_token_free(tok
);
2414 isl_stream_error(s
, tok
, "unexpected isl_token");
2416 isl_token_free(tok
);
2426 obj
.type
->free(obj
.v
);
2433 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2438 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2444 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2445 obj
.type
== isl_obj_set
, goto error
);
2447 if (obj
.type
== isl_obj_set
)
2448 obj
.v
= isl_map_from_range(obj
.v
);
2452 obj
.type
->free(obj
.v
);
2456 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2462 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2463 obj
.v
= isl_map_range(obj
.v
);
2464 obj
.type
= isl_obj_set
;
2466 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2471 obj
.type
->free(obj
.v
);
2475 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2480 if (obj
.type
== isl_obj_map
) {
2481 obj
.type
= isl_obj_union_map
;
2482 obj
.v
= isl_union_map_from_map(obj
.v
);
2484 if (obj
.type
== isl_obj_set
) {
2485 obj
.type
= isl_obj_union_set
;
2486 obj
.v
= isl_union_set_from_set(obj
.v
);
2488 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2489 isl_union_set_is_empty(obj
.v
))
2490 obj
.type
= isl_obj_union_map
;
2491 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2492 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2496 obj
.type
->free(obj
.v
);
2500 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2505 if (obj
.type
== isl_obj_set
) {
2506 obj
.type
= isl_obj_union_set
;
2507 obj
.v
= isl_union_set_from_set(obj
.v
);
2510 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2514 obj
.type
->free(obj
.v
);
2518 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2521 struct isl_map
*map
;
2522 struct isl_basic_map
*bmap
;
2529 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2532 bmap
= isl_basic_map_empty_like_map(map
);
2534 bmap
= isl_basic_map_copy(map
->p
[0]);
2544 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2546 isl_basic_map
*bmap
;
2547 bmap
= basic_map_read(s
);
2550 if (!isl_basic_map_may_be_set(bmap
))
2551 isl_die(s
->ctx
, isl_error_invalid
,
2552 "input is not a set", goto error
);
2553 return isl_basic_map_range(bmap
);
2555 isl_basic_map_free(bmap
);
2559 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2562 struct isl_basic_map
*bmap
;
2563 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2566 bmap
= basic_map_read(s
);
2571 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2574 isl_basic_set
*bset
;
2575 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2578 bset
= basic_set_read(s
);
2583 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2586 struct isl_basic_map
*bmap
;
2587 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2590 bmap
= basic_map_read(s
);
2595 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2598 isl_basic_set
*bset
;
2599 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2602 bset
= basic_set_read(s
);
2607 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2610 struct isl_map
*map
;
2611 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2614 map
= isl_stream_read_map(s
);
2619 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2622 struct isl_map
*map
;
2623 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2626 map
= isl_stream_read_map(s
);
2631 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2635 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2638 set
= isl_stream_read_set(s
);
2643 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2647 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2650 set
= isl_stream_read_set(s
);
2655 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2658 isl_union_map
*umap
;
2659 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2662 umap
= isl_stream_read_union_map(s
);
2667 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2670 isl_union_map
*umap
;
2671 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2674 umap
= isl_stream_read_union_map(s
);
2679 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2682 isl_union_set
*uset
;
2683 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2686 uset
= isl_stream_read_union_set(s
);
2691 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2694 isl_union_set
*uset
;
2695 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2698 uset
= isl_stream_read_union_set(s
);
2703 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2705 struct isl_vec
*vec
= NULL
;
2706 struct isl_token
*tok
;
2710 tok
= isl_stream_next_token(s
);
2711 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2712 isl_stream_error(s
, tok
, "expecting vector length");
2716 size
= isl_int_get_si(tok
->u
.v
);
2717 isl_token_free(tok
);
2719 vec
= isl_vec_alloc(s
->ctx
, size
);
2721 for (j
= 0; j
< size
; ++j
) {
2722 tok
= isl_stream_next_token(s
);
2723 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2724 isl_stream_error(s
, tok
, "expecting constant value");
2727 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2728 isl_token_free(tok
);
2733 isl_token_free(tok
);
2738 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2740 return isl_vec_read_polylib(s
);
2743 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2746 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2754 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2755 struct isl_stream
*s
)
2761 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2766 obj
.type
->free(obj
.v
);
2770 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2773 isl_pw_qpolynomial
*pwqp
;
2774 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2777 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2782 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2785 isl_pw_qpolynomial
*pwqp
;
2786 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2789 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2794 /* Is the next token an identifer not in "v"?
2796 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2800 struct isl_token
*tok
;
2802 tok
= isl_stream_next_token(s
);
2805 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2806 isl_stream_push_token(s
, tok
);
2808 vars_drop(v
, v
->n
- n
);
2813 /* First read the domain of the affine expression, which may be
2814 * a parameter space or a set.
2815 * The tricky part is that we don't know if the domain is a set or not,
2816 * so when we are trying to read the domain, we may actually be reading
2817 * the affine expression itself (defined on a parameter domains)
2818 * If the tuple we are reading is named, we assume it's the domain.
2819 * Also, if inside the tuple, the first thing we find is a nested tuple
2820 * or a new identifier, we again assume it's the domain.
2821 * Otherwise, we assume we are reading an affine expression.
2823 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2824 __isl_take isl_set
*dom
, struct vars
*v
)
2826 struct isl_token
*tok
;
2828 tok
= isl_stream_next_token(s
);
2829 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2830 isl_stream_push_token(s
, tok
);
2831 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2833 if (!tok
|| tok
->type
!= '[') {
2834 isl_stream_error(s
, tok
, "expecting '['");
2837 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2838 isl_stream_push_token(s
, tok
);
2839 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2841 isl_stream_push_token(s
, tok
);
2846 isl_stream_push_token(s
, tok
);
2851 /* Read an affine expression from "s".
2853 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2858 ma
= isl_stream_read_multi_aff(s
);
2861 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2862 isl_die(s
->ctx
, isl_error_invalid
,
2863 "expecting single affine expression",
2866 aff
= isl_multi_aff_get_aff(ma
, 0);
2867 isl_multi_aff_free(ma
);
2870 isl_multi_aff_free(ma
);
2874 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2876 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2877 __isl_take isl_set
*dom
, struct vars
*v
)
2879 isl_pw_aff
*pwaff
= NULL
;
2881 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2884 if (isl_stream_eat(s
, '['))
2887 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2889 if (isl_stream_eat(s
, ']'))
2892 dom
= read_optional_formula(s
, dom
, v
, 0);
2893 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2898 isl_pw_aff_free(pwaff
);
2902 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2905 isl_set
*dom
= NULL
;
2907 isl_pw_aff
*pa
= NULL
;
2910 v
= vars_new(s
->ctx
);
2914 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2915 if (next_is_tuple(s
)) {
2916 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2917 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2920 if (isl_stream_eat(s
, '{'))
2924 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2925 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2926 vars_drop(v
, v
->n
- n
);
2928 while (isl_stream_eat_if_available(s
, ';')) {
2932 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2933 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2934 vars_drop(v
, v
->n
- n
);
2936 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2939 if (isl_stream_eat(s
, '}'))
2948 isl_pw_aff_free(pa
);
2952 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2955 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2958 aff
= isl_stream_read_aff(s
);
2963 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2966 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2969 pa
= isl_stream_read_pw_aff(s
);
2974 /* Read an isl_pw_multi_aff from "s".
2975 * We currently read a generic object and if it turns out to be a set or
2976 * a map, we convert that to an isl_pw_multi_aff.
2977 * It would be more efficient if we were to construct the isl_pw_multi_aff
2980 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
2988 if (obj
.type
== isl_obj_map
)
2989 return isl_pw_multi_aff_from_map(obj
.v
);
2990 if (obj
.type
== isl_obj_set
)
2991 return isl_pw_multi_aff_from_set(obj
.v
);
2993 obj
.type
->free(obj
.v
);
2994 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2998 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3001 isl_pw_multi_aff
*pma
;
3002 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3005 pma
= isl_stream_read_pw_multi_aff(s
);
3010 /* Read an isl_union_pw_multi_aff from "s".
3011 * We currently read a generic object and if it turns out to be a set or
3012 * a map, we convert that to an isl_union_pw_multi_aff.
3013 * It would be more efficient if we were to construct
3014 * the isl_union_pw_multi_aff directly.
3016 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3017 struct isl_stream
*s
)
3025 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3026 obj
= to_union(s
->ctx
, obj
);
3027 if (obj
.type
== isl_obj_union_map
)
3028 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3029 if (obj
.type
== isl_obj_union_set
)
3030 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3032 obj
.type
->free(obj
.v
);
3033 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3037 /* Read an isl_union_pw_multi_aff from "str".
3039 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3040 isl_ctx
*ctx
, const char *str
)
3042 isl_union_pw_multi_aff
*upma
;
3043 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3046 upma
= isl_stream_read_union_pw_multi_aff(s
);
3051 /* Assuming "pa" represents a single affine expression defined on a universe
3052 * domain, extract this affine expression.
3054 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3061 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3062 "expecting single affine expression",
3064 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3065 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3066 "expecting universe domain",
3069 aff
= isl_aff_copy(pa
->p
[0].aff
);
3070 isl_pw_aff_free(pa
);
3073 isl_pw_aff_free(pa
);
3077 /* Read a multi-affine expression from "s".
3078 * If the multi-affine expression has a domain, then then tuple
3079 * representing this domain cannot involve any affine expressions.
3080 * The tuple representing the actual expressions needs to consist
3081 * of only affine expressions. Moreover, these expressions can
3082 * only depend on parameters and input dimensions and not on other
3083 * output dimensions.
3085 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
3088 isl_set
*dom
= NULL
;
3089 isl_multi_pw_aff
*tuple
= NULL
;
3091 isl_space
*space
, *dom_space
;
3092 isl_multi_aff
*ma
= NULL
;
3094 v
= vars_new(s
->ctx
);
3098 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3099 if (next_is_tuple(s
)) {
3100 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3101 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3104 if (!isl_set_plain_is_universe(dom
))
3105 isl_die(s
->ctx
, isl_error_invalid
,
3106 "expecting universe parameter domain", goto error
);
3107 if (isl_stream_eat(s
, '{'))
3110 tuple
= read_tuple(s
, v
, 0, 0);
3113 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3118 has_expr
= tuple_has_expr(tuple
);
3122 isl_die(s
->ctx
, isl_error_invalid
,
3123 "expecting universe domain", goto error
);
3124 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3125 set
= isl_set_universe(space
);
3126 dom
= isl_set_intersect_params(set
, dom
);
3127 isl_multi_pw_aff_free(tuple
);
3128 tuple
= read_tuple(s
, v
, 0, 0);
3133 if (isl_stream_eat(s
, '}'))
3136 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3137 dim
= isl_set_dim(dom
, isl_dim_all
);
3138 dom_space
= isl_set_get_space(dom
);
3139 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3140 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3141 if (!isl_space_is_params(dom_space
))
3142 space
= isl_space_map_from_domain_and_range(
3143 isl_space_copy(dom_space
), space
);
3144 isl_space_free(dom_space
);
3145 ma
= isl_multi_aff_alloc(space
);
3147 for (i
= 0; i
< n
; ++i
) {
3150 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3151 aff
= aff_from_pw_aff(pa
);
3154 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3156 isl_die(s
->ctx
, isl_error_invalid
,
3157 "not an affine expression", goto error
);
3159 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3160 space
= isl_multi_aff_get_domain_space(ma
);
3161 aff
= isl_aff_reset_domain_space(aff
, space
);
3162 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3165 isl_multi_pw_aff_free(tuple
);
3170 isl_multi_pw_aff_free(tuple
);
3173 isl_multi_aff_free(ma
);
3177 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3180 isl_multi_aff
*maff
;
3181 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3184 maff
= isl_stream_read_multi_aff(s
);
3189 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3190 struct isl_stream
*s
)
3195 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3196 obj
.type
= isl_obj_union_pw_qpolynomial
;
3197 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3200 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3205 obj
.type
->free(obj
.v
);
3209 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3210 isl_ctx
*ctx
, const char *str
)
3212 isl_union_pw_qpolynomial
*upwqp
;
3213 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3216 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);