clang 19.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 "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"
19#include "clang/AST/DeclCXX.h"
20
21using namespace clang;
22using namespace clang::interp;
23
25 InterpFrame *Caller, CodePtr RetPC, unsigned ArgSize)
26 : Caller(Caller), S(S), Depth(Caller ? Caller->Depth + 1 : 0), Func(Func),
27 RetPC(RetPC), ArgSize(ArgSize), Args(static_cast<char *>(S.Stk.top())),
28 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 new (localInlineDesc(Local.Offset)) InlineDescriptor(Local.Desc);
42 }
43 }
44}
45
47 unsigned VarArgSize)
48 : InterpFrame(S, Func, S.Current, RetPC, Func->getArgSize() + VarArgSize) {
49 // As per our calling convention, the this pointer is
50 // part of the ArgSize.
51 // If the function has RVO, the RVO pointer is first.
52 // If the fuction has a This pointer, that one is next.
53 // Then follow the actual arguments (but those are handled
54 // in getParamPointer()).
55 if (Func->hasRVO())
56 RVOPtr = stackRef<Pointer>(0);
57
58 if (Func->hasThisPointer()) {
59 if (Func->hasRVO())
60 This = stackRef<Pointer>(sizeof(Pointer));
61 else
62 This = stackRef<Pointer>(0);
63 }
64}
65
67 for (auto &Param : Params)
68 S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
69
70 // When destroying the InterpFrame, call the Dtor for all block
71 // that haven't been destroyed via a destroy() op yet.
72 // This happens when the execution is interruped midway-through.
73 if (Func) {
74 for (auto &Scope : Func->scopes()) {
75 for (auto &Local : Scope.locals()) {
76 Block *B = localBlock(Local.Offset);
77 if (B->isInitialized())
78 B->invokeDtor();
79 }
80 }
81 }
82}
83
84void InterpFrame::destroy(unsigned Idx) {
85 for (auto &Local : Func->getScope(Idx).locals()) {
86 S.deallocate(localBlock(Local.Offset));
87 }
88}
89
91 for (PrimType Ty : Func->args_reverse())
92 TYPE_SWITCH(Ty, S.Stk.discard<T>());
93}
94
95template <typename T>
96static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
97 OS << V;
98}
99
100template <>
101void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx,
102 QualType Ty) {
103 if (P.isZero()) {
104 OS << "nullptr";
105 return;
106 }
107
108 auto printDesc = [&OS, &Ctx](const Descriptor *Desc) {
109 if (const auto *D = Desc->asDecl()) {
110 // Subfields or named values.
111 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
112 OS << *VD;
113 return;
114 }
115 // Base classes.
116 if (isa<RecordDecl>(D))
117 return;
118 }
119 // Temporary expression.
120 if (const auto *E = Desc->asExpr()) {
121 E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
122 return;
123 }
124 llvm_unreachable("Invalid descriptor type");
125 };
126
127 if (!Ty->isReferenceType())
128 OS << "&";
130 for (Pointer F = P; !F.isRoot(); ) {
131 Levels.push_back(F);
132 F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
133 }
134
135 // Drop the first pointer since we print it unconditionally anyway.
136 if (!Levels.empty())
137 Levels.erase(Levels.begin());
138
139 printDesc(P.getDeclDesc());
140 for (const auto &It : Levels) {
141 if (It.inArray()) {
142 OS << "[" << It.expand().getIndex() << "]";
143 continue;
144 }
145 if (auto Index = It.getIndex()) {
146 OS << " + " << Index;
147 continue;
148 }
149 OS << ".";
150 printDesc(It.getFieldDesc());
151 }
152}
153
154void InterpFrame::describe(llvm::raw_ostream &OS) const {
155 const FunctionDecl *F = getCallee();
156 if (const auto *M = dyn_cast<CXXMethodDecl>(F);
157 M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
158 print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
159 OS << "->";
160 }
161 OS << *F << "(";
162 unsigned Off = 0;
163
164 Off += Func->hasRVO() ? primSize(PT_Ptr) : 0;
165 Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0;
166
167 for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
168 QualType Ty = F->getParamDecl(I)->getType();
169
170 PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr);
171
172 TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
173 Off += align(primSize(PrimTy));
174 if (I + 1 != N)
175 OS << ", ";
176 }
177 OS << ")";
178}
179
181 if (Caller->Caller)
182 return Caller;
183 return S.getSplitFrame();
184}
185
187 if (!Caller->Func)
188 return S.getRange(nullptr, {});
189 return S.getRange(Caller->Func, RetPC - sizeof(uintptr_t));
190}
191
193 if (!Func)
194 return nullptr;
195 return Func->getDecl();
196}
197
198Pointer InterpFrame::getLocalPointer(unsigned Offset) const {
199 assert(Offset < Func->getFrameSize() && "Invalid local offset.");
200 return Pointer(localBlock(Offset));
201}
202
204 // Return the block if it was created previously.
205 auto Pt = Params.find(Off);
206 if (Pt != Params.end()) {
207 return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
208 }
209
210 // Allocate memory to store the parameter and the block metadata.
211 const auto &Desc = Func->getParamDescriptor(Off);
212 size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
213 auto Memory = std::make_unique<char[]>(BlockSize);
214 auto *B = new (Memory.get()) Block(Desc.second);
215
216 // Copy the initial value.
217 TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
218
219 // Record the param.
220 Params.insert({Off, std::move(Memory)});
221 return Pointer(B);
222}
223
225 // Implicitly created functions don't have any code we could point at,
226 // so return the call site.
227 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
228 return Caller->getSource(RetPC);
229
230 return S.getSource(Func, PC);
231}
232
234 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
235 return Caller->getExpr(RetPC);
236
237 return S.getExpr(Func, PC);
238}
239
241 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
242 return Caller->getLocation(RetPC);
243
244 return S.getLocation(Func, PC);
245}
246
248 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
249 return Caller->getRange(RetPC);
250
251 return S.getRange(Func, PC);
252}
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3266
StringRef P
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:96
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:113
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
QualType getRecordType(const RecordDecl *Decl) const
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:694
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:598
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1959
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2674
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3657
A (possibly-)qualified type.
Definition: Type.h:738
Encodes a location in the source.
A trivial tuple used to represent a source range.
bool isReferenceType() const
Definition: Type.h:7383
QualType getType() const
Definition: Decl.h:717
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void invokeDtor()
Invokes the Destructor.
Definition: InterpBlock.h:115
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:106
bool isInitialized() const
Definition: InterpBlock.h:74
Pointer into the code segment.
Definition: Source.h:30
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:119
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Bytecode function.
Definition: Function.h:77
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:128
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:92
bool hasBody() const
Checks if the function already has a body attached.
Definition: Function.h:174
bool hasThisPointer() const
Definition: Function.h:171
llvm::iterator_range< arg_reverse_iterator > args_reverse() const
Definition: Function.h:123
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:30
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::const_iterator > scopes() const
Range over the scope blocks.
Definition: Function.h:116
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:110
Frame storing local variables.
Definition: InterpFrame.h:28
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:90
const Expr * getExpr(CodePtr PC) const
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:31
virtual SourceInfo getSource(CodePtr PC) const
Map a location to a source.
SourceLocation getLocation(CodePtr PC) const
~InterpFrame()
Destroys the frame, killing all live pointers to stack slots.
Definition: InterpFrame.cpp:66
SourceRange getRange(CodePtr PC) const
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
Frame * getCaller() const override
Returns the parent frame object.
InterpFrame(InterpState &S, const Function *Func, InterpFrame *Caller, CodePtr RetPC, unsigned ArgSize)
Creates a new frame for a method call.
Definition: InterpFrame.cpp:24
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:84
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
const FunctionDecl * getCallee() const override
Returns the caller.
SourceRange getCallRange() const override
Returns the location of the call to the frame.
void describe(llvm::raw_ostream &OS) const override
Describes the frame with arguments for diagnostic purposes.
void discard()
Discards the top value from the stack.
Definition: InterpStack.h:56
Interpreter context.
Definition: InterpState.h:35
Context & Ctx
Interpreter Context.
Definition: InterpState.h:111
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:91
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:109
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:48
ASTContext & getCtx() const override
Definition: InterpState.h:59
A pointer to a memory block, live or dead.
Definition: Pointer.h:65
Describes a scope block.
Definition: Function.h:35
llvm::iterator_range< LocalVectorTy::const_iterator > locals() const
Definition: Function.h:49
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
SourceLocation getLocation(const Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition: Source.cpp:47
SourceRange getRange(const Function *F, CodePtr PC) const
Definition: Source.cpp:51
const Expr * getExpr(const Function *F, CodePtr PC) const
Returns the expression if an opcode belongs to one, null otherwise.
Definition: Source.cpp:41
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:95
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:32
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:22
The JSON file list parser is used to communicate input to InstallAPI.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Describes a memory block created by an allocation site.
Definition: Descriptor.h:88
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:56