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