use crate::interpreter_types::{InterpreterTypes, MemoryTr, RuntimeFlag, StackTr};
use context_interface::Host;
use core::cmp::max;
use primitives::U256;
use crate::InstructionContext;
pub fn mload<WIRE: InterpreterTypes, H: Host + ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn_top!([], top, context.interpreter);
let offset = as_usize_or_fail!(context.interpreter, top);
resize_memory!(context.interpreter, context.host.gas_params(), offset, 32);
*top =
U256::try_from_be_slice(context.interpreter.memory.slice_len(offset, 32).as_ref()).unwrap()
}
pub fn mstore<WIRE: InterpreterTypes, H: Host + ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn!([offset, value], context.interpreter);
let offset = as_usize_or_fail!(context.interpreter, offset);
resize_memory!(context.interpreter, context.host.gas_params(), offset, 32);
context
.interpreter
.memory
.set(offset, &value.to_be_bytes::<32>());
}
pub fn mstore8<WIRE: InterpreterTypes, H: Host + ?Sized>(context: InstructionContext<'_, H, WIRE>) {
popn!([offset, value], context.interpreter);
let offset = as_usize_or_fail!(context.interpreter, offset);
resize_memory!(context.interpreter, context.host.gas_params(), offset, 1);
context.interpreter.memory.set(offset, &[value.byte(0)]);
}
pub fn msize<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
push!(
context.interpreter,
U256::from(context.interpreter.memory.size())
);
}
pub fn mcopy<WIRE: InterpreterTypes, H: Host + ?Sized>(context: InstructionContext<'_, H, WIRE>) {
check!(context.interpreter, CANCUN);
popn!([dst, src, len], context.interpreter);
let len = as_usize_or_fail!(context.interpreter, len);
gas!(
context.interpreter,
context.host.gas_params().mcopy_cost(len)
);
if len == 0 {
return;
}
let dst = as_usize_or_fail!(context.interpreter, dst);
let src = as_usize_or_fail!(context.interpreter, src);
resize_memory!(
context.interpreter,
context.host.gas_params(),
max(dst, src),
len
);
context.interpreter.memory.copy(dst, src, len);
}