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