[llvm] 14951a5 - [MCParser] Extract some VariantKind from getVariantKindForName
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 2 17:08:23 PST 2025
Author: Fangrui Song
Date: 2025-03-02T17:08:17-08:00
New Revision: 14951a5a3120e50084b3c5fb217e2d47992a24d1
URL: https://github.com/llvm/llvm-project/commit/14951a5a3120e50084b3c5fb217e2d47992a24d1
DIFF: https://github.com/llvm/llvm-project/commit/14951a5a3120e50084b3c5fb217e2d47992a24d1.diff
LOG: [MCParser] Extract some VariantKind from getVariantKindForName
All VariantKinds except VK_None/VK_Invalid are target-specific (e.g. a
target may not support "@plt" even if it is widely available).
Move the parsers to lib/Target to ensure that VariantKind from unrelated
targets will not be parsed.
Added:
Modified:
llvm/lib/MC/MCExpr.cpp
llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp
llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
Removed:
################################################################################
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index f2243baced857..9999a80a43a20 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -246,6 +246,7 @@ const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
}
+// TODO: Move target-specific Kinds to lib/Target/.
StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
switch (Kind) {
// clang-format off
@@ -444,11 +445,9 @@ MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
.Case("gotrel", VK_GOTREL)
.Case("pcrel", VK_PCREL)
.Case("gotpcrel", VK_GOTPCREL)
- .Case("gotpcrel_norelax", VK_GOTPCREL_NORELAX)
.Case("gottpoff", VK_GOTTPOFF)
.Case("indntpoff", VK_INDNTPOFF)
.Case("ntpoff", VK_NTPOFF)
- .Case("gotntpoff", VK_GOTNTPOFF)
.Case("plt", VK_PLT)
.Case("tlscall", VK_TLSCALL)
.Case("tlsdesc", VK_TLSDESC)
@@ -466,45 +465,6 @@ MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
.Case("gotpageoff", VK_GOTPAGEOFF)
.Case("imgrel", VK_COFF_IMGREL32)
.Case("secrel32", VK_SECREL)
- .Case("size", VK_SIZE)
- .Case("abs8", VK_X86_ABS8)
- .Case("pltoff", VK_X86_PLTOFF)
- .Case("gdgot", VK_Hexagon_GD_GOT)
- .Case("gdplt", VK_Hexagon_GD_PLT)
- .Case("iegot", VK_Hexagon_IE_GOT)
- .Case("ie", VK_Hexagon_IE)
- .Case("ldgot", VK_Hexagon_LD_GOT)
- .Case("ldplt", VK_Hexagon_LD_PLT)
- .Case("lo8", VK_AVR_LO8)
- .Case("hi8", VK_AVR_HI8)
- .Case("hlo8", VK_AVR_HLO8)
- .Case("typeindex", VK_WASM_TYPEINDEX)
- .Case("tbrel", VK_WASM_TBREL)
- .Case("mbrel", VK_WASM_MBREL)
- .Case("tlsrel", VK_WASM_TLSREL)
- .Case("got at tls", VK_WASM_GOT_TLS)
- .Case("funcindex", VK_WASM_FUNCINDEX)
- .Case("gotpcrel32 at lo", VK_AMDGPU_GOTPCREL32_LO)
- .Case("gotpcrel32 at hi", VK_AMDGPU_GOTPCREL32_HI)
- .Case("rel32 at lo", VK_AMDGPU_REL32_LO)
- .Case("rel32 at hi", VK_AMDGPU_REL32_HI)
- .Case("rel64", VK_AMDGPU_REL64)
- .Case("abs32 at lo", VK_AMDGPU_ABS32_LO)
- .Case("abs32 at hi", VK_AMDGPU_ABS32_HI)
- .Case("hi", VK_VE_HI32)
- .Case("lo", VK_VE_LO32)
- .Case("pc_hi", VK_VE_PC_HI32)
- .Case("pc_lo", VK_VE_PC_LO32)
- .Case("got_hi", VK_VE_GOT_HI32)
- .Case("got_lo", VK_VE_GOT_LO32)
- .Case("gotoff_hi", VK_VE_GOTOFF_HI32)
- .Case("gotoff_lo", VK_VE_GOTOFF_LO32)
- .Case("plt_hi", VK_VE_PLT_HI32)
- .Case("plt_lo", VK_VE_PLT_LO32)
- .Case("tls_gd_hi", VK_VE_TLS_GD_HI32)
- .Case("tls_gd_lo", VK_VE_TLS_GD_LO32)
- .Case("tpoff_hi", VK_VE_TPOFF_HI32)
- .Case("tpoff_lo", VK_VE_TPOFF_LO32)
.Default(VK_Invalid);
}
diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
index 54ed3789326cb..3a8210b693c87 100644
--- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
+++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
@@ -1612,6 +1612,8 @@ class AMDGPUAsmParser : public MCTargetAsmParser {
OperandVector &Operands, MCStreamer &Out,
uint64_t &ErrorInfo,
bool MatchingInlineAsm) override;
+ MCSymbolRefExpr::VariantKind
+ getVariantKindForName(StringRef Name) const override;
bool ParseDirective(AsmToken DirectiveID) override;
ParseStatus parseOperand(OperandVector &Operands, StringRef Mnemonic,
OperandMode Mode = OperandMode_Default);
@@ -5450,6 +5452,20 @@ bool AMDGPUAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
llvm_unreachable("Implement any new match types added!");
}
+MCSymbolRefExpr::VariantKind
+AMDGPUAsmParser::getVariantKindForName(StringRef Name) const {
+ return StringSwitch<MCSymbolRefExpr::VariantKind>(Name.lower())
+ .Case("gotpcrel", MCSymbolRefExpr::VK_GOTPCREL)
+ .Case("gotpcrel32 at lo", MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO)
+ .Case("gotpcrel32 at hi", MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI)
+ .Case("rel32 at lo", MCSymbolRefExpr::VK_AMDGPU_REL32_LO)
+ .Case("rel32 at hi", MCSymbolRefExpr::VK_AMDGPU_REL32_HI)
+ .Case("rel64", MCSymbolRefExpr::VK_AMDGPU_REL64)
+ .Case("abs32 at lo", MCSymbolRefExpr::VK_AMDGPU_ABS32_LO)
+ .Case("abs32 at hi", MCSymbolRefExpr::VK_AMDGPU_ABS32_HI)
+ .Default(MCSymbolRefExpr::VK_Invalid);
+}
+
bool AMDGPUAsmParser::ParseAsAbsoluteExpression(uint32_t &Ret) {
int64_t Tmp = -1;
if (!isToken(AsmToken::Integer) && !isToken(AsmToken::Identifier)) {
diff --git a/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp b/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp
index 68bd89f624b8a..ae63877c7fc27 100644
--- a/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp
+++ b/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp
@@ -77,6 +77,9 @@ class AVRAsmParser : public MCTargetAsmParser {
unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
unsigned Kind) override;
+ MCSymbolRefExpr::VariantKind
+ getVariantKindForName(StringRef Name) const override;
+
MCRegister toDREG(MCRegister Reg, unsigned From = AVR::sub_lo) {
MCRegisterClass const *Class = &AVRMCRegisterClasses[AVR::DREGSRegClassID];
return MRI->getMatchingSuperReg(Reg, From, Class);
@@ -783,3 +786,12 @@ unsigned AVRAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
}
return Match_InvalidOperand;
}
+
+MCSymbolRefExpr::VariantKind
+AVRAsmParser::getVariantKindForName(StringRef Name) const {
+ return StringSwitch<MCSymbolRefExpr::VariantKind>(Name.lower())
+ .Case("lo8", MCSymbolRefExpr::VK_AVR_LO8)
+ .Case("hi8", MCSymbolRefExpr::VK_AVR_HI8)
+ .Case("hlo8", MCSymbolRefExpr::VK_AVR_HLO8)
+ .Default(MCSymbolRefExpr::VK_Invalid);
+}
diff --git a/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp b/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
index f8c57fc5e0058..4535237683227 100644
--- a/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
+++ b/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
@@ -140,6 +140,8 @@ class HexagonAsmParser : public MCTargetAsmParser {
unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
unsigned Kind) override;
+ MCSymbolRefExpr::VariantKind
+ getVariantKindForName(StringRef Name) const override;
bool OutOfRange(SMLoc IDLoc, long long Val, long long Max);
int processInstruction(MCInst &Inst, OperandVector const &Operands,
SMLoc IDLoc);
@@ -1330,6 +1332,24 @@ unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
return Match_InvalidOperand;
}
+MCSymbolRefExpr::VariantKind
+HexagonAsmParser::getVariantKindForName(StringRef Name) const {
+ return StringSwitch<MCSymbolRefExpr::VariantKind>(Name.lower())
+ .Case("dtprel", MCSymbolRefExpr::VK_DTPREL)
+ .Case("gdgot", MCSymbolRefExpr::VK_Hexagon_GD_GOT)
+ .Case("gdplt", MCSymbolRefExpr::VK_Hexagon_GD_PLT)
+ .Case("got", MCSymbolRefExpr::VK_GOT)
+ .Case("gotrel", MCSymbolRefExpr::VK_GOTREL)
+ .Case("ie", MCSymbolRefExpr::VK_Hexagon_IE)
+ .Case("iegot", MCSymbolRefExpr::VK_Hexagon_IE_GOT)
+ .Case("ldgot", MCSymbolRefExpr::VK_Hexagon_LD_GOT)
+ .Case("ldplt", MCSymbolRefExpr::VK_Hexagon_LD_PLT)
+ .Case("pcrel", MCSymbolRefExpr::VK_PCREL)
+ .Case("plt", MCSymbolRefExpr::VK_PLT)
+ .Case("tprel", MCSymbolRefExpr::VK_TPREL)
+ .Default(MCSymbolRefExpr::VK_Invalid);
+}
+
// FIXME: Calls to OutOfRange should propagate failure up to parseStatement.
bool HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
std::string errStr;
diff --git a/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp b/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
index 0190e6072d76a..0093544cc3e7f 100644
--- a/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
+++ b/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
@@ -63,6 +63,9 @@ class VEAsmParser : public MCTargetAsmParser {
unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
unsigned Kind) override;
+ MCSymbolRefExpr::VariantKind
+ getVariantKindForName(StringRef Name) const override;
+
// Custom parse functions for VE specific operands.
ParseStatus parseMEMOperand(OperandVector &Operands);
ParseStatus parseMEMAsOperand(OperandVector &Operands);
@@ -1583,3 +1586,23 @@ unsigned VEAsmParser::validateTargetOperandClass(MCParsedAsmOperand &GOp,
}
return Match_InvalidOperand;
}
+
+MCSymbolRefExpr::VariantKind
+VEAsmParser::getVariantKindForName(StringRef Name) const {
+ return StringSwitch<MCSymbolRefExpr::VariantKind>(Name.lower())
+ .Case("hi", MCSymbolRefExpr::VK_VE_HI32)
+ .Case("lo", MCSymbolRefExpr::VK_VE_LO32)
+ .Case("pc_hi", MCSymbolRefExpr::VK_VE_PC_HI32)
+ .Case("pc_lo", MCSymbolRefExpr::VK_VE_PC_LO32)
+ .Case("got_hi", MCSymbolRefExpr::VK_VE_GOT_HI32)
+ .Case("got_lo", MCSymbolRefExpr::VK_VE_GOT_LO32)
+ .Case("gotoff_hi", MCSymbolRefExpr::VK_VE_GOTOFF_HI32)
+ .Case("gotoff_lo", MCSymbolRefExpr::VK_VE_GOTOFF_LO32)
+ .Case("plt_hi", MCSymbolRefExpr::VK_VE_PLT_HI32)
+ .Case("plt_lo", MCSymbolRefExpr::VK_VE_PLT_LO32)
+ .Case("tls_gd_hi", MCSymbolRefExpr::VK_VE_TLS_GD_HI32)
+ .Case("tls_gd_lo", MCSymbolRefExpr::VK_VE_TLS_GD_LO32)
+ .Case("tpoff_hi", MCSymbolRefExpr::VK_VE_TPOFF_HI32)
+ .Case("tpoff_lo", MCSymbolRefExpr::VK_VE_TPOFF_LO32)
+ .Default(MCSymbolRefExpr::VK_Invalid);
+}
diff --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
index 2a4e2c897b18d..55175f9859705 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
@@ -1210,6 +1210,9 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
llvm_unreachable("Implement any new match types added!");
}
+ MCSymbolRefExpr::VariantKind
+ getVariantKindForName(StringRef Name) const override;
+
void doBeforeLabelEmit(MCSymbol *Symbol, SMLoc IDLoc) override {
// Code below only applies to labels in text sections.
auto *CWS = cast<MCSectionWasm>(getStreamer().getCurrentSectionOnly());
@@ -1280,6 +1283,19 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
};
} // end anonymous namespace
+MCSymbolRefExpr::VariantKind
+WebAssemblyAsmParser::getVariantKindForName(StringRef Name) const {
+ return StringSwitch<MCSymbolRefExpr::VariantKind>(Name.lower())
+ .Case("typeindex", MCSymbolRefExpr::VK_WASM_TYPEINDEX)
+ .Case("tbrel", MCSymbolRefExpr::VK_WASM_TBREL)
+ .Case("mbrel", MCSymbolRefExpr::VK_WASM_MBREL)
+ .Case("tlsrel", MCSymbolRefExpr::VK_WASM_TLSREL)
+ .Case("got", MCSymbolRefExpr::VK_GOT)
+ .Case("got at tls", MCSymbolRefExpr::VK_WASM_GOT_TLS)
+ .Case("funcindex", MCSymbolRefExpr::VK_WASM_FUNCINDEX)
+ .Default(MCSymbolRefExpr::VK_Invalid);
+}
+
// Force static initialization.
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmParser() {
RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32());
diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index a6285a55f4155..2b69ef5fc2af6 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -1182,6 +1182,9 @@ class X86AsmParser : public MCTargetAsmParser {
unsigned checkTargetMatchPredicate(MCInst &Inst) override;
+ MCSymbolRefExpr::VariantKind
+ getVariantKindForName(StringRef Name) const override;
+
bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
bool processInstruction(MCInst &Inst, const OperandVector &Ops);
@@ -4247,6 +4250,40 @@ unsigned X86AsmParser::checkTargetMatchPredicate(MCInst &Inst) {
return Match_Success;
}
+MCSymbolRefExpr::VariantKind
+X86AsmParser::getVariantKindForName(StringRef Name) const {
+ return StringSwitch<MCSymbolRefExpr::VariantKind>(Name.lower())
+ .Case("abs8", MCSymbolRefExpr::VK_X86_ABS8)
+ .Case("dtpoff", MCSymbolRefExpr::VK_DTPOFF)
+ .Case("dtprel", MCSymbolRefExpr::VK_DTPREL)
+ .Case("got", MCSymbolRefExpr::VK_GOT)
+ .Case("gotent", MCSymbolRefExpr::VK_GOTENT)
+ .Case("gotntpoff", MCSymbolRefExpr::VK_GOTNTPOFF)
+ .Case("gotoff", MCSymbolRefExpr::VK_GOTOFF)
+ .Case("gotpcrel", MCSymbolRefExpr::VK_GOTPCREL)
+ .Case("gotpcrel_norelax", MCSymbolRefExpr::VK_GOTPCREL_NORELAX)
+ .Case("gotrel", MCSymbolRefExpr::VK_GOTREL)
+ .Case("gottpoff", MCSymbolRefExpr::VK_GOTTPOFF)
+ .Case("indntpoff", MCSymbolRefExpr::VK_INDNTPOFF)
+ .Case("imgrel", MCSymbolRefExpr::VK_COFF_IMGREL32)
+ .Case("ntpoff", MCSymbolRefExpr::VK_NTPOFF)
+ .Case("pcrel", MCSymbolRefExpr::VK_PCREL)
+ .Case("plt", MCSymbolRefExpr::VK_PLT)
+ .Case("pltoff", MCSymbolRefExpr::VK_X86_PLTOFF)
+ .Case("secrel32", MCSymbolRefExpr::VK_SECREL)
+ .Case("size", MCSymbolRefExpr::VK_SIZE)
+ .Case("tlscall", MCSymbolRefExpr::VK_TLSCALL)
+ .Case("tlsdesc", MCSymbolRefExpr::VK_TLSDESC)
+ .Case("tlsgd", MCSymbolRefExpr::VK_TLSGD)
+ .Case("tlsld", MCSymbolRefExpr::VK_TLSLD)
+ .Case("tlsldm", MCSymbolRefExpr::VK_TLSLDM)
+ .Case("tlvp", MCSymbolRefExpr::VK_TLVP)
+ .Case("tlvppage", MCSymbolRefExpr::VK_TLVPPAGE)
+ .Case("tlvppageoff", MCSymbolRefExpr::VK_TLVPPAGEOFF)
+ .Case("tpoff", MCSymbolRefExpr::VK_TPOFF)
+ .Default(MCSymbolRefExpr::VK_Invalid);
+}
+
bool X86AsmParser::matchAndEmitATTInstruction(
SMLoc IDLoc, unsigned &Opcode, MCInst &Inst, OperandVector &Operands,
MCStreamer &Out, uint64_t &ErrorInfo, bool MatchingInlineAsm) {
More information about the llvm-commits
mailing list