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