1*7330f729Sjoerg //===--- ByteCodeEmitter.cpp - Instruction emitter for the VM ---*- C++ -*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg
9*7330f729Sjoerg #include "ByteCodeEmitter.h"
10*7330f729Sjoerg #include "Context.h"
11*7330f729Sjoerg #include "Opcode.h"
12*7330f729Sjoerg #include "Program.h"
13*7330f729Sjoerg #include "clang/AST/DeclCXX.h"
14*7330f729Sjoerg
15*7330f729Sjoerg using namespace clang;
16*7330f729Sjoerg using namespace clang::interp;
17*7330f729Sjoerg
18*7330f729Sjoerg using APSInt = llvm::APSInt;
19*7330f729Sjoerg using Error = llvm::Error;
20*7330f729Sjoerg
compileFunc(const FunctionDecl * F)21*7330f729Sjoerg Expected<Function *> ByteCodeEmitter::compileFunc(const FunctionDecl *F) {
22*7330f729Sjoerg // Do not try to compile undefined functions.
23*7330f729Sjoerg if (!F->isDefined(F) || (!F->hasBody() && F->willHaveBody()))
24*7330f729Sjoerg return nullptr;
25*7330f729Sjoerg
26*7330f729Sjoerg // Set up argument indices.
27*7330f729Sjoerg unsigned ParamOffset = 0;
28*7330f729Sjoerg SmallVector<PrimType, 8> ParamTypes;
29*7330f729Sjoerg llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
30*7330f729Sjoerg
31*7330f729Sjoerg // If the return is not a primitive, a pointer to the storage where the value
32*7330f729Sjoerg // is initialized in is passed as the first argument.
33*7330f729Sjoerg QualType Ty = F->getReturnType();
34*7330f729Sjoerg if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
35*7330f729Sjoerg ParamTypes.push_back(PT_Ptr);
36*7330f729Sjoerg ParamOffset += align(primSize(PT_Ptr));
37*7330f729Sjoerg }
38*7330f729Sjoerg
39*7330f729Sjoerg // Assign descriptors to all parameters.
40*7330f729Sjoerg // Composite objects are lowered to pointers.
41*7330f729Sjoerg for (const ParmVarDecl *PD : F->parameters()) {
42*7330f729Sjoerg PrimType Ty;
43*7330f729Sjoerg if (llvm::Optional<PrimType> T = Ctx.classify(PD->getType())) {
44*7330f729Sjoerg Ty = *T;
45*7330f729Sjoerg } else {
46*7330f729Sjoerg Ty = PT_Ptr;
47*7330f729Sjoerg }
48*7330f729Sjoerg
49*7330f729Sjoerg Descriptor *Desc = P.createDescriptor(PD, Ty);
50*7330f729Sjoerg ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
51*7330f729Sjoerg Params.insert({PD, ParamOffset});
52*7330f729Sjoerg ParamOffset += align(primSize(Ty));
53*7330f729Sjoerg ParamTypes.push_back(Ty);
54*7330f729Sjoerg }
55*7330f729Sjoerg
56*7330f729Sjoerg // Create a handle over the emitted code.
57*7330f729Sjoerg Function *Func = P.createFunction(F, ParamOffset, std::move(ParamTypes),
58*7330f729Sjoerg std::move(ParamDescriptors));
59*7330f729Sjoerg // Compile the function body.
60*7330f729Sjoerg if (!F->isConstexpr() || !visitFunc(F)) {
61*7330f729Sjoerg // Return a dummy function if compilation failed.
62*7330f729Sjoerg if (BailLocation)
63*7330f729Sjoerg return llvm::make_error<ByteCodeGenError>(*BailLocation);
64*7330f729Sjoerg else
65*7330f729Sjoerg return Func;
66*7330f729Sjoerg } else {
67*7330f729Sjoerg // Create scopes from descriptors.
68*7330f729Sjoerg llvm::SmallVector<Scope, 2> Scopes;
69*7330f729Sjoerg for (auto &DS : Descriptors) {
70*7330f729Sjoerg Scopes.emplace_back(std::move(DS));
71*7330f729Sjoerg }
72*7330f729Sjoerg
73*7330f729Sjoerg // Set the function's code.
74*7330f729Sjoerg Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
75*7330f729Sjoerg std::move(Scopes));
76*7330f729Sjoerg return Func;
77*7330f729Sjoerg }
78*7330f729Sjoerg }
79*7330f729Sjoerg
createLocal(Descriptor * D)80*7330f729Sjoerg Scope::Local ByteCodeEmitter::createLocal(Descriptor *D) {
81*7330f729Sjoerg NextLocalOffset += sizeof(Block);
82*7330f729Sjoerg unsigned Location = NextLocalOffset;
83*7330f729Sjoerg NextLocalOffset += align(D->getAllocSize());
84*7330f729Sjoerg return {Location, D};
85*7330f729Sjoerg }
86*7330f729Sjoerg
emitLabel(LabelTy Label)87*7330f729Sjoerg void ByteCodeEmitter::emitLabel(LabelTy Label) {
88*7330f729Sjoerg const size_t Target = Code.size();
89*7330f729Sjoerg LabelOffsets.insert({Label, Target});
90*7330f729Sjoerg auto It = LabelRelocs.find(Label);
91*7330f729Sjoerg if (It != LabelRelocs.end()) {
92*7330f729Sjoerg for (unsigned Reloc : It->second) {
93*7330f729Sjoerg using namespace llvm::support;
94*7330f729Sjoerg
95*7330f729Sjoerg /// Rewrite the operand of all jumps to this label.
96*7330f729Sjoerg void *Location = Code.data() + Reloc - sizeof(int32_t);
97*7330f729Sjoerg const int32_t Offset = Target - static_cast<int64_t>(Reloc);
98*7330f729Sjoerg endian::write<int32_t, endianness::native, 1>(Location, Offset);
99*7330f729Sjoerg }
100*7330f729Sjoerg LabelRelocs.erase(It);
101*7330f729Sjoerg }
102*7330f729Sjoerg }
103*7330f729Sjoerg
getOffset(LabelTy Label)104*7330f729Sjoerg int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
105*7330f729Sjoerg // Compute the PC offset which the jump is relative to.
106*7330f729Sjoerg const int64_t Position = Code.size() + sizeof(Opcode) + sizeof(int32_t);
107*7330f729Sjoerg
108*7330f729Sjoerg // If target is known, compute jump offset.
109*7330f729Sjoerg auto It = LabelOffsets.find(Label);
110*7330f729Sjoerg if (It != LabelOffsets.end()) {
111*7330f729Sjoerg return It->second - Position;
112*7330f729Sjoerg }
113*7330f729Sjoerg
114*7330f729Sjoerg // Otherwise, record relocation and return dummy offset.
115*7330f729Sjoerg LabelRelocs[Label].push_back(Position);
116*7330f729Sjoerg return 0ull;
117*7330f729Sjoerg }
118*7330f729Sjoerg
bail(const SourceLocation & Loc)119*7330f729Sjoerg bool ByteCodeEmitter::bail(const SourceLocation &Loc) {
120*7330f729Sjoerg if (!BailLocation)
121*7330f729Sjoerg BailLocation = Loc;
122*7330f729Sjoerg return false;
123*7330f729Sjoerg }
124*7330f729Sjoerg
125*7330f729Sjoerg template <typename... Tys>
emitOp(Opcode Op,const Tys &...Args,const SourceInfo & SI)126*7330f729Sjoerg bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) {
127*7330f729Sjoerg bool Success = true;
128*7330f729Sjoerg
129*7330f729Sjoerg /// Helper to write bytecode and bail out if 32-bit offsets become invalid.
130*7330f729Sjoerg auto emit = [this, &Success](const char *Data, size_t Size) {
131*7330f729Sjoerg if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
132*7330f729Sjoerg Success = false;
133*7330f729Sjoerg return;
134*7330f729Sjoerg }
135*7330f729Sjoerg Code.insert(Code.end(), Data, Data + Size);
136*7330f729Sjoerg };
137*7330f729Sjoerg
138*7330f729Sjoerg /// The opcode is followed by arguments. The source info is
139*7330f729Sjoerg /// attached to the address after the opcode.
140*7330f729Sjoerg emit(reinterpret_cast<const char *>(&Op), sizeof(Opcode));
141*7330f729Sjoerg if (SI)
142*7330f729Sjoerg SrcMap.emplace_back(Code.size(), SI);
143*7330f729Sjoerg
144*7330f729Sjoerg /// The initializer list forces the expression to be evaluated
145*7330f729Sjoerg /// for each argument in the variadic template, in order.
146*7330f729Sjoerg (void)std::initializer_list<int>{
147*7330f729Sjoerg (emit(reinterpret_cast<const char *>(&Args), sizeof(Args)), 0)...};
148*7330f729Sjoerg
149*7330f729Sjoerg return Success;
150*7330f729Sjoerg }
151*7330f729Sjoerg
jumpTrue(const LabelTy & Label)152*7330f729Sjoerg bool ByteCodeEmitter::jumpTrue(const LabelTy &Label) {
153*7330f729Sjoerg return emitJt(getOffset(Label), SourceInfo{});
154*7330f729Sjoerg }
155*7330f729Sjoerg
jumpFalse(const LabelTy & Label)156*7330f729Sjoerg bool ByteCodeEmitter::jumpFalse(const LabelTy &Label) {
157*7330f729Sjoerg return emitJf(getOffset(Label), SourceInfo{});
158*7330f729Sjoerg }
159*7330f729Sjoerg
jump(const LabelTy & Label)160*7330f729Sjoerg bool ByteCodeEmitter::jump(const LabelTy &Label) {
161*7330f729Sjoerg return emitJmp(getOffset(Label), SourceInfo{});
162*7330f729Sjoerg }
163*7330f729Sjoerg
fallthrough(const LabelTy & Label)164*7330f729Sjoerg bool ByteCodeEmitter::fallthrough(const LabelTy &Label) {
165*7330f729Sjoerg emitLabel(Label);
166*7330f729Sjoerg return true;
167*7330f729Sjoerg }
168*7330f729Sjoerg
169*7330f729Sjoerg //===----------------------------------------------------------------------===//
170*7330f729Sjoerg // Opcode emitters
171*7330f729Sjoerg //===----------------------------------------------------------------------===//
172*7330f729Sjoerg
173*7330f729Sjoerg #define GET_LINK_IMPL
174*7330f729Sjoerg #include "Opcodes.inc"
175*7330f729Sjoerg #undef GET_LINK_IMPL
176