[RISCV] Don't accept '-min', '-inf' or '-nan' in RISCVAsmParser::parseFPImm.
authorCraig Topper <craig.topper@sifive.com>
Tue, 7 Mar 2023 23:33:50 +0000 (15:33 -0800)
committerCraig Topper <craig.topper@sifive.com>
Tue, 7 Mar 2023 23:36:37 +0000 (15:36 -0800)
We need to check for identifier before optionally parsing a minus sign.

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

index e3c9e1d..a7ba82c 100644 (file)
@@ -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");
index d1436ec..0263d61 100644 (file)
@@ -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