Browse Source

Better style of ExecutionEngine initialization. Some eth::jit -> evmjit namespace transfers.

cl-refactor
Paweł Bylica 10 years ago
parent
commit
b8fb3a138c
  1. 10
      include/evmjit/JIT.h
  2. 18
      libevmjit-cpp/JitVM.cpp
  3. 2
      libevmjit-cpp/JitVM.h
  4. 6
      libevmjit/Cache.cpp
  5. 5
      libevmjit/Cache.h
  6. 5
      libevmjit/ExecStats.cpp
  7. 6
      libevmjit/ExecStats.h
  8. 35
      libevmjit/ExecutionEngine.cpp
  9. 20
      libevmjit/ExecutionEngine.h
  10. 2
      libevmjit/interface.cpp

10
include/evmjit/JIT.h

@ -4,14 +4,6 @@
namespace dev namespace dev
{ {
namespace eth
{
namespace jit
{
class ExecutionEngine;
}
}
namespace evmjit namespace evmjit
{ {
@ -26,7 +18,7 @@ public:
static bool isCodeReady(h256 _codeHash); static bool isCodeReady(h256 _codeHash);
private: private:
friend class dev::eth::jit::ExecutionEngine; friend class ExecutionEngine;
static void* getCode(h256 _codeHash); static void* getCode(h256 _codeHash);
static void mapCode(h256 _codeHash, void* _funcAddr); static void mapCode(h256 _codeHash, void* _funcAddr);

18
libevmjit-cpp/JitVM.cpp

@ -19,8 +19,6 @@ extern "C" void env_sload(); // fake declaration for linker symbol stripping wor
bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step) bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step)
{ {
using namespace jit;
auto rejected = false; auto rejected = false;
// TODO: Rejecting transactions with gas limit > 2^63 can be used by attacker to take JIT out of scope // TODO: Rejecting transactions with gas limit > 2^63 can be used by attacker to take JIT out of scope
rejected |= m_gas > std::numeric_limits<decltype(m_data.gas)>::max(); // Do not accept requests with gas > 2^63 (int64 max) rejected |= m_gas > std::numeric_limits<decltype(m_data.gas)>::max(); // Do not accept requests with gas > 2^63 (int64 max)
@ -54,23 +52,23 @@ bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step)
m_data.codeSize = _ext.code.size(); m_data.codeSize = _ext.code.size();
m_data.codeHash = eth2llvm(sha3(_ext.code)); m_data.codeHash = eth2llvm(sha3(_ext.code));
m_context.init(m_data, reinterpret_cast<Env*>(&_ext)); m_context.init(m_data, reinterpret_cast<evmjit::Env*>(&_ext));
auto exitCode = jit::ExecutionEngine::run(m_context); auto exitCode = evmjit::ExecutionEngine::run(m_context);
switch (exitCode) switch (exitCode)
{ {
case ReturnCode::Suicide: case evmjit::ReturnCode::Suicide:
_ext.suicide(right160(llvm2eth(m_data.address))); _ext.suicide(right160(llvm2eth(m_data.address)));
break; break;
case ReturnCode::BadJumpDestination: case evmjit::ReturnCode::BadJumpDestination:
BOOST_THROW_EXCEPTION(BadJumpDestination()); BOOST_THROW_EXCEPTION(BadJumpDestination());
case ReturnCode::OutOfGas: case evmjit::ReturnCode::OutOfGas:
BOOST_THROW_EXCEPTION(OutOfGas()); BOOST_THROW_EXCEPTION(OutOfGas());
case ReturnCode::StackUnderflow: case evmjit::ReturnCode::StackUnderflow: // FIXME: Remove support for detail errors
BOOST_THROW_EXCEPTION(StackUnderflow()); BOOST_THROW_EXCEPTION(StackUnderflow());
case ReturnCode::BadInstruction: case evmjit::ReturnCode::BadInstruction:
BOOST_THROW_EXCEPTION(BadInstruction()); BOOST_THROW_EXCEPTION(BadInstruction());
case ReturnCode::LinkerWorkaround: // never happens case evmjit::ReturnCode::LinkerWorkaround: // never happens
env_sload(); // but forces linker to include env_* JIT callback functions env_sload(); // but forces linker to include env_* JIT callback functions
break; break;
default: default:

2
libevmjit-cpp/JitVM.h

@ -18,7 +18,7 @@ private:
explicit JitVM(u256 _gas = 0) : VMFace(_gas) {} explicit JitVM(u256 _gas = 0) : VMFace(_gas) {}
jit::RuntimeData m_data; jit::RuntimeData m_data;
jit::ExecutionContext m_context; evmjit::ExecutionContext m_context;
std::unique_ptr<VMFace> m_fallbackVM; ///< VM used in case of input data rejected by JIT std::unique_ptr<VMFace> m_fallbackVM; ///< VM used in case of input data rejected by JIT
}; };

6
libevmjit/Cache.cpp

@ -16,10 +16,9 @@
namespace dev namespace dev
{ {
namespace eth namespace evmjit
{
namespace jit
{ {
using namespace eth::jit;
namespace namespace
{ {
@ -177,4 +176,3 @@ std::unique_ptr<llvm::MemoryBuffer> ObjectCache::getObject(llvm::Module const* _
} }
} }
}

5
libevmjit/Cache.h

@ -12,9 +12,7 @@ namespace llvm
namespace dev namespace dev
{ {
namespace eth namespace evmjit
{
namespace jit
{ {
class ExecutionEngineListener; class ExecutionEngineListener;
@ -57,4 +55,3 @@ public:
} }
} }
}

5
libevmjit/ExecStats.cpp

@ -8,9 +8,7 @@
namespace dev namespace dev
{ {
namespace eth namespace evmjit
{
namespace jit
{ {
void ExecStats::stateChanged(ExecState _state) void ExecStats::stateChanged(ExecState _state)
@ -95,4 +93,3 @@ StatsCollector::~StatsCollector()
} }
} }
}

6
libevmjit/ExecStats.h

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
#include <vector> #include <vector>
#include <string> #include <string>
#include <chrono> #include <chrono>
@ -8,9 +9,7 @@
namespace dev namespace dev
{ {
namespace eth namespace evmjit
{
namespace jit
{ {
class ExecStats : public ExecutionEngineListener class ExecStats : public ExecutionEngineListener
@ -42,4 +41,3 @@ public:
} }
} }
}

35
libevmjit/ExecutionEngine.cpp

@ -30,11 +30,8 @@
namespace dev namespace dev
{ {
namespace eth namespace evmjit
{ {
namespace jit
{
using evmjit::JIT;
namespace namespace
{ {
@ -56,7 +53,7 @@ std::string hash2str(i256 const& _hash)
return str; return str;
} }
void printVersion() void printVersion() // FIXME: Fix LLVM version parsing
{ {
std::cout << "Ethereum EVM JIT Compiler (http://github.com/ethereum/evmjit):\n" std::cout << "Ethereum EVM JIT Compiler (http://github.com/ethereum/evmjit):\n"
<< " EVMJIT version " << EVMJIT_VERSION << "\n" << " EVMJIT version " << EVMJIT_VERSION << "\n"
@ -90,19 +87,7 @@ void parseOptions()
cl::ParseEnvironmentOptions("evmjit", "EVMJIT", "Ethereum EVM JIT Compiler"); cl::ParseEnvironmentOptions("evmjit", "EVMJIT", "Ethereum EVM JIT Compiler");
} }
// FIXME: It is temporary, becaue ExecutionEngine.h is currently our public header std::unique_ptr<llvm::ExecutionEngine> init()
// and including llvm::ExecutionEngine there is not a good idea.
llvm::ExecutionEngine* g_ee = nullptr;
}
ExecutionEngine& ExecutionEngine::get()
{
static ExecutionEngine instance;
return instance;
}
ExecutionEngine::ExecutionEngine()
{ {
/// ExecutionEngine is created only once /// ExecutionEngine is created only once
@ -127,20 +112,23 @@ ExecutionEngine::ExecutionEngine()
builder.setEngineKind(llvm::EngineKind::JIT); builder.setEngineKind(llvm::EngineKind::JIT);
builder.setOptLevel(g_optimize ? llvm::CodeGenOpt::Default : llvm::CodeGenOpt::None); builder.setOptLevel(g_optimize ? llvm::CodeGenOpt::Default : llvm::CodeGenOpt::None);
g_ee = (builder.create()); auto ee = std::unique_ptr<llvm::ExecutionEngine>{builder.create()};
// TODO: Update cache listener // TODO: Update cache listener
g_ee->setObjectCache(Cache::init(g_cache, nullptr)); ee->setObjectCache(Cache::init(g_cache, nullptr));
// FIXME: Disabled during API changes // FIXME: Disabled during API changes
//if (preloadCache) //if (preloadCache)
// Cache::preload(*ee, funcCache); // Cache::preload(*ee, funcCache);
return ee;
} }
}
ReturnCode ExecutionEngine::run(ExecutionContext& _context) ReturnCode ExecutionEngine::run(ExecutionContext& _context)
{ {
ExecutionEngine::get(); // FIXME static auto s_ee = init();
std::unique_ptr<ExecStats> listener{new ExecStats}; std::unique_ptr<ExecStats> listener{new ExecStats};
listener->stateChanged(ExecState::Started); listener->stateChanged(ExecState::Started);
@ -175,9 +163,9 @@ ReturnCode ExecutionEngine::run(ExecutionContext& _context)
if (g_dump) if (g_dump)
module->dump(); module->dump();
g_ee->addModule(std::move(module)); s_ee->addModule(std::move(module));
listener->stateChanged(ExecState::CodeGen); listener->stateChanged(ExecState::CodeGen);
entryFuncPtr = (EntryFuncPtr)g_ee->getFunctionAddress(mainFuncName); entryFuncPtr = (EntryFuncPtr)s_ee->getFunctionAddress(mainFuncName);
if (!CHECK(entryFuncPtr)) if (!CHECK(entryFuncPtr))
return ReturnCode::LLVMLinkError; return ReturnCode::LLVMLinkError;
JIT::mapCode(codeHash, (void*)entryFuncPtr); // FIXME: Remove cast JIT::mapCode(codeHash, (void*)entryFuncPtr); // FIXME: Remove cast
@ -200,4 +188,3 @@ ReturnCode ExecutionEngine::run(ExecutionContext& _context)
} }
} }
}

20
libevmjit/ExecutionEngine.h

@ -1,20 +1,14 @@
#pragma once #pragma once
#include <memory>
#include "Common.h" #include "Common.h"
namespace dev namespace dev
{ {
namespace evmjit namespace evmjit
{ {
class ExecutionContext; class ExecutionContext;
}
namespace eth using namespace eth::jit;
{
namespace jit
{
using namespace evmjit; // FIXME
enum class ExecState enum class ExecState
{ {
@ -46,16 +40,8 @@ public:
class ExecutionEngine class ExecutionEngine
{ {
public: public:
ExecutionEngine(ExecutionEngine const&) = delete;
ExecutionEngine& operator=(ExecutionEngine const&) = delete;
EXPORT static ReturnCode run(ExecutionContext& _context); EXPORT static ReturnCode run(ExecutionContext& _context);
private:
ExecutionEngine();
static ExecutionEngine& get();
}; };
} }
} }
}

2
libevmjit/interface.cpp

@ -3,8 +3,8 @@
extern "C" extern "C"
{ {
using namespace dev::eth::jit; using namespace dev::eth::jit;
using namespace dev::evmjit;
EXPORT void* evmjit_create(RuntimeData* _data, Env* _env) noexcept EXPORT void* evmjit_create(RuntimeData* _data, Env* _env) noexcept
{ {

Loading…
Cancel
Save