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_map
, isl_obj_union_set
,
558 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
559 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
560 isl_obj_union_pw_qpolynomial
,
561 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
562 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
563 isl_obj_union_pw_qpolynomial_fold
,
564 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
565 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
566 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
567 { .test
= (isc_bin_test_fn
)
568 &isl_union_pw_qpolynomial_plain_is_equal
} },
569 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
570 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
571 { .test
= (isc_bin_test_fn
)
572 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
573 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
574 (isc_bin_op_fn
) &isl_str_concat
},
578 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
579 __isl_take isl_union_map
*umap2
)
581 return isl_union_map_apply_range(umap2
, umap1
);
584 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
585 __isl_take isl_union_pw_qpolynomial
*upwqp
,
586 __isl_take isl_union_map
*umap
)
588 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
591 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
592 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
593 __isl_take isl_union_map
*umap
)
595 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
598 struct isc_named_bin_op named_bin_ops
[] = {
599 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
601 (isc_bin_op_fn
) &map_after_map
} },
602 { "after", { -1, isl_obj_union_pw_qpolynomial
,
603 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
604 (isc_bin_op_fn
) &qpolynomial_after_map
} },
605 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
606 isl_obj_union_map
, isl_obj_list
,
607 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
608 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
610 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
611 { "before", { -1, isl_obj_union_map
,
612 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
613 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
614 { "before", { -1, isl_obj_union_map
,
615 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
616 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
617 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
619 (isc_bin_op_fn
) &isl_union_set_product
} },
620 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
622 (isc_bin_op_fn
) &isl_union_map_product
} },
626 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
628 return isl_set_from_basic_set(isl_union_set_sample(uset
));
631 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
633 return isl_map_from_basic_map(isl_union_map_sample(umap
));
636 static __isl_give
struct isl_list
*union_map_power(
637 __isl_take isl_union_map
*umap
)
640 struct isl_list
*list
;
643 ctx
= isl_union_map_get_ctx(umap
);
644 list
= isl_list_alloc(ctx
, 2);
648 list
->obj
[0].type
= isl_obj_union_map
;
649 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
650 list
->obj
[1].type
= isl_obj_bool
;
651 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
652 if (exact
< 0 || !list
->obj
[0].v
)
657 isl_union_map_free(umap
);
663 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
664 __isl_take isl_union_pw_qpolynomial
*upwqp
)
667 struct isl_list
*list
;
670 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
671 list
= isl_list_alloc(ctx
, 2);
675 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
676 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
677 isl_fold_max
, &tight
);
678 list
->obj
[1].type
= isl_obj_bool
;
679 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
680 if (tight
< 0 || !list
->obj
[0].v
)
685 isl_union_pw_qpolynomial_free(upwqp
);
692 void *map_codegen(void *arg
)
695 isl_union_map
*umap
= (isl_union_map
*)arg
;
696 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
698 CloogOptions
*options
;
699 CloogDomain
*context
;
700 CloogUnionDomain
*ud
;
702 struct clast_stmt
*stmt
;
704 state
= cloog_isl_state_malloc(ctx
);
705 options
= cloog_options_malloc(state
);
706 options
->language
= CLOOG_LANGUAGE_C
;
707 options
->strides
= 1;
710 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
712 dim
= isl_union_map_get_space(umap
);
713 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
715 input
= cloog_input_alloc(context
, ud
);
717 stmt
= cloog_clast_create_from_input(input
, options
);
718 clast_pprint(stdout
, stmt
, 0, options
);
719 cloog_clast_free(stmt
);
722 cloog_options_free(options
);
723 cloog_state_free(state
);
724 isl_union_map_free(umap
);
728 void *set_codegen(void *arg
)
731 isl_union_set
*uset
= (isl_union_set
*)arg
;
733 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
735 CloogOptions
*options
;
736 CloogDomain
*context
;
737 CloogUnionDomain
*ud
;
739 struct clast_stmt
*stmt
;
741 if (isl_union_set_n_set(uset
) > 1)
742 isl_die(ctx
, isl_error_invalid
,
743 "code generation for more than one domain "
744 "requires a schedule", goto error
);
746 state
= cloog_isl_state_malloc(ctx
);
747 options
= cloog_options_malloc(state
);
748 options
->language
= CLOOG_LANGUAGE_C
;
749 options
->strides
= 1;
752 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
753 ud
= cloog_union_domain_from_isl_set(set
);
755 dim
= isl_union_set_get_space(uset
);
756 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
758 input
= cloog_input_alloc(context
, ud
);
760 stmt
= cloog_clast_create_from_input(input
, options
);
761 clast_pprint(stdout
, stmt
, 0, options
);
762 cloog_clast_free(stmt
);
764 cloog_options_free(options
);
765 cloog_state_free(state
);
767 isl_union_set_free(uset
);
773 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
776 struct isl_list
*list
;
777 struct pet_scop
*scop
;
778 isl_union_map
*sched
, *reads
, *writes
;
779 isl_union_set
*domain
;
780 struct iscc_options
*options
;
786 options
= isl_ctx_peek_iscc_options(ctx
);
787 if (!options
|| !options
->io
) {
789 isl_die(ctx
, isl_error_invalid
,
790 "parse_file operation not allowed", return NULL
);
793 list
= isl_list_alloc(ctx
, 4);
797 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
, 1);
798 domain
= pet_scop_collect_domains(scop
);
799 sched
= pet_scop_collect_schedule(scop
);
800 reads
= pet_scop_collect_reads(scop
);
801 writes
= pet_scop_collect_writes(scop
);
804 list
->obj
[0].type
= isl_obj_union_set
;
805 list
->obj
[0].v
= domain
;
806 list
->obj
[1].type
= isl_obj_union_map
;
807 list
->obj
[1].v
= writes
;
808 list
->obj
[2].type
= isl_obj_union_map
;
809 list
->obj
[2].v
= reads
;
810 list
->obj
[3].type
= isl_obj_union_map
;
811 list
->obj
[3].v
= sched
;
813 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
814 !list
->obj
[2].v
|| !list
->obj
[3].v
)
826 static int add_point(__isl_take isl_point
*pnt
, void *user
)
828 isl_union_set
**scan
= (isl_union_set
**) user
;
830 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
835 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
839 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
841 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
842 isl_union_set_free(scan
);
846 isl_union_set_free(uset
);
850 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
852 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
855 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
856 __isl_take isl_union_pw_qpolynomial
*upwqp
)
858 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
861 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
862 __isl_take isl_union_pw_qpolynomial
*upwqp
)
864 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
867 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
868 __isl_take isl_union_pw_qpolynomial
*upwqp
)
870 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
873 typedef void *(*isc_un_op_fn
)(void *arg
);
875 enum isl_token_type op
;
880 struct isc_named_un_op
{
884 struct isc_named_un_op named_un_ops
[] = {
885 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
886 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
887 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
888 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
889 {"card", { -1, isl_obj_union_set
,
890 isl_obj_union_pw_qpolynomial
,
891 (isc_un_op_fn
) &isl_union_set_card
} },
892 {"card", { -1, isl_obj_union_map
,
893 isl_obj_union_pw_qpolynomial
,
894 (isc_un_op_fn
) &isl_union_map_card
} },
895 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
896 (isc_un_op_fn
) &isl_union_set_coalesce
} },
897 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
898 (isc_un_op_fn
) &isl_union_map_coalesce
} },
899 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
900 isl_obj_union_pw_qpolynomial
,
901 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
902 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
903 isl_obj_union_pw_qpolynomial_fold
,
904 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
906 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
908 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
911 {"coefficients", { -1, isl_obj_union_set
,
913 (isc_un_op_fn
) &isl_union_set_coefficients
} },
914 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
915 (isc_un_op_fn
) &isl_union_set_solutions
} },
916 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
917 (isc_un_op_fn
) &isl_union_map_deltas
} },
918 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
919 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
920 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
921 (isc_un_op_fn
) &isl_union_map_domain
} },
922 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
923 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
924 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
926 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
927 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
928 (isc_un_op_fn
) &isl_union_map_domain
} },
929 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
931 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
932 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
934 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
935 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
936 (isc_un_op_fn
) &isl_union_map_domain_map
} },
937 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
938 (isc_un_op_fn
) &isl_union_map_range
} },
939 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
940 (isc_un_op_fn
) &isl_union_map_range
} },
941 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
942 (isc_un_op_fn
) &isl_union_map_range_map
} },
943 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
944 (isc_un_op_fn
) &isl_union_set_identity
} },
945 {"lattice_width", { -1, isl_obj_union_set
,
946 isl_obj_union_pw_qpolynomial
,
947 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
948 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
949 (isc_un_op_fn
) &isl_union_map_lexmin
} },
950 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
951 (isc_un_op_fn
) &isl_union_map_lexmax
} },
952 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
953 (isc_un_op_fn
) &isl_union_set_lexmin
} },
954 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
955 (isc_un_op_fn
) &isl_union_set_lexmax
} },
956 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
957 (isc_un_op_fn
) &isl_union_set_lift
} },
958 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
959 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
960 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
961 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
962 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
963 isl_obj_union_pw_qpolynomial
,
964 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
965 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
966 isl_obj_union_pw_qpolynomial
,
967 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
968 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
969 isl_obj_union_pw_qpolynomial
,
970 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
972 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
973 (isc_un_op_fn
) &parse
} },
975 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
976 (isc_un_op_fn
) &union_map_power
} },
977 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
978 (isc_un_op_fn
) &union_set_sample
} },
979 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
980 (isc_un_op_fn
) &union_map_sample
} },
981 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
982 (isc_un_op_fn
) &union_set_scan
} },
983 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
984 (isc_un_op_fn
) &union_map_scan
} },
985 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
986 isl_obj_union_pw_qpolynomial
,
987 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
988 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
989 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
990 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
991 (isc_un_op_fn
) &isl_union_set_unwrap
} },
992 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
993 (isc_un_op_fn
) &isl_union_map_wrap
} },
994 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
995 (isc_un_op_fn
) &isl_union_map_zip
} },
999 struct isl_named_obj
{
1004 static void free_obj(struct isl_obj obj
)
1006 obj
.type
->free(obj
.v
);
1009 static int same_name(const void *entry
, const void *val
)
1011 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1013 return !strcmp(named
->name
, val
);
1016 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1017 char *name
, struct isl_obj obj
)
1019 struct isl_hash_table_entry
*entry
;
1021 struct isl_named_obj
*named
;
1023 name_hash
= isl_hash_string(isl_hash_init(), name
);
1024 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1028 named
= entry
->data
;
1029 free_obj(named
->obj
);
1032 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1036 entry
->data
= named
;
1047 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1048 struct isl_hash_table
*table
, char *name
)
1050 struct isl_obj obj
= { isl_obj_none
, NULL
};
1051 struct isl_hash_table_entry
*entry
;
1054 name_hash
= isl_hash_string(isl_hash_init(), name
);
1055 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1057 struct isl_named_obj
*named
;
1058 named
= entry
->data
;
1060 } else if (isdigit(name
[0]))
1061 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1063 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1066 obj
.v
= obj
.type
->copy(obj
.v
);
1070 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1072 if (obj
.type
== super
)
1074 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1076 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1078 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1079 super
== isl_obj_union_pw_qpolynomial
)
1081 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1082 super
== isl_obj_union_pw_qpolynomial_fold
)
1084 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1086 if (obj
.type
== isl_obj_list
) {
1087 struct isl_list
*list
= obj
.v
;
1088 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1089 return is_subtype(list
->obj
[0], super
);
1091 if (super
== isl_obj_str
)
1096 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1098 struct isl_list
*list
= obj
.v
;
1101 obj
.v
= obj
.type
->copy(obj
.v
);
1103 isl_list_free(list
);
1108 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1111 if (obj
.type
== type
)
1113 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1114 obj
.type
= isl_obj_union_map
;
1115 obj
.v
= isl_union_map_from_map(obj
.v
);
1118 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1119 obj
.type
= isl_obj_union_set
;
1120 obj
.v
= isl_union_set_from_set(obj
.v
);
1123 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1124 type
== isl_obj_union_pw_qpolynomial
) {
1125 obj
.type
= isl_obj_union_pw_qpolynomial
;
1126 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1129 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1130 type
== isl_obj_union_pw_qpolynomial_fold
) {
1131 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1132 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1135 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1136 if (type
== isl_obj_union_map
) {
1137 obj
.type
= isl_obj_union_map
;
1140 if (type
== isl_obj_union_pw_qpolynomial
) {
1141 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1142 isl_union_set_free(obj
.v
);
1143 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1144 obj
.type
= isl_obj_union_pw_qpolynomial
;
1147 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1148 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1149 isl_union_set_free(obj
.v
);
1150 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1152 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1156 if (obj
.type
== isl_obj_list
) {
1157 struct isl_list
*list
= obj
.v
;
1158 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1159 return convert(ctx
, obj_at(obj
, 0), type
);
1161 if (type
== isl_obj_str
) {
1166 p
= isl_printer_to_str(ctx
);
1169 p
= obj
.type
->print(p
, obj
.v
);
1170 s
= isl_printer_get_str(p
);
1171 isl_printer_free(p
);
1173 str
= isl_str_from_string(ctx
, s
);
1178 obj
.type
= isl_obj_str
;
1184 obj
.type
= isl_obj_none
;
1189 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1193 struct isl_token
*tok
;
1195 tok
= isl_stream_next_token(s
);
1199 for (i
= 0; ; ++i
) {
1202 if (bin_ops
[i
].op
!= tok
->type
)
1204 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1207 isl_token_free(tok
);
1211 for (i
= 0; ; ++i
) {
1212 if (!named_bin_ops
[i
].name
)
1214 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1216 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1219 isl_token_free(tok
);
1220 return &named_bin_ops
[i
].op
;
1223 isl_stream_push_token(s
, tok
);
1228 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1231 struct isl_token
*tok
;
1233 tok
= isl_stream_next_token(s
);
1237 for (i
= 0; ; ++i
) {
1238 if (!named_un_ops
[i
].name
)
1240 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1243 isl_token_free(tok
);
1244 return &named_un_ops
[i
].op
;
1247 isl_stream_push_token(s
, tok
);
1252 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1257 for (i
= 0; ; ++i
) {
1258 if (!named_un_ops
[i
].name
)
1260 if (named_un_ops
[i
].op
.op
!= like
->op
)
1262 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1265 return &named_un_ops
[i
].op
;
1271 static int is_assign(struct isl_stream
*s
)
1273 struct isl_token
*tok
;
1274 struct isl_token
*tok2
;
1277 tok
= isl_stream_next_token(s
);
1280 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1281 isl_stream_push_token(s
, tok
);
1285 tok2
= isl_stream_next_token(s
);
1287 isl_stream_push_token(s
, tok
);
1290 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1291 isl_stream_push_token(s
, tok2
);
1292 isl_stream_push_token(s
, tok
);
1297 static struct isl_obj
read_obj(struct isl_stream
*s
,
1298 struct isl_hash_table
*table
);
1299 static struct isl_obj
read_expr(struct isl_stream
*s
,
1300 struct isl_hash_table
*table
);
1302 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1303 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1305 struct isl_obj obj
= { isl_obj_none
, NULL
};
1307 obj
= read_obj(s
, table
);
1311 op
= find_matching_un_op(op
, obj
);
1314 isl_die(s
->ctx
, isl_error_invalid
,
1315 "no such unary operator defined on given operand",
1318 obj
= convert(s
->ctx
, obj
, op
->arg
);
1319 obj
.v
= op
->fn(obj
.v
);
1325 obj
.type
= isl_obj_none
;
1330 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1332 struct isl_list
*list
;
1335 if (obj
.type
!= isl_obj_union_map
)
1336 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1337 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1338 list
= isl_list_alloc(ctx
, 2);
1342 list
->obj
[0].type
= isl_obj_union_map
;
1343 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1344 list
->obj
[1].type
= isl_obj_bool
;
1345 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1347 obj
.type
= isl_obj_list
;
1348 if (exact
< 0 || !list
->obj
[0].v
)
1354 obj
.type
= isl_obj_none
;
1359 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1361 struct isl_list
*list
= obj
.v
;
1362 struct isl_token
*tok
;
1365 tok
= isl_stream_next_token(s
);
1366 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1367 isl_stream_error(s
, tok
, "expecting index");
1369 isl_stream_push_token(s
, tok
);
1372 i
= isl_int_get_si(tok
->u
.v
);
1373 isl_token_free(tok
);
1374 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1375 if (isl_stream_eat(s
, ']'))
1378 return obj_at(obj
, i
);
1381 obj
.type
= isl_obj_none
;
1386 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1387 struct isl_hash_table
*table
)
1391 obj
= read_expr(s
, table
);
1392 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1393 is_subtype(obj
, isl_obj_union_map
), goto error
);
1395 if (obj
.type
== isl_obj_list
) {
1396 struct isl_list
*list
= obj
.v
;
1397 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1398 obj
= obj_at(obj
, 0);
1400 if (obj
.type
== isl_obj_set
)
1401 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1402 else if (obj
.type
== isl_obj_map
)
1403 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1404 if (obj
.type
== isl_obj_union_set
) {
1405 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1407 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1411 if (isl_stream_eat(s
, ')'))
1416 isl_union_map_free(umap
);
1419 obj
.type
= isl_obj_none
;
1424 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1425 __isl_take isl_union_set
*uset
)
1427 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1428 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1430 obj
.type
= isl_obj_list
;
1431 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1436 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1437 __isl_take isl_union_map
*umap
)
1439 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1440 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1442 obj
.type
= isl_obj_list
;
1443 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1448 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1449 struct isl_obj obj
, struct isl_hash_table
*table
)
1453 arg
= read_expr(s
, table
);
1454 if (!is_subtype(arg
, isl_obj_union_map
) &&
1455 !is_subtype(arg
, isl_obj_union_set
))
1456 isl_die(s
->ctx
, isl_error_invalid
,
1457 "expecting set of map argument", goto error
);
1459 if (arg
.type
== isl_obj_list
) {
1460 struct isl_list
*list
= arg
.v
;
1461 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1462 arg
= obj_at(arg
, 0);
1464 if (arg
.type
== isl_obj_set
)
1465 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1466 else if (arg
.type
== isl_obj_map
)
1467 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1468 if (arg
.type
== isl_obj_union_set
)
1469 obj
= apply_fun_set(obj
, arg
.v
);
1471 obj
= apply_fun_map(obj
, arg
.v
);
1475 if (isl_stream_eat(s
, ')'))
1483 obj
.type
= isl_obj_none
;
1488 struct add_vertex_data
{
1489 struct isl_list
*list
;
1493 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1495 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1496 isl_basic_set
*expr
;
1498 expr
= isl_vertex_get_expr(vertex
);
1500 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1501 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1504 isl_vertex_free(vertex
);
1509 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1512 isl_basic_set
*hull
;
1513 isl_vertices
*vertices
= NULL
;
1514 struct isl_list
*list
= NULL
;
1516 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1518 set
= isl_set_remove_divs(set
);
1519 hull
= isl_set_convex_hull(set
);
1520 vertices
= isl_basic_set_compute_vertices(hull
);
1521 isl_basic_set_free(hull
);
1525 ctx
= isl_vertices_get_ctx(vertices
);
1526 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1531 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1533 data
->list
= isl_list_concat(list
, data
->list
);
1535 isl_vertices_free(vertices
);
1540 isl_vertices_free(vertices
);
1544 static struct isl_obj
vertices(struct isl_stream
*s
,
1545 struct isl_hash_table
*table
)
1549 struct isl_list
*list
= NULL
;
1550 isl_union_set
*uset
;
1551 struct add_vertex_data data
= { NULL
};
1553 obj
= read_expr(s
, table
);
1554 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1555 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1559 ctx
= isl_union_set_get_ctx(uset
);
1560 list
= isl_list_alloc(ctx
, 0);
1566 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1569 isl_union_set_free(uset
);
1571 obj
.type
= isl_obj_list
;
1576 isl_union_set_free(uset
);
1577 isl_list_free(data
.list
);
1579 obj
.type
= isl_obj_none
;
1584 static struct isl_obj
type_of(struct isl_stream
*s
,
1585 struct isl_hash_table
*table
)
1589 const char *type
= "unknown";
1591 obj
= read_expr(s
, table
);
1593 if (obj
.type
== isl_obj_map
||
1594 obj
.type
== isl_obj_union_map
)
1596 if (obj
.type
== isl_obj_set
||
1597 obj
.type
== isl_obj_union_set
)
1599 if (obj
.type
== isl_obj_pw_qpolynomial
||
1600 obj
.type
== isl_obj_union_pw_qpolynomial
)
1601 type
= "piecewise quasipolynomial";
1602 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1603 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1604 type
= "piecewise quasipolynomial fold";
1605 if (obj
.type
== isl_obj_list
)
1607 if (obj
.type
== isl_obj_bool
)
1609 if (obj
.type
== isl_obj_str
)
1611 if (obj
.type
== isl_obj_int
)
1615 obj
.type
= isl_obj_str
;
1616 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1621 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1622 struct isl_hash_table
*table
)
1626 obj
= read_obj(s
, table
);
1627 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1628 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1635 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1636 struct isl_hash_table
*table
)
1640 obj
= read_obj(s
, table
);
1641 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1642 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1649 static struct isl_obj
last_any(struct isl_stream
*s
,
1650 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1651 __isl_take isl_union_map
*may_source
)
1653 struct isl_obj obj
= { isl_obj_none
, NULL
};
1654 isl_union_map
*sink
= NULL
;
1655 isl_union_map
*schedule
= NULL
;
1656 isl_union_map
*may_dep
;
1657 isl_union_map
*must_dep
;
1659 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1662 sink
= read_map(s
, table
);
1666 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1669 schedule
= read_map(s
, table
);
1673 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1674 schedule
, &must_dep
, &may_dep
,
1678 obj
.type
= isl_obj_union_map
;
1679 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1683 isl_union_map_free(may_source
);
1684 isl_union_map_free(must_source
);
1685 isl_union_map_free(sink
);
1686 isl_union_map_free(schedule
);
1688 obj
.type
= isl_obj_none
;
1693 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1695 struct isl_obj obj
= { isl_obj_none
, NULL
};
1696 isl_union_map
*must_source
= NULL
;
1697 isl_union_map
*may_source
= NULL
;
1698 isl_union_map
*sink
= NULL
;
1699 isl_union_map
*schedule
= NULL
;
1700 isl_union_map
*may_dep
;
1702 may_source
= read_map(s
, table
);
1706 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1707 must_source
= read_map(s
, table
);
1710 return last_any(s
, table
, must_source
, may_source
);
1713 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1716 sink
= read_map(s
, table
);
1720 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1723 schedule
= read_map(s
, table
);
1727 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1728 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1729 schedule
, NULL
, &may_dep
,
1733 obj
.type
= isl_obj_union_map
;
1738 isl_union_map_free(may_source
);
1739 isl_union_map_free(must_source
);
1740 isl_union_map_free(sink
);
1741 isl_union_map_free(schedule
);
1743 obj
.type
= isl_obj_none
;
1748 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1750 struct isl_obj obj
= { isl_obj_none
, NULL
};
1751 struct isl_list
*list
= NULL
;
1752 isl_union_map
*must_source
= NULL
;
1753 isl_union_map
*may_source
= NULL
;
1754 isl_union_map
*sink
= NULL
;
1755 isl_union_map
*schedule
= NULL
;
1756 isl_union_map
*must_dep
;
1757 isl_union_map
*must_no_source
;
1759 must_source
= read_map(s
, table
);
1763 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1764 may_source
= read_map(s
, table
);
1767 return last_any(s
, table
, must_source
, may_source
);
1770 list
= isl_list_alloc(s
->ctx
, 2);
1774 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1777 sink
= read_map(s
, table
);
1781 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1784 schedule
= read_map(s
, table
);
1788 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1789 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1790 schedule
, &must_dep
, NULL
,
1791 &must_no_source
, NULL
) < 0) {
1792 isl_list_free(list
);
1796 list
->obj
[0].type
= isl_obj_union_map
;
1797 list
->obj
[0].v
= must_dep
;
1798 list
->obj
[1].type
= isl_obj_union_map
;
1799 list
->obj
[1].v
= must_no_source
;
1802 obj
.type
= isl_obj_list
;
1806 isl_list_free(list
);
1807 isl_union_map_free(may_source
);
1808 isl_union_map_free(must_source
);
1809 isl_union_map_free(sink
);
1810 isl_union_map_free(schedule
);
1812 obj
.type
= isl_obj_none
;
1817 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1818 struct isl_hash_table
*table
)
1820 isl_union_set
*domain
;
1821 isl_union_map
*validity
;
1822 isl_union_map
*proximity
;
1824 domain
= read_set(s
, table
);
1828 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1829 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1832 isl_union_map
*umap
;
1833 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1834 umap
= read_map(s
, table
);
1835 validity
= isl_union_map_union(validity
, umap
);
1836 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1837 umap
= read_map(s
, table
);
1838 proximity
= isl_union_map_union(proximity
, umap
);
1843 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1846 static struct isl_obj
schedule(struct isl_stream
*s
,
1847 struct isl_hash_table
*table
)
1849 struct isl_obj obj
= { isl_obj_none
, NULL
};
1850 isl_schedule
*schedule
;
1852 schedule
= get_schedule(s
, table
);
1854 obj
.v
= isl_schedule_get_map(schedule
);
1855 obj
.type
= isl_obj_union_map
;
1857 isl_schedule_free(schedule
);
1862 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1864 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1866 struct isl_obj obj
= { isl_obj_none
, NULL
};
1867 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1868 struct isl_list
*list
;
1870 list
= isl_list_alloc(ctx
, 2);
1875 obj
.type
= isl_obj_list
;
1877 list
->obj
[0].type
= isl_obj_union_map
;
1878 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1880 if (isl_band_has_children(band
)) {
1881 isl_band_list
*children
;
1883 children
= isl_band_get_children(band
);
1884 list
->obj
[1] = band_list_to_obj_list(children
);
1886 list
->obj
[1].type
= isl_obj_list
;
1887 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1890 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1893 isl_band_free(band
);
1897 isl_band_free(band
);
1899 obj
.type
= isl_obj_none
;
1904 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1906 struct isl_obj obj
= { isl_obj_none
, NULL
};
1907 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1908 struct isl_list
*list
;
1911 n
= isl_band_list_n_band(bands
);
1912 list
= isl_list_alloc(ctx
, n
);
1917 obj
.type
= isl_obj_list
;
1919 for (i
= 0; i
< n
; ++i
) {
1922 band
= isl_band_list_get_band(bands
, i
);
1923 list
->obj
[i
] = band_to_obj_list(band
);
1924 if (!list
->obj
[i
].v
)
1928 isl_band_list_free(bands
);
1932 isl_band_list_free(bands
);
1934 obj
.type
= isl_obj_none
;
1939 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1940 struct isl_hash_table
*table
)
1942 struct isl_obj obj
= { isl_obj_none
, NULL
};
1943 isl_schedule
*schedule
;
1944 isl_band_list
*roots
;
1946 schedule
= get_schedule(s
, table
);
1950 roots
= isl_schedule_get_band_forest(schedule
);
1951 isl_schedule_free(schedule
);
1953 return band_list_to_obj_list(roots
);
1956 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1958 struct isl_token
*tok
;
1960 if (isl_stream_eat_if_available(s
, '+'))
1961 return transitive_closure(s
->ctx
, obj
);
1963 tok
= isl_stream_next_token(s
);
1964 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1965 isl_stream_error(s
, tok
, "expecting -1");
1967 isl_stream_push_token(s
, tok
);
1970 isl_token_free(tok
);
1971 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1972 if (obj
.type
!= isl_obj_union_map
)
1973 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1975 obj
.v
= isl_union_map_reverse(obj
.v
);
1982 obj
.type
= isl_obj_none
;
1987 static struct isl_obj
check_assert(struct isl_stream
*s
,
1988 struct isl_hash_table
*table
)
1992 obj
= read_expr(s
, table
);
1993 if (obj
.type
!= isl_obj_bool
)
1994 isl_die(s
->ctx
, isl_error_invalid
,
1995 "expecting boolean expression", goto error
);
1996 if (obj
.v
!= &isl_bool_true
)
1997 isl_die(s
->ctx
, isl_error_unknown
,
1998 "assertion failed", abort());
2001 obj
.type
= isl_obj_none
;
2006 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2009 struct isl_token
*tok
;
2010 struct isl_stream
*s_file
;
2011 struct iscc_options
*options
;
2014 tok
= isl_stream_next_token(s
);
2015 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2016 isl_stream_error(s
, tok
, "expecting filename");
2017 isl_token_free(tok
);
2021 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2022 if (!options
|| !options
->io
) {
2023 isl_token_free(tok
);
2024 isl_die(s
->ctx
, isl_error_invalid
,
2025 "read operation not allowed", goto error
);
2028 file
= fopen(tok
->u
.s
, "r");
2029 isl_token_free(tok
);
2030 isl_assert(s
->ctx
, file
, goto error
);
2032 s_file
= isl_stream_new_file(s
->ctx
, file
);
2038 obj
= isl_stream_read_obj(s_file
);
2040 isl_stream_free(s_file
);
2045 obj
.type
= isl_obj_none
;
2050 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2051 struct isl_hash_table
*table
)
2054 struct isl_token
*tok
;
2055 struct isl_stream
*s_file
;
2056 struct iscc_options
*options
;
2060 tok
= isl_stream_next_token(s
);
2061 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2062 isl_stream_error(s
, tok
, "expecting filename");
2063 isl_token_free(tok
);
2067 obj
= read_expr(s
, table
);
2069 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2070 if (!options
|| !options
->io
) {
2071 isl_token_free(tok
);
2072 isl_die(s
->ctx
, isl_error_invalid
,
2073 "write operation not allowed", goto error
);
2076 file
= fopen(tok
->u
.s
, "w");
2077 isl_token_free(tok
);
2079 isl_die(s
->ctx
, isl_error_unknown
,
2080 "could not open file for writing", goto error
);
2082 p
= isl_printer_to_file(s
->ctx
, file
);
2083 p
= isl_printer_set_output_format(p
, options
->format
);
2084 p
= obj
.type
->print(p
, obj
.v
);
2085 p
= isl_printer_end_line(p
);
2086 isl_printer_free(p
);
2091 obj
.type
= isl_obj_none
;
2096 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2098 struct isl_token
*tok
;
2099 struct isl_obj obj
= { isl_obj_none
, NULL
};
2101 tok
= isl_stream_next_token(s
);
2104 if (tok
->type
== ISL_TOKEN_STRING
) {
2106 str
= isl_str_alloc(s
->ctx
);
2109 str
->s
= strdup(tok
->u
.s
);
2110 isl_token_free(tok
);
2112 obj
.type
= isl_obj_str
;
2114 isl_stream_push_token(s
, tok
);
2117 isl_token_free(tok
);
2121 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2123 struct isl_token
*tok
;
2124 struct isl_obj obj
= { isl_obj_none
, NULL
};
2126 tok
= isl_stream_next_token(s
);
2129 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2130 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2131 isl_token_free(tok
);
2132 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2133 obj
.type
= isl_obj_bool
;
2135 isl_stream_push_token(s
, tok
);
2138 isl_token_free(tok
);
2142 static __isl_give
char *read_ident(struct isl_stream
*s
)
2145 struct isl_token
*tok
, *tok2
;
2147 name
= isl_stream_read_ident_if_available(s
);
2151 tok
= isl_stream_next_token(s
);
2154 if (tok
->type
!= '$') {
2155 isl_stream_push_token(s
, tok
);
2158 tok2
= isl_stream_next_token(s
);
2159 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2161 isl_stream_push_token(s
, tok2
);
2162 isl_stream_push_token(s
, tok
);
2166 name
= isl_int_get_str(tok2
->u
.v
);
2167 isl_token_free(tok
);
2168 isl_token_free(tok2
);
2173 static struct isl_obj
read_list(struct isl_stream
*s
,
2174 struct isl_hash_table
*table
, struct isl_obj obj
)
2176 struct isl_list
*list
;
2178 list
= isl_list_alloc(s
->ctx
, 2);
2182 list
->obj
[1] = read_obj(s
, table
);
2184 obj
.type
= isl_obj_list
;
2186 if (!list
->obj
[1].v
)
2189 while (isl_stream_eat_if_available(s
, ',')) {
2190 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2198 obj
.type
= isl_obj_none
;
2203 static struct isl_obj
read_obj(struct isl_stream
*s
,
2204 struct isl_hash_table
*table
)
2206 struct isl_obj obj
= { isl_obj_none
, NULL
};
2208 struct isc_un_op
*op
= NULL
;
2210 obj
= read_string_if_available(s
);
2213 obj
= read_bool_if_available(s
);
2216 if (isl_stream_eat_if_available(s
, '(')) {
2217 if (isl_stream_next_token_is(s
, ')')) {
2218 obj
.type
= isl_obj_list
;
2219 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2221 obj
= read_expr(s
, table
);
2222 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2223 obj
= read_list(s
, table
, obj
);
2225 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2228 op
= read_prefix_un_op_if_available(s
);
2230 return read_un_op_expr(s
, table
, op
);
2232 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2233 return check_assert(s
, table
);
2234 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2235 return read_from_file(s
);
2236 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2237 return write_to_file(s
, table
);
2238 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2239 return vertices(s
, table
);
2240 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2241 return any(s
, table
);
2242 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2243 return last(s
, table
);
2244 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2245 return schedule(s
, table
);
2246 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2247 return schedule_forest(s
, table
);
2248 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2249 return type_of(s
, table
);
2251 name
= read_ident(s
);
2253 obj
= stored_obj(s
->ctx
, table
, name
);
2255 obj
= isl_stream_read_obj(s
);
2260 if (isl_stream_eat_if_available(s
, '^'))
2261 obj
= power(s
, obj
);
2262 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2263 obj
= obj_at_index(s
, obj
);
2264 else if (is_subtype(obj
, isl_obj_union_map
) &&
2265 isl_stream_eat_if_available(s
, '(')) {
2266 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2267 obj
= apply(s
, obj
.v
, table
);
2268 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2269 isl_stream_eat_if_available(s
, '(')) {
2270 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2271 obj
= apply_fun(s
, obj
, table
);
2272 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2273 isl_stream_eat_if_available(s
, '(')) {
2274 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2275 obj
= apply_fun(s
, obj
, table
);
2281 obj
.type
= isl_obj_none
;
2286 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2287 struct isl_obj lhs
, struct isl_obj rhs
)
2291 for (i
= 0; ; ++i
) {
2294 if (bin_ops
[i
].op
!= like
->op
)
2296 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2298 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2304 for (i
= 0; ; ++i
) {
2305 if (!named_bin_ops
[i
].name
)
2307 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2309 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2311 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2314 return &named_bin_ops
[i
].op
;
2320 static int next_is_neg_int(struct isl_stream
*s
)
2322 struct isl_token
*tok
;
2325 tok
= isl_stream_next_token(s
);
2326 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2327 isl_stream_push_token(s
, tok
);
2332 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2333 struct isl_obj lhs
, struct isl_obj rhs
)
2337 lhs
= convert(ctx
, lhs
, op
->lhs
);
2338 rhs
= convert(ctx
, rhs
, op
->rhs
);
2339 if (op
->res
!= isl_obj_bool
)
2340 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2342 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2345 obj
.v
= isl_bool_from_int(res
);
2352 static struct isl_obj
read_expr(struct isl_stream
*s
,
2353 struct isl_hash_table
*table
)
2355 struct isl_obj obj
= { isl_obj_none
, NULL
};
2356 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2358 obj
= read_obj(s
, table
);
2360 struct isc_bin_op
*op
= NULL
;
2362 op
= read_bin_op_if_available(s
, obj
);
2366 right_obj
= read_obj(s
, table
);
2368 op
= find_matching_bin_op(op
, obj
, right_obj
);
2371 isl_die(s
->ctx
, isl_error_invalid
,
2372 "no such binary operator defined on given operands",
2375 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2378 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2379 right_obj
= read_obj(s
, table
);
2380 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2385 free_obj(right_obj
);
2387 obj
.type
= isl_obj_none
;
2392 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2393 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2395 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2396 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2398 struct isl_obj obj
= { isl_obj_none
, NULL
};
2402 struct isc_bin_op
*op
= NULL
;
2407 if (isl_stream_is_empty(s
))
2410 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2411 return source_file(s
, table
, p
);
2413 assign
= is_assign(s
);
2415 lhs
= isl_stream_read_ident_if_available(s
);
2416 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2418 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2423 obj
= read_expr(s
, table
);
2424 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2425 fprintf(stderr
, "Interrupted\n");
2426 isl_ctx_reset_error(s
->ctx
);
2428 if (isl_stream_eat(s
, ';'))
2432 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2433 p
= obj
.type
->print(p
, obj
.v
);
2434 p
= isl_printer_end_line(p
);
2439 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2440 static int count
= 0;
2441 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2442 lhs
= strdup(buf
+ 1);
2444 p
= isl_printer_print_str(p
, buf
);
2445 p
= isl_printer_print_str(p
, " := ");
2446 p
= obj
.type
->print(p
, obj
.v
);
2447 p
= isl_printer_end_line(p
);
2449 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2454 isl_stream_flush_tokens(s
);
2455 isl_stream_skip_line(s
);
2461 int free_cb(void **entry
, void *user
)
2463 struct isl_named_obj
*named
= *entry
;
2465 free_obj(named
->obj
);
2472 static void register_named_ops(struct isl_stream
*s
)
2476 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2477 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2478 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2481 for (i
= 0; ; ++i
) {
2482 if (!named_un_ops
[i
].name
)
2484 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2485 named_un_ops
[i
].name
);
2486 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2489 for (i
= 0; ; ++i
) {
2490 if (!named_bin_ops
[i
].name
)
2492 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2493 named_bin_ops
[i
].name
);
2494 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2498 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2499 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2501 struct isl_token
*tok
;
2502 struct isl_stream
*s_file
;
2505 tok
= isl_stream_next_token(s
);
2506 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2507 isl_stream_error(s
, tok
, "expecting filename");
2508 isl_token_free(tok
);
2512 file
= fopen(tok
->u
.s
, "r");
2513 isl_token_free(tok
);
2514 isl_assert(s
->ctx
, file
, return p
);
2516 s_file
= isl_stream_new_file(s
->ctx
, file
);
2522 register_named_ops(s_file
);
2524 while (!s_file
->eof
)
2525 p
= read_line(s_file
, table
, p
, 0);
2527 isl_stream_free(s_file
);
2530 isl_stream_eat(s
, ';');
2535 int main(int argc
, char **argv
)
2537 struct isl_ctx
*ctx
;
2538 struct isl_stream
*s
;
2539 struct isl_hash_table
*table
;
2540 struct iscc_options
*options
;
2542 int tty
= isatty(0);
2544 options
= iscc_options_new_with_defaults();
2546 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
2548 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
2549 s
= isl_stream_new_file(ctx
, stdin
);
2551 table
= isl_hash_table_alloc(ctx
, 10);
2553 p
= isl_printer_to_file(ctx
, stdout
);
2554 p
= isl_printer_set_output_format(p
, options
->format
);
2557 register_named_ops(s
);
2559 install_signal_handler(ctx
);
2561 while (p
&& !s
->eof
) {
2562 isl_ctx_resume(ctx
);
2563 p
= read_line(s
, table
, p
, tty
);
2566 remove_signal_handler(ctx
);
2568 isl_printer_free(p
);
2569 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2570 isl_hash_table_free(ctx
, table
);