Move target-specific logic out of generic MCAssembler.
[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/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/MC/MCAsmBackend.h"
22 #include "llvm/ADT/OwningPtr.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/TargetRegistry.h"
30
31 using namespace llvm;
32
33 namespace {
34 namespace stats {
35 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
36 STATISTIC(evaluateFixup, "Number of evaluated fixups");
37 STATISTIC(FragmentLayouts, "Number of fragment layouts");
38 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
39 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
40 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
41 }
42 }
43
44 // FIXME FIXME FIXME: There are number of places in this file where we convert
45 // what is a 64-bit assembler value used for computation into a value in the
46 // object file, which may truncate it. We should detect that truncation where
47 // invalid and report errors back.
48
49 /* *** */
50
51 MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
52   : Assembler(Asm), LastValidFragment()
53  {
54   // Compute the section layout order. Virtual sections must go last.
55   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
56     if (!it->getSection().isVirtualSection())
57       SectionOrder.push_back(&*it);
58   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
59     if (it->getSection().isVirtualSection())
60       SectionOrder.push_back(&*it);
61 }
62
63 bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const {
64   const MCSectionData &SD = *F->getParent();
65   const MCFragment *LastValid = LastValidFragment.lookup(&SD);
66   if (!LastValid)
67     return false;
68   assert(LastValid->getParent() == F->getParent());
69   return F->getLayoutOrder() <= LastValid->getLayoutOrder();
70 }
71
72 void MCAsmLayout::Invalidate(MCFragment *F) {
73   // If this fragment wasn't already up-to-date, we don't need to do anything.
74   if (!isFragmentUpToDate(F))
75     return;
76
77   // Otherwise, reset the last valid fragment to this fragment.
78   const MCSectionData &SD = *F->getParent();
79   LastValidFragment[&SD] = F;
80 }
81
82 void MCAsmLayout::EnsureValid(const MCFragment *F) const {
83   MCSectionData &SD = *F->getParent();
84
85   MCFragment *Cur = LastValidFragment[&SD];
86   if (!Cur)
87     Cur = &*SD.begin();
88   else
89     Cur = Cur->getNextNode();
90
91   // Advance the layout position until the fragment is up-to-date.
92   while (!isFragmentUpToDate(F)) {
93     const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur);
94     Cur = Cur->getNextNode();
95   }
96 }
97
98 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
99   EnsureValid(F);
100   assert(F->Offset != ~UINT64_C(0) && "Address not set!");
101   return F->Offset;
102 }
103
104 uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
105   const MCSymbol &S = SD->getSymbol();
106
107   // If this is a variable, then recursively evaluate now.
108   if (S.isVariable()) {
109     MCValue Target;
110     if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
111       report_fatal_error("unable to evaluate offset for variable '" +
112                          S.getName() + "'");
113
114     // Verify that any used symbols are defined.
115     if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
116       report_fatal_error("unable to evaluate offset to undefined symbol '" +
117                          Target.getSymA()->getSymbol().getName() + "'");
118     if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
119       report_fatal_error("unable to evaluate offset to undefined symbol '" +
120                          Target.getSymB()->getSymbol().getName() + "'");
121
122     uint64_t Offset = Target.getConstant();
123     if (Target.getSymA())
124       Offset += getSymbolOffset(&Assembler.getSymbolData(
125                                   Target.getSymA()->getSymbol()));
126     if (Target.getSymB())
127       Offset -= getSymbolOffset(&Assembler.getSymbolData(
128                                   Target.getSymB()->getSymbol()));
129     return Offset;
130   }
131
132   assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
133   return getFragmentOffset(SD->getFragment()) + SD->getOffset();
134 }
135
136 uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
137   // The size is the last fragment's end offset.
138   const MCFragment &F = SD->getFragmentList().back();
139   return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
140 }
141
142 uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
143   // Virtual sections have no file size.
144   if (SD->getSection().isVirtualSection())
145     return 0;
146
147   // Otherwise, the file size is the same as the address space size.
148   return getSectionAddressSize(SD);
149 }
150
151 /* *** */
152
153 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
154 }
155
156 MCFragment::~MCFragment() {
157 }
158
159 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
160   : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
161 {
162   if (Parent)
163     Parent->getFragmentList().push_back(this);
164 }
165
166 /* *** */
167
168 MCSectionData::MCSectionData() : Section(0) {}
169
170 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
171   : Section(&_Section),
172     Ordinal(~UINT32_C(0)),
173     Alignment(1),
174     HasInstructions(false)
175 {
176   if (A)
177     A->getSectionList().push_back(this);
178 }
179
180 /* *** */
181
182 MCSymbolData::MCSymbolData() : Symbol(0) {}
183
184 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
185                            uint64_t _Offset, MCAssembler *A)
186   : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
187     IsExternal(false), IsPrivateExtern(false),
188     CommonSize(0), SymbolSize(0), CommonAlign(0),
189     Flags(0), Index(0)
190 {
191   if (A)
192     A->getSymbolList().push_back(this);
193 }
194
195 /* *** */
196
197 MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
198                          MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
199                          raw_ostream &OS_)
200   : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
201     OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false)
202 {
203 }
204
205 MCAssembler::~MCAssembler() {
206 }
207
208 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
209   // Non-temporary labels should always be visible to the linker.
210   if (!Symbol.isTemporary())
211     return true;
212
213   // Absolute temporary labels are never visible.
214   if (!Symbol.isInSection())
215     return false;
216
217   // Otherwise, check if the section requires symbols even for temporary labels.
218   return getBackend().doesSectionRequireSymbols(Symbol.getSection());
219 }
220
221 const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
222   // Linker visible symbols define atoms.
223   if (isSymbolLinkerVisible(SD->getSymbol()))
224     return SD;
225
226   // Absolute and undefined symbols have no defining atom.
227   if (!SD->getFragment())
228     return 0;
229
230   // Non-linker visible symbols in sections which can't be atomized have no
231   // defining atom.
232   if (!getBackend().isSectionAtomizable(
233         SD->getFragment()->getParent()->getSection()))
234     return 0;
235
236   // Otherwise, return the atom for the containing fragment.
237   return SD->getFragment()->getAtom();
238 }
239
240 bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
241                                 const MCFixup &Fixup, const MCFragment *DF,
242                                 MCValue &Target, uint64_t &Value) const {
243   ++stats::evaluateFixup;
244
245   if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
246     report_fatal_error("expected relocatable expression");
247
248   bool IsPCRel = Backend.getFixupKindInfo(
249     Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
250
251   bool IsResolved;
252   if (IsPCRel) {
253     if (Target.getSymB()) {
254       IsResolved = false;
255     } else if (!Target.getSymA()) {
256       IsResolved = false;
257     } else {
258       const MCSymbolRefExpr *A = Target.getSymA();
259       const MCSymbol &SA = A->getSymbol();
260       if (A->getKind() != MCSymbolRefExpr::VK_None ||
261           SA.AliasedSymbol().isUndefined()) {
262         IsResolved = false;
263       } else {
264         const MCSymbolData &DataA = getSymbolData(SA);
265         IsResolved =
266           getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
267                                                              *DF, false, true);
268       }
269     }
270   } else {
271     IsResolved = Target.isAbsolute();
272   }
273
274   Value = Target.getConstant();
275
276   bool IsThumb = false;
277   if (const MCSymbolRefExpr *A = Target.getSymA()) {
278     const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
279     if (Sym.isDefined())
280       Value += Layout.getSymbolOffset(&getSymbolData(Sym));
281     if (isThumbFunc(&Sym))
282       IsThumb = true;
283   }
284   if (const MCSymbolRefExpr *B = Target.getSymB()) {
285     const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
286     if (Sym.isDefined())
287       Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
288   }
289
290
291   bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
292                          MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
293   assert((ShouldAlignPC ? IsPCRel : true) &&
294     "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
295
296   if (IsPCRel) {
297     uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
298
299     // A number of ARM fixups in Thumb mode require that the effective PC
300     // address be determined as the 32-bit aligned version of the actual offset.
301     if (ShouldAlignPC) Offset &= ~0x3;
302     Value -= Offset;
303   }
304
305   // ARM fixups based from a thumb function address need to have the low
306   // bit set. The actual value is always at least 16-bit aligned, so the
307   // low bit is normally clear and available for use as an ISA flag for
308   // interworking.
309   if (IsThumb)
310     Value |= 1;
311
312   return IsResolved;
313 }
314
315 uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
316                                           const MCFragment &F) const {
317   switch (F.getKind()) {
318   case MCFragment::FT_Data:
319     return cast<MCDataFragment>(F).getContents().size();
320   case MCFragment::FT_Fill:
321     return cast<MCFillFragment>(F).getSize();
322   case MCFragment::FT_Inst:
323     return cast<MCInstFragment>(F).getInstSize();
324
325   case MCFragment::FT_LEB:
326     return cast<MCLEBFragment>(F).getContents().size();
327
328   case MCFragment::FT_Align: {
329     const MCAlignFragment &AF = cast<MCAlignFragment>(F);
330     unsigned Offset = Layout.getFragmentOffset(&AF);
331     unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
332     if (Size > AF.getMaxBytesToEmit())
333       return 0;
334     return Size;
335   }
336
337   case MCFragment::FT_Org: {
338     MCOrgFragment &OF = cast<MCOrgFragment>(F);
339     int64_t TargetLocation;
340     if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
341       report_fatal_error("expected assembly-time absolute expression");
342
343     // FIXME: We need a way to communicate this error.
344     uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
345     int64_t Size = TargetLocation - FragmentOffset;
346     if (Size < 0 || Size >= 0x40000000)
347       report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
348                          "' (at offset '" + Twine(FragmentOffset) + "')");
349     return Size;
350   }
351
352   case MCFragment::FT_Dwarf:
353     return cast<MCDwarfLineAddrFragment>(F).getContents().size();
354   case MCFragment::FT_DwarfFrame:
355     return cast<MCDwarfCallFrameFragment>(F).getContents().size();
356   }
357
358   assert(0 && "invalid fragment kind");
359   return 0;
360 }
361
362 void MCAsmLayout::LayoutFragment(MCFragment *F) {
363   MCFragment *Prev = F->getPrevNode();
364
365   // We should never try to recompute something which is up-to-date.
366   assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!");
367   // We should never try to compute the fragment layout if it's predecessor
368   // isn't up-to-date.
369   assert((!Prev || isFragmentUpToDate(Prev)) &&
370          "Attempt to compute fragment before it's predecessor!");
371
372   ++stats::FragmentLayouts;
373
374   // Compute fragment offset and size.
375   uint64_t Offset = 0;
376   if (Prev)
377     Offset += Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
378
379   F->Offset = Offset;
380   LastValidFragment[F->getParent()] = F;
381 }
382
383 /// WriteFragmentData - Write the \arg F data to the output file.
384 static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
385                               const MCFragment &F) {
386   MCObjectWriter *OW = &Asm.getWriter();
387   uint64_t Start = OW->getStream().tell();
388   (void) Start;
389
390   ++stats::EmittedFragments;
391
392   // FIXME: Embed in fragments instead?
393   uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
394   switch (F.getKind()) {
395   case MCFragment::FT_Align: {
396     MCAlignFragment &AF = cast<MCAlignFragment>(F);
397     uint64_t Count = FragmentSize / AF.getValueSize();
398
399     assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
400
401     // FIXME: This error shouldn't actually occur (the front end should emit
402     // multiple .align directives to enforce the semantics it wants), but is
403     // severe enough that we want to report it. How to handle this?
404     if (Count * AF.getValueSize() != FragmentSize)
405       report_fatal_error("undefined .align directive, value size '" +
406                         Twine(AF.getValueSize()) +
407                         "' is not a divisor of padding size '" +
408                         Twine(FragmentSize) + "'");
409
410     // See if we are aligning with nops, and if so do that first to try to fill
411     // the Count bytes.  Then if that did not fill any bytes or there are any
412     // bytes left to fill use the the Value and ValueSize to fill the rest.
413     // If we are aligning with nops, ask that target to emit the right data.
414     if (AF.hasEmitNops()) {
415       if (!Asm.getBackend().WriteNopData(Count, OW))
416         report_fatal_error("unable to write nop sequence of " +
417                           Twine(Count) + " bytes");
418       break;
419     }
420
421     // Otherwise, write out in multiples of the value size.
422     for (uint64_t i = 0; i != Count; ++i) {
423       switch (AF.getValueSize()) {
424       default:
425         assert(0 && "Invalid size!");
426       case 1: OW->Write8 (uint8_t (AF.getValue())); break;
427       case 2: OW->Write16(uint16_t(AF.getValue())); break;
428       case 4: OW->Write32(uint32_t(AF.getValue())); break;
429       case 8: OW->Write64(uint64_t(AF.getValue())); break;
430       }
431     }
432     break;
433   }
434
435   case MCFragment::FT_Data: {
436     MCDataFragment &DF = cast<MCDataFragment>(F);
437     assert(FragmentSize == DF.getContents().size() && "Invalid size!");
438     OW->WriteBytes(DF.getContents().str());
439     break;
440   }
441
442   case MCFragment::FT_Fill: {
443     MCFillFragment &FF = cast<MCFillFragment>(F);
444
445     assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
446
447     for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
448       switch (FF.getValueSize()) {
449       default:
450         assert(0 && "Invalid size!");
451       case 1: OW->Write8 (uint8_t (FF.getValue())); break;
452       case 2: OW->Write16(uint16_t(FF.getValue())); break;
453       case 4: OW->Write32(uint32_t(FF.getValue())); break;
454       case 8: OW->Write64(uint64_t(FF.getValue())); break;
455       }
456     }
457     break;
458   }
459
460   case MCFragment::FT_Inst: {
461     MCInstFragment &IF = cast<MCInstFragment>(F);
462     OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size()));
463     break;
464   }
465
466   case MCFragment::FT_LEB: {
467     MCLEBFragment &LF = cast<MCLEBFragment>(F);
468     OW->WriteBytes(LF.getContents().str());
469     break;
470   }
471
472   case MCFragment::FT_Org: {
473     MCOrgFragment &OF = cast<MCOrgFragment>(F);
474
475     for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
476       OW->Write8(uint8_t(OF.getValue()));
477
478     break;
479   }
480
481   case MCFragment::FT_Dwarf: {
482     const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
483     OW->WriteBytes(OF.getContents().str());
484     break;
485   }
486   case MCFragment::FT_DwarfFrame: {
487     const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
488     OW->WriteBytes(CF.getContents().str());
489     break;
490   }
491   }
492
493   assert(OW->getStream().tell() - Start == FragmentSize);
494 }
495
496 void MCAssembler::writeSectionData(const MCSectionData *SD,
497                                    const MCAsmLayout &Layout) const {
498   // Ignore virtual sections.
499   if (SD->getSection().isVirtualSection()) {
500     assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
501
502     // Check that contents are only things legal inside a virtual section.
503     for (MCSectionData::const_iterator it = SD->begin(),
504            ie = SD->end(); it != ie; ++it) {
505       switch (it->getKind()) {
506       default:
507         assert(0 && "Invalid fragment in virtual section!");
508       case MCFragment::FT_Data: {
509         // Check that we aren't trying to write a non-zero contents (or fixups)
510         // into a virtual section. This is to support clients which use standard
511         // directives to fill the contents of virtual sections.
512         MCDataFragment &DF = cast<MCDataFragment>(*it);
513         assert(DF.fixup_begin() == DF.fixup_end() &&
514                "Cannot have fixups in virtual section!");
515         for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
516           assert(DF.getContents()[i] == 0 &&
517                  "Invalid data value for virtual section!");
518         break;
519       }
520       case MCFragment::FT_Align:
521         // Check that we aren't trying to write a non-zero value into a virtual
522         // section.
523         assert((!cast<MCAlignFragment>(it)->getValueSize() ||
524                 !cast<MCAlignFragment>(it)->getValue()) &&
525                "Invalid align in virtual section!");
526         break;
527       case MCFragment::FT_Fill:
528         assert(!cast<MCFillFragment>(it)->getValueSize() &&
529                "Invalid fill in virtual section!");
530         break;
531       }
532     }
533
534     return;
535   }
536
537   uint64_t Start = getWriter().getStream().tell();
538   (void) Start;
539
540   for (MCSectionData::const_iterator it = SD->begin(),
541          ie = SD->end(); it != ie; ++it)
542     WriteFragmentData(*this, Layout, *it);
543
544   assert(getWriter().getStream().tell() - Start ==
545          Layout.getSectionAddressSize(SD));
546 }
547
548
549 uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
550                                   MCFragment &F,
551                                   const MCFixup &Fixup) {
552    // Evaluate the fixup.
553    MCValue Target;
554    uint64_t FixedValue;
555    if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
556      // The fixup was unresolved, we need a relocation. Inform the object
557      // writer of the relocation, and give it an opportunity to adjust the
558      // fixup value if need be.
559      getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
560    }
561    return FixedValue;
562  }
563
564 void MCAssembler::Finish() {
565   DEBUG_WITH_TYPE("mc-dump", {
566       llvm::errs() << "assembler backend - pre-layout\n--\n";
567       dump(); });
568
569   // Create the layout object.
570   MCAsmLayout Layout(*this);
571
572   // Create dummy fragments and assign section ordinals.
573   unsigned SectionIndex = 0;
574   for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
575     // Create dummy fragments to eliminate any empty sections, this simplifies
576     // layout.
577     if (it->getFragmentList().empty())
578       new MCDataFragment(it);
579
580     it->setOrdinal(SectionIndex++);
581   }
582
583   // Assign layout order indices to sections and fragments.
584   for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
585     MCSectionData *SD = Layout.getSectionOrder()[i];
586     SD->setLayoutOrder(i);
587
588     unsigned FragmentIndex = 0;
589     for (MCSectionData::iterator it2 = SD->begin(),
590            ie2 = SD->end(); it2 != ie2; ++it2)
591       it2->setLayoutOrder(FragmentIndex++);
592   }
593
594   // Layout until everything fits.
595   while (layoutOnce(Layout))
596     continue;
597
598   DEBUG_WITH_TYPE("mc-dump", {
599       llvm::errs() << "assembler backend - post-relaxation\n--\n";
600       dump(); });
601
602   // Finalize the layout, including fragment lowering.
603   finishLayout(Layout);
604
605   DEBUG_WITH_TYPE("mc-dump", {
606       llvm::errs() << "assembler backend - final-layout\n--\n";
607       dump(); });
608
609   uint64_t StartOffset = OS.tell();
610
611   // Allow the object writer a chance to perform post-layout binding (for
612   // example, to set the index fields in the symbol data).
613   getWriter().ExecutePostLayoutBinding(*this, Layout);
614
615   // Evaluate and apply the fixups, generating relocation entries as necessary.
616   for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
617     for (MCSectionData::iterator it2 = it->begin(),
618            ie2 = it->end(); it2 != ie2; ++it2) {
619       MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
620       if (DF) {
621         for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
622                ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
623           MCFixup &Fixup = *it3;
624           uint64_t FixedValue = handleFixup(Layout, *DF, Fixup);
625           getBackend().ApplyFixup(Fixup, DF->getContents().data(),
626                                   DF->getContents().size(), FixedValue);
627         }
628       }
629       MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
630       if (IF) {
631         for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(),
632                ie3 = IF->fixup_end(); it3 != ie3; ++it3) {
633           MCFixup &Fixup = *it3;
634           uint64_t FixedValue = handleFixup(Layout, *IF, Fixup);
635           getBackend().ApplyFixup(Fixup, IF->getCode().data(),
636                                   IF->getCode().size(), FixedValue);
637         }
638       }
639     }
640   }
641
642   // Write the object file.
643   getWriter().WriteObject(*this, Layout);
644
645   stats::ObjectBytes += OS.tell() - StartOffset;
646 }
647
648 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
649                                        const MCInstFragment *DF,
650                                        const MCAsmLayout &Layout) const {
651   if (getRelaxAll())
652     return true;
653
654   // If we cannot resolve the fixup value, it requires relaxation.
655   MCValue Target;
656   uint64_t Value;
657   if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
658     return true;
659
660   return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
661 }
662
663 bool MCAssembler::fragmentNeedsRelaxation(const MCInstFragment *IF,
664                                           const MCAsmLayout &Layout) const {
665   // If this inst doesn't ever need relaxation, ignore it. This occurs when we
666   // are intentionally pushing out inst fragments, or because we relaxed a
667   // previous instruction to one that doesn't need relaxation.
668   if (!getBackend().MayNeedRelaxation(IF->getInst()))
669     return false;
670
671   for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
672          ie = IF->fixup_end(); it != ie; ++it)
673     if (fixupNeedsRelaxation(*it, IF, Layout))
674       return true;
675
676   return false;
677 }
678
679 bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
680                                    MCInstFragment &IF) {
681   if (!fragmentNeedsRelaxation(&IF, Layout))
682     return false;
683
684   ++stats::RelaxedInstructions;
685
686   // FIXME-PERF: We could immediately lower out instructions if we can tell
687   // they are fully resolved, to avoid retesting on later passes.
688
689   // Relax the fragment.
690
691   MCInst Relaxed;
692   getBackend().RelaxInstruction(IF.getInst(), Relaxed);
693
694   // Encode the new instruction.
695   //
696   // FIXME-PERF: If it matters, we could let the target do this. It can
697   // probably do so more efficiently in many cases.
698   SmallVector<MCFixup, 4> Fixups;
699   SmallString<256> Code;
700   raw_svector_ostream VecOS(Code);
701   getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
702   VecOS.flush();
703
704   // Update the instruction fragment.
705   IF.setInst(Relaxed);
706   IF.getCode() = Code;
707   IF.getFixups().clear();
708   // FIXME: Eliminate copy.
709   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
710     IF.getFixups().push_back(Fixups[i]);
711
712   return true;
713 }
714
715 bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
716   int64_t Value = 0;
717   uint64_t OldSize = LF.getContents().size();
718   bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
719   (void)IsAbs;
720   assert(IsAbs);
721   SmallString<8> &Data = LF.getContents();
722   Data.clear();
723   raw_svector_ostream OSE(Data);
724   if (LF.isSigned())
725     MCObjectWriter::EncodeSLEB128(Value, OSE);
726   else
727     MCObjectWriter::EncodeULEB128(Value, OSE);
728   OSE.flush();
729   return OldSize != LF.getContents().size();
730 }
731
732 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
733                                      MCDwarfLineAddrFragment &DF) {
734   int64_t AddrDelta = 0;
735   uint64_t OldSize = DF.getContents().size();
736   bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
737   (void)IsAbs;
738   assert(IsAbs);
739   int64_t LineDelta;
740   LineDelta = DF.getLineDelta();
741   SmallString<8> &Data = DF.getContents();
742   Data.clear();
743   raw_svector_ostream OSE(Data);
744   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
745   OSE.flush();
746   return OldSize != Data.size();
747 }
748
749 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
750                                               MCDwarfCallFrameFragment &DF) {
751   int64_t AddrDelta = 0;
752   uint64_t OldSize = DF.getContents().size();
753   bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
754   (void)IsAbs;
755   assert(IsAbs);
756   SmallString<8> &Data = DF.getContents();
757   Data.clear();
758   raw_svector_ostream OSE(Data);
759   MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
760   OSE.flush();
761   return OldSize != Data.size();
762 }
763
764 bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout,
765                                     MCSectionData &SD) {
766   MCFragment *FirstInvalidFragment = NULL;
767   // Scan for fragments that need relaxation.
768   for (MCSectionData::iterator it2 = SD.begin(),
769          ie2 = SD.end(); it2 != ie2; ++it2) {
770     // Check if this is an fragment that needs relaxation.
771     bool relaxedFrag = false;
772     switch(it2->getKind()) {
773     default:
774           break;
775     case MCFragment::FT_Inst:
776       relaxedFrag = relaxInstruction(Layout, *cast<MCInstFragment>(it2));
777       break;
778     case MCFragment::FT_Dwarf:
779       relaxedFrag = relaxDwarfLineAddr(Layout,
780                                        *cast<MCDwarfLineAddrFragment>(it2));
781       break;
782     case MCFragment::FT_DwarfFrame:
783       relaxedFrag =
784         relaxDwarfCallFrameFragment(Layout,
785                                     *cast<MCDwarfCallFrameFragment>(it2));
786       break;
787     case MCFragment::FT_LEB:
788       relaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(it2));
789       break;
790     }
791     // Update the layout, and remember that we relaxed.
792     if (relaxedFrag && !FirstInvalidFragment)
793       FirstInvalidFragment = it2;
794   }
795   if (FirstInvalidFragment) {
796     Layout.Invalidate(FirstInvalidFragment);
797     return true;
798   }
799   return false;
800 }
801
802 bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
803   ++stats::RelaxationSteps;
804
805   bool WasRelaxed = false;
806   for (iterator it = begin(), ie = end(); it != ie; ++it) {
807     MCSectionData &SD = *it;
808     while(layoutSectionOnce(Layout, SD))
809       WasRelaxed = true;
810   }
811
812   return WasRelaxed;
813 }
814
815 void MCAssembler::finishLayout(MCAsmLayout &Layout) {
816   // The layout is done. Mark every fragment as valid.
817   for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
818     Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
819   }
820 }
821
822 // Debugging methods
823
824 namespace llvm {
825
826 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
827   OS << "<MCFixup" << " Offset:" << AF.getOffset()
828      << " Value:" << *AF.getValue()
829      << " Kind:" << AF.getKind() << ">";
830   return OS;
831 }
832
833 }
834
835 void MCFragment::dump() {
836   raw_ostream &OS = llvm::errs();
837
838   OS << "<";
839   switch (getKind()) {
840   case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
841   case MCFragment::FT_Data:  OS << "MCDataFragment"; break;
842   case MCFragment::FT_Fill:  OS << "MCFillFragment"; break;
843   case MCFragment::FT_Inst:  OS << "MCInstFragment"; break;
844   case MCFragment::FT_Org:   OS << "MCOrgFragment"; break;
845   case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
846   case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
847   case MCFragment::FT_LEB:   OS << "MCLEBFragment"; break;
848   }
849
850   OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
851      << " Offset:" << Offset << ">";
852
853   switch (getKind()) {
854   case MCFragment::FT_Align: {
855     const MCAlignFragment *AF = cast<MCAlignFragment>(this);
856     if (AF->hasEmitNops())
857       OS << " (emit nops)";
858     OS << "\n       ";
859     OS << " Alignment:" << AF->getAlignment()
860        << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
861        << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
862     break;
863   }
864   case MCFragment::FT_Data:  {
865     const MCDataFragment *DF = cast<MCDataFragment>(this);
866     OS << "\n       ";
867     OS << " Contents:[";
868     const SmallVectorImpl<char> &Contents = DF->getContents();
869     for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
870       if (i) OS << ",";
871       OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
872     }
873     OS << "] (" << Contents.size() << " bytes)";
874
875     if (!DF->getFixups().empty()) {
876       OS << ",\n       ";
877       OS << " Fixups:[";
878       for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
879              ie = DF->fixup_end(); it != ie; ++it) {
880         if (it != DF->fixup_begin()) OS << ",\n                ";
881         OS << *it;
882       }
883       OS << "]";
884     }
885     break;
886   }
887   case MCFragment::FT_Fill:  {
888     const MCFillFragment *FF = cast<MCFillFragment>(this);
889     OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
890        << " Size:" << FF->getSize();
891     break;
892   }
893   case MCFragment::FT_Inst:  {
894     const MCInstFragment *IF = cast<MCInstFragment>(this);
895     OS << "\n       ";
896     OS << " Inst:";
897     IF->getInst().dump_pretty(OS);
898     break;
899   }
900   case MCFragment::FT_Org:  {
901     const MCOrgFragment *OF = cast<MCOrgFragment>(this);
902     OS << "\n       ";
903     OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
904     break;
905   }
906   case MCFragment::FT_Dwarf:  {
907     const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
908     OS << "\n       ";
909     OS << " AddrDelta:" << OF->getAddrDelta()
910        << " LineDelta:" << OF->getLineDelta();
911     break;
912   }
913   case MCFragment::FT_DwarfFrame:  {
914     const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
915     OS << "\n       ";
916     OS << " AddrDelta:" << CF->getAddrDelta();
917     break;
918   }
919   case MCFragment::FT_LEB: {
920     const MCLEBFragment *LF = cast<MCLEBFragment>(this);
921     OS << "\n       ";
922     OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
923     break;
924   }
925   }
926   OS << ">";
927 }
928
929 void MCSectionData::dump() {
930   raw_ostream &OS = llvm::errs();
931
932   OS << "<MCSectionData";
933   OS << " Alignment:" << getAlignment() << " Fragments:[\n      ";
934   for (iterator it = begin(), ie = end(); it != ie; ++it) {
935     if (it != begin()) OS << ",\n      ";
936     it->dump();
937   }
938   OS << "]>";
939 }
940
941 void MCSymbolData::dump() {
942   raw_ostream &OS = llvm::errs();
943
944   OS << "<MCSymbolData Symbol:" << getSymbol()
945      << " Fragment:" << getFragment() << " Offset:" << getOffset()
946      << " Flags:" << getFlags() << " Index:" << getIndex();
947   if (isCommon())
948     OS << " (common, size:" << getCommonSize()
949        << " align: " << getCommonAlignment() << ")";
950   if (isExternal())
951     OS << " (external)";
952   if (isPrivateExtern())
953     OS << " (private extern)";
954   OS << ">";
955 }
956
957 void MCAssembler::dump() {
958   raw_ostream &OS = llvm::errs();
959
960   OS << "<MCAssembler\n";
961   OS << "  Sections:[\n    ";
962   for (iterator it = begin(), ie = end(); it != ie; ++it) {
963     if (it != begin()) OS << ",\n    ";
964     it->dump();
965   }
966   OS << "],\n";
967   OS << "  Symbols:[";
968
969   for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
970     if (it != symbol_begin()) OS << ",\n           ";
971     it->dump();
972   }
973   OS << "]>\n";
974 }