#include "ByteCodeEmitter.h"
#include "Context.h"
#include "Floating.h"
#include "IntegralAP.h"
#include "Opcode.h"
#include "Program.h"
#include "clang/AST/ASTLambda.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Basic/Builtins.h"
#include <type_traits>
using namespace clang;
using namespace clang::interp;
static bool isUnevaluatedBuiltin(unsigned BuiltinID) {
return BuiltinID == Builtin::BI__builtin_classify_type ||
BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size;
}
Function *ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) {
if (!FuncDecl->param_empty() && !FuncDecl->param_begin())
return nullptr;
bool IsLambdaStaticInvoker = false;
if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
MD && MD->isLambdaStaticInvoker()) {
IsLambdaStaticInvoker = true;
const CXXRecordDecl *ClosureClass = MD->getParent();
assert(ClosureClass->captures_begin() == ClosureClass->captures_end());
if (ClosureClass->isGenericLambda()) {
const CXXMethodDecl *LambdaCallOp = ClosureClass->getLambdaCallOperator();
assert(MD->isFunctionTemplateSpecialization() &&
"A generic lambda's static-invoker function must be a "
"template specialization");
const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
FunctionTemplateDecl *CallOpTemplate =
LambdaCallOp->getDescribedFunctionTemplate();
void *InsertPos = nullptr;
const FunctionDecl *CorrespondingCallOpSpecialization =
CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
assert(CorrespondingCallOpSpecialization);
FuncDecl = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
}
}
unsigned ParamOffset = 0;
SmallVector<PrimType, 8> ParamTypes;
SmallVector<unsigned, 8> ParamOffsets;
llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
QualType Ty = FuncDecl->getReturnType();
bool HasRVO = false;
if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
HasRVO = true;
ParamTypes.push_back(PT_Ptr);
ParamOffsets.push_back(ParamOffset);
ParamOffset += align(primSize(PT_Ptr));
}
bool HasThisPointer = false;
if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl)) {
if (!IsLambdaStaticInvoker) {
HasThisPointer = MD->isInstance();
if (MD->isImplicitObjectMemberFunction()) {
ParamTypes.push_back(PT_Ptr);
ParamOffsets.push_back(ParamOffset);
ParamOffset += align(primSize(PT_Ptr));
}
}
if (isLambdaCallOperator(MD)) {
if (!MD->getParent()->isCompleteDefinition())
return nullptr;
const Record *R = P.getOrCreateRecord(MD->getParent());
llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
FieldDecl *LTC;
MD->getParent()->getCaptureFields(LC, LTC);
for (auto Cap : LC) {
if (MD->isStatic())
return nullptr;
unsigned Offset = R->getField(Cap.second)->Offset;
this->LambdaCaptures[Cap.first] = {
Offset, Cap.second->getType()->isReferenceType()};
}
if (LTC) {
QualType CaptureType = R->getField(LTC)->Decl->getType();
this->LambdaThisCapture = {R->getField(LTC)->Offset,
CaptureType->isReferenceType() ||
CaptureType->isPointerType()};
}
}
}
for (const ParmVarDecl *PD : FuncDecl->parameters()) {
std::optional<PrimType> T = Ctx.classify(PD->getType());
PrimType PT = T.value_or(PT_Ptr);
Descriptor *Desc = P.createDescriptor(PD, PT);
ParamDescriptors.insert({ParamOffset, {PT, Desc}});
Params.insert({PD, {ParamOffset, T != std::nullopt}});
ParamOffsets.push_back(ParamOffset);
ParamOffset += align(primSize(PT));
ParamTypes.push_back(PT);
}
Function *Func = P.getFunction(FuncDecl);
if (!Func) {
bool IsUnevaluatedBuiltin = false;
if (unsigned BI = FuncDecl->getBuiltinID())
IsUnevaluatedBuiltin = isUnevaluatedBuiltin(BI);
Func =
P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
std::move(ParamDescriptors), std::move(ParamOffsets),
HasThisPointer, HasRVO, IsUnevaluatedBuiltin);
}
assert(Func);
if (!FuncDecl->isDefined() ||
(FuncDecl->willHaveBody() && !FuncDecl->hasBody())) {
Func->setDefined(false);
return Func;
}
Func->setDefined(true);
bool IsEligibleForCompilation = false;
if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
IsEligibleForCompilation = MD->isLambdaStaticInvoker();
if (!IsEligibleForCompilation)
IsEligibleForCompilation =
FuncDecl->isConstexpr() || FuncDecl->hasAttr<MSConstexprAttr>();
if (!IsEligibleForCompilation || !visitFunc(FuncDecl)) {
Func->setIsFullyCompiled(true);
return Func;
}
llvm::SmallVector<Scope, 2> Scopes;
for (auto &DS : Descriptors) {
Scopes.emplace_back(std::move(DS));
}
Func->setCode(NextLocalOffset, std::move(Code), std::move(SrcMap),
std::move(Scopes), FuncDecl->hasBody());
Func->setIsFullyCompiled(true);
return Func;
}
Scope::Local ByteCodeEmitter::createLocal(Descriptor *D) {
NextLocalOffset += sizeof(Block);
unsigned Location = NextLocalOffset;
NextLocalOffset += align(D->getAllocSize());
return {Location, D};
}
void ByteCodeEmitter::emitLabel(LabelTy Label) {
const size_t Target = Code.size();
LabelOffsets.insert({Label, Target});
if (auto It = LabelRelocs.find(Label);
It != LabelRelocs.end()) {
for (unsigned Reloc : It->second) {
using namespace llvm::support;
void *Location = Code.data() + Reloc - align(sizeof(int32_t));
assert(aligned(Location));
const int32_t Offset = Target - static_cast<int64_t>(Reloc);
endian::write<int32_t, llvm::endianness::native>(Location, Offset);
}
LabelRelocs.erase(It);
}
}
int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
const int64_t Position =
Code.size() + align(sizeof(Opcode)) + align(sizeof(int32_t));
assert(aligned(Position));
if (auto It = LabelOffsets.find(Label);
It != LabelOffsets.end())
return It->second - Position;
LabelRelocs[Label].push_back(Position);
return 0ull;
}
template <typename T>
static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
bool &Success) {
size_t Size;
if constexpr (std::is_pointer_v<T>)
Size = sizeof(uint32_t);
else
Size = sizeof(T);
if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
Success = false;
return;
}
size_t ValPos = align(Code.size());
Size = align(Size);
assert(aligned(ValPos + Size));
Code.resize(ValPos + Size);
if constexpr (!std::is_pointer_v<T>) {
new (Code.data() + ValPos) T(Val);
} else {
uint32_t ID = P.getOrCreateNativePointer(Val);
new (Code.data() + ValPos) uint32_t(ID);
}
}
template <typename T>
static void emitSerialized(std::vector<std::byte> &Code, const T &Val,
bool &Success) {
size_t Size = Val.bytesToSerialize();
if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
Success = false;
return;
}
size_t ValPos = align(Code.size());
Size = align(Size);
assert(aligned(ValPos + Size));
Code.resize(ValPos + Size);
Val.serialize(Code.data() + ValPos);
}
template <>
void emit(Program &P, std::vector<std::byte> &Code, const Floating &Val,
bool &Success) {
emitSerialized(Code, Val, Success);
}
template <>
void emit(Program &P, std::vector<std::byte> &Code,
const IntegralAP<false> &Val, bool &Success) {
emitSerialized(Code, Val, Success);
}
template <>
void emit(Program &P, std::vector<std::byte> &Code, const IntegralAP<true> &Val,
bool &Success) {
emitSerialized(Code, Val, Success);
}
template <typename... Tys>
bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &... Args, const SourceInfo &SI) {
bool Success = true;
emit(P, Code, Op, Success);
if (SI)
SrcMap.emplace_back(Code.size(), SI);
(..., emit(P, Code, Args, Success));
return Success;
}
bool ByteCodeEmitter::jumpTrue(const LabelTy &Label) {
return emitJt(getOffset(Label), SourceInfo{});
}
bool ByteCodeEmitter::jumpFalse(const LabelTy &Label) {
return emitJf(getOffset(Label), SourceInfo{});
}
bool ByteCodeEmitter::jump(const LabelTy &Label) {
return emitJmp(getOffset(Label), SourceInfo{});
}
bool ByteCodeEmitter::fallthrough(const LabelTy &Label) {
emitLabel(Label);
return true;
}
#define GET_LINK_IMPL
#include "Opcodes.inc"
#undef GET_LINK_IMPL