Skip to content

Commit

Permalink
Implement MCOPY
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Apr 29, 2023
1 parent 100c8f9 commit 3cf1aa0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,29 @@ inline code_iterator swapn(StackTop stack, ExecutionState& state, code_iterator
return pos + 2;
}

inline Result mcopy(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
{
const auto& dst_index = stack.pop();
const auto& src_index = stack.pop();
const auto& size = stack.pop();

if (!check_memory(gas_left, state.memory, std::max(dst_index, src_index), size))
return {EVMC_OUT_OF_GAS, gas_left};

auto dst = static_cast<size_t>(dst_index);
auto src = static_cast<size_t>(src_index);
auto copy_size = static_cast<size_t>(size);

const auto copy_cost = num_words(copy_size) * 3;
if ((gas_left -= copy_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};

if (copy_size > 0)
std::memcpy(&state.memory[dst], &state.memory[src], copy_size);

return {EVMC_SUCCESS, gas_left};
}

template <size_t NumTopics>
inline Result log(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
{
Expand Down
1 change: 1 addition & 0 deletions lib/evmone/instructions_opcodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ enum Opcode : uint8_t

OP_DUPN = 0xb5,
OP_SWAPN = 0xb6,
OP_MCOPY = 0xb7,

OP_CREATE = 0xf0,
OP_CALL = 0xf1,
Expand Down
2 changes: 2 additions & 0 deletions lib/evmone/instructions_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ constexpr inline GasCostTable gas_costs = []() noexcept {
table[EVMC_SHANGHAI][OP_PUSH0] = 2;

table[EVMC_CANCUN] = table[EVMC_SHANGHAI];
table[EVMC_CANCUN][OP_MCOPY] = 3;
table[EVMC_CANCUN][OP_DUPN] = 3;
table[EVMC_CANCUN][OP_SWAPN] = 3;
table[EVMC_CANCUN][OP_RJUMP] = 2;
Expand Down Expand Up @@ -375,6 +376,7 @@ constexpr inline std::array<Traits, 256> traits = []() noexcept {

table[OP_DUPN] = {"DUPN", 1, false, 0, 1, EVMC_CANCUN};
table[OP_SWAPN] = {"SWAPN", 1, false, 0, 0, EVMC_CANCUN};
table[OP_MCOPY] = {"MCOPY", 0, false, 3, -3, EVMC_CANCUN};

table[OP_CREATE] = {"CREATE", 0, false, 3, -2, EVMC_FRONTIER};
table[OP_CALL] = {"CALL", 0, false, 7, -6, EVMC_FRONTIER};
Expand Down

0 comments on commit 3cf1aa0

Please sign in to comment.