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 struct isc_bin_op bin_ops
[] = {
385 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
386 (isc_bin_op_fn
) &isl_val_add
},
387 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
388 (isc_bin_op_fn
) &isl_val_sub
},
389 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
390 (isc_bin_op_fn
) &isl_val_mul
},
391 { '+', isl_obj_pw_multi_aff
, isl_obj_pw_multi_aff
,
392 isl_obj_pw_multi_aff
,
393 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
394 { '+', isl_obj_union_set
, isl_obj_union_set
,
396 (isc_bin_op_fn
) &isl_union_set_union
},
397 { '+', isl_obj_union_map
, isl_obj_union_map
,
399 (isc_bin_op_fn
) &isl_union_map_union
},
400 { '-', isl_obj_union_set
, isl_obj_union_set
,
402 (isc_bin_op_fn
) &isl_union_set_subtract
},
403 { '-', isl_obj_union_map
, isl_obj_union_map
,
405 (isc_bin_op_fn
) &isl_union_map_subtract
},
406 { '*', isl_obj_union_set
, isl_obj_union_set
,
408 (isc_bin_op_fn
) &isl_union_set_intersect
},
409 { '*', isl_obj_union_map
, isl_obj_union_map
,
411 (isc_bin_op_fn
) &isl_union_map_intersect
},
412 { '*', isl_obj_union_map
, isl_obj_union_set
,
414 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
415 { '.', isl_obj_union_map
, isl_obj_union_map
,
417 (isc_bin_op_fn
) &isl_union_map_apply_range
},
418 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
419 isl_obj_union_pw_qpolynomial
,
420 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
421 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
423 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
424 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
426 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
427 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
428 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
429 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
430 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
431 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
433 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
434 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
436 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
437 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
439 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
440 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
442 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
443 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
445 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
446 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
448 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
449 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
452 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
453 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
456 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
457 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
459 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
460 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
462 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
463 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
465 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
466 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
468 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
469 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
471 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
472 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
474 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
475 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
477 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
478 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
480 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
481 { '.', isl_obj_union_pw_qpolynomial_fold
,
482 isl_obj_union_pw_qpolynomial_fold
,
483 isl_obj_union_pw_qpolynomial_fold
,
484 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
485 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
486 isl_obj_union_pw_qpolynomial
,
487 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
488 { '+', isl_obj_union_pw_qpolynomial
,
489 isl_obj_union_pw_qpolynomial_fold
,
490 isl_obj_union_pw_qpolynomial_fold
,
491 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
492 { '+', isl_obj_union_pw_qpolynomial_fold
,
493 isl_obj_union_pw_qpolynomial
,
494 isl_obj_union_pw_qpolynomial_fold
,
495 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
496 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
497 isl_obj_union_pw_qpolynomial
,
498 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
499 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
500 isl_obj_union_pw_qpolynomial
,
501 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
502 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
503 isl_obj_union_pw_qpolynomial
,
504 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
505 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
506 isl_obj_union_pw_qpolynomial_fold
,
507 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
508 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
509 isl_obj_union_pw_qpolynomial_fold
,
510 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
511 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
512 isl_obj_union_pw_qpolynomial
,
513 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
514 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
515 isl_obj_union_pw_qpolynomial
,
516 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
517 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
518 isl_obj_union_pw_qpolynomial_fold
,
519 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
520 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
521 isl_obj_union_pw_qpolynomial
,
522 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
523 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
524 isl_obj_union_pw_qpolynomial
,
525 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
526 { '%', isl_obj_union_set
, isl_obj_union_set
,
528 (isc_bin_op_fn
) &isl_union_set_gist
},
529 { '%', isl_obj_union_map
, isl_obj_union_map
,
531 (isc_bin_op_fn
) &isl_union_map_gist
},
532 { '%', isl_obj_union_map
, isl_obj_union_set
,
534 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
535 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
536 isl_obj_union_pw_qpolynomial
,
537 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
538 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
539 isl_obj_union_pw_qpolynomial_fold
,
540 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
541 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
542 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
543 { .test
= (isc_bin_test_fn
)
544 &isl_union_pw_qpolynomial_plain_is_equal
} },
545 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
546 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
547 { .test
= (isc_bin_test_fn
)
548 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
549 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
550 (isc_bin_op_fn
) &isl_str_concat
},
554 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
555 __isl_take isl_union_map
*umap2
)
557 return isl_union_map_apply_range(umap2
, umap1
);
560 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
561 __isl_take isl_union_pw_qpolynomial
*upwqp
,
562 __isl_take isl_union_map
*umap
)
564 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
567 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
568 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
569 __isl_take isl_union_map
*umap
)
571 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
574 struct isc_named_bin_op named_bin_ops
[] = {
575 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
577 (isc_bin_op_fn
) &map_after_map
} },
578 { "after", { -1, isl_obj_union_pw_qpolynomial
,
579 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
580 (isc_bin_op_fn
) &qpolynomial_after_map
} },
581 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
582 isl_obj_union_map
, isl_obj_list
,
583 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
584 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
586 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
587 { "before", { -1, isl_obj_union_map
,
588 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
589 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
590 { "before", { -1, isl_obj_union_map
,
591 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
592 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
593 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
595 (isc_bin_op_fn
) &isl_union_set_product
} },
596 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
598 (isc_bin_op_fn
) &isl_union_map_product
} },
602 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
604 return isl_set_from_basic_set(isl_union_set_sample(uset
));
607 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
609 return isl_map_from_basic_map(isl_union_map_sample(umap
));
612 static __isl_give
struct isl_list
*union_map_power(
613 __isl_take isl_union_map
*umap
)
616 struct isl_list
*list
;
619 ctx
= isl_union_map_get_ctx(umap
);
620 list
= isl_list_alloc(ctx
, 2);
624 list
->obj
[0].type
= isl_obj_union_map
;
625 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
626 list
->obj
[1].type
= isl_obj_bool
;
627 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
628 if (exact
< 0 || !list
->obj
[0].v
)
633 isl_union_map_free(umap
);
639 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
640 __isl_take isl_union_pw_qpolynomial
*upwqp
)
643 struct isl_list
*list
;
646 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
647 list
= isl_list_alloc(ctx
, 2);
651 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
652 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
653 isl_fold_max
, &tight
);
654 list
->obj
[1].type
= isl_obj_bool
;
655 list
->obj
[1].v
= tight
? &isl_bool_true
: &isl_bool_false
;
656 if (tight
< 0 || !list
->obj
[0].v
)
661 isl_union_pw_qpolynomial_free(upwqp
);
668 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
671 struct isl_list
*list
;
672 struct pet_scop
*scop
;
673 isl_union_map
*sched
, *may_reads
, *must_writes
, *may_writes
;
674 isl_union_set
*domain
;
675 struct iscc_options
*options
;
681 options
= isl_ctx_peek_iscc_options(ctx
);
682 if (!options
|| !options
->io
) {
684 isl_die(ctx
, isl_error_invalid
,
685 "parse_file operation not allowed", return NULL
);
688 list
= isl_list_alloc(ctx
, 5);
692 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
693 domain
= pet_scop_collect_domains(scop
);
694 sched
= pet_scop_collect_schedule(scop
);
695 may_reads
= pet_scop_collect_may_reads(scop
);
696 may_writes
= pet_scop_collect_may_writes(scop
);
697 must_writes
= pet_scop_collect_must_writes(scop
);
700 list
->obj
[0].type
= isl_obj_union_set
;
701 list
->obj
[0].v
= domain
;
702 list
->obj
[1].type
= isl_obj_union_map
;
703 list
->obj
[1].v
= must_writes
;
704 list
->obj
[2].type
= isl_obj_union_map
;
705 list
->obj
[2].v
= may_writes
;
706 list
->obj
[3].type
= isl_obj_union_map
;
707 list
->obj
[3].v
= may_reads
;
708 list
->obj
[4].type
= isl_obj_union_map
;
709 list
->obj
[4].v
= sched
;
711 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
712 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
724 static int add_point(__isl_take isl_point
*pnt
, void *user
)
726 isl_union_set
**scan
= (isl_union_set
**) user
;
728 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
733 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
737 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
739 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
740 isl_union_set_free(scan
);
744 isl_union_set_free(uset
);
748 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
750 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
753 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
754 __isl_take isl_union_pw_qpolynomial
*upwqp
)
756 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
759 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
760 __isl_take isl_union_pw_qpolynomial
*upwqp
)
762 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
765 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
766 __isl_take isl_union_pw_qpolynomial
*upwqp
)
768 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
771 typedef void *(*isc_un_op_fn
)(void *arg
);
773 enum isl_token_type op
;
778 struct isc_named_un_op
{
782 struct isc_named_un_op named_un_ops
[] = {
783 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
784 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
785 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
786 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
787 {"card", { -1, isl_obj_union_set
,
788 isl_obj_union_pw_qpolynomial
,
789 (isc_un_op_fn
) &isl_union_set_card
} },
790 {"card", { -1, isl_obj_union_map
,
791 isl_obj_union_pw_qpolynomial
,
792 (isc_un_op_fn
) &isl_union_map_card
} },
793 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
794 (isc_un_op_fn
) &isl_union_set_coalesce
} },
795 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
796 (isc_un_op_fn
) &isl_union_map_coalesce
} },
797 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
798 isl_obj_union_pw_qpolynomial
,
799 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
800 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
801 isl_obj_union_pw_qpolynomial_fold
,
802 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
803 {"coefficients", { -1, isl_obj_union_set
,
805 (isc_un_op_fn
) &isl_union_set_coefficients
} },
806 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
807 (isc_un_op_fn
) &isl_union_set_solutions
} },
808 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
809 (isc_un_op_fn
) &isl_union_map_deltas
} },
810 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
811 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
812 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
813 (isc_un_op_fn
) &isl_union_map_domain
} },
814 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
815 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
816 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
818 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
819 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
820 (isc_un_op_fn
) &isl_union_map_domain
} },
821 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
823 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
824 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
826 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
827 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
828 (isc_un_op_fn
) &isl_union_map_domain_map
} },
829 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
830 (isc_un_op_fn
) &isl_union_map_range
} },
831 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
832 (isc_un_op_fn
) &isl_union_map_range
} },
833 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
834 (isc_un_op_fn
) &isl_union_map_range_map
} },
835 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
836 (isc_un_op_fn
) &isl_union_set_identity
} },
837 {"lattice_width", { -1, isl_obj_union_set
,
838 isl_obj_union_pw_qpolynomial
,
839 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
840 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
841 (isc_un_op_fn
) &isl_union_map_lexmin
} },
842 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
843 (isc_un_op_fn
) &isl_union_map_lexmax
} },
844 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
845 (isc_un_op_fn
) &isl_union_set_lexmin
} },
846 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
847 (isc_un_op_fn
) &isl_union_set_lexmax
} },
848 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
849 (isc_un_op_fn
) &isl_union_set_lift
} },
850 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
851 (isc_un_op_fn
) &isl_union_map_params
} },
852 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
853 (isc_un_op_fn
) &isl_union_set_params
} },
854 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
855 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
856 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
857 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
858 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
859 isl_obj_union_pw_qpolynomial
,
860 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
861 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
862 isl_obj_union_pw_qpolynomial
,
863 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
864 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
865 isl_obj_union_pw_qpolynomial
,
866 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
868 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
869 (isc_un_op_fn
) &parse
} },
871 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
872 (isc_un_op_fn
) &union_map_power
} },
873 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
874 (isc_un_op_fn
) &union_set_sample
} },
875 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
876 (isc_un_op_fn
) &union_map_sample
} },
877 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
878 (isc_un_op_fn
) &union_set_scan
} },
879 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
880 (isc_un_op_fn
) &union_map_scan
} },
881 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
882 isl_obj_union_pw_qpolynomial
,
883 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
884 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
885 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
886 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
887 (isc_un_op_fn
) &isl_union_set_unwrap
} },
888 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
889 (isc_un_op_fn
) &isl_union_map_wrap
} },
890 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
891 (isc_un_op_fn
) &isl_union_map_zip
} },
895 struct isl_named_obj
{
900 static void free_obj(struct isl_obj obj
)
902 obj
.type
->free(obj
.v
);
905 static int same_name(const void *entry
, const void *val
)
907 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
909 return !strcmp(named
->name
, val
);
912 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
913 char *name
, struct isl_obj obj
)
915 struct isl_hash_table_entry
*entry
;
917 struct isl_named_obj
*named
;
919 name_hash
= isl_hash_string(isl_hash_init(), name
);
920 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
925 free_obj(named
->obj
);
928 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
943 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
944 struct isl_hash_table
*table
, char *name
)
946 struct isl_obj obj
= { isl_obj_none
, NULL
};
947 struct isl_hash_table_entry
*entry
;
950 name_hash
= isl_hash_string(isl_hash_init(), name
);
951 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
953 struct isl_named_obj
*named
;
956 } else if (isdigit(name
[0]))
957 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
959 fprintf(stderr
, "unknown identifier '%s'\n", name
);
962 obj
.v
= obj
.type
->copy(obj
.v
);
966 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
968 if (obj
.type
== super
)
970 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
972 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
974 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
975 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
976 int is_set
= isl_space_is_set(space
);
977 isl_space_free(space
);
980 if (obj
.type
== isl_obj_pw_qpolynomial
&&
981 super
== isl_obj_union_pw_qpolynomial
)
983 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
984 super
== isl_obj_union_pw_qpolynomial_fold
)
986 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
988 if (obj
.type
== isl_obj_list
) {
989 struct isl_list
*list
= obj
.v
;
990 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
991 return is_subtype(list
->obj
[0], super
);
993 if (super
== isl_obj_str
)
998 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1000 struct isl_list
*list
= obj
.v
;
1003 obj
.v
= obj
.type
->copy(obj
.v
);
1005 isl_list_free(list
);
1010 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1013 if (obj
.type
== type
)
1015 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1016 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1017 obj
.type
= isl_obj_union_set
;
1018 obj
.v
= isl_union_set_from_set(set
);
1021 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1022 obj
.type
= isl_obj_union_map
;
1023 obj
.v
= isl_union_map_from_map(obj
.v
);
1026 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1027 obj
.type
= isl_obj_union_set
;
1028 obj
.v
= isl_union_set_from_set(obj
.v
);
1031 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1032 type
== isl_obj_union_pw_qpolynomial
) {
1033 obj
.type
= isl_obj_union_pw_qpolynomial
;
1034 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1037 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1038 type
== isl_obj_union_pw_qpolynomial_fold
) {
1039 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1040 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1043 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1044 if (type
== isl_obj_union_map
) {
1045 obj
.type
= isl_obj_union_map
;
1048 if (type
== isl_obj_union_pw_qpolynomial
) {
1049 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1050 isl_union_set_free(obj
.v
);
1051 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1052 obj
.type
= isl_obj_union_pw_qpolynomial
;
1055 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1056 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1057 isl_union_set_free(obj
.v
);
1058 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1060 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1064 if (obj
.type
== isl_obj_list
) {
1065 struct isl_list
*list
= obj
.v
;
1066 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1067 return convert(ctx
, obj_at(obj
, 0), type
);
1069 if (type
== isl_obj_str
) {
1074 p
= isl_printer_to_str(ctx
);
1077 p
= obj
.type
->print(p
, obj
.v
);
1078 s
= isl_printer_get_str(p
);
1079 isl_printer_free(p
);
1081 str
= isl_str_from_string(ctx
, s
);
1086 obj
.type
= isl_obj_str
;
1092 obj
.type
= isl_obj_none
;
1097 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1101 struct isl_token
*tok
;
1103 tok
= isl_stream_next_token(s
);
1107 for (i
= 0; ; ++i
) {
1110 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1112 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1115 isl_token_free(tok
);
1119 for (i
= 0; ; ++i
) {
1120 if (!named_bin_ops
[i
].name
)
1122 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1124 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1127 isl_token_free(tok
);
1128 return &named_bin_ops
[i
].op
;
1131 isl_stream_push_token(s
, tok
);
1136 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1139 struct isl_token
*tok
;
1141 tok
= isl_stream_next_token(s
);
1145 for (i
= 0; ; ++i
) {
1146 if (!named_un_ops
[i
].name
)
1148 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1151 isl_token_free(tok
);
1152 return &named_un_ops
[i
].op
;
1155 isl_stream_push_token(s
, tok
);
1160 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1165 for (i
= 0; ; ++i
) {
1166 if (!named_un_ops
[i
].name
)
1168 if (named_un_ops
[i
].op
.op
!= like
->op
)
1170 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1173 return &named_un_ops
[i
].op
;
1179 static int is_assign(struct isl_stream
*s
)
1181 struct isl_token
*tok
;
1182 struct isl_token
*tok2
;
1185 tok
= isl_stream_next_token(s
);
1188 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1189 isl_stream_push_token(s
, tok
);
1193 tok2
= isl_stream_next_token(s
);
1195 isl_stream_push_token(s
, tok
);
1198 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1199 isl_stream_push_token(s
, tok2
);
1200 isl_stream_push_token(s
, tok
);
1205 static struct isl_obj
read_obj(struct isl_stream
*s
,
1206 struct isl_hash_table
*table
);
1207 static struct isl_obj
read_expr(struct isl_stream
*s
,
1208 struct isl_hash_table
*table
);
1210 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1211 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1213 struct isl_obj obj
= { isl_obj_none
, NULL
};
1215 obj
= read_obj(s
, table
);
1219 op
= find_matching_un_op(op
, obj
);
1222 isl_die(s
->ctx
, isl_error_invalid
,
1223 "no such unary operator defined on given operand",
1226 obj
= convert(s
->ctx
, obj
, op
->arg
);
1227 obj
.v
= op
->fn(obj
.v
);
1233 obj
.type
= isl_obj_none
;
1238 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1240 struct isl_list
*list
;
1243 if (obj
.type
!= isl_obj_union_map
)
1244 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1245 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1246 list
= isl_list_alloc(ctx
, 2);
1250 list
->obj
[0].type
= isl_obj_union_map
;
1251 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1252 list
->obj
[1].type
= isl_obj_bool
;
1253 list
->obj
[1].v
= exact
? &isl_bool_true
: &isl_bool_false
;
1255 obj
.type
= isl_obj_list
;
1256 if (exact
< 0 || !list
->obj
[0].v
)
1262 obj
.type
= isl_obj_none
;
1267 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1269 struct isl_list
*list
= obj
.v
;
1270 struct isl_token
*tok
;
1274 tok
= isl_stream_next_token(s
);
1275 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1276 isl_stream_error(s
, tok
, "expecting index");
1278 isl_stream_push_token(s
, tok
);
1281 v
= isl_token_get_val(s
->ctx
, tok
);
1282 i
= isl_val_get_num_si(v
);
1284 isl_token_free(tok
);
1285 isl_assert(s
->ctx
, i
< list
->n
, goto error
);
1286 if (isl_stream_eat(s
, ']'))
1289 return obj_at(obj
, i
);
1292 obj
.type
= isl_obj_none
;
1297 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1298 struct isl_hash_table
*table
)
1302 obj
= read_expr(s
, table
);
1303 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1304 is_subtype(obj
, isl_obj_union_map
), goto error
);
1306 if (obj
.type
== isl_obj_list
) {
1307 struct isl_list
*list
= obj
.v
;
1308 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1309 obj
= obj_at(obj
, 0);
1311 if (obj
.type
== isl_obj_set
)
1312 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1313 else if (obj
.type
== isl_obj_map
)
1314 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1315 if (obj
.type
== isl_obj_union_set
) {
1316 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1318 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1322 if (isl_stream_eat(s
, ')'))
1327 isl_union_map_free(umap
);
1330 obj
.type
= isl_obj_none
;
1335 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1336 __isl_take isl_union_set
*uset
)
1338 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1339 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1341 obj
.type
= isl_obj_list
;
1342 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1347 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1348 __isl_take isl_union_map
*umap
)
1350 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1351 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1353 obj
.type
= isl_obj_list
;
1354 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1359 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1360 struct isl_obj obj
, struct isl_hash_table
*table
)
1364 arg
= read_expr(s
, table
);
1365 if (!is_subtype(arg
, isl_obj_union_map
) &&
1366 !is_subtype(arg
, isl_obj_union_set
))
1367 isl_die(s
->ctx
, isl_error_invalid
,
1368 "expecting set of map argument", goto error
);
1370 if (arg
.type
== isl_obj_list
) {
1371 struct isl_list
*list
= arg
.v
;
1372 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1373 arg
= obj_at(arg
, 0);
1375 if (arg
.type
== isl_obj_set
)
1376 arg
= convert(s
->ctx
, arg
, isl_obj_union_set
);
1377 else if (arg
.type
== isl_obj_map
)
1378 arg
= convert(s
->ctx
, arg
, isl_obj_union_map
);
1379 if (arg
.type
== isl_obj_union_set
)
1380 obj
= apply_fun_set(obj
, arg
.v
);
1382 obj
= apply_fun_map(obj
, arg
.v
);
1386 if (isl_stream_eat(s
, ')'))
1394 obj
.type
= isl_obj_none
;
1399 struct add_vertex_data
{
1400 struct isl_list
*list
;
1404 static int add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1406 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1410 ma
= isl_vertex_get_expr(vertex
);
1411 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1413 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1414 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1417 isl_vertex_free(vertex
);
1422 static int set_vertices(__isl_take isl_set
*set
, void *user
)
1425 isl_basic_set
*hull
;
1426 isl_vertices
*vertices
= NULL
;
1427 struct isl_list
*list
= NULL
;
1429 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1431 set
= isl_set_remove_divs(set
);
1432 hull
= isl_set_convex_hull(set
);
1433 vertices
= isl_basic_set_compute_vertices(hull
);
1434 isl_basic_set_free(hull
);
1438 ctx
= isl_vertices_get_ctx(vertices
);
1439 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1444 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1446 data
->list
= isl_list_concat(list
, data
->list
);
1448 isl_vertices_free(vertices
);
1453 isl_vertices_free(vertices
);
1457 static struct isl_obj
vertices(struct isl_stream
*s
,
1458 struct isl_hash_table
*table
)
1462 struct isl_list
*list
= NULL
;
1463 isl_union_set
*uset
;
1464 struct add_vertex_data data
= { NULL
};
1466 obj
= read_expr(s
, table
);
1467 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1468 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1472 ctx
= isl_union_set_get_ctx(uset
);
1473 list
= isl_list_alloc(ctx
, 0);
1479 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1482 isl_union_set_free(uset
);
1484 obj
.type
= isl_obj_list
;
1489 isl_union_set_free(uset
);
1490 isl_list_free(data
.list
);
1492 obj
.type
= isl_obj_none
;
1497 static struct isl_obj
type_of(struct isl_stream
*s
,
1498 struct isl_hash_table
*table
)
1502 const char *type
= "unknown";
1504 obj
= read_expr(s
, table
);
1506 if (obj
.type
== isl_obj_map
||
1507 obj
.type
== isl_obj_union_map
)
1509 if (obj
.type
== isl_obj_set
||
1510 obj
.type
== isl_obj_union_set
)
1512 if (obj
.type
== isl_obj_pw_multi_aff
)
1513 type
= "piecewise multi-quasiaffine expression";
1514 if (obj
.type
== isl_obj_pw_qpolynomial
||
1515 obj
.type
== isl_obj_union_pw_qpolynomial
)
1516 type
= "piecewise quasipolynomial";
1517 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1518 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1519 type
= "piecewise quasipolynomial fold";
1520 if (obj
.type
== isl_obj_list
)
1522 if (obj
.type
== isl_obj_bool
)
1524 if (obj
.type
== isl_obj_str
)
1526 if (obj
.type
== isl_obj_val
)
1530 obj
.type
= isl_obj_str
;
1531 obj
.v
= isl_str_from_string(s
->ctx
, strdup(type
));
1536 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1537 struct isl_hash_table
*table
)
1541 obj
= read_obj(s
, table
);
1542 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1543 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1550 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1551 struct isl_hash_table
*table
)
1555 obj
= read_obj(s
, table
);
1556 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1557 isl_assert(s
->ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1564 static struct isl_obj
last_any(struct isl_stream
*s
,
1565 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1566 __isl_take isl_union_map
*may_source
)
1568 struct isl_obj obj
= { isl_obj_none
, NULL
};
1569 isl_union_map
*sink
= NULL
;
1570 isl_union_map
*schedule
= NULL
;
1571 isl_union_map
*may_dep
;
1572 isl_union_map
*must_dep
;
1574 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1577 sink
= read_map(s
, table
);
1581 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1584 schedule
= read_map(s
, table
);
1588 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1589 schedule
, &must_dep
, &may_dep
,
1593 obj
.type
= isl_obj_union_map
;
1594 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1598 isl_union_map_free(may_source
);
1599 isl_union_map_free(must_source
);
1600 isl_union_map_free(sink
);
1601 isl_union_map_free(schedule
);
1603 obj
.type
= isl_obj_none
;
1608 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1610 struct isl_obj obj
= { isl_obj_none
, NULL
};
1611 isl_union_map
*must_source
= NULL
;
1612 isl_union_map
*may_source
= NULL
;
1613 isl_union_map
*sink
= NULL
;
1614 isl_union_map
*schedule
= NULL
;
1615 isl_union_map
*may_dep
;
1617 may_source
= read_map(s
, table
);
1621 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1622 must_source
= read_map(s
, table
);
1625 return last_any(s
, table
, must_source
, may_source
);
1628 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1631 sink
= read_map(s
, table
);
1635 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1638 schedule
= read_map(s
, table
);
1642 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1643 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1644 schedule
, NULL
, &may_dep
,
1648 obj
.type
= isl_obj_union_map
;
1653 isl_union_map_free(may_source
);
1654 isl_union_map_free(must_source
);
1655 isl_union_map_free(sink
);
1656 isl_union_map_free(schedule
);
1658 obj
.type
= isl_obj_none
;
1663 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1665 struct isl_obj obj
= { isl_obj_none
, NULL
};
1666 struct isl_list
*list
= NULL
;
1667 isl_union_map
*must_source
= NULL
;
1668 isl_union_map
*may_source
= NULL
;
1669 isl_union_map
*sink
= NULL
;
1670 isl_union_map
*schedule
= NULL
;
1671 isl_union_map
*must_dep
;
1672 isl_union_map
*must_no_source
;
1674 must_source
= read_map(s
, table
);
1678 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1679 may_source
= read_map(s
, table
);
1682 return last_any(s
, table
, must_source
, may_source
);
1685 list
= isl_list_alloc(s
->ctx
, 2);
1689 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1692 sink
= read_map(s
, table
);
1696 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1699 schedule
= read_map(s
, table
);
1703 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1704 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1705 schedule
, &must_dep
, NULL
,
1706 &must_no_source
, NULL
) < 0) {
1707 isl_list_free(list
);
1711 list
->obj
[0].type
= isl_obj_union_map
;
1712 list
->obj
[0].v
= must_dep
;
1713 list
->obj
[1].type
= isl_obj_union_map
;
1714 list
->obj
[1].v
= must_no_source
;
1717 obj
.type
= isl_obj_list
;
1721 isl_list_free(list
);
1722 isl_union_map_free(may_source
);
1723 isl_union_map_free(must_source
);
1724 isl_union_map_free(sink
);
1725 isl_union_map_free(schedule
);
1727 obj
.type
= isl_obj_none
;
1732 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1733 struct isl_hash_table
*table
)
1735 isl_union_set
*domain
;
1736 isl_union_map
*validity
;
1737 isl_union_map
*proximity
;
1739 domain
= read_set(s
, table
);
1743 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1744 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1747 isl_union_map
*umap
;
1748 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1749 umap
= read_map(s
, table
);
1750 validity
= isl_union_map_union(validity
, umap
);
1751 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1752 umap
= read_map(s
, table
);
1753 proximity
= isl_union_map_union(proximity
, umap
);
1758 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1761 static struct isl_obj
schedule(struct isl_stream
*s
,
1762 struct isl_hash_table
*table
)
1764 struct isl_obj obj
= { isl_obj_none
, NULL
};
1765 isl_schedule
*schedule
;
1767 schedule
= get_schedule(s
, table
);
1769 obj
.v
= isl_schedule_get_map(schedule
);
1770 obj
.type
= isl_obj_union_map
;
1772 isl_schedule_free(schedule
);
1777 /* Read a schedule for code generation.
1778 * If the input is a set rather than a map, then we construct
1779 * an identity schedule on the given set.
1781 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1782 struct isl_hash_table
*table
)
1786 obj
= read_obj(s
, table
);
1788 if (is_subtype(obj
, isl_obj_union_map
)) {
1789 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1793 if (is_subtype(obj
, isl_obj_union_set
)) {
1794 obj
= convert(s
->ctx
, obj
, isl_obj_union_set
);
1795 return isl_union_set_identity(obj
.v
);
1799 isl_die(s
->ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1802 /* Generate an AST for the given schedule and options and print
1803 * the AST on the printer.
1805 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1806 __isl_take isl_union_map
*schedule
,
1807 __isl_take isl_union_map
*options
)
1811 isl_ast_build
*build
;
1815 space
= isl_union_map_get_space(schedule
);
1816 context
= isl_set_universe(isl_space_params(space
));
1818 build
= isl_ast_build_from_context(context
);
1819 build
= isl_ast_build_set_options(build
, options
);
1820 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1821 isl_ast_build_free(build
);
1826 format
= isl_printer_get_output_format(p
);
1827 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1828 p
= isl_printer_print_ast_node(p
, tree
);
1829 p
= isl_printer_set_output_format(p
, format
);
1831 isl_ast_node_free(tree
);
1836 /* Perform the codegen operation.
1837 * In particular, read a schedule, check if the user has specified any options
1838 * and then generate an AST from the schedule (and options) and print it.
1840 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1841 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1843 isl_union_map
*schedule
;
1844 isl_union_map
*options
;
1846 schedule
= get_codegen_schedule(s
, table
);
1850 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1851 options
= read_map(s
, table
);
1853 options
= isl_union_map_empty(
1854 isl_union_map_get_space(schedule
));
1856 p
= print_code(p
, schedule
, options
);
1858 isl_stream_eat(s
, ';');
1863 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1865 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1867 struct isl_obj obj
= { isl_obj_none
, NULL
};
1868 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1869 struct isl_list
*list
;
1871 list
= isl_list_alloc(ctx
, 2);
1876 obj
.type
= isl_obj_list
;
1878 list
->obj
[0].type
= isl_obj_union_map
;
1879 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1881 if (isl_band_has_children(band
)) {
1882 isl_band_list
*children
;
1884 children
= isl_band_get_children(band
);
1885 list
->obj
[1] = band_list_to_obj_list(children
);
1887 list
->obj
[1].type
= isl_obj_list
;
1888 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1891 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1894 isl_band_free(band
);
1898 isl_band_free(band
);
1900 obj
.type
= isl_obj_none
;
1905 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1907 struct isl_obj obj
= { isl_obj_none
, NULL
};
1908 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1909 struct isl_list
*list
;
1912 n
= isl_band_list_n_band(bands
);
1913 list
= isl_list_alloc(ctx
, n
);
1918 obj
.type
= isl_obj_list
;
1920 for (i
= 0; i
< n
; ++i
) {
1923 band
= isl_band_list_get_band(bands
, i
);
1924 list
->obj
[i
] = band_to_obj_list(band
);
1925 if (!list
->obj
[i
].v
)
1929 isl_band_list_free(bands
);
1933 isl_band_list_free(bands
);
1935 obj
.type
= isl_obj_none
;
1940 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1941 struct isl_hash_table
*table
)
1943 struct isl_obj obj
= { isl_obj_none
, NULL
};
1944 isl_schedule
*schedule
;
1945 isl_band_list
*roots
;
1947 schedule
= get_schedule(s
, table
);
1951 roots
= isl_schedule_get_band_forest(schedule
);
1952 isl_schedule_free(schedule
);
1954 return band_list_to_obj_list(roots
);
1957 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1959 struct isl_token
*tok
;
1962 if (isl_stream_eat_if_available(s
, '+'))
1963 return transitive_closure(s
->ctx
, obj
);
1965 isl_assert(s
->ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1966 if (obj
.type
!= isl_obj_union_map
)
1967 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
1969 tok
= isl_stream_next_token(s
);
1970 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1971 isl_stream_error(s
, tok
, "expecting integer exponent");
1973 isl_stream_push_token(s
, tok
);
1977 v
= isl_token_get_val(s
->ctx
, tok
);
1978 if (isl_val_is_zero(v
)) {
1979 isl_stream_error(s
, tok
, "expecting non-zero exponent");
1982 isl_stream_push_token(s
, tok
);
1986 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
1987 isl_token_free(tok
);
1994 obj
.type
= isl_obj_none
;
1999 static struct isl_obj
check_assert(struct isl_stream
*s
,
2000 struct isl_hash_table
*table
)
2004 obj
= read_expr(s
, table
);
2005 if (obj
.type
!= isl_obj_bool
)
2006 isl_die(s
->ctx
, isl_error_invalid
,
2007 "expecting boolean expression", goto error
);
2008 if (obj
.v
!= &isl_bool_true
)
2009 isl_die(s
->ctx
, isl_error_unknown
,
2010 "assertion failed", abort());
2013 obj
.type
= isl_obj_none
;
2018 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2021 struct isl_token
*tok
;
2022 struct isl_stream
*s_file
;
2023 struct iscc_options
*options
;
2027 tok
= isl_stream_next_token(s
);
2028 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2029 isl_stream_error(s
, tok
, "expecting filename");
2030 isl_token_free(tok
);
2034 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2035 if (!options
|| !options
->io
) {
2036 isl_token_free(tok
);
2037 isl_die(s
->ctx
, isl_error_invalid
,
2038 "read operation not allowed", goto error
);
2041 name
= isl_token_get_str(s
->ctx
, tok
);
2042 isl_token_free(tok
);
2043 file
= fopen(name
, "r");
2045 isl_assert(s
->ctx
, file
, goto error
);
2047 s_file
= isl_stream_new_file(s
->ctx
, file
);
2053 obj
= isl_stream_read_obj(s_file
);
2055 isl_stream_free(s_file
);
2060 obj
.type
= isl_obj_none
;
2065 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2066 struct isl_hash_table
*table
)
2069 struct isl_token
*tok
;
2070 struct isl_stream
*s_file
;
2071 struct iscc_options
*options
;
2076 tok
= isl_stream_next_token(s
);
2077 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2078 isl_stream_error(s
, tok
, "expecting filename");
2079 isl_token_free(tok
);
2083 obj
= read_expr(s
, table
);
2085 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2086 if (!options
|| !options
->io
) {
2087 isl_token_free(tok
);
2088 isl_die(s
->ctx
, isl_error_invalid
,
2089 "write operation not allowed", goto error
);
2092 name
= isl_token_get_str(s
->ctx
, tok
);
2093 isl_token_free(tok
);
2094 file
= fopen(name
, "w");
2097 isl_die(s
->ctx
, isl_error_unknown
,
2098 "could not open file for writing", goto error
);
2100 p
= isl_printer_to_file(s
->ctx
, file
);
2101 p
= isl_printer_set_output_format(p
, options
->format
);
2102 p
= obj
.type
->print(p
, obj
.v
);
2103 p
= isl_printer_end_line(p
);
2104 isl_printer_free(p
);
2109 obj
.type
= isl_obj_none
;
2114 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2116 struct isl_token
*tok
;
2117 struct isl_obj obj
= { isl_obj_none
, NULL
};
2119 tok
= isl_stream_next_token(s
);
2122 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2124 str
= isl_str_alloc(s
->ctx
);
2127 str
->s
= isl_token_get_str(s
->ctx
, tok
);
2128 isl_token_free(tok
);
2130 obj
.type
= isl_obj_str
;
2132 isl_stream_push_token(s
, tok
);
2135 isl_token_free(tok
);
2139 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2141 struct isl_token
*tok
;
2142 struct isl_obj obj
= { isl_obj_none
, NULL
};
2145 tok
= isl_stream_next_token(s
);
2148 type
= isl_token_get_type(tok
);
2149 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2150 int is_true
= type
== ISL_TOKEN_TRUE
;
2151 isl_token_free(tok
);
2152 obj
.v
= is_true
? &isl_bool_true
: &isl_bool_false
;
2153 obj
.type
= isl_obj_bool
;
2155 isl_stream_push_token(s
, tok
);
2159 static __isl_give
char *read_ident(struct isl_stream
*s
)
2163 struct isl_token
*tok
, *tok2
;
2165 name
= isl_stream_read_ident_if_available(s
);
2169 tok
= isl_stream_next_token(s
);
2172 if (isl_token_get_type(tok
) != '$') {
2173 isl_stream_push_token(s
, tok
);
2176 tok2
= isl_stream_next_token(s
);
2177 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2179 isl_stream_push_token(s
, tok2
);
2180 isl_stream_push_token(s
, tok
);
2184 v
= isl_token_get_val(s
->ctx
, tok2
);
2185 name
= isl_val_to_str(v
);
2187 isl_token_free(tok
);
2188 isl_token_free(tok2
);
2193 static struct isl_obj
read_list(struct isl_stream
*s
,
2194 struct isl_hash_table
*table
, struct isl_obj obj
)
2196 struct isl_list
*list
;
2198 list
= isl_list_alloc(s
->ctx
, 2);
2202 list
->obj
[1] = read_obj(s
, table
);
2204 obj
.type
= isl_obj_list
;
2206 if (!list
->obj
[1].v
)
2209 while (isl_stream_eat_if_available(s
, ',')) {
2210 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2218 obj
.type
= isl_obj_none
;
2223 static struct isl_obj
read_obj(struct isl_stream
*s
,
2224 struct isl_hash_table
*table
)
2226 struct isl_obj obj
= { isl_obj_none
, NULL
};
2228 struct isc_un_op
*op
= NULL
;
2230 obj
= read_string_if_available(s
);
2233 obj
= read_bool_if_available(s
);
2236 if (isl_stream_eat_if_available(s
, '(')) {
2237 if (isl_stream_next_token_is(s
, ')')) {
2238 obj
.type
= isl_obj_list
;
2239 obj
.v
= isl_list_alloc(s
->ctx
, 0);
2241 obj
= read_expr(s
, table
);
2242 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2243 obj
= read_list(s
, table
, obj
);
2245 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2248 op
= read_prefix_un_op_if_available(s
);
2250 return read_un_op_expr(s
, table
, op
);
2252 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2253 return check_assert(s
, table
);
2254 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2255 return read_from_file(s
);
2256 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2257 return write_to_file(s
, table
);
2258 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2259 return vertices(s
, table
);
2260 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2261 return any(s
, table
);
2262 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2263 return last(s
, table
);
2264 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2265 return schedule(s
, table
);
2266 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2267 return schedule_forest(s
, table
);
2268 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2269 return type_of(s
, table
);
2271 name
= read_ident(s
);
2273 obj
= stored_obj(s
->ctx
, table
, name
);
2275 obj
= isl_stream_read_obj(s
);
2280 if (isl_stream_eat_if_available(s
, '^'))
2281 obj
= power(s
, obj
);
2282 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2283 obj
= obj_at_index(s
, obj
);
2284 else if (is_subtype(obj
, isl_obj_union_map
) &&
2285 isl_stream_eat_if_available(s
, '(')) {
2286 obj
= convert(s
->ctx
, obj
, isl_obj_union_map
);
2287 obj
= apply(s
, obj
.v
, table
);
2288 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2289 isl_stream_eat_if_available(s
, '(')) {
2290 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2291 obj
= apply_fun(s
, obj
, table
);
2292 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2293 isl_stream_eat_if_available(s
, '(')) {
2294 obj
= convert(s
->ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2295 obj
= apply_fun(s
, obj
, table
);
2301 obj
.type
= isl_obj_none
;
2306 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2307 struct isl_obj lhs
, struct isl_obj rhs
)
2311 for (i
= 0; ; ++i
) {
2314 if (bin_ops
[i
].op
!= like
->op
)
2316 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2318 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2324 for (i
= 0; ; ++i
) {
2325 if (!named_bin_ops
[i
].name
)
2327 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2329 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2331 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2334 return &named_bin_ops
[i
].op
;
2340 static int next_is_neg_int(struct isl_stream
*s
)
2342 struct isl_token
*tok
;
2345 tok
= isl_stream_next_token(s
);
2346 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2348 v
= isl_token_get_val(s
->ctx
, tok
);
2349 ret
= isl_val_is_neg(v
);
2353 isl_stream_push_token(s
, tok
);
2358 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2359 struct isl_obj lhs
, struct isl_obj rhs
)
2363 lhs
= convert(ctx
, lhs
, op
->lhs
);
2364 rhs
= convert(ctx
, rhs
, op
->rhs
);
2365 if (op
->res
!= isl_obj_bool
)
2366 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2368 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2371 obj
.v
= isl_bool_from_int(res
);
2378 static struct isl_obj
read_expr(struct isl_stream
*s
,
2379 struct isl_hash_table
*table
)
2381 struct isl_obj obj
= { isl_obj_none
, NULL
};
2382 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2384 obj
= read_obj(s
, table
);
2386 struct isc_bin_op
*op
= NULL
;
2388 op
= read_bin_op_if_available(s
, obj
);
2392 right_obj
= read_obj(s
, table
);
2394 op
= find_matching_bin_op(op
, obj
, right_obj
);
2397 isl_die(s
->ctx
, isl_error_invalid
,
2398 "no such binary operator defined on given operands",
2401 obj
= call_bin_op(s
->ctx
, op
, obj
, right_obj
);
2404 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2405 right_obj
= read_obj(s
, table
);
2406 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2411 free_obj(right_obj
);
2413 obj
.type
= isl_obj_none
;
2418 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2419 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2421 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2422 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2424 struct isl_obj obj
= { isl_obj_none
, NULL
};
2428 struct isc_bin_op
*op
= NULL
;
2433 if (isl_stream_is_empty(s
))
2436 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2437 return source_file(s
, table
, p
);
2438 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2439 return codegen(s
, table
, p
);
2441 assign
= is_assign(s
);
2443 lhs
= isl_stream_read_ident_if_available(s
);
2444 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2446 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2451 obj
= read_expr(s
, table
);
2452 if (isl_ctx_last_error(s
->ctx
) == isl_error_abort
) {
2453 fprintf(stderr
, "Interrupted\n");
2454 isl_ctx_reset_error(s
->ctx
);
2456 if (isl_stream_eat(s
, ';'))
2460 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2461 p
= obj
.type
->print(p
, obj
.v
);
2462 p
= isl_printer_end_line(p
);
2467 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2468 static int count
= 0;
2469 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2470 lhs
= strdup(buf
+ 1);
2472 p
= isl_printer_print_str(p
, buf
);
2473 p
= isl_printer_print_str(p
, " := ");
2474 p
= obj
.type
->print(p
, obj
.v
);
2475 p
= isl_printer_end_line(p
);
2477 if (lhs
&& do_assign(s
->ctx
, table
, lhs
, obj
))
2482 isl_stream_flush_tokens(s
);
2483 isl_stream_skip_line(s
);
2489 int free_cb(void **entry
, void *user
)
2491 struct isl_named_obj
*named
= *entry
;
2493 free_obj(named
->obj
);
2500 static void register_named_ops(struct isl_stream
*s
)
2504 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2505 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2506 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2509 for (i
= 0; ; ++i
) {
2510 if (!named_un_ops
[i
].name
)
2512 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2513 named_un_ops
[i
].name
);
2514 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2517 for (i
= 0; ; ++i
) {
2518 if (!named_bin_ops
[i
].name
)
2520 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2521 named_bin_ops
[i
].name
);
2522 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2526 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2527 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2529 struct isl_token
*tok
;
2530 struct isl_stream
*s_file
;
2531 struct iscc_options
*options
;
2535 tok
= isl_stream_next_token(s
);
2536 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2537 isl_stream_error(s
, tok
, "expecting filename");
2538 isl_token_free(tok
);
2542 isl_stream_eat(s
, ';');
2544 options
= isl_ctx_peek_iscc_options(s
->ctx
);
2545 if (!options
|| !options
->io
) {
2546 isl_token_free(tok
);
2547 isl_die(s
->ctx
, isl_error_invalid
,
2548 "source operation not allowed", return p
);
2551 name
= isl_token_get_str(s
->ctx
, tok
);
2552 isl_token_free(tok
);
2553 file
= fopen(name
, "r");
2555 isl_assert(s
->ctx
, file
, return p
);
2557 s_file
= isl_stream_new_file(s
->ctx
, file
);
2563 register_named_ops(s_file
);
2565 while (!s_file
->eof
)
2566 p
= read_line(s_file
, table
, p
, 0);
2568 isl_stream_free(s_file
);
2574 int main(int argc
, char **argv
)
2576 struct isl_ctx
*ctx
;
2577 struct isl_stream
*s
;
2578 struct isl_hash_table
*table
;
2579 struct iscc_options
*options
;
2581 int tty
= isatty(0);
2583 options
= iscc_options_new_with_defaults();
2586 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2587 pet_options_set_autodetect(ctx
, 1);
2588 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2589 s
= isl_stream_new_file(ctx
, stdin
);
2591 table
= isl_hash_table_alloc(ctx
, 10);
2593 p
= isl_printer_to_file(ctx
, stdout
);
2594 p
= isl_printer_set_output_format(p
, options
->format
);
2597 register_named_ops(s
);
2599 install_signal_handler(ctx
);
2601 while (p
&& !s
->eof
) {
2602 isl_ctx_resume(ctx
);
2603 p
= read_line(s
, table
, p
, tty
);
2606 remove_signal_handler(ctx
);
2608 isl_printer_free(p
);
2609 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2610 isl_hash_table_free(ctx
, table
);