clang  16.0.0git
InterpFrame.cpp
Go to the documentation of this file.
1 //===--- InterpFrame.cpp - 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 #include "InterpFrame.h"
10 #include "Function.h"
11 #include "InterpStack.h"
12 #include "InterpState.h"
13 #include "Pointer.h"
14 #include "PrimType.h"
15 #include "Program.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclCXX.h"
18 
19 using namespace clang;
20 using namespace clang::interp;
21 
23  InterpFrame *Caller, CodePtr RetPC, Pointer &&This)
24  : Caller(Caller), S(S), Func(Func), This(std::move(This)), RetPC(RetPC),
25  ArgSize(Func ? Func->getArgSize() : 0),
26  Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) {
27  if (Func) {
28  if (unsigned FrameSize = Func->getFrameSize()) {
29  Locals = std::make_unique<char[]>(FrameSize);
30  for (auto &Scope : Func->scopes()) {
31  for (auto &Local : Scope.locals()) {
32  Block *B = new (localBlock(Local.Offset)) Block(Local.Desc);
33  B->invokeCtor();
34  }
35  }
36  }
37  }
38 }
39 
41  : Caller(S.Current), S(S), Func(Func), RetPC(RetPC),
42  ArgSize(Func ? Func->getArgSize() : 0),
43  Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) {
44  assert(Func);
45 
46  // As per our calling convention, the this pointer is
47  // part of the ArgSize.
48  // If the function has RVO, the RVO pointer is first.
49  // If the fuction has a This pointer, that one is next.
50  // Then follow the actual arguments (but those are handled
51  // in getParamPointer()).
52  if (Func->hasRVO())
53  RVOPtr = stackRef<Pointer>(0);
54 
55  if (Func->hasThisPointer()) {
56  if (Func->hasRVO())
57  This = stackRef<Pointer>(sizeof(Pointer));
58  else
59  This = stackRef<Pointer>(0);
60  }
61 
62  if (unsigned FrameSize = Func->getFrameSize()) {
63  Locals = std::make_unique<char[]>(FrameSize);
64  for (auto &Scope : Func->scopes()) {
65  for (auto &Local : Scope.locals()) {
66  Block *B = new (localBlock(Local.Offset)) Block(Local.Desc);
67  B->invokeCtor();
68  }
69  }
70  }
71 }
72 
74  if (Func && Func->isConstructor() && This.isBaseClass())
75  This.initialize();
76  for (auto &Param : Params)
77  S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
78 }
79 
80 void InterpFrame::destroy(unsigned Idx) {
81  for (auto &Local : Func->getScope(Idx).locals()) {
82  S.deallocate(reinterpret_cast<Block *>(localBlock(Local.Offset)));
83  }
84 }
85 
87  for (PrimType Ty : Func->args_reverse())
88  TYPE_SWITCH(Ty, S.Stk.discard<T>());
89 }
90 
91 template <typename T>
92 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
93  OS << V;
94 }
95 
96 template <>
97 void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
98  QualType Ty) {
99  if (P.isZero()) {
100  OS << "nullptr";
101  return;
102  }
103 
104  auto printDesc = [&OS, &Ctx](Descriptor *Desc) {
105  if (auto *D = Desc->asDecl()) {
106  // Subfields or named values.
107  if (auto *VD = dyn_cast<ValueDecl>(D)) {
108  OS << *VD;
109  return;
110  }
111  // Base classes.
112  if (isa<RecordDecl>(D)) {
113  return;
114  }
115  }
116  // Temporary expression.
117  if (auto *E = Desc->asExpr()) {
118  E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
119  return;
120  }
121  llvm_unreachable("Invalid descriptor type");
122  };
123 
124  if (!Ty->isReferenceType())
125  OS << "&";
127  for (Pointer F = P; !F.isRoot(); ) {
128  Levels.push_back(F);
129  F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
130  }
131 
132  printDesc(P.getDeclDesc());
133  for (auto It = Levels.rbegin(); It != Levels.rend(); ++It) {
134  if (It->inArray()) {
135  OS << "[" << It->expand().getIndex() << "]";
136  continue;
137  }
138  if (auto Index = It->getIndex()) {
139  OS << " + " << Index;
140  continue;
141  }
142  OS << ".";
143  printDesc(It->getFieldDesc());
144  }
145 }
146 
147 void InterpFrame::describe(llvm::raw_ostream &OS) {
148  const FunctionDecl *F = getCallee();
149  auto *M = dyn_cast<CXXMethodDecl>(F);
150  if (M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
151  print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
152  OS << "->";
153  }
154  OS << *F << "(";
155  unsigned Off = 0;
156 
157  Off += Func->hasRVO() ? primSize(PT_Ptr) : 0;
158  Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0;
159 
160  for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
161  QualType Ty = F->getParamDecl(I)->getType();
162 
163  PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr);
164 
165  TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
166  Off += align(primSize(PrimTy));
167  if (I + 1 != N)
168  OS << ", ";
169  }
170  OS << ")";
171 }
172 
174  if (Caller->Caller)
175  return Caller;
176  return S.getSplitFrame();
177 }
178 
180  if (!Caller->Func)
181  return S.getLocation(nullptr, {});
182  return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
183 }
184 
186  return Func->getDecl();
187 }
188 
190  assert(Offset < Func->getFrameSize() && "Invalid local offset.");
191  return Pointer(
192  reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block)));
193 }
194 
196  // Return the block if it was created previously.
197  auto Pt = Params.find(Off);
198  if (Pt != Params.end()) {
199  return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
200  }
201 
202  // Allocate memory to store the parameter and the block metadata.
203  const auto &Desc = Func->getParamDescriptor(Off);
204  size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
205  auto Memory = std::make_unique<char[]>(BlockSize);
206  auto *B = new (Memory.get()) Block(Desc.second);
207 
208  // Copy the initial value.
209  TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
210 
211  // Record the param.
212  Params.insert({Off, std::move(Memory)});
213  return Pointer(B);
214 }
215 
217  return S.getSource(Func, PC);
218 }
219 
221  return S.getExpr(Func, PC);
222 }
223 
225  return S.getLocation(Func, PC);
226 }
227 
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
Pointer.h
clang::interp::Function::getDecl
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:89
clang::interp::InterpStack::discard
void discard()
Discards the top value from the stack.
Definition: InterpStack.h:54
clang::interp::InterpFrame::getCaller
Frame * getCaller() const override
Returns the parent frame object.
Definition: InterpFrame.cpp:173
clang::interp::align
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:58
clang::interp::InterpState
Interpreter context.
Definition: InterpState.h:34
clang::interp::Context::classify
llvm::Optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:75
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:737
DeclCXX.h
clang::interp::InterpFrame::~InterpFrame
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
Definition: InterpFrame.cpp:73
clang::interp::Scope
Describes a scope block.
Definition: Function.h:32
clang::interp::Block::invokeCtor
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:69
clang::interp::Pointer
A pointer to a memory block, live or dead.
Definition: Pointer.h:36
clang::interp::Function::hasRVO
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:102
Offset
unsigned Offset
Definition: Format.cpp:2717
clang::interp::InterpFrame
Frame storing local variables.
Definition: InterpFrame.h:29
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6895
V
#define V(N, I)
Definition: ASTContext.h:3235
clang::interp
Definition: ASTContext.h:155
TYPE_SWITCH
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:86
clang::interp::InterpFrame::destroy
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:80
clang::interp::Function::args_reverse
llvm::iterator_range< arg_reverse_iterator > args_reverse() const
Definition: Function.h:113
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::interp::SourceMapper::getLocation
SourceLocation getLocation(const Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition: Source.cpp:37
clang::interp::InterpState::getCtx
ASTContext & getCtx() const override
Definition: InterpState.h:53
InterpState.h
clang::interp::Block
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:35
clang::interp::InterpState::deallocate
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:57
clang::interp::PrimType
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:28
uintptr_t
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c-base.h:140
clang::interp::InterpState::getSplitFrame
Frame * getSplitFrame()
Definition: InterpState.h:42
ASTContext.h
clang::ASTContext::getRecordType
QualType getRecordType(const RecordDecl *Decl) const
Definition: ASTContext.cpp:4759
PrimType.h
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
InterpStack.h
clang::interp::InterpFrame::popArgs
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:86
clang::interp::RVOPtr
bool RVOPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1151
clang::interp::Scope::locals
llvm::iterator_range< LocalVectorTy::const_iterator > locals() const
Definition: Function.h:46
clang::interp::Function::scopes
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::const_iterator > scopes() const
Range over the scope blocks.
Definition: Function.h:106
clang::interp::InterpFrame::getCallee
const FunctionDecl * getCallee() const override
Returns the caller.
Definition: InterpFrame.cpp:185
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::interp::primSize
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:18
clang::interp::SourceMapper::getExpr
const Expr * getExpr(const Function *F, CodePtr PC) const
Returns the expression if an opcode belongs to one, null otherwise.
Definition: Source.cpp:31
clang::interp::Function::hasThisPointer
bool hasThisPointer() const
Definition: Function.h:136
clang::ASTContext::getPrintingPolicy
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:713
clang::interp::Function::getParamDescriptor
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:26
clang::interp::InterpFrame::Caller
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:32
clang::interp::InterpState::Stk
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:100
clang::interp::CodePtr
Pointer into the code segment.
Definition: Source.h:25
clang::interp::Function::isConstructor
bool isConstructor() const
Checks if the function is a constructor.
Definition: Function.h:131
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
std
Definition: Format.h:4477
clang::interp::Descriptor
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
clang::interp::InterpFrame::getExpr
const Expr * getExpr(CodePtr PC) const
Definition: InterpFrame.cpp:220
clang::interp::Function::getScope
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:118
clang::interp::This
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1137
clang::interp::InterpFrame::getLocalPointer
Pointer getLocalPointer(unsigned Offset)
Returns a pointer to a local variables.
Definition: InterpFrame.cpp:189
clang
Definition: CalledOnceCheck.h:17
Function.h
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::InterpState::Ctx
Context & Ctx
Interpreter Context.
Definition: InterpState.h:102
InterpFrame.h
clang::interp::InterpState::getSource
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:84
Program.h
clang::interp::PT_Ptr
@ PT_Ptr
Definition: PrimType.h:38
GCCTypeClass::Pointer
@ Pointer
print
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:92
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::interp::Function::getFrameSize
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition: Function.h:79
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