Skip to content

Commit 67957a4

Browse files
committed
[MC] Start merging MCAsmLayout into MCAssembler
Follow-up to 10c894c. MCAsmLayout, introduced by ac8a954 (2010), provides APIs to compute fragment/symbol/section offsets. The separate class is cumbersome and passing it around has overhead. Let's remove it as the underlying implementation is tightly coupled with MCAsmLayout anyway. Some forwarders are added to ease migration.
1 parent 3cf762b commit 67957a4

File tree

11 files changed

+203
-208
lines changed

11 files changed

+203
-208
lines changed

llvm/include/llvm/MC/MCAsmLayout.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@ class MCAsmLayout {
3131
/// List of sections in layout order.
3232
llvm::SmallVector<MCSection *, 16> SectionOrder;
3333

34-
/// Compute the layout for the section if necessary.
35-
void ensureValid(const MCFragment *F) const;
36-
3734
public:
3835
MCAsmLayout(MCAssembler &Assembler);
3936

4037
/// Get the assembler object this is a layout for.
4138
MCAssembler &getAssembler() const { return Assembler; }
4239

43-
void layoutBundle(MCFragment *Prev, MCFragment *F);
4440

4541
/// \name Section Access (in layout order)
4642
/// @{
@@ -70,11 +66,6 @@ class MCAsmLayout {
7066
/// file. This may include additional padding, or be 0 for virtual sections.
7167
uint64_t getSectionFileSize(const MCSection *Sec) const;
7268

73-
/// Get the offset of the given symbol, as computed in the current
74-
/// layout.
75-
/// \return True on success.
76-
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
77-
7869
/// Variant that reports a fatal error if the offset is not computable.
7970
uint64_t getSymbolOffset(const MCSymbol &S) const;
8071

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class MCAssembler {
118118
std::unique_ptr<MCCodeEmitter> Emitter;
119119
std::unique_ptr<MCObjectWriter> Writer;
120120

121+
MCAsmLayout *Layout = nullptr;
121122
bool RelaxAll = false;
122123
bool SubsectionsViaSymbols = false;
123124
bool IncrementalLinkerCompatible = false;
@@ -171,7 +172,6 @@ class MCAssembler {
171172
/// Evaluate a fixup to a relocatable expression and the value which should be
172173
/// placed into the fixup.
173174
///
174-
/// \param Layout The layout to use for evaluation.
175175
/// \param Fixup The fixup to evaluate.
176176
/// \param DF The fragment the fixup is inside.
177177
/// \param Target [out] On return, the relocatable expression the fixup
@@ -183,45 +183,38 @@ class MCAssembler {
183183
/// \return Whether the fixup value was fully resolved. This is true if the
184184
/// \p Value result is fixed, otherwise the value may change due to
185185
/// relocation.
186-
bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
187-
const MCFragment *DF, MCValue &Target,
188-
const MCSubtargetInfo *STI, uint64_t &Value,
189-
bool &WasForced) const;
186+
bool evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
187+
MCValue &Target, const MCSubtargetInfo *STI,
188+
uint64_t &Value, bool &WasForced) const;
190189

191190
/// Check whether a fixup can be satisfied, or whether it needs to be relaxed
192191
/// (increased in size, in order to hold its value correctly).
193-
bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
194-
const MCAsmLayout &Layout) const;
192+
bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF) const;
195193

196194
/// Check whether the given fragment needs relaxation.
197-
bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
198-
const MCAsmLayout &Layout) const;
195+
bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF) const;
199196

200197
/// Perform one layout iteration and return true if any offsets
201198
/// were adjusted.
202-
bool layoutOnce(MCAsmLayout &Layout);
199+
bool layoutOnce();
203200

204201
/// Perform relaxation on a single fragment - returns true if the fragment
205202
/// changes as a result of relaxation.
206-
bool relaxFragment(MCAsmLayout &Layout, MCFragment &F);
207-
bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
208-
bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
209-
bool relaxBoundaryAlign(MCAsmLayout &Layout, MCBoundaryAlignFragment &BF);
210-
bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
211-
bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
212-
MCDwarfCallFrameFragment &DF);
213-
bool relaxCVInlineLineTable(MCAsmLayout &Layout,
214-
MCCVInlineLineTableFragment &DF);
215-
bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
216-
bool relaxPseudoProbeAddr(MCAsmLayout &Layout, MCPseudoProbeAddrFragment &DF);
203+
bool relaxFragment(MCFragment &F);
204+
bool relaxInstruction(MCRelaxableFragment &IF);
205+
bool relaxLEB(MCLEBFragment &IF);
206+
bool relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
207+
bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF);
208+
bool relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF);
209+
bool relaxCVInlineLineTable(MCCVInlineLineTableFragment &DF);
210+
bool relaxCVDefRange(MCCVDefRangeFragment &DF);
211+
bool relaxPseudoProbeAddr(MCPseudoProbeAddrFragment &DF);
217212

218213
/// finishLayout - Finalize a layout, including fragment lowering.
219214
void finishLayout(MCAsmLayout &Layout);
220215

