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();
101 /// Instances of this class represent a uniqued identifier for a section in the
102 /// current translation unit. The MCContext class uniques and creates these.
105 enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO };
107 /// \brief Express the state of bundle locked groups while emitting code.
108 enum BundleLockStateType {
111 BundleLockedAlignToEnd
115 MCSection(const MCSection &) = delete;
116 void operator=(const MCSection &) = delete;
119 MCSymbol *End = nullptr;
120 /// The alignment requirement of this section.
121 unsigned Alignment = 1;
122 /// The section index in the assemblers section list.
123 unsigned Ordinal = 0;
124 /// The index of this section in the layout order.
125 unsigned LayoutOrder;
127 /// \brief Keeping track of bundle-locked state.
128 BundleLockStateType BundleLockState = NotBundleLocked;
130 /// \brief Current nesting depth of bundle_lock directives.
131 unsigned BundleLockNestingDepth = 0;
133 /// \brief We've seen a bundle_lock directive but not its first instruction
135 bool BundleGroupBeforeFirstInst = false;
137 /// Whether this section has had instructions emitted into it.
138 unsigned HasInstructions : 1;
143 MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
144 SectionVariant Variant;
148 virtual ~MCSection();
150 SectionKind getKind() const { return Kind; }
152 SectionVariant getVariant() const { return Variant; }
154 MCSymbol *getBeginSymbol() { return Begin; }
155 const MCSymbol *getBeginSymbol() const {
156 return const_cast<MCSection *>(this)->getBeginSymbol();
158 void setBeginSymbol(MCSymbol *Sym) {
162 MCSymbol *getEndSymbol(MCContext &Ctx);
163 bool hasEnded() const;
165 unsigned getAlignment() const { return Alignment; }
166 void setAlignment(unsigned Value) { Alignment = Value; }
168 unsigned getOrdinal() const { return Ordinal; }
169 void setOrdinal(unsigned Value) { Ordinal = Value; }
171 unsigned getLayoutOrder() const { return LayoutOrder; }
172 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
174 BundleLockStateType getBundleLockState() const { return BundleLockState; }
175 void setBundleLockState(BundleLockStateType NewState);
176 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
178 bool isBundleGroupBeforeFirstInst() const {
179 return BundleGroupBeforeFirstInst;
181 void setBundleGroupBeforeFirstInst(bool IsFirst) {
182 BundleGroupBeforeFirstInst = IsFirst;
185 bool hasInstructions() const { return HasInstructions; }
186 void setHasInstructions(bool Value) { HasInstructions = Value; }
188 MCSectionData &getSectionData() { return Data; }
189 const MCSectionData &getSectionData() const {
190 return const_cast<MCSection *>(this)->getSectionData();
193 MCSectionData::FragmentListType &getFragmentList();
194 const MCSectionData::FragmentListType &getFragmentList() const {
195 return const_cast<MCSection *>(this)->getFragmentList();
198 MCSectionData::iterator begin();
199 MCSectionData::const_iterator begin() const {
200 return const_cast<MCSection *>(this)->begin();
203 MCSectionData::iterator end();
204 MCSectionData::const_iterator end() const {
205 return const_cast<MCSection *>(this)->end();
208 MCSectionData::reverse_iterator rbegin();
209 MCSectionData::const_reverse_iterator rbegin() const {
210 return const_cast<MCSection *>(this)->rbegin();
213 MCSectionData::reverse_iterator rend();
214 MCSectionData::const_reverse_iterator rend() const {
215 return const_cast<MCSection *>(this)->rend();
218 MCSectionData::iterator getSubsectionInsertionPoint(unsigned Subsection);
220 virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
221 const MCExpr *Subsection) const = 0;
223 /// Return true if a .align directive should use "optimized nops" to fill
225 virtual bool UseCodeAlign() const = 0;
227 /// Check whether this section is "virtual", that is has no actual object
229 virtual bool isVirtualSection() const = 0;
232 } // end namespace llvm