[llvm] a8433b8 - MCObjectwriter: Add member variable MCAssembler * and simplify code
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat May 24 00:11:37 PDT 2025
Author: Fangrui Song
Date: 2025-05-24T00:11:32-07:00
New Revision: a8433b88fa40242a87e837187a730502026a3bdb
URL: https://github.com/llvm/llvm-project/commit/a8433b88fa40242a87e837187a730502026a3bdb
DIFF: https://github.com/llvm/llvm-project/commit/a8433b88fa40242a87e837187a730502026a3bdb.diff
LOG: MCObjectwriter: Add member variable MCAssembler * and simplify code
Added:
Modified:
llvm/include/llvm/MC/MCELFObjectWriter.h
llvm/include/llvm/MC/MCMachObjectWriter.h
llvm/include/llvm/MC/MCObjectWriter.h
llvm/include/llvm/MC/MCWinCOFFObjectWriter.h
llvm/lib/MC/ELFObjectWriter.cpp
llvm/lib/MC/MCAssembler.cpp
llvm/lib/MC/MCExpr.cpp
llvm/lib/MC/MCObjectWriter.cpp
llvm/lib/MC/MachObjectWriter.cpp
llvm/lib/MC/WasmObjectWriter.cpp
llvm/lib/MC/WinCOFFObjectWriter.cpp
llvm/lib/MC/XCOFFObjectWriter.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCELFObjectWriter.h b/llvm/include/llvm/MC/MCELFObjectWriter.h
index f0f2dca4738e4..0bac33820ad9d 100644
--- a/llvm/include/llvm/MC/MCELFObjectWriter.h
+++ b/llvm/include/llvm/MC/MCELFObjectWriter.h
@@ -169,12 +169,11 @@ class ELFObjectWriter final : public MCObjectWriter {
bool IsLittleEndian);
void reset() override;
- void executePostLayoutBinding(MCAssembler &Asm) override;
+ void executePostLayoutBinding() override;
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
uint64_t &FixedValue) override;
- bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
- const MCSymbol &SymA,
+ bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
const MCFragment &FB, bool InSet,
bool IsPCRel) const override;
uint64_t writeObject(MCAssembler &Asm) override;
diff --git a/llvm/include/llvm/MC/MCMachObjectWriter.h b/llvm/include/llvm/MC/MCMachObjectWriter.h
index 77f305dad27f8..9a6d26237021a 100644
--- a/llvm/include/llvm/MC/MCMachObjectWriter.h
+++ b/llvm/include/llvm/MC/MCMachObjectWriter.h
@@ -341,10 +341,9 @@ class MachObjectWriter final : public MCObjectWriter {
void computeSectionAddresses(const MCAssembler &Asm);
- void executePostLayoutBinding(MCAssembler &Asm) override;
+ void executePostLayoutBinding() override;
- bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
- const MCSymbol &SymA,
+ bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
const MCFragment &FB, bool InSet,
bool IsPCRel) const override;
diff --git a/llvm/include/llvm/MC/MCObjectWriter.h b/llvm/include/llvm/MC/MCObjectWriter.h
index da919c941e507..b821ab214926c 100644
--- a/llvm/include/llvm/MC/MCObjectWriter.h
+++ b/llvm/include/llvm/MC/MCObjectWriter.h
@@ -32,6 +32,7 @@ class MCValue;
/// should be emitted as part of writeObject().
class MCObjectWriter {
protected:
+ MCAssembler *Asm = nullptr;
/// List of declared file names
SmallVector<std::pair<std::string, size_t>, 0> FileNames;
// XCOFF specific: Optional compiler version.
@@ -54,6 +55,8 @@ class MCObjectWriter {
MCObjectWriter &operator=(const MCObjectWriter &) = delete;
virtual ~MCObjectWriter();
+ void setAssembler(MCAssembler *A) { Asm = A; }
+
/// lifetime management
virtual void reset();
@@ -65,7 +68,7 @@ class MCObjectWriter {
///
/// This routine is called by the assembler after layout and relaxation is
/// complete.
- virtual void executePostLayoutBinding(MCAssembler &Asm) {}
+ virtual void executePostLayoutBinding() {}
/// Record a relocation entry.
///
@@ -82,12 +85,10 @@ class MCObjectWriter {
///
/// Clients are not required to answer precisely and may conservatively return
/// false, even when a
diff erence is fully resolved.
- bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
- const MCSymbol &A, const MCSymbol &B,
+ bool isSymbolRefDifferenceFullyResolved(const MCSymbol &A, const MCSymbol &B,
bool InSet) const;
- virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
- const MCSymbol &SymA,
+ virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
const MCFragment &FB,
bool InSet,
bool IsPCRel) const;
diff --git a/llvm/include/llvm/MC/MCWinCOFFObjectWriter.h b/llvm/include/llvm/MC/MCWinCOFFObjectWriter.h
index 13d8c7d060c9e..8c58e7c0efd75 100644
--- a/llvm/include/llvm/MC/MCWinCOFFObjectWriter.h
+++ b/llvm/include/llvm/MC/MCWinCOFFObjectWriter.h
@@ -63,9 +63,8 @@ class WinCOFFObjectWriter final : public MCObjectWriter {
void setIncrementalLinkerCompatible(bool Value) {
IncrementalLinkerCompatible = Value;
}
- void executePostLayoutBinding(MCAssembler &Asm) override;
- bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
- const MCSymbol &SymA,
+ void executePostLayoutBinding() override;
+ bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
const MCFragment &FB, bool InSet,
bool IsPCRel) const override;
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index ba950b217e70c..acc1bfbd1ae67 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -1187,7 +1187,7 @@ bool ELFObjectWriter::hasRelocationAddend() const {
return TargetObjectWriter->hasRelocationAddend();
}
-void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
+void ELFObjectWriter::executePostLayoutBinding() {
// The presence of symbol versions causes undefined symbols and
// versions declared with @@@ to be renamed.
for (const Symver &S : Symvers) {
@@ -1203,9 +1203,9 @@ void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
auto *Alias =
- cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
- Asm.registerSymbol(*Alias);
- const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
+ cast<MCSymbolELF>(Asm->getContext().getOrCreateSymbol(Prefix + Tail));
+ Asm->registerSymbol(*Alias);
+ const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm->getContext());
Alias->setVariableValue(Value);
// Aliases defined with .symvar copy the binding from the symbol they alias.
@@ -1219,15 +1219,15 @@ void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
if (Symbol.isUndefined() && Rest.starts_with("@@") &&
!Rest.starts_with("@@@")) {
- Asm.getContext().reportError(S.Loc, "default version symbol " +
- AliasName + " must be defined");
+ Asm->getContext().reportError(S.Loc, "default version symbol " +
+ AliasName + " must be defined");
continue;
}
if (auto It = Renames.find(&Symbol);
It != Renames.end() && It->second != Alias) {
- Asm.getContext().reportError(S.Loc, Twine("multiple versions for ") +
- Symbol.getName());
+ Asm->getContext().reportError(S.Loc, Twine("multiple versions for ") +
+ Symbol.getName());
continue;
}
@@ -1416,8 +1416,7 @@ bool ELFObjectWriter::usesRela(const MCTargetOptions *TO,
}
bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
- const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
- bool InSet, bool IsPCRel) const {
+ const MCSymbol &SA, const MCFragment &FB, bool InSet, bool IsPCRel) const {
const auto &SymA = cast<MCSymbolELF>(SA);
if (IsPCRel) {
assert(!InSet);
diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index 163b45ca41822..d873a61c854ce 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -85,6 +85,8 @@ MCAssembler::MCAssembler(MCContext &Context,
Emitter(std::move(Emitter)), Writer(std::move(Writer)) {
if (this->Backend)
this->Backend->setAssembler(this);
+ if (this->Writer)
+ this->Writer->setAssembler(this);
}
void MCAssembler::reset() {
@@ -187,7 +189,7 @@ bool MCAssembler::evaluateFixup(const MCFragment *DF, const MCFixup &Fixup,
if (Add && !Sub && !Add->isUndefined() && !Add->isAbsolute()) {
IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl(
- *this, *Add, *DF, false, true);
+ *Add, *DF, false, true);
}
} else {
IsResolved = Target.isAbsolute();
@@ -895,7 +897,7 @@ void MCAssembler::layout() {
// Allow the object writer a chance to perform post-layout binding (for
// example, to set the index fields in the symbol data).
- getWriter().executePostLayoutBinding(*this);
+ getWriter().executePostLayoutBinding();
// Fragment sizes are finalized. For RISC-V linker relaxation, this flag
// helps check whether a PC-relative fixup is fully resolved.
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 2a8b91d554a09..f890e477adf94 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -295,7 +295,7 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
const MCSymbol &SA = *A, &SB = *B;
if (SA.isUndefined() || SB.isUndefined())
return;
- if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, SA, SB, InSet))
+ if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(SA, SB, InSet))
return;
auto FinalizeFolding = [&]() {
diff --git a/llvm/lib/MC/MCObjectWriter.cpp b/llvm/lib/MC/MCObjectWriter.cpp
index 6d5010dcae0a0..5f1f2472c417c 100644
--- a/llvm/lib/MC/MCObjectWriter.cpp
+++ b/llvm/lib/MC/MCObjectWriter.cpp
@@ -27,18 +27,17 @@ void MCObjectWriter::reset() {
CGProfile.clear();
}
-bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
- const MCSymbol &SA,
+bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(const MCSymbol &SA,
const MCSymbol &SB,
bool InSet) const {
assert(!SA.isUndefined() && !SB.isUndefined());
- return isSymbolRefDifferenceFullyResolvedImpl(Asm, SA, *SB.getFragment(),
- InSet, /*IsPCRel=*/false);
+ return isSymbolRefDifferenceFullyResolvedImpl(SA, *SB.getFragment(), InSet,
+ /*IsPCRel=*/false);
}
bool MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
- const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
- bool InSet, bool IsPCRel) const {
+ const MCSymbol &SymA, const MCFragment &FB, bool InSet,
+ bool IsPCRel) const {
const MCSection &SecA = SymA.getSection();
const MCSection &SecB = *FB.getParent();
// On ELF and COFF A - B is absolute if A and B are in the same section.
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 940327c296d85..0de16b515c4bd 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -712,16 +712,16 @@ void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm) {
}
}
-void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
- computeSectionAddresses(Asm);
+void MachObjectWriter::executePostLayoutBinding() {
+ computeSectionAddresses(*Asm);
// Create symbol data for any indirect symbols.
- bindIndirectSymbols(Asm);
+ bindIndirectSymbols(*Asm);
}
bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
- const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
- bool InSet, bool IsPCRel) const {
+ const MCSymbol &SymA, const MCFragment &FB, bool InSet,
+ bool IsPCRel) const {
if (InSet)
return true;
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 18c85c6392d97..6f51a70175775 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -296,7 +296,7 @@ class WasmObjectWriter : public MCObjectWriter {
const MCFixup &Fixup, MCValue Target,
uint64_t &FixedValue) override;
- void executePostLayoutBinding(MCAssembler &Asm) override;
+ void executePostLayoutBinding() override;
void prepareImports(SmallVectorImpl<wasm::WasmImport> &Imports,
MCAssembler &Asm);
uint64_t writeObject(MCAssembler &Asm) override;
@@ -449,22 +449,22 @@ void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
W->write<uint32_t>(wasm::WasmVersion);
}
-void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
+void WasmObjectWriter::executePostLayoutBinding() {
// Some compilation units require the indirect function table to be present
// but don't explicitly reference it. This is the case for call_indirect
// without the reference-types feature, and also function bitcasts in all
// cases. In those cases the __indirect_function_table has the
// WASM_SYMBOL_NO_STRIP attribute. Here we make sure this symbol makes it to
// the assembler, if needed.
- if (auto *Sym = Asm.getContext().lookupSymbol("__indirect_function_table")) {
+ if (auto *Sym = Asm->getContext().lookupSymbol("__indirect_function_table")) {
const auto *WasmSym = static_cast<const MCSymbolWasm *>(Sym);
if (WasmSym->isNoStrip())
- Asm.registerSymbol(*Sym);
+ Asm->registerSymbol(*Sym);
}
// Build a map of sections to the function that defines them, for use
// in recordRelocation.
- for (const MCSymbol &S : Asm.symbols()) {
+ for (const MCSymbol &S : Asm->symbols()) {
const auto &WS = static_cast<const MCSymbolWasm &>(S);
if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection());
diff --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp
index b7d05b864bb39..599c1d81ec6ba 100644
--- a/llvm/lib/MC/WinCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -1173,8 +1173,8 @@ void WinCOFFObjectWriter::reset() {
}
bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
- const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
- bool InSet, bool IsPCRel) const {
+ const MCSymbol &SymA, const MCFragment &FB, bool InSet,
+ bool IsPCRel) const {
// Don't drop relocations between functions, even if they are in the same text
// section. Multiple Visual C++ linker features depend on having the
// relocations present. The /INCREMENTAL flag will cause these relocations to
@@ -1187,10 +1187,10 @@ bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
return &SymA.getSection() == FB.getParent();
}
-void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
- ObjWriter->executePostLayoutBinding(Asm);
+void WinCOFFObjectWriter::executePostLayoutBinding() {
+ ObjWriter->executePostLayoutBinding(*Asm);
if (DwoWriter)
- DwoWriter->executePostLayoutBinding(Asm);
+ DwoWriter->executePostLayoutBinding(*Asm);
}
void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
diff --git a/llvm/lib/MC/XCOFFObjectWriter.cpp b/llvm/lib/MC/XCOFFObjectWriter.cpp
index 0834568f6dfe6..b0d4bd9ede37d 100644
--- a/llvm/lib/MC/XCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/XCOFFObjectWriter.cpp
@@ -348,7 +348,7 @@ class XCOFFWriter final : public XCOFFObjectWriter {
void reset() override;
- void executePostLayoutBinding(MCAssembler &) override;
+ void executePostLayoutBinding() override;
void recordRelocation(MCAssembler &, const MCFragment *, const MCFixup &,
MCValue, uint64_t &) override;
@@ -555,8 +555,8 @@ static MCSectionXCOFF *getContainingCsect(const MCSymbolXCOFF *XSym) {
return XSym->getRepresentedCsect();
}
-void XCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
- for (const auto &S : Asm) {
+void XCOFFWriter::executePostLayoutBinding() {
+ for (const auto &S : *Asm) {
const auto *MCSec = cast<const MCSectionXCOFF>(&S);
assert(!SectionMap.contains(MCSec) && "Cannot add a section twice.");
@@ -587,7 +587,7 @@ void XCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
llvm_unreachable("unsupport section type!");
}
- for (const MCSymbol &S : Asm.symbols()) {
+ for (const MCSymbol &S : Asm->symbols()) {
// Nothing to do for temporary symbols.
if (S.isTemporary())
continue;
@@ -653,7 +653,7 @@ void XCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
Strings.add(Vers);
Strings.finalize();
- assignAddressesAndIndices(Asm);
+ assignAddressesAndIndices(*Asm);
}
void XCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
More information about the llvm-commits
mailing list