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
} },
676 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
678 return isl_set_from_basic_set(isl_union_set_sample(uset
));
681 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
683 return isl_map_from_basic_map(isl_union_map_sample(umap
));
686 static __isl_give
struct isl_list
*union_map_power(
687 __isl_take isl_union_map
*umap
)
690 struct isl_list
*list
;
693 ctx
= isl_union_map_get_ctx(umap
);
694 list
= isl_list_alloc(ctx
, 2);
698 list
->obj
[0].type
= isl_obj_union_map
;
699 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
700 list
->obj
[1].type
= isl_obj_bool
;
701 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
702 if (exact
< 0 || !list
->obj
[0].v
)
707 isl_union_map_free(umap
);
713 /* Compute a lower or upper bound on "upwqp" depending on "type" and
714 * return a list containing two elements, the bound and a boolean
715 * indicating whether the result is tight.
717 static __isl_give
struct isl_list
*union_pw_qpolynomial_bound(
718 __isl_take isl_union_pw_qpolynomial
*upwqp
, enum isl_fold type
)
721 struct isl_list
*list
;
724 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
725 list
= isl_list_alloc(ctx
, 2);
729 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
730 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
, type
, &tight
);
731 list
->obj
[1].type
= isl_obj_bool
;
732 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
733 if (tight
< 0 || !list
->obj
[0].v
)
738 isl_union_pw_qpolynomial_free(upwqp
);
744 /* Compute a lower bound on "upwqp" and return a list containing
745 * two elements, the bound and a booleanindicating whether
746 * the result is tight.
748 static __isl_give
struct isl_list
*union_pw_qpolynomial_lower_bound(
749 __isl_take isl_union_pw_qpolynomial
*upwqp
)
751 return union_pw_qpolynomial_bound(upwqp
, isl_fold_min
);
754 /* Compute a upper bound on "upwqp" and return a list containing
755 * two elements, the bound and a booleanindicating whether
756 * the result is tight.
758 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
759 __isl_take isl_union_pw_qpolynomial
*upwqp
)
761 return union_pw_qpolynomial_bound(upwqp
, isl_fold_max
);
765 /* Collect all statement instances, except those of kill statements.
767 static __isl_give isl_union_set
*collect_non_kill_instances(
768 struct pet_scop
*scop
)
772 isl_union_set
*domain
;
777 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
779 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
780 struct pet_stmt
*stmt
= scop
->stmts
[i
];
782 if (pet_stmt_is_kill(stmt
))
784 domain_i
= isl_set_copy(stmt
->domain
);
785 if (scop
->stmts
[i
]->n_arg
> 0)
786 domain_i
= isl_map_domain(isl_set_unwrap(domain_i
));
787 domain
= isl_union_set_add_set(domain
, domain_i
);
793 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
796 struct isl_list
*list
;
797 struct pet_scop
*scop
;
799 isl_union_map
*may_reads
, *must_writes
, *may_writes
;
800 isl_union_set
*domain
;
801 struct iscc_options
*options
;
807 options
= isl_ctx_peek_iscc_options(ctx
);
808 if (!options
|| !options
->io
) {
810 isl_die(ctx
, isl_error_invalid
,
811 "parse_file operation not allowed", return NULL
);
814 list
= isl_list_alloc(ctx
, 5);
818 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
819 domain
= collect_non_kill_instances(scop
);
820 sched
= pet_scop_get_schedule(scop
);
821 sched
= isl_schedule_intersect_domain(sched
,
822 isl_union_set_copy(domain
));
823 may_reads
= pet_scop_get_may_reads(scop
);
824 may_writes
= pet_scop_get_may_writes(scop
);
825 must_writes
= pet_scop_get_must_writes(scop
);
828 list
->obj
[0].type
= isl_obj_union_set
;
829 list
->obj
[0].v
= domain
;
830 list
->obj
[1].type
= isl_obj_union_map
;
831 list
->obj
[1].v
= must_writes
;
832 list
->obj
[2].type
= isl_obj_union_map
;
833 list
->obj
[2].v
= may_writes
;
834 list
->obj
[3].type
= isl_obj_union_map
;
835 list
->obj
[3].v
= may_reads
;
836 list
->obj
[4].type
= isl_obj_schedule
;
837 list
->obj
[4].v
= sched
;
839 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
840 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
852 static isl_stat
add_point(__isl_take isl_point
*pnt
, void *user
)
854 isl_union_set
**scan
= (isl_union_set
**) user
;
856 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
861 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
865 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
867 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
868 isl_union_set_free(scan
);
872 isl_union_set_free(uset
);
876 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
878 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
881 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
882 __isl_take isl_union_pw_qpolynomial
*upwqp
)
884 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
887 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
888 __isl_take isl_union_pw_qpolynomial
*upwqp
)
890 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
893 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
894 __isl_take isl_union_pw_qpolynomial
*upwqp
)
896 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
899 /* Return the domain of "schedule".
901 static __isl_give isl_union_set
*schedule_domain(
902 __isl_take isl_schedule
*schedule
)
904 isl_union_set
*domain
;
906 domain
= isl_schedule_get_domain(schedule
);
907 isl_schedule_free(schedule
);
912 /* Convert "schedule" to a union map representation.
914 static __isl_give isl_union_map
*schedule_map(__isl_take isl_schedule
*schedule
)
918 map
= isl_schedule_get_map(schedule
);
919 isl_schedule_free(schedule
);
924 typedef void *(*isc_un_op_fn
)(void *arg
);
926 enum isl_token_type op
;
931 struct isc_named_un_op
{
935 struct isc_named_un_op named_un_ops
[] = {
936 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
937 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
938 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
939 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
940 {"card", { -1, isl_obj_union_set
,
941 isl_obj_union_pw_qpolynomial
,
942 (isc_un_op_fn
) &isl_union_set_card
} },
943 {"card", { -1, isl_obj_union_map
,
944 isl_obj_union_pw_qpolynomial
,
945 (isc_un_op_fn
) &isl_union_map_card
} },
946 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
947 (isc_un_op_fn
) &isl_union_set_coalesce
} },
948 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
949 (isc_un_op_fn
) &isl_union_map_coalesce
} },
950 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
951 isl_obj_union_pw_qpolynomial
,
952 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
953 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
954 isl_obj_union_pw_qpolynomial_fold
,
955 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
956 {"coefficients", { -1, isl_obj_union_set
,
958 (isc_un_op_fn
) &isl_union_set_coefficients
} },
959 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
960 (isc_un_op_fn
) &isl_union_set_solutions
} },
961 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
962 (isc_un_op_fn
) &isl_union_map_deltas
} },
963 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
964 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
965 {"dom", { -1, isl_obj_schedule
, isl_obj_union_set
,
966 (isc_un_op_fn
) &schedule_domain
} },
967 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
968 (isc_un_op_fn
) &isl_union_map_domain
} },
969 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
970 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
971 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
973 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
974 {"domain", { -1, isl_obj_schedule
, isl_obj_union_set
,
975 (isc_un_op_fn
) &schedule_domain
} },
976 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
977 (isc_un_op_fn
) &isl_union_map_domain
} },
978 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
980 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
981 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
983 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
984 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
985 (isc_un_op_fn
) &isl_union_map_domain_map
} },
986 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
987 (isc_un_op_fn
) &isl_union_map_range
} },
988 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
989 (isc_un_op_fn
) &isl_union_map_range
} },
990 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
991 (isc_un_op_fn
) &isl_union_map_range_map
} },
992 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
993 (isc_un_op_fn
) &isl_union_set_identity
} },
994 {"lattice_width", { -1, isl_obj_union_set
,
995 isl_obj_union_pw_qpolynomial
,
996 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
997 {"lb", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
998 (isc_un_op_fn
) &union_pw_qpolynomial_lower_bound
} },
999 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
1000 (isc_un_op_fn
) &isl_union_map_lexmin
} },
1001 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
1002 (isc_un_op_fn
) &isl_union_map_lexmax
} },
1003 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
1004 (isc_un_op_fn
) &isl_union_set_lexmin
} },
1005 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
1006 (isc_un_op_fn
) &isl_union_set_lexmax
} },
1007 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
1008 (isc_un_op_fn
) &isl_union_set_lift
} },
1009 {"map", { -1, isl_obj_schedule
, isl_obj_union_map
,
1010 (isc_un_op_fn
) &schedule_map
} },
1011 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
1012 (isc_un_op_fn
) &isl_union_map_params
} },
1013 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
1014 (isc_un_op_fn
) &isl_union_set_params
} },
1015 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
1016 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
1017 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
1018 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
1019 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
1020 isl_obj_union_pw_qpolynomial
,
1021 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
1022 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
1023 isl_obj_union_pw_qpolynomial
,
1024 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
1025 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
1026 isl_obj_union_pw_qpolynomial
,
1027 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
1029 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
1030 (isc_un_op_fn
) &parse
} },
1032 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
1033 (isc_un_op_fn
) &union_map_power
} },
1034 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
1035 (isc_un_op_fn
) &union_set_sample
} },
1036 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
1037 (isc_un_op_fn
) &union_map_sample
} },
1038 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
1039 (isc_un_op_fn
) &union_set_scan
} },
1040 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
1041 (isc_un_op_fn
) &union_map_scan
} },
1042 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
1043 isl_obj_union_pw_qpolynomial
,
1044 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
1045 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
1046 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
1047 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
1048 (isc_un_op_fn
) &isl_union_set_unwrap
} },
1049 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
1050 (isc_un_op_fn
) &isl_union_map_wrap
} },
1051 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
1052 (isc_un_op_fn
) &isl_union_map_zip
} },
1056 struct isl_named_obj
{
1061 static void free_obj(struct isl_obj obj
)
1063 obj
.type
->free(obj
.v
);
1066 static int same_name(const void *entry
, const void *val
)
1068 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
1070 return !strcmp(named
->name
, val
);
1073 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
1074 char *name
, struct isl_obj obj
)
1076 struct isl_hash_table_entry
*entry
;
1078 struct isl_named_obj
*named
;
1080 name_hash
= isl_hash_string(isl_hash_init(), name
);
1081 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
1085 named
= entry
->data
;
1086 free_obj(named
->obj
);
1089 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1093 entry
->data
= named
;
1104 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1105 struct isl_hash_table
*table
, char *name
)
1107 struct isl_obj obj
= { isl_obj_none
, NULL
};
1108 struct isl_hash_table_entry
*entry
;
1111 name_hash
= isl_hash_string(isl_hash_init(), name
);
1112 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1114 struct isl_named_obj
*named
;
1115 named
= entry
->data
;
1117 } else if (isdigit(name
[0]))
1118 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1120 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1123 obj
.v
= obj
.type
->copy(obj
.v
);
1127 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1129 if (obj
.type
== super
)
1131 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1133 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1135 if (obj
.type
== isl_obj_schedule
&& super
== isl_obj_union_map
)
1137 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
1138 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
1139 int is_set
= isl_space_is_set(space
);
1140 isl_space_free(space
);
1143 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1144 super
== isl_obj_union_pw_qpolynomial
)
1146 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1147 super
== isl_obj_union_pw_qpolynomial_fold
)
1149 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1151 if (obj
.type
== isl_obj_list
) {
1152 struct isl_list
*list
= obj
.v
;
1153 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1154 return is_subtype(list
->obj
[0], super
);
1156 if (super
== isl_obj_str
)
1161 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1163 struct isl_list
*list
= obj
.v
;
1166 obj
.v
= obj
.type
->copy(obj
.v
);
1168 isl_list_free(list
);
1173 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1176 if (obj
.type
== type
)
1178 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1179 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1180 obj
.type
= isl_obj_union_set
;
1181 obj
.v
= isl_union_set_from_set(set
);
1184 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1185 obj
.type
= isl_obj_union_map
;
1186 obj
.v
= isl_union_map_from_map(obj
.v
);
1189 if (obj
.type
== isl_obj_schedule
&& type
== isl_obj_union_map
) {
1190 obj
.type
= isl_obj_union_map
;
1191 obj
.v
= schedule_map(obj
.v
);
1194 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1195 obj
.type
= isl_obj_union_set
;
1196 obj
.v
= isl_union_set_from_set(obj
.v
);
1199 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1200 type
== isl_obj_union_pw_qpolynomial
) {
1201 obj
.type
= isl_obj_union_pw_qpolynomial
;
1202 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1205 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1206 type
== isl_obj_union_pw_qpolynomial_fold
) {
1207 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1208 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1211 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1212 if (type
== isl_obj_union_map
) {
1213 obj
.type
= isl_obj_union_map
;
1216 if (type
== isl_obj_union_pw_qpolynomial
) {
1217 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1218 isl_union_set_free(obj
.v
);
1219 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1220 obj
.type
= isl_obj_union_pw_qpolynomial
;
1223 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1224 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1225 isl_union_set_free(obj
.v
);
1226 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1228 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1232 if (obj
.type
== isl_obj_list
) {
1233 struct isl_list
*list
= obj
.v
;
1234 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1235 return convert(ctx
, obj_at(obj
, 0), type
);
1237 if (type
== isl_obj_str
) {
1242 p
= isl_printer_to_str(ctx
);
1245 p
= obj
.type
->print(p
, obj
.v
);
1246 s
= isl_printer_get_str(p
);
1247 isl_printer_free(p
);
1249 str
= isl_str_from_string(ctx
, s
);
1254 obj
.type
= isl_obj_str
;
1260 obj
.type
= isl_obj_none
;
1265 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1270 struct isl_token
*tok
, *tok2
;
1272 tok
= isl_stream_next_token(s
);
1276 for (i
= 0; ; ++i
) {
1279 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1281 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1284 isl_token_free(tok
);
1288 for (i
= 0; ; ++i
) {
1289 if (!named_bin_ops
[i
].name
)
1291 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1293 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1296 isl_token_free(tok
);
1297 return &named_bin_ops
[i
].op
;
1300 for (i
= 0; ; ++i
) {
1301 if (!compound_bin_ops
[i
].full
)
1303 if (compound_bin_ops
[i
].op1
!= isl_token_get_type(tok
))
1306 tok2
= isl_stream_next_token(s
);
1308 if (compound_bin_ops
[i
].op2
!= isl_token_get_type(tok2
))
1310 if (!is_subtype(lhs
, compound_bin_ops
[i
].op
.lhs
))
1313 isl_token_free(tok2
);
1314 isl_token_free(tok
);
1315 return &compound_bin_ops
[i
].op
;
1319 isl_stream_push_token(s
, tok2
);
1320 isl_stream_push_token(s
, tok
);
1325 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1328 struct isl_token
*tok
;
1330 tok
= isl_stream_next_token(s
);
1334 for (i
= 0; ; ++i
) {
1335 if (!named_un_ops
[i
].name
)
1337 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1340 isl_token_free(tok
);
1341 return &named_un_ops
[i
].op
;
1344 isl_stream_push_token(s
, tok
);
1349 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1354 for (i
= 0; ; ++i
) {
1355 if (!named_un_ops
[i
].name
)
1357 if (named_un_ops
[i
].op
.op
!= like
->op
)
1359 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1362 return &named_un_ops
[i
].op
;
1368 static int is_assign(struct isl_stream
*s
)
1370 struct isl_token
*tok
;
1371 struct isl_token
*tok2
;
1374 tok
= isl_stream_next_token(s
);
1377 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1378 isl_stream_push_token(s
, tok
);
1382 tok2
= isl_stream_next_token(s
);
1384 isl_stream_push_token(s
, tok
);
1387 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1388 isl_stream_push_token(s
, tok2
);
1389 isl_stream_push_token(s
, tok
);
1394 static struct isl_obj
read_obj(struct isl_stream
*s
,
1395 struct isl_hash_table
*table
);
1396 static struct isl_obj
read_expr(struct isl_stream
*s
,
1397 struct isl_hash_table
*table
);
1399 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1400 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1403 struct isl_obj obj
= { isl_obj_none
, NULL
};
1405 obj
= read_obj(s
, table
);
1409 op
= find_matching_un_op(op
, obj
);
1411 ctx
= isl_stream_get_ctx(s
);
1413 isl_die(ctx
, isl_error_invalid
,
1414 "no such unary operator defined on given operand",
1417 obj
= convert(ctx
, obj
, op
->arg
);
1418 obj
.v
= op
->fn(obj
.v
);
1424 obj
.type
= isl_obj_none
;
1429 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1431 struct isl_list
*list
;
1434 if (obj
.type
!= isl_obj_union_map
)
1435 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1436 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1437 list
= isl_list_alloc(ctx
, 2);
1441 list
->obj
[0].type
= isl_obj_union_map
;
1442 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1443 list
->obj
[1].type
= isl_obj_bool
;
1444 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
1446 obj
.type
= isl_obj_list
;
1447 if (exact
< 0 || !list
->obj
[0].v
)
1453 obj
.type
= isl_obj_none
;
1458 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1460 struct isl_list
*list
= obj
.v
;
1461 struct isl_token
*tok
;
1466 tok
= isl_stream_next_token(s
);
1467 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1468 isl_stream_error(s
, tok
, "expecting index");
1470 isl_stream_push_token(s
, tok
);
1473 ctx
= isl_stream_get_ctx(s
);
1474 v
= isl_token_get_val(ctx
, tok
);
1475 i
= isl_val_get_num_si(v
);
1477 isl_token_free(tok
);
1478 isl_assert(ctx
, i
< list
->n
, goto error
);
1479 if (isl_stream_eat(s
, ']'))
1482 return obj_at(obj
, i
);
1485 obj
.type
= isl_obj_none
;
1490 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1491 struct isl_hash_table
*table
)
1496 obj
= read_expr(s
, table
);
1497 ctx
= isl_stream_get_ctx(s
);
1498 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1499 is_subtype(obj
, isl_obj_union_map
), goto error
);
1501 if (obj
.type
== isl_obj_list
) {
1502 struct isl_list
*list
= obj
.v
;
1503 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1504 obj
= obj_at(obj
, 0);
1506 if (obj
.type
== isl_obj_set
)
1507 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1508 else if (obj
.type
== isl_obj_map
)
1509 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1510 if (obj
.type
== isl_obj_union_set
) {
1511 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1513 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1517 if (isl_stream_eat(s
, ')'))
1522 isl_union_map_free(umap
);
1525 obj
.type
= isl_obj_none
;
1530 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1531 __isl_take isl_union_set
*uset
)
1533 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1534 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1536 obj
.type
= isl_obj_list
;
1537 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1542 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1543 __isl_take isl_union_map
*umap
)
1545 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1546 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1548 obj
.type
= isl_obj_list
;
1549 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1554 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1555 struct isl_obj obj
, struct isl_hash_table
*table
)
1560 arg
= read_expr(s
, table
);
1561 ctx
= isl_stream_get_ctx(s
);
1562 if (!is_subtype(arg
, isl_obj_union_map
) &&
1563 !is_subtype(arg
, isl_obj_union_set
))
1564 isl_die(ctx
, isl_error_invalid
,
1565 "expecting set of map argument", goto error
);
1567 if (arg
.type
== isl_obj_list
) {
1568 struct isl_list
*list
= arg
.v
;
1569 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1570 arg
= obj_at(arg
, 0);
1572 if (arg
.type
== isl_obj_set
)
1573 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1574 else if (arg
.type
== isl_obj_map
)
1575 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1576 if (arg
.type
== isl_obj_union_set
)
1577 obj
= apply_fun_set(obj
, arg
.v
);
1579 obj
= apply_fun_map(obj
, arg
.v
);
1583 if (isl_stream_eat(s
, ')'))
1591 obj
.type
= isl_obj_none
;
1596 struct add_vertex_data
{
1597 struct isl_list
*list
;
1601 static isl_stat
add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1603 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1607 ma
= isl_vertex_get_expr(vertex
);
1608 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1610 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1611 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1614 isl_vertex_free(vertex
);
1619 static isl_stat
set_vertices(__isl_take isl_set
*set
, void *user
)
1622 isl_basic_set
*hull
;
1623 isl_vertices
*vertices
= NULL
;
1624 struct isl_list
*list
= NULL
;
1626 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1628 set
= isl_set_remove_divs(set
);
1629 hull
= isl_set_convex_hull(set
);
1630 vertices
= isl_basic_set_compute_vertices(hull
);
1631 isl_basic_set_free(hull
);
1635 ctx
= isl_vertices_get_ctx(vertices
);
1636 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1641 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1643 data
->list
= isl_list_concat(list
, data
->list
);
1645 isl_vertices_free(vertices
);
1650 isl_vertices_free(vertices
);
1651 return isl_stat_error
;
1654 static struct isl_obj
vertices(struct isl_stream
*s
,
1655 struct isl_hash_table
*table
)
1659 struct isl_list
*list
= NULL
;
1660 isl_union_set
*uset
= NULL
;
1661 struct add_vertex_data data
= { NULL
};
1663 obj
= read_expr(s
, table
);
1664 ctx
= isl_stream_get_ctx(s
);
1665 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1666 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1670 list
= isl_list_alloc(ctx
, 0);
1676 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1679 isl_union_set_free(uset
);
1681 obj
.type
= isl_obj_list
;
1686 isl_union_set_free(uset
);
1687 isl_list_free(data
.list
);
1689 obj
.type
= isl_obj_none
;
1694 static struct isl_obj
type_of(struct isl_stream
*s
,
1695 struct isl_hash_table
*table
)
1698 const char *type
= "unknown";
1700 obj
= read_expr(s
, table
);
1702 if (obj
.type
== isl_obj_map
||
1703 obj
.type
== isl_obj_union_map
)
1705 if (obj
.type
== isl_obj_set
||
1706 obj
.type
== isl_obj_union_set
)
1708 if (obj
.type
== isl_obj_pw_multi_aff
)
1709 type
= "piecewise multi-quasiaffine expression";
1710 if (obj
.type
== isl_obj_pw_qpolynomial
||
1711 obj
.type
== isl_obj_union_pw_qpolynomial
)
1712 type
= "piecewise quasipolynomial";
1713 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1714 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1715 type
= "piecewise quasipolynomial fold";
1716 if (obj
.type
== isl_obj_list
)
1718 if (obj
.type
== isl_obj_bool
)
1720 if (obj
.type
== isl_obj_str
)
1722 if (obj
.type
== isl_obj_val
)
1724 if (obj
.type
== isl_obj_schedule
)
1728 obj
.type
= isl_obj_str
;
1729 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1734 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1735 struct isl_hash_table
*table
)
1740 obj
= read_obj(s
, table
);
1741 ctx
= isl_stream_get_ctx(s
);
1742 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1743 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1750 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1751 struct isl_hash_table
*table
)
1756 obj
= read_obj(s
, table
);
1757 ctx
= isl_stream_get_ctx(s
);
1758 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1759 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1766 /* Read a schedule in the form of either a schedule (tree) or a union map
1767 * from "s" and store the schedule in "access".
1769 static __isl_give isl_union_access_info
*access_info_set_schedule(
1770 __isl_take isl_union_access_info
*access
, struct isl_stream
*s
,
1771 struct isl_hash_table
*table
)
1776 obj
= read_obj(s
, table
);
1777 if (obj
.type
== isl_obj_schedule
)
1778 return isl_union_access_info_set_schedule(access
, obj
.v
);
1779 ctx
= isl_stream_get_ctx(s
);
1780 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1782 return isl_union_access_info_set_schedule_map(access
, obj
.v
);
1785 static struct isl_obj
last_any(struct isl_stream
*s
,
1786 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1787 __isl_take isl_union_map
*may_source
)
1789 struct isl_obj obj
= { isl_obj_none
, NULL
};
1790 isl_union_access_info
*access
;
1791 isl_union_flow
*flow
;
1792 isl_union_map
*sink
= NULL
;
1793 isl_union_map
*may_dep
;
1795 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1798 sink
= read_map(s
, table
);
1802 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1805 access
= isl_union_access_info_from_sink(sink
);
1806 access
= isl_union_access_info_set_must_source(access
, must_source
);
1807 access
= isl_union_access_info_set_may_source(access
, may_source
);
1808 access
= access_info_set_schedule(access
, s
, table
);
1809 flow
= isl_union_access_info_compute_flow(access
);
1810 may_dep
= isl_union_flow_get_may_dependence(flow
);
1811 isl_union_flow_free(flow
);
1816 obj
.type
= isl_obj_union_map
;
1821 isl_union_map_free(may_source
);
1822 isl_union_map_free(must_source
);
1823 isl_union_map_free(sink
);
1825 obj
.type
= isl_obj_none
;
1830 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1832 struct isl_obj obj
= { isl_obj_none
, NULL
};
1833 isl_union_access_info
*access
;
1834 isl_union_flow
*flow
;
1835 isl_union_map
*may_source
= NULL
;
1836 isl_union_map
*sink
= NULL
;
1837 isl_union_map
*may_dep
;
1839 may_source
= read_map(s
, table
);
1843 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1844 isl_union_map
*must_source
;
1845 must_source
= read_map(s
, table
);
1848 return last_any(s
, table
, must_source
, may_source
);
1851 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1854 sink
= read_map(s
, table
);
1858 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1861 access
= isl_union_access_info_from_sink(sink
);
1862 access
= isl_union_access_info_set_may_source(access
, may_source
);
1863 access
= access_info_set_schedule(access
, s
, table
);
1864 flow
= isl_union_access_info_compute_flow(access
);
1865 may_dep
= isl_union_flow_get_may_dependence(flow
);
1866 isl_union_flow_free(flow
);
1871 obj
.type
= isl_obj_union_map
;
1876 isl_union_map_free(may_source
);
1877 isl_union_map_free(sink
);
1879 obj
.type
= isl_obj_none
;
1884 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1886 struct isl_obj obj
= { isl_obj_none
, NULL
};
1887 struct isl_list
*list
= NULL
;
1888 isl_union_access_info
*access
;
1889 isl_union_flow
*flow
;
1890 isl_union_map
*must_source
= NULL
;
1891 isl_union_map
*sink
= NULL
;
1892 isl_union_map
*must_dep
;
1893 isl_union_map
*must_no_source
;
1895 must_source
= read_map(s
, table
);
1899 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1900 isl_union_map
*may_source
;
1901 may_source
= read_map(s
, table
);
1904 return last_any(s
, table
, must_source
, may_source
);
1907 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1911 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1914 sink
= read_map(s
, table
);
1918 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1921 access
= isl_union_access_info_from_sink(sink
);
1922 access
= isl_union_access_info_set_must_source(access
, must_source
);
1923 access
= access_info_set_schedule(access
, s
, table
);
1924 flow
= isl_union_access_info_compute_flow(access
);
1925 must_dep
= isl_union_flow_get_must_dependence(flow
);
1926 must_no_source
= isl_union_flow_get_must_no_source(flow
);
1927 isl_union_flow_free(flow
);
1929 list
->obj
[0].type
= isl_obj_union_map
;
1930 list
->obj
[0].v
= must_dep
;
1931 list
->obj
[1].type
= isl_obj_union_map
;
1932 list
->obj
[1].v
= must_no_source
;
1934 if (!must_dep
|| !must_no_source
) {
1935 isl_list_free(list
);
1940 obj
.type
= isl_obj_list
;
1944 isl_list_free(list
);
1945 isl_union_map_free(must_source
);
1946 isl_union_map_free(sink
);
1948 obj
.type
= isl_obj_none
;
1953 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1954 struct isl_hash_table
*table
)
1956 isl_union_set
*domain
;
1957 isl_union_map
*validity
;
1958 isl_union_map
*proximity
;
1960 domain
= read_set(s
, table
);
1964 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1965 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1968 isl_union_map
*umap
;
1969 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1970 umap
= read_map(s
, table
);
1971 validity
= isl_union_map_union(validity
, umap
);
1972 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1973 umap
= read_map(s
, table
);
1974 proximity
= isl_union_map_union(proximity
, umap
);
1979 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1982 static struct isl_obj
schedule(struct isl_stream
*s
,
1983 struct isl_hash_table
*table
)
1985 struct isl_obj obj
= { isl_obj_none
, NULL
};
1986 isl_schedule
*schedule
;
1988 schedule
= get_schedule(s
, table
);
1991 obj
.type
= isl_obj_schedule
;
1996 /* Read a schedule for code generation in the form of either
1997 * a schedule tree or a union map.
1998 * If the input is a set rather than a map, then we construct
1999 * an identity union map schedule on the given set.
2001 static struct isl_obj
get_codegen_schedule(struct isl_stream
*s
,
2002 struct isl_hash_table
*table
)
2007 obj
= read_obj(s
, table
);
2008 ctx
= isl_stream_get_ctx(s
);
2010 if (obj
.type
== isl_obj_schedule
)
2012 if (is_subtype(obj
, isl_obj_union_set
)) {
2013 obj
= convert(ctx
, obj
, isl_obj_union_set
);
2014 obj
.v
= isl_union_set_identity(obj
.v
);
2015 obj
.type
= isl_obj_union_map
;
2017 if (is_subtype(obj
, isl_obj_union_map
))
2018 return convert(ctx
, obj
, isl_obj_union_map
);
2022 obj
.type
= isl_obj_none
;
2023 isl_die(ctx
, isl_error_invalid
, "expecting schedule, set or map",
2027 /* Generate an AST for the given schedule and options and return the AST.
2029 static __isl_give isl_ast_node
*get_ast_from_union_map(
2030 __isl_take isl_union_map
*schedule
, __isl_take isl_union_map
*options
)
2034 isl_ast_build
*build
;
2037 space
= isl_union_map_get_space(schedule
);
2038 context
= isl_set_universe(isl_space_params(space
));
2040 build
= isl_ast_build_from_context(context
);
2041 build
= isl_ast_build_set_options(build
, options
);
2042 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
2043 isl_ast_build_free(build
);
2048 /* Generate an AST for the given schedule and return the AST.
2050 static __isl_give isl_ast_node
*get_ast_from_schedule(
2051 __isl_take isl_schedule
*schedule
)
2053 isl_ast_build
*build
;
2056 build
= isl_ast_build_alloc(isl_schedule_get_ctx(schedule
));
2057 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2058 isl_ast_build_free(build
);
2063 /* Print the AST "tree" on the printer "p".
2065 static __isl_give isl_printer
*print_ast(__isl_take isl_printer
*p
,
2066 __isl_take isl_ast_node
*tree
)
2070 format
= isl_printer_get_output_format(p
);
2071 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
2072 p
= isl_printer_print_ast_node(p
, tree
);
2073 p
= isl_printer_set_output_format(p
, format
);
2075 isl_ast_node_free(tree
);
2080 /* Perform the codegen operation.
2081 * In particular, read a schedule, check if the user has specified any options
2082 * and then generate an AST from the schedule (and options) and print it.
2083 * In case the schedule is specified as a schedule tree, the AST generation
2084 * options are embedded in the schedule, so they are not read in separately.
2086 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
2087 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2092 obj
= get_codegen_schedule(s
, table
);
2096 if (obj
.type
== isl_obj_schedule
) {
2097 isl_schedule
*schedule
= obj
.v
;
2099 tree
= get_ast_from_schedule(schedule
);
2101 isl_union_map
*schedule
= obj
.v
;
2102 isl_union_map
*options
;
2104 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
2105 options
= read_map(s
, table
);
2107 options
= isl_union_map_empty(
2108 isl_union_map_get_space(schedule
));
2110 tree
= get_ast_from_union_map(schedule
, options
);
2114 p
= print_ast(p
, tree
);
2116 isl_stream_eat(s
, ';');
2121 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
2123 struct isl_token
*tok
;
2127 ctx
= isl_stream_get_ctx(s
);
2128 if (isl_stream_eat_if_available(s
, '+'))
2129 return transitive_closure(ctx
, obj
);
2131 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
2132 if (obj
.type
!= isl_obj_union_map
)
2133 obj
= convert(ctx
, obj
, isl_obj_union_map
);
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
);
2144 if (isl_val_is_zero(v
)) {
2145 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2148 isl_stream_push_token(s
, tok
);
2152 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2153 isl_token_free(tok
);
2160 obj
.type
= isl_obj_none
;
2165 static struct isl_obj
check_assert(struct isl_stream
*s
,
2166 struct isl_hash_table
*table
)
2171 obj
= read_expr(s
, table
);
2172 ctx
= isl_stream_get_ctx(s
);
2173 if (obj
.type
!= isl_obj_bool
)
2174 isl_die(ctx
, isl_error_invalid
,
2175 "expecting boolean expression", goto error
);
2176 if (obj
.v
!= &iscc_bool_true
)
2177 isl_die(ctx
, isl_error_unknown
,
2178 "assertion failed", abort());
2181 obj
.type
= isl_obj_none
;
2186 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2190 struct isl_token
*tok
;
2191 struct isl_stream
*s_file
;
2192 struct iscc_options
*options
;
2196 tok
= isl_stream_next_token(s
);
2197 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2198 isl_stream_error(s
, tok
, "expecting filename");
2199 isl_token_free(tok
);
2203 ctx
= isl_stream_get_ctx(s
);
2204 options
= isl_ctx_peek_iscc_options(ctx
);
2205 if (!options
|| !options
->io
) {
2206 isl_token_free(tok
);
2207 isl_die(ctx
, isl_error_invalid
,
2208 "read operation not allowed", goto error
);
2211 name
= isl_token_get_str(ctx
, tok
);
2212 isl_token_free(tok
);
2213 file
= fopen(name
, "r");
2215 isl_assert(ctx
, file
, goto error
);
2217 s_file
= isl_stream_new_file(ctx
, file
);
2223 obj
= isl_stream_read_obj(s_file
);
2225 isl_stream_free(s_file
);
2230 obj
.type
= isl_obj_none
;
2235 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2236 struct isl_hash_table
*table
)
2238 struct isl_obj obj
= { isl_obj_none
, NULL
};
2239 struct isl_token
*tok
;
2240 struct iscc_options
*options
;
2246 tok
= isl_stream_next_token(s
);
2247 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2248 isl_stream_error(s
, tok
, "expecting filename");
2249 isl_token_free(tok
);
2253 obj
= read_expr(s
, table
);
2255 ctx
= isl_stream_get_ctx(s
);
2256 options
= isl_ctx_peek_iscc_options(ctx
);
2257 if (!options
|| !options
->io
) {
2258 isl_token_free(tok
);
2259 isl_die(ctx
, isl_error_invalid
,
2260 "write operation not allowed", goto error
);
2263 name
= isl_token_get_str(ctx
, tok
);
2264 isl_token_free(tok
);
2265 file
= fopen(name
, "w");
2268 isl_die(ctx
, isl_error_unknown
,
2269 "could not open file for writing", goto error
);
2271 p
= isl_printer_to_file(ctx
, file
);
2272 p
= isl_printer_set_output_format(p
, options
->format
);
2273 p
= obj
.type
->print(p
, obj
.v
);
2274 p
= isl_printer_end_line(p
);
2275 isl_printer_free(p
);
2280 obj
.type
= isl_obj_none
;
2285 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2287 struct isl_token
*tok
;
2288 struct isl_obj obj
= { isl_obj_none
, NULL
};
2290 tok
= isl_stream_next_token(s
);
2293 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2295 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2298 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2299 isl_token_free(tok
);
2301 obj
.type
= isl_obj_str
;
2303 isl_stream_push_token(s
, tok
);
2306 isl_token_free(tok
);
2310 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2312 struct isl_token
*tok
;
2313 struct isl_obj obj
= { isl_obj_none
, NULL
};
2316 tok
= isl_stream_next_token(s
);
2319 type
= isl_token_get_type(tok
);
2320 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2321 int is_true
= type
== ISL_TOKEN_TRUE
;
2322 isl_token_free(tok
);
2323 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2324 obj
.type
= isl_obj_bool
;
2326 isl_stream_push_token(s
, tok
);
2330 static __isl_give
char *read_ident(struct isl_stream
*s
)
2334 struct isl_token
*tok
, *tok2
;
2336 name
= isl_stream_read_ident_if_available(s
);
2340 tok
= isl_stream_next_token(s
);
2343 if (isl_token_get_type(tok
) != '$') {
2344 isl_stream_push_token(s
, tok
);
2347 tok2
= isl_stream_next_token(s
);
2348 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2350 isl_stream_push_token(s
, tok2
);
2351 isl_stream_push_token(s
, tok
);
2355 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2356 name
= isl_val_to_str(v
);
2358 isl_token_free(tok
);
2359 isl_token_free(tok2
);
2364 static struct isl_obj
read_list(struct isl_stream
*s
,
2365 struct isl_hash_table
*table
, struct isl_obj obj
)
2367 struct isl_list
*list
;
2369 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2373 list
->obj
[1] = read_obj(s
, table
);
2375 obj
.type
= isl_obj_list
;
2377 if (!list
->obj
[1].v
)
2380 while (isl_stream_eat_if_available(s
, ',')) {
2381 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2389 obj
.type
= isl_obj_none
;
2394 static struct isl_obj
read_obj(struct isl_stream
*s
,
2395 struct isl_hash_table
*table
)
2398 struct isl_obj obj
= { isl_obj_none
, NULL
};
2400 struct isc_un_op
*op
= NULL
;
2402 obj
= read_string_if_available(s
);
2405 obj
= read_bool_if_available(s
);
2408 ctx
= isl_stream_get_ctx(s
);
2409 if (isl_stream_eat_if_available(s
, '(')) {
2410 if (isl_stream_next_token_is(s
, ')')) {
2411 obj
.type
= isl_obj_list
;
2412 obj
.v
= isl_list_alloc(ctx
, 0);
2414 obj
= read_expr(s
, table
);
2415 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2416 obj
= read_list(s
, table
, obj
);
2418 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2421 op
= read_prefix_un_op_if_available(s
);
2423 return read_un_op_expr(s
, table
, op
);
2425 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2426 return check_assert(s
, table
);
2427 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2428 return read_from_file(s
);
2429 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2430 return write_to_file(s
, table
);
2431 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2432 return vertices(s
, table
);
2433 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2434 return any(s
, table
);
2435 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2436 return last(s
, table
);
2437 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2438 return schedule(s
, table
);
2439 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2440 return type_of(s
, table
);
2442 name
= read_ident(s
);
2444 obj
= stored_obj(ctx
, table
, name
);
2446 obj
= isl_stream_read_obj(s
);
2451 if (isl_stream_eat_if_available(s
, '^'))
2452 obj
= power(s
, obj
);
2453 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2454 obj
= obj_at_index(s
, obj
);
2455 else if (is_subtype(obj
, isl_obj_union_map
) &&
2456 isl_stream_eat_if_available(s
, '(')) {
2457 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2458 obj
= apply(s
, obj
.v
, table
);
2459 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2460 isl_stream_eat_if_available(s
, '(')) {
2461 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2462 obj
= apply_fun(s
, obj
, table
);
2463 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2464 isl_stream_eat_if_available(s
, '(')) {
2465 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2466 obj
= apply_fun(s
, obj
, table
);
2472 obj
.type
= isl_obj_none
;
2477 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2478 struct isl_obj lhs
, struct isl_obj rhs
)
2482 for (i
= 0; ; ++i
) {
2485 if (bin_ops
[i
].op
!= like
->op
)
2487 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2489 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2495 for (i
= 0; ; ++i
) {
2496 if (!named_bin_ops
[i
].name
)
2498 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2500 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2502 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2505 return &named_bin_ops
[i
].op
;
2508 for (i
= 0; ; ++i
) {
2509 if (!compound_bin_ops
[i
].full
)
2511 if (compound_bin_ops
[i
].op
.op
!= like
->op
)
2513 if (!is_subtype(lhs
, compound_bin_ops
[i
].op
.lhs
))
2515 if (!is_subtype(rhs
, compound_bin_ops
[i
].op
.rhs
))
2518 return &compound_bin_ops
[i
].op
;
2524 static int next_is_neg_int(struct isl_stream
*s
)
2526 struct isl_token
*tok
;
2529 tok
= isl_stream_next_token(s
);
2530 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2532 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2533 ret
= isl_val_is_neg(v
);
2537 isl_stream_push_token(s
, tok
);
2542 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2543 struct isl_obj lhs
, struct isl_obj rhs
)
2547 lhs
= convert(ctx
, lhs
, op
->lhs
);
2548 rhs
= convert(ctx
, rhs
, op
->rhs
);
2549 if (op
->res
!= isl_obj_bool
)
2550 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2552 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2555 obj
.v
= iscc_bool_from_int(res
);
2562 static struct isl_obj
read_expr(struct isl_stream
*s
,
2563 struct isl_hash_table
*table
)
2566 struct isl_obj obj
= { isl_obj_none
, NULL
};
2567 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2569 obj
= read_obj(s
, table
);
2570 ctx
= isl_stream_get_ctx(s
);
2572 struct isc_bin_op
*op
= NULL
;
2574 op
= read_bin_op_if_available(s
, obj
);
2578 right_obj
= read_obj(s
, table
);
2580 op
= find_matching_bin_op(op
, obj
, right_obj
);
2583 isl_die(ctx
, isl_error_invalid
,
2584 "no such binary operator defined on given operands",
2587 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2590 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2591 right_obj
= read_obj(s
, table
);
2592 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2597 free_obj(right_obj
);
2599 obj
.type
= isl_obj_none
;
2604 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2605 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2607 /* Print "obj" to the printer "p".
2608 * If the object is a schedule, then print it in block format.
2610 static __isl_give isl_printer
*print_obj(__isl_take isl_printer
*p
,
2613 if (obj
.type
!= isl_obj_schedule
)
2614 return obj
.type
->print(p
, obj
.v
);
2616 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
2617 p
= obj
.type
->print(p
, obj
.v
);
2618 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2623 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2624 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2627 struct isl_obj obj
= { isl_obj_none
, NULL
};
2635 if (isl_stream_is_empty(s
))
2638 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2639 return source_file(s
, table
, p
);
2640 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2641 return codegen(s
, table
, p
);
2643 assign
= is_assign(s
);
2645 lhs
= isl_stream_read_ident_if_available(s
);
2646 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2648 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2653 obj
= read_expr(s
, table
);
2654 ctx
= isl_stream_get_ctx(s
);
2655 if (isl_stream_eat(s
, ';'))
2659 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2660 p
= print_obj(p
, obj
);
2661 p
= isl_printer_end_line(p
);
2666 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2667 static int count
= 0;
2668 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2669 lhs
= strdup(buf
+ 1);
2671 p
= isl_printer_print_str(p
, buf
);
2672 p
= isl_printer_print_str(p
, " := ");
2673 p
= obj
.type
->print(p
, obj
.v
);
2674 p
= isl_printer_end_line(p
);
2676 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2681 isl_stream_flush_tokens(s
);
2682 isl_stream_skip_line(s
);
2688 static isl_stat
free_cb(void **entry
, void *user
)
2690 struct isl_named_obj
*named
= *entry
;
2692 free_obj(named
->obj
);
2699 static void register_named_ops(struct isl_stream
*s
)
2703 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2704 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2705 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2708 for (i
= 0; ; ++i
) {
2709 if (!named_un_ops
[i
].name
)
2711 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2712 named_un_ops
[i
].name
);
2713 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2716 for (i
= 0; ; ++i
) {
2717 if (!named_bin_ops
[i
].name
)
2719 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2720 named_bin_ops
[i
].name
);
2721 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2724 for (i
= 0; ; ++i
) {
2725 if (!compound_bin_ops
[i
].full
)
2727 compound_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2728 compound_bin_ops
[i
].full
);
2729 assert(compound_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2733 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2734 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2737 struct isl_token
*tok
;
2738 struct isl_stream
*s_file
;
2739 struct iscc_options
*options
;
2743 tok
= isl_stream_next_token(s
);
2744 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2745 isl_stream_error(s
, tok
, "expecting filename");
2746 isl_token_free(tok
);
2750 isl_stream_eat(s
, ';');
2752 ctx
= isl_stream_get_ctx(s
);
2753 options
= isl_ctx_peek_iscc_options(ctx
);
2754 if (!options
|| !options
->io
) {
2755 isl_token_free(tok
);
2756 isl_die(ctx
, isl_error_invalid
,
2757 "source operation not allowed", return p
);
2760 name
= isl_token_get_str(ctx
, tok
);
2761 isl_token_free(tok
);
2762 file
= fopen(name
, "r");
2764 isl_assert(ctx
, file
, return p
);
2766 s_file
= isl_stream_new_file(ctx
, file
);
2772 register_named_ops(s_file
);
2774 while (!isl_stream_is_empty(s_file
))
2775 p
= read_line(s_file
, table
, p
, 0);
2777 isl_stream_free(s_file
);
2783 int main(int argc
, char **argv
)
2785 struct isl_ctx
*ctx
;
2786 struct isl_stream
*s
;
2787 struct isl_hash_table
*table
;
2788 struct iscc_options
*options
;
2790 int tty
= isatty(0);
2792 options
= iscc_options_new_with_defaults();
2795 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2796 pet_options_set_autodetect(ctx
, 1);
2797 pet_options_set_encapsulate_dynamic_control(ctx
, 1);
2798 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2799 s
= isl_stream_new_file(ctx
, stdin
);
2801 table
= isl_hash_table_alloc(ctx
, 10);
2803 p
= isl_printer_to_file(ctx
, stdout
);
2804 p
= isl_printer_set_output_format(p
, options
->format
);
2807 register_named_ops(s
);
2809 install_signal_handler(ctx
);
2814 empty
= isl_stream_is_empty(s
);
2815 if (empty
&& isl_ctx_last_error(ctx
) != isl_error_abort
)
2818 p
= read_line(s
, table
, p
, tty
);
2819 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2820 fprintf(stderr
, "Interrupted\n");
2821 isl_ctx_resume(ctx
);
2822 isl_ctx_reset_error(ctx
);
2826 remove_signal_handler(ctx
);
2828 isl_printer_free(p
);
2829 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2830 isl_hash_table_free(ctx
, table
);