7 #include <isl/stream.h>
10 #include <isl/vertices.h>
13 #include <isl/schedule.h>
14 #include <isl/ast_build.h>
15 #include <isl_obj_list.h>
16 #include <isl_obj_str.h>
17 #include <barvinok/isl.h>
18 #include <barvinok/options.h>
19 #include "lattice_width.h"
26 static isl_ctx
*main_ctx
;
28 static void handler(int signum
)
30 if (isl_ctx_aborted(main_ctx
))
32 isl_ctx_abort(main_ctx
);
35 static struct sigaction sa_old
;
37 static void install_signal_handler(isl_ctx
*ctx
)
43 memset(&sa
, 0, sizeof(struct sigaction
));
44 sa
.sa_handler
= &handler
;
45 sa
.sa_flags
= SA_RESTART
;
46 sigaction(SIGINT
, &sa
, &sa_old
);
49 static void remove_signal_handler(isl_ctx
*ctx
)
51 sigaction(SIGINT
, &sa_old
, NULL
);
56 static void install_signal_handler(isl_ctx
*ctx
)
60 static void remove_signal_handler(isl_ctx
*ctx
)
70 int pet_options_set_autodetect(isl_ctx
*ctx
, int val
)
76 static int isl_bool_false
= 0;
77 static int isl_bool_true
= 1;
78 static int isl_bool_error
= -1;
80 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
81 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
82 ISCC_SCHEDULE
, ISCC_SCHEDULE_FOREST
,
83 ISCC_MINIMIZING
, ISCC_RESPECTING
,
84 ISCC_CODEGEN
, ISCC_USING
,
85 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
87 static const char *op_name
[ISCC_N_OP
] = {
88 [ISCC_ASSERT
] = "assert",
90 [ISCC_WRITE
] = "write",
91 [ISCC_PRINT
] = "print",
92 [ISCC_SOURCE
] = "source",
93 [ISCC_VERTICES
] = "vertices",
96 [ISCC_BEFORE
] = "before",
97 [ISCC_UNDER
] = "under",
98 [ISCC_SCHEDULE
] = "schedule",
99 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
100 [ISCC_MINIMIZING
] = "minimizing",
101 [ISCC_RESPECTING
] = "respecting",
102 [ISCC_CODEGEN
] = "codegen",
103 [ISCC_USING
] = "using",
104 [ISCC_TYPEOF
] = "typeof"
106 static enum isl_token_type iscc_op
[ISCC_N_OP
];
108 struct isl_arg_choice iscc_format
[] = {
109 {"isl", ISL_FORMAT_ISL
},
110 {"omega", ISL_FORMAT_OMEGA
},
111 {"polylib", ISL_FORMAT_POLYLIB
},
112 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
113 {"latex", ISL_FORMAT_LATEX
},
118 struct iscc_options
{
119 struct barvinok_options
*barvinok
;
120 struct pet_options
*pet
;
125 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
126 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
129 ISL_ARG_CHILD(struct iscc_options
, pet
, "pet", &pet_options_args
, "pet options")
131 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
132 iscc_format
, ISL_FORMAT_ISL
, "output format")
133 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
134 "allow read and write operations")
137 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
138 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
140 static void *isl_obj_bool_copy(void *v
)
145 static void isl_obj_bool_free(void *v
)
149 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
152 if (v
== &isl_bool_true
)
153 return isl_printer_print_str(p
, "True");
154 else if (v
== &isl_bool_false
)
155 return isl_printer_print_str(p
, "False");
157 return isl_printer_print_str(p
, "Error");
160 static void *isl_obj_bool_add(void *v1
, void *v2
)
165 struct isl_obj_vtable isl_obj_bool_vtable
= {
171 #define isl_obj_bool (&isl_obj_bool_vtable)
173 int *isl_bool_from_int(int res
)
175 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
178 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
179 __isl_take isl_union_map
*map2
)
181 return isl_union_map_is_subset(map2
, map1
);
183 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
184 __isl_take isl_union_set
*set2
)
186 return isl_union_set_is_subset(set2
, set1
);
189 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
190 __isl_take isl_union_map
*map2
)
192 return isl_union_map_is_strict_subset(map2
, map1
);
194 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
195 __isl_take isl_union_set
*set2
)
197 return isl_union_set_is_strict_subset(set2
, set1
);
200 extern struct isl_obj_vtable isl_obj_list_vtable
;
201 #define isl_obj_list (&isl_obj_list_vtable)
203 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
204 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
206 enum isl_token_type op
;
212 isc_bin_test_fn test
;
215 struct isc_named_bin_op
{
217 struct isc_bin_op op
;
221 isl_union_pw_qpolynomial
*upwqp
;
222 isl_union_pw_qpolynomial
*res
;
225 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
227 struct iscc_at
*at
= (struct iscc_at
*) user
;
232 set
= isl_set_from_point(isl_point_copy(pnt
));
233 v
= isl_union_pw_qpolynomial_eval(
234 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
235 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
237 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
238 isl_union_pw_qpolynomial_from_pw_qpolynomial(
239 isl_pw_qpolynomial_alloc(set
, qp
)));
244 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
245 __isl_take isl_union_pw_qpolynomial
*upwqp
,
246 __isl_take isl_union_set
*uset
)
251 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
253 isl_union_set_foreach_point(uset
, eval_at
, &at
);
255 isl_union_pw_qpolynomial_free(upwqp
);
256 isl_union_set_free(uset
);
261 struct iscc_fold_at
{
262 isl_union_pw_qpolynomial_fold
*upwf
;
263 isl_union_pw_qpolynomial
*res
;
266 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
268 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
273 set
= isl_set_from_point(isl_point_copy(pnt
));
274 v
= isl_union_pw_qpolynomial_fold_eval(
275 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
276 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
278 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
279 isl_union_pw_qpolynomial_from_pw_qpolynomial(
280 isl_pw_qpolynomial_alloc(set
, qp
)));
285 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
286 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
287 __isl_take isl_union_set
*uset
)
289 struct iscc_fold_at at
;
292 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
294 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
296 isl_union_pw_qpolynomial_fold_free(upwf
);
297 isl_union_set_free(uset
);
302 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
303 __isl_take isl_union_pw_qpolynomial
*upwqp
,
304 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
306 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
310 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
311 __isl_take isl_union_map
*umap
,
312 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
315 struct isl_list
*list
;
318 ctx
= isl_union_map_get_ctx(umap
);
319 list
= isl_list_alloc(ctx
, 2);
323 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
324 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
326 list
->obj
[1].type
= isl_obj_bool
;
327 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
328 if (tight
< 0 || !list
->obj
[0].v
)
333 isl_union_map_free(umap
);
334 isl_union_pw_qpolynomial_fold_free(upwf
);
340 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
341 __isl_take isl_union_set
*uset
,
342 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
345 struct isl_list
*list
;
348 ctx
= isl_union_set_get_ctx(uset
);
349 list
= isl_list_alloc(ctx
, 2);
353 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
354 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
356 list
->obj
[1].type
= isl_obj_bool
;
357 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
358 if (tight
< 0 || !list
->obj
[0].v
)
363 isl_union_set_free(uset
);
364 isl_union_pw_qpolynomial_fold_free(upwf
);
370 static __isl_give isl_union_pw_qpolynomial
*isl_val_mul_union_pw_qpolynomial(
371 __isl_take isl_val
*v
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
373 return isl_union_pw_qpolynomial_scale_val(upwqp
, v
);
376 static __isl_give isl_union_pw_qpolynomial_fold
*
377 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val
*v
,
378 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
380 return isl_union_pw_qpolynomial_fold_scale_val(upwf
, v
);
383 struct isc_bin_op bin_ops
[] = {
384 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
385 (isc_bin_op_fn
) &isl_val_add
},
386 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
387 (isc_bin_op_fn
) &isl_val_sub
},
388 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
389 (isc_bin_op_fn
) &isl_val_mul
},
390 { '+', isl_obj_union_set
, isl_obj_union_set
,
392 (isc_bin_op_fn
) &isl_union_set_union
},
393 { '+', isl_obj_union_map
, isl_obj_union_map
,
395 (isc_bin_op_fn
) &isl_union_map_union
},
396 { '-', isl_obj_union_set
, isl_obj_union_set
,
398 (isc_bin_op_fn
) &isl_union_set_subtract
},
399 { '-', isl_obj_union_map
, isl_obj_union_map
,
401 (isc_bin_op_fn
) &isl_union_map_subtract
},
402 { '*', isl_obj_union_set
, isl_obj_union_set
,
404 (isc_bin_op_fn
) &isl_union_set_intersect
},
405 { '*', isl_obj_union_map
, isl_obj_union_map
,
407 (isc_bin_op_fn
) &isl_union_map_intersect
},
408 { '*', isl_obj_union_map
, isl_obj_union_set
,
410 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
411 { '.', isl_obj_union_map
, isl_obj_union_map
,
413 (isc_bin_op_fn
) &isl_union_map_apply_range
},
414 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
415 isl_obj_union_pw_qpolynomial
,
416 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
417 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
419 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
420 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
422 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
423 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
424 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
425 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
426 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
427 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
429 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
430 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
432 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
433 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
435 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
436 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
438 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
439 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
441 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
442 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
444 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
445 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
448 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
449 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
452 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
453 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
455 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
456 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
458 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
459 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
461 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
462 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
464 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
465 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
467 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
468 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
470 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
471 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
473 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
474 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
476 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
477 { '.', isl_obj_union_pw_qpolynomial_fold
,
478 isl_obj_union_pw_qpolynomial_fold
,
479 isl_obj_union_pw_qpolynomial_fold
,
480 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
481 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
482 isl_obj_union_pw_qpolynomial
,
483 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
484 { '+', isl_obj_union_pw_qpolynomial
,
485 isl_obj_union_pw_qpolynomial_fold
,
486 isl_obj_union_pw_qpolynomial_fold
,
487 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
488 { '+', isl_obj_union_pw_qpolynomial_fold
,
489 isl_obj_union_pw_qpolynomial
,
490 isl_obj_union_pw_qpolynomial_fold
,
491 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
492 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
493 isl_obj_union_pw_qpolynomial
,
494 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
495 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
496 isl_obj_union_pw_qpolynomial
,
497 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
498 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
499 isl_obj_union_pw_qpolynomial
,
500 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
501 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
502 isl_obj_union_pw_qpolynomial_fold
,
503 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
504 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
505 isl_obj_union_pw_qpolynomial_fold
,
506 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
507 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
508 isl_obj_union_pw_qpolynomial
,
509 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
510 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
511 isl_obj_union_pw_qpolynomial
,
512 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
513 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
514 isl_obj_union_pw_qpolynomial_fold
,
515 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
516 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
517 isl_obj_union_pw_qpolynomial
,
518 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
519 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
520 isl_obj_union_pw_qpolynomial
,
521 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
522 { '%', isl_obj_union_set
, isl_obj_union_set
,
524 (isc_bin_op_fn
) &isl_union_set_gist
},
525 { '%', isl_obj_union_map
, isl_obj_union_map
,
527 (isc_bin_op_fn
) &isl_union_map_gist
},
528 { '%', isl_obj_union_map
, isl_obj_union_set
,
530 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
531 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
532 isl_obj_union_pw_qpolynomial
,
533 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
534 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
535 isl_obj_union_pw_qpolynomial_fold
,
536 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
537 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
538 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
539 { .test
= (isc_bin_test_fn
)
540 &isl_union_pw_qpolynomial_plain_is_equal
} },
541 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
542 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
543 { .test
= (isc_bin_test_fn
)
544 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
545 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
546 (isc_bin_op_fn
) &isl_str_concat
},
550 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
551 __isl_take isl_union_map
*umap2
)
553 return isl_union_map_apply_range(umap2
, umap1
);
556 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
557 __isl_take isl_union_pw_qpolynomial
*upwqp
,
558 __isl_take isl_union_map
*umap
)
560 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
563 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
564 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
565 __isl_take isl_union_map
*umap
)
567 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
570 struct isc_named_bin_op named_bin_ops
[] = {
571 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
573 (isc_bin_op_fn
) &map_after_map
} },
574 { "after", { -1, isl_obj_union_pw_qpolynomial
,
575 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
576 (isc_bin_op_fn
) &qpolynomial_after_map
} },
577 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
578 isl_obj_union_map
, isl_obj_list
,
579 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
580 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
582 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
583 { "before", { -1, isl_obj_union_map
,
584 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
585 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
586 { "before", { -1, isl_obj_union_map
,
587 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
588 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
589 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
591 (isc_bin_op_fn
) &isl_union_set_product
} },
592 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
594 (isc_bin_op_fn
) &isl_union_map_product
} },
598 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
600 return isl_set_from_basic_set(isl_union_set_sample(uset
));
603 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
605 return isl_map_from_basic_map(isl_union_map_sample(umap
));
608 static __isl_give
struct isl_list
*union_map_power(
609 __isl_take isl_union_map
*umap
)
612 struct isl_list
*list
;
615 ctx
= isl_union_map_get_ctx(umap
);
616 list
= isl_list_alloc(ctx
, 2);
620 list
->obj
[0].type
= isl_obj_union_map
;
621 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
622 list
->obj
[1].type
= isl_obj_bool
;
623 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
624 if (exact
< 0 || !list
->obj
[0].v
)
629 isl_union_map_free(umap
);
635 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
636 __isl_take isl_union_pw_qpolynomial
*upwqp
)
639 struct isl_list
*list
;
642 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
643 list
= isl_list_alloc(ctx
, 2);
647 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
648 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
649 isl_fold_max
, &tight
);
650 list
->obj
[1].type
= isl_obj_bool
;
651 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
652 if (tight
< 0 || !list
->obj
[0].v
)
657 isl_union_pw_qpolynomial_free(upwqp
);
664 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
667 struct isl_list
*list
;
668 struct pet_scop
*scop
;
669 isl_union_map
*sched
, *may_reads
, *must_writes
, *may_writes
;
670 isl_union_set
*domain
;
671 struct iscc_options
*options
;
677 options
= isl_ctx_peek_iscc_options(ctx
);
678 if (!options
|| !options
->io
) {
680 isl_die(ctx
, isl_error_invalid
,
681 "parse_file operation not allowed", return NULL
);
684 list
= isl_list_alloc(ctx
, 5);
688 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
689 domain
= pet_scop_collect_domains(scop
);
690 sched
= pet_scop_collect_schedule(scop
);
691 may_reads
= pet_scop_collect_may_reads(scop
);
692 may_writes
= pet_scop_collect_may_writes(scop
);
693 must_writes
= pet_scop_collect_must_writes(scop
);
696 list
->obj
[0].type
= isl_obj_union_set
;
697 list
->obj
[0].v
= domain
;
698 list
->obj
[1].type
= isl_obj_union_map
;
699 list
->obj
[1].v
= must_writes
;
700 list
->obj
[2].type
= isl_obj_union_map
;
701 list
->obj
[2].v
= may_writes
;
702 list
->obj
[3].type
= isl_obj_union_map
;
703 list
->obj
[3].v
= may_reads
;
704 list
->obj
[4].type
= isl_obj_union_map
;
705 list
->obj
[4].v
= sched
;
707 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
708 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
720 static int add_point(__isl_take isl_point
*pnt
, void *user
)
722 isl_union_set
**scan
= (isl_union_set
**) user
;
724 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
729 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
733 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
735 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
736 isl_union_set_free(scan
);
740 isl_union_set_free(uset
);
744 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
746 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
749 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
750 __isl_take isl_union_pw_qpolynomial
*upwqp
)
752 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
755 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
756 __isl_take isl_union_pw_qpolynomial
*upwqp
)
758 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
761 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
762 __isl_take isl_union_pw_qpolynomial
*upwqp
)
764 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
767 typedef void *(*isc_un_op_fn
)(void *arg
);
769 enum isl_token_type op
;
774 struct isc_named_un_op
{
778 struct isc_named_un_op named_un_ops
[] = {
779 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
780 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
781 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
782 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
783 {"card", { -1, isl_obj_union_set
,
784 isl_obj_union_pw_qpolynomial
,
785 (isc_un_op_fn
) &isl_union_set_card
} },
786 {"card", { -1, isl_obj_union_map
,
787 isl_obj_union_pw_qpolynomial
,
788 (isc_un_op_fn
) &isl_union_map_card
} },
789 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
790 (isc_un_op_fn
) &isl_union_set_coalesce
} },
791 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
792 (isc_un_op_fn
) &isl_union_map_coalesce
} },
793 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
794 isl_obj_union_pw_qpolynomial
,
795 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
796 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
797 isl_obj_union_pw_qpolynomial_fold
,
798 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
799 {"coefficients", { -1, isl_obj_union_set
,
801 (isc_un_op_fn
) &isl_union_set_coefficients
} },
802 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
803 (isc_un_op_fn
) &isl_union_set_solutions
} },
804 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
805 (isc_un_op_fn
) &isl_union_map_deltas
} },
806 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
807 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
808 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
809 (isc_un_op_fn
) &isl_union_map_domain
} },
810 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
811 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
812 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
814 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
815 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
816 (isc_un_op_fn
) &isl_union_map_domain
} },
817 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
819 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
820 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
822 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
823 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
824 (isc_un_op_fn
) &isl_union_map_domain_map
} },
825 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
826 (isc_un_op_fn
) &isl_union_map_range
} },
827 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
828 (isc_un_op_fn
) &isl_union_map_range
} },
829 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
830 (isc_un_op_fn
) &isl_union_map_range_map
} },
831 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
832 (isc_un_op_fn
) &isl_union_set_identity
} },
833 {"lattice_width", { -1, isl_obj_union_set
,
834 isl_obj_union_pw_qpolynomial
,
835 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
836 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
837 (isc_un_op_fn
) &isl_union_map_lexmin
} },
838 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
839 (isc_un_op_fn
) &isl_union_map_lexmax
} },
840 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
841 (isc_un_op_fn
) &isl_union_set_lexmin
} },
842 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
843 (isc_un_op_fn
) &isl_union_set_lexmax
} },
844 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
845 (isc_un_op_fn
) &isl_union_set_lift
} },
846 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
847 (isc_un_op_fn
) &isl_union_map_params
} },
848 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
849 (isc_un_op_fn
) &isl_union_set_params
} },
850 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
851 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
852 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
853 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
854 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
855 isl_obj_union_pw_qpolynomial
,
856 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
857 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
858 isl_obj_union_pw_qpolynomial
,
859 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
860 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
861 isl_obj_union_pw_qpolynomial
,
862 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
864 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
865 (isc_un_op_fn
) &parse
} },
867 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
868 (isc_un_op_fn
) &union_map_power
} },
869 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
870 (isc_un_op_fn
) &union_set_sample
} },
871 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
872 (isc_un_op_fn
) &union_map_sample
} },
873 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
874 (isc_un_op_fn
) &union_set_scan
} },
875 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
876 (isc_un_op_fn
) &union_map_scan
} },
877 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
878 isl_obj_union_pw_qpolynomial
,
879 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
880 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
881 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
882 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
883 (isc_un_op_fn
) &isl_union_set_unwrap
} },
884 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
885 (isc_un_op_fn
) &isl_union_map_wrap
} },
886 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
887 (isc_un_op_fn
) &isl_union_map_zip
} },
891 struct isl_named_obj
{
896 static void free_obj(struct isl_obj obj
)
898 obj
.type
->free(obj
.v
);
901 static int same_name(const void *entry
, const void *val
)
903 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
905 return !strcmp(named
->name
, val
);
908 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
909 char *name
, struct isl_obj obj
)
911 struct isl_hash_table_entry
*entry
;
913 struct isl_named_obj
*named
;
915 name_hash
= isl_hash_string(isl_hash_init(), name
);
916 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
921 free_obj(named
->obj
);
924 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
939 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
940 struct isl_hash_table
*table
, char *name
)
942 struct isl_obj obj
= { isl_obj_none
, NULL
};
943 struct isl_hash_table_entry
*entry
;
946 name_hash
= isl_hash_string(isl_hash_init(), name
);
947 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
949 struct isl_named_obj
*named
;
952 } else if (isdigit(name
[0]))
953 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
955 fprintf(stderr
, "unknown identifier '%s'\n", name
);
958 obj
.v
= obj
.type
->copy(obj
.v
);
962 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
964 if (obj
.type
== super
)
966 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
968 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
970 if (obj
.type
== isl_obj_pw_qpolynomial
&&
971 super
== isl_obj_union_pw_qpolynomial
)
973 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
974 super
== isl_obj_union_pw_qpolynomial_fold
)
976 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
978 if (obj
.type
== isl_obj_list
) {
979 struct isl_list
*list
= obj
.v
;
980 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
981 return is_subtype(list
->obj
[0], super
);
983 if (super
== isl_obj_str
)
988 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
990 struct isl_list
*list
= obj
.v
;
993 obj
.v
= obj
.type
->copy(obj
.v
);
1000 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1003 if (obj
.type
== type
)
1005 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1006 obj
.type
= isl_obj_union_map
;
1007 obj
.v
= isl_union_map_from_map(obj
.v
);
1010 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1011 obj
.type
= isl_obj_union_set
;
1012 obj
.v
= isl_union_set_from_set(obj
.v
);
1015 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1016 type
== isl_obj_union_pw_qpolynomial
) {
1017 obj
.type
= isl_obj_union_pw_qpolynomial
;
1018 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1021 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1022 type
== isl_obj_union_pw_qpolynomial_fold
) {
1023 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1024 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1027 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1028 if (type
== isl_obj_union_map
) {
1029 obj
.type
= isl_obj_union_map
;
1032 if (type
== isl_obj_union_pw_qpolynomial
) {
1033 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1034 isl_union_set_free(obj
.v
);
1035 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1036 obj
.type
= isl_obj_union_pw_qpolynomial
;
1039 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1040 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1041 isl_union_set_free(obj
.v
);
1042 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1044 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1048 if (obj
.type
== isl_obj_list
) {
1049 struct isl_list
*list
= obj
.v
;
1050 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1051 return convert(ctx
, obj_at(obj
, 0), type
);
1053 if (type
== isl_obj_str
) {
1058 p
= isl_printer_to_str(ctx
);
1061 p
= obj
.type
->print(p
, obj
.v
);
1062 s
= isl_printer_get_str(p
);
1063 isl_printer_free(p
);
1065 str
= isl_str_from_string(ctx
, s
);
1070 obj
.type
= isl_obj_str
;
1076 obj
.type
= isl_obj_none
;
1081 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1085 struct isl_token
*tok
;
1087 tok
= isl_stream_next_token(s
);
1091 for (i
= 0; ; ++i
) {
1094 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1096 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1099 isl_token_free(tok
);
1103 for (i
= 0; ; ++i
) {
1104 if (!named_bin_ops
[i
].name
)
1106 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1108 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1111 isl_token_free(tok
);
1112 return &named_bin_ops
[i
].op
;
1115 isl_stream_push_token(s
, tok
);
1120 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1123 struct isl_token
*tok
;
1125 tok
= isl_stream_next_token(s
);
1129 for (i
= 0; ; ++i
) {
1130 if (!named_un_ops
[i
].name
)
1132 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1135 isl_token_free(tok
);
1136 return &named_un_ops
[i
].op
;
1139 isl_stream_push_token(s
, tok
);
1144 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1149 for (i
= 0; ; ++i
) {
1150 if (!named_un_ops
[i
].name
)
1152 if (named_un_ops
[i
].op
.op
!= like
->op
)
1154 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1157 return &named_un_ops
[i
].op
;
1163 static int is_assign(struct isl_stream
*s
)
1165 struct isl_token
*tok
;
1166 struct isl_token
*tok2
;
1169 tok
= isl_stream_next_token(s
);
1172 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1173 isl_stream_push_token(s
, tok
);
1177 tok2
= isl_stream_next_token(s
);
1179 isl_stream_push_token(s
, tok
);
1182 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1183 isl_stream_push_token(s
, tok2
);
1184 isl_stream_push_token(s
, tok
);
1189 static struct isl_obj
read_obj(struct isl_stream
*s
,
1190 struct isl_hash_table
*table
);
1191 static struct isl_obj
read_expr(struct isl_stream
*s
,
1192 struct isl_hash_table
*table
);
1194 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1195 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1197 struct isl_obj obj
= { isl_obj_none
, NULL
};
1199 obj
= read_obj(s
, table
);
1203 op
= find_matching_un_op(op
, obj
);
1206 isl_die(s
->ctx
, isl_error_invalid
,
1207 "no such unary operator defined on given operand",
1210 obj
= convert(s
->ctx
, obj
, op
->arg
);
1211 obj
.v
= op
->fn(obj
.v
);
1217 obj
.type
= isl_obj_none
;
1222 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1224 struct isl_list
*list
;
1227 if (obj
.type
!= isl_obj_union_map
)
1228 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1229 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1230 list
= isl_list_alloc(ctx
, 2);
1234 list
->obj
[0].type
= isl_obj_union_map
;
1235 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1236 list
->obj
[1].type
= isl_obj_bool
;
1237 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1239 obj
.type
= isl_obj_list
;
1240 if (exact
< 0 || !list
->obj
[0].v
)
1246 obj
.type
= isl_obj_none
;
1251 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1253 struct isl_list
*list
= obj
.v
;
1254 struct isl_token
*tok
;
1258 tok
= isl_stream_next_token(s
);
1259 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1260 isl_stream_error(s
, tok
, "expecting index");
1262 isl_stream_push_token(s
, tok
);
1265 v
= isl_token_get_val(s
->ctx
, tok
);
1266 i
= isl_val_get_num_si(v
);
1268 isl_token_free(tok
);
1269 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1270 if (isl_stream_eat(s
, ']'))
1273 return obj_at(obj
, i
);
1276 obj
.type
= isl_obj_none
;
1281 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1282 struct isl_hash_table
*table
)
1286 obj
= read_expr(s
, table
);
1287 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1288 is_subtype(obj
, isl_obj_union_map
), goto error
);
1290 if (obj
.type
== isl_obj_list
) {
1291 struct isl_list
*list
= obj
.v
;
1292 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1293 obj
= obj_at(obj
, 0);
1295 if (obj
.type
== isl_obj_set
)
1296 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1297 else if (obj
.type
== isl_obj_map
)
1298 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1299 if (obj
.type
== isl_obj_union_set
) {
1300 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1302 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1306 if (isl_stream_eat(s
, ')'))
1311 isl_union_map_free(umap
);
1314 obj
.type
= isl_obj_none
;
1319 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1320 __isl_take isl_union_set
*uset
)
1322 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1323 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1325 obj
.type
= isl_obj_list
;
1326 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1331 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1332 __isl_take isl_union_map
*umap
)
1334 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1335 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1337 obj
.type
= isl_obj_list
;
1338 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1343 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1344 struct isl_obj obj
, struct isl_hash_table
*table
)
1348 arg
= read_expr(s
, table
);
1349 if (!is_subtype(arg
, isl_obj_union_map
) &&
1350 !is_subtype(arg
, isl_obj_union_set
))
1351 isl_die(s
->ctx
, isl_error_invalid
,
1352 "expecting set of map argument", goto error
);
1354 if (arg
.type
== isl_obj_list
) {
1355 struct isl_list
*list
= arg
.v
;
1356 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1357 arg
= obj_at(arg
, 0);
1359 if (arg
.type
== isl_obj_set
)
1360 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1361 else if (arg
.type
== isl_obj_map
)
1362 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1363 if (arg
.type
== isl_obj_union_set
)
1364 obj
= apply_fun_set(obj
, arg
.v
);
1366 obj
= apply_fun_map(obj
, arg
.v
);
1370 if (isl_stream_eat(s
, ')'))
1378 obj
.type
= isl_obj_none
;
1383 struct add_vertex_data
{
1384 struct isl_list
*list
;
1388 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1390 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1391 isl_basic_set
*expr
;
1393 expr
= isl_vertex_get_expr(vertex
);
1395 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1396 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1399 isl_vertex_free(vertex
);
1404 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1407 isl_basic_set
*hull
;
1408 isl_vertices
*vertices
= NULL
;
1409 struct isl_list
*list
= NULL
;
1411 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1413 set
= isl_set_remove_divs(set
);
1414 hull
= isl_set_convex_hull(set
);
1415 vertices
= isl_basic_set_compute_vertices(hull
);
1416 isl_basic_set_free(hull
);
1420 ctx
= isl_vertices_get_ctx(vertices
);
1421 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1426 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1428 data
->list
= isl_list_concat(list
, data
->list
);
1430 isl_vertices_free(vertices
);
1435 isl_vertices_free(vertices
);
1439 static struct isl_obj
vertices(struct isl_stream
*s
,
1440 struct isl_hash_table
*table
)
1444 struct isl_list
*list
= NULL
;
1445 isl_union_set
*uset
;
1446 struct add_vertex_data data
= { NULL
};
1448 obj
= read_expr(s
, table
);
1449 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1450 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1454 ctx
= isl_union_set_get_ctx(uset
);
1455 list
= isl_list_alloc(ctx
, 0);
1461 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1464 isl_union_set_free(uset
);
1466 obj
.type
= isl_obj_list
;
1471 isl_union_set_free(uset
);
1472 isl_list_free(data
.list
);
1474 obj
.type
= isl_obj_none
;
1479 static struct isl_obj
type_of(struct isl_stream
*s
,
1480 struct isl_hash_table
*table
)
1484 const char *type
= "unknown";
1486 obj
= read_expr(s
, table
);
1488 if (obj
.type
== isl_obj_map
||
1489 obj
.type
== isl_obj_union_map
)
1491 if (obj
.type
== isl_obj_set
||
1492 obj
.type
== isl_obj_union_set
)
1494 if (obj
.type
== isl_obj_pw_qpolynomial
||
1495 obj
.type
== isl_obj_union_pw_qpolynomial
)
1496 type
= "piecewise quasipolynomial";
1497 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1498 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1499 type
= "piecewise quasipolynomial fold";
1500 if (obj
.type
== isl_obj_list
)
1502 if (obj
.type
== isl_obj_bool
)
1504 if (obj
.type
== isl_obj_str
)
1506 if (obj
.type
== isl_obj_val
)
1510 obj
.type
= isl_obj_str
;
1511 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1516 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1517 struct isl_hash_table
*table
)
1521 obj
= read_obj(s
, table
);
1522 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1523 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1530 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1531 struct isl_hash_table
*table
)
1535 obj
= read_obj(s
, table
);
1536 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1537 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1544 static struct isl_obj
last_any(struct isl_stream
*s
,
1545 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1546 __isl_take isl_union_map
*may_source
)
1548 struct isl_obj obj
= { isl_obj_none
, NULL
};
1549 isl_union_map
*sink
= NULL
;
1550 isl_union_map
*schedule
= NULL
;
1551 isl_union_map
*may_dep
;
1552 isl_union_map
*must_dep
;
1554 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1557 sink
= read_map(s
, table
);
1561 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1564 schedule
= read_map(s
, table
);
1568 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1569 schedule
, &must_dep
, &may_dep
,
1573 obj
.type
= isl_obj_union_map
;
1574 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1578 isl_union_map_free(may_source
);
1579 isl_union_map_free(must_source
);
1580 isl_union_map_free(sink
);
1581 isl_union_map_free(schedule
);
1583 obj
.type
= isl_obj_none
;
1588 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1590 struct isl_obj obj
= { isl_obj_none
, NULL
};
1591 isl_union_map
*must_source
= NULL
;
1592 isl_union_map
*may_source
= NULL
;
1593 isl_union_map
*sink
= NULL
;
1594 isl_union_map
*schedule
= NULL
;
1595 isl_union_map
*may_dep
;
1597 may_source
= read_map(s
, table
);
1601 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1602 must_source
= read_map(s
, table
);
1605 return last_any(s
, table
, must_source
, may_source
);
1608 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1611 sink
= read_map(s
, table
);
1615 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1618 schedule
= read_map(s
, table
);
1622 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1623 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1624 schedule
, NULL
, &may_dep
,
1628 obj
.type
= isl_obj_union_map
;
1633 isl_union_map_free(may_source
);
1634 isl_union_map_free(must_source
);
1635 isl_union_map_free(sink
);
1636 isl_union_map_free(schedule
);
1638 obj
.type
= isl_obj_none
;
1643 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1645 struct isl_obj obj
= { isl_obj_none
, NULL
};
1646 struct isl_list
*list
= NULL
;
1647 isl_union_map
*must_source
= NULL
;
1648 isl_union_map
*may_source
= NULL
;
1649 isl_union_map
*sink
= NULL
;
1650 isl_union_map
*schedule
= NULL
;
1651 isl_union_map
*must_dep
;
1652 isl_union_map
*must_no_source
;
1654 must_source
= read_map(s
, table
);
1658 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1659 may_source
= read_map(s
, table
);
1662 return last_any(s
, table
, must_source
, may_source
);
1665 list
= isl_list_alloc(s
->ctx
, 2);
1669 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1672 sink
= read_map(s
, table
);
1676 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1679 schedule
= read_map(s
, table
);
1683 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1684 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1685 schedule
, &must_dep
, NULL
,
1686 &must_no_source
, NULL
) < 0) {
1687 isl_list_free(list
);
1691 list
->obj
[0].type
= isl_obj_union_map
;
1692 list
->obj
[0].v
= must_dep
;
1693 list
->obj
[1].type
= isl_obj_union_map
;
1694 list
->obj
[1].v
= must_no_source
;
1697 obj
.type
= isl_obj_list
;
1701 isl_list_free(list
);
1702 isl_union_map_free(may_source
);
1703 isl_union_map_free(must_source
);
1704 isl_union_map_free(sink
);
1705 isl_union_map_free(schedule
);
1707 obj
.type
= isl_obj_none
;
1712 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1713 struct isl_hash_table
*table
)
1715 isl_union_set
*domain
;
1716 isl_union_map
*validity
;
1717 isl_union_map
*proximity
;
1719 domain
= read_set(s
, table
);
1723 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1724 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1727 isl_union_map
*umap
;
1728 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1729 umap
= read_map(s
, table
);
1730 validity
= isl_union_map_union(validity
, umap
);
1731 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1732 umap
= read_map(s
, table
);
1733 proximity
= isl_union_map_union(proximity
, umap
);
1738 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1741 static struct isl_obj
schedule(struct isl_stream
*s
,
1742 struct isl_hash_table
*table
)
1744 struct isl_obj obj
= { isl_obj_none
, NULL
};
1745 isl_schedule
*schedule
;
1747 schedule
= get_schedule(s
, table
);
1749 obj
.v
= isl_schedule_get_map(schedule
);
1750 obj
.type
= isl_obj_union_map
;
1752 isl_schedule_free(schedule
);
1757 /* Read a schedule for code generation.
1758 * If the input is a set rather than a map, then we construct
1759 * an identity schedule on the given set.
1761 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1762 struct isl_hash_table
*table
)
1766 obj
= read_obj(s
, table
);
1768 if (is_subtype(obj
, isl_obj_union_map
)) {
1769 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1773 if (is_subtype(obj
, isl_obj_union_set
)) {
1774 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1775 return isl_union_set_identity(obj
.v
);
1779 isl_die(s
->ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1782 /* Generate an AST for the given schedule and options and print
1783 * the AST on the printer.
1785 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1786 __isl_take isl_union_map
*schedule
,
1787 __isl_take isl_union_map
*options
)
1791 isl_ast_build
*build
;
1795 space
= isl_union_map_get_space(schedule
);
1796 context
= isl_set_universe(isl_space_params(space
));
1798 build
= isl_ast_build_from_context(context
);
1799 build
= isl_ast_build_set_options(build
, options
);
1800 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1801 isl_ast_build_free(build
);
1806 format
= isl_printer_get_output_format(p
);
1807 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1808 p
= isl_printer_print_ast_node(p
, tree
);
1809 p
= isl_printer_set_output_format(p
, format
);
1811 isl_ast_node_free(tree
);
1816 /* Perform the codegen operation.
1817 * In particular, read a schedule, check if the user has specified any options
1818 * and then generate an AST from the schedule (and options) and print it.
1820 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1821 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1823 isl_union_map
*schedule
;
1824 isl_union_map
*options
;
1826 schedule
= get_codegen_schedule(s
, table
);
1830 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1831 options
= read_map(s
, table
);
1833 options
= isl_union_map_empty(
1834 isl_union_map_get_space(schedule
));
1836 p
= print_code(p
, schedule
, options
);
1838 isl_stream_eat(s
, ';');
1843 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1845 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1847 struct isl_obj obj
= { isl_obj_none
, NULL
};
1848 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1849 struct isl_list
*list
;
1851 list
= isl_list_alloc(ctx
, 2);
1856 obj
.type
= isl_obj_list
;
1858 list
->obj
[0].type
= isl_obj_union_map
;
1859 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1861 if (isl_band_has_children(band
)) {
1862 isl_band_list
*children
;
1864 children
= isl_band_get_children(band
);
1865 list
->obj
[1] = band_list_to_obj_list(children
);
1867 list
->obj
[1].type
= isl_obj_list
;
1868 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1871 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1874 isl_band_free(band
);
1878 isl_band_free(band
);
1880 obj
.type
= isl_obj_none
;
1885 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1887 struct isl_obj obj
= { isl_obj_none
, NULL
};
1888 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1889 struct isl_list
*list
;
1892 n
= isl_band_list_n_band(bands
);
1893 list
= isl_list_alloc(ctx
, n
);
1898 obj
.type
= isl_obj_list
;
1900 for (i
= 0; i
< n
; ++i
) {
1903 band
= isl_band_list_get_band(bands
, i
);
1904 list
->obj
[i
] = band_to_obj_list(band
);
1905 if (!list
->obj
[i
].v
)
1909 isl_band_list_free(bands
);
1913 isl_band_list_free(bands
);
1915 obj
.type
= isl_obj_none
;
1920 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1921 struct isl_hash_table
*table
)
1923 struct isl_obj obj
= { isl_obj_none
, NULL
};
1924 isl_schedule
*schedule
;
1925 isl_band_list
*roots
;
1927 schedule
= get_schedule(s
, table
);
1931 roots
= isl_schedule_get_band_forest(schedule
);
1932 isl_schedule_free(schedule
);
1934 return band_list_to_obj_list(roots
);
1937 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1939 struct isl_token
*tok
;
1942 if (isl_stream_eat_if_available(s
, '+'))
1943 return transitive_closure(s
->ctx
, obj
);
1945 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1946 if (obj
.type
!= isl_obj_union_map
)
1947 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1949 tok
= isl_stream_next_token(s
);
1950 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1951 isl_stream_error(s
, tok
, "expecting integer exponent");
1953 isl_stream_push_token(s
, tok
);
1957 v
= isl_token_get_val(s
->ctx
, tok
);
1958 if (isl_val_is_zero(v
)) {
1959 isl_stream_error(s
, tok
, "expecting non-zero exponent");
1962 isl_stream_push_token(s
, tok
);
1966 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
1967 isl_token_free(tok
);
1974 obj
.type
= isl_obj_none
;
1979 static struct isl_obj
check_assert(struct isl_stream
*s
,
1980 struct isl_hash_table
*table
)
1984 obj
= read_expr(s
, table
);
1985 if (obj
.type
!= isl_obj_bool
)
1986 isl_die(s
->ctx
, isl_error_invalid
,
1987 "expecting boolean expression", goto error
);
1988 if (obj
.v
!= &isl_bool_true
)
1989 isl_die(s
->ctx
, isl_error_unknown
,
1990 "assertion failed", abort());
1993 obj
.type
= isl_obj_none
;
1998 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2001 struct isl_token
*tok
;
2002 struct isl_stream
*s_file
;
2003 struct iscc_options
*options
;
2007 tok
= isl_stream_next_token(s
);
2008 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2009 isl_stream_error(s
, tok
, "expecting filename");
2010 isl_token_free(tok
);
2014 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2015 if (!options
|| !options
->io
) {
2016 isl_token_free(tok
);
2017 isl_die(s
->ctx
, isl_error_invalid
,
2018 "read operation not allowed", goto error
);
2021 name
= isl_token_get_str(s
->ctx
, tok
);
2022 isl_token_free(tok
);
2023 file
= fopen(name
, "r");
2025 isl_assert(s
->ctx
, file
, goto error
);
2027 s_file
= isl_stream_new_file(s
->ctx
, file
);
2033 obj
= isl_stream_read_obj(s_file
);
2035 isl_stream_free(s_file
);
2040 obj
.type
= isl_obj_none
;
2045 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2046 struct isl_hash_table
*table
)
2049 struct isl_token
*tok
;
2050 struct isl_stream
*s_file
;
2051 struct iscc_options
*options
;
2056 tok
= isl_stream_next_token(s
);
2057 if (!tok
|| isl_token_get_type(tok
) != 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 name
= isl_token_get_str(s
->ctx
, tok
);
2073 isl_token_free(tok
);
2074 file
= fopen(name
, "w");
2077 isl_die(s
->ctx
, isl_error_unknown
,
2078 "could not open file for writing", goto error
);
2080 p
= isl_printer_to_file(s
->ctx
, file
);
2081 p
= isl_printer_set_output_format(p
, options
->format
);
2082 p
= obj
.type
->print(p
, obj
.v
);
2083 p
= isl_printer_end_line(p
);
2084 isl_printer_free(p
);
2089 obj
.type
= isl_obj_none
;
2094 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2096 struct isl_token
*tok
;
2097 struct isl_obj obj
= { isl_obj_none
, NULL
};
2099 tok
= isl_stream_next_token(s
);
2102 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2104 str
= isl_str_alloc(s
->ctx
);
2107 str
->s
= isl_token_get_str(s
->ctx
, tok
);
2108 isl_token_free(tok
);
2110 obj
.type
= isl_obj_str
;
2112 isl_stream_push_token(s
, tok
);
2115 isl_token_free(tok
);
2119 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2121 struct isl_token
*tok
;
2122 struct isl_obj obj
= { isl_obj_none
, NULL
};
2125 tok
= isl_stream_next_token(s
);
2128 type
= isl_token_get_type(tok
);
2129 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2130 int is_true
= type
== ISL_TOKEN_TRUE
;
2131 isl_token_free(tok
);
2132 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2133 obj
.type
= isl_obj_bool
;
2135 isl_stream_push_token(s
, tok
);
2139 static __isl_give
char *read_ident(struct isl_stream
*s
)
2143 struct isl_token
*tok
, *tok2
;
2145 name
= isl_stream_read_ident_if_available(s
);
2149 tok
= isl_stream_next_token(s
);
2152 if (isl_token_get_type(tok
) != '$') {
2153 isl_stream_push_token(s
, tok
);
2156 tok2
= isl_stream_next_token(s
);
2157 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2159 isl_stream_push_token(s
, tok2
);
2160 isl_stream_push_token(s
, tok
);
2164 v
= isl_token_get_val(s
->ctx
, tok2
);
2165 name
= isl_val_to_str(v
);
2167 isl_token_free(tok
);
2168 isl_token_free(tok2
);
2173 static struct isl_obj
read_list(struct isl_stream
*s
,
2174 struct isl_hash_table
*table
, struct isl_obj obj
)
2176 struct isl_list
*list
;
2178 list
= isl_list_alloc(s
->ctx
, 2);
2182 list
->obj
[1] = read_obj(s
, table
);
2184 obj
.type
= isl_obj_list
;
2186 if (!list
->obj
[1].v
)
2189 while (isl_stream_eat_if_available(s
, ',')) {
2190 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2198 obj
.type
= isl_obj_none
;
2203 static struct isl_obj
read_obj(struct isl_stream
*s
,
2204 struct isl_hash_table
*table
)
2206 struct isl_obj obj
= { isl_obj_none
, NULL
};
2208 struct isc_un_op
*op
= NULL
;
2210 obj
= read_string_if_available(s
);
2213 obj
= read_bool_if_available(s
);
2216 if (isl_stream_eat_if_available(s
, '(')) {
2217 if (isl_stream_next_token_is(s
, ')')) {
2218 obj
.type
= isl_obj_list
;
2219 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2221 obj
= read_expr(s
, table
);
2222 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2223 obj
= read_list(s
, table
, obj
);
2225 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2228 op
= read_prefix_un_op_if_available(s
);
2230 return read_un_op_expr(s
, table
, op
);
2232 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2233 return check_assert(s
, table
);
2234 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2235 return read_from_file(s
);
2236 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2237 return write_to_file(s
, table
);
2238 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2239 return vertices(s
, table
);
2240 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2241 return any(s
, table
);
2242 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2243 return last(s
, table
);
2244 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2245 return schedule(s
, table
);
2246 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2247 return schedule_forest(s
, table
);
2248 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2249 return type_of(s
, table
);
2251 name
= read_ident(s
);
2253 obj
= stored_obj(s
->ctx
, table
, name
);
2255 obj
= isl_stream_read_obj(s
);
2260 if (isl_stream_eat_if_available(s
, '^'))
2261 obj
= power(s
, obj
);
2262 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2263 obj
= obj_at_index(s
, obj
);
2264 else if (is_subtype(obj
, isl_obj_union_map
) &&
2265 isl_stream_eat_if_available(s
, '(')) {
2266 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2267 obj
= apply(s
, obj
.v
, table
);
2268 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2269 isl_stream_eat_if_available(s
, '(')) {
2270 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2271 obj
= apply_fun(s
, obj
, table
);
2272 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2273 isl_stream_eat_if_available(s
, '(')) {
2274 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2275 obj
= apply_fun(s
, obj
, table
);
2281 obj
.type
= isl_obj_none
;
2286 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2287 struct isl_obj lhs
, struct isl_obj rhs
)
2291 for (i
= 0; ; ++i
) {
2294 if (bin_ops
[i
].op
!= like
->op
)
2296 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2298 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2304 for (i
= 0; ; ++i
) {
2305 if (!named_bin_ops
[i
].name
)
2307 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2309 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2311 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2314 return &named_bin_ops
[i
].op
;
2320 static int next_is_neg_int(struct isl_stream
*s
)
2322 struct isl_token
*tok
;
2325 tok
= isl_stream_next_token(s
);
2326 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2328 v
= isl_token_get_val(s
->ctx
, tok
);
2329 ret
= isl_val_is_neg(v
);
2333 isl_stream_push_token(s
, tok
);
2338 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2339 struct isl_obj lhs
, struct isl_obj rhs
)
2343 lhs
= convert(ctx
, lhs
, op
->lhs
);
2344 rhs
= convert(ctx
, rhs
, op
->rhs
);
2345 if (op
->res
!= isl_obj_bool
)
2346 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2348 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2351 obj
.v
= isl_bool_from_int(res
);
2358 static struct isl_obj
read_expr(struct isl_stream
*s
,
2359 struct isl_hash_table
*table
)
2361 struct isl_obj obj
= { isl_obj_none
, NULL
};
2362 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2364 obj
= read_obj(s
, table
);
2366 struct isc_bin_op
*op
= NULL
;
2368 op
= read_bin_op_if_available(s
, obj
);
2372 right_obj
= read_obj(s
, table
);
2374 op
= find_matching_bin_op(op
, obj
, right_obj
);
2377 isl_die(s
->ctx
, isl_error_invalid
,
2378 "no such binary operator defined on given operands",
2381 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2384 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2385 right_obj
= read_obj(s
, table
);
2386 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2391 free_obj(right_obj
);
2393 obj
.type
= isl_obj_none
;
2398 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2399 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2401 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2402 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2404 struct isl_obj obj
= { isl_obj_none
, NULL
};
2408 struct isc_bin_op
*op
= NULL
;
2413 if (isl_stream_is_empty(s
))
2416 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2417 return source_file(s
, table
, p
);
2418 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2419 return codegen(s
, table
, p
);
2421 assign
= is_assign(s
);
2423 lhs
= isl_stream_read_ident_if_available(s
);
2424 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2426 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2431 obj
= read_expr(s
, table
);
2432 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2433 fprintf(stderr
, "Interrupted\n");
2434 isl_ctx_reset_error(s
->ctx
);
2436 if (isl_stream_eat(s
, ';'))
2440 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2441 p
= obj
.type
->print(p
, obj
.v
);
2442 p
= isl_printer_end_line(p
);
2447 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2448 static int count
= 0;
2449 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2450 lhs
= strdup(buf
+ 1);
2452 p
= isl_printer_print_str(p
, buf
);
2453 p
= isl_printer_print_str(p
, " := ");
2454 p
= obj
.type
->print(p
, obj
.v
);
2455 p
= isl_printer_end_line(p
);
2457 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2462 isl_stream_flush_tokens(s
);
2463 isl_stream_skip_line(s
);
2469 int free_cb(void **entry
, void *user
)
2471 struct isl_named_obj
*named
= *entry
;
2473 free_obj(named
->obj
);
2480 static void register_named_ops(struct isl_stream
*s
)
2484 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2485 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2486 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2489 for (i
= 0; ; ++i
) {
2490 if (!named_un_ops
[i
].name
)
2492 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2493 named_un_ops
[i
].name
);
2494 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2497 for (i
= 0; ; ++i
) {
2498 if (!named_bin_ops
[i
].name
)
2500 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2501 named_bin_ops
[i
].name
);
2502 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2506 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2507 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2509 struct isl_token
*tok
;
2510 struct isl_stream
*s_file
;
2514 tok
= isl_stream_next_token(s
);
2515 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2516 isl_stream_error(s
, tok
, "expecting filename");
2517 isl_token_free(tok
);
2521 name
= isl_token_get_str(s
->ctx
, tok
);
2522 isl_token_free(tok
);
2523 file
= fopen(name
, "r");
2525 isl_assert(s
->ctx
, file
, return p
);
2527 s_file
= isl_stream_new_file(s
->ctx
, file
);
2533 register_named_ops(s_file
);
2535 while (!s_file
->eof
)
2536 p
= read_line(s_file
, table
, p
, 0);
2538 isl_stream_free(s_file
);
2541 isl_stream_eat(s
, ';');
2546 int main(int argc
, char **argv
)
2548 struct isl_ctx
*ctx
;
2549 struct isl_stream
*s
;
2550 struct isl_hash_table
*table
;
2551 struct iscc_options
*options
;
2553 int tty
= isatty(0);
2555 options
= iscc_options_new_with_defaults();
2558 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2559 pet_options_set_autodetect(ctx
, 1);
2560 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2561 s
= isl_stream_new_file(ctx
, stdin
);
2563 table
= isl_hash_table_alloc(ctx
, 10);
2565 p
= isl_printer_to_file(ctx
, stdout
);
2566 p
= isl_printer_set_output_format(p
, options
->format
);
2569 register_named_ops(s
);
2571 install_signal_handler(ctx
);
2573 while (p
&& !s
->eof
) {
2574 isl_ctx_resume(ctx
);
2575 p
= read_line(s
, table
, p
, tty
);
2578 remove_signal_handler(ctx
);
2580 isl_printer_free(p
);
2581 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2582 isl_hash_table_free(ctx
, table
);