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