handle extraction of scops inside a loop
[pet.git] / emit.c
blob86d429f013fc1f95c8cec6f33d224432ae7a677e
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_double(yaml_emitter_t *emitter, double d)
63 char buffer[40];
65 snprintf(buffer, sizeof(buffer), "%g", d);
66 return emit_string(emitter, buffer);
69 static int emit_map(yaml_emitter_t *emitter, __isl_keep isl_map *map)
71 isl_ctx *ctx = isl_map_get_ctx(map);
72 isl_printer *p;
73 char *str;
74 int r;
76 p = isl_printer_to_str(ctx);
77 p = isl_printer_print_map(p, map);
78 str = isl_printer_get_str(p);
79 isl_printer_free(p);
80 r = emit_string(emitter, str);
81 free(str);
82 return r;
85 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
87 isl_ctx *ctx = isl_set_get_ctx(set);
88 isl_printer *p;
89 char *str;
90 int r;
92 p = isl_printer_to_str(ctx);
93 p = isl_printer_print_set(p, set);
94 str = isl_printer_get_str(p);
95 isl_printer_free(p);
96 r = emit_string(emitter, str);
97 free(str);
98 return r;
101 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
103 yaml_event_t event;
105 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
106 YAML_BLOCK_MAPPING_STYLE))
107 return -1;
108 if (!yaml_emitter_emit(emitter, &event))
109 return -1;
111 if (emit_string(emitter, "context") < 0)
112 return -1;
113 if (emit_set(emitter, array->context) < 0)
114 return -1;
116 if (emit_string(emitter, "extent") < 0)
117 return -1;
118 if (emit_set(emitter, array->extent) < 0)
119 return -1;
121 if (array->value_bounds) {
122 if (emit_string(emitter, "value_bounds") < 0)
123 return -1;
124 if (emit_set(emitter, array->value_bounds) < 0)
125 return -1;
128 if (emit_string(emitter, "element_type") < 0)
129 return -1;
130 if (emit_string(emitter, array->element_type) < 0)
131 return -1;
133 if (array->live_out) {
134 if (emit_string(emitter, "live_out") < 0)
135 return -1;
136 if (emit_string(emitter, "1") < 0)
137 return -1;
140 if (!yaml_mapping_end_event_initialize(&event))
141 return -1;
142 if (!yaml_emitter_emit(emitter, &event))
143 return -1;
145 return 0;
148 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
149 struct pet_array **arrays)
151 int i;
152 yaml_event_t event;
154 if (emit_string(emitter, "arrays") < 0)
155 return -1;
156 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
157 YAML_BLOCK_SEQUENCE_STYLE))
158 return -1;
159 if (!yaml_emitter_emit(emitter, &event))
160 return -1;
162 for (i = 0; i < n_array; ++i)
163 if (emit_array(emitter, arrays[i]) < 0)
164 return -1;
166 if (!yaml_sequence_end_event_initialize(&event))
167 return -1;
168 if (!yaml_emitter_emit(emitter, &event))
169 return -1;
171 return 0;
174 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
176 if (emit_string(emitter, pet_type_str(type)) < 0)
177 return -1;
178 return 0;
181 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
183 yaml_event_t event;
185 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
186 YAML_BLOCK_MAPPING_STYLE))
187 return -1;
188 if (!yaml_emitter_emit(emitter, &event))
189 return -1;
191 if (emit_string(emitter, "type") < 0)
192 return -1;
193 if (emit_type(emitter, expr->type) < 0)
194 return -1;
196 switch (expr->type) {
197 case pet_expr_double:
198 if (emit_string(emitter, "value") < 0)
199 return -1;
200 if (emit_double(emitter, expr->d) < 0)
201 return -1;
202 break;
203 case pet_expr_access:
204 if (emit_string(emitter, "relation") < 0)
205 return -1;
206 if (emit_map(emitter, expr->acc.access) < 0)
207 return -1;
208 if (emit_string(emitter, "read") < 0)
209 return -1;
210 if (emit_int(emitter, expr->acc.read) < 0)
211 return -1;
212 if (emit_string(emitter, "write") < 0)
213 return -1;
214 if (emit_int(emitter, expr->acc.write) < 0)
215 return -1;
216 break;
217 case pet_expr_unary:
218 case pet_expr_binary:
219 if (emit_string(emitter, "operation") < 0)
220 return -1;
221 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
222 return -1;
223 break;
224 case pet_expr_ternary:
225 break;
226 case pet_expr_call:
227 if (emit_string(emitter, "name") < 0)
228 return -1;
229 if (emit_string(emitter, expr->name) < 0)
230 return -1;
231 break;
234 if (expr->n_arg > 0) {
235 int i;
237 if (emit_string(emitter, "arguments") < 0)
238 return -1;
239 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
240 YAML_BLOCK_SEQUENCE_STYLE))
241 return -1;
242 if (!yaml_emitter_emit(emitter, &event))
243 return -1;
245 for (i = 0; i < expr->n_arg; ++i)
246 if (emit_expr(emitter, expr->args[i]) < 0)
247 return -1;
249 if (!yaml_sequence_end_event_initialize(&event))
250 return -1;
251 if (!yaml_emitter_emit(emitter, &event))
252 return -1;
255 if (!yaml_mapping_end_event_initialize(&event))
256 return -1;
257 if (!yaml_emitter_emit(emitter, &event))
258 return -1;
260 return 0;
263 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
265 yaml_event_t event;
267 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
268 YAML_BLOCK_MAPPING_STYLE))
269 return -1;
270 if (!yaml_emitter_emit(emitter, &event))
271 return -1;
273 if (emit_string(emitter, "line") < 0)
274 return -1;
275 if (emit_int(emitter, stmt->line) < 0)
276 return -1;
278 if (emit_string(emitter, "domain") < 0)
279 return -1;
280 if (emit_set(emitter, stmt->domain) < 0)
281 return -1;
283 if (emit_string(emitter, "schedule") < 0)
284 return -1;
285 if (emit_map(emitter, stmt->schedule) < 0)
286 return -1;
288 if (emit_string(emitter, "body") < 0)
289 return -1;
290 if (emit_expr(emitter, stmt->body) < 0)
291 return -1;
293 if (!yaml_mapping_end_event_initialize(&event))
294 return -1;
295 if (!yaml_emitter_emit(emitter, &event))
296 return -1;
298 return 0;
301 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
302 struct pet_stmt **stmts)
304 int i;
305 yaml_event_t event;
307 if (emit_string(emitter, "statements") < 0)
308 return -1;
309 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
310 YAML_BLOCK_SEQUENCE_STYLE))
311 return -1;
312 if (!yaml_emitter_emit(emitter, &event))
313 return -1;
315 for (i = 0; i < n_stmt; ++i)
316 if (emit_stmt(emitter, stmts[i]) < 0)
317 return -1;
319 if (!yaml_sequence_end_event_initialize(&event))
320 return -1;
321 if (!yaml_emitter_emit(emitter, &event))
322 return -1;
324 return 0;
327 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
329 yaml_event_t event;
331 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
332 YAML_BLOCK_MAPPING_STYLE))
333 return -1;
334 if (!yaml_emitter_emit(emitter, &event))
335 return -1;
337 if (emit_string(emitter, "context") < 0)
338 return -1;
339 if (emit_set(emitter, scop->context) < 0)
340 return -1;
342 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
343 return -1;
345 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
346 return -1;
348 if (!yaml_mapping_end_event_initialize(&event))
349 return -1;
350 if (!yaml_emitter_emit(emitter, &event))
351 return -1;
353 return 0;
356 /* Print a YAML serialization of "scop" to "out".
358 int pet_scop_emit(FILE *out, struct pet_scop *scop)
360 yaml_emitter_t emitter;
361 yaml_event_t event;
363 yaml_emitter_initialize(&emitter);
365 yaml_emitter_set_output_file(&emitter, out);
367 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
368 if (!yaml_emitter_emit(&emitter, &event))
369 goto error;
371 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
372 goto error;
373 if (!yaml_emitter_emit(&emitter, &event))
374 goto error;
376 if (emit_scop(&emitter, scop) < 0)
377 goto error;
379 if (!yaml_document_end_event_initialize(&event, 1))
380 goto error;
381 if (!yaml_emitter_emit(&emitter, &event))
382 goto error;
384 yaml_stream_end_event_initialize(&event);
385 if (!yaml_emitter_emit(&emitter, &event))
386 goto error;
388 yaml_emitter_delete(&emitter);
389 return 0;
390 error:
391 yaml_emitter_delete(&emitter);
392 return -1;