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::interpretAsPointer(const Expr *E,
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(KV: {PD, {.Index: 0, .IsPtr: false}});
99 }
100
101 return this->visitExpr(E, /*DestroyToplevelScope=*/false);
102}
103
104void EvalEmitter::emitLabel(LabelTy Label) { CurrentLabel = Label; }
105
106EvalEmitter::LabelTy EvalEmitter::getLabel() { return NextLabel++; }
107
108Scope::Local EvalEmitter::createLocal(Descriptor *D) {
109 // Allocate memory for a local.
110 auto Memory = std::make_unique<char[]>(num: 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(Elt: std::move(Memory));
127 return {.Offset: Off, .Desc: D};
128}
129
130bool EvalEmitter::jumpTrue(const LabelTy &Label, SourceInfo SI) {
131 if (isActive()) {
132 CurrentSource = SI;
133 if (S.Stk.pop<bool>())
134 ActiveLabel = Label;
135 }
136 return true;
137}
138
139bool EvalEmitter::jumpFalse(const LabelTy &Label, SourceInfo SI) {
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
156bool EvalEmitter::fallthrough(const LabelTy &Label) {
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(Arg: 0);
172 if (!this->visit(E: Arg)) {
173 S.Stk.clearTo(NewSize: StackSizeBefore);
174
175 if (S.inConstantContext() || Arg->HasSideEffects(Ctx: S.getASTContext()))
176 return this->emitBool(V: false, E);
177 return Invalid(S, OpPC);
178 }
179
180 PrimType T = Ctx.classify(T: Arg->getType()).value_or(PT: PT_Ptr);
181 if (T == PT_Ptr) {
182 const auto &Ptr = S.Stk.pop<Pointer>();
183 return this->emitBool(V: CheckBCPResult(S, Ptr), E);
184 }
185
186 // Otherwise, this is fine!
187 if (!this->emitPop(T, I: E))
188 return false;
189 return this->emitBool(V: 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(V: 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(V: Ptr.toAPValue(ASTCtx: 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, ResultType: EvalResult.getSourceType())) {
240 EvalResult.takeValue(V: 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(V: Ptr.toAPValue(ASTCtx: Ctx.getASTContext()));
250 return true;
251 }
252
253 if (!Ptr.isLive() && !Ptr.isTemporary())
254 return false;
255
256 EvalResult.takeValue(V: Ptr.toAPValue(ASTCtx: 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(Ctx: S.getASTContext(), ResultType: EvalResult.getSourceType())) {
277 EvalResult.takeValue(V: 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(Index: I);
290 S.Stk.push<Pointer>(Args&: B, Args: 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(Index: 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(Index: 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(Index: 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(Index: I);
342 const auto &Desc = B->getBlockDesc<InlineDescriptor>();
343
344 S.Stk.push<bool>(Args: 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(Index: 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(Idx: *GlobalIndex);
373 APValue *Cached = Temp->getOrCreateValue(MayCreate: true);
374
375 QualType TempType = E->getType();
376 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Val: E))
377 TempType = MTE->getSubExpr()->skipRValueSubobjectAdjustments()->getType();
378
379 if (OptPrimType T = Ctx.classify(T: TempType)) {
380 TYPE_SWITCH(*T,
381 { *Cached = Ptr.deref<T>().toAPValue(Ctx.getASTContext()); });
382 } else {
383 if (std::optional<APValue> APV = Ptr.toRValue(Ctx, ResultType: 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
397