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/MCAssembler.h"
22 #include "llvm/MC/SectionKind.h"
23 #include "llvm/Support/Compiler.h"
36 struct ilist_node_traits<MCFragment> {
37 MCFragment *createNode(const MCFragment &V);
38 static void deleteNode(MCFragment *V);
40 void addNodeToList(MCFragment *) {}
41 void removeNodeFromList(MCFragment *) {}
42 void transferNodesFromList(ilist_node_traits & /*SrcTraits*/,
43 ilist_iterator<MCFragment> /*first*/,
44 ilist_iterator<MCFragment> /*last*/) {}
47 /// Instances of this class represent a uniqued identifier for a section in the
48 /// current translation unit. The MCContext class uniques and creates these.
51 enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO };
53 /// \brief Express the state of bundle locked groups while emitting code.
54 enum BundleLockStateType {
57 BundleLockedAlignToEnd
60 typedef iplist<MCFragment> FragmentListType;
62 typedef FragmentListType::const_iterator const_iterator;
63 typedef FragmentListType::iterator iterator;
65 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
66 typedef FragmentListType::reverse_iterator reverse_iterator;
69 MCSection(const MCSection &) = delete;
70 void operator=(const MCSection &) = delete;
73 MCSymbol *End = nullptr;
74 /// The alignment requirement of this section.
75 unsigned Alignment = 1;
76 /// The section index in the assemblers section list.
78 /// The index of this section in the layout order.
81 /// \brief Keeping track of bundle-locked state.
82 BundleLockStateType BundleLockState = NotBundleLocked;
84 /// \brief Current nesting depth of bundle_lock directives.
85 unsigned BundleLockNestingDepth = 0;
87 /// \brief We've seen a bundle_lock directive but not its first instruction
89 unsigned BundleGroupBeforeFirstInst : 1;
91 /// Whether this section has had instructions emitted into it.
92 unsigned HasInstructions : 1;
94 unsigned IsRegistered : 1;
96 MCDummyFragment DummyFragment;
98 FragmentListType Fragments;
100 /// Mapping from subsection number to insertion point for subsection numbers
101 /// below that number.
102 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
105 MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
106 SectionVariant Variant;
110 virtual ~MCSection();
112 SectionKind getKind() const { return Kind; }
114 SectionVariant getVariant() const { return Variant; }
116 MCSymbol *getBeginSymbol() { return Begin; }
117 const MCSymbol *getBeginSymbol() const {
118 return const_cast<MCSection *>(this)->getBeginSymbol();
120 void setBeginSymbol(MCSymbol *Sym) {
124 MCSymbol *getEndSymbol(MCContext &Ctx);
125 bool hasEnded() const;
127 unsigned getAlignment() const { return Alignment; }
128 void setAlignment(unsigned Value) { Alignment = Value; }
130 unsigned getOrdinal() const { return Ordinal; }
131 void setOrdinal(unsigned Value) { Ordinal = Value; }
133 unsigned getLayoutOrder() const { return LayoutOrder; }
134 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
136 BundleLockStateType getBundleLockState() const { return BundleLockState; }
137 void setBundleLockState(BundleLockStateType NewState);
138 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
140 bool isBundleGroupBeforeFirstInst() const {
141 return BundleGroupBeforeFirstInst;
143 void setBundleGroupBeforeFirstInst(bool IsFirst) {
144 BundleGroupBeforeFirstInst = IsFirst;
147 bool hasInstructions() const { return HasInstructions; }
148 void setHasInstructions(bool Value) { HasInstructions = Value; }
150 bool isRegistered() const { return IsRegistered; }
151 void setIsRegistered(bool Value) { IsRegistered = Value; }
153 MCSection::FragmentListType &getFragmentList() { return Fragments; }
154 const MCSection::FragmentListType &getFragmentList() const {
155 return const_cast<MCSection *>(this)->getFragmentList();
158 const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
159 MCDummyFragment &getDummyFragment() { return DummyFragment; }
161 MCSection::iterator begin();
162 MCSection::const_iterator begin() const {
163 return const_cast<MCSection *>(this)->begin();
166 MCSection::iterator end();
167 MCSection::const_iterator end() const {
168 return const_cast<MCSection *>(this)->end();
171 MCSection::reverse_iterator rbegin();
172 MCSection::const_reverse_iterator rbegin() const {
173 return const_cast<MCSection *>(this)->rbegin();
176 MCSection::reverse_iterator rend();
177 MCSection::const_reverse_iterator rend() const {
178 return const_cast<MCSection *>(this)->rend();
181 MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection);
185 virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
186 const MCExpr *Subsection) const = 0;
188 /// Return true if a .align directive should use "optimized nops" to fill
190 virtual bool UseCodeAlign() const = 0;
192 /// Check whether this section is "virtual", that is has no actual object
194 virtual bool isVirtualSection() const = 0;
197 } // end namespace llvm