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
;
231 set
= isl_set_from_point(isl_point_copy(pnt
));
232 qp
= isl_union_pw_qpolynomial_eval(
233 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
235 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
236 isl_union_pw_qpolynomial_from_pw_qpolynomial(
237 isl_pw_qpolynomial_alloc(set
, qp
)));
242 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
243 __isl_take isl_union_pw_qpolynomial
*upwqp
,
244 __isl_take isl_union_set
*uset
)
249 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
251 isl_union_set_foreach_point(uset
, eval_at
, &at
);
253 isl_union_pw_qpolynomial_free(upwqp
);
254 isl_union_set_free(uset
);
259 struct iscc_fold_at
{
260 isl_union_pw_qpolynomial_fold
*upwf
;
261 isl_union_pw_qpolynomial
*res
;
264 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
266 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
270 set
= isl_set_from_point(isl_point_copy(pnt
));
271 qp
= isl_union_pw_qpolynomial_fold_eval(
272 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
274 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
275 isl_union_pw_qpolynomial_from_pw_qpolynomial(
276 isl_pw_qpolynomial_alloc(set
, qp
)));
281 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
282 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
283 __isl_take isl_union_set
*uset
)
285 struct iscc_fold_at at
;
288 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
290 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
292 isl_union_pw_qpolynomial_fold_free(upwf
);
293 isl_union_set_free(uset
);
298 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
299 __isl_take isl_union_pw_qpolynomial
*upwqp
,
300 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
302 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
306 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
307 __isl_take isl_union_map
*umap
,
308 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
311 struct isl_list
*list
;
314 ctx
= isl_union_map_get_ctx(umap
);
315 list
= isl_list_alloc(ctx
, 2);
319 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
320 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
322 list
->obj
[1].type
= isl_obj_bool
;
323 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
324 if (tight
< 0 || !list
->obj
[0].v
)
329 isl_union_map_free(umap
);
330 isl_union_pw_qpolynomial_fold_free(upwf
);
336 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
337 __isl_take isl_union_set
*uset
,
338 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
341 struct isl_list
*list
;
344 ctx
= isl_union_set_get_ctx(uset
);
345 list
= isl_list_alloc(ctx
, 2);
349 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
350 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
352 list
->obj
[1].type
= isl_obj_bool
;
353 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
354 if (tight
< 0 || !list
->obj
[0].v
)
359 isl_union_set_free(uset
);
360 isl_union_pw_qpolynomial_fold_free(upwf
);
366 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_int_mul(
367 __isl_take isl_union_pw_qpolynomial
*upwqp
, __isl_take isl_int_obj
*i
)
375 isl_int_obj_get_int(i
, &v
);
376 upwqp
= isl_union_pw_qpolynomial_mul_isl_int(upwqp
, v
);
383 isl_union_pw_qpolynomial_free(upwqp
);
387 static __isl_give isl_union_pw_qpolynomial
*int_union_pw_qpolynomial_mul(
388 __isl_take isl_int_obj
*i
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
390 return union_pw_qpolynomial_int_mul(upwqp
, i
);
393 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_fold_int_mul(
394 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
395 __isl_take isl_int_obj
*i
)
403 isl_int_obj_get_int(i
, &v
);
404 upwf
= isl_union_pw_qpolynomial_fold_mul_isl_int(upwf
, v
);
411 isl_union_pw_qpolynomial_fold_free(upwf
);
415 static __isl_give isl_union_pw_qpolynomial_fold
*int_union_pw_qpolynomial_fold_mul(
416 __isl_take isl_int_obj
*i
,
417 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
419 return union_pw_qpolynomial_fold_int_mul(upwf
, i
);
422 struct isc_bin_op bin_ops
[] = {
423 { '+', isl_obj_int
, isl_obj_int
, isl_obj_int
,
424 (isc_bin_op_fn
) &isl_int_obj_add
},
425 { '-', isl_obj_int
, isl_obj_int
, isl_obj_int
,
426 (isc_bin_op_fn
) &isl_int_obj_sub
},
427 { '*', isl_obj_int
, isl_obj_int
, isl_obj_int
,
428 (isc_bin_op_fn
) &isl_int_obj_mul
},
429 { '+', isl_obj_union_set
, isl_obj_union_set
,
431 (isc_bin_op_fn
) &isl_union_set_union
},
432 { '+', isl_obj_union_map
, isl_obj_union_map
,
434 (isc_bin_op_fn
) &isl_union_map_union
},
435 { '-', isl_obj_union_set
, isl_obj_union_set
,
437 (isc_bin_op_fn
) &isl_union_set_subtract
},
438 { '-', isl_obj_union_map
, isl_obj_union_map
,
440 (isc_bin_op_fn
) &isl_union_map_subtract
},
441 { '*', isl_obj_union_set
, isl_obj_union_set
,
443 (isc_bin_op_fn
) &isl_union_set_intersect
},
444 { '*', isl_obj_union_map
, isl_obj_union_map
,
446 (isc_bin_op_fn
) &isl_union_map_intersect
},
447 { '*', isl_obj_union_map
, isl_obj_union_set
,
449 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
450 { '.', isl_obj_union_map
, isl_obj_union_map
,
452 (isc_bin_op_fn
) &isl_union_map_apply_range
},
453 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
454 isl_obj_union_pw_qpolynomial
,
455 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
456 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
458 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
459 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
461 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
462 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
463 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
464 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
465 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
466 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
468 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
469 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
471 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
472 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
474 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
475 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
477 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
478 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
480 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
481 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
483 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
484 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
487 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
488 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
491 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
492 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
494 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
495 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
497 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
498 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
500 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
501 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
503 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
504 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
506 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
507 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
509 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
510 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
512 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
513 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
515 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
516 { '.', isl_obj_union_pw_qpolynomial_fold
,
517 isl_obj_union_pw_qpolynomial_fold
,
518 isl_obj_union_pw_qpolynomial_fold
,
519 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
520 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
521 isl_obj_union_pw_qpolynomial
,
522 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
523 { '+', isl_obj_union_pw_qpolynomial
,
524 isl_obj_union_pw_qpolynomial_fold
,
525 isl_obj_union_pw_qpolynomial_fold
,
526 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
527 { '+', isl_obj_union_pw_qpolynomial_fold
,
528 isl_obj_union_pw_qpolynomial
,
529 isl_obj_union_pw_qpolynomial_fold
,
530 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
531 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
532 isl_obj_union_pw_qpolynomial
,
533 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
534 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial
,
535 isl_obj_union_pw_qpolynomial
,
536 (isc_bin_op_fn
) &int_union_pw_qpolynomial_mul
},
537 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_int
,
538 isl_obj_union_pw_qpolynomial
,
539 (isc_bin_op_fn
) &union_pw_qpolynomial_int_mul
},
540 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial_fold
,
541 isl_obj_union_pw_qpolynomial_fold
,
542 (isc_bin_op_fn
) &int_union_pw_qpolynomial_fold_mul
},
543 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_int
,
544 isl_obj_union_pw_qpolynomial_fold
,
545 (isc_bin_op_fn
) &union_pw_qpolynomial_fold_int_mul
},
546 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
547 isl_obj_union_pw_qpolynomial
,
548 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
549 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
550 isl_obj_union_pw_qpolynomial
,
551 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
552 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
553 isl_obj_union_pw_qpolynomial_fold
,
554 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
555 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
556 isl_obj_union_pw_qpolynomial
,
557 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
558 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
559 isl_obj_union_pw_qpolynomial
,
560 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
561 { '%', isl_obj_union_set
, isl_obj_union_set
,
563 (isc_bin_op_fn
) &isl_union_set_gist
},
564 { '%', isl_obj_union_map
, isl_obj_union_map
,
566 (isc_bin_op_fn
) &isl_union_map_gist
},
567 { '%', isl_obj_union_map
, isl_obj_union_set
,
569 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
570 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
571 isl_obj_union_pw_qpolynomial
,
572 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
573 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
574 isl_obj_union_pw_qpolynomial_fold
,
575 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
576 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
577 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
578 { .test
= (isc_bin_test_fn
)
579 &isl_union_pw_qpolynomial_plain_is_equal
} },
580 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
581 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
582 { .test
= (isc_bin_test_fn
)
583 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
584 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
585 (isc_bin_op_fn
) &isl_str_concat
},
589 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
590 __isl_take isl_union_map
*umap2
)
592 return isl_union_map_apply_range(umap2
, umap1
);
595 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
596 __isl_take isl_union_pw_qpolynomial
*upwqp
,
597 __isl_take isl_union_map
*umap
)
599 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
602 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
603 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
604 __isl_take isl_union_map
*umap
)
606 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
609 struct isc_named_bin_op named_bin_ops
[] = {
610 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
612 (isc_bin_op_fn
) &map_after_map
} },
613 { "after", { -1, isl_obj_union_pw_qpolynomial
,
614 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
615 (isc_bin_op_fn
) &qpolynomial_after_map
} },
616 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
617 isl_obj_union_map
, isl_obj_list
,
618 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
619 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
621 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
622 { "before", { -1, isl_obj_union_map
,
623 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
624 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
625 { "before", { -1, isl_obj_union_map
,
626 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
627 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
628 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
630 (isc_bin_op_fn
) &isl_union_set_product
} },
631 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
633 (isc_bin_op_fn
) &isl_union_map_product
} },
637 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
639 return isl_set_from_basic_set(isl_union_set_sample(uset
));
642 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
644 return isl_map_from_basic_map(isl_union_map_sample(umap
));
647 static __isl_give
struct isl_list
*union_map_power(
648 __isl_take isl_union_map
*umap
)
651 struct isl_list
*list
;
654 ctx
= isl_union_map_get_ctx(umap
);
655 list
= isl_list_alloc(ctx
, 2);
659 list
->obj
[0].type
= isl_obj_union_map
;
660 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
661 list
->obj
[1].type
= isl_obj_bool
;
662 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
663 if (exact
< 0 || !list
->obj
[0].v
)
668 isl_union_map_free(umap
);
674 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
675 __isl_take isl_union_pw_qpolynomial
*upwqp
)
678 struct isl_list
*list
;
681 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
682 list
= isl_list_alloc(ctx
, 2);
686 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
687 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
688 isl_fold_max
, &tight
);
689 list
->obj
[1].type
= isl_obj_bool
;
690 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
691 if (tight
< 0 || !list
->obj
[0].v
)
696 isl_union_pw_qpolynomial_free(upwqp
);
703 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
706 struct isl_list
*list
;
707 struct pet_scop
*scop
;
708 isl_union_map
*sched
, *reads
, *writes
;
709 isl_union_set
*domain
;
710 struct iscc_options
*options
;
716 options
= isl_ctx_peek_iscc_options(ctx
);
717 if (!options
|| !options
->io
) {
719 isl_die(ctx
, isl_error_invalid
,
720 "parse_file operation not allowed", return NULL
);
723 list
= isl_list_alloc(ctx
, 4);
727 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
728 domain
= pet_scop_collect_domains(scop
);
729 sched
= pet_scop_collect_schedule(scop
);
730 reads
= pet_scop_collect_reads(scop
);
731 writes
= pet_scop_collect_writes(scop
);
734 list
->obj
[0].type
= isl_obj_union_set
;
735 list
->obj
[0].v
= domain
;
736 list
->obj
[1].type
= isl_obj_union_map
;
737 list
->obj
[1].v
= writes
;
738 list
->obj
[2].type
= isl_obj_union_map
;
739 list
->obj
[2].v
= reads
;
740 list
->obj
[3].type
= isl_obj_union_map
;
741 list
->obj
[3].v
= sched
;
743 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
744 !list
->obj
[2].v
|| !list
->obj
[3].v
)
756 static int add_point(__isl_take isl_point
*pnt
, void *user
)
758 isl_union_set
**scan
= (isl_union_set
**) user
;
760 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
765 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
769 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
771 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
772 isl_union_set_free(scan
);
776 isl_union_set_free(uset
);
780 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
782 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
785 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
786 __isl_take isl_union_pw_qpolynomial
*upwqp
)
788 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
791 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
792 __isl_take isl_union_pw_qpolynomial
*upwqp
)
794 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
797 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
798 __isl_take isl_union_pw_qpolynomial
*upwqp
)
800 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
803 typedef void *(*isc_un_op_fn
)(void *arg
);
805 enum isl_token_type op
;
810 struct isc_named_un_op
{
814 struct isc_named_un_op named_un_ops
[] = {
815 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
816 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
817 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
818 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
819 {"card", { -1, isl_obj_union_set
,
820 isl_obj_union_pw_qpolynomial
,
821 (isc_un_op_fn
) &isl_union_set_card
} },
822 {"card", { -1, isl_obj_union_map
,
823 isl_obj_union_pw_qpolynomial
,
824 (isc_un_op_fn
) &isl_union_map_card
} },
825 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
826 (isc_un_op_fn
) &isl_union_set_coalesce
} },
827 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
828 (isc_un_op_fn
) &isl_union_map_coalesce
} },
829 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
830 isl_obj_union_pw_qpolynomial
,
831 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
832 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
833 isl_obj_union_pw_qpolynomial_fold
,
834 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
835 {"coefficients", { -1, isl_obj_union_set
,
837 (isc_un_op_fn
) &isl_union_set_coefficients
} },
838 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
839 (isc_un_op_fn
) &isl_union_set_solutions
} },
840 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
841 (isc_un_op_fn
) &isl_union_map_deltas
} },
842 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
843 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
844 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
845 (isc_un_op_fn
) &isl_union_map_domain
} },
846 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
847 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
848 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
850 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
851 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
852 (isc_un_op_fn
) &isl_union_map_domain
} },
853 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
855 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
856 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
858 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
859 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
860 (isc_un_op_fn
) &isl_union_map_domain_map
} },
861 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
862 (isc_un_op_fn
) &isl_union_map_range
} },
863 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
864 (isc_un_op_fn
) &isl_union_map_range
} },
865 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
866 (isc_un_op_fn
) &isl_union_map_range_map
} },
867 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
868 (isc_un_op_fn
) &isl_union_set_identity
} },
869 {"lattice_width", { -1, isl_obj_union_set
,
870 isl_obj_union_pw_qpolynomial
,
871 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
872 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
873 (isc_un_op_fn
) &isl_union_map_lexmin
} },
874 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
875 (isc_un_op_fn
) &isl_union_map_lexmax
} },
876 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
877 (isc_un_op_fn
) &isl_union_set_lexmin
} },
878 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
879 (isc_un_op_fn
) &isl_union_set_lexmax
} },
880 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
881 (isc_un_op_fn
) &isl_union_set_lift
} },
882 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
883 (isc_un_op_fn
) &isl_union_map_params
} },
884 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
885 (isc_un_op_fn
) &isl_union_set_params
} },
886 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
887 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
888 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
889 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
890 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
891 isl_obj_union_pw_qpolynomial
,
892 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
893 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
894 isl_obj_union_pw_qpolynomial
,
895 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
896 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
897 isl_obj_union_pw_qpolynomial
,
898 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
900 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
901 (isc_un_op_fn
) &parse
} },
903 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
904 (isc_un_op_fn
) &union_map_power
} },
905 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
906 (isc_un_op_fn
) &union_set_sample
} },
907 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
908 (isc_un_op_fn
) &union_map_sample
} },
909 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
910 (isc_un_op_fn
) &union_set_scan
} },
911 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
912 (isc_un_op_fn
) &union_map_scan
} },
913 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
914 isl_obj_union_pw_qpolynomial
,
915 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
916 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
917 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
918 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
919 (isc_un_op_fn
) &isl_union_set_unwrap
} },
920 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
921 (isc_un_op_fn
) &isl_union_map_wrap
} },
922 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
923 (isc_un_op_fn
) &isl_union_map_zip
} },
927 struct isl_named_obj
{
932 static void free_obj(struct isl_obj obj
)
934 obj
.type
->free(obj
.v
);
937 static int same_name(const void *entry
, const void *val
)
939 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
941 return !strcmp(named
->name
, val
);
944 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
945 char *name
, struct isl_obj obj
)
947 struct isl_hash_table_entry
*entry
;
949 struct isl_named_obj
*named
;
951 name_hash
= isl_hash_string(isl_hash_init(), name
);
952 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
957 free_obj(named
->obj
);
960 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
975 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
976 struct isl_hash_table
*table
, char *name
)
978 struct isl_obj obj
= { isl_obj_none
, NULL
};
979 struct isl_hash_table_entry
*entry
;
982 name_hash
= isl_hash_string(isl_hash_init(), name
);
983 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
985 struct isl_named_obj
*named
;
988 } else if (isdigit(name
[0]))
989 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
991 fprintf(stderr
, "unknown identifier '%s'\n", name
);
994 obj
.v
= obj
.type
->copy(obj
.v
);
998 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1000 if (obj
.type
== super
)
1002 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1004 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1006 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1007 super
== isl_obj_union_pw_qpolynomial
)
1009 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1010 super
== isl_obj_union_pw_qpolynomial_fold
)
1012 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1014 if (obj
.type
== isl_obj_list
) {
1015 struct isl_list
*list
= obj
.v
;
1016 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1017 return is_subtype(list
->obj
[0], super
);
1019 if (super
== isl_obj_str
)
1024 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1026 struct isl_list
*list
= obj
.v
;
1029 obj
.v
= obj
.type
->copy(obj
.v
);
1031 isl_list_free(list
);
1036 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1039 if (obj
.type
== type
)
1041 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1042 obj
.type
= isl_obj_union_map
;
1043 obj
.v
= isl_union_map_from_map(obj
.v
);
1046 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1047 obj
.type
= isl_obj_union_set
;
1048 obj
.v
= isl_union_set_from_set(obj
.v
);
1051 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1052 type
== isl_obj_union_pw_qpolynomial
) {
1053 obj
.type
= isl_obj_union_pw_qpolynomial
;
1054 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1057 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1058 type
== isl_obj_union_pw_qpolynomial_fold
) {
1059 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1060 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1063 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1064 if (type
== isl_obj_union_map
) {
1065 obj
.type
= isl_obj_union_map
;
1068 if (type
== isl_obj_union_pw_qpolynomial
) {
1069 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1070 isl_union_set_free(obj
.v
);
1071 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1072 obj
.type
= isl_obj_union_pw_qpolynomial
;
1075 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1076 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1077 isl_union_set_free(obj
.v
);
1078 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1080 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1084 if (obj
.type
== isl_obj_list
) {
1085 struct isl_list
*list
= obj
.v
;
1086 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1087 return convert(ctx
, obj_at(obj
, 0), type
);
1089 if (type
== isl_obj_str
) {
1094 p
= isl_printer_to_str(ctx
);
1097 p
= obj
.type
->print(p
, obj
.v
);
1098 s
= isl_printer_get_str(p
);
1099 isl_printer_free(p
);
1101 str
= isl_str_from_string(ctx
, s
);
1106 obj
.type
= isl_obj_str
;
1112 obj
.type
= isl_obj_none
;
1117 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1121 struct isl_token
*tok
;
1123 tok
= isl_stream_next_token(s
);
1127 for (i
= 0; ; ++i
) {
1130 if (bin_ops
[i
].op
!= tok
->type
)
1132 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1135 isl_token_free(tok
);
1139 for (i
= 0; ; ++i
) {
1140 if (!named_bin_ops
[i
].name
)
1142 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1144 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1147 isl_token_free(tok
);
1148 return &named_bin_ops
[i
].op
;
1151 isl_stream_push_token(s
, tok
);
1156 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1159 struct isl_token
*tok
;
1161 tok
= isl_stream_next_token(s
);
1165 for (i
= 0; ; ++i
) {
1166 if (!named_un_ops
[i
].name
)
1168 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1171 isl_token_free(tok
);
1172 return &named_un_ops
[i
].op
;
1175 isl_stream_push_token(s
, tok
);
1180 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1185 for (i
= 0; ; ++i
) {
1186 if (!named_un_ops
[i
].name
)
1188 if (named_un_ops
[i
].op
.op
!= like
->op
)
1190 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1193 return &named_un_ops
[i
].op
;
1199 static int is_assign(struct isl_stream
*s
)
1201 struct isl_token
*tok
;
1202 struct isl_token
*tok2
;
1205 tok
= isl_stream_next_token(s
);
1208 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1209 isl_stream_push_token(s
, tok
);
1213 tok2
= isl_stream_next_token(s
);
1215 isl_stream_push_token(s
, tok
);
1218 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1219 isl_stream_push_token(s
, tok2
);
1220 isl_stream_push_token(s
, tok
);
1225 static struct isl_obj
read_obj(struct isl_stream
*s
,
1226 struct isl_hash_table
*table
);
1227 static struct isl_obj
read_expr(struct isl_stream
*s
,
1228 struct isl_hash_table
*table
);
1230 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1231 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1233 struct isl_obj obj
= { isl_obj_none
, NULL
};
1235 obj
= read_obj(s
, table
);
1239 op
= find_matching_un_op(op
, obj
);
1242 isl_die(s
->ctx
, isl_error_invalid
,
1243 "no such unary operator defined on given operand",
1246 obj
= convert(s
->ctx
, obj
, op
->arg
);
1247 obj
.v
= op
->fn(obj
.v
);
1253 obj
.type
= isl_obj_none
;
1258 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1260 struct isl_list
*list
;
1263 if (obj
.type
!= isl_obj_union_map
)
1264 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1265 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1266 list
= isl_list_alloc(ctx
, 2);
1270 list
->obj
[0].type
= isl_obj_union_map
;
1271 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1272 list
->obj
[1].type
= isl_obj_bool
;
1273 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1275 obj
.type
= isl_obj_list
;
1276 if (exact
< 0 || !list
->obj
[0].v
)
1282 obj
.type
= isl_obj_none
;
1287 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1289 struct isl_list
*list
= obj
.v
;
1290 struct isl_token
*tok
;
1293 tok
= isl_stream_next_token(s
);
1294 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1295 isl_stream_error(s
, tok
, "expecting index");
1297 isl_stream_push_token(s
, tok
);
1300 i
= isl_int_get_si(tok
->u
.v
);
1301 isl_token_free(tok
);
1302 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1303 if (isl_stream_eat(s
, ']'))
1306 return obj_at(obj
, i
);
1309 obj
.type
= isl_obj_none
;
1314 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1315 struct isl_hash_table
*table
)
1319 obj
= read_expr(s
, table
);
1320 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1321 is_subtype(obj
, isl_obj_union_map
), goto error
);
1323 if (obj
.type
== isl_obj_list
) {
1324 struct isl_list
*list
= obj
.v
;
1325 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1326 obj
= obj_at(obj
, 0);
1328 if (obj
.type
== isl_obj_set
)
1329 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1330 else if (obj
.type
== isl_obj_map
)
1331 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1332 if (obj
.type
== isl_obj_union_set
) {
1333 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1335 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1339 if (isl_stream_eat(s
, ')'))
1344 isl_union_map_free(umap
);
1347 obj
.type
= isl_obj_none
;
1352 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1353 __isl_take isl_union_set
*uset
)
1355 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1356 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1358 obj
.type
= isl_obj_list
;
1359 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1364 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1365 __isl_take isl_union_map
*umap
)
1367 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1368 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1370 obj
.type
= isl_obj_list
;
1371 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1376 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1377 struct isl_obj obj
, struct isl_hash_table
*table
)
1381 arg
= read_expr(s
, table
);
1382 if (!is_subtype(arg
, isl_obj_union_map
) &&
1383 !is_subtype(arg
, isl_obj_union_set
))
1384 isl_die(s
->ctx
, isl_error_invalid
,
1385 "expecting set of map argument", goto error
);
1387 if (arg
.type
== isl_obj_list
) {
1388 struct isl_list
*list
= arg
.v
;
1389 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1390 arg
= obj_at(arg
, 0);
1392 if (arg
.type
== isl_obj_set
)
1393 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1394 else if (arg
.type
== isl_obj_map
)
1395 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1396 if (arg
.type
== isl_obj_union_set
)
1397 obj
= apply_fun_set(obj
, arg
.v
);
1399 obj
= apply_fun_map(obj
, arg
.v
);
1403 if (isl_stream_eat(s
, ')'))
1411 obj
.type
= isl_obj_none
;
1416 struct add_vertex_data
{
1417 struct isl_list
*list
;
1421 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1423 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1424 isl_basic_set
*expr
;
1426 expr
= isl_vertex_get_expr(vertex
);
1428 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1429 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1432 isl_vertex_free(vertex
);
1437 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1440 isl_basic_set
*hull
;
1441 isl_vertices
*vertices
= NULL
;
1442 struct isl_list
*list
= NULL
;
1444 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1446 set
= isl_set_remove_divs(set
);
1447 hull
= isl_set_convex_hull(set
);
1448 vertices
= isl_basic_set_compute_vertices(hull
);
1449 isl_basic_set_free(hull
);
1453 ctx
= isl_vertices_get_ctx(vertices
);
1454 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1459 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1461 data
->list
= isl_list_concat(list
, data
->list
);
1463 isl_vertices_free(vertices
);
1468 isl_vertices_free(vertices
);
1472 static struct isl_obj
vertices(struct isl_stream
*s
,
1473 struct isl_hash_table
*table
)
1477 struct isl_list
*list
= NULL
;
1478 isl_union_set
*uset
;
1479 struct add_vertex_data data
= { NULL
};
1481 obj
= read_expr(s
, table
);
1482 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1483 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1487 ctx
= isl_union_set_get_ctx(uset
);
1488 list
= isl_list_alloc(ctx
, 0);
1494 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1497 isl_union_set_free(uset
);
1499 obj
.type
= isl_obj_list
;
1504 isl_union_set_free(uset
);
1505 isl_list_free(data
.list
);
1507 obj
.type
= isl_obj_none
;
1512 static struct isl_obj
type_of(struct isl_stream
*s
,
1513 struct isl_hash_table
*table
)
1517 const char *type
= "unknown";
1519 obj
= read_expr(s
, table
);
1521 if (obj
.type
== isl_obj_map
||
1522 obj
.type
== isl_obj_union_map
)
1524 if (obj
.type
== isl_obj_set
||
1525 obj
.type
== isl_obj_union_set
)
1527 if (obj
.type
== isl_obj_pw_qpolynomial
||
1528 obj
.type
== isl_obj_union_pw_qpolynomial
)
1529 type
= "piecewise quasipolynomial";
1530 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1531 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1532 type
= "piecewise quasipolynomial fold";
1533 if (obj
.type
== isl_obj_list
)
1535 if (obj
.type
== isl_obj_bool
)
1537 if (obj
.type
== isl_obj_str
)
1539 if (obj
.type
== isl_obj_int
)
1543 obj
.type
= isl_obj_str
;
1544 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1549 static __isl_give isl_union_set
*read_set(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_set
);
1556 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1563 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1564 struct isl_hash_table
*table
)
1568 obj
= read_obj(s
, table
);
1569 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1570 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1577 static struct isl_obj
last_any(struct isl_stream
*s
,
1578 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1579 __isl_take isl_union_map
*may_source
)
1581 struct isl_obj obj
= { isl_obj_none
, NULL
};
1582 isl_union_map
*sink
= NULL
;
1583 isl_union_map
*schedule
= NULL
;
1584 isl_union_map
*may_dep
;
1585 isl_union_map
*must_dep
;
1587 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1590 sink
= read_map(s
, table
);
1594 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1597 schedule
= read_map(s
, table
);
1601 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1602 schedule
, &must_dep
, &may_dep
,
1606 obj
.type
= isl_obj_union_map
;
1607 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1611 isl_union_map_free(may_source
);
1612 isl_union_map_free(must_source
);
1613 isl_union_map_free(sink
);
1614 isl_union_map_free(schedule
);
1616 obj
.type
= isl_obj_none
;
1621 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1623 struct isl_obj obj
= { isl_obj_none
, NULL
};
1624 isl_union_map
*must_source
= NULL
;
1625 isl_union_map
*may_source
= NULL
;
1626 isl_union_map
*sink
= NULL
;
1627 isl_union_map
*schedule
= NULL
;
1628 isl_union_map
*may_dep
;
1630 may_source
= read_map(s
, table
);
1634 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1635 must_source
= read_map(s
, table
);
1638 return last_any(s
, table
, must_source
, may_source
);
1641 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1644 sink
= read_map(s
, table
);
1648 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1651 schedule
= read_map(s
, table
);
1655 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1656 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1657 schedule
, NULL
, &may_dep
,
1661 obj
.type
= isl_obj_union_map
;
1666 isl_union_map_free(may_source
);
1667 isl_union_map_free(must_source
);
1668 isl_union_map_free(sink
);
1669 isl_union_map_free(schedule
);
1671 obj
.type
= isl_obj_none
;
1676 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1678 struct isl_obj obj
= { isl_obj_none
, NULL
};
1679 struct isl_list
*list
= NULL
;
1680 isl_union_map
*must_source
= NULL
;
1681 isl_union_map
*may_source
= NULL
;
1682 isl_union_map
*sink
= NULL
;
1683 isl_union_map
*schedule
= NULL
;
1684 isl_union_map
*must_dep
;
1685 isl_union_map
*must_no_source
;
1687 must_source
= read_map(s
, table
);
1691 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1692 may_source
= read_map(s
, table
);
1695 return last_any(s
, table
, must_source
, may_source
);
1698 list
= isl_list_alloc(s
->ctx
, 2);
1702 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1705 sink
= read_map(s
, table
);
1709 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1712 schedule
= read_map(s
, table
);
1716 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1717 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1718 schedule
, &must_dep
, NULL
,
1719 &must_no_source
, NULL
) < 0) {
1720 isl_list_free(list
);
1724 list
->obj
[0].type
= isl_obj_union_map
;
1725 list
->obj
[0].v
= must_dep
;
1726 list
->obj
[1].type
= isl_obj_union_map
;
1727 list
->obj
[1].v
= must_no_source
;
1730 obj
.type
= isl_obj_list
;
1734 isl_list_free(list
);
1735 isl_union_map_free(may_source
);
1736 isl_union_map_free(must_source
);
1737 isl_union_map_free(sink
);
1738 isl_union_map_free(schedule
);
1740 obj
.type
= isl_obj_none
;
1745 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1746 struct isl_hash_table
*table
)
1748 isl_union_set
*domain
;
1749 isl_union_map
*validity
;
1750 isl_union_map
*proximity
;
1752 domain
= read_set(s
, table
);
1756 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1757 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1760 isl_union_map
*umap
;
1761 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1762 umap
= read_map(s
, table
);
1763 validity
= isl_union_map_union(validity
, umap
);
1764 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1765 umap
= read_map(s
, table
);
1766 proximity
= isl_union_map_union(proximity
, umap
);
1771 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1774 static struct isl_obj
schedule(struct isl_stream
*s
,
1775 struct isl_hash_table
*table
)
1777 struct isl_obj obj
= { isl_obj_none
, NULL
};
1778 isl_schedule
*schedule
;
1780 schedule
= get_schedule(s
, table
);
1782 obj
.v
= isl_schedule_get_map(schedule
);
1783 obj
.type
= isl_obj_union_map
;
1785 isl_schedule_free(schedule
);
1790 /* Read a schedule for code generation.
1791 * If the input is a set rather than a map, then we construct
1792 * an identity schedule on the given set.
1794 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1795 struct isl_hash_table
*table
)
1799 obj
= read_obj(s
, table
);
1801 if (is_subtype(obj
, isl_obj_union_map
)) {
1802 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1806 if (is_subtype(obj
, isl_obj_union_set
)) {
1807 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1808 return isl_union_set_identity(obj
.v
);
1812 isl_die(s
->ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1815 /* Generate an AST for the given schedule and options and print
1816 * the AST on the printer.
1818 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1819 __isl_take isl_union_map
*schedule
,
1820 __isl_take isl_union_map
*options
)
1823 isl_ast_build
*build
;
1827 context
= isl_set_universe(isl_union_map_get_space(schedule
));
1829 build
= isl_ast_build_from_context(context
);
1830 build
= isl_ast_build_set_options(build
, options
);
1831 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1832 isl_ast_build_free(build
);
1837 format
= isl_printer_get_output_format(p
);
1838 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1839 p
= isl_printer_print_ast_node(p
, tree
);
1840 p
= isl_printer_set_output_format(p
, format
);
1842 isl_ast_node_free(tree
);
1847 /* Perform the codegen operation.
1848 * In particular, read a schedule, check if the user has specified any options
1849 * and then generate an AST from the schedule (and options) and print it.
1851 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1852 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1854 isl_union_map
*schedule
;
1855 isl_union_map
*options
;
1857 schedule
= get_codegen_schedule(s
, table
);
1861 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1862 options
= read_map(s
, table
);
1864 options
= isl_union_map_empty(
1865 isl_union_map_get_space(schedule
));
1867 p
= print_code(p
, schedule
, options
);
1869 isl_stream_eat(s
, ';');
1874 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1876 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1878 struct isl_obj obj
= { isl_obj_none
, NULL
};
1879 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1880 struct isl_list
*list
;
1882 list
= isl_list_alloc(ctx
, 2);
1887 obj
.type
= isl_obj_list
;
1889 list
->obj
[0].type
= isl_obj_union_map
;
1890 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1892 if (isl_band_has_children(band
)) {
1893 isl_band_list
*children
;
1895 children
= isl_band_get_children(band
);
1896 list
->obj
[1] = band_list_to_obj_list(children
);
1898 list
->obj
[1].type
= isl_obj_list
;
1899 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1902 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1905 isl_band_free(band
);
1909 isl_band_free(band
);
1911 obj
.type
= isl_obj_none
;
1916 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1918 struct isl_obj obj
= { isl_obj_none
, NULL
};
1919 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1920 struct isl_list
*list
;
1923 n
= isl_band_list_n_band(bands
);
1924 list
= isl_list_alloc(ctx
, n
);
1929 obj
.type
= isl_obj_list
;
1931 for (i
= 0; i
< n
; ++i
) {
1934 band
= isl_band_list_get_band(bands
, i
);
1935 list
->obj
[i
] = band_to_obj_list(band
);
1936 if (!list
->obj
[i
].v
)
1940 isl_band_list_free(bands
);
1944 isl_band_list_free(bands
);
1946 obj
.type
= isl_obj_none
;
1951 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1952 struct isl_hash_table
*table
)
1954 struct isl_obj obj
= { isl_obj_none
, NULL
};
1955 isl_schedule
*schedule
;
1956 isl_band_list
*roots
;
1958 schedule
= get_schedule(s
, table
);
1962 roots
= isl_schedule_get_band_forest(schedule
);
1963 isl_schedule_free(schedule
);
1965 return band_list_to_obj_list(roots
);
1968 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1970 struct isl_token
*tok
;
1972 if (isl_stream_eat_if_available(s
, '+'))
1973 return transitive_closure(s
->ctx
, obj
);
1975 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1976 if (obj
.type
!= isl_obj_union_map
)
1977 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1979 tok
= isl_stream_next_token(s
);
1980 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_is_zero(tok
->u
.v
)) {
1981 isl_stream_error(s
, tok
, "expecting non-zero integer exponent");
1983 isl_stream_push_token(s
, tok
);
1987 obj
.v
= isl_union_map_fixed_power(obj
.v
, tok
->u
.v
);
1988 isl_token_free(tok
);
1995 obj
.type
= isl_obj_none
;
2000 static struct isl_obj
check_assert(struct isl_stream
*s
,
2001 struct isl_hash_table
*table
)
2005 obj
= read_expr(s
, table
);
2006 if (obj
.type
!= isl_obj_bool
)
2007 isl_die(s
->ctx
, isl_error_invalid
,
2008 "expecting boolean expression", goto error
);
2009 if (obj
.v
!= &isl_bool_true
)
2010 isl_die(s
->ctx
, isl_error_unknown
,
2011 "assertion failed", abort());
2014 obj
.type
= isl_obj_none
;
2019 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2022 struct isl_token
*tok
;
2023 struct isl_stream
*s_file
;
2024 struct iscc_options
*options
;
2027 tok
= isl_stream_next_token(s
);
2028 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2029 isl_stream_error(s
, tok
, "expecting filename");
2030 isl_token_free(tok
);
2034 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2035 if (!options
|| !options
->io
) {
2036 isl_token_free(tok
);
2037 isl_die(s
->ctx
, isl_error_invalid
,
2038 "read operation not allowed", goto error
);
2041 file
= fopen(tok
->u
.s
, "r");
2042 isl_token_free(tok
);
2043 isl_assert(s
->ctx
, file
, goto error
);
2045 s_file
= isl_stream_new_file(s
->ctx
, file
);
2051 obj
= isl_stream_read_obj(s_file
);
2053 isl_stream_free(s_file
);
2058 obj
.type
= isl_obj_none
;
2063 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2064 struct isl_hash_table
*table
)
2067 struct isl_token
*tok
;
2068 struct isl_stream
*s_file
;
2069 struct iscc_options
*options
;
2073 tok
= isl_stream_next_token(s
);
2074 if (!tok
|| tok
->type
!= 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 file
= fopen(tok
->u
.s
, "w");
2090 isl_token_free(tok
);
2092 isl_die(s
->ctx
, isl_error_unknown
,
2093 "could not open file for writing", goto error
);
2095 p
= isl_printer_to_file(s
->ctx
, file
);
2096 p
= isl_printer_set_output_format(p
, options
->format
);
2097 p
= obj
.type
->print(p
, obj
.v
);
2098 p
= isl_printer_end_line(p
);
2099 isl_printer_free(p
);
2104 obj
.type
= isl_obj_none
;
2109 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2111 struct isl_token
*tok
;
2112 struct isl_obj obj
= { isl_obj_none
, NULL
};
2114 tok
= isl_stream_next_token(s
);
2117 if (tok
->type
== ISL_TOKEN_STRING
) {
2119 str
= isl_str_alloc(s
->ctx
);
2122 str
->s
= strdup(tok
->u
.s
);
2123 isl_token_free(tok
);
2125 obj
.type
= isl_obj_str
;
2127 isl_stream_push_token(s
, tok
);
2130 isl_token_free(tok
);
2134 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2136 struct isl_token
*tok
;
2137 struct isl_obj obj
= { isl_obj_none
, NULL
};
2139 tok
= isl_stream_next_token(s
);
2142 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2143 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2144 isl_token_free(tok
);
2145 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2146 obj
.type
= isl_obj_bool
;
2148 isl_stream_push_token(s
, tok
);
2152 static __isl_give
char *read_ident(struct isl_stream
*s
)
2155 struct isl_token
*tok
, *tok2
;
2157 name
= isl_stream_read_ident_if_available(s
);
2161 tok
= isl_stream_next_token(s
);
2164 if (tok
->type
!= '$') {
2165 isl_stream_push_token(s
, tok
);
2168 tok2
= isl_stream_next_token(s
);
2169 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2171 isl_stream_push_token(s
, tok2
);
2172 isl_stream_push_token(s
, tok
);
2176 name
= isl_int_get_str(tok2
->u
.v
);
2177 isl_token_free(tok
);
2178 isl_token_free(tok2
);
2183 static struct isl_obj
read_list(struct isl_stream
*s
,
2184 struct isl_hash_table
*table
, struct isl_obj obj
)
2186 struct isl_list
*list
;
2188 list
= isl_list_alloc(s
->ctx
, 2);
2192 list
->obj
[1] = read_obj(s
, table
);
2194 obj
.type
= isl_obj_list
;
2196 if (!list
->obj
[1].v
)
2199 while (isl_stream_eat_if_available(s
, ',')) {
2200 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2208 obj
.type
= isl_obj_none
;
2213 static struct isl_obj
read_obj(struct isl_stream
*s
,
2214 struct isl_hash_table
*table
)
2216 struct isl_obj obj
= { isl_obj_none
, NULL
};
2218 struct isc_un_op
*op
= NULL
;
2220 obj
= read_string_if_available(s
);
2223 obj
= read_bool_if_available(s
);
2226 if (isl_stream_eat_if_available(s
, '(')) {
2227 if (isl_stream_next_token_is(s
, ')')) {
2228 obj
.type
= isl_obj_list
;
2229 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2231 obj
= read_expr(s
, table
);
2232 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2233 obj
= read_list(s
, table
, obj
);
2235 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2238 op
= read_prefix_un_op_if_available(s
);
2240 return read_un_op_expr(s
, table
, op
);
2242 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2243 return check_assert(s
, table
);
2244 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2245 return read_from_file(s
);
2246 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2247 return write_to_file(s
, table
);
2248 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2249 return vertices(s
, table
);
2250 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2251 return any(s
, table
);
2252 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2253 return last(s
, table
);
2254 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2255 return schedule(s
, table
);
2256 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2257 return schedule_forest(s
, table
);
2258 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2259 return type_of(s
, table
);
2261 name
= read_ident(s
);
2263 obj
= stored_obj(s
->ctx
, table
, name
);
2265 obj
= isl_stream_read_obj(s
);
2270 if (isl_stream_eat_if_available(s
, '^'))
2271 obj
= power(s
, obj
);
2272 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2273 obj
= obj_at_index(s
, obj
);
2274 else if (is_subtype(obj
, isl_obj_union_map
) &&
2275 isl_stream_eat_if_available(s
, '(')) {
2276 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2277 obj
= apply(s
, obj
.v
, table
);
2278 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2279 isl_stream_eat_if_available(s
, '(')) {
2280 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2281 obj
= apply_fun(s
, obj
, table
);
2282 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2283 isl_stream_eat_if_available(s
, '(')) {
2284 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2285 obj
= apply_fun(s
, obj
, table
);
2291 obj
.type
= isl_obj_none
;
2296 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2297 struct isl_obj lhs
, struct isl_obj rhs
)
2301 for (i
= 0; ; ++i
) {
2304 if (bin_ops
[i
].op
!= like
->op
)
2306 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2308 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2314 for (i
= 0; ; ++i
) {
2315 if (!named_bin_ops
[i
].name
)
2317 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2319 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2321 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2324 return &named_bin_ops
[i
].op
;
2330 static int next_is_neg_int(struct isl_stream
*s
)
2332 struct isl_token
*tok
;
2335 tok
= isl_stream_next_token(s
);
2336 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2337 isl_stream_push_token(s
, tok
);
2342 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2343 struct isl_obj lhs
, struct isl_obj rhs
)
2347 lhs
= convert(ctx
, lhs
, op
->lhs
);
2348 rhs
= convert(ctx
, rhs
, op
->rhs
);
2349 if (op
->res
!= isl_obj_bool
)
2350 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2352 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2355 obj
.v
= isl_bool_from_int(res
);
2362 static struct isl_obj
read_expr(struct isl_stream
*s
,
2363 struct isl_hash_table
*table
)
2365 struct isl_obj obj
= { isl_obj_none
, NULL
};
2366 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2368 obj
= read_obj(s
, table
);
2370 struct isc_bin_op
*op
= NULL
;
2372 op
= read_bin_op_if_available(s
, obj
);
2376 right_obj
= read_obj(s
, table
);
2378 op
= find_matching_bin_op(op
, obj
, right_obj
);
2381 isl_die(s
->ctx
, isl_error_invalid
,
2382 "no such binary operator defined on given operands",
2385 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2388 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2389 right_obj
= read_obj(s
, table
);
2390 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2395 free_obj(right_obj
);
2397 obj
.type
= isl_obj_none
;
2402 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2403 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2405 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2406 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2408 struct isl_obj obj
= { isl_obj_none
, NULL
};
2412 struct isc_bin_op
*op
= NULL
;
2417 if (isl_stream_is_empty(s
))
2420 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2421 return source_file(s
, table
, p
);
2422 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2423 return codegen(s
, table
, p
);
2425 assign
= is_assign(s
);
2427 lhs
= isl_stream_read_ident_if_available(s
);
2428 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2430 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2435 obj
= read_expr(s
, table
);
2436 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2437 fprintf(stderr
, "Interrupted\n");
2438 isl_ctx_reset_error(s
->ctx
);
2440 if (isl_stream_eat(s
, ';'))
2444 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2445 p
= obj
.type
->print(p
, obj
.v
);
2446 p
= isl_printer_end_line(p
);
2451 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2452 static int count
= 0;
2453 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2454 lhs
= strdup(buf
+ 1);
2456 p
= isl_printer_print_str(p
, buf
);
2457 p
= isl_printer_print_str(p
, " := ");
2458 p
= obj
.type
->print(p
, obj
.v
);
2459 p
= isl_printer_end_line(p
);
2461 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2466 isl_stream_flush_tokens(s
);
2467 isl_stream_skip_line(s
);
2473 int free_cb(void **entry
, void *user
)
2475 struct isl_named_obj
*named
= *entry
;
2477 free_obj(named
->obj
);
2484 static void register_named_ops(struct isl_stream
*s
)
2488 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2489 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2490 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2493 for (i
= 0; ; ++i
) {
2494 if (!named_un_ops
[i
].name
)
2496 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2497 named_un_ops
[i
].name
);
2498 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2501 for (i
= 0; ; ++i
) {
2502 if (!named_bin_ops
[i
].name
)
2504 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2505 named_bin_ops
[i
].name
);
2506 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2510 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2511 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2513 struct isl_token
*tok
;
2514 struct isl_stream
*s_file
;
2517 tok
= isl_stream_next_token(s
);
2518 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2519 isl_stream_error(s
, tok
, "expecting filename");
2520 isl_token_free(tok
);
2524 file
= fopen(tok
->u
.s
, "r");
2525 isl_token_free(tok
);
2526 isl_assert(s
->ctx
, file
, return p
);
2528 s_file
= isl_stream_new_file(s
->ctx
, file
);
2534 register_named_ops(s_file
);
2536 while (!s_file
->eof
)
2537 p
= read_line(s_file
, table
, p
, 0);
2539 isl_stream_free(s_file
);
2542 isl_stream_eat(s
, ';');
2547 int main(int argc
, char **argv
)
2549 struct isl_ctx
*ctx
;
2550 struct isl_stream
*s
;
2551 struct isl_hash_table
*table
;
2552 struct iscc_options
*options
;
2554 int tty
= isatty(0);
2556 options
= iscc_options_new_with_defaults();
2559 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2560 pet_options_set_autodetect(ctx
, 1);
2561 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2562 s
= isl_stream_new_file(ctx
, stdin
);
2564 table
= isl_hash_table_alloc(ctx
, 10);
2566 p
= isl_printer_to_file(ctx
, stdout
);
2567 p
= isl_printer_set_output_format(p
, options
->format
);
2570 register_named_ops(s
);
2572 install_signal_handler(ctx
);
2574 while (p
&& !s
->eof
) {
2575 isl_ctx_resume(ctx
);
2576 p
= read_line(s
, table
, p
, tty
);
2579 remove_signal_handler(ctx
);
2581 isl_printer_free(p
);
2582 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2583 isl_hash_table_free(ctx
, table
);