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 "MemberPointer.h" 16 #include "Pointer.h" 17 #include "PrimType.h" 18 #include "Program.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/ExprCXX.h" 22 23 using namespace clang; 24 using namespace clang::interp; 25 26 InterpFrame::InterpFrame(InterpState &S, const Function *Func, 27 InterpFrame *Caller, CodePtr RetPC, unsigned ArgSize) 28 : Caller(Caller), S(S), Depth(Caller ? Caller->Depth + 1 : 0), Func(Func), 29 RetPC(RetPC), ArgSize(ArgSize), Args(static_cast<char *>(S.Stk.top())), 30 FrameOffset(S.Stk.size()) { 31 if (!Func) 32 return; 33 34 unsigned FrameSize = Func->getFrameSize(); 35 if (FrameSize == 0) 36 return; 37 38 Locals = std::make_unique<char[]>(FrameSize); 39 for (auto &Scope : Func->scopes()) { 40 for (auto &Local : Scope.locals()) { 41 new (localBlock(Local.Offset)) Block(S.Ctx.getEvalID(), Local.Desc); 42 // Note that we are NOT calling invokeCtor() here, since that is done 43 // via the InitScope op. 44 new (localInlineDesc(Local.Offset)) InlineDescriptor(Local.Desc); 45 } 46 } 47 } 48 49 InterpFrame::InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC, 50 unsigned VarArgSize) 51 : InterpFrame(S, Func, S.Current, RetPC, Func->getArgSize() + VarArgSize) { 52 // As per our calling convention, the this pointer is 53 // part of the ArgSize. 54 // If the function has RVO, the RVO pointer is first. 55 // If the fuction has a This pointer, that one is next. 56 // Then follow the actual arguments (but those are handled 57 // in getParamPointer()). 58 if (Func->hasRVO()) 59 RVOPtr = stackRef<Pointer>(0); 60 61 if (Func->hasThisPointer()) { 62 if (Func->hasRVO()) 63 This = stackRef<Pointer>(sizeof(Pointer)); 64 else 65 This = stackRef<Pointer>(0); 66 } 67 } 68 69 InterpFrame::~InterpFrame() { 70 for (auto &Param : Params) 71 S.deallocate(reinterpret_cast<Block *>(Param.second.get())); 72 73 // When destroying the InterpFrame, call the Dtor for all block 74 // that haven't been destroyed via a destroy() op yet. 75 // This happens when the execution is interruped midway-through. 76 if (Func) { 77 for (auto &Scope : Func->scopes()) { 78 for (auto &Local : Scope.locals()) { 79 S.deallocate(localBlock(Local.Offset)); 80 } 81 } 82 } 83 } 84 85 void InterpFrame::initScope(unsigned Idx) { 86 if (!Func) 87 return; 88 for (auto &Local : Func->getScope(Idx).locals()) { 89 localBlock(Local.Offset)->invokeCtor(); 90 } 91 } 92 93 void InterpFrame::destroy(unsigned Idx) { 94 for (auto &Local : Func->getScope(Idx).locals()) { 95 S.deallocate(localBlock(Local.Offset)); 96 } 97 } 98 99 template <typename T> 100 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &ASTCtx, 101 QualType Ty) { 102 V.toAPValue(ASTCtx).printPretty(OS, ASTCtx, Ty); 103 } 104 105 void InterpFrame::describe(llvm::raw_ostream &OS) const { 106 // We create frames for builtin functions as well, but we can't reliably 107 // diagnose them. The 'in call to' diagnostics for them add no value to the 108 // user _and_ it doesn't generally work since the argument types don't always 109 // match the function prototype. Just ignore them. 110 // Similarly, for lambda static invokers, we would just print __invoke(). 111 if (const auto *F = getFunction(); 112 F && (F->isBuiltin() || F->isLambdaStaticInvoker())) 113 return; 114 115 const Expr *CallExpr = Caller->getExpr(getRetPC()); 116 const FunctionDecl *F = getCallee(); 117 bool IsMemberCall = isa<CXXMethodDecl>(F) && !isa<CXXConstructorDecl>(F) && 118 cast<CXXMethodDecl>(F)->isImplicitObjectMemberFunction(); 119 if (Func->hasThisPointer() && IsMemberCall) { 120 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(CallExpr)) { 121 const Expr *Object = MCE->getImplicitObjectArgument(); 122 Object->printPretty(OS, /*Helper=*/nullptr, 123 S.getASTContext().getPrintingPolicy(), 124 /*Indentation=*/0); 125 if (Object->getType()->isPointerType()) 126 OS << "->"; 127 else 128 OS << "."; 129 } else if (const auto *OCE = 130 dyn_cast_if_present<CXXOperatorCallExpr>(CallExpr)) { 131 OCE->getArg(0)->printPretty(OS, /*Helper=*/nullptr, 132 S.getASTContext().getPrintingPolicy(), 133 /*Indentation=*/0); 134 OS << "."; 135 } else if (const auto *M = dyn_cast<CXXMethodDecl>(F)) { 136 print(OS, This, S.getASTContext(), 137 S.getASTContext().getLValueReferenceType( 138 S.getASTContext().getRecordType(M->getParent()))); 139 OS << "."; 140 } 141 } 142 143 F->getNameForDiagnostic(OS, S.getASTContext().getPrintingPolicy(), 144 /*Qualified=*/false); 145 OS << '('; 146 unsigned Off = 0; 147 148 Off += Func->hasRVO() ? primSize(PT_Ptr) : 0; 149 Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0; 150 151 for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) { 152 QualType Ty = F->getParamDecl(I)->getType(); 153 154 PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr); 155 156 TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getASTContext(), Ty)); 157 Off += align(primSize(PrimTy)); 158 if (I + 1 != N) 159 OS << ", "; 160 } 161 OS << ")"; 162 } 163 164 Frame *InterpFrame::getCaller() const { 165 if (Caller->Caller) 166 return Caller; 167 return S.getSplitFrame(); 168 } 169 170 SourceRange InterpFrame::getCallRange() const { 171 if (!Caller->Func) { 172 if (SourceRange NullRange = S.getRange(nullptr, {}); NullRange.isValid()) 173 return NullRange; 174 return S.EvalLocation; 175 } 176 return S.getRange(Caller->Func, RetPC - sizeof(uintptr_t)); 177 } 178 179 const FunctionDecl *InterpFrame::getCallee() const { 180 if (!Func) 181 return nullptr; 182 return Func->getDecl(); 183 } 184 185 Pointer InterpFrame::getLocalPointer(unsigned Offset) const { 186 assert(Offset < Func->getFrameSize() && "Invalid local offset."); 187 return Pointer(localBlock(Offset)); 188 } 189 190 Pointer InterpFrame::getParamPointer(unsigned Off) { 191 // Return the block if it was created previously. 192 if (auto Pt = Params.find(Off); Pt != Params.end()) 193 return Pointer(reinterpret_cast<Block *>(Pt->second.get())); 194 195 // Allocate memory to store the parameter and the block metadata. 196 const auto &Desc = Func->getParamDescriptor(Off); 197 size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize(); 198 auto Memory = std::make_unique<char[]>(BlockSize); 199 auto *B = new (Memory.get()) Block(S.Ctx.getEvalID(), Desc.second); 200 B->invokeCtor(); 201 202 // Copy the initial value. 203 TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off))); 204 205 // Record the param. 206 Params.insert({Off, std::move(Memory)}); 207 return Pointer(B); 208 } 209 210 SourceInfo InterpFrame::getSource(CodePtr PC) const { 211 // Implicitly created functions don't have any code we could point at, 212 // so return the call site. 213 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 214 return Caller->getSource(RetPC); 215 216 return S.getSource(Func, PC); 217 } 218 219 const Expr *InterpFrame::getExpr(CodePtr PC) const { 220 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 221 return Caller->getExpr(RetPC); 222 223 return S.getExpr(Func, PC); 224 } 225 226 SourceLocation InterpFrame::getLocation(CodePtr PC) const { 227 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 228 return Caller->getLocation(RetPC); 229 230 return S.getLocation(Func, PC); 231 } 232 233 SourceRange InterpFrame::getRange(CodePtr PC) const { 234 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 235 return Caller->getRange(RetPC); 236 237 return S.getRange(Func, PC); 238 } 239