1*7330f729Sjoerg //===--- InterpFrame.cpp - Call Frame implementation for the VM -*- C++ -*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg
9*7330f729Sjoerg #include "InterpFrame.h"
10*7330f729Sjoerg #include "Function.h"
11*7330f729Sjoerg #include "Interp.h"
12*7330f729Sjoerg #include "InterpStack.h"
13*7330f729Sjoerg #include "PrimType.h"
14*7330f729Sjoerg #include "Program.h"
15*7330f729Sjoerg #include "clang/AST/DeclCXX.h"
16*7330f729Sjoerg
17*7330f729Sjoerg using namespace clang;
18*7330f729Sjoerg using namespace clang::interp;
19*7330f729Sjoerg
InterpFrame(InterpState & S,Function * Func,InterpFrame * Caller,CodePtr RetPC,Pointer && This)20*7330f729Sjoerg InterpFrame::InterpFrame(InterpState &S, Function *Func, InterpFrame *Caller,
21*7330f729Sjoerg CodePtr RetPC, Pointer &&This)
22*7330f729Sjoerg : Caller(Caller), S(S), Func(Func), This(std::move(This)), RetPC(RetPC),
23*7330f729Sjoerg ArgSize(Func ? Func->getArgSize() : 0),
24*7330f729Sjoerg Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) {
25*7330f729Sjoerg if (Func) {
26*7330f729Sjoerg if (unsigned FrameSize = Func->getFrameSize()) {
27*7330f729Sjoerg Locals = std::make_unique<char[]>(FrameSize);
28*7330f729Sjoerg for (auto &Scope : Func->scopes()) {
29*7330f729Sjoerg for (auto &Local : Scope.locals()) {
30*7330f729Sjoerg Block *B = new (localBlock(Local.Offset)) Block(Local.Desc);
31*7330f729Sjoerg B->invokeCtor();
32*7330f729Sjoerg }
33*7330f729Sjoerg }
34*7330f729Sjoerg }
35*7330f729Sjoerg }
36*7330f729Sjoerg }
37*7330f729Sjoerg
~InterpFrame()38*7330f729Sjoerg InterpFrame::~InterpFrame() {
39*7330f729Sjoerg if (Func && Func->isConstructor() && This.isBaseClass())
40*7330f729Sjoerg This.initialize();
41*7330f729Sjoerg for (auto &Param : Params)
42*7330f729Sjoerg S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
43*7330f729Sjoerg }
44*7330f729Sjoerg
destroy(unsigned Idx)45*7330f729Sjoerg void InterpFrame::destroy(unsigned Idx) {
46*7330f729Sjoerg for (auto &Local : Func->getScope(Idx).locals()) {
47*7330f729Sjoerg S.deallocate(reinterpret_cast<Block *>(localBlock(Local.Offset)));
48*7330f729Sjoerg }
49*7330f729Sjoerg }
50*7330f729Sjoerg
popArgs()51*7330f729Sjoerg void InterpFrame::popArgs() {
52*7330f729Sjoerg for (PrimType Ty : Func->args_reverse())
53*7330f729Sjoerg TYPE_SWITCH(Ty, S.Stk.discard<T>());
54*7330f729Sjoerg }
55*7330f729Sjoerg
56*7330f729Sjoerg template <typename T>
print(llvm::raw_ostream & OS,const T & V,ASTContext &,QualType)57*7330f729Sjoerg static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
58*7330f729Sjoerg OS << V;
59*7330f729Sjoerg }
60*7330f729Sjoerg
61*7330f729Sjoerg template <>
print(llvm::raw_ostream & OS,const Pointer & P,ASTContext & Ctx,QualType Ty)62*7330f729Sjoerg void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
63*7330f729Sjoerg QualType Ty) {
64*7330f729Sjoerg if (P.isZero()) {
65*7330f729Sjoerg OS << "nullptr";
66*7330f729Sjoerg return;
67*7330f729Sjoerg }
68*7330f729Sjoerg
69*7330f729Sjoerg auto printDesc = [&OS, &Ctx](Descriptor *Desc) {
70*7330f729Sjoerg if (auto *D = Desc->asDecl()) {
71*7330f729Sjoerg // Subfields or named values.
72*7330f729Sjoerg if (auto *VD = dyn_cast<ValueDecl>(D)) {
73*7330f729Sjoerg OS << *VD;
74*7330f729Sjoerg return;
75*7330f729Sjoerg }
76*7330f729Sjoerg // Base classes.
77*7330f729Sjoerg if (isa<RecordDecl>(D)) {
78*7330f729Sjoerg return;
79*7330f729Sjoerg }
80*7330f729Sjoerg }
81*7330f729Sjoerg // Temporary expression.
82*7330f729Sjoerg if (auto *E = Desc->asExpr()) {
83*7330f729Sjoerg E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
84*7330f729Sjoerg return;
85*7330f729Sjoerg }
86*7330f729Sjoerg llvm_unreachable("Invalid descriptor type");
87*7330f729Sjoerg };
88*7330f729Sjoerg
89*7330f729Sjoerg if (!Ty->isReferenceType())
90*7330f729Sjoerg OS << "&";
91*7330f729Sjoerg llvm::SmallVector<Pointer, 2> Levels;
92*7330f729Sjoerg for (Pointer F = P; !F.isRoot(); ) {
93*7330f729Sjoerg Levels.push_back(F);
94*7330f729Sjoerg F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
95*7330f729Sjoerg }
96*7330f729Sjoerg
97*7330f729Sjoerg printDesc(P.getDeclDesc());
98*7330f729Sjoerg for (auto It = Levels.rbegin(); It != Levels.rend(); ++It) {
99*7330f729Sjoerg if (It->inArray()) {
100*7330f729Sjoerg OS << "[" << It->expand().getIndex() << "]";
101*7330f729Sjoerg continue;
102*7330f729Sjoerg }
103*7330f729Sjoerg if (auto Index = It->getIndex()) {
104*7330f729Sjoerg OS << " + " << Index;
105*7330f729Sjoerg continue;
106*7330f729Sjoerg }
107*7330f729Sjoerg OS << ".";
108*7330f729Sjoerg printDesc(It->getFieldDesc());
109*7330f729Sjoerg }
110*7330f729Sjoerg }
111*7330f729Sjoerg
describe(llvm::raw_ostream & OS)112*7330f729Sjoerg void InterpFrame::describe(llvm::raw_ostream &OS) {
113*7330f729Sjoerg const FunctionDecl *F = getCallee();
114*7330f729Sjoerg auto *M = dyn_cast<CXXMethodDecl>(F);
115*7330f729Sjoerg if (M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
116*7330f729Sjoerg print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
117*7330f729Sjoerg OS << "->";
118*7330f729Sjoerg }
119*7330f729Sjoerg OS << *F << "(";
120*7330f729Sjoerg unsigned Off = Func->hasRVO() ? primSize(PT_Ptr) : 0;
121*7330f729Sjoerg for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
122*7330f729Sjoerg QualType Ty = F->getParamDecl(I)->getType();
123*7330f729Sjoerg
124*7330f729Sjoerg PrimType PrimTy;
125*7330f729Sjoerg if (llvm::Optional<PrimType> T = S.Ctx.classify(Ty)) {
126*7330f729Sjoerg PrimTy = *T;
127*7330f729Sjoerg } else {
128*7330f729Sjoerg PrimTy = PT_Ptr;
129*7330f729Sjoerg }
130*7330f729Sjoerg
131*7330f729Sjoerg TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
132*7330f729Sjoerg Off += align(primSize(PrimTy));
133*7330f729Sjoerg if (I + 1 != N)
134*7330f729Sjoerg OS << ", ";
135*7330f729Sjoerg }
136*7330f729Sjoerg OS << ")";
137*7330f729Sjoerg }
138*7330f729Sjoerg
getCaller() const139*7330f729Sjoerg Frame *InterpFrame::getCaller() const {
140*7330f729Sjoerg if (Caller->Caller)
141*7330f729Sjoerg return Caller;
142*7330f729Sjoerg return S.getSplitFrame();
143*7330f729Sjoerg }
144*7330f729Sjoerg
getCallLocation() const145*7330f729Sjoerg SourceLocation InterpFrame::getCallLocation() const {
146*7330f729Sjoerg if (!Caller->Func)
147*7330f729Sjoerg return S.getLocation(nullptr, {});
148*7330f729Sjoerg return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
149*7330f729Sjoerg }
150*7330f729Sjoerg
getCallee() const151*7330f729Sjoerg const FunctionDecl *InterpFrame::getCallee() const {
152*7330f729Sjoerg return Func->getDecl();
153*7330f729Sjoerg }
154*7330f729Sjoerg
getLocalPointer(unsigned Offset)155*7330f729Sjoerg Pointer InterpFrame::getLocalPointer(unsigned Offset) {
156*7330f729Sjoerg assert(Offset < Func->getFrameSize() && "Invalid local offset.");
157*7330f729Sjoerg return Pointer(
158*7330f729Sjoerg reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block)));
159*7330f729Sjoerg }
160*7330f729Sjoerg
getParamPointer(unsigned Off)161*7330f729Sjoerg Pointer InterpFrame::getParamPointer(unsigned Off) {
162*7330f729Sjoerg // Return the block if it was created previously.
163*7330f729Sjoerg auto Pt = Params.find(Off);
164*7330f729Sjoerg if (Pt != Params.end()) {
165*7330f729Sjoerg return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
166*7330f729Sjoerg }
167*7330f729Sjoerg
168*7330f729Sjoerg // Allocate memory to store the parameter and the block metadata.
169*7330f729Sjoerg const auto &Desc = Func->getParamDescriptor(Off);
170*7330f729Sjoerg size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
171*7330f729Sjoerg auto Memory = std::make_unique<char[]>(BlockSize);
172*7330f729Sjoerg auto *B = new (Memory.get()) Block(Desc.second);
173*7330f729Sjoerg
174*7330f729Sjoerg // Copy the initial value.
175*7330f729Sjoerg TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
176*7330f729Sjoerg
177*7330f729Sjoerg // Record the param.
178*7330f729Sjoerg Params.insert({Off, std::move(Memory)});
179*7330f729Sjoerg return Pointer(B);
180*7330f729Sjoerg }
181*7330f729Sjoerg
getSource(CodePtr PC) const182*7330f729Sjoerg SourceInfo InterpFrame::getSource(CodePtr PC) const {
183*7330f729Sjoerg return S.getSource(Func, PC);
184*7330f729Sjoerg }
185*7330f729Sjoerg
getExpr(CodePtr PC) const186*7330f729Sjoerg const Expr *InterpFrame::getExpr(CodePtr PC) const {
187*7330f729Sjoerg return S.getExpr(Func, PC);
188*7330f729Sjoerg }
189*7330f729Sjoerg
getLocation(CodePtr PC) const190*7330f729Sjoerg SourceLocation InterpFrame::getLocation(CodePtr PC) const {
191*7330f729Sjoerg return S.getLocation(Func, PC);
192*7330f729Sjoerg }
193*7330f729Sjoerg
194