2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
22 #include <isl_stream_private.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_set.h>
26 #include <isl/union_map.h>
27 #include <isl_mat_private.h>
28 #include <isl_aff_private.h>
29 #include <isl_vec_private.h>
31 #include <isl_val_private.h>
36 struct variable
*next
;
45 static struct vars
*vars_new(struct isl_ctx
*ctx
)
48 v
= isl_alloc_type(ctx
, struct vars
);
57 static void variable_free(struct variable
*var
)
60 struct variable
*next
= var
->next
;
67 static void vars_free(struct vars
*v
)
75 static void vars_drop(struct vars
*v
, int n
)
86 struct variable
*next
= var
->next
;
94 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
98 var
= isl_calloc_type(v
->ctx
, struct variable
);
101 var
->name
= strdup(name
);
102 var
->name
[len
] = '\0';
111 static int vars_pos(struct vars
*v
, const char *s
, int len
)
118 for (q
= v
->v
; q
; q
= q
->next
) {
119 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
126 v
->v
= variable_new(v
, s
, len
, v
->n
);
134 static int vars_add_anon(struct vars
*v
)
136 v
->v
= variable_new(v
, "", 0, v
->n
);
145 /* Obtain next token, with some preprocessing.
146 * In particular, evaluate expressions of the form x^y,
147 * with x and y values.
149 static struct isl_token
*next_token(__isl_keep isl_stream
*s
)
151 struct isl_token
*tok
, *tok2
;
153 tok
= isl_stream_next_token(s
);
154 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
156 if (!isl_stream_eat_if_available(s
, '^'))
158 tok2
= isl_stream_next_token(s
);
159 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
160 isl_stream_error(s
, tok2
, "expecting constant value");
164 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
166 isl_token_free(tok2
);
170 isl_token_free(tok2
);
174 /* Read an isl_val from "s".
176 * The following token sequences are recognized
179 * "-" "infty" -> -infty
184 * where n, d and v are integer constants.
186 __isl_give isl_val
*isl_stream_read_val(__isl_keep isl_stream
*s
)
188 struct isl_token
*tok
= NULL
;
189 struct isl_token
*tok2
= NULL
;
194 isl_stream_error(s
, NULL
, "unexpected EOF");
197 if (tok
->type
== ISL_TOKEN_INFTY
) {
199 return isl_val_infty(s
->ctx
);
201 if (tok
->type
== '-' &&
202 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
204 return isl_val_neginfty(s
->ctx
);
206 if (tok
->type
== ISL_TOKEN_NAN
) {
208 return isl_val_nan(s
->ctx
);
210 if (tok
->type
!= ISL_TOKEN_VALUE
) {
211 isl_stream_error(s
, tok
, "expecting value");
215 if (isl_stream_eat_if_available(s
, '/')) {
216 tok2
= next_token(s
);
218 isl_stream_error(s
, NULL
, "unexpected EOF");
221 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
222 isl_stream_error(s
, tok2
, "expecting value");
225 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
226 val
= isl_val_normalize(val
);
228 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
232 isl_token_free(tok2
);
236 isl_token_free(tok2
);
240 /* Read an isl_val from "str".
242 struct isl_val
*isl_val_read_from_str(struct isl_ctx
*ctx
,
246 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
249 val
= isl_stream_read_val(s
);
254 static int accept_cst_factor(__isl_keep isl_stream
*s
, isl_int
*f
)
256 struct isl_token
*tok
;
259 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
260 isl_stream_error(s
, tok
, "expecting constant value");
264 isl_int_mul(*f
, *f
, tok
->u
.v
);
268 if (isl_stream_eat_if_available(s
, '*'))
269 return accept_cst_factor(s
, f
);
277 /* Given an affine expression aff, return an affine expression
278 * for aff % d, with d the next token on the stream, which is
279 * assumed to be a constant.
281 * We introduce an integer division q = [aff/d] and the result
282 * is set to aff - d q.
284 static __isl_give isl_pw_aff
*affine_mod(__isl_keep isl_stream
*s
,
285 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
287 struct isl_token
*tok
;
291 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
292 isl_stream_error(s
, tok
, "expecting constant value");
296 q
= isl_pw_aff_copy(aff
);
297 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
298 q
= isl_pw_aff_floor(q
);
299 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
301 aff
= isl_pw_aff_sub(aff
, q
);
306 isl_pw_aff_free(aff
);
311 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
312 __isl_take isl_space
*space
, struct vars
*v
);
313 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
314 __isl_take isl_space
*dim
, struct vars
*v
);
316 static __isl_give isl_pw_aff
*accept_minmax(__isl_keep isl_stream
*s
,
317 __isl_take isl_space
*dim
, struct vars
*v
)
319 struct isl_token
*tok
;
320 isl_pw_aff_list
*list
= NULL
;
323 tok
= isl_stream_next_token(s
);
326 min
= tok
->type
== ISL_TOKEN_MIN
;
329 if (isl_stream_eat(s
, '('))
332 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
336 if (isl_stream_eat(s
, ')'))
340 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
343 isl_pw_aff_list_free(list
);
347 /* Is "tok" the start of an integer division?
349 static int is_start_of_div(struct isl_token
*tok
)
353 if (tok
->type
== '[')
355 if (tok
->type
== ISL_TOKEN_FLOOR
)
357 if (tok
->type
== ISL_TOKEN_CEIL
)
359 if (tok
->type
== ISL_TOKEN_FLOORD
)
361 if (tok
->type
== ISL_TOKEN_CEILD
)
366 /* Read an integer division from "s" and return it as an isl_pw_aff.
368 * The integer division can be of the form
370 * [<affine expression>]
371 * floor(<affine expression>)
372 * ceil(<affine expression>)
373 * floord(<affine expression>,<denominator>)
374 * ceild(<affine expression>,<denominator>)
376 static __isl_give isl_pw_aff
*accept_div(__isl_keep isl_stream
*s
,
377 __isl_take isl_space
*dim
, struct vars
*v
)
379 struct isl_token
*tok
;
383 isl_pw_aff
*pwaff
= NULL
;
385 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
387 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
389 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
391 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
394 if (isl_stream_eat(s
, '('))
397 if (isl_stream_eat(s
, '['))
401 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
404 if (isl_stream_eat(s
, ','))
410 if (tok
->type
!= ISL_TOKEN_VALUE
) {
411 isl_stream_error(s
, tok
, "expected denominator");
412 isl_stream_push_token(s
, tok
);
415 pwaff
= isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
420 pwaff
= isl_pw_aff_ceil(pwaff
);
422 pwaff
= isl_pw_aff_floor(pwaff
);
425 if (isl_stream_eat(s
, ')'))
428 if (isl_stream_eat(s
, ']'))
436 isl_pw_aff_free(pwaff
);
440 static __isl_give isl_pw_aff
*accept_affine_factor(__isl_keep isl_stream
*s
,
441 __isl_take isl_space
*dim
, struct vars
*v
)
443 struct isl_token
*tok
= NULL
;
444 isl_pw_aff
*res
= NULL
;
448 isl_stream_error(s
, NULL
, "unexpected EOF");
452 if (tok
->type
== ISL_TOKEN_AFF
) {
453 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
455 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
457 int pos
= vars_pos(v
, tok
->u
.s
, -1);
463 vars_drop(v
, v
->n
- n
);
464 isl_stream_error(s
, tok
, "unknown identifier");
468 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
471 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
472 res
= isl_pw_aff_from_aff(aff
);
474 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
475 if (isl_stream_eat_if_available(s
, '*')) {
476 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
477 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
481 ls
= isl_local_space_from_space(isl_space_copy(dim
));
482 aff
= isl_aff_zero_on_domain(ls
);
483 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
484 res
= isl_pw_aff_from_aff(aff
);
487 } else if (tok
->type
== '(') {
490 res
= accept_affine(s
, isl_space_copy(dim
), v
);
493 if (isl_stream_eat(s
, ')'))
495 } else if (is_start_of_div(tok
)) {
496 isl_stream_push_token(s
, tok
);
498 res
= accept_div(s
, isl_space_copy(dim
), v
);
499 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
500 isl_stream_push_token(s
, tok
);
502 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
504 isl_stream_error(s
, tok
, "expecting factor");
507 if (isl_stream_eat_if_available(s
, '%') ||
508 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
510 return affine_mod(s
, v
, res
);
512 if (isl_stream_eat_if_available(s
, '*')) {
515 isl_int_set_si(f
, 1);
516 if (accept_cst_factor(s
, &f
) < 0) {
520 res
= isl_pw_aff_scale(res
, f
);
523 if (isl_stream_eat_if_available(s
, '/')) {
526 isl_int_set_si(f
, 1);
527 if (accept_cst_factor(s
, &f
) < 0) {
531 res
= isl_pw_aff_scale_down(res
, f
);
540 isl_pw_aff_free(res
);
545 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
550 space
= isl_pw_aff_get_domain_space(pwaff
);
551 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
552 aff
= isl_aff_add_constant(aff
, v
);
554 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
557 /* Return a piecewise affine expression defined on the specified domain
558 * that represents NaN.
560 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
564 ls
= isl_local_space_from_space(isl_space_copy(space
));
565 return isl_pw_aff_nan_on_domain(ls
);
568 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
569 __isl_take isl_space
*space
, struct vars
*v
)
571 struct isl_token
*tok
= NULL
;
576 ls
= isl_local_space_from_space(isl_space_copy(space
));
577 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
584 isl_stream_error(s
, NULL
, "unexpected EOF");
587 if (tok
->type
== '-') {
592 if (tok
->type
== '(' || is_start_of_div(tok
) ||
593 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
594 tok
->type
== ISL_TOKEN_IDENT
||
595 tok
->type
== ISL_TOKEN_AFF
) {
597 isl_stream_push_token(s
, tok
);
599 term
= accept_affine_factor(s
,
600 isl_space_copy(space
), v
);
602 res
= isl_pw_aff_sub(res
, term
);
604 res
= isl_pw_aff_add(res
, term
);
608 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
610 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
611 if (isl_stream_eat_if_available(s
, '*') ||
612 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
614 term
= accept_affine_factor(s
,
615 isl_space_copy(space
), v
);
616 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
617 res
= isl_pw_aff_add(res
, term
);
621 res
= add_cst(res
, tok
->u
.v
);
624 } else if (tok
->type
== ISL_TOKEN_NAN
) {
625 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
627 isl_stream_error(s
, tok
, "unexpected isl_token");
628 isl_stream_push_token(s
, tok
);
629 isl_pw_aff_free(res
);
630 isl_space_free(space
);
636 if (tok
&& tok
->type
== '-') {
639 } else if (tok
&& tok
->type
== '+') {
642 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
643 isl_int_is_neg(tok
->u
.v
)) {
644 isl_stream_push_token(s
, tok
);
647 isl_stream_push_token(s
, tok
);
652 isl_space_free(space
);
655 isl_space_free(space
);
657 isl_pw_aff_free(res
);
661 /* Is "type" the type of a comparison operator between lists
662 * of affine expressions?
664 static int is_list_comparator_type(int type
)
667 case ISL_TOKEN_LEX_LT
:
668 case ISL_TOKEN_LEX_GT
:
669 case ISL_TOKEN_LEX_LE
:
670 case ISL_TOKEN_LEX_GE
:
677 static int is_comparator(struct isl_token
*tok
)
681 if (is_list_comparator_type(tok
->type
))
697 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
698 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
699 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
700 __isl_take isl_space
*dim
, struct vars
*v
, int rational
);
702 /* Accept a ternary operator, given the first argument.
704 static __isl_give isl_pw_aff
*accept_ternary(__isl_keep isl_stream
*s
,
705 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
708 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
713 if (isl_stream_eat(s
, '?'))
716 dim
= isl_space_wrap(isl_map_get_space(cond
));
717 pwaff1
= accept_extended_affine(s
, dim
, v
, rational
);
721 if (isl_stream_eat(s
, ':'))
724 dim
= isl_pw_aff_get_domain_space(pwaff1
);
725 pwaff2
= accept_extended_affine(s
, dim
, v
, rational
);
729 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
730 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
733 isl_pw_aff_free(pwaff1
);
734 isl_pw_aff_free(pwaff2
);
738 /* Set *line and *col to those of the next token, if any.
740 static void set_current_line_col(__isl_keep isl_stream
*s
, int *line
, int *col
)
742 struct isl_token
*tok
;
744 tok
= isl_stream_next_token(s
);
750 isl_stream_push_token(s
, tok
);
753 /* Push a token encapsulating "pa" onto "s", with the given
756 static int push_aff(__isl_keep isl_stream
*s
, int line
, int col
,
757 __isl_take isl_pw_aff
*pa
)
759 struct isl_token
*tok
;
761 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
764 tok
->type
= ISL_TOKEN_AFF
;
766 isl_stream_push_token(s
, tok
);
774 /* Accept an affine expression that may involve ternary operators.
775 * We first read an affine expression.
776 * If it is not followed by a comparison operator, we simply return it.
777 * Otherwise, we assume the affine expression is part of the first
778 * argument of a ternary operator and try to parse that.
780 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
781 __isl_take isl_space
*dim
, struct vars
*v
, int rational
)
786 struct isl_token
*tok
;
787 int line
= -1, col
= -1;
790 set_current_line_col(s
, &line
, &col
);
792 pwaff
= accept_affine(s
, dim
, v
);
794 pwaff
= isl_pw_aff_set_rational(pwaff
);
798 tok
= isl_stream_next_token(s
);
800 return isl_pw_aff_free(pwaff
);
802 is_comp
= is_comparator(tok
);
803 isl_stream_push_token(s
, tok
);
807 space
= isl_pw_aff_get_domain_space(pwaff
);
808 cond
= isl_map_universe(isl_space_unwrap(space
));
810 if (push_aff(s
, line
, col
, pwaff
) < 0)
811 cond
= isl_map_free(cond
);
815 cond
= read_formula(s
, v
, cond
, rational
);
817 return accept_ternary(s
, cond
, v
, rational
);
820 static __isl_give isl_map
*read_var_def(__isl_keep isl_stream
*s
,
821 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
828 if (type
== isl_dim_param
)
829 pos
= isl_map_dim(map
, isl_dim_param
);
831 pos
= isl_map_dim(map
, isl_dim_in
);
832 if (type
== isl_dim_out
) {
833 isl_size n_out
= isl_map_dim(map
, isl_dim_out
);
834 if (pos
< 0 || n_out
< 0)
835 return isl_map_free(map
);
841 return isl_map_free(map
);
844 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
846 def_map
= isl_map_from_pw_aff(def
);
847 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
848 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
850 map
= isl_map_intersect(map
, def_map
);
855 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
856 __isl_take isl_space
*dim
, struct vars
*v
)
859 isl_pw_aff_list
*list
;
860 struct isl_token
*tok
= NULL
;
862 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
863 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
868 tok
= isl_stream_next_token(s
);
870 isl_stream_error(s
, NULL
, "unexpected EOF");
873 if (tok
->type
!= ',') {
874 isl_stream_push_token(s
, tok
);
879 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
880 list
= isl_pw_aff_list_concat(list
,
881 isl_pw_aff_list_from_pw_aff(pwaff
));
890 isl_pw_aff_list_free(list
);
894 static __isl_give isl_map
*read_defined_var_list(__isl_keep isl_stream
*s
,
895 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
897 struct isl_token
*tok
;
899 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
903 if (tok
->type
!= ISL_TOKEN_IDENT
)
906 p
= vars_pos(v
, tok
->u
.s
, -1);
910 isl_stream_error(s
, tok
, "expecting unique identifier");
914 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
917 tok
= isl_stream_next_token(s
);
918 if (tok
&& tok
->type
== '=') {
920 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
921 tok
= isl_stream_next_token(s
);
924 if (!tok
|| tok
->type
!= ',')
930 isl_stream_push_token(s
, tok
);
939 static int next_is_tuple(__isl_keep isl_stream
*s
)
941 struct isl_token
*tok
;
944 tok
= isl_stream_next_token(s
);
947 if (tok
->type
== '[') {
948 isl_stream_push_token(s
, tok
);
951 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
952 isl_stream_push_token(s
, tok
);
956 is_tuple
= isl_stream_next_token_is(s
, '[');
958 isl_stream_push_token(s
, tok
);
963 /* Is "pa" an expression in term of earlier dimensions?
964 * The alternative is that the dimension is defined to be equal to itself,
965 * meaning that it has a universe domain and an expression that depends
966 * on itself. "i" is the position of the expression in a sequence
967 * of "n" expressions. The final dimensions of "pa" correspond to
968 * these "n" expressions.
970 static isl_bool
pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
975 return isl_bool_error
;
977 return isl_bool_true
;
978 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
979 return isl_bool_true
;
982 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
983 return isl_bool_true
;
984 return isl_bool_false
;
987 /* Does the tuple contain any dimensions that are defined
988 * in terms of earlier dimensions?
990 static isl_bool
tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
994 isl_bool has_expr
= isl_bool_false
;
997 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
999 return isl_bool_error
;
1000 for (i
= 0; i
< n
; ++i
) {
1001 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1002 has_expr
= pw_aff_is_expr(pa
, i
, n
);
1003 isl_pw_aff_free(pa
);
1004 if (has_expr
< 0 || has_expr
)
1011 /* Set the name of dimension "pos" in "space" to "name".
1012 * During printing, we add primes if the same name appears more than once
1013 * to distinguish the occurrences. Here, we remove those primes from "name"
1014 * before setting the name of the dimension.
1016 static __isl_give isl_space
*space_set_dim_name(__isl_take isl_space
*space
,
1017 int pos
, char *name
)
1024 prime
= strchr(name
, '\'');
1027 space
= isl_space_set_dim_name(space
, isl_dim_out
, pos
, name
);
1034 /* Accept a piecewise affine expression.
1036 * At the outer level, the piecewise affine expression may be of the form
1038 * aff1 : condition1; aff2 : conditions2; ...
1044 * each of the affine expressions may in turn include ternary operators.
1046 * There may be parentheses around some subexpression of "aff1"
1047 * around "aff1" itself, around "aff1 : condition1" and/or
1048 * around the entire piecewise affine expression.
1049 * We therefore remove the opening parenthesis (if any) from the stream
1050 * in case the closing parenthesis follows the colon, but if the closing
1051 * parenthesis is the first thing in the stream after the parsed affine
1052 * expression, we push the parsed expression onto the stream and parse
1053 * again in case the parentheses enclose some subexpression of "aff1".
1055 static __isl_give isl_pw_aff
*accept_piecewise_affine(__isl_keep isl_stream
*s
,
1056 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1059 isl_space
*res_space
;
1061 res_space
= isl_space_from_domain(isl_space_copy(space
));
1062 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1063 res
= isl_pw_aff_empty(res_space
);
1067 int line
= -1, col
= -1;
1069 set_current_line_col(s
, &line
, &col
);
1070 seen_paren
= isl_stream_eat_if_available(s
, '(');
1072 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1075 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1077 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1079 if (push_aff(s
, line
, col
, pa
) < 0)
1081 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1084 if (isl_stream_eat_if_available(s
, ':')) {
1085 isl_space
*dom_space
;
1088 dom_space
= isl_pw_aff_get_domain_space(pa
);
1089 dom
= isl_set_universe(dom_space
);
1090 dom
= read_formula(s
, v
, dom
, rational
);
1091 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
1094 res
= isl_pw_aff_union_add(res
, pa
);
1096 if (seen_paren
&& isl_stream_eat(s
, ')'))
1098 } while (isl_stream_eat_if_available(s
, ';'));
1100 isl_space_free(space
);
1104 isl_space_free(space
);
1105 return isl_pw_aff_free(res
);
1108 /* Read an affine expression from "s" for use in read_tuple.
1110 * accept_extended_affine requires a wrapped space as input.
1111 * read_tuple on the other hand expects each isl_pw_aff
1112 * to have an anonymous space. We therefore adjust the space
1113 * of the isl_pw_aff before returning it.
1115 static __isl_give isl_pw_aff
*read_tuple_var_def(__isl_keep isl_stream
*s
,
1116 struct vars
*v
, int rational
)
1121 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1123 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1125 space
= isl_space_set_alloc(s
->ctx
, 0, v
->n
);
1126 def
= isl_pw_aff_reset_domain_space(def
, space
);
1131 /* Read a list of tuple elements by calling "read_el" on each of them and
1132 * return a space with the same number of set dimensions derived from
1133 * the parameter space "space" and possibly updated by "read_el".
1134 * The elements in the list are separated by either "," or "][".
1135 * If "comma" is set then only "," is allowed.
1137 static __isl_give isl_space
*read_tuple_list(__isl_keep isl_stream
*s
,
1138 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1139 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1140 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1147 space
= isl_space_set_from_params(space
);
1149 if (isl_stream_next_token_is(s
, ']'))
1153 struct isl_token
*tok
;
1155 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1157 space
= read_el(s
, v
, space
, rational
, user
);
1161 tok
= isl_stream_next_token(s
);
1162 if (!comma
&& tok
&& tok
->type
== ']' &&
1163 isl_stream_next_token_is(s
, '[')) {
1164 isl_token_free(tok
);
1165 tok
= isl_stream_next_token(s
);
1166 } else if (!tok
|| tok
->type
!= ',') {
1168 isl_stream_push_token(s
, tok
);
1172 isl_token_free(tok
);
1178 /* Read a tuple space from "s" derived from the parameter space "space".
1179 * Call "read_el" on each element in the tuples.
1181 static __isl_give isl_space
*read_tuple_space(__isl_keep isl_stream
*s
,
1182 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1183 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1184 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1188 struct isl_token
*tok
;
1190 isl_space
*res
= NULL
;
1192 tok
= isl_stream_next_token(s
);
1195 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1196 name
= strdup(tok
->u
.s
);
1197 isl_token_free(tok
);
1201 isl_stream_push_token(s
, tok
);
1202 if (isl_stream_eat(s
, '['))
1204 if (next_is_tuple(s
)) {
1206 res
= read_tuple_space(s
, v
, isl_space_copy(space
),
1207 rational
, comma
, read_el
, user
);
1208 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1210 out
= read_tuple_space(s
, v
, isl_space_copy(space
),
1211 rational
, comma
, read_el
, user
);
1212 res
= isl_space_product(res
, out
);
1214 res
= read_tuple_list(s
, v
, isl_space_copy(space
),
1215 rational
, comma
, read_el
, user
);
1216 if (isl_stream_eat(s
, ']'))
1220 res
= isl_space_set_tuple_name(res
, isl_dim_set
, name
);
1224 isl_space_free(space
);
1228 isl_space_free(res
);
1229 isl_space_free(space
);
1233 /* Construct an isl_pw_aff defined on a space with v->n variables
1234 * that is equal to the last of those variables.
1236 static __isl_give isl_pw_aff
*identity_tuple_el(struct vars
*v
)
1241 space
= isl_space_set_alloc(v
->ctx
, 0, v
->n
);
1242 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1243 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
- 1, 1);
1244 return isl_pw_aff_from_aff(aff
);
1247 /* This function is called for each element in a tuple inside read_tuple.
1248 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1249 * over a space containing all variables in "v" defined so far.
1250 * The isl_pw_aff expresses the new variable in terms of earlier variables
1251 * if a definition is provided. Otherwise, it is represented as being
1253 * Add the isl_pw_aff to *list.
1254 * If the new variable was named, then adjust "space" accordingly and
1255 * return the updated space.
1257 static __isl_give isl_space
*read_tuple_pw_aff_el(__isl_keep isl_stream
*s
,
1258 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1260 isl_pw_aff_list
**list
= (isl_pw_aff_list
**) user
;
1262 struct isl_token
*tok
;
1265 tok
= next_token(s
);
1267 isl_stream_error(s
, NULL
, "unexpected EOF");
1268 return isl_space_free(space
);
1271 if (tok
->type
== ISL_TOKEN_IDENT
) {
1273 int p
= vars_pos(v
, tok
->u
.s
, -1);
1279 if (tok
->type
== '*') {
1280 if (vars_add_anon(v
) < 0)
1282 isl_token_free(tok
);
1283 pa
= identity_tuple_el(v
);
1284 } else if (new_name
) {
1285 isl_size pos
= isl_space_dim(space
, isl_dim_out
);
1289 space
= space_set_dim_name(space
, pos
, v
->v
->name
);
1290 isl_token_free(tok
);
1291 if (isl_stream_eat_if_available(s
, '='))
1292 pa
= read_tuple_var_def(s
, v
, rational
);
1294 pa
= identity_tuple_el(v
);
1296 isl_stream_push_token(s
, tok
);
1298 if (vars_add_anon(v
) < 0)
1300 pa
= read_tuple_var_def(s
, v
, rational
);
1303 *list
= isl_pw_aff_list_add(*list
, pa
);
1305 return isl_space_free(space
);
1309 isl_token_free(tok
);
1310 return isl_space_free(space
);
1313 /* Read a tuple and represent it as an isl_multi_pw_aff.
1314 * The range space of the isl_multi_pw_aff is the space of the tuple.
1315 * The domain space is an anonymous space
1316 * with a dimension for each variable in the set of variables in "v",
1317 * including the variables in the range.
1318 * If a given dimension is not defined in terms of earlier dimensions in
1319 * the input, then the corresponding isl_pw_aff is set equal to one time
1320 * the variable corresponding to the dimension being defined.
1322 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1323 * Each element in this list is defined over a space representing
1324 * the variables defined so far. We need to adjust the earlier
1325 * elements to have as many variables in the domain as the final
1326 * element in the list.
1328 static __isl_give isl_multi_pw_aff
*read_tuple(__isl_keep isl_stream
*s
,
1329 struct vars
*v
, int rational
, int comma
)
1334 isl_pw_aff_list
*list
;
1336 space
= isl_space_params_alloc(v
->ctx
, 0);
1337 list
= isl_pw_aff_list_alloc(s
->ctx
, 0);
1338 space
= read_tuple_space(s
, v
, space
, rational
, comma
,
1339 &read_tuple_pw_aff_el
, &list
);
1340 n
= isl_space_dim(space
, isl_dim_set
);
1342 space
= isl_space_free(space
);
1343 for (i
= 0; i
+ 1 < n
; ++i
) {
1346 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
1347 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, n
- (i
+ 1));
1348 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
1351 space
= isl_space_from_range(space
);
1352 space
= isl_space_add_dims(space
, isl_dim_in
, v
->n
);
1353 return isl_multi_pw_aff_from_pw_aff_list(space
, list
);
1356 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1357 * We first create the appropriate space in "map" based on the range
1358 * space of this isl_multi_pw_aff. Then, we add equalities based
1359 * on the affine expressions. These live in an anonymous space,
1360 * however, so we first need to reset the space to that of "map".
1362 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1363 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1369 isl_space
*space
= NULL
;
1371 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1374 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1375 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1379 if (type
== isl_dim_param
) {
1380 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1381 isl_space_is_wrapping(space
)) {
1382 isl_die(ctx
, isl_error_invalid
,
1383 "parameter tuples cannot be named or nested",
1386 map
= isl_map_add_dims(map
, type
, n
);
1387 for (i
= 0; i
< n
; ++i
) {
1389 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1390 isl_die(ctx
, isl_error_invalid
,
1391 "parameters must be named",
1393 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1394 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1396 } else if (type
== isl_dim_in
) {
1399 set
= isl_set_universe(isl_space_copy(space
));
1401 set
= isl_set_set_rational(set
);
1402 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1403 map
= isl_map_from_domain(set
);
1407 set
= isl_set_universe(isl_space_copy(space
));
1409 set
= isl_set_set_rational(set
);
1410 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1413 for (i
= 0; i
< n
; ++i
) {
1420 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1421 space
= isl_pw_aff_get_domain_space(pa
);
1422 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1423 aff
= isl_aff_add_coefficient_si(aff
,
1424 isl_dim_in
, v
->n
- n
+ i
, -1);
1425 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1427 pa
= isl_pw_aff_set_rational(pa
);
1428 set
= isl_pw_aff_zero_set(pa
);
1429 map_i
= isl_map_from_range(set
);
1430 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1431 map
= isl_map_intersect(map
, map_i
);
1434 isl_space_free(space
);
1435 isl_multi_pw_aff_free(tuple
);
1438 isl_space_free(space
);
1439 isl_multi_pw_aff_free(tuple
);
1444 /* Read a tuple from "s" and add it to "map".
1445 * The tuple is initially represented as an isl_multi_pw_aff and
1446 * then added to "map".
1448 static __isl_give isl_map
*read_map_tuple(__isl_keep isl_stream
*s
,
1449 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1450 int rational
, int comma
)
1452 isl_multi_pw_aff
*tuple
;
1454 tuple
= read_tuple(s
, v
, rational
, comma
);
1456 return isl_map_free(map
);
1458 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1461 /* Given two equal-length lists of piecewise affine expression with the space
1462 * of "set" as domain, construct a set in the same space that expresses
1463 * that "left" and "right" satisfy the comparison "type".
1465 * A space is constructed of the same dimension as the number of elements
1466 * in the two lists. The comparison is then expressed in a map from
1467 * this space to itself and wrapped into a set. Finally the two lists
1468 * of piecewise affine expressions are plugged into this set.
1470 * Let S be the space of "set" and T the constructed space.
1471 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1472 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1473 * while the comparison is first expressed in T -> T, then [T -> T]
1476 static __isl_give isl_set
*list_cmp(__isl_keep isl_set
*set
, int type
,
1477 __isl_take isl_pw_aff_list
*left
, __isl_take isl_pw_aff_list
*right
)
1481 isl_multi_pw_aff
*mpa1
, *mpa2
;
1483 n
= isl_pw_aff_list_n_pw_aff(left
);
1484 if (!set
|| n
< 0 || !right
)
1487 space
= isl_set_get_space(set
);
1488 space
= isl_space_from_domain(space
);
1489 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
1490 mpa1
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), left
);
1491 mpa2
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), right
);
1492 mpa1
= isl_multi_pw_aff_range_product(mpa1
, mpa2
);
1494 space
= isl_space_range(space
);
1496 case ISL_TOKEN_LEX_LT
:
1497 set
= isl_map_wrap(isl_map_lex_lt(space
));
1499 case ISL_TOKEN_LEX_GT
:
1500 set
= isl_map_wrap(isl_map_lex_gt(space
));
1502 case ISL_TOKEN_LEX_LE
:
1503 set
= isl_map_wrap(isl_map_lex_le(space
));
1505 case ISL_TOKEN_LEX_GE
:
1506 set
= isl_map_wrap(isl_map_lex_ge(space
));
1509 isl_multi_pw_aff_free(mpa1
);
1510 isl_space_free(space
);
1511 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
1512 "unhandled list comparison type", return NULL
);
1514 set
= isl_set_preimage_multi_pw_aff(set
, mpa1
);
1517 isl_pw_aff_list_free(left
);
1518 isl_pw_aff_list_free(right
);
1522 /* Construct constraints of the form
1526 * where a is an element in "left", op is an operator of type "type" and
1527 * b is an element in "right", add the constraints to "set" and return
1529 * "rational" is set if the constraints should be treated as
1530 * a rational constraints.
1532 * If "type" is the type of a comparison operator between lists
1533 * of affine expressions, then a single (compound) constraint
1534 * is constructed by list_cmp instead.
1536 static __isl_give isl_set
*construct_constraints(
1537 __isl_take isl_set
*set
, int type
,
1538 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1543 left
= isl_pw_aff_list_copy(left
);
1544 right
= isl_pw_aff_list_copy(right
);
1546 left
= isl_pw_aff_list_set_rational(left
);
1547 right
= isl_pw_aff_list_set_rational(right
);
1549 if (is_list_comparator_type(type
))
1550 cond
= list_cmp(set
, type
, left
, right
);
1551 else if (type
== ISL_TOKEN_LE
)
1552 cond
= isl_pw_aff_list_le_set(left
, right
);
1553 else if (type
== ISL_TOKEN_GE
)
1554 cond
= isl_pw_aff_list_ge_set(left
, right
);
1555 else if (type
== ISL_TOKEN_LT
)
1556 cond
= isl_pw_aff_list_lt_set(left
, right
);
1557 else if (type
== ISL_TOKEN_GT
)
1558 cond
= isl_pw_aff_list_gt_set(left
, right
);
1559 else if (type
== ISL_TOKEN_NE
)
1560 cond
= isl_pw_aff_list_ne_set(left
, right
);
1562 cond
= isl_pw_aff_list_eq_set(left
, right
);
1564 return isl_set_intersect(set
, cond
);
1567 /* Read a constraint from "s", add it to "map" and return the result.
1568 * "v" contains a description of the identifiers parsed so far.
1569 * "rational" is set if the constraint should be treated as
1570 * a rational constraint.
1571 * The constraint read from "s" may be applied to multiple pairs
1572 * of affine expressions and may be chained.
1573 * In particular, a list of affine expressions is read, followed
1574 * by a comparison operator and another list of affine expressions.
1575 * The comparison operator is then applied to each pair of elements
1576 * in the two lists and the results are added to "map".
1577 * However, if the operator expects two lists of affine expressions,
1578 * then it is applied directly to those lists and the two lists
1579 * are required to have the same length.
1580 * If the next token is another comparison operator, then another
1581 * list of affine expressions is read and the process repeats.
1583 * The processing is performed on a wrapped copy of "map" because
1584 * an affine expression cannot have a binary relation as domain.
1586 static __isl_give isl_map
*add_constraint(__isl_keep isl_stream
*s
,
1587 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1589 struct isl_token
*tok
;
1591 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1595 set
= isl_map_wrap(map
);
1596 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1599 tok
= isl_stream_next_token(s
);
1600 if (!is_comparator(tok
)) {
1601 isl_stream_error(s
, tok
, "missing operator");
1603 isl_stream_push_token(s
, tok
);
1607 isl_token_free(tok
);
1609 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1610 n1
= isl_pw_aff_list_n_pw_aff(list1
);
1611 n2
= isl_pw_aff_list_n_pw_aff(list2
);
1612 if (n1
< 0 || n2
< 0)
1614 if (is_list_comparator_type(type
) && n1
!= n2
) {
1615 isl_stream_error(s
, NULL
,
1616 "list arguments not of same size");
1620 set
= construct_constraints(set
, type
, list1
, list2
, rational
);
1621 isl_pw_aff_list_free(list1
);
1624 tok
= isl_stream_next_token(s
);
1625 if (!is_comparator(tok
)) {
1627 isl_stream_push_token(s
, tok
);
1631 isl_token_free(tok
);
1633 isl_pw_aff_list_free(list1
);
1635 return isl_set_unwrap(set
);
1637 isl_pw_aff_list_free(list1
);
1638 isl_pw_aff_list_free(list2
);
1643 static __isl_give isl_map
*read_exists(__isl_keep isl_stream
*s
,
1644 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1647 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1649 map
= isl_map_from_domain(isl_map_wrap(map
));
1650 map
= read_defined_var_list(s
, v
, map
, rational
);
1652 if (isl_stream_eat(s
, ':'))
1655 map
= read_formula(s
, v
, map
, rational
);
1656 map
= isl_set_unwrap(isl_map_domain(map
));
1658 vars_drop(v
, v
->n
- n
);
1659 if (seen_paren
&& isl_stream_eat(s
, ')'))
1668 /* Parse an expression between parentheses and push the result
1669 * back on the stream.
1671 * The parsed expression may be either an affine expression
1672 * or a condition. The first type is pushed onto the stream
1673 * as an isl_pw_aff, while the second is pushed as an isl_map.
1675 * If the initial token indicates the start of a condition,
1676 * we parse it as such.
1677 * Otherwise, we first parse an affine expression and push
1678 * that onto the stream. If the affine expression covers the
1679 * entire expression between parentheses, we return.
1680 * Otherwise, we assume that the affine expression is the
1681 * start of a condition and continue parsing.
1683 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
1684 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1686 struct isl_token
*tok
, *tok2
;
1690 tok
= isl_stream_next_token(s
);
1691 if (!tok
|| tok
->type
!= '(')
1694 if (isl_stream_next_token_is(s
, '('))
1695 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1698 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1699 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1700 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1701 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1702 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1703 map
= read_formula(s
, v
, map
, rational
);
1704 if (isl_stream_eat(s
, ')'))
1706 tok
->type
= ISL_TOKEN_MAP
;
1708 isl_stream_push_token(s
, tok
);
1712 tok2
= isl_stream_next_token(s
);
1717 isl_stream_push_token(s
, tok2
);
1719 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1723 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1726 tok2
->type
= ISL_TOKEN_AFF
;
1727 tok2
->u
.pwaff
= pwaff
;
1729 if (isl_stream_eat_if_available(s
, ')')) {
1730 isl_stream_push_token(s
, tok2
);
1731 isl_token_free(tok
);
1736 isl_stream_push_token(s
, tok2
);
1738 map
= read_formula(s
, v
, map
, rational
);
1739 if (isl_stream_eat(s
, ')'))
1742 tok
->type
= ISL_TOKEN_MAP
;
1744 isl_stream_push_token(s
, tok
);
1748 isl_pw_aff_free(pwaff
);
1750 isl_token_free(tok
);
1755 static __isl_give isl_map
*read_conjunct(__isl_keep isl_stream
*s
,
1756 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1758 if (isl_stream_next_token_is(s
, '('))
1759 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1762 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1763 struct isl_token
*tok
;
1764 tok
= isl_stream_next_token(s
);
1768 map
= isl_map_copy(tok
->u
.map
);
1769 isl_token_free(tok
);
1773 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1774 return read_exists(s
, v
, map
, rational
);
1776 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1779 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1780 isl_space
*dim
= isl_map_get_space(map
);
1782 return isl_map_empty(dim
);
1785 return add_constraint(s
, v
, map
, rational
);
1791 static __isl_give isl_map
*read_conjuncts(__isl_keep isl_stream
*s
,
1792 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1797 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1798 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1800 res
= isl_map_subtract(isl_map_copy(map
), res
);
1802 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1805 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1806 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
1808 res
= isl_map_subtract(res
, res_i
);
1810 res
= isl_map_intersect(res
, res_i
);
1817 static struct isl_map
*read_disjuncts(__isl_keep isl_stream
*s
,
1818 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1822 if (isl_stream_next_token_is(s
, '}'))
1825 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1826 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1829 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
1830 res
= isl_map_union(res
, res_i
);
1837 /* Read a first order formula from "s", add the corresponding
1838 * constraints to "map" and return the result.
1840 * In particular, read a formula of the form
1848 * where a and b are disjunctions.
1850 * In the first case, map is replaced by
1852 * map \cap { [..] : a }
1854 * In the second case, it is replaced by
1856 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1858 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
1859 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1863 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
1865 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
1868 res
= isl_map_subtract(isl_map_copy(map
), res
);
1869 res2
= read_disjuncts(s
, v
, map
, rational
);
1870 res
= isl_map_union(res
, res2
);
1877 static isl_size
polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1879 isl_size n_out
, n_in
, n_param
, n_div
;
1881 n_param
= isl_basic_map_dim(bmap
, isl_dim_param
);
1882 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
1883 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
1884 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1885 if (n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1886 return isl_size_error
;
1889 return 1 + n_param
+ n_in
+ pos
;
1893 return 1 + n_param
+ pos
;
1897 return 1 + n_param
+ n_in
+ n_out
+ pos
;
1906 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1907 __isl_keep isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1910 struct isl_token
*tok
;
1919 tok
= isl_stream_next_token(s
);
1920 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1921 isl_stream_error(s
, tok
, "expecting coefficient");
1923 isl_stream_push_token(s
, tok
);
1926 if (!tok
->on_new_line
) {
1927 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1928 isl_stream_push_token(s
, tok
);
1932 type
= isl_int_get_si(tok
->u
.v
);
1933 isl_token_free(tok
);
1935 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1937 k
= isl_basic_map_alloc_equality(bmap
);
1940 k
= isl_basic_map_alloc_inequality(bmap
);
1946 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1948 return isl_basic_map_free(bmap
);
1949 for (j
= 0; j
< 1 + total
; ++j
) {
1951 tok
= isl_stream_next_token(s
);
1952 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1953 isl_stream_error(s
, tok
, "expecting coefficient");
1955 isl_stream_push_token(s
, tok
);
1958 if (tok
->on_new_line
) {
1959 isl_stream_error(s
, tok
,
1960 "coefficient should not appear on new line");
1961 isl_stream_push_token(s
, tok
);
1964 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1966 isl_int_set(c
[pos
], tok
->u
.v
);
1967 isl_token_free(tok
);
1969 return isl_basic_map_free(bmap
);
1974 isl_basic_map_free(bmap
);
1978 static __isl_give isl_basic_map
*basic_map_read_polylib(
1979 __isl_keep isl_stream
*s
)
1982 struct isl_token
*tok
;
1983 struct isl_token
*tok2
;
1986 unsigned in
= 0, out
, local
= 0;
1987 struct isl_basic_map
*bmap
= NULL
;
1990 tok
= isl_stream_next_token(s
);
1992 isl_stream_error(s
, NULL
, "unexpected EOF");
1995 tok2
= isl_stream_next_token(s
);
1997 isl_token_free(tok
);
1998 isl_stream_error(s
, NULL
, "unexpected EOF");
2001 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2002 isl_stream_push_token(s
, tok2
);
2003 isl_stream_push_token(s
, tok
);
2004 isl_stream_error(s
, NULL
,
2005 "expecting constraint matrix dimensions");
2008 n_row
= isl_int_get_si(tok
->u
.v
);
2009 n_col
= isl_int_get_si(tok2
->u
.v
);
2010 on_new_line
= tok2
->on_new_line
;
2011 isl_token_free(tok2
);
2012 isl_token_free(tok
);
2013 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
2014 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
2015 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
2016 tok
= isl_stream_next_token_on_same_line(s
);
2018 if (tok
->type
!= ISL_TOKEN_VALUE
) {
2019 isl_stream_error(s
, tok
,
2020 "expecting number of output dimensions");
2021 isl_stream_push_token(s
, tok
);
2024 out
= isl_int_get_si(tok
->u
.v
);
2025 isl_token_free(tok
);
2027 tok
= isl_stream_next_token_on_same_line(s
);
2028 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2029 isl_stream_error(s
, tok
,
2030 "expecting number of input dimensions");
2032 isl_stream_push_token(s
, tok
);
2035 in
= isl_int_get_si(tok
->u
.v
);
2036 isl_token_free(tok
);
2038 tok
= isl_stream_next_token_on_same_line(s
);
2039 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2040 isl_stream_error(s
, tok
,
2041 "expecting number of existentials");
2043 isl_stream_push_token(s
, tok
);
2046 local
= isl_int_get_si(tok
->u
.v
);
2047 isl_token_free(tok
);
2049 tok
= isl_stream_next_token_on_same_line(s
);
2050 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2051 isl_stream_error(s
, tok
,
2052 "expecting number of parameters");
2054 isl_stream_push_token(s
, tok
);
2057 nparam
= isl_int_get_si(tok
->u
.v
);
2058 isl_token_free(tok
);
2059 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
2060 isl_stream_error(s
, NULL
,
2061 "dimensions don't match");
2065 out
= n_col
- 2 - nparam
;
2066 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
2070 for (i
= 0; i
< local
; ++i
) {
2071 int k
= isl_basic_map_alloc_div(bmap
);
2074 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
2077 for (i
= 0; i
< n_row
; ++i
)
2078 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
2080 tok
= isl_stream_next_token_on_same_line(s
);
2082 isl_stream_error(s
, tok
, "unexpected extra token on line");
2083 isl_stream_push_token(s
, tok
);
2087 bmap
= isl_basic_map_simplify(bmap
);
2088 bmap
= isl_basic_map_finalize(bmap
);
2091 isl_basic_map_free(bmap
);
2095 static struct isl_map
*map_read_polylib(__isl_keep isl_stream
*s
)
2097 struct isl_token
*tok
;
2098 struct isl_token
*tok2
;
2100 struct isl_map
*map
;
2102 tok
= isl_stream_next_token(s
);
2104 isl_stream_error(s
, NULL
, "unexpected EOF");
2107 tok2
= isl_stream_next_token_on_same_line(s
);
2108 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
2109 isl_stream_push_token(s
, tok2
);
2110 isl_stream_push_token(s
, tok
);
2111 return isl_map_from_basic_map(basic_map_read_polylib(s
));
2114 isl_stream_error(s
, tok2
, "unexpected token");
2115 isl_stream_push_token(s
, tok2
);
2116 isl_stream_push_token(s
, tok
);
2119 n
= isl_int_get_si(tok
->u
.v
);
2120 isl_token_free(tok
);
2122 isl_assert(s
->ctx
, n
>= 1, return NULL
);
2124 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
2126 for (i
= 1; map
&& i
< n
; ++i
)
2127 map
= isl_map_union(map
,
2128 isl_map_from_basic_map(basic_map_read_polylib(s
)));
2133 static int optional_power(__isl_keep isl_stream
*s
)
2136 struct isl_token
*tok
;
2138 tok
= isl_stream_next_token(s
);
2141 if (tok
->type
!= '^') {
2142 isl_stream_push_token(s
, tok
);
2145 isl_token_free(tok
);
2146 tok
= isl_stream_next_token(s
);
2147 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2148 isl_stream_error(s
, tok
, "expecting exponent");
2150 isl_stream_push_token(s
, tok
);
2153 pow
= isl_int_get_si(tok
->u
.v
);
2154 isl_token_free(tok
);
2158 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2159 __isl_keep isl_map
*map
, struct vars
*v
);
2161 static __isl_give isl_pw_qpolynomial
*read_factor(__isl_keep isl_stream
*s
,
2162 __isl_keep isl_map
*map
, struct vars
*v
)
2164 isl_pw_qpolynomial
*pwqp
;
2165 struct isl_token
*tok
;
2167 tok
= next_token(s
);
2169 isl_stream_error(s
, NULL
, "unexpected EOF");
2172 if (tok
->type
== '(') {
2175 isl_token_free(tok
);
2176 pwqp
= read_term(s
, map
, v
);
2179 if (isl_stream_eat(s
, ')'))
2181 pow
= optional_power(s
);
2182 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2183 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
2184 struct isl_token
*tok2
;
2185 isl_qpolynomial
*qp
;
2187 tok2
= isl_stream_next_token(s
);
2188 if (tok2
&& tok2
->type
== '/') {
2189 isl_token_free(tok2
);
2190 tok2
= next_token(s
);
2191 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2192 isl_stream_error(s
, tok2
, "expected denominator");
2193 isl_token_free(tok
);
2194 isl_token_free(tok2
);
2197 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
2198 tok
->u
.v
, tok2
->u
.v
);
2199 isl_token_free(tok2
);
2201 isl_stream_push_token(s
, tok2
);
2202 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
2205 isl_token_free(tok
);
2206 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2207 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2208 isl_qpolynomial
*qp
;
2209 isl_token_free(tok
);
2210 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2211 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2212 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2213 isl_qpolynomial
*qp
;
2214 isl_token_free(tok
);
2215 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2216 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2217 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2219 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2221 isl_qpolynomial
*qp
;
2223 isl_token_free(tok
);
2227 vars_drop(v
, v
->n
- n
);
2228 isl_stream_error(s
, tok
, "unknown identifier");
2229 isl_token_free(tok
);
2232 isl_token_free(tok
);
2233 pow
= optional_power(s
);
2234 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2235 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2236 } else if (is_start_of_div(tok
)) {
2240 isl_stream_push_token(s
, tok
);
2241 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2242 pow
= optional_power(s
);
2243 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2244 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2245 } else if (tok
->type
== '-') {
2246 isl_token_free(tok
);
2247 pwqp
= read_factor(s
, map
, v
);
2248 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2250 isl_stream_error(s
, tok
, "unexpected isl_token");
2251 isl_stream_push_token(s
, tok
);
2255 if (isl_stream_eat_if_available(s
, '*') ||
2256 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2257 isl_pw_qpolynomial
*pwqp2
;
2259 pwqp2
= read_factor(s
, map
, v
);
2260 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2265 isl_pw_qpolynomial_free(pwqp
);
2269 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2270 __isl_keep isl_map
*map
, struct vars
*v
)
2272 struct isl_token
*tok
;
2273 isl_pw_qpolynomial
*pwqp
;
2275 pwqp
= read_factor(s
, map
, v
);
2278 tok
= next_token(s
);
2282 if (tok
->type
== '+') {
2283 isl_pw_qpolynomial
*pwqp2
;
2285 isl_token_free(tok
);
2286 pwqp2
= read_factor(s
, map
, v
);
2287 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2288 } else if (tok
->type
== '-') {
2289 isl_pw_qpolynomial
*pwqp2
;
2291 isl_token_free(tok
);
2292 pwqp2
= read_factor(s
, map
, v
);
2293 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2294 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2295 isl_int_is_neg(tok
->u
.v
)) {
2296 isl_pw_qpolynomial
*pwqp2
;
2298 isl_stream_push_token(s
, tok
);
2299 pwqp2
= read_factor(s
, map
, v
);
2300 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2302 isl_stream_push_token(s
, tok
);
2310 static __isl_give isl_map
*read_optional_formula(__isl_keep isl_stream
*s
,
2311 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2313 struct isl_token
*tok
;
2315 tok
= isl_stream_next_token(s
);
2317 isl_stream_error(s
, NULL
, "unexpected EOF");
2320 if (tok
->type
== ':' ||
2321 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2322 isl_token_free(tok
);
2323 map
= read_formula(s
, v
, map
, rational
);
2325 isl_stream_push_token(s
, tok
);
2333 static struct isl_obj
obj_read_poly(__isl_keep isl_stream
*s
,
2334 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2336 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2337 isl_pw_qpolynomial
*pwqp
;
2338 struct isl_set
*set
;
2340 pwqp
= read_term(s
, map
, v
);
2341 map
= read_optional_formula(s
, map
, v
, 0);
2342 set
= isl_map_range(map
);
2344 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2346 vars_drop(v
, v
->n
- n
);
2352 static struct isl_obj
obj_read_poly_or_fold(__isl_keep isl_stream
*s
,
2353 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2355 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2356 isl_pw_qpolynomial
*pwqp
;
2357 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2359 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
2360 return obj_read_poly(s
, set
, v
, n
);
2362 if (isl_stream_eat(s
, '('))
2365 pwqp
= read_term(s
, set
, v
);
2366 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
2368 while (isl_stream_eat_if_available(s
, ',')) {
2369 isl_pw_qpolynomial_fold
*pwf_i
;
2370 pwqp
= read_term(s
, set
, v
);
2371 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
2373 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2376 if (isl_stream_eat(s
, ')'))
2379 set
= read_optional_formula(s
, set
, v
, 0);
2380 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2382 vars_drop(v
, v
->n
- n
);
2388 isl_pw_qpolynomial_fold_free(pwf
);
2389 obj
.type
= isl_obj_none
;
2393 static int is_rational(__isl_keep isl_stream
*s
)
2395 struct isl_token
*tok
;
2397 tok
= isl_stream_next_token(s
);
2400 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2401 isl_token_free(tok
);
2402 isl_stream_eat(s
, ':');
2406 isl_stream_push_token(s
, tok
);
2411 static struct isl_obj
obj_read_body(__isl_keep isl_stream
*s
,
2412 __isl_take isl_map
*map
, struct vars
*v
)
2414 struct isl_token
*tok
;
2415 struct isl_obj obj
= { isl_obj_set
, NULL
};
2419 rational
= is_rational(s
);
2421 map
= isl_map_set_rational(map
);
2423 if (isl_stream_next_token_is(s
, ':')) {
2424 obj
.type
= isl_obj_set
;
2425 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2429 if (!next_is_tuple(s
))
2430 return obj_read_poly_or_fold(s
, map
, v
, n
);
2432 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2435 tok
= isl_stream_next_token(s
);
2438 if (tok
->type
== ISL_TOKEN_TO
) {
2439 obj
.type
= isl_obj_map
;
2440 isl_token_free(tok
);
2441 if (!next_is_tuple(s
)) {
2442 isl_set
*set
= isl_map_domain(map
);
2443 return obj_read_poly_or_fold(s
, set
, v
, n
);
2445 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2449 map
= isl_map_domain(map
);
2450 isl_stream_push_token(s
, tok
);
2453 map
= read_optional_formula(s
, map
, v
, rational
);
2455 vars_drop(v
, v
->n
- n
);
2461 obj
.type
= isl_obj_none
;
2465 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2467 if (obj
.type
== isl_obj_map
) {
2468 obj
.v
= isl_union_map_from_map(obj
.v
);
2469 obj
.type
= isl_obj_union_map
;
2470 } else if (obj
.type
== isl_obj_set
) {
2471 obj
.v
= isl_union_set_from_set(obj
.v
);
2472 obj
.type
= isl_obj_union_set
;
2473 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2474 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2475 obj
.type
= isl_obj_union_pw_qpolynomial
;
2476 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2477 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2478 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2480 isl_assert(ctx
, 0, goto error
);
2483 obj
.type
->free(obj
.v
);
2484 obj
.type
= isl_obj_none
;
2488 static struct isl_obj
obj_add(__isl_keep isl_stream
*s
,
2489 struct isl_obj obj1
, struct isl_obj obj2
)
2491 if (obj2
.type
== isl_obj_none
|| !obj2
.v
)
2493 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2494 obj1
= to_union(s
->ctx
, obj1
);
2495 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2496 obj2
= to_union(s
->ctx
, obj2
);
2497 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2498 obj1
= to_union(s
->ctx
, obj1
);
2499 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2500 obj2
= to_union(s
->ctx
, obj2
);
2501 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2502 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2503 obj1
= to_union(s
->ctx
, obj1
);
2504 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2505 obj2
.type
== isl_obj_pw_qpolynomial
)
2506 obj2
= to_union(s
->ctx
, obj2
);
2507 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2508 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2509 obj1
= to_union(s
->ctx
, obj1
);
2510 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2511 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2512 obj2
= to_union(s
->ctx
, obj2
);
2513 if (obj1
.type
!= obj2
.type
) {
2514 isl_stream_error(s
, NULL
,
2515 "attempt to combine incompatible objects");
2518 if (!obj1
.type
->add
)
2519 isl_die(s
->ctx
, isl_error_internal
,
2520 "combination not supported on object type", goto error
);
2521 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2522 obj1
= to_union(s
->ctx
, obj1
);
2523 obj2
= to_union(s
->ctx
, obj2
);
2525 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2526 obj1
= to_union(s
->ctx
, obj1
);
2527 obj2
= to_union(s
->ctx
, obj2
);
2529 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2530 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2531 obj1
= to_union(s
->ctx
, obj1
);
2532 obj2
= to_union(s
->ctx
, obj2
);
2534 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2535 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2536 obj1
= to_union(s
->ctx
, obj1
);
2537 obj2
= to_union(s
->ctx
, obj2
);
2539 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2542 obj1
.type
->free(obj1
.v
);
2543 obj2
.type
->free(obj2
.v
);
2544 obj1
.type
= isl_obj_none
;
2549 /* Are the first two tokens on "s", "domain" (either as a string
2550 * or as an identifier) followed by ":"?
2552 static int next_is_domain_colon(__isl_keep isl_stream
*s
)
2554 struct isl_token
*tok
;
2558 tok
= isl_stream_next_token(s
);
2561 if (tok
->type
!= ISL_TOKEN_IDENT
&& tok
->type
!= ISL_TOKEN_STRING
) {
2562 isl_stream_push_token(s
, tok
);
2566 name
= isl_token_get_str(s
->ctx
, tok
);
2567 res
= !strcmp(name
, "domain") && isl_stream_next_token_is(s
, ':');
2570 isl_stream_push_token(s
, tok
);
2575 /* Do the first tokens on "s" look like a schedule?
2577 * The root of a schedule is always a domain node, so the first thing
2578 * we expect in the stream is a domain key, i.e., "domain" followed
2579 * by ":". If the schedule was printed in YAML flow style, then
2580 * we additionally expect a "{" to open the outer mapping.
2582 static int next_is_schedule(__isl_keep isl_stream
*s
)
2584 struct isl_token
*tok
;
2587 tok
= isl_stream_next_token(s
);
2590 if (tok
->type
!= '{') {
2591 isl_stream_push_token(s
, tok
);
2592 return next_is_domain_colon(s
);
2595 is_schedule
= next_is_domain_colon(s
);
2596 isl_stream_push_token(s
, tok
);
2601 /* Read an isl_schedule from "s" and store it in an isl_obj.
2603 static struct isl_obj
schedule_read(__isl_keep isl_stream
*s
)
2607 obj
.type
= isl_obj_schedule
;
2608 obj
.v
= isl_stream_read_schedule(s
);
2613 /* Read a disjunction of object bodies from "s".
2614 * That is, read the inside of the braces, but not the braces themselves.
2615 * "v" contains a description of the identifiers parsed so far.
2616 * "map" contains information about the parameters.
2618 static struct isl_obj
obj_read_disjuncts(__isl_keep isl_stream
*s
,
2619 struct vars
*v
, __isl_keep isl_map
*map
)
2621 struct isl_obj obj
= { isl_obj_set
, NULL
};
2623 if (isl_stream_next_token_is(s
, '}')) {
2624 obj
.type
= isl_obj_union_set
;
2625 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2631 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2635 obj
= obj_add(s
, obj
, o
);
2636 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2638 if (!isl_stream_eat_if_available(s
, ';'))
2640 if (isl_stream_next_token_is(s
, '}'))
2647 static struct isl_obj
obj_read(__isl_keep isl_stream
*s
)
2649 isl_map
*map
= NULL
;
2650 struct isl_token
*tok
;
2651 struct vars
*v
= NULL
;
2652 struct isl_obj obj
= { isl_obj_set
, NULL
};
2654 if (next_is_schedule(s
))
2655 return schedule_read(s
);
2657 tok
= next_token(s
);
2659 isl_stream_error(s
, NULL
, "unexpected EOF");
2662 if (tok
->type
== ISL_TOKEN_VALUE
) {
2663 struct isl_token
*tok2
;
2664 struct isl_map
*map
;
2666 tok2
= isl_stream_next_token(s
);
2667 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2668 isl_int_is_neg(tok2
->u
.v
)) {
2670 isl_stream_push_token(s
, tok2
);
2671 obj
.type
= isl_obj_val
;
2672 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2673 isl_token_free(tok
);
2676 isl_stream_push_token(s
, tok2
);
2677 isl_stream_push_token(s
, tok
);
2678 map
= map_read_polylib(s
);
2681 if (isl_map_may_be_set(map
))
2682 obj
.v
= isl_map_range(map
);
2684 obj
.type
= isl_obj_map
;
2689 v
= vars_new(s
->ctx
);
2691 isl_stream_push_token(s
, tok
);
2694 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2695 if (tok
->type
== '[') {
2696 isl_stream_push_token(s
, tok
);
2697 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2700 tok
= isl_stream_next_token(s
);
2701 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2702 isl_stream_error(s
, tok
, "expecting '->'");
2704 isl_stream_push_token(s
, tok
);
2707 isl_token_free(tok
);
2708 tok
= isl_stream_next_token(s
);
2710 if (!tok
|| tok
->type
!= '{') {
2711 isl_stream_error(s
, tok
, "expecting '{'");
2713 isl_stream_push_token(s
, tok
);
2716 isl_token_free(tok
);
2718 tok
= isl_stream_next_token(s
);
2721 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2722 isl_token_free(tok
);
2723 if (isl_stream_eat(s
, '='))
2725 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2729 isl_stream_push_token(s
, tok
);
2731 obj
= obj_read_disjuncts(s
, v
, map
);
2732 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2735 tok
= isl_stream_next_token(s
);
2736 if (tok
&& tok
->type
== '}') {
2737 isl_token_free(tok
);
2739 isl_stream_error(s
, tok
, "unexpected isl_token");
2741 isl_token_free(tok
);
2751 obj
.type
->free(obj
.v
);
2758 struct isl_obj
isl_stream_read_obj(__isl_keep isl_stream
*s
)
2763 __isl_give isl_map
*isl_stream_read_map(__isl_keep isl_stream
*s
)
2769 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2770 obj
.type
== isl_obj_set
, goto error
);
2772 if (obj
.type
== isl_obj_set
)
2773 obj
.v
= isl_map_from_range(obj
.v
);
2777 obj
.type
->free(obj
.v
);
2781 __isl_give isl_set
*isl_stream_read_set(__isl_keep isl_stream
*s
)
2787 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2788 obj
.v
= isl_map_range(obj
.v
);
2789 obj
.type
= isl_obj_set
;
2791 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2796 obj
.type
->free(obj
.v
);
2800 __isl_give isl_union_map
*isl_stream_read_union_map(__isl_keep isl_stream
*s
)
2805 if (obj
.type
== isl_obj_map
) {
2806 obj
.type
= isl_obj_union_map
;
2807 obj
.v
= isl_union_map_from_map(obj
.v
);
2809 if (obj
.type
== isl_obj_set
) {
2810 obj
.type
= isl_obj_union_set
;
2811 obj
.v
= isl_union_set_from_set(obj
.v
);
2813 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
2814 isl_union_set_is_empty(obj
.v
))
2815 obj
.type
= isl_obj_union_map
;
2816 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
2817 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
2821 obj
.type
->free(obj
.v
);
2825 /* Extract an isl_union_set from "obj".
2826 * This only works if the object was detected as either a set
2827 * (in which case it is converted to a union set) or a union set.
2829 static __isl_give isl_union_set
*extract_union_set(isl_ctx
*ctx
,
2832 if (obj
.type
== isl_obj_set
) {
2833 obj
.type
= isl_obj_union_set
;
2834 obj
.v
= isl_union_set_from_set(obj
.v
);
2837 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2841 obj
.type
->free(obj
.v
);
2845 /* Read an isl_union_set from "s".
2846 * First read a generic object and then try and extract
2847 * an isl_union_set from that.
2849 __isl_give isl_union_set
*isl_stream_read_union_set(__isl_keep isl_stream
*s
)
2854 return extract_union_set(s
->ctx
, obj
);
2857 static __isl_give isl_basic_map
*basic_map_read(__isl_keep isl_stream
*s
)
2860 struct isl_map
*map
;
2861 struct isl_basic_map
*bmap
;
2864 if (obj
.v
&& (obj
.type
!= isl_obj_map
&& obj
.type
!= isl_obj_set
))
2865 isl_die(s
->ctx
, isl_error_invalid
, "not a (basic) set or map",
2872 isl_die(s
->ctx
, isl_error_invalid
,
2873 "set or map description involves "
2874 "more than one disjunct", goto error
);
2877 bmap
= isl_basic_map_empty(isl_map_get_space(map
));
2879 bmap
= isl_basic_map_copy(map
->p
[0]);
2885 obj
.type
->free(obj
.v
);
2889 static __isl_give isl_basic_set
*basic_set_read(__isl_keep isl_stream
*s
)
2891 isl_basic_map
*bmap
;
2892 bmap
= basic_map_read(s
);
2895 if (!isl_basic_map_may_be_set(bmap
))
2896 isl_die(s
->ctx
, isl_error_invalid
,
2897 "input is not a set", goto error
);
2898 return isl_basic_map_range(bmap
);
2900 isl_basic_map_free(bmap
);
2904 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2907 struct isl_basic_map
*bmap
;
2908 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2911 bmap
= basic_map_read(s
);
2916 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2919 isl_basic_set
*bset
;
2920 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2923 bset
= basic_set_read(s
);
2928 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2931 struct isl_basic_map
*bmap
;
2932 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2935 bmap
= basic_map_read(s
);
2940 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2943 isl_basic_set
*bset
;
2944 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2947 bset
= basic_set_read(s
);
2952 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2955 struct isl_map
*map
;
2956 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2959 map
= isl_stream_read_map(s
);
2964 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2967 struct isl_map
*map
;
2968 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2971 map
= isl_stream_read_map(s
);
2976 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2980 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2983 set
= isl_stream_read_set(s
);
2988 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2992 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2995 set
= isl_stream_read_set(s
);
3000 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
3003 isl_union_map
*umap
;
3004 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3007 umap
= isl_stream_read_union_map(s
);
3012 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
3015 isl_union_map
*umap
;
3016 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3019 umap
= isl_stream_read_union_map(s
);
3024 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
3027 isl_union_set
*uset
;
3028 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3031 uset
= isl_stream_read_union_set(s
);
3036 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
3039 isl_union_set
*uset
;
3040 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3043 uset
= isl_stream_read_union_set(s
);
3048 static __isl_give isl_vec
*isl_vec_read_polylib(__isl_keep isl_stream
*s
)
3050 struct isl_vec
*vec
= NULL
;
3051 struct isl_token
*tok
;
3055 tok
= isl_stream_next_token(s
);
3056 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3057 isl_stream_error(s
, tok
, "expecting vector length");
3061 size
= isl_int_get_si(tok
->u
.v
);
3062 isl_token_free(tok
);
3064 vec
= isl_vec_alloc(s
->ctx
, size
);
3066 for (j
= 0; j
< size
; ++j
) {
3067 tok
= isl_stream_next_token(s
);
3068 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3069 isl_stream_error(s
, tok
, "expecting constant value");
3072 isl_int_set(vec
->el
[j
], tok
->u
.v
);
3073 isl_token_free(tok
);
3078 isl_token_free(tok
);
3083 static __isl_give isl_vec
*vec_read(__isl_keep isl_stream
*s
)
3085 return isl_vec_read_polylib(s
);
3088 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
3091 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3099 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
3100 __isl_keep isl_stream
*s
)
3106 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
3111 obj
.type
->free(obj
.v
);
3115 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
3118 isl_pw_qpolynomial
*pwqp
;
3119 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3122 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3127 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
3130 isl_pw_qpolynomial
*pwqp
;
3131 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3134 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3139 /* Is the next token an identifer not in "v"?
3141 static int next_is_fresh_ident(__isl_keep isl_stream
*s
, struct vars
*v
)
3145 struct isl_token
*tok
;
3147 tok
= isl_stream_next_token(s
);
3150 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
3151 isl_stream_push_token(s
, tok
);
3153 vars_drop(v
, v
->n
- n
);
3158 /* First read the domain of the affine expression, which may be
3159 * a parameter space or a set.
3160 * The tricky part is that we don't know if the domain is a set or not,
3161 * so when we are trying to read the domain, we may actually be reading
3162 * the affine expression itself (defined on a parameter domains)
3163 * If the tuple we are reading is named, we assume it's the domain.
3164 * Also, if inside the tuple, the first thing we find is a nested tuple
3165 * or a new identifier, we again assume it's the domain.
3166 * Finally, if the tuple is empty, then it must be the domain
3167 * since it does not contain an affine expression.
3168 * Otherwise, we assume we are reading an affine expression.
3170 static __isl_give isl_set
*read_aff_domain(__isl_keep isl_stream
*s
,
3171 __isl_take isl_set
*dom
, struct vars
*v
)
3173 struct isl_token
*tok
, *tok2
;
3176 tok
= isl_stream_next_token(s
);
3177 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
3178 isl_stream_push_token(s
, tok
);
3179 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3181 if (!tok
|| tok
->type
!= '[') {
3182 isl_stream_error(s
, tok
, "expecting '['");
3185 tok2
= isl_stream_next_token(s
);
3186 is_empty
= tok2
&& tok2
->type
== ']';
3188 isl_stream_push_token(s
, tok2
);
3189 if (is_empty
|| next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
3190 isl_stream_push_token(s
, tok
);
3191 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3193 isl_stream_push_token(s
, tok
);
3198 isl_stream_push_token(s
, tok
);
3203 /* Read an affine expression from "s".
3205 __isl_give isl_aff
*isl_stream_read_aff(__isl_keep isl_stream
*s
)
3211 ma
= isl_stream_read_multi_aff(s
);
3212 dim
= isl_multi_aff_dim(ma
, isl_dim_out
);
3216 isl_die(s
->ctx
, isl_error_invalid
,
3217 "expecting single affine expression",
3220 aff
= isl_multi_aff_get_aff(ma
, 0);
3221 isl_multi_aff_free(ma
);
3224 isl_multi_aff_free(ma
);
3228 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3230 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(__isl_keep isl_stream
*s
,
3231 __isl_take isl_set
*dom
, struct vars
*v
)
3233 isl_pw_aff
*pwaff
= NULL
;
3235 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
3238 if (isl_stream_eat(s
, '['))
3241 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
3243 if (isl_stream_eat(s
, ']'))
3246 dom
= read_optional_formula(s
, dom
, v
, 0);
3247 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3252 isl_pw_aff_free(pwaff
);
3256 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(__isl_keep isl_stream
*s
)
3259 isl_set
*dom
= NULL
;
3261 isl_pw_aff
*pa
= NULL
;
3264 v
= vars_new(s
->ctx
);
3268 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3269 if (next_is_tuple(s
)) {
3270 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3271 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3274 if (isl_stream_eat(s
, '{'))
3278 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3279 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3280 vars_drop(v
, v
->n
- n
);
3282 while (isl_stream_eat_if_available(s
, ';')) {
3286 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3287 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3288 vars_drop(v
, v
->n
- n
);
3290 pa
= isl_pw_aff_union_add(pa
, pa_i
);
3293 if (isl_stream_eat(s
, '}'))
3302 isl_pw_aff_free(pa
);
3306 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3309 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3312 aff
= isl_stream_read_aff(s
);
3317 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3320 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3323 pa
= isl_stream_read_pw_aff(s
);
3328 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3329 * from a tuple "tuple" read by read_tuple.
3331 * Note that the function read_tuple accepts tuples where some output or
3332 * set dimensions are defined in terms of other output or set dimensions
3333 * since this function is also used to read maps. As a special case,
3334 * read_tuple also accept dimensions that are defined in terms of themselves
3335 * (i.e., that are not defined).
3336 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3337 * that the definitions of the output/set dimensions do not involve any
3338 * output/set dimensions.
3339 * Finally, drop the output dimensions from the domain of the result
3340 * of read_tuple (which is of the form [input, output] -> [output],
3341 * with anonymous domain) and reset the space.
3343 static __isl_give isl_multi_pw_aff
*extract_mpa_from_tuple(
3344 __isl_take isl_space
*dom_space
, __isl_keep isl_multi_pw_aff
*tuple
)
3349 isl_multi_pw_aff
*mpa
;
3351 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3352 dim
= isl_space_dim(dom_space
, isl_dim_all
);
3353 if (n
< 0 || dim
< 0)
3354 dom_space
= isl_space_free(dom_space
);
3355 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3356 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3357 if (!isl_space_is_params(dom_space
))
3358 space
= isl_space_map_from_domain_and_range(
3359 isl_space_copy(dom_space
), space
);
3360 isl_space_free(dom_space
);
3361 mpa
= isl_multi_pw_aff_alloc(space
);
3363 for (i
= 0; i
< n
; ++i
) {
3365 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3367 return isl_multi_pw_aff_free(mpa
);
3368 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3369 isl_ctx
*ctx
= isl_pw_aff_get_ctx(pa
);
3370 isl_pw_aff_free(pa
);
3371 isl_die(ctx
, isl_error_invalid
,
3372 "not an affine expression",
3373 return isl_multi_pw_aff_free(mpa
));
3375 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3376 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3377 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3378 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3384 /* Read a tuple of affine expressions, together with optional constraints
3385 * on the domain from "s". "dom" represents the initial constraints
3388 * The isl_multi_aff may live in either a set or a map space.
3389 * First read the first tuple and check if it is followed by a "->".
3390 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3391 * read in the next tuple. This tuple (or the first tuple if it was
3392 * not followed by a "->") is then converted into an isl_multi_pw_aff
3393 * through a call to extract_mpa_from_tuple.
3394 * The result is converted to an isl_pw_multi_aff and
3395 * its domain is intersected with the domain.
3397 static __isl_give isl_pw_multi_aff
*read_conditional_multi_aff(
3398 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3400 isl_multi_pw_aff
*tuple
;
3401 isl_multi_pw_aff
*mpa
;
3402 isl_pw_multi_aff
*pma
;
3405 tuple
= read_tuple(s
, v
, 0, 0);
3408 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3409 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3410 dom
= isl_map_domain(map
);
3411 tuple
= read_tuple(s
, v
, 0, 0);
3416 dom
= read_optional_formula(s
, dom
, v
, 0);
3418 vars_drop(v
, v
->n
- n
);
3420 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
3421 isl_multi_pw_aff_free(tuple
);
3422 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
3423 pma
= isl_pw_multi_aff_intersect_domain(pma
, dom
);
3431 /* Read an isl_pw_multi_aff from "s".
3433 * In particular, first read the parameters and then read a sequence
3434 * of one or more tuples of affine expressions with optional conditions and
3437 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(
3438 __isl_keep isl_stream
*s
)
3442 isl_pw_multi_aff
*pma
= NULL
;
3444 v
= vars_new(s
->ctx
);
3448 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3449 if (next_is_tuple(s
)) {
3450 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3451 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3454 if (isl_stream_eat(s
, '{'))
3457 pma
= read_conditional_multi_aff(s
, isl_set_copy(dom
), v
);
3459 while (isl_stream_eat_if_available(s
, ';')) {
3460 isl_pw_multi_aff
*pma2
;
3462 pma2
= read_conditional_multi_aff(s
, isl_set_copy(dom
), v
);
3463 pma
= isl_pw_multi_aff_union_add(pma
, pma2
);
3468 if (isl_stream_eat(s
, '}'))
3475 isl_pw_multi_aff_free(pma
);
3481 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3484 isl_pw_multi_aff
*pma
;
3485 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3488 pma
= isl_stream_read_pw_multi_aff(s
);
3493 /* Read an isl_union_pw_multi_aff from "s".
3494 * We currently read a generic object and if it turns out to be a set or
3495 * a map, we convert that to an isl_union_pw_multi_aff.
3496 * It would be more efficient if we were to construct
3497 * the isl_union_pw_multi_aff directly.
3499 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3500 __isl_keep isl_stream
*s
)
3508 if (obj
.type
== isl_obj_map
|| obj
.type
== isl_obj_set
)
3509 obj
= to_union(s
->ctx
, obj
);
3510 if (obj
.type
== isl_obj_union_map
)
3511 return isl_union_pw_multi_aff_from_union_map(obj
.v
);
3512 if (obj
.type
== isl_obj_union_set
)
3513 return isl_union_pw_multi_aff_from_union_set(obj
.v
);
3515 obj
.type
->free(obj
.v
);
3516 isl_die(s
->ctx
, isl_error_invalid
, "unexpected object type",
3520 /* Read an isl_union_pw_multi_aff from "str".
3522 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3523 isl_ctx
*ctx
, const char *str
)
3525 isl_union_pw_multi_aff
*upma
;
3526 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3529 upma
= isl_stream_read_union_pw_multi_aff(s
);
3534 /* Assuming "pa" represents a single affine expression defined on a universe
3535 * domain, extract this affine expression.
3537 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3544 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3545 "expecting single affine expression",
3547 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3548 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3549 "expecting universe domain",
3552 aff
= isl_aff_copy(pa
->p
[0].aff
);
3553 isl_pw_aff_free(pa
);
3556 isl_pw_aff_free(pa
);
3560 /* This function is called for each element in a tuple inside
3561 * isl_stream_read_multi_val.
3562 * Read an isl_val from "s" and add it to *list.
3564 static __isl_give isl_space
*read_val_el(__isl_keep isl_stream
*s
,
3565 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3567 isl_val_list
**list
= (isl_val_list
**) user
;
3570 val
= isl_stream_read_val(s
);
3571 *list
= isl_val_list_add(*list
, val
);
3573 return isl_space_free(space
);
3578 /* Read an isl_multi_val from "s".
3580 * We first read a tuple space, collecting the element values in a list.
3581 * Then we create an isl_multi_val from the space and the isl_val_list.
3583 __isl_give isl_multi_val
*isl_stream_read_multi_val(__isl_keep isl_stream
*s
)
3586 isl_set
*dom
= NULL
;
3588 isl_multi_val
*mv
= NULL
;
3591 v
= vars_new(s
->ctx
);
3595 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3596 if (next_is_tuple(s
)) {
3597 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3598 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3601 if (!isl_set_plain_is_universe(dom
))
3602 isl_die(s
->ctx
, isl_error_invalid
,
3603 "expecting universe parameter domain", goto error
);
3604 if (isl_stream_eat(s
, '{'))
3607 space
= isl_set_get_space(dom
);
3609 list
= isl_val_list_alloc(s
->ctx
, 0);
3610 space
= read_tuple_space(s
, v
, space
, 1, 0, &read_val_el
, &list
);
3611 mv
= isl_multi_val_from_val_list(space
, list
);
3613 if (isl_stream_eat(s
, '}'))
3622 isl_multi_val_free(mv
);
3626 /* Read an isl_multi_val from "str".
3628 __isl_give isl_multi_val
*isl_multi_val_read_from_str(isl_ctx
*ctx
,
3632 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3635 mv
= isl_stream_read_multi_val(s
);
3640 /* Read a multi-affine expression from "s".
3641 * If the multi-affine expression has a domain, then the tuple
3642 * representing this domain cannot involve any affine expressions.
3643 * The tuple representing the actual expressions needs to consist
3644 * of only affine expressions. Moreover, these expressions can
3645 * only depend on parameters and input dimensions and not on other
3646 * output dimensions.
3648 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(__isl_keep isl_stream
*s
)
3651 isl_set
*dom
= NULL
;
3652 isl_multi_pw_aff
*tuple
= NULL
;
3655 isl_space
*space
, *dom_space
;
3656 isl_multi_aff
*ma
= NULL
;
3658 v
= vars_new(s
->ctx
);
3662 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3663 if (next_is_tuple(s
)) {
3664 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3665 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3668 if (!isl_set_plain_is_universe(dom
))
3669 isl_die(s
->ctx
, isl_error_invalid
,
3670 "expecting universe parameter domain", goto error
);
3671 if (isl_stream_eat(s
, '{'))
3674 tuple
= read_tuple(s
, v
, 0, 0);
3677 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3682 has_expr
= tuple_has_expr(tuple
);
3686 isl_die(s
->ctx
, isl_error_invalid
,
3687 "expecting universe domain", goto error
);
3688 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3689 set
= isl_set_universe(space
);
3690 dom
= isl_set_intersect_params(set
, dom
);
3691 isl_multi_pw_aff_free(tuple
);
3692 tuple
= read_tuple(s
, v
, 0, 0);
3697 if (isl_stream_eat(s
, '}'))
3700 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3701 dim
= isl_set_dim(dom
, isl_dim_all
);
3702 if (n
< 0 || dim
< 0)
3704 dom_space
= isl_set_get_space(dom
);
3705 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3706 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3707 if (!isl_space_is_params(dom_space
))
3708 space
= isl_space_map_from_domain_and_range(
3709 isl_space_copy(dom_space
), space
);
3710 isl_space_free(dom_space
);
3711 ma
= isl_multi_aff_alloc(space
);
3713 for (i
= 0; i
< n
; ++i
) {
3716 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3717 aff
= aff_from_pw_aff(pa
);
3720 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3722 isl_die(s
->ctx
, isl_error_invalid
,
3723 "not an affine expression", goto error
);
3725 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3726 space
= isl_multi_aff_get_domain_space(ma
);
3727 aff
= isl_aff_reset_domain_space(aff
, space
);
3728 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3731 isl_multi_pw_aff_free(tuple
);
3736 isl_multi_pw_aff_free(tuple
);
3739 isl_multi_aff_free(ma
);
3743 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3746 isl_multi_aff
*maff
;
3747 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3750 maff
= isl_stream_read_multi_aff(s
);
3755 /* Read an isl_multi_pw_aff from "s".
3757 * The input format is similar to that of map, except that any conditions
3758 * on the domains should be specified inside the tuple since each
3759 * piecewise affine expression may have a different domain.
3760 * However, additional, shared conditions can also be specified.
3761 * This is especially useful for setting the explicit domain
3762 * of a zero-dimensional isl_multi_pw_aff.
3764 * Since we do not know in advance if the isl_multi_pw_aff lives
3765 * in a set or a map space, we first read the first tuple and check
3766 * if it is followed by a "->". If so, we convert the tuple into
3767 * the domain of the isl_multi_pw_aff and read in the next tuple.
3768 * This tuple (or the first tuple if it was not followed by a "->")
3769 * is then converted into the isl_multi_pw_aff through a call
3770 * to extract_mpa_from_tuple and the domain of the result
3771 * is intersected with the domain.
3773 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(
3774 __isl_keep isl_stream
*s
)
3777 isl_set
*dom
= NULL
;
3778 isl_multi_pw_aff
*tuple
= NULL
;
3779 isl_multi_pw_aff
*mpa
= NULL
;
3781 v
= vars_new(s
->ctx
);
3785 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3786 if (next_is_tuple(s
)) {
3787 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3788 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3791 if (isl_stream_eat(s
, '{'))
3794 tuple
= read_tuple(s
, v
, 0, 0);
3797 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3798 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3799 dom
= isl_map_domain(map
);
3800 tuple
= read_tuple(s
, v
, 0, 0);
3805 if (isl_stream_eat_if_available(s
, ':'))
3806 dom
= read_formula(s
, v
, dom
, 0);
3808 if (isl_stream_eat(s
, '}'))
3811 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
3813 isl_multi_pw_aff_free(tuple
);
3815 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
3818 isl_multi_pw_aff_free(tuple
);
3821 isl_multi_pw_aff_free(mpa
);
3825 /* Read an isl_multi_pw_aff from "str".
3827 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
3830 isl_multi_pw_aff
*mpa
;
3831 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3834 mpa
= isl_stream_read_multi_pw_aff(s
);
3839 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3841 static __isl_give isl_union_pw_aff
*read_union_pw_aff_with_dom(
3842 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3845 isl_union_pw_aff
*upa
= NULL
;
3850 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3851 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3852 vars_drop(v
, v
->n
- n
);
3854 upa
= isl_union_pw_aff_from_pw_aff(pa
);
3856 while (isl_stream_eat_if_available(s
, ';')) {
3858 isl_union_pw_aff
*upa_i
;
3861 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3862 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3863 vars_drop(v
, v
->n
- n
);
3865 upa_i
= isl_union_pw_aff_from_pw_aff(pa_i
);
3866 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
3873 /* Read an isl_union_pw_aff from "s".
3875 * First check if there are any paramters, then read in the opening brace
3876 * and use read_union_pw_aff_with_dom to read in the body of
3877 * the isl_union_pw_aff. Finally, read the closing brace.
3879 __isl_give isl_union_pw_aff
*isl_stream_read_union_pw_aff(
3880 __isl_keep isl_stream
*s
)
3884 isl_union_pw_aff
*upa
= NULL
;
3886 v
= vars_new(s
->ctx
);
3890 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3891 if (next_is_tuple(s
)) {
3892 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3893 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3896 if (isl_stream_eat(s
, '{'))
3899 upa
= read_union_pw_aff_with_dom(s
, isl_set_copy(dom
), v
);
3901 if (isl_stream_eat(s
, '}'))
3910 isl_union_pw_aff_free(upa
);
3914 /* Read an isl_union_pw_aff from "str".
3916 __isl_give isl_union_pw_aff
*isl_union_pw_aff_read_from_str(isl_ctx
*ctx
,
3919 isl_union_pw_aff
*upa
;
3920 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3923 upa
= isl_stream_read_union_pw_aff(s
);
3928 /* This function is called for each element in a tuple inside
3929 * isl_stream_read_multi_union_pw_aff.
3931 * Read a '{', the union piecewise affine expression body and a '}' and
3932 * add the isl_union_pw_aff to *list.
3934 static __isl_give isl_space
*read_union_pw_aff_el(__isl_keep isl_stream
*s
,
3935 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
3938 isl_union_pw_aff
*upa
;
3939 isl_union_pw_aff_list
**list
= (isl_union_pw_aff_list
**) user
;
3941 dom
= isl_set_universe(isl_space_params(isl_space_copy(space
)));
3942 if (isl_stream_eat(s
, '{'))
3944 upa
= read_union_pw_aff_with_dom(s
, dom
, v
);
3945 *list
= isl_union_pw_aff_list_add(*list
, upa
);
3946 if (isl_stream_eat(s
, '}'))
3947 return isl_space_free(space
);
3949 return isl_space_free(space
);
3953 return isl_space_free(space
);
3956 /* Do the next tokens in "s" correspond to an empty tuple?
3957 * In particular, does the stream start with a '[', followed by a ']',
3958 * not followed by a "->"?
3960 static int next_is_empty_tuple(__isl_keep isl_stream
*s
)
3962 struct isl_token
*tok
, *tok2
, *tok3
;
3963 int is_empty_tuple
= 0;
3965 tok
= isl_stream_next_token(s
);
3968 if (tok
->type
!= '[') {
3969 isl_stream_push_token(s
, tok
);
3973 tok2
= isl_stream_next_token(s
);
3974 if (tok2
&& tok2
->type
== ']') {
3975 tok3
= isl_stream_next_token(s
);
3976 is_empty_tuple
= !tok
|| tok
->type
!= ISL_TOKEN_TO
;
3978 isl_stream_push_token(s
, tok3
);
3981 isl_stream_push_token(s
, tok2
);
3982 isl_stream_push_token(s
, tok
);
3984 return is_empty_tuple
;
3987 /* Do the next tokens in "s" correspond to a tuple of parameters?
3988 * In particular, does the stream start with a '[' that is not
3989 * followed by a '{' or a nested tuple?
3991 static int next_is_param_tuple(__isl_keep isl_stream
*s
)
3993 struct isl_token
*tok
, *tok2
;
3996 tok
= isl_stream_next_token(s
);
3999 if (tok
->type
!= '[' || next_is_tuple(s
)) {
4000 isl_stream_push_token(s
, tok
);
4004 tok2
= isl_stream_next_token(s
);
4005 is_tuple
= tok2
&& tok2
->type
!= '{';
4007 isl_stream_push_token(s
, tok2
);
4008 isl_stream_push_token(s
, tok
);
4013 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
4014 * i.e., everything except the parameter specification and
4015 * without shared domain constraints.
4016 * "v" contains a description of the identifiers parsed so far.
4017 * The parameters, if any, are specified by "space".
4019 * The body is of the form
4021 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4023 * Read the tuple, collecting the individual isl_union_pw_aff
4024 * elements in a list and construct the result from the tuple space and
4027 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body_core(
4028 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
4030 isl_union_pw_aff_list
*list
;
4031 isl_multi_union_pw_aff
*mupa
;
4033 list
= isl_union_pw_aff_list_alloc(s
->ctx
, 0);
4034 space
= read_tuple_space(s
, v
, space
, 1, 0,
4035 &read_union_pw_aff_el
, &list
);
4036 mupa
= isl_multi_union_pw_aff_from_union_pw_aff_list(space
, list
);
4041 /* Read the body of an isl_union_set from "s",
4042 * i.e., everything except the parameter specification.
4043 * "v" contains a description of the identifiers parsed so far.
4044 * The parameters, if any, are specified by "space".
4046 * First read a generic disjunction of object bodies and then try and extract
4047 * an isl_union_set from that.
4049 static __isl_give isl_union_set
*read_union_set_body(__isl_keep isl_stream
*s
,
4050 struct vars
*v
, __isl_take isl_space
*space
)
4052 struct isl_obj obj
= { isl_obj_set
, NULL
};
4055 map
= isl_set_universe(space
);
4056 if (isl_stream_eat(s
, '{') < 0)
4058 obj
= obj_read_disjuncts(s
, v
, map
);
4059 if (isl_stream_eat(s
, '}') < 0)
4063 return extract_union_set(s
->ctx
, obj
);
4065 obj
.type
->free(obj
.v
);
4070 /* Read the body of an isl_multi_union_pw_aff from "s",
4071 * i.e., everything except the parameter specification.
4072 * "v" contains a description of the identifiers parsed so far.
4073 * The parameters, if any, are specified by "space".
4075 * In particular, handle the special case with shared domain constraints.
4076 * These are specified as
4080 * and are especially useful for setting the explicit domain
4081 * of a zero-dimensional isl_multi_union_pw_aff.
4082 * The core isl_multi_union_pw_aff body ([...]) is read by
4083 * read_multi_union_pw_aff_body_core.
4085 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body(
4086 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
4088 isl_multi_union_pw_aff
*mupa
;
4090 if (!isl_stream_next_token_is(s
, '('))
4091 return read_multi_union_pw_aff_body_core(s
, v
, space
);
4093 if (isl_stream_eat(s
, '(') < 0)
4095 mupa
= read_multi_union_pw_aff_body_core(s
, v
, isl_space_copy(space
));
4096 if (isl_stream_eat_if_available(s
, ':')) {
4099 dom
= read_union_set_body(s
, v
, space
);
4100 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4102 isl_space_free(space
);
4104 if (isl_stream_eat(s
, ')') < 0)
4105 return isl_multi_union_pw_aff_free(mupa
);
4109 isl_space_free(space
);
4113 /* Read an isl_multi_union_pw_aff from "s".
4115 * The input has the form
4117 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4121 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4123 * Additionally, a shared domain may be specified as
4129 * [..] -> ([..] : ...)
4131 * The first case is handled by the caller, the second case
4132 * is handled by read_multi_union_pw_aff_body.
4134 * We first check for the special case of an empty tuple "[]".
4135 * Then we check if there are any parameters.
4136 * Finally, read the tuple and construct the result.
4138 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_core(
4139 __isl_keep isl_stream
*s
)
4142 isl_set
*dom
= NULL
;
4144 isl_multi_union_pw_aff
*mupa
= NULL
;
4146 if (next_is_empty_tuple(s
)) {
4147 if (isl_stream_eat(s
, '['))
4149 if (isl_stream_eat(s
, ']'))
4151 space
= isl_space_set_alloc(s
->ctx
, 0, 0);
4152 return isl_multi_union_pw_aff_zero(space
);
4155 v
= vars_new(s
->ctx
);
4159 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4160 if (next_is_param_tuple(s
)) {
4161 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4162 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4165 space
= isl_set_get_space(dom
);
4167 mupa
= read_multi_union_pw_aff_body(s
, v
, space
);
4175 isl_multi_union_pw_aff_free(mupa
);
4179 /* Read an isl_multi_union_pw_aff from "s".
4181 * In particular, handle the special case with shared domain constraints.
4182 * These are specified as
4186 * and are especially useful for setting the explicit domain
4187 * of a zero-dimensional isl_multi_union_pw_aff.
4188 * The core isl_multi_union_pw_aff ([...]) is read by
4189 * read_multi_union_pw_aff_core.
4191 __isl_give isl_multi_union_pw_aff
*isl_stream_read_multi_union_pw_aff(
4192 __isl_keep isl_stream
*s
)
4194 isl_multi_union_pw_aff
*mupa
;
4196 if (!isl_stream_next_token_is(s
, '('))
4197 return read_multi_union_pw_aff_core(s
);
4199 if (isl_stream_eat(s
, '(') < 0)
4201 mupa
= read_multi_union_pw_aff_core(s
);
4202 if (isl_stream_eat_if_available(s
, ':')) {
4205 dom
= isl_stream_read_union_set(s
);
4206 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4208 if (isl_stream_eat(s
, ')') < 0)
4209 return isl_multi_union_pw_aff_free(mupa
);
4213 /* Read an isl_multi_union_pw_aff from "str".
4215 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_read_from_str(
4216 isl_ctx
*ctx
, const char *str
)
4218 isl_multi_union_pw_aff
*mupa
;
4219 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4222 mupa
= isl_stream_read_multi_union_pw_aff(s
);
4227 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
4228 __isl_keep isl_stream
*s
)
4233 if (obj
.type
== isl_obj_pw_qpolynomial
) {
4234 obj
.type
= isl_obj_union_pw_qpolynomial
;
4235 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
4238 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
4243 obj
.type
->free(obj
.v
);
4247 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
4248 isl_ctx
*ctx
, const char *str
)
4250 isl_union_pw_qpolynomial
*upwqp
;
4251 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4254 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);