clang 24.0.0git
InterpFrame.h
Go to the documentation of this file.
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 "InterpBlock.h"
18#include "Pointer.h"
19
20namespace clang {
21namespace interp {
22class Function;
23class InterpState;
24class Pointer;
25
26/// Frame storing local variables.
27class InterpFrame final : public Frame {
28public:
29 /// The frame of the previous function.
31
32 /// Bottom Frame.
34
35 /// Creates a new frame for a method call.
37 CodePtr RetPC, unsigned ArgSize);
38
39 /// Creates a new frame with the values that make sense.
40 /// I.e., the caller is the current frame of S,
41 /// the This() pointer is the current Pointer on the top of S's stack,
42 /// and the RVO pointer is before that.
43 InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC,
44 unsigned VarArgSize = 0);
45
46 /// Destroys the frame, killing all live pointers to stack slots.
48
49 /// Returns the number of bytes needed to allocate an InterpFrame for the
50 /// given function.
51 static size_t allocSize(const Function *F) {
52 return sizeof(InterpFrame) + F->getFrameSize() +
53 (F->getArgSize() + (sizeof(Block) * F->getNumWrittenParams()));
54 }
55
56 std::string getName() const {
57 if (!Func)
58 return "Bottom frame";
59 return Func->getName();
60 }
61
62 static void free(InterpFrame *F) {
63 if (!F->isBottomFrame()) {
64 F->~InterpFrame();
65 delete[] reinterpret_cast<char *>(F);
66 } else {
67 F->~InterpFrame();
68 }
69 }
70
71 /// Invokes the destructors for a scope.
72 void destroy(unsigned Idx);
73 void initScope(unsigned Idx);
74 void destroyScopes();
75 void enableLocal(unsigned Idx);
76 bool isLocalEnabled(unsigned Idx) const {
77 return localInlineDesc(Idx)->IsActive;
78 }
79
80 /// Describes the frame with arguments for diagnostic purposes.
81 void describe(llvm::raw_ostream &OS) const override;
82
83 /// Returns the parent frame object.
84 Frame *getCaller() const override { return Caller; }
85
86 /// Returns the location of the call to the frame.
87 SourceRange getCallRange() const override;
88
89 /// Returns the caller.
90 const FunctionDecl *getCallee() const override;
91
92 /// Returns the current function.
93 const Function *getFunction() const { return Func; }
94
95#ifndef NDEBUG
96 /// Returns the offset on the stack at which the frame starts.
97 size_t getFrameOffset() const { return FrameOffset; }
98#endif
99
100 /// Returns the value of a local variable.
101 template <typename T> const T &getLocal(unsigned Offset) const {
102 return localRef<T>(Offset);
103 }
104
105 /// Mutates a local variable.
106 template <typename T> void setLocal(unsigned Offset, const T &Value) {
107 localRef<T>(Offset) = Value;
108 localInlineDesc(Offset)->IsInitialized = true;
109 localInlineDesc(Offset)->LifeState = Lifetime::Started;
110 }
111
112 /// Returns a pointer to a local variables.
113 Pointer getLocalPointer(unsigned Offset) const;
114 Block *getLocalBlock(unsigned Offset) const;
115
116 /// Returns the value of an argument.
117 template <typename T> const T &getParam(unsigned Index) const {
118 Block *ArgBlock = argBlock(Index);
119 if (!ArgBlock->isInitialized())
120 return stackRef<T>(Func->getParamDescriptor(Index).Offset);
121 return ArgBlock->deref<T>();
122 }
123
124 /// Mutates a local copy of a parameter.
125 template <typename T> void setParam(unsigned Index, const T &Value) {
126 argBlock(Index)->deref<T>() = Value;
127 }
128
129 /// Returns a pointer to an argument - lazily creates a block.
130 Pointer getParamPointer(unsigned Offset);
131
132 bool hasThisPointer() const { return FuncFlags & HasThisFlag; }
133
134 /// Returns the 'this' pointer.
135 const Pointer &getThis() const {
136 assert(hasThisPointer());
137 assert(!isBottomFrame());
138 return stackRef<Pointer>((FuncFlags & HasRVOFlag) ? sizeof(Pointer) : 0);
139 }
140
141 /// Returns the RVO pointer, if the Function has one.
142 const Pointer &getRVOPtr() const {
143 assert(Func);
144 assert(Func->hasRVO());
145 assert(!isBottomFrame());
146 return stackRef<Pointer>(0);
147 }
148
149 /// Checks if the frame is a root frame - return should quit the interpreter.
150 bool isRoot() const { return !Func; }
151
152 /// Returns the return address of the frame.
153 CodePtr getRetPC() const { return RetPC; }
154 /// Returns the return address of the opcode in the caller frame.
156 // All the Call ops we have take a Function* and an unsigned.
157 if (RetPC)
158 return RetPC - align(sizeof(void *)) - align(sizeof(unsigned));
159 return RetPC;
160 }
161
162 /// Map a location to a source.
163 SourceInfo getSource(CodePtr PC) const;
164 const Expr *getExpr(CodePtr PC) const;
166 SourceRange getRange(CodePtr PC) const;
167
168 unsigned getDepth() const { return Depth; }
169 unsigned getArgSize() const { return ArgSize; }
170
171 bool isStdFunction() const;
172
173 bool isBottomFrame() const { return !Caller; }
174
175 void dump() const { dump(llvm::errs(), 0); }
176 void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const;
177
178private:
179 static constexpr uint8_t HasRVOFlag = 1u << 0u;
180 static constexpr uint8_t HasThisFlag = 1u << 1u;
181
182 /// Returns an original argument from the stack.
183 template <typename T> const T &stackRef(unsigned Offset) const {
184 assert(Args);
185 return *reinterpret_cast<const T *>(Args - ArgSize + Offset);
186 }
187
188 /// Returns an offset to a local.
189 template <typename T> T &localRef(unsigned Offset) const {
190 return localBlock(Offset)->deref<T>();
191 }
192
193 /// Pointer to local memory.
194 char *locals() const {
195 return (reinterpret_cast<char *>(const_cast<InterpFrame *>(this))) +
196 align(sizeof(InterpFrame));
197 }
198
199 /// Pointer to argument memory.
200 char *args() const {
201 return (reinterpret_cast<char *>(const_cast<InterpFrame *>(this))) +
202 sizeof(InterpFrame) + Func->getFrameSize();
203 }
204
205 /// Returns a pointer to a local's block.
206 Block *localBlock(unsigned Offset) const {
207 return reinterpret_cast<Block *>(locals() + Offset - sizeof(Block));
208 }
209
210 /// Returns a pointer to an argument block.
211 Block *argBlock(unsigned Index) const {
212 unsigned ByteOffset = Func->getParamDescriptor(Index).BlockOffset;
213 return reinterpret_cast<Block *>(args() + ByteOffset);
214 }
215
216 /// Returns the inline descriptor of the local.
217 InlineDescriptor *localInlineDesc(unsigned Offset) const {
218 return reinterpret_cast<InlineDescriptor *>(locals() + Offset);
219 }
220
221private:
222 /// Reference to the interpreter state.
223 InterpState &S;
224 /// Depth of this frame.
225 unsigned Depth;
226 /// Reference to the function being executed.
227 const Function *Func;
228 /// Return address.
229 CodePtr RetPC;
230 /// The size of all the arguments.
231 const unsigned ArgSize;
232 /// Pointer to the arguments in the callee's frame.
233 char *Args = nullptr;
234#ifndef NDEBUG
235 /// Offset on the stack at entry.
236 size_t FrameOffset = 0;
237#endif
238
239public:
241
242private:
243 /// Relevant flags about the function.
244 uint8_t FuncFlags = 0;
245};
246
247} // namespace interp
248} // namespace clang
249
250#endif
Expr * getExpr()
Get 'expr' part of the associated expression/statement.
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2029
Encodes a location in the source.
A trivial tuple used to represent a source range.
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
const T & deref() const
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:92
Pointer into the code segment.
Definition Source.h:30
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:99
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition Function.h:124
unsigned getNumWrittenParams() const
Returns the number of parameter this function takes when it's called, i.e excluding the instance poin...
Definition Function.h:247
unsigned getArgSize() const
Returns the size of the argument stack.
Definition Function.h:126
CodePtr getRetOpPC() const
Returns the return address of the opcode in the caller frame.
static void free(InterpFrame *F)
Definition InterpFrame.h:62
InterpFrame(InterpState &S)
Bottom Frame.
bool isLocalEnabled(unsigned Idx) const
Definition InterpFrame.h:76
void setParam(unsigned Index, const T &Value)
Mutates a local copy of a parameter.
InterpFrame * Caller
The frame of the previous function.
Definition InterpFrame.h:30
Frame * getCaller() const override
Returns the parent frame object.
Definition InterpFrame.h:84
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
CodePtr getRetPC() const
Returns the return address of the frame.
void enableLocal(unsigned Idx)
Block * getLocalBlock(unsigned Offset) const
SourceLocation getLocation(CodePtr PC) const
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
const Pointer & getThis() const
Returns the 'this' pointer.
const Function * getFunction() const
Returns the current function.
Definition InterpFrame.h:93
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition InterpFrame.h:97
SourceRange getRange(CodePtr PC) const
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
unsigned getArgSize() const
bool isRoot() const
Checks if the frame is a root frame - return should quit the interpreter.
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
unsigned getDepth() const
const T & getParam(unsigned Index) const
Returns the value of an argument.
void destroy(unsigned Idx)
Invokes the destructors for a scope.
const Pointer & getRVOPtr() const
Returns the RVO pointer, if the Function has one.
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
const FunctionDecl * getCallee() const override
Returns the caller.
std::string getName() const
Definition InterpFrame.h:56
void initScope(unsigned Idx)
const T & getLocal(unsigned Offset) const
Returns the value of a local variable.
SourceRange getCallRange() const override
Returns the location of the call to the frame.
void describe(llvm::raw_ostream &OS) const override
Describes the frame with arguments for diagnostic purposes.
static size_t allocSize(const Function *F)
Returns the number of bytes needed to allocate an InterpFrame for the given function.
Definition InterpFrame.h:51
Interpreter context.
Definition InterpState.h:43
A pointer to a memory block, live or dead.
Definition Pointer.h:405
Describes the statement/declaration an opcode was generated from.
Definition Source.h:76
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:201
The JSON file list parser is used to communicate input to InstallAPI.
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition JsonSupport.h:21
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t