32 const std::string _version;
33 std::vector<std::string> _includePaths;
34 std::unique_ptr<llvm::Linker> _linker;
35 std::unique_ptr<llvm::LLVMContext> _context;
56 inline void setIncludePaths(
const std::vector<std::string>&
includePaths) {
60 inline const std::vector<std::string>& getIncludePaths()
const {
64 std::unique_ptr<LlvmModelLibrary<Base>> create() {
66 OStreamConfigRestore
coutb(std::cout);
70 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
72 llvm::InitializeAllTargets();
73 llvm::InitializeAllAsmPrinters();
75 _context.reset(
new llvm::LLVMContext());
77 const std::map<std::string, ModelCSourceGen<Base>*>&
models = this->modelLibraryHelper_->getModels();
79 const std::map<std::string, std::string>&
modelSources = this->getSources(*
p.second);
83 const std::map<std::string, std::string>&
sources = this->getLibrarySources();
86 const std::map<std::string, std::string>&
customSource = this->modelLibraryHelper_->getCustomSources();
89 llvm::InitializeNativeTarget();
93 this->modelLibraryHelper_->finishedJob();
98 static inline std::unique_ptr<LlvmModelLibrary<Base>> create(ModelLibraryCSourceGen<Base>&
modelLibraryHelper) {
105 virtual void createLlvmModules(
const std::map<std::string, std::string>&
sources) {
107 createLlvmModule(
p.first,
p.second);
111 virtual void createLlvmModule(
const std::string&
filename,
112 const std::string&
source) {
113 using namespace llvm;
114 using namespace clang;
116 static const char*
argv [] = {
"program",
"-Wall",
"-x",
"c",
"string-input"};
117 static const int argc =
sizeof (
argv) /
sizeof (
argv[0]);
128 throw CGException(
"Failed to create compiler invocation");
129 CompilerInvocation::setLangDefaults(*
invocation->getLangOpts(),
IK_C,
130 LangStandard::lang_unspecified);
131 invocation->getFrontendOpts().DisableFree =
false;
140 throw CGException(
"No diagnostics");
143 llvm::MemoryBuffer *
buffer = llvm::MemoryBuffer::getMemBufferCopy(
source,
"SIMPLE_BUFFER");
145 throw CGException(
"Failed to create memory buffer");
149 po.addRemappedFile(
"string-input",
buffer);
152 for (
size_t s = 0; s < _includePaths.size(); s++)
153 hso.AddPath(llvm::StringRef(_includePaths[s]), clang::frontend::Angled,
true,
false,
false);
158 throw CGException(
"Failed to emit LLVM bitcode");
160 llvm::Module*
module = action->takeModule();
162 throw CGException(
"No module");
164 if (_linker.get() ==
nullptr) {
165 _linker.reset(
new llvm::Linker(std::string(
"MyLinker"),
module));
178 inline llvm::Module* mergeModules(
const std::vector<llvm::Module*>&
modules) {
185 for (
size_t m = 1; m <
modules.size(); m++) {
192 return ld->releaseModule();