InterpFrame.cpp 5.3 KB

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