[llvm] [RISCV] Don't allow mixing ABI and non-ABI register names when parsing register list. (PR #134210)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 3 00:17:28 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mc

Author: Craig Topper (topperc)

<details>
<summary>Changes</summary>

This is the last parsing bug I currently know about. After this I'm going to start refactoring the code and writing better error messages using all the recently added invalid tests.

---
Full diff: https://github.com/llvm/llvm-project/pull/134210.diff


3 Files Affected:

- (modified) llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp (+19-5) 
- (modified) llvm/test/MC/RISCV/rv32zcmp-invalid.s (+7-4) 
- (modified) llvm/test/MC/RISCV/rv64zcmp-invalid.s (+31-4) 


``````````diff
diff --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index d90d1dda07081..189c81268256e 100644
--- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
@@ -2587,6 +2587,7 @@ ParseStatus RISCVAsmParser::parseRegListCommon(OperandVector &Operands,
     return Error(getLoc(), "register list must start from 'ra' or 'x1'");
 
   StringRef RegName = getLexer().getTok().getIdentifier();
+  bool XRegs = RegName[0] == 'x';
   MCRegister RegEnd = matchRegisterNameHelper(RegName);
   if (RegEnd != RISCV::X1)
     return Error(getLoc(), "register list must start from 'ra' or 'x1'");
@@ -2597,6 +2598,9 @@ ParseStatus RISCVAsmParser::parseRegListCommon(OperandVector &Operands,
     if (getLexer().isNot(AsmToken::Identifier))
       return Error(getLoc(), "invalid register");
     StringRef RegName = getLexer().getTok().getIdentifier();
+    // If 'x1' was used intead of 'ra', this must be 'x8' and not 's0'.
+    if (XRegs != (RegName[0] == 'x'))
+      return Error(getLoc(), "invalid register");
     RegEnd = matchRegisterNameHelper(RegName);
     if (!RegEnd)
       return Error(getLoc(), "invalid register");
@@ -2608,10 +2612,14 @@ ParseStatus RISCVAsmParser::parseRegListCommon(OperandVector &Operands,
     // parse case like -s1
     if (parseOptionalToken(AsmToken::Minus)) {
       StringRef EndName = getLexer().getTok().getIdentifier();
-      // FIXME: the register mapping and checks of RVE is wrong
+      // If 'x1' and 'x8' were used, this must also start with 'x'.
+      if (XRegs != (EndName[0] == 'x'))
+        return Error(getLoc(), "invalid register");
       RegEnd = matchRegisterNameHelper(EndName);
+      // If 'x8' was used instead of 's0', this must be 'x9' and can't be
+      // 'x18'-'x27'.
       if (!(RegEnd == RISCV::X9 ||
-            (RegEnd >= RISCV::X18 && RegEnd <= RISCV::X27)))
+            (!XRegs && RegEnd >= RISCV::X18 && RegEnd <= RISCV::X27)))
         return Error(getLoc(), "invalid register");
       getLexer().Lex();
     }
@@ -2624,10 +2632,13 @@ ParseStatus RISCVAsmParser::parseRegListCommon(OperandVector &Operands,
             "first contiguous registers pair of register list must be 'x8-x9'");
 
       // parse ', x18' for extra part
-      if (getLexer().isNot(AsmToken::Identifier) || IsRVE)
+      if (getLexer().isNot(AsmToken::Identifier) || IsRVE || !XRegs)
         return Error(getLoc(), "invalid register");
       StringRef EndName = getLexer().getTok().getIdentifier();
-      RegEnd = MatchRegisterName(EndName);
+      // If 'x1' and 'x8' were used, this must also start with 'x'.
+      if (EndName[0] != 'x')
+        return Error(getLoc(), "invalid register");
+      RegEnd = matchRegisterNameHelper(EndName);
       if (RegEnd != RISCV::X18)
         return Error(getLoc(),
                      "second contiguous registers pair of register list "
@@ -2639,7 +2650,10 @@ ParseStatus RISCVAsmParser::parseRegListCommon(OperandVector &Operands,
         if (getLexer().isNot(AsmToken::Identifier) || IsRVE)
           return Error(getLoc(), "invalid register");
         EndName = getLexer().getTok().getIdentifier();
-        RegEnd = MatchRegisterName(EndName);
+        // If 'x1' and 'x8' were used, this must also start with 'x'.
+        if (EndName[0] != 'x')
+          return Error(getLoc(), "invalid register");
+        RegEnd = matchRegisterNameHelper(EndName);
         if (!(RegEnd >= RISCV::X19 && RegEnd <= RISCV::X27))
           return Error(getLoc(), "invalid register");
         getLexer().Lex();
diff --git a/llvm/test/MC/RISCV/rv32zcmp-invalid.s b/llvm/test/MC/RISCV/rv32zcmp-invalid.s
index c41cc35a8f8ee..e970331d1e1a9 100644
--- a/llvm/test/MC/RISCV/rv32zcmp-invalid.s
+++ b/llvm/test/MC/RISCV/rv32zcmp-invalid.s
@@ -35,13 +35,16 @@ cm.pop {ra, t1}, -40
 cm.pop {ra, s0-t1}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:20: error: second contiguous registers pair of register list must start from 'x18'
-cm.pop {ra, x8-x9, x28}, -40
+cm.pop {x1, x8-x9, x28}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:24: error: invalid register
-cm.pop {ra, x8-x9, x18-x28}, -40
+cm.pop {x1, x8-x9, x18-x28}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:24: error: invalid register
-cm.pop {ra, x8-x9, x18-x17}, -40
+cm.pop {x1, x8-x9, x18-x17}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:16: error: invalid register
-cm.pop {ra, x8-f8, x18-x17}, -40
+cm.pop {x1, x8-f8, x18-x17}, -40
+
+# CHECK-ERROR: :[[@LINE+1]]:16: error: invalid register
+cm.pop {ra, s0-f8}, -40
diff --git a/llvm/test/MC/RISCV/rv64zcmp-invalid.s b/llvm/test/MC/RISCV/rv64zcmp-invalid.s
index c66415cb49b34..29cd406eaaaab 100644
--- a/llvm/test/MC/RISCV/rv64zcmp-invalid.s
+++ b/llvm/test/MC/RISCV/rv64zcmp-invalid.s
@@ -38,16 +38,16 @@ cm.pop {ra, t1}, -40
 cm.pop {ra, s0-t1}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:20: error: second contiguous registers pair of register list must start from 'x18'
-cm.pop {ra, x8-x9, x28}, -40
+cm.pop {x1, x8-x9, x28}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:24: error: invalid register
-cm.pop {ra, x8-x9, x18-x28}, -40
+cm.pop {x1, x8-x9, x18-x28}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:24: error: invalid register
-cm.pop {ra, x8-x9, x18-x17}, -40
+cm.pop {x1, x8-x9, x18-x17}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:16: error: invalid register
-cm.pop {ra, x8-f8, x18-x17}, -40
+cm.pop {x1, x8-f8, x18-x17}, -40
 
 # CHECK-ERROR: :[[@LINE+1]]:15: error: stack adjustment is invalid for this instruction and register list
 cm.pop {ra}, -x1
@@ -57,3 +57,30 @@ cm.push {ra}, x1
 
 # CHECK-ERROR: :[[@LINE+1]]:12: error: register list must end with '}'
 cm.push {x1-x9}, -32
+
+# CHECK-ERROR: :[[@LINE+1]]:13: error: invalid register
+cm.push {x1,s0}
+
+# CHECK-ERROR: :[[@LINE+1]]:13: error: invalid register
+cm.push {ra,x8}
+
+# CHECK-ERROR: :[[@LINE+1]]:16: error: invalid register
+cm.push {x1,x8-s1}
+
+# CHECK-ERROR: :[[@LINE+1]]:16: error: invalid register
+cm.push {ra,s0-x9}
+
+# CHECK-ERROR: :[[@LINE+1]]:16: error: invalid register
+cm.push {x1,x8-x18}
+
+# CHECK-ERROR: :[[@LINE+1]]:19: error: invalid register
+cm.push {x1,x8-x9,s2}
+
+# CHECK-ERROR: :[[@LINE+1]]:19: error: invalid register
+cm.push {ra,s0-s1,x18}
+
+# CHECK-ERROR: :[[@LINE+1]]:19: error: invalid register
+cm.push {ra,s0-s1,s2}
+
+# CHECK-ERROR: :[[@LINE+1]]:23: error: invalid register
+cm.push {x1,x8-x9,x18-s3}

``````````

</details>


https://github.com/llvm/llvm-project/pull/134210


More information about the llvm-commits mailing list