@@ -83,9 +83,12 @@ STATISTIC(PaddingFragmentsBytes,
83
83
84
84
/* *** */
85
85
86
- MCAssembler::MCAssembler (MCContext &Context, MCAsmBackend &Backend,
87
- MCCodeEmitter &Emitter, MCObjectWriter &Writer)
88
- : Context(Context), Backend(Backend), Emitter(Emitter), Writer(Writer),
86
+ MCAssembler::MCAssembler (MCContext &Context,
87
+ std::unique_ptr<MCAsmBackend> Backend,
88
+ std::unique_ptr<MCCodeEmitter> Emitter,
89
+ std::unique_ptr<MCObjectWriter> Writer)
90
+ : Context(Context), Backend(std::move(Backend)),
91
+ Emitter(std::move(Emitter)), Writer(std::move(Writer)),
89
92
BundleAlignSize(0 ), RelaxAll(false ), SubsectionsViaSymbols(false ),
90
93
IncrementalLinkerCompatible(false ), ELFHeaderEFlags(0 ) {
91
94
VersionInfo.Major = 0 ; // Major version == 0 for "none specified"
@@ -110,9 +113,12 @@ void MCAssembler::reset() {
110
113
VersionInfo.Major = 0 ;
111
114
112
115
// reset objects owned by us
113
- getBackend ().reset ();
114
- getEmitter ().reset ();
115
- getWriter ().reset ();
116
+ if (getBackendPtr ())
117
+ getBackendPtr ()->reset ();
118
+ if (getEmitterPtr ())
119
+ getEmitterPtr ()->reset ();
120
+ if (getWriterPtr ())
121
+ getWriterPtr ()->reset ();
116
122
getLOHContainer ().reset ();
117
123
}
118
124
@@ -215,8 +221,9 @@ bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
215
221
}
216
222
}
217
223
218
- bool IsPCRel = Backend.getFixupKindInfo (
219
- Fixup.getKind ()).Flags & MCFixupKindInfo::FKF_IsPCRel;
224
+ assert (getBackendPtr () && " Expected assembler backend" );
225
+ bool IsPCRel = getBackendPtr ()->getFixupKindInfo (Fixup.getKind ()).Flags &
226
+ MCFixupKindInfo::FKF_IsPCRel;
220
227
221
228
bool IsResolved;
222
229
if (IsPCRel) {
@@ -251,8 +258,8 @@ bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
251
258
Value -= Layout.getSymbolOffset (Sym);
252
259
}
253
260
254
- bool ShouldAlignPC = Backend .getFixupKindInfo (Fixup.getKind ()).Flags &
255
- MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
261
+ bool ShouldAlignPC = getBackend () .getFixupKindInfo (Fixup.getKind ()).Flags &
262
+ MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
256
263
assert ((ShouldAlignPC ? IsPCRel : true ) &&
257
264
" FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!" );
258
265
@@ -266,14 +273,15 @@ bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
266
273
}
267
274
268
275
// Let the backend force a relocation if needed.
269
- if (IsResolved && Backend .shouldForceRelocation (*this , Fixup, Target))
276
+ if (IsResolved && getBackend () .shouldForceRelocation (*this , Fixup, Target))
270
277
IsResolved = false ;
271
278
272
279
return IsResolved;
273
280
}
274
281
275
282
uint64_t MCAssembler::computeFragmentSize (const MCAsmLayout &Layout,
276
283
const MCFragment &F) const {
284
+ assert (getBackendPtr () && " Requires assembler backend" );
277
285
switch (F.getKind ()) {
278
286
case MCFragment::FT_Data:
279
287
return cast<MCDataFragment>(F).getContents ().size ();
@@ -437,6 +445,7 @@ void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) {
437
445
438
446
void MCAssembler::writeFragmentPadding (const MCFragment &F, uint64_t FSize,
439
447
MCObjectWriter *OW) const {
448
+ assert (getBackendPtr () && " Expected assembler backend" );
440
449
// Should NOP padding be written out before this fragment?
441
450
unsigned BundlePadding = F.getBundlePadding ();
442
451
if (BundlePadding > 0 ) {
@@ -470,7 +479,8 @@ void MCAssembler::writeFragmentPadding(const MCFragment &F, uint64_t FSize,
470
479
// / \brief Write the fragment \p F to the output file.
471
480
static void writeFragment (const MCAssembler &Asm, const MCAsmLayout &Layout,
472
481
const MCFragment &F) {
473
- MCObjectWriter *OW = &Asm.getWriter ();
482
+ MCObjectWriter *OW = Asm.getWriterPtr ();
483
+ assert (OW && " Need ObjectWriter to write fragment" );
474
484
475
485
// FIXME: Embed in fragments instead?
476
486
uint64_t FragmentSize = Asm.computeFragmentSize (Layout, F);
@@ -619,6 +629,8 @@ static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
619
629
620
630
void MCAssembler::writeSectionData (const MCSection *Sec,
621
631
const MCAsmLayout &Layout) const {
632
+ assert (getBackendPtr () && " Expected assembler backend" );
633
+
622
634
// Ignore virtual sections.
623
635
if (Sec->isVirtualSection ()) {
624
636
assert (Layout.getSectionFileSize (Sec) == 0 && " Invalid size for section!" );
@@ -688,6 +700,7 @@ MCAssembler::handleFixup(const MCAsmLayout &Layout, MCFragment &F,
688
700
}
689
701
690
702
void MCAssembler::layout (MCAsmLayout &Layout) {
703
+ assert (getBackendPtr () && " Expected assembler backend" );
691
704
DEBUG_WITH_TYPE (" mc-dump" , {
692
705
errs () << " assembler backend - pre-layout\n --\n " ;
693
706
dump (); });
@@ -788,6 +801,7 @@ void MCAssembler::Finish() {
788
801
bool MCAssembler::fixupNeedsRelaxation (const MCFixup &Fixup,
789
802
const MCRelaxableFragment *DF,
790
803
const MCAsmLayout &Layout) const {
804
+ assert (getBackendPtr () && " Expected assembler backend" );
791
805
MCValue Target;
792
806
uint64_t Value;
793
807
bool Resolved = evaluateFixup (Layout, Fixup, DF, Target, Value);
@@ -801,6 +815,7 @@ bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
801
815
802
816
bool MCAssembler::fragmentNeedsRelaxation (const MCRelaxableFragment *F,
803
817
const MCAsmLayout &Layout) const {
818
+ assert (getBackendPtr () && " Expected assembler backend" );
804
819
// If this inst doesn't ever need relaxation, ignore it. This occurs when we
805
820
// are intentionally pushing out inst fragments, or because we relaxed a
806
821
// previous instruction to one that doesn't need relaxation.
@@ -816,6 +831,8 @@ bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
816
831
817
832
bool MCAssembler::relaxInstruction (MCAsmLayout &Layout,
818
833
MCRelaxableFragment &F) {
834
+ assert (getEmitterPtr () &&
835
+ " Expected CodeEmitter defined for relaxInstruction" );
819
836
if (!fragmentNeedsRelaxation (&F, Layout))
820
837
return false ;
821
838
@@ -848,6 +865,7 @@ bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
848
865
849
866
bool MCAssembler::relaxPaddingFragment (MCAsmLayout &Layout,
850
867
MCPaddingFragment &PF) {
868
+ assert (getBackendPtr () && " Expected assembler backend" );
851
869
uint64_t OldSize = PF.getSize ();
852
870
if (!getBackend ().relaxFragment (&PF, Layout))
853
871
return false ;
@@ -992,6 +1010,7 @@ bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
992
1010
}
993
1011
994
1012
void MCAssembler::finishLayout (MCAsmLayout &Layout) {
1013
+ assert (getBackendPtr () && " Expected assembler backend" );
995
1014
// The layout is done. Mark every fragment as valid.
996
1015
for (unsigned int i = 0 , n = Layout.getSectionOrder ().size (); i != n; ++i) {
997
1016
MCSection &Section = *Layout.getSectionOrder ()[i];
0 commit comments