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