InterpFrame.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //===--- InterpFrame.h - 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. // Defines the class storing information about stack frames in the interpreter.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_AST_INTERP_INTERPFRAME_H
  13. #define LLVM_CLANG_AST_INTERP_INTERPFRAME_H
  14. #include "Frame.h"
  15. #include "Program.h"
  16. #include "State.h"
  17. #include <cstdint>
  18. #include <vector>
  19. namespace clang {
  20. namespace interp {
  21. class Function;
  22. class InterpState;
  23. class Pointer;
  24. /// Frame storing local variables.
  25. class InterpFrame final : public Frame {
  26. public:
  27. /// The frame of the previous function.
  28. InterpFrame *Caller;
  29. /// Creates a new frame for a method call.
  30. InterpFrame(InterpState &S, const Function *Func, InterpFrame *Caller,
  31. CodePtr RetPC);
  32. /// Creates a new frame with the values that make sense.
  33. /// I.e., the caller is the current frame of S,
  34. /// the This() pointer is the current Pointer on the top of S's stack,
  35. /// and the RVO pointer is before that.
  36. InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC);
  37. /// Destroys the frame, killing all live pointers to stack slots.
  38. ~InterpFrame();
  39. /// Invokes the destructors for a scope.
  40. void destroy(unsigned Idx);
  41. /// Pops the arguments off the stack.
  42. void popArgs();
  43. /// Describes the frame with arguments for diagnostic purposes.
  44. void describe(llvm::raw_ostream &OS) override;
  45. /// Returns the parent frame object.
  46. Frame *getCaller() const override;
  47. /// Returns the location of the call to the frame.
  48. SourceLocation getCallLocation() const override;
  49. /// Returns the caller.
  50. const FunctionDecl *getCallee() const override;
  51. /// Returns the current function.
  52. const Function *getFunction() const { return Func; }
  53. /// Returns the offset on the stack at which the frame starts.
  54. size_t getFrameOffset() const { return FrameOffset; }
  55. /// Returns the value of a local variable.
  56. template <typename T> const T &getLocal(unsigned Offset) const {
  57. return localRef<T>(Offset);
  58. }
  59. /// Mutates a local variable.
  60. template <typename T> void setLocal(unsigned Offset, const T &Value) {
  61. localRef<T>(Offset) = Value;
  62. localInlineDesc(Offset)->IsInitialized = true;
  63. }
  64. /// Returns a pointer to a local variables.
  65. Pointer getLocalPointer(unsigned Offset) const;
  66. /// Returns the value of an argument.
  67. template <typename T> const T &getParam(unsigned Offset) const {
  68. auto Pt = Params.find(Offset);
  69. if (Pt == Params.end()) {
  70. return stackRef<T>(Offset);
  71. } else {
  72. return Pointer(reinterpret_cast<Block *>(Pt->second.get())).deref<T>();
  73. }
  74. }
  75. /// Mutates a local copy of a parameter.
  76. template <typename T> void setParam(unsigned Offset, const T &Value) {
  77. getParamPointer(Offset).deref<T>() = Value;
  78. }
  79. /// Returns a pointer to an argument - lazily creates a block.
  80. Pointer getParamPointer(unsigned Offset);
  81. /// Returns the 'this' pointer.
  82. const Pointer &getThis() const { return This; }
  83. /// Returns the RVO pointer, if the Function has one.
  84. const Pointer &getRVOPtr() const { return RVOPtr; }
  85. /// Checks if the frame is a root frame - return should quit the interpreter.
  86. bool isRoot() const { return !Func; }
  87. /// Returns the PC of the frame's code start.
  88. CodePtr getPC() const { return Func->getCodeBegin(); }
  89. /// Returns the return address of the frame.
  90. CodePtr getRetPC() const { return RetPC; }
  91. /// Map a location to a source.
  92. virtual SourceInfo getSource(CodePtr PC) const;
  93. const Expr *getExpr(CodePtr PC) const;
  94. SourceLocation getLocation(CodePtr PC) const;
  95. private:
  96. /// Returns an original argument from the stack.
  97. template <typename T> const T &stackRef(unsigned Offset) const {
  98. assert(Args);
  99. return *reinterpret_cast<const T *>(Args - ArgSize + Offset);
  100. }
  101. /// Returns an offset to a local.
  102. template <typename T> T &localRef(unsigned Offset) const {
  103. return getLocalPointer(Offset).deref<T>();
  104. }
  105. /// Returns a pointer to a local's block.
  106. void *localBlock(unsigned Offset) const {
  107. return Locals.get() + Offset - sizeof(Block);
  108. }
  109. // Returns the inline descriptor of the local.
  110. InlineDescriptor *localInlineDesc(unsigned Offset) const {
  111. return reinterpret_cast<InlineDescriptor *>(Locals.get() + Offset);
  112. }
  113. private:
  114. /// Reference to the interpreter state.
  115. InterpState &S;
  116. /// Reference to the function being executed.
  117. const Function *Func;
  118. /// Current object pointer for methods.
  119. Pointer This;
  120. /// Pointer the non-primitive return value gets constructed in.
  121. Pointer RVOPtr;
  122. /// Return address.
  123. CodePtr RetPC;
  124. /// The size of all the arguments.
  125. const unsigned ArgSize;
  126. /// Pointer to the arguments in the callee's frame.
  127. char *Args = nullptr;
  128. /// Fixed, initial storage for known local variables.
  129. std::unique_ptr<char[]> Locals;
  130. /// Offset on the stack at entry.
  131. const size_t FrameOffset;
  132. /// Mapping from arg offsets to their argument blocks.
  133. llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Params;
  134. };
  135. } // namespace interp
  136. } // namespace clang
  137. #endif