wrap results of unsigned computations
[pet.git] / emit.c
blobf04d1a9017d15015dd71f61f1c8c47d3c25a234e
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 (!yaml_mapping_end_event_initialize(&event))
101 return -1;
102 if (!yaml_emitter_emit(emitter, &event))
103 return -1;
105 return 0;
108 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
109 struct pet_array **arrays)
111 int i;
112 yaml_event_t event;
114 if (emit_string(emitter, "arrays") < 0)
115 return -1;
116 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
117 YAML_BLOCK_SEQUENCE_STYLE))
118 return -1;
119 if (!yaml_emitter_emit(emitter, &event))
120 return -1;
122 for (i = 0; i < n_array; ++i)
123 if (emit_array(emitter, arrays[i]) < 0)
124 return -1;
126 if (!yaml_sequence_end_event_initialize(&event))
127 return -1;
128 if (!yaml_emitter_emit(emitter, &event))
129 return -1;
131 return 0;
134 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
136 if (emit_string(emitter, pet_type_str(type)) < 0)
137 return -1;
138 return 0;
141 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
143 yaml_event_t event;
145 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
146 YAML_BLOCK_MAPPING_STYLE))
147 return -1;
148 if (!yaml_emitter_emit(emitter, &event))
149 return -1;
151 if (emit_string(emitter, "type") < 0)
152 return -1;
153 if (emit_type(emitter, expr->type) < 0)
154 return -1;
156 switch (expr->type) {
157 case pet_expr_double:
158 if (emit_string(emitter, "value") < 0)
159 return -1;
160 if (emit_double(emitter, expr->d) < 0)
161 return -1;
162 break;
163 case pet_expr_access:
164 if (emit_string(emitter, "relation") < 0)
165 return -1;
166 if (emit_map(emitter, expr->acc.access) < 0)
167 return -1;
168 if (emit_string(emitter, "read") < 0)
169 return -1;
170 if (emit_int(emitter, expr->acc.read) < 0)
171 return -1;
172 if (emit_string(emitter, "write") < 0)
173 return -1;
174 if (emit_int(emitter, expr->acc.write) < 0)
175 return -1;
176 break;
177 case pet_expr_unary:
178 case pet_expr_binary:
179 if (emit_string(emitter, "operation") < 0)
180 return -1;
181 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
182 return -1;
183 break;
184 case pet_expr_ternary:
185 break;
186 case pet_expr_call:
187 if (emit_string(emitter, "name") < 0)
188 return -1;
189 if (emit_string(emitter, expr->name) < 0)
190 return -1;
191 break;
194 if (expr->n_arg > 0) {
195 int i;
197 if (emit_string(emitter, "arguments") < 0)
198 return -1;
199 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
200 YAML_BLOCK_SEQUENCE_STYLE))
201 return -1;
202 if (!yaml_emitter_emit(emitter, &event))
203 return -1;
205 for (i = 0; i < expr->n_arg; ++i)
206 if (emit_expr(emitter, expr->args[i]) < 0)
207 return -1;
209 if (!yaml_sequence_end_event_initialize(&event))
210 return -1;
211 if (!yaml_emitter_emit(emitter, &event))
212 return -1;
215 if (!yaml_mapping_end_event_initialize(&event))
216 return -1;
217 if (!yaml_emitter_emit(emitter, &event))
218 return -1;
220 return 0;
223 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
225 yaml_event_t event;
227 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
228 YAML_BLOCK_MAPPING_STYLE))
229 return -1;
230 if (!yaml_emitter_emit(emitter, &event))
231 return -1;
233 if (emit_string(emitter, "line") < 0)
234 return -1;
235 if (emit_int(emitter, stmt->line) < 0)
236 return -1;
238 if (emit_string(emitter, "domain") < 0)
239 return -1;
240 if (emit_set(emitter, stmt->domain) < 0)
241 return -1;
243 if (emit_string(emitter, "schedule") < 0)
244 return -1;
245 if (emit_map(emitter, stmt->schedule) < 0)
246 return -1;
248 if (emit_string(emitter, "body") < 0)
249 return -1;
250 if (emit_expr(emitter, stmt->body) < 0)
251 return -1;
253 if (!yaml_mapping_end_event_initialize(&event))
254 return -1;
255 if (!yaml_emitter_emit(emitter, &event))
256 return -1;
258 return 0;
261 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
262 struct pet_stmt **stmts)
264 int i;
265 yaml_event_t event;
267 if (emit_string(emitter, "statements") < 0)
268 return -1;
269 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
270 YAML_BLOCK_SEQUENCE_STYLE))
271 return -1;
272 if (!yaml_emitter_emit(emitter, &event))
273 return -1;
275 for (i = 0; i < n_stmt; ++i)
276 if (emit_stmt(emitter, stmts[i]) < 0)
277 return -1;
279 if (!yaml_sequence_end_event_initialize(&event))
280 return -1;
281 if (!yaml_emitter_emit(emitter, &event))
282 return -1;
284 return 0;
287 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
289 yaml_event_t event;
291 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
292 YAML_BLOCK_MAPPING_STYLE))
293 return -1;
294 if (!yaml_emitter_emit(emitter, &event))
295 return -1;
297 if (emit_string(emitter, "context") < 0)
298 return -1;
299 if (emit_set(emitter, scop->context) < 0)
300 return -1;
302 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
303 return -1;
305 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
306 return -1;
308 if (!yaml_mapping_end_event_initialize(&event))
309 return -1;
310 if (!yaml_emitter_emit(emitter, &event))
311 return -1;
313 return 0;
316 /* Print a YAML serialization of "scop" to "out".
318 int pet_scop_emit(FILE *out, struct pet_scop *scop)
320 yaml_emitter_t emitter;
321 yaml_event_t event;
323 yaml_emitter_initialize(&emitter);
325 yaml_emitter_set_output_file(&emitter, out);
327 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
328 if (!yaml_emitter_emit(&emitter, &event))
329 goto error;
331 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
332 goto error;
333 if (!yaml_emitter_emit(&emitter, &event))
334 goto error;
336 if (emit_scop(&emitter, scop) < 0)
337 goto error;
339 if (!yaml_document_end_event_initialize(&event, 1))
340 goto error;
341 if (!yaml_emitter_emit(&emitter, &event))
342 goto error;
344 yaml_stream_end_event_initialize(&event);
345 if (!yaml_emitter_emit(&emitter, &event))
346 goto error;
348 yaml_emitter_delete(&emitter);
349 return 0;
350 error:
351 yaml_emitter_delete(&emitter);
352 return -1;