1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
//===-- bind.cpp ----------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the Boost Software License. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "bind.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "valueparser.h"
#if LDC_LLVM_VER >= 1000
#if LDC_LLVM_VER >= 1100
#define LLAlign llvm::Align
#else
#define LLAlign llvm::MaybeAlign
#endif
#define LLMaybeAlign llvm::MaybeAlign
#else
#define LLAlign
#define LLMaybeAlign
#endif
namespace {
enum { SmallParamsCount = 5 };
llvm::FunctionType *getDstFuncType(llvm::FunctionType &srcType,
const llvm::ArrayRef<ParamSlice> ¶ms) {
assert(!srcType.isVarArg());
llvm::SmallVector<llvm::Type *, SmallParamsCount> newParams;
const auto srcParamsCount = srcType.params().size();
assert(params.size() == srcParamsCount);
for (size_t i = 0; i < srcParamsCount; ++i) {
if (params[i].data == nullptr) {
newParams.push_back(srcType.getParamType(static_cast<unsigned>(i)));
}
}
auto retType = srcType.getReturnType();
return llvm::FunctionType::get(retType, newParams, /*isVarArg*/ false);
}
llvm::Function *createBindFunc(llvm::Module &module, llvm::Function &srcFunc,
llvm::Function &exampleFunc,
llvm::FunctionType &funcType,
const llvm::ArrayRef<ParamSlice> ¶ms) {
auto newFunc = llvm::Function::Create(
&funcType, llvm::GlobalValue::ExternalLinkage, "\1.jit_bind", &module);
newFunc->setCallingConv(srcFunc.getCallingConv());
// auto srcAttributes = srcFunc.getAttributes();
// newFunc->addAttributes(llvm::AttributeList::ReturnIndex,
// srcAttributes.getRetAttributes());
// newFunc->addAttributes(llvm::AttributeList::FunctionIndex,
// srcAttributes.getFnAttributes());
// unsigned dstInd = 0;
// for (size_t i = 0; i < params.size(); ++i) {
// if (params[i].data == nullptr) {
// newFunc->addAttributes(llvm::AttributeList::FirstArgIndex + dstInd,
// srcAttributes.getParamAttributes(
// static_cast<unsigned>(i)));
// ++dstInd;
// }
// }
// assert(dstInd == funcType.getNumParams());
newFunc->setAttributes(exampleFunc.getAttributes());
return newFunc;
}
llvm::Value *
allocParam(llvm::IRBuilder<> &builder, llvm::Type &srcType,
const llvm::DataLayout &layout, const ParamSlice ¶m,
llvm::function_ref<void(const std::string &)> errHandler,
const BindOverride &override) {
if (param.type == ParamType::Aggregate && srcType.isPointerTy()) {
auto elemType = llvm::cast<llvm::PointerType>(&srcType)->getElementType();
auto stackArg = builder.CreateAlloca(elemType);
if (auto alignment = layout.getABITypeAlignment(elemType))
stackArg->setAlignment(LLAlign(alignment));
auto init =
parseInitializer(layout, *elemType, param.data, errHandler, override);
builder.CreateStore(init, stackArg);
return stackArg;
}
auto stackArg = builder.CreateAlloca(&srcType);
if (auto alignment = layout.getABITypeAlignment(&srcType))
stackArg->setAlignment(LLAlign(alignment));
auto init =
parseInitializer(layout, srcType, param.data, errHandler, override);
builder.CreateStore(init, stackArg);
return builder.CreateLoad(stackArg);
}
void doBind(llvm::Module &module, llvm::Function &dstFunc,
llvm::Function &srcFunc, const llvm::ArrayRef<ParamSlice> ¶ms,
llvm::function_ref<void(const std::string &)> errHandler,
const BindOverride &override) {
auto &context = dstFunc.getContext();
auto bb = llvm::BasicBlock::Create(context, "", &dstFunc);
llvm::IRBuilder<> builder(context);
builder.SetInsertPoint(bb);
llvm::SmallVector<llvm::Value *, SmallParamsCount> args;
auto currentArg = dstFunc.arg_begin();
auto funcType = srcFunc.getFunctionType();
auto &layout = module.getDataLayout();
for (size_t i = 0; i < params.size(); ++i) {
llvm::Value *arg = nullptr;
const auto ¶m = params[i];
if (param.data == nullptr) {
arg = currentArg;
++currentArg;
} else {
auto type = funcType->getParamType(static_cast<unsigned>(i));
arg = allocParam(builder, *type, layout, param, errHandler, override);
}
assert(arg != nullptr);
args.push_back(arg);
}
assert(currentArg == dstFunc.arg_end());
auto ret = builder.CreateCall(&srcFunc, args);
if (!srcFunc.isDeclaration()) {
ret->addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::AlwaysInline);
}
ret->setCallingConv(srcFunc.getCallingConv());
ret->setAttributes(srcFunc.getAttributes());
if (dstFunc.getReturnType()->isVoidTy()) {
builder.CreateRetVoid();
} else {
builder.CreateRet(ret);
}
}
}
llvm::Function *
bindParamsToFunc(llvm::Module &module, llvm::Function &srcFunc,
llvm::Function &exampleFunc,
const llvm::ArrayRef<ParamSlice> ¶ms,
llvm::function_ref<void(const std::string &)> errHandler,
const BindOverride &override) {
auto srcType = srcFunc.getFunctionType();
auto dstType = getDstFuncType(*srcType, params);
auto newFunc = createBindFunc(module, srcFunc, exampleFunc, *dstType, params);
doBind(module, *newFunc, srcFunc, params, errHandler, override);
return newFunc;
}
|