InterpFrame.cpp 6.1 KB

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