Skip to content

Commit 14f03a7

Browse files
committed
fixup! Accept [-128,255]
1 parent 7a053f3 commit 14f03a7

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,6 @@ struct RISCVOperand final : public MCParsedAsmOperand {
808808

809809
bool isSImm5() const { return isSImm<5>(); }
810810
bool isSImm6() const { return isSImm<6>(); }
811-
bool isSImm8() const { return isSImm<8>(); }
812811
bool isSImm10() const { return isSImm<10>(); }
813812
bool isSImm11() const { return isSImm<11>(); }
814813
bool isSImm16() const { return isSImm<16>(); }
@@ -904,6 +903,7 @@ struct RISCVOperand final : public MCParsedAsmOperand {
904903
VK == RISCV::S_QC_ABS20;
905904
}
906905

906+
bool isSImm8Unsigned() const { return isSImm<8>() || isUImm<8>(); }
907907
bool isSImm10Unsigned() const { return isSImm<10>() || isUImm<10>(); }
908908

909909
bool isUImm20LUI() const {
@@ -1200,6 +1200,14 @@ struct RISCVOperand final : public MCParsedAsmOperand {
12001200
addExpr(Inst, getImm(), isRV64Imm());
12011201
}
12021202

1203+
void addSImm8UnsignedOperands(MCInst &Inst, unsigned N) const {
1204+
assert(N == 1 && "Invalid number of operands!");
1205+
int64_t Imm;
1206+
[[maybe_unused]] bool IsConstant = evaluateConstantImm(getImm(), Imm);
1207+
assert(IsConstant);
1208+
Inst.addOperand(MCOperand::createImm(SignExtend64<8>(Imm)));
1209+
}
1210+
12031211
void addSImm10UnsignedOperands(MCInst &Inst, unsigned N) const {
12041212
assert(N == 1 && "Invalid number of operands!");
12051213
int64_t Imm;
@@ -1548,9 +1556,9 @@ bool RISCVAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
15481556
return generateImmOutOfRangeError(
15491557
Operands, ErrorInfo, 0, (1 << 9) - 8,
15501558
"immediate must be a multiple of 8 bytes in the range");
1551-
case Match_InvalidSImm8:
1559+
case Match_InvalidSImm8Unsigned:
15521560
return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 7),
1553-
(1 << 7) - 1);
1561+
(1 << 8) - 1);
15541562
case Match_InvalidSImm10:
15551563
return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 9),
15561564
(1 << 9) - 1);

llvm/lib/Target/RISCV/RISCVInstrInfoP.td

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,27 @@
1818
// Operand and SDNode transformation definitions.
1919
//===----------------------------------------------------------------------===//
2020

21-
def simm8 : RISCVSImmOp<8>;
2221
def simm10 : RISCVSImmOp<10>;
2322

23+
def SImm8UnsignedAsmOperand : SImmAsmOperand<8, "Unsigned"> {
24+
let RenderMethod = "addSImm8UnsignedOperands";
25+
}
26+
27+
// A 8-bit signed immediate allowing range [-128, 255]
28+
// but represented as [-128, 255].
29+
def simm8_unsigned : RISCVOp {
30+
let ParserMatchClass = SImm8UnsignedAsmOperand;
31+
let EncoderMethod = "getImmOpValue";
32+
let DecoderMethod = "decodeSImmOperand<8>";
33+
let OperandType = "OPERAND_SIMM10";
34+
let MCOperandPredicate = [{
35+
int64_t Imm;
36+
if (!MCOp.evaluateAsConstantImm(Imm))
37+
return false;
38+
return isInt<8>(Imm);
39+
}];
40+
}
41+
2442
def SImm10UnsignedAsmOperand : SImmAsmOperand<10, "Unsigned"> {
2543
let RenderMethod = "addSImm10UnsignedOperands";
2644
}
@@ -76,8 +94,8 @@ class PLUI_i<bits<7> funct7, string opcodestr>
7694

7795
let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
7896
class PLI_B_i<bits<8> funct8, string opcodestr>
79-
: RVInst<(outs GPR:$rd), (ins simm8:$imm8), opcodestr, "$rd, $imm8", [],
80-
InstFormatOther> {
97+
: RVInst<(outs GPR:$rd), (ins simm8_unsigned:$imm8), opcodestr,
98+
"$rd, $imm8", [], InstFormatOther> {
8199
bits<8> imm8;
82100
bits<5> rd;
83101

llvm/test/MC/RISCV/rv32p-invalid.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Imm overflow
55
pli.h a0, 0x400 # CHECK: :[[@LINE]]:11: error: immediate must be an integer in the range [-512, 511]
66
plui.h a1, 0x400 # CHECK: :[[@LINE]]:12: error: immediate must be an integer in the range [-512, 1023]
7-
pli.b a0, 0x200 # CHECK: :[[@LINE]]:11: error: immediate must be an integer in the range [-128, 127]
7+
pli.b a0, 0x200 # CHECK: :[[@LINE]]:11: error: immediate must be an integer in the range [-128, 255]
88

99
pslli.b a6, a7, 100 # CHECK: :[[@LINE]]:17: error: immediate must be an integer in the range [0, 7]
1010
pslli.h ra, sp, 100 # CHECK: :[[@LINE]]:17: error: immediate must be an integer in the range [0, 15]

llvm/test/MC/RISCV/rv64p-valid.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ pli.b a6, 6
8282
# CHECK-ASM-AND-OBJ: pli.b a6, -1
8383
# CHECK-ASM: encoding: [0x1b,0x28,0xff,0xb4]
8484
pli.b a6, -1
85+
# CHECK-ASM-AND-OBJ: pli.b a6, -1
86+
# CHECK-ASM: encoding: [0x1b,0x28,0xff,0xb4]
87+
pli.b a6, 255
8588
# CHECK-ASM-AND-OBJ: psext.h.b t3, a2
8689
# CHECK-ASM: encoding: [0x1b,0x2e,0x46,0xe0]
8790
psext.h.b t3, a2

0 commit comments

Comments
 (0)