clang 18.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 "ByteCodeGenError.h"
11#include "Context.h"
12#include "Floating.h"
13#include "Opcode.h"
14#include "Program.h"
15#include "clang/AST/ASTLambda.h"
16#include "clang/AST/DeclCXX.h"
18#include <type_traits>
19
20using namespace clang;
21using namespace clang::interp;
22
25 // Set up argument indices.
26 unsigned ParamOffset = 0;
27 SmallVector<PrimType, 8> ParamTypes;
28 SmallVector<unsigned, 8> ParamOffsets;
29 llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
30
31 // If the return is not a primitive, a pointer to the storage where the
32 // value is initialized in is passed as the first argument. See 'RVO'
33 // elsewhere in the code.
34 QualType Ty = FuncDecl->getReturnType();
35 bool HasRVO = false;
36 if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
37 HasRVO = true;
38 ParamTypes.push_back(PT_Ptr);
39 ParamOffsets.push_back(ParamOffset);
41 }
42
43 // If the function decl is a member decl, the next parameter is
44 // the 'this' pointer. This parameter is pop()ed from the
45 // InterpStack when calling the function.
46 bool HasThisPointer = false;
47 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) {
48 if (MD->isImplicitObjectMemberFunction()) {
49 HasThisPointer = true;
50 ParamTypes.push_back(PT_Ptr);
51 ParamOffsets.push_back(ParamOffset);
53 }
54
55 // Set up lambda capture to closure record field mapping.
56 if (isLambdaCallOperator(MD)) {
57 const Record *R = P.getOrCreateRecord(MD->getParent());
58 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
59 FieldDecl *LTC;
60
61 MD->getParent()->getCaptureFields(LC, LTC);
62
63 for (auto Cap : LC) {
64 unsigned Offset = R->getField(Cap.second)->Offset;
65 this->LambdaCaptures[Cap.first] = {
66 Offset, Cap.second->getType()->isReferenceType()};
67 }
68 if (LTC)
69 this->LambdaThisCapture = R->getField(LTC)->Offset;
70 }
71 }
72
73 // Assign descriptors to all parameters.
74 // Composite objects are lowered to pointers.
75 for (const ParmVarDecl *PD : FuncDecl->parameters()) {
76 std::optional<PrimType> T = Ctx.classify(PD->getType());
77 PrimType PT = T.value_or(PT_Ptr);
78 Descriptor *Desc = P.createDescriptor(PD, PT);
79 ParamDescriptors.insert({ParamOffset, {PT, Desc}});
80 Params.insert({PD, {ParamOffset, T != std::nullopt}});
81 ParamOffsets.push_back(ParamOffset);
83 ParamTypes.push_back(PT);
84 }
85
86 // Create a handle over the emitted code.
87 Function *Func = P.getFunction(FuncDecl);
88 if (!Func) {
89 bool IsUnevaluatedBuiltin = false;
90 if (unsigned BI = FuncDecl->getBuiltinID())
91 IsUnevaluatedBuiltin = Ctx.getASTContext().BuiltinInfo.isUnevaluated(BI);
92
93 Func =
94 P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
95 std::move(ParamDescriptors), std::move(ParamOffsets),
96 HasThisPointer, HasRVO, IsUnevaluatedBuiltin);
97 }
98
99 assert(Func);
100 // For not-yet-defined functions, we only create a Function instance and
101 // compile their body later.
102 if (!FuncDecl->isDefined()) {
103 Func->setDefined(false);
104 return Func;
105 }
106
107 Func->setDefined(true);
108
109 // Lambda static invokers are a special case that we emit custom code for.
110 bool IsEligibleForCompilation = false;
111 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
112 IsEligibleForCompilation = MD->isLambdaStaticInvoker();
113 if (!IsEligibleForCompilation)
114 IsEligibleForCompilation = FuncDecl->isConstexpr();
115
116 // Compile the function body.
117 if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
118 // Return a dummy function if compilation failed.
119 if (BailLocation)
120 return llvm::make_error<ByteCodeGenError>(*BailLocation);
121
122 Func->setIsFullyCompiled(true);
123 return Func;
124 }
125
126 // Create scopes from descriptors.
128 for (auto &DS : Descriptors) {
129 Scopes.emplace_back(std::move(DS));
130 }
131
132 // Set the function's code.
133 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
134 std::move(Scopes), FuncDecl->hasBody());
135 Func->setIsFullyCompiled(true);
136 return Func;
137}
138
140 NextLocalOffset += sizeof(Block);
141 unsigned Location = NextLocalOffset;
142 NextLocalOffset += align(D->getAllocSize());
143 return {Location, D};
144}
145
147 const size_t Target = Code.size();
148 LabelOffsets.insert({Label, Target});
149
150 if (auto It = LabelRelocs.find(Label);
151 It != LabelRelocs.end()) {
152 for (unsigned Reloc : It->second) {
153 using namespace llvm::support;
154
155 // Rewrite the operand of all jumps to this label.
156 void *Location = Code.data() + Reloc - align(sizeof(int32_t));
157 assert(aligned(Location));
158 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
159 endian::write<int32_t, llvm::endianness::native>(Location, Offset);
160 }
161 LabelRelocs.erase(It);
162 }
163}
164
165int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
166 // Compute the PC offset which the jump is relative to.
167 const int64_t Position =
168 Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
169 assert(aligned(Position));
170
171 // If target is known, compute jump offset.
172 if (auto It = LabelOffsets.find(Label);
173 It != LabelOffsets.end())
174 return It->second - Position;
175
176 // Otherwise, record relocation and return dummy offset.
177 LabelRelocs[Label].push_back(Position);
178 return 0ull;
179}
180
182 if (!BailLocation)
183 BailLocation = Loc;
184 return false;
185}
186
187/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
188/// Pointers will be automatically marshalled as 32-bit IDs.
189template <typename T>
190static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
191 bool &Success) {
192 size_t Size;
193
194 if constexpr (std::is_pointer_v<T>)
195 Size = sizeof(uint32_t);
196 else
197 Size = sizeof(T);
198
199 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
200 Success = false;
201 return;
202 }
203
204 // Access must be aligned!
205 size_t ValPos = align(Code.size());
206 Size = align(Size);
207 assert(aligned(ValPos + Size));
208 Code.resize(ValPos + Size);
209
210 if constexpr (!std::is_pointer_v<T>) {
211 new (Code.data() + ValPos) T(Val);
212 } else {
213 uint32_t ID = P.getOrCreateNativePointer(Val);
214 new (Code.data() + ValPos) uint32_t(ID);
215 }
216}
217
218template <>
219void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
220 bool &Success) {
221 size_t Size = Val.bytesToSerialize();
222
223 if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
224 Success = false;
225 return;
226 }
227
228 // Access must be aligned!
229 size_t ValPos = align(Code.size());
230 Size = align(Size);
231 assert(aligned(ValPos + Size));
232 Code.resize(ValPos + Size);
233
234 Val.serialize(Code.data() + ValPos);
235}
236
237template <typename... Tys>
238bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) {
239 bool Success = true;
240
241 // The opcode is followed by arguments. The source info is
242 // attached to the address after the opcode.
243 emit(P, Code, Op, Success);
244 if (SI)
245 SrcMap.emplace_back(Code.size(), SI);
246
247 // The initializer list forces the expression to be evaluated
248 // for each argument in the variadic template, in order.
249 (void)std::initializer_list<int>{(emit(P, Code, Args, Success), 0)...};
250
251 return Success;
252}
253
255 return emitJt(getOffset(Label), SourceInfo{});
256}
257
259 return emitJf(getOffset(Label), SourceInfo{});
260}
261
263 return emitJmp(getOffset(Label), SourceInfo{});
264}
265
268 return true;
269}
270
271//===----------------------------------------------------------------------===//
272// Opcode emitters
273//===----------------------------------------------------------------------===//
274
275#define GET_LINK_IMPL
276#include "Opcodes.inc"
277#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 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.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
std::string Label
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:638
bool isUnevaluated(unsigned ID) const
Returns true if this builtin does not perform the side-effects of its arguments.
Definition: Builtins.h:143
Represents a member of a struct/union/class.
Definition: Decl.h:3015
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3228
Represents a function declaration or definition.
Definition: Decl.h:1957
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3569
QualType getReturnType() const
Definition: Decl.h:2712
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2641
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2405
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3107
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:3154
Represents a parameter to a function.
Definition: Decl.h:1747
A (possibly-)qualified type.
Definition: Type.h:736
Encodes a location in the source.
bool isVoidType() const
Definition: Type.h:7352
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.
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
unsigned LambdaThisCapture
Offset of the This parameter in a lambda record.
llvm::DenseMap< const ValueDecl *, ParamOffset > LambdaCaptures
Lambda captures.
bool fallthrough(const LabelTy &Label)
Local createLocal(Descriptor *D)
Callback for local registration.
virtual bool visitFunc(const FunctionDecl *E)=0
Methods implemented by the compiler.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
bool bail(const Stmt *S)
Bails out if a given node cannot be compiled.
llvm::Expected< Function * > compileFunc(const FunctionDecl *FuncDecl)
Compiles the function into the module.
bool jumpFalse(const LabelTy &Label)
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:58
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:91
void serialize(std::byte *Buff) const
Definition: Floating.h:144
size_t bytesToSerialize() const
Definition: Floating.h:139
Bytecode function.
Definition: Function.h:76
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Function * getFunction(const FunctionDecl *F)
Returns a function.
Definition: Program.cpp:214
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:117
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
Definition: Program.h:96
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Definition: Program.cpp:221
Structure/Class descriptor.
Definition: Record.h:25
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition: Record.cpp:30
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:95
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:91
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
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:27
Describes a memory block created by an allocation site.
Definition: Descriptor.h:79
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition: Descriptor.h:172
Information about a local's storage.
Definition: Function.h:37