1*700637cbSDimitry Andric //===--- ByteCodeEmitter.cpp - Instruction emitter for the VM ---*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric
9*700637cbSDimitry Andric #include "ByteCodeEmitter.h"
10*700637cbSDimitry Andric #include "Context.h"
11*700637cbSDimitry Andric #include "Floating.h"
12*700637cbSDimitry Andric #include "IntegralAP.h"
13*700637cbSDimitry Andric #include "Opcode.h"
14*700637cbSDimitry Andric #include "Program.h"
15*700637cbSDimitry Andric #include "clang/AST/ASTLambda.h"
16*700637cbSDimitry Andric #include "clang/AST/Attr.h"
17*700637cbSDimitry Andric #include "clang/AST/DeclCXX.h"
18*700637cbSDimitry Andric #include <type_traits>
19*700637cbSDimitry Andric
20*700637cbSDimitry Andric using namespace clang;
21*700637cbSDimitry Andric using namespace clang::interp;
22*700637cbSDimitry Andric
compileFunc(const FunctionDecl * FuncDecl,Function * Func)23*700637cbSDimitry Andric void ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl,
24*700637cbSDimitry Andric Function *Func) {
25*700637cbSDimitry Andric assert(FuncDecl);
26*700637cbSDimitry Andric assert(Func);
27*700637cbSDimitry Andric
28*700637cbSDimitry Andric // Manually created functions that haven't been assigned proper
29*700637cbSDimitry Andric // parameters yet.
30*700637cbSDimitry Andric if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
31*700637cbSDimitry Andric return;
32*700637cbSDimitry Andric
33*700637cbSDimitry Andric if (!FuncDecl->isDefined())
34*700637cbSDimitry Andric return;
35*700637cbSDimitry Andric
36*700637cbSDimitry Andric // Set up lambda captures.
37*700637cbSDimitry Andric if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
38*700637cbSDimitry Andric MD && isLambdaCallOperator(MD)) {
39*700637cbSDimitry Andric // Set up lambda capture to closure record field mapping.
40*700637cbSDimitry Andric const Record *R = P.getOrCreateRecord(MD->getParent());
41*700637cbSDimitry Andric assert(R);
42*700637cbSDimitry Andric llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
43*700637cbSDimitry Andric FieldDecl *LTC;
44*700637cbSDimitry Andric
45*700637cbSDimitry Andric MD->getParent()->getCaptureFields(LC, LTC);
46*700637cbSDimitry Andric
47*700637cbSDimitry Andric for (auto Cap : LC) {
48*700637cbSDimitry Andric unsigned Offset = R->getField(Cap.second)->Offset;
49*700637cbSDimitry Andric this->LambdaCaptures[Cap.first] = {
50*700637cbSDimitry Andric Offset, Cap.second->getType()->isReferenceType()};
51*700637cbSDimitry Andric }
52*700637cbSDimitry Andric if (LTC) {
53*700637cbSDimitry Andric QualType CaptureType = R->getField(LTC)->Decl->getType();
54*700637cbSDimitry Andric this->LambdaThisCapture = {R->getField(LTC)->Offset,
55*700637cbSDimitry Andric CaptureType->isPointerOrReferenceType()};
56*700637cbSDimitry Andric }
57*700637cbSDimitry Andric }
58*700637cbSDimitry Andric
59*700637cbSDimitry Andric // Register parameters with their offset.
60*700637cbSDimitry Andric unsigned ParamIndex = 0;
61*700637cbSDimitry Andric unsigned Drop = Func->hasRVO() +
62*700637cbSDimitry Andric (Func->hasThisPointer() && !Func->isThisPointerExplicit());
63*700637cbSDimitry Andric for (auto ParamOffset : llvm::drop_begin(Func->ParamOffsets, Drop)) {
64*700637cbSDimitry Andric const ParmVarDecl *PD = FuncDecl->parameters()[ParamIndex];
65*700637cbSDimitry Andric std::optional<PrimType> T = Ctx.classify(PD->getType());
66*700637cbSDimitry Andric this->Params.insert({PD, {ParamOffset, T != std::nullopt}});
67*700637cbSDimitry Andric ++ParamIndex;
68*700637cbSDimitry Andric }
69*700637cbSDimitry Andric
70*700637cbSDimitry Andric Func->setDefined(true);
71*700637cbSDimitry Andric
72*700637cbSDimitry Andric // Lambda static invokers are a special case that we emit custom code for.
73*700637cbSDimitry Andric bool IsEligibleForCompilation = Func->isLambdaStaticInvoker() ||
74*700637cbSDimitry Andric FuncDecl->isConstexpr() ||
75*700637cbSDimitry Andric FuncDecl->hasAttr<MSConstexprAttr>();
76*700637cbSDimitry Andric
77*700637cbSDimitry Andric // Compile the function body.
78*700637cbSDimitry Andric if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
79*700637cbSDimitry Andric Func->setIsFullyCompiled(true);
80*700637cbSDimitry Andric return;
81*700637cbSDimitry Andric }
82*700637cbSDimitry Andric
83*700637cbSDimitry Andric // Create scopes from descriptors.
84*700637cbSDimitry Andric llvm::SmallVector<Scope, 2> Scopes;
85*700637cbSDimitry Andric for (auto &DS : Descriptors) {
86*700637cbSDimitry Andric Scopes.emplace_back(std::move(DS));
87*700637cbSDimitry Andric }
88*700637cbSDimitry Andric
89*700637cbSDimitry Andric // Set the function's code.
90*700637cbSDimitry Andric Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
91*700637cbSDimitry Andric std::move(Scopes), FuncDecl->hasBody());
92*700637cbSDimitry Andric Func->setIsFullyCompiled(true);
93*700637cbSDimitry Andric }
94*700637cbSDimitry Andric
createLocal(Descriptor * D)95*700637cbSDimitry Andric Scope::Local ByteCodeEmitter::createLocal(Descriptor *D) {
96*700637cbSDimitry Andric NextLocalOffset += sizeof(Block);
97*700637cbSDimitry Andric unsigned Location = NextLocalOffset;
98*700637cbSDimitry Andric NextLocalOffset += align(D->getAllocSize());
99*700637cbSDimitry Andric return {Location, D};
100*700637cbSDimitry Andric }
101*700637cbSDimitry Andric
emitLabel(LabelTy Label)102*700637cbSDimitry Andric void ByteCodeEmitter::emitLabel(LabelTy Label) {
103*700637cbSDimitry Andric const size_t Target = Code.size();
104*700637cbSDimitry Andric LabelOffsets.insert({Label, Target});
105*700637cbSDimitry Andric
106*700637cbSDimitry Andric if (auto It = LabelRelocs.find(Label); It != LabelRelocs.end()) {
107*700637cbSDimitry Andric for (unsigned Reloc : It->second) {
108*700637cbSDimitry Andric using namespace llvm::support;
109*700637cbSDimitry Andric
110*700637cbSDimitry Andric // Rewrite the operand of all jumps to this label.
111*700637cbSDimitry Andric void *Location = Code.data() + Reloc - align(sizeof(int32_t));
112*700637cbSDimitry Andric assert(aligned(Location));
113*700637cbSDimitry Andric const int32_t Offset = Target - static_cast<int64_t>(Reloc);
114*700637cbSDimitry Andric endian::write<int32_t, llvm::endianness::native>(Location, Offset);
115*700637cbSDimitry Andric }
116*700637cbSDimitry Andric LabelRelocs.erase(It);
117*700637cbSDimitry Andric }
118*700637cbSDimitry Andric }
119*700637cbSDimitry Andric
getOffset(LabelTy Label)120*700637cbSDimitry Andric int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
121*700637cbSDimitry Andric // Compute the PC offset which the jump is relative to.
122*700637cbSDimitry Andric const int64_t Position =
123*700637cbSDimitry Andric Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
124*700637cbSDimitry Andric assert(aligned(Position));
125*700637cbSDimitry Andric
126*700637cbSDimitry Andric // If target is known, compute jump offset.
127*700637cbSDimitry Andric if (auto It = LabelOffsets.find(Label); It != LabelOffsets.end())
128*700637cbSDimitry Andric return It->second - Position;
129*700637cbSDimitry Andric
130*700637cbSDimitry Andric // Otherwise, record relocation and return dummy offset.
131*700637cbSDimitry Andric LabelRelocs[Label].push_back(Position);
132*700637cbSDimitry Andric return 0ull;
133*700637cbSDimitry Andric }
134*700637cbSDimitry Andric
135*700637cbSDimitry Andric /// Helper to write bytecode and bail out if 32-bit offsets become invalid.
136*700637cbSDimitry Andric /// Pointers will be automatically marshalled as 32-bit IDs.
137*700637cbSDimitry Andric template <typename T>
emit(Program & P,std::vector<std::byte> & Code,const T & Val,bool & Success)138*700637cbSDimitry Andric static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
139*700637cbSDimitry Andric bool &Success) {
140*700637cbSDimitry Andric size_t Size;
141*700637cbSDimitry Andric
142*700637cbSDimitry Andric if constexpr (std::is_pointer_v<T>)
143*700637cbSDimitry Andric Size = sizeof(uint32_t);
144*700637cbSDimitry Andric else
145*700637cbSDimitry Andric Size = sizeof(T);
146*700637cbSDimitry Andric
147*700637cbSDimitry Andric if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
148*700637cbSDimitry Andric Success = false;
149*700637cbSDimitry Andric return;
150*700637cbSDimitry Andric }
151*700637cbSDimitry Andric
152*700637cbSDimitry Andric // Access must be aligned!
153*700637cbSDimitry Andric size_t ValPos = align(Code.size());
154*700637cbSDimitry Andric Size = align(Size);
155*700637cbSDimitry Andric assert(aligned(ValPos + Size));
156*700637cbSDimitry Andric Code.resize(ValPos + Size);
157*700637cbSDimitry Andric
158*700637cbSDimitry Andric if constexpr (!std::is_pointer_v<T>) {
159*700637cbSDimitry Andric new (Code.data() + ValPos) T(Val);
160*700637cbSDimitry Andric } else {
161*700637cbSDimitry Andric uint32_t ID = P.getOrCreateNativePointer(Val);
162*700637cbSDimitry Andric new (Code.data() + ValPos) uint32_t(ID);
163*700637cbSDimitry Andric }
164*700637cbSDimitry Andric }
165*700637cbSDimitry Andric
166*700637cbSDimitry Andric /// Emits a serializable value. These usually (potentially) contain
167*700637cbSDimitry Andric /// heap-allocated memory and aren't trivially copyable.
168*700637cbSDimitry Andric template <typename T>
emitSerialized(std::vector<std::byte> & Code,const T & Val,bool & Success)169*700637cbSDimitry Andric static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
170*700637cbSDimitry Andric bool &Success) {
171*700637cbSDimitry Andric size_t Size = Val.bytesToSerialize();
172*700637cbSDimitry Andric
173*700637cbSDimitry Andric if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
174*700637cbSDimitry Andric Success = false;
175*700637cbSDimitry Andric return;
176*700637cbSDimitry Andric }
177*700637cbSDimitry Andric
178*700637cbSDimitry Andric // Access must be aligned!
179*700637cbSDimitry Andric assert(aligned(Code.size()));
180*700637cbSDimitry Andric size_t ValPos = Code.size();
181*700637cbSDimitry Andric Size = align(Size);
182*700637cbSDimitry Andric assert(aligned(ValPos + Size));
183*700637cbSDimitry Andric Code.resize(ValPos + Size);
184*700637cbSDimitry Andric
185*700637cbSDimitry Andric Val.serialize(Code.data() + ValPos);
186*700637cbSDimitry Andric }
187*700637cbSDimitry Andric
188*700637cbSDimitry Andric template <>
emit(Program & P,std::vector<std::byte> & Code,const Floating & Val,bool & Success)189*700637cbSDimitry Andric void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
190*700637cbSDimitry Andric bool &Success) {
191*700637cbSDimitry Andric emitSerialized(Code, Val, Success);
192*700637cbSDimitry Andric }
193*700637cbSDimitry Andric
194*700637cbSDimitry Andric template <>
emit(Program & P,std::vector<std::byte> & Code,const IntegralAP<false> & Val,bool & Success)195*700637cbSDimitry Andric void emit(Program &P, std::vector<std::byte> &Code,
196*700637cbSDimitry Andric const IntegralAP<false> &Val, bool &Success) {
197*700637cbSDimitry Andric emitSerialized(Code, Val, Success);
198*700637cbSDimitry Andric }
199*700637cbSDimitry Andric
200*700637cbSDimitry Andric template <>
emit(Program & P,std::vector<std::byte> & Code,const IntegralAP<true> & Val,bool & Success)201*700637cbSDimitry Andric void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
202*700637cbSDimitry Andric bool &Success) {
203*700637cbSDimitry Andric emitSerialized(Code, Val, Success);
204*700637cbSDimitry Andric }
205*700637cbSDimitry Andric
206*700637cbSDimitry Andric template <>
emit(Program & P,std::vector<std::byte> & Code,const FixedPoint & Val,bool & Success)207*700637cbSDimitry Andric void emit(Program &P, std::vector<std::byte> &Code, const FixedPoint &Val,
208*700637cbSDimitry Andric bool &Success) {
209*700637cbSDimitry Andric emitSerialized(Code, Val, Success);
210*700637cbSDimitry Andric }
211*700637cbSDimitry Andric
212*700637cbSDimitry Andric template <typename... Tys>
emitOp(Opcode Op,const Tys &...Args,const SourceInfo & SI)213*700637cbSDimitry Andric bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args,
214*700637cbSDimitry Andric const SourceInfo &SI) {
215*700637cbSDimitry Andric bool Success = true;
216*700637cbSDimitry Andric
217*700637cbSDimitry Andric // The opcode is followed by arguments. The source info is
218*700637cbSDimitry Andric // attached to the address after the opcode.
219*700637cbSDimitry Andric emit(P, Code, Op, Success);
220*700637cbSDimitry Andric if (SI)
221*700637cbSDimitry Andric SrcMap.emplace_back(Code.size(), SI);
222*700637cbSDimitry Andric
223*700637cbSDimitry Andric (..., emit(P, Code, Args, Success));
224*700637cbSDimitry Andric return Success;
225*700637cbSDimitry Andric }
226*700637cbSDimitry Andric
jumpTrue(const LabelTy & Label)227*700637cbSDimitry Andric bool ByteCodeEmitter::jumpTrue(const LabelTy &Label) {
228*700637cbSDimitry Andric return emitJt(getOffset(Label), SourceInfo{});
229*700637cbSDimitry Andric }
230*700637cbSDimitry Andric
jumpFalse(const LabelTy & Label)231*700637cbSDimitry Andric bool ByteCodeEmitter::jumpFalse(const LabelTy &Label) {
232*700637cbSDimitry Andric return emitJf(getOffset(Label), SourceInfo{});
233*700637cbSDimitry Andric }
234*700637cbSDimitry Andric
jump(const LabelTy & Label)235*700637cbSDimitry Andric bool ByteCodeEmitter::jump(const LabelTy &Label) {
236*700637cbSDimitry Andric return emitJmp(getOffset(Label), SourceInfo{});
237*700637cbSDimitry Andric }
238*700637cbSDimitry Andric
fallthrough(const LabelTy & Label)239*700637cbSDimitry Andric bool ByteCodeEmitter::fallthrough(const LabelTy &Label) {
240*700637cbSDimitry Andric emitLabel(Label);
241*700637cbSDimitry Andric return true;
242*700637cbSDimitry Andric }
243*700637cbSDimitry Andric
speculate(const CallExpr * E,const LabelTy & EndLabel)244*700637cbSDimitry Andric bool ByteCodeEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
245*700637cbSDimitry Andric const Expr *Arg = E->getArg(0);
246*700637cbSDimitry Andric PrimType T = Ctx.classify(Arg->getType()).value_or(PT_Ptr);
247*700637cbSDimitry Andric if (!this->emitBCP(getOffset(EndLabel), T, E))
248*700637cbSDimitry Andric return false;
249*700637cbSDimitry Andric if (!this->visit(Arg))
250*700637cbSDimitry Andric return false;
251*700637cbSDimitry Andric return true;
252*700637cbSDimitry Andric }
253*700637cbSDimitry Andric
254*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
255*700637cbSDimitry Andric // Opcode emitters
256*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
257*700637cbSDimitry Andric
258*700637cbSDimitry Andric #define GET_LINK_IMPL
259*700637cbSDimitry Andric #include "Opcodes.inc"
260*700637cbSDimitry Andric #undef GET_LINK_IMPL
261