Skip to content

Commit 10c894c

Browse files
committed
[MC] Move MCAsmLayout from MCFragment.cpp to MCAssembler.cpp. NFC
8d73623 (2015) moved these MCAsmLayout functions to MCFragment.cpp, but the original placement is better as these functions are tightly coupled with MCAssembler.cpp.
1 parent 038bc1c commit 10c894c

File tree

2 files changed

+127
-132
lines changed

2 files changed

+127
-132
lines changed

llvm/lib/MC/MCAssembler.cpp

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
383383
llvm_unreachable("invalid fragment kind");
384384
}
385385

386+
MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
387+
// Compute the section layout order. Virtual sections must go last.
388+
for (MCSection &Sec : Asm)
389+
if (!Sec.isVirtualSection())
390+
SectionOrder.push_back(&Sec);
391+
for (MCSection &Sec : Asm)
392+
if (Sec.isVirtualSection())
393+
SectionOrder.push_back(&Sec);
394+
}
395+
386396
// Compute the amount of padding required before the fragment \p F to
387397
// obey bundling restrictions, where \p FOffset is the fragment's offset in
388398
// its section and \p FSize is the fragment's size.
@@ -464,11 +474,6 @@ void MCAsmLayout::layoutBundle(MCFragment *Prev, MCFragment *F) {
464474
DF->Offset = EF->Offset;
465475
}
466476

467-
uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
468-
ensureValid(F);
469-
return F->Offset;
470-
}
471-
472477
void MCAsmLayout::ensureValid(const MCFragment *Frag) const {
473478
MCSection &Sec = *Frag->getParent();
474479
if (Sec.hasLayout())
@@ -487,6 +492,123 @@ void MCAsmLayout::ensureValid(const MCFragment *Frag) const {
487492
}
488493
}
489494

