clang 20.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"
18#include <type_traits>
19
20using namespace clang;
21using namespace clang::interp;
22
24
25 // Manually created functions that haven't been assigned proper
26 // parameters yet.
27 if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
28 return nullptr;
29
30 bool IsLambdaStaticInvoker = false;
31 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
32 MD && MD->isLambdaStaticInvoker()) {
33 // For a lambda static invoker, we might have to pick a specialized
34 // version if the lambda is generic. In that case, the picked function
35 // will *NOT* be a static invoker anymore. However, it will still
36 // be a non-static member function, this (usually) requiring an
37 // instance pointer. We suppress that later in this function.
38 IsLambdaStaticInvoker = true;
39
40 const CXXRecordDecl *ClosureClass = MD->getParent();
41 assert(ClosureClass->captures_begin() == ClosureClass->captures_end());
42 if (ClosureClass->isGenericLambda()) {
43 const CXXMethodDecl *LambdaCallOp = ClosureClass->getLambdaCallOperator();
44 assert(MD->isFunctionTemplateSpecialization() &&
45 "A generic lambda's static-invoker function must be a "
46 "template specialization");
47 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
48 FunctionTemplateDecl *CallOpTemplate =
49 LambdaCallOp->getDescribedFunctionTemplate();
50 void *InsertPos = nullptr;
51 const FunctionDecl *CorrespondingCallOpSpecialization =
52 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
53 assert(CorrespondingCallOpSpecialization);
54 FuncDecl = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
55 }
56 }
57
58 // Set up argument indices.
59 unsigned ParamOffset = 0;
60 SmallVector<PrimType, 8> ParamTypes;
61 SmallVector<unsigned, 8> ParamOffsets;
62 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
63
64 // If the return is not a primitive, a pointer to the storage where the
65 // value is initialized in is passed as the first argument. See 'RVO'
66 // elsewhere in the code.
67 QualType Ty = FuncDecl->getReturnType();
68 bool HasRVO = false;
69 if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
70 HasRVO = true;
71 ParamTypes.push_back(PT_Ptr);
72 ParamOffsets.push_back(ParamOffset);
74 }
75
76 // If the function decl is a member decl, the next parameter is
77 // the 'this' pointer. This parameter is pop()ed from the
78 // InterpStack when calling the function.
79 bool HasThisPointer = false;
80 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) {
81 if (!IsLambdaStaticInvoker) {
82 HasThisPointer = MD->isInstance();
83 if (MD->isImplicitObjectMemberFunction()) {
84 ParamTypes.push_back(PT_Ptr);
85 ParamOffsets.push_back(ParamOffset);
87 }
88 }
89
90 // Set up lambda capture to closure record field mapping.
91 if (isLambdaCallOperator(MD)) {
92 // The parent record needs to be complete, we need to know about all
93 // the lambda captures.
94 if (!MD->getParent()->isCompleteDefinition())
95 return nullptr;
96
97 const Record *R = P.getOrCreateRecord(MD->getParent());
98 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
99 FieldDecl *LTC;
100
101 MD->getParent()->getCaptureFields(LC, LTC);
102
103 for (auto Cap : LC) {
104 // Static lambdas cannot have any captures. If this one does,
105 // it has already been diagnosed and we can only ignore it.
106 if (MD->isStatic())
107 return nullptr;
108
109 unsigned Offset = R->getField(Cap.second)->Offset;
110 this->LambdaCaptures[Cap.first] = {
111 Offset, Cap.second->getType()->isReferenceType()};
112 }
113 if (LTC) {
114 QualType CaptureType = R->getField(LTC)->Decl->getType();
115 this->LambdaThisCapture = {R->getField(LTC)->Offset,
116 CaptureType->isReferenceType() ||
117 CaptureType->isPointerType()};
118 }
119 }
120 }
121
122 // Assign descriptors to all parameters.
123 // Composite objects are lowered to pointers.
124 for (const ParmVarDecl *PD : FuncDecl->parameters()) {
125 std::optional<PrimType> T = Ctx.classify(PD->getType());
126 PrimType PT = T.value_or(PT_Ptr);
127 Descriptor *Desc = P.createDescriptor(PD, PT);
128 ParamDescriptors.insert({ParamOffset, {PT, Desc}});
129 Params.insert({PD, {ParamOffset, T != std::nullopt}});
130 ParamOffsets.push_back(ParamOffset);
131 ParamOffset += align(primSize(PT));
132 ParamTypes.push_back(PT);
133 }
134
135 // Create a handle over the emitted code.
136 Function *Func = P.getFunction(FuncDecl);
137 if (!Func) {
138 unsigned BuiltinID = FuncDecl->getBuiltinID();
139 Func =
140 P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
141 std::move(ParamDescriptors), std::move(ParamOffsets),
142 HasThisPointer, HasRVO, BuiltinID);
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 (FuncDecl->willHaveBody() && !FuncDecl->hasBody())) {
150 Func->setDefined(false);
151 return Func;
152 }
153
154 Func->setDefined(true);
155
156 // Lambda static invokers are a special case that we emit custom code for.
157 bool IsEligibleForCompilation = false;
158 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
159 IsEligibleForCompilation = MD->isLambdaStaticInvoker();
160 if (!IsEligibleForCompilation)
161 IsEligibleForCompilation =
162 FuncDecl->isConstexpr() || FuncDecl->hasAttr<MSConstexprAttr>();
163
164 // Compile the function body.
165 if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
166 Func->setIsFullyCompiled(true);
167 return Func;
168 }
169
170 // Create scopes from descriptors.
172 for (auto &DS : Descriptors) {
173 Scopes.emplace_back(std::move(DS));
174 }
175
176 // Set the function's code.
177 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
178 std::move(Scopes), FuncDecl->hasBody());
179 Func->setIsFullyCompiled(true);
180 return Func;
181}
182
183/// Compile an ObjC block, i.e. ^(){}, that thing.
184///
185/// FIXME: We do not support calling the block though, so we create a function
186/// here but do not compile any code for it.
188 const BlockDecl *BD = BE->getBlockDecl();
189 // Set up argument indices.
190 unsigned ParamOffset = 0;
191 SmallVector<PrimType, 8> ParamTypes;
192 SmallVector<unsigned, 8> ParamOffsets;
193 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
194
195 // Assign descriptors to all parameters.
196 // Composite objects are lowered to pointers.
197 for (const ParmVarDecl *PD : BD->parameters()) {
198 std::optional<PrimType> T = Ctx.classify(PD->getType());
199 PrimType PT = T.value_or(PT_Ptr);
200 Descriptor *Desc = P.createDescriptor(PD, PT);
201 ParamDescriptors.insert({ParamOffset, {PT, Desc}});
202 Params.insert({PD, {ParamOffset, T != std::nullopt}});
203 ParamOffsets.push_back(ParamOffset);
204 ParamOffset += align(primSize(PT));
205 ParamTypes.push_back(PT);
206 }
207
208 if (BD->hasCaptures())
209 return nullptr;
210
211 // Create a handle over the emitted code.
212 Function *Func =
213 P.createFunction(BE, ParamOffset, std::move(ParamTypes),
214 std::move(ParamDescriptors), std::move(ParamOffsets),
215 /*HasThisPointer=*/false, /*HasRVO=*/false,
216 /*IsUnevaluatedBuiltin=*/false);
217
218 assert(Func);
219 Func->setDefined(true);
220 // We don't compile the BlockDecl code at all right now.
221 Func->setIsFullyCompiled(true);
222 return Func;
223}
224
226 NextLocalOffset += sizeof(Block);
227 unsigned Location = NextLocalOffset;
228 NextLocalOffset += align(D->getAllocSize());
229 return {Location, D};
230}
231
233 const size_t Target = Code.size();
234 LabelOffsets.insert({Label, Target});
235
236 if (auto It = LabelRelocs.find(Label); It != LabelRelocs.end()) {
237 for (unsigned Reloc : It->second) {
238 using namespace llvm::support;
239
240 // Rewrite the operand of all jumps to this label.
241 void *Location = Code.data() + Reloc - align(sizeof(int32_t));
242 assert(aligned(Location));
243 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
244 endian::write<int32_t, llvm::endianness::native>(Location, Offset);
245 }
246 LabelRelocs.erase(It);
247 }
248}
249
250int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
251 // Compute the PC offset which the jump is relative to.
252 const int64_t Position =
253 Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
254 assert(aligned(Position));
255
256 // If target is known, compute jump offset.
257 if (auto It = LabelOffsets.find(Label); It != LabelOffsets.end())
258 return It->second - Position;
259
260 // Otherwise, record relocation and return dummy offset.
261 LabelRelocs[Label].push_back(Position);
262 return 0ull;
263}
264
265/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
266/// Pointers will be automatically marshalled as 32-bit IDs.
267template <typename T>
268static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
269 bool &Success) {
270 size_t Size;
271
272 if constexpr (std::is_pointer_v<T>)
273 Size = sizeof(uint32_t);
274 else
275 Size = sizeof(T);
276
277 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
278 Success = false;
279 return;
280 }
281
282 // Access must be aligned!
283 size_t ValPos = align(Code.size());
284 Size = align(Size);
285 assert(aligned(ValPos + Size));
286 Code.resize(ValPos + Size);
287
288 if constexpr (!std::is_pointer_v<T>) {
289 new (Code.data() + ValPos) T(Val);
290 } else {
291 uint32_t ID = P.getOrCreateNativePointer(Val);
292 new (Code.data() + ValPos) uint32_t(ID);
293 }
294}
295
296/// Emits a serializable value. These usually (potentially) contain
297/// heap-allocated memory and aren't trivially copyable.
298template <typename T>
299static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
300 bool &Success) {
301 size_t Size = Val.bytesToSerialize();
302
303 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
304 Success = false;
305 return;
306 }
307
308 // Access must be aligned!
309 size_t ValPos = align(Code.size());
310 Size = align(Size);
311 assert(aligned(ValPos + Size));
312 Code.resize(ValPos + Size);
313
314 Val.serialize(Code.data() + ValPos);
315}
316
317template <>
318void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
319 bool &Success) {
320 emitSerialized(Code, Val, Success);
321}
322
323template <>
324void emit(Program &P, std::vector<std::byte> &Code,
325 const IntegralAP<false> &Val, bool &Success) {
326 emitSerialized(Code, Val, Success);
327}
328
329template <>
330void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
331 bool &Success) {
332 emitSerialized(Code, Val, Success);
333}
334
335template <typename... Tys>
336bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args,
337 const SourceInfo &SI) {
338 bool Success = true;
339
340 // The opcode is followed by arguments. The source info is
341 // attached to the address after the opcode.
342 emit(P, Code, Op, Success);
343 if (SI)
344 SrcMap.emplace_back(Code.size(), SI);
345
346 (..., emit(P, Code, Args, Success));
347 return Success;
348}
349
351 return emitJt(getOffset(Label), SourceInfo{});
352}
353
355 return emitJf(getOffset(Label), SourceInfo{});
356}
357
359 return emitJmp(getOffset(Label), SourceInfo{});
360}
361
364 return true;
365}
366
367//===----------------------------------------------------------------------===//
368// Opcode emitters
369//===----------------------------------------------------------------------===//
370
371#define GET_LINK_IMPL
372#include "Opcodes.inc"
373#undef GET_LINK_IMPL
This file provides some common utility functions for processing Lambda related AST Constructs.
StringRef P
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.
const Decl * D
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Target Target
Definition: MachO.h:51
std::string Label
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4474
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
Definition: Decl.h:4593
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:4560
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6414
const BlockDecl * getBlockDecl() const
Definition: Expr.h:6426
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2078
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:1629
capture_const_iterator captures_end() const
Definition: DeclCXX.h:1119
capture_const_iterator captures_begin() const
Definition: DeclCXX.h:1113
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition: DeclCXX.cpp:1688
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2089
bool hasAttr() const
Definition: DeclBase.h:580
Represents a member of a struct/union/class.
Definition: Decl.h:3033
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3250
Represents a function declaration or definition.
Definition: Decl.h:1935
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:4052
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3638
QualType getReturnType() const
Definition: Decl.h:2720
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2649
param_iterator param_begin()
Definition: Decl.h:2661
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2398
bool param_empty() const
Definition: Decl.h:2660
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3163
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:3210
bool willHaveBody() const
True if this function will eventually have a body, once it's fully parsed.
Definition: Decl.h:2561
Declaration of a template function.
Definition: DeclTemplate.h:959
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:1725
A (possibly-)qualified type.
Definition: Type.h:929
A template argument list.
Definition: DeclTemplate.h:250
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
Definition: DeclTemplate.h:280
bool isVoidType() const
Definition: Type.h:8510
bool isPointerType() const
Definition: Type.h:8186
bool isReferenceType() const
Definition: Type.h:8204
QualType getType() const
Definition: Decl.h:682
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.
Function * compileObjCBlock(const BlockExpr *BE)
Compile an ObjC block, i.e.
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:135
Bytecode function.
Definition: Function.h:81
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:273
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:118
Function * createFunction(const FunctionDecl *Def, Ts &&...Args)
Creates a new function from a code range.
Definition: Program.h:98
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Definition: Program.cpp:280
Structure/Class descriptor.
Definition: Record.h:25
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition: Record.cpp:40
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:135
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:131
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:34
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.
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:116
const FieldDecl * Decl
Definition: Record.h:29
Information about a local's storage.
Definition: Function.h:39