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_named_int(yaml_emitter_t
*emitter
, const char *name
, int i
)
63 if (emit_string(emitter
, name
) < 0)
65 if (emit_int(emitter
, i
) < 0)
70 static int emit_double(yaml_emitter_t
*emitter
, double d
)
74 snprintf(buffer
, sizeof(buffer
), "%g", d
);
75 return emit_string(emitter
, buffer
);
78 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
80 isl_ctx
*ctx
= isl_map_get_ctx(map
);
85 p
= isl_printer_to_str(ctx
);
86 p
= isl_printer_print_map(p
, map
);
87 str
= isl_printer_get_str(p
);
89 r
= emit_string(emitter
, str
);
94 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
96 isl_ctx
*ctx
= isl_set_get_ctx(set
);
101 p
= isl_printer_to_str(ctx
);
102 p
= isl_printer_print_set(p
, set
);
103 str
= isl_printer_get_str(p
);
105 r
= emit_string(emitter
, str
);
110 static int emit_named_set(yaml_emitter_t
*emitter
, const char *name
,
111 __isl_keep isl_set
*set
)
113 if (emit_string(emitter
, name
) < 0)
115 if (emit_set(emitter
, set
) < 0)
120 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
124 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
125 YAML_BLOCK_MAPPING_STYLE
))
127 if (!yaml_emitter_emit(emitter
, &event
))
130 if (emit_string(emitter
, "context") < 0)
132 if (emit_set(emitter
, array
->context
) < 0)
135 if (emit_string(emitter
, "extent") < 0)
137 if (emit_set(emitter
, array
->extent
) < 0)
140 if (array
->value_bounds
) {
141 if (emit_string(emitter
, "value_bounds") < 0)
143 if (emit_set(emitter
, array
->value_bounds
) < 0)
147 if (emit_string(emitter
, "element_type") < 0)
149 if (emit_string(emitter
, array
->element_type
) < 0)
151 if (emit_named_int(emitter
, "element_size", array
->element_size
) < 0)
154 if (array
->live_out
) {
155 if (emit_string(emitter
, "live_out") < 0)
157 if (emit_string(emitter
, "1") < 0)
161 if (array
->uniquely_defined
) {
162 if (emit_string(emitter
, "uniquely_defined") < 0)
164 if (emit_string(emitter
, "1") < 0)
168 if (array
->declared
&& emit_named_int(emitter
, "declared", 1) < 0)
170 if (array
->exposed
&& emit_named_int(emitter
, "exposed", 1) < 0)
173 if (!yaml_mapping_end_event_initialize(&event
))
175 if (!yaml_emitter_emit(emitter
, &event
))
181 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
182 struct pet_array
**arrays
)
187 if (emit_string(emitter
, "arrays") < 0)
189 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
190 YAML_BLOCK_SEQUENCE_STYLE
))
192 if (!yaml_emitter_emit(emitter
, &event
))
195 for (i
= 0; i
< n_array
; ++i
)
196 if (emit_array(emitter
, arrays
[i
]) < 0)
199 if (!yaml_sequence_end_event_initialize(&event
))
201 if (!yaml_emitter_emit(emitter
, &event
))
207 static int emit_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
209 if (emit_string(emitter
, pet_type_str(type
)) < 0)
214 static int emit_expr(yaml_emitter_t
*emitter
, struct pet_expr
*expr
)
218 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
219 YAML_BLOCK_MAPPING_STYLE
))
221 if (!yaml_emitter_emit(emitter
, &event
))
224 if (emit_string(emitter
, "type") < 0)
226 if (emit_type(emitter
, expr
->type
) < 0)
229 switch (expr
->type
) {
230 case pet_expr_double
:
231 if (emit_string(emitter
, "value") < 0)
233 if (emit_double(emitter
, expr
->d
.val
) < 0)
235 if (emit_string(emitter
, "string") < 0)
237 if (emit_string(emitter
, expr
->d
.s
) < 0)
240 case pet_expr_access
:
241 if (emit_string(emitter
, "relation") < 0)
243 if (emit_map(emitter
, expr
->acc
.access
) < 0)
245 if (emit_string(emitter
, "read") < 0)
247 if (emit_int(emitter
, expr
->acc
.read
) < 0)
249 if (emit_string(emitter
, "write") < 0)
251 if (emit_int(emitter
, expr
->acc
.write
) < 0)
255 case pet_expr_binary
:
256 if (emit_string(emitter
, "operation") < 0)
258 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
261 case pet_expr_ternary
:
264 if (emit_string(emitter
, "name") < 0)
266 if (emit_string(emitter
, expr
->name
) < 0)
271 if (expr
->n_arg
> 0) {
274 if (emit_string(emitter
, "arguments") < 0)
276 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
277 YAML_BLOCK_SEQUENCE_STYLE
))
279 if (!yaml_emitter_emit(emitter
, &event
))
282 for (i
= 0; i
< expr
->n_arg
; ++i
)
283 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
286 if (!yaml_sequence_end_event_initialize(&event
))
288 if (!yaml_emitter_emit(emitter
, &event
))
292 if (!yaml_mapping_end_event_initialize(&event
))
294 if (!yaml_emitter_emit(emitter
, &event
))
300 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
304 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
305 YAML_BLOCK_MAPPING_STYLE
))
307 if (!yaml_emitter_emit(emitter
, &event
))
310 if (emit_string(emitter
, "line") < 0)
312 if (emit_int(emitter
, stmt
->line
) < 0)
315 if (emit_string(emitter
, "domain") < 0)
317 if (emit_set(emitter
, stmt
->domain
) < 0)
320 if (emit_string(emitter
, "schedule") < 0)
322 if (emit_map(emitter
, stmt
->schedule
) < 0)
325 if (emit_string(emitter
, "body") < 0)
327 if (emit_expr(emitter
, stmt
->body
) < 0)
330 if (stmt
->n_arg
> 0) {
333 if (emit_string(emitter
, "arguments") < 0)
335 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
336 YAML_BLOCK_SEQUENCE_STYLE
))
338 if (!yaml_emitter_emit(emitter
, &event
))
341 for (i
= 0; i
< stmt
->n_arg
; ++i
)
342 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
345 if (!yaml_sequence_end_event_initialize(&event
))
347 if (!yaml_emitter_emit(emitter
, &event
))
351 if (!yaml_mapping_end_event_initialize(&event
))
353 if (!yaml_emitter_emit(emitter
, &event
))
359 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
360 struct pet_stmt
**stmts
)
365 if (emit_string(emitter
, "statements") < 0)
367 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
368 YAML_BLOCK_SEQUENCE_STYLE
))
370 if (!yaml_emitter_emit(emitter
, &event
))
373 for (i
= 0; i
< n_stmt
; ++i
)
374 if (emit_stmt(emitter
, stmts
[i
]) < 0)
377 if (!yaml_sequence_end_event_initialize(&event
))
379 if (!yaml_emitter_emit(emitter
, &event
))
385 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
389 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
390 YAML_BLOCK_MAPPING_STYLE
))
392 if (!yaml_emitter_emit(emitter
, &event
))
395 if (emit_string(emitter
, "context") < 0)
397 if (emit_set(emitter
, scop
->context
) < 0)
399 if (!isl_set_plain_is_universe(scop
->context_value
) &&
400 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
403 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
406 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
409 if (!yaml_mapping_end_event_initialize(&event
))
411 if (!yaml_emitter_emit(emitter
, &event
))
417 /* Print a YAML serialization of "scop" to "out".
419 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
421 yaml_emitter_t emitter
;
424 yaml_emitter_initialize(&emitter
);
426 yaml_emitter_set_output_file(&emitter
, out
);
428 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
429 if (!yaml_emitter_emit(&emitter
, &event
))
432 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
434 if (!yaml_emitter_emit(&emitter
, &event
))
437 if (emit_scop(&emitter
, scop
) < 0)
440 if (!yaml_document_end_event_initialize(&event
, 1))
442 if (!yaml_emitter_emit(&emitter
, &event
))
445 yaml_stream_end_event_initialize(&event
);
446 if (!yaml_emitter_emit(&emitter
, &event
))
449 yaml_emitter_delete(&emitter
);
452 yaml_emitter_delete(&emitter
);