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