Skip to content

Commit 31944ac

Browse files
authored
[RISCV] Render P-ext simm10_unsigned as a simm10 after parsing. (#148749)
Instead of allowing a parsed MCInst to have a either uimm10 or simm10, always render as simm10. This avoids a mismatch between parsed MCInst and disassembled MCInst when a uimm10 value is used.
1 parent 3265a36 commit 31944ac

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,14 @@ struct RISCVOperand final : public MCParsedAsmOperand {
11991199
addExpr(Inst, getImm(), isRV64Imm());
12001200
}
12011201

1202+
void addSImm10UnsignedOperands(MCInst &Inst, unsigned N) const {
1203+
assert(N == 1 && "Invalid number of operands!");
1204+
int64_t Imm;
1205+
[[maybe_unused]] bool IsConstant = evaluateConstantImm(getImm(), Imm);
1206+
assert(IsConstant);
1207+
Inst.addOperand(MCOperand::createImm(SignExtend64<10>(Imm)));
1208+
}
1209+
12021210
void addFPImmOperands(MCInst &Inst, unsigned N) const {
12031211
assert(N == 1 && "Invalid number of operands!");
12041212
if (isImm()) {

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ enum OperandType : unsigned {
339339
OPERAND_SIMM6,
340340
OPERAND_SIMM6_NONZERO,
341341
OPERAND_SIMM10,
342-
OPERAND_SIMM10_UNSIGNED,
343342
OPERAND_SIMM10_LSB0000_NONZERO,
344343
OPERAND_SIMM11,
345344
OPERAND_SIMM12,

llvm/lib/Target/RISCV/RISCVInstrInfoP.td

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@
2020

2121
def simm10 : RISCVSImmLeafOp<10>;
2222

23+
def SImm10UnsignedAsmOperand : SImmAsmOperand<10, "Unsigned"> {
24+
let RenderMethod = "addSImm10UnsignedOperands";
25+
}
26+
2327
// A 10-bit signed immediate allowing range [-512, 1023]
24-
// but will decode to [-512, 511].
28+
// but represented as [-512, 511].
2529
def simm10_unsigned : RISCVOp {
26-
let ParserMatchClass = SImmAsmOperand<10, "Unsigned">;
30+
let ParserMatchClass = SImm10UnsignedAsmOperand;
2731
let EncoderMethod = "getImmOpValue";
2832
let DecoderMethod = "decodeSImmOperand<10>";
29-
let OperandType = "OPERAND_SIMM10_UNSIGNED";
33+
let OperandType = "OPERAND_SIMM10";
3034
let MCOperandPredicate = [{
3135
int64_t Imm;
3236
if (!MCOp.evaluateAsConstantImm(Imm))
3337
return false;
34-
return isInt<10>(Imm) || isUInt<10>(Imm);
38+
return isInt<10>(Imm);
3539
}];
3640
}
3741

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: | FileCheck -check-prefixes=CHECK-ASM,CHECK-ASM-AND-OBJ %s
33
# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+experimental-p < %s \
44
# RUN: | llvm-objdump --mattr=+experimental-p -M no-aliases -d -r --no-print-imm-hex - \
5-
# RUN: | FileCheck --check-prefixes=CHECK-ASM-AND-OBJ,CHECK-OBJ %s
5+
# RUN: | FileCheck --check-prefixes=CHECK-ASM-AND-OBJ %s
66

77
# CHECK-ASM-AND-OBJ: clz a0, a1
88
# CHECK-ASM: encoding: [0x13,0x95,0x05,0x60]
@@ -73,7 +73,6 @@ psabs.b t0, t1
7373
# CHECK-ASM-AND-OBJ: plui.h gp, 32
7474
# CHECK-ASM: encoding: [0x9b,0x21,0x20,0xf0]
7575
plui.h gp, 32
76-
# CHECK-OBJ: plui.h gp, -412
77-
# CHECK-ASM: plui.h gp, 612
76+
# CHECK-ASM-AND-OBJ: plui.h gp, -412
7877
# CHECK-ASM: encoding: [0x9b,0xa1,0x64,0xf0]
7978
plui.h gp, 612

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: | FileCheck -check-prefixes=CHECK-ASM,CHECK-ASM-AND-OBJ %s
33
# RUN: llvm-mc -filetype=obj --triple=riscv64 -mattr=+experimental-p < %s \
44
# RUN: | llvm-objdump --triple=riscv64 --mattr=+experimental-p -M no-aliases --no-print-imm-hex -d -r - \
5-
# RUN: | FileCheck --check-prefixes=CHECK-ASM-AND-OBJ,CHECK-OBJ %s
5+
# RUN: | FileCheck --check-prefixes=CHECK-ASM-AND-OBJ %s
66

77
# CHECK-ASM-AND-OBJ: clz a0, a1
88
# CHECK-ASM: encoding: [0x13,0x95,0x05,0x60]
@@ -97,14 +97,12 @@ psabs.b a0, s2
9797
# CHECK-ASM-AND-OBJ: plui.h s2, 4
9898
# CHECK-ASM: encoding: [0x1b,0x29,0x04,0xf0]
9999
plui.h s2, 4
100-
# CHECK-OBJ: plui.h gp, -412
101-
# CHECK-ASM: plui.h gp, 612
100+
# CHECK-ASM-AND-OBJ: plui.h gp, -412
102101
# CHECK-ASM: encoding: [0x9b,0xa1,0x64,0xf0]
103102
plui.h gp, 612
104103
# CHECK-ASM-AND-OBJ: plui.w a2, 1
105104
# CHECK-ASM: encoding: [0x1b,0x26,0x01,0xf2]
106105
plui.w a2, 1
107-
# CHECK-OBJ: plui.w a2, -1
108-
# CHECK-ASM: plui.w a2, 1023
106+
# CHECK-ASM-AND-OBJ: plui.w a2, -1
109107
# CHECK-ASM: encoding: [0x1b,0xa6,0xff,0xf3]
110108
plui.w a2, 1023

0 commit comments

Comments
 (0)