2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
40 #include "scop_yaml.h"
43 static int emit_string(yaml_emitter_t
*emitter
, const char *str
)
47 if (!yaml_scalar_event_initialize(&event
, NULL
, NULL
,
48 (yaml_char_t
*) str
, strlen(str
),
49 1, 1, YAML_PLAIN_SCALAR_STYLE
))
51 if (!yaml_emitter_emit(emitter
, &event
))
57 /* Print the string "name" and the string "str" to "emitter".
59 static int emit_named_string(yaml_emitter_t
*emitter
, const char *name
,
62 if (emit_string(emitter
, name
) < 0)
64 if (emit_string(emitter
, str
) < 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)
83 if (emit_id(emitter
, id
) < 0)
88 static int emit_int(yaml_emitter_t
*emitter
, int i
)
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)
100 if (emit_int(emitter
, i
) < 0)
105 /* Print the unsigned integer "u" to "emitter".
107 static int emit_unsigned(yaml_emitter_t
*emitter
, unsigned u
)
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
,
120 if (emit_string(emitter
, name
) < 0)
122 if (emit_int(emitter
, u
) < 0)
127 static int emit_double(yaml_emitter_t
*emitter
, double d
)
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
);
142 p
= isl_printer_to_str(ctx
);
143 p
= isl_printer_print_map(p
, map
);
144 str
= isl_printer_get_str(p
);
146 r
= emit_string(emitter
, str
);
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
);
160 p
= isl_printer_to_str(ctx
);
161 p
= isl_printer_print_val(p
, val
);
162 str
= isl_printer_get_str(p
);
164 r
= emit_string(emitter
, str
);
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)
176 if (emit_val(emitter
, val
) < 0)
181 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
183 isl_ctx
*ctx
= isl_set_get_ctx(set
);
188 p
= isl_printer_to_str(ctx
);
189 p
= isl_printer_print_set(p
, set
);
190 str
= isl_printer_get_str(p
);
192 r
= emit_string(emitter
, str
);
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)
202 if (emit_set(emitter
, set
) < 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)
214 if (emit_map(emitter
, map
) < 0)
219 /* Print the isl_multi_pw_aff "mpa" to "emitter".
221 static int emit_multi_pw_aff(yaml_emitter_t
*emitter
,
222 __isl_keep isl_multi_pw_aff
*mpa
)
224 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(mpa
);
229 p
= isl_printer_to_str(ctx
);
230 p
= isl_printer_print_multi_pw_aff(p
, mpa
);
231 str
= isl_printer_get_str(p
);
233 r
= emit_string(emitter
, str
);
238 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
240 static int emit_named_multi_pw_aff(yaml_emitter_t
*emitter
, const char *name
,
241 __isl_keep isl_multi_pw_aff
*mpa
)
243 if (emit_string(emitter
, name
) < 0)
245 if (emit_multi_pw_aff(emitter
, mpa
) < 0)
250 /* Print "type" to "emitter".
252 static int emit_type(yaml_emitter_t
*emitter
, struct pet_type
*type
)
256 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
257 YAML_BLOCK_MAPPING_STYLE
))
259 if (!yaml_emitter_emit(emitter
, &event
))
262 if (emit_string(emitter
, "name") < 0)
264 if (emit_string(emitter
, type
->name
) < 0)
267 if (emit_string(emitter
, "definition") < 0)
269 if (emit_string(emitter
, type
->definition
) < 0)
272 if (!yaml_mapping_end_event_initialize(&event
))
274 if (!yaml_emitter_emit(emitter
, &event
))
280 /* Print the list of "n_type" "types", if any, to "emitter".
282 static int emit_types(yaml_emitter_t
*emitter
, int n_type
,
283 struct pet_type
**types
)
291 if (emit_string(emitter
, "types") < 0)
293 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
294 YAML_BLOCK_SEQUENCE_STYLE
))
296 if (!yaml_emitter_emit(emitter
, &event
))
299 for (i
= 0; i
< n_type
; ++i
)
300 if (emit_type(emitter
, types
[i
]) < 0)
303 if (!yaml_sequence_end_event_initialize(&event
))
305 if (!yaml_emitter_emit(emitter
, &event
))
311 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
315 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
316 YAML_BLOCK_MAPPING_STYLE
))
318 if (!yaml_emitter_emit(emitter
, &event
))
321 if (emit_string(emitter
, "context") < 0)
323 if (emit_set(emitter
, array
->context
) < 0)
326 if (emit_string(emitter
, "extent") < 0)
328 if (emit_set(emitter
, array
->extent
) < 0)
331 if (array
->value_bounds
) {
332 if (emit_string(emitter
, "value_bounds") < 0)
334 if (emit_set(emitter
, array
->value_bounds
) < 0)
338 if (emit_string(emitter
, "element_type") < 0)
340 if (emit_string(emitter
, array
->element_type
) < 0)
342 if (emit_named_int(emitter
, "element_size", array
->element_size
) < 0)
345 if (array
->element_is_record
)
346 if (emit_named_int(emitter
, "element_is_record",
347 array
->element_is_record
) < 0)
350 if (array
->live_out
) {
351 if (emit_string(emitter
, "live_out") < 0)
353 if (emit_string(emitter
, "1") < 0)
357 if (array
->uniquely_defined
) {
358 if (emit_string(emitter
, "uniquely_defined") < 0)
360 if (emit_string(emitter
, "1") < 0)
364 if (array
->declared
&& emit_named_int(emitter
, "declared", 1) < 0)
366 if (array
->exposed
&& emit_named_int(emitter
, "exposed", 1) < 0)
369 if (!yaml_mapping_end_event_initialize(&event
))
371 if (!yaml_emitter_emit(emitter
, &event
))
377 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
378 struct pet_array
**arrays
)
383 if (emit_string(emitter
, "arrays") < 0)
385 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
386 YAML_BLOCK_SEQUENCE_STYLE
))
388 if (!yaml_emitter_emit(emitter
, &event
))
391 for (i
= 0; i
< n_array
; ++i
)
392 if (emit_array(emitter
, arrays
[i
]) < 0)
395 if (!yaml_sequence_end_event_initialize(&event
))
397 if (!yaml_emitter_emit(emitter
, &event
))
403 static int emit_expr_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
405 if (emit_string(emitter
, pet_type_str(type
)) < 0)
410 static int emit_expr(yaml_emitter_t
*emitter
, __isl_keep pet_expr
*expr
)
414 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
415 YAML_BLOCK_MAPPING_STYLE
))
417 if (!yaml_emitter_emit(emitter
, &event
))
420 if (emit_string(emitter
, "type") < 0)
422 if (emit_expr_type(emitter
, expr
->type
) < 0)
425 switch (expr
->type
) {
429 if (emit_named_val(emitter
, "value", expr
->i
) < 0)
432 case pet_expr_double
:
433 if (emit_string(emitter
, "value") < 0)
435 if (emit_double(emitter
, expr
->d
.val
) < 0)
437 if (emit_string(emitter
, "string") < 0)
439 if (emit_string(emitter
, expr
->d
.s
) < 0)
442 case pet_expr_access
:
443 if (emit_string(emitter
, "relation") < 0)
445 if (emit_map(emitter
, expr
->acc
.access
) < 0)
447 if (emit_named_multi_pw_aff(emitter
,
448 "index", expr
->acc
.index
) < 0)
450 if (expr
->acc
.ref_id
&&
451 emit_named_id(emitter
, "reference", expr
->acc
.ref_id
) < 0)
453 if (emit_string(emitter
, "read") < 0)
455 if (emit_int(emitter
, expr
->acc
.read
) < 0)
457 if (emit_string(emitter
, "write") < 0)
459 if (emit_int(emitter
, expr
->acc
.write
) < 0)
463 if (emit_string(emitter
, "operation") < 0)
465 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
469 if (emit_string(emitter
, "name") < 0)
471 if (emit_string(emitter
, expr
->name
) < 0)
475 if (emit_string(emitter
, "type_name") < 0)
477 if (emit_string(emitter
, expr
->type_name
) < 0)
482 if (expr
->n_arg
> 0) {
485 if (emit_string(emitter
, "arguments") < 0)
487 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
488 YAML_BLOCK_SEQUENCE_STYLE
))
490 if (!yaml_emitter_emit(emitter
, &event
))
493 for (i
= 0; i
< expr
->n_arg
; ++i
)
494 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
497 if (!yaml_sequence_end_event_initialize(&event
))
499 if (!yaml_emitter_emit(emitter
, &event
))
503 if (!yaml_mapping_end_event_initialize(&event
))
505 if (!yaml_emitter_emit(emitter
, &event
))
511 /* Print the string "name" and the expression "expr" to "emitter".
513 static int emit_named_expr(yaml_emitter_t
*emitter
, const char *name
,
514 __isl_keep pet_expr
*expr
)
516 if (emit_string(emitter
, name
) < 0)
518 if (emit_expr(emitter
, expr
) < 0)
523 /* Print "type" to "emitter".
525 static int emit_tree_type(yaml_emitter_t
*emitter
, enum pet_tree_type type
)
527 if (emit_string(emitter
, pet_tree_type_str(type
)) < 0)
532 /* Recursively print "tree" to "emitter".
534 static int emit_tree(yaml_emitter_t
*emitter
, __isl_keep pet_tree
*tree
)
539 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
540 YAML_BLOCK_MAPPING_STYLE
))
542 if (!yaml_emitter_emit(emitter
, &event
))
545 if (emit_string(emitter
, "type") < 0)
547 if (emit_tree_type(emitter
, tree
->type
) < 0)
550 switch (tree
->type
) {
554 if (emit_named_int(emitter
, "block", tree
->u
.b
.block
) < 0)
556 if (tree
->u
.b
.n
== 0)
559 if (emit_string(emitter
, "children") < 0)
561 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
562 YAML_BLOCK_SEQUENCE_STYLE
))
564 if (!yaml_emitter_emit(emitter
, &event
))
567 for (i
= 0; i
< tree
->u
.b
.n
; ++i
)
568 if (emit_tree(emitter
, tree
->u
.b
.child
[i
]) < 0)
571 if (!yaml_sequence_end_event_initialize(&event
))
573 if (!yaml_emitter_emit(emitter
, &event
))
577 case pet_tree_continue
:
580 if (emit_named_expr(emitter
, "variable", tree
->u
.d
.var
) < 0)
583 case pet_tree_decl_init
:
584 if (emit_named_expr(emitter
, "variable", tree
->u
.d
.var
) < 0)
586 if (emit_named_expr(emitter
,
587 "initialization", tree
->u
.d
.init
) < 0)
591 if (emit_named_expr(emitter
, "expr", tree
->u
.e
.expr
) < 0)
595 if (emit_named_int(emitter
, "declared", tree
->u
.l
.declared
) < 0)
597 if (emit_named_expr(emitter
, "variable", tree
->u
.l
.iv
) < 0)
599 if (emit_named_expr(emitter
,
600 "initialization", tree
->u
.l
.init
) < 0)
602 if (emit_named_expr(emitter
, "condition", tree
->u
.l
.cond
) < 0)
604 if (emit_named_expr(emitter
, "increment", tree
->u
.l
.inc
) < 0)
606 if (emit_string(emitter
, "body") < 0)
608 if (emit_tree(emitter
, tree
->u
.l
.body
) < 0)
612 if (emit_named_expr(emitter
, "condition", tree
->u
.l
.cond
) < 0)
614 if (emit_string(emitter
, "body") < 0)
616 if (emit_tree(emitter
, tree
->u
.l
.body
) < 0)
619 case pet_tree_infinite_loop
:
620 if (emit_string(emitter
, "body") < 0)
622 if (emit_tree(emitter
, tree
->u
.l
.body
) < 0)
626 if (emit_named_expr(emitter
, "condition", tree
->u
.i
.cond
) < 0)
628 if (emit_string(emitter
, "then") < 0)
630 if (emit_tree(emitter
, tree
->u
.i
.then_body
) < 0)
633 case pet_tree_if_else
:
634 if (emit_named_expr(emitter
, "condition", tree
->u
.i
.cond
) < 0)
636 if (emit_string(emitter
, "then") < 0)
638 if (emit_tree(emitter
, tree
->u
.i
.then_body
) < 0)
640 if (emit_string(emitter
, "else") < 0)
642 if (emit_tree(emitter
, tree
->u
.i
.else_body
) < 0)
647 if (!yaml_mapping_end_event_initialize(&event
))
649 if (!yaml_emitter_emit(emitter
, &event
))
655 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
659 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
660 YAML_BLOCK_MAPPING_STYLE
))
662 if (!yaml_emitter_emit(emitter
, &event
))
665 if (emit_string(emitter
, "line") < 0)
667 if (emit_int(emitter
, pet_loc_get_line(stmt
->loc
)) < 0)
670 if (emit_string(emitter
, "domain") < 0)
672 if (emit_set(emitter
, stmt
->domain
) < 0)
675 if (emit_string(emitter
, "schedule") < 0)
677 if (emit_map(emitter
, stmt
->schedule
) < 0)
680 if (emit_string(emitter
, "body") < 0)
682 if (emit_tree(emitter
, stmt
->body
) < 0)
685 if (stmt
->n_arg
> 0) {
688 if (emit_string(emitter
, "arguments") < 0)
690 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
691 YAML_BLOCK_SEQUENCE_STYLE
))
693 if (!yaml_emitter_emit(emitter
, &event
))
696 for (i
= 0; i
< stmt
->n_arg
; ++i
)
697 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
700 if (!yaml_sequence_end_event_initialize(&event
))
702 if (!yaml_emitter_emit(emitter
, &event
))
706 if (!yaml_mapping_end_event_initialize(&event
))
708 if (!yaml_emitter_emit(emitter
, &event
))
714 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
715 struct pet_stmt
**stmts
)
720 if (emit_string(emitter
, "statements") < 0)
722 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
723 YAML_BLOCK_SEQUENCE_STYLE
))
725 if (!yaml_emitter_emit(emitter
, &event
))
728 for (i
= 0; i
< n_stmt
; ++i
)
729 if (emit_stmt(emitter
, stmts
[i
]) < 0)
732 if (!yaml_sequence_end_event_initialize(&event
))
734 if (!yaml_emitter_emit(emitter
, &event
))
740 /* Print "implication" to "emitter".
742 static int emit_implication(yaml_emitter_t
*emitter
,
743 struct pet_implication
*implication
)
747 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
748 YAML_BLOCK_MAPPING_STYLE
))
750 if (!yaml_emitter_emit(emitter
, &event
))
753 if (emit_named_int(emitter
, "satisfied", implication
->satisfied
) < 0)
756 if (emit_named_map(emitter
, "extension", implication
->extension
) < 0)
759 if (!yaml_mapping_end_event_initialize(&event
))
761 if (!yaml_emitter_emit(emitter
, &event
))
767 /* Print the list of "n_implication" "implications", if any, to "emitter".
769 static int emit_implications(yaml_emitter_t
*emitter
, int n_implication
,
770 struct pet_implication
**implications
)
775 if (n_implication
== 0)
778 if (emit_string(emitter
, "implications") < 0)
780 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
781 YAML_BLOCK_SEQUENCE_STYLE
))
783 if (!yaml_emitter_emit(emitter
, &event
))
786 for (i
= 0; i
< n_implication
; ++i
)
787 if (emit_implication(emitter
, implications
[i
]) < 0)
790 if (!yaml_sequence_end_event_initialize(&event
))
792 if (!yaml_emitter_emit(emitter
, &event
))
798 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
802 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
803 YAML_BLOCK_MAPPING_STYLE
))
805 if (!yaml_emitter_emit(emitter
, &event
))
808 if (emit_named_unsigned(emitter
,
809 "start", pet_loc_get_start(scop
->loc
)) < 0)
811 if (emit_named_unsigned(emitter
, "end", pet_loc_get_end(scop
->loc
)) < 0)
813 if (emit_named_string(emitter
,
814 "indent", pet_loc_get_indent(scop
->loc
)) < 0)
816 if (emit_string(emitter
, "context") < 0)
818 if (emit_set(emitter
, scop
->context
) < 0)
820 if (!isl_set_plain_is_universe(scop
->context_value
) &&
821 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
824 if (emit_types(emitter
, scop
->n_type
, scop
->types
) < 0)
826 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
829 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
832 if (emit_implications(emitter
, scop
->n_implication
,
833 scop
->implications
) < 0)
836 if (!yaml_mapping_end_event_initialize(&event
))
838 if (!yaml_emitter_emit(emitter
, &event
))
844 /* Print a YAML serialization of "scop" to "out".
846 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
848 yaml_emitter_t emitter
;
851 yaml_emitter_initialize(&emitter
);
853 yaml_emitter_set_output_file(&emitter
, out
);
855 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
856 if (!yaml_emitter_emit(&emitter
, &event
))
859 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
861 if (!yaml_emitter_emit(&emitter
, &event
))
864 if (emit_scop(&emitter
, scop
) < 0)
867 if (!yaml_document_end_event_initialize(&event
, 1))
869 if (!yaml_emitter_emit(&emitter
, &event
))
872 yaml_stream_end_event_initialize(&event
);
873 if (!yaml_emitter_emit(&emitter
, &event
))
876 yaml_emitter_delete(&emitter
);
879 yaml_emitter_delete(&emitter
);