scop.c: expr_collect_accesses: drop unused variable
[pet.git] / emit.c
blob2aa4bf909fa79c0c6cdc1d689b946165148c14f3
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;
472 if (!yaml_mapping_end_event_initialize(&event))
473 return -1;
474 if (!yaml_emitter_emit(emitter, &event))
475 return -1;
477 return 0;
480 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
481 struct pet_array **arrays)
483 int i;
484 yaml_event_t event;
486 if (emit_string(emitter, "arrays") < 0)
487 return -1;
488 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
489 YAML_BLOCK_SEQUENCE_STYLE))
490 return -1;
491 if (!yaml_emitter_emit(emitter, &event))
492 return -1;
494 for (i = 0; i < n_array; ++i)
495 if (emit_array(emitter, arrays[i]) < 0)
496 return -1;
498 if (!yaml_sequence_end_event_initialize(&event))
499 return -1;
500 if (!yaml_emitter_emit(emitter, &event))
501 return -1;
503 return 0;
506 static int emit_expr_type(yaml_emitter_t *emitter, enum pet_expr_type type)
508 if (emit_string(emitter, pet_type_str(type)) < 0)
509 return -1;
510 return 0;
513 /* Print the fields of the access expression "expr" to "emitter".
515 * Only print the access relations that are present and relevant
516 * for the type of access.
518 * If the depth of the access is equal to the depth that can be derived from
519 * the index expression, then there is no need to print it.
521 static int emit_access_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
523 int depth;
525 if (expr->acc.kill && expr->acc.access[pet_expr_access_fake_killed] &&
526 emit_named_union_map(emitter, "killed",
527 expr->acc.access[pet_expr_access_fake_killed]) < 0)
528 return -1;
529 if (expr->acc.read && expr->acc.access[pet_expr_access_may_read] &&
530 emit_named_union_map(emitter, "may_read",
531 expr->acc.access[pet_expr_access_may_read]) < 0)
532 return -1;
533 if (expr->acc.write && expr->acc.access[pet_expr_access_may_write] &&
534 emit_named_union_map(emitter, "may_write",
535 expr->acc.access[pet_expr_access_may_write]) < 0)
536 return -1;
537 if (expr->acc.write && expr->acc.access[pet_expr_access_must_write] &&
538 emit_named_union_map(emitter, "must_write",
539 expr->acc.access[pet_expr_access_must_write]) < 0)
540 return -1;
541 if (emit_named_multi_pw_aff(emitter, "index", expr->acc.index) < 0)
542 return -1;
543 depth = isl_multi_pw_aff_dim(expr->acc.index, isl_dim_out);
544 if (expr->acc.depth != depth &&
545 emit_named_int(emitter, "depth", expr->acc.depth) < 0)
546 return -1;
547 if (expr->acc.ref_id &&
548 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
549 return -1;
550 if (expr->acc.kill) {
551 if (emit_named_unsigned(emitter, "kill", 1) < 0)
552 return -1;
553 } else {
554 if (emit_string(emitter, "read") < 0)
555 return -1;
556 if (emit_int(emitter, expr->acc.read) < 0)
557 return -1;
558 if (emit_string(emitter, "write") < 0)
559 return -1;
560 if (emit_int(emitter, expr->acc.write) < 0)
561 return -1;
564 return 0;
567 static int emit_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
569 yaml_event_t event;
571 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
572 YAML_BLOCK_MAPPING_STYLE))
573 return -1;
574 if (!yaml_emitter_emit(emitter, &event))
575 return -1;
577 if (emit_string(emitter, "type") < 0)
578 return -1;
579 if (emit_expr_type(emitter, expr->type) < 0)
580 return -1;
582 switch (expr->type) {
583 case pet_expr_error:
584 return -1;
585 case pet_expr_int:
586 if (emit_named_val(emitter, "value", expr->i) < 0)
587 return -1;
588 break;
589 case pet_expr_double:
590 if (emit_string(emitter, "value") < 0)
591 return -1;
592 if (emit_double(emitter, expr->d.val) < 0)
593 return -1;
594 if (emit_string(emitter, "string") < 0)
595 return -1;
596 if (emit_string(emitter, expr->d.s) < 0)
597 return -1;
598 break;
599 case pet_expr_access:
600 if (emit_access_expr(emitter, expr) < 0)
601 return -1;
602 break;
603 case pet_expr_op:
604 if (emit_string(emitter, "operation") < 0)
605 return -1;
606 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
607 return -1;
608 break;
609 case pet_expr_call:
610 if (emit_string(emitter, "name") < 0)
611 return -1;
612 if (emit_string(emitter, expr->c.name) < 0)
613 return -1;
614 break;
615 case pet_expr_cast:
616 if (emit_string(emitter, "type_name") < 0)
617 return -1;
618 if (emit_string(emitter, expr->type_name) < 0)
619 return -1;
620 break;
623 if (expr->n_arg > 0) {
624 int i;
626 if (emit_string(emitter, "arguments") < 0)
627 return -1;
628 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
629 YAML_BLOCK_SEQUENCE_STYLE))
630 return -1;
631 if (!yaml_emitter_emit(emitter, &event))
632 return -1;
634 for (i = 0; i < expr->n_arg; ++i)
635 if (emit_expr(emitter, expr->args[i]) < 0)
636 return -1;
638 if (!yaml_sequence_end_event_initialize(&event))
639 return -1;
640 if (!yaml_emitter_emit(emitter, &event))
641 return -1;
644 if (!yaml_mapping_end_event_initialize(&event))
645 return -1;
646 if (!yaml_emitter_emit(emitter, &event))
647 return -1;
649 return 0;
652 /* Print the string "name" and the expression "expr" to "emitter".
654 static int emit_named_expr(yaml_emitter_t *emitter, const char *name,
655 __isl_keep pet_expr *expr)
657 if (emit_string(emitter, name) < 0)
658 return -1;
659 if (emit_expr(emitter, expr) < 0)
660 return -1;
661 return 0;
664 /* Print "type" to "emitter".
666 static int emit_tree_type(yaml_emitter_t *emitter, enum pet_tree_type type)
668 if (emit_string(emitter, pet_tree_type_str(type)) < 0)
669 return -1;
670 return 0;
673 /* Recursively print "tree" to "emitter".
675 static int emit_tree(yaml_emitter_t *emitter, __isl_keep pet_tree *tree)
677 yaml_event_t event;
678 int i;
680 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
681 YAML_BLOCK_MAPPING_STYLE))
682 return -1;
683 if (!yaml_emitter_emit(emitter, &event))
684 return -1;
686 if (emit_string(emitter, "type") < 0)
687 return -1;
688 if (emit_tree_type(emitter, tree->type) < 0)
689 return -1;
691 switch (tree->type) {
692 case pet_tree_error:
693 return -1;
694 case pet_tree_block:
695 if (emit_named_int(emitter, "block", tree->u.b.block) < 0)
696 return -1;
697 if (tree->u.b.n == 0)
698 break;
700 if (emit_string(emitter, "children") < 0)
701 return -1;
702 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
703 YAML_BLOCK_SEQUENCE_STYLE))
704 return -1;
705 if (!yaml_emitter_emit(emitter, &event))
706 return -1;
708 for (i = 0; i < tree->u.b.n; ++i)
709 if (emit_tree(emitter, tree->u.b.child[i]) < 0)
710 return -1;
712 if (!yaml_sequence_end_event_initialize(&event))
713 return -1;
714 if (!yaml_emitter_emit(emitter, &event))
715 return -1;
716 break;
717 case pet_tree_break:
718 case pet_tree_continue:
719 break;
720 case pet_tree_decl:
721 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
722 return -1;
723 break;
724 case pet_tree_decl_init:
725 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
726 return -1;
727 if (emit_named_expr(emitter,
728 "initialization", tree->u.d.init) < 0)
729 return -1;
730 break;
731 case pet_tree_expr:
732 if (emit_named_expr(emitter, "expr", tree->u.e.expr) < 0)
733 return -1;
734 break;
735 case pet_tree_for:
736 if (tree->u.l.independent)
737 if (emit_named_int(emitter, "independent", 1) < 0)
738 return -1;
739 if (emit_named_int(emitter, "declared", tree->u.l.declared) < 0)
740 return -1;
741 if (emit_named_expr(emitter, "variable", tree->u.l.iv) < 0)
742 return -1;
743 if (emit_named_expr(emitter,
744 "initialization", tree->u.l.init) < 0)
745 return -1;
746 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
747 return -1;
748 if (emit_named_expr(emitter, "increment", tree->u.l.inc) < 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_while:
756 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
757 return -1;
758 if (emit_string(emitter, "body") < 0)
759 return -1;
760 if (emit_tree(emitter, tree->u.l.body) < 0)
761 return -1;
762 break;
763 case pet_tree_infinite_loop:
764 if (emit_string(emitter, "body") < 0)
765 return -1;
766 if (emit_tree(emitter, tree->u.l.body) < 0)
767 return -1;
768 break;
769 case pet_tree_if:
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 break;
777 case pet_tree_if_else:
778 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
779 return -1;
780 if (emit_string(emitter, "then") < 0)
781 return -1;
782 if (emit_tree(emitter, tree->u.i.then_body) < 0)
783 return -1;
784 if (emit_string(emitter, "else") < 0)
785 return -1;
786 if (emit_tree(emitter, tree->u.i.else_body) < 0)
787 return -1;
788 break;
791 if (!yaml_mapping_end_event_initialize(&event))
792 return -1;
793 if (!yaml_emitter_emit(emitter, &event))
794 return -1;
796 return 0;
799 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
801 yaml_event_t event;
803 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
804 YAML_BLOCK_MAPPING_STYLE))
805 return -1;
806 if (!yaml_emitter_emit(emitter, &event))
807 return -1;
809 if (emit_string(emitter, "line") < 0)
810 return -1;
811 if (emit_int(emitter, pet_loc_get_line(stmt->loc)) < 0)
812 return -1;
814 if (emit_string(emitter, "domain") < 0)
815 return -1;
816 if (emit_set(emitter, stmt->domain) < 0)
817 return -1;
819 if (emit_string(emitter, "body") < 0)
820 return -1;
821 if (emit_tree(emitter, stmt->body) < 0)
822 return -1;
824 if (stmt->n_arg > 0) {
825 int i;
827 if (emit_string(emitter, "arguments") < 0)
828 return -1;
829 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
830 YAML_BLOCK_SEQUENCE_STYLE))
831 return -1;
832 if (!yaml_emitter_emit(emitter, &event))
833 return -1;
835 for (i = 0; i < stmt->n_arg; ++i)
836 if (emit_expr(emitter, stmt->args[i]) < 0)
837 return -1;
839 if (!yaml_sequence_end_event_initialize(&event))
840 return -1;
841 if (!yaml_emitter_emit(emitter, &event))
842 return -1;
845 if (!yaml_mapping_end_event_initialize(&event))
846 return -1;
847 if (!yaml_emitter_emit(emitter, &event))
848 return -1;
850 return 0;
853 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
854 struct pet_stmt **stmts)
856 int i;
857 yaml_event_t event;
859 if (emit_string(emitter, "statements") < 0)
860 return -1;
861 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
862 YAML_BLOCK_SEQUENCE_STYLE))
863 return -1;
864 if (!yaml_emitter_emit(emitter, &event))
865 return -1;
867 for (i = 0; i < n_stmt; ++i)
868 if (emit_stmt(emitter, stmts[i]) < 0)
869 return -1;
871 if (!yaml_sequence_end_event_initialize(&event))
872 return -1;
873 if (!yaml_emitter_emit(emitter, &event))
874 return -1;
876 return 0;
879 /* Print "implication" to "emitter".
881 static int emit_implication(yaml_emitter_t *emitter,
882 struct pet_implication *implication)
884 yaml_event_t event;
886 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
887 YAML_BLOCK_MAPPING_STYLE))
888 return -1;
889 if (!yaml_emitter_emit(emitter, &event))
890 return -1;
892 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
893 return -1;
895 if (emit_named_map(emitter, "extension", implication->extension) < 0)
896 return -1;
898 if (!yaml_mapping_end_event_initialize(&event))
899 return -1;
900 if (!yaml_emitter_emit(emitter, &event))
901 return -1;
903 return 0;
906 /* Print the list of "n_implication" "implications", if any, to "emitter".
908 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
909 struct pet_implication **implications)
911 int i;
912 yaml_event_t event;
914 if (n_implication == 0)
915 return 0;
917 if (emit_string(emitter, "implications") < 0)
918 return -1;
919 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
920 YAML_BLOCK_SEQUENCE_STYLE))
921 return -1;
922 if (!yaml_emitter_emit(emitter, &event))
923 return -1;
925 for (i = 0; i < n_implication; ++i)
926 if (emit_implication(emitter, implications[i]) < 0)
927 return -1;
929 if (!yaml_sequence_end_event_initialize(&event))
930 return -1;
931 if (!yaml_emitter_emit(emitter, &event))
932 return -1;
934 return 0;
937 /* Print "independence" to "emitter".
939 static int emit_independence(yaml_emitter_t *emitter,
940 struct pet_independence *independence)
942 yaml_event_t event;
944 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
945 YAML_BLOCK_MAPPING_STYLE))
946 return -1;
947 if (!yaml_emitter_emit(emitter, &event))
948 return -1;
950 if (emit_named_union_map(emitter, "filter", independence->filter) < 0)
951 return -1;
953 if (emit_named_union_set(emitter, "local", independence->local) < 0)
954 return -1;
956 if (!yaml_mapping_end_event_initialize(&event))
957 return -1;
958 if (!yaml_emitter_emit(emitter, &event))
959 return -1;
961 return 0;
964 /* Print the list of "n_independence" "independences", if any, to "emitter".
966 static int emit_independences(yaml_emitter_t *emitter, int n_independence,
967 struct pet_independence **independences)
969 int i;
970 yaml_event_t event;
972 if (n_independence == 0)
973 return 0;
975 if (emit_string(emitter, "independences") < 0)
976 return -1;
977 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
978 YAML_BLOCK_SEQUENCE_STYLE))
979 return -1;
980 if (!yaml_emitter_emit(emitter, &event))
981 return -1;
983 for (i = 0; i < n_independence; ++i)
984 if (emit_independence(emitter, independences[i]) < 0)
985 return -1;
987 if (!yaml_sequence_end_event_initialize(&event))
988 return -1;
989 if (!yaml_emitter_emit(emitter, &event))
990 return -1;
992 return 0;
995 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
997 yaml_event_t event;
999 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
1000 YAML_BLOCK_MAPPING_STYLE))
1001 return -1;
1002 if (!yaml_emitter_emit(emitter, &event))
1003 return -1;
1005 if (emit_named_unsigned(emitter,
1006 "start", pet_loc_get_start(scop->loc)) < 0)
1007 return -1;
1008 if (emit_named_unsigned(emitter, "end", pet_loc_get_end(scop->loc)) < 0)
1009 return -1;
1010 if (emit_named_string(emitter,
1011 "indent", pet_loc_get_indent(scop->loc)) < 0)
1012 return -1;
1013 if (emit_string(emitter, "context") < 0)
1014 return -1;
1015 if (emit_set(emitter, scop->context) < 0)
1016 return -1;
1017 if (!isl_set_plain_is_universe(scop->context_value) &&
1018 emit_named_set(emitter, "context_value", scop->context_value) < 0)
1019 return -1;
1020 if (emit_named_schedule(emitter, "schedule", scop->schedule) < 0)
1021 return -1;
1023 if (emit_types(emitter, scop->n_type, scop->types) < 0)
1024 return -1;
1025 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
1026 return -1;
1028 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
1029 return -1;
1031 if (emit_implications(emitter, scop->n_implication,
1032 scop->implications) < 0)
1033 return -1;
1035 if (emit_independences(emitter, scop->n_independence,
1036 scop->independences) < 0)
1037 return -1;
1039 if (!yaml_mapping_end_event_initialize(&event))
1040 return -1;
1041 if (!yaml_emitter_emit(emitter, &event))
1042 return -1;
1044 return 0;
1047 /* Print a YAML serialization of "scop" to "out".
1049 int pet_scop_emit(FILE *out, struct pet_scop *scop)
1051 yaml_emitter_t emitter;
1052 yaml_event_t event;
1054 yaml_emitter_initialize(&emitter);
1056 yaml_emitter_set_output_file(&emitter, out);
1058 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
1059 if (!yaml_emitter_emit(&emitter, &event))
1060 goto error;
1062 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
1063 goto error;
1064 if (!yaml_emitter_emit(&emitter, &event))
1065 goto error;
1067 if (emit_scop(&emitter, scop) < 0)
1068 goto error;
1070 if (!yaml_document_end_event_initialize(&event, 1))
1071 goto error;
1072 if (!yaml_emitter_emit(&emitter, &event))
1073 goto error;
1075 yaml_stream_end_event_initialize(&event);
1076 if (!yaml_emitter_emit(&emitter, &event))
1077 goto error;
1079 yaml_emitter_delete(&emitter);
1080 return 0;
1081 error:
1082 yaml_emitter_delete(&emitter);
1083 return -1;