scan.cc: compute_wrapping: drop dead code
[pet.git] / emit.c
blob6982278369e0e261f56c8887ad730d665ab3d8ff
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 /* Print the string "name" and the map "map" to "emitter".
163 static int emit_named_map(yaml_emitter_t *emitter, const char *name,
164 __isl_keep isl_map *map)
166 if (emit_string(emitter, name) < 0)
167 return -1;
168 if (emit_map(emitter, map) < 0)
169 return -1;
170 return 0;
173 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
175 yaml_event_t event;
177 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
178 YAML_BLOCK_MAPPING_STYLE))
179 return -1;
180 if (!yaml_emitter_emit(emitter, &event))
181 return -1;
183 if (emit_string(emitter, "context") < 0)
184 return -1;
185 if (emit_set(emitter, array->context) < 0)
186 return -1;
188 if (emit_string(emitter, "extent") < 0)
189 return -1;
190 if (emit_set(emitter, array->extent) < 0)
191 return -1;
193 if (array->value_bounds) {
194 if (emit_string(emitter, "value_bounds") < 0)
195 return -1;
196 if (emit_set(emitter, array->value_bounds) < 0)
197 return -1;
200 if (emit_string(emitter, "element_type") < 0)
201 return -1;
202 if (emit_string(emitter, array->element_type) < 0)
203 return -1;
204 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
205 return -1;
207 if (array->live_out) {
208 if (emit_string(emitter, "live_out") < 0)
209 return -1;
210 if (emit_string(emitter, "1") < 0)
211 return -1;
214 if (array->uniquely_defined) {
215 if (emit_string(emitter, "uniquely_defined") < 0)
216 return -1;
217 if (emit_string(emitter, "1") < 0)
218 return -1;
221 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
222 return -1;
223 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
224 return -1;
226 if (!yaml_mapping_end_event_initialize(&event))
227 return -1;
228 if (!yaml_emitter_emit(emitter, &event))
229 return -1;
231 return 0;
234 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
235 struct pet_array **arrays)
237 int i;
238 yaml_event_t event;
240 if (emit_string(emitter, "arrays") < 0)
241 return -1;
242 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
243 YAML_BLOCK_SEQUENCE_STYLE))
244 return -1;
245 if (!yaml_emitter_emit(emitter, &event))
246 return -1;
248 for (i = 0; i < n_array; ++i)
249 if (emit_array(emitter, arrays[i]) < 0)
250 return -1;
252 if (!yaml_sequence_end_event_initialize(&event))
253 return -1;
254 if (!yaml_emitter_emit(emitter, &event))
255 return -1;
257 return 0;
260 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
262 if (emit_string(emitter, pet_type_str(type)) < 0)
263 return -1;
264 return 0;
267 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
269 yaml_event_t event;
271 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
272 YAML_BLOCK_MAPPING_STYLE))
273 return -1;
274 if (!yaml_emitter_emit(emitter, &event))
275 return -1;
277 if (emit_string(emitter, "type") < 0)
278 return -1;
279 if (emit_type(emitter, expr->type) < 0)
280 return -1;
282 switch (expr->type) {
283 case pet_expr_double:
284 if (emit_string(emitter, "value") < 0)
285 return -1;
286 if (emit_double(emitter, expr->d.val) < 0)
287 return -1;
288 if (emit_string(emitter, "string") < 0)
289 return -1;
290 if (emit_string(emitter, expr->d.s) < 0)
291 return -1;
292 break;
293 case pet_expr_access:
294 if (emit_string(emitter, "relation") < 0)
295 return -1;
296 if (emit_map(emitter, expr->acc.access) < 0)
297 return -1;
298 if (expr->acc.ref_id &&
299 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
300 return -1;
301 if (emit_string(emitter, "read") < 0)
302 return -1;
303 if (emit_int(emitter, expr->acc.read) < 0)
304 return -1;
305 if (emit_string(emitter, "write") < 0)
306 return -1;
307 if (emit_int(emitter, expr->acc.write) < 0)
308 return -1;
309 break;
310 case pet_expr_unary:
311 case pet_expr_binary:
312 if (emit_string(emitter, "operation") < 0)
313 return -1;
314 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
315 return -1;
316 break;
317 case pet_expr_ternary:
318 break;
319 case pet_expr_call:
320 if (emit_string(emitter, "name") < 0)
321 return -1;
322 if (emit_string(emitter, expr->name) < 0)
323 return -1;
324 break;
325 case pet_expr_cast:
326 if (emit_string(emitter, "type_name") < 0)
327 return -1;
328 if (emit_string(emitter, expr->type_name) < 0)
329 return -1;
330 break;
333 if (expr->n_arg > 0) {
334 int i;
336 if (emit_string(emitter, "arguments") < 0)
337 return -1;
338 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
339 YAML_BLOCK_SEQUENCE_STYLE))
340 return -1;
341 if (!yaml_emitter_emit(emitter, &event))
342 return -1;
344 for (i = 0; i < expr->n_arg; ++i)
345 if (emit_expr(emitter, expr->args[i]) < 0)
346 return -1;
348 if (!yaml_sequence_end_event_initialize(&event))
349 return -1;
350 if (!yaml_emitter_emit(emitter, &event))
351 return -1;
354 if (!yaml_mapping_end_event_initialize(&event))
355 return -1;
356 if (!yaml_emitter_emit(emitter, &event))
357 return -1;
359 return 0;
362 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
364 yaml_event_t event;
366 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
367 YAML_BLOCK_MAPPING_STYLE))
368 return -1;
369 if (!yaml_emitter_emit(emitter, &event))
370 return -1;
372 if (emit_string(emitter, "line") < 0)
373 return -1;
374 if (emit_int(emitter, stmt->line) < 0)
375 return -1;
377 if (emit_string(emitter, "domain") < 0)
378 return -1;
379 if (emit_set(emitter, stmt->domain) < 0)
380 return -1;
382 if (emit_string(emitter, "schedule") < 0)
383 return -1;
384 if (emit_map(emitter, stmt->schedule) < 0)
385 return -1;
387 if (emit_string(emitter, "body") < 0)
388 return -1;
389 if (emit_expr(emitter, stmt->body) < 0)
390 return -1;
392 if (stmt->n_arg > 0) {
393 int i;
395 if (emit_string(emitter, "arguments") < 0)
396 return -1;
397 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
398 YAML_BLOCK_SEQUENCE_STYLE))
399 return -1;
400 if (!yaml_emitter_emit(emitter, &event))
401 return -1;
403 for (i = 0; i < stmt->n_arg; ++i)
404 if (emit_expr(emitter, stmt->args[i]) < 0)
405 return -1;
407 if (!yaml_sequence_end_event_initialize(&event))
408 return -1;
409 if (!yaml_emitter_emit(emitter, &event))
410 return -1;
413 if (!yaml_mapping_end_event_initialize(&event))
414 return -1;
415 if (!yaml_emitter_emit(emitter, &event))
416 return -1;
418 return 0;
421 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
422 struct pet_stmt **stmts)
424 int i;
425 yaml_event_t event;
427 if (emit_string(emitter, "statements") < 0)
428 return -1;
429 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
430 YAML_BLOCK_SEQUENCE_STYLE))
431 return -1;
432 if (!yaml_emitter_emit(emitter, &event))
433 return -1;
435 for (i = 0; i < n_stmt; ++i)
436 if (emit_stmt(emitter, stmts[i]) < 0)
437 return -1;
439 if (!yaml_sequence_end_event_initialize(&event))
440 return -1;
441 if (!yaml_emitter_emit(emitter, &event))
442 return -1;
444 return 0;
447 /* Print "implication" to "emitter".
449 static int emit_implication(yaml_emitter_t *emitter,
450 struct pet_implication *implication)
452 yaml_event_t event;
454 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
455 YAML_BLOCK_MAPPING_STYLE))
456 return -1;
457 if (!yaml_emitter_emit(emitter, &event))
458 return -1;
460 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
461 return -1;
463 if (emit_named_map(emitter, "extension", implication->extension) < 0)
464 return -1;
466 if (!yaml_mapping_end_event_initialize(&event))
467 return -1;
468 if (!yaml_emitter_emit(emitter, &event))
469 return -1;
471 return 0;
474 /* Print the list of "n_implication" "implications", if any, to "emitter".
476 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
477 struct pet_implication **implications)
479 int i;
480 yaml_event_t event;
482 if (n_implication == 0)
483 return 0;
485 if (emit_string(emitter, "implications") < 0)
486 return -1;
487 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
488 YAML_BLOCK_SEQUENCE_STYLE))
489 return -1;
490 if (!yaml_emitter_emit(emitter, &event))
491 return -1;
493 for (i = 0; i < n_implication; ++i)
494 if (emit_implication(emitter, implications[i]) < 0)
495 return -1;
497 if (!yaml_sequence_end_event_initialize(&event))
498 return -1;
499 if (!yaml_emitter_emit(emitter, &event))
500 return -1;
502 return 0;
505 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
507 yaml_event_t event;
509 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
510 YAML_BLOCK_MAPPING_STYLE))
511 return -1;
512 if (!yaml_emitter_emit(emitter, &event))
513 return -1;
515 if (emit_named_unsigned(emitter, "start", scop->start) < 0)
516 return -1;
517 if (emit_named_unsigned(emitter, "end", scop->end) < 0)
518 return -1;
519 if (emit_string(emitter, "context") < 0)
520 return -1;
521 if (emit_set(emitter, scop->context) < 0)
522 return -1;
523 if (!isl_set_plain_is_universe(scop->context_value) &&
524 emit_named_set(emitter, "context_value", scop->context_value) < 0)
525 return -1;
527 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
528 return -1;
530 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
531 return -1;
533 if (emit_implications(emitter, scop->n_implication,
534 scop->implications) < 0)
535 return -1;
537 if (!yaml_mapping_end_event_initialize(&event))
538 return -1;
539 if (!yaml_emitter_emit(emitter, &event))
540 return -1;
542 return 0;
545 /* Print a YAML serialization of "scop" to "out".
547 int pet_scop_emit(FILE *out, struct pet_scop *scop)
549 yaml_emitter_t emitter;
550 yaml_event_t event;
552 yaml_emitter_initialize(&emitter);
554 yaml_emitter_set_output_file(&emitter, out);
556 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
557 if (!yaml_emitter_emit(&emitter, &event))
558 goto error;
560 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
561 goto error;
562 if (!yaml_emitter_emit(&emitter, &event))
563 goto error;
565 if (emit_scop(&emitter, scop) < 0)
566 goto error;
568 if (!yaml_document_end_event_initialize(&event, 1))
569 goto error;
570 if (!yaml_emitter_emit(&emitter, &event))
571 goto error;
573 yaml_stream_end_event_initialize(&event);
574 if (!yaml_emitter_emit(&emitter, &event))
575 goto error;
577 yaml_emitter_delete(&emitter);
578 return 0;
579 error:
580 yaml_emitter_delete(&emitter);
581 return -1;