| 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 | |
| 13 | #ifndef LLVM_CLANG_AST_INTERP_INTERPFRAME_H |
| 14 | #define LLVM_CLANG_AST_INTERP_INTERPFRAME_H |
| 15 | |
| 16 | #include "Frame.h" |
| 17 | #include "InterpBlock.h" |
| 18 | #include "Pointer.h" |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace interp { |
| 22 | class Function; |
| 23 | class InterpState; |
| 24 | class Pointer; |
| 25 | |
| 26 | /// Frame storing local variables. |
| 27 | class InterpFrame final : public Frame { |
| 28 | public: |
| 29 | /// The frame of the previous function. |
| 30 | InterpFrame *Caller; |
| 31 | |
| 32 | /// Bottom Frame. |
| 33 | InterpFrame(InterpState &S); |
| 34 | |
| 35 | /// Creates a new frame for a method call. |
| 36 | InterpFrame(InterpState &S, const Function *Func, InterpFrame *Caller, |
| 37 | CodePtr RetPC, unsigned ArgSize); |
| 38 | |
| 39 | /// Creates a new frame with the values that make sense. |
| 40 | /// I.e., the caller is the current frame of S, |
| 41 | /// the This() pointer is the current Pointer on the top of S's stack, |
| 42 | /// and the RVO pointer is before that. |
| 43 | InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC, |
| 44 | unsigned VarArgSize = 0); |
| 45 | |
| 46 | /// Destroys the frame, killing all live pointers to stack slots. |
| 47 | ~InterpFrame(); |
| 48 | |
| 49 | static void free(InterpFrame *F) { |
| 50 | if (!F->isBottomFrame()) |
| 51 | delete F; |
| 52 | } |
| 53 | |
| 54 | /// Invokes the destructors for a scope. |
| 55 | void destroy(unsigned Idx); |
| 56 | void initScope(unsigned Idx); |
| 57 | void destroyScopes(); |
| 58 | void enableLocal(unsigned Idx); |
| 59 | bool isLocalEnabled(unsigned Idx) const { |
| 60 | return localInlineDesc(Offset: Idx)->IsActive; |
| 61 | } |
| 62 | |
| 63 | /// Describes the frame with arguments for diagnostic purposes. |
| 64 | void describe(llvm::raw_ostream &OS) const override; |
| 65 | |
| 66 | /// Returns the parent frame object. |
| 67 | Frame *getCaller() const override { return Caller; } |
| 68 | |
| 69 | /// Returns the location of the call to the frame. |
| 70 | SourceRange getCallRange() const override; |
| 71 | |
| 72 | /// Returns the caller. |
| 73 | const FunctionDecl *getCallee() const override; |
| 74 | |
| 75 | /// Returns the current function. |
| 76 | const Function *getFunction() const { return Func; } |
| 77 | |
| 78 | /// Returns the offset on the stack at which the frame starts. |
| 79 | size_t getFrameOffset() const { return FrameOffset; } |
| 80 | |
| 81 | /// Returns the value of a local variable. |
| 82 | template <typename T> const T &getLocal(unsigned Offset) const { |
| 83 | return localRef<T>(Offset); |
| 84 | } |
| 85 | |
| 86 | /// Mutates a local variable. |
| 87 | template <typename T> void setLocal(unsigned Offset, const T &Value) { |
| 88 | localRef<T>(Offset) = Value; |
| 89 | localInlineDesc(Offset)->IsInitialized = true; |
| 90 | } |
| 91 | |
| 92 | /// Returns a pointer to a local variables. |
| 93 | Pointer getLocalPointer(unsigned Offset) const; |
| 94 | Block *getLocalBlock(unsigned Offset) const; |
| 95 | |
| 96 | /// Returns the value of an argument. |
| 97 | template <typename T> const T &getParam(unsigned Offset) const { |
| 98 | auto Pt = Params.find(Val: Offset); |
| 99 | if (Pt == Params.end()) |
| 100 | return stackRef<T>(Offset); |
| 101 | return reinterpret_cast<const Block *>(Pt->second.get())->deref<T>(); |
| 102 | } |
| 103 | |
| 104 | /// Mutates a local copy of a parameter. |
| 105 | template <typename T> void setParam(unsigned Offset, const T &Value) { |
| 106 | getParamPointer(Offset).deref<T>() = Value; |
| 107 | } |
| 108 | |
| 109 | /// Returns a pointer to an argument - lazily creates a block. |
| 110 | Pointer getParamPointer(unsigned Offset); |
| 111 | |
| 112 | bool hasThisPointer() const { return Func && Func->hasThisPointer(); } |
| 113 | /// Returns the 'this' pointer. |
| 114 | const Pointer &getThis() const { |
| 115 | assert(hasThisPointer()); |
| 116 | assert(!isBottomFrame()); |
| 117 | return stackRef<Pointer>(Offset: ThisPointerOffset); |
| 118 | } |
| 119 | |
| 120 | /// Returns the RVO pointer, if the Function has one. |
| 121 | const Pointer &getRVOPtr() const { |
| 122 | assert(Func); |
| 123 | assert(Func->hasRVO()); |
| 124 | assert(!isBottomFrame()); |
| 125 | return stackRef<Pointer>(Offset: 0); |
| 126 | } |
| 127 | |
| 128 | /// Checks if the frame is a root frame - return should quit the interpreter. |
| 129 | bool isRoot() const { return !Func; } |
| 130 | |
| 131 | /// Returns the PC of the frame's code start. |
| 132 | CodePtr getPC() const { return Func->getCodeBegin(); } |
| 133 | |
| 134 | /// Returns the return address of the frame. |
| 135 | CodePtr getRetPC() const { return RetPC; } |
| 136 | |
| 137 | /// Map a location to a source. |
| 138 | SourceInfo getSource(CodePtr PC) const; |
| 139 | const Expr *getExpr(CodePtr PC) const; |
| 140 | SourceLocation getLocation(CodePtr PC) const; |
| 141 | SourceRange getRange(CodePtr PC) const; |
| 142 | |
| 143 | unsigned getDepth() const { return Depth; } |
| 144 | |
| 145 | bool isStdFunction() const; |
| 146 | |
| 147 | bool isBottomFrame() const { return !Caller; } |
| 148 | |
| 149 | void dump() const { dump(OS&: llvm::errs(), Indent: 0); } |
| 150 | void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const; |
| 151 | |
| 152 | private: |
| 153 | /// Returns an original argument from the stack. |
| 154 | template <typename T> const T &stackRef(unsigned Offset) const { |
| 155 | assert(Args); |
| 156 | return *reinterpret_cast<const T *>(Args - ArgSize + Offset); |
| 157 | } |
| 158 | |
| 159 | /// Returns an offset to a local. |
| 160 | template <typename T> T &localRef(unsigned Offset) const { |
| 161 | return localBlock(Offset)->deref<T>(); |
| 162 | } |
| 163 | |
| 164 | /// Returns a pointer to a local's block. |
| 165 | Block *localBlock(unsigned Offset) const { |
| 166 | return reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block)); |
| 167 | } |
| 168 | |
| 169 | /// Returns the inline descriptor of the local. |
| 170 | InlineDescriptor *localInlineDesc(unsigned Offset) const { |
| 171 | return reinterpret_cast<InlineDescriptor *>(Locals.get() + Offset); |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | /// Reference to the interpreter state. |
| 176 | InterpState &S; |
| 177 | /// Depth of this frame. |
| 178 | unsigned Depth; |
| 179 | /// Reference to the function being executed. |
| 180 | const Function *Func; |
| 181 | /// Offset of the instance pointer. Use with stackRef<>(). |
| 182 | unsigned ThisPointerOffset; |
| 183 | /// Return address. |
| 184 | CodePtr RetPC; |
| 185 | /// The size of all the arguments. |
| 186 | const unsigned ArgSize; |
| 187 | /// Pointer to the arguments in the callee's frame. |
| 188 | char *Args = nullptr; |
| 189 | /// Fixed, initial storage for known local variables. |
| 190 | std::unique_ptr<char[]> Locals; |
| 191 | /// Offset on the stack at entry. |
| 192 | const size_t FrameOffset; |
| 193 | /// Mapping from arg offsets to their argument blocks. |
| 194 | llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Params; |
| 195 | |
| 196 | public: |
| 197 | unsigned MSVCConstexprAllowed = 0; |
| 198 | }; |
| 199 | |
| 200 | } // namespace interp |
| 201 | } // namespace clang |
| 202 | |
| 203 | #endif |
| 204 | |