8 #include <isl/stream.h>
11 #include <isl/vertices.h>
14 #include <isl/schedule.h>
15 #include <isl/ast_build.h>
16 #include <isl_obj_list.h>
17 #include <isl_obj_str.h>
18 #include <barvinok/isl.h>
19 #include <barvinok/options.h>
20 #include "lattice_width.h"
27 static isl_ctx
*main_ctx
;
29 static void handler(int signum
)
31 if (isl_ctx_aborted(main_ctx
))
33 isl_ctx_abort(main_ctx
);
36 static struct sigaction sa_old
;
38 static void install_signal_handler(isl_ctx
*ctx
)
44 memset(&sa
, 0, sizeof(struct sigaction
));
45 sa
.sa_handler
= &handler
;
46 sa
.sa_flags
= SA_RESTART
;
47 sigaction(SIGINT
, &sa
, &sa_old
);
50 static void remove_signal_handler(isl_ctx
*ctx
)
52 sigaction(SIGINT
, &sa_old
, NULL
);
57 static void install_signal_handler(isl_ctx
*ctx
)
61 static void remove_signal_handler(isl_ctx
*ctx
)
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_CODEGEN
, ISCC_USING
,
86 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
88 static const char *op_name
[ISCC_N_OP
] = {
89 [ISCC_ASSERT
] = "assert",
91 [ISCC_WRITE
] = "write",
92 [ISCC_PRINT
] = "print",
93 [ISCC_SOURCE
] = "source",
94 [ISCC_VERTICES
] = "vertices",
97 [ISCC_BEFORE
] = "before",
98 [ISCC_UNDER
] = "under",
99 [ISCC_SCHEDULE
] = "schedule",
100 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
101 [ISCC_MINIMIZING
] = "minimizing",
102 [ISCC_RESPECTING
] = "respecting",
103 [ISCC_CODEGEN
] = "codegen",
104 [ISCC_USING
] = "using",
105 [ISCC_TYPEOF
] = "typeof"
107 static enum isl_token_type iscc_op
[ISCC_N_OP
];
109 struct isl_arg_choice iscc_format
[] = {
110 {"isl", ISL_FORMAT_ISL
},
111 {"omega", ISL_FORMAT_OMEGA
},
112 {"polylib", ISL_FORMAT_POLYLIB
},
113 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
114 {"latex", ISL_FORMAT_LATEX
},
119 struct iscc_options
{
120 struct barvinok_options
*barvinok
;
121 struct pet_options
*pet
;
126 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
127 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
130 ISL_ARG_CHILD(struct iscc_options
, pet
, "pet", &pet_options_args
, "pet options")
132 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
133 iscc_format
, ISL_FORMAT_ISL
, "output format")
134 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
135 "allow read and write operations")
138 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
139 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
141 static void *isl_obj_bool_copy(void *v
)
146 static void isl_obj_bool_free(void *v
)
150 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
153 if (v
== &isl_bool_true
)
154 return isl_printer_print_str(p
, "True");
155 else if (v
== &isl_bool_false
)
156 return isl_printer_print_str(p
, "False");
158 return isl_printer_print_str(p
, "Error");
161 static void *isl_obj_bool_add(void *v1
, void *v2
)
166 struct isl_obj_vtable isl_obj_bool_vtable
= {
172 #define isl_obj_bool (&isl_obj_bool_vtable)
174 int *isl_bool_from_int(int res
)
176 return res
< 0 ? &isl_bool_error
: res
? &isl_bool_true
: &isl_bool_false
;
179 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
180 __isl_take isl_union_map
*map2
)
182 return isl_union_map_is_subset(map2
, map1
);
184 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
185 __isl_take isl_union_set
*set2
)
187 return isl_union_set_is_subset(set2
, set1
);
190 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
191 __isl_take isl_union_map
*map2
)
193 return isl_union_map_is_strict_subset(map2
, map1
);
195 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
196 __isl_take isl_union_set
*set2
)
198 return isl_union_set_is_strict_subset(set2
, set1
);
201 extern struct isl_obj_vtable isl_obj_list_vtable
;
202 #define isl_obj_list (&isl_obj_list_vtable)
204 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
205 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
207 enum isl_token_type op
;
213 isc_bin_test_fn test
;
216 struct isc_named_bin_op
{
218 struct isc_bin_op op
;
222 isl_union_pw_qpolynomial
*upwqp
;
223 isl_union_pw_qpolynomial
*res
;
226 static int eval_at(__isl_take isl_point
*pnt
, void *user
)
228 struct iscc_at
*at
= (struct iscc_at
*) user
;
233 set
= isl_set_from_point(isl_point_copy(pnt
));
234 v
= isl_union_pw_qpolynomial_eval(
235 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
236 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
238 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
239 isl_union_pw_qpolynomial_from_pw_qpolynomial(
240 isl_pw_qpolynomial_alloc(set
, qp
)));
245 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
246 __isl_take isl_union_pw_qpolynomial
*upwqp
,
247 __isl_take isl_union_set
*uset
)
252 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
254 isl_union_set_foreach_point(uset
, eval_at
, &at
);
256 isl_union_pw_qpolynomial_free(upwqp
);
257 isl_union_set_free(uset
);
262 struct iscc_fold_at
{
263 isl_union_pw_qpolynomial_fold
*upwf
;
264 isl_union_pw_qpolynomial
*res
;
267 static int eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
269 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
274 set
= isl_set_from_point(isl_point_copy(pnt
));
275 v
= isl_union_pw_qpolynomial_fold_eval(
276 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
277 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
279 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
280 isl_union_pw_qpolynomial_from_pw_qpolynomial(
281 isl_pw_qpolynomial_alloc(set
, qp
)));
286 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
287 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
288 __isl_take isl_union_set
*uset
)
290 struct iscc_fold_at at
;
293 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
295 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
297 isl_union_pw_qpolynomial_fold_free(upwf
);
298 isl_union_set_free(uset
);
303 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
304 __isl_take isl_union_pw_qpolynomial
*upwqp
,
305 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
307 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
311 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
312 __isl_take isl_union_map
*umap
,
313 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
316 struct isl_list
*list
;
319 ctx
= isl_union_map_get_ctx(umap
);
320 list
= isl_list_alloc(ctx
, 2);
324 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
325 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
327 list
->obj
[1].type
= isl_obj_bool
;
328 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
329 if (tight
< 0 || !list
->obj
[0].v
)
334 isl_union_map_free(umap
);
335 isl_union_pw_qpolynomial_fold_free(upwf
);
341 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
342 __isl_take isl_union_set
*uset
,
343 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
346 struct isl_list
*list
;
349 ctx
= isl_union_set_get_ctx(uset
);
350 list
= isl_list_alloc(ctx
, 2);
354 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
355 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
357 list
->obj
[1].type
= isl_obj_bool
;
358 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
359 if (tight
< 0 || !list
->obj
[0].v
)
364 isl_union_set_free(uset
);
365 isl_union_pw_qpolynomial_fold_free(upwf
);
371 static __isl_give isl_union_pw_qpolynomial
*isl_val_mul_union_pw_qpolynomial(
372 __isl_take isl_val
*v
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
374 return isl_union_pw_qpolynomial_scale_val(upwqp
, v
);
377 static __isl_give isl_union_pw_qpolynomial_fold
*
378 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val
*v
,
379 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
381 return isl_union_pw_qpolynomial_fold_scale_val(upwf
, v
);
384 /* Are the two strings "str1" and "str2" equal to each other?
386 static int str_eq(__isl_keep isl_str
*str1
, __isl_keep isl_str
*str2
)
391 return !strcmp(str1
->s
, str2
->s
);
394 struct isc_bin_op bin_ops
[] = {
395 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
396 (isc_bin_op_fn
) &isl_val_add
},
397 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
398 (isc_bin_op_fn
) &isl_val_sub
},
399 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
400 (isc_bin_op_fn
) &isl_val_mul
},
401 { '+', isl_obj_pw_multi_aff
, isl_obj_pw_multi_aff
,
402 isl_obj_pw_multi_aff
,
403 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
404 { '+', isl_obj_union_set
, isl_obj_union_set
,
406 (isc_bin_op_fn
) &isl_union_set_union
},
407 { '+', isl_obj_union_map
, isl_obj_union_map
,
409 (isc_bin_op_fn
) &isl_union_map_union
},
410 { '-', isl_obj_union_set
, isl_obj_union_set
,
412 (isc_bin_op_fn
) &isl_union_set_subtract
},
413 { '-', isl_obj_union_map
, isl_obj_union_map
,
415 (isc_bin_op_fn
) &isl_union_map_subtract
},
416 { '*', isl_obj_union_set
, isl_obj_union_set
,
418 (isc_bin_op_fn
) &isl_union_set_intersect
},
419 { '*', isl_obj_union_map
, isl_obj_union_map
,
421 (isc_bin_op_fn
) &isl_union_map_intersect
},
422 { '*', isl_obj_union_map
, isl_obj_union_set
,
424 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
425 { '.', isl_obj_union_map
, isl_obj_union_map
,
427 (isc_bin_op_fn
) &isl_union_map_apply_range
},
428 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
429 isl_obj_union_pw_qpolynomial
,
430 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
431 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
433 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
434 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
436 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
437 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
438 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
439 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
440 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
441 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
443 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
444 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
446 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
447 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
449 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
450 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
452 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
453 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
455 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
456 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
458 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
459 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
462 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
463 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
466 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
467 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
469 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
470 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
472 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
473 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
475 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
476 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
478 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
479 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
481 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
482 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
484 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
485 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
487 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
488 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
490 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
491 { '.', isl_obj_union_pw_qpolynomial_fold
,
492 isl_obj_union_pw_qpolynomial_fold
,
493 isl_obj_union_pw_qpolynomial_fold
,
494 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
495 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
496 isl_obj_union_pw_qpolynomial
,
497 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
498 { '+', isl_obj_union_pw_qpolynomial
,
499 isl_obj_union_pw_qpolynomial_fold
,
500 isl_obj_union_pw_qpolynomial_fold
,
501 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
502 { '+', isl_obj_union_pw_qpolynomial_fold
,
503 isl_obj_union_pw_qpolynomial
,
504 isl_obj_union_pw_qpolynomial_fold
,
505 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
506 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
507 isl_obj_union_pw_qpolynomial
,
508 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
509 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
510 isl_obj_union_pw_qpolynomial
,
511 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
512 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
513 isl_obj_union_pw_qpolynomial
,
514 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
515 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
516 isl_obj_union_pw_qpolynomial_fold
,
517 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
518 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
519 isl_obj_union_pw_qpolynomial_fold
,
520 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
521 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
522 isl_obj_union_pw_qpolynomial
,
523 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
524 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
525 isl_obj_union_pw_qpolynomial
,
526 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
527 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
528 isl_obj_union_pw_qpolynomial_fold
,
529 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
530 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
531 isl_obj_union_pw_qpolynomial
,
532 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
533 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
534 isl_obj_union_pw_qpolynomial
,
535 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
536 { '%', isl_obj_union_set
, isl_obj_union_set
,
538 (isc_bin_op_fn
) &isl_union_set_gist
},
539 { '%', isl_obj_union_map
, isl_obj_union_map
,
541 (isc_bin_op_fn
) &isl_union_map_gist
},
542 { '%', isl_obj_union_map
, isl_obj_union_set
,
544 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
545 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
546 isl_obj_union_pw_qpolynomial
,
547 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
548 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
549 isl_obj_union_pw_qpolynomial_fold
,
550 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
551 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
552 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
553 { .test
= (isc_bin_test_fn
)
554 &isl_union_pw_qpolynomial_plain_is_equal
} },
555 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
556 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
557 { .test
= (isc_bin_test_fn
)
558 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
559 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
560 (isc_bin_op_fn
) &isl_str_concat
},
561 { '=', isl_obj_str
, isl_obj_str
, isl_obj_bool
,
562 { .test
= (isc_bin_test_fn
) &str_eq
} },
566 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
567 __isl_take isl_union_map
*umap2
)
569 return isl_union_map_apply_range(umap2
, umap1
);
572 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
573 __isl_take isl_union_pw_qpolynomial
*upwqp
,
574 __isl_take isl_union_map
*umap
)
576 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
579 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
580 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
581 __isl_take isl_union_map
*umap
)
583 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
586 struct isc_named_bin_op named_bin_ops
[] = {
587 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
589 (isc_bin_op_fn
) &map_after_map
} },
590 { "after", { -1, isl_obj_union_pw_qpolynomial
,
591 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
592 (isc_bin_op_fn
) &qpolynomial_after_map
} },
593 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
594 isl_obj_union_map
, isl_obj_list
,
595 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
596 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
598 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
599 { "before", { -1, isl_obj_union_map
,
600 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
601 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
602 { "before", { -1, isl_obj_union_map
,
603 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
604 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
605 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
607 (isc_bin_op_fn
) &isl_union_set_product
} },
608 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
610 (isc_bin_op_fn
) &isl_union_map_product
} },
614 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
616 return isl_set_from_basic_set(isl_union_set_sample(uset
));
619 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
621 return isl_map_from_basic_map(isl_union_map_sample(umap
));
624 static __isl_give
struct isl_list
*union_map_power(
625 __isl_take isl_union_map
*umap
)
628 struct isl_list
*list
;
631 ctx
= isl_union_map_get_ctx(umap
);
632 list
= isl_list_alloc(ctx
, 2);
636 list
->obj
[0].type
= isl_obj_union_map
;
637 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
638 list
->obj
[1].type
= isl_obj_bool
;
639 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
640 if (exact
< 0 || !list
->obj
[0].v
)
645 isl_union_map_free(umap
);
651 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
652 __isl_take isl_union_pw_qpolynomial
*upwqp
)
655 struct isl_list
*list
;
658 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
659 list
= isl_list_alloc(ctx
, 2);
663 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
664 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
665 isl_fold_max
, &tight
);
666 list
->obj
[1].type
= isl_obj_bool
;
667 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
668 if (tight
< 0 || !list
->obj
[0].v
)
673 isl_union_pw_qpolynomial_free(upwqp
);
680 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
683 struct isl_list
*list
;
684 struct pet_scop
*scop
;
685 isl_union_map
*sched
, *may_reads
, *must_writes
, *may_writes
;
686 isl_union_set
*domain
;
687 struct iscc_options
*options
;
693 options
= isl_ctx_peek_iscc_options(ctx
);
694 if (!options
|| !options
->io
) {
696 isl_die(ctx
, isl_error_invalid
,
697 "parse_file operation not allowed", return NULL
);
700 list
= isl_list_alloc(ctx
, 5);
704 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
705 domain
= pet_scop_collect_domains(scop
);
706 sched
= scop
? isl_schedule_get_map(scop
->schedule
) : NULL
;
707 may_reads
= pet_scop_collect_may_reads(scop
);
708 may_writes
= pet_scop_collect_may_writes(scop
);
709 must_writes
= pet_scop_collect_must_writes(scop
);
712 list
->obj
[0].type
= isl_obj_union_set
;
713 list
->obj
[0].v
= domain
;
714 list
->obj
[1].type
= isl_obj_union_map
;
715 list
->obj
[1].v
= must_writes
;
716 list
->obj
[2].type
= isl_obj_union_map
;
717 list
->obj
[2].v
= may_writes
;
718 list
->obj
[3].type
= isl_obj_union_map
;
719 list
->obj
[3].v
= may_reads
;
720 list
->obj
[4].type
= isl_obj_union_map
;
721 list
->obj
[4].v
= sched
;
723 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
724 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
736 static int add_point(__isl_take isl_point
*pnt
, void *user
)
738 isl_union_set
**scan
= (isl_union_set
**) user
;
740 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
745 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
749 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
751 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
752 isl_union_set_free(scan
);
756 isl_union_set_free(uset
);
760 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
762 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
765 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
766 __isl_take isl_union_pw_qpolynomial
*upwqp
)
768 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
771 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
772 __isl_take isl_union_pw_qpolynomial
*upwqp
)
774 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
777 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
778 __isl_take isl_union_pw_qpolynomial
*upwqp
)
780 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
783 typedef void *(*isc_un_op_fn
)(void *arg
);
785 enum isl_token_type op
;
790 struct isc_named_un_op
{
794 struct isc_named_un_op named_un_ops
[] = {
795 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
796 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
797 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
798 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
799 {"card", { -1, isl_obj_union_set
,
800 isl_obj_union_pw_qpolynomial
,
801 (isc_un_op_fn
) &isl_union_set_card
} },
802 {"card", { -1, isl_obj_union_map
,
803 isl_obj_union_pw_qpolynomial
,
804 (isc_un_op_fn
) &isl_union_map_card
} },
805 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
806 (isc_un_op_fn
) &isl_union_set_coalesce
} },
807 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
808 (isc_un_op_fn
) &isl_union_map_coalesce
} },
809 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
810 isl_obj_union_pw_qpolynomial
,
811 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
812 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
813 isl_obj_union_pw_qpolynomial_fold
,
814 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
815 {"coefficients", { -1, isl_obj_union_set
,
817 (isc_un_op_fn
) &isl_union_set_coefficients
} },
818 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
819 (isc_un_op_fn
) &isl_union_set_solutions
} },
820 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
821 (isc_un_op_fn
) &isl_union_map_deltas
} },
822 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
823 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
824 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
825 (isc_un_op_fn
) &isl_union_map_domain
} },
826 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
827 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
828 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
830 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
831 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
832 (isc_un_op_fn
) &isl_union_map_domain
} },
833 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
835 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
836 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
838 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
839 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
840 (isc_un_op_fn
) &isl_union_map_domain_map
} },
841 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
842 (isc_un_op_fn
) &isl_union_map_range
} },
843 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
844 (isc_un_op_fn
) &isl_union_map_range
} },
845 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
846 (isc_un_op_fn
) &isl_union_map_range_map
} },
847 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
848 (isc_un_op_fn
) &isl_union_set_identity
} },
849 {"lattice_width", { -1, isl_obj_union_set
,
850 isl_obj_union_pw_qpolynomial
,
851 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
852 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
853 (isc_un_op_fn
) &isl_union_map_lexmin
} },
854 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
855 (isc_un_op_fn
) &isl_union_map_lexmax
} },
856 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
857 (isc_un_op_fn
) &isl_union_set_lexmin
} },
858 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
859 (isc_un_op_fn
) &isl_union_set_lexmax
} },
860 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
861 (isc_un_op_fn
) &isl_union_set_lift
} },
862 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
863 (isc_un_op_fn
) &isl_union_map_params
} },
864 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
865 (isc_un_op_fn
) &isl_union_set_params
} },
866 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
867 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
868 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
869 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
870 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
871 isl_obj_union_pw_qpolynomial
,
872 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
873 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
874 isl_obj_union_pw_qpolynomial
,
875 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
876 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
877 isl_obj_union_pw_qpolynomial
,
878 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
880 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
881 (isc_un_op_fn
) &parse
} },
883 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
884 (isc_un_op_fn
) &union_map_power
} },
885 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
886 (isc_un_op_fn
) &union_set_sample
} },
887 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
888 (isc_un_op_fn
) &union_map_sample
} },
889 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
890 (isc_un_op_fn
) &union_set_scan
} },
891 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
892 (isc_un_op_fn
) &union_map_scan
} },
893 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
894 isl_obj_union_pw_qpolynomial
,
895 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
896 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
897 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
898 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
899 (isc_un_op_fn
) &isl_union_set_unwrap
} },
900 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
901 (isc_un_op_fn
) &isl_union_map_wrap
} },
902 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
903 (isc_un_op_fn
) &isl_union_map_zip
} },
907 struct isl_named_obj
{
912 static void free_obj(struct isl_obj obj
)
914 obj
.type
->free(obj
.v
);
917 static int same_name(const void *entry
, const void *val
)
919 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
921 return !strcmp(named
->name
, val
);
924 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
925 char *name
, struct isl_obj obj
)
927 struct isl_hash_table_entry
*entry
;
929 struct isl_named_obj
*named
;
931 name_hash
= isl_hash_string(isl_hash_init(), name
);
932 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
937 free_obj(named
->obj
);
940 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
955 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
956 struct isl_hash_table
*table
, char *name
)
958 struct isl_obj obj
= { isl_obj_none
, NULL
};
959 struct isl_hash_table_entry
*entry
;
962 name_hash
= isl_hash_string(isl_hash_init(), name
);
963 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
965 struct isl_named_obj
*named
;
968 } else if (isdigit(name
[0]))
969 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
971 fprintf(stderr
, "unknown identifier '%s'\n", name
);
974 obj
.v
= obj
.type
->copy(obj
.v
);
978 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
980 if (obj
.type
== super
)
982 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
984 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
986 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
987 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
988 int is_set
= isl_space_is_set(space
);
989 isl_space_free(space
);
992 if (obj
.type
== isl_obj_pw_qpolynomial
&&
993 super
== isl_obj_union_pw_qpolynomial
)
995 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
996 super
== isl_obj_union_pw_qpolynomial_fold
)
998 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1000 if (obj
.type
== isl_obj_list
) {
1001 struct isl_list
*list
= obj
.v
;
1002 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1003 return is_subtype(list
->obj
[0], super
);
1005 if (super
== isl_obj_str
)
1010 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1012 struct isl_list
*list
= obj
.v
;
1015 obj
.v
= obj
.type
->copy(obj
.v
);
1017 isl_list_free(list
);
1022 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1025 if (obj
.type
== type
)
1027 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1028 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1029 obj
.type
= isl_obj_union_set
;
1030 obj
.v
= isl_union_set_from_set(set
);
1033 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1034 obj
.type
= isl_obj_union_map
;
1035 obj
.v
= isl_union_map_from_map(obj
.v
);
1038 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1039 obj
.type
= isl_obj_union_set
;
1040 obj
.v
= isl_union_set_from_set(obj
.v
);
1043 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1044 type
== isl_obj_union_pw_qpolynomial
) {
1045 obj
.type
= isl_obj_union_pw_qpolynomial
;
1046 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1049 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1050 type
== isl_obj_union_pw_qpolynomial_fold
) {
1051 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1052 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1055 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1056 if (type
== isl_obj_union_map
) {
1057 obj
.type
= isl_obj_union_map
;
1060 if (type
== isl_obj_union_pw_qpolynomial
) {
1061 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1062 isl_union_set_free(obj
.v
);
1063 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1064 obj
.type
= isl_obj_union_pw_qpolynomial
;
1067 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1068 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1069 isl_union_set_free(obj
.v
);
1070 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1072 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1076 if (obj
.type
== isl_obj_list
) {
1077 struct isl_list
*list
= obj
.v
;
1078 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1079 return convert(ctx
, obj_at(obj
, 0), type
);
1081 if (type
== isl_obj_str
) {
1086 p
= isl_printer_to_str(ctx
);
1089 p
= obj
.type
->print(p
, obj
.v
);
1090 s
= isl_printer_get_str(p
);
1091 isl_printer_free(p
);
1093 str
= isl_str_from_string(ctx
, s
);
1098 obj
.type
= isl_obj_str
;
1104 obj
.type
= isl_obj_none
;
1109 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1113 struct isl_token
*tok
;
1115 tok
= isl_stream_next_token(s
);
1119 for (i
= 0; ; ++i
) {
1122 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1124 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1127 isl_token_free(tok
);
1131 for (i
= 0; ; ++i
) {
1132 if (!named_bin_ops
[i
].name
)
1134 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1136 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1139 isl_token_free(tok
);
1140 return &named_bin_ops
[i
].op
;
1143 isl_stream_push_token(s
, tok
);
1148 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1151 struct isl_token
*tok
;
1153 tok
= isl_stream_next_token(s
);
1157 for (i
= 0; ; ++i
) {
1158 if (!named_un_ops
[i
].name
)
1160 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1163 isl_token_free(tok
);
1164 return &named_un_ops
[i
].op
;
1167 isl_stream_push_token(s
, tok
);
1172 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1177 for (i
= 0; ; ++i
) {
1178 if (!named_un_ops
[i
].name
)
1180 if (named_un_ops
[i
].op
.op
!= like
->op
)
1182 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1185 return &named_un_ops
[i
].op
;
1191 static int is_assign(struct isl_stream
*s
)
1193 struct isl_token
*tok
;
1194 struct isl_token
*tok2
;
1197 tok
= isl_stream_next_token(s
);
1200 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1201 isl_stream_push_token(s
, tok
);
1205 tok2
= isl_stream_next_token(s
);
1207 isl_stream_push_token(s
, tok
);
1210 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1211 isl_stream_push_token(s
, tok2
);
1212 isl_stream_push_token(s
, tok
);
1217 static struct isl_obj
read_obj(struct isl_stream
*s
,
1218 struct isl_hash_table
*table
);
1219 static struct isl_obj
read_expr(struct isl_stream
*s
,
1220 struct isl_hash_table
*table
);
1222 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1223 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1226 struct isl_obj obj
= { isl_obj_none
, NULL
};
1228 obj
= read_obj(s
, table
);
1232 op
= find_matching_un_op(op
, obj
);
1234 ctx
= isl_stream_get_ctx(s
);
1236 isl_die(ctx
, isl_error_invalid
,
1237 "no such unary operator defined on given operand",
1240 obj
= convert(ctx
, obj
, op
->arg
);
1241 obj
.v
= op
->fn(obj
.v
);
1247 obj
.type
= isl_obj_none
;
1252 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1254 struct isl_list
*list
;
1257 if (obj
.type
!= isl_obj_union_map
)
1258 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1259 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1260 list
= isl_list_alloc(ctx
, 2);
1264 list
->obj
[0].type
= isl_obj_union_map
;
1265 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1266 list
->obj
[1].type
= isl_obj_bool
;
1267 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1269 obj
.type
= isl_obj_list
;
1270 if (exact
< 0 || !list
->obj
[0].v
)
1276 obj
.type
= isl_obj_none
;
1281 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1283 struct isl_list
*list
= obj
.v
;
1284 struct isl_token
*tok
;
1289 tok
= isl_stream_next_token(s
);
1290 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1291 isl_stream_error(s
, tok
, "expecting index");
1293 isl_stream_push_token(s
, tok
);
1296 ctx
= isl_stream_get_ctx(s
);
1297 v
= isl_token_get_val(ctx
, tok
);
1298 i
= isl_val_get_num_si(v
);
1300 isl_token_free(tok
);
1301 isl_assert(ctx
, i
< list
->n
, goto error
);
1302 if (isl_stream_eat(s
, ']'))
1305 return obj_at(obj
, i
);
1308 obj
.type
= isl_obj_none
;
1313 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1314 struct isl_hash_table
*table
)
1319 obj
= read_expr(s
, table
);
1320 ctx
= isl_stream_get_ctx(s
);
1321 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1322 is_subtype(obj
, isl_obj_union_map
), goto error
);
1324 if (obj
.type
== isl_obj_list
) {
1325 struct isl_list
*list
= obj
.v
;
1326 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1327 obj
= obj_at(obj
, 0);
1329 if (obj
.type
== isl_obj_set
)
1330 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1331 else if (obj
.type
== isl_obj_map
)
1332 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1333 if (obj
.type
== isl_obj_union_set
) {
1334 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1336 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1340 if (isl_stream_eat(s
, ')'))
1345 isl_union_map_free(umap
);
1348 obj
.type
= isl_obj_none
;
1353 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1354 __isl_take isl_union_set
*uset
)
1356 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1357 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1359 obj
.type
= isl_obj_list
;
1360 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1365 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1366 __isl_take isl_union_map
*umap
)
1368 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1369 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1371 obj
.type
= isl_obj_list
;
1372 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1377 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1378 struct isl_obj obj
, struct isl_hash_table
*table
)
1383 arg
= read_expr(s
, table
);
1384 ctx
= isl_stream_get_ctx(s
);
1385 if (!is_subtype(arg
, isl_obj_union_map
) &&
1386 !is_subtype(arg
, isl_obj_union_set
))
1387 isl_die(ctx
, isl_error_invalid
,
1388 "expecting set of map argument", goto error
);
1390 if (arg
.type
== isl_obj_list
) {
1391 struct isl_list
*list
= arg
.v
;
1392 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1393 arg
= obj_at(arg
, 0);
1395 if (arg
.type
== isl_obj_set
)
1396 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1397 else if (arg
.type
== isl_obj_map
)
1398 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1399 if (arg
.type
== isl_obj_union_set
)
1400 obj
= apply_fun_set(obj
, arg
.v
);
1402 obj
= apply_fun_map(obj
, arg
.v
);
1406 if (isl_stream_eat(s
, ')'))
1414 obj
.type
= isl_obj_none
;
1419 struct add_vertex_data
{
1420 struct isl_list
*list
;
1424 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1426 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1430 ma
= isl_vertex_get_expr(vertex
);
1431 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1433 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1434 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1437 isl_vertex_free(vertex
);
1442 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1445 isl_basic_set
*hull
;
1446 isl_vertices
*vertices
= NULL
;
1447 struct isl_list
*list
= NULL
;
1449 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1451 set
= isl_set_remove_divs(set
);
1452 hull
= isl_set_convex_hull(set
);
1453 vertices
= isl_basic_set_compute_vertices(hull
);
1454 isl_basic_set_free(hull
);
1458 ctx
= isl_vertices_get_ctx(vertices
);
1459 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1464 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1466 data
->list
= isl_list_concat(list
, data
->list
);
1468 isl_vertices_free(vertices
);
1473 isl_vertices_free(vertices
);
1477 static struct isl_obj
vertices(struct isl_stream
*s
,
1478 struct isl_hash_table
*table
)
1482 struct isl_list
*list
= NULL
;
1483 isl_union_set
*uset
= NULL
;
1484 struct add_vertex_data data
= { NULL
};
1486 obj
= read_expr(s
, table
);
1487 ctx
= isl_stream_get_ctx(s
);
1488 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1489 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1493 list
= isl_list_alloc(ctx
, 0);
1499 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1502 isl_union_set_free(uset
);
1504 obj
.type
= isl_obj_list
;
1509 isl_union_set_free(uset
);
1510 isl_list_free(data
.list
);
1512 obj
.type
= isl_obj_none
;
1517 static struct isl_obj
type_of(struct isl_stream
*s
,
1518 struct isl_hash_table
*table
)
1522 const char *type
= "unknown";
1524 obj
= read_expr(s
, table
);
1526 if (obj
.type
== isl_obj_map
||
1527 obj
.type
== isl_obj_union_map
)
1529 if (obj
.type
== isl_obj_set
||
1530 obj
.type
== isl_obj_union_set
)
1532 if (obj
.type
== isl_obj_pw_multi_aff
)
1533 type
= "piecewise multi-quasiaffine expression";
1534 if (obj
.type
== isl_obj_pw_qpolynomial
||
1535 obj
.type
== isl_obj_union_pw_qpolynomial
)
1536 type
= "piecewise quasipolynomial";
1537 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1538 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1539 type
= "piecewise quasipolynomial fold";
1540 if (obj
.type
== isl_obj_list
)
1542 if (obj
.type
== isl_obj_bool
)
1544 if (obj
.type
== isl_obj_str
)
1546 if (obj
.type
== isl_obj_val
)
1550 obj
.type
= isl_obj_str
;
1551 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1556 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1557 struct isl_hash_table
*table
)
1562 obj
= read_obj(s
, table
);
1563 ctx
= isl_stream_get_ctx(s
);
1564 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1565 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1572 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1573 struct isl_hash_table
*table
)
1578 obj
= read_obj(s
, table
);
1579 ctx
= isl_stream_get_ctx(s
);
1580 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1581 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1588 static struct isl_obj
last_any(struct isl_stream
*s
,
1589 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1590 __isl_take isl_union_map
*may_source
)
1592 struct isl_obj obj
= { isl_obj_none
, NULL
};
1593 isl_union_map
*sink
= NULL
;
1594 isl_union_map
*schedule
= NULL
;
1595 isl_union_map
*may_dep
;
1596 isl_union_map
*must_dep
;
1598 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1601 sink
= read_map(s
, table
);
1605 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1608 schedule
= read_map(s
, table
);
1612 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1613 schedule
, &must_dep
, &may_dep
,
1617 obj
.type
= isl_obj_union_map
;
1618 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1622 isl_union_map_free(may_source
);
1623 isl_union_map_free(must_source
);
1624 isl_union_map_free(sink
);
1625 isl_union_map_free(schedule
);
1627 obj
.type
= isl_obj_none
;
1632 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1634 struct isl_obj obj
= { isl_obj_none
, NULL
};
1635 isl_union_map
*must_source
= NULL
;
1636 isl_union_map
*may_source
= NULL
;
1637 isl_union_map
*sink
= NULL
;
1638 isl_union_map
*schedule
= NULL
;
1639 isl_union_map
*may_dep
;
1641 may_source
= read_map(s
, table
);
1645 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1646 must_source
= read_map(s
, table
);
1649 return last_any(s
, table
, must_source
, may_source
);
1652 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1655 sink
= read_map(s
, table
);
1659 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1662 schedule
= read_map(s
, table
);
1666 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1667 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1668 schedule
, NULL
, &may_dep
,
1672 obj
.type
= isl_obj_union_map
;
1677 isl_union_map_free(may_source
);
1678 isl_union_map_free(must_source
);
1679 isl_union_map_free(sink
);
1680 isl_union_map_free(schedule
);
1682 obj
.type
= isl_obj_none
;
1687 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1689 struct isl_obj obj
= { isl_obj_none
, NULL
};
1690 struct isl_list
*list
= NULL
;
1691 isl_union_map
*must_source
= NULL
;
1692 isl_union_map
*may_source
= NULL
;
1693 isl_union_map
*sink
= NULL
;
1694 isl_union_map
*schedule
= NULL
;
1695 isl_union_map
*must_dep
;
1696 isl_union_map
*must_no_source
;
1698 must_source
= read_map(s
, table
);
1702 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1703 may_source
= read_map(s
, table
);
1706 return last_any(s
, table
, must_source
, may_source
);
1709 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1713 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1716 sink
= read_map(s
, table
);
1720 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1723 schedule
= read_map(s
, table
);
1727 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1728 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1729 schedule
, &must_dep
, NULL
,
1730 &must_no_source
, NULL
) < 0) {
1731 isl_list_free(list
);
1735 list
->obj
[0].type
= isl_obj_union_map
;
1736 list
->obj
[0].v
= must_dep
;
1737 list
->obj
[1].type
= isl_obj_union_map
;
1738 list
->obj
[1].v
= must_no_source
;
1741 obj
.type
= isl_obj_list
;
1745 isl_list_free(list
);
1746 isl_union_map_free(may_source
);
1747 isl_union_map_free(must_source
);
1748 isl_union_map_free(sink
);
1749 isl_union_map_free(schedule
);
1751 obj
.type
= isl_obj_none
;
1756 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1757 struct isl_hash_table
*table
)
1759 isl_union_set
*domain
;
1760 isl_union_map
*validity
;
1761 isl_union_map
*proximity
;
1763 domain
= read_set(s
, table
);
1767 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1768 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1771 isl_union_map
*umap
;
1772 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1773 umap
= read_map(s
, table
);
1774 validity
= isl_union_map_union(validity
, umap
);
1775 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1776 umap
= read_map(s
, table
);
1777 proximity
= isl_union_map_union(proximity
, umap
);
1782 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1785 static struct isl_obj
schedule(struct isl_stream
*s
,
1786 struct isl_hash_table
*table
)
1788 struct isl_obj obj
= { isl_obj_none
, NULL
};
1789 isl_schedule
*schedule
;
1791 schedule
= get_schedule(s
, table
);
1793 obj
.v
= isl_schedule_get_map(schedule
);
1794 obj
.type
= isl_obj_union_map
;
1796 isl_schedule_free(schedule
);
1801 /* Read a schedule for code generation.
1802 * If the input is a set rather than a map, then we construct
1803 * an identity schedule on the given set.
1805 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1806 struct isl_hash_table
*table
)
1811 obj
= read_obj(s
, table
);
1812 ctx
= isl_stream_get_ctx(s
);
1814 if (is_subtype(obj
, isl_obj_union_map
)) {
1815 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1819 if (is_subtype(obj
, isl_obj_union_set
)) {
1820 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1821 return isl_union_set_identity(obj
.v
);
1825 isl_die(ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1828 /* Generate an AST for the given schedule and options and print
1829 * the AST on the printer.
1831 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1832 __isl_take isl_union_map
*schedule
,
1833 __isl_take isl_union_map
*options
)
1837 isl_ast_build
*build
;
1841 space
= isl_union_map_get_space(schedule
);
1842 context
= isl_set_universe(isl_space_params(space
));
1844 build
= isl_ast_build_from_context(context
);
1845 build
= isl_ast_build_set_options(build
, options
);
1846 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1847 isl_ast_build_free(build
);
1852 format
= isl_printer_get_output_format(p
);
1853 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1854 p
= isl_printer_print_ast_node(p
, tree
);
1855 p
= isl_printer_set_output_format(p
, format
);
1857 isl_ast_node_free(tree
);
1862 /* Perform the codegen operation.
1863 * In particular, read a schedule, check if the user has specified any options
1864 * and then generate an AST from the schedule (and options) and print it.
1866 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1867 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1869 isl_union_map
*schedule
;
1870 isl_union_map
*options
;
1872 schedule
= get_codegen_schedule(s
, table
);
1876 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1877 options
= read_map(s
, table
);
1879 options
= isl_union_map_empty(
1880 isl_union_map_get_space(schedule
));
1882 p
= print_code(p
, schedule
, options
);
1884 isl_stream_eat(s
, ';');
1889 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1891 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1893 struct isl_obj obj
= { isl_obj_none
, NULL
};
1894 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1895 struct isl_list
*list
;
1897 list
= isl_list_alloc(ctx
, 2);
1902 obj
.type
= isl_obj_list
;
1904 list
->obj
[0].type
= isl_obj_union_map
;
1905 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1907 if (isl_band_has_children(band
)) {
1908 isl_band_list
*children
;
1910 children
= isl_band_get_children(band
);
1911 list
->obj
[1] = band_list_to_obj_list(children
);
1913 list
->obj
[1].type
= isl_obj_list
;
1914 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1917 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1920 isl_band_free(band
);
1924 isl_band_free(band
);
1926 obj
.type
= isl_obj_none
;
1931 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1933 struct isl_obj obj
= { isl_obj_none
, NULL
};
1934 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1935 struct isl_list
*list
;
1938 n
= isl_band_list_n_band(bands
);
1939 list
= isl_list_alloc(ctx
, n
);
1944 obj
.type
= isl_obj_list
;
1946 for (i
= 0; i
< n
; ++i
) {
1949 band
= isl_band_list_get_band(bands
, i
);
1950 list
->obj
[i
] = band_to_obj_list(band
);
1951 if (!list
->obj
[i
].v
)
1955 isl_band_list_free(bands
);
1959 isl_band_list_free(bands
);
1961 obj
.type
= isl_obj_none
;
1966 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1967 struct isl_hash_table
*table
)
1969 struct isl_obj obj
= { isl_obj_none
, NULL
};
1970 isl_schedule
*schedule
;
1971 isl_band_list
*roots
;
1973 schedule
= get_schedule(s
, table
);
1977 roots
= isl_schedule_get_band_forest(schedule
);
1978 isl_schedule_free(schedule
);
1980 return band_list_to_obj_list(roots
);
1983 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1985 struct isl_token
*tok
;
1989 ctx
= isl_stream_get_ctx(s
);
1990 if (isl_stream_eat_if_available(s
, '+'))
1991 return transitive_closure(ctx
, obj
);
1993 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1994 if (obj
.type
!= isl_obj_union_map
)
1995 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1997 tok
= isl_stream_next_token(s
);
1998 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1999 isl_stream_error(s
, tok
, "expecting integer exponent");
2001 isl_stream_push_token(s
, tok
);
2005 v
= isl_token_get_val(ctx
, tok
);
2006 if (isl_val_is_zero(v
)) {
2007 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2010 isl_stream_push_token(s
, tok
);
2014 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2015 isl_token_free(tok
);
2022 obj
.type
= isl_obj_none
;
2027 static struct isl_obj
check_assert(struct isl_stream
*s
,
2028 struct isl_hash_table
*table
)
2033 obj
= read_expr(s
, table
);
2034 ctx
= isl_stream_get_ctx(s
);
2035 if (obj
.type
!= isl_obj_bool
)
2036 isl_die(ctx
, isl_error_invalid
,
2037 "expecting boolean expression", goto error
);
2038 if (obj
.v
!= &isl_bool_true
)
2039 isl_die(ctx
, isl_error_unknown
,
2040 "assertion failed", abort());
2043 obj
.type
= isl_obj_none
;
2048 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2052 struct isl_token
*tok
;
2053 struct isl_stream
*s_file
;
2054 struct iscc_options
*options
;
2058 tok
= isl_stream_next_token(s
);
2059 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2060 isl_stream_error(s
, tok
, "expecting filename");
2061 isl_token_free(tok
);
2065 ctx
= isl_stream_get_ctx(s
);
2066 options
= isl_ctx_peek_iscc_options(ctx
);
2067 if (!options
|| !options
->io
) {
2068 isl_token_free(tok
);
2069 isl_die(ctx
, isl_error_invalid
,
2070 "read operation not allowed", goto error
);
2073 name
= isl_token_get_str(ctx
, tok
);
2074 isl_token_free(tok
);
2075 file
= fopen(name
, "r");
2077 isl_assert(ctx
, file
, goto error
);
2079 s_file
= isl_stream_new_file(ctx
, file
);
2085 obj
= isl_stream_read_obj(s_file
);
2087 isl_stream_free(s_file
);
2092 obj
.type
= isl_obj_none
;
2097 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2098 struct isl_hash_table
*table
)
2101 struct isl_token
*tok
;
2102 struct isl_stream
*s_file
;
2103 struct iscc_options
*options
;
2109 tok
= isl_stream_next_token(s
);
2110 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2111 isl_stream_error(s
, tok
, "expecting filename");
2112 isl_token_free(tok
);
2116 obj
= read_expr(s
, table
);
2118 ctx
= isl_stream_get_ctx(s
);
2119 options
= isl_ctx_peek_iscc_options(ctx
);
2120 if (!options
|| !options
->io
) {
2121 isl_token_free(tok
);
2122 isl_die(ctx
, isl_error_invalid
,
2123 "write operation not allowed", goto error
);
2126 name
= isl_token_get_str(ctx
, tok
);
2127 isl_token_free(tok
);
2128 file
= fopen(name
, "w");
2131 isl_die(ctx
, isl_error_unknown
,
2132 "could not open file for writing", goto error
);
2134 p
= isl_printer_to_file(ctx
, file
);
2135 p
= isl_printer_set_output_format(p
, options
->format
);
2136 p
= obj
.type
->print(p
, obj
.v
);
2137 p
= isl_printer_end_line(p
);
2138 isl_printer_free(p
);
2143 obj
.type
= isl_obj_none
;
2148 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2150 struct isl_token
*tok
;
2151 struct isl_obj obj
= { isl_obj_none
, NULL
};
2153 tok
= isl_stream_next_token(s
);
2156 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2158 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2161 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2162 isl_token_free(tok
);
2164 obj
.type
= isl_obj_str
;
2166 isl_stream_push_token(s
, tok
);
2169 isl_token_free(tok
);
2173 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2175 struct isl_token
*tok
;
2176 struct isl_obj obj
= { isl_obj_none
, NULL
};
2179 tok
= isl_stream_next_token(s
);
2182 type
= isl_token_get_type(tok
);
2183 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2184 int is_true
= type
== ISL_TOKEN_TRUE
;
2185 isl_token_free(tok
);
2186 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2187 obj
.type
= isl_obj_bool
;
2189 isl_stream_push_token(s
, tok
);
2193 static __isl_give
char *read_ident(struct isl_stream
*s
)
2197 struct isl_token
*tok
, *tok2
;
2199 name
= isl_stream_read_ident_if_available(s
);
2203 tok
= isl_stream_next_token(s
);
2206 if (isl_token_get_type(tok
) != '$') {
2207 isl_stream_push_token(s
, tok
);
2210 tok2
= isl_stream_next_token(s
);
2211 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2213 isl_stream_push_token(s
, tok2
);
2214 isl_stream_push_token(s
, tok
);
2218 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2219 name
= isl_val_to_str(v
);
2221 isl_token_free(tok
);
2222 isl_token_free(tok2
);
2227 static struct isl_obj
read_list(struct isl_stream
*s
,
2228 struct isl_hash_table
*table
, struct isl_obj obj
)
2230 struct isl_list
*list
;
2232 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2236 list
->obj
[1] = read_obj(s
, table
);
2238 obj
.type
= isl_obj_list
;
2240 if (!list
->obj
[1].v
)
2243 while (isl_stream_eat_if_available(s
, ',')) {
2244 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2252 obj
.type
= isl_obj_none
;
2257 static struct isl_obj
read_obj(struct isl_stream
*s
,
2258 struct isl_hash_table
*table
)
2261 struct isl_obj obj
= { isl_obj_none
, NULL
};
2263 struct isc_un_op
*op
= NULL
;
2265 obj
= read_string_if_available(s
);
2268 obj
= read_bool_if_available(s
);
2271 ctx
= isl_stream_get_ctx(s
);
2272 if (isl_stream_eat_if_available(s
, '(')) {
2273 if (isl_stream_next_token_is(s
, ')')) {
2274 obj
.type
= isl_obj_list
;
2275 obj
.v
= isl_list_alloc(ctx
, 0);
2277 obj
= read_expr(s
, table
);
2278 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2279 obj
= read_list(s
, table
, obj
);
2281 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2284 op
= read_prefix_un_op_if_available(s
);
2286 return read_un_op_expr(s
, table
, op
);
2288 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2289 return check_assert(s
, table
);
2290 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2291 return read_from_file(s
);
2292 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2293 return write_to_file(s
, table
);
2294 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2295 return vertices(s
, table
);
2296 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2297 return any(s
, table
);
2298 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2299 return last(s
, table
);
2300 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2301 return schedule(s
, table
);
2302 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2303 return schedule_forest(s
, table
);
2304 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2305 return type_of(s
, table
);
2307 name
= read_ident(s
);
2309 obj
= stored_obj(ctx
, table
, name
);
2311 obj
= isl_stream_read_obj(s
);
2316 if (isl_stream_eat_if_available(s
, '^'))
2317 obj
= power(s
, obj
);
2318 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2319 obj
= obj_at_index(s
, obj
);
2320 else if (is_subtype(obj
, isl_obj_union_map
) &&
2321 isl_stream_eat_if_available(s
, '(')) {
2322 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2323 obj
= apply(s
, obj
.v
, table
);
2324 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2325 isl_stream_eat_if_available(s
, '(')) {
2326 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2327 obj
= apply_fun(s
, obj
, table
);
2328 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2329 isl_stream_eat_if_available(s
, '(')) {
2330 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2331 obj
= apply_fun(s
, obj
, table
);
2337 obj
.type
= isl_obj_none
;
2342 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2343 struct isl_obj lhs
, struct isl_obj rhs
)
2347 for (i
= 0; ; ++i
) {
2350 if (bin_ops
[i
].op
!= like
->op
)
2352 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2354 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2360 for (i
= 0; ; ++i
) {
2361 if (!named_bin_ops
[i
].name
)
2363 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2365 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2367 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2370 return &named_bin_ops
[i
].op
;
2376 static int next_is_neg_int(struct isl_stream
*s
)
2378 struct isl_token
*tok
;
2381 tok
= isl_stream_next_token(s
);
2382 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2384 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2385 ret
= isl_val_is_neg(v
);
2389 isl_stream_push_token(s
, tok
);
2394 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2395 struct isl_obj lhs
, struct isl_obj rhs
)
2399 lhs
= convert(ctx
, lhs
, op
->lhs
);
2400 rhs
= convert(ctx
, rhs
, op
->rhs
);
2401 if (op
->res
!= isl_obj_bool
)
2402 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2404 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2407 obj
.v
= isl_bool_from_int(res
);
2414 static struct isl_obj
read_expr(struct isl_stream
*s
,
2415 struct isl_hash_table
*table
)
2418 struct isl_obj obj
= { isl_obj_none
, NULL
};
2419 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2421 obj
= read_obj(s
, table
);
2422 ctx
= isl_stream_get_ctx(s
);
2424 struct isc_bin_op
*op
= NULL
;
2426 op
= read_bin_op_if_available(s
, obj
);
2430 right_obj
= read_obj(s
, table
);
2432 op
= find_matching_bin_op(op
, obj
, right_obj
);
2435 isl_die(ctx
, isl_error_invalid
,
2436 "no such binary operator defined on given operands",
2439 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2442 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2443 right_obj
= read_obj(s
, table
);
2444 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2449 free_obj(right_obj
);
2451 obj
.type
= isl_obj_none
;
2456 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2457 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2459 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2460 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2463 struct isl_obj obj
= { isl_obj_none
, NULL
};
2467 struct isc_bin_op
*op
= NULL
;
2472 if (isl_stream_is_empty(s
))
2475 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2476 return source_file(s
, table
, p
);
2477 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2478 return codegen(s
, table
, p
);
2480 assign
= is_assign(s
);
2482 lhs
= isl_stream_read_ident_if_available(s
);
2483 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2485 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2490 obj
= read_expr(s
, table
);
2491 ctx
= isl_stream_get_ctx(s
);
2492 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2493 fprintf(stderr
, "Interrupted\n");
2494 isl_ctx_reset_error(ctx
);
2496 if (isl_stream_eat(s
, ';'))
2500 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2501 p
= obj
.type
->print(p
, obj
.v
);
2502 p
= isl_printer_end_line(p
);
2507 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2508 static int count
= 0;
2509 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2510 lhs
= strdup(buf
+ 1);
2512 p
= isl_printer_print_str(p
, buf
);
2513 p
= isl_printer_print_str(p
, " := ");
2514 p
= obj
.type
->print(p
, obj
.v
);
2515 p
= isl_printer_end_line(p
);
2517 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2522 isl_stream_flush_tokens(s
);
2523 isl_stream_skip_line(s
);
2529 int free_cb(void **entry
, void *user
)
2531 struct isl_named_obj
*named
= *entry
;
2533 free_obj(named
->obj
);
2540 static void register_named_ops(struct isl_stream
*s
)
2544 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2545 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2546 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2549 for (i
= 0; ; ++i
) {
2550 if (!named_un_ops
[i
].name
)
2552 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2553 named_un_ops
[i
].name
);
2554 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2557 for (i
= 0; ; ++i
) {
2558 if (!named_bin_ops
[i
].name
)
2560 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2561 named_bin_ops
[i
].name
);
2562 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2566 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2567 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2570 struct isl_token
*tok
;
2571 struct isl_stream
*s_file
;
2572 struct iscc_options
*options
;
2576 tok
= isl_stream_next_token(s
);
2577 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2578 isl_stream_error(s
, tok
, "expecting filename");
2579 isl_token_free(tok
);
2583 isl_stream_eat(s
, ';');
2585 ctx
= isl_stream_get_ctx(s
);
2586 options
= isl_ctx_peek_iscc_options(ctx
);
2587 if (!options
|| !options
->io
) {
2588 isl_token_free(tok
);
2589 isl_die(ctx
, isl_error_invalid
,
2590 "source operation not allowed", return p
);
2593 name
= isl_token_get_str(ctx
, tok
);
2594 isl_token_free(tok
);
2595 file
= fopen(name
, "r");
2597 isl_assert(ctx
, file
, return p
);
2599 s_file
= isl_stream_new_file(ctx
, file
);
2605 register_named_ops(s_file
);
2607 while (!isl_stream_is_empty(s_file
))
2608 p
= read_line(s_file
, table
, p
, 0);
2610 isl_stream_free(s_file
);
2616 int main(int argc
, char **argv
)
2618 struct isl_ctx
*ctx
;
2619 struct isl_stream
*s
;
2620 struct isl_hash_table
*table
;
2621 struct iscc_options
*options
;
2623 int tty
= isatty(0);
2625 options
= iscc_options_new_with_defaults();
2628 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2629 pet_options_set_autodetect(ctx
, 1);
2630 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2631 s
= isl_stream_new_file(ctx
, stdin
);
2633 table
= isl_hash_table_alloc(ctx
, 10);
2635 p
= isl_printer_to_file(ctx
, stdout
);
2636 p
= isl_printer_set_output_format(p
, options
->format
);
2639 register_named_ops(s
);
2641 install_signal_handler(ctx
);
2643 while (p
&& !isl_stream_is_empty(s
)) {
2644 isl_ctx_resume(ctx
);
2645 p
= read_line(s
, table
, p
, tty
);
2648 remove_signal_handler(ctx
);
2650 isl_printer_free(p
);
2651 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2652 isl_hash_table_free(ctx
, table
);