InterpFrame.h 4.5 KB

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