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