pet_scop_collect_arrays: consider all accessed data spaces of an array
[pet.git] / emit.c
bloba6edd72ad0c3f6052b8dfa911c202f77a990b18e
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <yaml.h>
37 #include <isl/id.h>
38 #include <isl/val.h>
39 #include <isl/aff.h>
40 #include <isl/set.h>
41 #include <isl/map.h>
42 #include <isl/union_set.h>
43 #include <isl/union_map.h>
44 #include <isl/schedule.h>
45 #include <isl/printer.h>
47 #include "expr.h"
48 #include "loc.h"
49 #include "scop.h"
50 #include "scop_yaml.h"
51 #include "tree.h"
53 static int emit_string(yaml_emitter_t *emitter, const char *str)
55 yaml_event_t event;
57 if (!yaml_scalar_event_initialize(&event, NULL, NULL,
58 (yaml_char_t *) str, strlen(str),
59 1, 1, YAML_PLAIN_SCALAR_STYLE))
60 return -1;
61 if (!yaml_emitter_emit(emitter, &event))
62 return -1;
64 return 0;
67 /* Print the string "name" and the string "str" to "emitter".
69 static int emit_named_string(yaml_emitter_t *emitter, const char *name,
70 const char *str)
72 if (emit_string(emitter, name) < 0)
73 return -1;
74 if (emit_string(emitter, str) < 0)
75 return -1;
76 return 0;
79 /* Print the isl_id "id" to "emitter".
81 static int emit_id(yaml_emitter_t *emitter, __isl_keep isl_id *id)
83 return emit_string(emitter, isl_id_get_name(id));
86 /* Print the string "name" and the isl_id "id" to "emitter".
88 static int emit_named_id(yaml_emitter_t *emitter, const char *name,
89 __isl_keep isl_id *id)
91 if (emit_string(emitter, name) < 0)
92 return -1;
93 if (emit_id(emitter, id) < 0)
94 return -1;
95 return 0;
98 static int emit_int(yaml_emitter_t *emitter, int i)
100 char buffer[40];
102 snprintf(buffer, sizeof(buffer), "%d", i);
103 return emit_string(emitter, buffer);
106 static int emit_named_int(yaml_emitter_t *emitter, const char *name, int i)
108 if (emit_string(emitter, name) < 0)
109 return -1;
110 if (emit_int(emitter, i) < 0)
111 return -1;
112 return 0;
115 /* Print the unsigned integer "u" to "emitter".
117 static int emit_unsigned(yaml_emitter_t *emitter, unsigned u)
119 char buffer[40];
121 snprintf(buffer, sizeof(buffer), "%u", u);
122 return emit_string(emitter, buffer);
125 /* Print the string "name" and the unsigned integer "u" to "emitter".
127 static int emit_named_unsigned(yaml_emitter_t *emitter, const char *name,
128 unsigned u)
130 if (emit_string(emitter, name) < 0)
131 return -1;
132 if (emit_unsigned(emitter, u) < 0)
133 return -1;
134 return 0;
137 static int emit_double(yaml_emitter_t *emitter, double d)
139 char buffer[40];
141 snprintf(buffer, sizeof(buffer), "%g", d);
142 return emit_string(emitter, buffer);
145 static int emit_map(yaml_emitter_t *emitter, __isl_keep isl_map *map)
147 isl_ctx *ctx = isl_map_get_ctx(map);
148 isl_printer *p;
149 char *str;
150 int r;
152 p = isl_printer_to_str(ctx);
153 p = isl_printer_print_map(p, map);
154 str = isl_printer_get_str(p);
155 isl_printer_free(p);
156 r = emit_string(emitter, str);
157 free(str);
158 return r;
161 /* Print the isl_val "val" to "emitter".
163 static int emit_val(yaml_emitter_t *emitter, __isl_keep isl_val *val)
165 isl_ctx *ctx = isl_val_get_ctx(val);
166 isl_printer *p;
167 char *str;
168 int r;
170 p = isl_printer_to_str(ctx);
171 p = isl_printer_print_val(p, val);
172 str = isl_printer_get_str(p);
173 isl_printer_free(p);
174 r = emit_string(emitter, str);
175 free(str);
176 return r;
179 /* Print the string "name" and the isl_val "val" to "emitter".
181 static int emit_named_val(yaml_emitter_t *emitter, const char *name,
182 __isl_keep isl_val *val)
184 if (emit_string(emitter, name) < 0)
185 return -1;
186 if (emit_val(emitter, val) < 0)
187 return -1;
188 return 0;
191 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
193 isl_ctx *ctx = isl_set_get_ctx(set);
194 isl_printer *p;
195 char *str;
196 int r;
198 p = isl_printer_to_str(ctx);
199 p = isl_printer_print_set(p, set);
200 str = isl_printer_get_str(p);
201 isl_printer_free(p);
202 r = emit_string(emitter, str);
203 free(str);
204 return r;
207 static int emit_named_set(yaml_emitter_t *emitter, const char *name,
208 __isl_keep isl_set *set)
210 if (emit_string(emitter, name) < 0)
211 return -1;
212 if (emit_set(emitter, set) < 0)
213 return -1;
214 return 0;
217 /* Print the string "name" and the map "map" to "emitter".
219 static int emit_named_map(yaml_emitter_t *emitter, const char *name,
220 __isl_keep isl_map *map)
222 if (emit_string(emitter, name) < 0)
223 return -1;
224 if (emit_map(emitter, map) < 0)
225 return -1;
226 return 0;
229 /* Print the union set "uset" to "emitter".
231 static int emit_union_set(yaml_emitter_t *emitter,
232 __isl_keep isl_union_set *uset)
234 isl_ctx *ctx = isl_union_set_get_ctx(uset);
235 isl_printer *p;
236 char *str;
237 int r;
239 p = isl_printer_to_str(ctx);
240 p = isl_printer_print_union_set(p, uset);
241 str = isl_printer_get_str(p);
242 isl_printer_free(p);
243 r = emit_string(emitter, str);
244 free(str);
245 return r;
248 /* Print the union map "umap" to "emitter".
250 static int emit_union_map(yaml_emitter_t *emitter,
251 __isl_keep isl_union_map *umap)
253 isl_ctx *ctx = isl_union_map_get_ctx(umap);
254 isl_printer *p;
255 char *str;
256 int r;
258 p = isl_printer_to_str(ctx);
259 p = isl_printer_print_union_map(p, umap);
260 str = isl_printer_get_str(p);
261 isl_printer_free(p);
262 r = emit_string(emitter, str);
263 free(str);
264 return r;
267 /* Print the string "name" and the union set "uset" to "emitter".
269 static int emit_named_union_set(yaml_emitter_t *emitter, const char *name,
270 __isl_keep isl_union_set *uset)
272 if (emit_string(emitter, name) < 0)
273 return -1;
274 if (emit_union_set(emitter, uset) < 0)
275 return -1;
276 return 0;
279 /* Print the string "name" and the union map "umap" to "emitter".
281 static int emit_named_union_map(yaml_emitter_t *emitter, const char *name,
282 __isl_keep isl_union_map *umap)
284 if (emit_string(emitter, name) < 0)
285 return -1;
286 if (emit_union_map(emitter, umap) < 0)
287 return -1;
288 return 0;
291 /* Print the isl_multi_pw_aff "mpa" to "emitter".
293 static int emit_multi_pw_aff(yaml_emitter_t *emitter,
294 __isl_keep isl_multi_pw_aff *mpa)
296 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(mpa);
297 isl_printer *p;
298 char *str;
299 int r;
301 p = isl_printer_to_str(ctx);
302 p = isl_printer_print_multi_pw_aff(p, mpa);
303 str = isl_printer_get_str(p);
304 isl_printer_free(p);
305 r = emit_string(emitter, str);
306 free(str);
307 return r;
310 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
312 static int emit_named_multi_pw_aff(yaml_emitter_t *emitter, const char *name,
313 __isl_keep isl_multi_pw_aff *mpa)
315 if (emit_string(emitter, name) < 0)
316 return -1;
317 if (emit_multi_pw_aff(emitter, mpa) < 0)
318 return -1;
319 return 0;
322 /* Print the schedule "schedule" to "emitter".
324 static int emit_schedule(yaml_emitter_t *emitter,
325 __isl_keep isl_schedule *schedule)
327 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
328 isl_printer *p;
329 char *str;
330 int r;
332 p = isl_printer_to_str(ctx);
333 p = isl_printer_print_schedule(p, schedule);
334 str = isl_printer_get_str(p);
335 isl_printer_free(p);
336 r = emit_string(emitter, str);
337 free(str);
338 return r;
341 /* Print the string "name" and the schedule "schedule" to "emitter".
343 static int emit_named_schedule(yaml_emitter_t *emitter, const char *name,
344 __isl_keep isl_schedule *schedule)
346 if (emit_string(emitter, name) < 0)
347 return -1;
348 if (emit_schedule(emitter, schedule) < 0)
349 return -1;
350 return 0;
353 /* Print "type" to "emitter".
355 static int emit_type(yaml_emitter_t *emitter, struct pet_type *type)
357 yaml_event_t event;
359 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
360 YAML_BLOCK_MAPPING_STYLE))
361 return -1;
362 if (!yaml_emitter_emit(emitter, &event))
363 return -1;
365 if (emit_string(emitter, "name") < 0)
366 return -1;
367 if (emit_string(emitter, type->name) < 0)
368 return -1;
370 if (emit_string(emitter, "definition") < 0)
371 return -1;
372 if (emit_string(emitter, type->definition) < 0)
373 return -1;
375 if (!yaml_mapping_end_event_initialize(&event))
376 return -1;
377 if (!yaml_emitter_emit(emitter, &event))
378 return -1;
380 return 0;
383 /* Print the list of "n_type" "types", if any, to "emitter".
385 static int emit_types(yaml_emitter_t *emitter, int n_type,
386 struct pet_type **types)
388 int i;
389 yaml_event_t event;
391 if (n_type == 0)
392 return 0;
394 if (emit_string(emitter, "types") < 0)
395 return -1;
396 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
397 YAML_BLOCK_SEQUENCE_STYLE))
398 return -1;
399 if (!yaml_emitter_emit(emitter, &event))
400 return -1;
402 for (i = 0; i < n_type; ++i)
403 if (emit_type(emitter, types[i]) < 0)
404 return -1;
406 if (!yaml_sequence_end_event_initialize(&event))
407 return -1;
408 if (!yaml_emitter_emit(emitter, &event))
409 return -1;
411 return 0;
414 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
416 yaml_event_t event;
418 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
419 YAML_BLOCK_MAPPING_STYLE))
420 return -1;
421 if (!yaml_emitter_emit(emitter, &event))
422 return -1;
424 if (emit_string(emitter, "context") < 0)
425 return -1;
426 if (emit_set(emitter, array->context) < 0)
427 return -1;
429 if (emit_string(emitter, "extent") < 0)
430 return -1;
431 if (emit_set(emitter, array->extent) < 0)
432 return -1;
434 if (array->value_bounds) {
435 if (emit_string(emitter, "value_bounds") < 0)
436 return -1;
437 if (emit_set(emitter, array->value_bounds) < 0)
438 return -1;
441 if (emit_string(emitter, "element_type") < 0)
442 return -1;
443 if (emit_string(emitter, array->element_type) < 0)
444 return -1;
445 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
446 return -1;
448 if (array->element_is_record)
449 if (emit_named_int(emitter, "element_is_record",
450 array->element_is_record) < 0)
451 return -1;
453 if (array->live_out) {
454 if (emit_string(emitter, "live_out") < 0)
455 return -1;
456 if (emit_string(emitter, "1") < 0)
457 return -1;
460 if (array->uniquely_defined) {
461 if (emit_string(emitter, "uniquely_defined") < 0)
462 return -1;
463 if (emit_string(emitter, "1") < 0)
464 return -1;
467 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
468 return -1;
469 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
470 return -1;
471 if (array->outer && emit_named_int(emitter, "outer", 1) < 0)
472 return -1;
474 if (!yaml_mapping_end_event_initialize(&event))
475 return -1;
476 if (!yaml_emitter_emit(emitter, &event))
477 return -1;
479 return 0;
482 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
483 struct pet_array **arrays)
485 int i;
486 yaml_event_t event;
488 if (emit_string(emitter, "arrays") < 0)
489 return -1;
490 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
491 YAML_BLOCK_SEQUENCE_STYLE))
492 return -1;
493 if (!yaml_emitter_emit(emitter, &event))
494 return -1;
496 for (i = 0; i < n_array; ++i)
497 if (emit_array(emitter, arrays[i]) < 0)
498 return -1;
500 if (!yaml_sequence_end_event_initialize(&event))
501 return -1;
502 if (!yaml_emitter_emit(emitter, &event))
503 return -1;
505 return 0;
508 static int emit_expr_type(yaml_emitter_t *emitter, enum pet_expr_type type)
510 if (emit_string(emitter, pet_type_str(type)) < 0)
511 return -1;
512 return 0;
515 /* Print the fields of the access expression "expr" to "emitter".
517 * Only print the access relations that are present and relevant
518 * for the type of access.
520 * If the depth of the access is equal to the depth that can be derived from
521 * the index expression, then there is no need to print it.
523 static int emit_access_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
525 int depth;
527 if (expr->acc.kill && expr->acc.access[pet_expr_access_fake_killed] &&
528 emit_named_union_map(emitter, "killed",
529 expr->acc.access[pet_expr_access_fake_killed]) < 0)
530 return -1;
531 if (expr->acc.read && expr->acc.access[pet_expr_access_may_read] &&
532 emit_named_union_map(emitter, "may_read",
533 expr->acc.access[pet_expr_access_may_read]) < 0)
534 return -1;
535 if (expr->acc.write && expr->acc.access[pet_expr_access_may_write] &&
536 emit_named_union_map(emitter, "may_write",
537 expr->acc.access[pet_expr_access_may_write]) < 0)
538 return -1;
539 if (expr->acc.write && expr->acc.access[pet_expr_access_must_write] &&
540 emit_named_union_map(emitter, "must_write",
541 expr->acc.access[pet_expr_access_must_write]) < 0)
542 return -1;
543 if (emit_named_multi_pw_aff(emitter, "index", expr->acc.index) < 0)
544 return -1;
545 depth = isl_multi_pw_aff_dim(expr->acc.index, isl_dim_out);
546 if (expr->acc.depth != depth &&
547 emit_named_int(emitter, "depth", expr->acc.depth) < 0)
548 return -1;
549 if (expr->acc.ref_id &&
550 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
551 return -1;
552 if (expr->acc.kill) {
553 if (emit_named_unsigned(emitter, "kill", 1) < 0)
554 return -1;
555 } else {
556 if (emit_string(emitter, "read") < 0)
557 return -1;
558 if (emit_int(emitter, expr->acc.read) < 0)
559 return -1;
560 if (emit_string(emitter, "write") < 0)
561 return -1;
562 if (emit_int(emitter, expr->acc.write) < 0)
563 return -1;
566 return 0;
569 static int emit_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
571 yaml_event_t event;
573 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
574 YAML_BLOCK_MAPPING_STYLE))
575 return -1;
576 if (!yaml_emitter_emit(emitter, &event))
577 return -1;
579 if (emit_string(emitter, "type") < 0)
580 return -1;
581 if (emit_expr_type(emitter, expr->type) < 0)
582 return -1;
584 switch (expr->type) {
585 case pet_expr_error:
586 return -1;
587 case pet_expr_int:
588 if (emit_named_val(emitter, "value", expr->i) < 0)
589 return -1;
590 break;
591 case pet_expr_double:
592 if (emit_string(emitter, "value") < 0)
593 return -1;
594 if (emit_double(emitter, expr->d.val) < 0)
595 return -1;
596 if (emit_string(emitter, "string") < 0)
597 return -1;
598 if (emit_string(emitter, expr->d.s) < 0)
599 return -1;
600 break;
601 case pet_expr_access:
602 if (emit_access_expr(emitter, expr) < 0)
603 return -1;
604 break;
605 case pet_expr_op:
606 if (emit_string(emitter, "operation") < 0)
607 return -1;
608 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
609 return -1;
610 break;
611 case pet_expr_call:
612 if (emit_string(emitter, "name") < 0)
613 return -1;
614 if (emit_string(emitter, expr->c.name) < 0)
615 return -1;
616 break;
617 case pet_expr_cast:
618 if (emit_string(emitter, "type_name") < 0)
619 return -1;
620 if (emit_string(emitter, expr->type_name) < 0)
621 return -1;
622 break;
625 if (expr->n_arg > 0) {
626 int i;
628 if (emit_string(emitter, "arguments") < 0)
629 return -1;
630 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
631 YAML_BLOCK_SEQUENCE_STYLE))
632 return -1;
633 if (!yaml_emitter_emit(emitter, &event))
634 return -1;
636 for (i = 0; i < expr->n_arg; ++i)
637 if (emit_expr(emitter, expr->args[i]) < 0)
638 return -1;
640 if (!yaml_sequence_end_event_initialize(&event))
641 return -1;
642 if (!yaml_emitter_emit(emitter, &event))
643 return -1;
646 if (!yaml_mapping_end_event_initialize(&event))
647 return -1;
648 if (!yaml_emitter_emit(emitter, &event))
649 return -1;
651 return 0;
654 /* Print the string "name" and the expression "expr" to "emitter".
656 static int emit_named_expr(yaml_emitter_t *emitter, const char *name,
657 __isl_keep pet_expr *expr)
659 if (emit_string(emitter, name) < 0)
660 return -1;
661 if (emit_expr(emitter, expr) < 0)
662 return -1;
663 return 0;
666 /* Print "type" to "emitter".
668 static int emit_tree_type(yaml_emitter_t *emitter, enum pet_tree_type type)
670 if (emit_string(emitter, pet_tree_type_str(type)) < 0)
671 return -1;
672 return 0;
675 /* Recursively print "tree" to "emitter".
677 static int emit_tree(yaml_emitter_t *emitter, __isl_keep pet_tree *tree)
679 yaml_event_t event;
680 int i;
682 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
683 YAML_BLOCK_MAPPING_STYLE))
684 return -1;
685 if (!yaml_emitter_emit(emitter, &event))
686 return -1;
688 if (emit_string(emitter, "type") < 0)
689 return -1;
690 if (emit_tree_type(emitter, tree->type) < 0)
691 return -1;
693 switch (tree->type) {
694 case pet_tree_error:
695 return -1;
696 case pet_tree_block:
697 if (emit_named_int(emitter, "block", tree->u.b.block) < 0)
698 return -1;
699 if (tree->u.b.n == 0)
700 break;
702 if (emit_string(emitter, "children") < 0)
703 return -1;
704 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
705 YAML_BLOCK_SEQUENCE_STYLE))
706 return -1;
707 if (!yaml_emitter_emit(emitter, &event))
708 return -1;
710 for (i = 0; i < tree->u.b.n; ++i)
711 if (emit_tree(emitter, tree->u.b.child[i]) < 0)
712 return -1;
714 if (!yaml_sequence_end_event_initialize(&event))
715 return -1;
716 if (!yaml_emitter_emit(emitter, &event))
717 return -1;
718 break;
719 case pet_tree_break:
720 case pet_tree_continue:
721 break;
722 case pet_tree_decl:
723 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
724 return -1;
725 break;
726 case pet_tree_decl_init:
727 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
728 return -1;
729 if (emit_named_expr(emitter,
730 "initialization", tree->u.d.init) < 0)
731 return -1;
732 break;
733 case pet_tree_expr:
734 if (emit_named_expr(emitter, "expr", tree->u.e.expr) < 0)
735 return -1;
736 break;
737 case pet_tree_for:
738 if (tree->u.l.independent)
739 if (emit_named_int(emitter, "independent", 1) < 0)
740 return -1;
741 if (emit_named_int(emitter, "declared", tree->u.l.declared) < 0)
742 return -1;
743 if (emit_named_expr(emitter, "variable", tree->u.l.iv) < 0)
744 return -1;
745 if (emit_named_expr(emitter,
746 "initialization", tree->u.l.init) < 0)
747 return -1;
748 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
749 return -1;
750 if (emit_named_expr(emitter, "increment", tree->u.l.inc) < 0)
751 return -1;
752 if (emit_string(emitter, "body") < 0)
753 return -1;
754 if (emit_tree(emitter, tree->u.l.body) < 0)
755 return -1;
756 break;
757 case pet_tree_while:
758 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
759 return -1;
760 if (emit_string(emitter, "body") < 0)
761 return -1;
762 if (emit_tree(emitter, tree->u.l.body) < 0)
763 return -1;
764 break;
765 case pet_tree_infinite_loop:
766 if (emit_string(emitter, "body") < 0)
767 return -1;
768 if (emit_tree(emitter, tree->u.l.body) < 0)
769 return -1;
770 break;
771 case pet_tree_if:
772 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
773 return -1;
774 if (emit_string(emitter, "then") < 0)
775 return -1;
776 if (emit_tree(emitter, tree->u.i.then_body) < 0)
777 return -1;
778 break;
779 case pet_tree_if_else:
780 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
781 return -1;
782 if (emit_string(emitter, "then") < 0)
783 return -1;
784 if (emit_tree(emitter, tree->u.i.then_body) < 0)
785 return -1;
786 if (emit_string(emitter, "else") < 0)
787 return -1;
788 if (emit_tree(emitter, tree->u.i.else_body) < 0)
789 return -1;
790 break;
793 if (!yaml_mapping_end_event_initialize(&event))
794 return -1;
795 if (!yaml_emitter_emit(emitter, &event))
796 return -1;
798 return 0;
801 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
803 yaml_event_t event;
805 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
806 YAML_BLOCK_MAPPING_STYLE))
807 return -1;
808 if (!yaml_emitter_emit(emitter, &event))
809 return -1;
811 if (emit_string(emitter, "line") < 0)
812 return -1;
813 if (emit_int(emitter, pet_loc_get_line(stmt->loc)) < 0)
814 return -1;
816 if (emit_string(emitter, "domain") < 0)
817 return -1;
818 if (emit_set(emitter, stmt->domain) < 0)
819 return -1;
821 if (emit_string(emitter, "body") < 0)
822 return -1;
823 if (emit_tree(emitter, stmt->body) < 0)
824 return -1;
826 if (stmt->n_arg > 0) {
827 int i;
829 if (emit_string(emitter, "arguments") < 0)
830 return -1;
831 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
832 YAML_BLOCK_SEQUENCE_STYLE))
833 return -1;
834 if (!yaml_emitter_emit(emitter, &event))
835 return -1;
837 for (i = 0; i < stmt->n_arg; ++i)
838 if (emit_expr(emitter, stmt->args[i]) < 0)
839 return -1;
841 if (!yaml_sequence_end_event_initialize(&event))
842 return -1;
843 if (!yaml_emitter_emit(emitter, &event))
844 return -1;
847 if (!yaml_mapping_end_event_initialize(&event))
848 return -1;
849 if (!yaml_emitter_emit(emitter, &event))
850 return -1;
852 return 0;
855 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
856 struct pet_stmt **stmts)
858 int i;
859 yaml_event_t event;
861 if (emit_string(emitter, "statements") < 0)
862 return -1;
863 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
864 YAML_BLOCK_SEQUENCE_STYLE))
865 return -1;
866 if (!yaml_emitter_emit(emitter, &event))
867 return -1;
869 for (i = 0; i < n_stmt; ++i)
870 if (emit_stmt(emitter, stmts[i]) < 0)
871 return -1;
873 if (!yaml_sequence_end_event_initialize(&event))
874 return -1;
875 if (!yaml_emitter_emit(emitter, &event))
876 return -1;
878 return 0;
881 /* Print "implication" to "emitter".
883 static int emit_implication(yaml_emitter_t *emitter,
884 struct pet_implication *implication)
886 yaml_event_t event;
888 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
889 YAML_BLOCK_MAPPING_STYLE))
890 return -1;
891 if (!yaml_emitter_emit(emitter, &event))
892 return -1;
894 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
895 return -1;
897 if (emit_named_map(emitter, "extension", implication->extension) < 0)
898 return -1;
900 if (!yaml_mapping_end_event_initialize(&event))
901 return -1;
902 if (!yaml_emitter_emit(emitter, &event))
903 return -1;
905 return 0;
908 /* Print the list of "n_implication" "implications", if any, to "emitter".
910 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
911 struct pet_implication **implications)
913 int i;
914 yaml_event_t event;
916 if (n_implication == 0)
917 return 0;
919 if (emit_string(emitter, "implications") < 0)
920 return -1;
921 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
922 YAML_BLOCK_SEQUENCE_STYLE))
923 return -1;
924 if (!yaml_emitter_emit(emitter, &event))
925 return -1;
927 for (i = 0; i < n_implication; ++i)
928 if (emit_implication(emitter, implications[i]) < 0)
929 return -1;
931 if (!yaml_sequence_end_event_initialize(&event))
932 return -1;
933 if (!yaml_emitter_emit(emitter, &event))
934 return -1;
936 return 0;
939 /* Print "independence" to "emitter".
941 static int emit_independence(yaml_emitter_t *emitter,
942 struct pet_independence *independence)
944 yaml_event_t event;
946 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
947 YAML_BLOCK_MAPPING_STYLE))
948 return -1;
949 if (!yaml_emitter_emit(emitter, &event))
950 return -1;
952 if (emit_named_union_map(emitter, "filter", independence->filter) < 0)
953 return -1;
955 if (emit_named_union_set(emitter, "local", independence->local) < 0)
956 return -1;
958 if (!yaml_mapping_end_event_initialize(&event))
959 return -1;
960 if (!yaml_emitter_emit(emitter, &event))
961 return -1;
963 return 0;
966 /* Print the list of "n_independence" "independences", if any, to "emitter".
968 static int emit_independences(yaml_emitter_t *emitter, int n_independence,
969 struct pet_independence **independences)
971 int i;
972 yaml_event_t event;
974 if (n_independence == 0)
975 return 0;
977 if (emit_string(emitter, "independences") < 0)
978 return -1;
979 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
980 YAML_BLOCK_SEQUENCE_STYLE))
981 return -1;
982 if (!yaml_emitter_emit(emitter, &event))
983 return -1;
985 for (i = 0; i < n_independence; ++i)
986 if (emit_independence(emitter, independences[i]) < 0)
987 return -1;
989 if (!yaml_sequence_end_event_initialize(&event))
990 return -1;
991 if (!yaml_emitter_emit(emitter, &event))
992 return -1;
994 return 0;
997 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
999 yaml_event_t event;
1001 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
1002 YAML_BLOCK_MAPPING_STYLE))
1003 return -1;
1004 if (!yaml_emitter_emit(emitter, &event))
1005 return -1;
1007 if (emit_named_unsigned(emitter,
1008 "start", pet_loc_get_start(scop->loc)) < 0)
1009 return -1;
1010 if (emit_named_unsigned(emitter, "end", pet_loc_get_end(scop->loc)) < 0)
1011 return -1;
1012 if (emit_named_string(emitter,
1013 "indent", pet_loc_get_indent(scop->loc)) < 0)
1014 return -1;
1015 if (emit_string(emitter, "context") < 0)
1016 return -1;
1017 if (emit_set(emitter, scop->context) < 0)
1018 return -1;
1019 if (!isl_set_plain_is_universe(scop->context_value) &&
1020 emit_named_set(emitter, "context_value", scop->context_value) < 0)
1021 return -1;
1022 if (emit_named_schedule(emitter, "schedule", scop->schedule) < 0)
1023 return -1;
1025 if (emit_types(emitter, scop->n_type, scop->types) < 0)
1026 return -1;
1027 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
1028 return -1;
1030 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
1031 return -1;
1033 if (emit_implications(emitter, scop->n_implication,
1034 scop->implications) < 0)
1035 return -1;
1037 if (emit_independences(emitter, scop->n_independence,
1038 scop->independences) < 0)
1039 return -1;
1041 if (!yaml_mapping_end_event_initialize(&event))
1042 return -1;
1043 if (!yaml_emitter_emit(emitter, &event))
1044 return -1;
1046 return 0;
1049 /* Print a YAML serialization of "scop" to "out".
1051 int pet_scop_emit(FILE *out, struct pet_scop *scop)
1053 yaml_emitter_t emitter;
1054 yaml_event_t event;
1056 yaml_emitter_initialize(&emitter);
1058 yaml_emitter_set_output_file(&emitter, out);
1060 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
1061 if (!yaml_emitter_emit(&emitter, &event))
1062 goto error;
1064 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
1065 goto error;
1066 if (!yaml_emitter_emit(&emitter, &event))
1067 goto error;
1069 if (emit_scop(&emitter, scop) < 0)
1070 goto error;
1072 if (!yaml_document_end_event_initialize(&event, 1))
1073 goto error;
1074 if (!yaml_emitter_emit(&emitter, &event))
1075 goto error;
1077 yaml_stream_end_event_initialize(&event);
1078 if (!yaml_emitter_emit(&emitter, &event))
1079 goto error;
1081 yaml_emitter_delete(&emitter);
1082 return 0;
1083 error:
1084 yaml_emitter_delete(&emitter);
1085 return -1;