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
23void ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl,
24 Function *Func) {
25 assert(FuncDecl);
26 assert(Func);
27 assert(FuncDecl->isThisDeclarationADefinition());
28
29 // Set up lambda captures.
30 if (Func->isLambdaCallOperator()) {
31 // Set up lambda capture to closure record field mapping.
32 const CXXRecordDecl *ParentDecl = Func->getParentDecl();
33 const Record *R = P.getOrCreateRecord(RD: ParentDecl);
34 assert(R);
35 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
36 FieldDecl *LTC;
37
38 ParentDecl->getCaptureFields(Captures&: LC, ThisCapture&: LTC);
39
40 for (const auto &Cap : LC) {
41 unsigned Offset = R->getField(FD: Cap.second)->Offset;
42 this->LambdaCaptures[Cap.first] = {
43 .Offset: Offset, .IsPtr: Cap.second->getType()->isReferenceType()};
44 }
45 if (LTC) {
46 QualType CaptureType = R->getField(FD: LTC)->Decl->getType();
47 this->LambdaThisCapture = {.Offset: R->getField(FD: LTC)->Offset,
48 .IsPtr: CaptureType->isPointerOrReferenceType()};
49 }
50 }
51
52 bool IsValid = !FuncDecl->isInvalidDecl();
53 // Register parameters and their index.
54 for (unsigned ParamIndex = 0, N = Func->getNumWrittenParams();
55 ParamIndex != N; ++ParamIndex) {
56 const ParmVarDecl *PD = FuncDecl->getParamDecl(i: ParamIndex);
57 if (PD->isInvalidDecl())
58 IsValid = false;
59 this->Params.insert(KV: {PD, {.Index: ParamIndex, .IsPtr: Ctx.canClassify(T: PD->getType())}});
60 }
61
62 Func->setDefined(true);
63
64 // Lambda static invokers are a special case that we emit custom code for.
65 bool IsEligibleForCompilation = Func->isLambdaStaticInvoker() ||
66 FuncDecl->isConstexpr() ||
67 FuncDecl->hasAttr<MSConstexprAttr>();
68
69 // Compile the function body.
70 if (!IsEligibleForCompilation || !visitFunc(E: FuncDecl)) {
71 Func->setIsFullyCompiled(true);
72 return;
73 }
74
75 // Create scopes from descriptors.
76 llvm::SmallVector<Scope, 2> Scopes;
77 for (auto &DS : Descriptors) {
78 Scopes.emplace_back(Args: std::move(DS));
79 }
80
81 // Set the function's code.
82 Func->setCode(Source: FuncDecl, NewFrameSize: NextLocalOffset, NewCode: std::move(Code), NewSrcMap: std::move(SrcMap),
83 NewScopes: std::move(Scopes), NewHasBody: FuncDecl->hasBody(), NewIsValid: IsValid);
84 Func->setIsFullyCompiled(true);
85}
86
87Scope::Local ByteCodeEmitter::createLocal(const Descriptor *D) {
88 NextLocalOffset += sizeof(Block);
89 unsigned Location = NextLocalOffset;
90 NextLocalOffset += align(Size: Block::InlineDescMD + D->getAllocSize());
91 return {.Desc: D, .Offset: Location};
92}
93
94void ByteCodeEmitter::emitLabel(LabelTy Label) {
95 const size_t Target = Code.size();
96 LabelOffsets.insert(KV: {Label, Target});
97
98 if (auto It = LabelRelocs.find(Val: Label); It != LabelRelocs.end()) {
99 for (unsigned Reloc : It->second) {
100 using namespace llvm::support;
101
102 // Rewrite the operand of all jumps to this label.
103 void *Location = Code.data() + Reloc - align(Size: sizeof(int32_t));
104 assert(aligned(Location));
105 const int32_t Offset = Target - static_cast<int64_t>(Reloc);
106 endian::write<int32_t, llvm::endianness::native>(P: Location, V: Offset);
107 }
108 LabelRelocs.erase(I: It);
109 }
110}
111
112int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
113 // Compute the PC offset which the jump is relative to.
114 const int64_t Position =
115 Code.size() + align(Size: sizeof(Opcode)) + align(Size: sizeof(int32_t));
116 assert(aligned(Position));
117
118 // If target is known, compute jump offset.
119 if (auto It = LabelOffsets.find(Val: Label); It != LabelOffsets.end())
120 return It->second - Position;
121
122 // Otherwise, record relocation and return dummy offset.
123 LabelRelocs[Label].push_back(Elt: Position);
124 return 0ull;
125}
126
127/// Helper to write bytecode and bail out if 32-bit offsets become invalid.
128template <typename T>
129static void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
130 const T &Val, bool &Success) {
131 size_t ValPos = Code.size();
132 size_t Size;
133
134 if constexpr (std::is_pointer_v<T>)
135 Size = align(Size: sizeof(uintptr_t));
136 else
137 Size = align(Size: sizeof(T));
138
139 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
140 Success = false;
141 return;
142 }
143
144 // Access must be aligned!
145 assert(aligned(ValPos));
146 assert(aligned(ValPos + Size));
147 Code.resize_for_overwrite(N: ValPos + Size);
148
149 if constexpr (std::is_pointer_v<T>)
150 new (Code.data() + ValPos) uintptr_t(reinterpret_cast<uintptr_t>(Val));
151 else
152 new (Code.data() + ValPos) T(Val);
153}
154
155/// Emits a serializable value. These usually (potentially) contain
156/// heap-allocated memory and aren't trivially copyable.
157template <typename T>
158static void emitSerialized(llvm::SmallVectorImpl<std::byte> &Code, const T &Val,
159 bool &Success) {
160 size_t ValPos = Code.size();
161 size_t Size = align(Val.bytesToSerialize());
162
163 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
164 Success = false;
165 return;
166 }
167
168 // Access must be aligned!
169 assert(aligned(ValPos));
170 assert(aligned(ValPos + Size));
171 Code.resize_for_overwrite(N: ValPos + Size);
172
173 Val.serialize(Code.data() + ValPos);
174}
175
176template <>
177void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
178 const Floating &Val, bool &Success) {
179 emitSerialized(Code, Val, Success);
180}
181
182template <>
183void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
184 const IntegralAP<false> &Val, bool &Success) {
185 emitSerialized(Code, Val, Success);
186}
187
188template <>
189void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
190 const IntegralAP<true> &Val, bool &Success) {
191 emitSerialized(Code, Val, Success);
192}
193
194template <>
195void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
196 const FixedPoint &Val, bool &Success) {
197 emitSerialized(Code, Val, Success);
198}
199
200template <typename... Tys>
201bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args, SourceInfo SI) {
202 bool Success = true;
203
204 // The opcode is followed by arguments. The source info is
205 // attached to the address after the opcode.
206 emit(P, Code, Val: Op, Success);
207
208 SI = LocOverride.value_or(u&: SI);
209 if (SrcMap.empty() || SrcMap.back() != SI)
210 SrcMap.push(Offset: Code.size(), Info: SI);
211
212 (..., emit(P, Code, Args, Success));
213 return Success;
214}
215
216bool ByteCodeEmitter::jumpTrue(const LabelTy &Label, SourceInfo SI) {
217 return emitJt(getOffset(Label), SI);
218}
219
220bool ByteCodeEmitter::jumpFalse(const LabelTy &Label, SourceInfo SI) {
221 return emitJf(getOffset(Label), SI);
222}
223
224bool ByteCodeEmitter::jump(const LabelTy &Label, SourceInfo SI) {
225 return emitJmp(getOffset(Label), SI);
226}
227
228bool ByteCodeEmitter::fallthrough(const LabelTy &Label) {
229 emitLabel(Label);
230 return true;
231}
232
233bool ByteCodeEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
234 const Expr *Arg = E->getArg(Arg: 0);
235 PrimType T = Ctx.classify(T: Arg->getType()).value_or(PT: PT_Ptr);
236 if (!this->emitBCP(getOffset(Label: EndLabel), T, E))
237 return false;
238 if (!this->visit(E: Arg))
239 return false;
240 return true;
241}
242
243//===----------------------------------------------------------------------===//
244// Opcode emitters
245//===----------------------------------------------------------------------===//
246
247#define GET_LINK_IMPL
248#include "Opcodes.inc"
249#undef GET_LINK_IMPL
250