221-
std::tuple<MCValue, uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
222-
MCFragment &F,
223-
const MCFixup &Fixup,
224-
const MCSubtargetInfo *STI);
216+
std::tuple<MCValue, uint64_t, bool>
217+
handleFixup(MCFragment &F, const MCFixup &Fixup, const MCSubtargetInfo *STI);
225218

226219
public:
227220
struct Symver {
@@ -246,10 +239,28 @@ class MCAssembler {
246239
MCAssembler &operator=(const MCAssembler &) = delete;
247240
~MCAssembler();
248241

249-
/// Compute the effective fragment size assuming it is laid out at the given
250-
/// \p SectionAddress and \p FragmentOffset.
251-
uint64_t computeFragmentSize(const MCAsmLayout &Layout,
252-
const MCFragment &F) const;
242+
/// Compute the effective fragment size.
243+
uint64_t computeFragmentSize(const MCFragment &F) const;
244+
245+
void layoutBundle(MCFragment *Prev, MCFragment *F) const;
246+
void ensureValid(MCSection &Sec) const;
247+
248+
// Get the offset of the given fragment inside its containing section.
249+
uint64_t getFragmentOffset(const MCFragment &F) const;
250+
251+
uint64_t getSectionAddressSize(const MCSection &Sec) const;
252+
uint64_t getSectionFileSize(const MCSection &Sec) const;
253+
254+
// Get the offset of the given symbol, as computed in the current
255+
// layout.
256+
// \return True on success.
257+
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
258+
259+
// Variant that reports a fatal error if the offset is not computable.
260+
uint64_t getSymbolOffset(const MCSymbol &S) const;
261+
262+
// If this symbol is equivalent to A + Constant, return A.
263+
const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
253264

254265
/// Check whether a particular symbol is visible to the linker and is required
255266
/// in the symbol table, or whether it can be discarded by the assembler. This
@@ -349,6 +360,8 @@ class MCAssembler {
349360
IncrementalLinkerCompatible = Value;
350361
}
351362

363+
MCAsmLayout *getLayout() const { return Layout; }
364+
bool hasLayout() const { return Layout; }
352365
bool getRelaxAll() const { return RelaxAll; }
353366
void setRelaxAll(bool Value) { RelaxAll = Value; }
354367

llvm/include/llvm/MC/MCExpr.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class MCExpr {
6565
}
6666

6767
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
68-
const MCAsmLayout *Layout,
6968
const MCFixup *Fixup,
7069
const SectionAddrMap *Addrs, bool InSet) const;
7170

@@ -96,11 +95,8 @@ class MCExpr {
9695
/// Try to evaluate the expression to an absolute value.
9796
///
9897
/// \param Res - The absolute value, if evaluation succeeds.
99-
/// \param Layout - The assembler layout object to use for evaluating symbol
100-
/// values. If not given, then only non-symbolic expressions will be
101-
/// evaluated.
10298
/// \return - True on success.
103-
bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
99+
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm,
104100
const SectionAddrMap &Addrs) const;
105101
bool evaluateAsAbsolute(int64_t &Res) const;
106102
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
@@ -124,7 +120,7 @@ class MCExpr {
124120
///
125121
/// This is a more aggressive variant of evaluateAsRelocatable. The intended
126122
/// use is for when relocations are not available, like the .size directive.
127-
bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
123+
bool evaluateAsValue(MCValue &Res, const MCAssembler &Asm) const;
128124

129125
/// Find the "associated section" for this expression, which is
130126
/// currently defined as the absolute section for constants, or

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct ELFWriter {
108108
DwoOnly,
109109
} Mode;
110110

111-
static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
111+
static uint64_t symbolValue(const MCSymbol &Sym, const MCAssembler &Asm);
112112
static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
113113
bool Used, bool Renamed);
114114

@@ -454,16 +454,15 @@ void ELFWriter::writeHeader(const MCAssembler &Asm) {
454454
W.write<uint16_t>(StringTableIndex);
455455
}
456456

457-
uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
458-
const MCAsmLayout &Layout) {
457+
uint64_t ELFWriter::symbolValue(const MCSymbol &Sym, const MCAssembler &Asm) {
459458
if (Sym.isCommon())
460459
return Sym.getCommonAlignment()->value();
461460

462461
uint64_t Res;
463-
if (!Layout.getSymbolOffset(Sym, Res))
462+
if (!Asm.getSymbolOffset(Sym, Res))
464463
return 0;
465464

466-
if (Layout.getAssembler().isThumbFunc(&Sym))
465+
if (Asm.isThumbFunc(&Sym))
467466
Res |= 1;
468467

469468
return Res;
@@ -542,7 +541,7 @@ void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
542541
uint8_t Visibility = Symbol.getVisibility();
543542
uint8_t Other = Symbol.getOther() | Visibility;
544543

545-
uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
544+
uint64_t Value = symbolValue(*MSD.Symbol, Layout.getAssembler());
546545
uint64_t Size = 0;
547546

548547
const MCExpr *ESize = MSD.Symbol->getSize();

0 commit comments

Comments
 (0)