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
;
1294 tok
= isl_stream_next_token(s
);
1295 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1296 isl_stream_error(s
, tok
, "expecting index");
1298 isl_stream_push_token(s
, tok
);
1301 v
= isl_token_get_val(s
->ctx
, tok
);
1302 i
= isl_val_get_num_si(v
);
1304 isl_token_free(tok
);
1305 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1306 if (isl_stream_eat(s
, ']'))
1309 return obj_at(obj
, i
);
1312 obj
.type
= isl_obj_none
;
1317 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1318 struct isl_hash_table
*table
)
1322 obj
= read_expr(s
, table
);
1323 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1324 is_subtype(obj
, isl_obj_union_map
), goto error
);
1326 if (obj
.type
== isl_obj_list
) {
1327 struct isl_list
*list
= obj
.v
;
1328 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1329 obj
= obj_at(obj
, 0);
1331 if (obj
.type
== isl_obj_set
)
1332 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1333 else if (obj
.type
== isl_obj_map
)
1334 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1335 if (obj
.type
== isl_obj_union_set
) {
1336 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1338 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1342 if (isl_stream_eat(s
, ')'))
1347 isl_union_map_free(umap
);
1350 obj
.type
= isl_obj_none
;
1355 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1356 __isl_take isl_union_set
*uset
)
1358 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1359 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1361 obj
.type
= isl_obj_list
;
1362 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1367 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1368 __isl_take isl_union_map
*umap
)
1370 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1371 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1373 obj
.type
= isl_obj_list
;
1374 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1379 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1380 struct isl_obj obj
, struct isl_hash_table
*table
)
1384 arg
= read_expr(s
, table
);
1385 if (!is_subtype(arg
, isl_obj_union_map
) &&
1386 !is_subtype(arg
, isl_obj_union_set
))
1387 isl_die(s
->ctx
, isl_error_invalid
,
1388 "expecting set of map argument", goto error
);
1390 if (arg
.type
== isl_obj_list
) {
1391 struct isl_list
*list
= arg
.v
;
1392 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1393 arg
= obj_at(arg
, 0);
1395 if (arg
.type
== isl_obj_set
)
1396 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1397 else if (arg
.type
== isl_obj_map
)
1398 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1399 if (arg
.type
== isl_obj_union_set
)
1400 obj
= apply_fun_set(obj
, arg
.v
);
1402 obj
= apply_fun_map(obj
, arg
.v
);
1406 if (isl_stream_eat(s
, ')'))
1414 obj
.type
= isl_obj_none
;
1419 struct add_vertex_data
{
1420 struct isl_list
*list
;
1424 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1426 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1427 isl_basic_set
*expr
;
1429 expr
= isl_vertex_get_expr(vertex
);
1431 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1432 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1435 isl_vertex_free(vertex
);
1440 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1443 isl_basic_set
*hull
;
1444 isl_vertices
*vertices
= NULL
;
1445 struct isl_list
*list
= NULL
;
1447 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1449 set
= isl_set_remove_divs(set
);
1450 hull
= isl_set_convex_hull(set
);
1451 vertices
= isl_basic_set_compute_vertices(hull
);
1452 isl_basic_set_free(hull
);
1456 ctx
= isl_vertices_get_ctx(vertices
);
1457 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1462 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1464 data
->list
= isl_list_concat(list
, data
->list
);
1466 isl_vertices_free(vertices
);
1471 isl_vertices_free(vertices
);
1475 static struct isl_obj
vertices(struct isl_stream
*s
,
1476 struct isl_hash_table
*table
)
1480 struct isl_list
*list
= NULL
;
1481 isl_union_set
*uset
;
1482 struct add_vertex_data data
= { NULL
};
1484 obj
= read_expr(s
, table
);
1485 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1486 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1490 ctx
= isl_union_set_get_ctx(uset
);
1491 list
= isl_list_alloc(ctx
, 0);
1497 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1500 isl_union_set_free(uset
);
1502 obj
.type
= isl_obj_list
;
1507 isl_union_set_free(uset
);
1508 isl_list_free(data
.list
);
1510 obj
.type
= isl_obj_none
;
1515 static struct isl_obj
type_of(struct isl_stream
*s
,
1516 struct isl_hash_table
*table
)
1520 const char *type
= "unknown";
1522 obj
= read_expr(s
, table
);
1524 if (obj
.type
== isl_obj_map
||
1525 obj
.type
== isl_obj_union_map
)
1527 if (obj
.type
== isl_obj_set
||
1528 obj
.type
== isl_obj_union_set
)
1530 if (obj
.type
== isl_obj_pw_qpolynomial
||
1531 obj
.type
== isl_obj_union_pw_qpolynomial
)
1532 type
= "piecewise quasipolynomial";
1533 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1534 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1535 type
= "piecewise quasipolynomial fold";
1536 if (obj
.type
== isl_obj_list
)
1538 if (obj
.type
== isl_obj_bool
)
1540 if (obj
.type
== isl_obj_str
)
1542 if (obj
.type
== isl_obj_int
)
1546 obj
.type
= isl_obj_str
;
1547 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1552 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1553 struct isl_hash_table
*table
)
1557 obj
= read_obj(s
, table
);
1558 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1559 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1566 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1567 struct isl_hash_table
*table
)
1571 obj
= read_obj(s
, table
);
1572 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1573 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1580 static struct isl_obj
last_any(struct isl_stream
*s
,
1581 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1582 __isl_take isl_union_map
*may_source
)
1584 struct isl_obj obj
= { isl_obj_none
, NULL
};
1585 isl_union_map
*sink
= NULL
;
1586 isl_union_map
*schedule
= NULL
;
1587 isl_union_map
*may_dep
;
1588 isl_union_map
*must_dep
;
1590 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1593 sink
= read_map(s
, table
);
1597 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1600 schedule
= read_map(s
, table
);
1604 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1605 schedule
, &must_dep
, &may_dep
,
1609 obj
.type
= isl_obj_union_map
;
1610 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1614 isl_union_map_free(may_source
);
1615 isl_union_map_free(must_source
);
1616 isl_union_map_free(sink
);
1617 isl_union_map_free(schedule
);
1619 obj
.type
= isl_obj_none
;
1624 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1626 struct isl_obj obj
= { isl_obj_none
, NULL
};
1627 isl_union_map
*must_source
= NULL
;
1628 isl_union_map
*may_source
= NULL
;
1629 isl_union_map
*sink
= NULL
;
1630 isl_union_map
*schedule
= NULL
;
1631 isl_union_map
*may_dep
;
1633 may_source
= read_map(s
, table
);
1637 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1638 must_source
= read_map(s
, table
);
1641 return last_any(s
, table
, must_source
, may_source
);
1644 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1647 sink
= read_map(s
, table
);
1651 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1654 schedule
= read_map(s
, table
);
1658 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1659 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1660 schedule
, NULL
, &may_dep
,
1664 obj
.type
= isl_obj_union_map
;
1669 isl_union_map_free(may_source
);
1670 isl_union_map_free(must_source
);
1671 isl_union_map_free(sink
);
1672 isl_union_map_free(schedule
);
1674 obj
.type
= isl_obj_none
;
1679 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1681 struct isl_obj obj
= { isl_obj_none
, NULL
};
1682 struct isl_list
*list
= NULL
;
1683 isl_union_map
*must_source
= NULL
;
1684 isl_union_map
*may_source
= NULL
;
1685 isl_union_map
*sink
= NULL
;
1686 isl_union_map
*schedule
= NULL
;
1687 isl_union_map
*must_dep
;
1688 isl_union_map
*must_no_source
;
1690 must_source
= read_map(s
, table
);
1694 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1695 may_source
= read_map(s
, table
);
1698 return last_any(s
, table
, must_source
, may_source
);
1701 list
= isl_list_alloc(s
->ctx
, 2);
1705 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1708 sink
= read_map(s
, table
);
1712 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1715 schedule
= read_map(s
, table
);
1719 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1720 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1721 schedule
, &must_dep
, NULL
,
1722 &must_no_source
, NULL
) < 0) {
1723 isl_list_free(list
);
1727 list
->obj
[0].type
= isl_obj_union_map
;
1728 list
->obj
[0].v
= must_dep
;
1729 list
->obj
[1].type
= isl_obj_union_map
;
1730 list
->obj
[1].v
= must_no_source
;
1733 obj
.type
= isl_obj_list
;
1737 isl_list_free(list
);
1738 isl_union_map_free(may_source
);
1739 isl_union_map_free(must_source
);
1740 isl_union_map_free(sink
);
1741 isl_union_map_free(schedule
);
1743 obj
.type
= isl_obj_none
;
1748 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1749 struct isl_hash_table
*table
)
1751 isl_union_set
*domain
;
1752 isl_union_map
*validity
;
1753 isl_union_map
*proximity
;
1755 domain
= read_set(s
, table
);
1759 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1760 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1763 isl_union_map
*umap
;
1764 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1765 umap
= read_map(s
, table
);
1766 validity
= isl_union_map_union(validity
, umap
);
1767 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1768 umap
= read_map(s
, table
);
1769 proximity
= isl_union_map_union(proximity
, umap
);
1774 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1777 static struct isl_obj
schedule(struct isl_stream
*s
,
1778 struct isl_hash_table
*table
)
1780 struct isl_obj obj
= { isl_obj_none
, NULL
};
1781 isl_schedule
*schedule
;
1783 schedule
= get_schedule(s
, table
);
1785 obj
.v
= isl_schedule_get_map(schedule
);
1786 obj
.type
= isl_obj_union_map
;
1788 isl_schedule_free(schedule
);
1793 /* Read a schedule for code generation.
1794 * If the input is a set rather than a map, then we construct
1795 * an identity schedule on the given set.
1797 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1798 struct isl_hash_table
*table
)
1802 obj
= read_obj(s
, table
);
1804 if (is_subtype(obj
, isl_obj_union_map
)) {
1805 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1809 if (is_subtype(obj
, isl_obj_union_set
)) {
1810 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1811 return isl_union_set_identity(obj
.v
);
1815 isl_die(s
->ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1818 /* Generate an AST for the given schedule and options and print
1819 * the AST on the printer.
1821 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1822 __isl_take isl_union_map
*schedule
,
1823 __isl_take isl_union_map
*options
)
1827 isl_ast_build
*build
;
1831 space
= isl_union_map_get_space(schedule
);
1832 context
= isl_set_universe(isl_space_params(space
));
1834 build
= isl_ast_build_from_context(context
);
1835 build
= isl_ast_build_set_options(build
, options
);
1836 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1837 isl_ast_build_free(build
);
1842 format
= isl_printer_get_output_format(p
);
1843 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1844 p
= isl_printer_print_ast_node(p
, tree
);
1845 p
= isl_printer_set_output_format(p
, format
);
1847 isl_ast_node_free(tree
);
1852 /* Perform the codegen operation.
1853 * In particular, read a schedule, check if the user has specified any options
1854 * and then generate an AST from the schedule (and options) and print it.
1856 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1857 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1859 isl_union_map
*schedule
;
1860 isl_union_map
*options
;
1862 schedule
= get_codegen_schedule(s
, table
);
1866 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1867 options
= read_map(s
, table
);
1869 options
= isl_union_map_empty(
1870 isl_union_map_get_space(schedule
));
1872 p
= print_code(p
, schedule
, options
);
1874 isl_stream_eat(s
, ';');
1879 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1881 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1883 struct isl_obj obj
= { isl_obj_none
, NULL
};
1884 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1885 struct isl_list
*list
;
1887 list
= isl_list_alloc(ctx
, 2);
1892 obj
.type
= isl_obj_list
;
1894 list
->obj
[0].type
= isl_obj_union_map
;
1895 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1897 if (isl_band_has_children(band
)) {
1898 isl_band_list
*children
;
1900 children
= isl_band_get_children(band
);
1901 list
->obj
[1] = band_list_to_obj_list(children
);
1903 list
->obj
[1].type
= isl_obj_list
;
1904 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1907 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1910 isl_band_free(band
);
1914 isl_band_free(band
);
1916 obj
.type
= isl_obj_none
;
1921 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1923 struct isl_obj obj
= { isl_obj_none
, NULL
};
1924 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1925 struct isl_list
*list
;
1928 n
= isl_band_list_n_band(bands
);
1929 list
= isl_list_alloc(ctx
, n
);
1934 obj
.type
= isl_obj_list
;
1936 for (i
= 0; i
< n
; ++i
) {
1939 band
= isl_band_list_get_band(bands
, i
);
1940 list
->obj
[i
] = band_to_obj_list(band
);
1941 if (!list
->obj
[i
].v
)
1945 isl_band_list_free(bands
);
1949 isl_band_list_free(bands
);
1951 obj
.type
= isl_obj_none
;
1956 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1957 struct isl_hash_table
*table
)
1959 struct isl_obj obj
= { isl_obj_none
, NULL
};
1960 isl_schedule
*schedule
;
1961 isl_band_list
*roots
;
1963 schedule
= get_schedule(s
, table
);
1967 roots
= isl_schedule_get_band_forest(schedule
);
1968 isl_schedule_free(schedule
);
1970 return band_list_to_obj_list(roots
);
1973 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1975 struct isl_token
*tok
;
1978 if (isl_stream_eat_if_available(s
, '+'))
1979 return transitive_closure(s
->ctx
, obj
);
1981 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1982 if (obj
.type
!= isl_obj_union_map
)
1983 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1985 tok
= isl_stream_next_token(s
);
1986 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1987 isl_stream_error(s
, tok
, "expecting integer exponent");
1989 isl_stream_push_token(s
, tok
);
1993 v
= isl_token_get_val(s
->ctx
, tok
);
1994 if (isl_val_is_zero(v
)) {
1995 isl_stream_error(s
, tok
, "expecting non-zero exponent");
1998 isl_stream_push_token(s
, tok
);
2002 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2003 isl_token_free(tok
);
2010 obj
.type
= isl_obj_none
;
2015 static struct isl_obj
check_assert(struct isl_stream
*s
,
2016 struct isl_hash_table
*table
)
2020 obj
= read_expr(s
, table
);
2021 if (obj
.type
!= isl_obj_bool
)
2022 isl_die(s
->ctx
, isl_error_invalid
,
2023 "expecting boolean expression", goto error
);
2024 if (obj
.v
!= &isl_bool_true
)
2025 isl_die(s
->ctx
, isl_error_unknown
,
2026 "assertion failed", abort());
2029 obj
.type
= isl_obj_none
;
2034 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2037 struct isl_token
*tok
;
2038 struct isl_stream
*s_file
;
2039 struct iscc_options
*options
;
2042 tok
= isl_stream_next_token(s
);
2043 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2044 isl_stream_error(s
, tok
, "expecting filename");
2045 isl_token_free(tok
);
2049 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2050 if (!options
|| !options
->io
) {
2051 isl_token_free(tok
);
2052 isl_die(s
->ctx
, isl_error_invalid
,
2053 "read operation not allowed", goto error
);
2056 file
= fopen(tok
->u
.s
, "r");
2057 isl_token_free(tok
);
2058 isl_assert(s
->ctx
, file
, goto error
);
2060 s_file
= isl_stream_new_file(s
->ctx
, file
);
2066 obj
= isl_stream_read_obj(s_file
);
2068 isl_stream_free(s_file
);
2073 obj
.type
= isl_obj_none
;
2078 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2079 struct isl_hash_table
*table
)
2082 struct isl_token
*tok
;
2083 struct isl_stream
*s_file
;
2084 struct iscc_options
*options
;
2088 tok
= isl_stream_next_token(s
);
2089 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2090 isl_stream_error(s
, tok
, "expecting filename");
2091 isl_token_free(tok
);
2095 obj
= read_expr(s
, table
);
2097 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2098 if (!options
|| !options
->io
) {
2099 isl_token_free(tok
);
2100 isl_die(s
->ctx
, isl_error_invalid
,
2101 "write operation not allowed", goto error
);
2104 file
= fopen(tok
->u
.s
, "w");
2105 isl_token_free(tok
);
2107 isl_die(s
->ctx
, isl_error_unknown
,
2108 "could not open file for writing", goto error
);
2110 p
= isl_printer_to_file(s
->ctx
, file
);
2111 p
= isl_printer_set_output_format(p
, options
->format
);
2112 p
= obj
.type
->print(p
, obj
.v
);
2113 p
= isl_printer_end_line(p
);
2114 isl_printer_free(p
);
2119 obj
.type
= isl_obj_none
;
2124 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2126 struct isl_token
*tok
;
2127 struct isl_obj obj
= { isl_obj_none
, NULL
};
2129 tok
= isl_stream_next_token(s
);
2132 if (tok
->type
== ISL_TOKEN_STRING
) {
2134 str
= isl_str_alloc(s
->ctx
);
2137 str
->s
= strdup(tok
->u
.s
);
2138 isl_token_free(tok
);
2140 obj
.type
= isl_obj_str
;
2142 isl_stream_push_token(s
, tok
);
2145 isl_token_free(tok
);
2149 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2151 struct isl_token
*tok
;
2152 struct isl_obj obj
= { isl_obj_none
, NULL
};
2154 tok
= isl_stream_next_token(s
);
2157 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2158 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2159 isl_token_free(tok
);
2160 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2161 obj
.type
= isl_obj_bool
;
2163 isl_stream_push_token(s
, tok
);
2167 static __isl_give
char *read_ident(struct isl_stream
*s
)
2171 struct isl_token
*tok
, *tok2
;
2173 name
= isl_stream_read_ident_if_available(s
);
2177 tok
= isl_stream_next_token(s
);
2180 if (tok
->type
!= '$') {
2181 isl_stream_push_token(s
, tok
);
2184 tok2
= isl_stream_next_token(s
);
2185 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2187 isl_stream_push_token(s
, tok2
);
2188 isl_stream_push_token(s
, tok
);
2192 v
= isl_token_get_val(s
->ctx
, tok2
);
2193 name
= isl_val_to_str(v
);
2195 isl_token_free(tok
);
2196 isl_token_free(tok2
);
2201 static struct isl_obj
read_list(struct isl_stream
*s
,
2202 struct isl_hash_table
*table
, struct isl_obj obj
)
2204 struct isl_list
*list
;
2206 list
= isl_list_alloc(s
->ctx
, 2);
2210 list
->obj
[1] = read_obj(s
, table
);
2212 obj
.type
= isl_obj_list
;
2214 if (!list
->obj
[1].v
)
2217 while (isl_stream_eat_if_available(s
, ',')) {
2218 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2226 obj
.type
= isl_obj_none
;
2231 static struct isl_obj
read_obj(struct isl_stream
*s
,
2232 struct isl_hash_table
*table
)
2234 struct isl_obj obj
= { isl_obj_none
, NULL
};
2236 struct isc_un_op
*op
= NULL
;
2238 obj
= read_string_if_available(s
);
2241 obj
= read_bool_if_available(s
);
2244 if (isl_stream_eat_if_available(s
, '(')) {
2245 if (isl_stream_next_token_is(s
, ')')) {
2246 obj
.type
= isl_obj_list
;
2247 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2249 obj
= read_expr(s
, table
);
2250 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2251 obj
= read_list(s
, table
, obj
);
2253 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2256 op
= read_prefix_un_op_if_available(s
);
2258 return read_un_op_expr(s
, table
, op
);
2260 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2261 return check_assert(s
, table
);
2262 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2263 return read_from_file(s
);
2264 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2265 return write_to_file(s
, table
);
2266 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2267 return vertices(s
, table
);
2268 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2269 return any(s
, table
);
2270 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2271 return last(s
, table
);
2272 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2273 return schedule(s
, table
);
2274 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2275 return schedule_forest(s
, table
);
2276 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2277 return type_of(s
, table
);
2279 name
= read_ident(s
);
2281 obj
= stored_obj(s
->ctx
, table
, name
);
2283 obj
= isl_stream_read_obj(s
);
2288 if (isl_stream_eat_if_available(s
, '^'))
2289 obj
= power(s
, obj
);
2290 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2291 obj
= obj_at_index(s
, obj
);
2292 else if (is_subtype(obj
, isl_obj_union_map
) &&
2293 isl_stream_eat_if_available(s
, '(')) {
2294 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2295 obj
= apply(s
, obj
.v
, table
);
2296 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2297 isl_stream_eat_if_available(s
, '(')) {
2298 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2299 obj
= apply_fun(s
, obj
, table
);
2300 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2301 isl_stream_eat_if_available(s
, '(')) {
2302 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2303 obj
= apply_fun(s
, obj
, table
);
2309 obj
.type
= isl_obj_none
;
2314 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2315 struct isl_obj lhs
, struct isl_obj rhs
)
2319 for (i
= 0; ; ++i
) {
2322 if (bin_ops
[i
].op
!= like
->op
)
2324 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2326 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2332 for (i
= 0; ; ++i
) {
2333 if (!named_bin_ops
[i
].name
)
2335 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2337 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2339 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2342 return &named_bin_ops
[i
].op
;
2348 static int next_is_neg_int(struct isl_stream
*s
)
2350 struct isl_token
*tok
;
2353 tok
= isl_stream_next_token(s
);
2354 if (tok
&& tok
->type
== ISL_TOKEN_VALUE
) {
2356 v
= isl_token_get_val(s
->ctx
, tok
);
2357 ret
= isl_val_is_neg(v
);
2361 isl_stream_push_token(s
, tok
);
2366 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2367 struct isl_obj lhs
, struct isl_obj rhs
)
2371 lhs
= convert(ctx
, lhs
, op
->lhs
);
2372 rhs
= convert(ctx
, rhs
, op
->rhs
);
2373 if (op
->res
!= isl_obj_bool
)
2374 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2376 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2379 obj
.v
= isl_bool_from_int(res
);
2386 static struct isl_obj
read_expr(struct isl_stream
*s
,
2387 struct isl_hash_table
*table
)
2389 struct isl_obj obj
= { isl_obj_none
, NULL
};
2390 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2392 obj
= read_obj(s
, table
);
2394 struct isc_bin_op
*op
= NULL
;
2396 op
= read_bin_op_if_available(s
, obj
);
2400 right_obj
= read_obj(s
, table
);
2402 op
= find_matching_bin_op(op
, obj
, right_obj
);
2405 isl_die(s
->ctx
, isl_error_invalid
,
2406 "no such binary operator defined on given operands",
2409 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2412 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2413 right_obj
= read_obj(s
, table
);
2414 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2419 free_obj(right_obj
);
2421 obj
.type
= isl_obj_none
;
2426 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2427 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2429 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2430 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2432 struct isl_obj obj
= { isl_obj_none
, NULL
};
2436 struct isc_bin_op
*op
= NULL
;
2441 if (isl_stream_is_empty(s
))
2444 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2445 return source_file(s
, table
, p
);
2446 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2447 return codegen(s
, table
, p
);
2449 assign
= is_assign(s
);
2451 lhs
= isl_stream_read_ident_if_available(s
);
2452 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2454 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2459 obj
= read_expr(s
, table
);
2460 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2461 fprintf(stderr
, "Interrupted\n");
2462 isl_ctx_reset_error(s
->ctx
);
2464 if (isl_stream_eat(s
, ';'))
2468 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2469 p
= obj
.type
->print(p
, obj
.v
);
2470 p
= isl_printer_end_line(p
);
2475 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2476 static int count
= 0;
2477 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2478 lhs
= strdup(buf
+ 1);
2480 p
= isl_printer_print_str(p
, buf
);
2481 p
= isl_printer_print_str(p
, " := ");
2482 p
= obj
.type
->print(p
, obj
.v
);
2483 p
= isl_printer_end_line(p
);
2485 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2490 isl_stream_flush_tokens(s
);
2491 isl_stream_skip_line(s
);
2497 int free_cb(void **entry
, void *user
)
2499 struct isl_named_obj
*named
= *entry
;
2501 free_obj(named
->obj
);
2508 static void register_named_ops(struct isl_stream
*s
)
2512 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2513 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2514 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2517 for (i
= 0; ; ++i
) {
2518 if (!named_un_ops
[i
].name
)
2520 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2521 named_un_ops
[i
].name
);
2522 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2525 for (i
= 0; ; ++i
) {
2526 if (!named_bin_ops
[i
].name
)
2528 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2529 named_bin_ops
[i
].name
);
2530 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2534 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2535 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2537 struct isl_token
*tok
;
2538 struct isl_stream
*s_file
;
2541 tok
= isl_stream_next_token(s
);
2542 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2543 isl_stream_error(s
, tok
, "expecting filename");
2544 isl_token_free(tok
);
2548 file
= fopen(tok
->u
.s
, "r");
2549 isl_token_free(tok
);
2550 isl_assert(s
->ctx
, file
, return p
);
2552 s_file
= isl_stream_new_file(s
->ctx
, file
);
2558 register_named_ops(s_file
);
2560 while (!s_file
->eof
)
2561 p
= read_line(s_file
, table
, p
, 0);
2563 isl_stream_free(s_file
);
2566 isl_stream_eat(s
, ';');
2571 int main(int argc
, char **argv
)
2573 struct isl_ctx
*ctx
;
2574 struct isl_stream
*s
;
2575 struct isl_hash_table
*table
;
2576 struct iscc_options
*options
;
2578 int tty
= isatty(0);
2580 options
= iscc_options_new_with_defaults();
2583 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2584 pet_options_set_autodetect(ctx
, 1);
2585 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2586 s
= isl_stream_new_file(ctx
, stdin
);
2588 table
= isl_hash_table_alloc(ctx
, 10);
2590 p
= isl_printer_to_file(ctx
, stdout
);
2591 p
= isl_printer_set_output_format(p
, options
->format
);
2594 register_named_ops(s
);
2596 install_signal_handler(ctx
);
2598 while (p
&& !s
->eof
) {
2599 isl_ctx_resume(ctx
);
2600 p
= read_line(s
, table
, p
, tty
);
2603 remove_signal_handler(ctx
);
2605 isl_printer_free(p
);
2606 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2607 isl_hash_table_free(ctx
, table
);