1 # build parse functions for a parsed struct file
3 function elem_name
(v
, elem
)
8 function parse_array
(f
, v
, elnum
, flags
,
9 LOCAL
, type
, elem
, array_len
)
11 type = elements
[elnum
, "type"];
12 elem = elements
[elnum
, "elem"];
13 array_len = elements
[elnum
, "array_len"];
14 v
["ELEM"] = elem_name
(v
, elem
);
17 v
["ARRAY_LEN"] = array_len
;
20 print_template
(f
,"prs_array_optional.tpl", v
);
25 print_template
(f
,"prs_array_remainder.tpl", v
);
29 if (type ==
"wchar" || type ==
"uint16") {
30 if (match(array_len
,"[0-9]") ==
1) {
31 print_template
(f
, "prs_wstring_fixed.tpl", v
);
33 print_template
(f
, "prs_wstring.tpl", v
);
35 } else if (type ==
"uint8") {
36 if (match(array_len
,"[0-9]") ==
1) {
37 print_template
(f
, "prs_uint8s_fixed.tpl", v
);
39 print_template
(f
, "prs_uint8s.tpl", v
);
42 print_template
(f
, "prs_array.tpl", v
);
47 function parse_element
(f
, v
, elnum
, flags
,
50 if (elements
[elnum
,"nowire"] != "") {
53 type = elements
[elnum
, "type"];
54 if (substr(type
,1,1) ==
".") return;
55 elem = elements
[elnum
, "elem"];
56 if (elements
[elnum
,"ptr"] ==
"") {
61 v
["ELEM"] = elem_name
(v
, elem
);
64 print_template
(f
, "prs_element.tpl", v
);
67 function parse_union
(f
, v
, elnum
, flags
,
70 v
["UNION"] = elements
[elnum
, "elem"];
71 v
["SWITCH"] = elements
[elnum
, "switch"];
73 if (elements
[elnum
, "ptr"] ==
"1") {
74 v
["UNION"] = v
["UNION"]"->";
76 v
["UNION"] = v
["UNION"]".";
79 print_template
(f
, "union_start.tpl", v
);
80 for (i=
0;i
<unions
[elnum
, "num_elems"];i
++) {
81 v
["CASE"] = elements
[unions
[elnum
, i
], "case"];
82 print_template
(f
, "prs_case.tpl", v
);
83 if (elements
[elnum
, "ptr"] ==
"1") {
84 parse_scalars
(f
, v
, unions
[elnum
, i
], "PARSE_SCALARS");
85 parse_buffers
(f
, v
, unions
[elnum
, i
], "PARSE_BUFFERS");
87 if (flags ==
"PARSE_SCALARS") {
88 parse_scalars
(f
, v
, unions
[elnum
, i
], flags
);
90 parse_buffers
(f
, v
, unions
[elnum
, i
], flags
);
93 print_template
(f
, "prs_break.tpl", v
);
97 print_template
(f
, "union_end.tpl", v
);
100 function parse_scalar
(f
, v
, elnum
, flags
)
102 if (elements
[elnum
, "type"] ==
"union") {
103 parse_union
(f
, v
, elnum
, flags
);
104 } else if (elements
[elnum
, "array_len"]!="") {
105 parse_array
(f
, v
, elnum
, flags
);
107 parse_element
(f
, v
, elnum
, flags
);
111 function parse_align2
(f
, v
, elnum
, flags
,
114 elem = elements
[elnum
, "elem"];
115 v
["OFFSET"] = elem_name
(v
, elem
);
116 print_template
(f
, "prs_align2.tpl", v
);
119 function parse_align4
(f
, v
, elnum
, flags
,
122 elem = elements
[elnum
, "elem"];
123 v
["OFFSET"] = elem_name
(v
, elem
);
124 print_template
(f
, "prs_align4.tpl", v
);
127 function parse_pointer
(f
, v
, elnum
, flags
,
130 elem = elements
[elnum
, "elem"];
131 v
["ELEM"] = elem_name
(v
, elem
);
133 print_template
(f
, "prs_pointer.tpl", v
);
136 function parse_scalar_fn
(m
, v
, elnum
,
139 elem = elements
[elnum
, "elem"];
140 type = elements
[elnum
, "type"]
141 xprintf
(m
, "%s %s", type
, elem_name
(v
, elem
));
142 if (type ==
"union") {
143 } else if (elements
[elnum
, "array_len"]!="") {
148 function parse_pointer_fn
(f
, v
, elnum
, flags
,
151 elem = elements
[elnum
, "elem"];
152 v
["ELEM"] = elem_name
(v
, elem
);
154 xprintf
(m
, "%s\n", v
["ELEM"]);
157 function parse_scalars_fn
(m
, v
, elnum
, flags
)
159 if (elements
[elnum
, "type"] ==
".align2") {
161 else if (elements
[elnum
, "type"] ==
".align4") {
163 else if (elements
[elnum
, "ptr"] ==
"1") {
164 parse_pointer_fn
(m
, v
, elnum
, flags
);
166 parse_scalar_fn
(m
, v
, elnum
, flags
);
170 function parse_scalars
(f
, v
, elnum
, flags
)
172 if (elements
[elnum
, "type"] ==
".align2") {
173 parse_align2
(f
, v
, elnum
, flags
);
175 else if (elements
[elnum
, "type"] ==
".align4") {
176 parse_align4
(f
, v
, elnum
, flags
);
178 else if (elements
[elnum
, "ptr"] ==
"1") {
179 parse_pointer
(f
, v
, elnum
, flags
);
181 parse_scalar
(f
, v
, elnum
, flags
);
185 function parse_buffers
(f
, v
, elnum
, flags
,
188 elem = elements
[elnum
, "elem"];
189 type = elements
[elnum
, "type"];
190 v
["ELEM"] = elem_name
(v
, elem
);
191 if (elements
[elnum
, "type"] ==
".align2") {
193 else if (elements
[elnum
, "type"] ==
".align4") {
194 } else if (elements
[elnum
, "ptr"] ==
"1") {
195 print_template
(f
, "ifptr_start.tpl", v
);
196 parse_scalar
(f
, v
, elnum
, "PARSE_SCALARS|PARSE_BUFFERS");
197 print_template
(f
, "ifptr_end.tpl", v
);
199 parse_scalar
(f
, v
, elnum
, flags
);
203 function struct_parser
(f
, m
, v
, struct_num
,
204 LOCAL
, i
, n1
, f1
, num_elems
)
207 num_elems = structs
[struct_num
, "num_elems"];
208 v
["STRUCTNAME"] = structs
[struct_num
, "name"];
209 v
["FUNCNAME"] =
"io_" v
["STRUCTNAME"];
210 print_template
(f
, "fn_start.tpl", v
);
212 for (n1=
0;n1
<num_elems
;n1
++) {
213 if (elements
[structs
[struct_num
, n1
], "type"] ==
".trailer") {
219 # first all the structure pointers, scalars and arrays
221 parse_scalars
(f
, v
, structs
[struct_num
, i
], "PARSE_SCALARS");
224 print_template
(f
, "fn_mid.tpl", v
);
228 parse_buffers
(f
, v
, structs
[struct_num
, i
], "PARSE_BUFFERS");
232 for (i=n1
;i
<num_elems
;i
++) {
233 parse_scalars
(f
, v
, structs
[struct_num
, i
], "PARSE_SCALARS");
234 parse_buffers
(f
, v
, structs
[struct_num
, i
], "PARSE_BUFFERS");
238 print_template
(f
, "fn_end.tpl", v
);
241 print_template
(f
, "fn_end0.tpl", v
);
247 xprintf
(m
, "void fn_%s(\n", v
["STRUCTNAME"]);
249 for (i=f1
+1;i
<num_elems
;i
++) {
250 parse_scalars_fn
(m
, v
, structs
[struct_num
, i
]);
251 if (i
!= num_elems
-1)
256 xprintf
(m
, ")\n{\n}\n");
259 function produce_parsers
(f
, m
,
264 print_template
(f
, "module_start.tpl", v
);
266 for (i=
0;i
< num_structs
;i
++) {
267 struct_parser
(f
, m
, v
, i
);
270 print_template
(f
, "module_end.tpl", v
);