Skip to content

Commit a8433b8

Browse files
committed
MCObjectwriter: Add member variable MCAssembler * and simplify code
1 parent 6d1d937 commit a8433b8

12 files changed

+51
-53
lines changed

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,11 @@ class ELFObjectWriter final : public MCObjectWriter {
169169
bool IsLittleEndian);
170170

171171
void reset() override;
172-
void executePostLayoutBinding(MCAssembler &Asm) override;
172+
void executePostLayoutBinding() override;
173173
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
174174
const MCFixup &Fixup, MCValue Target,
175175
uint64_t &FixedValue) override;
176-
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
177-
const MCSymbol &SymA,
176+
bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
178177
const MCFragment &FB, bool InSet,
179178
bool IsPCRel) const override;
180179
uint64_t writeObject(MCAssembler &Asm) override;

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,9 @@ class MachObjectWriter final : public MCObjectWriter {
341341

342342
void computeSectionAddresses(const MCAssembler &Asm);
343343

344-
void executePostLayoutBinding(MCAssembler &Asm) override;
344+
void executePostLayoutBinding() override;
345345

346-
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
347-
const MCSymbol &SymA,
346+
bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
348347
const MCFragment &FB, bool InSet,
349348
bool IsPCRel) const override;
350349

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class MCValue;
3232
/// should be emitted as part of writeObject().
3333
class MCObjectWriter {
3434
protected:
35+
MCAssembler *Asm = nullptr;
3536
/// List of declared file names
3637
SmallVector<std::pair<std::string, size_t>, 0> FileNames;
3738
// XCOFF specific: Optional compiler version.
@@ -54,6 +55,8 @@ class MCObjectWriter {
5455
MCObjectWriter &operator=(const MCObjectWriter &) = delete;
5556
virtual ~MCObjectWriter();
5657

58+
void setAssembler(MCAssembler *A) { Asm = A; }
59+
5760
/// lifetime management
5861
virtual void reset();
5962

@@ -65,7 +68,7 @@ class MCObjectWriter {
6568
///
6669
/// This routine is called by the assembler after layout and relaxation is
6770
/// complete.
68-
virtual void executePostLayoutBinding(MCAssembler &Asm) {}
71+
virtual void executePostLayoutBinding() {}
6972

7073
/// Record a relocation entry.
7174
///
@@ -82,12 +85,10 @@ class MCObjectWriter {
8285
///
8386
/// Clients are not required to answer precisely and may conservatively return
8487
/// false, even when a difference is fully resolved.
85-
bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
86-
const MCSymbol &A, const MCSymbol &B,
88+
bool isSymbolRefDifferenceFullyResolved(const MCSymbol &A, const MCSymbol &B,
8789
bool InSet) const;
8890

89-
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
90-
const MCSymbol &SymA,
91+
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
9192
const MCFragment &FB,
9293
bool InSet,
9394
bool IsPCRel) const;

llvm/include/llvm/MC/MCWinCOFFObjectWriter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ class WinCOFFObjectWriter final : public MCObjectWriter {
6363
void setIncrementalLinkerCompatible(bool Value) {
6464
IncrementalLinkerCompatible = Value;
6565
}
66-
void executePostLayoutBinding(MCAssembler &Asm) override;
67-
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
68-
const MCSymbol &SymA,
66+
void executePostLayoutBinding() override;
67+
bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
6968
const MCFragment &FB, bool InSet,
7069
bool IsPCRel) const override;
7170
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ bool ELFObjectWriter::hasRelocationAddend() const {
11871187
return TargetObjectWriter->hasRelocationAddend();
11881188
}
11891189

1190-
void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
1190+
void ELFObjectWriter::executePostLayoutBinding() {
11911191
// The presence of symbol versions causes undefined symbols and
11921192
// versions declared with @@@ to be renamed.
11931193
for (const Symver &S : Symvers) {
@@ -1203,9 +1203,9 @@ void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
12031203
Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
12041204

12051205
auto *Alias =
1206-
cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
1207-
Asm.registerSymbol(*Alias);
1208-
const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
1206+
cast<MCSymbolELF>(Asm->getContext().getOrCreateSymbol(Prefix + Tail));
1207+
Asm->registerSymbol(*Alias);
1208+
const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm->getContext());
12091209
Alias->setVariableValue(Value);
12101210

12111211
// Aliases defined with .symvar copy the binding from the symbol they alias.
@@ -1219,15 +1219,15 @@ void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
12191219

12201220
if (Symbol.isUndefined() && Rest.starts_with("@@") &&
12211221
!Rest.starts_with("@@@")) {
1222-
Asm.getContext().reportError(S.Loc, "default version symbol " +
1223-
AliasName + " must be defined");
1222+
Asm->getContext().reportError(S.Loc, "default version symbol " +
1223+
AliasName + " must be defined");
12241224
continue;
12251225
}
12261226

12271227
if (auto It = Renames.find(&Symbol);
12281228
It != Renames.end() && It->second != Alias) {
1229-
Asm.getContext().reportError(S.Loc, Twine("multiple versions for ") +
1230-
Symbol.getName());
1229+
Asm->getContext().reportError(S.Loc, Twine("multiple versions for ") +
1230+
Symbol.getName());
12311231
continue;
12321232
}
12331233

@@ -1416,8 +1416,7 @@ bool ELFObjectWriter::usesRela(const MCTargetOptions *TO,
14161416
}
14171417

14181418
bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1419-
const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1420-
bool InSet, bool IsPCRel) const {
1419+
const MCSymbol &SA, const MCFragment &FB, bool InSet, bool IsPCRel) const {
14211420
const auto &SymA = cast<MCSymbolELF>(SA);
14221421
if (IsPCRel) {
14231422
assert(!InSet);

llvm/lib/MC/MCAssembler.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ MCAssembler::MCAssembler(MCContext &Context,
8585
Emitter(std::move(Emitter)), Writer(std::move(Writer)) {
8686
if (this->Backend)
8787
this->Backend->setAssembler(this);
88+
if (this->Writer)
89+
this->Writer->setAssembler(this);
8890
}
8991

9092
void MCAssembler::reset() {
@@ -187,7 +189,7 @@ bool MCAssembler::evaluateFixup(const MCFragment *DF, const MCFixup &Fixup,
187189

188190
if (Add && !Sub && !Add->isUndefined() && !Add->isAbsolute()) {
189191
IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl(
190-
*this, *Add, *DF, false, true);
192+
*Add, *DF, false, true);
191193
}
192194
} else {
193195
IsResolved = Target.isAbsolute();
@@ -895,7 +897,7 @@ void MCAssembler::layout() {
895897

896898
// Allow the object writer a chance to perform post-layout binding (for
897899
// example, to set the index fields in the symbol data).
898-
getWriter().executePostLayoutBinding(*this);
900+
getWriter().executePostLayoutBinding();
899901

900902
// Fragment sizes are finalized. For RISC-V linker relaxation, this flag
901903
// helps check whether a PC-relative fixup is fully resolved.

llvm/lib/MC/MCExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
295295
const MCSymbol &SA = *A, &SB = *B;
296296
if (SA.isUndefined() || SB.isUndefined())
297297
return;
298-
if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, SA, SB, InSet))
298+
if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(SA, SB, InSet))
299299
return;
300300

301301
auto FinalizeFolding = [&]() {

llvm/lib/MC/MCObjectWriter.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ void MCObjectWriter::reset() {
2727
CGProfile.clear();
2828
}
2929

30-
bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
31-
const MCSymbol &SA,
30+
bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(const MCSymbol &SA,
3231
const MCSymbol &SB,
3332
bool InSet) const {
3433
assert(!SA.isUndefined() && !SB.isUndefined());
35-
return isSymbolRefDifferenceFullyResolvedImpl(Asm, SA, *SB.getFragment(),
36-
InSet, /*IsPCRel=*/false);
34+
return isSymbolRefDifferenceFullyResolvedImpl(SA, *SB.getFragment(), InSet,
35+
/*IsPCRel=*/false);
3736
}
3837

3938
bool MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
40-
const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
41-
bool InSet, bool IsPCRel) const {
39+
const MCSymbol &SymA, const MCFragment &FB, bool InSet,
40+
bool IsPCRel) const {
4241
const MCSection &SecA = SymA.getSection();
4342
const MCSection &SecB = *FB.getParent();
4443
// On ELF and COFF A - B is absolute if A and B are in the same section.

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -712,16 +712,16 @@ void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm) {
712712
}
713713
}
714714

715-
void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
716-
computeSectionAddresses(Asm);
715+
void MachObjectWriter::executePostLayoutBinding() {
716+
computeSectionAddresses(*Asm);
717717

718718
// Create symbol data for any indirect symbols.
719-
bindIndirectSymbols(Asm);
719+
bindIndirectSymbols(*Asm);
720720
}
721721

722722
bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
723-
const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
724-
bool InSet, bool IsPCRel) const {
723+
const MCSymbol &SymA, const MCFragment &FB, bool InSet,
724+
bool IsPCRel) const {
725725
if (InSet)
726726
return true;
727727

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class WasmObjectWriter : public MCObjectWriter {
296296
const MCFixup &Fixup, MCValue Target,
297297
uint64_t &FixedValue) override;
298298

299-
void executePostLayoutBinding(MCAssembler &Asm) override;
299+
void executePostLayoutBinding() override;
300300
void prepareImports(SmallVectorImpl<wasm::WasmImport> &Imports,
301301
MCAssembler &Asm);
302302
uint64_t writeObject(MCAssembler &Asm) override;
@@ -449,22 +449,22 @@ void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
449449
W->write<uint32_t>(wasm::WasmVersion);
450450
}
451451

452-
void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
452+
void WasmObjectWriter::executePostLayoutBinding() {
453453
// Some compilation units require the indirect function table to be present
454454
// but don't explicitly reference it. This is the case for call_indirect
455455
// without the reference-types feature, and also function bitcasts in all
456456
// cases. In those cases the __indirect_function_table has the
457457
// WASM_SYMBOL_NO_STRIP attribute. Here we make sure this symbol makes it to
458458
// the assembler, if needed.
459-
if (auto *Sym = Asm.getContext().lookupSymbol("__indirect_function_table")) {
459+
if (auto *Sym = Asm->getContext().lookupSymbol("__indirect_function_table")) {
460460
const auto *WasmSym = static_cast<const MCSymbolWasm *>(Sym);
461461
if (WasmSym->isNoStrip())
462-
Asm.registerSymbol(*Sym);
462+
Asm->registerSymbol(*Sym);
463463
}
464464

465465
// Build a map of sections to the function that defines them, for use
466466
// in recordRelocation.
467-
for (const MCSymbol &S : Asm.symbols()) {
467+
for (const MCSymbol &S : Asm->symbols()) {
468468
const auto &WS = static_cast<const MCSymbolWasm &>(S);
469469
if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
470470
const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection());

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,8 @@ void WinCOFFObjectWriter::reset() {
11731173
}
11741174

11751175
bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1176-
const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
1177-
bool InSet, bool IsPCRel) const {
1176+
const MCSymbol &SymA, const MCFragment &FB, bool InSet,
1177+
bool IsPCRel) const {
11781178
// Don't drop relocations between functions, even if they are in the same text
11791179
// section. Multiple Visual C++ linker features depend on having the
11801180
// relocations present. The /INCREMENTAL flag will cause these relocations to
@@ -1187,10 +1187,10 @@ bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
11871187
return &SymA.getSection() == FB.getParent();
11881188
}
11891189

1190-
void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
1191-
ObjWriter->executePostLayoutBinding(Asm);
1190+
void WinCOFFObjectWriter::executePostLayoutBinding() {
1191+
ObjWriter->executePostLayoutBinding(*Asm);
11921192
if (DwoWriter)
1193-
DwoWriter->executePostLayoutBinding(Asm);
1193+
DwoWriter->executePostLayoutBinding(*Asm);
11941194
}
11951195

11961196
void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,

llvm/lib/MC/XCOFFObjectWriter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class XCOFFWriter final : public XCOFFObjectWriter {
348348

349349
void reset() override;
350350

351-
void executePostLayoutBinding(MCAssembler &) override;
351+
void executePostLayoutBinding() override;
352352

353353
void recordRelocation(MCAssembler &, const MCFragment *, const MCFixup &,
354354
MCValue, uint64_t &) override;
@@ -555,8 +555,8 @@ static MCSectionXCOFF *getContainingCsect(const MCSymbolXCOFF *XSym) {
555555
return XSym->getRepresentedCsect();
556556
}
557557

558-
void XCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
559-
for (const auto &S : Asm) {
558+
void XCOFFWriter::executePostLayoutBinding() {
559+
for (const auto &S : *Asm) {
560560
const auto *MCSec = cast<const MCSectionXCOFF>(&S);
561561
assert(!SectionMap.contains(MCSec) && "Cannot add a section twice.");
562562

@@ -587,7 +587,7 @@ void XCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
587587
llvm_unreachable("unsupport section type!");
588588
}
589589

590-
for (const MCSymbol &S : Asm.symbols()) {
590+
for (const MCSymbol &S : Asm->symbols()) {
591591
// Nothing to do for temporary symbols.
592592
if (S.isTemporary())
593593
continue;
@@ -653,7 +653,7 @@ void XCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
653653
Strings.add(Vers);
654654

655655
Strings.finalize();
656-
assignAddressesAndIndices(Asm);
656+
assignAddressesAndIndices(*Asm);
657657
}
658658

659659
void XCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,

0 commit comments

Comments
 (0)