Skip to content

Commit 39183be

Browse files
committed
[RISCV] Only allow 5 bit shift amounts in disassembler for RV32.
Fixes 2 old TODOs
1 parent d233fed commit 39183be

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,20 @@ 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+
327+
Inst.addOperand(MCOperand::createImm(Imm));
328+
return MCDisassembler::Success;
329+
}
330+
317331
template <unsigned N>
318332
static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm,
319333
int64_t Address,
@@ -323,6 +337,14 @@ static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm,
323337
return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
324338
}
325339

340+
static DecodeStatus
341+
decodeUImmLog2XLenNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address,
342+
const MCDisassembler *Decoder) {
343+
if (Imm == 0)
344+
return MCDisassembler::Fail;
345+
return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
346+
}
347+
326348
template <unsigned N>
327349
static DecodeStatus decodeSImmOperand(MCInst &Inst, uint32_t Imm,
328350
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)