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 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
117 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
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")
125 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
126 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
128 static void *isl_obj_bool_copy(void *v
)
133 static void isl_obj_bool_free(void *v
)
137 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
140 if (v
== &isl_bool_true
)
141 return isl_printer_print_str(p
, "True");
142 else if (v
== &isl_bool_false
)
143 return isl_printer_print_str(p
, "False");
145 return isl_printer_print_str(p
, "Error");
148 static void *isl_obj_bool_add(void *v1
, void *v2
)
153 struct isl_obj_vtable isl_obj_bool_vtable
= {
159 #define isl_obj_bool (&isl_obj_bool_vtable)
161 int *isl_bool_from_int(int res
)
163 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
166 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
167 __isl_take isl_union_map
*map2
)
169 return isl_union_map_is_subset(map2
, map1
);
171 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
172 __isl_take isl_union_set
*set2
)
174 return isl_union_set_is_subset(set2
, set1
);
177 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
178 __isl_take isl_union_map
*map2
)
180 return isl_union_map_is_strict_subset(map2
, map1
);
182 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
183 __isl_take isl_union_set
*set2
)
185 return isl_union_set_is_strict_subset(set2
, set1
);
188 extern struct isl_obj_vtable isl_obj_list_vtable
;
189 #define isl_obj_list (&isl_obj_list_vtable)
191 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
192 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
194 enum isl_token_type op
;
200 isc_bin_test_fn test
;
203 struct isc_named_bin_op
{
205 struct isc_bin_op op
;
209 isl_union_pw_qpolynomial
*upwqp
;
210 isl_union_pw_qpolynomial
*res
;
213 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
215 struct iscc_at
*at
= (struct iscc_at
*) user
;
219 set
= isl_set_from_point(isl_point_copy(pnt
));
220 qp
= isl_union_pw_qpolynomial_eval(
221 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
223 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
224 isl_union_pw_qpolynomial_from_pw_qpolynomial(
225 isl_pw_qpolynomial_alloc(set
, qp
)));
230 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
231 __isl_take isl_union_pw_qpolynomial
*upwqp
,
232 __isl_take isl_union_set
*uset
)
237 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
239 isl_union_set_foreach_point(uset
, eval_at
, &at
);
241 isl_union_pw_qpolynomial_free(upwqp
);
242 isl_union_set_free(uset
);
247 struct iscc_fold_at
{
248 isl_union_pw_qpolynomial_fold
*upwf
;
249 isl_union_pw_qpolynomial
*res
;
252 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
254 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
258 set
= isl_set_from_point(isl_point_copy(pnt
));
259 qp
= isl_union_pw_qpolynomial_fold_eval(
260 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
262 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
263 isl_union_pw_qpolynomial_from_pw_qpolynomial(
264 isl_pw_qpolynomial_alloc(set
, qp
)));
269 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
270 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
271 __isl_take isl_union_set
*uset
)
273 struct iscc_fold_at at
;
276 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
278 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
280 isl_union_pw_qpolynomial_fold_free(upwf
);
281 isl_union_set_free(uset
);
286 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
287 __isl_take isl_union_pw_qpolynomial
*upwqp
,
288 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
290 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
294 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
295 __isl_take isl_union_map
*umap
,
296 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
299 struct isl_list
*list
;
302 ctx
= isl_union_map_get_ctx(umap
);
303 list
= isl_list_alloc(ctx
, 2);
307 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
308 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
310 list
->obj
[1].type
= isl_obj_bool
;
311 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
312 if (tight
< 0 || !list
->obj
[0].v
)
317 isl_union_map_free(umap
);
318 isl_union_pw_qpolynomial_fold_free(upwf
);
324 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
325 __isl_take isl_union_set
*uset
,
326 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
329 struct isl_list
*list
;
332 ctx
= isl_union_set_get_ctx(uset
);
333 list
= isl_list_alloc(ctx
, 2);
337 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
338 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
340 list
->obj
[1].type
= isl_obj_bool
;
341 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
342 if (tight
< 0 || !list
->obj
[0].v
)
347 isl_union_set_free(uset
);
348 isl_union_pw_qpolynomial_fold_free(upwf
);
354 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_int_mul(
355 __isl_take isl_union_pw_qpolynomial
*upwqp
, __isl_take isl_int_obj
*i
)
363 isl_int_obj_get_int(i
, &v
);
364 upwqp
= isl_union_pw_qpolynomial_mul_isl_int(upwqp
, v
);
371 isl_union_pw_qpolynomial_free(upwqp
);
375 static __isl_give isl_union_pw_qpolynomial
*int_union_pw_qpolynomial_mul(
376 __isl_take isl_int_obj
*i
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
378 return union_pw_qpolynomial_int_mul(upwqp
, i
);
381 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_fold_int_mul(
382 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
383 __isl_take isl_int_obj
*i
)
391 isl_int_obj_get_int(i
, &v
);
392 upwf
= isl_union_pw_qpolynomial_fold_mul_isl_int(upwf
, v
);
399 isl_union_pw_qpolynomial_fold_free(upwf
);
403 static __isl_give isl_union_pw_qpolynomial_fold
*int_union_pw_qpolynomial_fold_mul(
404 __isl_take isl_int_obj
*i
,
405 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
407 return union_pw_qpolynomial_fold_int_mul(upwf
, i
);
410 struct isc_bin_op bin_ops
[] = {
411 { '+', isl_obj_int
, isl_obj_int
, isl_obj_int
,
412 (isc_bin_op_fn
) &isl_int_obj_add
},
413 { '-', isl_obj_int
, isl_obj_int
, isl_obj_int
,
414 (isc_bin_op_fn
) &isl_int_obj_sub
},
415 { '*', isl_obj_int
, isl_obj_int
, isl_obj_int
,
416 (isc_bin_op_fn
) &isl_int_obj_mul
},
417 { '+', isl_obj_union_set
, isl_obj_union_set
,
419 (isc_bin_op_fn
) &isl_union_set_union
},
420 { '+', isl_obj_union_map
, isl_obj_union_map
,
422 (isc_bin_op_fn
) &isl_union_map_union
},
423 { '-', isl_obj_union_set
, isl_obj_union_set
,
425 (isc_bin_op_fn
) &isl_union_set_subtract
},
426 { '-', isl_obj_union_map
, isl_obj_union_map
,
428 (isc_bin_op_fn
) &isl_union_map_subtract
},
429 { '*', isl_obj_union_set
, isl_obj_union_set
,
431 (isc_bin_op_fn
) &isl_union_set_intersect
},
432 { '*', isl_obj_union_map
, isl_obj_union_map
,
434 (isc_bin_op_fn
) &isl_union_map_intersect
},
435 { '*', isl_obj_union_map
, isl_obj_union_set
,
437 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
438 { '.', isl_obj_union_map
, isl_obj_union_map
,
440 (isc_bin_op_fn
) &isl_union_map_apply_range
},
441 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
442 isl_obj_union_pw_qpolynomial
,
443 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
444 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
446 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
447 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
449 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
450 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
451 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
452 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
453 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
454 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
456 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
457 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
459 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
460 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
462 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
463 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
465 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
466 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
468 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
469 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
471 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
472 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
475 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
476 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
479 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
480 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
482 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
483 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
485 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
486 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
488 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
489 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
491 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
492 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
494 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
495 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
497 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
498 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
500 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
501 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
503 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
504 { '.', isl_obj_union_pw_qpolynomial_fold
,
505 isl_obj_union_pw_qpolynomial_fold
,
506 isl_obj_union_pw_qpolynomial_fold
,
507 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
508 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
509 isl_obj_union_pw_qpolynomial
,
510 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
511 { '+', isl_obj_union_pw_qpolynomial
,
512 isl_obj_union_pw_qpolynomial_fold
,
513 isl_obj_union_pw_qpolynomial_fold
,
514 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
515 { '+', isl_obj_union_pw_qpolynomial_fold
,
516 isl_obj_union_pw_qpolynomial
,
517 isl_obj_union_pw_qpolynomial_fold
,
518 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
519 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
520 isl_obj_union_pw_qpolynomial
,
521 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
522 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial
,
523 isl_obj_union_pw_qpolynomial
,
524 (isc_bin_op_fn
) &int_union_pw_qpolynomial_mul
},
525 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_int
,
526 isl_obj_union_pw_qpolynomial
,
527 (isc_bin_op_fn
) &union_pw_qpolynomial_int_mul
},
528 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial_fold
,
529 isl_obj_union_pw_qpolynomial_fold
,
530 (isc_bin_op_fn
) &int_union_pw_qpolynomial_fold_mul
},
531 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_int
,
532 isl_obj_union_pw_qpolynomial_fold
,
533 (isc_bin_op_fn
) &union_pw_qpolynomial_fold_int_mul
},
534 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
535 isl_obj_union_pw_qpolynomial
,
536 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
537 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
538 isl_obj_union_pw_qpolynomial
,
539 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
540 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
541 isl_obj_union_pw_qpolynomial_fold
,
542 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
543 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
544 isl_obj_union_pw_qpolynomial
,
545 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
546 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
547 isl_obj_union_pw_qpolynomial
,
548 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
549 { '%', isl_obj_union_set
, isl_obj_union_set
,
551 (isc_bin_op_fn
) &isl_union_set_gist
},
552 { '%', isl_obj_union_map
, isl_obj_union_map
,
554 (isc_bin_op_fn
) &isl_union_map_gist
},
555 { '%', isl_obj_union_map
, isl_obj_union_set
,
557 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
558 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
559 isl_obj_union_pw_qpolynomial
,
560 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
561 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
562 isl_obj_union_pw_qpolynomial_fold
,
563 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
564 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
565 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
566 { .test
= (isc_bin_test_fn
)
567 &isl_union_pw_qpolynomial_plain_is_equal
} },
568 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
569 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
570 { .test
= (isc_bin_test_fn
)
571 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
572 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
573 (isc_bin_op_fn
) &isl_str_concat
},
577 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
578 __isl_take isl_union_map
*umap2
)
580 return isl_union_map_apply_range(umap2
, umap1
);
583 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
584 __isl_take isl_union_pw_qpolynomial
*upwqp
,
585 __isl_take isl_union_map
*umap
)
587 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
590 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
591 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
592 __isl_take isl_union_map
*umap
)
594 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
597 struct isc_named_bin_op named_bin_ops
[] = {
598 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
600 (isc_bin_op_fn
) &map_after_map
} },
601 { "after", { -1, isl_obj_union_pw_qpolynomial
,
602 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
603 (isc_bin_op_fn
) &qpolynomial_after_map
} },
604 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
605 isl_obj_union_map
, isl_obj_list
,
606 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
607 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
609 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
610 { "before", { -1, isl_obj_union_map
,
611 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
612 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
613 { "before", { -1, isl_obj_union_map
,
614 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
615 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
616 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
618 (isc_bin_op_fn
) &isl_union_set_product
} },
619 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
621 (isc_bin_op_fn
) &isl_union_map_product
} },
625 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
627 return isl_set_from_basic_set(isl_union_set_sample(uset
));
630 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
632 return isl_map_from_basic_map(isl_union_map_sample(umap
));
635 static __isl_give
struct isl_list
*union_map_power(
636 __isl_take isl_union_map
*umap
)
639 struct isl_list
*list
;
642 ctx
= isl_union_map_get_ctx(umap
);
643 list
= isl_list_alloc(ctx
, 2);
647 list
->obj
[0].type
= isl_obj_union_map
;
648 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
649 list
->obj
[1].type
= isl_obj_bool
;
650 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
651 if (exact
< 0 || !list
->obj
[0].v
)
656 isl_union_map_free(umap
);
662 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
663 __isl_take isl_union_pw_qpolynomial
*upwqp
)
666 struct isl_list
*list
;
669 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
670 list
= isl_list_alloc(ctx
, 2);
674 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
675 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
676 isl_fold_max
, &tight
);
677 list
->obj
[1].type
= isl_obj_bool
;
678 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
679 if (tight
< 0 || !list
->obj
[0].v
)
684 isl_union_pw_qpolynomial_free(upwqp
);
691 void *map_codegen(void *arg
)
694 isl_union_map
*umap
= (isl_union_map
*)arg
;
695 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
697 CloogOptions
*options
;
698 CloogDomain
*context
;
699 CloogUnionDomain
*ud
;
701 struct clast_stmt
*stmt
;
703 state
= cloog_isl_state_malloc(ctx
);
704 options
= cloog_options_malloc(state
);
705 options
->language
= CLOOG_LANGUAGE_C
;
706 options
->strides
= 1;
709 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
711 dim
= isl_union_map_get_space(umap
);
712 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
714 input
= cloog_input_alloc(context
, ud
);
716 stmt
= cloog_clast_create_from_input(input
, options
);
717 clast_pprint(stdout
, stmt
, 0, options
);
718 cloog_clast_free(stmt
);
721 cloog_options_free(options
);
722 cloog_state_free(state
);
723 isl_union_map_free(umap
);
727 void *set_codegen(void *arg
)
730 isl_union_set
*uset
= (isl_union_set
*)arg
;
732 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
734 CloogOptions
*options
;
735 CloogDomain
*context
;
736 CloogUnionDomain
*ud
;
738 struct clast_stmt
*stmt
;
740 if (isl_union_set_n_set(uset
) > 1)
741 isl_die(ctx
, isl_error_invalid
,
742 "code generation for more than one domain "
743 "requires a schedule", goto error
);
745 state
= cloog_isl_state_malloc(ctx
);
746 options
= cloog_options_malloc(state
);
747 options
->language
= CLOOG_LANGUAGE_C
;
748 options
->strides
= 1;
751 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
752 ud
= cloog_union_domain_from_isl_set(set
);
754 dim
= isl_union_set_get_space(uset
);
755 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
757 input
= cloog_input_alloc(context
, ud
);
759 stmt
= cloog_clast_create_from_input(input
, options
);
760 clast_pprint(stdout
, stmt
, 0, options
);
761 cloog_clast_free(stmt
);
763 cloog_options_free(options
);
764 cloog_state_free(state
);
766 isl_union_set_free(uset
);
772 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
775 struct isl_list
*list
;
776 struct pet_scop
*scop
;
777 isl_union_map
*sched
, *reads
, *writes
;
778 isl_union_set
*domain
;
779 struct iscc_options
*options
;
785 options
= isl_ctx_peek_iscc_options(ctx
);
786 if (!options
|| !options
->io
) {
788 isl_die(ctx
, isl_error_invalid
,
789 "parse_file operation not allowed", return NULL
);
792 list
= isl_list_alloc(ctx
, 4);
796 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
, 1);
797 domain
= pet_scop_collect_domains(scop
);
798 sched
= pet_scop_collect_schedule(scop
);
799 reads
= pet_scop_collect_reads(scop
);
800 writes
= pet_scop_collect_writes(scop
);
803 list
->obj
[0].type
= isl_obj_union_set
;
804 list
->obj
[0].v
= domain
;
805 list
->obj
[1].type
= isl_obj_union_map
;
806 list
->obj
[1].v
= writes
;
807 list
->obj
[2].type
= isl_obj_union_map
;
808 list
->obj
[2].v
= reads
;
809 list
->obj
[3].type
= isl_obj_union_map
;
810 list
->obj
[3].v
= sched
;
812 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
813 !list
->obj
[2].v
|| !list
->obj
[3].v
)
825 static int add_point(__isl_take isl_point
*pnt
, void *user
)
827 isl_union_set
**scan
= (isl_union_set
**) user
;
829 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
834 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
838 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
840 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
841 isl_union_set_free(scan
);
845 isl_union_set_free(uset
);
849 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
851 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
854 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
855 __isl_take isl_union_pw_qpolynomial
*upwqp
)
857 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
860 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
861 __isl_take isl_union_pw_qpolynomial
*upwqp
)
863 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
866 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
867 __isl_take isl_union_pw_qpolynomial
*upwqp
)
869 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
872 typedef void *(*isc_un_op_fn
)(void *arg
);
874 enum isl_token_type op
;
879 struct isc_named_un_op
{
883 struct isc_named_un_op named_un_ops
[] = {
884 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
885 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
886 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
887 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
888 {"card", { -1, isl_obj_union_set
,
889 isl_obj_union_pw_qpolynomial
,
890 (isc_un_op_fn
) &isl_union_set_card
} },
891 {"card", { -1, isl_obj_union_map
,
892 isl_obj_union_pw_qpolynomial
,
893 (isc_un_op_fn
) &isl_union_map_card
} },
894 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
895 (isc_un_op_fn
) &isl_union_set_coalesce
} },
896 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
897 (isc_un_op_fn
) &isl_union_map_coalesce
} },
898 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
899 isl_obj_union_pw_qpolynomial
,
900 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
901 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
902 isl_obj_union_pw_qpolynomial_fold
,
903 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
905 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
907 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
910 {"coefficients", { -1, isl_obj_union_set
,
912 (isc_un_op_fn
) &isl_union_set_coefficients
} },
913 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
914 (isc_un_op_fn
) &isl_union_set_solutions
} },
915 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
916 (isc_un_op_fn
) &isl_union_map_deltas
} },
917 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
918 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
919 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
920 (isc_un_op_fn
) &isl_union_map_domain
} },
921 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
922 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
923 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
925 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
926 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
927 (isc_un_op_fn
) &isl_union_map_domain
} },
928 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
930 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
931 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
933 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
934 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
935 (isc_un_op_fn
) &isl_union_map_domain_map
} },
936 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
937 (isc_un_op_fn
) &isl_union_map_range
} },
938 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
939 (isc_un_op_fn
) &isl_union_map_range
} },
940 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
941 (isc_un_op_fn
) &isl_union_map_range_map
} },
942 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
943 (isc_un_op_fn
) &isl_union_set_identity
} },
944 {"lattice_width", { -1, isl_obj_union_set
,
945 isl_obj_union_pw_qpolynomial
,
946 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
947 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
948 (isc_un_op_fn
) &isl_union_map_lexmin
} },
949 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
950 (isc_un_op_fn
) &isl_union_map_lexmax
} },
951 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
952 (isc_un_op_fn
) &isl_union_set_lexmin
} },
953 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
954 (isc_un_op_fn
) &isl_union_set_lexmax
} },
955 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
956 (isc_un_op_fn
) &isl_union_set_lift
} },
957 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
958 (isc_un_op_fn
) &isl_union_map_params
} },
959 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
960 (isc_un_op_fn
) &isl_union_set_params
} },
961 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
962 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
963 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
964 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
965 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
966 isl_obj_union_pw_qpolynomial
,
967 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
968 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
969 isl_obj_union_pw_qpolynomial
,
970 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
971 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
972 isl_obj_union_pw_qpolynomial
,
973 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
975 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
976 (isc_un_op_fn
) &parse
} },
978 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
979 (isc_un_op_fn
) &union_map_power
} },
980 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
981 (isc_un_op_fn
) &union_set_sample
} },
982 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
983 (isc_un_op_fn
) &union_map_sample
} },
984 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
985 (isc_un_op_fn
) &union_set_scan
} },
986 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
987 (isc_un_op_fn
) &union_map_scan
} },
988 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
989 isl_obj_union_pw_qpolynomial
,
990 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
991 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
992 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
993 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
994 (isc_un_op_fn
) &isl_union_set_unwrap
} },
995 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
996 (isc_un_op_fn
) &isl_union_map_wrap
} },
997 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
998 (isc_un_op_fn
) &isl_union_map_zip
} },
1002 struct isl_named_obj
{
1007 static void free_obj(struct isl_obj obj
)
1009 obj
.type
->free(obj
.v
);
1012 static int same_name(const void *entry
, const void *val
)
1014 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1016 return !strcmp(named
->name
, val
);
1019 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1020 char *name
, struct isl_obj obj
)
1022 struct isl_hash_table_entry
*entry
;
1024 struct isl_named_obj
*named
;
1026 name_hash
= isl_hash_string(isl_hash_init(), name
);
1027 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1031 named
= entry
->data
;
1032 free_obj(named
->obj
);
1035 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1039 entry
->data
= named
;
1050 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1051 struct isl_hash_table
*table
, char *name
)
1053 struct isl_obj obj
= { isl_obj_none
, NULL
};
1054 struct isl_hash_table_entry
*entry
;
1057 name_hash
= isl_hash_string(isl_hash_init(), name
);
1058 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1060 struct isl_named_obj
*named
;
1061 named
= entry
->data
;
1063 } else if (isdigit(name
[0]))
1064 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1066 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1069 obj
.v
= obj
.type
->copy(obj
.v
);
1073 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1075 if (obj
.type
== super
)
1077 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1079 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1081 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1082 super
== isl_obj_union_pw_qpolynomial
)
1084 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1085 super
== isl_obj_union_pw_qpolynomial_fold
)
1087 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1089 if (obj
.type
== isl_obj_list
) {
1090 struct isl_list
*list
= obj
.v
;
1091 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1092 return is_subtype(list
->obj
[0], super
);
1094 if (super
== isl_obj_str
)
1099 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1101 struct isl_list
*list
= obj
.v
;
1104 obj
.v
= obj
.type
->copy(obj
.v
);
1106 isl_list_free(list
);
1111 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1114 if (obj
.type
== type
)
1116 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1117 obj
.type
= isl_obj_union_map
;
1118 obj
.v
= isl_union_map_from_map(obj
.v
);
1121 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1122 obj
.type
= isl_obj_union_set
;
1123 obj
.v
= isl_union_set_from_set(obj
.v
);
1126 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1127 type
== isl_obj_union_pw_qpolynomial
) {
1128 obj
.type
= isl_obj_union_pw_qpolynomial
;
1129 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1132 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1133 type
== isl_obj_union_pw_qpolynomial_fold
) {
1134 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1135 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1138 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1139 if (type
== isl_obj_union_map
) {
1140 obj
.type
= isl_obj_union_map
;
1143 if (type
== isl_obj_union_pw_qpolynomial
) {
1144 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1145 isl_union_set_free(obj
.v
);
1146 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1147 obj
.type
= isl_obj_union_pw_qpolynomial
;
1150 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1151 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1152 isl_union_set_free(obj
.v
);
1153 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1155 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1159 if (obj
.type
== isl_obj_list
) {
1160 struct isl_list
*list
= obj
.v
;
1161 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1162 return convert(ctx
, obj_at(obj
, 0), type
);
1164 if (type
== isl_obj_str
) {
1169 p
= isl_printer_to_str(ctx
);
1172 p
= obj
.type
->print(p
, obj
.v
);
1173 s
= isl_printer_get_str(p
);
1174 isl_printer_free(p
);
1176 str
= isl_str_from_string(ctx
, s
);
1181 obj
.type
= isl_obj_str
;
1187 obj
.type
= isl_obj_none
;
1192 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1196 struct isl_token
*tok
;
1198 tok
= isl_stream_next_token(s
);
1202 for (i
= 0; ; ++i
) {
1205 if (bin_ops
[i
].op
!= tok
->type
)
1207 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1210 isl_token_free(tok
);
1214 for (i
= 0; ; ++i
) {
1215 if (!named_bin_ops
[i
].name
)
1217 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1219 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1222 isl_token_free(tok
);
1223 return &named_bin_ops
[i
].op
;
1226 isl_stream_push_token(s
, tok
);
1231 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1234 struct isl_token
*tok
;
1236 tok
= isl_stream_next_token(s
);
1240 for (i
= 0; ; ++i
) {
1241 if (!named_un_ops
[i
].name
)
1243 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1246 isl_token_free(tok
);
1247 return &named_un_ops
[i
].op
;
1250 isl_stream_push_token(s
, tok
);
1255 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1260 for (i
= 0; ; ++i
) {
1261 if (!named_un_ops
[i
].name
)
1263 if (named_un_ops
[i
].op
.op
!= like
->op
)
1265 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1268 return &named_un_ops
[i
].op
;
1274 static int is_assign(struct isl_stream
*s
)
1276 struct isl_token
*tok
;
1277 struct isl_token
*tok2
;
1280 tok
= isl_stream_next_token(s
);
1283 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1284 isl_stream_push_token(s
, tok
);
1288 tok2
= isl_stream_next_token(s
);
1290 isl_stream_push_token(s
, tok
);
1293 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1294 isl_stream_push_token(s
, tok2
);
1295 isl_stream_push_token(s
, tok
);
1300 static struct isl_obj
read_obj(struct isl_stream
*s
,
1301 struct isl_hash_table
*table
);
1302 static struct isl_obj
read_expr(struct isl_stream
*s
,
1303 struct isl_hash_table
*table
);
1305 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1306 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1308 struct isl_obj obj
= { isl_obj_none
, NULL
};
1310 obj
= read_obj(s
, table
);
1314 op
= find_matching_un_op(op
, obj
);
1317 isl_die(s
->ctx
, isl_error_invalid
,
1318 "no such unary operator defined on given operand",
1321 obj
= convert(s
->ctx
, obj
, op
->arg
);
1322 obj
.v
= op
->fn(obj
.v
);
1328 obj
.type
= isl_obj_none
;
1333 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1335 struct isl_list
*list
;
1338 if (obj
.type
!= isl_obj_union_map
)
1339 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1340 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1341 list
= isl_list_alloc(ctx
, 2);
1345 list
->obj
[0].type
= isl_obj_union_map
;
1346 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1347 list
->obj
[1].type
= isl_obj_bool
;
1348 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1350 obj
.type
= isl_obj_list
;
1351 if (exact
< 0 || !list
->obj
[0].v
)
1357 obj
.type
= isl_obj_none
;
1362 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1364 struct isl_list
*list
= obj
.v
;
1365 struct isl_token
*tok
;
1368 tok
= isl_stream_next_token(s
);
1369 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1370 isl_stream_error(s
, tok
, "expecting index");
1372 isl_stream_push_token(s
, tok
);
1375 i
= isl_int_get_si(tok
->u
.v
);
1376 isl_token_free(tok
);
1377 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1378 if (isl_stream_eat(s
, ']'))
1381 return obj_at(obj
, i
);
1384 obj
.type
= isl_obj_none
;
1389 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1390 struct isl_hash_table
*table
)
1394 obj
= read_expr(s
, table
);
1395 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1396 is_subtype(obj
, isl_obj_union_map
), goto error
);
1398 if (obj
.type
== isl_obj_list
) {
1399 struct isl_list
*list
= obj
.v
;
1400 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1401 obj
= obj_at(obj
, 0);
1403 if (obj
.type
== isl_obj_set
)
1404 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1405 else if (obj
.type
== isl_obj_map
)
1406 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1407 if (obj
.type
== isl_obj_union_set
) {
1408 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1410 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1414 if (isl_stream_eat(s
, ')'))
1419 isl_union_map_free(umap
);
1422 obj
.type
= isl_obj_none
;
1427 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1428 __isl_take isl_union_set
*uset
)
1430 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1431 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1433 obj
.type
= isl_obj_list
;
1434 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1439 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1440 __isl_take isl_union_map
*umap
)
1442 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1443 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1445 obj
.type
= isl_obj_list
;
1446 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1451 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1452 struct isl_obj obj
, struct isl_hash_table
*table
)
1456 arg
= read_expr(s
, table
);
1457 if (!is_subtype(arg
, isl_obj_union_map
) &&
1458 !is_subtype(arg
, isl_obj_union_set
))
1459 isl_die(s
->ctx
, isl_error_invalid
,
1460 "expecting set of map argument", goto error
);
1462 if (arg
.type
== isl_obj_list
) {
1463 struct isl_list
*list
= arg
.v
;
1464 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1465 arg
= obj_at(arg
, 0);
1467 if (arg
.type
== isl_obj_set
)
1468 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1469 else if (arg
.type
== isl_obj_map
)
1470 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1471 if (arg
.type
== isl_obj_union_set
)
1472 obj
= apply_fun_set(obj
, arg
.v
);
1474 obj
= apply_fun_map(obj
, arg
.v
);
1478 if (isl_stream_eat(s
, ')'))
1486 obj
.type
= isl_obj_none
;
1491 struct add_vertex_data
{
1492 struct isl_list
*list
;
1496 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1498 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1499 isl_basic_set
*expr
;
1501 expr
= isl_vertex_get_expr(vertex
);
1503 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1504 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1507 isl_vertex_free(vertex
);
1512 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1515 isl_basic_set
*hull
;
1516 isl_vertices
*vertices
= NULL
;
1517 struct isl_list
*list
= NULL
;
1519 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1521 set
= isl_set_remove_divs(set
);
1522 hull
= isl_set_convex_hull(set
);
1523 vertices
= isl_basic_set_compute_vertices(hull
);
1524 isl_basic_set_free(hull
);
1528 ctx
= isl_vertices_get_ctx(vertices
);
1529 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1534 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1536 data
->list
= isl_list_concat(list
, data
->list
);
1538 isl_vertices_free(vertices
);
1543 isl_vertices_free(vertices
);
1547 static struct isl_obj
vertices(struct isl_stream
*s
,
1548 struct isl_hash_table
*table
)
1552 struct isl_list
*list
= NULL
;
1553 isl_union_set
*uset
;
1554 struct add_vertex_data data
= { NULL
};
1556 obj
= read_expr(s
, table
);
1557 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1558 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1562 ctx
= isl_union_set_get_ctx(uset
);
1563 list
= isl_list_alloc(ctx
, 0);
1569 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1572 isl_union_set_free(uset
);
1574 obj
.type
= isl_obj_list
;
1579 isl_union_set_free(uset
);
1580 isl_list_free(data
.list
);
1582 obj
.type
= isl_obj_none
;
1587 static struct isl_obj
type_of(struct isl_stream
*s
,
1588 struct isl_hash_table
*table
)
1592 const char *type
= "unknown";
1594 obj
= read_expr(s
, table
);
1596 if (obj
.type
== isl_obj_map
||
1597 obj
.type
== isl_obj_union_map
)
1599 if (obj
.type
== isl_obj_set
||
1600 obj
.type
== isl_obj_union_set
)
1602 if (obj
.type
== isl_obj_pw_qpolynomial
||
1603 obj
.type
== isl_obj_union_pw_qpolynomial
)
1604 type
= "piecewise quasipolynomial";
1605 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1606 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1607 type
= "piecewise quasipolynomial fold";
1608 if (obj
.type
== isl_obj_list
)
1610 if (obj
.type
== isl_obj_bool
)
1612 if (obj
.type
== isl_obj_str
)
1614 if (obj
.type
== isl_obj_int
)
1618 obj
.type
= isl_obj_str
;
1619 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1624 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1625 struct isl_hash_table
*table
)
1629 obj
= read_obj(s
, table
);
1630 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1631 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1638 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1639 struct isl_hash_table
*table
)
1643 obj
= read_obj(s
, table
);
1644 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1645 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1652 static struct isl_obj
last_any(struct isl_stream
*s
,
1653 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1654 __isl_take isl_union_map
*may_source
)
1656 struct isl_obj obj
= { isl_obj_none
, NULL
};
1657 isl_union_map
*sink
= NULL
;
1658 isl_union_map
*schedule
= NULL
;
1659 isl_union_map
*may_dep
;
1660 isl_union_map
*must_dep
;
1662 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1665 sink
= read_map(s
, table
);
1669 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1672 schedule
= read_map(s
, table
);
1676 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1677 schedule
, &must_dep
, &may_dep
,
1681 obj
.type
= isl_obj_union_map
;
1682 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1686 isl_union_map_free(may_source
);
1687 isl_union_map_free(must_source
);
1688 isl_union_map_free(sink
);
1689 isl_union_map_free(schedule
);
1691 obj
.type
= isl_obj_none
;
1696 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1698 struct isl_obj obj
= { isl_obj_none
, NULL
};
1699 isl_union_map
*must_source
= NULL
;
1700 isl_union_map
*may_source
= NULL
;
1701 isl_union_map
*sink
= NULL
;
1702 isl_union_map
*schedule
= NULL
;
1703 isl_union_map
*may_dep
;
1705 may_source
= read_map(s
, table
);
1709 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1710 must_source
= read_map(s
, table
);
1713 return last_any(s
, table
, must_source
, may_source
);
1716 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1719 sink
= read_map(s
, table
);
1723 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1726 schedule
= read_map(s
, table
);
1730 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1731 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1732 schedule
, NULL
, &may_dep
,
1736 obj
.type
= isl_obj_union_map
;
1741 isl_union_map_free(may_source
);
1742 isl_union_map_free(must_source
);
1743 isl_union_map_free(sink
);
1744 isl_union_map_free(schedule
);
1746 obj
.type
= isl_obj_none
;
1751 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1753 struct isl_obj obj
= { isl_obj_none
, NULL
};
1754 struct isl_list
*list
= NULL
;
1755 isl_union_map
*must_source
= NULL
;
1756 isl_union_map
*may_source
= NULL
;
1757 isl_union_map
*sink
= NULL
;
1758 isl_union_map
*schedule
= NULL
;
1759 isl_union_map
*must_dep
;
1760 isl_union_map
*must_no_source
;
1762 must_source
= read_map(s
, table
);
1766 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1767 may_source
= read_map(s
, table
);
1770 return last_any(s
, table
, must_source
, may_source
);
1773 list
= isl_list_alloc(s
->ctx
, 2);
1777 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1780 sink
= read_map(s
, table
);
1784 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1787 schedule
= read_map(s
, table
);
1791 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1792 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1793 schedule
, &must_dep
, NULL
,
1794 &must_no_source
, NULL
) < 0) {
1795 isl_list_free(list
);
1799 list
->obj
[0].type
= isl_obj_union_map
;
1800 list
->obj
[0].v
= must_dep
;
1801 list
->obj
[1].type
= isl_obj_union_map
;
1802 list
->obj
[1].v
= must_no_source
;
1805 obj
.type
= isl_obj_list
;
1809 isl_list_free(list
);
1810 isl_union_map_free(may_source
);
1811 isl_union_map_free(must_source
);
1812 isl_union_map_free(sink
);
1813 isl_union_map_free(schedule
);
1815 obj
.type
= isl_obj_none
;
1820 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1821 struct isl_hash_table
*table
)
1823 isl_union_set
*domain
;
1824 isl_union_map
*validity
;
1825 isl_union_map
*proximity
;
1827 domain
= read_set(s
, table
);
1831 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1832 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1835 isl_union_map
*umap
;
1836 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1837 umap
= read_map(s
, table
);
1838 validity
= isl_union_map_union(validity
, umap
);
1839 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1840 umap
= read_map(s
, table
);
1841 proximity
= isl_union_map_union(proximity
, umap
);
1846 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1849 static struct isl_obj
schedule(struct isl_stream
*s
,
1850 struct isl_hash_table
*table
)
1852 struct isl_obj obj
= { isl_obj_none
, NULL
};
1853 isl_schedule
*schedule
;
1855 schedule
= get_schedule(s
, table
);
1857 obj
.v
= isl_schedule_get_map(schedule
);
1858 obj
.type
= isl_obj_union_map
;
1860 isl_schedule_free(schedule
);
1865 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1867 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1869 struct isl_obj obj
= { isl_obj_none
, NULL
};
1870 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1871 struct isl_list
*list
;
1873 list
= isl_list_alloc(ctx
, 2);
1878 obj
.type
= isl_obj_list
;
1880 list
->obj
[0].type
= isl_obj_union_map
;
1881 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1883 if (isl_band_has_children(band
)) {
1884 isl_band_list
*children
;
1886 children
= isl_band_get_children(band
);
1887 list
->obj
[1] = band_list_to_obj_list(children
);
1889 list
->obj
[1].type
= isl_obj_list
;
1890 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1893 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1896 isl_band_free(band
);
1900 isl_band_free(band
);
1902 obj
.type
= isl_obj_none
;
1907 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1909 struct isl_obj obj
= { isl_obj_none
, NULL
};
1910 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1911 struct isl_list
*list
;
1914 n
= isl_band_list_n_band(bands
);
1915 list
= isl_list_alloc(ctx
, n
);
1920 obj
.type
= isl_obj_list
;
1922 for (i
= 0; i
< n
; ++i
) {
1925 band
= isl_band_list_get_band(bands
, i
);
1926 list
->obj
[i
] = band_to_obj_list(band
);
1927 if (!list
->obj
[i
].v
)
1931 isl_band_list_free(bands
);
1935 isl_band_list_free(bands
);
1937 obj
.type
= isl_obj_none
;
1942 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1943 struct isl_hash_table
*table
)
1945 struct isl_obj obj
= { isl_obj_none
, NULL
};
1946 isl_schedule
*schedule
;
1947 isl_band_list
*roots
;
1949 schedule
= get_schedule(s
, table
);
1953 roots
= isl_schedule_get_band_forest(schedule
);
1954 isl_schedule_free(schedule
);
1956 return band_list_to_obj_list(roots
);
1959 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1961 struct isl_token
*tok
;
1963 if (isl_stream_eat_if_available(s
, '+'))
1964 return transitive_closure(s
->ctx
, obj
);
1966 tok
= isl_stream_next_token(s
);
1967 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1968 isl_stream_error(s
, tok
, "expecting -1");
1970 isl_stream_push_token(s
, tok
);
1973 isl_token_free(tok
);
1974 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1975 if (obj
.type
!= isl_obj_union_map
)
1976 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1978 obj
.v
= isl_union_map_reverse(obj
.v
);
1985 obj
.type
= isl_obj_none
;
1990 static struct isl_obj
check_assert(struct isl_stream
*s
,
1991 struct isl_hash_table
*table
)
1995 obj
= read_expr(s
, table
);
1996 if (obj
.type
!= isl_obj_bool
)
1997 isl_die(s
->ctx
, isl_error_invalid
,
1998 "expecting boolean expression", goto error
);
1999 if (obj
.v
!= &isl_bool_true
)
2000 isl_die(s
->ctx
, isl_error_unknown
,
2001 "assertion failed", abort());
2004 obj
.type
= isl_obj_none
;
2009 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2012 struct isl_token
*tok
;
2013 struct isl_stream
*s_file
;
2014 struct iscc_options
*options
;
2017 tok
= isl_stream_next_token(s
);
2018 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2019 isl_stream_error(s
, tok
, "expecting filename");
2020 isl_token_free(tok
);
2024 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2025 if (!options
|| !options
->io
) {
2026 isl_token_free(tok
);
2027 isl_die(s
->ctx
, isl_error_invalid
,
2028 "read operation not allowed", goto error
);
2031 file
= fopen(tok
->u
.s
, "r");
2032 isl_token_free(tok
);
2033 isl_assert(s
->ctx
, file
, goto error
);
2035 s_file
= isl_stream_new_file(s
->ctx
, file
);
2041 obj
= isl_stream_read_obj(s_file
);
2043 isl_stream_free(s_file
);
2048 obj
.type
= isl_obj_none
;
2053 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2054 struct isl_hash_table
*table
)
2057 struct isl_token
*tok
;
2058 struct isl_stream
*s_file
;
2059 struct iscc_options
*options
;
2063 tok
= isl_stream_next_token(s
);
2064 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2065 isl_stream_error(s
, tok
, "expecting filename");
2066 isl_token_free(tok
);
2070 obj
= read_expr(s
, table
);
2072 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2073 if (!options
|| !options
->io
) {
2074 isl_token_free(tok
);
2075 isl_die(s
->ctx
, isl_error_invalid
,
2076 "write operation not allowed", goto error
);
2079 file
= fopen(tok
->u
.s
, "w");
2080 isl_token_free(tok
);
2082 isl_die(s
->ctx
, isl_error_unknown
,
2083 "could not open file for writing", goto error
);
2085 p
= isl_printer_to_file(s
->ctx
, file
);
2086 p
= isl_printer_set_output_format(p
, options
->format
);
2087 p
= obj
.type
->print(p
, obj
.v
);
2088 p
= isl_printer_end_line(p
);
2089 isl_printer_free(p
);
2094 obj
.type
= isl_obj_none
;
2099 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2101 struct isl_token
*tok
;
2102 struct isl_obj obj
= { isl_obj_none
, NULL
};
2104 tok
= isl_stream_next_token(s
);
2107 if (tok
->type
== ISL_TOKEN_STRING
) {
2109 str
= isl_str_alloc(s
->ctx
);
2112 str
->s
= strdup(tok
->u
.s
);
2113 isl_token_free(tok
);
2115 obj
.type
= isl_obj_str
;
2117 isl_stream_push_token(s
, tok
);
2120 isl_token_free(tok
);
2124 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2126 struct isl_token
*tok
;
2127 struct isl_obj obj
= { isl_obj_none
, NULL
};
2129 tok
= isl_stream_next_token(s
);
2132 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2133 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2134 isl_token_free(tok
);
2135 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2136 obj
.type
= isl_obj_bool
;
2138 isl_stream_push_token(s
, tok
);
2141 isl_token_free(tok
);
2145 static __isl_give
char *read_ident(struct isl_stream
*s
)
2148 struct isl_token
*tok
, *tok2
;
2150 name
= isl_stream_read_ident_if_available(s
);
2154 tok
= isl_stream_next_token(s
);
2157 if (tok
->type
!= '$') {
2158 isl_stream_push_token(s
, tok
);
2161 tok2
= isl_stream_next_token(s
);
2162 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2164 isl_stream_push_token(s
, tok2
);
2165 isl_stream_push_token(s
, tok
);
2169 name
= isl_int_get_str(tok2
->u
.v
);
2170 isl_token_free(tok
);
2171 isl_token_free(tok2
);
2176 static struct isl_obj
read_list(struct isl_stream
*s
,
2177 struct isl_hash_table
*table
, struct isl_obj obj
)
2179 struct isl_list
*list
;
2181 list
= isl_list_alloc(s
->ctx
, 2);
2185 list
->obj
[1] = read_obj(s
, table
);
2187 obj
.type
= isl_obj_list
;
2189 if (!list
->obj
[1].v
)
2192 while (isl_stream_eat_if_available(s
, ',')) {
2193 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2201 obj
.type
= isl_obj_none
;
2206 static struct isl_obj
read_obj(struct isl_stream
*s
,
2207 struct isl_hash_table
*table
)
2209 struct isl_obj obj
= { isl_obj_none
, NULL
};
2211 struct isc_un_op
*op
= NULL
;
2213 obj
= read_string_if_available(s
);
2216 obj
= read_bool_if_available(s
);
2219 if (isl_stream_eat_if_available(s
, '(')) {
2220 if (isl_stream_next_token_is(s
, ')')) {
2221 obj
.type
= isl_obj_list
;
2222 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2224 obj
= read_expr(s
, table
);
2225 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2226 obj
= read_list(s
, table
, obj
);
2228 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2231 op
= read_prefix_un_op_if_available(s
);
2233 return read_un_op_expr(s
, table
, op
);
2235 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2236 return check_assert(s
, table
);
2237 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2238 return read_from_file(s
);
2239 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2240 return write_to_file(s
, table
);
2241 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2242 return vertices(s
, table
);
2243 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2244 return any(s
, table
);
2245 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2246 return last(s
, table
);
2247 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2248 return schedule(s
, table
);
2249 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2250 return schedule_forest(s
, table
);
2251 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2252 return type_of(s
, table
);
2254 name
= read_ident(s
);
2256 obj
= stored_obj(s
->ctx
, table
, name
);
2258 obj
= isl_stream_read_obj(s
);
2263 if (isl_stream_eat_if_available(s
, '^'))
2264 obj
= power(s
, obj
);
2265 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2266 obj
= obj_at_index(s
, obj
);
2267 else if (is_subtype(obj
, isl_obj_union_map
) &&
2268 isl_stream_eat_if_available(s
, '(')) {
2269 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2270 obj
= apply(s
, obj
.v
, table
);
2271 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2272 isl_stream_eat_if_available(s
, '(')) {
2273 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2274 obj
= apply_fun(s
, obj
, table
);
2275 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2276 isl_stream_eat_if_available(s
, '(')) {
2277 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2278 obj
= apply_fun(s
, obj
, table
);
2284 obj
.type
= isl_obj_none
;
2289 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2290 struct isl_obj lhs
, struct isl_obj rhs
)
2294 for (i
= 0; ; ++i
) {
2297 if (bin_ops
[i
].op
!= like
->op
)
2299 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2301 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2307 for (i
= 0; ; ++i
) {
2308 if (!named_bin_ops
[i
].name
)
2310 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2312 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2314 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2317 return &named_bin_ops
[i
].op
;
2323 static int next_is_neg_int(struct isl_stream
*s
)
2325 struct isl_token
*tok
;
2328 tok
= isl_stream_next_token(s
);
2329 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2330 isl_stream_push_token(s
, tok
);
2335 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2336 struct isl_obj lhs
, struct isl_obj rhs
)
2340 lhs
= convert(ctx
, lhs
, op
->lhs
);
2341 rhs
= convert(ctx
, rhs
, op
->rhs
);
2342 if (op
->res
!= isl_obj_bool
)
2343 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2345 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2348 obj
.v
= isl_bool_from_int(res
);
2355 static struct isl_obj
read_expr(struct isl_stream
*s
,
2356 struct isl_hash_table
*table
)
2358 struct isl_obj obj
= { isl_obj_none
, NULL
};
2359 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2361 obj
= read_obj(s
, table
);
2363 struct isc_bin_op
*op
= NULL
;
2365 op
= read_bin_op_if_available(s
, obj
);
2369 right_obj
= read_obj(s
, table
);
2371 op
= find_matching_bin_op(op
, obj
, right_obj
);
2374 isl_die(s
->ctx
, isl_error_invalid
,
2375 "no such binary operator defined on given operands",
2378 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2381 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2382 right_obj
= read_obj(s
, table
);
2383 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2388 free_obj(right_obj
);
2390 obj
.type
= isl_obj_none
;
2395 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2396 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2398 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2399 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2401 struct isl_obj obj
= { isl_obj_none
, NULL
};
2405 struct isc_bin_op
*op
= NULL
;
2410 if (isl_stream_is_empty(s
))
2413 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2414 return source_file(s
, table
, p
);
2416 assign
= is_assign(s
);
2418 lhs
= isl_stream_read_ident_if_available(s
);
2419 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2421 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2426 obj
= read_expr(s
, table
);
2427 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2428 fprintf(stderr
, "Interrupted\n");
2429 isl_ctx_reset_error(s
->ctx
);
2431 if (isl_stream_eat(s
, ';'))
2435 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2436 p
= obj
.type
->print(p
, obj
.v
);
2437 p
= isl_printer_end_line(p
);
2442 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2443 static int count
= 0;
2444 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2445 lhs
= strdup(buf
+ 1);
2447 p
= isl_printer_print_str(p
, buf
);
2448 p
= isl_printer_print_str(p
, " := ");
2449 p
= obj
.type
->print(p
, obj
.v
);
2450 p
= isl_printer_end_line(p
);
2452 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2457 isl_stream_flush_tokens(s
);
2458 isl_stream_skip_line(s
);
2464 int free_cb(void **entry
, void *user
)
2466 struct isl_named_obj
*named
= *entry
;
2468 free_obj(named
->obj
);
2475 static void register_named_ops(struct isl_stream
*s
)
2479 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2480 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2481 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2484 for (i
= 0; ; ++i
) {
2485 if (!named_un_ops
[i
].name
)
2487 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2488 named_un_ops
[i
].name
);
2489 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2492 for (i
= 0; ; ++i
) {
2493 if (!named_bin_ops
[i
].name
)
2495 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2496 named_bin_ops
[i
].name
);
2497 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2501 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2502 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2504 struct isl_token
*tok
;
2505 struct isl_stream
*s_file
;
2508 tok
= isl_stream_next_token(s
);
2509 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2510 isl_stream_error(s
, tok
, "expecting filename");
2511 isl_token_free(tok
);
2515 file
= fopen(tok
->u
.s
, "r");
2516 isl_token_free(tok
);
2517 isl_assert(s
->ctx
, file
, return p
);
2519 s_file
= isl_stream_new_file(s
->ctx
, file
);
2525 register_named_ops(s_file
);
2527 while (!s_file
->eof
)
2528 p
= read_line(s_file
, table
, p
, 0);
2530 isl_stream_free(s_file
);
2533 isl_stream_eat(s
, ';');
2538 int main(int argc
, char **argv
)
2540 struct isl_ctx
*ctx
;
2541 struct isl_stream
*s
;
2542 struct isl_hash_table
*table
;
2543 struct iscc_options
*options
;
2545 int tty
= isatty(0);
2547 options
= iscc_options_new_with_defaults();
2550 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2551 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2552 s
= isl_stream_new_file(ctx
, stdin
);
2554 table
= isl_hash_table_alloc(ctx
, 10);
2556 p
= isl_printer_to_file(ctx
, stdout
);
2557 p
= isl_printer_set_output_format(p
, options
->format
);
2560 register_named_ops(s
);
2562 install_signal_handler(ctx
);
2564 while (p
&& !s
->eof
) {
2565 isl_ctx_resume(ctx
);
2566 p
= read_line(s
, table
, p
, tty
);
2569 remove_signal_handler(ctx
);
2571 isl_printer_free(p
);
2572 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2573 isl_hash_table_free(ctx
, table
);