LLVM  14.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 
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/ilist.h"
18 #include "llvm/MC/MCFragment.h"
19 #include "llvm/MC/SectionKind.h"
20 #include "llvm/Support/Alignment.h"
21 #include <cassert>
22 #include <utility>
23 
24 namespace llvm {
25 
26 class MCAsmInfo;
27 class MCContext;
28 class MCExpr;
29 class MCSymbol;
30 class raw_ostream;
31 class Triple;
32 
33 template <> struct ilist_alloc_traits<MCFragment> {
34  static void deleteNode(MCFragment *V);
35 };
36 
37 /// Instances of this class represent a uniqued identifier for a section in the
38 /// current translation unit. The MCContext class uniques and creates these.
39 class MCSection {
40 public:
41  static constexpr unsigned NonUniqueID = ~0U;
42 
44  SV_COFF = 0,
50  };
51 
52  /// Express the state of bundle locked groups while emitting code.
57  };
58 
60 
63 
66 
67 private:
68  MCSymbol *Begin;
69  MCSymbol *End = nullptr;
70  /// The alignment requirement of this section.
71  Align Alignment;
72  /// The section index in the assemblers section list.
73  unsigned Ordinal = 0;
74  /// The index of this section in the layout order.
75  unsigned LayoutOrder;
76 
77  /// Keeping track of bundle-locked state.
78  BundleLockStateType BundleLockState = NotBundleLocked;
79 
80  /// Current nesting depth of bundle_lock directives.
81  unsigned BundleLockNestingDepth = 0;
82 
83  /// We've seen a bundle_lock directive but not its first instruction
84  /// yet.
85  bool BundleGroupBeforeFirstInst : 1;
86 
87  /// Whether this section has had instructions emitted into it.
88  bool HasInstructions : 1;
89 
90  bool IsRegistered : 1;
91 
92  MCDummyFragment DummyFragment;
93 
94  FragmentListType Fragments;
95 
96  /// Mapping from subsection number to insertion point for subsection numbers
97  /// below that number.
98  SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
99 
100  /// State for tracking labels that don't yet have Fragments
101  struct PendingLabel {
102  MCSymbol* Sym;
103  unsigned Subsection;
104  PendingLabel(MCSymbol* Sym, unsigned Subsection = 0)
105  : Sym(Sym), Subsection(Subsection) {}
106  };
107  SmallVector<PendingLabel, 2> PendingLabels;
108 
109 protected:
110  // TODO Make Name private when possible.
114 
116  ~MCSection();
117 
118 public:
119  MCSection(const MCSection &) = delete;
120  MCSection &operator=(const MCSection &) = delete;
121 
122  StringRef getName() const { return Name; }
123  SectionKind getKind() const { return Kind; }
124 
125  SectionVariant getVariant() const { return Variant; }
126 
127  MCSymbol *getBeginSymbol() { return Begin; }
128  const MCSymbol *getBeginSymbol() const {
129  return const_cast<MCSection *>(this)->getBeginSymbol();
130  }
132  assert(!Begin);
133  Begin = Sym;
134  }
136  bool hasEnded() const;
137 
138  unsigned getAlignment() const { return Alignment.value(); }
139  void setAlignment(Align Value) { Alignment = Value; }
140 
141  unsigned getOrdinal() const { return Ordinal; }
142  void setOrdinal(unsigned Value) { Ordinal = Value; }
143 
144  unsigned getLayoutOrder() const { return LayoutOrder; }
145  void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
146 
147  BundleLockStateType getBundleLockState() const { return BundleLockState; }
149  bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
150 
152  return BundleGroupBeforeFirstInst;
153  }
154  void setBundleGroupBeforeFirstInst(bool IsFirst) {
155  BundleGroupBeforeFirstInst = IsFirst;
156  }
157 
158  bool hasInstructions() const { return HasInstructions; }
159  void setHasInstructions(bool Value) { HasInstructions = Value; }
160 
161  bool isRegistered() const { return IsRegistered; }
162  void setIsRegistered(bool Value) { IsRegistered = Value; }
163 
166  return const_cast<MCSection *>(this)->getFragmentList();
167  }
168 
169  /// Support for MCFragment::getNextNode().
171  return &MCSection::Fragments;
172  }
173 
174  const MCDummyFragment &getDummyFragment() const { return DummyFragment; }
175  MCDummyFragment &getDummyFragment() { return DummyFragment; }
176 
177  iterator begin() { return Fragments.begin(); }
178  const_iterator begin() const { return Fragments.begin(); }
179 
180  iterator end() { return Fragments.end(); }
181  const_iterator end() const { return Fragments.end(); }
182 
184 
185  void dump() const;
186 
187  virtual void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
188  raw_ostream &OS,
189  const MCExpr *Subsection) const = 0;
190 
191  /// Return true if a .align directive should use "optimized nops" to fill
192  /// instead of 0s.
193  virtual bool UseCodeAlign() const = 0;
194 
195  /// Check whether this section is "virtual", that is has no actual object
196  /// file contents.
197  virtual bool isVirtualSection() const = 0;
198 
199  virtual StringRef getVirtualSectionKind() const;
200 
201  /// Add a pending label for the requested subsection. This label will be
202  /// associated with a fragment in flushPendingLabels()
203  void addPendingLabel(MCSymbol* label, unsigned Subsection = 0);
204 
205  /// Associate all pending labels in a subsection with a fragment.
206  void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0,
207  unsigned Subsection = 0);
208 
209  /// Associate all pending labels with empty data fragments. One fragment
210  /// will be created for each subsection as necessary.
211  void flushPendingLabels();
212 };
213 
214 } // end namespace llvm
215 
216 #endif // LLVM_MC_MCSECTION_H
llvm::MCSection::isBundleLocked
bool isBundleLocked() const
Definition: MCSection.h:149
llvm::MCSection::Name
StringRef Name
Definition: MCSection.h:111
llvm::MCSection::setIsRegistered
void setIsRegistered(bool Value)
Definition: MCSection.h:162
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCSection::SV_COFF
@ SV_COFF
Definition: MCSection.h:44
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCSection::setBundleLockState
void setBundleLockState(BundleLockStateType NewState)
Definition: MCSection.cpp:39
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::MCSection::addPendingLabel
void addPendingLabel(MCSymbol *label, unsigned Subsection=0)
Add a pending label for the requested subsection.
Definition: MCSection.cpp:91
llvm::MCSection::SV_XCOFF
@ SV_XCOFF
Definition: MCSection.h:49
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::MCSection::getDummyFragment
MCDummyFragment & getDummyFragment()
Definition: MCSection.h:175
llvm::MCSection::getBeginSymbol
MCSymbol * getBeginSymbol()
Definition: MCSection.h:127
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
ilist.h
llvm::MCSection::SV_MachO
@ SV_MachO
Definition: MCSection.h:47
llvm::MCSection::dump
void dump() const
Definition: MCSection.cpp:127
llvm::MCSection::getLayoutOrder
unsigned getLayoutOrder() const
Definition: MCSection.h:144
llvm::MCSection::getSublistAccess
static FragmentListType MCSection::* getSublistAccess(MCFragment *)
Support for MCFragment::getNextNode().
Definition: MCSection.h:170
MCFragment.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::iplist_impl< simple_ilist< MCFragment, Options... >, ilist_traits< MCFragment > >::const_reverse_iterator
base_list_type::const_reverse_iterator const_reverse_iterator
Definition: ilist.h:181
llvm::MCSection::getBundleLockState
BundleLockStateType getBundleLockState() const
Definition: MCSection.h:147
llvm::MCSection::getSubsectionInsertionPoint
MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection)
Definition: MCSection.cpp:59
llvm::MCSection::MCSection
MCSection(SectionVariant V, StringRef Name, SectionKind K, MCSymbol *Begin)
Definition: MCSection.cpp:23
llvm::MCSection::setBundleGroupBeforeFirstInst
void setBundleGroupBeforeFirstInst(bool IsFirst)
Definition: MCSection.h:154
llvm::iplist< MCFragment >
llvm::MCSection::getFragmentList
MCSection::FragmentListType & getFragmentList()
Definition: MCSection.h:164
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::MCSection::end
iterator end()
Definition: MCSection.h:180
llvm::MCFragment
Definition: MCFragment.h:31
llvm::MCDummyFragment
Definition: MCFragment.h:115
llvm::MCSection::PrintSwitchToSection
virtual void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, const MCExpr *Subsection) const =0
llvm::MCSection::setLayoutOrder
void setLayoutOrder(unsigned Value)
Definition: MCSection.h:145
llvm::MCSection::hasEnded
bool hasEnded() const
Definition: MCSection.cpp:35
llvm::MCSection::Variant
SectionVariant Variant
Definition: MCSection.h:112
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCSection::end
const_iterator end() const
Definition: MCSection.h:181
llvm::MCSection::getFragmentList
const MCSection::FragmentListType & getFragmentList() const
Definition: MCSection.h:165
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::MCSection::getVariant
SectionVariant getVariant() const
Definition: MCSection.h:125
llvm::MCSection::getEndSymbol
MCSymbol * getEndSymbol(MCContext &Ctx)
Definition: MCSection.cpp:29
llvm::iplist_impl< simple_ilist< MCFragment, Options... >, ilist_traits< MCFragment > >::reverse_iterator
base_list_type::reverse_iterator reverse_iterator
Definition: ilist.h:179
llvm::MCSection::BundleLockedAlignToEnd
@ BundleLockedAlignToEnd
Definition: MCSection.h:56
llvm::MCSection::~MCSection
~MCSection()
uint64_t
llvm::iplist_impl< simple_ilist< MCFragment, Options... >, ilist_traits< MCFragment > >::iterator
base_list_type::iterator iterator
Definition: ilist.h:177
llvm::MCSection::operator=
MCSection & operator=(const MCSection &)=delete
llvm::ilist_alloc_traits
Use delete by default for iplist and ilist.
Definition: ilist.h:40
llvm::MCSection::NonUniqueID
static constexpr unsigned NonUniqueID
Definition: MCSection.h:41
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCSection::getVirtualSectionKind
virtual StringRef getVirtualSectionKind() const
Definition: MCSection.cpp:89
llvm::MCSection::BundleLockStateType
BundleLockStateType
Express the state of bundle locked groups while emitting code.
Definition: MCSection.h:53
llvm::MCSection::setHasInstructions
void setHasInstructions(bool Value)
Definition: MCSection.h:159
llvm::MCSection::getAlignment
unsigned getAlignment() const
Definition: MCSection.h:138
llvm::MCSection::getDummyFragment
const MCDummyFragment & getDummyFragment() const
Definition: MCSection.h:174
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MCSection::isVirtualSection
virtual bool isVirtualSection() const =0
Check whether this section is "virtual", that is has no actual object file contents.
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::MCSection::SV_GOFF
@ SV_GOFF
Definition: MCSection.h:46
llvm::MCSection::SV_ELF
@ SV_ELF
Definition: MCSection.h:45
llvm::MCSection::getName
StringRef getName() const
Definition: MCSection.h:122
llvm::MCSection::BundleLocked
@ BundleLocked
Definition: MCSection.h:55
llvm::MCSection::setAlignment
void setAlignment(Align Value)
Definition: MCSection.h:139
llvm::MCSection::isBundleGroupBeforeFirstInst
bool isBundleGroupBeforeFirstInst() const
Definition: MCSection.h:151
Alignment.h
llvm::MCSection::SectionVariant
SectionVariant
Definition: MCSection.h:43
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
llvm::MCSection::hasInstructions
bool hasInstructions() const
Definition: MCSection.h:158
llvm::ilist_iterator
Iterator for intrusive lists based on ilist_node.
Definition: ilist_iterator.h:57
llvm::Align::value
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
llvm::MCSection::flushPendingLabels
void flushPendingLabels()
Associate all pending labels with empty data fragments.
Definition: MCSection.cpp:112
llvm::MCSection::getBeginSymbol
const MCSymbol * getBeginSymbol() const
Definition: MCSection.h:128
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::MCSection::setBeginSymbol
void setBeginSymbol(MCSymbol *Sym)
Definition: MCSection.h:131
llvm::MCSection::NotBundleLocked
@ NotBundleLocked
Definition: MCSection.h:54
llvm::MCSection::isRegistered
bool isRegistered() const
Definition: MCSection.h:161
SectionKind.h
llvm::MCSection::getKind
SectionKind getKind() const
Definition: MCSection.h:123
llvm::MCSection::begin
iterator begin()
Definition: MCSection.h:177
llvm::MCSection::begin
const_iterator begin() const
Definition: MCSection.h:178
SmallVector.h
llvm::MCSection::Kind
SectionKind Kind
Definition: MCSection.h:113
llvm::ilist_alloc_traits::deleteNode
static void deleteNode(NodeTy *V)
Definition: ilist.h:41
llvm::MCSection::UseCodeAlign
virtual bool UseCodeAlign() const =0
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
llvm::iplist_impl< simple_ilist< MCFragment, Options... >, ilist_traits< MCFragment > >::const_iterator
base_list_type::const_iterator const_iterator
Definition: ilist.h:178
llvm::MCSection::SV_Wasm
@ SV_Wasm
Definition: MCSection.h:48
llvm::MCSection::getOrdinal
unsigned getOrdinal() const
Definition: MCSection.h:141
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::ISD::MCSymbol
@ MCSymbol
Definition: ISDOpcodes.h:172
llvm::MCSection::setOrdinal
void setOrdinal(unsigned Value)
Definition: MCSection.h:142