update isl for change in isl_pw_aff_cond
[pet.git] / emit.c
blobd1e07f1f8be0b74b34f60805ac5f22865ad6b6e6
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 (!yaml_mapping_end_event_initialize(&event))
162 return -1;
163 if (!yaml_emitter_emit(emitter, &event))
164 return -1;
166 return 0;
169 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
170 struct pet_array **arrays)
172 int i;
173 yaml_event_t event;
175 if (emit_string(emitter, "arrays") < 0)
176 return -1;
177 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
178 YAML_BLOCK_SEQUENCE_STYLE))
179 return -1;
180 if (!yaml_emitter_emit(emitter, &event))
181 return -1;
183 for (i = 0; i < n_array; ++i)
184 if (emit_array(emitter, arrays[i]) < 0)
185 return -1;
187 if (!yaml_sequence_end_event_initialize(&event))
188 return -1;
189 if (!yaml_emitter_emit(emitter, &event))
190 return -1;
192 return 0;
195 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
197 if (emit_string(emitter, pet_type_str(type)) < 0)
198 return -1;
199 return 0;
202 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
204 yaml_event_t event;
206 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
207 YAML_BLOCK_MAPPING_STYLE))
208 return -1;
209 if (!yaml_emitter_emit(emitter, &event))
210 return -1;
212 if (emit_string(emitter, "type") < 0)
213 return -1;
214 if (emit_type(emitter, expr->type) < 0)
215 return -1;
217 switch (expr->type) {
218 case pet_expr_double:
219 if (emit_string(emitter, "value") < 0)
220 return -1;
221 if (emit_double(emitter, expr->d) < 0)
222 return -1;
223 break;
224 case pet_expr_access:
225 if (emit_string(emitter, "relation") < 0)
226 return -1;
227 if (emit_map(emitter, expr->acc.access) < 0)
228 return -1;
229 if (emit_string(emitter, "read") < 0)
230 return -1;
231 if (emit_int(emitter, expr->acc.read) < 0)
232 return -1;
233 if (emit_string(emitter, "write") < 0)
234 return -1;
235 if (emit_int(emitter, expr->acc.write) < 0)
236 return -1;
237 break;
238 case pet_expr_unary:
239 case pet_expr_binary:
240 if (emit_string(emitter, "operation") < 0)
241 return -1;
242 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
243 return -1;
244 break;
245 case pet_expr_ternary:
246 break;
247 case pet_expr_call:
248 if (emit_string(emitter, "name") < 0)
249 return -1;
250 if (emit_string(emitter, expr->name) < 0)
251 return -1;
252 break;
255 if (expr->n_arg > 0) {
256 int i;
258 if (emit_string(emitter, "arguments") < 0)
259 return -1;
260 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
261 YAML_BLOCK_SEQUENCE_STYLE))
262 return -1;
263 if (!yaml_emitter_emit(emitter, &event))
264 return -1;
266 for (i = 0; i < expr->n_arg; ++i)
267 if (emit_expr(emitter, expr->args[i]) < 0)
268 return -1;
270 if (!yaml_sequence_end_event_initialize(&event))
271 return -1;
272 if (!yaml_emitter_emit(emitter, &event))
273 return -1;
276 if (!yaml_mapping_end_event_initialize(&event))
277 return -1;
278 if (!yaml_emitter_emit(emitter, &event))
279 return -1;
281 return 0;
284 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
286 yaml_event_t event;
288 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
289 YAML_BLOCK_MAPPING_STYLE))
290 return -1;
291 if (!yaml_emitter_emit(emitter, &event))
292 return -1;
294 if (emit_string(emitter, "line") < 0)
295 return -1;
296 if (emit_int(emitter, stmt->line) < 0)
297 return -1;
299 if (emit_string(emitter, "domain") < 0)
300 return -1;
301 if (emit_set(emitter, stmt->domain) < 0)
302 return -1;
304 if (emit_string(emitter, "schedule") < 0)
305 return -1;
306 if (emit_map(emitter, stmt->schedule) < 0)
307 return -1;
309 if (emit_string(emitter, "body") < 0)
310 return -1;
311 if (emit_expr(emitter, stmt->body) < 0)
312 return -1;
314 if (stmt->n_arg > 0) {
315 int i;
317 if (emit_string(emitter, "arguments") < 0)
318 return -1;
319 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
320 YAML_BLOCK_SEQUENCE_STYLE))
321 return -1;
322 if (!yaml_emitter_emit(emitter, &event))
323 return -1;
325 for (i = 0; i < stmt->n_arg; ++i)
326 if (emit_expr(emitter, stmt->args[i]) < 0)
327 return -1;
329 if (!yaml_sequence_end_event_initialize(&event))
330 return -1;
331 if (!yaml_emitter_emit(emitter, &event))
332 return -1;
335 if (!yaml_mapping_end_event_initialize(&event))
336 return -1;
337 if (!yaml_emitter_emit(emitter, &event))
338 return -1;
340 return 0;
343 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
344 struct pet_stmt **stmts)
346 int i;
347 yaml_event_t event;
349 if (emit_string(emitter, "statements") < 0)
350 return -1;
351 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
352 YAML_BLOCK_SEQUENCE_STYLE))
353 return -1;
354 if (!yaml_emitter_emit(emitter, &event))
355 return -1;
357 for (i = 0; i < n_stmt; ++i)
358 if (emit_stmt(emitter, stmts[i]) < 0)
359 return -1;
361 if (!yaml_sequence_end_event_initialize(&event))
362 return -1;
363 if (!yaml_emitter_emit(emitter, &event))
364 return -1;
366 return 0;
369 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
371 yaml_event_t event;
373 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
374 YAML_BLOCK_MAPPING_STYLE))
375 return -1;
376 if (!yaml_emitter_emit(emitter, &event))
377 return -1;
379 if (emit_string(emitter, "context") < 0)
380 return -1;
381 if (emit_set(emitter, scop->context) < 0)
382 return -1;
383 if (!isl_set_plain_is_universe(scop->context_value) &&
384 emit_named_set(emitter, "context_value", scop->context_value) < 0)
385 return -1;
387 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
388 return -1;
390 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
391 return -1;
393 if (!yaml_mapping_end_event_initialize(&event))
394 return -1;
395 if (!yaml_emitter_emit(emitter, &event))
396 return -1;
398 return 0;
401 /* Print a YAML serialization of "scop" to "out".
403 int pet_scop_emit(FILE *out, struct pet_scop *scop)
405 yaml_emitter_t emitter;
406 yaml_event_t event;
408 yaml_emitter_initialize(&emitter);
410 yaml_emitter_set_output_file(&emitter, out);
412 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
413 if (!yaml_emitter_emit(&emitter, &event))
414 goto error;
416 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
417 goto error;
418 if (!yaml_emitter_emit(&emitter, &event))
419 goto error;
421 if (emit_scop(&emitter, scop) < 0)
422 goto error;
424 if (!yaml_document_end_event_initialize(&event, 1))
425 goto error;
426 if (!yaml_emitter_emit(&emitter, &event))
427 goto error;
429 yaml_stream_end_event_initialize(&event);
430 if (!yaml_emitter_emit(&emitter, &event))
431 goto error;
433 yaml_emitter_delete(&emitter);
434 return 0;
435 error:
436 yaml_emitter_delete(&emitter);
437 return -1;