Debian Codegraph @

clang/lib/AST/ByteCode/EvalEmitter.cpp

raw
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
15 using namespace clang;
16 using namespace clang::interp;
17
18 EvalEmitter::EvalEmitter(Context &Ctx, Program &P, State &Parent,
19 InterpStack &Stk)
20 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {}
21
22 EvalEmitter::~EvalEmitter() {
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.
33 void EvalEmitter::cleanup() { S.cleanup(); }
34
35 EvaluationResult EvalEmitter::interpretExpr(const Expr *E,
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
52 EvaluationResult 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 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
74 EvaluationResult EvalEmitter::interpretAsPointer(const Expr *E,
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
92 bool 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
102 void EvalEmitter::emitLabel(LabelTy Label) { CurrentLabel = Label; }
103
104 EvalEmitter::LabelTy EvalEmitter::getLabel() { return NextLabel++; }
105
106 Scope::Local EvalEmitter::createLocal(Descriptor *D) {
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
128 bool EvalEmitter::jumpTrue(const LabelTy &Label) {
129 if (isActive()) {
130 if (S.Stk.pop<bool>())
131 ActiveLabel = Label;
132 }
133 return true;
134 }
135
136 bool EvalEmitter::jumpFalse(const LabelTy &Label) {
137 if (isActive()) {
138 if (!S.Stk.pop<bool>())
139 ActiveLabel = Label;
140 }
141 return true;
142 }
143
144 bool EvalEmitter::jump(const LabelTy &Label) {
145 if (isActive())
146 CurrentLabel = ActiveLabel = Label;
147 return true;
148 }
149
150 bool EvalEmitter::fallthrough(const LabelTy &Label) {
151 if (isActive())
152 ActiveLabel = Label;
153 CurrentLabel = Label;
154 return true;
155 }
156
157 bool 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
182 template <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
191 template <> 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
252 bool EvalEmitter::emitRetVoid(SourceInfo Info) {
253 EvalResult.setValid();
254 return true;
255 }
256
257 bool 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
275 bool 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
284 template <PrimType OpType>
285 bool 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
300 template <PrimType OpType>
301 bool 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
315 bool 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
327 bool 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
338 bool 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.
358 void 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