Skip to content

Commit

Permalink
Implement DATALOADI instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Mar 18, 2023
1 parent a464dd1 commit 336a205
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/evmone/advanced_instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ constexpr std::array<instruction_exec_fn, 256> instruction_implementations = [](
table[OP_RJUMPV] = op_undefined;
table[OP_CALLF] = op_undefined;
table[OP_RETF] = op_undefined;
table[OP_DATALOADI] = op_undefined;

table[OP_DUPN] = op_undefined;
table[OP_SWAPN] = op_undefined;
Expand Down
13 changes: 13 additions & 0 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,19 @@ inline evmc_status_code dataload(StackTop stack, ExecutionState& state) noexcept
return EVMC_SUCCESS;
}

inline code_iterator dataloadi(StackTop stack, ExecutionState& state, code_iterator pos) noexcept
{
const auto index = read_uint16_be(&pos[1]);

const auto begin = static_cast<size_t>(index * 32);
uint8_t data[32] = {};
for (size_t i = 0; i < 32; ++i)
data[i] = state.data[begin + i];

stack.push(intx::be::load<uint256>(data));
return pos + 3;
}

inline evmc_status_code datacopy(StackTop stack, ExecutionState& state) noexcept
{
const auto& mem_index = stack.pop();
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 @@ -166,6 +166,7 @@ enum Opcode : uint8_t
OP_DATALOAD = 0xb7,
OP_DATASIZE = 0xb8,
OP_DATACOPY = 0xb9,
OP_DATALOADI = 0xba,

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 @@ -174,6 +174,7 @@ constexpr inline GasCostTable gas_costs = []() noexcept {
table[EVMC_CANCUN][OP_DATALOAD] = 3;
table[EVMC_CANCUN][OP_DATASIZE] = 2;
table[EVMC_CANCUN][OP_DATACOPY] = 3;
table[EVMC_CANCUN][OP_DATALOADI] = 2;

table[EVMC_PRAGUE] = table[EVMC_CANCUN];

Expand Down Expand Up @@ -381,6 +382,7 @@ constexpr inline std::array<Traits, 256> traits = []() noexcept {
table[OP_DATALOAD] = {"DATALOAD", 0, false, 1, 0, EVMC_CANCUN};
table[OP_DATASIZE] = {"DATASIZE", 0, false, 0, 1, EVMC_CANCUN};
table[OP_DATACOPY] = {"DATACOPY", 0, false, 3, -3, EVMC_CANCUN};
table[OP_DATALOADI] = {"DATALOADI", 2, false, 0, 1, EVMC_CANCUN};

table[OP_CREATE] = {"CREATE", 0, false, 3, -2, EVMC_FRONTIER};
table[OP_CALL] = {"CALL", 0, false, 7, -6, EVMC_FRONTIER};
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/instructions_xmacro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
ON_OPCODE_IDENTIFIER(OP_DATALOAD, dataload) \
ON_OPCODE_IDENTIFIER(OP_DATASIZE, datasize) \
ON_OPCODE_IDENTIFIER(OP_DATACOPY, datacopy) \
ON_OPCODE_UNDEFINED(0xba) \
ON_OPCODE_IDENTIFIER(OP_DATALOADI, dataloadi) \
ON_OPCODE_UNDEFINED(0xbb) \
ON_OPCODE_UNDEFINED(0xbc) \
ON_OPCODE_UNDEFINED(0xbd) \
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ TEST(eof_validation, EOF1_undefined_opcodes)
// a separate test
if ((opcode >= OP_PUSH1 && opcode <= OP_PUSH32) || opcode == OP_DUPN ||
opcode == OP_SWAPN || opcode == OP_RJUMP || opcode == OP_RJUMPI || opcode == OP_CALLF ||
opcode == OP_RJUMPV)
opcode == OP_RJUMPV || opcode == OP_DATALOADI)
continue;
// These opcodes are deprecated since Cancun.
// gas_cost table current implementation does not allow to undef instructions.
Expand Down
3 changes: 3 additions & 0 deletions test/unittests/instructions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ constexpr void validate_traits_of() noexcept
static_assert(tr.immediate_size == 2);
else if constexpr (Op == OP_DUPN || Op == OP_SWAPN)
static_assert(tr.immediate_size == 1);
else if constexpr (Op == OP_DATALOADI)
static_assert(tr.immediate_size == 2);
else
static_assert(tr.immediate_size == 0); // Including RJUMPV.

Expand Down Expand Up @@ -113,6 +115,7 @@ constexpr bool instruction_only_in_evmone(evmc_revision rev, Opcode op) noexcept
case OP_DATALOAD:
case OP_DATASIZE:
case OP_DATACOPY:
case OP_DATALOADI:
return true;
default:
return false;
Expand Down

0 comments on commit 336a205

Please sign in to comment.