1 //===--- InterpFrame.cpp - Call Frame implementation for the VM -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "InterpFrame.h"
12 #include "InterpStack.h"
15 #include "clang/AST/DeclCXX.h"
17 using namespace clang;
18 using namespace clang::interp;
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()) {
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);
38 InterpFrame::~InterpFrame() {
39 if (Func && Func->isConstructor() && This.isBaseClass())
41 for (auto &Param : Params)
42 S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
45 void InterpFrame::destroy(unsigned Idx) {
46 for (auto &Local : Func->getScope(Idx).locals()) {
47 S.deallocate(reinterpret_cast<Block *>(localBlock(Local.Offset)));
51 void InterpFrame::popArgs() {
52 for (PrimType Ty : Func->args_reverse())
53 TYPE_SWITCH(Ty, S.Stk.discard<T>());
57 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
62 void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
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)) {
77 if (isa<RecordDecl>(D)) {
81 // Temporary expression.
82 if (auto *E = Desc->asExpr()) {
83 E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
86 llvm_unreachable("Invalid descriptor type");
89 if (!Ty->isReferenceType())
91 llvm::SmallVector<Pointer, 2> Levels;
92 for (Pointer F = P; !F.isRoot(); ) {
94 F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
97 printDesc(P.getDeclDesc());
98 for (auto It = Levels.rbegin(); It != Levels.rend(); ++It) {
100 OS << "[" << It->expand().getIndex() << "]";
103 if (auto Index = It->getIndex()) {
104 OS << " + " << Index;
108 printDesc(It->getFieldDesc());
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()));
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();
125 if (llvm::Optional<PrimType> T = S.Ctx.classify(Ty)) {
131 TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
132 Off += align(primSize(PrimTy));
139 Frame *InterpFrame::getCaller() const {
142 return S.getSplitFrame();
145 SourceLocation InterpFrame::getCallLocation() const {
147 return S.getLocation(nullptr, {});
148 return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
151 const FunctionDecl *InterpFrame::getCallee() const {
152 return Func->getDecl();
155 Pointer InterpFrame::getLocalPointer(unsigned Offset) {
156 assert(Offset < Func->getFrameSize() && "Invalid local offset.");
158 reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block)));
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()));
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);
174 // Copy the initial value.
175 TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
178 Params.insert({Off, std::move(Memory)});
182 SourceInfo InterpFrame::getSource(CodePtr PC) const {
183 return S.getSource(Func, PC);
186 const Expr *InterpFrame::getExpr(CodePtr PC) const {
187 return S.getExpr(Func, PC);
190 SourceLocation InterpFrame::getLocation(CodePtr PC) const {
191 return S.getLocation(Func, PC);