2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 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>
29 #include <isl_val_private.h>
34 struct variable
*next
;
43 static struct vars
*vars_new(struct isl_ctx
*ctx
)
46 v
= isl_alloc_type(ctx
, struct vars
);
55 static void variable_free(struct variable
*var
)
58 struct variable
*next
= var
->next
;
65 static void vars_free(struct vars
*v
)
73 static void vars_drop(struct vars
*v
, int n
)
84 struct variable
*next
= var
->next
;
92 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
96 var
= isl_calloc_type(v
->ctx
, struct variable
);
99 var
->name
= strdup(name
);
100 var
->name
[len
] = '\0';
109 static int vars_pos(struct vars
*v
, const char *s
, int len
)
116 for (q
= v
->v
; q
; q
= q
->next
) {
117 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
124 v
->v
= variable_new(v
, s
, len
, v
->n
);
132 static int vars_add_anon(struct vars
*v
)
134 v
->v
= variable_new(v
, "", 0, v
->n
);
143 /* Obtain next token, with some preprocessing.
144 * In particular, evaluate expressions of the form x^y,
145 * with x and y values.
147 static struct isl_token
*next_token(struct isl_stream
*s
)
149 struct isl_token
*tok
, *tok2
;
151 tok
= isl_stream_next_token(s
);
152 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
154 if (!isl_stream_eat_if_available(s
, '^'))
156 tok2
= isl_stream_next_token(s
);
157 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
158 isl_stream_error(s
, tok2
, "expecting constant value");
162 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
164 isl_token_free(tok2
);
168 isl_token_free(tok2
);
172 /* Read an isl_val from "s".
174 * The following token sequences are recognized
177 * "-" "infty" -> -infty
182 * where n, d and v are integer constants.
184 __isl_give isl_val
*isl_stream_read_val(struct isl_stream
*s
)
186 struct isl_token
*tok
= NULL
;
187 struct isl_token
*tok2
= NULL
;
192 isl_stream_error(s
, NULL
, "unexpected EOF");
195 if (tok
->type
== ISL_TOKEN_INFTY
) {
197 return isl_val_infty(s
->ctx
);
199 if (tok
->type
== '-' &&
200 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
202 return isl_val_neginfty(s
->ctx
);
204 if (tok
->type
== ISL_TOKEN_NAN
) {
206 return isl_val_nan(s
->ctx
);
208 if (tok
->type
!= ISL_TOKEN_VALUE
) {
209 isl_stream_error(s
, tok
, "expecting value");
213 if (isl_stream_eat_if_available(s
, '/')) {
214 tok2
= next_token(s
);
216 isl_stream_error(s
, NULL
, "unexpected EOF");
219 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
220 isl_stream_error(s
, tok2
, "expecting value");
223 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
224 val
= isl_val_normalize(val
);
226 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
230 isl_token_free(tok2
);
234 isl_token_free(tok2
);
238 /* Read an isl_val from "str".
240 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
244 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
247 val
= isl_stream_read_val(s
);
252 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
254 struct isl_token
*tok
;
257 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
258 isl_stream_error(s
, tok
, "expecting constant value");
262 isl_int_mul(*f
, *f
, tok
->u
.v
);
266 if (isl_stream_eat_if_available(s
, '*'))
267 return accept_cst_factor(s
, f
);
275 /* Given an affine expression aff, return an affine expression
276 * for aff % d, with d the next token on the stream, which is
277 * assumed to be a constant.
279 * We introduce an integer division q = [aff/d] and the result
280 * is set to aff - d q.
282 static __isl_give isl_pw_aff
*affine_mod(struct isl_stream
*s
,
283 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
285 struct isl_token
*tok
;
289 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
290 isl_stream_error(s
, tok
, "expecting constant value");
294 q
= isl_pw_aff_copy(aff
);
295 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
296 q
= isl_pw_aff_floor(q
);
297 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
299 aff
= isl_pw_aff_sub(aff
, q
);
304 isl_pw_aff_free(aff
);
309 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
310 __isl_take isl_space
*dim
, struct vars
*v
);
311 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
312 __isl_take isl_space
*dim
, struct vars
*v
);
314 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
315 __isl_take isl_space
*dim
, struct vars
*v
)
317 struct isl_token
*tok
;
318 isl_pw_aff_list
*list
= NULL
;
321 tok
= isl_stream_next_token(s
);
324 min
= tok
->type
== ISL_TOKEN_MIN
;
327 if (isl_stream_eat(s
, '('))
330 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
334 if (isl_stream_eat(s
, ')'))
338 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
341 isl_pw_aff_list_free(list
);
345 /* Is "tok" the start of an integer division?
347 static int is_start_of_div(struct isl_token
*tok
)
351 if (tok
->type
== '[')
353 if (tok
->type
== ISL_TOKEN_FLOOR
)
355 if (tok
->type
== ISL_TOKEN_CEIL
)
357 if (tok
->type
== ISL_TOKEN_FLOORD
)
359 if (tok
->type
== ISL_TOKEN_CEILD
)
364 /* Read an integer division from "s" and return it as an isl_pw_aff.
366 * The integer division can be of the form
368 * [<affine expression>]
369 * floor(<affine expression>)
370 * ceil(<affine expression>)
371 * floord(<affine expression>,<denominator>)
372 * ceild(<affine expression>,<denominator>)
374 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
375 __isl_take isl_space
*dim
, struct vars
*v
)
377 struct isl_token
*tok
;
381 isl_pw_aff
*pwaff
= NULL
;
383 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
385 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
387 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
389 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
392 if (isl_stream_eat(s
, '('))
395 if (isl_stream_eat(s
, '['))
399 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
402 if (isl_stream_eat(s
, ','))
408 if (tok
->type
!= ISL_TOKEN_VALUE
) {
409 isl_stream_error(s
, tok
, "expected denominator");
410 isl_stream_push_token(s
, tok
);
413 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
418 pwaff
= isl_pw_aff_ceil(pwaff
);
420 pwaff
= isl_pw_aff_floor(pwaff
);
423 if (isl_stream_eat(s
, ')'))
426 if (isl_stream_eat(s
, ']'))
434 isl_pw_aff_free(pwaff
);
438 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
439 __isl_take isl_space
*dim
, struct vars
*v
)
441 struct isl_token
*tok
= NULL
;
442 isl_pw_aff
*res
= NULL
;
446 isl_stream_error(s
, NULL
, "unexpected EOF");
450 if (tok
->type
== ISL_TOKEN_AFF
) {
451 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
453 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
455 int pos
= vars_pos(v
, tok
->u
.s
, -1);
461 vars_drop(v
, v
->n
- n
);
462 isl_stream_error(s
, tok
, "unknown identifier");
466 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
469 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
470 res
= isl_pw_aff_from_aff(aff
);
472 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
473 if (isl_stream_eat_if_available(s
, '*')) {
474 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
475 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
479 ls
= isl_local_space_from_space(isl_space_copy(dim
));
480 aff
= isl_aff_zero_on_domain(ls
);
481 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
482 res
= isl_pw_aff_from_aff(aff
);
485 } else if (tok
->type
== '(') {
488 res
= accept_affine(s
, isl_space_copy(dim
), v
);
491 if (isl_stream_eat(s
, ')'))
493 } else if (is_start_of_div(tok
)) {
494 isl_stream_push_token(s
, tok
);
496 res
= accept_div(s
, isl_space_copy(dim
), v
);
497 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
498 isl_stream_push_token(s
, tok
);
500 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
502 isl_stream_error(s
, tok
, "expecting factor");
505 if (isl_stream_eat_if_available(s
, '%') ||
506 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
508 return affine_mod(s
, v
, res
);
510 if (isl_stream_eat_if_available(s
, '*')) {
513 isl_int_set_si(f
, 1);
514 if (accept_cst_factor(s
, &f
) < 0) {
518 res
= isl_pw_aff_scale(res
, f
);
521 if (isl_stream_eat_if_available(s
, '/')) {
524 isl_int_set_si(f
, 1);
525 if (accept_cst_factor(s
, &f
) < 0) {
529 res
= isl_pw_aff_scale_down(res
, f
);
538 isl_pw_aff_free(res
);
543 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
548 space
= isl_pw_aff_get_domain_space(pwaff
);
549 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
550 aff
= isl_aff_add_constant(aff
, v
);
552 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
555 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
556 __isl_take isl_space
*dim
, struct vars
*v
)
558 struct isl_token
*tok
= NULL
;
563 ls
= isl_local_space_from_space(isl_space_copy(dim
));
564 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
571 isl_stream_error(s
, NULL
, "unexpected EOF");
574 if (tok
->type
== '-') {
579 if (tok
->type
== '(' || is_start_of_div(tok
) ||
580 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
581 tok
->type
== ISL_TOKEN_IDENT
||
582 tok
->type
== ISL_TOKEN_AFF
) {
584 isl_stream_push_token(s
, tok
);
586 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
588 res
= isl_pw_aff_sub(res
, term
);
590 res
= isl_pw_aff_add(res
, term
);
594 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
596 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
597 if (isl_stream_eat_if_available(s
, '*') ||
598 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
600 term
= accept_affine_factor(s
,
601 isl_space_copy(dim
), v
);
602 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
603 res
= isl_pw_aff_add(res
, term
);
607 res
= add_cst(res
, tok
->u
.v
);
611 isl_stream_error(s
, tok
, "unexpected isl_token");
612 isl_stream_push_token(s
, tok
);
613 isl_pw_aff_free(res
);
620 if (tok
&& tok
->type
== '-') {
623 } else if (tok
&& tok
->type
== '+') {
626 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
627 isl_int_is_neg(tok
->u
.v
)) {
628 isl_stream_push_token(s
, tok
);
631 isl_stream_push_token(s
, tok
);
641 isl_pw_aff_free(res
);
645 static int is_comparator(struct isl_token
*tok
)
663 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
664 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
665 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
666 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
668 /* Accept a ternary operator, given the first argument.
670 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
671 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
674 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
679 if (isl_stream_eat(s
, '?'))
682 dim
= isl_space_wrap(isl_map_get_space(cond
));
683 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
687 if (isl_stream_eat(s
, ':'))
690 dim
= isl_pw_aff_get_domain_space(pwaff1
);
691 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
695 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
696 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
699 isl_pw_aff_free(pwaff1
);
700 isl_pw_aff_free(pwaff2
);
704 /* Accept an affine expression that may involve ternary operators.
705 * We first read an affine expression.
706 * If it is not followed by a comparison operator, we simply return it.
707 * Otherwise, we assume the affine epxression is part of the first
708 * argument of a ternary operator and try to parse that.
710 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
711 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
716 struct isl_token
*tok
;
717 int line
= -1, col
= -1;
720 tok
= isl_stream_next_token(s
);
724 isl_stream_push_token(s
, tok
);
727 pwaff
= accept_affine(s
, dim
, v
);
729 pwaff
= isl_pw_aff_set_rational(pwaff
);
733 tok
= isl_stream_next_token(s
);
735 return isl_pw_aff_free(pwaff
);
737 is_comp
= is_comparator(tok
);
738 isl_stream_push_token(s
, tok
);
742 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
744 return isl_pw_aff_free(pwaff
);
745 tok
->type
= ISL_TOKEN_AFF
;
746 tok
->u
.pwaff
= pwaff
;
748 space
= isl_pw_aff_get_domain_space(pwaff
);
749 cond
= isl_map_universe(isl_space_unwrap(space
));
751 isl_stream_push_token(s
, tok
);
753 cond
= read_formula(s
, v
, cond
, rational
);
755 return accept_ternary(s
, cond
, v
, rational
);
758 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
759 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
766 if (type
== isl_dim_param
)
767 pos
= isl_map_dim(map
, isl_dim_param
);
769 pos
= isl_map_dim(map
, isl_dim_in
);
770 if (type
== isl_dim_out
)
771 pos
+= isl_map_dim(map
, isl_dim_out
);
776 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
778 def_map
= isl_map_from_pw_aff(def
);
779 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
780 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
782 map
= isl_map_intersect(map
, def_map
);
787 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
788 __isl_take isl_space
*dim
, struct vars
*v
)
791 isl_pw_aff_list
*list
;
792 struct isl_token
*tok
= NULL
;
794 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
795 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
800 tok
= isl_stream_next_token(s
);
802 isl_stream_error(s
, NULL
, "unexpected EOF");
805 if (tok
->type
!= ',') {
806 isl_stream_push_token(s
, tok
);
811 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
812 list
= isl_pw_aff_list_concat(list
,
813 isl_pw_aff_list_from_pw_aff(pwaff
));
822 isl_pw_aff_list_free(list
);
826 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
827 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
829 struct isl_token
*tok
;
831 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
835 if (tok
->type
!= ISL_TOKEN_IDENT
)
838 p
= vars_pos(v
, tok
->u
.s
, -1);
842 isl_stream_error(s
, tok
, "expecting unique identifier");
846 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
849 tok
= isl_stream_next_token(s
);
850 if (tok
&& tok
->type
== '=') {
852 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
853 tok
= isl_stream_next_token(s
);
856 if (!tok
|| tok
->type
!= ',')
862 isl_stream_push_token(s
, tok
);
871 static int next_is_tuple(struct isl_stream
*s
)
873 struct isl_token
*tok
;
876 tok
= isl_stream_next_token(s
);
879 if (tok
->type
== '[') {
880 isl_stream_push_token(s
, tok
);
883 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
884 isl_stream_push_token(s
, tok
);
888 is_tuple
= isl_stream_next_token_is(s
, '[');
890 isl_stream_push_token(s
, tok
);
895 /* Allocate an initial tuple with zero dimensions and an anonymous,
896 * unstructured space.
897 * A tuple is represented as an isl_multi_pw_aff.
898 * The range space is the space of the tuple.
899 * The domain space is an anonymous space
900 * with a dimension for each variable in the set of variables in "v".
901 * If a given dimension is not defined in terms of earlier dimensions in
902 * the input, then the corresponding isl_pw_aff is set equal to one time
903 * the variable corresponding to the dimension being defined.
905 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
907 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
910 /* Is "pa" an expression in term of earlier dimensions?
911 * The alternative is that the dimension is defined to be equal to itself,
912 * meaning that it has a universe domain and an expression that depends
913 * on itself. "i" is the position of the expression in a sequence
914 * of "n" expressions. The final dimensions of "pa" correspond to
915 * these "n" expressions.
917 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
925 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
929 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
934 /* Does the tuple contain any dimensions that are defined
935 * in terms of earlier dimensions?
937 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
945 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
946 for (i
= 0; i
< n
; ++i
) {
947 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
948 has_expr
= pw_aff_is_expr(pa
, i
, n
);
950 if (has_expr
< 0 || has_expr
)
957 /* Add a dimension to the given tuple.
958 * The dimension is initially undefined, so it is encoded
959 * as one times itself.
961 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
962 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
968 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
969 space
= isl_multi_pw_aff_get_domain_space(tuple
);
970 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
971 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
972 pa
= isl_pw_aff_from_aff(aff
);
973 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
974 isl_multi_pw_aff_from_pw_aff(pa
));
979 /* Set the name of dimension "pos" in "tuple" to "name".
980 * During printing, we add primes if the same name appears more than once
981 * to distinguish the occurrences. Here, we remove those primes from "name"
982 * before setting the name of the dimension.
984 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
985 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
992 prime
= strchr(name
, '\'');
995 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
1002 /* Read an affine expression from "s" and replace the definition
1003 * of dimension "pos" in "tuple" by this expression.
1005 * accept_extended_affine requires a wrapped space as input.
1006 * The domain space of "tuple", on the other hand is an anonymous space,
1007 * so we have to adjust the space of the isl_pw_aff before adding it
1010 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
1011 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
1017 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1018 def
= accept_extended_affine(s
, space
, v
, rational
);
1019 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1020 def
= isl_pw_aff_reset_domain_space(def
, space
);
1021 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
1026 /* Read a list of variables and/or affine expressions and return the list
1027 * as an isl_multi_pw_aff.
1028 * The elements in the list are separated by either "," or "][".
1029 * If "comma" is set then only "," is allowed.
1031 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
1032 struct vars
*v
, int rational
, int comma
)
1035 struct isl_token
*tok
;
1036 isl_multi_pw_aff
*res
;
1038 res
= tuple_alloc(v
);
1040 if (isl_stream_next_token_is(s
, ']'))
1043 while ((tok
= next_token(s
)) != NULL
) {
1046 res
= tuple_add_dim(res
, v
);
1048 if (tok
->type
== ISL_TOKEN_IDENT
) {
1050 int p
= vars_pos(v
, tok
->u
.s
, -1);
1056 if (tok
->type
== '*') {
1057 if (vars_add_anon(v
) < 0)
1059 isl_token_free(tok
);
1060 } else if (new_name
) {
1061 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
1062 isl_token_free(tok
);
1063 if (isl_stream_eat_if_available(s
, '='))
1064 res
= read_tuple_var_def(s
, res
, i
, v
,
1067 isl_stream_push_token(s
, tok
);
1069 if (vars_add_anon(v
) < 0)
1071 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
1074 tok
= isl_stream_next_token(s
);
1075 if (!comma
&& tok
&& tok
->type
== ']' &&
1076 isl_stream_next_token_is(s
, '[')) {
1077 isl_token_free(tok
);
1078 tok
= isl_stream_next_token(s
);
1079 } else if (!tok
|| tok
->type
!= ',')
1082 isl_token_free(tok
);
1086 isl_stream_push_token(s
, tok
);
1090 isl_token_free(tok
);
1091 return isl_multi_pw_aff_free(res
);
1094 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1096 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1097 struct vars
*v
, int rational
, int comma
)
1099 struct isl_token
*tok
;
1101 isl_multi_pw_aff
*res
= NULL
;
1103 tok
= isl_stream_next_token(s
);
1106 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1107 name
= strdup(tok
->u
.s
);
1108 isl_token_free(tok
);
1112 isl_stream_push_token(s
, tok
);
1113 if (isl_stream_eat(s
, '['))
1115 if (next_is_tuple(s
)) {
1116 isl_multi_pw_aff
*out
;
1118 res
= read_tuple(s
, v
, rational
, comma
);
1119 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1121 out
= read_tuple(s
, v
, rational
, comma
);
1122 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1123 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1124 res
= isl_multi_pw_aff_range_product(res
, out
);
1126 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1127 if (isl_stream_eat(s
, ']'))
1131 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1138 return isl_multi_pw_aff_free(res
);
1141 /* Read a tuple from "s" and add it to "map".
1142 * The tuple is initially represented as an isl_multi_pw_aff.
1143 * We first create the appropriate space in "map" based on the range
1144 * space of this isl_multi_pw_aff. Then, we add equalities based
1145 * on the affine expressions. These live in an anonymous space,
1146 * however, so we first need to reset the space to that of "map".
1148 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1149 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1150 int rational
, int comma
)
1153 isl_multi_pw_aff
*tuple
;
1154 isl_space
*space
= NULL
;
1156 tuple
= read_tuple(s
, v
, rational
, comma
);
1160 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1161 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1165 if (type
== isl_dim_param
) {
1166 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1167 isl_space_is_wrapping(space
)) {
1168 isl_die(s
->ctx
, isl_error_invalid
,
1169 "parameter tuples cannot be named or nested",
1172 map
= isl_map_add_dims(map
, type
, n
);
1173 for (i
= 0; i
< n
; ++i
) {
1175 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1176 isl_die(s
->ctx
, isl_error_invalid
,
1177 "parameters must be named",
1179 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1180 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1182 } else if (type
== isl_dim_in
) {
1185 set
= isl_set_universe(isl_space_copy(space
));
1187 set
= isl_set_set_rational(set
);
1188 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1189 map
= isl_map_from_domain(set
);
1193 set
= isl_set_universe(isl_space_copy(space
));
1195 set
= isl_set_set_rational(set
);
1196 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1199 for (i
= 0; i
< n
; ++i
) {
1206 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1207 space
= isl_pw_aff_get_domain_space(pa
);
1208 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1209 aff
= isl_aff_add_coefficient_si(aff
,
1210 isl_dim_in
, v
->n
- n
+ i
, -1);
1211 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1213 pa
= isl_pw_aff_set_rational(pa
);
1214 set
= isl_pw_aff_zero_set(pa
);
1215 map_i
= isl_map_from_range(set
);
1216 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1217 map
= isl_map_intersect(map
, map_i
);
1220 isl_space_free(space
);
1221 isl_multi_pw_aff_free(tuple
);
1224 isl_space_free(space
);
1225 isl_multi_pw_aff_free(tuple
);
1230 static __isl_give isl_set
*construct_constraints(
1231 __isl_take isl_set
*set
, int type
,
1232 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1237 left
= isl_pw_aff_list_copy(left
);
1238 right
= isl_pw_aff_list_copy(right
);
1240 left
= isl_pw_aff_list_set_rational(left
);
1241 right
= isl_pw_aff_list_set_rational(right
);
1243 if (type
== ISL_TOKEN_LE
)
1244 cond
= isl_pw_aff_list_le_set(left
, right
);
1245 else if (type
== ISL_TOKEN_GE
)
1246 cond
= isl_pw_aff_list_ge_set(left
, right
);
1247 else if (type
== ISL_TOKEN_LT
)
1248 cond
= isl_pw_aff_list_lt_set(left
, right
);
1249 else if (type
== ISL_TOKEN_GT
)
1250 cond
= isl_pw_aff_list_gt_set(left
, right
);
1251 else if (type
== ISL_TOKEN_NE
)
1252 cond
= isl_pw_aff_list_ne_set(left
, right
);
1254 cond
= isl_pw_aff_list_eq_set(left
, right
);
1256 return isl_set_intersect(set
, cond
);
1259 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1260 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1262 struct isl_token
*tok
= NULL
;
1263 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1266 set
= isl_map_wrap(map
);
1267 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1270 tok
= isl_stream_next_token(s
);
1271 if (!is_comparator(tok
)) {
1272 isl_stream_error(s
, tok
, "missing operator");
1274 isl_stream_push_token(s
, tok
);
1279 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1283 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1285 isl_token_free(tok
);
1286 isl_pw_aff_list_free(list1
);
1289 tok
= isl_stream_next_token(s
);
1290 if (!is_comparator(tok
)) {
1292 isl_stream_push_token(s
, tok
);
1296 isl_pw_aff_list_free(list1
);
1298 return isl_set_unwrap(set
);
1301 isl_token_free(tok
);
1302 isl_pw_aff_list_free(list1
);
1303 isl_pw_aff_list_free(list2
);
1308 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1309 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1312 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1314 map
= isl_map_from_domain(isl_map_wrap(map
));
1315 map
= read_defined_var_list(s
, v
, map
, rational
);
1317 if (isl_stream_eat(s
, ':'))
1320 map
= read_formula(s
, v
, map
, rational
);
1321 map
= isl_set_unwrap(isl_map_domain(map
));
1323 vars_drop(v
, v
->n
- n
);
1324 if (seen_paren
&& isl_stream_eat(s
, ')'))
1333 /* Parse an expression between parentheses and push the result
1334 * back on the stream.
1336 * The parsed expression may be either an affine expression
1337 * or a condition. The first type is pushed onto the stream
1338 * as an isl_pw_aff, while the second is pushed as an isl_map.
1340 * If the initial token indicates the start of a condition,
1341 * we parse it as such.
1342 * Otherwise, we first parse an affine expression and push
1343 * that onto the stream. If the affine expression covers the
1344 * entire expression between parentheses, we return.
1345 * Otherwise, we assume that the affine expression is the
1346 * start of a condition and continue parsing.
1348 static int resolve_paren_expr(struct isl_stream
*s
,
1349 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1351 struct isl_token
*tok
, *tok2
;
1355 tok
= isl_stream_next_token(s
);
1356 if (!tok
|| tok
->type
!= '(')
1359 if (isl_stream_next_token_is(s
, '('))
1360 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1363 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1364 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1365 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1366 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1367 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1368 map
= read_formula(s
, v
, map
, rational
);
1369 if (isl_stream_eat(s
, ')'))
1371 tok
->type
= ISL_TOKEN_MAP
;
1373 isl_stream_push_token(s
, tok
);
1377 tok2
= isl_stream_next_token(s
);
1382 isl_stream_push_token(s
, tok2
);
1384 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1388 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1391 tok2
->type
= ISL_TOKEN_AFF
;
1392 tok2
->u
.pwaff
= pwaff
;
1394 if (isl_stream_eat_if_available(s
, ')')) {
1395 isl_stream_push_token(s
, tok2
);
1396 isl_token_free(tok
);
1401 isl_stream_push_token(s
, tok2
);
1403 map
= read_formula(s
, v
, map
, rational
);
1404 if (isl_stream_eat(s
, ')'))
1407 tok
->type
= ISL_TOKEN_MAP
;
1409 isl_stream_push_token(s
, tok
);
1413 isl_pw_aff_free(pwaff
);
1415 isl_token_free(tok
);
1420 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1421 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1423 if (isl_stream_next_token_is(s
, '('))
1424 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1427 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1428 struct isl_token
*tok
;
1429 tok
= isl_stream_next_token(s
);
1433 map
= isl_map_copy(tok
->u
.map
);
1434 isl_token_free(tok
);
1438 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1439 return read_exists(s
, v
, map
, rational
);
1441 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1444 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1445 isl_space
*dim
= isl_map_get_space(map
);
1447 return isl_map_empty(dim
);
1450 return add_constraint(s
, v
, map
, rational
);
1456 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1457 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1462 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1463 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1465 res
= isl_map_subtract(isl_map_copy(map
), res
);
1467 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1470 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1471 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1473 res
= isl_map_subtract(res
, res_i
);
1475 res
= isl_map_intersect(res
, res_i
);
1482 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1483 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1487 if (isl_stream_next_token_is(s
, '}')) {
1488 isl_space
*dim
= isl_map_get_space(map
);
1490 return isl_map_universe(dim
);
1493 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1494 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1497 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1498 res
= isl_map_union(res
, res_i
);
1505 /* Read a first order formula from "s", add the corresponding
1506 * constraints to "map" and return the result.
1508 * In particular, read a formula of the form
1516 * where a and b are disjunctions.
1518 * In the first case, map is replaced by
1520 * map \cap { [..] : a }
1522 * In the second case, it is replaced by
1524 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1526 static __isl_give isl_map
*read_formula(struct isl_stream
*s
,
1527 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1531 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1533 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1536 res
= isl_map_subtract(isl_map_copy(map
), res
);
1537 res2
= read_disjuncts(s
, v
, map
, rational
);
1538 res
= isl_map_union(res
, res2
);
1545 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1547 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1548 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1549 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1550 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1552 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1553 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1554 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1556 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1557 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1558 isl_basic_map_dim(bmap
, isl_dim_in
) +
1559 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1560 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1562 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1568 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1569 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1572 struct isl_token
*tok
;
1582 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1583 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1585 tok
= isl_stream_next_token(s
);
1586 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1587 isl_stream_error(s
, tok
, "expecting coefficient");
1589 isl_stream_push_token(s
, tok
);
1592 if (!tok
->on_new_line
) {
1593 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1594 isl_stream_push_token(s
, tok
);
1598 type
= isl_int_get_si(tok
->u
.v
);
1599 isl_token_free(tok
);
1601 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1603 k
= isl_basic_map_alloc_equality(bmap
);
1606 k
= isl_basic_map_alloc_inequality(bmap
);
1612 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1614 tok
= isl_stream_next_token(s
);
1615 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1616 isl_stream_error(s
, tok
, "expecting coefficient");
1618 isl_stream_push_token(s
, tok
);
1621 if (tok
->on_new_line
) {
1622 isl_stream_error(s
, tok
,
1623 "coefficient should not appear on new line");
1624 isl_stream_push_token(s
, tok
);
1627 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1628 isl_int_set(c
[pos
], tok
->u
.v
);
1629 isl_token_free(tok
);
1634 isl_basic_map_free(bmap
);
1638 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1641 struct isl_token
*tok
;
1642 struct isl_token
*tok2
;
1645 unsigned in
= 0, out
, local
= 0;
1646 struct isl_basic_map
*bmap
= NULL
;
1649 tok
= isl_stream_next_token(s
);
1651 isl_stream_error(s
, NULL
, "unexpected EOF");
1654 tok2
= isl_stream_next_token(s
);
1656 isl_token_free(tok
);
1657 isl_stream_error(s
, NULL
, "unexpected EOF");
1660 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1661 isl_stream_push_token(s
, tok2
);
1662 isl_stream_push_token(s
, tok
);
1663 isl_stream_error(s
, NULL
,
1664 "expecting constraint matrix dimensions");
1667 n_row
= isl_int_get_si(tok
->u
.v
);
1668 n_col
= isl_int_get_si(tok2
->u
.v
);
1669 on_new_line
= tok2
->on_new_line
;
1670 isl_token_free(tok2
);
1671 isl_token_free(tok
);
1672 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1673 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1674 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1675 tok
= isl_stream_next_token_on_same_line(s
);
1677 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1678 isl_stream_error(s
, tok
,
1679 "expecting number of output dimensions");
1680 isl_stream_push_token(s
, tok
);
1683 out
= isl_int_get_si(tok
->u
.v
);
1684 isl_token_free(tok
);
1686 tok
= isl_stream_next_token_on_same_line(s
);
1687 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1688 isl_stream_error(s
, tok
,
1689 "expecting number of input dimensions");
1691 isl_stream_push_token(s
, tok
);
1694 in
= isl_int_get_si(tok
->u
.v
);
1695 isl_token_free(tok
);
1697 tok
= isl_stream_next_token_on_same_line(s
);
1698 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1699 isl_stream_error(s
, tok
,
1700 "expecting number of existentials");
1702 isl_stream_push_token(s
, tok
);
1705 local
= isl_int_get_si(tok
->u
.v
);
1706 isl_token_free(tok
);
1708 tok
= isl_stream_next_token_on_same_line(s
);
1709 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1710 isl_stream_error(s
, tok
,
1711 "expecting number of parameters");
1713 isl_stream_push_token(s
, tok
);
1716 nparam
= isl_int_get_si(tok
->u
.v
);
1717 isl_token_free(tok
);
1718 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1719 isl_stream_error(s
, NULL
,
1720 "dimensions don't match");
1724 out
= n_col
- 2 - nparam
;
1725 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1729 for (i
= 0; i
< local
; ++i
) {
1730 int k
= isl_basic_map_alloc_div(bmap
);
1733 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1736 for (i
= 0; i
< n_row
; ++i
)
1737 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1739 tok
= isl_stream_next_token_on_same_line(s
);
1741 isl_stream_error(s
, tok
, "unexpected extra token on line");
1742 isl_stream_push_token(s
, tok
);
1746 bmap
= isl_basic_map_simplify(bmap
);
1747 bmap
= isl_basic_map_finalize(bmap
);
1750 isl_basic_map_free(bmap
);
1754 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1756 struct isl_token
*tok
;
1757 struct isl_token
*tok2
;
1759 struct isl_map
*map
;
1761 tok
= isl_stream_next_token(s
);
1763 isl_stream_error(s
, NULL
, "unexpected EOF");
1766 tok2
= isl_stream_next_token_on_same_line(s
);
1767 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1768 isl_stream_push_token(s
, tok2
);
1769 isl_stream_push_token(s
, tok
);
1770 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1773 isl_stream_error(s
, tok2
, "unexpected token");
1774 isl_stream_push_token(s
, tok2
);
1775 isl_stream_push_token(s
, tok
);
1778 n
= isl_int_get_si(tok
->u
.v
);
1779 isl_token_free(tok
);
1781 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1783 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1785 for (i
= 1; map
&& i
< n
; ++i
)
1786 map
= isl_map_union(map
,
1787 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1792 static int optional_power(struct isl_stream
*s
)
1795 struct isl_token
*tok
;
1797 tok
= isl_stream_next_token(s
);
1800 if (tok
->type
!= '^') {
1801 isl_stream_push_token(s
, tok
);
1804 isl_token_free(tok
);
1805 tok
= isl_stream_next_token(s
);
1806 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1807 isl_stream_error(s
, tok
, "expecting exponent");
1809 isl_stream_push_token(s
, tok
);
1812 pow
= isl_int_get_si(tok
->u
.v
);
1813 isl_token_free(tok
);
1817 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1818 __isl_keep isl_map
*map
, struct vars
*v
);
1820 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1821 __isl_keep isl_map
*map
, struct vars
*v
)
1823 isl_pw_qpolynomial
*pwqp
;
1824 struct isl_token
*tok
;
1826 tok
= next_token(s
);
1828 isl_stream_error(s
, NULL
, "unexpected EOF");
1831 if (tok
->type
== '(') {
1834 isl_token_free(tok
);
1835 pwqp
= read_term(s
, map
, v
);
1838 if (isl_stream_eat(s
, ')'))
1840 pow
= optional_power(s
);
1841 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1842 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1843 struct isl_token
*tok2
;
1844 isl_qpolynomial
*qp
;
1846 tok2
= isl_stream_next_token(s
);
1847 if (tok2
&& tok2
->type
== '/') {
1848 isl_token_free(tok2
);
1849 tok2
= next_token(s
);
1850 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1851 isl_stream_error(s
, tok2
, "expected denominator");
1852 isl_token_free(tok
);
1853 isl_token_free(tok2
);
1856 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1857 tok
->u
.v
, tok2
->u
.v
);
1858 isl_token_free(tok2
);
1860 isl_stream_push_token(s
, tok2
);
1861 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1864 isl_token_free(tok
);
1865 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1866 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1867 isl_qpolynomial
*qp
;
1868 isl_token_free(tok
);
1869 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1870 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1871 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1872 isl_qpolynomial
*qp
;
1873 isl_token_free(tok
);
1874 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1875 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1876 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1878 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1880 isl_qpolynomial
*qp
;
1882 isl_token_free(tok
);
1886 vars_drop(v
, v
->n
- n
);
1887 isl_stream_error(s
, tok
, "unknown identifier");
1888 isl_token_free(tok
);
1891 isl_token_free(tok
);
1892 pow
= optional_power(s
);
1893 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
1894 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1895 } else if (is_start_of_div(tok
)) {
1899 isl_stream_push_token(s
, tok
);
1900 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
1901 pow
= optional_power(s
);
1902 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
1903 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1904 } else if (tok
->type
== '-') {
1905 isl_token_free(tok
);
1906 pwqp
= read_factor(s
, map
, v
);
1907 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
1909 isl_stream_error(s
, tok
, "unexpected isl_token");
1910 isl_stream_push_token(s
, tok
);
1914 if (isl_stream_eat_if_available(s
, '*') ||
1915 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1916 isl_pw_qpolynomial
*pwqp2
;
1918 pwqp2
= read_factor(s
, map
, v
);
1919 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
1924 isl_pw_qpolynomial_free(pwqp
);
1928 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1929 __isl_keep isl_map
*map
, struct vars
*v
)
1931 struct isl_token
*tok
;
1932 isl_pw_qpolynomial
*pwqp
;
1934 pwqp
= read_factor(s
, map
, v
);
1937 tok
= next_token(s
);
1941 if (tok
->type
== '+') {
1942 isl_pw_qpolynomial
*pwqp2
;
1944 isl_token_free(tok
);
1945 pwqp2
= read_factor(s
, map
, v
);
1946 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1947 } else if (tok
->type
== '-') {
1948 isl_pw_qpolynomial
*pwqp2
;
1950 isl_token_free(tok
);
1951 pwqp2
= read_factor(s
, map
, v
);
1952 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
1953 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1954 isl_int_is_neg(tok
->u
.v
)) {
1955 isl_pw_qpolynomial
*pwqp2
;
1957 isl_stream_push_token(s
, tok
);
1958 pwqp2
= read_factor(s
, map
, v
);
1959 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1961 isl_stream_push_token(s
, tok
);
1969 static __isl_give isl_map
*read_optional_formula(struct isl_stream
*s
,
1970 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
1972 struct isl_token
*tok
;
1974 tok
= isl_stream_next_token(s
);
1976 isl_stream_error(s
, NULL
, "unexpected EOF");
1979 if (tok
->type
== ':' ||
1980 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
1981 isl_token_free(tok
);
1982 map
= read_formula(s
, v
, map
, rational
);
1984 isl_stream_push_token(s
, tok
);
1992 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1993 __isl_take isl_map
*map
, struct vars
*v
, int n
)
1995 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1996 isl_pw_qpolynomial
*pwqp
;
1997 struct isl_set
*set
;
1999 pwqp
= read_term(s
, map
, v
);
2000 map
= read_optional_formula(s
, map
, v
, 0);
2001 set
= isl_map_range(map
);
2003 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2005 vars_drop(v
, v
->n
- n
);
2011 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
2012 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2014 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2015 isl_pw_qpolynomial
*pwqp
;
2016 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2018 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2019 return obj_read_poly(s
, set
, v
, n
);
2021 if (isl_stream_eat(s
, '('))
2024 pwqp
= read_term(s
, set
, v
);
2025 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2027 while (isl_stream_eat_if_available(s
, ',')) {
2028 isl_pw_qpolynomial_fold
*pwf_i
;
2029 pwqp
= read_term(s
, set
, v
);
2030 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2032 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2035 if (isl_stream_eat(s
, ')'))
2038 set
= read_optional_formula(s
, set
, v
, 0);
2039 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2041 vars_drop(v
, v
->n
- n
);
2047 isl_pw_qpolynomial_fold_free(pwf
);
2048 obj
.type
= isl_obj_none
;
2052 static int is_rational(struct isl_stream
*s
)
2054 struct isl_token
*tok
;
2056 tok
= isl_stream_next_token(s
);
2059 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2060 isl_token_free(tok
);
2061 isl_stream_eat(s
, ':');
2065 isl_stream_push_token(s
, tok
);
2070 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2071 __isl_take isl_map
*map
, struct vars
*v
)
2073 struct isl_token
*tok
;
2074 struct isl_obj obj
= { isl_obj_set
, NULL
};
2078 rational
= is_rational(s
);
2080 map
= isl_map_set_rational(map
);
2082 if (isl_stream_next_token_is(s
, ':')) {
2083 obj
.type
= isl_obj_set
;
2084 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2088 if (!next_is_tuple(s
))
2089 return obj_read_poly_or_fold(s
, map
, v
, n
);
2091 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2094 tok
= isl_stream_next_token(s
);
2097 if (tok
->type
== ISL_TOKEN_TO
) {
2098 obj
.type
= isl_obj_map
;
2099 isl_token_free(tok
);
2100 if (!next_is_tuple(s
)) {
2101 isl_set
*set
= isl_map_domain(map
);
2102 return obj_read_poly_or_fold(s
, set
, v
, n
);
2104 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2108 map
= isl_map_domain(map
);
2109 isl_stream_push_token(s
, tok
);
2112 map
= read_optional_formula(s
, map
, v
, rational
);
2114 vars_drop(v
, v
->n
- n
);
2120 obj
.type
= isl_obj_none
;
2124 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2126 if (obj
.type
== isl_obj_map
) {
2127 obj
.v
= isl_union_map_from_map(obj
.v
);
2128 obj
.type
= isl_obj_union_map
;
2129 } else if (obj
.type
== isl_obj_set
) {
2130 obj
.v
= isl_union_set_from_set(obj
.v
);
2131 obj
.type
= isl_obj_union_set
;
2132 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2133 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2134 obj
.type
= isl_obj_union_pw_qpolynomial
;
2135 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2136 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2137 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2139 isl_assert(ctx
, 0, goto error
);
2142 obj
.type
->free(obj
.v
);
2143 obj
.type
= isl_obj_none
;
2147 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2148 struct isl_obj obj1
, struct isl_obj obj2
)
2150 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2151 obj1
= to_union(ctx
, obj1
);
2152 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2153 obj2
= to_union(ctx
, obj2
);
2154 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2155 obj1
= to_union(ctx
, obj1
);
2156 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2157 obj2
= to_union(ctx
, obj2
);
2158 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2159 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2160 obj1
= to_union(ctx
, obj1
);
2161 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2162 obj2
.type
== isl_obj_pw_qpolynomial
)
2163 obj2
= to_union(ctx
, obj2
);
2164 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2165 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2166 obj1
= to_union(ctx
, obj1
);
2167 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2168 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2169 obj2
= to_union(ctx
, obj2
);
2170 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2171 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2172 obj1
= to_union(ctx
, obj1
);
2173 obj2
= to_union(ctx
, obj2
);
2175 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2176 obj1
= to_union(ctx
, obj1
);
2177 obj2
= to_union(ctx
, obj2
);
2179 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2180 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2181 obj1
= to_union(ctx
, obj1
);
2182 obj2
= to_union(ctx
, obj2
);
2184 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2185 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2186 obj1
= to_union(ctx
, obj1
);
2187 obj2
= to_union(ctx
, obj2
);
2189 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2192 obj1
.type
->free(obj1
.v
);
2193 obj2
.type
->free(obj2
.v
);
2194 obj1
.type
= isl_obj_none
;
2199 static struct isl_obj
obj_read(struct isl_stream
*s
)
2201 isl_map
*map
= NULL
;
2202 struct isl_token
*tok
;
2203 struct vars
*v
= NULL
;
2204 struct isl_obj obj
= { isl_obj_set
, NULL
};
2206 tok
= next_token(s
);
2208 isl_stream_error(s
, NULL
, "unexpected EOF");
2211 if (tok
->type
== ISL_TOKEN_VALUE
) {
2212 struct isl_token
*tok2
;
2213 struct isl_map
*map
;
2215 tok2
= isl_stream_next_token(s
);
2216 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2217 isl_int_is_neg(tok2
->u
.v
)) {
2219 isl_stream_push_token(s
, tok2
);
2220 obj
.type
= isl_obj_int
;
2221 obj
.v
= isl_int_obj_alloc(s
->ctx
, tok
->u
.v
);
2222 isl_token_free(tok
);
2225 isl_stream_push_token(s
, tok2
);
2226 isl_stream_push_token(s
, tok
);
2227 map
= map_read_polylib(s
);
2230 if (isl_map_may_be_set(map
))
2231 obj
.v
= isl_map_range(map
);
2233 obj
.type
= isl_obj_map
;
2238 v
= vars_new(s
->ctx
);
2240 isl_stream_push_token(s
, tok
);
2243 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2244 if (tok
->type
== '[') {
2245 isl_stream_push_token(s
, tok
);
2246 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2249 tok
= isl_stream_next_token(s
);
2250 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2251 isl_stream_error(s
, tok
, "expecting '->'");
2253 isl_stream_push_token(s
, tok
);
2256 isl_token_free(tok
);
2257 tok
= isl_stream_next_token(s
);
2259 if (!tok
|| tok
->type
!= '{') {
2260 isl_stream_error(s
, tok
, "expecting '{'");
2262 isl_stream_push_token(s
, tok
);
2265 isl_token_free(tok
);
2267 tok
= isl_stream_next_token(s
);
2270 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2271 isl_token_free(tok
);
2272 if (isl_stream_eat(s
, '='))
2274 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2277 } else if (tok
->type
== '}') {
2278 obj
.type
= isl_obj_union_set
;
2279 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2280 isl_token_free(tok
);
2283 isl_stream_push_token(s
, tok
);
2288 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2289 if (o
.type
== isl_obj_none
|| !o
.v
)
2294 obj
= obj_add(s
->ctx
, obj
, o
);
2295 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2298 tok
= isl_stream_next_token(s
);
2299 if (!tok
|| tok
->type
!= ';')
2301 isl_token_free(tok
);
2302 if (isl_stream_next_token_is(s
, '}')) {
2303 tok
= isl_stream_next_token(s
);
2308 if (tok
&& tok
->type
== '}') {
2309 isl_token_free(tok
);
2311 isl_stream_error(s
, tok
, "unexpected isl_token");
2313 isl_token_free(tok
);
2323 obj
.type
->free(obj
.v
);
2330 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2335 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2341 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2342 obj
.type
== isl_obj_set
, goto error
);
2344 if (obj
.type
== isl_obj_set
)
2345 obj
.v
= isl_map_from_range(obj
.v
);
2349 obj
.type
->free(obj
.v
);
2353 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2359 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2360 obj
.v
= isl_map_range(obj
.v
);
2361 obj
.type
= isl_obj_set
;
2363 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2368 obj
.type
->free(obj
.v
);
2372 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2377 if (obj
.type
== isl_obj_map
) {
2378 obj
.type
= isl_obj_union_map
;
2379 obj
.v
= isl_union_map_from_map(obj
.v
);
2381 if (obj
.type
== isl_obj_set
) {
2382 obj
.type
= isl_obj_union_set
;
2383 obj
.v
= isl_union_set_from_set(obj
.v
);
2385 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2386 isl_union_set_is_empty(obj
.v
))
2387 obj
.type
= isl_obj_union_map
;
2388 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2389 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2393 obj
.type
->free(obj
.v
);
2397 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2402 if (obj
.type
== isl_obj_set
) {
2403 obj
.type
= isl_obj_union_set
;
2404 obj
.v
= isl_union_set_from_set(obj
.v
);
2407 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2411 obj
.type
->free(obj
.v
);
2415 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2418 struct isl_map
*map
;
2419 struct isl_basic_map
*bmap
;
2426 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2429 bmap
= isl_basic_map_empty_like_map(map
);
2431 bmap
= isl_basic_map_copy(map
->p
[0]);
2441 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2443 isl_basic_map
*bmap
;
2444 bmap
= basic_map_read(s
);
2447 if (!isl_basic_map_may_be_set(bmap
))
2448 isl_die(s
->ctx
, isl_error_invalid
,
2449 "input is not a set", goto error
);
2450 return isl_basic_map_range(bmap
);
2452 isl_basic_map_free(bmap
);
2456 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2459 struct isl_basic_map
*bmap
;
2460 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2463 bmap
= basic_map_read(s
);
2468 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2471 isl_basic_set
*bset
;
2472 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2475 bset
= basic_set_read(s
);
2480 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2483 struct isl_basic_map
*bmap
;
2484 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2487 bmap
= basic_map_read(s
);
2492 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2495 isl_basic_set
*bset
;
2496 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2499 bset
= basic_set_read(s
);
2504 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2507 struct isl_map
*map
;
2508 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2511 map
= isl_stream_read_map(s
);
2516 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2519 struct isl_map
*map
;
2520 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2523 map
= isl_stream_read_map(s
);
2528 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2532 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2535 set
= isl_stream_read_set(s
);
2540 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2544 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2547 set
= isl_stream_read_set(s
);
2552 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2555 isl_union_map
*umap
;
2556 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2559 umap
= isl_stream_read_union_map(s
);
2564 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2567 isl_union_map
*umap
;
2568 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2571 umap
= isl_stream_read_union_map(s
);
2576 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2579 isl_union_set
*uset
;
2580 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2583 uset
= isl_stream_read_union_set(s
);
2588 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2591 isl_union_set
*uset
;
2592 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2595 uset
= isl_stream_read_union_set(s
);
2600 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2602 struct isl_vec
*vec
= NULL
;
2603 struct isl_token
*tok
;
2607 tok
= isl_stream_next_token(s
);
2608 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2609 isl_stream_error(s
, tok
, "expecting vector length");
2613 size
= isl_int_get_si(tok
->u
.v
);
2614 isl_token_free(tok
);
2616 vec
= isl_vec_alloc(s
->ctx
, size
);
2618 for (j
= 0; j
< size
; ++j
) {
2619 tok
= isl_stream_next_token(s
);
2620 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2621 isl_stream_error(s
, tok
, "expecting constant value");
2624 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2625 isl_token_free(tok
);
2630 isl_token_free(tok
);
2635 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2637 return isl_vec_read_polylib(s
);
2640 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2643 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2651 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2652 struct isl_stream
*s
)
2658 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2663 obj
.type
->free(obj
.v
);
2667 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2670 isl_pw_qpolynomial
*pwqp
;
2671 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2674 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2679 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2682 isl_pw_qpolynomial
*pwqp
;
2683 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2686 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2691 /* Is the next token an identifer not in "v"?
2693 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2697 struct isl_token
*tok
;
2699 tok
= isl_stream_next_token(s
);
2702 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2703 isl_stream_push_token(s
, tok
);
2705 vars_drop(v
, v
->n
- n
);
2710 /* First read the domain of the affine expression, which may be
2711 * a parameter space or a set.
2712 * The tricky part is that we don't know if the domain is a set or not,
2713 * so when we are trying to read the domain, we may actually be reading
2714 * the affine expression itself (defined on a parameter domains)
2715 * If the tuple we are reading is named, we assume it's the domain.
2716 * Also, if inside the tuple, the first thing we find is a nested tuple
2717 * or a new identifier, we again assume it's the domain.
2718 * Otherwise, we assume we are reading an affine expression.
2720 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2721 __isl_take isl_set
*dom
, struct vars
*v
)
2723 struct isl_token
*tok
;
2725 tok
= isl_stream_next_token(s
);
2726 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2727 isl_stream_push_token(s
, tok
);
2728 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2730 if (!tok
|| tok
->type
!= '[') {
2731 isl_stream_error(s
, tok
, "expecting '['");
2734 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2735 isl_stream_push_token(s
, tok
);
2736 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2738 isl_stream_push_token(s
, tok
);
2743 isl_stream_push_token(s
, tok
);
2748 /* Read an affine expression from "s".
2750 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2755 ma
= isl_stream_read_multi_aff(s
);
2758 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2759 isl_die(s
->ctx
, isl_error_invalid
,
2760 "expecting single affine expression",
2763 aff
= isl_multi_aff_get_aff(ma
, 0);
2764 isl_multi_aff_free(ma
);
2767 isl_multi_aff_free(ma
);
2771 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2773 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2774 __isl_take isl_set
*dom
, struct vars
*v
)
2776 isl_pw_aff
*pwaff
= NULL
;
2778 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2781 if (isl_stream_eat(s
, '['))
2784 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2786 if (isl_stream_eat(s
, ']'))
2789 dom
= read_optional_formula(s
, dom
, v
, 0);
2790 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2795 isl_pw_aff_free(pwaff
);
2799 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2802 isl_set
*dom
= NULL
;
2804 isl_pw_aff
*pa
= NULL
;
2807 v
= vars_new(s
->ctx
);
2811 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2812 if (next_is_tuple(s
)) {
2813 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2814 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2817 if (isl_stream_eat(s
, '{'))
2821 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2822 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2823 vars_drop(v
, v
->n
- n
);
2825 while (isl_stream_eat_if_available(s
, ';')) {
2829 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2830 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2831 vars_drop(v
, v
->n
- n
);
2833 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2836 if (isl_stream_eat(s
, '}'))
2845 isl_pw_aff_free(pa
);
2849 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2852 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2855 aff
= isl_stream_read_aff(s
);
2860 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2863 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2866 pa
= isl_stream_read_pw_aff(s
);
2871 /* Read an isl_pw_multi_aff from "s".
2872 * We currently read a generic object and if it turns out to be a set or
2873 * a map, we convert that to an isl_pw_multi_aff.
2874 * It would be more efficient if we were to construct the isl_pw_multi_aff
2877 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
2885 if (obj
.type
== isl_obj_map
)
2886 return isl_pw_multi_aff_from_map(obj
.v
);
2887 if (obj
.type
== isl_obj_set
)
2888 return isl_pw_multi_aff_from_set(obj
.v
);
2890 obj
.type
->free(obj
.v
);
2891 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2895 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
2898 isl_pw_multi_aff
*pma
;
2899 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2902 pma
= isl_stream_read_pw_multi_aff(s
);
2907 /* Read an isl_union_pw_multi_aff from "s".
2908 * We currently read a generic object and if it turns out to be a set or
2909 * a map, we convert that to an isl_union_pw_multi_aff.
2910 * It would be more efficient if we were to construct
2911 * the isl_union_pw_multi_aff directly.
2913 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
2914 struct isl_stream
*s
)
2922 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
2923 obj
= to_union(s
->ctx
, obj
);
2924 if (obj
.type
== isl_obj_union_map
)
2925 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
2926 if (obj
.type
== isl_obj_union_set
)
2927 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
2929 obj
.type
->free(obj
.v
);
2930 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2934 /* Read an isl_union_pw_multi_aff from "str".
2936 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
2937 isl_ctx
*ctx
, const char *str
)
2939 isl_union_pw_multi_aff
*upma
;
2940 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2943 upma
= isl_stream_read_union_pw_multi_aff(s
);
2948 /* Assuming "pa" represents a single affine expression defined on a universe
2949 * domain, extract this affine expression.
2951 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
2958 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2959 "expecting single affine expression",
2961 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
2962 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2963 "expecting universe domain",
2966 aff
= isl_aff_copy(pa
->p
[0].aff
);
2967 isl_pw_aff_free(pa
);
2970 isl_pw_aff_free(pa
);
2974 /* Read a multi-affine expression from "s".
2975 * If the multi-affine expression has a domain, then then tuple
2976 * representing this domain cannot involve any affine expressions.
2977 * The tuple representing the actual expressions needs to consist
2978 * of only affine expressions. Moreover, these expressions can
2979 * only depend on parameters and input dimensions and not on other
2980 * output dimensions.
2982 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
2985 isl_set
*dom
= NULL
;
2986 isl_multi_pw_aff
*tuple
= NULL
;
2988 isl_space
*space
, *dom_space
;
2989 isl_multi_aff
*ma
= NULL
;
2991 v
= vars_new(s
->ctx
);
2995 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2996 if (next_is_tuple(s
)) {
2997 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2998 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3001 if (!isl_set_plain_is_universe(dom
))
3002 isl_die(s
->ctx
, isl_error_invalid
,
3003 "expecting universe parameter domain", goto error
);
3004 if (isl_stream_eat(s
, '{'))
3007 tuple
= read_tuple(s
, v
, 0, 0);
3010 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3015 has_expr
= tuple_has_expr(tuple
);
3019 isl_die(s
->ctx
, isl_error_invalid
,
3020 "expecting universe domain", goto error
);
3021 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3022 set
= isl_set_universe(space
);
3023 dom
= isl_set_intersect_params(set
, dom
);
3024 isl_multi_pw_aff_free(tuple
);
3025 tuple
= read_tuple(s
, v
, 0, 0);
3030 if (isl_stream_eat(s
, '}'))
3033 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3034 dim
= isl_set_dim(dom
, isl_dim_all
);
3035 dom_space
= isl_set_get_space(dom
);
3036 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3037 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3038 if (!isl_space_is_params(dom_space
))
3039 space
= isl_space_map_from_domain_and_range(
3040 isl_space_copy(dom_space
), space
);
3041 isl_space_free(dom_space
);
3042 ma
= isl_multi_aff_alloc(space
);
3044 for (i
= 0; i
< n
; ++i
) {
3047 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3048 aff
= aff_from_pw_aff(pa
);
3051 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3053 isl_die(s
->ctx
, isl_error_invalid
,
3054 "not an affine expression", goto error
);
3056 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3057 space
= isl_multi_aff_get_domain_space(ma
);
3058 aff
= isl_aff_reset_domain_space(aff
, space
);
3059 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3062 isl_multi_pw_aff_free(tuple
);
3067 isl_multi_pw_aff_free(tuple
);
3070 isl_multi_aff_free(ma
);
3074 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3077 isl_multi_aff
*maff
;
3078 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3081 maff
= isl_stream_read_multi_aff(s
);
3086 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3087 struct isl_stream
*s
)
3092 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3093 obj
.type
= isl_obj_union_pw_qpolynomial
;
3094 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3097 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3102 obj
.type
->free(obj
.v
);
3106 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3107 isl_ctx
*ctx
, const char *str
)
3109 isl_union_pw_qpolynomial
*upwqp
;
3110 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3113 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);