[llvm] bfb1805 - [RISCV] Don't accept '-min', '-inf' or '-nan' in RISCVAsmParser::parseFPImm.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 7 15:37:10 PST 2023


Author: Craig Topper
Date: 2023-03-07T15:36:37-08:00
New Revision: bfb180587c36b054dcf235bbb23533e81303ebbf

URL: https://github.com/llvm/llvm-project/commit/bfb180587c36b054dcf235bbb23533e81303ebbf
DIFF: https://github.com/llvm/llvm-project/commit/bfb180587c36b054dcf235bbb23533e81303ebbf.diff

LOG: [RISCV] Don't accept '-min', '-inf' or '-nan' in RISCVAsmParser::parseFPImm.

We need to check for identifier before optionally parsing a minus sign.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
    llvm/test/MC/RISCV/zfa-invalid.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index e3c9e1d3f0888..a7ba82c95e5fc 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 d1436ecba5bc1..0263d61bd7f9b 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


        


More information about the llvm-commits mailing list