forked from repoocaj/kme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk_kme_defs
executable file
·202 lines (181 loc) · 3.93 KB
/
mk_kme_defs
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
#!/usr/bin/perl
require 'assert.pl';
#
# @(#) $Id
# NOTE: This table must be reconsidered when using a different
# architecture.
#
%sizeof = (
'char', '1',
'unsigned char', '1',
'short', '2',
'short int', '2',
'unsigned short', '2',
'unsigned short int', '2',
'short unsigned int', '2',
'int', '4',
'unsigned int', '4',
'long', '4',
'long int', '4',
'unsigned long', '4',
'unsigned long int', '4',
'long unsigned int', '4',
'long long', '8',
'long long int', '8',
'unsigned long long', '8',
'unsigned long long int', '8',
'float', '4',
'double', '8',
'pointer', '4',
);
#
# This table is where the default for each type is set.
# a program will never get them all right. (you and I know
# int c = getc(), c should be displayed as a character, but
# int c = 0x123, c should be as a decimal/hex number. Still, we
# allow this customizable table....
#
%kme_fmt = (
'char', 'b',
'unsigned char', 'b',
'short', 'x',
'short int', 'x',
'unsigned short', 'x',
'unsigned short int', 'x',
'short unsigned int', 'x',
'int', 'l',
'unsigned int', 'l',
'long', 'l',
'long int', 'l',
'unsigned long', 'l',
'unsigned long int', 'l',
'long unsigned int', 'l',
'long long', '2l',
'long long int', '2l',
'unsigned long long', '2l',
'unsigned long long int', '2l',
'float', '???',
'double', '???',
'pointer', 'l',
);
$size = 0 ;
$offset = 0 ;
$label = "" ;
$fmt = "" ;
$output_column = 0 ;
while (<>)
{
if (($strname) = /^struct ($\.+) {/)
{
print "\n!$strname ";
&read_struct ;
}
print "\n" ;
}
sub read_struct
{
while(<>)
{
if (/}/)
{
&align_data;
&retire_writes;
last ;
}
($type, $member, $offset, $rsize) = unpack("A46A36A9A8", $_) ;
# Nuke leading whitespace from $type
$type =~ s/^(\s*)// ;
#
# We'll politely forget x86 (where x < 3) where pointers
# can be different sizes...
# Pick out "struct foo *"
# and "(*int)()" (function pointers)
#
if (($type =~ /\*/) || ($type =~ /\(\*/) )
{
$type = 'pointer' ;
}
#
# ignore recursive struct definitions..pstruct will deal
# with these when it gets 'round to it.
# Be sure this is done after 'pointer' test, so we
# allow struct foo *
#
if ($type =~ /struct /)
{
next ;
}
$size = $sizeof{$type} ;
# print "Type: ->$type<- Member: $member, O: $offset , S: $size R: $rsize \n";
@member = split(/\./, $member);
# Emit the labels...
$label = $label . "\"$member[$#member]\" " ;
& emit_primitive ;
}
}
sub retire_writes
{
$output_column = 0 ;
$real_offset = 0 ;
print "\t", $label, "n\n\t", $fmt, "n\n";
$label = "" ;
$fmt = "";
}
sub emit_primitive
{
# Really dumb heurstics here...
# Figure out if repeats (i.e. char foo[10] ; )
&assert ('$size >= 0');
if ($size == 0)
{
$repeat = 1 ;
}
else
{
$repeat = $rsize / $size ;
}
&assert ('$repeat == int($repeat)');
$output_column += $repeat * $size;
if ($repeat == 1)
{
$repeat = ""
}
&align_data ;
$fmt .= $repeat . $kme_fmt{$type} ;
if (($output_column > 16) || ($nitems++ > 7))
{
# $retire_writes ;
$nitems = 0 ;
$output_column = 0 ;
print "\t", $label, "n\n\t", $fmt, "n\n";
$label = "" ;
$fmt = "";
}
}
sub emit_pad
{
$fmt .= "+" ;
}
sub align_data
{
$num_pads = $offset - $real_offset ;
# print "\n Real offset: $real_offset, $offset, Emitting $num_pads ";
#
# While non-obvious, this makes the end of structure padding
# work right....
# struct foo { int x ; char a ; } , this code pads foo to 8..
#
if ($num_pads < 0 )
{
$num_pads = (4 - $real_offset) & 3 ;
# print "Padding: $num_pads $offset $real_offset \n";
}
while ($num_pads > 0 )
{
&emit_pad;
$num_pads--;
$real_offset ++ ;
}
$real_offset += $rsize ;
return ;
}