5 #include <isl_stream.h>
6 #include <isl_vertices.h>
7 #include <isl_obj_list.h>
8 #include <isl_obj_str.h>
9 #include <barvinok/barvinok.h>
14 #include <cloog/isl/cloog.h>
17 static int isl_bool_false
= 0;
18 static int isl_bool_true
= 1;
19 static int isl_bool_error
= -1;
21 enum iscc_op
{ ISCC_READ
, ISCC_SOURCE
, ISCC_VERTICES
,
22 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
, ISCC_N_OP
};
23 static const char *op_name
[ISCC_N_OP
] = {
24 "read", "source", "vertices", "last", "any", "before", "under" };
25 static enum isl_token_type iscc_op
[ISCC_N_OP
];
27 struct isl_arg_choice iscc_format
[] = {
28 {"isl", ISL_FORMAT_ISL
},
29 {"omega", ISL_FORMAT_OMEGA
},
30 {"polylib", ISL_FORMAT_POLYLIB
},
31 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
32 {"latex", ISL_FORMAT_LATEX
},
38 struct barvinok_options
*barvinok
;
42 struct isl_arg iscc_options_arg
[] = {
43 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
45 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
46 iscc_format
, ISL_FORMAT_ISL
, "output format")
50 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
52 static void *isl_obj_bool_copy(void *v
)
57 static void isl_obj_bool_free(void *v
)
61 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
64 if (v
== &isl_bool_true
)
65 return isl_printer_print_str(p
, "True");
66 else if (v
== &isl_bool_false
)
67 return isl_printer_print_str(p
, "False");
69 return isl_printer_print_str(p
, "Error");
72 static void *isl_obj_bool_add(void *v1
, void *v2
)
77 struct isl_obj_vtable isl_obj_bool_vtable
= {
83 #define isl_obj_bool (&isl_obj_bool_vtable)
85 int *isl_bool_from_int(int res
)
87 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
90 int *union_map_is_equal(__isl_take isl_union_map
*map1
,
91 __isl_take isl_union_map
*map2
)
93 int res
= isl_union_map_is_equal(map1
, map2
);
94 isl_union_map_free(map1
);
95 isl_union_map_free(map2
);
96 return isl_bool_from_int(res
);
98 int *union_set_is_equal(__isl_take isl_union_set
*set1
,
99 __isl_take isl_union_set
*set2
)
101 return union_map_is_equal((isl_union_map
*)set1
, (isl_union_map
*)set2
);
104 int *union_map_is_subset(__isl_take isl_union_map
*map1
,
105 __isl_take isl_union_map
*map2
)
107 int res
= isl_union_map_is_subset(map1
, map2
);
108 isl_union_map_free(map1
);
109 isl_union_map_free(map2
);
110 return isl_bool_from_int(res
);
112 int *union_set_is_subset(__isl_take isl_union_set
*set1
,
113 __isl_take isl_union_set
*set2
)
115 return union_map_is_subset((isl_union_map
*)set1
, (isl_union_map
*)set2
);
118 int *union_map_is_strict_subset(__isl_take isl_union_map
*map1
,
119 __isl_take isl_union_map
*map2
)
121 int res
= isl_union_map_is_strict_subset(map1
, map2
);
122 isl_union_map_free(map1
);
123 isl_union_map_free(map2
);
124 return isl_bool_from_int(res
);
126 int *union_set_is_strict_subset(__isl_take isl_union_set
*set1
,
127 __isl_take isl_union_set
*set2
)
129 return union_map_is_strict_subset((isl_union_map
*)set1
,
130 (isl_union_map
*)set2
);
133 int *union_map_is_superset(__isl_take isl_union_map
*map1
,
134 __isl_take isl_union_map
*map2
)
136 return union_map_is_subset(map2
, map1
);
138 int *union_set_is_superset(__isl_take isl_union_set
*set1
,
139 __isl_take isl_union_set
*set2
)
141 return union_set_is_subset(set2
, set1
);
144 int *union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
145 __isl_take isl_union_map
*map2
)
147 return union_map_is_strict_subset(map2
, map1
);
149 int *union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
150 __isl_take isl_union_set
*set2
)
152 return union_set_is_strict_subset(set2
, set1
);
155 extern struct isl_obj_vtable isl_obj_list_vtable
;
156 #define isl_obj_list (&isl_obj_list_vtable)
158 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
160 enum isl_token_type op
;
166 struct isc_named_bin_op
{
168 struct isc_bin_op op
;
172 isl_union_pw_qpolynomial
*upwqp
;
173 isl_union_pw_qpolynomial
*res
;
176 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
178 struct iscc_at
*at
= (struct iscc_at
*) user
;
182 set
= isl_set_from_point(isl_point_copy(pnt
));
183 qp
= isl_union_pw_qpolynomial_eval(
184 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
186 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
187 isl_union_pw_qpolynomial_from_pw_qpolynomial(
188 isl_pw_qpolynomial_alloc(set
, qp
)));
193 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
194 __isl_take isl_union_pw_qpolynomial
*upwqp
,
195 __isl_take isl_union_set
*uset
)
200 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
202 isl_union_set_foreach_point(uset
, eval_at
, &at
);
204 isl_union_pw_qpolynomial_free(upwqp
);
205 isl_union_set_free(uset
);
210 struct iscc_fold_at
{
211 isl_union_pw_qpolynomial_fold
*upwf
;
212 isl_union_pw_qpolynomial
*res
;
215 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
217 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
221 set
= isl_set_from_point(isl_point_copy(pnt
));
222 qp
= isl_union_pw_qpolynomial_fold_eval(
223 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
225 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
226 isl_union_pw_qpolynomial_from_pw_qpolynomial(
227 isl_pw_qpolynomial_alloc(set
, qp
)));
232 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
233 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
234 __isl_take isl_union_set
*uset
)
236 struct iscc_fold_at at
;
239 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
241 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
243 isl_union_pw_qpolynomial_fold_free(upwf
);
244 isl_union_set_free(uset
);
249 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
250 __isl_take isl_union_pw_qpolynomial
*upwqp
,
251 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
253 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
257 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
258 __isl_take isl_union_map
*umap
,
259 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
262 struct isl_list
*list
;
265 ctx
= isl_union_map_get_ctx(umap
);
266 list
= isl_list_alloc(ctx
, 2);
270 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
271 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
273 list
->obj
[1].type
= isl_obj_bool
;
274 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
275 if (tight
< 0 || !list
->obj
[0].v
)
280 isl_union_map_free(umap
);
281 isl_union_pw_qpolynomial_fold_free(upwf
);
287 struct isc_bin_op bin_ops
[] = {
288 { '+', isl_obj_union_set
, isl_obj_union_set
,
290 (isc_bin_op_fn
) &isl_union_set_union
},
291 { '+', isl_obj_union_map
, isl_obj_union_map
,
293 (isc_bin_op_fn
) &isl_union_map_union
},
294 { '-', isl_obj_union_set
, isl_obj_union_set
,
296 (isc_bin_op_fn
) &isl_union_set_subtract
},
297 { '-', isl_obj_union_map
, isl_obj_union_map
,
299 (isc_bin_op_fn
) &isl_union_map_subtract
},
300 { '*', isl_obj_union_set
, isl_obj_union_set
,
302 (isc_bin_op_fn
) &isl_union_set_intersect
},
303 { '*', isl_obj_union_map
, isl_obj_union_map
,
305 (isc_bin_op_fn
) &isl_union_map_intersect
},
306 { '*', isl_obj_union_map
, isl_obj_union_set
,
308 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
309 { '.', isl_obj_union_map
, isl_obj_union_map
,
311 (isc_bin_op_fn
) &isl_union_map_apply_range
},
312 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
313 isl_obj_union_pw_qpolynomial
,
314 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
315 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
317 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
318 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
320 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
321 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
322 (isc_bin_op_fn
) &union_set_is_equal
},
323 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
324 (isc_bin_op_fn
) &union_map_is_equal
},
325 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
326 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_subset
},
327 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
328 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_subset
},
329 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
330 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_subset
},
331 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
332 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_subset
},
333 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
334 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_superset
},
335 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
336 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_superset
},
337 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
338 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_superset
},
339 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
340 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_superset
},
341 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
343 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
344 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
346 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
347 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
349 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
350 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
352 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
353 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
355 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
356 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
358 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
359 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
361 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
362 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
364 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
365 { '.', isl_obj_union_pw_qpolynomial_fold
,
366 isl_obj_union_pw_qpolynomial_fold
,
367 isl_obj_union_pw_qpolynomial_fold
,
368 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
369 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
370 isl_obj_union_pw_qpolynomial
,
371 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
372 { '+', isl_obj_union_pw_qpolynomial
,
373 isl_obj_union_pw_qpolynomial_fold
,
374 isl_obj_union_pw_qpolynomial_fold
,
375 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
376 { '+', isl_obj_union_pw_qpolynomial_fold
,
377 isl_obj_union_pw_qpolynomial
,
378 isl_obj_union_pw_qpolynomial_fold
,
379 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
380 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
381 isl_obj_union_pw_qpolynomial
,
382 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
383 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
384 isl_obj_union_pw_qpolynomial
,
385 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
386 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
387 isl_obj_union_pw_qpolynomial
,
388 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
389 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
390 isl_obj_union_pw_qpolynomial_fold
,
391 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
392 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
393 isl_obj_union_pw_qpolynomial
,
394 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
395 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
396 isl_obj_union_pw_qpolynomial
,
397 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
398 { '%', isl_obj_union_set
, isl_obj_union_set
,
400 (isc_bin_op_fn
) &isl_union_set_gist
},
401 { '%', isl_obj_union_map
, isl_obj_union_map
,
403 (isc_bin_op_fn
) &isl_union_map_gist
},
404 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
405 isl_obj_union_pw_qpolynomial
,
406 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
407 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
408 isl_obj_union_pw_qpolynomial_fold
,
409 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
410 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
411 (isc_bin_op_fn
) &isl_str_concat
},
415 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
416 __isl_take isl_union_map
*umap2
)
418 return isl_union_map_apply_range(umap2
, umap1
);
421 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
422 __isl_take isl_union_pw_qpolynomial
*upwqp
,
423 __isl_take isl_union_map
*umap
)
425 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
428 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
429 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
430 __isl_take isl_union_map
*umap
)
432 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
435 struct isc_named_bin_op named_bin_ops
[] = {
436 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
438 (isc_bin_op_fn
) &map_after_map
} },
439 { "after", { -1, isl_obj_union_pw_qpolynomial
,
440 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
441 (isc_bin_op_fn
) &qpolynomial_after_map
} },
442 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
443 isl_obj_union_map
, isl_obj_list
,
444 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
445 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
447 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
448 { "before", { -1, isl_obj_union_map
,
449 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
450 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
451 { "before", { -1, isl_obj_union_map
,
452 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
453 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
454 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
456 (isc_bin_op_fn
) &isl_union_set_product
} },
457 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
459 (isc_bin_op_fn
) &isl_union_map_product
} },
463 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
465 return isl_set_from_basic_set(isl_union_set_sample(uset
));
468 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
470 return isl_map_from_basic_map(isl_union_map_sample(umap
));
473 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
474 __isl_take isl_union_pw_qpolynomial
*upwqp
)
477 struct isl_list
*list
;
480 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
481 list
= isl_list_alloc(ctx
, 2);
485 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
486 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
487 isl_fold_max
, &tight
);
488 list
->obj
[1].type
= isl_obj_bool
;
489 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
490 if (tight
< 0 || !list
->obj
[0].v
)
495 isl_union_pw_qpolynomial_free(upwqp
);
502 void *map_codegen(void *arg
)
505 isl_union_map
*umap
= (isl_union_map
*)arg
;
506 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
508 CloogOptions
*options
;
509 CloogDomain
*context
;
510 CloogUnionDomain
*ud
;
512 struct clast_stmt
*stmt
;
514 state
= cloog_isl_state_malloc(ctx
);
515 options
= cloog_options_malloc(state
);
516 options
->language
= LANGUAGE_C
;
517 options
->strides
= 1;
519 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
521 dim
= isl_union_map_get_dim(umap
);
522 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
524 input
= cloog_input_alloc(context
, ud
);
526 stmt
= cloog_clast_create_from_input(input
, options
);
527 clast_pprint(stdout
, stmt
, 0, options
);
528 cloog_clast_free(stmt
);
531 cloog_options_free(options
);
532 cloog_state_free(state
);
533 isl_union_map_free(umap
);
537 void *set_codegen(void *arg
)
540 isl_union_set
*uset
= (isl_union_set
*)arg
;
541 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
543 CloogOptions
*options
;
544 CloogDomain
*context
;
545 CloogUnionDomain
*ud
;
547 struct clast_stmt
*stmt
;
549 state
= cloog_isl_state_malloc(ctx
);
550 options
= cloog_options_malloc(state
);
551 options
->language
= LANGUAGE_C
;
552 options
->strides
= 1;
554 ud
= cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset
));
556 dim
= isl_union_set_get_dim(uset
);
557 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
559 input
= cloog_input_alloc(context
, ud
);
561 stmt
= cloog_clast_create_from_input(input
, options
);
562 clast_pprint(stdout
, stmt
, 0, options
);
563 cloog_clast_free(stmt
);
566 cloog_options_free(options
);
567 cloog_state_free(state
);
568 isl_union_set_free(uset
);
573 typedef void *(*isc_un_op_fn
)(void *arg
);
575 enum isl_token_type op
;
580 struct isc_named_un_op
{
584 struct isc_named_un_op named_un_ops
[] = {
585 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
586 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
587 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
588 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
589 {"card", { -1, isl_obj_union_set
,
590 isl_obj_union_pw_qpolynomial
,
591 (isc_un_op_fn
) &isl_union_set_card
} },
592 {"card", { -1, isl_obj_union_map
,
593 isl_obj_union_pw_qpolynomial
,
594 (isc_un_op_fn
) &isl_union_map_card
} },
595 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
596 (isc_un_op_fn
) &isl_union_set_coalesce
} },
597 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
598 (isc_un_op_fn
) &isl_union_map_coalesce
} },
599 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
600 isl_obj_union_pw_qpolynomial
,
601 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
602 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
603 isl_obj_union_pw_qpolynomial_fold
,
604 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
606 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
608 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
611 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
612 (isc_un_op_fn
) &isl_union_map_deltas
} },
613 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
614 (isc_un_op_fn
) &isl_union_map_domain
} },
615 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
616 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
617 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
619 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
620 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
621 (isc_un_op_fn
) &isl_union_map_range
} },
622 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
623 (isc_un_op_fn
) &isl_union_set_identity
} },
624 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
625 (isc_un_op_fn
) &isl_union_map_lexmin
} },
626 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
627 (isc_un_op_fn
) &isl_union_map_lexmax
} },
628 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
629 (isc_un_op_fn
) &isl_union_set_lexmin
} },
630 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
631 (isc_un_op_fn
) &isl_union_set_lexmax
} },
632 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
633 (isc_un_op_fn
) &union_set_sample
} },
634 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
635 (isc_un_op_fn
) &union_map_sample
} },
636 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
637 isl_obj_union_pw_qpolynomial
,
638 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
639 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
640 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
641 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
642 (isc_un_op_fn
) &isl_union_set_unwrap
} },
643 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
644 (isc_un_op_fn
) &isl_union_map_wrap
} },
648 struct isl_named_obj
{
653 static void free_obj(struct isl_obj obj
)
655 obj
.type
->free(obj
.v
);
658 static int same_name(const void *entry
, const void *val
)
660 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
662 return !strcmp(named
->name
, val
);
665 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
666 char *name
, struct isl_obj obj
)
668 struct isl_hash_table_entry
*entry
;
670 struct isl_named_obj
*named
;
672 name_hash
= isl_hash_string(isl_hash_init(), name
);
673 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
678 free_obj(named
->obj
);
681 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
696 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
697 struct isl_hash_table
*table
, char *name
)
699 struct isl_obj obj
= { isl_obj_none
, NULL
};
700 struct isl_hash_table_entry
*entry
;
703 name_hash
= isl_hash_string(isl_hash_init(), name
);
704 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
706 struct isl_named_obj
*named
;
710 fprintf(stderr
, "unknown identifier '%s'\n", name
);
713 obj
.v
= obj
.type
->copy(obj
.v
);
717 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
719 if (obj
.type
== super
)
721 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
723 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
725 if (obj
.type
== isl_obj_pw_qpolynomial
&&
726 super
== isl_obj_union_pw_qpolynomial
)
728 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
729 super
== isl_obj_union_pw_qpolynomial_fold
)
731 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
733 if (obj
.type
== isl_obj_list
) {
734 struct isl_list
*list
= obj
.v
;
735 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
736 return is_subtype(list
->obj
[0], super
);
738 if (super
== isl_obj_str
)
743 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
745 struct isl_list
*list
= obj
.v
;
748 obj
.v
= obj
.type
->copy(obj
.v
);
755 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
758 if (obj
.type
== type
)
760 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
761 obj
.type
= isl_obj_union_map
;
762 obj
.v
= isl_union_map_from_map(obj
.v
);
765 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
766 obj
.type
= isl_obj_union_set
;
767 obj
.v
= isl_union_set_from_set(obj
.v
);
770 if (obj
.type
== isl_obj_pw_qpolynomial
&&
771 type
== isl_obj_union_pw_qpolynomial
) {
772 obj
.type
= isl_obj_union_pw_qpolynomial
;
773 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
776 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
777 type
== isl_obj_union_pw_qpolynomial_fold
) {
778 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
779 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
782 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
783 if (type
== isl_obj_union_map
) {
784 obj
.type
= isl_obj_union_map
;
787 if (type
== isl_obj_union_pw_qpolynomial
) {
788 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
789 isl_union_set_free(obj
.v
);
790 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
791 obj
.type
= isl_obj_union_pw_qpolynomial
;
794 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
795 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
796 isl_union_set_free(obj
.v
);
797 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
799 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
803 if (obj
.type
== isl_obj_list
) {
804 struct isl_list
*list
= obj
.v
;
805 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
806 return convert(ctx
, obj_at(obj
, 0), type
);
808 if (type
== isl_obj_str
) {
813 p
= isl_printer_to_str(ctx
);
816 p
= obj
.type
->print(p
, obj
.v
);
817 s
= isl_printer_get_str(p
);
820 str
= isl_str_alloc(ctx
);
828 obj
.type
= isl_obj_str
;
834 obj
.type
= isl_obj_none
;
839 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
843 struct isl_token
*tok
;
845 tok
= isl_stream_next_token(s
);
852 if (bin_ops
[i
].op
!= tok
->type
)
854 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
862 if (!named_bin_ops
[i
].name
)
864 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
866 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
870 return &named_bin_ops
[i
].op
;
873 isl_stream_push_token(s
, tok
);
878 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
881 struct isl_token
*tok
;
883 tok
= isl_stream_next_token(s
);
888 if (!named_un_ops
[i
].name
)
890 if (named_un_ops
[i
].op
.op
!= tok
->type
)
894 return &named_un_ops
[i
].op
;
897 isl_stream_push_token(s
, tok
);
902 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
908 if (!named_un_ops
[i
].name
)
910 if (named_un_ops
[i
].op
.op
!= like
->op
)
912 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
915 return &named_un_ops
[i
].op
;
921 static int is_assign(struct isl_stream
*s
)
923 struct isl_token
*tok
;
924 struct isl_token
*tok2
;
927 tok
= isl_stream_next_token(s
);
930 if (tok
->type
!= ISL_TOKEN_IDENT
) {
931 isl_stream_push_token(s
, tok
);
935 tok2
= isl_stream_next_token(s
);
937 isl_stream_push_token(s
, tok
);
940 assign
= tok2
->type
== ISL_TOKEN_DEF
;
941 isl_stream_push_token(s
, tok2
);
942 isl_stream_push_token(s
, tok
);
947 static struct isl_obj
read_obj(struct isl_stream
*s
,
948 struct isl_hash_table
*table
);
949 static struct isl_obj
read_expr(struct isl_stream
*s
,
950 struct isl_hash_table
*table
);
952 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
953 struct isl_hash_table
*table
, struct isc_un_op
*op
)
955 struct isl_obj obj
= { isl_obj_none
, NULL
};
957 obj
= read_obj(s
, table
);
961 op
= find_matching_un_op(op
, obj
);
963 isl_assert(s
->ctx
, op
, goto error
);
964 obj
= convert(s
->ctx
, obj
, op
->arg
);
965 obj
.v
= op
->fn(obj
.v
);
971 obj
.type
= isl_obj_none
;
976 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
978 struct isl_list
*list
;
981 if (obj
.type
!= isl_obj_union_map
)
982 obj
= convert(ctx
, obj
, isl_obj_union_map
);
983 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
984 list
= isl_list_alloc(ctx
, 2);
988 list
->obj
[0].type
= isl_obj_union_map
;
989 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
990 list
->obj
[1].type
= isl_obj_bool
;
991 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
993 obj
.type
= isl_obj_list
;
994 if (exact
< 0 || !list
->obj
[0].v
)
1000 obj
.type
= isl_obj_none
;
1005 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1007 struct isl_list
*list
= obj
.v
;
1008 struct isl_token
*tok
;
1011 tok
= isl_stream_next_token(s
);
1012 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1013 isl_stream_error(s
, tok
, "expecting index");
1015 isl_stream_push_token(s
, tok
);
1018 i
= isl_int_get_si(tok
->u
.v
);
1019 isl_token_free(tok
);
1020 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1021 if (isl_stream_eat(s
, ']'))
1024 return obj_at(obj
, i
);
1027 obj
.type
= isl_obj_none
;
1032 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1033 struct isl_hash_table
*table
)
1037 obj
= read_expr(s
, table
);
1038 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1039 is_subtype(obj
, isl_obj_union_map
), goto error
);
1041 if (obj
.type
== isl_obj_list
) {
1042 struct isl_list
*list
= obj
.v
;
1043 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1044 obj
= obj_at(obj
, 0);
1046 if (obj
.type
== isl_obj_set
)
1047 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1048 else if (obj
.type
== isl_obj_map
)
1049 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1050 if (obj
.type
== isl_obj_union_set
) {
1051 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1053 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1057 if (isl_stream_eat(s
, ')'))
1062 isl_union_map_free(umap
);
1065 obj
.type
= isl_obj_none
;
1070 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1071 struct isl_obj obj
, struct isl_hash_table
*table
)
1075 arg
= read_expr(s
, table
);
1076 isl_assert(s
->ctx
, is_subtype(arg
, isl_obj_union_map
), goto error
);
1078 if (arg
.type
== isl_obj_list
) {
1079 struct isl_list
*list
= arg
.v
;
1080 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1081 arg
= obj_at(arg
, 0);
1083 if (arg
.type
== isl_obj_map
)
1084 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1085 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1086 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(arg
.v
, obj
.v
);
1088 obj
.type
= isl_obj_list
;
1089 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(arg
.v
, obj
.v
);
1094 if (isl_stream_eat(s
, ')'))
1102 obj
.type
= isl_obj_none
;
1107 struct add_vertex_data
{
1108 struct isl_list
*list
;
1112 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1114 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1115 isl_basic_set
*expr
;
1117 expr
= isl_vertex_get_expr(vertex
);
1119 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1120 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1123 isl_vertex_free(vertex
);
1128 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1131 isl_basic_set
*hull
;
1132 isl_vertices
*vertices
= NULL
;
1133 struct isl_list
*list
= NULL
;
1135 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1137 set
= isl_set_remove_divs(set
);
1138 hull
= isl_set_convex_hull(set
);
1139 vertices
= isl_basic_set_compute_vertices(hull
);
1140 isl_basic_set_free(hull
);
1144 ctx
= isl_vertices_get_ctx(vertices
);
1145 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1150 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1152 data
->list
= isl_list_concat(list
, data
->list
);
1154 isl_vertices_free(vertices
);
1159 isl_vertices_free(vertices
);
1163 static struct isl_obj
vertices(struct isl_stream
*s
,
1164 struct isl_hash_table
*table
)
1168 struct isl_list
*list
= NULL
;
1169 isl_union_set
*uset
;
1170 struct add_vertex_data data
= { NULL
};
1172 obj
= read_expr(s
, table
);
1173 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1174 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1178 ctx
= isl_union_set_get_ctx(uset
);
1179 list
= isl_list_alloc(ctx
, 0);
1185 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1188 isl_union_set_free(uset
);
1190 obj
.type
= isl_obj_list
;
1195 isl_union_set_free(uset
);
1196 isl_list_free(data
.list
);
1198 obj
.type
= isl_obj_none
;
1203 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1204 struct isl_hash_table
*table
)
1208 obj
= read_obj(s
, table
);
1209 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1210 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1217 static struct isl_obj
last_any(struct isl_stream
*s
,
1218 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1219 __isl_take isl_union_map
*may_source
)
1221 struct isl_obj obj
= { isl_obj_none
, NULL
};
1222 isl_union_map
*sink
= NULL
;
1223 isl_union_map
*schedule
= NULL
;
1224 isl_union_map
*may_dep
;
1225 isl_union_map
*must_dep
;
1227 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1230 sink
= read_map(s
, table
);
1234 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1237 schedule
= read_map(s
, table
);
1241 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1242 schedule
, &must_dep
, &may_dep
,
1246 obj
.type
= isl_obj_union_map
;
1247 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1251 isl_union_map_free(may_source
);
1252 isl_union_map_free(must_source
);
1253 isl_union_map_free(sink
);
1254 isl_union_map_free(schedule
);
1256 obj
.type
= isl_obj_none
;
1261 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1263 struct isl_obj obj
= { isl_obj_none
, NULL
};
1264 isl_union_map
*must_source
= NULL
;
1265 isl_union_map
*may_source
= NULL
;
1266 isl_union_map
*sink
= NULL
;
1267 isl_union_map
*schedule
= NULL
;
1268 isl_union_map
*may_dep
;
1270 may_source
= read_map(s
, table
);
1274 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1275 must_source
= read_map(s
, table
);
1278 return last_any(s
, table
, must_source
, may_source
);
1281 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1284 sink
= read_map(s
, table
);
1288 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1291 schedule
= read_map(s
, table
);
1295 must_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1296 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1297 schedule
, NULL
, &may_dep
,
1301 obj
.type
= isl_obj_union_map
;
1306 isl_union_map_free(may_source
);
1307 isl_union_map_free(must_source
);
1308 isl_union_map_free(sink
);
1309 isl_union_map_free(schedule
);
1311 obj
.type
= isl_obj_none
;
1316 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1318 struct isl_obj obj
= { isl_obj_none
, NULL
};
1319 struct isl_list
*list
= NULL
;
1320 isl_union_map
*must_source
= NULL
;
1321 isl_union_map
*may_source
= NULL
;
1322 isl_union_map
*sink
= NULL
;
1323 isl_union_map
*schedule
= NULL
;
1324 isl_union_map
*must_dep
;
1325 isl_union_set
*must_no_source
;
1327 must_source
= read_map(s
, table
);
1331 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1332 may_source
= read_map(s
, table
);
1335 return last_any(s
, table
, must_source
, may_source
);
1338 list
= isl_list_alloc(s
->ctx
, 2);
1342 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1345 sink
= read_map(s
, table
);
1349 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1352 schedule
= read_map(s
, table
);
1356 may_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1357 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1358 schedule
, &must_dep
, NULL
,
1359 &must_no_source
, NULL
) < 0)
1362 list
->obj
[0].type
= isl_obj_union_map
;
1363 list
->obj
[0].v
= must_dep
;
1364 list
->obj
[1].type
= isl_obj_union_set
;
1365 list
->obj
[1].v
= must_no_source
;
1368 obj
.type
= isl_obj_list
;
1372 isl_list_free(list
);
1373 isl_union_map_free(may_source
);
1374 isl_union_map_free(must_source
);
1375 isl_union_map_free(sink
);
1376 isl_union_map_free(schedule
);
1378 obj
.type
= isl_obj_none
;
1383 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1385 struct isl_token
*tok
;
1387 if (isl_stream_eat_if_available(s
, '+'))
1388 return transitive_closure(s
->ctx
, obj
);
1390 tok
= isl_stream_next_token(s
);
1391 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1392 isl_stream_error(s
, tok
, "expecting -1");
1394 isl_stream_push_token(s
, tok
);
1397 isl_token_free(tok
);
1398 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1399 if (obj
.type
!= isl_obj_union_map
)
1400 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1402 obj
.v
= isl_union_map_reverse(obj
.v
);
1409 obj
.type
= isl_obj_none
;
1414 static struct isl_obj
read_from_file(struct isl_stream
*s
)
1417 struct isl_token
*tok
;
1418 struct isl_stream
*s_file
;
1421 tok
= isl_stream_next_token(s
);
1422 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1423 isl_stream_error(s
, tok
, "expecting filename");
1424 isl_token_free(tok
);
1428 file
= fopen(tok
->u
.s
, "r");
1429 isl_token_free(tok
);
1430 isl_assert(s
->ctx
, file
, goto error
);
1432 s_file
= isl_stream_new_file(s
->ctx
, file
);
1438 obj
= isl_stream_read_obj(s_file
);
1440 isl_stream_free(s_file
);
1445 obj
.type
= isl_obj_none
;
1450 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
1452 struct isl_token
*tok
;
1453 struct isl_obj obj
= { isl_obj_none
, NULL
};
1455 tok
= isl_stream_next_token(s
);
1458 if (tok
->type
== ISL_TOKEN_STRING
) {
1460 str
= isl_str_alloc(s
->ctx
);
1463 str
->s
= strdup(tok
->u
.s
);
1464 isl_token_free(tok
);
1466 obj
.type
= isl_obj_str
;
1468 isl_stream_push_token(s
, tok
);
1471 isl_token_free(tok
);
1475 static struct isl_obj
read_obj(struct isl_stream
*s
,
1476 struct isl_hash_table
*table
)
1478 struct isl_obj obj
= { isl_obj_none
, NULL
};
1480 struct isc_un_op
*op
= NULL
;
1482 obj
= read_string_if_available(s
);
1485 if (isl_stream_eat_if_available(s
, '(')) {
1486 obj
= read_expr(s
, table
);
1487 if (!obj
.v
|| isl_stream_eat(s
, ')'))
1490 op
= read_prefix_un_op_if_available(s
);
1492 return read_un_op_expr(s
, table
, op
);
1494 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
1495 return read_from_file(s
);
1496 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
1497 return vertices(s
, table
);
1498 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
1499 return any(s
, table
);
1500 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
1501 return last(s
, table
);
1503 name
= isl_stream_read_ident_if_available(s
);
1505 obj
= stored_obj(s
->ctx
, table
, name
);
1507 obj
= isl_stream_read_obj(s
);
1512 if (isl_stream_eat_if_available(s
, '^'))
1513 obj
= power(s
, obj
);
1514 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
1515 obj
= obj_at_index(s
, obj
);
1516 else if (is_subtype(obj
, isl_obj_union_map
) &&
1517 isl_stream_eat_if_available(s
, '(')) {
1518 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1519 obj
= apply(s
, obj
.v
, table
);
1520 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
1521 isl_stream_eat_if_available(s
, '(')) {
1522 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
1523 obj
= apply_fun(s
, obj
, table
);
1524 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
1525 isl_stream_eat_if_available(s
, '(')) {
1526 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
1527 obj
= apply_fun(s
, obj
, table
);
1533 obj
.type
= isl_obj_none
;
1538 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
1539 struct isl_obj lhs
, struct isl_obj rhs
)
1543 for (i
= 0; ; ++i
) {
1546 if (bin_ops
[i
].op
!= like
->op
)
1548 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1550 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
1556 for (i
= 0; ; ++i
) {
1557 if (!named_bin_ops
[i
].name
)
1559 if (named_bin_ops
[i
].op
.op
!= like
->op
)
1561 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1563 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
1566 return &named_bin_ops
[i
].op
;
1572 static struct isl_obj
read_expr(struct isl_stream
*s
,
1573 struct isl_hash_table
*table
)
1575 struct isl_obj obj
= { isl_obj_none
, NULL
};
1576 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
1578 obj
= read_obj(s
, table
);
1580 struct isc_bin_op
*op
= NULL
;
1582 op
= read_bin_op_if_available(s
, obj
);
1586 right_obj
= read_obj(s
, table
);
1588 op
= find_matching_bin_op(op
, obj
, right_obj
);
1590 isl_assert(s
->ctx
, op
, goto error
);
1591 obj
= convert(s
->ctx
, obj
, op
->lhs
);
1592 right_obj
= convert(s
->ctx
, right_obj
, op
->rhs
);
1593 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
1599 free_obj(right_obj
);
1601 obj
.type
= isl_obj_none
;
1606 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
1607 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
1609 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
1610 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1612 struct isl_obj obj
= { isl_obj_none
, NULL
};
1615 struct isc_bin_op
*op
= NULL
;
1619 if (isl_stream_is_empty(s
))
1622 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
1623 return source_file(s
, table
, p
);
1625 assign
= is_assign(s
);
1627 lhs
= isl_stream_read_ident_if_available(s
);
1628 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
1632 obj
= read_expr(s
, table
);
1633 if (obj
.type
== isl_obj_none
|| obj
.v
== NULL
)
1635 if (isl_stream_eat(s
, ';'))
1639 if (do_assign(s
->ctx
, table
, lhs
, obj
))
1642 p
= obj
.type
->print(p
, obj
.v
);
1643 p
= isl_printer_end_line(p
);
1649 isl_stream_flush_tokens(s
);
1650 isl_stream_skip_line(s
);
1656 int free_cb(void **entry
, void *user
)
1658 struct isl_named_obj
*named
= *entry
;
1660 free_obj(named
->obj
);
1667 static void register_named_ops(struct isl_stream
*s
)
1671 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
1672 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
1673 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
1676 for (i
= 0; ; ++i
) {
1677 if (!named_un_ops
[i
].name
)
1679 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
1680 named_un_ops
[i
].name
);
1681 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
1684 for (i
= 0; ; ++i
) {
1685 if (!named_bin_ops
[i
].name
)
1687 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
1688 named_bin_ops
[i
].name
);
1689 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
1693 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
1694 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1696 struct isl_token
*tok
;
1697 struct isl_stream
*s_file
;
1700 tok
= isl_stream_next_token(s
);
1701 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1702 isl_stream_error(s
, tok
, "expecting filename");
1703 isl_token_free(tok
);
1707 file
= fopen(tok
->u
.s
, "r");
1708 isl_token_free(tok
);
1709 isl_assert(s
->ctx
, file
, return p
);
1711 s_file
= isl_stream_new_file(s
->ctx
, file
);
1717 register_named_ops(s_file
);
1719 while (!s_file
->eof
)
1720 p
= read_line(s_file
, table
, p
);
1722 isl_stream_free(s_file
);
1725 isl_stream_eat(s
, ';');
1730 int main(int argc
, char **argv
)
1732 struct isl_ctx
*ctx
;
1733 struct isl_stream
*s
;
1734 struct isl_hash_table
*table
;
1735 struct iscc_options
*options
;
1738 options
= iscc_options_new_with_defaults();
1740 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
1742 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
1743 s
= isl_stream_new_file(ctx
, stdin
);
1745 table
= isl_hash_table_alloc(ctx
, 10);
1747 p
= isl_printer_to_file(ctx
, stdout
);
1748 p
= isl_printer_set_output_format(p
, options
->format
);
1751 register_named_ops(s
);
1753 while (p
&& !s
->eof
) {
1754 p
= read_line(s
, table
, p
);
1757 isl_printer_free(p
);
1758 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
1759 isl_hash_table_free(ctx
, table
);