1 # build the parse tree for a struct file
3 function find_structure
(name
,
6 for (i=
0;i
<num_structs
;i
++) {
7 if (structs
[i
, "name"] == name
) return i
;
12 function start_module
(name
)
22 function set_option
(name
, value
)
24 options
[name
] = value
;
25 options
[num_options
, "name"] = name
;
26 options
[num_options
, "value"] = value
;
30 function parse_define
(def1
, def2
,
36 function start_struct
(name
)
38 current_struct=num_structs
;
39 structs
[name
]=current_struct
;
40 structs
[current_struct
, "name"]=name
;
41 structs
[current_struct
, "num_elems"]=
0;
42 structs
[current_struct
, "num_unions"]=
0;
43 structs
[current_struct
, "recurse"] = options
["recurse"];
46 function end_struct
(name
)
48 if (name
!="") structs
[num_structs
, "name"]=name
;
49 printf("struct %s with %d elements\n",
50 structs
[num_structs
, "name"],
51 structs
[num_structs
, "num_elems"]);
56 function add_element
(type
, elem
, case
,
57 LOCAL
, elem_num
, i
, v
)
59 while (defines
[type
]!="") {
62 elem_num=num_elements
;
64 if (substr(elem
, 1, 1) ==
".") {
66 elements
[elem_num
, "nowire"]=
1;
69 if (substr(elem
, 1, 1) ==
"*") {
71 elements
[elem_num
, "ptr"]=
1;
76 v =
substr(elem
, i
+1, length(elem
)-i
-1);
77 elem=
substr(elem
, 1, i
-1);
79 elements
[elem_num
, "switch"] = v
;
81 elements
[elem_num
, "array_len"] = v
;
85 elements
[elem_num
, "type"] = type
;
86 elements
[elem_num
, "elem"] = elem
;
87 elements
[elem_num
, "case"] = case
;
93 function add_struct_elem
(type
, elem
, case
,
96 elem_num=structs
[current_struct
, "num_elems"];
97 structs
[current_struct
, elem_num
] = add_element
(type
, elem
, case
);
98 structs
[current_struct
, "num_elems"]++;
99 return structs
[current_struct
, elem_num
];
102 function start_union
(elem
)
104 current_union = add_struct_elem
("union", elem
);
105 unions
[current_union
, "num_elems"] =
0;
108 function start_union_notencap
(switch
)
110 add_struct_elem
("uint32", "switch_"switch
);
111 start_union
("UNKNOWN[switch_"switch
"]");
114 function start_union_encap
(struct
, type
, switch
, union
)
116 start_struct
(struct
);
117 add_struct_elem
(type
, switch
);
118 add_struct_elem
(type
, "switch_"switch
);
119 start_union
(union
"[switch_"switch
"]");
123 function parse_case
(case
, type
, elem
,
126 split(case
, a
, "[:]");
128 elem_num = unions
[current_union
, "num_elems"];
129 unions
[current_union
, elem_num
] = add_element
(type
, elem
, case
);
130 unions
[current_union
, "num_elems"]++;
133 function end_union
(name
)
136 elements
[current_union
, "elem"] = name
;
139 if (encap_union==
"1") {
145 function delete_element
(struct
, elnum
,
148 for (i=elnum
;i
<structs
[struct
,"num_elems"]-1;i
++) {
149 structs
[struct
, i
] = structs
[struct
, i
+1];
151 structs
[struct
, "num_elems"]--;
154 function copy_struct
(from
, to
,
157 for (i=
0;i
<structs
[from
,"num_elems"];i
++) {
158 structs
[to
, i
] = structs
[from
, i
];
160 structs
[to
, "name"] = structs
[from
, "name"];
161 structs
[to
, "num_elems"] = structs
[from
, "num_elems"];
162 structs
[to
, "num_unions"] = structs
[from
, "num_unions"];
165 function add_sizeis_array
(count
, type
, elem
)
167 copy_struct
(current_struct
, current_struct
+1);
169 start_struct
("array_"current_struct
"_"elem
);
170 add_struct_elem
("uint32", count
);
171 add_struct_elem
(type
, elem
"["count
"]");
173 current_struct=num_structs
;
174 add_struct_elem
("array_"current_struct
-1"_"elem
, "*"elem
"_ptr");
178 function start_function
(type
, fname
)
181 structs
[current_struct
, "recurse"] =
"False";
184 function end_function
(LOCAL
, i
)
186 copy_struct
(num_structs
, num_structs
+1);
187 structs
[num_structs
, "name"] =
"Q_"structs
[num_structs
, "name"];
188 for (i=
0;i
<structs
[num_structs
, "num_elems"];i
++) {
189 if (match(elements
[structs
[num_structs
, i
], "properties"], "in") ==
0) {
190 delete_element
(num_structs
, i
);
195 current_struct=num_structs
;
196 structs
[num_structs
, "name"] =
"R_"structs
[num_structs
, "name"];
197 for (i=
0;i
<structs
[num_structs
, "num_elems"];i
++) {
198 if (match(elements
[structs
[num_structs
, i
], "properties"], "out") ==
0) {
199 delete_element
(num_structs
, i
);
203 if (return_result
!="void")
204 add_function_param
("[out]", return_result
, "status");
208 function add_function_param
(properties
, type
, elem
,
212 if (substr(type
, len
) ==
"*") {
213 type=
substr(type
, 1, len
-1);
216 if (substr(elem
,1,1) ==
"*" &&
217 (match(properties
,"in") ==
0 ||
218 find_structure
(type
) != "-1")) {
219 elem=
substr(elem
, 2);
221 elnum = add_struct_elem
(type
, elem
);
222 elements
[elnum
, "properties"] = properties
;