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 struct ilist_node_traits<MCFragment> {
36 MCFragment *createNode(const MCFragment &V);
37 static void deleteNode(MCFragment *V);
39 void addNodeToList(MCFragment *) {}
40 void removeNodeFromList(MCFragment *) {}
41 void transferNodesFromList(ilist_node_traits & /*SrcTraits*/,
42 ilist_iterator<MCFragment> /*first*/,
43 ilist_iterator<MCFragment> /*last*/) {}
46 /// Instances of this class represent a uniqued identifier for a section in the
47 /// current translation unit. The MCContext class uniques and creates these.
50 enum SectionVariant { SV_COFF = 0, SV_ELF, SV_MachO };
52 /// \brief Express the state of bundle locked groups while emitting code.
53 enum BundleLockStateType {
56 BundleLockedAlignToEnd
59 typedef iplist<MCFragment> FragmentListType;
61 typedef FragmentListType::const_iterator const_iterator;
62 typedef FragmentListType::iterator iterator;
64 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
65 typedef FragmentListType::reverse_iterator reverse_iterator;
68 MCSection(const MCSection &) = delete;
69 void operator=(const MCSection &) = delete;
72 MCSymbol *End = nullptr;
73 /// The alignment requirement of this section.
74 unsigned Alignment = 1;
75 /// The section index in the assemblers section list.
77 /// The index of this section in the layout order.
80 /// \brief Keeping track of bundle-locked state.
81 BundleLockStateType BundleLockState = NotBundleLocked;
83 /// \brief Current nesting depth of bundle_lock directives.
84 unsigned BundleLockNestingDepth = 0;
86 /// \brief We've seen a bundle_lock directive but not its first instruction
88 unsigned BundleGroupBeforeFirstInst : 1;
90 /// Whether this section has had instructions emitted into it.
91 unsigned HasInstructions : 1;
93 unsigned IsRegistered : 1;
95 FragmentListType Fragments;
97 /// Mapping from subsection number to insertion point for subsection numbers
98 /// below that number.
99 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
102 MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
103 SectionVariant Variant;
107 virtual ~MCSection();
109 SectionKind getKind() const { return Kind; }
111 SectionVariant getVariant() const { return Variant; }
113 MCSymbol *getBeginSymbol() { return Begin; }
114 const MCSymbol *getBeginSymbol() const {
115 return const_cast<MCSection *>(this)->getBeginSymbol();
117 void setBeginSymbol(MCSymbol *Sym) {
121 MCSymbol *getEndSymbol(MCContext &Ctx);
122 bool hasEnded() const;
124 unsigned getAlignment() const { return Alignment; }
125 void setAlignment(unsigned Value) { Alignment = Value; }
127 unsigned getOrdinal() const { return Ordinal; }
128 void setOrdinal(unsigned Value) { Ordinal = Value; }
130 unsigned getLayoutOrder() const { return LayoutOrder; }
131 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
133 BundleLockStateType getBundleLockState() const { return BundleLockState; }
134 void setBundleLockState(BundleLockStateType NewState);
135 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
137 bool isBundleGroupBeforeFirstInst() const {
138 return BundleGroupBeforeFirstInst;
140 void setBundleGroupBeforeFirstInst(bool IsFirst) {
141 BundleGroupBeforeFirstInst = IsFirst;
144 bool hasInstructions() const { return HasInstructions; }
145 void setHasInstructions(bool Value) { HasInstructions = 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 MCSection::iterator begin();
156 MCSection::const_iterator begin() const {
157 return const_cast<MCSection *>(this)->begin();
160 MCSection::iterator end();
161 MCSection::const_iterator end() const {
162 return const_cast<MCSection *>(this)->end();
165 MCSection::reverse_iterator rbegin();
166 MCSection::const_reverse_iterator rbegin() const {
167 return const_cast<MCSection *>(this)->rbegin();
170 MCSection::reverse_iterator rend();
171 MCSection::const_reverse_iterator rend() const {
172 return const_cast<MCSection *>(this)->rend();
175 MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection);
179 virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
180 const MCExpr *Subsection) const = 0;
182 /// Return true if a .align directive should use "optimized nops" to fill
184 virtual bool UseCodeAlign() const = 0;
186 /// Check whether this section is "virtual", that is has no actual object
188 virtual bool isVirtualSection() const = 0;
191 } // end namespace llvm