495+
uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
496+
ensureValid(F);
497+
return F->Offset;
498+
}
499+
500+
// Simple getSymbolOffset helper for the non-variable case.
501+
static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
502+
bool ReportError, uint64_t &Val) {
503+
if (!S.getFragment()) {
504+
if (ReportError)
505+
report_fatal_error("unable to evaluate offset to undefined symbol '" +
506+
S.getName() + "'");
507+
return false;
508+
}
509+
Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
510+
return true;
511+
}
512+
513+
static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
514+
bool ReportError, uint64_t &Val) {
515+
if (!S.isVariable())
516+
return getLabelOffset(Layout, S, ReportError, Val);
517+
518+
// If SD is a variable, evaluate it.
519+
MCValue Target;
520+
if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
521+
report_fatal_error("unable to evaluate offset for variable '" +
522+
S.getName() + "'");
523+
524+
uint64_t Offset = Target.getConstant();
525+
526+
const MCSymbolRefExpr *A = Target.getSymA();
527+
if (A) {
528+
uint64_t ValA;
529+
// FIXME: On most platforms, `Target`'s component symbols are labels from
530+
// having been simplified during evaluation, but on Mach-O they can be
531+
// variables due to PR19203. This, and the line below for `B` can be
532+
// restored to call `getLabelOffset` when PR19203 is fixed.
533+
if (!getSymbolOffsetImpl(Layout, A->getSymbol(), ReportError, ValA))
534+
return false;
535+
Offset += ValA;
536+
}
537+
538+
const MCSymbolRefExpr *B = Target.getSymB();
539+
if (B) {
540+
uint64_t ValB;
541+
if (!getSymbolOffsetImpl(Layout, B->getSymbol(), ReportError, ValB))
542+
return false;
543+
Offset -= ValB;
544+
}
545+
546+
Val = Offset;
547+
return true;
548+
}
549+
550+
bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
551+
return getSymbolOffsetImpl(*this, S, false, Val);
552+
}
553+
554+
uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
555+
uint64_t Val;
556+
getSymbolOffsetImpl(*this, S, true, Val);
557+
return Val;
558+
}
559+
560+
const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
561+
if (!Symbol.isVariable())
562+
return &Symbol;
563+
564+
const MCExpr *Expr = Symbol.getVariableValue();
565+
MCValue Value;
566+
if (!Expr->evaluateAsValue(Value, *this)) {
567+
Assembler.getContext().reportError(Expr->getLoc(),
568+
"expression could not be evaluated");
569+
return nullptr;
570+
}
571+
572+
const MCSymbolRefExpr *RefB = Value.getSymB();
573+
if (RefB) {
574+
Assembler.getContext().reportError(
575+
Expr->getLoc(),
576+
Twine("symbol '") + RefB->getSymbol().getName() +
577+
"' could not be evaluated in a subtraction expression");
578+
return nullptr;
579+
}
580+
581+
const MCSymbolRefExpr *A = Value.getSymA();
582+
if (!A)
583+
return nullptr;
584+
585+
const MCSymbol &ASym = A->getSymbol();
586+
const MCAssembler &Asm = getAssembler();
587+
if (ASym.isCommon()) {
588+
Asm.getContext().reportError(Expr->getLoc(),
589+
"Common symbol '" + ASym.getName() +
590+
"' cannot be used in assignment expr");
591+
return nullptr;
592+
}
593+
594+
return &ASym;
595+
}
596+
597+
uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
598+
// The size is the last fragment's end offset.
599+
const MCFragment &F = *Sec->curFragList()->Tail;
600+
return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
601+
}
602+
603+
uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
604+
// Virtual sections have no file size.
605+
if (Sec->isVirtualSection())
606+
return 0;
607+
608+
// Otherwise, the file size is the same as the address space size.
609+
return getSectionAddressSize(Sec);
610+
}
611+
490612
bool MCAssembler::registerSymbol(const MCSymbol &Symbol) {
491613
bool Changed = !Symbol.isRegistered();
492614
if (Changed) {

llvm/lib/MC/MCFragment.cpp

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
#include "llvm/ADT/StringExtras.h"
1212
#include "llvm/ADT/Twine.h"
1313
#include "llvm/Config/llvm-config.h"
14-
#include "llvm/MC/MCAsmLayout.h"
15-
#include "llvm/MC/MCAssembler.h"
1614
#include "llvm/MC/MCContext.h"
17-
#include "llvm/MC/MCExpr.h"
1815
#include "llvm/MC/MCFixup.h"
1916
#include "llvm/MC/MCSection.h"
2017
#include "llvm/MC/MCSectionMachO.h"
2118
#include "llvm/MC/MCSymbol.h"
22-
#include "llvm/MC/MCValue.h"
2319
#include "llvm/Support/Casting.h"
2420
#include "llvm/Support/Compiler.h"
2521
#include "llvm/Support/ErrorHandling.h"
@@ -30,129 +26,6 @@
3026

3127
using namespace llvm;
3228

33-
MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
34-
// Compute the section layout order. Virtual sections must go last.
35-
for (MCSection &Sec : Asm)
36-
if (!Sec.isVirtualSection())
37-
SectionOrder.push_back(&Sec);
38-
for (MCSection &Sec : Asm)
39-
if (Sec.isVirtualSection())
40-
SectionOrder.push_back(&Sec);
41-
}
42-
43-
// Simple getSymbolOffset helper for the non-variable case.
44-
static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
45-
bool ReportError, uint64_t &Val) {
46-
if (!S.getFragment()) {
47-
if (ReportError)
48-
report_fatal_error("unable to evaluate offset to undefined symbol '" +
49-
S.getName() + "'");
50-
return false;
51-
}
52-
Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
53-
return true;
54-
}
55-
56-
static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
57-
bool ReportError, uint64_t &Val) {
58-
if (!S.isVariable())
59-
return getLabelOffset(Layout, S, ReportError, Val);
60-
61-
// If SD is a variable, evaluate it.
62-
MCValue Target;
63-
if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
64-
report_fatal_error("unable to evaluate offset for variable '" +
65-
S.getName() + "'");
66-
67-
uint64_t Offset = Target.getConstant();
68-
69-
const MCSymbolRefExpr *A = Target.getSymA();
70-
if (A) {
71-
uint64_t ValA;
72-
// FIXME: On most platforms, `Target`'s component symbols are labels from
73-
// having been simplified during evaluation, but on Mach-O they can be
74-
// variables due to PR19203. This, and the line below for `B` can be
75-
// restored to call `getLabelOffset` when PR19203 is fixed.
76-
if (!getSymbolOffsetImpl(Layout, A->getSymbol(), ReportError, ValA))
77-
return false;
78-
Offset += ValA;
79-
}
80-
81-
const MCSymbolRefExpr *B = Target.getSymB();
82-
if (B) {
83-
uint64_t ValB;
84-
if (!getSymbolOffsetImpl(Layout, B->getSymbol(), ReportError, ValB))
85-
return false;
86-
Offset -= ValB;
87-
}
88-
89-
Val = Offset;
90-
return true;
91-
}
92-
93-
bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
94-
return getSymbolOffsetImpl(*this, S, false, Val);
95-
}
96-
97-
uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
98-
uint64_t Val;
99-
getSymbolOffsetImpl(*this, S, true, Val);
100-
return Val;
101-
}
102-
103-
const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
104-
if (!Symbol.isVariable())
105-
return &Symbol;
106-
107-
const MCExpr *Expr = Symbol.getVariableValue();
108-
MCValue Value;
109-
if (!Expr->evaluateAsValue(Value, *this)) {
110-
Assembler.getContext().reportError(
111-
Expr->getLoc(), "expression could not be evaluated");
112-
return nullptr;
113-
}
114-
115-
const MCSymbolRefExpr *RefB = Value.getSymB();
116-
if (RefB) {
117-
Assembler.getContext().reportError(
118-
Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
119-
"' could not be evaluated in a subtraction expression");
120-
return nullptr;
121-
}
122-
123-
const MCSymbolRefExpr *A = Value.getSymA();
124-
if (!A)
125-
return nullptr;
126-
127-
const MCSymbol &ASym = A->getSymbol();
128-
const MCAssembler &Asm = getAssembler();
129-
if (ASym.isCommon()) {
130-
Asm.getContext().reportError(Expr->getLoc(),
131-
"Common symbol '" + ASym.getName() +
132-
"' cannot be used in assignment expr");
133-
return nullptr;
134-
}
135-
136-
return &ASym;
137-
}
138-
139-
uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
140-
// The size is the last fragment's end offset.
141-
const MCFragment &F = *Sec->curFragList()->Tail;
142-
return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
143-
}
144-
145-
uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
146-
// Virtual sections have no file size.
147-
if (Sec->isVirtualSection())
148-
return 0;
149-
150-
// Otherwise, the file size is the same as the address space size.
151-
return getSectionAddressSize(Sec);
152-
}
153-
154-
/* *** */
155-
15629
MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)
15730
: Kind(Kind), HasInstructions(HasInstructions), LinkerRelaxable(false) {}
15831

0 commit comments

Comments
 (0)