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"
33 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
34 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
35 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
36 STATISTIC(EvaluateFixup, "Number of evaluated fixups");
37 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
41 // FIXME FIXME FIXME: There are number of places in this file where we convert
42 // what is a 64-bit assembler value used for computation into a value in the
43 // object file, which may truncate it. We should detect that truncation where
44 // invalid and report errors back.
48 uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const {
49 assert(F->getParent() && "Missing section()!");
50 return getSectionAddress(F->getParent()) + getFragmentOffset(F);
53 uint64_t MCAsmLayout::getFragmentEffectiveSize(const MCFragment *F) const {
54 assert(F->EffectiveSize != ~UINT64_C(0) && "Address not set!");
55 return F->EffectiveSize;
58 void MCAsmLayout::setFragmentEffectiveSize(MCFragment *F, uint64_t Value) {
59 F->EffectiveSize = Value;
62 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
63 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
67 void MCAsmLayout::setFragmentOffset(MCFragment *F, uint64_t Value) {
71 uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
72 assert(SD->getFragment() && "Invalid getAddress() on undefined symbol!");
73 return getFragmentAddress(SD->getFragment()) + SD->getOffset();
76 uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
77 assert(SD->Address != ~UINT64_C(0) && "Address not set!");
81 void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) {
85 uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
86 assert(SD->Size != ~UINT64_C(0) && "File size not set!");
89 void MCAsmLayout::setSectionSize(MCSectionData *SD, uint64_t Value) {
93 uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
94 assert(SD->FileSize != ~UINT64_C(0) && "File size not set!");
97 void MCAsmLayout::setSectionFileSize(MCSectionData *SD, uint64_t Value) {
105 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
108 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
111 EffectiveSize(~UINT64_C(0))
114 Parent->getFragmentList().push_back(this);
117 MCFragment::~MCFragment() {
122 MCSectionData::MCSectionData() : Section(0) {}
124 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
125 : Section(&_Section),
127 Address(~UINT64_C(0)),
129 FileSize(~UINT64_C(0)),
130 HasInstructions(false)
133 A->getSectionList().push_back(this);
138 MCSymbolData::MCSymbolData() : Symbol(0) {}
140 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
141 uint64_t _Offset, MCAssembler *A)
142 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
143 IsExternal(false), IsPrivateExtern(false),
144 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
147 A->getSymbolList().push_back(this);
152 MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
153 MCCodeEmitter &_Emitter, raw_ostream &_OS)
154 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
155 OS(_OS), SubsectionsViaSymbols(false)
159 MCAssembler::~MCAssembler() {
162 static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
163 const MCAsmFixup &Fixup,
164 const MCValue Target,
165 const MCSection *BaseSection) {
166 // The effective fixup address is
167 // addr(atom(A)) + offset(A)
168 // - addr(atom(B)) - offset(B)
169 // - addr(<base symbol>) + <fixup offset from base symbol>
170 // and the offsets are not relocatable, so the fixup is fully resolved when
171 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
173 // The simple (Darwin, except on x86_64) way of dealing with this was to
174 // assume that any reference to a temporary symbol *must* be a temporary
175 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
176 // relocation to a temporary symbol (in the same section) is fully
177 // resolved. This also works in conjunction with absolutized .set, which
178 // requires the compiler to use .set to absolutize the differences between
179 // symbols which the compiler knows to be assembly time constants, so we don't
180 // need to worry about consider symbol differences fully resolved.
182 // Non-relative fixups are only resolved if constant.
184 return Target.isAbsolute();
186 // Otherwise, relative fixups are only resolved if not a difference and the
187 // target is a temporary in the same section.
188 if (Target.isAbsolute() || Target.getSymB())
191 const MCSymbol *A = &Target.getSymA()->getSymbol();
192 if (!A->isTemporary() || !A->isInSection() ||
193 &A->getSection() != BaseSection)
199 static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
200 const MCAsmLayout &Layout,
201 const MCAsmFixup &Fixup,
202 const MCValue Target,
203 const MCSymbolData *BaseSymbol) {
204 // The effective fixup address is
205 // addr(atom(A)) + offset(A)
206 // - addr(atom(B)) - offset(B)
207 // - addr(BaseSymbol) + <fixup offset from base symbol>
208 // and the offsets are not relocatable, so the fixup is fully resolved when
209 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
211 // Note that "false" is almost always conservatively correct (it means we emit
212 // a relocation which is unnecessary), except when it would force us to emit a
213 // relocation which the target cannot encode.
215 const MCSymbolData *A_Base = 0, *B_Base = 0;
216 if (const MCSymbolRefExpr *A = Target.getSymA()) {
217 // Modified symbol references cannot be resolved.
218 if (A->getKind() != MCSymbolRefExpr::VK_None)
221 A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol()));
226 if (const MCSymbolRefExpr *B = Target.getSymB()) {
227 // Modified symbol references cannot be resolved.
228 if (B->getKind() != MCSymbolRefExpr::VK_None)
231 B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol()));
236 // If there is no base, A and B have to be the same atom for this fixup to be
239 return A_Base == B_Base;
241 // Otherwise, B must be missing and A must be the base.
242 return !B_Base && BaseSymbol == A_Base;
245 bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
246 // Non-temporary labels should always be visible to the linker.
247 if (!SD->getSymbol().isTemporary())
250 // Absolute temporary labels are never visible.
251 if (!SD->getFragment())
254 // Otherwise, check if the section requires symbols even for temporary labels.
255 return getBackend().doesSectionRequireSymbols(
256 SD->getFragment()->getParent()->getSection());
259 // FIXME-PERF: This routine is really slow.
260 const MCSymbolData *MCAssembler::getAtomForAddress(const MCAsmLayout &Layout,
261 const MCSectionData *Section,
262 uint64_t Address) const {
263 const MCSymbolData *Best = 0;
264 uint64_t BestAddress = 0;
266 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
267 ie = symbol_end(); it != ie; ++it) {
268 // Ignore non-linker visible symbols.
269 if (!isSymbolLinkerVisible(it))
272 // Ignore symbols not in the same section.
273 if (!it->getFragment() || it->getFragment()->getParent() != Section)
276 // Otherwise, find the closest symbol preceding this address (ties are
277 // resolved in favor of the last defined symbol).
278 uint64_t SymbolAddress = Layout.getSymbolAddress(it);
279 if (SymbolAddress <= Address && (!Best || SymbolAddress >= BestAddress)) {
281 BestAddress = SymbolAddress;
288 // FIXME-PERF: This routine is really slow.
289 const MCSymbolData *MCAssembler::getAtom(const MCAsmLayout &Layout,
290 const MCSymbolData *SD) const {
291 // Linker visible symbols define atoms.
292 if (isSymbolLinkerVisible(SD))
295 // Absolute and undefined symbols have no defining atom.
296 if (!SD->getFragment())
299 // Otherwise, search by address.
300 return getAtomForAddress(Layout, SD->getFragment()->getParent(),
301 Layout.getSymbolAddress(SD));
304 bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
305 const MCAsmFixup &Fixup, const MCFragment *DF,
306 MCValue &Target, uint64_t &Value) const {
307 ++stats::EvaluateFixup;
309 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
310 llvm_report_error("expected relocatable expression");
312 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
313 // doesn't support small relocations, but then under what criteria does the
314 // assembler allow symbol differences?
316 Value = Target.getConstant();
319 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
320 bool IsResolved = true;
321 if (const MCSymbolRefExpr *A = Target.getSymA()) {
322 if (A->getSymbol().isDefined())
323 Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
327 if (const MCSymbolRefExpr *B = Target.getSymB()) {
328 if (B->getSymbol().isDefined())
329 Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
334 // If we are using scattered symbols, determine whether this value is actually
335 // resolved; scattering may cause atoms to move.
336 if (IsResolved && getBackend().hasScatteredSymbols()) {
337 if (getBackend().hasReliableSymbolDifference()) {
338 // If this is a PCrel relocation, find the base atom (identified by its
339 // symbol) that the fixup value is relative to.
340 const MCSymbolData *BaseSymbol = 0;
342 BaseSymbol = getAtomForAddress(
343 Layout, DF->getParent(), Layout.getFragmentAddress(DF)+Fixup.Offset);
349 IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
352 const MCSection *BaseSection = 0;
354 BaseSection = &DF->getParent()->getSection();
356 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
362 Value -= Layout.getFragmentAddress(DF) + Fixup.Offset;
367 void MCAssembler::LayoutSection(MCSectionData &SD,
369 uint64_t StartAddress) {
370 Layout.setSectionAddress(&SD, StartAddress);
372 uint64_t Address = StartAddress;
373 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
376 uint64_t FragmentOffset = Address - StartAddress;
377 Layout.setFragmentOffset(&F, FragmentOffset);
379 // Evaluate fragment size.
380 uint64_t EffectiveSize = 0;
381 switch (F.getKind()) {
382 case MCFragment::FT_Align: {
383 MCAlignFragment &AF = cast<MCAlignFragment>(F);
385 EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
386 if (EffectiveSize > AF.getMaxBytesToEmit())
391 case MCFragment::FT_Data:
392 EffectiveSize = cast<MCDataFragment>(F).getContents().size();
395 case MCFragment::FT_Fill: {
396 MCFillFragment &FF = cast<MCFillFragment>(F);
397 EffectiveSize = FF.getValueSize() * FF.getCount();
401 case MCFragment::FT_Inst:
402 EffectiveSize = cast<MCInstFragment>(F).getInstSize();
405 case MCFragment::FT_Org: {
406 MCOrgFragment &OF = cast<MCOrgFragment>(F);
408 int64_t TargetLocation;
409 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
410 llvm_report_error("expected assembly-time absolute expression");
412 // FIXME: We need a way to communicate this error.
413 int64_t Offset = TargetLocation - FragmentOffset;
415 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
416 "' (at offset '" + Twine(FragmentOffset) + "'");
418 EffectiveSize = Offset;
422 case MCFragment::FT_ZeroFill: {
423 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
425 // Align the fragment offset; it is safe to adjust the offset freely since
426 // this is only in virtual sections.
428 // FIXME: We shouldn't be doing this here.
429 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
430 Layout.setFragmentOffset(&F, Address - StartAddress);
432 EffectiveSize = ZFF.getSize();
437 Layout.setFragmentEffectiveSize(&F, EffectiveSize);
438 Address += EffectiveSize;
441 // Set the section sizes.
442 Layout.setSectionSize(&SD, Address - StartAddress);
443 if (getBackend().isVirtualSection(SD.getSection()))
444 Layout.setSectionFileSize(&SD, 0);
446 Layout.setSectionFileSize(&SD, Address - StartAddress);
449 /// WriteFragmentData - Write the \arg F data to the output file.
450 static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
451 const MCFragment &F, MCObjectWriter *OW) {
452 uint64_t Start = OW->getStream().tell();
455 ++stats::EmittedFragments;
457 // FIXME: Embed in fragments instead?
458 uint64_t FragmentSize = Layout.getFragmentEffectiveSize(&F);
459 switch (F.getKind()) {
460 case MCFragment::FT_Align: {
461 MCAlignFragment &AF = cast<MCAlignFragment>(F);
462 uint64_t Count = FragmentSize / AF.getValueSize();
464 // FIXME: This error shouldn't actually occur (the front end should emit
465 // multiple .align directives to enforce the semantics it wants), but is
466 // severe enough that we want to report it. How to handle this?
467 if (Count * AF.getValueSize() != FragmentSize)
468 llvm_report_error("undefined .align directive, value size '" +
469 Twine(AF.getValueSize()) +
470 "' is not a divisor of padding size '" +
471 Twine(FragmentSize) + "'");
473 // See if we are aligning with nops, and if so do that first to try to fill
474 // the Count bytes. Then if that did not fill any bytes or there are any
475 // bytes left to fill use the the Value and ValueSize to fill the rest.
476 // If we are aligning with nops, ask that target to emit the right data.
477 if (AF.getEmitNops()) {
478 if (!Asm.getBackend().WriteNopData(Count, OW))
479 llvm_report_error("unable to write nop sequence of " +
480 Twine(Count) + " bytes");
484 // Otherwise, write out in multiples of the value size.
485 for (uint64_t i = 0; i != Count; ++i) {
486 switch (AF.getValueSize()) {
488 assert(0 && "Invalid size!");
489 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
490 case 2: OW->Write16(uint16_t(AF.getValue())); break;
491 case 4: OW->Write32(uint32_t(AF.getValue())); break;
492 case 8: OW->Write64(uint64_t(AF.getValue())); break;
498 case MCFragment::FT_Data: {
499 MCDataFragment &DF = cast<MCDataFragment>(F);
500 assert(FragmentSize == DF.getContents().size() && "Invalid size!");
501 OW->WriteBytes(DF.getContents().str());
505 case MCFragment::FT_Fill: {
506 MCFillFragment &FF = cast<MCFillFragment>(F);
507 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
508 switch (FF.getValueSize()) {
510 assert(0 && "Invalid size!");
511 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
512 case 2: OW->Write16(uint16_t(FF.getValue())); break;
513 case 4: OW->Write32(uint32_t(FF.getValue())); break;
514 case 8: OW->Write64(uint64_t(FF.getValue())); break;
520 case MCFragment::FT_Inst:
521 llvm_unreachable("unexpected inst fragment after lowering");
524 case MCFragment::FT_Org: {
525 MCOrgFragment &OF = cast<MCOrgFragment>(F);
527 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
528 OW->Write8(uint8_t(OF.getValue()));
533 case MCFragment::FT_ZeroFill: {
534 assert(0 && "Invalid zero fill fragment in concrete section!");
539 assert(OW->getStream().tell() - Start == FragmentSize);
542 void MCAssembler::WriteSectionData(const MCSectionData *SD,
543 const MCAsmLayout &Layout,
544 MCObjectWriter *OW) const {
545 uint64_t SectionSize = Layout.getSectionSize(SD);
546 uint64_t SectionFileSize = Layout.getSectionFileSize(SD);
548 // Ignore virtual sections.
549 if (getBackend().isVirtualSection(SD->getSection())) {
550 assert(SectionFileSize == 0 && "Invalid size for section!");
554 uint64_t Start = OW->getStream().tell();
557 for (MCSectionData::const_iterator it = SD->begin(),
558 ie = SD->end(); it != ie; ++it)
559 WriteFragmentData(*this, Layout, *it, OW);
561 // Add section padding.
562 assert(SectionFileSize >= SectionSize && "Invalid section sizes!");
563 OW->WriteZeros(SectionFileSize - SectionSize);
565 assert(OW->getStream().tell() - Start == SectionFileSize);
568 void MCAssembler::Finish() {
569 DEBUG_WITH_TYPE("mc-dump", {
570 llvm::errs() << "assembler backend - pre-layout\n--\n";
573 // Assign section and fragment ordinals, all subsequent backend code is
574 // responsible for updating these in place.
575 unsigned SectionIndex = 0;
576 unsigned FragmentIndex = 0;
577 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
578 it->setOrdinal(SectionIndex++);
580 for (MCSectionData::iterator it2 = it->begin(),
581 ie2 = it->end(); it2 != ie2; ++it2)
582 it2->setOrdinal(FragmentIndex++);
585 // Layout until everything fits.
586 MCAsmLayout Layout(*this);
587 while (LayoutOnce(Layout))
590 DEBUG_WITH_TYPE("mc-dump", {
591 llvm::errs() << "assembler backend - post-relaxation\n--\n";
594 // Finalize the layout, including fragment lowering.
595 FinishLayout(Layout);
597 DEBUG_WITH_TYPE("mc-dump", {
598 llvm::errs() << "assembler backend - final-layout\n--\n";
601 uint64_t StartOffset = OS.tell();
602 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
604 llvm_report_error("unable to create object writer!");
606 // Allow the object writer a chance to perform post-layout binding (for
607 // example, to set the index fields in the symbol data).
608 Writer->ExecutePostLayoutBinding(*this);
610 // Evaluate and apply the fixups, generating relocation entries as necessary.
611 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
612 for (MCSectionData::iterator it2 = it->begin(),
613 ie2 = it->end(); it2 != ie2; ++it2) {
614 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
618 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
619 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
620 MCAsmFixup &Fixup = *it3;
622 // Evaluate the fixup.
625 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
626 // The fixup was unresolved, we need a relocation. Inform the object
627 // writer of the relocation, and give it an opportunity to adjust the
628 // fixup value if need be.
629 Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
632 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
637 // Write the object file.
638 Writer->WriteObject(*this, Layout);
641 stats::ObjectBytes += OS.tell() - StartOffset;
644 bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
645 const MCFragment *DF,
646 const MCAsmLayout &Layout) const {
647 // If we cannot resolve the fixup value, it requires relaxation.
650 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
653 // Otherwise, relax if the value is too big for a (signed) i8.
654 return int64_t(Value) != int64_t(int8_t(Value));
657 bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
658 const MCAsmLayout &Layout) const {
659 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
660 // are intentionally pushing out inst fragments, or because we relaxed a
661 // previous instruction to one that doesn't need relaxation.
662 if (!getBackend().MayNeedRelaxation(IF->getInst(), IF->getFixups()))
665 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
666 ie = IF->fixup_end(); it != ie; ++it)
667 if (FixupNeedsRelaxation(*it, IF, Layout))
673 bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
674 ++stats::RelaxationSteps;
676 // Layout the concrete sections and fragments.
677 uint64_t Address = 0;
678 MCSectionData *Prev = 0;
679 for (iterator it = begin(), ie = end(); it != ie; ++it) {
680 MCSectionData &SD = *it;
682 // Skip virtual sections.
683 if (getBackend().isVirtualSection(SD.getSection()))
686 // Align this section if necessary by adding padding bytes to the previous
688 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
689 assert(Prev && "Missing prev section!");
690 Layout.setSectionFileSize(Prev, Layout.getSectionFileSize(Prev) + Pad);
694 // Layout the section fragments and its size.
695 LayoutSection(SD, Layout, Address);
696 Address += Layout.getSectionFileSize(&SD);
701 // Layout the virtual sections.
702 for (iterator it = begin(), ie = end(); it != ie; ++it) {
703 MCSectionData &SD = *it;
705 if (!getBackend().isVirtualSection(SD.getSection()))
708 // Align this section if necessary by adding padding bytes to the previous
710 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
713 LayoutSection(SD, Layout, Address);
714 Address += Layout.getSectionSize(&SD);
717 // Scan for fragments that need relaxation.
718 for (iterator it = begin(), ie = end(); it != ie; ++it) {
719 MCSectionData &SD = *it;
721 for (MCSectionData::iterator it2 = SD.begin(),
722 ie2 = SD.end(); it2 != ie2; ++it2) {
723 // Check if this is an instruction fragment that needs relaxation.
724 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
725 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
728 ++stats::RelaxedInstructions;
730 // FIXME-PERF: We could immediately lower out instructions if we can tell
731 // they are fully resolved, to avoid retesting on later passes.
733 // Relax the fragment.
736 getBackend().RelaxInstruction(IF, Relaxed);
738 // Encode the new instruction.
740 // FIXME-PERF: If it matters, we could let the target do this. It can
741 // probably do so more efficiently in many cases.
742 SmallVector<MCFixup, 4> Fixups;
743 SmallString<256> Code;
744 raw_svector_ostream VecOS(Code);
745 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
748 // Update the instruction fragment.
749 IF->setInst(Relaxed);
750 IF->getCode() = Code;
751 IF->getFixups().clear();
752 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
753 MCFixup &F = Fixups[i];
754 IF->getFixups().push_back(MCAsmFixup(F.getOffset(), *F.getValue(),
760 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
761 // smart MCAsmLayout object.
769 void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
770 // Lower out any instruction fragments, to simplify the fixup application and
773 // FIXME-PERF: We don't have to do this, but the assumption is that it is
774 // cheap (we will mostly end up eliminating fragments and appending on to data
775 // fragments), so the extra complexity downstream isn't worth it. Evaluate
777 for (iterator it = begin(), ie = end(); it != ie; ++it) {
778 MCSectionData &SD = *it;
780 for (MCSectionData::iterator it2 = SD.begin(),
781 ie2 = SD.end(); it2 != ie2; ++it2) {
782 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
786 // Create a new data fragment for the instruction.
788 // FIXME-PERF: Reuse previous data fragment if possible.
789 MCDataFragment *DF = new MCDataFragment();
790 SD.getFragmentList().insert(it2, DF);
792 // Update the data fragments layout data.
794 // FIXME: Add MCAsmLayout utility for this.
795 DF->setParent(IF->getParent());
796 DF->setOrdinal(IF->getOrdinal());
797 Layout.setFragmentOffset(DF, Layout.getFragmentOffset(IF));
798 Layout.setFragmentEffectiveSize(DF, Layout.getFragmentEffectiveSize(IF));
800 // Copy in the data and the fixups.
801 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
802 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
803 DF->getFixups().push_back(IF->getFixups()[i]);
805 // Delete the instruction fragment and update the iterator.
806 SD.getFragmentList().erase(IF);
816 raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
817 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
818 << " Kind:" << AF.Kind << ">";
824 void MCFragment::dump() {
825 raw_ostream &OS = llvm::errs();
827 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
828 << " EffectiveSize:" << EffectiveSize;
833 void MCAlignFragment::dump() {
834 raw_ostream &OS = llvm::errs();
836 OS << "<MCAlignFragment ";
837 this->MCFragment::dump();
839 OS << " Alignment:" << getAlignment()
840 << " Value:" << getValue() << " ValueSize:" << getValueSize()
841 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
844 void MCDataFragment::dump() {
845 raw_ostream &OS = llvm::errs();
847 OS << "<MCDataFragment ";
848 this->MCFragment::dump();
851 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
853 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
855 OS << "] (" << getContents().size() << " bytes)";
857 if (!getFixups().empty()) {
860 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
861 if (it != fixup_begin()) OS << ",\n ";
870 void MCFillFragment::dump() {
871 raw_ostream &OS = llvm::errs();
873 OS << "<MCFillFragment ";
874 this->MCFragment::dump();
876 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
877 << " Count:" << getCount() << ">";
880 void MCInstFragment::dump() {
881 raw_ostream &OS = llvm::errs();
883 OS << "<MCInstFragment ";
884 this->MCFragment::dump();
887 getInst().dump_pretty(OS);
891 void MCOrgFragment::dump() {
892 raw_ostream &OS = llvm::errs();
894 OS << "<MCOrgFragment ";
895 this->MCFragment::dump();
897 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
900 void MCZeroFillFragment::dump() {
901 raw_ostream &OS = llvm::errs();
903 OS << "<MCZeroFillFragment ";
904 this->MCFragment::dump();
906 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
909 void MCSectionData::dump() {
910 raw_ostream &OS = llvm::errs();
912 OS << "<MCSectionData";
913 OS << " Alignment:" << getAlignment() << " Address:" << Address
914 << " Size:" << Size << " FileSize:" << FileSize
915 << " Fragments:[\n ";
916 for (iterator it = begin(), ie = end(); it != ie; ++it) {
917 if (it != begin()) OS << ",\n ";
923 void MCSymbolData::dump() {
924 raw_ostream &OS = llvm::errs();
926 OS << "<MCSymbolData Symbol:" << getSymbol()
927 << " Fragment:" << getFragment() << " Offset:" << getOffset()
928 << " Flags:" << getFlags() << " Index:" << getIndex();
930 OS << " (common, size:" << getCommonSize()
931 << " align: " << getCommonAlignment() << ")";
934 if (isPrivateExtern())
935 OS << " (private extern)";
939 void MCAssembler::dump() {
940 raw_ostream &OS = llvm::errs();
942 OS << "<MCAssembler\n";
943 OS << " Sections:[\n ";
944 for (iterator it = begin(), ie = end(); it != ie; ++it) {
945 if (it != begin()) OS << ",\n ";
951 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
952 if (it != symbol_begin()) OS << ",\n ";