Skip to content

Commit

Permalink
Merge pull request #318 from ethereum/vm_object
Browse files Browse the repository at this point in the history
Add evmone::VM class
  • Loading branch information
chfast authored May 12, 2021
2 parents 5c92a50 + 2a71ef7 commit 0647358
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/evmone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ add_library(evmone
analysis.hpp
baseline.cpp
baseline.hpp
evmone.cpp
execution.cpp
execution.hpp
instruction_traits.hpp
Expand All @@ -22,6 +21,7 @@ add_library(evmone
instructions_calls.cpp
limits.hpp
opcodes_helpers.h
vm.cpp
)
target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE evmc::instructions ethash::keccak)
target_include_directories(evmone PUBLIC
Expand All @@ -42,6 +42,6 @@ if(NOT SANITIZE)
target_link_options(evmone PRIVATE $<$<PLATFORM_ID:Linux>:LINKER:--no-undefined>)
endif()

set_source_files_properties(evmone.cpp PROPERTIES COMPILE_DEFINITIONS PROJECT_VERSION="${PROJECT_VERSION}")
set_source_files_properties(vm.cpp PROPERTIES COMPILE_DEFINITIONS PROJECT_VERSION="${PROJECT_VERSION}")

add_standalone_library(evmone)
8 changes: 5 additions & 3 deletions lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "baseline.hpp"
#include "execution_state.hpp"
#include "instructions.hpp"
#include "vm.hpp"
#include <evmc/instructions.h>
#include <memory>

Expand Down Expand Up @@ -96,15 +97,16 @@ inline evmc_status_code check_requirements(const char* const* instruction_names,
}
} // namespace

evmc_result execute(evmc_vm* /*vm*/, const evmc_host_interface* host, evmc_host_context* ctx,
evmc_result execute(evmc_vm* c_vm, const evmc_host_interface* host, evmc_host_context* ctx,
evmc_revision rev, const evmc_message* msg, const uint8_t* code, size_t code_size) noexcept
{
auto vm = static_cast<VM*>(c_vm);
const auto jumpdest_map = analyze(code, code_size);
auto state = std::make_unique<ExecutionState>(*msg, rev, *host, ctx, code, code_size);
return execute(*state, jumpdest_map);
return execute(*vm, *state, jumpdest_map);
}

evmc_result execute(ExecutionState& state, const CodeAnalysis& analysis) noexcept
evmc_result execute(const VM& /*vm*/, ExecutionState& state, const CodeAnalysis& analysis) noexcept
{
const auto rev = state.rev;
const auto code = state.code.data();
Expand Down
12 changes: 9 additions & 3 deletions lib/evmone/baseline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#include <evmc/utils.h>
#include <vector>

namespace evmone::baseline
namespace evmone
{
class VM;

namespace baseline
{
struct CodeAnalysis
{
Expand All @@ -25,5 +29,7 @@ evmc_result execute(evmc_vm* vm, const evmc_host_interface* host, evmc_host_cont
evmc_revision rev, const evmc_message* msg, const uint8_t* code, size_t code_size) noexcept;

/// Executes in Baseline interpreter on the given external and initialized state.
evmc_result execute(ExecutionState& state, const CodeAnalysis& analysis) noexcept;
} // namespace evmone::baseline
evmc_result execute(const VM&, ExecutionState& state, const CodeAnalysis& analysis) noexcept;

} // namespace baseline
} // namespace evmone
29 changes: 18 additions & 11 deletions lib/evmone/evmone.cpp → lib/evmone/vm.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2018-2020 The evmone Authors.
// Copyright 2018 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

/// @file
/// EVMC instance and entry point of evmone is defined here.
/// The file name matches the evmone.h public header.
/// EVMC instance (class VM) and entry point of evmone is defined here.

#include "vm.hpp"
#include "baseline.hpp"
#include "execution.hpp"
#include <evmone/evmone.h>
Expand All @@ -16,8 +16,8 @@ namespace
{
void destroy(evmc_vm* vm) noexcept
{
// TODO: Mark function with [[gnu:nonnull]] or add CHECK().
delete vm;
assert(vm != nullptr);
delete static_cast<VM*>(vm);
}

constexpr evmc_capabilities_flagset get_capabilities(evmc_vm* /*vm*/) noexcept
Expand All @@ -43,20 +43,27 @@ evmc_set_option_result set_option(evmc_vm* vm, char const* name, char const* val
}
return EVMC_SET_OPTION_INVALID_NAME;
}

} // namespace
} // namespace evmone

extern "C" {
EVMC_EXPORT evmc_vm* evmc_create_evmone() noexcept
{
return new evmc_vm{

inline constexpr VM::VM() noexcept
: evmc_vm{
EVMC_ABI_VERSION,
"evmone",
PROJECT_VERSION,
evmone::destroy,
evmone::execute,
evmone::get_capabilities,
evmone::set_option,
};
}
{}

} // namespace evmone

extern "C" {
EVMC_EXPORT evmc_vm* evmc_create_evmone() noexcept
{
return new evmone::VM{};
}
}
16 changes: 16 additions & 0 deletions lib/evmone/vm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2021 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <evmc/evmc.h>

namespace evmone
{
/// The evmone EVMC instance.
class VM : public evmc_vm
{
public:
inline constexpr VM() noexcept;
};
} // namespace evmone

0 comments on commit 0647358

Please sign in to comment.