clang  10.0.0svn
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.
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.
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;
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
Represents a function declaration or definition.
Definition: Decl.h:1784
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
Pointer into the code segment.
Definition: Source.h:25
SourceLocation getLocation(CodePtr PC) const
Frame storing local variables.
Definition: InterpFrame.h:29
Pointer getLocalPointer(unsigned Offset)
Returns a pointer to a local variables.
constexpr XRayInstrMask Function
Definition: XRayInstr.h:38
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:81
A pointer to a memory block, live or dead.
Definition: Pointer.h:39
const T & getLocal(unsigned Offset)
Returns the value of a local variable.
Definition: InterpFrame.h:66
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:32
A memory block, either on the stack or in the heap.
Definition: Block.h:37
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
Definition: InterpFrame.h:71
Function * getFunction() const
Returns the current function.
Definition: InterpFrame.h:60
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:45
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.cpp:24
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:51
unsigned Offset
Definition: Format.cpp:1809
This represents one expression.
Definition: Expr.h:108
virtual SourceInfo getSource(CodePtr PC) const
Map a location to a source.
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
Definition: InterpFrame.cpp:38
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition: InterpFrame.h:63
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Encodes a location in the source.
Bytecode function.
Definition: Function.h:59
const Expr * getExpr(CodePtr PC) const
InterpFrame(InterpState &S, Function *Func, InterpFrame *Caller, CodePtr RetPC, Pointer &&This)
Creates a new frame for a method call.
Definition: InterpFrame.cpp:20
Dataflow Directional Tag Classes.
CodePtr getPC() const
Returns the PC of the frame&#39;s code start.
Definition: InterpFrame.h:103
const T & getParam(unsigned Offset)
Returns the value of an argument.
Definition: InterpFrame.h:79
const FunctionDecl * getCallee() const
Returns the caller.
T & deref() const
Dereferences the pointer, if it&#39;s live.
Definition: Pointer.h:280
Interpreter context.
Definition: InterpState.h:34
void setParam(unsigned Offset, const T &Value)
Mutates a local copy of a parameter.
Definition: InterpFrame.h:89
const Pointer & getThis() const
Returns the &#39;this&#39; pointer.
Definition: InterpFrame.h:97
Frame * getCaller() const
Returns the parent frame object.
bool isRoot() const
Checks if the frame is a root frame - return should quit the interpreter.
Definition: InterpFrame.h:100
SourceLocation getCallLocation() const
Returns the location of the call to the frame.
void describe(llvm::raw_ostream &OS)
Describes the frame with arguments for diagnostic purposes.
CodePtr getRetPC() const
Returns the return address of the frame.
Definition: InterpFrame.h:106