extract_affine: a variable that has been assigned an unknown value is not affine
[pet.git] / emit.c
blobebfcce2e4ec2e47af80480826889164d30f6092f
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_named_set(yaml_emitter_t *emitter, const char *name,
102 __isl_keep isl_set *set)
104 if (emit_string(emitter, name) < 0)
105 return -1;
106 if (emit_set(emitter, set) < 0)
107 return -1;
108 return 0;
111 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
113 yaml_event_t event;
115 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
116 YAML_BLOCK_MAPPING_STYLE))
117 return -1;
118 if (!yaml_emitter_emit(emitter, &event))
119 return -1;
121 if (emit_string(emitter, "context") < 0)
122 return -1;
123 if (emit_set(emitter, array->context) < 0)
124 return -1;
126 if (emit_string(emitter, "extent") < 0)
127 return -1;
128 if (emit_set(emitter, array->extent) < 0)
129 return -1;
131 if (array->value_bounds) {
132 if (emit_string(emitter, "value_bounds") < 0)
133 return -1;
134 if (emit_set(emitter, array->value_bounds) < 0)
135 return -1;
138 if (emit_string(emitter, "element_type") < 0)
139 return -1;
140 if (emit_string(emitter, array->element_type) < 0)
141 return -1;
143 if (array->live_out) {
144 if (emit_string(emitter, "live_out") < 0)
145 return -1;
146 if (emit_string(emitter, "1") < 0)
147 return -1;
150 if (!yaml_mapping_end_event_initialize(&event))
151 return -1;
152 if (!yaml_emitter_emit(emitter, &event))
153 return -1;
155 return 0;
158 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
159 struct pet_array **arrays)
161 int i;
162 yaml_event_t event;
164 if (emit_string(emitter, "arrays") < 0)
165 return -1;
166 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
167 YAML_BLOCK_SEQUENCE_STYLE))
168 return -1;
169 if (!yaml_emitter_emit(emitter, &event))
170 return -1;
172 for (i = 0; i < n_array; ++i)
173 if (emit_array(emitter, arrays[i]) < 0)
174 return -1;
176 if (!yaml_sequence_end_event_initialize(&event))
177 return -1;
178 if (!yaml_emitter_emit(emitter, &event))
179 return -1;
181 return 0;
184 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
186 if (emit_string(emitter, pet_type_str(type)) < 0)
187 return -1;
188 return 0;
191 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
193 yaml_event_t event;
195 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
196 YAML_BLOCK_MAPPING_STYLE))
197 return -1;
198 if (!yaml_emitter_emit(emitter, &event))
199 return -1;
201 if (emit_string(emitter, "type") < 0)
202 return -1;
203 if (emit_type(emitter, expr->type) < 0)
204 return -1;
206 switch (expr->type) {
207 case pet_expr_double:
208 if (emit_string(emitter, "value") < 0)
209 return -1;
210 if (emit_double(emitter, expr->d) < 0)
211 return -1;
212 break;
213 case pet_expr_access:
214 if (emit_string(emitter, "relation") < 0)
215 return -1;
216 if (emit_map(emitter, expr->acc.access) < 0)
217 return -1;
218 if (emit_string(emitter, "read") < 0)
219 return -1;
220 if (emit_int(emitter, expr->acc.read) < 0)
221 return -1;
222 if (emit_string(emitter, "write") < 0)
223 return -1;
224 if (emit_int(emitter, expr->acc.write) < 0)
225 return -1;
226 break;
227 case pet_expr_unary:
228 case pet_expr_binary:
229 if (emit_string(emitter, "operation") < 0)
230 return -1;
231 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
232 return -1;
233 break;
234 case pet_expr_ternary:
235 break;
236 case pet_expr_call:
237 if (emit_string(emitter, "name") < 0)
238 return -1;
239 if (emit_string(emitter, expr->name) < 0)
240 return -1;
241 break;
244 if (expr->n_arg > 0) {
245 int i;
247 if (emit_string(emitter, "arguments") < 0)
248 return -1;
249 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
250 YAML_BLOCK_SEQUENCE_STYLE))
251 return -1;
252 if (!yaml_emitter_emit(emitter, &event))
253 return -1;
255 for (i = 0; i < expr->n_arg; ++i)
256 if (emit_expr(emitter, expr->args[i]) < 0)
257 return -1;
259 if (!yaml_sequence_end_event_initialize(&event))
260 return -1;
261 if (!yaml_emitter_emit(emitter, &event))
262 return -1;
265 if (!yaml_mapping_end_event_initialize(&event))
266 return -1;
267 if (!yaml_emitter_emit(emitter, &event))
268 return -1;
270 return 0;
273 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
275 yaml_event_t event;
277 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
278 YAML_BLOCK_MAPPING_STYLE))
279 return -1;
280 if (!yaml_emitter_emit(emitter, &event))
281 return -1;
283 if (emit_string(emitter, "line") < 0)
284 return -1;
285 if (emit_int(emitter, stmt->line) < 0)
286 return -1;
288 if (emit_string(emitter, "domain") < 0)
289 return -1;
290 if (emit_set(emitter, stmt->domain) < 0)
291 return -1;
293 if (emit_string(emitter, "schedule") < 0)
294 return -1;
295 if (emit_map(emitter, stmt->schedule) < 0)
296 return -1;
298 if (emit_string(emitter, "body") < 0)
299 return -1;
300 if (emit_expr(emitter, stmt->body) < 0)
301 return -1;
303 if (stmt->n_arg > 0) {
304 int i;
306 if (emit_string(emitter, "arguments") < 0)
307 return -1;
308 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
309 YAML_BLOCK_SEQUENCE_STYLE))
310 return -1;
311 if (!yaml_emitter_emit(emitter, &event))
312 return -1;
314 for (i = 0; i < stmt->n_arg; ++i)
315 if (emit_expr(emitter, stmt->args[i]) < 0)
316 return -1;
318 if (!yaml_sequence_end_event_initialize(&event))
319 return -1;
320 if (!yaml_emitter_emit(emitter, &event))
321 return -1;
324 if (!yaml_mapping_end_event_initialize(&event))
325 return -1;
326 if (!yaml_emitter_emit(emitter, &event))
327 return -1;
329 return 0;
332 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
333 struct pet_stmt **stmts)
335 int i;
336 yaml_event_t event;
338 if (emit_string(emitter, "statements") < 0)
339 return -1;
340 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
341 YAML_BLOCK_SEQUENCE_STYLE))
342 return -1;
343 if (!yaml_emitter_emit(emitter, &event))
344 return -1;
346 for (i = 0; i < n_stmt; ++i)
347 if (emit_stmt(emitter, stmts[i]) < 0)
348 return -1;
350 if (!yaml_sequence_end_event_initialize(&event))
351 return -1;
352 if (!yaml_emitter_emit(emitter, &event))
353 return -1;
355 return 0;
358 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
360 yaml_event_t event;
362 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
363 YAML_BLOCK_MAPPING_STYLE))
364 return -1;
365 if (!yaml_emitter_emit(emitter, &event))
366 return -1;
368 if (emit_string(emitter, "context") < 0)
369 return -1;
370 if (emit_set(emitter, scop->context) < 0)
371 return -1;
372 if (!isl_set_plain_is_universe(scop->context_value) &&
373 emit_named_set(emitter, "context_value", scop->context_value) < 0)
374 return -1;
376 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
377 return -1;
379 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
380 return -1;
382 if (!yaml_mapping_end_event_initialize(&event))
383 return -1;
384 if (!yaml_emitter_emit(emitter, &event))
385 return -1;
387 return 0;
390 /* Print a YAML serialization of "scop" to "out".
392 int pet_scop_emit(FILE *out, struct pet_scop *scop)
394 yaml_emitter_t emitter;
395 yaml_event_t event;
397 yaml_emitter_initialize(&emitter);
399 yaml_emitter_set_output_file(&emitter, out);
401 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
402 if (!yaml_emitter_emit(&emitter, &event))
403 goto error;
405 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
406 goto error;
407 if (!yaml_emitter_emit(&emitter, &event))
408 goto error;
410 if (emit_scop(&emitter, scop) < 0)
411 goto error;
413 if (!yaml_document_end_event_initialize(&event, 1))
414 goto error;
415 if (!yaml_emitter_emit(&emitter, &event))
416 goto error;
418 yaml_stream_end_event_initialize(&event);
419 if (!yaml_emitter_emit(&emitter, &event))
420 goto error;
422 yaml_emitter_delete(&emitter);
423 return 0;
424 error:
425 yaml_emitter_delete(&emitter);
426 return -1;