-
Notifications
You must be signed in to change notification settings - Fork 577
/
Copy patharm_table_chain.pl
executable file
·323 lines (305 loc) · 12.4 KB
/
arm_table_chain.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
317
318
319
320
321
322
323
#!/usr/bin/perl
# **********************************************************
# Copyright (c) 2014-2020 Google, Inc. All rights reserved.
# **********************************************************
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of Google, Inc. nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
# Pass this the file with the op_instr[] encoding chain starting points
# array along with headers containing the table shortcuts and the files
# containing the decoding table files to chain.
# If -o OP_<opc> is passed it will only consider that opcode; else it will
# process all opcodes.
# It will construct each encoding chain and in-place edit the table files.
# It has assumptions on the precise format of the decoding table
# and of the op_instr* starting point array.
#
# To run on one A32 opcode:
#
# tools/arm_table_chain.pl -v -o <opc> core/ir/arm/table_{private,encode,a32}*.[ch]
#
# To run on all A32 opcodes:
#
# tools/arm_table_chain.pl core/ir/arm/table_{private,encode,a32}*.[ch]
#
# To run on one T32 opcode:
#
# tools/arm_table_chain.pl -v -o <opc> core/ir/arm/table_{private,encode,t32}*.[ch]
#
# To run on all T32 opcodes for outside of IT blocks:
#
# tools/arm_table_chain.pl core/ir/arm/table_{private,encode,t32}*.[ch]
#
# To run on all T32 opcodes for inside of IT blocks:
#
# tools/arm_table_chain.pl -it core/ir/arm/table_{private,encode,t32}*.[ch]
#
# Assuming the .n format are always first and thus always the entries,
# we can run arm_table_chain.pl twice for inside and outside of IT blocks
# with T32 tables without affecting the in-table chaining.
my $verbose = 0;
die "Usage: $0 [-o OP_<opcode>] [-it] <table-files>\n" if ($#ARGV < 1);
my $single_op = '';
while ($ARGV[0] eq '-v') {
shift;
$verbose++;
}
if ($ARGV[0] eq '-o') {
shift;
$single_op = shift;
}
if ($ARGV[0] eq '-it') {
$it_block = 1;
shift;
}
my @allfiles = @ARGV;
my $table = "";
my $shape = "";
my $major = 0;
my $minor = 0;
my $instance = 0;
my $t32 = 0;
my @infiles;
# Remove table_t32_16.c or table_t32_16_it.c
foreach $infile (@allfiles) {
if ($it_block) {
push(@infiles, $infile) if ($infile !~/t32_16\.c/);
} else {
push(@infiles, $infile) if ($infile !~/t32_16_it\.c/);
}
}
# Must process headers first
@infiles = sort({return -1 if($a =~ /\.h$/);return 1 if($b =~ /\.h$/); return 0;}
@infiles);
foreach $infile (@infiles) {
print "Processing $infile\n" if ($verbose > 0);
$t32 = 1 if ($infile =~ /_t32_/);
open(INFILE, "< $infile") || die "Couldn't open $file\n";
while (<INFILE>) {
print "xxx $_\n" if ($verbose > 2);
if (/^#define (\w+)\s+\(ptr_int_t\)&(\w+)/) {
$shorthand{$2} = $1;
print "shorthand for $2 = $1\n" if ($verbose > 1);
}
if (/^const instr_info_t (\w+)([^=]+)=/) {
$table = $1;
$shape = $2;
$major = -1;
$minor = 0;
}
if (/{\s*\/\*\s*(\d+)\s*\*\/\s*$/) {
$major++;
$minor = 0;
}
if (/^\s*{(OP_\w+)[ ,]/ && ($single_op eq '' || $single_op eq $1) &&
$1 ne 'OP_CONTD') {
my $opc = $1;
$instance{$opc} = 0 if (!defined($instance{$opc}));
# Ignore duplicate encodings
my $is_new = 1;
my $instance_next = $instance{$opc} + 1;
my $encoding = extract_encoding($_);
my $hex = extract_hex($_);
for (my $i = 0; $i < @{$entry{$opc}}; $i++) {
if ($encoding eq $entry{$opc}[$i]{'encoding'}) {
$is_new = 0;
$dup{$opc}{$encoding} = 1;
# We must place the entry with the fewest required bits into the
# encoding chain, to allow for entries to set distinguishing
# immed and other varying bits on duplicate entries (e.g., b vs f
# for the SIMD D bit) for easier decode table reading.
if (hex($hex) < hex($hex{$opc}{$encoding})) {
$hex{$opc}{$encoding} = $hex;
# Replace the stored instance with this one
$is_new = 1;
$instance_next = $instance{$opc} + 1;
$instance{$opc} = $i;
}
print "Duplicate $hex $encoding => $hex{$opc}{$encoding}\n"
if ($verbose > 1);
last;
}
}
goto dup_line if (!$is_new);
$entry{$opc}[$instance{$opc}]{'line'} = $_;
$entry{$opc}[$instance{$opc}]{'encoding'} = $encoding;
$entry{$opc}[$instance{$opc}]{'hex'} = $hex;
$hex{$opc}{$encoding} = $hex;
die "Error: no shorthand for $table\n" if ($shorthand{$table} eq '');
if ($shape =~ /\d/) {
$entry{$opc}[$instance{$opc}]{'addr_short'} =
sprintf "$shorthand{$table}\[$major][0x%02x]", $minor;
$entry{$opc}[$instance{$opc}]{'addr_long'} =
sprintf "$table\[$major][0x%02x]", $minor;
} else {
$entry{$opc}[$instance{$opc}]{'addr_short'} =
sprintf "$shorthand{$table}\[0x%02x]", $minor;
$entry{$opc}[$instance{$opc}]{'addr_long'} =
sprintf "$table\[0x%02x]", $minor;
}
# Order for sorting:
# + Prefer no-shift over shift
# + Prefer shift via immed over shift via reg
# + Prefer P=1 and U=1
# + Prefer 8-byte over 16-byte and over 4-byte
my $priority = $instance{$opc};
$priority-=10 if (/sh2, i/);
$priority-=10 if (/sh2, R/);
$priority-=10 if (/xop_shift/);
$priority+=10 if (/, [in]/);
$priority+=10 if (/, M.\d/);
$priority-=10 if (/, M.S/);
$priority+=50 if (/PUW=1../);
$priority+=100 if (/PUW=1.0/);
$priority+=10 if (/PUW=.1./);
$priority+=10 if (/[VW][ABC]q,/);
$priority-=50 if (/[VW][ABC]d,/);
# exop must be final member of chain
$priority-=1000 if (/exop\[\w+\]},/);
# + Prefer 16-bit Thumb code over 32-bit Thumb code
$priority+=500 if (/0x([\da-f]){4},/);
$priority+=10 if (/\bSPw\b|\bPCw\b/);
$priority+=10 if (/\bMSPP8w\b|\bMPCP8w\b/);
$priority+=10 if (/\bMSPDBl\b|\bMSPl\b/);
# + We have to take PC special-case first, b/c in some cases
# it has a larger immed which conflicts w/ other bits in non-PC
# (e.g., T32 OP_ldr with MPCN12w opnd). I don't think this
# generalization of that one known case as any negatives.
$priority+=200 if ($table =~ /PC$/);
$entry{$opc}[$instance{$opc}]{'priority'} = $priority;
if ($verbose > 0) {
my $tmp = $entry{$opc}[$instance{$opc}]{'addr_long'};
print "$priority $tmp $_";
}
$instance{$opc} = $instance_next;
}
dup_line:
$minor++ if (/^\s*{[A-Z]/);
}
close(INFILE);
}
foreach my $opc (keys %entry) {
@{$entry{$opc}} = sort({$b->{'priority'} <=> $a->{'priority'}} @{$entry{$opc}});
if ($verbose > 1) {
print "Sorted:\n";
for (my $i = 0; $i < @{$entry{$opc}}; $i++) {
my $tmp = $entry{$opc}[$i]{'addr_long'};
my $pri = $entry{$opc}[$i]{'priority'};
print "$pri $tmp\n";
}
}
}
# Now edit the files in place
$^I = '.bak';
foreach $infile (@infiles) {
@ARGV = ($infile);
while (<>) {
my $encoding = extract_encoding($_);
my $hex = extract_hex($_);
my $handled = 0;
if (/^\s+\/\* (OP_\w+)[ ,]/ && ($single_op eq '' || $single_op eq $1)) {
my $opc = $1;
if (defined($entry{$opc}[0]{'addr_long'})) {
my $start = $entry{$opc}[0]{'addr_long'};
if ($t32) {
if ($it_block) {
# Assuming the .n format are always first and thus always
# the entries, we can implement here for IT block and
# not inside the in-table chaining.
if ($opc !~ /OP_cps/ && $opc !~ /OP_setend$/) {
# OP_cps* and OP_setend are not permitted in IT block.
# We exclude OP_cps* and OP_setend to avoid creating
# separate table for T32.32 IT block instructions.
s/{(&.*,\s*&.*,\s*)&.*}/{\1&$start}/;
}
} else {
s/{(&.*,\s*)&.*(,\s*&.*)}/{\1&$start\2}/;
}
} else {
s/{&.*,(\s*&.*\s*&.*)}/{&$start,\1}/;
}
}
}
if (/^\s*{(OP_\w+)[ ,]/ && ($single_op eq '' || $single_op eq $1) &&
$1 ne 'OP_CONTD') {
my $opc = $1;
if (defined($dup{$opc}{$encoding})) {
# Keep the one with the fewest bits, to allow for optional bits
# (e.g., immeds or reg bits) to be set to make the tables easier to
# read.
# For duplicate hex values, take 1st one.
if ($hex eq $hex{$opc}{$encoding} &&
!defined($printed_dup{$opc}{$encoding})) {
$printed_dup{$opc}{$encoding} = 1;
if (@{$entry{$opc}} == 1) {
s/, *[\w\[\]_]+},/, END_LIST},/ unless /exop\[\w+\]},/;
$handled = 1;
}
} else {
s/, *[\w\[\]_]+},/, DUP_ENTRY},/ unless /exop\[\w+\]},/;
$handled = 1;
}
}
if (!$handled) {
for (my $i = 0; $i < @{$entry{$opc}}; $i++) {
if ($_ eq $entry{$opc}[$i]{'line'}) {
if ($i == @{$entry{$opc}} - 1) {
s/, *[\w\[\]_]+},/, END_LIST},/ unless /exop\[\w+\]},/;
} else {
if (/exop\[\w+\]},/) {
print STDERR
"ERROR: exop must be final element in chain: $_\n";
} else {
my $chain = $entry{$opc}[$i+1]{'addr_short'};
s/, *[\w\[\]_]+},/, $chain},/;
}
}
last;
}
}
}
}
print;
}
}
sub extract_encoding($)
{
my ($line) = @_;
# Remove up through opcode name (to remove encoding hexes) and
# final field and comments
$line =~ s/^[^"]+"/"/;
$line =~ s/,[^,]+}.*$//;
$line =~ s/\s*$//;
return $line;
}
sub extract_hex($)
{
my ($line) = @_;
if ($line =~ /{OP_\w+,\s*0x([0-9a-fA-F]+),/) {
return $1;
} else {
return "not found";
}
}