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 void InterpFrame::popArgs() { 100 for (PrimType Ty : Func->args_reverse()) 101 TYPE_SWITCH(Ty, S.Stk.discard<T>()); 102 } 103 104 template <typename T> 105 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &ASTCtx, 106 QualType Ty) { 107 V.toAPValue(ASTCtx).printPretty(OS, ASTCtx, Ty); 108 } 109 110 template <> 111 void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx, 112 QualType Ty) { 113 if (P.isZero()) { 114 OS << "nullptr"; 115 return; 116 } 117 118 auto printDesc = [&OS, &Ctx](const Descriptor *Desc) { 119 if (const auto *D = Desc->asDecl()) { 120 // Subfields or named values. 121 if (const auto *VD = dyn_cast<ValueDecl>(D)) { 122 OS << *VD; 123 return; 124 } 125 // Base classes. 126 if (isa<RecordDecl>(D)) 127 return; 128 } 129 // Temporary expression. 130 if (const auto *E = Desc->asExpr()) { 131 E->printPretty(OS, nullptr, Ctx.getPrintingPolicy()); 132 return; 133 } 134 llvm_unreachable("Invalid descriptor type"); 135 }; 136 137 if (!Ty->isReferenceType()) 138 OS << "&"; 139 llvm::SmallVector<Pointer, 2> Levels; 140 for (Pointer F = P; !F.isRoot();) { 141 Levels.push_back(F); 142 F = F.isArrayElement() ? F.getArray().expand() : F.getBase(); 143 } 144 145 // Drop the first pointer since we print it unconditionally anyway. 146 if (!Levels.empty()) 147 Levels.erase(Levels.begin()); 148 149 printDesc(P.getDeclDesc()); 150 for (const auto &It : Levels) { 151 if (It.inArray()) { 152 OS << "[" << It.expand().getIndex() << "]"; 153 continue; 154 } 155 if (auto Index = It.getIndex()) { 156 OS << " + " << Index; 157 continue; 158 } 159 OS << "."; 160 printDesc(It.getFieldDesc()); 161 } 162 } 163 164 void InterpFrame::describe(llvm::raw_ostream &OS) const { 165 // We create frames for builtin functions as well, but we can't reliably 166 // diagnose them. The 'in call to' diagnostics for them add no value to the 167 // user _and_ it doesn't generally work since the argument types don't always 168 // match the function prototype. Just ignore them. 169 // Similarly, for lambda static invokers, we would just print __invoke(). 170 if (const auto *F = getFunction(); 171 F && (F->isBuiltin() || F->isLambdaStaticInvoker())) 172 return; 173 174 const Expr *CallExpr = Caller->getExpr(getRetPC()); 175 const FunctionDecl *F = getCallee(); 176 bool IsMemberCall = isa<CXXMethodDecl>(F) && !isa<CXXConstructorDecl>(F) && 177 cast<CXXMethodDecl>(F)->isImplicitObjectMemberFunction(); 178 if (Func->hasThisPointer() && IsMemberCall) { 179 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(CallExpr)) { 180 const Expr *Object = MCE->getImplicitObjectArgument(); 181 Object->printPretty(OS, /*Helper=*/nullptr, 182 S.getCtx().getPrintingPolicy(), 183 /*Indentation=*/0); 184 if (Object->getType()->isPointerType()) 185 OS << "->"; 186 else 187 OS << "."; 188 } else if (const auto *OCE = 189 dyn_cast_if_present<CXXOperatorCallExpr>(CallExpr)) { 190 OCE->getArg(0)->printPretty(OS, /*Helper=*/nullptr, 191 S.getCtx().getPrintingPolicy(), 192 /*Indentation=*/0); 193 OS << "."; 194 } else if (const auto *M = dyn_cast<CXXMethodDecl>(F)) { 195 print(OS, This, S.getCtx(), 196 S.getCtx().getLValueReferenceType( 197 S.getCtx().getRecordType(M->getParent()))); 198 OS << "."; 199 } 200 } 201 202 F->getNameForDiagnostic(OS, S.getCtx().getPrintingPolicy(), 203 /*Qualified=*/false); 204 OS << '('; 205 unsigned Off = 0; 206 207 Off += Func->hasRVO() ? primSize(PT_Ptr) : 0; 208 Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0; 209 210 for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) { 211 QualType Ty = F->getParamDecl(I)->getType(); 212 213 PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr); 214 215 TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty)); 216 Off += align(primSize(PrimTy)); 217 if (I + 1 != N) 218 OS << ", "; 219 } 220 OS << ")"; 221 } 222 223 Frame *InterpFrame::getCaller() const { 224 if (Caller->Caller) 225 return Caller; 226 return S.getSplitFrame(); 227 } 228 229 SourceRange InterpFrame::getCallRange() const { 230 if (!Caller->Func) { 231 if (SourceRange NullRange = S.getRange(nullptr, {}); NullRange.isValid()) 232 return NullRange; 233 return S.EvalLocation; 234 } 235 return S.getRange(Caller->Func, RetPC - sizeof(uintptr_t)); 236 } 237 238 const FunctionDecl *InterpFrame::getCallee() const { 239 if (!Func) 240 return nullptr; 241 return Func->getDecl(); 242 } 243 244 Pointer InterpFrame::getLocalPointer(unsigned Offset) const { 245 assert(Offset < Func->getFrameSize() && "Invalid local offset."); 246 return Pointer(localBlock(Offset)); 247 } 248 249 Pointer InterpFrame::getParamPointer(unsigned Off) { 250 // Return the block if it was created previously. 251 if (auto Pt = Params.find(Off); Pt != Params.end()) 252 return Pointer(reinterpret_cast<Block *>(Pt->second.get())); 253 254 // Allocate memory to store the parameter and the block metadata. 255 const auto &Desc = Func->getParamDescriptor(Off); 256 size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize(); 257 auto Memory = std::make_unique<char[]>(BlockSize); 258 auto *B = new (Memory.get()) Block(S.Ctx.getEvalID(), Desc.second); 259 B->invokeCtor(); 260 261 // Copy the initial value. 262 TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off))); 263 264 // Record the param. 265 Params.insert({Off, std::move(Memory)}); 266 return Pointer(B); 267 } 268 269 SourceInfo InterpFrame::getSource(CodePtr PC) const { 270 // Implicitly created functions don't have any code we could point at, 271 // so return the call site. 272 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 273 return Caller->getSource(RetPC); 274 275 return S.getSource(Func, PC); 276 } 277 278 const Expr *InterpFrame::getExpr(CodePtr PC) const { 279 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 280 return Caller->getExpr(RetPC); 281 282 return S.getExpr(Func, PC); 283 } 284 285 SourceLocation InterpFrame::getLocation(CodePtr PC) const { 286 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 287 return Caller->getLocation(RetPC); 288 289 return S.getLocation(Func, PC); 290 } 291 292 SourceRange InterpFrame::getRange(CodePtr PC) const { 293 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) 294 return Caller->getRange(RetPC); 295 296 return S.getRange(Func, PC); 297 } 298