LLVM 20.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 };
72
73 struct FragList {
74 MCFragment *Head = nullptr;
75 MCFragment *Tail = nullptr;
76 };
77
78private:
79 // At parse time, this holds the fragment list of the current subsection. At
80 // layout time, this holds the concatenated fragment lists of all subsections.
81 FragList *CurFragList;
82 MCSymbol *Begin;
83 MCSymbol *End = nullptr;
84 /// The alignment requirement of this section.
85 Align Alignment;
86 /// The section index in the assemblers section list.
87 unsigned Ordinal = 0;
88
89 /// Keeping track of bundle-locked state.
90 BundleLockStateType BundleLockState = NotBundleLocked;
91
92 /// Current nesting depth of bundle_lock directives.
93 unsigned BundleLockNestingDepth = 0;
94
95 /// We've seen a bundle_lock directive but not its first instruction
96 /// yet.
97 bool BundleGroupBeforeFirstInst : 1;
98
99 /// Whether this section has had instructions emitted into it.
100 bool HasInstructions : 1;
101
102 bool HasLayout : 1;
103
104 bool IsRegistered : 1;
105
106 bool IsText : 1;
107
108 bool IsVirtual : 1;
109
110 MCDummyFragment DummyFragment;
111
112 // Mapping from subsection number to fragment list. At layout time, the
113 // subsection 0 list is replaced with concatenated fragments from all
114 // subsections.
116
117protected:
118 // TODO Make Name private when possible.
121
122 MCSection(SectionVariant V, StringRef Name, bool IsText, bool IsVirtual,
123 MCSymbol *Begin);
124 ~MCSection();
125
126public:
127 MCSection(const MCSection &) = delete;
128 MCSection &operator=(const MCSection &) = delete;
129
130 StringRef getName() const { return Name; }
131 bool isText() const { return IsText; }
132
134
135 MCSymbol *getBeginSymbol() { return Begin; }
136 const MCSymbol *getBeginSymbol() const {
137 return const_cast<MCSection *>(this)->getBeginSymbol();
138 }
140 assert(!Begin);
141 Begin = Sym;
142 }
144 bool hasEnded() const;
145
146 Align getAlign() const { return Alignment; }
147 void setAlignment(Align Value) { Alignment = Value; }
148
149 /// Makes sure that Alignment is at least MinAlignment.
150 void ensureMinAlignment(Align MinAlignment) {
151 if (Alignment < MinAlignment)
152 Alignment = MinAlignment;
153 }
154
155 unsigned getOrdinal() const { return Ordinal; }
156 void setOrdinal(unsigned Value) { Ordinal = Value; }
157
158 BundleLockStateType getBundleLockState() const { return BundleLockState; }
160 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
161
163 return BundleGroupBeforeFirstInst;
164 }
165 void setBundleGroupBeforeFirstInst(bool IsFirst) {
166 BundleGroupBeforeFirstInst = IsFirst;
167 }
168
169 bool hasInstructions() const { return HasInstructions; }
170 void setHasInstructions(bool Value) { HasInstructions = Value; }
171
172 bool hasLayout() const { return HasLayout; }
173 void setHasLayout(bool Value) { HasLayout = Value; }
174
175 bool isRegistered() const { return IsRegistered; }
176 void setIsRegistered(bool Value) { IsRegistered = Value; }
177
178 const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
179 MCDummyFragment &getDummyFragment() { return DummyFragment; }
180
181 FragList *curFragList() const { return CurFragList; }
182 iterator begin() const { return iterator(CurFragList->Head); }
183 iterator end() const { return {}; }
184 bool empty() const { return !CurFragList->Head; }
185
186 void dump() const;
187
188 virtual void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
190 uint32_t Subsection) const = 0;
191
192 /// Return true if a .align directive should use "optimized nops" to fill
193 /// instead of 0s.
194 virtual bool useCodeAlign() const = 0;
195
196 /// Check whether this section is "virtual", that is has no actual object
197 /// file contents.
198 bool isVirtualSection() const { return IsVirtual; }
199
200 virtual StringRef getVirtualSectionKind() const;
201};
202
203} // end namespace llvm
204
205#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:147
const MCDummyFragment & getDummyFragment() const
Definition: MCSection.h:178
unsigned getOrdinal() const
Definition: MCSection.h:155
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:150
void dump() const
Definition: MCSection.cpp:72
bool hasLayout() const
Definition: MCSection.h:172
SectionVariant getVariant() const
Definition: MCSection.h:133
MCSymbol * getEndSymbol(MCContext &Ctx)
Definition: MCSection.cpp:33
Align getAlign() const
Definition: MCSection.h:146
SectionVariant Variant
Definition: MCSection.h:120
void setBundleLockState(BundleLockStateType NewState)
Definition: MCSection.cpp:50
static constexpr unsigned NonUniqueID
Definition: MCSection.h:40
const MCSymbol * getBeginSymbol() const
Definition: MCSection.h:136
bool hasInstructions() const
Definition: MCSection.h:169
bool isRegistered() const
Definition: MCSection.h:175
void setHasInstructions(bool Value)
Definition: MCSection.h:170
void setBeginSymbol(MCSymbol *Sym)
Definition: MCSection.h:139
MCSection(const MCSection &)=delete
friend MCAssembler
Definition: MCSection.h:38
bool isBundleGroupBeforeFirstInst() const
Definition: MCSection.h:162
void setOrdinal(unsigned Value)
Definition: MCSection.h:156
bool isText() const
Definition: MCSection.h:131
bool isVirtualSection() const
Check whether this section is "virtual", that is has no actual object file contents.
Definition: MCSection.h:198
StringRef Name
Definition: MCSection.h:119
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:183
bool isBundleLocked() const
Definition: MCSection.h:160
MCDummyFragment & getDummyFragment()
Definition: MCSection.h:179
MCSection & operator=(const MCSection &)=delete
StringRef getName() const
Definition: MCSection.h:130
bool hasEnded() const
Definition: MCSection.cpp:39
bool empty() const
Definition: MCSection.h:184
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:181
MCSymbol * getBeginSymbol()
Definition: MCSection.h:135
BundleLockStateType getBundleLockState() const
Definition: MCSection.h:158
void setHasLayout(bool Value)
Definition: MCSection.h:173
iterator begin() const
Definition: MCSection.h:182
void setBundleGroupBeforeFirstInst(bool IsFirst)
Definition: MCSection.h:165
void setIsRegistered(bool Value)
Definition: MCSection.h:176
virtual void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, uint32_t Subsection) const =0
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:1210
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