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 iscc_bool_false
= 0;
78 static int iscc_bool_true
= 1;
79 static int iscc_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
== &iscc_bool_true
)
154 return isl_printer_print_str(p
, "True");
155 else if (v
== &iscc_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 *iscc_bool_from_int(int res
)
176 return res
< 0 ? &iscc_bool_error
:
177 res
? &iscc_bool_true
: &iscc_bool_false
;
180 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
181 __isl_take isl_union_map
*map2
)
183 return isl_union_map_is_subset(map2
, map1
);
185 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
186 __isl_take isl_union_set
*set2
)
188 return isl_union_set_is_subset(set2
, set1
);
191 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
192 __isl_take isl_union_map
*map2
)
194 return isl_union_map_is_strict_subset(map2
, map1
);
196 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
197 __isl_take isl_union_set
*set2
)
199 return isl_union_set_is_strict_subset(set2
, set1
);
202 extern struct isl_obj_vtable isl_obj_list_vtable
;
203 #define isl_obj_list (&isl_obj_list_vtable)
205 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
206 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
208 enum isl_token_type op
;
214 isc_bin_test_fn test
;
217 struct isc_named_bin_op
{
219 struct isc_bin_op op
;
223 isl_union_pw_qpolynomial
*upwqp
;
224 isl_union_pw_qpolynomial
*res
;
227 static isl_stat
eval_at(__isl_take isl_point
*pnt
, void *user
)
229 struct iscc_at
*at
= (struct iscc_at
*) user
;
234 set
= isl_set_from_point(isl_point_copy(pnt
));
235 v
= isl_union_pw_qpolynomial_eval(
236 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
237 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
239 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
240 isl_union_pw_qpolynomial_from_pw_qpolynomial(
241 isl_pw_qpolynomial_alloc(set
, qp
)));
246 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
247 __isl_take isl_union_pw_qpolynomial
*upwqp
,
248 __isl_take isl_union_set
*uset
)
253 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
255 isl_union_set_foreach_point(uset
, eval_at
, &at
);
257 isl_union_pw_qpolynomial_free(upwqp
);
258 isl_union_set_free(uset
);
263 struct iscc_fold_at
{
264 isl_union_pw_qpolynomial_fold
*upwf
;
265 isl_union_pw_qpolynomial
*res
;
268 static isl_stat
eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
270 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
275 set
= isl_set_from_point(isl_point_copy(pnt
));
276 v
= isl_union_pw_qpolynomial_fold_eval(
277 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
278 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
280 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
281 isl_union_pw_qpolynomial_from_pw_qpolynomial(
282 isl_pw_qpolynomial_alloc(set
, qp
)));
287 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
288 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
289 __isl_take isl_union_set
*uset
)
291 struct iscc_fold_at at
;
294 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
296 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
298 isl_union_pw_qpolynomial_fold_free(upwf
);
299 isl_union_set_free(uset
);
304 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
305 __isl_take isl_union_pw_qpolynomial
*upwqp
,
306 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
308 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
312 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
313 __isl_take isl_union_map
*umap
,
314 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
317 struct isl_list
*list
;
320 ctx
= isl_union_map_get_ctx(umap
);
321 list
= isl_list_alloc(ctx
, 2);
325 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
326 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
328 list
->obj
[1].type
= isl_obj_bool
;
329 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
330 if (tight
< 0 || !list
->obj
[0].v
)
335 isl_union_map_free(umap
);
336 isl_union_pw_qpolynomial_fold_free(upwf
);
342 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
343 __isl_take isl_union_set
*uset
,
344 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
347 struct isl_list
*list
;
350 ctx
= isl_union_set_get_ctx(uset
);
351 list
= isl_list_alloc(ctx
, 2);
355 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
356 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
358 list
->obj
[1].type
= isl_obj_bool
;
359 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
360 if (tight
< 0 || !list
->obj
[0].v
)
365 isl_union_set_free(uset
);
366 isl_union_pw_qpolynomial_fold_free(upwf
);
372 static __isl_give isl_union_pw_qpolynomial
*isl_val_mul_union_pw_qpolynomial(
373 __isl_take isl_val
*v
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
375 return isl_union_pw_qpolynomial_scale_val(upwqp
, v
);
378 static __isl_give isl_union_pw_qpolynomial_fold
*
379 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val
*v
,
380 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
382 return isl_union_pw_qpolynomial_fold_scale_val(upwf
, v
);
385 /* Are the two strings "str1" and "str2" equal to each other?
387 static int str_eq(__isl_keep isl_str
*str1
, __isl_keep isl_str
*str2
)
392 return !strcmp(str1
->s
, str2
->s
);
395 struct isc_bin_op bin_ops
[] = {
396 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
397 (isc_bin_op_fn
) &isl_val_add
},
398 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
399 (isc_bin_op_fn
) &isl_val_sub
},
400 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
401 (isc_bin_op_fn
) &isl_val_mul
},
402 { '+', isl_obj_pw_multi_aff
, isl_obj_pw_multi_aff
,
403 isl_obj_pw_multi_aff
,
404 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
405 { '+', isl_obj_union_set
, isl_obj_union_set
,
407 (isc_bin_op_fn
) &isl_union_set_union
},
408 { '+', isl_obj_union_map
, isl_obj_union_map
,
410 (isc_bin_op_fn
) &isl_union_map_union
},
411 { '-', isl_obj_union_set
, isl_obj_union_set
,
413 (isc_bin_op_fn
) &isl_union_set_subtract
},
414 { '-', isl_obj_union_map
, isl_obj_union_map
,
416 (isc_bin_op_fn
) &isl_union_map_subtract
},
417 { '*', isl_obj_union_set
, isl_obj_union_set
,
419 (isc_bin_op_fn
) &isl_union_set_intersect
},
420 { '*', isl_obj_union_map
, isl_obj_union_map
,
422 (isc_bin_op_fn
) &isl_union_map_intersect
},
423 { '*', isl_obj_union_map
, isl_obj_union_set
,
425 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
426 { '.', isl_obj_union_map
, isl_obj_union_map
,
428 (isc_bin_op_fn
) &isl_union_map_apply_range
},
429 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
430 isl_obj_union_pw_qpolynomial
,
431 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
432 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
434 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
435 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
437 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
438 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
439 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
440 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
441 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
442 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
444 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
445 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
447 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
448 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
450 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
451 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
453 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
454 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
456 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
457 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
459 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
460 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
463 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
464 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
467 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
468 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
470 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
471 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
473 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
474 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
476 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
477 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
479 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
480 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
482 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
483 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
485 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
486 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
488 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
489 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
491 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
492 { '.', isl_obj_union_pw_qpolynomial_fold
,
493 isl_obj_union_pw_qpolynomial_fold
,
494 isl_obj_union_pw_qpolynomial_fold
,
495 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
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_add
},
499 { '+', isl_obj_union_pw_qpolynomial
,
500 isl_obj_union_pw_qpolynomial_fold
,
501 isl_obj_union_pw_qpolynomial_fold
,
502 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
503 { '+', isl_obj_union_pw_qpolynomial_fold
,
504 isl_obj_union_pw_qpolynomial
,
505 isl_obj_union_pw_qpolynomial_fold
,
506 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
507 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
508 isl_obj_union_pw_qpolynomial
,
509 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
510 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
511 isl_obj_union_pw_qpolynomial
,
512 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
513 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
514 isl_obj_union_pw_qpolynomial
,
515 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
516 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
517 isl_obj_union_pw_qpolynomial_fold
,
518 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
519 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
520 isl_obj_union_pw_qpolynomial_fold
,
521 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
522 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
523 isl_obj_union_pw_qpolynomial
,
524 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
525 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
526 isl_obj_union_pw_qpolynomial
,
527 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
528 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
529 isl_obj_union_pw_qpolynomial_fold
,
530 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
531 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
532 isl_obj_union_pw_qpolynomial
,
533 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
534 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
535 isl_obj_union_pw_qpolynomial
,
536 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
537 { '%', isl_obj_union_set
, isl_obj_union_set
,
539 (isc_bin_op_fn
) &isl_union_set_gist
},
540 { '%', isl_obj_union_map
, isl_obj_union_map
,
542 (isc_bin_op_fn
) &isl_union_map_gist
},
543 { '%', isl_obj_union_map
, isl_obj_union_set
,
545 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
546 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
547 isl_obj_union_pw_qpolynomial
,
548 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
549 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
550 isl_obj_union_pw_qpolynomial_fold
,
551 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
552 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
553 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
554 { .test
= (isc_bin_test_fn
)
555 &isl_union_pw_qpolynomial_plain_is_equal
} },
556 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
557 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
558 { .test
= (isc_bin_test_fn
)
559 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
560 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
561 (isc_bin_op_fn
) &isl_str_concat
},
562 { '=', isl_obj_str
, isl_obj_str
, isl_obj_bool
,
563 { .test
= (isc_bin_test_fn
) &str_eq
} },
567 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
568 __isl_take isl_union_map
*umap2
)
570 return isl_union_map_apply_range(umap2
, umap1
);
573 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
574 __isl_take isl_union_pw_qpolynomial
*upwqp
,
575 __isl_take isl_union_map
*umap
)
577 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
580 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
581 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
582 __isl_take isl_union_map
*umap
)
584 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
587 struct isc_named_bin_op named_bin_ops
[] = {
588 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
590 (isc_bin_op_fn
) &map_after_map
} },
591 { "after", { -1, isl_obj_union_pw_qpolynomial
,
592 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
593 (isc_bin_op_fn
) &qpolynomial_after_map
} },
594 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
595 isl_obj_union_map
, isl_obj_list
,
596 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
597 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
599 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
600 { "before", { -1, isl_obj_union_map
,
601 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
602 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
603 { "before", { -1, isl_obj_union_map
,
604 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
605 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
606 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
608 (isc_bin_op_fn
) &isl_union_set_product
} },
609 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
611 (isc_bin_op_fn
) &isl_union_map_product
} },
615 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
617 return isl_set_from_basic_set(isl_union_set_sample(uset
));
620 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
622 return isl_map_from_basic_map(isl_union_map_sample(umap
));
625 static __isl_give
struct isl_list
*union_map_power(
626 __isl_take isl_union_map
*umap
)
629 struct isl_list
*list
;
632 ctx
= isl_union_map_get_ctx(umap
);
633 list
= isl_list_alloc(ctx
, 2);
637 list
->obj
[0].type
= isl_obj_union_map
;
638 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
639 list
->obj
[1].type
= isl_obj_bool
;
640 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
641 if (exact
< 0 || !list
->obj
[0].v
)
646 isl_union_map_free(umap
);
652 /* Compute a lower or upper bound on "upwqp" depending on "type" and
653 * return a list containing two elements, the bound and a boolean
654 * indicating whether the result is tight.
656 static __isl_give
struct isl_list
*union_pw_qpolynomial_bound(
657 __isl_take isl_union_pw_qpolynomial
*upwqp
, enum isl_fold type
)
660 struct isl_list
*list
;
663 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
664 list
= isl_list_alloc(ctx
, 2);
668 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
669 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
, type
, &tight
);
670 list
->obj
[1].type
= isl_obj_bool
;
671 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
672 if (tight
< 0 || !list
->obj
[0].v
)
677 isl_union_pw_qpolynomial_free(upwqp
);
683 /* Compute a lower bound on "upwqp" and return a list containing
684 * two elements, the bound and a booleanindicating whether
685 * the result is tight.
687 static __isl_give
struct isl_list
*union_pw_qpolynomial_lower_bound(
688 __isl_take isl_union_pw_qpolynomial
*upwqp
)
690 return union_pw_qpolynomial_bound(upwqp
, isl_fold_min
);
693 /* Compute a upper bound on "upwqp" and return a list containing
694 * two elements, the bound and a booleanindicating whether
695 * the result is tight.
697 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
698 __isl_take isl_union_pw_qpolynomial
*upwqp
)
700 return union_pw_qpolynomial_bound(upwqp
, isl_fold_max
);
704 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
707 struct isl_list
*list
;
708 struct pet_scop
*scop
;
709 isl_union_map
*sched
, *may_reads
, *must_writes
, *may_writes
;
710 isl_union_set
*domain
;
711 struct iscc_options
*options
;
717 options
= isl_ctx_peek_iscc_options(ctx
);
718 if (!options
|| !options
->io
) {
720 isl_die(ctx
, isl_error_invalid
,
721 "parse_file operation not allowed", return NULL
);
724 list
= isl_list_alloc(ctx
, 5);
728 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
729 domain
= pet_scop_collect_domains(scop
);
730 sched
= scop
? isl_schedule_get_map(scop
->schedule
) : NULL
;
731 may_reads
= pet_scop_collect_may_reads(scop
);
732 may_writes
= pet_scop_collect_may_writes(scop
);
733 must_writes
= pet_scop_collect_must_writes(scop
);
736 list
->obj
[0].type
= isl_obj_union_set
;
737 list
->obj
[0].v
= domain
;
738 list
->obj
[1].type
= isl_obj_union_map
;
739 list
->obj
[1].v
= must_writes
;
740 list
->obj
[2].type
= isl_obj_union_map
;
741 list
->obj
[2].v
= may_writes
;
742 list
->obj
[3].type
= isl_obj_union_map
;
743 list
->obj
[3].v
= may_reads
;
744 list
->obj
[4].type
= isl_obj_union_map
;
745 list
->obj
[4].v
= sched
;
747 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
748 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
760 static isl_stat
add_point(__isl_take isl_point
*pnt
, void *user
)
762 isl_union_set
**scan
= (isl_union_set
**) user
;
764 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
769 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
773 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
775 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
776 isl_union_set_free(scan
);
780 isl_union_set_free(uset
);
784 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
786 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
789 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
790 __isl_take isl_union_pw_qpolynomial
*upwqp
)
792 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
795 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
796 __isl_take isl_union_pw_qpolynomial
*upwqp
)
798 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
801 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
802 __isl_take isl_union_pw_qpolynomial
*upwqp
)
804 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
807 /* Return the domain of "schedule".
809 static __isl_give isl_union_set
*schedule_domain(
810 __isl_take isl_schedule
*schedule
)
812 isl_union_set
*domain
;
814 domain
= isl_schedule_get_domain(schedule
);
815 isl_schedule_free(schedule
);
820 /* Convert "schedule" to a union map representation.
822 static __isl_give isl_union_map
*schedule_map(__isl_take isl_schedule
*schedule
)
826 map
= isl_schedule_get_map(schedule
);
827 isl_schedule_free(schedule
);
832 typedef void *(*isc_un_op_fn
)(void *arg
);
834 enum isl_token_type op
;
839 struct isc_named_un_op
{
843 struct isc_named_un_op named_un_ops
[] = {
844 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
845 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
846 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
847 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
848 {"card", { -1, isl_obj_union_set
,
849 isl_obj_union_pw_qpolynomial
,
850 (isc_un_op_fn
) &isl_union_set_card
} },
851 {"card", { -1, isl_obj_union_map
,
852 isl_obj_union_pw_qpolynomial
,
853 (isc_un_op_fn
) &isl_union_map_card
} },
854 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
855 (isc_un_op_fn
) &isl_union_set_coalesce
} },
856 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
857 (isc_un_op_fn
) &isl_union_map_coalesce
} },
858 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
859 isl_obj_union_pw_qpolynomial
,
860 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
861 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
862 isl_obj_union_pw_qpolynomial_fold
,
863 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
864 {"coefficients", { -1, isl_obj_union_set
,
866 (isc_un_op_fn
) &isl_union_set_coefficients
} },
867 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
868 (isc_un_op_fn
) &isl_union_set_solutions
} },
869 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
870 (isc_un_op_fn
) &isl_union_map_deltas
} },
871 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
872 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
873 {"dom", { -1, isl_obj_schedule
, isl_obj_union_set
,
874 (isc_un_op_fn
) &schedule_domain
} },
875 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
876 (isc_un_op_fn
) &isl_union_map_domain
} },
877 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
878 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
879 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
881 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
882 {"domain", { -1, isl_obj_schedule
, isl_obj_union_set
,
883 (isc_un_op_fn
) &schedule_domain
} },
884 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
885 (isc_un_op_fn
) &isl_union_map_domain
} },
886 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
888 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
889 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
891 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
892 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
893 (isc_un_op_fn
) &isl_union_map_domain_map
} },
894 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
895 (isc_un_op_fn
) &isl_union_map_range
} },
896 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
897 (isc_un_op_fn
) &isl_union_map_range
} },
898 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
899 (isc_un_op_fn
) &isl_union_map_range_map
} },
900 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
901 (isc_un_op_fn
) &isl_union_set_identity
} },
902 {"lattice_width", { -1, isl_obj_union_set
,
903 isl_obj_union_pw_qpolynomial
,
904 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
905 {"lb", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
906 (isc_un_op_fn
) &union_pw_qpolynomial_lower_bound
} },
907 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
908 (isc_un_op_fn
) &isl_union_map_lexmin
} },
909 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
910 (isc_un_op_fn
) &isl_union_map_lexmax
} },
911 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
912 (isc_un_op_fn
) &isl_union_set_lexmin
} },
913 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
914 (isc_un_op_fn
) &isl_union_set_lexmax
} },
915 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
916 (isc_un_op_fn
) &isl_union_set_lift
} },
917 {"map", { -1, isl_obj_schedule
, isl_obj_union_map
,
918 (isc_un_op_fn
) &schedule_map
} },
919 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
920 (isc_un_op_fn
) &isl_union_map_params
} },
921 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
922 (isc_un_op_fn
) &isl_union_set_params
} },
923 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
924 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
925 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
926 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
927 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
928 isl_obj_union_pw_qpolynomial
,
929 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
930 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
931 isl_obj_union_pw_qpolynomial
,
932 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
933 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
934 isl_obj_union_pw_qpolynomial
,
935 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
937 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
938 (isc_un_op_fn
) &parse
} },
940 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
941 (isc_un_op_fn
) &union_map_power
} },
942 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
943 (isc_un_op_fn
) &union_set_sample
} },
944 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
945 (isc_un_op_fn
) &union_map_sample
} },
946 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
947 (isc_un_op_fn
) &union_set_scan
} },
948 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
949 (isc_un_op_fn
) &union_map_scan
} },
950 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
951 isl_obj_union_pw_qpolynomial
,
952 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
953 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
954 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
955 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
956 (isc_un_op_fn
) &isl_union_set_unwrap
} },
957 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
958 (isc_un_op_fn
) &isl_union_map_wrap
} },
959 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
960 (isc_un_op_fn
) &isl_union_map_zip
} },
964 struct isl_named_obj
{
969 static void free_obj(struct isl_obj obj
)
971 obj
.type
->free(obj
.v
);
974 static int same_name(const void *entry
, const void *val
)
976 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
978 return !strcmp(named
->name
, val
);
981 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
982 char *name
, struct isl_obj obj
)
984 struct isl_hash_table_entry
*entry
;
986 struct isl_named_obj
*named
;
988 name_hash
= isl_hash_string(isl_hash_init(), name
);
989 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
994 free_obj(named
->obj
);
997 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1001 entry
->data
= named
;
1012 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1013 struct isl_hash_table
*table
, char *name
)
1015 struct isl_obj obj
= { isl_obj_none
, NULL
};
1016 struct isl_hash_table_entry
*entry
;
1019 name_hash
= isl_hash_string(isl_hash_init(), name
);
1020 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1022 struct isl_named_obj
*named
;
1023 named
= entry
->data
;
1025 } else if (isdigit(name
[0]))
1026 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1028 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1031 obj
.v
= obj
.type
->copy(obj
.v
);
1035 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1037 if (obj
.type
== super
)
1039 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1041 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1043 if (obj
.type
== isl_obj_schedule
&& super
== isl_obj_union_map
)
1045 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
1046 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
1047 int is_set
= isl_space_is_set(space
);
1048 isl_space_free(space
);
1051 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1052 super
== isl_obj_union_pw_qpolynomial
)
1054 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1055 super
== isl_obj_union_pw_qpolynomial_fold
)
1057 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1059 if (obj
.type
== isl_obj_list
) {
1060 struct isl_list
*list
= obj
.v
;
1061 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1062 return is_subtype(list
->obj
[0], super
);
1064 if (super
== isl_obj_str
)
1069 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1071 struct isl_list
*list
= obj
.v
;
1074 obj
.v
= obj
.type
->copy(obj
.v
);
1076 isl_list_free(list
);
1081 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1084 if (obj
.type
== type
)
1086 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1087 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1088 obj
.type
= isl_obj_union_set
;
1089 obj
.v
= isl_union_set_from_set(set
);
1092 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1093 obj
.type
= isl_obj_union_map
;
1094 obj
.v
= isl_union_map_from_map(obj
.v
);
1097 if (obj
.type
== isl_obj_schedule
&& type
== isl_obj_union_map
) {
1098 obj
.type
= isl_obj_union_map
;
1099 obj
.v
= schedule_map(obj
.v
);
1102 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1103 obj
.type
= isl_obj_union_set
;
1104 obj
.v
= isl_union_set_from_set(obj
.v
);
1107 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1108 type
== isl_obj_union_pw_qpolynomial
) {
1109 obj
.type
= isl_obj_union_pw_qpolynomial
;
1110 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1113 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1114 type
== isl_obj_union_pw_qpolynomial_fold
) {
1115 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1116 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1119 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1120 if (type
== isl_obj_union_map
) {
1121 obj
.type
= isl_obj_union_map
;
1124 if (type
== isl_obj_union_pw_qpolynomial
) {
1125 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1126 isl_union_set_free(obj
.v
);
1127 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1128 obj
.type
= isl_obj_union_pw_qpolynomial
;
1131 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1132 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1133 isl_union_set_free(obj
.v
);
1134 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1136 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1140 if (obj
.type
== isl_obj_list
) {
1141 struct isl_list
*list
= obj
.v
;
1142 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1143 return convert(ctx
, obj_at(obj
, 0), type
);
1145 if (type
== isl_obj_str
) {
1150 p
= isl_printer_to_str(ctx
);
1153 p
= obj
.type
->print(p
, obj
.v
);
1154 s
= isl_printer_get_str(p
);
1155 isl_printer_free(p
);
1157 str
= isl_str_from_string(ctx
, s
);
1162 obj
.type
= isl_obj_str
;
1168 obj
.type
= isl_obj_none
;
1173 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1177 struct isl_token
*tok
;
1179 tok
= isl_stream_next_token(s
);
1183 for (i
= 0; ; ++i
) {
1186 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1188 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1191 isl_token_free(tok
);
1195 for (i
= 0; ; ++i
) {
1196 if (!named_bin_ops
[i
].name
)
1198 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1200 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1203 isl_token_free(tok
);
1204 return &named_bin_ops
[i
].op
;
1207 isl_stream_push_token(s
, tok
);
1212 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1215 struct isl_token
*tok
;
1217 tok
= isl_stream_next_token(s
);
1221 for (i
= 0; ; ++i
) {
1222 if (!named_un_ops
[i
].name
)
1224 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1227 isl_token_free(tok
);
1228 return &named_un_ops
[i
].op
;
1231 isl_stream_push_token(s
, tok
);
1236 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1241 for (i
= 0; ; ++i
) {
1242 if (!named_un_ops
[i
].name
)
1244 if (named_un_ops
[i
].op
.op
!= like
->op
)
1246 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1249 return &named_un_ops
[i
].op
;
1255 static int is_assign(struct isl_stream
*s
)
1257 struct isl_token
*tok
;
1258 struct isl_token
*tok2
;
1261 tok
= isl_stream_next_token(s
);
1264 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1265 isl_stream_push_token(s
, tok
);
1269 tok2
= isl_stream_next_token(s
);
1271 isl_stream_push_token(s
, tok
);
1274 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1275 isl_stream_push_token(s
, tok2
);
1276 isl_stream_push_token(s
, tok
);
1281 static struct isl_obj
read_obj(struct isl_stream
*s
,
1282 struct isl_hash_table
*table
);
1283 static struct isl_obj
read_expr(struct isl_stream
*s
,
1284 struct isl_hash_table
*table
);
1286 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1287 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1290 struct isl_obj obj
= { isl_obj_none
, NULL
};
1292 obj
= read_obj(s
, table
);
1296 op
= find_matching_un_op(op
, obj
);
1298 ctx
= isl_stream_get_ctx(s
);
1300 isl_die(ctx
, isl_error_invalid
,
1301 "no such unary operator defined on given operand",
1304 obj
= convert(ctx
, obj
, op
->arg
);
1305 obj
.v
= op
->fn(obj
.v
);
1311 obj
.type
= isl_obj_none
;
1316 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1318 struct isl_list
*list
;
1321 if (obj
.type
!= isl_obj_union_map
)
1322 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1323 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1324 list
= isl_list_alloc(ctx
, 2);
1328 list
->obj
[0].type
= isl_obj_union_map
;
1329 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1330 list
->obj
[1].type
= isl_obj_bool
;
1331 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
1333 obj
.type
= isl_obj_list
;
1334 if (exact
< 0 || !list
->obj
[0].v
)
1340 obj
.type
= isl_obj_none
;
1345 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1347 struct isl_list
*list
= obj
.v
;
1348 struct isl_token
*tok
;
1353 tok
= isl_stream_next_token(s
);
1354 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1355 isl_stream_error(s
, tok
, "expecting index");
1357 isl_stream_push_token(s
, tok
);
1360 ctx
= isl_stream_get_ctx(s
);
1361 v
= isl_token_get_val(ctx
, tok
);
1362 i
= isl_val_get_num_si(v
);
1364 isl_token_free(tok
);
1365 isl_assert(ctx
, i
< list
->n
, goto error
);
1366 if (isl_stream_eat(s
, ']'))
1369 return obj_at(obj
, i
);
1372 obj
.type
= isl_obj_none
;
1377 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1378 struct isl_hash_table
*table
)
1383 obj
= read_expr(s
, table
);
1384 ctx
= isl_stream_get_ctx(s
);
1385 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1386 is_subtype(obj
, isl_obj_union_map
), goto error
);
1388 if (obj
.type
== isl_obj_list
) {
1389 struct isl_list
*list
= obj
.v
;
1390 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1391 obj
= obj_at(obj
, 0);
1393 if (obj
.type
== isl_obj_set
)
1394 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1395 else if (obj
.type
== isl_obj_map
)
1396 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1397 if (obj
.type
== isl_obj_union_set
) {
1398 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1400 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1404 if (isl_stream_eat(s
, ')'))
1409 isl_union_map_free(umap
);
1412 obj
.type
= isl_obj_none
;
1417 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1418 __isl_take isl_union_set
*uset
)
1420 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1421 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1423 obj
.type
= isl_obj_list
;
1424 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1429 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1430 __isl_take isl_union_map
*umap
)
1432 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1433 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1435 obj
.type
= isl_obj_list
;
1436 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1441 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1442 struct isl_obj obj
, struct isl_hash_table
*table
)
1447 arg
= read_expr(s
, table
);
1448 ctx
= isl_stream_get_ctx(s
);
1449 if (!is_subtype(arg
, isl_obj_union_map
) &&
1450 !is_subtype(arg
, isl_obj_union_set
))
1451 isl_die(ctx
, isl_error_invalid
,
1452 "expecting set of map argument", goto error
);
1454 if (arg
.type
== isl_obj_list
) {
1455 struct isl_list
*list
= arg
.v
;
1456 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1457 arg
= obj_at(arg
, 0);
1459 if (arg
.type
== isl_obj_set
)
1460 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1461 else if (arg
.type
== isl_obj_map
)
1462 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1463 if (arg
.type
== isl_obj_union_set
)
1464 obj
= apply_fun_set(obj
, arg
.v
);
1466 obj
= apply_fun_map(obj
, arg
.v
);
1470 if (isl_stream_eat(s
, ')'))
1478 obj
.type
= isl_obj_none
;
1483 struct add_vertex_data
{
1484 struct isl_list
*list
;
1488 static isl_stat
add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1490 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1494 ma
= isl_vertex_get_expr(vertex
);
1495 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1497 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1498 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1501 isl_vertex_free(vertex
);
1506 static isl_stat
set_vertices(__isl_take isl_set
*set
, void *user
)
1509 isl_basic_set
*hull
;
1510 isl_vertices
*vertices
= NULL
;
1511 struct isl_list
*list
= NULL
;
1513 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1515 set
= isl_set_remove_divs(set
);
1516 hull
= isl_set_convex_hull(set
);
1517 vertices
= isl_basic_set_compute_vertices(hull
);
1518 isl_basic_set_free(hull
);
1522 ctx
= isl_vertices_get_ctx(vertices
);
1523 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1528 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1530 data
->list
= isl_list_concat(list
, data
->list
);
1532 isl_vertices_free(vertices
);
1537 isl_vertices_free(vertices
);
1538 return isl_stat_error
;
1541 static struct isl_obj
vertices(struct isl_stream
*s
,
1542 struct isl_hash_table
*table
)
1546 struct isl_list
*list
= NULL
;
1547 isl_union_set
*uset
= NULL
;
1548 struct add_vertex_data data
= { NULL
};
1550 obj
= read_expr(s
, table
);
1551 ctx
= isl_stream_get_ctx(s
);
1552 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1553 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1557 list
= isl_list_alloc(ctx
, 0);
1563 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1566 isl_union_set_free(uset
);
1568 obj
.type
= isl_obj_list
;
1573 isl_union_set_free(uset
);
1574 isl_list_free(data
.list
);
1576 obj
.type
= isl_obj_none
;
1581 static struct isl_obj
type_of(struct isl_stream
*s
,
1582 struct isl_hash_table
*table
)
1586 const char *type
= "unknown";
1588 obj
= read_expr(s
, table
);
1590 if (obj
.type
== isl_obj_map
||
1591 obj
.type
== isl_obj_union_map
)
1593 if (obj
.type
== isl_obj_set
||
1594 obj
.type
== isl_obj_union_set
)
1596 if (obj
.type
== isl_obj_pw_multi_aff
)
1597 type
= "piecewise multi-quasiaffine expression";
1598 if (obj
.type
== isl_obj_pw_qpolynomial
||
1599 obj
.type
== isl_obj_union_pw_qpolynomial
)
1600 type
= "piecewise quasipolynomial";
1601 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1602 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1603 type
= "piecewise quasipolynomial fold";
1604 if (obj
.type
== isl_obj_list
)
1606 if (obj
.type
== isl_obj_bool
)
1608 if (obj
.type
== isl_obj_str
)
1610 if (obj
.type
== isl_obj_val
)
1612 if (obj
.type
== isl_obj_schedule
)
1616 obj
.type
= isl_obj_str
;
1617 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1622 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1623 struct isl_hash_table
*table
)
1628 obj
= read_obj(s
, table
);
1629 ctx
= isl_stream_get_ctx(s
);
1630 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1631 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1638 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1639 struct isl_hash_table
*table
)
1644 obj
= read_obj(s
, table
);
1645 ctx
= isl_stream_get_ctx(s
);
1646 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1647 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1654 /* Read a schedule in the form of either a schedule (tree) or a union map
1655 * from "s" and store the schedule in "access".
1657 static __isl_give isl_union_access_info
*access_info_set_schedule(
1658 __isl_take isl_union_access_info
*access
, struct isl_stream
*s
,
1659 struct isl_hash_table
*table
)
1664 obj
= read_obj(s
, table
);
1665 if (obj
.type
== isl_obj_schedule
)
1666 return isl_union_access_info_set_schedule(access
, obj
.v
);
1667 ctx
= isl_stream_get_ctx(s
);
1668 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1670 return isl_union_access_info_set_schedule_map(access
, obj
.v
);
1673 static struct isl_obj
last_any(struct isl_stream
*s
,
1674 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1675 __isl_take isl_union_map
*may_source
)
1677 struct isl_obj obj
= { isl_obj_none
, NULL
};
1678 isl_union_access_info
*access
;
1679 isl_union_flow
*flow
;
1680 isl_union_map
*sink
= NULL
;
1681 isl_union_map
*may_dep
;
1683 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1686 sink
= read_map(s
, table
);
1690 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1693 access
= isl_union_access_info_from_sink(sink
);
1694 access
= isl_union_access_info_set_must_source(access
, must_source
);
1695 access
= isl_union_access_info_set_may_source(access
, may_source
);
1696 access
= access_info_set_schedule(access
, s
, table
);
1697 flow
= isl_union_access_info_compute_flow(access
);
1698 may_dep
= isl_union_flow_get_may_dependence(flow
);
1699 isl_union_flow_free(flow
);
1704 obj
.type
= isl_obj_union_map
;
1709 isl_union_map_free(may_source
);
1710 isl_union_map_free(must_source
);
1711 isl_union_map_free(sink
);
1713 obj
.type
= isl_obj_none
;
1718 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1720 struct isl_obj obj
= { isl_obj_none
, NULL
};
1721 isl_union_access_info
*access
;
1722 isl_union_flow
*flow
;
1723 isl_union_map
*may_source
= NULL
;
1724 isl_union_map
*sink
= NULL
;
1725 isl_union_map
*may_dep
;
1727 may_source
= read_map(s
, table
);
1731 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1732 isl_union_map
*must_source
;
1733 must_source
= read_map(s
, table
);
1736 return last_any(s
, table
, must_source
, may_source
);
1739 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1742 sink
= read_map(s
, table
);
1746 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1749 access
= isl_union_access_info_from_sink(sink
);
1750 access
= isl_union_access_info_set_may_source(access
, may_source
);
1751 access
= access_info_set_schedule(access
, s
, table
);
1752 flow
= isl_union_access_info_compute_flow(access
);
1753 may_dep
= isl_union_flow_get_may_dependence(flow
);
1754 isl_union_flow_free(flow
);
1759 obj
.type
= isl_obj_union_map
;
1764 isl_union_map_free(may_source
);
1765 isl_union_map_free(sink
);
1767 obj
.type
= isl_obj_none
;
1772 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1774 struct isl_obj obj
= { isl_obj_none
, NULL
};
1775 struct isl_list
*list
= NULL
;
1776 isl_union_access_info
*access
;
1777 isl_union_flow
*flow
;
1778 isl_union_map
*must_source
= NULL
;
1779 isl_union_map
*sink
= NULL
;
1780 isl_union_map
*must_dep
;
1781 isl_union_map
*must_no_source
;
1783 must_source
= read_map(s
, table
);
1787 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1788 isl_union_map
*may_source
;
1789 may_source
= read_map(s
, table
);
1792 return last_any(s
, table
, must_source
, may_source
);
1795 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1799 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1802 sink
= read_map(s
, table
);
1806 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1809 access
= isl_union_access_info_from_sink(sink
);
1810 access
= isl_union_access_info_set_must_source(access
, must_source
);
1811 access
= access_info_set_schedule(access
, s
, table
);
1812 flow
= isl_union_access_info_compute_flow(access
);
1813 must_dep
= isl_union_flow_get_must_dependence(flow
);
1814 must_no_source
= isl_union_flow_get_must_no_source(flow
);
1815 isl_union_flow_free(flow
);
1817 list
->obj
[0].type
= isl_obj_union_map
;
1818 list
->obj
[0].v
= must_dep
;
1819 list
->obj
[1].type
= isl_obj_union_map
;
1820 list
->obj
[1].v
= must_no_source
;
1822 if (!must_dep
|| !must_no_source
) {
1823 isl_list_free(list
);
1828 obj
.type
= isl_obj_list
;
1832 isl_list_free(list
);
1833 isl_union_map_free(must_source
);
1834 isl_union_map_free(sink
);
1836 obj
.type
= isl_obj_none
;
1841 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1842 struct isl_hash_table
*table
)
1844 isl_union_set
*domain
;
1845 isl_union_map
*validity
;
1846 isl_union_map
*proximity
;
1848 domain
= read_set(s
, table
);
1852 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1853 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1856 isl_union_map
*umap
;
1857 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1858 umap
= read_map(s
, table
);
1859 validity
= isl_union_map_union(validity
, umap
);
1860 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1861 umap
= read_map(s
, table
);
1862 proximity
= isl_union_map_union(proximity
, umap
);
1867 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1870 static struct isl_obj
schedule(struct isl_stream
*s
,
1871 struct isl_hash_table
*table
)
1873 struct isl_obj obj
= { isl_obj_none
, NULL
};
1874 isl_schedule
*schedule
;
1876 schedule
= get_schedule(s
, table
);
1879 obj
.type
= isl_obj_schedule
;
1884 /* Read a schedule for code generation in the form of either
1885 * a schedule tree or a union map.
1886 * If the input is a set rather than a map, then we construct
1887 * an identity union map schedule on the given set.
1889 static struct isl_obj
get_codegen_schedule(struct isl_stream
*s
,
1890 struct isl_hash_table
*table
)
1895 obj
= read_obj(s
, table
);
1896 ctx
= isl_stream_get_ctx(s
);
1898 if (obj
.type
== isl_obj_schedule
)
1900 if (is_subtype(obj
, isl_obj_union_set
)) {
1901 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1902 obj
.v
= isl_union_set_identity(obj
.v
);
1903 obj
.type
= isl_obj_union_map
;
1905 if (is_subtype(obj
, isl_obj_union_map
))
1906 return convert(ctx
, obj
, isl_obj_union_map
);
1910 obj
.type
= isl_obj_none
;
1911 isl_die(ctx
, isl_error_invalid
, "expecting schedule, set or map",
1915 /* Generate an AST for the given schedule and options and return the AST.
1917 static __isl_give isl_ast_node
*get_ast_from_union_map(
1918 __isl_take isl_union_map
*schedule
, __isl_take isl_union_map
*options
)
1922 isl_ast_build
*build
;
1925 space
= isl_union_map_get_space(schedule
);
1926 context
= isl_set_universe(isl_space_params(space
));
1928 build
= isl_ast_build_from_context(context
);
1929 build
= isl_ast_build_set_options(build
, options
);
1930 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1931 isl_ast_build_free(build
);
1936 /* Generate an AST for the given schedule and return the AST.
1938 static __isl_give isl_ast_node
*get_ast_from_schedule(
1939 __isl_take isl_schedule
*schedule
)
1941 isl_ast_build
*build
;
1944 build
= isl_ast_build_alloc(isl_schedule_get_ctx(schedule
));
1945 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
1946 isl_ast_build_free(build
);
1951 /* Print the AST "tree" on the printer "p".
1953 static __isl_give isl_printer
*print_ast(__isl_take isl_printer
*p
,
1954 __isl_take isl_ast_node
*tree
)
1958 format
= isl_printer_get_output_format(p
);
1959 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1960 p
= isl_printer_print_ast_node(p
, tree
);
1961 p
= isl_printer_set_output_format(p
, format
);
1963 isl_ast_node_free(tree
);
1968 /* Perform the codegen operation.
1969 * In particular, read a schedule, check if the user has specified any options
1970 * and then generate an AST from the schedule (and options) and print it.
1971 * In case the schedule is specified as a schedule tree, the AST generation
1972 * options are embedded in the schedule, so they are not read in separately.
1974 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1975 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1980 obj
= get_codegen_schedule(s
, table
);
1984 if (obj
.type
== isl_obj_schedule
) {
1985 isl_schedule
*schedule
= obj
.v
;
1987 tree
= get_ast_from_schedule(schedule
);
1989 isl_union_map
*schedule
= obj
.v
;
1990 isl_union_map
*options
;
1992 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1993 options
= read_map(s
, table
);
1995 options
= isl_union_map_empty(
1996 isl_union_map_get_space(schedule
));
1998 tree
= get_ast_from_union_map(schedule
, options
);
2001 p
= print_ast(p
, tree
);
2003 isl_stream_eat(s
, ';');
2008 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
2010 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
2012 struct isl_obj obj
= { isl_obj_none
, NULL
};
2013 isl_ctx
*ctx
= isl_band_get_ctx(band
);
2014 struct isl_list
*list
;
2016 list
= isl_list_alloc(ctx
, 2);
2021 obj
.type
= isl_obj_list
;
2023 list
->obj
[0].type
= isl_obj_union_map
;
2024 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
2026 if (isl_band_has_children(band
)) {
2027 isl_band_list
*children
;
2029 children
= isl_band_get_children(band
);
2030 list
->obj
[1] = band_list_to_obj_list(children
);
2032 list
->obj
[1].type
= isl_obj_list
;
2033 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
2036 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
2039 isl_band_free(band
);
2043 isl_band_free(band
);
2045 obj
.type
= isl_obj_none
;
2050 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
2052 struct isl_obj obj
= { isl_obj_none
, NULL
};
2053 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
2054 struct isl_list
*list
;
2057 n
= isl_band_list_n_band(bands
);
2058 list
= isl_list_alloc(ctx
, n
);
2063 obj
.type
= isl_obj_list
;
2065 for (i
= 0; i
< n
; ++i
) {
2068 band
= isl_band_list_get_band(bands
, i
);
2069 list
->obj
[i
] = band_to_obj_list(band
);
2070 if (!list
->obj
[i
].v
)
2074 isl_band_list_free(bands
);
2078 isl_band_list_free(bands
);
2080 obj
.type
= isl_obj_none
;
2085 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
2086 struct isl_hash_table
*table
)
2088 struct isl_obj obj
= { isl_obj_none
, NULL
};
2089 isl_schedule
*schedule
;
2090 isl_band_list
*roots
;
2092 schedule
= get_schedule(s
, table
);
2096 roots
= isl_schedule_get_band_forest(schedule
);
2097 isl_schedule_free(schedule
);
2099 return band_list_to_obj_list(roots
);
2102 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
2104 struct isl_token
*tok
;
2108 ctx
= isl_stream_get_ctx(s
);
2109 if (isl_stream_eat_if_available(s
, '+'))
2110 return transitive_closure(ctx
, obj
);
2112 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
2113 if (obj
.type
!= isl_obj_union_map
)
2114 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2116 tok
= isl_stream_next_token(s
);
2117 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
2118 isl_stream_error(s
, tok
, "expecting integer exponent");
2120 isl_stream_push_token(s
, tok
);
2124 v
= isl_token_get_val(ctx
, tok
);
2125 if (isl_val_is_zero(v
)) {
2126 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2129 isl_stream_push_token(s
, tok
);
2133 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2134 isl_token_free(tok
);
2141 obj
.type
= isl_obj_none
;
2146 static struct isl_obj
check_assert(struct isl_stream
*s
,
2147 struct isl_hash_table
*table
)
2152 obj
= read_expr(s
, table
);
2153 ctx
= isl_stream_get_ctx(s
);
2154 if (obj
.type
!= isl_obj_bool
)
2155 isl_die(ctx
, isl_error_invalid
,
2156 "expecting boolean expression", goto error
);
2157 if (obj
.v
!= &iscc_bool_true
)
2158 isl_die(ctx
, isl_error_unknown
,
2159 "assertion failed", abort());
2162 obj
.type
= isl_obj_none
;
2167 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2171 struct isl_token
*tok
;
2172 struct isl_stream
*s_file
;
2173 struct iscc_options
*options
;
2177 tok
= isl_stream_next_token(s
);
2178 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2179 isl_stream_error(s
, tok
, "expecting filename");
2180 isl_token_free(tok
);
2184 ctx
= isl_stream_get_ctx(s
);
2185 options
= isl_ctx_peek_iscc_options(ctx
);
2186 if (!options
|| !options
->io
) {
2187 isl_token_free(tok
);
2188 isl_die(ctx
, isl_error_invalid
,
2189 "read operation not allowed", goto error
);
2192 name
= isl_token_get_str(ctx
, tok
);
2193 isl_token_free(tok
);
2194 file
= fopen(name
, "r");
2196 isl_assert(ctx
, file
, goto error
);
2198 s_file
= isl_stream_new_file(ctx
, file
);
2204 obj
= isl_stream_read_obj(s_file
);
2206 isl_stream_free(s_file
);
2211 obj
.type
= isl_obj_none
;
2216 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2217 struct isl_hash_table
*table
)
2220 struct isl_token
*tok
;
2221 struct isl_stream
*s_file
;
2222 struct iscc_options
*options
;
2228 tok
= isl_stream_next_token(s
);
2229 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2230 isl_stream_error(s
, tok
, "expecting filename");
2231 isl_token_free(tok
);
2235 obj
= read_expr(s
, table
);
2237 ctx
= isl_stream_get_ctx(s
);
2238 options
= isl_ctx_peek_iscc_options(ctx
);
2239 if (!options
|| !options
->io
) {
2240 isl_token_free(tok
);
2241 isl_die(ctx
, isl_error_invalid
,
2242 "write operation not allowed", goto error
);
2245 name
= isl_token_get_str(ctx
, tok
);
2246 isl_token_free(tok
);
2247 file
= fopen(name
, "w");
2250 isl_die(ctx
, isl_error_unknown
,
2251 "could not open file for writing", goto error
);
2253 p
= isl_printer_to_file(ctx
, file
);
2254 p
= isl_printer_set_output_format(p
, options
->format
);
2255 p
= obj
.type
->print(p
, obj
.v
);
2256 p
= isl_printer_end_line(p
);
2257 isl_printer_free(p
);
2262 obj
.type
= isl_obj_none
;
2267 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2269 struct isl_token
*tok
;
2270 struct isl_obj obj
= { isl_obj_none
, NULL
};
2272 tok
= isl_stream_next_token(s
);
2275 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2277 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2280 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2281 isl_token_free(tok
);
2283 obj
.type
= isl_obj_str
;
2285 isl_stream_push_token(s
, tok
);
2288 isl_token_free(tok
);
2292 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2294 struct isl_token
*tok
;
2295 struct isl_obj obj
= { isl_obj_none
, NULL
};
2298 tok
= isl_stream_next_token(s
);
2301 type
= isl_token_get_type(tok
);
2302 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2303 int is_true
= type
== ISL_TOKEN_TRUE
;
2304 isl_token_free(tok
);
2305 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2306 obj
.type
= isl_obj_bool
;
2308 isl_stream_push_token(s
, tok
);
2312 static __isl_give
char *read_ident(struct isl_stream
*s
)
2316 struct isl_token
*tok
, *tok2
;
2318 name
= isl_stream_read_ident_if_available(s
);
2322 tok
= isl_stream_next_token(s
);
2325 if (isl_token_get_type(tok
) != '$') {
2326 isl_stream_push_token(s
, tok
);
2329 tok2
= isl_stream_next_token(s
);
2330 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2332 isl_stream_push_token(s
, tok2
);
2333 isl_stream_push_token(s
, tok
);
2337 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2338 name
= isl_val_to_str(v
);
2340 isl_token_free(tok
);
2341 isl_token_free(tok2
);
2346 static struct isl_obj
read_list(struct isl_stream
*s
,
2347 struct isl_hash_table
*table
, struct isl_obj obj
)
2349 struct isl_list
*list
;
2351 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2355 list
->obj
[1] = read_obj(s
, table
);
2357 obj
.type
= isl_obj_list
;
2359 if (!list
->obj
[1].v
)
2362 while (isl_stream_eat_if_available(s
, ',')) {
2363 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2371 obj
.type
= isl_obj_none
;
2376 static struct isl_obj
read_obj(struct isl_stream
*s
,
2377 struct isl_hash_table
*table
)
2380 struct isl_obj obj
= { isl_obj_none
, NULL
};
2382 struct isc_un_op
*op
= NULL
;
2384 obj
= read_string_if_available(s
);
2387 obj
= read_bool_if_available(s
);
2390 ctx
= isl_stream_get_ctx(s
);
2391 if (isl_stream_eat_if_available(s
, '(')) {
2392 if (isl_stream_next_token_is(s
, ')')) {
2393 obj
.type
= isl_obj_list
;
2394 obj
.v
= isl_list_alloc(ctx
, 0);
2396 obj
= read_expr(s
, table
);
2397 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2398 obj
= read_list(s
, table
, obj
);
2400 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2403 op
= read_prefix_un_op_if_available(s
);
2405 return read_un_op_expr(s
, table
, op
);
2407 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2408 return check_assert(s
, table
);
2409 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2410 return read_from_file(s
);
2411 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2412 return write_to_file(s
, table
);
2413 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2414 return vertices(s
, table
);
2415 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2416 return any(s
, table
);
2417 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2418 return last(s
, table
);
2419 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2420 return schedule(s
, table
);
2421 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2422 return schedule_forest(s
, table
);
2423 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2424 return type_of(s
, table
);
2426 name
= read_ident(s
);
2428 obj
= stored_obj(ctx
, table
, name
);
2430 obj
= isl_stream_read_obj(s
);
2435 if (isl_stream_eat_if_available(s
, '^'))
2436 obj
= power(s
, obj
);
2437 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2438 obj
= obj_at_index(s
, obj
);
2439 else if (is_subtype(obj
, isl_obj_union_map
) &&
2440 isl_stream_eat_if_available(s
, '(')) {
2441 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2442 obj
= apply(s
, obj
.v
, table
);
2443 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2444 isl_stream_eat_if_available(s
, '(')) {
2445 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2446 obj
= apply_fun(s
, obj
, table
);
2447 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2448 isl_stream_eat_if_available(s
, '(')) {
2449 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2450 obj
= apply_fun(s
, obj
, table
);
2456 obj
.type
= isl_obj_none
;
2461 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2462 struct isl_obj lhs
, struct isl_obj rhs
)
2466 for (i
= 0; ; ++i
) {
2469 if (bin_ops
[i
].op
!= like
->op
)
2471 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2473 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2479 for (i
= 0; ; ++i
) {
2480 if (!named_bin_ops
[i
].name
)
2482 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2484 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2486 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2489 return &named_bin_ops
[i
].op
;
2495 static int next_is_neg_int(struct isl_stream
*s
)
2497 struct isl_token
*tok
;
2500 tok
= isl_stream_next_token(s
);
2501 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2503 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2504 ret
= isl_val_is_neg(v
);
2508 isl_stream_push_token(s
, tok
);
2513 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2514 struct isl_obj lhs
, struct isl_obj rhs
)
2518 lhs
= convert(ctx
, lhs
, op
->lhs
);
2519 rhs
= convert(ctx
, rhs
, op
->rhs
);
2520 if (op
->res
!= isl_obj_bool
)
2521 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2523 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2526 obj
.v
= iscc_bool_from_int(res
);
2533 static struct isl_obj
read_expr(struct isl_stream
*s
,
2534 struct isl_hash_table
*table
)
2537 struct isl_obj obj
= { isl_obj_none
, NULL
};
2538 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2540 obj
= read_obj(s
, table
);
2541 ctx
= isl_stream_get_ctx(s
);
2543 struct isc_bin_op
*op
= NULL
;
2545 op
= read_bin_op_if_available(s
, obj
);
2549 right_obj
= read_obj(s
, table
);
2551 op
= find_matching_bin_op(op
, obj
, right_obj
);
2554 isl_die(ctx
, isl_error_invalid
,
2555 "no such binary operator defined on given operands",
2558 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2561 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2562 right_obj
= read_obj(s
, table
);
2563 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2568 free_obj(right_obj
);
2570 obj
.type
= isl_obj_none
;
2575 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2576 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2578 /* Print "obj" to the printer "p".
2579 * If the object is a schedule, then print it in block format.
2581 static __isl_give isl_printer
*print_obj(__isl_take isl_printer
*p
,
2584 if (obj
.type
!= isl_obj_schedule
)
2585 return obj
.type
->print(p
, obj
.v
);
2587 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
2588 p
= obj
.type
->print(p
, obj
.v
);
2589 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2594 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2595 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2598 struct isl_obj obj
= { isl_obj_none
, NULL
};
2602 struct isc_bin_op
*op
= NULL
;
2607 if (isl_stream_is_empty(s
))
2610 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2611 return source_file(s
, table
, p
);
2612 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2613 return codegen(s
, table
, p
);
2615 assign
= is_assign(s
);
2617 lhs
= isl_stream_read_ident_if_available(s
);
2618 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2620 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2625 obj
= read_expr(s
, table
);
2626 ctx
= isl_stream_get_ctx(s
);
2627 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2628 fprintf(stderr
, "Interrupted\n");
2629 isl_ctx_reset_error(ctx
);
2631 if (isl_stream_eat(s
, ';'))
2635 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2636 p
= print_obj(p
, obj
);
2637 p
= isl_printer_end_line(p
);
2642 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2643 static int count
= 0;
2644 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2645 lhs
= strdup(buf
+ 1);
2647 p
= isl_printer_print_str(p
, buf
);
2648 p
= isl_printer_print_str(p
, " := ");
2649 p
= obj
.type
->print(p
, obj
.v
);
2650 p
= isl_printer_end_line(p
);
2652 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2657 isl_stream_flush_tokens(s
);
2658 isl_stream_skip_line(s
);
2664 static isl_stat
free_cb(void **entry
, void *user
)
2666 struct isl_named_obj
*named
= *entry
;
2668 free_obj(named
->obj
);
2675 static void register_named_ops(struct isl_stream
*s
)
2679 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2680 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2681 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2684 for (i
= 0; ; ++i
) {
2685 if (!named_un_ops
[i
].name
)
2687 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2688 named_un_ops
[i
].name
);
2689 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2692 for (i
= 0; ; ++i
) {
2693 if (!named_bin_ops
[i
].name
)
2695 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2696 named_bin_ops
[i
].name
);
2697 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2701 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2702 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2705 struct isl_token
*tok
;
2706 struct isl_stream
*s_file
;
2707 struct iscc_options
*options
;
2711 tok
= isl_stream_next_token(s
);
2712 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2713 isl_stream_error(s
, tok
, "expecting filename");
2714 isl_token_free(tok
);
2718 isl_stream_eat(s
, ';');
2720 ctx
= isl_stream_get_ctx(s
);
2721 options
= isl_ctx_peek_iscc_options(ctx
);
2722 if (!options
|| !options
->io
) {
2723 isl_token_free(tok
);
2724 isl_die(ctx
, isl_error_invalid
,
2725 "source operation not allowed", return p
);
2728 name
= isl_token_get_str(ctx
, tok
);
2729 isl_token_free(tok
);
2730 file
= fopen(name
, "r");
2732 isl_assert(ctx
, file
, return p
);
2734 s_file
= isl_stream_new_file(ctx
, file
);
2740 register_named_ops(s_file
);
2742 while (!isl_stream_is_empty(s_file
))
2743 p
= read_line(s_file
, table
, p
, 0);
2745 isl_stream_free(s_file
);
2751 int main(int argc
, char **argv
)
2753 struct isl_ctx
*ctx
;
2754 struct isl_stream
*s
;
2755 struct isl_hash_table
*table
;
2756 struct iscc_options
*options
;
2758 int tty
= isatty(0);
2760 options
= iscc_options_new_with_defaults();
2763 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2764 pet_options_set_autodetect(ctx
, 1);
2765 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2766 s
= isl_stream_new_file(ctx
, stdin
);
2768 table
= isl_hash_table_alloc(ctx
, 10);
2770 p
= isl_printer_to_file(ctx
, stdout
);
2771 p
= isl_printer_set_output_format(p
, options
->format
);
2774 register_named_ops(s
);
2776 install_signal_handler(ctx
);
2778 while (p
&& !isl_stream_is_empty(s
)) {
2779 isl_ctx_resume(ctx
);
2780 p
= read_line(s
, table
, p
, tty
);
2783 remove_signal_handler(ctx
);
2785 isl_printer_free(p
);
2786 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2787 isl_hash_table_free(ctx
, table
);