[llvm] 8c7c791 - [MCParser] Use getVariantKindForName and move PPC specific VariantKind to PowerPC/

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 2 16:21:04 PST 2025


Author: Fangrui Song
Date: 2025-03-02T16:20:59-08:00
New Revision: 8c7c791284877e36f73c41ffa56b52c13e613993

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

LOG: [MCParser] Use getVariantKindForName and move PPC specific VariantKind to PowerPC/

Added: 
    

Modified: 
    llvm/lib/MC/MCExpr.cpp
    llvm/lib/MC/MCParser/AsmParser.cpp
    llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index ede7655733f25..f2243baced857 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -431,6 +431,8 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
   llvm_unreachable("Invalid variant kind");
 }
 
+// FIXME: All variant kinds are target-specific. Move them to
+// *AsmParser::getVariantKindForName and remove this function.
 MCSymbolRefExpr::VariantKind
 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
   return StringSwitch<VariantKind>(Name.lower())
@@ -467,68 +469,6 @@ MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
       .Case("size", VK_SIZE)
       .Case("abs8", VK_X86_ABS8)
       .Case("pltoff", VK_X86_PLTOFF)
-      .Case("l", VK_PPC_LO)
-      .Case("h", VK_PPC_HI)
-      .Case("ha", VK_PPC_HA)
-      .Case("high", VK_PPC_HIGH)
-      .Case("higha", VK_PPC_HIGHA)
-      .Case("higher", VK_PPC_HIGHER)
-      .Case("highera", VK_PPC_HIGHERA)
-      .Case("highest", VK_PPC_HIGHEST)
-      .Case("highesta", VK_PPC_HIGHESTA)
-      .Case("got at l", VK_PPC_GOT_LO)
-      .Case("got at h", VK_PPC_GOT_HI)
-      .Case("got at ha", VK_PPC_GOT_HA)
-      .Case("local", VK_PPC_LOCAL)
-      .Case("tocbase", VK_PPC_TOCBASE)
-      .Case("toc", VK_PPC_TOC)
-      .Case("toc at l", VK_PPC_TOC_LO)
-      .Case("toc at h", VK_PPC_TOC_HI)
-      .Case("toc at ha", VK_PPC_TOC_HA)
-      .Case("u", VK_PPC_U)
-      .Case("l", VK_PPC_L)
-      .Case("tls", VK_PPC_TLS)
-      .Case("dtpmod", VK_PPC_DTPMOD)
-      .Case("tprel at l", VK_PPC_TPREL_LO)
-      .Case("tprel at h", VK_PPC_TPREL_HI)
-      .Case("tprel at ha", VK_PPC_TPREL_HA)
-      .Case("tprel at high", VK_PPC_TPREL_HIGH)
-      .Case("tprel at higha", VK_PPC_TPREL_HIGHA)
-      .Case("tprel at higher", VK_PPC_TPREL_HIGHER)
-      .Case("tprel at highera", VK_PPC_TPREL_HIGHERA)
-      .Case("tprel at highest", VK_PPC_TPREL_HIGHEST)
-      .Case("tprel at highesta", VK_PPC_TPREL_HIGHESTA)
-      .Case("dtprel at l", VK_PPC_DTPREL_LO)
-      .Case("dtprel at h", VK_PPC_DTPREL_HI)
-      .Case("dtprel at ha", VK_PPC_DTPREL_HA)
-      .Case("dtprel at high", VK_PPC_DTPREL_HIGH)
-      .Case("dtprel at higha", VK_PPC_DTPREL_HIGHA)
-      .Case("dtprel at higher", VK_PPC_DTPREL_HIGHER)
-      .Case("dtprel at highera", VK_PPC_DTPREL_HIGHERA)
-      .Case("dtprel at highest", VK_PPC_DTPREL_HIGHEST)
-      .Case("dtprel at highesta", VK_PPC_DTPREL_HIGHESTA)
-      .Case("got at tprel", VK_PPC_GOT_TPREL)
-      .Case("got at tprel@l", VK_PPC_GOT_TPREL_LO)
-      .Case("got at tprel@h", VK_PPC_GOT_TPREL_HI)
-      .Case("got at tprel@ha", VK_PPC_GOT_TPREL_HA)
-      .Case("got at dtprel", VK_PPC_GOT_DTPREL)
-      .Case("got at dtprel@l", VK_PPC_GOT_DTPREL_LO)
-      .Case("got at dtprel@h", VK_PPC_GOT_DTPREL_HI)
-      .Case("got at dtprel@ha", VK_PPC_GOT_DTPREL_HA)
-      .Case("got at tlsgd", VK_PPC_GOT_TLSGD)
-      .Case("got at tlsgd@l", VK_PPC_GOT_TLSGD_LO)
-      .Case("got at tlsgd@h", VK_PPC_GOT_TLSGD_HI)
-      .Case("got at tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
-      .Case("got at tlsld", VK_PPC_GOT_TLSLD)
-      .Case("got at tlsld@l", VK_PPC_GOT_TLSLD_LO)
-      .Case("got at tlsld@h", VK_PPC_GOT_TLSLD_HI)
-      .Case("got at tlsld@ha", VK_PPC_GOT_TLSLD_HA)
-      .Case("got at pcrel", VK_PPC_GOT_PCREL)
-      .Case("got at tlsgd@pcrel", VK_PPC_GOT_TLSGD_PCREL)
-      .Case("got at tlsld@pcrel", VK_PPC_GOT_TLSLD_PCREL)
-      .Case("got at tprel@pcrel", VK_PPC_GOT_TPREL_PCREL)
-      .Case("tls at pcrel", VK_PPC_TLS_PCREL)
-      .Case("notoc", VK_PPC_NOTOC)
       .Case("gdgot", VK_Hexagon_GD_GOT)
       .Case("gdplt", VK_Hexagon_GD_PLT)
       .Case("iegot", VK_Hexagon_IE_GOT)

diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index be90ac33f8532..fca6b3785fe6a 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -1279,7 +1279,7 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
       std::pair<StringRef, StringRef> Split = IDVal.split('@');
       MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
       if (Split.first.size() != IDVal.size()) {
-        Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
+        Variant = getTargetParser().getVariantKindForName(Split.second);
         if (Variant == MCSymbolRefExpr::VK_Invalid)
           return TokError("invalid variant '" + Split.second + "'");
         IDVal = Split.first;
@@ -1458,8 +1458,8 @@ static std::string angleBracketString(StringRef AltMacroStr) {
 bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
   // Parse the expression.
   Res = nullptr;
-  if (getTargetParser().parsePrimaryExpr(Res, EndLoc) ||
-      parseBinOpRHS(1, Res, EndLoc))
+  auto &TS = getTargetParser();
+  if (TS.parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
     return true;
 
   // As a special case, we support 'a op b @ modifier' by rewriting the
@@ -1470,7 +1470,7 @@ bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
       return TokError("unexpected symbol modifier following '@'");
 
     MCSymbolRefExpr::VariantKind Variant =
-        MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
+        TS.getVariantKindForName(getTok().getIdentifier());
     if (Variant == MCSymbolRefExpr::VK_Invalid)
       return TokError("invalid variant '" + getTok().getIdentifier() + "'");
 

diff  --git a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
index a3646532c4530..8f30fd5ebf142 100644
--- a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
+++ b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
@@ -156,6 +156,8 @@ class PPCAsmParser : public MCTargetAsmParser {
   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
                                       unsigned Kind) override;
 
+  MCSymbolRefExpr::VariantKind
+  getVariantKindForName(StringRef Name) const override;
   const MCExpr *applyModifierToExpr(const MCExpr *E,
                                     MCSymbolRefExpr::VariantKind,
                                     MCContext &Ctx) override;
@@ -1901,6 +1903,81 @@ unsigned PPCAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
   return Match_InvalidOperand;
 }
 
+MCSymbolRefExpr::VariantKind
+PPCAsmParser::getVariantKindForName(StringRef Name) const {
+  return StringSwitch<MCSymbolRefExpr::VariantKind>(Name.lower())
+      .Case("dtprel", MCSymbolRefExpr::VK_DTPREL)
+      .Case("got", MCSymbolRefExpr::VK_GOT)
+      .Case("pcrel", MCSymbolRefExpr::VK_PCREL)
+      .Case("plt", MCSymbolRefExpr::VK_PLT)
+      .Case("tlsgd", MCSymbolRefExpr::VK_TLSGD)
+      .Case("tlsld", MCSymbolRefExpr::VK_TLSLD)
+      .Case("tprel", MCSymbolRefExpr::VK_TPREL)
+      .Case("l", MCSymbolRefExpr::VK_PPC_LO)
+      .Case("h", MCSymbolRefExpr::VK_PPC_HI)
+      .Case("ha", MCSymbolRefExpr::VK_PPC_HA)
+      .Case("high", MCSymbolRefExpr::VK_PPC_HIGH)
+      .Case("higha", MCSymbolRefExpr::VK_PPC_HIGHA)
+      .Case("higher", MCSymbolRefExpr::VK_PPC_HIGHER)
+      .Case("highera", MCSymbolRefExpr::VK_PPC_HIGHERA)
+      .Case("highest", MCSymbolRefExpr::VK_PPC_HIGHEST)
+      .Case("highesta", MCSymbolRefExpr::VK_PPC_HIGHESTA)
+      .Case("got at l", MCSymbolRefExpr::VK_PPC_GOT_LO)
+      .Case("got at h", MCSymbolRefExpr::VK_PPC_GOT_HI)
+      .Case("got at ha", MCSymbolRefExpr::VK_PPC_GOT_HA)
+      .Case("local", MCSymbolRefExpr::VK_PPC_LOCAL)
+      .Case("tocbase", MCSymbolRefExpr::VK_PPC_TOCBASE)
+      .Case("toc", MCSymbolRefExpr::VK_PPC_TOC)
+      .Case("toc at l", MCSymbolRefExpr::VK_PPC_TOC_LO)
+      .Case("toc at h", MCSymbolRefExpr::VK_PPC_TOC_HI)
+      .Case("toc at ha", MCSymbolRefExpr::VK_PPC_TOC_HA)
+      .Case("u", MCSymbolRefExpr::VK_PPC_U)
+      // .Case("l", MCSymbolRefExpr::VK_PPC_L) VK_PPC_LO?
+      .Case("tls", MCSymbolRefExpr::VK_PPC_TLS)
+      .Case("dtpmod", MCSymbolRefExpr::VK_PPC_DTPMOD)
+      .Case("tprel at l", MCSymbolRefExpr::VK_PPC_TPREL_LO)
+      .Case("tprel at h", MCSymbolRefExpr::VK_PPC_TPREL_HI)
+      .Case("tprel at ha", MCSymbolRefExpr::VK_PPC_TPREL_HA)
+      .Case("tprel at high", MCSymbolRefExpr::VK_PPC_TPREL_HIGH)
+      .Case("tprel at higha", MCSymbolRefExpr::VK_PPC_TPREL_HIGHA)
+      .Case("tprel at higher", MCSymbolRefExpr::VK_PPC_TPREL_HIGHER)
+      .Case("tprel at highera", MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA)
+      .Case("tprel at highest", MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST)
+      .Case("tprel at highesta", MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA)
+      .Case("dtprel at l", MCSymbolRefExpr::VK_PPC_DTPREL_LO)
+      .Case("dtprel at h", MCSymbolRefExpr::VK_PPC_DTPREL_HI)
+      .Case("dtprel at ha", MCSymbolRefExpr::VK_PPC_DTPREL_HA)
+      .Case("dtprel at high", MCSymbolRefExpr::VK_PPC_DTPREL_HIGH)
+      .Case("dtprel at higha", MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA)
+      .Case("dtprel at higher", MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER)
+      .Case("dtprel at highera", MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA)
+      .Case("dtprel at highest", MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST)
+      .Case("dtprel at highesta", MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA)
+      .Case("got at tprel", MCSymbolRefExpr::VK_PPC_GOT_TPREL)
+      .Case("got at tprel@l", MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO)
+      .Case("got at tprel@h", MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI)
+      .Case("got at tprel@ha", MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA)
+      .Case("got at dtprel", MCSymbolRefExpr::VK_PPC_GOT_DTPREL)
+      .Case("got at dtprel@l", MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO)
+      .Case("got at dtprel@h", MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI)
+      .Case("got at dtprel@ha", MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA)
+      .Case("got at tlsgd", MCSymbolRefExpr::VK_PPC_GOT_TLSGD)
+      .Case("got at tlsgd@l", MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO)
+      .Case("got at tlsgd@h", MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI)
+      .Case("got at tlsgd@ha", MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA)
+      .Case("got at tlsld", MCSymbolRefExpr::VK_PPC_GOT_TLSLD)
+      .Case("got at tlsld@l", MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO)
+      .Case("got at tlsld@h", MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI)
+      .Case("got at tlsld@ha", MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA)
+      .Case("got at pcrel", MCSymbolRefExpr::VK_PPC_GOT_PCREL)
+      .Case("got at tlsgd@pcrel", MCSymbolRefExpr::VK_PPC_GOT_TLSGD_PCREL)
+      .Case("got at tlsld@pcrel", MCSymbolRefExpr::VK_PPC_GOT_TLSLD_PCREL)
+      .Case("got at tprel@pcrel", MCSymbolRefExpr::VK_PPC_GOT_TPREL_PCREL)
+      .Case("tls at pcrel", MCSymbolRefExpr::VK_PPC_TLS_PCREL)
+      .Case("notoc", MCSymbolRefExpr::VK_PPC_NOTOC)
+      .Default(MCSymbolRefExpr::VK_Invalid);
+}
+
 const MCExpr *
 PPCAsmParser::applyModifierToExpr(const MCExpr *E,
                                   MCSymbolRefExpr::VariantKind Variant,


        


More information about the llvm-commits mailing list