Thanks to visit codestin.com
Credit goes to clang.llvm.org

clang 22.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 static void free(InterpFrame *F) {
50 if (!F->isBottomFrame())
51 delete F;
52 }
53
54 /// Invokes the destructors for a scope.
55 void destroy(unsigned Idx);
56 void initScope(unsigned Idx);
57 void destroyScopes();
58
59 /// Describes the frame with arguments for diagnostic purposes.
60 void describe(llvm::raw_ostream &OS) const override;
61
62 /// Returns the parent frame object.
63 Frame *getCaller() const override { return Caller; }
64
65 /// Returns the location of the call to the frame.
66 SourceRange getCallRange() const override;
67
68 /// Returns the caller.
69 const FunctionDecl *getCallee() const override;
70
71 /// Returns the current function.
72 const Function *getFunction() const { return Func; }
73
74 /// Returns the offset on the stack at which the frame starts.
75 size_t getFrameOffset() const { return FrameOffset; }
76
77 /// Returns the value of a local variable.
78 template <typename T> const T &getLocal(unsigned Offset) const {
79 return localRef<T>(Offset);
80 }
81
82 /// Mutates a local variable.
83 template <typename T> void setLocal(unsigned Offset, const T &Value) {
84 localRef<T>(Offset) = Value;
85 localInlineDesc(Offset)->IsInitialized = true;
86 }
87
88 /// Returns a pointer to a local variables.
89 Pointer getLocalPointer(unsigned Offset) const;
90 Block *getLocalBlock(unsigned Offset) const;
91
92 /// Returns the value of an argument.
93 template <typename T> const T &getParam(unsigned Offset) const {
94 auto Pt = Params.find(Offset);
95 if (Pt == Params.end())
96 return stackRef<T>(Offset);
97 return reinterpret_cast<const Block *>(Pt->second.get())->deref<T>();
98 }
99
100 /// Mutates a local copy of a parameter.
101 template <typename T> void setParam(unsigned Offset, const T &Value) {
102 getParamPointer(Offset).deref<T>() = Value;
103 }
104
105 /// Returns a pointer to an argument - lazily creates a block.
106 Pointer getParamPointer(unsigned Offset);
107
108 bool hasThisPointer() const { return Func && Func->hasThisPointer(); }
109 /// Returns the 'this' pointer.
110 const Pointer &getThis() const {
111 assert(hasThisPointer());
112 return stackRef<Pointer>(ThisPointerOffset);
113 }
114
115 /// Returns the RVO pointer, if the Function has one.
116 const Pointer &getRVOPtr() const {
117 assert(Func);
118 assert(Func->hasRVO());
119 return stackRef<Pointer>(0);
120 }
121
122 /// Checks if the frame is a root frame - return should quit the interpreter.
123 bool isRoot() const { return !Func; }
124
125 /// Returns the PC of the frame's code start.
126 CodePtr getPC() const { return Func->getCodeBegin(); }
127
128 /// Returns the return address of the frame.
129 CodePtr getRetPC() const { return RetPC; }
130
131 /// Map a location to a source.
132 SourceInfo getSource(CodePtr PC) const;
133 const Expr *getExpr(CodePtr PC) const;
135 SourceRange getRange(CodePtr PC) const;
136
137 unsigned getDepth() const { return Depth; }
138
139 bool isStdFunction() const;
140
141 bool isBottomFrame() const { return !Caller; }
142
143 void dump() const { dump(llvm::errs(), 0); }
144 void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const;
145
146private:
147 /// Returns an original argument from the stack.
148 template <typename T> const T &stackRef(unsigned Offset) const {
149 assert(Args);
150 return *reinterpret_cast<const T *>(Args - ArgSize + Offset);
151 }
152
153 /// Returns an offset to a local.
154 template <typename T> T &localRef(unsigned Offset) const {
155 return localBlock(Offset)->deref<T>();
156 }
157
158 /// Returns a pointer to a local's block.
159 Block *localBlock(unsigned Offset) const {
160 return reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block));
161 }
162
163 /// Returns the inline descriptor of the local.
164 InlineDescriptor *localInlineDesc(unsigned Offset) const {
165 return reinterpret_cast<InlineDescriptor *>(Locals.get() + Offset);
166 }
167
168private:
169 /// Reference to the interpreter state.
170 InterpState &S;
171 /// Depth of this frame.
172 unsigned Depth;
173 /// Reference to the function being executed.
174 const Function *Func;
175 /// Offset of the instance pointer. Use with stackRef<>().
176 unsigned ThisPointerOffset;
177 /// Return address.
178 CodePtr RetPC;
179 /// The size of all the arguments.
180 const unsigned ArgSize;
181 /// Pointer to the arguments in the callee's frame.
182 char *Args = nullptr;
183 /// Fixed, initial storage for known local variables.
184 std::unique_ptr<char[]> Locals;
185 /// Offset on the stack at entry.
186 const size_t FrameOffset;
187 /// Mapping from arg offsets to their argument blocks.
188 llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Params;
189};
190
191} // namespace interp
192} // namespace clang
193
194#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:1999
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
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:86
static void free(InterpFrame *F)
Definition InterpFrame.h:49
InterpFrame(InterpState &S)
Bottom Frame.
InterpFrame * Caller
The frame of the previous function.
Definition InterpFrame.h:30
Frame * getCaller() const override
Returns the parent frame object.
Definition InterpFrame.h:63
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
CodePtr getRetPC() const
Returns the return address of the frame.
Block * getLocalBlock(unsigned Offset) const
CodePtr getPC() const
Returns the PC of the frame's code start.
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:72
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition InterpFrame.h:75
SourceRange getRange(CodePtr PC) const
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
Definition InterpFrame.h:83
const T & getParam(unsigned Offset) const
Returns the value of an argument.
Definition InterpFrame.h:93
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
void setParam(unsigned Offset, const T &Value)
Mutates a local copy of a parameter.
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 initScope(unsigned Idx)
const T & getLocal(unsigned Offset) const
Returns the value of a local variable.
Definition InterpFrame.h:78
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.
Interpreter context.
Definition InterpState.h:43
A pointer to a memory block, live or dead.
Definition Pointer.h:91
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:662
Describes the statement/declaration an opcode was generated from.
Definition Source.h:73
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
const FunctionProtoType * T