update isl for rename of isl_map_insert
[pet.git] / emit.c
blob0729f1930551d7a40c3fcf477bcb2838c3489b24
1 #include <yaml.h>
3 #include "scop.h"
4 #include "scop_yaml.h"
6 static int emit_string(yaml_emitter_t *emitter, const char *str)
8 yaml_event_t event;
10 if (!yaml_scalar_event_initialize(&event, NULL, NULL,
11 (yaml_char_t *) str, strlen(str),
12 1, 1, YAML_PLAIN_SCALAR_STYLE))
13 return -1;
14 if (!yaml_emitter_emit(emitter, &event))
15 return -1;
17 return 0;
20 static int emit_int(yaml_emitter_t *emitter, int i)
22 char buffer[40];
24 snprintf(buffer, sizeof(buffer), "%d", i);
25 return emit_string(emitter, buffer);
28 static int emit_double(yaml_emitter_t *emitter, double d)
30 char buffer[40];
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);
39 isl_printer *p;
40 char *str;
41 int r;
43 p = isl_printer_to_str(ctx);
44 p = isl_printer_print_map(p, map);
45 str = isl_printer_get_str(p);
46 isl_printer_free(p);
47 r = emit_string(emitter, str);
48 free(str);
49 return r;
52 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
54 isl_ctx *ctx = isl_set_get_ctx(set);
55 isl_printer *p;
56 char *str;
57 int r;
59 p = isl_printer_to_str(ctx);
60 p = isl_printer_print_set(p, set);
61 str = isl_printer_get_str(p);
62 isl_printer_free(p);
63 r = emit_string(emitter, str);
64 free(str);
65 return r;
68 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
70 yaml_event_t event;
72 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
73 YAML_BLOCK_MAPPING_STYLE))
74 return -1;
75 if (!yaml_emitter_emit(emitter, &event))
76 return -1;
78 if (emit_string(emitter, "context") < 0)
79 return -1;
80 if (emit_set(emitter, array->context) < 0)
81 return -1;
83 if (emit_string(emitter, "extent") < 0)
84 return -1;
85 if (emit_set(emitter, array->extent) < 0)
86 return -1;
88 if (array->value_bounds) {
89 if (emit_string(emitter, "value_bounds") < 0)
90 return -1;
91 if (emit_set(emitter, array->value_bounds) < 0)
92 return -1;
95 if (emit_string(emitter, "element_type") < 0)
96 return -1;
97 if (emit_string(emitter, array->element_type) < 0)
98 return -1;
100 if (array->live_out) {
101 if (emit_string(emitter, "live_out") < 0)
102 return -1;
103 if (emit_string(emitter, "1") < 0)
104 return -1;
107 if (!yaml_mapping_end_event_initialize(&event))
108 return -1;
109 if (!yaml_emitter_emit(emitter, &event))
110 return -1;
112 return 0;
115 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
116 struct pet_array **arrays)
118 int i;
119 yaml_event_t event;
121 if (emit_string(emitter, "arrays") < 0)
122 return -1;
123 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
124 YAML_BLOCK_SEQUENCE_STYLE))
125 return -1;
126 if (!yaml_emitter_emit(emitter, &event))
127 return -1;
129 for (i = 0; i < n_array; ++i)
130 if (emit_array(emitter, arrays[i]) < 0)
131 return -1;
133 if (!yaml_sequence_end_event_initialize(&event))
134 return -1;
135 if (!yaml_emitter_emit(emitter, &event))
136 return -1;
138 return 0;
141 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
143 if (emit_string(emitter, pet_type_str(type)) < 0)
144 return -1;
145 return 0;
148 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
150 yaml_event_t event;
152 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
153 YAML_BLOCK_MAPPING_STYLE))
154 return -1;
155 if (!yaml_emitter_emit(emitter, &event))
156 return -1;
158 if (emit_string(emitter, "type") < 0)
159 return -1;
160 if (emit_type(emitter, expr->type) < 0)
161 return -1;
163 switch (expr->type) {
164 case pet_expr_double:
165 if (emit_string(emitter, "value") < 0)
166 return -1;
167 if (emit_double(emitter, expr->d) < 0)
168 return -1;
169 break;
170 case pet_expr_access:
171 if (emit_string(emitter, "relation") < 0)
172 return -1;
173 if (emit_map(emitter, expr->acc.access) < 0)
174 return -1;
175 if (emit_string(emitter, "read") < 0)
176 return -1;
177 if (emit_int(emitter, expr->acc.read) < 0)
178 return -1;
179 if (emit_string(emitter, "write") < 0)
180 return -1;
181 if (emit_int(emitter, expr->acc.write) < 0)
182 return -1;
183 break;
184 case pet_expr_unary:
185 case pet_expr_binary:
186 if (emit_string(emitter, "operation") < 0)
187 return -1;
188 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
189 return -1;
190 break;
191 case pet_expr_ternary:
192 break;
193 case pet_expr_call:
194 if (emit_string(emitter, "name") < 0)
195 return -1;
196 if (emit_string(emitter, expr->name) < 0)
197 return -1;
198 break;
201 if (expr->n_arg > 0) {
202 int i;
204 if (emit_string(emitter, "arguments") < 0)
205 return -1;
206 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
207 YAML_BLOCK_SEQUENCE_STYLE))
208 return -1;
209 if (!yaml_emitter_emit(emitter, &event))
210 return -1;
212 for (i = 0; i < expr->n_arg; ++i)
213 if (emit_expr(emitter, expr->args[i]) < 0)
214 return -1;
216 if (!yaml_sequence_end_event_initialize(&event))
217 return -1;
218 if (!yaml_emitter_emit(emitter, &event))
219 return -1;
222 if (!yaml_mapping_end_event_initialize(&event))
223 return -1;
224 if (!yaml_emitter_emit(emitter, &event))
225 return -1;
227 return 0;
230 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
232 yaml_event_t event;
234 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
235 YAML_BLOCK_MAPPING_STYLE))
236 return -1;
237 if (!yaml_emitter_emit(emitter, &event))
238 return -1;
240 if (emit_string(emitter, "line") < 0)
241 return -1;
242 if (emit_int(emitter, stmt->line) < 0)
243 return -1;
245 if (emit_string(emitter, "domain") < 0)
246 return -1;
247 if (emit_set(emitter, stmt->domain) < 0)
248 return -1;
250 if (emit_string(emitter, "schedule") < 0)
251 return -1;
252 if (emit_map(emitter, stmt->schedule) < 0)
253 return -1;
255 if (emit_string(emitter, "body") < 0)
256 return -1;
257 if (emit_expr(emitter, stmt->body) < 0)
258 return -1;
260 if (!yaml_mapping_end_event_initialize(&event))
261 return -1;
262 if (!yaml_emitter_emit(emitter, &event))
263 return -1;
265 return 0;
268 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
269 struct pet_stmt **stmts)
271 int i;
272 yaml_event_t event;
274 if (emit_string(emitter, "statements") < 0)
275 return -1;
276 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
277 YAML_BLOCK_SEQUENCE_STYLE))
278 return -1;
279 if (!yaml_emitter_emit(emitter, &event))
280 return -1;
282 for (i = 0; i < n_stmt; ++i)
283 if (emit_stmt(emitter, stmts[i]) < 0)
284 return -1;
286 if (!yaml_sequence_end_event_initialize(&event))
287 return -1;
288 if (!yaml_emitter_emit(emitter, &event))
289 return -1;
291 return 0;
294 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
296 yaml_event_t event;
298 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
299 YAML_BLOCK_MAPPING_STYLE))
300 return -1;
301 if (!yaml_emitter_emit(emitter, &event))
302 return -1;
304 if (emit_string(emitter, "context") < 0)
305 return -1;
306 if (emit_set(emitter, scop->context) < 0)
307 return -1;
309 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
310 return -1;
312 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
313 return -1;
315 if (!yaml_mapping_end_event_initialize(&event))
316 return -1;
317 if (!yaml_emitter_emit(emitter, &event))
318 return -1;
320 return 0;
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;
328 yaml_event_t event;
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))
336 goto error;
338 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
339 goto error;
340 if (!yaml_emitter_emit(&emitter, &event))
341 goto error;
343 if (emit_scop(&emitter, scop) < 0)
344 goto error;
346 if (!yaml_document_end_event_initialize(&event, 1))
347 goto error;
348 if (!yaml_emitter_emit(&emitter, &event))
349 goto error;
351 yaml_stream_end_event_initialize(&event);
352 if (!yaml_emitter_emit(&emitter, &event))
353 goto error;
355 yaml_emitter_delete(&emitter);
356 return 0;
357 error:
358 yaml_emitter_delete(&emitter);
359 return -1;