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/ADT/ilist_node.h"
20 #include "llvm/MC/MCFragment.h"
21 #include "llvm/MC/SectionKind.h"
22 #include "llvm/Support/Compiler.h"
34 template <> struct ilist_alloc_traits<MCFragment> {
35 static void deleteNode(MCFragment *V);
38 /// Instances of this class represent a uniqued identifier for a section in the
39 /// current translation unit. The MCContext class uniques and creates these.
42 enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO };
44 /// \brief Express the state of bundle locked groups while emitting code.
45 enum BundleLockStateType {
48 BundleLockedAlignToEnd
51 typedef iplist<MCFragment> FragmentListType;
53 typedef FragmentListType::const_iterator const_iterator;
54 typedef FragmentListType::iterator iterator;
56 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
57 typedef FragmentListType::reverse_iterator reverse_iterator;
60 MCSection(const MCSection &) = delete;
61 void operator=(const MCSection &) = delete;
64 MCSymbol *End = nullptr;
65 /// The alignment requirement of this section.
66 unsigned Alignment = 1;
67 /// The section index in the assemblers section list.
69 /// The index of this section in the layout order.
72 /// \brief Keeping track of bundle-locked state.
73 BundleLockStateType BundleLockState = NotBundleLocked;
75 /// \brief Current nesting depth of bundle_lock directives.
76 unsigned BundleLockNestingDepth = 0;
78 /// \brief We've seen a bundle_lock directive but not its first instruction
80 unsigned BundleGroupBeforeFirstInst : 1;
82 /// Whether this section has had instructions emitted into it.
83 unsigned HasInstructions : 1;
85 unsigned 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 MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
97 SectionVariant Variant;
102 SectionKind getKind() const { return Kind; }
104 SectionVariant getVariant() const { return Variant; }
106 MCSymbol *getBeginSymbol() { return Begin; }
107 const MCSymbol *getBeginSymbol() const {
108 return const_cast<MCSection *>(this)->getBeginSymbol();
110 void setBeginSymbol(MCSymbol *Sym) {
114 MCSymbol *getEndSymbol(MCContext &Ctx);
115 bool hasEnded() const;
117 unsigned getAlignment() const { return Alignment; }
118 void setAlignment(unsigned Value) { Alignment = Value; }
120 unsigned getOrdinal() const { return Ordinal; }
121 void setOrdinal(unsigned Value) { Ordinal = Value; }
123 unsigned getLayoutOrder() const { return LayoutOrder; }
124 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
126 BundleLockStateType getBundleLockState() const { return BundleLockState; }
127 void setBundleLockState(BundleLockStateType NewState);
128 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
130 bool isBundleGroupBeforeFirstInst() const {
131 return BundleGroupBeforeFirstInst;
133 void setBundleGroupBeforeFirstInst(bool IsFirst) {
134 BundleGroupBeforeFirstInst = IsFirst;
137 bool hasInstructions() const { return HasInstructions; }
138 void setHasInstructions(bool Value) { HasInstructions = Value; }
140 bool isRegistered() const { return IsRegistered; }
141 void setIsRegistered(bool Value) { IsRegistered = Value; }
143 MCSection::FragmentListType &getFragmentList() { return Fragments; }
144 const MCSection::FragmentListType &getFragmentList() const {
145 return const_cast<MCSection *>(this)->getFragmentList();
148 /// Support for MCFragment::getNextNode().
149 static FragmentListType MCSection::*getSublistAccess(MCFragment *) {
150 return &MCSection::Fragments;
153 const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
154 MCDummyFragment &getDummyFragment() { return DummyFragment; }
156 iterator begin() { return Fragments.begin(); }
157 const_iterator begin() const { return Fragments.begin(); }
159 iterator end() { return Fragments.end(); }
160 const_iterator end() const { return Fragments.end(); }
162 reverse_iterator rbegin() { return Fragments.rbegin(); }
163 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
165 reverse_iterator rend() { return Fragments.rend(); }
166 const_reverse_iterator rend() const { return Fragments.rend(); }
168 MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection);
172 virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
173 const MCExpr *Subsection) const = 0;
175 /// Return true if a .align directive should use "optimized nops" to fill
177 virtual bool UseCodeAlign() const = 0;
179 /// Check whether this section is "virtual", that is has no actual object
181 virtual bool isVirtualSection() const = 0;
184 } // end namespace llvm