Skip to content

Commit

Permalink
Merge pull request #277 from ethereum/dup_swap_refactor
Browse files Browse the repository at this point in the history
Refactor DUP and SWAP instruction implementations
  • Loading branch information
chfast authored Dec 18, 2020
2 parents a9d81e1 + deeb33b commit 578ce07
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
66 changes: 33 additions & 33 deletions lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,39 +263,39 @@ constexpr std::array<instruction_exec_fn, 256> instruction_implementations = [](
for (auto op = size_t{OP_PUSH9}; op <= OP_PUSH32; ++op)
table[op] = op_push_full;

table[OP_DUP1] = op<dup<OP_DUP1>>;
table[OP_DUP2] = op<dup<OP_DUP2>>;
table[OP_DUP3] = op<dup<OP_DUP3>>;
table[OP_DUP4] = op<dup<OP_DUP4>>;
table[OP_DUP5] = op<dup<OP_DUP5>>;
table[OP_DUP6] = op<dup<OP_DUP6>>;
table[OP_DUP7] = op<dup<OP_DUP7>>;
table[OP_DUP8] = op<dup<OP_DUP8>>;
table[OP_DUP9] = op<dup<OP_DUP9>>;
table[OP_DUP10] = op<dup<OP_DUP10>>;
table[OP_DUP11] = op<dup<OP_DUP11>>;
table[OP_DUP12] = op<dup<OP_DUP12>>;
table[OP_DUP13] = op<dup<OP_DUP13>>;
table[OP_DUP14] = op<dup<OP_DUP14>>;
table[OP_DUP15] = op<dup<OP_DUP15>>;
table[OP_DUP16] = op<dup<OP_DUP16>>;

table[OP_SWAP1] = op<swap<OP_SWAP1>>;
table[OP_SWAP2] = op<swap<OP_SWAP2>>;
table[OP_SWAP3] = op<swap<OP_SWAP3>>;
table[OP_SWAP4] = op<swap<OP_SWAP4>>;
table[OP_SWAP5] = op<swap<OP_SWAP5>>;
table[OP_SWAP6] = op<swap<OP_SWAP6>>;
table[OP_SWAP7] = op<swap<OP_SWAP7>>;
table[OP_SWAP8] = op<swap<OP_SWAP8>>;
table[OP_SWAP9] = op<swap<OP_SWAP9>>;
table[OP_SWAP10] = op<swap<OP_SWAP10>>;
table[OP_SWAP11] = op<swap<OP_SWAP11>>;
table[OP_SWAP12] = op<swap<OP_SWAP12>>;
table[OP_SWAP13] = op<swap<OP_SWAP13>>;
table[OP_SWAP14] = op<swap<OP_SWAP14>>;
table[OP_SWAP15] = op<swap<OP_SWAP15>>;
table[OP_SWAP16] = op<swap<OP_SWAP16>>;
table[OP_DUP1] = op<dup<1>>;
table[OP_DUP2] = op<dup<2>>;
table[OP_DUP3] = op<dup<3>>;
table[OP_DUP4] = op<dup<4>>;
table[OP_DUP5] = op<dup<5>>;
table[OP_DUP6] = op<dup<6>>;
table[OP_DUP7] = op<dup<7>>;
table[OP_DUP8] = op<dup<8>>;
table[OP_DUP9] = op<dup<9>>;
table[OP_DUP10] = op<dup<10>>;
table[OP_DUP11] = op<dup<11>>;
table[OP_DUP12] = op<dup<12>>;
table[OP_DUP13] = op<dup<13>>;
table[OP_DUP14] = op<dup<14>>;
table[OP_DUP15] = op<dup<15>>;
table[OP_DUP16] = op<dup<16>>;

table[OP_SWAP1] = op<swap<1>>;
table[OP_SWAP2] = op<swap<2>>;
table[OP_SWAP3] = op<swap<3>>;
table[OP_SWAP4] = op<swap<4>>;
table[OP_SWAP5] = op<swap<5>>;
table[OP_SWAP6] = op<swap<6>>;
table[OP_SWAP7] = op<swap<7>>;
table[OP_SWAP8] = op<swap<8>>;
table[OP_SWAP9] = op<swap<9>>;
table[OP_SWAP10] = op<swap<10>>;
table[OP_SWAP11] = op<swap<11>>;
table[OP_SWAP12] = op<swap<12>>;
table[OP_SWAP13] = op<swap<13>>;
table[OP_SWAP14] = op<swap<14>>;
table[OP_SWAP15] = op<swap<15>>;
table[OP_SWAP16] = op<swap<16>>;

table[OP_LOG0] = op_log<OP_LOG0>;
table[OP_LOG1] = op_log<OP_LOG1>;
Expand Down
17 changes: 10 additions & 7 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,19 +619,22 @@ inline void msize(ExecutionState& state) noexcept
state.stack.push(state.memory.size());
}


template <evmc_opcode DupOp>
/// DUP instruction implementation.
/// @tparam N The number as in the instruction definition, e.g. DUP3 is dup<3>.
template <size_t N>
inline void dup(evm_stack& stack) noexcept
{
constexpr auto index = DupOp - OP_DUP1;
stack.push(stack[index]);
static_assert(N >= 1 && N <= 16);
stack.push(stack[N - 1]);
}

template <evmc_opcode SwapOp>
/// SWAP instruction implementation.
/// @tparam N The number as in the instruction definition, e.g. SWAP3 is swap<3>.
template <size_t N>
inline void swap(evm_stack& stack) noexcept
{
constexpr auto index = SwapOp - OP_SWAP1 + 1;
std::swap(stack.top(), stack[index]);
static_assert(N >= 1 && N <= 16);
std::swap(stack.top(), stack[N]);
}


Expand Down

0 comments on commit 578ce07

Please sign in to comment.