Skip to content

Commit

Permalink
[RISCV] Don't accept '-min', '-inf' or '-nan' in RISCVAsmParser::pars…
Browse files Browse the repository at this point in the history
…eFPImm.

We need to check for identifier before optionally parsing a minus sign.
  • Loading branch information
topperc committed Mar 7, 2023
1 parent f6e7a5c commit bfb1805
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
36 changes: 21 additions & 15 deletions llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
Expand Up @@ -1563,34 +1563,40 @@ RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
OperandMatchResultTy RISCVAsmParser::parseFPImm(OperandVector &Operands) {
SMLoc S = getLoc();

// Handle negation, as that still comes through as a separate token.
bool IsNegative = parseOptionalToken(AsmToken::Minus);

const AsmToken &Tok = getTok();
if (!Tok.is(AsmToken::Real) && !Tok.is(AsmToken::Integer) &&
!Tok.is(AsmToken::Identifier)) {
TokError("invalid floating point immediate");
return MatchOperand_ParseFail;
}

// Parse special floats (inf/nan/min) representation.
if (Tok.is(AsmToken::Identifier)) {
if (Tok.getString().compare_insensitive("inf") == 0) {
if (getTok().is(AsmToken::Identifier)) {
StringRef Identifier = getTok().getIdentifier();
if (Identifier.compare_insensitive("inf") == 0) {
APFloat SpecialVal = APFloat::getInf(APFloat::IEEEsingle());
Operands.push_back(RISCVOperand::createFPImm(
SpecialVal.bitcastToAPInt().getZExtValue(), S));
} else if (Tok.getString().compare_insensitive("nan") == 0) {
} else if (Identifier.compare_insensitive("nan") == 0) {
APFloat SpecialVal = APFloat::getNaN(APFloat::IEEEsingle());
Operands.push_back(RISCVOperand::createFPImm(
SpecialVal.bitcastToAPInt().getZExtValue(), S));
} else if (Tok.getString().compare_insensitive("min") == 0) {
} else if (Identifier.compare_insensitive("min") == 0) {
unsigned SpecialVal = RISCVLoadFPImm::getFPImm(1);
Operands.push_back(RISCVOperand::createFPImm(SpecialVal, S));
} else {
TokError("invalid floating point literal");
return MatchOperand_ParseFail;
}
} else if (Tok.is(AsmToken::Integer)) {

Lex(); // Eat the token.

return MatchOperand_Success;
}

// Handle negation, as that still comes through as a separate token.
bool IsNegative = parseOptionalToken(AsmToken::Minus);

const AsmToken &Tok = getTok();
if (!Tok.is(AsmToken::Real) && !Tok.is(AsmToken::Integer)) {
TokError("invalid floating point immediate");
return MatchOperand_ParseFail;
}

if (Tok.is(AsmToken::Integer)) {
// Parse integer representation.
if (Tok.getIntVal() > 31 || IsNegative) {
TokError("encoded floating point value out of range");
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/MC/RISCV/zfa-invalid.s
Expand Up @@ -34,3 +34,15 @@ fli.d ft1, 3.560000e+02
# CHECK-NO-RV64: error: operand must be a valid floating-point constant
# CHECK-NO-RV32: error: operand must be a valid floating-point constant
fli.h ft1, 1.600000e+00

# CHECK-NO-RV64: error: invalid floating point immediate
# CHECK-NO-RV32: error: invalid floating point immediate
fli.s ft1, -min

# CHECK-NO-RV64: error: invalid floating point immediate
# CHECK-NO-RV32: error: invalid floating point immediate
fli.s ft1, -inf

# CHECK-NO-RV64: error: invalid floating point immediate
# CHECK-NO-RV32: error: invalid floating point immediate
fli.s ft1, -nan

0 comments on commit bfb1805

Please sign in to comment.