Skip to content

Commit

Permalink
Opcode/Machine: handle MCOPY instruction (EIP-5656)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodibozman committed Mar 1, 2024
1 parent b2ee4c9 commit 8eff41c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/eval/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ pub fn mstore8(state: &mut Machine) -> Control {
}
}

#[inline]
pub fn mcopy(state: &mut Machine) -> Control {
pop_u256!(state, dst, src, len);

try_or_fail!(state.memory.resize_offset(dst, len));

if len == U256::zero() {
return Control::Continue(1);
}

let src_usize = as_usize_or_fail!(src);
let len_usize = as_usize_or_fail!(len);
let data = state.memory.get(src_usize, len_usize);

// NB: using `set` would be more straighforward but it lacks
// of internal checks.
match state.memory.copy_large(dst, U256::zero(), len, &data) {
Ok(()) => Control::Continue(1),
Err(e) => Control::Exit(e.into()),
}
}

#[inline]
pub fn jump(state: &mut Machine) -> Control {
pop_u256!(state, dest);
Expand Down
5 changes: 5 additions & 0 deletions core/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ fn eval_mstore8(state: &mut Machine, _opcode: Opcode, _position: usize) -> Contr
self::misc::mstore8(state)
}

fn eval_mcopy(state: &mut Machine, _opcode: Opcode, _position: usize) -> Control {
self::misc::mcopy(state)
}

fn eval_jump(state: &mut Machine, _opcode: Opcode, _position: usize) -> Control {
self::misc::jump(state)
}
Expand Down Expand Up @@ -492,6 +496,7 @@ pub fn eval(state: &mut Machine, opcode: Opcode, position: usize) -> Control {
table[Opcode::MLOAD.as_usize()] = eval_mload as _;
table[Opcode::MSTORE.as_usize()] = eval_mstore as _;
table[Opcode::MSTORE8.as_usize()] = eval_mstore8 as _;
table[Opcode::MCOPY.as_usize()] = eval_mcopy as _;
table[Opcode::JUMP.as_usize()] = eval_jump as _;
table[Opcode::JUMPI.as_usize()] = eval_jumpi as _;
table[Opcode::PC.as_usize()] = eval_pc as _;
Expand Down
2 changes: 2 additions & 0 deletions core/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ impl Opcode {
pub const MSTORE: Opcode = Opcode(0x52);
/// `MSTORE8`
pub const MSTORE8: Opcode = Opcode(0x53);
/// `MCOPY`
pub const MCOPY: Opcode = Opcode(0x5e);
/// `JUMP`
pub const JUMP: Opcode = Opcode(0x56);
/// `JUMPI`
Expand Down

0 comments on commit 8eff41c

Please sign in to comment.