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
17 #include <isl_map_private.h>
21 #include <isl/stream.h>
23 #include "isl_polynomial_private.h"
24 #include <isl/union_map.h>
25 #include <isl_mat_private.h>
31 struct variable
*next
;
40 static struct vars
*vars_new(struct isl_ctx
*ctx
)
43 v
= isl_alloc_type(ctx
, struct vars
);
52 static void variable_free(struct variable
*var
)
55 struct variable
*next
= var
->next
;
56 isl_vec_free(var
->def
);
63 static void vars_free(struct vars
*v
)
71 static void vars_drop(struct vars
*v
, int n
)
82 struct variable
*next
= var
->next
;
83 isl_vec_free(var
->def
);
91 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
95 var
= isl_alloc_type(v
->ctx
, struct variable
);
98 var
->name
= strdup(name
);
99 var
->name
[len
] = '\0';
109 static int vars_pos(struct vars
*v
, const char *s
, int len
)
116 for (q
= v
->v
; q
; q
= q
->next
) {
117 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
124 v
->v
= variable_new(v
, s
, len
, v
->n
);
132 static int vars_add_anon(struct vars
*v
)
134 v
->v
= variable_new(v
, "", 0, v
->n
);
143 static __isl_give isl_basic_map
*set_name(__isl_take isl_basic_map
*bmap
,
144 enum isl_dim_type type
, unsigned pos
, char *name
)
153 prime
= strchr(name
, '\'');
156 bmap
= isl_basic_map_set_dim_name(bmap
, type
, pos
, name
);
163 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
165 struct isl_token
*tok
;
167 tok
= isl_stream_next_token(s
);
168 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
169 isl_stream_error(s
, tok
, "expecting constant value");
173 isl_int_mul(*f
, *f
, tok
->u
.v
);
177 if (isl_stream_eat_if_available(s
, '*'))
178 return accept_cst_factor(s
, f
);
186 /* Given an affine expression aff, return an affine expression
187 * for aff % d, with d the next token on the stream, which is
188 * assumed to be a constant.
190 * We introduce an integer division q = [aff/d] and the result
191 * is set to aff - d q.
193 static __isl_give isl_vec
*affine_mod(struct isl_stream
*s
,
194 struct vars
*v
, __isl_take isl_vec
*aff
)
196 struct isl_token
*tok
;
197 struct variable
*var
;
200 tok
= isl_stream_next_token(s
);
201 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
202 isl_stream_error(s
, tok
, "expecting constant value");
206 if (vars_add_anon(v
) < 0)
211 var
->def
= isl_vec_alloc(s
->ctx
, 2 + v
->n
);
214 isl_seq_cpy(var
->def
->el
+ 1, aff
->el
, aff
->size
);
215 isl_int_set_si(var
->def
->el
[1 + aff
->size
], 0);
216 isl_int_set(var
->def
->el
[0], tok
->u
.v
);
218 mod
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
222 isl_seq_cpy(mod
->el
, aff
->el
, aff
->size
);
223 isl_int_neg(mod
->el
[aff
->size
], tok
->u
.v
);
234 static struct isl_vec
*accept_affine(struct isl_stream
*s
, struct vars
*v
);
235 static int read_div_definition(struct isl_stream
*s
, struct vars
*v
);
237 static __isl_give isl_vec
*accept_affine_factor(struct isl_stream
*s
,
240 struct isl_token
*tok
= NULL
;
243 tok
= isl_stream_next_token(s
);
245 isl_stream_error(s
, NULL
, "unexpected EOF");
248 if (tok
->type
== ISL_TOKEN_IDENT
) {
250 int pos
= vars_pos(v
, tok
->u
.s
, -1);
254 isl_stream_error(s
, tok
, "unknown identifier");
258 aff
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
261 isl_seq_clr(aff
->el
, aff
->size
);
262 isl_int_set_si(aff
->el
[1 + pos
], 1);
264 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
265 if (isl_stream_eat_if_available(s
, '*')) {
266 aff
= accept_affine_factor(s
, v
);
267 aff
= isl_vec_scale(aff
, tok
->u
.v
);
269 aff
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
272 isl_seq_clr(aff
->el
, aff
->size
);
273 isl_int_set(aff
->el
[0], tok
->u
.v
);
276 } else if (tok
->type
== '(') {
279 aff
= accept_affine(s
, v
);
282 if (isl_stream_eat(s
, ')'))
284 } else if (tok
->type
== '[') {
285 if (vars_add_anon(v
) < 0)
287 aff
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
290 isl_seq_clr(aff
->el
, aff
->size
);
291 isl_int_set_si(aff
->el
[1 + v
->n
- 1], 1);
292 isl_stream_push_token(s
, tok
);
294 if (read_div_definition(s
, v
) < 0)
297 isl_stream_error(s
, tok
, "expecting factor");
300 if (isl_stream_eat_if_available(s
, '%'))
301 return affine_mod(s
, v
, aff
);
302 if (isl_stream_eat_if_available(s
, '*')) {
305 isl_int_set_si(f
, 1);
306 if (accept_cst_factor(s
, &f
) < 0) {
310 aff
= isl_vec_scale(aff
, f
);
322 static struct isl_vec
*accept_affine(struct isl_stream
*s
, struct vars
*v
)
324 struct isl_token
*tok
= NULL
;
328 aff
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
331 isl_seq_clr(aff
->el
, aff
->size
);
334 tok
= isl_stream_next_token(s
);
336 isl_stream_error(s
, NULL
, "unexpected EOF");
339 if (tok
->type
== '-') {
344 if (tok
->type
== '(' || tok
->type
== '[' ||
345 tok
->type
== ISL_TOKEN_IDENT
) {
347 isl_stream_push_token(s
, tok
);
349 aff2
= accept_affine_factor(s
, v
);
351 aff2
= isl_vec_scale(aff2
, s
->ctx
->negone
);
352 aff
= isl_vec_zero_extend(aff
, 1 + v
->n
);
353 aff
= isl_vec_add(aff
, aff2
);
357 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
359 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
360 if (isl_stream_eat_if_available(s
, '*') ||
361 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
363 aff2
= accept_affine_factor(s
, v
);
364 aff2
= isl_vec_scale(aff2
, tok
->u
.v
);
365 aff
= isl_vec_zero_extend(aff
, 1 + v
->n
);
366 aff
= isl_vec_add(aff
, aff2
);
370 isl_int_add(aff
->el
[0], aff
->el
[0], tok
->u
.v
);
374 isl_stream_error(s
, tok
, "unexpected isl_token");
375 isl_stream_push_token(s
, tok
);
381 tok
= isl_stream_next_token(s
);
382 if (tok
&& tok
->type
== '-') {
385 } else if (tok
&& tok
->type
== '+') {
388 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
389 isl_int_is_neg(tok
->u
.v
)) {
390 isl_stream_push_token(s
, tok
);
393 isl_stream_push_token(s
, tok
);
405 /* Add any variables in the variable list "v" that are not already in "bmap"
406 * as existentially quantified variables in "bmap".
408 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
413 struct variable
*var
;
415 extra
= v
->n
- isl_basic_map_total_dim(bmap
);
420 bmap
= isl_basic_map_extend_dim(bmap
, isl_basic_map_get_dim(bmap
),
421 extra
, 0, 2 * extra
);
423 for (i
= 0; i
< extra
; ++i
)
424 if (isl_basic_map_alloc_div(bmap
) < 0)
427 for (i
= 0, var
= v
->v
; i
< extra
; ++i
, var
= var
->next
) {
428 int k
= bmap
->n_div
- 1 - i
;
430 isl_seq_cpy(bmap
->div
[k
], var
->def
->el
, var
->def
->size
);
431 isl_seq_clr(bmap
->div
[k
] + var
->def
->size
,
432 2 + v
->n
- var
->def
->size
);
434 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
440 isl_basic_map_free(bmap
);
444 static __isl_give isl_basic_map
*read_var_def(struct isl_stream
*s
,
445 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, struct vars
*v
)
448 isl_basic_map
*def
= NULL
;
453 if (vars_add_anon(v
) < 0)
457 vec
= accept_affine(s
, v
);
461 dim
= isl_basic_map_get_dim(bmap
);
462 def
= isl_basic_map_universe(dim
);
463 def
= add_divs(def
, v
);
464 def
= isl_basic_map_extend_constraints(def
, 1, 0);
465 k
= isl_basic_map_alloc_equality(def
);
467 isl_seq_cpy(def
->eq
[k
], vec
->el
, vec
->size
);
468 isl_int_set_si(def
->eq
[k
][1 + n
- 1], -1);
474 vars_drop(v
, v
->n
- n
);
476 def
= isl_basic_map_simplify(def
);
477 def
= isl_basic_map_finalize(def
);
478 bmap
= isl_basic_map_intersect(bmap
, def
);
481 isl_basic_map_free(bmap
);
482 isl_basic_map_free(def
);
486 static __isl_give isl_basic_map
*read_var_list(struct isl_stream
*s
,
487 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, struct vars
*v
)
490 struct isl_token
*tok
;
492 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
495 if (tok
->type
== ISL_TOKEN_IDENT
) {
497 int p
= vars_pos(v
, tok
->u
.s
, -1);
504 bmap
= isl_basic_map_add(bmap
, type
, 1);
505 bmap
= set_name(bmap
, type
, i
, v
->v
->name
);
507 } else if (tok
->type
== ISL_TOKEN_IDENT
||
508 tok
->type
== ISL_TOKEN_VALUE
||
511 if (type
== isl_dim_param
) {
512 isl_stream_error(s
, tok
,
513 "expecting unique identifier");
516 isl_stream_push_token(s
, tok
);
518 bmap
= isl_basic_map_add(bmap
, type
, 1);
519 bmap
= read_var_def(s
, bmap
, type
, v
);
523 tok
= isl_stream_next_token(s
);
524 if (!tok
|| tok
->type
!= ',')
531 isl_stream_push_token(s
, tok
);
536 isl_basic_map_free(bmap
);
540 static __isl_give isl_mat
*accept_affine_list(struct isl_stream
*s
,
545 struct isl_token
*tok
= NULL
;
547 vec
= accept_affine(s
, v
);
548 mat
= isl_mat_from_row_vec(vec
);
553 tok
= isl_stream_next_token(s
);
555 isl_stream_error(s
, NULL
, "unexpected EOF");
558 if (tok
->type
!= ',') {
559 isl_stream_push_token(s
, tok
);
564 vec
= accept_affine(s
, v
);
565 mat
= isl_mat_add_zero_cols(mat
, 1 + v
->n
- isl_mat_cols(mat
));
566 mat
= isl_mat_vec_concat(mat
, vec
);
577 static int read_div_definition(struct isl_stream
*s
, struct vars
*v
)
579 struct isl_token
*tok
;
582 struct variable
*var
;
584 if (isl_stream_eat(s
, '['))
587 tok
= isl_stream_next_token(s
);
590 if (tok
->type
== '(') {
594 isl_stream_push_token(s
, tok
);
598 aff
= accept_affine(s
, v
);
602 var
->def
= isl_vec_alloc(s
->ctx
, 2 + v
->n
);
608 isl_seq_cpy(var
->def
->el
+ 1, aff
->el
, aff
->size
);
612 if (seen_paren
&& isl_stream_eat(s
, ')'))
614 if (isl_stream_eat(s
, '/'))
617 tok
= isl_stream_next_token(s
);
620 if (tok
->type
!= ISL_TOKEN_VALUE
) {
621 isl_stream_error(s
, tok
, "expected denominator");
622 isl_stream_push_token(s
, tok
);
625 isl_int_set(var
->def
->el
[0], tok
->u
.v
);
628 if (isl_stream_eat(s
, ']'))
634 static struct isl_basic_map
*add_div_definition(struct isl_stream
*s
,
635 struct vars
*v
, struct isl_basic_map
*bmap
, int pos
)
637 struct variable
*var
= v
->v
;
638 unsigned o_out
= isl_basic_map_offset(bmap
, isl_dim_out
) - 1;
640 if (read_div_definition(s
, v
) < 0)
643 if (isl_basic_map_add_div_constraints_var(bmap
, o_out
+ pos
,
649 isl_basic_map_free(bmap
);
653 static struct isl_basic_map
*read_defined_var_list(struct isl_stream
*s
,
654 struct vars
*v
, struct isl_basic_map
*bmap
)
656 struct isl_token
*tok
;
658 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
661 unsigned n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
663 if (tok
->type
!= ISL_TOKEN_IDENT
)
666 p
= vars_pos(v
, tok
->u
.s
, -1);
670 isl_stream_error(s
, tok
, "expecting unique identifier");
674 bmap
= isl_basic_map_cow(bmap
);
675 bmap
= isl_basic_map_add(bmap
, isl_dim_out
, 1);
676 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
680 tok
= isl_stream_next_token(s
);
681 if (tok
&& tok
->type
== '=') {
683 bmap
= add_div_definition(s
, v
, bmap
, n_out
);
684 tok
= isl_stream_next_token(s
);
687 if (!tok
|| tok
->type
!= ',')
693 isl_stream_push_token(s
, tok
);
698 isl_basic_map_free(bmap
);
702 static int next_is_tuple(struct isl_stream
*s
)
704 struct isl_token
*tok
;
707 tok
= isl_stream_next_token(s
);
710 if (tok
->type
== '[') {
711 isl_stream_push_token(s
, tok
);
714 if (tok
->type
!= ISL_TOKEN_IDENT
&& !tok
->is_keyword
) {
715 isl_stream_push_token(s
, tok
);
719 is_tuple
= isl_stream_next_token_is(s
, '[');
721 isl_stream_push_token(s
, tok
);
726 static __isl_give isl_basic_map
*read_tuple(struct isl_stream
*s
,
727 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, struct vars
*v
);
729 static __isl_give isl_basic_map
*read_nested_tuple(struct isl_stream
*s
,
730 __isl_take isl_basic_map
*bmap
, struct vars
*v
)
732 bmap
= read_tuple(s
, bmap
, isl_dim_in
, v
);
733 if (isl_stream_eat(s
, ISL_TOKEN_TO
))
735 bmap
= read_tuple(s
, bmap
, isl_dim_out
, v
);
736 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
739 isl_basic_map_free(bmap
);
743 static __isl_give isl_basic_map
*read_tuple(struct isl_stream
*s
,
744 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, struct vars
*v
)
746 struct isl_token
*tok
;
749 tok
= isl_stream_next_token(s
);
750 if (tok
&& (tok
->type
== ISL_TOKEN_IDENT
|| tok
->is_keyword
)) {
751 name
= strdup(tok
->u
.s
);
755 tok
= isl_stream_next_token(s
);
757 if (!tok
|| tok
->type
!= '[') {
758 isl_stream_error(s
, tok
, "expecting '['");
762 if (type
!= isl_dim_param
&& next_is_tuple(s
)) {
763 isl_dim
*dim
= isl_basic_map_get_dim(bmap
);
764 int nparam
= isl_dim_size(dim
, isl_dim_param
);
765 int n_in
= isl_dim_size(dim
, isl_dim_in
);
766 isl_basic_map
*nested
;
767 if (type
== isl_dim_out
)
768 dim
= isl_dim_move(dim
, isl_dim_param
, nparam
,
769 isl_dim_in
, 0, n_in
);
770 nested
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
771 nested
= read_nested_tuple(s
, nested
, v
);
772 if (type
== isl_dim_in
) {
773 nested
= isl_basic_map_reverse(nested
);
774 bmap
= isl_basic_map_intersect(nested
, bmap
);
777 dim
= isl_dim_range(isl_basic_map_get_dim(nested
));
778 dim
= isl_dim_drop(dim
, isl_dim_param
, nparam
, n_in
);
779 dim
= isl_dim_join(isl_basic_map_get_dim(bmap
), dim
);
780 bset
= isl_basic_map_domain(bmap
);
781 nested
= isl_basic_map_reset_dim(nested
, dim
);
782 bmap
= isl_basic_map_intersect_domain(nested
, bset
);
785 bmap
= read_var_list(s
, bmap
, type
, v
);
786 tok
= isl_stream_next_token(s
);
787 if (!tok
|| tok
->type
!= ']') {
788 isl_stream_error(s
, tok
, "expecting ']'");
794 bmap
= isl_basic_map_set_tuple_name(bmap
, type
, name
);
802 isl_basic_map_free(bmap
);
806 static __isl_give isl_basic_map
*construct_constraint(
807 __isl_take isl_basic_map
*bmap
, enum isl_token_type type
,
808 isl_int
*left
, isl_int
*right
)
816 len
= 1 + isl_basic_map_total_dim(bmap
);
819 k
= isl_basic_map_alloc_inequality(bmap
);
822 if (type
== ISL_TOKEN_LE
)
823 isl_seq_combine(bmap
->ineq
[k
], ctx
->negone
, left
,
826 else if (type
== ISL_TOKEN_GE
)
827 isl_seq_combine(bmap
->ineq
[k
], ctx
->one
, left
,
830 else if (type
== ISL_TOKEN_LT
) {
831 isl_seq_combine(bmap
->ineq
[k
], ctx
->negone
, left
,
834 isl_int_sub_ui(bmap
->ineq
[k
][0], bmap
->ineq
[k
][0], 1);
835 } else if (type
== ISL_TOKEN_GT
) {
836 isl_seq_combine(bmap
->ineq
[k
], ctx
->one
, left
,
839 isl_int_sub_ui(bmap
->ineq
[k
][0], bmap
->ineq
[k
][0], 1);
841 isl_seq_combine(bmap
->ineq
[k
], ctx
->one
, left
,
844 isl_basic_map_inequality_to_equality(bmap
, k
);
849 isl_basic_map_free(bmap
);
853 static int is_comparator(struct isl_token
*tok
)
870 static struct isl_basic_map
*add_constraint(struct isl_stream
*s
,
871 struct vars
*v
, struct isl_basic_map
*bmap
)
874 struct isl_token
*tok
= NULL
;
875 struct isl_mat
*aff1
= NULL
, *aff2
= NULL
;
877 bmap
= isl_basic_map_cow(bmap
);
879 aff1
= accept_affine_list(s
, v
);
882 tok
= isl_stream_next_token(s
);
883 if (!is_comparator(tok
)) {
884 isl_stream_error(s
, tok
, "missing operator");
886 isl_stream_push_token(s
, tok
);
891 aff2
= accept_affine_list(s
, v
);
895 aff1
= isl_mat_add_zero_cols(aff1
, aff2
->n_col
- aff1
->n_col
);
898 bmap
= add_divs(bmap
, v
);
899 bmap
= isl_basic_map_extend_constraints(bmap
, 0,
900 aff1
->n_row
* aff2
->n_row
);
901 for (i
= 0; i
< aff1
->n_row
; ++i
)
902 for (j
= 0; j
< aff2
->n_row
; ++j
)
903 bmap
= construct_constraint(bmap
, tok
->type
,
904 aff1
->row
[i
], aff2
->row
[j
]);
909 tok
= isl_stream_next_token(s
);
910 if (!is_comparator(tok
)) {
912 isl_stream_push_token(s
, tok
);
924 isl_basic_map_free(bmap
);
928 static isl_map
*read_constraint(struct isl_stream
*s
,
929 struct vars
*v
, __isl_take isl_basic_map
*bmap
)
936 bmap
= add_constraint(s
, v
, bmap
);
937 bmap
= isl_basic_map_simplify(bmap
);
938 bmap
= isl_basic_map_finalize(bmap
);
940 vars_drop(v
, v
->n
- n
);
942 return isl_map_from_basic_map(bmap
);
944 isl_basic_map_free(bmap
);
948 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
949 struct vars
*v
, __isl_take isl_basic_map
*bmap
);
951 static __isl_give isl_map
*read_exists(struct isl_stream
*s
,
952 struct vars
*v
, __isl_take isl_basic_map
*bmap
)
955 int seen_paren
= isl_stream_eat_if_available(s
, '(');
958 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
959 bmap
= read_defined_var_list(s
, v
, bmap
);
961 if (isl_stream_eat(s
, ':'))
964 map
= read_disjuncts(s
, v
, bmap
);
965 map
= isl_set_unwrap(isl_map_domain(map
));
968 vars_drop(v
, v
->n
- n
);
969 if (seen_paren
&& isl_stream_eat(s
, ')'))
974 isl_basic_map_free(bmap
);
979 static __isl_give isl_map
*read_conjunct(struct isl_stream
*s
,
980 struct vars
*v
, __isl_take isl_basic_map
*bmap
)
984 if (isl_stream_eat_if_available(s
, '(')) {
985 map
= read_disjuncts(s
, v
, bmap
);
986 if (isl_stream_eat(s
, ')'))
991 if (isl_stream_eat_if_available(s
, ISL_TOKEN_EXISTS
))
992 return read_exists(s
, v
, bmap
);
994 if (isl_stream_eat_if_available(s
, ISL_TOKEN_TRUE
))
995 return isl_map_from_basic_map(bmap
);
997 if (isl_stream_eat_if_available(s
, ISL_TOKEN_FALSE
)) {
998 isl_dim
*dim
= isl_basic_map_get_dim(bmap
);
999 isl_basic_map_free(bmap
);
1000 return isl_map_empty(dim
);
1003 return read_constraint(s
, v
, bmap
);
1009 static __isl_give isl_map
*read_conjuncts(struct isl_stream
*s
,
1010 struct vars
*v
, __isl_take isl_basic_map
*bmap
)
1015 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1016 map
= read_conjunct(s
, v
, isl_basic_map_copy(bmap
));
1019 t
= isl_map_from_basic_map(isl_basic_map_copy(bmap
));
1020 map
= isl_map_subtract(t
, map
);
1023 while (isl_stream_eat_if_available(s
, ISL_TOKEN_AND
)) {
1026 negate
= isl_stream_eat_if_available(s
, ISL_TOKEN_NOT
);
1027 map_i
= read_conjunct(s
, v
, isl_basic_map_copy(bmap
));
1029 map
= isl_map_subtract(map
, map_i
);
1031 map
= isl_map_intersect(map
, map_i
);
1034 isl_basic_map_free(bmap
);
1038 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
1039 struct vars
*v
, __isl_take isl_basic_map
*bmap
)
1041 struct isl_map
*map
;
1043 if (isl_stream_next_token_is(s
, '}')) {
1044 isl_dim
*dim
= isl_basic_map_get_dim(bmap
);
1045 isl_basic_map_free(bmap
);
1046 return isl_map_universe(dim
);
1049 map
= read_conjuncts(s
, v
, isl_basic_map_copy(bmap
));
1050 while (isl_stream_eat_if_available(s
, ISL_TOKEN_OR
)) {
1053 map_i
= read_conjuncts(s
, v
, isl_basic_map_copy(bmap
));
1054 map
= isl_map_union(map
, map_i
);
1057 isl_basic_map_free(bmap
);
1061 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
1063 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
1064 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1065 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
1066 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
1068 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
1069 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
1070 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
1072 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
1073 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
1074 isl_basic_map_dim(bmap
, isl_dim_in
) +
1075 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
1076 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
1078 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
1084 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
1085 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
1088 struct isl_token
*tok
;
1098 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1099 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
1101 tok
= isl_stream_next_token(s
);
1102 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1103 isl_stream_error(s
, tok
, "expecting coefficient");
1105 isl_stream_push_token(s
, tok
);
1108 if (!tok
->on_new_line
) {
1109 isl_stream_error(s
, tok
, "coefficient should appear on new line");
1110 isl_stream_push_token(s
, tok
);
1114 type
= isl_int_get_si(tok
->u
.v
);
1115 isl_token_free(tok
);
1117 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
1119 k
= isl_basic_map_alloc_equality(bmap
);
1122 k
= isl_basic_map_alloc_inequality(bmap
);
1128 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
1130 tok
= isl_stream_next_token(s
);
1131 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1132 isl_stream_error(s
, tok
, "expecting coefficient");
1134 isl_stream_push_token(s
, tok
);
1137 if (tok
->on_new_line
) {
1138 isl_stream_error(s
, tok
,
1139 "coefficient should not appear on new line");
1140 isl_stream_push_token(s
, tok
);
1143 pos
= polylib_pos_to_isl_pos(bmap
, j
);
1144 isl_int_set(c
[pos
], tok
->u
.v
);
1145 isl_token_free(tok
);
1150 isl_basic_map_free(bmap
);
1154 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
,
1158 struct isl_token
*tok
;
1159 struct isl_token
*tok2
;
1162 unsigned in
= 0, out
, local
= 0;
1163 struct isl_basic_map
*bmap
= NULL
;
1168 tok
= isl_stream_next_token(s
);
1170 isl_stream_error(s
, NULL
, "unexpected EOF");
1173 tok2
= isl_stream_next_token(s
);
1175 isl_token_free(tok
);
1176 isl_stream_error(s
, NULL
, "unexpected EOF");
1179 if (tok
->type
!= ISL_TOKEN_VALUE
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1180 isl_stream_push_token(s
, tok2
);
1181 isl_stream_push_token(s
, tok
);
1182 isl_stream_error(s
, NULL
,
1183 "expecting constraint matrix dimensions");
1186 n_row
= isl_int_get_si(tok
->u
.v
);
1187 n_col
= isl_int_get_si(tok2
->u
.v
);
1188 on_new_line
= tok2
->on_new_line
;
1189 isl_token_free(tok2
);
1190 isl_token_free(tok
);
1191 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
1192 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
1193 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
1194 tok
= isl_stream_next_token_on_same_line(s
);
1196 if (tok
->type
!= ISL_TOKEN_VALUE
) {
1197 isl_stream_error(s
, tok
,
1198 "expecting number of output dimensions");
1199 isl_stream_push_token(s
, tok
);
1202 out
= isl_int_get_si(tok
->u
.v
);
1203 isl_token_free(tok
);
1205 tok
= isl_stream_next_token_on_same_line(s
);
1206 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1207 isl_stream_error(s
, tok
,
1208 "expecting number of input dimensions");
1210 isl_stream_push_token(s
, tok
);
1213 in
= isl_int_get_si(tok
->u
.v
);
1214 isl_token_free(tok
);
1216 tok
= isl_stream_next_token_on_same_line(s
);
1217 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1218 isl_stream_error(s
, tok
,
1219 "expecting number of existentials");
1221 isl_stream_push_token(s
, tok
);
1224 local
= isl_int_get_si(tok
->u
.v
);
1225 isl_token_free(tok
);
1227 tok
= isl_stream_next_token_on_same_line(s
);
1228 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1229 isl_stream_error(s
, tok
,
1230 "expecting number of parameters");
1232 isl_stream_push_token(s
, tok
);
1235 nparam
= isl_int_get_si(tok
->u
.v
);
1236 isl_token_free(tok
);
1237 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1238 isl_stream_error(s
, NULL
,
1239 "dimensions don't match");
1243 out
= n_col
- 2 - nparam
;
1244 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1248 for (i
= 0; i
< local
; ++i
) {
1249 int k
= isl_basic_map_alloc_div(bmap
);
1252 isl_seq_clr(bmap
->div
[k
], 1 + 1 + nparam
+ in
+ out
+ local
);
1255 for (i
= 0; i
< n_row
; ++i
)
1256 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1258 tok
= isl_stream_next_token_on_same_line(s
);
1260 isl_stream_error(s
, tok
, "unexpected extra token on line");
1261 isl_stream_push_token(s
, tok
);
1265 bmap
= isl_basic_map_simplify(bmap
);
1266 bmap
= isl_basic_map_finalize(bmap
);
1269 isl_basic_map_free(bmap
);
1273 static struct isl_map
*map_read_polylib(struct isl_stream
*s
, int nparam
)
1275 struct isl_token
*tok
;
1276 struct isl_token
*tok2
;
1278 struct isl_map
*map
;
1280 tok
= isl_stream_next_token(s
);
1282 isl_stream_error(s
, NULL
, "unexpected EOF");
1285 tok2
= isl_stream_next_token_on_same_line(s
);
1286 if (tok2
&& tok2
->type
== ISL_TOKEN_VALUE
) {
1287 isl_stream_push_token(s
, tok2
);
1288 isl_stream_push_token(s
, tok
);
1289 return isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
));
1292 isl_stream_error(s
, tok2
, "unexpected token");
1293 isl_stream_push_token(s
, tok2
);
1294 isl_stream_push_token(s
, tok
);
1297 n
= isl_int_get_si(tok
->u
.v
);
1298 isl_token_free(tok
);
1300 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1302 map
= isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
));
1304 for (i
= 1; map
&& i
< n
; ++i
)
1305 map
= isl_map_union(map
,
1306 isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
)));
1311 static int optional_power(struct isl_stream
*s
)
1314 struct isl_token
*tok
;
1316 tok
= isl_stream_next_token(s
);
1319 if (tok
->type
!= '^') {
1320 isl_stream_push_token(s
, tok
);
1323 isl_token_free(tok
);
1324 tok
= isl_stream_next_token(s
);
1325 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1326 isl_stream_error(s
, tok
, "expecting exponent");
1328 isl_stream_push_token(s
, tok
);
1331 pow
= isl_int_get_si(tok
->u
.v
);
1332 isl_token_free(tok
);
1336 static __isl_give isl_div
*read_div(struct isl_stream
*s
,
1337 __isl_take isl_dim
*dim
, struct vars
*v
)
1340 isl_basic_map
*bmap
;
1343 bmap
= isl_basic_map_universe(dim
);
1345 if (vars_add_anon(v
) < 0)
1347 if (read_div_definition(s
, v
) < 0)
1349 bmap
= add_divs(bmap
, v
);
1350 bmap
= isl_basic_map_order_divs(bmap
);
1353 vars_drop(v
, v
->n
- n
);
1355 return isl_basic_map_div(bmap
, bmap
->n_div
- 1);
1357 isl_basic_map_free(bmap
);
1361 static __isl_give isl_qpolynomial
*read_term(struct isl_stream
*s
,
1362 __isl_keep isl_basic_map
*bmap
, struct vars
*v
);
1364 static __isl_give isl_qpolynomial
*read_factor(struct isl_stream
*s
,
1365 __isl_keep isl_basic_map
*bmap
, struct vars
*v
)
1367 struct isl_qpolynomial
*qp
;
1368 struct isl_token
*tok
;
1370 tok
= isl_stream_next_token(s
);
1372 isl_stream_error(s
, NULL
, "unexpected EOF");
1375 if (tok
->type
== '(') {
1378 isl_token_free(tok
);
1379 qp
= read_term(s
, bmap
, v
);
1382 if (isl_stream_eat(s
, ')'))
1384 pow
= optional_power(s
);
1385 qp
= isl_qpolynomial_pow(qp
, pow
);
1386 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1387 struct isl_token
*tok2
;
1388 tok2
= isl_stream_next_token(s
);
1389 if (tok2
&& tok2
->type
== '/') {
1390 isl_token_free(tok2
);
1391 tok2
= isl_stream_next_token(s
);
1392 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1393 isl_stream_error(s
, tok2
, "expected denominator");
1394 isl_token_free(tok
);
1395 isl_token_free(tok2
);
1398 qp
= isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap
),
1399 tok
->u
.v
, tok2
->u
.v
);
1400 isl_token_free(tok2
);
1402 isl_stream_push_token(s
, tok2
);
1403 qp
= isl_qpolynomial_cst(isl_basic_map_get_dim(bmap
),
1406 isl_token_free(tok
);
1407 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1408 isl_token_free(tok
);
1409 qp
= isl_qpolynomial_infty(isl_basic_map_get_dim(bmap
));
1410 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1411 isl_token_free(tok
);
1412 qp
= isl_qpolynomial_nan(isl_basic_map_get_dim(bmap
));
1413 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1415 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1418 isl_token_free(tok
);
1422 vars_drop(v
, v
->n
- n
);
1423 isl_stream_error(s
, tok
, "unknown identifier");
1424 isl_token_free(tok
);
1427 isl_token_free(tok
);
1428 pow
= optional_power(s
);
1429 qp
= isl_qpolynomial_var_pow(isl_basic_map_get_dim(bmap
), pos
, pow
);
1430 } else if (tok
->type
== '[') {
1434 isl_stream_push_token(s
, tok
);
1435 div
= read_div(s
, isl_basic_map_get_dim(bmap
), v
);
1436 pow
= optional_power(s
);
1437 qp
= isl_qpolynomial_div_pow(div
, pow
);
1438 } else if (tok
->type
== '-') {
1439 struct isl_qpolynomial
*qp2
;
1441 isl_token_free(tok
);
1442 qp
= isl_qpolynomial_cst(isl_basic_map_get_dim(bmap
),
1444 qp2
= read_factor(s
, bmap
, v
);
1445 qp
= isl_qpolynomial_mul(qp
, qp2
);
1447 isl_stream_error(s
, tok
, "unexpected isl_token");
1448 isl_stream_push_token(s
, tok
);
1452 if (isl_stream_eat_if_available(s
, '*') ||
1453 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1454 struct isl_qpolynomial
*qp2
;
1456 qp2
= read_factor(s
, bmap
, v
);
1457 qp
= isl_qpolynomial_mul(qp
, qp2
);
1462 isl_qpolynomial_free(qp
);
1466 static __isl_give isl_qpolynomial
*read_term(struct isl_stream
*s
,
1467 __isl_keep isl_basic_map
*bmap
, struct vars
*v
)
1469 struct isl_token
*tok
;
1470 struct isl_qpolynomial
*qp
;
1472 qp
= read_factor(s
, bmap
, v
);
1475 tok
= isl_stream_next_token(s
);
1479 if (tok
->type
== '+') {
1480 struct isl_qpolynomial
*qp2
;
1482 isl_token_free(tok
);
1483 qp2
= read_factor(s
, bmap
, v
);
1484 qp
= isl_qpolynomial_add(qp
, qp2
);
1485 } else if (tok
->type
== '-') {
1486 struct isl_qpolynomial
*qp2
;
1488 isl_token_free(tok
);
1489 qp2
= read_factor(s
, bmap
, v
);
1490 qp
= isl_qpolynomial_sub(qp
, qp2
);
1491 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1492 isl_int_is_neg(tok
->u
.v
)) {
1493 struct isl_qpolynomial
*qp2
;
1495 isl_stream_push_token(s
, tok
);
1496 qp2
= read_factor(s
, bmap
, v
);
1497 qp
= isl_qpolynomial_add(qp
, qp2
);
1499 isl_stream_push_token(s
, tok
);
1507 static __isl_give isl_map
*read_optional_disjuncts(struct isl_stream
*s
,
1508 __isl_take isl_basic_map
*bmap
, struct vars
*v
)
1510 struct isl_token
*tok
;
1511 struct isl_map
*map
;
1513 tok
= isl_stream_next_token(s
);
1515 isl_stream_error(s
, NULL
, "unexpected EOF");
1518 map
= isl_map_from_basic_map(isl_basic_map_copy(bmap
));
1519 if (tok
->type
== ':' ||
1520 (tok
->type
== ISL_TOKEN_OR
&& !strcmp(tok
->u
.s
, "|"))) {
1521 isl_token_free(tok
);
1522 map
= isl_map_intersect(map
,
1523 read_disjuncts(s
, v
, isl_basic_map_copy(bmap
)));
1525 isl_stream_push_token(s
, tok
);
1527 isl_basic_map_free(bmap
);
1531 isl_basic_map_free(bmap
);
1535 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1536 __isl_take isl_basic_map
*bmap
, struct vars
*v
, int n
)
1538 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1539 struct isl_pw_qpolynomial
*pwqp
;
1540 struct isl_qpolynomial
*qp
;
1541 struct isl_map
*map
;
1542 struct isl_set
*set
;
1544 qp
= read_term(s
, bmap
, v
);
1545 map
= read_optional_disjuncts(s
, bmap
, v
);
1546 set
= isl_map_range(map
);
1548 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
1550 vars_drop(v
, v
->n
- n
);
1556 static struct isl_obj
obj_read_poly_or_fold(struct isl_stream
*s
,
1557 __isl_take isl_basic_map
*bmap
, struct vars
*v
, int n
)
1559 struct isl_obj obj
= { isl_obj_pw_qpolynomial_fold
, NULL
};
1560 struct isl_obj obj_p
;
1561 isl_qpolynomial
*qp
;
1562 isl_qpolynomial_fold
*fold
= NULL
;
1563 isl_pw_qpolynomial_fold
*pwf
;
1567 if (!isl_stream_eat_if_available(s
, ISL_TOKEN_MAX
))
1568 return obj_read_poly(s
, bmap
, v
, n
);
1570 if (isl_stream_eat(s
, '('))
1573 qp
= read_term(s
, bmap
, v
);
1574 fold
= isl_qpolynomial_fold_alloc(isl_fold_max
, qp
);
1576 while (isl_stream_eat_if_available(s
, ',')) {
1577 isl_qpolynomial_fold
*fold_i
;
1578 qp
= read_term(s
, bmap
, v
);
1579 fold_i
= isl_qpolynomial_fold_alloc(isl_fold_max
, qp
);
1580 fold
= isl_qpolynomial_fold_fold(fold
, fold_i
);
1583 if (isl_stream_eat(s
, ')'))
1586 map
= read_optional_disjuncts(s
, bmap
, v
);
1587 set
= isl_map_range(map
);
1588 pwf
= isl_pw_qpolynomial_fold_alloc(isl_fold_max
, set
, fold
);
1590 vars_drop(v
, v
->n
- n
);
1595 isl_basic_map_free(bmap
);
1596 isl_qpolynomial_fold_free(fold
);
1597 obj
.type
= isl_obj_none
;
1601 static int is_rational(struct isl_stream
*s
)
1603 struct isl_token
*tok
;
1605 tok
= isl_stream_next_token(s
);
1608 if (tok
->type
== ISL_TOKEN_RAT
&& isl_stream_next_token_is(s
, ':')) {
1609 isl_token_free(tok
);
1610 isl_stream_eat(s
, ':');
1614 isl_stream_push_token(s
, tok
);
1619 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
1620 __isl_take isl_basic_map
*bmap
, struct vars
*v
)
1622 struct isl_map
*map
= NULL
;
1623 struct isl_token
*tok
;
1624 struct isl_obj obj
= { isl_obj_set
, NULL
};
1628 bmap
= isl_basic_map_set_rational(bmap
);
1630 if (!next_is_tuple(s
))
1631 return obj_read_poly_or_fold(s
, bmap
, v
, n
);
1633 bmap
= read_tuple(s
, bmap
, isl_dim_in
, v
);
1636 tok
= isl_stream_next_token(s
);
1637 if (tok
&& tok
->type
== ISL_TOKEN_TO
) {
1638 obj
.type
= isl_obj_map
;
1639 isl_token_free(tok
);
1640 if (!next_is_tuple(s
)) {
1641 bmap
= isl_basic_map_reverse(bmap
);
1642 return obj_read_poly_or_fold(s
, bmap
, v
, n
);
1644 bmap
= read_tuple(s
, bmap
, isl_dim_out
, v
);
1648 bmap
= isl_basic_map_reverse(bmap
);
1650 isl_stream_push_token(s
, tok
);
1653 map
= read_optional_disjuncts(s
, bmap
, v
);
1655 vars_drop(v
, v
->n
- n
);
1660 isl_basic_map_free(bmap
);
1661 obj
.type
= isl_obj_none
;
1665 static struct isl_obj
to_union(isl_ctx
*ctx
, struct isl_obj obj
)
1667 if (obj
.type
== isl_obj_map
) {
1668 obj
.v
= isl_union_map_from_map(obj
.v
);
1669 obj
.type
= isl_obj_union_map
;
1670 } else if (obj
.type
== isl_obj_set
) {
1671 obj
.v
= isl_union_set_from_set(obj
.v
);
1672 obj
.type
= isl_obj_union_set
;
1673 } else if (obj
.type
== isl_obj_pw_qpolynomial
) {
1674 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1675 obj
.type
= isl_obj_union_pw_qpolynomial
;
1676 } else if (obj
.type
== isl_obj_pw_qpolynomial_fold
) {
1677 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1678 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1680 isl_assert(ctx
, 0, goto error
);
1683 obj
.type
->free(obj
.v
);
1684 obj
.type
= isl_obj_none
;
1688 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
1689 struct isl_obj obj1
, struct isl_obj obj2
)
1691 if (obj1
.type
== isl_obj_set
&& obj2
.type
== isl_obj_union_set
)
1692 obj1
= to_union(ctx
, obj1
);
1693 if (obj1
.type
== isl_obj_union_set
&& obj2
.type
== isl_obj_set
)
1694 obj2
= to_union(ctx
, obj2
);
1695 if (obj1
.type
== isl_obj_map
&& obj2
.type
== isl_obj_union_map
)
1696 obj1
= to_union(ctx
, obj1
);
1697 if (obj1
.type
== isl_obj_union_map
&& obj2
.type
== isl_obj_map
)
1698 obj2
= to_union(ctx
, obj2
);
1699 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
1700 obj2
.type
== isl_obj_union_pw_qpolynomial
)
1701 obj1
= to_union(ctx
, obj1
);
1702 if (obj1
.type
== isl_obj_union_pw_qpolynomial
&&
1703 obj2
.type
== isl_obj_pw_qpolynomial
)
1704 obj2
= to_union(ctx
, obj2
);
1705 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
1706 obj2
.type
== isl_obj_union_pw_qpolynomial_fold
)
1707 obj1
= to_union(ctx
, obj1
);
1708 if (obj1
.type
== isl_obj_union_pw_qpolynomial_fold
&&
1709 obj2
.type
== isl_obj_pw_qpolynomial_fold
)
1710 obj2
= to_union(ctx
, obj2
);
1711 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
1712 if (obj1
.type
== isl_obj_map
&& !isl_map_has_equal_dim(obj1
.v
, obj2
.v
)) {
1713 obj1
= to_union(ctx
, obj1
);
1714 obj2
= to_union(ctx
, obj2
);
1716 if (obj1
.type
== isl_obj_set
&& !isl_set_has_equal_dim(obj1
.v
, obj2
.v
)) {
1717 obj1
= to_union(ctx
, obj1
);
1718 obj2
= to_union(ctx
, obj2
);
1720 if (obj1
.type
== isl_obj_pw_qpolynomial
&&
1721 !isl_pw_qpolynomial_has_equal_dim(obj1
.v
, obj2
.v
)) {
1722 obj1
= to_union(ctx
, obj1
);
1723 obj2
= to_union(ctx
, obj2
);
1725 if (obj1
.type
== isl_obj_pw_qpolynomial_fold
&&
1726 !isl_pw_qpolynomial_fold_has_equal_dim(obj1
.v
, obj2
.v
)) {
1727 obj1
= to_union(ctx
, obj1
);
1728 obj2
= to_union(ctx
, obj2
);
1730 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
1733 obj1
.type
->free(obj1
.v
);
1734 obj2
.type
->free(obj2
.v
);
1735 obj1
.type
= isl_obj_none
;
1740 static struct isl_obj
obj_read(struct isl_stream
*s
, int nparam
)
1742 isl_basic_map
*bmap
= NULL
;
1743 struct isl_token
*tok
;
1744 struct vars
*v
= NULL
;
1745 struct isl_obj obj
= { isl_obj_set
, NULL
};
1747 tok
= isl_stream_next_token(s
);
1749 isl_stream_error(s
, NULL
, "unexpected EOF");
1752 if (tok
->type
== ISL_TOKEN_VALUE
) {
1753 struct isl_token
*tok2
;
1754 struct isl_map
*map
;
1756 tok2
= isl_stream_next_token(s
);
1757 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
||
1758 isl_int_is_neg(tok2
->u
.v
)) {
1760 isl_stream_push_token(s
, tok2
);
1761 obj
.type
= isl_obj_int
;
1762 obj
.v
= isl_int_obj_alloc(s
->ctx
, tok
->u
.v
);
1763 isl_token_free(tok
);
1766 isl_stream_push_token(s
, tok2
);
1767 isl_stream_push_token(s
, tok
);
1768 map
= map_read_polylib(s
, nparam
);
1771 if (isl_map_dim(map
, isl_dim_in
) > 0)
1772 obj
.type
= isl_obj_map
;
1776 v
= vars_new(s
->ctx
);
1778 isl_stream_push_token(s
, tok
);
1781 bmap
= isl_basic_map_alloc(s
->ctx
, 0, 0, 0, 0, 0, 0);
1782 if (tok
->type
== '[') {
1783 isl_stream_push_token(s
, tok
);
1784 bmap
= read_tuple(s
, bmap
, isl_dim_param
, v
);
1788 isl_assert(s
->ctx
, nparam
== v
->n
, goto error
);
1789 tok
= isl_stream_next_token(s
);
1790 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
1791 isl_stream_error(s
, tok
, "expecting '->'");
1793 isl_stream_push_token(s
, tok
);
1796 isl_token_free(tok
);
1797 tok
= isl_stream_next_token(s
);
1798 } else if (nparam
> 0)
1799 bmap
= isl_basic_map_add(bmap
, isl_dim_param
, nparam
);
1800 if (!tok
|| tok
->type
!= '{') {
1801 isl_stream_error(s
, tok
, "expecting '{'");
1803 isl_stream_push_token(s
, tok
);
1806 isl_token_free(tok
);
1808 tok
= isl_stream_next_token(s
);
1811 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
1812 isl_token_free(tok
);
1813 if (isl_stream_eat(s
, '='))
1815 bmap
= read_tuple(s
, bmap
, isl_dim_param
, v
);
1819 isl_assert(s
->ctx
, nparam
== v
->n
, goto error
);
1820 } else if (tok
->type
== '}') {
1821 obj
.type
= isl_obj_union_set
;
1822 obj
.v
= isl_union_set_empty(isl_basic_map_get_dim(bmap
));
1823 isl_token_free(tok
);
1826 isl_stream_push_token(s
, tok
);
1831 o
= obj_read_body(s
, isl_basic_map_copy(bmap
), v
);
1832 if (o
.type
== isl_obj_none
|| !o
.v
)
1837 obj
= obj_add(s
->ctx
, obj
, o
);
1838 if (obj
.type
== isl_obj_none
|| !obj
.v
)
1841 tok
= isl_stream_next_token(s
);
1842 if (!tok
|| tok
->type
!= ';')
1844 isl_token_free(tok
);
1845 if (isl_stream_next_token_is(s
, '}')) {
1846 tok
= isl_stream_next_token(s
);
1851 if (tok
&& tok
->type
== '}') {
1852 isl_token_free(tok
);
1854 isl_stream_error(s
, tok
, "unexpected isl_token");
1856 isl_token_free(tok
);
1861 isl_basic_map_free(bmap
);
1865 isl_basic_map_free(bmap
);
1866 obj
.type
->free(obj
.v
);
1873 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
1875 return obj_read(s
, -1);
1878 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
, int nparam
)
1881 struct isl_map
*map
;
1883 obj
= obj_read(s
, nparam
);
1885 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
1886 obj
.type
== isl_obj_set
, goto error
);
1890 obj
.type
->free(obj
.v
);
1894 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
, int nparam
)
1897 struct isl_set
*set
;
1899 obj
= obj_read(s
, nparam
);
1901 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
1905 obj
.type
->free(obj
.v
);
1909 __isl_give isl_union_map
*isl_stream_read_union_map(struct isl_stream
*s
)
1912 isl_union_map
*umap
;
1914 obj
= obj_read(s
, -1);
1915 if (obj
.type
== isl_obj_map
) {
1916 obj
.type
= isl_obj_union_map
;
1917 obj
.v
= isl_union_map_from_map(obj
.v
);
1919 if (obj
.type
== isl_obj_set
) {
1920 obj
.type
= isl_obj_union_set
;
1921 obj
.v
= isl_union_set_from_set(obj
.v
);
1924 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
||
1925 obj
.type
== isl_obj_union_set
, goto error
);
1929 obj
.type
->free(obj
.v
);
1933 __isl_give isl_union_set
*isl_stream_read_union_set(struct isl_stream
*s
)
1936 isl_union_set
*uset
;
1938 obj
= obj_read(s
, -1);
1939 if (obj
.type
== isl_obj_set
) {
1940 obj
.type
= isl_obj_union_set
;
1941 obj
.v
= isl_union_set_from_set(obj
.v
);
1944 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1948 obj
.type
->free(obj
.v
);
1952 static struct isl_basic_map
*basic_map_read(struct isl_stream
*s
, int nparam
)
1955 struct isl_map
*map
;
1956 struct isl_basic_map
*bmap
;
1958 obj
= obj_read(s
, nparam
);
1963 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
1966 bmap
= isl_basic_map_empty_like_map(map
);
1968 bmap
= isl_basic_map_copy(map
->p
[0]);
1978 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
1979 FILE *input
, int nparam
)
1981 struct isl_basic_map
*bmap
;
1982 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
1985 bmap
= basic_map_read(s
, nparam
);
1990 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
1991 FILE *input
, int nparam
)
1993 struct isl_basic_map
*bmap
;
1994 bmap
= isl_basic_map_read_from_file(ctx
, input
, nparam
);
1997 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
1998 return (struct isl_basic_set
*)bmap
;
2000 isl_basic_map_free(bmap
);
2004 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
2005 const char *str
, int nparam
)
2007 struct isl_basic_map
*bmap
;
2008 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2011 bmap
= basic_map_read(s
, nparam
);
2016 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
2017 const char *str
, int nparam
)
2019 struct isl_basic_map
*bmap
;
2020 bmap
= isl_basic_map_read_from_str(ctx
, str
, nparam
);
2023 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
2024 return (struct isl_basic_set
*)bmap
;
2026 isl_basic_map_free(bmap
);
2030 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
2031 FILE *input
, int nparam
)
2033 struct isl_map
*map
;
2034 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2037 map
= isl_stream_read_map(s
, nparam
);
2042 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
2043 const char *str
, int nparam
)
2045 struct isl_map
*map
;
2046 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2049 map
= isl_stream_read_map(s
, nparam
);
2054 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
2055 FILE *input
, int nparam
)
2057 struct isl_map
*map
;
2058 map
= isl_map_read_from_file(ctx
, input
, nparam
);
2061 isl_assert(ctx
, isl_map_n_in(map
) == 0, goto error
);
2062 return (struct isl_set
*)map
;
2068 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
2069 const char *str
, int nparam
)
2071 struct isl_map
*map
;
2072 map
= isl_map_read_from_str(ctx
, str
, nparam
);
2075 isl_assert(ctx
, isl_map_n_in(map
) == 0, goto error
);
2076 return (struct isl_set
*)map
;
2082 __isl_give isl_union_map
*isl_union_map_read_from_str(struct isl_ctx
*ctx
,
2085 isl_union_map
*umap
;
2086 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2089 umap
= isl_stream_read_union_map(s
);
2094 __isl_give isl_union_set
*isl_union_set_read_from_str(struct isl_ctx
*ctx
,
2097 isl_union_set
*uset
;
2098 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2101 uset
= isl_stream_read_union_set(s
);
2106 static __isl_give isl_vec
*isl_vec_read_polylib(struct isl_stream
*s
)
2108 struct isl_vec
*vec
= NULL
;
2109 struct isl_token
*tok
;
2113 tok
= isl_stream_next_token(s
);
2114 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2115 isl_stream_error(s
, tok
, "expecting vector length");
2119 size
= isl_int_get_si(tok
->u
.v
);
2120 isl_token_free(tok
);
2122 vec
= isl_vec_alloc(s
->ctx
, size
);
2124 for (j
= 0; j
< size
; ++j
) {
2125 tok
= isl_stream_next_token(s
);
2126 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
2127 isl_stream_error(s
, tok
, "expecting constant value");
2130 isl_int_set(vec
->el
[j
], tok
->u
.v
);
2131 isl_token_free(tok
);
2136 isl_token_free(tok
);
2141 static __isl_give isl_vec
*vec_read(struct isl_stream
*s
,
2142 unsigned input_format
)
2144 if (input_format
== ISL_FORMAT_POLYLIB
)
2145 return isl_vec_read_polylib(s
);
2146 isl_assert(s
->ctx
, 0, return NULL
);
2149 struct isl_vec
*isl_vec_read_from_file(struct isl_ctx
*ctx
,
2150 FILE *input
, unsigned input_format
)
2153 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
2156 v
= vec_read(s
, input_format
);
2161 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
2162 struct isl_stream
*s
)
2165 struct isl_pw_qpolynomial
*pwqp
;
2167 obj
= obj_read(s
, -1);
2169 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
2174 obj
.type
->free(obj
.v
);
2178 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_read_from_str(isl_ctx
*ctx
,
2181 isl_pw_qpolynomial
*pwqp
;
2182 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
2185 pwqp
= isl_stream_read_pw_qpolynomial(s
);