introduce pet_expr_access_type
[pet.git] / emit.c
blobc80a51dfe98f4a9d21d91c295bdbb4b2a85449f7
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 "expr.h"
38 #include "loc.h"
39 #include "scop.h"
40 #include "scop_yaml.h"
41 #include "tree.h"
43 static int emit_string(yaml_emitter_t *emitter, const char *str)
45 yaml_event_t event;
47 if (!yaml_scalar_event_initialize(&event, NULL, NULL,
48 (yaml_char_t *) str, strlen(str),
49 1, 1, YAML_PLAIN_SCALAR_STYLE))
50 return -1;
51 if (!yaml_emitter_emit(emitter, &event))
52 return -1;
54 return 0;
57 /* Print the string "name" and the string "str" to "emitter".
59 static int emit_named_string(yaml_emitter_t *emitter, const char *name,
60 const char *str)
62 if (emit_string(emitter, name) < 0)
63 return -1;
64 if (emit_string(emitter, str) < 0)
65 return -1;
66 return 0;
69 /* Print the isl_id "id" to "emitter".
71 static int emit_id(yaml_emitter_t *emitter, __isl_keep isl_id *id)
73 return emit_string(emitter, isl_id_get_name(id));
76 /* Print the string "name" and the isl_id "id" to "emitter".
78 static int emit_named_id(yaml_emitter_t *emitter, const char *name,
79 __isl_keep isl_id *id)
81 if (emit_string(emitter, name) < 0)
82 return -1;
83 if (emit_id(emitter, id) < 0)
84 return -1;
85 return 0;
88 static int emit_int(yaml_emitter_t *emitter, int i)
90 char buffer[40];
92 snprintf(buffer, sizeof(buffer), "%d", i);
93 return emit_string(emitter, buffer);
96 static int emit_named_int(yaml_emitter_t *emitter, const char *name, int i)
98 if (emit_string(emitter, name) < 0)
99 return -1;
100 if (emit_int(emitter, i) < 0)
101 return -1;
102 return 0;
105 /* Print the unsigned integer "u" to "emitter".
107 static int emit_unsigned(yaml_emitter_t *emitter, unsigned u)
109 char buffer[40];
111 snprintf(buffer, sizeof(buffer), "%u", u);
112 return emit_string(emitter, buffer);
115 /* Print the string "name" and the unsigned integer "u" to "emitter".
117 static int emit_named_unsigned(yaml_emitter_t *emitter, const char *name,
118 unsigned u)
120 if (emit_string(emitter, name) < 0)
121 return -1;
122 if (emit_int(emitter, u) < 0)
123 return -1;
124 return 0;
127 static int emit_double(yaml_emitter_t *emitter, double d)
129 char buffer[40];
131 snprintf(buffer, sizeof(buffer), "%g", d);
132 return emit_string(emitter, buffer);
135 static int emit_map(yaml_emitter_t *emitter, __isl_keep isl_map *map)
137 isl_ctx *ctx = isl_map_get_ctx(map);
138 isl_printer *p;
139 char *str;
140 int r;
142 p = isl_printer_to_str(ctx);
143 p = isl_printer_print_map(p, map);
144 str = isl_printer_get_str(p);
145 isl_printer_free(p);
146 r = emit_string(emitter, str);
147 free(str);
148 return r;
151 /* Print the isl_val "val" to "emitter".
153 static int emit_val(yaml_emitter_t *emitter, __isl_keep isl_val *val)
155 isl_ctx *ctx = isl_val_get_ctx(val);
156 isl_printer *p;
157 char *str;
158 int r;
160 p = isl_printer_to_str(ctx);
161 p = isl_printer_print_val(p, val);
162 str = isl_printer_get_str(p);
163 isl_printer_free(p);
164 r = emit_string(emitter, str);
165 free(str);
166 return r;
169 /* Print the string "name" and the isl_val "val" to "emitter".
171 static int emit_named_val(yaml_emitter_t *emitter, const char *name,
172 __isl_keep isl_val *val)
174 if (emit_string(emitter, name) < 0)
175 return -1;
176 if (emit_val(emitter, val) < 0)
177 return -1;
178 return 0;
181 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
183 isl_ctx *ctx = isl_set_get_ctx(set);
184 isl_printer *p;
185 char *str;
186 int r;
188 p = isl_printer_to_str(ctx);
189 p = isl_printer_print_set(p, set);
190 str = isl_printer_get_str(p);
191 isl_printer_free(p);
192 r = emit_string(emitter, str);
193 free(str);
194 return r;
197 static int emit_named_set(yaml_emitter_t *emitter, const char *name,
198 __isl_keep isl_set *set)
200 if (emit_string(emitter, name) < 0)
201 return -1;
202 if (emit_set(emitter, set) < 0)
203 return -1;
204 return 0;
207 /* Print the string "name" and the map "map" to "emitter".
209 static int emit_named_map(yaml_emitter_t *emitter, const char *name,
210 __isl_keep isl_map *map)
212 if (emit_string(emitter, name) < 0)
213 return -1;
214 if (emit_map(emitter, map) < 0)
215 return -1;
216 return 0;
219 /* Print the union set "uset" to "emitter".
221 static int emit_union_set(yaml_emitter_t *emitter,
222 __isl_keep isl_union_set *uset)
224 isl_ctx *ctx = isl_union_set_get_ctx(uset);
225 isl_printer *p;
226 char *str;
227 int r;
229 p = isl_printer_to_str(ctx);
230 p = isl_printer_print_union_set(p, uset);
231 str = isl_printer_get_str(p);
232 isl_printer_free(p);
233 r = emit_string(emitter, str);
234 free(str);
235 return r;
238 /* Print the union map "umap" to "emitter".
240 static int emit_union_map(yaml_emitter_t *emitter,
241 __isl_keep isl_union_map *umap)
243 isl_ctx *ctx = isl_union_map_get_ctx(umap);
244 isl_printer *p;
245 char *str;
246 int r;
248 p = isl_printer_to_str(ctx);
249 p = isl_printer_print_union_map(p, umap);
250 str = isl_printer_get_str(p);
251 isl_printer_free(p);
252 r = emit_string(emitter, str);
253 free(str);
254 return r;
257 /* Print the string "name" and the union set "uset" to "emitter".
259 static int emit_named_union_set(yaml_emitter_t *emitter, const char *name,
260 __isl_keep isl_union_set *uset)
262 if (emit_string(emitter, name) < 0)
263 return -1;
264 if (emit_union_set(emitter, uset) < 0)
265 return -1;
266 return 0;
269 /* Print the string "name" and the union map "umap" to "emitter".
271 static int emit_named_union_map(yaml_emitter_t *emitter, const char *name,
272 __isl_keep isl_union_map *umap)
274 if (emit_string(emitter, name) < 0)
275 return -1;
276 if (emit_union_map(emitter, umap) < 0)
277 return -1;
278 return 0;
281 /* Print the isl_multi_pw_aff "mpa" to "emitter".
283 static int emit_multi_pw_aff(yaml_emitter_t *emitter,
284 __isl_keep isl_multi_pw_aff *mpa)
286 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(mpa);
287 isl_printer *p;
288 char *str;
289 int r;
291 p = isl_printer_to_str(ctx);
292 p = isl_printer_print_multi_pw_aff(p, mpa);
293 str = isl_printer_get_str(p);
294 isl_printer_free(p);
295 r = emit_string(emitter, str);
296 free(str);
297 return r;
300 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
302 static int emit_named_multi_pw_aff(yaml_emitter_t *emitter, const char *name,
303 __isl_keep isl_multi_pw_aff *mpa)
305 if (emit_string(emitter, name) < 0)
306 return -1;
307 if (emit_multi_pw_aff(emitter, mpa) < 0)
308 return -1;
309 return 0;
312 /* Print "type" to "emitter".
314 static int emit_type(yaml_emitter_t *emitter, struct pet_type *type)
316 yaml_event_t event;
318 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
319 YAML_BLOCK_MAPPING_STYLE))
320 return -1;
321 if (!yaml_emitter_emit(emitter, &event))
322 return -1;
324 if (emit_string(emitter, "name") < 0)
325 return -1;
326 if (emit_string(emitter, type->name) < 0)
327 return -1;
329 if (emit_string(emitter, "definition") < 0)
330 return -1;
331 if (emit_string(emitter, type->definition) < 0)
332 return -1;
334 if (!yaml_mapping_end_event_initialize(&event))
335 return -1;
336 if (!yaml_emitter_emit(emitter, &event))
337 return -1;
339 return 0;
342 /* Print the list of "n_type" "types", if any, to "emitter".
344 static int emit_types(yaml_emitter_t *emitter, int n_type,
345 struct pet_type **types)
347 int i;
348 yaml_event_t event;
350 if (n_type == 0)
351 return 0;
353 if (emit_string(emitter, "types") < 0)
354 return -1;
355 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
356 YAML_BLOCK_SEQUENCE_STYLE))
357 return -1;
358 if (!yaml_emitter_emit(emitter, &event))
359 return -1;
361 for (i = 0; i < n_type; ++i)
362 if (emit_type(emitter, types[i]) < 0)
363 return -1;
365 if (!yaml_sequence_end_event_initialize(&event))
366 return -1;
367 if (!yaml_emitter_emit(emitter, &event))
368 return -1;
370 return 0;
373 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
375 yaml_event_t event;
377 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
378 YAML_BLOCK_MAPPING_STYLE))
379 return -1;
380 if (!yaml_emitter_emit(emitter, &event))
381 return -1;
383 if (emit_string(emitter, "context") < 0)
384 return -1;
385 if (emit_set(emitter, array->context) < 0)
386 return -1;
388 if (emit_string(emitter, "extent") < 0)
389 return -1;
390 if (emit_set(emitter, array->extent) < 0)
391 return -1;
393 if (array->value_bounds) {
394 if (emit_string(emitter, "value_bounds") < 0)
395 return -1;
396 if (emit_set(emitter, array->value_bounds) < 0)
397 return -1;
400 if (emit_string(emitter, "element_type") < 0)
401 return -1;
402 if (emit_string(emitter, array->element_type) < 0)
403 return -1;
404 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
405 return -1;
407 if (array->element_is_record)
408 if (emit_named_int(emitter, "element_is_record",
409 array->element_is_record) < 0)
410 return -1;
412 if (array->live_out) {
413 if (emit_string(emitter, "live_out") < 0)
414 return -1;
415 if (emit_string(emitter, "1") < 0)
416 return -1;
419 if (array->uniquely_defined) {
420 if (emit_string(emitter, "uniquely_defined") < 0)
421 return -1;
422 if (emit_string(emitter, "1") < 0)
423 return -1;
426 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
427 return -1;
428 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
429 return -1;
431 if (!yaml_mapping_end_event_initialize(&event))
432 return -1;
433 if (!yaml_emitter_emit(emitter, &event))
434 return -1;
436 return 0;
439 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
440 struct pet_array **arrays)
442 int i;
443 yaml_event_t event;
445 if (emit_string(emitter, "arrays") < 0)
446 return -1;
447 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
448 YAML_BLOCK_SEQUENCE_STYLE))
449 return -1;
450 if (!yaml_emitter_emit(emitter, &event))
451 return -1;
453 for (i = 0; i < n_array; ++i)
454 if (emit_array(emitter, arrays[i]) < 0)
455 return -1;
457 if (!yaml_sequence_end_event_initialize(&event))
458 return -1;
459 if (!yaml_emitter_emit(emitter, &event))
460 return -1;
462 return 0;
465 static int emit_expr_type(yaml_emitter_t *emitter, enum pet_expr_type type)
467 if (emit_string(emitter, pet_type_str(type)) < 0)
468 return -1;
469 return 0;
472 /* Print the fields of the access expression "expr" to "emitter".
474 * If the depth of the access is equal to the depth that can be derived from
475 * the index expression, then there is no need to print it.
477 static int emit_access_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
479 int depth;
481 if (expr->acc.access) {
482 if (emit_string(emitter, "relation") < 0)
483 return -1;
484 if (emit_map(emitter, expr->acc.access) < 0)
485 return -1;
487 if (emit_named_multi_pw_aff(emitter, "index", expr->acc.index) < 0)
488 return -1;
489 depth = isl_multi_pw_aff_dim(expr->acc.index, isl_dim_out);
490 if (expr->acc.depth != depth &&
491 emit_named_int(emitter, "depth", expr->acc.depth) < 0)
492 return -1;
493 if (expr->acc.ref_id &&
494 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
495 return -1;
496 if (expr->acc.kill) {
497 if (emit_named_unsigned(emitter, "kill", 1) < 0)
498 return -1;
499 } else {
500 if (emit_string(emitter, "read") < 0)
501 return -1;
502 if (emit_int(emitter, expr->acc.read) < 0)
503 return -1;
504 if (emit_string(emitter, "write") < 0)
505 return -1;
506 if (emit_int(emitter, expr->acc.write) < 0)
507 return -1;
510 return 0;
513 static int emit_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
515 yaml_event_t event;
517 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
518 YAML_BLOCK_MAPPING_STYLE))
519 return -1;
520 if (!yaml_emitter_emit(emitter, &event))
521 return -1;
523 if (emit_string(emitter, "type") < 0)
524 return -1;
525 if (emit_expr_type(emitter, expr->type) < 0)
526 return -1;
528 switch (expr->type) {
529 case pet_expr_error:
530 return -1;
531 case pet_expr_int:
532 if (emit_named_val(emitter, "value", expr->i) < 0)
533 return -1;
534 break;
535 case pet_expr_double:
536 if (emit_string(emitter, "value") < 0)
537 return -1;
538 if (emit_double(emitter, expr->d.val) < 0)
539 return -1;
540 if (emit_string(emitter, "string") < 0)
541 return -1;
542 if (emit_string(emitter, expr->d.s) < 0)
543 return -1;
544 break;
545 case pet_expr_access:
546 if (emit_access_expr(emitter, expr) < 0)
547 return -1;
548 break;
549 case pet_expr_op:
550 if (emit_string(emitter, "operation") < 0)
551 return -1;
552 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
553 return -1;
554 break;
555 case pet_expr_call:
556 if (emit_string(emitter, "name") < 0)
557 return -1;
558 if (emit_string(emitter, expr->name) < 0)
559 return -1;
560 break;
561 case pet_expr_cast:
562 if (emit_string(emitter, "type_name") < 0)
563 return -1;
564 if (emit_string(emitter, expr->type_name) < 0)
565 return -1;
566 break;
569 if (expr->n_arg > 0) {
570 int i;
572 if (emit_string(emitter, "arguments") < 0)
573 return -1;
574 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
575 YAML_BLOCK_SEQUENCE_STYLE))
576 return -1;
577 if (!yaml_emitter_emit(emitter, &event))
578 return -1;
580 for (i = 0; i < expr->n_arg; ++i)
581 if (emit_expr(emitter, expr->args[i]) < 0)
582 return -1;
584 if (!yaml_sequence_end_event_initialize(&event))
585 return -1;
586 if (!yaml_emitter_emit(emitter, &event))
587 return -1;
590 if (!yaml_mapping_end_event_initialize(&event))
591 return -1;
592 if (!yaml_emitter_emit(emitter, &event))
593 return -1;
595 return 0;
598 /* Print the string "name" and the expression "expr" to "emitter".
600 static int emit_named_expr(yaml_emitter_t *emitter, const char *name,
601 __isl_keep pet_expr *expr)
603 if (emit_string(emitter, name) < 0)
604 return -1;
605 if (emit_expr(emitter, expr) < 0)
606 return -1;
607 return 0;
610 /* Print "type" to "emitter".
612 static int emit_tree_type(yaml_emitter_t *emitter, enum pet_tree_type type)
614 if (emit_string(emitter, pet_tree_type_str(type)) < 0)
615 return -1;
616 return 0;
619 /* Recursively print "tree" to "emitter".
621 static int emit_tree(yaml_emitter_t *emitter, __isl_keep pet_tree *tree)
623 yaml_event_t event;
624 int i;
626 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
627 YAML_BLOCK_MAPPING_STYLE))
628 return -1;
629 if (!yaml_emitter_emit(emitter, &event))
630 return -1;
632 if (emit_string(emitter, "type") < 0)
633 return -1;
634 if (emit_tree_type(emitter, tree->type) < 0)
635 return -1;
637 switch (tree->type) {
638 case pet_tree_error:
639 return -1;
640 case pet_tree_block:
641 if (emit_named_int(emitter, "block", tree->u.b.block) < 0)
642 return -1;
643 if (tree->u.b.n == 0)
644 break;
646 if (emit_string(emitter, "children") < 0)
647 return -1;
648 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
649 YAML_BLOCK_SEQUENCE_STYLE))
650 return -1;
651 if (!yaml_emitter_emit(emitter, &event))
652 return -1;
654 for (i = 0; i < tree->u.b.n; ++i)
655 if (emit_tree(emitter, tree->u.b.child[i]) < 0)
656 return -1;
658 if (!yaml_sequence_end_event_initialize(&event))
659 return -1;
660 if (!yaml_emitter_emit(emitter, &event))
661 return -1;
662 break;
663 case pet_tree_break:
664 case pet_tree_continue:
665 break;
666 case pet_tree_decl:
667 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
668 return -1;
669 break;
670 case pet_tree_decl_init:
671 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
672 return -1;
673 if (emit_named_expr(emitter,
674 "initialization", tree->u.d.init) < 0)
675 return -1;
676 break;
677 case pet_tree_expr:
678 if (emit_named_expr(emitter, "expr", tree->u.e.expr) < 0)
679 return -1;
680 break;
681 case pet_tree_for:
682 if (tree->u.l.independent)
683 if (emit_named_int(emitter, "independent", 1) < 0)
684 return -1;
685 if (emit_named_int(emitter, "declared", tree->u.l.declared) < 0)
686 return -1;
687 if (emit_named_expr(emitter, "variable", tree->u.l.iv) < 0)
688 return -1;
689 if (emit_named_expr(emitter,
690 "initialization", tree->u.l.init) < 0)
691 return -1;
692 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
693 return -1;
694 if (emit_named_expr(emitter, "increment", tree->u.l.inc) < 0)
695 return -1;
696 if (emit_string(emitter, "body") < 0)
697 return -1;
698 if (emit_tree(emitter, tree->u.l.body) < 0)
699 return -1;
700 break;
701 case pet_tree_while:
702 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
703 return -1;
704 if (emit_string(emitter, "body") < 0)
705 return -1;
706 if (emit_tree(emitter, tree->u.l.body) < 0)
707 return -1;
708 break;
709 case pet_tree_infinite_loop:
710 if (emit_string(emitter, "body") < 0)
711 return -1;
712 if (emit_tree(emitter, tree->u.l.body) < 0)
713 return -1;
714 break;
715 case pet_tree_if:
716 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
717 return -1;
718 if (emit_string(emitter, "then") < 0)
719 return -1;
720 if (emit_tree(emitter, tree->u.i.then_body) < 0)
721 return -1;
722 break;
723 case pet_tree_if_else:
724 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
725 return -1;
726 if (emit_string(emitter, "then") < 0)
727 return -1;
728 if (emit_tree(emitter, tree->u.i.then_body) < 0)
729 return -1;
730 if (emit_string(emitter, "else") < 0)
731 return -1;
732 if (emit_tree(emitter, tree->u.i.else_body) < 0)
733 return -1;
734 break;
737 if (!yaml_mapping_end_event_initialize(&event))
738 return -1;
739 if (!yaml_emitter_emit(emitter, &event))
740 return -1;
742 return 0;
745 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
747 yaml_event_t event;
749 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
750 YAML_BLOCK_MAPPING_STYLE))
751 return -1;
752 if (!yaml_emitter_emit(emitter, &event))
753 return -1;
755 if (emit_string(emitter, "line") < 0)
756 return -1;
757 if (emit_int(emitter, pet_loc_get_line(stmt->loc)) < 0)
758 return -1;
760 if (emit_string(emitter, "domain") < 0)
761 return -1;
762 if (emit_set(emitter, stmt->domain) < 0)
763 return -1;
765 if (emit_string(emitter, "schedule") < 0)
766 return -1;
767 if (emit_map(emitter, stmt->schedule) < 0)
768 return -1;
770 if (emit_string(emitter, "body") < 0)
771 return -1;
772 if (emit_tree(emitter, stmt->body) < 0)
773 return -1;
775 if (stmt->n_arg > 0) {
776 int i;
778 if (emit_string(emitter, "arguments") < 0)
779 return -1;
780 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
781 YAML_BLOCK_SEQUENCE_STYLE))
782 return -1;
783 if (!yaml_emitter_emit(emitter, &event))
784 return -1;
786 for (i = 0; i < stmt->n_arg; ++i)
787 if (emit_expr(emitter, stmt->args[i]) < 0)
788 return -1;
790 if (!yaml_sequence_end_event_initialize(&event))
791 return -1;
792 if (!yaml_emitter_emit(emitter, &event))
793 return -1;
796 if (!yaml_mapping_end_event_initialize(&event))
797 return -1;
798 if (!yaml_emitter_emit(emitter, &event))
799 return -1;
801 return 0;
804 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
805 struct pet_stmt **stmts)
807 int i;
808 yaml_event_t event;
810 if (emit_string(emitter, "statements") < 0)
811 return -1;
812 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
813 YAML_BLOCK_SEQUENCE_STYLE))
814 return -1;
815 if (!yaml_emitter_emit(emitter, &event))
816 return -1;
818 for (i = 0; i < n_stmt; ++i)
819 if (emit_stmt(emitter, stmts[i]) < 0)
820 return -1;
822 if (!yaml_sequence_end_event_initialize(&event))
823 return -1;
824 if (!yaml_emitter_emit(emitter, &event))
825 return -1;
827 return 0;
830 /* Print "implication" to "emitter".
832 static int emit_implication(yaml_emitter_t *emitter,
833 struct pet_implication *implication)
835 yaml_event_t event;
837 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
838 YAML_BLOCK_MAPPING_STYLE))
839 return -1;
840 if (!yaml_emitter_emit(emitter, &event))
841 return -1;
843 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
844 return -1;
846 if (emit_named_map(emitter, "extension", implication->extension) < 0)
847 return -1;
849 if (!yaml_mapping_end_event_initialize(&event))
850 return -1;
851 if (!yaml_emitter_emit(emitter, &event))
852 return -1;
854 return 0;
857 /* Print the list of "n_implication" "implications", if any, to "emitter".
859 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
860 struct pet_implication **implications)
862 int i;
863 yaml_event_t event;
865 if (n_implication == 0)
866 return 0;
868 if (emit_string(emitter, "implications") < 0)
869 return -1;
870 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
871 YAML_BLOCK_SEQUENCE_STYLE))
872 return -1;
873 if (!yaml_emitter_emit(emitter, &event))
874 return -1;
876 for (i = 0; i < n_implication; ++i)
877 if (emit_implication(emitter, implications[i]) < 0)
878 return -1;
880 if (!yaml_sequence_end_event_initialize(&event))
881 return -1;
882 if (!yaml_emitter_emit(emitter, &event))
883 return -1;
885 return 0;
888 /* Print "independence" to "emitter".
890 static int emit_independence(yaml_emitter_t *emitter,
891 struct pet_independence *independence)
893 yaml_event_t event;
895 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
896 YAML_BLOCK_MAPPING_STYLE))
897 return -1;
898 if (!yaml_emitter_emit(emitter, &event))
899 return -1;
901 if (emit_named_union_map(emitter, "filter", independence->filter) < 0)
902 return -1;
904 if (emit_named_union_set(emitter, "local", independence->local) < 0)
905 return -1;
907 if (!yaml_mapping_end_event_initialize(&event))
908 return -1;
909 if (!yaml_emitter_emit(emitter, &event))
910 return -1;
912 return 0;
915 /* Print the list of "n_independence" "independences", if any, to "emitter".
917 static int emit_independences(yaml_emitter_t *emitter, int n_independence,
918 struct pet_independence **independences)
920 int i;
921 yaml_event_t event;
923 if (n_independence == 0)
924 return 0;
926 if (emit_string(emitter, "independences") < 0)
927 return -1;
928 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
929 YAML_BLOCK_SEQUENCE_STYLE))
930 return -1;
931 if (!yaml_emitter_emit(emitter, &event))
932 return -1;
934 for (i = 0; i < n_independence; ++i)
935 if (emit_independence(emitter, independences[i]) < 0)
936 return -1;
938 if (!yaml_sequence_end_event_initialize(&event))
939 return -1;
940 if (!yaml_emitter_emit(emitter, &event))
941 return -1;
943 return 0;
946 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
948 yaml_event_t event;
950 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
951 YAML_BLOCK_MAPPING_STYLE))
952 return -1;
953 if (!yaml_emitter_emit(emitter, &event))
954 return -1;
956 if (emit_named_unsigned(emitter,
957 "start", pet_loc_get_start(scop->loc)) < 0)
958 return -1;
959 if (emit_named_unsigned(emitter, "end", pet_loc_get_end(scop->loc)) < 0)
960 return -1;
961 if (emit_named_string(emitter,
962 "indent", pet_loc_get_indent(scop->loc)) < 0)
963 return -1;
964 if (emit_string(emitter, "context") < 0)
965 return -1;
966 if (emit_set(emitter, scop->context) < 0)
967 return -1;
968 if (!isl_set_plain_is_universe(scop->context_value) &&
969 emit_named_set(emitter, "context_value", scop->context_value) < 0)
970 return -1;
972 if (emit_types(emitter, scop->n_type, scop->types) < 0)
973 return -1;
974 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
975 return -1;
977 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
978 return -1;
980 if (emit_implications(emitter, scop->n_implication,
981 scop->implications) < 0)
982 return -1;
984 if (emit_independences(emitter, scop->n_independence,
985 scop->independences) < 0)
986 return -1;
988 if (!yaml_mapping_end_event_initialize(&event))
989 return -1;
990 if (!yaml_emitter_emit(emitter, &event))
991 return -1;
993 return 0;
996 /* Print a YAML serialization of "scop" to "out".
998 int pet_scop_emit(FILE *out, struct pet_scop *scop)
1000 yaml_emitter_t emitter;
1001 yaml_event_t event;
1003 yaml_emitter_initialize(&emitter);
1005 yaml_emitter_set_output_file(&emitter, out);
1007 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
1008 if (!yaml_emitter_emit(&emitter, &event))
1009 goto error;
1011 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
1012 goto error;
1013 if (!yaml_emitter_emit(&emitter, &event))
1014 goto error;
1016 if (emit_scop(&emitter, scop) < 0)
1017 goto error;
1019 if (!yaml_document_end_event_initialize(&event, 1))
1020 goto error;
1021 if (!yaml_emitter_emit(&emitter, &event))
1022 goto error;
1024 yaml_stream_end_event_initialize(&event);
1025 if (!yaml_emitter_emit(&emitter, &event))
1026 goto error;
1028 yaml_emitter_delete(&emitter);
1029 return 0;
1030 error:
1031 yaml_emitter_delete(&emitter);
1032 return -1;