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