github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/jit/jit_engine.cpp (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #include "jit/jit_engine.h" 21 22 namespace neb { 23 namespace jit { 24 using namespace llvm; 25 26 void jit_engine::init(std::vector<std::unique_ptr<Module>> ms, 27 const std::string &func_name) { 28 m_modules = std::move(ms); 29 m_func_name = func_name; 30 // Grab a target machine and try to build a factory function for the 31 // target-specific Orc callback manager. 32 m_EB = std::make_unique<llvm::EngineBuilder>(); 33 m_EB->setOptLevel(CodeGenOpt::Default); 34 auto TM = std::unique_ptr<TargetMachine>(m_EB->selectTarget()); 35 m_T = std::make_unique<Triple>(TM->getTargetTriple()); 36 37 auto CompileCallbackMgr = orc::createLocalCompileCallbackManager(*m_T, 0); 38 // If we couldn't build the factory function then there must not be a 39 // callback manager for this target. Bail out. 40 if (!CompileCallbackMgr) { 41 LOG(ERROR) << "No callback manager available for target '" 42 << TM->getTargetTriple().str() << "'.\n"; 43 throw neb::jit_internal_failure("No callback manager available for target"); 44 } 45 46 auto IndirectStubsMgrBuilder = 47 orc::createLocalIndirectStubsManagerBuilder(*m_T); 48 49 // If we couldn't build a stubs-manager-builder for this target then bail 50 // out. 51 if (!IndirectStubsMgrBuilder) { 52 LOG(ERROR) << "No indirect stubs manager available for target '" 53 << TM->getTargetTriple().str() << "'.\n"; 54 throw neb::jit_internal_failure( 55 "No indirect stubs manager available for target"); 56 } 57 58 // Everything looks good. Build the JIT. 59 bool OrcInlineStubs = true; 60 m_jit = std::make_unique<OrcLazyJIT>( 61 std::move(TM), std::move(CompileCallbackMgr), 62 std::move(IndirectStubsMgrBuilder), OrcInlineStubs); 63 64 // Add the module, look up main and run it. 65 for (auto &M : m_modules) { 66 outs().flush(); 67 68 try { 69 cantFail(m_jit->addModule(std::shared_ptr<Module>(std::move(M))), 70 nullptr); 71 } catch (std::exception &e) { 72 LOG(ERROR) << e.what(); 73 } 74 } 75 try { 76 m_main_sym = std::make_unique<llvm::JITSymbol>( 77 m_jit->findSymbol(std::string(m_func_name, std::allocator<char>()))); 78 if (*m_main_sym) { 79 return; 80 } else if (auto Err = m_main_sym->takeError()) { 81 logAllUnhandledErrors(std::move(Err), llvm::errs(), ""); 82 throw neb::jit_internal_failure("Unhandled errors"); 83 } else { 84 LOG(ERROR) << "Could not find target function.\n"; 85 throw neb::jit_internal_failure("Could not find target function"); 86 } 87 } catch (std::exception &e) { 88 LOG(INFO) << e.what(); 89 } 90 } 91 } // namespace jit 92 } // namespace neb