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_pw_multi_aff
, isl_obj_pw_multi_aff
,
391 isl_obj_pw_multi_aff
,
392 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
393 { '+', isl_obj_union_set
, isl_obj_union_set
,
395 (isc_bin_op_fn
) &isl_union_set_union
},
396 { '+', isl_obj_union_map
, isl_obj_union_map
,
398 (isc_bin_op_fn
) &isl_union_map_union
},
399 { '-', isl_obj_union_set
, isl_obj_union_set
,
401 (isc_bin_op_fn
) &isl_union_set_subtract
},
402 { '-', isl_obj_union_map
, isl_obj_union_map
,
404 (isc_bin_op_fn
) &isl_union_map_subtract
},
405 { '*', isl_obj_union_set
, isl_obj_union_set
,
407 (isc_bin_op_fn
) &isl_union_set_intersect
},
408 { '*', isl_obj_union_map
, isl_obj_union_map
,
410 (isc_bin_op_fn
) &isl_union_map_intersect
},
411 { '*', isl_obj_union_map
, isl_obj_union_set
,
413 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
414 { '.', isl_obj_union_map
, isl_obj_union_map
,
416 (isc_bin_op_fn
) &isl_union_map_apply_range
},
417 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
418 isl_obj_union_pw_qpolynomial
,
419 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
420 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
422 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
423 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
425 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
426 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
427 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
428 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
429 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
430 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
432 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
433 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
435 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
436 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
438 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
439 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
441 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
442 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
444 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
445 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
447 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
448 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
451 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
452 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
455 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
456 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
458 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
459 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
461 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
462 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
464 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
465 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
467 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
468 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
470 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
471 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
473 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
474 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
476 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
477 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
479 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
480 { '.', isl_obj_union_pw_qpolynomial_fold
,
481 isl_obj_union_pw_qpolynomial_fold
,
482 isl_obj_union_pw_qpolynomial_fold
,
483 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
484 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
485 isl_obj_union_pw_qpolynomial
,
486 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
487 { '+', isl_obj_union_pw_qpolynomial
,
488 isl_obj_union_pw_qpolynomial_fold
,
489 isl_obj_union_pw_qpolynomial_fold
,
490 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
491 { '+', isl_obj_union_pw_qpolynomial_fold
,
492 isl_obj_union_pw_qpolynomial
,
493 isl_obj_union_pw_qpolynomial_fold
,
494 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
495 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
496 isl_obj_union_pw_qpolynomial
,
497 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
498 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
499 isl_obj_union_pw_qpolynomial
,
500 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
501 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
502 isl_obj_union_pw_qpolynomial
,
503 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
504 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
505 isl_obj_union_pw_qpolynomial_fold
,
506 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
507 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
508 isl_obj_union_pw_qpolynomial_fold
,
509 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
510 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
511 isl_obj_union_pw_qpolynomial
,
512 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
513 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
514 isl_obj_union_pw_qpolynomial
,
515 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
516 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
517 isl_obj_union_pw_qpolynomial_fold
,
518 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
519 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
520 isl_obj_union_pw_qpolynomial
,
521 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
522 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
523 isl_obj_union_pw_qpolynomial
,
524 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
525 { '%', isl_obj_union_set
, isl_obj_union_set
,
527 (isc_bin_op_fn
) &isl_union_set_gist
},
528 { '%', isl_obj_union_map
, isl_obj_union_map
,
530 (isc_bin_op_fn
) &isl_union_map_gist
},
531 { '%', isl_obj_union_map
, isl_obj_union_set
,
533 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
534 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
535 isl_obj_union_pw_qpolynomial
,
536 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
537 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
538 isl_obj_union_pw_qpolynomial_fold
,
539 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
540 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
541 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
542 { .test
= (isc_bin_test_fn
)
543 &isl_union_pw_qpolynomial_plain_is_equal
} },
544 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
545 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
546 { .test
= (isc_bin_test_fn
)
547 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
548 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
549 (isc_bin_op_fn
) &isl_str_concat
},
553 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
554 __isl_take isl_union_map
*umap2
)
556 return isl_union_map_apply_range(umap2
, umap1
);
559 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
560 __isl_take isl_union_pw_qpolynomial
*upwqp
,
561 __isl_take isl_union_map
*umap
)
563 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
566 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
567 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
568 __isl_take isl_union_map
*umap
)
570 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
573 struct isc_named_bin_op named_bin_ops
[] = {
574 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
576 (isc_bin_op_fn
) &map_after_map
} },
577 { "after", { -1, isl_obj_union_pw_qpolynomial
,
578 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
579 (isc_bin_op_fn
) &qpolynomial_after_map
} },
580 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
581 isl_obj_union_map
, isl_obj_list
,
582 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
583 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
585 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
586 { "before", { -1, isl_obj_union_map
,
587 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
588 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
589 { "before", { -1, isl_obj_union_map
,
590 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
591 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
592 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
594 (isc_bin_op_fn
) &isl_union_set_product
} },
595 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
597 (isc_bin_op_fn
) &isl_union_map_product
} },
601 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
603 return isl_set_from_basic_set(isl_union_set_sample(uset
));
606 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
608 return isl_map_from_basic_map(isl_union_map_sample(umap
));
611 static __isl_give
struct isl_list
*union_map_power(
612 __isl_take isl_union_map
*umap
)
615 struct isl_list
*list
;
618 ctx
= isl_union_map_get_ctx(umap
);
619 list
= isl_list_alloc(ctx
, 2);
623 list
->obj
[0].type
= isl_obj_union_map
;
624 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
625 list
->obj
[1].type
= isl_obj_bool
;
626 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
627 if (exact
< 0 || !list
->obj
[0].v
)
632 isl_union_map_free(umap
);
638 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
639 __isl_take isl_union_pw_qpolynomial
*upwqp
)
642 struct isl_list
*list
;
645 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
646 list
= isl_list_alloc(ctx
, 2);
650 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
651 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
652 isl_fold_max
, &tight
);
653 list
->obj
[1].type
= isl_obj_bool
;
654 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
655 if (tight
< 0 || !list
->obj
[0].v
)
660 isl_union_pw_qpolynomial_free(upwqp
);
667 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
670 struct isl_list
*list
;
671 struct pet_scop
*scop
;
672 isl_union_map
*sched
, *may_reads
, *must_writes
, *may_writes
;
673 isl_union_set
*domain
;
674 struct iscc_options
*options
;
680 options
= isl_ctx_peek_iscc_options(ctx
);
681 if (!options
|| !options
->io
) {
683 isl_die(ctx
, isl_error_invalid
,
684 "parse_file operation not allowed", return NULL
);
687 list
= isl_list_alloc(ctx
, 5);
691 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
692 domain
= pet_scop_collect_domains(scop
);
693 sched
= pet_scop_collect_schedule(scop
);
694 may_reads
= pet_scop_collect_may_reads(scop
);
695 may_writes
= pet_scop_collect_may_writes(scop
);
696 must_writes
= pet_scop_collect_must_writes(scop
);
699 list
->obj
[0].type
= isl_obj_union_set
;
700 list
->obj
[0].v
= domain
;
701 list
->obj
[1].type
= isl_obj_union_map
;
702 list
->obj
[1].v
= must_writes
;
703 list
->obj
[2].type
= isl_obj_union_map
;
704 list
->obj
[2].v
= may_writes
;
705 list
->obj
[3].type
= isl_obj_union_map
;
706 list
->obj
[3].v
= may_reads
;
707 list
->obj
[4].type
= isl_obj_union_map
;
708 list
->obj
[4].v
= sched
;
710 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
711 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
723 static int add_point(__isl_take isl_point
*pnt
, void *user
)
725 isl_union_set
**scan
= (isl_union_set
**) user
;
727 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
732 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
736 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
738 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
739 isl_union_set_free(scan
);
743 isl_union_set_free(uset
);
747 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
749 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
752 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
753 __isl_take isl_union_pw_qpolynomial
*upwqp
)
755 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
758 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
759 __isl_take isl_union_pw_qpolynomial
*upwqp
)
761 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
764 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
765 __isl_take isl_union_pw_qpolynomial
*upwqp
)
767 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
770 typedef void *(*isc_un_op_fn
)(void *arg
);
772 enum isl_token_type op
;
777 struct isc_named_un_op
{
781 struct isc_named_un_op named_un_ops
[] = {
782 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
783 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
784 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
785 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
786 {"card", { -1, isl_obj_union_set
,
787 isl_obj_union_pw_qpolynomial
,
788 (isc_un_op_fn
) &isl_union_set_card
} },
789 {"card", { -1, isl_obj_union_map
,
790 isl_obj_union_pw_qpolynomial
,
791 (isc_un_op_fn
) &isl_union_map_card
} },
792 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
793 (isc_un_op_fn
) &isl_union_set_coalesce
} },
794 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
795 (isc_un_op_fn
) &isl_union_map_coalesce
} },
796 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
797 isl_obj_union_pw_qpolynomial
,
798 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
799 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
800 isl_obj_union_pw_qpolynomial_fold
,
801 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
802 {"coefficients", { -1, isl_obj_union_set
,
804 (isc_un_op_fn
) &isl_union_set_coefficients
} },
805 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
806 (isc_un_op_fn
) &isl_union_set_solutions
} },
807 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
808 (isc_un_op_fn
) &isl_union_map_deltas
} },
809 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
810 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
811 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
812 (isc_un_op_fn
) &isl_union_map_domain
} },
813 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
814 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
815 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
817 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
818 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
819 (isc_un_op_fn
) &isl_union_map_domain
} },
820 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
822 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
823 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
825 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
826 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
827 (isc_un_op_fn
) &isl_union_map_domain_map
} },
828 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
829 (isc_un_op_fn
) &isl_union_map_range
} },
830 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
831 (isc_un_op_fn
) &isl_union_map_range
} },
832 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
833 (isc_un_op_fn
) &isl_union_map_range_map
} },
834 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
835 (isc_un_op_fn
) &isl_union_set_identity
} },
836 {"lattice_width", { -1, isl_obj_union_set
,
837 isl_obj_union_pw_qpolynomial
,
838 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
839 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
840 (isc_un_op_fn
) &isl_union_map_lexmin
} },
841 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
842 (isc_un_op_fn
) &isl_union_map_lexmax
} },
843 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
844 (isc_un_op_fn
) &isl_union_set_lexmin
} },
845 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
846 (isc_un_op_fn
) &isl_union_set_lexmax
} },
847 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
848 (isc_un_op_fn
) &isl_union_set_lift
} },
849 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
850 (isc_un_op_fn
) &isl_union_map_params
} },
851 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
852 (isc_un_op_fn
) &isl_union_set_params
} },
853 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
854 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
855 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
856 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
857 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
858 isl_obj_union_pw_qpolynomial
,
859 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
860 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
861 isl_obj_union_pw_qpolynomial
,
862 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
863 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
864 isl_obj_union_pw_qpolynomial
,
865 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
867 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
868 (isc_un_op_fn
) &parse
} },
870 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
871 (isc_un_op_fn
) &union_map_power
} },
872 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
873 (isc_un_op_fn
) &union_set_sample
} },
874 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
875 (isc_un_op_fn
) &union_map_sample
} },
876 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
877 (isc_un_op_fn
) &union_set_scan
} },
878 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
879 (isc_un_op_fn
) &union_map_scan
} },
880 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
881 isl_obj_union_pw_qpolynomial
,
882 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
883 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
884 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
885 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
886 (isc_un_op_fn
) &isl_union_set_unwrap
} },
887 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
888 (isc_un_op_fn
) &isl_union_map_wrap
} },
889 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
890 (isc_un_op_fn
) &isl_union_map_zip
} },
894 struct isl_named_obj
{
899 static void free_obj(struct isl_obj obj
)
901 obj
.type
->free(obj
.v
);
904 static int same_name(const void *entry
, const void *val
)
906 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
908 return !strcmp(named
->name
, val
);
911 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
912 char *name
, struct isl_obj obj
)
914 struct isl_hash_table_entry
*entry
;
916 struct isl_named_obj
*named
;
918 name_hash
= isl_hash_string(isl_hash_init(), name
);
919 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
924 free_obj(named
->obj
);
927 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
942 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
943 struct isl_hash_table
*table
, char *name
)
945 struct isl_obj obj
= { isl_obj_none
, NULL
};
946 struct isl_hash_table_entry
*entry
;
949 name_hash
= isl_hash_string(isl_hash_init(), name
);
950 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
952 struct isl_named_obj
*named
;
955 } else if (isdigit(name
[0]))
956 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
958 fprintf(stderr
, "unknown identifier '%s'\n", name
);
961 obj
.v
= obj
.type
->copy(obj
.v
);
965 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
967 if (obj
.type
== super
)
969 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
971 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
973 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
974 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
975 int is_set
= isl_space_is_set(space
);
976 isl_space_free(space
);
979 if (obj
.type
== isl_obj_pw_qpolynomial
&&
980 super
== isl_obj_union_pw_qpolynomial
)
982 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
983 super
== isl_obj_union_pw_qpolynomial_fold
)
985 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
987 if (obj
.type
== isl_obj_list
) {
988 struct isl_list
*list
= obj
.v
;
989 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
990 return is_subtype(list
->obj
[0], super
);
992 if (super
== isl_obj_str
)
997 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
999 struct isl_list
*list
= obj
.v
;
1002 obj
.v
= obj
.type
->copy(obj
.v
);
1004 isl_list_free(list
);
1009 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1012 if (obj
.type
== type
)
1014 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1015 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1016 obj
.type
= isl_obj_union_set
;
1017 obj
.v
= isl_union_set_from_set(set
);
1020 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1021 obj
.type
= isl_obj_union_map
;
1022 obj
.v
= isl_union_map_from_map(obj
.v
);
1025 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1026 obj
.type
= isl_obj_union_set
;
1027 obj
.v
= isl_union_set_from_set(obj
.v
);
1030 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1031 type
== isl_obj_union_pw_qpolynomial
) {
1032 obj
.type
= isl_obj_union_pw_qpolynomial
;
1033 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1036 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1037 type
== isl_obj_union_pw_qpolynomial_fold
) {
1038 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1039 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1042 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1043 if (type
== isl_obj_union_map
) {
1044 obj
.type
= isl_obj_union_map
;
1047 if (type
== isl_obj_union_pw_qpolynomial
) {
1048 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1049 isl_union_set_free(obj
.v
);
1050 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1051 obj
.type
= isl_obj_union_pw_qpolynomial
;
1054 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1055 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1056 isl_union_set_free(obj
.v
);
1057 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1059 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1063 if (obj
.type
== isl_obj_list
) {
1064 struct isl_list
*list
= obj
.v
;
1065 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1066 return convert(ctx
, obj_at(obj
, 0), type
);
1068 if (type
== isl_obj_str
) {
1073 p
= isl_printer_to_str(ctx
);
1076 p
= obj
.type
->print(p
, obj
.v
);
1077 s
= isl_printer_get_str(p
);
1078 isl_printer_free(p
);
1080 str
= isl_str_from_string(ctx
, s
);
1085 obj
.type
= isl_obj_str
;
1091 obj
.type
= isl_obj_none
;
1096 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1100 struct isl_token
*tok
;
1102 tok
= isl_stream_next_token(s
);
1106 for (i
= 0; ; ++i
) {
1109 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1111 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1114 isl_token_free(tok
);
1118 for (i
= 0; ; ++i
) {
1119 if (!named_bin_ops
[i
].name
)
1121 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1123 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1126 isl_token_free(tok
);
1127 return &named_bin_ops
[i
].op
;
1130 isl_stream_push_token(s
, tok
);
1135 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1138 struct isl_token
*tok
;
1140 tok
= isl_stream_next_token(s
);
1144 for (i
= 0; ; ++i
) {
1145 if (!named_un_ops
[i
].name
)
1147 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1150 isl_token_free(tok
);
1151 return &named_un_ops
[i
].op
;
1154 isl_stream_push_token(s
, tok
);
1159 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1164 for (i
= 0; ; ++i
) {
1165 if (!named_un_ops
[i
].name
)
1167 if (named_un_ops
[i
].op
.op
!= like
->op
)
1169 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1172 return &named_un_ops
[i
].op
;
1178 static int is_assign(struct isl_stream
*s
)
1180 struct isl_token
*tok
;
1181 struct isl_token
*tok2
;
1184 tok
= isl_stream_next_token(s
);
1187 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1188 isl_stream_push_token(s
, tok
);
1192 tok2
= isl_stream_next_token(s
);
1194 isl_stream_push_token(s
, tok
);
1197 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1198 isl_stream_push_token(s
, tok2
);
1199 isl_stream_push_token(s
, tok
);
1204 static struct isl_obj
read_obj(struct isl_stream
*s
,
1205 struct isl_hash_table
*table
);
1206 static struct isl_obj
read_expr(struct isl_stream
*s
,
1207 struct isl_hash_table
*table
);
1209 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1210 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1212 struct isl_obj obj
= { isl_obj_none
, NULL
};
1214 obj
= read_obj(s
, table
);
1218 op
= find_matching_un_op(op
, obj
);
1221 isl_die(s
->ctx
, isl_error_invalid
,
1222 "no such unary operator defined on given operand",
1225 obj
= convert(s
->ctx
, obj
, op
->arg
);
1226 obj
.v
= op
->fn(obj
.v
);
1232 obj
.type
= isl_obj_none
;
1237 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1239 struct isl_list
*list
;
1242 if (obj
.type
!= isl_obj_union_map
)
1243 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1244 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1245 list
= isl_list_alloc(ctx
, 2);
1249 list
->obj
[0].type
= isl_obj_union_map
;
1250 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1251 list
->obj
[1].type
= isl_obj_bool
;
1252 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1254 obj
.type
= isl_obj_list
;
1255 if (exact
< 0 || !list
->obj
[0].v
)
1261 obj
.type
= isl_obj_none
;
1266 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1268 struct isl_list
*list
= obj
.v
;
1269 struct isl_token
*tok
;
1273 tok
= isl_stream_next_token(s
);
1274 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1275 isl_stream_error(s
, tok
, "expecting index");
1277 isl_stream_push_token(s
, tok
);
1280 v
= isl_token_get_val(s
->ctx
, tok
);
1281 i
= isl_val_get_num_si(v
);
1283 isl_token_free(tok
);
1284 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1285 if (isl_stream_eat(s
, ']'))
1288 return obj_at(obj
, i
);
1291 obj
.type
= isl_obj_none
;
1296 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1297 struct isl_hash_table
*table
)
1301 obj
= read_expr(s
, table
);
1302 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1303 is_subtype(obj
, isl_obj_union_map
), goto error
);
1305 if (obj
.type
== isl_obj_list
) {
1306 struct isl_list
*list
= obj
.v
;
1307 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1308 obj
= obj_at(obj
, 0);
1310 if (obj
.type
== isl_obj_set
)
1311 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1312 else if (obj
.type
== isl_obj_map
)
1313 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1314 if (obj
.type
== isl_obj_union_set
) {
1315 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1317 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1321 if (isl_stream_eat(s
, ')'))
1326 isl_union_map_free(umap
);
1329 obj
.type
= isl_obj_none
;
1334 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1335 __isl_take isl_union_set
*uset
)
1337 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1338 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1340 obj
.type
= isl_obj_list
;
1341 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1346 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1347 __isl_take isl_union_map
*umap
)
1349 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1350 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1352 obj
.type
= isl_obj_list
;
1353 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1358 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1359 struct isl_obj obj
, struct isl_hash_table
*table
)
1363 arg
= read_expr(s
, table
);
1364 if (!is_subtype(arg
, isl_obj_union_map
) &&
1365 !is_subtype(arg
, isl_obj_union_set
))
1366 isl_die(s
->ctx
, isl_error_invalid
,
1367 "expecting set of map argument", goto error
);
1369 if (arg
.type
== isl_obj_list
) {
1370 struct isl_list
*list
= arg
.v
;
1371 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1372 arg
= obj_at(arg
, 0);
1374 if (arg
.type
== isl_obj_set
)
1375 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1376 else if (arg
.type
== isl_obj_map
)
1377 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1378 if (arg
.type
== isl_obj_union_set
)
1379 obj
= apply_fun_set(obj
, arg
.v
);
1381 obj
= apply_fun_map(obj
, arg
.v
);
1385 if (isl_stream_eat(s
, ')'))
1393 obj
.type
= isl_obj_none
;
1398 struct add_vertex_data
{
1399 struct isl_list
*list
;
1403 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1405 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1409 ma
= isl_vertex_get_expr(vertex
);
1410 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1412 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1413 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1416 isl_vertex_free(vertex
);
1421 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1424 isl_basic_set
*hull
;
1425 isl_vertices
*vertices
= NULL
;
1426 struct isl_list
*list
= NULL
;
1428 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1430 set
= isl_set_remove_divs(set
);
1431 hull
= isl_set_convex_hull(set
);
1432 vertices
= isl_basic_set_compute_vertices(hull
);
1433 isl_basic_set_free(hull
);
1437 ctx
= isl_vertices_get_ctx(vertices
);
1438 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1443 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1445 data
->list
= isl_list_concat(list
, data
->list
);
1447 isl_vertices_free(vertices
);
1452 isl_vertices_free(vertices
);
1456 static struct isl_obj
vertices(struct isl_stream
*s
,
1457 struct isl_hash_table
*table
)
1461 struct isl_list
*list
= NULL
;
1462 isl_union_set
*uset
;
1463 struct add_vertex_data data
= { NULL
};
1465 obj
= read_expr(s
, table
);
1466 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1467 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1471 ctx
= isl_union_set_get_ctx(uset
);
1472 list
= isl_list_alloc(ctx
, 0);
1478 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1481 isl_union_set_free(uset
);
1483 obj
.type
= isl_obj_list
;
1488 isl_union_set_free(uset
);
1489 isl_list_free(data
.list
);
1491 obj
.type
= isl_obj_none
;
1496 static struct isl_obj
type_of(struct isl_stream
*s
,
1497 struct isl_hash_table
*table
)
1501 const char *type
= "unknown";
1503 obj
= read_expr(s
, table
);
1505 if (obj
.type
== isl_obj_map
||
1506 obj
.type
== isl_obj_union_map
)
1508 if (obj
.type
== isl_obj_set
||
1509 obj
.type
== isl_obj_union_set
)
1511 if (obj
.type
== isl_obj_pw_multi_aff
)
1512 type
= "piecewise multi-quasiaffine expression";
1513 if (obj
.type
== isl_obj_pw_qpolynomial
||
1514 obj
.type
== isl_obj_union_pw_qpolynomial
)
1515 type
= "piecewise quasipolynomial";
1516 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1517 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1518 type
= "piecewise quasipolynomial fold";
1519 if (obj
.type
== isl_obj_list
)
1521 if (obj
.type
== isl_obj_bool
)
1523 if (obj
.type
== isl_obj_str
)
1525 if (obj
.type
== isl_obj_val
)
1529 obj
.type
= isl_obj_str
;
1530 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1535 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1536 struct isl_hash_table
*table
)
1540 obj
= read_obj(s
, table
);
1541 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1542 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1549 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1550 struct isl_hash_table
*table
)
1554 obj
= read_obj(s
, table
);
1555 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1556 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1563 static struct isl_obj
last_any(struct isl_stream
*s
,
1564 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1565 __isl_take isl_union_map
*may_source
)
1567 struct isl_obj obj
= { isl_obj_none
, NULL
};
1568 isl_union_map
*sink
= NULL
;
1569 isl_union_map
*schedule
= NULL
;
1570 isl_union_map
*may_dep
;
1571 isl_union_map
*must_dep
;
1573 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1576 sink
= read_map(s
, table
);
1580 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1583 schedule
= read_map(s
, table
);
1587 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1588 schedule
, &must_dep
, &may_dep
,
1592 obj
.type
= isl_obj_union_map
;
1593 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1597 isl_union_map_free(may_source
);
1598 isl_union_map_free(must_source
);
1599 isl_union_map_free(sink
);
1600 isl_union_map_free(schedule
);
1602 obj
.type
= isl_obj_none
;
1607 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1609 struct isl_obj obj
= { isl_obj_none
, NULL
};
1610 isl_union_map
*must_source
= NULL
;
1611 isl_union_map
*may_source
= NULL
;
1612 isl_union_map
*sink
= NULL
;
1613 isl_union_map
*schedule
= NULL
;
1614 isl_union_map
*may_dep
;
1616 may_source
= read_map(s
, table
);
1620 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1621 must_source
= read_map(s
, table
);
1624 return last_any(s
, table
, must_source
, may_source
);
1627 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1630 sink
= read_map(s
, table
);
1634 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1637 schedule
= read_map(s
, table
);
1641 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1642 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1643 schedule
, NULL
, &may_dep
,
1647 obj
.type
= isl_obj_union_map
;
1652 isl_union_map_free(may_source
);
1653 isl_union_map_free(must_source
);
1654 isl_union_map_free(sink
);
1655 isl_union_map_free(schedule
);
1657 obj
.type
= isl_obj_none
;
1662 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1664 struct isl_obj obj
= { isl_obj_none
, NULL
};
1665 struct isl_list
*list
= NULL
;
1666 isl_union_map
*must_source
= NULL
;
1667 isl_union_map
*may_source
= NULL
;
1668 isl_union_map
*sink
= NULL
;
1669 isl_union_map
*schedule
= NULL
;
1670 isl_union_map
*must_dep
;
1671 isl_union_map
*must_no_source
;
1673 must_source
= read_map(s
, table
);
1677 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1678 may_source
= read_map(s
, table
);
1681 return last_any(s
, table
, must_source
, may_source
);
1684 list
= isl_list_alloc(s
->ctx
, 2);
1688 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1691 sink
= read_map(s
, table
);
1695 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1698 schedule
= read_map(s
, table
);
1702 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1703 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1704 schedule
, &must_dep
, NULL
,
1705 &must_no_source
, NULL
) < 0) {
1706 isl_list_free(list
);
1710 list
->obj
[0].type
= isl_obj_union_map
;
1711 list
->obj
[0].v
= must_dep
;
1712 list
->obj
[1].type
= isl_obj_union_map
;
1713 list
->obj
[1].v
= must_no_source
;
1716 obj
.type
= isl_obj_list
;
1720 isl_list_free(list
);
1721 isl_union_map_free(may_source
);
1722 isl_union_map_free(must_source
);
1723 isl_union_map_free(sink
);
1724 isl_union_map_free(schedule
);
1726 obj
.type
= isl_obj_none
;
1731 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1732 struct isl_hash_table
*table
)
1734 isl_union_set
*domain
;
1735 isl_union_map
*validity
;
1736 isl_union_map
*proximity
;
1738 domain
= read_set(s
, table
);
1742 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1743 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1746 isl_union_map
*umap
;
1747 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1748 umap
= read_map(s
, table
);
1749 validity
= isl_union_map_union(validity
, umap
);
1750 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1751 umap
= read_map(s
, table
);
1752 proximity
= isl_union_map_union(proximity
, umap
);
1757 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1760 static struct isl_obj
schedule(struct isl_stream
*s
,
1761 struct isl_hash_table
*table
)
1763 struct isl_obj obj
= { isl_obj_none
, NULL
};
1764 isl_schedule
*schedule
;
1766 schedule
= get_schedule(s
, table
);
1768 obj
.v
= isl_schedule_get_map(schedule
);
1769 obj
.type
= isl_obj_union_map
;
1771 isl_schedule_free(schedule
);
1776 /* Read a schedule for code generation.
1777 * If the input is a set rather than a map, then we construct
1778 * an identity schedule on the given set.
1780 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1781 struct isl_hash_table
*table
)
1785 obj
= read_obj(s
, table
);
1787 if (is_subtype(obj
, isl_obj_union_map
)) {
1788 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1792 if (is_subtype(obj
, isl_obj_union_set
)) {
1793 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1794 return isl_union_set_identity(obj
.v
);
1798 isl_die(s
->ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1801 /* Generate an AST for the given schedule and options and print
1802 * the AST on the printer.
1804 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1805 __isl_take isl_union_map
*schedule
,
1806 __isl_take isl_union_map
*options
)
1810 isl_ast_build
*build
;
1814 space
= isl_union_map_get_space(schedule
);
1815 context
= isl_set_universe(isl_space_params(space
));
1817 build
= isl_ast_build_from_context(context
);
1818 build
= isl_ast_build_set_options(build
, options
);
1819 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1820 isl_ast_build_free(build
);
1825 format
= isl_printer_get_output_format(p
);
1826 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1827 p
= isl_printer_print_ast_node(p
, tree
);
1828 p
= isl_printer_set_output_format(p
, format
);
1830 isl_ast_node_free(tree
);
1835 /* Perform the codegen operation.
1836 * In particular, read a schedule, check if the user has specified any options
1837 * and then generate an AST from the schedule (and options) and print it.
1839 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1840 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1842 isl_union_map
*schedule
;
1843 isl_union_map
*options
;
1845 schedule
= get_codegen_schedule(s
, table
);
1849 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1850 options
= read_map(s
, table
);
1852 options
= isl_union_map_empty(
1853 isl_union_map_get_space(schedule
));
1855 p
= print_code(p
, schedule
, options
);
1857 isl_stream_eat(s
, ';');
1862 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1864 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1866 struct isl_obj obj
= { isl_obj_none
, NULL
};
1867 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1868 struct isl_list
*list
;
1870 list
= isl_list_alloc(ctx
, 2);
1875 obj
.type
= isl_obj_list
;
1877 list
->obj
[0].type
= isl_obj_union_map
;
1878 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1880 if (isl_band_has_children(band
)) {
1881 isl_band_list
*children
;
1883 children
= isl_band_get_children(band
);
1884 list
->obj
[1] = band_list_to_obj_list(children
);
1886 list
->obj
[1].type
= isl_obj_list
;
1887 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1890 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1893 isl_band_free(band
);
1897 isl_band_free(band
);
1899 obj
.type
= isl_obj_none
;
1904 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1906 struct isl_obj obj
= { isl_obj_none
, NULL
};
1907 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1908 struct isl_list
*list
;
1911 n
= isl_band_list_n_band(bands
);
1912 list
= isl_list_alloc(ctx
, n
);
1917 obj
.type
= isl_obj_list
;
1919 for (i
= 0; i
< n
; ++i
) {
1922 band
= isl_band_list_get_band(bands
, i
);
1923 list
->obj
[i
] = band_to_obj_list(band
);
1924 if (!list
->obj
[i
].v
)
1928 isl_band_list_free(bands
);
1932 isl_band_list_free(bands
);
1934 obj
.type
= isl_obj_none
;
1939 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1940 struct isl_hash_table
*table
)
1942 struct isl_obj obj
= { isl_obj_none
, NULL
};
1943 isl_schedule
*schedule
;
1944 isl_band_list
*roots
;
1946 schedule
= get_schedule(s
, table
);
1950 roots
= isl_schedule_get_band_forest(schedule
);
1951 isl_schedule_free(schedule
);
1953 return band_list_to_obj_list(roots
);
1956 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1958 struct isl_token
*tok
;
1961 if (isl_stream_eat_if_available(s
, '+'))
1962 return transitive_closure(s
->ctx
, obj
);
1964 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1965 if (obj
.type
!= isl_obj_union_map
)
1966 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1968 tok
= isl_stream_next_token(s
);
1969 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1970 isl_stream_error(s
, tok
, "expecting integer exponent");
1972 isl_stream_push_token(s
, tok
);
1976 v
= isl_token_get_val(s
->ctx
, tok
);
1977 if (isl_val_is_zero(v
)) {
1978 isl_stream_error(s
, tok
, "expecting non-zero exponent");
1981 isl_stream_push_token(s
, tok
);
1985 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
1986 isl_token_free(tok
);
1993 obj
.type
= isl_obj_none
;
1998 static struct isl_obj
check_assert(struct isl_stream
*s
,
1999 struct isl_hash_table
*table
)
2003 obj
= read_expr(s
, table
);
2004 if (obj
.type
!= isl_obj_bool
)
2005 isl_die(s
->ctx
, isl_error_invalid
,
2006 "expecting boolean expression", goto error
);
2007 if (obj
.v
!= &isl_bool_true
)
2008 isl_die(s
->ctx
, isl_error_unknown
,
2009 "assertion failed", abort());
2012 obj
.type
= isl_obj_none
;
2017 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2020 struct isl_token
*tok
;
2021 struct isl_stream
*s_file
;
2022 struct iscc_options
*options
;
2026 tok
= isl_stream_next_token(s
);
2027 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2028 isl_stream_error(s
, tok
, "expecting filename");
2029 isl_token_free(tok
);
2033 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2034 if (!options
|| !options
->io
) {
2035 isl_token_free(tok
);
2036 isl_die(s
->ctx
, isl_error_invalid
,
2037 "read operation not allowed", goto error
);
2040 name
= isl_token_get_str(s
->ctx
, tok
);
2041 isl_token_free(tok
);
2042 file
= fopen(name
, "r");
2044 isl_assert(s
->ctx
, file
, goto error
);
2046 s_file
= isl_stream_new_file(s
->ctx
, file
);
2052 obj
= isl_stream_read_obj(s_file
);
2054 isl_stream_free(s_file
);
2059 obj
.type
= isl_obj_none
;
2064 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2065 struct isl_hash_table
*table
)
2068 struct isl_token
*tok
;
2069 struct isl_stream
*s_file
;
2070 struct iscc_options
*options
;
2075 tok
= isl_stream_next_token(s
);
2076 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2077 isl_stream_error(s
, tok
, "expecting filename");
2078 isl_token_free(tok
);
2082 obj
= read_expr(s
, table
);
2084 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2085 if (!options
|| !options
->io
) {
2086 isl_token_free(tok
);
2087 isl_die(s
->ctx
, isl_error_invalid
,
2088 "write operation not allowed", goto error
);
2091 name
= isl_token_get_str(s
->ctx
, tok
);
2092 isl_token_free(tok
);
2093 file
= fopen(name
, "w");
2096 isl_die(s
->ctx
, isl_error_unknown
,
2097 "could not open file for writing", goto error
);
2099 p
= isl_printer_to_file(s
->ctx
, file
);
2100 p
= isl_printer_set_output_format(p
, options
->format
);
2101 p
= obj
.type
->print(p
, obj
.v
);
2102 p
= isl_printer_end_line(p
);
2103 isl_printer_free(p
);
2108 obj
.type
= isl_obj_none
;
2113 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2115 struct isl_token
*tok
;
2116 struct isl_obj obj
= { isl_obj_none
, NULL
};
2118 tok
= isl_stream_next_token(s
);
2121 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2123 str
= isl_str_alloc(s
->ctx
);
2126 str
->s
= isl_token_get_str(s
->ctx
, tok
);
2127 isl_token_free(tok
);
2129 obj
.type
= isl_obj_str
;
2131 isl_stream_push_token(s
, tok
);
2134 isl_token_free(tok
);
2138 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2140 struct isl_token
*tok
;
2141 struct isl_obj obj
= { isl_obj_none
, NULL
};
2144 tok
= isl_stream_next_token(s
);
2147 type
= isl_token_get_type(tok
);
2148 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2149 int is_true
= type
== ISL_TOKEN_TRUE
;
2150 isl_token_free(tok
);
2151 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2152 obj
.type
= isl_obj_bool
;
2154 isl_stream_push_token(s
, tok
);
2158 static __isl_give
char *read_ident(struct isl_stream
*s
)
2162 struct isl_token
*tok
, *tok2
;
2164 name
= isl_stream_read_ident_if_available(s
);
2168 tok
= isl_stream_next_token(s
);
2171 if (isl_token_get_type(tok
) != '$') {
2172 isl_stream_push_token(s
, tok
);
2175 tok2
= isl_stream_next_token(s
);
2176 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2178 isl_stream_push_token(s
, tok2
);
2179 isl_stream_push_token(s
, tok
);
2183 v
= isl_token_get_val(s
->ctx
, tok2
);
2184 name
= isl_val_to_str(v
);
2186 isl_token_free(tok
);
2187 isl_token_free(tok2
);
2192 static struct isl_obj
read_list(struct isl_stream
*s
,
2193 struct isl_hash_table
*table
, struct isl_obj obj
)
2195 struct isl_list
*list
;
2197 list
= isl_list_alloc(s
->ctx
, 2);
2201 list
->obj
[1] = read_obj(s
, table
);
2203 obj
.type
= isl_obj_list
;
2205 if (!list
->obj
[1].v
)
2208 while (isl_stream_eat_if_available(s
, ',')) {
2209 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2217 obj
.type
= isl_obj_none
;
2222 static struct isl_obj
read_obj(struct isl_stream
*s
,
2223 struct isl_hash_table
*table
)
2225 struct isl_obj obj
= { isl_obj_none
, NULL
};
2227 struct isc_un_op
*op
= NULL
;
2229 obj
= read_string_if_available(s
);
2232 obj
= read_bool_if_available(s
);
2235 if (isl_stream_eat_if_available(s
, '(')) {
2236 if (isl_stream_next_token_is(s
, ')')) {
2237 obj
.type
= isl_obj_list
;
2238 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2240 obj
= read_expr(s
, table
);
2241 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2242 obj
= read_list(s
, table
, obj
);
2244 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2247 op
= read_prefix_un_op_if_available(s
);
2249 return read_un_op_expr(s
, table
, op
);
2251 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2252 return check_assert(s
, table
);
2253 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2254 return read_from_file(s
);
2255 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2256 return write_to_file(s
, table
);
2257 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2258 return vertices(s
, table
);
2259 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2260 return any(s
, table
);
2261 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2262 return last(s
, table
);
2263 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2264 return schedule(s
, table
);
2265 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2266 return schedule_forest(s
, table
);
2267 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2268 return type_of(s
, table
);
2270 name
= read_ident(s
);
2272 obj
= stored_obj(s
->ctx
, table
, name
);
2274 obj
= isl_stream_read_obj(s
);
2279 if (isl_stream_eat_if_available(s
, '^'))
2280 obj
= power(s
, obj
);
2281 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2282 obj
= obj_at_index(s
, obj
);
2283 else if (is_subtype(obj
, isl_obj_union_map
) &&
2284 isl_stream_eat_if_available(s
, '(')) {
2285 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2286 obj
= apply(s
, obj
.v
, table
);
2287 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2288 isl_stream_eat_if_available(s
, '(')) {
2289 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2290 obj
= apply_fun(s
, obj
, table
);
2291 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2292 isl_stream_eat_if_available(s
, '(')) {
2293 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2294 obj
= apply_fun(s
, obj
, table
);
2300 obj
.type
= isl_obj_none
;
2305 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2306 struct isl_obj lhs
, struct isl_obj rhs
)
2310 for (i
= 0; ; ++i
) {
2313 if (bin_ops
[i
].op
!= like
->op
)
2315 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2317 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2323 for (i
= 0; ; ++i
) {
2324 if (!named_bin_ops
[i
].name
)
2326 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2328 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2330 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2333 return &named_bin_ops
[i
].op
;
2339 static int next_is_neg_int(struct isl_stream
*s
)
2341 struct isl_token
*tok
;
2344 tok
= isl_stream_next_token(s
);
2345 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2347 v
= isl_token_get_val(s
->ctx
, tok
);
2348 ret
= isl_val_is_neg(v
);
2352 isl_stream_push_token(s
, tok
);
2357 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2358 struct isl_obj lhs
, struct isl_obj rhs
)
2362 lhs
= convert(ctx
, lhs
, op
->lhs
);
2363 rhs
= convert(ctx
, rhs
, op
->rhs
);
2364 if (op
->res
!= isl_obj_bool
)
2365 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2367 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2370 obj
.v
= isl_bool_from_int(res
);
2377 static struct isl_obj
read_expr(struct isl_stream
*s
,
2378 struct isl_hash_table
*table
)
2380 struct isl_obj obj
= { isl_obj_none
, NULL
};
2381 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2383 obj
= read_obj(s
, table
);
2385 struct isc_bin_op
*op
= NULL
;
2387 op
= read_bin_op_if_available(s
, obj
);
2391 right_obj
= read_obj(s
, table
);
2393 op
= find_matching_bin_op(op
, obj
, right_obj
);
2396 isl_die(s
->ctx
, isl_error_invalid
,
2397 "no such binary operator defined on given operands",
2400 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2403 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2404 right_obj
= read_obj(s
, table
);
2405 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2410 free_obj(right_obj
);
2412 obj
.type
= isl_obj_none
;
2417 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2418 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2420 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2421 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2423 struct isl_obj obj
= { isl_obj_none
, NULL
};
2427 struct isc_bin_op
*op
= NULL
;
2432 if (isl_stream_is_empty(s
))
2435 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2436 return source_file(s
, table
, p
);
2437 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2438 return codegen(s
, table
, p
);
2440 assign
= is_assign(s
);
2442 lhs
= isl_stream_read_ident_if_available(s
);
2443 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2445 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2450 obj
= read_expr(s
, table
);
2451 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2452 fprintf(stderr
, "Interrupted\n");
2453 isl_ctx_reset_error(s
->ctx
);
2455 if (isl_stream_eat(s
, ';'))
2459 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2460 p
= obj
.type
->print(p
, obj
.v
);
2461 p
= isl_printer_end_line(p
);
2466 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2467 static int count
= 0;
2468 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2469 lhs
= strdup(buf
+ 1);
2471 p
= isl_printer_print_str(p
, buf
);
2472 p
= isl_printer_print_str(p
, " := ");
2473 p
= obj
.type
->print(p
, obj
.v
);
2474 p
= isl_printer_end_line(p
);
2476 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2481 isl_stream_flush_tokens(s
);
2482 isl_stream_skip_line(s
);
2488 int free_cb(void **entry
, void *user
)
2490 struct isl_named_obj
*named
= *entry
;
2492 free_obj(named
->obj
);
2499 static void register_named_ops(struct isl_stream
*s
)
2503 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2504 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2505 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2508 for (i
= 0; ; ++i
) {
2509 if (!named_un_ops
[i
].name
)
2511 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2512 named_un_ops
[i
].name
);
2513 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2516 for (i
= 0; ; ++i
) {
2517 if (!named_bin_ops
[i
].name
)
2519 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2520 named_bin_ops
[i
].name
);
2521 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2525 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2526 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2528 struct isl_token
*tok
;
2529 struct isl_stream
*s_file
;
2533 tok
= isl_stream_next_token(s
);
2534 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2535 isl_stream_error(s
, tok
, "expecting filename");
2536 isl_token_free(tok
);
2540 name
= isl_token_get_str(s
->ctx
, tok
);
2541 isl_token_free(tok
);
2542 file
= fopen(name
, "r");
2544 isl_assert(s
->ctx
, file
, return p
);
2546 s_file
= isl_stream_new_file(s
->ctx
, file
);
2552 register_named_ops(s_file
);
2554 while (!s_file
->eof
)
2555 p
= read_line(s_file
, table
, p
, 0);
2557 isl_stream_free(s_file
);
2560 isl_stream_eat(s
, ';');
2565 int main(int argc
, char **argv
)
2567 struct isl_ctx
*ctx
;
2568 struct isl_stream
*s
;
2569 struct isl_hash_table
*table
;
2570 struct iscc_options
*options
;
2572 int tty
= isatty(0);
2574 options
= iscc_options_new_with_defaults();
2577 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2578 pet_options_set_autodetect(ctx
, 1);
2579 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2580 s
= isl_stream_new_file(ctx
, stdin
);
2582 table
= isl_hash_table_alloc(ctx
, 10);
2584 p
= isl_printer_to_file(ctx
, stdout
);
2585 p
= isl_printer_set_output_format(p
, options
->format
);
2588 register_named_ops(s
);
2590 install_signal_handler(ctx
);
2592 while (p
&& !s
->eof
) {
2593 isl_ctx_resume(ctx
);
2594 p
= read_line(s
, table
, p
, tty
);
2597 remove_signal_handler(ctx
);
2599 isl_printer_free(p
);
2600 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2601 isl_hash_table_free(ctx
, table
);