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