[llvm] 3f1e7ef - [LoongArch] Support parsing register names in CFI instructions

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 18:20:56 PST 2024


Author: wanglei
Date: 2024-11-22T10:20:52+08:00
New Revision: 3f1e7ef5344c3236bcabf3982dbdc985c43bc078

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

LOG: [LoongArch] Support parsing register names in CFI instructions

Reviewed By: MQ-mengqing, heiher, xen0n

Pull Request: https://github.com/llvm/llvm-project/pull/117120

Added: 
    

Modified: 
    llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
    llvm/test/MC/LoongArch/Directives/cfi.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp b/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
index 96eb8b1b0528ac..efc8b77f8d8fab 100644
--- a/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
+++ b/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
@@ -669,13 +669,33 @@ static bool matchRegisterNameHelper(MCRegister &RegNo, StringRef Name) {
 
 bool LoongArchAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
                                        SMLoc &EndLoc) {
-  return Error(getLoc(), "invalid register number");
+  if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
+    return Error(getLoc(), "invalid register name");
+
+  if (!LoongArchMCRegisterClasses[LoongArch::GPRRegClassID].contains(Reg) &&
+      !LoongArchMCRegisterClasses[LoongArch::FPR32RegClassID].contains(Reg))
+    return Error(getLoc(), "invalid register name");
+
+  return false;
 }
 
 ParseStatus LoongArchAsmParser::tryParseRegister(MCRegister &Reg,
                                                  SMLoc &StartLoc,
                                                  SMLoc &EndLoc) {
-  llvm_unreachable("Unimplemented function.");
+  const AsmToken &Tok = getParser().getTok();
+  StartLoc = Tok.getLoc();
+  EndLoc = Tok.getEndLoc();
+
+  parseOptionalToken(AsmToken::Dollar);
+  if (getLexer().getKind() != AsmToken::Identifier)
+    return ParseStatus::NoMatch;
+
+  StringRef Name = Tok.getIdentifier();
+  if (matchRegisterNameHelper(Reg, Name))
+    return ParseStatus::NoMatch;
+
+  getParser().Lex(); // Eat identifier token.
+  return ParseStatus::Success;
 }
 
 bool LoongArchAsmParser::classifySymbolRef(const MCExpr *Expr,

diff  --git a/llvm/test/MC/LoongArch/Directives/cfi.s b/llvm/test/MC/LoongArch/Directives/cfi.s
index 7101fc90729067..978028dfd66e6d 100644
--- a/llvm/test/MC/LoongArch/Directives/cfi.s
+++ b/llvm/test/MC/LoongArch/Directives/cfi.s
@@ -1,10 +1,10 @@
 ## Test cfi directives.
 
-# RUN: llvm-mc %s --triple=loongarch32 | FileCheck %s
-# RUN: llvm-mc %s --triple=loongarch64 | FileCheck %s
-# RUN: not llvm-mc --triple=loongarch32 --defsym=ERR=1 < %s 2>&1 \
+# RUN: llvm-mc %s --triple=loongarch32 --mattr=+lasx | FileCheck %s
+# RUN: llvm-mc %s --triple=loongarch64 --mattr=+lasx | FileCheck %s
+# RUN: not llvm-mc --triple=loongarch32 --mattr=+lasx --defsym=ERR=1 < %s 2>&1 \
 # RUN:     | FileCheck %s --check-prefix=CHECK-ERR
-# RUN: not llvm-mc --triple=loongarch64 --defsym=ERR=1 < %s 2>&1 \
+# RUN: not llvm-mc --triple=loongarch64 --mattr=+lasx --defsym=ERR=1 < %s 2>&1 \
 # RUN:     | FileCheck %s --check-prefix=CHECK-ERR
 
 # CHECK: .cfi_startproc
@@ -15,20 +15,36 @@
 .cfi_offset 9, 8
 # CHECK-NEXT: .cfi_offset 31, 16
 .cfi_offset 31, 16
+# CHECK-NEXT: .cfi_offset 22, -8
+.cfi_offset r22, -8
+# CHECK-NEXT: .cfi_offset 22, -8
+.cfi_offset $r22, -8
+# CHECK-NEXT: .cfi_offset 22, -8
+.cfi_offset fp, -8
+# CHECK-NEXT: .cfi_offset 22, -8
+.cfi_offset $fp, -8
+# CHECK-NEXT: .cfi_offset 42, 8
+.cfi_offset f10, 8
+# CHECK-NEXT: .cfi_offset 56, 8
+.cfi_offset fs0, 8
 # CHECK-NEXT: .cfi_endproc
 .cfi_endproc
 
 .ifdef ERR
 .cfi_startproc
-# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register number
+# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register name
 .cfi_offset -22, -8
-# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register number
-.cfi_offset fp, -8
-# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register number
+# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register name
+.cfi_offset lr, -8
+# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register name
+.cfi_offset r32, -8
+# CHECK-ERR: :[[#@LINE+1]]:14: error: invalid register name
+.cfi_offset $r32, -8
+# CHECK-ERR: :[[#@LINE+1]]:14: error: invalid register name
 .cfi_offset $22, -8
-# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register number
-.cfi_offset $r22, -8
-# CHECK-ERR: :[[#@LINE+1]]:13: error: invalid register number
-.cfi_offset $fp, -8
+# CHECK-ERR: :[[#@LINE+1]]:16: error: invalid register name
+.cfi_offset vr0, 8
+# CHECK-ERR: :[[#@LINE+1]]:16: error: invalid register name
+.cfi_offset xr0, 8
 .cfi_endproc
 .endif


        


More information about the llvm-commits mailing list