32
32
#include " llvm/Support/CommandLine.h"
33
33
#include " llvm/Support/Debug.h"
34
34
#include " llvm/Support/ErrorHandling.h"
35
- #include " llvm/Support/FormatVariadic.h"
36
35
#include " llvm/Support/FormattedStream.h"
37
36
#include " llvm/Support/LEB128.h"
38
- #include " llvm/Support/MathExtras.h"
39
37
#include " llvm/Support/raw_ostream.h"
40
38
#include " llvm/TableGen/Error.h"
41
39
#include " llvm/TableGen/Record.h"
@@ -78,12 +76,6 @@ static cl::opt<SuppressLevel> DecoderEmitterSuppressDuplicates(
78
76
" significantly reducing Table Duplications" )),
79
77
cl::init(SUPPRESSION_DISABLE), cl::cat(DisassemblerEmitterCat));
80
78
81
- static cl::opt<uint32_t >
82
- NumToSkipSizeInBytes (" num-to-skip-size" ,
83
- cl::desc (" number of bytes to use for num-to-skip "
84
- " entries in the decoder table (2 or 3)" ),
85
- cl::init(2 ), cl::cat(DisassemblerEmitterCat));
86
-
87
79
STATISTIC (NumEncodings, " Number of encodings considered" );
88
80
STATISTIC (NumEncodingsLackingDisasm,
89
81
" Number of encodings without disassembler info" );
@@ -138,29 +130,10 @@ struct DecoderTable : public std::vector<uint8_t> {
138
130
// in the table for patching.
139
131
size_t insertNumToSkip () {
140
132
size_t Size = size ();
141
- insert (end (), NumToSkipSizeInBytes , 0 );
133
+ insert (end (), 3 , 0 );
142
134
return Size ;
143
135
}
144
-
145
- void patchNumToSkip (size_t FixupIdx, uint32_t DestIdx) {
146
- // Calculate the distance from the byte following the fixup entry byte
147
- // to the destination. The Target is calculated from after the
148
- // `NumToSkipSizeInBytes`-byte NumToSkip entry itself, so subtract
149
- // `NumToSkipSizeInBytes` from the displacement here to account for that.
150
- assert (DestIdx >= FixupIdx + NumToSkipSizeInBytes &&
151
- " Expecting a forward jump in the decoding table" );
152
- uint32_t Delta = DestIdx - FixupIdx - NumToSkipSizeInBytes;
153
- if (!isUIntN (8 * NumToSkipSizeInBytes, Delta))
154
- PrintFatalError (
155
- " disassembler decoding table too large, try --num-to-skip-size=3" );
156
-
157
- (*this )[FixupIdx] = static_cast <uint8_t >(Delta);
158
- (*this )[FixupIdx + 1 ] = static_cast <uint8_t >(Delta >> 8 );
159
- if (NumToSkipSizeInBytes == 3 )
160
- (*this )[FixupIdx + 2 ] = static_cast <uint8_t >(Delta >> 16 );
161
- }
162
136
};
163
-
164
137
struct DecoderTableInfo {
165
138
DecoderTable Table;
166
139
FixupScopeList FixupStack;
@@ -717,8 +690,19 @@ static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
717
690
uint32_t DestIdx) {
718
691
// Any NumToSkip fixups in the current scope can resolve to the
719
692
// current location.
720
- for (uint32_t FixupIdx : Fixups)
721
- Table.patchNumToSkip (FixupIdx, DestIdx);
693
+ for (uint32_t FixupIdx : reverse (Fixups)) {
694
+ // Calculate the distance from the byte following the fixup entry byte
695
+ // to the destination. The Target is calculated from after the 24-bit
696
+ // NumToSkip entry itself, so subtract three from the displacement here
697
+ // to account for that.
698
+ uint32_t Delta = DestIdx - FixupIdx - 3 ;
699
+ // Our NumToSkip entries are 24-bits. Make sure our table isn't too
700
+ // big.
701
+ assert (isUInt<24 >(Delta));
702
+ Table[FixupIdx] = (uint8_t )Delta;
703
+ Table[FixupIdx + 1 ] = (uint8_t )(Delta >> 8 );
704
+ Table[FixupIdx + 2 ] = (uint8_t )(Delta >> 16 );
705
+ }
722
706
}
723
707
724
708
// Emit table entries to decode instructions given a segment or segments
@@ -775,9 +759,15 @@ void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
775
759
Delegate->emitTableEntries (TableInfo);
776
760
777
761
// Now that we've emitted the body of the handler, update the NumToSkip
778
- // of the filter itself to be able to skip forward when false.
779
- if (PrevFilter)
780
- Table.patchNumToSkip (PrevFilter, Table.size ());
762
+ // of the filter itself to be able to skip forward when false. Subtract
763
+ // three as to account for the width of the NumToSkip field itself.
764
+ if (PrevFilter) {
765
+ uint32_t NumToSkip = Table.size () - PrevFilter - 3 ;
766
+ assert (isUInt<24 >(NumToSkip) && " disassembler decoding table too large!" );
767
+ Table[PrevFilter] = (uint8_t )NumToSkip;
768
+ Table[PrevFilter + 1 ] = (uint8_t )(NumToSkip >> 8 );
769
+ Table[PrevFilter + 2 ] = (uint8_t )(NumToSkip >> 16 );
770
+ }
781
771
}
782
772
783
773
// If there is no fallthrough, then the final filter should get fixed
@@ -824,8 +814,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
824
814
OS << (unsigned )*I++ << " , " ;
825
815
};
826
816
827
- // Emit `NumToSkipSizeInBytes`-byte numtoskip value to OS, returning the
828
- // NumToSkip value.
817
+ // Emit 24-bit numtoskip value to OS, returning the NumToSkip value.
829
818
auto emitNumToSkip = [](DecoderTable::const_iterator &I,
830
819
formatted_raw_ostream &OS) {
831
820
uint8_t Byte = *I++;
@@ -834,11 +823,9 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
834
823
Byte = *I++;
835
824
OS << (unsigned )Byte << " , " ;
836
825
NumToSkip |= Byte << 8 ;
837
- if (NumToSkipSizeInBytes == 3 ) {
838
- Byte = *I++;
839
- OS << (unsigned )(Byte ) << " , " ;
840
- NumToSkip |= Byte << 16 ;
841
- }
826
+ Byte = *I++;
827
+ OS << (unsigned )(Byte ) << " , " ;
828
+ NumToSkip |= Byte << 16 ;
842
829
return NumToSkip;
843
830
};
844
831
@@ -880,7 +867,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
880
867
// The filter value is ULEB128 encoded.
881
868
emitULEB128 (I, OS);
882
869
883
- // numtoskip value.
870
+ // 24-bit numtoskip value.
884
871
uint32_t NumToSkip = emitNumToSkip (I, OS);
885
872
OS << " // Skip to: " << ((I - Table.begin ()) + NumToSkip) << " \n " ;
886
873
break ;
@@ -896,7 +883,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
896
883
// ULEB128 encoded field value.
897
884
emitULEB128 (I, OS);
898
885
899
- // numtoskip value.
886
+ // 24-bit numtoskip value.
900
887
uint32_t NumToSkip = emitNumToSkip (I, OS);
901
888
OS << " // Skip to: " << ((I - Table.begin ()) + NumToSkip) << " \n " ;
902
889
break ;
@@ -906,7 +893,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
906
893
OS << Indent << " MCD::OPC_CheckPredicate, " ;
907
894
emitULEB128 (I, OS);
908
895
909
- // numtoskip value.
896
+ // 24-bit numtoskip value.
910
897
uint32_t NumToSkip = emitNumToSkip (I, OS);
911
898
OS << " // Skip to: " << ((I - Table.begin ()) + NumToSkip) << " \n " ;
912
899
break ;
@@ -938,7 +925,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
938
925
939
926
// Fallthrough for OPC_TryDecode.
940
927
941
- // numtoskip value.
928
+ // 24-bit numtoskip value.
942
929
uint32_t NumToSkip = emitNumToSkip (I, OS);
943
930
944
931
OS << " // Opcode: " << NumberedEncodings[EncodingID]
@@ -1424,9 +1411,9 @@ void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1424
1411
TableInfo.Table .push_back (NumBits);
1425
1412
TableInfo.Table .insertULEB128 (Ilnd.FieldVal );
1426
1413
1427
- // Allocate space in the table for fixup (NumToSkipSizeInBytes) so all
1428
- // our relative position calculations work OK even before we fully
1429
- // resolve the real value here.
1414
+ // The fixup is always 24-bits, so go ahead and allocate the space
1415
+ // in the table so all our relative position calculations work OK even
1416
+ // before we fully resolve the real value here.
1430
1417
1431
1418
// Push location for NumToSkip backpatching.
1432
1419
TableInfo.FixupStack .back ().push_back (TableInfo.Table .insertNumToSkip ());
@@ -2170,18 +2157,7 @@ insertBits(InsnType &field, uint64_t bits, unsigned startBit, unsigned numBits)
2170
2157
// decodeInstruction().
2171
2158
static void emitDecodeInstruction (formatted_raw_ostream &OS,
2172
2159
bool IsVarLenInst) {
2173
- OS << formatv (" \n constexpr unsigned NumToSkipSizeInBytes = {};\n " ,
2174
- NumToSkipSizeInBytes);
2175
-
2176
2160
OS << R"(
2177
- inline unsigned decodeNumToSkip(const uint8_t *&Ptr) {
2178
- unsigned NumToSkip = *Ptr++;
2179
- NumToSkip |= (*Ptr++) << 8;
2180
- if constexpr (NumToSkipSizeInBytes == 3)
2181
- NumToSkip |= (*Ptr++) << 16;
2182
- return NumToSkip;
2183
- }
2184
-
2185
2161
template <typename InsnType>
2186
2162
static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
2187
2163
InsnType insn, uint64_t Address,
@@ -2219,7 +2195,10 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
2219
2195
// Decode the field value.
2220
2196
uint64_t Val = decodeULEB128AndIncUnsafe(++Ptr);
2221
2197
bool Failed = Val != CurFieldValue;
2222
- unsigned NumToSkip = decodeNumToSkip(Ptr);
2198
+ // NumToSkip is a plain 24-bit integer.
2199
+ unsigned NumToSkip = *Ptr++;
2200
+ NumToSkip |= (*Ptr++) << 8;
2201
+ NumToSkip |= (*Ptr++) << 16;
2223
2202
2224
2203
// Perform the filter operation.
2225
2204
if (Failed)
@@ -2243,7 +2222,10 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
2243
2222
uint64_t ExpectedValue = decodeULEB128(++Ptr, &PtrLen);
2244
2223
Ptr += PtrLen;
2245
2224
bool Failed = ExpectedValue != FieldValue;
2246
- unsigned NumToSkip = decodeNumToSkip(Ptr);
2225
+ // NumToSkip is a plain 24-bit integer.
2226
+ unsigned NumToSkip = *Ptr++;
2227
+ NumToSkip |= (*Ptr++) << 8;
2228
+ NumToSkip |= (*Ptr++) << 16;
2247
2229
2248
2230
// If the actual and expected values don't match, skip.
2249
2231
if (Failed)
@@ -2258,7 +2240,10 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
2258
2240
case MCD::OPC_CheckPredicate: {
2259
2241
// Decode the Predicate Index value.
2260
2242
unsigned PIdx = decodeULEB128AndIncUnsafe(++Ptr);
2261
- unsigned NumToSkip = decodeNumToSkip(Ptr);
2243
+ // NumToSkip is a plain 24-bit integer.
2244
+ unsigned NumToSkip = *Ptr++;
2245
+ NumToSkip |= (*Ptr++) << 8;
2246
+ NumToSkip |= (*Ptr++) << 16;
2262
2247
// Check the predicate.
2263
2248
bool Failed = !checkDecoderPredicate(PIdx, Bits);
2264
2249
if (Failed)
@@ -2293,7 +2278,10 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
2293
2278
// Decode the Opcode value.
2294
2279
unsigned Opc = decodeULEB128AndIncUnsafe(++Ptr);
2295
2280
unsigned DecodeIdx = decodeULEB128AndIncUnsafe(Ptr);
2296
- unsigned NumToSkip = decodeNumToSkip(Ptr);
2281
+ // NumToSkip is a plain 24-bit integer.
2282
+ unsigned NumToSkip = *Ptr++;
2283
+ NumToSkip |= (*Ptr++) << 8;
2284
+ NumToSkip |= (*Ptr++) << 16;
2297
2285
2298
2286
// Perform the decode operation.
2299
2287
MCInst TmpMI;
@@ -2418,9 +2406,6 @@ handleHwModesUnrelatedEncodings(const CodeGenInstruction *Instr,
2418
2406
2419
2407
// Emits disassembler code for instruction decoding.
2420
2408
void DecoderEmitter::run (raw_ostream &o) {
2421
- if (NumToSkipSizeInBytes != 2 && NumToSkipSizeInBytes != 3 )
2422
- PrintFatalError (" Invalid value for num-to-skip-size, must be 2 or 3" );
2423
-
2424
2409
formatted_raw_ostream OS (o);
2425
2410
OS << R"(
2426
2411
#include "llvm/MC/MCInst.h"
0 commit comments