clang  13.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 "Interp.h"
12 #include "InterpStack.h"
13 #include "PrimType.h"
14 #include "Program.h"
15 #include "clang/AST/DeclCXX.h"
16 
17 using namespace clang;
18 using namespace clang::interp;
19 
21  CodePtr RetPC, Pointer &&This)
22  : Caller(Caller), S(S), Func(Func), This(std::move(This)), RetPC(RetPC),
23  ArgSize(Func ? Func->getArgSize() : 0),
24  Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) {
25  if (Func) {
26  if (unsigned FrameSize = Func->getFrameSize()) {
27  Locals = std::make_unique<char[]>(FrameSize);
28  for (auto &Scope : Func->scopes()) {
29  for (auto &Local : Scope.locals()) {
30  Block *B = new (localBlock(Local.Offset)) Block(Local.Desc);
31  B->invokeCtor();
32  }
33  }
34  }
35  }
36 }
37 
39  if (Func && Func->isConstructor() && This.isBaseClass())
40  This.initialize();
41  for (auto &Param : Params)
42  S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
43 }
44 
45 void InterpFrame::destroy(unsigned Idx) {
46  for (auto &Local : Func->getScope(Idx).locals()) {
47  S.deallocate(reinterpret_cast<Block *>(localBlock(Local.Offset)));
48  }
49 }
50 
52  for (PrimType Ty : Func->args_reverse())
53  TYPE_SWITCH(Ty, S.Stk.discard<T>());
54 }
55 
56 template <typename T>
57 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
58  OS << V;
59 }
60 
61 template <>
62 void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
63  QualType Ty) {
64  if (P.isZero()) {
65  OS << "nullptr";
66  return;
67  }
68 
69  auto printDesc = [&OS, &Ctx](Descriptor *Desc) {
70  if (auto *D = Desc->asDecl()) {
71  // Subfields or named values.
72  if (auto *VD = dyn_cast<ValueDecl>(D)) {
73  OS << *VD;
74  return;
75  }
76  // Base classes.
77  if (isa<RecordDecl>(D)) {
78  return;
79  }
80  }
81  // Temporary expression.
82  if (auto *E = Desc->asExpr()) {
83  E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
84  return;
85  }
86  llvm_unreachable("Invalid descriptor type");
87  };
88 
89  if (!Ty->isReferenceType())
90  OS << "&";
92  for (Pointer F = P; !F.isRoot(); ) {
93  Levels.push_back(F);
94  F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
95  }
96 
97  printDesc(P.getDeclDesc());
98  for (auto It = Levels.rbegin(); It != Levels.rend(); ++It) {
99  if (It->inArray()) {
100  OS << "[" << It->expand().getIndex() << "]";
101  continue;
102  }
103  if (auto Index = It->getIndex()) {
104  OS << " + " << Index;
105  continue;
106  }
107  OS << ".";
108  printDesc(It->getFieldDesc());
109  }
110 }
111 
112 void InterpFrame::describe(llvm::raw_ostream &OS) {
113  const FunctionDecl *F = getCallee();
114  auto *M = dyn_cast<CXXMethodDecl>(F);
115  if (M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
116  print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
117  OS << "->";
118  }
119  OS << *F << "(";
120  unsigned Off = Func->hasRVO() ? primSize(PT_Ptr) : 0;
121  for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
122  QualType Ty = F->getParamDecl(I)->getType();
123 
124  PrimType PrimTy;
125  if (llvm::Optional<PrimType> T = S.Ctx.classify(Ty)) {
126  PrimTy = *T;
127  } else {
128  PrimTy = PT_Ptr;
129  }
130 
131  TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
132  Off += align(primSize(PrimTy));
133  if (I + 1 != N)
134  OS << ", ";
135  }
136  OS << ")";
137 }
138 
140  if (Caller->Caller)
141  return Caller;
142  return S.getSplitFrame();
143 }
144 
146  if (!Caller->Func)
147  return S.getLocation(nullptr, {});
148  return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
149 }
150 
152  return Func->getDecl();
153 }
154 
156  assert(Offset < Func->getFrameSize() && "Invalid local offset.");
157  return Pointer(
158  reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block)));
159 }
160 
162  // Return the block if it was created previously.
163  auto Pt = Params.find(Off);
164  if (Pt != Params.end()) {
165  return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
166  }
167 
168  // Allocate memory to store the parameter and the block metadata.
169  const auto &Desc = Func->getParamDescriptor(Off);
170  size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
171  auto Memory = std::make_unique<char[]>(BlockSize);
172  auto *B = new (Memory.get()) Block(Desc.second);
173 
174  // Copy the initial value.
175  TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
176 
177  // Record the param.
178  Params.insert({Off, std::move(Memory)});
179  return Pointer(B);
180 }
181 
183  return S.getSource(Func, PC);
184 }
185 
187  return S.getExpr(Func, PC);
188 }
189 
191  return S.getLocation(Func, PC);
192 }
193 
clang::interp::InterpFrame::getSource
virtual SourceInfo getSource(CodePtr PC) const
Map a location to a source.
Definition: InterpFrame.cpp:182
clang::interp::Function::getDecl
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:74
clang::interp::InterpStack::discard
void discard()
Discards the top value from the stack.
Definition: InterpStack.h:44
clang::interp::InterpFrame::getCaller
Frame * getCaller() const override
Returns the parent frame object.
Definition: InterpFrame.cpp:139
clang::interp::align
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:57
clang::interp::InterpState
Interpreter context.
Definition: InterpState.h:34
clang::interp::Function::scopes
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::iterator > scopes()
Range over the scope blocks.
Definition: Function.h:86
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:89
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:668
DeclCXX.h
clang::interp::InterpFrame::~InterpFrame
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
Definition: InterpFrame.cpp:38
clang::interp::Scope
Describes a scope block.
Definition: Function.h:32
llvm::Optional< PrimType >
clang::interp::Block::invokeCtor
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:71
clang::interp::Pointer
A pointer to a memory block, live or dead.
Definition: Pointer.h:39
clang::interp::InterpState::getSource
SourceInfo getSource(Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:84
clang::interp::Function::hasRVO
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:83
Offset
unsigned Offset
Definition: Format.cpp:2181
clang::interp::InterpFrame
Frame storing local variables.
Definition: InterpFrame.h:29
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6687
V
#define V(N, I)
Definition: ASTContext.h:3039
clang::interp
Definition: ASTContext.h:157
TYPE_SWITCH
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:85
clang::interp::InterpFrame::destroy
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:45
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:189
clang::interp::InterpState::getCtx
ASTContext & getCtx() const override
Definition: InterpState.h:53
clang::interp::Block
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:37
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:27
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:100
clang::interp::InterpState::getSplitFrame
Frame * getSplitFrame()
Definition: InterpState.h:42
clang::ASTContext::getRecordType
QualType getRecordType(const RecordDecl *Decl) const
Definition: ASTContext.cpp:4536
PrimType.h
clang::interp::InterpFrame::getCallLocation
SourceLocation getCallLocation() const override
Returns the location of the call to the frame.
Definition: InterpFrame.cpp:145
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:81
InterpStack.h
clang::interp::InterpFrame::popArgs
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:51
clang::interp::Scope::locals
llvm::iterator_range< LocalVectorTy::iterator > locals()
Definition: Function.h:46
clang::interp::InterpFrame::getCallee
const FunctionDecl * getCallee() const override
Returns the caller.
Definition: InterpFrame.cpp:151
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:17
clang::interp::SourceMapper::getLocation
SourceLocation getLocation(Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition: Source.cpp:37
Interp.h
clang::ASTContext::getPrintingPolicy
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:640
clang::interp::Function::getParamDescriptor
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:28
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:109
clang::interp::InterpFrame::getLocation
SourceLocation getLocation(CodePtr PC) const
Definition: InterpFrame.cpp:190
clang::interp::Frame
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
std
Definition: Format.h:3702
clang::interp::Descriptor
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
clang::interp::Function::args_reverse
llvm::iterator_range< arg_reverse_iterator > args_reverse()
Definition: Function.h:92
clang::interp::InterpFrame::getExpr
const Expr * getExpr(CodePtr PC) const
Definition: InterpFrame.cpp:186
clang::interp::Function::getScope
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:97
clang::interp::This
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:827
clang::interp::InterpFrame::getLocalPointer
Pointer getLocalPointer(unsigned Offset)
Returns a pointer to a local variables.
Definition: InterpFrame.cpp:155
clang
Definition: CalledOnceCheck.h:17
clang::interp::SourceMapper::getExpr
const Expr * getExpr(Function *F, CodePtr PC) const
Returns the expression if an opcode belongs to one, null otherwise.
Definition: Source.cpp:31
Function.h
clang::interp::Function
Bytecode function.
Definition: Function.h:59
clang::interp::InterpFrame::describe
void describe(llvm::raw_ostream &OS) override
Describes the frame with arguments for diagnostic purposes.
Definition: InterpFrame.cpp:112
clang::interp::InterpState::Ctx
Context & Ctx
Interpreter Context.
Definition: InterpState.h:102
InterpFrame.h
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
Program.h
clang::interp::PT_Ptr
@ PT_Ptr
Definition: PrimType.h:37
GCCTypeClass::Pointer
@ Pointer
print
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:57
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:64
clang::interp::InterpFrame::getParamPointer
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
Definition: InterpFrame.cpp:161
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1825
clang::interp::Context::classify
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
Definition: Context.cpp:62