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 "Block.h"
17#include "Frame.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 enableLocal(unsigned Offset);
62 bool isLocalEnabled(unsigned Offset) const {
63 return getLocalBlock(Offset)
65 .IsEnabled;
66 }
67
68 /// Describes the frame with arguments for diagnostic purposes.
69 void describe(llvm::raw_ostream &OS) const override;
70
71 /// Returns the parent frame object.
72 Frame *getCaller() const override { return Caller; }
73
74 /// Returns the location of the call to the frame.
75 SourceRange getCallRange() const override;
76
77 /// Returns the caller.
78 const FunctionDecl *getCallee() const override;
79
80 /// Returns the current function.
81 const Function *getFunction() const { return Func; }
82
83#ifndef NDEBUG
84 /// Returns the offset on the stack at which the frame starts.
85 size_t getFrameOffset() const { return FrameOffset; }
86#endif
87
88 /// Returns the value of a local variable.
89 template <typename T> const T &getLocal(unsigned Offset) const {
90 return localRef<T>(Offset);
91 }
92
93 /// Mutates a local variable.
94 template <typename T> void setLocal(unsigned Offset, const T &Value) {
95 Block *B = getLocalBlock(Offset);
96
97 B->deref<T>() = Value;
98 auto &LID = B->getBlockDesc<LocalInlineDescriptor>();
99 LID.IsInitialized = true;
100 LID.setLifetime(Lifetime::Started);
101 }
102
103 /// Returns a pointer to a local variables.
104 Pointer getLocalPointer(unsigned Offset) const {
105 assert(Offset < Func->getFrameSize() && "Invalid local offset.");
106 return Pointer(getLocalBlock(Offset));
107 }
108 Block *getLocalBlock(unsigned Offset) const {
109 Block *B = localBlock(Offset);
110 if (LLVM_UNLIKELY(!B->isInitialized())) {
111 B->invokeCtor();
112 new (localInlineDesc(Offset)) LocalInlineDescriptor();
113 }
114 return B;
115 };
116
117 /// Returns the value of an argument.
118 template <typename T> const T &getParam(unsigned Index) const {
119 Block *ArgBlock = argBlock(Index);
120 if (!ArgBlock->isInitialized())
121 return stackRef<T>(Func->getParamDescriptor(Index).Offset);
122 return ArgBlock->deref<T>();
123 }
124
125 /// Mutates a local copy of a parameter.
126 template <typename T> void setParam(unsigned Index, const T &Value) {
127 argBlock(Index)->deref<T>() = Value;
128 }
129
130 /// Returns a pointer to an argument - lazily creates a block.
131 Pointer getParamPointer(unsigned Offset);
132
133 bool hasThisPointer() const { return FuncFlags & HasThisFlag; }
134
135 /// Returns the 'this' pointer.
136 const Pointer &getThis() const {
137 assert(hasThisPointer());
138 assert(!isBottomFrame());
139 return stackRef<Pointer>((FuncFlags & HasRVOFlag) ? sizeof(Pointer) : 0);
140 }
141
142 /// Returns the RVO pointer, if the Function has one.
143 const Pointer &getRVOPtr() const {
144 assert(Func);
145 assert(Func->hasRVO());
146 assert(!isBottomFrame());
147 return stackRef<Pointer>(0);
148 }
149
150 CodePtr getRetPC() const { return RetPC; }
151 /// Returns the return address of the opcode in the caller frame.
153 // All the Call ops we have take a Function* and an unsigned.
154 if (RetPC)
155 return RetPC - align(sizeof(void *)) - align(sizeof(unsigned));
156 return RetPC;
157 }
158
159 /// Map a location to a source.
160 SourceInfo getSource(CodePtr PC) const;
161 const Expr *getExpr(CodePtr PC) const { return getSource(PC).asExpr(); }
163 return getSource(PC).getLoc();
164 }
165 SourceRange getRange(CodePtr PC) const { return getSource(PC).getRange(); }
166
167 unsigned getArgSize() const { return ArgSize; }
168
169 bool isStdFunction() const;
170
171 bool isBottomFrame() const { return !Caller; }
172
173 void dump() const { dump(llvm::errs(), 0); }
174 void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const;
175
176private:
177 static constexpr uint8_t HasRVOFlag = 1u << 0u;
178 static constexpr uint8_t HasThisFlag = 1u << 1u;
179
180 /// Returns an original argument from the stack.
181 template <typename T> const T &stackRef(unsigned Offset) const {
182 assert(Args);
183 return *reinterpret_cast<const T *>(Args - ArgSize + Offset);
184 }
185
186 /// Returns an offset to a local.
187 template <typename T> T &localRef(unsigned Offset) const {
188 return localBlock(Offset)->deref<T>();
189 }
190
191 /// Pointer to local memory.
192 char *locals() const {
193 return (reinterpret_cast<char *>(const_cast<InterpFrame *>(this))) +
194 align(sizeof(InterpFrame));
195 }
196
197 /// Pointer to argument memory.
198 char *args() const {
199 return (reinterpret_cast<char *>(const_cast<InterpFrame *>(this))) +
200 sizeof(InterpFrame) + Func->getFrameSize();
201 }
202
203 /// Returns a pointer to a local's block.
204 /// Will NOT initialize the block.
205 Block *localBlock(unsigned Offset) const {
206 return reinterpret_cast<Block *>(locals() + Offset - sizeof(Block));
207 }
208
209 /// Returns a pointer to an argument block.
210 Block *argBlock(unsigned Index) const {
211 unsigned ByteOffset = Func->getParamDescriptor(Index).BlockOffset;
212 return reinterpret_cast<Block *>(args() + ByteOffset);
213 }
214
215 /// Returns the inline descriptor of the local.
216 LocalInlineDescriptor *localInlineDesc(unsigned Offset) const {
217 return reinterpret_cast<LocalInlineDescriptor *>(locals() + Offset);
218 }
219
220public:
221 /// The frame of the previous function.
223
224private:
225 /// Reference to the interpreter state.
226 InterpState &S;
227 /// Reference to the function being executed.
228 const Function *Func;
229 /// Return address.
230 CodePtr RetPC;
231 /// Pointer to the arguments in the callee's frame.
232 char *Args = nullptr;
233#ifndef NDEBUG
234 /// Offset on the stack at entry.
235 size_t FrameOffset = 0;
236#endif
237 /// The size of all the arguments.
238 const unsigned ArgSize;
239
240public:
242
243private:
244 /// Relevant flags about the function.
245 uint8_t FuncFlags = 0;
246};
247
248} // namespace interp
249} // namespace clang
250
251#endif
This represents one expression.
Definition Expr.h:114
Represents a function declaration or definition.
Definition Decl.h:2082
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 Block.h:68
const T & deref() const
Definition Block.h:143
void invokeCtor()
Invokes the constructor.
Definition Block.h:165
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition Block.h:130
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:253
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.
void setParam(unsigned Index, const T &Value)
Mutates a local copy of a parameter.
InterpFrame * Caller
The frame of the previous function.
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
Frame * getCaller() const override
Returns the parent frame object.
Definition InterpFrame.h:72
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
SourceRange getRange(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:81
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition InterpFrame.h:85
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
Definition InterpFrame.h:94
unsigned getArgSize() const
Block * getLocalBlock(unsigned Offset) const
const Expr * getExpr(CodePtr PC) const
const T & getParam(unsigned Index) const
Returns the value of an argument.
bool isLocalEnabled(unsigned Offset) const
Definition InterpFrame.h:62
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.
void enableLocal(unsigned Offset)
std::string getName() const
Definition InterpFrame.h:53
const T & getLocal(unsigned Offset) const
Returns the value of a local variable.
Definition InterpFrame.h:89
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:47
A pointer to a memory block, live or dead.
Definition Pointer.h:544
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:218
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