Use range loops for accessing file names. NFC.
[oota-llvm.git] / include / llvm / MC / MCAssembler.h
1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
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 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
12
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/MC/MCDirectives.h"
22 #include "llvm/MC/MCFixup.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCLinkerOptimizationHint.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/DataTypes.h"
30 #include <algorithm>
31 #include <vector> // FIXME: Shouldn't be needed.
32
33 namespace llvm {
34 class raw_ostream;
35 class MCAsmLayout;
36 class MCAssembler;
37 class MCContext;
38 class MCCodeEmitter;
39 class MCExpr;
40 class MCFragment;
41 class MCObjectWriter;
42 class MCSection;
43 class MCSubtargetInfo;
44 class MCValue;
45 class MCAsmBackend;
46
47 class MCFragment : public ilist_node<MCFragment> {
48   friend class MCAsmLayout;
49
50   MCFragment(const MCFragment &) = delete;
51   void operator=(const MCFragment &) = delete;
52
53 public:
54   enum FragmentType {
55     FT_Align,
56     FT_Data,
57     FT_CompactEncodedInst,
58     FT_Fill,
59     FT_Relaxable,
60     FT_Org,
61     FT_Dwarf,
62     FT_DwarfFrame,
63     FT_LEB
64   };
65
66 private:
67   FragmentType Kind;
68
69   /// The data for the section this fragment is in.
70   MCSection *Parent;
71
72   /// Atom - The atom this fragment is in, as represented by it's defining
73   /// symbol.
74   const MCSymbol *Atom;
75
76   /// \name Assembler Backend Data
77   /// @{
78   //
79   // FIXME: This could all be kept private to the assembler implementation.
80
81   /// Offset - The offset of this fragment in its section. This is ~0 until
82   /// initialized.
83   uint64_t Offset;
84
85   /// LayoutOrder - The layout order of this fragment.
86   unsigned LayoutOrder;
87
88   /// @}
89
90 protected:
91   MCFragment(FragmentType Kind, MCSection *Parent = nullptr);
92
93 public:
94   // Only for sentinel.
95   MCFragment();
96   virtual ~MCFragment();
97
98   FragmentType getKind() const { return Kind; }
99
100   MCSection *getParent() const { return Parent; }
101   void setParent(MCSection *Value) { Parent = Value; }
102
103   const MCSymbol *getAtom() const { return Atom; }
104   void setAtom(const MCSymbol *Value) { Atom = Value; }
105
106   unsigned getLayoutOrder() const { return LayoutOrder; }
107   void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
108
109   /// \brief Does this fragment have instructions emitted into it? By default
110   /// this is false, but specific fragment types may set it to true.
111   virtual bool hasInstructions() const { return false; }
112
113   /// \brief Should this fragment be placed at the end of an aligned bundle?
114   virtual bool alignToBundleEnd() const { return false; }
115   virtual void setAlignToBundleEnd(bool V) {}
116
117   /// \brief Get the padding size that must be inserted before this fragment.
118   /// Used for bundling. By default, no padding is inserted.
119   /// Note that padding size is restricted to 8 bits. This is an optimization
120   /// to reduce the amount of space used for each fragment. In practice, larger
121   /// padding should never be required.
122   virtual uint8_t getBundlePadding() const { return 0; }
123
124   /// \brief Set the padding size for this fragment. By default it's a no-op,
125   /// and only some fragments have a meaningful implementation.
126   virtual void setBundlePadding(uint8_t N) {}
127
128   void dump();
129 };
130
131 /// Interface implemented by fragments that contain encoded instructions and/or
132 /// data.
133 ///
134 class MCEncodedFragment : public MCFragment {
135   virtual void anchor();
136
137   uint8_t BundlePadding;
138
139 public:
140   MCEncodedFragment(MCFragment::FragmentType FType, MCSection *Sec = nullptr)
141       : MCFragment(FType, Sec), BundlePadding(0) {}
142   ~MCEncodedFragment() override;
143
144   virtual SmallVectorImpl<char> &getContents() = 0;
145   virtual const SmallVectorImpl<char> &getContents() const = 0;
146
147   uint8_t getBundlePadding() const override { return BundlePadding; }
148
149   void setBundlePadding(uint8_t N) override { BundlePadding = N; }
150
151   static bool classof(const MCFragment *F) {
152     MCFragment::FragmentType Kind = F->getKind();
153     switch (Kind) {
154     default:
155       return false;
156     case MCFragment::FT_Relaxable:
157     case MCFragment::FT_CompactEncodedInst:
158     case MCFragment::FT_Data:
159       return true;
160     }
161   }
162 };
163
164 /// Interface implemented by fragments that contain encoded instructions and/or
165 /// data and also have fixups registered.
166 ///
167 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
168   void anchor() override;
169
170 public:
171   MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
172                               MCSection *Sec = nullptr)
173       : MCEncodedFragment(FType, Sec) {}
174
175   ~MCEncodedFragmentWithFixups() override;
176
177   typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
178   typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
179
180   virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
181   virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
182
183   virtual fixup_iterator fixup_begin() = 0;
184   virtual const_fixup_iterator fixup_begin() const = 0;
185   virtual fixup_iterator fixup_end() = 0;
186   virtual const_fixup_iterator fixup_end() const = 0;
187
188   static bool classof(const MCFragment *F) {
189     MCFragment::FragmentType Kind = F->getKind();
190     return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
191   }
192 };
193
194 /// Fragment for data and encoded instructions.
195 ///
196 class MCDataFragment : public MCEncodedFragmentWithFixups {
197   void anchor() override;
198
199   /// \brief Does this fragment contain encoded instructions anywhere in it?
200   bool HasInstructions;
201
202   /// \brief Should this fragment be aligned to the end of a bundle?
203   bool AlignToBundleEnd;
204
205   SmallVector<char, 32> Contents;
206
207   /// Fixups - The list of fixups in this fragment.
208   SmallVector<MCFixup, 4> Fixups;
209
210 public:
211   MCDataFragment(MCSection *Sec = nullptr)
212       : MCEncodedFragmentWithFixups(FT_Data, Sec), HasInstructions(false),
213         AlignToBundleEnd(false) {}
214
215   SmallVectorImpl<char> &getContents() override { return Contents; }
216   const SmallVectorImpl<char> &getContents() const override { return Contents; }
217
218   SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
219
220   const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
221
222   bool hasInstructions() const override { return HasInstructions; }
223   virtual void setHasInstructions(bool V) { HasInstructions = V; }
224
225   bool alignToBundleEnd() const override { return AlignToBundleEnd; }
226   void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
227
228   fixup_iterator fixup_begin() override { return Fixups.begin(); }
229   const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
230
231   fixup_iterator fixup_end() override { return Fixups.end(); }
232   const_fixup_iterator fixup_end() const override { return Fixups.end(); }
233
234   static bool classof(const MCFragment *F) {
235     return F->getKind() == MCFragment::FT_Data;
236   }
237 };
238
239 /// This is a compact (memory-size-wise) fragment for holding an encoded
240 /// instruction (non-relaxable) that has no fixups registered. When applicable,
241 /// it can be used instead of MCDataFragment and lead to lower memory
242 /// consumption.
243 ///
244 class MCCompactEncodedInstFragment : public MCEncodedFragment {
245   void anchor() override;
246
247   /// \brief Should this fragment be aligned to the end of a bundle?
248   bool AlignToBundleEnd;
249
250   SmallVector<char, 4> Contents;
251
252 public:
253   MCCompactEncodedInstFragment(MCSection *Sec = nullptr)
254       : MCEncodedFragment(FT_CompactEncodedInst, Sec), AlignToBundleEnd(false) {
255   }
256
257   bool hasInstructions() const override { return true; }
258
259   SmallVectorImpl<char> &getContents() override { return Contents; }
260   const SmallVectorImpl<char> &getContents() const override { return Contents; }
261
262   bool alignToBundleEnd() const override { return AlignToBundleEnd; }
263   void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
264
265   static bool classof(const MCFragment *F) {
266     return F->getKind() == MCFragment::FT_CompactEncodedInst;
267   }
268 };
269
270 /// A relaxable fragment holds on to its MCInst, since it may need to be
271 /// relaxed during the assembler layout and relaxation stage.
272 ///
273 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
274   void anchor() override;
275
276   /// Inst - The instruction this is a fragment for.
277   MCInst Inst;
278
279   /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
280   /// Keep a copy instead of a reference to make sure that updates to STI
281   /// in the assembler are not seen here.
282   const MCSubtargetInfo STI;
283
284   /// Contents - Binary data for the currently encoded instruction.
285   SmallVector<char, 8> Contents;
286
287   /// Fixups - The list of fixups in this fragment.
288   SmallVector<MCFixup, 1> Fixups;
289
290 public:
291   MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
292                       MCSection *Sec = nullptr)
293       : MCEncodedFragmentWithFixups(FT_Relaxable, Sec), Inst(Inst), STI(STI) {}
294
295   SmallVectorImpl<char> &getContents() override { return Contents; }
296   const SmallVectorImpl<char> &getContents() const override { return Contents; }
297
298   const MCInst &getInst() const { return Inst; }
299   void setInst(const MCInst &Value) { Inst = Value; }
300
301   const MCSubtargetInfo &getSubtargetInfo() { return STI; }
302
303   SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
304
305   const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
306
307   bool hasInstructions() const override { return true; }
308
309   fixup_iterator fixup_begin() override { return Fixups.begin(); }
310   const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
311
312   fixup_iterator fixup_end() override { return Fixups.end(); }
313   const_fixup_iterator fixup_end() const override { return Fixups.end(); }
314
315   static bool classof(const MCFragment *F) {
316     return F->getKind() == MCFragment::FT_Relaxable;
317   }
318 };
319
320 class MCAlignFragment : public MCFragment {
321   virtual void anchor();
322
323   /// Alignment - The alignment to ensure, in bytes.
324   unsigned Alignment;
325
326   /// Value - Value to use for filling padding bytes.
327   int64_t Value;
328
329   /// ValueSize - The size of the integer (in bytes) of \p Value.
330   unsigned ValueSize;
331
332   /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
333   /// cannot be satisfied in this width then this fragment is ignored.
334   unsigned MaxBytesToEmit;
335
336   /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
337   /// of using the provided value. The exact interpretation of this flag is
338   /// target dependent.
339   bool EmitNops : 1;
340
341 public:
342   MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
343                   unsigned MaxBytesToEmit, MCSection *Sec = nullptr)
344       : MCFragment(FT_Align, Sec), Alignment(Alignment), Value(Value),
345         ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit), EmitNops(false) {}
346
347   /// \name Accessors
348   /// @{
349
350   unsigned getAlignment() const { return Alignment; }
351
352   int64_t getValue() const { return Value; }
353
354   unsigned getValueSize() const { return ValueSize; }
355
356   unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
357
358   bool hasEmitNops() const { return EmitNops; }
359   void setEmitNops(bool Value) { EmitNops = Value; }
360
361   /// @}
362
363   static bool classof(const MCFragment *F) {
364     return F->getKind() == MCFragment::FT_Align;
365   }
366 };
367
368 class MCFillFragment : public MCFragment {
369   virtual void anchor();
370
371   /// Value - Value to use for filling bytes.
372   int64_t Value;
373
374   /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
375   /// this is a virtual fill fragment.
376   unsigned ValueSize;
377
378   /// Size - The number of bytes to insert.
379   uint64_t Size;
380
381 public:
382   MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
383                  MCSection *Sec = nullptr)
384       : MCFragment(FT_Fill, Sec), Value(Value), ValueSize(ValueSize),
385         Size(Size) {
386     assert((!ValueSize || (Size % ValueSize) == 0) &&
387            "Fill size must be a multiple of the value size!");
388   }
389
390   /// \name Accessors
391   /// @{
392
393   int64_t getValue() const { return Value; }
394
395   unsigned getValueSize() const { return ValueSize; }
396
397   uint64_t getSize() const { return Size; }
398
399   /// @}
400
401   static bool classof(const MCFragment *F) {
402     return F->getKind() == MCFragment::FT_Fill;
403   }
404 };
405
406 class MCOrgFragment : public MCFragment {
407   virtual void anchor();
408
409   /// Offset - The offset this fragment should start at.
410   const MCExpr *Offset;
411
412   /// Value - Value to use for filling bytes.
413   int8_t Value;
414
415 public:
416   MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSection *Sec = nullptr)
417       : MCFragment(FT_Org, Sec), Offset(&Offset), Value(Value) {}
418
419   /// \name Accessors
420   /// @{
421
422   const MCExpr &getOffset() const { return *Offset; }
423
424   uint8_t getValue() const { return Value; }
425
426   /// @}
427
428   static bool classof(const MCFragment *F) {
429     return F->getKind() == MCFragment::FT_Org;
430   }
431 };
432
433 class MCLEBFragment : public MCFragment {
434   virtual void anchor();
435
436   /// Value - The value this fragment should contain.
437   const MCExpr *Value;
438
439   /// IsSigned - True if this is a sleb128, false if uleb128.
440   bool IsSigned;
441
442   SmallString<8> Contents;
443
444 public:
445   MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr)
446       : MCFragment(FT_LEB, Sec), Value(&Value_), IsSigned(IsSigned_) {
447     Contents.push_back(0);
448   }
449
450   /// \name Accessors
451   /// @{
452
453   const MCExpr &getValue() const { return *Value; }
454
455   bool isSigned() const { return IsSigned; }
456
457   SmallString<8> &getContents() { return Contents; }
458   const SmallString<8> &getContents() const { return Contents; }
459
460   /// @}
461
462   static bool classof(const MCFragment *F) {
463     return F->getKind() == MCFragment::FT_LEB;
464   }
465 };
466
467 class MCDwarfLineAddrFragment : public MCFragment {
468   virtual void anchor();
469
470   /// LineDelta - the value of the difference between the two line numbers
471   /// between two .loc dwarf directives.
472   int64_t LineDelta;
473
474   /// AddrDelta - The expression for the difference of the two symbols that
475   /// make up the address delta between two .loc dwarf directives.
476   const MCExpr *AddrDelta;
477
478   SmallString<8> Contents;
479
480 public:
481   MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
482                           MCSection *Sec = nullptr)
483       : MCFragment(FT_Dwarf, Sec), LineDelta(LineDelta), AddrDelta(&AddrDelta) {
484     Contents.push_back(0);
485   }
486
487   /// \name Accessors
488   /// @{
489
490   int64_t getLineDelta() const { return LineDelta; }
491
492   const MCExpr &getAddrDelta() const { return *AddrDelta; }
493
494   SmallString<8> &getContents() { return Contents; }
495   const SmallString<8> &getContents() const { return Contents; }
496
497   /// @}
498
499   static bool classof(const MCFragment *F) {
500     return F->getKind() == MCFragment::FT_Dwarf;
501   }
502 };
503
504 class MCDwarfCallFrameFragment : public MCFragment {
505   virtual void anchor();
506
507   /// AddrDelta - The expression for the difference of the two symbols that
508   /// make up the address delta between two .cfi_* dwarf directives.
509   const MCExpr *AddrDelta;
510
511   SmallString<8> Contents;
512
513 public:
514   MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr)
515       : MCFragment(FT_DwarfFrame, Sec), AddrDelta(&AddrDelta) {
516     Contents.push_back(0);
517   }
518
519   /// \name Accessors
520   /// @{
521
522   const MCExpr &getAddrDelta() const { return *AddrDelta; }
523
524   SmallString<8> &getContents() { return Contents; }
525   const SmallString<8> &getContents() const { return Contents; }
526
527   /// @}
528
529   static bool classof(const MCFragment *F) {
530     return F->getKind() == MCFragment::FT_DwarfFrame;
531   }
532 };
533
534 // FIXME: This really doesn't belong here. See comments below.
535 struct IndirectSymbolData {
536   MCSymbol *Symbol;
537   MCSection *Section;
538 };
539
540 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
541 // to one another.
542 struct DataRegionData {
543   // This enum should be kept in sync w/ the mach-o definition in
544   // llvm/Object/MachOFormat.h.
545   enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
546   MCSymbol *Start;
547   MCSymbol *End;
548 };
549
550 class MCAssembler {
551   friend class MCAsmLayout;
552
553 public:
554   typedef SetVector<MCSection *> SectionListType;
555   typedef std::vector<const MCSymbol *> SymbolDataListType;
556
557   typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
558   typedef pointee_iterator<SectionListType::iterator> iterator;
559
560   typedef pointee_iterator<SymbolDataListType::const_iterator>
561   const_symbol_iterator;
562   typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
563
564   typedef iterator_range<symbol_iterator> symbol_range;
565   typedef iterator_range<const_symbol_iterator> const_symbol_range;
566
567   typedef std::vector<IndirectSymbolData>::const_iterator
568       const_indirect_symbol_iterator;
569   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
570
571   typedef std::vector<DataRegionData>::const_iterator
572       const_data_region_iterator;
573   typedef std::vector<DataRegionData>::iterator data_region_iterator;
574
575   /// MachO specific deployment target version info.
576   // A Major version of 0 indicates that no version information was supplied
577   // and so the corresponding load command should not be emitted.
578   typedef struct {
579     MCVersionMinType Kind;
580     unsigned Major;
581     unsigned Minor;
582     unsigned Update;
583   } VersionMinInfoType;
584
585 private:
586   MCAssembler(const MCAssembler &) = delete;
587   void operator=(const MCAssembler &) = delete;
588
589   MCContext &Context;
590
591   MCAsmBackend &Backend;
592
593   MCCodeEmitter &Emitter;
594
595   MCObjectWriter &Writer;
596
597   raw_ostream &OS;
598
599   SectionListType Sections;
600
601   SymbolDataListType Symbols;
602
603   DenseSet<const MCSymbol *> LocalsUsedInReloc;
604
605   std::vector<IndirectSymbolData> IndirectSymbols;
606
607   std::vector<DataRegionData> DataRegions;
608
609   /// The list of linker options to propagate into the object file.
610   std::vector<std::vector<std::string>> LinkerOptions;
611
612   /// List of declared file names
613   std::vector<std::string> FileNames;
614
615   /// The set of function symbols for which a .thumb_func directive has
616   /// been seen.
617   //
618   // FIXME: We really would like this in target specific code rather than
619   // here. Maybe when the relocation stuff moves to target specific,
620   // this can go with it? The streamer would need some target specific
621   // refactoring too.
622   mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
623
624   /// \brief The bundle alignment size currently set in the assembler.
625   ///
626   /// By default it's 0, which means bundling is disabled.
627   unsigned BundleAlignSize;
628
629   unsigned RelaxAll : 1;
630   unsigned SubsectionsViaSymbols : 1;
631
632   /// ELF specific e_header flags
633   // It would be good if there were an MCELFAssembler class to hold this.
634   // ELF header flags are used both by the integrated and standalone assemblers.
635   // Access to the flags is necessary in cases where assembler directives affect
636   // which flags to be set.
637   unsigned ELFHeaderEFlags;
638
639   /// Used to communicate Linker Optimization Hint information between
640   /// the Streamer and the .o writer
641   MCLOHContainer LOHContainer;
642
643   VersionMinInfoType VersionMinInfo;
644
645 private:
646   /// Evaluate a fixup to a relocatable expression and the value which should be
647   /// placed into the fixup.
648   ///
649   /// \param Layout The layout to use for evaluation.
650   /// \param Fixup The fixup to evaluate.
651   /// \param DF The fragment the fixup is inside.
652   /// \param Target [out] On return, the relocatable expression the fixup
653   /// evaluates to.
654   /// \param Value [out] On return, the value of the fixup as currently laid
655   /// out.
656   /// \return Whether the fixup value was fully resolved. This is true if the
657   /// \p Value result is fixed, otherwise the value may change due to
658   /// relocation.
659   bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
660                      const MCFragment *DF, MCValue &Target,
661                      uint64_t &Value) const;
662
663   /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
664   /// (increased in size, in order to hold its value correctly).
665   bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
666                             const MCAsmLayout &Layout) const;
667
668   /// Check whether the given fragment needs relaxation.
669   bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
670                                const MCAsmLayout &Layout) const;
671
672   /// \brief Perform one layout iteration and return true if any offsets
673   /// were adjusted.
674   bool layoutOnce(MCAsmLayout &Layout);
675
676   /// \brief Perform one layout iteration of the given section and return true
677   /// if any offsets were adjusted.
678   bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
679
680   bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
681
682   bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
683
684   bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
685   bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
686                                    MCDwarfCallFrameFragment &DF);
687
688   /// finishLayout - Finalize a layout, including fragment lowering.
689   void finishLayout(MCAsmLayout &Layout);
690
691   std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
692                                         MCFragment &F, const MCFixup &Fixup);
693
694 public:
695   void addLocalUsedInReloc(const MCSymbol &Sym);
696   bool isLocalUsedInReloc(const MCSymbol &Sym) const;
697
698   /// Compute the effective fragment size assuming it is laid out at the given
699   /// \p SectionAddress and \p FragmentOffset.
700   uint64_t computeFragmentSize(const MCAsmLayout &Layout,
701                                const MCFragment &F) const;
702
703   /// Find the symbol which defines the atom containing the given symbol, or
704   /// null if there is no such symbol.
705   const MCSymbol *getAtom(const MCSymbol &S) const;
706
707   /// Check whether a particular symbol is visible to the linker and is required
708   /// in the symbol table, or whether it can be discarded by the assembler. This
709   /// also effects whether the assembler treats the label as potentially
710   /// defining a separate atom.
711   bool isSymbolLinkerVisible(const MCSymbol &SD) const;
712
713   /// Emit the section contents using the given object writer.
714   void writeSectionData(const MCSection *Section,
715                         const MCAsmLayout &Layout) const;
716
717   /// Check whether a given symbol has been flagged with .thumb_func.
718   bool isThumbFunc(const MCSymbol *Func) const;
719
720   /// Flag a function symbol as the target of a .thumb_func directive.
721   void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
722
723   /// ELF e_header flags
724   unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
725   void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
726
727   /// MachO deployment target version information.
728   const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
729   void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
730                          unsigned Update) {
731     VersionMinInfo.Kind = Kind;
732     VersionMinInfo.Major = Major;
733     VersionMinInfo.Minor = Minor;
734     VersionMinInfo.Update = Update;
735   }
736
737 public:
738   /// Construct a new assembler instance.
739   ///
740   /// \param OS The stream to output to.
741   //
742   // FIXME: How are we going to parameterize this? Two obvious options are stay
743   // concrete and require clients to pass in a target like object. The other
744   // option is to make this abstract, and have targets provide concrete
745   // implementations as we do with AsmParser.
746   MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
747               MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
748               raw_ostream &OS);
749   ~MCAssembler();
750
751   /// Reuse an assembler instance
752   ///
753   void reset();
754
755   MCContext &getContext() const { return Context; }
756
757   MCAsmBackend &getBackend() const { return Backend; }
758
759   MCCodeEmitter &getEmitter() const { return Emitter; }
760
761   MCObjectWriter &getWriter() const { return Writer; }
762
763   /// Finish - Do final processing and write the object to the output stream.
764   /// \p Writer is used for custom object writer (as the MCJIT does),
765   /// if not specified it is automatically created from backend.
766   void Finish();
767
768   // FIXME: This does not belong here.
769   bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
770   void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
771
772   bool getRelaxAll() const { return RelaxAll; }
773   void setRelaxAll(bool Value) { RelaxAll = Value; }
774
775   bool isBundlingEnabled() const { return BundleAlignSize != 0; }
776
777   unsigned getBundleAlignSize() const { return BundleAlignSize; }
778
779   void setBundleAlignSize(unsigned Size) {
780     assert((Size == 0 || !(Size & (Size - 1))) &&
781            "Expect a power-of-two bundle align size");
782     BundleAlignSize = Size;
783   }
784
785   /// \name Section List Access
786   /// @{
787
788   iterator begin() { return Sections.begin(); }
789   const_iterator begin() const { return Sections.begin(); }
790
791   iterator end() { return Sections.end(); }
792   const_iterator end() const { return Sections.end(); }
793
794   size_t size() const { return Sections.size(); }
795
796   /// @}
797   /// \name Symbol List Access
798   /// @{
799   symbol_iterator symbol_begin() { return Symbols.begin(); }
800   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
801
802   symbol_iterator symbol_end() { return Symbols.end(); }
803   const_symbol_iterator symbol_end() const { return Symbols.end(); }
804
805   symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
806   const_symbol_range symbols() const {
807     return make_range(symbol_begin(), symbol_end());
808   }
809
810   size_t symbol_size() const { return Symbols.size(); }
811
812   /// @}
813   /// \name Indirect Symbol List Access
814   /// @{
815
816   // FIXME: This is a total hack, this should not be here. Once things are
817   // factored so that the streamer has direct access to the .o writer, it can
818   // disappear.
819   std::vector<IndirectSymbolData> &getIndirectSymbols() {
820     return IndirectSymbols;
821   }
822
823   indirect_symbol_iterator indirect_symbol_begin() {
824     return IndirectSymbols.begin();
825   }
826   const_indirect_symbol_iterator indirect_symbol_begin() const {
827     return IndirectSymbols.begin();
828   }
829
830   indirect_symbol_iterator indirect_symbol_end() {
831     return IndirectSymbols.end();
832   }
833   const_indirect_symbol_iterator indirect_symbol_end() const {
834     return IndirectSymbols.end();
835   }
836
837   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
838
839   /// @}
840   /// \name Linker Option List Access
841   /// @{
842
843   std::vector<std::vector<std::string>> &getLinkerOptions() {
844     return LinkerOptions;
845   }
846
847   /// @}
848   /// \name Data Region List Access
849   /// @{
850
851   // FIXME: This is a total hack, this should not be here. Once things are
852   // factored so that the streamer has direct access to the .o writer, it can
853   // disappear.
854   std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
855
856   data_region_iterator data_region_begin() { return DataRegions.begin(); }
857   const_data_region_iterator data_region_begin() const {
858     return DataRegions.begin();
859   }
860
861   data_region_iterator data_region_end() { return DataRegions.end(); }
862   const_data_region_iterator data_region_end() const {
863     return DataRegions.end();
864   }
865
866   size_t data_region_size() const { return DataRegions.size(); }
867
868   /// @}
869   /// \name Data Region List Access
870   /// @{
871
872   // FIXME: This is a total hack, this should not be here. Once things are
873   // factored so that the streamer has direct access to the .o writer, it can
874   // disappear.
875   MCLOHContainer &getLOHContainer() { return LOHContainer; }
876   const MCLOHContainer &getLOHContainer() const {
877     return const_cast<MCAssembler *>(this)->getLOHContainer();
878   }
879   /// @}
880   /// \name Backend Data Access
881   /// @{
882
883   bool registerSection(MCSection &Section) { return Sections.insert(&Section); }
884
885   bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }
886
887   MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
888     return const_cast<MCSymbolData &>(
889         static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
890   }
891
892   const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
893     return Symbol.getData();
894   }
895
896   MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
897                                       bool *Created = nullptr) {
898     if (Created)
899       *Created = !hasSymbolData(Symbol);
900     if (!hasSymbolData(Symbol)) {
901       Symbol.initializeData();
902       Symbols.push_back(&Symbol);
903     }
904     return Symbol.getData();
905   }
906
907   ArrayRef<std::string> getFileNames() { return FileNames; }
908
909   void addFileName(StringRef FileName) {
910     if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
911         FileNames.end())
912       FileNames.push_back(FileName);
913   }
914
915   /// \brief Write the necessary bundle padding to the given object writer.
916   /// Expects a fragment \p F containing instructions and its size \p FSize.
917   void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
918                             MCObjectWriter *OW) const;
919
920   /// @}
921
922   void dump();
923 };
924
925 /// \brief Compute the amount of padding required before the fragment \p F to
926 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
927 /// its section and \p FSize is the fragment's size.
928 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
929                               uint64_t FOffset, uint64_t FSize);
930
931 } // end namespace llvm
932
933 #endif