1//===--- InterpFrame.cpp - Call Frame implementation 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 "InterpFrame.h"
10#include "Boolean.h"
11#include "Floating.h"
12#include "Function.h"
13#include "InterpStack.h"
14#include "InterpState.h"
15#include "Pointer.h"
16#include "PrimType.h"
17#include "Program.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclCXX.h"
20
21using namespace clang;
22using namespace clang::interp;
23
24InterpFrame::InterpFrame(InterpState &S, const Function *Func,
25 InterpFrame *Caller, CodePtr RetPC, unsigned ArgSize)
26 : Caller(Caller), S(S), Depth(Caller ? Caller->Depth + 1 : 0), Func(Func),
27 RetPC(RetPC), ArgSize(ArgSize), Args(static_cast<char *>(S.Stk.top())),
28 FrameOffset(S.Stk.size()) {
29 if (!Func)
30 return;
31
32 unsigned FrameSize = Func->getFrameSize();
33 if (FrameSize == 0)
34 return;
35
36 Locals = std::make_unique<char[]>(num: FrameSize);
37 for (auto &Scope : Func->scopes()) {
38 for (auto &Local : Scope.locals()) {
39 Block *B = new (localBlock(Offset: Local.Offset)) Block(Local.Desc);
40 B->invokeCtor();
41 new (localInlineDesc(Offset: Local.Offset)) InlineDescriptor(Local.Desc);
42 }
43 }
44}
45
46InterpFrame::InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC,
47 unsigned VarArgSize)
48 : InterpFrame(S, Func, S.Current, RetPC, Func->getArgSize() + VarArgSize) {
49 // As per our calling convention, the this pointer is
50 // part of the ArgSize.
51 // If the function has RVO, the RVO pointer is first.
52 // If the fuction has a This pointer, that one is next.
53 // Then follow the actual arguments (but those are handled
54 // in getParamPointer()).
55 if (Func->hasRVO())
56 RVOPtr = stackRef<Pointer>(Offset: 0);
57
58 if (Func->hasThisPointer()) {
59 if (Func->hasRVO())
60 This = stackRef<Pointer>(Offset: sizeof(Pointer));
61 else
62 This = stackRef<Pointer>(Offset: 0);
63 }
64}
65
66InterpFrame::~InterpFrame() {
67 for (auto &Param : Params)
68 S.deallocate(B: reinterpret_cast<Block *>(Param.second.get()));
69
70 // When destroying the InterpFrame, call the Dtor for all block
71 // that haven't been destroyed via a destroy() op yet.
72 // This happens when the execution is interruped midway-through.
73 if (Func) {
74 for (auto &Scope : Func->scopes()) {
75 for (auto &Local : Scope.locals()) {
76 Block *B = localBlock(Offset: Local.Offset);
77 if (B->isInitialized())
78 B->invokeDtor();
79 }
80 }
81 }
82}
83
84void InterpFrame::destroy(unsigned Idx) {
85 for (auto &Local : Func->getScope(Idx).locals()) {
86 S.deallocate(B: localBlock(Offset: Local.Offset));
87 }
88}
89
90void InterpFrame::popArgs() {
91 for (PrimType Ty : Func->args_reverse())
92 TYPE_SWITCH(Ty, S.Stk.discard<T>());
93}
94
95template <typename T>
96static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
97 OS << V;
98}
99
100template <>
101void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
102 QualType Ty) {
103 if (P.isZero()) {
104 OS << "nullptr";
105 return;
106 }
107
108 auto printDesc = [&OS, &Ctx](const Descriptor *Desc) {
109 if (const auto *D = Desc->asDecl()) {
110 // Subfields or named values.
111 if (const auto *VD = dyn_cast<ValueDecl>(Val: D)) {
112 OS << *VD;
113 return;
114 }
115 // Base classes.
116 if (isa<RecordDecl>(Val: D))
117 return;
118 }
119 // Temporary expression.
120 if (const auto *E = Desc->asExpr()) {
121 E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
122 return;
123 }
124 llvm_unreachable("Invalid descriptor type");
125 };
126
127 if (!Ty->isReferenceType())
128 OS << "&";
129 llvm::SmallVector<Pointer, 2> Levels;
130 for (Pointer F = P; !F.isRoot(); ) {
131 Levels.push_back(Elt: F);
132 F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
133 }
134
135 // Drop the first pointer since we print it unconditionally anyway.
136 if (!Levels.empty())
137 Levels.erase(CI: Levels.begin());
138
139 printDesc(P.getDeclDesc());
140 for (const auto &It : Levels) {
141 if (It.inArray()) {
142 OS << "[" << It.expand().getIndex() << "]";
143 continue;
144 }
145 if (auto Index = It.getIndex()) {
146 OS << " + " << Index;
147 continue;
148 }
149 OS << ".";
150 printDesc(It.getFieldDesc());
151 }
152}
153
154void InterpFrame::describe(llvm::raw_ostream &OS) const {
155 // We create frames for builtin functions as well, but we can't reliably
156 // diagnose them. The 'in call to' diagnostics for them add no value to the
157 // user _and_ it doesn't generally work since the argument types don't always
158 // match the function prototype. Just ignore them.
159 if (const auto *F = getFunction(); F && F->isBuiltin())
160 return;
161
162 const FunctionDecl *F = getCallee();
163 if (const auto *M = dyn_cast<CXXMethodDecl>(Val: F);
164 M && M->isInstance() && !isa<CXXConstructorDecl>(Val: F)) {
165 print(OS, P: This, Ctx&: S.getCtx(), Ty: S.getCtx().getRecordType(M->getParent()));
166 OS << "->";
167 }
168 OS << *F << "(";
169 unsigned Off = 0;
170
171 Off += Func->hasRVO() ? primSize(Type: PT_Ptr) : 0;
172 Off += Func->hasThisPointer() ? primSize(Type: PT_Ptr) : 0;
173
174 for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
175 QualType Ty = F->getParamDecl(i: I)->getType();
176
177 PrimType PrimTy = S.Ctx.classify(T: Ty).value_or(u: PT_Ptr);
178
179 TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
180 Off += align(Size: primSize(Type: PrimTy));
181 if (I + 1 != N)
182 OS << ", ";
183 }
184 OS << ")";
185}
186
187Frame *InterpFrame::getCaller() const {
188 if (Caller->Caller)
189 return Caller;
190 return S.getSplitFrame();
191}
192
193SourceRange InterpFrame::getCallRange() const {
194 if (!Caller->Func)
195 return S.getRange(F: nullptr, PC: {});
196 return S.getRange(F: Caller->Func, PC: RetPC - sizeof(uintptr_t));
197}
198
199const FunctionDecl *InterpFrame::getCallee() const {
200 if (!Func)
201 return nullptr;
202 return Func->getDecl();
203}
204
205Pointer InterpFrame::getLocalPointer(unsigned Offset) const {
206 assert(Offset < Func->getFrameSize() && "Invalid local offset.");
207 return Pointer(localBlock(Offset));
208}
209
210Pointer InterpFrame::getParamPointer(unsigned Off) {
211 // Return the block if it was created previously.
212 auto Pt = Params.find(Val: Off);
213 if (Pt != Params.end()) {
214 return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
215 }
216
217 // Allocate memory to store the parameter and the block metadata.
218 const auto &Desc = Func->getParamDescriptor(Offset: Off);
219 size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
220 auto Memory = std::make_unique<char[]>(num: BlockSize);
221 auto *B = new (Memory.get()) Block(Desc.second);
222
223 // Copy the initial value.
224 TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
225
226 // Record the param.
227 Params.insert(KV: {Off, std::move(Memory)});
228 return Pointer(B);
229}
230
231SourceInfo InterpFrame::getSource(CodePtr PC) const {
232 // Implicitly created functions don't have any code we could point at,
233 // so return the call site.
234 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
235 return Caller->getSource(PC: RetPC);
236
237 return S.getSource(F: Func, PC);
238}
239
240const Expr *InterpFrame::getExpr(CodePtr PC) const {
241 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
242 return Caller->getExpr(PC: RetPC);
243
244 return S.getExpr(F: Func, PC);
245}
246
247SourceLocation InterpFrame::getLocation(CodePtr PC) const {
248 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
249 return Caller->getLocation(PC: RetPC);
250
251 return S.getLocation(F: Func, PC);
252}
253
254SourceRange InterpFrame::getRange(CodePtr PC) const {
255 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
256 return Caller->getRange(PC: RetPC);
257
258 return S.getRange(F: Func, PC);
259}
260

source code of clang/lib/AST/Interp/InterpFrame.cpp