forked from DFHack/df_misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_sizeofunit.pl
executable file
·316 lines (262 loc) · 9.28 KB
/
get_sizeofunit.pl
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/perl
# input is 1st argument or 'codegen/codegen.out.xml'
# 2nd arg is either 'linux' or 'windows' ; for osx use linux
use strict;
use warnings;
my $input = $ARGV[0] or die 'need path to codegen.out.xml';
my $target_class = 'unit';
my $os = $ARGV[1] or die('os not provided (argv[1])');
if ($os =~ /linux/i or $os =~ /darwin/i) {
$os = 'linux';
} elsif ($os =~ /windows/i) {
$os = 'windows';
} else {
die "Unknown OS: " . $ARGV[1] . "\n";
}
my $arch = $ARGV[2] or die('arch not provided (argv[2])');
if ($arch =~ /64/i) {
$arch = 64;
} elsif ($arch =~ /32/i) {
$arch = 32;
} else {
die "Unknown architecture: " . $ARGV[2] . "\n";
}
# 32 bits on Windows and 32-bit *nix, 64 bits on 64-bit *nix
my $SIZEOF_LONG;
if ($os eq 'windows' || $arch == 32) {
$SIZEOF_LONG = 4;
} else {
$SIZEOF_LONG = 8;
}
my $SIZEOF_PTR = ($arch == 64) ? 8 : 4;
use XML::LibXML;
my %global_types;
our $compound_off;
our $compound_pointer;
sub get_sizeof {
my ($type) = @_;
my $meta = $type->getAttribute('ld:meta');
local $compound_off = 0;
$compound_off = $SIZEOF_PTR if ($meta eq 'class-type');
my $sz = sizeof($type);
# see comment is sub sizeof ; but gcc has sizeof(cls) aligned
$sz = align_field($sz, $SIZEOF_PTR) if $os eq 'linux' and $meta eq 'class-type';
return $sz;
}
my %align_cache;
my %sizeof_cache;
sub align_field {
my ($off, $fldalign) = @_;
my $dt = $off % $fldalign;
$off += $fldalign - $dt if $dt > 0;
return $off;
}
sub get_field_align {
my ($field) = @_;
my $al = $SIZEOF_PTR;
my $meta = $field->getAttribute('ld:meta');
if ($meta eq 'number') {
$al = sizeof($field);
# linux aligns int64_t to $SIZEOF_PTR, windows to 8
# floats are 4 bytes so no pb
$al = 4 if ($al > 4 and (($os eq 'linux' and $arch == 32) or $al != 8));
} elsif ($meta eq 'global') {
$al = get_global_align($field);
} elsif ($meta eq 'compound') {
$al = get_compound_align($field);
} elsif ($meta eq 'static-array') {
my $tg = $field->findnodes('child::ld:item')->[0];
$al = get_field_align($tg);
} elsif ($meta eq 'bytes') {
$al = $field->getAttribute('alignment') || 1;
} elsif ($meta eq 'primitive') {
my $subtype = $field->getAttribute('ld:subtype');
if ($subtype eq 'stl-fstream' and $os eq 'windows') { $al = 8; }
}
return $al;
}
sub get_global_align {
my ($field) = @_;
my $typename = $field->getAttribute('type-name');
return $align_cache{$typename} if $align_cache{$typename};
my $g = $global_types{$typename};
my $st = $field->getAttribute('ld:subtype') || '';
if ($st eq 'bitfield' or $st eq 'enum' or $g->getAttribute('ld:meta') eq 'bitfield-type')
{
my $base = $field->getAttribute('base-type') || $g->getAttribute('base-type') || 'uint32_t';
print "$st type $base\n" if $base !~ /int(\d+)_t/;
# dont cache, field->base-type may differ
return $1/8;
}
my $al = 1;
for my $gf ($g->findnodes('child::ld:field')) {
my $fld_al = get_field_align($gf);
$al = $fld_al if $fld_al > $al;
}
$align_cache{$typename} = $al;
return $al;
}
sub get_compound_align {
my ($field) = @_;
my $st = $field->getAttribute('ld:subtype') || '';
if ($st eq 'bitfield' or $st eq 'enum')
{
my $base = $field->getAttribute('base-type') || 'uint32_t';
if ($base eq 'long') {
return $SIZEOF_LONG;
}
print "$st type $base\n" if $base !~ /int(\d+)_t/;
return $1/8;
}
my $al = 1;
for my $f ($field->findnodes('child::ld:field')) {
my $fal = get_field_align($f);
$al = $fal if $fal > $al;
}
return $al;
}
sub sizeof {
my ($field) = @_;
my $meta = $field->getAttribute('ld:meta');
if ($meta eq 'number') {
if ($field->getAttribute('ld:subtype') eq 'long') {
return $SIZEOF_LONG;
}
return $field->getAttribute('ld:bits')/8;
} elsif ($meta eq 'pointer') {
return $SIZEOF_PTR;
} elsif ($meta eq 'static-array') {
my $count = $field->getAttribute('count');
my $tg = $field->findnodes('child::ld:item')->[0];
return $count * sizeof($tg);
} elsif ($meta eq 'bitfield-type' or $meta eq 'enum-type') {
my $base = $field->getAttribute('base-type') || 'uint32_t';
print "$meta type $base\n" if $base !~ /int(\d+)_t/;
return $1/8;
} elsif ($meta eq 'global') {
my $typename = $field->getAttribute('type-name');
return $sizeof_cache{$typename} if $sizeof_cache{$typename};
my $g = $global_types{$typename};
my $st = $field->getAttribute('ld:subtype') || '';
if ($st eq 'bitfield' or $st eq 'enum' or $g->getAttribute('ld:meta') eq 'bitfield-type')
{
my $base = $field->getAttribute('base-type') || $g->getAttribute('base-type') || 'uint32_t';
print "$st type $base\n" if $base !~ /int(\d+)_t/;
return $1/8;
}
return sizeof($g);
} elsif ($meta eq 'class-type' or $meta eq 'struct-type' or $meta eq 'compound') {
return sizeof_compound($field);
} elsif ($meta eq 'container') {
my $subtype = $field->getAttribute('ld:subtype');
if ($subtype eq 'stl-vector') {
if ($os eq 'linux' or $os eq 'windows') {
return ($arch == 64) ? 24 : 12;
} else {
print "sizeof stl-vector on $os\n";
}
} elsif ($subtype eq 'stl-bit-vector') {
if ($os eq 'linux') {
return ($arch == 64) ? 40 : 20;
} elsif ($os eq 'windows') {
return ($arch == 64) ? 32 : 16;
} else {
print "sizeof stl-bit-vector on $os\n";
}
} elsif ($subtype eq 'stl-deque') {
if ($os eq 'linux') {
return ($arch == 64) ? 80 : 40;
} elsif ($os eq 'windows') {
return ($arch == 64) ? 40 : 20;
} else {
print "sizeof stl-deque on $os\n";
}
} elsif ($subtype eq 'df-linked-list') {
return 3 * $SIZEOF_PTR;
} elsif ($subtype eq 'df-flagarray') {
return 4 + $SIZEOF_PTR;
} elsif ($subtype eq 'df-static-flagarray') {
return $field->getAttribute('count');
} elsif ($subtype eq 'df-array') {
return 4 + $SIZEOF_PTR; # XXX 4->2 ?
} else {
print "sizeof container $subtype\n";
}
} elsif ($meta eq 'primitive') {
my $subtype = $field->getAttribute('ld:subtype');
if ($subtype eq 'stl-string') {
if ($os eq 'linux') {
return ($arch == 64) ? 8 : 4;
} elsif ($os eq 'windows') {
return ($arch == 64) ? 32 : 24;
} else {
print "sizeof stl-string on $os\n";
}
print "sizeof stl-string\n";
} elsif ($subtype eq 'stl-fstream') {
if ($os eq 'linux') {
return 284; # TODO: fix on x64
} elsif ($os eq 'windows') {
return ($arch == 64) ? 280 : 192;
} else {
print "sizeof stl-fstream on $os\n";
}
print "sizeof stl-fstream\n";
} else {
print "sizeof primitive $subtype\n";
}
} elsif ($meta eq 'bytes') {
return $field->getAttribute('size');
} else {
print "sizeof $meta\n";
}
}
sub sizeof_compound {
my ($field) = @_;
my $typename = $field->getAttribute('type-name');
return $sizeof_cache{$typename} if $typename and $sizeof_cache{$typename};
my $meta = $field->getAttribute('ld:meta');
my $st = $field->getAttribute('ld:subtype') || '';
if ($st eq 'bitfield' or $st eq 'enum')
{
my $base = $field->getAttribute('base-type') || 'uint32_t';
if ($base eq 'long') {
$sizeof_cache{$typename} = $SIZEOF_LONG if $typename;
return $SIZEOF_LONG;
}
print "$st type $base\n" if $base !~ /int(\d+)_t/;
$sizeof_cache{$typename} = $1/8 if $typename;
return $1/8;
}
if ($field->getAttribute('is-union'))
{
my $sz = 0;
for my $f ($field->findnodes('child::ld:field'))
{
my $fsz = sizeof($f);
$sz = $fsz if $fsz > $sz;
}
return $sz;
}
my $parent = $field->getAttribute('inherits-from');
my $off = 0;
$off = $SIZEOF_PTR if ($meta eq 'class-type');
$off = sizeof($global_types{$parent}) if ($parent);
my $al = 1;
$al = $SIZEOF_PTR if ($meta eq 'class-type');
for my $f ($field->findnodes('child::ld:field'))
{
my $fa = get_field_align($f);
$al = $fa if $fa > $al;
$off = align_field($off, $fa);
$off += sizeof($f);
}
# GCC: class a { vtable; char; } ; class b:a { char c2; } -> c2 has offset 5 (Windows MSVC: offset 8)
$al = 1 if ($meta eq 'class-type' and $os eq 'linux');
$off = align_field($off, $al);
$sizeof_cache{$typename} = $off if $typename;
return $off;
}
my $doc = XML::LibXML->new()->parse_file($input);
$global_types{$_->getAttribute('type-name')} = $_ foreach $doc->findnodes('/ld:data-definition/ld:global-type');
print get_sizeof($global_types{$target_class}) . "\n";