clang 17.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 "Pointer.h"
19#include "Source.h"
20#include "clang/AST/Decl.h"
21#include "llvm/Support/raw_ostream.h"
22
23namespace clang {
24namespace interp {
25class Program;
26class ByteCodeEmitter;
27enum PrimType : uint32_t;
28
29/// Describes a scope block.
30///
31/// The block gathers all the descriptors of the locals defined in this block.
32class Scope final {
33public:
34 /// Information about a local's storage.
35 struct Local {
36 /// Offset of the local in frame.
37 unsigned Offset;
38 /// Descriptor of the local.
40 };
41
43
44 Scope(LocalVectorTy &&Descriptors) : Descriptors(std::move(Descriptors)) {}
45
46 llvm::iterator_range<LocalVectorTy::const_iterator> locals() const {
47 return llvm::make_range(Descriptors.begin(), Descriptors.end());
48 }
49
50private:
51 /// Object descriptors in this block.
52 LocalVectorTy Descriptors;
53};
54
55/// Bytecode function.
56///
57/// Contains links to the bytecode of the function, as well as metadata
58/// describing all arguments and stack-local variables.
59///
60/// # Calling Convention
61///
62/// When calling a function, all argument values must be on the stack.
63///
64/// If the function has a This pointer (i.e. hasThisPointer() returns true,
65/// the argument values need to be preceeded by a Pointer for the This object.
66///
67/// If the function uses Return Value Optimization, the arguments (and
68/// potentially the This pointer) need to be proceeded by a Pointer pointing
69/// to the location to construct the returned value.
70///
71/// After the function has been called, it will remove all arguments,
72/// including RVO and This pointer, from the stack.
73///
74class Function final {
75public:
76 using ParamDescriptor = std::pair<PrimType, Descriptor *>;
77
78 /// Returns the size of the function's local stack.
79 unsigned getFrameSize() const { return FrameSize; }
80 /// Returns the size of the argument stack.
81 unsigned getArgSize() const { return ArgSize; }
82
83 /// Returns a pointer to the start of the code.
84 CodePtr getCodeBegin() const { return Code.data(); }
85 /// Returns a pointer to the end of the code.
86 CodePtr getCodeEnd() const { return Code.data() + Code.size(); }
87
88 /// Returns the original FunctionDecl.
89 const FunctionDecl *getDecl() const { return F; }
90
91 /// Returns the name of the function decl this code
92 /// was generated for.
93 const std::string getName() const {
94 if (!F)
95 return "<<expr>>";
96
97 return F->getQualifiedNameAsString();
98 }
99
100 /// Returns the location.
101 SourceLocation getLoc() const { return Loc; }
102
103 /// Returns a parameter descriptor.
105
106 /// Checks if the first argument is a RVO pointer.
107 bool hasRVO() const { return HasRVO; }
108
109 /// Range over the scope blocks.
110 llvm::iterator_range<llvm::SmallVector<Scope, 2>::const_iterator>
111 scopes() const {
112 return llvm::make_range(Scopes.begin(), Scopes.end());
113 }
114
115 /// Range over argument types.
118 llvm::iterator_range<arg_reverse_iterator> args_reverse() const {
119 return llvm::reverse(ParamTypes);
120 }
121
122 /// Returns a specific scope.
123 Scope &getScope(unsigned Idx) { return Scopes[Idx]; }
124 const Scope &getScope(unsigned Idx) const { return Scopes[Idx]; }
125
126 /// Returns the source information at a given PC.
127 SourceInfo getSource(CodePtr PC) const;
128
129 /// Checks if the function is valid to call in constexpr.
130 bool isConstexpr() const { return IsValid; }
131
132 /// Checks if the function is virtual.
133 bool isVirtual() const;
134
135 /// Checks if the function is a constructor.
136 bool isConstructor() const { return isa<CXXConstructorDecl>(F); }
137 /// Checks if the function is a destructor.
138 bool isDestructor() const { return isa<CXXDestructorDecl>(F); }
139
140 /// Checks if the function is fully done compiling.
141 bool isFullyCompiled() const { return IsFullyCompiled; }
142
143 bool hasThisPointer() const { return HasThisPointer; }
144
145 // Checks if the funtion already has a body attached.
146 bool hasBody() const { return HasBody; }
147
148 unsigned getBuiltinID() const { return F->getBuiltinID(); }
149
150 unsigned getNumParams() const { return ParamTypes.size(); }
151
152private:
153 /// Construct a function representing an actual function.
154 Function(Program &P, const FunctionDecl *F, unsigned ArgSize,
156 llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
157 bool HasThisPointer, bool HasRVO);
158
159 /// Sets the code of a function.
160 void setCode(unsigned NewFrameSize, std::vector<char> &&NewCode, SourceMap &&NewSrcMap,
161 llvm::SmallVector<Scope, 2> &&NewScopes) {
162 FrameSize = NewFrameSize;
163 Code = std::move(NewCode);
164 SrcMap = std::move(NewSrcMap);
165 Scopes = std::move(NewScopes);
166 IsValid = true;
167 HasBody = true;
168 }
169
170 void setIsFullyCompiled(bool FC) { IsFullyCompiled = FC; }
171
172private:
173 friend class Program;
174 friend class ByteCodeEmitter;
175
176 /// Program reference.
177 Program &P;
178 /// Location of the executed code.
179 SourceLocation Loc;
180 /// Declaration this function was compiled from.
181 const FunctionDecl *F;
182 /// Local area size: storage + metadata.
183 unsigned FrameSize = 0;
184 /// Size of the argument stack.
185 unsigned ArgSize;
186 /// Program code.
187 std::vector<char> Code;
188 /// Opcode-to-expression mapping.
189 SourceMap SrcMap;
190 /// List of block descriptors.
192 /// List of argument types.
194 /// Map from byte offset to parameter descriptor.
195 llvm::DenseMap<unsigned, ParamDescriptor> Params;
196 /// Flag to indicate if the function is valid.
197 bool IsValid = false;
198 /// Flag to indicate if the function is done being
199 /// compiled to bytecode.
200 bool IsFullyCompiled = false;
201 /// Flag indicating if this function takes the this pointer
202 /// as the first implicit argument
203 bool HasThisPointer = false;
204 /// Whether this function has Return Value Optimization, i.e.
205 /// the return value is constructed in the caller's stack frame.
206 /// This is done for functions that return non-primive values.
207 bool HasRVO = false;
208 /// If we've already compiled the function's body.
209 bool HasBody = false;
210
211public:
212 /// Dumps the disassembled bytecode to \c llvm::errs().
213 void dump() const;
214 void dump(llvm::raw_ostream &OS) const;
215};
216
217} // namespace interp
218} // namespace clang
219
220#endif
StringRef P
unsigned Offset
Definition: Format.cpp:2778
llvm::raw_ostream & OS
Definition: Logger.cpp:24
Represents a function declaration or definition.
Definition: Decl.h:1917
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3430
std::string getQualifiedNameAsString() const
Definition: Decl.cpp:1646
Encodes a location in the source.
An emitter which links the program to bytecode for later use.
Pointer into the code segment.
Definition: Source.h:26
Bytecode function.
Definition: Function.h:74
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition: Function.h:123
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition: Function.h:84
SourceLocation getLoc() const
Returns the location.
Definition: Function.h:101
bool isDestructor() const
Checks if the function is a destructor.
Definition: Function.h:138
CodePtr getCodeEnd() const
Returns a pointer to the end of the code.
Definition: Function.h:86
const std::string getName() const
Returns the name of the function decl this code was generated for.
Definition: Function.h:93
unsigned getNumParams() const
Definition: Function.h:150
std::pair< PrimType, Descriptor * > ParamDescriptor
Definition: Function.h:76
unsigned getFrameSize() const
Returns the size of the function's local stack.
Definition: Function.h:79
bool isFullyCompiled() const
Checks if the function is fully done compiling.
Definition: Function.h:141
bool isConstructor() const
Checks if the function is a constructor.
Definition: Function.h:136
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:89
bool hasBody() const
Definition: Function.h:146
bool hasThisPointer() const
Definition: Function.h:143
bool isVirtual() const
Checks if the function is virtual.
Definition: Function.cpp:42
unsigned getBuiltinID() const
Definition: Function.h:148
void dump() const
Dumps the disassembled bytecode to llvm::errs().
Definition: Disasm.cpp:34
llvm::iterator_range< arg_reverse_iterator > args_reverse() const
Definition: Function.h:118
bool isConstexpr() const
Checks if the function is valid to call in constexpr.
Definition: Function.h:130
const Scope & getScope(unsigned Idx) const
Definition: Function.h:124
unsigned getArgSize() const
Returns the size of the argument stack.
Definition: Function.h:81
SmallVectorImpl< PrimType >::const_reverse_iterator arg_reverse_iterator
Range over argument types.
Definition: Function.h:117
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:32
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
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Describes a scope block.
Definition: Function.h:32
llvm::SmallVector< Local, 8 > LocalVectorTy
Definition: Function.h:42
llvm::iterator_range< LocalVectorTy::const_iterator > locals() const
Definition: Function.h:46
Scope(LocalVectorTy &&Descriptors)
Definition: Function.h:44
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:70
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:88
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:30
Definition: Format.h:4674
Describes a memory block created by an allocation site.
Definition: Descriptor.h:76
Information about a local's storage.
Definition: Function.h:35
unsigned Offset
Offset of the local in frame.
Definition: Function.h:37
Descriptor * Desc
Descriptor of the local.
Definition: Function.h:39