2 * Copyright 2011 Leiden University. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
37 #include "scop_yaml.h"
39 static int emit_string(yaml_emitter_t
*emitter
, const char *str
)
43 if (!yaml_scalar_event_initialize(&event
, NULL
, NULL
,
44 (yaml_char_t
*) str
, strlen(str
),
45 1, 1, YAML_PLAIN_SCALAR_STYLE
))
47 if (!yaml_emitter_emit(emitter
, &event
))
53 /* Print the isl_id "id" to "emitter".
55 static int emit_id(yaml_emitter_t
*emitter
, __isl_keep isl_id
*id
)
57 return emit_string(emitter
, isl_id_get_name(id
));
60 /* Print the string "name" and the isl_id "id" to "emitter".
62 static int emit_named_id(yaml_emitter_t
*emitter
, const char *name
,
63 __isl_keep isl_id
*id
)
65 if (emit_string(emitter
, name
) < 0)
67 if (emit_id(emitter
, id
) < 0)
72 static int emit_int(yaml_emitter_t
*emitter
, int i
)
76 snprintf(buffer
, sizeof(buffer
), "%d", i
);
77 return emit_string(emitter
, buffer
);
80 static int emit_named_int(yaml_emitter_t
*emitter
, const char *name
, int i
)
82 if (emit_string(emitter
, name
) < 0)
84 if (emit_int(emitter
, i
) < 0)
89 /* Print the unsigned integer "u" to "emitter".
91 static int emit_unsigned(yaml_emitter_t
*emitter
, unsigned u
)
95 snprintf(buffer
, sizeof(buffer
), "%u", u
);
96 return emit_string(emitter
, buffer
);
99 /* Print the string "name" and the unsigned integer "u" to "emitter".
101 static int emit_named_unsigned(yaml_emitter_t
*emitter
, const char *name
,
104 if (emit_string(emitter
, name
) < 0)
106 if (emit_int(emitter
, u
) < 0)
111 static int emit_double(yaml_emitter_t
*emitter
, double d
)
115 snprintf(buffer
, sizeof(buffer
), "%g", d
);
116 return emit_string(emitter
, buffer
);
119 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
121 isl_ctx
*ctx
= isl_map_get_ctx(map
);
126 p
= isl_printer_to_str(ctx
);
127 p
= isl_printer_print_map(p
, map
);
128 str
= isl_printer_get_str(p
);
130 r
= emit_string(emitter
, str
);
135 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
137 isl_ctx
*ctx
= isl_set_get_ctx(set
);
142 p
= isl_printer_to_str(ctx
);
143 p
= isl_printer_print_set(p
, set
);
144 str
= isl_printer_get_str(p
);
146 r
= emit_string(emitter
, str
);
151 static int emit_named_set(yaml_emitter_t
*emitter
, const char *name
,
152 __isl_keep isl_set
*set
)
154 if (emit_string(emitter
, name
) < 0)
156 if (emit_set(emitter
, set
) < 0)
161 /* Print the string "name" and the map "map" to "emitter".
163 static int emit_named_map(yaml_emitter_t
*emitter
, const char *name
,
164 __isl_keep isl_map
*map
)
166 if (emit_string(emitter
, name
) < 0)
168 if (emit_map(emitter
, map
) < 0)
173 /* Print the isl_multi_pw_aff "mpa" to "emitter".
175 static int emit_multi_pw_aff(yaml_emitter_t
*emitter
,
176 __isl_keep isl_multi_pw_aff
*mpa
)
178 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(mpa
);
183 p
= isl_printer_to_str(ctx
);
184 p
= isl_printer_print_multi_pw_aff(p
, mpa
);
185 str
= isl_printer_get_str(p
);
187 r
= emit_string(emitter
, str
);
192 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
194 static int emit_named_multi_pw_aff(yaml_emitter_t
*emitter
, const char *name
,
195 __isl_keep isl_multi_pw_aff
*mpa
)
197 if (emit_string(emitter
, name
) < 0)
199 if (emit_multi_pw_aff(emitter
, mpa
) < 0)
204 /* Print "type" to "emitter".
206 static int emit_type(yaml_emitter_t
*emitter
, struct pet_type
*type
)
210 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
211 YAML_BLOCK_MAPPING_STYLE
))
213 if (!yaml_emitter_emit(emitter
, &event
))
216 if (emit_string(emitter
, "name") < 0)
218 if (emit_string(emitter
, type
->name
) < 0)
221 if (emit_string(emitter
, "definition") < 0)
223 if (emit_string(emitter
, type
->definition
) < 0)
226 if (!yaml_mapping_end_event_initialize(&event
))
228 if (!yaml_emitter_emit(emitter
, &event
))
234 /* Print the list of "n_type" "types", if any, to "emitter".
236 static int emit_types(yaml_emitter_t
*emitter
, int n_type
,
237 struct pet_type
**types
)
245 if (emit_string(emitter
, "types") < 0)
247 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
248 YAML_BLOCK_SEQUENCE_STYLE
))
250 if (!yaml_emitter_emit(emitter
, &event
))
253 for (i
= 0; i
< n_type
; ++i
)
254 if (emit_type(emitter
, types
[i
]) < 0)
257 if (!yaml_sequence_end_event_initialize(&event
))
259 if (!yaml_emitter_emit(emitter
, &event
))
265 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
269 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
270 YAML_BLOCK_MAPPING_STYLE
))
272 if (!yaml_emitter_emit(emitter
, &event
))
275 if (emit_string(emitter
, "context") < 0)
277 if (emit_set(emitter
, array
->context
) < 0)
280 if (emit_string(emitter
, "extent") < 0)
282 if (emit_set(emitter
, array
->extent
) < 0)
285 if (array
->value_bounds
) {
286 if (emit_string(emitter
, "value_bounds") < 0)
288 if (emit_set(emitter
, array
->value_bounds
) < 0)
292 if (emit_string(emitter
, "element_type") < 0)
294 if (emit_string(emitter
, array
->element_type
) < 0)
296 if (emit_named_int(emitter
, "element_size", array
->element_size
) < 0)
299 if (array
->element_is_record
)
300 if (emit_named_int(emitter
, "element_is_record",
301 array
->element_is_record
) < 0)
304 if (array
->live_out
) {
305 if (emit_string(emitter
, "live_out") < 0)
307 if (emit_string(emitter
, "1") < 0)
311 if (array
->uniquely_defined
) {
312 if (emit_string(emitter
, "uniquely_defined") < 0)
314 if (emit_string(emitter
, "1") < 0)
318 if (array
->declared
&& emit_named_int(emitter
, "declared", 1) < 0)
320 if (array
->exposed
&& emit_named_int(emitter
, "exposed", 1) < 0)
323 if (!yaml_mapping_end_event_initialize(&event
))
325 if (!yaml_emitter_emit(emitter
, &event
))
331 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
332 struct pet_array
**arrays
)
337 if (emit_string(emitter
, "arrays") < 0)
339 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
340 YAML_BLOCK_SEQUENCE_STYLE
))
342 if (!yaml_emitter_emit(emitter
, &event
))
345 for (i
= 0; i
< n_array
; ++i
)
346 if (emit_array(emitter
, arrays
[i
]) < 0)
349 if (!yaml_sequence_end_event_initialize(&event
))
351 if (!yaml_emitter_emit(emitter
, &event
))
357 static int emit_expr_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
359 if (emit_string(emitter
, pet_type_str(type
)) < 0)
364 static int emit_expr(yaml_emitter_t
*emitter
, struct pet_expr
*expr
)
368 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
369 YAML_BLOCK_MAPPING_STYLE
))
371 if (!yaml_emitter_emit(emitter
, &event
))
374 if (emit_string(emitter
, "type") < 0)
376 if (emit_expr_type(emitter
, expr
->type
) < 0)
379 switch (expr
->type
) {
380 case pet_expr_double
:
381 if (emit_string(emitter
, "value") < 0)
383 if (emit_double(emitter
, expr
->d
.val
) < 0)
385 if (emit_string(emitter
, "string") < 0)
387 if (emit_string(emitter
, expr
->d
.s
) < 0)
390 case pet_expr_access
:
391 if (emit_string(emitter
, "relation") < 0)
393 if (emit_map(emitter
, expr
->acc
.access
) < 0)
395 if (emit_named_multi_pw_aff(emitter
,
396 "index", expr
->acc
.index
) < 0)
398 if (expr
->acc
.ref_id
&&
399 emit_named_id(emitter
, "reference", expr
->acc
.ref_id
) < 0)
401 if (emit_string(emitter
, "read") < 0)
403 if (emit_int(emitter
, expr
->acc
.read
) < 0)
405 if (emit_string(emitter
, "write") < 0)
407 if (emit_int(emitter
, expr
->acc
.write
) < 0)
411 case pet_expr_binary
:
412 if (emit_string(emitter
, "operation") < 0)
414 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
417 case pet_expr_ternary
:
420 if (emit_string(emitter
, "name") < 0)
422 if (emit_string(emitter
, expr
->name
) < 0)
426 if (emit_string(emitter
, "type_name") < 0)
428 if (emit_string(emitter
, expr
->type_name
) < 0)
433 if (expr
->n_arg
> 0) {
436 if (emit_string(emitter
, "arguments") < 0)
438 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
439 YAML_BLOCK_SEQUENCE_STYLE
))
441 if (!yaml_emitter_emit(emitter
, &event
))
444 for (i
= 0; i
< expr
->n_arg
; ++i
)
445 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
448 if (!yaml_sequence_end_event_initialize(&event
))
450 if (!yaml_emitter_emit(emitter
, &event
))
454 if (!yaml_mapping_end_event_initialize(&event
))
456 if (!yaml_emitter_emit(emitter
, &event
))
462 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
466 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
467 YAML_BLOCK_MAPPING_STYLE
))
469 if (!yaml_emitter_emit(emitter
, &event
))
472 if (emit_string(emitter
, "line") < 0)
474 if (emit_int(emitter
, stmt
->line
) < 0)
477 if (emit_string(emitter
, "domain") < 0)
479 if (emit_set(emitter
, stmt
->domain
) < 0)
482 if (emit_string(emitter
, "schedule") < 0)
484 if (emit_map(emitter
, stmt
->schedule
) < 0)
487 if (emit_string(emitter
, "body") < 0)
489 if (emit_expr(emitter
, stmt
->body
) < 0)
492 if (stmt
->n_arg
> 0) {
495 if (emit_string(emitter
, "arguments") < 0)
497 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
498 YAML_BLOCK_SEQUENCE_STYLE
))
500 if (!yaml_emitter_emit(emitter
, &event
))
503 for (i
= 0; i
< stmt
->n_arg
; ++i
)
504 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
507 if (!yaml_sequence_end_event_initialize(&event
))
509 if (!yaml_emitter_emit(emitter
, &event
))
513 if (!yaml_mapping_end_event_initialize(&event
))
515 if (!yaml_emitter_emit(emitter
, &event
))
521 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
522 struct pet_stmt
**stmts
)
527 if (emit_string(emitter
, "statements") < 0)
529 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
530 YAML_BLOCK_SEQUENCE_STYLE
))
532 if (!yaml_emitter_emit(emitter
, &event
))
535 for (i
= 0; i
< n_stmt
; ++i
)
536 if (emit_stmt(emitter
, stmts
[i
]) < 0)
539 if (!yaml_sequence_end_event_initialize(&event
))
541 if (!yaml_emitter_emit(emitter
, &event
))
547 /* Print "implication" to "emitter".
549 static int emit_implication(yaml_emitter_t
*emitter
,
550 struct pet_implication
*implication
)
554 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
555 YAML_BLOCK_MAPPING_STYLE
))
557 if (!yaml_emitter_emit(emitter
, &event
))
560 if (emit_named_int(emitter
, "satisfied", implication
->satisfied
) < 0)
563 if (emit_named_map(emitter
, "extension", implication
->extension
) < 0)
566 if (!yaml_mapping_end_event_initialize(&event
))
568 if (!yaml_emitter_emit(emitter
, &event
))
574 /* Print the list of "n_implication" "implications", if any, to "emitter".
576 static int emit_implications(yaml_emitter_t
*emitter
, int n_implication
,
577 struct pet_implication
**implications
)
582 if (n_implication
== 0)
585 if (emit_string(emitter
, "implications") < 0)
587 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
588 YAML_BLOCK_SEQUENCE_STYLE
))
590 if (!yaml_emitter_emit(emitter
, &event
))
593 for (i
= 0; i
< n_implication
; ++i
)
594 if (emit_implication(emitter
, implications
[i
]) < 0)
597 if (!yaml_sequence_end_event_initialize(&event
))
599 if (!yaml_emitter_emit(emitter
, &event
))
605 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
609 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
610 YAML_BLOCK_MAPPING_STYLE
))
612 if (!yaml_emitter_emit(emitter
, &event
))
615 if (emit_named_unsigned(emitter
, "start", scop
->start
) < 0)
617 if (emit_named_unsigned(emitter
, "end", scop
->end
) < 0)
619 if (emit_string(emitter
, "context") < 0)
621 if (emit_set(emitter
, scop
->context
) < 0)
623 if (!isl_set_plain_is_universe(scop
->context_value
) &&
624 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
627 if (emit_types(emitter
, scop
->n_type
, scop
->types
) < 0)
629 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
632 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
635 if (emit_implications(emitter
, scop
->n_implication
,
636 scop
->implications
) < 0)
639 if (!yaml_mapping_end_event_initialize(&event
))
641 if (!yaml_emitter_emit(emitter
, &event
))
647 /* Print a YAML serialization of "scop" to "out".
649 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
651 yaml_emitter_t emitter
;
654 yaml_emitter_initialize(&emitter
);
656 yaml_emitter_set_output_file(&emitter
, out
);
658 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
659 if (!yaml_emitter_emit(&emitter
, &event
))
662 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
664 if (!yaml_emitter_emit(&emitter
, &event
))
667 if (emit_scop(&emitter
, scop
) < 0)
670 if (!yaml_document_end_event_initialize(&event
, 1))
672 if (!yaml_emitter_emit(&emitter
, &event
))
675 yaml_stream_end_event_initialize(&event
);
676 if (!yaml_emitter_emit(&emitter
, &event
))
679 yaml_emitter_delete(&emitter
);
682 yaml_emitter_delete(&emitter
);