Skip to content

Commit ea06f7f

Browse files
authored
[RISCV] For RV32C, disassembly of c.slli should fail when immediate > 31 (#133713)
Fixes #133712. The change causes `c.slli` instructions whose immediate has bit 5 set to be rejected when disassembling RV32C. Added a test to exhaustively cover c.slli for 32 bit targets. A minor tweak to make the debug output a little more readable. The spec. (20240411) says: > For RV32C, shamt[5] must be zero; the code points with shamt[5]=1 are designated for custom extensions. For RV32C and RV64C, the shift amount must be non-zero; the code points with shamt=0 are HINTs. For all base ISAs, the code points with rd=x0 are HINTs, except those with shamt[5]=1 in RV32C.
1 parent 799e905 commit ea06f7f

File tree

2 files changed

+3127
-4
lines changed

2 files changed

+3127
-4
lines changed

llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,12 @@ static DecodeStatus decodeRVCInstrRdRs1UImm(MCInst &Inst, uint32_t Insn,
558558
const MCDisassembler *Decoder) {
559559
Inst.addOperand(MCOperand::createReg(RISCV::X0));
560560
Inst.addOperand(Inst.getOperand(0));
561-
uint32_t UImm6 =
562-
fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5);
561+
562+
uint32_t UImm6 = fieldFromInstruction(Insn, 12, 1) << 5;
563+
// On RV32C, uimm[5]=1 is reserved for custom extensions.
564+
if (UImm6 != 0 && Decoder->getSubtargetInfo().hasFeature(RISCV::Feature32Bit))
565+
return MCDisassembler::Fail;
566+
UImm6 |= fieldFromInstruction(Insn, 2, 5);
563567
[[maybe_unused]] DecodeStatus Result =
564568
decodeUImmOperand<6>(Inst, UImm6, Address, Decoder);
565569
assert(Result == MCDisassembler::Success && "Invalid immediate");
@@ -784,7 +788,7 @@ DecodeStatus RISCVDisassembler::getInstruction16(MCInst &MI, uint64_t &Size,
784788
if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
785789
continue;
786790

787-
LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << "table:\n");
791+
LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
788792
DecodeStatus Result =
789793
decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
790794
if (Result == MCDisassembler::Fail)
@@ -820,7 +824,7 @@ DecodeStatus RISCVDisassembler::getInstruction48(MCInst &MI, uint64_t &Size,
820824
if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
821825
continue;
822826

823-
LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << "table:\n");
827+
LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
824828
DecodeStatus Result =
825829
decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
826830
if (Result == MCDisassembler::Fail)

0 commit comments

Comments
 (0)