[llvm] [M68k] Disassemble index suppress in the full extension word correctly (PR #202080)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 6 12:41:22 PDT 2026


https://github.com/mshockwave created https://github.com/llvm/llvm-project/pull/202080

When the IS (Index Suppress) bit in the full extension word is set, we need to use NoReg for the index register in the decoded MCInst. This was not properly supported in the disassembler before.

This patch fixes this issue by using a pseudo 5-bit "register" binary encoding for index register, where the first 4 bits carry the actual register number and the last bit (at MSB) indicates whether we suppress index register or not.

>From c25f43c1d25bce3595f0b0808715e2eb8586ac5b Mon Sep 17 00:00:00 2001
From: Min Hsu <min at myhsu.dev>
Date: Sat, 6 Jun 2026 12:34:43 -0700
Subject: [PATCH] [M68k] Disassemble index suppress in the full extension word
 correctly

---
 .../Target/M68k/Disassembler/M68kDisassembler.cpp   | 13 +++++++++++++
 llvm/lib/Target/M68k/M68kInstrFormats.td            |  7 ++-----
 .../Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp  |  5 ++++-
 llvm/test/MC/Disassembler/M68k/data.txt             |  3 +++
 4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp b/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
index f3ce9daaacc35..8e57add28a5f0 100644
--- a/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
+++ b/llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
@@ -132,6 +132,19 @@ static DecodeStatus DecodeScale(MCInst &Inst, uint64_t Imm, uint64_t Address,
   return DecodeStatus::Success;
 }
 
+static DecodeStatus DecodeIndexRegister(MCInst &Inst, uint64_t IndexReg,
+                                        uint64_t Address, const void *Decoder) {
+  // 4 bits actual RegNo + 1 bit index suppress
+  assert(isUInt<5>(IndexReg));
+  if (IndexReg & 0b10000) {
+    // Index suppress, fill in with NoReg
+    Inst.addOperand(MCOperand::createReg(M68k::NoRegister));
+    return DecodeStatus::Success;
+  }
+
+  return DecodeXR32RegisterClass(Inst, IndexReg & 0b1111, Address, Decoder);
+}
+
 #include "M68kGenDisassemblerTables.inc"
 
 #undef DecodeFPDR32RegisterClass
diff --git a/llvm/lib/Target/M68k/M68kInstrFormats.td b/llvm/lib/Target/M68k/M68kInstrFormats.td
index 0f19b118672df..fcd622cf70867 100644
--- a/llvm/lib/Target/M68k/M68kInstrFormats.td
+++ b/llvm/lib/Target/M68k/M68kInstrFormats.td
@@ -100,7 +100,7 @@ class MxEncFullExt<string reg_opnd, string bd_opnd, string scale_opnd,
                     string bd_decoder = "", bit size_w_l = false> {
   dag Value = (ascend (descend
      // D/A + REGISTER
-     (operand "$"#reg_opnd, 4),
+     (slice "$"#reg_opnd, 3, 0, (decoder "DecodeIndexRegister")),
      // W/L
      size_w_l,
      // SCALE
@@ -109,10 +109,7 @@ class MxEncFullExt<string reg_opnd, string bd_opnd, string scale_opnd,
      // Base Register Suppress
      0b0,
      // Index Register Suppress
-     // FIXME: the disassembler currently does not support mapping a single operand
-     // (i.e. reg_opnd in this case) to multiple bit ranges. (the REGISTER earlier
-     // and the index suppress here)
-     (operand "$"#reg_opnd, 1, (encoder "encodeIndexSuppress")),
+     (slice "$"#reg_opnd, 4, 4, (encoder "encodeIndexSuppress")),
      // Base Displacement Size
      !cond(
        !eq(bd_size, 2) : 0b10,
diff --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp
index 8adcaef99e6a3..fd05cd871b6b0 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp
@@ -236,7 +236,10 @@ void M68kMCCodeEmitter::encodeIndexSuppress(const MCInst &MI, unsigned OpIdx,
   // We suppress the index (i.e. IS = 1) if the index register is NoReg.
   const MCOperand &Op = MI.getOperand(OpIdx);
   MCRegister IndexReg = Op.getReg();
-  Value.setBitVal(/*Pos=*/0, !IndexReg.isValid());
+  // We use a special 5-bit "register" binary for index register: the [0, 3]
+  // bits carry the actual index register encoding, while bit 4 goes to the
+  // index suppress bit in the instruction.
+  Value.setBitVal(/*Pos=*/4, !IndexReg.isValid());
 }
 
 void M68kMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &Op,
diff --git a/llvm/test/MC/Disassembler/M68k/data.txt b/llvm/test/MC/Disassembler/M68k/data.txt
index 8b9a9ce0d47bd..41e82c985732d 100644
--- a/llvm/test/MC/Disassembler/M68k/data.txt
+++ b/llvm/test/MC/Disassembler/M68k/data.txt
@@ -12,6 +12,9 @@
 # CHECK: lea (0,%pc,%d0.W*8), %a0
 0x41 0xfb 0x07 0x30 0x00 0x00 0x00 0x00
 
+# CHECK: lea (70000,%pc), %a0
+0x41 0xfb 0x09 0x70 0x00 0x01 0x11 0x70
+
 # CHECK: move.w (%a2), %a0
 0x30 0x52
 



More information about the llvm-commits mailing list