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 /* Print the unsigned integer "u" to "emitter".
72 static int emit_unsigned(yaml_emitter_t
*emitter
, unsigned u
)
76 snprintf(buffer
, sizeof(buffer
), "%u", u
);
77 return emit_string(emitter
, buffer
);
80 /* Print the string "name" and the unsigned integer "u" to "emitter".
82 static int emit_named_unsigned(yaml_emitter_t
*emitter
, const char *name
,
85 if (emit_string(emitter
, name
) < 0)
87 if (emit_int(emitter
, u
) < 0)
92 static int emit_double(yaml_emitter_t
*emitter
, double d
)
96 snprintf(buffer
, sizeof(buffer
), "%g", d
);
97 return emit_string(emitter
, buffer
);
100 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
102 isl_ctx
*ctx
= isl_map_get_ctx(map
);
107 p
= isl_printer_to_str(ctx
);
108 p
= isl_printer_print_map(p
, map
);
109 str
= isl_printer_get_str(p
);
111 r
= emit_string(emitter
, str
);
116 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
118 isl_ctx
*ctx
= isl_set_get_ctx(set
);
123 p
= isl_printer_to_str(ctx
);
124 p
= isl_printer_print_set(p
, set
);
125 str
= isl_printer_get_str(p
);
127 r
= emit_string(emitter
, str
);
132 static int emit_named_set(yaml_emitter_t
*emitter
, const char *name
,
133 __isl_keep isl_set
*set
)
135 if (emit_string(emitter
, name
) < 0)
137 if (emit_set(emitter
, set
) < 0)
142 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
146 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
147 YAML_BLOCK_MAPPING_STYLE
))
149 if (!yaml_emitter_emit(emitter
, &event
))
152 if (emit_string(emitter
, "context") < 0)
154 if (emit_set(emitter
, array
->context
) < 0)
157 if (emit_string(emitter
, "extent") < 0)
159 if (emit_set(emitter
, array
->extent
) < 0)
162 if (array
->value_bounds
) {
163 if (emit_string(emitter
, "value_bounds") < 0)
165 if (emit_set(emitter
, array
->value_bounds
) < 0)
169 if (emit_string(emitter
, "element_type") < 0)
171 if (emit_string(emitter
, array
->element_type
) < 0)
173 if (emit_named_int(emitter
, "element_size", array
->element_size
) < 0)
176 if (array
->live_out
) {
177 if (emit_string(emitter
, "live_out") < 0)
179 if (emit_string(emitter
, "1") < 0)
183 if (array
->uniquely_defined
) {
184 if (emit_string(emitter
, "uniquely_defined") < 0)
186 if (emit_string(emitter
, "1") < 0)
190 if (array
->declared
&& emit_named_int(emitter
, "declared", 1) < 0)
192 if (array
->exposed
&& emit_named_int(emitter
, "exposed", 1) < 0)
195 if (!yaml_mapping_end_event_initialize(&event
))
197 if (!yaml_emitter_emit(emitter
, &event
))
203 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
204 struct pet_array
**arrays
)
209 if (emit_string(emitter
, "arrays") < 0)
211 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
212 YAML_BLOCK_SEQUENCE_STYLE
))
214 if (!yaml_emitter_emit(emitter
, &event
))
217 for (i
= 0; i
< n_array
; ++i
)
218 if (emit_array(emitter
, arrays
[i
]) < 0)
221 if (!yaml_sequence_end_event_initialize(&event
))
223 if (!yaml_emitter_emit(emitter
, &event
))
229 static int emit_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
231 if (emit_string(emitter
, pet_type_str(type
)) < 0)
236 static int emit_expr(yaml_emitter_t
*emitter
, struct pet_expr
*expr
)
240 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
241 YAML_BLOCK_MAPPING_STYLE
))
243 if (!yaml_emitter_emit(emitter
, &event
))
246 if (emit_string(emitter
, "type") < 0)
248 if (emit_type(emitter
, expr
->type
) < 0)
251 switch (expr
->type
) {
252 case pet_expr_double
:
253 if (emit_string(emitter
, "value") < 0)
255 if (emit_double(emitter
, expr
->d
.val
) < 0)
257 if (emit_string(emitter
, "string") < 0)
259 if (emit_string(emitter
, expr
->d
.s
) < 0)
262 case pet_expr_access
:
263 if (emit_string(emitter
, "relation") < 0)
265 if (emit_map(emitter
, expr
->acc
.access
) < 0)
267 if (emit_string(emitter
, "read") < 0)
269 if (emit_int(emitter
, expr
->acc
.read
) < 0)
271 if (emit_string(emitter
, "write") < 0)
273 if (emit_int(emitter
, expr
->acc
.write
) < 0)
277 case pet_expr_binary
:
278 if (emit_string(emitter
, "operation") < 0)
280 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
283 case pet_expr_ternary
:
286 if (emit_string(emitter
, "name") < 0)
288 if (emit_string(emitter
, expr
->name
) < 0)
292 if (emit_string(emitter
, "type_name") < 0)
294 if (emit_string(emitter
, expr
->type_name
) < 0)
299 if (expr
->n_arg
> 0) {
302 if (emit_string(emitter
, "arguments") < 0)
304 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
305 YAML_BLOCK_SEQUENCE_STYLE
))
307 if (!yaml_emitter_emit(emitter
, &event
))
310 for (i
= 0; i
< expr
->n_arg
; ++i
)
311 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
314 if (!yaml_sequence_end_event_initialize(&event
))
316 if (!yaml_emitter_emit(emitter
, &event
))
320 if (!yaml_mapping_end_event_initialize(&event
))
322 if (!yaml_emitter_emit(emitter
, &event
))
328 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
332 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
333 YAML_BLOCK_MAPPING_STYLE
))
335 if (!yaml_emitter_emit(emitter
, &event
))
338 if (emit_string(emitter
, "line") < 0)
340 if (emit_int(emitter
, stmt
->line
) < 0)
343 if (emit_string(emitter
, "domain") < 0)
345 if (emit_set(emitter
, stmt
->domain
) < 0)
348 if (emit_string(emitter
, "schedule") < 0)
350 if (emit_map(emitter
, stmt
->schedule
) < 0)
353 if (emit_string(emitter
, "body") < 0)
355 if (emit_expr(emitter
, stmt
->body
) < 0)
358 if (stmt
->n_arg
> 0) {
361 if (emit_string(emitter
, "arguments") < 0)
363 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
364 YAML_BLOCK_SEQUENCE_STYLE
))
366 if (!yaml_emitter_emit(emitter
, &event
))
369 for (i
= 0; i
< stmt
->n_arg
; ++i
)
370 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
373 if (!yaml_sequence_end_event_initialize(&event
))
375 if (!yaml_emitter_emit(emitter
, &event
))
379 if (!yaml_mapping_end_event_initialize(&event
))
381 if (!yaml_emitter_emit(emitter
, &event
))
387 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
388 struct pet_stmt
**stmts
)
393 if (emit_string(emitter
, "statements") < 0)
395 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
396 YAML_BLOCK_SEQUENCE_STYLE
))
398 if (!yaml_emitter_emit(emitter
, &event
))
401 for (i
= 0; i
< n_stmt
; ++i
)
402 if (emit_stmt(emitter
, stmts
[i
]) < 0)
405 if (!yaml_sequence_end_event_initialize(&event
))
407 if (!yaml_emitter_emit(emitter
, &event
))
413 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
417 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
418 YAML_BLOCK_MAPPING_STYLE
))
420 if (!yaml_emitter_emit(emitter
, &event
))
423 if (emit_named_unsigned(emitter
, "start", scop
->start
) < 0)
425 if (emit_named_unsigned(emitter
, "end", scop
->end
) < 0)
427 if (emit_string(emitter
, "context") < 0)
429 if (emit_set(emitter
, scop
->context
) < 0)
431 if (!isl_set_plain_is_universe(scop
->context_value
) &&
432 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
435 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
438 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
441 if (!yaml_mapping_end_event_initialize(&event
))
443 if (!yaml_emitter_emit(emitter
, &event
))
449 /* Print a YAML serialization of "scop" to "out".
451 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
453 yaml_emitter_t emitter
;
456 yaml_emitter_initialize(&emitter
);
458 yaml_emitter_set_output_file(&emitter
, out
);
460 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
461 if (!yaml_emitter_emit(&emitter
, &event
))
464 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
466 if (!yaml_emitter_emit(&emitter
, &event
))
469 if (emit_scop(&emitter
, scop
) < 0)
472 if (!yaml_document_end_event_initialize(&event
, 1))
474 if (!yaml_emitter_emit(&emitter
, &event
))
477 yaml_stream_end_event_initialize(&event
);
478 if (!yaml_emitter_emit(&emitter
, &event
))
481 yaml_emitter_delete(&emitter
);
484 yaml_emitter_delete(&emitter
);