Skip to content

Commit

Permalink
Remove dependency on EVMC instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet authored and chfast committed Nov 30, 2022
1 parent 7d4d227 commit d15ac9b
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 50 deletions.
3 changes: 2 additions & 1 deletion lib/evmone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_library(evmone
eof.hpp
instructions.hpp
instructions_calls.cpp
instructions_opcodes.hpp
instructions_storage.cpp
instructions_traits.hpp
instructions_xmacro.hpp
Expand All @@ -32,7 +33,7 @@ add_library(evmone
vm.hpp
)
target_compile_features(evmone PUBLIC cxx_std_20)
target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE evmc::instructions ethash::keccak)
target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE ethash::keccak)
target_include_directories(evmone PUBLIC
$<BUILD_INTERFACE:${include_dir}>$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/advanced_analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#pragma once

#include "execution_state.hpp"
#include "instructions_opcodes.hpp"
#include <evmc/evmc.hpp>
#include <evmc/instructions.h>
#include <evmc/utils.h>
#include <intx/intx.hpp>
#include <array>
Expand Down
17 changes: 8 additions & 9 deletions lib/evmone/advanced_instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,42 @@ using namespace evmone::instr;

/// Instruction implementations - "core" instruction + stack height adjustment.
/// @{
template <evmc_opcode Op, void CoreFn(StackTop) noexcept = core::impl<Op>>
template <Opcode Op, void CoreFn(StackTop) noexcept = core::impl<Op>>
inline void impl(AdvancedExecutionState& state) noexcept
{
CoreFn(state.stack.top_item);
state.stack.top_item += instr::traits[Op].stack_height_change;
}

template <evmc_opcode Op, void CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
template <Opcode Op, void CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
inline void impl(AdvancedExecutionState& state) noexcept
{
CoreFn(state.stack.top_item, state);
state.stack.top_item += instr::traits[Op].stack_height_change;
}

template <evmc_opcode Op,
evmc_status_code CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
template <Opcode Op, evmc_status_code CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
inline evmc_status_code impl(AdvancedExecutionState& state) noexcept
{
const auto status = CoreFn(state.stack.top_item, state);
state.stack.top_item += instr::traits[Op].stack_height_change;
return status;
}

template <evmc_opcode Op, StopToken CoreFn() noexcept = core::impl<Op>>
template <Opcode Op, StopToken CoreFn() noexcept = core::impl<Op>>
inline StopToken impl(AdvancedExecutionState& /*state*/) noexcept
{
return CoreFn();
}

template <evmc_opcode Op, StopToken CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
template <Opcode Op, StopToken CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
inline StopToken impl(AdvancedExecutionState& state) noexcept
{
// Stack height adjustment may be omitted.
return CoreFn(state.stack.top_item, state);
}

template <evmc_opcode Op,
template <Opcode Op,
code_iterator CoreFn(StackTop, ExecutionState&, code_iterator) noexcept = core::impl<Op>>
inline code_iterator impl(AdvancedExecutionState& state, code_iterator pos) noexcept
{
Expand Down Expand Up @@ -180,7 +179,7 @@ const Instruction* op_push_full(const Instruction* instr, AdvancedExecutionState
return ++instr;
}

template <evmc_opcode Op>
template <Opcode Op>
const Instruction* op_call(const Instruction* instr, AdvancedExecutionState& state) noexcept
{
const auto gas_left_correction = state.current_block_cost - instr->arg.number;
Expand All @@ -196,7 +195,7 @@ const Instruction* op_call(const Instruction* instr, AdvancedExecutionState& sta
return ++instr;
}

template <evmc_opcode Op>
template <Opcode Op>
const Instruction* op_create(const Instruction* instr, AdvancedExecutionState& state) noexcept
{
const auto gas_left_correction = state.current_block_cost - instr->arg.number;
Expand Down
5 changes: 2 additions & 3 deletions lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "execution_state.hpp"
#include "instructions.hpp"
#include "vm.hpp"
#include <evmc/instructions.h>
#include <memory>

#ifdef NDEBUG
Expand Down Expand Up @@ -101,7 +100,7 @@ namespace
/// The stack height is stack_top - stack_bottom.
/// @return Status code with information which check has failed
/// or EVMC_SUCCESS if everything is fine.
template <evmc_opcode Op>
template <Opcode Op>
inline evmc_status_code check_requirements(const CostTable& cost_table, int64_t& gas_left,
const uint256* stack_top, const uint256* stack_bottom) noexcept
{
Expand Down Expand Up @@ -203,7 +202,7 @@ struct Position
/// @}

/// A helper to invoke the instruction implementation of the given opcode Op.
template <evmc_opcode Op>
template <Opcode Op>
[[release_inline]] inline Position invoke(const CostTable& cost_table, const uint256* stack_bottom,
Position pos, ExecutionState& state) noexcept
{
Expand Down
6 changes: 3 additions & 3 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,14 +851,14 @@ inline evmc_status_code log(StackTop stack, ExecutionState& state) noexcept
}


template <evmc_opcode Op>
template <Opcode Op>
evmc_status_code call_impl(StackTop stack, ExecutionState& state) noexcept;
inline constexpr auto call = call_impl<OP_CALL>;
inline constexpr auto callcode = call_impl<OP_CALLCODE>;
inline constexpr auto delegatecall = call_impl<OP_DELEGATECALL>;
inline constexpr auto staticcall = call_impl<OP_STATICCALL>;

template <evmc_opcode Op>
template <Opcode Op>
evmc_status_code create_impl(StackTop stack, ExecutionState& state) noexcept;
inline constexpr auto create = create_impl<OP_CREATE>;
inline constexpr auto create2 = create_impl<OP_CREATE2>;
Expand Down Expand Up @@ -922,7 +922,7 @@ inline StopToken selfdestruct(StackTop stack, ExecutionState& state) noexcept
/// implementing the instruction identified by the opcode.
/// instr::impl<OP_DUP1>(/*...*/);
/// The unspecialized template is invalid and should never to used.
template <evmc_opcode Op>
template <Opcode Op>
inline constexpr auto impl = nullptr;

#undef ON_OPCODE_IDENTIFIER
Expand Down
4 changes: 2 additions & 2 deletions lib/evmone/instructions_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace evmone::instr::core
{
template <evmc_opcode Op>
template <Opcode Op>
evmc_status_code call_impl(StackTop stack, ExecutionState& state) noexcept
{
static_assert(
Expand Down Expand Up @@ -110,7 +110,7 @@ template evmc_status_code call_impl<OP_DELEGATECALL>(
template evmc_status_code call_impl<OP_CALLCODE>(StackTop stack, ExecutionState& state) noexcept;


template <evmc_opcode Op>
template <Opcode Op>
evmc_status_code create_impl(StackTop stack, ExecutionState& state) noexcept
{
static_assert(Op == OP_CREATE || Op == OP_CREATE2);
Expand Down
168 changes: 168 additions & 0 deletions lib/evmone/instructions_opcodes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2022 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#pragma once

/// The list of EVM opcodes from every EVM revision.
///
/// TODO: Change to `enum class Opcode: uint8_t`.
namespace evmone
{
enum Opcode
{
OP_STOP = 0x00,
OP_ADD = 0x01,
OP_MUL = 0x02,
OP_SUB = 0x03,
OP_DIV = 0x04,
OP_SDIV = 0x05,
OP_MOD = 0x06,
OP_SMOD = 0x07,
OP_ADDMOD = 0x08,
OP_MULMOD = 0x09,
OP_EXP = 0x0a,
OP_SIGNEXTEND = 0x0b,

OP_LT = 0x10,
OP_GT = 0x11,
OP_SLT = 0x12,
OP_SGT = 0x13,
OP_EQ = 0x14,
OP_ISZERO = 0x15,
OP_AND = 0x16,
OP_OR = 0x17,
OP_XOR = 0x18,
OP_NOT = 0x19,
OP_BYTE = 0x1a,
OP_SHL = 0x1b,
OP_SHR = 0x1c,
OP_SAR = 0x1d,

OP_KECCAK256 = 0x20,

OP_ADDRESS = 0x30,
OP_BALANCE = 0x31,
OP_ORIGIN = 0x32,
OP_CALLER = 0x33,
OP_CALLVALUE = 0x34,
OP_CALLDATALOAD = 0x35,
OP_CALLDATASIZE = 0x36,
OP_CALLDATACOPY = 0x37,
OP_CODESIZE = 0x38,
OP_CODECOPY = 0x39,
OP_GASPRICE = 0x3a,
OP_EXTCODESIZE = 0x3b,
OP_EXTCODECOPY = 0x3c,
OP_RETURNDATASIZE = 0x3d,
OP_RETURNDATACOPY = 0x3e,
OP_EXTCODEHASH = 0x3f,

OP_BLOCKHASH = 0x40,
OP_COINBASE = 0x41,
OP_TIMESTAMP = 0x42,
OP_NUMBER = 0x43,
OP_PREVRANDAO = 0x44,
OP_GASLIMIT = 0x45,
OP_CHAINID = 0x46,
OP_SELFBALANCE = 0x47,
OP_BASEFEE = 0x48,

OP_POP = 0x50,
OP_MLOAD = 0x51,
OP_MSTORE = 0x52,
OP_MSTORE8 = 0x53,
OP_SLOAD = 0x54,
OP_SSTORE = 0x55,
OP_JUMP = 0x56,
OP_JUMPI = 0x57,
OP_PC = 0x58,
OP_MSIZE = 0x59,
OP_GAS = 0x5a,
OP_JUMPDEST = 0x5b,

OP_PUSH0 = 0x5f,
OP_PUSH1 = 0x60,
OP_PUSH2 = 0x61,
OP_PUSH3 = 0x62,
OP_PUSH4 = 0x63,
OP_PUSH5 = 0x64,
OP_PUSH6 = 0x65,
OP_PUSH7 = 0x66,
OP_PUSH8 = 0x67,
OP_PUSH9 = 0x68,
OP_PUSH10 = 0x69,
OP_PUSH11 = 0x6a,
OP_PUSH12 = 0x6b,
OP_PUSH13 = 0x6c,
OP_PUSH14 = 0x6d,
OP_PUSH15 = 0x6e,
OP_PUSH16 = 0x6f,
OP_PUSH17 = 0x70,
OP_PUSH18 = 0x71,
OP_PUSH19 = 0x72,
OP_PUSH20 = 0x73,
OP_PUSH21 = 0x74,
OP_PUSH22 = 0x75,
OP_PUSH23 = 0x76,
OP_PUSH24 = 0x77,
OP_PUSH25 = 0x78,
OP_PUSH26 = 0x79,
OP_PUSH27 = 0x7a,
OP_PUSH28 = 0x7b,
OP_PUSH29 = 0x7c,
OP_PUSH30 = 0x7d,
OP_PUSH31 = 0x7e,
OP_PUSH32 = 0x7f,
OP_DUP1 = 0x80,
OP_DUP2 = 0x81,
OP_DUP3 = 0x82,
OP_DUP4 = 0x83,
OP_DUP5 = 0x84,
OP_DUP6 = 0x85,
OP_DUP7 = 0x86,
OP_DUP8 = 0x87,
OP_DUP9 = 0x88,
OP_DUP10 = 0x89,
OP_DUP11 = 0x8a,
OP_DUP12 = 0x8b,
OP_DUP13 = 0x8c,
OP_DUP14 = 0x8d,
OP_DUP15 = 0x8e,
OP_DUP16 = 0x8f,
OP_SWAP1 = 0x90,
OP_SWAP2 = 0x91,
OP_SWAP3 = 0x92,
OP_SWAP4 = 0x93,
OP_SWAP5 = 0x94,
OP_SWAP6 = 0x95,
OP_SWAP7 = 0x96,
OP_SWAP8 = 0x97,
OP_SWAP9 = 0x98,
OP_SWAP10 = 0x99,
OP_SWAP11 = 0x9a,
OP_SWAP12 = 0x9b,
OP_SWAP13 = 0x9c,
OP_SWAP14 = 0x9d,
OP_SWAP15 = 0x9e,
OP_SWAP16 = 0x9f,
OP_LOG0 = 0xa0,
OP_LOG1 = 0xa1,
OP_LOG2 = 0xa2,
OP_LOG3 = 0xa3,
OP_LOG4 = 0xa4,

OP_CREATE = 0xf0,
OP_CALL = 0xf1,
OP_CALLCODE = 0xf2,
OP_RETURN = 0xf3,
OP_DELEGATECALL = 0xf4,
OP_CREATE2 = 0xf5,

OP_STATICCALL = 0xfa,

OP_REVERT = 0xfd,
OP_INVALID = 0xfe,
OP_SELFDESTRUCT = 0xff
};
} // namespace evmone
4 changes: 2 additions & 2 deletions lib/evmone/instructions_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <evmc/instructions.h>
#include "instructions_opcodes.hpp"
#include <array>
#include <optional>

Expand Down Expand Up @@ -199,7 +199,7 @@ struct Traits
/// Determines if an instruction has constant base gas cost across all revisions.
/// Note that this is not true for instructions with constant base gas cost but
/// not available in the first revision (e.g. SHL).
inline constexpr bool has_const_gas_cost(evmc_opcode op) noexcept
inline constexpr bool has_const_gas_cost(Opcode op) noexcept
{
const auto g = gas_costs[EVMC_FRONTIER][op];
for (size_t r = EVMC_FRONTIER + 1; r <= EVMC_MAX_REVISION; ++r)
Expand Down
3 changes: 2 additions & 1 deletion lib/evmone/tracing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <evmc/instructions.h>
#include <evmc/evmc.h>
#include <evmc/utils.h>
#include <intx/intx.hpp>
#include <memory>
#include <ostream>
Expand Down
Loading

0 comments on commit d15ac9b

Please sign in to comment.