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
20 #include "isl_stream.h"
21 #include "isl_map_private.h"
23 #include "isl_polynomial_private.h"
28 struct variable
*next
;
37 static struct vars
*vars_new(struct isl_ctx
*ctx
)
40 v
= isl_alloc_type(ctx
, struct vars
);
49 static void variable_free(struct variable
*var
)
52 struct variable
*next
= var
->next
;
59 static void vars_free(struct vars
*v
)
67 static void vars_drop(struct vars
*v
, int n
)
78 struct variable
*next
= var
->next
;
86 static struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
90 var
= isl_alloc_type(v
->ctx
, struct variable
);
93 var
->name
= strdup(name
);
94 var
->name
[len
] = '\0';
103 static int vars_pos(struct vars
*v
, const char *s
, int len
)
110 for (q
= v
->v
; q
; q
= q
->next
) {
111 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
118 v
->v
= variable_new(v
, s
, len
, v
->n
);
126 static __isl_give isl_basic_map
*set_name(__isl_take isl_basic_map
*bmap
,
127 enum isl_dim_type type
, unsigned pos
, char *name
)
136 prime
= strchr(name
, '\'');
139 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, name
);
148 isl_basic_map_free(bmap
);
152 static int accept_cst_factor(struct isl_stream
*s
, isl_int
*f
)
154 struct isl_token
*tok
;
156 tok
= isl_stream_next_token(s
);
157 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
158 isl_stream_error(s
, tok
, "expecting constant value");
162 isl_int_mul(*f
, *f
, tok
->u
.v
);
166 if (isl_stream_eat_if_available(s
, '*'))
167 return accept_cst_factor(s
, f
);
175 static struct isl_vec
*accept_affine(struct isl_stream
*s
, struct vars
*v
);
177 static __isl_give isl_vec
*accept_affine_factor(struct isl_stream
*s
,
180 struct isl_token
*tok
= NULL
;
183 tok
= isl_stream_next_token(s
);
185 isl_stream_error(s
, NULL
, "unexpected EOF");
188 if (tok
->type
== ISL_TOKEN_IDENT
) {
190 int pos
= vars_pos(v
, tok
->u
.s
, -1);
194 isl_stream_error(s
, tok
, "unknown identifier");
198 aff
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
201 isl_seq_clr(aff
->el
, aff
->size
);
202 isl_int_set_si(aff
->el
[1 + pos
], 1);
204 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
205 if (isl_stream_eat_if_available(s
, '*')) {
206 aff
= accept_affine_factor(s
, v
);
207 aff
= isl_vec_scale(aff
, tok
->u
.v
);
209 aff
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
212 isl_seq_clr(aff
->el
, aff
->size
);
213 isl_int_set(aff
->el
[0], tok
->u
.v
);
216 } else if (tok
->type
== '(') {
219 aff
= accept_affine(s
, v
);
222 if (isl_stream_eat(s
, ')'))
225 isl_stream_error(s
, tok
, "expecting factor");
228 if (isl_stream_eat_if_available(s
, '*')) {
231 isl_int_set_si(f
, 1);
232 if (accept_cst_factor(s
, &f
) < 0) {
236 aff
= isl_vec_scale(aff
, f
);
247 static struct isl_vec
*accept_affine(struct isl_stream
*s
, struct vars
*v
)
249 struct isl_token
*tok
= NULL
;
253 aff
= isl_vec_alloc(v
->ctx
, 1 + v
->n
);
256 isl_seq_clr(aff
->el
, aff
->size
);
259 tok
= isl_stream_next_token(s
);
261 isl_stream_error(s
, NULL
, "unexpected EOF");
264 if (tok
->type
== '-') {
269 if (tok
->type
== '(' || tok
->type
== ISL_TOKEN_IDENT
) {
271 isl_stream_push_token(s
, tok
);
273 aff2
= accept_affine_factor(s
, v
);
275 aff2
= isl_vec_scale(aff2
, s
->ctx
->negone
);
276 aff
= isl_vec_add(aff
, aff2
);
280 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
282 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
283 if (isl_stream_eat_if_available(s
, '*') ||
284 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
286 aff2
= accept_affine_factor(s
, v
);
287 aff2
= isl_vec_scale(aff2
, tok
->u
.v
);
288 aff
= isl_vec_add(aff
, aff2
);
292 isl_int_add(aff
->el
[0], aff
->el
[0], tok
->u
.v
);
298 tok
= isl_stream_next_token(s
);
299 if (tok
&& tok
->type
== '-') {
302 } else if (tok
&& tok
->type
== '+') {
305 } else if (tok
&& tok
->type
== ISL_TOKEN_VALUE
&&
306 isl_int_is_neg(tok
->u
.v
)) {
307 isl_stream_push_token(s
, tok
);
310 isl_stream_push_token(s
, tok
);
322 static __isl_give isl_basic_map
*read_var_list(struct isl_stream
*s
,
323 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, struct vars
*v
)
326 struct isl_token
*tok
;
328 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
331 if (tok
->type
== ISL_TOKEN_IDENT
) {
333 int p
= vars_pos(v
, tok
->u
.s
, -1);
340 bmap
= isl_basic_map_add(bmap
, type
, 1);
341 bmap
= set_name(bmap
, type
, i
, v
->v
->name
);
343 } else if (tok
->type
== ISL_TOKEN_IDENT
||
344 tok
->type
== ISL_TOKEN_VALUE
) {
347 if (type
== isl_dim_param
) {
348 isl_stream_error(s
, tok
,
349 "expecting unique identifier");
352 isl_stream_push_token(s
, tok
);
354 vec
= accept_affine(s
, v
);
357 v
->v
= variable_new(v
, "", 0, v
->n
);
361 bmap
= isl_basic_map_add(bmap
, type
, 1);
362 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
363 k
= isl_basic_map_alloc_equality(bmap
);
365 isl_seq_cpy(bmap
->eq
[k
], vec
->el
, vec
->size
);
366 isl_int_set_si(bmap
->eq
[k
][vec
->size
], -1);
374 tok
= isl_stream_next_token(s
);
375 if (!tok
|| tok
->type
!= ',')
382 isl_stream_push_token(s
, tok
);
387 isl_basic_map_free(bmap
);
391 static __isl_give isl_mat
*accept_affine_list(struct isl_stream
*s
,
396 struct isl_token
*tok
= NULL
;
398 vec
= accept_affine(s
, v
);
399 mat
= isl_mat_from_row_vec(vec
);
404 tok
= isl_stream_next_token(s
);
406 isl_stream_error(s
, NULL
, "unexpected EOF");
409 if (tok
->type
!= ',') {
410 isl_stream_push_token(s
, tok
);
415 vec
= accept_affine(s
, v
);
416 mat
= isl_mat_vec_concat(mat
, vec
);
427 static struct isl_basic_map
*add_div_definition(struct isl_stream
*s
,
428 struct vars
*v
, struct isl_basic_map
*bmap
, int k
)
430 struct isl_token
*tok
;
434 if (isl_stream_eat(s
, '['))
437 tok
= isl_stream_next_token(s
);
440 if (tok
->type
== '(') {
444 isl_stream_push_token(s
, tok
);
446 aff
= accept_affine(s
, v
);
450 isl_seq_cpy(bmap
->div
[k
] + 1, aff
->el
, aff
->size
);
454 if (seen_paren
&& isl_stream_eat(s
, ')'))
456 if (isl_stream_eat(s
, '/'))
459 tok
= isl_stream_next_token(s
);
462 if (tok
->type
!= ISL_TOKEN_VALUE
) {
463 isl_stream_error(s
, tok
, "expected denominator");
464 isl_stream_push_token(s
, tok
);
467 isl_int_set(bmap
->div
[k
][0], tok
->u
.v
);
470 if (isl_stream_eat(s
, ']'))
473 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
478 isl_basic_map_free(bmap
);
482 static struct isl_basic_map
*read_defined_var_list(struct isl_stream
*s
,
483 struct vars
*v
, struct isl_basic_map
*bmap
)
485 struct isl_token
*tok
;
487 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
491 unsigned total
= isl_basic_map_total_dim(bmap
);
493 if (tok
->type
!= ISL_TOKEN_IDENT
)
496 p
= vars_pos(v
, tok
->u
.s
, -1);
500 isl_stream_error(s
, tok
, "expecting unique identifier");
504 bmap
= isl_basic_map_cow(bmap
);
505 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
508 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
510 isl_seq_clr(bmap
->div
[k
], 1 + 1 + total
);
513 tok
= isl_stream_next_token(s
);
514 if (tok
&& tok
->type
== '=') {
516 bmap
= add_div_definition(s
, v
, bmap
, k
);
517 tok
= isl_stream_next_token(s
);
520 if (!tok
|| tok
->type
!= ',')
526 isl_stream_push_token(s
, tok
);
531 isl_basic_map_free(bmap
);
535 static __isl_give isl_basic_map
*read_tuple(struct isl_stream
*s
,
536 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, struct vars
*v
)
538 struct isl_token
*tok
;
540 tok
= isl_stream_next_token(s
);
541 if (!tok
|| tok
->type
!= '[') {
542 isl_stream_error(s
, tok
, "expecting '['");
546 bmap
= read_var_list(s
, bmap
, type
, v
);
547 tok
= isl_stream_next_token(s
);
548 if (!tok
|| tok
->type
!= ']') {
549 isl_stream_error(s
, tok
, "expecting ']'");
558 isl_basic_map_free(bmap
);
562 static struct isl_basic_map
*add_constraints(struct isl_stream
*s
,
563 struct vars
*v
, struct isl_basic_map
*bmap
);
565 static struct isl_basic_map
*add_exists(struct isl_stream
*s
,
566 struct vars
*v
, struct isl_basic_map
*bmap
)
568 struct isl_token
*tok
;
575 tok
= isl_stream_next_token(s
);
578 if (tok
->type
== '(') {
582 isl_stream_push_token(s
, tok
);
584 bmap
= read_defined_var_list(s
, v
, bmap
);
588 if (isl_stream_eat(s
, ':'))
590 bmap
= add_constraints(s
, v
, bmap
);
591 if (seen_paren
&& isl_stream_eat(s
, ')'))
595 isl_basic_map_free(bmap
);
599 static __isl_give isl_basic_map
*construct_constraint(
600 __isl_take isl_basic_map
*bmap
, enum isl_token_type type
,
601 isl_int
*left
, isl_int
*right
)
609 len
= 1 + isl_basic_map_total_dim(bmap
);
612 k
= isl_basic_map_alloc_inequality(bmap
);
615 if (type
== ISL_TOKEN_LE
)
616 isl_seq_combine(bmap
->ineq
[k
], ctx
->negone
, left
,
619 else if (type
== ISL_TOKEN_GE
)
620 isl_seq_combine(bmap
->ineq
[k
], ctx
->one
, left
,
623 else if (type
== ISL_TOKEN_LT
) {
624 isl_seq_combine(bmap
->ineq
[k
], ctx
->negone
, left
,
627 isl_int_sub_ui(bmap
->ineq
[k
][0], bmap
->ineq
[k
][0], 1);
628 } else if (type
== ISL_TOKEN_GT
) {
629 isl_seq_combine(bmap
->ineq
[k
], ctx
->one
, left
,
632 isl_int_sub_ui(bmap
->ineq
[k
][0], bmap
->ineq
[k
][0], 1);
634 isl_seq_combine(bmap
->ineq
[k
], ctx
->one
, left
,
637 isl_basic_map_inequality_to_equality(bmap
, k
);
642 isl_basic_map_free(bmap
);
646 static int is_comparator(struct isl_token
*tok
)
663 static struct isl_basic_map
*add_constraint(struct isl_stream
*s
,
664 struct vars
*v
, struct isl_basic_map
*bmap
)
667 unsigned total
= isl_basic_map_total_dim(bmap
);
668 struct isl_token
*tok
= NULL
;
669 struct isl_mat
*aff1
= NULL
, *aff2
= NULL
;
671 tok
= isl_stream_next_token(s
);
674 if (tok
->type
== ISL_TOKEN_EXISTS
) {
676 return add_exists(s
, v
, bmap
);
678 isl_stream_push_token(s
, tok
);
681 bmap
= isl_basic_map_cow(bmap
);
683 aff1
= accept_affine_list(s
, v
);
686 tok
= isl_stream_next_token(s
);
687 if (!is_comparator(tok
)) {
688 isl_stream_error(s
, tok
, "missing operator");
690 isl_stream_push_token(s
, tok
);
694 isl_assert(aff1
->ctx
, aff1
->n_col
== 1 + total
, goto error
);
696 aff2
= accept_affine_list(s
, v
);
699 isl_assert(aff2
->ctx
, aff2
->n_col
== 1 + total
, goto error
);
701 bmap
= isl_basic_map_extend_constraints(bmap
, 0,
702 aff1
->n_row
* aff2
->n_row
);
703 for (i
= 0; i
< aff1
->n_row
; ++i
)
704 for (j
= 0; j
< aff2
->n_row
; ++j
)
705 bmap
= construct_constraint(bmap
, tok
->type
,
706 aff1
->row
[i
], aff2
->row
[j
]);
711 tok
= isl_stream_next_token(s
);
712 if (!is_comparator(tok
)) {
714 isl_stream_push_token(s
, tok
);
726 isl_basic_map_free(bmap
);
730 static struct isl_basic_map
*add_constraints(struct isl_stream
*s
,
731 struct vars
*v
, struct isl_basic_map
*bmap
)
733 struct isl_token
*tok
;
736 bmap
= add_constraint(s
, v
, bmap
);
739 tok
= isl_stream_next_token(s
);
741 isl_stream_error(s
, NULL
, "unexpected EOF");
744 if (tok
->type
!= ISL_TOKEN_AND
)
748 isl_stream_push_token(s
, tok
);
754 isl_basic_map_free(bmap
);
758 static struct isl_basic_map
*read_disjunct(struct isl_stream
*s
,
759 struct vars
*v
, __isl_take isl_dim
*dim
)
762 struct isl_token
*tok
;
763 struct isl_basic_map
*bmap
;
765 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
769 tok
= isl_stream_next_token(s
);
772 if (tok
->type
== '(') {
776 isl_stream_push_token(s
, tok
);
778 bmap
= add_constraints(s
, v
, bmap
);
779 bmap
= isl_basic_map_simplify(bmap
);
780 bmap
= isl_basic_map_finalize(bmap
);
782 if (seen_paren
&& isl_stream_eat(s
, ')'))
787 isl_basic_map_free(bmap
);
791 static struct isl_map
*read_disjuncts(struct isl_stream
*s
,
792 struct vars
*v
, __isl_take isl_dim
*dim
)
794 struct isl_token
*tok
;
797 tok
= isl_stream_next_token(s
);
799 isl_stream_error(s
, NULL
, "unexpected EOF");
802 if (tok
->type
== '}') {
803 isl_stream_push_token(s
, tok
);
804 return isl_map_universe(dim
);
806 isl_stream_push_token(s
, tok
);
808 map
= isl_map_empty(isl_dim_copy(dim
));
810 struct isl_basic_map
*bmap
;
813 bmap
= read_disjunct(s
, v
, isl_dim_copy(dim
));
814 map
= isl_map_union(map
, isl_map_from_basic_map(bmap
));
816 vars_drop(v
, v
->n
- n
);
818 tok
= isl_stream_next_token(s
);
819 if (!tok
|| tok
->type
!= ISL_TOKEN_OR
)
824 isl_stream_push_token(s
, tok
);
833 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map
*bmap
, int pos
)
835 if (pos
< isl_basic_map_dim(bmap
, isl_dim_out
))
836 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
837 isl_basic_map_dim(bmap
, isl_dim_in
) + pos
;
838 pos
-= isl_basic_map_dim(bmap
, isl_dim_out
);
840 if (pos
< isl_basic_map_dim(bmap
, isl_dim_in
))
841 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) + pos
;
842 pos
-= isl_basic_map_dim(bmap
, isl_dim_in
);
844 if (pos
< isl_basic_map_dim(bmap
, isl_dim_div
))
845 return 1 + isl_basic_map_dim(bmap
, isl_dim_param
) +
846 isl_basic_map_dim(bmap
, isl_dim_in
) +
847 isl_basic_map_dim(bmap
, isl_dim_out
) + pos
;
848 pos
-= isl_basic_map_dim(bmap
, isl_dim_div
);
850 if (pos
< isl_basic_map_dim(bmap
, isl_dim_param
))
856 static __isl_give isl_basic_map
*basic_map_read_polylib_constraint(
857 struct isl_stream
*s
, __isl_take isl_basic_map
*bmap
)
860 struct isl_token
*tok
;
870 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
871 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
873 tok
= isl_stream_next_token(s
);
874 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
875 isl_stream_error(s
, tok
, "expecting coefficient");
877 isl_stream_push_token(s
, tok
);
880 if (!tok
->on_new_line
) {
881 isl_stream_error(s
, tok
, "coefficient should appear on new line");
882 isl_stream_push_token(s
, tok
);
886 type
= isl_int_get_si(tok
->u
.v
);
889 isl_assert(s
->ctx
, type
== 0 || type
== 1, goto error
);
891 k
= isl_basic_map_alloc_equality(bmap
);
894 k
= isl_basic_map_alloc_inequality(bmap
);
900 for (j
= 0; j
< 1 + isl_basic_map_total_dim(bmap
); ++j
) {
902 tok
= isl_stream_next_token(s
);
903 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
904 isl_stream_error(s
, tok
, "expecting coefficient");
906 isl_stream_push_token(s
, tok
);
909 if (tok
->on_new_line
) {
910 isl_stream_error(s
, tok
,
911 "coefficient should not appear on new line");
912 isl_stream_push_token(s
, tok
);
915 pos
= polylib_pos_to_isl_pos(bmap
, j
);
916 isl_int_set(c
[pos
], tok
->u
.v
);
922 isl_basic_map_free(bmap
);
926 static __isl_give isl_basic_map
*basic_map_read_polylib(struct isl_stream
*s
,
930 struct isl_token
*tok
;
931 struct isl_token
*tok2
;
934 unsigned in
= 0, out
, local
= 0;
935 struct isl_basic_map
*bmap
= NULL
;
940 tok
= isl_stream_next_token(s
);
942 isl_stream_error(s
, NULL
, "unexpected EOF");
945 tok2
= isl_stream_next_token(s
);
948 isl_stream_error(s
, NULL
, "unexpected EOF");
951 n_row
= isl_int_get_si(tok
->u
.v
);
952 n_col
= isl_int_get_si(tok2
->u
.v
);
953 on_new_line
= tok2
->on_new_line
;
954 isl_token_free(tok2
);
956 isl_assert(s
->ctx
, !on_new_line
, return NULL
);
957 isl_assert(s
->ctx
, n_row
>= 0, return NULL
);
958 isl_assert(s
->ctx
, n_col
>= 2 + nparam
, return NULL
);
959 tok
= isl_stream_next_token_on_same_line(s
);
961 if (tok
->type
!= ISL_TOKEN_VALUE
) {
962 isl_stream_error(s
, tok
,
963 "expecting number of output dimensions");
964 isl_stream_push_token(s
, tok
);
967 out
= isl_int_get_si(tok
->u
.v
);
970 tok
= isl_stream_next_token_on_same_line(s
);
971 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
972 isl_stream_error(s
, tok
,
973 "expecting number of input dimensions");
975 isl_stream_push_token(s
, tok
);
978 in
= isl_int_get_si(tok
->u
.v
);
981 tok
= isl_stream_next_token_on_same_line(s
);
982 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
983 isl_stream_error(s
, tok
,
984 "expecting number of existentials");
986 isl_stream_push_token(s
, tok
);
989 local
= isl_int_get_si(tok
->u
.v
);
992 tok
= isl_stream_next_token_on_same_line(s
);
993 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
994 isl_stream_error(s
, tok
,
995 "expecting number of parameters");
997 isl_stream_push_token(s
, tok
);
1000 nparam
= isl_int_get_si(tok
->u
.v
);
1001 isl_token_free(tok
);
1002 if (n_col
!= 1 + out
+ in
+ local
+ nparam
+ 1) {
1003 isl_stream_error(s
, NULL
,
1004 "dimensions don't match");
1008 out
= n_col
- 2 - nparam
;
1009 bmap
= isl_basic_map_alloc(s
->ctx
, nparam
, in
, out
, local
, n_row
, n_row
);
1013 for (i
= 0; i
< local
; ++i
) {
1014 int k
= isl_basic_map_alloc_div(bmap
);
1019 for (i
= 0; i
< n_row
; ++i
)
1020 bmap
= basic_map_read_polylib_constraint(s
, bmap
);
1022 tok
= isl_stream_next_token_on_same_line(s
);
1024 isl_stream_error(s
, tok
, "unexpected extra token on line");
1025 isl_stream_push_token(s
, tok
);
1029 bmap
= isl_basic_map_simplify(bmap
);
1030 bmap
= isl_basic_map_finalize(bmap
);
1033 isl_basic_map_free(bmap
);
1037 static struct isl_map
*map_read_polylib(struct isl_stream
*s
, int nparam
)
1039 struct isl_token
*tok
;
1040 struct isl_token
*tok2
;
1042 struct isl_map
*map
;
1044 tok
= isl_stream_next_token(s
);
1046 isl_stream_error(s
, NULL
, "unexpected EOF");
1049 tok2
= isl_stream_next_token_on_same_line(s
);
1051 isl_stream_push_token(s
, tok2
);
1052 isl_stream_push_token(s
, tok
);
1053 return isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
));
1055 n
= isl_int_get_si(tok
->u
.v
);
1056 isl_token_free(tok
);
1058 isl_assert(s
->ctx
, n
>= 1, return NULL
);
1060 map
= isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
));
1062 for (i
= 1; i
< n
; ++i
)
1063 map
= isl_map_union(map
,
1064 isl_map_from_basic_map(basic_map_read_polylib(s
, nparam
)));
1069 static int optional_power(struct isl_stream
*s
)
1072 struct isl_token
*tok
;
1074 tok
= isl_stream_next_token(s
);
1077 if (tok
->type
!= '^') {
1078 isl_stream_push_token(s
, tok
);
1081 isl_token_free(tok
);
1082 tok
= isl_stream_next_token(s
);
1083 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1084 isl_stream_error(s
, tok
, "expecting exponent");
1086 isl_stream_push_token(s
, tok
);
1089 pow
= isl_int_get_si(tok
->u
.v
);
1090 isl_token_free(tok
);
1094 static __isl_give isl_div
*read_div(struct isl_stream
*s
,
1095 __isl_keep isl_basic_map
*bmap
, struct vars
*v
)
1097 unsigned total
= isl_basic_map_total_dim(bmap
);
1100 bmap
= isl_basic_map_copy(bmap
);
1101 bmap
= isl_basic_map_cow(bmap
);
1102 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1105 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
1107 isl_seq_clr(bmap
->div
[k
], 1 + 1 + total
);
1108 bmap
= add_div_definition(s
, v
, bmap
, k
);
1110 return isl_basic_map_div(bmap
, k
);
1112 isl_basic_map_free(bmap
);
1116 static __isl_give isl_qpolynomial
*read_term(struct isl_stream
*s
,
1117 __isl_keep isl_basic_map
*bmap
, struct vars
*v
);
1119 static __isl_give isl_qpolynomial
*read_factor(struct isl_stream
*s
,
1120 __isl_keep isl_basic_map
*bmap
, struct vars
*v
)
1122 struct isl_qpolynomial
*qp
;
1123 struct isl_token
*tok
;
1125 tok
= isl_stream_next_token(s
);
1127 isl_stream_error(s
, NULL
, "unexpected EOF");
1130 if (tok
->type
== '(') {
1131 isl_token_free(tok
);
1132 qp
= read_term(s
, bmap
, v
);
1135 if (isl_stream_eat(s
, ')'))
1137 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
1138 struct isl_token
*tok2
;
1139 tok2
= isl_stream_next_token(s
);
1140 if (tok2
&& tok2
->type
== '/') {
1141 isl_token_free(tok2
);
1142 tok2
= isl_stream_next_token(s
);
1143 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1144 isl_stream_error(s
, tok2
, "expected denominator");
1145 isl_token_free(tok
);
1146 isl_token_free(tok2
);
1149 qp
= isl_qpolynomial_rat_cst(isl_basic_map_get_dim(bmap
),
1150 tok
->u
.v
, tok2
->u
.v
);
1151 isl_token_free(tok2
);
1153 isl_stream_push_token(s
, tok2
);
1154 qp
= isl_qpolynomial_cst(isl_basic_map_get_dim(bmap
),
1157 isl_token_free(tok
);
1158 } else if (tok
->type
== ISL_TOKEN_INFTY
) {
1159 isl_token_free(tok
);
1160 qp
= isl_qpolynomial_infty(isl_basic_map_get_dim(bmap
));
1161 } else if (tok
->type
== ISL_TOKEN_NAN
) {
1162 isl_token_free(tok
);
1163 qp
= isl_qpolynomial_nan(isl_basic_map_get_dim(bmap
));
1164 } else if (tok
->type
== ISL_TOKEN_IDENT
) {
1166 int pos
= vars_pos(v
, tok
->u
.s
, -1);
1168 isl_token_free(tok
);
1172 isl_stream_error(s
, tok
, "unknown identifier");
1175 pow
= optional_power(s
);
1176 qp
= isl_qpolynomial_pow(isl_basic_map_get_dim(bmap
), pos
, pow
);
1177 } else if (tok
->type
== '[') {
1181 isl_stream_push_token(s
, tok
);
1182 div
= read_div(s
, bmap
, v
);
1183 pow
= optional_power(s
);
1184 qp
= isl_qpolynomial_div_pow(div
, pow
);
1185 } else if (tok
->type
== '-') {
1186 struct isl_qpolynomial
*qp2
;
1188 isl_token_free(tok
);
1189 qp
= isl_qpolynomial_cst(isl_basic_map_get_dim(bmap
),
1191 qp2
= read_factor(s
, bmap
, v
);
1192 qp
= isl_qpolynomial_mul(qp
, qp2
);
1194 isl_stream_error(s
, tok
, "unexpected isl_token");
1195 isl_stream_push_token(s
, tok
);
1199 if (isl_stream_eat_if_available(s
, '*') ||
1200 isl_stream_next_token_is(s
, ISL_TOKEN_IDENT
)) {
1201 struct isl_qpolynomial
*qp2
;
1203 qp2
= read_factor(s
, bmap
, v
);
1204 qp
= isl_qpolynomial_mul(qp
, qp2
);
1209 isl_qpolynomial_free(qp
);
1213 static __isl_give isl_qpolynomial
*read_term(struct isl_stream
*s
,
1214 __isl_keep isl_basic_map
*bmap
, struct vars
*v
)
1216 struct isl_token
*tok
;
1217 struct isl_qpolynomial
*qp
;
1219 qp
= read_factor(s
, bmap
, v
);
1222 tok
= isl_stream_next_token(s
);
1226 if (tok
->type
== '+') {
1227 struct isl_qpolynomial
*qp2
;
1229 isl_token_free(tok
);
1230 qp2
= read_factor(s
, bmap
, v
);
1231 qp
= isl_qpolynomial_add(qp
, qp2
);
1232 } else if (tok
->type
== '-') {
1233 struct isl_qpolynomial
*qp2
;
1235 isl_token_free(tok
);
1236 qp2
= read_factor(s
, bmap
, v
);
1237 qp
= isl_qpolynomial_sub(qp
, qp2
);
1238 } else if (tok
->type
== ISL_TOKEN_VALUE
&&
1239 isl_int_is_neg(tok
->u
.v
)) {
1240 struct isl_qpolynomial
*qp2
;
1242 qp2
= isl_qpolynomial_cst(isl_basic_map_get_dim(bmap
),
1244 isl_token_free(tok
);
1245 qp
= isl_qpolynomial_add(qp
, qp2
);
1247 isl_stream_push_token(s
, tok
);
1255 static __isl_give isl_map
*read_optional_disjuncts(struct isl_stream
*s
,
1256 __isl_take isl_basic_map
*bmap
, struct vars
*v
)
1258 struct isl_token
*tok
;
1259 struct isl_map
*map
;
1261 tok
= isl_stream_next_token(s
);
1263 isl_stream_error(s
, NULL
, "unexpected EOF");
1266 map
= isl_map_from_basic_map(isl_basic_map_copy(bmap
));
1267 if (tok
->type
== ':') {
1268 isl_token_free(tok
);
1269 map
= isl_map_intersect(map
,
1270 read_disjuncts(s
, v
, isl_basic_map_get_dim(bmap
)));
1272 isl_stream_push_token(s
, tok
);
1274 isl_basic_map_free(bmap
);
1278 isl_basic_map_free(bmap
);
1282 static struct isl_obj
obj_read_poly(struct isl_stream
*s
,
1283 __isl_take isl_basic_map
*bmap
, struct vars
*v
, int n
)
1285 struct isl_obj obj
= { isl_obj_pw_qpolynomial
, NULL
};
1286 struct isl_pw_qpolynomial
*pwqp
;
1287 struct isl_qpolynomial
*qp
;
1288 struct isl_map
*map
;
1289 struct isl_set
*set
;
1291 bmap
= isl_basic_map_reverse(bmap
);
1293 qp
= read_term(s
, bmap
, v
);
1294 map
= read_optional_disjuncts(s
, bmap
, v
);
1295 set
= isl_set_from_map(map
);
1297 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
1299 vars_drop(v
, v
->n
- n
);
1305 static struct isl_obj
obj_read_body(struct isl_stream
*s
,
1306 __isl_take isl_basic_map
*bmap
, struct vars
*v
, isl_obj_type type
)
1308 struct isl_map
*map
= NULL
;
1309 struct isl_token
*tok
;
1310 struct isl_obj obj
= { type
, NULL
};
1313 if (!isl_stream_next_token_is(s
, '['))
1314 return obj_read_poly(s
, bmap
, v
, n
);
1316 bmap
= read_tuple(s
, bmap
, isl_dim_in
, v
);
1319 tok
= isl_stream_next_token(s
);
1320 if (tok
&& tok
->type
== ISL_TOKEN_TO
) {
1321 obj
.type
= isl_obj_map
;
1322 isl_token_free(tok
);
1323 tok
= isl_stream_next_token(s
);
1324 if (tok
&& tok
->type
!= '[') {
1325 isl_stream_push_token(s
, tok
);
1326 return obj_read_poly(s
, bmap
, v
, n
);
1328 isl_stream_push_token(s
, tok
);
1329 bmap
= read_tuple(s
, bmap
, isl_dim_out
, v
);
1333 bmap
= isl_basic_map_reverse(bmap
);
1335 isl_stream_push_token(s
, tok
);
1338 map
= read_optional_disjuncts(s
, bmap
, v
);
1340 vars_drop(v
, v
->n
- n
);
1345 isl_basic_map_free(bmap
);
1346 obj
.type
= isl_obj_none
;
1350 static struct isl_obj
obj_add(struct isl_ctx
*ctx
,
1351 struct isl_obj obj1
, struct isl_obj obj2
)
1353 isl_assert(ctx
, obj1
.type
== obj2
.type
, goto error
);
1354 obj1
.v
= obj1
.type
->add(obj1
.v
, obj2
.v
);
1357 obj1
.type
->free(obj1
.v
);
1358 obj2
.type
->free(obj2
.v
);
1359 obj1
.type
= isl_obj_none
;
1364 static struct isl_obj
obj_read(struct isl_stream
*s
, int nparam
)
1366 struct isl_basic_map
*bmap
= NULL
;
1367 struct isl_token
*tok
;
1368 struct vars
*v
= NULL
;
1369 struct isl_obj obj
= { isl_obj_set
, NULL
};
1371 tok
= isl_stream_next_token(s
);
1373 isl_stream_error(s
, NULL
, "unexpected EOF");
1376 if (tok
->type
== ISL_TOKEN_VALUE
) {
1377 struct isl_map
*map
;
1378 isl_stream_push_token(s
, tok
);
1379 map
= map_read_polylib(s
, nparam
);
1382 if (isl_map_dim(map
, isl_dim_in
) > 0)
1383 obj
.type
= isl_obj_map
;
1387 v
= vars_new(s
->ctx
);
1388 bmap
= isl_basic_map_alloc(s
->ctx
, 0, 0, 0, 0, 0, 0);
1389 if (tok
->type
== '[') {
1390 isl_stream_push_token(s
, tok
);
1391 bmap
= read_tuple(s
, bmap
, isl_dim_param
, v
);
1395 isl_assert(s
->ctx
, nparam
== v
->n
, goto error
);
1396 tok
= isl_stream_next_token(s
);
1397 if (!tok
|| tok
->type
!= ISL_TOKEN_TO
) {
1398 isl_stream_error(s
, tok
, "expecting '->'");
1400 isl_stream_push_token(s
, tok
);
1403 isl_token_free(tok
);
1404 tok
= isl_stream_next_token(s
);
1405 } else if (nparam
> 0)
1406 bmap
= isl_basic_map_add(bmap
, isl_dim_param
, nparam
);
1407 if (!tok
|| tok
->type
!= '{') {
1408 isl_stream_error(s
, tok
, "expecting '{'");
1410 isl_stream_push_token(s
, tok
);
1413 isl_token_free(tok
);
1415 tok
= isl_stream_next_token(s
);
1418 else if (tok
->type
== ISL_TOKEN_IDENT
&& !strcmp(tok
->u
.s
, "Sym")) {
1419 isl_token_free(tok
);
1420 if (isl_stream_eat(s
, '='))
1422 bmap
= read_tuple(s
, bmap
, isl_dim_param
, v
);
1426 isl_assert(s
->ctx
, nparam
== v
->n
, goto error
);
1428 isl_stream_push_token(s
, tok
);
1433 o
= obj_read_body(s
, isl_basic_map_copy(bmap
), v
, obj
.type
);
1434 if (o
.type
== isl_obj_none
)
1439 obj
= obj_add(s
->ctx
, obj
, o
);
1440 if (obj
.type
== isl_obj_none
)
1443 tok
= isl_stream_next_token(s
);
1444 if (!tok
|| tok
->type
!= ';')
1446 isl_token_free(tok
);
1449 if (tok
&& tok
->type
== '}') {
1450 isl_token_free(tok
);
1452 isl_stream_error(s
, tok
, "unexpected isl_token");
1454 isl_token_free(tok
);
1458 isl_basic_map_free(bmap
);
1462 isl_basic_map_free(bmap
);
1463 obj
.type
->free(obj
.v
);
1470 struct isl_obj
isl_stream_read_obj(struct isl_stream
*s
)
1472 return obj_read(s
, -1);
1475 __isl_give isl_map
*isl_stream_read_map(struct isl_stream
*s
, int nparam
)
1478 struct isl_map
*map
;
1480 obj
= obj_read(s
, nparam
);
1482 isl_assert(s
->ctx
, obj
.type
== isl_obj_map
||
1483 obj
.type
== isl_obj_set
, goto error
);
1487 obj
.type
->free(obj
.v
);
1491 __isl_give isl_set
*isl_stream_read_set(struct isl_stream
*s
, int nparam
)
1494 struct isl_set
*set
;
1496 obj
= obj_read(s
, nparam
);
1498 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
1502 obj
.type
->free(obj
.v
);
1506 static struct isl_basic_map
*basic_map_read(struct isl_stream
*s
, int nparam
)
1509 struct isl_map
*map
;
1510 struct isl_basic_map
*bmap
;
1512 obj
= obj_read(s
, nparam
);
1517 isl_assert(map
->ctx
, map
->n
<= 1, goto error
);
1520 bmap
= isl_basic_map_empty_like_map(map
);
1522 bmap
= isl_basic_map_copy(map
->p
[0]);
1532 __isl_give isl_basic_map
*isl_basic_map_read_from_file(isl_ctx
*ctx
,
1533 FILE *input
, int nparam
)
1535 struct isl_basic_map
*bmap
;
1536 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
1539 bmap
= basic_map_read(s
, nparam
);
1544 __isl_give isl_basic_set
*isl_basic_set_read_from_file(isl_ctx
*ctx
,
1545 FILE *input
, int nparam
)
1547 struct isl_basic_map
*bmap
;
1548 bmap
= isl_basic_map_read_from_file(ctx
, input
, nparam
);
1551 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
1552 return (struct isl_basic_set
*)bmap
;
1554 isl_basic_map_free(bmap
);
1558 struct isl_basic_map
*isl_basic_map_read_from_str(struct isl_ctx
*ctx
,
1559 const char *str
, int nparam
)
1561 struct isl_basic_map
*bmap
;
1562 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
1565 bmap
= basic_map_read(s
, nparam
);
1570 struct isl_basic_set
*isl_basic_set_read_from_str(struct isl_ctx
*ctx
,
1571 const char *str
, int nparam
)
1573 struct isl_basic_map
*bmap
;
1574 bmap
= isl_basic_map_read_from_str(ctx
, str
, nparam
);
1577 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
1578 return (struct isl_basic_set
*)bmap
;
1580 isl_basic_map_free(bmap
);
1584 __isl_give isl_map
*isl_map_read_from_file(struct isl_ctx
*ctx
,
1585 FILE *input
, int nparam
)
1587 struct isl_map
*map
;
1588 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
1591 map
= isl_stream_read_map(s
, nparam
);
1596 __isl_give isl_map
*isl_map_read_from_str(struct isl_ctx
*ctx
,
1597 const char *str
, int nparam
)
1599 struct isl_map
*map
;
1600 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
1603 map
= isl_stream_read_map(s
, nparam
);
1608 __isl_give isl_set
*isl_set_read_from_file(struct isl_ctx
*ctx
,
1609 FILE *input
, int nparam
)
1611 struct isl_map
*map
;
1612 map
= isl_map_read_from_file(ctx
, input
, nparam
);
1615 isl_assert(ctx
, isl_map_n_in(map
) == 0, goto error
);
1616 return (struct isl_set
*)map
;
1622 struct isl_set
*isl_set_read_from_str(struct isl_ctx
*ctx
,
1623 const char *str
, int nparam
)
1625 struct isl_map
*map
;
1626 map
= isl_map_read_from_str(ctx
, str
, nparam
);
1629 isl_assert(ctx
, isl_map_n_in(map
) == 0, goto error
);
1630 return (struct isl_set
*)map
;
1636 static char *next_line(FILE *input
, char *line
, unsigned len
)
1641 if (!(p
= fgets(line
, len
, input
)))
1643 while (isspace(*p
) && *p
!= '\n')
1645 } while (*p
== '#' || *p
== '\n');
1650 static struct isl_vec
*isl_vec_read_from_file_polylib(struct isl_ctx
*ctx
,
1653 struct isl_vec
*vec
= NULL
;
1662 isl_assert(ctx
, next_line(input
, line
, sizeof(line
)), return NULL
);
1663 isl_assert(ctx
, sscanf(line
, "%u", &size
) == 1, return NULL
);
1665 vec
= isl_vec_alloc(ctx
, size
);
1667 p
= next_line(input
, line
, sizeof(line
));
1668 isl_assert(ctx
, p
, goto error
);
1670 for (j
= 0; j
< size
; ++j
) {
1671 n
= sscanf(p
, "%s%n", val
, &offset
);
1672 isl_assert(ctx
, n
!= 0, goto error
);
1673 isl_int_read(vec
->el
[j
], val
);
1683 struct isl_vec
*isl_vec_read_from_file(struct isl_ctx
*ctx
,
1684 FILE *input
, unsigned input_format
)
1686 if (input_format
== ISL_FORMAT_POLYLIB
)
1687 return isl_vec_read_from_file_polylib(ctx
, input
);
1689 isl_assert(ctx
, 0, return NULL
);
1692 __isl_give isl_pw_qpolynomial
*isl_stream_read_pw_qpolynomial(
1693 struct isl_stream
*s
)
1696 struct isl_pw_qpolynomial
*pwqp
;
1698 obj
= obj_read(s
, -1);
1700 isl_assert(s
->ctx
, obj
.type
== isl_obj_pw_qpolynomial
,
1705 obj
.type
->free(obj
.v
);