clang 19.0.0git
ByteCodeEmitter.cpp
Go to the documentation of this file.
1//===--- ByteCodeEmitter.cpp - Instruction emitter 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 "ByteCodeEmitter.h"
10#include "Context.h"
11#include "Floating.h"
12#include "IntegralAP.h"
13#include "Opcode.h"
14#include "Program.h"
15#include "clang/AST/ASTLambda.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
19#include <type_traits>
20
21using namespace clang;
22using namespace clang::interp;
23
24/// Unevaluated builtins don't get their arguments put on the stack
25/// automatically. They instead operate on the AST of their Call
26/// Expression.
27/// Similar information is available via ASTContext::BuiltinInfo,
28/// but that is not correct for our use cases.
29static bool isUnevaluatedBuiltin(unsigned BuiltinID) {
30 return BuiltinID == Builtin::BI__builtin_classify_type;
31}
32
34 bool IsLambdaStaticInvoker = false;
35 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
36 MD && MD->isLambdaStaticInvoker()) {
37 // For a lambda static invoker, we might have to pick a specialized
38 // version if the lambda is generic. In that case, the picked function
39 // will *NOT* be a static invoker anymore. However, it will still
40 // be a non-static member function, this (usually) requiring an
41 // instance pointer. We suppress that later in this function.
42 IsLambdaStaticInvoker = true;
43
44 const CXXRecordDecl *ClosureClass = MD->getParent();
45 assert(ClosureClass->captures_begin() == ClosureClass->captures_end());
46 if (ClosureClass->isGenericLambda()) {
47 const CXXMethodDecl *LambdaCallOp = ClosureClass->getLambdaCallOperator();
48 assert(MD->isFunctionTemplateSpecialization() &&
49 "A generic lambda's static-invoker function must be a "
50 "template specialization");
51 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
52 FunctionTemplateDecl *CallOpTemplate =
53 LambdaCallOp->getDescribedFunctionTemplate();
54 void *InsertPos = nullptr;
55 const FunctionDecl *CorrespondingCallOpSpecialization =
56 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
57 assert(CorrespondingCallOpSpecialization);
58 FuncDecl = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
59 }
60 }
61
62 // Set up argument indices.
63 unsigned ParamOffset = 0;
64 SmallVector<PrimType, 8> ParamTypes;
65 SmallVector<unsigned, 8> ParamOffsets;
66 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
67
68 // If the return is not a primitive, a pointer to the storage where the
69 // value is initialized in is passed as the first argument. See 'RVO'
70 // elsewhere in the code.
71 QualType Ty = FuncDecl->getReturnType();
72 bool HasRVO = false;
73 if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
74 HasRVO = true;
75 ParamTypes.push_back(PT_Ptr);
76 ParamOffsets.push_back(ParamOffset);
78 }
79
80 // If the function decl is a member decl, the next parameter is
81 // the 'this' pointer. This parameter is pop()ed from the
82 // InterpStack when calling the function.
83 bool HasThisPointer = false;
84 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) {
85 if (MD->isImplicitObjectMemberFunction() && !IsLambdaStaticInvoker) {
86 HasThisPointer = true;
87 ParamTypes.push_back(PT_Ptr);
88 ParamOffsets.push_back(ParamOffset);
90 }
91
92 // Set up lambda capture to closure record field mapping.
93 if (isLambdaCallOperator(MD)) {
94 const Record *R = P.getOrCreateRecord(MD->getParent());
95 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
96 FieldDecl *LTC;
97
98 MD->getParent()->getCaptureFields(LC, LTC);
99
100 for (auto Cap : LC) {
101 // Static lambdas cannot have any captures. If this one does,
102 // it has already been diagnosed and we can only ignore it.
103 if (MD->isStatic())
104 return nullptr;
105
106 unsigned Offset = R->getField(Cap.second)->Offset;
107 this->LambdaCaptures[Cap.first] = {
108 Offset, Cap.second->getType()->isReferenceType()};
109 }
110 if (LTC) {
111 QualType CaptureType = R->getField(LTC)->Decl->getType();
112 this->LambdaThisCapture = {R->getField(LTC)->Offset,
113 CaptureType->isReferenceType() ||
114 CaptureType->isPointerType()};
115 }
116 }
117 }
118
119 // Assign descriptors to all parameters.
120 // Composite objects are lowered to pointers.
121 for (const ParmVarDecl *PD : FuncDecl->parameters()) {
122 std::optional<PrimType> T = Ctx.classify(PD->getType());
123 PrimType PT = T.value_or(PT_Ptr);
124 Descriptor *Desc = P.createDescriptor(PD, PT);
125 ParamDescriptors.insert({ParamOffset, {PT, Desc}});
126 Params.insert({PD, {ParamOffset, T != std::nullopt}});
127 ParamOffsets.push_back(ParamOffset);
128 ParamOffset += align(primSize(PT));
129 ParamTypes.push_back(PT);
130 }
131
132 // Create a handle over the emitted code.
133 Function *Func = P.getFunction(FuncDecl);
134 if (!Func) {
135 bool IsUnevaluatedBuiltin = false;
136 if (unsigned BI = FuncDecl->getBuiltinID())
137 IsUnevaluatedBuiltin = isUnevaluatedBuiltin(BI);
138
139 Func =
140 P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
141 std::move(ParamDescriptors), std::move(ParamOffsets),
142 HasThisPointer, HasRVO, IsUnevaluatedBuiltin);
143 }
144
145 assert(Func);
146 // For not-yet-defined functions, we only create a Function instance and
147 // compile their body later.
148 if (!FuncDecl->isDefined()) {
149 Func->setDefined(false);
150 return Func;
151 }
152
153 Func->setDefined(true);
154
155 // Lambda static invokers are a special case that we emit custom code for.
156 bool IsEligibleForCompilation = false;
157 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
158 IsEligibleForCompilation = MD->isLambdaStaticInvoker();
159 if (!IsEligibleForCompilation)
160 IsEligibleForCompilation =
161 FuncDecl->isConstexpr() || FuncDecl->hasAttr<MSConstexprAttr>();
162
163 // Compile the function body.
164 if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
165 Func->setIsFullyCompiled(true);
166 return Func;
167 }
168
169 // Create scopes from descriptors.
171 for (auto &DS : Descriptors) {
172 Scopes.emplace_back(std::move(DS));
173 }
174
175 // Set the function's code.
176 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
177 std::move(Scopes), FuncDecl->hasBody());
178 Func->setIsFullyCompiled(true);
179 return Func;
180}
181
183 NextLocalOffset += sizeof(Block);
184 unsigned Location = NextLocalOffset;
185 NextLocalOffset += align(D->getAllocSize());
186 return {Location, D};
187}
188
190 const size_t Target = Code.size();
191 LabelOffsets.insert({Label, Target});
192
193 if (auto It = LabelRelocs.find(Label);
194 It != LabelRelocs.end()) {
195 for (unsigned Reloc : It->second) {
196 using namespace llvm::support;
197
198 // Rewrite the operand of all jumps to this label.
199 void *Location = Code.data() + Reloc - align(sizeof(int32_t));
200 assert(aligned(Location));
201 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
202 endian::write<int32_t, llvm::endianness::native>(Location, Offset);
203 }
204 LabelRelocs.erase(It);
205 }
206}
207
208int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
209 // Compute the PC offset which the jump is relative to.
210 const int64_t Position =
211 Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
212 assert(aligned(Position));
213
214 // If target is known, compute jump offset.
215 if (auto It = LabelOffsets.find(Label);
216 It != LabelOffsets.end())
217 return It->second - Position;
218
219 // Otherwise, record relocation and return dummy offset.
220 LabelRelocs[Label].push_back(Position);
221 return 0ull;
222}
223
224/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
225/// Pointers will be automatically marshalled as 32-bit IDs.
226template <typename T>
227static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
228 bool &Success) {
229 size_t Size;
230
231 if constexpr (std::is_pointer_v<T>)
232 Size = sizeof(uint32_t);
233 else
234 Size = sizeof(T);
235
236 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
237 Success = false;
238 return;
239 }
240
241 // Access must be aligned!
242 size_t ValPos = align(Code.size());
243 Size = align(Size);
244 assert(aligned(ValPos + Size));
245 Code.resize(ValPos + Size);
246
247 if constexpr (!std::is_pointer_v<T>) {
248 new (Code.data() + ValPos) T(Val);
249 } else {
250 uint32_t ID = P.getOrCreateNativePointer(Val);
251 new (Code.data() + ValPos) uint32_t(ID);
252 }
253}
254
255/// Emits a serializable value. These usually (potentially) contain
256/// heap-allocated memory and aren't trivially copyable.
257template <typename T>
258static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
259 bool &Success) {
260 size_t Size = Val.bytesToSerialize();
261
262 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
263 Success = false;
264 return;
265 }
266
267 // Access must be aligned!
268 size_t ValPos = align(Code.size());
269 Size = align(Size);
270 assert(aligned(ValPos + Size));
271 Code.resize(ValPos + Size);
272
273 Val.serialize(Code.data() + ValPos);
274}
275
276template <>
277void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
278 bool &Success) {
279 emitSerialized(Code, Val, Success);
280}
281
282template <>
283void emit(Program &P, std::vector<std::byte> &Code,
284 const IntegralAP<false> &Val, bool &Success) {
285 emitSerialized(Code, Val, Success);
286}
287
288template <>
289void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
290 bool &Success) {
291 emitSerialized(Code, Val, Success);
292}
293
294template <typename... Tys>
295bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) {
296 bool Success = true;
297
298 // The opcode is followed by arguments. The source info is
299 // attached to the address after the opcode.
300 emit(P, Code, Op, Success);
301 if (SI)
302 SrcMap.emplace_back(Code.size(), SI);
303
304 (..., emit(P, Code, Args, Success));
305 return Success;
306}
307
309 return emitJt(getOffset(Label), SourceInfo{});
310}
311
313 return emitJf(getOffset(Label), SourceInfo{});
314}
315
317 return emitJmp(getOffset(Label), SourceInfo{});
318}
319
322 return true;
323}
324
325//===----------------------------------------------------------------------===//
326// Opcode emitters
327//===----------------------------------------------------------------------===//
328
329#define GET_LINK_IMPL
330#include "Opcodes.inc"
331#undef GET_LINK_IMPL
This file provides some common utility functions for processing Lambda related AST Constructs.
StringRef P
Defines enum values for all the target-independent builtin functions.
static bool isUnevaluatedBuiltin(unsigned BuiltinID)
Unevaluated builtins don't get their arguments put on the stack automatically.
static void emit(Program &P, std::vector< std::byte > &Code, const T &Val, bool &Success)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
static void emitSerialized(std::vector< std::byte > &Code, const T &Val, bool &Success)
Emits a serializable value.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Target Target
Definition: MachO.h:47
std::string Label
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2057
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
bool isGenericLambda() const
Determine whether this class describes a generic lambda function object (i.e.
Definition: DeclCXX.cpp:1563
capture_const_iterator captures_end() const
Definition: DeclCXX.h:1110
capture_const_iterator captures_begin() const
Definition: DeclCXX.h:1104
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition: DeclCXX.cpp:1593
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2065
bool hasAttr() const
Definition: DeclBase.h:582
Represents a member of a struct/union/class.
Definition: Decl.h:3058
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3271
Represents a function declaration or definition.
Definition: Decl.h:1971
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:4042
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3632
QualType getReturnType() const
Definition: Decl.h:2755
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2684
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2433
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3156
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition: Decl.cpp:3203
Declaration of a template function.
Definition: DeclTemplate.h:958
FunctionDecl * findSpecialization(ArrayRef< TemplateArgument > Args, void *&InsertPos)
Return the specialization with the provided arguments if it exists, otherwise return the insertion po...
Represents a parameter to a function.
Definition: Decl.h:1761
A (possibly-)qualified type.
Definition: Type.h:738
A template argument list.
Definition: DeclTemplate.h:244
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
Definition: DeclTemplate.h:274
bool isVoidType() const
Definition: Type.h:7695
bool isPointerType() const
Definition: Type.h:7402
bool isReferenceType() const
Definition: Type.h:7414
QualType getType() const
Definition: Decl.h:717
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
bool jump(const LabelTy &Label)
void emitLabel(LabelTy Label)
Define a label.
ParamOffset LambdaThisCapture
Offset of the This parameter in a lambda record.
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
bool fallthrough(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for local registration.
Function * compileFunc(const FunctionDecl *FuncDecl)
Compiles the function into the module.
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
bool jumpFalse(const LabelTy &Label)
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:119
Bytecode function.
Definition: Function.h:77
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Function * getFunction(const FunctionDecl *F)
Returns a function.
Definition: Program.cpp:231
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, Descriptor::MetadataSize MDSize=std::nullopt, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
Definition: Program.h:116
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
Definition: Program.h:95
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Definition: Program.cpp:238
Structure/Class descriptor.
Definition: Record.h:25
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition: Record.cpp:39
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:103
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.
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:27
const FunctionProtoType * T
@ Success
Template argument deduction was successful.
Describes a memory block created by an allocation site.
Definition: Descriptor.h:88
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:201
const FieldDecl * Decl
Definition: Record.h:29
Information about a local's storage.
Definition: Function.h:38