clang 22.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 "clang/AST/DeclCXX.h"
14
15using namespace clang;
16using namespace clang::interp;
17
19 InterpStack &Stk)
20 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {}
21
23 for (auto &V : Locals) {
24 Block *B = reinterpret_cast<Block *>(V.get());
25 if (B->isInitialized())
26 B->invokeDtor();
27 }
28}
29
30/// Clean up all our resources. This needs to done in failed evaluations before
31/// we call InterpStack::clear(), because there might be a Pointer on the stack
32/// pointing into a Block in the EvalEmitter.
33void EvalEmitter::cleanup() { S.cleanup(); }
34
36 bool ConvertResultToRValue,
37 bool DestroyToplevelScope) {
38 S.setEvalLocation(E->getExprLoc());
39 this->ConvertResultToRValue = ConvertResultToRValue && !isa<ConstantExpr>(E);
40 this->CheckFullyInitialized = isa<ConstantExpr>(E);
41 EvalResult.setSource(E);
42
43 if (!this->visitExpr(E, DestroyToplevelScope)) {
44 // EvalResult may already have a result set, but something failed
45 // after that (e.g. evaluating destructors).
46 EvalResult.setInvalid();
47 }
48
49 return std::move(this->EvalResult);
50}
51
53 bool CheckFullyInitialized) {
54 assert(VD);
55 assert(Init);
56 this->CheckFullyInitialized = CheckFullyInitialized;
57 S.EvaluatingDecl = VD;
58 S.setEvalLocation(VD->getLocation());
59 EvalResult.setSource(VD);
60
61 QualType T = VD->getType();
62 this->ConvertResultToRValue = !Init->isGLValue() && !T->isPointerType() &&
63 !T->isObjCObjectPointerType();
64 EvalResult.setSource(VD);
65
66 if (!this->visitDeclAndReturn(VD, Init, S.inConstantContext()))
67 EvalResult.setInvalid();
68
69 S.EvaluatingDecl = nullptr;
70 updateGlobalTemporaries();
71 return std::move(this->EvalResult);
72}
73
75 PtrCallback PtrCB) {
76
77 S.setEvalLocation(E->getExprLoc());
78 this->ConvertResultToRValue = false;
79 this->CheckFullyInitialized = false;
80 this->PtrCB = PtrCB;
81 EvalResult.setSource(E);
82
83 if (!this->visitExpr(E, /*DestroyToplevelScope=*/true)) {
84 // EvalResult may already have a result set, but something failed
85 // after that (e.g. evaluating destructors).
86 EvalResult.setInvalid();
87 }
88
89 return std::move(this->EvalResult);
90}
91
92bool EvalEmitter::interpretCall(const FunctionDecl *FD, const Expr *E) {
93 // Add parameters to the parameter map. The values in the ParamOffset don't
94 // matter in this case as reading from them can't ever work.
95 for (const ParmVarDecl *PD : FD->parameters()) {
96 this->Params.insert({PD, {0, false}});
97 }
98
99 return this->visitExpr(E, /*DestroyToplevelScope=*/false);
100}
101
102void EvalEmitter::emitLabel(LabelTy Label) { CurrentLabel = Label; }
103
105
107 // Allocate memory for a local.
108 auto Memory = std::make_unique<char[]>(sizeof(Block) + D->getAllocSize());
109 auto *B = new (Memory.get()) Block(Ctx.getEvalID(), D, /*isStatic=*/false);
110 B->invokeCtor();
111
112 // Initialize local variable inline descriptor.
113 auto &Desc = B->getBlockDesc<InlineDescriptor>();
114 Desc.Desc = D;
115 Desc.Offset = sizeof(InlineDescriptor);
116 Desc.IsActive = false;
117 Desc.IsBase = false;
118 Desc.IsFieldMutable = false;
119 Desc.IsConst = false;
120 Desc.IsInitialized = false;
121
122 // Register the local.
123 unsigned Off = Locals.size();
124 Locals.push_back(std::move(Memory));
125 return {Off, D};
126}
127
128bool EvalEmitter::jumpTrue(const LabelTy &Label) {
129 if (isActive()) {
130 if (S.Stk.pop<bool>())
131 ActiveLabel = Label;
132 }
133 return true;
134}
135
136bool EvalEmitter::jumpFalse(const LabelTy &Label) {
137 if (isActive()) {
138 if (!S.Stk.pop<bool>())
139 ActiveLabel = Label;
140 }
141 return true;
142}
143
144bool EvalEmitter::jump(const LabelTy &Label) {
145 if (isActive())
146 CurrentLabel = ActiveLabel = Label;
147 return true;
148}
149
151 if (isActive())
152 ActiveLabel = Label;
153 CurrentLabel = Label;
154 return true;
155}
156
157bool EvalEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
158 if (!isActive())
159 return true;
160 size_t StackSizeBefore = S.Stk.size();
161 const Expr *Arg = E->getArg(0);
162 if (!this->visit(Arg)) {
163 S.Stk.clearTo(StackSizeBefore);
164
165 if (S.inConstantContext() || Arg->HasSideEffects(S.getASTContext()))
166 return this->emitBool(false, E);
167 return Invalid(S, OpPC);
168 }
169
170 PrimType T = Ctx.classify(Arg->getType()).value_or(PT_Ptr);
171 if (T == PT_Ptr) {
172 const auto &Ptr = S.Stk.pop<Pointer>();
173 return this->emitBool(CheckBCPResult(S, Ptr), E);
174 }
175
176 // Otherwise, this is fine!
177 if (!this->emitPop(T, E))
178 return false;
179 return this->emitBool(true, E);
180}
181
182template <PrimType OpType> bool EvalEmitter::emitRet(SourceInfo Info) {
183 if (!isActive())
184 return true;
185
186 using T = typename PrimConv<OpType>::T;
187 EvalResult.takeValue(S.Stk.pop<T>().toAPValue(Ctx.getASTContext()));
188 return true;
189}
190
191template <> bool EvalEmitter::emitRet<PT_Ptr>(SourceInfo Info) {
192 if (!isActive())
193 return true;
194
195 const Pointer &Ptr = S.Stk.pop<Pointer>();
196
197 if (Ptr.isFunctionPointer()) {
198 EvalResult.takeValue(Ptr.toAPValue(Ctx.getASTContext()));
199 return true;
200 }
201
202 // If we're returning a raw pointer, call our callback.
203 if (this->PtrCB)
204 return (*this->PtrCB)(Ptr);
205
206 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
207 return false;
208 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
209 return false;
210
211 // Implicitly convert lvalue to rvalue, if requested.
212 if (ConvertResultToRValue) {
213 if (!Ptr.isZero() && !Ptr.isDereferencable())
214 return false;
215
216 if (Ptr.pointsToStringLiteral() && Ptr.isArrayRoot())
217 return false;
218
219 if (!Ptr.isZero() && !CheckFinalLoad(S, OpPC, Ptr))
220 return false;
221
222 // Never allow reading from a non-const pointer, unless the memory
223 // has been created in this evaluation.
224 if (!Ptr.isZero() && !Ptr.isConst() && Ptr.isBlockPointer() &&
225 Ptr.block()->getEvalID() != Ctx.getEvalID())
226 return false;
227
228 if (std::optional<APValue> V =
229 Ptr.toRValue(Ctx, EvalResult.getSourceType())) {
230 EvalResult.takeValue(std::move(*V));
231 } else {
232 return false;
233 }
234 } else {
235 // If this is pointing to a local variable, just return
236 // the result, even if the pointer is dead.
237 // This will later be diagnosed by CheckLValueConstantExpression.
238 if (Ptr.isBlockPointer() && !Ptr.block()->isStatic()) {
239 EvalResult.takeValue(Ptr.toAPValue(Ctx.getASTContext()));
240 return true;
241 }
242
243 if (!Ptr.isLive() && !Ptr.isTemporary())
244 return false;
245
246 EvalResult.takeValue(Ptr.toAPValue(Ctx.getASTContext()));
247 }
248
249 return true;
250}
251
252bool EvalEmitter::emitRetVoid(SourceInfo Info) {
253 EvalResult.setValid();
254 return true;
255}
256
257bool EvalEmitter::emitRetValue(SourceInfo Info) {
258 const auto &Ptr = S.Stk.pop<Pointer>();
259
260 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
261 return false;
262 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
263 return false;
264
265 if (std::optional<APValue> APV =
266 Ptr.toRValue(S.getASTContext(), EvalResult.getSourceType())) {
267 EvalResult.takeValue(std::move(*APV));
268 return true;
269 }
270
271 EvalResult.setInvalid();
272 return false;
273}
274
275bool EvalEmitter::emitGetPtrLocal(uint32_t I, SourceInfo Info) {
276 if (!isActive())
277 return true;
278
279 Block *B = getLocal(I);
280 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
281 return true;
282}
283
284template <PrimType OpType>
285bool EvalEmitter::emitGetLocal(uint32_t I, SourceInfo Info) {
286 if (!isActive())
287 return true;
288
289 using T = typename PrimConv<OpType>::T;
290
291 Block *B = getLocal(I);
292
293 if (!CheckLocalLoad(S, OpPC, B))
294 return false;
295
296 S.Stk.push<T>(*reinterpret_cast<T *>(B->data()));
297 return true;
298}
299
300template <PrimType OpType>
301bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) {
302 if (!isActive())
303 return true;
304
305 using T = typename PrimConv<OpType>::T;
306
307 Block *B = getLocal(I);
308 *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
309 auto &Desc = B->getBlockDesc<InlineDescriptor>();
310 Desc.IsInitialized = true;
311
312 return true;
313}
314
315bool EvalEmitter::emitDestroy(uint32_t I, SourceInfo Info) {
316 if (!isActive())
317 return true;
318
319 for (auto &Local : Descriptors[I]) {
320 Block *B = getLocal(Local.Offset);
321 S.deallocate(B);
322 }
323
324 return true;
325}
326
327bool EvalEmitter::emitGetLocalEnabled(uint32_t I, SourceInfo Info) {
328 if (!isActive())
329 return true;
330
331 Block *B = getLocal(I);
332 const auto &Desc = B->getBlockDesc<InlineDescriptor>();
333
334 S.Stk.push<bool>(Desc.IsActive);
335 return true;
336}
337
338bool EvalEmitter::emitEnableLocal(uint32_t I, SourceInfo Info) {
339 if (!isActive())
340 return true;
341
342 // FIXME: This is a little dirty, but to avoid adding a flag to
343 // InlineDescriptor that's only ever useful on the toplevel of local
344 // variables, we reuse the IsActive flag for the enabled state. We should
345 // probably use a different struct than InlineDescriptor for the block-level
346 // inline descriptor of local varaibles.
347 Block *B = getLocal(I);
348 auto &Desc = B->getBlockDesc<InlineDescriptor>();
349 Desc.IsActive = true;
350 return true;
351}
352
353/// Global temporaries (LifetimeExtendedTemporary) carry their value
354/// around as an APValue, which codegen accesses.
355/// We set their value once when creating them, but we don't update it
356/// afterwards when code changes it later.
357/// This is what we do here.
358void EvalEmitter::updateGlobalTemporaries() {
359 for (const auto &[E, Temp] : S.SeenGlobalTemporaries) {
360 UnsignedOrNone GlobalIndex = P.getGlobal(E);
361 assert(GlobalIndex);
362 const Pointer &Ptr = P.getPtrGlobal(*GlobalIndex);
363 APValue *Cached = Temp->getOrCreateValue(true);
364 if (OptPrimType T = Ctx.classify(E->getType())) {
365 TYPE_SWITCH(*T,
366 { *Cached = Ptr.deref<T>().toAPValue(Ctx.getASTContext()); });
367 } else {
368 if (std::optional<APValue> APV =
369 Ptr.toRValue(Ctx, Temp->getTemporaryExpr()->getType()))
370 *Cached = *APV;
371 }
372 }
373 S.SeenGlobalTemporaries.clear();
374}
375
376//===----------------------------------------------------------------------===//
377// Opcode evaluators
378//===----------------------------------------------------------------------===//
379
380#define GET_EVAL_IMPL
381#include "Opcodes.inc"
382#undef GET_EVAL_IMPL
#define V(N, I)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
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:211
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2943
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3147
SourceLocation getLocation() const
Definition DeclBase.h:439
This represents one expression.
Definition Expr.h:112
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition Expr.cpp:3669
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:276
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2000
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2774
Represents a parameter to a function.
Definition Decl.h:1790
A (possibly-)qualified type.
Definition TypeBase.h:937
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
void invokeDtor()
Invokes the Destructor.
std::byte * data()
Returns a pointer to the stored data.
bool isStatic() const
Checks if the block has static storage duration.
Definition InterpBlock.h:79
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:92
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition InterpBlock.h:94
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:41
ASTContext & getASTContext() const
Returns the AST context.
Definition Context.h:79
unsigned getEvalID() const
Definition Context.h:147
bool jump(const LabelTy &Label)
EvaluationResult interpretDecl(const VarDecl *VD, const Expr *Init, bool CheckFullyInitialized)
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false, bool DestroyToplevelScope=false)
bool jumpFalse(const LabelTy &Label)
virtual bool visit(const Expr *E)=0
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
Local createLocal(Descriptor *D)
Callback for registering a local.
bool interpretCall(const FunctionDecl *FD, const Expr *E)
Interpret the given expression as if it was in the body of the given function, i.e.
llvm::function_ref< bool(const Pointer &)> PtrCallback
Definition EvalEmitter.h:36
void emitLabel(LabelTy Label)
Define a label.
bool isActive() const
Since expressions can only jump forward, predicated execution is used to deal with if-else statements...
Definition EvalEmitter.h:80
virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope)=0
Methods implemented by the compiler.
bool fallthrough(const LabelTy &Label)
virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext)=0
void cleanup()
Clean up all resources.
LabelTy getLabel()
Create a label.
EvaluationResult interpretAsPointer(const Expr *E, PtrCallback PtrCB)
Interpret the given Expr to a Pointer.
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk)
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
Definition EvalEmitter.h:94
virtual bool emitBool(bool V, const Expr *E)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
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.
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
T pop()
Returns the value from the top of the stack and removes it.
Definition InterpStack.h:39
InterpStack & Stk
Temporary stack.
A pointer to a memory block, live or dead.
Definition Pointer.h:92
bool isConst() const
Checks if an object or a subfield is mutable.
Definition Pointer.h:563
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:669
bool pointsToStringLiteral() const
Definition Pointer.cpp:668
bool isArrayRoot() const
Whether this array refers to an array, but not to the first element.
Definition Pointer.h:400
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:273
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:259
APValue toAPValue(const ASTContext &ASTCtx) const
Converts the pointer to an APValue.
Definition Pointer.cpp:172
bool isDereferencable() const
Whether this block can be read from at all.
Definition Pointer.h:703
bool isBlockPointer() const
Definition Pointer.h:474
std::optional< APValue > toRValue(const Context &Ctx, QualType ResultType) const
Converts the pointer to an APValue that is an rvalue.
Definition Pointer.cpp:721
bool isTemporary() const
Checks if the storage is temporary.
Definition Pointer.h:507
const Block * block() const
Definition Pointer.h:608
bool isFunctionPointer() const
Definition Pointer.h:476
The program contains and links the bytecode for all functions.
Definition Program.h:36
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
Interface for the VM to interact with the AST walker's context.
Definition State.h:79
bool CheckBCPResult(InterpState &S, const Pointer &Ptr)
Definition Interp.cpp:308
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition Interp.cpp:867
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2113
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B)
Definition Interp.cpp:796
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition Descriptor.h:121
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:160
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition Descriptor.h:242
Inline descriptor embedded in structures and arrays.
Definition Descriptor.h:66
Mapping from primitive types to their representation.
Definition PrimType.h:138
Information about a local's storage.
Definition Function.h:39