2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013 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
39 #include "scop_yaml.h"
41 static int emit_string(yaml_emitter_t
*emitter
, const char *str
)
45 if (!yaml_scalar_event_initialize(&event
, NULL
, NULL
,
46 (yaml_char_t
*) str
, strlen(str
),
47 1, 1, YAML_PLAIN_SCALAR_STYLE
))
49 if (!yaml_emitter_emit(emitter
, &event
))
55 /* Print the isl_id "id" to "emitter".
57 static int emit_id(yaml_emitter_t
*emitter
, __isl_keep isl_id
*id
)
59 return emit_string(emitter
, isl_id_get_name(id
));
62 /* Print the string "name" and the isl_id "id" to "emitter".
64 static int emit_named_id(yaml_emitter_t
*emitter
, const char *name
,
65 __isl_keep isl_id
*id
)
67 if (emit_string(emitter
, name
) < 0)
69 if (emit_id(emitter
, id
) < 0)
74 static int emit_int(yaml_emitter_t
*emitter
, int i
)
78 snprintf(buffer
, sizeof(buffer
), "%d", i
);
79 return emit_string(emitter
, buffer
);
82 static int emit_named_int(yaml_emitter_t
*emitter
, const char *name
, int i
)
84 if (emit_string(emitter
, name
) < 0)
86 if (emit_int(emitter
, i
) < 0)
91 /* Print the unsigned integer "u" to "emitter".
93 static int emit_unsigned(yaml_emitter_t
*emitter
, unsigned u
)
97 snprintf(buffer
, sizeof(buffer
), "%u", u
);
98 return emit_string(emitter
, buffer
);
101 /* Print the string "name" and the unsigned integer "u" to "emitter".
103 static int emit_named_unsigned(yaml_emitter_t
*emitter
, const char *name
,
106 if (emit_string(emitter
, name
) < 0)
108 if (emit_int(emitter
, u
) < 0)
113 static int emit_double(yaml_emitter_t
*emitter
, double d
)
117 snprintf(buffer
, sizeof(buffer
), "%g", d
);
118 return emit_string(emitter
, buffer
);
121 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
123 isl_ctx
*ctx
= isl_map_get_ctx(map
);
128 p
= isl_printer_to_str(ctx
);
129 p
= isl_printer_print_map(p
, map
);
130 str
= isl_printer_get_str(p
);
132 r
= emit_string(emitter
, str
);
137 /* Print the isl_val "val" to "emitter".
139 static int emit_val(yaml_emitter_t
*emitter
, __isl_keep isl_val
*val
)
141 isl_ctx
*ctx
= isl_val_get_ctx(val
);
146 p
= isl_printer_to_str(ctx
);
147 p
= isl_printer_print_val(p
, val
);
148 str
= isl_printer_get_str(p
);
150 r
= emit_string(emitter
, str
);
155 /* Print the string "name" and the isl_val "val" to "emitter".
157 static int emit_named_val(yaml_emitter_t
*emitter
, const char *name
,
158 __isl_keep isl_val
*val
)
160 if (emit_string(emitter
, name
) < 0)
162 if (emit_val(emitter
, val
) < 0)
167 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
169 isl_ctx
*ctx
= isl_set_get_ctx(set
);
174 p
= isl_printer_to_str(ctx
);
175 p
= isl_printer_print_set(p
, set
);
176 str
= isl_printer_get_str(p
);
178 r
= emit_string(emitter
, str
);
183 static int emit_named_set(yaml_emitter_t
*emitter
, const char *name
,
184 __isl_keep isl_set
*set
)
186 if (emit_string(emitter
, name
) < 0)
188 if (emit_set(emitter
, set
) < 0)
193 /* Print the string "name" and the map "map" to "emitter".
195 static int emit_named_map(yaml_emitter_t
*emitter
, const char *name
,
196 __isl_keep isl_map
*map
)
198 if (emit_string(emitter
, name
) < 0)
200 if (emit_map(emitter
, map
) < 0)
205 /* Print the isl_multi_pw_aff "mpa" to "emitter".
207 static int emit_multi_pw_aff(yaml_emitter_t
*emitter
,
208 __isl_keep isl_multi_pw_aff
*mpa
)
210 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(mpa
);
215 p
= isl_printer_to_str(ctx
);
216 p
= isl_printer_print_multi_pw_aff(p
, mpa
);
217 str
= isl_printer_get_str(p
);
219 r
= emit_string(emitter
, str
);
224 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
226 static int emit_named_multi_pw_aff(yaml_emitter_t
*emitter
, const char *name
,
227 __isl_keep isl_multi_pw_aff
*mpa
)
229 if (emit_string(emitter
, name
) < 0)
231 if (emit_multi_pw_aff(emitter
, mpa
) < 0)
236 /* Print "type" to "emitter".
238 static int emit_type(yaml_emitter_t
*emitter
, struct pet_type
*type
)
242 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
243 YAML_BLOCK_MAPPING_STYLE
))
245 if (!yaml_emitter_emit(emitter
, &event
))
248 if (emit_string(emitter
, "name") < 0)
250 if (emit_string(emitter
, type
->name
) < 0)
253 if (emit_string(emitter
, "definition") < 0)
255 if (emit_string(emitter
, type
->definition
) < 0)
258 if (!yaml_mapping_end_event_initialize(&event
))
260 if (!yaml_emitter_emit(emitter
, &event
))
266 /* Print the list of "n_type" "types", if any, to "emitter".
268 static int emit_types(yaml_emitter_t
*emitter
, int n_type
,
269 struct pet_type
**types
)
277 if (emit_string(emitter
, "types") < 0)
279 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
280 YAML_BLOCK_SEQUENCE_STYLE
))
282 if (!yaml_emitter_emit(emitter
, &event
))
285 for (i
= 0; i
< n_type
; ++i
)
286 if (emit_type(emitter
, types
[i
]) < 0)
289 if (!yaml_sequence_end_event_initialize(&event
))
291 if (!yaml_emitter_emit(emitter
, &event
))
297 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
301 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
302 YAML_BLOCK_MAPPING_STYLE
))
304 if (!yaml_emitter_emit(emitter
, &event
))
307 if (emit_string(emitter
, "context") < 0)
309 if (emit_set(emitter
, array
->context
) < 0)
312 if (emit_string(emitter
, "extent") < 0)
314 if (emit_set(emitter
, array
->extent
) < 0)
317 if (array
->value_bounds
) {
318 if (emit_string(emitter
, "value_bounds") < 0)
320 if (emit_set(emitter
, array
->value_bounds
) < 0)
324 if (emit_string(emitter
, "element_type") < 0)
326 if (emit_string(emitter
, array
->element_type
) < 0)
328 if (emit_named_int(emitter
, "element_size", array
->element_size
) < 0)
331 if (array
->element_is_record
)
332 if (emit_named_int(emitter
, "element_is_record",
333 array
->element_is_record
) < 0)
336 if (array
->live_out
) {
337 if (emit_string(emitter
, "live_out") < 0)
339 if (emit_string(emitter
, "1") < 0)
343 if (array
->uniquely_defined
) {
344 if (emit_string(emitter
, "uniquely_defined") < 0)
346 if (emit_string(emitter
, "1") < 0)
350 if (array
->declared
&& emit_named_int(emitter
, "declared", 1) < 0)
352 if (array
->exposed
&& emit_named_int(emitter
, "exposed", 1) < 0)
355 if (!yaml_mapping_end_event_initialize(&event
))
357 if (!yaml_emitter_emit(emitter
, &event
))
363 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
364 struct pet_array
**arrays
)
369 if (emit_string(emitter
, "arrays") < 0)
371 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
372 YAML_BLOCK_SEQUENCE_STYLE
))
374 if (!yaml_emitter_emit(emitter
, &event
))
377 for (i
= 0; i
< n_array
; ++i
)
378 if (emit_array(emitter
, arrays
[i
]) < 0)
381 if (!yaml_sequence_end_event_initialize(&event
))
383 if (!yaml_emitter_emit(emitter
, &event
))
389 static int emit_expr_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
391 if (emit_string(emitter
, pet_type_str(type
)) < 0)
396 static int emit_expr(yaml_emitter_t
*emitter
, __isl_keep pet_expr
*expr
)
400 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
401 YAML_BLOCK_MAPPING_STYLE
))
403 if (!yaml_emitter_emit(emitter
, &event
))
406 if (emit_string(emitter
, "type") < 0)
408 if (emit_expr_type(emitter
, expr
->type
) < 0)
411 switch (expr
->type
) {
415 if (emit_named_val(emitter
, "value", expr
->i
) < 0)
418 case pet_expr_double
:
419 if (emit_string(emitter
, "value") < 0)
421 if (emit_double(emitter
, expr
->d
.val
) < 0)
423 if (emit_string(emitter
, "string") < 0)
425 if (emit_string(emitter
, expr
->d
.s
) < 0)
428 case pet_expr_access
:
429 if (emit_string(emitter
, "relation") < 0)
431 if (emit_map(emitter
, expr
->acc
.access
) < 0)
433 if (emit_named_multi_pw_aff(emitter
,
434 "index", expr
->acc
.index
) < 0)
436 if (expr
->acc
.ref_id
&&
437 emit_named_id(emitter
, "reference", expr
->acc
.ref_id
) < 0)
439 if (emit_string(emitter
, "read") < 0)
441 if (emit_int(emitter
, expr
->acc
.read
) < 0)
443 if (emit_string(emitter
, "write") < 0)
445 if (emit_int(emitter
, expr
->acc
.write
) < 0)
449 if (emit_string(emitter
, "operation") < 0)
451 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
455 if (emit_string(emitter
, "name") < 0)
457 if (emit_string(emitter
, expr
->name
) < 0)
461 if (emit_string(emitter
, "type_name") < 0)
463 if (emit_string(emitter
, expr
->type_name
) < 0)
468 if (expr
->n_arg
> 0) {
471 if (emit_string(emitter
, "arguments") < 0)
473 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
474 YAML_BLOCK_SEQUENCE_STYLE
))
476 if (!yaml_emitter_emit(emitter
, &event
))
479 for (i
= 0; i
< expr
->n_arg
; ++i
)
480 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
483 if (!yaml_sequence_end_event_initialize(&event
))
485 if (!yaml_emitter_emit(emitter
, &event
))
489 if (!yaml_mapping_end_event_initialize(&event
))
491 if (!yaml_emitter_emit(emitter
, &event
))
497 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
501 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
502 YAML_BLOCK_MAPPING_STYLE
))
504 if (!yaml_emitter_emit(emitter
, &event
))
507 if (emit_string(emitter
, "line") < 0)
509 if (emit_int(emitter
, stmt
->line
) < 0)
512 if (emit_string(emitter
, "domain") < 0)
514 if (emit_set(emitter
, stmt
->domain
) < 0)
517 if (emit_string(emitter
, "schedule") < 0)
519 if (emit_map(emitter
, stmt
->schedule
) < 0)
522 if (emit_string(emitter
, "body") < 0)
524 if (emit_expr(emitter
, stmt
->body
) < 0)
527 if (stmt
->n_arg
> 0) {
530 if (emit_string(emitter
, "arguments") < 0)
532 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
533 YAML_BLOCK_SEQUENCE_STYLE
))
535 if (!yaml_emitter_emit(emitter
, &event
))
538 for (i
= 0; i
< stmt
->n_arg
; ++i
)
539 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
542 if (!yaml_sequence_end_event_initialize(&event
))
544 if (!yaml_emitter_emit(emitter
, &event
))
548 if (!yaml_mapping_end_event_initialize(&event
))
550 if (!yaml_emitter_emit(emitter
, &event
))
556 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
557 struct pet_stmt
**stmts
)
562 if (emit_string(emitter
, "statements") < 0)
564 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
565 YAML_BLOCK_SEQUENCE_STYLE
))
567 if (!yaml_emitter_emit(emitter
, &event
))
570 for (i
= 0; i
< n_stmt
; ++i
)
571 if (emit_stmt(emitter
, stmts
[i
]) < 0)
574 if (!yaml_sequence_end_event_initialize(&event
))
576 if (!yaml_emitter_emit(emitter
, &event
))
582 /* Print "implication" to "emitter".
584 static int emit_implication(yaml_emitter_t
*emitter
,
585 struct pet_implication
*implication
)
589 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
590 YAML_BLOCK_MAPPING_STYLE
))
592 if (!yaml_emitter_emit(emitter
, &event
))
595 if (emit_named_int(emitter
, "satisfied", implication
->satisfied
) < 0)
598 if (emit_named_map(emitter
, "extension", implication
->extension
) < 0)
601 if (!yaml_mapping_end_event_initialize(&event
))
603 if (!yaml_emitter_emit(emitter
, &event
))
609 /* Print the list of "n_implication" "implications", if any, to "emitter".
611 static int emit_implications(yaml_emitter_t
*emitter
, int n_implication
,
612 struct pet_implication
**implications
)
617 if (n_implication
== 0)
620 if (emit_string(emitter
, "implications") < 0)
622 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
623 YAML_BLOCK_SEQUENCE_STYLE
))
625 if (!yaml_emitter_emit(emitter
, &event
))
628 for (i
= 0; i
< n_implication
; ++i
)
629 if (emit_implication(emitter
, implications
[i
]) < 0)
632 if (!yaml_sequence_end_event_initialize(&event
))
634 if (!yaml_emitter_emit(emitter
, &event
))
640 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
644 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
645 YAML_BLOCK_MAPPING_STYLE
))
647 if (!yaml_emitter_emit(emitter
, &event
))
650 if (emit_named_unsigned(emitter
, "start", scop
->start
) < 0)
652 if (emit_named_unsigned(emitter
, "end", scop
->end
) < 0)
654 if (emit_string(emitter
, "context") < 0)
656 if (emit_set(emitter
, scop
->context
) < 0)
658 if (!isl_set_plain_is_universe(scop
->context_value
) &&
659 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
662 if (emit_types(emitter
, scop
->n_type
, scop
->types
) < 0)
664 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
667 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
670 if (emit_implications(emitter
, scop
->n_implication
,
671 scop
->implications
) < 0)
674 if (!yaml_mapping_end_event_initialize(&event
))
676 if (!yaml_emitter_emit(emitter
, &event
))
682 /* Print a YAML serialization of "scop" to "out".
684 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
686 yaml_emitter_t emitter
;
689 yaml_emitter_initialize(&emitter
);
691 yaml_emitter_set_output_file(&emitter
, out
);
693 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
694 if (!yaml_emitter_emit(&emitter
, &event
))
697 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
699 if (!yaml_emitter_emit(&emitter
, &event
))
702 if (emit_scop(&emitter
, scop
) < 0)
705 if (!yaml_document_end_event_initialize(&event
, 1))
707 if (!yaml_emitter_emit(&emitter
, &event
))
710 yaml_stream_end_event_initialize(&event
);
711 if (!yaml_emitter_emit(&emitter
, &event
))
714 yaml_emitter_delete(&emitter
);
717 yaml_emitter_delete(&emitter
);