clang 20.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"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/Expr.h"
21#include "llvm/ADT/PointerUnion.h"
22#include "llvm/Support/raw_ostream.h"
23
24namespace clang {
25namespace interp {
26class Block;
27class DeadBlock;
28class InterpState;
29class Pointer;
30enum PrimType : unsigned;
31
32/// A memory block, either on the stack or in the heap.
33///
34/// The storage described by the block is immediately followed by
35/// optional metadata, which is followed by the actual data.
36///
37/// Block* rawData() data()
38/// │ │ │
39/// │ │ │
40/// ▼ ▼ ▼
41/// ┌───────────────┬─────────────────────────┬─────────────────┐
42/// │ Block │ Metadata │ Data │
43/// │ sizeof(Block) │ Desc->getMetadataSize() │ Desc->getSize() │
44/// └───────────────┴─────────────────────────┴─────────────────┘
45///
46/// Desc->getAllocSize() describes the size after the Block, i.e.
47/// the data size and the metadata size.
48///
49class Block final {
50public:
51 /// Creates a new block.
52 Block(unsigned EvalID, const std::optional<unsigned> &DeclID,
53 const Descriptor *Desc, bool IsStatic = false, bool IsExtern = false)
54 : EvalID(EvalID), DeclID(DeclID), IsStatic(IsStatic), IsExtern(IsExtern),
55 IsDynamic(false), Desc(Desc) {
56 assert(Desc);
57 }
58
59 Block(unsigned EvalID, const Descriptor *Desc, bool IsStatic = false,
60 bool IsExtern = false)
61 : EvalID(EvalID), DeclID((unsigned)-1), IsStatic(IsStatic),
62 IsExtern(IsExtern), IsDynamic(false), Desc(Desc) {
63 assert(Desc);
64 }
65
66 /// Returns the block's descriptor.
67 const Descriptor *getDescriptor() const { return Desc; }
68 /// Checks if the block has any live pointers.
69 bool hasPointers() const { return Pointers; }
70 /// Checks if the block is extern.
71 bool isExtern() const { return IsExtern; }
72 /// Checks if the block has static storage duration.
73 bool isStatic() const { return IsStatic; }
74 /// Checks if the block is temporary.
75 bool isTemporary() const { return Desc->IsTemporary; }
76 bool isDynamic() const { return IsDynamic; }
77 /// Returns the size of the block.
78 unsigned getSize() const { return Desc->getAllocSize(); }
79 /// Returns the declaration ID.
80 std::optional<unsigned> getDeclID() const { return DeclID; }
81 /// Returns whether the data of this block has been initialized via
82 /// invoking the Ctor func.
83 bool isInitialized() const { return IsInitialized; }
84 /// The Evaluation ID this block was created in.
85 unsigned getEvalID() const { return EvalID; }
86
87 /// Returns a pointer to the stored data.
88 /// You are allowed to read Desc->getSize() bytes from this address.
89 std::byte *data() {
90 // rawData might contain metadata as well.
91 size_t DataOffset = Desc->getMetadataSize();
92 return rawData() + DataOffset;
93 }
94 const std::byte *data() const {
95 // rawData might contain metadata as well.
96 size_t DataOffset = Desc->getMetadataSize();
97 return rawData() + DataOffset;
98 }
99
100 /// Returns a pointer to the raw data, including metadata.
101 /// You are allowed to read Desc->getAllocSize() bytes from this address.
102 std::byte *rawData() {
103 return reinterpret_cast<std::byte *>(this) + sizeof(Block);
104 }
105 const std::byte *rawData() const {
106 return reinterpret_cast<const std::byte *>(this) + sizeof(Block);
107 }
108
109 /// Invokes the constructor.
110 void invokeCtor() {
111 assert(!IsInitialized);
112 std::memset(rawData(), 0, Desc->getAllocSize());
113 if (Desc->CtorFn) {
114 Desc->CtorFn(this, data(), Desc->IsConst, Desc->IsMutable,
115 /*isActive=*/true, /*InUnion=*/false, Desc);
116 }
117 IsInitialized = true;
118 }
119
120 /// Invokes the Destructor.
121 void invokeDtor() {
122 assert(IsInitialized);
123 if (Desc->DtorFn)
124 Desc->DtorFn(this, data(), Desc);
125 IsInitialized = false;
126 }
127
128 void dump() const { dump(llvm::errs()); }
129 void dump(llvm::raw_ostream &OS) const;
130
131private:
132 friend class Pointer;
133 friend class DeadBlock;
134 friend class InterpState;
135 friend class DynamicAllocator;
136
137 Block(unsigned EvalID, const Descriptor *Desc, bool IsExtern, bool IsStatic,
138 bool IsDead)
139 : EvalID(EvalID), IsStatic(IsStatic), IsExtern(IsExtern), IsDead(true),
140 IsDynamic(false), Desc(Desc) {
141 assert(Desc);
142 }
143
144 /// Deletes a dead block at the end of its lifetime.
145 void cleanup();
146
147 /// Pointer chain management.
148 void addPointer(Pointer *P);
149 void removePointer(Pointer *P);
150 void replacePointer(Pointer *Old, Pointer *New);
151#ifndef NDEBUG
152 bool hasPointer(const Pointer *P) const;
153#endif
154
155 const unsigned EvalID = ~0u;
156 /// Start of the chain of pointers.
157 Pointer *Pointers = nullptr;
158 /// Unique identifier of the declaration.
159 std::optional<unsigned> DeclID;
160 /// Flag indicating if the block has static storage duration.
161 bool IsStatic = false;
162 /// Flag indicating if the block is an extern.
163 bool IsExtern = false;
164 /// Flag indicating if the pointer is dead. This is only ever
165 /// set once, when converting the Block to a DeadBlock.
166 bool IsDead = false;
167 /// Flag indicating if the block contents have been initialized
168 /// via invokeCtor.
169 bool IsInitialized = false;
170 /// Flag indicating if this block has been allocated via dynamic
171 /// memory allocation (e.g. malloc).
172 bool IsDynamic = false;
173 /// Pointer to the stack slot descriptor.
174 const Descriptor *Desc;
175};
176
177/// Descriptor for a dead block.
178///
179/// Dead blocks are chained in a double-linked list to deallocate them
180/// whenever pointers become dead.
181class DeadBlock final {
182public:
183 /// Copies the block.
184 DeadBlock(DeadBlock *&Root, Block *Blk);
185
186 /// Returns a pointer to the stored data.
187 std::byte *data() { return B.data(); }
188 std::byte *rawData() { return B.rawData(); }
189
190private:
191 friend class Block;
192 friend class InterpState;
193
194 void free();
195
196 /// Root pointer of the list.
197 DeadBlock *&Root;
198 /// Previous block in the list.
199 DeadBlock *Prev;
200 /// Next block in the list.
201 DeadBlock *Next;
202
203 /// Actual block storing data and tracking pointers.
204 Block B;
205};
206
207} // namespace interp
208} // namespace clang
209
210#endif
StringRef P
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
bool IsStatic
Definition: Format.cpp:3026
const CFGBlock * Block
Definition: HTMLLogger.cpp:153
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
unsigned getSize() const
Returns the size of the block.
Definition: InterpBlock.h:78
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:121
bool isExtern() const
Checks if the block is extern.
Definition: InterpBlock.h:71
std::byte * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:89
Block(unsigned EvalID, const Descriptor *Desc, bool IsStatic=false, bool IsExtern=false)
Definition: InterpBlock.h:59
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition: InterpBlock.h:67
const std::byte * rawData() const
Definition: InterpBlock.h:105
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:110
bool isStatic() const
Checks if the block has static storage duration.
Definition: InterpBlock.h:73
Block(unsigned EvalID, const std::optional< unsigned > &DeclID, const Descriptor *Desc, bool IsStatic=false, bool IsExtern=false)
Creates a new block.
Definition: InterpBlock.h:52
bool isTemporary() const
Checks if the block is temporary.
Definition: InterpBlock.h:75
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:102
const std::byte * data() const
Definition: InterpBlock.h:94
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition: InterpBlock.h:83
bool isDynamic() const
Definition: InterpBlock.h:76
void dump() const
Definition: InterpBlock.h:128
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition: InterpBlock.h:85
std::optional< unsigned > getDeclID() const
Returns the declaration ID.
Definition: InterpBlock.h:80
bool hasPointers() const
Checks if the block has any live pointers.
Definition: InterpBlock.h:69
Descriptor for a dead block.
Definition: InterpBlock.h:181
std::byte * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:187
Manages dynamic memory allocations done during bytecode interpretation.
Interpreter context.
Definition: InterpState.h:36
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:33
The JSON file list parser is used to communicate input to InstallAPI.
#define true
Definition: stdbool.h:25
#define false
Definition: stdbool.h:26
Describes a memory block created by an allocation site.
Definition: Descriptor.h:111
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:149
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:230
const BlockDtorFn DtorFn
Definition: Descriptor.h:161
const BlockCtorFn CtorFn
Storage management methods.
Definition: Descriptor.h:160
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:234
const bool IsMutable
Flag indicating if a field is mutable.
Definition: Descriptor.h:151
const bool IsTemporary
Flag indicating if the block is a temporary.
Definition: Descriptor.h:153