scop_add_break: try and merge filters
[pet.git] / emit.c
blobe2371f30f97f269b2116653a1cc40158fd56a1bb
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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
31 * Leiden University.
32 */
34 #include <yaml.h>
36 #include "scop.h"
37 #include "scop_yaml.h"
39 static int emit_string(yaml_emitter_t *emitter, const char *str)
41 yaml_event_t event;
43 if (!yaml_scalar_event_initialize(&event, NULL, NULL,
44 (yaml_char_t *) str, strlen(str),
45 1, 1, YAML_PLAIN_SCALAR_STYLE))
46 return -1;
47 if (!yaml_emitter_emit(emitter, &event))
48 return -1;
50 return 0;
53 static int emit_int(yaml_emitter_t *emitter, int i)
55 char buffer[40];
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)
64 return -1;
65 if (emit_int(emitter, i) < 0)
66 return -1;
67 return 0;
70 static int emit_double(yaml_emitter_t *emitter, double d)
72 char buffer[40];
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);
81 isl_printer *p;
82 char *str;
83 int r;
85 p = isl_printer_to_str(ctx);
86 p = isl_printer_print_map(p, map);
87 str = isl_printer_get_str(p);
88 isl_printer_free(p);
89 r = emit_string(emitter, str);
90 free(str);
91 return r;
94 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
96 isl_ctx *ctx = isl_set_get_ctx(set);
97 isl_printer *p;
98 char *str;
99 int r;
101 p = isl_printer_to_str(ctx);
102 p = isl_printer_print_set(p, set);
103 str = isl_printer_get_str(p);
104 isl_printer_free(p);
105 r = emit_string(emitter, str);
106 free(str);
107 return r;
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)
114 return -1;
115 if (emit_set(emitter, set) < 0)
116 return -1;
117 return 0;
120 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
122 yaml_event_t event;
124 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
125 YAML_BLOCK_MAPPING_STYLE))
126 return -1;
127 if (!yaml_emitter_emit(emitter, &event))
128 return -1;
130 if (emit_string(emitter, "context") < 0)
131 return -1;
132 if (emit_set(emitter, array->context) < 0)
133 return -1;
135 if (emit_string(emitter, "extent") < 0)
136 return -1;
137 if (emit_set(emitter, array->extent) < 0)
138 return -1;
140 if (array->value_bounds) {
141 if (emit_string(emitter, "value_bounds") < 0)
142 return -1;
143 if (emit_set(emitter, array->value_bounds) < 0)
144 return -1;
147 if (emit_string(emitter, "element_type") < 0)
148 return -1;
149 if (emit_string(emitter, array->element_type) < 0)
150 return -1;
151 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
152 return -1;
154 if (array->live_out) {
155 if (emit_string(emitter, "live_out") < 0)
156 return -1;
157 if (emit_string(emitter, "1") < 0)
158 return -1;
161 if (array->uniquely_defined) {
162 if (emit_string(emitter, "uniquely_defined") < 0)
163 return -1;
164 if (emit_string(emitter, "1") < 0)
165 return -1;
168 if (!yaml_mapping_end_event_initialize(&event))
169 return -1;
170 if (!yaml_emitter_emit(emitter, &event))
171 return -1;
173 return 0;
176 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
177 struct pet_array **arrays)
179 int i;
180 yaml_event_t event;
182 if (emit_string(emitter, "arrays") < 0)
183 return -1;
184 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
185 YAML_BLOCK_SEQUENCE_STYLE))
186 return -1;
187 if (!yaml_emitter_emit(emitter, &event))
188 return -1;
190 for (i = 0; i < n_array; ++i)
191 if (emit_array(emitter, arrays[i]) < 0)
192 return -1;
194 if (!yaml_sequence_end_event_initialize(&event))
195 return -1;
196 if (!yaml_emitter_emit(emitter, &event))
197 return -1;
199 return 0;
202 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
204 if (emit_string(emitter, pet_type_str(type)) < 0)
205 return -1;
206 return 0;
209 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
211 yaml_event_t event;
213 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
214 YAML_BLOCK_MAPPING_STYLE))
215 return -1;
216 if (!yaml_emitter_emit(emitter, &event))
217 return -1;
219 if (emit_string(emitter, "type") < 0)
220 return -1;
221 if (emit_type(emitter, expr->type) < 0)
222 return -1;
224 switch (expr->type) {
225 case pet_expr_double:
226 if (emit_string(emitter, "value") < 0)
227 return -1;
228 if (emit_double(emitter, expr->d) < 0)
229 return -1;
230 break;
231 case pet_expr_access:
232 if (emit_string(emitter, "relation") < 0)
233 return -1;
234 if (emit_map(emitter, expr->acc.access) < 0)
235 return -1;
236 if (emit_string(emitter, "read") < 0)
237 return -1;
238 if (emit_int(emitter, expr->acc.read) < 0)
239 return -1;
240 if (emit_string(emitter, "write") < 0)
241 return -1;
242 if (emit_int(emitter, expr->acc.write) < 0)
243 return -1;
244 break;
245 case pet_expr_unary:
246 case pet_expr_binary:
247 if (emit_string(emitter, "operation") < 0)
248 return -1;
249 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
250 return -1;
251 break;
252 case pet_expr_ternary:
253 break;
254 case pet_expr_call:
255 if (emit_string(emitter, "name") < 0)
256 return -1;
257 if (emit_string(emitter, expr->name) < 0)
258 return -1;
259 break;
262 if (expr->n_arg > 0) {
263 int i;
265 if (emit_string(emitter, "arguments") < 0)
266 return -1;
267 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
268 YAML_BLOCK_SEQUENCE_STYLE))
269 return -1;
270 if (!yaml_emitter_emit(emitter, &event))
271 return -1;
273 for (i = 0; i < expr->n_arg; ++i)
274 if (emit_expr(emitter, expr->args[i]) < 0)
275 return -1;
277 if (!yaml_sequence_end_event_initialize(&event))
278 return -1;
279 if (!yaml_emitter_emit(emitter, &event))
280 return -1;
283 if (!yaml_mapping_end_event_initialize(&event))
284 return -1;
285 if (!yaml_emitter_emit(emitter, &event))
286 return -1;
288 return 0;
291 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
293 yaml_event_t event;
295 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
296 YAML_BLOCK_MAPPING_STYLE))
297 return -1;
298 if (!yaml_emitter_emit(emitter, &event))
299 return -1;
301 if (emit_string(emitter, "line") < 0)
302 return -1;
303 if (emit_int(emitter, stmt->line) < 0)
304 return -1;
306 if (emit_string(emitter, "domain") < 0)
307 return -1;
308 if (emit_set(emitter, stmt->domain) < 0)
309 return -1;
311 if (emit_string(emitter, "schedule") < 0)
312 return -1;
313 if (emit_map(emitter, stmt->schedule) < 0)
314 return -1;
316 if (emit_string(emitter, "body") < 0)
317 return -1;
318 if (emit_expr(emitter, stmt->body) < 0)
319 return -1;
321 if (stmt->n_arg > 0) {
322 int i;
324 if (emit_string(emitter, "arguments") < 0)
325 return -1;
326 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
327 YAML_BLOCK_SEQUENCE_STYLE))
328 return -1;
329 if (!yaml_emitter_emit(emitter, &event))
330 return -1;
332 for (i = 0; i < stmt->n_arg; ++i)
333 if (emit_expr(emitter, stmt->args[i]) < 0)
334 return -1;
336 if (!yaml_sequence_end_event_initialize(&event))
337 return -1;
338 if (!yaml_emitter_emit(emitter, &event))
339 return -1;
342 if (!yaml_mapping_end_event_initialize(&event))
343 return -1;
344 if (!yaml_emitter_emit(emitter, &event))
345 return -1;
347 return 0;
350 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
351 struct pet_stmt **stmts)
353 int i;
354 yaml_event_t event;
356 if (emit_string(emitter, "statements") < 0)
357 return -1;
358 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
359 YAML_BLOCK_SEQUENCE_STYLE))
360 return -1;
361 if (!yaml_emitter_emit(emitter, &event))
362 return -1;
364 for (i = 0; i < n_stmt; ++i)
365 if (emit_stmt(emitter, stmts[i]) < 0)
366 return -1;
368 if (!yaml_sequence_end_event_initialize(&event))
369 return -1;
370 if (!yaml_emitter_emit(emitter, &event))
371 return -1;
373 return 0;
376 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
378 yaml_event_t event;
380 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
381 YAML_BLOCK_MAPPING_STYLE))
382 return -1;
383 if (!yaml_emitter_emit(emitter, &event))
384 return -1;
386 if (emit_string(emitter, "context") < 0)
387 return -1;
388 if (emit_set(emitter, scop->context) < 0)
389 return -1;
390 if (!isl_set_plain_is_universe(scop->context_value) &&
391 emit_named_set(emitter, "context_value", scop->context_value) < 0)
392 return -1;
394 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
395 return -1;
397 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
398 return -1;
400 if (!yaml_mapping_end_event_initialize(&event))
401 return -1;
402 if (!yaml_emitter_emit(emitter, &event))
403 return -1;
405 return 0;
408 /* Print a YAML serialization of "scop" to "out".
410 int pet_scop_emit(FILE *out, struct pet_scop *scop)
412 yaml_emitter_t emitter;
413 yaml_event_t event;
415 yaml_emitter_initialize(&emitter);
417 yaml_emitter_set_output_file(&emitter, out);
419 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
420 if (!yaml_emitter_emit(&emitter, &event))
421 goto error;
423 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
424 goto error;
425 if (!yaml_emitter_emit(&emitter, &event))
426 goto error;
428 if (emit_scop(&emitter, scop) < 0)
429 goto error;
431 if (!yaml_document_end_event_initialize(&event, 1))
432 goto error;
433 if (!yaml_emitter_emit(&emitter, &event))
434 goto error;
436 yaml_stream_end_event_initialize(&event);
437 if (!yaml_emitter_emit(&emitter, &event))
438 goto error;
440 yaml_emitter_delete(&emitter);
441 return 0;
442 error:
443 yaml_emitter_delete(&emitter);
444 return -1;