File: bind.cpp

package info (click to toggle)
ldc 1%3A1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 53,728 kB
  • sloc: cpp: 55,939; ansic: 10,599; sh: 958; makefile: 801; asm: 507; objc: 122; exp: 30; python: 12
file content (157 lines) | stat: -rw-r--r-- 5,807 bytes parent folder | download
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> &params) {
  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> &params) {
  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 &param,
           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> &params,
            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 &param = 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> &params,
                 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;
}