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 // Manually created functions that haven't been assigned proper
30 // parameters yet.
31 if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
32 return;
33
34 // Set up lambda captures.
35 if (Func->isLambdaCallOperator()) {
36 // Set up lambda capture to closure record field mapping.
37 const CXXRecordDecl *ParentDecl = Func->getParentDecl();
38 const Record *R = P.getOrCreateRecord(RD: ParentDecl);
39 assert(R);
40 llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
41 FieldDecl *LTC;
42
43 ParentDecl->getCaptureFields(Captures&: LC, ThisCapture&: LTC);
44
45 for (auto Cap : LC) {
46 unsigned Offset = R->getField(FD: Cap.second)->Offset;
47 this->LambdaCaptures[Cap.first] = {
48 .Offset: Offset, .IsPtr: Cap.second->getType()->isReferenceType()};
49 }
50 if (LTC) {
51 QualType CaptureType = R->getField(FD: LTC)->Decl->getType();
52 this->LambdaThisCapture = {.Offset: R->getField(FD: LTC)->Offset,
53 .IsPtr: CaptureType->isPointerOrReferenceType()};
54 }
55 }
56
57 bool IsValid = !FuncDecl->isInvalidDecl();
58 // Register parameters and their index.
59 for (unsigned ParamIndex = 0, N = Func->getNumWrittenParams();
60 ParamIndex != N; ++ParamIndex) {
61 const ParmVarDecl *PD = FuncDecl->getParamDecl(i: ParamIndex);
62 if (PD->isInvalidDecl())
63 IsValid = false;
64 this->Params.insert(KV: {PD, {.Index: ParamIndex, .IsPtr: Ctx.canClassify(T: PD->getType())}});
65 }
66
67 Func->setDefined(true);
68
69 // Lambda static invokers are a special case that we emit custom code for.
70 bool IsEligibleForCompilation = Func->isLambdaStaticInvoker() ||
71 FuncDecl->isConstexpr() ||
72 FuncDecl->hasAttr<MSConstexprAttr>();
73
74 // Compile the function body.
75 if (!IsEligibleForCompilation || !visitFunc(E: FuncDecl)) {
76 Func->setIsFullyCompiled(true);
77 return;
78 }
79
80 // Create scopes from descriptors.
81 llvm::SmallVector<Scope, 2> Scopes;
82 for (auto &DS : Descriptors) {
83 Scopes.emplace_back(Args: std::move(DS));
84 }
85
86 // Set the function's code.
87 Func->setCode(Source: FuncDecl, NewFrameSize: NextLocalOffset, NewCode: std::move(Code), NewSrcMap: std::move(SrcMap),
88 NewScopes: std::move(Scopes), NewHasBody: FuncDecl->hasBody(), NewIsValid: IsValid);
89 Func->setIsFullyCompiled(true);
90}
91
92Scope::Local ByteCodeEmitter::createLocal(Descriptor *D) {
93 NextLocalOffset += sizeof(Block);
94 unsigned Location = NextLocalOffset;
95 NextLocalOffset += align(Size: D->getAllocSize());
96 return {.Offset: Location, .Desc: D};
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.
133/// Pointers will be automatically marshalled as 32-bit IDs.
134template <typename T>
135static void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
136 const T &Val, bool &Success) {
137 size_t ValPos = Code.size();
138 size_t Size;
139
140 if constexpr (std::is_pointer_v<T>)
141 Size = align(Size: sizeof(uint32_t));
142 else
143 Size = align(Size: sizeof(T));
144
145 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
146 Success = false;
147 return;
148 }
149
150 // Access must be aligned!
151 assert(aligned(ValPos));
152 assert(aligned(ValPos + Size));
153 Code.resize_for_overwrite(N: ValPos + Size);
154
155 if constexpr (!std::is_pointer_v<T>) {
156 new (Code.data() + ValPos) T(Val);
157 } else {
158 uint32_t ID = P.getOrCreateNativePointer(Ptr: Val);
159 new (Code.data() + ValPos) uint32_t(ID);
160 }
161}
162
163/// Emits a serializable value. These usually (potentially) contain
164/// heap-allocated memory and aren't trivially copyable.
165template <typename T>
166static void emitSerialized(llvm::SmallVectorImpl<std::byte> &Code, const T &Val,
167 bool &Success) {
168 size_t ValPos = Code.size();
169 size_t Size = align(Val.bytesToSerialize());
170
171 if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
172 Success = false;
173 return;
174 }
175
176 // Access must be aligned!
177 assert(aligned(ValPos));
178 assert(aligned(ValPos + Size));
179 Code.resize_for_overwrite(N: ValPos + Size);
180
181 Val.serialize(Code.data() + ValPos);
182}
183
184template <>
185void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
186 const Floating &Val, bool &Success) {
187 emitSerialized(Code, Val, Success);
188}
189
190template <>
191void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
192 const IntegralAP<false> &Val, bool &Success) {
193 emitSerialized(Code, Val, Success);
194}
195
196template <>
197void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
198 const IntegralAP<true> &Val, bool &Success) {
199 emitSerialized(Code, Val, Success);
200}
201
202template <>
203void emit(Program &P, llvm::SmallVectorImpl<std::byte> &Code,
204 const FixedPoint &Val, bool &Success) {
205 emitSerialized(Code, Val, Success);
206}
207
208template <typename... Tys>
209bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args, SourceInfo SI) {
210 bool Success = true;
211
212 // The opcode is followed by arguments. The source info is
213 // attached to the address after the opcode.
214 emit(P, Code, Val: Op, Success);
215 if (LocOverride)
216 SrcMap.emplace_back(args: Code.size(), args&: *LocOverride);
217 else if (SI)
218 SrcMap.emplace_back(args: Code.size(), args&: SI);
219
220 (..., emit(P, Code, Args, Success));
221 return Success;
222}
223
224bool ByteCodeEmitter::jumpTrue(const LabelTy &Label, SourceInfo SI) {
225 return emitJt(getOffset(Label), SI);
226}
227
228bool ByteCodeEmitter::jumpFalse(const LabelTy &Label, SourceInfo SI) {
229 return emitJf(getOffset(Label), SI);
230}
231
232bool ByteCodeEmitter::jump(const LabelTy &Label, SourceInfo SI) {
233 return emitJmp(getOffset(Label), SI);
234}
235
236bool ByteCodeEmitter::fallthrough(const LabelTy &Label) {
237 emitLabel(Label);
238 return true;
239}
240
241bool ByteCodeEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
242 const Expr *Arg = E->getArg(Arg: 0);
243 PrimType T = Ctx.classify(T: Arg->getType()).value_or(PT: PT_Ptr);
244 if (!this->emitBCP(getOffset(Label: EndLabel), T, E))
245 return false;
246 if (!this->visit(E: Arg))
247 return false;
248 return true;
249}
250
251//===----------------------------------------------------------------------===//
252// Opcode emitters
253//===----------------------------------------------------------------------===//
254
255#define GET_LINK_IMPL
256#include "Opcodes.inc"
257#undef GET_LINK_IMPL
258