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