[llvm] [NFC][DecoderEmitter] Code cleanup in `DecoderEmitter::emitTable` (PR #158014)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 14 16:51:44 PDT 2025
================
@@ -774,141 +794,149 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
DecoderTable::const_iterator E = Table.end();
const uint8_t *const EndPtr = Table.data() + Table.size();
- auto emitNumToSkipComment = [&](uint32_t NumToSkip, bool InComment = false) {
+ auto EmitPos = [&OS](uint32_t Pos) {
+ constexpr uint32_t StartColumn = 12;
+ OS << "/* " << Pos << " */";
+ OS.PadToColumn(StartColumn);
+ };
+
+ auto StartComment = [&OS]() {
+ constexpr uint32_t CommentColumn = 52;
+ OS.PadToColumn(CommentColumn);
+ OS << "// ";
+ };
+
+ auto EmitNumToSkipComment = [&](uint32_t NumToSkip) {
uint32_t Index = ((I - Table.begin()) + NumToSkip);
- OS << (InComment ? ", " : "// ");
- OS << "Skip to: " << Index;
+ OS << "skip to " << Index;
};
// The first entry when specializing decoders per bitwidth is the bitwidth.
// This will be used for additional checks in `decodeInstruction`.
if (SpecializeDecodersPerBitwidth) {
- OS << "/* 0 */";
- OS.PadToColumn(14);
- emitULEB128(I, OS);
- OS << " // Bitwidth " << BitWidth << '\n';
+ EmitPos(0);
+ EmitULEB128(I, OS);
+ StartComment();
+ OS << "Bitwidth " << BitWidth << '\n';
}
+ auto DecodeAndEmitULEB128 = [EndPtr,
+ &EmitULEB128](DecoderTable::const_iterator &I,
+ formatted_raw_ostream &OS) {
+ const char *ErrMsg = nullptr;
+ uint64_t Value = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
+ assert(ErrMsg == nullptr && "ULEB128 value too large!");
+
+ EmitULEB128(I, OS);
+ return Value;
+ };
+
unsigned OpcodeMask = 0;
while (I != E) {
assert(I < E && "incomplete decode table entry!");
- uint64_t Pos = I - Table.begin();
- OS << "/* " << Pos << " */";
- OS.PadToColumn(12);
-
+ uint32_t Pos = I - Table.begin();
+ EmitPos(Pos);
const uint8_t DecoderOp = *I++;
OpcodeMask |= (1 << DecoderOp);
+ OS << getDecoderOpName(static_cast<DecoderOps>(DecoderOp)) << ", ";
switch (DecoderOp) {
default:
PrintFatalError("Invalid decode table opcode: " + Twine((int)DecoderOp) +
" at index " + Twine(Pos));
- case MCD::OPC_Scope: {
- OS << " MCD::OPC_Scope, ";
- uint32_t NumToSkip = emitNumToSkip(I, OS);
- emitNumToSkipComment(NumToSkip);
- OS << '\n';
+ case OPC_Scope: {
+ uint32_t NumToSkip = EmitNumToSkip(I, OS);
+ StartComment();
+ EmitNumToSkipComment(NumToSkip);
break;
}
- case MCD::OPC_ExtractField: {
- OS << " MCD::OPC_ExtractField, ";
-
+ case OPC_ExtractField: {
// ULEB128 encoded start value.
- const char *ErrMsg = nullptr;
- unsigned Start = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
- assert(ErrMsg == nullptr && "ULEB128 value too large!");
- emitULEB128(I, OS);
-
+ unsigned Start = DecodeAndEmitULEB128(I, OS);
unsigned Len = *I++;
- OS << Len << ", // Inst{";
+ OS << Len << ',';
+ StartComment();
+ OS << "Field = Inst{";
if (Len > 1)
- OS << (Start + Len - 1) << "-";
- OS << Start << "} ...\n";
+ OS << (Start + Len - 1) << '-';
+ OS << Start << '}';
break;
}
- case MCD::OPC_FilterValueOrSkip: {
- OS << " MCD::OPC_FilterValueOrSkip, ";
+ case OPC_FilterValueOrSkip: {
// The filter value is ULEB128 encoded.
- emitULEB128(I, OS);
- uint32_t NumToSkip = emitNumToSkip(I, OS);
- emitNumToSkipComment(NumToSkip);
- OS << '\n';
+ uint64_t FilterVal = DecodeAndEmitULEB128(I, OS);
+ uint32_t NumToSkip = EmitNumToSkip(I, OS);
+ StartComment();
+ OS << "if Field != " << FilterVal << ' ';
+ EmitNumToSkipComment(NumToSkip);
break;
}
- case MCD::OPC_FilterValue: {
- OS << " MCD::OPC_FilterValue, ";
+ case OPC_FilterValue: {
// The filter value is ULEB128 encoded.
- emitULEB128(I, OS);
- OS << '\n';
+ uint64_t FilterVal = DecodeAndEmitULEB128(I, OS);
+
+ StartComment();
+ OS << "if Field != " << FilterVal << " pop scope";
break;
}
- case MCD::OPC_CheckField: {
- OS << " MCD::OPC_CheckField, ";
+ case OPC_CheckField: {
// ULEB128 encoded start value.
- emitULEB128(I, OS);
+ unsigned Start = DecodeAndEmitULEB128(I, OS);
+
// 8-bit length.
unsigned Len = *I++;
OS << Len << ", ";
+
// ULEB128 encoded field value.
- emitULEB128(I, OS);
- OS << '\n';
+ uint64_t FieldVal = DecodeAndEmitULEB128(I, OS);
+
+ StartComment();
+ OS << "if Inst{";
+ if (Len > 1)
+ OS << (Start + Len - 1) << '-';
+ OS << Start << "} != " << FieldVal << " pop scope";
break;
}
- case MCD::OPC_CheckPredicate: {
- OS << " MCD::OPC_CheckPredicate, ";
- emitULEB128(I, OS);
- OS << '\n';
+ case OPC_CheckPredicate: {
+ unsigned PIdx = DecodeAndEmitULEB128(I, OS);
+ StartComment();
+ OS << "if !checkPredicate(" << PIdx << ") pop scope";
break;
}
- case MCD::OPC_Decode:
- case MCD::OPC_TryDecode: {
- bool IsTry = DecoderOp == MCD::OPC_TryDecode;
+ case OPC_Decode:
+ case OPC_TryDecode: {
// Decode the Opcode value.
- const char *ErrMsg = nullptr;
- unsigned Opc = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
- assert(ErrMsg == nullptr && "ULEB128 value too large!");
-
- OS << " MCD::OPC_" << (IsTry ? "Try" : "") << "Decode, ";
- emitULEB128(I, OS);
+ unsigned Opc = DecodeAndEmitULEB128(I, OS);
// Decoder index.
- unsigned DecodeIdx = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
- assert(ErrMsg == nullptr && "ULEB128 value too large!");
- emitULEB128(I, OS);
+ unsigned DecodeIdx = DecodeAndEmitULEB128(I, OS);
auto EncI = OpcodeToEncodingID.find(Opc);
assert(EncI != OpcodeToEncodingID.end() && "no encoding entry");
auto EncodingID = EncI->second;
- if (!IsTry) {
- OS << "// Opcode: " << Encodings[EncodingID].getName()
- << ", DecodeIdx: " << DecodeIdx << '\n';
- break;
- }
- OS << '\n';
+ StartComment();
+ OS << "Opcode: " << Encodings[EncodingID].getName()
+ << ", DecodeIdx: " << DecodeIdx;
break;
}
- case MCD::OPC_SoftFail: {
- OS << " MCD::OPC_SoftFail, ";
+ case OPC_SoftFail: {
// Decode the positive mask.
- const char *ErrMsg = nullptr;
- uint64_t PositiveMask = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
- assert(ErrMsg == nullptr && "ULEB128 value too large!");
- emitULEB128(I, OS);
+ uint64_t PositiveMask = DecodeAndEmitULEB128(I, OS);
// Decode the negative mask.
- uint64_t NegativeMask = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
- assert(ErrMsg == nullptr && "ULEB128 value too large!");
- emitULEB128(I, OS);
- OS << "// +ve mask: 0x";
+ uint64_t NegativeMask = DecodeAndEmitULEB128(I, OS);
+
+ StartComment();
+ OS << "+ve mask: 0x";
OS.write_hex(PositiveMask);
----------------
jurahul wrote:
Done.
https://github.com/llvm/llvm-project/pull/158014
More information about the llvm-commits
mailing list