clang 22.0.0git
Function.h
Go to the documentation of this file.
1//===--- Function.h - Bytecode function 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// Defines the Function class which holds all bytecode function-specific data.
10//
11// The scope class which describes local variables is also defined here.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_INTERP_FUNCTION_H
16#define LLVM_CLANG_AST_INTERP_FUNCTION_H
17
18#include "Descriptor.h"
19#include "Source.h"
20#include "clang/AST/Attr.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/Support/raw_ostream.h"
25
26namespace clang {
27namespace interp {
28class Program;
29class ByteCodeEmitter;
30class Pointer;
31enum PrimType : uint8_t;
32
33/// Describes a scope block.
34///
35/// The block gathers all the descriptors of the locals defined in this block.
36class Scope final {
37public:
38 /// Information about a local's storage.
39 struct Local {
40 /// Offset of the local in frame.
41 unsigned Offset;
42 /// Descriptor of the local.
44 /// If the cleanup for this local should be emitted.
45 bool EnabledByDefault = true;
46 };
47
49
50 Scope(LocalVectorTy &&Descriptors) : Descriptors(std::move(Descriptors)) {}
51
52 llvm::iterator_range<LocalVectorTy::const_iterator> locals() const {
53 return llvm::make_range(Descriptors.begin(), Descriptors.end());
54 }
55
56 llvm::iterator_range<LocalVectorTy::const_reverse_iterator>
58 return llvm::reverse(Descriptors);
59 }
60
61private:
62 /// Object descriptors in this block.
63 LocalVectorTy Descriptors;
64};
65
67 llvm::PointerUnion<const FunctionDecl *, const BlockExpr *>;
68
69/// Bytecode function.
70///
71/// Contains links to the bytecode of the function, as well as metadata
72/// describing all arguments and stack-local variables.
73///
74/// # Calling Convention
75///
76/// When calling a function, all argument values must be on the stack.
77///
78/// If the function has a This pointer (i.e. hasThisPointer() returns true,
79/// the argument values need to be preceeded by a Pointer for the This object.
80///
81/// If the function uses Return Value Optimization, the arguments (and
82/// potentially the This pointer) need to be preceeded by a Pointer pointing
83/// to the location to construct the returned value.
84///
85/// After the function has been called, it will remove all arguments,
86/// including RVO and This pointer, from the stack.
87///
88class Function final {
89public:
98 using ParamDescriptor = std::pair<PrimType, Descriptor *>;
99
100 /// Returns the size of the function's local stack.
101 unsigned getFrameSize() const { return FrameSize; }
102 /// Returns the size of the argument stack.
103 unsigned getArgSize() const { return ArgSize; }
104
105 /// Returns a pointer to the start of the code.
106 CodePtr getCodeBegin() const { return Code.data(); }
107 /// Returns a pointer to the end of the code.
108 CodePtr getCodeEnd() const { return Code.data() + Code.size(); }
109
110 /// Returns the original FunctionDecl.
111 const FunctionDecl *getDecl() const {
112 return dyn_cast<const FunctionDecl *>(Source);
113 }
114 const BlockExpr *getExpr() const {
115 return dyn_cast<const BlockExpr *>(Source);
116 }
117
118 /// Returns the name of the function decl this code
119 /// was generated for.
120 std::string getName() const {
121 if (!Source || !getDecl())
122 return "<<expr>>";
123
125 }
126
127 /// Returns a parameter descriptor.
128 ParamDescriptor getParamDescriptor(unsigned Offset) const;
129
130 /// Checks if the first argument is a RVO pointer.
131 bool hasRVO() const { return HasRVO; }
132
133 bool hasNonNullAttr() const { return getDecl()->hasAttr<NonNullAttr>(); }
134
135 /// Range over the scope blocks.
136 llvm::iterator_range<llvm::SmallVector<Scope, 2>::const_iterator>
137 scopes() const {
138 return llvm::make_range(Scopes.begin(), Scopes.end());
139 }
140
141 /// Range over argument types.
144 llvm::iterator_range<arg_reverse_iterator> args_reverse() const {
145 return llvm::reverse(ParamTypes);
146 }
147
148 /// Returns a specific scope.
149 Scope &getScope(unsigned Idx) { return Scopes[Idx]; }
150 const Scope &getScope(unsigned Idx) const { return Scopes[Idx]; }
151
152 /// Returns the source information at a given PC.
153 SourceInfo getSource(CodePtr PC) const;
154
155 /// Checks if the function is valid to call.
156 bool isValid() const { return IsValid || isLambdaStaticInvoker(); }
157
158 /// Checks if the function is virtual.
159 bool isVirtual() const { return Virtual; };
160 bool isImmediate() const { return Immediate; }
161 bool isConstexpr() const { return Constexpr; }
162
163 /// Checks if the function is a constructor.
164 bool isConstructor() const { return Kind == FunctionKind::Ctor; }
165 /// Checks if the function is a destructor.
166 bool isDestructor() const { return Kind == FunctionKind::Dtor; }
167 /// Checks if the function is copy or move operator.
168 bool isCopyOrMoveOperator() const {
170 }
171
172 /// Returns whether this function is a lambda static invoker,
173 /// which we generate custom byte code for.
176 }
177
178 /// Returns whether this function is the call operator
179 /// of a lambda record decl.
180 bool isLambdaCallOperator() const {
182 }
183
184 /// Returns the parent record decl, if any.
186 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
187 dyn_cast<const FunctionDecl *>(Source)))
188 return MD->getParent();
189 return nullptr;
190 }
191
192 /// Checks if the function is fully done compiling.
193 bool isFullyCompiled() const { return IsFullyCompiled; }
194
195 bool hasThisPointer() const { return HasThisPointer; }
196
197 /// Checks if the function already has a body attached.
198 bool hasBody() const { return HasBody; }
199
200 /// Checks if the function is defined.
201 bool isDefined() const { return Defined; }
202
203 bool isVariadic() const { return Variadic; }
204
205 unsigned getNumParams() const { return ParamTypes.size(); }
206
207 /// Returns the number of parameter this function takes when it's called,
208 /// i.e excluding the instance pointer and the RVO pointer.
209 unsigned getNumWrittenParams() const {
210 assert(getNumParams() >= (unsigned)(hasThisPointer() + hasRVO()));
211 return getNumParams() - hasThisPointer() - hasRVO();
212 }
213 unsigned getWrittenArgSize() const {
214 return ArgSize - (align(primSize(PT_Ptr)) * (hasThisPointer() + hasRVO()));
215 }
216
218 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
219 dyn_cast<const FunctionDecl *>(Source)))
220 return MD->isExplicitObjectMemberFunction();
221 return false;
222 }
223
224 unsigned getParamOffset(unsigned ParamIndex) const {
225 return ParamOffsets[ParamIndex];
226 }
227
228 PrimType getParamType(unsigned ParamIndex) const {
229 return ParamTypes[ParamIndex];
230 }
231
232private:
233 /// Construct a function representing an actual function.
234 Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
236 llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
237 llvm::SmallVectorImpl<unsigned> &&ParamOffsets, bool HasThisPointer,
238 bool HasRVO, bool IsLambdaStaticInvoker);
239
240 /// Sets the code of a function.
241 void setCode(FunctionDeclTy Source, unsigned NewFrameSize,
242 llvm::SmallVector<std::byte> &&NewCode, SourceMap &&NewSrcMap,
243 llvm::SmallVector<Scope, 2> &&NewScopes, bool NewHasBody) {
244 this->Source = Source;
245 FrameSize = NewFrameSize;
246 Code = std::move(NewCode);
247 SrcMap = std::move(NewSrcMap);
248 Scopes = std::move(NewScopes);
249 IsValid = true;
250 HasBody = NewHasBody;
251 }
252
253 void setIsFullyCompiled(bool FC) { IsFullyCompiled = FC; }
254 void setDefined(bool D) { Defined = D; }
255
256private:
257 friend class Program;
258 friend class ByteCodeEmitter;
259 friend class Context;
260
261 /// Program reference.
262 Program &P;
263 /// Function Kind.
264 FunctionKind Kind;
265 /// Declaration this function was compiled from.
266 FunctionDeclTy Source;
267 /// Local area size: storage + metadata.
268 unsigned FrameSize = 0;
269 /// Size of the argument stack.
270 unsigned ArgSize;
271 /// Program code.
273 /// Opcode-to-expression mapping.
274 SourceMap SrcMap;
275 /// List of block descriptors.
277 /// List of argument types.
279 /// Map from byte offset to parameter descriptor.
280 llvm::DenseMap<unsigned, ParamDescriptor> Params;
281 /// List of parameter offsets.
283 /// Flag to indicate if the function is valid.
284 LLVM_PREFERRED_TYPE(bool)
285 unsigned IsValid : 1;
286 /// Flag to indicate if the function is done being
287 /// compiled to bytecode.
288 LLVM_PREFERRED_TYPE(bool)
289 unsigned IsFullyCompiled : 1;
290 /// Flag indicating if this function takes the this pointer
291 /// as the first implicit argument
292 LLVM_PREFERRED_TYPE(bool)
293 unsigned HasThisPointer : 1;
294 /// Whether this function has Return Value Optimization, i.e.
295 /// the return value is constructed in the caller's stack frame.
296 /// This is done for functions that return non-primive values.
297 LLVM_PREFERRED_TYPE(bool)
298 unsigned HasRVO : 1;
299 /// If we've already compiled the function's body.
300 LLVM_PREFERRED_TYPE(bool)
301 unsigned HasBody : 1;
302 LLVM_PREFERRED_TYPE(bool)
303 unsigned Defined : 1;
304 LLVM_PREFERRED_TYPE(bool)
305 unsigned Variadic : 1;
306 LLVM_PREFERRED_TYPE(bool)
307 unsigned Virtual : 1;
308 LLVM_PREFERRED_TYPE(bool)
309 unsigned Immediate : 1;
310 LLVM_PREFERRED_TYPE(bool)
311 unsigned Constexpr : 1;
312
313public:
314 /// Dumps the disassembled bytecode to \c llvm::errs().
315 void dump(CodePtr PC = {}) const;
316 void dump(llvm::raw_ostream &OS, CodePtr PC = {}) const;
317};
318
319} // namespace interp
320} // namespace clang
321
322#endif
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition Expr.h:6558
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2109
bool hasAttr() const
Definition DeclBase.h:577
Represents a function declaration or definition.
Definition Decl.h:2000
std::string getQualifiedNameAsString() const
Definition Decl.cpp:1680
An emitter which links the program to bytecode for later use.
Pointer into the code segment.
Definition Source.h:30
friend class Program
Definition Function.h:257
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition Function.h:149
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition Function.h:106
bool isDestructor() const
Checks if the function is a destructor.
Definition Function.h:166
CodePtr getCodeEnd() const
Returns a pointer to the end of the code.
Definition Function.h:108
friend class ByteCodeEmitter
Definition Function.h:258
std::string getName() const
Returns the name of the function decl this code was generated for.
Definition Function.h:120
bool isVirtual() const
Checks if the function is virtual.
Definition Function.h:159
unsigned getNumParams() const
Definition Function.h:205
bool isDefined() const
Checks if the function is defined.
Definition Function.h:201
bool hasNonNullAttr() const
Definition Function.h:133
PrimType getParamType(unsigned ParamIndex) const
Definition Function.h:228
std::pair< PrimType, Descriptor * > ParamDescriptor
Definition Function.h:98
const CXXRecordDecl * getParentDecl() const
Returns the parent record decl, if any.
Definition Function.h:185
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition Function.h:101
bool isLambdaCallOperator() const
Returns whether this function is the call operator of a lambda record decl.
Definition Function.h:180
const BlockExpr * getExpr() const
Definition Function.h:114
bool isFullyCompiled() const
Checks if the function is fully done compiling.
Definition Function.h:193
bool isConstructor() const
Checks if the function is a constructor.
Definition Function.h:164
unsigned getParamOffset(unsigned ParamIndex) const
Definition Function.h:224
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition Function.h:111
bool hasBody() const
Checks if the function already has a body attached.
Definition Function.h:198
bool hasThisPointer() const
Definition Function.h:195
SmallVectorImpl< PrimType >::const_reverse_iterator arg_reverse_iterator
Range over argument types.
Definition Function.h:142
void dump(CodePtr PC={}) const
Dumps the disassembled bytecode to llvm::errs().
Definition Disasm.cpp:141
void dump(llvm::raw_ostream &OS, CodePtr PC={}) const
llvm::iterator_range< arg_reverse_iterator > args_reverse() const
Definition Function.h:144
bool isConstexpr() const
Definition Function.h:161
const Scope & getScope(unsigned Idx) const
Definition Function.h:150
bool isThisPointerExplicit() const
Definition Function.h:217
unsigned getNumWrittenParams() const
Returns the number of parameter this function takes when it's called, i.e excluding the instance poin...
Definition Function.h:209
friend class Context
Definition Function.h:259
unsigned getWrittenArgSize() const
Definition Function.h:213
unsigned getArgSize() const
Returns the size of the argument stack.
Definition Function.h:103
bool isLambdaStaticInvoker() const
Returns whether this function is a lambda static invoker, which we generate custom byte code for.
Definition Function.h:174
bool isVariadic() const
Definition Function.h:203
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition Function.cpp:63
ParamDescriptor getParamDescriptor(unsigned Offset) const
Returns a parameter descriptor.
Definition Function.cpp:57
bool isValid() const
Checks if the function is valid to call.
Definition Function.h:156
bool isImmediate() const
Definition Function.h:160
bool isCopyOrMoveOperator() const
Checks if the function is copy or move operator.
Definition Function.h:168
llvm::iterator_range< llvm::SmallVector< Scope, 2 >::const_iterator > scopes() const
Range over the scope blocks.
Definition Function.h:137
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition Function.h:131
A pointer to a memory block, live or dead.
Definition Pointer.h:92
The program contains and links the bytecode for all functions.
Definition Program.h:36
Describes a scope block.
Definition Function.h:36
llvm::SmallVector< Local, 8 > LocalVectorTy
Definition Function.h:48
llvm::iterator_range< LocalVectorTy::const_iterator > locals() const
Definition Function.h:52
Scope(LocalVectorTy &&Descriptors)
Definition Function.h:50
llvm::iterator_range< LocalVectorTy::const_reverse_iterator > locals_reverse() const
Definition Function.h:57
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:189
llvm::PointerUnion< const FunctionDecl *, const BlockExpr * > FunctionDeclTy
Definition Function.h:66
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition Source.h:98
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:23
The JSON file list parser is used to communicate input to InstallAPI.
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
Information about a local's storage.
Definition Function.h:39
unsigned Offset
Offset of the local in frame.
Definition Function.h:41
bool EnabledByDefault
If the cleanup for this local should be emitted.
Definition Function.h:45
Descriptor * Desc
Descriptor of the local.
Definition Function.h:43