/src/llvm-project/clang/lib/AST/Interp/InterpFrame.cpp
Line | Count | Source (jump to first uncovered line) |
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 "Boolean.h" |
11 | | #include "Floating.h" |
12 | | #include "Function.h" |
13 | | #include "InterpStack.h" |
14 | | #include "InterpState.h" |
15 | | #include "Pointer.h" |
16 | | #include "PrimType.h" |
17 | | #include "Program.h" |
18 | | #include "clang/AST/ASTContext.h" |
19 | | #include "clang/AST/DeclCXX.h" |
20 | | |
21 | | using namespace clang; |
22 | | using namespace clang::interp; |
23 | | |
24 | | InterpFrame::InterpFrame(InterpState &S, const Function *Func, |
25 | | InterpFrame *Caller, CodePtr RetPC) |
26 | | : Caller(Caller), S(S), Depth(Caller ? Caller->Depth + 1 : 0), Func(Func), |
27 | | RetPC(RetPC), ArgSize(Func ? Func->getArgSize() : 0), |
28 | 0 | Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) { |
29 | 0 | if (!Func) |
30 | 0 | return; |
31 | | |
32 | 0 | unsigned FrameSize = Func->getFrameSize(); |
33 | 0 | if (FrameSize == 0) |
34 | 0 | return; |
35 | | |
36 | 0 | Locals = std::make_unique<char[]>(FrameSize); |
37 | 0 | for (auto &Scope : Func->scopes()) { |
38 | 0 | for (auto &Local : Scope.locals()) { |
39 | 0 | Block *B = new (localBlock(Local.Offset)) Block(Local.Desc); |
40 | 0 | B->invokeCtor(); |
41 | 0 | InlineDescriptor *ID = localInlineDesc(Local.Offset); |
42 | 0 | ID->Desc = Local.Desc; |
43 | 0 | ID->IsActive = true; |
44 | 0 | ID->Offset = sizeof(InlineDescriptor); |
45 | 0 | ID->IsBase = false; |
46 | 0 | ID->IsFieldMutable = false; |
47 | 0 | ID->IsConst = false; |
48 | 0 | ID->IsInitialized = false; |
49 | 0 | } |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | InterpFrame::InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC) |
54 | 0 | : InterpFrame(S, Func, S.Current, RetPC) { |
55 | | // As per our calling convention, the this pointer is |
56 | | // part of the ArgSize. |
57 | | // If the function has RVO, the RVO pointer is first. |
58 | | // If the fuction has a This pointer, that one is next. |
59 | | // Then follow the actual arguments (but those are handled |
60 | | // in getParamPointer()). |
61 | 0 | if (Func->hasRVO()) |
62 | 0 | RVOPtr = stackRef<Pointer>(0); |
63 | |
|
64 | 0 | if (Func->hasThisPointer()) { |
65 | 0 | if (Func->hasRVO()) |
66 | 0 | This = stackRef<Pointer>(sizeof(Pointer)); |
67 | 0 | else |
68 | 0 | This = stackRef<Pointer>(0); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | InterpFrame::~InterpFrame() { |
73 | 0 | for (auto &Param : Params) |
74 | 0 | S.deallocate(reinterpret_cast<Block *>(Param.second.get())); |
75 | | |
76 | | // When destroying the InterpFrame, call the Dtor for all block |
77 | | // that haven't been destroyed via a destroy() op yet. |
78 | | // This happens when the execution is interruped midway-through. |
79 | 0 | if (Func) { |
80 | 0 | for (auto &Scope : Func->scopes()) { |
81 | 0 | for (auto &Local : Scope.locals()) { |
82 | 0 | Block *B = localBlock(Local.Offset); |
83 | 0 | if (B->isInitialized()) |
84 | 0 | B->invokeDtor(); |
85 | 0 | } |
86 | 0 | } |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | 0 | void InterpFrame::destroy(unsigned Idx) { |
91 | 0 | for (auto &Local : Func->getScope(Idx).locals()) { |
92 | 0 | S.deallocate(localBlock(Local.Offset)); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | 0 | void InterpFrame::popArgs() { |
97 | 0 | for (PrimType Ty : Func->args_reverse()) |
98 | 0 | TYPE_SWITCH(Ty, S.Stk.discard<T>()); |
99 | 0 | } |
100 | | |
101 | | template <typename T> |
102 | 0 | static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) { |
103 | 0 | OS << V; |
104 | 0 | } Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<8u, true> >(llvm::raw_ostream&, clang::interp::Integral<8u, true> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<8u, false> >(llvm::raw_ostream&, clang::interp::Integral<8u, false> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<16u, true> >(llvm::raw_ostream&, clang::interp::Integral<16u, true> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<16u, false> >(llvm::raw_ostream&, clang::interp::Integral<16u, false> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<32u, true> >(llvm::raw_ostream&, clang::interp::Integral<32u, true> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<32u, false> >(llvm::raw_ostream&, clang::interp::Integral<32u, false> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<64u, true> >(llvm::raw_ostream&, clang::interp::Integral<64u, true> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Integral<64u, false> >(llvm::raw_ostream&, clang::interp::Integral<64u, false> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::IntegralAP<false> >(llvm::raw_ostream&, clang::interp::IntegralAP<false> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::IntegralAP<true> >(llvm::raw_ostream&, clang::interp::IntegralAP<true> const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Floating>(llvm::raw_ostream&, clang::interp::Floating const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::Boolean>(llvm::raw_ostream&, clang::interp::Boolean const&, clang::ASTContext&, clang::QualType) Unexecuted instantiation: InterpFrame.cpp:void print<clang::interp::FunctionPointer>(llvm::raw_ostream&, clang::interp::FunctionPointer const&, clang::ASTContext&, clang::QualType) |
105 | | |
106 | | template <> |
107 | | void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx, |
108 | 0 | QualType Ty) { |
109 | 0 | if (P.isZero()) { |
110 | 0 | OS << "nullptr"; |
111 | 0 | return; |
112 | 0 | } |
113 | | |
114 | 0 | auto printDesc = [&OS, &Ctx](const Descriptor *Desc) { |
115 | 0 | if (const auto *D = Desc->asDecl()) { |
116 | | // Subfields or named values. |
117 | 0 | if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
118 | 0 | OS << *VD; |
119 | 0 | return; |
120 | 0 | } |
121 | | // Base classes. |
122 | 0 | if (isa<RecordDecl>(D)) |
123 | 0 | return; |
124 | 0 | } |
125 | | // Temporary expression. |
126 | 0 | if (const auto *E = Desc->asExpr()) { |
127 | 0 | E->printPretty(OS, nullptr, Ctx.getPrintingPolicy()); |
128 | 0 | return; |
129 | 0 | } |
130 | 0 | llvm_unreachable("Invalid descriptor type"); |
131 | 0 | }; |
132 | |
|
133 | 0 | if (!Ty->isReferenceType()) |
134 | 0 | OS << "&"; |
135 | 0 | llvm::SmallVector<Pointer, 2> Levels; |
136 | 0 | for (Pointer F = P; !F.isRoot(); ) { |
137 | 0 | Levels.push_back(F); |
138 | 0 | F = F.isArrayElement() ? F.getArray().expand() : F.getBase(); |
139 | 0 | } |
140 | | |
141 | | // Drop the first pointer since we print it unconditionally anyway. |
142 | 0 | if (!Levels.empty()) |
143 | 0 | Levels.erase(Levels.begin()); |
144 | |
|
145 | 0 | printDesc(P.getDeclDesc()); |
146 | 0 | for (const auto &It : Levels) { |
147 | 0 | if (It.inArray()) { |
148 | 0 | OS << "[" << It.expand().getIndex() << "]"; |
149 | 0 | continue; |
150 | 0 | } |
151 | 0 | if (auto Index = It.getIndex()) { |
152 | 0 | OS << " + " << Index; |
153 | 0 | continue; |
154 | 0 | } |
155 | 0 | OS << "."; |
156 | 0 | printDesc(It.getFieldDesc()); |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | 0 | void InterpFrame::describe(llvm::raw_ostream &OS) const { |
161 | 0 | const FunctionDecl *F = getCallee(); |
162 | 0 | if (const auto *M = dyn_cast<CXXMethodDecl>(F); |
163 | 0 | M && M->isInstance() && !isa<CXXConstructorDecl>(F)) { |
164 | 0 | print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent())); |
165 | 0 | OS << "->"; |
166 | 0 | } |
167 | 0 | OS << *F << "("; |
168 | 0 | unsigned Off = 0; |
169 | |
|
170 | 0 | Off += Func->hasRVO() ? primSize(PT_Ptr) : 0; |
171 | 0 | Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0; |
172 | |
|
173 | 0 | for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) { |
174 | 0 | QualType Ty = F->getParamDecl(I)->getType(); |
175 | |
|
176 | 0 | PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr); |
177 | |
|
178 | 0 | TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty)); |
179 | 0 | Off += align(primSize(PrimTy)); |
180 | 0 | if (I + 1 != N) |
181 | 0 | OS << ", "; |
182 | 0 | } |
183 | 0 | OS << ")"; |
184 | 0 | } |
185 | | |
186 | 0 | Frame *InterpFrame::getCaller() const { |
187 | 0 | if (Caller->Caller) |
188 | 0 | return Caller; |
189 | 0 | return S.getSplitFrame(); |
190 | 0 | } |
191 | | |
192 | 0 | SourceRange InterpFrame::getCallRange() const { |
193 | 0 | if (!Caller->Func) |
194 | 0 | return S.getRange(nullptr, {}); |
195 | 0 | return S.getRange(Caller->Func, RetPC - sizeof(uintptr_t)); |
196 | 0 | } |
197 | | |
198 | 0 | const FunctionDecl *InterpFrame::getCallee() const { |
199 | 0 | return Func->getDecl(); |
200 | 0 | } |
201 | | |
202 | 0 | Pointer InterpFrame::getLocalPointer(unsigned Offset) const { |
203 | 0 | assert(Offset < Func->getFrameSize() && "Invalid local offset."); |
204 | 0 | return Pointer(localBlock(Offset), sizeof(InlineDescriptor)); |
205 | 0 | } |
206 | | |
207 | 0 | Pointer InterpFrame::getParamPointer(unsigned Off) { |
208 | | // Return the block if it was created previously. |
209 | 0 | auto Pt = Params.find(Off); |
210 | 0 | if (Pt != Params.end()) { |
211 | 0 | return Pointer(reinterpret_cast<Block *>(Pt->second.get())); |
212 | 0 | } |
213 | | |
214 | | // Allocate memory to store the parameter and the block metadata. |
215 | 0 | const auto &Desc = Func->getParamDescriptor(Off); |
216 | 0 | size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize(); |
217 | 0 | auto Memory = std::make_unique<char[]>(BlockSize); |
218 | 0 | auto *B = new (Memory.get()) Block(Desc.second); |
219 | | |
220 | | // Copy the initial value. |
221 | 0 | TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off))); |
222 | | |
223 | | // Record the param. |
224 | 0 | Params.insert({Off, std::move(Memory)}); |
225 | 0 | return Pointer(B); |
226 | 0 | } |
227 | | |
228 | 0 | SourceInfo InterpFrame::getSource(CodePtr PC) const { |
229 | | // Implicitly created functions don't have any code we could point at, |
230 | | // so return the call site. |
231 | 0 | if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) |
232 | 0 | return Caller->getSource(RetPC); |
233 | | |
234 | 0 | return S.getSource(Func, PC); |
235 | 0 | } |
236 | | |
237 | 0 | const Expr *InterpFrame::getExpr(CodePtr PC) const { |
238 | 0 | return S.getExpr(Func, PC); |
239 | 0 | } |
240 | | |
241 | 0 | SourceLocation InterpFrame::getLocation(CodePtr PC) const { |
242 | 0 | return S.getLocation(Func, PC); |
243 | 0 | } |
244 | | |
245 | 0 | SourceRange InterpFrame::getRange(CodePtr PC) const { |
246 | 0 | if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller) |
247 | 0 | return Caller->getRange(RetPC); |
248 | | |
249 | 0 | return S.getRange(Func, PC); |
250 | 0 | } |