update isl for support for member accesses in AST
[pet.git] / emit.c
blob93644e9fc7c661e439f8a23463d77205cee19712
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 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
206 yaml_event_t event;
208 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
209 YAML_BLOCK_MAPPING_STYLE))
210 return -1;
211 if (!yaml_emitter_emit(emitter, &event))
212 return -1;
214 if (emit_string(emitter, "context") < 0)
215 return -1;
216 if (emit_set(emitter, array->context) < 0)
217 return -1;
219 if (emit_string(emitter, "extent") < 0)
220 return -1;
221 if (emit_set(emitter, array->extent) < 0)
222 return -1;
224 if (array->value_bounds) {
225 if (emit_string(emitter, "value_bounds") < 0)
226 return -1;
227 if (emit_set(emitter, array->value_bounds) < 0)
228 return -1;
231 if (emit_string(emitter, "element_type") < 0)
232 return -1;
233 if (emit_string(emitter, array->element_type) < 0)
234 return -1;
235 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
236 return -1;
238 if (array->live_out) {
239 if (emit_string(emitter, "live_out") < 0)
240 return -1;
241 if (emit_string(emitter, "1") < 0)
242 return -1;
245 if (array->uniquely_defined) {
246 if (emit_string(emitter, "uniquely_defined") < 0)
247 return -1;
248 if (emit_string(emitter, "1") < 0)
249 return -1;
252 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
253 return -1;
254 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
255 return -1;
257 if (!yaml_mapping_end_event_initialize(&event))
258 return -1;
259 if (!yaml_emitter_emit(emitter, &event))
260 return -1;
262 return 0;
265 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
266 struct pet_array **arrays)
268 int i;
269 yaml_event_t event;
271 if (emit_string(emitter, "arrays") < 0)
272 return -1;
273 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
274 YAML_BLOCK_SEQUENCE_STYLE))
275 return -1;
276 if (!yaml_emitter_emit(emitter, &event))
277 return -1;
279 for (i = 0; i < n_array; ++i)
280 if (emit_array(emitter, arrays[i]) < 0)
281 return -1;
283 if (!yaml_sequence_end_event_initialize(&event))
284 return -1;
285 if (!yaml_emitter_emit(emitter, &event))
286 return -1;
288 return 0;
291 static int emit_type(yaml_emitter_t *emitter, enum pet_expr_type type)
293 if (emit_string(emitter, pet_type_str(type)) < 0)
294 return -1;
295 return 0;
298 static int emit_expr(yaml_emitter_t *emitter, struct pet_expr *expr)
300 yaml_event_t event;
302 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
303 YAML_BLOCK_MAPPING_STYLE))
304 return -1;
305 if (!yaml_emitter_emit(emitter, &event))
306 return -1;
308 if (emit_string(emitter, "type") < 0)
309 return -1;
310 if (emit_type(emitter, expr->type) < 0)
311 return -1;
313 switch (expr->type) {
314 case pet_expr_double:
315 if (emit_string(emitter, "value") < 0)
316 return -1;
317 if (emit_double(emitter, expr->d.val) < 0)
318 return -1;
319 if (emit_string(emitter, "string") < 0)
320 return -1;
321 if (emit_string(emitter, expr->d.s) < 0)
322 return -1;
323 break;
324 case pet_expr_access:
325 if (emit_string(emitter, "relation") < 0)
326 return -1;
327 if (emit_map(emitter, expr->acc.access) < 0)
328 return -1;
329 if (emit_named_multi_pw_aff(emitter,
330 "index", expr->acc.index) < 0)
331 return -1;
332 if (expr->acc.ref_id &&
333 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
334 return -1;
335 if (emit_string(emitter, "read") < 0)
336 return -1;
337 if (emit_int(emitter, expr->acc.read) < 0)
338 return -1;
339 if (emit_string(emitter, "write") < 0)
340 return -1;
341 if (emit_int(emitter, expr->acc.write) < 0)
342 return -1;
343 break;
344 case pet_expr_unary:
345 case pet_expr_binary:
346 if (emit_string(emitter, "operation") < 0)
347 return -1;
348 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
349 return -1;
350 break;
351 case pet_expr_ternary:
352 break;
353 case pet_expr_call:
354 if (emit_string(emitter, "name") < 0)
355 return -1;
356 if (emit_string(emitter, expr->name) < 0)
357 return -1;
358 break;
359 case pet_expr_cast:
360 if (emit_string(emitter, "type_name") < 0)
361 return -1;
362 if (emit_string(emitter, expr->type_name) < 0)
363 return -1;
364 break;
367 if (expr->n_arg > 0) {
368 int i;
370 if (emit_string(emitter, "arguments") < 0)
371 return -1;
372 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
373 YAML_BLOCK_SEQUENCE_STYLE))
374 return -1;
375 if (!yaml_emitter_emit(emitter, &event))
376 return -1;
378 for (i = 0; i < expr->n_arg; ++i)
379 if (emit_expr(emitter, expr->args[i]) < 0)
380 return -1;
382 if (!yaml_sequence_end_event_initialize(&event))
383 return -1;
384 if (!yaml_emitter_emit(emitter, &event))
385 return -1;
388 if (!yaml_mapping_end_event_initialize(&event))
389 return -1;
390 if (!yaml_emitter_emit(emitter, &event))
391 return -1;
393 return 0;
396 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
398 yaml_event_t event;
400 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
401 YAML_BLOCK_MAPPING_STYLE))
402 return -1;
403 if (!yaml_emitter_emit(emitter, &event))
404 return -1;
406 if (emit_string(emitter, "line") < 0)
407 return -1;
408 if (emit_int(emitter, stmt->line) < 0)
409 return -1;
411 if (emit_string(emitter, "domain") < 0)
412 return -1;
413 if (emit_set(emitter, stmt->domain) < 0)
414 return -1;
416 if (emit_string(emitter, "schedule") < 0)
417 return -1;
418 if (emit_map(emitter, stmt->schedule) < 0)
419 return -1;
421 if (emit_string(emitter, "body") < 0)
422 return -1;
423 if (emit_expr(emitter, stmt->body) < 0)
424 return -1;
426 if (stmt->n_arg > 0) {
427 int i;
429 if (emit_string(emitter, "arguments") < 0)
430 return -1;
431 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
432 YAML_BLOCK_SEQUENCE_STYLE))
433 return -1;
434 if (!yaml_emitter_emit(emitter, &event))
435 return -1;
437 for (i = 0; i < stmt->n_arg; ++i)
438 if (emit_expr(emitter, stmt->args[i]) < 0)
439 return -1;
441 if (!yaml_sequence_end_event_initialize(&event))
442 return -1;
443 if (!yaml_emitter_emit(emitter, &event))
444 return -1;
447 if (!yaml_mapping_end_event_initialize(&event))
448 return -1;
449 if (!yaml_emitter_emit(emitter, &event))
450 return -1;
452 return 0;
455 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
456 struct pet_stmt **stmts)
458 int i;
459 yaml_event_t event;
461 if (emit_string(emitter, "statements") < 0)
462 return -1;
463 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
464 YAML_BLOCK_SEQUENCE_STYLE))
465 return -1;
466 if (!yaml_emitter_emit(emitter, &event))
467 return -1;
469 for (i = 0; i < n_stmt; ++i)
470 if (emit_stmt(emitter, stmts[i]) < 0)
471 return -1;
473 if (!yaml_sequence_end_event_initialize(&event))
474 return -1;
475 if (!yaml_emitter_emit(emitter, &event))
476 return -1;
478 return 0;
481 /* Print "implication" to "emitter".
483 static int emit_implication(yaml_emitter_t *emitter,
484 struct pet_implication *implication)
486 yaml_event_t event;
488 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
489 YAML_BLOCK_MAPPING_STYLE))
490 return -1;
491 if (!yaml_emitter_emit(emitter, &event))
492 return -1;
494 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
495 return -1;
497 if (emit_named_map(emitter, "extension", implication->extension) < 0)
498 return -1;
500 if (!yaml_mapping_end_event_initialize(&event))
501 return -1;
502 if (!yaml_emitter_emit(emitter, &event))
503 return -1;
505 return 0;
508 /* Print the list of "n_implication" "implications", if any, to "emitter".
510 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
511 struct pet_implication **implications)
513 int i;
514 yaml_event_t event;
516 if (n_implication == 0)
517 return 0;
519 if (emit_string(emitter, "implications") < 0)
520 return -1;
521 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
522 YAML_BLOCK_SEQUENCE_STYLE))
523 return -1;
524 if (!yaml_emitter_emit(emitter, &event))
525 return -1;
527 for (i = 0; i < n_implication; ++i)
528 if (emit_implication(emitter, implications[i]) < 0)
529 return -1;
531 if (!yaml_sequence_end_event_initialize(&event))
532 return -1;
533 if (!yaml_emitter_emit(emitter, &event))
534 return -1;
536 return 0;
539 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
541 yaml_event_t event;
543 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
544 YAML_BLOCK_MAPPING_STYLE))
545 return -1;
546 if (!yaml_emitter_emit(emitter, &event))
547 return -1;
549 if (emit_named_unsigned(emitter, "start", scop->start) < 0)
550 return -1;
551 if (emit_named_unsigned(emitter, "end", scop->end) < 0)
552 return -1;
553 if (emit_string(emitter, "context") < 0)
554 return -1;
555 if (emit_set(emitter, scop->context) < 0)
556 return -1;
557 if (!isl_set_plain_is_universe(scop->context_value) &&
558 emit_named_set(emitter, "context_value", scop->context_value) < 0)
559 return -1;
561 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
562 return -1;
564 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
565 return -1;
567 if (emit_implications(emitter, scop->n_implication,
568 scop->implications) < 0)
569 return -1;
571 if (!yaml_mapping_end_event_initialize(&event))
572 return -1;
573 if (!yaml_emitter_emit(emitter, &event))
574 return -1;
576 return 0;
579 /* Print a YAML serialization of "scop" to "out".
581 int pet_scop_emit(FILE *out, struct pet_scop *scop)
583 yaml_emitter_t emitter;
584 yaml_event_t event;
586 yaml_emitter_initialize(&emitter);
588 yaml_emitter_set_output_file(&emitter, out);
590 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
591 if (!yaml_emitter_emit(&emitter, &event))
592 goto error;
594 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
595 goto error;
596 if (!yaml_emitter_emit(&emitter, &event))
597 goto error;
599 if (emit_scop(&emitter, scop) < 0)
600 goto error;
602 if (!yaml_document_end_event_initialize(&event, 1))
603 goto error;
604 if (!yaml_emitter_emit(&emitter, &event))
605 goto error;
607 yaml_stream_end_event_initialize(&event);
608 if (!yaml_emitter_emit(&emitter, &event))
609 goto error;
611 yaml_emitter_delete(&emitter);
612 return 0;
613 error:
614 yaml_emitter_delete(&emitter);
615 return -1;