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
)
1576 obj
.type
= isl_obj_str
;
1577 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1582 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1583 struct isl_hash_table
*table
)
1588 obj
= read_obj(s
, table
);
1589 ctx
= isl_stream_get_ctx(s
);
1590 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1591 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1598 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1599 struct isl_hash_table
*table
)
1604 obj
= read_obj(s
, table
);
1605 ctx
= isl_stream_get_ctx(s
);
1606 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1607 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1614 static struct isl_obj
last_any(struct isl_stream
*s
,
1615 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1616 __isl_take isl_union_map
*may_source
)
1618 struct isl_obj obj
= { isl_obj_none
, NULL
};
1619 isl_union_access_info
*access
;
1620 isl_union_flow
*flow
;
1621 isl_union_map
*sink
= NULL
;
1622 isl_union_map
*schedule
= NULL
;
1623 isl_union_map
*may_dep
;
1625 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1628 sink
= read_map(s
, table
);
1632 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1635 schedule
= read_map(s
, table
);
1639 access
= isl_union_access_info_from_sink(sink
);
1640 access
= isl_union_access_info_set_must_source(access
, must_source
);
1641 access
= isl_union_access_info_set_may_source(access
, may_source
);
1642 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
1643 flow
= isl_union_access_info_compute_flow(access
);
1644 may_dep
= isl_union_flow_get_may_dependence(flow
);
1645 isl_union_flow_free(flow
);
1650 obj
.type
= isl_obj_union_map
;
1655 isl_union_map_free(may_source
);
1656 isl_union_map_free(must_source
);
1657 isl_union_map_free(sink
);
1658 isl_union_map_free(schedule
);
1660 obj
.type
= isl_obj_none
;
1665 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1667 struct isl_obj obj
= { isl_obj_none
, NULL
};
1668 isl_union_access_info
*access
;
1669 isl_union_flow
*flow
;
1670 isl_union_map
*may_source
= NULL
;
1671 isl_union_map
*sink
= NULL
;
1672 isl_union_map
*schedule
= NULL
;
1673 isl_union_map
*may_dep
;
1675 may_source
= read_map(s
, table
);
1679 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1680 isl_union_map
*must_source
;
1681 must_source
= read_map(s
, table
);
1684 return last_any(s
, table
, must_source
, may_source
);
1687 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1690 sink
= read_map(s
, table
);
1694 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1697 schedule
= read_map(s
, table
);
1701 access
= isl_union_access_info_from_sink(sink
);
1702 access
= isl_union_access_info_set_may_source(access
, may_source
);
1703 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
1704 flow
= isl_union_access_info_compute_flow(access
);
1705 may_dep
= isl_union_flow_get_may_dependence(flow
);
1706 isl_union_flow_free(flow
);
1711 obj
.type
= isl_obj_union_map
;
1716 isl_union_map_free(may_source
);
1717 isl_union_map_free(sink
);
1718 isl_union_map_free(schedule
);
1720 obj
.type
= isl_obj_none
;
1725 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1727 struct isl_obj obj
= { isl_obj_none
, NULL
};
1728 struct isl_list
*list
= NULL
;
1729 isl_union_access_info
*access
;
1730 isl_union_flow
*flow
;
1731 isl_union_map
*must_source
= NULL
;
1732 isl_union_map
*sink
= NULL
;
1733 isl_union_map
*schedule
= NULL
;
1734 isl_union_map
*must_dep
;
1735 isl_union_map
*must_no_source
;
1737 must_source
= read_map(s
, table
);
1741 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1742 isl_union_map
*may_source
;
1743 may_source
= read_map(s
, table
);
1746 return last_any(s
, table
, must_source
, may_source
);
1749 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1753 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1756 sink
= read_map(s
, table
);
1760 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1763 schedule
= read_map(s
, table
);
1767 access
= isl_union_access_info_from_sink(sink
);
1768 access
= isl_union_access_info_set_must_source(access
, must_source
);
1769 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
1770 flow
= isl_union_access_info_compute_flow(access
);
1771 must_dep
= isl_union_flow_get_must_dependence(flow
);
1772 must_no_source
= isl_union_flow_get_must_no_source(flow
);
1773 isl_union_flow_free(flow
);
1775 list
->obj
[0].type
= isl_obj_union_map
;
1776 list
->obj
[0].v
= must_dep
;
1777 list
->obj
[1].type
= isl_obj_union_map
;
1778 list
->obj
[1].v
= must_no_source
;
1780 if (!must_dep
|| !must_no_source
) {
1781 isl_list_free(list
);
1786 obj
.type
= isl_obj_list
;
1790 isl_list_free(list
);
1791 isl_union_map_free(must_source
);
1792 isl_union_map_free(sink
);
1793 isl_union_map_free(schedule
);
1795 obj
.type
= isl_obj_none
;
1800 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1801 struct isl_hash_table
*table
)
1803 isl_union_set
*domain
;
1804 isl_union_map
*validity
;
1805 isl_union_map
*proximity
;
1807 domain
= read_set(s
, table
);
1811 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1812 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1815 isl_union_map
*umap
;
1816 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1817 umap
= read_map(s
, table
);
1818 validity
= isl_union_map_union(validity
, umap
);
1819 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1820 umap
= read_map(s
, table
);
1821 proximity
= isl_union_map_union(proximity
, umap
);
1826 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1829 static struct isl_obj
schedule(struct isl_stream
*s
,
1830 struct isl_hash_table
*table
)
1832 struct isl_obj obj
= { isl_obj_none
, NULL
};
1833 isl_schedule
*schedule
;
1835 schedule
= get_schedule(s
, table
);
1837 obj
.v
= isl_schedule_get_map(schedule
);
1838 obj
.type
= isl_obj_union_map
;
1840 isl_schedule_free(schedule
);
1845 /* Read a schedule for code generation.
1846 * If the input is a set rather than a map, then we construct
1847 * an identity schedule on the given set.
1849 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1850 struct isl_hash_table
*table
)
1855 obj
= read_obj(s
, table
);
1856 ctx
= isl_stream_get_ctx(s
);
1858 if (is_subtype(obj
, isl_obj_union_map
)) {
1859 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1863 if (is_subtype(obj
, isl_obj_union_set
)) {
1864 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1865 return isl_union_set_identity(obj
.v
);
1869 isl_die(ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1872 /* Generate an AST for the given schedule and options and print
1873 * the AST on the printer.
1875 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1876 __isl_take isl_union_map
*schedule
,
1877 __isl_take isl_union_map
*options
)
1881 isl_ast_build
*build
;
1885 space
= isl_union_map_get_space(schedule
);
1886 context
= isl_set_universe(isl_space_params(space
));
1888 build
= isl_ast_build_from_context(context
);
1889 build
= isl_ast_build_set_options(build
, options
);
1890 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1891 isl_ast_build_free(build
);
1896 format
= isl_printer_get_output_format(p
);
1897 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1898 p
= isl_printer_print_ast_node(p
, tree
);
1899 p
= isl_printer_set_output_format(p
, format
);
1901 isl_ast_node_free(tree
);
1906 /* Perform the codegen operation.
1907 * In particular, read a schedule, check if the user has specified any options
1908 * and then generate an AST from the schedule (and options) and print it.
1910 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1911 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1913 isl_union_map
*schedule
;
1914 isl_union_map
*options
;
1916 schedule
= get_codegen_schedule(s
, table
);
1920 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1921 options
= read_map(s
, table
);
1923 options
= isl_union_map_empty(
1924 isl_union_map_get_space(schedule
));
1926 p
= print_code(p
, schedule
, options
);
1928 isl_stream_eat(s
, ';');
1933 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1935 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1937 struct isl_obj obj
= { isl_obj_none
, NULL
};
1938 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1939 struct isl_list
*list
;
1941 list
= isl_list_alloc(ctx
, 2);
1946 obj
.type
= isl_obj_list
;
1948 list
->obj
[0].type
= isl_obj_union_map
;
1949 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1951 if (isl_band_has_children(band
)) {
1952 isl_band_list
*children
;
1954 children
= isl_band_get_children(band
);
1955 list
->obj
[1] = band_list_to_obj_list(children
);
1957 list
->obj
[1].type
= isl_obj_list
;
1958 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1961 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1964 isl_band_free(band
);
1968 isl_band_free(band
);
1970 obj
.type
= isl_obj_none
;
1975 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1977 struct isl_obj obj
= { isl_obj_none
, NULL
};
1978 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1979 struct isl_list
*list
;
1982 n
= isl_band_list_n_band(bands
);
1983 list
= isl_list_alloc(ctx
, n
);
1988 obj
.type
= isl_obj_list
;
1990 for (i
= 0; i
< n
; ++i
) {
1993 band
= isl_band_list_get_band(bands
, i
);
1994 list
->obj
[i
] = band_to_obj_list(band
);
1995 if (!list
->obj
[i
].v
)
1999 isl_band_list_free(bands
);
2003 isl_band_list_free(bands
);
2005 obj
.type
= isl_obj_none
;
2010 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
2011 struct isl_hash_table
*table
)
2013 struct isl_obj obj
= { isl_obj_none
, NULL
};
2014 isl_schedule
*schedule
;
2015 isl_band_list
*roots
;
2017 schedule
= get_schedule(s
, table
);
2021 roots
= isl_schedule_get_band_forest(schedule
);
2022 isl_schedule_free(schedule
);
2024 return band_list_to_obj_list(roots
);
2027 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
2029 struct isl_token
*tok
;
2033 ctx
= isl_stream_get_ctx(s
);
2034 if (isl_stream_eat_if_available(s
, '+'))
2035 return transitive_closure(ctx
, obj
);
2037 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
2038 if (obj
.type
!= isl_obj_union_map
)
2039 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2041 tok
= isl_stream_next_token(s
);
2042 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
2043 isl_stream_error(s
, tok
, "expecting integer exponent");
2045 isl_stream_push_token(s
, tok
);
2049 v
= isl_token_get_val(ctx
, tok
);
2050 if (isl_val_is_zero(v
)) {
2051 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2054 isl_stream_push_token(s
, tok
);
2058 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2059 isl_token_free(tok
);
2066 obj
.type
= isl_obj_none
;
2071 static struct isl_obj
check_assert(struct isl_stream
*s
,
2072 struct isl_hash_table
*table
)
2077 obj
= read_expr(s
, table
);
2078 ctx
= isl_stream_get_ctx(s
);
2079 if (obj
.type
!= isl_obj_bool
)
2080 isl_die(ctx
, isl_error_invalid
,
2081 "expecting boolean expression", goto error
);
2082 if (obj
.v
!= &iscc_bool_true
)
2083 isl_die(ctx
, isl_error_unknown
,
2084 "assertion failed", abort());
2087 obj
.type
= isl_obj_none
;
2092 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2096 struct isl_token
*tok
;
2097 struct isl_stream
*s_file
;
2098 struct iscc_options
*options
;
2102 tok
= isl_stream_next_token(s
);
2103 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2104 isl_stream_error(s
, tok
, "expecting filename");
2105 isl_token_free(tok
);
2109 ctx
= isl_stream_get_ctx(s
);
2110 options
= isl_ctx_peek_iscc_options(ctx
);
2111 if (!options
|| !options
->io
) {
2112 isl_token_free(tok
);
2113 isl_die(ctx
, isl_error_invalid
,
2114 "read operation not allowed", goto error
);
2117 name
= isl_token_get_str(ctx
, tok
);
2118 isl_token_free(tok
);
2119 file
= fopen(name
, "r");
2121 isl_assert(ctx
, file
, goto error
);
2123 s_file
= isl_stream_new_file(ctx
, file
);
2129 obj
= isl_stream_read_obj(s_file
);
2131 isl_stream_free(s_file
);
2136 obj
.type
= isl_obj_none
;
2141 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2142 struct isl_hash_table
*table
)
2145 struct isl_token
*tok
;
2146 struct isl_stream
*s_file
;
2147 struct iscc_options
*options
;
2153 tok
= isl_stream_next_token(s
);
2154 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2155 isl_stream_error(s
, tok
, "expecting filename");
2156 isl_token_free(tok
);
2160 obj
= read_expr(s
, table
);
2162 ctx
= isl_stream_get_ctx(s
);
2163 options
= isl_ctx_peek_iscc_options(ctx
);
2164 if (!options
|| !options
->io
) {
2165 isl_token_free(tok
);
2166 isl_die(ctx
, isl_error_invalid
,
2167 "write operation not allowed", goto error
);
2170 name
= isl_token_get_str(ctx
, tok
);
2171 isl_token_free(tok
);
2172 file
= fopen(name
, "w");
2175 isl_die(ctx
, isl_error_unknown
,
2176 "could not open file for writing", goto error
);
2178 p
= isl_printer_to_file(ctx
, file
);
2179 p
= isl_printer_set_output_format(p
, options
->format
);
2180 p
= obj
.type
->print(p
, obj
.v
);
2181 p
= isl_printer_end_line(p
);
2182 isl_printer_free(p
);
2187 obj
.type
= isl_obj_none
;
2192 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2194 struct isl_token
*tok
;
2195 struct isl_obj obj
= { isl_obj_none
, NULL
};
2197 tok
= isl_stream_next_token(s
);
2200 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2202 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2205 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2206 isl_token_free(tok
);
2208 obj
.type
= isl_obj_str
;
2210 isl_stream_push_token(s
, tok
);
2213 isl_token_free(tok
);
2217 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2219 struct isl_token
*tok
;
2220 struct isl_obj obj
= { isl_obj_none
, NULL
};
2223 tok
= isl_stream_next_token(s
);
2226 type
= isl_token_get_type(tok
);
2227 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2228 int is_true
= type
== ISL_TOKEN_TRUE
;
2229 isl_token_free(tok
);
2230 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2231 obj
.type
= isl_obj_bool
;
2233 isl_stream_push_token(s
, tok
);
2237 static __isl_give
char *read_ident(struct isl_stream
*s
)
2241 struct isl_token
*tok
, *tok2
;
2243 name
= isl_stream_read_ident_if_available(s
);
2247 tok
= isl_stream_next_token(s
);
2250 if (isl_token_get_type(tok
) != '$') {
2251 isl_stream_push_token(s
, tok
);
2254 tok2
= isl_stream_next_token(s
);
2255 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2257 isl_stream_push_token(s
, tok2
);
2258 isl_stream_push_token(s
, tok
);
2262 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2263 name
= isl_val_to_str(v
);
2265 isl_token_free(tok
);
2266 isl_token_free(tok2
);
2271 static struct isl_obj
read_list(struct isl_stream
*s
,
2272 struct isl_hash_table
*table
, struct isl_obj obj
)
2274 struct isl_list
*list
;
2276 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2280 list
->obj
[1] = read_obj(s
, table
);
2282 obj
.type
= isl_obj_list
;
2284 if (!list
->obj
[1].v
)
2287 while (isl_stream_eat_if_available(s
, ',')) {
2288 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2296 obj
.type
= isl_obj_none
;
2301 static struct isl_obj
read_obj(struct isl_stream
*s
,
2302 struct isl_hash_table
*table
)
2305 struct isl_obj obj
= { isl_obj_none
, NULL
};
2307 struct isc_un_op
*op
= NULL
;
2309 obj
= read_string_if_available(s
);
2312 obj
= read_bool_if_available(s
);
2315 ctx
= isl_stream_get_ctx(s
);
2316 if (isl_stream_eat_if_available(s
, '(')) {
2317 if (isl_stream_next_token_is(s
, ')')) {
2318 obj
.type
= isl_obj_list
;
2319 obj
.v
= isl_list_alloc(ctx
, 0);
2321 obj
= read_expr(s
, table
);
2322 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2323 obj
= read_list(s
, table
, obj
);
2325 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2328 op
= read_prefix_un_op_if_available(s
);
2330 return read_un_op_expr(s
, table
, op
);
2332 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2333 return check_assert(s
, table
);
2334 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2335 return read_from_file(s
);
2336 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2337 return write_to_file(s
, table
);
2338 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2339 return vertices(s
, table
);
2340 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2341 return any(s
, table
);
2342 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2343 return last(s
, table
);
2344 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2345 return schedule(s
, table
);
2346 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2347 return schedule_forest(s
, table
);
2348 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2349 return type_of(s
, table
);
2351 name
= read_ident(s
);
2353 obj
= stored_obj(ctx
, table
, name
);
2355 obj
= isl_stream_read_obj(s
);
2360 if (isl_stream_eat_if_available(s
, '^'))
2361 obj
= power(s
, obj
);
2362 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2363 obj
= obj_at_index(s
, obj
);
2364 else if (is_subtype(obj
, isl_obj_union_map
) &&
2365 isl_stream_eat_if_available(s
, '(')) {
2366 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2367 obj
= apply(s
, obj
.v
, table
);
2368 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2369 isl_stream_eat_if_available(s
, '(')) {
2370 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2371 obj
= apply_fun(s
, obj
, table
);
2372 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2373 isl_stream_eat_if_available(s
, '(')) {
2374 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2375 obj
= apply_fun(s
, obj
, table
);
2381 obj
.type
= isl_obj_none
;
2386 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2387 struct isl_obj lhs
, struct isl_obj rhs
)
2391 for (i
= 0; ; ++i
) {
2394 if (bin_ops
[i
].op
!= like
->op
)
2396 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2398 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2404 for (i
= 0; ; ++i
) {
2405 if (!named_bin_ops
[i
].name
)
2407 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2409 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2411 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2414 return &named_bin_ops
[i
].op
;
2420 static int next_is_neg_int(struct isl_stream
*s
)
2422 struct isl_token
*tok
;
2425 tok
= isl_stream_next_token(s
);
2426 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2428 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2429 ret
= isl_val_is_neg(v
);
2433 isl_stream_push_token(s
, tok
);
2438 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2439 struct isl_obj lhs
, struct isl_obj rhs
)
2443 lhs
= convert(ctx
, lhs
, op
->lhs
);
2444 rhs
= convert(ctx
, rhs
, op
->rhs
);
2445 if (op
->res
!= isl_obj_bool
)
2446 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2448 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2451 obj
.v
= iscc_bool_from_int(res
);
2458 static struct isl_obj
read_expr(struct isl_stream
*s
,
2459 struct isl_hash_table
*table
)
2462 struct isl_obj obj
= { isl_obj_none
, NULL
};
2463 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2465 obj
= read_obj(s
, table
);
2466 ctx
= isl_stream_get_ctx(s
);
2468 struct isc_bin_op
*op
= NULL
;
2470 op
= read_bin_op_if_available(s
, obj
);
2474 right_obj
= read_obj(s
, table
);
2476 op
= find_matching_bin_op(op
, obj
, right_obj
);
2479 isl_die(ctx
, isl_error_invalid
,
2480 "no such binary operator defined on given operands",
2483 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2486 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2487 right_obj
= read_obj(s
, table
);
2488 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2493 free_obj(right_obj
);
2495 obj
.type
= isl_obj_none
;
2500 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2501 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2503 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2504 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2507 struct isl_obj obj
= { isl_obj_none
, NULL
};
2511 struct isc_bin_op
*op
= NULL
;
2516 if (isl_stream_is_empty(s
))
2519 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2520 return source_file(s
, table
, p
);
2521 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2522 return codegen(s
, table
, p
);
2524 assign
= is_assign(s
);
2526 lhs
= isl_stream_read_ident_if_available(s
);
2527 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2529 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2534 obj
= read_expr(s
, table
);
2535 ctx
= isl_stream_get_ctx(s
);
2536 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2537 fprintf(stderr
, "Interrupted\n");
2538 isl_ctx_reset_error(ctx
);
2540 if (isl_stream_eat(s
, ';'))
2544 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2545 p
= obj
.type
->print(p
, obj
.v
);
2546 p
= isl_printer_end_line(p
);
2551 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2552 static int count
= 0;
2553 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2554 lhs
= strdup(buf
+ 1);
2556 p
= isl_printer_print_str(p
, buf
);
2557 p
= isl_printer_print_str(p
, " := ");
2558 p
= obj
.type
->print(p
, obj
.v
);
2559 p
= isl_printer_end_line(p
);
2561 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2566 isl_stream_flush_tokens(s
);
2567 isl_stream_skip_line(s
);
2573 static isl_stat
free_cb(void **entry
, void *user
)
2575 struct isl_named_obj
*named
= *entry
;
2577 free_obj(named
->obj
);
2584 static void register_named_ops(struct isl_stream
*s
)
2588 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2589 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2590 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2593 for (i
= 0; ; ++i
) {
2594 if (!named_un_ops
[i
].name
)
2596 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2597 named_un_ops
[i
].name
);
2598 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2601 for (i
= 0; ; ++i
) {
2602 if (!named_bin_ops
[i
].name
)
2604 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2605 named_bin_ops
[i
].name
);
2606 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2610 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2611 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2614 struct isl_token
*tok
;
2615 struct isl_stream
*s_file
;
2616 struct iscc_options
*options
;
2620 tok
= isl_stream_next_token(s
);
2621 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2622 isl_stream_error(s
, tok
, "expecting filename");
2623 isl_token_free(tok
);
2627 isl_stream_eat(s
, ';');
2629 ctx
= isl_stream_get_ctx(s
);
2630 options
= isl_ctx_peek_iscc_options(ctx
);
2631 if (!options
|| !options
->io
) {
2632 isl_token_free(tok
);
2633 isl_die(ctx
, isl_error_invalid
,
2634 "source operation not allowed", return p
);
2637 name
= isl_token_get_str(ctx
, tok
);
2638 isl_token_free(tok
);
2639 file
= fopen(name
, "r");
2641 isl_assert(ctx
, file
, return p
);
2643 s_file
= isl_stream_new_file(ctx
, file
);
2649 register_named_ops(s_file
);
2651 while (!isl_stream_is_empty(s_file
))
2652 p
= read_line(s_file
, table
, p
, 0);
2654 isl_stream_free(s_file
);
2660 int main(int argc
, char **argv
)
2662 struct isl_ctx
*ctx
;
2663 struct isl_stream
*s
;
2664 struct isl_hash_table
*table
;
2665 struct iscc_options
*options
;
2667 int tty
= isatty(0);
2669 options
= iscc_options_new_with_defaults();
2672 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2673 pet_options_set_autodetect(ctx
, 1);
2674 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2675 s
= isl_stream_new_file(ctx
, stdin
);
2677 table
= isl_hash_table_alloc(ctx
, 10);
2679 p
= isl_printer_to_file(ctx
, stdout
);
2680 p
= isl_printer_set_output_format(p
, options
->format
);
2683 register_named_ops(s
);
2685 install_signal_handler(ctx
);
2687 while (p
&& !isl_stream_is_empty(s
)) {
2688 isl_ctx_resume(ctx
);
2689 p
= read_line(s
, table
, p
, tty
);
2692 remove_signal_handler(ctx
);
2694 isl_printer_free(p
);
2695 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2696 isl_hash_table_free(ctx
, table
);