[llvm] [NFC][DecoderEmitter] Code cleanup in `DecoderEmitter::emitTable` (PR #158014)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 11 09:47:48 PDT 2025


================
@@ -775,141 +795,153 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
   DecoderTable::const_iterator E = Table.end();
   const uint8_t *const EndPtr = Table.data() + Table.size();
 
+  constexpr uint32_t StartColumn = 14;
+
+  auto StartComment = [&OS]() {
+    constexpr uint32_t CommentColumn = 60;
+    OS.PadToColumn(CommentColumn);
+    OS << " // ";
+  };
+
   auto emitNumToSkipComment = [&](uint32_t NumToSkip, bool InComment = false) {
     uint32_t Index = ((I - Table.begin()) + NumToSkip);
-    OS << (InComment ? ", " : "// ");
+    if (InComment)
+      OS << ", ";
+    else
+      StartComment();
     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);
+    OS.PadToColumn(StartColumn);
     emitULEB128(I, OS);
     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);
+    OS.PadToColumn(StartColumn);
 
     const uint8_t DecoderOp = *I++;
     OpcodeMask |= (1 << DecoderOp);
+    OS << "MCD::" << getDecoderOpName(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';
       break;
     }
     case MCD::OPC_ExtractField: {
-      OS << "  MCD::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 << "Inst{";
----------------
lenary wrote:

Yeah, this works :)

https://github.com/llvm/llvm-project/pull/158014


More information about the llvm-commits mailing list