2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2019,2022 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");
439 pwaff
= isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
444 pwaff
= isl_pw_aff_ceil(pwaff
);
446 pwaff
= isl_pw_aff_floor(pwaff
);
449 if (isl_stream_eat(s
, ')'))
452 if (isl_stream_eat(s
, ']'))
456 isl_space_free(space
);
459 isl_space_free(space
);
460 isl_pw_aff_free(pwaff
);
464 /* Divide "pa" by an integer constant read from the stream.
466 static __isl_give isl_pw_aff
*pw_aff_div_by_cst(__isl_keep isl_stream
*s
,
467 __isl_take isl_pw_aff
*pa
)
471 isl_int_set_si(f
, 1);
472 if (accept_cst_factor(s
, &f
) < 0)
473 pa
= isl_pw_aff_free(pa
);
474 pa
= isl_pw_aff_scale_down(pa
, f
);
480 static __isl_give isl_pw_aff
*accept_affine_factor(__isl_keep isl_stream
*s
,
481 __isl_take isl_space
*space
, struct vars
*v
)
483 struct isl_token
*tok
= NULL
;
484 isl_pw_aff
*res
= NULL
;
488 isl_stream_error(s
, NULL
, "unexpected EOF");
492 if (tok
->type
== ISL_TOKEN_AFF
) {
493 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
495 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
497 int pos
= vars_pos(v
, tok
->u
.s
, -1);
503 vars_drop(v
, v
->n
- n
);
504 isl_stream_error(s
, tok
, "unknown identifier");
508 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(space
)));
511 aff
->v
= isl_vec_set_element_si(aff
->v
, 2 + pos
, 1);
513 aff
= isl_aff_free(aff
);
514 res
= isl_pw_aff_from_aff(aff
);
516 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
517 if (isl_stream_eat_if_available(s
, '*')) {
518 res
= accept_affine_factor(s
, isl_space_copy(space
), v
);
519 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
523 ls
= isl_local_space_from_space(isl_space_copy(space
));
524 aff
= isl_aff_zero_on_domain(ls
);
525 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
526 res
= isl_pw_aff_from_aff(aff
);
529 } else if (tok
->type
== '(') {
532 res
= accept_affine(s
, isl_space_copy(space
), v
);
535 if (isl_stream_eat(s
, ')'))
537 } else if (is_start_of_div(tok
)) {
538 isl_stream_push_token(s
, tok
);
540 res
= accept_div(s
, isl_space_copy(space
), v
);
541 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
542 isl_stream_push_token(s
, tok
);
544 res
= accept_minmax(s
, isl_space_copy(space
), v
);
546 isl_stream_error(s
, tok
, "expecting factor");
549 if (isl_stream_eat_if_available(s
, '%') ||
550 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
551 isl_space_free(space
);
552 return affine_mod(s
, v
, res
);
554 if (isl_stream_eat_if_available(s
, '*')) {
557 isl_int_set_si(f
, 1);
558 if (accept_cst_factor(s
, &f
) < 0) {
562 res
= isl_pw_aff_scale(res
, f
);
565 if (isl_stream_eat_if_available(s
, '/'))
566 res
= pw_aff_div_by_cst(s
, res
);
567 if (isl_stream_eat_if_available(s
, ISL_TOKEN_INT_DIV
))
568 res
= isl_pw_aff_floor(pw_aff_div_by_cst(s
, res
));
570 isl_space_free(space
);
575 isl_pw_aff_free(res
);
576 isl_space_free(space
);
580 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
585 space
= isl_pw_aff_get_domain_space(pwaff
);
586 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
587 aff
= isl_aff_add_constant(aff
, v
);
589 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
592 /* Return a piecewise affine expression defined on the specified domain
593 * that represents NaN.
595 static __isl_give isl_pw_aff
*nan_on_domain(__isl_keep isl_space
*space
)
597 return isl_pw_aff_nan_on_domain_space(isl_space_copy(space
));
600 static __isl_give isl_pw_aff
*accept_affine(__isl_keep isl_stream
*s
,
601 __isl_take isl_space
*space
, struct vars
*v
)
603 struct isl_token
*tok
= NULL
;
608 ls
= isl_local_space_from_space(isl_space_copy(space
));
609 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
616 isl_stream_error(s
, NULL
, "unexpected EOF");
619 if (tok
->type
== '-') {
624 if (tok
->type
== '(' || is_start_of_div(tok
) ||
625 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
626 tok
->type
== ISL_TOKEN_IDENT
||
627 tok
->type
== ISL_TOKEN_AFF
) {
629 isl_stream_push_token(s
, tok
);
631 term
= accept_affine_factor(s
,
632 isl_space_copy(space
), v
);
634 res
= isl_pw_aff_sub(res
, term
);
636 res
= isl_pw_aff_add(res
, term
);
640 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
642 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
643 if (isl_stream_eat_if_available(s
, '*') ||
644 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
646 term
= accept_affine_factor(s
,
647 isl_space_copy(space
), v
);
648 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
649 res
= isl_pw_aff_add(res
, term
);
653 if (isl_stream_eat_if_available(s
,
654 ISL_TOKEN_INT_DIV
) &&
655 int_div_by_cst(s
, &tok
->u
.v
) < 0)
657 res
= add_cst(res
, tok
->u
.v
);
660 } else if (tok
->type
== ISL_TOKEN_NAN
) {
661 res
= isl_pw_aff_add(res
, nan_on_domain(space
));
663 isl_stream_error(s
, tok
, "unexpected isl_token");
664 isl_stream_push_token(s
, tok
);
665 isl_pw_aff_free(res
);
666 isl_space_free(space
);
672 if (tok
&& tok
->type
== '-') {
675 } else if (tok
&& tok
->type
== '+') {
678 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
679 isl_int_is_neg(tok
->u
.v
)) {
680 isl_stream_push_token(s
, tok
);
683 isl_stream_push_token(s
, tok
);
688 isl_space_free(space
);
691 isl_space_free(space
);
693 isl_pw_aff_free(res
);
697 /* Is "type" the type of a comparison operator between lists
698 * of affine expressions?
700 static int is_list_comparator_type(int type
)
703 case ISL_TOKEN_LEX_LT
:
704 case ISL_TOKEN_LEX_GT
:
705 case ISL_TOKEN_LEX_LE
:
706 case ISL_TOKEN_LEX_GE
:
713 static int is_comparator(struct isl_token
*tok
)
717 if (is_list_comparator_type(tok
->type
))
733 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
734 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
735 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
736 __isl_take isl_space
*space
, struct vars
*v
, int rational
);
738 /* Accept a ternary operator, given the first argument.
740 static __isl_give isl_pw_aff
*accept_ternary(__isl_keep isl_stream
*s
,
741 __isl_take isl_map
*cond
, struct vars
*v
, int rational
)
744 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
, *pa_cond
;
749 if (isl_stream_eat(s
, '?'))
752 space
= isl_space_wrap(isl_map_get_space(cond
));
753 pwaff1
= accept_extended_affine(s
, space
, v
, rational
);
757 if (isl_stream_eat(s
, ':'))
760 space
= isl_pw_aff_get_domain_space(pwaff1
);
761 pwaff2
= accept_extended_affine(s
, space
, v
, rational
);
765 pa_cond
= isl_set_indicator_function(isl_map_wrap(cond
));
766 return isl_pw_aff_cond(pa_cond
, pwaff1
, pwaff2
);
769 isl_pw_aff_free(pwaff1
);
770 isl_pw_aff_free(pwaff2
);
774 /* Set *line and *col to those of the next token, if any.
776 static void set_current_line_col(__isl_keep isl_stream
*s
, int *line
, int *col
)
778 struct isl_token
*tok
;
780 tok
= isl_stream_next_token(s
);
786 isl_stream_push_token(s
, tok
);
789 /* Push a token encapsulating "pa" onto "s", with the given
792 static isl_stat
push_aff(__isl_keep isl_stream
*s
, int line
, int col
,
793 __isl_take isl_pw_aff
*pa
)
795 struct isl_token
*tok
;
797 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
800 tok
->type
= ISL_TOKEN_AFF
;
802 isl_stream_push_token(s
, tok
);
807 return isl_stat_error
;
810 /* Is the next token a comparison operator?
812 static int next_is_comparator(__isl_keep isl_stream
*s
)
815 struct isl_token
*tok
;
817 tok
= isl_stream_next_token(s
);
821 is_comp
= is_comparator(tok
);
822 isl_stream_push_token(s
, tok
);
827 /* Accept an affine expression that may involve ternary operators.
828 * We first read an affine expression.
829 * If it is not followed by a comparison operator, we simply return it.
830 * Otherwise, we assume the affine expression is part of the first
831 * argument of a ternary operator and try to parse that.
833 static __isl_give isl_pw_aff
*accept_extended_affine(__isl_keep isl_stream
*s
,
834 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
838 int line
= -1, col
= -1;
840 set_current_line_col(s
, &line
, &col
);
842 pwaff
= accept_affine(s
, space
, v
);
844 pwaff
= isl_pw_aff_set_rational(pwaff
);
847 if (!next_is_comparator(s
))
850 space
= isl_pw_aff_get_domain_space(pwaff
);
851 cond
= isl_map_universe(isl_space_unwrap(space
));
853 if (push_aff(s
, line
, col
, pwaff
) < 0)
854 cond
= isl_map_free(cond
);
858 cond
= read_formula(s
, v
, cond
, rational
);
860 return accept_ternary(s
, cond
, v
, rational
);
863 static __isl_give isl_map
*read_var_def(__isl_keep isl_stream
*s
,
864 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
871 if (type
== isl_dim_param
)
872 pos
= isl_map_dim(map
, isl_dim_param
);
874 pos
= isl_map_dim(map
, isl_dim_in
);
875 if (type
== isl_dim_out
) {
876 isl_size n_out
= isl_map_dim(map
, isl_dim_out
);
877 if (pos
< 0 || n_out
< 0)
878 return isl_map_free(map
);
884 return isl_map_free(map
);
887 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)),
889 def_map
= isl_map_from_pw_aff(def
);
890 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
891 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
893 map
= isl_map_intersect(map
, def_map
);
898 static __isl_give isl_pw_aff_list
*accept_affine_list(__isl_keep isl_stream
*s
,
899 __isl_take isl_space
*space
, struct vars
*v
)
902 isl_pw_aff_list
*list
;
903 struct isl_token
*tok
= NULL
;
905 pwaff
= accept_affine(s
, isl_space_copy(space
), v
);
906 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
911 tok
= isl_stream_next_token(s
);
913 isl_stream_error(s
, NULL
, "unexpected EOF");
916 if (tok
->type
!= ',') {
917 isl_stream_push_token(s
, tok
);
922 pwaff
= accept_affine(s
, isl_space_copy(space
), v
);
923 list
= isl_pw_aff_list_concat(list
,
924 isl_pw_aff_list_from_pw_aff(pwaff
));
929 isl_space_free(space
);
932 isl_space_free(space
);
933 isl_pw_aff_list_free(list
);
937 static __isl_give isl_map
*read_defined_var_list(__isl_keep isl_stream
*s
,
938 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
940 struct isl_token
*tok
;
942 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
946 if (tok
->type
!= ISL_TOKEN_IDENT
)
949 p
= vars_pos(v
, tok
->u
.s
, -1);
953 isl_stream_error(s
, tok
, "expecting unique identifier");
957 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
960 tok
= isl_stream_next_token(s
);
961 if (tok
&& tok
->type
== '=') {
963 map
= read_var_def(s
, map
, isl_dim_out
, v
, rational
);
964 tok
= isl_stream_next_token(s
);
967 if (!tok
|| tok
->type
!= ',')
973 isl_stream_push_token(s
, tok
);
982 static int next_is_tuple(__isl_keep isl_stream
*s
)
984 struct isl_token
*tok
;
987 tok
= isl_stream_next_token(s
);
990 if (tok
->type
== '[') {
991 isl_stream_push_token(s
, tok
);
994 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
995 isl_stream_push_token(s
, tok
);
999 is_tuple
= isl_stream_next_token_is(s
, '[');
1001 isl_stream_push_token(s
, tok
);
1006 /* Does the next token mark the end of a tuple element?
1008 static int next_is_end_tuple_element(__isl_keep isl_stream
*s
)
1010 return isl_stream_next_token_is(s
, ',') ||
1011 isl_stream_next_token_is(s
, ']');
1014 /* Is the next token one that necessarily forms the start of a condition?
1016 static int next_is_condition_start(__isl_keep isl_stream
*s
)
1018 return isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1019 isl_stream_next_token_is(s
, ISL_TOKEN_NOT
) ||
1020 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1021 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
) ||
1022 isl_stream_next_token_is(s
, ISL_TOKEN_MAP
);
1025 /* Is "pa" an expression in term of earlier dimensions?
1026 * The alternative is that the dimension is defined to be equal to itself,
1027 * meaning that it has a universe domain and an expression that depends
1028 * on itself. "i" is the position of the expression in a sequence
1029 * of "n" expressions. The final dimensions of "pa" correspond to
1030 * these "n" expressions.
1032 static isl_bool
pw_aff_is_expr(__isl_keep isl_pw_aff
*pa
, int i
, int n
)
1037 return isl_bool_error
;
1039 return isl_bool_true
;
1040 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
1041 return isl_bool_true
;
1044 if (isl_int_is_zero(aff
->v
->el
[aff
->v
->size
- n
+ i
]))
1045 return isl_bool_true
;
1046 return isl_bool_false
;
1049 /* Does the tuple contain any dimensions that are defined
1050 * in terms of earlier dimensions?
1052 static isl_bool
tuple_has_expr(__isl_keep isl_multi_pw_aff
*tuple
)
1056 isl_bool has_expr
= isl_bool_false
;
1059 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1061 return isl_bool_error
;
1062 for (i
= 0; i
< n
; ++i
) {
1063 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1064 has_expr
= pw_aff_is_expr(pa
, i
, n
);
1065 isl_pw_aff_free(pa
);
1066 if (has_expr
< 0 || has_expr
)
1073 /* Set the name of dimension "pos" in "space" to "name".
1074 * During printing, we add primes if the same name appears more than once
1075 * to distinguish the occurrences. Here, we remove those primes from "name"
1076 * before setting the name of the dimension.
1078 static __isl_give isl_space
*space_set_dim_name(__isl_take isl_space
*space
,
1079 int pos
, char *name
)
1086 prime
= strchr(name
, '\'');
1089 space
= isl_space_set_dim_name(space
, isl_dim_out
, pos
, name
);
1096 /* Set the name of the last (output) dimension of "space" to "name",
1097 * ignoring any primes in "name".
1099 static __isl_give isl_space
*space_set_last_dim_name(
1100 __isl_take isl_space
*space
, char *name
)
1104 pos
= isl_space_dim(space
, isl_dim_out
);
1106 return isl_space_free(space
);
1107 return space_set_dim_name(space
, pos
- 1, name
);
1110 /* Construct an isl_pw_aff defined on a "space" (with v->n variables)
1111 * that is equal to the last of those variables.
1113 static __isl_give isl_pw_aff
*identity_tuple_el_on_space(
1114 __isl_take isl_space
*space
, struct vars
*v
)
1118 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1119 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, v
->n
- 1, 1);
1120 return isl_pw_aff_from_aff(aff
);
1123 /* Construct an isl_pw_aff defined on the domain space of "pa"
1124 * that is equal to the last variable in "v".
1126 * That is, if D is the domain space of "pa", then construct
1130 static __isl_give isl_pw_aff
*init_range(__isl_keep isl_pw_aff
*pa
,
1135 space
= isl_pw_aff_get_domain_space(pa
);
1136 return identity_tuple_el_on_space(space
, v
);
1139 /* Impose the lower bound "lower" on the variable represented by "range_pa".
1141 * In particular, "range_pa" is of the form
1143 * D[..., i] -> i : C
1145 * with D also the domains space of "lower' and "C" some constraints.
1147 * Return the expression
1149 * D[..., i] -> i : C and i >= lower
1151 static __isl_give isl_pw_aff
*set_lower(__isl_take isl_pw_aff
*range_pa
,
1152 __isl_take isl_pw_aff
*lower
)
1156 range
= isl_pw_aff_ge_set(isl_pw_aff_copy(range_pa
), lower
);
1157 return isl_pw_aff_intersect_domain(range_pa
, range
);
1160 /* Impose the upper bound "upper" on the variable represented by "range_pa".
1162 * In particular, "range_pa" is of the form
1164 * D[..., i] -> i : C
1166 * with D also the domains space of "upper' and "C" some constraints.
1168 * Return the expression
1170 * D[..., i] -> i : C and i <= upper
1172 static __isl_give isl_pw_aff
*set_upper(__isl_take isl_pw_aff
*range_pa
,
1173 __isl_take isl_pw_aff
*upper
)
1177 range
= isl_pw_aff_le_set(isl_pw_aff_copy(range_pa
), upper
);
1178 return isl_pw_aff_intersect_domain(range_pa
, range
);
1181 /* Construct a piecewise affine expression corresponding
1182 * to the last variable in "v" that is greater than or equal to "pa".
1184 * In particular, if D is the domain space of "pa",
1185 * then construct the expression
1189 * impose lower bound "pa" and return
1191 * D[..., i] -> i : i >= pa
1193 static __isl_give isl_pw_aff
*construct_lower(__isl_take isl_pw_aff
*pa
,
1196 return set_lower(init_range(pa
, v
), pa
);
1199 /* Construct a piecewise affine expression corresponding
1200 * to the last variable in "v" that is smaller than or equal to "pa".
1202 * In particular, if D is the domain space of "pa",
1203 * then construct the expression
1207 * impose lower bound "pa" and return
1209 * D[..., i] -> i : i <= pa
1211 static __isl_give isl_pw_aff
*construct_upper(__isl_take isl_pw_aff
*pa
,
1214 return set_upper(init_range(pa
, v
), pa
);
1217 /* Construct a piecewise affine expression corresponding
1218 * to the last variable in "v" that ranges between "pa" and "pa2".
1220 * In particular, if D is the domain space of "pa" (and "pa2"),
1221 * then construct the expression
1225 * impose lower bound "pa" and upper bound "pa2" and return
1227 * D[..., i] -> i : pa <= i <= pa2
1229 static __isl_give isl_pw_aff
*construct_range(__isl_take isl_pw_aff
*pa
,
1230 __isl_take isl_pw_aff
*pa2
, struct vars
*v
)
1232 return set_upper(set_lower(init_range(pa
, v
), pa
), pa2
);
1235 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
1236 struct vars
*v
, __isl_take isl_map
*map
, int rational
);
1238 /* Given that the (piecewise) affine expression "pa"
1239 * has just been parsed, followed by a colon,
1240 * continue parsing as part of a piecewise affine expression.
1242 * In particular, check if the colon is followed by a condition.
1243 * If so, parse the conditions(a) on "pa" and include them in the domain.
1244 * Otherwise, if the colon is followed by another (piecewise) affine expression
1245 * then consider the two expressions as endpoints of a range of values and
1246 * return a piecewise affine expression that takes values in that range.
1247 * Note that an affine expression followed by a comparison operator
1248 * is considered to be part of a condition.
1249 * If the colon is not followed by anything (inside the tuple element),
1250 * then consider "pa" as a lower bound on a range of values without upper bound
1251 * and return a piecewise affine expression that takes values in that range.
1253 static __isl_give isl_pw_aff
*update_piecewise_affine_colon(
1254 __isl_take isl_pw_aff
*pa
, __isl_keep isl_stream
*s
,
1255 struct vars
*v
, int rational
)
1257 isl_space
*dom_space
;
1260 dom_space
= isl_pw_aff_get_domain_space(pa
);
1261 map
= isl_map_universe(isl_space_from_domain(dom_space
));
1263 if (isl_stream_next_token_is(s
, '('))
1264 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
1266 if (next_is_end_tuple_element(s
)) {
1268 return construct_lower(pa
, v
);
1270 if (!next_is_condition_start(s
)) {
1271 int line
= -1, col
= -1;
1275 set_current_line_col(s
, &line
, &col
);
1276 space
= isl_space_wrap(isl_map_get_space(map
));
1277 pa2
= accept_affine(s
, space
, v
);
1279 pa2
= isl_pw_aff_set_rational(pa2
);
1280 if (!next_is_comparator(s
)) {
1282 pa2
= isl_pw_aff_domain_factor_domain(pa2
);
1283 return construct_range(pa
, pa2
, v
);
1285 if (push_aff(s
, line
, col
, pa2
) < 0)
1289 map
= read_formula(s
, v
, map
, rational
);
1290 pa
= isl_pw_aff_intersect_domain(pa
, isl_map_domain(map
));
1295 isl_pw_aff_free(pa
);
1299 /* Accept a piecewise affine expression.
1301 * At the outer level, the piecewise affine expression may be of the form
1303 * aff1 : condition1; aff2 : conditions2; ...
1316 * each of the affine expressions may in turn include ternary operators.
1318 * If the first token is a colon, then the expression must be
1319 * ":" or ": aff2", depending on whether anything follows the colon
1320 * inside the tuple element.
1321 * The first is considered to represent an arbitrary value.
1322 * The second is considered to represent a range of values
1323 * with the given upper bound and no lower bound.
1325 * There may be parentheses around some subexpression of "aff1"
1326 * around "aff1" itself, around "aff1 : condition1" and/or
1327 * around the entire piecewise affine expression.
1328 * We therefore remove the opening parenthesis (if any) from the stream
1329 * in case the closing parenthesis follows the colon, but if the closing
1330 * parenthesis is the first thing in the stream after the parsed affine
1331 * expression, we push the parsed expression onto the stream and parse
1332 * again in case the parentheses enclose some subexpression of "aff1".
1334 static __isl_give isl_pw_aff
*accept_piecewise_affine(__isl_keep isl_stream
*s
,
1335 __isl_take isl_space
*space
, struct vars
*v
, int rational
)
1338 isl_space
*res_space
;
1340 if (isl_stream_eat_if_available(s
, ':')) {
1341 if (next_is_end_tuple_element(s
))
1342 return identity_tuple_el_on_space(space
, v
);
1344 return construct_upper(accept_affine(s
, space
, v
), v
);
1347 res_space
= isl_space_from_domain(isl_space_copy(space
));
1348 res_space
= isl_space_add_dims(res_space
, isl_dim_out
, 1);
1349 res
= isl_pw_aff_empty(res_space
);
1353 int line
= -1, col
= -1;
1355 set_current_line_col(s
, &line
, &col
);
1356 seen_paren
= isl_stream_eat_if_available(s
, '(');
1358 pa
= accept_piecewise_affine(s
, isl_space_copy(space
),
1361 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1363 if (seen_paren
&& isl_stream_eat_if_available(s
, ')')) {
1365 if (push_aff(s
, line
, col
, pa
) < 0)
1367 pa
= accept_extended_affine(s
, isl_space_copy(space
),
1370 if (isl_stream_eat_if_available(s
, ':'))
1371 pa
= update_piecewise_affine_colon(pa
, s
, v
, rational
);
1373 res
= isl_pw_aff_union_add(res
, pa
);
1375 if (seen_paren
&& isl_stream_eat(s
, ')'))
1377 } while (isl_stream_eat_if_available(s
, ';'));
1379 isl_space_free(space
);
1383 isl_space_free(space
);
1384 return isl_pw_aff_free(res
);
1387 /* Read an affine expression from "s" for use in read_tuple.
1389 * accept_extended_affine requires a wrapped space as input.
1390 * read_tuple on the other hand expects each isl_pw_aff
1391 * to have an anonymous space. We therefore adjust the space
1392 * of the isl_pw_aff before returning it.
1394 static __isl_give isl_pw_aff
*read_tuple_var_def(__isl_keep isl_stream
*s
,
1395 struct vars
*v
, int rational
)
1400 space
= isl_space_wrap(isl_space_alloc(s
->ctx
, 0, v
->n
, 0));
1402 def
= accept_piecewise_affine(s
, space
, v
, rational
);
1403 def
= isl_pw_aff_domain_factor_domain(def
);
1408 /* Read a list of tuple elements by calling "read_el" on each of them and
1409 * return a space with the same number of set dimensions derived from
1410 * the parameter space "space" and possibly updated by "read_el".
1411 * The elements in the list are separated by either "," or "][".
1412 * If "comma" is set then only "," is allowed.
1414 static __isl_give isl_space
*read_tuple_list(__isl_keep isl_stream
*s
,
1415 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1416 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1417 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1424 space
= isl_space_set_from_params(space
);
1426 if (isl_stream_next_token_is(s
, ']'))
1430 struct isl_token
*tok
;
1432 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1434 space
= read_el(s
, v
, space
, rational
, user
);
1438 tok
= isl_stream_next_token(s
);
1439 if (!comma
&& tok
&& tok
->type
== ']' &&
1440 isl_stream_next_token_is(s
, '[')) {
1441 isl_token_free(tok
);
1442 tok
= isl_stream_next_token(s
);
1443 } else if (!tok
|| tok
->type
!= ',') {
1445 isl_stream_push_token(s
, tok
);
1449 isl_token_free(tok
);
1455 /* Read a tuple space from "s" derived from the parameter space "space".
1456 * Call "read_el" on each element in the tuples.
1458 static __isl_give isl_space
*read_tuple_space(__isl_keep isl_stream
*s
,
1459 struct vars
*v
, __isl_take isl_space
*space
, int rational
, int comma
,
1460 __isl_give isl_space
*(*read_el
)(__isl_keep isl_stream
*s
,
1461 struct vars
*v
, __isl_take isl_space
*space
, int rational
,
1465 struct isl_token
*tok
;
1467 isl_space
*res
= NULL
;
1469 tok
= isl_stream_next_token(s
);
1472 if (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
) {
1473 name
= strdup(tok
->u
.s
);
1474 isl_token_free(tok
);
1478 isl_stream_push_token(s
, tok
);
1479 if (isl_stream_eat(s
, '['))
1481 if (next_is_tuple(s
)) {
1483 res
= read_tuple_space(s
, v
, isl_space_copy(space
),
1484 rational
, comma
, read_el
, user
);
1485 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1487 out
= read_tuple_space(s
, v
, isl_space_copy(space
),
1488 rational
, comma
, read_el
, user
);
1489 res
= isl_space_product(res
, out
);
1491 res
= read_tuple_list(s
, v
, isl_space_copy(space
),
1492 rational
, comma
, read_el
, user
);
1493 if (isl_stream_eat(s
, ']'))
1497 res
= isl_space_set_tuple_name(res
, isl_dim_set
, name
);
1501 isl_space_free(space
);
1505 isl_space_free(res
);
1506 isl_space_free(space
);
1510 /* Construct an isl_pw_aff defined on a space with v->n variables
1511 * that is equal to the last of those variables.
1513 static __isl_give isl_pw_aff
*identity_tuple_el(struct vars
*v
)
1517 space
= isl_space_set_alloc(v
->ctx
, 0, v
->n
);
1518 return identity_tuple_el_on_space(space
, v
);
1521 /* This function is called for each element in a tuple inside read_tuple.
1522 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1523 * over a space containing all variables in "v" defined so far.
1524 * The isl_pw_aff expresses the new variable in terms of earlier variables
1525 * if a definition is provided. Otherwise, it is represented as being
1527 * Add the isl_pw_aff to *list.
1528 * If the new variable was named, then adjust "space" accordingly and
1529 * return the updated space.
1531 static __isl_give isl_space
*read_tuple_pw_aff_el(__isl_keep isl_stream
*s
,
1532 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1534 isl_pw_aff_list
**list
= (isl_pw_aff_list
**) user
;
1536 struct isl_token
*tok
;
1539 tok
= next_token(s
);
1541 isl_stream_error(s
, NULL
, "unexpected EOF");
1542 return isl_space_free(space
);
1545 if (tok
->type
== ISL_TOKEN_IDENT
) {
1547 int p
= vars_pos(v
, tok
->u
.s
, -1);
1553 if (tok
->type
== '*') {
1554 if (vars_add_anon(v
) < 0)
1556 isl_token_free(tok
);
1557 pa
= identity_tuple_el(v
);
1558 } else if (new_name
) {
1559 space
= space_set_last_dim_name(space
, v
->v
->name
);
1560 isl_token_free(tok
);
1561 if (isl_stream_eat_if_available(s
, '='))
1562 pa
= read_tuple_var_def(s
, v
, rational
);
1564 pa
= identity_tuple_el(v
);
1566 isl_stream_push_token(s
, tok
);
1568 if (vars_add_anon(v
) < 0)
1570 pa
= read_tuple_var_def(s
, v
, rational
);
1573 *list
= isl_pw_aff_list_add(*list
, pa
);
1575 return isl_space_free(space
);
1579 isl_token_free(tok
);
1580 return isl_space_free(space
);
1583 /* Read a tuple and represent it as an isl_multi_pw_aff.
1584 * The range space of the isl_multi_pw_aff is the space of the tuple.
1585 * The domain space is an anonymous space
1586 * with a dimension for each variable in the set of variables in "v",
1587 * including the variables in the range.
1588 * If a given dimension is not defined in terms of earlier dimensions in
1589 * the input, then the corresponding isl_pw_aff is set equal to one time
1590 * the variable corresponding to the dimension being defined.
1592 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1593 * Each element in this list is defined over a space representing
1594 * the variables defined so far. We need to adjust the earlier
1595 * elements to have as many variables in the domain as the final
1596 * element in the list.
1598 static __isl_give isl_multi_pw_aff
*read_tuple(__isl_keep isl_stream
*s
,
1599 struct vars
*v
, int rational
, int comma
)
1604 isl_pw_aff_list
*list
;
1606 space
= isl_space_params_alloc(v
->ctx
, 0);
1607 list
= isl_pw_aff_list_alloc(s
->ctx
, 0);
1608 space
= read_tuple_space(s
, v
, space
, rational
, comma
,
1609 &read_tuple_pw_aff_el
, &list
);
1610 n
= isl_space_dim(space
, isl_dim_set
);
1612 space
= isl_space_free(space
);
1613 for (i
= 0; i
+ 1 < n
; ++i
) {
1616 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
1617 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, n
- (i
+ 1));
1618 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
1621 space
= isl_space_from_range(space
);
1622 space
= isl_space_add_dims(space
, isl_dim_in
, v
->n
);
1623 return isl_multi_pw_aff_from_pw_aff_list(space
, list
);
1626 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1627 * We first create the appropriate space in "map" based on the range
1628 * space of this isl_multi_pw_aff. Then, we add equalities based
1629 * on the affine expressions. These live in an anonymous space,
1630 * however, so we first need to reset the space to that of "map".
1632 static __isl_give isl_map
*map_from_tuple(__isl_take isl_multi_pw_aff
*tuple
,
1633 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1639 isl_space
*space
= NULL
;
1641 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
1644 ctx
= isl_multi_pw_aff_get_ctx(tuple
);
1645 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
1649 if (type
== isl_dim_param
) {
1650 if (isl_space_has_tuple_name(space
, isl_dim_set
) ||
1651 isl_space_is_wrapping(space
)) {
1652 isl_die(ctx
, isl_error_invalid
,
1653 "parameter tuples cannot be named or nested",
1656 map
= isl_map_add_dims(map
, type
, n
);
1657 for (i
= 0; i
< n
; ++i
) {
1659 if (!isl_space_has_dim_name(space
, isl_dim_set
, i
))
1660 isl_die(ctx
, isl_error_invalid
,
1661 "parameters must be named",
1663 id
= isl_space_get_dim_id(space
, isl_dim_set
, i
);
1664 map
= isl_map_set_dim_id(map
, isl_dim_param
, i
, id
);
1666 } else if (type
== isl_dim_in
) {
1669 set
= isl_set_universe(isl_space_copy(space
));
1671 set
= isl_set_set_rational(set
);
1672 set
= isl_set_intersect_params(set
, isl_map_params(map
));
1673 map
= isl_map_from_domain(set
);
1677 set
= isl_set_universe(isl_space_copy(space
));
1679 set
= isl_set_set_rational(set
);
1680 map
= isl_map_from_domain_and_range(isl_map_domain(map
), set
);
1683 for (i
= 0; i
< n
; ++i
) {
1690 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
1691 space
= isl_pw_aff_get_domain_space(pa
);
1692 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1693 aff
= isl_aff_add_coefficient_si(aff
,
1694 isl_dim_in
, v
->n
- n
+ i
, -1);
1695 pa
= isl_pw_aff_add(pa
, isl_pw_aff_from_aff(aff
));
1697 pa
= isl_pw_aff_set_rational(pa
);
1698 set
= isl_pw_aff_zero_set(pa
);
1699 map_i
= isl_map_from_range(set
);
1700 map_i
= isl_map_reset_space(map_i
, isl_map_get_space(map
));
1701 map
= isl_map_intersect(map
, map_i
);
1704 isl_space_free(space
);
1705 isl_multi_pw_aff_free(tuple
);
1708 isl_space_free(space
);
1709 isl_multi_pw_aff_free(tuple
);
1714 /* Read a tuple from "s" and add it to "map".
1715 * The tuple is initially represented as an isl_multi_pw_aff and
1716 * then added to "map".
1718 static __isl_give isl_map
*read_map_tuple(__isl_keep isl_stream
*s
,
1719 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
,
1720 int rational
, int comma
)
1722 isl_multi_pw_aff
*tuple
;
1724 tuple
= read_tuple(s
, v
, rational
, comma
);
1726 return isl_map_free(map
);
1728 return map_from_tuple(tuple
, map
, type
, v
, rational
);
1731 /* Read the parameter domain of an expression from "s" (if any) and
1732 * check that it does not involve any constraints.
1733 * "v" contains a description of the identifiers parsed so far
1734 * (of which there should not be any at this point) and is extended
1737 static __isl_give isl_set
*read_universe_params(__isl_keep isl_stream
*s
,
1742 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
1743 if (next_is_tuple(s
)) {
1744 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
1745 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
1746 return isl_set_free(dom
);
1748 if (!isl_set_plain_is_universe(dom
))
1749 isl_die(s
->ctx
, isl_error_invalid
,
1750 "expecting universe parameter domain",
1751 return isl_set_free(dom
));
1756 /* This function is called for each element in a tuple inside read_space_tuples.
1757 * Add a new variable to "v" and adjust "space" accordingly
1758 * if the variable has a name.
1760 static __isl_give isl_space
*read_tuple_id(__isl_keep isl_stream
*s
,
1761 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
1763 struct isl_token
*tok
;
1765 tok
= next_token(s
);
1767 isl_stream_error(s
, NULL
, "unexpected EOF");
1768 return isl_space_free(space
);
1771 if (tok
->type
== ISL_TOKEN_IDENT
) {
1773 int p
= vars_pos(v
, tok
->u
.s
, -1);
1777 isl_stream_error(s
, tok
, "expecting fresh identifier");
1780 space
= space_set_last_dim_name(space
, v
->v
->name
);
1781 } else if (tok
->type
== '*') {
1782 if (vars_add_anon(v
) < 0)
1785 isl_stream_error(s
, tok
, "expecting identifier or '*'");
1789 isl_token_free(tok
);
1792 isl_token_free(tok
);
1793 return isl_space_free(space
);
1796 /* Given a parameter space "params", extend it with one or two tuples
1798 * "v" contains a description of the identifiers parsed so far and is extended
1801 static __isl_give isl_space
*read_space_tuples(__isl_keep isl_stream
*s
,
1802 struct vars
*v
, __isl_take isl_space
*params
)
1804 isl_space
*space
, *ran
;
1806 space
= read_tuple_space(s
, v
, isl_space_copy(params
), 1, 1,
1807 &read_tuple_id
, NULL
);
1808 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
1809 ran
= read_tuple_space(s
, v
, isl_space_copy(params
), 1, 1,
1810 &read_tuple_id
, NULL
);
1811 space
= isl_space_unwrap(isl_space_product(space
, ran
));
1813 isl_space_free(params
);
1818 /* Read an isl_space object from "s".
1820 * First read the parameters (if any).
1822 * Then check if the description is of the special form "{ : }",
1823 * in which case it represents a parameter space.
1824 * Otherwise, it has one or two tuples.
1826 __isl_give isl_space
*isl_stream_read_space(__isl_keep isl_stream
*s
)
1832 v
= vars_new(s
->ctx
);
1835 dom
= read_universe_params(s
, v
);
1836 space
= isl_set_get_space(dom
);
1839 if (isl_stream_eat(s
, '{'))
1842 if (!isl_stream_eat_if_available(s
, ':'))
1843 space
= read_space_tuples(s
, v
, space
);
1845 if (isl_stream_eat(s
, '}'))
1852 isl_space_free(space
);
1856 /* Read an isl_space object from "str".
1858 __isl_give isl_space
*isl_space_read_from_str(isl_ctx
*ctx
,
1861 struct isl_space
*space
;
1862 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
1865 space
= isl_stream_read_space(s
);
1870 /* Given two equal-length lists of piecewise affine expression with the space
1871 * of "set" as domain, construct a set in the same space that expresses
1872 * that "left" and "right" satisfy the comparison "type".
1874 * A space is constructed of the same dimension as the number of elements
1875 * in the two lists. The comparison is then expressed in a map from
1876 * this space to itself and wrapped into a set. Finally the two lists
1877 * of piecewise affine expressions are plugged into this set.
1879 * Let S be the space of "set" and T the constructed space.
1880 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1881 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1882 * while the comparison is first expressed in T -> T, then [T -> T]
1885 static __isl_give isl_set
*list_cmp(__isl_keep isl_set
*set
, int type
,
1886 __isl_take isl_pw_aff_list
*left
, __isl_take isl_pw_aff_list
*right
)
1890 isl_multi_pw_aff
*mpa1
, *mpa2
;
1892 n
= isl_pw_aff_list_n_pw_aff(left
);
1893 if (!set
|| n
< 0 || !right
)
1896 space
= isl_set_get_space(set
);
1897 space
= isl_space_from_domain(space
);
1898 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
1899 mpa1
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), left
);
1900 mpa2
= isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space
), right
);
1901 mpa1
= isl_multi_pw_aff_range_product(mpa1
, mpa2
);
1903 space
= isl_space_range(space
);
1905 case ISL_TOKEN_LEX_LT
:
1906 set
= isl_map_wrap(isl_map_lex_lt(space
));
1908 case ISL_TOKEN_LEX_GT
:
1909 set
= isl_map_wrap(isl_map_lex_gt(space
));
1911 case ISL_TOKEN_LEX_LE
:
1912 set
= isl_map_wrap(isl_map_lex_le(space
));
1914 case ISL_TOKEN_LEX_GE
:
1915 set
= isl_map_wrap(isl_map_lex_ge(space
));
1918 isl_multi_pw_aff_free(mpa1
);
1919 isl_space_free(space
);
1920 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
1921 "unhandled list comparison type", return NULL
);
1923 set
= isl_set_preimage_multi_pw_aff(set
, mpa1
);
1926 isl_pw_aff_list_free(left
);
1927 isl_pw_aff_list_free(right
);
1931 /* Construct constraints of the form
1935 * where a is an element in "left", op is an operator of type "type" and
1936 * b is an element in "right", add the constraints to "set" and return
1938 * "rational" is set if the constraints should be treated as
1939 * a rational constraints.
1941 * If "type" is the type of a comparison operator between lists
1942 * of affine expressions, then a single (compound) constraint
1943 * is constructed by list_cmp instead.
1945 static __isl_give isl_set
*construct_constraints(
1946 __isl_take isl_set
*set
, int type
,
1947 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
,
1952 left
= isl_pw_aff_list_copy(left
);
1953 right
= isl_pw_aff_list_copy(right
);
1955 left
= isl_pw_aff_list_set_rational(left
);
1956 right
= isl_pw_aff_list_set_rational(right
);
1958 if (is_list_comparator_type(type
))
1959 cond
= list_cmp(set
, type
, left
, right
);
1960 else if (type
== ISL_TOKEN_LE
)
1961 cond
= isl_pw_aff_list_le_set(left
, right
);
1962 else if (type
== ISL_TOKEN_GE
)
1963 cond
= isl_pw_aff_list_ge_set(left
, right
);
1964 else if (type
== ISL_TOKEN_LT
)
1965 cond
= isl_pw_aff_list_lt_set(left
, right
);
1966 else if (type
== ISL_TOKEN_GT
)
1967 cond
= isl_pw_aff_list_gt_set(left
, right
);
1968 else if (type
== ISL_TOKEN_NE
)
1969 cond
= isl_pw_aff_list_ne_set(left
, right
);
1971 cond
= isl_pw_aff_list_eq_set(left
, right
);
1973 return isl_set_intersect(set
, cond
);
1976 /* Read a constraint from "s", add it to "map" and return the result.
1977 * "v" contains a description of the identifiers parsed so far.
1978 * "rational" is set if the constraint should be treated as
1979 * a rational constraint.
1980 * The constraint read from "s" may be applied to multiple pairs
1981 * of affine expressions and may be chained.
1982 * In particular, a list of affine expressions is read, followed
1983 * by a comparison operator and another list of affine expressions.
1984 * The comparison operator is then applied to each pair of elements
1985 * in the two lists and the results are added to "map".
1986 * However, if the operator expects two lists of affine expressions,
1987 * then it is applied directly to those lists and the two lists
1988 * are required to have the same length.
1989 * If the next token is another comparison operator, then another
1990 * list of affine expressions is read and the process repeats.
1992 * The processing is performed on a wrapped copy of "map" because
1993 * an affine expression cannot have a binary relation as domain.
1995 static __isl_give isl_map
*add_constraint(__isl_keep isl_stream
*s
,
1996 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
1998 struct isl_token
*tok
;
2000 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
2004 set
= isl_map_wrap(map
);
2005 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
2008 tok
= isl_stream_next_token(s
);
2009 if (!is_comparator(tok
)) {
2010 isl_stream_error(s
, tok
, "missing operator");
2012 isl_stream_push_token(s
, tok
);
2016 isl_token_free(tok
);
2018 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
2019 n1
= isl_pw_aff_list_n_pw_aff(list1
);
2020 n2
= isl_pw_aff_list_n_pw_aff(list2
);
2021 if (n1
< 0 || n2
< 0)
2023 if (is_list_comparator_type(type
) && n1
!= n2
) {
2024 isl_stream_error(s
, NULL
,
2025 "list arguments not of same size");
2029 set
= construct_constraints(set
, type
, list1
, list2
, rational
);
2030 isl_pw_aff_list_free(list1
);
2033 if (!next_is_comparator(s
))
2035 tok
= isl_stream_next_token(s
);
2037 isl_token_free(tok
);
2039 isl_pw_aff_list_free(list1
);
2041 return isl_set_unwrap(set
);
2043 isl_pw_aff_list_free(list1
);
2044 isl_pw_aff_list_free(list2
);
2049 static __isl_give isl_map
*read_exists(__isl_keep isl_stream
*s
,
2050 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2053 int seen_paren
= isl_stream_eat_if_available(s
, '(');
2055 map
= isl_map_from_domain(isl_map_wrap(map
));
2056 map
= read_defined_var_list(s
, v
, map
, rational
);
2058 if (isl_stream_eat(s
, ':'))
2061 map
= read_formula(s
, v
, map
, rational
);
2062 map
= isl_set_unwrap(isl_map_domain(map
));
2064 vars_drop(v
, v
->n
- n
);
2065 if (seen_paren
&& isl_stream_eat(s
, ')'))
2074 /* Parse an expression between parentheses and push the result
2075 * back on the stream.
2077 * The parsed expression may be either an affine expression
2078 * or a condition. The first type is pushed onto the stream
2079 * as an isl_pw_aff, while the second is pushed as an isl_map.
2081 * If the initial token indicates the start of a condition,
2082 * we parse it as such.
2083 * Otherwise, we first parse an affine expression and push
2084 * that onto the stream. If the affine expression covers the
2085 * entire expression between parentheses, we return.
2086 * Otherwise, we assume that the affine expression is the
2087 * start of a condition and continue parsing.
2089 static int resolve_paren_expr(__isl_keep isl_stream
*s
,
2090 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2092 struct isl_token
*tok
, *tok2
;
2097 tok
= isl_stream_next_token(s
);
2098 if (!tok
|| tok
->type
!= '(')
2101 if (isl_stream_next_token_is(s
, '('))
2102 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
2105 if (next_is_condition_start(s
)) {
2106 map
= read_formula(s
, v
, map
, rational
);
2107 if (isl_stream_eat(s
, ')'))
2109 tok
->type
= ISL_TOKEN_MAP
;
2111 isl_stream_push_token(s
, tok
);
2115 tok2
= isl_stream_next_token(s
);
2120 isl_stream_push_token(s
, tok2
);
2122 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
2126 has_paren
= isl_stream_eat_if_available(s
, ')');
2128 if (push_aff(s
, line
, col
, pwaff
) < 0)
2132 isl_token_free(tok
);
2137 map
= read_formula(s
, v
, map
, rational
);
2138 if (isl_stream_eat(s
, ')'))
2141 tok
->type
= ISL_TOKEN_MAP
;
2143 isl_stream_push_token(s
, tok
);
2147 isl_token_free(tok
);
2152 static __isl_give isl_map
*read_conjunct(__isl_keep isl_stream
*s
,
2153 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2155 if (isl_stream_next_token_is(s
, '('))
2156 if (resolve_paren_expr(s
, v
, isl_map_copy(map
), rational
))
2159 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
2160 struct isl_token
*tok
;
2161 tok
= isl_stream_next_token(s
);
2165 map
= isl_map_copy(tok
->u
.map
);
2166 isl_token_free(tok
);
2170 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
2171 return read_exists(s
, v
, map
, rational
);
2173 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
2176 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
2177 isl_space
*space
= isl_map_get_space(map
);
2179 return isl_map_empty(space
);
2182 return add_constraint(s
, v
, map
, rational
);
2188 static __isl_give isl_map
*read_conjuncts(__isl_keep isl_stream
*s
,
2189 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2194 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
2195 res
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
2197 res
= isl_map_subtract(isl_map_copy(map
), res
);
2199 while (res
&& isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
2202 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
2203 res_i
= read_conjunct(s
, v
, isl_map_copy(map
), rational
);
2205 res
= isl_map_subtract(res
, res_i
);
2207 res
= isl_map_intersect(res
, res_i
);
2214 static __isl_give isl_map
*read_disjuncts(__isl_keep isl_stream
*s
,
2215 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2219 if (isl_stream_next_token_is(s
, '}'))
2222 res
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
2223 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
2226 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
), rational
);
2227 res
= isl_map_union(res
, res_i
);
2234 /* Read a first order formula from "s", add the corresponding
2235 * constraints to "map" and return the result.
2237 * In particular, read a formula of the form
2245 * where a and b are disjunctions.
2247 * In the first case, map is replaced by
2249 * map \cap { [..] : a }
2251 * In the second case, it is replaced by
2253 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
2255 static __isl_give isl_map
*read_formula(__isl_keep isl_stream
*s
,
2256 struct vars
*v
, __isl_take isl_map
*map
, int rational
)
2260 res
= read_disjuncts(s
, v
, isl_map_copy(map
), rational
);
2262 if (isl_stream_eat_if_available(s
, ISL_TOKEN_IMPLIES
)) {
2265 res
= isl_map_subtract(isl_map_copy(map
), res
);
2266 res2
= read_disjuncts(s
, v
, map
, rational
);
2267 res
= isl_map_union(res
, res2
);
2274 static isl_size
polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
2276 isl_size n_out
, n_in
, n_param
, n_div
;
2278 n_param
= isl_basic_map_dim(bmap
, isl_dim_param
);
2279 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
2280 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
2281 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2282 if (n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
2283 return isl_size_error
;
2286 return 1 + n_param
+ n_in
+ pos
;
2290 return 1 + n_param
+ pos
;
2294 return 1 + n_param
+ n_in
+ n_out
+ pos
;
2303 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
2304 __isl_keep isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
2307 struct isl_token
*tok
;
2316 tok
= isl_stream_next_token(s
);
2317 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2318 isl_stream_error(s
, tok
, "expecting coefficient");
2320 isl_stream_push_token(s
, tok
);
2323 if (!tok
->on_new_line
) {
2324 isl_stream_error(s
, tok
, "coefficient should appear on new line");
2325 isl_stream_push_token(s
, tok
);
2329 type
= isl_int_get_si(tok
->u
.v
);
2330 isl_token_free(tok
);
2332 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
2334 k
= isl_basic_map_alloc_equality(bmap
);
2337 k
= isl_basic_map_alloc_inequality(bmap
);
2343 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2345 return isl_basic_map_free(bmap
);
2346 for (j
= 0; j
< 1 + total
; ++j
) {
2348 tok
= isl_stream_next_token(s
);
2349 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2350 isl_stream_error(s
, tok
, "expecting coefficient");
2352 isl_stream_push_token(s
, tok
);
2355 if (tok
->on_new_line
) {
2356 isl_stream_error(s
, tok
,
2357 "coefficient should not appear on new line");
2358 isl_stream_push_token(s
, tok
);
2361 pos
= polylib_pos_to_isl_pos(bmap
, j
);
2363 isl_int_set(c
[pos
], tok
->u
.v
);
2364 isl_token_free(tok
);
2366 return isl_basic_map_free(bmap
);
2371 isl_basic_map_free(bmap
);
2375 static __isl_give isl_basic_map
*basic_map_read_polylib(
2376 __isl_keep isl_stream
*s
)
2379 struct isl_token
*tok
;
2380 struct isl_token
*tok2
;
2383 unsigned in
= 0, out
, local
= 0;
2384 struct isl_basic_map
*bmap
= NULL
;
2387 tok
= isl_stream_next_token(s
);
2389 isl_stream_error(s
, NULL
, "unexpected EOF");
2392 tok2
= isl_stream_next_token(s
);
2394 isl_token_free(tok
);
2395 isl_stream_error(s
, NULL
, "unexpected EOF");
2398 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2399 isl_stream_push_token(s
, tok2
);
2400 isl_stream_push_token(s
, tok
);
2401 isl_stream_error(s
, NULL
,
2402 "expecting constraint matrix dimensions");
2405 n_row
= isl_int_get_si(tok
->u
.v
);
2406 n_col
= isl_int_get_si(tok2
->u
.v
);
2407 on_new_line
= tok2
->on_new_line
;
2408 isl_token_free(tok2
);
2409 isl_token_free(tok
);
2410 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
2411 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
2412 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
2413 tok
= isl_stream_next_token_on_same_line(s
);
2415 if (tok
->type
!= ISL_TOKEN_VALUE
) {
2416 isl_stream_error(s
, tok
,
2417 "expecting number of output dimensions");
2418 isl_stream_push_token(s
, tok
);
2421 out
= isl_int_get_si(tok
->u
.v
);
2422 isl_token_free(tok
);
2424 tok
= isl_stream_next_token_on_same_line(s
);
2425 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2426 isl_stream_error(s
, tok
,
2427 "expecting number of input dimensions");
2429 isl_stream_push_token(s
, tok
);
2432 in
= isl_int_get_si(tok
->u
.v
);
2433 isl_token_free(tok
);
2435 tok
= isl_stream_next_token_on_same_line(s
);
2436 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2437 isl_stream_error(s
, tok
,
2438 "expecting number of existentials");
2440 isl_stream_push_token(s
, tok
);
2443 local
= isl_int_get_si(tok
->u
.v
);
2444 isl_token_free(tok
);
2446 tok
= isl_stream_next_token_on_same_line(s
);
2447 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2448 isl_stream_error(s
, tok
,
2449 "expecting number of parameters");
2451 isl_stream_push_token(s
, tok
);
2454 nparam
= isl_int_get_si(tok
->u
.v
);
2455 isl_token_free(tok
);
2456 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
2457 isl_stream_error(s
, NULL
,
2458 "dimensions don't match");
2462 out
= n_col
- 2 - nparam
;
2463 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
2467 for (i
= 0; i
< local
; ++i
) {
2468 int k
= isl_basic_map_alloc_div(bmap
);
2471 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
2474 for (i
= 0; i
< n_row
; ++i
)
2475 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
2477 tok
= isl_stream_next_token_on_same_line(s
);
2479 isl_stream_error(s
, tok
, "unexpected extra token on line");
2480 isl_stream_push_token(s
, tok
);
2484 bmap
= isl_basic_map_simplify(bmap
);
2485 bmap
= isl_basic_map_finalize(bmap
);
2488 isl_basic_map_free(bmap
);
2492 static __isl_give isl_map
*map_read_polylib(__isl_keep isl_stream
*s
)
2494 struct isl_token
*tok
;
2495 struct isl_token
*tok2
;
2497 struct isl_map
*map
;
2499 tok
= isl_stream_next_token(s
);
2501 isl_stream_error(s
, NULL
, "unexpected EOF");
2504 tok2
= isl_stream_next_token_on_same_line(s
);
2505 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
2506 isl_stream_push_token(s
, tok2
);
2507 isl_stream_push_token(s
, tok
);
2508 return isl_map_from_basic_map(basic_map_read_polylib(s
));
2511 isl_stream_error(s
, tok2
, "unexpected token");
2512 isl_stream_push_token(s
, tok2
);
2513 isl_stream_push_token(s
, tok
);
2516 n
= isl_int_get_si(tok
->u
.v
);
2517 isl_token_free(tok
);
2519 isl_assert(s
->ctx
, n
>= 1, return NULL
);
2521 map
= isl_map_from_basic_map(basic_map_read_polylib(s
));
2523 for (i
= 1; map
&& i
< n
; ++i
)
2524 map
= isl_map_union(map
,
2525 isl_map_from_basic_map(basic_map_read_polylib(s
)));
2530 static int optional_power(__isl_keep isl_stream
*s
)
2533 struct isl_token
*tok
;
2535 tok
= isl_stream_next_token(s
);
2538 if (tok
->type
!= '^') {
2539 isl_stream_push_token(s
, tok
);
2542 isl_token_free(tok
);
2543 tok
= isl_stream_next_token(s
);
2544 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2545 isl_stream_error(s
, tok
, "expecting exponent");
2547 isl_stream_push_token(s
, tok
);
2550 pow
= isl_int_get_si(tok
->u
.v
);
2551 isl_token_free(tok
);
2555 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2556 __isl_keep isl_map
*map
, struct vars
*v
);
2558 static __isl_give isl_pw_qpolynomial
*read_factor(__isl_keep isl_stream
*s
,
2559 __isl_keep isl_map
*map
, struct vars
*v
)
2561 isl_pw_qpolynomial
*pwqp
;
2562 struct isl_token
*tok
;
2564 tok
= next_token(s
);
2566 isl_stream_error(s
, NULL
, "unexpected EOF");
2569 if (tok
->type
== '(') {
2572 isl_token_free(tok
);
2573 pwqp
= read_term(s
, map
, v
);
2576 if (isl_stream_eat(s
, ')'))
2578 pow
= optional_power(s
);
2579 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2580 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
2581 struct isl_token
*tok2
;
2582 isl_qpolynomial
*qp
;
2584 tok2
= isl_stream_next_token(s
);
2585 if (tok2
&& tok2
->type
== '/') {
2586 isl_token_free(tok2
);
2587 tok2
= next_token(s
);
2588 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2589 isl_stream_error(s
, tok2
, "expected denominator");
2590 isl_token_free(tok
);
2591 isl_token_free(tok2
);
2594 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
2595 tok
->u
.v
, tok2
->u
.v
);
2596 isl_token_free(tok2
);
2598 isl_stream_push_token(s
, tok2
);
2599 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
2602 isl_token_free(tok
);
2603 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2604 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
2605 isl_qpolynomial
*qp
;
2606 isl_token_free(tok
);
2607 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
2608 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2609 } else if (tok
->type
== ISL_TOKEN_NAN
) {
2610 isl_qpolynomial
*qp
;
2611 isl_token_free(tok
);
2612 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
2613 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2614 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
2616 int pos
= vars_pos(v
, tok
->u
.s
, -1);
2618 isl_qpolynomial
*qp
;
2620 isl_token_free(tok
);
2624 vars_drop(v
, v
->n
- n
);
2625 isl_stream_error(s
, tok
, "unknown identifier");
2626 isl_token_free(tok
);
2629 isl_token_free(tok
);
2630 pow
= optional_power(s
);
2631 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
2632 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
2633 } else if (is_start_of_div(tok
)) {
2637 isl_stream_push_token(s
, tok
);
2638 pwaff
= accept_div(s
, isl_map_get_space(map
), v
);
2639 pow
= optional_power(s
);
2640 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
2641 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
2642 } else if (tok
->type
== '-') {
2643 isl_token_free(tok
);
2644 pwqp
= read_factor(s
, map
, v
);
2645 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
2647 isl_stream_error(s
, tok
, "unexpected isl_token");
2648 isl_stream_push_token(s
, tok
);
2652 if (isl_stream_eat_if_available(s
, '*') ||
2653 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
2654 isl_pw_qpolynomial
*pwqp2
;
2656 pwqp2
= read_factor(s
, map
, v
);
2657 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
2662 isl_pw_qpolynomial_free(pwqp
);
2666 static __isl_give isl_pw_qpolynomial
*read_term(__isl_keep isl_stream
*s
,
2667 __isl_keep isl_map
*map
, struct vars
*v
)
2669 struct isl_token
*tok
;
2670 isl_pw_qpolynomial
*pwqp
;
2672 pwqp
= read_factor(s
, map
, v
);
2675 tok
= next_token(s
);
2679 if (tok
->type
== '+') {
2680 isl_pw_qpolynomial
*pwqp2
;
2682 isl_token_free(tok
);
2683 pwqp2
= read_factor(s
, map
, v
);
2684 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2685 } else if (tok
->type
== '-') {
2686 isl_pw_qpolynomial
*pwqp2
;
2688 isl_token_free(tok
);
2689 pwqp2
= read_factor(s
, map
, v
);
2690 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
2691 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
2692 isl_int_is_neg(tok
->u
.v
)) {
2693 isl_pw_qpolynomial
*pwqp2
;
2695 isl_stream_push_token(s
, tok
);
2696 pwqp2
= read_factor(s
, map
, v
);
2697 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
2699 isl_stream_push_token(s
, tok
);
2707 static __isl_give isl_map
*read_optional_formula(__isl_keep isl_stream
*s
,
2708 __isl_take isl_map
*map
, struct vars
*v
, int rational
)
2710 struct isl_token
*tok
;
2712 tok
= isl_stream_next_token(s
);
2714 isl_stream_error(s
, NULL
, "unexpected EOF");
2717 if (tok
->type
== ':' ||
2718 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
2719 isl_token_free(tok
);
2720 map
= read_formula(s
, v
, map
, rational
);
2722 isl_stream_push_token(s
, tok
);
2730 static struct isl_obj
obj_read_poly(__isl_keep isl_stream
*s
,
2731 __isl_take isl_map
*map
, struct vars
*v
, int n
)
2733 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
2734 isl_pw_qpolynomial
*pwqp
;
2735 struct isl_set
*set
;
2737 pwqp
= read_term(s
, map
, v
);
2738 map
= read_optional_formula(s
, map
, v
, 0);
2739 set
= isl_map_range(map
);
2741 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
2743 vars_drop(v
, v
->n
- n
);
2749 static struct isl_obj
obj_read_poly_or_fold(__isl_keep isl_stream
*s
,
2750 __isl_take isl_set
*set
, struct vars
*v
, int n
)
2753 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
2754 isl_pw_qpolynomial
*pwqp
;
2755 isl_pw_qpolynomial_fold
*pwf
= NULL
;
2758 max
= isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
);
2759 min
= !max
&& isl_stream_eat_if_available(s
, ISL_TOKEN_MIN
);
2761 return obj_read_poly(s
, set
, v
, n
);
2762 fold
= max
? isl_fold_max
: isl_fold_min
;
2764 if (isl_stream_eat(s
, '('))
2767 pwqp
= read_term(s
, set
, v
);
2768 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold
, pwqp
);
2770 while (isl_stream_eat_if_available(s
, ',')) {
2771 isl_pw_qpolynomial_fold
*pwf_i
;
2772 pwqp
= read_term(s
, set
, v
);
2773 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold
, pwqp
);
2774 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
2777 if (isl_stream_eat(s
, ')'))
2780 set
= read_optional_formula(s
, set
, v
, 0);
2781 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
2783 vars_drop(v
, v
->n
- n
);
2789 isl_pw_qpolynomial_fold_free(pwf
);
2790 obj
.type
= isl_obj_none
;
2794 static int is_rational(__isl_keep isl_stream
*s
)
2796 struct isl_token
*tok
;
2798 tok
= isl_stream_next_token(s
);
2801 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
2802 isl_token_free(tok
);
2803 isl_stream_eat(s
, ':');
2807 isl_stream_push_token(s
, tok
);
2812 static struct isl_obj
obj_read_body(__isl_keep isl_stream
*s
,
2813 __isl_take isl_map
*map
, struct vars
*v
)
2815 struct isl_token
*tok
;
2816 struct isl_obj obj
= { isl_obj_set
, NULL
};
2820 rational
= is_rational(s
);
2822 map
= isl_map_set_rational(map
);
2824 if (isl_stream_next_token_is(s
, ':')) {
2825 obj
.type
= isl_obj_set
;
2826 obj
.v
= read_optional_formula(s
, map
, v
, rational
);
2830 if (!next_is_tuple(s
))
2831 return obj_read_poly_or_fold(s
, map
, v
, n
);
2833 map
= read_map_tuple(s
, map
, isl_dim_in
, v
, rational
, 0);
2836 tok
= isl_stream_next_token(s
);
2839 if (tok
->type
== ISL_TOKEN_TO
) {
2840 obj
.type
= isl_obj_map
;
2841 isl_token_free(tok
);
2842 if (!next_is_tuple(s
)) {
2843 isl_set
*set
= isl_map_domain(map
);
2844 return obj_read_poly_or_fold(s
, set
, v
, n
);
2846 map
= read_map_tuple(s
, map
, isl_dim_out
, v
, rational
, 0);
2850 map
= isl_map_domain(map
);
2851 isl_stream_push_token(s
, tok
);
2854 map
= read_optional_formula(s
, map
, v
, rational
);
2856 vars_drop(v
, v
->n
- n
);
2862 obj
.type
= isl_obj_none
;
2866 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
2868 if (obj
.type
== isl_obj_map
) {
2869 obj
.v
= isl_union_map_from_map(obj
.v
);
2870 obj
.type
= isl_obj_union_map
;
2871 } else if (obj
.type
== isl_obj_set
) {
2872 obj
.v
= isl_union_set_from_set(obj
.v
);
2873 obj
.type
= isl_obj_union_set
;
2874 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
2875 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
2876 obj
.type
= isl_obj_union_pw_qpolynomial
;
2877 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
2878 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
2879 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
2881 isl_assert(ctx
, 0, goto error
);
2884 obj
.type
->free(obj
.v
);
2885 obj
.type
= isl_obj_none
;
2889 static struct isl_obj
obj_add(__isl_keep isl_stream
*s
,
2890 struct isl_obj obj1
, struct isl_obj obj2
)
2892 if (obj2
.type
== isl_obj_none
|| !obj2
.v
)
2894 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
2895 obj1
= to_union(s
->ctx
, obj1
);
2896 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
2897 obj2
= to_union(s
->ctx
, obj2
);
2898 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
2899 obj1
= to_union(s
->ctx
, obj1
);
2900 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
2901 obj2
= to_union(s
->ctx
, obj2
);
2902 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2903 obj2
.type
== isl_obj_union_pw_qpolynomial
)
2904 obj1
= to_union(s
->ctx
, obj1
);
2905 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
2906 obj2
.type
== isl_obj_pw_qpolynomial
)
2907 obj2
= to_union(s
->ctx
, obj2
);
2908 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2909 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
2910 obj1
= to_union(s
->ctx
, obj1
);
2911 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
2912 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
2913 obj2
= to_union(s
->ctx
, obj2
);
2914 if (obj1
.type
!= obj2
.type
) {
2915 isl_stream_error(s
, NULL
,
2916 "attempt to combine incompatible objects");
2919 if (!obj1
.type
->add
)
2920 isl_die(s
->ctx
, isl_error_internal
,
2921 "combination not supported on object type", goto error
);
2922 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
2923 obj1
= to_union(s
->ctx
, obj1
);
2924 obj2
= to_union(s
->ctx
, obj2
);
2926 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
2927 obj1
= to_union(s
->ctx
, obj1
);
2928 obj2
= to_union(s
->ctx
, obj2
);
2930 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
2931 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
2932 obj1
= to_union(s
->ctx
, obj1
);
2933 obj2
= to_union(s
->ctx
, obj2
);
2935 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
2936 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
2937 obj1
= to_union(s
->ctx
, obj1
);
2938 obj2
= to_union(s
->ctx
, obj2
);
2940 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
2943 obj1
.type
->free(obj1
.v
);
2944 obj2
.type
->free(obj2
.v
);
2945 obj1
.type
= isl_obj_none
;
2950 /* Are the first two tokens on "s", "domain" (either as a string
2951 * or as an identifier) followed by ":"?
2953 static int next_is_domain_colon(__isl_keep isl_stream
*s
)
2955 struct isl_token
*tok
;
2959 tok
= isl_stream_next_token(s
);
2962 if (tok
->type
!= ISL_TOKEN_IDENT
&& tok
->type
!= ISL_TOKEN_STRING
) {
2963 isl_stream_push_token(s
, tok
);
2967 name
= isl_token_get_str(s
->ctx
, tok
);
2968 res
= !strcmp(name
, "domain") && isl_stream_next_token_is(s
, ':');
2971 isl_stream_push_token(s
, tok
);
2976 /* Do the first tokens on "s" look like a schedule?
2978 * The root of a schedule is always a domain node, so the first thing
2979 * we expect in the stream is a domain key, i.e., "domain" followed
2980 * by ":". If the schedule was printed in YAML flow style, then
2981 * we additionally expect a "{" to open the outer mapping.
2983 static int next_is_schedule(__isl_keep isl_stream
*s
)
2985 struct isl_token
*tok
;
2988 tok
= isl_stream_next_token(s
);
2991 if (tok
->type
!= '{') {
2992 isl_stream_push_token(s
, tok
);
2993 return next_is_domain_colon(s
);
2996 is_schedule
= next_is_domain_colon(s
);
2997 isl_stream_push_token(s
, tok
);
3002 /* Read an isl_schedule from "s" and store it in an isl_obj.
3004 static struct isl_obj
schedule_read(__isl_keep isl_stream
*s
)
3008 obj
.type
= isl_obj_schedule
;
3009 obj
.v
= isl_stream_read_schedule(s
);
3014 /* Read a disjunction of object bodies from "s".
3015 * That is, read the inside of the braces, but not the braces themselves.
3016 * "v" contains a description of the identifiers parsed so far.
3017 * "map" contains information about the parameters.
3019 static struct isl_obj
obj_read_disjuncts(__isl_keep isl_stream
*s
,
3020 struct vars
*v
, __isl_keep isl_map
*map
)
3022 struct isl_obj obj
= { isl_obj_set
, NULL
};
3024 if (isl_stream_next_token_is(s
, '}')) {
3025 obj
.type
= isl_obj_union_set
;
3026 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
3032 o
= obj_read_body(s
, isl_map_copy(map
), v
);
3036 obj
= obj_add(s
, obj
, o
);
3037 if (obj
.type
== isl_obj_none
|| !obj
.v
)
3039 if (!isl_stream_eat_if_available(s
, ';'))
3041 if (isl_stream_next_token_is(s
, '}'))
3048 static struct isl_obj
obj_read(__isl_keep isl_stream
*s
)
3050 isl_map
*map
= NULL
;
3051 struct isl_token
*tok
;
3052 struct vars
*v
= NULL
;
3053 struct isl_obj obj
= { isl_obj_set
, NULL
};
3055 if (next_is_schedule(s
))
3056 return schedule_read(s
);
3058 tok
= next_token(s
);
3060 isl_stream_error(s
, NULL
, "unexpected EOF");
3063 if (tok
->type
== ISL_TOKEN_VALUE
) {
3064 struct isl_token
*tok2
;
3065 struct isl_map
*map
;
3067 tok2
= isl_stream_next_token(s
);
3068 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
3069 isl_int_is_neg(tok2
->u
.v
)) {
3071 isl_stream_push_token(s
, tok2
);
3072 obj
.type
= isl_obj_val
;
3073 obj
.v
= isl_val_int_from_isl_int(s
->ctx
, tok
->u
.v
);
3074 isl_token_free(tok
);
3077 isl_stream_push_token(s
, tok2
);
3078 isl_stream_push_token(s
, tok
);
3079 map
= map_read_polylib(s
);
3082 if (isl_map_may_be_set(map
))
3083 obj
.v
= isl_map_range(map
);
3085 obj
.type
= isl_obj_map
;
3090 v
= vars_new(s
->ctx
);
3092 isl_stream_push_token(s
, tok
);
3095 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
3096 if (tok
->type
== '[') {
3097 isl_stream_push_token(s
, tok
);
3098 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 0);
3101 tok
= isl_stream_next_token(s
);
3102 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
3103 isl_stream_error(s
, tok
, "expecting '->'");
3105 isl_stream_push_token(s
, tok
);
3108 isl_token_free(tok
);
3109 tok
= isl_stream_next_token(s
);
3111 if (!tok
|| tok
->type
!= '{') {
3112 isl_stream_error(s
, tok
, "expecting '{'");
3114 isl_stream_push_token(s
, tok
);
3117 isl_token_free(tok
);
3119 tok
= isl_stream_next_token(s
);
3122 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
3123 isl_token_free(tok
);
3124 if (isl_stream_eat(s
, '='))
3126 map
= read_map_tuple(s
, map
, isl_dim_param
, v
, 0, 1);
3130 isl_stream_push_token(s
, tok
);
3132 obj
= obj_read_disjuncts(s
, v
, map
);
3133 if (obj
.type
== isl_obj_none
|| !obj
.v
)
3136 tok
= isl_stream_next_token(s
);
3137 if (tok
&& tok
->type
== '}') {
3138 isl_token_free(tok
);
3140 isl_stream_error(s
, tok
, "unexpected isl_token");
3142 isl_token_free(tok
);
3152 obj
.type
->free(obj
.v
);
3159 struct isl_obj
isl_stream_read_obj(__isl_keep isl_stream
*s
)
3164 __isl_give isl_map
*isl_stream_read_map(__isl_keep isl_stream
*s
)
3170 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
3171 obj
.type
== isl_obj_set
, goto error
);
3173 if (obj
.type
== isl_obj_set
)
3174 obj
.v
= isl_map_from_range(obj
.v
);
3178 obj
.type
->free(obj
.v
);
3182 __isl_give isl_set
*isl_stream_read_set(__isl_keep isl_stream
*s
)
3188 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
3189 obj
.v
= isl_map_range(obj
.v
);
3190 obj
.type
= isl_obj_set
;
3192 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
3197 obj
.type
->free(obj
.v
);
3201 __isl_give isl_union_map
*isl_stream_read_union_map(__isl_keep isl_stream
*s
)
3206 if (obj
.type
== isl_obj_map
) {
3207 obj
.type
= isl_obj_union_map
;
3208 obj
.v
= isl_union_map_from_map(obj
.v
);
3210 if (obj
.type
== isl_obj_set
) {
3211 obj
.type
= isl_obj_union_set
;
3212 obj
.v
= isl_union_set_from_set(obj
.v
);
3214 if (obj
.v
&& obj
.type
== isl_obj_union_set
&&
3215 isl_union_set_is_empty(obj
.v
))
3216 obj
.type
= isl_obj_union_map
;
3217 if (obj
.v
&& obj
.type
!= isl_obj_union_map
)
3218 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
3222 obj
.type
->free(obj
.v
);
3226 /* Extract an isl_union_set from "obj".
3227 * This only works if the object was detected as either a set
3228 * (in which case it is converted to a union set) or a union set.
3230 static __isl_give isl_union_set
*extract_union_set(isl_ctx
*ctx
,
3233 if (obj
.type
== isl_obj_set
) {
3234 obj
.type
= isl_obj_union_set
;
3235 obj
.v
= isl_union_set_from_set(obj
.v
);
3238 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
3242 obj
.type
->free(obj
.v
);
3246 /* Read an isl_union_set from "s".
3247 * First read a generic object and then try and extract
3248 * an isl_union_set from that.
3250 __isl_give isl_union_set
*isl_stream_read_union_set(__isl_keep isl_stream
*s
)
3255 return extract_union_set(s
->ctx
, obj
);
3258 static __isl_give isl_basic_map
*basic_map_read(__isl_keep isl_stream
*s
)
3261 struct isl_map
*map
;
3262 struct isl_basic_map
*bmap
;
3265 if (obj
.v
&& (obj
.type
!= isl_obj_map
&& obj
.type
!= isl_obj_set
))
3266 isl_die(s
->ctx
, isl_error_invalid
, "not a (basic) set or map",
3273 isl_die(s
->ctx
, isl_error_invalid
,
3274 "set or map description involves "
3275 "more than one disjunct", goto error
);
3278 bmap
= isl_basic_map_empty(isl_map_get_space(map
));
3280 bmap
= isl_basic_map_copy(map
->p
[0]);
3286 obj
.type
->free(obj
.v
);
3290 static __isl_give isl_basic_set
*basic_set_read(__isl_keep isl_stream
*s
)
3292 isl_basic_map
*bmap
;
3293 bmap
= basic_map_read(s
);
3296 if (!isl_basic_map_may_be_set(bmap
))
3297 isl_die(s
->ctx
, isl_error_invalid
,
3298 "input is not a set", goto error
);
3299 return isl_basic_map_range(bmap
);
3301 isl_basic_map_free(bmap
);
3305 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
3308 struct isl_basic_map
*bmap
;
3309 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3312 bmap
= basic_map_read(s
);
3317 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
3320 isl_basic_set
*bset
;
3321 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3324 bset
= basic_set_read(s
);
3329 __isl_give isl_basic_map
*isl_basic_map_read_from_str(isl_ctx
*ctx
,
3332 struct isl_basic_map
*bmap
;
3333 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3336 bmap
= basic_map_read(s
);
3341 __isl_give isl_basic_set
*isl_basic_set_read_from_str(isl_ctx
*ctx
,
3344 isl_basic_set
*bset
;
3345 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3348 bset
= basic_set_read(s
);
3353 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
3356 struct isl_map
*map
;
3357 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3360 map
= isl_stream_read_map(s
);
3365 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
3368 struct isl_map
*map
;
3369 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3372 map
= isl_stream_read_map(s
);
3377 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
3381 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3384 set
= isl_stream_read_set(s
);
3389 __isl_give isl_set
*isl_set_read_from_str(isl_ctx
*ctx
, const char *str
)
3392 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3395 set
= isl_stream_read_set(s
);
3400 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
3403 isl_union_map
*umap
;
3404 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3407 umap
= isl_stream_read_union_map(s
);
3412 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
3415 isl_union_map
*umap
;
3416 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3419 umap
= isl_stream_read_union_map(s
);
3424 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
3427 isl_union_set
*uset
;
3428 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3431 uset
= isl_stream_read_union_set(s
);
3436 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
3439 isl_union_set
*uset
;
3440 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3443 uset
= isl_stream_read_union_set(s
);
3448 static __isl_give isl_vec
*isl_vec_read_polylib(__isl_keep isl_stream
*s
)
3450 struct isl_vec
*vec
= NULL
;
3451 struct isl_token
*tok
;
3455 tok
= isl_stream_next_token(s
);
3456 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3457 isl_stream_error(s
, tok
, "expecting vector length");
3461 size
= isl_int_get_si(tok
->u
.v
);
3462 isl_token_free(tok
);
3464 vec
= isl_vec_alloc(s
->ctx
, size
);
3466 for (j
= 0; j
< size
; ++j
) {
3467 tok
= isl_stream_next_token(s
);
3468 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
3469 isl_stream_error(s
, tok
, "expecting constant value");
3472 isl_int_set(vec
->el
[j
], tok
->u
.v
);
3473 isl_token_free(tok
);
3478 isl_token_free(tok
);
3483 static __isl_give isl_vec
*vec_read(__isl_keep isl_stream
*s
)
3485 return isl_vec_read_polylib(s
);
3488 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
3491 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3499 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
3500 __isl_keep isl_stream
*s
)
3506 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
3511 obj
.type
->free(obj
.v
);
3515 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
3518 isl_pw_qpolynomial
*pwqp
;
3519 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3522 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3527 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
3530 isl_pw_qpolynomial
*pwqp
;
3531 isl_stream
*s
= isl_stream_new_file(ctx
, input
);
3534 pwqp
= isl_stream_read_pw_qpolynomial(s
);
3539 /* Read an isl_pw_qpolynomial_fold from "s".
3540 * First read a generic object and
3541 * then check that it is an isl_pw_qpolynomial_fold.
3543 __isl_give isl_pw_qpolynomial_fold
*isl_stream_read_pw_qpolynomial_fold(
3544 __isl_keep isl_stream
*s
)
3549 if (obj
.v
&& obj
.type
!= isl_obj_pw_qpolynomial_fold
)
3550 isl_die(s
->ctx
, isl_error_invalid
, "invalid input", goto error
);
3554 obj
.type
->free(obj
.v
);
3558 /* Read an isl_pw_qpolynomial_fold from "str".
3560 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_read_from_str(
3561 isl_ctx
*ctx
, const char *str
)
3563 isl_pw_qpolynomial_fold
*pwqp
;
3566 s
= isl_stream_new_str(ctx
, str
);
3569 pwqp
= isl_stream_read_pw_qpolynomial_fold(s
);
3575 /* Is the next token an identifier not in "v"?
3577 static int next_is_fresh_ident(__isl_keep isl_stream
*s
, struct vars
*v
)
3581 struct isl_token
*tok
;
3583 tok
= isl_stream_next_token(s
);
3586 fresh
= tok
->type
== ISL_TOKEN_IDENT
&& vars_pos(v
, tok
->u
.s
, -1) >= n
;
3587 isl_stream_push_token(s
, tok
);
3589 vars_drop(v
, v
->n
- n
);
3594 /* First read the domain of the affine expression, which may be
3595 * a parameter space or a set.
3596 * The tricky part is that we don't know if the domain is a set or not,
3597 * so when we are trying to read the domain, we may actually be reading
3598 * the affine expression itself (defined on a parameter domains)
3599 * If the tuple we are reading is named, we assume it's the domain.
3600 * Also, if inside the tuple, the first thing we find is a nested tuple
3601 * or a new identifier, we again assume it's the domain.
3602 * Finally, if the tuple is empty, then it must be the domain
3603 * since it does not contain an affine expression.
3604 * Otherwise, we assume we are reading an affine expression.
3606 static __isl_give isl_set
*read_aff_domain(__isl_keep isl_stream
*s
,
3607 __isl_take isl_set
*dom
, struct vars
*v
)
3609 struct isl_token
*tok
, *tok2
;
3612 tok
= isl_stream_next_token(s
);
3613 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
3614 isl_stream_push_token(s
, tok
);
3615 return read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3617 if (!tok
|| tok
->type
!= '[') {
3618 isl_stream_error(s
, tok
, "expecting '['");
3621 tok2
= isl_stream_next_token(s
);
3622 is_empty
= tok2
&& tok2
->type
== ']';
3624 isl_stream_push_token(s
, tok2
);
3625 if (is_empty
|| next_is_tuple(s
) || next_is_fresh_ident(s
, v
)) {
3626 isl_stream_push_token(s
, tok
);
3627 dom
= read_map_tuple(s
, dom
, isl_dim_set
, v
, 0, 0);
3629 isl_stream_push_token(s
, tok
);
3634 isl_stream_push_token(s
, tok
);
3639 /* Read an affine expression from "s".
3641 __isl_give isl_aff
*isl_stream_read_aff(__isl_keep isl_stream
*s
)
3647 ma
= isl_stream_read_multi_aff(s
);
3648 dim
= isl_multi_aff_dim(ma
, isl_dim_out
);
3652 isl_die(s
->ctx
, isl_error_invalid
,
3653 "expecting single affine expression",
3656 aff
= isl_multi_aff_get_aff(ma
, 0);
3657 isl_multi_aff_free(ma
);
3660 isl_multi_aff_free(ma
);
3664 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3666 static __isl_give isl_pw_aff
*read_pw_aff_with_dom(__isl_keep isl_stream
*s
,
3667 __isl_take isl_set
*dom
, struct vars
*v
)
3669 isl_pw_aff
*pwaff
= NULL
;
3671 if (!isl_set_is_params(dom
) && isl_stream_eat(s
, ISL_TOKEN_TO
))
3674 if (isl_stream_eat(s
, '['))
3677 pwaff
= accept_affine(s
, isl_set_get_space(dom
), v
);
3679 if (isl_stream_eat(s
, ']'))
3682 dom
= read_optional_formula(s
, dom
, v
, 0);
3683 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
3688 isl_pw_aff_free(pwaff
);
3692 __isl_give isl_pw_aff
*isl_stream_read_pw_aff(__isl_keep isl_stream
*s
)
3695 isl_set
*dom
= NULL
;
3697 isl_pw_aff
*pa
= NULL
;
3700 v
= vars_new(s
->ctx
);
3704 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3705 if (next_is_tuple(s
)) {
3706 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3707 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3710 if (isl_stream_eat(s
, '{'))
3714 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3715 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3716 vars_drop(v
, v
->n
- n
);
3718 while (isl_stream_eat_if_available(s
, ';')) {
3722 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
3723 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
3724 vars_drop(v
, v
->n
- n
);
3726 pa
= isl_pw_aff_union_add(pa
, pa_i
);
3729 if (isl_stream_eat(s
, '}'))
3738 isl_pw_aff_free(pa
);
3742 __isl_give isl_aff
*isl_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3745 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3748 aff
= isl_stream_read_aff(s
);
3753 __isl_give isl_pw_aff
*isl_pw_aff_read_from_str(isl_ctx
*ctx
, const char *str
)
3756 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3759 pa
= isl_stream_read_pw_aff(s
);
3764 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3765 * from a tuple "tuple" read by read_tuple.
3767 * Note that the function read_tuple accepts tuples where some output or
3768 * set dimensions are defined in terms of other output or set dimensions
3769 * since this function is also used to read maps. As a special case,
3770 * read_tuple also accepts dimensions that are defined in terms of themselves
3771 * (i.e., that are not defined).
3772 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3773 * that the definitions of the output/set dimensions do not involve any
3774 * output/set dimensions.
3775 * Finally, drop the output dimensions from the domain of the result
3776 * of read_tuple (which is of the form [input, output] -> [output],
3777 * with anonymous domain) and reset the space.
3779 static __isl_give isl_multi_pw_aff
*extract_mpa_from_tuple(
3780 __isl_take isl_space
*dom_space
, __isl_keep isl_multi_pw_aff
*tuple
)
3785 isl_multi_pw_aff
*mpa
;
3787 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
3788 dim
= isl_space_dim(dom_space
, isl_dim_all
);
3789 if (n
< 0 || dim
< 0)
3790 dom_space
= isl_space_free(dom_space
);
3791 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
3792 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
3793 if (!isl_space_is_params(dom_space
))
3794 space
= isl_space_map_from_domain_and_range(
3795 isl_space_copy(dom_space
), space
);
3796 isl_space_free(dom_space
);
3797 mpa
= isl_multi_pw_aff_alloc(space
);
3799 for (i
= 0; i
< n
; ++i
) {
3801 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
3803 return isl_multi_pw_aff_free(mpa
);
3804 if (isl_pw_aff_involves_dims(pa
, isl_dim_in
, dim
, i
+ 1)) {
3805 isl_ctx
*ctx
= isl_pw_aff_get_ctx(pa
);
3806 isl_pw_aff_free(pa
);
3807 isl_die(ctx
, isl_error_invalid
,
3808 "not an affine expression",
3809 return isl_multi_pw_aff_free(mpa
));
3811 pa
= isl_pw_aff_drop_dims(pa
, isl_dim_in
, dim
, n
);
3812 space
= isl_multi_pw_aff_get_domain_space(mpa
);
3813 pa
= isl_pw_aff_reset_domain_space(pa
, space
);
3814 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
3820 /* Read a tuple of affine expressions, together with optional constraints
3821 * on the domain from "s". "dom" represents the initial constraints
3824 * The isl_multi_aff may live in either a set or a map space.
3825 * First read the first tuple and check if it is followed by a "->".
3826 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3827 * read in the next tuple. This tuple (or the first tuple if it was
3828 * not followed by a "->") is then converted into an isl_multi_pw_aff
3829 * through a call to extract_mpa_from_tuple.
3830 * The result is converted to an isl_pw_multi_aff and
3831 * its domain is intersected with the domain.
3833 * Note that the last tuple may introduce new identifiers,
3834 * but these cannot be referenced in the description of the domain.
3836 static __isl_give isl_pw_multi_aff
*read_conditional_multi_aff(
3837 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
3839 isl_multi_pw_aff
*tuple
;
3840 isl_multi_pw_aff
*mpa
;
3841 isl_pw_multi_aff
*pma
;
3846 tuple
= read_tuple(s
, v
, 0, 0);
3849 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
3850 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
3851 dom
= isl_map_domain(map
);
3853 tuple
= read_tuple(s
, v
, 0, 0);
3857 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
3858 isl_multi_pw_aff_free(tuple
);
3860 dom
= isl_set_free(dom
);
3862 vars_drop(v
, v
->n
- n_dom
);
3863 dom
= read_optional_formula(s
, dom
, v
, 0);
3865 vars_drop(v
, v
->n
- n
);
3867 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
3868 pma
= isl_pw_multi_aff_intersect_domain(pma
, dom
);
3876 /* Read an isl_union_pw_multi_aff from "s".
3878 * In particular, first read the parameters and then read a sequence
3879 * of zero or more tuples of affine expressions with optional conditions and
3882 __isl_give isl_union_pw_multi_aff
*isl_stream_read_union_pw_multi_aff(
3883 __isl_keep isl_stream
*s
)
3887 isl_union_pw_multi_aff
*upma
= NULL
;
3889 v
= vars_new(s
->ctx
);
3893 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
3894 if (next_is_tuple(s
)) {
3895 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
3896 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
3899 if (isl_stream_eat(s
, '{'))
3902 upma
= isl_union_pw_multi_aff_empty(isl_set_get_space(dom
));
3905 isl_pw_multi_aff
*pma
;
3906 isl_union_pw_multi_aff
*upma2
;
3908 if (isl_stream_next_token_is(s
, '}'))
3911 pma
= read_conditional_multi_aff(s
, isl_set_copy(dom
), v
);
3912 upma2
= isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
3913 upma
= isl_union_pw_multi_aff_union_add(upma
, upma2
);
3916 } while (isl_stream_eat_if_available(s
, ';'));
3918 if (isl_stream_eat(s
, '}'))
3925 isl_union_pw_multi_aff_free(upma
);
3931 /* Read an isl_pw_multi_aff from "s".
3933 * Read a more generic isl_union_pw_multi_aff first and
3934 * then check that the result lives in a single space.
3936 __isl_give isl_pw_multi_aff
*isl_stream_read_pw_multi_aff(
3937 __isl_keep isl_stream
*s
)
3939 isl_bool single_space
;
3940 isl_union_pw_multi_aff
*upma
;
3942 upma
= isl_stream_read_union_pw_multi_aff(s
);
3943 single_space
= isl_union_pw_multi_aff_isa_pw_multi_aff(upma
);
3944 if (single_space
< 0)
3945 upma
= isl_union_pw_multi_aff_free(upma
);
3946 else if (!single_space
)
3947 isl_die(s
->ctx
, isl_error_invalid
,
3948 "expecting expression in single space",
3949 upma
= isl_union_pw_multi_aff_free(upma
));
3950 return isl_union_pw_multi_aff_as_pw_multi_aff(upma
);
3953 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_read_from_str(isl_ctx
*ctx
,
3956 isl_pw_multi_aff
*pma
;
3957 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3960 pma
= isl_stream_read_pw_multi_aff(s
);
3965 /* Read an isl_union_pw_multi_aff from "str".
3967 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_read_from_str(
3968 isl_ctx
*ctx
, const char *str
)
3970 isl_union_pw_multi_aff
*upma
;
3971 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
3974 upma
= isl_stream_read_union_pw_multi_aff(s
);
3979 /* Assuming "pa" represents a single affine expression defined on a universe
3980 * domain, extract this affine expression.
3982 static __isl_give isl_aff
*aff_from_pw_aff(__isl_take isl_pw_aff
*pa
)
3989 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3990 "expecting single affine expression",
3992 if (!isl_set_plain_is_universe(pa
->p
[0].set
))
3993 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3994 "expecting universe domain",
3997 aff
= isl_aff_copy(pa
->p
[0].aff
);
3998 isl_pw_aff_free(pa
);
4001 isl_pw_aff_free(pa
);
4008 #include <isl_multi_read_no_explicit_domain_templ.c>
4013 #include <isl_multi_read_no_explicit_domain_templ.c>
4015 /* Read a multi-affine expression from "s".
4016 * If the multi-affine expression has a domain, then the tuple
4017 * representing this domain cannot involve any affine expressions.
4018 * The tuple representing the actual expressions needs to consist
4019 * of only affine expressions. Moreover, these expressions can
4020 * only depend on parameters and input dimensions and not on other
4021 * output dimensions.
4023 __isl_give isl_multi_aff
*isl_stream_read_multi_aff(__isl_keep isl_stream
*s
)
4026 isl_set
*dom
= NULL
;
4027 isl_multi_pw_aff
*tuple
= NULL
;
4030 isl_space
*space
, *dom_space
;
4031 isl_multi_aff
*ma
= NULL
;
4033 v
= vars_new(s
->ctx
);
4037 dom
= read_universe_params(s
, v
);
4040 if (isl_stream_eat(s
, '{'))
4043 tuple
= read_tuple(s
, v
, 0, 0);
4046 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
4051 has_expr
= tuple_has_expr(tuple
);
4055 isl_die(s
->ctx
, isl_error_invalid
,
4056 "expecting universe domain", goto error
);
4057 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
4058 set
= isl_set_universe(space
);
4059 dom
= isl_set_intersect_params(set
, dom
);
4060 isl_multi_pw_aff_free(tuple
);
4061 tuple
= read_tuple(s
, v
, 0, 0);
4066 if (isl_stream_eat(s
, '}'))
4069 n
= isl_multi_pw_aff_dim(tuple
, isl_dim_out
);
4070 dim
= isl_set_dim(dom
, isl_dim_all
);
4071 if (n
< 0 || dim
< 0)
4073 dom_space
= isl_set_get_space(dom
);
4074 space
= isl_space_range(isl_multi_pw_aff_get_space(tuple
));
4075 space
= isl_space_align_params(space
, isl_space_copy(dom_space
));
4076 if (!isl_space_is_params(dom_space
))
4077 space
= isl_space_map_from_domain_and_range(
4078 isl_space_copy(dom_space
), space
);
4079 isl_space_free(dom_space
);
4080 ma
= isl_multi_aff_alloc(space
);
4082 for (i
= 0; i
< n
; ++i
) {
4085 pa
= isl_multi_pw_aff_get_pw_aff(tuple
, i
);
4086 aff
= aff_from_pw_aff(pa
);
4089 if (isl_aff_involves_dims(aff
, isl_dim_in
, dim
, i
+ 1)) {
4091 isl_die(s
->ctx
, isl_error_invalid
,
4092 "not an affine expression", goto error
);
4094 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, dim
, n
);
4095 space
= isl_multi_aff_get_domain_space(ma
);
4096 aff
= isl_aff_reset_domain_space(aff
, space
);
4097 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4100 isl_multi_pw_aff_free(tuple
);
4105 isl_multi_pw_aff_free(tuple
);
4108 isl_multi_aff_free(ma
);
4112 __isl_give isl_multi_aff
*isl_multi_aff_read_from_str(isl_ctx
*ctx
,
4115 isl_multi_aff
*maff
;
4116 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4119 maff
= isl_stream_read_multi_aff(s
);
4124 /* Read an isl_multi_pw_aff from "s".
4126 * The input format is similar to that of map, except that any conditions
4127 * on the domains should be specified inside the tuple since each
4128 * piecewise affine expression may have a different domain.
4129 * However, additional, shared conditions can also be specified.
4130 * This is especially useful for setting the explicit domain
4131 * of a zero-dimensional isl_multi_pw_aff.
4133 * Since we do not know in advance if the isl_multi_pw_aff lives
4134 * in a set or a map space, we first read the first tuple and check
4135 * if it is followed by a "->". If so, we convert the tuple into
4136 * the domain of the isl_multi_pw_aff and read in the next tuple.
4137 * This tuple (or the first tuple if it was not followed by a "->")
4138 * is then converted into the isl_multi_pw_aff through a call
4139 * to extract_mpa_from_tuple and the domain of the result
4140 * is intersected with the domain.
4142 * Note that the last tuple may introduce new identifiers,
4143 * but these cannot be referenced in the description of the domain.
4145 __isl_give isl_multi_pw_aff
*isl_stream_read_multi_pw_aff(
4146 __isl_keep isl_stream
*s
)
4150 isl_set
*dom
= NULL
;
4151 isl_multi_pw_aff
*tuple
= NULL
;
4152 isl_multi_pw_aff
*mpa
= NULL
;
4154 v
= vars_new(s
->ctx
);
4158 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4159 if (next_is_tuple(s
)) {
4160 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4161 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4164 if (isl_stream_eat(s
, '{'))
4168 tuple
= read_tuple(s
, v
, 0, 0);
4171 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TO
)) {
4172 isl_map
*map
= map_from_tuple(tuple
, dom
, isl_dim_in
, v
, 0);
4173 dom
= isl_map_domain(map
);
4175 tuple
= read_tuple(s
, v
, 0, 0);
4180 vars_drop(v
, v
->n
- n_dom
);
4181 if (isl_stream_eat_if_available(s
, ':'))
4182 dom
= read_formula(s
, v
, dom
, 0);
4184 if (isl_stream_eat(s
, '}'))
4187 mpa
= extract_mpa_from_tuple(isl_set_get_space(dom
), tuple
);
4189 isl_multi_pw_aff_free(tuple
);
4191 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
4194 isl_multi_pw_aff_free(tuple
);
4197 isl_multi_pw_aff_free(mpa
);
4201 /* Read an isl_multi_pw_aff from "str".
4203 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_read_from_str(isl_ctx
*ctx
,
4206 isl_multi_pw_aff
*mpa
;
4207 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4210 mpa
= isl_stream_read_multi_pw_aff(s
);
4215 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
4217 static __isl_give isl_union_pw_aff
*read_union_pw_aff_with_dom(
4218 __isl_keep isl_stream
*s
, __isl_take isl_set
*dom
, struct vars
*v
)
4221 isl_union_pw_aff
*upa
= NULL
;
4226 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
4227 pa
= read_pw_aff_with_dom(s
, aff_dom
, v
);
4228 vars_drop(v
, v
->n
- n
);
4230 upa
= isl_union_pw_aff_from_pw_aff(pa
);
4232 while (isl_stream_eat_if_available(s
, ';')) {
4234 isl_union_pw_aff
*upa_i
;
4237 aff_dom
= read_aff_domain(s
, isl_set_copy(dom
), v
);
4238 pa_i
= read_pw_aff_with_dom(s
, aff_dom
, v
);
4239 vars_drop(v
, v
->n
- n
);
4241 upa_i
= isl_union_pw_aff_from_pw_aff(pa_i
);
4242 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
4249 /* Read an isl_union_pw_aff from "s".
4251 * First check if there are any paramters, then read in the opening brace
4252 * and use read_union_pw_aff_with_dom to read in the body of
4253 * the isl_union_pw_aff. Finally, read the closing brace.
4255 __isl_give isl_union_pw_aff
*isl_stream_read_union_pw_aff(
4256 __isl_keep isl_stream
*s
)
4260 isl_union_pw_aff
*upa
= NULL
;
4262 v
= vars_new(s
->ctx
);
4266 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4267 if (next_is_tuple(s
)) {
4268 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4269 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4272 if (isl_stream_eat(s
, '{'))
4275 upa
= read_union_pw_aff_with_dom(s
, isl_set_copy(dom
), v
);
4277 if (isl_stream_eat(s
, '}'))
4286 isl_union_pw_aff_free(upa
);
4290 /* Read an isl_union_pw_aff from "str".
4292 __isl_give isl_union_pw_aff
*isl_union_pw_aff_read_from_str(isl_ctx
*ctx
,
4295 isl_union_pw_aff
*upa
;
4296 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4299 upa
= isl_stream_read_union_pw_aff(s
);
4304 /* This function is called for each element in a tuple inside
4305 * isl_stream_read_multi_union_pw_aff.
4307 * Read a '{', the union piecewise affine expression body and a '}' and
4308 * add the isl_union_pw_aff to *list.
4310 static __isl_give isl_space
*read_union_pw_aff_el(__isl_keep isl_stream
*s
,
4311 struct vars
*v
, __isl_take isl_space
*space
, int rational
, void *user
)
4314 isl_union_pw_aff
*upa
;
4315 isl_union_pw_aff_list
**list
= (isl_union_pw_aff_list
**) user
;
4317 dom
= isl_set_universe(isl_space_params(isl_space_copy(space
)));
4318 if (isl_stream_eat(s
, '{'))
4320 upa
= read_union_pw_aff_with_dom(s
, dom
, v
);
4321 *list
= isl_union_pw_aff_list_add(*list
, upa
);
4322 if (isl_stream_eat(s
, '}'))
4323 return isl_space_free(space
);
4325 return isl_space_free(space
);
4329 return isl_space_free(space
);
4332 /* Do the next tokens in "s" correspond to an empty tuple?
4333 * In particular, does the stream start with a '[', followed by a ']',
4334 * not followed by a "->"?
4336 static int next_is_empty_tuple(__isl_keep isl_stream
*s
)
4338 struct isl_token
*tok
, *tok2
, *tok3
;
4339 int is_empty_tuple
= 0;
4341 tok
= isl_stream_next_token(s
);
4344 if (tok
->type
!= '[') {
4345 isl_stream_push_token(s
, tok
);
4349 tok2
= isl_stream_next_token(s
);
4350 if (tok2
&& tok2
->type
== ']') {
4351 tok3
= isl_stream_next_token(s
);
4352 is_empty_tuple
= !tok
|| tok
->type
!= ISL_TOKEN_TO
;
4354 isl_stream_push_token(s
, tok3
);
4357 isl_stream_push_token(s
, tok2
);
4358 isl_stream_push_token(s
, tok
);
4360 return is_empty_tuple
;
4363 /* Do the next tokens in "s" correspond to a tuple of parameters?
4364 * In particular, does the stream start with a '[' that is not
4365 * followed by a '{' or a nested tuple?
4367 static int next_is_param_tuple(__isl_keep isl_stream
*s
)
4369 struct isl_token
*tok
, *tok2
;
4372 tok
= isl_stream_next_token(s
);
4375 if (tok
->type
!= '[' || next_is_tuple(s
)) {
4376 isl_stream_push_token(s
, tok
);
4380 tok2
= isl_stream_next_token(s
);
4381 is_tuple
= tok2
&& tok2
->type
!= '{';
4383 isl_stream_push_token(s
, tok2
);
4384 isl_stream_push_token(s
, tok
);
4389 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
4390 * i.e., everything except the parameter specification and
4391 * without shared domain constraints.
4392 * "v" contains a description of the identifiers parsed so far.
4393 * The parameters, if any, are specified by "space".
4395 * The body is of the form
4397 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4399 * Read the tuple, collecting the individual isl_union_pw_aff
4400 * elements in a list and construct the result from the tuple space and
4403 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body_core(
4404 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
4406 isl_union_pw_aff_list
*list
;
4407 isl_multi_union_pw_aff
*mupa
;
4409 list
= isl_union_pw_aff_list_alloc(s
->ctx
, 0);
4410 space
= read_tuple_space(s
, v
, space
, 1, 0,
4411 &read_union_pw_aff_el
, &list
);
4412 mupa
= isl_multi_union_pw_aff_from_union_pw_aff_list(space
, list
);
4417 /* Read the body of an isl_union_set from "s",
4418 * i.e., everything except the parameter specification.
4419 * "v" contains a description of the identifiers parsed so far.
4420 * The parameters, if any, are specified by "space".
4422 * First read a generic disjunction of object bodies and then try and extract
4423 * an isl_union_set from that.
4425 static __isl_give isl_union_set
*read_union_set_body(__isl_keep isl_stream
*s
,
4426 struct vars
*v
, __isl_take isl_space
*space
)
4428 struct isl_obj obj
= { isl_obj_set
, NULL
};
4431 map
= isl_set_universe(space
);
4432 if (isl_stream_eat(s
, '{') < 0)
4434 obj
= obj_read_disjuncts(s
, v
, map
);
4435 if (isl_stream_eat(s
, '}') < 0)
4439 return extract_union_set(s
->ctx
, obj
);
4441 obj
.type
->free(obj
.v
);
4446 /* Read the body of an isl_multi_union_pw_aff from "s",
4447 * i.e., everything except the parameter specification.
4448 * "v" contains a description of the identifiers parsed so far.
4449 * The parameters, if any, are specified by "space".
4451 * In particular, handle the special case with shared domain constraints.
4452 * These are specified as
4456 * and are especially useful for setting the explicit domain
4457 * of a zero-dimensional isl_multi_union_pw_aff.
4458 * The core isl_multi_union_pw_aff body ([...]) is read by
4459 * read_multi_union_pw_aff_body_core.
4461 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_body(
4462 __isl_keep isl_stream
*s
, struct vars
*v
, __isl_take isl_space
*space
)
4464 isl_multi_union_pw_aff
*mupa
;
4466 if (!isl_stream_next_token_is(s
, '('))
4467 return read_multi_union_pw_aff_body_core(s
, v
, space
);
4469 if (isl_stream_eat(s
, '(') < 0)
4471 mupa
= read_multi_union_pw_aff_body_core(s
, v
, isl_space_copy(space
));
4472 if (isl_stream_eat_if_available(s
, ':')) {
4475 dom
= read_union_set_body(s
, v
, space
);
4476 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4478 isl_space_free(space
);
4480 if (isl_stream_eat(s
, ')') < 0)
4481 return isl_multi_union_pw_aff_free(mupa
);
4485 isl_space_free(space
);
4489 /* Read an isl_multi_union_pw_aff from "s".
4491 * The input has the form
4493 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4497 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4499 * Additionally, a shared domain may be specified as
4505 * [..] -> ([..] : ...)
4507 * The first case is handled by the caller, the second case
4508 * is handled by read_multi_union_pw_aff_body.
4510 * We first check for the special case of an empty tuple "[]".
4511 * Then we check if there are any parameters.
4512 * Finally, read the tuple and construct the result.
4514 static __isl_give isl_multi_union_pw_aff
*read_multi_union_pw_aff_core(
4515 __isl_keep isl_stream
*s
)
4518 isl_set
*dom
= NULL
;
4520 isl_multi_union_pw_aff
*mupa
= NULL
;
4522 if (next_is_empty_tuple(s
)) {
4523 if (isl_stream_eat(s
, '['))
4525 if (isl_stream_eat(s
, ']'))
4527 space
= isl_space_set_alloc(s
->ctx
, 0, 0);
4528 return isl_multi_union_pw_aff_zero(space
);
4531 v
= vars_new(s
->ctx
);
4535 dom
= isl_set_universe(isl_space_params_alloc(s
->ctx
, 0));
4536 if (next_is_param_tuple(s
)) {
4537 dom
= read_map_tuple(s
, dom
, isl_dim_param
, v
, 1, 0);
4538 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
4541 space
= isl_set_get_space(dom
);
4543 mupa
= read_multi_union_pw_aff_body(s
, v
, space
);
4551 isl_multi_union_pw_aff_free(mupa
);
4555 /* Read an isl_multi_union_pw_aff from "s".
4557 * In particular, handle the special case with shared domain constraints.
4558 * These are specified as
4562 * and are especially useful for setting the explicit domain
4563 * of a zero-dimensional isl_multi_union_pw_aff.
4564 * The core isl_multi_union_pw_aff ([...]) is read by
4565 * read_multi_union_pw_aff_core.
4567 __isl_give isl_multi_union_pw_aff
*isl_stream_read_multi_union_pw_aff(
4568 __isl_keep isl_stream
*s
)
4570 isl_multi_union_pw_aff
*mupa
;
4572 if (!isl_stream_next_token_is(s
, '('))
4573 return read_multi_union_pw_aff_core(s
);
4575 if (isl_stream_eat(s
, '(') < 0)
4577 mupa
= read_multi_union_pw_aff_core(s
);
4578 if (isl_stream_eat_if_available(s
, ':')) {
4581 dom
= isl_stream_read_union_set(s
);
4582 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
4584 if (isl_stream_eat(s
, ')') < 0)
4585 return isl_multi_union_pw_aff_free(mupa
);
4589 /* Read an isl_multi_union_pw_aff from "str".
4591 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_read_from_str(
4592 isl_ctx
*ctx
, const char *str
)
4594 isl_multi_union_pw_aff
*mupa
;
4595 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4598 mupa
= isl_stream_read_multi_union_pw_aff(s
);
4603 __isl_give isl_union_pw_qpolynomial
*isl_stream_read_union_pw_qpolynomial(
4604 __isl_keep isl_stream
*s
)
4609 if (obj
.type
== isl_obj_pw_qpolynomial
) {
4610 obj
.type
= isl_obj_union_pw_qpolynomial
;
4611 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
4614 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_pw_qpolynomial
,
4619 obj
.type
->free(obj
.v
);
4623 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_read_from_str(
4624 isl_ctx
*ctx
, const char *str
)
4626 isl_union_pw_qpolynomial
*upwqp
;
4627 isl_stream
*s
= isl_stream_new_str(ctx
, str
);
4630 upwqp
= isl_stream_read_union_pw_qpolynomial(s
);