8 #include <isl/stream.h>
11 #include <isl/vertices.h>
14 #include <isl/schedule.h>
15 #include <isl/ast_build.h>
16 #include <isl_obj_list.h>
17 #include <isl_obj_str.h>
18 #include <barvinok/isl.h>
19 #include <barvinok/options.h>
20 #include "lattice_width.h"
27 static isl_ctx
*main_ctx
;
29 static void handler(int signum
)
31 if (isl_ctx_aborted(main_ctx
))
33 isl_ctx_abort(main_ctx
);
36 static struct sigaction sa_old
;
38 static void install_signal_handler(isl_ctx
*ctx
)
44 memset(&sa
, 0, sizeof(struct sigaction
));
45 sa
.sa_handler
= &handler
;
46 sa
.sa_flags
= SA_RESTART
;
47 sigaction(SIGINT
, &sa
, &sa_old
);
50 static void remove_signal_handler(isl_ctx
*ctx
)
52 sigaction(SIGINT
, &sa_old
, NULL
);
57 static void install_signal_handler(isl_ctx
*ctx
)
61 static void remove_signal_handler(isl_ctx
*ctx
)
71 int pet_options_set_autodetect(isl_ctx
*ctx
, int val
)
77 static int iscc_bool_false
= 0;
78 static int iscc_bool_true
= 1;
79 static int iscc_bool_error
= -1;
81 enum iscc_op
{ ISCC_READ
, ISCC_WRITE
, ISCC_SOURCE
, ISCC_VERTICES
,
82 ISCC_LAST
, ISCC_ANY
, ISCC_BEFORE
, ISCC_UNDER
,
83 ISCC_SCHEDULE
, ISCC_SCHEDULE_FOREST
,
84 ISCC_MINIMIZING
, ISCC_RESPECTING
,
85 ISCC_CODEGEN
, ISCC_USING
,
86 ISCC_TYPEOF
, ISCC_PRINT
, ISCC_ASSERT
,
88 static const char *op_name
[ISCC_N_OP
] = {
89 [ISCC_ASSERT
] = "assert",
91 [ISCC_WRITE
] = "write",
92 [ISCC_PRINT
] = "print",
93 [ISCC_SOURCE
] = "source",
94 [ISCC_VERTICES
] = "vertices",
97 [ISCC_BEFORE
] = "before",
98 [ISCC_UNDER
] = "under",
99 [ISCC_SCHEDULE
] = "schedule",
100 [ISCC_SCHEDULE_FOREST
] = "schedule_forest",
101 [ISCC_MINIMIZING
] = "minimizing",
102 [ISCC_RESPECTING
] = "respecting",
103 [ISCC_CODEGEN
] = "codegen",
104 [ISCC_USING
] = "using",
105 [ISCC_TYPEOF
] = "typeof"
107 static enum isl_token_type iscc_op
[ISCC_N_OP
];
109 struct isl_arg_choice iscc_format
[] = {
110 {"isl", ISL_FORMAT_ISL
},
111 {"omega", ISL_FORMAT_OMEGA
},
112 {"polylib", ISL_FORMAT_POLYLIB
},
113 {"ext-polylib", ISL_FORMAT_EXT_POLYLIB
},
114 {"latex", ISL_FORMAT_LATEX
},
119 struct iscc_options
{
120 struct barvinok_options
*barvinok
;
121 struct pet_options
*pet
;
126 ISL_ARGS_START(struct iscc_options
, iscc_options_args
)
127 ISL_ARG_CHILD(struct iscc_options
, barvinok
, "barvinok", &barvinok_options_args
,
130 ISL_ARG_CHILD(struct iscc_options
, pet
, "pet", &pet_options_args
, "pet options")
132 ISL_ARG_CHOICE(struct iscc_options
, format
, 0, "format", \
133 iscc_format
, ISL_FORMAT_ISL
, "output format")
134 ISL_ARG_BOOL(struct iscc_options
, io
, 0, "io", 1,
135 "allow read and write operations")
138 ISL_ARG_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
139 ISL_ARG_CTX_DEF(iscc_options
, struct iscc_options
, iscc_options_args
)
141 static void *isl_obj_bool_copy(void *v
)
146 static void isl_obj_bool_free(void *v
)
150 static __isl_give isl_printer
*isl_obj_bool_print(__isl_take isl_printer
*p
,
153 if (v
== &iscc_bool_true
)
154 return isl_printer_print_str(p
, "True");
155 else if (v
== &iscc_bool_false
)
156 return isl_printer_print_str(p
, "False");
158 return isl_printer_print_str(p
, "Error");
161 static void *isl_obj_bool_add(void *v1
, void *v2
)
166 struct isl_obj_vtable isl_obj_bool_vtable
= {
172 #define isl_obj_bool (&isl_obj_bool_vtable)
174 int *iscc_bool_from_int(int res
)
176 return res
< 0 ? &iscc_bool_error
:
177 res
? &iscc_bool_true
: &iscc_bool_false
;
180 static int isl_union_map_is_superset(__isl_take isl_union_map
*map1
,
181 __isl_take isl_union_map
*map2
)
183 return isl_union_map_is_subset(map2
, map1
);
185 static int isl_union_set_is_superset(__isl_take isl_union_set
*set1
,
186 __isl_take isl_union_set
*set2
)
188 return isl_union_set_is_subset(set2
, set1
);
191 static int isl_union_map_is_strict_superset(__isl_take isl_union_map
*map1
,
192 __isl_take isl_union_map
*map2
)
194 return isl_union_map_is_strict_subset(map2
, map1
);
196 static int isl_union_set_is_strict_superset(__isl_take isl_union_set
*set1
,
197 __isl_take isl_union_set
*set2
)
199 return isl_union_set_is_strict_subset(set2
, set1
);
202 extern struct isl_obj_vtable isl_obj_list_vtable
;
203 #define isl_obj_list (&isl_obj_list_vtable)
205 typedef void *(*isc_bin_op_fn
)(void *lhs
, void *rhs
);
206 typedef int (*isc_bin_test_fn
)(void *lhs
, void *rhs
);
208 enum isl_token_type op
;
214 isc_bin_test_fn test
;
217 struct isc_named_bin_op
{
219 struct isc_bin_op op
;
223 isl_union_pw_qpolynomial
*upwqp
;
224 isl_union_pw_qpolynomial
*res
;
227 static isl_stat
eval_at(__isl_take isl_point
*pnt
, void *user
)
229 struct iscc_at
*at
= (struct iscc_at
*) user
;
234 set
= isl_set_from_point(isl_point_copy(pnt
));
235 v
= isl_union_pw_qpolynomial_eval(
236 isl_union_pw_qpolynomial_copy(at
->upwqp
), pnt
);
237 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
239 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
240 isl_union_pw_qpolynomial_from_pw_qpolynomial(
241 isl_pw_qpolynomial_alloc(set
, qp
)));
246 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_at(
247 __isl_take isl_union_pw_qpolynomial
*upwqp
,
248 __isl_take isl_union_set
*uset
)
253 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
255 isl_union_set_foreach_point(uset
, eval_at
, &at
);
257 isl_union_pw_qpolynomial_free(upwqp
);
258 isl_union_set_free(uset
);
263 struct iscc_fold_at
{
264 isl_union_pw_qpolynomial_fold
*upwf
;
265 isl_union_pw_qpolynomial
*res
;
268 static isl_stat
eval_fold_at(__isl_take isl_point
*pnt
, void *user
)
270 struct iscc_fold_at
*at
= (struct iscc_fold_at
*) user
;
275 set
= isl_set_from_point(isl_point_copy(pnt
));
276 v
= isl_union_pw_qpolynomial_fold_eval(
277 isl_union_pw_qpolynomial_fold_copy(at
->upwf
), pnt
);
278 qp
= isl_qpolynomial_val_on_domain(isl_set_get_space(set
), v
);
280 at
->res
= isl_union_pw_qpolynomial_add(at
->res
,
281 isl_union_pw_qpolynomial_from_pw_qpolynomial(
282 isl_pw_qpolynomial_alloc(set
, qp
)));
287 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_fold_at(
288 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
289 __isl_take isl_union_set
*uset
)
291 struct iscc_fold_at at
;
294 at
.res
= isl_union_pw_qpolynomial_zero(isl_union_set_get_space(uset
));
296 isl_union_set_foreach_point(uset
, eval_fold_at
, &at
);
298 isl_union_pw_qpolynomial_fold_free(upwf
);
299 isl_union_set_free(uset
);
304 static __isl_give isl_union_pw_qpolynomial_fold
*union_pw_qpolynomial_add_union_pw_qpolynomial_fold(
305 __isl_take isl_union_pw_qpolynomial
*upwqp
,
306 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
308 return isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(upwf
,
312 static __isl_give
struct isl_list
*union_map_apply_union_pw_qpolynomial_fold(
313 __isl_take isl_union_map
*umap
,
314 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
317 struct isl_list
*list
;
320 ctx
= isl_union_map_get_ctx(umap
);
321 list
= isl_list_alloc(ctx
, 2);
325 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
326 list
->obj
[0].v
= isl_union_map_apply_union_pw_qpolynomial_fold(umap
,
328 list
->obj
[1].type
= isl_obj_bool
;
329 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
330 if (tight
< 0 || !list
->obj
[0].v
)
335 isl_union_map_free(umap
);
336 isl_union_pw_qpolynomial_fold_free(upwf
);
342 static __isl_give
struct isl_list
*union_set_apply_union_pw_qpolynomial_fold(
343 __isl_take isl_union_set
*uset
,
344 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
347 struct isl_list
*list
;
350 ctx
= isl_union_set_get_ctx(uset
);
351 list
= isl_list_alloc(ctx
, 2);
355 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
356 list
->obj
[0].v
= isl_union_set_apply_union_pw_qpolynomial_fold(uset
,
358 list
->obj
[1].type
= isl_obj_bool
;
359 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
360 if (tight
< 0 || !list
->obj
[0].v
)
365 isl_union_set_free(uset
);
366 isl_union_pw_qpolynomial_fold_free(upwf
);
372 static __isl_give isl_union_pw_qpolynomial
*isl_val_mul_union_pw_qpolynomial(
373 __isl_take isl_val
*v
, __isl_take isl_union_pw_qpolynomial
*upwqp
)
375 return isl_union_pw_qpolynomial_scale_val(upwqp
, v
);
378 static __isl_give isl_union_pw_qpolynomial_fold
*
379 int_val_mul_union_pw_qpolynomial_fold(__isl_take isl_val
*v
,
380 __isl_take isl_union_pw_qpolynomial_fold
*upwf
)
382 return isl_union_pw_qpolynomial_fold_scale_val(upwf
, v
);
385 /* Are the two strings "str1" and "str2" equal to each other?
387 static int str_eq(__isl_keep isl_str
*str1
, __isl_keep isl_str
*str2
)
392 return !strcmp(str1
->s
, str2
->s
);
395 struct isc_bin_op bin_ops
[] = {
396 { '+', isl_obj_val
, isl_obj_val
, isl_obj_val
,
397 (isc_bin_op_fn
) &isl_val_add
},
398 { '-', isl_obj_val
, isl_obj_val
, isl_obj_val
,
399 (isc_bin_op_fn
) &isl_val_sub
},
400 { '*', isl_obj_val
, isl_obj_val
, isl_obj_val
,
401 (isc_bin_op_fn
) &isl_val_mul
},
402 { '+', isl_obj_pw_multi_aff
, isl_obj_pw_multi_aff
,
403 isl_obj_pw_multi_aff
,
404 (isc_bin_op_fn
) &isl_pw_multi_aff_add
},
405 { '+', isl_obj_union_set
, isl_obj_union_set
,
407 (isc_bin_op_fn
) &isl_union_set_union
},
408 { '+', isl_obj_union_map
, isl_obj_union_map
,
410 (isc_bin_op_fn
) &isl_union_map_union
},
411 { '-', isl_obj_union_set
, isl_obj_union_set
,
413 (isc_bin_op_fn
) &isl_union_set_subtract
},
414 { '-', isl_obj_union_map
, isl_obj_union_map
,
416 (isc_bin_op_fn
) &isl_union_map_subtract
},
417 { '*', isl_obj_union_set
, isl_obj_union_set
,
419 (isc_bin_op_fn
) &isl_union_set_intersect
},
420 { '*', isl_obj_union_map
, isl_obj_union_map
,
422 (isc_bin_op_fn
) &isl_union_map_intersect
},
423 { '*', isl_obj_union_map
, isl_obj_union_set
,
425 (isc_bin_op_fn
) &isl_union_map_intersect_domain
},
426 { '.', isl_obj_union_map
, isl_obj_union_map
,
428 (isc_bin_op_fn
) &isl_union_map_apply_range
},
429 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
430 isl_obj_union_pw_qpolynomial
,
431 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
},
432 { '.', isl_obj_union_map
, isl_obj_union_pw_qpolynomial_fold
,
434 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
},
435 { ISL_TOKEN_TO
, isl_obj_union_set
, isl_obj_union_set
,
437 (isc_bin_op_fn
) &isl_union_map_from_domain_and_range
},
438 { '=', isl_obj_union_set
, isl_obj_union_set
, isl_obj_bool
,
439 { .test
= (isc_bin_test_fn
) &isl_union_set_is_equal
} },
440 { '=', isl_obj_union_map
, isl_obj_union_map
, isl_obj_bool
,
441 { .test
= (isc_bin_test_fn
) &isl_union_map_is_equal
} },
442 { ISL_TOKEN_LE
, isl_obj_union_set
, isl_obj_union_set
,
444 { .test
= (isc_bin_test_fn
) &isl_union_set_is_subset
} },
445 { ISL_TOKEN_LE
, isl_obj_union_map
, isl_obj_union_map
,
447 { .test
= (isc_bin_test_fn
) &isl_union_map_is_subset
} },
448 { ISL_TOKEN_LT
, isl_obj_union_set
, isl_obj_union_set
,
450 { .test
= (isc_bin_test_fn
) &isl_union_set_is_strict_subset
} },
451 { ISL_TOKEN_LT
, isl_obj_union_map
, isl_obj_union_map
,
453 { .test
= (isc_bin_test_fn
) &isl_union_map_is_strict_subset
} },
454 { ISL_TOKEN_GE
, isl_obj_union_set
, isl_obj_union_set
,
456 { .test
= (isc_bin_test_fn
) &isl_union_set_is_superset
} },
457 { ISL_TOKEN_GE
, isl_obj_union_map
, isl_obj_union_map
,
459 { .test
= (isc_bin_test_fn
) &isl_union_map_is_superset
} },
460 { ISL_TOKEN_GT
, isl_obj_union_set
, isl_obj_union_set
,
463 (isc_bin_test_fn
) &isl_union_set_is_strict_superset
} },
464 { ISL_TOKEN_GT
, isl_obj_union_map
, isl_obj_union_map
,
467 (isc_bin_test_fn
) &isl_union_map_is_strict_superset
} },
468 { ISL_TOKEN_LEX_LE
, isl_obj_union_set
, isl_obj_union_set
,
470 (isc_bin_op_fn
) &isl_union_set_lex_le_union_set
},
471 { ISL_TOKEN_LEX_LT
, isl_obj_union_set
, isl_obj_union_set
,
473 (isc_bin_op_fn
) &isl_union_set_lex_lt_union_set
},
474 { ISL_TOKEN_LEX_GE
, isl_obj_union_set
, isl_obj_union_set
,
476 (isc_bin_op_fn
) &isl_union_set_lex_ge_union_set
},
477 { ISL_TOKEN_LEX_GT
, isl_obj_union_set
, isl_obj_union_set
,
479 (isc_bin_op_fn
) &isl_union_set_lex_gt_union_set
},
480 { ISL_TOKEN_LEX_LE
, isl_obj_union_map
, isl_obj_union_map
,
482 (isc_bin_op_fn
) &isl_union_map_lex_le_union_map
},
483 { ISL_TOKEN_LEX_LT
, isl_obj_union_map
, isl_obj_union_map
,
485 (isc_bin_op_fn
) &isl_union_map_lex_lt_union_map
},
486 { ISL_TOKEN_LEX_GE
, isl_obj_union_map
, isl_obj_union_map
,
488 (isc_bin_op_fn
) &isl_union_map_lex_ge_union_map
},
489 { ISL_TOKEN_LEX_GT
, isl_obj_union_map
, isl_obj_union_map
,
491 (isc_bin_op_fn
) &isl_union_map_lex_gt_union_map
},
492 { '.', isl_obj_union_pw_qpolynomial_fold
,
493 isl_obj_union_pw_qpolynomial_fold
,
494 isl_obj_union_pw_qpolynomial_fold
,
495 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_fold
},
496 { '+', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
497 isl_obj_union_pw_qpolynomial
,
498 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_add
},
499 { '+', isl_obj_union_pw_qpolynomial
,
500 isl_obj_union_pw_qpolynomial_fold
,
501 isl_obj_union_pw_qpolynomial_fold
,
502 (isc_bin_op_fn
) &union_pw_qpolynomial_add_union_pw_qpolynomial_fold
},
503 { '+', isl_obj_union_pw_qpolynomial_fold
,
504 isl_obj_union_pw_qpolynomial
,
505 isl_obj_union_pw_qpolynomial_fold
,
506 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial
},
507 { '-', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
508 isl_obj_union_pw_qpolynomial
,
509 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_sub
},
510 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial
,
511 isl_obj_union_pw_qpolynomial
,
512 (isc_bin_op_fn
) &isl_val_mul_union_pw_qpolynomial
},
513 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_val
,
514 isl_obj_union_pw_qpolynomial
,
515 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_scale_val
},
516 { '*', isl_obj_val
, isl_obj_union_pw_qpolynomial_fold
,
517 isl_obj_union_pw_qpolynomial_fold
,
518 (isc_bin_op_fn
) &int_val_mul_union_pw_qpolynomial_fold
},
519 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_val
,
520 isl_obj_union_pw_qpolynomial_fold
,
521 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_scale_val
},
522 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
523 isl_obj_union_pw_qpolynomial
,
524 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_mul
},
525 { '*', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
526 isl_obj_union_pw_qpolynomial
,
527 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_intersect_domain
},
528 { '*', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
529 isl_obj_union_pw_qpolynomial_fold
,
530 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_intersect_domain
},
531 { '@', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
532 isl_obj_union_pw_qpolynomial
,
533 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_at
},
534 { '@', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
535 isl_obj_union_pw_qpolynomial
,
536 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_at
},
537 { '%', isl_obj_union_set
, isl_obj_union_set
,
539 (isc_bin_op_fn
) &isl_union_set_gist
},
540 { '%', isl_obj_union_map
, isl_obj_union_map
,
542 (isc_bin_op_fn
) &isl_union_map_gist
},
543 { '%', isl_obj_union_map
, isl_obj_union_set
,
545 (isc_bin_op_fn
) &isl_union_map_gist_domain
},
546 { '%', isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
547 isl_obj_union_pw_qpolynomial
,
548 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_gist
},
549 { '%', isl_obj_union_pw_qpolynomial_fold
, isl_obj_union_set
,
550 isl_obj_union_pw_qpolynomial_fold
,
551 (isc_bin_op_fn
) &isl_union_pw_qpolynomial_fold_gist
},
552 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial
,
553 isl_obj_union_pw_qpolynomial
, isl_obj_bool
,
554 { .test
= (isc_bin_test_fn
)
555 &isl_union_pw_qpolynomial_plain_is_equal
} },
556 { ISL_TOKEN_EQ_EQ
, isl_obj_union_pw_qpolynomial_fold
,
557 isl_obj_union_pw_qpolynomial_fold
, isl_obj_bool
,
558 { .test
= (isc_bin_test_fn
)
559 &isl_union_pw_qpolynomial_fold_plain_is_equal
} },
560 { '+', isl_obj_str
, isl_obj_str
, isl_obj_str
,
561 (isc_bin_op_fn
) &isl_str_concat
},
562 { '=', isl_obj_str
, isl_obj_str
, isl_obj_bool
,
563 { .test
= (isc_bin_test_fn
) &str_eq
} },
567 static __isl_give isl_union_map
*map_after_map(__isl_take isl_union_map
*umap1
,
568 __isl_take isl_union_map
*umap2
)
570 return isl_union_map_apply_range(umap2
, umap1
);
573 static __isl_give isl_union_pw_qpolynomial
*qpolynomial_after_map(
574 __isl_take isl_union_pw_qpolynomial
*upwqp
,
575 __isl_take isl_union_map
*umap
)
577 return isl_union_map_apply_union_pw_qpolynomial(umap
, upwqp
);
580 static __isl_give
struct isl_list
*qpolynomial_fold_after_map(
581 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
582 __isl_take isl_union_map
*umap
)
584 return union_map_apply_union_pw_qpolynomial_fold(umap
, upwf
);
587 struct isc_named_bin_op named_bin_ops
[] = {
588 { "after", { -1, isl_obj_union_map
, isl_obj_union_map
,
590 (isc_bin_op_fn
) &map_after_map
} },
591 { "after", { -1, isl_obj_union_pw_qpolynomial
,
592 isl_obj_union_map
, isl_obj_union_pw_qpolynomial
,
593 (isc_bin_op_fn
) &qpolynomial_after_map
} },
594 { "after", { -1, isl_obj_union_pw_qpolynomial_fold
,
595 isl_obj_union_map
, isl_obj_list
,
596 (isc_bin_op_fn
) &qpolynomial_fold_after_map
} },
597 { "before", { -1, isl_obj_union_map
, isl_obj_union_map
,
599 (isc_bin_op_fn
) &isl_union_map_apply_range
} },
600 { "before", { -1, isl_obj_union_map
,
601 isl_obj_union_pw_qpolynomial
, isl_obj_union_pw_qpolynomial
,
602 (isc_bin_op_fn
) &isl_union_map_apply_union_pw_qpolynomial
} },
603 { "before", { -1, isl_obj_union_map
,
604 isl_obj_union_pw_qpolynomial_fold
, isl_obj_list
,
605 (isc_bin_op_fn
) &union_map_apply_union_pw_qpolynomial_fold
} },
606 { "cross", { -1, isl_obj_union_set
, isl_obj_union_set
,
608 (isc_bin_op_fn
) &isl_union_set_product
} },
609 { "cross", { -1, isl_obj_union_map
, isl_obj_union_map
,
611 (isc_bin_op_fn
) &isl_union_map_product
} },
615 __isl_give isl_set
*union_set_sample(__isl_take isl_union_set
*uset
)
617 return isl_set_from_basic_set(isl_union_set_sample(uset
));
620 __isl_give isl_map
*union_map_sample(__isl_take isl_union_map
*umap
)
622 return isl_map_from_basic_map(isl_union_map_sample(umap
));
625 static __isl_give
struct isl_list
*union_map_power(
626 __isl_take isl_union_map
*umap
)
629 struct isl_list
*list
;
632 ctx
= isl_union_map_get_ctx(umap
);
633 list
= isl_list_alloc(ctx
, 2);
637 list
->obj
[0].type
= isl_obj_union_map
;
638 list
->obj
[0].v
= isl_union_map_power(umap
, &exact
);
639 list
->obj
[1].type
= isl_obj_bool
;
640 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
641 if (exact
< 0 || !list
->obj
[0].v
)
646 isl_union_map_free(umap
);
652 /* Compute a lower or upper bound on "upwqp" depending on "type" and
653 * return a list containing two elements, the bound and a boolean
654 * indicating whether the result is tight.
656 static __isl_give
struct isl_list
*union_pw_qpolynomial_bound(
657 __isl_take isl_union_pw_qpolynomial
*upwqp
, enum isl_fold type
)
660 struct isl_list
*list
;
663 ctx
= isl_union_pw_qpolynomial_get_ctx(upwqp
);
664 list
= isl_list_alloc(ctx
, 2);
668 list
->obj
[0].type
= isl_obj_union_pw_qpolynomial_fold
;
669 list
->obj
[0].v
= isl_union_pw_qpolynomial_bound(upwqp
, type
, &tight
);
670 list
->obj
[1].type
= isl_obj_bool
;
671 list
->obj
[1].v
= tight
? &iscc_bool_true
: &iscc_bool_false
;
672 if (tight
< 0 || !list
->obj
[0].v
)
677 isl_union_pw_qpolynomial_free(upwqp
);
683 /* Compute a lower bound on "upwqp" and return a list containing
684 * two elements, the bound and a booleanindicating whether
685 * the result is tight.
687 static __isl_give
struct isl_list
*union_pw_qpolynomial_lower_bound(
688 __isl_take isl_union_pw_qpolynomial
*upwqp
)
690 return union_pw_qpolynomial_bound(upwqp
, isl_fold_min
);
693 /* Compute a upper bound on "upwqp" and return a list containing
694 * two elements, the bound and a booleanindicating whether
695 * the result is tight.
697 static __isl_give
struct isl_list
*union_pw_qpolynomial_upper_bound(
698 __isl_take isl_union_pw_qpolynomial
*upwqp
)
700 return union_pw_qpolynomial_bound(upwqp
, isl_fold_max
);
704 static __isl_give isl_list
*parse(__isl_take isl_str
*str
)
707 struct isl_list
*list
;
708 struct pet_scop
*scop
;
709 isl_union_map
*sched
, *may_reads
, *must_writes
, *may_writes
;
710 isl_union_set
*domain
;
711 struct iscc_options
*options
;
717 options
= isl_ctx_peek_iscc_options(ctx
);
718 if (!options
|| !options
->io
) {
720 isl_die(ctx
, isl_error_invalid
,
721 "parse_file operation not allowed", return NULL
);
724 list
= isl_list_alloc(ctx
, 5);
728 scop
= pet_scop_extract_from_C_source(ctx
, str
->s
, NULL
);
729 domain
= pet_scop_collect_domains(scop
);
730 sched
= scop
? isl_schedule_get_map(scop
->schedule
) : NULL
;
731 may_reads
= pet_scop_collect_may_reads(scop
);
732 may_writes
= pet_scop_collect_may_writes(scop
);
733 must_writes
= pet_scop_collect_must_writes(scop
);
736 list
->obj
[0].type
= isl_obj_union_set
;
737 list
->obj
[0].v
= domain
;
738 list
->obj
[1].type
= isl_obj_union_map
;
739 list
->obj
[1].v
= must_writes
;
740 list
->obj
[2].type
= isl_obj_union_map
;
741 list
->obj
[2].v
= may_writes
;
742 list
->obj
[3].type
= isl_obj_union_map
;
743 list
->obj
[3].v
= may_reads
;
744 list
->obj
[4].type
= isl_obj_union_map
;
745 list
->obj
[4].v
= sched
;
747 if (!list
->obj
[0].v
|| !list
->obj
[1].v
||
748 !list
->obj
[2].v
|| !list
->obj
[3].v
|| !list
->obj
[4].v
)
760 static isl_stat
add_point(__isl_take isl_point
*pnt
, void *user
)
762 isl_union_set
**scan
= (isl_union_set
**) user
;
764 *scan
= isl_union_set_add_set(*scan
, isl_set_from_point(pnt
));
769 static __isl_give isl_union_set
*union_set_scan(__isl_take isl_union_set
*uset
)
773 scan
= isl_union_set_empty(isl_union_set_get_space(uset
));
775 if (isl_union_set_foreach_point(uset
, add_point
, &scan
) < 0) {
776 isl_union_set_free(scan
);
780 isl_union_set_free(uset
);
784 static __isl_give isl_union_map
*union_map_scan(__isl_take isl_union_map
*umap
)
786 return isl_union_set_unwrap(union_set_scan(isl_union_map_wrap(umap
)));
789 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_poly(
790 __isl_take isl_union_pw_qpolynomial
*upwqp
)
792 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 0);
795 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_lpoly(
796 __isl_take isl_union_pw_qpolynomial
*upwqp
)
798 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, -1);
801 static __isl_give isl_union_pw_qpolynomial
*union_pw_qpolynomial_upoly(
802 __isl_take isl_union_pw_qpolynomial
*upwqp
)
804 return isl_union_pw_qpolynomial_to_polynomial(upwqp
, 1);
807 typedef void *(*isc_un_op_fn
)(void *arg
);
809 enum isl_token_type op
;
814 struct isc_named_un_op
{
818 struct isc_named_un_op named_un_ops
[] = {
819 {"aff", { -1, isl_obj_union_map
, isl_obj_union_map
,
820 (isc_un_op_fn
) &isl_union_map_affine_hull
} },
821 {"aff", { -1, isl_obj_union_set
, isl_obj_union_set
,
822 (isc_un_op_fn
) &isl_union_set_affine_hull
} },
823 {"card", { -1, isl_obj_union_set
,
824 isl_obj_union_pw_qpolynomial
,
825 (isc_un_op_fn
) &isl_union_set_card
} },
826 {"card", { -1, isl_obj_union_map
,
827 isl_obj_union_pw_qpolynomial
,
828 (isc_un_op_fn
) &isl_union_map_card
} },
829 {"coalesce", { -1, isl_obj_union_set
, isl_obj_union_set
,
830 (isc_un_op_fn
) &isl_union_set_coalesce
} },
831 {"coalesce", { -1, isl_obj_union_map
, isl_obj_union_map
,
832 (isc_un_op_fn
) &isl_union_map_coalesce
} },
833 {"coalesce", { -1, isl_obj_union_pw_qpolynomial
,
834 isl_obj_union_pw_qpolynomial
,
835 (isc_un_op_fn
) &isl_union_pw_qpolynomial_coalesce
} },
836 {"coalesce", { -1, isl_obj_union_pw_qpolynomial_fold
,
837 isl_obj_union_pw_qpolynomial_fold
,
838 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_coalesce
} },
839 {"coefficients", { -1, isl_obj_union_set
,
841 (isc_un_op_fn
) &isl_union_set_coefficients
} },
842 {"solutions", { -1, isl_obj_union_set
, isl_obj_union_set
,
843 (isc_un_op_fn
) &isl_union_set_solutions
} },
844 {"deltas", { -1, isl_obj_union_map
, isl_obj_union_set
,
845 (isc_un_op_fn
) &isl_union_map_deltas
} },
846 {"deltas_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
847 (isc_un_op_fn
) &isl_union_map_deltas_map
} },
848 {"dom", { -1, isl_obj_union_map
, isl_obj_union_set
,
849 (isc_un_op_fn
) &isl_union_map_domain
} },
850 {"dom", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_union_set
,
851 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
852 {"dom", { -1, isl_obj_union_pw_qpolynomial_fold
,
854 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
855 {"domain", { -1, isl_obj_union_map
, isl_obj_union_set
,
856 (isc_un_op_fn
) &isl_union_map_domain
} },
857 {"domain", { -1, isl_obj_union_pw_qpolynomial
,
859 (isc_un_op_fn
) &isl_union_pw_qpolynomial_domain
} },
860 {"domain", { -1, isl_obj_union_pw_qpolynomial_fold
,
862 (isc_un_op_fn
) &isl_union_pw_qpolynomial_fold_domain
} },
863 {"domain_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
864 (isc_un_op_fn
) &isl_union_map_domain_map
} },
865 {"ran", { -1, isl_obj_union_map
, isl_obj_union_set
,
866 (isc_un_op_fn
) &isl_union_map_range
} },
867 {"range", { -1, isl_obj_union_map
, isl_obj_union_set
,
868 (isc_un_op_fn
) &isl_union_map_range
} },
869 {"range_map", { -1, isl_obj_union_map
, isl_obj_union_map
,
870 (isc_un_op_fn
) &isl_union_map_range_map
} },
871 {"identity", { -1, isl_obj_union_set
, isl_obj_union_map
,
872 (isc_un_op_fn
) &isl_union_set_identity
} },
873 {"lattice_width", { -1, isl_obj_union_set
,
874 isl_obj_union_pw_qpolynomial
,
875 (isc_un_op_fn
) &isl_union_set_lattice_width
} },
876 {"lb", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
877 (isc_un_op_fn
) &union_pw_qpolynomial_lower_bound
} },
878 {"lexmin", { -1, isl_obj_union_map
, isl_obj_union_map
,
879 (isc_un_op_fn
) &isl_union_map_lexmin
} },
880 {"lexmax", { -1, isl_obj_union_map
, isl_obj_union_map
,
881 (isc_un_op_fn
) &isl_union_map_lexmax
} },
882 {"lexmin", { -1, isl_obj_union_set
, isl_obj_union_set
,
883 (isc_un_op_fn
) &isl_union_set_lexmin
} },
884 {"lexmax", { -1, isl_obj_union_set
, isl_obj_union_set
,
885 (isc_un_op_fn
) &isl_union_set_lexmax
} },
886 {"lift", { -1, isl_obj_union_set
, isl_obj_union_set
,
887 (isc_un_op_fn
) &isl_union_set_lift
} },
888 {"params", { -1, isl_obj_union_map
, isl_obj_set
,
889 (isc_un_op_fn
) &isl_union_map_params
} },
890 {"params", { -1, isl_obj_union_set
, isl_obj_set
,
891 (isc_un_op_fn
) &isl_union_set_params
} },
892 {"poly", { -1, isl_obj_union_map
, isl_obj_union_map
,
893 (isc_un_op_fn
) &isl_union_map_polyhedral_hull
} },
894 {"poly", { -1, isl_obj_union_set
, isl_obj_union_set
,
895 (isc_un_op_fn
) &isl_union_set_polyhedral_hull
} },
896 {"poly", { -1, isl_obj_union_pw_qpolynomial
,
897 isl_obj_union_pw_qpolynomial
,
898 (isc_un_op_fn
) &union_pw_qpolynomial_poly
} },
899 {"lpoly", { -1, isl_obj_union_pw_qpolynomial
,
900 isl_obj_union_pw_qpolynomial
,
901 (isc_un_op_fn
) &union_pw_qpolynomial_lpoly
} },
902 {"upoly", { -1, isl_obj_union_pw_qpolynomial
,
903 isl_obj_union_pw_qpolynomial
,
904 (isc_un_op_fn
) &union_pw_qpolynomial_upoly
} },
906 {"parse_file", { -1, isl_obj_str
, isl_obj_list
,
907 (isc_un_op_fn
) &parse
} },
909 {"pow", { -1, isl_obj_union_map
, isl_obj_list
,
910 (isc_un_op_fn
) &union_map_power
} },
911 {"sample", { -1, isl_obj_union_set
, isl_obj_set
,
912 (isc_un_op_fn
) &union_set_sample
} },
913 {"sample", { -1, isl_obj_union_map
, isl_obj_map
,
914 (isc_un_op_fn
) &union_map_sample
} },
915 {"scan", { -1, isl_obj_union_set
, isl_obj_union_set
,
916 (isc_un_op_fn
) &union_set_scan
} },
917 {"scan", { -1, isl_obj_union_map
, isl_obj_union_map
,
918 (isc_un_op_fn
) &union_map_scan
} },
919 {"sum", { -1, isl_obj_union_pw_qpolynomial
,
920 isl_obj_union_pw_qpolynomial
,
921 (isc_un_op_fn
) &isl_union_pw_qpolynomial_sum
} },
922 {"ub", { -1, isl_obj_union_pw_qpolynomial
, isl_obj_list
,
923 (isc_un_op_fn
) &union_pw_qpolynomial_upper_bound
} },
924 {"unwrap", { -1, isl_obj_union_set
, isl_obj_union_map
,
925 (isc_un_op_fn
) &isl_union_set_unwrap
} },
926 {"wrap", { -1, isl_obj_union_map
, isl_obj_union_set
,
927 (isc_un_op_fn
) &isl_union_map_wrap
} },
928 {"zip", { -1, isl_obj_union_map
, isl_obj_union_map
,
929 (isc_un_op_fn
) &isl_union_map_zip
} },
933 struct isl_named_obj
{
938 static void free_obj(struct isl_obj obj
)
940 obj
.type
->free(obj
.v
);
943 static int same_name(const void *entry
, const void *val
)
945 const struct isl_named_obj
*named
= (const struct isl_named_obj
*)entry
;
947 return !strcmp(named
->name
, val
);
950 static int do_assign(struct isl_ctx
*ctx
, struct isl_hash_table
*table
,
951 char *name
, struct isl_obj obj
)
953 struct isl_hash_table_entry
*entry
;
955 struct isl_named_obj
*named
;
957 name_hash
= isl_hash_string(isl_hash_init(), name
);
958 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 1);
963 free_obj(named
->obj
);
966 named
= isl_alloc_type(ctx
, struct isl_named_obj
);
981 static struct isl_obj
stored_obj(struct isl_ctx
*ctx
,
982 struct isl_hash_table
*table
, char *name
)
984 struct isl_obj obj
= { isl_obj_none
, NULL
};
985 struct isl_hash_table_entry
*entry
;
988 name_hash
= isl_hash_string(isl_hash_init(), name
);
989 entry
= isl_hash_table_find(ctx
, table
, name_hash
, same_name
, name
, 0);
991 struct isl_named_obj
*named
;
994 } else if (isdigit(name
[0]))
995 fprintf(stderr
, "unknown identifier '$%s'\n", name
);
997 fprintf(stderr
, "unknown identifier '%s'\n", name
);
1000 obj
.v
= obj
.type
->copy(obj
.v
);
1004 static int is_subtype(struct isl_obj obj
, isl_obj_type super
)
1006 if (obj
.type
== super
)
1008 if (obj
.type
== isl_obj_map
&& super
== isl_obj_union_map
)
1010 if (obj
.type
== isl_obj_set
&& super
== isl_obj_union_set
)
1012 if (obj
.type
== isl_obj_pw_multi_aff
&& super
== isl_obj_union_set
) {
1013 isl_space
*space
= isl_pw_multi_aff_get_space(obj
.v
);
1014 int is_set
= isl_space_is_set(space
);
1015 isl_space_free(space
);
1018 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1019 super
== isl_obj_union_pw_qpolynomial
)
1021 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1022 super
== isl_obj_union_pw_qpolynomial_fold
)
1024 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
))
1026 if (obj
.type
== isl_obj_list
) {
1027 struct isl_list
*list
= obj
.v
;
1028 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1029 return is_subtype(list
->obj
[0], super
);
1031 if (super
== isl_obj_str
)
1036 static struct isl_obj
obj_at(struct isl_obj obj
, int i
)
1038 struct isl_list
*list
= obj
.v
;
1041 obj
.v
= obj
.type
->copy(obj
.v
);
1043 isl_list_free(list
);
1048 static struct isl_obj
convert(isl_ctx
*ctx
, struct isl_obj obj
,
1051 if (obj
.type
== type
)
1053 if (obj
.type
== isl_obj_pw_multi_aff
&& type
== isl_obj_union_set
) {
1054 isl_set
*set
= isl_set_from_pw_multi_aff(obj
.v
);
1055 obj
.type
= isl_obj_union_set
;
1056 obj
.v
= isl_union_set_from_set(set
);
1059 if (obj
.type
== isl_obj_map
&& type
== isl_obj_union_map
) {
1060 obj
.type
= isl_obj_union_map
;
1061 obj
.v
= isl_union_map_from_map(obj
.v
);
1064 if (obj
.type
== isl_obj_set
&& type
== isl_obj_union_set
) {
1065 obj
.type
= isl_obj_union_set
;
1066 obj
.v
= isl_union_set_from_set(obj
.v
);
1069 if (obj
.type
== isl_obj_pw_qpolynomial
&&
1070 type
== isl_obj_union_pw_qpolynomial
) {
1071 obj
.type
= isl_obj_union_pw_qpolynomial
;
1072 obj
.v
= isl_union_pw_qpolynomial_from_pw_qpolynomial(obj
.v
);
1075 if (obj
.type
== isl_obj_pw_qpolynomial_fold
&&
1076 type
== isl_obj_union_pw_qpolynomial_fold
) {
1077 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1078 obj
.v
= isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj
.v
);
1081 if (obj
.type
== isl_obj_union_set
&& isl_union_set_is_empty(obj
.v
)) {
1082 if (type
== isl_obj_union_map
) {
1083 obj
.type
= isl_obj_union_map
;
1086 if (type
== isl_obj_union_pw_qpolynomial
) {
1087 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1088 isl_union_set_free(obj
.v
);
1089 obj
.v
= isl_union_pw_qpolynomial_zero(dim
);
1090 obj
.type
= isl_obj_union_pw_qpolynomial
;
1093 if (type
== isl_obj_union_pw_qpolynomial_fold
) {
1094 isl_space
*dim
= isl_union_set_get_space(obj
.v
);
1095 isl_union_set_free(obj
.v
);
1096 obj
.v
= isl_union_pw_qpolynomial_fold_zero(dim
,
1098 obj
.type
= isl_obj_union_pw_qpolynomial_fold
;
1102 if (obj
.type
== isl_obj_list
) {
1103 struct isl_list
*list
= obj
.v
;
1104 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1105 return convert(ctx
, obj_at(obj
, 0), type
);
1107 if (type
== isl_obj_str
) {
1112 p
= isl_printer_to_str(ctx
);
1115 p
= obj
.type
->print(p
, obj
.v
);
1116 s
= isl_printer_get_str(p
);
1117 isl_printer_free(p
);
1119 str
= isl_str_from_string(ctx
, s
);
1124 obj
.type
= isl_obj_str
;
1130 obj
.type
= isl_obj_none
;
1135 static struct isc_bin_op
*read_bin_op_if_available(struct isl_stream
*s
,
1139 struct isl_token
*tok
;
1141 tok
= isl_stream_next_token(s
);
1145 for (i
= 0; ; ++i
) {
1148 if (bin_ops
[i
].op
!= isl_token_get_type(tok
))
1150 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
1153 isl_token_free(tok
);
1157 for (i
= 0; ; ++i
) {
1158 if (!named_bin_ops
[i
].name
)
1160 if (named_bin_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1162 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
1165 isl_token_free(tok
);
1166 return &named_bin_ops
[i
].op
;
1169 isl_stream_push_token(s
, tok
);
1174 static struct isc_un_op
*read_prefix_un_op_if_available(struct isl_stream
*s
)
1177 struct isl_token
*tok
;
1179 tok
= isl_stream_next_token(s
);
1183 for (i
= 0; ; ++i
) {
1184 if (!named_un_ops
[i
].name
)
1186 if (named_un_ops
[i
].op
.op
!= isl_token_get_type(tok
))
1189 isl_token_free(tok
);
1190 return &named_un_ops
[i
].op
;
1193 isl_stream_push_token(s
, tok
);
1198 static struct isc_un_op
*find_matching_un_op(struct isc_un_op
*like
,
1203 for (i
= 0; ; ++i
) {
1204 if (!named_un_ops
[i
].name
)
1206 if (named_un_ops
[i
].op
.op
!= like
->op
)
1208 if (!is_subtype(arg
, named_un_ops
[i
].op
.arg
))
1211 return &named_un_ops
[i
].op
;
1217 static int is_assign(struct isl_stream
*s
)
1219 struct isl_token
*tok
;
1220 struct isl_token
*tok2
;
1223 tok
= isl_stream_next_token(s
);
1226 if (isl_token_get_type(tok
) != ISL_TOKEN_IDENT
) {
1227 isl_stream_push_token(s
, tok
);
1231 tok2
= isl_stream_next_token(s
);
1233 isl_stream_push_token(s
, tok
);
1236 assign
= isl_token_get_type(tok2
) == ISL_TOKEN_DEF
;
1237 isl_stream_push_token(s
, tok2
);
1238 isl_stream_push_token(s
, tok
);
1243 static struct isl_obj
read_obj(struct isl_stream
*s
,
1244 struct isl_hash_table
*table
);
1245 static struct isl_obj
read_expr(struct isl_stream
*s
,
1246 struct isl_hash_table
*table
);
1248 static struct isl_obj
read_un_op_expr(struct isl_stream
*s
,
1249 struct isl_hash_table
*table
, struct isc_un_op
*op
)
1252 struct isl_obj obj
= { isl_obj_none
, NULL
};
1254 obj
= read_obj(s
, table
);
1258 op
= find_matching_un_op(op
, obj
);
1260 ctx
= isl_stream_get_ctx(s
);
1262 isl_die(ctx
, isl_error_invalid
,
1263 "no such unary operator defined on given operand",
1266 obj
= convert(ctx
, obj
, op
->arg
);
1267 obj
.v
= op
->fn(obj
.v
);
1273 obj
.type
= isl_obj_none
;
1278 static struct isl_obj
transitive_closure(struct isl_ctx
*ctx
, struct isl_obj obj
)
1280 struct isl_list
*list
;
1283 if (obj
.type
!= isl_obj_union_map
)
1284 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1285 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1286 list
= isl_list_alloc(ctx
, 2);
1290 list
->obj
[0].type
= isl_obj_union_map
;
1291 list
->obj
[0].v
= isl_union_map_transitive_closure(obj
.v
, &exact
);
1292 list
->obj
[1].type
= isl_obj_bool
;
1293 list
->obj
[1].v
= exact
? &iscc_bool_true
: &iscc_bool_false
;
1295 obj
.type
= isl_obj_list
;
1296 if (exact
< 0 || !list
->obj
[0].v
)
1302 obj
.type
= isl_obj_none
;
1307 static struct isl_obj
obj_at_index(struct isl_stream
*s
, struct isl_obj obj
)
1309 struct isl_list
*list
= obj
.v
;
1310 struct isl_token
*tok
;
1315 tok
= isl_stream_next_token(s
);
1316 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
1317 isl_stream_error(s
, tok
, "expecting index");
1319 isl_stream_push_token(s
, tok
);
1322 ctx
= isl_stream_get_ctx(s
);
1323 v
= isl_token_get_val(ctx
, tok
);
1324 i
= isl_val_get_num_si(v
);
1326 isl_token_free(tok
);
1327 isl_assert(ctx
, i
< list
->n
, goto error
);
1328 if (isl_stream_eat(s
, ']'))
1331 return obj_at(obj
, i
);
1334 obj
.type
= isl_obj_none
;
1339 static struct isl_obj
apply(struct isl_stream
*s
, __isl_take isl_union_map
*umap
,
1340 struct isl_hash_table
*table
)
1345 obj
= read_expr(s
, table
);
1346 ctx
= isl_stream_get_ctx(s
);
1347 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_set
) ||
1348 is_subtype(obj
, isl_obj_union_map
), goto error
);
1350 if (obj
.type
== isl_obj_list
) {
1351 struct isl_list
*list
= obj
.v
;
1352 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1353 obj
= obj_at(obj
, 0);
1355 if (obj
.type
== isl_obj_set
)
1356 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1357 else if (obj
.type
== isl_obj_map
)
1358 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1359 if (obj
.type
== isl_obj_union_set
) {
1360 obj
.v
= isl_union_set_apply(obj
.v
, umap
);
1362 obj
.v
= isl_union_map_apply_range(obj
.v
, umap
);
1366 if (isl_stream_eat(s
, ')'))
1371 isl_union_map_free(umap
);
1374 obj
.type
= isl_obj_none
;
1379 static struct isl_obj
apply_fun_set(struct isl_obj obj
,
1380 __isl_take isl_union_set
*uset
)
1382 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1383 obj
.v
= isl_union_set_apply_union_pw_qpolynomial(uset
, obj
.v
);
1385 obj
.type
= isl_obj_list
;
1386 obj
.v
= union_set_apply_union_pw_qpolynomial_fold(uset
, obj
.v
);
1391 static struct isl_obj
apply_fun_map(struct isl_obj obj
,
1392 __isl_take isl_union_map
*umap
)
1394 if (obj
.type
== isl_obj_union_pw_qpolynomial
) {
1395 obj
.v
= isl_union_map_apply_union_pw_qpolynomial(umap
, obj
.v
);
1397 obj
.type
= isl_obj_list
;
1398 obj
.v
= union_map_apply_union_pw_qpolynomial_fold(umap
, obj
.v
);
1403 static struct isl_obj
apply_fun(struct isl_stream
*s
,
1404 struct isl_obj obj
, struct isl_hash_table
*table
)
1409 arg
= read_expr(s
, table
);
1410 ctx
= isl_stream_get_ctx(s
);
1411 if (!is_subtype(arg
, isl_obj_union_map
) &&
1412 !is_subtype(arg
, isl_obj_union_set
))
1413 isl_die(ctx
, isl_error_invalid
,
1414 "expecting set of map argument", goto error
);
1416 if (arg
.type
== isl_obj_list
) {
1417 struct isl_list
*list
= arg
.v
;
1418 if (list
->n
== 2 && list
->obj
[1].type
== isl_obj_bool
)
1419 arg
= obj_at(arg
, 0);
1421 if (arg
.type
== isl_obj_set
)
1422 arg
= convert(ctx
, arg
, isl_obj_union_set
);
1423 else if (arg
.type
== isl_obj_map
)
1424 arg
= convert(ctx
, arg
, isl_obj_union_map
);
1425 if (arg
.type
== isl_obj_union_set
)
1426 obj
= apply_fun_set(obj
, arg
.v
);
1428 obj
= apply_fun_map(obj
, arg
.v
);
1432 if (isl_stream_eat(s
, ')'))
1440 obj
.type
= isl_obj_none
;
1445 struct add_vertex_data
{
1446 struct isl_list
*list
;
1450 static isl_stat
add_vertex(__isl_take isl_vertex
*vertex
, void *user
)
1452 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1456 ma
= isl_vertex_get_expr(vertex
);
1457 dom
= isl_set_from_basic_set(isl_vertex_get_domain(vertex
));
1459 data
->list
->obj
[data
->i
].type
= isl_obj_pw_multi_aff
;
1460 data
->list
->obj
[data
->i
].v
= isl_pw_multi_aff_alloc(dom
, ma
);
1463 isl_vertex_free(vertex
);
1468 static isl_stat
set_vertices(__isl_take isl_set
*set
, void *user
)
1471 isl_basic_set
*hull
;
1472 isl_vertices
*vertices
= NULL
;
1473 struct isl_list
*list
= NULL
;
1475 struct add_vertex_data
*data
= (struct add_vertex_data
*)user
;
1477 set
= isl_set_remove_divs(set
);
1478 hull
= isl_set_convex_hull(set
);
1479 vertices
= isl_basic_set_compute_vertices(hull
);
1480 isl_basic_set_free(hull
);
1484 ctx
= isl_vertices_get_ctx(vertices
);
1485 data
->list
= isl_list_alloc(ctx
, isl_vertices_get_n_vertices(vertices
));
1490 r
= isl_vertices_foreach_vertex(vertices
, &add_vertex
, user
);
1492 data
->list
= isl_list_concat(list
, data
->list
);
1494 isl_vertices_free(vertices
);
1499 isl_vertices_free(vertices
);
1500 return isl_stat_error
;
1503 static struct isl_obj
vertices(struct isl_stream
*s
,
1504 struct isl_hash_table
*table
)
1508 struct isl_list
*list
= NULL
;
1509 isl_union_set
*uset
= NULL
;
1510 struct add_vertex_data data
= { NULL
};
1512 obj
= read_expr(s
, table
);
1513 ctx
= isl_stream_get_ctx(s
);
1514 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1515 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1519 list
= isl_list_alloc(ctx
, 0);
1525 if (isl_union_set_foreach_set(uset
, &set_vertices
, &data
) < 0)
1528 isl_union_set_free(uset
);
1530 obj
.type
= isl_obj_list
;
1535 isl_union_set_free(uset
);
1536 isl_list_free(data
.list
);
1538 obj
.type
= isl_obj_none
;
1543 static struct isl_obj
type_of(struct isl_stream
*s
,
1544 struct isl_hash_table
*table
)
1548 const char *type
= "unknown";
1550 obj
= read_expr(s
, table
);
1552 if (obj
.type
== isl_obj_map
||
1553 obj
.type
== isl_obj_union_map
)
1555 if (obj
.type
== isl_obj_set
||
1556 obj
.type
== isl_obj_union_set
)
1558 if (obj
.type
== isl_obj_pw_multi_aff
)
1559 type
= "piecewise multi-quasiaffine expression";
1560 if (obj
.type
== isl_obj_pw_qpolynomial
||
1561 obj
.type
== isl_obj_union_pw_qpolynomial
)
1562 type
= "piecewise quasipolynomial";
1563 if (obj
.type
== isl_obj_pw_qpolynomial_fold
||
1564 obj
.type
== isl_obj_union_pw_qpolynomial_fold
)
1565 type
= "piecewise quasipolynomial fold";
1566 if (obj
.type
== isl_obj_list
)
1568 if (obj
.type
== isl_obj_bool
)
1570 if (obj
.type
== isl_obj_str
)
1572 if (obj
.type
== isl_obj_val
)
1576 obj
.type
= isl_obj_str
;
1577 obj
.v
= isl_str_from_string(isl_stream_get_ctx(s
), strdup(type
));
1582 static __isl_give isl_union_set
*read_set(struct isl_stream
*s
,
1583 struct isl_hash_table
*table
)
1588 obj
= read_obj(s
, table
);
1589 ctx
= isl_stream_get_ctx(s
);
1590 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1591 isl_assert(ctx
, obj
.type
== isl_obj_union_set
, goto error
);
1598 static __isl_give isl_union_map
*read_map(struct isl_stream
*s
,
1599 struct isl_hash_table
*table
)
1604 obj
= read_obj(s
, table
);
1605 ctx
= isl_stream_get_ctx(s
);
1606 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1607 isl_assert(ctx
, obj
.type
== isl_obj_union_map
, goto error
);
1614 static struct isl_obj
last_any(struct isl_stream
*s
,
1615 struct isl_hash_table
*table
, __isl_take isl_union_map
*must_source
,
1616 __isl_take isl_union_map
*may_source
)
1618 struct isl_obj obj
= { isl_obj_none
, NULL
};
1619 isl_union_map
*sink
= NULL
;
1620 isl_union_map
*schedule
= NULL
;
1621 isl_union_map
*may_dep
;
1622 isl_union_map
*must_dep
;
1624 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1627 sink
= read_map(s
, table
);
1631 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1634 schedule
= read_map(s
, table
);
1638 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1639 schedule
, &must_dep
, &may_dep
,
1643 obj
.type
= isl_obj_union_map
;
1644 obj
.v
= isl_union_map_union(must_dep
, may_dep
);
1648 isl_union_map_free(may_source
);
1649 isl_union_map_free(must_source
);
1650 isl_union_map_free(sink
);
1651 isl_union_map_free(schedule
);
1653 obj
.type
= isl_obj_none
;
1658 static struct isl_obj
any(struct isl_stream
*s
, struct isl_hash_table
*table
)
1660 struct isl_obj obj
= { isl_obj_none
, NULL
};
1661 isl_union_map
*must_source
= NULL
;
1662 isl_union_map
*may_source
= NULL
;
1663 isl_union_map
*sink
= NULL
;
1664 isl_union_map
*schedule
= NULL
;
1665 isl_union_map
*may_dep
;
1667 may_source
= read_map(s
, table
);
1671 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
])) {
1672 must_source
= read_map(s
, table
);
1675 return last_any(s
, table
, must_source
, may_source
);
1678 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1681 sink
= read_map(s
, table
);
1685 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1688 schedule
= read_map(s
, table
);
1692 must_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1693 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1694 schedule
, NULL
, &may_dep
,
1698 obj
.type
= isl_obj_union_map
;
1703 isl_union_map_free(may_source
);
1704 isl_union_map_free(must_source
);
1705 isl_union_map_free(sink
);
1706 isl_union_map_free(schedule
);
1708 obj
.type
= isl_obj_none
;
1713 static struct isl_obj
last(struct isl_stream
*s
, struct isl_hash_table
*table
)
1715 struct isl_obj obj
= { isl_obj_none
, NULL
};
1716 struct isl_list
*list
= NULL
;
1717 isl_union_map
*must_source
= NULL
;
1718 isl_union_map
*may_source
= NULL
;
1719 isl_union_map
*sink
= NULL
;
1720 isl_union_map
*schedule
= NULL
;
1721 isl_union_map
*must_dep
;
1722 isl_union_map
*must_no_source
;
1724 must_source
= read_map(s
, table
);
1728 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
])) {
1729 may_source
= read_map(s
, table
);
1732 return last_any(s
, table
, must_source
, may_source
);
1735 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
1739 if (isl_stream_eat(s
, iscc_op
[ISCC_BEFORE
]))
1742 sink
= read_map(s
, table
);
1746 if (isl_stream_eat(s
, iscc_op
[ISCC_UNDER
]))
1749 schedule
= read_map(s
, table
);
1753 may_source
= isl_union_map_empty(isl_union_map_get_space(sink
));
1754 if (isl_union_map_compute_flow(sink
, must_source
, may_source
,
1755 schedule
, &must_dep
, NULL
,
1756 &must_no_source
, NULL
) < 0) {
1757 isl_list_free(list
);
1761 list
->obj
[0].type
= isl_obj_union_map
;
1762 list
->obj
[0].v
= must_dep
;
1763 list
->obj
[1].type
= isl_obj_union_map
;
1764 list
->obj
[1].v
= must_no_source
;
1767 obj
.type
= isl_obj_list
;
1771 isl_list_free(list
);
1772 isl_union_map_free(may_source
);
1773 isl_union_map_free(must_source
);
1774 isl_union_map_free(sink
);
1775 isl_union_map_free(schedule
);
1777 obj
.type
= isl_obj_none
;
1782 static __isl_give isl_schedule
*get_schedule(struct isl_stream
*s
,
1783 struct isl_hash_table
*table
)
1785 isl_union_set
*domain
;
1786 isl_union_map
*validity
;
1787 isl_union_map
*proximity
;
1789 domain
= read_set(s
, table
);
1793 validity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1794 proximity
= isl_union_map_empty(isl_union_set_get_space(domain
));
1797 isl_union_map
*umap
;
1798 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_RESPECTING
])) {
1799 umap
= read_map(s
, table
);
1800 validity
= isl_union_map_union(validity
, umap
);
1801 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_MINIMIZING
])) {
1802 umap
= read_map(s
, table
);
1803 proximity
= isl_union_map_union(proximity
, umap
);
1808 return isl_union_set_compute_schedule(domain
, validity
, proximity
);
1811 static struct isl_obj
schedule(struct isl_stream
*s
,
1812 struct isl_hash_table
*table
)
1814 struct isl_obj obj
= { isl_obj_none
, NULL
};
1815 isl_schedule
*schedule
;
1817 schedule
= get_schedule(s
, table
);
1819 obj
.v
= isl_schedule_get_map(schedule
);
1820 obj
.type
= isl_obj_union_map
;
1822 isl_schedule_free(schedule
);
1827 /* Read a schedule for code generation.
1828 * If the input is a set rather than a map, then we construct
1829 * an identity schedule on the given set.
1831 static __isl_give isl_union_map
*get_codegen_schedule(struct isl_stream
*s
,
1832 struct isl_hash_table
*table
)
1837 obj
= read_obj(s
, table
);
1838 ctx
= isl_stream_get_ctx(s
);
1840 if (is_subtype(obj
, isl_obj_union_map
)) {
1841 obj
= convert(ctx
, obj
, isl_obj_union_map
);
1845 if (is_subtype(obj
, isl_obj_union_set
)) {
1846 obj
= convert(ctx
, obj
, isl_obj_union_set
);
1847 return isl_union_set_identity(obj
.v
);
1851 isl_die(ctx
, isl_error_invalid
, "expecting set or map", return NULL
);
1854 /* Generate an AST for the given schedule and options and print
1855 * the AST on the printer.
1857 static __isl_give isl_printer
*print_code(__isl_take isl_printer
*p
,
1858 __isl_take isl_union_map
*schedule
,
1859 __isl_take isl_union_map
*options
)
1863 isl_ast_build
*build
;
1867 space
= isl_union_map_get_space(schedule
);
1868 context
= isl_set_universe(isl_space_params(space
));
1870 build
= isl_ast_build_from_context(context
);
1871 build
= isl_ast_build_set_options(build
, options
);
1872 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
1873 isl_ast_build_free(build
);
1878 format
= isl_printer_get_output_format(p
);
1879 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
1880 p
= isl_printer_print_ast_node(p
, tree
);
1881 p
= isl_printer_set_output_format(p
, format
);
1883 isl_ast_node_free(tree
);
1888 /* Perform the codegen operation.
1889 * In particular, read a schedule, check if the user has specified any options
1890 * and then generate an AST from the schedule (and options) and print it.
1892 static __isl_give isl_printer
*codegen(struct isl_stream
*s
,
1893 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
1895 isl_union_map
*schedule
;
1896 isl_union_map
*options
;
1898 schedule
= get_codegen_schedule(s
, table
);
1902 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_USING
]))
1903 options
= read_map(s
, table
);
1905 options
= isl_union_map_empty(
1906 isl_union_map_get_space(schedule
));
1908 p
= print_code(p
, schedule
, options
);
1910 isl_stream_eat(s
, ';');
1915 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
);
1917 static struct isl_obj
band_to_obj_list(__isl_take isl_band
*band
)
1919 struct isl_obj obj
= { isl_obj_none
, NULL
};
1920 isl_ctx
*ctx
= isl_band_get_ctx(band
);
1921 struct isl_list
*list
;
1923 list
= isl_list_alloc(ctx
, 2);
1928 obj
.type
= isl_obj_list
;
1930 list
->obj
[0].type
= isl_obj_union_map
;
1931 list
->obj
[0].v
= isl_band_get_partial_schedule(band
);
1933 if (isl_band_has_children(band
)) {
1934 isl_band_list
*children
;
1936 children
= isl_band_get_children(band
);
1937 list
->obj
[1] = band_list_to_obj_list(children
);
1939 list
->obj
[1].type
= isl_obj_list
;
1940 list
->obj
[1].v
= isl_list_alloc(ctx
, 0);
1943 if (!list
->obj
[0].v
|| !list
->obj
[1].v
)
1946 isl_band_free(band
);
1950 isl_band_free(band
);
1952 obj
.type
= isl_obj_none
;
1957 static struct isl_obj
band_list_to_obj_list(__isl_take isl_band_list
*bands
)
1959 struct isl_obj obj
= { isl_obj_none
, NULL
};
1960 isl_ctx
*ctx
= isl_band_list_get_ctx(bands
);
1961 struct isl_list
*list
;
1964 n
= isl_band_list_n_band(bands
);
1965 list
= isl_list_alloc(ctx
, n
);
1970 obj
.type
= isl_obj_list
;
1972 for (i
= 0; i
< n
; ++i
) {
1975 band
= isl_band_list_get_band(bands
, i
);
1976 list
->obj
[i
] = band_to_obj_list(band
);
1977 if (!list
->obj
[i
].v
)
1981 isl_band_list_free(bands
);
1985 isl_band_list_free(bands
);
1987 obj
.type
= isl_obj_none
;
1992 static struct isl_obj
schedule_forest(struct isl_stream
*s
,
1993 struct isl_hash_table
*table
)
1995 struct isl_obj obj
= { isl_obj_none
, NULL
};
1996 isl_schedule
*schedule
;
1997 isl_band_list
*roots
;
1999 schedule
= get_schedule(s
, table
);
2003 roots
= isl_schedule_get_band_forest(schedule
);
2004 isl_schedule_free(schedule
);
2006 return band_list_to_obj_list(roots
);
2009 static struct isl_obj
power(struct isl_stream
*s
, struct isl_obj obj
)
2011 struct isl_token
*tok
;
2015 ctx
= isl_stream_get_ctx(s
);
2016 if (isl_stream_eat_if_available(s
, '+'))
2017 return transitive_closure(ctx
, obj
);
2019 isl_assert(ctx
, is_subtype(obj
, isl_obj_union_map
), goto error
);
2020 if (obj
.type
!= isl_obj_union_map
)
2021 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2023 tok
= isl_stream_next_token(s
);
2024 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_VALUE
) {
2025 isl_stream_error(s
, tok
, "expecting integer exponent");
2027 isl_stream_push_token(s
, tok
);
2031 v
= isl_token_get_val(ctx
, tok
);
2032 if (isl_val_is_zero(v
)) {
2033 isl_stream_error(s
, tok
, "expecting non-zero exponent");
2036 isl_stream_push_token(s
, tok
);
2040 obj
.v
= isl_union_map_fixed_power_val(obj
.v
, v
);
2041 isl_token_free(tok
);
2048 obj
.type
= isl_obj_none
;
2053 static struct isl_obj
check_assert(struct isl_stream
*s
,
2054 struct isl_hash_table
*table
)
2059 obj
= read_expr(s
, table
);
2060 ctx
= isl_stream_get_ctx(s
);
2061 if (obj
.type
!= isl_obj_bool
)
2062 isl_die(ctx
, isl_error_invalid
,
2063 "expecting boolean expression", goto error
);
2064 if (obj
.v
!= &iscc_bool_true
)
2065 isl_die(ctx
, isl_error_unknown
,
2066 "assertion failed", abort());
2069 obj
.type
= isl_obj_none
;
2074 static struct isl_obj
read_from_file(struct isl_stream
*s
)
2078 struct isl_token
*tok
;
2079 struct isl_stream
*s_file
;
2080 struct iscc_options
*options
;
2084 tok
= isl_stream_next_token(s
);
2085 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2086 isl_stream_error(s
, tok
, "expecting filename");
2087 isl_token_free(tok
);
2091 ctx
= isl_stream_get_ctx(s
);
2092 options
= isl_ctx_peek_iscc_options(ctx
);
2093 if (!options
|| !options
->io
) {
2094 isl_token_free(tok
);
2095 isl_die(ctx
, isl_error_invalid
,
2096 "read operation not allowed", goto error
);
2099 name
= isl_token_get_str(ctx
, tok
);
2100 isl_token_free(tok
);
2101 file
= fopen(name
, "r");
2103 isl_assert(ctx
, file
, goto error
);
2105 s_file
= isl_stream_new_file(ctx
, file
);
2111 obj
= isl_stream_read_obj(s_file
);
2113 isl_stream_free(s_file
);
2118 obj
.type
= isl_obj_none
;
2123 static struct isl_obj
write_to_file(struct isl_stream
*s
,
2124 struct isl_hash_table
*table
)
2127 struct isl_token
*tok
;
2128 struct isl_stream
*s_file
;
2129 struct iscc_options
*options
;
2135 tok
= isl_stream_next_token(s
);
2136 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2137 isl_stream_error(s
, tok
, "expecting filename");
2138 isl_token_free(tok
);
2142 obj
= read_expr(s
, table
);
2144 ctx
= isl_stream_get_ctx(s
);
2145 options
= isl_ctx_peek_iscc_options(ctx
);
2146 if (!options
|| !options
->io
) {
2147 isl_token_free(tok
);
2148 isl_die(ctx
, isl_error_invalid
,
2149 "write operation not allowed", goto error
);
2152 name
= isl_token_get_str(ctx
, tok
);
2153 isl_token_free(tok
);
2154 file
= fopen(name
, "w");
2157 isl_die(ctx
, isl_error_unknown
,
2158 "could not open file for writing", goto error
);
2160 p
= isl_printer_to_file(ctx
, file
);
2161 p
= isl_printer_set_output_format(p
, options
->format
);
2162 p
= obj
.type
->print(p
, obj
.v
);
2163 p
= isl_printer_end_line(p
);
2164 isl_printer_free(p
);
2169 obj
.type
= isl_obj_none
;
2174 static struct isl_obj
read_string_if_available(struct isl_stream
*s
)
2176 struct isl_token
*tok
;
2177 struct isl_obj obj
= { isl_obj_none
, NULL
};
2179 tok
= isl_stream_next_token(s
);
2182 if (isl_token_get_type(tok
) == ISL_TOKEN_STRING
) {
2184 str
= isl_str_alloc(isl_stream_get_ctx(s
));
2187 str
->s
= isl_token_get_str(isl_stream_get_ctx(s
), tok
);
2188 isl_token_free(tok
);
2190 obj
.type
= isl_obj_str
;
2192 isl_stream_push_token(s
, tok
);
2195 isl_token_free(tok
);
2199 static struct isl_obj
read_bool_if_available(struct isl_stream
*s
)
2201 struct isl_token
*tok
;
2202 struct isl_obj obj
= { isl_obj_none
, NULL
};
2205 tok
= isl_stream_next_token(s
);
2208 type
= isl_token_get_type(tok
);
2209 if (type
== ISL_TOKEN_FALSE
|| type
== ISL_TOKEN_TRUE
) {
2210 int is_true
= type
== ISL_TOKEN_TRUE
;
2211 isl_token_free(tok
);
2212 obj
.v
= is_true
? &iscc_bool_true
: &iscc_bool_false
;
2213 obj
.type
= isl_obj_bool
;
2215 isl_stream_push_token(s
, tok
);
2219 static __isl_give
char *read_ident(struct isl_stream
*s
)
2223 struct isl_token
*tok
, *tok2
;
2225 name
= isl_stream_read_ident_if_available(s
);
2229 tok
= isl_stream_next_token(s
);
2232 if (isl_token_get_type(tok
) != '$') {
2233 isl_stream_push_token(s
, tok
);
2236 tok2
= isl_stream_next_token(s
);
2237 if (!tok2
|| isl_token_get_type(tok2
) != ISL_TOKEN_VALUE
) {
2239 isl_stream_push_token(s
, tok2
);
2240 isl_stream_push_token(s
, tok
);
2244 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok2
);
2245 name
= isl_val_to_str(v
);
2247 isl_token_free(tok
);
2248 isl_token_free(tok2
);
2253 static struct isl_obj
read_list(struct isl_stream
*s
,
2254 struct isl_hash_table
*table
, struct isl_obj obj
)
2256 struct isl_list
*list
;
2258 list
= isl_list_alloc(isl_stream_get_ctx(s
), 2);
2262 list
->obj
[1] = read_obj(s
, table
);
2264 obj
.type
= isl_obj_list
;
2266 if (!list
->obj
[1].v
)
2269 while (isl_stream_eat_if_available(s
, ',')) {
2270 obj
.v
= list
= isl_list_add_obj(list
, read_obj(s
, table
));
2278 obj
.type
= isl_obj_none
;
2283 static struct isl_obj
read_obj(struct isl_stream
*s
,
2284 struct isl_hash_table
*table
)
2287 struct isl_obj obj
= { isl_obj_none
, NULL
};
2289 struct isc_un_op
*op
= NULL
;
2291 obj
= read_string_if_available(s
);
2294 obj
= read_bool_if_available(s
);
2297 ctx
= isl_stream_get_ctx(s
);
2298 if (isl_stream_eat_if_available(s
, '(')) {
2299 if (isl_stream_next_token_is(s
, ')')) {
2300 obj
.type
= isl_obj_list
;
2301 obj
.v
= isl_list_alloc(ctx
, 0);
2303 obj
= read_expr(s
, table
);
2304 if (obj
.v
&& isl_stream_eat_if_available(s
, ','))
2305 obj
= read_list(s
, table
, obj
);
2307 if (!obj
.v
|| isl_stream_eat(s
, ')'))
2310 op
= read_prefix_un_op_if_available(s
);
2312 return read_un_op_expr(s
, table
, op
);
2314 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ASSERT
]))
2315 return check_assert(s
, table
);
2316 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_READ
]))
2317 return read_from_file(s
);
2318 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_WRITE
]))
2319 return write_to_file(s
, table
);
2320 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_VERTICES
]))
2321 return vertices(s
, table
);
2322 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_ANY
]))
2323 return any(s
, table
);
2324 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_LAST
]))
2325 return last(s
, table
);
2326 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE
]))
2327 return schedule(s
, table
);
2328 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SCHEDULE_FOREST
]))
2329 return schedule_forest(s
, table
);
2330 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_TYPEOF
]))
2331 return type_of(s
, table
);
2333 name
= read_ident(s
);
2335 obj
= stored_obj(ctx
, table
, name
);
2337 obj
= isl_stream_read_obj(s
);
2342 if (isl_stream_eat_if_available(s
, '^'))
2343 obj
= power(s
, obj
);
2344 else if (obj
.type
== isl_obj_list
&& isl_stream_eat_if_available(s
, '['))
2345 obj
= obj_at_index(s
, obj
);
2346 else if (is_subtype(obj
, isl_obj_union_map
) &&
2347 isl_stream_eat_if_available(s
, '(')) {
2348 obj
= convert(ctx
, obj
, isl_obj_union_map
);
2349 obj
= apply(s
, obj
.v
, table
);
2350 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial
) &&
2351 isl_stream_eat_if_available(s
, '(')) {
2352 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial
);
2353 obj
= apply_fun(s
, obj
, table
);
2354 } else if (is_subtype(obj
, isl_obj_union_pw_qpolynomial_fold
) &&
2355 isl_stream_eat_if_available(s
, '(')) {
2356 obj
= convert(ctx
, obj
, isl_obj_union_pw_qpolynomial_fold
);
2357 obj
= apply_fun(s
, obj
, table
);
2363 obj
.type
= isl_obj_none
;
2368 static struct isc_bin_op
*find_matching_bin_op(struct isc_bin_op
*like
,
2369 struct isl_obj lhs
, struct isl_obj rhs
)
2373 for (i
= 0; ; ++i
) {
2376 if (bin_ops
[i
].op
!= like
->op
)
2378 if (!is_subtype(lhs
, bin_ops
[i
].lhs
))
2380 if (!is_subtype(rhs
, bin_ops
[i
].rhs
))
2386 for (i
= 0; ; ++i
) {
2387 if (!named_bin_ops
[i
].name
)
2389 if (named_bin_ops
[i
].op
.op
!= like
->op
)
2391 if (!is_subtype(lhs
, named_bin_ops
[i
].op
.lhs
))
2393 if (!is_subtype(rhs
, named_bin_ops
[i
].op
.rhs
))
2396 return &named_bin_ops
[i
].op
;
2402 static int next_is_neg_int(struct isl_stream
*s
)
2404 struct isl_token
*tok
;
2407 tok
= isl_stream_next_token(s
);
2408 if (tok
&& isl_token_get_type(tok
) == ISL_TOKEN_VALUE
) {
2410 v
= isl_token_get_val(isl_stream_get_ctx(s
), tok
);
2411 ret
= isl_val_is_neg(v
);
2415 isl_stream_push_token(s
, tok
);
2420 static struct isl_obj
call_bin_op(isl_ctx
*ctx
, struct isc_bin_op
*op
,
2421 struct isl_obj lhs
, struct isl_obj rhs
)
2425 lhs
= convert(ctx
, lhs
, op
->lhs
);
2426 rhs
= convert(ctx
, rhs
, op
->rhs
);
2427 if (op
->res
!= isl_obj_bool
)
2428 obj
.v
= op
->o
.fn(lhs
.v
, rhs
.v
);
2430 int res
= op
->o
.test(lhs
.v
, rhs
.v
);
2433 obj
.v
= iscc_bool_from_int(res
);
2440 static struct isl_obj
read_expr(struct isl_stream
*s
,
2441 struct isl_hash_table
*table
)
2444 struct isl_obj obj
= { isl_obj_none
, NULL
};
2445 struct isl_obj right_obj
= { isl_obj_none
, NULL
};
2447 obj
= read_obj(s
, table
);
2448 ctx
= isl_stream_get_ctx(s
);
2450 struct isc_bin_op
*op
= NULL
;
2452 op
= read_bin_op_if_available(s
, obj
);
2456 right_obj
= read_obj(s
, table
);
2458 op
= find_matching_bin_op(op
, obj
, right_obj
);
2461 isl_die(ctx
, isl_error_invalid
,
2462 "no such binary operator defined on given operands",
2465 obj
= call_bin_op(ctx
, op
, obj
, right_obj
);
2468 if (obj
.type
== isl_obj_val
&& next_is_neg_int(s
)) {
2469 right_obj
= read_obj(s
, table
);
2470 obj
.v
= isl_val_add(obj
.v
, right_obj
.v
);
2475 free_obj(right_obj
);
2477 obj
.type
= isl_obj_none
;
2482 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2483 struct isl_hash_table
*table
, __isl_take isl_printer
*p
);
2485 static __isl_give isl_printer
*read_line(struct isl_stream
*s
,
2486 struct isl_hash_table
*table
, __isl_take isl_printer
*p
, int tty
)
2489 struct isl_obj obj
= { isl_obj_none
, NULL
};
2493 struct isc_bin_op
*op
= NULL
;
2498 if (isl_stream_is_empty(s
))
2501 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_SOURCE
]))
2502 return source_file(s
, table
, p
);
2503 if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_CODEGEN
]))
2504 return codegen(s
, table
, p
);
2506 assign
= is_assign(s
);
2508 lhs
= isl_stream_read_ident_if_available(s
);
2509 if (isl_stream_eat(s
, ISL_TOKEN_DEF
))
2511 } else if (isl_stream_eat_if_available(s
, iscc_op
[ISCC_PRINT
]))
2516 obj
= read_expr(s
, table
);
2517 ctx
= isl_stream_get_ctx(s
);
2518 if (isl_ctx_last_error(ctx
) == isl_error_abort
) {
2519 fprintf(stderr
, "Interrupted\n");
2520 isl_ctx_reset_error(ctx
);
2522 if (isl_stream_eat(s
, ';'))
2526 if (obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2527 p
= obj
.type
->print(p
, obj
.v
);
2528 p
= isl_printer_end_line(p
);
2533 if (!assign
&& obj
.type
!= isl_obj_none
&& obj
.v
!= NULL
) {
2534 static int count
= 0;
2535 snprintf(buf
, sizeof(buf
), "$%d", count
++);
2536 lhs
= strdup(buf
+ 1);
2538 p
= isl_printer_print_str(p
, buf
);
2539 p
= isl_printer_print_str(p
, " := ");
2540 p
= obj
.type
->print(p
, obj
.v
);
2541 p
= isl_printer_end_line(p
);
2543 if (lhs
&& do_assign(ctx
, table
, lhs
, obj
))
2548 isl_stream_flush_tokens(s
);
2549 isl_stream_skip_line(s
);
2555 static isl_stat
free_cb(void **entry
, void *user
)
2557 struct isl_named_obj
*named
= *entry
;
2559 free_obj(named
->obj
);
2566 static void register_named_ops(struct isl_stream
*s
)
2570 for (i
= 0; i
< ISCC_N_OP
; ++i
) {
2571 iscc_op
[i
] = isl_stream_register_keyword(s
, op_name
[i
]);
2572 assert(iscc_op
[i
] != ISL_TOKEN_ERROR
);
2575 for (i
= 0; ; ++i
) {
2576 if (!named_un_ops
[i
].name
)
2578 named_un_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2579 named_un_ops
[i
].name
);
2580 assert(named_un_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2583 for (i
= 0; ; ++i
) {
2584 if (!named_bin_ops
[i
].name
)
2586 named_bin_ops
[i
].op
.op
= isl_stream_register_keyword(s
,
2587 named_bin_ops
[i
].name
);
2588 assert(named_bin_ops
[i
].op
.op
!= ISL_TOKEN_ERROR
);
2592 static __isl_give isl_printer
*source_file(struct isl_stream
*s
,
2593 struct isl_hash_table
*table
, __isl_take isl_printer
*p
)
2596 struct isl_token
*tok
;
2597 struct isl_stream
*s_file
;
2598 struct iscc_options
*options
;
2602 tok
= isl_stream_next_token(s
);
2603 if (!tok
|| isl_token_get_type(tok
) != ISL_TOKEN_STRING
) {
2604 isl_stream_error(s
, tok
, "expecting filename");
2605 isl_token_free(tok
);
2609 isl_stream_eat(s
, ';');
2611 ctx
= isl_stream_get_ctx(s
);
2612 options
= isl_ctx_peek_iscc_options(ctx
);
2613 if (!options
|| !options
->io
) {
2614 isl_token_free(tok
);
2615 isl_die(ctx
, isl_error_invalid
,
2616 "source operation not allowed", return p
);
2619 name
= isl_token_get_str(ctx
, tok
);
2620 isl_token_free(tok
);
2621 file
= fopen(name
, "r");
2623 isl_assert(ctx
, file
, return p
);
2625 s_file
= isl_stream_new_file(ctx
, file
);
2631 register_named_ops(s_file
);
2633 while (!isl_stream_is_empty(s_file
))
2634 p
= read_line(s_file
, table
, p
, 0);
2636 isl_stream_free(s_file
);
2642 int main(int argc
, char **argv
)
2644 struct isl_ctx
*ctx
;
2645 struct isl_stream
*s
;
2646 struct isl_hash_table
*table
;
2647 struct iscc_options
*options
;
2649 int tty
= isatty(0);
2651 options
= iscc_options_new_with_defaults();
2654 ctx
= isl_ctx_alloc_with_options(&iscc_options_args
, options
);
2655 pet_options_set_autodetect(ctx
, 1);
2656 argc
= isl_ctx_parse_options(ctx
, argc
, argv
, ISL_ARG_ALL
);
2657 s
= isl_stream_new_file(ctx
, stdin
);
2659 table
= isl_hash_table_alloc(ctx
, 10);
2661 p
= isl_printer_to_file(ctx
, stdout
);
2662 p
= isl_printer_set_output_format(p
, options
->format
);
2665 register_named_ops(s
);
2667 install_signal_handler(ctx
);
2669 while (p
&& !isl_stream_is_empty(s
)) {
2670 isl_ctx_resume(ctx
);
2671 p
= read_line(s
, table
, p
, tty
);
2674 remove_signal_handler(ctx
);
2676 isl_printer_free(p
);
2677 isl_hash_table_foreach(ctx
, table
, free_cb
, NULL
);
2678 isl_hash_table_free(ctx
, table
);