From bfb180587c36b054dcf235bbb23533e81303ebbf Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Tue, 7 Mar 2023 15:33:50 -0800 Subject: [PATCH] [RISCV] Don't accept '-min', '-inf' or '-nan' in RISCVAsmParser::parseFPImm. We need to check for identifier before optionally parsing a minus sign. --- llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp | 36 +++++++++++++--------- llvm/test/MC/RISCV/zfa-invalid.s | 12 ++++++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp index e3c9e1d..a7ba82c 100644 --- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp +++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp @@ -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"); diff --git a/llvm/test/MC/RISCV/zfa-invalid.s b/llvm/test/MC/RISCV/zfa-invalid.s index d1436ec..0263d61 100644 --- a/llvm/test/MC/RISCV/zfa-invalid.s +++ b/llvm/test/MC/RISCV/zfa-invalid.s @@ -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 -- 2.7.4