clang 10.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
20 InterpFrame::InterpFrame(InterpState &S, Function *Func, InterpFrame *Caller,
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
38 InterpFrame::~InterpFrame() {
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
51 void InterpFrame::popArgs() {
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 << "&";
91 llvm::SmallVector<Pointer, 2> Levels;
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
139 Frame *InterpFrame::getCaller() const {
140 if (Caller->Caller)
141 return Caller;
142 return S.getSplitFrame();
143 }
144
145 SourceLocation InterpFrame::getCallLocation() const {
146 if (!Caller->Func)
147 return S.getLocation(nullptr, {});
148 return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
149 }
150
151 const FunctionDecl *InterpFrame::getCallee() const {
152 return Func->getDecl();
153 }
154
155 Pointer InterpFrame::getLocalPointer(unsigned Offset) {
156 assert(Offset < Func->getFrameSize() && "Invalid local offset.");
157 return Pointer(
158 reinterpret_cast<Block *>(Locals.get() + Offset - sizeof(Block)));
159 }
160
161 Pointer InterpFrame::getParamPointer(unsigned Off) {
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
182 SourceInfo InterpFrame::getSource(CodePtr PC) const {
183 return S.getSource(Func, PC);
184 }
185
186 const Expr *InterpFrame::getExpr(CodePtr PC) const {
187 return S.getExpr(Func, PC);
188 }
189
190 SourceLocation InterpFrame::getLocation(CodePtr PC) const {
191 return S.getLocation(Func, PC);
192 }
193
Interp.h
clang::interp::Function::scopes
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::iterator > scopes()
Range over the scope blocks.
Definition: Function.h:86
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1783
clang::interp::primSize
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:17
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
clang::interp::InterpFrame::getParamPointer
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
Definition: InterpFrame.cpp:161
clang::interp::Pointer::initialize
void initialize() const
Initializes a field.
Definition: Pointer.cpp:154
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
P
StringRef P
Definition: ASTMatchersInternal.cpp:485
clang::interp::InterpFrame::getLocalPointer
Pointer getLocalPointer(unsigned Offset)
Returns a pointer to a local variables.
Definition: InterpFrame.cpp:155
clang::interp::align
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:57
clang::interp::Function::getDecl
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:74
clang::interp::This
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:827
clang::interp::Function::hasRVO
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:83
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:81
clang::interp::Context::classify
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
Definition: Context.cpp:61
clang::interp::Pointer
A pointer to a memory block, live or dead.
Definition: Pointer.h:39
clang::interp::InterpFrame::Caller
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:32
clang::interp::Function::getScope
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:97
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
llvm::Optional< PrimType >
clang::interp::Function::isConstructor
bool isConstructor() const
Checks if the function is a constructor.
Definition: Function.h:109
clang::interp::Function::getParamDescriptor
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:28
std
Definition: Format.h:2445
clang::interp::InterpState::Stk
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:100
clang::interp::Block
A memory block, either on the stack or in the heap.
Definition: Block.h:37
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6516
PrimType.h
clang::interp::InterpFrame::destroy
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:45
clang::interp::InterpState::getCtx
ASTContext & getCtx() const override
Definition: InterpState.h:53
clang::interp::PrimType
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:27
clang::ASTContext::getPrintingPolicy
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:671
clang::interp::Pointer::isRoot
bool isRoot() const
Pointer points directly to a block.
Definition: Pointer.h:216
clang::interp::InterpState::Ctx
Context & Ctx
Interpreter Context.
Definition: InterpState.h:102
clang::interp::Descriptor
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
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
V
#define V(N, I)
Definition: ASTContext.h:2941
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:62
clang::interp::Block::invokeCtor
void invokeCtor()
Invokes the constructor.
Definition: Block.h:71
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::ASTContext::getRecordType
QualType getRecordType(const RecordDecl *Decl) const
Definition: ASTContext.cpp:4173
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::CXXMethodDecl
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1931
clang::interp::Function
Bytecode function.
Definition: Function.h:59
InterpStack.h
clang::interp::Function::getFrameSize
unsigned getFrameSize() const
Returns the size of the function&#39;s local stack.
Definition: Function.h:64
llvm::SmallVector
Definition: LLVM.h:35
clang::interp::Pointer::isBaseClass
bool isBaseClass() const
Checks if a structure is a base class.
Definition: Pointer.h:244
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
clang::interp::InterpState::getSource
SourceInfo getSource(Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:84
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
Function.h
clang::interp::InterpFrame::getCallee
const FunctionDecl * getCallee() const
Returns the caller.
Definition: InterpFrame.cpp:151
clang::interp::PT_Ptr
Definition: PrimType.h:37
clang::interp::InterpState::deallocate
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:57
clang::interp::InterpState
Interpreter context.
Definition: InterpState.h:34
clang::interp::Scope::locals
llvm::iterator_range< LocalVectorTy::iterator > locals()
Definition: Function.h:46
clang::interp::Pointer::isZero
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:140
clang::interp::Pointer::getDeclDesc
Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition: Pointer.h:147
clang::interp::InterpFrame::getCaller
Frame * getCaller() const
Returns the parent frame object.
Definition: InterpFrame.cpp:139
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
InterpFrame.h
TYPE_SWITCH
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:85
DeclCXX.h
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
clang::interp
Definition: ASTContext.h:146
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::InterpState::getSplitFrame
Frame * getSplitFrame()
Definition: InterpState.h:42
clang::interp::SourceMapper::getLocation
SourceLocation getLocation(Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition: Source.cpp:37
clang::interp::Scope
Describes a scope block.
Definition: Function.h:32
print
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:57
clang::interp::InterpStack::discard
void discard()
Discards the top value from the stack.
Definition: InterpStack.h:44
clang::interp::Function::args_reverse
llvm::iterator_range< arg_reverse_iterator > args_reverse()
Definition: Function.h:92