1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# build the parse tree for a struct file
function find_structure(name,
LOCAL, i)
{
for (i=0;i<num_structs;i++) {
if (structs[i, "name"] == name) return i;
}
return "-1";
}
function start_module(name)
{
module=name;
num_structs=0;
num_elements=0;
num_unions=0;
num_tests=0;
}
function parse_define(def1, def2,
LOCAL, type, i)
{
defines[def1]=def2;
}
function start_struct(name)
{
current_struct=num_structs;
structs[name]=current_struct;
structs[current_struct, "name"]=name;
structs[current_struct, "num_elems"]=0;
structs[current_struct, "num_unions"]=0;
}
function end_struct(name)
{
if (name!="") structs[num_structs, "name"]=name;
printf("struct %s with %d elements\n",
structs[num_structs, "name"],
structs[num_structs, "num_elems"]);
num_structs++;
current_struct="";
}
function add_element(type, elem, case,
LOCAL, elem_num, i, v)
{
while (defines[type]!="") {
type=defines[type];
}
elem_num=num_elements;
if (substr(elem, 1, 1) == ".") {
elem=substr(elem, 2);
elements[elem_num, "nowire"]=1;
}
if (substr(elem, 1, 1) == "*") {
elem=substr(elem, 2);
elements[elem_num, "ptr"]=1;
}
i=match(elem,"[[]");
if (i != 0) {
v = substr(elem, i+1, length(elem)-i-1);
elem=substr(elem, 1, i-1);
if (type=="union") {
elements[elem_num, "switch"] = v;
} else {
elements[elem_num, "array_len"] = v;
}
}
elements[elem_num, "type"] = type;
elements[elem_num, "elem"] = elem;
elements[elem_num, "case"] = case;
num_elements++;
return elem_num;
}
function add_struct_elem(type, elem, case,
LOCAL, elem_num)
{
elem_num=structs[current_struct, "num_elems"];
structs[current_struct, elem_num] = add_element(type, elem, case);
structs[current_struct, "num_elems"]++;
return structs[current_struct, elem_num];
}
function start_union(elem)
{
current_union = add_struct_elem("union", elem);
unions[current_union, "num_elems"] = 0;
}
function start_union_notencap(switch)
{
add_struct_elem("uint32", "switch_"switch);
start_union("UNKNOWN[switch_"switch"]");
}
function start_union_encap(struct, type, switch, union)
{
start_struct(struct);
add_struct_elem(type, switch);
add_struct_elem(type, "switch_"switch);
start_union(union"[switch_"switch"]");
encap_union="1";
}
function parse_case(case, type, elem,
LOCAL, elem_num)
{
split(case, a, "[:]");
case = a[1];
elem_num = unions[current_union, "num_elems"];
unions[current_union, elem_num] = add_element(type, elem, case);
unions[current_union, "num_elems"]++;
}
function end_union(name)
{
if (name!="") {
elements[current_union, "elem"] = name;
}
current_union="";
if (encap_union=="1") {
end_struct(name);
encap_union="0";
}
}
function delete_element(struct, elnum,
LOCAL, i)
{
for (i=elnum;i<structs[struct,"num_elems"]-1;i++) {
structs[struct, i] = structs[struct, i+1];
}
structs[struct, "num_elems"]--;
}
function copy_struct(from, to,
LOCAL, i)
{
for (i=0;i<structs[from,"num_elems"];i++) {
structs[to, i] = structs[from, i];
}
structs[to, "name"] = structs[from, "name"];
structs[to, "num_elems"] = structs[from, "num_elems"];
structs[to, "num_unions"] = structs[from, "num_unions"];
}
function add_sizeis_array(count, type, elem)
{
copy_struct(current_struct, current_struct+1);
elem=substr(elem,2);
start_struct("array_"current_struct"_"elem);
add_struct_elem("uint32", count);
add_struct_elem(type, elem"["count"]");
end_struct("");
current_struct=num_structs;
add_struct_elem("array_"current_struct-1"_"elem, "*"elem"_ptr");
}
function start_function(type, fname)
{
start_struct(fname);
add_function_param("[in,out]",".trailer", "");
}
function end_function(LOCAL, i)
{
copy_struct(num_structs, num_structs+1);
structs[num_structs, "name"] = "Q_"structs[num_structs, "name"];
for (i=0;i<structs[num_structs, "num_elems"];i++) {
if (match(elements[structs[num_structs, i], "properties"], "in") == 0) {
delete_element(num_structs, i);
i--;
}
}
end_struct();
current_struct=num_structs;
structs[num_structs, "name"] = "R_"structs[num_structs, "name"];
for (i=0;i<structs[num_structs, "num_elems"];i++) {
if (match(elements[structs[num_structs, i], "properties"], "out") == 0) {
delete_element(num_structs, i);
i--;
}
}
if (return_result!="void")
add_function_param("[out]", return_result, "status");
end_struct();
}
function add_function_param(properties, type, elem,
LOCAL, elnum, len)
{
len=length(type);
if (substr(type, len) == "*") {
type=substr(type, 1, len-1);
elem="*"elem;
}
if (substr(elem,1,1) == "*" &&
(match(properties,"in") == 0 ||
find_structure(type) != "-1")) {
elem=substr(elem, 2);
}
elnum = add_struct_elem(type, elem);
elements[elnum, "properties"] = properties;
}
|