2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2019 Cerebras Systems
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_id_private.h>
25 #include <isl_stream_private.h>
27 #include "isl_polynomial_private.h"
28 #include <isl/union_set.h>
29 #include <isl/union_map.h>
30 #include <isl_mat_private.h>
31 #include <isl_aff_private.h>
32 #include <isl_vec_private.h>
34 #include <isl_val_private.h>
39 struct variable
*next
;
48 static struct vars
*vars_new(struct isl_ctx
*ctx
)
51 v
= isl_alloc_type(ctx
, struct vars
);
60 static void variable_free(struct variable
*var
)
63 struct variable
*next
= var
->next
;
70 static void vars_free(struct vars
*v
)
78 static void vars_drop(struct vars
*v
, int n
)
89 struct variable
*next
= var
->next
;
97 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
100 struct variable
*var
;
101 var
= isl_calloc_type(v
->ctx
, struct variable
);
104 var
->name
= strdup(name
);
105 var
->name
[len
] = '\0';
114 static int vars_pos(struct vars
*v
, const char *s
, int len
)
121 for (q
= v
->v
; q
; q
= q
->next
) {
122 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
129 v
->v
= variable_new(v
, s
, len
, v
->n
);
137 static int vars_add_anon(struct vars
*v
)
139 v
->v
= variable_new(v
, "", 0, v
->n
);
148 /* Obtain next token, with some preprocessing.
149 * In particular, evaluate expressions of the form x^y,
150 * with x and y values.
152 static struct isl_token
*next_token(__isl_keep isl_stream
*s
)
154 struct isl_token
*tok
, *tok2
;
156 tok
= isl_stream_next_token(s
);
157 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
159 if (!isl_stream_eat_if_available(s
, '^'))
161 tok2
= isl_stream_next_token(s
);
162 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
163 isl_stream_error(s
, tok2
, "expecting constant value");
167 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
169 isl_token_free(tok2
);
173 isl_token_free(tok2
);
177 /* Read an isl_val from "s".
179 * The following token sequences are recognized
182 * "-" "infty" -> -infty
187 * where n, d and v are integer constants.
189 __isl_give isl_val
*isl_stream_read_val(__isl_keep isl_stream
*s
)
191 struct isl_token
*tok
= NULL
;
192 struct isl_token
*tok2
= NULL
;
197 isl_stream_error(s
, NULL
, "unexpected EOF");
200 if (tok
->type
== ISL_TOKEN_INFTY
) {
202 return isl_val_infty(s
->ctx
);
204 if (tok
->type
== '-' &&
205 isl_stream_eat_if_available(s
, ISL_TOKEN_INFTY
)) {
207 return isl_val_neginfty(s
->ctx
);
209 if (tok
->type
== ISL_TOKEN_NAN
) {
211 return isl_val_nan(s
->ctx
);
213 if (tok
->type
!= ISL_TOKEN_VALUE
) {
214 isl_stream_error(s
, tok
, "expecting value");
218 if (isl_stream_eat_if_available(s
, '/')) {
219 tok2
= next_token(s
);
221 isl_stream_error(s
, NULL
, "unexpected EOF");
224 if (tok2
->type
!= ISL_TOKEN_VALUE
) {
225 isl_stream_error(s
, tok2
, "expecting value");
228 val
= isl_val_rat_from_isl_int(s
->ctx
, tok
->u
.v
, tok2
->u
.v
);
229 val
= isl_val_normalize(val
);
231 val
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
235 isl_token_free(tok2
);
239 isl_token_free(tok2
);
243 /* Read an isl_val from "str".
245 __isl_give isl_val
*isl_val_read_from_str(isl_ctx
*ctx
, const char *str
)
248 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
251 val
= isl_stream_read_val(s
);
256 /* Perform an integer division on *f and
257 * an integer value read from the stream.
259 static isl_stat
int_div_by_cst(__isl_keep isl_stream
*s
, isl_int
*f
)
261 struct isl_token
*tok
;
264 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
265 isl_stream_error(s
, tok
, "expecting constant value");
269 isl_int_fdiv_q(*f
, *f
, tok
->u
.v
);
276 return isl_stat_error
;
279 static isl_stat
accept_cst_factor(__isl_keep isl_stream
*s
, isl_int
*f
)
281 struct isl_token
*tok
;
284 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
285 isl_stream_error(s
, tok
, "expecting constant value");
289 isl_int_mul(*f
, *f
, tok
->u
.v
);
293 if (isl_stream_eat_if_available(s
, '*'))
294 return accept_cst_factor(s
, f
);
299 return isl_stat_error
;
302 /* Given an affine expression aff, return an affine expression
303 * for aff % d, with d the next token on the stream, which is
304 * assumed to be a constant.
306 * We introduce an integer division q = [aff/d] and the result
307 * is set to aff - d q.
309 static __isl_give isl_pw_aff
*affine_mod(__isl_keep isl_stream
*s
,
310 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
312 struct isl_token
*tok
;
316 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
317 isl_stream_error(s
, tok
, "expecting constant value");
321 q
= isl_pw_aff_copy(aff
);
322 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
323 q
= isl_pw_aff_floor(q
);
324 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
326 aff
= isl_pw_aff_sub(aff
, q
);
331 isl_pw_aff_free(aff
);
336 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
337 __isl_take isl_space
*space
, struct vars
*v
);
338 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
339 __isl_take isl_space
*space
, struct vars
*v
);
341 static __isl_give isl_pw_aff
*accept_minmax(__isl_keep isl_stream
*s
,
342 __isl_take isl_space
*space
, struct vars
*v
)
344 struct isl_token
*tok
;
345 isl_pw_aff_list
*list
= NULL
;
348 tok
= isl_stream_next_token(s
);
351 min
= tok
->type
== ISL_TOKEN_MIN
;
354 if (isl_stream_eat(s
, '('))
357 list
= accept_affine_list(s
, isl_space_copy(space
), v
);
361 if (isl_stream_eat(s
, ')'))
364 isl_space_free(space
);
365 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
367 isl_space_free(space
);
368 isl_pw_aff_list_free(list
);
372 /* Is "tok" the start of an integer division?
374 static int is_start_of_div(struct isl_token
*tok
)
378 if (tok
->type
== '[')
380 if (tok
->type
== ISL_TOKEN_FLOOR
)
382 if (tok
->type
== ISL_TOKEN_CEIL
)
384 if (tok
->type
== ISL_TOKEN_FLOORD
)
386 if (tok
->type
== ISL_TOKEN_CEILD
)
391 /* Read an integer division from "s" and return it as an isl_pw_aff.
393 * The integer division can be of the form
395 * [<affine expression>]
396 * floor(<affine expression>)
397 * ceil(<affine expression>)
398 * floord(<affine expression>,<denominator>)
399 * ceild(<affine expression>,<denominator>)
401 static __isl_give isl_pw_aff
*accept_div(__isl_keep isl_stream
*s
,
402 __isl_take isl_space
*space
, struct vars
*v
)
404 struct isl_token
*tok
;
408 isl_pw_aff
*pwaff
= NULL
;
410 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
412 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
414 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOOR
))
416 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEIL
))
419 if (isl_stream_eat(s
, '('))
422 if (isl_stream_eat(s
, '['))
426 pwaff
= accept_affine(s
, isl_space_copy(space
), v
);
429 if (isl_stream_eat(s
, ','))
435 if (tok
->type
!= ISL_TOKEN_VALUE
) {
436 isl_stream_error(s
, tok
, "expected denominator");
437 isl_stream_push_token(s
, tok
);
440 pwaff
= isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
445 pwaff
= isl_pw_aff_ceil(pwaff
);
447 pwaff
= isl_pw_aff_floor(pwaff
);
450 if (isl_stream_eat(s
, ')'))
453 if (isl_stream_eat(s
, ']'))
457 isl_space_free(space
);
460 isl_space_free(space
);
461 isl_pw_aff_free(pwaff
);
465 /* Divide "pa" by an integer constant read from the stream.
467 static __isl_give isl_pw_aff
*pw_aff_div_by_cst(__isl_keep isl_stream
*s
,
468 __isl_take isl_pw_aff
*pa
)
472 isl_int_set_si(f
, 1);
473 if (accept_cst_factor(s
, &f
) < 0)
474 pa
= isl_pw_aff_free(pa
);
475 pa
= isl_pw_aff_scale_down(pa
, f
);
481 static __isl_give isl_pw_aff
*accept_affine_factor(__isl_keep isl_stream
*s
,
482 __isl_take isl_space
*space
, struct vars
*v
)
484 struct isl_token
*tok
= NULL
;
485 isl_pw_aff
*res
= NULL
;
489 isl_stream_error(s
, NULL
, "unexpected EOF");
493 if (tok
->type
== ISL_TOKEN_AFF
) {
494 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
496 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
498 int pos
= vars_pos(v
, tok
->u
.s
, -1);
504 vars_drop(v
, v
->n
- n
);
505 isl_stream_error(s
, tok
, "unknown identifier");
509 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(space
)));
512 aff
->v
= isl_vec_set_element_si(aff
->v
, 2 + pos
, 1);
514 aff
= isl_aff_free(aff
);
515 res
= isl_pw_aff_from_aff(aff
);
517 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
518 if (isl_stream_eat_if_available(s
, '*')) {
519 res
= accept_affine_factor(s
, isl_space_copy(space
), v
);
520 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
524 ls
= isl_local_space_from_space(isl_space_copy(space
));
525 aff
= isl_aff_zero_on_domain(ls
);
526 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
527 res
= isl_pw_aff_from_aff(aff
);
530 } else if (tok
->type
== '(') {
533 res
= accept_affine(s
, isl_space_copy(space
), v
);
536 if (isl_stream_eat(s
, ')'))
538 } else if (is_start_of_div(tok
)) {
539 isl_stream_push_token(s
, tok
);
541 res
= accept_div(s
, isl_space_copy(space
), v
);
542 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
543 isl_stream_push_token(s
, tok
);
545 res
= accept_minmax(s
, isl_space_copy(space
), v
);
547 isl_stream_error(s
, tok
, "expecting factor");
550 if (isl_stream_eat_if_available(s
, '%') ||
551 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
552 isl_space_free(space
);
553 return affine_mod(s
, v
, res
);
555 if (isl_stream_eat_if_available(s
, '*')) {
558 isl_int_set_si(f
, 1);
559 if (accept_cst_factor(s
, &f
) < 0) {
563 res
= isl_pw_aff_scale(res
, f
);
566 if (isl_stream_eat_if_available(s
, '/'))
567 res
= pw_aff_div_by_cst(s
, res
);
568 if (isl_stream_eat_if_available(s
, ISL_TOKEN_INT_DIV
))
569 res
= isl_pw_aff_floor(pw_aff_div_by_cst(s
, res
));
571 isl_space_free(space
);
576 isl_pw_aff_free(res
);
577 isl_space_free(space
);
581 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
586 space
= isl_pw_aff_get_domain_space(pwaff
);
587 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
588 aff
= isl_aff_add_constant(aff
, v
);
590 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
593 /* Return a piecewise affine expression defined on the specified domain
594 * that represents NaN.
596 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
598 return isl_pw_aff_nan_on_domain_space(isl_space_copy(space
));
601 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
602 __isl_take isl_space
*space
, struct vars
*v
)
604 struct isl_token
*tok
= NULL
;
609 ls
= isl_local_space_from_space(isl_space_copy(space
));
610 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
617 isl_stream_error(s
, NULL
, "unexpected EOF");
620 if (tok
->type
== '-') {
625 if (tok
->type
== '(' || is_start_of_div(tok
) ||
626 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
627 tok
->type
== ISL_TOKEN_IDENT
||
628 tok
->type
== ISL_TOKEN_AFF
) {
630 isl_stream_push_token(s
, tok
);
632 term
= accept_affine_factor(s
,
633 isl_space_copy(space
), v
);
635 res
= isl_pw_aff_sub(res
, term
);
637 res
= isl_pw_aff_add(res
, term
);
641 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
643 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
644 if (isl_stream_eat_if_available(s
, '*') ||
645 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
647 term
= accept_affine_factor(s
,
648 isl_space_copy(space
), v
);
649 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
650 res
= isl_pw_aff_add(res
, term
);
654 if (isl_stream_eat_if_available(s
,
655 ISL_TOKEN_INT_DIV
) &&
656 int_div_by_cst(s
, &tok
->u
.v
) < 0)
658 res
= add_cst(res
, tok
->u
.v
);
661 } else if (tok
->type
== ISL_TOKEN_NAN
) {
662 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
664 isl_stream_error(s
, tok
, "unexpected isl_token");
665 isl_stream_push_token(s
, tok
);
666 isl_pw_aff_free(res
);
667 isl_space_free(space
);
673 if (tok
&& tok
->type
== '-') {
676 } else if (tok
&& tok
->type
== '+') {
679 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
680 isl_int_is_neg(tok
->u
.v
)) {
681 isl_stream_push_token(s
, tok
);
684 isl_stream_push_token(s
, tok
);
689 isl_space_free(space
);
692 isl_space_free(space
);
694 isl_pw_aff_free(res
);
698 /* Is "type" the type of a comparison operator between lists
699 * of affine expressions?
701 static int is_list_comparator_type(int type
)
704 case ISL_TOKEN_LEX_LT
:
705 case ISL_TOKEN_LEX_GT
:
706 case ISL_TOKEN_LEX_LE
:
707 case ISL_TOKEN_LEX_GE
:
714 static int is_comparator(struct isl_token
*tok
)
718 if (is_list_comparator_type(tok
->type
))
734 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
735 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
736 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
737 __isl_take isl_space
*space
, struct vars
*v
, int rational
);
739 /* Accept a ternary operator, given the first argument.
741 static __isl_give isl_pw_aff
*accept_ternary(__isl_keep isl_stream
*s
,
742 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
745 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
750 if (isl_stream_eat(s
, '?'))
753 space
= isl_space_wrap(isl_map_get_space(cond
));
754 pwaff1
= accept_extended_affine(s
, space
, v
, rational
);
758 if (isl_stream_eat(s
, ':'))
761 space
= isl_pw_aff_get_domain_space(pwaff1
);
762 pwaff2
= accept_extended_affine(s
, space
, v
, rational
);
766 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
767 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
770 isl_pw_aff_free(pwaff1
);
771 isl_pw_aff_free(pwaff2
);
775 /* Set *line and *col to those of the next token, if any.
777 static void set_current_line_col(__isl_keep isl_stream
*s
, int *line
, int *col
)
779 struct isl_token
*tok
;
781 tok
= isl_stream_next_token(s
);
787 isl_stream_push_token(s
, tok
);
790 /* Push a token encapsulating "pa" onto "s", with the given
793 static isl_stat
push_aff(__isl_keep isl_stream
*s
, int line
, int col
,
794 __isl_take isl_pw_aff
*pa
)
796 struct isl_token
*tok
;
798 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
801 tok
->type
= ISL_TOKEN_AFF
;
803 isl_stream_push_token(s
, tok
);
808 return isl_stat_error
;
811 /* Is the next token a comparison operator?
813 static int next_is_comparator(__isl_keep isl_stream
*s
)
816 struct isl_token
*tok
;
818 tok
= isl_stream_next_token(s
);
822 is_comp
= is_comparator(tok
);
823 isl_stream_push_token(s
, tok
);
828 /* Accept an affine expression that may involve ternary operators.
829 * We first read an affine expression.
830 * If it is not followed by a comparison operator, we simply return it.
831 * Otherwise, we assume the affine expression is part of the first
832 * argument of a ternary operator and try to parse that.
834 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
835 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
839 int line
= -1, col
= -1;
841 set_current_line_col(s
, &line
, &col
);
843 pwaff
= accept_affine(s
, space
, v
);
845 pwaff
= isl_pw_aff_set_rational(pwaff
);
848 if (!next_is_comparator(s
))
851 space
= isl_pw_aff_get_domain_space(pwaff
);
852 cond
= isl_map_universe(isl_space_unwrap(space
));
854 if (push_aff(s
, line
, col
, pwaff
) < 0)
855 cond
= isl_map_free(cond
);
859 cond
= read_formula(s
, v
, cond
, rational
);
861 return accept_ternary(s
, cond
, v
, rational
);
864 static __isl_give isl_map
*read_var_def(__isl_keep isl_stream
*s
,
865 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
872 if (type
== isl_dim_param
)
873 pos
= isl_map_dim(map
, isl_dim_param
);
875 pos
= isl_map_dim(map
, isl_dim_in
);
876 if (type
== isl_dim_out
) {
877 isl_size n_out
= isl_map_dim(map
, isl_dim_out
);
878 if (pos
< 0 || n_out
< 0)
879 return isl_map_free(map
);
885 return isl_map_free(map
);
888 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
890 def_map
= isl_map_from_pw_aff(def
);
891 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
892 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
894 map
= isl_map_intersect(map
, def_map
);
899 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
900 __isl_take isl_space
*space
, struct vars
*v
)
903 isl_pw_aff_list
*list
;
904 struct isl_token
*tok
= NULL
;
906 pwaff
= accept_affine(s
, isl_space_copy(space
), v
);
907 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
912 tok
= isl_stream_next_token(s
);
914 isl_stream_error(s
, NULL
, "unexpected EOF");
917 if (tok
->type
!= ',') {
918 isl_stream_push_token(s
, tok
);
923 pwaff
= accept_affine(s
, isl_space_copy(space
), v
);
924 list
= isl_pw_aff_list_concat(list
,
925 isl_pw_aff_list_from_pw_aff(pwaff
));
930 isl_space_free(space
);
933 isl_space_free(space
);
934 isl_pw_aff_list_free(list
);
938 static __isl_give isl_map
*read_defined_var_list(__isl_keep isl_stream
*s
,
939 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
941 struct isl_token
*tok
;
943 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
947 if (tok
->type
!= ISL_TOKEN_IDENT
)
950 p
= vars_pos(v
, tok
->u
.s
, -1);
954 isl_stream_error(s
, tok
, "expecting unique identifier");
958 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
961 tok
= isl_stream_next_token(s
);
962 if (tok
&& tok
->type
== '=') {
964 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
965 tok
= isl_stream_next_token(s
);
968 if (!tok
|| tok
->type
!= ',')
974 isl_stream_push_token(s
, tok
);
983 static int next_is_tuple(__isl_keep isl_stream
*s
)
985 struct isl_token
*tok
;
988 tok
= isl_stream_next_token(s
);
991 if (tok
->type
== '[') {
992 isl_stream_push_token(s
, tok
);
995 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
996 isl_stream_push_token(s
, tok
);
1000 is_tuple
= isl_stream_next_token_is(s
, '[');
1002 isl_stream_push_token(s
, tok
);
1007 /* Does the next token mark the end of a tuple element?
1009 static int next_is_end_tuple_element(__isl_keep isl_stream
*s
)
1011 return isl_stream_next_token_is(s
, ',') ||
1012 isl_stream_next_token_is(s
, ']');
1015 /* Is the next token one that necessarily forms the start of a condition?
1017 static int next_is_condition_start(__isl_keep isl_stream
*s
)
1019 return isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1020 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1021 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1022 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1023 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
);
1026 /* Is "pa" an expression in term of earlier dimensions?
1027 * The alternative is that the dimension is defined to be equal to itself,
1028 * meaning that it has a universe domain and an expression that depends
1029 * on itself. "i" is the position of the expression in a sequence
1030 * of "n" expressions. The final dimensions of "pa" correspond to
1031 * these "n" expressions.
1033 static isl_bool
pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
1038 return isl_bool_error
;
1040 return isl_bool_true
;
1041 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
1042 return isl_bool_true
;
1045 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
1046 return isl_bool_true
;
1047 return isl_bool_false
;
1050 /* Does the tuple contain any dimensions that are defined
1051 * in terms of earlier dimensions?
1053 static isl_bool
tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
1057 isl_bool has_expr
= isl_bool_false
;
1060 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1062 return isl_bool_error
;
1063 for (i
= 0; i
< n
; ++i
) {
1064 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1065 has_expr
= pw_aff_is_expr(pa
, i
, n
);
1066 isl_pw_aff_free(pa
);
1067 if (has_expr
< 0 || has_expr
)
1074 /* Set the name of dimension "pos" in "space" to "name".
1075 * During printing, we add primes if the same name appears more than once
1076 * to distinguish the occurrences. Here, we remove those primes from "name"
1077 * before setting the name of the dimension.
1079 static __isl_give isl_space
*space_set_dim_name(__isl_take isl_space
*space
,
1080 int pos
, char *name
)
1087 prime
= strchr(name
, '\'');
1090 space
= isl_space_set_dim_name(space
, isl_dim_out
, pos
, name
);
1097 /* Construct an isl_pw_aff defined on a "space" (with v->n variables)
1098 * that is equal to the last of those variables.
1100 static __isl_give isl_pw_aff
*identity_tuple_el_on_space(
1101 __isl_take isl_space
*space
, struct vars
*v
)
1105 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1106 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
- 1, 1);
1107 return isl_pw_aff_from_aff(aff
);
1110 /* Construct an isl_pw_aff defined on the domain space of "pa"
1111 * that is equal to the last variable in "v".
1113 * That is, if D is the domain space of "pa", then construct
1117 static __isl_give isl_pw_aff
*init_range(__isl_keep isl_pw_aff
*pa
,
1122 space
= isl_pw_aff_get_domain_space(pa
);
1123 return identity_tuple_el_on_space(space
, v
);
1126 /* Impose the lower bound "lower" on the variable represented by "range_pa".
1128 * In particular, "range_pa" is of the form
1130 * D[..., i] -> i : C
1132 * with D also the domains space of "lower' and "C" some constraints.
1134 * Return the expression
1136 * D[..., i] -> i : C and i >= lower
1138 static __isl_give isl_pw_aff
*set_lower(__isl_take isl_pw_aff
*range_pa
,
1139 __isl_take isl_pw_aff
*lower
)
1143 range
= isl_pw_aff_ge_set(isl_pw_aff_copy(range_pa
), lower
);
1144 return isl_pw_aff_intersect_domain(range_pa
, range
);
1147 /* Impose the upper bound "upper" on the variable represented by "range_pa".
1149 * In particular, "range_pa" is of the form
1151 * D[..., i] -> i : C
1153 * with D also the domains space of "upper' and "C" some constraints.
1155 * Return the expression
1157 * D[..., i] -> i : C and i <= upper
1159 static __isl_give isl_pw_aff
*set_upper(__isl_take isl_pw_aff
*range_pa
,
1160 __isl_take isl_pw_aff
*upper
)
1164 range
= isl_pw_aff_le_set(isl_pw_aff_copy(range_pa
), upper
);
1165 return isl_pw_aff_intersect_domain(range_pa
, range
);
1168 /* Construct a piecewise affine expression corresponding
1169 * to the last variable in "v" that is greater than or equal to "pa".
1171 * In particular, if D is the domain space of "pa",
1172 * then construct the expression
1176 * impose lower bound "pa" and return
1178 * D[..., i] -> i : i >= pa
1180 static __isl_give isl_pw_aff
*construct_lower(__isl_take isl_pw_aff
*pa
,
1183 return set_lower(init_range(pa
, v
), pa
);
1186 /* Construct a piecewise affine expression corresponding
1187 * to the last variable in "v" that is smaller than or equal to "pa".
1189 * In particular, if D is the domain space of "pa",
1190 * then construct the expression
1194 * impose lower bound "pa" and return
1196 * D[..., i] -> i : i <= pa
1198 static __isl_give isl_pw_aff
*construct_upper(__isl_take isl_pw_aff
*pa
,
1201 return set_upper(init_range(pa
, v
), pa
);
1204 /* Construct a piecewise affine expression corresponding
1205 * to the last variable in "v" that ranges between "pa" and "pa2".
1207 * In particular, if D is the domain space of "pa" (and "pa2"),
1208 * then construct the expression
1212 * impose lower bound "pa" and upper bound "pa2" and return
1214 * D[..., i] -> i : pa <= i <= pa2
1216 static __isl_give isl_pw_aff
*construct_range(__isl_take isl_pw_aff
*pa
,
1217 __isl_take isl_pw_aff
*pa2
, struct vars
*v
)
1219 return set_upper(set_lower(init_range(pa
, v
), pa
), pa2
);
1222 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
1223 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
1225 /* Given that the (piecewise) affine expression "pa"
1226 * has just been parsed, followed by a colon,
1227 * continue parsing as part of a piecewise affine expression.
1229 * In particular, check if the colon is followed by a condition.
1230 * If so, parse the conditions(a) on "pa" and include them in the domain.
1231 * Otherwise, if the colon is followed by another (piecewise) affine expression
1232 * then consider the two expressions as endpoints of a range of values and
1233 * return a piecewise affine expression that takes values in that range.
1234 * Note that an affine expression followed by a comparison operator
1235 * is considered to be part of a condition.
1236 * If the colon is not followed by anything (inside the tuple element),
1237 * then consider "pa" as a lower bound on a range of values without upper bound
1238 * and return a piecewise affine expression that takes values in that range.
1240 static __isl_give isl_pw_aff
*update_piecewise_affine_colon(
1241 __isl_take isl_pw_aff
*pa
, __isl_keep isl_stream
*s
,
1242 struct vars
*v
, int rational
)
1244 isl_space
*dom_space
;
1247 dom_space
= isl_pw_aff_get_domain_space(pa
);
1248 map
= isl_map_universe(isl_space_from_domain(dom_space
));
1250 if (isl_stream_next_token_is(s
, '('))
1251 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1253 if (next_is_end_tuple_element(s
)) {
1255 return construct_lower(pa
, v
);
1257 if (!next_is_condition_start(s
)) {
1258 int line
= -1, col
= -1;
1262 set_current_line_col(s
, &line
, &col
);
1263 space
= isl_space_wrap(isl_map_get_space(map
));
1264 pa2
= accept_affine(s
, space
, v
);
1266 pa2
= isl_pw_aff_set_rational(pa2
);
1267 if (!next_is_comparator(s
)) {
1269 pa2
= isl_pw_aff_domain_factor_domain(pa2
);
1270 return construct_range(pa
, pa2
, v
);
1272 if (push_aff(s
, line
, col
, pa2
) < 0)
1276 map
= read_formula(s
, v
, map
, rational
);
1277 pa
= isl_pw_aff_intersect_domain(pa
, isl_map_domain(map
));
1282 isl_pw_aff_free(pa
);
1286 /* Accept a piecewise affine expression.
1288 * At the outer level, the piecewise affine expression may be of the form
1290 * aff1 : condition1; aff2 : conditions2; ...
1303 * each of the affine expressions may in turn include ternary operators.
1305 * If the first token is a colon, then the expression must be
1306 * ":" or ": aff2", depending on whether anything follows the colon
1307 * inside the tuple element.
1308 * The first is considered to represent an arbitrary value.
1309 * The second is considered to represent a range of values
1310 * with the given upper bound and no lower bound.
1312 * There may be parentheses around some subexpression of "aff1"
1313 * around "aff1" itself, around "aff1 : condition1" and/or
1314 * around the entire piecewise affine expression.
1315 * We therefore remove the opening parenthesis (if any) from the stream
1316 * in case the closing parenthesis follows the colon, but if the closing
1317 * parenthesis is the first thing in the stream after the parsed affine
1318 * expression, we push the parsed expression onto the stream and parse
1319 * again in case the parentheses enclose some subexpression of "aff1".
1321 static __isl_give isl_pw_aff
*accept_piecewise_affine(__isl_keep isl_stream
*s
,
1322 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1325 isl_space
*res_space
;
1327 if (isl_stream_eat_if_available(s
, ':')) {
1328 if (next_is_end_tuple_element(s
))
1329 return identity_tuple_el_on_space(space
, v
);
1331 return construct_upper(accept_affine(s
, space
, v
), v
);
1334 res_space
= isl_space_from_domain(isl_space_copy(space
));
1335 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1336 res
= isl_pw_aff_empty(res_space
);
1340 int line
= -1, col
= -1;
1342 set_current_line_col(s
, &line
, &col
);
1343 seen_paren
= isl_stream_eat_if_available(s
, '(');
1345 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1348 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1350 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1352 if (push_aff(s
, line
, col
, pa
) < 0)
1354 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1357 if (isl_stream_eat_if_available(s
, ':'))
1358 pa
= update_piecewise_affine_colon(pa
, s
, v
, rational
);
1360 res
= isl_pw_aff_union_add(res
, pa
);
1362 if (seen_paren
&& isl_stream_eat(s
, ')'))
1364 } while (isl_stream_eat_if_available(s
, ';'));
1366 isl_space_free(space
);
1370 isl_space_free(space
);
1371 return isl_pw_aff_free(res
);
1374 /* Read an affine expression from "s" for use in read_tuple.
1376 * accept_extended_affine requires a wrapped space as input.
1377 * read_tuple on the other hand expects each isl_pw_aff
1378 * to have an anonymous space. We therefore adjust the space
1379 * of the isl_pw_aff before returning it.
1381 static __isl_give isl_pw_aff
*read_tuple_var_def(__isl_keep isl_stream
*s
,
1382 struct vars
*v
, int rational
)
1387 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1389 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1390 def
= isl_pw_aff_domain_factor_domain(def
);
1395 /* Read a list of tuple elements by calling "read_el" on each of them and
1396 * return a space with the same number of set dimensions derived from
1397 * the parameter space "space" and possibly updated by "read_el".
1398 * The elements in the list are separated by either "," or "][".
1399 * If "comma" is set then only "," is allowed.
1401 static __isl_give isl_space
*read_tuple_list(__isl_keep isl_stream
*s
,
1402 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1403 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1404 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1411 space
= isl_space_set_from_params(space
);
1413 if (isl_stream_next_token_is(s
, ']'))
1417 struct isl_token
*tok
;
1419 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1421 space
= read_el(s
, v
, space
, rational
, user
);
1425 tok
= isl_stream_next_token(s
);
1426 if (!comma
&& tok
&& tok
->type
== ']' &&
1427 isl_stream_next_token_is(s
, '[')) {
1428 isl_token_free(tok
);
1429 tok
= isl_stream_next_token(s
);
1430 } else if (!tok
|| tok
->type
!= ',') {
1432 isl_stream_push_token(s
, tok
);
1436 isl_token_free(tok
);
1442 /* Read a tuple space from "s" derived from the parameter space "space".
1443 * Call "read_el" on each element in the tuples.
1445 static __isl_give isl_space
*read_tuple_space(__isl_keep isl_stream
*s
,
1446 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1447 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1448 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1452 struct isl_token
*tok
;
1454 isl_space
*res
= NULL
;
1456 tok
= isl_stream_next_token(s
);
1459 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1460 name
= strdup(tok
->u
.s
);
1461 isl_token_free(tok
);
1465 isl_stream_push_token(s
, tok
);
1466 if (isl_stream_eat(s
, '['))
1468 if (next_is_tuple(s
)) {
1470 res
= read_tuple_space(s
, v
, isl_space_copy(space
),
1471 rational
, comma
, read_el
, user
);
1472 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1474 out
= read_tuple_space(s
, v
, isl_space_copy(space
),
1475 rational
, comma
, read_el
, user
);
1476 res
= isl_space_product(res
, out
);
1478 res
= read_tuple_list(s
, v
, isl_space_copy(space
),
1479 rational
, comma
, read_el
, user
);
1480 if (isl_stream_eat(s
, ']'))
1484 res
= isl_space_set_tuple_name(res
, isl_dim_set
, name
);
1488 isl_space_free(space
);
1492 isl_space_free(res
);
1493 isl_space_free(space
);
1497 /* Construct an isl_pw_aff defined on a space with v->n variables
1498 * that is equal to the last of those variables.
1500 static __isl_give isl_pw_aff
*identity_tuple_el(struct vars
*v
)
1504 space
= isl_space_set_alloc(v
->ctx
, 0, v
->n
);
1505 return identity_tuple_el_on_space(space
, v
);
1508 /* This function is called for each element in a tuple inside read_tuple.
1509 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1510 * over a space containing all variables in "v" defined so far.
1511 * The isl_pw_aff expresses the new variable in terms of earlier variables
1512 * if a definition is provided. Otherwise, it is represented as being
1514 * Add the isl_pw_aff to *list.
1515 * If the new variable was named, then adjust "space" accordingly and
1516 * return the updated space.
1518 static __isl_give isl_space
*read_tuple_pw_aff_el(__isl_keep isl_stream
*s
,
1519 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1521 isl_pw_aff_list
**list
= (isl_pw_aff_list
**) user
;
1523 struct isl_token
*tok
;
1526 tok
= next_token(s
);
1528 isl_stream_error(s
, NULL
, "unexpected EOF");
1529 return isl_space_free(space
);
1532 if (tok
->type
== ISL_TOKEN_IDENT
) {
1534 int p
= vars_pos(v
, tok
->u
.s
, -1);
1540 if (tok
->type
== '*') {
1541 if (vars_add_anon(v
) < 0)
1543 isl_token_free(tok
);
1544 pa
= identity_tuple_el(v
);
1545 } else if (new_name
) {
1546 isl_size pos
= isl_space_dim(space
, isl_dim_out
);
1550 space
= space_set_dim_name(space
, pos
, v
->v
->name
);
1551 isl_token_free(tok
);
1552 if (isl_stream_eat_if_available(s
, '='))
1553 pa
= read_tuple_var_def(s
, v
, rational
);
1555 pa
= identity_tuple_el(v
);
1557 isl_stream_push_token(s
, tok
);
1559 if (vars_add_anon(v
) < 0)
1561 pa
= read_tuple_var_def(s
, v
, rational
);
1564 *list
= isl_pw_aff_list_add(*list
, pa
);
1566 return isl_space_free(space
);
1570 isl_token_free(tok
);
1571 return isl_space_free(space
);
1574 /* Read a tuple and represent it as an isl_multi_pw_aff.
1575 * The range space of the isl_multi_pw_aff is the space of the tuple.
1576 * The domain space is an anonymous space
1577 * with a dimension for each variable in the set of variables in "v",
1578 * including the variables in the range.
1579 * If a given dimension is not defined in terms of earlier dimensions in
1580 * the input, then the corresponding isl_pw_aff is set equal to one time
1581 * the variable corresponding to the dimension being defined.
1583 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1584 * Each element in this list is defined over a space representing
1585 * the variables defined so far. We need to adjust the earlier
1586 * elements to have as many variables in the domain as the final
1587 * element in the list.
1589 static __isl_give isl_multi_pw_aff
*read_tuple(__isl_keep isl_stream
*s
,
1590 struct vars
*v
, int rational
, int comma
)
1595 isl_pw_aff_list
*list
;
1597 space
= isl_space_params_alloc(v
->ctx
, 0);
1598 list
= isl_pw_aff_list_alloc(s
->ctx
, 0);
1599 space
= read_tuple_space(s
, v
, space
, rational
, comma
,
1600 &read_tuple_pw_aff_el
, &list
);
1601 n
= isl_space_dim(space
, isl_dim_set
);
1603 space
= isl_space_free(space
);
1604 for (i
= 0; i
+ 1 < n
; ++i
) {
1607 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
1608 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, n
- (i
+ 1));
1609 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
1612 space
= isl_space_from_range(space
);
1613 space
= isl_space_add_dims(space
, isl_dim_in
, v
->n
);
1614 return isl_multi_pw_aff_from_pw_aff_list(space
, list
);
1617 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1618 * We first create the appropriate space in "map" based on the range
1619 * space of this isl_multi_pw_aff. Then, we add equalities based
1620 * on the affine expressions. These live in an anonymous space,
1621 * however, so we first need to reset the space to that of "map".
1623 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1624 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1630 isl_space
*space
= NULL
;
1632 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1635 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1636 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1640 if (type
== isl_dim_param
) {
1641 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1642 isl_space_is_wrapping(space
)) {
1643 isl_die(ctx
, isl_error_invalid
,
1644 "parameter tuples cannot be named or nested",
1647 map
= isl_map_add_dims(map
, type
, n
);
1648 for (i
= 0; i
< n
; ++i
) {
1650 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1651 isl_die(ctx
, isl_error_invalid
,
1652 "parameters must be named",
1654 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1655 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1657 } else if (type
== isl_dim_in
) {
1660 set
= isl_set_universe(isl_space_copy(space
));
1662 set
= isl_set_set_rational(set
);
1663 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1664 map
= isl_map_from_domain(set
);
1668 set
= isl_set_universe(isl_space_copy(space
));
1670 set
= isl_set_set_rational(set
);
1671 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1674 for (i
= 0; i
< n
; ++i
) {
1681 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1682 space
= isl_pw_aff_get_domain_space(pa
);
1683 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1684 aff
= isl_aff_add_coefficient_si(aff
,
1685 isl_dim_in
, v
->n
- n
+ i
, -1);
1686 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1688 pa
= isl_pw_aff_set_rational(pa
);
1689 set
= isl_pw_aff_zero_set(pa
);
1690 map_i
= isl_map_from_range(set
);
1691 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1692 map
= isl_map_intersect(map
, map_i
);
1695 isl_space_free(space
);
1696 isl_multi_pw_aff_free(tuple
);
1699 isl_space_free(space
);
1700 isl_multi_pw_aff_free(tuple
);
1705 /* Read a tuple from "s" and add it to "map".
1706 * The tuple is initially represented as an isl_multi_pw_aff and
1707 * then added to "map".
1709 static __isl_give isl_map
*read_map_tuple(__isl_keep isl_stream
*s
,
1710 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1711 int rational
, int comma
)
1713 isl_multi_pw_aff
*tuple
;
1715 tuple
= read_tuple(s
, v
, rational
, comma
);
1717 return isl_map_free(map
);
1719 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1722 /* Given two equal-length lists of piecewise affine expression with the space
1723 * of "set" as domain, construct a set in the same space that expresses
1724 * that "left" and "right" satisfy the comparison "type".
1726 * A space is constructed of the same dimension as the number of elements
1727 * in the two lists. The comparison is then expressed in a map from
1728 * this space to itself and wrapped into a set. Finally the two lists
1729 * of piecewise affine expressions are plugged into this set.
1731 * Let S be the space of "set" and T the constructed space.
1732 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1733 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1734 * while the comparison is first expressed in T -> T, then [T -> T]
1737 static __isl_give isl_set
*list_cmp(__isl_keep isl_set
*set
, int type
,
1738 __isl_take isl_pw_aff_list
*left
, __isl_take isl_pw_aff_list
*right
)
1742 isl_multi_pw_aff
*mpa1
, *mpa2
;
1744 n
= isl_pw_aff_list_n_pw_aff(left
);
1745 if (!set
|| n
< 0 || !right
)
1748 space
= isl_set_get_space(set
);
1749 space
= isl_space_from_domain(space
);
1750 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
1751 mpa1
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), left
);
1752 mpa2
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), right
);
1753 mpa1
= isl_multi_pw_aff_range_product(mpa1
, mpa2
);
1755 space
= isl_space_range(space
);
1757 case ISL_TOKEN_LEX_LT
:
1758 set
= isl_map_wrap(isl_map_lex_lt(space
));
1760 case ISL_TOKEN_LEX_GT
:
1761 set
= isl_map_wrap(isl_map_lex_gt(space
));
1763 case ISL_TOKEN_LEX_LE
:
1764 set
= isl_map_wrap(isl_map_lex_le(space
));
1766 case ISL_TOKEN_LEX_GE
:
1767 set
= isl_map_wrap(isl_map_lex_ge(space
));
1770 isl_multi_pw_aff_free(mpa1
);
1771 isl_space_free(space
);
1772 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
1773 "unhandled list comparison type", return NULL
);
1775 set
= isl_set_preimage_multi_pw_aff(set
, mpa1
);
1778 isl_pw_aff_list_free(left
);
1779 isl_pw_aff_list_free(right
);
1783 /* Construct constraints of the form
1787 * where a is an element in "left", op is an operator of type "type" and
1788 * b is an element in "right", add the constraints to "set" and return
1790 * "rational" is set if the constraints should be treated as
1791 * a rational constraints.
1793 * If "type" is the type of a comparison operator between lists
1794 * of affine expressions, then a single (compound) constraint
1795 * is constructed by list_cmp instead.
1797 static __isl_give isl_set
*construct_constraints(
1798 __isl_take isl_set
*set
, int type
,
1799 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1804 left
= isl_pw_aff_list_copy(left
);
1805 right
= isl_pw_aff_list_copy(right
);
1807 left
= isl_pw_aff_list_set_rational(left
);
1808 right
= isl_pw_aff_list_set_rational(right
);
1810 if (is_list_comparator_type(type
))
1811 cond
= list_cmp(set
, type
, left
, right
);
1812 else if (type
== ISL_TOKEN_LE
)
1813 cond
= isl_pw_aff_list_le_set(left
, right
);
1814 else if (type
== ISL_TOKEN_GE
)
1815 cond
= isl_pw_aff_list_ge_set(left
, right
);
1816 else if (type
== ISL_TOKEN_LT
)
1817 cond
= isl_pw_aff_list_lt_set(left
, right
);
1818 else if (type
== ISL_TOKEN_GT
)
1819 cond
= isl_pw_aff_list_gt_set(left
, right
);
1820 else if (type
== ISL_TOKEN_NE
)
1821 cond
= isl_pw_aff_list_ne_set(left
, right
);
1823 cond
= isl_pw_aff_list_eq_set(left
, right
);
1825 return isl_set_intersect(set
, cond
);
1828 /* Read a constraint from "s", add it to "map" and return the result.
1829 * "v" contains a description of the identifiers parsed so far.
1830 * "rational" is set if the constraint should be treated as
1831 * a rational constraint.
1832 * The constraint read from "s" may be applied to multiple pairs
1833 * of affine expressions and may be chained.
1834 * In particular, a list of affine expressions is read, followed
1835 * by a comparison operator and another list of affine expressions.
1836 * The comparison operator is then applied to each pair of elements
1837 * in the two lists and the results are added to "map".
1838 * However, if the operator expects two lists of affine expressions,
1839 * then it is applied directly to those lists and the two lists
1840 * are required to have the same length.
1841 * If the next token is another comparison operator, then another
1842 * list of affine expressions is read and the process repeats.
1844 * The processing is performed on a wrapped copy of "map" because
1845 * an affine expression cannot have a binary relation as domain.
1847 static __isl_give isl_map
*add_constraint(__isl_keep isl_stream
*s
,
1848 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1850 struct isl_token
*tok
;
1852 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
1856 set
= isl_map_wrap(map
);
1857 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1860 tok
= isl_stream_next_token(s
);
1861 if (!is_comparator(tok
)) {
1862 isl_stream_error(s
, tok
, "missing operator");
1864 isl_stream_push_token(s
, tok
);
1868 isl_token_free(tok
);
1870 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
1871 n1
= isl_pw_aff_list_n_pw_aff(list1
);
1872 n2
= isl_pw_aff_list_n_pw_aff(list2
);
1873 if (n1
< 0 || n2
< 0)
1875 if (is_list_comparator_type(type
) && n1
!= n2
) {
1876 isl_stream_error(s
, NULL
,
1877 "list arguments not of same size");
1881 set
= construct_constraints(set
, type
, list1
, list2
, rational
);
1882 isl_pw_aff_list_free(list1
);
1885 if (!next_is_comparator(s
))
1887 tok
= isl_stream_next_token(s
);
1889 isl_token_free(tok
);
1891 isl_pw_aff_list_free(list1
);
1893 return isl_set_unwrap(set
);
1895 isl_pw_aff_list_free(list1
);
1896 isl_pw_aff_list_free(list2
);
1901 static __isl_give isl_map
*read_exists(__isl_keep isl_stream
*s
,
1902 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1905 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1907 map
= isl_map_from_domain(isl_map_wrap(map
));
1908 map
= read_defined_var_list(s
, v
, map
, rational
);
1910 if (isl_stream_eat(s
, ':'))
1913 map
= read_formula(s
, v
, map
, rational
);
1914 map
= isl_set_unwrap(isl_map_domain(map
));
1916 vars_drop(v
, v
->n
- n
);
1917 if (seen_paren
&& isl_stream_eat(s
, ')'))
1926 /* Parse an expression between parentheses and push the result
1927 * back on the stream.
1929 * The parsed expression may be either an affine expression
1930 * or a condition. The first type is pushed onto the stream
1931 * as an isl_pw_aff, while the second is pushed as an isl_map.
1933 * If the initial token indicates the start of a condition,
1934 * we parse it as such.
1935 * Otherwise, we first parse an affine expression and push
1936 * that onto the stream. If the affine expression covers the
1937 * entire expression between parentheses, we return.
1938 * Otherwise, we assume that the affine expression is the
1939 * start of a condition and continue parsing.
1941 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
1942 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1944 struct isl_token
*tok
, *tok2
;
1949 tok
= isl_stream_next_token(s
);
1950 if (!tok
|| tok
->type
!= '(')
1953 if (isl_stream_next_token_is(s
, '('))
1954 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1957 if (next_is_condition_start(s
)) {
1958 map
= read_formula(s
, v
, map
, rational
);
1959 if (isl_stream_eat(s
, ')'))
1961 tok
->type
= ISL_TOKEN_MAP
;
1963 isl_stream_push_token(s
, tok
);
1967 tok2
= isl_stream_next_token(s
);
1972 isl_stream_push_token(s
, tok2
);
1974 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1978 has_paren
= isl_stream_eat_if_available(s
, ')');
1980 if (push_aff(s
, line
, col
, pwaff
) < 0)
1984 isl_token_free(tok
);
1989 map
= read_formula(s
, v
, map
, rational
);
1990 if (isl_stream_eat(s
, ')'))
1993 tok
->type
= ISL_TOKEN_MAP
;
1995 isl_stream_push_token(s
, tok
);
1999 isl_token_free(tok
);
2004 static __isl_give isl_map
*read_conjunct(__isl_keep isl_stream
*s
,
2005 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2007 if (isl_stream_next_token_is(s
, '('))
2008 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
2011 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
2012 struct isl_token
*tok
;
2013 tok
= isl_stream_next_token(s
);
2017 map
= isl_map_copy(tok
->u
.map
);
2018 isl_token_free(tok
);
2022 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
2023 return read_exists(s
, v
, map
, rational
);
2025 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
2028 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
2029 isl_space
*space
= isl_map_get_space(map
);
2031 return isl_map_empty(space
);
2034 return add_constraint(s
, v
, map
, rational
);
2040 static __isl_give isl_map
*read_conjuncts(__isl_keep isl_stream
*s
,
2041 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2046 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
2047 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
2049 res
= isl_map_subtract(isl_map_copy(map
), res
);
2051 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
2054 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
2055 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
2057 res
= isl_map_subtract(res
, res_i
);
2059 res
= isl_map_intersect(res
, res_i
);
2066 static __isl_give isl_map
*read_disjuncts(__isl_keep isl_stream
*s
,
2067 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2071 if (isl_stream_next_token_is(s
, '}'))
2074 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
2075 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
2078 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
2079 res
= isl_map_union(res
, res_i
);
2086 /* Read a first order formula from "s", add the corresponding
2087 * constraints to "map" and return the result.
2089 * In particular, read a formula of the form
2097 * where a and b are disjunctions.
2099 * In the first case, map is replaced by
2101 * map \cap { [..] : a }
2103 * In the second case, it is replaced by
2105 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
2107 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
2108 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2112 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
2114 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
2117 res
= isl_map_subtract(isl_map_copy(map
), res
);
2118 res2
= read_disjuncts(s
, v
, map
, rational
);
2119 res
= isl_map_union(res
, res2
);
2126 static isl_size
polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
2128 isl_size n_out
, n_in
, n_param
, n_div
;
2130 n_param
= isl_basic_map_dim(bmap
, isl_dim_param
);
2131 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
2132 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
2133 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2134 if (n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
2135 return isl_size_error
;
2138 return 1 + n_param
+ n_in
+ pos
;
2142 return 1 + n_param
+ pos
;
2146 return 1 + n_param
+ n_in
+ n_out
+ pos
;
2155 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
2156 __isl_keep isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
2159 struct isl_token
*tok
;
2168 tok
= isl_stream_next_token(s
);
2169 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2170 isl_stream_error(s
, tok
, "expecting coefficient");
2172 isl_stream_push_token(s
, tok
);
2175 if (!tok
->on_new_line
) {
2176 isl_stream_error(s
, tok
, "coefficient should appear on new line");
2177 isl_stream_push_token(s
, tok
);
2181 type
= isl_int_get_si(tok
->u
.v
);
2182 isl_token_free(tok
);
2184 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
2186 k
= isl_basic_map_alloc_equality(bmap
);
2189 k
= isl_basic_map_alloc_inequality(bmap
);
2195 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2197 return isl_basic_map_free(bmap
);
2198 for (j
= 0; j
< 1 + total
; ++j
) {
2200 tok
= isl_stream_next_token(s
);
2201 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2202 isl_stream_error(s
, tok
, "expecting coefficient");
2204 isl_stream_push_token(s
, tok
);
2207 if (tok
->on_new_line
) {
2208 isl_stream_error(s
, tok
,
2209 "coefficient should not appear on new line");
2210 isl_stream_push_token(s
, tok
);
2213 pos
= polylib_pos_to_isl_pos(bmap
, j
);
2215 isl_int_set(c
[pos
], tok
->u
.v
);
2216 isl_token_free(tok
);
2218 return isl_basic_map_free(bmap
);
2223 isl_basic_map_free(bmap
);
2227 static __isl_give isl_basic_map
*basic_map_read_polylib(
2228 __isl_keep isl_stream
*s
)
2231 struct isl_token
*tok
;
2232 struct isl_token
*tok2
;
2235 unsigned in
= 0, out
, local
= 0;
2236 struct isl_basic_map
*bmap
= NULL
;
2239 tok
= isl_stream_next_token(s
);
2241 isl_stream_error(s
, NULL
, "unexpected EOF");
2244 tok2
= isl_stream_next_token(s
);
2246 isl_token_free(tok
);
2247 isl_stream_error(s
, NULL
, "unexpected EOF");
2250 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2251 isl_stream_push_token(s
, tok2
);
2252 isl_stream_push_token(s
, tok
);
2253 isl_stream_error(s
, NULL
,
2254 "expecting constraint matrix dimensions");
2257 n_row
= isl_int_get_si(tok
->u
.v
);
2258 n_col
= isl_int_get_si(tok2
->u
.v
);
2259 on_new_line
= tok2
->on_new_line
;
2260 isl_token_free(tok2
);
2261 isl_token_free(tok
);
2262 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
2263 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
2264 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
2265 tok
= isl_stream_next_token_on_same_line(s
);
2267 if (tok
->type
!= ISL_TOKEN_VALUE
) {
2268 isl_stream_error(s
, tok
,
2269 "expecting number of output dimensions");
2270 isl_stream_push_token(s
, tok
);
2273 out
= isl_int_get_si(tok
->u
.v
);
2274 isl_token_free(tok
);
2276 tok
= isl_stream_next_token_on_same_line(s
);
2277 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2278 isl_stream_error(s
, tok
,
2279 "expecting number of input dimensions");
2281 isl_stream_push_token(s
, tok
);
2284 in
= isl_int_get_si(tok
->u
.v
);
2285 isl_token_free(tok
);
2287 tok
= isl_stream_next_token_on_same_line(s
);
2288 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2289 isl_stream_error(s
, tok
,
2290 "expecting number of existentials");
2292 isl_stream_push_token(s
, tok
);
2295 local
= isl_int_get_si(tok
->u
.v
);
2296 isl_token_free(tok
);
2298 tok
= isl_stream_next_token_on_same_line(s
);
2299 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2300 isl_stream_error(s
, tok
,
2301 "expecting number of parameters");
2303 isl_stream_push_token(s
, tok
);
2306 nparam
= isl_int_get_si(tok
->u
.v
);
2307 isl_token_free(tok
);
2308 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
2309 isl_stream_error(s
, NULL
,
2310 "dimensions don't match");
2314 out
= n_col
- 2 - nparam
;
2315 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
2319 for (i
= 0; i
< local
; ++i
) {
2320 int k
= isl_basic_map_alloc_div(bmap
);
2323 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
2326 for (i
= 0; i
< n_row
; ++i
)
2327 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
2329 tok
= isl_stream_next_token_on_same_line(s
);
2331 isl_stream_error(s
, tok
, "unexpected extra token on line");
2332 isl_stream_push_token(s
, tok
);
2336 bmap
= isl_basic_map_simplify(bmap
);
2337 bmap
= isl_basic_map_finalize(bmap
);
2340 isl_basic_map_free(bmap
);
2344 static __isl_give isl_map
*map_read_polylib(__isl_keep isl_stream
*s
)
2346 struct isl_token
*tok
;
2347 struct isl_token
*tok2
;
2349 struct isl_map
*map
;
2351 tok
= isl_stream_next_token(s
);
2353 isl_stream_error(s
, NULL
, "unexpected EOF");
2356 tok2
= isl_stream_next_token_on_same_line(s
);
2357 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
2358 isl_stream_push_token(s
, tok2
);
2359 isl_stream_push_token(s
, tok
);
2360 return isl_map_from_basic_map(basic_map_read_polylib(s
));
2363 isl_stream_error(s
, tok2
, "unexpected token");
2364 isl_stream_push_token(s
, tok2
);
2365 isl_stream_push_token(s
, tok
);
2368 n
= isl_int_get_si(tok
->u
.v
);
2369 isl_token_free(tok
);
2371 isl_assert(s
->ctx
, n
>= 1, return NULL
);
2373 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
2375 for (i
= 1; map
&& i
< n
; ++i
)
2376 map
= isl_map_union(map
,
2377 isl_map_from_basic_map(basic_map_read_polylib(s
)));
2382 static int optional_power(__isl_keep isl_stream
*s
)
2385 struct isl_token
*tok
;
2387 tok
= isl_stream_next_token(s
);
2390 if (tok
->type
!= '^') {
2391 isl_stream_push_token(s
, tok
);
2394 isl_token_free(tok
);
2395 tok
= isl_stream_next_token(s
);
2396 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2397 isl_stream_error(s
, tok
, "expecting exponent");
2399 isl_stream_push_token(s
, tok
);
2402 pow
= isl_int_get_si(tok
->u
.v
);
2403 isl_token_free(tok
);
2407 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2408 __isl_keep isl_map
*map
, struct vars
*v
);
2410 static __isl_give isl_pw_qpolynomial
*read_factor(__isl_keep isl_stream
*s
,
2411 __isl_keep isl_map
*map
, struct vars
*v
)
2413 isl_pw_qpolynomial
*pwqp
;
2414 struct isl_token
*tok
;
2416 tok
= next_token(s
);
2418 isl_stream_error(s
, NULL
, "unexpected EOF");
2421 if (tok
->type
== '(') {
2424 isl_token_free(tok
);
2425 pwqp
= read_term(s
, map
, v
);
2428 if (isl_stream_eat(s
, ')'))
2430 pow
= optional_power(s
);
2431 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2432 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
2433 struct isl_token
*tok2
;
2434 isl_qpolynomial
*qp
;
2436 tok2
= isl_stream_next_token(s
);
2437 if (tok2
&& tok2
->type
== '/') {
2438 isl_token_free(tok2
);
2439 tok2
= next_token(s
);
2440 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2441 isl_stream_error(s
, tok2
, "expected denominator");
2442 isl_token_free(tok
);
2443 isl_token_free(tok2
);
2446 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
2447 tok
->u
.v
, tok2
->u
.v
);
2448 isl_token_free(tok2
);
2450 isl_stream_push_token(s
, tok2
);
2451 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
2454 isl_token_free(tok
);
2455 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2456 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2457 isl_qpolynomial
*qp
;
2458 isl_token_free(tok
);
2459 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2460 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2461 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2462 isl_qpolynomial
*qp
;
2463 isl_token_free(tok
);
2464 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2465 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2466 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2468 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2470 isl_qpolynomial
*qp
;
2472 isl_token_free(tok
);
2476 vars_drop(v
, v
->n
- n
);
2477 isl_stream_error(s
, tok
, "unknown identifier");
2478 isl_token_free(tok
);
2481 isl_token_free(tok
);
2482 pow
= optional_power(s
);
2483 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2484 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2485 } else if (is_start_of_div(tok
)) {
2489 isl_stream_push_token(s
, tok
);
2490 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2491 pow
= optional_power(s
);
2492 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2493 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2494 } else if (tok
->type
== '-') {
2495 isl_token_free(tok
);
2496 pwqp
= read_factor(s
, map
, v
);
2497 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2499 isl_stream_error(s
, tok
, "unexpected isl_token");
2500 isl_stream_push_token(s
, tok
);
2504 if (isl_stream_eat_if_available(s
, '*') ||
2505 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2506 isl_pw_qpolynomial
*pwqp2
;
2508 pwqp2
= read_factor(s
, map
, v
);
2509 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2514 isl_pw_qpolynomial_free(pwqp
);
2518 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2519 __isl_keep isl_map
*map
, struct vars
*v
)
2521 struct isl_token
*tok
;
2522 isl_pw_qpolynomial
*pwqp
;
2524 pwqp
= read_factor(s
, map
, v
);
2527 tok
= next_token(s
);
2531 if (tok
->type
== '+') {
2532 isl_pw_qpolynomial
*pwqp2
;
2534 isl_token_free(tok
);
2535 pwqp2
= read_factor(s
, map
, v
);
2536 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2537 } else if (tok
->type
== '-') {
2538 isl_pw_qpolynomial
*pwqp2
;
2540 isl_token_free(tok
);
2541 pwqp2
= read_factor(s
, map
, v
);
2542 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2543 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2544 isl_int_is_neg(tok
->u
.v
)) {
2545 isl_pw_qpolynomial
*pwqp2
;
2547 isl_stream_push_token(s
, tok
);
2548 pwqp2
= read_factor(s
, map
, v
);
2549 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2551 isl_stream_push_token(s
, tok
);
2559 static __isl_give isl_map
*read_optional_formula(__isl_keep isl_stream
*s
,
2560 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2562 struct isl_token
*tok
;
2564 tok
= isl_stream_next_token(s
);
2566 isl_stream_error(s
, NULL
, "unexpected EOF");
2569 if (tok
->type
== ':' ||
2570 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2571 isl_token_free(tok
);
2572 map
= read_formula(s
, v
, map
, rational
);
2574 isl_stream_push_token(s
, tok
);
2582 static struct isl_obj
obj_read_poly(__isl_keep isl_stream
*s
,
2583 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2585 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2586 isl_pw_qpolynomial
*pwqp
;
2587 struct isl_set
*set
;
2589 pwqp
= read_term(s
, map
, v
);
2590 map
= read_optional_formula(s
, map
, v
, 0);
2591 set
= isl_map_range(map
);
2593 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2595 vars_drop(v
, v
->n
- n
);
2601 static struct isl_obj
obj_read_poly_or_fold(__isl_keep isl_stream
*s
,
2602 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2605 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2606 isl_pw_qpolynomial
*pwqp
;
2607 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2610 max
= isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
);
2611 min
= !max
&& isl_stream_eat_if_available(s
, ISL_TOKEN_MIN
);
2613 return obj_read_poly(s
, set
, v
, n
);
2614 fold
= max
? isl_fold_max
: isl_fold_min
;
2616 if (isl_stream_eat(s
, '('))
2619 pwqp
= read_term(s
, set
, v
);
2620 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold
, pwqp
);
2622 while (isl_stream_eat_if_available(s
, ',')) {
2623 isl_pw_qpolynomial_fold
*pwf_i
;
2624 pwqp
= read_term(s
, set
, v
);
2625 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold
, pwqp
);
2626 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2629 if (isl_stream_eat(s
, ')'))
2632 set
= read_optional_formula(s
, set
, v
, 0);
2633 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2635 vars_drop(v
, v
->n
- n
);
2641 isl_pw_qpolynomial_fold_free(pwf
);
2642 obj
.type
= isl_obj_none
;
2646 static int is_rational(__isl_keep isl_stream
*s
)
2648 struct isl_token
*tok
;
2650 tok
= isl_stream_next_token(s
);
2653 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2654 isl_token_free(tok
);
2655 isl_stream_eat(s
, ':');
2659 isl_stream_push_token(s
, tok
);
2664 static struct isl_obj
obj_read_body(__isl_keep isl_stream
*s
,
2665 __isl_take isl_map
*map
, struct vars
*v
)
2667 struct isl_token
*tok
;
2668 struct isl_obj obj
= { isl_obj_set
, NULL
};
2672 rational
= is_rational(s
);
2674 map
= isl_map_set_rational(map
);
2676 if (isl_stream_next_token_is(s
, ':')) {
2677 obj
.type
= isl_obj_set
;
2678 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2682 if (!next_is_tuple(s
))
2683 return obj_read_poly_or_fold(s
, map
, v
, n
);
2685 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2688 tok
= isl_stream_next_token(s
);
2691 if (tok
->type
== ISL_TOKEN_TO
) {
2692 obj
.type
= isl_obj_map
;
2693 isl_token_free(tok
);
2694 if (!next_is_tuple(s
)) {
2695 isl_set
*set
= isl_map_domain(map
);
2696 return obj_read_poly_or_fold(s
, set
, v
, n
);
2698 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2702 map
= isl_map_domain(map
);
2703 isl_stream_push_token(s
, tok
);
2706 map
= read_optional_formula(s
, map
, v
, rational
);
2708 vars_drop(v
, v
->n
- n
);
2714 obj
.type
= isl_obj_none
;
2718 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2720 if (obj
.type
== isl_obj_map
) {
2721 obj
.v
= isl_union_map_from_map(obj
.v
);
2722 obj
.type
= isl_obj_union_map
;
2723 } else if (obj
.type
== isl_obj_set
) {
2724 obj
.v
= isl_union_set_from_set(obj
.v
);
2725 obj
.type
= isl_obj_union_set
;
2726 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2727 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2728 obj
.type
= isl_obj_union_pw_qpolynomial
;
2729 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2730 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2731 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2733 isl_assert(ctx
, 0, goto error
);
2736 obj
.type
->free(obj
.v
);
2737 obj
.type
= isl_obj_none
;
2741 static struct isl_obj
obj_add(__isl_keep isl_stream
*s
,
2742 struct isl_obj obj1
, struct isl_obj obj2
)
2744 if (obj2
.type
== isl_obj_none
|| !obj2
.v
)
2746 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2747 obj1
= to_union(s
->ctx
, obj1
);
2748 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2749 obj2
= to_union(s
->ctx
, obj2
);
2750 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2751 obj1
= to_union(s
->ctx
, obj1
);
2752 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2753 obj2
= to_union(s
->ctx
, obj2
);
2754 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2755 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2756 obj1
= to_union(s
->ctx
, obj1
);
2757 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2758 obj2
.type
== isl_obj_pw_qpolynomial
)
2759 obj2
= to_union(s
->ctx
, obj2
);
2760 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2761 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2762 obj1
= to_union(s
->ctx
, obj1
);
2763 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2764 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2765 obj2
= to_union(s
->ctx
, obj2
);
2766 if (obj1
.type
!= obj2
.type
) {
2767 isl_stream_error(s
, NULL
,
2768 "attempt to combine incompatible objects");
2771 if (!obj1
.type
->add
)
2772 isl_die(s
->ctx
, isl_error_internal
,
2773 "combination not supported on object type", goto error
);
2774 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2775 obj1
= to_union(s
->ctx
, obj1
);
2776 obj2
= to_union(s
->ctx
, obj2
);
2778 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2779 obj1
= to_union(s
->ctx
, obj1
);
2780 obj2
= to_union(s
->ctx
, obj2
);
2782 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2783 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2784 obj1
= to_union(s
->ctx
, obj1
);
2785 obj2
= to_union(s
->ctx
, obj2
);
2787 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2788 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2789 obj1
= to_union(s
->ctx
, obj1
);
2790 obj2
= to_union(s
->ctx
, obj2
);
2792 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2795 obj1
.type
->free(obj1
.v
);
2796 obj2
.type
->free(obj2
.v
);
2797 obj1
.type
= isl_obj_none
;
2802 /* Are the first two tokens on "s", "domain" (either as a string
2803 * or as an identifier) followed by ":"?
2805 static int next_is_domain_colon(__isl_keep isl_stream
*s
)
2807 struct isl_token
*tok
;
2811 tok
= isl_stream_next_token(s
);
2814 if (tok
->type
!= ISL_TOKEN_IDENT
&& tok
->type
!= ISL_TOKEN_STRING
) {
2815 isl_stream_push_token(s
, tok
);
2819 name
= isl_token_get_str(s
->ctx
, tok
);
2820 res
= !strcmp(name
, "domain") && isl_stream_next_token_is(s
, ':');
2823 isl_stream_push_token(s
, tok
);
2828 /* Do the first tokens on "s" look like a schedule?
2830 * The root of a schedule is always a domain node, so the first thing
2831 * we expect in the stream is a domain key, i.e., "domain" followed
2832 * by ":". If the schedule was printed in YAML flow style, then
2833 * we additionally expect a "{" to open the outer mapping.
2835 static int next_is_schedule(__isl_keep isl_stream
*s
)
2837 struct isl_token
*tok
;
2840 tok
= isl_stream_next_token(s
);
2843 if (tok
->type
!= '{') {
2844 isl_stream_push_token(s
, tok
);
2845 return next_is_domain_colon(s
);
2848 is_schedule
= next_is_domain_colon(s
);
2849 isl_stream_push_token(s
, tok
);
2854 /* Read an isl_schedule from "s" and store it in an isl_obj.
2856 static struct isl_obj
schedule_read(__isl_keep isl_stream
*s
)
2860 obj
.type
= isl_obj_schedule
;
2861 obj
.v
= isl_stream_read_schedule(s
);
2866 /* Read a disjunction of object bodies from "s".
2867 * That is, read the inside of the braces, but not the braces themselves.
2868 * "v" contains a description of the identifiers parsed so far.
2869 * "map" contains information about the parameters.
2871 static struct isl_obj
obj_read_disjuncts(__isl_keep isl_stream
*s
,
2872 struct vars
*v
, __isl_keep isl_map
*map
)
2874 struct isl_obj obj
= { isl_obj_set
, NULL
};
2876 if (isl_stream_next_token_is(s
, '}')) {
2877 obj
.type
= isl_obj_union_set
;
2878 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
2884 o
= obj_read_body(s
, isl_map_copy(map
), v
);
2888 obj
= obj_add(s
, obj
, o
);
2889 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2891 if (!isl_stream_eat_if_available(s
, ';'))
2893 if (isl_stream_next_token_is(s
, '}'))
2900 static struct isl_obj
obj_read(__isl_keep isl_stream
*s
)
2902 isl_map
*map
= NULL
;
2903 struct isl_token
*tok
;
2904 struct vars
*v
= NULL
;
2905 struct isl_obj obj
= { isl_obj_set
, NULL
};
2907 if (next_is_schedule(s
))
2908 return schedule_read(s
);
2910 tok
= next_token(s
);
2912 isl_stream_error(s
, NULL
, "unexpected EOF");
2915 if (tok
->type
== ISL_TOKEN_VALUE
) {
2916 struct isl_token
*tok2
;
2917 struct isl_map
*map
;
2919 tok2
= isl_stream_next_token(s
);
2920 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
2921 isl_int_is_neg(tok2
->u
.v
)) {
2923 isl_stream_push_token(s
, tok2
);
2924 obj
.type
= isl_obj_val
;
2925 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
2926 isl_token_free(tok
);
2929 isl_stream_push_token(s
, tok2
);
2930 isl_stream_push_token(s
, tok
);
2931 map
= map_read_polylib(s
);
2934 if (isl_map_may_be_set(map
))
2935 obj
.v
= isl_map_range(map
);
2937 obj
.type
= isl_obj_map
;
2942 v
= vars_new(s
->ctx
);
2944 isl_stream_push_token(s
, tok
);
2947 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
2948 if (tok
->type
== '[') {
2949 isl_stream_push_token(s
, tok
);
2950 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
2953 tok
= isl_stream_next_token(s
);
2954 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
2955 isl_stream_error(s
, tok
, "expecting '->'");
2957 isl_stream_push_token(s
, tok
);
2960 isl_token_free(tok
);
2961 tok
= isl_stream_next_token(s
);
2963 if (!tok
|| tok
->type
!= '{') {
2964 isl_stream_error(s
, tok
, "expecting '{'");
2966 isl_stream_push_token(s
, tok
);
2969 isl_token_free(tok
);
2971 tok
= isl_stream_next_token(s
);
2974 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
2975 isl_token_free(tok
);
2976 if (isl_stream_eat(s
, '='))
2978 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
2982 isl_stream_push_token(s
, tok
);
2984 obj
= obj_read_disjuncts(s
, v
, map
);
2985 if (obj
.type
== isl_obj_none
|| !obj
.v
)
2988 tok
= isl_stream_next_token(s
);
2989 if (tok
&& tok
->type
== '}') {
2990 isl_token_free(tok
);
2992 isl_stream_error(s
, tok
, "unexpected isl_token");
2994 isl_token_free(tok
);
3004 obj
.type
->free(obj
.v
);
3011 struct isl_obj
isl_stream_read_obj(__isl_keep isl_stream
*s
)
3016 __isl_give isl_map
*isl_stream_read_map(__isl_keep isl_stream
*s
)
3022 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
3023 obj
.type
== isl_obj_set
, goto error
);
3025 if (obj
.type
== isl_obj_set
)
3026 obj
.v
= isl_map_from_range(obj
.v
);
3030 obj
.type
->free(obj
.v
);
3034 __isl_give isl_set
*isl_stream_read_set(__isl_keep isl_stream
*s
)
3040 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
3041 obj
.v
= isl_map_range(obj
.v
);
3042 obj
.type
= isl_obj_set
;
3044 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
3049 obj
.type
->free(obj
.v
);
3053 __isl_give isl_union_map
*isl_stream_read_union_map(__isl_keep isl_stream
*s
)
3058 if (obj
.type
== isl_obj_map
) {
3059 obj
.type
= isl_obj_union_map
;
3060 obj
.v
= isl_union_map_from_map(obj
.v
);
3062 if (obj
.type
== isl_obj_set
) {
3063 obj
.type
= isl_obj_union_set
;
3064 obj
.v
= isl_union_set_from_set(obj
.v
);
3066 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
3067 isl_union_set_is_empty(obj
.v
))
3068 obj
.type
= isl_obj_union_map
;
3069 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
3070 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
3074 obj
.type
->free(obj
.v
);
3078 /* Extract an isl_union_set from "obj".
3079 * This only works if the object was detected as either a set
3080 * (in which case it is converted to a union set) or a union set.
3082 static __isl_give isl_union_set
*extract_union_set(isl_ctx
*ctx
,
3085 if (obj
.type
== isl_obj_set
) {
3086 obj
.type
= isl_obj_union_set
;
3087 obj
.v
= isl_union_set_from_set(obj
.v
);
3090 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
3094 obj
.type
->free(obj
.v
);
3098 /* Read an isl_union_set from "s".
3099 * First read a generic object and then try and extract
3100 * an isl_union_set from that.
3102 __isl_give isl_union_set
*isl_stream_read_union_set(__isl_keep isl_stream
*s
)
3107 return extract_union_set(s
->ctx
, obj
);
3110 static __isl_give isl_basic_map
*basic_map_read(__isl_keep isl_stream
*s
)
3113 struct isl_map
*map
;
3114 struct isl_basic_map
*bmap
;
3117 if (obj
.v
&& (obj
.type
!= isl_obj_map
&& obj
.type
!= isl_obj_set
))
3118 isl_die(s
->ctx
, isl_error_invalid
, "not a (basic) set or map",
3125 isl_die(s
->ctx
, isl_error_invalid
,
3126 "set or map description involves "
3127 "more than one disjunct", goto error
);
3130 bmap
= isl_basic_map_empty(isl_map_get_space(map
));
3132 bmap
= isl_basic_map_copy(map
->p
[0]);
3138 obj
.type
->free(obj
.v
);
3142 static __isl_give isl_basic_set
*basic_set_read(__isl_keep isl_stream
*s
)
3144 isl_basic_map
*bmap
;
3145 bmap
= basic_map_read(s
);
3148 if (!isl_basic_map_may_be_set(bmap
))
3149 isl_die(s
->ctx
, isl_error_invalid
,
3150 "input is not a set", goto error
);
3151 return isl_basic_map_range(bmap
);
3153 isl_basic_map_free(bmap
);
3157 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
3160 struct isl_basic_map
*bmap
;
3161 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3164 bmap
= basic_map_read(s
);
3169 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
3172 isl_basic_set
*bset
;
3173 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3176 bset
= basic_set_read(s
);
3181 __isl_give isl_basic_map
*isl_basic_map_read_from_str(isl_ctx
*ctx
,
3184 struct isl_basic_map
*bmap
;
3185 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3188 bmap
= basic_map_read(s
);
3193 __isl_give isl_basic_set
*isl_basic_set_read_from_str(isl_ctx
*ctx
,
3196 isl_basic_set
*bset
;
3197 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3200 bset
= basic_set_read(s
);
3205 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
3208 struct isl_map
*map
;
3209 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3212 map
= isl_stream_read_map(s
);
3217 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
3220 struct isl_map
*map
;
3221 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3224 map
= isl_stream_read_map(s
);
3229 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
3233 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3236 set
= isl_stream_read_set(s
);
3241 __isl_give isl_set
*isl_set_read_from_str(isl_ctx
*ctx
, const char *str
)
3244 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3247 set
= isl_stream_read_set(s
);
3252 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
3255 isl_union_map
*umap
;
3256 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3259 umap
= isl_stream_read_union_map(s
);
3264 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
3267 isl_union_map
*umap
;
3268 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3271 umap
= isl_stream_read_union_map(s
);
3276 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
3279 isl_union_set
*uset
;
3280 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3283 uset
= isl_stream_read_union_set(s
);
3288 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
3291 isl_union_set
*uset
;
3292 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3295 uset
= isl_stream_read_union_set(s
);
3300 static __isl_give isl_vec
*isl_vec_read_polylib(__isl_keep isl_stream
*s
)
3302 struct isl_vec
*vec
= NULL
;
3303 struct isl_token
*tok
;
3307 tok
= isl_stream_next_token(s
);
3308 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3309 isl_stream_error(s
, tok
, "expecting vector length");
3313 size
= isl_int_get_si(tok
->u
.v
);
3314 isl_token_free(tok
);
3316 vec
= isl_vec_alloc(s
->ctx
, size
);
3318 for (j
= 0; j
< size
; ++j
) {
3319 tok
= isl_stream_next_token(s
);
3320 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3321 isl_stream_error(s
, tok
, "expecting constant value");
3324 isl_int_set(vec
->el
[j
], tok
->u
.v
);
3325 isl_token_free(tok
);
3330 isl_token_free(tok
);
3335 static __isl_give isl_vec
*vec_read(__isl_keep isl_stream
*s
)
3337 return isl_vec_read_polylib(s
);
3340 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
3343 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3351 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
3352 __isl_keep isl_stream
*s
)
3358 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
3363 obj
.type
->free(obj
.v
);
3367 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
3370 isl_pw_qpolynomial
*pwqp
;
3371 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3374 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3379 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
3382 isl_pw_qpolynomial
*pwqp
;
3383 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3386 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3391 /* Read an isl_pw_qpolynomial_fold from "s".
3392 * First read a generic object and
3393 * then check that it is an isl_pw_qpolynomial_fold.
3395 __isl_give isl_pw_qpolynomial_fold
*isl_stream_read_pw_qpolynomial_fold(
3396 __isl_keep isl_stream
*s
)
3401 if (obj
.v
&& obj
.type
!= isl_obj_pw_qpolynomial_fold
)
3402 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
3406 obj
.type
->free(obj
.v
);
3410 /* Read an isl_pw_qpolynomial_fold from "str".
3412 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_read_from_str(
3413 isl_ctx
*ctx
, const char *str
)
3415 isl_pw_qpolynomial_fold
*pwqp
;
3418 s
= isl_stream_new_str(ctx
, str
);
3421 pwqp
= isl_stream_read_pw_qpolynomial_fold(s
);
3427 /* Is the next token an identifier not in "v"?
3429 static int next_is_fresh_ident(__isl_keep isl_stream
*s
, struct vars
*v
)
3433 struct isl_token
*tok
;
3435 tok
= isl_stream_next_token(s
);
3438 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
3439 isl_stream_push_token(s
, tok
);
3441 vars_drop(v
, v
->n
- n
);
3446 /* First read the domain of the affine expression, which may be
3447 * a parameter space or a set.
3448 * The tricky part is that we don't know if the domain is a set or not,
3449 * so when we are trying to read the domain, we may actually be reading
3450 * the affine expression itself (defined on a parameter domains)
3451 * If the tuple we are reading is named, we assume it's the domain.
3452 * Also, if inside the tuple, the first thing we find is a nested tuple
3453 * or a new identifier, we again assume it's the domain.
3454 * Finally, if the tuple is empty, then it must be the domain
3455 * since it does not contain an affine expression.
3456 * Otherwise, we assume we are reading an affine expression.
3458 static __isl_give isl_set
*read_aff_domain(__isl_keep isl_stream
*s
,
3459 __isl_take isl_set
*dom
, struct vars
*v
)
3461 struct isl_token
*tok
, *tok2
;
3464 tok
= isl_stream_next_token(s
);
3465 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
3466 isl_stream_push_token(s
, tok
);
3467 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3469 if (!tok
|| tok
->type
!= '[') {
3470 isl_stream_error(s
, tok
, "expecting '['");
3473 tok2
= isl_stream_next_token(s
);
3474 is_empty
= tok2
&& tok2
->type
== ']';
3476 isl_stream_push_token(s
, tok2
);
3477 if (is_empty
|| next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
3478 isl_stream_push_token(s
, tok
);
3479 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3481 isl_stream_push_token(s
, tok
);
3486 isl_stream_push_token(s
, tok
);
3491 /* Read an affine expression from "s".
3493 __isl_give isl_aff
*isl_stream_read_aff(__isl_keep isl_stream
*s
)
3499 ma
= isl_stream_read_multi_aff(s
);
3500 dim
= isl_multi_aff_dim(ma
, isl_dim_out
);
3504 isl_die(s
->ctx
, isl_error_invalid
,
3505 "expecting single affine expression",
3508 aff
= isl_multi_aff_get_aff(ma
, 0);
3509 isl_multi_aff_free(ma
);
3512 isl_multi_aff_free(ma
);
3516 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3518 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(__isl_keep isl_stream
*s
,
3519 __isl_take isl_set
*dom
, struct vars
*v
)
3521 isl_pw_aff
*pwaff
= NULL
;
3523 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
3526 if (isl_stream_eat(s
, '['))
3529 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
3531 if (isl_stream_eat(s
, ']'))
3534 dom
= read_optional_formula(s
, dom
, v
, 0);
3535 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3540 isl_pw_aff_free(pwaff
);
3544 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(__isl_keep isl_stream
*s
)
3547 isl_set
*dom
= NULL
;
3549 isl_pw_aff
*pa
= NULL
;
3552 v
= vars_new(s
->ctx
);
3556 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3557 if (next_is_tuple(s
)) {
3558 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3559 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3562 if (isl_stream_eat(s
, '{'))
3566 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3567 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3568 vars_drop(v
, v
->n
- n
);
3570 while (isl_stream_eat_if_available(s
, ';')) {
3574 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3575 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3576 vars_drop(v
, v
->n
- n
);
3578 pa
= isl_pw_aff_union_add(pa
, pa_i
);
3581 if (isl_stream_eat(s
, '}'))
3590 isl_pw_aff_free(pa
);
3594 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3597 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3600 aff
= isl_stream_read_aff(s
);
3605 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3608 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3611 pa
= isl_stream_read_pw_aff(s
);
3616 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3617 * from a tuple "tuple" read by read_tuple.
3619 * Note that the function read_tuple accepts tuples where some output or
3620 * set dimensions are defined in terms of other output or set dimensions
3621 * since this function is also used to read maps. As a special case,
3622 * read_tuple also accept dimensions that are defined in terms of themselves
3623 * (i.e., that are not defined).
3624 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3625 * that the definitions of the output/set dimensions do not involve any
3626 * output/set dimensions.
3627 * Finally, drop the output dimensions from the domain of the result
3628 * of read_tuple (which is of the form [input, output] -> [output],
3629 * with anonymous domain) and reset the space.
3631 static __isl_give isl_multi_pw_aff
*extract_mpa_from_tuple(
3632 __isl_take isl_space
*dom_space
, __isl_keep isl_multi_pw_aff
*tuple
)
3637 isl_multi_pw_aff
*mpa
;
3639 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3640 dim
= isl_space_dim(dom_space
, isl_dim_all
);
3641 if (n
< 0 || dim
< 0)
3642 dom_space
= isl_space_free(dom_space
);
3643 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3644 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3645 if (!isl_space_is_params(dom_space
))
3646 space
= isl_space_map_from_domain_and_range(
3647 isl_space_copy(dom_space
), space
);
3648 isl_space_free(dom_space
);
3649 mpa
= isl_multi_pw_aff_alloc(space
);
3651 for (i
= 0; i
< n
; ++i
) {
3653 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3655 return isl_multi_pw_aff_free(mpa
);
3656 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3657 isl_ctx
*ctx
= isl_pw_aff_get_ctx(pa
);
3658 isl_pw_aff_free(pa
);
3659 isl_die(ctx
, isl_error_invalid
,
3660 "not an affine expression",
3661 return isl_multi_pw_aff_free(mpa
));
3663 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3664 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3665 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3666 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3672 /* Read a tuple of affine expressions, together with optional constraints
3673 * on the domain from "s". "dom" represents the initial constraints
3676 * The isl_multi_aff may live in either a set or a map space.
3677 * First read the first tuple and check if it is followed by a "->".
3678 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3679 * read in the next tuple. This tuple (or the first tuple if it was
3680 * not followed by a "->") is then converted into an isl_multi_pw_aff
3681 * through a call to extract_mpa_from_tuple.
3682 * The result is converted to an isl_pw_multi_aff and
3683 * its domain is intersected with the domain.
3685 * Note that the last tuple may introduce new identifiers,
3686 * but these cannot be referenced in the description of the domain.
3688 static __isl_give isl_pw_multi_aff
*read_conditional_multi_aff(
3689 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3691 isl_multi_pw_aff
*tuple
;
3692 isl_multi_pw_aff
*mpa
;
3693 isl_pw_multi_aff
*pma
;
3698 tuple
= read_tuple(s
, v
, 0, 0);
3701 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3702 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3703 dom
= isl_map_domain(map
);
3705 tuple
= read_tuple(s
, v
, 0, 0);
3709 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
3710 isl_multi_pw_aff_free(tuple
);
3712 dom
= isl_set_free(dom
);
3714 vars_drop(v
, v
->n
- n_dom
);
3715 dom
= read_optional_formula(s
, dom
, v
, 0);
3717 vars_drop(v
, v
->n
- n
);
3719 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
3720 pma
= isl_pw_multi_aff_intersect_domain(pma
, dom
);
3728 /* Read an isl_union_pw_multi_aff from "s".
3730 * In particular, first read the parameters and then read a sequence
3731 * of zero or more tuples of affine expressions with optional conditions and
3734 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3735 __isl_keep isl_stream
*s
)
3739 isl_union_pw_multi_aff
*upma
= NULL
;
3741 v
= vars_new(s
->ctx
);
3745 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3746 if (next_is_tuple(s
)) {
3747 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3748 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3751 if (isl_stream_eat(s
, '{'))
3754 upma
= isl_union_pw_multi_aff_empty(isl_set_get_space(dom
));
3757 isl_pw_multi_aff
*pma
;
3758 isl_union_pw_multi_aff
*upma2
;
3760 if (isl_stream_next_token_is(s
, '}'))
3763 pma
= read_conditional_multi_aff(s
, isl_set_copy(dom
), v
);
3764 upma2
= isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
3765 upma
= isl_union_pw_multi_aff_union_add(upma
, upma2
);
3768 } while (isl_stream_eat_if_available(s
, ';'));
3770 if (isl_stream_eat(s
, '}'))
3777 isl_union_pw_multi_aff_free(upma
);
3783 /* Read an isl_pw_multi_aff from "s".
3785 * Read a more generic isl_union_pw_multi_aff first and
3786 * then check that the result lives in a single space.
3788 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(
3789 __isl_keep isl_stream
*s
)
3791 isl_bool single_space
;
3792 isl_union_pw_multi_aff
*upma
;
3794 upma
= isl_stream_read_union_pw_multi_aff(s
);
3795 single_space
= isl_union_pw_multi_aff_isa_pw_multi_aff(upma
);
3796 if (single_space
< 0)
3797 upma
= isl_union_pw_multi_aff_free(upma
);
3798 else if (!single_space
)
3799 isl_die(s
->ctx
, isl_error_invalid
,
3800 "expecting expression in single space",
3801 upma
= isl_union_pw_multi_aff_free(upma
));
3802 return isl_union_pw_multi_aff_as_pw_multi_aff(upma
);
3805 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3808 isl_pw_multi_aff
*pma
;
3809 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3812 pma
= isl_stream_read_pw_multi_aff(s
);
3817 /* Read an isl_union_pw_multi_aff from "str".
3819 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3820 isl_ctx
*ctx
, const char *str
)
3822 isl_union_pw_multi_aff
*upma
;
3823 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3826 upma
= isl_stream_read_union_pw_multi_aff(s
);
3831 /* Assuming "pa" represents a single affine expression defined on a universe
3832 * domain, extract this affine expression.
3834 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3841 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3842 "expecting single affine expression",
3844 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3845 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3846 "expecting universe domain",
3849 aff
= isl_aff_copy(pa
->p
[0].aff
);
3850 isl_pw_aff_free(pa
);
3853 isl_pw_aff_free(pa
);
3860 #include <isl_multi_read_no_explicit_domain_templ.c>
3865 #include <isl_multi_read_no_explicit_domain_templ.c>
3867 /* Read a multi-affine expression from "s".
3868 * If the multi-affine expression has a domain, then the tuple
3869 * representing this domain cannot involve any affine expressions.
3870 * The tuple representing the actual expressions needs to consist
3871 * of only affine expressions. Moreover, these expressions can
3872 * only depend on parameters and input dimensions and not on other
3873 * output dimensions.
3875 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(__isl_keep isl_stream
*s
)
3878 isl_set
*dom
= NULL
;
3879 isl_multi_pw_aff
*tuple
= NULL
;
3882 isl_space
*space
, *dom_space
;
3883 isl_multi_aff
*ma
= NULL
;
3885 v
= vars_new(s
->ctx
);
3889 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3890 if (next_is_tuple(s
)) {
3891 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3892 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3895 if (!isl_set_plain_is_universe(dom
))
3896 isl_die(s
->ctx
, isl_error_invalid
,
3897 "expecting universe parameter domain", goto error
);
3898 if (isl_stream_eat(s
, '{'))
3901 tuple
= read_tuple(s
, v
, 0, 0);
3904 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3909 has_expr
= tuple_has_expr(tuple
);
3913 isl_die(s
->ctx
, isl_error_invalid
,
3914 "expecting universe domain", goto error
);
3915 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3916 set
= isl_set_universe(space
);
3917 dom
= isl_set_intersect_params(set
, dom
);
3918 isl_multi_pw_aff_free(tuple
);
3919 tuple
= read_tuple(s
, v
, 0, 0);
3924 if (isl_stream_eat(s
, '}'))
3927 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3928 dim
= isl_set_dim(dom
, isl_dim_all
);
3929 if (n
< 0 || dim
< 0)
3931 dom_space
= isl_set_get_space(dom
);
3932 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3933 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3934 if (!isl_space_is_params(dom_space
))
3935 space
= isl_space_map_from_domain_and_range(
3936 isl_space_copy(dom_space
), space
);
3937 isl_space_free(dom_space
);
3938 ma
= isl_multi_aff_alloc(space
);
3940 for (i
= 0; i
< n
; ++i
) {
3943 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3944 aff
= aff_from_pw_aff(pa
);
3947 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
3949 isl_die(s
->ctx
, isl_error_invalid
,
3950 "not an affine expression", goto error
);
3952 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
3953 space
= isl_multi_aff_get_domain_space(ma
);
3954 aff
= isl_aff_reset_domain_space(aff
, space
);
3955 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3958 isl_multi_pw_aff_free(tuple
);
3963 isl_multi_pw_aff_free(tuple
);
3966 isl_multi_aff_free(ma
);
3970 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
3973 isl_multi_aff
*maff
;
3974 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3977 maff
= isl_stream_read_multi_aff(s
);
3982 /* Read an isl_multi_pw_aff from "s".
3984 * The input format is similar to that of map, except that any conditions
3985 * on the domains should be specified inside the tuple since each
3986 * piecewise affine expression may have a different domain.
3987 * However, additional, shared conditions can also be specified.
3988 * This is especially useful for setting the explicit domain
3989 * of a zero-dimensional isl_multi_pw_aff.
3991 * Since we do not know in advance if the isl_multi_pw_aff lives
3992 * in a set or a map space, we first read the first tuple and check
3993 * if it is followed by a "->". If so, we convert the tuple into
3994 * the domain of the isl_multi_pw_aff and read in the next tuple.
3995 * This tuple (or the first tuple if it was not followed by a "->")
3996 * is then converted into the isl_multi_pw_aff through a call
3997 * to extract_mpa_from_tuple and the domain of the result
3998 * is intersected with the domain.
4000 * Note that the last tuple may introduce new identifiers,
4001 * but these cannot be referenced in the description of the domain.
4003 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(
4004 __isl_keep isl_stream
*s
)
4008 isl_set
*dom
= NULL
;
4009 isl_multi_pw_aff
*tuple
= NULL
;
4010 isl_multi_pw_aff
*mpa
= NULL
;
4012 v
= vars_new(s
->ctx
);
4016 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4017 if (next_is_tuple(s
)) {
4018 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4019 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4022 if (isl_stream_eat(s
, '{'))
4026 tuple
= read_tuple(s
, v
, 0, 0);
4029 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
4030 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
4031 dom
= isl_map_domain(map
);
4033 tuple
= read_tuple(s
, v
, 0, 0);
4038 vars_drop(v
, v
->n
- n_dom
);
4039 if (isl_stream_eat_if_available(s
, ':'))
4040 dom
= read_formula(s
, v
, dom
, 0);
4042 if (isl_stream_eat(s
, '}'))
4045 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
4047 isl_multi_pw_aff_free(tuple
);
4049 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
4052 isl_multi_pw_aff_free(tuple
);
4055 isl_multi_pw_aff_free(mpa
);
4059 /* Read an isl_multi_pw_aff from "str".
4061 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
4064 isl_multi_pw_aff
*mpa
;
4065 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4068 mpa
= isl_stream_read_multi_pw_aff(s
);
4073 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
4075 static __isl_give isl_union_pw_aff
*read_union_pw_aff_with_dom(
4076 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
4079 isl_union_pw_aff
*upa
= NULL
;
4084 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
4085 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
4086 vars_drop(v
, v
->n
- n
);
4088 upa
= isl_union_pw_aff_from_pw_aff(pa
);
4090 while (isl_stream_eat_if_available(s
, ';')) {
4092 isl_union_pw_aff
*upa_i
;
4095 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
4096 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
4097 vars_drop(v
, v
->n
- n
);
4099 upa_i
= isl_union_pw_aff_from_pw_aff(pa_i
);
4100 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
4107 /* Read an isl_union_pw_aff from "s".
4109 * First check if there are any paramters, then read in the opening brace
4110 * and use read_union_pw_aff_with_dom to read in the body of
4111 * the isl_union_pw_aff. Finally, read the closing brace.
4113 __isl_give isl_union_pw_aff
*isl_stream_read_union_pw_aff(
4114 __isl_keep isl_stream
*s
)
4118 isl_union_pw_aff
*upa
= NULL
;
4120 v
= vars_new(s
->ctx
);
4124 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4125 if (next_is_tuple(s
)) {
4126 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4127 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4130 if (isl_stream_eat(s
, '{'))
4133 upa
= read_union_pw_aff_with_dom(s
, isl_set_copy(dom
), v
);
4135 if (isl_stream_eat(s
, '}'))
4144 isl_union_pw_aff_free(upa
);
4148 /* Read an isl_union_pw_aff from "str".
4150 __isl_give isl_union_pw_aff
*isl_union_pw_aff_read_from_str(isl_ctx
*ctx
,
4153 isl_union_pw_aff
*upa
;
4154 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4157 upa
= isl_stream_read_union_pw_aff(s
);
4162 /* This function is called for each element in a tuple inside
4163 * isl_stream_read_multi_union_pw_aff.
4165 * Read a '{', the union piecewise affine expression body and a '}' and
4166 * add the isl_union_pw_aff to *list.
4168 static __isl_give isl_space
*read_union_pw_aff_el(__isl_keep isl_stream
*s
,
4169 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
4172 isl_union_pw_aff
*upa
;
4173 isl_union_pw_aff_list
**list
= (isl_union_pw_aff_list
**) user
;
4175 dom
= isl_set_universe(isl_space_params(isl_space_copy(space
)));
4176 if (isl_stream_eat(s
, '{'))
4178 upa
= read_union_pw_aff_with_dom(s
, dom
, v
);
4179 *list
= isl_union_pw_aff_list_add(*list
, upa
);
4180 if (isl_stream_eat(s
, '}'))
4181 return isl_space_free(space
);
4183 return isl_space_free(space
);
4187 return isl_space_free(space
);
4190 /* Do the next tokens in "s" correspond to an empty tuple?
4191 * In particular, does the stream start with a '[', followed by a ']',
4192 * not followed by a "->"?
4194 static int next_is_empty_tuple(__isl_keep isl_stream
*s
)
4196 struct isl_token
*tok
, *tok2
, *tok3
;
4197 int is_empty_tuple
= 0;
4199 tok
= isl_stream_next_token(s
);
4202 if (tok
->type
!= '[') {
4203 isl_stream_push_token(s
, tok
);
4207 tok2
= isl_stream_next_token(s
);
4208 if (tok2
&& tok2
->type
== ']') {
4209 tok3
= isl_stream_next_token(s
);
4210 is_empty_tuple
= !tok
|| tok
->type
!= ISL_TOKEN_TO
;
4212 isl_stream_push_token(s
, tok3
);
4215 isl_stream_push_token(s
, tok2
);
4216 isl_stream_push_token(s
, tok
);
4218 return is_empty_tuple
;
4221 /* Do the next tokens in "s" correspond to a tuple of parameters?
4222 * In particular, does the stream start with a '[' that is not
4223 * followed by a '{' or a nested tuple?
4225 static int next_is_param_tuple(__isl_keep isl_stream
*s
)
4227 struct isl_token
*tok
, *tok2
;
4230 tok
= isl_stream_next_token(s
);
4233 if (tok
->type
!= '[' || next_is_tuple(s
)) {
4234 isl_stream_push_token(s
, tok
);
4238 tok2
= isl_stream_next_token(s
);
4239 is_tuple
= tok2
&& tok2
->type
!= '{';
4241 isl_stream_push_token(s
, tok2
);
4242 isl_stream_push_token(s
, tok
);
4247 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
4248 * i.e., everything except the parameter specification and
4249 * without shared domain constraints.
4250 * "v" contains a description of the identifiers parsed so far.
4251 * The parameters, if any, are specified by "space".
4253 * The body is of the form
4255 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4257 * Read the tuple, collecting the individual isl_union_pw_aff
4258 * elements in a list and construct the result from the tuple space and
4261 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body_core(
4262 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
4264 isl_union_pw_aff_list
*list
;
4265 isl_multi_union_pw_aff
*mupa
;
4267 list
= isl_union_pw_aff_list_alloc(s
->ctx
, 0);
4268 space
= read_tuple_space(s
, v
, space
, 1, 0,
4269 &read_union_pw_aff_el
, &list
);
4270 mupa
= isl_multi_union_pw_aff_from_union_pw_aff_list(space
, list
);
4275 /* Read the body of an isl_union_set from "s",
4276 * i.e., everything except the parameter specification.
4277 * "v" contains a description of the identifiers parsed so far.
4278 * The parameters, if any, are specified by "space".
4280 * First read a generic disjunction of object bodies and then try and extract
4281 * an isl_union_set from that.
4283 static __isl_give isl_union_set
*read_union_set_body(__isl_keep isl_stream
*s
,
4284 struct vars
*v
, __isl_take isl_space
*space
)
4286 struct isl_obj obj
= { isl_obj_set
, NULL
};
4289 map
= isl_set_universe(space
);
4290 if (isl_stream_eat(s
, '{') < 0)
4292 obj
= obj_read_disjuncts(s
, v
, map
);
4293 if (isl_stream_eat(s
, '}') < 0)
4297 return extract_union_set(s
->ctx
, obj
);
4299 obj
.type
->free(obj
.v
);
4304 /* Read the body of an isl_multi_union_pw_aff from "s",
4305 * i.e., everything except the parameter specification.
4306 * "v" contains a description of the identifiers parsed so far.
4307 * The parameters, if any, are specified by "space".
4309 * In particular, handle the special case with shared domain constraints.
4310 * These are specified as
4314 * and are especially useful for setting the explicit domain
4315 * of a zero-dimensional isl_multi_union_pw_aff.
4316 * The core isl_multi_union_pw_aff body ([...]) is read by
4317 * read_multi_union_pw_aff_body_core.
4319 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body(
4320 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
4322 isl_multi_union_pw_aff
*mupa
;
4324 if (!isl_stream_next_token_is(s
, '('))
4325 return read_multi_union_pw_aff_body_core(s
, v
, space
);
4327 if (isl_stream_eat(s
, '(') < 0)
4329 mupa
= read_multi_union_pw_aff_body_core(s
, v
, isl_space_copy(space
));
4330 if (isl_stream_eat_if_available(s
, ':')) {
4333 dom
= read_union_set_body(s
, v
, space
);
4334 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4336 isl_space_free(space
);
4338 if (isl_stream_eat(s
, ')') < 0)
4339 return isl_multi_union_pw_aff_free(mupa
);
4343 isl_space_free(space
);
4347 /* Read an isl_multi_union_pw_aff from "s".
4349 * The input has the form
4351 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4355 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4357 * Additionally, a shared domain may be specified as
4363 * [..] -> ([..] : ...)
4365 * The first case is handled by the caller, the second case
4366 * is handled by read_multi_union_pw_aff_body.
4368 * We first check for the special case of an empty tuple "[]".
4369 * Then we check if there are any parameters.
4370 * Finally, read the tuple and construct the result.
4372 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_core(
4373 __isl_keep isl_stream
*s
)
4376 isl_set
*dom
= NULL
;
4378 isl_multi_union_pw_aff
*mupa
= NULL
;
4380 if (next_is_empty_tuple(s
)) {
4381 if (isl_stream_eat(s
, '['))
4383 if (isl_stream_eat(s
, ']'))
4385 space
= isl_space_set_alloc(s
->ctx
, 0, 0);
4386 return isl_multi_union_pw_aff_zero(space
);
4389 v
= vars_new(s
->ctx
);
4393 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4394 if (next_is_param_tuple(s
)) {
4395 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4396 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4399 space
= isl_set_get_space(dom
);
4401 mupa
= read_multi_union_pw_aff_body(s
, v
, space
);
4409 isl_multi_union_pw_aff_free(mupa
);
4413 /* Read an isl_multi_union_pw_aff from "s".
4415 * In particular, handle the special case with shared domain constraints.
4416 * These are specified as
4420 * and are especially useful for setting the explicit domain
4421 * of a zero-dimensional isl_multi_union_pw_aff.
4422 * The core isl_multi_union_pw_aff ([...]) is read by
4423 * read_multi_union_pw_aff_core.
4425 __isl_give isl_multi_union_pw_aff
*isl_stream_read_multi_union_pw_aff(
4426 __isl_keep isl_stream
*s
)
4428 isl_multi_union_pw_aff
*mupa
;
4430 if (!isl_stream_next_token_is(s
, '('))
4431 return read_multi_union_pw_aff_core(s
);
4433 if (isl_stream_eat(s
, '(') < 0)
4435 mupa
= read_multi_union_pw_aff_core(s
);
4436 if (isl_stream_eat_if_available(s
, ':')) {
4439 dom
= isl_stream_read_union_set(s
);
4440 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4442 if (isl_stream_eat(s
, ')') < 0)
4443 return isl_multi_union_pw_aff_free(mupa
);
4447 /* Read an isl_multi_union_pw_aff from "str".
4449 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_read_from_str(
4450 isl_ctx
*ctx
, const char *str
)
4452 isl_multi_union_pw_aff
*mupa
;
4453 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4456 mupa
= isl_stream_read_multi_union_pw_aff(s
);
4461 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
4462 __isl_keep isl_stream
*s
)
4467 if (obj
.type
== isl_obj_pw_qpolynomial
) {
4468 obj
.type
= isl_obj_union_pw_qpolynomial
;
4469 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
4472 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
4477 obj
.type
->free(obj
.v
);
4481 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
4482 isl_ctx
*ctx
, const char *str
)
4484 isl_union_pw_qpolynomial
*upwqp
;
4485 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4488 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);