LLVM 19.0.0git
MCSection.h
Go to the documentation of this file.
1//===- MCSection.h - Machine Code Sections ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the MCSection class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_MC_MCSECTION_H
14#define LLVM_MC_MCSECTION_H
15
17#include "llvm/MC/MCFragment.h"
18#include "llvm/MC/SectionKind.h"
20#include <cassert>
21#include <utility>
22
23namespace llvm {
24
25class MCAsmInfo;
26class MCAssembler;
27class MCContext;
28class MCExpr;
29class MCObjectStreamer;
30class MCSymbol;
31class raw_ostream;
32class Triple;
33
34/// Instances of this class represent a uniqued identifier for a section in the
35/// current translation unit. The MCContext class uniques and creates these.
36class MCSection {
37public:
40 static constexpr unsigned NonUniqueID = ~0U;
41
51 };
52
53 /// Express the state of bundle locked groups while emitting code.
58 };
59
60 struct iterator {
61 MCFragment *F = nullptr;
62 iterator() = default;
63 explicit iterator(MCFragment *F) : F(F) {}
64 MCFragment &operator*() const { return *F; }
65 bool operator==(const iterator &O) const { return F == O.F; }
66 bool operator!=(const iterator &O) const { return F != O.F; }
68 F = F->Next;
69 return *this;
70 }
71 iterator operator++(int) { return iterator(F->Next); }
72 };
73
74 struct FragList {
75 MCFragment *Head = nullptr;
76 MCFragment *Tail = nullptr;
77 };
78
79private:
80 // At parse time, this holds the fragment list of the current subsection. At
81 // layout time, this holds the concatenated fragment lists of all subsections.
82 FragList *CurFragList;
83 MCSymbol *Begin;
84 MCSymbol *End = nullptr;
85 /// The alignment requirement of this section.
86 Align Alignment;
87 /// The section index in the assemblers section list.
88 unsigned Ordinal = 0;
89 /// The index of this section in the layout order.
90 unsigned LayoutOrder = 0;
91
92 /// Keeping track of bundle-locked state.
93 BundleLockStateType BundleLockState = NotBundleLocked;
94
95 /// Current nesting depth of bundle_lock directives.
96 unsigned BundleLockNestingDepth = 0;
97
98 /// We've seen a bundle_lock directive but not its first instruction
99 /// yet.
100 bool BundleGroupBeforeFirstInst : 1;
101
102 /// Whether this section has had instructions emitted into it.
103 bool HasInstructions : 1;
104
105 bool HasLayout : 1;
106
107 bool IsRegistered : 1;
108
109 bool IsText : 1;
110
111 bool IsVirtual : 1;
112
113 MCDummyFragment DummyFragment;
114
115 // Mapping from subsection number to fragment list. At layout time, the
116 // subsection 0 list is replaced with concatenated fragments from all
117 // subsections.
119
120 /// State for tracking labels that don't yet have Fragments
121 struct PendingLabel {
122 MCSymbol* Sym;
123 unsigned Subsection;
124 PendingLabel(MCSymbol* Sym, unsigned Subsection = 0)
125 : Sym(Sym), Subsection(Subsection) {}
126 };
127 SmallVector<PendingLabel, 2> PendingLabels;
128
129protected:
130 // TODO Make Name private when possible.
133
134 MCSection(SectionVariant V, StringRef Name, bool IsText, bool IsVirtual,
135 MCSymbol *Begin);
136 ~MCSection();
137
138public:
139 MCSection(const MCSection &) = delete;
140 MCSection &operator=(const MCSection &) = delete;
141
142 StringRef getName() const { return Name; }
143 bool isText() const { return IsText; }
144
146
147 MCSymbol *getBeginSymbol() { return Begin; }
148 const MCSymbol *getBeginSymbol() const {
149 return const_cast<MCSection *>(this)->getBeginSymbol();
150 }
152 assert(!Begin);
153 Begin = Sym;
154 }
156 bool hasEnded() const;
157
158 Align getAlign() const { return Alignment; }
159 void setAlignment(Align Value) { Alignment = Value; }
160
161 /// Makes sure that Alignment is at least MinAlignment.
162 void ensureMinAlignment(Align MinAlignment) {
163 if (Alignment < MinAlignment)
164 Alignment = MinAlignment;
165 }
166
167 unsigned getOrdinal() const { return Ordinal; }
168 void setOrdinal(unsigned Value) { Ordinal = Value; }
169
170 unsigned getLayoutOrder() const { return LayoutOrder; }
171 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
172
173 BundleLockStateType getBundleLockState() const { return BundleLockState; }
175 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
176
178 return BundleGroupBeforeFirstInst;
179 }
180 void setBundleGroupBeforeFirstInst(bool IsFirst) {
181 BundleGroupBeforeFirstInst = IsFirst;
182 }
183
184 bool hasInstructions() const { return HasInstructions; }
185 void setHasInstructions(bool Value) { HasInstructions = Value; }
186
187 bool hasLayout() const { return HasLayout; }
188 void setHasLayout(bool Value) { HasLayout = Value; }
189
190 bool isRegistered() const { return IsRegistered; }
191 void setIsRegistered(bool Value) { IsRegistered = Value; }
192
193 const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
194 MCDummyFragment &getDummyFragment() { return DummyFragment; }
195
196 FragList *curFragList() const { return CurFragList; }
197 iterator begin() const { return iterator(CurFragList->Head); }
198 iterator end() const { return {}; }
199 bool empty() const { return !CurFragList->Head; }
200
201 void dump() const;
202
203 virtual void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
205 uint32_t Subsection) const = 0;
206
207 /// Return true if a .align directive should use "optimized nops" to fill
208 /// instead of 0s.
209 virtual bool useCodeAlign() const = 0;
210
211 /// Check whether this section is "virtual", that is has no actual object
212 /// file contents.
213 bool isVirtualSection() const { return IsVirtual; }
214
215 virtual StringRef getVirtualSectionKind() const;
216
217 /// Add a pending label for the requested subsection. This label will be
218 /// associated with a fragment in flushPendingLabels()
219 void addPendingLabel(MCSymbol* label, unsigned Subsection = 0);
220};
221
222} // end namespace llvm
223
224#endif // LLVM_MC_MCSECTION_H
Symbol * Sym
Definition: ELF_riscv.cpp:479
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallVector class.
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Context object for machine code objects.
Definition: MCContext.h:83
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
void setAlignment(Align Value)
Definition: MCSection.h:159
const MCDummyFragment & getDummyFragment() const
Definition: MCSection.h:193
unsigned getOrdinal() const
Definition: MCSection.h:167
void setLayoutOrder(unsigned Value)
Definition: MCSection.h:171
virtual StringRef getVirtualSectionKind() const
Definition: MCSection.cpp:69
friend MCObjectStreamer
Definition: MCSection.h:39
void ensureMinAlignment(Align MinAlignment)
Makes sure that Alignment is at least MinAlignment.
Definition: MCSection.h:162
void dump() const
Definition: MCSection.cpp:72
unsigned getLayoutOrder() const
Definition: MCSection.h:170
bool hasLayout() const
Definition: MCSection.h:187
SectionVariant getVariant() const
Definition: MCSection.h:145
MCSymbol * getEndSymbol(MCContext &Ctx)
Definition: MCSection.cpp:33
Align getAlign() const
Definition: MCSection.h:158
SectionVariant Variant
Definition: MCSection.h:132
void setBundleLockState(BundleLockStateType NewState)
Definition: MCSection.cpp:50
static constexpr unsigned NonUniqueID
Definition: MCSection.h:40
const MCSymbol * getBeginSymbol() const
Definition: MCSection.h:148
bool hasInstructions() const
Definition: MCSection.h:184
bool isRegistered() const
Definition: MCSection.h:190
void setHasInstructions(bool Value)
Definition: MCSection.h:185
void setBeginSymbol(MCSymbol *Sym)
Definition: MCSection.h:151
MCSection(const MCSection &)=delete
friend MCAssembler
Definition: MCSection.h:38
bool isBundleGroupBeforeFirstInst() const
Definition: MCSection.h:177
void setOrdinal(unsigned Value)
Definition: MCSection.h:168
bool isText() const
Definition: MCSection.h:143
bool isVirtualSection() const
Check whether this section is "virtual", that is has no actual object file contents.
Definition: MCSection.h:213
StringRef Name
Definition: MCSection.h:131
virtual bool useCodeAlign() const =0
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
iterator end() const
Definition: MCSection.h:198
bool isBundleLocked() const
Definition: MCSection.h:175
MCDummyFragment & getDummyFragment()
Definition: MCSection.h:194
MCSection & operator=(const MCSection &)=delete
StringRef getName() const
Definition: MCSection.h:142
bool hasEnded() const
Definition: MCSection.cpp:39
bool empty() const
Definition: MCSection.h:199
BundleLockStateType
Express the state of bundle locked groups while emitting code.
Definition: MCSection.h:54
@ BundleLockedAlignToEnd
Definition: MCSection.h:57
FragList * curFragList() const
Definition: MCSection.h:196
MCSymbol * getBeginSymbol()
Definition: MCSection.h:147
BundleLockStateType getBundleLockState() const
Definition: MCSection.h:173
void setHasLayout(bool Value)
Definition: MCSection.h:188
iterator begin() const
Definition: MCSection.h:197
void setBundleGroupBeforeFirstInst(bool IsFirst)
Definition: MCSection.h:180
void setIsRegistered(bool Value)
Definition: MCSection.h:191
virtual void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, uint32_t Subsection) const =0
void addPendingLabel(MCSymbol *label, unsigned Subsection=0)
Add a pending label for the requested subsection.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
iterator & operator++()
Definition: MCSection.h:67
MCFragment & operator*() const
Definition: MCSection.h:64
bool operator==(const iterator &O) const
Definition: MCSection.h:65
bool operator!=(const iterator &O) const
Definition: MCSection.h:66
iterator(MCFragment *F)
Definition: MCSection.h:63
iterator operator++(int)
Definition: MCSection.h:71