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 typedef void *(*isc_un_op_fn
)(void *arg
);
809 enum isl_token_type op
;
814 struct isc_named_un_op
{
818 struct isc_named_un_op named_un_ops
[] = {
819 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
820 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
821 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
822 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
823 {"card", { -1, isl_obj_union_set
,
824 isl_obj_union_pw_qpolynomial
,
825 (isc_un_op_fn
) &isl_union_set_card
} },
826 {"card", { -1, isl_obj_union_map
,
827 isl_obj_union_pw_qpolynomial
,
828 (isc_un_op_fn
) &isl_union_map_card
} },
829 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
830 (isc_un_op_fn
) &isl_union_set_coalesce
} },
831 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
832 (isc_un_op_fn
) &isl_union_map_coalesce
} },
833 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
834 isl_obj_union_pw_qpolynomial
,
835 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
836 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
837 isl_obj_union_pw_qpolynomial_fold
,
838 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
839 {"coefficients", { -1, isl_obj_union_set
,
841 (isc_un_op_fn
) &isl_union_set_coefficients
} },
842 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
843 (isc_un_op_fn
) &isl_union_set_solutions
} },
844 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
845 (isc_un_op_fn
) &isl_union_map_deltas
} },
846 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
847 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
848 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
849 (isc_un_op_fn
) &isl_union_map_domain
} },
850 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
851 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
852 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
854 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
855 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
856 (isc_un_op_fn
) &isl_union_map_domain
} },
857 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
859 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
860 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
862 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
863 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
864 (isc_un_op_fn
) &isl_union_map_domain_map
} },
865 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
866 (isc_un_op_fn
) &isl_union_map_range
} },
867 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
868 (isc_un_op_fn
) &isl_union_map_range
} },
869 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
870 (isc_un_op_fn
) &isl_union_map_range_map
} },
871 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
872 (isc_un_op_fn
) &isl_union_set_identity
} },
873 {"lattice_width", { -1, isl_obj_union_set
,
874 isl_obj_union_pw_qpolynomial
,
875 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
876 {"lb", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
877 (isc_un_op_fn
) &union_pw_qpolynomial_lower_bound
} },
878 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
879 (isc_un_op_fn
) &isl_union_map_lexmin
} },
880 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
881 (isc_un_op_fn
) &isl_union_map_lexmax
} },
882 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
883 (isc_un_op_fn
) &isl_union_set_lexmin
} },
884 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
885 (isc_un_op_fn
) &isl_union_set_lexmax
} },
886 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
887 (isc_un_op_fn
) &isl_union_set_lift
} },
888 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
889 (isc_un_op_fn
) &isl_union_map_params
} },
890 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
891 (isc_un_op_fn
) &isl_union_set_params
} },
892 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
893 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
894 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
895 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
896 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
897 isl_obj_union_pw_qpolynomial
,
898 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
899 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
900 isl_obj_union_pw_qpolynomial
,
901 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
902 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
903 isl_obj_union_pw_qpolynomial
,
904 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
906 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
907 (isc_un_op_fn
) &parse
} },
909 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
910 (isc_un_op_fn
) &union_map_power
} },
911 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
912 (isc_un_op_fn
) &union_set_sample
} },
913 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
914 (isc_un_op_fn
) &union_map_sample
} },
915 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
916 (isc_un_op_fn
) &union_set_scan
} },
917 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
918 (isc_un_op_fn
) &union_map_scan
} },
919 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
920 isl_obj_union_pw_qpolynomial
,
921 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
922 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
923 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
924 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
925 (isc_un_op_fn
) &isl_union_set_unwrap
} },
926 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
927 (isc_un_op_fn
) &isl_union_map_wrap
} },
928 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
929 (isc_un_op_fn
) &isl_union_map_zip
} },
933 struct isl_named_obj
{
938 static void free_obj(struct isl_obj obj
)
940 obj
.type
->free(obj
.v
);
943 static int same_name(const void *entry
, const void *val
)
945 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
947 return !strcmp(named
->name
, val
);
950 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
951 char *name
, struct isl_obj obj
)
953 struct isl_hash_table_entry
*entry
;
955 struct isl_named_obj
*named
;
957 name_hash
= isl_hash_string(isl_hash_init(), name
);
958 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
963 free_obj(named
->obj
);
966 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
981 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
982 struct isl_hash_table
*table
, char *name
)
984 struct isl_obj obj
= { isl_obj_none
, NULL
};
985 struct isl_hash_table_entry
*entry
;
988 name_hash
= isl_hash_string(isl_hash_init(), name
);
989 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
991 struct isl_named_obj
*named
;
994 } else if (isdigit(name
[0]))
995 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
997 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1000 obj
.v
= obj
.type
->copy(obj
.v
);
1004 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1006 if (obj
.type
== super
)
1008 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1010 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1012 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
1013 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
1014 int is_set
= isl_space_is_set(space
);
1015 isl_space_free(space
);
1018 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1019 super
== isl_obj_union_pw_qpolynomial
)
1021 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1022 super
== isl_obj_union_pw_qpolynomial_fold
)
1024 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1026 if (obj
.type
== isl_obj_list
) {
1027 struct isl_list
*list
= obj
.v
;
1028 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1029 return is_subtype(list
->obj
[0], super
);
1031 if (super
== isl_obj_str
)
1036 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1038 struct isl_list
*list
= obj
.v
;
1041 obj
.v
= obj
.type
->copy(obj
.v
);
1043 isl_list_free(list
);
1048 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1051 if (obj
.type
== type
)
1053 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1054 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1055 obj
.type
= isl_obj_union_set
;
1056 obj
.v
= isl_union_set_from_set(set
);
1059 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1060 obj
.type
= isl_obj_union_map
;
1061 obj
.v
= isl_union_map_from_map(obj
.v
);
1064 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1065 obj
.type
= isl_obj_union_set
;
1066 obj
.v
= isl_union_set_from_set(obj
.v
);
1069 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1070 type
== isl_obj_union_pw_qpolynomial
) {
1071 obj
.type
= isl_obj_union_pw_qpolynomial
;
1072 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1075 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1076 type
== isl_obj_union_pw_qpolynomial_fold
) {
1077 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1078 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1081 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1082 if (type
== isl_obj_union_map
) {
1083 obj
.type
= isl_obj_union_map
;
1086 if (type
== isl_obj_union_pw_qpolynomial
) {
1087 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1088 isl_union_set_free(obj
.v
);
1089 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1090 obj
.type
= isl_obj_union_pw_qpolynomial
;
1093 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1094 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1095 isl_union_set_free(obj
.v
);
1096 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1098 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1102 if (obj
.type
== isl_obj_list
) {
1103 struct isl_list
*list
= obj
.v
;
1104 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1105 return convert(ctx
, obj_at(obj
, 0), type
);
1107 if (type
== isl_obj_str
) {
1112 p
= isl_printer_to_str(ctx
);
1115 p
= obj
.type
->print(p
, obj
.v
);
1116 s
= isl_printer_get_str(p
);
1117 isl_printer_free(p
);
1119 str
= isl_str_from_string(ctx
, s
);
1124 obj
.type
= isl_obj_str
;
1130 obj
.type
= isl_obj_none
;
1135 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1139 struct isl_token
*tok
;
1141 tok
= isl_stream_next_token(s
);
1145 for (i
= 0; ; ++i
) {
1148 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1150 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1153 isl_token_free(tok
);
1157 for (i
= 0; ; ++i
) {
1158 if (!named_bin_ops
[i
].name
)
1160 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1162 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1165 isl_token_free(tok
);
1166 return &named_bin_ops
[i
].op
;
1169 isl_stream_push_token(s
, tok
);
1174 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1177 struct isl_token
*tok
;
1179 tok
= isl_stream_next_token(s
);
1183 for (i
= 0; ; ++i
) {
1184 if (!named_un_ops
[i
].name
)
1186 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1189 isl_token_free(tok
);
1190 return &named_un_ops
[i
].op
;
1193 isl_stream_push_token(s
, tok
);
1198 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1203 for (i
= 0; ; ++i
) {
1204 if (!named_un_ops
[i
].name
)
1206 if (named_un_ops
[i
].op
.op
!= like
->op
)
1208 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1211 return &named_un_ops
[i
].op
;
1217 static int is_assign(struct isl_stream
*s
)
1219 struct isl_token
*tok
;
1220 struct isl_token
*tok2
;
1223 tok
= isl_stream_next_token(s
);
1226 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1227 isl_stream_push_token(s
, tok
);
1231 tok2
= isl_stream_next_token(s
);
1233 isl_stream_push_token(s
, tok
);
1236 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1237 isl_stream_push_token(s
, tok2
);
1238 isl_stream_push_token(s
, tok
);
1243 static struct isl_obj
read_obj(struct isl_stream
*s
,
1244 struct isl_hash_table
*table
);
1245 static struct isl_obj
read_expr(struct isl_stream
*s
,
1246 struct isl_hash_table
*table
);
1248 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1249 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1252 struct isl_obj obj
= { isl_obj_none
, NULL
};
1254 obj
= read_obj(s
, table
);
1258 op
= find_matching_un_op(op
, obj
);
1260 ctx
= isl_stream_get_ctx(s
);
1262 isl_die(ctx
, isl_error_invalid
,
1263 "no such unary operator defined on given operand",
1266 obj
= convert(ctx
, obj
, op
->arg
);
1267 obj
.v
= op
->fn(obj
.v
);
1273 obj
.type
= isl_obj_none
;
1278 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1280 struct isl_list
*list
;
1283 if (obj
.type
!= isl_obj_union_map
)
1284 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1285 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1286 list
= isl_list_alloc(ctx
, 2);
1290 list
->obj
[0].type
= isl_obj_union_map
;
1291 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1292 list
->obj
[1].type
= isl_obj_bool
;
1293 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
1295 obj
.type
= isl_obj_list
;
1296 if (exact
< 0 || !list
->obj
[0].v
)
1302 obj
.type
= isl_obj_none
;
1307 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1309 struct isl_list
*list
= obj
.v
;
1310 struct isl_token
*tok
;
1315 tok
= isl_stream_next_token(s
);
1316 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1317 isl_stream_error(s
, tok
, "expecting index");
1319 isl_stream_push_token(s
, tok
);
1322 ctx
= isl_stream_get_ctx(s
);
1323 v
= isl_token_get_val(ctx
, tok
);
1324 i
= isl_val_get_num_si(v
);
1326 isl_token_free(tok
);
1327 isl_assert(ctx
, i
< list
->n
, goto error
);
1328 if (isl_stream_eat(s
, ']'))
1331 return obj_at(obj
, i
);
1334 obj
.type
= isl_obj_none
;
1339 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1340 struct isl_hash_table
*table
)
1345 obj
= read_expr(s
, table
);
1346 ctx
= isl_stream_get_ctx(s
);
1347 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1348 is_subtype(obj
, isl_obj_union_map
), goto error
);
1350 if (obj
.type
== isl_obj_list
) {
1351 struct isl_list
*list
= obj
.v
;
1352 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1353 obj
= obj_at(obj
, 0);
1355 if (obj
.type
== isl_obj_set
)
1356 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1357 else if (obj
.type
== isl_obj_map
)
1358 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1359 if (obj
.type
== isl_obj_union_set
) {
1360 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1362 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1366 if (isl_stream_eat(s
, ')'))
1371 isl_union_map_free(umap
);
1374 obj
.type
= isl_obj_none
;
1379 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1380 __isl_take isl_union_set
*uset
)
1382 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1383 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1385 obj
.type
= isl_obj_list
;
1386 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1391 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1392 __isl_take isl_union_map
*umap
)
1394 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1395 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1397 obj
.type
= isl_obj_list
;
1398 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1403 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1404 struct isl_obj obj
, struct isl_hash_table
*table
)
1409 arg
= read_expr(s
, table
);
1410 ctx
= isl_stream_get_ctx(s
);
1411 if (!is_subtype(arg
, isl_obj_union_map
) &&
1412 !is_subtype(arg
, isl_obj_union_set
))
1413 isl_die(ctx
, isl_error_invalid
,
1414 "expecting set of map argument", goto error
);
1416 if (arg
.type
== isl_obj_list
) {
1417 struct isl_list
*list
= arg
.v
;
1418 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1419 arg
= obj_at(arg
, 0);
1421 if (arg
.type
== isl_obj_set
)
1422 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1423 else if (arg
.type
== isl_obj_map
)
1424 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1425 if (arg
.type
== isl_obj_union_set
)
1426 obj
= apply_fun_set(obj
, arg
.v
);
1428 obj
= apply_fun_map(obj
, arg
.v
);
1432 if (isl_stream_eat(s
, ')'))
1440 obj
.type
= isl_obj_none
;
1445 struct add_vertex_data
{
1446 struct isl_list
*list
;
1450 static isl_stat
add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1452 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1456 ma
= isl_vertex_get_expr(vertex
);
1457 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1459 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1460 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1463 isl_vertex_free(vertex
);
1468 static isl_stat
set_vertices(__isl_take isl_set
*set
, void *user
)
1471 isl_basic_set
*hull
;
1472 isl_vertices
*vertices
= NULL
;
1473 struct isl_list
*list
= NULL
;
1475 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1477 set
= isl_set_remove_divs(set
);
1478 hull
= isl_set_convex_hull(set
);
1479 vertices
= isl_basic_set_compute_vertices(hull
);
1480 isl_basic_set_free(hull
);
1484 ctx
= isl_vertices_get_ctx(vertices
);
1485 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1490 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1492 data
->list
= isl_list_concat(list
, data
->list
);
1494 isl_vertices_free(vertices
);
1499 isl_vertices_free(vertices
);
1500 return isl_stat_error
;
1503 static struct isl_obj
vertices(struct isl_stream
*s
,
1504 struct isl_hash_table
*table
)
1508 struct isl_list
*list
= NULL
;
1509 isl_union_set
*uset
= NULL
;
1510 struct add_vertex_data data
= { NULL
};
1512 obj
= read_expr(s
, table
);
1513 ctx
= isl_stream_get_ctx(s
);
1514 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1515 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1519 list
= isl_list_alloc(ctx
, 0);
1525 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1528 isl_union_set_free(uset
);
1530 obj
.type
= isl_obj_list
;
1535 isl_union_set_free(uset
);
1536 isl_list_free(data
.list
);
1538 obj
.type
= isl_obj_none
;
1543 static struct isl_obj
type_of(struct isl_stream
*s
,
1544 struct isl_hash_table
*table
)
1548 const char *type
= "unknown";
1550 obj
= read_expr(s
, table
);
1552 if (obj
.type
== isl_obj_map
||
1553 obj
.type
== isl_obj_union_map
)
1555 if (obj
.type
== isl_obj_set
||
1556 obj
.type
== isl_obj_union_set
)
1558 if (obj
.type
== isl_obj_pw_multi_aff
)
1559 type
= "piecewise multi-quasiaffine expression";
1560 if (obj
.type
== isl_obj_pw_qpolynomial
||
1561 obj
.type
== isl_obj_union_pw_qpolynomial
)
1562 type
= "piecewise quasipolynomial";
1563 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1564 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1565 type
= "piecewise quasipolynomial fold";
1566 if (obj
.type
== isl_obj_list
)
1568 if (obj
.type
== isl_obj_bool
)
1570 if (obj
.type
== isl_obj_str
)
1572 if (obj
.type
== isl_obj_val
)
1574 if (obj
.type
== isl_obj_schedule
)
1578 obj
.type
= isl_obj_str
;
1579 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1584 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1585 struct isl_hash_table
*table
)
1590 obj
= read_obj(s
, table
);
1591 ctx
= isl_stream_get_ctx(s
);
1592 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1593 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1600 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1601 struct isl_hash_table
*table
)
1606 obj
= read_obj(s
, table
);
1607 ctx
= isl_stream_get_ctx(s
);
1608 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1609 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1616 /* Read a schedule in the form of either a schedule (tree) or a union map
1617 * from "s" and store the schedule in "access".
1619 static __isl_give isl_union_access_info
*access_info_set_schedule(
1620 __isl_take isl_union_access_info
*access
, struct isl_stream
*s
,
1621 struct isl_hash_table
*table
)
1626 obj
= read_obj(s
, table
);
1627 if (obj
.type
== isl_obj_schedule
)
1628 return isl_union_access_info_set_schedule(access
, obj
.v
);
1629 ctx
= isl_stream_get_ctx(s
);
1630 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1632 return isl_union_access_info_set_schedule_map(access
, obj
.v
);
1635 static struct isl_obj
last_any(struct isl_stream
*s
,
1636 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1637 __isl_take isl_union_map
*may_source
)
1639 struct isl_obj obj
= { isl_obj_none
, NULL
};
1640 isl_union_access_info
*access
;
1641 isl_union_flow
*flow
;
1642 isl_union_map
*sink
= NULL
;
1643 isl_union_map
*may_dep
;
1645 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1648 sink
= read_map(s
, table
);
1652 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1655 access
= isl_union_access_info_from_sink(sink
);
1656 access
= isl_union_access_info_set_must_source(access
, must_source
);
1657 access
= isl_union_access_info_set_may_source(access
, may_source
);
1658 access
= access_info_set_schedule(access
, s
, table
);
1659 flow
= isl_union_access_info_compute_flow(access
);
1660 may_dep
= isl_union_flow_get_may_dependence(flow
);
1661 isl_union_flow_free(flow
);
1666 obj
.type
= isl_obj_union_map
;
1671 isl_union_map_free(may_source
);
1672 isl_union_map_free(must_source
);
1673 isl_union_map_free(sink
);
1675 obj
.type
= isl_obj_none
;
1680 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1682 struct isl_obj obj
= { isl_obj_none
, NULL
};
1683 isl_union_access_info
*access
;
1684 isl_union_flow
*flow
;
1685 isl_union_map
*may_source
= NULL
;
1686 isl_union_map
*sink
= NULL
;
1687 isl_union_map
*may_dep
;
1689 may_source
= read_map(s
, table
);
1693 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1694 isl_union_map
*must_source
;
1695 must_source
= read_map(s
, table
);
1698 return last_any(s
, table
, must_source
, may_source
);
1701 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1704 sink
= read_map(s
, table
);
1708 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1711 access
= isl_union_access_info_from_sink(sink
);
1712 access
= isl_union_access_info_set_may_source(access
, may_source
);
1713 access
= access_info_set_schedule(access
, s
, table
);
1714 flow
= isl_union_access_info_compute_flow(access
);
1715 may_dep
= isl_union_flow_get_may_dependence(flow
);
1716 isl_union_flow_free(flow
);
1721 obj
.type
= isl_obj_union_map
;
1726 isl_union_map_free(may_source
);
1727 isl_union_map_free(sink
);
1729 obj
.type
= isl_obj_none
;
1734 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1736 struct isl_obj obj
= { isl_obj_none
, NULL
};
1737 struct isl_list
*list
= NULL
;
1738 isl_union_access_info
*access
;
1739 isl_union_flow
*flow
;
1740 isl_union_map
*must_source
= NULL
;
1741 isl_union_map
*sink
= NULL
;
1742 isl_union_map
*must_dep
;
1743 isl_union_map
*must_no_source
;
1745 must_source
= read_map(s
, table
);
1749 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1750 isl_union_map
*may_source
;
1751 may_source
= read_map(s
, table
);
1754 return last_any(s
, table
, must_source
, may_source
);
1757 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1761 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1764 sink
= read_map(s
, table
);
1768 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1771 access
= isl_union_access_info_from_sink(sink
);
1772 access
= isl_union_access_info_set_must_source(access
, must_source
);
1773 access
= access_info_set_schedule(access
, s
, table
);
1774 flow
= isl_union_access_info_compute_flow(access
);
1775 must_dep
= isl_union_flow_get_must_dependence(flow
);
1776 must_no_source
= isl_union_flow_get_must_no_source(flow
);
1777 isl_union_flow_free(flow
);
1779 list
->obj
[0].type
= isl_obj_union_map
;
1780 list
->obj
[0].v
= must_dep
;
1781 list
->obj
[1].type
= isl_obj_union_map
;
1782 list
->obj
[1].v
= must_no_source
;
1784 if (!must_dep
|| !must_no_source
) {
1785 isl_list_free(list
);
1790 obj
.type
= isl_obj_list
;
1794 isl_list_free(list
);
1795 isl_union_map_free(must_source
);
1796 isl_union_map_free(sink
);
1798 obj
.type
= isl_obj_none
;
1803 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1804 struct isl_hash_table
*table
)
1806 isl_union_set
*domain
;
1807 isl_union_map
*validity
;
1808 isl_union_map
*proximity
;
1810 domain
= read_set(s
, table
);
1814 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1815 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1818 isl_union_map
*umap
;
1819 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1820 umap
= read_map(s
, table
);
1821 validity
= isl_union_map_union(validity
, umap
);
1822 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1823 umap
= read_map(s
, table
);
1824 proximity
= isl_union_map_union(proximity
, umap
);
1829 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1832 static struct isl_obj
schedule(struct isl_stream
*s
,
1833 struct isl_hash_table
*table
)
1835 struct isl_obj obj
= { isl_obj_none
, NULL
};
1836 isl_schedule
*schedule
;
1838 schedule
= get_schedule(s
, table
);
1840 obj
.v
= isl_schedule_get_map(schedule
);
1841 obj
.type
= isl_obj_union_map
;
1843 isl_schedule_free(schedule
);
1848 /* Read a schedule for code generation in the form of either
1849 * a schedule tree or a union map.
1850 * If the input is a set rather than a map, then we construct
1851 * an identity union map schedule on the given set.
1853 static struct isl_obj
get_codegen_schedule(struct isl_stream
*s
,
1854 struct isl_hash_table
*table
)
1859 obj
= read_obj(s
, table
);
1860 ctx
= isl_stream_get_ctx(s
);
1862 if (obj
.type
== isl_obj_schedule
)
1864 if (is_subtype(obj
, isl_obj_union_set
)) {
1865 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1866 obj
.v
= isl_union_set_identity(obj
.v
);
1867 obj
.type
= isl_obj_union_map
;
1869 if (is_subtype(obj
, isl_obj_union_map
))
1870 return convert(ctx
, obj
, isl_obj_union_map
);
1874 obj
.type
= isl_obj_none
;
1875 isl_die(ctx
, isl_error_invalid
, "expecting schedule, set or map",
1879 /* Generate an AST for the given schedule and options and return the AST.
1881 static __isl_give isl_ast_node
*get_ast_from_union_map(
1882 __isl_take isl_union_map
*schedule
, __isl_take isl_union_map
*options
)
1886 isl_ast_build
*build
;
1889 space
= isl_union_map_get_space(schedule
);
1890 context
= isl_set_universe(isl_space_params(space
));
1892 build
= isl_ast_build_from_context(context
);
1893 build
= isl_ast_build_set_options(build
, options
);
1894 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1895 isl_ast_build_free(build
);
1900 /* Generate an AST for the given schedule and return the AST.
1902 static __isl_give isl_ast_node
*get_ast_from_schedule(
1903 __isl_take isl_schedule
*schedule
)
1905 isl_ast_build
*build
;
1908 build
= isl_ast_build_alloc(isl_schedule_get_ctx(schedule
));
1909 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
1910 isl_ast_build_free(build
);
1915 /* Print the AST "tree" on the printer "p".
1917 static __isl_give isl_printer
*print_ast(__isl_take isl_printer
*p
,
1918 __isl_take isl_ast_node
*tree
)
1922 format
= isl_printer_get_output_format(p
);
1923 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1924 p
= isl_printer_print_ast_node(p
, tree
);
1925 p
= isl_printer_set_output_format(p
, format
);
1927 isl_ast_node_free(tree
);
1932 /* Perform the codegen operation.
1933 * In particular, read a schedule, check if the user has specified any options
1934 * and then generate an AST from the schedule (and options) and print it.
1935 * In case the schedule is specified as a schedule tree, the AST generation
1936 * options are embedded in the schedule, so they are not read in separately.
1938 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1939 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1944 obj
= get_codegen_schedule(s
, table
);
1948 if (obj
.type
== isl_obj_schedule
) {
1949 isl_schedule
*schedule
= obj
.v
;
1951 tree
= get_ast_from_schedule(schedule
);
1953 isl_union_map
*schedule
= obj
.v
;
1954 isl_union_map
*options
;
1956 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1957 options
= read_map(s
, table
);
1959 options
= isl_union_map_empty(
1960 isl_union_map_get_space(schedule
));
1962 tree
= get_ast_from_union_map(schedule
, options
);
1965 p
= print_ast(p
, tree
);
1967 isl_stream_eat(s
, ';');
1972 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1974 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1976 struct isl_obj obj
= { isl_obj_none
, NULL
};
1977 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1978 struct isl_list
*list
;
1980 list
= isl_list_alloc(ctx
, 2);
1985 obj
.type
= isl_obj_list
;
1987 list
->obj
[0].type
= isl_obj_union_map
;
1988 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1990 if (isl_band_has_children(band
)) {
1991 isl_band_list
*children
;
1993 children
= isl_band_get_children(band
);
1994 list
->obj
[1] = band_list_to_obj_list(children
);
1996 list
->obj
[1].type
= isl_obj_list
;
1997 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
2000 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
2003 isl_band_free(band
);
2007 isl_band_free(band
);
2009 obj
.type
= isl_obj_none
;
2014 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
2016 struct isl_obj obj
= { isl_obj_none
, NULL
};
2017 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
2018 struct isl_list
*list
;
2021 n
= isl_band_list_n_band(bands
);
2022 list
= isl_list_alloc(ctx
, n
);
2027 obj
.type
= isl_obj_list
;
2029 for (i
= 0; i
< n
; ++i
) {
2032 band
= isl_band_list_get_band(bands
, i
);
2033 list
->obj
[i
] = band_to_obj_list(band
);
2034 if (!list
->obj
[i
].v
)
2038 isl_band_list_free(bands
);
2042 isl_band_list_free(bands
);
2044 obj
.type
= isl_obj_none
;
2049 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
2050 struct isl_hash_table
*table
)
2052 struct isl_obj obj
= { isl_obj_none
, NULL
};
2053 isl_schedule
*schedule
;
2054 isl_band_list
*roots
;
2056 schedule
= get_schedule(s
, table
);
2060 roots
= isl_schedule_get_band_forest(schedule
);
2061 isl_schedule_free(schedule
);
2063 return band_list_to_obj_list(roots
);
2066 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
2068 struct isl_token
*tok
;
2072 ctx
= isl_stream_get_ctx(s
);
2073 if (isl_stream_eat_if_available(s
, '+'))
2074 return transitive_closure(ctx
, obj
);
2076 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
2077 if (obj
.type
!= isl_obj_union_map
)
2078 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2080 tok
= isl_stream_next_token(s
);
2081 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
2082 isl_stream_error(s
, tok
, "expecting integer exponent");
2084 isl_stream_push_token(s
, tok
);
2088 v
= isl_token_get_val(ctx
, tok
);
2089 if (isl_val_is_zero(v
)) {
2090 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2093 isl_stream_push_token(s
, tok
);
2097 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2098 isl_token_free(tok
);
2105 obj
.type
= isl_obj_none
;
2110 static struct isl_obj
check_assert(struct isl_stream
*s
,
2111 struct isl_hash_table
*table
)
2116 obj
= read_expr(s
, table
);
2117 ctx
= isl_stream_get_ctx(s
);
2118 if (obj
.type
!= isl_obj_bool
)
2119 isl_die(ctx
, isl_error_invalid
,
2120 "expecting boolean expression", goto error
);
2121 if (obj
.v
!= &iscc_bool_true
)
2122 isl_die(ctx
, isl_error_unknown
,
2123 "assertion failed", abort());
2126 obj
.type
= isl_obj_none
;
2131 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2135 struct isl_token
*tok
;
2136 struct isl_stream
*s_file
;
2137 struct iscc_options
*options
;
2141 tok
= isl_stream_next_token(s
);
2142 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2143 isl_stream_error(s
, tok
, "expecting filename");
2144 isl_token_free(tok
);
2148 ctx
= isl_stream_get_ctx(s
);
2149 options
= isl_ctx_peek_iscc_options(ctx
);
2150 if (!options
|| !options
->io
) {
2151 isl_token_free(tok
);
2152 isl_die(ctx
, isl_error_invalid
,
2153 "read operation not allowed", goto error
);
2156 name
= isl_token_get_str(ctx
, tok
);
2157 isl_token_free(tok
);
2158 file
= fopen(name
, "r");
2160 isl_assert(ctx
, file
, goto error
);
2162 s_file
= isl_stream_new_file(ctx
, file
);
2168 obj
= isl_stream_read_obj(s_file
);
2170 isl_stream_free(s_file
);
2175 obj
.type
= isl_obj_none
;
2180 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2181 struct isl_hash_table
*table
)
2184 struct isl_token
*tok
;
2185 struct isl_stream
*s_file
;
2186 struct iscc_options
*options
;
2192 tok
= isl_stream_next_token(s
);
2193 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2194 isl_stream_error(s
, tok
, "expecting filename");
2195 isl_token_free(tok
);
2199 obj
= read_expr(s
, table
);
2201 ctx
= isl_stream_get_ctx(s
);
2202 options
= isl_ctx_peek_iscc_options(ctx
);
2203 if (!options
|| !options
->io
) {
2204 isl_token_free(tok
);
2205 isl_die(ctx
, isl_error_invalid
,
2206 "write operation not allowed", goto error
);
2209 name
= isl_token_get_str(ctx
, tok
);
2210 isl_token_free(tok
);
2211 file
= fopen(name
, "w");
2214 isl_die(ctx
, isl_error_unknown
,
2215 "could not open file for writing", goto error
);
2217 p
= isl_printer_to_file(ctx
, file
);
2218 p
= isl_printer_set_output_format(p
, options
->format
);
2219 p
= obj
.type
->print(p
, obj
.v
);
2220 p
= isl_printer_end_line(p
);
2221 isl_printer_free(p
);
2226 obj
.type
= isl_obj_none
;
2231 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2233 struct isl_token
*tok
;
2234 struct isl_obj obj
= { isl_obj_none
, NULL
};
2236 tok
= isl_stream_next_token(s
);
2239 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2241 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2244 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2245 isl_token_free(tok
);
2247 obj
.type
= isl_obj_str
;
2249 isl_stream_push_token(s
, tok
);
2252 isl_token_free(tok
);
2256 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2258 struct isl_token
*tok
;
2259 struct isl_obj obj
= { isl_obj_none
, NULL
};
2262 tok
= isl_stream_next_token(s
);
2265 type
= isl_token_get_type(tok
);
2266 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2267 int is_true
= type
== ISL_TOKEN_TRUE
;
2268 isl_token_free(tok
);
2269 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2270 obj
.type
= isl_obj_bool
;
2272 isl_stream_push_token(s
, tok
);
2276 static __isl_give
char *read_ident(struct isl_stream
*s
)
2280 struct isl_token
*tok
, *tok2
;
2282 name
= isl_stream_read_ident_if_available(s
);
2286 tok
= isl_stream_next_token(s
);
2289 if (isl_token_get_type(tok
) != '$') {
2290 isl_stream_push_token(s
, tok
);
2293 tok2
= isl_stream_next_token(s
);
2294 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2296 isl_stream_push_token(s
, tok2
);
2297 isl_stream_push_token(s
, tok
);
2301 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2302 name
= isl_val_to_str(v
);
2304 isl_token_free(tok
);
2305 isl_token_free(tok2
);
2310 static struct isl_obj
read_list(struct isl_stream
*s
,
2311 struct isl_hash_table
*table
, struct isl_obj obj
)
2313 struct isl_list
*list
;
2315 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2319 list
->obj
[1] = read_obj(s
, table
);
2321 obj
.type
= isl_obj_list
;
2323 if (!list
->obj
[1].v
)
2326 while (isl_stream_eat_if_available(s
, ',')) {
2327 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2335 obj
.type
= isl_obj_none
;
2340 static struct isl_obj
read_obj(struct isl_stream
*s
,
2341 struct isl_hash_table
*table
)
2344 struct isl_obj obj
= { isl_obj_none
, NULL
};
2346 struct isc_un_op
*op
= NULL
;
2348 obj
= read_string_if_available(s
);
2351 obj
= read_bool_if_available(s
);
2354 ctx
= isl_stream_get_ctx(s
);
2355 if (isl_stream_eat_if_available(s
, '(')) {
2356 if (isl_stream_next_token_is(s
, ')')) {
2357 obj
.type
= isl_obj_list
;
2358 obj
.v
= isl_list_alloc(ctx
, 0);
2360 obj
= read_expr(s
, table
);
2361 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2362 obj
= read_list(s
, table
, obj
);
2364 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2367 op
= read_prefix_un_op_if_available(s
);
2369 return read_un_op_expr(s
, table
, op
);
2371 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2372 return check_assert(s
, table
);
2373 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2374 return read_from_file(s
);
2375 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2376 return write_to_file(s
, table
);
2377 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2378 return vertices(s
, table
);
2379 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2380 return any(s
, table
);
2381 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2382 return last(s
, table
);
2383 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2384 return schedule(s
, table
);
2385 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2386 return schedule_forest(s
, table
);
2387 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2388 return type_of(s
, table
);
2390 name
= read_ident(s
);
2392 obj
= stored_obj(ctx
, table
, name
);
2394 obj
= isl_stream_read_obj(s
);
2399 if (isl_stream_eat_if_available(s
, '^'))
2400 obj
= power(s
, obj
);
2401 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2402 obj
= obj_at_index(s
, obj
);
2403 else if (is_subtype(obj
, isl_obj_union_map
) &&
2404 isl_stream_eat_if_available(s
, '(')) {
2405 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2406 obj
= apply(s
, obj
.v
, table
);
2407 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2408 isl_stream_eat_if_available(s
, '(')) {
2409 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2410 obj
= apply_fun(s
, obj
, table
);
2411 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2412 isl_stream_eat_if_available(s
, '(')) {
2413 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2414 obj
= apply_fun(s
, obj
, table
);
2420 obj
.type
= isl_obj_none
;
2425 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2426 struct isl_obj lhs
, struct isl_obj rhs
)
2430 for (i
= 0; ; ++i
) {
2433 if (bin_ops
[i
].op
!= like
->op
)
2435 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2437 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2443 for (i
= 0; ; ++i
) {
2444 if (!named_bin_ops
[i
].name
)
2446 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2448 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2450 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2453 return &named_bin_ops
[i
].op
;
2459 static int next_is_neg_int(struct isl_stream
*s
)
2461 struct isl_token
*tok
;
2464 tok
= isl_stream_next_token(s
);
2465 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2467 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2468 ret
= isl_val_is_neg(v
);
2472 isl_stream_push_token(s
, tok
);
2477 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2478 struct isl_obj lhs
, struct isl_obj rhs
)
2482 lhs
= convert(ctx
, lhs
, op
->lhs
);
2483 rhs
= convert(ctx
, rhs
, op
->rhs
);
2484 if (op
->res
!= isl_obj_bool
)
2485 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2487 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2490 obj
.v
= iscc_bool_from_int(res
);
2497 static struct isl_obj
read_expr(struct isl_stream
*s
,
2498 struct isl_hash_table
*table
)
2501 struct isl_obj obj
= { isl_obj_none
, NULL
};
2502 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2504 obj
= read_obj(s
, table
);
2505 ctx
= isl_stream_get_ctx(s
);
2507 struct isc_bin_op
*op
= NULL
;
2509 op
= read_bin_op_if_available(s
, obj
);
2513 right_obj
= read_obj(s
, table
);
2515 op
= find_matching_bin_op(op
, obj
, right_obj
);
2518 isl_die(ctx
, isl_error_invalid
,
2519 "no such binary operator defined on given operands",
2522 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2525 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2526 right_obj
= read_obj(s
, table
);
2527 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2532 free_obj(right_obj
);
2534 obj
.type
= isl_obj_none
;
2539 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2540 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2542 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2543 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2546 struct isl_obj obj
= { isl_obj_none
, NULL
};
2550 struct isc_bin_op
*op
= NULL
;
2555 if (isl_stream_is_empty(s
))
2558 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2559 return source_file(s
, table
, p
);
2560 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2561 return codegen(s
, table
, p
);
2563 assign
= is_assign(s
);
2565 lhs
= isl_stream_read_ident_if_available(s
);
2566 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2568 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2573 obj
= read_expr(s
, table
);
2574 ctx
= isl_stream_get_ctx(s
);
2575 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2576 fprintf(stderr
, "Interrupted\n");
2577 isl_ctx_reset_error(ctx
);
2579 if (isl_stream_eat(s
, ';'))
2583 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2584 p
= obj
.type
->print(p
, obj
.v
);
2585 p
= isl_printer_end_line(p
);
2590 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2591 static int count
= 0;
2592 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2593 lhs
= strdup(buf
+ 1);
2595 p
= isl_printer_print_str(p
, buf
);
2596 p
= isl_printer_print_str(p
, " := ");
2597 p
= obj
.type
->print(p
, obj
.v
);
2598 p
= isl_printer_end_line(p
);
2600 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2605 isl_stream_flush_tokens(s
);
2606 isl_stream_skip_line(s
);
2612 static isl_stat
free_cb(void **entry
, void *user
)
2614 struct isl_named_obj
*named
= *entry
;
2616 free_obj(named
->obj
);
2623 static void register_named_ops(struct isl_stream
*s
)
2627 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2628 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2629 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2632 for (i
= 0; ; ++i
) {
2633 if (!named_un_ops
[i
].name
)
2635 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2636 named_un_ops
[i
].name
);
2637 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2640 for (i
= 0; ; ++i
) {
2641 if (!named_bin_ops
[i
].name
)
2643 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2644 named_bin_ops
[i
].name
);
2645 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2649 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2650 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2653 struct isl_token
*tok
;
2654 struct isl_stream
*s_file
;
2655 struct iscc_options
*options
;
2659 tok
= isl_stream_next_token(s
);
2660 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2661 isl_stream_error(s
, tok
, "expecting filename");
2662 isl_token_free(tok
);
2666 isl_stream_eat(s
, ';');
2668 ctx
= isl_stream_get_ctx(s
);
2669 options
= isl_ctx_peek_iscc_options(ctx
);
2670 if (!options
|| !options
->io
) {
2671 isl_token_free(tok
);
2672 isl_die(ctx
, isl_error_invalid
,
2673 "source operation not allowed", return p
);
2676 name
= isl_token_get_str(ctx
, tok
);
2677 isl_token_free(tok
);
2678 file
= fopen(name
, "r");
2680 isl_assert(ctx
, file
, return p
);
2682 s_file
= isl_stream_new_file(ctx
, file
);
2688 register_named_ops(s_file
);
2690 while (!isl_stream_is_empty(s_file
))
2691 p
= read_line(s_file
, table
, p
, 0);
2693 isl_stream_free(s_file
);
2699 int main(int argc
, char **argv
)
2701 struct isl_ctx
*ctx
;
2702 struct isl_stream
*s
;
2703 struct isl_hash_table
*table
;
2704 struct iscc_options
*options
;
2706 int tty
= isatty(0);
2708 options
= iscc_options_new_with_defaults();
2711 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2712 pet_options_set_autodetect(ctx
, 1);
2713 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2714 s
= isl_stream_new_file(ctx
, stdin
);
2716 table
= isl_hash_table_alloc(ctx
, 10);
2718 p
= isl_printer_to_file(ctx
, stdout
);
2719 p
= isl_printer_set_output_format(p
, options
->format
);
2722 register_named_ops(s
);
2724 install_signal_handler(ctx
);
2726 while (p
&& !isl_stream_is_empty(s
)) {
2727 isl_ctx_resume(ctx
);
2728 p
= read_line(s
, table
, p
, tty
);
2731 remove_signal_handler(ctx
);
2733 isl_printer_free(p
);
2734 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2735 isl_hash_table_free(ctx
, table
);