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/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/TargetRegistry.h"
29 #include "llvm/Target/TargetAsmBackend.h"
36 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
37 STATISTIC(EvaluateFixup, "Number of evaluated fixups");
38 STATISTIC(FragmentLayouts, "Number of fragment layouts");
39 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
40 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
41 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
45 // FIXME FIXME FIXME: There are number of places in this file where we convert
46 // what is a 64-bit assembler value used for computation into a value in the
47 // object file, which may truncate it. We should detect that truncation where
48 // invalid and report errors back.
52 MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
53 : Assembler(Asm), LastValidFragment()
55 // Compute the section layout order. Virtual sections must go last.
56 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
57 if (!it->getSection().isVirtualSection())
58 SectionOrder.push_back(&*it);
59 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
60 if (it->getSection().isVirtualSection())
61 SectionOrder.push_back(&*it);
64 bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const {
65 const MCSectionData &SD = *F->getParent();
66 const MCFragment *LastValid = LastValidFragment.lookup(&SD);
69 assert(LastValid->getParent() == F->getParent());
70 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
73 void MCAsmLayout::Invalidate(MCFragment *F) {
74 // If this fragment wasn't already up-to-date, we don't need to do anything.
75 if (!isFragmentUpToDate(F))
78 // Otherwise, reset the last valid fragment to this fragment.
79 const MCSectionData &SD = *F->getParent();
80 LastValidFragment[&SD] = F;
83 void MCAsmLayout::EnsureValid(const MCFragment *F) const {
84 MCSectionData &SD = *F->getParent();
86 MCFragment *Cur = LastValidFragment[&SD];
90 Cur = Cur->getNextNode();
92 // Advance the layout position until the fragment is up-to-date.
93 while (!isFragmentUpToDate(F)) {
94 const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur);
95 Cur = Cur->getNextNode();
99 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
101 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
105 uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
106 assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
107 return getFragmentOffset(SD->getFragment()) + SD->getOffset();
110 uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
111 // The size is the last fragment's end offset.
112 const MCFragment &F = SD->getFragmentList().back();
113 return getFragmentOffset(&F) + getAssembler().ComputeFragmentSize(*this, F);
116 uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
117 // Virtual sections have no file size.
118 if (SD->getSection().isVirtualSection())
121 // Otherwise, the file size is the same as the address space size.
122 return getSectionAddressSize(SD);
127 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
130 MCFragment::~MCFragment() {
133 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
134 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
137 Parent->getFragmentList().push_back(this);
142 MCSectionData::MCSectionData() : Section(0) {}
144 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
145 : Section(&_Section),
146 Ordinal(~UINT32_C(0)),
148 HasInstructions(false)
151 A->getSectionList().push_back(this);
156 MCSymbolData::MCSymbolData() : Symbol(0) {}
158 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
159 uint64_t _Offset, MCAssembler *A)
160 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
161 IsExternal(false), IsPrivateExtern(false),
162 CommonSize(0), SymbolSize(0), CommonAlign(0),
166 A->getSymbolList().push_back(this);
171 MCAssembler::MCAssembler(MCContext &Context_, TargetAsmBackend &Backend_,
172 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
174 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
175 OS(OS_), RelaxAll(false), SubsectionsViaSymbols(false)
179 MCAssembler::~MCAssembler() {
182 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
183 // Non-temporary labels should always be visible to the linker.
184 if (!Symbol.isTemporary())
187 // Absolute temporary labels are never visible.
188 if (!Symbol.isInSection())
191 // Otherwise, check if the section requires symbols even for temporary labels.
192 return getBackend().doesSectionRequireSymbols(Symbol.getSection());
195 const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
196 // Linker visible symbols define atoms.
197 if (isSymbolLinkerVisible(SD->getSymbol()))
200 // Absolute and undefined symbols have no defining atom.
201 if (!SD->getFragment())
204 // Non-linker visible symbols in sections which can't be atomized have no
206 if (!getBackend().isSectionAtomizable(
207 SD->getFragment()->getParent()->getSection()))
210 // Otherwise, return the atom for the containing fragment.
211 return SD->getFragment()->getAtom();
214 bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
215 const MCFixup &Fixup, const MCFragment *DF,
216 MCValue &Target, uint64_t &Value) const {
217 ++stats::EvaluateFixup;
219 if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
220 report_fatal_error("expected relocatable expression");
222 bool IsPCRel = Backend.getFixupKindInfo(
223 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
227 if (Target.getSymB()) {
229 } else if (!Target.getSymA()) {
232 const MCSymbol &SA = Target.getSymA()->getSymbol();
233 if (SA.AliasedSymbol().isUndefined()) {
236 const MCSymbolData &DataA = getSymbolData(SA);
238 getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
243 IsResolved = Target.isAbsolute();
246 Value = Target.getConstant();
248 bool IsThumb = false;
249 if (const MCSymbolRefExpr *A = Target.getSymA()) {
250 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
252 Value += Layout.getSymbolOffset(&getSymbolData(Sym));
253 if (isThumbFunc(&Sym))
256 if (const MCSymbolRefExpr *B = Target.getSymB()) {
257 const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
259 Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
263 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
264 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
265 assert((ShouldAlignPC ? IsPCRel : true) &&
266 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
269 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
271 // A number of ARM fixups in Thumb mode require that the effective PC
272 // address be determined as the 32-bit aligned version of the actual offset.
273 if (ShouldAlignPC) Offset &= ~0x3;
277 // ARM fixups based from a thumb function address need to have the low
278 // bit set. The actual value is always at least 16-bit aligned, so the
279 // low bit is normally clear and available for use as an ISA flag for
287 uint64_t MCAssembler::ComputeFragmentSize(const MCAsmLayout &Layout,
288 const MCFragment &F) const {
289 switch (F.getKind()) {
290 case MCFragment::FT_Data:
291 return cast<MCDataFragment>(F).getContents().size();
292 case MCFragment::FT_Fill:
293 return cast<MCFillFragment>(F).getSize();
294 case MCFragment::FT_Inst:
295 return cast<MCInstFragment>(F).getInstSize();
297 case MCFragment::FT_LEB:
298 return cast<MCLEBFragment>(F).getContents().size();
300 case MCFragment::FT_Align: {
301 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
302 unsigned Offset = Layout.getFragmentOffset(&AF);
303 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
304 if (Size > AF.getMaxBytesToEmit())
309 case MCFragment::FT_Org: {
310 MCOrgFragment &OF = cast<MCOrgFragment>(F);
311 int64_t TargetLocation;
312 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
313 report_fatal_error("expected assembly-time absolute expression");
315 // FIXME: We need a way to communicate this error.
316 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
317 int64_t Size = TargetLocation - FragmentOffset;
318 if (Size < 0 || Size >= 0x40000000)
319 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
320 "' (at offset '" + Twine(FragmentOffset) + "')");
324 case MCFragment::FT_Dwarf:
325 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
328 assert(0 && "invalid fragment kind");
332 void MCAsmLayout::LayoutFragment(MCFragment *F) {
333 MCFragment *Prev = F->getPrevNode();
335 // We should never try to recompute something which is up-to-date.
336 assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!");
337 // We should never try to compute the fragment layout if it's predecessor
339 assert((!Prev || isFragmentUpToDate(Prev)) &&
340 "Attempt to compute fragment before it's predecessor!");
342 ++stats::FragmentLayouts;
344 // Compute fragment offset and size.
347 Offset += Prev->Offset + getAssembler().ComputeFragmentSize(*this, *Prev);
350 LastValidFragment[F->getParent()] = F;
353 /// WriteFragmentData - Write the \arg F data to the output file.
354 static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
355 const MCFragment &F) {
356 MCObjectWriter *OW = &Asm.getWriter();
357 uint64_t Start = OW->getStream().tell();
360 ++stats::EmittedFragments;
362 // FIXME: Embed in fragments instead?
363 uint64_t FragmentSize = Asm.ComputeFragmentSize(Layout, F);
364 switch (F.getKind()) {
365 case MCFragment::FT_Align: {
366 MCAlignFragment &AF = cast<MCAlignFragment>(F);
367 uint64_t Count = FragmentSize / AF.getValueSize();
369 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
371 // FIXME: This error shouldn't actually occur (the front end should emit
372 // multiple .align directives to enforce the semantics it wants), but is
373 // severe enough that we want to report it. How to handle this?
374 if (Count * AF.getValueSize() != FragmentSize)
375 report_fatal_error("undefined .align directive, value size '" +
376 Twine(AF.getValueSize()) +
377 "' is not a divisor of padding size '" +
378 Twine(FragmentSize) + "'");
380 // See if we are aligning with nops, and if so do that first to try to fill
381 // the Count bytes. Then if that did not fill any bytes or there are any
382 // bytes left to fill use the the Value and ValueSize to fill the rest.
383 // If we are aligning with nops, ask that target to emit the right data.
384 if (AF.hasEmitNops()) {
385 if (!Asm.getBackend().WriteNopData(Count, OW))
386 report_fatal_error("unable to write nop sequence of " +
387 Twine(Count) + " bytes");
391 // Otherwise, write out in multiples of the value size.
392 for (uint64_t i = 0; i != Count; ++i) {
393 switch (AF.getValueSize()) {
395 assert(0 && "Invalid size!");
396 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
397 case 2: OW->Write16(uint16_t(AF.getValue())); break;
398 case 4: OW->Write32(uint32_t(AF.getValue())); break;
399 case 8: OW->Write64(uint64_t(AF.getValue())); break;
405 case MCFragment::FT_Data: {
406 MCDataFragment &DF = cast<MCDataFragment>(F);
407 assert(FragmentSize == DF.getContents().size() && "Invalid size!");
408 OW->WriteBytes(DF.getContents().str());
412 case MCFragment::FT_Fill: {
413 MCFillFragment &FF = cast<MCFillFragment>(F);
415 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
417 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
418 switch (FF.getValueSize()) {
420 assert(0 && "Invalid size!");
421 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
422 case 2: OW->Write16(uint16_t(FF.getValue())); break;
423 case 4: OW->Write32(uint32_t(FF.getValue())); break;
424 case 8: OW->Write64(uint64_t(FF.getValue())); break;
430 case MCFragment::FT_Inst: {
431 MCInstFragment &IF = cast<MCInstFragment>(F);
432 OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size()));
436 case MCFragment::FT_LEB: {
437 MCLEBFragment &LF = cast<MCLEBFragment>(F);
438 OW->WriteBytes(LF.getContents().str());
442 case MCFragment::FT_Org: {
443 MCOrgFragment &OF = cast<MCOrgFragment>(F);
445 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
446 OW->Write8(uint8_t(OF.getValue()));
451 case MCFragment::FT_Dwarf: {
452 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
453 OW->WriteBytes(OF.getContents().str());
458 assert(OW->getStream().tell() - Start == FragmentSize);
461 void MCAssembler::WriteSectionData(const MCSectionData *SD,
462 const MCAsmLayout &Layout) const {
463 // Ignore virtual sections.
464 if (SD->getSection().isVirtualSection()) {
465 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
467 // Check that contents are only things legal inside a virtual section.
468 for (MCSectionData::const_iterator it = SD->begin(),
469 ie = SD->end(); it != ie; ++it) {
470 switch (it->getKind()) {
472 assert(0 && "Invalid fragment in virtual section!");
473 case MCFragment::FT_Data: {
474 // Check that we aren't trying to write a non-zero contents (or fixups)
475 // into a virtual section. This is to support clients which use standard
476 // directives to fill the contents of virtual sections.
477 MCDataFragment &DF = cast<MCDataFragment>(*it);
478 assert(DF.fixup_begin() == DF.fixup_end() &&
479 "Cannot have fixups in virtual section!");
480 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
481 assert(DF.getContents()[i] == 0 &&
482 "Invalid data value for virtual section!");
485 case MCFragment::FT_Align:
486 // Check that we aren't trying to write a non-zero value into a virtual
488 assert((!cast<MCAlignFragment>(it)->getValueSize() ||
489 !cast<MCAlignFragment>(it)->getValue()) &&
490 "Invalid align in virtual section!");
492 case MCFragment::FT_Fill:
493 assert(!cast<MCFillFragment>(it)->getValueSize() &&
494 "Invalid fill in virtual section!");
502 uint64_t Start = getWriter().getStream().tell();
505 for (MCSectionData::const_iterator it = SD->begin(),
506 ie = SD->end(); it != ie; ++it)
507 WriteFragmentData(*this, Layout, *it);
509 assert(getWriter().getStream().tell() - Start ==
510 Layout.getSectionAddressSize(SD));
514 uint64_t MCAssembler::HandleFixup(const MCAsmLayout &Layout,
516 const MCFixup &Fixup) {
517 // Evaluate the fixup.
520 if (!EvaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
521 // The fixup was unresolved, we need a relocation. Inform the object
522 // writer of the relocation, and give it an opportunity to adjust the
523 // fixup value if need be.
524 getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
529 void MCAssembler::Finish() {
530 DEBUG_WITH_TYPE("mc-dump", {
531 llvm::errs() << "assembler backend - pre-layout\n--\n";
534 // Create the layout object.
535 MCAsmLayout Layout(*this);
537 // Create dummy fragments and assign section ordinals.
538 unsigned SectionIndex = 0;
539 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
540 // Create dummy fragments to eliminate any empty sections, this simplifies
542 if (it->getFragmentList().empty())
543 new MCDataFragment(it);
545 it->setOrdinal(SectionIndex++);
548 // Assign layout order indices to sections and fragments.
549 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
550 MCSectionData *SD = Layout.getSectionOrder()[i];
551 SD->setLayoutOrder(i);
553 unsigned FragmentIndex = 0;
554 for (MCSectionData::iterator it2 = SD->begin(),
555 ie2 = SD->end(); it2 != ie2; ++it2)
556 it2->setLayoutOrder(FragmentIndex++);
559 // Layout until everything fits.
560 while (LayoutOnce(Layout))
563 DEBUG_WITH_TYPE("mc-dump", {
564 llvm::errs() << "assembler backend - post-relaxation\n--\n";
567 // Finalize the layout, including fragment lowering.
568 FinishLayout(Layout);
570 DEBUG_WITH_TYPE("mc-dump", {
571 llvm::errs() << "assembler backend - final-layout\n--\n";
574 uint64_t StartOffset = OS.tell();
576 // Allow the object writer a chance to perform post-layout binding (for
577 // example, to set the index fields in the symbol data).
578 getWriter().ExecutePostLayoutBinding(*this, Layout);
580 // Evaluate and apply the fixups, generating relocation entries as necessary.
581 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
582 for (MCSectionData::iterator it2 = it->begin(),
583 ie2 = it->end(); it2 != ie2; ++it2) {
584 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
586 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
587 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
588 MCFixup &Fixup = *it3;
589 uint64_t FixedValue = HandleFixup(Layout, *DF, Fixup);
590 getBackend().ApplyFixup(Fixup, DF->getContents().data(),
591 DF->getContents().size(), FixedValue);
594 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
596 for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(),
597 ie3 = IF->fixup_end(); it3 != ie3; ++it3) {
598 MCFixup &Fixup = *it3;
599 uint64_t FixedValue = HandleFixup(Layout, *IF, Fixup);
600 getBackend().ApplyFixup(Fixup, IF->getCode().data(),
601 IF->getCode().size(), FixedValue);
607 // Write the object file.
608 getWriter().WriteObject(*this, Layout);
610 stats::ObjectBytes += OS.tell() - StartOffset;
613 bool MCAssembler::FixupNeedsRelaxation(const MCFixup &Fixup,
614 const MCFragment *DF,
615 const MCAsmLayout &Layout) const {
619 // If we cannot resolve the fixup value, it requires relaxation.
622 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
625 // Otherwise, relax if the value is too big for a (signed) i8.
627 // FIXME: This is target dependent!
628 return int64_t(Value) != int64_t(int8_t(Value));
631 bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
632 const MCAsmLayout &Layout) const {
633 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
634 // are intentionally pushing out inst fragments, or because we relaxed a
635 // previous instruction to one that doesn't need relaxation.
636 if (!getBackend().MayNeedRelaxation(IF->getInst()))
639 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
640 ie = IF->fixup_end(); it != ie; ++it)
641 if (FixupNeedsRelaxation(*it, IF, Layout))
647 bool MCAssembler::RelaxInstruction(MCAsmLayout &Layout,
648 MCInstFragment &IF) {
649 if (!FragmentNeedsRelaxation(&IF, Layout))
652 ++stats::RelaxedInstructions;
654 // FIXME-PERF: We could immediately lower out instructions if we can tell
655 // they are fully resolved, to avoid retesting on later passes.
657 // Relax the fragment.
660 getBackend().RelaxInstruction(IF.getInst(), Relaxed);
662 // Encode the new instruction.
664 // FIXME-PERF: If it matters, we could let the target do this. It can
665 // probably do so more efficiently in many cases.
666 SmallVector<MCFixup, 4> Fixups;
667 SmallString<256> Code;
668 raw_svector_ostream VecOS(Code);
669 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
672 // Update the instruction fragment.
675 IF.getFixups().clear();
676 // FIXME: Eliminate copy.
677 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
678 IF.getFixups().push_back(Fixups[i]);
683 bool MCAssembler::RelaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
685 uint64_t OldSize = LF.getContents().size();
686 LF.getValue().EvaluateAsAbsolute(Value, Layout);
687 SmallString<8> &Data = LF.getContents();
689 raw_svector_ostream OSE(Data);
691 MCObjectWriter::EncodeSLEB128(Value, OSE);
693 MCObjectWriter::EncodeULEB128(Value, OSE);
695 return OldSize != LF.getContents().size();
698 bool MCAssembler::RelaxDwarfLineAddr(MCAsmLayout &Layout,
699 MCDwarfLineAddrFragment &DF) {
700 int64_t AddrDelta = 0;
701 uint64_t OldSize = DF.getContents().size();
702 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
706 LineDelta = DF.getLineDelta();
707 SmallString<8> &Data = DF.getContents();
709 raw_svector_ostream OSE(Data);
710 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
712 return OldSize != Data.size();
715 bool MCAssembler::LayoutSectionOnce(MCAsmLayout &Layout,
717 MCFragment *FirstInvalidFragment = NULL;
718 // Scan for fragments that need relaxation.
719 for (MCSectionData::iterator it2 = SD.begin(),
720 ie2 = SD.end(); it2 != ie2; ++it2) {
721 // Check if this is an fragment that needs relaxation.
722 bool relaxedFrag = false;
723 switch(it2->getKind()) {
726 case MCFragment::FT_Inst:
727 relaxedFrag = RelaxInstruction(Layout, *cast<MCInstFragment>(it2));
729 case MCFragment::FT_Dwarf:
730 relaxedFrag = RelaxDwarfLineAddr(Layout,
731 *cast<MCDwarfLineAddrFragment>(it2));
733 case MCFragment::FT_LEB:
734 relaxedFrag = RelaxLEB(Layout, *cast<MCLEBFragment>(it2));
737 // Update the layout, and remember that we relaxed.
738 if (relaxedFrag && !FirstInvalidFragment)
739 FirstInvalidFragment = it2;
741 if (FirstInvalidFragment) {
742 Layout.Invalidate(FirstInvalidFragment);
748 bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
749 ++stats::RelaxationSteps;
751 bool WasRelaxed = false;
752 for (iterator it = begin(), ie = end(); it != ie; ++it) {
753 MCSectionData &SD = *it;
754 while(LayoutSectionOnce(Layout, SD))
761 void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
762 // The layout is done. Mark every fragment as valid.
763 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
764 Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
772 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
773 OS << "<MCFixup" << " Offset:" << AF.getOffset()
774 << " Value:" << *AF.getValue()
775 << " Kind:" << AF.getKind() << ">";
781 void MCFragment::dump() {
782 raw_ostream &OS = llvm::errs();
786 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
787 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
788 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
789 case MCFragment::FT_Inst: OS << "MCInstFragment"; break;
790 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
791 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
792 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
795 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
796 << " Offset:" << Offset << ">";
799 case MCFragment::FT_Align: {
800 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
801 if (AF->hasEmitNops())
802 OS << " (emit nops)";
804 OS << " Alignment:" << AF->getAlignment()
805 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
806 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
809 case MCFragment::FT_Data: {
810 const MCDataFragment *DF = cast<MCDataFragment>(this);
813 const SmallVectorImpl<char> &Contents = DF->getContents();
814 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
816 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
818 OS << "] (" << Contents.size() << " bytes)";
820 if (!DF->getFixups().empty()) {
823 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
824 ie = DF->fixup_end(); it != ie; ++it) {
825 if (it != DF->fixup_begin()) OS << ",\n ";
832 case MCFragment::FT_Fill: {
833 const MCFillFragment *FF = cast<MCFillFragment>(this);
834 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
835 << " Size:" << FF->getSize();
838 case MCFragment::FT_Inst: {
839 const MCInstFragment *IF = cast<MCInstFragment>(this);
842 IF->getInst().dump_pretty(OS);
845 case MCFragment::FT_Org: {
846 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
848 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
851 case MCFragment::FT_Dwarf: {
852 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
854 OS << " AddrDelta:" << OF->getAddrDelta()
855 << " LineDelta:" << OF->getLineDelta();
858 case MCFragment::FT_LEB: {
859 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
861 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
868 void MCSectionData::dump() {
869 raw_ostream &OS = llvm::errs();
871 OS << "<MCSectionData";
872 OS << " Alignment:" << getAlignment() << " Fragments:[\n ";
873 for (iterator it = begin(), ie = end(); it != ie; ++it) {
874 if (it != begin()) OS << ",\n ";
880 void MCSymbolData::dump() {
881 raw_ostream &OS = llvm::errs();
883 OS << "<MCSymbolData Symbol:" << getSymbol()
884 << " Fragment:" << getFragment() << " Offset:" << getOffset()
885 << " Flags:" << getFlags() << " Index:" << getIndex();
887 OS << " (common, size:" << getCommonSize()
888 << " align: " << getCommonAlignment() << ")";
891 if (isPrivateExtern())
892 OS << " (private extern)";
896 void MCAssembler::dump() {
897 raw_ostream &OS = llvm::errs();
899 OS << "<MCAssembler\n";
900 OS << " Sections:[\n ";
901 for (iterator it = begin(), ie = end(); it != ie; ++it) {
902 if (it != begin()) OS << ",\n ";
908 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
909 if (it != symbol_begin()) OS << ",\n ";