Skip to content

Commit 5277fd0

Browse files
committed
Remove dependency on EVMC instructions
1 parent 698e019 commit 5277fd0

16 files changed

+227
-50
lines changed

lib/evmone/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_library(evmone
2222
eof.hpp
2323
instructions.hpp
2424
instructions_calls.cpp
25+
instructions_opcodes.hpp
2526
instructions_storage.cpp
2627
instructions_traits.hpp
2728
instructions_xmacro.hpp

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 <Opcode 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 <Opcode 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 <Opcode 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 <Opcode 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 <Opcode 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 <Opcode 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 <Opcode 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 <Opcode 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

+4-4
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 <Opcode 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
{
@@ -202,8 +202,8 @@ struct Position
202202
}
203203
/// @}
204204

205-
/// A helper to invoke the instruction implementation of the given opcode Op.
206-
template <evmc_opcode Op>
205+
/// A helper to invoke the instruction implementation of the given opcode Opcode.
206+
template <Opcode 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

+4-4
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 <Opcode 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 <Opcode 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>;
@@ -918,11 +918,11 @@ inline StopToken selfdestruct(StackTop stack, ExecutionState& state) noexcept
918918

919919
/// Maps an opcode to the instruction implementation.
920920
///
921-
/// The set of template specializations which map opcodes `Op` to the function
921+
/// The set of template specializations which map opcodes `Opcode` to the function
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 <Opcode 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 <Opcode 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 <Opcode 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

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