revm_interpreter/instructions/
memory.rs1use crate::{
2 gas,
3 interpreter_types::{InterpreterTypes, MemoryTr, RuntimeFlag, StackTr},
4};
5use core::cmp::max;
6use primitives::U256;
7
8use crate::InstructionContext;
9
10pub fn mload<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
14 popn_top!([], top, context.interpreter);
16 let offset = as_usize_or_fail!(context.interpreter, top);
17 resize_memory!(context.interpreter, offset, 32);
18 *top =
19 U256::try_from_be_slice(context.interpreter.memory.slice_len(offset, 32).as_ref()).unwrap()
20}
21
22pub fn mstore<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
26 popn!([offset, value], context.interpreter);
28 let offset = as_usize_or_fail!(context.interpreter, offset);
29 resize_memory!(context.interpreter, offset, 32);
30 context
31 .interpreter
32 .memory
33 .set(offset, &value.to_be_bytes::<32>());
34}
35
36pub fn mstore8<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
40 popn!([offset, value], context.interpreter);
42 let offset = as_usize_or_fail!(context.interpreter, offset);
43 resize_memory!(context.interpreter, offset, 1);
44 context.interpreter.memory.set(offset, &[value.byte(0)]);
45}
46
47pub fn msize<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
51 push!(
53 context.interpreter,
54 U256::from(context.interpreter.memory.size())
55 );
56}
57
58pub fn mcopy<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
62 check!(context.interpreter, CANCUN);
63 popn!([dst, src, len], context.interpreter);
64
65 let len = as_usize_or_fail!(context.interpreter, len);
67 gas_or_fail!(context.interpreter, gas::copy_cost_verylow(len));
69 if len == 0 {
70 return;
71 }
72
73 let dst = as_usize_or_fail!(context.interpreter, dst);
74 let src = as_usize_or_fail!(context.interpreter, src);
75 resize_memory!(context.interpreter, max(dst, src), len);
77 context.interpreter.memory.copy(dst, src, len);
79}