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
== ISL_TOKEN_LE
) {
271 isl_seq_neg(bmap
->ineq
[k
], bmap
->ineq
[k
], 1+total
);
272 } else if (tok
->type
== ISL_TOKEN_GE
) {
275 } else if (tok
->type
== '=') {
277 isl_stream_error(s
, tok
, "too many operators");
284 isl_stream_push_token(s
, tok
);
291 isl_stream_error(s
, tok
, "missing operator");
295 isl_basic_map_inequality_to_equality(bmap
, k
);
300 isl_basic_map_free(bmap
);
304 static struct isl_basic_map
*add_constraints(struct isl_stream
*s
,
305 struct vars
**v
, struct isl_basic_map
*bmap
)
307 struct isl_token
*tok
;
310 bmap
= add_constraint(s
, v
, bmap
);
313 tok
= isl_stream_next_token(s
);
315 isl_stream_error(s
, NULL
, "unexpected EOF");
318 if (tok
->type
!= ISL_TOKEN_AND
)
322 isl_stream_push_token(s
, tok
);
328 isl_basic_map_free(bmap
);
332 static struct isl_basic_map
*basic_map_read(struct isl_stream
*s
)
334 struct isl_basic_map
*bmap
= NULL
;
335 struct isl_token
*tok
;
336 struct vars
*v
= NULL
;
340 tok
= isl_stream_next_token(s
);
341 if (!tok
|| tok
->type
!= '{') {
342 isl_stream_error(s
, tok
, "expecting '{'");
344 isl_stream_push_token(s
, tok
);
348 v
= vars_new(s
->ctx
);
349 v
= read_tuple(s
, v
);
353 tok
= isl_stream_next_token(s
);
354 if (tok
&& tok
->type
== ISL_TOKEN_TO
) {
356 v
= read_tuple(s
, v
);
362 isl_stream_push_token(s
, tok
);
366 bmap
= isl_basic_map_alloc(s
->ctx
, 0, n1
, n2
, 0, 0,0);
369 tok
= isl_stream_next_token(s
);
370 if (tok
&& tok
->type
== ':') {
372 bmap
= add_constraints(s
, &v
, bmap
);
373 tok
= isl_stream_next_token(s
);
375 if (tok
&& tok
->type
== '}') {
378 isl_stream_error(s
, tok
, "unexpected isl_token");
387 isl_basic_map_free(bmap
);
393 struct isl_basic_map
*isl_basic_map_read_from_file_omega(
394 struct isl_ctx
*ctx
, FILE *input
)
396 struct isl_basic_map
*bmap
;
397 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
400 bmap
= basic_map_read(s
);
405 struct isl_basic_set
*isl_basic_set_read_from_file_omega(
406 struct isl_ctx
*ctx
, FILE *input
)
408 struct isl_basic_map
*bmap
;
409 bmap
= isl_basic_map_read_from_file_omega(ctx
, input
);
412 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
413 return (struct isl_basic_set
*)bmap
;
415 isl_basic_map_free(bmap
);
419 struct isl_basic_map
*isl_basic_map_read_from_str_omega(
420 struct isl_ctx
*ctx
, const char *str
)
422 struct isl_basic_map
*bmap
;
423 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
426 bmap
= basic_map_read(s
);
431 struct isl_basic_set
*isl_basic_set_read_from_str_omega(
432 struct isl_ctx
*ctx
, const char *str
)
434 struct isl_basic_map
*bmap
;
435 bmap
= isl_basic_map_read_from_str_omega(ctx
, str
);
438 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
439 return (struct isl_basic_set
*)bmap
;
441 isl_basic_map_free(bmap
);