Move getSubsectionInsertionPoint to MCSection.
[oota-llvm.git] / include / llvm / MC / MCSection.h
1 //===- MCSection.h - Machine Code Sections ----------------------*- 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 // This file declares the MCSection class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_MC_MCSECTION_H
15 #define LLVM_MC_MCSECTION_H
16
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/ilist.h"
20 #include "llvm/ADT/ilist_node.h"
21 #include "llvm/MC/SectionKind.h"
22 #include "llvm/Support/Compiler.h"
23
24 namespace llvm {
25 class MCAssembler;
26 class MCAsmInfo;
27 class MCContext;
28 class MCExpr;
29 class MCFragment;
30 class MCSection;
31 class MCSymbol;
32 class raw_ostream;
33
34 class MCSectionData {
35   friend class MCAsmLayout;
36   friend class MCSection;
37
38   MCSectionData(const MCSectionData &) = delete;
39   void operator=(const MCSectionData &) = delete;
40
41 public:
42   typedef iplist<MCFragment> FragmentListType;
43
44   typedef FragmentListType::const_iterator const_iterator;
45   typedef FragmentListType::iterator iterator;
46
47   typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
48   typedef FragmentListType::reverse_iterator reverse_iterator;
49
50 private:
51   FragmentListType Fragments;
52   MCSection *Section;
53
54   /// \name Assembler Backend Data
55   /// @{
56   //
57   // FIXME: This could all be kept private to the assembler implementation.
58
59   /// Mapping from subsection number to insertion point for subsection numbers
60   /// below that number.
61   SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
62
63   /// @}
64
65 public:
66   explicit MCSectionData(MCSection &Section);
67
68   MCSection &getSection() const { return *Section; }
69
70   /// \name Fragment Access
71   /// @{
72
73   const FragmentListType &getFragmentList() const { return Fragments; }
74   FragmentListType &getFragmentList() { return Fragments; }
75
76   iterator begin();
77   const_iterator begin() const {
78     return const_cast<MCSectionData *>(this)->begin();
79   }
80
81   iterator end();
82   const_iterator end() const {
83     return const_cast<MCSectionData *>(this)->end();
84   }
85
86   reverse_iterator rbegin();
87   const_reverse_iterator rbegin() const {
88     return const_cast<MCSectionData *>(this)->rbegin();
89   }
90
91   reverse_iterator rend();
92   const_reverse_iterator rend() const {
93     return const_cast<MCSectionData *>(this)->rend();
94   }
95
96   size_t size() const;
97
98   bool empty() const;
99
100   void dump();
101
102   /// @}
103 };
104
105 /// Instances of this class represent a uniqued identifier for a section in the
106 /// current translation unit.  The MCContext class uniques and creates these.
107 class MCSection {
108 public:
109   enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO };
110
111   /// \brief Express the state of bundle locked groups while emitting code.
112   enum BundleLockStateType {
113     NotBundleLocked,
114     BundleLocked,
115     BundleLockedAlignToEnd
116   };
117
118 private:
119   MCSection(const MCSection &) = delete;
120   void operator=(const MCSection &) = delete;
121
122   MCSymbol *Begin;
123   MCSymbol *End = nullptr;
124   /// The alignment requirement of this section.
125   unsigned Alignment = 1;
126   /// The section index in the assemblers section list.
127   unsigned Ordinal = 0;
128   /// The index of this section in the layout order.
129   unsigned LayoutOrder;
130
131   /// \brief Keeping track of bundle-locked state.
132   BundleLockStateType BundleLockState = NotBundleLocked;
133
134   /// \brief Current nesting depth of bundle_lock directives.
135   unsigned BundleLockNestingDepth = 0;
136
137   /// \brief We've seen a bundle_lock directive but not its first instruction
138   /// yet.
139   bool BundleGroupBeforeFirstInst = false;
140
141   /// Whether this section has had instructions emitted into it.
142   unsigned HasInstructions : 1;
143
144   MCSectionData Data;
145
146 protected:
147   MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
148   SectionVariant Variant;
149   SectionKind Kind;
150
151 public:
152   virtual ~MCSection();
153
154   SectionKind getKind() const { return Kind; }
155
156   SectionVariant getVariant() const { return Variant; }
157
158   MCSymbol *getBeginSymbol() { return Begin; }
159   const MCSymbol *getBeginSymbol() const {
160     return const_cast<MCSection *>(this)->getBeginSymbol();
161   }
162   void setBeginSymbol(MCSymbol *Sym) {
163     assert(!Begin);
164     Begin = Sym;
165   }
166   MCSymbol *getEndSymbol(MCContext &Ctx);
167   bool hasEnded() const;
168
169   unsigned getAlignment() const { return Alignment; }
170   void setAlignment(unsigned Value) { Alignment = Value; }
171
172   unsigned getOrdinal() const { return Ordinal; }
173   void setOrdinal(unsigned Value) { Ordinal = Value; }
174
175   unsigned getLayoutOrder() const { return LayoutOrder; }
176   void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
177
178   BundleLockStateType getBundleLockState() const { return BundleLockState; }
179   void setBundleLockState(BundleLockStateType NewState);
180   bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
181
182   bool isBundleGroupBeforeFirstInst() const {
183     return BundleGroupBeforeFirstInst;
184   }
185   void setBundleGroupBeforeFirstInst(bool IsFirst) {
186     BundleGroupBeforeFirstInst = IsFirst;
187   }
188
189   bool hasInstructions() const { return HasInstructions; }
190   void setHasInstructions(bool Value) { HasInstructions = Value; }
191
192   MCSectionData &getSectionData() { return Data; }
193   const MCSectionData &getSectionData() const {
194     return const_cast<MCSection *>(this)->getSectionData();
195   }
196
197   MCSectionData::FragmentListType &getFragmentList();
198   const MCSectionData::FragmentListType &getFragmentList() const {
199     return const_cast<MCSection *>(this)->getFragmentList();
200   }
201
202   MCSectionData::iterator begin();
203   MCSectionData::const_iterator begin() const {
204     return const_cast<MCSection *>(this)->begin();
205   }
206
207   MCSectionData::iterator end();
208   MCSectionData::const_iterator end() const {
209     return const_cast<MCSection *>(this)->end();
210   }
211
212   MCSectionData::reverse_iterator rbegin();
213   MCSectionData::const_reverse_iterator rbegin() const {
214     return const_cast<MCSection *>(this)->rbegin();
215   }
216
217   MCSectionData::reverse_iterator rend();
218   MCSectionData::const_reverse_iterator rend() const {
219     return const_cast<MCSection *>(this)->rend();
220   }
221
222   MCSectionData::iterator getSubsectionInsertionPoint(unsigned Subsection);
223
224   virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
225                                     const MCExpr *Subsection) const = 0;
226
227   /// Return true if a .align directive should use "optimized nops" to fill
228   /// instead of 0s.
229   virtual bool UseCodeAlign() const = 0;
230
231   /// Check whether this section is "virtual", that is has no actual object
232   /// file contents.
233   virtual bool isVirtualSection() const = 0;
234 };
235
236 } // end namespace llvm
237
238 #endif