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