Skip to content

Commit 4de8e56

Browse files
jrtc27tstellar
authored andcommitted
[RISCV] Fix parseBareSymbol to not double-parse top-level operators
By failing to lex the token we end up both parsing it as a binary operator ourselves and parsing it as a unary operator when calling parseExpression on the RHS. For plus this is harmless but for minus this parses "foo - 4" as "foo - -4", effectively treating a top-level minus as a plus. Fixes #54105 Reviewed By: asb, MaskRay Differential Revision: https://reviews.llvm.org/D120635 (cherry picked from commit 6aa8521)
1 parent b29813f commit 4de8e56

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,9 +1607,11 @@ OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
16071607
return MatchOperand_Success;
16081608
case AsmToken::Plus:
16091609
Opcode = MCBinaryExpr::Add;
1610+
getLexer().Lex();
16101611
break;
16111612
case AsmToken::Minus:
16121613
Opcode = MCBinaryExpr::Sub;
1614+
getLexer().Lex();
16131615
break;
16141616
}
16151617

llvm/test/MC/RISCV/rvi-pseudos.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,9 @@ sw a3, zero, a4
187187
# CHECK: auipc a5, %pcrel_hi((255+a_symbol)-4)
188188
# CHECK: addi a5, a5, %pcrel_lo(.Lpcrel_hi30)
189189
lla a5, (0xFF + a_symbol) - 4
190+
191+
## Check that we don't double-parse a top-level minus.
192+
# CHECK: .Lpcrel_hi31:
193+
# CHECK: auipc a5, %pcrel_hi(a_symbol-4)
194+
# CHECK: addi a5, a5, %pcrel_lo(.Lpcrel_hi31)
195+
lla a5, a_symbol - 4

0 commit comments

Comments
 (0)