1e5dd7070Spatrick //===--- ByteCodeEmitter.cpp - Instruction emitter for the VM ---*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "ByteCodeEmitter.h"
10e5dd7070Spatrick #include "Context.h"
11e5dd7070Spatrick #include "Opcode.h"
12e5dd7070Spatrick #include "Program.h"
13e5dd7070Spatrick #include "clang/AST/DeclCXX.h"
14*12c85518Srobert #include <type_traits>
15e5dd7070Spatrick
16e5dd7070Spatrick using namespace clang;
17e5dd7070Spatrick using namespace clang::interp;
18e5dd7070Spatrick
19e5dd7070Spatrick using APSInt = llvm::APSInt;
20e5dd7070Spatrick using Error = llvm::Error;
21e5dd7070Spatrick
22*12c85518Srobert Expected<Function *>
compileFunc(const FunctionDecl * FuncDecl)23*12c85518Srobert ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) {
24*12c85518Srobert // Function is not defined at all or not yet. We will
25*12c85518Srobert // create a Function instance but not compile the body. That
26*12c85518Srobert // will (maybe) happen later.
27*12c85518Srobert bool HasBody = FuncDecl->hasBody(FuncDecl);
28e5dd7070Spatrick
29*12c85518Srobert // Create a handle over the emitted code.
30*12c85518Srobert Function *Func = P.getFunction(FuncDecl);
31*12c85518Srobert if (!Func) {
32e5dd7070Spatrick // Set up argument indices.
33e5dd7070Spatrick unsigned ParamOffset = 0;
34e5dd7070Spatrick SmallVector<PrimType, 8> ParamTypes;
35e5dd7070Spatrick llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
36e5dd7070Spatrick
37*12c85518Srobert // If the return is not a primitive, a pointer to the storage where the
38*12c85518Srobert // value is initialized in is passed as the first argument. See 'RVO'
39*12c85518Srobert // elsewhere in the code.
40*12c85518Srobert QualType Ty = FuncDecl->getReturnType();
41*12c85518Srobert bool HasRVO = false;
42e5dd7070Spatrick if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
43*12c85518Srobert HasRVO = true;
44*12c85518Srobert ParamTypes.push_back(PT_Ptr);
45*12c85518Srobert ParamOffset += align(primSize(PT_Ptr));
46*12c85518Srobert }
47*12c85518Srobert
48*12c85518Srobert // If the function decl is a member decl, the next parameter is
49*12c85518Srobert // the 'this' pointer. This parameter is pop()ed from the
50*12c85518Srobert // InterpStack when calling the function.
51*12c85518Srobert bool HasThisPointer = false;
52*12c85518Srobert if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
53*12c85518Srobert MD && MD->isInstance()) {
54*12c85518Srobert HasThisPointer = true;
55e5dd7070Spatrick ParamTypes.push_back(PT_Ptr);
56e5dd7070Spatrick ParamOffset += align(primSize(PT_Ptr));
57e5dd7070Spatrick }
58e5dd7070Spatrick
59e5dd7070Spatrick // Assign descriptors to all parameters.
60e5dd7070Spatrick // Composite objects are lowered to pointers.
61*12c85518Srobert for (const ParmVarDecl *PD : FuncDecl->parameters()) {
62*12c85518Srobert PrimType Ty = Ctx.classify(PD->getType()).value_or(PT_Ptr);
63e5dd7070Spatrick Descriptor *Desc = P.createDescriptor(PD, Ty);
64e5dd7070Spatrick ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
65e5dd7070Spatrick Params.insert({PD, ParamOffset});
66e5dd7070Spatrick ParamOffset += align(primSize(Ty));
67e5dd7070Spatrick ParamTypes.push_back(Ty);
68e5dd7070Spatrick }
69e5dd7070Spatrick
70*12c85518Srobert Func =
71*12c85518Srobert P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
72*12c85518Srobert std::move(ParamDescriptors), HasThisPointer, HasRVO);
73*12c85518Srobert }
74*12c85518Srobert
75*12c85518Srobert assert(Func);
76*12c85518Srobert if (!HasBody)
77*12c85518Srobert return Func;
78*12c85518Srobert
79e5dd7070Spatrick // Compile the function body.
80*12c85518Srobert if (!FuncDecl->isConstexpr() || !visitFunc(FuncDecl)) {
81e5dd7070Spatrick // Return a dummy function if compilation failed.
82e5dd7070Spatrick if (BailLocation)
83e5dd7070Spatrick return llvm::make_error<ByteCodeGenError>(*BailLocation);
84*12c85518Srobert else {
85*12c85518Srobert Func->setIsFullyCompiled(true);
86e5dd7070Spatrick return Func;
87*12c85518Srobert }
88e5dd7070Spatrick } else {
89e5dd7070Spatrick // Create scopes from descriptors.
90e5dd7070Spatrick llvm::SmallVector<Scope, 2> Scopes;
91e5dd7070Spatrick for (auto &DS : Descriptors) {
92e5dd7070Spatrick Scopes.emplace_back(std::move(DS));
93e5dd7070Spatrick }
94e5dd7070Spatrick
95e5dd7070Spatrick // Set the function's code.
96e5dd7070Spatrick Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
97e5dd7070Spatrick std::move(Scopes));
98*12c85518Srobert Func->setIsFullyCompiled(true);
99e5dd7070Spatrick return Func;
100e5dd7070Spatrick }
101e5dd7070Spatrick }
102e5dd7070Spatrick
createLocal(Descriptor * D)103e5dd7070Spatrick Scope::Local ByteCodeEmitter::createLocal(Descriptor *D) {
104e5dd7070Spatrick NextLocalOffset += sizeof(Block);
105e5dd7070Spatrick unsigned Location = NextLocalOffset;
106e5dd7070Spatrick NextLocalOffset += align(D->getAllocSize());
107e5dd7070Spatrick return {Location, D};
108e5dd7070Spatrick }
109e5dd7070Spatrick
emitLabel(LabelTy Label)110e5dd7070Spatrick void ByteCodeEmitter::emitLabel(LabelTy Label) {
111e5dd7070Spatrick const size_t Target = Code.size();
112e5dd7070Spatrick LabelOffsets.insert({Label, Target});
113e5dd7070Spatrick auto It = LabelRelocs.find(Label);
114e5dd7070Spatrick if (It != LabelRelocs.end()) {
115e5dd7070Spatrick for (unsigned Reloc : It->second) {
116e5dd7070Spatrick using namespace llvm::support;
117e5dd7070Spatrick
118e5dd7070Spatrick /// Rewrite the operand of all jumps to this label.
119*12c85518Srobert void *Location = Code.data() + Reloc - align(sizeof(int32_t));
120*12c85518Srobert assert(aligned(Location));
121e5dd7070Spatrick const int32_t Offset = Target - static_cast<int64_t>(Reloc);
122e5dd7070Spatrick endian::write<int32_t, endianness::native, 1>(Location, Offset);
123e5dd7070Spatrick }
124e5dd7070Spatrick LabelRelocs.erase(It);
125e5dd7070Spatrick }
126e5dd7070Spatrick }
127e5dd7070Spatrick
getOffset(LabelTy Label)128e5dd7070Spatrick int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
129e5dd7070Spatrick // Compute the PC offset which the jump is relative to.
130*12c85518Srobert const int64_t Position =
131*12c85518Srobert Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
132*12c85518Srobert assert(aligned(Position));
133e5dd7070Spatrick
134e5dd7070Spatrick // If target is known, compute jump offset.
135e5dd7070Spatrick auto It = LabelOffsets.find(Label);
136e5dd7070Spatrick if (It != LabelOffsets.end()) {
137e5dd7070Spatrick return It->second - Position;
138e5dd7070Spatrick }
139e5dd7070Spatrick
140e5dd7070Spatrick // Otherwise, record relocation and return dummy offset.
141e5dd7070Spatrick LabelRelocs[Label].push_back(Position);
142e5dd7070Spatrick return 0ull;
143e5dd7070Spatrick }
144e5dd7070Spatrick
bail(const SourceLocation & Loc)145e5dd7070Spatrick bool ByteCodeEmitter::bail(const SourceLocation &Loc) {
146e5dd7070Spatrick if (!BailLocation)
147e5dd7070Spatrick BailLocation = Loc;
148e5dd7070Spatrick return false;
149e5dd7070Spatrick }
150e5dd7070Spatrick
151e5dd7070Spatrick /// Helper to write bytecode and bail out if 32-bit offsets become invalid.
152*12c85518Srobert /// Pointers will be automatically marshalled as 32-bit IDs.
153*12c85518Srobert template <typename T>
emit(Program & P,std::vector<char> & Code,const T & Val,bool & Success)154*12c85518Srobert static void emit(Program &P, std::vector<char> &Code, const T &Val,
155*12c85518Srobert bool &Success) {
156*12c85518Srobert size_t Size;
157*12c85518Srobert
158*12c85518Srobert if constexpr (std::is_pointer_v<T>)
159*12c85518Srobert Size = sizeof(uint32_t);
160*12c85518Srobert else
161*12c85518Srobert Size = sizeof(T);
162*12c85518Srobert
163e5dd7070Spatrick if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
164e5dd7070Spatrick Success = false;
165e5dd7070Spatrick return;
166e5dd7070Spatrick }
167*12c85518Srobert
168*12c85518Srobert // Access must be aligned!
169*12c85518Srobert size_t ValPos = align(Code.size());
170*12c85518Srobert Size = align(Size);
171*12c85518Srobert assert(aligned(ValPos + Size));
172*12c85518Srobert Code.resize(ValPos + Size);
173*12c85518Srobert
174*12c85518Srobert if constexpr (!std::is_pointer_v<T>) {
175*12c85518Srobert new (Code.data() + ValPos) T(Val);
176*12c85518Srobert } else {
177*12c85518Srobert uint32_t ID = P.getOrCreateNativePointer(Val);
178*12c85518Srobert new (Code.data() + ValPos) uint32_t(ID);
179*12c85518Srobert }
180*12c85518Srobert }
181*12c85518Srobert
182*12c85518Srobert template <typename... Tys>
emitOp(Opcode Op,const Tys &...Args,const SourceInfo & SI)183*12c85518Srobert bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) {
184*12c85518Srobert bool Success = true;
185e5dd7070Spatrick
186e5dd7070Spatrick /// The opcode is followed by arguments. The source info is
187e5dd7070Spatrick /// attached to the address after the opcode.
188*12c85518Srobert emit(P, Code, Op, Success);
189e5dd7070Spatrick if (SI)
190e5dd7070Spatrick SrcMap.emplace_back(Code.size(), SI);
191e5dd7070Spatrick
192e5dd7070Spatrick /// The initializer list forces the expression to be evaluated
193e5dd7070Spatrick /// for each argument in the variadic template, in order.
194*12c85518Srobert (void)std::initializer_list<int>{(emit(P, Code, Args, Success), 0)...};
195e5dd7070Spatrick
196e5dd7070Spatrick return Success;
197e5dd7070Spatrick }
198e5dd7070Spatrick
jumpTrue(const LabelTy & Label)199e5dd7070Spatrick bool ByteCodeEmitter::jumpTrue(const LabelTy &Label) {
200e5dd7070Spatrick return emitJt(getOffset(Label), SourceInfo{});
201e5dd7070Spatrick }
202e5dd7070Spatrick
jumpFalse(const LabelTy & Label)203e5dd7070Spatrick bool ByteCodeEmitter::jumpFalse(const LabelTy &Label) {
204e5dd7070Spatrick return emitJf(getOffset(Label), SourceInfo{});
205e5dd7070Spatrick }
206e5dd7070Spatrick
jump(const LabelTy & Label)207e5dd7070Spatrick bool ByteCodeEmitter::jump(const LabelTy &Label) {
208e5dd7070Spatrick return emitJmp(getOffset(Label), SourceInfo{});
209e5dd7070Spatrick }
210e5dd7070Spatrick
fallthrough(const LabelTy & Label)211e5dd7070Spatrick bool ByteCodeEmitter::fallthrough(const LabelTy &Label) {
212e5dd7070Spatrick emitLabel(Label);
213e5dd7070Spatrick return true;
214e5dd7070Spatrick }
215e5dd7070Spatrick
216e5dd7070Spatrick //===----------------------------------------------------------------------===//
217e5dd7070Spatrick // Opcode emitters
218e5dd7070Spatrick //===----------------------------------------------------------------------===//
219e5dd7070Spatrick
220e5dd7070Spatrick #define GET_LINK_IMPL
221e5dd7070Spatrick #include "Opcodes.inc"
222e5dd7070Spatrick #undef GET_LINK_IMPL
223