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_named_set(yaml_emitter_t
*emitter
, const char *name
,
102 __isl_keep isl_set
*set
)
104 if (emit_string(emitter
, name
) < 0)
106 if (emit_set(emitter
, set
) < 0)
111 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
115 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
116 YAML_BLOCK_MAPPING_STYLE
))
118 if (!yaml_emitter_emit(emitter
, &event
))
121 if (emit_string(emitter
, "context") < 0)
123 if (emit_set(emitter
, array
->context
) < 0)
126 if (emit_string(emitter
, "extent") < 0)
128 if (emit_set(emitter
, array
->extent
) < 0)
131 if (array
->value_bounds
) {
132 if (emit_string(emitter
, "value_bounds") < 0)
134 if (emit_set(emitter
, array
->value_bounds
) < 0)
138 if (emit_string(emitter
, "element_type") < 0)
140 if (emit_string(emitter
, array
->element_type
) < 0)
143 if (array
->live_out
) {
144 if (emit_string(emitter
, "live_out") < 0)
146 if (emit_string(emitter
, "1") < 0)
150 if (!yaml_mapping_end_event_initialize(&event
))
152 if (!yaml_emitter_emit(emitter
, &event
))
158 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
159 struct pet_array
**arrays
)
164 if (emit_string(emitter
, "arrays") < 0)
166 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
167 YAML_BLOCK_SEQUENCE_STYLE
))
169 if (!yaml_emitter_emit(emitter
, &event
))
172 for (i
= 0; i
< n_array
; ++i
)
173 if (emit_array(emitter
, arrays
[i
]) < 0)
176 if (!yaml_sequence_end_event_initialize(&event
))
178 if (!yaml_emitter_emit(emitter
, &event
))
184 static int emit_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
186 if (emit_string(emitter
, pet_type_str(type
)) < 0)
191 static int emit_expr(yaml_emitter_t
*emitter
, struct pet_expr
*expr
)
195 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
196 YAML_BLOCK_MAPPING_STYLE
))
198 if (!yaml_emitter_emit(emitter
, &event
))
201 if (emit_string(emitter
, "type") < 0)
203 if (emit_type(emitter
, expr
->type
) < 0)
206 switch (expr
->type
) {
207 case pet_expr_double
:
208 if (emit_string(emitter
, "value") < 0)
210 if (emit_double(emitter
, expr
->d
) < 0)
213 case pet_expr_access
:
214 if (emit_string(emitter
, "relation") < 0)
216 if (emit_map(emitter
, expr
->acc
.access
) < 0)
218 if (emit_string(emitter
, "read") < 0)
220 if (emit_int(emitter
, expr
->acc
.read
) < 0)
222 if (emit_string(emitter
, "write") < 0)
224 if (emit_int(emitter
, expr
->acc
.write
) < 0)
228 case pet_expr_binary
:
229 if (emit_string(emitter
, "operation") < 0)
231 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
234 case pet_expr_ternary
:
237 if (emit_string(emitter
, "name") < 0)
239 if (emit_string(emitter
, expr
->name
) < 0)
244 if (expr
->n_arg
> 0) {
247 if (emit_string(emitter
, "arguments") < 0)
249 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
250 YAML_BLOCK_SEQUENCE_STYLE
))
252 if (!yaml_emitter_emit(emitter
, &event
))
255 for (i
= 0; i
< expr
->n_arg
; ++i
)
256 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
259 if (!yaml_sequence_end_event_initialize(&event
))
261 if (!yaml_emitter_emit(emitter
, &event
))
265 if (!yaml_mapping_end_event_initialize(&event
))
267 if (!yaml_emitter_emit(emitter
, &event
))
273 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
277 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
278 YAML_BLOCK_MAPPING_STYLE
))
280 if (!yaml_emitter_emit(emitter
, &event
))
283 if (emit_string(emitter
, "line") < 0)
285 if (emit_int(emitter
, stmt
->line
) < 0)
288 if (emit_string(emitter
, "domain") < 0)
290 if (emit_set(emitter
, stmt
->domain
) < 0)
293 if (emit_string(emitter
, "schedule") < 0)
295 if (emit_map(emitter
, stmt
->schedule
) < 0)
298 if (emit_string(emitter
, "body") < 0)
300 if (emit_expr(emitter
, stmt
->body
) < 0)
303 if (stmt
->n_arg
> 0) {
306 if (emit_string(emitter
, "arguments") < 0)
308 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
309 YAML_BLOCK_SEQUENCE_STYLE
))
311 if (!yaml_emitter_emit(emitter
, &event
))
314 for (i
= 0; i
< stmt
->n_arg
; ++i
)
315 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
318 if (!yaml_sequence_end_event_initialize(&event
))
320 if (!yaml_emitter_emit(emitter
, &event
))
324 if (!yaml_mapping_end_event_initialize(&event
))
326 if (!yaml_emitter_emit(emitter
, &event
))
332 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
333 struct pet_stmt
**stmts
)
338 if (emit_string(emitter
, "statements") < 0)
340 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
341 YAML_BLOCK_SEQUENCE_STYLE
))
343 if (!yaml_emitter_emit(emitter
, &event
))
346 for (i
= 0; i
< n_stmt
; ++i
)
347 if (emit_stmt(emitter
, stmts
[i
]) < 0)
350 if (!yaml_sequence_end_event_initialize(&event
))
352 if (!yaml_emitter_emit(emitter
, &event
))
358 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
362 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
363 YAML_BLOCK_MAPPING_STYLE
))
365 if (!yaml_emitter_emit(emitter
, &event
))
368 if (emit_string(emitter
, "context") < 0)
370 if (emit_set(emitter
, scop
->context
) < 0)
372 if (!isl_set_plain_is_universe(scop
->context_value
) &&
373 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
376 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
379 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
382 if (!yaml_mapping_end_event_initialize(&event
))
384 if (!yaml_emitter_emit(emitter
, &event
))
390 /* Print a YAML serialization of "scop" to "out".
392 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
394 yaml_emitter_t emitter
;
397 yaml_emitter_initialize(&emitter
);
399 yaml_emitter_set_output_file(&emitter
, out
);
401 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
402 if (!yaml_emitter_emit(&emitter
, &event
))
405 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
407 if (!yaml_emitter_emit(&emitter
, &event
))
410 if (emit_scop(&emitter
, scop
) < 0)
413 if (!yaml_document_end_event_initialize(&event
, 1))
415 if (!yaml_emitter_emit(&emitter
, &event
))
418 yaml_stream_end_event_initialize(&event
);
419 if (!yaml_emitter_emit(&emitter
, &event
))
422 yaml_emitter_delete(&emitter
);
425 yaml_emitter_delete(&emitter
);