scop.c: extract out acces_apply_value_bounds
[pet.git] / emit.c
bloba14af59f6c4b90b73b38b04fe11f5d0e1c258fb1
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 /* Print the isl_id "id" to "emitter".
55 static int emit_id(yaml_emitter_t *emitter, __isl_keep isl_id *id)
57 return emit_string(emitter, isl_id_get_name(id));
60 /* Print the string "name" and the isl_id "id" to "emitter".
62 static int emit_named_id(yaml_emitter_t *emitter, const char *name,
63 __isl_keep isl_id *id)
65 if (emit_string(emitter, name) < 0)
66 return -1;
67 if (emit_id(emitter, id) < 0)
68 return -1;
69 return 0;
72 static int emit_int(yaml_emitter_t *emitter, int i)
74 char buffer[40];
76 snprintf(buffer, sizeof(buffer), "%d", i);
77 return emit_string(emitter, buffer);
80 static int emit_named_int(yaml_emitter_t *emitter, const char *name, int i)
82 if (emit_string(emitter, name) < 0)
83 return -1;
84 if (emit_int(emitter, i) < 0)
85 return -1;
86 return 0;
89 /* Print the unsigned integer "u" to "emitter".
91 static int emit_unsigned(yaml_emitter_t *emitter, unsigned u)
93 char buffer[40];
95 snprintf(buffer, sizeof(buffer), "%u", u);
96 return emit_string(emitter, buffer);
99 /* Print the string "name" and the unsigned integer "u" to "emitter".
101 static int emit_named_unsigned(yaml_emitter_t *emitter, const char *name,
102 unsigned u)
104 if (emit_string(emitter, name) < 0)
105 return -1;
106 if (emit_int(emitter, u) < 0)
107 return -1;
108 return 0;
111 static int emit_double(yaml_emitter_t *emitter, double d)
113 char buffer[40];
115 snprintf(buffer, sizeof(buffer), "%g", d);
116 return emit_string(emitter, buffer);
119 static int emit_map(yaml_emitter_t *emitter, __isl_keep isl_map *map)
121 isl_ctx *ctx = isl_map_get_ctx(map);
122 isl_printer *p;
123 char *str;
124 int r;
126 p = isl_printer_to_str(ctx);
127 p = isl_printer_print_map(p, map);
128 str = isl_printer_get_str(p);
129 isl_printer_free(p);
130 r = emit_string(emitter, str);
131 free(str);
132 return r;
135 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
137 isl_ctx *ctx = isl_set_get_ctx(set);
138 isl_printer *p;
139 char *str;
140 int r;
142 p = isl_printer_to_str(ctx);
143 p = isl_printer_print_set(p, set);
144 str = isl_printer_get_str(p);
145 isl_printer_free(p);
146 r = emit_string(emitter, str);
147 free(str);
148 return r;
151 static int emit_named_set(yaml_emitter_t *emitter, const char *name,
152 __isl_keep isl_set *set)
154 if (emit_string(emitter, name) < 0)
155 return -1;
156 if (emit_set(emitter, set) < 0)
157 return -1;
158 return 0;
161 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
163 yaml_event_t event;
165 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
166 YAML_BLOCK_MAPPING_STYLE))
167 return -1;
168 if (!yaml_emitter_emit(emitter, &event))
169 return -1;
171 if (emit_string(emitter, "context") < 0)
172 return -1;
173 if (emit_set(emitter, array->context) < 0)
174 return -1;
176 if (emit_string(emitter, "extent") < 0)
177 return -1;
178 if (emit_set(emitter, array->extent) < 0)
179 return -1;
181 if (array->value_bounds) {
182 if (emit_string(emitter, "value_bounds") < 0)
183 return -1;
184 if (emit_set(emitter, array->value_bounds) < 0)
185 return -1;
188 if (emit_string(emitter, "element_type") < 0)
189 return -1;
190 if (emit_string(emitter, array->element_type) < 0)
191 return -1;
192 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
193 return -1;
195 if (array->live_out) {
196 if (emit_string(emitter, "live_out") < 0)
197 return -1;
198 if (emit_string(emitter, "1") < 0)
199 return -1;
202 if (array->uniquely_defined) {
203 if (emit_string(emitter, "uniquely_defined") < 0)
204 return -1;
205 if (emit_string(emitter, "1") < 0)
206 return -1;
209 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
210 return -1;
211 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
212 return -1;
214 if (!yaml_mapping_end_event_initialize(&event))
215 return -1;
216 if (!yaml_emitter_emit(emitter, &event))
217 return -1;
219 return 0;
222 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
223 struct pet_array **arrays)
225 int i;
226 yaml_event_t event;
228 if (emit_string(emitter, "arrays") < 0)
229 return -1;
230 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
231 YAML_BLOCK_SEQUENCE_STYLE))
232 return -1;
233 if (!yaml_emitter_emit(emitter, &event))
234 return -1;
236 for (i = 0; i < n_array; ++i)
237 if (emit_array(emitter, arrays[i]) < 0)
238 return -1;
240 if (!yaml_sequence_end_event_initialize(&event))
241 return -1;
242 if (!yaml_emitter_emit(emitter, &event))
243 return -1;
245 return 0;
248 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
250 if (emit_string(emitter, pet_type_str(type)) < 0)
251 return -1;
252 return 0;
255 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
257 yaml_event_t event;
259 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
260 YAML_BLOCK_MAPPING_STYLE))
261 return -1;
262 if (!yaml_emitter_emit(emitter, &event))
263 return -1;
265 if (emit_string(emitter, "type") < 0)
266 return -1;
267 if (emit_type(emitter, expr->type) < 0)
268 return -1;
270 switch (expr->type) {
271 case pet_expr_double:
272 if (emit_string(emitter, "value") < 0)
273 return -1;
274 if (emit_double(emitter, expr->d.val) < 0)
275 return -1;
276 if (emit_string(emitter, "string") < 0)
277 return -1;
278 if (emit_string(emitter, expr->d.s) < 0)
279 return -1;
280 break;
281 case pet_expr_access:
282 if (emit_string(emitter, "relation") < 0)
283 return -1;
284 if (emit_map(emitter, expr->acc.access) < 0)
285 return -1;
286 if (expr->acc.ref_id &&
287 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
288 return -1;
289 if (emit_string(emitter, "read") < 0)
290 return -1;
291 if (emit_int(emitter, expr->acc.read) < 0)
292 return -1;
293 if (emit_string(emitter, "write") < 0)
294 return -1;
295 if (emit_int(emitter, expr->acc.write) < 0)
296 return -1;
297 break;
298 case pet_expr_unary:
299 case pet_expr_binary:
300 if (emit_string(emitter, "operation") < 0)
301 return -1;
302 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
303 return -1;
304 break;
305 case pet_expr_ternary:
306 break;
307 case pet_expr_call:
308 if (emit_string(emitter, "name") < 0)
309 return -1;
310 if (emit_string(emitter, expr->name) < 0)
311 return -1;
312 break;
313 case pet_expr_cast:
314 if (emit_string(emitter, "type_name") < 0)
315 return -1;
316 if (emit_string(emitter, expr->type_name) < 0)
317 return -1;
318 break;
321 if (expr->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 < expr->n_arg; ++i)
333 if (emit_expr(emitter, expr->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_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
352 yaml_event_t event;
354 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
355 YAML_BLOCK_MAPPING_STYLE))
356 return -1;
357 if (!yaml_emitter_emit(emitter, &event))
358 return -1;
360 if (emit_string(emitter, "line") < 0)
361 return -1;
362 if (emit_int(emitter, stmt->line) < 0)
363 return -1;
365 if (emit_string(emitter, "domain") < 0)
366 return -1;
367 if (emit_set(emitter, stmt->domain) < 0)
368 return -1;
370 if (emit_string(emitter, "schedule") < 0)
371 return -1;
372 if (emit_map(emitter, stmt->schedule) < 0)
373 return -1;
375 if (emit_string(emitter, "body") < 0)
376 return -1;
377 if (emit_expr(emitter, stmt->body) < 0)
378 return -1;
380 if (stmt->n_arg > 0) {
381 int i;
383 if (emit_string(emitter, "arguments") < 0)
384 return -1;
385 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
386 YAML_BLOCK_SEQUENCE_STYLE))
387 return -1;
388 if (!yaml_emitter_emit(emitter, &event))
389 return -1;
391 for (i = 0; i < stmt->n_arg; ++i)
392 if (emit_expr(emitter, stmt->args[i]) < 0)
393 return -1;
395 if (!yaml_sequence_end_event_initialize(&event))
396 return -1;
397 if (!yaml_emitter_emit(emitter, &event))
398 return -1;
401 if (!yaml_mapping_end_event_initialize(&event))
402 return -1;
403 if (!yaml_emitter_emit(emitter, &event))
404 return -1;
406 return 0;
409 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
410 struct pet_stmt **stmts)
412 int i;
413 yaml_event_t event;
415 if (emit_string(emitter, "statements") < 0)
416 return -1;
417 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
418 YAML_BLOCK_SEQUENCE_STYLE))
419 return -1;
420 if (!yaml_emitter_emit(emitter, &event))
421 return -1;
423 for (i = 0; i < n_stmt; ++i)
424 if (emit_stmt(emitter, stmts[i]) < 0)
425 return -1;
427 if (!yaml_sequence_end_event_initialize(&event))
428 return -1;
429 if (!yaml_emitter_emit(emitter, &event))
430 return -1;
432 return 0;
435 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
437 yaml_event_t event;
439 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
440 YAML_BLOCK_MAPPING_STYLE))
441 return -1;
442 if (!yaml_emitter_emit(emitter, &event))
443 return -1;
445 if (emit_named_unsigned(emitter, "start", scop->start) < 0)
446 return -1;
447 if (emit_named_unsigned(emitter, "end", scop->end) < 0)
448 return -1;
449 if (emit_string(emitter, "context") < 0)
450 return -1;
451 if (emit_set(emitter, scop->context) < 0)
452 return -1;
453 if (!isl_set_plain_is_universe(scop->context_value) &&
454 emit_named_set(emitter, "context_value", scop->context_value) < 0)
455 return -1;
457 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
458 return -1;
460 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
461 return -1;
463 if (!yaml_mapping_end_event_initialize(&event))
464 return -1;
465 if (!yaml_emitter_emit(emitter, &event))
466 return -1;
468 return 0;
471 /* Print a YAML serialization of "scop" to "out".
473 int pet_scop_emit(FILE *out, struct pet_scop *scop)
475 yaml_emitter_t emitter;
476 yaml_event_t event;
478 yaml_emitter_initialize(&emitter);
480 yaml_emitter_set_output_file(&emitter, out);
482 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
483 if (!yaml_emitter_emit(&emitter, &event))
484 goto error;
486 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
487 goto error;
488 if (!yaml_emitter_emit(&emitter, &event))
489 goto error;
491 if (emit_scop(&emitter, scop) < 0)
492 goto error;
494 if (!yaml_document_end_event_initialize(&event, 1))
495 goto error;
496 if (!yaml_emitter_emit(&emitter, &event))
497 goto error;
499 yaml_stream_end_event_initialize(&event);
500 if (!yaml_emitter_emit(&emitter, &event))
501 goto error;
503 yaml_emitter_delete(&emitter);
504 return 0;
505 error:
506 yaml_emitter_delete(&emitter);
507 return -1;