1 //===- MCSection.h - Machine Code Sections ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MCSection class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSECTION_H
15 #define LLVM_MC_MCSECTION_H
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"
35 friend class MCAsmLayout;
36 friend class MCSection;
38 MCSectionData(const MCSectionData &) = delete;
39 void operator=(const MCSectionData &) = delete;
42 typedef iplist<MCFragment> FragmentListType;
44 typedef FragmentListType::const_iterator const_iterator;
45 typedef FragmentListType::iterator iterator;
47 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
48 typedef FragmentListType::reverse_iterator reverse_iterator;
51 FragmentListType Fragments;
54 /// \name Assembler Backend Data
57 // FIXME: This could all be kept private to the assembler implementation.
59 /// Mapping from subsection number to insertion point for subsection numbers
60 /// below that number.
61 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
66 explicit MCSectionData(MCSection &Section);
68 MCSection &getSection() const { return *Section; }
70 /// \name Fragment Access
73 const FragmentListType &getFragmentList() const { return Fragments; }
74 FragmentListType &getFragmentList() { return Fragments; }
77 const_iterator begin() const {
78 return const_cast<MCSectionData *>(this)->begin();
82 const_iterator end() const {
83 return const_cast<MCSectionData *>(this)->end();
86 reverse_iterator rbegin();
87 const_reverse_iterator rbegin() const {
88 return const_cast<MCSectionData *>(this)->rbegin();
91 reverse_iterator rend();
92 const_reverse_iterator rend() const {
93 return const_cast<MCSectionData *>(this)->rend();
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.
109 enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO };
111 /// \brief Express the state of bundle locked groups while emitting code.
112 enum BundleLockStateType {
115 BundleLockedAlignToEnd
119 MCSection(const MCSection &) = delete;
120 void operator=(const MCSection &) = delete;
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;
131 /// \brief Keeping track of bundle-locked state.
132 BundleLockStateType BundleLockState = NotBundleLocked;
134 /// \brief Current nesting depth of bundle_lock directives.
135 unsigned BundleLockNestingDepth = 0;
137 /// \brief We've seen a bundle_lock directive but not its first instruction
139 bool BundleGroupBeforeFirstInst = false;
141 /// Whether this section has had instructions emitted into it.
142 unsigned HasInstructions : 1;
147 MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
148 SectionVariant Variant;
152 virtual ~MCSection();
154 SectionKind getKind() const { return Kind; }
156 SectionVariant getVariant() const { return Variant; }
158 MCSymbol *getBeginSymbol() { return Begin; }
159 const MCSymbol *getBeginSymbol() const {
160 return const_cast<MCSection *>(this)->getBeginSymbol();
162 void setBeginSymbol(MCSymbol *Sym) {
166 MCSymbol *getEndSymbol(MCContext &Ctx);
167 bool hasEnded() const;
169 unsigned getAlignment() const { return Alignment; }
170 void setAlignment(unsigned Value) { Alignment = Value; }
172 unsigned getOrdinal() const { return Ordinal; }
173 void setOrdinal(unsigned Value) { Ordinal = Value; }
175 unsigned getLayoutOrder() const { return LayoutOrder; }
176 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
178 BundleLockStateType getBundleLockState() const { return BundleLockState; }
179 void setBundleLockState(BundleLockStateType NewState);
180 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
182 bool isBundleGroupBeforeFirstInst() const {
183 return BundleGroupBeforeFirstInst;
185 void setBundleGroupBeforeFirstInst(bool IsFirst) {
186 BundleGroupBeforeFirstInst = IsFirst;
189 bool hasInstructions() const { return HasInstructions; }
190 void setHasInstructions(bool Value) { HasInstructions = Value; }
192 MCSectionData &getSectionData() { return Data; }
193 const MCSectionData &getSectionData() const {
194 return const_cast<MCSection *>(this)->getSectionData();
197 MCSectionData::FragmentListType &getFragmentList();
198 const MCSectionData::FragmentListType &getFragmentList() const {
199 return const_cast<MCSection *>(this)->getFragmentList();
202 MCSectionData::iterator begin();
203 MCSectionData::const_iterator begin() const {
204 return const_cast<MCSection *>(this)->begin();
207 MCSectionData::iterator end();
208 MCSectionData::const_iterator end() const {
209 return const_cast<MCSection *>(this)->end();
212 MCSectionData::reverse_iterator rbegin();
213 MCSectionData::const_reverse_iterator rbegin() const {
214 return const_cast<MCSection *>(this)->rbegin();
217 MCSectionData::reverse_iterator rend();
218 MCSectionData::const_reverse_iterator rend() const {
219 return const_cast<MCSection *>(this)->rend();
222 MCSectionData::iterator getSubsectionInsertionPoint(unsigned Subsection);
224 virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
225 const MCExpr *Subsection) const = 0;
227 /// Return true if a .align directive should use "optimized nops" to fill
229 virtual bool UseCodeAlign() const = 0;
231 /// Check whether this section is "virtual", that is has no actual object
233 virtual bool isVirtualSection() const = 0;
236 } // end namespace llvm