7 #include <isl/stream.h>
10 #include <isl/vertices.h>
13 #include <isl/schedule.h>
14 #include <isl_obj_list.h>
15 #include <isl_obj_str.h>
16 #include <barvinok/isl.h>
17 #include <barvinok/options.h>
18 #include "lattice_width.h"
25 static isl_ctx
*main_ctx
;
27 static void handler(int signum
)
29 if (isl_ctx_aborted(main_ctx
))
31 isl_ctx_abort(main_ctx
);
34 static struct sigaction sa_old
;
36 static void install_signal_handler(isl_ctx
*ctx
)
42 memset(&sa
, 0, sizeof(struct sigaction
));
43 sa
.sa_handler
= &handler
;
44 sa
.sa_flags
= SA_RESTART
;
45 sigaction(SIGINT
, &sa
, &sa_old
);
48 static void remove_signal_handler(isl_ctx
*ctx
)
50 sigaction(SIGINT
, &sa_old
, NULL
);
55 static void install_signal_handler(isl_ctx
*ctx
)
59 static void remove_signal_handler(isl_ctx
*ctx
)
66 #include <cloog/isl/cloog.h>
73 int pet_options_set_autodetect(isl_ctx
*ctx
, int val
)
79 static int isl_bool_false
= 0;
80 static int isl_bool_true
= 1;
81 static int isl_bool_error
= -1;
83 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
84 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
85 ISCC_SCHEDULE
, ISCC_SCHEDULE_FOREST
,
86 ISCC_MINIMIZING
, ISCC_RESPECTING
,
87 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
89 static const char *op_name
[ISCC_N_OP
] = {
90 [ISCC_ASSERT
] = "assert",
92 [ISCC_WRITE
] = "write",
93 [ISCC_PRINT
] = "print",
94 [ISCC_SOURCE
] = "source",
95 [ISCC_VERTICES
] = "vertices",
98 [ISCC_BEFORE
] = "before",
99 [ISCC_UNDER
] = "under",
100 [ISCC_SCHEDULE
] = "schedule",
101 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
102 [ISCC_MINIMIZING
] = "minimizing",
103 [ISCC_RESPECTING
] = "respecting",
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 void *map_codegen(void *arg
)
706 isl_union_map
*umap
= (isl_union_map
*)arg
;
707 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
709 CloogOptions
*options
;
710 CloogDomain
*context
;
711 CloogUnionDomain
*ud
;
713 struct clast_stmt
*stmt
;
715 state
= cloog_isl_state_malloc(ctx
);
716 options
= cloog_options_malloc(state
);
717 options
->language
= CLOOG_LANGUAGE_C
;
718 options
->strides
= 1;
721 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
723 dim
= isl_union_map_get_space(umap
);
724 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
726 input
= cloog_input_alloc(context
, ud
);
728 stmt
= cloog_clast_create_from_input(input
, options
);
729 clast_pprint(stdout
, stmt
, 0, options
);
730 cloog_clast_free(stmt
);
733 cloog_options_free(options
);
734 cloog_state_free(state
);
735 isl_union_map_free(umap
);
739 void *set_codegen(void *arg
)
742 isl_union_set
*uset
= (isl_union_set
*)arg
;
744 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
746 CloogOptions
*options
;
747 CloogDomain
*context
;
748 CloogUnionDomain
*ud
;
750 struct clast_stmt
*stmt
;
752 if (isl_union_set_n_set(uset
) > 1)
753 isl_die(ctx
, isl_error_invalid
,
754 "code generation for more than one domain "
755 "requires a schedule", goto error
);
757 state
= cloog_isl_state_malloc(ctx
);
758 options
= cloog_options_malloc(state
);
759 options
->language
= CLOOG_LANGUAGE_C
;
760 options
->strides
= 1;
763 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
764 ud
= cloog_union_domain_from_isl_set(set
);
766 dim
= isl_union_set_get_space(uset
);
767 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
769 input
= cloog_input_alloc(context
, ud
);
771 stmt
= cloog_clast_create_from_input(input
, options
);
772 clast_pprint(stdout
, stmt
, 0, options
);
773 cloog_clast_free(stmt
);
775 cloog_options_free(options
);
776 cloog_state_free(state
);
778 isl_union_set_free(uset
);
784 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
787 struct isl_list
*list
;
788 struct pet_scop
*scop
;
789 isl_union_map
*sched
, *reads
, *writes
;
790 isl_union_set
*domain
;
791 struct iscc_options
*options
;
797 options
= isl_ctx_peek_iscc_options(ctx
);
798 if (!options
|| !options
->io
) {
800 isl_die(ctx
, isl_error_invalid
,
801 "parse_file operation not allowed", return NULL
);
804 list
= isl_list_alloc(ctx
, 4);
808 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
809 domain
= pet_scop_collect_domains(scop
);
810 sched
= pet_scop_collect_schedule(scop
);
811 reads
= pet_scop_collect_reads(scop
);
812 writes
= pet_scop_collect_writes(scop
);
815 list
->obj
[0].type
= isl_obj_union_set
;
816 list
->obj
[0].v
= domain
;
817 list
->obj
[1].type
= isl_obj_union_map
;
818 list
->obj
[1].v
= writes
;
819 list
->obj
[2].type
= isl_obj_union_map
;
820 list
->obj
[2].v
= reads
;
821 list
->obj
[3].type
= isl_obj_union_map
;
822 list
->obj
[3].v
= sched
;
824 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
825 !list
->obj
[2].v
|| !list
->obj
[3].v
)
837 static int add_point(__isl_take isl_point
*pnt
, void *user
)
839 isl_union_set
**scan
= (isl_union_set
**) user
;
841 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
846 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
850 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
852 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
853 isl_union_set_free(scan
);
857 isl_union_set_free(uset
);
861 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
863 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
866 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
867 __isl_take isl_union_pw_qpolynomial
*upwqp
)
869 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
872 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
873 __isl_take isl_union_pw_qpolynomial
*upwqp
)
875 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
878 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
879 __isl_take isl_union_pw_qpolynomial
*upwqp
)
881 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
884 typedef void *(*isc_un_op_fn
)(void *arg
);
886 enum isl_token_type op
;
891 struct isc_named_un_op
{
895 struct isc_named_un_op named_un_ops
[] = {
896 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
897 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
898 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
899 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
900 {"card", { -1, isl_obj_union_set
,
901 isl_obj_union_pw_qpolynomial
,
902 (isc_un_op_fn
) &isl_union_set_card
} },
903 {"card", { -1, isl_obj_union_map
,
904 isl_obj_union_pw_qpolynomial
,
905 (isc_un_op_fn
) &isl_union_map_card
} },
906 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
907 (isc_un_op_fn
) &isl_union_set_coalesce
} },
908 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
909 (isc_un_op_fn
) &isl_union_map_coalesce
} },
910 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
911 isl_obj_union_pw_qpolynomial
,
912 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
913 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
914 isl_obj_union_pw_qpolynomial_fold
,
915 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
917 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
919 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
922 {"coefficients", { -1, isl_obj_union_set
,
924 (isc_un_op_fn
) &isl_union_set_coefficients
} },
925 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
926 (isc_un_op_fn
) &isl_union_set_solutions
} },
927 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
928 (isc_un_op_fn
) &isl_union_map_deltas
} },
929 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
930 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
931 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
932 (isc_un_op_fn
) &isl_union_map_domain
} },
933 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
934 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
935 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
937 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
938 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
939 (isc_un_op_fn
) &isl_union_map_domain
} },
940 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
942 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
943 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
945 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
946 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
947 (isc_un_op_fn
) &isl_union_map_domain_map
} },
948 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
949 (isc_un_op_fn
) &isl_union_map_range
} },
950 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
951 (isc_un_op_fn
) &isl_union_map_range
} },
952 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
953 (isc_un_op_fn
) &isl_union_map_range_map
} },
954 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
955 (isc_un_op_fn
) &isl_union_set_identity
} },
956 {"lattice_width", { -1, isl_obj_union_set
,
957 isl_obj_union_pw_qpolynomial
,
958 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
959 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
960 (isc_un_op_fn
) &isl_union_map_lexmin
} },
961 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
962 (isc_un_op_fn
) &isl_union_map_lexmax
} },
963 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
964 (isc_un_op_fn
) &isl_union_set_lexmin
} },
965 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
966 (isc_un_op_fn
) &isl_union_set_lexmax
} },
967 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
968 (isc_un_op_fn
) &isl_union_set_lift
} },
969 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
970 (isc_un_op_fn
) &isl_union_map_params
} },
971 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
972 (isc_un_op_fn
) &isl_union_set_params
} },
973 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
974 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
975 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
976 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
977 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
978 isl_obj_union_pw_qpolynomial
,
979 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
980 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
981 isl_obj_union_pw_qpolynomial
,
982 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
983 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
984 isl_obj_union_pw_qpolynomial
,
985 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
987 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
988 (isc_un_op_fn
) &parse
} },
990 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
991 (isc_un_op_fn
) &union_map_power
} },
992 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
993 (isc_un_op_fn
) &union_set_sample
} },
994 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
995 (isc_un_op_fn
) &union_map_sample
} },
996 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
997 (isc_un_op_fn
) &union_set_scan
} },
998 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
999 (isc_un_op_fn
) &union_map_scan
} },
1000 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
1001 isl_obj_union_pw_qpolynomial
,
1002 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
1003 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
1004 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
1005 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
1006 (isc_un_op_fn
) &isl_union_set_unwrap
} },
1007 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
1008 (isc_un_op_fn
) &isl_union_map_wrap
} },
1009 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
1010 (isc_un_op_fn
) &isl_union_map_zip
} },
1014 struct isl_named_obj
{
1019 static void free_obj(struct isl_obj obj
)
1021 obj
.type
->free(obj
.v
);
1024 static int same_name(const void *entry
, const void *val
)
1026 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1028 return !strcmp(named
->name
, val
);
1031 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1032 char *name
, struct isl_obj obj
)
1034 struct isl_hash_table_entry
*entry
;
1036 struct isl_named_obj
*named
;
1038 name_hash
= isl_hash_string(isl_hash_init(), name
);
1039 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1043 named
= entry
->data
;
1044 free_obj(named
->obj
);
1047 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1051 entry
->data
= named
;
1062 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1063 struct isl_hash_table
*table
, char *name
)
1065 struct isl_obj obj
= { isl_obj_none
, NULL
};
1066 struct isl_hash_table_entry
*entry
;
1069 name_hash
= isl_hash_string(isl_hash_init(), name
);
1070 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1072 struct isl_named_obj
*named
;
1073 named
= entry
->data
;
1075 } else if (isdigit(name
[0]))
1076 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1078 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1081 obj
.v
= obj
.type
->copy(obj
.v
);
1085 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1087 if (obj
.type
== super
)
1089 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1091 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1093 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1094 super
== isl_obj_union_pw_qpolynomial
)
1096 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1097 super
== isl_obj_union_pw_qpolynomial_fold
)
1099 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1101 if (obj
.type
== isl_obj_list
) {
1102 struct isl_list
*list
= obj
.v
;
1103 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1104 return is_subtype(list
->obj
[0], super
);
1106 if (super
== isl_obj_str
)
1111 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1113 struct isl_list
*list
= obj
.v
;
1116 obj
.v
= obj
.type
->copy(obj
.v
);
1118 isl_list_free(list
);
1123 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1126 if (obj
.type
== type
)
1128 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1129 obj
.type
= isl_obj_union_map
;
1130 obj
.v
= isl_union_map_from_map(obj
.v
);
1133 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1134 obj
.type
= isl_obj_union_set
;
1135 obj
.v
= isl_union_set_from_set(obj
.v
);
1138 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1139 type
== isl_obj_union_pw_qpolynomial
) {
1140 obj
.type
= isl_obj_union_pw_qpolynomial
;
1141 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1144 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1145 type
== isl_obj_union_pw_qpolynomial_fold
) {
1146 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1147 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1150 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1151 if (type
== isl_obj_union_map
) {
1152 obj
.type
= isl_obj_union_map
;
1155 if (type
== isl_obj_union_pw_qpolynomial
) {
1156 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1157 isl_union_set_free(obj
.v
);
1158 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1159 obj
.type
= isl_obj_union_pw_qpolynomial
;
1162 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1163 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1164 isl_union_set_free(obj
.v
);
1165 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1167 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1171 if (obj
.type
== isl_obj_list
) {
1172 struct isl_list
*list
= obj
.v
;
1173 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1174 return convert(ctx
, obj_at(obj
, 0), type
);
1176 if (type
== isl_obj_str
) {
1181 p
= isl_printer_to_str(ctx
);
1184 p
= obj
.type
->print(p
, obj
.v
);
1185 s
= isl_printer_get_str(p
);
1186 isl_printer_free(p
);
1188 str
= isl_str_from_string(ctx
, s
);
1193 obj
.type
= isl_obj_str
;
1199 obj
.type
= isl_obj_none
;
1204 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1208 struct isl_token
*tok
;
1210 tok
= isl_stream_next_token(s
);
1214 for (i
= 0; ; ++i
) {
1217 if (bin_ops
[i
].op
!= tok
->type
)
1219 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1222 isl_token_free(tok
);
1226 for (i
= 0; ; ++i
) {
1227 if (!named_bin_ops
[i
].name
)
1229 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1231 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1234 isl_token_free(tok
);
1235 return &named_bin_ops
[i
].op
;
1238 isl_stream_push_token(s
, tok
);
1243 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1246 struct isl_token
*tok
;
1248 tok
= isl_stream_next_token(s
);
1252 for (i
= 0; ; ++i
) {
1253 if (!named_un_ops
[i
].name
)
1255 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1258 isl_token_free(tok
);
1259 return &named_un_ops
[i
].op
;
1262 isl_stream_push_token(s
, tok
);
1267 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1272 for (i
= 0; ; ++i
) {
1273 if (!named_un_ops
[i
].name
)
1275 if (named_un_ops
[i
].op
.op
!= like
->op
)
1277 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1280 return &named_un_ops
[i
].op
;
1286 static int is_assign(struct isl_stream
*s
)
1288 struct isl_token
*tok
;
1289 struct isl_token
*tok2
;
1292 tok
= isl_stream_next_token(s
);
1295 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1296 isl_stream_push_token(s
, tok
);
1300 tok2
= isl_stream_next_token(s
);
1302 isl_stream_push_token(s
, tok
);
1305 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1306 isl_stream_push_token(s
, tok2
);
1307 isl_stream_push_token(s
, tok
);
1312 static struct isl_obj
read_obj(struct isl_stream
*s
,
1313 struct isl_hash_table
*table
);
1314 static struct isl_obj
read_expr(struct isl_stream
*s
,
1315 struct isl_hash_table
*table
);
1317 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1318 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1320 struct isl_obj obj
= { isl_obj_none
, NULL
};
1322 obj
= read_obj(s
, table
);
1326 op
= find_matching_un_op(op
, obj
);
1329 isl_die(s
->ctx
, isl_error_invalid
,
1330 "no such unary operator defined on given operand",
1333 obj
= convert(s
->ctx
, obj
, op
->arg
);
1334 obj
.v
= op
->fn(obj
.v
);
1340 obj
.type
= isl_obj_none
;
1345 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1347 struct isl_list
*list
;
1350 if (obj
.type
!= isl_obj_union_map
)
1351 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1352 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1353 list
= isl_list_alloc(ctx
, 2);
1357 list
->obj
[0].type
= isl_obj_union_map
;
1358 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1359 list
->obj
[1].type
= isl_obj_bool
;
1360 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1362 obj
.type
= isl_obj_list
;
1363 if (exact
< 0 || !list
->obj
[0].v
)
1369 obj
.type
= isl_obj_none
;
1374 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1376 struct isl_list
*list
= obj
.v
;
1377 struct isl_token
*tok
;
1380 tok
= isl_stream_next_token(s
);
1381 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1382 isl_stream_error(s
, tok
, "expecting index");
1384 isl_stream_push_token(s
, tok
);
1387 i
= isl_int_get_si(tok
->u
.v
);
1388 isl_token_free(tok
);
1389 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1390 if (isl_stream_eat(s
, ']'))
1393 return obj_at(obj
, i
);
1396 obj
.type
= isl_obj_none
;
1401 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1402 struct isl_hash_table
*table
)
1406 obj
= read_expr(s
, table
);
1407 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1408 is_subtype(obj
, isl_obj_union_map
), goto error
);
1410 if (obj
.type
== isl_obj_list
) {
1411 struct isl_list
*list
= obj
.v
;
1412 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1413 obj
= obj_at(obj
, 0);
1415 if (obj
.type
== isl_obj_set
)
1416 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1417 else if (obj
.type
== isl_obj_map
)
1418 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1419 if (obj
.type
== isl_obj_union_set
) {
1420 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1422 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1426 if (isl_stream_eat(s
, ')'))
1431 isl_union_map_free(umap
);
1434 obj
.type
= isl_obj_none
;
1439 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1440 __isl_take isl_union_set
*uset
)
1442 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1443 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1445 obj
.type
= isl_obj_list
;
1446 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1451 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1452 __isl_take isl_union_map
*umap
)
1454 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1455 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1457 obj
.type
= isl_obj_list
;
1458 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1463 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1464 struct isl_obj obj
, struct isl_hash_table
*table
)
1468 arg
= read_expr(s
, table
);
1469 if (!is_subtype(arg
, isl_obj_union_map
) &&
1470 !is_subtype(arg
, isl_obj_union_set
))
1471 isl_die(s
->ctx
, isl_error_invalid
,
1472 "expecting set of map argument", goto error
);
1474 if (arg
.type
== isl_obj_list
) {
1475 struct isl_list
*list
= arg
.v
;
1476 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1477 arg
= obj_at(arg
, 0);
1479 if (arg
.type
== isl_obj_set
)
1480 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1481 else if (arg
.type
== isl_obj_map
)
1482 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1483 if (arg
.type
== isl_obj_union_set
)
1484 obj
= apply_fun_set(obj
, arg
.v
);
1486 obj
= apply_fun_map(obj
, arg
.v
);
1490 if (isl_stream_eat(s
, ')'))
1498 obj
.type
= isl_obj_none
;
1503 struct add_vertex_data
{
1504 struct isl_list
*list
;
1508 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1510 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1511 isl_basic_set
*expr
;
1513 expr
= isl_vertex_get_expr(vertex
);
1515 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1516 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1519 isl_vertex_free(vertex
);
1524 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1527 isl_basic_set
*hull
;
1528 isl_vertices
*vertices
= NULL
;
1529 struct isl_list
*list
= NULL
;
1531 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1533 set
= isl_set_remove_divs(set
);
1534 hull
= isl_set_convex_hull(set
);
1535 vertices
= isl_basic_set_compute_vertices(hull
);
1536 isl_basic_set_free(hull
);
1540 ctx
= isl_vertices_get_ctx(vertices
);
1541 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1546 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1548 data
->list
= isl_list_concat(list
, data
->list
);
1550 isl_vertices_free(vertices
);
1555 isl_vertices_free(vertices
);
1559 static struct isl_obj
vertices(struct isl_stream
*s
,
1560 struct isl_hash_table
*table
)
1564 struct isl_list
*list
= NULL
;
1565 isl_union_set
*uset
;
1566 struct add_vertex_data data
= { NULL
};
1568 obj
= read_expr(s
, table
);
1569 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1570 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1574 ctx
= isl_union_set_get_ctx(uset
);
1575 list
= isl_list_alloc(ctx
, 0);
1581 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1584 isl_union_set_free(uset
);
1586 obj
.type
= isl_obj_list
;
1591 isl_union_set_free(uset
);
1592 isl_list_free(data
.list
);
1594 obj
.type
= isl_obj_none
;
1599 static struct isl_obj
type_of(struct isl_stream
*s
,
1600 struct isl_hash_table
*table
)
1604 const char *type
= "unknown";
1606 obj
= read_expr(s
, table
);
1608 if (obj
.type
== isl_obj_map
||
1609 obj
.type
== isl_obj_union_map
)
1611 if (obj
.type
== isl_obj_set
||
1612 obj
.type
== isl_obj_union_set
)
1614 if (obj
.type
== isl_obj_pw_qpolynomial
||
1615 obj
.type
== isl_obj_union_pw_qpolynomial
)
1616 type
= "piecewise quasipolynomial";
1617 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1618 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1619 type
= "piecewise quasipolynomial fold";
1620 if (obj
.type
== isl_obj_list
)
1622 if (obj
.type
== isl_obj_bool
)
1624 if (obj
.type
== isl_obj_str
)
1626 if (obj
.type
== isl_obj_int
)
1630 obj
.type
= isl_obj_str
;
1631 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1636 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1637 struct isl_hash_table
*table
)
1641 obj
= read_obj(s
, table
);
1642 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1643 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1650 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1651 struct isl_hash_table
*table
)
1655 obj
= read_obj(s
, table
);
1656 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1657 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1664 static struct isl_obj
last_any(struct isl_stream
*s
,
1665 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1666 __isl_take isl_union_map
*may_source
)
1668 struct isl_obj obj
= { isl_obj_none
, NULL
};
1669 isl_union_map
*sink
= NULL
;
1670 isl_union_map
*schedule
= NULL
;
1671 isl_union_map
*may_dep
;
1672 isl_union_map
*must_dep
;
1674 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1677 sink
= read_map(s
, table
);
1681 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1684 schedule
= read_map(s
, table
);
1688 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1689 schedule
, &must_dep
, &may_dep
,
1693 obj
.type
= isl_obj_union_map
;
1694 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1698 isl_union_map_free(may_source
);
1699 isl_union_map_free(must_source
);
1700 isl_union_map_free(sink
);
1701 isl_union_map_free(schedule
);
1703 obj
.type
= isl_obj_none
;
1708 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1710 struct isl_obj obj
= { isl_obj_none
, NULL
};
1711 isl_union_map
*must_source
= NULL
;
1712 isl_union_map
*may_source
= NULL
;
1713 isl_union_map
*sink
= NULL
;
1714 isl_union_map
*schedule
= NULL
;
1715 isl_union_map
*may_dep
;
1717 may_source
= read_map(s
, table
);
1721 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1722 must_source
= read_map(s
, table
);
1725 return last_any(s
, table
, must_source
, may_source
);
1728 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1731 sink
= read_map(s
, table
);
1735 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1738 schedule
= read_map(s
, table
);
1742 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1743 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1744 schedule
, NULL
, &may_dep
,
1748 obj
.type
= isl_obj_union_map
;
1753 isl_union_map_free(may_source
);
1754 isl_union_map_free(must_source
);
1755 isl_union_map_free(sink
);
1756 isl_union_map_free(schedule
);
1758 obj
.type
= isl_obj_none
;
1763 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1765 struct isl_obj obj
= { isl_obj_none
, NULL
};
1766 struct isl_list
*list
= NULL
;
1767 isl_union_map
*must_source
= NULL
;
1768 isl_union_map
*may_source
= NULL
;
1769 isl_union_map
*sink
= NULL
;
1770 isl_union_map
*schedule
= NULL
;
1771 isl_union_map
*must_dep
;
1772 isl_union_map
*must_no_source
;
1774 must_source
= read_map(s
, table
);
1778 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1779 may_source
= read_map(s
, table
);
1782 return last_any(s
, table
, must_source
, may_source
);
1785 list
= isl_list_alloc(s
->ctx
, 2);
1789 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1792 sink
= read_map(s
, table
);
1796 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1799 schedule
= read_map(s
, table
);
1803 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1804 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1805 schedule
, &must_dep
, NULL
,
1806 &must_no_source
, NULL
) < 0) {
1807 isl_list_free(list
);
1811 list
->obj
[0].type
= isl_obj_union_map
;
1812 list
->obj
[0].v
= must_dep
;
1813 list
->obj
[1].type
= isl_obj_union_map
;
1814 list
->obj
[1].v
= must_no_source
;
1817 obj
.type
= isl_obj_list
;
1821 isl_list_free(list
);
1822 isl_union_map_free(may_source
);
1823 isl_union_map_free(must_source
);
1824 isl_union_map_free(sink
);
1825 isl_union_map_free(schedule
);
1827 obj
.type
= isl_obj_none
;
1832 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1833 struct isl_hash_table
*table
)
1835 isl_union_set
*domain
;
1836 isl_union_map
*validity
;
1837 isl_union_map
*proximity
;
1839 domain
= read_set(s
, table
);
1843 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1844 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1847 isl_union_map
*umap
;
1848 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1849 umap
= read_map(s
, table
);
1850 validity
= isl_union_map_union(validity
, umap
);
1851 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1852 umap
= read_map(s
, table
);
1853 proximity
= isl_union_map_union(proximity
, umap
);
1858 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1861 static struct isl_obj
schedule(struct isl_stream
*s
,
1862 struct isl_hash_table
*table
)
1864 struct isl_obj obj
= { isl_obj_none
, NULL
};
1865 isl_schedule
*schedule
;
1867 schedule
= get_schedule(s
, table
);
1869 obj
.v
= isl_schedule_get_map(schedule
);
1870 obj
.type
= isl_obj_union_map
;
1872 isl_schedule_free(schedule
);
1877 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1879 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1881 struct isl_obj obj
= { isl_obj_none
, NULL
};
1882 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1883 struct isl_list
*list
;
1885 list
= isl_list_alloc(ctx
, 2);
1890 obj
.type
= isl_obj_list
;
1892 list
->obj
[0].type
= isl_obj_union_map
;
1893 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1895 if (isl_band_has_children(band
)) {
1896 isl_band_list
*children
;
1898 children
= isl_band_get_children(band
);
1899 list
->obj
[1] = band_list_to_obj_list(children
);
1901 list
->obj
[1].type
= isl_obj_list
;
1902 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1905 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1908 isl_band_free(band
);
1912 isl_band_free(band
);
1914 obj
.type
= isl_obj_none
;
1919 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1921 struct isl_obj obj
= { isl_obj_none
, NULL
};
1922 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1923 struct isl_list
*list
;
1926 n
= isl_band_list_n_band(bands
);
1927 list
= isl_list_alloc(ctx
, n
);
1932 obj
.type
= isl_obj_list
;
1934 for (i
= 0; i
< n
; ++i
) {
1937 band
= isl_band_list_get_band(bands
, i
);
1938 list
->obj
[i
] = band_to_obj_list(band
);
1939 if (!list
->obj
[i
].v
)
1943 isl_band_list_free(bands
);
1947 isl_band_list_free(bands
);
1949 obj
.type
= isl_obj_none
;
1954 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1955 struct isl_hash_table
*table
)
1957 struct isl_obj obj
= { isl_obj_none
, NULL
};
1958 isl_schedule
*schedule
;
1959 isl_band_list
*roots
;
1961 schedule
= get_schedule(s
, table
);
1965 roots
= isl_schedule_get_band_forest(schedule
);
1966 isl_schedule_free(schedule
);
1968 return band_list_to_obj_list(roots
);
1971 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1973 struct isl_token
*tok
;
1975 if (isl_stream_eat_if_available(s
, '+'))
1976 return transitive_closure(s
->ctx
, obj
);
1978 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1979 if (obj
.type
!= isl_obj_union_map
)
1980 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1982 tok
= isl_stream_next_token(s
);
1983 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_is_zero(tok
->u
.v
)) {
1984 isl_stream_error(s
, tok
, "expecting non-zero integer exponent");
1986 isl_stream_push_token(s
, tok
);
1990 obj
.v
= isl_union_map_fixed_power(obj
.v
, tok
->u
.v
);
1991 isl_token_free(tok
);
1998 obj
.type
= isl_obj_none
;
2003 static struct isl_obj
check_assert(struct isl_stream
*s
,
2004 struct isl_hash_table
*table
)
2008 obj
= read_expr(s
, table
);
2009 if (obj
.type
!= isl_obj_bool
)
2010 isl_die(s
->ctx
, isl_error_invalid
,
2011 "expecting boolean expression", goto error
);
2012 if (obj
.v
!= &isl_bool_true
)
2013 isl_die(s
->ctx
, isl_error_unknown
,
2014 "assertion failed", abort());
2017 obj
.type
= isl_obj_none
;
2022 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2025 struct isl_token
*tok
;
2026 struct isl_stream
*s_file
;
2027 struct iscc_options
*options
;
2030 tok
= isl_stream_next_token(s
);
2031 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2032 isl_stream_error(s
, tok
, "expecting filename");
2033 isl_token_free(tok
);
2037 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2038 if (!options
|| !options
->io
) {
2039 isl_token_free(tok
);
2040 isl_die(s
->ctx
, isl_error_invalid
,
2041 "read operation not allowed", goto error
);
2044 file
= fopen(tok
->u
.s
, "r");
2045 isl_token_free(tok
);
2046 isl_assert(s
->ctx
, file
, goto error
);
2048 s_file
= isl_stream_new_file(s
->ctx
, file
);
2054 obj
= isl_stream_read_obj(s_file
);
2056 isl_stream_free(s_file
);
2061 obj
.type
= isl_obj_none
;
2066 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2067 struct isl_hash_table
*table
)
2070 struct isl_token
*tok
;
2071 struct isl_stream
*s_file
;
2072 struct iscc_options
*options
;
2076 tok
= isl_stream_next_token(s
);
2077 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2078 isl_stream_error(s
, tok
, "expecting filename");
2079 isl_token_free(tok
);
2083 obj
= read_expr(s
, table
);
2085 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2086 if (!options
|| !options
->io
) {
2087 isl_token_free(tok
);
2088 isl_die(s
->ctx
, isl_error_invalid
,
2089 "write operation not allowed", goto error
);
2092 file
= fopen(tok
->u
.s
, "w");
2093 isl_token_free(tok
);
2095 isl_die(s
->ctx
, isl_error_unknown
,
2096 "could not open file for writing", goto error
);
2098 p
= isl_printer_to_file(s
->ctx
, file
);
2099 p
= isl_printer_set_output_format(p
, options
->format
);
2100 p
= obj
.type
->print(p
, obj
.v
);
2101 p
= isl_printer_end_line(p
);
2102 isl_printer_free(p
);
2107 obj
.type
= isl_obj_none
;
2112 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2114 struct isl_token
*tok
;
2115 struct isl_obj obj
= { isl_obj_none
, NULL
};
2117 tok
= isl_stream_next_token(s
);
2120 if (tok
->type
== ISL_TOKEN_STRING
) {
2122 str
= isl_str_alloc(s
->ctx
);
2125 str
->s
= strdup(tok
->u
.s
);
2126 isl_token_free(tok
);
2128 obj
.type
= isl_obj_str
;
2130 isl_stream_push_token(s
, tok
);
2133 isl_token_free(tok
);
2137 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2139 struct isl_token
*tok
;
2140 struct isl_obj obj
= { isl_obj_none
, NULL
};
2142 tok
= isl_stream_next_token(s
);
2145 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2146 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2147 isl_token_free(tok
);
2148 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2149 obj
.type
= isl_obj_bool
;
2151 isl_stream_push_token(s
, tok
);
2154 isl_token_free(tok
);
2158 static __isl_give
char *read_ident(struct isl_stream
*s
)
2161 struct isl_token
*tok
, *tok2
;
2163 name
= isl_stream_read_ident_if_available(s
);
2167 tok
= isl_stream_next_token(s
);
2170 if (tok
->type
!= '$') {
2171 isl_stream_push_token(s
, tok
);
2174 tok2
= isl_stream_next_token(s
);
2175 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2177 isl_stream_push_token(s
, tok2
);
2178 isl_stream_push_token(s
, tok
);
2182 name
= isl_int_get_str(tok2
->u
.v
);
2183 isl_token_free(tok
);
2184 isl_token_free(tok2
);
2189 static struct isl_obj
read_list(struct isl_stream
*s
,
2190 struct isl_hash_table
*table
, struct isl_obj obj
)
2192 struct isl_list
*list
;
2194 list
= isl_list_alloc(s
->ctx
, 2);
2198 list
->obj
[1] = read_obj(s
, table
);
2200 obj
.type
= isl_obj_list
;
2202 if (!list
->obj
[1].v
)
2205 while (isl_stream_eat_if_available(s
, ',')) {
2206 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2214 obj
.type
= isl_obj_none
;
2219 static struct isl_obj
read_obj(struct isl_stream
*s
,
2220 struct isl_hash_table
*table
)
2222 struct isl_obj obj
= { isl_obj_none
, NULL
};
2224 struct isc_un_op
*op
= NULL
;
2226 obj
= read_string_if_available(s
);
2229 obj
= read_bool_if_available(s
);
2232 if (isl_stream_eat_if_available(s
, '(')) {
2233 if (isl_stream_next_token_is(s
, ')')) {
2234 obj
.type
= isl_obj_list
;
2235 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2237 obj
= read_expr(s
, table
);
2238 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2239 obj
= read_list(s
, table
, obj
);
2241 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2244 op
= read_prefix_un_op_if_available(s
);
2246 return read_un_op_expr(s
, table
, op
);
2248 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2249 return check_assert(s
, table
);
2250 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2251 return read_from_file(s
);
2252 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2253 return write_to_file(s
, table
);
2254 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2255 return vertices(s
, table
);
2256 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2257 return any(s
, table
);
2258 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2259 return last(s
, table
);
2260 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2261 return schedule(s
, table
);
2262 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2263 return schedule_forest(s
, table
);
2264 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2265 return type_of(s
, table
);
2267 name
= read_ident(s
);
2269 obj
= stored_obj(s
->ctx
, table
, name
);
2271 obj
= isl_stream_read_obj(s
);
2276 if (isl_stream_eat_if_available(s
, '^'))
2277 obj
= power(s
, obj
);
2278 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2279 obj
= obj_at_index(s
, obj
);
2280 else if (is_subtype(obj
, isl_obj_union_map
) &&
2281 isl_stream_eat_if_available(s
, '(')) {
2282 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2283 obj
= apply(s
, obj
.v
, table
);
2284 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2285 isl_stream_eat_if_available(s
, '(')) {
2286 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2287 obj
= apply_fun(s
, obj
, table
);
2288 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2289 isl_stream_eat_if_available(s
, '(')) {
2290 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2291 obj
= apply_fun(s
, obj
, table
);
2297 obj
.type
= isl_obj_none
;
2302 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2303 struct isl_obj lhs
, struct isl_obj rhs
)
2307 for (i
= 0; ; ++i
) {
2310 if (bin_ops
[i
].op
!= like
->op
)
2312 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2314 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2320 for (i
= 0; ; ++i
) {
2321 if (!named_bin_ops
[i
].name
)
2323 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2325 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2327 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2330 return &named_bin_ops
[i
].op
;
2336 static int next_is_neg_int(struct isl_stream
*s
)
2338 struct isl_token
*tok
;
2341 tok
= isl_stream_next_token(s
);
2342 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2343 isl_stream_push_token(s
, tok
);
2348 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2349 struct isl_obj lhs
, struct isl_obj rhs
)
2353 lhs
= convert(ctx
, lhs
, op
->lhs
);
2354 rhs
= convert(ctx
, rhs
, op
->rhs
);
2355 if (op
->res
!= isl_obj_bool
)
2356 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2358 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2361 obj
.v
= isl_bool_from_int(res
);
2368 static struct isl_obj
read_expr(struct isl_stream
*s
,
2369 struct isl_hash_table
*table
)
2371 struct isl_obj obj
= { isl_obj_none
, NULL
};
2372 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2374 obj
= read_obj(s
, table
);
2376 struct isc_bin_op
*op
= NULL
;
2378 op
= read_bin_op_if_available(s
, obj
);
2382 right_obj
= read_obj(s
, table
);
2384 op
= find_matching_bin_op(op
, obj
, right_obj
);
2387 isl_die(s
->ctx
, isl_error_invalid
,
2388 "no such binary operator defined on given operands",
2391 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2394 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2395 right_obj
= read_obj(s
, table
);
2396 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2401 free_obj(right_obj
);
2403 obj
.type
= isl_obj_none
;
2408 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2409 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2411 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2412 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2414 struct isl_obj obj
= { isl_obj_none
, NULL
};
2418 struct isc_bin_op
*op
= NULL
;
2423 if (isl_stream_is_empty(s
))
2426 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2427 return source_file(s
, table
, p
);
2429 assign
= is_assign(s
);
2431 lhs
= isl_stream_read_ident_if_available(s
);
2432 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2434 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2439 obj
= read_expr(s
, table
);
2440 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2441 fprintf(stderr
, "Interrupted\n");
2442 isl_ctx_reset_error(s
->ctx
);
2444 if (isl_stream_eat(s
, ';'))
2448 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2449 p
= obj
.type
->print(p
, obj
.v
);
2450 p
= isl_printer_end_line(p
);
2455 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2456 static int count
= 0;
2457 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2458 lhs
= strdup(buf
+ 1);
2460 p
= isl_printer_print_str(p
, buf
);
2461 p
= isl_printer_print_str(p
, " := ");
2462 p
= obj
.type
->print(p
, obj
.v
);
2463 p
= isl_printer_end_line(p
);
2465 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2470 isl_stream_flush_tokens(s
);
2471 isl_stream_skip_line(s
);
2477 int free_cb(void **entry
, void *user
)
2479 struct isl_named_obj
*named
= *entry
;
2481 free_obj(named
->obj
);
2488 static void register_named_ops(struct isl_stream
*s
)
2492 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2493 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2494 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2497 for (i
= 0; ; ++i
) {
2498 if (!named_un_ops
[i
].name
)
2500 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2501 named_un_ops
[i
].name
);
2502 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2505 for (i
= 0; ; ++i
) {
2506 if (!named_bin_ops
[i
].name
)
2508 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2509 named_bin_ops
[i
].name
);
2510 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2514 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2515 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2517 struct isl_token
*tok
;
2518 struct isl_stream
*s_file
;
2521 tok
= isl_stream_next_token(s
);
2522 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2523 isl_stream_error(s
, tok
, "expecting filename");
2524 isl_token_free(tok
);
2528 file
= fopen(tok
->u
.s
, "r");
2529 isl_token_free(tok
);
2530 isl_assert(s
->ctx
, file
, return p
);
2532 s_file
= isl_stream_new_file(s
->ctx
, file
);
2538 register_named_ops(s_file
);
2540 while (!s_file
->eof
)
2541 p
= read_line(s_file
, table
, p
, 0);
2543 isl_stream_free(s_file
);
2546 isl_stream_eat(s
, ';');
2551 int main(int argc
, char **argv
)
2553 struct isl_ctx
*ctx
;
2554 struct isl_stream
*s
;
2555 struct isl_hash_table
*table
;
2556 struct iscc_options
*options
;
2558 int tty
= isatty(0);
2560 options
= iscc_options_new_with_defaults();
2563 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2564 pet_options_set_autodetect(ctx
, 1);
2565 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2566 s
= isl_stream_new_file(ctx
, stdin
);
2568 table
= isl_hash_table_alloc(ctx
, 10);
2570 p
= isl_printer_to_file(ctx
, stdout
);
2571 p
= isl_printer_set_output_format(p
, options
->format
);
2574 register_named_ops(s
);
2576 install_signal_handler(ctx
);
2578 while (p
&& !s
->eof
) {
2579 isl_ctx_resume(ctx
);
2580 p
= read_line(s
, table
, p
, tty
);
2583 remove_signal_handler(ctx
);
2585 isl_printer_free(p
);
2586 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2587 isl_hash_table_free(ctx
, table
);