Skip to content

Commit 1b25c0c

Browse files
authored
[RISCV] Improve assembler error message for Zcmp stack adjustment. (#129180)
Instead of referring the user to the spec, print the expected range.
1 parent c0bf4b2 commit 1b25c0c

File tree

6 files changed

+44
-50
lines changed

6 files changed

+44
-50
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,14 +1684,9 @@ bool RISCVAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
16841684
}
16851685
case Match_InvalidStackAdj: {
16861686
SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1687-
StringRef SpecName = "Zc";
1688-
if (getSTI().hasFeature(RISCV::FeatureVendorXqccmp))
1689-
SpecName = "Xqccmp";
1690-
1691-
return Error(ErrorLoc,
1692-
Twine("stack adjustment is invalid for this instruction") +
1693-
" and register list; refer to " + SpecName +
1694-
" spec for a detailed range of stack adjustment");
1687+
return Error(
1688+
ErrorLoc,
1689+
"stack adjustment is invalid for this instruction and register list");
16951690
}
16961691
}
16971692

@@ -2771,12 +2766,25 @@ ParseStatus RISCVAsmParser::parseZcmpStackAdj(OperandVector &Operands,
27712766

27722767
SMLoc S = getLoc();
27732768
int64_t StackAdjustment = getLexer().getTok().getIntVal();
2774-
unsigned Spimm = 0;
27752769
unsigned RlistVal = static_cast<RISCVOperand *>(Operands[1].get())->Rlist.Val;
27762770

2777-
if (Negative != ExpectNegative ||
2778-
!RISCVZC::getSpimm(RlistVal, Spimm, StackAdjustment, isRV64()))
2779-
return ParseStatus::NoMatch;
2771+
assert(RlistVal != RISCVZC::INVALID_RLIST);
2772+
unsigned StackAdjBase = RISCVZC::getStackAdjBase(RlistVal, isRV64());
2773+
if (Negative != ExpectNegative || StackAdjustment % 16 != 0 ||
2774+
StackAdjustment < StackAdjBase || (StackAdjustment - StackAdjBase) > 48) {
2775+
int64_t Lower = StackAdjBase;
2776+
int64_t Upper = StackAdjBase + 48;
2777+
if (ExpectNegative) {
2778+
Lower = -Lower;
2779+
Upper = -Upper;
2780+
std::swap(Lower, Upper);
2781+
}
2782+
return generateImmOutOfRangeError(S, Lower, Upper,
2783+
"stack adjustment for register list must "
2784+
"be a multiple of 16 bytes in the range");
2785+
}
2786+
2787+
unsigned Spimm = (StackAdjustment - StackAdjBase) / 16;
27802788
Operands.push_back(RISCVOperand::createSpimm(Spimm << 4, S));
27812789
getLexer().Lex();
27822790
return ParseStatus::Success;

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -637,20 +637,6 @@ inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {
637637
llvm_unreachable("Unexpected RlistVal");
638638
}
639639

640-
inline static bool getSpimm(unsigned RlistVal, unsigned &SpimmVal,
641-
int64_t StackAdjustment, bool IsRV64) {
642-
if (RlistVal == RLISTENCODE::INVALID_RLIST)
643-
return false;
644-
unsigned StackAdjBase = getStackAdjBase(RlistVal, IsRV64);
645-
StackAdjustment -= StackAdjBase;
646-
if (StackAdjustment % 16 != 0)
647-
return false;
648-
SpimmVal = StackAdjustment / 16;
649-
if (SpimmVal > 3)
650-
return false;
651-
return true;
652-
}
653-
654640
void printRlist(unsigned SlistEncode, raw_ostream &OS);
655641
} // namespace RISCVZC
656642

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ qc.cm.mva01s a1, a2
1313
# CHECK-ERROR: error: invalid register list, {ra, s0-s10} or {x1, x8-x9, x18-x26} is not supported
1414
qc.cm.popretz {ra, s0-s10}, 112
1515

16-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
16+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [16, 64]
1717
qc.cm.popretz {ra, s0-s1}, 112
1818

19-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
19+
# CHECK-ERROR: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2020
qc.cm.push {ra}, 16
2121

22-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
22+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2323
qc.cm.pushfp {ra, s0}, 16
2424

25-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
25+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [16, 64]
2626
qc.cm.pop {ra, s0-s1}, -32
2727

28-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
28+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2929
qc.cm.push {ra}, -8
3030

31-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
31+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
3232
qc.cm.pushfp {ra, s0}, -12
3333

34-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
34+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [16, 64]
3535
qc.cm.pop {ra, s0-s1}, -40

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ cm.mva01s a1, a2
1313
# CHECK-ERROR: error: invalid register list, {ra, s0-s10} or {x1, x8-x9, x18-x26} is not supported
1414
cm.popretz {ra, s0-s10}, 112
1515

16-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
16+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [16, 64]
1717
cm.popretz {ra, s0-s1}, 112
1818

19-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
19+
# CHECK-ERROR: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2020
cm.push {ra}, 16
2121

22-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
22+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [16, 64]
2323
cm.pop {ra, s0-s1}, -32
2424

25-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
25+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2626
cm.push {ra}, -8
2727

28-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
28+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [16, 64]
2929
cm.pop {ra, s0-s1}, -40

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ qc.cm.mva01s a1, a2
1313
# CHECK-ERROR: error: invalid register list, {ra, s0-s10} or {x1, x8-x9, x18-x26} is not supported
1414
qc.cm.popretz {ra, s0-s10}, 112
1515

16-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
16+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [32, 80]
1717
qc.cm.popretz {ra, s0-s1}, 112
1818

19-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
19+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2020
qc.cm.push {ra}, 16
2121

22-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
22+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2323
qc.cm.pushfp {ra, s0}, 16
2424

25-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
25+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [32, 80]
2626
qc.cm.pop {ra, s0-s1}, -32
2727

28-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
28+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2929
qc.cm.push {ra}, -15
3030

31-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
31+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
3232
qc.cm.push {ra, s0}, -15
3333

34-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Xqccmp spec for a detailed range of stack adjustment
34+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [32, 80]
3535
qc.cm.pop {ra, s0-s1}, -33

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ cm.mva01s a1, a2
1313
# CHECK-ERROR: error: invalid register list, {ra, s0-s10} or {x1, x8-x9, x18-x26} is not supported
1414
cm.popretz {ra, s0-s10}, 112
1515

16-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
16+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [32, 80]
1717
cm.popretz {ra, s0-s1}, 112
1818

19-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
19+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2020
cm.push {ra}, 16
2121

22-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
22+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [32, 80]
2323
cm.pop {ra, s0-s1}, -32
2424

25-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
25+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [-64, -16]
2626
cm.push {ra}, -15
2727

28-
# CHECK-ERROR: error: stack adjustment is invalid for this instruction and register list; refer to Zc spec for a detailed range of stack adjustment
28+
# CHECK-ERROR: error: stack adjustment for register list must be a multiple of 16 bytes in the range [32, 80]
2929
cm.pop {ra, s0-s1}, -33

0 commit comments

Comments
 (0)