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/ilist.h"
19 #include "llvm/MC/MCFragment.h"
20 #include "llvm/MC/SectionKind.h"
33 template <> struct ilist_alloc_traits<MCFragment> {
34 static void deleteNode(MCFragment *V);
37 /// Instances of this class represent a uniqued identifier for a section in the
38 /// current translation unit. The MCContext class uniques and creates these.
41 enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO, SV_Wasm };
43 /// Express the state of bundle locked groups while emitting code.
44 enum BundleLockStateType {
47 BundleLockedAlignToEnd
50 using FragmentListType = iplist<MCFragment>;
52 using const_iterator = FragmentListType::const_iterator;
53 using iterator = FragmentListType::iterator;
55 using const_reverse_iterator = FragmentListType::const_reverse_iterator;
56 using reverse_iterator = FragmentListType::reverse_iterator;
60 MCSymbol *End = nullptr;
61 /// The alignment requirement of this section.
62 unsigned Alignment = 1;
63 /// The section index in the assemblers section list.
65 /// The index of this section in the layout order.
68 /// Keeping track of bundle-locked state.
69 BundleLockStateType BundleLockState = NotBundleLocked;
71 /// Current nesting depth of bundle_lock directives.
72 unsigned BundleLockNestingDepth = 0;
74 /// We've seen a bundle_lock directive but not its first instruction
76 bool BundleGroupBeforeFirstInst : 1;
78 /// Whether this section has had instructions emitted into it.
79 bool HasInstructions : 1;
81 /// Whether this section has had data emitted into it.
82 /// Right now this is only used by the ARM backend.
85 bool IsRegistered : 1;
87 MCDummyFragment DummyFragment;
89 FragmentListType Fragments;
91 /// Mapping from subsection number to insertion point for subsection numbers
92 /// below that number.
93 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
96 SectionVariant Variant;
99 MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
103 MCSection(const MCSection &) = delete;
104 MCSection &operator=(const MCSection &) = delete;
106 SectionKind getKind() const { return Kind; }
108 SectionVariant getVariant() const { return Variant; }
110 MCSymbol *getBeginSymbol() { return Begin; }
111 const MCSymbol *getBeginSymbol() const {
112 return const_cast<MCSection *>(this)->getBeginSymbol();
114 void setBeginSymbol(MCSymbol *Sym) {
118 MCSymbol *getEndSymbol(MCContext &Ctx);
119 bool hasEnded() const;
121 unsigned getAlignment() const { return Alignment; }
122 void setAlignment(unsigned Value) { Alignment = Value; }
124 unsigned getOrdinal() const { return Ordinal; }
125 void setOrdinal(unsigned Value) { Ordinal = Value; }
127 unsigned getLayoutOrder() const { return LayoutOrder; }
128 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
130 BundleLockStateType getBundleLockState() const { return BundleLockState; }
131 void setBundleLockState(BundleLockStateType NewState);
132 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
134 bool isBundleGroupBeforeFirstInst() const {
135 return BundleGroupBeforeFirstInst;
137 void setBundleGroupBeforeFirstInst(bool IsFirst) {
138 BundleGroupBeforeFirstInst = IsFirst;
141 bool hasInstructions() const { return HasInstructions; }
142 void setHasInstructions(bool Value) { HasInstructions = Value; }
144 bool hasData() const { return HasData; }
145 void setHasData(bool Value) { HasData = Value; }
147 bool isRegistered() const { return IsRegistered; }
148 void setIsRegistered(bool Value) { IsRegistered = Value; }
150 MCSection::FragmentListType &getFragmentList() { return Fragments; }
151 const MCSection::FragmentListType &getFragmentList() const {
152 return const_cast<MCSection *>(this)->getFragmentList();
155 /// Support for MCFragment::getNextNode().
156 static FragmentListType MCSection::*getSublistAccess(MCFragment *) {
157 return &MCSection::Fragments;
160 const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
161 MCDummyFragment &getDummyFragment() { return DummyFragment; }
163 iterator begin() { return Fragments.begin(); }
164 const_iterator begin() const { return Fragments.begin(); }
166 iterator end() { return Fragments.end(); }
167 const_iterator end() const { return Fragments.end(); }
169 reverse_iterator rbegin() { return Fragments.rbegin(); }
170 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
172 reverse_iterator rend() { return Fragments.rend(); }
173 const_reverse_iterator rend() const { return Fragments.rend(); }
175 MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection);
179 virtual void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
181 const MCExpr *Subsection) const = 0;
183 /// Return true if a .align directive should use "optimized nops" to fill
185 virtual bool UseCodeAlign() const = 0;
187 /// Check whether this section is "virtual", that is has no actual object
189 virtual bool isVirtualSection() const = 0;
192 } // end namespace llvm
194 #endif // LLVM_MC_MCSECTION_H