| 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 | /// Returns the number of bytes needed to allocate an InterpFrame for the |
| 50 | /// given function. |
| 51 | static size_t allocSize(const Function *F) { |
| 52 | return sizeof(InterpFrame) + F->getFrameSize() + |
| 53 | (F->getArgSize() + (sizeof(Block) * F->getNumWrittenParams())); |
| 54 | } |
| 55 | |
| 56 | std::string getName() const { |
| 57 | if (!Func) |
| 58 | return "Bottom frame" ; |
| 59 | return Func->getName(); |
| 60 | } |
| 61 | |
| 62 | static void free(InterpFrame *F) { |
| 63 | if (!F->isBottomFrame()) { |
| 64 | F->~InterpFrame(); |
| 65 | delete[] reinterpret_cast<char *>(F); |
| 66 | } else { |
| 67 | F->~InterpFrame(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// Invokes the destructors for a scope. |
| 72 | void destroy(unsigned Idx); |
| 73 | void initScope(unsigned Idx); |
| 74 | void destroyScopes(); |
| 75 | void enableLocal(unsigned Idx); |
| 76 | bool isLocalEnabled(unsigned Idx) const { |
| 77 | return localInlineDesc(Offset: Idx)->IsActive; |
| 78 | } |
| 79 | |
| 80 | /// Describes the frame with arguments for diagnostic purposes. |
| 81 | void describe(llvm::raw_ostream &OS) const override; |
| 82 | |
| 83 | /// Returns the parent frame object. |
| 84 | Frame *getCaller() const override { return Caller; } |
| 85 | |
| 86 | /// Returns the location of the call to the frame. |
| 87 | SourceRange getCallRange() const override; |
| 88 | |
| 89 | /// Returns the caller. |
| 90 | const FunctionDecl *getCallee() const override; |
| 91 | |
| 92 | /// Returns the current function. |
| 93 | const Function *getFunction() const { return Func; } |
| 94 | |
| 95 | /// Returns the offset on the stack at which the frame starts. |
| 96 | size_t getFrameOffset() const { return FrameOffset; } |
| 97 | |
| 98 | /// Returns the value of a local variable. |
| 99 | template <typename T> const T &getLocal(unsigned Offset) const { |
| 100 | return localRef<T>(Offset); |
| 101 | } |
| 102 | |
| 103 | /// Mutates a local variable. |
| 104 | template <typename T> void setLocal(unsigned Offset, const T &Value) { |
| 105 | localRef<T>(Offset) = Value; |
| 106 | localInlineDesc(Offset)->IsInitialized = true; |
| 107 | } |
| 108 | |
| 109 | /// Returns a pointer to a local variables. |
| 110 | Pointer getLocalPointer(unsigned Offset) const; |
| 111 | Block *getLocalBlock(unsigned Offset) const; |
| 112 | |
| 113 | /// Returns the value of an argument. |
| 114 | template <typename T> const T &getParam(unsigned Index) const { |
| 115 | Block *ArgBlock = argBlock(Index); |
| 116 | if (!ArgBlock->isInitialized()) |
| 117 | return stackRef<T>(Func->getParamDescriptor(Index).Offset); |
| 118 | return ArgBlock->deref<T>(); |
| 119 | } |
| 120 | |
| 121 | /// Mutates a local copy of a parameter. |
| 122 | template <typename T> void setParam(unsigned Index, const T &Value) { |
| 123 | argBlock(Index)->deref<T>() = Value; |
| 124 | } |
| 125 | |
| 126 | /// Returns a pointer to an argument - lazily creates a block. |
| 127 | Pointer getParamPointer(unsigned Offset); |
| 128 | |
| 129 | bool hasThisPointer() const { return Func && Func->hasThisPointer(); } |
| 130 | |
| 131 | /// Returns the 'this' pointer. |
| 132 | const Pointer &getThis() const { |
| 133 | assert(hasThisPointer()); |
| 134 | assert(!isBottomFrame()); |
| 135 | return stackRef<Pointer>(Offset: ThisPointerOffset); |
| 136 | } |
| 137 | |
| 138 | /// Returns the RVO pointer, if the Function has one. |
| 139 | const Pointer &getRVOPtr() const { |
| 140 | assert(Func); |
| 141 | assert(Func->hasRVO()); |
| 142 | assert(!isBottomFrame()); |
| 143 | return stackRef<Pointer>(Offset: 0); |
| 144 | } |
| 145 | |
| 146 | /// Checks if the frame is a root frame - return should quit the interpreter. |
| 147 | bool isRoot() const { return !Func; } |
| 148 | |
| 149 | /// Returns the PC of the frame's code start. |
| 150 | CodePtr getPC() const { return Func->getCodeBegin(); } |
| 151 | |
| 152 | /// Returns the return address of the frame. |
| 153 | CodePtr getRetPC() const { return RetPC; } |
| 154 | |
| 155 | /// Map a location to a source. |
| 156 | SourceInfo getSource(CodePtr PC) const; |
| 157 | const Expr *getExpr(CodePtr PC) const; |
| 158 | SourceLocation getLocation(CodePtr PC) const; |
| 159 | SourceRange getRange(CodePtr PC) const; |
| 160 | |
| 161 | unsigned getDepth() const { return Depth; } |
| 162 | |
| 163 | bool isStdFunction() const; |
| 164 | |
| 165 | bool isBottomFrame() const { return !Caller; } |
| 166 | |
| 167 | void dump() const { dump(OS&: llvm::errs(), Indent: 0); } |
| 168 | void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const; |
| 169 | |
| 170 | private: |
| 171 | /// Returns an original argument from the stack. |
| 172 | template <typename T> const T &stackRef(unsigned Offset) const { |
| 173 | assert(Args); |
| 174 | return *reinterpret_cast<const T *>(Args - ArgSize + Offset); |
| 175 | } |
| 176 | |
| 177 | /// Returns an offset to a local. |
| 178 | template <typename T> T &localRef(unsigned Offset) const { |
| 179 | return localBlock(Offset)->deref<T>(); |
| 180 | } |
| 181 | |
| 182 | /// Pointer to local memory. |
| 183 | char *locals() const { |
| 184 | return (reinterpret_cast<char *>(const_cast<InterpFrame *>(this))) + |
| 185 | align(Size: sizeof(InterpFrame)); |
| 186 | } |
| 187 | |
| 188 | /// Pointer to argument memory. |
| 189 | char *args() const { |
| 190 | return (reinterpret_cast<char *>(const_cast<InterpFrame *>(this))) + |
| 191 | sizeof(InterpFrame) + Func->getFrameSize(); |
| 192 | } |
| 193 | |
| 194 | /// Returns a pointer to a local's block. |
| 195 | Block *localBlock(unsigned Offset) const { |
| 196 | return reinterpret_cast<Block *>(locals() + Offset - sizeof(Block)); |
| 197 | } |
| 198 | |
| 199 | /// Returns a pointer to an argument block. |
| 200 | Block *argBlock(unsigned Index) const { |
| 201 | unsigned ByteOffset = Func->getParamDescriptor(Index).BlockOffset; |
| 202 | return reinterpret_cast<Block *>(args() + ByteOffset); |
| 203 | } |
| 204 | |
| 205 | /// Returns the inline descriptor of the local. |
| 206 | InlineDescriptor *localInlineDesc(unsigned Offset) const { |
| 207 | return reinterpret_cast<InlineDescriptor *>(locals() + Offset); |
| 208 | } |
| 209 | |
| 210 | private: |
| 211 | /// Reference to the interpreter state. |
| 212 | InterpState &S; |
| 213 | /// Depth of this frame. |
| 214 | unsigned Depth; |
| 215 | /// Reference to the function being executed. |
| 216 | const Function *Func; |
| 217 | /// Offset of the instance pointer. Use with stackRef<>(). |
| 218 | unsigned ThisPointerOffset; |
| 219 | /// Return address. |
| 220 | CodePtr RetPC; |
| 221 | /// The size of all the arguments. |
| 222 | const unsigned ArgSize; |
| 223 | /// Pointer to the arguments in the callee's frame. |
| 224 | char *Args = nullptr; |
| 225 | /// Offset on the stack at entry. |
| 226 | const size_t FrameOffset; |
| 227 | |
| 228 | public: |
| 229 | unsigned MSVCConstexprAllowed = 0; |
| 230 | }; |
| 231 | |
| 232 | } // namespace interp |
| 233 | } // namespace clang |
| 234 | |
| 235 | #endif |
| 236 | |