@@ -222,8 +222,9 @@ class DecoderEmitter {
222222 DecoderEmitter (const RecordKeeper &R, StringRef PredicateNamespace)
223223 : RK(R), Target(R), PredicateNamespace(PredicateNamespace) {}
224224
225- // Emit the decoder state machine table.
226- void emitTable (formatted_raw_ostream &OS, DecoderTable &Table, indent Indent,
225+ // Emit the decoder state machine table. Return true if any `TryDecode` ops
226+ // were generated.
227+ bool emitTable (formatted_raw_ostream &OS, DecoderTable &Table, indent Indent,
227228 unsigned BitWidth, StringRef Namespace,
228229 const EncodingIDsVec &EncodingIDs) const ;
229230 void emitInstrLenTable (formatted_raw_ostream &OS,
@@ -827,7 +828,7 @@ unsigned Filter::usefulness() const {
827828// ////////////////////////////////
828829
829830// Emit the decoder state machine table.
830- void DecoderEmitter::emitTable (formatted_raw_ostream &OS, DecoderTable &Table,
831+ bool DecoderEmitter::emitTable (formatted_raw_ostream &OS, DecoderTable &Table,
831832 indent Indent, unsigned BitWidth,
832833 StringRef Namespace,
833834 const EncodingIDsVec &EncodingIDs) const {
@@ -884,6 +885,8 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
884885 OS << " (Fail)" ;
885886 };
886887
888+ bool HasTryDecode = false ;
889+
887890 while (I != E) {
888891 assert (I < E && " incomplete decode table entry!" );
889892
@@ -964,6 +967,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
964967 case MCD::OPC_TryDecodeOrFail: {
965968 bool IsFail = DecoderOp == MCD::OPC_TryDecodeOrFail;
966969 bool IsTry = DecoderOp == MCD::OPC_TryDecode || IsFail;
970+ HasTryDecode |= IsTry;
967971 // Decode the Opcode value.
968972 const char *ErrMsg = nullptr ;
969973 unsigned Opc = decodeULEB128 (&*I, nullptr , EndPtr, &ErrMsg);
@@ -1027,6 +1031,8 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
10271031 Indent -= 2 ;
10281032
10291033 OS << Indent << " };\n\n " ;
1034+
1035+ return HasTryDecode;
10301036}
10311037
10321038void DecoderEmitter::emitInstrLenTable (formatted_raw_ostream &OS,
@@ -2217,8 +2223,8 @@ static void insertBits(InsnType &field, InsnType bits, unsigned startBit,
22172223
22182224// emitDecodeInstruction - Emit the templated helper function
22192225// decodeInstruction().
2220- static void emitDecodeInstruction (formatted_raw_ostream &OS,
2221- bool IsVarLenInst ) {
2226+ static void emitDecodeInstruction (formatted_raw_ostream &OS, bool IsVarLenInst,
2227+ bool HasTryDecode ) {
22222228 OS << R"(
22232229static unsigned decodeNumToSkip(const uint8_t *&Ptr) {
22242230 unsigned NumToSkip = *Ptr++;
@@ -2364,7 +2370,9 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
23642370 << ", using decoder " << DecodeIdx << ": "
23652371 << (S != MCDisassembler::Fail ? "PASS\n" : "FAIL\n"));
23662372 return S;
2367- }
2373+ })" ;
2374+ if (HasTryDecode) {
2375+ OS << R"(
23682376 case MCD::OPC_TryDecode:
23692377 case MCD::OPC_TryDecodeOrFail: {
23702378 bool IsFail = DecoderOp == MCD::OPC_TryDecodeOrFail;
@@ -2399,7 +2407,9 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
23992407 // set before the decode attempt.
24002408 S = MCDisassembler::Success;
24012409 break;
2402- }
2410+ })" ;
2411+ }
2412+ OS << R"(
24032413 case MCD::OPC_SoftFail: {
24042414 // Decode the mask values.
24052415 uint64_t PositiveMask = decodeULEB128AndIncUnsafe(Ptr);
@@ -2609,6 +2619,7 @@ namespace {
26092619 }
26102620
26112621 DecoderTableInfo TableInfo;
2622+ bool HasTryDecode = false ;
26122623 for (const auto &Opc : OpcMap) {
26132624 // Emit the decoder for this namespace+width combination.
26142625 ArrayRef<EncodingAndInst> NumberedEncodingsRef (NumberedEncodings.data (),
@@ -2634,8 +2645,8 @@ namespace {
26342645 TableInfo.Table .push_back (MCD::OPC_Fail);
26352646
26362647 // Print the table to the output stream.
2637- emitTable (OS, TableInfo.Table , indent (0 ), FC.getBitWidth (), Opc. first . first ,
2638- Opc.second );
2648+ HasTryDecode |= emitTable (OS, TableInfo.Table , indent (0 ), FC.getBitWidth (),
2649+ Opc. first . first , Opc.second );
26392650 }
26402651
26412652 // For variable instruction, we emit a instruction length table
@@ -2650,7 +2661,7 @@ namespace {
26502661 emitDecoderFunction (OS, TableInfo.Decoders , indent (0 ));
26512662
26522663 // Emit the main entry point for the decoder, decodeInstruction().
2653- emitDecodeInstruction (OS, IsVarLenInst);
2664+ emitDecodeInstruction (OS, IsVarLenInst, HasTryDecode );
26542665
26552666 OS << " \n } // namespace\n " ;
26562667}
0 commit comments