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