clang 20.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 "Program.h"
18
19namespace clang {
20namespace interp {
21class Function;
22class InterpState;
23class Pointer;
24
25/// Frame storing local variables.
26class InterpFrame final : public Frame {
27public:
28 /// The frame of the previous function.
30
31 /// Creates a new frame for a method call.
33 CodePtr RetPC, unsigned ArgSize);
34
35 /// Creates a new frame with the values that make sense.
36 /// I.e., the caller is the current frame of S,
37 /// the This() pointer is the current Pointer on the top of S's stack,
38 /// and the RVO pointer is before that.
39 InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC,
40 unsigned VarArgSize = 0);
41
42 /// Destroys the frame, killing all live pointers to stack slots.
44
45 /// Invokes the destructors for a scope.
46 void destroy(unsigned Idx);
47 void initScope(unsigned Idx);
48
49 /// Pops the arguments off the stack.
50 void popArgs();
51
52 /// Describes the frame with arguments for diagnostic purposes.
53 void describe(llvm::raw_ostream &OS) const override;
54
55 /// Returns the parent frame object.
56 Frame *getCaller() const override;
57
58 /// Returns the location of the call to the frame.
59 SourceRange getCallRange() const override;
60
61 /// Returns the caller.
62 const FunctionDecl *getCallee() const override;
63
64 /// Returns the current function.
65 const Function *getFunction() const { return Func; }
66
67 /// Returns the offset on the stack at which the frame starts.
68 size_t getFrameOffset() const { return FrameOffset; }
69
70 /// Returns the value of a local variable.
71 template <typename T> const T &getLocal(unsigned Offset) const {
72 return localRef<T>(Offset);
73 }
74
75 /// Mutates a local variable.
76 template <typename T> void setLocal(unsigned Offset, const T &Value) {
77 localRef<T>(Offset) = Value;
78 localInlineDesc(Offset)->IsInitialized = true;
79 }
80
81 /// Returns a pointer to a local variables.
82 Pointer getLocalPointer(unsigned Offset) const;
83
84 /// Returns the value of an argument.
85 template <typename T> const T &getParam(unsigned Offset) const {
86 auto Pt = Params.find(Offset);
87 if (Pt == Params.end())
88 return stackRef<T>(Offset);
89 return Pointer(reinterpret_cast<Block *>(Pt->second.get())).deref<T>();
90 }
91
92 /// Mutates a local copy of a parameter.
93 template <typename T> void setParam(unsigned Offset, const T &Value) {
94 getParamPointer(Offset).deref<T>() = Value;
95 }
96
97 /// Returns a pointer to an argument - lazily creates a block.
98 Pointer getParamPointer(unsigned Offset);
99
100 /// Returns the 'this' pointer.
101 const Pointer &getThis() const { return This; }
102
103 /// Returns the RVO pointer, if the Function has one.
104 const Pointer &getRVOPtr() const { return RVOPtr; }
105
106 /// Checks if the frame is a root frame - return should quit the interpreter.
107 bool isRoot() const { return !Func; }
108
109 /// Returns the PC of the frame's code start.
110 CodePtr getPC() const { return Func->getCodeBegin(); }
111
112 /// Returns the return address of the frame.
113 CodePtr getRetPC() const { return RetPC; }
114
115 /// Map a location to a source.
116 virtual SourceInfo getSource(CodePtr PC) const;
117 const Expr *getExpr(CodePtr PC) const;
119 SourceRange getRange(CodePtr PC) const;
120
121 unsigned getDepth() const { return Depth; }
122
123 void dump() const { dump(llvm::errs(), 0); }
124 void dump(llvm::raw_ostream &OS, unsigned Indent = 0) const;
125
126private:
127 /// Returns an original argument from the stack.
128 template <typename T> const T &stackRef(unsigned Offset) const {
129 assert(Args);
130 return *reinterpret_cast<const T *>(Args - ArgSize + Offset);
131 }
132
133 /// Returns an offset to a local.
134 template <typename T> T &localRef(unsigned Offset) const {
135 return getLocalPointer(Offset).deref<T>();
136 }
137
138 /// Returns a pointer to a local's block.
139 Block *localBlock(unsigned Offset) const {
140 return reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block));
141 }
142
143 /// Returns the inline descriptor of the local.
144 InlineDescriptor *localInlineDesc(unsigned Offset) const {
145 return reinterpret_cast<InlineDescriptor *>(Locals.get() + Offset);
146 }
147
148private:
149 /// Reference to the interpreter state.
150 InterpState &S;
151 /// Depth of this frame.
152 unsigned Depth;
153 /// Reference to the function being executed.
154 const Function *Func;
155 /// Current object pointer for methods.
156 Pointer This;
157 /// Pointer the non-primitive return value gets constructed in.
158 Pointer RVOPtr;
159 /// Return address.
160 CodePtr RetPC;
161 /// The size of all the arguments.
162 const unsigned ArgSize;
163 /// Pointer to the arguments in the callee's frame.
164 char *Args = nullptr;
165 /// Fixed, initial storage for known local variables.
166 std::unique_ptr<char[]> Locals;
167 /// Offset on the stack at entry.
168 const size_t FrameOffset;
169 /// Mapping from arg offsets to their argument blocks.
170 llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Params;
171};
172
173} // namespace interp
174} // namespace clang
175
176#endif
const CFGBlock * Block
Definition: HTMLLogger.cpp:153
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1932
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:49
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:81
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.h:91
Frame storing local variables.
Definition: InterpFrame.h:26
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:99
const Expr * getExpr(CodePtr PC) const
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:29
virtual SourceInfo getSource(CodePtr PC) const
Map a location to a source.
CodePtr getRetPC() const
Returns the return address of the frame.
Definition: InterpFrame.h:113
CodePtr getPC() const
Returns the PC of the frame's code start.
Definition: InterpFrame.h:110
SourceLocation getLocation(CodePtr PC) const
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
Definition: InterpFrame.cpp:69
const Pointer & getThis() const
Returns the 'this' pointer.
Definition: InterpFrame.h:101
const Function * getFunction() const
Returns the current function.
Definition: InterpFrame.h:65
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition: InterpFrame.h:68
SourceRange getRange(CodePtr PC) const
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
Definition: InterpFrame.h:76
const T & getParam(unsigned Offset) const
Returns the value of an argument.
Definition: InterpFrame.h:85
bool isRoot() const
Checks if the frame is a root frame - return should quit the interpreter.
Definition: InterpFrame.h:107
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
unsigned getDepth() const
Definition: InterpFrame.h:121
Frame * getCaller() const override
Returns the parent frame object.
void setParam(unsigned Offset, const T &Value)
Mutates a local copy of a parameter.
Definition: InterpFrame.h:93
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:93
const Pointer & getRVOPtr() const
Returns the RVO pointer, if the Function has one.
Definition: InterpFrame.h:104
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)
Definition: InterpFrame.cpp:85
const T & getLocal(unsigned Offset) const
Returns the value of a local variable.
Definition: InterpFrame.h:71
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:36
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
T & deref() const
Dereferences the pointer, if it's live.
Definition: Pointer.h:623
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition: Descriptor.h:82