export pet_expr_access_get_may_access
[pet.git] / emit.c
blob30bdaba238be5668983da6457a4f6181c2fd381d
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 /* Print the isl_multi_pw_aff "mpa" to "emitter".
175 static int emit_multi_pw_aff(yaml_emitter_t *emitter,
176 __isl_keep isl_multi_pw_aff *mpa)
178 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(mpa);
179 isl_printer *p;
180 char *str;
181 int r;
183 p = isl_printer_to_str(ctx);
184 p = isl_printer_print_multi_pw_aff(p, mpa);
185 str = isl_printer_get_str(p);
186 isl_printer_free(p);
187 r = emit_string(emitter, str);
188 free(str);
189 return r;
192 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
194 static int emit_named_multi_pw_aff(yaml_emitter_t *emitter, const char *name,
195 __isl_keep isl_multi_pw_aff *mpa)
197 if (emit_string(emitter, name) < 0)
198 return -1;
199 if (emit_multi_pw_aff(emitter, mpa) < 0)
200 return -1;
201 return 0;
204 /* Print "type" to "emitter".
206 static int emit_type(yaml_emitter_t *emitter, struct pet_type *type)
208 yaml_event_t event;
210 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
211 YAML_BLOCK_MAPPING_STYLE))
212 return -1;
213 if (!yaml_emitter_emit(emitter, &event))
214 return -1;
216 if (emit_string(emitter, "name") < 0)
217 return -1;
218 if (emit_string(emitter, type->name) < 0)
219 return -1;
221 if (emit_string(emitter, "definition") < 0)
222 return -1;
223 if (emit_string(emitter, type->definition) < 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 /* Print the list of "n_type" "types", if any, to "emitter".
236 static int emit_types(yaml_emitter_t *emitter, int n_type,
237 struct pet_type **types)
239 int i;
240 yaml_event_t event;
242 if (n_type == 0)
243 return 0;
245 if (emit_string(emitter, "types") < 0)
246 return -1;
247 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
248 YAML_BLOCK_SEQUENCE_STYLE))
249 return -1;
250 if (!yaml_emitter_emit(emitter, &event))
251 return -1;
253 for (i = 0; i < n_type; ++i)
254 if (emit_type(emitter, types[i]) < 0)
255 return -1;
257 if (!yaml_sequence_end_event_initialize(&event))
258 return -1;
259 if (!yaml_emitter_emit(emitter, &event))
260 return -1;
262 return 0;
265 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
267 yaml_event_t event;
269 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
270 YAML_BLOCK_MAPPING_STYLE))
271 return -1;
272 if (!yaml_emitter_emit(emitter, &event))
273 return -1;
275 if (emit_string(emitter, "context") < 0)
276 return -1;
277 if (emit_set(emitter, array->context) < 0)
278 return -1;
280 if (emit_string(emitter, "extent") < 0)
281 return -1;
282 if (emit_set(emitter, array->extent) < 0)
283 return -1;
285 if (array->value_bounds) {
286 if (emit_string(emitter, "value_bounds") < 0)
287 return -1;
288 if (emit_set(emitter, array->value_bounds) < 0)
289 return -1;
292 if (emit_string(emitter, "element_type") < 0)
293 return -1;
294 if (emit_string(emitter, array->element_type) < 0)
295 return -1;
296 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
297 return -1;
299 if (array->live_out) {
300 if (emit_string(emitter, "live_out") < 0)
301 return -1;
302 if (emit_string(emitter, "1") < 0)
303 return -1;
306 if (array->uniquely_defined) {
307 if (emit_string(emitter, "uniquely_defined") < 0)
308 return -1;
309 if (emit_string(emitter, "1") < 0)
310 return -1;
313 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
314 return -1;
315 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
316 return -1;
318 if (!yaml_mapping_end_event_initialize(&event))
319 return -1;
320 if (!yaml_emitter_emit(emitter, &event))
321 return -1;
323 return 0;
326 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
327 struct pet_array **arrays)
329 int i;
330 yaml_event_t event;
332 if (emit_string(emitter, "arrays") < 0)
333 return -1;
334 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
335 YAML_BLOCK_SEQUENCE_STYLE))
336 return -1;
337 if (!yaml_emitter_emit(emitter, &event))
338 return -1;
340 for (i = 0; i < n_array; ++i)
341 if (emit_array(emitter, arrays[i]) < 0)
342 return -1;
344 if (!yaml_sequence_end_event_initialize(&event))
345 return -1;
346 if (!yaml_emitter_emit(emitter, &event))
347 return -1;
349 return 0;
352 static int emit_expr_type(yaml_emitter_t *emitter, enum pet_expr_type type)
354 if (emit_string(emitter, pet_type_str(type)) < 0)
355 return -1;
356 return 0;
359 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
361 yaml_event_t event;
363 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
364 YAML_BLOCK_MAPPING_STYLE))
365 return -1;
366 if (!yaml_emitter_emit(emitter, &event))
367 return -1;
369 if (emit_string(emitter, "type") < 0)
370 return -1;
371 if (emit_expr_type(emitter, expr->type) < 0)
372 return -1;
374 switch (expr->type) {
375 case pet_expr_double:
376 if (emit_string(emitter, "value") < 0)
377 return -1;
378 if (emit_double(emitter, expr->d.val) < 0)
379 return -1;
380 if (emit_string(emitter, "string") < 0)
381 return -1;
382 if (emit_string(emitter, expr->d.s) < 0)
383 return -1;
384 break;
385 case pet_expr_access:
386 if (emit_string(emitter, "relation") < 0)
387 return -1;
388 if (emit_map(emitter, expr->acc.access) < 0)
389 return -1;
390 if (emit_named_multi_pw_aff(emitter,
391 "index", expr->acc.index) < 0)
392 return -1;
393 if (expr->acc.ref_id &&
394 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
395 return -1;
396 if (emit_string(emitter, "read") < 0)
397 return -1;
398 if (emit_int(emitter, expr->acc.read) < 0)
399 return -1;
400 if (emit_string(emitter, "write") < 0)
401 return -1;
402 if (emit_int(emitter, expr->acc.write) < 0)
403 return -1;
404 break;
405 case pet_expr_unary:
406 case pet_expr_binary:
407 if (emit_string(emitter, "operation") < 0)
408 return -1;
409 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
410 return -1;
411 break;
412 case pet_expr_ternary:
413 break;
414 case pet_expr_call:
415 if (emit_string(emitter, "name") < 0)
416 return -1;
417 if (emit_string(emitter, expr->name) < 0)
418 return -1;
419 break;
420 case pet_expr_cast:
421 if (emit_string(emitter, "type_name") < 0)
422 return -1;
423 if (emit_string(emitter, expr->type_name) < 0)
424 return -1;
425 break;
428 if (expr->n_arg > 0) {
429 int i;
431 if (emit_string(emitter, "arguments") < 0)
432 return -1;
433 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
434 YAML_BLOCK_SEQUENCE_STYLE))
435 return -1;
436 if (!yaml_emitter_emit(emitter, &event))
437 return -1;
439 for (i = 0; i < expr->n_arg; ++i)
440 if (emit_expr(emitter, expr->args[i]) < 0)
441 return -1;
443 if (!yaml_sequence_end_event_initialize(&event))
444 return -1;
445 if (!yaml_emitter_emit(emitter, &event))
446 return -1;
449 if (!yaml_mapping_end_event_initialize(&event))
450 return -1;
451 if (!yaml_emitter_emit(emitter, &event))
452 return -1;
454 return 0;
457 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
459 yaml_event_t event;
461 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
462 YAML_BLOCK_MAPPING_STYLE))
463 return -1;
464 if (!yaml_emitter_emit(emitter, &event))
465 return -1;
467 if (emit_string(emitter, "line") < 0)
468 return -1;
469 if (emit_int(emitter, stmt->line) < 0)
470 return -1;
472 if (emit_string(emitter, "domain") < 0)
473 return -1;
474 if (emit_set(emitter, stmt->domain) < 0)
475 return -1;
477 if (emit_string(emitter, "schedule") < 0)
478 return -1;
479 if (emit_map(emitter, stmt->schedule) < 0)
480 return -1;
482 if (emit_string(emitter, "body") < 0)
483 return -1;
484 if (emit_expr(emitter, stmt->body) < 0)
485 return -1;
487 if (stmt->n_arg > 0) {
488 int i;
490 if (emit_string(emitter, "arguments") < 0)
491 return -1;
492 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
493 YAML_BLOCK_SEQUENCE_STYLE))
494 return -1;
495 if (!yaml_emitter_emit(emitter, &event))
496 return -1;
498 for (i = 0; i < stmt->n_arg; ++i)
499 if (emit_expr(emitter, stmt->args[i]) < 0)
500 return -1;
502 if (!yaml_sequence_end_event_initialize(&event))
503 return -1;
504 if (!yaml_emitter_emit(emitter, &event))
505 return -1;
508 if (!yaml_mapping_end_event_initialize(&event))
509 return -1;
510 if (!yaml_emitter_emit(emitter, &event))
511 return -1;
513 return 0;
516 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
517 struct pet_stmt **stmts)
519 int i;
520 yaml_event_t event;
522 if (emit_string(emitter, "statements") < 0)
523 return -1;
524 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
525 YAML_BLOCK_SEQUENCE_STYLE))
526 return -1;
527 if (!yaml_emitter_emit(emitter, &event))
528 return -1;
530 for (i = 0; i < n_stmt; ++i)
531 if (emit_stmt(emitter, stmts[i]) < 0)
532 return -1;
534 if (!yaml_sequence_end_event_initialize(&event))
535 return -1;
536 if (!yaml_emitter_emit(emitter, &event))
537 return -1;
539 return 0;
542 /* Print "implication" to "emitter".
544 static int emit_implication(yaml_emitter_t *emitter,
545 struct pet_implication *implication)
547 yaml_event_t event;
549 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
550 YAML_BLOCK_MAPPING_STYLE))
551 return -1;
552 if (!yaml_emitter_emit(emitter, &event))
553 return -1;
555 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
556 return -1;
558 if (emit_named_map(emitter, "extension", implication->extension) < 0)
559 return -1;
561 if (!yaml_mapping_end_event_initialize(&event))
562 return -1;
563 if (!yaml_emitter_emit(emitter, &event))
564 return -1;
566 return 0;
569 /* Print the list of "n_implication" "implications", if any, to "emitter".
571 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
572 struct pet_implication **implications)
574 int i;
575 yaml_event_t event;
577 if (n_implication == 0)
578 return 0;
580 if (emit_string(emitter, "implications") < 0)
581 return -1;
582 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
583 YAML_BLOCK_SEQUENCE_STYLE))
584 return -1;
585 if (!yaml_emitter_emit(emitter, &event))
586 return -1;
588 for (i = 0; i < n_implication; ++i)
589 if (emit_implication(emitter, implications[i]) < 0)
590 return -1;
592 if (!yaml_sequence_end_event_initialize(&event))
593 return -1;
594 if (!yaml_emitter_emit(emitter, &event))
595 return -1;
597 return 0;
600 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
602 yaml_event_t event;
604 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
605 YAML_BLOCK_MAPPING_STYLE))
606 return -1;
607 if (!yaml_emitter_emit(emitter, &event))
608 return -1;
610 if (emit_named_unsigned(emitter, "start", scop->start) < 0)
611 return -1;
612 if (emit_named_unsigned(emitter, "end", scop->end) < 0)
613 return -1;
614 if (emit_string(emitter, "context") < 0)
615 return -1;
616 if (emit_set(emitter, scop->context) < 0)
617 return -1;
618 if (!isl_set_plain_is_universe(scop->context_value) &&
619 emit_named_set(emitter, "context_value", scop->context_value) < 0)
620 return -1;
622 if (emit_types(emitter, scop->n_type, scop->types) < 0)
623 return -1;
624 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
625 return -1;
627 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
628 return -1;
630 if (emit_implications(emitter, scop->n_implication,
631 scop->implications) < 0)
632 return -1;
634 if (!yaml_mapping_end_event_initialize(&event))
635 return -1;
636 if (!yaml_emitter_emit(emitter, &event))
637 return -1;
639 return 0;
642 /* Print a YAML serialization of "scop" to "out".
644 int pet_scop_emit(FILE *out, struct pet_scop *scop)
646 yaml_emitter_t emitter;
647 yaml_event_t event;
649 yaml_emitter_initialize(&emitter);
651 yaml_emitter_set_output_file(&emitter, out);
653 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
654 if (!yaml_emitter_emit(&emitter, &event))
655 goto error;
657 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
658 goto error;
659 if (!yaml_emitter_emit(&emitter, &event))
660 goto error;
662 if (emit_scop(&emitter, scop) < 0)
663 goto error;
665 if (!yaml_document_end_event_initialize(&event, 1))
666 goto error;
667 if (!yaml_emitter_emit(&emitter, &event))
668 goto error;
670 yaml_stream_end_event_initialize(&event);
671 if (!yaml_emitter_emit(&emitter, &event))
672 goto error;
674 yaml_emitter_delete(&emitter);
675 return 0;
676 error:
677 yaml_emitter_delete(&emitter);
678 return -1;