diff --git a/evmcc/evmcc.cpp b/evmcc/evmcc.cpp index 9828d4876..5ee11abb1 100644 --- a/evmcc/evmcc.cpp +++ b/evmcc/evmcc.cpp @@ -196,7 +196,7 @@ int main(int argc, char** argv) data.code = bytecode.data(); // BROKEN: env_* functions must be implemented & RuntimeData struct created - auto result = engine.run(std::move(module), &data, nullptr); + auto result = engine.run(std::move(module), &data, nullptr, bytecode); return static_cast(result); } } diff --git a/libevmjit/Cache.cpp b/libevmjit/Cache.cpp index 1dd6f1d9f..663c0d820 100644 --- a/libevmjit/Cache.cpp +++ b/libevmjit/Cache.cpp @@ -27,7 +27,7 @@ ExecBundle& Cache::registerExec(Cache::Key _key, ExecBundle&& _exec) auto& map = getCacheMap(); auto r = map.insert(std::make_pair(_key, std::move(_exec))); assert(r.second && "Updating cached objects not supported"); - LOG() << "add " << _key << "\n"; + LOG() << "add\n"; return r.first->second; // return exec, now owned by cache } @@ -37,10 +37,10 @@ ExecBundle* Cache::findExec(Cache::Key _key) auto it = map.find(_key); if (it != map.end()) { - LOG() << "hit " << _key << "\n"; + LOG() << "hit\n"; return &it->second; } - LOG() << "miss " << _key << "\n"; + LOG() << "miss\n"; return nullptr; } diff --git a/libevmjit/Cache.h b/libevmjit/Cache.h index c9a74ea91..62875edac 100644 --- a/libevmjit/Cache.h +++ b/libevmjit/Cache.h @@ -12,16 +12,22 @@ namespace jit { /// A bundle of objects and information needed for a contract execution -struct ExecBundle +class ExecBundle { +public: std::unique_ptr engine; llvm::Function* entryFunc = nullptr; + + ExecBundle() = default; + ExecBundle(ExecBundle&&) = default; + ExecBundle(ExecBundle const&) = delete; + void operator=(ExecBundle) = delete; }; class Cache { public: - using Key = void const*; + using Key = std::string; static ExecBundle& registerExec(Key _key, ExecBundle&& _exec); static ExecBundle* findExec(Key _key); diff --git a/libevmjit/ExecutionEngine.cpp b/libevmjit/ExecutionEngine.cpp index 719be9ac6..936899828 100644 --- a/libevmjit/ExecutionEngine.cpp +++ b/libevmjit/ExecutionEngine.cpp @@ -36,13 +36,13 @@ namespace jit ReturnCode ExecutionEngine::run(bytes const& _code, RuntimeData* _data, Env* _env) { auto module = Compiler({}).compile(_code); - return run(std::move(module), _data, _env); + return run(std::move(module), _data, _env, _code); } -ReturnCode ExecutionEngine::run(std::unique_ptr _module, RuntimeData* _data, Env* _env) +ReturnCode ExecutionEngine::run(std::unique_ptr _module, RuntimeData* _data, Env* _env, bytes const& _code) { - auto key = _data->code; // TODO: Change cache key - if (auto cachedExec = Cache::findExec(key)) + std::string key{reinterpret_cast(_code.data()), _code.size()}; + if (auto cachedExec = Cache::findExec(std::move(key))) { return run(*cachedExec, _data, _env); } @@ -94,7 +94,7 @@ ReturnCode ExecutionEngine::run(std::unique_ptr _module, RuntimeDa if (!exec.entryFunc) return ReturnCode::LLVMLinkError; - auto& cachedExec = Cache::registerExec(_data->code, std::move(exec)); + auto& cachedExec = Cache::registerExec(key, std::move(exec)); auto returnCode = run(cachedExec, _data, _env); auto executionEndTime = std::chrono::high_resolution_clock::now(); diff --git a/libevmjit/ExecutionEngine.h b/libevmjit/ExecutionEngine.h index 785e4efda..499aa894a 100644 --- a/libevmjit/ExecutionEngine.h +++ b/libevmjit/ExecutionEngine.h @@ -13,7 +13,7 @@ namespace eth { namespace jit { -struct ExecBundle; +class ExecBundle; class ExecutionEngine { @@ -23,7 +23,7 @@ public: void operator=(ExecutionEngine) = delete; ReturnCode run(bytes const& _code, RuntimeData* _data, Env* _env); - ReturnCode run(std::unique_ptr module, RuntimeData* _data, Env* _env); + ReturnCode run(std::unique_ptr module, RuntimeData* _data, Env* _env, bytes const& _code); bytes returnData;