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 // We create frames for builtin functions as well, but we can't reliably
156 // diagnose them. The 'in call to' diagnostics for them add no value to the
157 // user _and_ it doesn't generally work since the argument types don't always
158 // match the function prototype. Just ignore them.
159 if (const auto *F = getFunction(); F && F->isBuiltin())
160 return;
161
162 const FunctionDecl *F = getCallee();
163 if (const auto *M = dyn_cast<CXXMethodDecl>(F);
164 M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
165 print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
166 OS << "->";
167 }
168 OS << *F << "(";
169 unsigned Off = 0;
170
171 Off += Func->hasRVO() ? primSize(PT_Ptr) : 0;
172 Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0;
173
174 for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
175 QualType Ty = F->getParamDecl(I)->getType();
176
177 PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr);
178
179 TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
180 Off += align(primSize(PrimTy));
181 if (I + 1 != N)
182 OS << ", ";
183 }
184 OS << ")";
185}
186
188 if (Caller->Caller)
189 return Caller;
190 return S.getSplitFrame();
191}
192
194 if (!Caller->Func) {
195 if (SourceRange NullRange = S.getRange(nullptr, {}); NullRange.isValid())
196 return NullRange;
197 return S.EvalLocation;
198 }
199 return S.getRange(Caller->Func, RetPC - sizeof(uintptr_t));
200}
201
203 if (!Func)
204 return nullptr;
205 return Func->getDecl();
206}
207
208Pointer InterpFrame::getLocalPointer(unsigned Offset) const {
209 assert(Offset < Func->getFrameSize() && "Invalid local offset.");
210 return Pointer(localBlock(Offset));
211}
212
214 // Return the block if it was created previously.
215 if (auto Pt = Params.find(Off); Pt != Params.end())
216 return Pointer(reinterpret_cast<Block *>(Pt->second.get()));
217
218 // Allocate memory to store the parameter and the block metadata.
219 const auto &Desc = Func->getParamDescriptor(Off);
220 size_t BlockSize = sizeof(Block) + Desc.second->getAllocSize();
221 auto Memory = std::make_unique<char[]>(BlockSize);
222 auto *B = new (Memory.get()) Block(Desc.second);
223
224 // Copy the initial value.
225 TYPE_SWITCH(Desc.first, new (B->data()) T(stackRef<T>(Off)));
226
227 // Record the param.
228 Params.insert({Off, std::move(Memory)});
229 return Pointer(B);
230}
231
233 // Implicitly created functions don't have any code we could point at,
234 // so return the call site.
235 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
236 return Caller->getSource(RetPC);
237
238 return S.getSource(Func, PC);
239}
240
242 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
243 return Caller->getExpr(RetPC);
244
245 return S.getExpr(Func, PC);
246}
247
249 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
250 return Caller->getLocation(RetPC);
251
252 return S.getLocation(Func, PC);
253}
254
256 if (Func && (!Func->hasBody() || Func->getDecl()->isImplicit()) && Caller)
257 return Caller->getRange(RetPC);
258
259 return S.getRange(Func, PC);
260}
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3294
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:117
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
QualType getRecordType(const RecordDecl *Decl) const
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:700
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:599
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1971
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2708
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3692
A (possibly-)qualified type.
Definition: Type.h:940
Encodes a location in the source.
A trivial tuple used to represent a source range.
bool isValid() const
bool isReferenceType() const
Definition: Type.h:7625
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:119
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:110
bool isInitialized() const
Definition: InterpBlock.h:78
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
bool isBuiltin() const
Definition: Function.h:183
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
const Function * getFunction() const
Returns the current function.
Definition: InterpFrame.h: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:117
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:115
SourceLocation EvalLocation
Source location of the evaluating expression.
Definition: InterpState.h:121
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:80
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:99
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.
const FunctionProtoType * T
__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:91
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:56