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