extract patching of index expressions and access relations to separate file
[pet.git] / emit.c
blobfcf10b9900377d7f34c571cc4bd9d9666a29c375
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/union_set.h>
39 #include "expr.h"
40 #include "loc.h"
41 #include "scop.h"
42 #include "scop_yaml.h"
43 #include "tree.h"
45 static int emit_string(yaml_emitter_t *emitter, const char *str)
47 yaml_event_t event;
49 if (!yaml_scalar_event_initialize(&event, NULL, NULL,
50 (yaml_char_t *) str, strlen(str),
51 1, 1, YAML_PLAIN_SCALAR_STYLE))
52 return -1;
53 if (!yaml_emitter_emit(emitter, &event))
54 return -1;
56 return 0;
59 /* Print the string "name" and the string "str" to "emitter".
61 static int emit_named_string(yaml_emitter_t *emitter, const char *name,
62 const char *str)
64 if (emit_string(emitter, name) < 0)
65 return -1;
66 if (emit_string(emitter, str) < 0)
67 return -1;
68 return 0;
71 /* Print the isl_id "id" to "emitter".
73 static int emit_id(yaml_emitter_t *emitter, __isl_keep isl_id *id)
75 return emit_string(emitter, isl_id_get_name(id));
78 /* Print the string "name" and the isl_id "id" to "emitter".
80 static int emit_named_id(yaml_emitter_t *emitter, const char *name,
81 __isl_keep isl_id *id)
83 if (emit_string(emitter, name) < 0)
84 return -1;
85 if (emit_id(emitter, id) < 0)
86 return -1;
87 return 0;
90 static int emit_int(yaml_emitter_t *emitter, int i)
92 char buffer[40];
94 snprintf(buffer, sizeof(buffer), "%d", i);
95 return emit_string(emitter, buffer);
98 static int emit_named_int(yaml_emitter_t *emitter, const char *name, int i)
100 if (emit_string(emitter, name) < 0)
101 return -1;
102 if (emit_int(emitter, i) < 0)
103 return -1;
104 return 0;
107 /* Print the unsigned integer "u" to "emitter".
109 static int emit_unsigned(yaml_emitter_t *emitter, unsigned u)
111 char buffer[40];
113 snprintf(buffer, sizeof(buffer), "%u", u);
114 return emit_string(emitter, buffer);
117 /* Print the string "name" and the unsigned integer "u" to "emitter".
119 static int emit_named_unsigned(yaml_emitter_t *emitter, const char *name,
120 unsigned u)
122 if (emit_string(emitter, name) < 0)
123 return -1;
124 if (emit_int(emitter, u) < 0)
125 return -1;
126 return 0;
129 static int emit_double(yaml_emitter_t *emitter, double d)
131 char buffer[40];
133 snprintf(buffer, sizeof(buffer), "%g", d);
134 return emit_string(emitter, buffer);
137 static int emit_map(yaml_emitter_t *emitter, __isl_keep isl_map *map)
139 isl_ctx *ctx = isl_map_get_ctx(map);
140 isl_printer *p;
141 char *str;
142 int r;
144 p = isl_printer_to_str(ctx);
145 p = isl_printer_print_map(p, map);
146 str = isl_printer_get_str(p);
147 isl_printer_free(p);
148 r = emit_string(emitter, str);
149 free(str);
150 return r;
153 /* Print the isl_val "val" to "emitter".
155 static int emit_val(yaml_emitter_t *emitter, __isl_keep isl_val *val)
157 isl_ctx *ctx = isl_val_get_ctx(val);
158 isl_printer *p;
159 char *str;
160 int r;
162 p = isl_printer_to_str(ctx);
163 p = isl_printer_print_val(p, val);
164 str = isl_printer_get_str(p);
165 isl_printer_free(p);
166 r = emit_string(emitter, str);
167 free(str);
168 return r;
171 /* Print the string "name" and the isl_val "val" to "emitter".
173 static int emit_named_val(yaml_emitter_t *emitter, const char *name,
174 __isl_keep isl_val *val)
176 if (emit_string(emitter, name) < 0)
177 return -1;
178 if (emit_val(emitter, val) < 0)
179 return -1;
180 return 0;
183 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
185 isl_ctx *ctx = isl_set_get_ctx(set);
186 isl_printer *p;
187 char *str;
188 int r;
190 p = isl_printer_to_str(ctx);
191 p = isl_printer_print_set(p, set);
192 str = isl_printer_get_str(p);
193 isl_printer_free(p);
194 r = emit_string(emitter, str);
195 free(str);
196 return r;
199 static int emit_named_set(yaml_emitter_t *emitter, const char *name,
200 __isl_keep isl_set *set)
202 if (emit_string(emitter, name) < 0)
203 return -1;
204 if (emit_set(emitter, set) < 0)
205 return -1;
206 return 0;
209 /* Print the string "name" and the map "map" to "emitter".
211 static int emit_named_map(yaml_emitter_t *emitter, const char *name,
212 __isl_keep isl_map *map)
214 if (emit_string(emitter, name) < 0)
215 return -1;
216 if (emit_map(emitter, map) < 0)
217 return -1;
218 return 0;
221 /* Print the union set "uset" to "emitter".
223 static int emit_union_set(yaml_emitter_t *emitter,
224 __isl_keep isl_union_set *uset)
226 isl_ctx *ctx = isl_union_set_get_ctx(uset);
227 isl_printer *p;
228 char *str;
229 int r;
231 p = isl_printer_to_str(ctx);
232 p = isl_printer_print_union_set(p, uset);
233 str = isl_printer_get_str(p);
234 isl_printer_free(p);
235 r = emit_string(emitter, str);
236 free(str);
237 return r;
240 /* Print the union map "umap" to "emitter".
242 static int emit_union_map(yaml_emitter_t *emitter,
243 __isl_keep isl_union_map *umap)
245 isl_ctx *ctx = isl_union_map_get_ctx(umap);
246 isl_printer *p;
247 char *str;
248 int r;
250 p = isl_printer_to_str(ctx);
251 p = isl_printer_print_union_map(p, umap);
252 str = isl_printer_get_str(p);
253 isl_printer_free(p);
254 r = emit_string(emitter, str);
255 free(str);
256 return r;
259 /* Print the string "name" and the union set "uset" to "emitter".
261 static int emit_named_union_set(yaml_emitter_t *emitter, const char *name,
262 __isl_keep isl_union_set *uset)
264 if (emit_string(emitter, name) < 0)
265 return -1;
266 if (emit_union_set(emitter, uset) < 0)
267 return -1;
268 return 0;
271 /* Print the string "name" and the union map "umap" to "emitter".
273 static int emit_named_union_map(yaml_emitter_t *emitter, const char *name,
274 __isl_keep isl_union_map *umap)
276 if (emit_string(emitter, name) < 0)
277 return -1;
278 if (emit_union_map(emitter, umap) < 0)
279 return -1;
280 return 0;
283 /* Print the isl_multi_pw_aff "mpa" to "emitter".
285 static int emit_multi_pw_aff(yaml_emitter_t *emitter,
286 __isl_keep isl_multi_pw_aff *mpa)
288 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(mpa);
289 isl_printer *p;
290 char *str;
291 int r;
293 p = isl_printer_to_str(ctx);
294 p = isl_printer_print_multi_pw_aff(p, mpa);
295 str = isl_printer_get_str(p);
296 isl_printer_free(p);
297 r = emit_string(emitter, str);
298 free(str);
299 return r;
302 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
304 static int emit_named_multi_pw_aff(yaml_emitter_t *emitter, const char *name,
305 __isl_keep isl_multi_pw_aff *mpa)
307 if (emit_string(emitter, name) < 0)
308 return -1;
309 if (emit_multi_pw_aff(emitter, mpa) < 0)
310 return -1;
311 return 0;
314 /* Print the schedule "schedule" to "emitter".
316 static int emit_schedule(yaml_emitter_t *emitter,
317 __isl_keep isl_schedule *schedule)
319 isl_ctx *ctx = isl_schedule_get_ctx(schedule);
320 isl_printer *p;
321 char *str;
322 int r;
324 p = isl_printer_to_str(ctx);
325 p = isl_printer_print_schedule(p, schedule);
326 str = isl_printer_get_str(p);
327 isl_printer_free(p);
328 r = emit_string(emitter, str);
329 free(str);
330 return r;
333 /* Print the string "name" and the schedule "schedule" to "emitter".
335 static int emit_named_schedule(yaml_emitter_t *emitter, const char *name,
336 __isl_keep isl_schedule *schedule)
338 if (emit_string(emitter, name) < 0)
339 return -1;
340 if (emit_schedule(emitter, schedule) < 0)
341 return -1;
342 return 0;
345 /* Print "type" to "emitter".
347 static int emit_type(yaml_emitter_t *emitter, struct pet_type *type)
349 yaml_event_t event;
351 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
352 YAML_BLOCK_MAPPING_STYLE))
353 return -1;
354 if (!yaml_emitter_emit(emitter, &event))
355 return -1;
357 if (emit_string(emitter, "name") < 0)
358 return -1;
359 if (emit_string(emitter, type->name) < 0)
360 return -1;
362 if (emit_string(emitter, "definition") < 0)
363 return -1;
364 if (emit_string(emitter, type->definition) < 0)
365 return -1;
367 if (!yaml_mapping_end_event_initialize(&event))
368 return -1;
369 if (!yaml_emitter_emit(emitter, &event))
370 return -1;
372 return 0;
375 /* Print the list of "n_type" "types", if any, to "emitter".
377 static int emit_types(yaml_emitter_t *emitter, int n_type,
378 struct pet_type **types)
380 int i;
381 yaml_event_t event;
383 if (n_type == 0)
384 return 0;
386 if (emit_string(emitter, "types") < 0)
387 return -1;
388 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
389 YAML_BLOCK_SEQUENCE_STYLE))
390 return -1;
391 if (!yaml_emitter_emit(emitter, &event))
392 return -1;
394 for (i = 0; i < n_type; ++i)
395 if (emit_type(emitter, types[i]) < 0)
396 return -1;
398 if (!yaml_sequence_end_event_initialize(&event))
399 return -1;
400 if (!yaml_emitter_emit(emitter, &event))
401 return -1;
403 return 0;
406 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
408 yaml_event_t event;
410 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
411 YAML_BLOCK_MAPPING_STYLE))
412 return -1;
413 if (!yaml_emitter_emit(emitter, &event))
414 return -1;
416 if (emit_string(emitter, "context") < 0)
417 return -1;
418 if (emit_set(emitter, array->context) < 0)
419 return -1;
421 if (emit_string(emitter, "extent") < 0)
422 return -1;
423 if (emit_set(emitter, array->extent) < 0)
424 return -1;
426 if (array->value_bounds) {
427 if (emit_string(emitter, "value_bounds") < 0)
428 return -1;
429 if (emit_set(emitter, array->value_bounds) < 0)
430 return -1;
433 if (emit_string(emitter, "element_type") < 0)
434 return -1;
435 if (emit_string(emitter, array->element_type) < 0)
436 return -1;
437 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
438 return -1;
440 if (array->element_is_record)
441 if (emit_named_int(emitter, "element_is_record",
442 array->element_is_record) < 0)
443 return -1;
445 if (array->live_out) {
446 if (emit_string(emitter, "live_out") < 0)
447 return -1;
448 if (emit_string(emitter, "1") < 0)
449 return -1;
452 if (array->uniquely_defined) {
453 if (emit_string(emitter, "uniquely_defined") < 0)
454 return -1;
455 if (emit_string(emitter, "1") < 0)
456 return -1;
459 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
460 return -1;
461 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
462 return -1;
464 if (!yaml_mapping_end_event_initialize(&event))
465 return -1;
466 if (!yaml_emitter_emit(emitter, &event))
467 return -1;
469 return 0;
472 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
473 struct pet_array **arrays)
475 int i;
476 yaml_event_t event;
478 if (emit_string(emitter, "arrays") < 0)
479 return -1;
480 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
481 YAML_BLOCK_SEQUENCE_STYLE))
482 return -1;
483 if (!yaml_emitter_emit(emitter, &event))
484 return -1;
486 for (i = 0; i < n_array; ++i)
487 if (emit_array(emitter, arrays[i]) < 0)
488 return -1;
490 if (!yaml_sequence_end_event_initialize(&event))
491 return -1;
492 if (!yaml_emitter_emit(emitter, &event))
493 return -1;
495 return 0;
498 static int emit_expr_type(yaml_emitter_t *emitter, enum pet_expr_type type)
500 if (emit_string(emitter, pet_type_str(type)) < 0)
501 return -1;
502 return 0;
505 /* Print the fields of the access expression "expr" to "emitter".
507 * Only print the access relations that are present and relevant
508 * for the type of access.
510 * If the depth of the access is equal to the depth that can be derived from
511 * the index expression, then there is no need to print it.
513 static int emit_access_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
515 int depth;
517 if (expr->acc.kill && expr->acc.access[pet_expr_access_fake_killed] &&
518 emit_named_union_map(emitter, "killed",
519 expr->acc.access[pet_expr_access_fake_killed]) < 0)
520 return -1;
521 if (expr->acc.read && expr->acc.access[pet_expr_access_may_read] &&
522 emit_named_union_map(emitter, "may_read",
523 expr->acc.access[pet_expr_access_may_read]) < 0)
524 return -1;
525 if (expr->acc.write && expr->acc.access[pet_expr_access_may_write] &&
526 emit_named_union_map(emitter, "may_write",
527 expr->acc.access[pet_expr_access_may_write]) < 0)
528 return -1;
529 if (expr->acc.write && expr->acc.access[pet_expr_access_must_write] &&
530 emit_named_union_map(emitter, "must_write",
531 expr->acc.access[pet_expr_access_must_write]) < 0)
532 return -1;
533 if (emit_named_multi_pw_aff(emitter, "index", expr->acc.index) < 0)
534 return -1;
535 depth = isl_multi_pw_aff_dim(expr->acc.index, isl_dim_out);
536 if (expr->acc.depth != depth &&
537 emit_named_int(emitter, "depth", expr->acc.depth) < 0)
538 return -1;
539 if (expr->acc.ref_id &&
540 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
541 return -1;
542 if (expr->acc.kill) {
543 if (emit_named_unsigned(emitter, "kill", 1) < 0)
544 return -1;
545 } else {
546 if (emit_string(emitter, "read") < 0)
547 return -1;
548 if (emit_int(emitter, expr->acc.read) < 0)
549 return -1;
550 if (emit_string(emitter, "write") < 0)
551 return -1;
552 if (emit_int(emitter, expr->acc.write) < 0)
553 return -1;
556 return 0;
559 static int emit_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
561 yaml_event_t event;
563 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
564 YAML_BLOCK_MAPPING_STYLE))
565 return -1;
566 if (!yaml_emitter_emit(emitter, &event))
567 return -1;
569 if (emit_string(emitter, "type") < 0)
570 return -1;
571 if (emit_expr_type(emitter, expr->type) < 0)
572 return -1;
574 switch (expr->type) {
575 case pet_expr_error:
576 return -1;
577 case pet_expr_int:
578 if (emit_named_val(emitter, "value", expr->i) < 0)
579 return -1;
580 break;
581 case pet_expr_double:
582 if (emit_string(emitter, "value") < 0)
583 return -1;
584 if (emit_double(emitter, expr->d.val) < 0)
585 return -1;
586 if (emit_string(emitter, "string") < 0)
587 return -1;
588 if (emit_string(emitter, expr->d.s) < 0)
589 return -1;
590 break;
591 case pet_expr_access:
592 if (emit_access_expr(emitter, expr) < 0)
593 return -1;
594 break;
595 case pet_expr_op:
596 if (emit_string(emitter, "operation") < 0)
597 return -1;
598 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
599 return -1;
600 break;
601 case pet_expr_call:
602 if (emit_string(emitter, "name") < 0)
603 return -1;
604 if (emit_string(emitter, expr->c.name) < 0)
605 return -1;
606 break;
607 case pet_expr_cast:
608 if (emit_string(emitter, "type_name") < 0)
609 return -1;
610 if (emit_string(emitter, expr->type_name) < 0)
611 return -1;
612 break;
615 if (expr->n_arg > 0) {
616 int i;
618 if (emit_string(emitter, "arguments") < 0)
619 return -1;
620 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
621 YAML_BLOCK_SEQUENCE_STYLE))
622 return -1;
623 if (!yaml_emitter_emit(emitter, &event))
624 return -1;
626 for (i = 0; i < expr->n_arg; ++i)
627 if (emit_expr(emitter, expr->args[i]) < 0)
628 return -1;
630 if (!yaml_sequence_end_event_initialize(&event))
631 return -1;
632 if (!yaml_emitter_emit(emitter, &event))
633 return -1;
636 if (!yaml_mapping_end_event_initialize(&event))
637 return -1;
638 if (!yaml_emitter_emit(emitter, &event))
639 return -1;
641 return 0;
644 /* Print the string "name" and the expression "expr" to "emitter".
646 static int emit_named_expr(yaml_emitter_t *emitter, const char *name,
647 __isl_keep pet_expr *expr)
649 if (emit_string(emitter, name) < 0)
650 return -1;
651 if (emit_expr(emitter, expr) < 0)
652 return -1;
653 return 0;
656 /* Print "type" to "emitter".
658 static int emit_tree_type(yaml_emitter_t *emitter, enum pet_tree_type type)
660 if (emit_string(emitter, pet_tree_type_str(type)) < 0)
661 return -1;
662 return 0;
665 /* Recursively print "tree" to "emitter".
667 static int emit_tree(yaml_emitter_t *emitter, __isl_keep pet_tree *tree)
669 yaml_event_t event;
670 int i;
672 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
673 YAML_BLOCK_MAPPING_STYLE))
674 return -1;
675 if (!yaml_emitter_emit(emitter, &event))
676 return -1;
678 if (emit_string(emitter, "type") < 0)
679 return -1;
680 if (emit_tree_type(emitter, tree->type) < 0)
681 return -1;
683 switch (tree->type) {
684 case pet_tree_error:
685 return -1;
686 case pet_tree_block:
687 if (emit_named_int(emitter, "block", tree->u.b.block) < 0)
688 return -1;
689 if (tree->u.b.n == 0)
690 break;
692 if (emit_string(emitter, "children") < 0)
693 return -1;
694 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
695 YAML_BLOCK_SEQUENCE_STYLE))
696 return -1;
697 if (!yaml_emitter_emit(emitter, &event))
698 return -1;
700 for (i = 0; i < tree->u.b.n; ++i)
701 if (emit_tree(emitter, tree->u.b.child[i]) < 0)
702 return -1;
704 if (!yaml_sequence_end_event_initialize(&event))
705 return -1;
706 if (!yaml_emitter_emit(emitter, &event))
707 return -1;
708 break;
709 case pet_tree_break:
710 case pet_tree_continue:
711 break;
712 case pet_tree_decl:
713 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
714 return -1;
715 break;
716 case pet_tree_decl_init:
717 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
718 return -1;
719 if (emit_named_expr(emitter,
720 "initialization", tree->u.d.init) < 0)
721 return -1;
722 break;
723 case pet_tree_expr:
724 if (emit_named_expr(emitter, "expr", tree->u.e.expr) < 0)
725 return -1;
726 break;
727 case pet_tree_for:
728 if (tree->u.l.independent)
729 if (emit_named_int(emitter, "independent", 1) < 0)
730 return -1;
731 if (emit_named_int(emitter, "declared", tree->u.l.declared) < 0)
732 return -1;
733 if (emit_named_expr(emitter, "variable", tree->u.l.iv) < 0)
734 return -1;
735 if (emit_named_expr(emitter,
736 "initialization", tree->u.l.init) < 0)
737 return -1;
738 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
739 return -1;
740 if (emit_named_expr(emitter, "increment", tree->u.l.inc) < 0)
741 return -1;
742 if (emit_string(emitter, "body") < 0)
743 return -1;
744 if (emit_tree(emitter, tree->u.l.body) < 0)
745 return -1;
746 break;
747 case pet_tree_while:
748 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
749 return -1;
750 if (emit_string(emitter, "body") < 0)
751 return -1;
752 if (emit_tree(emitter, tree->u.l.body) < 0)
753 return -1;
754 break;
755 case pet_tree_infinite_loop:
756 if (emit_string(emitter, "body") < 0)
757 return -1;
758 if (emit_tree(emitter, tree->u.l.body) < 0)
759 return -1;
760 break;
761 case pet_tree_if:
762 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
763 return -1;
764 if (emit_string(emitter, "then") < 0)
765 return -1;
766 if (emit_tree(emitter, tree->u.i.then_body) < 0)
767 return -1;
768 break;
769 case pet_tree_if_else:
770 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
771 return -1;
772 if (emit_string(emitter, "then") < 0)
773 return -1;
774 if (emit_tree(emitter, tree->u.i.then_body) < 0)
775 return -1;
776 if (emit_string(emitter, "else") < 0)
777 return -1;
778 if (emit_tree(emitter, tree->u.i.else_body) < 0)
779 return -1;
780 break;
783 if (!yaml_mapping_end_event_initialize(&event))
784 return -1;
785 if (!yaml_emitter_emit(emitter, &event))
786 return -1;
788 return 0;
791 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
793 yaml_event_t event;
795 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
796 YAML_BLOCK_MAPPING_STYLE))
797 return -1;
798 if (!yaml_emitter_emit(emitter, &event))
799 return -1;
801 if (emit_string(emitter, "line") < 0)
802 return -1;
803 if (emit_int(emitter, pet_loc_get_line(stmt->loc)) < 0)
804 return -1;
806 if (emit_string(emitter, "domain") < 0)
807 return -1;
808 if (emit_set(emitter, stmt->domain) < 0)
809 return -1;
811 if (emit_string(emitter, "body") < 0)
812 return -1;
813 if (emit_tree(emitter, stmt->body) < 0)
814 return -1;
816 if (stmt->n_arg > 0) {
817 int i;
819 if (emit_string(emitter, "arguments") < 0)
820 return -1;
821 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
822 YAML_BLOCK_SEQUENCE_STYLE))
823 return -1;
824 if (!yaml_emitter_emit(emitter, &event))
825 return -1;
827 for (i = 0; i < stmt->n_arg; ++i)
828 if (emit_expr(emitter, stmt->args[i]) < 0)
829 return -1;
831 if (!yaml_sequence_end_event_initialize(&event))
832 return -1;
833 if (!yaml_emitter_emit(emitter, &event))
834 return -1;
837 if (!yaml_mapping_end_event_initialize(&event))
838 return -1;
839 if (!yaml_emitter_emit(emitter, &event))
840 return -1;
842 return 0;
845 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
846 struct pet_stmt **stmts)
848 int i;
849 yaml_event_t event;
851 if (emit_string(emitter, "statements") < 0)
852 return -1;
853 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
854 YAML_BLOCK_SEQUENCE_STYLE))
855 return -1;
856 if (!yaml_emitter_emit(emitter, &event))
857 return -1;
859 for (i = 0; i < n_stmt; ++i)
860 if (emit_stmt(emitter, stmts[i]) < 0)
861 return -1;
863 if (!yaml_sequence_end_event_initialize(&event))
864 return -1;
865 if (!yaml_emitter_emit(emitter, &event))
866 return -1;
868 return 0;
871 /* Print "implication" to "emitter".
873 static int emit_implication(yaml_emitter_t *emitter,
874 struct pet_implication *implication)
876 yaml_event_t event;
878 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
879 YAML_BLOCK_MAPPING_STYLE))
880 return -1;
881 if (!yaml_emitter_emit(emitter, &event))
882 return -1;
884 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
885 return -1;
887 if (emit_named_map(emitter, "extension", implication->extension) < 0)
888 return -1;
890 if (!yaml_mapping_end_event_initialize(&event))
891 return -1;
892 if (!yaml_emitter_emit(emitter, &event))
893 return -1;
895 return 0;
898 /* Print the list of "n_implication" "implications", if any, to "emitter".
900 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
901 struct pet_implication **implications)
903 int i;
904 yaml_event_t event;
906 if (n_implication == 0)
907 return 0;
909 if (emit_string(emitter, "implications") < 0)
910 return -1;
911 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
912 YAML_BLOCK_SEQUENCE_STYLE))
913 return -1;
914 if (!yaml_emitter_emit(emitter, &event))
915 return -1;
917 for (i = 0; i < n_implication; ++i)
918 if (emit_implication(emitter, implications[i]) < 0)
919 return -1;
921 if (!yaml_sequence_end_event_initialize(&event))
922 return -1;
923 if (!yaml_emitter_emit(emitter, &event))
924 return -1;
926 return 0;
929 /* Print "independence" to "emitter".
931 static int emit_independence(yaml_emitter_t *emitter,
932 struct pet_independence *independence)
934 yaml_event_t event;
936 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
937 YAML_BLOCK_MAPPING_STYLE))
938 return -1;
939 if (!yaml_emitter_emit(emitter, &event))
940 return -1;
942 if (emit_named_union_map(emitter, "filter", independence->filter) < 0)
943 return -1;
945 if (emit_named_union_set(emitter, "local", independence->local) < 0)
946 return -1;
948 if (!yaml_mapping_end_event_initialize(&event))
949 return -1;
950 if (!yaml_emitter_emit(emitter, &event))
951 return -1;
953 return 0;
956 /* Print the list of "n_independence" "independences", if any, to "emitter".
958 static int emit_independences(yaml_emitter_t *emitter, int n_independence,
959 struct pet_independence **independences)
961 int i;
962 yaml_event_t event;
964 if (n_independence == 0)
965 return 0;
967 if (emit_string(emitter, "independences") < 0)
968 return -1;
969 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
970 YAML_BLOCK_SEQUENCE_STYLE))
971 return -1;
972 if (!yaml_emitter_emit(emitter, &event))
973 return -1;
975 for (i = 0; i < n_independence; ++i)
976 if (emit_independence(emitter, independences[i]) < 0)
977 return -1;
979 if (!yaml_sequence_end_event_initialize(&event))
980 return -1;
981 if (!yaml_emitter_emit(emitter, &event))
982 return -1;
984 return 0;
987 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
989 yaml_event_t event;
991 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
992 YAML_BLOCK_MAPPING_STYLE))
993 return -1;
994 if (!yaml_emitter_emit(emitter, &event))
995 return -1;
997 if (emit_named_unsigned(emitter,
998 "start", pet_loc_get_start(scop->loc)) < 0)
999 return -1;
1000 if (emit_named_unsigned(emitter, "end", pet_loc_get_end(scop->loc)) < 0)
1001 return -1;
1002 if (emit_named_string(emitter,
1003 "indent", pet_loc_get_indent(scop->loc)) < 0)
1004 return -1;
1005 if (emit_string(emitter, "context") < 0)
1006 return -1;
1007 if (emit_set(emitter, scop->context) < 0)
1008 return -1;
1009 if (!isl_set_plain_is_universe(scop->context_value) &&
1010 emit_named_set(emitter, "context_value", scop->context_value) < 0)
1011 return -1;
1012 if (emit_named_schedule(emitter, "schedule", scop->schedule) < 0)
1013 return -1;
1015 if (emit_types(emitter, scop->n_type, scop->types) < 0)
1016 return -1;
1017 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
1018 return -1;
1020 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
1021 return -1;
1023 if (emit_implications(emitter, scop->n_implication,
1024 scop->implications) < 0)
1025 return -1;
1027 if (emit_independences(emitter, scop->n_independence,
1028 scop->independences) < 0)
1029 return -1;
1031 if (!yaml_mapping_end_event_initialize(&event))
1032 return -1;
1033 if (!yaml_emitter_emit(emitter, &event))
1034 return -1;
1036 return 0;
1039 /* Print a YAML serialization of "scop" to "out".
1041 int pet_scop_emit(FILE *out, struct pet_scop *scop)
1043 yaml_emitter_t emitter;
1044 yaml_event_t event;
1046 yaml_emitter_initialize(&emitter);
1048 yaml_emitter_set_output_file(&emitter, out);
1050 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
1051 if (!yaml_emitter_emit(&emitter, &event))
1052 goto error;
1054 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
1055 goto error;
1056 if (!yaml_emitter_emit(&emitter, &event))
1057 goto error;
1059 if (emit_scop(&emitter, scop) < 0)
1060 goto error;
1062 if (!yaml_document_end_event_initialize(&event, 1))
1063 goto error;
1064 if (!yaml_emitter_emit(&emitter, &event))
1065 goto error;
1067 yaml_stream_end_event_initialize(&event);
1068 if (!yaml_emitter_emit(&emitter, &event))
1069 goto error;
1071 yaml_emitter_delete(&emitter);
1072 return 0;
1073 error:
1074 yaml_emitter_delete(&emitter);
1075 return -1;