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