github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/jit/jit_engine.h (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 #pragma once 21 #include "common/common.h" 22 #include "jit/OrcLazyJIT.h" 23 24 namespace neb { 25 namespace jit { 26 using namespace llvm; 27 class jit_engine { 28 public: 29 30 template <typename RT, typename... ARGS> RT run(ARGS... args) { 31 std::unique_lock<std::mutex> _l(m_mutex); 32 using MainFnPtr = RT (*)(ARGS...); 33 auto main_func = fromTargetAddress<MainFnPtr>( 34 cantFail(m_main_sym->getAddress(), nullptr)); 35 if (nullptr == main_func) 36 return RT(); 37 return main_func(args...); 38 } 39 40 void init(std::vector<std::unique_ptr<Module>> ms, 41 const std::string &func_name); 42 43 protected: 44 template <typename PtrTy> 45 static PtrTy fromTargetAddress(llvm::JITTargetAddress Addr) { 46 return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr)); 47 } 48 49 protected: 50 std::vector<std::unique_ptr<Module>> m_modules; 51 std::string m_func_name; 52 std::unique_ptr<llvm::EngineBuilder> m_EB; 53 std::unique_ptr<Triple> m_T; 54 std::unique_ptr<OrcLazyJIT> m_jit; 55 std::mutex m_mutex; 56 57 std::unique_ptr<llvm::JITSymbol> m_main_sym; 58 }; // end class jit_engine 59 } // namespace jit 60 } // namespace neb