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