Skip to content

Commit 370be08

Browse files
committed
Remove dependency on EVMC instructions
1 parent 698e019 commit 370be08

18 files changed

+240
-50
lines changed

lib/evmone/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ add_library(evmone
3030
tracing.hpp
3131
vm.cpp
3232
vm.hpp
33-
)
33+
instructions_opcodes.hpp)
3434
target_compile_features(evmone PUBLIC cxx_std_20)
3535
target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE evmc::instructions ethash::keccak)
3636
target_include_directories(evmone PUBLIC

lib/evmone/advanced_analysis.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#pragma once
55

66
#include "execution_state.hpp"
7+
#include "instructions_opcodes.hpp"
78
#include <evmc/evmc.hpp>
8-
#include <evmc/instructions.h>
99
#include <evmc/utils.h>
1010
#include <intx/intx.hpp>
1111
#include <array>

lib/evmone/advanced_instructions.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,42 @@ using namespace evmone::instr;
1414

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

24-
template <evmc_opcode Op, void CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
24+
template <Op Op, void CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
2525
inline void impl(AdvancedExecutionState& state) noexcept
2626
{
2727
CoreFn(state.stack.top_item, state);
2828
state.stack.top_item += instr::traits[Op].stack_height_change;
2929
}
3030

31-
template <evmc_opcode Op,
32-
evmc_status_code CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
31+
template <Op Op, evmc_status_code CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
3332
inline evmc_status_code impl(AdvancedExecutionState& state) noexcept
3433
{
3534
const auto status = CoreFn(state.stack.top_item, state);
3635
state.stack.top_item += instr::traits[Op].stack_height_change;
3736
return status;
3837
}
3938

40-
template <evmc_opcode Op, StopToken CoreFn() noexcept = core::impl<Op>>
39+
template <Op Op, StopToken CoreFn() noexcept = core::impl<Op>>
4140
inline StopToken impl(AdvancedExecutionState& /*state*/) noexcept
4241
{
4342
return CoreFn();
4443
}
4544

46-
template <evmc_opcode Op, StopToken CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
45+
template <Op Op, StopToken CoreFn(StackTop, ExecutionState&) noexcept = core::impl<Op>>
4746
inline StopToken impl(AdvancedExecutionState& state) noexcept
4847
{
4948
// Stack height adjustment may be omitted.
5049
return CoreFn(state.stack.top_item, state);
5150
}
5251

53-
template <evmc_opcode Op,
52+
template <Op Op,
5453
code_iterator CoreFn(StackTop, ExecutionState&, code_iterator) noexcept = core::impl<Op>>
5554
inline code_iterator impl(AdvancedExecutionState& state, code_iterator pos) noexcept
5655
{
@@ -180,7 +179,7 @@ const Instruction* op_push_full(const Instruction* instr, AdvancedExecutionState
180179
return ++instr;
181180
}
182181

183-
template <evmc_opcode Op>
182+
template <Op Op>
184183
const Instruction* op_call(const Instruction* instr, AdvancedExecutionState& state) noexcept
185184
{
186185
const auto gas_left_correction = state.current_block_cost - instr->arg.number;
@@ -196,7 +195,7 @@ const Instruction* op_call(const Instruction* instr, AdvancedExecutionState& sta
196195
return ++instr;
197196
}
198197

199-
template <evmc_opcode Op>
198+
template <Op Op>
200199
const Instruction* op_create(const Instruction* instr, AdvancedExecutionState& state) noexcept
201200
{
202201
const auto gas_left_correction = state.current_block_cost - instr->arg.number;

lib/evmone/baseline.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include "eof.hpp"
88
#include "execution_state.hpp"
99
#include "instructions.hpp"
10+
#include "instructions_opcodes.hpp"
1011
#include "vm.hpp"
11-
#include <evmc/instructions.h>
1212
#include <memory>
1313

1414
#ifdef NDEBUG
@@ -101,7 +101,7 @@ namespace
101101
/// The stack height is stack_top - stack_bottom.
102102
/// @return Status code with information which check has failed
103103
/// or EVMC_SUCCESS if everything is fine.
104-
template <evmc_opcode Op>
104+
template <Op Op>
105105
inline evmc_status_code check_requirements(const CostTable& cost_table, int64_t& gas_left,
106106
const uint256* stack_top, const uint256* stack_bottom) noexcept
107107
{
@@ -203,7 +203,7 @@ struct Position
203203
/// @}
204204

205205
/// A helper to invoke the instruction implementation of the given opcode Op.
206-
template <evmc_opcode Op>
206+
template <Op Op>
207207
[[release_inline]] inline Position invoke(const CostTable& cost_table, const uint256* stack_bottom,
208208
Position pos, ExecutionState& state) noexcept
209209
{

lib/evmone/instructions.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -851,14 +851,14 @@ inline evmc_status_code log(StackTop stack, ExecutionState& state) noexcept
851851
}
852852

853853

854-
template <evmc_opcode Op>
854+
template <Op Op>
855855
evmc_status_code call_impl(StackTop stack, ExecutionState& state) noexcept;
856856
inline constexpr auto call = call_impl<OP_CALL>;
857857
inline constexpr auto callcode = call_impl<OP_CALLCODE>;
858858
inline constexpr auto delegatecall = call_impl<OP_DELEGATECALL>;
859859
inline constexpr auto staticcall = call_impl<OP_STATICCALL>;
860860

861-
template <evmc_opcode Op>
861+
template <Op Op>
862862
evmc_status_code create_impl(StackTop stack, ExecutionState& state) noexcept;
863863
inline constexpr auto create = create_impl<OP_CREATE>;
864864
inline constexpr auto create2 = create_impl<OP_CREATE2>;
@@ -922,7 +922,7 @@ inline StopToken selfdestruct(StackTop stack, ExecutionState& state) noexcept
922922
/// implementing the instruction identified by the opcode.
923923
/// instr::impl<OP_DUP1>(/*...*/);
924924
/// The unspecialized template is invalid and should never to used.
925-
template <evmc_opcode Op>
925+
template <Op Op>
926926
inline constexpr auto impl = nullptr;
927927

928928
#undef ON_OPCODE_IDENTIFIER

lib/evmone/instructions_calls.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace evmone::instr::core
88
{
9-
template <evmc_opcode Op>
9+
template <Op Op>
1010
evmc_status_code call_impl(StackTop stack, ExecutionState& state) noexcept
1111
{
1212
static_assert(
@@ -110,7 +110,7 @@ template evmc_status_code call_impl<OP_DELEGATECALL>(
110110
template evmc_status_code call_impl<OP_CALLCODE>(StackTop stack, ExecutionState& state) noexcept;
111111

112112

113-
template <evmc_opcode Op>
113+
template <Op Op>
114114
evmc_status_code create_impl(StackTop stack, ExecutionState& state) noexcept
115115
{
116116
static_assert(Op == OP_CREATE || Op == OP_CREATE2);

lib/evmone/instructions_opcodes.hpp

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// evmone: Fast Ethereum Virtual Machine implementation
2+
// Copyright 2022 The evmone Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#pragma once
6+
7+
namespace evmone
8+
{
9+
enum Op
10+
{
11+
OP_STOP = 0x00,
12+
OP_ADD = 0x01,
13+
OP_MUL = 0x02,
14+
OP_SUB = 0x03,
15+
OP_DIV = 0x04,
16+
OP_SDIV = 0x05,
17+
OP_MOD = 0x06,
18+
OP_SMOD = 0x07,
19+
OP_ADDMOD = 0x08,
20+
OP_MULMOD = 0x09,
21+
OP_EXP = 0x0a,
22+
OP_SIGNEXTEND = 0x0b,
23+
24+
OP_LT = 0x10,
25+
OP_GT = 0x11,
26+
OP_SLT = 0x12,
27+
OP_SGT = 0x13,
28+
OP_EQ = 0x14,
29+
OP_ISZERO = 0x15,
30+
OP_AND = 0x16,
31+
OP_OR = 0x17,
32+
OP_XOR = 0x18,
33+
OP_NOT = 0x19,
34+
OP_BYTE = 0x1a,
35+
OP_SHL = 0x1b,
36+
OP_SHR = 0x1c,
37+
OP_SAR = 0x1d,
38+
39+
OP_KECCAK256 = 0x20,
40+
41+
OP_ADDRESS = 0x30,
42+
OP_BALANCE = 0x31,
43+
OP_ORIGIN = 0x32,
44+
OP_CALLER = 0x33,
45+
OP_CALLVALUE = 0x34,
46+
OP_CALLDATALOAD = 0x35,
47+
OP_CALLDATASIZE = 0x36,
48+
OP_CALLDATACOPY = 0x37,
49+
OP_CODESIZE = 0x38,
50+
OP_CODECOPY = 0x39,
51+
OP_GASPRICE = 0x3a,
52+
OP_EXTCODESIZE = 0x3b,
53+
OP_EXTCODECOPY = 0x3c,
54+
OP_RETURNDATASIZE = 0x3d,
55+
OP_RETURNDATACOPY = 0x3e,
56+
OP_EXTCODEHASH = 0x3f,
57+
58+
OP_BLOCKHASH = 0x40,
59+
OP_COINBASE = 0x41,
60+
OP_TIMESTAMP = 0x42,
61+
OP_NUMBER = 0x43,
62+
OP_PREVRANDAO = 0x44,
63+
OP_GASLIMIT = 0x45,
64+
OP_CHAINID = 0x46,
65+
OP_SELFBALANCE = 0x47,
66+
OP_BASEFEE = 0x48,
67+
68+
OP_POP = 0x50,
69+
OP_MLOAD = 0x51,
70+
OP_MSTORE = 0x52,
71+
OP_MSTORE8 = 0x53,
72+
OP_SLOAD = 0x54,
73+
OP_SSTORE = 0x55,
74+
OP_JUMP = 0x56,
75+
OP_JUMPI = 0x57,
76+
OP_PC = 0x58,
77+
OP_MSIZE = 0x59,
78+
OP_GAS = 0x5a,
79+
OP_JUMPDEST = 0x5b,
80+
81+
OP_PUSH0 = 0x5f,
82+
OP_PUSH1 = 0x60,
83+
OP_PUSH2 = 0x61,
84+
OP_PUSH3 = 0x62,
85+
OP_PUSH4 = 0x63,
86+
OP_PUSH5 = 0x64,
87+
OP_PUSH6 = 0x65,
88+
OP_PUSH7 = 0x66,
89+
OP_PUSH8 = 0x67,
90+
OP_PUSH9 = 0x68,
91+
OP_PUSH10 = 0x69,
92+
OP_PUSH11 = 0x6a,
93+
OP_PUSH12 = 0x6b,
94+
OP_PUSH13 = 0x6c,
95+
OP_PUSH14 = 0x6d,
96+
OP_PUSH15 = 0x6e,
97+
OP_PUSH16 = 0x6f,
98+
OP_PUSH17 = 0x70,
99+
OP_PUSH18 = 0x71,
100+
OP_PUSH19 = 0x72,
101+
OP_PUSH20 = 0x73,
102+
OP_PUSH21 = 0x74,
103+
OP_PUSH22 = 0x75,
104+
OP_PUSH23 = 0x76,
105+
OP_PUSH24 = 0x77,
106+
OP_PUSH25 = 0x78,
107+
OP_PUSH26 = 0x79,
108+
OP_PUSH27 = 0x7a,
109+
OP_PUSH28 = 0x7b,
110+
OP_PUSH29 = 0x7c,
111+
OP_PUSH30 = 0x7d,
112+
OP_PUSH31 = 0x7e,
113+
OP_PUSH32 = 0x7f,
114+
OP_DUP1 = 0x80,
115+
OP_DUP2 = 0x81,
116+
OP_DUP3 = 0x82,
117+
OP_DUP4 = 0x83,
118+
OP_DUP5 = 0x84,
119+
OP_DUP6 = 0x85,
120+
OP_DUP7 = 0x86,
121+
OP_DUP8 = 0x87,
122+
OP_DUP9 = 0x88,
123+
OP_DUP10 = 0x89,
124+
OP_DUP11 = 0x8a,
125+
OP_DUP12 = 0x8b,
126+
OP_DUP13 = 0x8c,
127+
OP_DUP14 = 0x8d,
128+
OP_DUP15 = 0x8e,
129+
OP_DUP16 = 0x8f,
130+
OP_SWAP1 = 0x90,
131+
OP_SWAP2 = 0x91,
132+
OP_SWAP3 = 0x92,
133+
OP_SWAP4 = 0x93,
134+
OP_SWAP5 = 0x94,
135+
OP_SWAP6 = 0x95,
136+
OP_SWAP7 = 0x96,
137+
OP_SWAP8 = 0x97,
138+
OP_SWAP9 = 0x98,
139+
OP_SWAP10 = 0x99,
140+
OP_SWAP11 = 0x9a,
141+
OP_SWAP12 = 0x9b,
142+
OP_SWAP13 = 0x9c,
143+
OP_SWAP14 = 0x9d,
144+
OP_SWAP15 = 0x9e,
145+
OP_SWAP16 = 0x9f,
146+
OP_LOG0 = 0xa0,
147+
OP_LOG1 = 0xa1,
148+
OP_LOG2 = 0xa2,
149+
OP_LOG3 = 0xa3,
150+
OP_LOG4 = 0xa4,
151+
152+
OP_CREATE = 0xf0,
153+
OP_CALL = 0xf1,
154+
OP_CALLCODE = 0xf2,
155+
OP_RETURN = 0xf3,
156+
OP_DELEGATECALL = 0xf4,
157+
OP_CREATE2 = 0xf5,
158+
159+
OP_STATICCALL = 0xfa,
160+
161+
OP_REVERT = 0xfd,
162+
OP_INVALID = 0xfe,
163+
OP_SELFDESTRUCT = 0xff
164+
};
165+
} // namespace evmone

lib/evmone/instructions_traits.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44
#pragma once
55

6-
#include <evmc/instructions.h>
6+
#include "instructions_opcodes.hpp"
77
#include <array>
88
#include <optional>
99

@@ -199,7 +199,7 @@ struct Traits
199199
/// Determines if an instruction has constant base gas cost across all revisions.
200200
/// Note that this is not true for instructions with constant base gas cost but
201201
/// not available in the first revision (e.g. SHL).
202-
inline constexpr bool has_const_gas_cost(evmc_opcode op) noexcept
202+
inline constexpr bool has_const_gas_cost(Op op) noexcept
203203
{
204204
const auto g = gas_costs[EVMC_FRONTIER][op];
205205
for (size_t r = EVMC_FRONTIER + 1; r <= EVMC_MAX_REVISION; ++r)

lib/evmone/tracing.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <evmc/hex.hpp>
99
#include <stack>
1010

11+
1112
namespace evmone
1213
{
1314
namespace

lib/evmone/tracing.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// SPDX-License-Identifier: Apache-2.0
44
#pragma once
55

6-
#include <evmc/instructions.h>
6+
#include <evmc/evmc.h>
7+
#include <evmc/utils.h>
78
#include <intx/intx.hpp>
89
#include <memory>
910
#include <ostream>

0 commit comments

Comments
 (0)