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