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 static int emit_int(yaml_emitter_t
*emitter
, int i
)
57 snprintf(buffer
, sizeof(buffer
), "%d", i
);
58 return emit_string(emitter
, buffer
);
61 static int emit_double(yaml_emitter_t
*emitter
, double d
)
65 snprintf(buffer
, sizeof(buffer
), "%g", d
);
66 return emit_string(emitter
, buffer
);
69 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
71 isl_ctx
*ctx
= isl_map_get_ctx(map
);
76 p
= isl_printer_to_str(ctx
);
77 p
= isl_printer_print_map(p
, map
);
78 str
= isl_printer_get_str(p
);
80 r
= emit_string(emitter
, str
);
85 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
87 isl_ctx
*ctx
= isl_set_get_ctx(set
);
92 p
= isl_printer_to_str(ctx
);
93 p
= isl_printer_print_set(p
, set
);
94 str
= isl_printer_get_str(p
);
96 r
= emit_string(emitter
, str
);
101 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
105 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
106 YAML_BLOCK_MAPPING_STYLE
))
108 if (!yaml_emitter_emit(emitter
, &event
))
111 if (emit_string(emitter
, "context") < 0)
113 if (emit_set(emitter
, array
->context
) < 0)
116 if (emit_string(emitter
, "extent") < 0)
118 if (emit_set(emitter
, array
->extent
) < 0)
121 if (array
->value_bounds
) {
122 if (emit_string(emitter
, "value_bounds") < 0)
124 if (emit_set(emitter
, array
->value_bounds
) < 0)
128 if (emit_string(emitter
, "element_type") < 0)
130 if (emit_string(emitter
, array
->element_type
) < 0)
133 if (array
->live_out
) {
134 if (emit_string(emitter
, "live_out") < 0)
136 if (emit_string(emitter
, "1") < 0)
140 if (!yaml_mapping_end_event_initialize(&event
))
142 if (!yaml_emitter_emit(emitter
, &event
))
148 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
149 struct pet_array
**arrays
)
154 if (emit_string(emitter
, "arrays") < 0)
156 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
157 YAML_BLOCK_SEQUENCE_STYLE
))
159 if (!yaml_emitter_emit(emitter
, &event
))
162 for (i
= 0; i
< n_array
; ++i
)
163 if (emit_array(emitter
, arrays
[i
]) < 0)
166 if (!yaml_sequence_end_event_initialize(&event
))
168 if (!yaml_emitter_emit(emitter
, &event
))
174 static int emit_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
176 if (emit_string(emitter
, pet_type_str(type
)) < 0)
181 static int emit_expr(yaml_emitter_t
*emitter
, struct pet_expr
*expr
)
185 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
186 YAML_BLOCK_MAPPING_STYLE
))
188 if (!yaml_emitter_emit(emitter
, &event
))
191 if (emit_string(emitter
, "type") < 0)
193 if (emit_type(emitter
, expr
->type
) < 0)
196 switch (expr
->type
) {
197 case pet_expr_double
:
198 if (emit_string(emitter
, "value") < 0)
200 if (emit_double(emitter
, expr
->d
) < 0)
203 case pet_expr_access
:
204 if (emit_string(emitter
, "relation") < 0)
206 if (emit_map(emitter
, expr
->acc
.access
) < 0)
208 if (emit_string(emitter
, "read") < 0)
210 if (emit_int(emitter
, expr
->acc
.read
) < 0)
212 if (emit_string(emitter
, "write") < 0)
214 if (emit_int(emitter
, expr
->acc
.write
) < 0)
218 case pet_expr_binary
:
219 if (emit_string(emitter
, "operation") < 0)
221 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
224 case pet_expr_ternary
:
227 if (emit_string(emitter
, "name") < 0)
229 if (emit_string(emitter
, expr
->name
) < 0)
234 if (expr
->n_arg
> 0) {
237 if (emit_string(emitter
, "arguments") < 0)
239 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
240 YAML_BLOCK_SEQUENCE_STYLE
))
242 if (!yaml_emitter_emit(emitter
, &event
))
245 for (i
= 0; i
< expr
->n_arg
; ++i
)
246 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
249 if (!yaml_sequence_end_event_initialize(&event
))
251 if (!yaml_emitter_emit(emitter
, &event
))
255 if (!yaml_mapping_end_event_initialize(&event
))
257 if (!yaml_emitter_emit(emitter
, &event
))
263 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
267 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
268 YAML_BLOCK_MAPPING_STYLE
))
270 if (!yaml_emitter_emit(emitter
, &event
))
273 if (emit_string(emitter
, "line") < 0)
275 if (emit_int(emitter
, stmt
->line
) < 0)
278 if (emit_string(emitter
, "domain") < 0)
280 if (emit_set(emitter
, stmt
->domain
) < 0)
283 if (emit_string(emitter
, "schedule") < 0)
285 if (emit_map(emitter
, stmt
->schedule
) < 0)
288 if (emit_string(emitter
, "body") < 0)
290 if (emit_expr(emitter
, stmt
->body
) < 0)
293 if (!yaml_mapping_end_event_initialize(&event
))
295 if (!yaml_emitter_emit(emitter
, &event
))
301 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
302 struct pet_stmt
**stmts
)
307 if (emit_string(emitter
, "statements") < 0)
309 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
310 YAML_BLOCK_SEQUENCE_STYLE
))
312 if (!yaml_emitter_emit(emitter
, &event
))
315 for (i
= 0; i
< n_stmt
; ++i
)
316 if (emit_stmt(emitter
, stmts
[i
]) < 0)
319 if (!yaml_sequence_end_event_initialize(&event
))
321 if (!yaml_emitter_emit(emitter
, &event
))
327 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
331 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
332 YAML_BLOCK_MAPPING_STYLE
))
334 if (!yaml_emitter_emit(emitter
, &event
))
337 if (emit_string(emitter
, "context") < 0)
339 if (emit_set(emitter
, scop
->context
) < 0)
342 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
345 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
348 if (!yaml_mapping_end_event_initialize(&event
))
350 if (!yaml_emitter_emit(emitter
, &event
))
356 /* Print a YAML serialization of "scop" to "out".
358 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
360 yaml_emitter_t emitter
;
363 yaml_emitter_initialize(&emitter
);
365 yaml_emitter_set_output_file(&emitter
, out
);
367 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
368 if (!yaml_emitter_emit(&emitter
, &event
))
371 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
373 if (!yaml_emitter_emit(&emitter
, &event
))
376 if (emit_scop(&emitter
, scop
) < 0)
379 if (!yaml_document_end_event_initialize(&event
, 1))
381 if (!yaml_emitter_emit(&emitter
, &event
))
384 yaml_stream_end_event_initialize(&event
);
385 if (!yaml_emitter_emit(&emitter
, &event
))
388 yaml_emitter_delete(&emitter
);
391 yaml_emitter_delete(&emitter
);