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