xref: /freebsd/contrib/llvm-project/clang/lib/AST/Interp/InterpFrame.cpp (revision 0ad011ececb978e22a9bff2acf76633b094f1ff6)
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       Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) {
29   if (!Func)
30     return;
31 
32   unsigned FrameSize = Func->getFrameSize();
33   if (FrameSize == 0)
34     return;
35 
36   Locals = std::make_unique<char[]>(FrameSize);
37   for (auto &Scope : Func->scopes()) {
38     for (auto &Local : Scope.locals()) {
39       Block *B = new (localBlock(Local.Offset)) Block(Local.Desc);
40       B->invokeCtor();
41       InlineDescriptor *ID = localInlineDesc(Local.Offset);
42       ID->Desc = Local.Desc;
43       ID->IsActive = true;
44       ID->Offset = sizeof(InlineDescriptor);
45       ID->IsBase = false;
46       ID->IsFieldMutable = false;
47       ID->IsConst = false;
48       ID->IsInitialized = false;
49     }
50   }
51 }
52 
53 InterpFrame::InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC)
54     : 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   if (Func->hasRVO())
62     RVOPtr = stackRef<Pointer>(0);
63 
64   if (Func->hasThisPointer()) {
65     if (Func->hasRVO())
66       This = stackRef<Pointer>(sizeof(Pointer));
67     else
68       This = stackRef<Pointer>(0);
69   }
70 }
71 
72 InterpFrame::~InterpFrame() {
73   for (auto &Param : Params)
74     S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
75 }
76 
77 void InterpFrame::destroy(unsigned Idx) {
78   for (auto &Local : Func->getScope(Idx).locals()) {
79     S.deallocate(localBlock(Local.Offset));
80   }
81 }
82 
83 void InterpFrame::popArgs() {
84   for (PrimType Ty : Func->args_reverse())
85     TYPE_SWITCH(Ty, S.Stk.discard<T>());
86 }
87 
88 template <typename T>
89 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
90   OS << V;
91 }
92 
93 template <>
94 void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
95            QualType Ty) {
96   if (P.isZero()) {
97     OS << "nullptr";
98     return;
99   }
100 
101   auto printDesc = [&OS, &Ctx](const Descriptor *Desc) {
102     if (const auto *D = Desc->asDecl()) {
103       // Subfields or named values.
104       if (const auto *VD = dyn_cast<ValueDecl>(D)) {
105         OS << *VD;
106         return;
107       }
108       // Base classes.
109       if (isa<RecordDecl>(D))
110         return;
111     }
112     // Temporary expression.
113     if (const auto *E = Desc->asExpr()) {
114       E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
115       return;
116     }
117     llvm_unreachable("Invalid descriptor type");
118   };
119 
120   if (!Ty->isReferenceType())
121     OS << "&";
122   llvm::SmallVector<Pointer, 2> Levels;
123   for (Pointer F = P; !F.isRoot(); ) {
124     Levels.push_back(F);
125     F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
126   }
127 
128   // Drop the first pointer since we print it unconditionally anyway.
129   if (!Levels.empty())
130     Levels.erase(Levels.begin());
131 
132   printDesc(P.getDeclDesc());
133   for (const auto &It : Levels) {
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) const {
148   const FunctionDecl *F = getCallee();
149   if (const auto *M = dyn_cast<CXXMethodDecl>(F);
150       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 
173 Frame *InterpFrame::getCaller() const {
174   if (Caller->Caller)
175     return Caller;
176   return S.getSplitFrame();
177 }
178 
179 SourceLocation InterpFrame::getCallLocation() const {
180   if (!Caller->Func)
181     return S.getLocation(nullptr, {});
182   return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
183 }
184 
185 const FunctionDecl *InterpFrame::getCallee() const {
186   return Func->getDecl();
187 }
188 
189 Pointer InterpFrame::getLocalPointer(unsigned Offset) const {
190   assert(Offset < Func->getFrameSize() && "Invalid local offset.");
191   return Pointer(localBlock(Offset), sizeof(InlineDescriptor));
192 }
193 
194 Pointer InterpFrame::getParamPointer(unsigned Off) {
195   // Return the block if it was created previously.
196   auto Pt = Params.find(Off);
197   if (Pt != Params.end()) {
198     return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
199   }
200 
201   // Allocate memory to store the parameter and the block metadata.
202   const auto &Desc = Func->getParamDescriptor(Off);
203   size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
204   auto Memory = std::make_unique<char[]>(BlockSize);
205   auto *B = new (Memory.get()) Block(Desc.second);
206 
207   // Copy the initial value.
208   TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
209 
210   // Record the param.
211   Params.insert({Off, std::move(Memory)});
212   return Pointer(B);
213 }
214 
215 SourceInfo InterpFrame::getSource(CodePtr PC) const {
216   // Implicitly created functions don't have any code we could point at,
217   // so return the call site.
218   if (Func && Func->getDecl()->isImplicit() && Caller)
219     return Caller->getSource(RetPC);
220 
221   return S.getSource(Func, PC);
222 }
223 
224 const Expr *InterpFrame::getExpr(CodePtr PC) const {
225   return S.getExpr(Func, PC);
226 }
227 
228 SourceLocation InterpFrame::getLocation(CodePtr PC) const {
229   return S.getLocation(Func, PC);
230 }
231 
232