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 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
959 (isc_un_op_fn
) &isl_union_map_params
} },
960 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
961 (isc_un_op_fn
) &isl_union_set_params
} },
962 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
963 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
964 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
965 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
966 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
967 isl_obj_union_pw_qpolynomial
,
968 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
969 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
970 isl_obj_union_pw_qpolynomial
,
971 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
972 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
973 isl_obj_union_pw_qpolynomial
,
974 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
976 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
977 (isc_un_op_fn
) &parse
} },
979 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
980 (isc_un_op_fn
) &union_map_power
} },
981 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
982 (isc_un_op_fn
) &union_set_sample
} },
983 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
984 (isc_un_op_fn
) &union_map_sample
} },
985 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
986 (isc_un_op_fn
) &union_set_scan
} },
987 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
988 (isc_un_op_fn
) &union_map_scan
} },
989 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
990 isl_obj_union_pw_qpolynomial
,
991 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
992 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
993 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
994 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
995 (isc_un_op_fn
) &isl_union_set_unwrap
} },
996 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
997 (isc_un_op_fn
) &isl_union_map_wrap
} },
998 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
999 (isc_un_op_fn
) &isl_union_map_zip
} },
1003 struct isl_named_obj
{
1008 static void free_obj(struct isl_obj obj
)
1010 obj
.type
->free(obj
.v
);
1013 static int same_name(const void *entry
, const void *val
)
1015 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1017 return !strcmp(named
->name
, val
);
1020 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1021 char *name
, struct isl_obj obj
)
1023 struct isl_hash_table_entry
*entry
;
1025 struct isl_named_obj
*named
;
1027 name_hash
= isl_hash_string(isl_hash_init(), name
);
1028 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1032 named
= entry
->data
;
1033 free_obj(named
->obj
);
1036 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1040 entry
->data
= named
;
1051 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1052 struct isl_hash_table
*table
, char *name
)
1054 struct isl_obj obj
= { isl_obj_none
, NULL
};
1055 struct isl_hash_table_entry
*entry
;
1058 name_hash
= isl_hash_string(isl_hash_init(), name
);
1059 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1061 struct isl_named_obj
*named
;
1062 named
= entry
->data
;
1064 } else if (isdigit(name
[0]))
1065 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1067 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1070 obj
.v
= obj
.type
->copy(obj
.v
);
1074 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1076 if (obj
.type
== super
)
1078 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1080 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1082 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1083 super
== isl_obj_union_pw_qpolynomial
)
1085 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1086 super
== isl_obj_union_pw_qpolynomial_fold
)
1088 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1090 if (obj
.type
== isl_obj_list
) {
1091 struct isl_list
*list
= obj
.v
;
1092 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1093 return is_subtype(list
->obj
[0], super
);
1095 if (super
== isl_obj_str
)
1100 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1102 struct isl_list
*list
= obj
.v
;
1105 obj
.v
= obj
.type
->copy(obj
.v
);
1107 isl_list_free(list
);
1112 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1115 if (obj
.type
== type
)
1117 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1118 obj
.type
= isl_obj_union_map
;
1119 obj
.v
= isl_union_map_from_map(obj
.v
);
1122 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1123 obj
.type
= isl_obj_union_set
;
1124 obj
.v
= isl_union_set_from_set(obj
.v
);
1127 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1128 type
== isl_obj_union_pw_qpolynomial
) {
1129 obj
.type
= isl_obj_union_pw_qpolynomial
;
1130 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1133 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1134 type
== isl_obj_union_pw_qpolynomial_fold
) {
1135 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1136 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1139 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1140 if (type
== isl_obj_union_map
) {
1141 obj
.type
= isl_obj_union_map
;
1144 if (type
== isl_obj_union_pw_qpolynomial
) {
1145 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1146 isl_union_set_free(obj
.v
);
1147 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1148 obj
.type
= isl_obj_union_pw_qpolynomial
;
1151 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1152 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1153 isl_union_set_free(obj
.v
);
1154 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1156 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1160 if (obj
.type
== isl_obj_list
) {
1161 struct isl_list
*list
= obj
.v
;
1162 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1163 return convert(ctx
, obj_at(obj
, 0), type
);
1165 if (type
== isl_obj_str
) {
1170 p
= isl_printer_to_str(ctx
);
1173 p
= obj
.type
->print(p
, obj
.v
);
1174 s
= isl_printer_get_str(p
);
1175 isl_printer_free(p
);
1177 str
= isl_str_from_string(ctx
, s
);
1182 obj
.type
= isl_obj_str
;
1188 obj
.type
= isl_obj_none
;
1193 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1197 struct isl_token
*tok
;
1199 tok
= isl_stream_next_token(s
);
1203 for (i
= 0; ; ++i
) {
1206 if (bin_ops
[i
].op
!= tok
->type
)
1208 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1211 isl_token_free(tok
);
1215 for (i
= 0; ; ++i
) {
1216 if (!named_bin_ops
[i
].name
)
1218 if (named_bin_ops
[i
].op
.op
!= tok
->type
)
1220 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1223 isl_token_free(tok
);
1224 return &named_bin_ops
[i
].op
;
1227 isl_stream_push_token(s
, tok
);
1232 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1235 struct isl_token
*tok
;
1237 tok
= isl_stream_next_token(s
);
1241 for (i
= 0; ; ++i
) {
1242 if (!named_un_ops
[i
].name
)
1244 if (named_un_ops
[i
].op
.op
!= tok
->type
)
1247 isl_token_free(tok
);
1248 return &named_un_ops
[i
].op
;
1251 isl_stream_push_token(s
, tok
);
1256 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1261 for (i
= 0; ; ++i
) {
1262 if (!named_un_ops
[i
].name
)
1264 if (named_un_ops
[i
].op
.op
!= like
->op
)
1266 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1269 return &named_un_ops
[i
].op
;
1275 static int is_assign(struct isl_stream
*s
)
1277 struct isl_token
*tok
;
1278 struct isl_token
*tok2
;
1281 tok
= isl_stream_next_token(s
);
1284 if (tok
->type
!= ISL_TOKEN_IDENT
) {
1285 isl_stream_push_token(s
, tok
);
1289 tok2
= isl_stream_next_token(s
);
1291 isl_stream_push_token(s
, tok
);
1294 assign
= tok2
->type
== ISL_TOKEN_DEF
;
1295 isl_stream_push_token(s
, tok2
);
1296 isl_stream_push_token(s
, tok
);
1301 static struct isl_obj
read_obj(struct isl_stream
*s
,
1302 struct isl_hash_table
*table
);
1303 static struct isl_obj
read_expr(struct isl_stream
*s
,
1304 struct isl_hash_table
*table
);
1306 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1307 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1309 struct isl_obj obj
= { isl_obj_none
, NULL
};
1311 obj
= read_obj(s
, table
);
1315 op
= find_matching_un_op(op
, obj
);
1318 isl_die(s
->ctx
, isl_error_invalid
,
1319 "no such unary operator defined on given operand",
1322 obj
= convert(s
->ctx
, obj
, op
->arg
);
1323 obj
.v
= op
->fn(obj
.v
);
1329 obj
.type
= isl_obj_none
;
1334 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1336 struct isl_list
*list
;
1339 if (obj
.type
!= isl_obj_union_map
)
1340 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1341 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1342 list
= isl_list_alloc(ctx
, 2);
1346 list
->obj
[0].type
= isl_obj_union_map
;
1347 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1348 list
->obj
[1].type
= isl_obj_bool
;
1349 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1351 obj
.type
= isl_obj_list
;
1352 if (exact
< 0 || !list
->obj
[0].v
)
1358 obj
.type
= isl_obj_none
;
1363 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1365 struct isl_list
*list
= obj
.v
;
1366 struct isl_token
*tok
;
1369 tok
= isl_stream_next_token(s
);
1370 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
) {
1371 isl_stream_error(s
, tok
, "expecting index");
1373 isl_stream_push_token(s
, tok
);
1376 i
= isl_int_get_si(tok
->u
.v
);
1377 isl_token_free(tok
);
1378 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1379 if (isl_stream_eat(s
, ']'))
1382 return obj_at(obj
, i
);
1385 obj
.type
= isl_obj_none
;
1390 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1391 struct isl_hash_table
*table
)
1395 obj
= read_expr(s
, table
);
1396 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1397 is_subtype(obj
, isl_obj_union_map
), goto error
);
1399 if (obj
.type
== isl_obj_list
) {
1400 struct isl_list
*list
= obj
.v
;
1401 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1402 obj
= obj_at(obj
, 0);
1404 if (obj
.type
== isl_obj_set
)
1405 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1406 else if (obj
.type
== isl_obj_map
)
1407 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1408 if (obj
.type
== isl_obj_union_set
) {
1409 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1411 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1415 if (isl_stream_eat(s
, ')'))
1420 isl_union_map_free(umap
);
1423 obj
.type
= isl_obj_none
;
1428 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1429 __isl_take isl_union_set
*uset
)
1431 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1432 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1434 obj
.type
= isl_obj_list
;
1435 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1440 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1441 __isl_take isl_union_map
*umap
)
1443 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1444 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1446 obj
.type
= isl_obj_list
;
1447 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1452 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1453 struct isl_obj obj
, struct isl_hash_table
*table
)
1457 arg
= read_expr(s
, table
);
1458 if (!is_subtype(arg
, isl_obj_union_map
) &&
1459 !is_subtype(arg
, isl_obj_union_set
))
1460 isl_die(s
->ctx
, isl_error_invalid
,
1461 "expecting set of map argument", goto error
);
1463 if (arg
.type
== isl_obj_list
) {
1464 struct isl_list
*list
= arg
.v
;
1465 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1466 arg
= obj_at(arg
, 0);
1468 if (arg
.type
== isl_obj_set
)
1469 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1470 else if (arg
.type
== isl_obj_map
)
1471 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1472 if (arg
.type
== isl_obj_union_set
)
1473 obj
= apply_fun_set(obj
, arg
.v
);
1475 obj
= apply_fun_map(obj
, arg
.v
);
1479 if (isl_stream_eat(s
, ')'))
1487 obj
.type
= isl_obj_none
;
1492 struct add_vertex_data
{
1493 struct isl_list
*list
;
1497 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1499 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1500 isl_basic_set
*expr
;
1502 expr
= isl_vertex_get_expr(vertex
);
1504 data
->list
->obj
[data
->i
].type
= isl_obj_set
;
1505 data
->list
->obj
[data
->i
].v
= isl_set_from_basic_set(expr
);
1508 isl_vertex_free(vertex
);
1513 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1516 isl_basic_set
*hull
;
1517 isl_vertices
*vertices
= NULL
;
1518 struct isl_list
*list
= NULL
;
1520 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1522 set
= isl_set_remove_divs(set
);
1523 hull
= isl_set_convex_hull(set
);
1524 vertices
= isl_basic_set_compute_vertices(hull
);
1525 isl_basic_set_free(hull
);
1529 ctx
= isl_vertices_get_ctx(vertices
);
1530 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1535 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1537 data
->list
= isl_list_concat(list
, data
->list
);
1539 isl_vertices_free(vertices
);
1544 isl_vertices_free(vertices
);
1548 static struct isl_obj
vertices(struct isl_stream
*s
,
1549 struct isl_hash_table
*table
)
1553 struct isl_list
*list
= NULL
;
1554 isl_union_set
*uset
;
1555 struct add_vertex_data data
= { NULL
};
1557 obj
= read_expr(s
, table
);
1558 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1559 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1563 ctx
= isl_union_set_get_ctx(uset
);
1564 list
= isl_list_alloc(ctx
, 0);
1570 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1573 isl_union_set_free(uset
);
1575 obj
.type
= isl_obj_list
;
1580 isl_union_set_free(uset
);
1581 isl_list_free(data
.list
);
1583 obj
.type
= isl_obj_none
;
1588 static struct isl_obj
type_of(struct isl_stream
*s
,
1589 struct isl_hash_table
*table
)
1593 const char *type
= "unknown";
1595 obj
= read_expr(s
, table
);
1597 if (obj
.type
== isl_obj_map
||
1598 obj
.type
== isl_obj_union_map
)
1600 if (obj
.type
== isl_obj_set
||
1601 obj
.type
== isl_obj_union_set
)
1603 if (obj
.type
== isl_obj_pw_qpolynomial
||
1604 obj
.type
== isl_obj_union_pw_qpolynomial
)
1605 type
= "piecewise quasipolynomial";
1606 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1607 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1608 type
= "piecewise quasipolynomial fold";
1609 if (obj
.type
== isl_obj_list
)
1611 if (obj
.type
== isl_obj_bool
)
1613 if (obj
.type
== isl_obj_str
)
1615 if (obj
.type
== isl_obj_int
)
1619 obj
.type
= isl_obj_str
;
1620 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1625 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1626 struct isl_hash_table
*table
)
1630 obj
= read_obj(s
, table
);
1631 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1632 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1639 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1640 struct isl_hash_table
*table
)
1644 obj
= read_obj(s
, table
);
1645 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1646 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1653 static struct isl_obj
last_any(struct isl_stream
*s
,
1654 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1655 __isl_take isl_union_map
*may_source
)
1657 struct isl_obj obj
= { isl_obj_none
, NULL
};
1658 isl_union_map
*sink
= NULL
;
1659 isl_union_map
*schedule
= NULL
;
1660 isl_union_map
*may_dep
;
1661 isl_union_map
*must_dep
;
1663 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1666 sink
= read_map(s
, table
);
1670 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1673 schedule
= read_map(s
, table
);
1677 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1678 schedule
, &must_dep
, &may_dep
,
1682 obj
.type
= isl_obj_union_map
;
1683 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1687 isl_union_map_free(may_source
);
1688 isl_union_map_free(must_source
);
1689 isl_union_map_free(sink
);
1690 isl_union_map_free(schedule
);
1692 obj
.type
= isl_obj_none
;
1697 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1699 struct isl_obj obj
= { isl_obj_none
, NULL
};
1700 isl_union_map
*must_source
= NULL
;
1701 isl_union_map
*may_source
= NULL
;
1702 isl_union_map
*sink
= NULL
;
1703 isl_union_map
*schedule
= NULL
;
1704 isl_union_map
*may_dep
;
1706 may_source
= read_map(s
, table
);
1710 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1711 must_source
= read_map(s
, table
);
1714 return last_any(s
, table
, must_source
, may_source
);
1717 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1720 sink
= read_map(s
, table
);
1724 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1727 schedule
= read_map(s
, table
);
1731 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1732 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1733 schedule
, NULL
, &may_dep
,
1737 obj
.type
= isl_obj_union_map
;
1742 isl_union_map_free(may_source
);
1743 isl_union_map_free(must_source
);
1744 isl_union_map_free(sink
);
1745 isl_union_map_free(schedule
);
1747 obj
.type
= isl_obj_none
;
1752 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1754 struct isl_obj obj
= { isl_obj_none
, NULL
};
1755 struct isl_list
*list
= NULL
;
1756 isl_union_map
*must_source
= NULL
;
1757 isl_union_map
*may_source
= NULL
;
1758 isl_union_map
*sink
= NULL
;
1759 isl_union_map
*schedule
= NULL
;
1760 isl_union_map
*must_dep
;
1761 isl_union_map
*must_no_source
;
1763 must_source
= read_map(s
, table
);
1767 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1768 may_source
= read_map(s
, table
);
1771 return last_any(s
, table
, must_source
, may_source
);
1774 list
= isl_list_alloc(s
->ctx
, 2);
1778 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1781 sink
= read_map(s
, table
);
1785 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1788 schedule
= read_map(s
, table
);
1792 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1793 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1794 schedule
, &must_dep
, NULL
,
1795 &must_no_source
, NULL
) < 0) {
1796 isl_list_free(list
);
1800 list
->obj
[0].type
= isl_obj_union_map
;
1801 list
->obj
[0].v
= must_dep
;
1802 list
->obj
[1].type
= isl_obj_union_map
;
1803 list
->obj
[1].v
= must_no_source
;
1806 obj
.type
= isl_obj_list
;
1810 isl_list_free(list
);
1811 isl_union_map_free(may_source
);
1812 isl_union_map_free(must_source
);
1813 isl_union_map_free(sink
);
1814 isl_union_map_free(schedule
);
1816 obj
.type
= isl_obj_none
;
1821 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1822 struct isl_hash_table
*table
)
1824 isl_union_set
*domain
;
1825 isl_union_map
*validity
;
1826 isl_union_map
*proximity
;
1828 domain
= read_set(s
, table
);
1832 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1833 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1836 isl_union_map
*umap
;
1837 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1838 umap
= read_map(s
, table
);
1839 validity
= isl_union_map_union(validity
, umap
);
1840 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1841 umap
= read_map(s
, table
);
1842 proximity
= isl_union_map_union(proximity
, umap
);
1847 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1850 static struct isl_obj
schedule(struct isl_stream
*s
,
1851 struct isl_hash_table
*table
)
1853 struct isl_obj obj
= { isl_obj_none
, NULL
};
1854 isl_schedule
*schedule
;
1856 schedule
= get_schedule(s
, table
);
1858 obj
.v
= isl_schedule_get_map(schedule
);
1859 obj
.type
= isl_obj_union_map
;
1861 isl_schedule_free(schedule
);
1866 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1868 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1870 struct isl_obj obj
= { isl_obj_none
, NULL
};
1871 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1872 struct isl_list
*list
;
1874 list
= isl_list_alloc(ctx
, 2);
1879 obj
.type
= isl_obj_list
;
1881 list
->obj
[0].type
= isl_obj_union_map
;
1882 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1884 if (isl_band_has_children(band
)) {
1885 isl_band_list
*children
;
1887 children
= isl_band_get_children(band
);
1888 list
->obj
[1] = band_list_to_obj_list(children
);
1890 list
->obj
[1].type
= isl_obj_list
;
1891 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1894 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1897 isl_band_free(band
);
1901 isl_band_free(band
);
1903 obj
.type
= isl_obj_none
;
1908 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1910 struct isl_obj obj
= { isl_obj_none
, NULL
};
1911 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1912 struct isl_list
*list
;
1915 n
= isl_band_list_n_band(bands
);
1916 list
= isl_list_alloc(ctx
, n
);
1921 obj
.type
= isl_obj_list
;
1923 for (i
= 0; i
< n
; ++i
) {
1926 band
= isl_band_list_get_band(bands
, i
);
1927 list
->obj
[i
] = band_to_obj_list(band
);
1928 if (!list
->obj
[i
].v
)
1932 isl_band_list_free(bands
);
1936 isl_band_list_free(bands
);
1938 obj
.type
= isl_obj_none
;
1943 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1944 struct isl_hash_table
*table
)
1946 struct isl_obj obj
= { isl_obj_none
, NULL
};
1947 isl_schedule
*schedule
;
1948 isl_band_list
*roots
;
1950 schedule
= get_schedule(s
, table
);
1954 roots
= isl_schedule_get_band_forest(schedule
);
1955 isl_schedule_free(schedule
);
1957 return band_list_to_obj_list(roots
);
1960 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1962 struct isl_token
*tok
;
1964 if (isl_stream_eat_if_available(s
, '+'))
1965 return transitive_closure(s
->ctx
, obj
);
1967 tok
= isl_stream_next_token(s
);
1968 if (!tok
|| tok
->type
!= ISL_TOKEN_VALUE
|| isl_int_cmp_si(tok
->u
.v
, -1)) {
1969 isl_stream_error(s
, tok
, "expecting -1");
1971 isl_stream_push_token(s
, tok
);
1974 isl_token_free(tok
);
1975 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1976 if (obj
.type
!= isl_obj_union_map
)
1977 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1979 obj
.v
= isl_union_map_reverse(obj
.v
);
1986 obj
.type
= isl_obj_none
;
1991 static struct isl_obj
check_assert(struct isl_stream
*s
,
1992 struct isl_hash_table
*table
)
1996 obj
= read_expr(s
, table
);
1997 if (obj
.type
!= isl_obj_bool
)
1998 isl_die(s
->ctx
, isl_error_invalid
,
1999 "expecting boolean expression", goto error
);
2000 if (obj
.v
!= &isl_bool_true
)
2001 isl_die(s
->ctx
, isl_error_unknown
,
2002 "assertion failed", abort());
2005 obj
.type
= isl_obj_none
;
2010 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2013 struct isl_token
*tok
;
2014 struct isl_stream
*s_file
;
2015 struct iscc_options
*options
;
2018 tok
= isl_stream_next_token(s
);
2019 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2020 isl_stream_error(s
, tok
, "expecting filename");
2021 isl_token_free(tok
);
2025 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2026 if (!options
|| !options
->io
) {
2027 isl_token_free(tok
);
2028 isl_die(s
->ctx
, isl_error_invalid
,
2029 "read operation not allowed", goto error
);
2032 file
= fopen(tok
->u
.s
, "r");
2033 isl_token_free(tok
);
2034 isl_assert(s
->ctx
, file
, goto error
);
2036 s_file
= isl_stream_new_file(s
->ctx
, file
);
2042 obj
= isl_stream_read_obj(s_file
);
2044 isl_stream_free(s_file
);
2049 obj
.type
= isl_obj_none
;
2054 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2055 struct isl_hash_table
*table
)
2058 struct isl_token
*tok
;
2059 struct isl_stream
*s_file
;
2060 struct iscc_options
*options
;
2064 tok
= isl_stream_next_token(s
);
2065 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2066 isl_stream_error(s
, tok
, "expecting filename");
2067 isl_token_free(tok
);
2071 obj
= read_expr(s
, table
);
2073 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2074 if (!options
|| !options
->io
) {
2075 isl_token_free(tok
);
2076 isl_die(s
->ctx
, isl_error_invalid
,
2077 "write operation not allowed", goto error
);
2080 file
= fopen(tok
->u
.s
, "w");
2081 isl_token_free(tok
);
2083 isl_die(s
->ctx
, isl_error_unknown
,
2084 "could not open file for writing", goto error
);
2086 p
= isl_printer_to_file(s
->ctx
, file
);
2087 p
= isl_printer_set_output_format(p
, options
->format
);
2088 p
= obj
.type
->print(p
, obj
.v
);
2089 p
= isl_printer_end_line(p
);
2090 isl_printer_free(p
);
2095 obj
.type
= isl_obj_none
;
2100 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2102 struct isl_token
*tok
;
2103 struct isl_obj obj
= { isl_obj_none
, NULL
};
2105 tok
= isl_stream_next_token(s
);
2108 if (tok
->type
== ISL_TOKEN_STRING
) {
2110 str
= isl_str_alloc(s
->ctx
);
2113 str
->s
= strdup(tok
->u
.s
);
2114 isl_token_free(tok
);
2116 obj
.type
= isl_obj_str
;
2118 isl_stream_push_token(s
, tok
);
2121 isl_token_free(tok
);
2125 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2127 struct isl_token
*tok
;
2128 struct isl_obj obj
= { isl_obj_none
, NULL
};
2130 tok
= isl_stream_next_token(s
);
2133 if (tok
->type
== ISL_TOKEN_FALSE
|| tok
->type
== ISL_TOKEN_TRUE
) {
2134 int is_true
= tok
->type
== ISL_TOKEN_TRUE
;
2135 isl_token_free(tok
);
2136 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2137 obj
.type
= isl_obj_bool
;
2139 isl_stream_push_token(s
, tok
);
2142 isl_token_free(tok
);
2146 static __isl_give
char *read_ident(struct isl_stream
*s
)
2149 struct isl_token
*tok
, *tok2
;
2151 name
= isl_stream_read_ident_if_available(s
);
2155 tok
= isl_stream_next_token(s
);
2158 if (tok
->type
!= '$') {
2159 isl_stream_push_token(s
, tok
);
2162 tok2
= isl_stream_next_token(s
);
2163 if (!tok2
|| tok2
->type
!= ISL_TOKEN_VALUE
) {
2165 isl_stream_push_token(s
, tok2
);
2166 isl_stream_push_token(s
, tok
);
2170 name
= isl_int_get_str(tok2
->u
.v
);
2171 isl_token_free(tok
);
2172 isl_token_free(tok2
);
2177 static struct isl_obj
read_list(struct isl_stream
*s
,
2178 struct isl_hash_table
*table
, struct isl_obj obj
)
2180 struct isl_list
*list
;
2182 list
= isl_list_alloc(s
->ctx
, 2);
2186 list
->obj
[1] = read_obj(s
, table
);
2188 obj
.type
= isl_obj_list
;
2190 if (!list
->obj
[1].v
)
2193 while (isl_stream_eat_if_available(s
, ',')) {
2194 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2202 obj
.type
= isl_obj_none
;
2207 static struct isl_obj
read_obj(struct isl_stream
*s
,
2208 struct isl_hash_table
*table
)
2210 struct isl_obj obj
= { isl_obj_none
, NULL
};
2212 struct isc_un_op
*op
= NULL
;
2214 obj
= read_string_if_available(s
);
2217 obj
= read_bool_if_available(s
);
2220 if (isl_stream_eat_if_available(s
, '(')) {
2221 if (isl_stream_next_token_is(s
, ')')) {
2222 obj
.type
= isl_obj_list
;
2223 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2225 obj
= read_expr(s
, table
);
2226 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2227 obj
= read_list(s
, table
, obj
);
2229 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2232 op
= read_prefix_un_op_if_available(s
);
2234 return read_un_op_expr(s
, table
, op
);
2236 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2237 return check_assert(s
, table
);
2238 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2239 return read_from_file(s
);
2240 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2241 return write_to_file(s
, table
);
2242 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2243 return vertices(s
, table
);
2244 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2245 return any(s
, table
);
2246 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2247 return last(s
, table
);
2248 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2249 return schedule(s
, table
);
2250 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2251 return schedule_forest(s
, table
);
2252 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2253 return type_of(s
, table
);
2255 name
= read_ident(s
);
2257 obj
= stored_obj(s
->ctx
, table
, name
);
2259 obj
= isl_stream_read_obj(s
);
2264 if (isl_stream_eat_if_available(s
, '^'))
2265 obj
= power(s
, obj
);
2266 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2267 obj
= obj_at_index(s
, obj
);
2268 else if (is_subtype(obj
, isl_obj_union_map
) &&
2269 isl_stream_eat_if_available(s
, '(')) {
2270 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2271 obj
= apply(s
, obj
.v
, table
);
2272 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2273 isl_stream_eat_if_available(s
, '(')) {
2274 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2275 obj
= apply_fun(s
, obj
, table
);
2276 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2277 isl_stream_eat_if_available(s
, '(')) {
2278 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2279 obj
= apply_fun(s
, obj
, table
);
2285 obj
.type
= isl_obj_none
;
2290 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2291 struct isl_obj lhs
, struct isl_obj rhs
)
2295 for (i
= 0; ; ++i
) {
2298 if (bin_ops
[i
].op
!= like
->op
)
2300 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2302 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2308 for (i
= 0; ; ++i
) {
2309 if (!named_bin_ops
[i
].name
)
2311 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2313 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2315 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2318 return &named_bin_ops
[i
].op
;
2324 static int next_is_neg_int(struct isl_stream
*s
)
2326 struct isl_token
*tok
;
2329 tok
= isl_stream_next_token(s
);
2330 ret
= tok
&& tok
->type
== ISL_TOKEN_VALUE
&& isl_int_is_neg(tok
->u
.v
);
2331 isl_stream_push_token(s
, tok
);
2336 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2337 struct isl_obj lhs
, struct isl_obj rhs
)
2341 lhs
= convert(ctx
, lhs
, op
->lhs
);
2342 rhs
= convert(ctx
, rhs
, op
->rhs
);
2343 if (op
->res
!= isl_obj_bool
)
2344 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2346 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2349 obj
.v
= isl_bool_from_int(res
);
2356 static struct isl_obj
read_expr(struct isl_stream
*s
,
2357 struct isl_hash_table
*table
)
2359 struct isl_obj obj
= { isl_obj_none
, NULL
};
2360 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2362 obj
= read_obj(s
, table
);
2364 struct isc_bin_op
*op
= NULL
;
2366 op
= read_bin_op_if_available(s
, obj
);
2370 right_obj
= read_obj(s
, table
);
2372 op
= find_matching_bin_op(op
, obj
, right_obj
);
2375 isl_die(s
->ctx
, isl_error_invalid
,
2376 "no such binary operator defined on given operands",
2379 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2382 if (obj
.type
== isl_obj_int
&& next_is_neg_int(s
)) {
2383 right_obj
= read_obj(s
, table
);
2384 obj
.v
= isl_int_obj_add(obj
.v
, right_obj
.v
);
2389 free_obj(right_obj
);
2391 obj
.type
= isl_obj_none
;
2396 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2397 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2399 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2400 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2402 struct isl_obj obj
= { isl_obj_none
, NULL
};
2406 struct isc_bin_op
*op
= NULL
;
2411 if (isl_stream_is_empty(s
))
2414 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2415 return source_file(s
, table
, p
);
2417 assign
= is_assign(s
);
2419 lhs
= isl_stream_read_ident_if_available(s
);
2420 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2422 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2427 obj
= read_expr(s
, table
);
2428 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2429 fprintf(stderr
, "Interrupted\n");
2430 isl_ctx_reset_error(s
->ctx
);
2432 if (isl_stream_eat(s
, ';'))
2436 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2437 p
= obj
.type
->print(p
, obj
.v
);
2438 p
= isl_printer_end_line(p
);
2443 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2444 static int count
= 0;
2445 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2446 lhs
= strdup(buf
+ 1);
2448 p
= isl_printer_print_str(p
, buf
);
2449 p
= isl_printer_print_str(p
, " := ");
2450 p
= obj
.type
->print(p
, obj
.v
);
2451 p
= isl_printer_end_line(p
);
2453 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2458 isl_stream_flush_tokens(s
);
2459 isl_stream_skip_line(s
);
2465 int free_cb(void **entry
, void *user
)
2467 struct isl_named_obj
*named
= *entry
;
2469 free_obj(named
->obj
);
2476 static void register_named_ops(struct isl_stream
*s
)
2480 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2481 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2482 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2485 for (i
= 0; ; ++i
) {
2486 if (!named_un_ops
[i
].name
)
2488 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2489 named_un_ops
[i
].name
);
2490 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2493 for (i
= 0; ; ++i
) {
2494 if (!named_bin_ops
[i
].name
)
2496 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2497 named_bin_ops
[i
].name
);
2498 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2502 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2503 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2505 struct isl_token
*tok
;
2506 struct isl_stream
*s_file
;
2509 tok
= isl_stream_next_token(s
);
2510 if (!tok
|| tok
->type
!= ISL_TOKEN_STRING
) {
2511 isl_stream_error(s
, tok
, "expecting filename");
2512 isl_token_free(tok
);
2516 file
= fopen(tok
->u
.s
, "r");
2517 isl_token_free(tok
);
2518 isl_assert(s
->ctx
, file
, return p
);
2520 s_file
= isl_stream_new_file(s
->ctx
, file
);
2526 register_named_ops(s_file
);
2528 while (!s_file
->eof
)
2529 p
= read_line(s_file
, table
, p
, 0);
2531 isl_stream_free(s_file
);
2534 isl_stream_eat(s
, ';');
2539 int main(int argc
, char **argv
)
2541 struct isl_ctx
*ctx
;
2542 struct isl_stream
*s
;
2543 struct isl_hash_table
*table
;
2544 struct iscc_options
*options
;
2546 int tty
= isatty(0);
2548 options
= iscc_options_new_with_defaults();
2550 argc
= iscc_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
2552 ctx
= isl_ctx_alloc_with_options(iscc_options_arg
, options
);
2553 s
= isl_stream_new_file(ctx
, stdin
);
2555 table
= isl_hash_table_alloc(ctx
, 10);
2557 p
= isl_printer_to_file(ctx
, stdout
);
2558 p
= isl_printer_set_output_format(p
, options
->format
);
2561 register_named_ops(s
);
2563 install_signal_handler(ctx
);
2565 while (p
&& !s
->eof
) {
2566 isl_ctx_resume(ctx
);
2567 p
= read_line(s
, table
, p
, tty
);
2570 remove_signal_handler(ctx
);
2572 isl_printer_free(p
);
2573 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2574 isl_hash_table_free(ctx
, table
);