From: Craig Topper Date: Tue, 7 Mar 2023 23:33:50 +0000 (-0800) Subject: [RISCV] Don't accept '-min', '-inf' or '-nan' in RISCVAsmParser::parseFPImm. X-Git-Tag: upstream/17.0.6~15570 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bfb180587c36b054dcf235bbb23533e81303ebbf;p=platform%2Fupstream%2Fllvm.git [RISCV] Don't accept '-min', '-inf' or '-nan' in RISCVAsmParser::parseFPImm. We need to check for identifier before optionally parsing a minus sign. --- 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