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_FLOORD
)
355 if (tok
->type
== ISL_TOKEN_CEILD
)
360 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
361 __isl_take isl_space
*dim
, struct vars
*v
)
363 struct isl_token
*tok
;
366 isl_pw_aff
*pwaff
= NULL
;
368 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
370 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
373 if (isl_stream_eat(s
, '('))
376 if (isl_stream_eat(s
, '['))
380 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
383 if (isl_stream_eat(s
, ','))
389 if (tok
->type
!= ISL_TOKEN_VALUE
) {
390 isl_stream_error(s
, tok
, "expected denominator");
391 isl_stream_push_token(s
, tok
);
394 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
399 pwaff
= isl_pw_aff_ceil(pwaff
);
401 pwaff
= isl_pw_aff_floor(pwaff
);
404 if (isl_stream_eat(s
, ')'))
407 if (isl_stream_eat(s
, ']'))
415 isl_pw_aff_free(pwaff
);
419 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
420 __isl_take isl_space
*dim
, struct vars
*v
)
422 struct isl_token
*tok
= NULL
;
423 isl_pw_aff
*res
= NULL
;
427 isl_stream_error(s
, NULL
, "unexpected EOF");
431 if (tok
->type
== ISL_TOKEN_AFF
) {
432 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
434 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
436 int pos
= vars_pos(v
, tok
->u
.s
, -1);
442 vars_drop(v
, v
->n
- n
);
443 isl_stream_error(s
, tok
, "unknown identifier");
447 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
450 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
451 res
= isl_pw_aff_from_aff(aff
);
453 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
454 if (isl_stream_eat_if_available(s
, '*')) {
455 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
456 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
460 ls
= isl_local_space_from_space(isl_space_copy(dim
));
461 aff
= isl_aff_zero_on_domain(ls
);
462 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
463 res
= isl_pw_aff_from_aff(aff
);
466 } else if (tok
->type
== '(') {
469 res
= accept_affine(s
, isl_space_copy(dim
), v
);
472 if (isl_stream_eat(s
, ')'))
474 } else if (is_start_of_div(tok
)) {
475 isl_stream_push_token(s
, tok
);
477 res
= accept_div(s
, isl_space_copy(dim
), v
);
478 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
479 isl_stream_push_token(s
, tok
);
481 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
483 isl_stream_error(s
, tok
, "expecting factor");
486 if (isl_stream_eat_if_available(s
, '%') ||
487 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
489 return affine_mod(s
, v
, res
);
491 if (isl_stream_eat_if_available(s
, '*')) {
494 isl_int_set_si(f
, 1);
495 if (accept_cst_factor(s
, &f
) < 0) {
499 res
= isl_pw_aff_scale(res
, f
);
502 if (isl_stream_eat_if_available(s
, '/')) {
505 isl_int_set_si(f
, 1);
506 if (accept_cst_factor(s
, &f
) < 0) {
510 res
= isl_pw_aff_scale_down(res
, f
);
519 isl_pw_aff_free(res
);
524 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
529 space
= isl_pw_aff_get_domain_space(pwaff
);
530 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
531 aff
= isl_aff_add_constant(aff
, v
);
533 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
536 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
537 __isl_take isl_space
*dim
, struct vars
*v
)
539 struct isl_token
*tok
= NULL
;
544 ls
= isl_local_space_from_space(isl_space_copy(dim
));
545 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
552 isl_stream_error(s
, NULL
, "unexpected EOF");
555 if (tok
->type
== '-') {
560 if (tok
->type
== '(' || is_start_of_div(tok
) ||
561 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
562 tok
->type
== ISL_TOKEN_IDENT
||
563 tok
->type
== ISL_TOKEN_AFF
) {
565 isl_stream_push_token(s
, tok
);
567 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
569 res
= isl_pw_aff_sub(res
, term
);
571 res
= isl_pw_aff_add(res
, term
);
575 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
577 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
578 if (isl_stream_eat_if_available(s
, '*') ||
579 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
581 term
= accept_affine_factor(s
,
582 isl_space_copy(dim
), v
);
583 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
584 res
= isl_pw_aff_add(res
, term
);
588 res
= add_cst(res
, tok
->u
.v
);
592 isl_stream_error(s
, tok
, "unexpected isl_token");
593 isl_stream_push_token(s
, tok
);
594 isl_pw_aff_free(res
);
601 if (tok
&& tok
->type
== '-') {
604 } else if (tok
&& tok
->type
== '+') {
607 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
608 isl_int_is_neg(tok
->u
.v
)) {
609 isl_stream_push_token(s
, tok
);
612 isl_stream_push_token(s
, tok
);
622 isl_pw_aff_free(res
);
626 static int is_comparator(struct isl_token
*tok
)
644 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
645 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
646 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
647 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
649 /* Accept a ternary operator, given the first argument.
651 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
652 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
655 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
660 if (isl_stream_eat(s
, '?'))
663 dim
= isl_space_wrap(isl_map_get_space(cond
));
664 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
668 if (isl_stream_eat(s
, ':'))
671 dim
= isl_pw_aff_get_domain_space(pwaff1
);
672 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
676 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
677 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
680 isl_pw_aff_free(pwaff1
);
681 isl_pw_aff_free(pwaff2
);
685 /* Accept an affine expression that may involve ternary operators.
686 * We first read an affine expression.
687 * If it is not followed by a comparison operator, we simply return it.
688 * Otherwise, we assume the affine epxression is part of the first
689 * argument of a ternary operator and try to parse that.
691 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
692 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
697 struct isl_token
*tok
;
698 int line
= -1, col
= -1;
701 tok
= isl_stream_next_token(s
);
705 isl_stream_push_token(s
, tok
);
708 pwaff
= accept_affine(s
, dim
, v
);
710 pwaff
= isl_pw_aff_set_rational(pwaff
);
714 tok
= isl_stream_next_token(s
);
716 return isl_pw_aff_free(pwaff
);
718 is_comp
= is_comparator(tok
);
719 isl_stream_push_token(s
, tok
);
723 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
725 return isl_pw_aff_free(pwaff
);
726 tok
->type
= ISL_TOKEN_AFF
;
727 tok
->u
.pwaff
= pwaff
;
729 space
= isl_pw_aff_get_domain_space(pwaff
);
730 cond
= isl_map_universe(isl_space_unwrap(space
));
732 isl_stream_push_token(s
, tok
);
734 cond
= read_disjuncts(s
, v
, cond
, rational
);
736 return accept_ternary(s
, cond
, v
, rational
);
739 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
740 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
747 if (type
== isl_dim_param
)
748 pos
= isl_map_dim(map
, isl_dim_param
);
750 pos
= isl_map_dim(map
, isl_dim_in
);
751 if (type
== isl_dim_out
)
752 pos
+= isl_map_dim(map
, isl_dim_out
);
757 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
759 def_map
= isl_map_from_pw_aff(def
);
760 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
761 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
763 map
= isl_map_intersect(map
, def_map
);
768 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
769 __isl_take isl_space
*dim
, struct vars
*v
)
772 isl_pw_aff_list
*list
;
773 struct isl_token
*tok
= NULL
;
775 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
776 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
781 tok
= isl_stream_next_token(s
);
783 isl_stream_error(s
, NULL
, "unexpected EOF");
786 if (tok
->type
!= ',') {
787 isl_stream_push_token(s
, tok
);
792 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
793 list
= isl_pw_aff_list_concat(list
,
794 isl_pw_aff_list_from_pw_aff(pwaff
));
803 isl_pw_aff_list_free(list
);
807 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
808 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
810 struct isl_token
*tok
;
812 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
816 if (tok
->type
!= ISL_TOKEN_IDENT
)
819 p
= vars_pos(v
, tok
->u
.s
, -1);
823 isl_stream_error(s
, tok
, "expecting unique identifier");
827 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
830 tok
= isl_stream_next_token(s
);
831 if (tok
&& tok
->type
== '=') {
833 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
834 tok
= isl_stream_next_token(s
);
837 if (!tok
|| tok
->type
!= ',')
843 isl_stream_push_token(s
, tok
);
852 static int next_is_tuple(struct isl_stream
*s
)
854 struct isl_token
*tok
;
857 tok
= isl_stream_next_token(s
);
860 if (tok
->type
== '[') {
861 isl_stream_push_token(s
, tok
);
864 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
865 isl_stream_push_token(s
, tok
);
869 is_tuple
= isl_stream_next_token_is(s
, '[');
871 isl_stream_push_token(s
, tok
);
876 /* Allocate an initial tuple with zero dimensions and an anonymous,
877 * unstructured space.
878 * A tuple is represented as an isl_multi_pw_aff.
879 * The range space is the space of the tuple.
880 * The domain space is an anonymous space
881 * with a dimension for each variable in the set of variables in "v".
882 * If a given dimension is not defined in terms of earlier dimensions in
883 * the input, then the corresponding isl_pw_aff is set equal to one time
884 * the variable corresponding to the dimension being defined.
886 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
888 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
891 /* Is "pa" an expression in term of earlier dimensions?
892 * The alternative is that the dimension is defined to be equal to itself,
893 * meaning that it has a universe domain and an expression that depends
894 * on itself. "i" is the position of the expression in a sequence
895 * of "n" expressions. The final dimensions of "pa" correspond to
896 * these "n" expressions.
898 static int pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
906 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
910 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
915 /* Does the tuple contain any dimensions that are defined
916 * in terms of earlier dimensions?
918 static int tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
926 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
927 for (i
= 0; i
< n
; ++i
) {
928 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
929 has_expr
= pw_aff_is_expr(pa
, i
, n
);
931 if (has_expr
< 0 || has_expr
)
938 /* Add a dimension to the given tuple.
939 * The dimension is initially undefined, so it is encoded
940 * as one times itself.
942 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
943 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
949 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
950 space
= isl_multi_pw_aff_get_domain_space(tuple
);
951 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
952 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
953 pa
= isl_pw_aff_from_aff(aff
);
954 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
955 isl_multi_pw_aff_from_pw_aff(pa
));
960 /* Set the name of dimension "pos" in "tuple" to "name".
961 * During printing, we add primes if the same name appears more than once
962 * to distinguish the occurrences. Here, we remove those primes from "name"
963 * before setting the name of the dimension.
965 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
966 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
973 prime
= strchr(name
, '\'');
976 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
983 /* Read an affine expression from "s" and replace the definition
984 * of dimension "pos" in "tuple" by this expression.
986 * accept_extended_affine requires a wrapped space as input.
987 * The domain space of "tuple", on the other hand is an anonymous space,
988 * so we have to adjust the space of the isl_pw_aff before adding it
991 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
992 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
,
998 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
999 def
= accept_extended_affine(s
, space
, v
, rational
);
1000 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1001 def
= isl_pw_aff_reset_domain_space(def
, space
);
1002 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
1007 /* Read a list of variables and/or affine expressions and return the list
1008 * as an isl_multi_pw_aff.
1009 * The elements in the list are separated by either "," or "][".
1010 * If "comma" is set then only "," is allowed.
1012 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
1013 struct vars
*v
, int rational
, int comma
)
1016 struct isl_token
*tok
;
1017 isl_multi_pw_aff
*res
;
1019 res
= tuple_alloc(v
);
1021 if (isl_stream_next_token_is(s
, ']'))
1024 while ((tok
= next_token(s
)) != NULL
) {
1027 res
= tuple_add_dim(res
, v
);
1029 if (tok
->type
== ISL_TOKEN_IDENT
) {
1031 int p
= vars_pos(v
, tok
->u
.s
, -1);
1037 if (tok
->type
== '*') {
1038 if (vars_add_anon(v
) < 0)
1040 isl_token_free(tok
);
1041 } else if (new_name
) {
1042 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
1043 isl_token_free(tok
);
1044 if (isl_stream_eat_if_available(s
, '='))
1045 res
= read_tuple_var_def(s
, res
, i
, v
,
1048 isl_stream_push_token(s
, tok
);
1050 if (vars_add_anon(v
) < 0)
1052 res
= read_tuple_var_def(s
, res
, i
, v
, rational
);
1055 tok
= isl_stream_next_token(s
);
1056 if (!comma
&& tok
&& tok
->type
== ']' &&
1057 isl_stream_next_token_is(s
, '[')) {
1058 isl_token_free(tok
);
1059 tok
= isl_stream_next_token(s
);
1060 } else if (!tok
|| tok
->type
!= ',')
1063 isl_token_free(tok
);
1067 isl_stream_push_token(s
, tok
);
1071 isl_token_free(tok
);
1072 return isl_multi_pw_aff_free(res
);
1075 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1077 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
1078 struct vars
*v
, int rational
, int comma
)
1080 struct isl_token
*tok
;
1082 isl_multi_pw_aff
*res
= NULL
;
1084 tok
= isl_stream_next_token(s
);
1087 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1088 name
= strdup(tok
->u
.s
);
1089 isl_token_free(tok
);
1093 isl_stream_push_token(s
, tok
);
1094 if (isl_stream_eat(s
, '['))
1096 if (next_is_tuple(s
)) {
1097 isl_multi_pw_aff
*out
;
1099 res
= read_tuple(s
, v
, rational
, comma
);
1100 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1102 out
= read_tuple(s
, v
, rational
, comma
);
1103 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
1104 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
1105 res
= isl_multi_pw_aff_range_product(res
, out
);
1107 res
= read_tuple_var_list(s
, v
, rational
, comma
);
1108 if (isl_stream_eat(s
, ']'))
1112 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
1119 return isl_multi_pw_aff_free(res
);
1122 /* Read a tuple from "s" and add it to "map".
1123 * The tuple is initially represented as an isl_multi_pw_aff.
1124 * We first create the appropriate space in "map" based on the range
1125 * space of this isl_multi_pw_aff. Then, we add equalities based
1126 * on the affine expressions. These live in an anonymous space,
1127 * however, so we first need to reset the space to that of "map".
1129 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
1130 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1131 int rational
, int comma
)
1134 isl_multi_pw_aff
*tuple
;
1135 isl_space
*space
= NULL
;
1137 tuple
= read_tuple(s
, v
, rational
, comma
);
1141 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1142 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1146 if (type
== isl_dim_param
) {
1147 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1148 isl_space_is_wrapping(space
)) {
1149 isl_die(s
->ctx
, isl_error_invalid
,
1150 "parameter tuples cannot be named or nested",
1153 map
= isl_map_add_dims(map
, type
, n
);
1154 for (i
= 0; i
< n
; ++i
) {
1156 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1157 isl_die(s
->ctx
, isl_error_invalid
,
1158 "parameters must be named",
1160 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1161 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1163 } else if (type
== isl_dim_in
) {
1166 set
= isl_set_universe(isl_space_copy(space
));
1168 set
= isl_set_set_rational(set
);
1169 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1170 map
= isl_map_from_domain(set
);
1174 set
= isl_set_universe(isl_space_copy(space
));
1176 set
= isl_set_set_rational(set
);
1177 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1180 for (i
= 0; i
< n
; ++i
) {
1187 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1188 space
= isl_pw_aff_get_domain_space(pa
);
1189 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1190 aff
= isl_aff_add_coefficient_si(aff
,
1191 isl_dim_in
, v
->n
- n
+ i
, -1);
1192 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1194 pa
= isl_pw_aff_set_rational(pa
);
1195 set
= isl_pw_aff_zero_set(pa
);
1196 map_i
= isl_map_from_range(set
);
1197 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1198 map
= isl_map_intersect(map
, map_i
);
1201 isl_space_free(space
);
1202 isl_multi_pw_aff_free(tuple
);
1205 isl_space_free(space
);
1206 isl_multi_pw_aff_free(tuple
);
1211 static __isl_give isl_set
*construct_constraints(
1212 __isl_take isl_set
*set
, int type
,
1213 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1218 left
= isl_pw_aff_list_copy(left
);
1219 right
= isl_pw_aff_list_copy(right
);
1221 left
= isl_pw_aff_list_set_rational(left
);
1222 right
= isl_pw_aff_list_set_rational(right
);
1224 if (type
== ISL_TOKEN_LE
)
1225 cond
= isl_pw_aff_list_le_set(left
, right
);
1226 else if (type
== ISL_TOKEN_GE
)
1227 cond
= isl_pw_aff_list_ge_set(left
, right
);
1228 else if (type
== ISL_TOKEN_LT
)
1229 cond
= isl_pw_aff_list_lt_set(left
, right
);
1230 else if (type
== ISL_TOKEN_GT
)
1231 cond
= isl_pw_aff_list_gt_set(left
, right
);
1232 else if (type
== ISL_TOKEN_NE
)
1233 cond
= isl_pw_aff_list_ne_set(left
, right
);
1235 cond
= isl_pw_aff_list_eq_set(left
, right
);
1237 return isl_set_intersect(set
, cond
);
1240 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1241 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1243 struct isl_token
*tok
= NULL
;
1244 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1247 set
= isl_map_wrap(map
);
1248 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1251 tok
= isl_stream_next_token(s
);
1252 if (!is_comparator(tok
)) {
1253 isl_stream_error(s
, tok
, "missing operator");
1255 isl_stream_push_token(s
, tok
);
1260 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1264 set
= construct_constraints(set
, tok
->type
, list1
, list2
,
1266 isl_token_free(tok
);
1267 isl_pw_aff_list_free(list1
);
1270 tok
= isl_stream_next_token(s
);
1271 if (!is_comparator(tok
)) {
1273 isl_stream_push_token(s
, tok
);
1277 isl_pw_aff_list_free(list1
);
1279 return isl_set_unwrap(set
);
1282 isl_token_free(tok
);
1283 isl_pw_aff_list_free(list1
);
1284 isl_pw_aff_list_free(list2
);
1289 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1290 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1293 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1295 map
= isl_map_from_domain(isl_map_wrap(map
));
1296 map
= read_defined_var_list(s
, v
, map
, rational
);
1298 if (isl_stream_eat(s
, ':'))
1301 map
= read_disjuncts(s
, v
, map
, rational
);
1302 map
= isl_set_unwrap(isl_map_domain(map
));
1304 vars_drop(v
, v
->n
- n
);
1305 if (seen_paren
&& isl_stream_eat(s
, ')'))
1314 /* Parse an expression between parentheses and push the result
1315 * back on the stream.
1317 * The parsed expression may be either an affine expression
1318 * or a condition. The first type is pushed onto the stream
1319 * as an isl_pw_aff, while the second is pushed as an isl_map.
1321 * If the initial token indicates the start of a condition,
1322 * we parse it as such.
1323 * Otherwise, we first parse an affine expression and push
1324 * that onto the stream. If the affine expression covers the
1325 * entire expression between parentheses, we return.
1326 * Otherwise, we assume that the affine expression is the
1327 * start of a condition and continue parsing.
1329 static int resolve_paren_expr(struct isl_stream
*s
,
1330 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1332 struct isl_token
*tok
, *tok2
;
1336 tok
= isl_stream_next_token(s
);
1337 if (!tok
|| tok
->type
!= '(')
1340 if (isl_stream_next_token_is(s
, '('))
1341 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1344 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1345 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1346 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1347 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1348 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1349 map
= read_disjuncts(s
, v
, map
, rational
);
1350 if (isl_stream_eat(s
, ')'))
1352 tok
->type
= ISL_TOKEN_MAP
;
1354 isl_stream_push_token(s
, tok
);
1358 tok2
= isl_stream_next_token(s
);
1363 isl_stream_push_token(s
, tok2
);
1365 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1369 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1372 tok2
->type
= ISL_TOKEN_AFF
;
1373 tok2
->u
.pwaff
= pwaff
;
1375 if (isl_stream_eat_if_available(s
, ')')) {
1376 isl_stream_push_token(s
, tok2
);
1377 isl_token_free(tok
);
1382 isl_stream_push_token(s
, tok2
);
1384 map
= read_disjuncts(s
, v
, map
, rational
);
1385 if (isl_stream_eat(s
, ')'))
1388 tok
->type
= ISL_TOKEN_MAP
;
1390 isl_stream_push_token(s
, tok
);
1394 isl_pw_aff_free(pwaff
);
1396 isl_token_free(tok
);
1401 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1402 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1404 if (isl_stream_next_token_is(s
, '('))
1405 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1408 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1409 struct isl_token
*tok
;
1410 tok
= isl_stream_next_token(s
);
1414 map
= isl_map_copy(tok
->u
.map
);
1415 isl_token_free(tok
);
1419 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1420 return read_exists(s
, v
, map
, rational
);
1422 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1425 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1426 isl_space
*dim
= isl_map_get_space(map
);
1428 return isl_map_empty(dim
);
1431 return add_constraint(s
, v
, map
, rational
);
1437 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1438 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1443 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1444 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1446 res
= isl_map_subtract(isl_map_copy(map
), res
);
1448 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1451 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1452 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1454 res
= isl_map_subtract(res
, res_i
);
1456 res
= isl_map_intersect(res
, res_i
);
1463 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1464 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1468 if (isl_stream_next_token_is(s
, '}')) {
1469 isl_space
*dim
= isl_map_get_space(map
);
1471 return isl_map_universe(dim
);
1474 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1475 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1478 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1479 res
= isl_map_union(res
, res_i
);
1486 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1488 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1489 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1490 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1491 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1493 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1494 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1495 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1497 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1498 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1499 isl_basic_map_dim(bmap
, isl_dim_in
) +
1500 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1501 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1503 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1509 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1510 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1513 struct isl_token
*tok
;
1523 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1524 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1526 tok
= isl_stream_next_token(s
);
1527 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1528 isl_stream_error(s
, tok
, "expecting coefficient");
1530 isl_stream_push_token(s
, tok
);
1533 if (!tok
->on_new_line
) {
1534 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1535 isl_stream_push_token(s
, tok
);
1539 type
= isl_int_get_si(tok
->u
.v
);
1540 isl_token_free(tok
);
1542 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1544 k
= isl_basic_map_alloc_equality(bmap
);
1547 k
= isl_basic_map_alloc_inequality(bmap
);
1553 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1555 tok
= isl_stream_next_token(s
);
1556 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1557 isl_stream_error(s
, tok
, "expecting coefficient");
1559 isl_stream_push_token(s
, tok
);
1562 if (tok
->on_new_line
) {
1563 isl_stream_error(s
, tok
,
1564 "coefficient should not appear on new line");
1565 isl_stream_push_token(s
, tok
);
1568 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1569 isl_int_set(c
[pos
], tok
->u
.v
);
1570 isl_token_free(tok
);
1575 isl_basic_map_free(bmap
);
1579 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1582 struct isl_token
*tok
;
1583 struct isl_token
*tok2
;
1586 unsigned in
= 0, out
, local
= 0;
1587 struct isl_basic_map
*bmap
= NULL
;
1590 tok
= isl_stream_next_token(s
);
1592 isl_stream_error(s
, NULL
, "unexpected EOF");
1595 tok2
= isl_stream_next_token(s
);
1597 isl_token_free(tok
);
1598 isl_stream_error(s
, NULL
, "unexpected EOF");
1601 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1602 isl_stream_push_token(s
, tok2
);
1603 isl_stream_push_token(s
, tok
);
1604 isl_stream_error(s
, NULL
,
1605 "expecting constraint matrix dimensions");
1608 n_row
= isl_int_get_si(tok
->u
.v
);
1609 n_col
= isl_int_get_si(tok2
->u
.v
);
1610 on_new_line
= tok2
->on_new_line
;
1611 isl_token_free(tok2
);
1612 isl_token_free(tok
);
1613 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1614 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1615 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1616 tok
= isl_stream_next_token_on_same_line(s
);
1618 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1619 isl_stream_error(s
, tok
,
1620 "expecting number of output dimensions");
1621 isl_stream_push_token(s
, tok
);
1624 out
= isl_int_get_si(tok
->u
.v
);
1625 isl_token_free(tok
);
1627 tok
= isl_stream_next_token_on_same_line(s
);
1628 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1629 isl_stream_error(s
, tok
,
1630 "expecting number of input dimensions");
1632 isl_stream_push_token(s
, tok
);
1635 in
= isl_int_get_si(tok
->u
.v
);
1636 isl_token_free(tok
);
1638 tok
= isl_stream_next_token_on_same_line(s
);
1639 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1640 isl_stream_error(s
, tok
,
1641 "expecting number of existentials");
1643 isl_stream_push_token(s
, tok
);
1646 local
= isl_int_get_si(tok
->u
.v
);
1647 isl_token_free(tok
);
1649 tok
= isl_stream_next_token_on_same_line(s
);
1650 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1651 isl_stream_error(s
, tok
,
1652 "expecting number of parameters");
1654 isl_stream_push_token(s
, tok
);
1657 nparam
= isl_int_get_si(tok
->u
.v
);
1658 isl_token_free(tok
);
1659 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1660 isl_stream_error(s
, NULL
,
1661 "dimensions don't match");
1665 out
= n_col
- 2 - nparam
;
1666 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1670 for (i
= 0; i
< local
; ++i
) {
1671 int k
= isl_basic_map_alloc_div(bmap
);
1674 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1677 for (i
= 0; i
< n_row
; ++i
)
1678 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1680 tok
= isl_stream_next_token_on_same_line(s
);
1682 isl_stream_error(s
, tok
, "unexpected extra token on line");
1683 isl_stream_push_token(s
, tok
);
1687 bmap
= isl_basic_map_simplify(bmap
);
1688 bmap
= isl_basic_map_finalize(bmap
);
1691 isl_basic_map_free(bmap
);
1695 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1697 struct isl_token
*tok
;
1698 struct isl_token
*tok2
;
1700 struct isl_map
*map
;
1702 tok
= isl_stream_next_token(s
);
1704 isl_stream_error(s
, NULL
, "unexpected EOF");
1707 tok2
= isl_stream_next_token_on_same_line(s
);
1708 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1709 isl_stream_push_token(s
, tok2
);
1710 isl_stream_push_token(s
, tok
);
1711 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1714 isl_stream_error(s
, tok2
, "unexpected token");
1715 isl_stream_push_token(s
, tok2
);
1716 isl_stream_push_token(s
, tok
);
1719 n
= isl_int_get_si(tok
->u
.v
);
1720 isl_token_free(tok
);
1722 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1724 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1726 for (i
= 1; map
&& i
< n
; ++i
)
1727 map
= isl_map_union(map
,
1728 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1733 static int optional_power(struct isl_stream
*s
)
1736 struct isl_token
*tok
;
1738 tok
= isl_stream_next_token(s
);
1741 if (tok
->type
!= '^') {
1742 isl_stream_push_token(s
, tok
);
1745 isl_token_free(tok
);
1746 tok
= isl_stream_next_token(s
);
1747 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1748 isl_stream_error(s
, tok
, "expecting exponent");
1750 isl_stream_push_token(s
, tok
);
1753 pow
= isl_int_get_si(tok
->u
.v
);
1754 isl_token_free(tok
);
1758 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1759 __isl_keep isl_map
*map
, struct vars
*v
);
1761 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1762 __isl_keep isl_map
*map
, struct vars
*v
)
1764 isl_pw_qpolynomial
*pwqp
;
1765 struct isl_token
*tok
;
1767 tok
= next_token(s
);
1769 isl_stream_error(s
, NULL
, "unexpected EOF");
1772 if (tok
->type
== '(') {
1775 isl_token_free(tok
);
1776 pwqp
= read_term(s
, map
, v
);
1779 if (isl_stream_eat(s
, ')'))
1781 pow
= optional_power(s
);
1782 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1783 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1784 struct isl_token
*tok2
;
1785 isl_qpolynomial
*qp
;
1787 tok2
= isl_stream_next_token(s
);
1788 if (tok2
&& tok2
->type
== '/') {
1789 isl_token_free(tok2
);
1790 tok2
= next_token(s
);
1791 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1792 isl_stream_error(s
, tok2
, "expected denominator");
1793 isl_token_free(tok
);
1794 isl_token_free(tok2
);
1797 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1798 tok
->u
.v
, tok2
->u
.v
);
1799 isl_token_free(tok2
);
1801 isl_stream_push_token(s
, tok2
);
1802 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1805 isl_token_free(tok
);
1806 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1807 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1808 isl_qpolynomial
*qp
;
1809 isl_token_free(tok
);
1810 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1811 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1812 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1813 isl_qpolynomial
*qp
;
1814 isl_token_free(tok
);
1815 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1816 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1817 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1819 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1821 isl_qpolynomial
*qp
;
1823 isl_token_free(tok
);
1827 vars_drop(v
, v
->n
- n
);
1828 isl_stream_error(s
, tok
, "unknown identifier");
1829 isl_token_free(tok
);
1832 isl_token_free(tok
);
1833 pow
= optional_power(s
);
1834 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
1835 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1836 } else if (is_start_of_div(tok
)) {
1840 isl_stream_push_token(s
, tok
);
1841 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
1842 pow
= optional_power(s
);
1843 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
1844 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1845 } else if (tok
->type
== '-') {
1846 isl_token_free(tok
);
1847 pwqp
= read_factor(s
, map
, v
);
1848 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
1850 isl_stream_error(s
, tok
, "unexpected isl_token");
1851 isl_stream_push_token(s
, tok
);
1855 if (isl_stream_eat_if_available(s
, '*') ||
1856 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1857 isl_pw_qpolynomial
*pwqp2
;
1859 pwqp2
= read_factor(s
, map
, v
);
1860 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
1865 isl_pw_qpolynomial_free(pwqp
);
1869 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1870 __isl_keep isl_map
*map
, struct vars
*v
)
1872 struct isl_token
*tok
;
1873 isl_pw_qpolynomial
*pwqp
;
1875 pwqp
= read_factor(s
, map
, v
);
1878 tok
= next_token(s
);
1882 if (tok
->type
== '+') {
1883 isl_pw_qpolynomial
*pwqp2
;
1885 isl_token_free(tok
);
1886 pwqp2
= read_factor(s
, map
, v
);
1887 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1888 } else if (tok
->type
== '-') {
1889 isl_pw_qpolynomial
*pwqp2
;
1891 isl_token_free(tok
);
1892 pwqp2
= read_factor(s
, map
, v
);
1893 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
1894 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1895 isl_int_is_neg(tok
->u
.v
)) {
1896 isl_pw_qpolynomial
*pwqp2
;
1898 isl_stream_push_token(s
, tok
);
1899 pwqp2
= read_factor(s
, map
, v
);
1900 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1902 isl_stream_push_token(s
, tok
);
1910 static __isl_give isl_map
*read_optional_disjuncts(struct isl_stream
*s
,
1911 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
1913 struct isl_token
*tok
;
1915 tok
= isl_stream_next_token(s
);
1917 isl_stream_error(s
, NULL
, "unexpected EOF");
1920 if (tok
->type
== ':' ||
1921 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
1922 isl_token_free(tok
);
1923 map
= read_disjuncts(s
, v
, map
, rational
);
1925 isl_stream_push_token(s
, tok
);
1933 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1934 __isl_take isl_map
*map
, struct vars
*v
, int n
)
1936 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1937 isl_pw_qpolynomial
*pwqp
;
1938 struct isl_set
*set
;
1940 pwqp
= read_term(s
, map
, v
);
1941 map
= read_optional_disjuncts(s
, map
, v
, 0);
1942 set
= isl_map_range(map
);
1944 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
1946 vars_drop(v
, v
->n
- n
);
1952 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
1953 __isl_take isl_set
*set
, struct vars
*v
, int n
)
1955 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
1956 isl_pw_qpolynomial
*pwqp
;
1957 isl_pw_qpolynomial_fold
*pwf
= NULL
;
1959 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
1960 return obj_read_poly(s
, set
, v
, n
);
1962 if (isl_stream_eat(s
, '('))
1965 pwqp
= read_term(s
, set
, v
);
1966 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
1968 while (isl_stream_eat_if_available(s
, ',')) {
1969 isl_pw_qpolynomial_fold
*pwf_i
;
1970 pwqp
= read_term(s
, set
, v
);
1971 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
1973 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
1976 if (isl_stream_eat(s
, ')'))
1979 set
= read_optional_disjuncts(s
, set
, v
, 0);
1980 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
1982 vars_drop(v
, v
->n
- n
);
1988 isl_pw_qpolynomial_fold_free(pwf
);
1989 obj
.type
= isl_obj_none
;
1993 static int is_rational(struct isl_stream
*s
)
1995 struct isl_token
*tok
;
1997 tok
= isl_stream_next_token(s
);
2000 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2001 isl_token_free(tok
);
2002 isl_stream_eat(s
, ':');
2006 isl_stream_push_token(s
, tok
);
2011 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
2012 __isl_take isl_map
*map
, struct vars
*v
)
2014 struct isl_token
*tok
;
2015 struct isl_obj obj
= { isl_obj_set
, NULL
};
2019 rational
= is_rational(s
);
2021 map
= isl_map_set_rational(map
);
2023 if (isl_stream_next_token_is(s
, ':')) {
2024 obj
.type
= isl_obj_set
;
2025 obj
.v
= read_optional_disjuncts(s
, map
, v
, rational
);
2029 if (!next_is_tuple(s
))
2030 return obj_read_poly_or_fold(s
, map
, v
, n
);
2032 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2035 tok
= isl_stream_next_token(s
);
2038 if (tok
->type
== ISL_TOKEN_TO
) {
2039 obj
.type
= isl_obj_map
;
2040 isl_token_free(tok
);
2041 if (!next_is_tuple(s
)) {
2042 isl_set
*set
= isl_map_domain(map
);
2043 return obj_read_poly_or_fold(s
, set
, v
, n
);
2045 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2049 map
= isl_map_domain(map
);
2050 isl_stream_push_token(s
, tok
);
2053 map
= read_optional_disjuncts(s
, map
, v
, rational
);
2055 vars_drop(v
, v
->n
- n
);
2061 obj
.type
= isl_obj_none
;
2065 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2067 if (obj
.type
== isl_obj_map
) {
2068 obj
.v
= isl_union_map_from_map(obj
.v
);
2069 obj
.type
= isl_obj_union_map
;
2070 } else if (obj
.type
== isl_obj_set
) {
2071 obj
.v
= isl_union_set_from_set(obj
.v
);
2072 obj
.type
= isl_obj_union_set
;
2073 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2074 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2075 obj
.type
= isl_obj_union_pw_qpolynomial
;
2076 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2077 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2078 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2080 isl_assert(ctx
, 0, goto error
);
2083 obj
.type
->free(obj
.v
);
2084 obj
.type
= isl_obj_none
;
2088 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
2089 struct isl_obj obj1
, struct isl_obj obj2
)
2091 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2092 obj1
= to_union(ctx
, obj1
);
2093 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2094 obj2
= to_union(ctx
, obj2
);
2095 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2096 obj1
= to_union(ctx
, obj1
);
2097 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2098 obj2
= to_union(ctx
, obj2
);
2099 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2100 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2101 obj1
= to_union(ctx
, obj1
);
2102 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2103 obj2
.type
== isl_obj_pw_qpolynomial
)
2104 obj2
= to_union(ctx
, obj2
);
2105 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2106 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2107 obj1
= to_union(ctx
, obj1
);
2108 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2109 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2110 obj2
= to_union(ctx
, obj2
);
2111 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
2112 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2113 obj1
= to_union(ctx
, obj1
);
2114 obj2
= to_union(ctx
, obj2
);
2116 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2117 obj1
= to_union(ctx
, obj1
);
2118 obj2
= to_union(ctx
, obj2
);
2120 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2121 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2122 obj1
= to_union(ctx
, obj1
);
2123 obj2
= to_union(ctx
, obj2
);
2125 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2126 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2127 obj1
= to_union(ctx
, obj1
);
2128 obj2
= to_union(ctx
, obj2
);
2130 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2133 obj1
.type
->free(obj1
.v
);
2134 obj2
.type
->free(obj2
.v
);
2135 obj1
.type
= isl_obj_none
;
2140 static struct isl_obj
obj_read(struct isl_stream
*s
)
2142 isl_map
*map
= NULL
;
2143 struct isl_token
*tok
;
2144 struct vars
*v
= NULL
;
2145 struct isl_obj obj
= { isl_obj_set
, NULL
};
2147 tok
= next_token(s
);
2149 isl_stream_error(s
, NULL
, "unexpected EOF");
2152 if (tok
->type
== ISL_TOKEN_VALUE
) {
2153 struct isl_token
*tok2
;
2154 struct isl_map
*map
;
2156 tok2
= isl_stream_next_token(s
);
2157 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2158 isl_int_is_neg(tok2
->u
.v
)) {
2160 isl_stream_push_token(s
, tok2
);
2161 obj
.type
= isl_obj_int
;
2162 obj
.v
= isl_int_obj_alloc(s
->ctx
, tok
->u
.v
);
2163 isl_token_free(tok
);
2166 isl_stream_push_token(s
, tok2
);
2167 isl_stream_push_token(s
, tok
);
2168 map
= map_read_polylib(s
);
2171 if (isl_map_may_be_set(map
))
2172 obj
.v
= isl_map_range(map
);
2174 obj
.type
= isl_obj_map
;
2179 v
= vars_new(s
->ctx
);
2181 isl_stream_push_token(s
, tok
);
2184 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2185 if (tok
->type
== '[') {
2186 isl_stream_push_token(s
, tok
);
2187 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2190 tok
= isl_stream_next_token(s
);
2191 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2192 isl_stream_error(s
, tok
, "expecting '->'");
2194 isl_stream_push_token(s
, tok
);
2197 isl_token_free(tok
);
2198 tok
= isl_stream_next_token(s
);
2200 if (!tok
|| tok
->type
!= '{') {
2201 isl_stream_error(s
, tok
, "expecting '{'");
2203 isl_stream_push_token(s
, tok
);
2206 isl_token_free(tok
);
2208 tok
= isl_stream_next_token(s
);
2211 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2212 isl_token_free(tok
);
2213 if (isl_stream_eat(s
, '='))
2215 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2218 } else if (tok
->type
== '}') {
2219 obj
.type
= isl_obj_union_set
;
2220 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2221 isl_token_free(tok
);
2224 isl_stream_push_token(s
, tok
);
2229 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2230 if (o
.type
== isl_obj_none
|| !o
.v
)
2235 obj
= obj_add(s
->ctx
, obj
, o
);
2236 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2239 tok
= isl_stream_next_token(s
);
2240 if (!tok
|| tok
->type
!= ';')
2242 isl_token_free(tok
);
2243 if (isl_stream_next_token_is(s
, '}')) {
2244 tok
= isl_stream_next_token(s
);
2249 if (tok
&& tok
->type
== '}') {
2250 isl_token_free(tok
);
2252 isl_stream_error(s
, tok
, "unexpected isl_token");
2254 isl_token_free(tok
);
2264 obj
.type
->free(obj
.v
);
2271 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2276 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2282 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2283 obj
.type
== isl_obj_set
, goto error
);
2285 if (obj
.type
== isl_obj_set
)
2286 obj
.v
= isl_map_from_range(obj
.v
);
2290 obj
.type
->free(obj
.v
);
2294 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2300 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2301 obj
.v
= isl_map_range(obj
.v
);
2302 obj
.type
= isl_obj_set
;
2304 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2309 obj
.type
->free(obj
.v
);
2313 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2318 if (obj
.type
== isl_obj_map
) {
2319 obj
.type
= isl_obj_union_map
;
2320 obj
.v
= isl_union_map_from_map(obj
.v
);
2322 if (obj
.type
== isl_obj_set
) {
2323 obj
.type
= isl_obj_union_set
;
2324 obj
.v
= isl_union_set_from_set(obj
.v
);
2326 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2327 isl_union_set_is_empty(obj
.v
))
2328 obj
.type
= isl_obj_union_map
;
2329 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2330 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2334 obj
.type
->free(obj
.v
);
2338 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2343 if (obj
.type
== isl_obj_set
) {
2344 obj
.type
= isl_obj_union_set
;
2345 obj
.v
= isl_union_set_from_set(obj
.v
);
2348 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2352 obj
.type
->free(obj
.v
);
2356 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2359 struct isl_map
*map
;
2360 struct isl_basic_map
*bmap
;
2367 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2370 bmap
= isl_basic_map_empty_like_map(map
);
2372 bmap
= isl_basic_map_copy(map
->p
[0]);
2382 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2384 isl_basic_map
*bmap
;
2385 bmap
= basic_map_read(s
);
2388 if (!isl_basic_map_may_be_set(bmap
))
2389 isl_die(s
->ctx
, isl_error_invalid
,
2390 "input is not a set", goto error
);
2391 return isl_basic_map_range(bmap
);
2393 isl_basic_map_free(bmap
);
2397 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2400 struct isl_basic_map
*bmap
;
2401 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2404 bmap
= basic_map_read(s
);
2409 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2412 isl_basic_set
*bset
;
2413 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2416 bset
= basic_set_read(s
);
2421 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2424 struct isl_basic_map
*bmap
;
2425 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2428 bmap
= basic_map_read(s
);
2433 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2436 isl_basic_set
*bset
;
2437 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2440 bset
= basic_set_read(s
);
2445 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2448 struct isl_map
*map
;
2449 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2452 map
= isl_stream_read_map(s
);
2457 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2460 struct isl_map
*map
;
2461 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2464 map
= isl_stream_read_map(s
);
2469 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2473 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2476 set
= isl_stream_read_set(s
);
2481 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2485 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2488 set
= isl_stream_read_set(s
);
2493 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2496 isl_union_map
*umap
;
2497 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2500 umap
= isl_stream_read_union_map(s
);
2505 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2508 isl_union_map
*umap
;
2509 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2512 umap
= isl_stream_read_union_map(s
);
2517 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2520 isl_union_set
*uset
;
2521 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2524 uset
= isl_stream_read_union_set(s
);
2529 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2532 isl_union_set
*uset
;
2533 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2536 uset
= isl_stream_read_union_set(s
);
2541 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2543 struct isl_vec
*vec
= NULL
;
2544 struct isl_token
*tok
;
2548 tok
= isl_stream_next_token(s
);
2549 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2550 isl_stream_error(s
, tok
, "expecting vector length");
2554 size
= isl_int_get_si(tok
->u
.v
);
2555 isl_token_free(tok
);
2557 vec
= isl_vec_alloc(s
->ctx
, size
);
2559 for (j
= 0; j
< size
; ++j
) {
2560 tok
= isl_stream_next_token(s
);
2561 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2562 isl_stream_error(s
, tok
, "expecting constant value");
2565 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2566 isl_token_free(tok
);
2571 isl_token_free(tok
);
2576 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2578 return isl_vec_read_polylib(s
);
2581 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2584 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2592 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2593 struct isl_stream
*s
)
2599 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2604 obj
.type
->free(obj
.v
);
2608 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2611 isl_pw_qpolynomial
*pwqp
;
2612 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2615 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2620 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2623 isl_pw_qpolynomial
*pwqp
;
2624 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2627 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2632 /* Is the next token an identifer not in "v"?
2634 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2638 struct isl_token
*tok
;
2640 tok
= isl_stream_next_token(s
);
2643 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2644 isl_stream_push_token(s
, tok
);
2646 vars_drop(v
, v
->n
- n
);
2651 /* First read the domain of the affine expression, which may be
2652 * a parameter space or a set.
2653 * The tricky part is that we don't know if the domain is a set or not,
2654 * so when we are trying to read the domain, we may actually be reading
2655 * the affine expression itself (defined on a parameter domains)
2656 * If the tuple we are reading is named, we assume it's the domain.
2657 * Also, if inside the tuple, the first thing we find is a nested tuple
2658 * or a new identifier, we again assume it's the domain.
2659 * Otherwise, we assume we are reading an affine expression.
2661 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2662 __isl_take isl_set
*dom
, struct vars
*v
)
2664 struct isl_token
*tok
;
2666 tok
= isl_stream_next_token(s
);
2667 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2668 isl_stream_push_token(s
, tok
);
2669 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2671 if (!tok
|| tok
->type
!= '[') {
2672 isl_stream_error(s
, tok
, "expecting '['");
2675 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2676 isl_stream_push_token(s
, tok
);
2677 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 1, 0);
2679 isl_stream_push_token(s
, tok
);
2684 isl_stream_push_token(s
, tok
);
2689 /* Read an affine expression from "s".
2691 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2696 ma
= isl_stream_read_multi_aff(s
);
2699 if (isl_multi_aff_dim(ma
, isl_dim_out
) != 1)
2700 isl_die(s
->ctx
, isl_error_invalid
,
2701 "expecting single affine expression",
2704 aff
= isl_multi_aff_get_aff(ma
, 0);
2705 isl_multi_aff_free(ma
);
2708 isl_multi_aff_free(ma
);
2712 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2714 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2715 __isl_take isl_set
*dom
, struct vars
*v
)
2717 isl_pw_aff
*pwaff
= NULL
;
2719 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2722 if (isl_stream_eat(s
, '['))
2725 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2727 if (isl_stream_eat(s
, ']'))
2730 dom
= read_optional_disjuncts(s
, dom
, v
, 0);
2731 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2736 isl_pw_aff_free(pwaff
);
2740 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2743 isl_set
*dom
= NULL
;
2745 isl_pw_aff
*pa
= NULL
;
2748 v
= vars_new(s
->ctx
);
2752 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2753 if (next_is_tuple(s
)) {
2754 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2755 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2758 if (isl_stream_eat(s
, '{'))
2762 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2763 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2764 vars_drop(v
, v
->n
- n
);
2766 while (isl_stream_eat_if_available(s
, ';')) {
2770 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2771 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2772 vars_drop(v
, v
->n
- n
);
2774 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2777 if (isl_stream_eat(s
, '}'))
2786 isl_pw_aff_free(pa
);
2790 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2793 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2796 aff
= isl_stream_read_aff(s
);
2801 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2804 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2807 pa
= isl_stream_read_pw_aff(s
);
2812 /* Read an isl_pw_multi_aff from "s".
2813 * We currently read a generic object and if it turns out to be a set or
2814 * a map, we convert that to an isl_pw_multi_aff.
2815 * It would be more efficient if we were to construct the isl_pw_multi_aff
2818 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
2826 if (obj
.type
== isl_obj_map
)
2827 return isl_pw_multi_aff_from_map(obj
.v
);
2828 if (obj
.type
== isl_obj_set
)
2829 return isl_pw_multi_aff_from_set(obj
.v
);
2831 obj
.type
->free(obj
.v
);
2832 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2836 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
2839 isl_pw_multi_aff
*pma
;
2840 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2843 pma
= isl_stream_read_pw_multi_aff(s
);
2848 /* Read an isl_union_pw_multi_aff from "s".
2849 * We currently read a generic object and if it turns out to be a set or
2850 * a map, we convert that to an isl_union_pw_multi_aff.
2851 * It would be more efficient if we were to construct
2852 * the isl_union_pw_multi_aff directly.
2854 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
2855 struct isl_stream
*s
)
2863 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
2864 obj
= to_union(s
->ctx
, obj
);
2865 if (obj
.type
== isl_obj_union_map
)
2866 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
2867 if (obj
.type
== isl_obj_union_set
)
2868 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
2870 obj
.type
->free(obj
.v
);
2871 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2875 /* Read an isl_union_pw_multi_aff from "str".
2877 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
2878 isl_ctx
*ctx
, const char *str
)
2880 isl_union_pw_multi_aff
*upma
;
2881 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2884 upma
= isl_stream_read_union_pw_multi_aff(s
);
2889 /* Assuming "pa" represents a single affine expression defined on a universe
2890 * domain, extract this affine expression.
2892 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
2899 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2900 "expecting single affine expression",
2902 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
2903 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2904 "expecting universe domain",
2907 aff
= isl_aff_copy(pa
->p
[0].aff
);
2908 isl_pw_aff_free(pa
);
2911 isl_pw_aff_free(pa
);
2915 /* Read a multi-affine expression from "s".
2916 * If the multi-affine expression has a domain, then then tuple
2917 * representing this domain cannot involve any affine expressions.
2918 * The tuple representing the actual expressions needs to consist
2919 * of only affine expressions. Moreover, these expressions can
2920 * only depend on parameters and input dimensions and not on other
2921 * output dimensions.
2923 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
2926 isl_set
*dom
= NULL
;
2927 isl_multi_pw_aff
*tuple
= NULL
;
2929 isl_space
*space
, *dom_space
;
2930 isl_multi_aff
*ma
= NULL
;
2932 v
= vars_new(s
->ctx
);
2936 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2937 if (next_is_tuple(s
)) {
2938 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
2939 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2942 if (!isl_set_plain_is_universe(dom
))
2943 isl_die(s
->ctx
, isl_error_invalid
,
2944 "expecting universe parameter domain", goto error
);
2945 if (isl_stream_eat(s
, '{'))
2948 tuple
= read_tuple(s
, v
, 0, 0);
2951 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
2956 has_expr
= tuple_has_expr(tuple
);
2960 isl_die(s
->ctx
, isl_error_invalid
,
2961 "expecting universe domain", goto error
);
2962 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
2963 set
= isl_set_universe(space
);
2964 dom
= isl_set_intersect_params(set
, dom
);
2965 isl_multi_pw_aff_free(tuple
);
2966 tuple
= read_tuple(s
, v
, 0, 0);
2971 if (isl_stream_eat(s
, '}'))
2974 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
2975 dim
= isl_set_dim(dom
, isl_dim_all
);
2976 dom_space
= isl_set_get_space(dom
);
2977 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
2978 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
2979 if (!isl_space_is_params(dom_space
))
2980 space
= isl_space_map_from_domain_and_range(
2981 isl_space_copy(dom_space
), space
);
2982 isl_space_free(dom_space
);
2983 ma
= isl_multi_aff_alloc(space
);
2985 for (i
= 0; i
< n
; ++i
) {
2988 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
2989 aff
= aff_from_pw_aff(pa
);
2992 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
2994 isl_die(s
->ctx
, isl_error_invalid
,
2995 "not an affine expression", goto error
);
2997 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
2998 space
= isl_multi_aff_get_domain_space(ma
);
2999 aff
= isl_aff_reset_domain_space(aff
, space
);
3000 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3003 isl_multi_pw_aff_free(tuple
);
3008 isl_multi_pw_aff_free(tuple
);
3011 isl_multi_aff_free(ma
);
3015 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3018 isl_multi_aff
*maff
;
3019 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3022 maff
= isl_stream_read_multi_aff(s
);
3027 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
3028 struct isl_stream
*s
)
3033 if (obj
.type
== isl_obj_pw_qpolynomial
) {
3034 obj
.type
= isl_obj_union_pw_qpolynomial
;
3035 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
3038 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
3043 obj
.type
->free(obj
.v
);
3047 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
3048 isl_ctx
*ctx
, const char *str
)
3050 isl_union_pw_qpolynomial
*upwqp
;
3051 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3054 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);