7 #include <isl/stream.h>
8 #include <isl/vertices.h>
11 #include <isl/schedule.h>
12 #include <isl_obj_list.h>
13 #include <isl_obj_str.h>
14 #include <barvinok/isl.h>
15 #include <barvinok/options.h>
16 #include "lattice_width.h"
23 static isl_ctx
*main_ctx
;
25 static void handler(int signum
)
27 if (isl_ctx_aborted(main_ctx
))
29 isl_ctx_abort(main_ctx
);
32 static struct sigaction sa_old
;
34 static void install_signal_handler(isl_ctx
*ctx
)
40 memset(&sa
, 0, sizeof(struct sigaction
));
41 sa
.sa_handler
= &handler
;
42 sa
.sa_flags
= SA_RESTART
;
43 sigaction(SIGINT
, &sa
, &sa_old
);
46 static void remove_signal_handler(isl_ctx
*ctx
)
48 sigaction(SIGINT
, &sa_old
, NULL
);
53 static void install_signal_handler(isl_ctx
*ctx
)
57 static void remove_signal_handler(isl_ctx
*ctx
)
64 #include <cloog/isl/cloog.h>
71 static int isl_bool_false
= 0;
72 static int isl_bool_true
= 1;
73 static int isl_bool_error
= -1;
75 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
76 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
77 ISCC_SCHEDULE
, ISCC_SCHEDULE_FOREST
,
78 ISCC_MINIMIZING
, ISCC_RESPECTING
,
79 ISCC_TYPEOF
, ISCC_PRINT
,
81 static const char *op_name
[ISCC_N_OP
] = {
83 [ISCC_WRITE
] = "write",
84 [ISCC_PRINT
] = "print",
85 [ISCC_SOURCE
] = "source",
86 [ISCC_VERTICES
] = "vertices",
89 [ISCC_BEFORE
] = "before",
90 [ISCC_UNDER
] = "under",
91 [ISCC_SCHEDULE
] = "schedule",
92 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
93 [ISCC_MINIMIZING
] = "minimizing",
94 [ISCC_RESPECTING
] = "respecting",
95 [ISCC_TYPEOF
] = "typeof"
97 static enum isl_token_type iscc_op
[ISCC_N_OP
];
99 struct isl_arg_choice iscc_format
[] = {
100 {"isl", ISL_FORMAT_ISL
},
101 {"omega", ISL_FORMAT_OMEGA
},
102 {"polylib", ISL_FORMAT_POLYLIB
},
103 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
104 {"latex", ISL_FORMAT_LATEX
},
109 struct iscc_options
{
110 struct barvinok_options
*barvinok
;
115 struct isl_arg iscc_options_arg
[] = {
116 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
118 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
119 iscc_format
, ISL_FORMAT_ISL
, "output format")
120 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
121 "allow read and write operations")
125 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
126 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
128 static void *isl_obj_bool_copy(void *v
)
133 static void isl_obj_bool_free(void *v
)
137 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
140 if (v
== &isl_bool_true
)
141 return isl_printer_print_str(p
, "True");
142 else if (v
== &isl_bool_false
)
143 return isl_printer_print_str(p
, "False");
145 return isl_printer_print_str(p
, "Error");
148 static void *isl_obj_bool_add(void *v1
, void *v2
)
153 struct isl_obj_vtable isl_obj_bool_vtable
= {
159 #define isl_obj_bool (&isl_obj_bool_vtable)
161 int *isl_bool_from_int(int res
)
163 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
166 int *union_map_is_equal(__isl_take isl_union_map
*map1
,
167 __isl_take isl_union_map
*map2
)
169 int res
= isl_union_map_is_equal(map1
, map2
);
170 isl_union_map_free(map1
);
171 isl_union_map_free(map2
);
172 return isl_bool_from_int(res
);
174 int *union_set_is_equal(__isl_take isl_union_set
*set1
,
175 __isl_take isl_union_set
*set2
)
177 return union_map_is_equal((isl_union_map
*)set1
, (isl_union_map
*)set2
);
180 int *union_map_is_subset(__isl_take isl_union_map
*map1
,
181 __isl_take isl_union_map
*map2
)
183 int res
= isl_union_map_is_subset(map1
, map2
);
184 isl_union_map_free(map1
);
185 isl_union_map_free(map2
);
186 return isl_bool_from_int(res
);
188 int *union_set_is_subset(__isl_take isl_union_set
*set1
,
189 __isl_take isl_union_set
*set2
)
191 return union_map_is_subset((isl_union_map
*)set1
, (isl_union_map
*)set2
);
194 int *union_map_is_strict_subset(__isl_take isl_union_map
*map1
,
195 __isl_take isl_union_map
*map2
)
197 int res
= isl_union_map_is_strict_subset(map1
, map2
);
198 isl_union_map_free(map1
);
199 isl_union_map_free(map2
);
200 return isl_bool_from_int(res
);
202 int *union_set_is_strict_subset(__isl_take isl_union_set
*set1
,
203 __isl_take isl_union_set
*set2
)
205 return union_map_is_strict_subset((isl_union_map
*)set1
,
206 (isl_union_map
*)set2
);
209 int *union_map_is_superset(__isl_take isl_union_map
*map1
,
210 __isl_take isl_union_map
*map2
)
212 return union_map_is_subset(map2
, map1
);
214 int *union_set_is_superset(__isl_take isl_union_set
*set1
,
215 __isl_take isl_union_set
*set2
)
217 return union_set_is_subset(set2
, set1
);
220 int *union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
221 __isl_take isl_union_map
*map2
)
223 return union_map_is_strict_subset(map2
, map1
);
225 int *union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
226 __isl_take isl_union_set
*set2
)
228 return union_set_is_strict_subset(set2
, set1
);
231 extern struct isl_obj_vtable isl_obj_list_vtable
;
232 #define isl_obj_list (&isl_obj_list_vtable)
234 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
236 enum isl_token_type op
;
242 struct isc_named_bin_op
{
244 struct isc_bin_op op
;
248 isl_union_pw_qpolynomial
*upwqp
;
249 isl_union_pw_qpolynomial
*res
;
252 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
254 struct iscc_at
*at
= (struct iscc_at
*) user
;
258 set
= isl_set_from_point(isl_point_copy(pnt
));
259 qp
= isl_union_pw_qpolynomial_eval(
260 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
262 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
263 isl_union_pw_qpolynomial_from_pw_qpolynomial(
264 isl_pw_qpolynomial_alloc(set
, qp
)));
269 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
270 __isl_take isl_union_pw_qpolynomial
*upwqp
,
271 __isl_take isl_union_set
*uset
)
276 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
278 isl_union_set_foreach_point(uset
, eval_at
, &at
);
280 isl_union_pw_qpolynomial_free(upwqp
);
281 isl_union_set_free(uset
);
286 struct iscc_fold_at
{
287 isl_union_pw_qpolynomial_fold
*upwf
;
288 isl_union_pw_qpolynomial
*res
;
291 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
293 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
297 set
= isl_set_from_point(isl_point_copy(pnt
));
298 qp
= isl_union_pw_qpolynomial_fold_eval(
299 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
301 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
302 isl_union_pw_qpolynomial_from_pw_qpolynomial(
303 isl_pw_qpolynomial_alloc(set
, qp
)));
308 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
309 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
310 __isl_take isl_union_set
*uset
)
312 struct iscc_fold_at at
;
315 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
317 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
319 isl_union_pw_qpolynomial_fold_free(upwf
);
320 isl_union_set_free(uset
);
325 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
326 __isl_take isl_union_pw_qpolynomial
*upwqp
,
327 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
329 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
333 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
334 __isl_take isl_union_map
*umap
,
335 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
338 struct isl_list
*list
;
341 ctx
= isl_union_map_get_ctx(umap
);
342 list
= isl_list_alloc(ctx
, 2);
346 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
347 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
349 list
->obj
[1].type
= isl_obj_bool
;
350 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
351 if (tight
< 0 || !list
->obj
[0].v
)
356 isl_union_map_free(umap
);
357 isl_union_pw_qpolynomial_fold_free(upwf
);
363 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
364 __isl_take isl_union_set
*uset
,
365 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
368 struct isl_list
*list
;
371 ctx
= isl_union_set_get_ctx(uset
);
372 list
= isl_list_alloc(ctx
, 2);
376 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
377 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
379 list
->obj
[1].type
= isl_obj_bool
;
380 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
381 if (tight
< 0 || !list
->obj
[0].v
)
386 isl_union_set_free(uset
);
387 isl_union_pw_qpolynomial_fold_free(upwf
);
393 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_int_mul(
394 __isl_take isl_union_pw_qpolynomial
*upwqp
, __isl_take isl_int_obj
*i
)
402 isl_int_obj_get_int(i
, &v
);
403 upwqp
= isl_union_pw_qpolynomial_mul_isl_int(upwqp
, v
);
410 isl_union_pw_qpolynomial_free(upwqp
);
414 static __isl_give isl_union_pw_qpolynomial
*int_union_pw_qpolynomial_mul(
415 __isl_take isl_int_obj
*i
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
417 return union_pw_qpolynomial_int_mul(upwqp
, i
);
420 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_fold_int_mul(
421 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
422 __isl_take isl_int_obj
*i
)
430 isl_int_obj_get_int(i
, &v
);
431 upwf
= isl_union_pw_qpolynomial_fold_mul_isl_int(upwf
, v
);
438 isl_union_pw_qpolynomial_fold_free(upwf
);
442 static __isl_give isl_union_pw_qpolynomial_fold
*int_union_pw_qpolynomial_fold_mul(
443 __isl_take isl_int_obj
*i
,
444 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
446 return union_pw_qpolynomial_fold_int_mul(upwf
, i
);
449 struct isc_bin_op bin_ops
[] = {
450 { '+', isl_obj_int
, isl_obj_int
, isl_obj_int
,
451 (isc_bin_op_fn
) &isl_int_obj_add
},
452 { '-', isl_obj_int
, isl_obj_int
, isl_obj_int
,
453 (isc_bin_op_fn
) &isl_int_obj_sub
},
454 { '*', isl_obj_int
, isl_obj_int
, isl_obj_int
,
455 (isc_bin_op_fn
) &isl_int_obj_mul
},
456 { '+', isl_obj_union_set
, isl_obj_union_set
,
458 (isc_bin_op_fn
) &isl_union_set_union
},
459 { '+', isl_obj_union_map
, isl_obj_union_map
,
461 (isc_bin_op_fn
) &isl_union_map_union
},
462 { '-', isl_obj_union_set
, isl_obj_union_set
,
464 (isc_bin_op_fn
) &isl_union_set_subtract
},
465 { '-', isl_obj_union_map
, isl_obj_union_map
,
467 (isc_bin_op_fn
) &isl_union_map_subtract
},
468 { '*', isl_obj_union_set
, isl_obj_union_set
,
470 (isc_bin_op_fn
) &isl_union_set_intersect
},
471 { '*', isl_obj_union_map
, isl_obj_union_map
,
473 (isc_bin_op_fn
) &isl_union_map_intersect
},
474 { '*', isl_obj_union_map
, isl_obj_union_set
,
476 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
477 { '.', isl_obj_union_map
, isl_obj_union_map
,
479 (isc_bin_op_fn
) &isl_union_map_apply_range
},
480 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
481 isl_obj_union_pw_qpolynomial
,
482 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
483 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
485 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
486 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
488 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
489 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
490 (isc_bin_op_fn
) &union_set_is_equal
},
491 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
492 (isc_bin_op_fn
) &union_map_is_equal
},
493 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
494 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_subset
},
495 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
496 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_subset
},
497 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
498 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_subset
},
499 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
500 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_subset
},
501 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
502 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_superset
},
503 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
504 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_superset
},
505 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
506 isl_obj_bool
, (isc_bin_op_fn
) &union_set_is_strict_superset
},
507 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
508 isl_obj_bool
, (isc_bin_op_fn
) &union_map_is_strict_superset
},
509 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
511 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
512 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
514 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
515 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
517 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
518 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
520 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
521 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
523 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
524 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
526 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
527 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
529 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
530 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
532 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
533 { '.', isl_obj_union_pw_qpolynomial_fold
,
534 isl_obj_union_pw_qpolynomial_fold
,
535 isl_obj_union_pw_qpolynomial_fold
,
536 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
537 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
538 isl_obj_union_pw_qpolynomial
,
539 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
540 { '+', isl_obj_union_pw_qpolynomial
,
541 isl_obj_union_pw_qpolynomial_fold
,
542 isl_obj_union_pw_qpolynomial_fold
,
543 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
544 { '+', isl_obj_union_pw_qpolynomial_fold
,
545 isl_obj_union_pw_qpolynomial
,
546 isl_obj_union_pw_qpolynomial_fold
,
547 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
548 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
549 isl_obj_union_pw_qpolynomial
,
550 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
551 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial
,
552 isl_obj_union_pw_qpolynomial
,
553 (isc_bin_op_fn
) &int_union_pw_qpolynomial_mul
},
554 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_int
,
555 isl_obj_union_pw_qpolynomial
,
556 (isc_bin_op_fn
) &union_pw_qpolynomial_int_mul
},
557 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial_fold
,
558 isl_obj_union_pw_qpolynomial_fold
,
559 (isc_bin_op_fn
) &int_union_pw_qpolynomial_fold_mul
},
560 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_int
,
561 isl_obj_union_pw_qpolynomial_fold
,
562 (isc_bin_op_fn
) &union_pw_qpolynomial_fold_int_mul
},
563 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
564 isl_obj_union_pw_qpolynomial
,
565 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
566 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
567 isl_obj_union_pw_qpolynomial
,
568 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
569 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
570 isl_obj_union_pw_qpolynomial_fold
,
571 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
572 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
573 isl_obj_union_pw_qpolynomial
,
574 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
575 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
576 isl_obj_union_pw_qpolynomial
,
577 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
578 { '%', isl_obj_union_set
, isl_obj_union_set
,
580 (isc_bin_op_fn
) &isl_union_set_gist
},
581 { '%', isl_obj_union_map
, isl_obj_union_map
,
583 (isc_bin_op_fn
) &isl_union_map_gist
},
584 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
585 isl_obj_union_pw_qpolynomial
,
586 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
587 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
588 isl_obj_union_pw_qpolynomial_fold
,
589 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
590 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
591 (isc_bin_op_fn
) &isl_str_concat
},
595 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
596 __isl_take isl_union_map
*umap2
)
598 return isl_union_map_apply_range(umap2
, umap1
);
601 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
602 __isl_take isl_union_pw_qpolynomial
*upwqp
,
603 __isl_take isl_union_map
*umap
)
605 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
608 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
609 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
610 __isl_take isl_union_map
*umap
)
612 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
615 struct isc_named_bin_op named_bin_ops
[] = {
616 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
618 (isc_bin_op_fn
) &map_after_map
} },
619 { "after", { -1, isl_obj_union_pw_qpolynomial
,
620 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
621 (isc_bin_op_fn
) &qpolynomial_after_map
} },
622 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
623 isl_obj_union_map
, isl_obj_list
,
624 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
625 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
627 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
628 { "before", { -1, isl_obj_union_map
,
629 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
630 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
631 { "before", { -1, isl_obj_union_map
,
632 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
633 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
634 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
636 (isc_bin_op_fn
) &isl_union_set_product
} },
637 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
639 (isc_bin_op_fn
) &isl_union_map_product
} },
643 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
645 return isl_set_from_basic_set(isl_union_set_sample(uset
));
648 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
650 return isl_map_from_basic_map(isl_union_map_sample(umap
));
653 static __isl_give
struct isl_list
*union_map_power(
654 __isl_take isl_union_map
*umap
)
657 struct isl_list
*list
;
660 ctx
= isl_union_map_get_ctx(umap
);
661 list
= isl_list_alloc(ctx
, 2);
665 list
->obj
[0].type
= isl_obj_union_map
;
666 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
667 list
->obj
[1].type
= isl_obj_bool
;
668 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
669 if (exact
< 0 || !list
->obj
[0].v
)
674 isl_union_map_free(umap
);
680 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
681 __isl_take isl_union_pw_qpolynomial
*upwqp
)
684 struct isl_list
*list
;
687 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
688 list
= isl_list_alloc(ctx
, 2);
692 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
693 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
694 isl_fold_max
, &tight
);
695 list
->obj
[1].type
= isl_obj_bool
;
696 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
697 if (tight
< 0 || !list
->obj
[0].v
)
702 isl_union_pw_qpolynomial_free(upwqp
);
709 void *map_codegen(void *arg
)
712 isl_union_map
*umap
= (isl_union_map
*)arg
;
713 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
715 CloogOptions
*options
;
716 CloogDomain
*context
;
717 CloogUnionDomain
*ud
;
719 struct clast_stmt
*stmt
;
721 state
= cloog_isl_state_malloc(ctx
);
722 options
= cloog_options_malloc(state
);
723 options
->language
= LANGUAGE_C
;
724 options
->strides
= 1;
727 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
729 dim
= isl_union_map_get_space(umap
);
730 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
732 input
= cloog_input_alloc(context
, ud
);
734 stmt
= cloog_clast_create_from_input(input
, options
);
735 clast_pprint(stdout
, stmt
, 0, options
);
736 cloog_clast_free(stmt
);
739 cloog_options_free(options
);
740 cloog_state_free(state
);
741 isl_union_map_free(umap
);
745 void *set_codegen(void *arg
)
748 isl_union_set
*uset
= (isl_union_set
*)arg
;
749 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
751 CloogOptions
*options
;
752 CloogDomain
*context
;
753 CloogUnionDomain
*ud
;
755 struct clast_stmt
*stmt
;
757 if (isl_union_set_n_set(uset
) > 1)
758 isl_die(ctx
, isl_error_invalid
,
759 "code generation for more than one domain "
760 "requires a schedule", goto error
);
762 state
= cloog_isl_state_malloc(ctx
);
763 options
= cloog_options_malloc(state
);
764 options
->language
= LANGUAGE_C
;
765 options
->strides
= 1;
768 ud
= cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset
));
770 dim
= isl_union_set_get_space(uset
);
771 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
773 input
= cloog_input_alloc(context
, ud
);
775 stmt
= cloog_clast_create_from_input(input
, options
);
776 clast_pprint(stdout
, stmt
, 0, options
);
777 cloog_clast_free(stmt
);
779 cloog_options_free(options
);
780 cloog_state_free(state
);
782 isl_union_set_free(uset
);
788 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
791 struct isl_list
*list
;
792 struct pet_scop
*scop
;
793 isl_union_map
*sched
, *reads
, *writes
;
794 isl_union_set
*domain
;
795 struct iscc_options
*options
;
801 options
= isl_ctx_peek_iscc_options(ctx
);
802 if (!options
|| !options
->io
) {
804 isl_die(ctx
, isl_error_invalid
,
805 "parse_file operation not allowed", return NULL
);
808 list
= isl_list_alloc(ctx
, 4);
812 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
, 1);
813 domain
= pet_scop_collect_domains(scop
);
814 sched
= pet_scop_collect_schedule(scop
);
815 reads
= pet_scop_collect_reads(scop
);
816 writes
= pet_scop_collect_writes(scop
);
819 list
->obj
[0].type
= isl_obj_union_set
;
820 list
->obj
[0].v
= domain
;
821 list
->obj
[1].type
= isl_obj_union_map
;
822 list
->obj
[1].v
= writes
;
823 list
->obj
[2].type
= isl_obj_union_map
;
824 list
->obj
[2].v
= reads
;
825 list
->obj
[3].type
= isl_obj_union_map
;
826 list
->obj
[3].v
= sched
;
828 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
829 !list
->obj
[2].v
|| !list
->obj
[3].v
)
841 static int add_point(__isl_take isl_point
*pnt
, void *user
)
843 isl_union_set
**scan
= (isl_union_set
**) user
;
845 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
850 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
854 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
856 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
857 isl_union_set_free(scan
);
861 isl_union_set_free(uset
);
865 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
867 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
870 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
871 __isl_take isl_union_pw_qpolynomial
*upwqp
)
873 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
876 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
877 __isl_take isl_union_pw_qpolynomial
*upwqp
)
879 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
882 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
883 __isl_take isl_union_pw_qpolynomial
*upwqp
)
885 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
888 typedef void *(*isc_un_op_fn
)(void *arg
);
890 enum isl_token_type op
;
895 struct isc_named_un_op
{
899 struct isc_named_un_op named_un_ops
[] = {
900 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
901 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
902 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
903 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
904 {"card", { -1, isl_obj_union_set
,
905 isl_obj_union_pw_qpolynomial
,
906 (isc_un_op_fn
) &isl_union_set_card
} },
907 {"card", { -1, isl_obj_union_map
,
908 isl_obj_union_pw_qpolynomial
,
909 (isc_un_op_fn
) &isl_union_map_card
} },
910 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
911 (isc_un_op_fn
) &isl_union_set_coalesce
} },
912 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
913 (isc_un_op_fn
) &isl_union_map_coalesce
} },
914 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
915 isl_obj_union_pw_qpolynomial
,
916 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
917 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
918 isl_obj_union_pw_qpolynomial_fold
,
919 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
921 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
923 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
926 {"coefficients", { -1, isl_obj_union_set
,
928 (isc_un_op_fn
) &isl_union_set_coefficients
} },
929 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
930 (isc_un_op_fn
) &isl_union_set_solutions
} },
931 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
932 (isc_un_op_fn
) &isl_union_map_deltas
} },
933 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
934 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
935 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
936 (isc_un_op_fn
) &isl_union_map_domain
} },
937 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
938 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
939 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
941 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
942 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
943 (isc_un_op_fn
) &isl_union_map_domain
} },
944 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
946 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
947 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
949 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
950 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
951 (isc_un_op_fn
) &isl_union_map_domain_map
} },
952 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
953 (isc_un_op_fn
) &isl_union_map_range
} },
954 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
955 (isc_un_op_fn
) &isl_union_map_range
} },
956 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
957 (isc_un_op_fn
) &isl_union_map_range_map
} },
958 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
959 (isc_un_op_fn
) &isl_union_set_identity
} },
960 {"lattice_width", { -1, isl_obj_union_set
,
961 isl_obj_union_pw_qpolynomial
,
962 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
963 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
964 (isc_un_op_fn
) &isl_union_map_lexmin
} },
965 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
966 (isc_un_op_fn
) &isl_union_map_lexmax
} },
967 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
968 (isc_un_op_fn
) &isl_union_set_lexmin
} },
969 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
970 (isc_un_op_fn
) &isl_union_set_lexmax
} },
971 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
972 (isc_un_op_fn
) &isl_union_set_lift
} },
973 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
974 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
975 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
976 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
977 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
978 isl_obj_union_pw_qpolynomial
,
979 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
980 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
981 isl_obj_union_pw_qpolynomial
,
982 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
983 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
984 isl_obj_union_pw_qpolynomial
,
985 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
987 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
988 (isc_un_op_fn
) &parse
} },
990 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
991 (isc_un_op_fn
) &union_map_power
} },
992 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
993 (isc_un_op_fn
) &union_set_sample
} },
994 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
995 (isc_un_op_fn
) &union_map_sample
} },
996 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
997 (isc_un_op_fn
) &union_set_scan
} },
998 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
999 (isc_un_op_fn
) &union_map_scan
} },
1000 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
1001 isl_obj_union_pw_qpolynomial
,
1002 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
1003 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
1004 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
1005 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
1006 (isc_un_op_fn
) &isl_union_set_unwrap
} },
1007 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
1008 (isc_un_op_fn
) &isl_union_map_wrap
} },
1009 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
1010 (isc_un_op_fn
) &isl_union_map_zip
} },
1014 struct isl_named_obj
{
1019 static void free_obj(struct isl_obj obj
)
1021 obj
.type
->free(obj
.v
);
1024 static int same_name(const void *entry
, const void *val
)
1026 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1028 return !strcmp(named
->name
, val
);
1031 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1032 char *name
, struct isl_obj obj
)
1034 struct isl_hash_table_entry
*entry
;
1036 struct isl_named_obj
*named
;
1038 name_hash
= isl_hash_string(isl_hash_init(), name
);
1039 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1043 named
= entry
->data
;
1044 free_obj(named
->obj
);
1047 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1051 entry
->data
= named
;
1062 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1063 struct isl_hash_table
*table
, char *name
)
1065 struct isl_obj obj
= { isl_obj_none
, NULL
};
1066 struct isl_hash_table_entry
*entry
;
1069 name_hash
= isl_hash_string(isl_hash_init(), name
);
1070 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1072 struct isl_named_obj
*named
;
1073 named
= entry
->data
;
1075 } else if (isdigit(name
[0]))
1076 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1078 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1081 obj
.v
= obj
.type
->copy(obj
.v
);
1085 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1087 if (obj
.type
== super
)
1089 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1091 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1093 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1094 super
== isl_obj_union_pw_qpolynomial
)
1096 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1097 super
== isl_obj_union_pw_qpolynomial_fold
)
1099 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1101 if (obj
.type
== isl_obj_list
) {
1102 struct isl_list
*list
= obj
.v
;
1103 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1104 return is_subtype(list
->obj
[0], super
);
1106 if (super
== isl_obj_str
)
1111 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1113 struct isl_list
*list
= obj
.v
;
1116 obj
.v
= obj
.type
->copy(obj
.v
);
1118 isl_list_free(list
);
1123 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1126 if (obj
.type
== type
)
1128 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1129 obj
.type
= isl_obj_union_map
;
1130 obj
.v
= isl_union_map_from_map(obj
.v
);
1133 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1134 obj
.type
= isl_obj_union_set
;
1135 obj
.v
= isl_union_set_from_set(obj
.v
);
1138 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1139 type
== isl_obj_union_pw_qpolynomial
) {
1140 obj
.type
= isl_obj_union_pw_qpolynomial
;
1141 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1144 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1145 type
== isl_obj_union_pw_qpolynomial_fold
) {
1146 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1147 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1150 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1151 if (type
== isl_obj_union_map
) {
1152 obj
.type
= isl_obj_union_map
;
1155 if (type
== isl_obj_union_pw_qpolynomial
) {
1156 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1157 isl_union_set_free(obj
.v
);
1158 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1159 obj
.type
= isl_obj_union_pw_qpolynomial
;
1162 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1163 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1164 isl_union_set_free(obj
.v
);
1165 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1167 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1171 if (obj
.type
== isl_obj_list
) {
1172 struct isl_list
*list
= obj
.v
;
1173 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1174 return convert(ctx
, obj_at(obj
, 0), type
);
1176 if (type
== isl_obj_str
) {
1181 p
= isl_printer_to_str(ctx
);
1184 p
= obj
.type
->print(p
, obj
.v
);
1185 s
= isl_printer_get_str(p
);
1186 isl_printer_free(p
);
1188 str
= isl_str_from_string(ctx
, s
);
1193 obj
.type
= isl_obj_str
;
1199 obj
.type
= isl_obj_none
;
1204 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1208 struct isl_token
*tok
;
1210 tok
= isl_stream_next_token(s
);
1214 for (i
= 0; ; ++i
) {
1217 if (bin_ops
[i
].op
!= tok
->type
)
1219 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1222 isl_token_free(tok
);
1226 for (i
= 0; ; ++i
) {
1227 if (!named_bin_ops
[i
].name
)
1229 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1231 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1234 isl_token_free(tok
);
1235 return &named_bin_ops
[i
].op
;
1238 isl_stream_push_token(s
, tok
);
1243 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1246 struct isl_token
*tok
;
1248 tok
= isl_stream_next_token(s
);
1252 for (i
= 0; ; ++i
) {
1253 if (!named_un_ops
[i
].name
)
1255 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1258 isl_token_free(tok
);
1259 return &named_un_ops
[i
].op
;
1262 isl_stream_push_token(s
, tok
);
1267 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1272 for (i
= 0; ; ++i
) {
1273 if (!named_un_ops
[i
].name
)
1275 if (named_un_ops
[i
].op
.op
!= like
->op
)
1277 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1280 return &named_un_ops
[i
].op
;
1286 static int is_assign(struct isl_stream
*s
)
1288 struct isl_token
*tok
;
1289 struct isl_token
*tok2
;
1292 tok
= isl_stream_next_token(s
);
1295 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1296 isl_stream_push_token(s
, tok
);
1300 tok2
= isl_stream_next_token(s
);
1302 isl_stream_push_token(s
, tok
);
1305 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1306 isl_stream_push_token(s
, tok2
);
1307 isl_stream_push_token(s
, tok
);
1312 static struct isl_obj
read_obj(struct isl_stream
*s
,
1313 struct isl_hash_table
*table
);
1314 static struct isl_obj
read_expr(struct isl_stream
*s
,
1315 struct isl_hash_table
*table
);
1317 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1318 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1320 struct isl_obj obj
= { isl_obj_none
, NULL
};
1322 obj
= read_obj(s
, table
);
1326 op
= find_matching_un_op(op
, obj
);
1329 isl_die(s
->ctx
, isl_error_invalid
,
1330 "no such unary operator defined on given operand",
1333 obj
= convert(s
->ctx
, obj
, op
->arg
);
1334 obj
.v
= op
->fn(obj
.v
);
1340 obj
.type
= isl_obj_none
;
1345 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1347 struct isl_list
*list
;
1350 if (obj
.type
!= isl_obj_union_map
)
1351 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1352 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1353 list
= isl_list_alloc(ctx
, 2);
1357 list
->obj
[0].type
= isl_obj_union_map
;
1358 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1359 list
->obj
[1].type
= isl_obj_bool
;
1360 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1362 obj
.type
= isl_obj_list
;
1363 if (exact
< 0 || !list
->obj
[0].v
)
1369 obj
.type
= isl_obj_none
;
1374 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1376 struct isl_list
*list
= obj
.v
;
1377 struct isl_token
*tok
;
1380 tok
= isl_stream_next_token(s
);
1381 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1382 isl_stream_error(s
, tok
, "expecting index");
1384 isl_stream_push_token(s
, tok
);
1387 i
= isl_int_get_si(tok
->u
.v
);
1388 isl_token_free(tok
);
1389 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1390 if (isl_stream_eat(s
, ']'))
1393 return obj_at(obj
, i
);
1396 obj
.type
= isl_obj_none
;
1401 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1402 struct isl_hash_table
*table
)
1406 obj
= read_expr(s
, table
);
1407 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1408 is_subtype(obj
, isl_obj_union_map
), goto error
);
1410 if (obj
.type
== isl_obj_list
) {
1411 struct isl_list
*list
= obj
.v
;
1412 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1413 obj
= obj_at(obj
, 0);
1415 if (obj
.type
== isl_obj_set
)
1416 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1417 else if (obj
.type
== isl_obj_map
)
1418 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1419 if (obj
.type
== isl_obj_union_set
) {
1420 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1422 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1426 if (isl_stream_eat(s
, ')'))
1431 isl_union_map_free(umap
);
1434 obj
.type
= isl_obj_none
;
1439 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1440 __isl_take isl_union_set
*uset
)
1442 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1443 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1445 obj
.type
= isl_obj_list
;
1446 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1451 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1452 __isl_take isl_union_map
*umap
)
1454 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1455 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1457 obj
.type
= isl_obj_list
;
1458 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1463 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1464 struct isl_obj obj
, struct isl_hash_table
*table
)
1468 arg
= read_expr(s
, table
);
1469 if (!is_subtype(arg
, isl_obj_union_map
) &&
1470 !is_subtype(arg
, isl_obj_union_set
))
1471 isl_die(s
->ctx
, isl_error_invalid
,
1472 "expecting set of map argument", goto error
);
1474 if (arg
.type
== isl_obj_list
) {
1475 struct isl_list
*list
= arg
.v
;
1476 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1477 arg
= obj_at(arg
, 0);
1479 if (arg
.type
== isl_obj_set
)
1480 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1481 else if (arg
.type
== isl_obj_map
)
1482 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1483 if (arg
.type
== isl_obj_union_set
)
1484 obj
= apply_fun_set(obj
, arg
.v
);
1486 obj
= apply_fun_map(obj
, arg
.v
);
1490 if (isl_stream_eat(s
, ')'))
1498 obj
.type
= isl_obj_none
;
1503 struct add_vertex_data
{
1504 struct isl_list
*list
;
1508 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1510 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1511 isl_basic_set
*expr
;
1513 expr
= isl_vertex_get_expr(vertex
);
1515 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1516 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1519 isl_vertex_free(vertex
);
1524 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1527 isl_basic_set
*hull
;
1528 isl_vertices
*vertices
= NULL
;
1529 struct isl_list
*list
= NULL
;
1531 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1533 set
= isl_set_remove_divs(set
);
1534 hull
= isl_set_convex_hull(set
);
1535 vertices
= isl_basic_set_compute_vertices(hull
);
1536 isl_basic_set_free(hull
);
1540 ctx
= isl_vertices_get_ctx(vertices
);
1541 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1546 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1548 data
->list
= isl_list_concat(list
, data
->list
);
1550 isl_vertices_free(vertices
);
1555 isl_vertices_free(vertices
);
1559 static struct isl_obj
vertices(struct isl_stream
*s
,
1560 struct isl_hash_table
*table
)
1564 struct isl_list
*list
= NULL
;
1565 isl_union_set
*uset
;
1566 struct add_vertex_data data
= { NULL
};
1568 obj
= read_expr(s
, table
);
1569 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1570 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1574 ctx
= isl_union_set_get_ctx(uset
);
1575 list
= isl_list_alloc(ctx
, 0);
1581 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1584 isl_union_set_free(uset
);
1586 obj
.type
= isl_obj_list
;
1591 isl_union_set_free(uset
);
1592 isl_list_free(data
.list
);
1594 obj
.type
= isl_obj_none
;
1599 static struct isl_obj
type_of(struct isl_stream
*s
,
1600 struct isl_hash_table
*table
)
1604 const char *type
= "unknown";
1606 obj
= read_expr(s
, table
);
1608 if (obj
.type
== isl_obj_map
||
1609 obj
.type
== isl_obj_union_map
)
1611 if (obj
.type
== isl_obj_set
||
1612 obj
.type
== isl_obj_union_set
)
1614 if (obj
.type
== isl_obj_pw_qpolynomial
||
1615 obj
.type
== isl_obj_union_pw_qpolynomial
)
1616 type
= "piecewise quasipolynomial";
1617 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1618 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1619 type
= "piecewise quasipolynomial fold";
1620 if (obj
.type
== isl_obj_list
)
1622 if (obj
.type
== isl_obj_bool
)
1624 if (obj
.type
== isl_obj_str
)
1626 if (obj
.type
== isl_obj_int
)
1630 obj
.type
= isl_obj_str
;
1631 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1636 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1637 struct isl_hash_table
*table
)
1641 obj
= read_obj(s
, table
);
1642 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1643 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1650 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1651 struct isl_hash_table
*table
)
1655 obj
= read_obj(s
, table
);
1656 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1657 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1664 static struct isl_obj
last_any(struct isl_stream
*s
,
1665 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1666 __isl_take isl_union_map
*may_source
)
1668 struct isl_obj obj
= { isl_obj_none
, NULL
};
1669 isl_union_map
*sink
= NULL
;
1670 isl_union_map
*schedule
= NULL
;
1671 isl_union_map
*may_dep
;
1672 isl_union_map
*must_dep
;
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 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1689 schedule
, &must_dep
, &may_dep
,
1693 obj
.type
= isl_obj_union_map
;
1694 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1698 isl_union_map_free(may_source
);
1699 isl_union_map_free(must_source
);
1700 isl_union_map_free(sink
);
1701 isl_union_map_free(schedule
);
1703 obj
.type
= isl_obj_none
;
1708 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1710 struct isl_obj obj
= { isl_obj_none
, NULL
};
1711 isl_union_map
*must_source
= NULL
;
1712 isl_union_map
*may_source
= NULL
;
1713 isl_union_map
*sink
= NULL
;
1714 isl_union_map
*schedule
= NULL
;
1715 isl_union_map
*may_dep
;
1717 may_source
= read_map(s
, table
);
1721 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1722 must_source
= read_map(s
, table
);
1725 return last_any(s
, table
, must_source
, may_source
);
1728 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1731 sink
= read_map(s
, table
);
1735 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1738 schedule
= read_map(s
, table
);
1742 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1743 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1744 schedule
, NULL
, &may_dep
,
1748 obj
.type
= isl_obj_union_map
;
1753 isl_union_map_free(may_source
);
1754 isl_union_map_free(must_source
);
1755 isl_union_map_free(sink
);
1756 isl_union_map_free(schedule
);
1758 obj
.type
= isl_obj_none
;
1763 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1765 struct isl_obj obj
= { isl_obj_none
, NULL
};
1766 struct isl_list
*list
= NULL
;
1767 isl_union_map
*must_source
= NULL
;
1768 isl_union_map
*may_source
= NULL
;
1769 isl_union_map
*sink
= NULL
;
1770 isl_union_map
*schedule
= NULL
;
1771 isl_union_map
*must_dep
;
1772 isl_union_map
*must_no_source
;
1774 must_source
= read_map(s
, table
);
1778 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1779 may_source
= read_map(s
, table
);
1782 return last_any(s
, table
, must_source
, may_source
);
1785 list
= isl_list_alloc(s
->ctx
, 2);
1789 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1792 sink
= read_map(s
, table
);
1796 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1799 schedule
= read_map(s
, table
);
1803 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1804 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1805 schedule
, &must_dep
, NULL
,
1806 &must_no_source
, NULL
) < 0) {
1807 isl_list_free(list
);
1811 list
->obj
[0].type
= isl_obj_union_map
;
1812 list
->obj
[0].v
= must_dep
;
1813 list
->obj
[1].type
= isl_obj_union_map
;
1814 list
->obj
[1].v
= must_no_source
;
1817 obj
.type
= isl_obj_list
;
1821 isl_list_free(list
);
1822 isl_union_map_free(may_source
);
1823 isl_union_map_free(must_source
);
1824 isl_union_map_free(sink
);
1825 isl_union_map_free(schedule
);
1827 obj
.type
= isl_obj_none
;
1832 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1833 struct isl_hash_table
*table
)
1835 isl_union_set
*domain
;
1836 isl_union_map
*validity
;
1837 isl_union_map
*proximity
;
1839 domain
= read_set(s
, table
);
1843 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1844 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1847 isl_union_map
*umap
;
1848 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1849 umap
= read_map(s
, table
);
1850 validity
= isl_union_map_union(validity
, umap
);
1851 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1852 umap
= read_map(s
, table
);
1853 proximity
= isl_union_map_union(proximity
, umap
);
1858 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1861 static struct isl_obj
schedule(struct isl_stream
*s
,
1862 struct isl_hash_table
*table
)
1864 struct isl_obj obj
= { isl_obj_none
, NULL
};
1865 isl_schedule
*schedule
;
1867 schedule
= get_schedule(s
, table
);
1869 obj
.v
= isl_schedule_get_map(schedule
);
1870 obj
.type
= isl_obj_union_map
;
1872 isl_schedule_free(schedule
);
1877 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1879 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1881 struct isl_obj obj
= { isl_obj_none
, NULL
};
1882 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1883 struct isl_list
*list
;
1885 list
= isl_list_alloc(ctx
, 2);
1890 obj
.type
= isl_obj_list
;
1892 list
->obj
[0].type
= isl_obj_union_map
;
1893 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1895 if (isl_band_has_children(band
)) {
1896 isl_band_list
*children
;
1898 children
= isl_band_get_children(band
);
1899 list
->obj
[1] = band_list_to_obj_list(children
);
1901 list
->obj
[1].type
= isl_obj_list
;
1902 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1905 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1908 isl_band_free(band
);
1912 isl_band_free(band
);
1914 obj
.type
= isl_obj_none
;
1919 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1921 struct isl_obj obj
= { isl_obj_none
, NULL
};
1922 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1923 struct isl_list
*list
;
1926 n
= isl_band_list_n_band(bands
);
1927 list
= isl_list_alloc(ctx
, n
);
1932 obj
.type
= isl_obj_list
;
1934 for (i
= 0; i
< n
; ++i
) {
1937 band
= isl_band_list_get_band(bands
, i
);
1938 list
->obj
[i
] = band_to_obj_list(band
);
1939 if (!list
->obj
[i
].v
)
1943 isl_band_list_free(bands
);
1947 isl_band_list_free(bands
);
1949 obj
.type
= isl_obj_none
;
1954 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1955 struct isl_hash_table
*table
)
1957 struct isl_obj obj
= { isl_obj_none
, NULL
};
1958 isl_schedule
*schedule
;
1959 isl_band_list
*roots
;
1961 schedule
= get_schedule(s
, table
);
1965 roots
= isl_schedule_get_band_forest(schedule
);
1966 isl_schedule_free(schedule
);
1968 return band_list_to_obj_list(roots
);
1971 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1973 struct isl_token
*tok
;
1975 if (isl_stream_eat_if_available(s
, '+'))
1976 return transitive_closure(s
->ctx
, obj
);
1978 tok
= isl_stream_next_token(s
);
1979 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1980 isl_stream_error(s
, tok
, "expecting -1");
1982 isl_stream_push_token(s
, tok
);
1985 isl_token_free(tok
);
1986 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1987 if (obj
.type
!= isl_obj_union_map
)
1988 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1990 obj
.v
= isl_union_map_reverse(obj
.v
);
1997 obj
.type
= isl_obj_none
;
2002 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2005 struct isl_token
*tok
;
2006 struct isl_stream
*s_file
;
2007 struct iscc_options
*options
;
2010 tok
= isl_stream_next_token(s
);
2011 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2012 isl_stream_error(s
, tok
, "expecting filename");
2013 isl_token_free(tok
);
2017 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2018 if (!options
|| !options
->io
) {
2019 isl_token_free(tok
);
2020 isl_die(s
->ctx
, isl_error_invalid
,
2021 "read operation not allowed", goto error
);
2024 file
= fopen(tok
->u
.s
, "r");
2025 isl_token_free(tok
);
2026 isl_assert(s
->ctx
, file
, goto error
);
2028 s_file
= isl_stream_new_file(s
->ctx
, file
);
2034 obj
= isl_stream_read_obj(s_file
);
2036 isl_stream_free(s_file
);
2041 obj
.type
= isl_obj_none
;
2046 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2047 struct isl_hash_table
*table
)
2050 struct isl_token
*tok
;
2051 struct isl_stream
*s_file
;
2052 struct iscc_options
*options
;
2056 tok
= isl_stream_next_token(s
);
2057 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2058 isl_stream_error(s
, tok
, "expecting filename");
2059 isl_token_free(tok
);
2063 obj
= read_expr(s
, table
);
2065 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2066 if (!options
|| !options
->io
) {
2067 isl_token_free(tok
);
2068 isl_die(s
->ctx
, isl_error_invalid
,
2069 "write operation not allowed", goto error
);
2072 file
= fopen(tok
->u
.s
, "w");
2073 isl_token_free(tok
);
2075 isl_die(s
->ctx
, isl_error_unknown
,
2076 "could not open file for writing", goto error
);
2078 p
= isl_printer_to_file(s
->ctx
, file
);
2079 p
= isl_printer_set_output_format(p
, options
->format
);
2080 p
= obj
.type
->print(p
, obj
.v
);
2081 p
= isl_printer_end_line(p
);
2082 isl_printer_free(p
);
2087 obj
.type
= isl_obj_none
;
2092 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2094 struct isl_token
*tok
;
2095 struct isl_obj obj
= { isl_obj_none
, NULL
};
2097 tok
= isl_stream_next_token(s
);
2100 if (tok
->type
== ISL_TOKEN_STRING
) {
2102 str
= isl_str_alloc(s
->ctx
);
2105 str
->s
= strdup(tok
->u
.s
);
2106 isl_token_free(tok
);
2108 obj
.type
= isl_obj_str
;
2110 isl_stream_push_token(s
, tok
);
2113 isl_token_free(tok
);
2117 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2119 struct isl_token
*tok
;
2120 struct isl_obj obj
= { isl_obj_none
, NULL
};
2122 tok
= isl_stream_next_token(s
);
2125 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2126 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2127 isl_token_free(tok
);
2128 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2129 obj
.type
= isl_obj_bool
;
2131 isl_stream_push_token(s
, tok
);
2134 isl_token_free(tok
);
2138 static __isl_give
char *read_ident(struct isl_stream
*s
)
2141 struct isl_token
*tok
, *tok2
;
2143 name
= isl_stream_read_ident_if_available(s
);
2147 tok
= isl_stream_next_token(s
);
2150 if (tok
->type
!= '$') {
2151 isl_stream_push_token(s
, tok
);
2154 tok2
= isl_stream_next_token(s
);
2155 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2157 isl_stream_push_token(s
, tok2
);
2158 isl_stream_push_token(s
, tok
);
2162 name
= isl_int_get_str(tok2
->u
.v
);
2163 isl_token_free(tok
);
2164 isl_token_free(tok2
);
2169 static struct isl_obj
read_list(struct isl_stream
*s
,
2170 struct isl_hash_table
*table
, struct isl_obj obj
)
2172 struct isl_list
*list
;
2174 list
= isl_list_alloc(s
->ctx
, 2);
2178 list
->obj
[1] = read_obj(s
, table
);
2180 obj
.type
= isl_obj_list
;
2182 if (!list
->obj
[1].v
)
2185 while (isl_stream_eat_if_available(s
, ',')) {
2186 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2194 obj
.type
= isl_obj_none
;
2199 static struct isl_obj
read_obj(struct isl_stream
*s
,
2200 struct isl_hash_table
*table
)
2202 struct isl_obj obj
= { isl_obj_none
, NULL
};
2204 struct isc_un_op
*op
= NULL
;
2206 obj
= read_string_if_available(s
);
2209 obj
= read_bool_if_available(s
);
2212 if (isl_stream_eat_if_available(s
, '(')) {
2213 if (isl_stream_next_token_is(s
, ')')) {
2214 obj
.type
= isl_obj_list
;
2215 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2217 obj
= read_expr(s
, table
);
2218 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2219 obj
= read_list(s
, table
, obj
);
2221 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2224 op
= read_prefix_un_op_if_available(s
);
2226 return read_un_op_expr(s
, table
, op
);
2228 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2229 return read_from_file(s
);
2230 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2231 return write_to_file(s
, table
);
2232 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2233 return vertices(s
, table
);
2234 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2235 return any(s
, table
);
2236 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2237 return last(s
, table
);
2238 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2239 return schedule(s
, table
);
2240 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2241 return schedule_forest(s
, table
);
2242 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2243 return type_of(s
, table
);
2245 name
= read_ident(s
);
2247 obj
= stored_obj(s
->ctx
, table
, name
);
2249 obj
= isl_stream_read_obj(s
);
2254 if (isl_stream_eat_if_available(s
, '^'))
2255 obj
= power(s
, obj
);
2256 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2257 obj
= obj_at_index(s
, obj
);
2258 else if (is_subtype(obj
, isl_obj_union_map
) &&
2259 isl_stream_eat_if_available(s
, '(')) {
2260 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2261 obj
= apply(s
, obj
.v
, table
);
2262 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2263 isl_stream_eat_if_available(s
, '(')) {
2264 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2265 obj
= apply_fun(s
, obj
, table
);
2266 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2267 isl_stream_eat_if_available(s
, '(')) {
2268 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2269 obj
= apply_fun(s
, obj
, table
);
2275 obj
.type
= isl_obj_none
;
2280 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2281 struct isl_obj lhs
, struct isl_obj rhs
)
2285 for (i
= 0; ; ++i
) {
2288 if (bin_ops
[i
].op
!= like
->op
)
2290 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2292 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2298 for (i
= 0; ; ++i
) {
2299 if (!named_bin_ops
[i
].name
)
2301 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2303 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2305 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2308 return &named_bin_ops
[i
].op
;
2314 static int next_is_neg_int(struct isl_stream
*s
)
2316 struct isl_token
*tok
;
2319 tok
= isl_stream_next_token(s
);
2320 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2321 isl_stream_push_token(s
, tok
);
2326 static struct isl_obj
read_expr(struct isl_stream
*s
,
2327 struct isl_hash_table
*table
)
2329 struct isl_obj obj
= { isl_obj_none
, NULL
};
2330 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2332 obj
= read_obj(s
, table
);
2334 struct isc_bin_op
*op
= NULL
;
2336 op
= read_bin_op_if_available(s
, obj
);
2340 right_obj
= read_obj(s
, table
);
2342 op
= find_matching_bin_op(op
, obj
, right_obj
);
2345 isl_die(s
->ctx
, isl_error_invalid
,
2346 "no such binary operator defined on given operands",
2349 obj
= convert(s
->ctx
, obj
, op
->lhs
);
2350 right_obj
= convert(s
->ctx
, right_obj
, op
->rhs
);
2351 obj
.v
= op
->fn(obj
.v
, right_obj
.v
);
2355 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2356 right_obj
= read_obj(s
, table
);
2357 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2362 free_obj(right_obj
);
2364 obj
.type
= isl_obj_none
;
2369 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2370 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2372 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2373 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2375 struct isl_obj obj
= { isl_obj_none
, NULL
};
2379 struct isc_bin_op
*op
= NULL
;
2384 if (isl_stream_is_empty(s
))
2387 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2388 return source_file(s
, table
, p
);
2390 assign
= is_assign(s
);
2392 lhs
= isl_stream_read_ident_if_available(s
);
2393 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2395 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2400 obj
= read_expr(s
, table
);
2401 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2402 fprintf(stderr
, "Interrupted\n");
2403 isl_ctx_reset_error(s
->ctx
);
2405 if (isl_stream_eat(s
, ';'))
2409 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
)
2410 p
= obj
.type
->print(p
, obj
.v
);
2411 p
= isl_printer_end_line(p
);
2415 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2416 static int count
= 0;
2417 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2418 lhs
= strdup(buf
+ 1);
2420 p
= isl_printer_print_str(p
, buf
);
2421 p
= isl_printer_print_str(p
, " := ");
2422 p
= obj
.type
->print(p
, obj
.v
);
2423 p
= isl_printer_end_line(p
);
2425 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2430 isl_stream_flush_tokens(s
);
2431 isl_stream_skip_line(s
);
2437 int free_cb(void **entry
, void *user
)
2439 struct isl_named_obj
*named
= *entry
;
2441 free_obj(named
->obj
);
2448 static void register_named_ops(struct isl_stream
*s
)
2452 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2453 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2454 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2457 for (i
= 0; ; ++i
) {
2458 if (!named_un_ops
[i
].name
)
2460 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2461 named_un_ops
[i
].name
);
2462 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2465 for (i
= 0; ; ++i
) {
2466 if (!named_bin_ops
[i
].name
)
2468 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2469 named_bin_ops
[i
].name
);
2470 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2474 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2475 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2477 struct isl_token
*tok
;
2478 struct isl_stream
*s_file
;
2481 tok
= isl_stream_next_token(s
);
2482 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2483 isl_stream_error(s
, tok
, "expecting filename");
2484 isl_token_free(tok
);
2488 file
= fopen(tok
->u
.s
, "r");
2489 isl_token_free(tok
);
2490 isl_assert(s
->ctx
, file
, return p
);
2492 s_file
= isl_stream_new_file(s
->ctx
, file
);
2498 register_named_ops(s_file
);
2500 while (!s_file
->eof
)
2501 p
= read_line(s_file
, table
, p
, 0);
2503 isl_stream_free(s_file
);
2506 isl_stream_eat(s
, ';');
2511 int main(int argc
, char **argv
)
2513 struct isl_ctx
*ctx
;
2514 struct isl_stream
*s
;
2515 struct isl_hash_table
*table
;
2516 struct iscc_options
*options
;
2518 int tty
= isatty(0);
2520 options
= iscc_options_new_with_defaults();
2522 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
2524 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
2525 s
= isl_stream_new_file(ctx
, stdin
);
2527 table
= isl_hash_table_alloc(ctx
, 10);
2529 p
= isl_printer_to_file(ctx
, stdout
);
2530 p
= isl_printer_set_output_format(p
, options
->format
);
2533 register_named_ops(s
);
2535 install_signal_handler(ctx
);
2537 while (p
&& !s
->eof
) {
2538 isl_ctx_resume(ctx
);
2539 p
= read_line(s
, table
, p
, tty
);
2542 remove_signal_handler(ctx
);
2544 isl_printer_free(p
);
2545 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2546 isl_hash_table_free(ctx
, table
);