diff --git a/include/evmjit/JIT.h b/include/evmjit/JIT.h index 8d6f9bbd0..901c351d9 100644 --- a/include/evmjit/JIT.h +++ b/include/evmjit/JIT.h @@ -153,6 +153,9 @@ public: /// \param _codeHash The Keccak hash of the EVM code. EXPORT static bool isCodeReady(h256 const& _codeHash); + /// Compile the given EVM code to machine code and make available for execution. + EXPORT static void compile(byte const* _code, uint64_t _codeSize, h256 const& _codeHash); + EXPORT static ReturnCode exec(ExecutionContext& _context); }; diff --git a/libevmjit/JIT.cpp b/libevmjit/JIT.cpp index 89d2e63eb..53c36dcb2 100644 --- a/libevmjit/JIT.cpp +++ b/libevmjit/JIT.cpp @@ -1,6 +1,7 @@ #include "evmjit/JIT.h" #include +#include #include "preprocessor/llvm_includes_start.h" #include @@ -82,6 +83,7 @@ void parseOptions() class JITImpl { std::unique_ptr m_engine; + mutable std::mutex x_codeMap; std::unordered_map m_codeMap; public: @@ -136,6 +138,7 @@ JITImpl::JITImpl() ExecFunc JITImpl::getExecFunc(h256 const& _codeHash) const { + std::lock_guard lock{x_codeMap}; auto it = m_codeMap.find(_codeHash); if (it != m_codeMap.end()) return it->second; @@ -144,6 +147,7 @@ ExecFunc JITImpl::getExecFunc(h256 const& _codeHash) const void JITImpl::mapExecFunc(h256 _codeHash, ExecFunc _funcAddr) { + std::lock_guard lock{x_codeMap}; m_codeMap.emplace(std::move(_codeHash), _funcAddr); } @@ -181,6 +185,14 @@ bool JIT::isCodeReady(h256 const& _codeHash) return JITImpl::instance().getExecFunc(_codeHash) != nullptr; } +void JIT::compile(byte const* _code, uint64_t _codeSize, h256 const& _codeHash) +{ + auto& jit = JITImpl::instance(); + auto execFunc = jit.compile(_code, _codeSize, _codeHash); + if (execFunc) // FIXME: What with error? + jit.mapExecFunc(_codeHash, execFunc); +} + ReturnCode JIT::exec(ExecutionContext& _context) { //std::unique_ptr listener{new ExecStats};