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 };
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 IsRegistered : 1;
103
104 bool IsText : 1;
105
106 bool IsVirtual : 1;
107
108 MCDummyFragment DummyFragment;
109
110 // Mapping from subsection number to fragment list. At layout time, the
111 // subsection 0 list is replaced with concatenated fragments from all
112 // subsections.
114
115protected:
116 // TODO Make Name private when possible.
119
120 MCSection(SectionVariant V, StringRef Name, bool IsText, bool IsVirtual,
121 MCSymbol *Begin);
122 ~MCSection();
123
124public:
125 MCSection(const MCSection &) = delete;
126 MCSection &operator=(const MCSection &) = delete;
127
128 StringRef getName() const { return Name; }
129 bool isText() const { return IsText; }
130
132
133 MCSymbol *getBeginSymbol() { return Begin; }
134 const MCSymbol *getBeginSymbol() const {
135 return const_cast<MCSection *>(this)->getBeginSymbol();
136 }
138 assert(!Begin);
139 Begin = Sym;
140 }
142 bool hasEnded() const;
143
144 Align getAlign() const { return Alignment; }
145 void setAlignment(Align Value) { Alignment = Value; }
146
147 /// Makes sure that Alignment is at least MinAlignment.
148 void ensureMinAlignment(Align MinAlignment) {
149 if (Alignment < MinAlignment)
150 Alignment = MinAlignment;
151 }
152
153 unsigned getOrdinal() const { return Ordinal; }
154 void setOrdinal(unsigned Value) { Ordinal = Value; }
155
156 BundleLockStateType getBundleLockState() const { return BundleLockState; }
158 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
159
161 return BundleGroupBeforeFirstInst;
162 }
163 void setBundleGroupBeforeFirstInst(bool IsFirst) {
164 BundleGroupBeforeFirstInst = IsFirst;
165 }
166
167 bool hasInstructions() const { return HasInstructions; }
168 void setHasInstructions(bool Value) { HasInstructions = Value; }
169
170 bool isRegistered() const { return IsRegistered; }
171 void setIsRegistered(bool Value) { IsRegistered = Value; }
172
173 const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
174 MCDummyFragment &getDummyFragment() { return DummyFragment; }
175
176 FragList *curFragList() const { return CurFragList; }
177 iterator begin() const { return iterator(CurFragList->Head); }
178 iterator end() const { return {}; }
179 bool empty() const { return !CurFragList->Head; }
180
181 void dump() const;
182
183 virtual void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
185 uint32_t Subsection) const = 0;
186
187 /// Return true if a .align directive should use "optimized nops" to fill
188 /// instead of 0s.
189 virtual bool useCodeAlign() const = 0;
190
191 /// Check whether this section is "virtual", that is has no actual object
192 /// file contents.
193 bool isVirtualSection() const { return IsVirtual; }
194
195 virtual StringRef getVirtualSectionKind() const;
196};
197
198} // end namespace llvm
199
200#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:145
const MCDummyFragment & getDummyFragment() const
Definition: MCSection.h:173
unsigned getOrdinal() const
Definition: MCSection.h:153
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:148
void dump() const
Definition: MCSection.cpp:72
SectionVariant getVariant() const
Definition: MCSection.h:131
MCSymbol * getEndSymbol(MCContext &Ctx)
Definition: MCSection.cpp:33
Align getAlign() const
Definition: MCSection.h:144
SectionVariant Variant
Definition: MCSection.h:118
void setBundleLockState(BundleLockStateType NewState)
Definition: MCSection.cpp:50
static constexpr unsigned NonUniqueID
Definition: MCSection.h:40
const MCSymbol * getBeginSymbol() const
Definition: MCSection.h:134
bool hasInstructions() const
Definition: MCSection.h:167
bool isRegistered() const
Definition: MCSection.h:170
void setHasInstructions(bool Value)
Definition: MCSection.h:168
void setBeginSymbol(MCSymbol *Sym)
Definition: MCSection.h:137
MCSection(const MCSection &)=delete
friend MCAssembler
Definition: MCSection.h:38
bool isBundleGroupBeforeFirstInst() const
Definition: MCSection.h:160
void setOrdinal(unsigned Value)
Definition: MCSection.h:154
bool isText() const
Definition: MCSection.h:129
bool isVirtualSection() const
Check whether this section is "virtual", that is has no actual object file contents.
Definition: MCSection.h:193
StringRef Name
Definition: MCSection.h:117
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:178
bool isBundleLocked() const
Definition: MCSection.h:158
MCDummyFragment & getDummyFragment()
Definition: MCSection.h:174
MCSection & operator=(const MCSection &)=delete
StringRef getName() const
Definition: MCSection.h:128
bool hasEnded() const
Definition: MCSection.cpp:39
bool empty() const
Definition: MCSection.h:179
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:176
MCSymbol * getBeginSymbol()
Definition: MCSection.h:133
BundleLockStateType getBundleLockState() const
Definition: MCSection.h:156
iterator begin() const
Definition: MCSection.h:177
void setBundleGroupBeforeFirstInst(bool IsFirst)
Definition: MCSection.h:163
void setIsRegistered(bool Value)
Definition: MCSection.h:171
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: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