[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 https://github.com/llvm/llvm-project/issues/54105

Reviewed By: asb, MaskRay

Differential Revision: https://reviews.llvm.org/D120635
This commit is contained in:
Jessica Clarke 2022-02-27 20:48:52 +00:00
parent 87e6251d66
commit 6aa8521fdb
2 changed files with 8 additions and 0 deletions

View File

@ -1613,9 +1613,11 @@ OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
return MatchOperand_Success;
case AsmToken::Plus:
Opcode = MCBinaryExpr::Add;
getLexer().Lex();
break;
case AsmToken::Minus:
Opcode = MCBinaryExpr::Sub;
getLexer().Lex();
break;
}

View File

@ -187,3 +187,9 @@ sw a3, zero, a4
# CHECK: auipc a5, %pcrel_hi((255+a_symbol)-4)
# CHECK: addi a5, a5, %pcrel_lo(.Lpcrel_hi30)
lla a5, (0xFF + a_symbol) - 4
## Check that we don't double-parse a top-level minus.
# CHECK: .Lpcrel_hi31:
# CHECK: auipc a5, %pcrel_hi(a_symbol-4)
# CHECK: addi a5, a5, %pcrel_lo(.Lpcrel_hi31)
lla a5, a_symbol - 4