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
20EvalEmitter::EvalEmitter(Context &Ctx, Program &P, State &Parent,
21 InterpStack &Stk)
22 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {}
23
24EvalEmitter::~EvalEmitter() {
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
37EvaluationResult EvalEmitter::interpretExpr(const Expr *E,
38 bool ConvertResultToRValue,
39 bool DestroyToplevelScope) {
40 S.setEvalLocation(E->getExprLoc());
41 this->ConvertResultToRValue = ConvertResultToRValue && !isa<ConstantExpr>(Val: E);
42 this->CheckFullyInitialized = isa<ConstantExpr>(Val: 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
54EvaluationResult EvalEmitter::interpretDecl(const VarDecl *VD, const Expr *Init,
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, ConstantContext: S.inConstantContext()))
69 EvalResult.setInvalid();
70
71 S.EvaluatingDecl = nullptr;
72 updateGlobalTemporaries();
73 return std::move(this->EvalResult);
74}
75
76EvaluationResult EvalEmitter::interpretDestructor(const VarDecl *VD,
77 const APValue &Value) {
78 assert(VD);
79 S.setEvalLocation(VD->getLocation());
80 S.EvaluatingDecl = VD;
81 S.EvalKind = EvaluationKind::Dtor;
82 EvalResult.setSource(VD);
83
84 if (!this->visitDtorCall(VD, Value))
85 EvalResult.setInvalid();
86
87 S.EvaluatingDecl = nullptr;
88 return std::move(this->EvalResult);
89}
90
91EvaluationResult EvalEmitter::interpretAsPointer(const Expr *E,
92 PtrCallback PtrCB) {
93 S.setEvalLocation(E->getExprLoc());
94 this->ConvertResultToRValue = false;
95 this->CheckFullyInitialized = false;
96 this->PtrCB = PtrCB;
97 EvalResult.setSource(E);
98
99 if (!this->visitExpr(E, DestroyToplevelScope: true)) {
100 // EvalResult may already have a result set, but something failed
101 // after that (e.g. evaluating destructors).
102 EvalResult.setInvalid();
103 }
104
105 return std::move(this->EvalResult);
106}
107
108EvaluationResult EvalEmitter::interpretAsLValuePointer(const Expr *E,
109 PtrCallback PtrCB) {
110 S.setEvalLocation(E->getExprLoc());
111 this->ConvertResultToRValue = false;
112 this->CheckFullyInitialized = false;
113 this->PtrCB = PtrCB;
114 EvalResult.setSource(E);
115
116 if (!this->visitLValueExpr(E, DestroyToplevelScope: true))
117 EvalResult.setInvalid();
118
119 return std::move(this->EvalResult);
120}
121
122bool EvalEmitter::interpretCall(const FunctionDecl *FD, const Expr *E) {
123 // Add parameters to the parameter map. The values in the ParamOffset don't
124 // matter in this case as reading from them can't ever work.
125 for (const ParmVarDecl *PD : FD->parameters()) {
126 this->Params.insert(KV: {PD, {.Index: 0, .IsPtr: false}});
127 }
128
129 return this->visitExpr(E, /*DestroyToplevelScope=*/false);
130}
131
132void EvalEmitter::emitLabel(LabelTy Label) { CurrentLabel = Label; }
133
134EvalEmitter::LabelTy EvalEmitter::getLabel() { return NextLabel++; }
135
136Scope::Local EvalEmitter::createLocal(Descriptor *D) {
137 // Allocate memory for a local.
138 auto Memory = std::make_unique<char[]>(num: sizeof(Block) + D->getAllocSize());
139 auto *B = new (Memory.get()) Block(Ctx.getEvalID(), D, /*IsStatic=*/false);
140 B->invokeCtorNoMemset();
141
142 // Initialize local variable inline descriptor.
143 auto &Desc = B->getBlockDesc<InlineDescriptor>();
144 Desc.Desc = D;
145 Desc.Offset = sizeof(InlineDescriptor);
146 Desc.IsActive = false;
147 Desc.IsBase = false;
148 Desc.IsFieldMutable = false;
149 Desc.IsConst = false;
150 Desc.IsInitialized = false;
151
152 // Register the local.
153 unsigned Off = Locals.size();
154 Locals.push_back(Elt: std::move(Memory));
155 return {.Offset: Off, .Desc: D};
156}
157
158bool EvalEmitter::jumpTrue(const LabelTy &Label, SourceInfo SI) {
159 if (isActive()) {
160 CurrentSource = SI;
161 if (S.Stk.pop<bool>())
162 ActiveLabel = Label;
163 }
164 return true;
165}
166
167bool EvalEmitter::jumpFalse(const LabelTy &Label, SourceInfo SI) {
168 if (isActive()) {
169 CurrentSource = SI;
170 if (!S.Stk.pop<bool>())
171 ActiveLabel = Label;
172 }
173 return true;
174}
175
176bool EvalEmitter::jump(const LabelTy &Label, SourceInfo SI) {
177 if (isActive()) {
178 CurrentSource = SI;
179 CurrentLabel = ActiveLabel = Label;
180 }
181 return true;
182}
183
184bool EvalEmitter::fallthrough(const LabelTy &Label) {
185 if (isActive())
186 ActiveLabel = Label;
187 CurrentLabel = Label;
188 return true;
189}
190
191bool EvalEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
192 if (!isActive())
193 return true;
194
195 PushIgnoreDiags(S, OpPC);
196 auto _ = llvm::scope_exit([&]() { PopIgnoreDiags(S, OpPC); });
197
198 size_t StackSizeBefore = S.Stk.size();
199 const Expr *Arg = E->getArg(Arg: 0);
200 if (!this->visit(E: Arg)) {
201 S.Stk.clearTo(NewSize: StackSizeBefore);
202
203 if (S.inConstantContext() || Arg->HasSideEffects(Ctx: S.getASTContext()))
204 return this->emitBool(V: false, E);
205 return Invalid(S, OpPC);
206 }
207
208 PrimType T = Ctx.classify(T: Arg->getType()).value_or(PT: PT_Ptr);
209 if (T == PT_Ptr) {
210 const auto &Ptr = S.Stk.pop<Pointer>();
211 return this->emitBool(V: CheckBCPResult(S, Ptr), E);
212 }
213
214 // Otherwise, this is fine!
215 if (!this->emitPop(T, I: E))
216 return false;
217 return this->emitBool(V: true, E);
218}
219
220template <PrimType OpType> bool EvalEmitter::emitRet(SourceInfo Info) {
221 if (!isActive())
222 return true;
223
224 using T = typename PrimConv<OpType>::T;
225 EvalResult.takeValue(V: S.Stk.pop<T>().toAPValue(Ctx.getASTContext()));
226 return true;
227}
228
229template <> bool EvalEmitter::emitRet<PT_Ptr>(SourceInfo Info) {
230 if (!isActive())
231 return true;
232
233 const Pointer &Ptr = S.Stk.pop<Pointer>();
234 // If we're returning a raw pointer, call our callback.
235 if (this->PtrCB)
236 return (*this->PtrCB)(S, OpPC, Ptr);
237
238 if (!EvalResult.checkDynamicAllocations(S, Ctx, Ptr, Info))
239 return false;
240 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
241 return false;
242
243 // Function pointers are always returned as lvalues.
244 if (Ptr.isFunctionPointer()) {
245 EvalResult.takeValue(V: Ptr.toAPValue(ASTCtx: Ctx.getASTContext()));
246 return true;
247 }
248
249 // Implicitly convert lvalue to rvalue, if requested.
250 if (ConvertResultToRValue) {
251 if (Ptr.isPastEnd())
252 return false;
253
254 if (Ptr.pointsToStringLiteral() && Ptr.isArrayRoot())
255 return false;
256
257 if (!Ptr.isZero() && !CheckFinalLoad(S, OpPC, Ptr))
258 return false;
259
260 // Never allow reading from a non-const pointer, unless the memory
261 // has been created in this evaluation.
262 if (!Ptr.isZero() && !Ptr.isConst() && Ptr.isBlockPointer() &&
263 Ptr.block()->getEvalID() != Ctx.getEvalID())
264 return false;
265
266 if (std::optional<APValue> V =
267 Ptr.toRValue(Ctx, ResultType: EvalResult.getSourceType())) {
268 EvalResult.takeValue(V: std::move(*V));
269 } else {
270 return false;
271 }
272 } else {
273 // If this is pointing to a local variable, just return
274 // the result, even if the pointer is dead.
275 // This will later be diagnosed by CheckLValueConstantExpression.
276 if (Ptr.isBlockPointer() && !Ptr.block()->isStatic()) {
277 EvalResult.takeValue(V: Ptr.toAPValue(ASTCtx: Ctx.getASTContext()));
278 return true;
279 }
280
281 if (!Ptr.isLive() && !Ptr.isTemporary())
282 return false;
283
284 // If the variable of this pointer is being evaluated when returning
285 // its value, mark it as constexpr-unknown.
286 APValue V = Ptr.toAPValue(ASTCtx: Ctx.getASTContext());
287 if (const Descriptor *DeclDesc = Ptr.getDeclDesc();
288 DeclDesc && S.EvaluatingDecl &&
289 DeclDesc->asVarDecl() == S.EvaluatingDecl &&
290 S.getLangOpts().CPlusPlus23 &&
291 S.EvaluatingDecl->getType()->isReferenceType()) {
292 V.setConstexprUnknown(true);
293 }
294 EvalResult.takeValue(V: std::move(V));
295 }
296
297 return true;
298}
299
300bool EvalEmitter::emitRetVoid(SourceInfo Info) {
301 EvalResult.setValid();
302 return true;
303}
304
305bool EvalEmitter::emitRetValue(SourceInfo Info) {
306 const auto &Ptr = S.Stk.pop<Pointer>();
307
308 if (!EvalResult.checkDynamicAllocations(S, Ctx, Ptr, Info))
309 return false;
310 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
311 return false;
312
313 if (std::optional<APValue> APV =
314 Ptr.toRValue(Ctx, ResultType: EvalResult.getSourceType())) {
315 EvalResult.takeValue(V: std::move(*APV));
316 return true;
317 }
318
319 EvalResult.setInvalid();
320 return false;
321}
322
323bool EvalEmitter::emitGetPtrLocal(uint32_t I, SourceInfo Info) {
324 if (!isActive())
325 return true;
326
327 Block *B = getLocal(Index: I);
328 S.Stk.push<Pointer>(Args&: B, Args: sizeof(InlineDescriptor));
329 return true;
330}
331
332bool EvalEmitter::emitGetRefLocal(uint32_t I, SourceInfo Info) {
333 if (!isActive())
334 return true;
335
336 Block *B = getLocal(Index: I);
337 return handleReference(S, OpPC, B);
338}
339
340template <PrimType OpType>
341bool EvalEmitter::emitGetLocal(uint32_t I, SourceInfo Info) {
342 if (!isActive())
343 return true;
344
345 using T = typename PrimConv<OpType>::T;
346
347 Block *B = getLocal(Index: I);
348
349 if (!CheckLocalLoad(S, OpPC, B))
350 return false;
351
352 S.Stk.push<T>(B->deref<T>());
353 return true;
354}
355
356template <PrimType OpType>
357bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) {
358 if (!isActive())
359 return true;
360
361 using T = typename PrimConv<OpType>::T;
362
363 Block *B = getLocal(Index: I);
364 B->deref<T>() = S.Stk.pop<T>();
365 auto &Desc = B->getBlockDesc<InlineDescriptor>();
366 Desc.IsInitialized = true;
367 Desc.LifeState = Lifetime::Started;
368
369 return true;
370}
371
372bool EvalEmitter::emitDestroy(uint32_t I, SourceInfo Info) {
373 if (!isActive())
374 return true;
375
376 for (auto &Local : Descriptors[I]) {
377 Block *B = getLocal(Index: Local.Offset);
378 S.deallocate(B);
379 }
380
381 return true;
382}
383
384bool EvalEmitter::emitGetLocalEnabled(uint32_t I, SourceInfo Info) {
385 if (!isActive())
386 return true;
387
388 Block *B = getLocal(Index: I);
389 const auto &Desc = B->getBlockDesc<InlineDescriptor>();
390
391 S.Stk.push<bool>(Args: Desc.IsActive);
392 return true;
393}
394
395bool EvalEmitter::emitEnableLocal(uint32_t I, SourceInfo Info) {
396 if (!isActive())
397 return true;
398
399 // FIXME: This is a little dirty, but to avoid adding a flag to
400 // InlineDescriptor that's only ever useful on the toplevel of local
401 // variables, we reuse the IsActive flag for the enabled state. We should
402 // probably use a different struct than InlineDescriptor for the block-level
403 // inline descriptor of local varaibles.
404 Block *B = getLocal(Index: I);
405 auto &Desc = B->getBlockDesc<InlineDescriptor>();
406 Desc.IsActive = true;
407 return true;
408}
409
410/// Global temporaries (LifetimeExtendedTemporary) carry their value
411/// around as an APValue, which codegen accesses.
412/// We set their value once when creating them, but we don't update it
413/// afterwards when code changes it later.
414/// This is what we do here.
415void EvalEmitter::updateGlobalTemporaries() {
416 for (const auto &[E, Temp] : S.SeenGlobalTemporaries) {
417 UnsignedOrNone GlobalIndex = P.getGlobal(E);
418 assert(GlobalIndex);
419 const Pointer &Ptr = P.getPtrGlobal(Idx: *GlobalIndex);
420 APValue *Cached = Temp->getOrCreateValue(MayCreate: true);
421
422 QualType TempType = E->getType();
423 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Val: E))
424 TempType = MTE->getSubExpr()->skipRValueSubobjectAdjustments()->getType();
425
426 if (OptPrimType T = Ctx.classify(T: TempType)) {
427 TYPE_SWITCH(*T,
428 { *Cached = Ptr.deref<T>().toAPValue(Ctx.getASTContext()); });
429 } else {
430 if (std::optional<APValue> APV = Ptr.toRValue(Ctx, ResultType: TempType))
431 *Cached = *APV;
432 }
433 }
434 S.SeenGlobalTemporaries.clear();
435}
436
437//===----------------------------------------------------------------------===//
438// Opcode evaluators
439//===----------------------------------------------------------------------===//
440
441#define GET_EVAL_IMPL
442#include "Opcodes.inc"
443#undef GET_EVAL_IMPL
444