7 #include <isl/stream.h>
8 #include <isl/vertices.h>
10 #include <isl/schedule.h>
11 #include <isl_obj_list.h>
12 #include <isl_obj_str.h>
13 #include <barvinok/isl.h>
14 #include <barvinok/options.h>
21 static isl_ctx
*main_ctx
;
23 static void handler(int signum
)
25 if (isl_ctx_aborted(main_ctx
))
27 isl_ctx_abort(main_ctx
);
30 static struct sigaction sa_old
;
32 static void install_signal_handler(isl_ctx
*ctx
)
38 memset(&sa
, 0, sizeof(struct sigaction
));
39 sa
.sa_handler
= &handler
;
40 sa
.sa_flags
= SA_RESTART
;
41 sigaction(SIGINT
, &sa
, &sa_old
);
44 static void remove_signal_handler(isl_ctx
*ctx
)
46 sigaction(SIGINT
, &sa_old
, NULL
);
51 static void install_signal_handler(isl_ctx
*ctx
)
55 static void remove_signal_handler(isl_ctx
*ctx
)
62 #include <cloog/isl/cloog.h>
65 static int isl_bool_false
= 0;
66 static int isl_bool_true
= 1;
67 static int isl_bool_error
= -1;
69 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
70 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
71 ISCC_SCHEDULE
, ISCC_SCHEDULE_BANDS
,
72 ISCC_MINIMIZING
, ISCC_RESPECTING
,
73 ISCC_TYPEOF
, ISCC_PRINT
,
75 static const char *op_name
[ISCC_N_OP
] = {
77 [ISCC_WRITE
] = "write",
78 [ISCC_PRINT
] = "print",
79 [ISCC_SOURCE
] = "source",
80 [ISCC_VERTICES
] = "vertices",
83 [ISCC_BEFORE
] = "before",
84 [ISCC_UNDER
] = "under",
85 [ISCC_SCHEDULE
] = "schedule",
86 [ISCC_SCHEDULE_BANDS
] = "schedule_bands",
87 [ISCC_MINIMIZING
] = "minimizing",
88 [ISCC_RESPECTING
] = "respecting",
89 [ISCC_TYPEOF
] = "typeof"
91 static enum isl_token_type iscc_op
[ISCC_N_OP
];
93 struct isl_arg_choice iscc_format
[] = {
94 {"isl", ISL_FORMAT_ISL
},
95 {"omega", ISL_FORMAT_OMEGA
},
96 {"polylib", ISL_FORMAT_POLYLIB
},
97 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
98 {"latex", ISL_FORMAT_LATEX
},
103 struct iscc_options
{
104 struct barvinok_options
*barvinok
;
109 struct isl_arg iscc_options_arg
[] = {
110 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
112 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
113 iscc_format
, ISL_FORMAT_ISL
, "output format")
114 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
115 "allow read and write operations")
119 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
120 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
122 static void *isl_obj_bool_copy(void *v
)
127 static void isl_obj_bool_free(void *v
)
131 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
134 if (v
== &isl_bool_true
)
135 return isl_printer_print_str(p
, "True");
136 else if (v
== &isl_bool_false
)
137 return isl_printer_print_str(p
, "False");
139 return isl_printer_print_str(p
, "Error");
142 static void *isl_obj_bool_add(void *v1
, void *v2
)
147 struct isl_obj_vtable isl_obj_bool_vtable
= {
153 #define isl_obj_bool (&isl_obj_bool_vtable)
155 int *isl_bool_from_int(int res
)
157 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
160 int *union_map_is_equal(__isl_take isl_union_map
*map1
,
161 __isl_take isl_union_map
*map2
)
163 int res
= isl_union_map_is_equal(map1
, map2
);
164 isl_union_map_free(map1
);
165 isl_union_map_free(map2
);
166 return isl_bool_from_int(res
);
168 int *union_set_is_equal(__isl_take isl_union_set
*set1
,
169 __isl_take isl_union_set
*set2
)
171 return union_map_is_equal((isl_union_map
*)set1
, (isl_union_map
*)set2
);
174 int *union_map_is_subset(__isl_take isl_union_map
*map1
,
175 __isl_take isl_union_map
*map2
)
177 int res
= isl_union_map_is_subset(map1
, map2
);
178 isl_union_map_free(map1
);
179 isl_union_map_free(map2
);
180 return isl_bool_from_int(res
);
182 int *union_set_is_subset(__isl_take isl_union_set
*set1
,
183 __isl_take isl_union_set
*set2
)
185 return union_map_is_subset((isl_union_map
*)set1
, (isl_union_map
*)set2
);
188 int *union_map_is_strict_subset(__isl_take isl_union_map
*map1
,
189 __isl_take isl_union_map
*map2
)
191 int res
= isl_union_map_is_strict_subset(map1
, map2
);
192 isl_union_map_free(map1
);
193 isl_union_map_free(map2
);
194 return isl_bool_from_int(res
);
196 int *union_set_is_strict_subset(__isl_take isl_union_set
*set1
,
197 __isl_take isl_union_set
*set2
)
199 return union_map_is_strict_subset((isl_union_map
*)set1
,
200 (isl_union_map
*)set2
);
203 int *union_map_is_superset(__isl_take isl_union_map
*map1
,
204 __isl_take isl_union_map
*map2
)
206 return union_map_is_subset(map2
, map1
);
208 int *union_set_is_superset(__isl_take isl_union_set
*set1
,
209 __isl_take isl_union_set
*set2
)
211 return union_set_is_subset(set2
, set1
);
214 int *union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
215 __isl_take isl_union_map
*map2
)
217 return union_map_is_strict_subset(map2
, map1
);
219 int *union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
220 __isl_take isl_union_set
*set2
)
222 return union_set_is_strict_subset(set2
, set1
);
225 extern struct isl_obj_vtable isl_obj_list_vtable
;
226 #define isl_obj_list (&isl_obj_list_vtable)
228 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
230 enum isl_token_type op
;
236 struct isc_named_bin_op
{
238 struct isc_bin_op op
;
242 isl_union_pw_qpolynomial
*upwqp
;
243 isl_union_pw_qpolynomial
*res
;
246 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
248 struct iscc_at
*at
= (struct iscc_at
*) user
;
252 set
= isl_set_from_point(isl_point_copy(pnt
));
253 qp
= isl_union_pw_qpolynomial_eval(
254 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
256 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
257 isl_union_pw_qpolynomial_from_pw_qpolynomial(
258 isl_pw_qpolynomial_alloc(set
, qp
)));
263 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
264 __isl_take isl_union_pw_qpolynomial
*upwqp
,
265 __isl_take isl_union_set
*uset
)
270 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
272 isl_union_set_foreach_point(uset
, eval_at
, &at
);
274 isl_union_pw_qpolynomial_free(upwqp
);
275 isl_union_set_free(uset
);
280 struct iscc_fold_at
{
281 isl_union_pw_qpolynomial_fold
*upwf
;
282 isl_union_pw_qpolynomial
*res
;
285 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
287 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
291 set
= isl_set_from_point(isl_point_copy(pnt
));
292 qp
= isl_union_pw_qpolynomial_fold_eval(
293 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
295 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
296 isl_union_pw_qpolynomial_from_pw_qpolynomial(
297 isl_pw_qpolynomial_alloc(set
, qp
)));
302 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
303 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
304 __isl_take isl_union_set
*uset
)
306 struct iscc_fold_at at
;
309 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_dim(uset
));
311 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
313 isl_union_pw_qpolynomial_fold_free(upwf
);
314 isl_union_set_free(uset
);
319 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
320 __isl_take isl_union_pw_qpolynomial
*upwqp
,
321 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
323 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
327 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
328 __isl_take isl_union_map
*umap
,
329 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
332 struct isl_list
*list
;
335 ctx
= isl_union_map_get_ctx(umap
);
336 list
= isl_list_alloc(ctx
, 2);
340 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
341 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
343 list
->obj
[1].type
= isl_obj_bool
;
344 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
345 if (tight
< 0 || !list
->obj
[0].v
)
350 isl_union_map_free(umap
);
351 isl_union_pw_qpolynomial_fold_free(upwf
);
357 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_int_mul(
358 __isl_take isl_union_pw_qpolynomial
*upwqp
, __isl_take isl_int_obj
*i
)
366 isl_int_obj_get_int(i
, &v
);
367 upwqp
= isl_union_pw_qpolynomial_mul_isl_int(upwqp
, v
);
374 isl_union_pw_qpolynomial_free(upwqp
);
378 static __isl_give isl_union_pw_qpolynomial
*int_union_pw_qpolynomial_mul(
379 __isl_take isl_int_obj
*i
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
381 return union_pw_qpolynomial_int_mul(upwqp
, i
);
384 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_fold_int_mul(
385 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
386 __isl_take isl_int_obj
*i
)
394 isl_int_obj_get_int(i
, &v
);
395 upwf
= isl_union_pw_qpolynomial_fold_mul_isl_int(upwf
, v
);
402 isl_union_pw_qpolynomial_fold_free(upwf
);
406 static __isl_give isl_union_pw_qpolynomial_fold
*int_union_pw_qpolynomial_fold_mul(
407 __isl_take isl_int_obj
*i
,
408 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
410 return union_pw_qpolynomial_fold_int_mul(upwf
, i
);
413 struct isc_bin_op bin_ops
[] = {
414 { '+', isl_obj_int
, isl_obj_int
, isl_obj_int
,
415 (isc_bin_op_fn
) &isl_int_obj_add
},
416 { '-', isl_obj_int
, isl_obj_int
, isl_obj_int
,
417 (isc_bin_op_fn
) &isl_int_obj_sub
},
418 { '*', isl_obj_int
, isl_obj_int
, isl_obj_int
,
419 (isc_bin_op_fn
) &isl_int_obj_mul
},
420 { '+', isl_obj_union_set
, isl_obj_union_set
,
422 (isc_bin_op_fn
) &isl_union_set_union
},
423 { '+', isl_obj_union_map
, isl_obj_union_map
,
425 (isc_bin_op_fn
) &isl_union_map_union
},
426 { '-', isl_obj_union_set
, isl_obj_union_set
,
428 (isc_bin_op_fn
) &isl_union_set_subtract
},
429 { '-', isl_obj_union_map
, isl_obj_union_map
,
431 (isc_bin_op_fn
) &isl_union_map_subtract
},
432 { '*', isl_obj_union_set
, isl_obj_union_set
,
434 (isc_bin_op_fn
) &isl_union_set_intersect
},
435 { '*', isl_obj_union_map
, isl_obj_union_map
,
437 (isc_bin_op_fn
) &isl_union_map_intersect
},
438 { '*', isl_obj_union_map
, isl_obj_union_set
,
440 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
441 { '.', isl_obj_union_map
, isl_obj_union_map
,
443 (isc_bin_op_fn
) &isl_union_map_apply_range
},
444 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
445 isl_obj_union_pw_qpolynomial
,
446 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
447 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
449 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
450 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
452 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
453 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
454 (isc_bin_op_fn
) &union_set_is_equal
},
455 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
456 (isc_bin_op_fn
) &union_map_is_equal
},
457 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
458 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_subset
},
459 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
460 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_subset
},
461 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
462 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_subset
},
463 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
464 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_subset
},
465 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
466 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_superset
},
467 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
468 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_superset
},
469 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
470 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_superset
},
471 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
472 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_superset
},
473 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
475 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
476 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
478 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
479 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
481 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
482 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
484 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
485 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
487 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
488 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
490 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
491 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
493 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
494 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
496 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
497 { '.', isl_obj_union_pw_qpolynomial_fold
,
498 isl_obj_union_pw_qpolynomial_fold
,
499 isl_obj_union_pw_qpolynomial_fold
,
500 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
501 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
502 isl_obj_union_pw_qpolynomial
,
503 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
504 { '+', isl_obj_union_pw_qpolynomial
,
505 isl_obj_union_pw_qpolynomial_fold
,
506 isl_obj_union_pw_qpolynomial_fold
,
507 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
508 { '+', isl_obj_union_pw_qpolynomial_fold
,
509 isl_obj_union_pw_qpolynomial
,
510 isl_obj_union_pw_qpolynomial_fold
,
511 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
512 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
513 isl_obj_union_pw_qpolynomial
,
514 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
515 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial
,
516 isl_obj_union_pw_qpolynomial
,
517 (isc_bin_op_fn
) &int_union_pw_qpolynomial_mul
},
518 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_int
,
519 isl_obj_union_pw_qpolynomial
,
520 (isc_bin_op_fn
) &union_pw_qpolynomial_int_mul
},
521 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial_fold
,
522 isl_obj_union_pw_qpolynomial_fold
,
523 (isc_bin_op_fn
) &int_union_pw_qpolynomial_fold_mul
},
524 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_int
,
525 isl_obj_union_pw_qpolynomial_fold
,
526 (isc_bin_op_fn
) &union_pw_qpolynomial_fold_int_mul
},
527 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
528 isl_obj_union_pw_qpolynomial
,
529 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
530 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
531 isl_obj_union_pw_qpolynomial
,
532 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
533 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
534 isl_obj_union_pw_qpolynomial_fold
,
535 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
536 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
537 isl_obj_union_pw_qpolynomial
,
538 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
539 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
540 isl_obj_union_pw_qpolynomial
,
541 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
542 { '%', isl_obj_union_set
, isl_obj_union_set
,
544 (isc_bin_op_fn
) &isl_union_set_gist
},
545 { '%', isl_obj_union_map
, isl_obj_union_map
,
547 (isc_bin_op_fn
) &isl_union_map_gist
},
548 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
549 isl_obj_union_pw_qpolynomial
,
550 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
551 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
552 isl_obj_union_pw_qpolynomial_fold
,
553 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
554 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
555 (isc_bin_op_fn
) &isl_str_concat
},
559 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
560 __isl_take isl_union_map
*umap2
)
562 return isl_union_map_apply_range(umap2
, umap1
);
565 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
566 __isl_take isl_union_pw_qpolynomial
*upwqp
,
567 __isl_take isl_union_map
*umap
)
569 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
572 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
573 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
574 __isl_take isl_union_map
*umap
)
576 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
579 struct isc_named_bin_op named_bin_ops
[] = {
580 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
582 (isc_bin_op_fn
) &map_after_map
} },
583 { "after", { -1, isl_obj_union_pw_qpolynomial
,
584 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
585 (isc_bin_op_fn
) &qpolynomial_after_map
} },
586 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
587 isl_obj_union_map
, isl_obj_list
,
588 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
589 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
591 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
592 { "before", { -1, isl_obj_union_map
,
593 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
594 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
595 { "before", { -1, isl_obj_union_map
,
596 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
597 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
598 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
600 (isc_bin_op_fn
) &isl_union_set_product
} },
601 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
603 (isc_bin_op_fn
) &isl_union_map_product
} },
607 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
609 return isl_set_from_basic_set(isl_union_set_sample(uset
));
612 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
614 return isl_map_from_basic_map(isl_union_map_sample(umap
));
617 static __isl_give
struct isl_list
*union_map_power(
618 __isl_take isl_union_map
*umap
)
621 struct isl_list
*list
;
624 ctx
= isl_union_map_get_ctx(umap
);
625 list
= isl_list_alloc(ctx
, 2);
629 list
->obj
[0].type
= isl_obj_union_map
;
630 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
631 list
->obj
[1].type
= isl_obj_bool
;
632 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
633 if (exact
< 0 || !list
->obj
[0].v
)
638 isl_union_map_free(umap
);
644 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
645 __isl_take isl_union_pw_qpolynomial
*upwqp
)
648 struct isl_list
*list
;
651 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
652 list
= isl_list_alloc(ctx
, 2);
656 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
657 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
658 isl_fold_max
, &tight
);
659 list
->obj
[1].type
= isl_obj_bool
;
660 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
661 if (tight
< 0 || !list
->obj
[0].v
)
666 isl_union_pw_qpolynomial_free(upwqp
);
673 void *map_codegen(void *arg
)
676 isl_union_map
*umap
= (isl_union_map
*)arg
;
677 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
679 CloogOptions
*options
;
680 CloogDomain
*context
;
681 CloogUnionDomain
*ud
;
683 struct clast_stmt
*stmt
;
685 state
= cloog_isl_state_malloc(ctx
);
686 options
= cloog_options_malloc(state
);
687 options
->language
= LANGUAGE_C
;
688 options
->strides
= 1;
691 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
693 dim
= isl_union_map_get_dim(umap
);
694 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
696 input
= cloog_input_alloc(context
, ud
);
698 stmt
= cloog_clast_create_from_input(input
, options
);
699 clast_pprint(stdout
, stmt
, 0, options
);
700 cloog_clast_free(stmt
);
703 cloog_options_free(options
);
704 cloog_state_free(state
);
705 isl_union_map_free(umap
);
709 void *set_codegen(void *arg
)
712 isl_union_set
*uset
= (isl_union_set
*)arg
;
713 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
715 CloogOptions
*options
;
716 CloogDomain
*context
;
717 CloogUnionDomain
*ud
;
719 struct clast_stmt
*stmt
;
721 if (isl_union_set_n_set(uset
) > 1)
722 isl_die(ctx
, isl_error_invalid
,
723 "code generation for more than one domain "
724 "requires a schedule", goto error
);
726 state
= cloog_isl_state_malloc(ctx
);
727 options
= cloog_options_malloc(state
);
728 options
->language
= LANGUAGE_C
;
729 options
->strides
= 1;
732 ud
= cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset
));
734 dim
= isl_union_set_get_dim(uset
);
735 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
737 input
= cloog_input_alloc(context
, ud
);
739 stmt
= cloog_clast_create_from_input(input
, options
);
740 clast_pprint(stdout
, stmt
, 0, options
);
741 cloog_clast_free(stmt
);
743 cloog_options_free(options
);
744 cloog_state_free(state
);
746 isl_union_set_free(uset
);
751 static int add_point(__isl_take isl_point
*pnt
, void *user
)
753 isl_union_set
**scan
= (isl_union_set
**) user
;
755 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
760 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
764 scan
= isl_union_set_empty(isl_union_set_get_dim(uset
));
766 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
767 isl_union_set_free(scan
);
771 isl_union_set_free(uset
);
775 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
777 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
780 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
781 __isl_take isl_union_pw_qpolynomial
*upwqp
)
783 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
786 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
787 __isl_take isl_union_pw_qpolynomial
*upwqp
)
789 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
792 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
793 __isl_take isl_union_pw_qpolynomial
*upwqp
)
795 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
798 typedef void *(*isc_un_op_fn
)(void *arg
);
800 enum isl_token_type op
;
805 struct isc_named_un_op
{
809 struct isc_named_un_op named_un_ops
[] = {
810 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
811 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
812 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
813 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
814 {"card", { -1, isl_obj_union_set
,
815 isl_obj_union_pw_qpolynomial
,
816 (isc_un_op_fn
) &isl_union_set_card
} },
817 {"card", { -1, isl_obj_union_map
,
818 isl_obj_union_pw_qpolynomial
,
819 (isc_un_op_fn
) &isl_union_map_card
} },
820 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
821 (isc_un_op_fn
) &isl_union_set_coalesce
} },
822 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
823 (isc_un_op_fn
) &isl_union_map_coalesce
} },
824 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
825 isl_obj_union_pw_qpolynomial
,
826 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
827 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
828 isl_obj_union_pw_qpolynomial_fold
,
829 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
831 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
833 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
836 {"coefficients", { -1, isl_obj_union_set
,
838 (isc_un_op_fn
) &isl_union_set_coefficients
} },
839 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
840 (isc_un_op_fn
) &isl_union_set_solutions
} },
841 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
842 (isc_un_op_fn
) &isl_union_map_deltas
} },
843 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
844 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
845 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
846 (isc_un_op_fn
) &isl_union_map_domain
} },
847 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
848 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
849 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
851 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
852 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
853 (isc_un_op_fn
) &isl_union_map_domain
} },
854 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
856 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
857 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
859 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
860 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
861 (isc_un_op_fn
) &isl_union_map_domain_map
} },
862 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
863 (isc_un_op_fn
) &isl_union_map_range
} },
864 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
865 (isc_un_op_fn
) &isl_union_map_range
} },
866 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
867 (isc_un_op_fn
) &isl_union_map_range_map
} },
868 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
869 (isc_un_op_fn
) &isl_union_set_identity
} },
870 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
871 (isc_un_op_fn
) &isl_union_map_lexmin
} },
872 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
873 (isc_un_op_fn
) &isl_union_map_lexmax
} },
874 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
875 (isc_un_op_fn
) &isl_union_set_lexmin
} },
876 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
877 (isc_un_op_fn
) &isl_union_set_lexmax
} },
878 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
879 (isc_un_op_fn
) &isl_union_set_lift
} },
880 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
881 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
882 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
883 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
884 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
885 isl_obj_union_pw_qpolynomial
,
886 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
887 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
888 isl_obj_union_pw_qpolynomial
,
889 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
890 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
891 isl_obj_union_pw_qpolynomial
,
892 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
893 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
894 (isc_un_op_fn
) &union_map_power
} },
895 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
896 (isc_un_op_fn
) &union_set_sample
} },
897 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
898 (isc_un_op_fn
) &union_map_sample
} },
899 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
900 (isc_un_op_fn
) &union_set_scan
} },
901 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
902 (isc_un_op_fn
) &union_map_scan
} },
903 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
904 isl_obj_union_pw_qpolynomial
,
905 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
906 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
907 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
908 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
909 (isc_un_op_fn
) &isl_union_set_unwrap
} },
910 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
911 (isc_un_op_fn
) &isl_union_map_wrap
} },
912 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
913 (isc_un_op_fn
) &isl_union_map_zip
} },
917 struct isl_named_obj
{
922 static void free_obj(struct isl_obj obj
)
924 obj
.type
->free(obj
.v
);
927 static int same_name(const void *entry
, const void *val
)
929 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
931 return !strcmp(named
->name
, val
);
934 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
935 char *name
, struct isl_obj obj
)
937 struct isl_hash_table_entry
*entry
;
939 struct isl_named_obj
*named
;
941 name_hash
= isl_hash_string(isl_hash_init(), name
);
942 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
947 free_obj(named
->obj
);
950 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
965 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
966 struct isl_hash_table
*table
, char *name
)
968 struct isl_obj obj
= { isl_obj_none
, NULL
};
969 struct isl_hash_table_entry
*entry
;
972 name_hash
= isl_hash_string(isl_hash_init(), name
);
973 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
975 struct isl_named_obj
*named
;
978 } else if (isdigit(name
[0]))
979 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
981 fprintf(stderr
, "unknown identifier '%s'\n", name
);
984 obj
.v
= obj
.type
->copy(obj
.v
);
988 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
990 if (obj
.type
== super
)
992 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
994 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
996 if (obj
.type
== isl_obj_pw_qpolynomial
&&
997 super
== isl_obj_union_pw_qpolynomial
)
999 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1000 super
== isl_obj_union_pw_qpolynomial_fold
)
1002 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1004 if (obj
.type
== isl_obj_list
) {
1005 struct isl_list
*list
= obj
.v
;
1006 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1007 return is_subtype(list
->obj
[0], super
);
1009 if (super
== isl_obj_str
)
1014 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1016 struct isl_list
*list
= obj
.v
;
1019 obj
.v
= obj
.type
->copy(obj
.v
);
1021 isl_list_free(list
);
1026 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1029 if (obj
.type
== type
)
1031 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1032 obj
.type
= isl_obj_union_map
;
1033 obj
.v
= isl_union_map_from_map(obj
.v
);
1036 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1037 obj
.type
= isl_obj_union_set
;
1038 obj
.v
= isl_union_set_from_set(obj
.v
);
1041 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1042 type
== isl_obj_union_pw_qpolynomial
) {
1043 obj
.type
= isl_obj_union_pw_qpolynomial
;
1044 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1047 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1048 type
== isl_obj_union_pw_qpolynomial_fold
) {
1049 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1050 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1053 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1054 if (type
== isl_obj_union_map
) {
1055 obj
.type
= isl_obj_union_map
;
1058 if (type
== isl_obj_union_pw_qpolynomial
) {
1059 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
1060 isl_union_set_free(obj
.v
);
1061 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1062 obj
.type
= isl_obj_union_pw_qpolynomial
;
1065 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1066 isl_dim
*dim
= isl_union_set_get_dim(obj
.v
);
1067 isl_union_set_free(obj
.v
);
1068 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1070 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1074 if (obj
.type
== isl_obj_list
) {
1075 struct isl_list
*list
= obj
.v
;
1076 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1077 return convert(ctx
, obj_at(obj
, 0), type
);
1079 if (type
== isl_obj_str
) {
1084 p
= isl_printer_to_str(ctx
);
1087 p
= obj
.type
->print(p
, obj
.v
);
1088 s
= isl_printer_get_str(p
);
1089 isl_printer_free(p
);
1091 str
= isl_str_from_string(ctx
, s
);
1096 obj
.type
= isl_obj_str
;
1102 obj
.type
= isl_obj_none
;
1107 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1111 struct isl_token
*tok
;
1113 tok
= isl_stream_next_token(s
);
1117 for (i
= 0; ; ++i
) {
1120 if (bin_ops
[i
].op
!= tok
->type
)
1122 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1125 isl_token_free(tok
);
1129 for (i
= 0; ; ++i
) {
1130 if (!named_bin_ops
[i
].name
)
1132 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1134 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1137 isl_token_free(tok
);
1138 return &named_bin_ops
[i
].op
;
1141 isl_stream_push_token(s
, tok
);
1146 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1149 struct isl_token
*tok
;
1151 tok
= isl_stream_next_token(s
);
1155 for (i
= 0; ; ++i
) {
1156 if (!named_un_ops
[i
].name
)
1158 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1161 isl_token_free(tok
);
1162 return &named_un_ops
[i
].op
;
1165 isl_stream_push_token(s
, tok
);
1170 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1175 for (i
= 0; ; ++i
) {
1176 if (!named_un_ops
[i
].name
)
1178 if (named_un_ops
[i
].op
.op
!= like
->op
)
1180 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1183 return &named_un_ops
[i
].op
;
1189 static int is_assign(struct isl_stream
*s
)
1191 struct isl_token
*tok
;
1192 struct isl_token
*tok2
;
1195 tok
= isl_stream_next_token(s
);
1198 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1199 isl_stream_push_token(s
, tok
);
1203 tok2
= isl_stream_next_token(s
);
1205 isl_stream_push_token(s
, tok
);
1208 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1209 isl_stream_push_token(s
, tok2
);
1210 isl_stream_push_token(s
, tok
);
1215 static struct isl_obj
read_obj(struct isl_stream
*s
,
1216 struct isl_hash_table
*table
);
1217 static struct isl_obj
read_expr(struct isl_stream
*s
,
1218 struct isl_hash_table
*table
);
1220 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1221 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1223 struct isl_obj obj
= { isl_obj_none
, NULL
};
1225 obj
= read_obj(s
, table
);
1229 op
= find_matching_un_op(op
, obj
);
1232 isl_die(s
->ctx
, isl_error_invalid
,
1233 "no such unary operator defined on given operand",
1236 obj
= convert(s
->ctx
, obj
, op
->arg
);
1237 obj
.v
= op
->fn(obj
.v
);
1243 obj
.type
= isl_obj_none
;
1248 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1250 struct isl_list
*list
;
1253 if (obj
.type
!= isl_obj_union_map
)
1254 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1255 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1256 list
= isl_list_alloc(ctx
, 2);
1260 list
->obj
[0].type
= isl_obj_union_map
;
1261 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1262 list
->obj
[1].type
= isl_obj_bool
;
1263 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1265 obj
.type
= isl_obj_list
;
1266 if (exact
< 0 || !list
->obj
[0].v
)
1272 obj
.type
= isl_obj_none
;
1277 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1279 struct isl_list
*list
= obj
.v
;
1280 struct isl_token
*tok
;
1283 tok
= isl_stream_next_token(s
);
1284 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1285 isl_stream_error(s
, tok
, "expecting index");
1287 isl_stream_push_token(s
, tok
);
1290 i
= isl_int_get_si(tok
->u
.v
);
1291 isl_token_free(tok
);
1292 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1293 if (isl_stream_eat(s
, ']'))
1296 return obj_at(obj
, i
);
1299 obj
.type
= isl_obj_none
;
1304 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1305 struct isl_hash_table
*table
)
1309 obj
= read_expr(s
, table
);
1310 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1311 is_subtype(obj
, isl_obj_union_map
), goto error
);
1313 if (obj
.type
== isl_obj_list
) {
1314 struct isl_list
*list
= obj
.v
;
1315 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1316 obj
= obj_at(obj
, 0);
1318 if (obj
.type
== isl_obj_set
)
1319 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1320 else if (obj
.type
== isl_obj_map
)
1321 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1322 if (obj
.type
== isl_obj_union_set
) {
1323 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1325 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1329 if (isl_stream_eat(s
, ')'))
1334 isl_union_map_free(umap
);
1337 obj
.type
= isl_obj_none
;
1342 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1343 struct isl_obj obj
, struct isl_hash_table
*table
)
1347 arg
= read_expr(s
, table
);
1348 if (!is_subtype(arg
, isl_obj_union_map
) &&
1349 !is_subtype(arg
, isl_obj_union_set
))
1350 isl_die(s
->ctx
, isl_error_invalid
,
1351 "expecting set of map argument", goto error
);
1353 if (arg
.type
== isl_obj_list
) {
1354 struct isl_list
*list
= arg
.v
;
1355 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1356 arg
= obj_at(arg
, 0);
1358 if (arg
.type
== isl_obj_set
)
1359 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1360 else if (arg
.type
== isl_obj_map
)
1361 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1362 if (arg
.type
== isl_obj_union_set
) {
1363 arg
.v
= isl_union_map_from_range(arg
.v
);
1364 arg
.type
= isl_obj_union_map
;
1366 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1367 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(arg
.v
, obj
.v
);
1369 obj
.type
= isl_obj_list
;
1370 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(arg
.v
, obj
.v
);
1375 if (isl_stream_eat(s
, ')'))
1383 obj
.type
= isl_obj_none
;
1388 struct add_vertex_data
{
1389 struct isl_list
*list
;
1393 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1395 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1396 isl_basic_set
*expr
;
1398 expr
= isl_vertex_get_expr(vertex
);
1400 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1401 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1404 isl_vertex_free(vertex
);
1409 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1412 isl_basic_set
*hull
;
1413 isl_vertices
*vertices
= NULL
;
1414 struct isl_list
*list
= NULL
;
1416 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1418 set
= isl_set_remove_divs(set
);
1419 hull
= isl_set_convex_hull(set
);
1420 vertices
= isl_basic_set_compute_vertices(hull
);
1421 isl_basic_set_free(hull
);
1425 ctx
= isl_vertices_get_ctx(vertices
);
1426 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1431 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1433 data
->list
= isl_list_concat(list
, data
->list
);
1435 isl_vertices_free(vertices
);
1440 isl_vertices_free(vertices
);
1444 static struct isl_obj
vertices(struct isl_stream
*s
,
1445 struct isl_hash_table
*table
)
1449 struct isl_list
*list
= NULL
;
1450 isl_union_set
*uset
;
1451 struct add_vertex_data data
= { NULL
};
1453 obj
= read_expr(s
, table
);
1454 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1455 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1459 ctx
= isl_union_set_get_ctx(uset
);
1460 list
= isl_list_alloc(ctx
, 0);
1466 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1469 isl_union_set_free(uset
);
1471 obj
.type
= isl_obj_list
;
1476 isl_union_set_free(uset
);
1477 isl_list_free(data
.list
);
1479 obj
.type
= isl_obj_none
;
1484 static struct isl_obj
type_of(struct isl_stream
*s
,
1485 struct isl_hash_table
*table
)
1489 const char *type
= "unknown";
1491 obj
= read_expr(s
, table
);
1493 if (obj
.type
== isl_obj_map
||
1494 obj
.type
== isl_obj_union_map
)
1496 if (obj
.type
== isl_obj_set
||
1497 obj
.type
== isl_obj_union_set
)
1499 if (obj
.type
== isl_obj_pw_qpolynomial
||
1500 obj
.type
== isl_obj_union_pw_qpolynomial
)
1501 type
= "piecewise quasipolynomial";
1502 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1503 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1504 type
= "piecewise quasipolynomial fold";
1505 if (obj
.type
== isl_obj_list
)
1507 if (obj
.type
== isl_obj_bool
)
1509 if (obj
.type
== isl_obj_str
)
1511 if (obj
.type
== isl_obj_int
)
1515 obj
.type
= isl_obj_str
;
1516 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1521 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1522 struct isl_hash_table
*table
)
1526 obj
= read_obj(s
, table
);
1527 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1528 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1535 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1536 struct isl_hash_table
*table
)
1540 obj
= read_obj(s
, table
);
1541 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1542 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1549 static struct isl_obj
last_any(struct isl_stream
*s
,
1550 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1551 __isl_take isl_union_map
*may_source
)
1553 struct isl_obj obj
= { isl_obj_none
, NULL
};
1554 isl_union_map
*sink
= NULL
;
1555 isl_union_map
*schedule
= NULL
;
1556 isl_union_map
*may_dep
;
1557 isl_union_map
*must_dep
;
1559 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1562 sink
= read_map(s
, table
);
1566 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1569 schedule
= read_map(s
, table
);
1573 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1574 schedule
, &must_dep
, &may_dep
,
1578 obj
.type
= isl_obj_union_map
;
1579 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1583 isl_union_map_free(may_source
);
1584 isl_union_map_free(must_source
);
1585 isl_union_map_free(sink
);
1586 isl_union_map_free(schedule
);
1588 obj
.type
= isl_obj_none
;
1593 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1595 struct isl_obj obj
= { isl_obj_none
, NULL
};
1596 isl_union_map
*must_source
= NULL
;
1597 isl_union_map
*may_source
= NULL
;
1598 isl_union_map
*sink
= NULL
;
1599 isl_union_map
*schedule
= NULL
;
1600 isl_union_map
*may_dep
;
1602 may_source
= read_map(s
, table
);
1606 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1607 must_source
= read_map(s
, table
);
1610 return last_any(s
, table
, must_source
, may_source
);
1613 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1616 sink
= read_map(s
, table
);
1620 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1623 schedule
= read_map(s
, table
);
1627 must_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1628 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1629 schedule
, NULL
, &may_dep
,
1633 obj
.type
= isl_obj_union_map
;
1638 isl_union_map_free(may_source
);
1639 isl_union_map_free(must_source
);
1640 isl_union_map_free(sink
);
1641 isl_union_map_free(schedule
);
1643 obj
.type
= isl_obj_none
;
1648 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1650 struct isl_obj obj
= { isl_obj_none
, NULL
};
1651 struct isl_list
*list
= NULL
;
1652 isl_union_map
*must_source
= NULL
;
1653 isl_union_map
*may_source
= NULL
;
1654 isl_union_map
*sink
= NULL
;
1655 isl_union_map
*schedule
= NULL
;
1656 isl_union_map
*must_dep
;
1657 isl_union_map
*must_no_source
;
1659 must_source
= read_map(s
, table
);
1663 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1664 may_source
= read_map(s
, table
);
1667 return last_any(s
, table
, must_source
, may_source
);
1670 list
= isl_list_alloc(s
->ctx
, 2);
1674 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1677 sink
= read_map(s
, table
);
1681 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1684 schedule
= read_map(s
, table
);
1688 may_source
= isl_union_map_empty(isl_union_map_get_dim(sink
));
1689 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1690 schedule
, &must_dep
, NULL
,
1691 &must_no_source
, NULL
) < 0) {
1692 isl_list_free(list
);
1696 list
->obj
[0].type
= isl_obj_union_map
;
1697 list
->obj
[0].v
= must_dep
;
1698 list
->obj
[1].type
= isl_obj_union_map
;
1699 list
->obj
[1].v
= must_no_source
;
1702 obj
.type
= isl_obj_list
;
1706 isl_list_free(list
);
1707 isl_union_map_free(may_source
);
1708 isl_union_map_free(must_source
);
1709 isl_union_map_free(sink
);
1710 isl_union_map_free(schedule
);
1712 obj
.type
= isl_obj_none
;
1717 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1718 struct isl_hash_table
*table
)
1720 isl_union_set
*domain
;
1721 isl_union_map
*validity
;
1722 isl_union_map
*proximity
;
1724 domain
= read_set(s
, table
);
1728 validity
= isl_union_map_empty(isl_union_set_get_dim(domain
));
1729 proximity
= isl_union_map_empty(isl_union_set_get_dim(domain
));
1732 isl_union_map
*umap
;
1733 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1734 umap
= read_map(s
, table
);
1735 validity
= isl_union_map_union(validity
, umap
);
1736 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1737 umap
= read_map(s
, table
);
1738 proximity
= isl_union_map_union(proximity
, umap
);
1743 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1746 static struct isl_obj
schedule(struct isl_stream
*s
,
1747 struct isl_hash_table
*table
)
1749 struct isl_obj obj
= { isl_obj_none
, NULL
};
1750 isl_schedule
*schedule
;
1752 schedule
= get_schedule(s
, table
);
1754 obj
.v
= isl_schedule_get_map(schedule
);
1755 obj
.type
= isl_obj_union_map
;
1757 isl_schedule_free(schedule
);
1762 static struct isl_obj
schedule_bands(struct isl_stream
*s
,
1763 struct isl_hash_table
*table
)
1767 struct isl_obj obj
= { isl_obj_none
, NULL
};
1768 struct isl_list
*list
;
1769 isl_schedule
*schedule
;
1771 schedule
= get_schedule(s
, table
);
1775 n_band
= isl_schedule_n_band(schedule
);
1776 list
= isl_list_alloc(s
->ctx
, n_band
);
1781 obj
.type
= isl_obj_list
;
1783 for (i
= 0; i
< n_band
; ++i
) {
1784 list
->obj
[i
].type
= isl_obj_union_map
;
1785 list
->obj
[i
].v
= isl_schedule_get_band(schedule
, i
);
1786 if (!list
->obj
[i
].v
)
1790 isl_schedule_free(schedule
);
1794 isl_schedule_free(schedule
);
1796 obj
.type
= isl_obj_none
;
1801 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1803 struct isl_token
*tok
;
1805 if (isl_stream_eat_if_available(s
, '+'))
1806 return transitive_closure(s
->ctx
, obj
);
1808 tok
= isl_stream_next_token(s
);
1809 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1810 isl_stream_error(s
, tok
, "expecting -1");
1812 isl_stream_push_token(s
, tok
);
1815 isl_token_free(tok
);
1816 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1817 if (obj
.type
!= isl_obj_union_map
)
1818 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1820 obj
.v
= isl_union_map_reverse(obj
.v
);
1827 obj
.type
= isl_obj_none
;
1832 static struct isl_obj
read_from_file(struct isl_stream
*s
)
1835 struct isl_token
*tok
;
1836 struct isl_stream
*s_file
;
1837 struct iscc_options
*options
;
1840 tok
= isl_stream_next_token(s
);
1841 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1842 isl_stream_error(s
, tok
, "expecting filename");
1843 isl_token_free(tok
);
1847 options
= isl_ctx_peek_iscc_options(s
->ctx
);
1848 if (!options
|| !options
->io
) {
1849 isl_token_free(tok
);
1850 isl_die(s
->ctx
, isl_error_invalid
,
1851 "read operation not allowed", goto error
);
1854 file
= fopen(tok
->u
.s
, "r");
1855 isl_token_free(tok
);
1856 isl_assert(s
->ctx
, file
, goto error
);
1858 s_file
= isl_stream_new_file(s
->ctx
, file
);
1864 obj
= isl_stream_read_obj(s_file
);
1866 isl_stream_free(s_file
);
1871 obj
.type
= isl_obj_none
;
1876 static struct isl_obj
write_to_file(struct isl_stream
*s
,
1877 struct isl_hash_table
*table
)
1880 struct isl_token
*tok
;
1881 struct isl_stream
*s_file
;
1882 struct iscc_options
*options
;
1886 tok
= isl_stream_next_token(s
);
1887 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
1888 isl_stream_error(s
, tok
, "expecting filename");
1889 isl_token_free(tok
);
1893 obj
= read_expr(s
, table
);
1895 options
= isl_ctx_peek_iscc_options(s
->ctx
);
1896 if (!options
|| !options
->io
) {
1897 isl_token_free(tok
);
1898 isl_die(s
->ctx
, isl_error_invalid
,
1899 "write operation not allowed", goto error
);
1902 file
= fopen(tok
->u
.s
, "w");
1903 isl_token_free(tok
);
1905 isl_die(s
->ctx
, isl_error_unknown
,
1906 "could not open file for writing", goto error
);
1908 p
= isl_printer_to_file(s
->ctx
, file
);
1909 p
= isl_printer_set_output_format(p
, options
->format
);
1910 p
= obj
.type
->print(p
, obj
.v
);
1911 p
= isl_printer_end_line(p
);
1912 isl_printer_free(p
);
1917 obj
.type
= isl_obj_none
;
1922 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
1924 struct isl_token
*tok
;
1925 struct isl_obj obj
= { isl_obj_none
, NULL
};
1927 tok
= isl_stream_next_token(s
);
1930 if (tok
->type
== ISL_TOKEN_STRING
) {
1932 str
= isl_str_alloc(s
->ctx
);
1935 str
->s
= strdup(tok
->u
.s
);
1936 isl_token_free(tok
);
1938 obj
.type
= isl_obj_str
;
1940 isl_stream_push_token(s
, tok
);
1943 isl_token_free(tok
);
1947 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
1949 struct isl_token
*tok
;
1950 struct isl_obj obj
= { isl_obj_none
, NULL
};
1952 tok
= isl_stream_next_token(s
);
1955 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
1956 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
1957 isl_token_free(tok
);
1958 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
1959 obj
.type
= isl_obj_bool
;
1961 isl_stream_push_token(s
, tok
);
1964 isl_token_free(tok
);
1968 static __isl_give
char *read_ident(struct isl_stream
*s
)
1971 struct isl_token
*tok
, *tok2
;
1973 name
= isl_stream_read_ident_if_available(s
);
1977 tok
= isl_stream_next_token(s
);
1980 if (tok
->type
!= '$') {
1981 isl_stream_push_token(s
, tok
);
1984 tok2
= isl_stream_next_token(s
);
1985 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
1987 isl_stream_push_token(s
, tok2
);
1988 isl_stream_push_token(s
, tok
);
1992 name
= isl_int_get_str(tok2
->u
.v
);
1993 isl_token_free(tok
);
1994 isl_token_free(tok2
);
1999 static struct isl_obj
read_list(struct isl_stream
*s
,
2000 struct isl_hash_table
*table
, struct isl_obj obj
)
2002 struct isl_list
*list
;
2004 list
= isl_list_alloc(s
->ctx
, 2);
2008 list
->obj
[1] = read_obj(s
, table
);
2010 obj
.type
= isl_obj_list
;
2012 if (!list
->obj
[1].v
)
2015 while (isl_stream_eat_if_available(s
, ',')) {
2016 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2024 obj
.type
= isl_obj_none
;
2029 static struct isl_obj
read_obj(struct isl_stream
*s
,
2030 struct isl_hash_table
*table
)
2032 struct isl_obj obj
= { isl_obj_none
, NULL
};
2034 struct isc_un_op
*op
= NULL
;
2036 obj
= read_string_if_available(s
);
2039 obj
= read_bool_if_available(s
);
2042 if (isl_stream_eat_if_available(s
, '(')) {
2043 obj
= read_expr(s
, table
);
2044 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2045 obj
= read_list(s
, table
, obj
);
2046 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2049 op
= read_prefix_un_op_if_available(s
);
2051 return read_un_op_expr(s
, table
, op
);
2053 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2054 return read_from_file(s
);
2055 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2056 return write_to_file(s
, table
);
2057 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2058 return vertices(s
, table
);
2059 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2060 return any(s
, table
);
2061 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2062 return last(s
, table
);
2063 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2064 return schedule(s
, table
);
2065 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_BANDS
]))
2066 return schedule_bands(s
, table
);
2067 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2068 return type_of(s
, table
);
2070 name
= read_ident(s
);
2072 obj
= stored_obj(s
->ctx
, table
, name
);
2074 obj
= isl_stream_read_obj(s
);
2079 if (isl_stream_eat_if_available(s
, '^'))
2080 obj
= power(s
, obj
);
2081 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2082 obj
= obj_at_index(s
, obj
);
2083 else if (is_subtype(obj
, isl_obj_union_map
) &&
2084 isl_stream_eat_if_available(s
, '(')) {
2085 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2086 obj
= apply(s
, obj
.v
, table
);
2087 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2088 isl_stream_eat_if_available(s
, '(')) {
2089 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2090 obj
= apply_fun(s
, obj
, table
);
2091 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2092 isl_stream_eat_if_available(s
, '(')) {
2093 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2094 obj
= apply_fun(s
, obj
, table
);
2100 obj
.type
= isl_obj_none
;
2105 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2106 struct isl_obj lhs
, struct isl_obj rhs
)
2110 for (i
= 0; ; ++i
) {
2113 if (bin_ops
[i
].op
!= like
->op
)
2115 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2117 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2123 for (i
= 0; ; ++i
) {
2124 if (!named_bin_ops
[i
].name
)
2126 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2128 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2130 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2133 return &named_bin_ops
[i
].op
;
2139 static int next_is_neg_int(struct isl_stream
*s
)
2141 struct isl_token
*tok
;
2144 tok
= isl_stream_next_token(s
);
2145 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2146 isl_stream_push_token(s
, tok
);
2151 static struct isl_obj
read_expr(struct isl_stream
*s
,
2152 struct isl_hash_table
*table
)
2154 struct isl_obj obj
= { isl_obj_none
, NULL
};
2155 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2157 obj
= read_obj(s
, table
);
2159 struct isc_bin_op
*op
= NULL
;
2161 op
= read_bin_op_if_available(s
, obj
);
2165 right_obj
= read_obj(s
, table
);
2167 op
= find_matching_bin_op(op
, obj
, right_obj
);
2170 isl_die(s
->ctx
, isl_error_invalid
,
2171 "no such binary operator defined on given operands",
2174 obj
= convert(s
->ctx
, obj
, op
->lhs
);
2175 right_obj
= convert(s
->ctx
, right_obj
, op
->rhs
);
2176 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
2180 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2181 right_obj
= read_obj(s
, table
);
2182 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2187 free_obj(right_obj
);
2189 obj
.type
= isl_obj_none
;
2194 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2195 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2197 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2198 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2200 struct isl_obj obj
= { isl_obj_none
, NULL
};
2204 struct isc_bin_op
*op
= NULL
;
2209 if (isl_stream_is_empty(s
))
2212 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2213 return source_file(s
, table
, p
);
2215 assign
= is_assign(s
);
2217 lhs
= isl_stream_read_ident_if_available(s
);
2218 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2220 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2225 obj
= read_expr(s
, table
);
2226 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2227 fprintf(stderr
, "Interrupted\n");
2228 isl_ctx_reset_error(s
->ctx
);
2230 if (isl_stream_eat(s
, ';'))
2234 p
= obj
.type
->print(p
, obj
.v
);
2235 p
= isl_printer_end_line(p
);
2239 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2240 static int count
= 0;
2241 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2242 lhs
= strdup(buf
+ 1);
2244 p
= isl_printer_print_str(p
, buf
);
2245 p
= isl_printer_print_str(p
, " := ");
2246 p
= obj
.type
->print(p
, obj
.v
);
2247 p
= isl_printer_end_line(p
);
2249 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2254 isl_stream_flush_tokens(s
);
2255 isl_stream_skip_line(s
);
2261 int free_cb(void **entry
, void *user
)
2263 struct isl_named_obj
*named
= *entry
;
2265 free_obj(named
->obj
);
2272 static void register_named_ops(struct isl_stream
*s
)
2276 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2277 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2278 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2281 for (i
= 0; ; ++i
) {
2282 if (!named_un_ops
[i
].name
)
2284 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2285 named_un_ops
[i
].name
);
2286 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2289 for (i
= 0; ; ++i
) {
2290 if (!named_bin_ops
[i
].name
)
2292 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2293 named_bin_ops
[i
].name
);
2294 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2298 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2299 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2301 struct isl_token
*tok
;
2302 struct isl_stream
*s_file
;
2305 tok
= isl_stream_next_token(s
);
2306 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2307 isl_stream_error(s
, tok
, "expecting filename");
2308 isl_token_free(tok
);
2312 file
= fopen(tok
->u
.s
, "r");
2313 isl_token_free(tok
);
2314 isl_assert(s
->ctx
, file
, return p
);
2316 s_file
= isl_stream_new_file(s
->ctx
, file
);
2322 register_named_ops(s_file
);
2324 while (!s_file
->eof
)
2325 p
= read_line(s_file
, table
, p
, 0);
2327 isl_stream_free(s_file
);
2330 isl_stream_eat(s
, ';');
2335 int main(int argc
, char **argv
)
2337 struct isl_ctx
*ctx
;
2338 struct isl_stream
*s
;
2339 struct isl_hash_table
*table
;
2340 struct iscc_options
*options
;
2342 int tty
= isatty(0);
2344 options
= iscc_options_new_with_defaults();
2346 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
2348 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
2349 s
= isl_stream_new_file(ctx
, stdin
);
2351 table
= isl_hash_table_alloc(ctx
, 10);
2353 p
= isl_printer_to_file(ctx
, stdout
);
2354 p
= isl_printer_set_output_format(p
, options
->format
);
2357 register_named_ops(s
);
2359 install_signal_handler(ctx
);
2361 while (p
&& !s
->eof
) {
2362 isl_ctx_resume(ctx
);
2363 p
= read_line(s
, table
, p
, tty
);
2366 remove_signal_handler(ctx
);
2368 isl_printer_free(p
);
2369 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2370 isl_hash_table_free(ctx
, table
);