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