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_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
179 total
= isl_basic_map_total_dim(bmap
);
180 for (i
= 0; i
< extra
; ++i
) {
182 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
184 isl_seq_clr(bmap
->div
[k
], 1+1+total
);
188 if (isl_stream_eat(s
, ':'))
190 bmap
= add_constraints(s
, v
, bmap
);
191 if (seen_paren
&& isl_stream_eat(s
, ')'))
195 isl_basic_map_free(bmap
);
199 static struct isl_basic_map
*add_constraint(struct isl_stream
*s
,
200 struct vars
**v
, struct isl_basic_map
*bmap
)
202 unsigned total
= isl_basic_map_total_dim(bmap
);
207 struct isl_token
*tok
= NULL
;
209 tok
= isl_stream_next_token(s
);
212 if (tok
->type
== ISL_TOKEN_EXISTS
) {
214 return add_exists(s
, v
, bmap
);
216 isl_stream_push_token(s
, tok
);
218 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
219 k
= isl_basic_map_alloc_inequality(bmap
);
222 isl_seq_clr(bmap
->ineq
[k
], 1+total
);
225 tok
= isl_stream_next_token(s
);
227 isl_stream_error(s
, NULL
, "unexpected EOF");
230 if (tok
->type
== ISL_TOKEN_IDENT
) {
232 int pos
= vars_pos(*v
, tok
->u
.s
, -1);
236 isl_stream_error(s
, tok
, "unknown identifier");
240 isl_int_add_ui(bmap
->ineq
[k
][1+pos
],
241 bmap
->ineq
[k
][1+pos
], 1);
243 isl_int_sub_ui(bmap
->ineq
[k
][1+pos
],
244 bmap
->ineq
[k
][1+pos
], 1);
245 } else if (tok
->type
== ISL_TOKEN_VALUE
) {
246 struct isl_token
*tok2
;
249 tok2
= isl_stream_next_token(s
);
250 if (tok2
&& tok2
->type
== ISL_TOKEN_IDENT
) {
251 pos
= vars_pos(*v
, tok2
->u
.s
, -1);
255 isl_stream_error(s
, tok2
,
256 "unknown identifier");
257 isl_token_free(tok2
);
260 isl_token_free(tok2
);
262 isl_stream_push_token(s
, tok2
);
264 isl_int_neg(tok
->u
.v
, tok
->u
.v
);
265 isl_int_add(bmap
->ineq
[k
][1+pos
],
266 bmap
->ineq
[k
][1+pos
], tok
->u
.v
);
267 } else if (tok
->type
== ISL_TOKEN_LE
) {
269 isl_seq_neg(bmap
->ineq
[k
], bmap
->ineq
[k
], 1+total
);
270 } else if (tok
->type
== ISL_TOKEN_GE
) {
273 } else if (tok
->type
== '=') {
275 isl_stream_error(s
, tok
, "too many operators");
282 isl_stream_push_token(s
, tok
);
289 isl_stream_error(s
, tok
, "missing operator");
293 isl_basic_map_inequality_to_equality(bmap
, k
);
298 isl_basic_map_free(bmap
);
302 static struct isl_basic_map
*add_constraints(struct isl_stream
*s
,
303 struct vars
**v
, struct isl_basic_map
*bmap
)
305 struct isl_token
*tok
;
308 bmap
= add_constraint(s
, v
, bmap
);
311 tok
= isl_stream_next_token(s
);
313 isl_stream_error(s
, NULL
, "unexpected EOF");
316 if (tok
->type
!= ISL_TOKEN_AND
)
320 isl_stream_push_token(s
, tok
);
326 isl_basic_map_free(bmap
);
330 static struct isl_basic_map
*basic_map_read(struct isl_stream
*s
)
332 struct isl_basic_map
*bmap
= NULL
;
333 struct isl_token
*tok
;
334 struct vars
*v
= NULL
;
338 tok
= isl_stream_next_token(s
);
339 if (!tok
|| tok
->type
!= '{') {
340 isl_stream_error(s
, tok
, "expecting '{'");
342 isl_stream_push_token(s
, tok
);
346 v
= vars_new(s
->ctx
);
347 v
= read_tuple(s
, v
);
351 tok
= isl_stream_next_token(s
);
352 if (tok
&& tok
->type
== ISL_TOKEN_TO
) {
354 v
= read_tuple(s
, v
);
360 isl_stream_push_token(s
, tok
);
364 bmap
= isl_basic_map_alloc(s
->ctx
, 0, n1
, n2
, 0, 0,0);
367 tok
= isl_stream_next_token(s
);
368 if (tok
&& tok
->type
== ':') {
370 bmap
= add_constraints(s
, &v
, bmap
);
371 tok
= isl_stream_next_token(s
);
373 if (tok
&& tok
->type
== '}') {
376 isl_stream_error(s
, tok
, "unexpected isl_token");
385 isl_basic_map_free(bmap
);
391 struct isl_basic_map
*isl_basic_map_read_from_file_omega(
392 struct isl_ctx
*ctx
, FILE *input
)
394 struct isl_basic_map
*bmap
;
395 struct isl_stream
*s
= isl_stream_new_file(ctx
, input
);
398 bmap
= basic_map_read(s
);
403 struct isl_basic_set
*isl_basic_set_read_from_file_omega(
404 struct isl_ctx
*ctx
, FILE *input
)
406 struct isl_basic_map
*bmap
;
407 bmap
= isl_basic_map_read_from_file_omega(ctx
, input
);
410 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
411 return (struct isl_basic_set
*)bmap
;
413 isl_basic_map_free(bmap
);
417 struct isl_basic_map
*isl_basic_map_read_from_str_omega(
418 struct isl_ctx
*ctx
, const char *str
)
420 struct isl_basic_map
*bmap
;
421 struct isl_stream
*s
= isl_stream_new_str(ctx
, str
);
424 bmap
= basic_map_read(s
);
429 struct isl_basic_set
*isl_basic_set_read_from_str_omega(
430 struct isl_ctx
*ctx
, const char *str
)
432 struct isl_basic_map
*bmap
;
433 bmap
= isl_basic_map_read_from_str_omega(ctx
, str
);
436 isl_assert(ctx
, isl_basic_map_n_in(bmap
) == 0, goto error
);
437 return (struct isl_basic_set
*)bmap
;
439 isl_basic_map_free(bmap
);