clang 22.0.0git
InterpBlock.h
Go to the documentation of this file.
1//===-- InterpBlock.h - Allocated blocks for the interpreter -*- 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// Defines the classes describing allocated blocks.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_BLOCK_H
14#define LLVM_CLANG_AST_INTERP_BLOCK_H
15
16#include "Descriptor.h"
17#include "llvm/Support/raw_ostream.h"
18
19namespace clang {
20namespace interp {
21class Block;
22class DeadBlock;
23class InterpState;
24class Pointer;
25enum PrimType : uint8_t;
26
27/// A memory block, either on the stack or in the heap.
28///
29/// The storage described by the block is immediately followed by
30/// optional metadata, which is followed by the actual data.
31///
32/// Block* rawData() data()
33/// │ │ │
34/// │ │ │
35/// ▼ ▼ ▼
36/// ┌───────────────┬─────────────────────────┬─────────────────┐
37/// │ Block │ Metadata │ Data │
38/// │ sizeof(Block) │ Desc->getMetadataSize() │ Desc->getSize() │
39/// └───────────────┴─────────────────────────┴─────────────────┘
40///
41/// Desc->getAllocSize() describes the size after the Block, i.e.
42/// the data size and the metadata size.
43///
44class Block final {
45private:
46 static constexpr uint8_t ExternFlag = 1 << 0;
47 static constexpr uint8_t DeadFlag = 1 << 1;
48 static constexpr uint8_t WeakFlag = 1 << 2;
49 static constexpr uint8_t DummyFlag = 1 << 3;
50
51public:
52 /// Creates a new block.
53 Block(unsigned EvalID, UnsignedOrNone DeclID, const Descriptor *Desc,
54 bool IsStatic = false, bool IsExtern = false, bool IsWeak = false,
55 bool IsDummy = false)
56 : Desc(Desc), DeclID(DeclID), EvalID(EvalID), IsStatic(IsStatic) {
57 assert(Desc);
58 AccessFlags |= (ExternFlag * IsExtern);
59 AccessFlags |= (WeakFlag * IsWeak);
60 AccessFlags |= (DummyFlag * IsDummy);
61 }
62
63 Block(unsigned EvalID, const Descriptor *Desc, bool IsStatic = false,
64 bool IsExtern = false, bool IsWeak = false, bool IsDummy = false)
65 : Desc(Desc), EvalID(EvalID), IsStatic(IsStatic) {
66 assert(Desc);
67 AccessFlags |= (ExternFlag * IsExtern);
68 AccessFlags |= (WeakFlag * IsWeak);
69 AccessFlags |= (DummyFlag * IsDummy);
70 }
71
72 /// Returns the block's descriptor.
73 const Descriptor *getDescriptor() const { return Desc; }
74 /// Checks if the block has any live pointers.
75 bool hasPointers() const { return Pointers; }
76 /// Checks if the block is extern.
77 bool isExtern() const { return AccessFlags & ExternFlag; }
78 /// Checks if the block has static storage duration.
79 bool isStatic() const { return IsStatic; }
80 /// Checks if the block is temporary.
81 bool isTemporary() const { return Desc->IsTemporary; }
82 bool isWeak() const { return AccessFlags & WeakFlag; }
83 bool isDynamic() const { return (DynAllocId != std::nullopt); }
84 bool isDummy() const { return AccessFlags & DummyFlag; }
85 bool isDead() const { return AccessFlags & DeadFlag; }
86 /// Returns the size of the block.
87 unsigned getSize() const { return Desc->getAllocSize(); }
88 /// Returns the declaration ID.
89 UnsignedOrNone getDeclID() const { return DeclID; }
90 /// Returns whether the data of this block has been initialized via
91 /// invoking the Ctor func.
92 bool isInitialized() const { return IsInitialized; }
93 /// The Evaluation ID this block was created in.
94 unsigned getEvalID() const { return EvalID; }
95 /// Move all pointers from this block to \param B.
96 void movePointersTo(Block *B);
97
98 /// Returns a pointer to the stored data.
99 /// You are allowed to read Desc->getSize() bytes from this address.
100 std::byte *data() {
101 // rawData might contain metadata as well.
102 size_t DataOffset = Desc->getMetadataSize();
103 return rawData() + DataOffset;
104 }
105 const std::byte *data() const {
106 // rawData might contain metadata as well.
107 size_t DataOffset = Desc->getMetadataSize();
108 return rawData() + DataOffset;
109 }
110
111 /// Returns a pointer to the raw data, including metadata.
112 /// You are allowed to read Desc->getAllocSize() bytes from this address.
113 std::byte *rawData() {
114 return reinterpret_cast<std::byte *>(this) + sizeof(Block);
115 }
116 const std::byte *rawData() const {
117 return reinterpret_cast<const std::byte *>(this) + sizeof(Block);
118 }
119
120 template <typename T> const T &deref() const {
121 return *reinterpret_cast<const T *>(data());
122 }
123 template <typename T> T &deref() { return *reinterpret_cast<T *>(data()); }
124
125 template <typename T> T &getBlockDesc() {
126 assert(sizeof(T) == getDescriptor()->getMetadataSize());
127 return *reinterpret_cast<T *>(rawData());
128 }
129 template <typename T> const T &getBlockDesc() const {
130 return const_cast<Block *>(this)->getBlockDesc<T>();
131 }
132
133 /// Invokes the constructor.
134 void invokeCtor() {
135 assert(!IsInitialized);
136 std::memset(rawData(), 0, Desc->getAllocSize());
137 if (Desc->CtorFn) {
138 Desc->CtorFn(this, data(), Desc->IsConst, Desc->IsMutable,
139 Desc->IsVolatile,
140 /*isActive=*/true, /*InUnion=*/false, Desc);
141 }
142 IsInitialized = true;
143 }
144
145 /// Invokes the Destructor.
146 void invokeDtor() {
147 assert(IsInitialized);
148 if (Desc->DtorFn)
149 Desc->DtorFn(this, data(), Desc);
150 IsInitialized = false;
151 }
152
153 void dump() const { dump(llvm::errs()); }
154 void dump(llvm::raw_ostream &OS) const;
155
156 bool isAccessible() const { return AccessFlags == 0; }
157
158private:
159 friend class Pointer;
160 friend class DeadBlock;
161 friend class InterpState;
162 friend class DynamicAllocator;
163 friend class Program;
164
165 Block(unsigned EvalID, const Descriptor *Desc, bool IsExtern, bool IsStatic,
166 bool IsWeak, bool IsDummy, bool IsDead)
167 : Desc(Desc), EvalID(EvalID), IsStatic(IsStatic) {
168 assert(Desc);
169 AccessFlags |= (ExternFlag * IsExtern);
170 AccessFlags |= (DeadFlag * IsDead);
171 AccessFlags |= (WeakFlag * IsWeak);
172 AccessFlags |= (DummyFlag * IsDummy);
173 }
174
175 /// To be called by DynamicAllocator.
176 void setDynAllocId(unsigned ID) { DynAllocId = ID; }
177
178 /// Deletes a dead block at the end of its lifetime.
179 void cleanup();
180
181 /// Pointer chain management.
182 void addPointer(Pointer *P);
183 void removePointer(Pointer *P);
184 void replacePointer(Pointer *Old, Pointer *New);
185#ifndef NDEBUG
186 bool hasPointer(const Pointer *P) const;
187#endif
188
189 /// Pointer to the stack slot descriptor.
190 const Descriptor *Desc;
191 /// Start of the chain of pointers.
192 Pointer *Pointers = nullptr;
193 /// Unique identifier of the declaration.
194 UnsignedOrNone DeclID = std::nullopt;
195 const unsigned EvalID = ~0u;
196 /// Flag indicating if the block has static storage duration.
197 bool IsStatic = false;
198 /// Flag indicating if the block contents have been initialized
199 /// via invokeCtor.
200 bool IsInitialized = false;
201 /// Allocation ID for this dynamic allocation, if it is one.
202 UnsignedOrNone DynAllocId = std::nullopt;
203 /// AccessFlags containing IsExtern, IsDead, IsWeak, and IsDummy bits.
204 uint8_t AccessFlags = 0;
205};
206
207/// Descriptor for a dead block.
208///
209/// Dead blocks are chained in a double-linked list to deallocate them
210/// whenever pointers become dead.
211class DeadBlock final {
212public:
213 /// Copies the block.
214 DeadBlock(DeadBlock *&Root, Block *Blk);
215
216 /// Returns a pointer to the stored data.
217 std::byte *data() { return B.data(); }
218 std::byte *rawData() { return B.rawData(); }
219
220private:
221 friend class Block;
222 friend class InterpState;
223
224 void free();
225
226 /// Root pointer of the list.
227 DeadBlock *&Root;
228 /// Previous block in the list.
229 DeadBlock *Prev;
230 /// Next block in the list.
231 DeadBlock *Next;
232
233 /// Actual block storing data and tracking pointers.
234 Block B;
235};
236
237} // namespace interp
238} // namespace clang
239
240#endif
tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
Clean up any erroneous/redundant code in the given Ranges in Code.
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
unsigned getSize() const
Returns the size of the block.
Definition InterpBlock.h:87
friend class Program
void invokeDtor()
Invokes the Destructor.
const T & deref() const
bool isExtern() const
Checks if the block is extern.
Definition InterpBlock.h:77
friend class Pointer
std::byte * data()
Returns a pointer to the stored data.
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition InterpBlock.h:73
Block(unsigned EvalID, const Descriptor *Desc, bool IsStatic=false, bool IsExtern=false, bool IsWeak=false, bool IsDummy=false)
Definition InterpBlock.h:63
const std::byte * rawData() const
bool isDead() const
Definition InterpBlock.h:85
void movePointersTo(Block *B)
Move all pointers from this block to.
void invokeCtor()
Invokes the constructor.
bool isStatic() const
Checks if the block has static storage duration.
Definition InterpBlock.h:79
const T & getBlockDesc() const
Block(unsigned EvalID, UnsignedOrNone DeclID, const Descriptor *Desc, bool IsStatic=false, bool IsExtern=false, bool IsWeak=false, bool IsDummy=false)
Creates a new block.
Definition InterpBlock.h:53
friend class DynamicAllocator
friend class InterpState
bool isTemporary() const
Checks if the block is temporary.
Definition InterpBlock.h:81
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
const std::byte * data() const
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:92
bool isDynamic() const
Definition InterpBlock.h:83
UnsignedOrNone getDeclID() const
Returns the declaration ID.
Definition InterpBlock.h:89
friend class DeadBlock
bool isDummy() const
Definition InterpBlock.h:84
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition InterpBlock.h:94
bool isWeak() const
Definition InterpBlock.h:82
bool isAccessible() const
bool hasPointers() const
Checks if the block has any live pointers.
Definition InterpBlock.h:75
Descriptor for a dead block.
std::byte * data()
Returns a pointer to the stored data.
DeadBlock(DeadBlock *&Root, Block *Blk)
Copies the block.
Interpreter context.
Definition InterpState.h:43
A pointer to a memory block, live or dead.
Definition Pointer.h:92
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition Descriptor.h:121