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
;
1406 isl_basic_set
*expr
;
1408 expr
= isl_vertex_get_expr(vertex
);
1410 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1411 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1414 isl_vertex_free(vertex
);
1419 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1422 isl_basic_set
*hull
;
1423 isl_vertices
*vertices
= NULL
;
1424 struct isl_list
*list
= NULL
;
1426 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1428 set
= isl_set_remove_divs(set
);
1429 hull
= isl_set_convex_hull(set
);
1430 vertices
= isl_basic_set_compute_vertices(hull
);
1431 isl_basic_set_free(hull
);
1435 ctx
= isl_vertices_get_ctx(vertices
);
1436 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1441 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1443 data
->list
= isl_list_concat(list
, data
->list
);
1445 isl_vertices_free(vertices
);
1450 isl_vertices_free(vertices
);
1454 static struct isl_obj
vertices(struct isl_stream
*s
,
1455 struct isl_hash_table
*table
)
1459 struct isl_list
*list
= NULL
;
1460 isl_union_set
*uset
;
1461 struct add_vertex_data data
= { NULL
};
1463 obj
= read_expr(s
, table
);
1464 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1465 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1469 ctx
= isl_union_set_get_ctx(uset
);
1470 list
= isl_list_alloc(ctx
, 0);
1476 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1479 isl_union_set_free(uset
);
1481 obj
.type
= isl_obj_list
;
1486 isl_union_set_free(uset
);
1487 isl_list_free(data
.list
);
1489 obj
.type
= isl_obj_none
;
1494 static struct isl_obj
type_of(struct isl_stream
*s
,
1495 struct isl_hash_table
*table
)
1499 const char *type
= "unknown";
1501 obj
= read_expr(s
, table
);
1503 if (obj
.type
== isl_obj_map
||
1504 obj
.type
== isl_obj_union_map
)
1506 if (obj
.type
== isl_obj_set
||
1507 obj
.type
== isl_obj_union_set
)
1509 if (obj
.type
== isl_obj_pw_multi_aff
)
1510 type
= "piecewise multi-quasiaffine expression";
1511 if (obj
.type
== isl_obj_pw_qpolynomial
||
1512 obj
.type
== isl_obj_union_pw_qpolynomial
)
1513 type
= "piecewise quasipolynomial";
1514 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1515 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1516 type
= "piecewise quasipolynomial fold";
1517 if (obj
.type
== isl_obj_list
)
1519 if (obj
.type
== isl_obj_bool
)
1521 if (obj
.type
== isl_obj_str
)
1523 if (obj
.type
== isl_obj_val
)
1527 obj
.type
= isl_obj_str
;
1528 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1533 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1534 struct isl_hash_table
*table
)
1538 obj
= read_obj(s
, table
);
1539 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1540 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1547 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1548 struct isl_hash_table
*table
)
1552 obj
= read_obj(s
, table
);
1553 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1554 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1561 static struct isl_obj
last_any(struct isl_stream
*s
,
1562 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1563 __isl_take isl_union_map
*may_source
)
1565 struct isl_obj obj
= { isl_obj_none
, NULL
};
1566 isl_union_map
*sink
= NULL
;
1567 isl_union_map
*schedule
= NULL
;
1568 isl_union_map
*may_dep
;
1569 isl_union_map
*must_dep
;
1571 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1574 sink
= read_map(s
, table
);
1578 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1581 schedule
= read_map(s
, table
);
1585 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1586 schedule
, &must_dep
, &may_dep
,
1590 obj
.type
= isl_obj_union_map
;
1591 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1595 isl_union_map_free(may_source
);
1596 isl_union_map_free(must_source
);
1597 isl_union_map_free(sink
);
1598 isl_union_map_free(schedule
);
1600 obj
.type
= isl_obj_none
;
1605 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1607 struct isl_obj obj
= { isl_obj_none
, NULL
};
1608 isl_union_map
*must_source
= NULL
;
1609 isl_union_map
*may_source
= NULL
;
1610 isl_union_map
*sink
= NULL
;
1611 isl_union_map
*schedule
= NULL
;
1612 isl_union_map
*may_dep
;
1614 may_source
= read_map(s
, table
);
1618 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1619 must_source
= read_map(s
, table
);
1622 return last_any(s
, table
, must_source
, may_source
);
1625 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1628 sink
= read_map(s
, table
);
1632 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1635 schedule
= read_map(s
, table
);
1639 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1640 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1641 schedule
, NULL
, &may_dep
,
1645 obj
.type
= isl_obj_union_map
;
1650 isl_union_map_free(may_source
);
1651 isl_union_map_free(must_source
);
1652 isl_union_map_free(sink
);
1653 isl_union_map_free(schedule
);
1655 obj
.type
= isl_obj_none
;
1660 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1662 struct isl_obj obj
= { isl_obj_none
, NULL
};
1663 struct isl_list
*list
= NULL
;
1664 isl_union_map
*must_source
= NULL
;
1665 isl_union_map
*may_source
= NULL
;
1666 isl_union_map
*sink
= NULL
;
1667 isl_union_map
*schedule
= NULL
;
1668 isl_union_map
*must_dep
;
1669 isl_union_map
*must_no_source
;
1671 must_source
= read_map(s
, table
);
1675 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1676 may_source
= read_map(s
, table
);
1679 return last_any(s
, table
, must_source
, may_source
);
1682 list
= isl_list_alloc(s
->ctx
, 2);
1686 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1689 sink
= read_map(s
, table
);
1693 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1696 schedule
= read_map(s
, table
);
1700 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1701 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1702 schedule
, &must_dep
, NULL
,
1703 &must_no_source
, NULL
) < 0) {
1704 isl_list_free(list
);
1708 list
->obj
[0].type
= isl_obj_union_map
;
1709 list
->obj
[0].v
= must_dep
;
1710 list
->obj
[1].type
= isl_obj_union_map
;
1711 list
->obj
[1].v
= must_no_source
;
1714 obj
.type
= isl_obj_list
;
1718 isl_list_free(list
);
1719 isl_union_map_free(may_source
);
1720 isl_union_map_free(must_source
);
1721 isl_union_map_free(sink
);
1722 isl_union_map_free(schedule
);
1724 obj
.type
= isl_obj_none
;
1729 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1730 struct isl_hash_table
*table
)
1732 isl_union_set
*domain
;
1733 isl_union_map
*validity
;
1734 isl_union_map
*proximity
;
1736 domain
= read_set(s
, table
);
1740 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1741 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1744 isl_union_map
*umap
;
1745 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1746 umap
= read_map(s
, table
);
1747 validity
= isl_union_map_union(validity
, umap
);
1748 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1749 umap
= read_map(s
, table
);
1750 proximity
= isl_union_map_union(proximity
, umap
);
1755 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1758 static struct isl_obj
schedule(struct isl_stream
*s
,
1759 struct isl_hash_table
*table
)
1761 struct isl_obj obj
= { isl_obj_none
, NULL
};
1762 isl_schedule
*schedule
;
1764 schedule
= get_schedule(s
, table
);
1766 obj
.v
= isl_schedule_get_map(schedule
);
1767 obj
.type
= isl_obj_union_map
;
1769 isl_schedule_free(schedule
);
1774 /* Read a schedule for code generation.
1775 * If the input is a set rather than a map, then we construct
1776 * an identity schedule on the given set.
1778 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1779 struct isl_hash_table
*table
)
1783 obj
= read_obj(s
, table
);
1785 if (is_subtype(obj
, isl_obj_union_map
)) {
1786 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1790 if (is_subtype(obj
, isl_obj_union_set
)) {
1791 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1792 return isl_union_set_identity(obj
.v
);
1796 isl_die(s
->ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1799 /* Generate an AST for the given schedule and options and print
1800 * the AST on the printer.
1802 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1803 __isl_take isl_union_map
*schedule
,
1804 __isl_take isl_union_map
*options
)
1808 isl_ast_build
*build
;
1812 space
= isl_union_map_get_space(schedule
);
1813 context
= isl_set_universe(isl_space_params(space
));
1815 build
= isl_ast_build_from_context(context
);
1816 build
= isl_ast_build_set_options(build
, options
);
1817 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1818 isl_ast_build_free(build
);
1823 format
= isl_printer_get_output_format(p
);
1824 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1825 p
= isl_printer_print_ast_node(p
, tree
);
1826 p
= isl_printer_set_output_format(p
, format
);
1828 isl_ast_node_free(tree
);
1833 /* Perform the codegen operation.
1834 * In particular, read a schedule, check if the user has specified any options
1835 * and then generate an AST from the schedule (and options) and print it.
1837 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1838 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1840 isl_union_map
*schedule
;
1841 isl_union_map
*options
;
1843 schedule
= get_codegen_schedule(s
, table
);
1847 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1848 options
= read_map(s
, table
);
1850 options
= isl_union_map_empty(
1851 isl_union_map_get_space(schedule
));
1853 p
= print_code(p
, schedule
, options
);
1855 isl_stream_eat(s
, ';');
1860 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1862 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1864 struct isl_obj obj
= { isl_obj_none
, NULL
};
1865 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1866 struct isl_list
*list
;
1868 list
= isl_list_alloc(ctx
, 2);
1873 obj
.type
= isl_obj_list
;
1875 list
->obj
[0].type
= isl_obj_union_map
;
1876 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1878 if (isl_band_has_children(band
)) {
1879 isl_band_list
*children
;
1881 children
= isl_band_get_children(band
);
1882 list
->obj
[1] = band_list_to_obj_list(children
);
1884 list
->obj
[1].type
= isl_obj_list
;
1885 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1888 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1891 isl_band_free(band
);
1895 isl_band_free(band
);
1897 obj
.type
= isl_obj_none
;
1902 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1904 struct isl_obj obj
= { isl_obj_none
, NULL
};
1905 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1906 struct isl_list
*list
;
1909 n
= isl_band_list_n_band(bands
);
1910 list
= isl_list_alloc(ctx
, n
);
1915 obj
.type
= isl_obj_list
;
1917 for (i
= 0; i
< n
; ++i
) {
1920 band
= isl_band_list_get_band(bands
, i
);
1921 list
->obj
[i
] = band_to_obj_list(band
);
1922 if (!list
->obj
[i
].v
)
1926 isl_band_list_free(bands
);
1930 isl_band_list_free(bands
);
1932 obj
.type
= isl_obj_none
;
1937 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1938 struct isl_hash_table
*table
)
1940 struct isl_obj obj
= { isl_obj_none
, NULL
};
1941 isl_schedule
*schedule
;
1942 isl_band_list
*roots
;
1944 schedule
= get_schedule(s
, table
);
1948 roots
= isl_schedule_get_band_forest(schedule
);
1949 isl_schedule_free(schedule
);
1951 return band_list_to_obj_list(roots
);
1954 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1956 struct isl_token
*tok
;
1959 if (isl_stream_eat_if_available(s
, '+'))
1960 return transitive_closure(s
->ctx
, obj
);
1962 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1963 if (obj
.type
!= isl_obj_union_map
)
1964 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1966 tok
= isl_stream_next_token(s
);
1967 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1968 isl_stream_error(s
, tok
, "expecting integer exponent");
1970 isl_stream_push_token(s
, tok
);
1974 v
= isl_token_get_val(s
->ctx
, tok
);
1975 if (isl_val_is_zero(v
)) {
1976 isl_stream_error(s
, tok
, "expecting non-zero exponent");
1979 isl_stream_push_token(s
, tok
);
1983 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
1984 isl_token_free(tok
);
1991 obj
.type
= isl_obj_none
;
1996 static struct isl_obj
check_assert(struct isl_stream
*s
,
1997 struct isl_hash_table
*table
)
2001 obj
= read_expr(s
, table
);
2002 if (obj
.type
!= isl_obj_bool
)
2003 isl_die(s
->ctx
, isl_error_invalid
,
2004 "expecting boolean expression", goto error
);
2005 if (obj
.v
!= &isl_bool_true
)
2006 isl_die(s
->ctx
, isl_error_unknown
,
2007 "assertion failed", abort());
2010 obj
.type
= isl_obj_none
;
2015 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2018 struct isl_token
*tok
;
2019 struct isl_stream
*s_file
;
2020 struct iscc_options
*options
;
2024 tok
= isl_stream_next_token(s
);
2025 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2026 isl_stream_error(s
, tok
, "expecting filename");
2027 isl_token_free(tok
);
2031 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2032 if (!options
|| !options
->io
) {
2033 isl_token_free(tok
);
2034 isl_die(s
->ctx
, isl_error_invalid
,
2035 "read operation not allowed", goto error
);
2038 name
= isl_token_get_str(s
->ctx
, tok
);
2039 isl_token_free(tok
);
2040 file
= fopen(name
, "r");
2042 isl_assert(s
->ctx
, file
, goto error
);
2044 s_file
= isl_stream_new_file(s
->ctx
, file
);
2050 obj
= isl_stream_read_obj(s_file
);
2052 isl_stream_free(s_file
);
2057 obj
.type
= isl_obj_none
;
2062 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2063 struct isl_hash_table
*table
)
2066 struct isl_token
*tok
;
2067 struct isl_stream
*s_file
;
2068 struct iscc_options
*options
;
2073 tok
= isl_stream_next_token(s
);
2074 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2075 isl_stream_error(s
, tok
, "expecting filename");
2076 isl_token_free(tok
);
2080 obj
= read_expr(s
, table
);
2082 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2083 if (!options
|| !options
->io
) {
2084 isl_token_free(tok
);
2085 isl_die(s
->ctx
, isl_error_invalid
,
2086 "write operation not allowed", goto error
);
2089 name
= isl_token_get_str(s
->ctx
, tok
);
2090 isl_token_free(tok
);
2091 file
= fopen(name
, "w");
2094 isl_die(s
->ctx
, isl_error_unknown
,
2095 "could not open file for writing", goto error
);
2097 p
= isl_printer_to_file(s
->ctx
, file
);
2098 p
= isl_printer_set_output_format(p
, options
->format
);
2099 p
= obj
.type
->print(p
, obj
.v
);
2100 p
= isl_printer_end_line(p
);
2101 isl_printer_free(p
);
2106 obj
.type
= isl_obj_none
;
2111 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2113 struct isl_token
*tok
;
2114 struct isl_obj obj
= { isl_obj_none
, NULL
};
2116 tok
= isl_stream_next_token(s
);
2119 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2121 str
= isl_str_alloc(s
->ctx
);
2124 str
->s
= isl_token_get_str(s
->ctx
, tok
);
2125 isl_token_free(tok
);
2127 obj
.type
= isl_obj_str
;
2129 isl_stream_push_token(s
, tok
);
2132 isl_token_free(tok
);
2136 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2138 struct isl_token
*tok
;
2139 struct isl_obj obj
= { isl_obj_none
, NULL
};
2142 tok
= isl_stream_next_token(s
);
2145 type
= isl_token_get_type(tok
);
2146 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2147 int is_true
= type
== ISL_TOKEN_TRUE
;
2148 isl_token_free(tok
);
2149 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2150 obj
.type
= isl_obj_bool
;
2152 isl_stream_push_token(s
, tok
);
2156 static __isl_give
char *read_ident(struct isl_stream
*s
)
2160 struct isl_token
*tok
, *tok2
;
2162 name
= isl_stream_read_ident_if_available(s
);
2166 tok
= isl_stream_next_token(s
);
2169 if (isl_token_get_type(tok
) != '$') {
2170 isl_stream_push_token(s
, tok
);
2173 tok2
= isl_stream_next_token(s
);
2174 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2176 isl_stream_push_token(s
, tok2
);
2177 isl_stream_push_token(s
, tok
);
2181 v
= isl_token_get_val(s
->ctx
, tok2
);
2182 name
= isl_val_to_str(v
);
2184 isl_token_free(tok
);
2185 isl_token_free(tok2
);
2190 static struct isl_obj
read_list(struct isl_stream
*s
,
2191 struct isl_hash_table
*table
, struct isl_obj obj
)
2193 struct isl_list
*list
;
2195 list
= isl_list_alloc(s
->ctx
, 2);
2199 list
->obj
[1] = read_obj(s
, table
);
2201 obj
.type
= isl_obj_list
;
2203 if (!list
->obj
[1].v
)
2206 while (isl_stream_eat_if_available(s
, ',')) {
2207 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2215 obj
.type
= isl_obj_none
;
2220 static struct isl_obj
read_obj(struct isl_stream
*s
,
2221 struct isl_hash_table
*table
)
2223 struct isl_obj obj
= { isl_obj_none
, NULL
};
2225 struct isc_un_op
*op
= NULL
;
2227 obj
= read_string_if_available(s
);
2230 obj
= read_bool_if_available(s
);
2233 if (isl_stream_eat_if_available(s
, '(')) {
2234 if (isl_stream_next_token_is(s
, ')')) {
2235 obj
.type
= isl_obj_list
;
2236 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2238 obj
= read_expr(s
, table
);
2239 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2240 obj
= read_list(s
, table
, obj
);
2242 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2245 op
= read_prefix_un_op_if_available(s
);
2247 return read_un_op_expr(s
, table
, op
);
2249 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2250 return check_assert(s
, table
);
2251 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2252 return read_from_file(s
);
2253 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2254 return write_to_file(s
, table
);
2255 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2256 return vertices(s
, table
);
2257 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2258 return any(s
, table
);
2259 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2260 return last(s
, table
);
2261 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2262 return schedule(s
, table
);
2263 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2264 return schedule_forest(s
, table
);
2265 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2266 return type_of(s
, table
);
2268 name
= read_ident(s
);
2270 obj
= stored_obj(s
->ctx
, table
, name
);
2272 obj
= isl_stream_read_obj(s
);
2277 if (isl_stream_eat_if_available(s
, '^'))
2278 obj
= power(s
, obj
);
2279 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2280 obj
= obj_at_index(s
, obj
);
2281 else if (is_subtype(obj
, isl_obj_union_map
) &&
2282 isl_stream_eat_if_available(s
, '(')) {
2283 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2284 obj
= apply(s
, obj
.v
, table
);
2285 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2286 isl_stream_eat_if_available(s
, '(')) {
2287 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2288 obj
= apply_fun(s
, obj
, table
);
2289 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2290 isl_stream_eat_if_available(s
, '(')) {
2291 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2292 obj
= apply_fun(s
, obj
, table
);
2298 obj
.type
= isl_obj_none
;
2303 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2304 struct isl_obj lhs
, struct isl_obj rhs
)
2308 for (i
= 0; ; ++i
) {
2311 if (bin_ops
[i
].op
!= like
->op
)
2313 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2315 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2321 for (i
= 0; ; ++i
) {
2322 if (!named_bin_ops
[i
].name
)
2324 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2326 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2328 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2331 return &named_bin_ops
[i
].op
;
2337 static int next_is_neg_int(struct isl_stream
*s
)
2339 struct isl_token
*tok
;
2342 tok
= isl_stream_next_token(s
);
2343 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2345 v
= isl_token_get_val(s
->ctx
, tok
);
2346 ret
= isl_val_is_neg(v
);
2350 isl_stream_push_token(s
, tok
);
2355 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2356 struct isl_obj lhs
, struct isl_obj rhs
)
2360 lhs
= convert(ctx
, lhs
, op
->lhs
);
2361 rhs
= convert(ctx
, rhs
, op
->rhs
);
2362 if (op
->res
!= isl_obj_bool
)
2363 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2365 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2368 obj
.v
= isl_bool_from_int(res
);
2375 static struct isl_obj
read_expr(struct isl_stream
*s
,
2376 struct isl_hash_table
*table
)
2378 struct isl_obj obj
= { isl_obj_none
, NULL
};
2379 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2381 obj
= read_obj(s
, table
);
2383 struct isc_bin_op
*op
= NULL
;
2385 op
= read_bin_op_if_available(s
, obj
);
2389 right_obj
= read_obj(s
, table
);
2391 op
= find_matching_bin_op(op
, obj
, right_obj
);
2394 isl_die(s
->ctx
, isl_error_invalid
,
2395 "no such binary operator defined on given operands",
2398 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2401 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2402 right_obj
= read_obj(s
, table
);
2403 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2408 free_obj(right_obj
);
2410 obj
.type
= isl_obj_none
;
2415 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2416 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2418 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2419 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2421 struct isl_obj obj
= { isl_obj_none
, NULL
};
2425 struct isc_bin_op
*op
= NULL
;
2430 if (isl_stream_is_empty(s
))
2433 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2434 return source_file(s
, table
, p
);
2435 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2436 return codegen(s
, table
, p
);
2438 assign
= is_assign(s
);
2440 lhs
= isl_stream_read_ident_if_available(s
);
2441 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2443 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2448 obj
= read_expr(s
, table
);
2449 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2450 fprintf(stderr
, "Interrupted\n");
2451 isl_ctx_reset_error(s
->ctx
);
2453 if (isl_stream_eat(s
, ';'))
2457 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2458 p
= obj
.type
->print(p
, obj
.v
);
2459 p
= isl_printer_end_line(p
);
2464 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2465 static int count
= 0;
2466 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2467 lhs
= strdup(buf
+ 1);
2469 p
= isl_printer_print_str(p
, buf
);
2470 p
= isl_printer_print_str(p
, " := ");
2471 p
= obj
.type
->print(p
, obj
.v
);
2472 p
= isl_printer_end_line(p
);
2474 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2479 isl_stream_flush_tokens(s
);
2480 isl_stream_skip_line(s
);
2486 int free_cb(void **entry
, void *user
)
2488 struct isl_named_obj
*named
= *entry
;
2490 free_obj(named
->obj
);
2497 static void register_named_ops(struct isl_stream
*s
)
2501 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2502 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2503 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2506 for (i
= 0; ; ++i
) {
2507 if (!named_un_ops
[i
].name
)
2509 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2510 named_un_ops
[i
].name
);
2511 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2514 for (i
= 0; ; ++i
) {
2515 if (!named_bin_ops
[i
].name
)
2517 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2518 named_bin_ops
[i
].name
);
2519 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2523 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2524 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2526 struct isl_token
*tok
;
2527 struct isl_stream
*s_file
;
2531 tok
= isl_stream_next_token(s
);
2532 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2533 isl_stream_error(s
, tok
, "expecting filename");
2534 isl_token_free(tok
);
2538 name
= isl_token_get_str(s
->ctx
, tok
);
2539 isl_token_free(tok
);
2540 file
= fopen(name
, "r");
2542 isl_assert(s
->ctx
, file
, return p
);
2544 s_file
= isl_stream_new_file(s
->ctx
, file
);
2550 register_named_ops(s_file
);
2552 while (!s_file
->eof
)
2553 p
= read_line(s_file
, table
, p
, 0);
2555 isl_stream_free(s_file
);
2558 isl_stream_eat(s
, ';');
2563 int main(int argc
, char **argv
)
2565 struct isl_ctx
*ctx
;
2566 struct isl_stream
*s
;
2567 struct isl_hash_table
*table
;
2568 struct iscc_options
*options
;
2570 int tty
= isatty(0);
2572 options
= iscc_options_new_with_defaults();
2575 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2576 pet_options_set_autodetect(ctx
, 1);
2577 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2578 s
= isl_stream_new_file(ctx
, stdin
);
2580 table
= isl_hash_table_alloc(ctx
, 10);
2582 p
= isl_printer_to_file(ctx
, stdout
);
2583 p
= isl_printer_set_output_format(p
, options
->format
);
2586 register_named_ops(s
);
2588 install_signal_handler(ctx
);
2590 while (p
&& !s
->eof
) {
2591 isl_ctx_resume(ctx
);
2592 p
= read_line(s
, table
, p
, tty
);
2595 remove_signal_handler(ctx
);
2597 isl_printer_free(p
);
2598 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2599 isl_hash_table_free(ctx
, table
);