Rename HasData to IsRegistered.
[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/MCFixup.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/MC/MCSymbol.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 {
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   /// 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 class MCSafeSEHFragment : public MCFragment {
535   virtual void anchor();
536
537   const MCSymbol *Sym;
538
539 public:
540   MCSafeSEHFragment(const MCSymbol *Sym, MCSection *Sec = nullptr)
541       : MCFragment(FT_SafeSEH, Sec), Sym(Sym) {}
542
543   /// \name Accessors
544   /// @{
545
546   const MCSymbol *getSymbol() { return Sym; }
547   const MCSymbol *getSymbol() const { return Sym; }
548
549   /// @}
550
551   static bool classof(const MCFragment *F) {
552     return F->getKind() == MCFragment::FT_SafeSEH;
553   }
554 };
555
556 // FIXME: This really doesn't belong here. See comments below.
557 struct IndirectSymbolData {
558   MCSymbol *Symbol;
559   MCSection *Section;
560 };
561
562 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
563 // to one another.
564 struct DataRegionData {
565   // This enum should be kept in sync w/ the mach-o definition in
566   // llvm/Object/MachOFormat.h.
567   enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
568   MCSymbol *Start;
569   MCSymbol *End;
570 };
571
572 class MCAssembler {
573   friend class MCAsmLayout;
574
575 public:
576   typedef std::vector<MCSection *> SectionListType;
577   typedef std::vector<const MCSymbol *> SymbolDataListType;
578
579   typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
580   typedef pointee_iterator<SectionListType::iterator> iterator;
581
582   typedef pointee_iterator<SymbolDataListType::const_iterator>
583   const_symbol_iterator;
584   typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
585
586   typedef iterator_range<symbol_iterator> symbol_range;
587   typedef iterator_range<const_symbol_iterator> const_symbol_range;
588
589   typedef std::vector<IndirectSymbolData>::const_iterator
590       const_indirect_symbol_iterator;
591   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
592
593   typedef std::vector<DataRegionData>::const_iterator
594       const_data_region_iterator;
595   typedef std::vector<DataRegionData>::iterator data_region_iterator;
596
597   /// MachO specific deployment target version info.
598   // A Major version of 0 indicates that no version information was supplied
599   // and so the corresponding load command should not be emitted.
600   typedef struct {
601     MCVersionMinType Kind;
602     unsigned Major;
603     unsigned Minor;
604     unsigned Update;
605   } VersionMinInfoType;
606
607 private:
608   MCAssembler(const MCAssembler &) = delete;
609   void operator=(const MCAssembler &) = delete;
610
611   MCContext &Context;
612
613   MCAsmBackend &Backend;
614
615   MCCodeEmitter &Emitter;
616
617   MCObjectWriter &Writer;
618
619   raw_ostream &OS;
620
621   SectionListType Sections;
622
623   SymbolDataListType Symbols;
624
625   DenseSet<const MCSymbol *> LocalsUsedInReloc;
626
627   std::vector<IndirectSymbolData> IndirectSymbols;
628
629   std::vector<DataRegionData> DataRegions;
630
631   /// The list of linker options to propagate into the object file.
632   std::vector<std::vector<std::string>> LinkerOptions;
633
634   /// List of declared file names
635   std::vector<std::string> FileNames;
636
637   /// The set of function symbols for which a .thumb_func directive has
638   /// been seen.
639   //
640   // FIXME: We really would like this in target specific code rather than
641   // here. Maybe when the relocation stuff moves to target specific,
642   // this can go with it? The streamer would need some target specific
643   // refactoring too.
644   mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
645
646   /// \brief The bundle alignment size currently set in the assembler.
647   ///
648   /// By default it's 0, which means bundling is disabled.
649   unsigned BundleAlignSize;
650
651   unsigned RelaxAll : 1;
652   unsigned SubsectionsViaSymbols : 1;
653
654   /// ELF specific e_header flags
655   // It would be good if there were an MCELFAssembler class to hold this.
656   // ELF header flags are used both by the integrated and standalone assemblers.
657   // Access to the flags is necessary in cases where assembler directives affect
658   // which flags to be set.
659   unsigned ELFHeaderEFlags;
660
661   /// Used to communicate Linker Optimization Hint information between
662   /// the Streamer and the .o writer
663   MCLOHContainer LOHContainer;
664
665   VersionMinInfoType VersionMinInfo;
666
667 private:
668   /// Evaluate a fixup to a relocatable expression and the value which should be
669   /// placed into the fixup.
670   ///
671   /// \param Layout The layout to use for evaluation.
672   /// \param Fixup The fixup to evaluate.
673   /// \param DF The fragment the fixup is inside.
674   /// \param Target [out] On return, the relocatable expression the fixup
675   /// evaluates to.
676   /// \param Value [out] On return, the value of the fixup as currently laid
677   /// out.
678   /// \return Whether the fixup value was fully resolved. This is true if the
679   /// \p Value result is fixed, otherwise the value may change due to
680   /// relocation.
681   bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
682                      const MCFragment *DF, MCValue &Target,
683                      uint64_t &Value) const;
684
685   /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
686   /// (increased in size, in order to hold its value correctly).
687   bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
688                             const MCAsmLayout &Layout) const;
689
690   /// Check whether the given fragment needs relaxation.
691   bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
692                                const MCAsmLayout &Layout) const;
693
694   /// \brief Perform one layout iteration and return true if any offsets
695   /// were adjusted.
696   bool layoutOnce(MCAsmLayout &Layout);
697
698   /// \brief Perform one layout iteration of the given section and return true
699   /// if any offsets were adjusted.
700   bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
701
702   bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
703
704   bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
705
706   bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
707   bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
708                                    MCDwarfCallFrameFragment &DF);
709
710   /// finishLayout - Finalize a layout, including fragment lowering.
711   void finishLayout(MCAsmLayout &Layout);
712
713   std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
714                                         MCFragment &F, const MCFixup &Fixup);
715
716 public:
717   void addLocalUsedInReloc(const MCSymbol &Sym);
718   bool isLocalUsedInReloc(const MCSymbol &Sym) const;
719
720   /// Compute the effective fragment size assuming it is laid out at the given
721   /// \p SectionAddress and \p FragmentOffset.
722   uint64_t computeFragmentSize(const MCAsmLayout &Layout,
723                                const MCFragment &F) const;
724
725   /// Find the symbol which defines the atom containing the given symbol, or
726   /// null if there is no such symbol.
727   const MCSymbol *getAtom(const MCSymbol &S) const;
728
729   /// Check whether a particular symbol is visible to the linker and is required
730   /// in the symbol table, or whether it can be discarded by the assembler. This
731   /// also effects whether the assembler treats the label as potentially
732   /// defining a separate atom.
733   bool isSymbolLinkerVisible(const MCSymbol &SD) const;
734
735   /// Emit the section contents using the given object writer.
736   void writeSectionData(const MCSection *Section,
737                         const MCAsmLayout &Layout) const;
738
739   /// Check whether a given symbol has been flagged with .thumb_func.
740   bool isThumbFunc(const MCSymbol *Func) const;
741
742   /// Flag a function symbol as the target of a .thumb_func directive.
743   void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
744
745   /// ELF e_header flags
746   unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
747   void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
748
749   /// MachO deployment target version information.
750   const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
751   void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
752                          unsigned Update) {
753     VersionMinInfo.Kind = Kind;
754     VersionMinInfo.Major = Major;
755     VersionMinInfo.Minor = Minor;
756     VersionMinInfo.Update = Update;
757   }
758
759 public:
760   /// Construct a new assembler instance.
761   ///
762   /// \param OS The stream to output to.
763   //
764   // FIXME: How are we going to parameterize this? Two obvious options are stay
765   // concrete and require clients to pass in a target like object. The other
766   // option is to make this abstract, and have targets provide concrete
767   // implementations as we do with AsmParser.
768   MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
769               MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
770               raw_ostream &OS);
771   ~MCAssembler();
772
773   /// Reuse an assembler instance
774   ///
775   void reset();
776
777   MCContext &getContext() const { return Context; }
778
779   MCAsmBackend &getBackend() const { return Backend; }
780
781   MCCodeEmitter &getEmitter() const { return Emitter; }
782
783   MCObjectWriter &getWriter() const { return Writer; }
784
785   /// Finish - Do final processing and write the object to the output stream.
786   /// \p Writer is used for custom object writer (as the MCJIT does),
787   /// if not specified it is automatically created from backend.
788   void Finish();
789
790   // FIXME: This does not belong here.
791   bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
792   void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
793
794   bool getRelaxAll() const { return RelaxAll; }
795   void setRelaxAll(bool Value) { RelaxAll = Value; }
796
797   bool isBundlingEnabled() const { return BundleAlignSize != 0; }
798
799   unsigned getBundleAlignSize() const { return BundleAlignSize; }
800
801   void setBundleAlignSize(unsigned Size) {
802     assert((Size == 0 || !(Size & (Size - 1))) &&
803            "Expect a power-of-two bundle align size");
804     BundleAlignSize = Size;
805   }
806
807   /// \name Section List Access
808   /// @{
809
810   iterator begin() { return Sections.begin(); }
811   const_iterator begin() const { return Sections.begin(); }
812
813   iterator end() { return Sections.end(); }
814   const_iterator end() const { return Sections.end(); }
815
816   size_t size() const { return Sections.size(); }
817
818   /// @}
819   /// \name Symbol List Access
820   /// @{
821   symbol_iterator symbol_begin() { return Symbols.begin(); }
822   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
823
824   symbol_iterator symbol_end() { return Symbols.end(); }
825   const_symbol_iterator symbol_end() const { return Symbols.end(); }
826
827   symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
828   const_symbol_range symbols() const {
829     return make_range(symbol_begin(), symbol_end());
830   }
831
832   size_t symbol_size() const { return Symbols.size(); }
833
834   /// @}
835   /// \name Indirect Symbol List Access
836   /// @{
837
838   // FIXME: This is a total hack, this should not be here. Once things are
839   // factored so that the streamer has direct access to the .o writer, it can
840   // disappear.
841   std::vector<IndirectSymbolData> &getIndirectSymbols() {
842     return IndirectSymbols;
843   }
844
845   indirect_symbol_iterator indirect_symbol_begin() {
846     return IndirectSymbols.begin();
847   }
848   const_indirect_symbol_iterator indirect_symbol_begin() const {
849     return IndirectSymbols.begin();
850   }
851
852   indirect_symbol_iterator indirect_symbol_end() {
853     return IndirectSymbols.end();
854   }
855   const_indirect_symbol_iterator indirect_symbol_end() const {
856     return IndirectSymbols.end();
857   }
858
859   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
860
861   /// @}
862   /// \name Linker Option List Access
863   /// @{
864
865   std::vector<std::vector<std::string>> &getLinkerOptions() {
866     return LinkerOptions;
867   }
868
869   /// @}
870   /// \name Data Region List Access
871   /// @{
872
873   // FIXME: This is a total hack, this should not be here. Once things are
874   // factored so that the streamer has direct access to the .o writer, it can
875   // disappear.
876   std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
877
878   data_region_iterator data_region_begin() { return DataRegions.begin(); }
879   const_data_region_iterator data_region_begin() const {
880     return DataRegions.begin();
881   }
882
883   data_region_iterator data_region_end() { return DataRegions.end(); }
884   const_data_region_iterator data_region_end() const {
885     return DataRegions.end();
886   }
887
888   size_t data_region_size() const { return DataRegions.size(); }
889
890   /// @}
891   /// \name Data Region List Access
892   /// @{
893
894   // FIXME: This is a total hack, this should not be here. Once things are
895   // factored so that the streamer has direct access to the .o writer, it can
896   // disappear.
897   MCLOHContainer &getLOHContainer() { return LOHContainer; }
898   const MCLOHContainer &getLOHContainer() const {
899     return const_cast<MCAssembler *>(this)->getLOHContainer();
900   }
901   /// @}
902   /// \name Backend Data Access
903   /// @{
904
905   bool registerSection(MCSection &Section) {
906     if (Section.isRegistered())
907       return false;
908     Sections.push_back(&Section);
909     Section.setIsRegistered(true);
910     return true;
911   }
912
913   void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr) {
914     bool New = !Symbol.isRegistered();
915     if (Created)
916       *Created = New;
917     if (New) {
918       Symbol.setIsRegistered(true);
919       Symbols.push_back(&Symbol);
920     }
921   }
922
923   ArrayRef<std::string> getFileNames() { return FileNames; }
924
925   void addFileName(StringRef FileName) {
926     if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
927         FileNames.end())
928       FileNames.push_back(FileName);
929   }
930
931   /// \brief Write the necessary bundle padding to the given object writer.
932   /// Expects a fragment \p F containing instructions and its size \p FSize.
933   void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
934                             MCObjectWriter *OW) const;
935
936   /// @}
937
938   void dump();
939 };
940
941 /// \brief Compute the amount of padding required before the fragment \p F to
942 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
943 /// its section and \p FSize is the fragment's size.
944 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
945                               uint64_t FOffset, uint64_t FSize);
946
947 } // end namespace llvm
948
949 #endif