2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
21 #include <isl_stream_private.h>
23 #include "isl_polynomial_private.h"
24 #include <isl/union_map.h>
25 #include <isl_mat_private.h>
26 #include <isl_aff_private.h>
32 struct variable
*next
;
41 static struct vars
*vars_new(struct isl_ctx
*ctx
)
44 v
= isl_alloc_type(ctx
, struct vars
);
53 static void variable_free(struct variable
*var
)
56 struct variable
*next
= var
->next
;
63 static void vars_free(struct vars
*v
)
71 static void vars_drop(struct vars
*v
, int n
)
82 struct variable
*next
= var
->next
;
90 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
94 var
= isl_calloc_type(v
->ctx
, struct variable
);
97 var
->name
= strdup(name
);
98 var
->name
[len
] = '\0';
107 static int vars_pos(struct vars
*v
, const char *s
, int len
)
114 for (q
= v
->v
; q
; q
= q
->next
) {
115 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
122 v
->v
= variable_new(v
, s
, len
, v
->n
);
130 static int vars_add_anon(struct vars
*v
)
132 v
->v
= variable_new(v
, "", 0, v
->n
);
141 static __isl_give isl_map
*set_name(__isl_take isl_map
*map
,
142 enum isl_dim_type type
, unsigned pos
, char *name
)
151 prime
= strchr(name
, '\'');
154 map
= isl_map_set_dim_name(map
, type
, pos
, name
);
161 /* Obtain next token, with some preprocessing.
162 * In particular, evaluate expressions of the form x^y,
163 * with x and y values.
165 static struct isl_token
*next_token(struct isl_stream
*s
)
167 struct isl_token
*tok
, *tok2
;
169 tok
= isl_stream_next_token(s
);
170 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
)
172 if (!isl_stream_eat_if_available(s
, '^'))
174 tok2
= isl_stream_next_token(s
);
175 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
176 isl_stream_error(s
, tok2
, "expecting constant value");
180 isl_int_pow_ui(tok
->u
.v
, tok
->u
.v
, isl_int_get_ui(tok2
->u
.v
));
182 isl_token_free(tok2
);
186 isl_token_free(tok2
);
190 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
192 struct isl_token
*tok
;
195 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
196 isl_stream_error(s
, tok
, "expecting constant value");
200 isl_int_mul(*f
, *f
, tok
->u
.v
);
204 if (isl_stream_eat_if_available(s
, '*'))
205 return accept_cst_factor(s
, f
);
213 /* Given an affine expression aff, return an affine expression
214 * for aff % d, with d the next token on the stream, which is
215 * assumed to be a constant.
217 * We introduce an integer division q = [aff/d] and the result
218 * is set to aff - d q.
220 static __isl_give isl_pw_aff
*affine_mod(struct isl_stream
*s
,
221 struct vars
*v
, __isl_take isl_pw_aff
*aff
)
223 struct isl_token
*tok
;
227 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
228 isl_stream_error(s
, tok
, "expecting constant value");
232 q
= isl_pw_aff_copy(aff
);
233 q
= isl_pw_aff_scale_down(q
, tok
->u
.v
);
234 q
= isl_pw_aff_floor(q
);
235 q
= isl_pw_aff_scale(q
, tok
->u
.v
);
237 aff
= isl_pw_aff_sub(aff
, q
);
242 isl_pw_aff_free(aff
);
247 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
248 __isl_take isl_space
*dim
, struct vars
*v
);
249 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
250 __isl_take isl_space
*dim
, struct vars
*v
);
252 static __isl_give isl_pw_aff
*accept_minmax(struct isl_stream
*s
,
253 __isl_take isl_space
*dim
, struct vars
*v
)
255 struct isl_token
*tok
;
256 isl_pw_aff_list
*list
= NULL
;
259 tok
= isl_stream_next_token(s
);
262 min
= tok
->type
== ISL_TOKEN_MIN
;
265 if (isl_stream_eat(s
, '('))
268 list
= accept_affine_list(s
, isl_space_copy(dim
), v
);
272 if (isl_stream_eat(s
, ')'))
276 return min
? isl_pw_aff_list_min(list
) : isl_pw_aff_list_max(list
);
279 isl_pw_aff_list_free(list
);
283 static __isl_give isl_pw_aff
*accept_div(struct isl_stream
*s
,
284 __isl_take isl_space
*dim
, struct vars
*v
)
286 struct isl_token
*tok
;
290 isl_pw_aff
*pwaff
= NULL
;
292 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FLOORD
))
294 else if (isl_stream_eat_if_available(s
, ISL_TOKEN_CEILD
))
297 if (isl_stream_eat(s
, '('))
300 if (isl_stream_eat(s
, '['))
302 if (isl_stream_eat_if_available(s
, '('))
306 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
309 if (isl_stream_eat(s
, ','))
312 if (seen_paren
&& isl_stream_eat(s
, ')'))
314 if (isl_stream_eat(s
, '/'))
321 if (tok
->type
!= ISL_TOKEN_VALUE
) {
322 isl_stream_error(s
, tok
, "expected denominator");
323 isl_stream_push_token(s
, tok
);
326 isl_pw_aff_scale_down(pwaff
, tok
->u
.v
);
330 pwaff
= isl_pw_aff_ceil(pwaff
);
332 pwaff
= isl_pw_aff_floor(pwaff
);
335 if (isl_stream_eat(s
, ')'))
338 if (isl_stream_eat(s
, ']'))
346 isl_pw_aff_free(pwaff
);
350 static __isl_give isl_pw_aff
*accept_affine_factor(struct isl_stream
*s
,
351 __isl_take isl_space
*dim
, struct vars
*v
)
353 struct isl_token
*tok
= NULL
;
354 isl_pw_aff
*res
= NULL
;
358 isl_stream_error(s
, NULL
, "unexpected EOF");
362 if (tok
->type
== ISL_TOKEN_AFF
) {
363 res
= isl_pw_aff_copy(tok
->u
.pwaff
);
365 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
367 int pos
= vars_pos(v
, tok
->u
.s
, -1);
373 isl_stream_error(s
, tok
, "unknown identifier");
377 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim
)));
380 isl_int_set_si(aff
->v
->el
[2 + pos
], 1);
381 res
= isl_pw_aff_from_aff(aff
);
383 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
384 if (isl_stream_eat_if_available(s
, '*')) {
385 res
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
386 res
= isl_pw_aff_scale(res
, tok
->u
.v
);
390 ls
= isl_local_space_from_space(isl_space_copy(dim
));
391 aff
= isl_aff_zero_on_domain(ls
);
392 aff
= isl_aff_add_constant(aff
, tok
->u
.v
);
393 res
= isl_pw_aff_from_aff(aff
);
396 } else if (tok
->type
== '(') {
399 res
= accept_affine(s
, isl_space_copy(dim
), v
);
402 if (isl_stream_eat(s
, ')'))
404 } else if (tok
->type
== '[' ||
405 tok
->type
== ISL_TOKEN_FLOORD
||
406 tok
->type
== ISL_TOKEN_CEILD
) {
407 isl_stream_push_token(s
, tok
);
409 res
= accept_div(s
, isl_space_copy(dim
), v
);
410 } else if (tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
) {
411 isl_stream_push_token(s
, tok
);
413 res
= accept_minmax(s
, isl_space_copy(dim
), v
);
415 isl_stream_error(s
, tok
, "expecting factor");
418 if (isl_stream_eat_if_available(s
, '%') ||
419 isl_stream_eat_if_available(s
, ISL_TOKEN_MOD
)) {
421 return affine_mod(s
, v
, res
);
423 if (isl_stream_eat_if_available(s
, '*')) {
426 isl_int_set_si(f
, 1);
427 if (accept_cst_factor(s
, &f
) < 0) {
431 res
= isl_pw_aff_scale(res
, f
);
440 isl_pw_aff_free(res
);
445 static __isl_give isl_pw_aff
*add_cst(__isl_take isl_pw_aff
*pwaff
, isl_int v
)
450 space
= isl_pw_aff_get_domain_space(pwaff
);
451 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
452 aff
= isl_aff_add_constant(aff
, v
);
454 return isl_pw_aff_add(pwaff
, isl_pw_aff_from_aff(aff
));
457 static __isl_give isl_pw_aff
*accept_affine(struct isl_stream
*s
,
458 __isl_take isl_space
*dim
, struct vars
*v
)
460 struct isl_token
*tok
= NULL
;
465 ls
= isl_local_space_from_space(isl_space_copy(dim
));
466 res
= isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
473 isl_stream_error(s
, NULL
, "unexpected EOF");
476 if (tok
->type
== '-') {
481 if (tok
->type
== '(' || tok
->type
== '[' ||
482 tok
->type
== ISL_TOKEN_MIN
|| tok
->type
== ISL_TOKEN_MAX
||
483 tok
->type
== ISL_TOKEN_FLOORD
||
484 tok
->type
== ISL_TOKEN_CEILD
||
485 tok
->type
== ISL_TOKEN_IDENT
||
486 tok
->type
== ISL_TOKEN_AFF
) {
488 isl_stream_push_token(s
, tok
);
490 term
= accept_affine_factor(s
, isl_space_copy(dim
), v
);
492 res
= isl_pw_aff_sub(res
, term
);
494 res
= isl_pw_aff_add(res
, term
);
498 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
500 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
501 if (isl_stream_eat_if_available(s
, '*') ||
502 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
504 term
= accept_affine_factor(s
,
505 isl_space_copy(dim
), v
);
506 term
= isl_pw_aff_scale(term
, tok
->u
.v
);
507 res
= isl_pw_aff_add(res
, term
);
511 res
= add_cst(res
, tok
->u
.v
);
515 isl_stream_error(s
, tok
, "unexpected isl_token");
516 isl_stream_push_token(s
, tok
);
517 isl_pw_aff_free(res
);
524 if (tok
&& tok
->type
== '-') {
527 } else if (tok
&& tok
->type
== '+') {
530 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
531 isl_int_is_neg(tok
->u
.v
)) {
532 isl_stream_push_token(s
, tok
);
535 isl_stream_push_token(s
, tok
);
545 isl_pw_aff_free(res
);
549 static int is_comparator(struct isl_token
*tok
)
567 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
568 struct vars
*v
, __isl_take isl_map
*map
);
569 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
570 __isl_take isl_space
*dim
, struct vars
*v
);
572 /* Accept a ternary operator, given the first argument.
574 static __isl_give isl_pw_aff
*accept_ternary(struct isl_stream
*s
,
575 __isl_take isl_map
*cond
, struct vars
*v
)
578 isl_pw_aff
*pwaff1
= NULL
, *pwaff2
= NULL
;
583 if (isl_stream_eat(s
, '?'))
586 dim
= isl_space_wrap(isl_map_get_space(cond
));
587 pwaff1
= accept_extended_affine(s
, dim
, v
);
591 if (isl_stream_eat(s
, ':'))
594 dim
= isl_pw_aff_get_domain_space(pwaff1
);
595 pwaff2
= accept_extended_affine(s
, dim
, v
);
599 return isl_pw_aff_cond(isl_map_wrap(cond
), pwaff1
, pwaff2
);
602 isl_pw_aff_free(pwaff1
);
603 isl_pw_aff_free(pwaff2
);
607 /* Accept an affine expression that may involve ternary operators.
608 * We first read an affine expression.
609 * If it is not followed by a comparison operator, we simply return it.
610 * Otherwise, we assume the affine epxression is part of the first
611 * argument of a ternary operator and try to parse that.
613 static __isl_give isl_pw_aff
*accept_extended_affine(struct isl_stream
*s
,
614 __isl_take isl_space
*dim
, struct vars
*v
)
619 struct isl_token
*tok
;
620 int line
= -1, col
= -1;
623 tok
= isl_stream_next_token(s
);
627 isl_stream_push_token(s
, tok
);
630 pwaff
= accept_affine(s
, dim
, v
);
634 tok
= isl_stream_next_token(s
);
636 return isl_pw_aff_free(pwaff
);
638 is_comp
= is_comparator(tok
);
639 isl_stream_push_token(s
, tok
);
643 tok
= isl_token_new(s
->ctx
, line
, col
, 0);
645 return isl_pw_aff_free(pwaff
);
646 tok
->type
= ISL_TOKEN_AFF
;
647 tok
->u
.pwaff
= pwaff
;
649 space
= isl_pw_aff_get_domain_space(pwaff
);
650 cond
= isl_map_universe(isl_space_unwrap(space
));
652 isl_stream_push_token(s
, tok
);
654 cond
= read_disjuncts(s
, v
, cond
);
656 return accept_ternary(s
, cond
, v
);
659 static __isl_give isl_map
*read_var_def(struct isl_stream
*s
,
660 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
)
666 if (type
== isl_dim_param
)
667 pos
= isl_map_dim(map
, isl_dim_param
);
669 pos
= isl_map_dim(map
, isl_dim_in
);
670 if (type
== isl_dim_out
)
671 pos
+= isl_map_dim(map
, isl_dim_out
);
676 def
= accept_extended_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
677 def_map
= isl_map_from_pw_aff(def
);
678 def_map
= isl_map_equate(def_map
, type
, pos
, isl_dim_out
, 0);
679 def_map
= isl_set_unwrap(isl_map_domain(def_map
));
681 map
= isl_map_intersect(map
, def_map
);
686 static __isl_give isl_map
*read_var_list(struct isl_stream
*s
,
687 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
)
690 struct isl_token
*tok
;
692 if (isl_stream_next_token_is(s
, ']'))
693 return isl_map_add_dims(map
, type
, 0);
695 while ((tok
= next_token(s
)) != NULL
) {
698 if (tok
->type
== ISL_TOKEN_IDENT
) {
700 int p
= vars_pos(v
, tok
->u
.s
, -1);
707 map
= isl_map_add_dims(map
, type
, 1);
708 map
= set_name(map
, type
, i
, v
->v
->name
);
710 if (isl_stream_eat_if_available(s
, '='))
711 map
= read_var_def(s
, map
, type
, v
);
713 if (type
== isl_dim_param
) {
714 isl_stream_error(s
, tok
,
715 "expecting unique identifier");
718 isl_stream_push_token(s
, tok
);
720 if (vars_add_anon(v
) < 0)
722 map
= isl_map_add_dims(map
, type
, 1);
723 map
= read_var_def(s
, map
, type
, v
);
726 tok
= isl_stream_next_token(s
);
727 if (tok
&& tok
->type
== ']' &&
728 isl_stream_next_token_is(s
, '[')) {
730 tok
= isl_stream_next_token(s
);
731 } else if (!tok
|| tok
->type
!= ',')
738 isl_stream_push_token(s
, tok
);
747 static __isl_give isl_pw_aff_list
*accept_affine_list(struct isl_stream
*s
,
748 __isl_take isl_space
*dim
, struct vars
*v
)
751 isl_pw_aff_list
*list
;
752 struct isl_token
*tok
= NULL
;
754 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
755 list
= isl_pw_aff_list_from_pw_aff(pwaff
);
760 tok
= isl_stream_next_token(s
);
762 isl_stream_error(s
, NULL
, "unexpected EOF");
765 if (tok
->type
!= ',') {
766 isl_stream_push_token(s
, tok
);
771 pwaff
= accept_affine(s
, isl_space_copy(dim
), v
);
772 list
= isl_pw_aff_list_concat(list
,
773 isl_pw_aff_list_from_pw_aff(pwaff
));
782 isl_pw_aff_list_free(list
);
786 static __isl_give isl_map
*read_defined_var_list(struct isl_stream
*s
,
787 struct vars
*v
, __isl_take isl_map
*map
)
789 struct isl_token
*tok
;
791 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
795 if (tok
->type
!= ISL_TOKEN_IDENT
)
798 p
= vars_pos(v
, tok
->u
.s
, -1);
802 isl_stream_error(s
, tok
, "expecting unique identifier");
806 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
809 tok
= isl_stream_next_token(s
);
810 if (tok
&& tok
->type
== '=') {
812 map
= read_var_def(s
, map
, isl_dim_out
, v
);
813 tok
= isl_stream_next_token(s
);
816 if (!tok
|| tok
->type
!= ',')
822 isl_stream_push_token(s
, tok
);
831 static int next_is_tuple(struct isl_stream
*s
)
833 struct isl_token
*tok
;
836 tok
= isl_stream_next_token(s
);
839 if (tok
->type
== '[') {
840 isl_stream_push_token(s
, tok
);
843 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
844 isl_stream_push_token(s
, tok
);
848 is_tuple
= isl_stream_next_token_is(s
, '[');
850 isl_stream_push_token(s
, tok
);
855 static __isl_give isl_map
*read_tuple(struct isl_stream
*s
,
856 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
);
858 static __isl_give isl_set
*read_nested_tuple(struct isl_stream
*s
,
859 __isl_take isl_map
*map
, struct vars
*v
)
861 map
= read_tuple(s
, map
, isl_dim_in
, v
);
862 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
864 map
= read_tuple(s
, map
, isl_dim_out
, v
);
865 return isl_map_wrap(map
);
871 static __isl_give isl_map
*read_tuple(struct isl_stream
*s
,
872 __isl_take isl_map
*map
, enum isl_dim_type type
, struct vars
*v
)
874 struct isl_token
*tok
;
877 tok
= isl_stream_next_token(s
);
878 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
879 name
= strdup(tok
->u
.s
);
883 tok
= isl_stream_next_token(s
);
885 if (!tok
|| tok
->type
!= '[') {
886 isl_stream_error(s
, tok
, "expecting '['");
890 if (type
!= isl_dim_param
&& next_is_tuple(s
)) {
891 isl_space
*dim
= isl_map_get_space(map
);
892 int nparam
= isl_space_dim(dim
, isl_dim_param
);
893 int n_in
= isl_space_dim(dim
, isl_dim_in
);
895 if (type
== isl_dim_out
) {
896 dim
= isl_space_move_dims(dim
, isl_dim_param
, nparam
,
897 isl_dim_in
, 0, n_in
);
898 dim
= isl_space_params(dim
);
900 nested
= read_nested_tuple(s
, isl_map_universe(dim
), v
);
901 if (type
== isl_dim_in
) {
902 nested
= isl_map_reverse(nested
);
903 map
= isl_map_intersect(nested
, map
);
906 dim
= isl_set_get_space(nested
);
907 dim
= isl_space_drop_dims(dim
, isl_dim_param
, nparam
, n_in
);
908 dim
= isl_space_join(isl_map_get_space(map
), dim
);
909 set
= isl_map_domain(map
);
910 nested
= isl_map_reset_space(nested
, dim
);
911 map
= isl_map_intersect_domain(nested
, set
);
914 map
= read_var_list(s
, map
, type
, v
);
915 tok
= isl_stream_next_token(s
);
916 if (!tok
|| tok
->type
!= ']') {
917 isl_stream_error(s
, tok
, "expecting ']'");
923 map
= isl_map_set_tuple_name(map
, type
, name
);
935 static __isl_give isl_set
*construct_constraints(
936 __isl_take isl_set
*set
, enum isl_token_type type
,
937 __isl_keep isl_pw_aff_list
*left
, __isl_keep isl_pw_aff_list
*right
)
941 if (type
== ISL_TOKEN_LE
)
942 cond
= isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left
),
943 isl_pw_aff_list_copy(right
));
944 else if (type
== ISL_TOKEN_GE
)
945 cond
= isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left
),
946 isl_pw_aff_list_copy(right
));
947 else if (type
== ISL_TOKEN_LT
)
948 cond
= isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left
),
949 isl_pw_aff_list_copy(right
));
950 else if (type
== ISL_TOKEN_GT
)
951 cond
= isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left
),
952 isl_pw_aff_list_copy(right
));
953 else if (type
== ISL_TOKEN_NE
)
954 cond
= isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left
),
955 isl_pw_aff_list_copy(right
));
957 cond
= isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left
),
958 isl_pw_aff_list_copy(right
));
960 return isl_set_intersect(set
, cond
);
963 static __isl_give isl_map
*add_constraint(struct isl_stream
*s
,
964 struct vars
*v
, __isl_take isl_map
*map
)
966 struct isl_token
*tok
= NULL
;
967 isl_pw_aff_list
*list1
= NULL
, *list2
= NULL
;
970 set
= isl_map_wrap(map
);
971 list1
= accept_affine_list(s
, isl_set_get_space(set
), v
);
974 tok
= isl_stream_next_token(s
);
975 if (!is_comparator(tok
)) {
976 isl_stream_error(s
, tok
, "missing operator");
978 isl_stream_push_token(s
, tok
);
983 list2
= accept_affine_list(s
, isl_set_get_space(set
), v
);
987 set
= construct_constraints(set
, tok
->type
, list1
, list2
);
989 isl_pw_aff_list_free(list1
);
992 tok
= isl_stream_next_token(s
);
993 if (!is_comparator(tok
)) {
995 isl_stream_push_token(s
, tok
);
999 isl_pw_aff_list_free(list1
);
1001 return isl_set_unwrap(set
);
1004 isl_token_free(tok
);
1005 isl_pw_aff_list_free(list1
);
1006 isl_pw_aff_list_free(list2
);
1011 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
1012 struct vars
*v
, __isl_take isl_map
*map
)
1015 int seen_paren
= isl_stream_eat_if_available(s
, '(');
1017 map
= isl_map_from_domain(isl_map_wrap(map
));
1018 map
= read_defined_var_list(s
, v
, map
);
1020 if (isl_stream_eat(s
, ':'))
1023 map
= read_disjuncts(s
, v
, map
);
1024 map
= isl_set_unwrap(isl_map_domain(map
));
1026 vars_drop(v
, v
->n
- n
);
1027 if (seen_paren
&& isl_stream_eat(s
, ')'))
1036 /* Parse an expression between parentheses and push the result
1037 * back on the stream.
1039 * The parsed expression may be either an affine expression
1040 * or a condition. The first type is pushed onto the stream
1041 * as an isl_pw_aff, while the second is pushed as an isl_map.
1043 * If the initial token indicates the start of a condition,
1044 * we parse it as such.
1045 * Otherwise, we first parse an affine expression and push
1046 * that onto the stream. If the affine expression covers the
1047 * entire expression between parentheses, we return.
1048 * Otherwise, we assume that the affine expression is the
1049 * start of a condition and continue parsing.
1051 static int resolve_paren_expr(struct isl_stream
*s
,
1052 struct vars
*v
, __isl_take isl_map
*map
)
1054 struct isl_token
*tok
, *tok2
;
1058 tok
= isl_stream_next_token(s
);
1059 if (!tok
|| tok
->type
!= '(')
1062 if (isl_stream_next_token_is(s
, ISL_TOKEN_EXISTS
) ||
1063 isl_stream_next_token_is(s
, ISL_TOKEN_TRUE
) ||
1064 isl_stream_next_token_is(s
, ISL_TOKEN_FALSE
)) {
1065 map
= read_disjuncts(s
, v
, map
);
1066 if (isl_stream_eat(s
, ')'))
1068 tok
->type
= ISL_TOKEN_MAP
;
1070 isl_stream_push_token(s
, tok
);
1074 tok2
= isl_stream_next_token(s
);
1079 isl_stream_push_token(s
, tok2
);
1081 pwaff
= accept_affine(s
, isl_space_wrap(isl_map_get_space(map
)), v
);
1085 tok2
= isl_token_new(s
->ctx
, line
, col
, 0);
1088 tok2
->type
= ISL_TOKEN_AFF
;
1089 tok2
->u
.pwaff
= pwaff
;
1091 if (isl_stream_eat_if_available(s
, ')')) {
1092 isl_stream_push_token(s
, tok2
);
1093 isl_token_free(tok
);
1098 isl_stream_push_token(s
, tok2
);
1100 map
= read_disjuncts(s
, v
, map
);
1101 if (isl_stream_eat(s
, ')'))
1104 tok
->type
= ISL_TOKEN_MAP
;
1106 isl_stream_push_token(s
, tok
);
1110 isl_pw_aff_free(pwaff
);
1112 isl_token_free(tok
);
1117 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
1118 struct vars
*v
, __isl_take isl_map
*map
)
1120 if (isl_stream_next_token_is(s
, '('))
1121 if (resolve_paren_expr(s
, v
, isl_map_copy(map
)))
1124 if (isl_stream_next_token_is(s
, ISL_TOKEN_MAP
)) {
1125 struct isl_token
*tok
;
1126 tok
= isl_stream_next_token(s
);
1130 map
= isl_map_copy(tok
->u
.map
);
1131 isl_token_free(tok
);
1135 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
1136 return read_exists(s
, v
, map
);
1138 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
1141 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
1142 isl_space
*dim
= isl_map_get_space(map
);
1144 return isl_map_empty(dim
);
1147 return add_constraint(s
, v
, map
);
1153 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1154 struct vars
*v
, __isl_take isl_map
*map
)
1159 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1160 res
= read_conjunct(s
, v
, isl_map_copy(map
));
1162 res
= isl_map_subtract(isl_map_copy(map
), res
);
1164 while (isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1167 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1168 res_i
= read_conjunct(s
, v
, isl_map_copy(map
));
1170 res
= isl_map_subtract(res
, res_i
);
1172 res
= isl_map_intersect(res
, res_i
);
1179 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1180 struct vars
*v
, __isl_take isl_map
*map
)
1184 if (isl_stream_next_token_is(s
, '}')) {
1185 isl_space
*dim
= isl_map_get_space(map
);
1187 return isl_map_universe(dim
);
1190 res
= read_conjuncts(s
, v
, isl_map_copy(map
));
1191 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1194 res_i
= read_conjuncts(s
, v
, isl_map_copy(map
));
1195 res
= isl_map_union(res
, res_i
);
1202 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1204 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1205 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1206 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1207 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1209 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1210 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1211 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1213 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1214 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1215 isl_basic_map_dim(bmap
, isl_dim_in
) +
1216 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1217 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1219 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1225 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1226 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1229 struct isl_token
*tok
;
1239 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1240 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1242 tok
= isl_stream_next_token(s
);
1243 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1244 isl_stream_error(s
, tok
, "expecting coefficient");
1246 isl_stream_push_token(s
, tok
);
1249 if (!tok
->on_new_line
) {
1250 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1251 isl_stream_push_token(s
, tok
);
1255 type
= isl_int_get_si(tok
->u
.v
);
1256 isl_token_free(tok
);
1258 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1260 k
= isl_basic_map_alloc_equality(bmap
);
1263 k
= isl_basic_map_alloc_inequality(bmap
);
1269 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1271 tok
= isl_stream_next_token(s
);
1272 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1273 isl_stream_error(s
, tok
, "expecting coefficient");
1275 isl_stream_push_token(s
, tok
);
1278 if (tok
->on_new_line
) {
1279 isl_stream_error(s
, tok
,
1280 "coefficient should not appear on new line");
1281 isl_stream_push_token(s
, tok
);
1284 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1285 isl_int_set(c
[pos
], tok
->u
.v
);
1286 isl_token_free(tok
);
1291 isl_basic_map_free(bmap
);
1295 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
,
1299 struct isl_token
*tok
;
1300 struct isl_token
*tok2
;
1303 unsigned in
= 0, out
, local
= 0;
1304 struct isl_basic_map
*bmap
= NULL
;
1309 tok
= isl_stream_next_token(s
);
1311 isl_stream_error(s
, NULL
, "unexpected EOF");
1314 tok2
= isl_stream_next_token(s
);
1316 isl_token_free(tok
);
1317 isl_stream_error(s
, NULL
, "unexpected EOF");
1320 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1321 isl_stream_push_token(s
, tok2
);
1322 isl_stream_push_token(s
, tok
);
1323 isl_stream_error(s
, NULL
,
1324 "expecting constraint matrix dimensions");
1327 n_row
= isl_int_get_si(tok
->u
.v
);
1328 n_col
= isl_int_get_si(tok2
->u
.v
);
1329 on_new_line
= tok2
->on_new_line
;
1330 isl_token_free(tok2
);
1331 isl_token_free(tok
);
1332 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1333 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1334 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1335 tok
= isl_stream_next_token_on_same_line(s
);
1337 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1338 isl_stream_error(s
, tok
,
1339 "expecting number of output dimensions");
1340 isl_stream_push_token(s
, tok
);
1343 out
= isl_int_get_si(tok
->u
.v
);
1344 isl_token_free(tok
);
1346 tok
= isl_stream_next_token_on_same_line(s
);
1347 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1348 isl_stream_error(s
, tok
,
1349 "expecting number of input dimensions");
1351 isl_stream_push_token(s
, tok
);
1354 in
= isl_int_get_si(tok
->u
.v
);
1355 isl_token_free(tok
);
1357 tok
= isl_stream_next_token_on_same_line(s
);
1358 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1359 isl_stream_error(s
, tok
,
1360 "expecting number of existentials");
1362 isl_stream_push_token(s
, tok
);
1365 local
= isl_int_get_si(tok
->u
.v
);
1366 isl_token_free(tok
);
1368 tok
= isl_stream_next_token_on_same_line(s
);
1369 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1370 isl_stream_error(s
, tok
,
1371 "expecting number of parameters");
1373 isl_stream_push_token(s
, tok
);
1376 nparam
= isl_int_get_si(tok
->u
.v
);
1377 isl_token_free(tok
);
1378 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1379 isl_stream_error(s
, NULL
,
1380 "dimensions don't match");
1384 out
= n_col
- 2 - nparam
;
1385 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1389 for (i
= 0; i
< local
; ++i
) {
1390 int k
= isl_basic_map_alloc_div(bmap
);
1393 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1396 for (i
= 0; i
< n_row
; ++i
)
1397 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1399 tok
= isl_stream_next_token_on_same_line(s
);
1401 isl_stream_error(s
, tok
, "unexpected extra token on line");
1402 isl_stream_push_token(s
, tok
);
1406 bmap
= isl_basic_map_simplify(bmap
);
1407 bmap
= isl_basic_map_finalize(bmap
);
1410 isl_basic_map_free(bmap
);
1414 static struct isl_map
*map_read_polylib(struct isl_stream
*s
, int nparam
)
1416 struct isl_token
*tok
;
1417 struct isl_token
*tok2
;
1419 struct isl_map
*map
;
1421 tok
= isl_stream_next_token(s
);
1423 isl_stream_error(s
, NULL
, "unexpected EOF");
1426 tok2
= isl_stream_next_token_on_same_line(s
);
1427 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1428 isl_stream_push_token(s
, tok2
);
1429 isl_stream_push_token(s
, tok
);
1430 return isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
));
1433 isl_stream_error(s
, tok2
, "unexpected token");
1434 isl_stream_push_token(s
, tok2
);
1435 isl_stream_push_token(s
, tok
);
1438 n
= isl_int_get_si(tok
->u
.v
);
1439 isl_token_free(tok
);
1441 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1443 map
= isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
));
1445 for (i
= 1; map
&& i
< n
; ++i
)
1446 map
= isl_map_union(map
,
1447 isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
)));
1452 static int optional_power(struct isl_stream
*s
)
1455 struct isl_token
*tok
;
1457 tok
= isl_stream_next_token(s
);
1460 if (tok
->type
!= '^') {
1461 isl_stream_push_token(s
, tok
);
1464 isl_token_free(tok
);
1465 tok
= isl_stream_next_token(s
);
1466 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1467 isl_stream_error(s
, tok
, "expecting exponent");
1469 isl_stream_push_token(s
, tok
);
1472 pow
= isl_int_get_si(tok
->u
.v
);
1473 isl_token_free(tok
);
1477 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1478 __isl_keep isl_map
*map
, struct vars
*v
);
1480 static __isl_give isl_pw_qpolynomial
*read_factor(struct isl_stream
*s
,
1481 __isl_keep isl_map
*map
, struct vars
*v
)
1483 isl_pw_qpolynomial
*pwqp
;
1484 struct isl_token
*tok
;
1486 tok
= next_token(s
);
1488 isl_stream_error(s
, NULL
, "unexpected EOF");
1491 if (tok
->type
== '(') {
1494 isl_token_free(tok
);
1495 pwqp
= read_term(s
, map
, v
);
1498 if (isl_stream_eat(s
, ')'))
1500 pow
= optional_power(s
);
1501 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1502 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1503 struct isl_token
*tok2
;
1504 tok2
= isl_stream_next_token(s
);
1505 isl_qpolynomial
*qp
;
1506 if (tok2
&& tok2
->type
== '/') {
1507 isl_token_free(tok2
);
1508 tok2
= next_token(s
);
1509 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1510 isl_stream_error(s
, tok2
, "expected denominator");
1511 isl_token_free(tok
);
1512 isl_token_free(tok2
);
1515 qp
= isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map
),
1516 tok
->u
.v
, tok2
->u
.v
);
1517 isl_token_free(tok2
);
1519 isl_stream_push_token(s
, tok2
);
1520 qp
= isl_qpolynomial_cst_on_domain(isl_map_get_space(map
),
1523 isl_token_free(tok
);
1524 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1525 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1526 isl_qpolynomial
*qp
;
1527 isl_token_free(tok
);
1528 qp
= isl_qpolynomial_infty_on_domain(isl_map_get_space(map
));
1529 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1530 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1531 isl_qpolynomial
*qp
;
1532 isl_token_free(tok
);
1533 qp
= isl_qpolynomial_nan_on_domain(isl_map_get_space(map
));
1534 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1535 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1537 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1539 isl_qpolynomial
*qp
;
1541 isl_token_free(tok
);
1545 vars_drop(v
, v
->n
- n
);
1546 isl_stream_error(s
, tok
, "unknown identifier");
1547 isl_token_free(tok
);
1550 isl_token_free(tok
);
1551 pow
= optional_power(s
);
1552 qp
= isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map
), pos
, pow
);
1553 pwqp
= isl_pw_qpolynomial_from_qpolynomial(qp
);
1554 } else if (tok
->type
== '[') {
1558 isl_stream_push_token(s
, tok
);
1559 pwaff
= accept_affine(s
, isl_map_get_space(map
), v
);
1560 pow
= optional_power(s
);
1561 pwqp
= isl_pw_qpolynomial_from_pw_aff(pwaff
);
1562 pwqp
= isl_pw_qpolynomial_pow(pwqp
, pow
);
1563 } else if (tok
->type
== '-') {
1564 isl_token_free(tok
);
1565 pwqp
= read_factor(s
, map
, v
);
1566 pwqp
= isl_pw_qpolynomial_neg(pwqp
);
1568 isl_stream_error(s
, tok
, "unexpected isl_token");
1569 isl_stream_push_token(s
, tok
);
1573 if (isl_stream_eat_if_available(s
, '*') ||
1574 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1575 isl_pw_qpolynomial
*pwqp2
;
1577 pwqp2
= read_factor(s
, map
, v
);
1578 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp2
);
1583 isl_pw_qpolynomial_free(pwqp
);
1587 static __isl_give isl_pw_qpolynomial
*read_term(struct isl_stream
*s
,
1588 __isl_keep isl_map
*map
, struct vars
*v
)
1590 struct isl_token
*tok
;
1591 isl_pw_qpolynomial
*pwqp
;
1593 pwqp
= read_factor(s
, map
, v
);
1596 tok
= next_token(s
);
1600 if (tok
->type
== '+') {
1601 isl_pw_qpolynomial
*pwqp2
;
1603 isl_token_free(tok
);
1604 pwqp2
= read_factor(s
, map
, v
);
1605 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1606 } else if (tok
->type
== '-') {
1607 isl_pw_qpolynomial
*pwqp2
;
1609 isl_token_free(tok
);
1610 pwqp2
= read_factor(s
, map
, v
);
1611 pwqp
= isl_pw_qpolynomial_sub(pwqp
, pwqp2
);
1612 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1613 isl_int_is_neg(tok
->u
.v
)) {
1614 isl_pw_qpolynomial
*pwqp2
;
1616 isl_stream_push_token(s
, tok
);
1617 pwqp2
= read_factor(s
, map
, v
);
1618 pwqp
= isl_pw_qpolynomial_add(pwqp
, pwqp2
);
1620 isl_stream_push_token(s
, tok
);
1628 static __isl_give isl_map
*read_optional_disjuncts(struct isl_stream
*s
,
1629 __isl_take isl_map
*map
, struct vars
*v
)
1631 struct isl_token
*tok
;
1633 tok
= isl_stream_next_token(s
);
1635 isl_stream_error(s
, NULL
, "unexpected EOF");
1638 if (tok
->type
== ':' ||
1639 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
1640 isl_token_free(tok
);
1641 map
= read_disjuncts(s
, v
, map
);
1643 isl_stream_push_token(s
, tok
);
1651 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1652 __isl_take isl_map
*map
, struct vars
*v
, int n
)
1654 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1655 isl_pw_qpolynomial
*pwqp
;
1656 struct isl_set
*set
;
1658 pwqp
= read_term(s
, map
, v
);
1659 map
= read_optional_disjuncts(s
, map
, v
);
1660 set
= isl_map_range(map
);
1662 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, set
);
1664 vars_drop(v
, v
->n
- n
);
1670 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
1671 __isl_take isl_set
*set
, struct vars
*v
, int n
)
1673 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
1674 isl_pw_qpolynomial
*pwqp
;
1675 isl_pw_qpolynomial_fold
*pwf
= NULL
;
1677 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
1678 return obj_read_poly(s
, set
, v
, n
);
1680 if (isl_stream_eat(s
, '('))
1683 pwqp
= read_term(s
, set
, v
);
1684 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
, pwqp
);
1686 while (isl_stream_eat_if_available(s
, ',')) {
1687 isl_pw_qpolynomial_fold
*pwf_i
;
1688 pwqp
= read_term(s
, set
, v
);
1689 pwf_i
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
1691 pwf
= isl_pw_qpolynomial_fold_fold(pwf
, pwf_i
);
1694 if (isl_stream_eat(s
, ')'))
1697 set
= read_optional_disjuncts(s
, set
, v
);
1698 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, set
);
1700 vars_drop(v
, v
->n
- n
);
1706 isl_pw_qpolynomial_fold_free(pwf
);
1707 obj
.type
= isl_obj_none
;
1711 static int is_rational(struct isl_stream
*s
)
1713 struct isl_token
*tok
;
1715 tok
= isl_stream_next_token(s
);
1718 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
1719 isl_token_free(tok
);
1720 isl_stream_eat(s
, ':');
1724 isl_stream_push_token(s
, tok
);
1729 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
1730 __isl_take isl_map
*map
, struct vars
*v
)
1732 struct isl_token
*tok
;
1733 struct isl_obj obj
= { isl_obj_set
, NULL
};
1737 map
= isl_map_set_rational(map
);
1739 if (isl_stream_next_token_is(s
, ':')) {
1740 obj
.type
= isl_obj_set
;
1741 obj
.v
= read_optional_disjuncts(s
, map
, v
);
1745 if (!next_is_tuple(s
))
1746 return obj_read_poly_or_fold(s
, map
, v
, n
);
1748 map
= read_tuple(s
, map
, isl_dim_in
, v
);
1751 tok
= isl_stream_next_token(s
);
1752 if (tok
&& tok
->type
== ISL_TOKEN_TO
) {
1753 obj
.type
= isl_obj_map
;
1754 isl_token_free(tok
);
1755 if (!next_is_tuple(s
)) {
1756 isl_set
*set
= isl_map_domain(map
);
1757 return obj_read_poly_or_fold(s
, set
, v
, n
);
1759 map
= read_tuple(s
, map
, isl_dim_out
, v
);
1763 map
= isl_map_reverse(map
);
1765 isl_stream_push_token(s
, tok
);
1768 map
= read_optional_disjuncts(s
, map
, v
);
1770 vars_drop(v
, v
->n
- n
);
1776 obj
.type
= isl_obj_none
;
1780 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
1782 if (obj
.type
== isl_obj_map
) {
1783 obj
.v
= isl_union_map_from_map(obj
.v
);
1784 obj
.type
= isl_obj_union_map
;
1785 } else if (obj
.type
== isl_obj_set
) {
1786 obj
.v
= isl_union_set_from_set(obj
.v
);
1787 obj
.type
= isl_obj_union_set
;
1788 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
1789 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1790 obj
.type
= isl_obj_union_pw_qpolynomial
;
1791 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
1792 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1793 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1795 isl_assert(ctx
, 0, goto error
);
1798 obj
.type
->free(obj
.v
);
1799 obj
.type
= isl_obj_none
;
1803 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
1804 struct isl_obj obj1
, struct isl_obj obj2
)
1806 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
1807 obj1
= to_union(ctx
, obj1
);
1808 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
1809 obj2
= to_union(ctx
, obj2
);
1810 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
1811 obj1
= to_union(ctx
, obj1
);
1812 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
1813 obj2
= to_union(ctx
, obj2
);
1814 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
1815 obj2
.type
== isl_obj_union_pw_qpolynomial
)
1816 obj1
= to_union(ctx
, obj1
);
1817 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
1818 obj2
.type
== isl_obj_pw_qpolynomial
)
1819 obj2
= to_union(ctx
, obj2
);
1820 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
1821 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
1822 obj1
= to_union(ctx
, obj1
);
1823 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
1824 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
1825 obj2
= to_union(ctx
, obj2
);
1826 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
1827 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_space(obj1
.v
, obj2
.v
)) {
1828 obj1
= to_union(ctx
, obj1
);
1829 obj2
= to_union(ctx
, obj2
);
1831 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_space(obj1
.v
, obj2
.v
)) {
1832 obj1
= to_union(ctx
, obj1
);
1833 obj2
= to_union(ctx
, obj2
);
1835 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
1836 !isl_pw_qpolynomial_has_equal_space(obj1
.v
, obj2
.v
)) {
1837 obj1
= to_union(ctx
, obj1
);
1838 obj2
= to_union(ctx
, obj2
);
1840 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
1841 !isl_pw_qpolynomial_fold_has_equal_space(obj1
.v
, obj2
.v
)) {
1842 obj1
= to_union(ctx
, obj1
);
1843 obj2
= to_union(ctx
, obj2
);
1845 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
1848 obj1
.type
->free(obj1
.v
);
1849 obj2
.type
->free(obj2
.v
);
1850 obj1
.type
= isl_obj_none
;
1855 static struct isl_obj
obj_read(struct isl_stream
*s
, int nparam
)
1857 isl_map
*map
= NULL
;
1858 struct isl_token
*tok
;
1859 struct vars
*v
= NULL
;
1860 struct isl_obj obj
= { isl_obj_set
, NULL
};
1862 tok
= next_token(s
);
1864 isl_stream_error(s
, NULL
, "unexpected EOF");
1867 if (tok
->type
== ISL_TOKEN_VALUE
) {
1868 struct isl_token
*tok2
;
1869 struct isl_map
*map
;
1871 tok2
= isl_stream_next_token(s
);
1872 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
1873 isl_int_is_neg(tok2
->u
.v
)) {
1875 isl_stream_push_token(s
, tok2
);
1876 obj
.type
= isl_obj_int
;
1877 obj
.v
= isl_int_obj_alloc(s
->ctx
, tok
->u
.v
);
1878 isl_token_free(tok
);
1881 isl_stream_push_token(s
, tok2
);
1882 isl_stream_push_token(s
, tok
);
1883 map
= map_read_polylib(s
, nparam
);
1886 if (isl_map_may_be_set(map
))
1887 obj
.v
= isl_map_range(map
);
1889 obj
.type
= isl_obj_map
;
1894 v
= vars_new(s
->ctx
);
1896 isl_stream_push_token(s
, tok
);
1899 map
= isl_map_universe(isl_space_params_alloc(s
->ctx
, 0));
1900 if (tok
->type
== '[') {
1901 isl_stream_push_token(s
, tok
);
1902 map
= read_tuple(s
, map
, isl_dim_param
, v
);
1906 isl_assert(s
->ctx
, nparam
== v
->n
, goto error
);
1907 tok
= isl_stream_next_token(s
);
1908 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
1909 isl_stream_error(s
, tok
, "expecting '->'");
1911 isl_stream_push_token(s
, tok
);
1914 isl_token_free(tok
);
1915 tok
= isl_stream_next_token(s
);
1916 } else if (nparam
> 0)
1917 map
= isl_map_add_dims(map
, isl_dim_param
, nparam
);
1918 if (!tok
|| tok
->type
!= '{') {
1919 isl_stream_error(s
, tok
, "expecting '{'");
1921 isl_stream_push_token(s
, tok
);
1924 isl_token_free(tok
);
1926 tok
= isl_stream_next_token(s
);
1929 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
1930 isl_token_free(tok
);
1931 if (isl_stream_eat(s
, '='))
1933 map
= read_tuple(s
, map
, isl_dim_param
, v
);
1937 isl_assert(s
->ctx
, nparam
== v
->n
, goto error
);
1938 } else if (tok
->type
== '}') {
1939 obj
.type
= isl_obj_union_set
;
1940 obj
.v
= isl_union_set_empty(isl_map_get_space(map
));
1941 isl_token_free(tok
);
1944 isl_stream_push_token(s
, tok
);
1949 o
= obj_read_body(s
, isl_map_copy(map
), v
);
1950 if (o
.type
== isl_obj_none
|| !o
.v
)
1955 obj
= obj_add(s
->ctx
, obj
, o
);
1956 if (obj
.type
== isl_obj_none
|| !obj
.v
)
1959 tok
= isl_stream_next_token(s
);
1960 if (!tok
|| tok
->type
!= ';')
1962 isl_token_free(tok
);
1963 if (isl_stream_next_token_is(s
, '}')) {
1964 tok
= isl_stream_next_token(s
);
1969 if (tok
&& tok
->type
== '}') {
1970 isl_token_free(tok
);
1972 isl_stream_error(s
, tok
, "unexpected isl_token");
1974 isl_token_free(tok
);
1984 obj
.type
->free(obj
.v
);
1991 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
1993 return obj_read(s
, -1);
1996 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
, int nparam
)
2000 obj
= obj_read(s
, nparam
);
2002 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
2003 obj
.type
== isl_obj_set
, goto error
);
2007 obj
.type
->free(obj
.v
);
2011 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
, int nparam
)
2015 obj
= obj_read(s
, nparam
);
2017 if (obj
.type
== isl_obj_map
&& isl_map_may_be_set(obj
.v
)) {
2018 obj
.v
= isl_map_range(obj
.v
);
2019 obj
.type
= isl_obj_set
;
2021 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
2026 obj
.type
->free(obj
.v
);
2030 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
2034 obj
= obj_read(s
, -1);
2035 if (obj
.type
== isl_obj_map
) {
2036 obj
.type
= isl_obj_union_map
;
2037 obj
.v
= isl_union_map_from_map(obj
.v
);
2039 if (obj
.type
== isl_obj_set
) {
2040 obj
.type
= isl_obj_union_set
;
2041 obj
.v
= isl_union_set_from_set(obj
.v
);
2044 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
||
2045 obj
.type
== isl_obj_union_set
, goto error
);
2049 obj
.type
->free(obj
.v
);
2053 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
2057 obj
= obj_read(s
, -1);
2058 if (obj
.type
== isl_obj_set
) {
2059 obj
.type
= isl_obj_union_set
;
2060 obj
.v
= isl_union_set_from_set(obj
.v
);
2063 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
2067 obj
.type
->free(obj
.v
);
2071 static struct isl_basic_map
*basic_map_read(struct isl_stream
*s
, int nparam
)
2074 struct isl_map
*map
;
2075 struct isl_basic_map
*bmap
;
2077 obj
= obj_read(s
, nparam
);
2082 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
2085 bmap
= isl_basic_map_empty_like_map(map
);
2087 bmap
= isl_basic_map_copy(map
->p
[0]);
2097 static __isl_give isl_basic_set
*basic_set_read(struct isl_stream
*s
,
2100 isl_basic_map
*bmap
;
2101 bmap
= basic_map_read(s
, nparam
);
2104 if (!isl_basic_map_may_be_set(bmap
))
2105 isl_die(s
->ctx
, isl_error_invalid
,
2106 "input is not a set", goto error
);
2107 return isl_basic_map_range(bmap
);
2109 isl_basic_map_free(bmap
);
2113 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
2114 FILE *input
, int nparam
)
2116 struct isl_basic_map
*bmap
;
2117 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2120 bmap
= basic_map_read(s
, nparam
);
2125 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
2126 FILE *input
, int nparam
)
2128 isl_basic_set
*bset
;
2129 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2132 bset
= basic_set_read(s
, nparam
);
2137 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2138 const char *str
, int nparam
)
2140 struct isl_basic_map
*bmap
;
2141 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2144 bmap
= basic_map_read(s
, nparam
);
2149 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2150 const char *str
, int nparam
)
2152 isl_basic_set
*bset
;
2153 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2156 bset
= basic_set_read(s
, nparam
);
2161 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2162 FILE *input
, int nparam
)
2164 struct isl_map
*map
;
2165 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2168 map
= isl_stream_read_map(s
, nparam
);
2173 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2174 const char *str
, int nparam
)
2176 struct isl_map
*map
;
2177 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2180 map
= isl_stream_read_map(s
, nparam
);
2185 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2186 FILE *input
, int nparam
)
2189 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2192 set
= isl_stream_read_set(s
, nparam
);
2197 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2198 const char *str
, int nparam
)
2201 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2204 set
= isl_stream_read_set(s
, nparam
);
2209 __isl_give isl_union_map
*isl_union_map_read_from_file(isl_ctx
*ctx
,
2212 isl_union_map
*umap
;
2213 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2216 umap
= isl_stream_read_union_map(s
);
2221 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2224 isl_union_map
*umap
;
2225 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2228 umap
= isl_stream_read_union_map(s
);
2233 __isl_give isl_union_set
*isl_union_set_read_from_file(isl_ctx
*ctx
,
2236 isl_union_set
*uset
;
2237 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2240 uset
= isl_stream_read_union_set(s
);
2245 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2248 isl_union_set
*uset
;
2249 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2252 uset
= isl_stream_read_union_set(s
);
2257 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2259 struct isl_vec
*vec
= NULL
;
2260 struct isl_token
*tok
;
2264 tok
= isl_stream_next_token(s
);
2265 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2266 isl_stream_error(s
, tok
, "expecting vector length");
2270 size
= isl_int_get_si(tok
->u
.v
);
2271 isl_token_free(tok
);
2273 vec
= isl_vec_alloc(s
->ctx
, size
);
2275 for (j
= 0; j
< size
; ++j
) {
2276 tok
= isl_stream_next_token(s
);
2277 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2278 isl_stream_error(s
, tok
, "expecting constant value");
2281 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2282 isl_token_free(tok
);
2287 isl_token_free(tok
);
2292 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
)
2294 return isl_vec_read_polylib(s
);
2297 __isl_give isl_vec
*isl_vec_read_from_file(isl_ctx
*ctx
, FILE *input
)
2300 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2308 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2309 struct isl_stream
*s
)
2313 obj
= obj_read(s
, -1);
2315 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2320 obj
.type
->free(obj
.v
);
2324 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2327 isl_pw_qpolynomial
*pwqp
;
2328 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2331 pwqp
= isl_stream_read_pw_qpolynomial(s
);
2336 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_file(isl_ctx
*ctx
,
2339 isl_pw_qpolynomial
*pwqp
;
2340 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2343 pwqp
= isl_stream_read_pw_qpolynomial(s
);