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 isl_id "id" to "emitter".
58 static int emit_id(yaml_emitter_t
*emitter
, __isl_keep isl_id
*id
)
60 return emit_string(emitter
, isl_id_get_name(id
));
63 /* Print the string "name" and the isl_id "id" to "emitter".
65 static int emit_named_id(yaml_emitter_t
*emitter
, const char *name
,
66 __isl_keep isl_id
*id
)
68 if (emit_string(emitter
, name
) < 0)
70 if (emit_id(emitter
, id
) < 0)
75 static int emit_int(yaml_emitter_t
*emitter
, int i
)
79 snprintf(buffer
, sizeof(buffer
), "%d", i
);
80 return emit_string(emitter
, buffer
);
83 static int emit_named_int(yaml_emitter_t
*emitter
, const char *name
, int i
)
85 if (emit_string(emitter
, name
) < 0)
87 if (emit_int(emitter
, i
) < 0)
92 /* Print the unsigned integer "u" to "emitter".
94 static int emit_unsigned(yaml_emitter_t
*emitter
, unsigned u
)
98 snprintf(buffer
, sizeof(buffer
), "%u", u
);
99 return emit_string(emitter
, buffer
);
102 /* Print the string "name" and the unsigned integer "u" to "emitter".
104 static int emit_named_unsigned(yaml_emitter_t
*emitter
, const char *name
,
107 if (emit_string(emitter
, name
) < 0)
109 if (emit_int(emitter
, u
) < 0)
114 static int emit_double(yaml_emitter_t
*emitter
, double d
)
118 snprintf(buffer
, sizeof(buffer
), "%g", d
);
119 return emit_string(emitter
, buffer
);
122 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
124 isl_ctx
*ctx
= isl_map_get_ctx(map
);
129 p
= isl_printer_to_str(ctx
);
130 p
= isl_printer_print_map(p
, map
);
131 str
= isl_printer_get_str(p
);
133 r
= emit_string(emitter
, str
);
138 /* Print the isl_val "val" to "emitter".
140 static int emit_val(yaml_emitter_t
*emitter
, __isl_keep isl_val
*val
)
142 isl_ctx
*ctx
= isl_val_get_ctx(val
);
147 p
= isl_printer_to_str(ctx
);
148 p
= isl_printer_print_val(p
, val
);
149 str
= isl_printer_get_str(p
);
151 r
= emit_string(emitter
, str
);
156 /* Print the string "name" and the isl_val "val" to "emitter".
158 static int emit_named_val(yaml_emitter_t
*emitter
, const char *name
,
159 __isl_keep isl_val
*val
)
161 if (emit_string(emitter
, name
) < 0)
163 if (emit_val(emitter
, val
) < 0)
168 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
170 isl_ctx
*ctx
= isl_set_get_ctx(set
);
175 p
= isl_printer_to_str(ctx
);
176 p
= isl_printer_print_set(p
, set
);
177 str
= isl_printer_get_str(p
);
179 r
= emit_string(emitter
, str
);
184 static int emit_named_set(yaml_emitter_t
*emitter
, const char *name
,
185 __isl_keep isl_set
*set
)
187 if (emit_string(emitter
, name
) < 0)
189 if (emit_set(emitter
, set
) < 0)
194 /* Print the string "name" and the map "map" to "emitter".
196 static int emit_named_map(yaml_emitter_t
*emitter
, const char *name
,
197 __isl_keep isl_map
*map
)
199 if (emit_string(emitter
, name
) < 0)
201 if (emit_map(emitter
, map
) < 0)
206 /* Print the isl_multi_pw_aff "mpa" to "emitter".
208 static int emit_multi_pw_aff(yaml_emitter_t
*emitter
,
209 __isl_keep isl_multi_pw_aff
*mpa
)
211 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(mpa
);
216 p
= isl_printer_to_str(ctx
);
217 p
= isl_printer_print_multi_pw_aff(p
, mpa
);
218 str
= isl_printer_get_str(p
);
220 r
= emit_string(emitter
, str
);
225 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
227 static int emit_named_multi_pw_aff(yaml_emitter_t
*emitter
, const char *name
,
228 __isl_keep isl_multi_pw_aff
*mpa
)
230 if (emit_string(emitter
, name
) < 0)
232 if (emit_multi_pw_aff(emitter
, mpa
) < 0)
237 /* Print "type" to "emitter".
239 static int emit_type(yaml_emitter_t
*emitter
, struct pet_type
*type
)
243 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
244 YAML_BLOCK_MAPPING_STYLE
))
246 if (!yaml_emitter_emit(emitter
, &event
))
249 if (emit_string(emitter
, "name") < 0)
251 if (emit_string(emitter
, type
->name
) < 0)
254 if (emit_string(emitter
, "definition") < 0)
256 if (emit_string(emitter
, type
->definition
) < 0)
259 if (!yaml_mapping_end_event_initialize(&event
))
261 if (!yaml_emitter_emit(emitter
, &event
))
267 /* Print the list of "n_type" "types", if any, to "emitter".
269 static int emit_types(yaml_emitter_t
*emitter
, int n_type
,
270 struct pet_type
**types
)
278 if (emit_string(emitter
, "types") < 0)
280 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
281 YAML_BLOCK_SEQUENCE_STYLE
))
283 if (!yaml_emitter_emit(emitter
, &event
))
286 for (i
= 0; i
< n_type
; ++i
)
287 if (emit_type(emitter
, types
[i
]) < 0)
290 if (!yaml_sequence_end_event_initialize(&event
))
292 if (!yaml_emitter_emit(emitter
, &event
))
298 static int emit_array(yaml_emitter_t
*emitter
, struct pet_array
*array
)
302 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
303 YAML_BLOCK_MAPPING_STYLE
))
305 if (!yaml_emitter_emit(emitter
, &event
))
308 if (emit_string(emitter
, "context") < 0)
310 if (emit_set(emitter
, array
->context
) < 0)
313 if (emit_string(emitter
, "extent") < 0)
315 if (emit_set(emitter
, array
->extent
) < 0)
318 if (array
->value_bounds
) {
319 if (emit_string(emitter
, "value_bounds") < 0)
321 if (emit_set(emitter
, array
->value_bounds
) < 0)
325 if (emit_string(emitter
, "element_type") < 0)
327 if (emit_string(emitter
, array
->element_type
) < 0)
329 if (emit_named_int(emitter
, "element_size", array
->element_size
) < 0)
332 if (array
->element_is_record
)
333 if (emit_named_int(emitter
, "element_is_record",
334 array
->element_is_record
) < 0)
337 if (array
->live_out
) {
338 if (emit_string(emitter
, "live_out") < 0)
340 if (emit_string(emitter
, "1") < 0)
344 if (array
->uniquely_defined
) {
345 if (emit_string(emitter
, "uniquely_defined") < 0)
347 if (emit_string(emitter
, "1") < 0)
351 if (array
->declared
&& emit_named_int(emitter
, "declared", 1) < 0)
353 if (array
->exposed
&& emit_named_int(emitter
, "exposed", 1) < 0)
356 if (!yaml_mapping_end_event_initialize(&event
))
358 if (!yaml_emitter_emit(emitter
, &event
))
364 static int emit_arrays(yaml_emitter_t
*emitter
, int n_array
,
365 struct pet_array
**arrays
)
370 if (emit_string(emitter
, "arrays") < 0)
372 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
373 YAML_BLOCK_SEQUENCE_STYLE
))
375 if (!yaml_emitter_emit(emitter
, &event
))
378 for (i
= 0; i
< n_array
; ++i
)
379 if (emit_array(emitter
, arrays
[i
]) < 0)
382 if (!yaml_sequence_end_event_initialize(&event
))
384 if (!yaml_emitter_emit(emitter
, &event
))
390 static int emit_expr_type(yaml_emitter_t
*emitter
, enum pet_expr_type type
)
392 if (emit_string(emitter
, pet_type_str(type
)) < 0)
397 static int emit_expr(yaml_emitter_t
*emitter
, __isl_keep pet_expr
*expr
)
401 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
402 YAML_BLOCK_MAPPING_STYLE
))
404 if (!yaml_emitter_emit(emitter
, &event
))
407 if (emit_string(emitter
, "type") < 0)
409 if (emit_expr_type(emitter
, expr
->type
) < 0)
412 switch (expr
->type
) {
416 if (emit_named_val(emitter
, "value", expr
->i
) < 0)
419 case pet_expr_double
:
420 if (emit_string(emitter
, "value") < 0)
422 if (emit_double(emitter
, expr
->d
.val
) < 0)
424 if (emit_string(emitter
, "string") < 0)
426 if (emit_string(emitter
, expr
->d
.s
) < 0)
429 case pet_expr_access
:
430 if (emit_string(emitter
, "relation") < 0)
432 if (emit_map(emitter
, expr
->acc
.access
) < 0)
434 if (emit_named_multi_pw_aff(emitter
,
435 "index", expr
->acc
.index
) < 0)
437 if (expr
->acc
.ref_id
&&
438 emit_named_id(emitter
, "reference", expr
->acc
.ref_id
) < 0)
440 if (emit_string(emitter
, "read") < 0)
442 if (emit_int(emitter
, expr
->acc
.read
) < 0)
444 if (emit_string(emitter
, "write") < 0)
446 if (emit_int(emitter
, expr
->acc
.write
) < 0)
450 if (emit_string(emitter
, "operation") < 0)
452 if (emit_string(emitter
, pet_op_str(expr
->op
)) < 0)
456 if (emit_string(emitter
, "name") < 0)
458 if (emit_string(emitter
, expr
->name
) < 0)
462 if (emit_string(emitter
, "type_name") < 0)
464 if (emit_string(emitter
, expr
->type_name
) < 0)
469 if (expr
->n_arg
> 0) {
472 if (emit_string(emitter
, "arguments") < 0)
474 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
475 YAML_BLOCK_SEQUENCE_STYLE
))
477 if (!yaml_emitter_emit(emitter
, &event
))
480 for (i
= 0; i
< expr
->n_arg
; ++i
)
481 if (emit_expr(emitter
, expr
->args
[i
]) < 0)
484 if (!yaml_sequence_end_event_initialize(&event
))
486 if (!yaml_emitter_emit(emitter
, &event
))
490 if (!yaml_mapping_end_event_initialize(&event
))
492 if (!yaml_emitter_emit(emitter
, &event
))
498 static int emit_stmt(yaml_emitter_t
*emitter
, struct pet_stmt
*stmt
)
502 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
503 YAML_BLOCK_MAPPING_STYLE
))
505 if (!yaml_emitter_emit(emitter
, &event
))
508 if (emit_string(emitter
, "line") < 0)
510 if (emit_int(emitter
, pet_loc_get_line(stmt
->loc
)) < 0)
513 if (emit_string(emitter
, "domain") < 0)
515 if (emit_set(emitter
, stmt
->domain
) < 0)
518 if (emit_string(emitter
, "schedule") < 0)
520 if (emit_map(emitter
, stmt
->schedule
) < 0)
523 if (emit_string(emitter
, "body") < 0)
525 if (emit_expr(emitter
, stmt
->body
) < 0)
528 if (stmt
->n_arg
> 0) {
531 if (emit_string(emitter
, "arguments") < 0)
533 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
534 YAML_BLOCK_SEQUENCE_STYLE
))
536 if (!yaml_emitter_emit(emitter
, &event
))
539 for (i
= 0; i
< stmt
->n_arg
; ++i
)
540 if (emit_expr(emitter
, stmt
->args
[i
]) < 0)
543 if (!yaml_sequence_end_event_initialize(&event
))
545 if (!yaml_emitter_emit(emitter
, &event
))
549 if (!yaml_mapping_end_event_initialize(&event
))
551 if (!yaml_emitter_emit(emitter
, &event
))
557 static int emit_statements(yaml_emitter_t
*emitter
, int n_stmt
,
558 struct pet_stmt
**stmts
)
563 if (emit_string(emitter
, "statements") < 0)
565 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
566 YAML_BLOCK_SEQUENCE_STYLE
))
568 if (!yaml_emitter_emit(emitter
, &event
))
571 for (i
= 0; i
< n_stmt
; ++i
)
572 if (emit_stmt(emitter
, stmts
[i
]) < 0)
575 if (!yaml_sequence_end_event_initialize(&event
))
577 if (!yaml_emitter_emit(emitter
, &event
))
583 /* Print "implication" to "emitter".
585 static int emit_implication(yaml_emitter_t
*emitter
,
586 struct pet_implication
*implication
)
590 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
591 YAML_BLOCK_MAPPING_STYLE
))
593 if (!yaml_emitter_emit(emitter
, &event
))
596 if (emit_named_int(emitter
, "satisfied", implication
->satisfied
) < 0)
599 if (emit_named_map(emitter
, "extension", implication
->extension
) < 0)
602 if (!yaml_mapping_end_event_initialize(&event
))
604 if (!yaml_emitter_emit(emitter
, &event
))
610 /* Print the list of "n_implication" "implications", if any, to "emitter".
612 static int emit_implications(yaml_emitter_t
*emitter
, int n_implication
,
613 struct pet_implication
**implications
)
618 if (n_implication
== 0)
621 if (emit_string(emitter
, "implications") < 0)
623 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
624 YAML_BLOCK_SEQUENCE_STYLE
))
626 if (!yaml_emitter_emit(emitter
, &event
))
629 for (i
= 0; i
< n_implication
; ++i
)
630 if (emit_implication(emitter
, implications
[i
]) < 0)
633 if (!yaml_sequence_end_event_initialize(&event
))
635 if (!yaml_emitter_emit(emitter
, &event
))
641 static int emit_scop(yaml_emitter_t
*emitter
, struct pet_scop
*scop
)
645 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
646 YAML_BLOCK_MAPPING_STYLE
))
648 if (!yaml_emitter_emit(emitter
, &event
))
651 if (emit_named_unsigned(emitter
,
652 "start", pet_loc_get_start(scop
->loc
)) < 0)
654 if (emit_named_unsigned(emitter
, "end", pet_loc_get_end(scop
->loc
)) < 0)
656 if (emit_string(emitter
, "context") < 0)
658 if (emit_set(emitter
, scop
->context
) < 0)
660 if (!isl_set_plain_is_universe(scop
->context_value
) &&
661 emit_named_set(emitter
, "context_value", scop
->context_value
) < 0)
664 if (emit_types(emitter
, scop
->n_type
, scop
->types
) < 0)
666 if (emit_arrays(emitter
, scop
->n_array
, scop
->arrays
) < 0)
669 if (emit_statements(emitter
, scop
->n_stmt
, scop
->stmts
) < 0)
672 if (emit_implications(emitter
, scop
->n_implication
,
673 scop
->implications
) < 0)
676 if (!yaml_mapping_end_event_initialize(&event
))
678 if (!yaml_emitter_emit(emitter
, &event
))
684 /* Print a YAML serialization of "scop" to "out".
686 int pet_scop_emit(FILE *out
, struct pet_scop
*scop
)
688 yaml_emitter_t emitter
;
691 yaml_emitter_initialize(&emitter
);
693 yaml_emitter_set_output_file(&emitter
, out
);
695 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
696 if (!yaml_emitter_emit(&emitter
, &event
))
699 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
701 if (!yaml_emitter_emit(&emitter
, &event
))
704 if (emit_scop(&emitter
, scop
) < 0)
707 if (!yaml_document_end_event_initialize(&event
, 1))
709 if (!yaml_emitter_emit(&emitter
, &event
))
712 yaml_stream_end_event_initialize(&event
);
713 if (!yaml_emitter_emit(&emitter
, &event
))
716 yaml_emitter_delete(&emitter
);
719 yaml_emitter_delete(&emitter
);