5 #include <isl_stream.h>
6 #include <isl_vertices.h>
7 #include <isl_obj_list.h>
8 #include <barvinok/barvinok.h>
13 #include <cloog/isl/cloog.h>
16 static int isl_bool_false
= 0;
17 static int isl_bool_true
= 1;
18 static int isl_bool_error
= -1;
20 static enum isl_token_type read_op
;
21 static enum isl_token_type vertices_op
;
23 struct isl_arg_choice iscc_format
[] = {
24 {"isl", ISL_FORMAT_ISL
},
25 {"omega", ISL_FORMAT_OMEGA
},
26 {"polylib", ISL_FORMAT_POLYLIB
},
27 {"latex", ISL_FORMAT_LATEX
},
33 struct barvinok_options
*barvinok
;
37 struct isl_arg iscc_options_arg
[] = {
38 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
40 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
41 iscc_format
, ISL_FORMAT_ISL
, "output format")
45 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
47 static void *isl_obj_bool_copy(void *v
)
52 static void isl_obj_bool_free(void *v
)
56 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
59 if (v
== &isl_bool_true
)
60 return isl_printer_print_str(p
, "True");
61 else if (v
== &isl_bool_false
)
62 return isl_printer_print_str(p
, "False");
64 return isl_printer_print_str(p
, "Error");
67 static void *isl_obj_bool_add(void *v1
, void *v2
)
72 struct isl_obj_vtable isl_obj_bool_vtable
= {
78 #define isl_obj_bool (&isl_obj_bool_vtable)
80 int *isl_bool_from_int(int res
)
82 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
85 int *union_map_is_equal(__isl_take isl_union_map
*map1
,
86 __isl_take isl_union_map
*map2
)
88 int res
= isl_union_map_is_equal(map1
, map2
);
89 isl_union_map_free(map1
);
90 isl_union_map_free(map2
);
91 return isl_bool_from_int(res
);
93 int *union_set_is_equal(__isl_take isl_union_set
*set1
,
94 __isl_take isl_union_set
*set2
)
96 return union_map_is_equal((isl_union_map
*)set1
, (isl_union_map
*)set2
);
99 int *union_map_is_subset(__isl_take isl_union_map
*map1
,
100 __isl_take isl_union_map
*map2
)
102 int res
= isl_union_map_is_subset(map1
, map2
);
103 isl_union_map_free(map1
);
104 isl_union_map_free(map2
);
105 return isl_bool_from_int(res
);
107 int *union_set_is_subset(__isl_take isl_union_set
*set1
,
108 __isl_take isl_union_set
*set2
)
110 return union_map_is_subset((isl_union_map
*)set1
, (isl_union_map
*)set2
);
113 int *union_map_is_strict_subset(__isl_take isl_union_map
*map1
,
114 __isl_take isl_union_map
*map2
)
116 int res
= isl_union_map_is_strict_subset(map1
, map2
);
117 isl_union_map_free(map1
);
118 isl_union_map_free(map2
);
119 return isl_bool_from_int(res
);
121 int *union_set_is_strict_subset(__isl_take isl_union_set
*set1
,
122 __isl_take isl_union_set
*set2
)
124 return union_map_is_strict_subset((isl_union_map
*)set1
,
125 (isl_union_map
*)set2
);
128 int *union_map_is_superset(__isl_take isl_union_map
*map1
,
129 __isl_take isl_union_map
*map2
)
131 return union_map_is_subset(map2
, map1
);
133 int *union_set_is_superset(__isl_take isl_union_set
*set1
,
134 __isl_take isl_union_set
*set2
)
136 return union_set_is_subset(set2
, set1
);
139 int *union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
140 __isl_take isl_union_map
*map2
)
142 return union_map_is_strict_subset(map2
, map1
);
144 int *union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
145 __isl_take isl_union_set
*set2
)
147 return union_set_is_strict_subset(set2
, set1
);
150 extern struct isl_obj_vtable isl_obj_list_vtable
;
151 #define isl_obj_list (&isl_obj_list_vtable)
153 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
155 enum isl_token_type op
;
161 struct isc_named_bin_op
{
163 struct isc_bin_op op
;
167 isl_union_pw_qpolynomial
*upwqp
;
168 isl_union_pw_qpolynomial
*res
;
171 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
173 struct iscc_at
*at
= (struct iscc_at
*) user
;
177 set
= isl_set_from_point(isl_point_copy(pnt
));
178 qp
= isl_union_pw_qpolynomial_eval(
179 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
181 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
182 isl_union_pw_qpolynomial_from_pw_qpolynomial(
183 isl_pw_qpolynomial_alloc(set
, qp
)));
188 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
189 __isl_take isl_union_pw_qpolynomial
*upwqp
,
190 __isl_take isl_union_set
*uset
)
195 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
197 isl_union_set_foreach_point(uset
, eval_at
, &at
);
199 isl_union_pw_qpolynomial_free(upwqp
);
200 isl_union_set_free(uset
);
205 struct iscc_fold_at
{
206 isl_union_pw_qpolynomial_fold
*upwf
;
207 isl_union_pw_qpolynomial
*res
;
210 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
212 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
216 set
= isl_set_from_point(isl_point_copy(pnt
));
217 qp
= isl_union_pw_qpolynomial_fold_eval(
218 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
220 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
221 isl_union_pw_qpolynomial_from_pw_qpolynomial(
222 isl_pw_qpolynomial_alloc(set
, qp
)));
227 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
228 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
229 __isl_take isl_union_set
*uset
)
231 struct iscc_fold_at at
;
234 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
236 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
238 isl_union_pw_qpolynomial_fold_free(upwf
);
239 isl_union_set_free(uset
);
244 struct isc_bin_op bin_ops
[] = {
245 { '+', isl_obj_union_set
, isl_obj_union_set
,
247 (isc_bin_op_fn
) &isl_union_set_union
},
248 { '+', isl_obj_union_map
, isl_obj_union_map
,
250 (isc_bin_op_fn
) &isl_union_map_union
},
251 { '-', isl_obj_union_set
, isl_obj_union_set
,
253 (isc_bin_op_fn
) &isl_union_set_subtract
},
254 { '-', isl_obj_union_map
, isl_obj_union_map
,
256 (isc_bin_op_fn
) &isl_union_map_subtract
},
257 { '*', isl_obj_union_set
, isl_obj_union_set
,
259 (isc_bin_op_fn
) &isl_union_set_intersect
},
260 { '*', isl_obj_union_map
, isl_obj_union_map
,
262 (isc_bin_op_fn
) &isl_union_map_intersect
},
263 { '*', isl_obj_union_map
, isl_obj_union_set
,
265 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
266 { '.', isl_obj_union_map
, isl_obj_union_map
,
268 (isc_bin_op_fn
) &isl_union_map_apply_range
},
269 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
270 isl_obj_union_pw_qpolynomial
,
271 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
272 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
274 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
275 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
276 (isc_bin_op_fn
) &union_set_is_equal
},
277 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
278 (isc_bin_op_fn
) &union_map_is_equal
},
279 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
280 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_subset
},
281 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
282 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_subset
},
283 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
284 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_subset
},
285 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
286 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_subset
},
287 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
288 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_superset
},
289 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
290 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_superset
},
291 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
292 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_superset
},
293 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
294 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_superset
},
295 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
296 isl_obj_union_pw_qpolynomial
,
297 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
298 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
299 isl_obj_union_pw_qpolynomial
,
300 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
301 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
302 isl_obj_union_pw_qpolynomial
,
303 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
304 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
305 isl_obj_union_pw_qpolynomial
,
306 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
307 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
308 isl_obj_union_pw_qpolynomial_fold
,
309 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
310 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
311 isl_obj_union_pw_qpolynomial
,
312 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
313 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
314 isl_obj_union_pw_qpolynomial
,
315 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
316 { '%', isl_obj_union_set
, isl_obj_union_set
,
318 (isc_bin_op_fn
) &isl_union_set_gist
},
319 { '%', isl_obj_union_map
, isl_obj_union_map
,
321 (isc_bin_op_fn
) &isl_union_map_gist
},
322 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
323 isl_obj_union_pw_qpolynomial
,
324 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
325 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
326 isl_obj_union_pw_qpolynomial_fold
,
327 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
330 struct isc_named_bin_op named_bin_ops
[] = {
331 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
333 (isc_bin_op_fn
) &isl_union_set_product
} },
334 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
336 (isc_bin_op_fn
) &isl_union_map_product
} },
340 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
342 return isl_set_from_basic_set(isl_union_set_sample(uset
));
345 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
347 return isl_map_from_basic_map(isl_union_map_sample(umap
));
350 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
351 __isl_take isl_union_pw_qpolynomial
*upwqp
)
354 struct isl_list
*list
;
357 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
358 list
= isl_list_alloc(ctx
, 2);
362 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
363 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
364 isl_fold_max
, &tight
);
365 list
->obj
[1].type
= isl_obj_bool
;
366 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
367 if (tight
< 0 || !list
->obj
[0].v
)
372 isl_union_pw_qpolynomial_free(upwqp
);
379 struct iscc_codegen_data
{
387 CloogBlockList
**nextBL
;
388 CloogScatteringList
*scatterings
;
389 CloogScatteringList
**nextScat
;
392 static int add_domain(__isl_take isl_map
*map
, void *user
)
394 struct iscc_codegen_data
*data
= (struct iscc_codegen_data
*)user
;
395 isl_ctx
*ctx
= isl_map_get_ctx(map
);
398 CloogStatement
*statement
;
400 CloogScatteringList
*s
;
403 n_in
= isl_map_dim(map
, isl_dim_in
);
405 isl_dim
*dim
= isl_map_get_dim(map
);
406 data
->nparam
= isl_map_dim(map
, isl_dim_param
);
407 data
->n_scat
= isl_map_dim(map
, isl_dim_out
);
408 dim
= isl_dim_drop_inputs(dim
, 0, n_in
);
409 dim
= isl_dim_drop_outputs(dim
, 0, data
->n_scat
);
410 data
->context
= isl_set_universe(dim
);
412 isl_assert(ctx
, data
->nparam
== isl_map_dim(map
, isl_dim_param
),
414 isl_assert(ctx
, data
->n_scat
== isl_map_dim(map
, isl_dim_out
),
417 if (n_in
> data
->max_iter
)
418 data
->max_iter
= n_in
;
420 l
= cloog_loop_malloc(data
->state
);
425 domain
= isl_map_domain(isl_map_copy(map
));
426 l
->domain
= cloog_domain_from_isl_set(domain
);
428 statement
= cloog_statement_alloc(data
->state
, data
->nr
);
429 statement
->usr
= NULL
;
430 dim
= isl_map_get_dim(map
);
431 statement
->name
= strdup(isl_dim_get_tuple_name(dim
, isl_dim_in
));
433 l
->block
= cloog_block_alloc(statement
, 0, NULL
, n_in
);
435 s
= isl_alloc_type(ctx
, CloogScatteringList
);
436 s
->scatt
= cloog_scattering_from_isl_map(map
);
440 *data
->nextBL
= cloog_block_list_alloc((*data
->next
)->block
);
441 data
->next
= &(*data
->next
)->next
;
442 data
->nextBL
= &(*data
->nextBL
)->next
;
444 data
->nextScat
= &(s
->next
);
453 char **generate_names(isl_ctx
*ctx
, int n
, const char *prefix
)
458 names
= isl_alloc_array(ctx
, char *, n
);
459 for (i
= 0; i
< n
; i
++) {
460 names
[i
] = isl_alloc_array(ctx
, char, 50);
461 sprintf(names
[i
], "%s%d", prefix
, i
);
467 void *codegen(void *arg
)
470 struct iscc_codegen_data data
;
472 isl_union_map
*umap
= (isl_union_map
*)arg
;
473 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
474 CloogOptions
*options
;
476 struct clast_stmt
*stmt
;
478 data
.state
= cloog_isl_state_malloc(ctx
);
479 options
= cloog_options_malloc(data
.state
);
480 options
->language
= LANGUAGE_C
;
481 p
= cloog_program_malloc();
483 isl_assert(ctx
, p
, goto error
);
484 p
->names
= cloog_names_malloc();
485 isl_assert(ctx
, p
->names
, goto error2
);
493 data
.next
= &p
->loop
;
494 data
.nextBL
= &p
->blocklist
;
495 data
.scatterings
= NULL
;
496 data
.nextScat
= &data
.scatterings
;
498 if (isl_union_map_foreach_map(umap
, &add_domain
, &data
) < 0)
504 p
->names
->nb_parameters
= data
.nparam
;
505 p
->names
->parameters
= isl_alloc_array(ctx
, char *, data
.nparam
);
506 dim
= isl_set_get_dim(data
.context
);
507 p
->context
= cloog_domain_from_isl_set(data
.context
);
508 for (i
= 0; i
< data
.nparam
; ++i
) {
509 const char *s
= isl_dim_get_name(dim
, isl_dim_param
, i
);
510 p
->names
->parameters
[i
] = strdup(s
);
514 p
->names
->nb_iterators
= data
.max_iter
;
515 p
->names
->iterators
= generate_names(ctx
, data
.max_iter
, "i");
517 p
->names
->nb_scattering
= data
.n_scat
;
518 p
->names
->scattering
= generate_names(ctx
, data
.n_scat
, "c");
519 p
->names
->nb_scalars
= 0;
520 p
->nb_scattdims
= data
.n_scat
;
521 p
->scaldims
= (int *)malloc(p
->nb_scattdims
*(sizeof(int)));
522 for (i
= 0; i
< p
->nb_scattdims
; i
++)
525 cloog_program_scatter(p
, data
.scatterings
, options
);
526 p
= cloog_program_generate(p
, options
);
528 stmt
= cloog_clast_create(p
, options
);
529 clast_pprint(stdout
, stmt
, 0, options
);
530 cloog_clast_free(stmt
);
533 cloog_scattering_list_free(data
.scatterings
);
535 cloog_program_free(p
);
537 cloog_options_free(options
);
538 cloog_state_free(data
.state
);
539 isl_union_map_free(umap
);
544 typedef void *(*isc_un_op_fn
)(void *arg
);
546 enum isl_token_type op
;
551 struct isc_named_un_op
{
555 struct isc_named_un_op named_un_ops
[] = {
556 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
557 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
558 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
559 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
560 {"card", { -1, isl_obj_union_set
,
561 isl_obj_union_pw_qpolynomial
,
562 (isc_un_op_fn
) &isl_union_set_card
} },
563 {"card", { -1, isl_obj_union_map
,
564 isl_obj_union_pw_qpolynomial
,
565 (isc_un_op_fn
) &isl_union_map_card
} },
566 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
567 (isc_un_op_fn
) &isl_union_set_coalesce
} },
568 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
569 (isc_un_op_fn
) &isl_union_map_coalesce
} },
570 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
571 isl_obj_union_pw_qpolynomial
,
572 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
573 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
574 isl_obj_union_pw_qpolynomial_fold
,
575 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
577 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
, &codegen
} },
579 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
580 (isc_un_op_fn
) &isl_union_map_deltas
} },
581 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
582 (isc_un_op_fn
) &isl_union_map_domain
} },
583 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
584 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
585 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
587 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
588 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
589 (isc_un_op_fn
) &isl_union_map_range
} },
590 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
591 (isc_un_op_fn
) &isl_union_map_lexmin
} },
592 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
593 (isc_un_op_fn
) &isl_union_map_lexmax
} },
594 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
595 (isc_un_op_fn
) &isl_union_set_lexmin
} },
596 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
597 (isc_un_op_fn
) &isl_union_set_lexmax
} },
598 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
599 (isc_un_op_fn
) &union_set_sample
} },
600 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
601 (isc_un_op_fn
) &union_map_sample
} },
602 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
603 isl_obj_union_pw_qpolynomial
,
604 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
605 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
606 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
607 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
608 (isc_un_op_fn
) &isl_union_set_unwrap
} },
609 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
610 (isc_un_op_fn
) &isl_union_map_wrap
} },
614 struct isl_named_obj
{
619 static void free_obj(struct isl_obj obj
)
621 obj
.type
->free(obj
.v
);
624 static int same_name(const void *entry
, const void *val
)
626 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
628 return !strcmp(named
->name
, val
);
631 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
632 char *name
, struct isl_obj obj
)
634 struct isl_hash_table_entry
*entry
;
636 struct isl_named_obj
*named
;
638 name_hash
= isl_hash_string(isl_hash_init(), name
);
639 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
644 free_obj(named
->obj
);
647 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
662 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
663 struct isl_hash_table
*table
, char *name
)
665 struct isl_obj obj
= { isl_obj_none
, NULL
};
666 struct isl_hash_table_entry
*entry
;
669 name_hash
= isl_hash_string(isl_hash_init(), name
);
670 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
672 struct isl_named_obj
*named
;
678 obj
.v
= obj
.type
->copy(obj
.v
);
682 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
684 if (obj
.type
== super
)
686 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
688 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
690 if (obj
.type
== isl_obj_pw_qpolynomial
&&
691 super
== isl_obj_union_pw_qpolynomial
)
693 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
694 super
== isl_obj_union_pw_qpolynomial_fold
)
696 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
701 static struct isl_obj
convert(struct isl_obj obj
, isl_obj_type type
)
703 if (obj
.type
== type
)
705 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
706 obj
.type
= isl_obj_union_map
;
707 obj
.v
= isl_union_map_from_map(obj
.v
);
710 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
711 obj
.type
= isl_obj_union_set
;
712 obj
.v
= isl_union_set_from_set(obj
.v
);
715 if (obj
.type
== isl_obj_pw_qpolynomial
&&
716 type
== isl_obj_union_pw_qpolynomial
) {
717 obj
.type
= isl_obj_union_pw_qpolynomial
;
718 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
721 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
722 type
== isl_obj_union_pw_qpolynomial_fold
) {
723 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
724 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
727 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
728 if (type
== isl_obj_union_map
) {
729 obj
.type
= isl_obj_union_map
;
732 if (type
== isl_obj_union_pw_qpolynomial
) {
733 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
734 isl_union_set_free(obj
.v
);
735 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
736 obj
.type
= isl_obj_union_pw_qpolynomial
;
739 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
740 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
741 isl_union_set_free(obj
.v
);
742 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
);
743 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
748 obj
.type
= isl_obj_none
;
753 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
757 struct isl_token
*tok
;
759 tok
= isl_stream_next_token(s
);
766 if (bin_ops
[i
].op
!= tok
->type
)
768 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
776 if (!named_bin_ops
[i
].name
)
778 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
780 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
784 return &named_bin_ops
[i
].op
;
787 isl_stream_push_token(s
, tok
);
792 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
795 struct isl_token
*tok
;
797 tok
= isl_stream_next_token(s
);
802 if (!named_un_ops
[i
].name
)
804 if (named_un_ops
[i
].op
.op
!= tok
->type
)
808 return &named_un_ops
[i
].op
;
811 isl_stream_push_token(s
, tok
);
816 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
822 if (!named_un_ops
[i
].name
)
824 if (named_un_ops
[i
].op
.op
!= like
->op
)
826 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
829 return &named_un_ops
[i
].op
;
835 static int is_assign(struct isl_stream
*s
)
837 struct isl_token
*tok
;
838 struct isl_token
*tok2
;
841 tok
= isl_stream_next_token(s
);
844 if (tok
->type
!= ISL_TOKEN_IDENT
) {
845 isl_stream_push_token(s
, tok
);
849 tok2
= isl_stream_next_token(s
);
851 isl_stream_push_token(s
, tok
);
854 assign
= tok2
->type
== ISL_TOKEN_DEF
;
855 isl_stream_push_token(s
, tok2
);
856 isl_stream_push_token(s
, tok
);
861 static struct isl_obj
read_obj(struct isl_stream
*s
,
862 struct isl_hash_table
*table
);
863 static struct isl_obj
read_expr(struct isl_stream
*s
,
864 struct isl_hash_table
*table
);
866 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
867 struct isl_hash_table
*table
, struct isc_un_op
*op
)
869 struct isl_obj obj
= { isl_obj_none
, NULL
};
871 obj
= read_obj(s
, table
);
875 op
= find_matching_un_op(op
, obj
);
877 isl_assert(s
->ctx
, op
, goto error
);
878 obj
= convert(obj
, op
->arg
);
879 obj
.v
= op
->fn(obj
.v
);
885 obj
.type
= isl_obj_none
;
890 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
892 struct isl_list
*list
;
895 if (obj
.type
== isl_obj_map
)
896 obj
= convert(obj
, isl_obj_union_map
);
897 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
898 list
= isl_list_alloc(ctx
, 2);
902 list
->obj
[0].type
= isl_obj_union_map
;
903 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
904 list
->obj
[1].type
= isl_obj_bool
;
905 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
907 obj
.type
= isl_obj_list
;
908 if (exact
< 0 || !list
->obj
[0].v
)
914 obj
.type
= isl_obj_none
;
919 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
921 struct isl_list
*list
= obj
.v
;
922 struct isl_token
*tok
;
925 tok
= isl_stream_next_token(s
);
926 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
927 isl_stream_error(s
, tok
, "expecting index");
929 isl_stream_push_token(s
, tok
);
932 i
= isl_int_get_si(tok
->u
.v
);
934 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
935 if (isl_stream_eat(s
, ']'))
939 obj
.v
= obj
.type
->copy(obj
.v
);
946 obj
.type
= isl_obj_none
;
951 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
952 struct isl_hash_table
*table
)
956 obj
= read_expr(s
, table
);
957 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
958 is_subtype(obj
, isl_obj_union_map
), goto error
);
960 if (obj
.type
== isl_obj_set
)
961 obj
= convert(obj
, isl_obj_union_set
);
962 else if (obj
.type
== isl_obj_map
)
963 obj
= convert(obj
, isl_obj_union_map
);
964 if (obj
.type
== isl_obj_union_set
) {
965 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
967 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
971 if (isl_stream_eat(s
, ')'))
976 isl_union_map_free(umap
);
979 obj
.type
= isl_obj_none
;
984 struct add_vertex_data
{
985 struct isl_list
*list
;
989 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
991 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
994 expr
= isl_vertex_get_expr(vertex
);
996 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
997 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1000 isl_vertex_free(vertex
);
1005 static struct isl_obj
vertices(struct isl_stream
*s
,
1006 struct isl_hash_table
*table
)
1011 isl_basic_set
*hull
;
1012 isl_vertices
*vertices
= NULL
;
1013 struct isl_list
*list
= NULL
;
1014 struct add_vertex_data data
;
1016 obj
= read_expr(s
, table
);
1017 isl_assert(s
->ctx
, obj
.type
== isl_obj_set
, goto error
);
1020 set
= isl_set_remove_divs(set
);
1021 hull
= isl_set_convex_hull(set
);
1022 vertices
= isl_basic_set_compute_vertices(hull
);
1023 isl_basic_set_free(hull
);
1025 ctx
= isl_vertices_get_ctx(vertices
);
1026 list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1033 if (isl_vertices_foreach_vertex(vertices
, &add_vertex
, &data
) < 0)
1036 isl_vertices_free(vertices
);
1038 obj
.type
= isl_obj_list
;
1043 isl_vertices_free(vertices
);
1044 isl_list_free(list
);
1046 obj
.type
= isl_obj_none
;
1051 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1053 struct isl_token
*tok
;
1055 if (isl_stream_eat_if_available(s
, '+'))
1056 return transitive_closure(s
->ctx
, obj
);
1058 tok
= isl_stream_next_token(s
);
1059 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1060 isl_stream_error(s
, tok
, "expecting -1");
1062 isl_stream_push_token(s
, tok
);
1065 isl_token_free(tok
);
1066 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1067 if (obj
.type
!= isl_obj_union_map
)
1068 obj
= convert(obj
, isl_obj_union_map
);
1070 obj
.v
= isl_union_map_reverse(obj
.v
);
1077 obj
.type
= isl_obj_none
;
1082 static struct isl_obj
read_from_file(struct isl_stream
*s
)
1085 struct isl_token
*tok
;
1086 struct isl_stream
*s_file
;
1089 tok
= isl_stream_next_token(s
);
1090 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1091 isl_stream_error(s
, tok
, "expecting filename");
1092 isl_token_free(tok
);
1096 file
= fopen(tok
->u
.s
, "r");
1097 isl_token_free(tok
);
1098 isl_assert(s
->ctx
, file
, goto error
);
1100 s_file
= isl_stream_new_file(s
->ctx
, file
);
1106 obj
= isl_stream_read_obj(s_file
);
1108 isl_stream_free(s_file
);
1113 obj
.type
= isl_obj_none
;
1118 static struct isl_obj
read_obj(struct isl_stream
*s
,
1119 struct isl_hash_table
*table
)
1121 struct isl_obj obj
= { isl_obj_none
, NULL
};
1123 struct isc_un_op
*op
= NULL
;
1125 if (isl_stream_eat_if_available(s
, '(')) {
1126 obj
= read_expr(s
, table
);
1127 if (!obj
.v
|| isl_stream_eat(s
, ')'))
1130 op
= read_prefix_un_op_if_available(s
);
1132 return read_un_op_expr(s
, table
, op
);
1134 if (isl_stream_eat_if_available(s
, read_op
))
1135 return read_from_file(s
);
1136 if (isl_stream_eat_if_available(s
, vertices_op
))
1137 return vertices(s
, table
);
1139 name
= isl_stream_read_ident_if_available(s
);
1141 obj
= stored_obj(s
->ctx
, table
, name
);
1143 obj
= isl_stream_read_obj(s
);
1148 if (isl_stream_eat_if_available(s
, '^'))
1149 obj
= power(s
, obj
);
1150 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
1151 obj
= obj_at_index(s
, obj
);
1152 else if (is_subtype(obj
, isl_obj_union_map
) &&
1153 isl_stream_eat_if_available(s
, '(')) {
1154 obj
= convert(obj
, isl_obj_union_map
);
1155 obj
= apply(s
, obj
.v
, table
);
1161 obj
.type
= isl_obj_none
;
1166 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
1167 struct isl_obj lhs
, struct isl_obj rhs
)
1171 for (i
= 0; ; ++i
) {
1174 if (bin_ops
[i
].op
!= like
->op
)
1176 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1178 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
1184 for (i
= 0; ; ++i
) {
1185 if (!named_bin_ops
[i
].name
)
1187 if (named_bin_ops
[i
].op
.op
!= like
->op
)
1189 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1191 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
1194 return &named_bin_ops
[i
].op
;
1200 static struct isl_obj
read_expr(struct isl_stream
*s
,
1201 struct isl_hash_table
*table
)
1203 struct isl_obj obj
= { isl_obj_none
, NULL
};
1204 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
1206 obj
= read_obj(s
, table
);
1208 struct isc_bin_op
*op
= NULL
;
1210 op
= read_bin_op_if_available(s
, obj
);
1214 right_obj
= read_obj(s
, table
);
1216 op
= find_matching_bin_op(op
, obj
, right_obj
);
1218 isl_assert(s
->ctx
, op
, goto error
);
1219 obj
= convert(obj
, op
->lhs
);
1220 right_obj
= convert(right_obj
, op
->rhs
);
1221 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
1227 free_obj(right_obj
);
1229 obj
.type
= isl_obj_none
;
1234 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
1235 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1237 struct isl_obj obj
= { isl_obj_none
, NULL
};
1240 struct isc_bin_op
*op
= NULL
;
1244 if (isl_stream_is_empty(s
))
1247 assign
= is_assign(s
);
1249 lhs
= isl_stream_read_ident_if_available(s
);
1250 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
1254 obj
= read_expr(s
, table
);
1255 if (obj
.type
== isl_obj_none
|| obj
.v
== NULL
)
1257 if (isl_stream_eat(s
, ';'))
1261 if (do_assign(s
->ctx
, table
, lhs
, obj
))
1264 p
= obj
.type
->print(p
, obj
.v
);
1265 p
= isl_printer_end_line(p
);
1271 isl_stream_flush_tokens(s
);
1272 isl_stream_skip_line(s
);
1278 int free_cb(void **entry
, void *user
)
1280 struct isl_named_obj
*named
= *entry
;
1282 free_obj(named
->obj
);
1289 static void register_named_ops(struct isl_stream
*s
)
1293 read_op
= isl_stream_register_keyword(s
, "read");
1294 assert(read_op
!= ISL_TOKEN_ERROR
);
1295 vertices_op
= isl_stream_register_keyword(s
, "vertices");
1296 assert(vertices_op
!= ISL_TOKEN_ERROR
);
1298 for (i
= 0; ; ++i
) {
1299 if (!named_un_ops
[i
].name
)
1301 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
1302 named_un_ops
[i
].name
);
1303 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
1306 for (i
= 0; ; ++i
) {
1307 if (!named_bin_ops
[i
].name
)
1309 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
1310 named_bin_ops
[i
].name
);
1311 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
1315 int main(int argc
, char **argv
)
1317 struct isl_ctx
*ctx
;
1318 struct isl_stream
*s
;
1319 struct isl_hash_table
*table
;
1320 struct iscc_options
*options
;
1323 options
= iscc_options_new_with_defaults();
1325 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
1327 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
1328 s
= isl_stream_new_file(ctx
, stdin
);
1330 table
= isl_hash_table_alloc(ctx
, 10);
1332 p
= isl_printer_to_file(ctx
, stdout
);
1333 p
= isl_printer_set_output_format(p
, options
->format
);
1336 register_named_ops(s
);
1339 p
= read_line(s
, table
, p
);
1342 isl_printer_free(p
);
1343 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
1344 isl_hash_table_free(ctx
, table
);