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 int pet_options_set_autodetect(isl_ctx
*ctx
, int val
)
77 static int isl_bool_false
= 0;
78 static int isl_bool_true
= 1;
79 static int isl_bool_error
= -1;
81 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
82 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
83 ISCC_SCHEDULE
, ISCC_SCHEDULE_FOREST
,
84 ISCC_MINIMIZING
, ISCC_RESPECTING
,
85 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
87 static const char *op_name
[ISCC_N_OP
] = {
88 [ISCC_ASSERT
] = "assert",
90 [ISCC_WRITE
] = "write",
91 [ISCC_PRINT
] = "print",
92 [ISCC_SOURCE
] = "source",
93 [ISCC_VERTICES
] = "vertices",
96 [ISCC_BEFORE
] = "before",
97 [ISCC_UNDER
] = "under",
98 [ISCC_SCHEDULE
] = "schedule",
99 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
100 [ISCC_MINIMIZING
] = "minimizing",
101 [ISCC_RESPECTING
] = "respecting",
102 [ISCC_TYPEOF
] = "typeof"
104 static enum isl_token_type iscc_op
[ISCC_N_OP
];
106 struct isl_arg_choice iscc_format
[] = {
107 {"isl", ISL_FORMAT_ISL
},
108 {"omega", ISL_FORMAT_OMEGA
},
109 {"polylib", ISL_FORMAT_POLYLIB
},
110 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
111 {"latex", ISL_FORMAT_LATEX
},
116 struct iscc_options
{
117 struct barvinok_options
*barvinok
;
118 struct pet_options
*pet
;
123 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
124 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
127 ISL_ARG_CHILD(struct iscc_options
, pet
, "pet", &pet_options_args
, "pet options")
129 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
130 iscc_format
, ISL_FORMAT_ISL
, "output format")
131 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
132 "allow read and write operations")
135 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
136 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
138 static void *isl_obj_bool_copy(void *v
)
143 static void isl_obj_bool_free(void *v
)
147 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
150 if (v
== &isl_bool_true
)
151 return isl_printer_print_str(p
, "True");
152 else if (v
== &isl_bool_false
)
153 return isl_printer_print_str(p
, "False");
155 return isl_printer_print_str(p
, "Error");
158 static void *isl_obj_bool_add(void *v1
, void *v2
)
163 struct isl_obj_vtable isl_obj_bool_vtable
= {
169 #define isl_obj_bool (&isl_obj_bool_vtable)
171 int *isl_bool_from_int(int res
)
173 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
176 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
177 __isl_take isl_union_map
*map2
)
179 return isl_union_map_is_subset(map2
, map1
);
181 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
182 __isl_take isl_union_set
*set2
)
184 return isl_union_set_is_subset(set2
, set1
);
187 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
188 __isl_take isl_union_map
*map2
)
190 return isl_union_map_is_strict_subset(map2
, map1
);
192 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
193 __isl_take isl_union_set
*set2
)
195 return isl_union_set_is_strict_subset(set2
, set1
);
198 extern struct isl_obj_vtable isl_obj_list_vtable
;
199 #define isl_obj_list (&isl_obj_list_vtable)
201 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
202 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
204 enum isl_token_type op
;
210 isc_bin_test_fn test
;
213 struct isc_named_bin_op
{
215 struct isc_bin_op op
;
219 isl_union_pw_qpolynomial
*upwqp
;
220 isl_union_pw_qpolynomial
*res
;
223 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
225 struct iscc_at
*at
= (struct iscc_at
*) user
;
229 set
= isl_set_from_point(isl_point_copy(pnt
));
230 qp
= isl_union_pw_qpolynomial_eval(
231 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
233 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
234 isl_union_pw_qpolynomial_from_pw_qpolynomial(
235 isl_pw_qpolynomial_alloc(set
, qp
)));
240 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
241 __isl_take isl_union_pw_qpolynomial
*upwqp
,
242 __isl_take isl_union_set
*uset
)
247 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
249 isl_union_set_foreach_point(uset
, eval_at
, &at
);
251 isl_union_pw_qpolynomial_free(upwqp
);
252 isl_union_set_free(uset
);
257 struct iscc_fold_at
{
258 isl_union_pw_qpolynomial_fold
*upwf
;
259 isl_union_pw_qpolynomial
*res
;
262 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
264 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
268 set
= isl_set_from_point(isl_point_copy(pnt
));
269 qp
= isl_union_pw_qpolynomial_fold_eval(
270 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
272 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
273 isl_union_pw_qpolynomial_from_pw_qpolynomial(
274 isl_pw_qpolynomial_alloc(set
, qp
)));
279 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
280 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
281 __isl_take isl_union_set
*uset
)
283 struct iscc_fold_at at
;
286 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
288 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
290 isl_union_pw_qpolynomial_fold_free(upwf
);
291 isl_union_set_free(uset
);
296 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
297 __isl_take isl_union_pw_qpolynomial
*upwqp
,
298 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
300 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
304 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
305 __isl_take isl_union_map
*umap
,
306 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
309 struct isl_list
*list
;
312 ctx
= isl_union_map_get_ctx(umap
);
313 list
= isl_list_alloc(ctx
, 2);
317 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
318 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
320 list
->obj
[1].type
= isl_obj_bool
;
321 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
322 if (tight
< 0 || !list
->obj
[0].v
)
327 isl_union_map_free(umap
);
328 isl_union_pw_qpolynomial_fold_free(upwf
);
334 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
335 __isl_take isl_union_set
*uset
,
336 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
339 struct isl_list
*list
;
342 ctx
= isl_union_set_get_ctx(uset
);
343 list
= isl_list_alloc(ctx
, 2);
347 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
348 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
350 list
->obj
[1].type
= isl_obj_bool
;
351 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
352 if (tight
< 0 || !list
->obj
[0].v
)
357 isl_union_set_free(uset
);
358 isl_union_pw_qpolynomial_fold_free(upwf
);
364 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_int_mul(
365 __isl_take isl_union_pw_qpolynomial
*upwqp
, __isl_take isl_int_obj
*i
)
373 isl_int_obj_get_int(i
, &v
);
374 upwqp
= isl_union_pw_qpolynomial_mul_isl_int(upwqp
, v
);
381 isl_union_pw_qpolynomial_free(upwqp
);
385 static __isl_give isl_union_pw_qpolynomial
*int_union_pw_qpolynomial_mul(
386 __isl_take isl_int_obj
*i
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
388 return union_pw_qpolynomial_int_mul(upwqp
, i
);
391 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_fold_int_mul(
392 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
393 __isl_take isl_int_obj
*i
)
401 isl_int_obj_get_int(i
, &v
);
402 upwf
= isl_union_pw_qpolynomial_fold_mul_isl_int(upwf
, v
);
409 isl_union_pw_qpolynomial_fold_free(upwf
);
413 static __isl_give isl_union_pw_qpolynomial_fold
*int_union_pw_qpolynomial_fold_mul(
414 __isl_take isl_int_obj
*i
,
415 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
417 return union_pw_qpolynomial_fold_int_mul(upwf
, i
);
420 struct isc_bin_op bin_ops
[] = {
421 { '+', isl_obj_int
, isl_obj_int
, isl_obj_int
,
422 (isc_bin_op_fn
) &isl_int_obj_add
},
423 { '-', isl_obj_int
, isl_obj_int
, isl_obj_int
,
424 (isc_bin_op_fn
) &isl_int_obj_sub
},
425 { '*', isl_obj_int
, isl_obj_int
, isl_obj_int
,
426 (isc_bin_op_fn
) &isl_int_obj_mul
},
427 { '+', isl_obj_union_set
, isl_obj_union_set
,
429 (isc_bin_op_fn
) &isl_union_set_union
},
430 { '+', isl_obj_union_map
, isl_obj_union_map
,
432 (isc_bin_op_fn
) &isl_union_map_union
},
433 { '-', isl_obj_union_set
, isl_obj_union_set
,
435 (isc_bin_op_fn
) &isl_union_set_subtract
},
436 { '-', isl_obj_union_map
, isl_obj_union_map
,
438 (isc_bin_op_fn
) &isl_union_map_subtract
},
439 { '*', isl_obj_union_set
, isl_obj_union_set
,
441 (isc_bin_op_fn
) &isl_union_set_intersect
},
442 { '*', isl_obj_union_map
, isl_obj_union_map
,
444 (isc_bin_op_fn
) &isl_union_map_intersect
},
445 { '*', isl_obj_union_map
, isl_obj_union_set
,
447 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
448 { '.', isl_obj_union_map
, isl_obj_union_map
,
450 (isc_bin_op_fn
) &isl_union_map_apply_range
},
451 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
452 isl_obj_union_pw_qpolynomial
,
453 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
454 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
456 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
457 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
459 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
460 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
461 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
462 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
463 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
464 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
466 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
467 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
469 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
470 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
472 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
473 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
475 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
476 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
478 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
479 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
481 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
482 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
485 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
486 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
489 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
490 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
492 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
493 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
495 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
496 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
498 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
499 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
501 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
502 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
504 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
505 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
507 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
508 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
510 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
511 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
513 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
514 { '.', isl_obj_union_pw_qpolynomial_fold
,
515 isl_obj_union_pw_qpolynomial_fold
,
516 isl_obj_union_pw_qpolynomial_fold
,
517 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
518 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
519 isl_obj_union_pw_qpolynomial
,
520 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
521 { '+', isl_obj_union_pw_qpolynomial
,
522 isl_obj_union_pw_qpolynomial_fold
,
523 isl_obj_union_pw_qpolynomial_fold
,
524 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
525 { '+', isl_obj_union_pw_qpolynomial_fold
,
526 isl_obj_union_pw_qpolynomial
,
527 isl_obj_union_pw_qpolynomial_fold
,
528 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
529 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
530 isl_obj_union_pw_qpolynomial
,
531 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
532 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial
,
533 isl_obj_union_pw_qpolynomial
,
534 (isc_bin_op_fn
) &int_union_pw_qpolynomial_mul
},
535 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_int
,
536 isl_obj_union_pw_qpolynomial
,
537 (isc_bin_op_fn
) &union_pw_qpolynomial_int_mul
},
538 { '*', isl_obj_int
, isl_obj_union_pw_qpolynomial_fold
,
539 isl_obj_union_pw_qpolynomial_fold
,
540 (isc_bin_op_fn
) &int_union_pw_qpolynomial_fold_mul
},
541 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_int
,
542 isl_obj_union_pw_qpolynomial_fold
,
543 (isc_bin_op_fn
) &union_pw_qpolynomial_fold_int_mul
},
544 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
545 isl_obj_union_pw_qpolynomial
,
546 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
547 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
548 isl_obj_union_pw_qpolynomial
,
549 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
550 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
551 isl_obj_union_pw_qpolynomial_fold
,
552 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
553 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
554 isl_obj_union_pw_qpolynomial
,
555 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
556 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
557 isl_obj_union_pw_qpolynomial
,
558 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
559 { '%', isl_obj_union_set
, isl_obj_union_set
,
561 (isc_bin_op_fn
) &isl_union_set_gist
},
562 { '%', isl_obj_union_map
, isl_obj_union_map
,
564 (isc_bin_op_fn
) &isl_union_map_gist
},
565 { '%', isl_obj_union_map
, isl_obj_union_set
,
567 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
568 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
569 isl_obj_union_pw_qpolynomial
,
570 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
571 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
572 isl_obj_union_pw_qpolynomial_fold
,
573 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
574 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
575 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
576 { .test
= (isc_bin_test_fn
)
577 &isl_union_pw_qpolynomial_plain_is_equal
} },
578 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
579 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
580 { .test
= (isc_bin_test_fn
)
581 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
582 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
583 (isc_bin_op_fn
) &isl_str_concat
},
587 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
588 __isl_take isl_union_map
*umap2
)
590 return isl_union_map_apply_range(umap2
, umap1
);
593 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
594 __isl_take isl_union_pw_qpolynomial
*upwqp
,
595 __isl_take isl_union_map
*umap
)
597 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
600 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
601 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
602 __isl_take isl_union_map
*umap
)
604 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
607 struct isc_named_bin_op named_bin_ops
[] = {
608 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
610 (isc_bin_op_fn
) &map_after_map
} },
611 { "after", { -1, isl_obj_union_pw_qpolynomial
,
612 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
613 (isc_bin_op_fn
) &qpolynomial_after_map
} },
614 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
615 isl_obj_union_map
, isl_obj_list
,
616 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
617 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
619 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
620 { "before", { -1, isl_obj_union_map
,
621 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
622 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
623 { "before", { -1, isl_obj_union_map
,
624 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
625 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
626 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
628 (isc_bin_op_fn
) &isl_union_set_product
} },
629 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
631 (isc_bin_op_fn
) &isl_union_map_product
} },
635 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
637 return isl_set_from_basic_set(isl_union_set_sample(uset
));
640 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
642 return isl_map_from_basic_map(isl_union_map_sample(umap
));
645 static __isl_give
struct isl_list
*union_map_power(
646 __isl_take isl_union_map
*umap
)
649 struct isl_list
*list
;
652 ctx
= isl_union_map_get_ctx(umap
);
653 list
= isl_list_alloc(ctx
, 2);
657 list
->obj
[0].type
= isl_obj_union_map
;
658 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
659 list
->obj
[1].type
= isl_obj_bool
;
660 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
661 if (exact
< 0 || !list
->obj
[0].v
)
666 isl_union_map_free(umap
);
672 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
673 __isl_take isl_union_pw_qpolynomial
*upwqp
)
676 struct isl_list
*list
;
679 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
680 list
= isl_list_alloc(ctx
, 2);
684 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
685 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
686 isl_fold_max
, &tight
);
687 list
->obj
[1].type
= isl_obj_bool
;
688 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
689 if (tight
< 0 || !list
->obj
[0].v
)
694 isl_union_pw_qpolynomial_free(upwqp
);
701 void *map_codegen(void *arg
)
704 isl_union_map
*umap
= (isl_union_map
*)arg
;
705 isl_ctx
*ctx
= isl_union_map_get_ctx(umap
);
707 CloogOptions
*options
;
708 CloogDomain
*context
;
709 CloogUnionDomain
*ud
;
711 struct clast_stmt
*stmt
;
713 state
= cloog_isl_state_malloc(ctx
);
714 options
= cloog_options_malloc(state
);
715 options
->language
= CLOOG_LANGUAGE_C
;
716 options
->strides
= 1;
719 ud
= cloog_union_domain_from_isl_union_map(isl_union_map_copy(umap
));
721 dim
= isl_union_map_get_space(umap
);
722 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
724 input
= cloog_input_alloc(context
, ud
);
726 stmt
= cloog_clast_create_from_input(input
, options
);
727 clast_pprint(stdout
, stmt
, 0, options
);
728 cloog_clast_free(stmt
);
731 cloog_options_free(options
);
732 cloog_state_free(state
);
733 isl_union_map_free(umap
);
737 void *set_codegen(void *arg
)
740 isl_union_set
*uset
= (isl_union_set
*)arg
;
742 isl_ctx
*ctx
= isl_union_set_get_ctx(uset
);
744 CloogOptions
*options
;
745 CloogDomain
*context
;
746 CloogUnionDomain
*ud
;
748 struct clast_stmt
*stmt
;
750 if (isl_union_set_n_set(uset
) > 1)
751 isl_die(ctx
, isl_error_invalid
,
752 "code generation for more than one domain "
753 "requires a schedule", goto error
);
755 state
= cloog_isl_state_malloc(ctx
);
756 options
= cloog_options_malloc(state
);
757 options
->language
= CLOOG_LANGUAGE_C
;
758 options
->strides
= 1;
761 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
762 ud
= cloog_union_domain_from_isl_set(set
);
764 dim
= isl_union_set_get_space(uset
);
765 context
= cloog_domain_from_isl_set(isl_set_universe(dim
));
767 input
= cloog_input_alloc(context
, ud
);
769 stmt
= cloog_clast_create_from_input(input
, options
);
770 clast_pprint(stdout
, stmt
, 0, options
);
771 cloog_clast_free(stmt
);
773 cloog_options_free(options
);
774 cloog_state_free(state
);
776 isl_union_set_free(uset
);
782 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
785 struct isl_list
*list
;
786 struct pet_scop
*scop
;
787 isl_union_map
*sched
, *reads
, *writes
;
788 isl_union_set
*domain
;
789 struct iscc_options
*options
;
795 options
= isl_ctx_peek_iscc_options(ctx
);
796 if (!options
|| !options
->io
) {
798 isl_die(ctx
, isl_error_invalid
,
799 "parse_file operation not allowed", return NULL
);
802 list
= isl_list_alloc(ctx
, 4);
806 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
807 domain
= pet_scop_collect_domains(scop
);
808 sched
= pet_scop_collect_schedule(scop
);
809 reads
= pet_scop_collect_reads(scop
);
810 writes
= pet_scop_collect_writes(scop
);
813 list
->obj
[0].type
= isl_obj_union_set
;
814 list
->obj
[0].v
= domain
;
815 list
->obj
[1].type
= isl_obj_union_map
;
816 list
->obj
[1].v
= writes
;
817 list
->obj
[2].type
= isl_obj_union_map
;
818 list
->obj
[2].v
= reads
;
819 list
->obj
[3].type
= isl_obj_union_map
;
820 list
->obj
[3].v
= sched
;
822 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
823 !list
->obj
[2].v
|| !list
->obj
[3].v
)
835 static int add_point(__isl_take isl_point
*pnt
, void *user
)
837 isl_union_set
**scan
= (isl_union_set
**) user
;
839 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
844 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
848 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
850 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
851 isl_union_set_free(scan
);
855 isl_union_set_free(uset
);
859 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
861 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
864 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
865 __isl_take isl_union_pw_qpolynomial
*upwqp
)
867 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
870 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
871 __isl_take isl_union_pw_qpolynomial
*upwqp
)
873 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
876 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
877 __isl_take isl_union_pw_qpolynomial
*upwqp
)
879 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
882 typedef void *(*isc_un_op_fn
)(void *arg
);
884 enum isl_token_type op
;
889 struct isc_named_un_op
{
893 struct isc_named_un_op named_un_ops
[] = {
894 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
895 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
896 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
897 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
898 {"card", { -1, isl_obj_union_set
,
899 isl_obj_union_pw_qpolynomial
,
900 (isc_un_op_fn
) &isl_union_set_card
} },
901 {"card", { -1, isl_obj_union_map
,
902 isl_obj_union_pw_qpolynomial
,
903 (isc_un_op_fn
) &isl_union_map_card
} },
904 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
905 (isc_un_op_fn
) &isl_union_set_coalesce
} },
906 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
907 (isc_un_op_fn
) &isl_union_map_coalesce
} },
908 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
909 isl_obj_union_pw_qpolynomial
,
910 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
911 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
912 isl_obj_union_pw_qpolynomial_fold
,
913 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
915 {"codegen", { -1, isl_obj_union_set
, isl_obj_none
,
917 {"codegen", { -1, isl_obj_union_map
, isl_obj_none
,
920 {"coefficients", { -1, isl_obj_union_set
,
922 (isc_un_op_fn
) &isl_union_set_coefficients
} },
923 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
924 (isc_un_op_fn
) &isl_union_set_solutions
} },
925 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
926 (isc_un_op_fn
) &isl_union_map_deltas
} },
927 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
928 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
929 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
930 (isc_un_op_fn
) &isl_union_map_domain
} },
931 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
932 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
933 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
935 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
936 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
937 (isc_un_op_fn
) &isl_union_map_domain
} },
938 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
940 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
941 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
943 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
944 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
945 (isc_un_op_fn
) &isl_union_map_domain_map
} },
946 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
947 (isc_un_op_fn
) &isl_union_map_range
} },
948 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
949 (isc_un_op_fn
) &isl_union_map_range
} },
950 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
951 (isc_un_op_fn
) &isl_union_map_range_map
} },
952 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
953 (isc_un_op_fn
) &isl_union_set_identity
} },
954 {"lattice_width", { -1, isl_obj_union_set
,
955 isl_obj_union_pw_qpolynomial
,
956 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
957 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
958 (isc_un_op_fn
) &isl_union_map_lexmin
} },
959 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
960 (isc_un_op_fn
) &isl_union_map_lexmax
} },
961 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
962 (isc_un_op_fn
) &isl_union_set_lexmin
} },
963 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
964 (isc_un_op_fn
) &isl_union_set_lexmax
} },
965 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
966 (isc_un_op_fn
) &isl_union_set_lift
} },
967 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
968 (isc_un_op_fn
) &isl_union_map_params
} },
969 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
970 (isc_un_op_fn
) &isl_union_set_params
} },
971 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
972 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
973 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
974 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
975 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
976 isl_obj_union_pw_qpolynomial
,
977 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
978 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
979 isl_obj_union_pw_qpolynomial
,
980 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
981 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
982 isl_obj_union_pw_qpolynomial
,
983 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
985 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
986 (isc_un_op_fn
) &parse
} },
988 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
989 (isc_un_op_fn
) &union_map_power
} },
990 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
991 (isc_un_op_fn
) &union_set_sample
} },
992 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
993 (isc_un_op_fn
) &union_map_sample
} },
994 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
995 (isc_un_op_fn
) &union_set_scan
} },
996 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
997 (isc_un_op_fn
) &union_map_scan
} },
998 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
999 isl_obj_union_pw_qpolynomial
,
1000 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
1001 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
1002 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
1003 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
1004 (isc_un_op_fn
) &isl_union_set_unwrap
} },
1005 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
1006 (isc_un_op_fn
) &isl_union_map_wrap
} },
1007 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
1008 (isc_un_op_fn
) &isl_union_map_zip
} },
1012 struct isl_named_obj
{
1017 static void free_obj(struct isl_obj obj
)
1019 obj
.type
->free(obj
.v
);
1022 static int same_name(const void *entry
, const void *val
)
1024 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1026 return !strcmp(named
->name
, val
);
1029 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1030 char *name
, struct isl_obj obj
)
1032 struct isl_hash_table_entry
*entry
;
1034 struct isl_named_obj
*named
;
1036 name_hash
= isl_hash_string(isl_hash_init(), name
);
1037 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1041 named
= entry
->data
;
1042 free_obj(named
->obj
);
1045 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1049 entry
->data
= named
;
1060 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1061 struct isl_hash_table
*table
, char *name
)
1063 struct isl_obj obj
= { isl_obj_none
, NULL
};
1064 struct isl_hash_table_entry
*entry
;
1067 name_hash
= isl_hash_string(isl_hash_init(), name
);
1068 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1070 struct isl_named_obj
*named
;
1071 named
= entry
->data
;
1073 } else if (isdigit(name
[0]))
1074 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1076 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1079 obj
.v
= obj
.type
->copy(obj
.v
);
1083 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1085 if (obj
.type
== super
)
1087 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1089 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1091 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1092 super
== isl_obj_union_pw_qpolynomial
)
1094 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1095 super
== isl_obj_union_pw_qpolynomial_fold
)
1097 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1099 if (obj
.type
== isl_obj_list
) {
1100 struct isl_list
*list
= obj
.v
;
1101 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1102 return is_subtype(list
->obj
[0], super
);
1104 if (super
== isl_obj_str
)
1109 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1111 struct isl_list
*list
= obj
.v
;
1114 obj
.v
= obj
.type
->copy(obj
.v
);
1116 isl_list_free(list
);
1121 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1124 if (obj
.type
== type
)
1126 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1127 obj
.type
= isl_obj_union_map
;
1128 obj
.v
= isl_union_map_from_map(obj
.v
);
1131 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1132 obj
.type
= isl_obj_union_set
;
1133 obj
.v
= isl_union_set_from_set(obj
.v
);
1136 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1137 type
== isl_obj_union_pw_qpolynomial
) {
1138 obj
.type
= isl_obj_union_pw_qpolynomial
;
1139 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1142 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1143 type
== isl_obj_union_pw_qpolynomial_fold
) {
1144 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1145 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1148 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1149 if (type
== isl_obj_union_map
) {
1150 obj
.type
= isl_obj_union_map
;
1153 if (type
== isl_obj_union_pw_qpolynomial
) {
1154 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1155 isl_union_set_free(obj
.v
);
1156 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1157 obj
.type
= isl_obj_union_pw_qpolynomial
;
1160 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1161 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1162 isl_union_set_free(obj
.v
);
1163 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1165 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1169 if (obj
.type
== isl_obj_list
) {
1170 struct isl_list
*list
= obj
.v
;
1171 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1172 return convert(ctx
, obj_at(obj
, 0), type
);
1174 if (type
== isl_obj_str
) {
1179 p
= isl_printer_to_str(ctx
);
1182 p
= obj
.type
->print(p
, obj
.v
);
1183 s
= isl_printer_get_str(p
);
1184 isl_printer_free(p
);
1186 str
= isl_str_from_string(ctx
, s
);
1191 obj
.type
= isl_obj_str
;
1197 obj
.type
= isl_obj_none
;
1202 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1206 struct isl_token
*tok
;
1208 tok
= isl_stream_next_token(s
);
1212 for (i
= 0; ; ++i
) {
1215 if (bin_ops
[i
].op
!= tok
->type
)
1217 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1220 isl_token_free(tok
);
1224 for (i
= 0; ; ++i
) {
1225 if (!named_bin_ops
[i
].name
)
1227 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1229 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1232 isl_token_free(tok
);
1233 return &named_bin_ops
[i
].op
;
1236 isl_stream_push_token(s
, tok
);
1241 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1244 struct isl_token
*tok
;
1246 tok
= isl_stream_next_token(s
);
1250 for (i
= 0; ; ++i
) {
1251 if (!named_un_ops
[i
].name
)
1253 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1256 isl_token_free(tok
);
1257 return &named_un_ops
[i
].op
;
1260 isl_stream_push_token(s
, tok
);
1265 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1270 for (i
= 0; ; ++i
) {
1271 if (!named_un_ops
[i
].name
)
1273 if (named_un_ops
[i
].op
.op
!= like
->op
)
1275 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1278 return &named_un_ops
[i
].op
;
1284 static int is_assign(struct isl_stream
*s
)
1286 struct isl_token
*tok
;
1287 struct isl_token
*tok2
;
1290 tok
= isl_stream_next_token(s
);
1293 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1294 isl_stream_push_token(s
, tok
);
1298 tok2
= isl_stream_next_token(s
);
1300 isl_stream_push_token(s
, tok
);
1303 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1304 isl_stream_push_token(s
, tok2
);
1305 isl_stream_push_token(s
, tok
);
1310 static struct isl_obj
read_obj(struct isl_stream
*s
,
1311 struct isl_hash_table
*table
);
1312 static struct isl_obj
read_expr(struct isl_stream
*s
,
1313 struct isl_hash_table
*table
);
1315 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1316 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1318 struct isl_obj obj
= { isl_obj_none
, NULL
};
1320 obj
= read_obj(s
, table
);
1324 op
= find_matching_un_op(op
, obj
);
1327 isl_die(s
->ctx
, isl_error_invalid
,
1328 "no such unary operator defined on given operand",
1331 obj
= convert(s
->ctx
, obj
, op
->arg
);
1332 obj
.v
= op
->fn(obj
.v
);
1338 obj
.type
= isl_obj_none
;
1343 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1345 struct isl_list
*list
;
1348 if (obj
.type
!= isl_obj_union_map
)
1349 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1350 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1351 list
= isl_list_alloc(ctx
, 2);
1355 list
->obj
[0].type
= isl_obj_union_map
;
1356 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1357 list
->obj
[1].type
= isl_obj_bool
;
1358 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1360 obj
.type
= isl_obj_list
;
1361 if (exact
< 0 || !list
->obj
[0].v
)
1367 obj
.type
= isl_obj_none
;
1372 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1374 struct isl_list
*list
= obj
.v
;
1375 struct isl_token
*tok
;
1378 tok
= isl_stream_next_token(s
);
1379 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1380 isl_stream_error(s
, tok
, "expecting index");
1382 isl_stream_push_token(s
, tok
);
1385 i
= isl_int_get_si(tok
->u
.v
);
1386 isl_token_free(tok
);
1387 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1388 if (isl_stream_eat(s
, ']'))
1391 return obj_at(obj
, i
);
1394 obj
.type
= isl_obj_none
;
1399 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1400 struct isl_hash_table
*table
)
1404 obj
= read_expr(s
, table
);
1405 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1406 is_subtype(obj
, isl_obj_union_map
), goto error
);
1408 if (obj
.type
== isl_obj_list
) {
1409 struct isl_list
*list
= obj
.v
;
1410 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1411 obj
= obj_at(obj
, 0);
1413 if (obj
.type
== isl_obj_set
)
1414 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1415 else if (obj
.type
== isl_obj_map
)
1416 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1417 if (obj
.type
== isl_obj_union_set
) {
1418 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1420 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1424 if (isl_stream_eat(s
, ')'))
1429 isl_union_map_free(umap
);
1432 obj
.type
= isl_obj_none
;
1437 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1438 __isl_take isl_union_set
*uset
)
1440 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1441 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1443 obj
.type
= isl_obj_list
;
1444 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1449 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1450 __isl_take isl_union_map
*umap
)
1452 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1453 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1455 obj
.type
= isl_obj_list
;
1456 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1461 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1462 struct isl_obj obj
, struct isl_hash_table
*table
)
1466 arg
= read_expr(s
, table
);
1467 if (!is_subtype(arg
, isl_obj_union_map
) &&
1468 !is_subtype(arg
, isl_obj_union_set
))
1469 isl_die(s
->ctx
, isl_error_invalid
,
1470 "expecting set of map argument", goto error
);
1472 if (arg
.type
== isl_obj_list
) {
1473 struct isl_list
*list
= arg
.v
;
1474 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1475 arg
= obj_at(arg
, 0);
1477 if (arg
.type
== isl_obj_set
)
1478 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1479 else if (arg
.type
== isl_obj_map
)
1480 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1481 if (arg
.type
== isl_obj_union_set
)
1482 obj
= apply_fun_set(obj
, arg
.v
);
1484 obj
= apply_fun_map(obj
, arg
.v
);
1488 if (isl_stream_eat(s
, ')'))
1496 obj
.type
= isl_obj_none
;
1501 struct add_vertex_data
{
1502 struct isl_list
*list
;
1506 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1508 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1509 isl_basic_set
*expr
;
1511 expr
= isl_vertex_get_expr(vertex
);
1513 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1514 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1517 isl_vertex_free(vertex
);
1522 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1525 isl_basic_set
*hull
;
1526 isl_vertices
*vertices
= NULL
;
1527 struct isl_list
*list
= NULL
;
1529 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1531 set
= isl_set_remove_divs(set
);
1532 hull
= isl_set_convex_hull(set
);
1533 vertices
= isl_basic_set_compute_vertices(hull
);
1534 isl_basic_set_free(hull
);
1538 ctx
= isl_vertices_get_ctx(vertices
);
1539 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1544 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1546 data
->list
= isl_list_concat(list
, data
->list
);
1548 isl_vertices_free(vertices
);
1553 isl_vertices_free(vertices
);
1557 static struct isl_obj
vertices(struct isl_stream
*s
,
1558 struct isl_hash_table
*table
)
1562 struct isl_list
*list
= NULL
;
1563 isl_union_set
*uset
;
1564 struct add_vertex_data data
= { NULL
};
1566 obj
= read_expr(s
, table
);
1567 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1568 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1572 ctx
= isl_union_set_get_ctx(uset
);
1573 list
= isl_list_alloc(ctx
, 0);
1579 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1582 isl_union_set_free(uset
);
1584 obj
.type
= isl_obj_list
;
1589 isl_union_set_free(uset
);
1590 isl_list_free(data
.list
);
1592 obj
.type
= isl_obj_none
;
1597 static struct isl_obj
type_of(struct isl_stream
*s
,
1598 struct isl_hash_table
*table
)
1602 const char *type
= "unknown";
1604 obj
= read_expr(s
, table
);
1606 if (obj
.type
== isl_obj_map
||
1607 obj
.type
== isl_obj_union_map
)
1609 if (obj
.type
== isl_obj_set
||
1610 obj
.type
== isl_obj_union_set
)
1612 if (obj
.type
== isl_obj_pw_qpolynomial
||
1613 obj
.type
== isl_obj_union_pw_qpolynomial
)
1614 type
= "piecewise quasipolynomial";
1615 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1616 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1617 type
= "piecewise quasipolynomial fold";
1618 if (obj
.type
== isl_obj_list
)
1620 if (obj
.type
== isl_obj_bool
)
1622 if (obj
.type
== isl_obj_str
)
1624 if (obj
.type
== isl_obj_int
)
1628 obj
.type
= isl_obj_str
;
1629 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1634 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1635 struct isl_hash_table
*table
)
1639 obj
= read_obj(s
, table
);
1640 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1641 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1648 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1649 struct isl_hash_table
*table
)
1653 obj
= read_obj(s
, table
);
1654 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1655 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1662 static struct isl_obj
last_any(struct isl_stream
*s
,
1663 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1664 __isl_take isl_union_map
*may_source
)
1666 struct isl_obj obj
= { isl_obj_none
, NULL
};
1667 isl_union_map
*sink
= NULL
;
1668 isl_union_map
*schedule
= NULL
;
1669 isl_union_map
*may_dep
;
1670 isl_union_map
*must_dep
;
1672 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1675 sink
= read_map(s
, table
);
1679 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1682 schedule
= read_map(s
, table
);
1686 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1687 schedule
, &must_dep
, &may_dep
,
1691 obj
.type
= isl_obj_union_map
;
1692 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1696 isl_union_map_free(may_source
);
1697 isl_union_map_free(must_source
);
1698 isl_union_map_free(sink
);
1699 isl_union_map_free(schedule
);
1701 obj
.type
= isl_obj_none
;
1706 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1708 struct isl_obj obj
= { isl_obj_none
, NULL
};
1709 isl_union_map
*must_source
= NULL
;
1710 isl_union_map
*may_source
= NULL
;
1711 isl_union_map
*sink
= NULL
;
1712 isl_union_map
*schedule
= NULL
;
1713 isl_union_map
*may_dep
;
1715 may_source
= read_map(s
, table
);
1719 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1720 must_source
= read_map(s
, table
);
1723 return last_any(s
, table
, must_source
, may_source
);
1726 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1729 sink
= read_map(s
, table
);
1733 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1736 schedule
= read_map(s
, table
);
1740 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1741 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1742 schedule
, NULL
, &may_dep
,
1746 obj
.type
= isl_obj_union_map
;
1751 isl_union_map_free(may_source
);
1752 isl_union_map_free(must_source
);
1753 isl_union_map_free(sink
);
1754 isl_union_map_free(schedule
);
1756 obj
.type
= isl_obj_none
;
1761 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1763 struct isl_obj obj
= { isl_obj_none
, NULL
};
1764 struct isl_list
*list
= NULL
;
1765 isl_union_map
*must_source
= NULL
;
1766 isl_union_map
*may_source
= NULL
;
1767 isl_union_map
*sink
= NULL
;
1768 isl_union_map
*schedule
= NULL
;
1769 isl_union_map
*must_dep
;
1770 isl_union_map
*must_no_source
;
1772 must_source
= read_map(s
, table
);
1776 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1777 may_source
= read_map(s
, table
);
1780 return last_any(s
, table
, must_source
, may_source
);
1783 list
= isl_list_alloc(s
->ctx
, 2);
1787 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1790 sink
= read_map(s
, table
);
1794 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1797 schedule
= read_map(s
, table
);
1801 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1802 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1803 schedule
, &must_dep
, NULL
,
1804 &must_no_source
, NULL
) < 0) {
1805 isl_list_free(list
);
1809 list
->obj
[0].type
= isl_obj_union_map
;
1810 list
->obj
[0].v
= must_dep
;
1811 list
->obj
[1].type
= isl_obj_union_map
;
1812 list
->obj
[1].v
= must_no_source
;
1815 obj
.type
= isl_obj_list
;
1819 isl_list_free(list
);
1820 isl_union_map_free(may_source
);
1821 isl_union_map_free(must_source
);
1822 isl_union_map_free(sink
);
1823 isl_union_map_free(schedule
);
1825 obj
.type
= isl_obj_none
;
1830 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1831 struct isl_hash_table
*table
)
1833 isl_union_set
*domain
;
1834 isl_union_map
*validity
;
1835 isl_union_map
*proximity
;
1837 domain
= read_set(s
, table
);
1841 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1842 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1845 isl_union_map
*umap
;
1846 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1847 umap
= read_map(s
, table
);
1848 validity
= isl_union_map_union(validity
, umap
);
1849 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1850 umap
= read_map(s
, table
);
1851 proximity
= isl_union_map_union(proximity
, umap
);
1856 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1859 static struct isl_obj
schedule(struct isl_stream
*s
,
1860 struct isl_hash_table
*table
)
1862 struct isl_obj obj
= { isl_obj_none
, NULL
};
1863 isl_schedule
*schedule
;
1865 schedule
= get_schedule(s
, table
);
1867 obj
.v
= isl_schedule_get_map(schedule
);
1868 obj
.type
= isl_obj_union_map
;
1870 isl_schedule_free(schedule
);
1875 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1877 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1879 struct isl_obj obj
= { isl_obj_none
, NULL
};
1880 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1881 struct isl_list
*list
;
1883 list
= isl_list_alloc(ctx
, 2);
1888 obj
.type
= isl_obj_list
;
1890 list
->obj
[0].type
= isl_obj_union_map
;
1891 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1893 if (isl_band_has_children(band
)) {
1894 isl_band_list
*children
;
1896 children
= isl_band_get_children(band
);
1897 list
->obj
[1] = band_list_to_obj_list(children
);
1899 list
->obj
[1].type
= isl_obj_list
;
1900 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1903 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1906 isl_band_free(band
);
1910 isl_band_free(band
);
1912 obj
.type
= isl_obj_none
;
1917 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1919 struct isl_obj obj
= { isl_obj_none
, NULL
};
1920 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1921 struct isl_list
*list
;
1924 n
= isl_band_list_n_band(bands
);
1925 list
= isl_list_alloc(ctx
, n
);
1930 obj
.type
= isl_obj_list
;
1932 for (i
= 0; i
< n
; ++i
) {
1935 band
= isl_band_list_get_band(bands
, i
);
1936 list
->obj
[i
] = band_to_obj_list(band
);
1937 if (!list
->obj
[i
].v
)
1941 isl_band_list_free(bands
);
1945 isl_band_list_free(bands
);
1947 obj
.type
= isl_obj_none
;
1952 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1953 struct isl_hash_table
*table
)
1955 struct isl_obj obj
= { isl_obj_none
, NULL
};
1956 isl_schedule
*schedule
;
1957 isl_band_list
*roots
;
1959 schedule
= get_schedule(s
, table
);
1963 roots
= isl_schedule_get_band_forest(schedule
);
1964 isl_schedule_free(schedule
);
1966 return band_list_to_obj_list(roots
);
1969 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1971 struct isl_token
*tok
;
1973 if (isl_stream_eat_if_available(s
, '+'))
1974 return transitive_closure(s
->ctx
, obj
);
1976 tok
= isl_stream_next_token(s
);
1977 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1978 isl_stream_error(s
, tok
, "expecting -1");
1980 isl_stream_push_token(s
, tok
);
1983 isl_token_free(tok
);
1984 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1985 if (obj
.type
!= isl_obj_union_map
)
1986 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1988 obj
.v
= isl_union_map_reverse(obj
.v
);
1995 obj
.type
= isl_obj_none
;
2000 static struct isl_obj
check_assert(struct isl_stream
*s
,
2001 struct isl_hash_table
*table
)
2005 obj
= read_expr(s
, table
);
2006 if (obj
.type
!= isl_obj_bool
)
2007 isl_die(s
->ctx
, isl_error_invalid
,
2008 "expecting boolean expression", goto error
);
2009 if (obj
.v
!= &isl_bool_true
)
2010 isl_die(s
->ctx
, isl_error_unknown
,
2011 "assertion failed", abort());
2014 obj
.type
= isl_obj_none
;
2019 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2022 struct isl_token
*tok
;
2023 struct isl_stream
*s_file
;
2024 struct iscc_options
*options
;
2027 tok
= isl_stream_next_token(s
);
2028 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2029 isl_stream_error(s
, tok
, "expecting filename");
2030 isl_token_free(tok
);
2034 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2035 if (!options
|| !options
->io
) {
2036 isl_token_free(tok
);
2037 isl_die(s
->ctx
, isl_error_invalid
,
2038 "read operation not allowed", goto error
);
2041 file
= fopen(tok
->u
.s
, "r");
2042 isl_token_free(tok
);
2043 isl_assert(s
->ctx
, file
, goto error
);
2045 s_file
= isl_stream_new_file(s
->ctx
, file
);
2051 obj
= isl_stream_read_obj(s_file
);
2053 isl_stream_free(s_file
);
2058 obj
.type
= isl_obj_none
;
2063 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2064 struct isl_hash_table
*table
)
2067 struct isl_token
*tok
;
2068 struct isl_stream
*s_file
;
2069 struct iscc_options
*options
;
2073 tok
= isl_stream_next_token(s
);
2074 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2075 isl_stream_error(s
, tok
, "expecting filename");
2076 isl_token_free(tok
);
2080 obj
= read_expr(s
, table
);
2082 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2083 if (!options
|| !options
->io
) {
2084 isl_token_free(tok
);
2085 isl_die(s
->ctx
, isl_error_invalid
,
2086 "write operation not allowed", goto error
);
2089 file
= fopen(tok
->u
.s
, "w");
2090 isl_token_free(tok
);
2092 isl_die(s
->ctx
, isl_error_unknown
,
2093 "could not open file for writing", goto error
);
2095 p
= isl_printer_to_file(s
->ctx
, file
);
2096 p
= isl_printer_set_output_format(p
, options
->format
);
2097 p
= obj
.type
->print(p
, obj
.v
);
2098 p
= isl_printer_end_line(p
);
2099 isl_printer_free(p
);
2104 obj
.type
= isl_obj_none
;
2109 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2111 struct isl_token
*tok
;
2112 struct isl_obj obj
= { isl_obj_none
, NULL
};
2114 tok
= isl_stream_next_token(s
);
2117 if (tok
->type
== ISL_TOKEN_STRING
) {
2119 str
= isl_str_alloc(s
->ctx
);
2122 str
->s
= strdup(tok
->u
.s
);
2123 isl_token_free(tok
);
2125 obj
.type
= isl_obj_str
;
2127 isl_stream_push_token(s
, tok
);
2130 isl_token_free(tok
);
2134 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2136 struct isl_token
*tok
;
2137 struct isl_obj obj
= { isl_obj_none
, NULL
};
2139 tok
= isl_stream_next_token(s
);
2142 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2143 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2144 isl_token_free(tok
);
2145 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2146 obj
.type
= isl_obj_bool
;
2148 isl_stream_push_token(s
, tok
);
2151 isl_token_free(tok
);
2155 static __isl_give
char *read_ident(struct isl_stream
*s
)
2158 struct isl_token
*tok
, *tok2
;
2160 name
= isl_stream_read_ident_if_available(s
);
2164 tok
= isl_stream_next_token(s
);
2167 if (tok
->type
!= '$') {
2168 isl_stream_push_token(s
, tok
);
2171 tok2
= isl_stream_next_token(s
);
2172 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2174 isl_stream_push_token(s
, tok2
);
2175 isl_stream_push_token(s
, tok
);
2179 name
= isl_int_get_str(tok2
->u
.v
);
2180 isl_token_free(tok
);
2181 isl_token_free(tok2
);
2186 static struct isl_obj
read_list(struct isl_stream
*s
,
2187 struct isl_hash_table
*table
, struct isl_obj obj
)
2189 struct isl_list
*list
;
2191 list
= isl_list_alloc(s
->ctx
, 2);
2195 list
->obj
[1] = read_obj(s
, table
);
2197 obj
.type
= isl_obj_list
;
2199 if (!list
->obj
[1].v
)
2202 while (isl_stream_eat_if_available(s
, ',')) {
2203 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2211 obj
.type
= isl_obj_none
;
2216 static struct isl_obj
read_obj(struct isl_stream
*s
,
2217 struct isl_hash_table
*table
)
2219 struct isl_obj obj
= { isl_obj_none
, NULL
};
2221 struct isc_un_op
*op
= NULL
;
2223 obj
= read_string_if_available(s
);
2226 obj
= read_bool_if_available(s
);
2229 if (isl_stream_eat_if_available(s
, '(')) {
2230 if (isl_stream_next_token_is(s
, ')')) {
2231 obj
.type
= isl_obj_list
;
2232 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2234 obj
= read_expr(s
, table
);
2235 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2236 obj
= read_list(s
, table
, obj
);
2238 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2241 op
= read_prefix_un_op_if_available(s
);
2243 return read_un_op_expr(s
, table
, op
);
2245 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2246 return check_assert(s
, table
);
2247 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2248 return read_from_file(s
);
2249 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2250 return write_to_file(s
, table
);
2251 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2252 return vertices(s
, table
);
2253 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2254 return any(s
, table
);
2255 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2256 return last(s
, table
);
2257 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2258 return schedule(s
, table
);
2259 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2260 return schedule_forest(s
, table
);
2261 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2262 return type_of(s
, table
);
2264 name
= read_ident(s
);
2266 obj
= stored_obj(s
->ctx
, table
, name
);
2268 obj
= isl_stream_read_obj(s
);
2273 if (isl_stream_eat_if_available(s
, '^'))
2274 obj
= power(s
, obj
);
2275 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2276 obj
= obj_at_index(s
, obj
);
2277 else if (is_subtype(obj
, isl_obj_union_map
) &&
2278 isl_stream_eat_if_available(s
, '(')) {
2279 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2280 obj
= apply(s
, obj
.v
, table
);
2281 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2282 isl_stream_eat_if_available(s
, '(')) {
2283 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2284 obj
= apply_fun(s
, obj
, table
);
2285 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2286 isl_stream_eat_if_available(s
, '(')) {
2287 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2288 obj
= apply_fun(s
, obj
, table
);
2294 obj
.type
= isl_obj_none
;
2299 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2300 struct isl_obj lhs
, struct isl_obj rhs
)
2304 for (i
= 0; ; ++i
) {
2307 if (bin_ops
[i
].op
!= like
->op
)
2309 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2311 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2317 for (i
= 0; ; ++i
) {
2318 if (!named_bin_ops
[i
].name
)
2320 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2322 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2324 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2327 return &named_bin_ops
[i
].op
;
2333 static int next_is_neg_int(struct isl_stream
*s
)
2335 struct isl_token
*tok
;
2338 tok
= isl_stream_next_token(s
);
2339 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2340 isl_stream_push_token(s
, tok
);
2345 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2346 struct isl_obj lhs
, struct isl_obj rhs
)
2350 lhs
= convert(ctx
, lhs
, op
->lhs
);
2351 rhs
= convert(ctx
, rhs
, op
->rhs
);
2352 if (op
->res
!= isl_obj_bool
)
2353 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2355 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2358 obj
.v
= isl_bool_from_int(res
);
2365 static struct isl_obj
read_expr(struct isl_stream
*s
,
2366 struct isl_hash_table
*table
)
2368 struct isl_obj obj
= { isl_obj_none
, NULL
};
2369 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2371 obj
= read_obj(s
, table
);
2373 struct isc_bin_op
*op
= NULL
;
2375 op
= read_bin_op_if_available(s
, obj
);
2379 right_obj
= read_obj(s
, table
);
2381 op
= find_matching_bin_op(op
, obj
, right_obj
);
2384 isl_die(s
->ctx
, isl_error_invalid
,
2385 "no such binary operator defined on given operands",
2388 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2391 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2392 right_obj
= read_obj(s
, table
);
2393 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2398 free_obj(right_obj
);
2400 obj
.type
= isl_obj_none
;
2405 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2406 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2408 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2409 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2411 struct isl_obj obj
= { isl_obj_none
, NULL
};
2415 struct isc_bin_op
*op
= NULL
;
2420 if (isl_stream_is_empty(s
))
2423 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2424 return source_file(s
, table
, p
);
2426 assign
= is_assign(s
);
2428 lhs
= isl_stream_read_ident_if_available(s
);
2429 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2431 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2436 obj
= read_expr(s
, table
);
2437 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2438 fprintf(stderr
, "Interrupted\n");
2439 isl_ctx_reset_error(s
->ctx
);
2441 if (isl_stream_eat(s
, ';'))
2445 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2446 p
= obj
.type
->print(p
, obj
.v
);
2447 p
= isl_printer_end_line(p
);
2452 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2453 static int count
= 0;
2454 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2455 lhs
= strdup(buf
+ 1);
2457 p
= isl_printer_print_str(p
, buf
);
2458 p
= isl_printer_print_str(p
, " := ");
2459 p
= obj
.type
->print(p
, obj
.v
);
2460 p
= isl_printer_end_line(p
);
2462 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2467 isl_stream_flush_tokens(s
);
2468 isl_stream_skip_line(s
);
2474 int free_cb(void **entry
, void *user
)
2476 struct isl_named_obj
*named
= *entry
;
2478 free_obj(named
->obj
);
2485 static void register_named_ops(struct isl_stream
*s
)
2489 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2490 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2491 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2494 for (i
= 0; ; ++i
) {
2495 if (!named_un_ops
[i
].name
)
2497 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2498 named_un_ops
[i
].name
);
2499 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2502 for (i
= 0; ; ++i
) {
2503 if (!named_bin_ops
[i
].name
)
2505 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2506 named_bin_ops
[i
].name
);
2507 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2511 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2512 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2514 struct isl_token
*tok
;
2515 struct isl_stream
*s_file
;
2518 tok
= isl_stream_next_token(s
);
2519 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2520 isl_stream_error(s
, tok
, "expecting filename");
2521 isl_token_free(tok
);
2525 file
= fopen(tok
->u
.s
, "r");
2526 isl_token_free(tok
);
2527 isl_assert(s
->ctx
, file
, return p
);
2529 s_file
= isl_stream_new_file(s
->ctx
, file
);
2535 register_named_ops(s_file
);
2537 while (!s_file
->eof
)
2538 p
= read_line(s_file
, table
, p
, 0);
2540 isl_stream_free(s_file
);
2543 isl_stream_eat(s
, ';');
2548 int main(int argc
, char **argv
)
2550 struct isl_ctx
*ctx
;
2551 struct isl_stream
*s
;
2552 struct isl_hash_table
*table
;
2553 struct iscc_options
*options
;
2555 int tty
= isatty(0);
2557 options
= iscc_options_new_with_defaults();
2560 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2561 pet_options_set_autodetect(ctx
, 1);
2562 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2563 s
= isl_stream_new_file(ctx
, stdin
);
2565 table
= isl_hash_table_alloc(ctx
, 10);
2567 p
= isl_printer_to_file(ctx
, stdout
);
2568 p
= isl_printer_set_output_format(p
, options
->format
);
2571 register_named_ops(s
);
2573 install_signal_handler(ctx
);
2575 while (p
&& !s
->eof
) {
2576 isl_ctx_resume(ctx
);
2577 p
= read_line(s
, table
, p
, tty
);
2580 remove_signal_handler(ctx
);
2582 isl_printer_free(p
);
2583 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2584 isl_hash_table_free(ctx
, table
);