xref: /freebsd/contrib/llvm-project/clang/lib/AST/Interp/InterpFrame.cpp (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 "Function.h"
12 #include "InterpStack.h"
13 #include "InterpState.h"
14 #include "Pointer.h"
15 #include "PrimType.h"
16 #include "Program.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
19 
20 using namespace clang;
21 using namespace clang::interp;
22 
23 InterpFrame::InterpFrame(InterpState &S, const Function *Func,
24                          InterpFrame *Caller, CodePtr RetPC)
25     : Caller(Caller), S(S), Func(Func), RetPC(RetPC),
26       ArgSize(Func ? Func->getArgSize() : 0),
27       Args(static_cast<char *>(S.Stk.top())), FrameOffset(S.Stk.size()) {
28   if (!Func)
29     return;
30 
31   unsigned FrameSize = Func->getFrameSize();
32   if (FrameSize == 0)
33     return;
34 
35   Locals = std::make_unique<char[]>(FrameSize);
36   for (auto &Scope : Func->scopes()) {
37     for (auto &Local : Scope.locals()) {
38       Block *B = new (localBlock(Local.Offset)) Block(Local.Desc);
39       B->invokeCtor();
40       InlineDescriptor *ID = localInlineDesc(Local.Offset);
41       ID->Desc = Local.Desc;
42       ID->IsActive = true;
43       ID->Offset = sizeof(InlineDescriptor);
44       ID->IsBase = false;
45       ID->IsFieldMutable = false;
46       ID->IsConst = false;
47       ID->IsInitialized = false;
48     }
49   }
50 }
51 
52 InterpFrame::InterpFrame(InterpState &S, const Function *Func, CodePtr RetPC)
53     : InterpFrame(S, Func, S.Current, RetPC) {
54   // As per our calling convention, the this pointer is
55   // part of the ArgSize.
56   // If the function has RVO, the RVO pointer is first.
57   // If the fuction has a This pointer, that one is next.
58   // Then follow the actual arguments (but those are handled
59   // in getParamPointer()).
60   if (Func->hasRVO())
61     RVOPtr = stackRef<Pointer>(0);
62 
63   if (Func->hasThisPointer()) {
64     if (Func->hasRVO())
65       This = stackRef<Pointer>(sizeof(Pointer));
66     else
67       This = stackRef<Pointer>(0);
68   }
69 }
70 
71 InterpFrame::~InterpFrame() {
72   for (auto &Param : Params)
73     S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
74 }
75 
76 void InterpFrame::destroy(unsigned Idx) {
77   for (auto &Local : Func->getScope(Idx).locals()) {
78     S.deallocate(reinterpret_cast<Block *>(localBlock(Local.Offset)));
79   }
80 }
81 
82 void InterpFrame::popArgs() {
83   for (PrimType Ty : Func->args_reverse())
84     TYPE_SWITCH(Ty, S.Stk.discard<T>());
85 }
86 
87 template <typename T>
88 static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
89   OS << V;
90 }
91 
92 template <>
93 void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
94            QualType Ty) {
95   if (P.isZero()) {
96     OS << "nullptr";
97     return;
98   }
99 
100   auto printDesc = [&OS, &Ctx](Descriptor *Desc) {
101     if (auto *D = Desc->asDecl()) {
102       // Subfields or named values.
103       if (auto *VD = dyn_cast<ValueDecl>(D)) {
104         OS << *VD;
105         return;
106       }
107       // Base classes.
108       if (isa<RecordDecl>(D)) {
109         return;
110       }
111     }
112     // Temporary expression.
113     if (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   printDesc(P.getDeclDesc());
129   for (const auto &It : Levels) {
130     if (It.inArray()) {
131       OS << "[" << It.expand().getIndex() << "]";
132       continue;
133     }
134     if (auto Index = It.getIndex()) {
135       OS << " + " << Index;
136       continue;
137     }
138     OS << ".";
139     printDesc(It.getFieldDesc());
140   }
141 }
142 
143 void InterpFrame::describe(llvm::raw_ostream &OS) {
144   const FunctionDecl *F = getCallee();
145   auto *M = dyn_cast<CXXMethodDecl>(F);
146   if (M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
147     print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
148     OS << "->";
149   }
150   OS << *F << "(";
151   unsigned Off = 0;
152 
153   Off += Func->hasRVO() ? primSize(PT_Ptr) : 0;
154   Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0;
155 
156   for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
157     QualType Ty = F->getParamDecl(I)->getType();
158 
159     PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr);
160 
161     TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
162     Off += align(primSize(PrimTy));
163     if (I + 1 != N)
164       OS << ", ";
165   }
166   OS << ")";
167 }
168 
169 Frame *InterpFrame::getCaller() const {
170   if (Caller->Caller)
171     return Caller;
172   return S.getSplitFrame();
173 }
174 
175 SourceLocation InterpFrame::getCallLocation() const {
176   if (!Caller->Func)
177     return S.getLocation(nullptr, {});
178   return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
179 }
180 
181 const FunctionDecl *InterpFrame::getCallee() const {
182   return Func->getDecl();
183 }
184 
185 Pointer InterpFrame::getLocalPointer(unsigned Offset) const {
186   assert(Offset < Func->getFrameSize() && "Invalid local offset.");
187   return Pointer(reinterpret_cast<Block *>(localBlock(Offset)),
188                  sizeof(InlineDescriptor));
189 }
190 
191 Pointer InterpFrame::getParamPointer(unsigned Off) {
192   // Return the block if it was created previously.
193   auto Pt = Params.find(Off);
194   if (Pt != Params.end()) {
195     return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
196   }
197 
198   // Allocate memory to store the parameter and the block metadata.
199   const auto &Desc = Func->getParamDescriptor(Off);
200   size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
201   auto Memory = std::make_unique<char[]>(BlockSize);
202   auto *B = new (Memory.get()) Block(Desc.second);
203 
204   // Copy the initial value.
205   TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
206 
207   // Record the param.
208   Params.insert({Off, std::move(Memory)});
209   return Pointer(B);
210 }
211 
212 SourceInfo InterpFrame::getSource(CodePtr PC) const {
213   return S.getSource(Func, PC);
214 }
215 
216 const Expr *InterpFrame::getExpr(CodePtr PC) const {
217   return S.getExpr(Func, PC);
218 }
219 
220 SourceLocation InterpFrame::getLocation(CodePtr PC) const {
221   return S.getLocation(Func, PC);
222 }
223 
224