6 static int emit_string(yaml_emitter_t
*emitter
, const char *str
)
10 if (!yaml_scalar_event_initialize(&event
, NULL
, NULL
,
11 (yaml_char_t
*) str
, strlen(str
),
12 1, 1, YAML_PLAIN_SCALAR_STYLE
))
14 if (!yaml_emitter_emit(emitter
, &event
))
20 static int emit_int(yaml_emitter_t
*emitter
, int i
)
24 snprintf(buffer
, sizeof(buffer
), "%d", i
);
25 return emit_string(emitter
, buffer
);
28 static int emit_double(yaml_emitter_t
*emitter
, double d
)
32 snprintf(buffer
, sizeof(buffer
), "%g", d
);
33 return emit_string(emitter
, buffer
);
36 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
38 isl_ctx
*ctx
= isl_map_get_ctx(map
);
43 p
= isl_printer_to_str(ctx
);
44 p
= isl_printer_print_map(p
, map
);
45 str
= isl_printer_get_str(p
);
47 r
= emit_string(emitter
, str
);
52 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
54 isl_ctx
*ctx
= isl_set_get_ctx(set
);
59 p
= isl_printer_to_str(ctx
);
60 p
= isl_printer_print_set(p
, set
);
61 str
= isl_printer_get_str(p
);
63 r
= emit_string(emitter
, str
);
68 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
72 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
73 YAML_BLOCK_MAPPING_STYLE
))
75 if (!yaml_emitter_emit(emitter
, &event
))
78 if (emit_string(emitter
, "context") < 0)
80 if (emit_set(emitter
, array
->context
) < 0)
83 if (emit_string(emitter
, "extent") < 0)
85 if (emit_set(emitter
, array
->extent
) < 0)
88 if (array
->value_bounds
) {
89 if (emit_string(emitter
, "value_bounds") < 0)
91 if (emit_set(emitter
, array
->value_bounds
) < 0)
95 if (emit_string(emitter
, "element_type") < 0)
97 if (emit_string(emitter
, array
->element_type
) < 0)
100 if (array
->live_out
) {
101 if (emit_string(emitter
, "live_out") < 0)
103 if (emit_string(emitter
, "1") < 0)
107 if (!yaml_mapping_end_event_initialize(&event
))
109 if (!yaml_emitter_emit(emitter
, &event
))
115 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
116 struct pet_array
**arrays
)
121 if (emit_string(emitter
, "arrays") < 0)
123 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
124 YAML_BLOCK_SEQUENCE_STYLE
))
126 if (!yaml_emitter_emit(emitter
, &event
))
129 for (i
= 0; i
< n_array
; ++i
)
130 if (emit_array(emitter
, arrays
[i
]) < 0)
133 if (!yaml_sequence_end_event_initialize(&event
))
135 if (!yaml_emitter_emit(emitter
, &event
))
141 static int emit_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
143 if (emit_string(emitter
, pet_type_str(type
)) < 0)
148 static int emit_expr(yaml_emitter_t
*emitter
, struct pet_expr
*expr
)
152 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
153 YAML_BLOCK_MAPPING_STYLE
))
155 if (!yaml_emitter_emit(emitter
, &event
))
158 if (emit_string(emitter
, "type") < 0)
160 if (emit_type(emitter
, expr
->type
) < 0)
163 switch (expr
->type
) {
164 case pet_expr_double
:
165 if (emit_string(emitter
, "value") < 0)
167 if (emit_double(emitter
, expr
->d
) < 0)
170 case pet_expr_access
:
171 if (emit_string(emitter
, "relation") < 0)
173 if (emit_map(emitter
, expr
->acc
.access
) < 0)
175 if (emit_string(emitter
, "read") < 0)
177 if (emit_int(emitter
, expr
->acc
.read
) < 0)
179 if (emit_string(emitter
, "write") < 0)
181 if (emit_int(emitter
, expr
->acc
.write
) < 0)
185 case pet_expr_binary
:
186 if (emit_string(emitter
, "operation") < 0)
188 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
191 case pet_expr_ternary
:
194 if (emit_string(emitter
, "name") < 0)
196 if (emit_string(emitter
, expr
->name
) < 0)
201 if (expr
->n_arg
> 0) {
204 if (emit_string(emitter
, "arguments") < 0)
206 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
207 YAML_BLOCK_SEQUENCE_STYLE
))
209 if (!yaml_emitter_emit(emitter
, &event
))
212 for (i
= 0; i
< expr
->n_arg
; ++i
)
213 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
216 if (!yaml_sequence_end_event_initialize(&event
))
218 if (!yaml_emitter_emit(emitter
, &event
))
222 if (!yaml_mapping_end_event_initialize(&event
))
224 if (!yaml_emitter_emit(emitter
, &event
))
230 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
234 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
235 YAML_BLOCK_MAPPING_STYLE
))
237 if (!yaml_emitter_emit(emitter
, &event
))
240 if (emit_string(emitter
, "line") < 0)
242 if (emit_int(emitter
, stmt
->line
) < 0)
245 if (emit_string(emitter
, "domain") < 0)
247 if (emit_set(emitter
, stmt
->domain
) < 0)
250 if (emit_string(emitter
, "schedule") < 0)
252 if (emit_map(emitter
, stmt
->schedule
) < 0)
255 if (emit_string(emitter
, "body") < 0)
257 if (emit_expr(emitter
, stmt
->body
) < 0)
260 if (!yaml_mapping_end_event_initialize(&event
))
262 if (!yaml_emitter_emit(emitter
, &event
))
268 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
269 struct pet_stmt
**stmts
)
274 if (emit_string(emitter
, "statements") < 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
< n_stmt
; ++i
)
283 if (emit_stmt(emitter
, stmts
[i
]) < 0)
286 if (!yaml_sequence_end_event_initialize(&event
))
288 if (!yaml_emitter_emit(emitter
, &event
))
294 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
298 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
299 YAML_BLOCK_MAPPING_STYLE
))
301 if (!yaml_emitter_emit(emitter
, &event
))
304 if (emit_string(emitter
, "context") < 0)
306 if (emit_set(emitter
, scop
->context
) < 0)
309 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
312 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
315 if (!yaml_mapping_end_event_initialize(&event
))
317 if (!yaml_emitter_emit(emitter
, &event
))
323 /* Print a YAML serialization of "scop" to "out".
325 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
327 yaml_emitter_t emitter
;
330 yaml_emitter_initialize(&emitter
);
332 yaml_emitter_set_output_file(&emitter
, out
);
334 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
335 if (!yaml_emitter_emit(&emitter
, &event
))
338 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
340 if (!yaml_emitter_emit(&emitter
, &event
))
343 if (emit_scop(&emitter
, scop
) < 0)
346 if (!yaml_document_end_event_initialize(&event
, 1))
348 if (!yaml_emitter_emit(&emitter
, &event
))
351 yaml_stream_end_event_initialize(&event
);
352 if (!yaml_emitter_emit(&emitter
, &event
))
355 yaml_emitter_delete(&emitter
);
358 yaml_emitter_delete(&emitter
);