7 #include <isl/stream.h>
8 #include <isl/vertices.h>
11 #include <isl/schedule.h>
12 #include <isl_obj_list.h>
13 #include <isl_obj_str.h>
14 #include <barvinok/isl.h>
15 #include <barvinok/options.h>
16 #include "lattice_width.h"
23 static isl_ctx
*main_ctx
;
25 static void handler(int signum
)
27 if (isl_ctx_aborted(main_ctx
))
29 isl_ctx_abort(main_ctx
);
32 static struct sigaction sa_old
;
34 static void install_signal_handler(isl_ctx
*ctx
)
40 memset(&sa
, 0, sizeof(struct sigaction
));
41 sa
.sa_handler
= &handler
;
42 sa
.sa_flags
= SA_RESTART
;
43 sigaction(SIGINT
, &sa
, &sa_old
);
46 static void remove_signal_handler(isl_ctx
*ctx
)
48 sigaction(SIGINT
, &sa_old
, NULL
);
53 static void install_signal_handler(isl_ctx
*ctx
)
57 static void remove_signal_handler(isl_ctx
*ctx
)
64 #include <cloog/isl/cloog.h>
71 static int isl_bool_false
= 0;
72 static int isl_bool_true
= 1;
73 static int isl_bool_error
= -1;
75 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
76 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
77 ISCC_SCHEDULE
, ISCC_SCHEDULE_FOREST
,
78 ISCC_MINIMIZING
, ISCC_RESPECTING
,
79 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
81 static const char *op_name
[ISCC_N_OP
] = {
82 [ISCC_ASSERT
] = "assert",
84 [ISCC_WRITE
] = "write",
85 [ISCC_PRINT
] = "print",
86 [ISCC_SOURCE
] = "source",
87 [ISCC_VERTICES
] = "vertices",
90 [ISCC_BEFORE
] = "before",
91 [ISCC_UNDER
] = "under",
92 [ISCC_SCHEDULE
] = "schedule",
93 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
94 [ISCC_MINIMIZING
] = "minimizing",
95 [ISCC_RESPECTING
] = "respecting",
96 [ISCC_TYPEOF
] = "typeof"
98 static enum isl_token_type iscc_op
[ISCC_N_OP
];
100 struct isl_arg_choice iscc_format
[] = {
101 {"isl", ISL_FORMAT_ISL
},
102 {"omega", ISL_FORMAT_OMEGA
},
103 {"polylib", ISL_FORMAT_POLYLIB
},
104 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
105 {"latex", ISL_FORMAT_LATEX
},
110 struct iscc_options
{
111 struct barvinok_options
*barvinok
;
116 struct isl_arg iscc_options_arg
[] = {
117 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", barvinok_options_arg
,
119 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
120 iscc_format
, ISL_FORMAT_ISL
, "output format")
121 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
122 "allow read and write operations")
126 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
127 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_arg
)
129 static void *isl_obj_bool_copy(void *v
)
134 static void isl_obj_bool_free(void *v
)
138 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
141 if (v
== &isl_bool_true
)
142 return isl_printer_print_str(p
, "True");
143 else if (v
== &isl_bool_false
)
144 return isl_printer_print_str(p
, "False");
146 return isl_printer_print_str(p
, "Error");
149 static void *isl_obj_bool_add(void *v1
, void *v2
)
154 struct isl_obj_vtable isl_obj_bool_vtable
= {
160 #define isl_obj_bool (&isl_obj_bool_vtable)
162 int *isl_bool_from_int(int res
)
164 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
167 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
168 __isl_take isl_union_map
*map2
)
170 return isl_union_map_is_subset(map2
, map1
);
172 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
173 __isl_take isl_union_set
*set2
)
175 return isl_union_set_is_subset(set2
, set1
);
178 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
179 __isl_take isl_union_map
*map2
)
181 return isl_union_map_is_strict_subset(map2
, map1
);
183 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
184 __isl_take isl_union_set
*set2
)
186 return isl_union_set_is_strict_subset(set2
, set1
);
189 extern struct isl_obj_vtable isl_obj_list_vtable
;
190 #define isl_obj_list (&isl_obj_list_vtable)
192 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
193 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
195 enum isl_token_type op
;
201 isc_bin_test_fn test
;
204 struct isc_named_bin_op
{
206 struct isc_bin_op op
;
210 isl_union_pw_qpolynomial
*upwqp
;
211 isl_union_pw_qpolynomial
*res
;
214 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
216 struct iscc_at
*at
= (struct iscc_at
*) user
;
220 set
= isl_set_from_point(isl_point_copy(pnt
));
221 qp
= isl_union_pw_qpolynomial_eval(
222 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
224 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
225 isl_union_pw_qpolynomial_from_pw_qpolynomial(
226 isl_pw_qpolynomial_alloc(set
, qp
)));
231 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
232 __isl_take isl_union_pw_qpolynomial
*upwqp
,
233 __isl_take isl_union_set
*uset
)
238 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
240 isl_union_set_foreach_point(uset
, eval_at
, &at
);
242 isl_union_pw_qpolynomial_free(upwqp
);
243 isl_union_set_free(uset
);
248 struct iscc_fold_at
{
249 isl_union_pw_qpolynomial_fold
*upwf
;
250 isl_union_pw_qpolynomial
*res
;
253 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
255 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
259 set
= isl_set_from_point(isl_point_copy(pnt
));
260 qp
= isl_union_pw_qpolynomial_fold_eval(
261 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
263 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
264 isl_union_pw_qpolynomial_from_pw_qpolynomial(
265 isl_pw_qpolynomial_alloc(set
, qp
)));
270 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
271 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
272 __isl_take isl_union_set
*uset
)
274 struct iscc_fold_at at
;
277 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
279 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
281 isl_union_pw_qpolynomial_fold_free(upwf
);
282 isl_union_set_free(uset
);
287 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
288 __isl_take isl_union_pw_qpolynomial
*upwqp
,
289 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
291 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
295 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
296 __isl_take isl_union_map
*umap
,
297 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
300 struct isl_list
*list
;
303 ctx
= isl_union_map_get_ctx(umap
);
304 list
= isl_list_alloc(ctx
, 2);
308 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
309 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
311 list
->obj
[1].type
= isl_obj_bool
;
312 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
313 if (tight
< 0 || !list
->obj
[0].v
)
318 isl_union_map_free(umap
);
319 isl_union_pw_qpolynomial_fold_free(upwf
);
325 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
326 __isl_take isl_union_set
*uset
,
327 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
330 struct isl_list
*list
;
333 ctx
= isl_union_set_get_ctx(uset
);
334 list
= isl_list_alloc(ctx
, 2);
338 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
339 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
341 list
->obj
[1].type
= isl_obj_bool
;
342 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
343 if (tight
< 0 || !list
->obj
[0].v
)
348 isl_union_set_free(uset
);
349 isl_union_pw_qpolynomial_fold_free(upwf
);
355 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_int_mul(
356 __isl_take isl_union_pw_qpolynomial
*upwqp
, __isl_take isl_int_obj
*i
)
364 isl_int_obj_get_int(i
, &v
);
365 upwqp
= isl_union_pw_qpolynomial_mul_isl_int(upwqp
, v
);
372 isl_union_pw_qpolynomial_free(upwqp
);
376 static __isl_give isl_union_pw_qpolynomial
*int_union_pw_qpolynomial_mul(
377 __isl_take isl_int_obj
*i
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
379 return union_pw_qpolynomial_int_mul(upwqp
, i
);
382 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_fold_int_mul(
383 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
384 __isl_take isl_int_obj
*i
)
392 isl_int_obj_get_int(i
, &v
);
393 upwf
= isl_union_pw_qpolynomial_fold_mul_isl_int(upwf
, v
);
400 isl_union_pw_qpolynomial_fold_free(upwf
);
404 static __isl_give isl_union_pw_qpolynomial_fold
*int_union_pw_qpolynomial_fold_mul(
405 __isl_take isl_int_obj
*i
,
406 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
408 return union_pw_qpolynomial_fold_int_mul(upwf
, i
);
411 struct isc_bin_op bin_ops
[] = {
412 { '+', isl_obj_int
, isl_obj_int
, isl_obj_int
,
413 (isc_bin_op_fn
) &isl_int_obj_add
},
414 { '-', isl_obj_int
, isl_obj_int
, isl_obj_int
,
415 (isc_bin_op_fn
) &isl_int_obj_sub
},
416 { '*', isl_obj_int
, isl_obj_int
, isl_obj_int
,
417 (isc_bin_op_fn
) &isl_int_obj_mul
},
418 { '+', isl_obj_union_set
, isl_obj_union_set
,
420 (isc_bin_op_fn
) &isl_union_set_union
},
421 { '+', isl_obj_union_map
, isl_obj_union_map
,
423 (isc_bin_op_fn
) &isl_union_map_union
},
424 { '-', isl_obj_union_set
, isl_obj_union_set
,
426 (isc_bin_op_fn
) &isl_union_set_subtract
},
427 { '-', isl_obj_union_map
, isl_obj_union_map
,
429 (isc_bin_op_fn
) &isl_union_map_subtract
},
430 { '*', isl_obj_union_set
, isl_obj_union_set
,
432 (isc_bin_op_fn
) &isl_union_set_intersect
},
433 { '*', isl_obj_union_map
, isl_obj_union_map
,
435 (isc_bin_op_fn
) &isl_union_map_intersect
},
436 { '*', isl_obj_union_map
, isl_obj_union_set
,
438 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
439 { '.', isl_obj_union_map
, isl_obj_union_map
,
441 (isc_bin_op_fn
) &isl_union_map_apply_range
},
442 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
443 isl_obj_union_pw_qpolynomial
,
444 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
445 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
447 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
448 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
450 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
451 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
452 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
453 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
454 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
455 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
457 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
458 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
460 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
461 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
463 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
464 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
466 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
467 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
469 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
470 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
472 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
473 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
476 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
477 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
480 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
481 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
483 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
484 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
486 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
487 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
489 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
490 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
492 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
493 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
495 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
496 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
498 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
499 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
501 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
502 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
504 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
505 { '.', isl_obj_union_pw_qpolynomial_fold
,
506 isl_obj_union_pw_qpolynomial_fold
,
507 isl_obj_union_pw_qpolynomial_fold
,
508 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
509 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
510 isl_obj_union_pw_qpolynomial
,
511 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
512 { '+', isl_obj_union_pw_qpolynomial
,
513 isl_obj_union_pw_qpolynomial_fold
,
514 isl_obj_union_pw_qpolynomial_fold
,
515 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
516 { '+', isl_obj_union_pw_qpolynomial_fold
,
517 isl_obj_union_pw_qpolynomial
,
518 isl_obj_union_pw_qpolynomial_fold
,
519 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
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_sub
},
523 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial
,
524 isl_obj_union_pw_qpolynomial
,
525 (isc_bin_op_fn
) &int_union_pw_qpolynomial_mul
},
526 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_int
,
527 isl_obj_union_pw_qpolynomial
,
528 (isc_bin_op_fn
) &union_pw_qpolynomial_int_mul
},
529 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial_fold
,
530 isl_obj_union_pw_qpolynomial_fold
,
531 (isc_bin_op_fn
) &int_union_pw_qpolynomial_fold_mul
},
532 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_int
,
533 isl_obj_union_pw_qpolynomial_fold
,
534 (isc_bin_op_fn
) &union_pw_qpolynomial_fold_int_mul
},
535 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
536 isl_obj_union_pw_qpolynomial
,
537 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
538 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
539 isl_obj_union_pw_qpolynomial
,
540 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
541 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
542 isl_obj_union_pw_qpolynomial_fold
,
543 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
544 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
545 isl_obj_union_pw_qpolynomial
,
546 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
547 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
548 isl_obj_union_pw_qpolynomial
,
549 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
550 { '%', isl_obj_union_set
, isl_obj_union_set
,
552 (isc_bin_op_fn
) &isl_union_set_gist
},
553 { '%', isl_obj_union_map
, isl_obj_union_map
,
555 (isc_bin_op_fn
) &isl_union_map_gist
},
556 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
557 isl_obj_union_pw_qpolynomial
,
558 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
559 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
560 isl_obj_union_pw_qpolynomial_fold
,
561 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
562 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
563 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
564 { .test
= (isc_bin_test_fn
)
565 &isl_union_pw_qpolynomial_plain_is_equal
} },
566 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
567 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
568 { .test
= (isc_bin_test_fn
)
569 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
570 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
571 (isc_bin_op_fn
) &isl_str_concat
},
575 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
576 __isl_take isl_union_map
*umap2
)
578 return isl_union_map_apply_range(umap2
, umap1
);
581 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
582 __isl_take isl_union_pw_qpolynomial
*upwqp
,
583 __isl_take isl_union_map
*umap
)
585 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
588 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
589 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
590 __isl_take isl_union_map
*umap
)
592 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
595 struct isc_named_bin_op named_bin_ops
[] = {
596 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
598 (isc_bin_op_fn
) &map_after_map
} },
599 { "after", { -1, isl_obj_union_pw_qpolynomial
,
600 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
601 (isc_bin_op_fn
) &qpolynomial_after_map
} },
602 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
603 isl_obj_union_map
, isl_obj_list
,
604 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
605 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
607 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
608 { "before", { -1, isl_obj_union_map
,
609 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
610 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
611 { "before", { -1, isl_obj_union_map
,
612 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
613 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
614 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
616 (isc_bin_op_fn
) &isl_union_set_product
} },
617 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
619 (isc_bin_op_fn
) &isl_union_map_product
} },
623 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
625 return isl_set_from_basic_set(isl_union_set_sample(uset
));
628 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
630 return isl_map_from_basic_map(isl_union_map_sample(umap
));
633 static __isl_give
struct isl_list
*union_map_power(
634 __isl_take isl_union_map
*umap
)
637 struct isl_list
*list
;
640 ctx
= isl_union_map_get_ctx(umap
);
641 list
= isl_list_alloc(ctx
, 2);
645 list
->obj
[0].type
= isl_obj_union_map
;
646 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
647 list
->obj
[1].type
= isl_obj_bool
;
648 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
649 if (exact
< 0 || !list
->obj
[0].v
)
654 isl_union_map_free(umap
);
660 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
661 __isl_take isl_union_pw_qpolynomial
*upwqp
)
664 struct isl_list
*list
;
667 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
668 list
= isl_list_alloc(ctx
, 2);
672 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
673 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
674 isl_fold_max
, &tight
);
675 list
->obj
[1].type
= isl_obj_bool
;
676 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
677 if (tight
< 0 || !list
->obj
[0].v
)
682 isl_union_pw_qpolynomial_free(upwqp
);
689 void *map_codegen(void *arg
)
692 isl_union_map
*umap
= (isl_union_map
*)arg
;
693 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
695 CloogOptions
*options
;
696 CloogDomain
*context
;
697 CloogUnionDomain
*ud
;
699 struct clast_stmt
*stmt
;
701 state
= cloog_isl_state_malloc(ctx
);
702 options
= cloog_options_malloc(state
);
703 options
->language
= LANGUAGE_C
;
704 options
->strides
= 1;
707 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
709 dim
= isl_union_map_get_space(umap
);
710 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
712 input
= cloog_input_alloc(context
, ud
);
714 stmt
= cloog_clast_create_from_input(input
, options
);
715 clast_pprint(stdout
, stmt
, 0, options
);
716 cloog_clast_free(stmt
);
719 cloog_options_free(options
);
720 cloog_state_free(state
);
721 isl_union_map_free(umap
);
725 void *set_codegen(void *arg
)
728 isl_union_set
*uset
= (isl_union_set
*)arg
;
729 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
731 CloogOptions
*options
;
732 CloogDomain
*context
;
733 CloogUnionDomain
*ud
;
735 struct clast_stmt
*stmt
;
737 if (isl_union_set_n_set(uset
) > 1)
738 isl_die(ctx
, isl_error_invalid
,
739 "code generation for more than one domain "
740 "requires a schedule", goto error
);
742 state
= cloog_isl_state_malloc(ctx
);
743 options
= cloog_options_malloc(state
);
744 options
->language
= LANGUAGE_C
;
745 options
->strides
= 1;
748 ud
= cloog_union_domain_from_isl_union_set(isl_union_set_copy(uset
));
750 dim
= isl_union_set_get_space(uset
);
751 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
753 input
= cloog_input_alloc(context
, ud
);
755 stmt
= cloog_clast_create_from_input(input
, options
);
756 clast_pprint(stdout
, stmt
, 0, options
);
757 cloog_clast_free(stmt
);
759 cloog_options_free(options
);
760 cloog_state_free(state
);
762 isl_union_set_free(uset
);
768 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
771 struct isl_list
*list
;
772 struct pet_scop
*scop
;
773 isl_union_map
*sched
, *reads
, *writes
;
774 isl_union_set
*domain
;
775 struct iscc_options
*options
;
781 options
= isl_ctx_peek_iscc_options(ctx
);
782 if (!options
|| !options
->io
) {
784 isl_die(ctx
, isl_error_invalid
,
785 "parse_file operation not allowed", return NULL
);
788 list
= isl_list_alloc(ctx
, 4);
792 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
, 1);
793 domain
= pet_scop_collect_domains(scop
);
794 sched
= pet_scop_collect_schedule(scop
);
795 reads
= pet_scop_collect_reads(scop
);
796 writes
= pet_scop_collect_writes(scop
);
799 list
->obj
[0].type
= isl_obj_union_set
;
800 list
->obj
[0].v
= domain
;
801 list
->obj
[1].type
= isl_obj_union_map
;
802 list
->obj
[1].v
= writes
;
803 list
->obj
[2].type
= isl_obj_union_map
;
804 list
->obj
[2].v
= reads
;
805 list
->obj
[3].type
= isl_obj_union_map
;
806 list
->obj
[3].v
= sched
;
808 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
809 !list
->obj
[2].v
|| !list
->obj
[3].v
)
821 static int add_point(__isl_take isl_point
*pnt
, void *user
)
823 isl_union_set
**scan
= (isl_union_set
**) user
;
825 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
830 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
834 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
836 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
837 isl_union_set_free(scan
);
841 isl_union_set_free(uset
);
845 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
847 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
850 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
851 __isl_take isl_union_pw_qpolynomial
*upwqp
)
853 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
856 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
857 __isl_take isl_union_pw_qpolynomial
*upwqp
)
859 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
862 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
863 __isl_take isl_union_pw_qpolynomial
*upwqp
)
865 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
868 typedef void *(*isc_un_op_fn
)(void *arg
);
870 enum isl_token_type op
;
875 struct isc_named_un_op
{
879 struct isc_named_un_op named_un_ops
[] = {
880 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
881 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
882 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
883 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
884 {"card", { -1, isl_obj_union_set
,
885 isl_obj_union_pw_qpolynomial
,
886 (isc_un_op_fn
) &isl_union_set_card
} },
887 {"card", { -1, isl_obj_union_map
,
888 isl_obj_union_pw_qpolynomial
,
889 (isc_un_op_fn
) &isl_union_map_card
} },
890 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
891 (isc_un_op_fn
) &isl_union_set_coalesce
} },
892 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
893 (isc_un_op_fn
) &isl_union_map_coalesce
} },
894 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
895 isl_obj_union_pw_qpolynomial
,
896 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
897 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
898 isl_obj_union_pw_qpolynomial_fold
,
899 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
901 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
903 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
906 {"coefficients", { -1, isl_obj_union_set
,
908 (isc_un_op_fn
) &isl_union_set_coefficients
} },
909 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
910 (isc_un_op_fn
) &isl_union_set_solutions
} },
911 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
912 (isc_un_op_fn
) &isl_union_map_deltas
} },
913 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
914 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
915 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
916 (isc_un_op_fn
) &isl_union_map_domain
} },
917 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
918 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
919 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
921 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
922 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
923 (isc_un_op_fn
) &isl_union_map_domain
} },
924 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
926 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
927 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
929 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
930 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
931 (isc_un_op_fn
) &isl_union_map_domain_map
} },
932 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
933 (isc_un_op_fn
) &isl_union_map_range
} },
934 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
935 (isc_un_op_fn
) &isl_union_map_range
} },
936 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
937 (isc_un_op_fn
) &isl_union_map_range_map
} },
938 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
939 (isc_un_op_fn
) &isl_union_set_identity
} },
940 {"lattice_width", { -1, isl_obj_union_set
,
941 isl_obj_union_pw_qpolynomial
,
942 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
943 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
944 (isc_un_op_fn
) &isl_union_map_lexmin
} },
945 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
946 (isc_un_op_fn
) &isl_union_map_lexmax
} },
947 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
948 (isc_un_op_fn
) &isl_union_set_lexmin
} },
949 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
950 (isc_un_op_fn
) &isl_union_set_lexmax
} },
951 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
952 (isc_un_op_fn
) &isl_union_set_lift
} },
953 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
954 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
955 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
956 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
957 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
958 isl_obj_union_pw_qpolynomial
,
959 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
960 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
961 isl_obj_union_pw_qpolynomial
,
962 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
963 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
964 isl_obj_union_pw_qpolynomial
,
965 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
967 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
968 (isc_un_op_fn
) &parse
} },
970 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
971 (isc_un_op_fn
) &union_map_power
} },
972 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
973 (isc_un_op_fn
) &union_set_sample
} },
974 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
975 (isc_un_op_fn
) &union_map_sample
} },
976 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
977 (isc_un_op_fn
) &union_set_scan
} },
978 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
979 (isc_un_op_fn
) &union_map_scan
} },
980 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
981 isl_obj_union_pw_qpolynomial
,
982 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
983 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
984 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
985 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
986 (isc_un_op_fn
) &isl_union_set_unwrap
} },
987 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
988 (isc_un_op_fn
) &isl_union_map_wrap
} },
989 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
990 (isc_un_op_fn
) &isl_union_map_zip
} },
994 struct isl_named_obj
{
999 static void free_obj(struct isl_obj obj
)
1001 obj
.type
->free(obj
.v
);
1004 static int same_name(const void *entry
, const void *val
)
1006 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1008 return !strcmp(named
->name
, val
);
1011 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1012 char *name
, struct isl_obj obj
)
1014 struct isl_hash_table_entry
*entry
;
1016 struct isl_named_obj
*named
;
1018 name_hash
= isl_hash_string(isl_hash_init(), name
);
1019 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1023 named
= entry
->data
;
1024 free_obj(named
->obj
);
1027 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1031 entry
->data
= named
;
1042 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1043 struct isl_hash_table
*table
, char *name
)
1045 struct isl_obj obj
= { isl_obj_none
, NULL
};
1046 struct isl_hash_table_entry
*entry
;
1049 name_hash
= isl_hash_string(isl_hash_init(), name
);
1050 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1052 struct isl_named_obj
*named
;
1053 named
= entry
->data
;
1055 } else if (isdigit(name
[0]))
1056 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1058 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1061 obj
.v
= obj
.type
->copy(obj
.v
);
1065 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1067 if (obj
.type
== super
)
1069 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1071 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1073 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1074 super
== isl_obj_union_pw_qpolynomial
)
1076 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1077 super
== isl_obj_union_pw_qpolynomial_fold
)
1079 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1081 if (obj
.type
== isl_obj_list
) {
1082 struct isl_list
*list
= obj
.v
;
1083 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1084 return is_subtype(list
->obj
[0], super
);
1086 if (super
== isl_obj_str
)
1091 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1093 struct isl_list
*list
= obj
.v
;
1096 obj
.v
= obj
.type
->copy(obj
.v
);
1098 isl_list_free(list
);
1103 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1106 if (obj
.type
== type
)
1108 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1109 obj
.type
= isl_obj_union_map
;
1110 obj
.v
= isl_union_map_from_map(obj
.v
);
1113 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1114 obj
.type
= isl_obj_union_set
;
1115 obj
.v
= isl_union_set_from_set(obj
.v
);
1118 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1119 type
== isl_obj_union_pw_qpolynomial
) {
1120 obj
.type
= isl_obj_union_pw_qpolynomial
;
1121 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1124 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1125 type
== isl_obj_union_pw_qpolynomial_fold
) {
1126 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1127 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1130 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1131 if (type
== isl_obj_union_map
) {
1132 obj
.type
= isl_obj_union_map
;
1135 if (type
== isl_obj_union_pw_qpolynomial
) {
1136 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1137 isl_union_set_free(obj
.v
);
1138 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1139 obj
.type
= isl_obj_union_pw_qpolynomial
;
1142 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1143 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1144 isl_union_set_free(obj
.v
);
1145 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1147 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1151 if (obj
.type
== isl_obj_list
) {
1152 struct isl_list
*list
= obj
.v
;
1153 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1154 return convert(ctx
, obj_at(obj
, 0), type
);
1156 if (type
== isl_obj_str
) {
1161 p
= isl_printer_to_str(ctx
);
1164 p
= obj
.type
->print(p
, obj
.v
);
1165 s
= isl_printer_get_str(p
);
1166 isl_printer_free(p
);
1168 str
= isl_str_from_string(ctx
, s
);
1173 obj
.type
= isl_obj_str
;
1179 obj
.type
= isl_obj_none
;
1184 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1188 struct isl_token
*tok
;
1190 tok
= isl_stream_next_token(s
);
1194 for (i
= 0; ; ++i
) {
1197 if (bin_ops
[i
].op
!= tok
->type
)
1199 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1202 isl_token_free(tok
);
1206 for (i
= 0; ; ++i
) {
1207 if (!named_bin_ops
[i
].name
)
1209 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1211 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1214 isl_token_free(tok
);
1215 return &named_bin_ops
[i
].op
;
1218 isl_stream_push_token(s
, tok
);
1223 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1226 struct isl_token
*tok
;
1228 tok
= isl_stream_next_token(s
);
1232 for (i
= 0; ; ++i
) {
1233 if (!named_un_ops
[i
].name
)
1235 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1238 isl_token_free(tok
);
1239 return &named_un_ops
[i
].op
;
1242 isl_stream_push_token(s
, tok
);
1247 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1252 for (i
= 0; ; ++i
) {
1253 if (!named_un_ops
[i
].name
)
1255 if (named_un_ops
[i
].op
.op
!= like
->op
)
1257 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1260 return &named_un_ops
[i
].op
;
1266 static int is_assign(struct isl_stream
*s
)
1268 struct isl_token
*tok
;
1269 struct isl_token
*tok2
;
1272 tok
= isl_stream_next_token(s
);
1275 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1276 isl_stream_push_token(s
, tok
);
1280 tok2
= isl_stream_next_token(s
);
1282 isl_stream_push_token(s
, tok
);
1285 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1286 isl_stream_push_token(s
, tok2
);
1287 isl_stream_push_token(s
, tok
);
1292 static struct isl_obj
read_obj(struct isl_stream
*s
,
1293 struct isl_hash_table
*table
);
1294 static struct isl_obj
read_expr(struct isl_stream
*s
,
1295 struct isl_hash_table
*table
);
1297 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1298 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1300 struct isl_obj obj
= { isl_obj_none
, NULL
};
1302 obj
= read_obj(s
, table
);
1306 op
= find_matching_un_op(op
, obj
);
1309 isl_die(s
->ctx
, isl_error_invalid
,
1310 "no such unary operator defined on given operand",
1313 obj
= convert(s
->ctx
, obj
, op
->arg
);
1314 obj
.v
= op
->fn(obj
.v
);
1320 obj
.type
= isl_obj_none
;
1325 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1327 struct isl_list
*list
;
1330 if (obj
.type
!= isl_obj_union_map
)
1331 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1332 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1333 list
= isl_list_alloc(ctx
, 2);
1337 list
->obj
[0].type
= isl_obj_union_map
;
1338 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1339 list
->obj
[1].type
= isl_obj_bool
;
1340 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1342 obj
.type
= isl_obj_list
;
1343 if (exact
< 0 || !list
->obj
[0].v
)
1349 obj
.type
= isl_obj_none
;
1354 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1356 struct isl_list
*list
= obj
.v
;
1357 struct isl_token
*tok
;
1360 tok
= isl_stream_next_token(s
);
1361 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1362 isl_stream_error(s
, tok
, "expecting index");
1364 isl_stream_push_token(s
, tok
);
1367 i
= isl_int_get_si(tok
->u
.v
);
1368 isl_token_free(tok
);
1369 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1370 if (isl_stream_eat(s
, ']'))
1373 return obj_at(obj
, i
);
1376 obj
.type
= isl_obj_none
;
1381 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1382 struct isl_hash_table
*table
)
1386 obj
= read_expr(s
, table
);
1387 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1388 is_subtype(obj
, isl_obj_union_map
), goto error
);
1390 if (obj
.type
== isl_obj_list
) {
1391 struct isl_list
*list
= obj
.v
;
1392 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1393 obj
= obj_at(obj
, 0);
1395 if (obj
.type
== isl_obj_set
)
1396 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1397 else if (obj
.type
== isl_obj_map
)
1398 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1399 if (obj
.type
== isl_obj_union_set
) {
1400 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1402 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1406 if (isl_stream_eat(s
, ')'))
1411 isl_union_map_free(umap
);
1414 obj
.type
= isl_obj_none
;
1419 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1420 __isl_take isl_union_set
*uset
)
1422 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1423 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1425 obj
.type
= isl_obj_list
;
1426 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1431 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1432 __isl_take isl_union_map
*umap
)
1434 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1435 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1437 obj
.type
= isl_obj_list
;
1438 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1443 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1444 struct isl_obj obj
, struct isl_hash_table
*table
)
1448 arg
= read_expr(s
, table
);
1449 if (!is_subtype(arg
, isl_obj_union_map
) &&
1450 !is_subtype(arg
, isl_obj_union_set
))
1451 isl_die(s
->ctx
, isl_error_invalid
,
1452 "expecting set of map argument", goto error
);
1454 if (arg
.type
== isl_obj_list
) {
1455 struct isl_list
*list
= arg
.v
;
1456 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1457 arg
= obj_at(arg
, 0);
1459 if (arg
.type
== isl_obj_set
)
1460 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1461 else if (arg
.type
== isl_obj_map
)
1462 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1463 if (arg
.type
== isl_obj_union_set
)
1464 obj
= apply_fun_set(obj
, arg
.v
);
1466 obj
= apply_fun_map(obj
, arg
.v
);
1470 if (isl_stream_eat(s
, ')'))
1478 obj
.type
= isl_obj_none
;
1483 struct add_vertex_data
{
1484 struct isl_list
*list
;
1488 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1490 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1491 isl_basic_set
*expr
;
1493 expr
= isl_vertex_get_expr(vertex
);
1495 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1496 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1499 isl_vertex_free(vertex
);
1504 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1507 isl_basic_set
*hull
;
1508 isl_vertices
*vertices
= NULL
;
1509 struct isl_list
*list
= NULL
;
1511 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1513 set
= isl_set_remove_divs(set
);
1514 hull
= isl_set_convex_hull(set
);
1515 vertices
= isl_basic_set_compute_vertices(hull
);
1516 isl_basic_set_free(hull
);
1520 ctx
= isl_vertices_get_ctx(vertices
);
1521 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1526 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1528 data
->list
= isl_list_concat(list
, data
->list
);
1530 isl_vertices_free(vertices
);
1535 isl_vertices_free(vertices
);
1539 static struct isl_obj
vertices(struct isl_stream
*s
,
1540 struct isl_hash_table
*table
)
1544 struct isl_list
*list
= NULL
;
1545 isl_union_set
*uset
;
1546 struct add_vertex_data data
= { NULL
};
1548 obj
= read_expr(s
, table
);
1549 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1550 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1554 ctx
= isl_union_set_get_ctx(uset
);
1555 list
= isl_list_alloc(ctx
, 0);
1561 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1564 isl_union_set_free(uset
);
1566 obj
.type
= isl_obj_list
;
1571 isl_union_set_free(uset
);
1572 isl_list_free(data
.list
);
1574 obj
.type
= isl_obj_none
;
1579 static struct isl_obj
type_of(struct isl_stream
*s
,
1580 struct isl_hash_table
*table
)
1584 const char *type
= "unknown";
1586 obj
= read_expr(s
, table
);
1588 if (obj
.type
== isl_obj_map
||
1589 obj
.type
== isl_obj_union_map
)
1591 if (obj
.type
== isl_obj_set
||
1592 obj
.type
== isl_obj_union_set
)
1594 if (obj
.type
== isl_obj_pw_qpolynomial
||
1595 obj
.type
== isl_obj_union_pw_qpolynomial
)
1596 type
= "piecewise quasipolynomial";
1597 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1598 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1599 type
= "piecewise quasipolynomial fold";
1600 if (obj
.type
== isl_obj_list
)
1602 if (obj
.type
== isl_obj_bool
)
1604 if (obj
.type
== isl_obj_str
)
1606 if (obj
.type
== isl_obj_int
)
1610 obj
.type
= isl_obj_str
;
1611 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1616 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1617 struct isl_hash_table
*table
)
1621 obj
= read_obj(s
, table
);
1622 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1623 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1630 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1631 struct isl_hash_table
*table
)
1635 obj
= read_obj(s
, table
);
1636 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1637 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1644 static struct isl_obj
last_any(struct isl_stream
*s
,
1645 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1646 __isl_take isl_union_map
*may_source
)
1648 struct isl_obj obj
= { isl_obj_none
, NULL
};
1649 isl_union_map
*sink
= NULL
;
1650 isl_union_map
*schedule
= NULL
;
1651 isl_union_map
*may_dep
;
1652 isl_union_map
*must_dep
;
1654 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1657 sink
= read_map(s
, table
);
1661 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1664 schedule
= read_map(s
, table
);
1668 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1669 schedule
, &must_dep
, &may_dep
,
1673 obj
.type
= isl_obj_union_map
;
1674 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1678 isl_union_map_free(may_source
);
1679 isl_union_map_free(must_source
);
1680 isl_union_map_free(sink
);
1681 isl_union_map_free(schedule
);
1683 obj
.type
= isl_obj_none
;
1688 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1690 struct isl_obj obj
= { isl_obj_none
, NULL
};
1691 isl_union_map
*must_source
= NULL
;
1692 isl_union_map
*may_source
= NULL
;
1693 isl_union_map
*sink
= NULL
;
1694 isl_union_map
*schedule
= NULL
;
1695 isl_union_map
*may_dep
;
1697 may_source
= read_map(s
, table
);
1701 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1702 must_source
= read_map(s
, table
);
1705 return last_any(s
, table
, must_source
, may_source
);
1708 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1711 sink
= read_map(s
, table
);
1715 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1718 schedule
= read_map(s
, table
);
1722 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1723 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1724 schedule
, NULL
, &may_dep
,
1728 obj
.type
= isl_obj_union_map
;
1733 isl_union_map_free(may_source
);
1734 isl_union_map_free(must_source
);
1735 isl_union_map_free(sink
);
1736 isl_union_map_free(schedule
);
1738 obj
.type
= isl_obj_none
;
1743 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1745 struct isl_obj obj
= { isl_obj_none
, NULL
};
1746 struct isl_list
*list
= NULL
;
1747 isl_union_map
*must_source
= NULL
;
1748 isl_union_map
*may_source
= NULL
;
1749 isl_union_map
*sink
= NULL
;
1750 isl_union_map
*schedule
= NULL
;
1751 isl_union_map
*must_dep
;
1752 isl_union_map
*must_no_source
;
1754 must_source
= read_map(s
, table
);
1758 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1759 may_source
= read_map(s
, table
);
1762 return last_any(s
, table
, must_source
, may_source
);
1765 list
= isl_list_alloc(s
->ctx
, 2);
1769 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1772 sink
= read_map(s
, table
);
1776 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1779 schedule
= read_map(s
, table
);
1783 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1784 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1785 schedule
, &must_dep
, NULL
,
1786 &must_no_source
, NULL
) < 0) {
1787 isl_list_free(list
);
1791 list
->obj
[0].type
= isl_obj_union_map
;
1792 list
->obj
[0].v
= must_dep
;
1793 list
->obj
[1].type
= isl_obj_union_map
;
1794 list
->obj
[1].v
= must_no_source
;
1797 obj
.type
= isl_obj_list
;
1801 isl_list_free(list
);
1802 isl_union_map_free(may_source
);
1803 isl_union_map_free(must_source
);
1804 isl_union_map_free(sink
);
1805 isl_union_map_free(schedule
);
1807 obj
.type
= isl_obj_none
;
1812 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1813 struct isl_hash_table
*table
)
1815 isl_union_set
*domain
;
1816 isl_union_map
*validity
;
1817 isl_union_map
*proximity
;
1819 domain
= read_set(s
, table
);
1823 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1824 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1827 isl_union_map
*umap
;
1828 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1829 umap
= read_map(s
, table
);
1830 validity
= isl_union_map_union(validity
, umap
);
1831 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1832 umap
= read_map(s
, table
);
1833 proximity
= isl_union_map_union(proximity
, umap
);
1838 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1841 static struct isl_obj
schedule(struct isl_stream
*s
,
1842 struct isl_hash_table
*table
)
1844 struct isl_obj obj
= { isl_obj_none
, NULL
};
1845 isl_schedule
*schedule
;
1847 schedule
= get_schedule(s
, table
);
1849 obj
.v
= isl_schedule_get_map(schedule
);
1850 obj
.type
= isl_obj_union_map
;
1852 isl_schedule_free(schedule
);
1857 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1859 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1861 struct isl_obj obj
= { isl_obj_none
, NULL
};
1862 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1863 struct isl_list
*list
;
1865 list
= isl_list_alloc(ctx
, 2);
1870 obj
.type
= isl_obj_list
;
1872 list
->obj
[0].type
= isl_obj_union_map
;
1873 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1875 if (isl_band_has_children(band
)) {
1876 isl_band_list
*children
;
1878 children
= isl_band_get_children(band
);
1879 list
->obj
[1] = band_list_to_obj_list(children
);
1881 list
->obj
[1].type
= isl_obj_list
;
1882 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1885 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1888 isl_band_free(band
);
1892 isl_band_free(band
);
1894 obj
.type
= isl_obj_none
;
1899 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1901 struct isl_obj obj
= { isl_obj_none
, NULL
};
1902 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1903 struct isl_list
*list
;
1906 n
= isl_band_list_n_band(bands
);
1907 list
= isl_list_alloc(ctx
, n
);
1912 obj
.type
= isl_obj_list
;
1914 for (i
= 0; i
< n
; ++i
) {
1917 band
= isl_band_list_get_band(bands
, i
);
1918 list
->obj
[i
] = band_to_obj_list(band
);
1919 if (!list
->obj
[i
].v
)
1923 isl_band_list_free(bands
);
1927 isl_band_list_free(bands
);
1929 obj
.type
= isl_obj_none
;
1934 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1935 struct isl_hash_table
*table
)
1937 struct isl_obj obj
= { isl_obj_none
, NULL
};
1938 isl_schedule
*schedule
;
1939 isl_band_list
*roots
;
1941 schedule
= get_schedule(s
, table
);
1945 roots
= isl_schedule_get_band_forest(schedule
);
1946 isl_schedule_free(schedule
);
1948 return band_list_to_obj_list(roots
);
1951 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1953 struct isl_token
*tok
;
1955 if (isl_stream_eat_if_available(s
, '+'))
1956 return transitive_closure(s
->ctx
, obj
);
1958 tok
= isl_stream_next_token(s
);
1959 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1960 isl_stream_error(s
, tok
, "expecting -1");
1962 isl_stream_push_token(s
, tok
);
1965 isl_token_free(tok
);
1966 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1967 if (obj
.type
!= isl_obj_union_map
)
1968 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1970 obj
.v
= isl_union_map_reverse(obj
.v
);
1977 obj
.type
= isl_obj_none
;
1982 static struct isl_obj
check_assert(struct isl_stream
*s
,
1983 struct isl_hash_table
*table
)
1987 obj
= read_expr(s
, table
);
1988 if (obj
.type
!= isl_obj_bool
)
1989 isl_die(s
->ctx
, isl_error_invalid
,
1990 "expecting boolean expression", goto error
);
1991 if (obj
.v
!= &isl_bool_true
)
1992 isl_die(s
->ctx
, isl_error_unknown
,
1993 "assertion failed", abort());
1996 obj
.type
= isl_obj_none
;
2001 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2004 struct isl_token
*tok
;
2005 struct isl_stream
*s_file
;
2006 struct iscc_options
*options
;
2009 tok
= isl_stream_next_token(s
);
2010 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2011 isl_stream_error(s
, tok
, "expecting filename");
2012 isl_token_free(tok
);
2016 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2017 if (!options
|| !options
->io
) {
2018 isl_token_free(tok
);
2019 isl_die(s
->ctx
, isl_error_invalid
,
2020 "read operation not allowed", goto error
);
2023 file
= fopen(tok
->u
.s
, "r");
2024 isl_token_free(tok
);
2025 isl_assert(s
->ctx
, file
, goto error
);
2027 s_file
= isl_stream_new_file(s
->ctx
, file
);
2033 obj
= isl_stream_read_obj(s_file
);
2035 isl_stream_free(s_file
);
2040 obj
.type
= isl_obj_none
;
2045 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2046 struct isl_hash_table
*table
)
2049 struct isl_token
*tok
;
2050 struct isl_stream
*s_file
;
2051 struct iscc_options
*options
;
2055 tok
= isl_stream_next_token(s
);
2056 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2057 isl_stream_error(s
, tok
, "expecting filename");
2058 isl_token_free(tok
);
2062 obj
= read_expr(s
, table
);
2064 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2065 if (!options
|| !options
->io
) {
2066 isl_token_free(tok
);
2067 isl_die(s
->ctx
, isl_error_invalid
,
2068 "write operation not allowed", goto error
);
2071 file
= fopen(tok
->u
.s
, "w");
2072 isl_token_free(tok
);
2074 isl_die(s
->ctx
, isl_error_unknown
,
2075 "could not open file for writing", goto error
);
2077 p
= isl_printer_to_file(s
->ctx
, file
);
2078 p
= isl_printer_set_output_format(p
, options
->format
);
2079 p
= obj
.type
->print(p
, obj
.v
);
2080 p
= isl_printer_end_line(p
);
2081 isl_printer_free(p
);
2086 obj
.type
= isl_obj_none
;
2091 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2093 struct isl_token
*tok
;
2094 struct isl_obj obj
= { isl_obj_none
, NULL
};
2096 tok
= isl_stream_next_token(s
);
2099 if (tok
->type
== ISL_TOKEN_STRING
) {
2101 str
= isl_str_alloc(s
->ctx
);
2104 str
->s
= strdup(tok
->u
.s
);
2105 isl_token_free(tok
);
2107 obj
.type
= isl_obj_str
;
2109 isl_stream_push_token(s
, tok
);
2112 isl_token_free(tok
);
2116 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2118 struct isl_token
*tok
;
2119 struct isl_obj obj
= { isl_obj_none
, NULL
};
2121 tok
= isl_stream_next_token(s
);
2124 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2125 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2126 isl_token_free(tok
);
2127 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2128 obj
.type
= isl_obj_bool
;
2130 isl_stream_push_token(s
, tok
);
2133 isl_token_free(tok
);
2137 static __isl_give
char *read_ident(struct isl_stream
*s
)
2140 struct isl_token
*tok
, *tok2
;
2142 name
= isl_stream_read_ident_if_available(s
);
2146 tok
= isl_stream_next_token(s
);
2149 if (tok
->type
!= '$') {
2150 isl_stream_push_token(s
, tok
);
2153 tok2
= isl_stream_next_token(s
);
2154 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2156 isl_stream_push_token(s
, tok2
);
2157 isl_stream_push_token(s
, tok
);
2161 name
= isl_int_get_str(tok2
->u
.v
);
2162 isl_token_free(tok
);
2163 isl_token_free(tok2
);
2168 static struct isl_obj
read_list(struct isl_stream
*s
,
2169 struct isl_hash_table
*table
, struct isl_obj obj
)
2171 struct isl_list
*list
;
2173 list
= isl_list_alloc(s
->ctx
, 2);
2177 list
->obj
[1] = read_obj(s
, table
);
2179 obj
.type
= isl_obj_list
;
2181 if (!list
->obj
[1].v
)
2184 while (isl_stream_eat_if_available(s
, ',')) {
2185 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2193 obj
.type
= isl_obj_none
;
2198 static struct isl_obj
read_obj(struct isl_stream
*s
,
2199 struct isl_hash_table
*table
)
2201 struct isl_obj obj
= { isl_obj_none
, NULL
};
2203 struct isc_un_op
*op
= NULL
;
2205 obj
= read_string_if_available(s
);
2208 obj
= read_bool_if_available(s
);
2211 if (isl_stream_eat_if_available(s
, '(')) {
2212 if (isl_stream_next_token_is(s
, ')')) {
2213 obj
.type
= isl_obj_list
;
2214 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2216 obj
= read_expr(s
, table
);
2217 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2218 obj
= read_list(s
, table
, obj
);
2220 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2223 op
= read_prefix_un_op_if_available(s
);
2225 return read_un_op_expr(s
, table
, op
);
2227 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2228 return check_assert(s
, table
);
2229 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2230 return read_from_file(s
);
2231 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2232 return write_to_file(s
, table
);
2233 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2234 return vertices(s
, table
);
2235 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2236 return any(s
, table
);
2237 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2238 return last(s
, table
);
2239 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2240 return schedule(s
, table
);
2241 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2242 return schedule_forest(s
, table
);
2243 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2244 return type_of(s
, table
);
2246 name
= read_ident(s
);
2248 obj
= stored_obj(s
->ctx
, table
, name
);
2250 obj
= isl_stream_read_obj(s
);
2255 if (isl_stream_eat_if_available(s
, '^'))
2256 obj
= power(s
, obj
);
2257 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2258 obj
= obj_at_index(s
, obj
);
2259 else if (is_subtype(obj
, isl_obj_union_map
) &&
2260 isl_stream_eat_if_available(s
, '(')) {
2261 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2262 obj
= apply(s
, obj
.v
, table
);
2263 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2264 isl_stream_eat_if_available(s
, '(')) {
2265 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2266 obj
= apply_fun(s
, obj
, table
);
2267 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2268 isl_stream_eat_if_available(s
, '(')) {
2269 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2270 obj
= apply_fun(s
, obj
, table
);
2276 obj
.type
= isl_obj_none
;
2281 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2282 struct isl_obj lhs
, struct isl_obj rhs
)
2286 for (i
= 0; ; ++i
) {
2289 if (bin_ops
[i
].op
!= like
->op
)
2291 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2293 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2299 for (i
= 0; ; ++i
) {
2300 if (!named_bin_ops
[i
].name
)
2302 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2304 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2306 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2309 return &named_bin_ops
[i
].op
;
2315 static int next_is_neg_int(struct isl_stream
*s
)
2317 struct isl_token
*tok
;
2320 tok
= isl_stream_next_token(s
);
2321 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2322 isl_stream_push_token(s
, tok
);
2327 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2328 struct isl_obj lhs
, struct isl_obj rhs
)
2332 lhs
= convert(ctx
, lhs
, op
->lhs
);
2333 rhs
= convert(ctx
, rhs
, op
->rhs
);
2334 if (op
->res
!= isl_obj_bool
)
2335 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2337 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2340 obj
.v
= isl_bool_from_int(res
);
2347 static struct isl_obj
read_expr(struct isl_stream
*s
,
2348 struct isl_hash_table
*table
)
2350 struct isl_obj obj
= { isl_obj_none
, NULL
};
2351 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2353 obj
= read_obj(s
, table
);
2355 struct isc_bin_op
*op
= NULL
;
2357 op
= read_bin_op_if_available(s
, obj
);
2361 right_obj
= read_obj(s
, table
);
2363 op
= find_matching_bin_op(op
, obj
, right_obj
);
2366 isl_die(s
->ctx
, isl_error_invalid
,
2367 "no such binary operator defined on given operands",
2370 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2373 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2374 right_obj
= read_obj(s
, table
);
2375 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2380 free_obj(right_obj
);
2382 obj
.type
= isl_obj_none
;
2387 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2388 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2390 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2391 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2393 struct isl_obj obj
= { isl_obj_none
, NULL
};
2397 struct isc_bin_op
*op
= NULL
;
2402 if (isl_stream_is_empty(s
))
2405 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2406 return source_file(s
, table
, p
);
2408 assign
= is_assign(s
);
2410 lhs
= isl_stream_read_ident_if_available(s
);
2411 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2413 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2418 obj
= read_expr(s
, table
);
2419 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2420 fprintf(stderr
, "Interrupted\n");
2421 isl_ctx_reset_error(s
->ctx
);
2423 if (isl_stream_eat(s
, ';'))
2427 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2428 p
= obj
.type
->print(p
, obj
.v
);
2429 p
= isl_printer_end_line(p
);
2434 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2435 static int count
= 0;
2436 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2437 lhs
= strdup(buf
+ 1);
2439 p
= isl_printer_print_str(p
, buf
);
2440 p
= isl_printer_print_str(p
, " := ");
2441 p
= obj
.type
->print(p
, obj
.v
);
2442 p
= isl_printer_end_line(p
);
2444 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2449 isl_stream_flush_tokens(s
);
2450 isl_stream_skip_line(s
);
2456 int free_cb(void **entry
, void *user
)
2458 struct isl_named_obj
*named
= *entry
;
2460 free_obj(named
->obj
);
2467 static void register_named_ops(struct isl_stream
*s
)
2471 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2472 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2473 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2476 for (i
= 0; ; ++i
) {
2477 if (!named_un_ops
[i
].name
)
2479 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2480 named_un_ops
[i
].name
);
2481 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2484 for (i
= 0; ; ++i
) {
2485 if (!named_bin_ops
[i
].name
)
2487 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2488 named_bin_ops
[i
].name
);
2489 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2493 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2494 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2496 struct isl_token
*tok
;
2497 struct isl_stream
*s_file
;
2500 tok
= isl_stream_next_token(s
);
2501 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2502 isl_stream_error(s
, tok
, "expecting filename");
2503 isl_token_free(tok
);
2507 file
= fopen(tok
->u
.s
, "r");
2508 isl_token_free(tok
);
2509 isl_assert(s
->ctx
, file
, return p
);
2511 s_file
= isl_stream_new_file(s
->ctx
, file
);
2517 register_named_ops(s_file
);
2519 while (!s_file
->eof
)
2520 p
= read_line(s_file
, table
, p
, 0);
2522 isl_stream_free(s_file
);
2525 isl_stream_eat(s
, ';');
2530 int main(int argc
, char **argv
)
2532 struct isl_ctx
*ctx
;
2533 struct isl_stream
*s
;
2534 struct isl_hash_table
*table
;
2535 struct iscc_options
*options
;
2537 int tty
= isatty(0);
2539 options
= iscc_options_new_with_defaults();
2541 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
2543 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
2544 s
= isl_stream_new_file(ctx
, stdin
);
2546 table
= isl_hash_table_alloc(ctx
, 10);
2548 p
= isl_printer_to_file(ctx
, stdout
);
2549 p
= isl_printer_set_output_format(p
, options
->format
);
2552 register_named_ops(s
);
2554 install_signal_handler(ctx
);
2556 while (p
&& !s
->eof
) {
2557 isl_ctx_resume(ctx
);
2558 p
= read_line(s
, table
, p
, tty
);
2561 remove_signal_handler(ctx
);
2563 isl_printer_free(p
);
2564 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2565 isl_hash_table_free(ctx
, table
);