Skip to content

Commit bde3d4a

Browse files
authored
[RISCV] Only allow 5 bit shift amounts in disassembler for RV32. (#115432)
Fixes 2 old TODOs
1 parent 92e0fb0 commit bde3d4a

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,19 @@ static DecodeStatus decodeUImmOperand(MCInst &Inst, uint32_t Imm,
314314
return MCDisassembler::Success;
315315
}
316316

317+
static DecodeStatus decodeUImmLog2XLenOperand(MCInst &Inst, uint32_t Imm,
318+
int64_t Address,
319+
const MCDisassembler *Decoder) {
320+
assert(isUInt<6>(Imm) && "Invalid immediate");
321+
322+
if (!Decoder->getSubtargetInfo().hasFeature(RISCV::Feature64Bit) &&
323+
!isUInt<5>(Imm))
324+
return MCDisassembler::Fail;
325+
326+
Inst.addOperand(MCOperand::createImm(Imm));
327+
return MCDisassembler::Success;
328+
}
329+
317330
template <unsigned N>
318331
static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm,
319332
int64_t Address,
@@ -323,6 +336,14 @@ static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm,
323336
return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
324337
}
325338

339+
static DecodeStatus
340+
decodeUImmLog2XLenNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address,
341+
const MCDisassembler *Decoder) {
342+
if (Imm == 0)
343+
return MCDisassembler::Fail;
344+
return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
345+
}
346+
326347
template <unsigned N>
327348
static DecodeStatus decodeSImmOperand(MCInst &Inst, uint32_t Imm,
328349
int64_t Address,

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ def uimmlog2xlen : RISCVOp, ImmLeaf<XLenVT, [{
201201
return isUInt<5>(Imm);
202202
}]> {
203203
let ParserMatchClass = UImmLog2XLenAsmOperand;
204-
// TODO: should ensure invalid shamt is rejected when decoding.
205-
let DecoderMethod = "decodeUImmOperand<6>";
204+
let DecoderMethod = "decodeUImmLog2XLenOperand";
206205
let MCOperandPredicate = [{
207206
int64_t Imm;
208207
if (!MCOp.evaluateAsConstantImm(Imm))

llvm/lib/Target/RISCV/RISCVInstrInfoC.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def uimmlog2xlennonzero : RISCVOp, ImmLeaf<XLenVT, [{
2424
return isUInt<5>(Imm) && (Imm != 0);
2525
}]> {
2626
let ParserMatchClass = UImmLog2XLenNonZeroAsmOperand;
27-
// TODO: should ensure invalid shamt is rejected when decoding.
28-
let DecoderMethod = "decodeUImmNonZeroOperand<6>";
27+
let DecoderMethod = "decodeUImmLog2XLenNonZeroOperand";
2928
let OperandType = "OPERAND_UIMMLOG2XLEN_NONZERO";
3029
let MCOperandPredicate = [{
3130
int64_t Imm;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# RUN: not llvm-mc -disassemble -triple=riscv32 -mattr=+c < %s 2>&1 | FileCheck %s --check-prefix=RV32
2+
# RUN: llvm-mc -disassemble -triple=riscv64 -mattr=+c < %s 2>&1 | FileCheck %s --check-prefix=RV64
3+
4+
[0x13,0x9b,0xdb,0x02]
5+
# RV32: warning: invalid instruction encoding
6+
# RV64: slli s6, s7, 45
7+
8+
[0xfd,0x92]
9+
# RV32: warning: invalid instruction encoding
10+
# RV64: srli a3, a3, 63

0 commit comments

Comments
 (0)