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