xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/InterpFrame.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 "Pointer.h"
18a7dea167SDimitry Andric #include "Program.h"
19a7dea167SDimitry Andric #include "State.h"
20a7dea167SDimitry Andric #include <cstdint>
21a7dea167SDimitry Andric #include <vector>
22a7dea167SDimitry Andric 
23a7dea167SDimitry Andric namespace clang {
24a7dea167SDimitry Andric namespace interp {
25a7dea167SDimitry Andric class Function;
26a7dea167SDimitry Andric class InterpState;
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.
35a7dea167SDimitry Andric   InterpFrame(InterpState &S, Function *Func, InterpFrame *Caller,
36a7dea167SDimitry Andric               CodePtr RetPC, Pointer &&This);
37a7dea167SDimitry Andric 
38a7dea167SDimitry Andric   /// Destroys the frame, killing all live pointers to stack slots.
39a7dea167SDimitry Andric   ~InterpFrame();
40a7dea167SDimitry Andric 
41a7dea167SDimitry Andric   /// Invokes the destructors for a scope.
42a7dea167SDimitry Andric   void destroy(unsigned Idx);
43a7dea167SDimitry Andric 
44a7dea167SDimitry Andric   /// Pops the arguments off the stack.
45a7dea167SDimitry Andric   void popArgs();
46a7dea167SDimitry Andric 
47a7dea167SDimitry Andric   /// Describes the frame with arguments for diagnostic purposes.
48*5ffd83dbSDimitry Andric   void describe(llvm::raw_ostream &OS) override;
49a7dea167SDimitry Andric 
50a7dea167SDimitry Andric   /// Returns the parent frame object.
51*5ffd83dbSDimitry Andric   Frame *getCaller() const override;
52a7dea167SDimitry Andric 
53a7dea167SDimitry Andric   /// Returns the location of the call to the frame.
54*5ffd83dbSDimitry Andric   SourceLocation getCallLocation() const override;
55a7dea167SDimitry Andric 
56a7dea167SDimitry Andric   /// Returns the caller.
57*5ffd83dbSDimitry Andric   const FunctionDecl *getCallee() const override;
58a7dea167SDimitry Andric 
59a7dea167SDimitry Andric   /// Returns the current function.
60a7dea167SDimitry Andric   Function *getFunction() const { return Func; }
61a7dea167SDimitry Andric 
62a7dea167SDimitry Andric   /// Returns the offset on the stack at which the frame starts.
63a7dea167SDimitry Andric   size_t getFrameOffset() const { return FrameOffset; }
64a7dea167SDimitry Andric 
65a7dea167SDimitry Andric   /// Returns the value of a local variable.
66a7dea167SDimitry Andric   template <typename T> const T &getLocal(unsigned Offset) {
67a7dea167SDimitry Andric     return localRef<T>(Offset);
68a7dea167SDimitry Andric   }
69a7dea167SDimitry Andric 
70a7dea167SDimitry Andric   /// Mutates a local variable.
71a7dea167SDimitry Andric   template <typename T> void setLocal(unsigned Offset, const T &Value) {
72a7dea167SDimitry Andric     localRef<T>(Offset) = Value;
73a7dea167SDimitry Andric   }
74a7dea167SDimitry Andric 
75a7dea167SDimitry Andric   /// Returns a pointer to a local variables.
76a7dea167SDimitry Andric   Pointer getLocalPointer(unsigned Offset);
77a7dea167SDimitry Andric 
78a7dea167SDimitry Andric   /// Returns the value of an argument.
79a7dea167SDimitry Andric   template <typename T> const T &getParam(unsigned Offset) {
80a7dea167SDimitry Andric     auto Pt = Params.find(Offset);
81a7dea167SDimitry Andric     if (Pt == Params.end()) {
82a7dea167SDimitry Andric       return stackRef<T>(Offset);
83a7dea167SDimitry Andric     } else {
84a7dea167SDimitry Andric       return Pointer(reinterpret_cast<Block *>(Pt->second.get())).deref<T>();
85a7dea167SDimitry Andric     }
86a7dea167SDimitry Andric   }
87a7dea167SDimitry Andric 
88a7dea167SDimitry Andric   /// Mutates a local copy of a parameter.
89a7dea167SDimitry Andric   template <typename T> void setParam(unsigned Offset, const T &Value) {
90a7dea167SDimitry Andric      getParamPointer(Offset).deref<T>() = Value;
91a7dea167SDimitry Andric   }
92a7dea167SDimitry Andric 
93a7dea167SDimitry Andric   /// Returns a pointer to an argument - lazily creates a block.
94a7dea167SDimitry Andric   Pointer getParamPointer(unsigned Offset);
95a7dea167SDimitry Andric 
96a7dea167SDimitry Andric   /// Returns the 'this' pointer.
97a7dea167SDimitry Andric   const Pointer &getThis() const { return This; }
98a7dea167SDimitry Andric 
99a7dea167SDimitry Andric   /// Checks if the frame is a root frame - return should quit the interpreter.
100a7dea167SDimitry Andric   bool isRoot() const { return !Func; }
101a7dea167SDimitry Andric 
102a7dea167SDimitry Andric   /// Returns the PC of the frame's code start.
103a7dea167SDimitry Andric   CodePtr getPC() const { return Func->getCodeBegin(); }
104a7dea167SDimitry Andric 
105a7dea167SDimitry Andric   /// Returns the return address of the frame.
106a7dea167SDimitry Andric   CodePtr getRetPC() const { return RetPC; }
107a7dea167SDimitry Andric 
108a7dea167SDimitry Andric   /// Map a location to a source.
109a7dea167SDimitry Andric   virtual SourceInfo getSource(CodePtr PC) const;
110a7dea167SDimitry Andric   const Expr *getExpr(CodePtr PC) const;
111a7dea167SDimitry Andric   SourceLocation getLocation(CodePtr PC) const;
112a7dea167SDimitry Andric 
113a7dea167SDimitry Andric private:
114a7dea167SDimitry Andric   /// Returns an original argument from the stack.
115a7dea167SDimitry Andric   template <typename T> const T &stackRef(unsigned Offset) {
116a7dea167SDimitry Andric     return *reinterpret_cast<const T *>(Args - ArgSize + Offset);
117a7dea167SDimitry Andric   }
118a7dea167SDimitry Andric 
119a7dea167SDimitry Andric   /// Returns an offset to a local.
120a7dea167SDimitry Andric   template <typename T> T &localRef(unsigned Offset) {
121a7dea167SDimitry Andric     return *reinterpret_cast<T *>(Locals.get() + Offset);
122a7dea167SDimitry Andric   }
123a7dea167SDimitry Andric 
124a7dea167SDimitry Andric   /// Returns a pointer to a local's block.
125a7dea167SDimitry Andric   void *localBlock(unsigned Offset) {
126a7dea167SDimitry Andric     return Locals.get() + Offset - sizeof(Block);
127a7dea167SDimitry Andric   }
128a7dea167SDimitry Andric 
129a7dea167SDimitry Andric private:
130a7dea167SDimitry Andric   /// Reference to the interpreter state.
131a7dea167SDimitry Andric   InterpState &S;
132a7dea167SDimitry Andric   /// Reference to the function being executed.
133a7dea167SDimitry Andric   Function *Func;
134a7dea167SDimitry Andric   /// Current object pointer for methods.
135a7dea167SDimitry Andric   Pointer This;
136a7dea167SDimitry Andric   /// Return address.
137a7dea167SDimitry Andric   CodePtr RetPC;
138a7dea167SDimitry Andric   /// The size of all the arguments.
139a7dea167SDimitry Andric   const unsigned ArgSize;
140a7dea167SDimitry Andric   /// Pointer to the arguments in the callee's frame.
141a7dea167SDimitry Andric   char *Args = nullptr;
142a7dea167SDimitry Andric   /// Fixed, initial storage for known local variables.
143a7dea167SDimitry Andric   std::unique_ptr<char[]> Locals;
144a7dea167SDimitry Andric   /// Offset on the stack at entry.
145a7dea167SDimitry Andric   const size_t FrameOffset;
146a7dea167SDimitry Andric   /// Mapping from arg offsets to their argument blocks.
147a7dea167SDimitry Andric   llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Params;
148a7dea167SDimitry Andric };
149a7dea167SDimitry Andric 
150a7dea167SDimitry Andric } // namespace interp
151a7dea167SDimitry Andric } // namespace clang
152a7dea167SDimitry Andric 
153a7dea167SDimitry Andric #endif
154