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>
33 struct variable
*next
;
42 static struct vars
*vars_new(struct isl_ctx
*ctx
)
45 v
= isl_alloc_type(ctx
, struct vars
);
54 static void variable_free(struct variable
*var
)
57 struct variable
*next
= var
->next
;
64 static void vars_free(struct vars
*v
)
72 static void vars_drop(struct vars
*v
, int n
)
83 struct variable
*next
= var
->next
;
91 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
95 var
= isl_calloc_type(v
->ctx
, struct variable
);
98 var
->name
= strdup(name
);
99 var
->name
[len
] = '\0';
108 static int vars_pos(struct vars
*v
, const char *s
, int len
)
115 for (q
= v
->v
; q
; q
= q
->next
) {
116 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
123 v
->v
= variable_new(v
, s
, len
, v
->n
);
131 static int vars_add_anon(struct vars
*v
)
133 v
->v
= variable_new(v
, "", 0, v
->n
);
142 /* Obtain next token, with some preprocessing.
143 * In particular, evaluate expressions of the form x^y,
144 * with x and y values.
146 static struct isl_token
*next_token(struct isl_stream
*s
)
148 struct isl_token
*tok
, *tok2
;
150 tok
= isl_stream_next_token(s
);
151 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
153 if (!isl_stream_eat_if_available(s
, '^'))
155 tok2
= isl_stream_next_token(s
);
156 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
157 isl_stream_error(s
, tok2
, "expecting constant value");
161 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
163 isl_token_free(tok2
);
167 isl_token_free(tok2
);
171 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
173 struct isl_token
*tok
;
176 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
177 isl_stream_error(s
, tok
, "expecting constant value");
181 isl_int_mul(*f
, *f
, tok
->u
.v
);
185 if (isl_stream_eat_if_available(s
, '*'))
186 return accept_cst_factor(s
, f
);
194 /* Given an affine expression aff, return an affine expression
195 * for aff % d, with d the next token on the stream, which is
196 * assumed to be a constant.
198 * We introduce an integer division q = [aff/d] and the result
199 * is set to aff - d q.
201 static __isl_give isl_pw_aff
*affine_mod(struct isl_stream
*s
,
202 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
204 struct isl_token
*tok
;
208 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
209 isl_stream_error(s
, tok
, "expecting constant value");
213 q
= isl_pw_aff_copy(aff
);
214 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
215 q
= isl_pw_aff_floor(q
);
216 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
218 aff
= isl_pw_aff_sub(aff
, q
);
223 isl_pw_aff_free(aff
);
228 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
229 __isl_take isl_space
*dim
, struct vars
*v
);
230 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
231 __isl_take isl_space
*dim
, struct vars
*v
);
233 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
234 __isl_take isl_space
*dim
, struct vars
*v
)
236 struct isl_token
*tok
;
237 isl_pw_aff_list
*list
= NULL
;
240 tok
= isl_stream_next_token(s
);
243 min
= tok
->type
== ISL_TOKEN_MIN
;
246 if (isl_stream_eat(s
, '('))
249 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
253 if (isl_stream_eat(s
, ')'))
257 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
260 isl_pw_aff_list_free(list
);
264 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
265 __isl_take isl_space
*dim
, struct vars
*v
)
267 struct isl_token
*tok
;
270 isl_pw_aff
*pwaff
= NULL
;
272 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
274 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
277 if (isl_stream_eat(s
, '('))
280 if (isl_stream_eat(s
, '['))
284 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
287 if (isl_stream_eat(s
, ','))
293 if (tok
->type
!= ISL_TOKEN_VALUE
) {
294 isl_stream_error(s
, tok
, "expected denominator");
295 isl_stream_push_token(s
, tok
);
298 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
303 pwaff
= isl_pw_aff_ceil(pwaff
);
305 pwaff
= isl_pw_aff_floor(pwaff
);
308 if (isl_stream_eat(s
, ')'))
311 if (isl_stream_eat(s
, ']'))
319 isl_pw_aff_free(pwaff
);
323 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
324 __isl_take isl_space
*dim
, struct vars
*v
)
326 struct isl_token
*tok
= NULL
;
327 isl_pw_aff
*res
= NULL
;
331 isl_stream_error(s
, NULL
, "unexpected EOF");
335 if (tok
->type
== ISL_TOKEN_AFF
) {
336 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
338 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
340 int pos
= vars_pos(v
, tok
->u
.s
, -1);
346 isl_stream_error(s
, tok
, "unknown identifier");
350 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
353 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
354 res
= isl_pw_aff_from_aff(aff
);
356 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
357 if (isl_stream_eat_if_available(s
, '*')) {
358 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
359 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
363 ls
= isl_local_space_from_space(isl_space_copy(dim
));
364 aff
= isl_aff_zero_on_domain(ls
);
365 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
366 res
= isl_pw_aff_from_aff(aff
);
369 } else if (tok
->type
== '(') {
372 res
= accept_affine(s
, isl_space_copy(dim
), v
);
375 if (isl_stream_eat(s
, ')'))
377 } else if (tok
->type
== '[' ||
378 tok
->type
== ISL_TOKEN_FLOORD
||
379 tok
->type
== ISL_TOKEN_CEILD
) {
380 isl_stream_push_token(s
, tok
);
382 res
= accept_div(s
, isl_space_copy(dim
), v
);
383 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
384 isl_stream_push_token(s
, tok
);
386 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
388 isl_stream_error(s
, tok
, "expecting factor");
391 if (isl_stream_eat_if_available(s
, '%') ||
392 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
394 return affine_mod(s
, v
, res
);
396 if (isl_stream_eat_if_available(s
, '*')) {
399 isl_int_set_si(f
, 1);
400 if (accept_cst_factor(s
, &f
) < 0) {
404 res
= isl_pw_aff_scale(res
, f
);
407 if (isl_stream_eat_if_available(s
, '/')) {
410 isl_int_set_si(f
, 1);
411 if (accept_cst_factor(s
, &f
) < 0) {
415 res
= isl_pw_aff_scale_down(res
, f
);
424 isl_pw_aff_free(res
);
429 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
434 space
= isl_pw_aff_get_domain_space(pwaff
);
435 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
436 aff
= isl_aff_add_constant(aff
, v
);
438 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
441 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
442 __isl_take isl_space
*dim
, struct vars
*v
)
444 struct isl_token
*tok
= NULL
;
449 ls
= isl_local_space_from_space(isl_space_copy(dim
));
450 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
457 isl_stream_error(s
, NULL
, "unexpected EOF");
460 if (tok
->type
== '-') {
465 if (tok
->type
== '(' || tok
->type
== '[' ||
466 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
467 tok
->type
== ISL_TOKEN_FLOORD
||
468 tok
->type
== ISL_TOKEN_CEILD
||
469 tok
->type
== ISL_TOKEN_IDENT
||
470 tok
->type
== ISL_TOKEN_AFF
) {
472 isl_stream_push_token(s
, tok
);
474 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
476 res
= isl_pw_aff_sub(res
, term
);
478 res
= isl_pw_aff_add(res
, term
);
482 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
484 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
485 if (isl_stream_eat_if_available(s
, '*') ||
486 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
488 term
= accept_affine_factor(s
,
489 isl_space_copy(dim
), v
);
490 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
491 res
= isl_pw_aff_add(res
, term
);
495 res
= add_cst(res
, tok
->u
.v
);
499 isl_stream_error(s
, tok
, "unexpected isl_token");
500 isl_stream_push_token(s
, tok
);
501 isl_pw_aff_free(res
);
508 if (tok
&& tok
->type
== '-') {
511 } else if (tok
&& tok
->type
== '+') {
514 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
515 isl_int_is_neg(tok
->u
.v
)) {
516 isl_stream_push_token(s
, tok
);
519 isl_stream_push_token(s
, tok
);
529 isl_pw_aff_free(res
);
533 static int is_comparator(struct isl_token
*tok
)
551 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
552 struct vars
*v
, __isl_take isl_map
*map
);
553 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
554 __isl_take isl_space
*dim
, struct vars
*v
);
556 /* Accept a ternary operator, given the first argument.
558 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
559 __isl_take isl_map
*cond
, struct vars
*v
)
562 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
567 if (isl_stream_eat(s
, '?'))
570 dim
= isl_space_wrap(isl_map_get_space(cond
));
571 pwaff1
= accept_extended_affine(s
, dim
, v
);
575 if (isl_stream_eat(s
, ':'))
578 dim
= isl_pw_aff_get_domain_space(pwaff1
);
579 pwaff2
= accept_extended_affine(s
, dim
, v
);
583 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
584 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
587 isl_pw_aff_free(pwaff1
);
588 isl_pw_aff_free(pwaff2
);
592 /* Accept an affine expression that may involve ternary operators.
593 * We first read an affine expression.
594 * If it is not followed by a comparison operator, we simply return it.
595 * Otherwise, we assume the affine epxression is part of the first
596 * argument of a ternary operator and try to parse that.
598 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
599 __isl_take isl_space
*dim
, struct vars
*v
)
604 struct isl_token
*tok
;
605 int line
= -1, col
= -1;
608 tok
= isl_stream_next_token(s
);
612 isl_stream_push_token(s
, tok
);
615 pwaff
= accept_affine(s
, dim
, v
);
619 tok
= isl_stream_next_token(s
);
621 return isl_pw_aff_free(pwaff
);
623 is_comp
= is_comparator(tok
);
624 isl_stream_push_token(s
, tok
);
628 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
630 return isl_pw_aff_free(pwaff
);
631 tok
->type
= ISL_TOKEN_AFF
;
632 tok
->u
.pwaff
= pwaff
;
634 space
= isl_pw_aff_get_domain_space(pwaff
);
635 cond
= isl_map_universe(isl_space_unwrap(space
));
637 isl_stream_push_token(s
, tok
);
639 cond
= read_disjuncts(s
, v
, cond
);
641 return accept_ternary(s
, cond
, v
);
644 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
645 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
)
651 if (type
== isl_dim_param
)
652 pos
= isl_map_dim(map
, isl_dim_param
);
654 pos
= isl_map_dim(map
, isl_dim_in
);
655 if (type
== isl_dim_out
)
656 pos
+= isl_map_dim(map
, isl_dim_out
);
661 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
662 def_map
= isl_map_from_pw_aff(def
);
663 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
664 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
666 map
= isl_map_intersect(map
, def_map
);
671 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
672 __isl_take isl_space
*dim
, struct vars
*v
)
675 isl_pw_aff_list
*list
;
676 struct isl_token
*tok
= NULL
;
678 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
679 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
684 tok
= isl_stream_next_token(s
);
686 isl_stream_error(s
, NULL
, "unexpected EOF");
689 if (tok
->type
!= ',') {
690 isl_stream_push_token(s
, tok
);
695 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
696 list
= isl_pw_aff_list_concat(list
,
697 isl_pw_aff_list_from_pw_aff(pwaff
));
706 isl_pw_aff_list_free(list
);
710 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
711 struct vars
*v
, __isl_take isl_map
*map
)
713 struct isl_token
*tok
;
715 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
719 if (tok
->type
!= ISL_TOKEN_IDENT
)
722 p
= vars_pos(v
, tok
->u
.s
, -1);
726 isl_stream_error(s
, tok
, "expecting unique identifier");
730 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
733 tok
= isl_stream_next_token(s
);
734 if (tok
&& tok
->type
== '=') {
736 map
= read_var_def(s
, map
, isl_dim_out
, v
);
737 tok
= isl_stream_next_token(s
);
740 if (!tok
|| tok
->type
!= ',')
746 isl_stream_push_token(s
, tok
);
755 static int next_is_tuple(struct isl_stream
*s
)
757 struct isl_token
*tok
;
760 tok
= isl_stream_next_token(s
);
763 if (tok
->type
== '[') {
764 isl_stream_push_token(s
, tok
);
767 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
768 isl_stream_push_token(s
, tok
);
772 is_tuple
= isl_stream_next_token_is(s
, '[');
774 isl_stream_push_token(s
, tok
);
779 /* Allocate an initial tuple with zero dimensions and an anonymous,
780 * unstructured space.
781 * A tuple is represented as an isl_multi_pw_aff.
782 * The range space is the space of the tuple.
783 * The domain space is an anonymous space
784 * with a dimension for each variable in the set of variables in "v".
785 * If a given dimension is not defined in terms of earlier dimensions in
786 * the input, then the corresponding isl_pw_aff is set equal to one time
787 * the variable corresponding to the dimension being defined.
789 static __isl_give isl_multi_pw_aff
*tuple_alloc(struct vars
*v
)
791 return isl_multi_pw_aff_alloc(isl_space_alloc(v
->ctx
, 0, v
->n
, 0));
794 /* Add a dimension to the given tuple.
795 * The dimension is initially undefined, so it is encoded
796 * as one times itself.
798 static __isl_give isl_multi_pw_aff
*tuple_add_dim(
799 __isl_take isl_multi_pw_aff
*tuple
, struct vars
*v
)
805 tuple
= isl_multi_pw_aff_add_dims(tuple
, isl_dim_in
, 1);
806 space
= isl_multi_pw_aff_get_domain_space(tuple
);
807 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
808 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
, 1);
809 pa
= isl_pw_aff_from_aff(aff
);
810 tuple
= isl_multi_pw_aff_flat_range_product(tuple
,
811 isl_multi_pw_aff_from_pw_aff(pa
));
816 /* Set the name of dimension "pos" in "tuple" to "name".
817 * During printing, we add primes if the same name appears more than once
818 * to distinguish the occurrences. Here, we remove those primes from "name"
819 * before setting the name of the dimension.
821 static __isl_give isl_multi_pw_aff
*tuple_set_dim_name(
822 __isl_take isl_multi_pw_aff
*tuple
, int pos
, char *name
)
829 prime
= strchr(name
, '\'');
832 tuple
= isl_multi_pw_aff_set_dim_name(tuple
, isl_dim_set
, pos
, name
);
839 /* Read an affine expression from "s" and replace the definition
840 * of dimension "pos" in "tuple" by this expression.
842 * accept_extended_affine requires a wrapped space as input.
843 * The domain space of "tuple", on the other hand is an anonymous space,
844 * so we have to adjust the space of the isl_pw_aff before adding it
847 static __isl_give isl_multi_pw_aff
*read_tuple_var_def(struct isl_stream
*s
,
848 __isl_take isl_multi_pw_aff
*tuple
, int pos
, struct vars
*v
)
853 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
854 def
= accept_extended_affine(s
, space
, v
);
855 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
856 def
= isl_pw_aff_reset_domain_space(def
, space
);
857 tuple
= isl_multi_pw_aff_set_pw_aff(tuple
, pos
, def
);
862 /* Read a list of variables and/or affine expressions and return the list
863 * as an isl_multi_pw_aff.
864 * The elements in the list are separated by either "," or "][".
866 static __isl_give isl_multi_pw_aff
*read_tuple_var_list(struct isl_stream
*s
,
870 struct isl_token
*tok
;
871 isl_multi_pw_aff
*res
;
873 res
= tuple_alloc(v
);
875 if (isl_stream_next_token_is(s
, ']'))
878 while ((tok
= next_token(s
)) != NULL
) {
881 res
= tuple_add_dim(res
, v
);
883 if (tok
->type
== ISL_TOKEN_IDENT
) {
885 int p
= vars_pos(v
, tok
->u
.s
, -1);
892 res
= tuple_set_dim_name(res
, i
, v
->v
->name
);
894 if (isl_stream_eat_if_available(s
, '='))
895 res
= read_tuple_var_def(s
, res
, i
, v
);
897 isl_stream_push_token(s
, tok
);
899 if (vars_add_anon(v
) < 0)
901 res
= read_tuple_var_def(s
, res
, i
, v
);
904 tok
= isl_stream_next_token(s
);
905 if (tok
&& tok
->type
== ']' &&
906 isl_stream_next_token_is(s
, '[')) {
908 tok
= isl_stream_next_token(s
);
909 } else if (!tok
|| tok
->type
!= ',')
916 isl_stream_push_token(s
, tok
);
921 return isl_multi_pw_aff_free(res
);
924 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
926 static __isl_give isl_multi_pw_aff
*read_tuple(struct isl_stream
*s
,
929 struct isl_token
*tok
;
931 isl_multi_pw_aff
*res
= NULL
;
933 tok
= isl_stream_next_token(s
);
936 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
937 name
= strdup(tok
->u
.s
);
942 isl_stream_push_token(s
, tok
);
943 if (isl_stream_eat(s
, '['))
945 if (next_is_tuple(s
)) {
946 isl_multi_pw_aff
*out
;
948 res
= read_tuple(s
, v
);
949 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
951 out
= read_tuple(s
, v
);
952 n
= isl_multi_pw_aff_dim(out
, isl_dim_out
);
953 res
= isl_multi_pw_aff_add_dims(res
, isl_dim_in
, n
);
954 res
= isl_multi_pw_aff_range_product(res
, out
);
956 res
= read_tuple_var_list(s
, v
);
957 if (isl_stream_eat(s
, ']'))
961 res
= isl_multi_pw_aff_set_tuple_name(res
, isl_dim_out
, name
);
968 return isl_multi_pw_aff_free(res
);
971 /* Read a tuple from "s" and add it to "map".
972 * The tuple is initially represented as an isl_multi_pw_aff.
973 * We first create the appropriate space in "map" based on the range
974 * space of this isl_multi_pw_aff. Then, we add equalities based
975 * on the affine expressions. These live in an anonymous space,
976 * however, so we first need to reset the space to that of "map".
978 static __isl_give isl_map
*read_map_tuple(struct isl_stream
*s
,
979 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
)
982 isl_multi_pw_aff
*tuple
;
983 isl_space
*space
= NULL
;
985 tuple
= read_tuple(s
, v
);
989 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
990 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
992 if (type
== isl_dim_param
) {
993 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
994 isl_space_is_wrapping(space
)) {
995 isl_die(s
->ctx
, isl_error_invalid
,
996 "parameter tuples cannot be named or nested",
999 map
= isl_map_add_dims(map
, type
, n
);
1000 for (i
= 0; i
< n
; ++i
) {
1002 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1003 isl_die(s
->ctx
, isl_error_invalid
,
1004 "parameters must be named",
1006 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1007 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1009 } else if (type
== isl_dim_in
) {
1012 set
= isl_set_universe(isl_space_copy(space
));
1013 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1014 map
= isl_map_from_domain(set
);
1018 set
= isl_set_universe(isl_space_copy(space
));
1019 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1022 for (i
= 0; i
< n
; ++i
) {
1029 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1030 space
= isl_pw_aff_get_domain_space(pa
);
1031 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1032 aff
= isl_aff_add_coefficient_si(aff
,
1033 isl_dim_in
, v
->n
- n
+ i
, -1);
1034 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1035 set
= isl_pw_aff_zero_set(pa
);
1036 map_i
= isl_map_from_range(set
);
1037 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1038 map
= isl_map_intersect(map
, map_i
);
1041 isl_space_free(space
);
1042 isl_multi_pw_aff_free(tuple
);
1045 isl_space_free(space
);
1046 isl_multi_pw_aff_free(tuple
);
1051 static __isl_give isl_set
*construct_constraints(
1052 __isl_take isl_set
*set
, int type
,
1053 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
)
1057 if (type
== ISL_TOKEN_LE
)
1058 cond
= isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left
),
1059 isl_pw_aff_list_copy(right
));
1060 else if (type
== ISL_TOKEN_GE
)
1061 cond
= isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left
),
1062 isl_pw_aff_list_copy(right
));
1063 else if (type
== ISL_TOKEN_LT
)
1064 cond
= isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left
),
1065 isl_pw_aff_list_copy(right
));
1066 else if (type
== ISL_TOKEN_GT
)
1067 cond
= isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left
),
1068 isl_pw_aff_list_copy(right
));
1069 else if (type
== ISL_TOKEN_NE
)
1070 cond
= isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left
),
1071 isl_pw_aff_list_copy(right
));
1073 cond
= isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left
),
1074 isl_pw_aff_list_copy(right
));
1076 return isl_set_intersect(set
, cond
);
1079 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
1080 struct vars
*v
, __isl_take isl_map
*map
)
1082 struct isl_token
*tok
= NULL
;
1083 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1086 set
= isl_map_wrap(map
);
1087 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1090 tok
= isl_stream_next_token(s
);
1091 if (!is_comparator(tok
)) {
1092 isl_stream_error(s
, tok
, "missing operator");
1094 isl_stream_push_token(s
, tok
);
1099 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1103 set
= construct_constraints(set
, tok
->type
, list1
, list2
);
1104 isl_token_free(tok
);
1105 isl_pw_aff_list_free(list1
);
1108 tok
= isl_stream_next_token(s
);
1109 if (!is_comparator(tok
)) {
1111 isl_stream_push_token(s
, tok
);
1115 isl_pw_aff_list_free(list1
);
1117 return isl_set_unwrap(set
);
1120 isl_token_free(tok
);
1121 isl_pw_aff_list_free(list1
);
1122 isl_pw_aff_list_free(list2
);
1127 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1128 struct vars
*v
, __isl_take isl_map
*map
)
1131 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1133 map
= isl_map_from_domain(isl_map_wrap(map
));
1134 map
= read_defined_var_list(s
, v
, map
);
1136 if (isl_stream_eat(s
, ':'))
1139 map
= read_disjuncts(s
, v
, map
);
1140 map
= isl_set_unwrap(isl_map_domain(map
));
1142 vars_drop(v
, v
->n
- n
);
1143 if (seen_paren
&& isl_stream_eat(s
, ')'))
1152 /* Parse an expression between parentheses and push the result
1153 * back on the stream.
1155 * The parsed expression may be either an affine expression
1156 * or a condition. The first type is pushed onto the stream
1157 * as an isl_pw_aff, while the second is pushed as an isl_map.
1159 * If the initial token indicates the start of a condition,
1160 * we parse it as such.
1161 * Otherwise, we first parse an affine expression and push
1162 * that onto the stream. If the affine expression covers the
1163 * entire expression between parentheses, we return.
1164 * Otherwise, we assume that the affine expression is the
1165 * start of a condition and continue parsing.
1167 static int resolve_paren_expr(struct isl_stream
*s
,
1168 struct vars
*v
, __isl_take isl_map
*map
)
1170 struct isl_token
*tok
, *tok2
;
1174 tok
= isl_stream_next_token(s
);
1175 if (!tok
|| tok
->type
!= '(')
1178 if (isl_stream_next_token_is(s
, '('))
1179 if (resolve_paren_expr(s
, v
, isl_map_copy(map
)))
1182 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1183 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1184 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1185 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1186 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1187 map
= read_disjuncts(s
, v
, map
);
1188 if (isl_stream_eat(s
, ')'))
1190 tok
->type
= ISL_TOKEN_MAP
;
1192 isl_stream_push_token(s
, tok
);
1196 tok2
= isl_stream_next_token(s
);
1201 isl_stream_push_token(s
, tok2
);
1203 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1207 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1210 tok2
->type
= ISL_TOKEN_AFF
;
1211 tok2
->u
.pwaff
= pwaff
;
1213 if (isl_stream_eat_if_available(s
, ')')) {
1214 isl_stream_push_token(s
, tok2
);
1215 isl_token_free(tok
);
1220 isl_stream_push_token(s
, tok2
);
1222 map
= read_disjuncts(s
, v
, map
);
1223 if (isl_stream_eat(s
, ')'))
1226 tok
->type
= ISL_TOKEN_MAP
;
1228 isl_stream_push_token(s
, tok
);
1232 isl_pw_aff_free(pwaff
);
1234 isl_token_free(tok
);
1239 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1240 struct vars
*v
, __isl_take isl_map
*map
)
1242 if (isl_stream_next_token_is(s
, '('))
1243 if (resolve_paren_expr(s
, v
, isl_map_copy(map
)))
1246 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1247 struct isl_token
*tok
;
1248 tok
= isl_stream_next_token(s
);
1252 map
= isl_map_copy(tok
->u
.map
);
1253 isl_token_free(tok
);
1257 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1258 return read_exists(s
, v
, map
);
1260 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1263 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1264 isl_space
*dim
= isl_map_get_space(map
);
1266 return isl_map_empty(dim
);
1269 return add_constraint(s
, v
, map
);
1275 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1276 struct vars
*v
, __isl_take isl_map
*map
)
1281 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1282 res
= read_conjunct(s
, v
, isl_map_copy(map
));
1284 res
= isl_map_subtract(isl_map_copy(map
), res
);
1286 while (isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1289 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1290 res_i
= read_conjunct(s
, v
, isl_map_copy(map
));
1292 res
= isl_map_subtract(res
, res_i
);
1294 res
= isl_map_intersect(res
, res_i
);
1301 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1302 struct vars
*v
, __isl_take isl_map
*map
)
1306 if (isl_stream_next_token_is(s
, '}')) {
1307 isl_space
*dim
= isl_map_get_space(map
);
1309 return isl_map_universe(dim
);
1312 res
= read_conjuncts(s
, v
, isl_map_copy(map
));
1313 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1316 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
));
1317 res
= isl_map_union(res
, res_i
);
1324 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1326 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1327 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1328 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1329 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1331 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1332 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1333 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1335 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1336 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1337 isl_basic_map_dim(bmap
, isl_dim_in
) +
1338 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1339 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1341 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1347 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1348 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1351 struct isl_token
*tok
;
1361 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1362 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1364 tok
= isl_stream_next_token(s
);
1365 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1366 isl_stream_error(s
, tok
, "expecting coefficient");
1368 isl_stream_push_token(s
, tok
);
1371 if (!tok
->on_new_line
) {
1372 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1373 isl_stream_push_token(s
, tok
);
1377 type
= isl_int_get_si(tok
->u
.v
);
1378 isl_token_free(tok
);
1380 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1382 k
= isl_basic_map_alloc_equality(bmap
);
1385 k
= isl_basic_map_alloc_inequality(bmap
);
1391 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1393 tok
= isl_stream_next_token(s
);
1394 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1395 isl_stream_error(s
, tok
, "expecting coefficient");
1397 isl_stream_push_token(s
, tok
);
1400 if (tok
->on_new_line
) {
1401 isl_stream_error(s
, tok
,
1402 "coefficient should not appear on new line");
1403 isl_stream_push_token(s
, tok
);
1406 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1407 isl_int_set(c
[pos
], tok
->u
.v
);
1408 isl_token_free(tok
);
1413 isl_basic_map_free(bmap
);
1417 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
)
1420 struct isl_token
*tok
;
1421 struct isl_token
*tok2
;
1424 unsigned in
= 0, out
, local
= 0;
1425 struct isl_basic_map
*bmap
= NULL
;
1428 tok
= isl_stream_next_token(s
);
1430 isl_stream_error(s
, NULL
, "unexpected EOF");
1433 tok2
= isl_stream_next_token(s
);
1435 isl_token_free(tok
);
1436 isl_stream_error(s
, NULL
, "unexpected EOF");
1439 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1440 isl_stream_push_token(s
, tok2
);
1441 isl_stream_push_token(s
, tok
);
1442 isl_stream_error(s
, NULL
,
1443 "expecting constraint matrix dimensions");
1446 n_row
= isl_int_get_si(tok
->u
.v
);
1447 n_col
= isl_int_get_si(tok2
->u
.v
);
1448 on_new_line
= tok2
->on_new_line
;
1449 isl_token_free(tok2
);
1450 isl_token_free(tok
);
1451 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1452 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1453 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1454 tok
= isl_stream_next_token_on_same_line(s
);
1456 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1457 isl_stream_error(s
, tok
,
1458 "expecting number of output dimensions");
1459 isl_stream_push_token(s
, tok
);
1462 out
= isl_int_get_si(tok
->u
.v
);
1463 isl_token_free(tok
);
1465 tok
= isl_stream_next_token_on_same_line(s
);
1466 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1467 isl_stream_error(s
, tok
,
1468 "expecting number of input dimensions");
1470 isl_stream_push_token(s
, tok
);
1473 in
= isl_int_get_si(tok
->u
.v
);
1474 isl_token_free(tok
);
1476 tok
= isl_stream_next_token_on_same_line(s
);
1477 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1478 isl_stream_error(s
, tok
,
1479 "expecting number of existentials");
1481 isl_stream_push_token(s
, tok
);
1484 local
= isl_int_get_si(tok
->u
.v
);
1485 isl_token_free(tok
);
1487 tok
= isl_stream_next_token_on_same_line(s
);
1488 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1489 isl_stream_error(s
, tok
,
1490 "expecting number of parameters");
1492 isl_stream_push_token(s
, tok
);
1495 nparam
= isl_int_get_si(tok
->u
.v
);
1496 isl_token_free(tok
);
1497 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1498 isl_stream_error(s
, NULL
,
1499 "dimensions don't match");
1503 out
= n_col
- 2 - nparam
;
1504 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1508 for (i
= 0; i
< local
; ++i
) {
1509 int k
= isl_basic_map_alloc_div(bmap
);
1512 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1515 for (i
= 0; i
< n_row
; ++i
)
1516 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1518 tok
= isl_stream_next_token_on_same_line(s
);
1520 isl_stream_error(s
, tok
, "unexpected extra token on line");
1521 isl_stream_push_token(s
, tok
);
1525 bmap
= isl_basic_map_simplify(bmap
);
1526 bmap
= isl_basic_map_finalize(bmap
);
1529 isl_basic_map_free(bmap
);
1533 static struct isl_map
*map_read_polylib(struct isl_stream
*s
)
1535 struct isl_token
*tok
;
1536 struct isl_token
*tok2
;
1538 struct isl_map
*map
;
1540 tok
= isl_stream_next_token(s
);
1542 isl_stream_error(s
, NULL
, "unexpected EOF");
1545 tok2
= isl_stream_next_token_on_same_line(s
);
1546 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1547 isl_stream_push_token(s
, tok2
);
1548 isl_stream_push_token(s
, tok
);
1549 return isl_map_from_basic_map(basic_map_read_polylib(s
));
1552 isl_stream_error(s
, tok2
, "unexpected token");
1553 isl_stream_push_token(s
, tok2
);
1554 isl_stream_push_token(s
, tok
);
1557 n
= isl_int_get_si(tok
->u
.v
);
1558 isl_token_free(tok
);
1560 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1562 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
1564 for (i
= 1; map
&& i
< n
; ++i
)
1565 map
= isl_map_union(map
,
1566 isl_map_from_basic_map(basic_map_read_polylib(s
)));
1571 static int optional_power(struct isl_stream
*s
)
1574 struct isl_token
*tok
;
1576 tok
= isl_stream_next_token(s
);
1579 if (tok
->type
!= '^') {
1580 isl_stream_push_token(s
, tok
);
1583 isl_token_free(tok
);
1584 tok
= isl_stream_next_token(s
);
1585 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1586 isl_stream_error(s
, tok
, "expecting exponent");
1588 isl_stream_push_token(s
, tok
);
1591 pow
= isl_int_get_si(tok
->u
.v
);
1592 isl_token_free(tok
);
1596 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1597 __isl_keep isl_map
*map
, struct vars
*v
);
1599 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1600 __isl_keep isl_map
*map
, struct vars
*v
)
1602 isl_pw_qpolynomial
*pwqp
;
1603 struct isl_token
*tok
;
1605 tok
= next_token(s
);
1607 isl_stream_error(s
, NULL
, "unexpected EOF");
1610 if (tok
->type
== '(') {
1613 isl_token_free(tok
);
1614 pwqp
= read_term(s
, map
, v
);
1617 if (isl_stream_eat(s
, ')'))
1619 pow
= optional_power(s
);
1620 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1621 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1622 struct isl_token
*tok2
;
1623 tok2
= isl_stream_next_token(s
);
1624 isl_qpolynomial
*qp
;
1625 if (tok2
&& tok2
->type
== '/') {
1626 isl_token_free(tok2
);
1627 tok2
= next_token(s
);
1628 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1629 isl_stream_error(s
, tok2
, "expected denominator");
1630 isl_token_free(tok
);
1631 isl_token_free(tok2
);
1634 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1635 tok
->u
.v
, tok2
->u
.v
);
1636 isl_token_free(tok2
);
1638 isl_stream_push_token(s
, tok2
);
1639 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1642 isl_token_free(tok
);
1643 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1644 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1645 isl_qpolynomial
*qp
;
1646 isl_token_free(tok
);
1647 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1648 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1649 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1650 isl_qpolynomial
*qp
;
1651 isl_token_free(tok
);
1652 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1653 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1654 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1656 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1658 isl_qpolynomial
*qp
;
1660 isl_token_free(tok
);
1664 vars_drop(v
, v
->n
- n
);
1665 isl_stream_error(s
, tok
, "unknown identifier");
1666 isl_token_free(tok
);
1669 isl_token_free(tok
);
1670 pow
= optional_power(s
);
1671 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
1672 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1673 } else if (tok
->type
== '[') {
1677 isl_stream_push_token(s
, tok
);
1678 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
1679 pow
= optional_power(s
);
1680 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
1681 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1682 } else if (tok
->type
== '-') {
1683 isl_token_free(tok
);
1684 pwqp
= read_factor(s
, map
, v
);
1685 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
1687 isl_stream_error(s
, tok
, "unexpected isl_token");
1688 isl_stream_push_token(s
, tok
);
1692 if (isl_stream_eat_if_available(s
, '*') ||
1693 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1694 isl_pw_qpolynomial
*pwqp2
;
1696 pwqp2
= read_factor(s
, map
, v
);
1697 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
1702 isl_pw_qpolynomial_free(pwqp
);
1706 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1707 __isl_keep isl_map
*map
, struct vars
*v
)
1709 struct isl_token
*tok
;
1710 isl_pw_qpolynomial
*pwqp
;
1712 pwqp
= read_factor(s
, map
, v
);
1715 tok
= next_token(s
);
1719 if (tok
->type
== '+') {
1720 isl_pw_qpolynomial
*pwqp2
;
1722 isl_token_free(tok
);
1723 pwqp2
= read_factor(s
, map
, v
);
1724 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1725 } else if (tok
->type
== '-') {
1726 isl_pw_qpolynomial
*pwqp2
;
1728 isl_token_free(tok
);
1729 pwqp2
= read_factor(s
, map
, v
);
1730 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
1731 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1732 isl_int_is_neg(tok
->u
.v
)) {
1733 isl_pw_qpolynomial
*pwqp2
;
1735 isl_stream_push_token(s
, tok
);
1736 pwqp2
= read_factor(s
, map
, v
);
1737 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1739 isl_stream_push_token(s
, tok
);
1747 static __isl_give isl_map
*read_optional_disjuncts(struct isl_stream
*s
,
1748 __isl_take isl_map
*map
, struct vars
*v
)
1750 struct isl_token
*tok
;
1752 tok
= isl_stream_next_token(s
);
1754 isl_stream_error(s
, NULL
, "unexpected EOF");
1757 if (tok
->type
== ':' ||
1758 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
1759 isl_token_free(tok
);
1760 map
= read_disjuncts(s
, v
, map
);
1762 isl_stream_push_token(s
, tok
);
1770 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1771 __isl_take isl_map
*map
, struct vars
*v
, int n
)
1773 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1774 isl_pw_qpolynomial
*pwqp
;
1775 struct isl_set
*set
;
1777 pwqp
= read_term(s
, map
, v
);
1778 map
= read_optional_disjuncts(s
, map
, v
);
1779 set
= isl_map_range(map
);
1781 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
1783 vars_drop(v
, v
->n
- n
);
1789 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
1790 __isl_take isl_set
*set
, struct vars
*v
, int n
)
1792 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
1793 isl_pw_qpolynomial
*pwqp
;
1794 isl_pw_qpolynomial_fold
*pwf
= NULL
;
1796 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
1797 return obj_read_poly(s
, set
, v
, n
);
1799 if (isl_stream_eat(s
, '('))
1802 pwqp
= read_term(s
, set
, v
);
1803 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
1805 while (isl_stream_eat_if_available(s
, ',')) {
1806 isl_pw_qpolynomial_fold
*pwf_i
;
1807 pwqp
= read_term(s
, set
, v
);
1808 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
1810 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
1813 if (isl_stream_eat(s
, ')'))
1816 set
= read_optional_disjuncts(s
, set
, v
);
1817 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
1819 vars_drop(v
, v
->n
- n
);
1825 isl_pw_qpolynomial_fold_free(pwf
);
1826 obj
.type
= isl_obj_none
;
1830 static int is_rational(struct isl_stream
*s
)
1832 struct isl_token
*tok
;
1834 tok
= isl_stream_next_token(s
);
1837 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
1838 isl_token_free(tok
);
1839 isl_stream_eat(s
, ':');
1843 isl_stream_push_token(s
, tok
);
1848 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
1849 __isl_take isl_map
*map
, struct vars
*v
)
1851 struct isl_token
*tok
;
1852 struct isl_obj obj
= { isl_obj_set
, NULL
};
1856 map
= isl_map_set_rational(map
);
1858 if (isl_stream_next_token_is(s
, ':')) {
1859 obj
.type
= isl_obj_set
;
1860 obj
.v
= read_optional_disjuncts(s
, map
, v
);
1864 if (!next_is_tuple(s
))
1865 return obj_read_poly_or_fold(s
, map
, v
, n
);
1867 map
= read_map_tuple(s
, map
, isl_dim_in
, v
);
1870 tok
= isl_stream_next_token(s
);
1873 if (tok
->type
== ISL_TOKEN_TO
) {
1874 obj
.type
= isl_obj_map
;
1875 isl_token_free(tok
);
1876 if (!next_is_tuple(s
)) {
1877 isl_set
*set
= isl_map_domain(map
);
1878 return obj_read_poly_or_fold(s
, set
, v
, n
);
1880 map
= read_map_tuple(s
, map
, isl_dim_out
, v
);
1884 map
= isl_map_domain(map
);
1885 isl_stream_push_token(s
, tok
);
1888 map
= read_optional_disjuncts(s
, map
, v
);
1890 vars_drop(v
, v
->n
- n
);
1896 obj
.type
= isl_obj_none
;
1900 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
1902 if (obj
.type
== isl_obj_map
) {
1903 obj
.v
= isl_union_map_from_map(obj
.v
);
1904 obj
.type
= isl_obj_union_map
;
1905 } else if (obj
.type
== isl_obj_set
) {
1906 obj
.v
= isl_union_set_from_set(obj
.v
);
1907 obj
.type
= isl_obj_union_set
;
1908 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
1909 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1910 obj
.type
= isl_obj_union_pw_qpolynomial
;
1911 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
1912 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1913 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1915 isl_assert(ctx
, 0, goto error
);
1918 obj
.type
->free(obj
.v
);
1919 obj
.type
= isl_obj_none
;
1923 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
1924 struct isl_obj obj1
, struct isl_obj obj2
)
1926 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
1927 obj1
= to_union(ctx
, obj1
);
1928 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
1929 obj2
= to_union(ctx
, obj2
);
1930 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
1931 obj1
= to_union(ctx
, obj1
);
1932 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
1933 obj2
= to_union(ctx
, obj2
);
1934 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
1935 obj2
.type
== isl_obj_union_pw_qpolynomial
)
1936 obj1
= to_union(ctx
, obj1
);
1937 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
1938 obj2
.type
== isl_obj_pw_qpolynomial
)
1939 obj2
= to_union(ctx
, obj2
);
1940 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
1941 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
1942 obj1
= to_union(ctx
, obj1
);
1943 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
1944 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
1945 obj2
= to_union(ctx
, obj2
);
1946 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
1947 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
1948 obj1
= to_union(ctx
, obj1
);
1949 obj2
= to_union(ctx
, obj2
);
1951 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
1952 obj1
= to_union(ctx
, obj1
);
1953 obj2
= to_union(ctx
, obj2
);
1955 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
1956 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
1957 obj1
= to_union(ctx
, obj1
);
1958 obj2
= to_union(ctx
, obj2
);
1960 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
1961 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
1962 obj1
= to_union(ctx
, obj1
);
1963 obj2
= to_union(ctx
, obj2
);
1965 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
1968 obj1
.type
->free(obj1
.v
);
1969 obj2
.type
->free(obj2
.v
);
1970 obj1
.type
= isl_obj_none
;
1975 static struct isl_obj
obj_read(struct isl_stream
*s
)
1977 isl_map
*map
= NULL
;
1978 struct isl_token
*tok
;
1979 struct vars
*v
= NULL
;
1980 struct isl_obj obj
= { isl_obj_set
, NULL
};
1982 tok
= next_token(s
);
1984 isl_stream_error(s
, NULL
, "unexpected EOF");
1987 if (tok
->type
== ISL_TOKEN_VALUE
) {
1988 struct isl_token
*tok2
;
1989 struct isl_map
*map
;
1991 tok2
= isl_stream_next_token(s
);
1992 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
1993 isl_int_is_neg(tok2
->u
.v
)) {
1995 isl_stream_push_token(s
, tok2
);
1996 obj
.type
= isl_obj_int
;
1997 obj
.v
= isl_int_obj_alloc(s
->ctx
, tok
->u
.v
);
1998 isl_token_free(tok
);
2001 isl_stream_push_token(s
, tok2
);
2002 isl_stream_push_token(s
, tok
);
2003 map
= map_read_polylib(s
);
2006 if (isl_map_may_be_set(map
))
2007 obj
.v
= isl_map_range(map
);
2009 obj
.type
= isl_obj_map
;
2014 v
= vars_new(s
->ctx
);
2016 isl_stream_push_token(s
, tok
);
2019 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2020 if (tok
->type
== '[') {
2021 isl_stream_push_token(s
, tok
);
2022 map
= read_map_tuple(s
, map
, isl_dim_param
, v
);
2025 tok
= isl_stream_next_token(s
);
2026 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2027 isl_stream_error(s
, tok
, "expecting '->'");
2029 isl_stream_push_token(s
, tok
);
2032 isl_token_free(tok
);
2033 tok
= isl_stream_next_token(s
);
2035 if (!tok
|| tok
->type
!= '{') {
2036 isl_stream_error(s
, tok
, "expecting '{'");
2038 isl_stream_push_token(s
, tok
);
2041 isl_token_free(tok
);
2043 tok
= isl_stream_next_token(s
);
2046 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2047 isl_token_free(tok
);
2048 if (isl_stream_eat(s
, '='))
2050 map
= read_map_tuple(s
, map
, isl_dim_param
, v
);
2053 } else if (tok
->type
== '}') {
2054 obj
.type
= isl_obj_union_set
;
2055 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2056 isl_token_free(tok
);
2059 isl_stream_push_token(s
, tok
);
2064 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2065 if (o
.type
== isl_obj_none
|| !o
.v
)
2070 obj
= obj_add(s
->ctx
, obj
, o
);
2071 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2074 tok
= isl_stream_next_token(s
);
2075 if (!tok
|| tok
->type
!= ';')
2077 isl_token_free(tok
);
2078 if (isl_stream_next_token_is(s
, '}')) {
2079 tok
= isl_stream_next_token(s
);
2084 if (tok
&& tok
->type
== '}') {
2085 isl_token_free(tok
);
2087 isl_stream_error(s
, tok
, "unexpected isl_token");
2089 isl_token_free(tok
);
2099 obj
.type
->free(obj
.v
);
2106 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
2111 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
)
2117 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2118 obj
.type
== isl_obj_set
, goto error
);
2120 if (obj
.type
== isl_obj_set
)
2121 obj
.v
= isl_map_from_range(obj
.v
);
2125 obj
.type
->free(obj
.v
);
2129 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
)
2135 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2136 obj
.v
= isl_map_range(obj
.v
);
2137 obj
.type
= isl_obj_set
;
2139 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2144 obj
.type
->free(obj
.v
);
2148 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2153 if (obj
.type
== isl_obj_map
) {
2154 obj
.type
= isl_obj_union_map
;
2155 obj
.v
= isl_union_map_from_map(obj
.v
);
2157 if (obj
.type
== isl_obj_set
) {
2158 obj
.type
= isl_obj_union_set
;
2159 obj
.v
= isl_union_set_from_set(obj
.v
);
2161 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2162 isl_union_set_is_empty(obj
.v
))
2163 obj
.type
= isl_obj_union_map
;
2164 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2165 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2169 obj
.type
->free(obj
.v
);
2173 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2178 if (obj
.type
== isl_obj_set
) {
2179 obj
.type
= isl_obj_union_set
;
2180 obj
.v
= isl_union_set_from_set(obj
.v
);
2183 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2187 obj
.type
->free(obj
.v
);
2191 static __isl_give isl_basic_map
*basic_map_read(struct isl_stream
*s
)
2194 struct isl_map
*map
;
2195 struct isl_basic_map
*bmap
;
2202 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2205 bmap
= isl_basic_map_empty_like_map(map
);
2207 bmap
= isl_basic_map_copy(map
->p
[0]);
2217 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
)
2219 isl_basic_map
*bmap
;
2220 bmap
= basic_map_read(s
);
2223 if (!isl_basic_map_may_be_set(bmap
))
2224 isl_die(s
->ctx
, isl_error_invalid
,
2225 "input is not a set", goto error
);
2226 return isl_basic_map_range(bmap
);
2228 isl_basic_map_free(bmap
);
2232 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2235 struct isl_basic_map
*bmap
;
2236 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2239 bmap
= basic_map_read(s
);
2244 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2247 isl_basic_set
*bset
;
2248 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2251 bset
= basic_set_read(s
);
2256 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2259 struct isl_basic_map
*bmap
;
2260 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2263 bmap
= basic_map_read(s
);
2268 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2271 isl_basic_set
*bset
;
2272 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2275 bset
= basic_set_read(s
);
2280 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2283 struct isl_map
*map
;
2284 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2287 map
= isl_stream_read_map(s
);
2292 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2295 struct isl_map
*map
;
2296 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2299 map
= isl_stream_read_map(s
);
2304 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2308 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2311 set
= isl_stream_read_set(s
);
2316 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2320 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2323 set
= isl_stream_read_set(s
);
2328 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2331 isl_union_map
*umap
;
2332 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2335 umap
= isl_stream_read_union_map(s
);
2340 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2343 isl_union_map
*umap
;
2344 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2347 umap
= isl_stream_read_union_map(s
);
2352 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2355 isl_union_set
*uset
;
2356 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2359 uset
= isl_stream_read_union_set(s
);
2364 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2367 isl_union_set
*uset
;
2368 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2371 uset
= isl_stream_read_union_set(s
);
2376 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2378 struct isl_vec
*vec
= NULL
;
2379 struct isl_token
*tok
;
2383 tok
= isl_stream_next_token(s
);
2384 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2385 isl_stream_error(s
, tok
, "expecting vector length");
2389 size
= isl_int_get_si(tok
->u
.v
);
2390 isl_token_free(tok
);
2392 vec
= isl_vec_alloc(s
->ctx
, size
);
2394 for (j
= 0; j
< size
; ++j
) {
2395 tok
= isl_stream_next_token(s
);
2396 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2397 isl_stream_error(s
, tok
, "expecting constant value");
2400 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2401 isl_token_free(tok
);
2406 isl_token_free(tok
);
2411 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2413 return isl_vec_read_polylib(s
);
2416 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2419 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2427 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2428 struct isl_stream
*s
)
2434 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2439 obj
.type
->free(obj
.v
);
2443 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2446 isl_pw_qpolynomial
*pwqp
;
2447 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2450 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2455 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2458 isl_pw_qpolynomial
*pwqp
;
2459 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2462 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2467 /* Read an affine expression from "s" with domain (space) "dom".
2468 * We call accept_affine to parse a possibly piecewise affine expression
2469 * and then check that the result is a single affine expression on
2470 * a universe domain.
2472 static __isl_give isl_aff
*read_aff_with_dom(struct isl_stream
*s
,
2473 __isl_take isl_set
*dom
, struct vars
*v
)
2475 isl_aff
*aff
= NULL
;
2476 isl_pw_aff
*pwaff
= NULL
;
2478 if (!isl_set_plain_is_universe(dom
))
2479 isl_die(s
->ctx
, isl_error_invalid
,
2480 "expecting universe domain", goto error
);
2482 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2485 if (isl_stream_eat(s
, '['))
2488 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2490 if (isl_stream_eat(s
, ']'))
2492 if (isl_stream_eat(s
, '}'))
2499 isl_die(s
->ctx
, isl_error_invalid
,
2500 "expecting single affine expression", goto error
);
2501 if (!isl_set_plain_is_universe(pwaff
->p
[0].set
))
2502 isl_die(s
->ctx
, isl_error_invalid
,
2503 "expecting universe domain", goto error
);
2505 aff
= isl_aff_copy(pwaff
->p
[0].aff
);
2508 isl_pw_aff_free(pwaff
);
2513 isl_pw_aff_free(pwaff
);
2518 /* Is the next token an identifer not in "v"?
2520 static int next_is_fresh_ident(struct isl_stream
*s
, struct vars
*v
)
2524 struct isl_token
*tok
;
2526 tok
= isl_stream_next_token(s
);
2529 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
2530 isl_stream_push_token(s
, tok
);
2532 vars_drop(v
, v
->n
- n
);
2537 /* First read the domain of the affine expression, which may be
2538 * a parameter space or a set.
2539 * The tricky part is that we don't know if the domain is a set or not,
2540 * so when we are trying to read the domain, we may actually be reading
2541 * the affine expression itself (defined on a parameter domains)
2542 * If the tuple we are reading is named, we assume it's the domain.
2543 * Also, if inside the tuple, the first thing we find is a nested tuple
2544 * or a new identifier, we again assume it's the domain.
2545 * Otherwise, we assume we are reading an affine expression.
2547 static __isl_give isl_set
*read_aff_domain(struct isl_stream
*s
,
2548 __isl_take isl_set
*dom
, struct vars
*v
)
2550 struct isl_token
*tok
;
2552 tok
= isl_stream_next_token(s
);
2553 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
2554 isl_stream_push_token(s
, tok
);
2555 return read_map_tuple(s
, dom
, isl_dim_set
, v
);
2557 if (!tok
|| tok
->type
!= '[') {
2558 isl_stream_error(s
, tok
, "expecting '['");
2561 if (next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
2562 isl_stream_push_token(s
, tok
);
2563 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
);
2565 isl_stream_push_token(s
, tok
);
2570 isl_stream_push_token(s
, tok
);
2576 /* Read an affine expression from "s".
2577 * We first read the domain of the affine expression, which may be
2578 * a parameter space or a set, and then call read_aff_with_dom.
2580 __isl_give isl_aff
*isl_stream_read_aff(struct isl_stream
*s
)
2583 isl_set
*dom
= NULL
;
2585 v
= vars_new(s
->ctx
);
2589 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2590 if (next_is_tuple(s
)) {
2591 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
);
2592 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2595 if (isl_stream_eat(s
, '{'))
2598 dom
= read_aff_domain(s
, dom
, v
);
2599 return read_aff_with_dom(s
, dom
, v
);
2606 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2608 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(struct isl_stream
*s
,
2609 __isl_take isl_set
*dom
, struct vars
*v
)
2611 isl_pw_aff
*pwaff
= NULL
;
2613 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
2616 if (isl_stream_eat(s
, '['))
2619 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
2621 if (isl_stream_eat(s
, ']'))
2624 dom
= read_optional_disjuncts(s
, dom
, v
);
2625 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
2630 isl_pw_aff_free(pwaff
);
2634 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(struct isl_stream
*s
)
2637 isl_set
*dom
= NULL
;
2639 isl_pw_aff
*pa
= NULL
;
2642 v
= vars_new(s
->ctx
);
2646 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
2647 if (next_is_tuple(s
)) {
2648 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
);
2649 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
2652 if (isl_stream_eat(s
, '{'))
2656 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2657 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2658 vars_drop(v
, v
->n
- n
);
2660 while (isl_stream_eat_if_available(s
, ';')) {
2664 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
2665 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
2666 vars_drop(v
, v
->n
- n
);
2668 pa
= isl_pw_aff_union_add(pa
, pa_i
);
2671 if (isl_stream_eat(s
, '}'))
2680 isl_pw_aff_free(pa
);
2684 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2687 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2690 aff
= isl_stream_read_aff(s
);
2695 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
2698 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2701 pa
= isl_stream_read_pw_aff(s
);
2706 /* Read an isl_pw_multi_aff from "s".
2707 * We currently read a generic object and if it turns out to be a set or
2708 * a map, we convert that to an isl_pw_multi_aff.
2709 * It would be more efficient if we were to construct the isl_pw_multi_aff
2712 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(struct isl_stream
*s
)
2720 if (obj
.type
== isl_obj_map
)
2721 return isl_pw_multi_aff_from_map(obj
.v
);
2722 if (obj
.type
== isl_obj_set
)
2723 return isl_pw_multi_aff_from_set(obj
.v
);
2725 obj
.type
->free(obj
.v
);
2726 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
2730 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
2733 isl_pw_multi_aff
*pma
;
2734 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2737 pma
= isl_stream_read_pw_multi_aff(s
);
2742 /* Read a multi-affine expression from "s".
2743 * We call isl_stream_read_pw_multi_aff to parse a possibly piecewise
2744 * multi-affine expression and then check that the result is
2745 * a single multi-affine expression on a universe domain.
2747 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(struct isl_stream
*s
)
2749 isl_pw_multi_aff
*pma
;
2750 isl_multi_aff
*maff
;
2752 pma
= isl_stream_read_pw_multi_aff(s
);
2756 isl_die(s
->ctx
, isl_error_invalid
,
2757 "expecting single list of affine expressions",
2758 return isl_pw_multi_aff_free(pma
));
2759 if (!isl_set_plain_is_universe(pma
->p
[0].set
))
2760 isl_die(s
->ctx
, isl_error_invalid
, "expecting universe domain",
2761 return isl_pw_multi_aff_free(pma
));
2762 maff
= isl_multi_aff_copy(pma
->p
[0].maff
);
2763 isl_pw_multi_aff_free(pma
);
2767 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
2770 isl_multi_aff
*maff
;
2771 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2774 maff
= isl_stream_read_multi_aff(s
);
2779 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
2780 struct isl_stream
*s
)
2785 if (obj
.type
== isl_obj_pw_qpolynomial
) {
2786 obj
.type
= isl_obj_union_pw_qpolynomial
;
2787 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2790 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
2795 obj
.type
->free(obj
.v
);
2799 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
2800 isl_ctx
*ctx
, const char *str
)
2802 isl_union_pw_qpolynomial
*upwqp
;
2803 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2806 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);