8 #include <isl/stream.h>
11 #include <isl/vertices.h>
13 #include <isl/schedule.h>
14 #include <isl/ast_build.h>
15 #include <isl_obj_list.h>
16 #include <isl_obj_str.h>
17 #include <barvinok/isl.h>
18 #include <barvinok/options.h>
19 #include "lattice_width.h"
26 static isl_ctx
*main_ctx
;
28 static void handler(int signum
)
30 if (isl_ctx_aborted(main_ctx
))
32 isl_ctx_abort(main_ctx
);
35 static struct sigaction sa_old
;
37 static void install_signal_handler(isl_ctx
*ctx
)
43 memset(&sa
, 0, sizeof(struct sigaction
));
44 sa
.sa_handler
= &handler
;
45 sa
.sa_flags
= SA_RESTART
;
46 sigaction(SIGINT
, &sa
, &sa_old
);
49 static void remove_signal_handler(isl_ctx
*ctx
)
51 sigaction(SIGINT
, &sa_old
, NULL
);
56 static void install_signal_handler(isl_ctx
*ctx
)
60 static void remove_signal_handler(isl_ctx
*ctx
)
70 int pet_options_set_autodetect(isl_ctx
*ctx
, int val
)
76 static int iscc_bool_false
= 0;
77 static int iscc_bool_true
= 1;
78 static int iscc_bool_error
= -1;
80 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
81 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
83 ISCC_MINIMIZING
, ISCC_RESPECTING
,
84 ISCC_CODEGEN
, ISCC_USING
,
85 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
87 static const char *op_name
[ISCC_N_OP
] = {
88 [ISCC_ASSERT
] = "assert",
90 [ISCC_WRITE
] = "write",
91 [ISCC_PRINT
] = "print",
92 [ISCC_SOURCE
] = "source",
93 [ISCC_VERTICES
] = "vertices",
96 [ISCC_BEFORE
] = "before",
97 [ISCC_UNDER
] = "under",
98 [ISCC_SCHEDULE
] = "schedule",
99 [ISCC_MINIMIZING
] = "minimizing",
100 [ISCC_RESPECTING
] = "respecting",
101 [ISCC_CODEGEN
] = "codegen",
102 [ISCC_USING
] = "using",
103 [ISCC_TYPEOF
] = "typeof"
105 static enum isl_token_type iscc_op
[ISCC_N_OP
];
107 struct isl_arg_choice iscc_format
[] = {
108 {"isl", ISL_FORMAT_ISL
},
109 {"omega", ISL_FORMAT_OMEGA
},
110 {"polylib", ISL_FORMAT_POLYLIB
},
111 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
112 {"latex", ISL_FORMAT_LATEX
},
117 struct iscc_options
{
118 struct barvinok_options
*barvinok
;
119 struct pet_options
*pet
;
124 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
125 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
128 ISL_ARG_CHILD(struct iscc_options
, pet
, "pet", &pet_options_args
, "pet options")
130 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
131 iscc_format
, ISL_FORMAT_ISL
, "output format")
132 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
133 "allow read and write operations")
136 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
137 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
139 static void *isl_obj_bool_copy(void *v
)
144 static void isl_obj_bool_free(void *v
)
148 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
151 if (v
== &iscc_bool_true
)
152 return isl_printer_print_str(p
, "True");
153 else if (v
== &iscc_bool_false
)
154 return isl_printer_print_str(p
, "False");
156 return isl_printer_print_str(p
, "Error");
159 static void *isl_obj_bool_add(void *v1
, void *v2
)
164 struct isl_obj_vtable isl_obj_bool_vtable
= {
170 #define isl_obj_bool (&isl_obj_bool_vtable)
172 int *iscc_bool_from_int(int res
)
174 return res
< 0 ? &iscc_bool_error
:
175 res
? &iscc_bool_true
: &iscc_bool_false
;
178 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
179 __isl_take isl_union_map
*map2
)
181 return isl_union_map_is_subset(map2
, map1
);
183 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
184 __isl_take isl_union_set
*set2
)
186 return isl_union_set_is_subset(set2
, set1
);
189 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
190 __isl_take isl_union_map
*map2
)
192 return isl_union_map_is_strict_subset(map2
, map1
);
194 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
195 __isl_take isl_union_set
*set2
)
197 return isl_union_set_is_strict_subset(set2
, set1
);
200 extern struct isl_obj_vtable isl_obj_list_vtable
;
201 #define isl_obj_list (&isl_obj_list_vtable)
203 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
204 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
206 enum isl_token_type op
;
212 isc_bin_test_fn test
;
215 struct isc_named_bin_op
{
217 struct isc_bin_op op
;
221 isl_union_pw_qpolynomial
*upwqp
;
222 isl_union_pw_qpolynomial
*res
;
225 static isl_stat
eval_at(__isl_take isl_point
*pnt
, void *user
)
227 struct iscc_at
*at
= (struct iscc_at
*) user
;
232 set
= isl_set_from_point(isl_point_copy(pnt
));
233 v
= isl_union_pw_qpolynomial_eval(
234 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
235 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
237 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
238 isl_union_pw_qpolynomial_from_pw_qpolynomial(
239 isl_pw_qpolynomial_alloc(set
, qp
)));
244 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
245 __isl_take isl_union_pw_qpolynomial
*upwqp
,
246 __isl_take isl_union_set
*uset
)
251 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
253 isl_union_set_foreach_point(uset
, eval_at
, &at
);
255 isl_union_pw_qpolynomial_free(upwqp
);
256 isl_union_set_free(uset
);
261 struct iscc_fold_at
{
262 isl_union_pw_qpolynomial_fold
*upwf
;
263 isl_union_pw_qpolynomial
*res
;
266 static isl_stat
eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
268 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
273 set
= isl_set_from_point(isl_point_copy(pnt
));
274 v
= isl_union_pw_qpolynomial_fold_eval(
275 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
276 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
278 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
279 isl_union_pw_qpolynomial_from_pw_qpolynomial(
280 isl_pw_qpolynomial_alloc(set
, qp
)));
285 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
286 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
287 __isl_take isl_union_set
*uset
)
289 struct iscc_fold_at at
;
292 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
294 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
296 isl_union_pw_qpolynomial_fold_free(upwf
);
297 isl_union_set_free(uset
);
302 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
303 __isl_take isl_union_pw_qpolynomial
*upwqp
,
304 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
306 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
310 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
311 __isl_take isl_union_map
*umap
,
312 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
315 struct isl_list
*list
;
318 ctx
= isl_union_map_get_ctx(umap
);
319 list
= isl_list_alloc(ctx
, 2);
323 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
324 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
326 list
->obj
[1].type
= isl_obj_bool
;
327 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
328 if (tight
< 0 || !list
->obj
[0].v
)
333 isl_union_map_free(umap
);
334 isl_union_pw_qpolynomial_fold_free(upwf
);
340 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
341 __isl_take isl_union_set
*uset
,
342 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
345 struct isl_list
*list
;
348 ctx
= isl_union_set_get_ctx(uset
);
349 list
= isl_list_alloc(ctx
, 2);
353 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
354 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
356 list
->obj
[1].type
= isl_obj_bool
;
357 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
358 if (tight
< 0 || !list
->obj
[0].v
)
363 isl_union_set_free(uset
);
364 isl_union_pw_qpolynomial_fold_free(upwf
);
370 static __isl_give isl_union_pw_qpolynomial
*isl_val_mul_union_pw_qpolynomial(
371 __isl_take isl_val
*v
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
373 return isl_union_pw_qpolynomial_scale_val(upwqp
, v
);
376 static __isl_give isl_union_pw_qpolynomial_fold
*
377 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val
*v
,
378 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
380 return isl_union_pw_qpolynomial_fold_scale_val(upwf
, v
);
383 /* Are the two strings "str1" and "str2" equal to each other?
385 static int str_eq(__isl_keep isl_str
*str1
, __isl_keep isl_str
*str2
)
390 return !strcmp(str1
->s
, str2
->s
);
393 struct isc_bin_op bin_ops
[] = {
394 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
395 (isc_bin_op_fn
) &isl_val_add
},
396 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
397 (isc_bin_op_fn
) &isl_val_sub
},
398 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
399 (isc_bin_op_fn
) &isl_val_mul
},
400 { '+', isl_obj_pw_multi_aff
, isl_obj_pw_multi_aff
,
401 isl_obj_pw_multi_aff
,
402 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
403 { '+', isl_obj_union_set
, isl_obj_union_set
,
405 (isc_bin_op_fn
) &isl_union_set_union
},
406 { '+', isl_obj_union_map
, isl_obj_union_map
,
408 (isc_bin_op_fn
) &isl_union_map_union
},
409 { '-', isl_obj_union_set
, isl_obj_union_set
,
411 (isc_bin_op_fn
) &isl_union_set_subtract
},
412 { '-', isl_obj_union_map
, isl_obj_union_map
,
414 (isc_bin_op_fn
) &isl_union_map_subtract
},
415 { '*', isl_obj_union_set
, isl_obj_union_set
,
417 (isc_bin_op_fn
) &isl_union_set_intersect
},
418 { '*', isl_obj_union_map
, isl_obj_union_map
,
420 (isc_bin_op_fn
) &isl_union_map_intersect
},
421 { '*', isl_obj_union_map
, isl_obj_union_set
,
423 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
424 { '.', isl_obj_union_map
, isl_obj_union_map
,
426 (isc_bin_op_fn
) &isl_union_map_apply_range
},
427 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
428 isl_obj_union_pw_qpolynomial
,
429 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
430 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
432 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
433 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
435 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
436 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
437 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
438 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
439 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
440 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
442 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
443 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
445 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
446 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
448 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
449 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
451 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
452 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
454 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
455 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
457 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
458 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
461 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
462 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
465 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
466 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
468 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
469 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
471 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
472 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
474 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
475 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
477 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
478 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
480 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
481 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
483 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
484 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
486 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
487 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
489 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
490 { '.', isl_obj_union_pw_qpolynomial_fold
,
491 isl_obj_union_pw_qpolynomial_fold
,
492 isl_obj_union_pw_qpolynomial_fold
,
493 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
494 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
495 isl_obj_union_pw_qpolynomial
,
496 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
497 { '+', isl_obj_union_pw_qpolynomial
,
498 isl_obj_union_pw_qpolynomial_fold
,
499 isl_obj_union_pw_qpolynomial_fold
,
500 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
501 { '+', isl_obj_union_pw_qpolynomial_fold
,
502 isl_obj_union_pw_qpolynomial
,
503 isl_obj_union_pw_qpolynomial_fold
,
504 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
505 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
506 isl_obj_union_pw_qpolynomial
,
507 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
508 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
509 isl_obj_union_pw_qpolynomial
,
510 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
511 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
512 isl_obj_union_pw_qpolynomial
,
513 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
514 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
515 isl_obj_union_pw_qpolynomial_fold
,
516 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
517 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
518 isl_obj_union_pw_qpolynomial_fold
,
519 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
520 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
521 isl_obj_union_pw_qpolynomial
,
522 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
523 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
524 isl_obj_union_pw_qpolynomial
,
525 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
526 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
527 isl_obj_union_pw_qpolynomial_fold
,
528 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
529 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
530 isl_obj_union_pw_qpolynomial
,
531 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
532 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
533 isl_obj_union_pw_qpolynomial
,
534 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
535 { '%', isl_obj_union_set
, isl_obj_union_set
,
537 (isc_bin_op_fn
) &isl_union_set_gist
},
538 { '%', isl_obj_union_map
, isl_obj_union_map
,
540 (isc_bin_op_fn
) &isl_union_map_gist
},
541 { '%', isl_obj_union_map
, isl_obj_union_set
,
543 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
544 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
545 isl_obj_union_pw_qpolynomial
,
546 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
547 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
548 isl_obj_union_pw_qpolynomial_fold
,
549 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
550 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
551 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
552 { .test
= (isc_bin_test_fn
)
553 &isl_union_pw_qpolynomial_plain_is_equal
} },
554 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
555 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
556 { .test
= (isc_bin_test_fn
)
557 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
558 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
559 (isc_bin_op_fn
) &isl_str_concat
},
560 { '=', isl_obj_str
, isl_obj_str
, isl_obj_bool
,
561 { .test
= (isc_bin_test_fn
) &str_eq
} },
565 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
566 __isl_take isl_union_map
*umap2
)
568 return isl_union_map_apply_range(umap2
, umap1
);
571 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
572 __isl_take isl_union_pw_qpolynomial
*upwqp
,
573 __isl_take isl_union_map
*umap
)
575 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
578 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
579 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
580 __isl_take isl_union_map
*umap
)
582 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
585 struct isc_named_bin_op named_bin_ops
[] = {
586 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
588 (isc_bin_op_fn
) &map_after_map
} },
589 { "after", { -1, isl_obj_union_pw_qpolynomial
,
590 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
591 (isc_bin_op_fn
) &qpolynomial_after_map
} },
592 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
593 isl_obj_union_map
, isl_obj_list
,
594 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
595 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
597 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
598 { "before", { -1, isl_obj_union_map
,
599 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
600 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
601 { "before", { -1, isl_obj_union_map
,
602 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
603 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
604 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
606 (isc_bin_op_fn
) &isl_union_set_product
} },
607 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
609 (isc_bin_op_fn
) &isl_union_map_product
} },
613 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
615 return isl_set_from_basic_set(isl_union_set_sample(uset
));
618 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
620 return isl_map_from_basic_map(isl_union_map_sample(umap
));
623 static __isl_give
struct isl_list
*union_map_power(
624 __isl_take isl_union_map
*umap
)
627 struct isl_list
*list
;
630 ctx
= isl_union_map_get_ctx(umap
);
631 list
= isl_list_alloc(ctx
, 2);
635 list
->obj
[0].type
= isl_obj_union_map
;
636 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
637 list
->obj
[1].type
= isl_obj_bool
;
638 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
639 if (exact
< 0 || !list
->obj
[0].v
)
644 isl_union_map_free(umap
);
650 /* Compute a lower or upper bound on "upwqp" depending on "type" and
651 * return a list containing two elements, the bound and a boolean
652 * indicating whether the result is tight.
654 static __isl_give
struct isl_list
*union_pw_qpolynomial_bound(
655 __isl_take isl_union_pw_qpolynomial
*upwqp
, enum isl_fold type
)
658 struct isl_list
*list
;
661 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
662 list
= isl_list_alloc(ctx
, 2);
666 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
667 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
, type
, &tight
);
668 list
->obj
[1].type
= isl_obj_bool
;
669 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
670 if (tight
< 0 || !list
->obj
[0].v
)
675 isl_union_pw_qpolynomial_free(upwqp
);
681 /* Compute a lower bound on "upwqp" and return a list containing
682 * two elements, the bound and a booleanindicating whether
683 * the result is tight.
685 static __isl_give
struct isl_list
*union_pw_qpolynomial_lower_bound(
686 __isl_take isl_union_pw_qpolynomial
*upwqp
)
688 return union_pw_qpolynomial_bound(upwqp
, isl_fold_min
);
691 /* Compute a upper bound on "upwqp" and return a list containing
692 * two elements, the bound and a booleanindicating whether
693 * the result is tight.
695 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
696 __isl_take isl_union_pw_qpolynomial
*upwqp
)
698 return union_pw_qpolynomial_bound(upwqp
, isl_fold_max
);
702 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
705 struct isl_list
*list
;
706 struct pet_scop
*scop
;
708 isl_union_map
*may_reads
, *must_writes
, *may_writes
;
709 isl_union_set
*domain
;
710 struct iscc_options
*options
;
716 options
= isl_ctx_peek_iscc_options(ctx
);
717 if (!options
|| !options
->io
) {
719 isl_die(ctx
, isl_error_invalid
,
720 "parse_file operation not allowed", return NULL
);
723 list
= isl_list_alloc(ctx
, 5);
727 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
728 domain
= pet_scop_collect_domains(scop
);
729 sched
= scop
? isl_schedule_copy(scop
->schedule
) : NULL
;
730 may_reads
= pet_scop_collect_may_reads(scop
);
731 may_writes
= pet_scop_collect_may_writes(scop
);
732 must_writes
= pet_scop_collect_must_writes(scop
);
735 list
->obj
[0].type
= isl_obj_union_set
;
736 list
->obj
[0].v
= domain
;
737 list
->obj
[1].type
= isl_obj_union_map
;
738 list
->obj
[1].v
= must_writes
;
739 list
->obj
[2].type
= isl_obj_union_map
;
740 list
->obj
[2].v
= may_writes
;
741 list
->obj
[3].type
= isl_obj_union_map
;
742 list
->obj
[3].v
= may_reads
;
743 list
->obj
[4].type
= isl_obj_schedule
;
744 list
->obj
[4].v
= sched
;
746 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
747 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
759 static isl_stat
add_point(__isl_take isl_point
*pnt
, void *user
)
761 isl_union_set
**scan
= (isl_union_set
**) user
;
763 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
768 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
772 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
774 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
775 isl_union_set_free(scan
);
779 isl_union_set_free(uset
);
783 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
785 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
788 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
789 __isl_take isl_union_pw_qpolynomial
*upwqp
)
791 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
794 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
795 __isl_take isl_union_pw_qpolynomial
*upwqp
)
797 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
800 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
801 __isl_take isl_union_pw_qpolynomial
*upwqp
)
803 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
806 /* Return the domain of "schedule".
808 static __isl_give isl_union_set
*schedule_domain(
809 __isl_take isl_schedule
*schedule
)
811 isl_union_set
*domain
;
813 domain
= isl_schedule_get_domain(schedule
);
814 isl_schedule_free(schedule
);
819 /* Convert "schedule" to a union map representation.
821 static __isl_give isl_union_map
*schedule_map(__isl_take isl_schedule
*schedule
)
825 map
= isl_schedule_get_map(schedule
);
826 isl_schedule_free(schedule
);
831 typedef void *(*isc_un_op_fn
)(void *arg
);
833 enum isl_token_type op
;
838 struct isc_named_un_op
{
842 struct isc_named_un_op named_un_ops
[] = {
843 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
844 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
845 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
846 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
847 {"card", { -1, isl_obj_union_set
,
848 isl_obj_union_pw_qpolynomial
,
849 (isc_un_op_fn
) &isl_union_set_card
} },
850 {"card", { -1, isl_obj_union_map
,
851 isl_obj_union_pw_qpolynomial
,
852 (isc_un_op_fn
) &isl_union_map_card
} },
853 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
854 (isc_un_op_fn
) &isl_union_set_coalesce
} },
855 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
856 (isc_un_op_fn
) &isl_union_map_coalesce
} },
857 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
858 isl_obj_union_pw_qpolynomial
,
859 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
860 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
861 isl_obj_union_pw_qpolynomial_fold
,
862 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
863 {"coefficients", { -1, isl_obj_union_set
,
865 (isc_un_op_fn
) &isl_union_set_coefficients
} },
866 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
867 (isc_un_op_fn
) &isl_union_set_solutions
} },
868 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
869 (isc_un_op_fn
) &isl_union_map_deltas
} },
870 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
871 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
872 {"dom", { -1, isl_obj_schedule
, isl_obj_union_set
,
873 (isc_un_op_fn
) &schedule_domain
} },
874 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
875 (isc_un_op_fn
) &isl_union_map_domain
} },
876 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
877 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
878 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
880 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
881 {"domain", { -1, isl_obj_schedule
, isl_obj_union_set
,
882 (isc_un_op_fn
) &schedule_domain
} },
883 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
884 (isc_un_op_fn
) &isl_union_map_domain
} },
885 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
887 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
888 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
890 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
891 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
892 (isc_un_op_fn
) &isl_union_map_domain_map
} },
893 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
894 (isc_un_op_fn
) &isl_union_map_range
} },
895 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
896 (isc_un_op_fn
) &isl_union_map_range
} },
897 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
898 (isc_un_op_fn
) &isl_union_map_range_map
} },
899 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
900 (isc_un_op_fn
) &isl_union_set_identity
} },
901 {"lattice_width", { -1, isl_obj_union_set
,
902 isl_obj_union_pw_qpolynomial
,
903 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
904 {"lb", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
905 (isc_un_op_fn
) &union_pw_qpolynomial_lower_bound
} },
906 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
907 (isc_un_op_fn
) &isl_union_map_lexmin
} },
908 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
909 (isc_un_op_fn
) &isl_union_map_lexmax
} },
910 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
911 (isc_un_op_fn
) &isl_union_set_lexmin
} },
912 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
913 (isc_un_op_fn
) &isl_union_set_lexmax
} },
914 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
915 (isc_un_op_fn
) &isl_union_set_lift
} },
916 {"map", { -1, isl_obj_schedule
, isl_obj_union_map
,
917 (isc_un_op_fn
) &schedule_map
} },
918 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
919 (isc_un_op_fn
) &isl_union_map_params
} },
920 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
921 (isc_un_op_fn
) &isl_union_set_params
} },
922 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
923 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
924 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
925 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
926 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
927 isl_obj_union_pw_qpolynomial
,
928 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
929 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
930 isl_obj_union_pw_qpolynomial
,
931 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
932 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
933 isl_obj_union_pw_qpolynomial
,
934 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
936 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
937 (isc_un_op_fn
) &parse
} },
939 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
940 (isc_un_op_fn
) &union_map_power
} },
941 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
942 (isc_un_op_fn
) &union_set_sample
} },
943 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
944 (isc_un_op_fn
) &union_map_sample
} },
945 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
946 (isc_un_op_fn
) &union_set_scan
} },
947 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
948 (isc_un_op_fn
) &union_map_scan
} },
949 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
950 isl_obj_union_pw_qpolynomial
,
951 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
952 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
953 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
954 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
955 (isc_un_op_fn
) &isl_union_set_unwrap
} },
956 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
957 (isc_un_op_fn
) &isl_union_map_wrap
} },
958 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
959 (isc_un_op_fn
) &isl_union_map_zip
} },
963 struct isl_named_obj
{
968 static void free_obj(struct isl_obj obj
)
970 obj
.type
->free(obj
.v
);
973 static int same_name(const void *entry
, const void *val
)
975 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
977 return !strcmp(named
->name
, val
);
980 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
981 char *name
, struct isl_obj obj
)
983 struct isl_hash_table_entry
*entry
;
985 struct isl_named_obj
*named
;
987 name_hash
= isl_hash_string(isl_hash_init(), name
);
988 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
993 free_obj(named
->obj
);
996 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
1000 entry
->data
= named
;
1011 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
1012 struct isl_hash_table
*table
, char *name
)
1014 struct isl_obj obj
= { isl_obj_none
, NULL
};
1015 struct isl_hash_table_entry
*entry
;
1018 name_hash
= isl_hash_string(isl_hash_init(), name
);
1019 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
1021 struct isl_named_obj
*named
;
1022 named
= entry
->data
;
1024 } else if (isdigit(name
[0]))
1025 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
1027 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1030 obj
.v
= obj
.type
->copy(obj
.v
);
1034 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1036 if (obj
.type
== super
)
1038 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1040 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1042 if (obj
.type
== isl_obj_schedule
&& super
== isl_obj_union_map
)
1044 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
1045 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
1046 int is_set
= isl_space_is_set(space
);
1047 isl_space_free(space
);
1050 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1051 super
== isl_obj_union_pw_qpolynomial
)
1053 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1054 super
== isl_obj_union_pw_qpolynomial_fold
)
1056 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1058 if (obj
.type
== isl_obj_list
) {
1059 struct isl_list
*list
= obj
.v
;
1060 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1061 return is_subtype(list
->obj
[0], super
);
1063 if (super
== isl_obj_str
)
1068 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1070 struct isl_list
*list
= obj
.v
;
1073 obj
.v
= obj
.type
->copy(obj
.v
);
1075 isl_list_free(list
);
1080 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1083 if (obj
.type
== type
)
1085 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1086 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1087 obj
.type
= isl_obj_union_set
;
1088 obj
.v
= isl_union_set_from_set(set
);
1091 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1092 obj
.type
= isl_obj_union_map
;
1093 obj
.v
= isl_union_map_from_map(obj
.v
);
1096 if (obj
.type
== isl_obj_schedule
&& type
== isl_obj_union_map
) {
1097 obj
.type
= isl_obj_union_map
;
1098 obj
.v
= schedule_map(obj
.v
);
1101 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1102 obj
.type
= isl_obj_union_set
;
1103 obj
.v
= isl_union_set_from_set(obj
.v
);
1106 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1107 type
== isl_obj_union_pw_qpolynomial
) {
1108 obj
.type
= isl_obj_union_pw_qpolynomial
;
1109 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1112 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1113 type
== isl_obj_union_pw_qpolynomial_fold
) {
1114 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1115 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1118 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1119 if (type
== isl_obj_union_map
) {
1120 obj
.type
= isl_obj_union_map
;
1123 if (type
== isl_obj_union_pw_qpolynomial
) {
1124 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1125 isl_union_set_free(obj
.v
);
1126 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1127 obj
.type
= isl_obj_union_pw_qpolynomial
;
1130 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1131 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1132 isl_union_set_free(obj
.v
);
1133 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1135 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1139 if (obj
.type
== isl_obj_list
) {
1140 struct isl_list
*list
= obj
.v
;
1141 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1142 return convert(ctx
, obj_at(obj
, 0), type
);
1144 if (type
== isl_obj_str
) {
1149 p
= isl_printer_to_str(ctx
);
1152 p
= obj
.type
->print(p
, obj
.v
);
1153 s
= isl_printer_get_str(p
);
1154 isl_printer_free(p
);
1156 str
= isl_str_from_string(ctx
, s
);
1161 obj
.type
= isl_obj_str
;
1167 obj
.type
= isl_obj_none
;
1172 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1176 struct isl_token
*tok
;
1178 tok
= isl_stream_next_token(s
);
1182 for (i
= 0; ; ++i
) {
1185 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1187 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1190 isl_token_free(tok
);
1194 for (i
= 0; ; ++i
) {
1195 if (!named_bin_ops
[i
].name
)
1197 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1199 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1202 isl_token_free(tok
);
1203 return &named_bin_ops
[i
].op
;
1206 isl_stream_push_token(s
, tok
);
1211 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1214 struct isl_token
*tok
;
1216 tok
= isl_stream_next_token(s
);
1220 for (i
= 0; ; ++i
) {
1221 if (!named_un_ops
[i
].name
)
1223 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1226 isl_token_free(tok
);
1227 return &named_un_ops
[i
].op
;
1230 isl_stream_push_token(s
, tok
);
1235 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1240 for (i
= 0; ; ++i
) {
1241 if (!named_un_ops
[i
].name
)
1243 if (named_un_ops
[i
].op
.op
!= like
->op
)
1245 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1248 return &named_un_ops
[i
].op
;
1254 static int is_assign(struct isl_stream
*s
)
1256 struct isl_token
*tok
;
1257 struct isl_token
*tok2
;
1260 tok
= isl_stream_next_token(s
);
1263 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1264 isl_stream_push_token(s
, tok
);
1268 tok2
= isl_stream_next_token(s
);
1270 isl_stream_push_token(s
, tok
);
1273 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1274 isl_stream_push_token(s
, tok2
);
1275 isl_stream_push_token(s
, tok
);
1280 static struct isl_obj
read_obj(struct isl_stream
*s
,
1281 struct isl_hash_table
*table
);
1282 static struct isl_obj
read_expr(struct isl_stream
*s
,
1283 struct isl_hash_table
*table
);
1285 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1286 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1289 struct isl_obj obj
= { isl_obj_none
, NULL
};
1291 obj
= read_obj(s
, table
);
1295 op
= find_matching_un_op(op
, obj
);
1297 ctx
= isl_stream_get_ctx(s
);
1299 isl_die(ctx
, isl_error_invalid
,
1300 "no such unary operator defined on given operand",
1303 obj
= convert(ctx
, obj
, op
->arg
);
1304 obj
.v
= op
->fn(obj
.v
);
1310 obj
.type
= isl_obj_none
;
1315 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1317 struct isl_list
*list
;
1320 if (obj
.type
!= isl_obj_union_map
)
1321 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1322 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1323 list
= isl_list_alloc(ctx
, 2);
1327 list
->obj
[0].type
= isl_obj_union_map
;
1328 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1329 list
->obj
[1].type
= isl_obj_bool
;
1330 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
1332 obj
.type
= isl_obj_list
;
1333 if (exact
< 0 || !list
->obj
[0].v
)
1339 obj
.type
= isl_obj_none
;
1344 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1346 struct isl_list
*list
= obj
.v
;
1347 struct isl_token
*tok
;
1352 tok
= isl_stream_next_token(s
);
1353 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1354 isl_stream_error(s
, tok
, "expecting index");
1356 isl_stream_push_token(s
, tok
);
1359 ctx
= isl_stream_get_ctx(s
);
1360 v
= isl_token_get_val(ctx
, tok
);
1361 i
= isl_val_get_num_si(v
);
1363 isl_token_free(tok
);
1364 isl_assert(ctx
, i
< list
->n
, goto error
);
1365 if (isl_stream_eat(s
, ']'))
1368 return obj_at(obj
, i
);
1371 obj
.type
= isl_obj_none
;
1376 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1377 struct isl_hash_table
*table
)
1382 obj
= read_expr(s
, table
);
1383 ctx
= isl_stream_get_ctx(s
);
1384 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1385 is_subtype(obj
, isl_obj_union_map
), goto error
);
1387 if (obj
.type
== isl_obj_list
) {
1388 struct isl_list
*list
= obj
.v
;
1389 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1390 obj
= obj_at(obj
, 0);
1392 if (obj
.type
== isl_obj_set
)
1393 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1394 else if (obj
.type
== isl_obj_map
)
1395 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1396 if (obj
.type
== isl_obj_union_set
) {
1397 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1399 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1403 if (isl_stream_eat(s
, ')'))
1408 isl_union_map_free(umap
);
1411 obj
.type
= isl_obj_none
;
1416 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1417 __isl_take isl_union_set
*uset
)
1419 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1420 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1422 obj
.type
= isl_obj_list
;
1423 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1428 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1429 __isl_take isl_union_map
*umap
)
1431 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1432 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1434 obj
.type
= isl_obj_list
;
1435 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1440 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1441 struct isl_obj obj
, struct isl_hash_table
*table
)
1446 arg
= read_expr(s
, table
);
1447 ctx
= isl_stream_get_ctx(s
);
1448 if (!is_subtype(arg
, isl_obj_union_map
) &&
1449 !is_subtype(arg
, isl_obj_union_set
))
1450 isl_die(ctx
, isl_error_invalid
,
1451 "expecting set of map argument", goto error
);
1453 if (arg
.type
== isl_obj_list
) {
1454 struct isl_list
*list
= arg
.v
;
1455 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1456 arg
= obj_at(arg
, 0);
1458 if (arg
.type
== isl_obj_set
)
1459 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1460 else if (arg
.type
== isl_obj_map
)
1461 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1462 if (arg
.type
== isl_obj_union_set
)
1463 obj
= apply_fun_set(obj
, arg
.v
);
1465 obj
= apply_fun_map(obj
, arg
.v
);
1469 if (isl_stream_eat(s
, ')'))
1477 obj
.type
= isl_obj_none
;
1482 struct add_vertex_data
{
1483 struct isl_list
*list
;
1487 static isl_stat
add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1489 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1493 ma
= isl_vertex_get_expr(vertex
);
1494 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1496 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1497 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1500 isl_vertex_free(vertex
);
1505 static isl_stat
set_vertices(__isl_take isl_set
*set
, void *user
)
1508 isl_basic_set
*hull
;
1509 isl_vertices
*vertices
= NULL
;
1510 struct isl_list
*list
= NULL
;
1512 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1514 set
= isl_set_remove_divs(set
);
1515 hull
= isl_set_convex_hull(set
);
1516 vertices
= isl_basic_set_compute_vertices(hull
);
1517 isl_basic_set_free(hull
);
1521 ctx
= isl_vertices_get_ctx(vertices
);
1522 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1527 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1529 data
->list
= isl_list_concat(list
, data
->list
);
1531 isl_vertices_free(vertices
);
1536 isl_vertices_free(vertices
);
1537 return isl_stat_error
;
1540 static struct isl_obj
vertices(struct isl_stream
*s
,
1541 struct isl_hash_table
*table
)
1545 struct isl_list
*list
= NULL
;
1546 isl_union_set
*uset
= NULL
;
1547 struct add_vertex_data data
= { NULL
};
1549 obj
= read_expr(s
, table
);
1550 ctx
= isl_stream_get_ctx(s
);
1551 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1552 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1556 list
= isl_list_alloc(ctx
, 0);
1562 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1565 isl_union_set_free(uset
);
1567 obj
.type
= isl_obj_list
;
1572 isl_union_set_free(uset
);
1573 isl_list_free(data
.list
);
1575 obj
.type
= isl_obj_none
;
1580 static struct isl_obj
type_of(struct isl_stream
*s
,
1581 struct isl_hash_table
*table
)
1585 const char *type
= "unknown";
1587 obj
= read_expr(s
, table
);
1589 if (obj
.type
== isl_obj_map
||
1590 obj
.type
== isl_obj_union_map
)
1592 if (obj
.type
== isl_obj_set
||
1593 obj
.type
== isl_obj_union_set
)
1595 if (obj
.type
== isl_obj_pw_multi_aff
)
1596 type
= "piecewise multi-quasiaffine expression";
1597 if (obj
.type
== isl_obj_pw_qpolynomial
||
1598 obj
.type
== isl_obj_union_pw_qpolynomial
)
1599 type
= "piecewise quasipolynomial";
1600 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1601 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1602 type
= "piecewise quasipolynomial fold";
1603 if (obj
.type
== isl_obj_list
)
1605 if (obj
.type
== isl_obj_bool
)
1607 if (obj
.type
== isl_obj_str
)
1609 if (obj
.type
== isl_obj_val
)
1611 if (obj
.type
== isl_obj_schedule
)
1615 obj
.type
= isl_obj_str
;
1616 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1621 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1622 struct isl_hash_table
*table
)
1627 obj
= read_obj(s
, table
);
1628 ctx
= isl_stream_get_ctx(s
);
1629 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1630 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1637 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1638 struct isl_hash_table
*table
)
1643 obj
= read_obj(s
, table
);
1644 ctx
= isl_stream_get_ctx(s
);
1645 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1646 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1653 /* Read a schedule in the form of either a schedule (tree) or a union map
1654 * from "s" and store the schedule in "access".
1656 static __isl_give isl_union_access_info
*access_info_set_schedule(
1657 __isl_take isl_union_access_info
*access
, struct isl_stream
*s
,
1658 struct isl_hash_table
*table
)
1663 obj
= read_obj(s
, table
);
1664 if (obj
.type
== isl_obj_schedule
)
1665 return isl_union_access_info_set_schedule(access
, obj
.v
);
1666 ctx
= isl_stream_get_ctx(s
);
1667 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1669 return isl_union_access_info_set_schedule_map(access
, obj
.v
);
1672 static struct isl_obj
last_any(struct isl_stream
*s
,
1673 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1674 __isl_take isl_union_map
*may_source
)
1676 struct isl_obj obj
= { isl_obj_none
, NULL
};
1677 isl_union_access_info
*access
;
1678 isl_union_flow
*flow
;
1679 isl_union_map
*sink
= NULL
;
1680 isl_union_map
*may_dep
;
1682 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1685 sink
= read_map(s
, table
);
1689 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1692 access
= isl_union_access_info_from_sink(sink
);
1693 access
= isl_union_access_info_set_must_source(access
, must_source
);
1694 access
= isl_union_access_info_set_may_source(access
, may_source
);
1695 access
= access_info_set_schedule(access
, s
, table
);
1696 flow
= isl_union_access_info_compute_flow(access
);
1697 may_dep
= isl_union_flow_get_may_dependence(flow
);
1698 isl_union_flow_free(flow
);
1703 obj
.type
= isl_obj_union_map
;
1708 isl_union_map_free(may_source
);
1709 isl_union_map_free(must_source
);
1710 isl_union_map_free(sink
);
1712 obj
.type
= isl_obj_none
;
1717 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1719 struct isl_obj obj
= { isl_obj_none
, NULL
};
1720 isl_union_access_info
*access
;
1721 isl_union_flow
*flow
;
1722 isl_union_map
*may_source
= NULL
;
1723 isl_union_map
*sink
= NULL
;
1724 isl_union_map
*may_dep
;
1726 may_source
= read_map(s
, table
);
1730 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1731 isl_union_map
*must_source
;
1732 must_source
= read_map(s
, table
);
1735 return last_any(s
, table
, must_source
, may_source
);
1738 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1741 sink
= read_map(s
, table
);
1745 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1748 access
= isl_union_access_info_from_sink(sink
);
1749 access
= isl_union_access_info_set_may_source(access
, may_source
);
1750 access
= access_info_set_schedule(access
, s
, table
);
1751 flow
= isl_union_access_info_compute_flow(access
);
1752 may_dep
= isl_union_flow_get_may_dependence(flow
);
1753 isl_union_flow_free(flow
);
1758 obj
.type
= isl_obj_union_map
;
1763 isl_union_map_free(may_source
);
1764 isl_union_map_free(sink
);
1766 obj
.type
= isl_obj_none
;
1771 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1773 struct isl_obj obj
= { isl_obj_none
, NULL
};
1774 struct isl_list
*list
= NULL
;
1775 isl_union_access_info
*access
;
1776 isl_union_flow
*flow
;
1777 isl_union_map
*must_source
= NULL
;
1778 isl_union_map
*sink
= NULL
;
1779 isl_union_map
*must_dep
;
1780 isl_union_map
*must_no_source
;
1782 must_source
= read_map(s
, table
);
1786 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1787 isl_union_map
*may_source
;
1788 may_source
= read_map(s
, table
);
1791 return last_any(s
, table
, must_source
, may_source
);
1794 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1798 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1801 sink
= read_map(s
, table
);
1805 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1808 access
= isl_union_access_info_from_sink(sink
);
1809 access
= isl_union_access_info_set_must_source(access
, must_source
);
1810 access
= access_info_set_schedule(access
, s
, table
);
1811 flow
= isl_union_access_info_compute_flow(access
);
1812 must_dep
= isl_union_flow_get_must_dependence(flow
);
1813 must_no_source
= isl_union_flow_get_must_no_source(flow
);
1814 isl_union_flow_free(flow
);
1816 list
->obj
[0].type
= isl_obj_union_map
;
1817 list
->obj
[0].v
= must_dep
;
1818 list
->obj
[1].type
= isl_obj_union_map
;
1819 list
->obj
[1].v
= must_no_source
;
1821 if (!must_dep
|| !must_no_source
) {
1822 isl_list_free(list
);
1827 obj
.type
= isl_obj_list
;
1831 isl_list_free(list
);
1832 isl_union_map_free(must_source
);
1833 isl_union_map_free(sink
);
1835 obj
.type
= isl_obj_none
;
1840 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1841 struct isl_hash_table
*table
)
1843 isl_union_set
*domain
;
1844 isl_union_map
*validity
;
1845 isl_union_map
*proximity
;
1847 domain
= read_set(s
, table
);
1851 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1852 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1855 isl_union_map
*umap
;
1856 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1857 umap
= read_map(s
, table
);
1858 validity
= isl_union_map_union(validity
, umap
);
1859 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1860 umap
= read_map(s
, table
);
1861 proximity
= isl_union_map_union(proximity
, umap
);
1866 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1869 static struct isl_obj
schedule(struct isl_stream
*s
,
1870 struct isl_hash_table
*table
)
1872 struct isl_obj obj
= { isl_obj_none
, NULL
};
1873 isl_schedule
*schedule
;
1875 schedule
= get_schedule(s
, table
);
1878 obj
.type
= isl_obj_schedule
;
1883 /* Read a schedule for code generation in the form of either
1884 * a schedule tree or a union map.
1885 * If the input is a set rather than a map, then we construct
1886 * an identity union map schedule on the given set.
1888 static struct isl_obj
get_codegen_schedule(struct isl_stream
*s
,
1889 struct isl_hash_table
*table
)
1894 obj
= read_obj(s
, table
);
1895 ctx
= isl_stream_get_ctx(s
);
1897 if (obj
.type
== isl_obj_schedule
)
1899 if (is_subtype(obj
, isl_obj_union_set
)) {
1900 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1901 obj
.v
= isl_union_set_identity(obj
.v
);
1902 obj
.type
= isl_obj_union_map
;
1904 if (is_subtype(obj
, isl_obj_union_map
))
1905 return convert(ctx
, obj
, isl_obj_union_map
);
1909 obj
.type
= isl_obj_none
;
1910 isl_die(ctx
, isl_error_invalid
, "expecting schedule, set or map",
1914 /* Generate an AST for the given schedule and options and return the AST.
1916 static __isl_give isl_ast_node
*get_ast_from_union_map(
1917 __isl_take isl_union_map
*schedule
, __isl_take isl_union_map
*options
)
1921 isl_ast_build
*build
;
1924 space
= isl_union_map_get_space(schedule
);
1925 context
= isl_set_universe(isl_space_params(space
));
1927 build
= isl_ast_build_from_context(context
);
1928 build
= isl_ast_build_set_options(build
, options
);
1929 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1930 isl_ast_build_free(build
);
1935 /* Generate an AST for the given schedule and return the AST.
1937 static __isl_give isl_ast_node
*get_ast_from_schedule(
1938 __isl_take isl_schedule
*schedule
)
1940 isl_ast_build
*build
;
1943 build
= isl_ast_build_alloc(isl_schedule_get_ctx(schedule
));
1944 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
1945 isl_ast_build_free(build
);
1950 /* Print the AST "tree" on the printer "p".
1952 static __isl_give isl_printer
*print_ast(__isl_take isl_printer
*p
,
1953 __isl_take isl_ast_node
*tree
)
1957 format
= isl_printer_get_output_format(p
);
1958 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1959 p
= isl_printer_print_ast_node(p
, tree
);
1960 p
= isl_printer_set_output_format(p
, format
);
1962 isl_ast_node_free(tree
);
1967 /* Perform the codegen operation.
1968 * In particular, read a schedule, check if the user has specified any options
1969 * and then generate an AST from the schedule (and options) and print it.
1970 * In case the schedule is specified as a schedule tree, the AST generation
1971 * options are embedded in the schedule, so they are not read in separately.
1973 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1974 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1979 obj
= get_codegen_schedule(s
, table
);
1983 if (obj
.type
== isl_obj_schedule
) {
1984 isl_schedule
*schedule
= obj
.v
;
1986 tree
= get_ast_from_schedule(schedule
);
1988 isl_union_map
*schedule
= obj
.v
;
1989 isl_union_map
*options
;
1991 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1992 options
= read_map(s
, table
);
1994 options
= isl_union_map_empty(
1995 isl_union_map_get_space(schedule
));
1997 tree
= get_ast_from_union_map(schedule
, options
);
2000 p
= print_ast(p
, tree
);
2002 isl_stream_eat(s
, ';');
2007 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
2009 struct isl_token
*tok
;
2013 ctx
= isl_stream_get_ctx(s
);
2014 if (isl_stream_eat_if_available(s
, '+'))
2015 return transitive_closure(ctx
, obj
);
2017 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
2018 if (obj
.type
!= isl_obj_union_map
)
2019 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2021 tok
= isl_stream_next_token(s
);
2022 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
2023 isl_stream_error(s
, tok
, "expecting integer exponent");
2025 isl_stream_push_token(s
, tok
);
2029 v
= isl_token_get_val(ctx
, tok
);
2030 if (isl_val_is_zero(v
)) {
2031 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2034 isl_stream_push_token(s
, tok
);
2038 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2039 isl_token_free(tok
);
2046 obj
.type
= isl_obj_none
;
2051 static struct isl_obj
check_assert(struct isl_stream
*s
,
2052 struct isl_hash_table
*table
)
2057 obj
= read_expr(s
, table
);
2058 ctx
= isl_stream_get_ctx(s
);
2059 if (obj
.type
!= isl_obj_bool
)
2060 isl_die(ctx
, isl_error_invalid
,
2061 "expecting boolean expression", goto error
);
2062 if (obj
.v
!= &iscc_bool_true
)
2063 isl_die(ctx
, isl_error_unknown
,
2064 "assertion failed", abort());
2067 obj
.type
= isl_obj_none
;
2072 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2076 struct isl_token
*tok
;
2077 struct isl_stream
*s_file
;
2078 struct iscc_options
*options
;
2082 tok
= isl_stream_next_token(s
);
2083 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2084 isl_stream_error(s
, tok
, "expecting filename");
2085 isl_token_free(tok
);
2089 ctx
= isl_stream_get_ctx(s
);
2090 options
= isl_ctx_peek_iscc_options(ctx
);
2091 if (!options
|| !options
->io
) {
2092 isl_token_free(tok
);
2093 isl_die(ctx
, isl_error_invalid
,
2094 "read operation not allowed", goto error
);
2097 name
= isl_token_get_str(ctx
, tok
);
2098 isl_token_free(tok
);
2099 file
= fopen(name
, "r");
2101 isl_assert(ctx
, file
, goto error
);
2103 s_file
= isl_stream_new_file(ctx
, file
);
2109 obj
= isl_stream_read_obj(s_file
);
2111 isl_stream_free(s_file
);
2116 obj
.type
= isl_obj_none
;
2121 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2122 struct isl_hash_table
*table
)
2125 struct isl_token
*tok
;
2126 struct isl_stream
*s_file
;
2127 struct iscc_options
*options
;
2133 tok
= isl_stream_next_token(s
);
2134 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2135 isl_stream_error(s
, tok
, "expecting filename");
2136 isl_token_free(tok
);
2140 obj
= read_expr(s
, table
);
2142 ctx
= isl_stream_get_ctx(s
);
2143 options
= isl_ctx_peek_iscc_options(ctx
);
2144 if (!options
|| !options
->io
) {
2145 isl_token_free(tok
);
2146 isl_die(ctx
, isl_error_invalid
,
2147 "write operation not allowed", goto error
);
2150 name
= isl_token_get_str(ctx
, tok
);
2151 isl_token_free(tok
);
2152 file
= fopen(name
, "w");
2155 isl_die(ctx
, isl_error_unknown
,
2156 "could not open file for writing", goto error
);
2158 p
= isl_printer_to_file(ctx
, file
);
2159 p
= isl_printer_set_output_format(p
, options
->format
);
2160 p
= obj
.type
->print(p
, obj
.v
);
2161 p
= isl_printer_end_line(p
);
2162 isl_printer_free(p
);
2167 obj
.type
= isl_obj_none
;
2172 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2174 struct isl_token
*tok
;
2175 struct isl_obj obj
= { isl_obj_none
, NULL
};
2177 tok
= isl_stream_next_token(s
);
2180 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2182 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2185 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2186 isl_token_free(tok
);
2188 obj
.type
= isl_obj_str
;
2190 isl_stream_push_token(s
, tok
);
2193 isl_token_free(tok
);
2197 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2199 struct isl_token
*tok
;
2200 struct isl_obj obj
= { isl_obj_none
, NULL
};
2203 tok
= isl_stream_next_token(s
);
2206 type
= isl_token_get_type(tok
);
2207 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2208 int is_true
= type
== ISL_TOKEN_TRUE
;
2209 isl_token_free(tok
);
2210 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2211 obj
.type
= isl_obj_bool
;
2213 isl_stream_push_token(s
, tok
);
2217 static __isl_give
char *read_ident(struct isl_stream
*s
)
2221 struct isl_token
*tok
, *tok2
;
2223 name
= isl_stream_read_ident_if_available(s
);
2227 tok
= isl_stream_next_token(s
);
2230 if (isl_token_get_type(tok
) != '$') {
2231 isl_stream_push_token(s
, tok
);
2234 tok2
= isl_stream_next_token(s
);
2235 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2237 isl_stream_push_token(s
, tok2
);
2238 isl_stream_push_token(s
, tok
);
2242 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2243 name
= isl_val_to_str(v
);
2245 isl_token_free(tok
);
2246 isl_token_free(tok2
);
2251 static struct isl_obj
read_list(struct isl_stream
*s
,
2252 struct isl_hash_table
*table
, struct isl_obj obj
)
2254 struct isl_list
*list
;
2256 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2260 list
->obj
[1] = read_obj(s
, table
);
2262 obj
.type
= isl_obj_list
;
2264 if (!list
->obj
[1].v
)
2267 while (isl_stream_eat_if_available(s
, ',')) {
2268 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2276 obj
.type
= isl_obj_none
;
2281 static struct isl_obj
read_obj(struct isl_stream
*s
,
2282 struct isl_hash_table
*table
)
2285 struct isl_obj obj
= { isl_obj_none
, NULL
};
2287 struct isc_un_op
*op
= NULL
;
2289 obj
= read_string_if_available(s
);
2292 obj
= read_bool_if_available(s
);
2295 ctx
= isl_stream_get_ctx(s
);
2296 if (isl_stream_eat_if_available(s
, '(')) {
2297 if (isl_stream_next_token_is(s
, ')')) {
2298 obj
.type
= isl_obj_list
;
2299 obj
.v
= isl_list_alloc(ctx
, 0);
2301 obj
= read_expr(s
, table
);
2302 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2303 obj
= read_list(s
, table
, obj
);
2305 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2308 op
= read_prefix_un_op_if_available(s
);
2310 return read_un_op_expr(s
, table
, op
);
2312 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2313 return check_assert(s
, table
);
2314 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2315 return read_from_file(s
);
2316 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2317 return write_to_file(s
, table
);
2318 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2319 return vertices(s
, table
);
2320 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2321 return any(s
, table
);
2322 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2323 return last(s
, table
);
2324 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2325 return schedule(s
, table
);
2326 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2327 return type_of(s
, table
);
2329 name
= read_ident(s
);
2331 obj
= stored_obj(ctx
, table
, name
);
2333 obj
= isl_stream_read_obj(s
);
2338 if (isl_stream_eat_if_available(s
, '^'))
2339 obj
= power(s
, obj
);
2340 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2341 obj
= obj_at_index(s
, obj
);
2342 else if (is_subtype(obj
, isl_obj_union_map
) &&
2343 isl_stream_eat_if_available(s
, '(')) {
2344 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2345 obj
= apply(s
, obj
.v
, table
);
2346 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2347 isl_stream_eat_if_available(s
, '(')) {
2348 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2349 obj
= apply_fun(s
, obj
, table
);
2350 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2351 isl_stream_eat_if_available(s
, '(')) {
2352 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2353 obj
= apply_fun(s
, obj
, table
);
2359 obj
.type
= isl_obj_none
;
2364 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2365 struct isl_obj lhs
, struct isl_obj rhs
)
2369 for (i
= 0; ; ++i
) {
2372 if (bin_ops
[i
].op
!= like
->op
)
2374 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2376 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2382 for (i
= 0; ; ++i
) {
2383 if (!named_bin_ops
[i
].name
)
2385 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2387 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2389 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2392 return &named_bin_ops
[i
].op
;
2398 static int next_is_neg_int(struct isl_stream
*s
)
2400 struct isl_token
*tok
;
2403 tok
= isl_stream_next_token(s
);
2404 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2406 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2407 ret
= isl_val_is_neg(v
);
2411 isl_stream_push_token(s
, tok
);
2416 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2417 struct isl_obj lhs
, struct isl_obj rhs
)
2421 lhs
= convert(ctx
, lhs
, op
->lhs
);
2422 rhs
= convert(ctx
, rhs
, op
->rhs
);
2423 if (op
->res
!= isl_obj_bool
)
2424 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2426 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2429 obj
.v
= iscc_bool_from_int(res
);
2436 static struct isl_obj
read_expr(struct isl_stream
*s
,
2437 struct isl_hash_table
*table
)
2440 struct isl_obj obj
= { isl_obj_none
, NULL
};
2441 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2443 obj
= read_obj(s
, table
);
2444 ctx
= isl_stream_get_ctx(s
);
2446 struct isc_bin_op
*op
= NULL
;
2448 op
= read_bin_op_if_available(s
, obj
);
2452 right_obj
= read_obj(s
, table
);
2454 op
= find_matching_bin_op(op
, obj
, right_obj
);
2457 isl_die(ctx
, isl_error_invalid
,
2458 "no such binary operator defined on given operands",
2461 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2464 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2465 right_obj
= read_obj(s
, table
);
2466 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2471 free_obj(right_obj
);
2473 obj
.type
= isl_obj_none
;
2478 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2479 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2481 /* Print "obj" to the printer "p".
2482 * If the object is a schedule, then print it in block format.
2484 static __isl_give isl_printer
*print_obj(__isl_take isl_printer
*p
,
2487 if (obj
.type
!= isl_obj_schedule
)
2488 return obj
.type
->print(p
, obj
.v
);
2490 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
2491 p
= obj
.type
->print(p
, obj
.v
);
2492 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2497 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2498 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2501 struct isl_obj obj
= { isl_obj_none
, NULL
};
2505 struct isc_bin_op
*op
= NULL
;
2510 if (isl_stream_is_empty(s
))
2513 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2514 return source_file(s
, table
, p
);
2515 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2516 return codegen(s
, table
, p
);
2518 assign
= is_assign(s
);
2520 lhs
= isl_stream_read_ident_if_available(s
);
2521 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2523 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2528 obj
= read_expr(s
, table
);
2529 ctx
= isl_stream_get_ctx(s
);
2530 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2531 fprintf(stderr
, "Interrupted\n");
2532 isl_ctx_reset_error(ctx
);
2534 if (isl_stream_eat(s
, ';'))
2538 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2539 p
= print_obj(p
, obj
);
2540 p
= isl_printer_end_line(p
);
2545 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2546 static int count
= 0;
2547 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2548 lhs
= strdup(buf
+ 1);
2550 p
= isl_printer_print_str(p
, buf
);
2551 p
= isl_printer_print_str(p
, " := ");
2552 p
= obj
.type
->print(p
, obj
.v
);
2553 p
= isl_printer_end_line(p
);
2555 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2560 isl_stream_flush_tokens(s
);
2561 isl_stream_skip_line(s
);
2567 static isl_stat
free_cb(void **entry
, void *user
)
2569 struct isl_named_obj
*named
= *entry
;
2571 free_obj(named
->obj
);
2578 static void register_named_ops(struct isl_stream
*s
)
2582 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2583 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2584 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2587 for (i
= 0; ; ++i
) {
2588 if (!named_un_ops
[i
].name
)
2590 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2591 named_un_ops
[i
].name
);
2592 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2595 for (i
= 0; ; ++i
) {
2596 if (!named_bin_ops
[i
].name
)
2598 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2599 named_bin_ops
[i
].name
);
2600 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2604 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2605 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2608 struct isl_token
*tok
;
2609 struct isl_stream
*s_file
;
2610 struct iscc_options
*options
;
2614 tok
= isl_stream_next_token(s
);
2615 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2616 isl_stream_error(s
, tok
, "expecting filename");
2617 isl_token_free(tok
);
2621 isl_stream_eat(s
, ';');
2623 ctx
= isl_stream_get_ctx(s
);
2624 options
= isl_ctx_peek_iscc_options(ctx
);
2625 if (!options
|| !options
->io
) {
2626 isl_token_free(tok
);
2627 isl_die(ctx
, isl_error_invalid
,
2628 "source operation not allowed", return p
);
2631 name
= isl_token_get_str(ctx
, tok
);
2632 isl_token_free(tok
);
2633 file
= fopen(name
, "r");
2635 isl_assert(ctx
, file
, return p
);
2637 s_file
= isl_stream_new_file(ctx
, file
);
2643 register_named_ops(s_file
);
2645 while (!isl_stream_is_empty(s_file
))
2646 p
= read_line(s_file
, table
, p
, 0);
2648 isl_stream_free(s_file
);
2654 int main(int argc
, char **argv
)
2656 struct isl_ctx
*ctx
;
2657 struct isl_stream
*s
;
2658 struct isl_hash_table
*table
;
2659 struct iscc_options
*options
;
2661 int tty
= isatty(0);
2663 options
= iscc_options_new_with_defaults();
2666 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2667 pet_options_set_autodetect(ctx
, 1);
2668 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2669 s
= isl_stream_new_file(ctx
, stdin
);
2671 table
= isl_hash_table_alloc(ctx
, 10);
2673 p
= isl_printer_to_file(ctx
, stdout
);
2674 p
= isl_printer_set_output_format(p
, options
->format
);
2677 register_named_ops(s
);
2679 install_signal_handler(ctx
);
2681 while (p
&& !isl_stream_is_empty(s
)) {
2682 isl_ctx_resume(ctx
);
2683 p
= read_line(s
, table
, p
, tty
);
2686 remove_signal_handler(ctx
);
2688 isl_printer_free(p
);
2689 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2690 isl_hash_table_free(ctx
, table
);