11 #include <isl/stream.h>
14 #include <isl/union_set.h>
15 #include <isl/union_map.h>
16 #include <isl/vertices.h>
18 #include <isl/schedule.h>
19 #include <isl/ast_build.h>
20 #include <isl/printer.h>
21 #include <isl_obj_list.h>
22 #include <isl_obj_str.h>
23 #include <barvinok/isl.h>
24 #include <barvinok/options.h>
25 #include "lattice_width.h"
32 static isl_ctx
*main_ctx
;
34 static void handler(int signum
)
36 if (isl_ctx_aborted(main_ctx
))
38 isl_ctx_abort(main_ctx
);
41 static struct sigaction sa_old
;
43 static void install_signal_handler(isl_ctx
*ctx
)
49 memset(&sa
, 0, sizeof(struct sigaction
));
50 sa
.sa_handler
= &handler
;
51 sa
.sa_flags
= SA_RESTART
;
52 sigaction(SIGINT
, &sa
, &sa_old
);
55 static void remove_signal_handler(isl_ctx
*ctx
)
57 sigaction(SIGINT
, &sa_old
, NULL
);
62 static void install_signal_handler(isl_ctx
*ctx
)
66 static void remove_signal_handler(isl_ctx
*ctx
)
76 int pet_options_set_autodetect(isl_ctx
*ctx
, int val
)
80 int pet_options_set_encapsulate_dynamic_control(isl_ctx
*ctx
, int val
)
86 static int iscc_bool_false
= 0;
87 static int iscc_bool_true
= 1;
88 static int iscc_bool_error
= -1;
90 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
91 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
93 ISCC_MINIMIZING
, ISCC_RESPECTING
,
94 ISCC_CODEGEN
, ISCC_USING
,
95 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
97 static const char *op_name
[ISCC_N_OP
] = {
98 [ISCC_ASSERT
] = "assert",
100 [ISCC_WRITE
] = "write",
101 [ISCC_PRINT
] = "print",
102 [ISCC_SOURCE
] = "source",
103 [ISCC_VERTICES
] = "vertices",
104 [ISCC_LAST
] = "last",
106 [ISCC_BEFORE
] = "before",
107 [ISCC_UNDER
] = "under",
108 [ISCC_SCHEDULE
] = "schedule",
109 [ISCC_MINIMIZING
] = "minimizing",
110 [ISCC_RESPECTING
] = "respecting",
111 [ISCC_CODEGEN
] = "codegen",
112 [ISCC_USING
] = "using",
113 [ISCC_TYPEOF
] = "typeof"
115 static enum isl_token_type iscc_op
[ISCC_N_OP
];
117 struct isl_arg_choice iscc_format
[] = {
118 {"isl", ISL_FORMAT_ISL
},
119 {"omega", ISL_FORMAT_OMEGA
},
120 {"polylib", ISL_FORMAT_POLYLIB
},
121 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
122 {"latex", ISL_FORMAT_LATEX
},
127 struct iscc_options
{
128 struct barvinok_options
*barvinok
;
129 struct pet_options
*pet
;
134 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
135 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
138 ISL_ARG_CHILD(struct iscc_options
, pet
, "pet", &pet_options_args
, "pet options")
140 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
141 iscc_format
, ISL_FORMAT_ISL
, "output format")
142 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
143 "allow read and write operations")
146 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
147 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
149 static void *isl_obj_bool_copy(void *v
)
154 static void isl_obj_bool_free(void *v
)
158 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
161 if (v
== &iscc_bool_true
)
162 return isl_printer_print_str(p
, "True");
163 else if (v
== &iscc_bool_false
)
164 return isl_printer_print_str(p
, "False");
166 return isl_printer_print_str(p
, "Error");
169 static void *isl_obj_bool_add(void *v1
, void *v2
)
174 struct isl_obj_vtable isl_obj_bool_vtable
= {
180 #define isl_obj_bool (&isl_obj_bool_vtable)
182 int *iscc_bool_from_int(int res
)
184 return res
< 0 ? &iscc_bool_error
:
185 res
? &iscc_bool_true
: &iscc_bool_false
;
188 /* Conjunction of "b1" and "b2".
189 * The result is returned as an integer because it is post-processed by
190 * iscc_bool_from_int.
192 static int isl_bool_and(isl_bool
*b1
, __isl_take isl_bool
*b2
)
194 if (b1
== &iscc_bool_error
|| b2
== &iscc_bool_error
)
196 return b1
== &iscc_bool_true
&& b2
== &iscc_bool_true
;
199 /* Disjunction of "b1" and "b2".
200 * The result is returned as an integer because it is post-processed by
201 * iscc_bool_from_int.
203 static int isl_bool_or(isl_bool
*b1
, __isl_take isl_bool
*b2
)
205 if (b1
== &iscc_bool_error
|| b2
== &iscc_bool_error
)
207 return b1
== &iscc_bool_true
|| b2
== &iscc_bool_true
;
210 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
211 __isl_take isl_union_map
*map2
)
213 return isl_union_map_is_subset(map2
, map1
);
215 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
216 __isl_take isl_union_set
*set2
)
218 return isl_union_set_is_subset(set2
, set1
);
221 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
222 __isl_take isl_union_map
*map2
)
224 return isl_union_map_is_strict_subset(map2
, map1
);
226 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
227 __isl_take isl_union_set
*set2
)
229 return isl_union_set_is_strict_subset(set2
, set1
);
232 extern struct isl_obj_vtable isl_obj_list_vtable
;
233 #define isl_obj_list (&isl_obj_list_vtable)
235 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
236 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
238 enum isl_token_type op
;
244 isc_bin_test_fn test
;
247 struct isc_named_bin_op
{
249 struct isc_bin_op op
;
251 /* Compound binary operator.
252 * "full" is only used to generate a unique token number for op.op
253 * in register_named_ops.
254 * "op1" is the first part of the compound operator.
255 * "op2" is the second part of the compound operator.
257 struct iscc_compound_bin_op
{
259 enum isl_token_type op1
;
260 enum isl_token_type op2
;
261 struct isc_bin_op op
;
265 isl_union_pw_qpolynomial
*upwqp
;
266 isl_union_pw_qpolynomial
*res
;
269 static isl_stat
eval_at(__isl_take isl_point
*pnt
, void *user
)
271 struct iscc_at
*at
= (struct iscc_at
*) user
;
276 set
= isl_set_from_point(isl_point_copy(pnt
));
277 v
= isl_union_pw_qpolynomial_eval(
278 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
279 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
281 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
282 isl_union_pw_qpolynomial_from_pw_qpolynomial(
283 isl_pw_qpolynomial_alloc(set
, qp
)));
288 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
289 __isl_take isl_union_pw_qpolynomial
*upwqp
,
290 __isl_take isl_union_set
*uset
)
295 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
297 isl_union_set_foreach_point(uset
, eval_at
, &at
);
299 isl_union_pw_qpolynomial_free(upwqp
);
300 isl_union_set_free(uset
);
305 struct iscc_fold_at
{
306 isl_union_pw_qpolynomial_fold
*upwf
;
307 isl_union_pw_qpolynomial
*res
;
310 static isl_stat
eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
312 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
317 set
= isl_set_from_point(isl_point_copy(pnt
));
318 v
= isl_union_pw_qpolynomial_fold_eval(
319 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
320 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
322 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
323 isl_union_pw_qpolynomial_from_pw_qpolynomial(
324 isl_pw_qpolynomial_alloc(set
, qp
)));
329 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
330 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
331 __isl_take isl_union_set
*uset
)
333 struct iscc_fold_at at
;
336 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
338 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
340 isl_union_pw_qpolynomial_fold_free(upwf
);
341 isl_union_set_free(uset
);
346 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
347 __isl_take isl_union_pw_qpolynomial
*upwqp
,
348 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
350 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
354 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
355 __isl_take isl_union_map
*umap
,
356 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
359 struct isl_list
*list
;
362 ctx
= isl_union_map_get_ctx(umap
);
363 list
= isl_list_alloc(ctx
, 2);
367 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
368 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
370 list
->obj
[1].type
= isl_obj_bool
;
371 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
372 if (tight
< 0 || !list
->obj
[0].v
)
377 isl_union_map_free(umap
);
378 isl_union_pw_qpolynomial_fold_free(upwf
);
384 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
385 __isl_take isl_union_set
*uset
,
386 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
389 struct isl_list
*list
;
392 ctx
= isl_union_set_get_ctx(uset
);
393 list
= isl_list_alloc(ctx
, 2);
397 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
398 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
400 list
->obj
[1].type
= isl_obj_bool
;
401 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
402 if (tight
< 0 || !list
->obj
[0].v
)
407 isl_union_set_free(uset
);
408 isl_union_pw_qpolynomial_fold_free(upwf
);
414 static __isl_give isl_union_pw_qpolynomial
*isl_val_mul_union_pw_qpolynomial(
415 __isl_take isl_val
*v
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
417 return isl_union_pw_qpolynomial_scale_val(upwqp
, v
);
420 static __isl_give isl_union_pw_qpolynomial_fold
*
421 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val
*v
,
422 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
424 return isl_union_pw_qpolynomial_fold_scale_val(upwf
, v
);
427 /* Are the two strings "str1" and "str2" equal to each other?
429 static int str_eq(__isl_keep isl_str
*str1
, __isl_keep isl_str
*str2
)
434 return !strcmp(str1
->s
, str2
->s
);
437 struct isc_bin_op bin_ops
[] = {
438 { '+', isl_obj_bool
, isl_obj_bool
, isl_obj_bool
,
439 (isc_bin_op_fn
) &isl_bool_or
},
440 { '*', isl_obj_bool
, isl_obj_bool
, isl_obj_bool
,
441 (isc_bin_op_fn
) &isl_bool_and
},
442 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
443 (isc_bin_op_fn
) &isl_val_add
},
444 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
445 (isc_bin_op_fn
) &isl_val_sub
},
446 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
447 (isc_bin_op_fn
) &isl_val_mul
},
448 { '+', isl_obj_pw_multi_aff
, isl_obj_pw_multi_aff
,
449 isl_obj_pw_multi_aff
,
450 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
451 { '+', isl_obj_union_set
, isl_obj_union_set
,
453 (isc_bin_op_fn
) &isl_union_set_union
},
454 { '+', isl_obj_union_map
, isl_obj_union_map
,
456 (isc_bin_op_fn
) &isl_union_map_union
},
457 { '-', isl_obj_union_set
, isl_obj_union_set
,
459 (isc_bin_op_fn
) &isl_union_set_subtract
},
460 { '-', isl_obj_union_map
, isl_obj_union_map
,
462 (isc_bin_op_fn
) &isl_union_map_subtract
},
463 { '-', isl_obj_union_map
, isl_obj_union_set
,
465 (isc_bin_op_fn
) &isl_union_map_subtract_domain
},
466 { '*', isl_obj_union_set
, isl_obj_union_set
,
468 (isc_bin_op_fn
) &isl_union_set_intersect
},
469 { '*', isl_obj_union_map
, isl_obj_union_map
,
471 (isc_bin_op_fn
) &isl_union_map_intersect
},
472 { '*', isl_obj_union_map
, isl_obj_union_set
,
474 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
475 { '.', isl_obj_union_map
, isl_obj_union_map
,
477 (isc_bin_op_fn
) &isl_union_map_apply_range
},
478 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
479 isl_obj_union_pw_qpolynomial
,
480 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
481 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
483 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
484 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
486 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
487 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
488 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
489 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
490 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
491 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
493 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
494 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
496 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
497 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
499 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
500 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
502 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
503 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
505 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
506 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
508 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
509 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
512 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
513 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
516 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
517 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
519 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
520 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
522 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
523 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
525 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
526 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
528 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
529 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
531 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
532 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
534 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
535 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
537 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
538 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
540 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
541 { '.', isl_obj_union_pw_qpolynomial_fold
,
542 isl_obj_union_pw_qpolynomial_fold
,
543 isl_obj_union_pw_qpolynomial_fold
,
544 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
545 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
546 isl_obj_union_pw_qpolynomial
,
547 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
548 { '+', isl_obj_union_pw_qpolynomial
,
549 isl_obj_union_pw_qpolynomial_fold
,
550 isl_obj_union_pw_qpolynomial_fold
,
551 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
552 { '+', isl_obj_union_pw_qpolynomial_fold
,
553 isl_obj_union_pw_qpolynomial
,
554 isl_obj_union_pw_qpolynomial_fold
,
555 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
556 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
557 isl_obj_union_pw_qpolynomial
,
558 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
559 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
560 isl_obj_union_pw_qpolynomial
,
561 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
562 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
563 isl_obj_union_pw_qpolynomial
,
564 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
565 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
566 isl_obj_union_pw_qpolynomial_fold
,
567 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
568 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
569 isl_obj_union_pw_qpolynomial_fold
,
570 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
571 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
572 isl_obj_union_pw_qpolynomial
,
573 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
574 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
575 isl_obj_union_pw_qpolynomial
,
576 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
577 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
578 isl_obj_union_pw_qpolynomial_fold
,
579 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
580 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
581 isl_obj_union_pw_qpolynomial
,
582 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
583 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
584 isl_obj_union_pw_qpolynomial
,
585 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
586 { '%', isl_obj_union_set
, isl_obj_union_set
,
588 (isc_bin_op_fn
) &isl_union_set_gist
},
589 { '%', isl_obj_union_map
, isl_obj_union_map
,
591 (isc_bin_op_fn
) &isl_union_map_gist
},
592 { '%', isl_obj_union_map
, isl_obj_union_set
,
594 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
595 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
596 isl_obj_union_pw_qpolynomial
,
597 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
598 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
599 isl_obj_union_pw_qpolynomial_fold
,
600 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
601 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
602 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
603 { .test
= (isc_bin_test_fn
)
604 &isl_union_pw_qpolynomial_plain_is_equal
} },
605 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
606 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
607 { .test
= (isc_bin_test_fn
)
608 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
609 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
610 (isc_bin_op_fn
) &isl_str_concat
},
611 { '=', isl_obj_str
, isl_obj_str
, isl_obj_bool
,
612 { .test
= (isc_bin_test_fn
) &str_eq
} },
616 struct iscc_compound_bin_op compound_bin_ops
[] = {
617 { "->*", ISL_TOKEN_TO
, '*',
618 { -1, isl_obj_union_map
, isl_obj_union_set
,
620 (isc_bin_op_fn
) &isl_union_map_intersect_range
} },
621 { "->-", ISL_TOKEN_TO
, '-',
622 { -1, isl_obj_union_map
, isl_obj_union_set
,
624 (isc_bin_op_fn
) &isl_union_map_subtract_range
} },
628 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
629 __isl_take isl_union_map
*umap2
)
631 return isl_union_map_apply_range(umap2
, umap1
);
634 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
635 __isl_take isl_union_pw_qpolynomial
*upwqp
,
636 __isl_take isl_union_map
*umap
)
638 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
641 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
642 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
643 __isl_take isl_union_map
*umap
)
645 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
648 struct isc_named_bin_op named_bin_ops
[] = {
649 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
651 (isc_bin_op_fn
) &map_after_map
} },
652 { "after", { -1, isl_obj_union_pw_qpolynomial
,
653 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
654 (isc_bin_op_fn
) &qpolynomial_after_map
} },
655 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
656 isl_obj_union_map
, isl_obj_list
,
657 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
658 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
660 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
661 { "before", { -1, isl_obj_union_map
,
662 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
663 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
664 { "before", { -1, isl_obj_union_map
,
665 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
666 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
667 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
669 (isc_bin_op_fn
) &isl_union_set_product
} },
670 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
672 (isc_bin_op_fn
) &isl_union_map_product
} },
673 { "cross_range", { -1, isl_obj_union_map
,
674 isl_obj_union_map
, isl_obj_union_map
,
675 (isc_bin_op_fn
) &isl_union_map_range_product
} },
679 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
681 return isl_set_from_basic_set(isl_union_set_sample(uset
));
684 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
686 return isl_map_from_basic_map(isl_union_map_sample(umap
));
689 static __isl_give
struct isl_list
*union_map_power(
690 __isl_take isl_union_map
*umap
)
693 struct isl_list
*list
;
696 ctx
= isl_union_map_get_ctx(umap
);
697 list
= isl_list_alloc(ctx
, 2);
701 list
->obj
[0].type
= isl_obj_union_map
;
702 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
703 list
->obj
[1].type
= isl_obj_bool
;
704 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
705 if (exact
< 0 || !list
->obj
[0].v
)
710 isl_union_map_free(umap
);
716 /* Compute a lower or upper bound on "upwqp" depending on "type" and
717 * return a list containing two elements, the bound and a boolean
718 * indicating whether the result is tight.
720 static __isl_give
struct isl_list
*union_pw_qpolynomial_bound(
721 __isl_take isl_union_pw_qpolynomial
*upwqp
, enum isl_fold type
)
724 struct isl_list
*list
;
727 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
728 list
= isl_list_alloc(ctx
, 2);
732 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
733 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
, type
, &tight
);
734 list
->obj
[1].type
= isl_obj_bool
;
735 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
736 if (tight
< 0 || !list
->obj
[0].v
)
741 isl_union_pw_qpolynomial_free(upwqp
);
747 /* Compute a lower bound on "upwqp" and return a list containing
748 * two elements, the bound and a booleanindicating whether
749 * the result is tight.
751 static __isl_give
struct isl_list
*union_pw_qpolynomial_lower_bound(
752 __isl_take isl_union_pw_qpolynomial
*upwqp
)
754 return union_pw_qpolynomial_bound(upwqp
, isl_fold_min
);
757 /* Compute a upper bound on "upwqp" and return a list containing
758 * two elements, the bound and a booleanindicating whether
759 * the result is tight.
761 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
762 __isl_take isl_union_pw_qpolynomial
*upwqp
)
764 return union_pw_qpolynomial_bound(upwqp
, isl_fold_max
);
768 /* Collect all statement instances, except those of kill statements.
770 static __isl_give isl_union_set
*collect_non_kill_instances(
771 struct pet_scop
*scop
)
775 isl_union_set
*domain
;
780 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
782 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
783 struct pet_stmt
*stmt
= scop
->stmts
[i
];
785 if (pet_stmt_is_kill(stmt
))
787 domain_i
= isl_set_copy(stmt
->domain
);
788 if (scop
->stmts
[i
]->n_arg
> 0)
789 domain_i
= isl_map_domain(isl_set_unwrap(domain_i
));
790 domain
= isl_union_set_add_set(domain
, domain_i
);
796 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
799 struct isl_list
*list
;
800 struct pet_scop
*scop
;
802 isl_union_map
*may_reads
, *must_writes
, *may_writes
;
803 isl_union_set
*domain
;
804 struct iscc_options
*options
;
810 options
= isl_ctx_peek_iscc_options(ctx
);
811 if (!options
|| !options
->io
) {
813 isl_die(ctx
, isl_error_invalid
,
814 "parse_file operation not allowed", return NULL
);
817 list
= isl_list_alloc(ctx
, 5);
821 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
822 domain
= collect_non_kill_instances(scop
);
823 sched
= pet_scop_get_schedule(scop
);
824 sched
= isl_schedule_intersect_domain(sched
,
825 isl_union_set_copy(domain
));
826 may_reads
= pet_scop_get_may_reads(scop
);
827 may_writes
= pet_scop_get_may_writes(scop
);
828 must_writes
= pet_scop_get_must_writes(scop
);
831 list
->obj
[0].type
= isl_obj_union_set
;
832 list
->obj
[0].v
= domain
;
833 list
->obj
[1].type
= isl_obj_union_map
;
834 list
->obj
[1].v
= must_writes
;
835 list
->obj
[2].type
= isl_obj_union_map
;
836 list
->obj
[2].v
= may_writes
;
837 list
->obj
[3].type
= isl_obj_union_map
;
838 list
->obj
[3].v
= may_reads
;
839 list
->obj
[4].type
= isl_obj_schedule
;
840 list
->obj
[4].v
= sched
;
842 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
843 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
855 static isl_stat
add_point(__isl_take isl_point
*pnt
, void *user
)
857 isl_union_set
**scan
= (isl_union_set
**) user
;
859 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
864 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
868 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
870 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
871 isl_union_set_free(scan
);
875 isl_union_set_free(uset
);
879 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
881 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
884 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
885 __isl_take isl_union_pw_qpolynomial
*upwqp
)
887 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
890 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
891 __isl_take isl_union_pw_qpolynomial
*upwqp
)
893 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
896 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
897 __isl_take isl_union_pw_qpolynomial
*upwqp
)
899 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
902 /* Return the domain of "schedule".
904 static __isl_give isl_union_set
*schedule_domain(
905 __isl_take isl_schedule
*schedule
)
907 isl_union_set
*domain
;
909 domain
= isl_schedule_get_domain(schedule
);
910 isl_schedule_free(schedule
);
915 /* Convert "schedule" to a union map representation.
917 static __isl_give isl_union_map
*schedule_map(__isl_take isl_schedule
*schedule
)
921 map
= isl_schedule_get_map(schedule
);
922 isl_schedule_free(schedule
);
927 typedef void *(*isc_un_op_fn
)(void *arg
);
929 enum isl_token_type op
;
934 struct isc_named_un_op
{
938 struct isc_named_un_op named_un_ops
[] = {
939 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
940 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
941 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
942 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
943 {"card", { -1, isl_obj_union_set
,
944 isl_obj_union_pw_qpolynomial
,
945 (isc_un_op_fn
) &isl_union_set_card
} },
946 {"card", { -1, isl_obj_union_map
,
947 isl_obj_union_pw_qpolynomial
,
948 (isc_un_op_fn
) &isl_union_map_card
} },
949 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
950 (isc_un_op_fn
) &isl_union_set_coalesce
} },
951 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
952 (isc_un_op_fn
) &isl_union_map_coalesce
} },
953 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
954 isl_obj_union_pw_qpolynomial
,
955 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
956 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
957 isl_obj_union_pw_qpolynomial_fold
,
958 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
959 {"coefficients", { -1, isl_obj_union_set
,
961 (isc_un_op_fn
) &isl_union_set_coefficients
} },
962 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
963 (isc_un_op_fn
) &isl_union_set_solutions
} },
964 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
965 (isc_un_op_fn
) &isl_union_map_deltas
} },
966 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
967 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
968 {"dom", { -1, isl_obj_schedule
, isl_obj_union_set
,
969 (isc_un_op_fn
) &schedule_domain
} },
970 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
971 (isc_un_op_fn
) &isl_union_map_domain
} },
972 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
973 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
974 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
976 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
977 {"domain", { -1, isl_obj_schedule
, isl_obj_union_set
,
978 (isc_un_op_fn
) &schedule_domain
} },
979 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
980 (isc_un_op_fn
) &isl_union_map_domain
} },
981 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
983 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
984 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
986 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
987 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
988 (isc_un_op_fn
) &isl_union_map_domain_map
} },
989 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
990 (isc_un_op_fn
) &isl_union_map_range
} },
991 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
992 (isc_un_op_fn
) &isl_union_map_range
} },
993 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
994 (isc_un_op_fn
) &isl_union_map_range_map
} },
995 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
996 (isc_un_op_fn
) &isl_union_set_identity
} },
997 {"lattice_width", { -1, isl_obj_union_set
,
998 isl_obj_union_pw_qpolynomial
,
999 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
1000 {"lb", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
1001 (isc_un_op_fn
) &union_pw_qpolynomial_lower_bound
} },
1002 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
1003 (isc_un_op_fn
) &isl_union_map_lexmin
} },
1004 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
1005 (isc_un_op_fn
) &isl_union_map_lexmax
} },
1006 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
1007 (isc_un_op_fn
) &isl_union_set_lexmin
} },
1008 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
1009 (isc_un_op_fn
) &isl_union_set_lexmax
} },
1010 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
1011 (isc_un_op_fn
) &isl_union_set_lift
} },
1012 {"map", { -1, isl_obj_schedule
, isl_obj_union_map
,
1013 (isc_un_op_fn
) &schedule_map
} },
1014 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
1015 (isc_un_op_fn
) &isl_union_map_params
} },
1016 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
1017 (isc_un_op_fn
) &isl_union_set_params
} },
1018 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
1019 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
1020 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
1021 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
1022 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
1023 isl_obj_union_pw_qpolynomial
,
1024 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
1025 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
1026 isl_obj_union_pw_qpolynomial
,
1027 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
1028 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
1029 isl_obj_union_pw_qpolynomial
,
1030 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
1032 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
1033 (isc_un_op_fn
) &parse
} },
1035 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
1036 (isc_un_op_fn
) &union_map_power
} },
1037 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
1038 (isc_un_op_fn
) &union_set_sample
} },
1039 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
1040 (isc_un_op_fn
) &union_map_sample
} },
1041 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
1042 (isc_un_op_fn
) &union_set_scan
} },
1043 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
1044 (isc_un_op_fn
) &union_map_scan
} },
1045 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
1046 isl_obj_union_pw_qpolynomial
,
1047 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
1048 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
1049 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
1050 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
1051 (isc_un_op_fn
) &isl_union_set_unwrap
} },
1052 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
1053 (isc_un_op_fn
) &isl_union_map_wrap
} },
1054 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
1055 (isc_un_op_fn
) &isl_union_map_zip
} },
1059 struct isl_named_obj
{
1064 static void free_obj(struct isl_obj obj
)
1066 obj
.type
->free(obj
.v
);
1069 static int same_name(const void *entry
, const void *val
)
1071 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1073 return !strcmp(named
->name
, val
);
1076 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1077 char *name
, struct isl_obj obj
)
1079 struct isl_hash_table_entry
*entry
;
1081 struct isl_named_obj
*named
;
1083 name_hash
= isl_hash_string(isl_hash_init(), name
);
1084 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1088 named
= entry
->data
;
1089 free_obj(named
->obj
);
1092 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1096 entry
->data
= named
;
1107 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1108 struct isl_hash_table
*table
, char *name
)
1110 struct isl_obj obj
= { isl_obj_none
, NULL
};
1111 struct isl_hash_table_entry
*entry
;
1114 name_hash
= isl_hash_string(isl_hash_init(), name
);
1115 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1117 } else if (entry
!= isl_hash_table_entry_none
) {
1118 struct isl_named_obj
*named
;
1119 named
= entry
->data
;
1121 } else if (isdigit(name
[0]))
1122 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1124 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1127 obj
.v
= obj
.type
->copy(obj
.v
);
1131 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1133 if (obj
.type
== super
)
1135 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1137 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1139 if (obj
.type
== isl_obj_schedule
&& super
== isl_obj_union_map
)
1141 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
1142 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
1143 int is_set
= isl_space_is_set(space
);
1144 isl_space_free(space
);
1147 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1148 super
== isl_obj_union_pw_qpolynomial
)
1150 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1151 super
== isl_obj_union_pw_qpolynomial_fold
)
1153 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1155 if (obj
.type
== isl_obj_list
) {
1156 struct isl_list
*list
= obj
.v
;
1157 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1158 return is_subtype(list
->obj
[0], super
);
1160 if (super
== isl_obj_str
)
1165 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1167 struct isl_list
*list
= obj
.v
;
1170 obj
.v
= obj
.type
->copy(obj
.v
);
1172 isl_list_free(list
);
1177 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1180 if (obj
.type
== type
)
1182 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1183 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1184 obj
.type
= isl_obj_union_set
;
1185 obj
.v
= isl_union_set_from_set(set
);
1188 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1189 obj
.type
= isl_obj_union_map
;
1190 obj
.v
= isl_union_map_from_map(obj
.v
);
1193 if (obj
.type
== isl_obj_schedule
&& type
== isl_obj_union_map
) {
1194 obj
.type
= isl_obj_union_map
;
1195 obj
.v
= schedule_map(obj
.v
);
1198 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1199 obj
.type
= isl_obj_union_set
;
1200 obj
.v
= isl_union_set_from_set(obj
.v
);
1203 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1204 type
== isl_obj_union_pw_qpolynomial
) {
1205 obj
.type
= isl_obj_union_pw_qpolynomial
;
1206 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1209 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1210 type
== isl_obj_union_pw_qpolynomial_fold
) {
1211 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1212 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1215 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1216 if (type
== isl_obj_union_map
) {
1217 obj
.type
= isl_obj_union_map
;
1220 if (type
== isl_obj_union_pw_qpolynomial
) {
1221 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1222 isl_union_set_free(obj
.v
);
1223 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1224 obj
.type
= isl_obj_union_pw_qpolynomial
;
1227 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1228 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1229 isl_union_set_free(obj
.v
);
1230 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1232 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1236 if (obj
.type
== isl_obj_list
) {
1237 struct isl_list
*list
= obj
.v
;
1238 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1239 return convert(ctx
, obj_at(obj
, 0), type
);
1241 if (type
== isl_obj_str
) {
1246 p
= isl_printer_to_str(ctx
);
1249 p
= obj
.type
->print(p
, obj
.v
);
1250 s
= isl_printer_get_str(p
);
1251 isl_printer_free(p
);
1253 str
= isl_str_from_string(ctx
, s
);
1258 obj
.type
= isl_obj_str
;
1264 obj
.type
= isl_obj_none
;
1269 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1274 struct isl_token
*tok
, *tok2
;
1276 tok
= isl_stream_next_token(s
);
1280 for (i
= 0; ; ++i
) {
1283 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1285 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1288 isl_token_free(tok
);
1292 for (i
= 0; ; ++i
) {
1293 if (!named_bin_ops
[i
].name
)
1295 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1297 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1300 isl_token_free(tok
);
1301 return &named_bin_ops
[i
].op
;
1304 for (i
= 0; ; ++i
) {
1305 if (!compound_bin_ops
[i
].full
)
1307 if (compound_bin_ops
[i
].op1
!= isl_token_get_type(tok
))
1310 tok2
= isl_stream_next_token(s
);
1312 if (compound_bin_ops
[i
].op2
!= isl_token_get_type(tok2
))
1314 if (!is_subtype(lhs
, compound_bin_ops
[i
].op
.lhs
))
1317 isl_token_free(tok2
);
1318 isl_token_free(tok
);
1319 return &compound_bin_ops
[i
].op
;
1323 isl_stream_push_token(s
, tok2
);
1324 isl_stream_push_token(s
, tok
);
1329 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1332 struct isl_token
*tok
;
1334 tok
= isl_stream_next_token(s
);
1338 for (i
= 0; ; ++i
) {
1339 if (!named_un_ops
[i
].name
)
1341 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1344 isl_token_free(tok
);
1345 return &named_un_ops
[i
].op
;
1348 isl_stream_push_token(s
, tok
);
1353 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1358 for (i
= 0; ; ++i
) {
1359 if (!named_un_ops
[i
].name
)
1361 if (named_un_ops
[i
].op
.op
!= like
->op
)
1363 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1366 return &named_un_ops
[i
].op
;
1372 static int is_assign(struct isl_stream
*s
)
1374 struct isl_token
*tok
;
1375 struct isl_token
*tok2
;
1378 tok
= isl_stream_next_token(s
);
1381 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1382 isl_stream_push_token(s
, tok
);
1386 tok2
= isl_stream_next_token(s
);
1388 isl_stream_push_token(s
, tok
);
1391 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1392 isl_stream_push_token(s
, tok2
);
1393 isl_stream_push_token(s
, tok
);
1398 static struct isl_obj
read_obj(struct isl_stream
*s
,
1399 struct isl_hash_table
*table
);
1400 static struct isl_obj
read_expr(struct isl_stream
*s
,
1401 struct isl_hash_table
*table
);
1403 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1404 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1407 struct isl_obj obj
= { isl_obj_none
, NULL
};
1409 obj
= read_obj(s
, table
);
1413 op
= find_matching_un_op(op
, obj
);
1415 ctx
= isl_stream_get_ctx(s
);
1417 isl_die(ctx
, isl_error_invalid
,
1418 "no such unary operator defined on given operand",
1421 obj
= convert(ctx
, obj
, op
->arg
);
1422 obj
.v
= op
->fn(obj
.v
);
1428 obj
.type
= isl_obj_none
;
1433 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1435 struct isl_list
*list
;
1438 if (obj
.type
!= isl_obj_union_map
)
1439 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1440 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1441 list
= isl_list_alloc(ctx
, 2);
1445 list
->obj
[0].type
= isl_obj_union_map
;
1446 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1447 list
->obj
[1].type
= isl_obj_bool
;
1448 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
1450 obj
.type
= isl_obj_list
;
1451 if (exact
< 0 || !list
->obj
[0].v
)
1457 obj
.type
= isl_obj_none
;
1462 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1464 struct isl_list
*list
= obj
.v
;
1465 struct isl_token
*tok
;
1470 tok
= isl_stream_next_token(s
);
1471 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1472 isl_stream_error(s
, tok
, "expecting index");
1474 isl_stream_push_token(s
, tok
);
1477 ctx
= isl_stream_get_ctx(s
);
1478 v
= isl_token_get_val(ctx
, tok
);
1479 i
= isl_val_get_num_si(v
);
1481 isl_token_free(tok
);
1482 isl_assert(ctx
, i
< list
->n
, goto error
);
1483 if (isl_stream_eat(s
, ']'))
1486 return obj_at(obj
, i
);
1489 obj
.type
= isl_obj_none
;
1494 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1495 struct isl_hash_table
*table
)
1500 obj
= read_expr(s
, table
);
1501 ctx
= isl_stream_get_ctx(s
);
1502 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1503 is_subtype(obj
, isl_obj_union_map
), goto error
);
1505 if (obj
.type
== isl_obj_list
) {
1506 struct isl_list
*list
= obj
.v
;
1507 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1508 obj
= obj_at(obj
, 0);
1510 if (obj
.type
== isl_obj_set
)
1511 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1512 else if (obj
.type
== isl_obj_map
)
1513 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1514 if (obj
.type
== isl_obj_union_set
) {
1515 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1517 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1521 if (isl_stream_eat(s
, ')'))
1526 isl_union_map_free(umap
);
1529 obj
.type
= isl_obj_none
;
1534 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1535 __isl_take isl_union_set
*uset
)
1537 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1538 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1540 obj
.type
= isl_obj_list
;
1541 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1546 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1547 __isl_take isl_union_map
*umap
)
1549 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1550 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1552 obj
.type
= isl_obj_list
;
1553 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1558 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1559 struct isl_obj obj
, struct isl_hash_table
*table
)
1564 arg
= read_expr(s
, table
);
1565 ctx
= isl_stream_get_ctx(s
);
1566 if (!is_subtype(arg
, isl_obj_union_map
) &&
1567 !is_subtype(arg
, isl_obj_union_set
))
1568 isl_die(ctx
, isl_error_invalid
,
1569 "expecting set of map argument", goto error
);
1571 if (arg
.type
== isl_obj_list
) {
1572 struct isl_list
*list
= arg
.v
;
1573 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1574 arg
= obj_at(arg
, 0);
1576 if (arg
.type
== isl_obj_set
)
1577 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1578 else if (arg
.type
== isl_obj_map
)
1579 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1580 if (arg
.type
== isl_obj_union_set
)
1581 obj
= apply_fun_set(obj
, arg
.v
);
1583 obj
= apply_fun_map(obj
, arg
.v
);
1587 if (isl_stream_eat(s
, ')'))
1595 obj
.type
= isl_obj_none
;
1600 struct add_vertex_data
{
1601 struct isl_list
*list
;
1605 static isl_stat
add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1607 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1611 ma
= isl_vertex_get_expr(vertex
);
1612 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1614 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1615 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1618 isl_vertex_free(vertex
);
1623 static isl_stat
set_vertices(__isl_take isl_set
*set
, void *user
)
1626 isl_basic_set
*hull
;
1627 isl_vertices
*vertices
= NULL
;
1628 struct isl_list
*list
= NULL
;
1630 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1632 set
= isl_set_remove_divs(set
);
1633 hull
= isl_set_convex_hull(set
);
1634 vertices
= isl_basic_set_compute_vertices(hull
);
1635 isl_basic_set_free(hull
);
1639 ctx
= isl_vertices_get_ctx(vertices
);
1640 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1645 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1647 data
->list
= isl_list_concat(list
, data
->list
);
1649 isl_vertices_free(vertices
);
1654 isl_vertices_free(vertices
);
1655 return isl_stat_error
;
1658 static struct isl_obj
vertices(struct isl_stream
*s
,
1659 struct isl_hash_table
*table
)
1663 struct isl_list
*list
= NULL
;
1664 isl_union_set
*uset
= NULL
;
1665 struct add_vertex_data data
= { NULL
};
1667 obj
= read_expr(s
, table
);
1668 ctx
= isl_stream_get_ctx(s
);
1669 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1670 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1674 list
= isl_list_alloc(ctx
, 0);
1680 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1683 isl_union_set_free(uset
);
1685 obj
.type
= isl_obj_list
;
1690 isl_union_set_free(uset
);
1691 isl_list_free(data
.list
);
1693 obj
.type
= isl_obj_none
;
1698 static struct isl_obj
type_of(struct isl_stream
*s
,
1699 struct isl_hash_table
*table
)
1702 const char *type
= "unknown";
1704 obj
= read_expr(s
, table
);
1706 if (obj
.type
== isl_obj_map
||
1707 obj
.type
== isl_obj_union_map
)
1709 if (obj
.type
== isl_obj_set
||
1710 obj
.type
== isl_obj_union_set
)
1712 if (obj
.type
== isl_obj_pw_multi_aff
)
1713 type
= "piecewise multi-quasiaffine expression";
1714 if (obj
.type
== isl_obj_pw_qpolynomial
||
1715 obj
.type
== isl_obj_union_pw_qpolynomial
)
1716 type
= "piecewise quasipolynomial";
1717 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1718 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1719 type
= "piecewise quasipolynomial fold";
1720 if (obj
.type
== isl_obj_list
)
1722 if (obj
.type
== isl_obj_bool
)
1724 if (obj
.type
== isl_obj_str
)
1726 if (obj
.type
== isl_obj_val
)
1728 if (obj
.type
== isl_obj_schedule
)
1732 obj
.type
= isl_obj_str
;
1733 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1738 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1739 struct isl_hash_table
*table
)
1744 obj
= read_obj(s
, table
);
1745 ctx
= isl_stream_get_ctx(s
);
1746 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1747 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1754 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1755 struct isl_hash_table
*table
)
1760 obj
= read_obj(s
, table
);
1761 ctx
= isl_stream_get_ctx(s
);
1762 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1763 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1770 /* Read a schedule in the form of either a schedule (tree) or a union map
1771 * from "s" and store the schedule in "access".
1773 static __isl_give isl_union_access_info
*access_info_set_schedule(
1774 __isl_take isl_union_access_info
*access
, struct isl_stream
*s
,
1775 struct isl_hash_table
*table
)
1780 obj
= read_obj(s
, table
);
1781 if (obj
.type
== isl_obj_schedule
)
1782 return isl_union_access_info_set_schedule(access
, obj
.v
);
1783 ctx
= isl_stream_get_ctx(s
);
1784 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1786 return isl_union_access_info_set_schedule_map(access
, obj
.v
);
1789 static struct isl_obj
last_any(struct isl_stream
*s
,
1790 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1791 __isl_take isl_union_map
*may_source
)
1793 struct isl_obj obj
= { isl_obj_none
, NULL
};
1794 isl_union_access_info
*access
;
1795 isl_union_flow
*flow
;
1796 isl_union_map
*sink
= NULL
;
1797 isl_union_map
*may_dep
;
1799 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1802 sink
= read_map(s
, table
);
1806 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1809 access
= isl_union_access_info_from_sink(sink
);
1810 access
= isl_union_access_info_set_must_source(access
, must_source
);
1811 access
= isl_union_access_info_set_may_source(access
, may_source
);
1812 access
= access_info_set_schedule(access
, s
, table
);
1813 flow
= isl_union_access_info_compute_flow(access
);
1814 may_dep
= isl_union_flow_get_may_dependence(flow
);
1815 isl_union_flow_free(flow
);
1820 obj
.type
= isl_obj_union_map
;
1825 isl_union_map_free(may_source
);
1826 isl_union_map_free(must_source
);
1827 isl_union_map_free(sink
);
1829 obj
.type
= isl_obj_none
;
1834 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1836 struct isl_obj obj
= { isl_obj_none
, NULL
};
1837 isl_union_access_info
*access
;
1838 isl_union_flow
*flow
;
1839 isl_union_map
*may_source
= NULL
;
1840 isl_union_map
*sink
= NULL
;
1841 isl_union_map
*may_dep
;
1843 may_source
= read_map(s
, table
);
1847 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1848 isl_union_map
*must_source
;
1849 must_source
= read_map(s
, table
);
1852 return last_any(s
, table
, must_source
, may_source
);
1855 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1858 sink
= read_map(s
, table
);
1862 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1865 access
= isl_union_access_info_from_sink(sink
);
1866 access
= isl_union_access_info_set_may_source(access
, may_source
);
1867 access
= access_info_set_schedule(access
, s
, table
);
1868 flow
= isl_union_access_info_compute_flow(access
);
1869 may_dep
= isl_union_flow_get_may_dependence(flow
);
1870 isl_union_flow_free(flow
);
1875 obj
.type
= isl_obj_union_map
;
1880 isl_union_map_free(may_source
);
1881 isl_union_map_free(sink
);
1883 obj
.type
= isl_obj_none
;
1888 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1890 struct isl_obj obj
= { isl_obj_none
, NULL
};
1891 struct isl_list
*list
= NULL
;
1892 isl_union_access_info
*access
;
1893 isl_union_flow
*flow
;
1894 isl_union_map
*must_source
= NULL
;
1895 isl_union_map
*sink
= NULL
;
1896 isl_union_map
*must_dep
;
1897 isl_union_map
*must_no_source
;
1899 must_source
= read_map(s
, table
);
1903 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1904 isl_union_map
*may_source
;
1905 may_source
= read_map(s
, table
);
1908 return last_any(s
, table
, must_source
, may_source
);
1911 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1915 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1918 sink
= read_map(s
, table
);
1922 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1925 access
= isl_union_access_info_from_sink(sink
);
1926 access
= isl_union_access_info_set_must_source(access
, must_source
);
1927 access
= access_info_set_schedule(access
, s
, table
);
1928 flow
= isl_union_access_info_compute_flow(access
);
1929 must_dep
= isl_union_flow_get_must_dependence(flow
);
1930 must_no_source
= isl_union_flow_get_must_no_source(flow
);
1931 isl_union_flow_free(flow
);
1933 list
->obj
[0].type
= isl_obj_union_map
;
1934 list
->obj
[0].v
= must_dep
;
1935 list
->obj
[1].type
= isl_obj_union_map
;
1936 list
->obj
[1].v
= must_no_source
;
1938 if (!must_dep
|| !must_no_source
) {
1939 isl_list_free(list
);
1944 obj
.type
= isl_obj_list
;
1948 isl_list_free(list
);
1949 isl_union_map_free(must_source
);
1950 isl_union_map_free(sink
);
1952 obj
.type
= isl_obj_none
;
1957 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1958 struct isl_hash_table
*table
)
1960 isl_union_set
*domain
;
1961 isl_union_map
*validity
;
1962 isl_union_map
*proximity
;
1964 domain
= read_set(s
, table
);
1968 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1969 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1972 isl_union_map
*umap
;
1973 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1974 umap
= read_map(s
, table
);
1975 validity
= isl_union_map_union(validity
, umap
);
1976 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1977 umap
= read_map(s
, table
);
1978 proximity
= isl_union_map_union(proximity
, umap
);
1983 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1986 static struct isl_obj
schedule(struct isl_stream
*s
,
1987 struct isl_hash_table
*table
)
1989 struct isl_obj obj
= { isl_obj_none
, NULL
};
1990 isl_schedule
*schedule
;
1992 schedule
= get_schedule(s
, table
);
1995 obj
.type
= isl_obj_schedule
;
2000 /* Read a schedule for code generation in the form of either
2001 * a schedule tree or a union map.
2003 static struct isl_obj
get_codegen_schedule(struct isl_stream
*s
,
2004 struct isl_hash_table
*table
)
2009 obj
= read_obj(s
, table
);
2010 ctx
= isl_stream_get_ctx(s
);
2012 if (obj
.type
== isl_obj_schedule
)
2014 if (is_subtype(obj
, isl_obj_union_map
))
2015 return convert(ctx
, obj
, isl_obj_union_map
);
2019 obj
.type
= isl_obj_none
;
2020 isl_die(ctx
, isl_error_invalid
, "expecting schedule or map",
2024 /* Generate an AST for the given schedule and options and return the AST.
2026 static __isl_give isl_ast_node
*get_ast_from_union_map(
2027 __isl_take isl_union_map
*schedule
, __isl_take isl_union_map
*options
)
2031 isl_ast_build
*build
;
2034 space
= isl_union_map_get_space(schedule
);
2035 context
= isl_set_universe(isl_space_params(space
));
2037 build
= isl_ast_build_from_context(context
);
2038 build
= isl_ast_build_set_options(build
, options
);
2039 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
2040 isl_ast_build_free(build
);
2045 /* Generate an AST for the given schedule and return the AST.
2047 static __isl_give isl_ast_node
*get_ast_from_schedule(
2048 __isl_take isl_schedule
*schedule
)
2050 isl_ast_build
*build
;
2053 build
= isl_ast_build_alloc(isl_schedule_get_ctx(schedule
));
2054 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2055 isl_ast_build_free(build
);
2060 /* Print the AST "tree" on the printer "p".
2062 static __isl_give isl_printer
*print_ast(__isl_take isl_printer
*p
,
2063 __isl_take isl_ast_node
*tree
)
2067 format
= isl_printer_get_output_format(p
);
2068 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
2069 p
= isl_printer_print_ast_node(p
, tree
);
2070 p
= isl_printer_set_output_format(p
, format
);
2072 isl_ast_node_free(tree
);
2077 /* Perform the codegen operation.
2078 * In particular, read a schedule, check if the user has specified any options
2079 * and then generate an AST from the schedule (and options) and print it.
2080 * In case the schedule is specified as a schedule tree, the AST generation
2081 * options are embedded in the schedule, so they are not read in separately.
2083 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
2084 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2089 obj
= get_codegen_schedule(s
, table
);
2093 if (obj
.type
== isl_obj_schedule
) {
2094 isl_schedule
*schedule
= obj
.v
;
2096 tree
= get_ast_from_schedule(schedule
);
2098 isl_union_map
*schedule
= obj
.v
;
2099 isl_union_map
*options
;
2101 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
2102 options
= read_map(s
, table
);
2104 options
= isl_union_map_empty(
2105 isl_union_map_get_space(schedule
));
2107 tree
= get_ast_from_union_map(schedule
, options
);
2111 p
= print_ast(p
, tree
);
2113 isl_stream_eat(s
, ';');
2118 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
2120 struct isl_token
*tok
;
2125 ctx
= isl_stream_get_ctx(s
);
2126 if (isl_stream_eat_if_available(s
, '+'))
2127 return transitive_closure(ctx
, obj
);
2129 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
2130 if (obj
.type
!= isl_obj_union_map
)
2131 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2133 if (isl_stream_eat_if_available(s
, '-'))
2135 tok
= isl_stream_next_token(s
);
2136 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
2137 isl_stream_error(s
, tok
, "expecting integer exponent");
2139 isl_stream_push_token(s
, tok
);
2143 v
= isl_token_get_val(ctx
, tok
);
2146 if (isl_val_is_zero(v
)) {
2147 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2150 isl_stream_push_token(s
, tok
);
2154 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2155 isl_token_free(tok
);
2162 obj
.type
= isl_obj_none
;
2167 static struct isl_obj
check_assert(struct isl_stream
*s
,
2168 struct isl_hash_table
*table
)
2173 obj
= read_expr(s
, table
);
2174 ctx
= isl_stream_get_ctx(s
);
2175 if (obj
.type
!= isl_obj_bool
)
2176 isl_die(ctx
, isl_error_invalid
,
2177 "expecting boolean expression", goto error
);
2178 if (obj
.v
!= &iscc_bool_true
)
2179 isl_die(ctx
, isl_error_unknown
,
2180 "assertion failed", abort());
2183 obj
.type
= isl_obj_none
;
2188 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2192 struct isl_token
*tok
;
2193 struct isl_stream
*s_file
;
2194 struct iscc_options
*options
;
2198 tok
= isl_stream_next_token(s
);
2199 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2200 isl_stream_error(s
, tok
, "expecting filename");
2201 isl_token_free(tok
);
2205 ctx
= isl_stream_get_ctx(s
);
2206 options
= isl_ctx_peek_iscc_options(ctx
);
2207 if (!options
|| !options
->io
) {
2208 isl_token_free(tok
);
2209 isl_die(ctx
, isl_error_invalid
,
2210 "read operation not allowed", goto error
);
2213 name
= isl_token_get_str(ctx
, tok
);
2214 isl_token_free(tok
);
2215 file
= fopen(name
, "r");
2217 isl_assert(ctx
, file
, goto error
);
2219 s_file
= isl_stream_new_file(ctx
, file
);
2225 obj
= isl_stream_read_obj(s_file
);
2227 isl_stream_free(s_file
);
2232 obj
.type
= isl_obj_none
;
2237 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2238 struct isl_hash_table
*table
)
2240 struct isl_obj obj
= { isl_obj_none
, NULL
};
2241 struct isl_token
*tok
;
2242 struct iscc_options
*options
;
2248 tok
= isl_stream_next_token(s
);
2249 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2250 isl_stream_error(s
, tok
, "expecting filename");
2251 isl_token_free(tok
);
2255 obj
= read_expr(s
, table
);
2257 ctx
= isl_stream_get_ctx(s
);
2258 options
= isl_ctx_peek_iscc_options(ctx
);
2259 if (!options
|| !options
->io
) {
2260 isl_token_free(tok
);
2261 isl_die(ctx
, isl_error_invalid
,
2262 "write operation not allowed", goto error
);
2265 name
= isl_token_get_str(ctx
, tok
);
2266 isl_token_free(tok
);
2267 file
= fopen(name
, "w");
2270 isl_die(ctx
, isl_error_unknown
,
2271 "could not open file for writing", goto error
);
2273 p
= isl_printer_to_file(ctx
, file
);
2274 p
= isl_printer_set_output_format(p
, options
->format
);
2275 p
= obj
.type
->print(p
, obj
.v
);
2276 p
= isl_printer_end_line(p
);
2277 isl_printer_free(p
);
2282 obj
.type
= isl_obj_none
;
2287 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2289 struct isl_token
*tok
;
2290 struct isl_obj obj
= { isl_obj_none
, NULL
};
2292 tok
= isl_stream_next_token(s
);
2295 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2297 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2300 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2301 isl_token_free(tok
);
2303 obj
.type
= isl_obj_str
;
2305 isl_stream_push_token(s
, tok
);
2308 isl_token_free(tok
);
2312 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2314 struct isl_token
*tok
;
2315 struct isl_obj obj
= { isl_obj_none
, NULL
};
2318 tok
= isl_stream_next_token(s
);
2321 type
= isl_token_get_type(tok
);
2322 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2323 int is_true
= type
== ISL_TOKEN_TRUE
;
2324 isl_token_free(tok
);
2325 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2326 obj
.type
= isl_obj_bool
;
2328 isl_stream_push_token(s
, tok
);
2332 static __isl_give
char *read_ident(struct isl_stream
*s
)
2336 struct isl_token
*tok
, *tok2
;
2338 name
= isl_stream_read_ident_if_available(s
);
2342 tok
= isl_stream_next_token(s
);
2345 if (isl_token_get_type(tok
) != '$') {
2346 isl_stream_push_token(s
, tok
);
2349 tok2
= isl_stream_next_token(s
);
2350 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2352 isl_stream_push_token(s
, tok2
);
2353 isl_stream_push_token(s
, tok
);
2357 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2358 name
= isl_val_to_str(v
);
2360 isl_token_free(tok
);
2361 isl_token_free(tok2
);
2366 static struct isl_obj
read_list(struct isl_stream
*s
,
2367 struct isl_hash_table
*table
, struct isl_obj obj
)
2369 struct isl_list
*list
;
2371 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2375 list
->obj
[1] = read_obj(s
, table
);
2377 obj
.type
= isl_obj_list
;
2379 if (!list
->obj
[1].v
)
2382 while (isl_stream_eat_if_available(s
, ',')) {
2383 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2391 obj
.type
= isl_obj_none
;
2396 static struct isl_obj
read_obj(struct isl_stream
*s
,
2397 struct isl_hash_table
*table
)
2400 struct isl_obj obj
= { isl_obj_none
, NULL
};
2402 struct isc_un_op
*op
= NULL
;
2404 obj
= read_string_if_available(s
);
2407 obj
= read_bool_if_available(s
);
2410 ctx
= isl_stream_get_ctx(s
);
2411 if (isl_stream_eat_if_available(s
, '(')) {
2412 if (isl_stream_next_token_is(s
, ')')) {
2413 obj
.type
= isl_obj_list
;
2414 obj
.v
= isl_list_alloc(ctx
, 0);
2416 obj
= read_expr(s
, table
);
2417 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2418 obj
= read_list(s
, table
, obj
);
2420 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2423 op
= read_prefix_un_op_if_available(s
);
2425 return read_un_op_expr(s
, table
, op
);
2427 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2428 return check_assert(s
, table
);
2429 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2430 return read_from_file(s
);
2431 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2432 return write_to_file(s
, table
);
2433 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2434 return vertices(s
, table
);
2435 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2436 return any(s
, table
);
2437 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2438 return last(s
, table
);
2439 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2440 return schedule(s
, table
);
2441 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2442 return type_of(s
, table
);
2444 name
= read_ident(s
);
2446 obj
= stored_obj(ctx
, table
, name
);
2448 obj
= isl_stream_read_obj(s
);
2453 if (isl_stream_eat_if_available(s
, '^'))
2454 obj
= power(s
, obj
);
2455 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2456 obj
= obj_at_index(s
, obj
);
2457 else if (is_subtype(obj
, isl_obj_union_map
) &&
2458 isl_stream_eat_if_available(s
, '(')) {
2459 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2460 obj
= apply(s
, obj
.v
, table
);
2461 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2462 isl_stream_eat_if_available(s
, '(')) {
2463 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2464 obj
= apply_fun(s
, obj
, table
);
2465 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2466 isl_stream_eat_if_available(s
, '(')) {
2467 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2468 obj
= apply_fun(s
, obj
, table
);
2474 obj
.type
= isl_obj_none
;
2479 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2480 struct isl_obj lhs
, struct isl_obj rhs
)
2484 for (i
= 0; ; ++i
) {
2487 if (bin_ops
[i
].op
!= like
->op
)
2489 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2491 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2497 for (i
= 0; ; ++i
) {
2498 if (!named_bin_ops
[i
].name
)
2500 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2502 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2504 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2507 return &named_bin_ops
[i
].op
;
2510 for (i
= 0; ; ++i
) {
2511 if (!compound_bin_ops
[i
].full
)
2513 if (compound_bin_ops
[i
].op
.op
!= like
->op
)
2515 if (!is_subtype(lhs
, compound_bin_ops
[i
].op
.lhs
))
2517 if (!is_subtype(rhs
, compound_bin_ops
[i
].op
.rhs
))
2520 return &compound_bin_ops
[i
].op
;
2526 static int next_is_neg_int(struct isl_stream
*s
)
2528 struct isl_token
*tok
;
2531 tok
= isl_stream_next_token(s
);
2532 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2534 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2535 ret
= isl_val_is_neg(v
);
2539 isl_stream_push_token(s
, tok
);
2544 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2545 struct isl_obj lhs
, struct isl_obj rhs
)
2549 lhs
= convert(ctx
, lhs
, op
->lhs
);
2550 rhs
= convert(ctx
, rhs
, op
->rhs
);
2551 if (op
->res
!= isl_obj_bool
)
2552 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2554 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2557 obj
.v
= iscc_bool_from_int(res
);
2564 static struct isl_obj
read_expr(struct isl_stream
*s
,
2565 struct isl_hash_table
*table
)
2568 struct isl_obj obj
= { isl_obj_none
, NULL
};
2569 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2571 obj
= read_obj(s
, table
);
2572 ctx
= isl_stream_get_ctx(s
);
2574 struct isc_bin_op
*op
= NULL
;
2576 op
= read_bin_op_if_available(s
, obj
);
2580 right_obj
= read_obj(s
, table
);
2582 op
= find_matching_bin_op(op
, obj
, right_obj
);
2585 isl_die(ctx
, isl_error_invalid
,
2586 "no such binary operator defined on given operands",
2589 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2592 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2593 right_obj
= read_obj(s
, table
);
2594 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2599 free_obj(right_obj
);
2601 obj
.type
= isl_obj_none
;
2606 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2607 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2609 /* Print "obj" to the printer "p".
2610 * If the object is a schedule, then print it in block format.
2612 static __isl_give isl_printer
*print_obj(__isl_take isl_printer
*p
,
2615 if (obj
.type
!= isl_obj_schedule
)
2616 return obj
.type
->print(p
, obj
.v
);
2618 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
2619 p
= obj
.type
->print(p
, obj
.v
);
2620 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2625 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2626 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2629 struct isl_obj obj
= { isl_obj_none
, NULL
};
2637 if (isl_stream_is_empty(s
))
2640 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2641 return source_file(s
, table
, p
);
2642 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2643 return codegen(s
, table
, p
);
2645 assign
= is_assign(s
);
2647 lhs
= isl_stream_read_ident_if_available(s
);
2648 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2650 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2655 obj
= read_expr(s
, table
);
2656 ctx
= isl_stream_get_ctx(s
);
2657 if (isl_stream_eat(s
, ';'))
2661 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2662 p
= print_obj(p
, obj
);
2663 p
= isl_printer_end_line(p
);
2668 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2669 static int count
= 0;
2670 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2671 lhs
= strdup(buf
+ 1);
2673 p
= isl_printer_print_str(p
, buf
);
2674 p
= isl_printer_print_str(p
, " := ");
2675 p
= obj
.type
->print(p
, obj
.v
);
2676 p
= isl_printer_end_line(p
);
2678 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2683 isl_stream_flush_tokens(s
);
2684 isl_stream_skip_line(s
);
2690 static isl_stat
free_cb(void **entry
, void *user
)
2692 struct isl_named_obj
*named
= *entry
;
2694 free_obj(named
->obj
);
2701 static void register_named_ops(struct isl_stream
*s
)
2705 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2706 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2707 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2710 for (i
= 0; ; ++i
) {
2711 if (!named_un_ops
[i
].name
)
2713 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2714 named_un_ops
[i
].name
);
2715 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2718 for (i
= 0; ; ++i
) {
2719 if (!named_bin_ops
[i
].name
)
2721 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2722 named_bin_ops
[i
].name
);
2723 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2726 for (i
= 0; ; ++i
) {
2727 if (!compound_bin_ops
[i
].full
)
2729 compound_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2730 compound_bin_ops
[i
].full
);
2731 assert(compound_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2735 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2736 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2739 struct isl_token
*tok
;
2740 struct isl_stream
*s_file
;
2741 struct iscc_options
*options
;
2745 tok
= isl_stream_next_token(s
);
2746 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2747 isl_stream_error(s
, tok
, "expecting filename");
2748 isl_token_free(tok
);
2752 isl_stream_eat(s
, ';');
2754 ctx
= isl_stream_get_ctx(s
);
2755 options
= isl_ctx_peek_iscc_options(ctx
);
2756 if (!options
|| !options
->io
) {
2757 isl_token_free(tok
);
2758 isl_die(ctx
, isl_error_invalid
,
2759 "source operation not allowed", return p
);
2762 name
= isl_token_get_str(ctx
, tok
);
2763 isl_token_free(tok
);
2764 file
= fopen(name
, "r");
2766 isl_assert(ctx
, file
, return p
);
2768 s_file
= isl_stream_new_file(ctx
, file
);
2774 register_named_ops(s_file
);
2776 while (!isl_stream_is_empty(s_file
))
2777 p
= read_line(s_file
, table
, p
, 0);
2779 isl_stream_free(s_file
);
2785 int main(int argc
, char **argv
)
2787 struct isl_ctx
*ctx
;
2788 struct isl_stream
*s
;
2789 struct isl_hash_table
*table
;
2790 struct iscc_options
*options
;
2792 int tty
= isatty(0);
2794 options
= iscc_options_new_with_defaults();
2797 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2798 pet_options_set_autodetect(ctx
, 1);
2799 pet_options_set_encapsulate_dynamic_control(ctx
, 1);
2800 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2801 s
= isl_stream_new_file(ctx
, stdin
);
2803 table
= isl_hash_table_alloc(ctx
, 10);
2805 p
= isl_printer_to_file(ctx
, stdout
);
2806 p
= isl_printer_set_output_format(p
, options
->format
);
2809 register_named_ops(s
);
2811 install_signal_handler(ctx
);
2816 empty
= isl_stream_is_empty(s
);
2817 if (empty
&& isl_ctx_last_error(ctx
) != isl_error_abort
)
2820 p
= read_line(s
, table
, p
, tty
);
2821 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2822 fprintf(stderr
, "Interrupted\n");
2823 isl_ctx_resume(ctx
);
2824 isl_ctx_reset_error(ctx
);
2828 remove_signal_handler(ctx
);
2830 isl_printer_free(p
);
2831 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2832 isl_hash_table_free(ctx
, table
);