2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
40 #include "scop_yaml.h"
42 static int emit_string(yaml_emitter_t
*emitter
, const char *str
)
46 if (!yaml_scalar_event_initialize(&event
, NULL
, NULL
,
47 (yaml_char_t
*) str
, strlen(str
),
48 1, 1, YAML_PLAIN_SCALAR_STYLE
))
50 if (!yaml_emitter_emit(emitter
, &event
))
56 /* Print the string "name" and the string "str" to "emitter".
58 static int emit_named_string(yaml_emitter_t
*emitter
, const char *name
,
61 if (emit_string(emitter
, name
) < 0)
63 if (emit_string(emitter
, str
) < 0)
68 /* Print the isl_id "id" to "emitter".
70 static int emit_id(yaml_emitter_t
*emitter
, __isl_keep isl_id
*id
)
72 return emit_string(emitter
, isl_id_get_name(id
));
75 /* Print the string "name" and the isl_id "id" to "emitter".
77 static int emit_named_id(yaml_emitter_t
*emitter
, const char *name
,
78 __isl_keep isl_id
*id
)
80 if (emit_string(emitter
, name
) < 0)
82 if (emit_id(emitter
, id
) < 0)
87 static int emit_int(yaml_emitter_t
*emitter
, int i
)
91 snprintf(buffer
, sizeof(buffer
), "%d", i
);
92 return emit_string(emitter
, buffer
);
95 static int emit_named_int(yaml_emitter_t
*emitter
, const char *name
, int i
)
97 if (emit_string(emitter
, name
) < 0)
99 if (emit_int(emitter
, i
) < 0)
104 /* Print the unsigned integer "u" to "emitter".
106 static int emit_unsigned(yaml_emitter_t
*emitter
, unsigned u
)
110 snprintf(buffer
, sizeof(buffer
), "%u", u
);
111 return emit_string(emitter
, buffer
);
114 /* Print the string "name" and the unsigned integer "u" to "emitter".
116 static int emit_named_unsigned(yaml_emitter_t
*emitter
, const char *name
,
119 if (emit_string(emitter
, name
) < 0)
121 if (emit_int(emitter
, u
) < 0)
126 static int emit_double(yaml_emitter_t
*emitter
, double d
)
130 snprintf(buffer
, sizeof(buffer
), "%g", d
);
131 return emit_string(emitter
, buffer
);
134 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
136 isl_ctx
*ctx
= isl_map_get_ctx(map
);
141 p
= isl_printer_to_str(ctx
);
142 p
= isl_printer_print_map(p
, map
);
143 str
= isl_printer_get_str(p
);
145 r
= emit_string(emitter
, str
);
150 /* Print the isl_val "val" to "emitter".
152 static int emit_val(yaml_emitter_t
*emitter
, __isl_keep isl_val
*val
)
154 isl_ctx
*ctx
= isl_val_get_ctx(val
);
159 p
= isl_printer_to_str(ctx
);
160 p
= isl_printer_print_val(p
, val
);
161 str
= isl_printer_get_str(p
);
163 r
= emit_string(emitter
, str
);
168 /* Print the string "name" and the isl_val "val" to "emitter".
170 static int emit_named_val(yaml_emitter_t
*emitter
, const char *name
,
171 __isl_keep isl_val
*val
)
173 if (emit_string(emitter
, name
) < 0)
175 if (emit_val(emitter
, val
) < 0)
180 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
182 isl_ctx
*ctx
= isl_set_get_ctx(set
);
187 p
= isl_printer_to_str(ctx
);
188 p
= isl_printer_print_set(p
, set
);
189 str
= isl_printer_get_str(p
);
191 r
= emit_string(emitter
, str
);
196 static int emit_named_set(yaml_emitter_t
*emitter
, const char *name
,
197 __isl_keep isl_set
*set
)
199 if (emit_string(emitter
, name
) < 0)
201 if (emit_set(emitter
, set
) < 0)
206 /* Print the string "name" and the map "map" to "emitter".
208 static int emit_named_map(yaml_emitter_t
*emitter
, const char *name
,
209 __isl_keep isl_map
*map
)
211 if (emit_string(emitter
, name
) < 0)
213 if (emit_map(emitter
, map
) < 0)
218 /* Print the isl_multi_pw_aff "mpa" to "emitter".
220 static int emit_multi_pw_aff(yaml_emitter_t
*emitter
,
221 __isl_keep isl_multi_pw_aff
*mpa
)
223 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(mpa
);
228 p
= isl_printer_to_str(ctx
);
229 p
= isl_printer_print_multi_pw_aff(p
, mpa
);
230 str
= isl_printer_get_str(p
);
232 r
= emit_string(emitter
, str
);
237 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
239 static int emit_named_multi_pw_aff(yaml_emitter_t
*emitter
, const char *name
,
240 __isl_keep isl_multi_pw_aff
*mpa
)
242 if (emit_string(emitter
, name
) < 0)
244 if (emit_multi_pw_aff(emitter
, mpa
) < 0)
249 /* Print "type" to "emitter".
251 static int emit_type(yaml_emitter_t
*emitter
, struct pet_type
*type
)
255 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
256 YAML_BLOCK_MAPPING_STYLE
))
258 if (!yaml_emitter_emit(emitter
, &event
))
261 if (emit_string(emitter
, "name") < 0)
263 if (emit_string(emitter
, type
->name
) < 0)
266 if (emit_string(emitter
, "definition") < 0)
268 if (emit_string(emitter
, type
->definition
) < 0)
271 if (!yaml_mapping_end_event_initialize(&event
))
273 if (!yaml_emitter_emit(emitter
, &event
))
279 /* Print the list of "n_type" "types", if any, to "emitter".
281 static int emit_types(yaml_emitter_t
*emitter
, int n_type
,
282 struct pet_type
**types
)
290 if (emit_string(emitter
, "types") < 0)
292 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
293 YAML_BLOCK_SEQUENCE_STYLE
))
295 if (!yaml_emitter_emit(emitter
, &event
))
298 for (i
= 0; i
< n_type
; ++i
)
299 if (emit_type(emitter
, types
[i
]) < 0)
302 if (!yaml_sequence_end_event_initialize(&event
))
304 if (!yaml_emitter_emit(emitter
, &event
))
310 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
314 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
315 YAML_BLOCK_MAPPING_STYLE
))
317 if (!yaml_emitter_emit(emitter
, &event
))
320 if (emit_string(emitter
, "context") < 0)
322 if (emit_set(emitter
, array
->context
) < 0)
325 if (emit_string(emitter
, "extent") < 0)
327 if (emit_set(emitter
, array
->extent
) < 0)
330 if (array
->value_bounds
) {
331 if (emit_string(emitter
, "value_bounds") < 0)
333 if (emit_set(emitter
, array
->value_bounds
) < 0)
337 if (emit_string(emitter
, "element_type") < 0)
339 if (emit_string(emitter
, array
->element_type
) < 0)
341 if (emit_named_int(emitter
, "element_size", array
->element_size
) < 0)
344 if (array
->element_is_record
)
345 if (emit_named_int(emitter
, "element_is_record",
346 array
->element_is_record
) < 0)
349 if (array
->live_out
) {
350 if (emit_string(emitter
, "live_out") < 0)
352 if (emit_string(emitter
, "1") < 0)
356 if (array
->uniquely_defined
) {
357 if (emit_string(emitter
, "uniquely_defined") < 0)
359 if (emit_string(emitter
, "1") < 0)
363 if (array
->declared
&& emit_named_int(emitter
, "declared", 1) < 0)
365 if (array
->exposed
&& emit_named_int(emitter
, "exposed", 1) < 0)
368 if (!yaml_mapping_end_event_initialize(&event
))
370 if (!yaml_emitter_emit(emitter
, &event
))
376 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
377 struct pet_array
**arrays
)
382 if (emit_string(emitter
, "arrays") < 0)
384 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
385 YAML_BLOCK_SEQUENCE_STYLE
))
387 if (!yaml_emitter_emit(emitter
, &event
))
390 for (i
= 0; i
< n_array
; ++i
)
391 if (emit_array(emitter
, arrays
[i
]) < 0)
394 if (!yaml_sequence_end_event_initialize(&event
))
396 if (!yaml_emitter_emit(emitter
, &event
))
402 static int emit_expr_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
404 if (emit_string(emitter
, pet_type_str(type
)) < 0)
409 static int emit_expr(yaml_emitter_t
*emitter
, __isl_keep pet_expr
*expr
)
413 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
414 YAML_BLOCK_MAPPING_STYLE
))
416 if (!yaml_emitter_emit(emitter
, &event
))
419 if (emit_string(emitter
, "type") < 0)
421 if (emit_expr_type(emitter
, expr
->type
) < 0)
424 switch (expr
->type
) {
428 if (emit_named_val(emitter
, "value", expr
->i
) < 0)
431 case pet_expr_double
:
432 if (emit_string(emitter
, "value") < 0)
434 if (emit_double(emitter
, expr
->d
.val
) < 0)
436 if (emit_string(emitter
, "string") < 0)
438 if (emit_string(emitter
, expr
->d
.s
) < 0)
441 case pet_expr_access
:
442 if (emit_string(emitter
, "relation") < 0)
444 if (emit_map(emitter
, expr
->acc
.access
) < 0)
446 if (emit_named_multi_pw_aff(emitter
,
447 "index", expr
->acc
.index
) < 0)
449 if (expr
->acc
.ref_id
&&
450 emit_named_id(emitter
, "reference", expr
->acc
.ref_id
) < 0)
452 if (emit_string(emitter
, "read") < 0)
454 if (emit_int(emitter
, expr
->acc
.read
) < 0)
456 if (emit_string(emitter
, "write") < 0)
458 if (emit_int(emitter
, expr
->acc
.write
) < 0)
462 if (emit_string(emitter
, "operation") < 0)
464 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
468 if (emit_string(emitter
, "name") < 0)
470 if (emit_string(emitter
, expr
->name
) < 0)
474 if (emit_string(emitter
, "type_name") < 0)
476 if (emit_string(emitter
, expr
->type_name
) < 0)
481 if (expr
->n_arg
> 0) {
484 if (emit_string(emitter
, "arguments") < 0)
486 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
487 YAML_BLOCK_SEQUENCE_STYLE
))
489 if (!yaml_emitter_emit(emitter
, &event
))
492 for (i
= 0; i
< expr
->n_arg
; ++i
)
493 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
496 if (!yaml_sequence_end_event_initialize(&event
))
498 if (!yaml_emitter_emit(emitter
, &event
))
502 if (!yaml_mapping_end_event_initialize(&event
))
504 if (!yaml_emitter_emit(emitter
, &event
))
510 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
514 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
515 YAML_BLOCK_MAPPING_STYLE
))
517 if (!yaml_emitter_emit(emitter
, &event
))
520 if (emit_string(emitter
, "line") < 0)
522 if (emit_int(emitter
, pet_loc_get_line(stmt
->loc
)) < 0)
525 if (emit_string(emitter
, "domain") < 0)
527 if (emit_set(emitter
, stmt
->domain
) < 0)
530 if (emit_string(emitter
, "schedule") < 0)
532 if (emit_map(emitter
, stmt
->schedule
) < 0)
535 if (emit_string(emitter
, "body") < 0)
537 if (emit_expr(emitter
, stmt
->body
) < 0)
540 if (stmt
->n_arg
> 0) {
543 if (emit_string(emitter
, "arguments") < 0)
545 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
546 YAML_BLOCK_SEQUENCE_STYLE
))
548 if (!yaml_emitter_emit(emitter
, &event
))
551 for (i
= 0; i
< stmt
->n_arg
; ++i
)
552 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
555 if (!yaml_sequence_end_event_initialize(&event
))
557 if (!yaml_emitter_emit(emitter
, &event
))
561 if (!yaml_mapping_end_event_initialize(&event
))
563 if (!yaml_emitter_emit(emitter
, &event
))
569 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
570 struct pet_stmt
**stmts
)
575 if (emit_string(emitter
, "statements") < 0)
577 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
578 YAML_BLOCK_SEQUENCE_STYLE
))
580 if (!yaml_emitter_emit(emitter
, &event
))
583 for (i
= 0; i
< n_stmt
; ++i
)
584 if (emit_stmt(emitter
, stmts
[i
]) < 0)
587 if (!yaml_sequence_end_event_initialize(&event
))
589 if (!yaml_emitter_emit(emitter
, &event
))
595 /* Print "implication" to "emitter".
597 static int emit_implication(yaml_emitter_t
*emitter
,
598 struct pet_implication
*implication
)
602 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
603 YAML_BLOCK_MAPPING_STYLE
))
605 if (!yaml_emitter_emit(emitter
, &event
))
608 if (emit_named_int(emitter
, "satisfied", implication
->satisfied
) < 0)
611 if (emit_named_map(emitter
, "extension", implication
->extension
) < 0)
614 if (!yaml_mapping_end_event_initialize(&event
))
616 if (!yaml_emitter_emit(emitter
, &event
))
622 /* Print the list of "n_implication" "implications", if any, to "emitter".
624 static int emit_implications(yaml_emitter_t
*emitter
, int n_implication
,
625 struct pet_implication
**implications
)
630 if (n_implication
== 0)
633 if (emit_string(emitter
, "implications") < 0)
635 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
636 YAML_BLOCK_SEQUENCE_STYLE
))
638 if (!yaml_emitter_emit(emitter
, &event
))
641 for (i
= 0; i
< n_implication
; ++i
)
642 if (emit_implication(emitter
, implications
[i
]) < 0)
645 if (!yaml_sequence_end_event_initialize(&event
))
647 if (!yaml_emitter_emit(emitter
, &event
))
653 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
657 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
658 YAML_BLOCK_MAPPING_STYLE
))
660 if (!yaml_emitter_emit(emitter
, &event
))
663 if (emit_named_unsigned(emitter
,
664 "start", pet_loc_get_start(scop
->loc
)) < 0)
666 if (emit_named_unsigned(emitter
, "end", pet_loc_get_end(scop
->loc
)) < 0)
668 if (emit_named_string(emitter
,
669 "indent", pet_loc_get_indent(scop
->loc
)) < 0)
671 if (emit_string(emitter
, "context") < 0)
673 if (emit_set(emitter
, scop
->context
) < 0)
675 if (!isl_set_plain_is_universe(scop
->context_value
) &&
676 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
679 if (emit_types(emitter
, scop
->n_type
, scop
->types
) < 0)
681 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
684 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
687 if (emit_implications(emitter
, scop
->n_implication
,
688 scop
->implications
) < 0)
691 if (!yaml_mapping_end_event_initialize(&event
))
693 if (!yaml_emitter_emit(emitter
, &event
))
699 /* Print a YAML serialization of "scop" to "out".
701 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
703 yaml_emitter_t emitter
;
706 yaml_emitter_initialize(&emitter
);
708 yaml_emitter_set_output_file(&emitter
, out
);
710 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
711 if (!yaml_emitter_emit(&emitter
, &event
))
714 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
716 if (!yaml_emitter_emit(&emitter
, &event
))
719 if (emit_scop(&emitter
, scop
) < 0)
722 if (!yaml_document_end_event_initialize(&event
, 1))
724 if (!yaml_emitter_emit(&emitter
, &event
))
727 yaml_stream_end_event_initialize(&event
);
728 if (!yaml_emitter_emit(&emitter
, &event
))
731 yaml_emitter_delete(&emitter
);
734 yaml_emitter_delete(&emitter
);