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