2 * Copyright 2011 Leiden University. All rights reserved.
7 static int init(yaml_emitter_t
*emitter
, FILE *out
)
11 yaml_emitter_initialize(emitter
);
13 yaml_emitter_set_output_file(emitter
, out
);
15 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
16 if (!yaml_emitter_emit(emitter
, &event
))
19 if (!yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 1))
21 if (!yaml_emitter_emit(emitter
, &event
))
26 static int fini(yaml_emitter_t
*emitter
)
30 if (!yaml_document_end_event_initialize(&event
, 1))
32 if (!yaml_emitter_emit(emitter
, &event
))
35 yaml_stream_end_event_initialize(&event
);
36 if (!yaml_emitter_emit(emitter
, &event
))
39 yaml_emitter_delete(emitter
);
42 yaml_emitter_delete(emitter
);
46 static int emit_string(yaml_emitter_t
*emitter
, const char *str
)
50 if (!yaml_scalar_event_initialize(&event
, NULL
, NULL
,
51 (yaml_char_t
*) str
, strlen(str
),
52 1, 1, YAML_PLAIN_SCALAR_STYLE
))
54 if (!yaml_emitter_emit(emitter
, &event
))
60 static int emit_int(yaml_emitter_t
*emitter
, int i
)
64 snprintf(buffer
, sizeof(buffer
), "%d", i
);
65 return emit_string(emitter
, buffer
);
68 static int emit_named_int(yaml_emitter_t
*emitter
, const char *name
, int i
)
70 if (emit_string(emitter
, name
) < 0)
72 if (emit_int(emitter
, i
) < 0)
77 static int emit_val(yaml_emitter_t
*emitter
, __isl_keep isl_val
*v
)
79 isl_ctx
*ctx
= isl_val_get_ctx(v
);
84 p
= isl_printer_to_str(ctx
);
85 p
= isl_printer_print_val(p
, v
);
86 str
= isl_printer_get_str(p
);
88 r
= emit_string(emitter
, str
);
93 static int emit_named_val(yaml_emitter_t
*emitter
, const char *name
,
94 __isl_keep isl_val
*v
)
96 if (emit_string(emitter
, name
) < 0)
98 if (emit_val(emitter
, v
) < 0)
103 static int emit_named_id(yaml_emitter_t
*emitter
, const char *name
,
104 __isl_keep isl_id
*id
)
106 if (emit_string(emitter
, name
) < 0)
108 if (emit_string(emitter
, isl_id_get_name(id
)) < 0)
113 static int emit_set(yaml_emitter_t
*emitter
, __isl_keep isl_set
*set
)
115 isl_ctx
*ctx
= isl_set_get_ctx(set
);
120 p
= isl_printer_to_str(ctx
);
121 p
= isl_printer_print_set(p
, set
);
122 str
= isl_printer_get_str(p
);
124 r
= emit_string(emitter
, str
);
129 static int emit_map(yaml_emitter_t
*emitter
, __isl_keep isl_map
*map
)
131 isl_ctx
*ctx
= isl_map_get_ctx(map
);
136 p
= isl_printer_to_str(ctx
);
137 p
= isl_printer_print_map(p
, map
);
138 str
= isl_printer_get_str(p
);
140 r
= emit_string(emitter
, str
);
145 static int emit_named_set(yaml_emitter_t
*emitter
, const char *name
,
146 __isl_keep isl_set
*set
)
148 if (emit_string(emitter
, name
) < 0)
150 if (emit_set(emitter
, set
) < 0)
155 static int emit_named_map(yaml_emitter_t
*emitter
, const char *name
,
156 __isl_keep isl_map
*map
)
158 if (emit_string(emitter
, name
) < 0)
160 if (emit_map(emitter
, map
) < 0)
165 static int emit_aff(yaml_emitter_t
*emitter
, __isl_keep isl_aff
*aff
)
167 isl_ctx
*ctx
= isl_aff_get_ctx(aff
);
172 p
= isl_printer_to_str(ctx
);
173 p
= isl_printer_print_aff(p
, aff
);
174 str
= isl_printer_get_str(p
);
176 r
= emit_string(emitter
, str
);
181 static int emit_named_aff(yaml_emitter_t
*emitter
, const char *name
,
182 __isl_keep isl_aff
*aff
)
184 if (emit_string(emitter
, name
) < 0)
186 if (emit_aff(emitter
, aff
) < 0)
191 static int emit_pw_aff(yaml_emitter_t
*emitter
, __isl_keep isl_pw_aff
*pa
)
193 isl_ctx
*ctx
= isl_pw_aff_get_ctx(pa
);
198 p
= isl_printer_to_str(ctx
);
199 p
= isl_printer_print_pw_aff(p
, pa
);
200 str
= isl_printer_get_str(p
);
202 r
= emit_string(emitter
, str
);
207 static int emit_named_pw_aff(yaml_emitter_t
*emitter
, const char *name
,
208 __isl_keep isl_pw_aff
*pwaff
)
210 if (emit_string(emitter
, name
) < 0)
212 if (emit_pw_aff(emitter
, pwaff
) < 0)
217 static int emit_multi_aff(yaml_emitter_t
*emitter
,
218 __isl_keep isl_multi_aff
*maff
)
220 isl_ctx
*ctx
= isl_multi_aff_get_ctx(maff
);
225 p
= isl_printer_to_str(ctx
);
226 p
= isl_printer_print_multi_aff(p
, maff
);
227 str
= isl_printer_get_str(p
);
229 r
= emit_string(emitter
, str
);
234 static int emit_named_multi_aff(yaml_emitter_t
*emitter
, const char *name
,
235 __isl_keep isl_multi_aff
*maff
)
237 if (emit_string(emitter
, name
) < 0)
239 if (emit_multi_aff(emitter
, maff
) < 0)
244 static int emit_pw_multi_aff(yaml_emitter_t
*emitter
,
245 __isl_keep isl_pw_multi_aff
*maff
)
247 isl_ctx
*ctx
= isl_pw_multi_aff_get_ctx(maff
);
252 p
= isl_printer_to_str(ctx
);
253 p
= isl_printer_print_pw_multi_aff(p
, maff
);
254 str
= isl_printer_get_str(p
);
256 r
= emit_string(emitter
, str
);
261 static int emit_named_pw_multi_aff(yaml_emitter_t
*emitter
, const char *name
,
262 __isl_keep isl_pw_multi_aff
*maff
)
264 if (emit_string(emitter
, name
) < 0)
266 if (emit_pw_multi_aff(emitter
, maff
) < 0)
271 int adg_var::print(yaml_emitter_t
*emitter
)
275 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
276 YAML_FLOW_MAPPING_STYLE
))
278 if (!yaml_emitter_emit(emitter
, &event
))
281 if (access
&& emit_named_pw_multi_aff(emitter
, "access", access
))
283 if (type
&& emit_named_id(emitter
, "type", type
))
286 if (!yaml_mapping_end_event_initialize(&event
))
288 if (!yaml_emitter_emit(emitter
, &event
))
294 static int emit_var_list(yaml_emitter_t
*emitter
,
295 const char *name
, std::vector
<adg_var
*> &list
)
299 if (list
.size() == 0)
302 if (emit_string(emitter
, name
) < 0)
305 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
306 YAML_BLOCK_SEQUENCE_STYLE
))
308 if (!yaml_emitter_emit(emitter
, &event
))
311 for (int i
= 0; i
< list
.size(); ++i
)
312 if (list
[i
]->print(emitter
) < 0)
315 if (!yaml_sequence_end_event_initialize(&event
))
317 if (!yaml_emitter_emit(emitter
, &event
))
323 static int emit_arg_list(yaml_emitter_t
*emitter
,
324 const char *name
, std::vector
<adg_arg
*> &list
)
328 if (list
.size() == 0)
331 if (emit_string(emitter
, name
) < 0)
334 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
335 YAML_BLOCK_SEQUENCE_STYLE
))
337 if (!yaml_emitter_emit(emitter
, &event
))
340 for (int i
= 0; i
< list
.size(); ++i
)
341 if (list
[i
]->print(emitter
) < 0)
344 if (!yaml_sequence_end_event_initialize(&event
))
346 if (!yaml_emitter_emit(emitter
, &event
))
352 static int emit_named_var(yaml_emitter_t
*emitter
, const char *name
,
355 if (emit_string(emitter
, name
) < 0)
357 if (var
->print(emitter
) < 0)
362 static int emit_arg_type(yaml_emitter_t
*emitter
, enum adg_arg_type type
)
370 case adg_arg_reference
:
371 s_type
= "reference";
376 case adg_arg_unknown
:
383 if (emit_string(emitter
, "type") < 0)
385 if (emit_string(emitter
, s_type
) < 0)
391 int adg_arg::print(yaml_emitter_t
*emitter
)
395 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
396 YAML_FLOW_MAPPING_STYLE
))
398 if (!yaml_emitter_emit(emitter
, &event
))
401 if (var
&& emit_named_var(emitter
, "var", var
))
403 if (emit_arg_type(emitter
, type
))
406 if (!yaml_mapping_end_event_initialize(&event
))
408 if (!yaml_emitter_emit(emitter
, &event
))
414 int adg_expr::print(yaml_emitter_t
*emitter
)
418 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
419 YAML_FLOW_MAPPING_STYLE
))
421 if (!yaml_emitter_emit(emitter
, &event
))
424 if (name
&& emit_named_id(emitter
, "name", name
))
426 if (expr
&& emit_named_pw_aff(emitter
, "expr", expr
))
429 if (!yaml_mapping_end_event_initialize(&event
))
431 if (!yaml_emitter_emit(emitter
, &event
))
437 static int emit_expr_list(yaml_emitter_t
*emitter
,
438 const char *name
, std::vector
<adg_expr
*> &list
)
442 if (list
.size() == 0)
445 if (emit_string(emitter
, name
) < 0)
448 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
449 YAML_BLOCK_SEQUENCE_STYLE
))
451 if (!yaml_emitter_emit(emitter
, &event
))
454 for (int i
= 0; i
< list
.size(); ++i
)
455 if (list
[i
]->print(emitter
) < 0)
458 if (!yaml_sequence_end_event_initialize(&event
))
460 if (!yaml_emitter_emit(emitter
, &event
))
466 int adg_domain::print(yaml_emitter_t
*emitter
)
470 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
471 YAML_ANY_MAPPING_STYLE
))
473 if (!yaml_emitter_emit(emitter
, &event
))
476 if (bounds
&& emit_named_set(emitter
, "bounds", bounds
))
478 if (emit_expr_list(emitter
, "controls", controls
))
480 if (emit_var_list(emitter
, "filters", filters
))
483 if (!yaml_mapping_end_event_initialize(&event
))
485 if (!yaml_emitter_emit(emitter
, &event
))
491 static int emit_named_domain(yaml_emitter_t
*emitter
, const char *name
,
494 if (emit_string(emitter
, name
) < 0)
496 if (domain
->print(emitter
) < 0)
501 static int emit_id_list(yaml_emitter_t
*emitter
,
502 const char *name
, std::vector
<isl_id
*> &list
)
506 if (list
.size() == 0)
509 if (emit_string(emitter
, name
) < 0)
512 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
513 YAML_FLOW_SEQUENCE_STYLE
))
515 if (!yaml_emitter_emit(emitter
, &event
))
518 for (int i
= 0; i
< list
.size(); ++i
)
519 if (emit_string(emitter
, isl_id_get_name(list
[i
])) < 0)
522 if (!yaml_sequence_end_event_initialize(&event
))
524 if (!yaml_emitter_emit(emitter
, &event
))
530 int adg_port::print(yaml_emitter_t
*emitter
)
534 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
535 YAML_BLOCK_MAPPING_STYLE
))
537 if (!yaml_emitter_emit(emitter
, &event
))
540 if (name
&& emit_named_id(emitter
, "name", name
))
542 if (node_name
&& emit_named_id(emitter
, "node", node_name
))
544 if (edge_name
&& emit_named_id(emitter
, "edge", edge_name
))
546 if (emit_var_list(emitter
, "vars", vars
))
548 if (domain
&& emit_named_domain(emitter
, "domain", domain
))
551 if (!yaml_mapping_end_event_initialize(&event
))
553 if (!yaml_emitter_emit(emitter
, &event
))
559 int adg_gate::print(yaml_emitter_t
*emitter
)
563 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
564 YAML_BLOCK_MAPPING_STYLE
))
566 if (!yaml_emitter_emit(emitter
, &event
))
569 if (name
&& emit_named_id(emitter
, "name", name
))
571 if (node_name
&& emit_named_id(emitter
, "node", node_name
))
573 if (in
&& emit_named_var(emitter
, "in", in
))
575 if (out
&& emit_named_var(emitter
, "out", out
))
577 if (domain
&& emit_named_domain(emitter
, "domain", domain
))
580 if (!yaml_mapping_end_event_initialize(&event
))
582 if (!yaml_emitter_emit(emitter
, &event
))
588 int adg_function::print(yaml_emitter_t
*emitter
)
592 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
593 YAML_BLOCK_MAPPING_STYLE
))
595 if (!yaml_emitter_emit(emitter
, &event
))
598 if (name
&& emit_named_id(emitter
, "name", name
))
601 if (emit_arg_list(emitter
, "in", in
))
603 if (emit_arg_list(emitter
, "out", out
))
605 if (emit_expr_list(emitter
, "ctrl", ctrl
))
608 if (domain
&& emit_named_domain(emitter
, "domain", domain
))
611 if (!yaml_mapping_end_event_initialize(&event
))
613 if (!yaml_emitter_emit(emitter
, &event
))
619 static int emit_port_list(yaml_emitter_t
*emitter
,
620 const char *name
, std::vector
<adg_port
*> &list
)
624 if (list
.size() == 0)
627 if (emit_string(emitter
, name
) < 0)
630 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
631 YAML_BLOCK_SEQUENCE_STYLE
))
633 if (!yaml_emitter_emit(emitter
, &event
))
636 for (int i
= 0; i
< list
.size(); ++i
)
637 if (list
[i
]->print(emitter
) < 0)
640 if (!yaml_sequence_end_event_initialize(&event
))
642 if (!yaml_emitter_emit(emitter
, &event
))
648 int adg_node::print(yaml_emitter_t
*emitter
)
652 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
653 YAML_BLOCK_MAPPING_STYLE
))
655 if (!yaml_emitter_emit(emitter
, &event
))
658 if (name
&& emit_named_id(emitter
, "name", name
))
662 if (emit_string(emitter
, "function") < 0)
664 function
->print(emitter
);
667 if (domain
&& emit_named_domain(emitter
, "domain", domain
))
669 if (schedule
&& emit_named_map(emitter
, "schedule", schedule
))
672 if (emit_port_list(emitter
, "input_ports", input_ports
))
674 if (emit_port_list(emitter
, "output_ports", output_ports
))
676 if (emit_expr_list(emitter
, "expressions", expressions
))
679 if (gates
.size() > 0) {
680 if (emit_string(emitter
, "gates") < 0)
682 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
683 YAML_BLOCK_SEQUENCE_STYLE
))
685 if (!yaml_emitter_emit(emitter
, &event
))
687 for (int i
= 0; i
< gates
.size(); ++i
)
688 if (gates
[i
]->print(emitter
))
690 if (!yaml_sequence_end_event_initialize(&event
))
692 if (!yaml_emitter_emit(emitter
, &event
))
696 if (!yaml_mapping_end_event_initialize(&event
))
698 if (!yaml_emitter_emit(emitter
, &event
))
704 static int emit_type(yaml_emitter_t
*emitter
, enum adg_edge_type type
)
712 case adg_edge_sticky_fifo
:
713 s_type
= "sticky fifo";
715 case adg_edge_shift_register
:
716 s_type
= "shift register";
718 case adg_edge_broadcast
:
719 s_type
= "broadcast";
721 case adg_edge_generic
:
728 if (emit_string(emitter
, "type") < 0)
730 if (emit_string(emitter
, s_type
) < 0)
736 int adg_edge::print(yaml_emitter_t
*emitter
)
740 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
741 YAML_BLOCK_MAPPING_STYLE
))
743 if (!yaml_emitter_emit(emitter
, &event
))
746 if (name
&& emit_named_id(emitter
, "name", name
))
748 if (emit_type(emitter
, type
))
750 if (map
&& emit_named_multi_aff(emitter
, "map", map
))
752 if (from_node_name
&&
753 emit_named_id(emitter
, "from_node", from_node_name
))
755 if (from_port_name
&&
756 emit_named_id(emitter
, "from_port", from_port_name
))
758 if (to_node_name
&& emit_named_id(emitter
, "to_node", to_node_name
))
760 if (to_port_name
&& emit_named_id(emitter
, "to_port", to_port_name
))
763 emit_named_val(emitter
, "value_size", value_size
);
765 if (!yaml_mapping_end_event_initialize(&event
))
767 if (!yaml_emitter_emit(emitter
, &event
))
773 int adg_param::print(yaml_emitter_t
*emitter
)
777 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
778 YAML_FLOW_MAPPING_STYLE
))
780 if (!yaml_emitter_emit(emitter
, &event
))
783 if (name
&& emit_named_id(emitter
, "name", name
))
785 if (lb
&& emit_named_aff(emitter
, "lb", lb
))
787 if (ub
&& emit_named_aff(emitter
, "ub", ub
))
789 if (val
&& emit_named_aff(emitter
, "val", val
))
792 if (!yaml_mapping_end_event_initialize(&event
))
794 if (!yaml_emitter_emit(emitter
, &event
))
800 static int emit_param_list(yaml_emitter_t
*emitter
,
801 const char *name
, std::vector
<adg_param
*> &list
)
805 if (list
.size() == 0)
808 if (emit_string(emitter
, name
) < 0)
811 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
812 YAML_ANY_SEQUENCE_STYLE
))
814 if (!yaml_emitter_emit(emitter
, &event
))
817 for (int i
= 0; i
< list
.size(); ++i
)
818 if (list
[i
]->print(emitter
) < 0)
821 if (!yaml_sequence_end_event_initialize(&event
))
823 if (!yaml_emitter_emit(emitter
, &event
))
829 int adg::print(yaml_emitter_t
*emitter
)
833 if (!yaml_mapping_start_event_initialize(&event
, NULL
, NULL
, 1,
834 YAML_BLOCK_MAPPING_STYLE
))
836 if (!yaml_emitter_emit(emitter
, &event
))
839 if (name
&& emit_named_id(emitter
, "name", name
))
841 if (context
&& emit_named_set(emitter
, "context", context
))
843 if (iterator_map
&& emit_named_map(emitter
, "iterator_map", iterator_map
))
845 if (emit_id_list(emitter
, "iterators", iterators
))
847 if (emit_id_list(emitter
, "all_iterators", all_iterators
))
849 if (emit_param_list(emitter
, "params", params
))
852 if (nodes
.size() > 0) {
853 if (emit_string(emitter
, "nodes") < 0)
855 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
856 YAML_BLOCK_SEQUENCE_STYLE
))
858 if (!yaml_emitter_emit(emitter
, &event
))
860 for (int i
= 0; i
< nodes
.size(); ++i
)
861 if (nodes
[i
]->print(emitter
))
863 if (!yaml_sequence_end_event_initialize(&event
))
865 if (!yaml_emitter_emit(emitter
, &event
))
869 if (edges
.size() > 0) {
870 if (emit_string(emitter
, "edges") < 0)
872 if (!yaml_sequence_start_event_initialize(&event
, NULL
, NULL
, 1,
873 YAML_BLOCK_SEQUENCE_STYLE
))
875 if (!yaml_emitter_emit(emitter
, &event
))
877 for (int i
= 0; i
< edges
.size(); ++i
)
878 if (edges
[i
]->print(emitter
))
880 if (!yaml_sequence_end_event_initialize(&event
))
882 if (!yaml_emitter_emit(emitter
, &event
))
886 if (!yaml_mapping_end_event_initialize(&event
))
888 if (!yaml_emitter_emit(emitter
, &event
))
894 /* Print a YAML serialization of this to "out".
896 void adg_var::print(FILE *out
)
898 yaml_emitter_t emitter
;
900 if (init(&emitter
, out
) < 0) {
901 yaml_emitter_delete(&emitter
);
909 /* Print a YAML serialization of this to "out".
911 void adg::print(FILE *out
)
913 yaml_emitter_t emitter
;
915 if (init(&emitter
, out
) < 0) {
916 yaml_emitter_delete(&emitter
);