1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "assembler"
11 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCAsmLayout.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Target/TargetRegistry.h"
26 #include "llvm/Target/TargetAsmBackend.h"
29 #include "../Target/X86/X86FixupKinds.h"
34 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
36 // FIXME FIXME FIXME: There are number of places in this file where we convert
37 // what is a 64-bit assembler value used for computation into a value in the
38 // object file, which may truncate it. We should detect that truncation where
39 // invalid and report errors back.
43 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
46 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
49 FileSize(~UINT64_C(0))
52 Parent->getFragmentList().push_back(this);
55 MCFragment::~MCFragment() {
58 uint64_t MCFragment::getAddress() const {
59 assert(getParent() && "Missing Section!");
60 return getParent()->getAddress() + Offset;
65 MCSectionData::MCSectionData() : Section(0) {}
67 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
70 Address(~UINT64_C(0)),
72 FileSize(~UINT64_C(0)),
73 HasInstructions(false)
76 A->getSectionList().push_back(this);
81 MCSymbolData::MCSymbolData() : Symbol(0) {}
83 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
84 uint64_t _Offset, MCAssembler *A)
85 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
86 IsExternal(false), IsPrivateExtern(false),
87 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
90 A->getSymbolList().push_back(this);
95 MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
96 MCCodeEmitter &_Emitter, raw_ostream &_OS)
97 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
98 OS(_OS), SubsectionsViaSymbols(false)
102 MCAssembler::~MCAssembler() {
105 static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
106 const MCAsmFixup &Fixup,
107 const MCValue Target,
108 const MCSection *BaseSection) {
109 // The effective fixup address is
110 // addr(atom(A)) + offset(A)
111 // - addr(atom(B)) - offset(B)
112 // - addr(<base symbol>) + <fixup offset from base symbol>
113 // and the offsets are not relocatable, so the fixup is fully resolved when
114 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
116 // The simple (Darwin, except on x86_64) way of dealing with this was to
117 // assume that any reference to a temporary symbol *must* be a temporary
118 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
119 // relocation to a temporary symbol (in the same section) is fully
120 // resolved. This also works in conjunction with absolutized .set, which
121 // requires the compiler to use .set to absolutize the differences between
122 // symbols which the compiler knows to be assembly time constants, so we don't
123 // need to worry about consider symbol differences fully resolved.
125 // Non-relative fixups are only resolved if constant.
127 return Target.isAbsolute();
129 // Otherwise, relative fixups are only resolved if not a difference and the
130 // target is a temporary in the same section.
131 if (Target.isAbsolute() || Target.getSymB())
134 const MCSymbol *A = &Target.getSymA()->getSymbol();
135 if (!A->isTemporary() || !A->isInSection() ||
136 &A->getSection() != BaseSection)
142 static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
143 const MCAsmFixup &Fixup,
144 const MCValue Target,
145 const MCSymbolData *BaseSymbol) {
146 // The effective fixup address is
147 // addr(atom(A)) + offset(A)
148 // - addr(atom(B)) - offset(B)
149 // - addr(BaseSymbol) + <fixup offset from base symbol>
150 // and the offsets are not relocatable, so the fixup is fully resolved when
151 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
153 // Note that "false" is almost always conservatively correct (it means we emit
154 // a relocation which is unnecessary), except when it would force us to emit a
155 // relocation which the target cannot encode.
157 const MCSymbolData *A_Base = 0, *B_Base = 0;
158 if (const MCSymbolRefExpr *A = Target.getSymA()) {
159 // Modified symbol references cannot be resolved.
160 if (A->getKind() != MCSymbolRefExpr::VK_None)
163 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
168 if (const MCSymbolRefExpr *B = Target.getSymB()) {
169 // Modified symbol references cannot be resolved.
170 if (B->getKind() != MCSymbolRefExpr::VK_None)
173 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
178 // If there is no base, A and B have to be the same atom for this fixup to be
181 return A_Base == B_Base;
183 // Otherwise, B must be missing and A must be the base.
184 return !B_Base && BaseSymbol == A_Base;
187 bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
188 // Non-temporary labels should always be visible to the linker.
189 if (!SD->getSymbol().isTemporary())
192 // Absolute temporary labels are never visible.
193 if (!SD->getFragment())
196 // Otherwise, check if the section requires symbols even for temporary labels.
197 return getBackend().doesSectionRequireSymbols(
198 SD->getFragment()->getParent()->getSection());
201 const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section,
202 uint64_t Address) const {
203 const MCSymbolData *Best = 0;
204 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
205 ie = symbol_end(); it != ie; ++it) {
206 // Ignore non-linker visible symbols.
207 if (!isSymbolLinkerVisible(it))
210 // Ignore symbols not in the same section.
211 if (!it->getFragment() || it->getFragment()->getParent() != Section)
214 // Otherwise, find the closest symbol preceding this address (ties are
215 // resolved in favor of the last defined symbol).
216 if (it->getAddress() <= Address &&
217 (!Best || it->getAddress() >= Best->getAddress()))
224 const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
225 // Linker visible symbols define atoms.
226 if (isSymbolLinkerVisible(SD))
229 // Absolute and undefined symbols have no defining atom.
230 if (!SD->getFragment())
233 // Otherwise, search by address.
234 return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress());
237 bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
238 const MCAsmFixup &Fixup, const MCFragment *DF,
239 MCValue &Target, uint64_t &Value) const {
240 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
241 llvm_report_error("expected relocatable expression");
243 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
244 // doesn't support small relocations, but then under what criteria does the
245 // assembler allow symbol differences?
247 Value = Target.getConstant();
250 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
251 bool IsResolved = true;
252 if (const MCSymbolRefExpr *A = Target.getSymA()) {
253 if (A->getSymbol().isDefined())
254 Value += getSymbolData(A->getSymbol()).getAddress();
258 if (const MCSymbolRefExpr *B = Target.getSymB()) {
259 if (B->getSymbol().isDefined())
260 Value -= getSymbolData(B->getSymbol()).getAddress();
265 // If we are using scattered symbols, determine whether this value is actually
266 // resolved; scattering may cause atoms to move.
267 if (IsResolved && getBackend().hasScatteredSymbols()) {
268 if (getBackend().hasReliableSymbolDifference()) {
269 // If this is a PCrel relocation, find the base atom (identified by its
270 // symbol) that the fixup value is relative to.
271 const MCSymbolData *BaseSymbol = 0;
273 BaseSymbol = getAtomForAddress(
274 DF->getParent(), DF->getAddress() + Fixup.Offset);
280 IsResolved = isScatteredFixupFullyResolved(*this, Fixup, Target,
283 const MCSection *BaseSection = 0;
285 BaseSection = &DF->getParent()->getSection();
287 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
293 Value -= DF->getAddress() + Fixup.Offset;
298 void MCAssembler::LayoutSection(MCSectionData &SD,
299 MCAsmLayout &Layout) {
300 uint64_t Address = SD.getAddress();
302 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
305 F.setOffset(Address - SD.getAddress());
307 // Evaluate fragment size.
308 switch (F.getKind()) {
309 case MCFragment::FT_Align: {
310 MCAlignFragment &AF = cast<MCAlignFragment>(F);
312 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
313 if (Size > AF.getMaxBytesToEmit())
316 AF.setFileSize(Size);
320 case MCFragment::FT_Data:
321 F.setFileSize(cast<MCDataFragment>(F).getContents().size());
324 case MCFragment::FT_Fill: {
325 MCFillFragment &FF = cast<MCFillFragment>(F);
326 F.setFileSize(FF.getValueSize() * FF.getCount());
330 case MCFragment::FT_Inst:
331 F.setFileSize(cast<MCInstFragment>(F).getInstSize());
334 case MCFragment::FT_Org: {
335 MCOrgFragment &OF = cast<MCOrgFragment>(F);
337 int64_t TargetLocation;
338 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
339 llvm_report_error("expected assembly-time absolute expression");
341 // FIXME: We need a way to communicate this error.
342 int64_t Offset = TargetLocation - F.getOffset();
344 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
345 "' (at offset '" + Twine(F.getOffset()) + "'");
347 F.setFileSize(Offset);
351 case MCFragment::FT_ZeroFill: {
352 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
354 // Align the fragment offset; it is safe to adjust the offset freely since
355 // this is only in virtual sections.
356 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
357 F.setOffset(Address - SD.getAddress());
359 // FIXME: This is misnamed.
360 F.setFileSize(ZFF.getSize());
365 Address += F.getFileSize();
368 // Set the section sizes.
369 SD.setSize(Address - SD.getAddress());
370 if (getBackend().isVirtualSection(SD.getSection()))
373 SD.setFileSize(Address - SD.getAddress());
376 /// WriteNopData - Write optimal nops to the output file for the \arg Count
377 /// bytes. This returns the number of bytes written. It may return 0 if
378 /// the \arg Count is more than the maximum optimal nops.
380 /// FIXME this is X86 32-bit specific and should move to a better place.
381 static uint64_t WriteNopData(uint64_t Count, MCObjectWriter *OW) {
382 static const uint8_t Nops[16][16] = {
390 {0x0f, 0x1f, 0x40, 0x00},
391 // nopl 0(%[re]ax,%[re]ax,1)
392 {0x0f, 0x1f, 0x44, 0x00, 0x00},
393 // nopw 0(%[re]ax,%[re]ax,1)
394 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
396 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
397 // nopl 0L(%[re]ax,%[re]ax,1)
398 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
399 // nopw 0L(%[re]ax,%[re]ax,1)
400 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
401 // nopw %cs:0L(%[re]ax,%[re]ax,1)
402 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
403 // nopl 0(%[re]ax,%[re]ax,1)
404 // nopw 0(%[re]ax,%[re]ax,1)
405 {0x0f, 0x1f, 0x44, 0x00, 0x00,
406 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
407 // nopw 0(%[re]ax,%[re]ax,1)
408 // nopw 0(%[re]ax,%[re]ax,1)
409 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
410 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
411 // nopw 0(%[re]ax,%[re]ax,1)
412 // nopl 0L(%[re]ax) */
413 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
414 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
417 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
418 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
420 // nopl 0L(%[re]ax,%[re]ax,1)
421 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
422 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}
428 for (uint64_t i = 0; i < Count; i++)
429 OW->Write8(uint8_t(Nops[Count - 1][i]));
434 /// WriteFragmentData - Write the \arg F data to the output file.
435 static void WriteFragmentData(const MCFragment &F, MCObjectWriter *OW) {
436 uint64_t Start = OW->getStream().tell();
441 // FIXME: Embed in fragments instead?
442 switch (F.getKind()) {
443 case MCFragment::FT_Align: {
444 MCAlignFragment &AF = cast<MCAlignFragment>(F);
445 uint64_t Count = AF.getFileSize() / AF.getValueSize();
447 // FIXME: This error shouldn't actually occur (the front end should emit
448 // multiple .align directives to enforce the semantics it wants), but is
449 // severe enough that we want to report it. How to handle this?
450 if (Count * AF.getValueSize() != AF.getFileSize())
451 llvm_report_error("undefined .align directive, value size '" +
452 Twine(AF.getValueSize()) +
453 "' is not a divisor of padding size '" +
454 Twine(AF.getFileSize()) + "'");
456 // See if we are aligning with nops, and if so do that first to try to fill
457 // the Count bytes. Then if that did not fill any bytes or there are any
458 // bytes left to fill use the the Value and ValueSize to fill the rest.
459 if (AF.getEmitNops()) {
460 uint64_t NopByteCount = WriteNopData(Count, OW);
461 Count -= NopByteCount;
464 for (uint64_t i = 0; i != Count; ++i) {
465 switch (AF.getValueSize()) {
467 assert(0 && "Invalid size!");
468 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
469 case 2: OW->Write16(uint16_t(AF.getValue())); break;
470 case 4: OW->Write32(uint32_t(AF.getValue())); break;
471 case 8: OW->Write64(uint64_t(AF.getValue())); break;
477 case MCFragment::FT_Data: {
478 MCDataFragment &DF = cast<MCDataFragment>(F);
479 assert(DF.getFileSize() == DF.getContents().size() && "Invalid size!");
480 OW->WriteBytes(DF.getContents().str());
484 case MCFragment::FT_Fill: {
485 MCFillFragment &FF = cast<MCFillFragment>(F);
486 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
487 switch (FF.getValueSize()) {
489 assert(0 && "Invalid size!");
490 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
491 case 2: OW->Write16(uint16_t(FF.getValue())); break;
492 case 4: OW->Write32(uint32_t(FF.getValue())); break;
493 case 8: OW->Write64(uint64_t(FF.getValue())); break;
499 case MCFragment::FT_Inst:
500 llvm_unreachable("unexpected inst fragment after lowering");
503 case MCFragment::FT_Org: {
504 MCOrgFragment &OF = cast<MCOrgFragment>(F);
506 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
507 OW->Write8(uint8_t(OF.getValue()));
512 case MCFragment::FT_ZeroFill: {
513 assert(0 && "Invalid zero fill fragment in concrete section!");
518 assert(OW->getStream().tell() - Start == F.getFileSize());
521 void MCAssembler::WriteSectionData(const MCSectionData *SD,
522 MCObjectWriter *OW) const {
523 // Ignore virtual sections.
524 if (getBackend().isVirtualSection(SD->getSection())) {
525 assert(SD->getFileSize() == 0);
529 uint64_t Start = OW->getStream().tell();
532 for (MCSectionData::const_iterator it = SD->begin(),
533 ie = SD->end(); it != ie; ++it)
534 WriteFragmentData(*it, OW);
536 // Add section padding.
537 assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!");
538 OW->WriteZeros(SD->getFileSize() - SD->getSize());
540 assert(OW->getStream().tell() - Start == SD->getFileSize());
543 void MCAssembler::Finish() {
544 DEBUG_WITH_TYPE("mc-dump", {
545 llvm::errs() << "assembler backend - pre-layout\n--\n";
548 // Layout until everything fits.
549 MCAsmLayout Layout(*this);
550 while (LayoutOnce(Layout))
553 DEBUG_WITH_TYPE("mc-dump", {
554 llvm::errs() << "assembler backend - post-relaxation\n--\n";
557 // Finalize the layout, including fragment lowering.
558 FinishLayout(Layout);
560 DEBUG_WITH_TYPE("mc-dump", {
561 llvm::errs() << "assembler backend - final-layout\n--\n";
564 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
566 llvm_report_error("unable to create object writer!");
568 // Allow the object writer a chance to perform post-layout binding (for
569 // example, to set the index fields in the symbol data).
570 Writer->ExecutePostLayoutBinding(*this);
572 // Evaluate and apply the fixups, generating relocation entries as necessary.
573 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
574 for (MCSectionData::iterator it2 = it->begin(),
575 ie2 = it->end(); it2 != ie2; ++it2) {
576 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
580 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
581 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
582 MCAsmFixup &Fixup = *it3;
584 // Evaluate the fixup.
587 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
588 // The fixup was unresolved, we need a relocation. Inform the object
589 // writer of the relocation, and give it an opportunity to adjust the
590 // fixup value if need be.
591 Writer->RecordRelocation(*this, DF, Fixup, Target, FixedValue);
594 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
599 // Write the object file.
600 Writer->WriteObject(*this);
604 bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
605 const MCFragment *DF,
606 const MCAsmLayout &Layout) const {
607 // Currently we only need to relax X86::reloc_pcrel_1byte.
608 if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte)
611 // If we cannot resolve the fixup value, it requires relaxation.
614 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
617 // Otherwise, relax if the value is too big for a (signed) i8.
618 return int64_t(Value) != int64_t(int8_t(Value));
621 bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
622 // Layout the concrete sections and fragments.
623 uint64_t Address = 0;
624 MCSectionData *Prev = 0;
625 for (iterator it = begin(), ie = end(); it != ie; ++it) {
626 MCSectionData &SD = *it;
628 // Skip virtual sections.
629 if (getBackend().isVirtualSection(SD.getSection()))
632 // Align this section if necessary by adding padding bytes to the previous
634 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
635 assert(Prev && "Missing prev section!");
636 Prev->setFileSize(Prev->getFileSize() + Pad);
640 // Layout the section fragments and its size.
641 SD.setAddress(Address);
642 LayoutSection(SD, Layout);
643 Address += SD.getFileSize();
648 // Layout the virtual sections.
649 for (iterator it = begin(), ie = end(); it != ie; ++it) {
650 MCSectionData &SD = *it;
652 if (!getBackend().isVirtualSection(SD.getSection()))
655 // Align this section if necessary by adding padding bytes to the previous
657 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
660 SD.setAddress(Address);
661 LayoutSection(SD, Layout);
662 Address += SD.getSize();
665 // Scan the fixups in order and relax any that don't fit.
666 for (iterator it = begin(), ie = end(); it != ie; ++it) {
667 MCSectionData &SD = *it;
669 for (MCSectionData::iterator it2 = SD.begin(),
670 ie2 = SD.end(); it2 != ie2; ++it2) {
671 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
675 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
676 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
677 MCAsmFixup &Fixup = *it3;
679 // Check whether we need to relax this fixup.
680 if (!FixupNeedsRelaxation(Fixup, DF, Layout))
683 // Relax the instruction.
685 // FIXME: This is a huge temporary hack which just looks for x86
686 // branches; the only thing we need to relax on x86 is
687 // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be
688 // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax
689 // an individual MCInst.
690 SmallVectorImpl<char> &C = DF->getContents();
691 uint64_t PrevOffset = Fixup.Offset;
695 if (unsigned(C[Fixup.Offset-1]) >= 0x70 &&
696 unsigned(C[Fixup.Offset-1]) <= 0x7f) {
697 C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10;
698 C[Fixup.Offset-1] = char(0x0f);
703 } else if (C[Fixup.Offset-1] == char(0xeb)) {
704 C[Fixup.Offset-1] = char(0xe9);
708 llvm_unreachable("unknown 1 byte pcrel instruction!");
710 Fixup.Value = MCBinaryExpr::Create(
711 MCBinaryExpr::Sub, Fixup.Value,
712 MCConstantExpr::Create(3, getContext()),
714 C.insert(C.begin() + Fixup.Offset, Amt, char(0));
715 Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte);
717 // Update the remaining fixups, which have slid.
719 // FIXME: This is bad for performance, but will be eliminated by the
720 // move to MCInst specific fragments.
722 for (; it3 != ie3; ++it3)
725 // Update all the symbols for this fragment, which may have slid.
727 // FIXME: This is really really bad for performance, but will be
728 // eliminated by the move to MCInst specific fragments.
729 for (MCAssembler::symbol_iterator it = symbol_begin(),
730 ie = symbol_end(); it != ie; ++it) {
731 MCSymbolData &SD = *it;
733 if (it->getFragment() != DF)
736 if (SD.getOffset() > PrevOffset)
737 SD.setOffset(SD.getOffset() + Amt);
742 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
743 // smart MCAsmLayout object.
752 void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
753 // Lower out any instruction fragments, to simplify the fixup application and
756 // FIXME-PERF: We don't have to do this, but the assumption is that it is
757 // cheap (we will mostly end up eliminating fragments and appending on to data
758 // fragments), so the extra complexity downstream isn't worth it. Evaluate
760 for (iterator it = begin(), ie = end(); it != ie; ++it) {
761 MCSectionData &SD = *it;
763 for (MCSectionData::iterator it2 = SD.begin(),
764 ie2 = SD.end(); it2 != ie2; ++it2) {
765 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
769 // Create a new data fragment for the instruction.
771 // FIXME: Reuse previous data fragment if possible.
772 MCDataFragment *DF = new MCDataFragment();
773 SD.getFragmentList().insert(it2, DF);
775 // Update the data fragments layout data.
776 DF->setParent(IF->getParent());
777 DF->setOffset(IF->getOffset());
778 DF->setFileSize(IF->getInstSize());
780 // Copy in the data and the fixups.
781 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
782 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
783 DF->getFixups().push_back(IF->getFixups()[i]);
785 // Delete the instruction fragment and update the iterator.
786 SD.getFragmentList().erase(IF);
796 raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
797 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
798 << " Kind:" << AF.Kind << ">";
804 void MCFragment::dump() {
805 raw_ostream &OS = llvm::errs();
807 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
808 << " FileSize:" << FileSize;
813 void MCAlignFragment::dump() {
814 raw_ostream &OS = llvm::errs();
816 OS << "<MCAlignFragment ";
817 this->MCFragment::dump();
819 OS << " Alignment:" << getAlignment()
820 << " Value:" << getValue() << " ValueSize:" << getValueSize()
821 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
824 void MCDataFragment::dump() {
825 raw_ostream &OS = llvm::errs();
827 OS << "<MCDataFragment ";
828 this->MCFragment::dump();
831 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
833 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
835 OS << "] (" << getContents().size() << " bytes)";
837 if (!getFixups().empty()) {
840 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
841 if (it != fixup_begin()) OS << ",\n ";
850 void MCFillFragment::dump() {
851 raw_ostream &OS = llvm::errs();
853 OS << "<MCFillFragment ";
854 this->MCFragment::dump();
856 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
857 << " Count:" << getCount() << ">";
860 void MCInstFragment::dump() {
861 raw_ostream &OS = llvm::errs();
863 OS << "<MCInstFragment ";
864 this->MCFragment::dump();
867 getInst().dump_pretty(OS);
871 void MCOrgFragment::dump() {
872 raw_ostream &OS = llvm::errs();
874 OS << "<MCOrgFragment ";
875 this->MCFragment::dump();
877 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
880 void MCZeroFillFragment::dump() {
881 raw_ostream &OS = llvm::errs();
883 OS << "<MCZeroFillFragment ";
884 this->MCFragment::dump();
886 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
889 void MCSectionData::dump() {
890 raw_ostream &OS = llvm::errs();
892 OS << "<MCSectionData";
893 OS << " Alignment:" << getAlignment() << " Address:" << Address
894 << " Size:" << Size << " FileSize:" << FileSize
895 << " Fragments:[\n ";
896 for (iterator it = begin(), ie = end(); it != ie; ++it) {
897 if (it != begin()) OS << ",\n ";
903 void MCSymbolData::dump() {
904 raw_ostream &OS = llvm::errs();
906 OS << "<MCSymbolData Symbol:" << getSymbol()
907 << " Fragment:" << getFragment() << " Offset:" << getOffset()
908 << " Flags:" << getFlags() << " Index:" << getIndex();
910 OS << " (common, size:" << getCommonSize()
911 << " align: " << getCommonAlignment() << ")";
914 if (isPrivateExtern())
915 OS << " (private extern)";
919 void MCAssembler::dump() {
920 raw_ostream &OS = llvm::errs();
922 OS << "<MCAssembler\n";
923 OS << " Sections:[\n ";
924 for (iterator it = begin(), ie = end(); it != ie; ++it) {
925 if (it != begin()) OS << ",\n ";
931 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
932 if (it != symbol_begin()) OS << ",\n ";