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