clang 17.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)
26 : Caller(Caller), S(S), Func(Func), RetPC(RetPC),
27 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
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
73 for (auto &Param : Params)
74 S.deallocate(reinterpret_cast<Block *>(Param.second.get()));
75}
76
77void InterpFrame::destroy(unsigned Idx) {
78 for (auto &Local : Func->getScope(Idx).locals()) {
79 S.deallocate(localBlock(Local.Offset));
80 }
81}
82
84 for (PrimType Ty : Func->args_reverse())
85 TYPE_SWITCH(Ty, S.Stk.discard<T>());
86}
87
88template <typename T>
89static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType) {
90 OS << V;
91}
92
93template <>
94void 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](Descriptor *Desc) {
102 if (auto *D = Desc->asDecl()) {
103 // Subfields or named values.
104 if (auto *VD = dyn_cast<ValueDecl>(D)) {
105 OS << *VD;
106 return;
107 }
108 // Base classes.
109 if (isa<RecordDecl>(D)) {
110 return;
111 }
112 }
113 // Temporary expression.
114 if (auto *E = Desc->asExpr()) {
115 E->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
116 return;
117 }
118 llvm_unreachable("Invalid descriptor type");
119 };
120
121 if (!Ty->isReferenceType())
122 OS << "&";
124 for (Pointer F = P; !F.isRoot(); ) {
125 Levels.push_back(F);
126 F = F.isArrayElement() ? F.getArray().expand() : F.getBase();
127 }
128
129 printDesc(P.getDeclDesc());
130 for (const auto &It : Levels) {
131 if (It.inArray()) {
132 OS << "[" << It.expand().getIndex() << "]";
133 continue;
134 }
135 if (auto Index = It.getIndex()) {
136 OS << " + " << Index;
137 continue;
138 }
139 OS << ".";
140 printDesc(It.getFieldDesc());
141 }
142}
143
144void InterpFrame::describe(llvm::raw_ostream &OS) {
145 const FunctionDecl *F = getCallee();
146 auto *M = dyn_cast<CXXMethodDecl>(F);
147 if (M && M->isInstance() && !isa<CXXConstructorDecl>(F)) {
148 print(OS, This, S.getCtx(), S.getCtx().getRecordType(M->getParent()));
149 OS << "->";
150 }
151 OS << *F << "(";
152 unsigned Off = 0;
153
154 Off += Func->hasRVO() ? primSize(PT_Ptr) : 0;
155 Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0;
156
157 for (unsigned I = 0, N = F->getNumParams(); I < N; ++I) {
158 QualType Ty = F->getParamDecl(I)->getType();
159
160 PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr);
161
162 TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getCtx(), Ty));
163 Off += align(primSize(PrimTy));
164 if (I + 1 != N)
165 OS << ", ";
166 }
167 OS << ")";
168}
169
171 if (Caller->Caller)
172 return Caller;
173 return S.getSplitFrame();
174}
175
177 if (!Caller->Func)
178 return S.getLocation(nullptr, {});
179 return S.getLocation(Caller->Func, RetPC - sizeof(uintptr_t));
180}
181
183 return Func->getDecl();
184}
185
187 assert(Offset < Func->getFrameSize() && "Invalid local offset.");
188 return Pointer(localBlock(Offset), sizeof(InlineDescriptor));
189}
190
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
213 return S.getSource(Func, PC);
214}
215
217 return S.getExpr(Func, PC);
218}
219
221 return S.getLocation(Func, PC);
222}
223
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3216
StringRef P
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
unsigned Offset
Definition: Format.cpp:2778
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:89
llvm::raw_ostream & OS
Definition: Logger.cpp:24
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:84
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:684
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1917
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2609
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3514
A (possibly-)qualified type.
Definition: Type.h:736
Encodes a location in the source.
bool isReferenceType() const
Definition: Type.h:6922
QualType getType() const
Definition: Decl.h:712
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void invokeCtor()
Invokes the constructor.
Definition: InterpBlock.h:100
Pointer into the code segment.
Definition: Source.h:26
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:80
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Bytecode function.
Definition: Function.h:74
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:123
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition: Function.h:79
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:89
bool hasThisPointer() const
Definition: Function.h:143
llvm::iterator_range< arg_reverse_iterator > args_reverse() const
Definition: Function.h:118
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition: Function.cpp:26
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::const_iterator > scopes() const
Range over the scope blocks.
Definition: Function.h:111
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition: Function.h:107
Frame storing local variables.
Definition: InterpFrame.h:29
void popArgs()
Pops the arguments off the stack.
Definition: InterpFrame.cpp:83
SourceLocation getCallLocation() const override
Returns the location of the call to the frame.
const Expr * getExpr(CodePtr PC) const
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:32
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:72
void describe(llvm::raw_ostream &OS) override
Describes the frame with arguments for diagnostic purposes.
InterpFrame(InterpState &S, const Function *Func, InterpFrame *Caller, CodePtr RetPC)
Creates a new frame for a method call.
Definition: InterpFrame.cpp:24
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
Frame * getCaller() const override
Returns the parent frame object.
void destroy(unsigned Idx)
Invokes the destructors for a scope.
Definition: InterpFrame.cpp:77
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
const FunctionDecl * getCallee() const override
Returns the caller.
void discard()
Discards the top value from the stack.
Definition: InterpStack.h:55
Interpreter context.
Definition: InterpState.h:34
Context & Ctx
Interpreter Context.
Definition: InterpState.h:103
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:85
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:101
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:49
ASTContext & getCtx() const override
Definition: InterpState.h:53
A pointer to a memory block, live or dead.
Definition: Pointer.h:61
Describes a scope block.
Definition: Function.h:32
llvm::iterator_range< LocalVectorTy::const_iterator > locals() const
Definition: Function.h:46
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:70
SourceLocation getLocation(const Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition: Source.cpp:37
const Expr * getExpr(const Function *F, CodePtr PC) const
Returns the expression if an opcode belongs to one, null otherwise.
Definition: Source.cpp:31
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:66
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:30
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:21
__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:76
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:53