5 #include "isl_stream.h"
6 #include "isl_map_private.h"
7 #include "isl_input_omega.h"
12 struct variable
*next
;
21 static struct vars
*vars_new(struct isl_ctx
*ctx
)
24 v
= isl_alloc_type(ctx
, struct vars
);
33 void variable_free(struct variable
*var
)
36 struct variable
*next
= var
->next
;
43 static void vars_free(struct vars
*v
)
51 struct variable
*variable_new(struct vars
*v
, const char *name
, int len
,
55 var
= isl_alloc_type(v
->ctx
, struct variable
);
58 var
->name
= strdup(name
);
59 var
->name
[len
] = '\0';
68 static int vars_pos(struct vars
*v
, const char *s
, int len
)
75 for (q
= v
->v
; q
; q
= q
->next
) {
76 if (strncmp(q
->name
, s
, len
) == 0 && q
->name
[len
] == '\0')
83 v
->v
= variable_new(v
, s
, len
, v
->n
);
91 static struct vars
*read_var_list(struct isl_stream
*s
, struct vars
*v
)
93 struct isl_token
*tok
;
95 while ((tok
= isl_stream_next_token(s
)) != NULL
) {
99 if (tok
->type
!= ISL_TOKEN_IDENT
)
102 p
= vars_pos(v
, tok
->u
.s
, -1);
106 isl_stream_error(s
, tok
, "expecting unique identifier");
110 tok
= isl_stream_next_token(s
);
111 if (!tok
|| tok
->type
!= ',')
117 isl_stream_push_token(s
, tok
);
126 static struct vars
*read_tuple(struct isl_stream
*s
, struct vars
*v
)
128 struct isl_token
*tok
;
130 tok
= isl_stream_next_token(s
);
131 if (!tok
|| tok
->type
!= '[') {
132 isl_stream_error(s
, tok
, "expecting '['");
136 v
= read_var_list(s
, v
);
137 tok
= isl_stream_next_token(s
);
138 if (!tok
|| tok
->type
!= ']') {
139 isl_stream_error(s
, tok
, "expecting ']'");
152 static struct isl_basic_map
*add_constraints(struct isl_stream
*s
,
153 struct vars
**v
, struct isl_basic_map
*bmap
);
155 static struct isl_basic_map
*add_exists(struct isl_stream
*s
,
156 struct vars
**v
, struct isl_basic_map
*bmap
)
158 struct isl_token
*tok
;
165 tok
= isl_stream_next_token(s
);
168 if (tok
->type
== '(') {
172 isl_stream_push_token(s
, tok
);
173 *v
= read_var_list(s
, *v
);
177 bmap
= isl_basic_map_cow(bmap
);
178 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
180 total
= isl_basic_map_total_dim(bmap
);
181 for (i
= 0; i
< extra
; ++i
) {
183 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
185 isl_seq_clr(bmap
->div
[k
], 1+1+total
);
189 if (isl_stream_eat(s
, ':'))
191 bmap
= add_constraints(s
, v
, bmap
);
192 if (seen_paren
&& isl_stream_eat(s
, ')'))
196 isl_basic_map_free(bmap
);
200 static struct isl_basic_map
*add_constraint(struct isl_stream
*s
,
201 struct vars
**v
, struct isl_basic_map
*bmap
)
203 unsigned total
= isl_basic_map_total_dim(bmap
);
208 struct isl_token
*tok
= NULL
;
210 tok
= isl_stream_next_token(s
);
213 if (tok
->type
== ISL_TOKEN_EXISTS
) {
215 return add_exists(s
, v
, bmap
);
217 isl_stream_push_token(s
, tok
);
219 bmap
= isl_basic_map_cow(bmap
);
220 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
221 k
= isl_basic_map_alloc_inequality(bmap
);
224 isl_seq_clr(bmap
->ineq
[k
], 1+total
);
227 tok
= isl_stream_next_token(s
);
229 isl_stream_error(s
, NULL
, "unexpected EOF");
232 if (tok
->type
== ISL_TOKEN_IDENT
) {
234 int pos
= vars_pos(*v
, tok
->u
.s
, -1);
238 isl_stream_error(s
, tok
, "unknown identifier");
242 isl_int_add_ui(bmap
->ineq
[k
][1+pos
],
243 bmap
->ineq
[k
][1+pos
], 1);
245 isl_int_sub_ui(bmap
->ineq
[k
][1+pos
],
246 bmap
->ineq
[k
][1+pos
], 1);
247 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
248 struct isl_token
*tok2
;
251 tok2
= isl_stream_next_token(s
);
252 if (tok2
&& tok2
->type
== ISL_TOKEN_IDENT
) {
253 pos
= vars_pos(*v
, tok2
->u
.s
, -1);
257 isl_stream_error(s
, tok2
,
258 "unknown identifier");
259 isl_token_free(tok2
);
262 isl_token_free(tok2
);
264 isl_stream_push_token(s
, tok2
);
266 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
267 isl_int_add(bmap
->ineq
[k
][1+pos
],
268 bmap
->ineq
[k
][1+pos
], tok
->u
.v
);
269 } else if (tok
->type
== '+') {
271 } else if (tok
->type
== ISL_TOKEN_LE
) {
273 isl_seq_neg(bmap
->ineq
[k
], bmap
->ineq
[k
], 1+total
);
274 } else if (tok
->type
== ISL_TOKEN_GE
) {
277 } else if (tok
->type
== '=') {
279 isl_stream_error(s
, tok
, "too many operators");
286 isl_stream_push_token(s
, tok
);
293 isl_stream_error(s
, tok
, "missing operator");
297 isl_basic_map_inequality_to_equality(bmap
, k
);
302 isl_basic_map_free(bmap
);
306 static struct isl_basic_map
*add_constraints(struct isl_stream
*s
,
307 struct vars
**v
, struct isl_basic_map
*bmap
)
309 struct isl_token
*tok
;
312 bmap
= add_constraint(s
, v
, bmap
);
315 tok
= isl_stream_next_token(s
);
317 isl_stream_error(s
, NULL
, "unexpected EOF");
320 if (tok
->type
!= ISL_TOKEN_AND
)
324 isl_stream_push_token(s
, tok
);
330 isl_basic_map_free(bmap
);
334 static struct isl_basic_map
*basic_map_read(struct isl_stream
*s
)
336 struct isl_basic_map
*bmap
= NULL
;
337 struct isl_token
*tok
;
338 struct vars
*v
= NULL
;
342 tok
= isl_stream_next_token(s
);
343 if (!tok
|| tok
->type
!= '{') {
344 isl_stream_error(s
, tok
, "expecting '{'");
346 isl_stream_push_token(s
, tok
);
350 v
= vars_new(s
->ctx
);
351 v
= read_tuple(s
, v
);
355 tok
= isl_stream_next_token(s
);
356 if (tok
&& tok
->type
== ISL_TOKEN_TO
) {
358 v
= read_tuple(s
, v
);
364 isl_stream_push_token(s
, tok
);
368 bmap
= isl_basic_map_alloc(s
->ctx
, 0, n1
, n2
, 0, 0,0);
371 tok
= isl_stream_next_token(s
);
372 if (tok
&& tok
->type
== ':') {
374 bmap
= add_constraints(s
, &v
, bmap
);
375 tok
= isl_stream_next_token(s
);
377 if (tok
&& tok
->type
== '}') {
380 isl_stream_error(s
, tok
, "unexpected isl_token");
389 isl_basic_map_free(bmap
);
395 struct isl_basic_map
*isl_basic_map_read_from_file_omega(
396 struct isl_ctx
*ctx
, FILE *input
)
398 struct isl_basic_map
*bmap
;
399 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
402 bmap
= basic_map_read(s
);
407 struct isl_basic_set
*isl_basic_set_read_from_file_omega(
408 struct isl_ctx
*ctx
, FILE *input
)
410 struct isl_basic_map
*bmap
;
411 bmap
= isl_basic_map_read_from_file_omega(ctx
, input
);
414 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
415 return (struct isl_basic_set
*)bmap
;
417 isl_basic_map_free(bmap
);
421 struct isl_basic_map
*isl_basic_map_read_from_str_omega(
422 struct isl_ctx
*ctx
, const char *str
)
424 struct isl_basic_map
*bmap
;
425 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
428 bmap
= basic_map_read(s
);
433 struct isl_basic_set
*isl_basic_set_read_from_str_omega(
434 struct isl_ctx
*ctx
, const char *str
)
436 struct isl_basic_map
*bmap
;
437 bmap
= isl_basic_map_read_from_str_omega(ctx
, str
);
440 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
441 return (struct isl_basic_set
*)bmap
;
443 isl_basic_map_free(bmap
);