8 #include <isl/stream.h>
11 #include <isl/vertices.h>
14 #include <isl/schedule.h>
15 #include <isl/ast_build.h>
16 #include <isl_obj_list.h>
17 #include <isl_obj_str.h>
18 #include <barvinok/isl.h>
19 #include <barvinok/options.h>
20 #include "lattice_width.h"
27 static isl_ctx
*main_ctx
;
29 static void handler(int signum
)
31 if (isl_ctx_aborted(main_ctx
))
33 isl_ctx_abort(main_ctx
);
36 static struct sigaction sa_old
;
38 static void install_signal_handler(isl_ctx
*ctx
)
44 memset(&sa
, 0, sizeof(struct sigaction
));
45 sa
.sa_handler
= &handler
;
46 sa
.sa_flags
= SA_RESTART
;
47 sigaction(SIGINT
, &sa
, &sa_old
);
50 static void remove_signal_handler(isl_ctx
*ctx
)
52 sigaction(SIGINT
, &sa_old
, NULL
);
57 static void install_signal_handler(isl_ctx
*ctx
)
61 static void remove_signal_handler(isl_ctx
*ctx
)
71 int pet_options_set_autodetect(isl_ctx
*ctx
, int val
)
77 static int iscc_bool_false
= 0;
78 static int iscc_bool_true
= 1;
79 static int iscc_bool_error
= -1;
81 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
82 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
83 ISCC_SCHEDULE
, ISCC_SCHEDULE_FOREST
,
84 ISCC_MINIMIZING
, ISCC_RESPECTING
,
85 ISCC_CODEGEN
, ISCC_USING
,
86 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
88 static const char *op_name
[ISCC_N_OP
] = {
89 [ISCC_ASSERT
] = "assert",
91 [ISCC_WRITE
] = "write",
92 [ISCC_PRINT
] = "print",
93 [ISCC_SOURCE
] = "source",
94 [ISCC_VERTICES
] = "vertices",
97 [ISCC_BEFORE
] = "before",
98 [ISCC_UNDER
] = "under",
99 [ISCC_SCHEDULE
] = "schedule",
100 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
101 [ISCC_MINIMIZING
] = "minimizing",
102 [ISCC_RESPECTING
] = "respecting",
103 [ISCC_CODEGEN
] = "codegen",
104 [ISCC_USING
] = "using",
105 [ISCC_TYPEOF
] = "typeof"
107 static enum isl_token_type iscc_op
[ISCC_N_OP
];
109 struct isl_arg_choice iscc_format
[] = {
110 {"isl", ISL_FORMAT_ISL
},
111 {"omega", ISL_FORMAT_OMEGA
},
112 {"polylib", ISL_FORMAT_POLYLIB
},
113 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
114 {"latex", ISL_FORMAT_LATEX
},
119 struct iscc_options
{
120 struct barvinok_options
*barvinok
;
121 struct pet_options
*pet
;
126 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
127 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
130 ISL_ARG_CHILD(struct iscc_options
, pet
, "pet", &pet_options_args
, "pet options")
132 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
133 iscc_format
, ISL_FORMAT_ISL
, "output format")
134 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
135 "allow read and write operations")
138 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
139 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
141 static void *isl_obj_bool_copy(void *v
)
146 static void isl_obj_bool_free(void *v
)
150 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
153 if (v
== &iscc_bool_true
)
154 return isl_printer_print_str(p
, "True");
155 else if (v
== &iscc_bool_false
)
156 return isl_printer_print_str(p
, "False");
158 return isl_printer_print_str(p
, "Error");
161 static void *isl_obj_bool_add(void *v1
, void *v2
)
166 struct isl_obj_vtable isl_obj_bool_vtable
= {
172 #define isl_obj_bool (&isl_obj_bool_vtable)
174 int *iscc_bool_from_int(int res
)
176 return res
< 0 ? &iscc_bool_error
:
177 res
? &iscc_bool_true
: &iscc_bool_false
;
180 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
181 __isl_take isl_union_map
*map2
)
183 return isl_union_map_is_subset(map2
, map1
);
185 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
186 __isl_take isl_union_set
*set2
)
188 return isl_union_set_is_subset(set2
, set1
);
191 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
192 __isl_take isl_union_map
*map2
)
194 return isl_union_map_is_strict_subset(map2
, map1
);
196 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
197 __isl_take isl_union_set
*set2
)
199 return isl_union_set_is_strict_subset(set2
, set1
);
202 extern struct isl_obj_vtable isl_obj_list_vtable
;
203 #define isl_obj_list (&isl_obj_list_vtable)
205 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
206 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
208 enum isl_token_type op
;
214 isc_bin_test_fn test
;
217 struct isc_named_bin_op
{
219 struct isc_bin_op op
;
223 isl_union_pw_qpolynomial
*upwqp
;
224 isl_union_pw_qpolynomial
*res
;
227 static isl_stat
eval_at(__isl_take isl_point
*pnt
, void *user
)
229 struct iscc_at
*at
= (struct iscc_at
*) user
;
234 set
= isl_set_from_point(isl_point_copy(pnt
));
235 v
= isl_union_pw_qpolynomial_eval(
236 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
237 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
239 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
240 isl_union_pw_qpolynomial_from_pw_qpolynomial(
241 isl_pw_qpolynomial_alloc(set
, qp
)));
246 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
247 __isl_take isl_union_pw_qpolynomial
*upwqp
,
248 __isl_take isl_union_set
*uset
)
253 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
255 isl_union_set_foreach_point(uset
, eval_at
, &at
);
257 isl_union_pw_qpolynomial_free(upwqp
);
258 isl_union_set_free(uset
);
263 struct iscc_fold_at
{
264 isl_union_pw_qpolynomial_fold
*upwf
;
265 isl_union_pw_qpolynomial
*res
;
268 static isl_stat
eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
270 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
275 set
= isl_set_from_point(isl_point_copy(pnt
));
276 v
= isl_union_pw_qpolynomial_fold_eval(
277 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
278 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
280 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
281 isl_union_pw_qpolynomial_from_pw_qpolynomial(
282 isl_pw_qpolynomial_alloc(set
, qp
)));
287 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
288 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
289 __isl_take isl_union_set
*uset
)
291 struct iscc_fold_at at
;
294 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
296 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
298 isl_union_pw_qpolynomial_fold_free(upwf
);
299 isl_union_set_free(uset
);
304 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
305 __isl_take isl_union_pw_qpolynomial
*upwqp
,
306 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
308 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
312 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
313 __isl_take isl_union_map
*umap
,
314 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
317 struct isl_list
*list
;
320 ctx
= isl_union_map_get_ctx(umap
);
321 list
= isl_list_alloc(ctx
, 2);
325 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
326 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
328 list
->obj
[1].type
= isl_obj_bool
;
329 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
330 if (tight
< 0 || !list
->obj
[0].v
)
335 isl_union_map_free(umap
);
336 isl_union_pw_qpolynomial_fold_free(upwf
);
342 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
343 __isl_take isl_union_set
*uset
,
344 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
347 struct isl_list
*list
;
350 ctx
= isl_union_set_get_ctx(uset
);
351 list
= isl_list_alloc(ctx
, 2);
355 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
356 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
358 list
->obj
[1].type
= isl_obj_bool
;
359 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
360 if (tight
< 0 || !list
->obj
[0].v
)
365 isl_union_set_free(uset
);
366 isl_union_pw_qpolynomial_fold_free(upwf
);
372 static __isl_give isl_union_pw_qpolynomial
*isl_val_mul_union_pw_qpolynomial(
373 __isl_take isl_val
*v
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
375 return isl_union_pw_qpolynomial_scale_val(upwqp
, v
);
378 static __isl_give isl_union_pw_qpolynomial_fold
*
379 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val
*v
,
380 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
382 return isl_union_pw_qpolynomial_fold_scale_val(upwf
, v
);
385 /* Are the two strings "str1" and "str2" equal to each other?
387 static int str_eq(__isl_keep isl_str
*str1
, __isl_keep isl_str
*str2
)
392 return !strcmp(str1
->s
, str2
->s
);
395 struct isc_bin_op bin_ops
[] = {
396 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
397 (isc_bin_op_fn
) &isl_val_add
},
398 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
399 (isc_bin_op_fn
) &isl_val_sub
},
400 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
401 (isc_bin_op_fn
) &isl_val_mul
},
402 { '+', isl_obj_pw_multi_aff
, isl_obj_pw_multi_aff
,
403 isl_obj_pw_multi_aff
,
404 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
405 { '+', isl_obj_union_set
, isl_obj_union_set
,
407 (isc_bin_op_fn
) &isl_union_set_union
},
408 { '+', isl_obj_union_map
, isl_obj_union_map
,
410 (isc_bin_op_fn
) &isl_union_map_union
},
411 { '-', isl_obj_union_set
, isl_obj_union_set
,
413 (isc_bin_op_fn
) &isl_union_set_subtract
},
414 { '-', isl_obj_union_map
, isl_obj_union_map
,
416 (isc_bin_op_fn
) &isl_union_map_subtract
},
417 { '*', isl_obj_union_set
, isl_obj_union_set
,
419 (isc_bin_op_fn
) &isl_union_set_intersect
},
420 { '*', isl_obj_union_map
, isl_obj_union_map
,
422 (isc_bin_op_fn
) &isl_union_map_intersect
},
423 { '*', isl_obj_union_map
, isl_obj_union_set
,
425 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
426 { '.', isl_obj_union_map
, isl_obj_union_map
,
428 (isc_bin_op_fn
) &isl_union_map_apply_range
},
429 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
430 isl_obj_union_pw_qpolynomial
,
431 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
432 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
434 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
435 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
437 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
438 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
439 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
440 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
441 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
442 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
444 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
445 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
447 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
448 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
450 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
451 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
453 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
454 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
456 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
457 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
459 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
460 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
463 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
464 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
467 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
468 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
470 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
471 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
473 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
474 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
476 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
477 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
479 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
480 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
482 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
483 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
485 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
486 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
488 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
489 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
491 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
492 { '.', isl_obj_union_pw_qpolynomial_fold
,
493 isl_obj_union_pw_qpolynomial_fold
,
494 isl_obj_union_pw_qpolynomial_fold
,
495 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
496 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
497 isl_obj_union_pw_qpolynomial
,
498 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
499 { '+', isl_obj_union_pw_qpolynomial
,
500 isl_obj_union_pw_qpolynomial_fold
,
501 isl_obj_union_pw_qpolynomial_fold
,
502 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
503 { '+', isl_obj_union_pw_qpolynomial_fold
,
504 isl_obj_union_pw_qpolynomial
,
505 isl_obj_union_pw_qpolynomial_fold
,
506 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
507 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
508 isl_obj_union_pw_qpolynomial
,
509 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
510 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
511 isl_obj_union_pw_qpolynomial
,
512 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
513 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
514 isl_obj_union_pw_qpolynomial
,
515 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
516 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
517 isl_obj_union_pw_qpolynomial_fold
,
518 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
519 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
520 isl_obj_union_pw_qpolynomial_fold
,
521 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
522 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
523 isl_obj_union_pw_qpolynomial
,
524 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
525 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
526 isl_obj_union_pw_qpolynomial
,
527 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
528 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
529 isl_obj_union_pw_qpolynomial_fold
,
530 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
531 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
532 isl_obj_union_pw_qpolynomial
,
533 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
534 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
535 isl_obj_union_pw_qpolynomial
,
536 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
537 { '%', isl_obj_union_set
, isl_obj_union_set
,
539 (isc_bin_op_fn
) &isl_union_set_gist
},
540 { '%', isl_obj_union_map
, isl_obj_union_map
,
542 (isc_bin_op_fn
) &isl_union_map_gist
},
543 { '%', isl_obj_union_map
, isl_obj_union_set
,
545 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
546 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
547 isl_obj_union_pw_qpolynomial
,
548 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
549 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
550 isl_obj_union_pw_qpolynomial_fold
,
551 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
552 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
553 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
554 { .test
= (isc_bin_test_fn
)
555 &isl_union_pw_qpolynomial_plain_is_equal
} },
556 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
557 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
558 { .test
= (isc_bin_test_fn
)
559 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
560 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
561 (isc_bin_op_fn
) &isl_str_concat
},
562 { '=', isl_obj_str
, isl_obj_str
, isl_obj_bool
,
563 { .test
= (isc_bin_test_fn
) &str_eq
} },
567 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
568 __isl_take isl_union_map
*umap2
)
570 return isl_union_map_apply_range(umap2
, umap1
);
573 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
574 __isl_take isl_union_pw_qpolynomial
*upwqp
,
575 __isl_take isl_union_map
*umap
)
577 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
580 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
581 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
582 __isl_take isl_union_map
*umap
)
584 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
587 struct isc_named_bin_op named_bin_ops
[] = {
588 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
590 (isc_bin_op_fn
) &map_after_map
} },
591 { "after", { -1, isl_obj_union_pw_qpolynomial
,
592 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
593 (isc_bin_op_fn
) &qpolynomial_after_map
} },
594 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
595 isl_obj_union_map
, isl_obj_list
,
596 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
597 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
599 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
600 { "before", { -1, isl_obj_union_map
,
601 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
602 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
603 { "before", { -1, isl_obj_union_map
,
604 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
605 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
606 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
608 (isc_bin_op_fn
) &isl_union_set_product
} },
609 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
611 (isc_bin_op_fn
) &isl_union_map_product
} },
615 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
617 return isl_set_from_basic_set(isl_union_set_sample(uset
));
620 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
622 return isl_map_from_basic_map(isl_union_map_sample(umap
));
625 static __isl_give
struct isl_list
*union_map_power(
626 __isl_take isl_union_map
*umap
)
629 struct isl_list
*list
;
632 ctx
= isl_union_map_get_ctx(umap
);
633 list
= isl_list_alloc(ctx
, 2);
637 list
->obj
[0].type
= isl_obj_union_map
;
638 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
639 list
->obj
[1].type
= isl_obj_bool
;
640 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
641 if (exact
< 0 || !list
->obj
[0].v
)
646 isl_union_map_free(umap
);
652 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
653 __isl_take isl_union_pw_qpolynomial
*upwqp
)
656 struct isl_list
*list
;
659 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
660 list
= isl_list_alloc(ctx
, 2);
664 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
665 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
,
666 isl_fold_max
, &tight
);
667 list
->obj
[1].type
= isl_obj_bool
;
668 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
669 if (tight
< 0 || !list
->obj
[0].v
)
674 isl_union_pw_qpolynomial_free(upwqp
);
681 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
684 struct isl_list
*list
;
685 struct pet_scop
*scop
;
686 isl_union_map
*sched
, *may_reads
, *must_writes
, *may_writes
;
687 isl_union_set
*domain
;
688 struct iscc_options
*options
;
694 options
= isl_ctx_peek_iscc_options(ctx
);
695 if (!options
|| !options
->io
) {
697 isl_die(ctx
, isl_error_invalid
,
698 "parse_file operation not allowed", return NULL
);
701 list
= isl_list_alloc(ctx
, 5);
705 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
706 domain
= pet_scop_collect_domains(scop
);
707 sched
= scop
? isl_schedule_get_map(scop
->schedule
) : NULL
;
708 may_reads
= pet_scop_collect_may_reads(scop
);
709 may_writes
= pet_scop_collect_may_writes(scop
);
710 must_writes
= pet_scop_collect_must_writes(scop
);
713 list
->obj
[0].type
= isl_obj_union_set
;
714 list
->obj
[0].v
= domain
;
715 list
->obj
[1].type
= isl_obj_union_map
;
716 list
->obj
[1].v
= must_writes
;
717 list
->obj
[2].type
= isl_obj_union_map
;
718 list
->obj
[2].v
= may_writes
;
719 list
->obj
[3].type
= isl_obj_union_map
;
720 list
->obj
[3].v
= may_reads
;
721 list
->obj
[4].type
= isl_obj_union_map
;
722 list
->obj
[4].v
= sched
;
724 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
725 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
737 static isl_stat
add_point(__isl_take isl_point
*pnt
, void *user
)
739 isl_union_set
**scan
= (isl_union_set
**) user
;
741 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
746 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
750 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
752 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
753 isl_union_set_free(scan
);
757 isl_union_set_free(uset
);
761 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
763 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
766 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
767 __isl_take isl_union_pw_qpolynomial
*upwqp
)
769 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
772 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
773 __isl_take isl_union_pw_qpolynomial
*upwqp
)
775 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
778 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
779 __isl_take isl_union_pw_qpolynomial
*upwqp
)
781 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
784 typedef void *(*isc_un_op_fn
)(void *arg
);
786 enum isl_token_type op
;
791 struct isc_named_un_op
{
795 struct isc_named_un_op named_un_ops
[] = {
796 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
797 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
798 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
799 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
800 {"card", { -1, isl_obj_union_set
,
801 isl_obj_union_pw_qpolynomial
,
802 (isc_un_op_fn
) &isl_union_set_card
} },
803 {"card", { -1, isl_obj_union_map
,
804 isl_obj_union_pw_qpolynomial
,
805 (isc_un_op_fn
) &isl_union_map_card
} },
806 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
807 (isc_un_op_fn
) &isl_union_set_coalesce
} },
808 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
809 (isc_un_op_fn
) &isl_union_map_coalesce
} },
810 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
811 isl_obj_union_pw_qpolynomial
,
812 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
813 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
814 isl_obj_union_pw_qpolynomial_fold
,
815 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
816 {"coefficients", { -1, isl_obj_union_set
,
818 (isc_un_op_fn
) &isl_union_set_coefficients
} },
819 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
820 (isc_un_op_fn
) &isl_union_set_solutions
} },
821 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
822 (isc_un_op_fn
) &isl_union_map_deltas
} },
823 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
824 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
825 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
826 (isc_un_op_fn
) &isl_union_map_domain
} },
827 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
828 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
829 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
831 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
832 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
833 (isc_un_op_fn
) &isl_union_map_domain
} },
834 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
836 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
837 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
839 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
840 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
841 (isc_un_op_fn
) &isl_union_map_domain_map
} },
842 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
843 (isc_un_op_fn
) &isl_union_map_range
} },
844 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
845 (isc_un_op_fn
) &isl_union_map_range
} },
846 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
847 (isc_un_op_fn
) &isl_union_map_range_map
} },
848 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
849 (isc_un_op_fn
) &isl_union_set_identity
} },
850 {"lattice_width", { -1, isl_obj_union_set
,
851 isl_obj_union_pw_qpolynomial
,
852 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
853 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
854 (isc_un_op_fn
) &isl_union_map_lexmin
} },
855 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
856 (isc_un_op_fn
) &isl_union_map_lexmax
} },
857 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
858 (isc_un_op_fn
) &isl_union_set_lexmin
} },
859 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
860 (isc_un_op_fn
) &isl_union_set_lexmax
} },
861 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
862 (isc_un_op_fn
) &isl_union_set_lift
} },
863 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
864 (isc_un_op_fn
) &isl_union_map_params
} },
865 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
866 (isc_un_op_fn
) &isl_union_set_params
} },
867 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
868 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
869 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
870 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
871 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
872 isl_obj_union_pw_qpolynomial
,
873 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
874 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
875 isl_obj_union_pw_qpolynomial
,
876 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
877 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
878 isl_obj_union_pw_qpolynomial
,
879 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
881 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
882 (isc_un_op_fn
) &parse
} },
884 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
885 (isc_un_op_fn
) &union_map_power
} },
886 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
887 (isc_un_op_fn
) &union_set_sample
} },
888 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
889 (isc_un_op_fn
) &union_map_sample
} },
890 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
891 (isc_un_op_fn
) &union_set_scan
} },
892 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
893 (isc_un_op_fn
) &union_map_scan
} },
894 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
895 isl_obj_union_pw_qpolynomial
,
896 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
897 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
898 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
899 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
900 (isc_un_op_fn
) &isl_union_set_unwrap
} },
901 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
902 (isc_un_op_fn
) &isl_union_map_wrap
} },
903 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
904 (isc_un_op_fn
) &isl_union_map_zip
} },
908 struct isl_named_obj
{
913 static void free_obj(struct isl_obj obj
)
915 obj
.type
->free(obj
.v
);
918 static int same_name(const void *entry
, const void *val
)
920 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
922 return !strcmp(named
->name
, val
);
925 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
926 char *name
, struct isl_obj obj
)
928 struct isl_hash_table_entry
*entry
;
930 struct isl_named_obj
*named
;
932 name_hash
= isl_hash_string(isl_hash_init(), name
);
933 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
938 free_obj(named
->obj
);
941 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
956 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
957 struct isl_hash_table
*table
, char *name
)
959 struct isl_obj obj
= { isl_obj_none
, NULL
};
960 struct isl_hash_table_entry
*entry
;
963 name_hash
= isl_hash_string(isl_hash_init(), name
);
964 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
966 struct isl_named_obj
*named
;
969 } else if (isdigit(name
[0]))
970 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
972 fprintf(stderr
, "unknown identifier '%s'\n", name
);
975 obj
.v
= obj
.type
->copy(obj
.v
);
979 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
981 if (obj
.type
== super
)
983 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
985 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
987 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
988 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
989 int is_set
= isl_space_is_set(space
);
990 isl_space_free(space
);
993 if (obj
.type
== isl_obj_pw_qpolynomial
&&
994 super
== isl_obj_union_pw_qpolynomial
)
996 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
997 super
== isl_obj_union_pw_qpolynomial_fold
)
999 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1001 if (obj
.type
== isl_obj_list
) {
1002 struct isl_list
*list
= obj
.v
;
1003 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1004 return is_subtype(list
->obj
[0], super
);
1006 if (super
== isl_obj_str
)
1011 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1013 struct isl_list
*list
= obj
.v
;
1016 obj
.v
= obj
.type
->copy(obj
.v
);
1018 isl_list_free(list
);
1023 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1026 if (obj
.type
== type
)
1028 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1029 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1030 obj
.type
= isl_obj_union_set
;
1031 obj
.v
= isl_union_set_from_set(set
);
1034 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1035 obj
.type
= isl_obj_union_map
;
1036 obj
.v
= isl_union_map_from_map(obj
.v
);
1039 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1040 obj
.type
= isl_obj_union_set
;
1041 obj
.v
= isl_union_set_from_set(obj
.v
);
1044 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1045 type
== isl_obj_union_pw_qpolynomial
) {
1046 obj
.type
= isl_obj_union_pw_qpolynomial
;
1047 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1050 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1051 type
== isl_obj_union_pw_qpolynomial_fold
) {
1052 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1053 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1056 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1057 if (type
== isl_obj_union_map
) {
1058 obj
.type
= isl_obj_union_map
;
1061 if (type
== isl_obj_union_pw_qpolynomial
) {
1062 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1063 isl_union_set_free(obj
.v
);
1064 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1065 obj
.type
= isl_obj_union_pw_qpolynomial
;
1068 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1069 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1070 isl_union_set_free(obj
.v
);
1071 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1073 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1077 if (obj
.type
== isl_obj_list
) {
1078 struct isl_list
*list
= obj
.v
;
1079 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1080 return convert(ctx
, obj_at(obj
, 0), type
);
1082 if (type
== isl_obj_str
) {
1087 p
= isl_printer_to_str(ctx
);
1090 p
= obj
.type
->print(p
, obj
.v
);
1091 s
= isl_printer_get_str(p
);
1092 isl_printer_free(p
);
1094 str
= isl_str_from_string(ctx
, s
);
1099 obj
.type
= isl_obj_str
;
1105 obj
.type
= isl_obj_none
;
1110 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1114 struct isl_token
*tok
;
1116 tok
= isl_stream_next_token(s
);
1120 for (i
= 0; ; ++i
) {
1123 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1125 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1128 isl_token_free(tok
);
1132 for (i
= 0; ; ++i
) {
1133 if (!named_bin_ops
[i
].name
)
1135 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1137 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1140 isl_token_free(tok
);
1141 return &named_bin_ops
[i
].op
;
1144 isl_stream_push_token(s
, tok
);
1149 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1152 struct isl_token
*tok
;
1154 tok
= isl_stream_next_token(s
);
1158 for (i
= 0; ; ++i
) {
1159 if (!named_un_ops
[i
].name
)
1161 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1164 isl_token_free(tok
);
1165 return &named_un_ops
[i
].op
;
1168 isl_stream_push_token(s
, tok
);
1173 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1178 for (i
= 0; ; ++i
) {
1179 if (!named_un_ops
[i
].name
)
1181 if (named_un_ops
[i
].op
.op
!= like
->op
)
1183 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1186 return &named_un_ops
[i
].op
;
1192 static int is_assign(struct isl_stream
*s
)
1194 struct isl_token
*tok
;
1195 struct isl_token
*tok2
;
1198 tok
= isl_stream_next_token(s
);
1201 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1202 isl_stream_push_token(s
, tok
);
1206 tok2
= isl_stream_next_token(s
);
1208 isl_stream_push_token(s
, tok
);
1211 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1212 isl_stream_push_token(s
, tok2
);
1213 isl_stream_push_token(s
, tok
);
1218 static struct isl_obj
read_obj(struct isl_stream
*s
,
1219 struct isl_hash_table
*table
);
1220 static struct isl_obj
read_expr(struct isl_stream
*s
,
1221 struct isl_hash_table
*table
);
1223 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1224 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1227 struct isl_obj obj
= { isl_obj_none
, NULL
};
1229 obj
= read_obj(s
, table
);
1233 op
= find_matching_un_op(op
, obj
);
1235 ctx
= isl_stream_get_ctx(s
);
1237 isl_die(ctx
, isl_error_invalid
,
1238 "no such unary operator defined on given operand",
1241 obj
= convert(ctx
, obj
, op
->arg
);
1242 obj
.v
= op
->fn(obj
.v
);
1248 obj
.type
= isl_obj_none
;
1253 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1255 struct isl_list
*list
;
1258 if (obj
.type
!= isl_obj_union_map
)
1259 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1260 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1261 list
= isl_list_alloc(ctx
, 2);
1265 list
->obj
[0].type
= isl_obj_union_map
;
1266 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1267 list
->obj
[1].type
= isl_obj_bool
;
1268 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
1270 obj
.type
= isl_obj_list
;
1271 if (exact
< 0 || !list
->obj
[0].v
)
1277 obj
.type
= isl_obj_none
;
1282 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1284 struct isl_list
*list
= obj
.v
;
1285 struct isl_token
*tok
;
1290 tok
= isl_stream_next_token(s
);
1291 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1292 isl_stream_error(s
, tok
, "expecting index");
1294 isl_stream_push_token(s
, tok
);
1297 ctx
= isl_stream_get_ctx(s
);
1298 v
= isl_token_get_val(ctx
, tok
);
1299 i
= isl_val_get_num_si(v
);
1301 isl_token_free(tok
);
1302 isl_assert(ctx
, i
< list
->n
, goto error
);
1303 if (isl_stream_eat(s
, ']'))
1306 return obj_at(obj
, i
);
1309 obj
.type
= isl_obj_none
;
1314 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1315 struct isl_hash_table
*table
)
1320 obj
= read_expr(s
, table
);
1321 ctx
= isl_stream_get_ctx(s
);
1322 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1323 is_subtype(obj
, isl_obj_union_map
), goto error
);
1325 if (obj
.type
== isl_obj_list
) {
1326 struct isl_list
*list
= obj
.v
;
1327 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1328 obj
= obj_at(obj
, 0);
1330 if (obj
.type
== isl_obj_set
)
1331 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1332 else if (obj
.type
== isl_obj_map
)
1333 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1334 if (obj
.type
== isl_obj_union_set
) {
1335 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1337 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1341 if (isl_stream_eat(s
, ')'))
1346 isl_union_map_free(umap
);
1349 obj
.type
= isl_obj_none
;
1354 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1355 __isl_take isl_union_set
*uset
)
1357 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1358 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1360 obj
.type
= isl_obj_list
;
1361 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1366 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1367 __isl_take isl_union_map
*umap
)
1369 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1370 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1372 obj
.type
= isl_obj_list
;
1373 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1378 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1379 struct isl_obj obj
, struct isl_hash_table
*table
)
1384 arg
= read_expr(s
, table
);
1385 ctx
= isl_stream_get_ctx(s
);
1386 if (!is_subtype(arg
, isl_obj_union_map
) &&
1387 !is_subtype(arg
, isl_obj_union_set
))
1388 isl_die(ctx
, isl_error_invalid
,
1389 "expecting set of map argument", goto error
);
1391 if (arg
.type
== isl_obj_list
) {
1392 struct isl_list
*list
= arg
.v
;
1393 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1394 arg
= obj_at(arg
, 0);
1396 if (arg
.type
== isl_obj_set
)
1397 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1398 else if (arg
.type
== isl_obj_map
)
1399 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1400 if (arg
.type
== isl_obj_union_set
)
1401 obj
= apply_fun_set(obj
, arg
.v
);
1403 obj
= apply_fun_map(obj
, arg
.v
);
1407 if (isl_stream_eat(s
, ')'))
1415 obj
.type
= isl_obj_none
;
1420 struct add_vertex_data
{
1421 struct isl_list
*list
;
1425 static isl_stat
add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1427 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1431 ma
= isl_vertex_get_expr(vertex
);
1432 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1434 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1435 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1438 isl_vertex_free(vertex
);
1443 static isl_stat
set_vertices(__isl_take isl_set
*set
, void *user
)
1446 isl_basic_set
*hull
;
1447 isl_vertices
*vertices
= NULL
;
1448 struct isl_list
*list
= NULL
;
1450 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1452 set
= isl_set_remove_divs(set
);
1453 hull
= isl_set_convex_hull(set
);
1454 vertices
= isl_basic_set_compute_vertices(hull
);
1455 isl_basic_set_free(hull
);
1459 ctx
= isl_vertices_get_ctx(vertices
);
1460 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1465 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1467 data
->list
= isl_list_concat(list
, data
->list
);
1469 isl_vertices_free(vertices
);
1474 isl_vertices_free(vertices
);
1475 return isl_stat_error
;
1478 static struct isl_obj
vertices(struct isl_stream
*s
,
1479 struct isl_hash_table
*table
)
1483 struct isl_list
*list
= NULL
;
1484 isl_union_set
*uset
= NULL
;
1485 struct add_vertex_data data
= { NULL
};
1487 obj
= read_expr(s
, table
);
1488 ctx
= isl_stream_get_ctx(s
);
1489 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1490 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1494 list
= isl_list_alloc(ctx
, 0);
1500 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1503 isl_union_set_free(uset
);
1505 obj
.type
= isl_obj_list
;
1510 isl_union_set_free(uset
);
1511 isl_list_free(data
.list
);
1513 obj
.type
= isl_obj_none
;
1518 static struct isl_obj
type_of(struct isl_stream
*s
,
1519 struct isl_hash_table
*table
)
1523 const char *type
= "unknown";
1525 obj
= read_expr(s
, table
);
1527 if (obj
.type
== isl_obj_map
||
1528 obj
.type
== isl_obj_union_map
)
1530 if (obj
.type
== isl_obj_set
||
1531 obj
.type
== isl_obj_union_set
)
1533 if (obj
.type
== isl_obj_pw_multi_aff
)
1534 type
= "piecewise multi-quasiaffine expression";
1535 if (obj
.type
== isl_obj_pw_qpolynomial
||
1536 obj
.type
== isl_obj_union_pw_qpolynomial
)
1537 type
= "piecewise quasipolynomial";
1538 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1539 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1540 type
= "piecewise quasipolynomial fold";
1541 if (obj
.type
== isl_obj_list
)
1543 if (obj
.type
== isl_obj_bool
)
1545 if (obj
.type
== isl_obj_str
)
1547 if (obj
.type
== isl_obj_val
)
1551 obj
.type
= isl_obj_str
;
1552 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1557 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1558 struct isl_hash_table
*table
)
1563 obj
= read_obj(s
, table
);
1564 ctx
= isl_stream_get_ctx(s
);
1565 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1566 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1573 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1574 struct isl_hash_table
*table
)
1579 obj
= read_obj(s
, table
);
1580 ctx
= isl_stream_get_ctx(s
);
1581 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1582 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1589 static struct isl_obj
last_any(struct isl_stream
*s
,
1590 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1591 __isl_take isl_union_map
*may_source
)
1593 struct isl_obj obj
= { isl_obj_none
, NULL
};
1594 isl_union_map
*sink
= NULL
;
1595 isl_union_map
*schedule
= NULL
;
1596 isl_union_map
*may_dep
;
1597 isl_union_map
*must_dep
;
1599 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1602 sink
= read_map(s
, table
);
1606 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1609 schedule
= read_map(s
, table
);
1613 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1614 schedule
, &must_dep
, &may_dep
,
1618 obj
.type
= isl_obj_union_map
;
1619 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1623 isl_union_map_free(may_source
);
1624 isl_union_map_free(must_source
);
1625 isl_union_map_free(sink
);
1626 isl_union_map_free(schedule
);
1628 obj
.type
= isl_obj_none
;
1633 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1635 struct isl_obj obj
= { isl_obj_none
, NULL
};
1636 isl_union_map
*must_source
= NULL
;
1637 isl_union_map
*may_source
= NULL
;
1638 isl_union_map
*sink
= NULL
;
1639 isl_union_map
*schedule
= NULL
;
1640 isl_union_map
*may_dep
;
1642 may_source
= read_map(s
, table
);
1646 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1647 must_source
= read_map(s
, table
);
1650 return last_any(s
, table
, must_source
, may_source
);
1653 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1656 sink
= read_map(s
, table
);
1660 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1663 schedule
= read_map(s
, table
);
1667 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1668 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1669 schedule
, NULL
, &may_dep
,
1673 obj
.type
= isl_obj_union_map
;
1678 isl_union_map_free(may_source
);
1679 isl_union_map_free(must_source
);
1680 isl_union_map_free(sink
);
1681 isl_union_map_free(schedule
);
1683 obj
.type
= isl_obj_none
;
1688 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1690 struct isl_obj obj
= { isl_obj_none
, NULL
};
1691 struct isl_list
*list
= NULL
;
1692 isl_union_map
*must_source
= NULL
;
1693 isl_union_map
*may_source
= NULL
;
1694 isl_union_map
*sink
= NULL
;
1695 isl_union_map
*schedule
= NULL
;
1696 isl_union_map
*must_dep
;
1697 isl_union_map
*must_no_source
;
1699 must_source
= read_map(s
, table
);
1703 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1704 may_source
= read_map(s
, table
);
1707 return last_any(s
, table
, must_source
, may_source
);
1710 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1714 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1717 sink
= read_map(s
, table
);
1721 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1724 schedule
= read_map(s
, table
);
1728 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1729 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1730 schedule
, &must_dep
, NULL
,
1731 &must_no_source
, NULL
) < 0) {
1732 isl_list_free(list
);
1736 list
->obj
[0].type
= isl_obj_union_map
;
1737 list
->obj
[0].v
= must_dep
;
1738 list
->obj
[1].type
= isl_obj_union_map
;
1739 list
->obj
[1].v
= must_no_source
;
1742 obj
.type
= isl_obj_list
;
1746 isl_list_free(list
);
1747 isl_union_map_free(may_source
);
1748 isl_union_map_free(must_source
);
1749 isl_union_map_free(sink
);
1750 isl_union_map_free(schedule
);
1752 obj
.type
= isl_obj_none
;
1757 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1758 struct isl_hash_table
*table
)
1760 isl_union_set
*domain
;
1761 isl_union_map
*validity
;
1762 isl_union_map
*proximity
;
1764 domain
= read_set(s
, table
);
1768 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1769 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1772 isl_union_map
*umap
;
1773 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1774 umap
= read_map(s
, table
);
1775 validity
= isl_union_map_union(validity
, umap
);
1776 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1777 umap
= read_map(s
, table
);
1778 proximity
= isl_union_map_union(proximity
, umap
);
1783 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1786 static struct isl_obj
schedule(struct isl_stream
*s
,
1787 struct isl_hash_table
*table
)
1789 struct isl_obj obj
= { isl_obj_none
, NULL
};
1790 isl_schedule
*schedule
;
1792 schedule
= get_schedule(s
, table
);
1794 obj
.v
= isl_schedule_get_map(schedule
);
1795 obj
.type
= isl_obj_union_map
;
1797 isl_schedule_free(schedule
);
1802 /* Read a schedule for code generation.
1803 * If the input is a set rather than a map, then we construct
1804 * an identity schedule on the given set.
1806 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1807 struct isl_hash_table
*table
)
1812 obj
= read_obj(s
, table
);
1813 ctx
= isl_stream_get_ctx(s
);
1815 if (is_subtype(obj
, isl_obj_union_map
)) {
1816 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1820 if (is_subtype(obj
, isl_obj_union_set
)) {
1821 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1822 return isl_union_set_identity(obj
.v
);
1826 isl_die(ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1829 /* Generate an AST for the given schedule and options and print
1830 * the AST on the printer.
1832 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1833 __isl_take isl_union_map
*schedule
,
1834 __isl_take isl_union_map
*options
)
1838 isl_ast_build
*build
;
1842 space
= isl_union_map_get_space(schedule
);
1843 context
= isl_set_universe(isl_space_params(space
));
1845 build
= isl_ast_build_from_context(context
);
1846 build
= isl_ast_build_set_options(build
, options
);
1847 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1848 isl_ast_build_free(build
);
1853 format
= isl_printer_get_output_format(p
);
1854 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1855 p
= isl_printer_print_ast_node(p
, tree
);
1856 p
= isl_printer_set_output_format(p
, format
);
1858 isl_ast_node_free(tree
);
1863 /* Perform the codegen operation.
1864 * In particular, read a schedule, check if the user has specified any options
1865 * and then generate an AST from the schedule (and options) and print it.
1867 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1868 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1870 isl_union_map
*schedule
;
1871 isl_union_map
*options
;
1873 schedule
= get_codegen_schedule(s
, table
);
1877 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1878 options
= read_map(s
, table
);
1880 options
= isl_union_map_empty(
1881 isl_union_map_get_space(schedule
));
1883 p
= print_code(p
, schedule
, options
);
1885 isl_stream_eat(s
, ';');
1890 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1892 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1894 struct isl_obj obj
= { isl_obj_none
, NULL
};
1895 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1896 struct isl_list
*list
;
1898 list
= isl_list_alloc(ctx
, 2);
1903 obj
.type
= isl_obj_list
;
1905 list
->obj
[0].type
= isl_obj_union_map
;
1906 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1908 if (isl_band_has_children(band
)) {
1909 isl_band_list
*children
;
1911 children
= isl_band_get_children(band
);
1912 list
->obj
[1] = band_list_to_obj_list(children
);
1914 list
->obj
[1].type
= isl_obj_list
;
1915 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1918 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1921 isl_band_free(band
);
1925 isl_band_free(band
);
1927 obj
.type
= isl_obj_none
;
1932 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1934 struct isl_obj obj
= { isl_obj_none
, NULL
};
1935 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1936 struct isl_list
*list
;
1939 n
= isl_band_list_n_band(bands
);
1940 list
= isl_list_alloc(ctx
, n
);
1945 obj
.type
= isl_obj_list
;
1947 for (i
= 0; i
< n
; ++i
) {
1950 band
= isl_band_list_get_band(bands
, i
);
1951 list
->obj
[i
] = band_to_obj_list(band
);
1952 if (!list
->obj
[i
].v
)
1956 isl_band_list_free(bands
);
1960 isl_band_list_free(bands
);
1962 obj
.type
= isl_obj_none
;
1967 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1968 struct isl_hash_table
*table
)
1970 struct isl_obj obj
= { isl_obj_none
, NULL
};
1971 isl_schedule
*schedule
;
1972 isl_band_list
*roots
;
1974 schedule
= get_schedule(s
, table
);
1978 roots
= isl_schedule_get_band_forest(schedule
);
1979 isl_schedule_free(schedule
);
1981 return band_list_to_obj_list(roots
);
1984 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
1986 struct isl_token
*tok
;
1990 ctx
= isl_stream_get_ctx(s
);
1991 if (isl_stream_eat_if_available(s
, '+'))
1992 return transitive_closure(ctx
, obj
);
1994 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
1995 if (obj
.type
!= isl_obj_union_map
)
1996 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1998 tok
= isl_stream_next_token(s
);
1999 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
2000 isl_stream_error(s
, tok
, "expecting integer exponent");
2002 isl_stream_push_token(s
, tok
);
2006 v
= isl_token_get_val(ctx
, tok
);
2007 if (isl_val_is_zero(v
)) {
2008 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2011 isl_stream_push_token(s
, tok
);
2015 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2016 isl_token_free(tok
);
2023 obj
.type
= isl_obj_none
;
2028 static struct isl_obj
check_assert(struct isl_stream
*s
,
2029 struct isl_hash_table
*table
)
2034 obj
= read_expr(s
, table
);
2035 ctx
= isl_stream_get_ctx(s
);
2036 if (obj
.type
!= isl_obj_bool
)
2037 isl_die(ctx
, isl_error_invalid
,
2038 "expecting boolean expression", goto error
);
2039 if (obj
.v
!= &iscc_bool_true
)
2040 isl_die(ctx
, isl_error_unknown
,
2041 "assertion failed", abort());
2044 obj
.type
= isl_obj_none
;
2049 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2053 struct isl_token
*tok
;
2054 struct isl_stream
*s_file
;
2055 struct iscc_options
*options
;
2059 tok
= isl_stream_next_token(s
);
2060 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2061 isl_stream_error(s
, tok
, "expecting filename");
2062 isl_token_free(tok
);
2066 ctx
= isl_stream_get_ctx(s
);
2067 options
= isl_ctx_peek_iscc_options(ctx
);
2068 if (!options
|| !options
->io
) {
2069 isl_token_free(tok
);
2070 isl_die(ctx
, isl_error_invalid
,
2071 "read operation not allowed", goto error
);
2074 name
= isl_token_get_str(ctx
, tok
);
2075 isl_token_free(tok
);
2076 file
= fopen(name
, "r");
2078 isl_assert(ctx
, file
, goto error
);
2080 s_file
= isl_stream_new_file(ctx
, file
);
2086 obj
= isl_stream_read_obj(s_file
);
2088 isl_stream_free(s_file
);
2093 obj
.type
= isl_obj_none
;
2098 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2099 struct isl_hash_table
*table
)
2102 struct isl_token
*tok
;
2103 struct isl_stream
*s_file
;
2104 struct iscc_options
*options
;
2110 tok
= isl_stream_next_token(s
);
2111 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2112 isl_stream_error(s
, tok
, "expecting filename");
2113 isl_token_free(tok
);
2117 obj
= read_expr(s
, table
);
2119 ctx
= isl_stream_get_ctx(s
);
2120 options
= isl_ctx_peek_iscc_options(ctx
);
2121 if (!options
|| !options
->io
) {
2122 isl_token_free(tok
);
2123 isl_die(ctx
, isl_error_invalid
,
2124 "write operation not allowed", goto error
);
2127 name
= isl_token_get_str(ctx
, tok
);
2128 isl_token_free(tok
);
2129 file
= fopen(name
, "w");
2132 isl_die(ctx
, isl_error_unknown
,
2133 "could not open file for writing", goto error
);
2135 p
= isl_printer_to_file(ctx
, file
);
2136 p
= isl_printer_set_output_format(p
, options
->format
);
2137 p
= obj
.type
->print(p
, obj
.v
);
2138 p
= isl_printer_end_line(p
);
2139 isl_printer_free(p
);
2144 obj
.type
= isl_obj_none
;
2149 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2151 struct isl_token
*tok
;
2152 struct isl_obj obj
= { isl_obj_none
, NULL
};
2154 tok
= isl_stream_next_token(s
);
2157 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2159 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2162 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2163 isl_token_free(tok
);
2165 obj
.type
= isl_obj_str
;
2167 isl_stream_push_token(s
, tok
);
2170 isl_token_free(tok
);
2174 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2176 struct isl_token
*tok
;
2177 struct isl_obj obj
= { isl_obj_none
, NULL
};
2180 tok
= isl_stream_next_token(s
);
2183 type
= isl_token_get_type(tok
);
2184 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2185 int is_true
= type
== ISL_TOKEN_TRUE
;
2186 isl_token_free(tok
);
2187 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2188 obj
.type
= isl_obj_bool
;
2190 isl_stream_push_token(s
, tok
);
2194 static __isl_give
char *read_ident(struct isl_stream
*s
)
2198 struct isl_token
*tok
, *tok2
;
2200 name
= isl_stream_read_ident_if_available(s
);
2204 tok
= isl_stream_next_token(s
);
2207 if (isl_token_get_type(tok
) != '$') {
2208 isl_stream_push_token(s
, tok
);
2211 tok2
= isl_stream_next_token(s
);
2212 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2214 isl_stream_push_token(s
, tok2
);
2215 isl_stream_push_token(s
, tok
);
2219 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2220 name
= isl_val_to_str(v
);
2222 isl_token_free(tok
);
2223 isl_token_free(tok2
);
2228 static struct isl_obj
read_list(struct isl_stream
*s
,
2229 struct isl_hash_table
*table
, struct isl_obj obj
)
2231 struct isl_list
*list
;
2233 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2237 list
->obj
[1] = read_obj(s
, table
);
2239 obj
.type
= isl_obj_list
;
2241 if (!list
->obj
[1].v
)
2244 while (isl_stream_eat_if_available(s
, ',')) {
2245 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2253 obj
.type
= isl_obj_none
;
2258 static struct isl_obj
read_obj(struct isl_stream
*s
,
2259 struct isl_hash_table
*table
)
2262 struct isl_obj obj
= { isl_obj_none
, NULL
};
2264 struct isc_un_op
*op
= NULL
;
2266 obj
= read_string_if_available(s
);
2269 obj
= read_bool_if_available(s
);
2272 ctx
= isl_stream_get_ctx(s
);
2273 if (isl_stream_eat_if_available(s
, '(')) {
2274 if (isl_stream_next_token_is(s
, ')')) {
2275 obj
.type
= isl_obj_list
;
2276 obj
.v
= isl_list_alloc(ctx
, 0);
2278 obj
= read_expr(s
, table
);
2279 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2280 obj
= read_list(s
, table
, obj
);
2282 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2285 op
= read_prefix_un_op_if_available(s
);
2287 return read_un_op_expr(s
, table
, op
);
2289 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2290 return check_assert(s
, table
);
2291 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2292 return read_from_file(s
);
2293 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2294 return write_to_file(s
, table
);
2295 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2296 return vertices(s
, table
);
2297 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2298 return any(s
, table
);
2299 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2300 return last(s
, table
);
2301 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2302 return schedule(s
, table
);
2303 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2304 return schedule_forest(s
, table
);
2305 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2306 return type_of(s
, table
);
2308 name
= read_ident(s
);
2310 obj
= stored_obj(ctx
, table
, name
);
2312 obj
= isl_stream_read_obj(s
);
2317 if (isl_stream_eat_if_available(s
, '^'))
2318 obj
= power(s
, obj
);
2319 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2320 obj
= obj_at_index(s
, obj
);
2321 else if (is_subtype(obj
, isl_obj_union_map
) &&
2322 isl_stream_eat_if_available(s
, '(')) {
2323 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2324 obj
= apply(s
, obj
.v
, table
);
2325 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2326 isl_stream_eat_if_available(s
, '(')) {
2327 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2328 obj
= apply_fun(s
, obj
, table
);
2329 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2330 isl_stream_eat_if_available(s
, '(')) {
2331 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2332 obj
= apply_fun(s
, obj
, table
);
2338 obj
.type
= isl_obj_none
;
2343 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2344 struct isl_obj lhs
, struct isl_obj rhs
)
2348 for (i
= 0; ; ++i
) {
2351 if (bin_ops
[i
].op
!= like
->op
)
2353 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2355 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2361 for (i
= 0; ; ++i
) {
2362 if (!named_bin_ops
[i
].name
)
2364 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2366 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2368 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2371 return &named_bin_ops
[i
].op
;
2377 static int next_is_neg_int(struct isl_stream
*s
)
2379 struct isl_token
*tok
;
2382 tok
= isl_stream_next_token(s
);
2383 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2385 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2386 ret
= isl_val_is_neg(v
);
2390 isl_stream_push_token(s
, tok
);
2395 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2396 struct isl_obj lhs
, struct isl_obj rhs
)
2400 lhs
= convert(ctx
, lhs
, op
->lhs
);
2401 rhs
= convert(ctx
, rhs
, op
->rhs
);
2402 if (op
->res
!= isl_obj_bool
)
2403 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2405 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2408 obj
.v
= iscc_bool_from_int(res
);
2415 static struct isl_obj
read_expr(struct isl_stream
*s
,
2416 struct isl_hash_table
*table
)
2419 struct isl_obj obj
= { isl_obj_none
, NULL
};
2420 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2422 obj
= read_obj(s
, table
);
2423 ctx
= isl_stream_get_ctx(s
);
2425 struct isc_bin_op
*op
= NULL
;
2427 op
= read_bin_op_if_available(s
, obj
);
2431 right_obj
= read_obj(s
, table
);
2433 op
= find_matching_bin_op(op
, obj
, right_obj
);
2436 isl_die(ctx
, isl_error_invalid
,
2437 "no such binary operator defined on given operands",
2440 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2443 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2444 right_obj
= read_obj(s
, table
);
2445 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2450 free_obj(right_obj
);
2452 obj
.type
= isl_obj_none
;
2457 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2458 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2460 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2461 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2464 struct isl_obj obj
= { isl_obj_none
, NULL
};
2468 struct isc_bin_op
*op
= NULL
;
2473 if (isl_stream_is_empty(s
))
2476 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2477 return source_file(s
, table
, p
);
2478 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2479 return codegen(s
, table
, p
);
2481 assign
= is_assign(s
);
2483 lhs
= isl_stream_read_ident_if_available(s
);
2484 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2486 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2491 obj
= read_expr(s
, table
);
2492 ctx
= isl_stream_get_ctx(s
);
2493 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2494 fprintf(stderr
, "Interrupted\n");
2495 isl_ctx_reset_error(ctx
);
2497 if (isl_stream_eat(s
, ';'))
2501 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2502 p
= obj
.type
->print(p
, obj
.v
);
2503 p
= isl_printer_end_line(p
);
2508 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2509 static int count
= 0;
2510 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2511 lhs
= strdup(buf
+ 1);
2513 p
= isl_printer_print_str(p
, buf
);
2514 p
= isl_printer_print_str(p
, " := ");
2515 p
= obj
.type
->print(p
, obj
.v
);
2516 p
= isl_printer_end_line(p
);
2518 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2523 isl_stream_flush_tokens(s
);
2524 isl_stream_skip_line(s
);
2530 static isl_stat
free_cb(void **entry
, void *user
)
2532 struct isl_named_obj
*named
= *entry
;
2534 free_obj(named
->obj
);
2541 static void register_named_ops(struct isl_stream
*s
)
2545 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2546 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2547 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2550 for (i
= 0; ; ++i
) {
2551 if (!named_un_ops
[i
].name
)
2553 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2554 named_un_ops
[i
].name
);
2555 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2558 for (i
= 0; ; ++i
) {
2559 if (!named_bin_ops
[i
].name
)
2561 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2562 named_bin_ops
[i
].name
);
2563 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2567 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2568 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2571 struct isl_token
*tok
;
2572 struct isl_stream
*s_file
;
2573 struct iscc_options
*options
;
2577 tok
= isl_stream_next_token(s
);
2578 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2579 isl_stream_error(s
, tok
, "expecting filename");
2580 isl_token_free(tok
);
2584 isl_stream_eat(s
, ';');
2586 ctx
= isl_stream_get_ctx(s
);
2587 options
= isl_ctx_peek_iscc_options(ctx
);
2588 if (!options
|| !options
->io
) {
2589 isl_token_free(tok
);
2590 isl_die(ctx
, isl_error_invalid
,
2591 "source operation not allowed", return p
);
2594 name
= isl_token_get_str(ctx
, tok
);
2595 isl_token_free(tok
);
2596 file
= fopen(name
, "r");
2598 isl_assert(ctx
, file
, return p
);
2600 s_file
= isl_stream_new_file(ctx
, file
);
2606 register_named_ops(s_file
);
2608 while (!isl_stream_is_empty(s_file
))
2609 p
= read_line(s_file
, table
, p
, 0);
2611 isl_stream_free(s_file
);
2617 int main(int argc
, char **argv
)
2619 struct isl_ctx
*ctx
;
2620 struct isl_stream
*s
;
2621 struct isl_hash_table
*table
;
2622 struct iscc_options
*options
;
2624 int tty
= isatty(0);
2626 options
= iscc_options_new_with_defaults();
2629 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2630 pet_options_set_autodetect(ctx
, 1);
2631 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2632 s
= isl_stream_new_file(ctx
, stdin
);
2634 table
= isl_hash_table_alloc(ctx
, 10);
2636 p
= isl_printer_to_file(ctx
, stdout
);
2637 p
= isl_printer_set_output_format(p
, options
->format
);
2640 register_named_ops(s
);
2642 install_signal_handler(ctx
);
2644 while (p
&& !isl_stream_is_empty(s
)) {
2645 isl_ctx_resume(ctx
);
2646 p
= read_line(s
, table
, p
, tty
);
2649 remove_signal_handler(ctx
);
2651 isl_printer_free(p
);
2652 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2653 isl_hash_table_free(ctx
, table
);