revm_interpreter/instructions/
stack.rs

1use crate::{
2    interpreter_types::{Immediates, InterpreterTypes, Jumps, RuntimeFlag, StackTr},
3    InstructionResult,
4};
5use primitives::U256;
6
7use crate::InstructionContext;
8
9/// Implements the POP instruction.
10///
11/// Removes the top item from the stack.
12pub fn pop<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
13    // Can ignore return. as relative N jump is safe operation.
14    popn!([_i], context.interpreter);
15}
16
17/// EIP-3855: PUSH0 instruction
18///
19/// Introduce a new instruction which pushes the constant value 0 onto the stack.
20pub fn push0<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
21    check!(context.interpreter, SHANGHAI);
22    push!(context.interpreter, U256::ZERO);
23}
24
25/// Implements the PUSH1-PUSH32 instructions.
26///
27/// Pushes N bytes from bytecode onto the stack as a 32-byte value.
28pub fn push<const N: usize, WIRE: InterpreterTypes, H: ?Sized>(
29    context: InstructionContext<'_, H, WIRE>,
30) {
31    let slice = context.interpreter.bytecode.read_slice(N);
32    if !context.interpreter.stack.push_slice(slice) {
33        context.interpreter.halt(InstructionResult::StackOverflow);
34        return;
35    }
36
37    // Can ignore return. as relative N jump is safe operation
38    context.interpreter.bytecode.relative_jump(N as isize);
39}
40
41/// Implements the DUP1-DUP16 instructions.
42///
43/// Duplicates the Nth stack item to the top of the stack.
44pub fn dup<const N: usize, WIRE: InterpreterTypes, H: ?Sized>(
45    context: InstructionContext<'_, H, WIRE>,
46) {
47    if !context.interpreter.stack.dup(N) {
48        context.interpreter.halt(InstructionResult::StackOverflow);
49    }
50}
51
52/// Implements the SWAP1-SWAP16 instructions.
53///
54/// Swaps the top stack item with the Nth stack item.
55pub fn swap<const N: usize, WIRE: InterpreterTypes, H: ?Sized>(
56    context: InstructionContext<'_, H, WIRE>,
57) {
58    assert!(N != 0);
59    if !context.interpreter.stack.exchange(0, N) {
60        context.interpreter.halt(InstructionResult::StackOverflow);
61    }
62}