clang 10.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 "Pointer.h"
18 #include "Program.h"
19 #include "State.h"
20 #include <cstdint>
21 #include <vector>
22
23 namespace clang {
24 namespace interp {
25 class Function;
26 class InterpState;
27
28 /// Frame storing local variables.
29 class InterpFrame final : public Frame {
30 public:
31 /// The frame of the previous function.
32 InterpFrame *Caller;
33
34 /// Creates a new frame for a method call.
35 InterpFrame(InterpState &S, Function *Func, InterpFrame *Caller,
36 CodePtr RetPC, Pointer &&This);
37
38 /// Destroys the frame, killing all live pointers to stack slots.
39 ~InterpFrame();
40
41 /// Invokes the destructors for a scope.
42 void destroy(unsigned Idx);
43
44 /// Pops the arguments off the stack.
45 void popArgs();
46
47 /// Describes the frame with arguments for diagnostic purposes.
48 void describe(llvm::raw_ostream &OS);
49
50 /// Returns the parent frame object.
51 Frame *getCaller() const;
52
53 /// Returns the location of the call to the frame.
54 SourceLocation getCallLocation() const;
55
56 /// Returns the caller.
57 const FunctionDecl *getCallee() const;
58
59 /// Returns the current function.
60 Function *getFunction() const { return Func; }
61
62 /// Returns the offset on the stack at which the frame starts.
63 size_t getFrameOffset() const { return FrameOffset; }
64
65 /// Returns the value of a local variable.
66 template <typename T> const T &getLocal(unsigned Offset) {
67 return localRef<T>(Offset);
68 }
69
70 /// Mutates a local variable.
71 template <typename T> void setLocal(unsigned Offset, const T &Value) {
72 localRef<T>(Offset) = Value;
73 }
74
75 /// Returns a pointer to a local variables.
76 Pointer getLocalPointer(unsigned Offset);
77
78 /// Returns the value of an argument.
79 template <typename T> const T &getParam(unsigned Offset) {
80 auto Pt = Params.find(Offset);
81 if (Pt == Params.end()) {
82 return stackRef<T>(Offset);
83 } else {
84 return Pointer(reinterpret_cast<Block *>(Pt->second.get())).deref<T>();
85 }
86 }
87
88 /// Mutates a local copy of a parameter.
89 template <typename T> void setParam(unsigned Offset, const T &Value) {
90 getParamPointer(Offset).deref<T>() = Value;
91 }
92
93 /// Returns a pointer to an argument - lazily creates a block.
94 Pointer getParamPointer(unsigned Offset);
95
96 /// Returns the 'this' pointer.
97 const Pointer &getThis() const { return This; }
98
99 /// Checks if the frame is a root frame - return should quit the interpreter.
100 bool isRoot() const { return !Func; }
101
102 /// Returns the PC of the frame's code start.
103 CodePtr getPC() const { return Func->getCodeBegin(); }
104
105 /// Returns the return address of the frame.
106 CodePtr getRetPC() const { return RetPC; }
107
108 /// Map a location to a source.
109 virtual SourceInfo getSource(CodePtr PC) const;
110 const Expr *getExpr(CodePtr PC) const;
111 SourceLocation getLocation(CodePtr PC) const;
112
113 private:
114 /// Returns an original argument from the stack.
115 template <typename T> const T &stackRef(unsigned Offset) {
116 return *reinterpret_cast<const T *>(Args - ArgSize + Offset);
117 }
118
119 /// Returns an offset to a local.
120 template <typename T> T &localRef(unsigned Offset) {
121 return *reinterpret_cast<T *>(Locals.get() + Offset);
122 }
123
124 /// Returns a pointer to a local's block.
125 void *localBlock(unsigned Offset) {
126 return Locals.get() + Offset - sizeof(Block);
127 }
128
129 private:
130 /// Reference to the interpreter state.
131 InterpState &S;
132 /// Reference to the function being executed.
133 Function *Func;
134 /// Current object pointer for methods.
135 Pointer This;
136 /// Return address.
137 CodePtr RetPC;
138 /// The size of all the arguments.
139 const unsigned ArgSize;
140 /// Pointer to the arguments in the callee's frame.
141 char *Args = nullptr;
142 /// Fixed, initial storage for known local variables.
143 std::unique_ptr<char[]> Locals;
144 /// Offset on the stack at entry.
145 const size_t FrameOffset;
146 /// Mapping from arg offsets to their argument blocks.
147 llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Params;
148 };
149
150 } // namespace interp
151 } // namespace clang
152
153 #endif
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1783
clang::interp::InterpFrame::getParamPointer
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
Definition: InterpFrame.cpp:161
clang::interp::CodePtr
Pointer into the code segment.
Definition: Source.h:25
clang::interp::InterpFrame::getLocation
SourceLocation getLocation(CodePtr PC) const
Definition: InterpFrame.cpp:190
clang::interp::InterpFrame
Frame storing local variables.
Definition: InterpFrame.h:29
clang::interp::InterpFrame::getLocalPointer
Pointer getLocalPointer(unsigned Offset)
Returns a pointer to a local variables.
Definition: InterpFrame.cpp:155
clang::XRayInstrKind::Function
constexpr XRayInstrMask Function
Definition: XRayInstr.h:38
Value
Value
Definition: UninitializedValues.cpp:101
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:81
clang::interp::Pointer
A pointer to a memory block, live or dead.
Definition: Pointer.h:39
clang::interp::InterpFrame::getLocal
const T & getLocal(unsigned Offset)
Returns the value of a local variable.
Definition: InterpFrame.h:66
clang::interp::InterpFrame::Caller
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:32
clang::interp::Block
A memory block, either on the stack or in the heap.
Definition: Block.h:37
clang::interp::InterpFrame::setLocal
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
Definition: InterpFrame.h:71
clang::interp::InterpFrame::getFunction
Function * getFunction() const
Returns the current function.
Definition: InterpFrame.h:60
clang::interp::InterpFrame::destroy
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:45
clang::interp::Function::getCodeBegin
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.cpp:24
clang::interp::InterpFrame::popArgs
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:51
Offset
unsigned Offset
Definition: Format.cpp:1827
clang::Expr
This represents one expression.
Definition: Expr.h:108
Frame.h
clang::interp::InterpFrame::getSource
virtual SourceInfo getSource(CodePtr PC) const
Map a location to a source.
Definition: InterpFrame.cpp:182
clang::interp::InterpFrame::~InterpFrame
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
Definition: InterpFrame.cpp:38
clang::interp::InterpFrame::getFrameOffset
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition: InterpFrame.h:63
Program.h
clang::interp::Frame
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::interp::Function
Bytecode function.
Definition: Function.h:59
clang::interp::InterpFrame::getExpr
const Expr * getExpr(CodePtr PC) const
Definition: InterpFrame.cpp:186
clang::interp::InterpFrame::InterpFrame
InterpFrame(InterpState &S, Function *Func, InterpFrame *Caller, CodePtr RetPC, Pointer &&This)
Creates a new frame for a method call.
Definition: InterpFrame.cpp:20
State.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::interp::InterpFrame::getPC
CodePtr getPC() const
Returns the PC of the frame&#39;s code start.
Definition: InterpFrame.h:103
clang::interp::InterpFrame::getParam
const T & getParam(unsigned Offset)
Returns the value of an argument.
Definition: InterpFrame.h:79
Pointer.h
clang::interp::InterpFrame::getCallee
const FunctionDecl * getCallee() const
Returns the caller.
Definition: InterpFrame.cpp:151
clang::interp::Pointer::deref
T & deref() const
Dereferences the pointer, if it&#39;s live.
Definition: Pointer.h:280
clang::interp::InterpState
Interpreter context.
Definition: InterpState.h:34
clang::interp::InterpFrame::setParam
void setParam(unsigned Offset, const T &Value)
Mutates a local copy of a parameter.
Definition: InterpFrame.h:89
clang::interp::InterpFrame::getThis
const Pointer & getThis() const
Returns the &#39;this&#39; pointer.
Definition: InterpFrame.h:97
clang::interp::InterpFrame::getCaller
Frame * getCaller() const
Returns the parent frame object.
Definition: InterpFrame.cpp:139
clang::interp::InterpFrame::isRoot
bool isRoot() const
Checks if the frame is a root frame - return should quit the interpreter.
Definition: InterpFrame.h:100
clang::interp::InterpFrame::getCallLocation
SourceLocation getCallLocation() const
Returns the location of the call to the frame.
Definition: InterpFrame.cpp:145
clang::interp::InterpFrame::describe
void describe(llvm::raw_ostream &OS)
Describes the frame with arguments for diagnostic purposes.
Definition: InterpFrame.cpp:112
clang::interp::InterpFrame::getRetPC
CodePtr getRetPC() const
Returns the return address of the frame.
Definition: InterpFrame.h:106