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