clang 20.0.0git
EvalEmitter.cpp
Go to the documentation of this file.
1//===--- EvalEmitter.cpp - Instruction emitter for the VM -------*- 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#include "EvalEmitter.h"
10#include "Context.h"
11#include "IntegralAP.h"
12#include "Interp.h"
13#include "Opcode.h"
14#include "clang/AST/DeclCXX.h"
15
16using namespace clang;
17using namespace clang::interp;
18
20 InterpStack &Stk)
21 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {
22 // Create a dummy frame for the interpreter which does not have locals.
23 S.Current =
24 new InterpFrame(S, /*Func=*/nullptr, /*Caller=*/nullptr, CodePtr(), 0);
25}
26
28 for (auto &[K, V] : Locals) {
29 Block *B = reinterpret_cast<Block *>(V.get());
30 if (B->isInitialized())
31 B->invokeDtor();
32 }
33}
34
35/// Clean up all our resources. This needs to done in failed evaluations before
36/// we call InterpStack::clear(), because there might be a Pointer on the stack
37/// pointing into a Block in the EvalEmitter.
39
41 bool ConvertResultToRValue) {
43 this->ConvertResultToRValue = ConvertResultToRValue && !isa<ConstantExpr>(E);
44 this->CheckFullyInitialized = isa<ConstantExpr>(E);
45 EvalResult.setSource(E);
46
47 if (!this->visitExpr(E)) {
48 // EvalResult may already have a result set, but something failed
49 // after that (e.g. evaluating destructors).
50 EvalResult.setInvalid();
51 }
52
53 return std::move(this->EvalResult);
54}
55
58 this->CheckFullyInitialized = CheckFullyInitialized;
59 S.EvaluatingDecl = VD;
60 EvalResult.setSource(VD);
61
62 if (const Expr *Init = VD->getAnyInitializer()) {
63 QualType T = VD->getType();
64 this->ConvertResultToRValue = !Init->isGLValue() && !T->isPointerType() &&
66 } else
67 this->ConvertResultToRValue = false;
68
69 EvalResult.setSource(VD);
70
71 if (!this->visitDeclAndReturn(VD, S.inConstantContext()))
72 EvalResult.setInvalid();
73
74 S.EvaluatingDecl = nullptr;
75 updateGlobalTemporaries();
76 return std::move(this->EvalResult);
77}
78
79void EvalEmitter::emitLabel(LabelTy Label) { CurrentLabel = Label; }
80
82
84 // Allocate memory for a local.
85 auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
86 auto *B = new (Memory.get()) Block(Ctx.getEvalID(), D, /*isStatic=*/false);
87 B->invokeCtor();
88
89 // Initialize local variable inline descriptor.
90 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
91 Desc.Desc = D;
92 Desc.Offset = sizeof(InlineDescriptor);
93 Desc.IsActive = true;
94 Desc.IsBase = false;
95 Desc.IsFieldMutable = false;
96 Desc.IsConst = false;
97 Desc.IsInitialized = false;
98
99 // Register the local.
100 unsigned Off = Locals.size();
101 Locals.insert({Off, std::move(Memory)});
102 return {Off, D};
103}
104
106 if (isActive()) {
107 if (S.Stk.pop<bool>())
108 ActiveLabel = Label;
109 }
110 return true;
111}
112
114 if (isActive()) {
115 if (!S.Stk.pop<bool>())
116 ActiveLabel = Label;
117 }
118 return true;
119}
120
122 if (isActive())
123 CurrentLabel = ActiveLabel = Label;
124 return true;
125}
126
128 if (isActive())
129 ActiveLabel = Label;
130 CurrentLabel = Label;
131 return true;
132}
133
135 return S.maybeDiagnoseDanglingAllocations();
136}
137
138template <PrimType OpType> bool EvalEmitter::emitRet(const SourceInfo &Info) {
139 if (!isActive())
140 return true;
141
142 if (!checkReturnState(S))
143 return false;
144
145 using T = typename PrimConv<OpType>::T;
146 EvalResult.setValue(S.Stk.pop<T>().toAPValue(Ctx.getASTContext()));
147 return true;
148}
149
150template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
151 if (!isActive())
152 return true;
153
154 const Pointer &Ptr = S.Stk.pop<Pointer>();
155
156 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
157 return false;
158 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
159 return false;
160
161 if (!checkReturnState(S))
162 return false;
163
164 // Implicitly convert lvalue to rvalue, if requested.
165 if (ConvertResultToRValue) {
166 if (!Ptr.isZero() && !Ptr.isDereferencable())
167 return false;
168
169 if (S.getLangOpts().CPlusPlus11 && Ptr.isBlockPointer() &&
170 !CheckFinalLoad(S, OpPC, Ptr)) {
171 return false;
172 }
173
174 // Never allow reading from a non-const pointer, unless the memory
175 // has been created in this evaluation.
176 if (!Ptr.isZero() && !Ptr.isConst() && Ptr.isBlockPointer() &&
177 Ptr.block()->getEvalID() != Ctx.getEvalID())
178 return false;
179
180 if (std::optional<APValue> V =
181 Ptr.toRValue(Ctx, EvalResult.getSourceType())) {
182 EvalResult.setValue(*V);
183 } else {
184 return false;
185 }
186 } else {
187 EvalResult.setValue(Ptr.toAPValue(Ctx.getASTContext()));
188 }
189
190 return true;
191}
192template <> bool EvalEmitter::emitRet<PT_FnPtr>(const SourceInfo &Info) {
193 if (!isActive())
194 return true;
195
196 if (!checkReturnState(S))
197 return false;
198 // Function pointers cannot be converted to rvalues.
199 EvalResult.setFunctionPointer(S.Stk.pop<FunctionPointer>());
200 return true;
201}
202
203bool EvalEmitter::emitRetVoid(const SourceInfo &Info) {
204 if (!checkReturnState(S))
205 return false;
206 EvalResult.setValid();
207 return true;
208}
209
210bool EvalEmitter::emitRetValue(const SourceInfo &Info) {
211 const auto &Ptr = S.Stk.pop<Pointer>();
212
213 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
214 return false;
215 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
216 return false;
217
218 if (!checkReturnState(S))
219 return false;
220
221 if (std::optional<APValue> APV =
222 Ptr.toRValue(S.getCtx(), EvalResult.getSourceType())) {
223 EvalResult.setValue(*APV);
224 return true;
225 }
226
227 EvalResult.setInvalid();
228 return false;
229}
230
231bool EvalEmitter::emitGetPtrLocal(uint32_t I, const SourceInfo &Info) {
232 if (!isActive())
233 return true;
234
235 Block *B = getLocal(I);
236 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
237 return true;
238}
239
240template <PrimType OpType>
241bool EvalEmitter::emitGetLocal(uint32_t I, const SourceInfo &Info) {
242 if (!isActive())
243 return true;
244
245 using T = typename PrimConv<OpType>::T;
246
247 Block *B = getLocal(I);
248 S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
249 return true;
250}
251
252template <PrimType OpType>
253bool EvalEmitter::emitSetLocal(uint32_t I, const SourceInfo &Info) {
254 if (!isActive())
255 return true;
256
257 using T = typename PrimConv<OpType>::T;
258
259 Block *B = getLocal(I);
260 *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
261 InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
262 Desc.IsInitialized = true;
263
264 return true;
265}
266
267bool EvalEmitter::emitDestroy(uint32_t I, const SourceInfo &Info) {
268 if (!isActive())
269 return true;
270
271 for (auto &Local : Descriptors[I]) {
272 Block *B = getLocal(Local.Offset);
273 S.deallocate(B);
274 }
275
276 return true;
277}
278
279/// Global temporaries (LifetimeExtendedTemporary) carry their value
280/// around as an APValue, which codegen accesses.
281/// We set their value once when creating them, but we don't update it
282/// afterwards when code changes it later.
283/// This is what we do here.
284void EvalEmitter::updateGlobalTemporaries() {
285 for (const auto &[E, Temp] : S.SeenGlobalTemporaries) {
286 if (std::optional<unsigned> GlobalIndex = P.getGlobal(E)) {
287 const Pointer &Ptr = P.getPtrGlobal(*GlobalIndex);
288 APValue *Cached = Temp->getOrCreateValue(true);
289
290 if (std::optional<PrimType> T = Ctx.classify(E->getType())) {
292 *T, { *Cached = Ptr.deref<T>().toAPValue(Ctx.getASTContext()); });
293 } else {
294 if (std::optional<APValue> APV =
295 Ptr.toRValue(Ctx, Temp->getTemporaryExpr()->getType()))
296 *Cached = *APV;
297 }
298 }
299 }
300 S.SeenGlobalTemporaries.clear();
301}
302
303//===----------------------------------------------------------------------===//
304// Opcode evaluators
305//===----------------------------------------------------------------------===//
306
307#define GET_EVAL_IMPL
308#include "Opcodes.inc"
309#undef GET_EVAL_IMPL
#define V(N, I)
Definition: ASTContext.h:3341
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
const Decl * D
Expr * E
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static bool checkReturnState(InterpState &S)
static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, QualType Type, const APValue &Value)
Check that this evaluated value is fully-initialized and can be loaded by an lvalue-to-rvalue convers...
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:148
std::string Label
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
QualType getType() const
Definition: Expr.h:142
A (possibly-)qualified type.
Definition: Type.h:941
bool isPointerType() const
Definition: Type.h:8008
bool isObjCObjectPointerType() const
Definition: Type.h:8150
QualType getType() const
Definition: Decl.h:678
Represents a variable declaration or definition.
Definition: Decl.h:879
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition: Decl.h:1306
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:121
std::byte * data()
Returns a pointer to the stored data.
Definition: InterpBlock.h:89
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:110
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:102
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition: InterpBlock.h:83
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition: InterpBlock.h:85
Pointer into the code segment.
Definition: Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:61
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:130
unsigned getEvalID() const
Definition: Context.h:112
bool jump(const LabelTy &Label)
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false)
Definition: EvalEmitter.cpp:40
EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized)
Definition: EvalEmitter.cpp:56
virtual bool visitExpr(const Expr *E)=0
Methods implemented by the compiler.
bool jumpFalse(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for registering a local.
Definition: EvalEmitter.cpp:83
void emitLabel(LabelTy Label)
Define a label.
Definition: EvalEmitter.cpp:79
bool isActive() const
Since expressions can only jump forward, predicated execution is used to deal with if-else statements...
Definition: EvalEmitter.h:69
bool fallthrough(const LabelTy &Label)
void cleanup()
Clean up all resources.
Definition: EvalEmitter.cpp:38
LabelTy getLabel()
Create a label.
Definition: EvalEmitter.cpp:81
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk)
Definition: EvalEmitter.cpp:19
virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
Definition: EvalEmitter.h:86
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Defines the result of an evaluation.
bool checkReturnValue(InterpState &S, const Context &Ctx, const Pointer &Ptr, const SourceInfo &Info)
Check that none of the blocks the given pointer (transitively) points to are dynamically allocated.
bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const
Check that all subobjects of the given pointer have been initialized.
Frame storing local variables.
Definition: InterpFrame.h:26
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:27
T pop()
Returns the value from the top of the stack and removes it.
Definition: InterpStack.h:43
void push(Tys &&...Args)
Constructs a value in place on the top of the stack.
Definition: InterpStack.h:35
Interpreter context.
Definition: InterpState.h:36
llvm::SmallVector< std::pair< const Expr *, const LifetimeExtendedTemporaryDecl * > > SeenGlobalTemporaries
Definition: InterpState.h:140
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:128
bool inConstantContext() const
Definition: InterpState.h:77
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
Definition: InterpState.h:136
InterpFrame * Current
The current frame.
Definition: InterpState.h:132
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:60
ASTContext & getCtx() const override
Definition: InterpState.h:62
void setEvalLocation(SourceLocation SL)
Definition: InterpState.h:104
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
bool isConst() const
Checks if an object or a subfield is mutable.
Definition: Pointer.h:538
T & deref() const
Dereferences the pointer, if it's live.
Definition: Pointer.h:625
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:260
APValue toAPValue(const ASTContext &ASTCtx) const
Converts the pointer to an APValue.
Definition: Pointer.cpp:140
bool isDereferencable() const
Whether this block can be read from at all.
Definition: Pointer.h:651
bool isBlockPointer() const
Definition: Pointer.h:456
std::optional< APValue > toRValue(const Context &Ctx, QualType ResultType) const
Converts the pointer to an APValue that is an rvalue.
Definition: Pointer.cpp:466
const Block * block() const
Definition: Pointer.h:569
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Block * getGlobal(unsigned Idx)
Returns the value of a global.
Definition: Program.h:73
Pointer getPtrGlobal(unsigned Idx) const
Returns a pointer to a global.
Definition: Program.cpp:104
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
Interface for the VM to interact with the AST walker's context.
Definition: State.h:56
const LangOptions & getLangOpts() const
Definition: State.cpp:116
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition: Interp.cpp:564
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:111
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:69
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:91
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:85
const Descriptor * Desc
Definition: Descriptor.h:99
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:71
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:82
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:76
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:97
Mapping from primitive types to their representation.
Definition: PrimType.h:75
Information about a local's storage.
Definition: Function.h:39
unsigned Offset
Offset of the local in frame.
Definition: Function.h:41