emit.c: emit_expr: extract out emit_access_expr
[pet.git] / emit.c
blob5b76aba89be7203b2f06933b9466ee0cbca56e8c
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 static int emit_access_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
476 if (emit_string(emitter, "relation") < 0)
477 return -1;
478 if (emit_map(emitter, expr->acc.access) < 0)
479 return -1;
480 if (emit_named_multi_pw_aff(emitter, "index", expr->acc.index) < 0)
481 return -1;
482 if (expr->acc.ref_id &&
483 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
484 return -1;
485 if (emit_string(emitter, "read") < 0)
486 return -1;
487 if (emit_int(emitter, expr->acc.read) < 0)
488 return -1;
489 if (emit_string(emitter, "write") < 0)
490 return -1;
491 if (emit_int(emitter, expr->acc.write) < 0)
492 return -1;
494 return 0;
497 static int emit_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
499 yaml_event_t event;
501 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
502 YAML_BLOCK_MAPPING_STYLE))
503 return -1;
504 if (!yaml_emitter_emit(emitter, &event))
505 return -1;
507 if (emit_string(emitter, "type") < 0)
508 return -1;
509 if (emit_expr_type(emitter, expr->type) < 0)
510 return -1;
512 switch (expr->type) {
513 case pet_expr_error:
514 return -1;
515 case pet_expr_int:
516 if (emit_named_val(emitter, "value", expr->i) < 0)
517 return -1;
518 break;
519 case pet_expr_double:
520 if (emit_string(emitter, "value") < 0)
521 return -1;
522 if (emit_double(emitter, expr->d.val) < 0)
523 return -1;
524 if (emit_string(emitter, "string") < 0)
525 return -1;
526 if (emit_string(emitter, expr->d.s) < 0)
527 return -1;
528 break;
529 case pet_expr_access:
530 if (emit_access_expr(emitter, expr) < 0)
531 return -1;
532 break;
533 case pet_expr_op:
534 if (emit_string(emitter, "operation") < 0)
535 return -1;
536 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
537 return -1;
538 break;
539 case pet_expr_call:
540 if (emit_string(emitter, "name") < 0)
541 return -1;
542 if (emit_string(emitter, expr->name) < 0)
543 return -1;
544 break;
545 case pet_expr_cast:
546 if (emit_string(emitter, "type_name") < 0)
547 return -1;
548 if (emit_string(emitter, expr->type_name) < 0)
549 return -1;
550 break;
553 if (expr->n_arg > 0) {
554 int i;
556 if (emit_string(emitter, "arguments") < 0)
557 return -1;
558 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
559 YAML_BLOCK_SEQUENCE_STYLE))
560 return -1;
561 if (!yaml_emitter_emit(emitter, &event))
562 return -1;
564 for (i = 0; i < expr->n_arg; ++i)
565 if (emit_expr(emitter, expr->args[i]) < 0)
566 return -1;
568 if (!yaml_sequence_end_event_initialize(&event))
569 return -1;
570 if (!yaml_emitter_emit(emitter, &event))
571 return -1;
574 if (!yaml_mapping_end_event_initialize(&event))
575 return -1;
576 if (!yaml_emitter_emit(emitter, &event))
577 return -1;
579 return 0;
582 /* Print the string "name" and the expression "expr" to "emitter".
584 static int emit_named_expr(yaml_emitter_t *emitter, const char *name,
585 __isl_keep pet_expr *expr)
587 if (emit_string(emitter, name) < 0)
588 return -1;
589 if (emit_expr(emitter, expr) < 0)
590 return -1;
591 return 0;
594 /* Print "type" to "emitter".
596 static int emit_tree_type(yaml_emitter_t *emitter, enum pet_tree_type type)
598 if (emit_string(emitter, pet_tree_type_str(type)) < 0)
599 return -1;
600 return 0;
603 /* Recursively print "tree" to "emitter".
605 static int emit_tree(yaml_emitter_t *emitter, __isl_keep pet_tree *tree)
607 yaml_event_t event;
608 int i;
610 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
611 YAML_BLOCK_MAPPING_STYLE))
612 return -1;
613 if (!yaml_emitter_emit(emitter, &event))
614 return -1;
616 if (emit_string(emitter, "type") < 0)
617 return -1;
618 if (emit_tree_type(emitter, tree->type) < 0)
619 return -1;
621 switch (tree->type) {
622 case pet_tree_error:
623 return -1;
624 case pet_tree_block:
625 if (emit_named_int(emitter, "block", tree->u.b.block) < 0)
626 return -1;
627 if (tree->u.b.n == 0)
628 break;
630 if (emit_string(emitter, "children") < 0)
631 return -1;
632 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
633 YAML_BLOCK_SEQUENCE_STYLE))
634 return -1;
635 if (!yaml_emitter_emit(emitter, &event))
636 return -1;
638 for (i = 0; i < tree->u.b.n; ++i)
639 if (emit_tree(emitter, tree->u.b.child[i]) < 0)
640 return -1;
642 if (!yaml_sequence_end_event_initialize(&event))
643 return -1;
644 if (!yaml_emitter_emit(emitter, &event))
645 return -1;
646 break;
647 case pet_tree_break:
648 case pet_tree_continue:
649 break;
650 case pet_tree_decl:
651 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
652 return -1;
653 break;
654 case pet_tree_decl_init:
655 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
656 return -1;
657 if (emit_named_expr(emitter,
658 "initialization", tree->u.d.init) < 0)
659 return -1;
660 break;
661 case pet_tree_expr:
662 if (emit_named_expr(emitter, "expr", tree->u.e.expr) < 0)
663 return -1;
664 break;
665 case pet_tree_for:
666 if (tree->u.l.independent)
667 if (emit_named_int(emitter, "independent", 1) < 0)
668 return -1;
669 if (emit_named_int(emitter, "declared", tree->u.l.declared) < 0)
670 return -1;
671 if (emit_named_expr(emitter, "variable", tree->u.l.iv) < 0)
672 return -1;
673 if (emit_named_expr(emitter,
674 "initialization", tree->u.l.init) < 0)
675 return -1;
676 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
677 return -1;
678 if (emit_named_expr(emitter, "increment", tree->u.l.inc) < 0)
679 return -1;
680 if (emit_string(emitter, "body") < 0)
681 return -1;
682 if (emit_tree(emitter, tree->u.l.body) < 0)
683 return -1;
684 break;
685 case pet_tree_while:
686 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
687 return -1;
688 if (emit_string(emitter, "body") < 0)
689 return -1;
690 if (emit_tree(emitter, tree->u.l.body) < 0)
691 return -1;
692 break;
693 case pet_tree_infinite_loop:
694 if (emit_string(emitter, "body") < 0)
695 return -1;
696 if (emit_tree(emitter, tree->u.l.body) < 0)
697 return -1;
698 break;
699 case pet_tree_if:
700 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
701 return -1;
702 if (emit_string(emitter, "then") < 0)
703 return -1;
704 if (emit_tree(emitter, tree->u.i.then_body) < 0)
705 return -1;
706 break;
707 case pet_tree_if_else:
708 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
709 return -1;
710 if (emit_string(emitter, "then") < 0)
711 return -1;
712 if (emit_tree(emitter, tree->u.i.then_body) < 0)
713 return -1;
714 if (emit_string(emitter, "else") < 0)
715 return -1;
716 if (emit_tree(emitter, tree->u.i.else_body) < 0)
717 return -1;
718 break;
721 if (!yaml_mapping_end_event_initialize(&event))
722 return -1;
723 if (!yaml_emitter_emit(emitter, &event))
724 return -1;
726 return 0;
729 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
731 yaml_event_t event;
733 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
734 YAML_BLOCK_MAPPING_STYLE))
735 return -1;
736 if (!yaml_emitter_emit(emitter, &event))
737 return -1;
739 if (emit_string(emitter, "line") < 0)
740 return -1;
741 if (emit_int(emitter, pet_loc_get_line(stmt->loc)) < 0)
742 return -1;
744 if (emit_string(emitter, "domain") < 0)
745 return -1;
746 if (emit_set(emitter, stmt->domain) < 0)
747 return -1;
749 if (emit_string(emitter, "schedule") < 0)
750 return -1;
751 if (emit_map(emitter, stmt->schedule) < 0)
752 return -1;
754 if (emit_string(emitter, "body") < 0)
755 return -1;
756 if (emit_tree(emitter, stmt->body) < 0)
757 return -1;
759 if (stmt->n_arg > 0) {
760 int i;
762 if (emit_string(emitter, "arguments") < 0)
763 return -1;
764 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
765 YAML_BLOCK_SEQUENCE_STYLE))
766 return -1;
767 if (!yaml_emitter_emit(emitter, &event))
768 return -1;
770 for (i = 0; i < stmt->n_arg; ++i)
771 if (emit_expr(emitter, stmt->args[i]) < 0)
772 return -1;
774 if (!yaml_sequence_end_event_initialize(&event))
775 return -1;
776 if (!yaml_emitter_emit(emitter, &event))
777 return -1;
780 if (!yaml_mapping_end_event_initialize(&event))
781 return -1;
782 if (!yaml_emitter_emit(emitter, &event))
783 return -1;
785 return 0;
788 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
789 struct pet_stmt **stmts)
791 int i;
792 yaml_event_t event;
794 if (emit_string(emitter, "statements") < 0)
795 return -1;
796 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
797 YAML_BLOCK_SEQUENCE_STYLE))
798 return -1;
799 if (!yaml_emitter_emit(emitter, &event))
800 return -1;
802 for (i = 0; i < n_stmt; ++i)
803 if (emit_stmt(emitter, stmts[i]) < 0)
804 return -1;
806 if (!yaml_sequence_end_event_initialize(&event))
807 return -1;
808 if (!yaml_emitter_emit(emitter, &event))
809 return -1;
811 return 0;
814 /* Print "implication" to "emitter".
816 static int emit_implication(yaml_emitter_t *emitter,
817 struct pet_implication *implication)
819 yaml_event_t event;
821 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
822 YAML_BLOCK_MAPPING_STYLE))
823 return -1;
824 if (!yaml_emitter_emit(emitter, &event))
825 return -1;
827 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
828 return -1;
830 if (emit_named_map(emitter, "extension", implication->extension) < 0)
831 return -1;
833 if (!yaml_mapping_end_event_initialize(&event))
834 return -1;
835 if (!yaml_emitter_emit(emitter, &event))
836 return -1;
838 return 0;
841 /* Print the list of "n_implication" "implications", if any, to "emitter".
843 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
844 struct pet_implication **implications)
846 int i;
847 yaml_event_t event;
849 if (n_implication == 0)
850 return 0;
852 if (emit_string(emitter, "implications") < 0)
853 return -1;
854 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
855 YAML_BLOCK_SEQUENCE_STYLE))
856 return -1;
857 if (!yaml_emitter_emit(emitter, &event))
858 return -1;
860 for (i = 0; i < n_implication; ++i)
861 if (emit_implication(emitter, implications[i]) < 0)
862 return -1;
864 if (!yaml_sequence_end_event_initialize(&event))
865 return -1;
866 if (!yaml_emitter_emit(emitter, &event))
867 return -1;
869 return 0;
872 /* Print "independence" to "emitter".
874 static int emit_independence(yaml_emitter_t *emitter,
875 struct pet_independence *independence)
877 yaml_event_t event;
879 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
880 YAML_BLOCK_MAPPING_STYLE))
881 return -1;
882 if (!yaml_emitter_emit(emitter, &event))
883 return -1;
885 if (emit_named_union_map(emitter, "filter", independence->filter) < 0)
886 return -1;
888 if (emit_named_union_set(emitter, "local", independence->local) < 0)
889 return -1;
891 if (!yaml_mapping_end_event_initialize(&event))
892 return -1;
893 if (!yaml_emitter_emit(emitter, &event))
894 return -1;
896 return 0;
899 /* Print the list of "n_independence" "independences", if any, to "emitter".
901 static int emit_independences(yaml_emitter_t *emitter, int n_independence,
902 struct pet_independence **independences)
904 int i;
905 yaml_event_t event;
907 if (n_independence == 0)
908 return 0;
910 if (emit_string(emitter, "independences") < 0)
911 return -1;
912 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
913 YAML_BLOCK_SEQUENCE_STYLE))
914 return -1;
915 if (!yaml_emitter_emit(emitter, &event))
916 return -1;
918 for (i = 0; i < n_independence; ++i)
919 if (emit_independence(emitter, independences[i]) < 0)
920 return -1;
922 if (!yaml_sequence_end_event_initialize(&event))
923 return -1;
924 if (!yaml_emitter_emit(emitter, &event))
925 return -1;
927 return 0;
930 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
932 yaml_event_t event;
934 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
935 YAML_BLOCK_MAPPING_STYLE))
936 return -1;
937 if (!yaml_emitter_emit(emitter, &event))
938 return -1;
940 if (emit_named_unsigned(emitter,
941 "start", pet_loc_get_start(scop->loc)) < 0)
942 return -1;
943 if (emit_named_unsigned(emitter, "end", pet_loc_get_end(scop->loc)) < 0)
944 return -1;
945 if (emit_named_string(emitter,
946 "indent", pet_loc_get_indent(scop->loc)) < 0)
947 return -1;
948 if (emit_string(emitter, "context") < 0)
949 return -1;
950 if (emit_set(emitter, scop->context) < 0)
951 return -1;
952 if (!isl_set_plain_is_universe(scop->context_value) &&
953 emit_named_set(emitter, "context_value", scop->context_value) < 0)
954 return -1;
956 if (emit_types(emitter, scop->n_type, scop->types) < 0)
957 return -1;
958 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
959 return -1;
961 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
962 return -1;
964 if (emit_implications(emitter, scop->n_implication,
965 scop->implications) < 0)
966 return -1;
968 if (emit_independences(emitter, scop->n_independence,
969 scop->independences) < 0)
970 return -1;
972 if (!yaml_mapping_end_event_initialize(&event))
973 return -1;
974 if (!yaml_emitter_emit(emitter, &event))
975 return -1;
977 return 0;
980 /* Print a YAML serialization of "scop" to "out".
982 int pet_scop_emit(FILE *out, struct pet_scop *scop)
984 yaml_emitter_t emitter;
985 yaml_event_t event;
987 yaml_emitter_initialize(&emitter);
989 yaml_emitter_set_output_file(&emitter, out);
991 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
992 if (!yaml_emitter_emit(&emitter, &event))
993 goto error;
995 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
996 goto error;
997 if (!yaml_emitter_emit(&emitter, &event))
998 goto error;
1000 if (emit_scop(&emitter, scop) < 0)
1001 goto error;
1003 if (!yaml_document_end_event_initialize(&event, 1))
1004 goto error;
1005 if (!yaml_emitter_emit(&emitter, &event))
1006 goto error;
1008 yaml_stream_end_event_initialize(&event);
1009 if (!yaml_emitter_emit(&emitter, &event))
1010 goto error;
1012 yaml_emitter_delete(&emitter);
1013 return 0;
1014 error:
1015 yaml_emitter_delete(&emitter);
1016 return -1;