clang 22.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 Function *Func) {
25 assert(FuncDecl);
26 assert(Func);
27
28 // Manually created functions that haven't been assigned proper
29 // parameters yet.
30 if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
31 return;
32
33 if (!FuncDecl->isDefined())
34 return;
35
36 // Set up lambda captures.
37 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
38 MD && isLambdaCallOperator(MD)) {
39 // Set up lambda capture to closure record field mapping.
40 const Record *R = P.getOrCreateRecord(MD->getParent());
41 assert(R);
42 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
43 FieldDecl *LTC;
44
45 MD->getParent()->getCaptureFields(LC, LTC);
46
47 for (auto Cap : LC) {
48 unsigned Offset = R->getField(Cap.second)->Offset;
49 this->LambdaCaptures[Cap.first] = {
50 Offset, Cap.second->getType()->isReferenceType()};
51 }
52 if (LTC) {
53 QualType CaptureType = R->getField(LTC)->Decl->getType();
54 this->LambdaThisCapture = {R->getField(LTC)->Offset,
55 CaptureType->isPointerOrReferenceType()};
56 }
57 }
58
59 // Register parameters with their offset.
60 unsigned ParamIndex = 0;
61 unsigned Drop = Func->hasRVO() +
62 (Func->hasThisPointer() && !Func->isThisPointerExplicit());
63 for (auto ParamOffset : llvm::drop_begin(Func->ParamOffsets, Drop)) {
64 const ParmVarDecl *PD = FuncDecl->parameters()[ParamIndex];
65 OptPrimType T = Ctx.classify(PD->getType());
66 this->Params.insert({PD, {ParamOffset, T != std::nullopt}});
67 ++ParamIndex;
68 }
69
70 Func->setDefined(true);
71
72 // Lambda static invokers are a special case that we emit custom code for.
73 bool IsEligibleForCompilation = Func->isLambdaStaticInvoker() ||
74 FuncDecl->isConstexpr() ||
75 FuncDecl->hasAttr<MSConstexprAttr>();
76
77 // Compile the function body.
78 if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
79 Func->setIsFullyCompiled(true);
80 return;
81 }
82
83 // Create scopes from descriptors.
85 for (auto &DS : Descriptors) {
86 Scopes.emplace_back(std::move(DS));
87 }
88
89 // Set the function's code.
90 Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
91 std::move(Scopes), FuncDecl->hasBody());
92 Func->setIsFullyCompiled(true);
93}
94
96 NextLocalOffset += sizeof(Block);
97 unsigned Location = NextLocalOffset;
98 NextLocalOffset += align(D->getAllocSize());
99 return {Location, D};
100}
101
103 const size_t Target = Code.size();
104 LabelOffsets.insert({Label, Target});
105
106 if (auto It = LabelRelocs.find(Label); It != LabelRelocs.end()) {
107 for (unsigned Reloc : It->second) {
108 using namespace llvm::support;
109
110 // Rewrite the operand of all jumps to this label.
111 void *Location = Code.data() + Reloc - align(sizeof(int32_t));
112 assert(aligned(Location));
113 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
114 endian::write<int32_t, llvm::endianness::native>(Location, Offset);
115 }
116 LabelRelocs.erase(It);
117 }
118}
119
120int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
121 // Compute the PC offset which the jump is relative to.
122 const int64_t Position =
123 Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
124 assert(aligned(Position));
125
126 // If target is known, compute jump offset.
127 if (auto It = LabelOffsets.find(Label); It != LabelOffsets.end())
128 return It->second - Position;
129
130 // Otherwise, record relocation and return dummy offset.
131 LabelRelocs[Label].push_back(Position);
132 return 0ull;
133}
134
135/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
136/// Pointers will be automatically marshalled as 32-bit IDs.
137template <typename T>
139 const T &Val, bool &Success) {
140 size_t ValPos = Code.size();
141 size_t Size;
142
143 if constexpr (std::is_pointer_v<T>)
144 Size = align(sizeof(uint32_t));
145 else
146 Size = align(sizeof(T));
147
148 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
149 Success = false;
150 return;
151 }
152
153 // Access must be aligned!
154 assert(aligned(ValPos));
155 assert(aligned(ValPos + Size));
156 Code.resize_for_overwrite(ValPos + Size);
157
158 if constexpr (!std::is_pointer_v<T>) {
159 new (Code.data() + ValPos) T(Val);
160 } else {
161 uint32_t ID = P.getOrCreateNativePointer(Val);
162 new (Code.data() + ValPos) uint32_t(ID);
163 }
164}
165
166/// Emits a serializable value. These usually (potentially) contain
167/// heap-allocated memory and aren't trivially copyable.
168template <typename T>
170 bool &Success) {
171 size_t ValPos = Code.size();
172 size_t Size = align(Val.bytesToSerialize());
173
174 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
175 Success = false;
176 return;
177 }
178
179 // Access must be aligned!
180 assert(aligned(ValPos));
181 assert(aligned(ValPos + Size));
182 Code.resize_for_overwrite(ValPos + Size);
183
184 Val.serialize(Code.data() + ValPos);
185}
186
187template <>
189 const Floating &Val, bool &Success) {
190 emitSerialized(Code, Val, Success);
191}
192
193template <>
195 const IntegralAP<false> &Val, bool &Success) {
196 emitSerialized(Code, Val, Success);
197}
198
199template <>
201 const IntegralAP<true> &Val, bool &Success) {
202 emitSerialized(Code, Val, Success);
203}
204
205template <>
207 const FixedPoint &Val, bool &Success) {
208 emitSerialized(Code, Val, Success);
209}
210
211template <typename... Tys>
212bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args,
213 const SourceInfo &SI) {
214 bool Success = true;
215
216 // The opcode is followed by arguments. The source info is
217 // attached to the address after the opcode.
218 emit(P, Code, Op, Success);
219 if (LocOverride)
220 SrcMap.emplace_back(Code.size(), *LocOverride);
221 else if (SI)
222 SrcMap.emplace_back(Code.size(), SI);
223
224 (..., emit(P, Code, Args, Success));
225 return Success;
226}
227
229 return emitJt(getOffset(Label), SourceInfo{});
230}
231
233 return emitJf(getOffset(Label), SourceInfo{});
234}
235
237 return emitJmp(getOffset(Label), SourceInfo{});
238}
239
242 return true;
243}
244
245bool ByteCodeEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
246 const Expr *Arg = E->getArg(0);
247 PrimType T = Ctx.classify(Arg->getType()).value_or(PT_Ptr);
248 if (!this->emitBCP(getOffset(EndLabel), T, E))
249 return false;
250 if (!this->visit(Arg))
251 return false;
252 return true;
253}
254
255//===----------------------------------------------------------------------===//
256// Opcode emitters
257//===----------------------------------------------------------------------===//
258
259#define GET_LINK_IMPL
260#include "Opcodes.inc"
261#undef GET_LINK_IMPL
This file provides some common utility functions for processing Lambda related AST Constructs.
StringRef P
static void emitSerialized(llvm::SmallVectorImpl< std::byte > &Code, const T &Val, bool &Success)
Emits a serializable value.
static void emit(Program &P, llvm::SmallVectorImpl< std::byte > &Code, const T &Val, bool &Success)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
const Decl * D
Expr * E
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Target Target
Definition: MachO.h:51
std::string Label
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2879
bool hasAttr() const
Definition: DeclBase.h:577
This represents one expression.
Definition: Expr.h:112
QualType getType() const
Definition: Expr.h:144
Represents a member of a struct/union/class.
Definition: Decl.h:3157
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3393
Represents a function declaration or definition.
Definition: Decl.h:1999
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2771
param_iterator param_begin()
Definition: Decl.h:2783
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2469
bool param_empty() const
Definition: Decl.h:2782
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3191
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:3238
Represents a parameter to a function.
Definition: Decl.h:1789
A (possibly-)qualified type.
Definition: TypeBase.h:937
bool isPointerOrReferenceType() const
Definition: TypeBase.h:8584
QualType getType() const
Definition: Decl.h:722
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:44
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 speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
void compileFunc(const FunctionDecl *FuncDecl, Function *Func=nullptr)
Compiles the function into the module.
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.
std::optional< SourceInfo > LocOverride
bool jumpFalse(const LabelTy &Label)
virtual bool visit(const Expr *E)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
OptPrimType classify(QualType T) const
Classifies a type.
Definition: Context.cpp:310
Wrapper around fixed point types.
Definition: FixedPoint.h:23
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition: Floating.h:35
Bytecode function.
Definition: Function.h:86
If an IntegralAP is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition: IntegralAP.h:36
PrimType value_or(PrimType PT) const
Definition: PrimType.h:68
The program contains and links the bytecode for all functions.
Definition: Program.h:36
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Definition: Program.cpp:295
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:73
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:189
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:185
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:34
The JSON file list parser is used to communicate input to InstallAPI.
@ Success
Annotation was successful.
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:28
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition: Descriptor.h:122
const FieldDecl * Decl
Definition: Record.h:29
Information about a local's storage.
Definition: Function.h:39