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