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