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