506f2a650a1eae0e4d11f99545c4242428505492
[oota-llvm.git] / lib / MC / MCAssembler.cpp
1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetRegistry.h"
26 #include "llvm/Target/TargetAsmBackend.h"
27
28 #include <vector>
29 using namespace llvm;
30
31 namespace {
32 namespace stats {
33 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
34 STATISTIC(EvaluateFixup, "Number of evaluated fixups");
35 STATISTIC(FragmentLayouts, "Number of fragment layouts");
36 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
37 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
38 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
39 STATISTIC(SectionLayouts, "Number of section layouts");
40 }
41 }
42
43 // FIXME FIXME FIXME: There are number of places in this file where we convert
44 // what is a 64-bit assembler value used for computation into a value in the
45 // object file, which may truncate it. We should detect that truncation where
46 // invalid and report errors back.
47
48 /* *** */
49
50 MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
51   : Assembler(Asm), LastValidFragment(0)
52  {
53   // Compute the section layout order. Virtual sections must go last.
54   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
55     if (!Asm.getBackend().isVirtualSection(it->getSection()))
56       SectionOrder.push_back(&*it);
57   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
58     if (Asm.getBackend().isVirtualSection(it->getSection()))
59       SectionOrder.push_back(&*it);
60 }
61
62 bool MCAsmLayout::isSectionUpToDate(const MCSectionData *SD) const {
63   // The first section is always up-to-date.
64   unsigned Index = SD->getLayoutOrder();
65   if (!Index)
66     return true;
67
68   // Otherwise, sections are always implicitly computed when the preceeding
69   // fragment is layed out.
70   const MCSectionData *Prev = getSectionOrder()[Index - 1];
71   return isFragmentUpToDate(&(Prev->getFragmentList().back()));
72 }
73
74 bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const {
75   return (LastValidFragment &&
76           F->getLayoutOrder() <= LastValidFragment->getLayoutOrder());
77 }
78
79 void MCAsmLayout::UpdateForSlide(MCFragment *F, int SlideAmount) {
80   // If this fragment wasn't already up-to-date, we don't need to do anything.
81   if (!isFragmentUpToDate(F))
82     return;
83
84   // Otherwise, reset the last valid fragment to the predecessor of the
85   // invalidated fragment.
86   LastValidFragment = F->getPrevNode();
87   if (!LastValidFragment) {
88     unsigned Index = F->getParent()->getLayoutOrder();
89     if (Index != 0) {
90       MCSectionData *Prev = getSectionOrder()[Index - 1];
91       LastValidFragment = &(Prev->getFragmentList().back());
92     }
93   }
94 }
95
96 void MCAsmLayout::EnsureValid(const MCFragment *F) const {
97   // Advance the layout position until the fragment is up-to-date.
98   while (!isFragmentUpToDate(F)) {
99     // Advance to the next fragment.
100     MCFragment *Cur = LastValidFragment;
101     if (Cur)
102       Cur = Cur->getNextNode();
103     if (!Cur) {
104       unsigned NextIndex = 0;
105       if (LastValidFragment)
106         NextIndex = LastValidFragment->getParent()->getLayoutOrder() + 1;
107       Cur = SectionOrder[NextIndex]->begin();
108     }
109
110     const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur);
111   }
112 }
113
114 void MCAsmLayout::FragmentReplaced(MCFragment *Src, MCFragment *Dst) {
115   if (LastValidFragment == Src)
116     LastValidFragment = Dst;
117
118   Dst->Offset = Src->Offset;
119   Dst->EffectiveSize = Src->EffectiveSize;
120 }
121
122 uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const {
123   assert(F->getParent() && "Missing section()!");
124   return getSectionAddress(F->getParent()) + getFragmentOffset(F);
125 }
126
127 uint64_t MCAsmLayout::getFragmentEffectiveSize(const MCFragment *F) const {
128   EnsureValid(F);
129   assert(F->EffectiveSize != ~UINT64_C(0) && "Address not set!");
130   return F->EffectiveSize;
131 }
132
133 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
134   EnsureValid(F);
135   assert(F->Offset != ~UINT64_C(0) && "Address not set!");
136   return F->Offset;
137 }
138
139 uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
140   assert(SD->getFragment() && "Invalid getAddress() on undefined symbol!");
141   return getFragmentAddress(SD->getFragment()) + SD->getOffset();
142 }
143
144 uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
145   EnsureValid(SD->begin());
146   assert(SD->Address != ~UINT64_C(0) && "Address not set!");
147   return SD->Address;
148 }
149
150 uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
151   // The size is the last fragment's end offset.
152   const MCFragment &F = SD->getFragmentList().back();
153   return getFragmentOffset(&F) + getFragmentEffectiveSize(&F);
154 }
155
156 uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
157   // Virtual sections have no file size.
158   if (getAssembler().getBackend().isVirtualSection(SD->getSection()))
159     return 0;
160
161   // Otherwise, the file size is the same as the address space size.
162   return getSectionAddressSize(SD);
163 }
164
165 uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
166   // The logical size is the address space size minus any tail padding.
167   uint64_t Size = getSectionAddressSize(SD);
168   const MCAlignFragment *AF =
169     dyn_cast<MCAlignFragment>(&(SD->getFragmentList().back()));
170   if (AF && AF->hasOnlyAlignAddress())
171     Size -= getFragmentEffectiveSize(AF);
172
173   return Size;
174 }
175
176 /* *** */
177
178 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
179 }
180
181 MCFragment::~MCFragment() {
182 }
183
184 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
185   : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0)),
186     EffectiveSize(~UINT64_C(0))
187 {
188   if (Parent)
189     Parent->getFragmentList().push_back(this);
190 }
191
192 /* *** */
193
194 MCSectionData::MCSectionData() : Section(0) {}
195
196 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
197   : Section(&_Section),
198     Alignment(1),
199     Address(~UINT64_C(0)),
200     HasInstructions(false)
201 {
202   if (A)
203     A->getSectionList().push_back(this);
204 }
205
206 /* *** */
207
208 MCSymbolData::MCSymbolData() : Symbol(0) {}
209
210 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
211                            uint64_t _Offset, MCAssembler *A)
212   : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
213     IsExternal(false), IsPrivateExtern(false),
214     CommonSize(0), SymbolSize(0), CommonAlign(0),
215     Flags(0), Index(0)
216 {
217   if (A)
218     A->getSymbolList().push_back(this);
219 }
220
221 /* *** */
222
223 MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
224                          MCCodeEmitter &_Emitter, bool _PadSectionToAlignment,
225                          raw_ostream &_OS)
226   : Context(_Context), Backend(_Backend), Emitter(_Emitter),
227     OS(_OS), RelaxAll(false), SubsectionsViaSymbols(false),
228     PadSectionToAlignment(_PadSectionToAlignment)
229 {
230 }
231
232 MCAssembler::~MCAssembler() {
233 }
234
235 static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
236                                                 const MCFixup &Fixup,
237                                                 const MCValue Target,
238                                                 const MCSection *BaseSection) {
239   // The effective fixup address is
240   //     addr(atom(A)) + offset(A)
241   //   - addr(atom(B)) - offset(B)
242   //   - addr(<base symbol>) + <fixup offset from base symbol>
243   // and the offsets are not relocatable, so the fixup is fully resolved when
244   //  addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
245   //
246   // The simple (Darwin, except on x86_64) way of dealing with this was to
247   // assume that any reference to a temporary symbol *must* be a temporary
248   // symbol in the same atom, unless the sections differ. Therefore, any PCrel
249   // relocation to a temporary symbol (in the same section) is fully
250   // resolved. This also works in conjunction with absolutized .set, which
251   // requires the compiler to use .set to absolutize the differences between
252   // symbols which the compiler knows to be assembly time constants, so we don't
253   // need to worry about considering symbol differences fully resolved.
254
255   // Non-relative fixups are only resolved if constant.
256   if (!BaseSection)
257     return Target.isAbsolute();
258
259   // Otherwise, relative fixups are only resolved if not a difference and the
260   // target is a temporary in the same section.
261   if (Target.isAbsolute() || Target.getSymB())
262     return false;
263
264   const MCSymbol *A = &Target.getSymA()->getSymbol();
265   if (!A->isTemporary() || !A->isInSection() ||
266       &A->getSection() != BaseSection)
267     return false;
268
269   return true;
270 }
271
272 static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
273                                           const MCAsmLayout &Layout,
274                                           const MCFixup &Fixup,
275                                           const MCValue Target,
276                                           const MCSymbolData *BaseSymbol) {
277   // The effective fixup address is
278   //     addr(atom(A)) + offset(A)
279   //   - addr(atom(B)) - offset(B)
280   //   - addr(BaseSymbol) + <fixup offset from base symbol>
281   // and the offsets are not relocatable, so the fixup is fully resolved when
282   //  addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
283   //
284   // Note that "false" is almost always conservatively correct (it means we emit
285   // a relocation which is unnecessary), except when it would force us to emit a
286   // relocation which the target cannot encode.
287
288   const MCSymbolData *A_Base = 0, *B_Base = 0;
289   if (const MCSymbolRefExpr *A = Target.getSymA()) {
290     // Modified symbol references cannot be resolved.
291     if (A->getKind() != MCSymbolRefExpr::VK_None)
292       return false;
293
294     A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
295     if (!A_Base)
296       return false;
297   }
298
299   if (const MCSymbolRefExpr *B = Target.getSymB()) {
300     // Modified symbol references cannot be resolved.
301     if (B->getKind() != MCSymbolRefExpr::VK_None)
302       return false;
303
304     B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
305     if (!B_Base)
306       return false;
307   }
308
309   // If there is no base, A and B have to be the same atom for this fixup to be
310   // fully resolved.
311   if (!BaseSymbol)
312     return A_Base == B_Base;
313
314   // Otherwise, B must be missing and A must be the base.
315   return !B_Base && BaseSymbol == A_Base;
316 }
317
318 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
319   // Non-temporary labels should always be visible to the linker.
320   if (!Symbol.isTemporary())
321     return true;
322
323   // Absolute temporary labels are never visible.
324   if (!Symbol.isInSection())
325     return false;
326
327   // Otherwise, check if the section requires symbols even for temporary labels.
328   return getBackend().doesSectionRequireSymbols(Symbol.getSection());
329 }
330
331 const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
332   // Linker visible symbols define atoms.
333   if (isSymbolLinkerVisible(SD->getSymbol()))
334     return SD;
335
336   // Absolute and undefined symbols have no defining atom.
337   if (!SD->getFragment())
338     return 0;
339
340   // Non-linker visible symbols in sections which can't be atomized have no
341   // defining atom.
342   if (!getBackend().isSectionAtomizable(
343         SD->getFragment()->getParent()->getSection()))
344     return 0;
345
346   // Otherwise, return the atom for the containing fragment.
347   return SD->getFragment()->getAtom();
348 }
349
350 bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
351                                 const MCFixup &Fixup, const MCFragment *DF,
352                                 MCValue &Target, uint64_t &Value) const {
353   ++stats::EvaluateFixup;
354
355   if (!Fixup.getValue()->EvaluateAsRelocatable(Target, &Layout))
356     report_fatal_error("expected relocatable expression");
357
358   // FIXME: How do non-scattered symbols work in ELF? I presume the linker
359   // doesn't support small relocations, but then under what criteria does the
360   // assembler allow symbol differences?
361
362   Value = Target.getConstant();
363
364   bool IsPCRel = Emitter.getFixupKindInfo(
365     Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
366   bool IsResolved = true;
367   if (const MCSymbolRefExpr *A = Target.getSymA()) {
368     if (A->getSymbol().isDefined())
369       Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
370     else
371       IsResolved = false;
372   }
373   if (const MCSymbolRefExpr *B = Target.getSymB()) {
374     if (B->getSymbol().isDefined())
375       Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
376     else
377       IsResolved = false;
378   }
379
380   // If we are using scattered symbols, determine whether this value is actually
381   // resolved; scattering may cause atoms to move.
382   if (IsResolved && getBackend().hasScatteredSymbols()) {
383     if (getBackend().hasReliableSymbolDifference()) {
384       // If this is a PCrel relocation, find the base atom (identified by its
385       // symbol) that the fixup value is relative to.
386       const MCSymbolData *BaseSymbol = 0;
387       if (IsPCRel) {
388         BaseSymbol = DF->getAtom();
389         if (!BaseSymbol)
390           IsResolved = false;
391       }
392
393       if (IsResolved)
394         IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
395                                                    BaseSymbol);
396     } else {
397       const MCSection *BaseSection = 0;
398       if (IsPCRel)
399         BaseSection = &DF->getParent()->getSection();
400
401       IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
402                                                        BaseSection);
403     }
404   }
405
406   if (IsPCRel)
407     Value -= Layout.getFragmentAddress(DF) + Fixup.getOffset();
408
409   return IsResolved;
410 }
411
412 uint64_t MCAssembler::ComputeFragmentSize(MCAsmLayout &Layout,
413                                           const MCFragment &F,
414                                           uint64_t SectionAddress,
415                                           uint64_t FragmentOffset) const {
416   switch (F.getKind()) {
417   case MCFragment::FT_Data:
418     return cast<MCDataFragment>(F).getContents().size();
419   case MCFragment::FT_Fill:
420     return cast<MCFillFragment>(F).getSize();
421   case MCFragment::FT_Inst:
422     return cast<MCInstFragment>(F).getInstSize();
423
424   case MCFragment::FT_Align: {
425     const MCAlignFragment &AF = cast<MCAlignFragment>(F);
426
427     assert((!AF.hasOnlyAlignAddress() || !AF.getNextNode()) &&
428            "Invalid OnlyAlignAddress bit, not the last fragment!");
429
430     uint64_t Size = OffsetToAlignment(SectionAddress + FragmentOffset,
431                                       AF.getAlignment());
432
433     // Honor MaxBytesToEmit.
434     if (Size > AF.getMaxBytesToEmit())
435       return 0;
436
437     return Size;
438   }
439
440   case MCFragment::FT_Org: {
441     const MCOrgFragment &OF = cast<MCOrgFragment>(F);
442
443     // FIXME: We should compute this sooner, we don't want to recurse here, and
444     // we would like to be more functional.
445     int64_t TargetLocation;
446     if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
447       report_fatal_error("expected assembly-time absolute expression");
448
449     // FIXME: We need a way to communicate this error.
450     int64_t Offset = TargetLocation - FragmentOffset;
451     if (Offset < 0 || Offset >= 0x40000000)
452       report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
453                          "' (at offset '" + Twine(FragmentOffset) + "')");
454
455     return Offset;
456   }
457   }
458
459   assert(0 && "invalid fragment kind");
460   return 0;
461 }
462
463 void MCAsmLayout::LayoutFile() {
464   // Initialize the first section and set the valid fragment layout point. All
465   // actual layout computations are done lazily.
466   LastValidFragment = 0;
467   if (!getSectionOrder().empty())
468     getSectionOrder().front()->Address = 0;
469 }
470
471 void MCAsmLayout::LayoutFragment(MCFragment *F) {
472   MCFragment *Prev = F->getPrevNode();
473
474   // We should never try to recompute something which is up-to-date.
475   assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!");
476   // We should never try to compute the fragment layout if the section isn't
477   // up-to-date.
478   assert(isSectionUpToDate(F->getParent()) &&
479          "Attempt to compute fragment before it's section!");
480   // We should never try to compute the fragment layout if it's predecessor
481   // isn't up-to-date.
482   assert((!Prev || isFragmentUpToDate(Prev)) &&
483          "Attempt to compute fragment before it's predecessor!");
484
485   ++stats::FragmentLayouts;
486
487   // Compute the fragment start address.
488   uint64_t StartAddress = F->getParent()->Address;
489   uint64_t Address = StartAddress;
490   if (Prev)
491     Address += Prev->Offset + Prev->EffectiveSize;
492
493   // Compute fragment offset and size.
494   F->Offset = Address - StartAddress;
495   F->EffectiveSize = getAssembler().ComputeFragmentSize(*this, *F, StartAddress,
496                                                         F->Offset);
497   LastValidFragment = F;
498
499   // If this is the last fragment in a section, update the next section address.
500   if (!F->getNextNode()) {
501     unsigned NextIndex = F->getParent()->getLayoutOrder() + 1;
502     if (NextIndex != getSectionOrder().size())
503       LayoutSection(getSectionOrder()[NextIndex]);
504   }
505 }
506
507 void MCAsmLayout::LayoutSection(MCSectionData *SD) {
508   unsigned SectionOrderIndex = SD->getLayoutOrder();
509
510   ++stats::SectionLayouts;
511
512   // Compute the section start address.
513   uint64_t StartAddress = 0;
514   if (SectionOrderIndex) {
515     MCSectionData *Prev = getSectionOrder()[SectionOrderIndex - 1];
516     StartAddress = getSectionAddress(Prev) + getSectionAddressSize(Prev);
517   }
518
519   // Honor the section alignment requirements.
520   StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment());
521
522   // Set the section address.
523   SD->Address = StartAddress;
524 }
525
526 /// WriteFragmentData - Write the \arg F data to the output file.
527 static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
528                               const MCFragment &F, MCObjectWriter *OW) {
529   uint64_t Start = OW->getStream().tell();
530   (void) Start;
531
532   ++stats::EmittedFragments;
533
534   // FIXME: Embed in fragments instead?
535   uint64_t FragmentSize = Layout.getFragmentEffectiveSize(&F);
536   switch (F.getKind()) {
537   case MCFragment::FT_Align: {
538     MCAlignFragment &AF = cast<MCAlignFragment>(F);
539     uint64_t Count = FragmentSize / AF.getValueSize();
540
541     assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
542
543     // FIXME: This error shouldn't actually occur (the front end should emit
544     // multiple .align directives to enforce the semantics it wants), but is
545     // severe enough that we want to report it. How to handle this?
546     if (Count * AF.getValueSize() != FragmentSize)
547       report_fatal_error("undefined .align directive, value size '" +
548                         Twine(AF.getValueSize()) +
549                         "' is not a divisor of padding size '" +
550                         Twine(FragmentSize) + "'");
551
552     // See if we are aligning with nops, and if so do that first to try to fill
553     // the Count bytes.  Then if that did not fill any bytes or there are any
554     // bytes left to fill use the the Value and ValueSize to fill the rest.
555     // If we are aligning with nops, ask that target to emit the right data.
556     if (AF.hasEmitNops()) {
557       if (!Asm.getBackend().WriteNopData(Count, OW))
558         report_fatal_error("unable to write nop sequence of " +
559                           Twine(Count) + " bytes");
560       break;
561     }
562
563     // Otherwise, write out in multiples of the value size.
564     for (uint64_t i = 0; i != Count; ++i) {
565       switch (AF.getValueSize()) {
566       default:
567         assert(0 && "Invalid size!");
568       case 1: OW->Write8 (uint8_t (AF.getValue())); break;
569       case 2: OW->Write16(uint16_t(AF.getValue())); break;
570       case 4: OW->Write32(uint32_t(AF.getValue())); break;
571       case 8: OW->Write64(uint64_t(AF.getValue())); break;
572       }
573     }
574     break;
575   }
576
577   case MCFragment::FT_Data: {
578     MCDataFragment &DF = cast<MCDataFragment>(F);
579     assert(FragmentSize == DF.getContents().size() && "Invalid size!");
580     OW->WriteBytes(DF.getContents().str());
581     break;
582   }
583
584   case MCFragment::FT_Fill: {
585     MCFillFragment &FF = cast<MCFillFragment>(F);
586
587     assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
588
589     for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
590       switch (FF.getValueSize()) {
591       default:
592         assert(0 && "Invalid size!");
593       case 1: OW->Write8 (uint8_t (FF.getValue())); break;
594       case 2: OW->Write16(uint16_t(FF.getValue())); break;
595       case 4: OW->Write32(uint32_t(FF.getValue())); break;
596       case 8: OW->Write64(uint64_t(FF.getValue())); break;
597       }
598     }
599     break;
600   }
601
602   case MCFragment::FT_Inst:
603     llvm_unreachable("unexpected inst fragment after lowering");
604     break;
605
606   case MCFragment::FT_Org: {
607     MCOrgFragment &OF = cast<MCOrgFragment>(F);
608
609     for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
610       OW->Write8(uint8_t(OF.getValue()));
611
612     break;
613   }
614   }
615
616   assert(OW->getStream().tell() - Start == FragmentSize);
617 }
618
619 void MCAssembler::WriteSectionData(const MCSectionData *SD,
620                                    const MCAsmLayout &Layout,
621                                    MCObjectWriter *OW) const {
622   // Ignore virtual sections.
623   if (getBackend().isVirtualSection(SD->getSection())) {
624     assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
625
626     // Check that contents are only things legal inside a virtual section.
627     for (MCSectionData::const_iterator it = SD->begin(),
628            ie = SD->end(); it != ie; ++it) {
629       switch (it->getKind()) {
630       default:
631         assert(0 && "Invalid fragment in virtual section!");
632       case MCFragment::FT_Data: {
633         // Check that we aren't trying to write a non-zero contents (or fixups)
634         // into a virtual section. This is to support clients which use standard
635         // directives to fill the contents of virtual sections.
636         MCDataFragment &DF = cast<MCDataFragment>(*it);
637         assert(DF.fixup_begin() == DF.fixup_end() &&
638                "Cannot have fixups in virtual section!");
639         for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
640           assert(DF.getContents()[i] == 0 &&
641                  "Invalid data value for virtual section!");
642         break;
643       }
644       case MCFragment::FT_Align:
645         // Check that we aren't trying to write a non-zero value into a virtual
646         // section.
647         assert((!cast<MCAlignFragment>(it)->getValueSize() ||
648                 !cast<MCAlignFragment>(it)->getValue()) &&
649                "Invalid align in virtual section!");
650         break;
651       case MCFragment::FT_Fill:
652         assert(!cast<MCFillFragment>(it)->getValueSize() &&
653                "Invalid fill in virtual section!");
654         break;
655       }
656     }
657
658     return;
659   }
660
661   uint64_t Start = OW->getStream().tell();
662   (void) Start;
663
664   for (MCSectionData::const_iterator it = SD->begin(),
665          ie = SD->end(); it != ie; ++it)
666     WriteFragmentData(*this, Layout, *it, OW);
667
668   assert(OW->getStream().tell() - Start == Layout.getSectionFileSize(SD));
669 }
670
671 void MCAssembler::AddSectionToTheEnd(MCSectionData &SD, MCAsmLayout &Layout) {
672   // Create dummy fragments and assign section ordinals.
673   unsigned SectionIndex = 0;
674   for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it)
675     SectionIndex++;
676
677   SD.setOrdinal(SectionIndex);
678
679   // Assign layout order indices to sections and fragments.
680   unsigned FragmentIndex = 0;
681   unsigned i = 0;
682   for (unsigned e = Layout.getSectionOrder().size(); i != e; ++i) {
683     MCSectionData *SD = Layout.getSectionOrder()[i];
684
685     for (MCSectionData::iterator it2 = SD->begin(),
686            ie2 = SD->end(); it2 != ie2; ++it2)
687       FragmentIndex++;
688   }
689
690   SD.setLayoutOrder(i);
691   for (MCSectionData::iterator it2 = SD.begin(),
692          ie2 = SD.end(); it2 != ie2; ++it2) {
693     it2->setLayoutOrder(FragmentIndex++);
694   }
695   Layout.getSectionOrder().push_back(&SD);
696
697   Layout.LayoutSection(&SD);
698
699   // Layout until everything fits.
700   while (LayoutOnce(Layout))
701     continue;
702
703 }
704
705 void MCAssembler::Finish(MCObjectWriter *Writer) {
706   DEBUG_WITH_TYPE("mc-dump", {
707       llvm::errs() << "assembler backend - pre-layout\n--\n";
708       dump(); });
709
710   // Create the layout object.
711   MCAsmLayout Layout(*this);
712
713   // Insert additional align fragments for concrete sections to explicitly pad
714   // the previous section to match their alignment requirements. This is for
715   // 'gas' compatibility, it shouldn't strictly be necessary.
716   if (PadSectionToAlignment) {
717     for (unsigned i = 1, e = Layout.getSectionOrder().size(); i < e; ++i) {
718       MCSectionData *SD = Layout.getSectionOrder()[i];
719
720       // Ignore sections without alignment requirements.
721       unsigned Align = SD->getAlignment();
722       if (Align <= 1)
723         continue;
724
725       // Ignore virtual sections, they don't cause file size modifications.
726       if (getBackend().isVirtualSection(SD->getSection()))
727         continue;
728
729       // Otherwise, create a new align fragment at the end of the previous
730       // section.
731       MCAlignFragment *AF = new MCAlignFragment(Align, 0, 1, Align,
732                                                 Layout.getSectionOrder()[i - 1]);
733       AF->setOnlyAlignAddress(true);
734     }
735   }
736
737   // Create dummy fragments and assign section ordinals.
738   unsigned SectionIndex = 0;
739   for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
740     // Create dummy fragments to eliminate any empty sections, this simplifies
741     // layout.
742     if (it->getFragmentList().empty())
743       new MCDataFragment(it);
744
745     it->setOrdinal(SectionIndex++);
746   }
747
748   // Assign layout order indices to sections and fragments.
749   unsigned FragmentIndex = 0;
750   for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
751     MCSectionData *SD = Layout.getSectionOrder()[i];
752     SD->setLayoutOrder(i);
753
754     for (MCSectionData::iterator it2 = SD->begin(),
755            ie2 = SD->end(); it2 != ie2; ++it2)
756       it2->setLayoutOrder(FragmentIndex++);
757   }
758
759   // Layout until everything fits.
760   while (LayoutOnce(Layout))
761     continue;
762
763   DEBUG_WITH_TYPE("mc-dump", {
764       llvm::errs() << "assembler backend - post-relaxation\n--\n";
765       dump(); });
766
767   // Finalize the layout, including fragment lowering.
768   FinishLayout(Layout);
769
770   DEBUG_WITH_TYPE("mc-dump", {
771       llvm::errs() << "assembler backend - final-layout\n--\n";
772       dump(); });
773
774   uint64_t StartOffset = OS.tell();
775
776   llvm::OwningPtr<MCObjectWriter> OwnWriter(0);
777   if (Writer == 0) {
778     //no custom Writer_ : create the default one life-managed by OwningPtr
779     OwnWriter.reset(getBackend().createObjectWriter(OS));
780     Writer = OwnWriter.get();
781     if (!Writer)
782       report_fatal_error("unable to create object writer!");
783   }
784
785   // Allow the object writer a chance to perform post-layout binding (for
786   // example, to set the index fields in the symbol data).
787   Writer->ExecutePostLayoutBinding(*this);
788
789   // Evaluate and apply the fixups, generating relocation entries as necessary.
790   for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
791     for (MCSectionData::iterator it2 = it->begin(),
792            ie2 = it->end(); it2 != ie2; ++it2) {
793       MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
794       if (!DF)
795         continue;
796
797       for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
798              ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
799         MCFixup &Fixup = *it3;
800
801         // Evaluate the fixup.
802         MCValue Target;
803         uint64_t FixedValue;
804         if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
805           // The fixup was unresolved, we need a relocation. Inform the object
806           // writer of the relocation, and give it an opportunity to adjust the
807           // fixup value if need be.
808           Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
809         }
810
811         getBackend().ApplyFixup(Fixup, *DF, FixedValue);
812       }
813     }
814   }
815
816   // Write the object file.
817   Writer->WriteObject(*this, Layout);
818
819   stats::ObjectBytes += OS.tell() - StartOffset;
820 }
821
822 bool MCAssembler::FixupNeedsRelaxation(const MCFixup &Fixup,
823                                        const MCFragment *DF,
824                                        const MCAsmLayout &Layout) const {
825   if (getRelaxAll())
826     return true;
827
828   // If we cannot resolve the fixup value, it requires relaxation.
829   MCValue Target;
830   uint64_t Value;
831   if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
832     return true;
833
834   // Otherwise, relax if the value is too big for a (signed) i8.
835   //
836   // FIXME: This is target dependent!
837   return int64_t(Value) != int64_t(int8_t(Value));
838 }
839
840 bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
841                                           const MCAsmLayout &Layout) const {
842   // If this inst doesn't ever need relaxation, ignore it. This occurs when we
843   // are intentionally pushing out inst fragments, or because we relaxed a
844   // previous instruction to one that doesn't need relaxation.
845   if (!getBackend().MayNeedRelaxation(IF->getInst()))
846     return false;
847
848   for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
849          ie = IF->fixup_end(); it != ie; ++it)
850     if (FixupNeedsRelaxation(*it, IF, Layout))
851       return true;
852
853   return false;
854 }
855
856 bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
857   ++stats::RelaxationSteps;
858
859   // Layout the sections in order.
860   Layout.LayoutFile();
861
862   // Scan for fragments that need relaxation.
863   bool WasRelaxed = false;
864   for (iterator it = begin(), ie = end(); it != ie; ++it) {
865     MCSectionData &SD = *it;
866
867     for (MCSectionData::iterator it2 = SD.begin(),
868            ie2 = SD.end(); it2 != ie2; ++it2) {
869       // Check if this is an instruction fragment that needs relaxation.
870       MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
871       if (!IF || !FragmentNeedsRelaxation(IF, Layout))
872         continue;
873
874       ++stats::RelaxedInstructions;
875
876       // FIXME-PERF: We could immediately lower out instructions if we can tell
877       // they are fully resolved, to avoid retesting on later passes.
878
879       // Relax the fragment.
880
881       MCInst Relaxed;
882       getBackend().RelaxInstruction(IF->getInst(), Relaxed);
883
884       // Encode the new instruction.
885       //
886       // FIXME-PERF: If it matters, we could let the target do this. It can
887       // probably do so more efficiently in many cases.
888       SmallVector<MCFixup, 4> Fixups;
889       SmallString<256> Code;
890       raw_svector_ostream VecOS(Code);
891       getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
892       VecOS.flush();
893
894       // Update the instruction fragment.
895       int SlideAmount = Code.size() - IF->getInstSize();
896       IF->setInst(Relaxed);
897       IF->getCode() = Code;
898       IF->getFixups().clear();
899       // FIXME: Eliminate copy.
900       for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
901         IF->getFixups().push_back(Fixups[i]);
902
903       // Update the layout, and remember that we relaxed.
904       Layout.UpdateForSlide(IF, SlideAmount);
905       WasRelaxed = true;
906     }
907   }
908
909   return WasRelaxed;
910 }
911
912 void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
913   // Lower out any instruction fragments, to simplify the fixup application and
914   // output.
915   //
916   // FIXME-PERF: We don't have to do this, but the assumption is that it is
917   // cheap (we will mostly end up eliminating fragments and appending on to data
918   // fragments), so the extra complexity downstream isn't worth it. Evaluate
919   // this assumption.
920   for (iterator it = begin(), ie = end(); it != ie; ++it) {
921     MCSectionData &SD = *it;
922
923     for (MCSectionData::iterator it2 = SD.begin(),
924            ie2 = SD.end(); it2 != ie2; ++it2) {
925       MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
926       if (!IF)
927         continue;
928
929       // Create a new data fragment for the instruction.
930       //
931       // FIXME-PERF: Reuse previous data fragment if possible.
932       MCDataFragment *DF = new MCDataFragment();
933       SD.getFragmentList().insert(it2, DF);
934
935       // Update the data fragments layout data.
936       DF->setParent(IF->getParent());
937       DF->setAtom(IF->getAtom());
938       DF->setLayoutOrder(IF->getLayoutOrder());
939       Layout.FragmentReplaced(IF, DF);
940
941       // Copy in the data and the fixups.
942       DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
943       for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
944         DF->getFixups().push_back(IF->getFixups()[i]);
945
946       // Delete the instruction fragment and update the iterator.
947       SD.getFragmentList().erase(IF);
948       it2 = DF;
949     }
950   }
951 }
952
953 // Debugging methods
954
955 namespace llvm {
956
957 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
958   OS << "<MCFixup" << " Offset:" << AF.getOffset()
959      << " Value:" << *AF.getValue()
960      << " Kind:" << AF.getKind() << ">";
961   return OS;
962 }
963
964 }
965
966 void MCFragment::dump() {
967   raw_ostream &OS = llvm::errs();
968
969   OS << "<";
970   switch (getKind()) {
971   case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
972   case MCFragment::FT_Data:  OS << "MCDataFragment"; break;
973   case MCFragment::FT_Fill:  OS << "MCFillFragment"; break;
974   case MCFragment::FT_Inst:  OS << "MCInstFragment"; break;
975   case MCFragment::FT_Org:   OS << "MCOrgFragment"; break;
976   }
977
978   OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
979      << " Offset:" << Offset << " EffectiveSize:" << EffectiveSize << ">";
980
981   switch (getKind()) {
982   case MCFragment::FT_Align: {
983     const MCAlignFragment *AF = cast<MCAlignFragment>(this);
984     if (AF->hasEmitNops())
985       OS << " (emit nops)";
986     if (AF->hasOnlyAlignAddress())
987       OS << " (only align section)";
988     OS << "\n       ";
989     OS << " Alignment:" << AF->getAlignment()
990        << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
991        << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
992     break;
993   }
994   case MCFragment::FT_Data:  {
995     const MCDataFragment *DF = cast<MCDataFragment>(this);
996     OS << "\n       ";
997     OS << " Contents:[";
998     const SmallVectorImpl<char> &Contents = DF->getContents();
999     for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
1000       if (i) OS << ",";
1001       OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1002     }
1003     OS << "] (" << Contents.size() << " bytes)";
1004
1005     if (!DF->getFixups().empty()) {
1006       OS << ",\n       ";
1007       OS << " Fixups:[";
1008       for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
1009              ie = DF->fixup_end(); it != ie; ++it) {
1010         if (it != DF->fixup_begin()) OS << ",\n                ";
1011         OS << *it;
1012       }
1013       OS << "]";
1014     }
1015     break;
1016   }
1017   case MCFragment::FT_Fill:  {
1018     const MCFillFragment *FF = cast<MCFillFragment>(this);
1019     OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
1020        << " Size:" << FF->getSize();
1021     break;
1022   }
1023   case MCFragment::FT_Inst:  {
1024     const MCInstFragment *IF = cast<MCInstFragment>(this);
1025     OS << "\n       ";
1026     OS << " Inst:";
1027     IF->getInst().dump_pretty(OS);
1028     break;
1029   }
1030   case MCFragment::FT_Org:  {
1031     const MCOrgFragment *OF = cast<MCOrgFragment>(this);
1032     OS << "\n       ";
1033     OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
1034     break;
1035   }
1036   }
1037   OS << ">";
1038 }
1039
1040 void MCSectionData::dump() {
1041   raw_ostream &OS = llvm::errs();
1042
1043   OS << "<MCSectionData";
1044   OS << " Alignment:" << getAlignment() << " Address:" << Address
1045      << " Fragments:[\n      ";
1046   for (iterator it = begin(), ie = end(); it != ie; ++it) {
1047     if (it != begin()) OS << ",\n      ";
1048     it->dump();
1049   }
1050   OS << "]>";
1051 }
1052
1053 void MCSymbolData::dump() {
1054   raw_ostream &OS = llvm::errs();
1055
1056   OS << "<MCSymbolData Symbol:" << getSymbol()
1057      << " Fragment:" << getFragment() << " Offset:" << getOffset()
1058      << " Flags:" << getFlags() << " Index:" << getIndex();
1059   if (isCommon())
1060     OS << " (common, size:" << getCommonSize()
1061        << " align: " << getCommonAlignment() << ")";
1062   if (isExternal())
1063     OS << " (external)";
1064   if (isPrivateExtern())
1065     OS << " (private extern)";
1066   OS << ">";
1067 }
1068
1069 void MCAssembler::dump() {
1070   raw_ostream &OS = llvm::errs();
1071
1072   OS << "<MCAssembler\n";
1073   OS << "  Sections:[\n    ";
1074   for (iterator it = begin(), ie = end(); it != ie; ++it) {
1075     if (it != begin()) OS << ",\n    ";
1076     it->dump();
1077   }
1078   OS << "],\n";
1079   OS << "  Symbols:[";
1080
1081   for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1082     if (it != symbol_begin()) OS << ",\n           ";
1083     it->dump();
1084   }
1085   OS << "]>\n";
1086 }