ethereum.shanghai.vm.instructions.stackethereum.cancun.vm.instructions.stack

Ethereum Virtual Machine (EVM) Stack Instructions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM stack related instructions.

pop

Remove item from stack.

Parameters

evm : The current EVM frame.

def pop(evm: Evm) -> None:
26
    """
27
    Remove item from stack.
28
29
    Parameters
30
    ----------
31
    evm :
32
        The current EVM frame.
33
34
    """
35
    # STACK
36
    stack.pop(evm.stack)
37
38
    # GAS
39
    charge_gas(evm, GAS_BASE)
40
41
    # OPERATION
42
    pass
43
44
    # PROGRAM COUNTER
45
    evm.pc += Uint(1)

push_n

Pushes a N-byte immediate onto the stack. Push zero if num_bytes is zero.

Parameters

evm : The current EVM frame.

num_bytes : The number of immediate bytes to be read from the code and pushed to the stack. Push zero if num_bytes is zero.

def push_n(evm: Evm, ​​num_bytes: int) -> None:
49
    """
50
    Pushes a N-byte immediate onto the stack. Push zero if num_bytes is zero.
51
52
    Parameters
53
    ----------
54
    evm :
55
        The current EVM frame.
56
57
    num_bytes :
58
        The number of immediate bytes to be read from the code and pushed to
59
        the stack. Push zero if num_bytes is zero.
60
61
    """
62
    # STACK
63
    pass
64
65
    # GAS
66
    if num_bytes == 0:
67
        charge_gas(evm, GAS_BASE)
68
    else:
69
        charge_gas(evm, GAS_VERY_LOW)
70
71
    # OPERATION
72
    data_to_push = U256.from_be_bytes(
73
        buffer_read(evm.code, U256(evm.pc + Uint(1)), U256(num_bytes))
74
    )
75
    stack.push(evm.stack, data_to_push)
76
77
    # PROGRAM COUNTER
78
    evm.pc += Uint(1) + Uint(num_bytes)

dup_n

Duplicate the Nth stack item (from top of the stack) to the top of stack.

Parameters

evm : The current EVM frame.

item_number : The stack item number (0-indexed from top of stack) to be duplicated to the top of stack.

def dup_n(evm: Evm, ​​item_number: int) -> None:
82
    """
83
    Duplicate the Nth stack item (from top of the stack) to the top of stack.
84
85
    Parameters
86
    ----------
87
    evm :
88
        The current EVM frame.
89
90
    item_number :
91
        The stack item number (0-indexed from top of stack) to be duplicated
92
        to the top of stack.
93
94
    """
95
    # STACK
96
    pass
97
98
    # GAS
99
    charge_gas(evm, GAS_VERY_LOW)
100
    if item_number >= len(evm.stack):
101
        raise StackUnderflowError
102
    data_to_duplicate = evm.stack[len(evm.stack) - 1 - item_number]
103
    stack.push(evm.stack, data_to_duplicate)
104
105
    # PROGRAM COUNTER
106
    evm.pc += Uint(1)

swap_n

Swap the top and the item_number element of the stack, where the top of the stack is position zero.

If item_number is zero, this function does nothing (which should not be possible, since there is no SWAP0 instruction).

Parameters

evm : The current EVM frame.

item_number : The stack item number (0-indexed from top of stack) to be swapped with the top of stack element.

def swap_n(evm: Evm, ​​item_number: int) -> None:
110
    """
111
    Swap the top and the `item_number` element of the stack, where
112
    the top of the stack is position zero.
113
114
    If `item_number` is zero, this function does nothing (which should not be
115
    possible, since there is no `SWAP0` instruction).
116
117
    Parameters
118
    ----------
119
    evm :
120
        The current EVM frame.
121
122
    item_number :
123
        The stack item number (0-indexed from top of stack) to be swapped
124
        with the top of stack element.
125
126
    """
127
    # STACK
128
    pass
129
130
    # GAS
131
    charge_gas(evm, GAS_VERY_LOW)
132
    if item_number >= len(evm.stack):
133
        raise StackUnderflowError
134
    evm.stack[-1], evm.stack[-1 - item_number] = (
135
        evm.stack[-1 - item_number],
136
        evm.stack[-1],
137
    )
138
139
    # PROGRAM COUNTER
140
    evm.pc += Uint(1)

push0

143
push0 = partial(push_n, num_bytes=0)

push1

144
push1 = partial(push_n, num_bytes=1)

push2

145
push2 = partial(push_n, num_bytes=2)

push3

146
push3 = partial(push_n, num_bytes=3)

push4

147
push4 = partial(push_n, num_bytes=4)

push5

148
push5 = partial(push_n, num_bytes=5)

push6

149
push6 = partial(push_n, num_bytes=6)

push7

150
push7 = partial(push_n, num_bytes=7)

push8

151
push8 = partial(push_n, num_bytes=8)

push9

152
push9 = partial(push_n, num_bytes=9)

push10

153
push10 = partial(push_n, num_bytes=10)

push11

154
push11 = partial(push_n, num_bytes=11)

push12

155
push12 = partial(push_n, num_bytes=12)

push13

156
push13 = partial(push_n, num_bytes=13)

push14

157
push14 = partial(push_n, num_bytes=14)

push15

158
push15 = partial(push_n, num_bytes=15)

push16

159
push16 = partial(push_n, num_bytes=16)

push17

160
push17 = partial(push_n, num_bytes=17)

push18

161
push18 = partial(push_n, num_bytes=18)

push19

162
push19 = partial(push_n, num_bytes=19)

push20

163
push20 = partial(push_n, num_bytes=20)

push21

164
push21 = partial(push_n, num_bytes=21)

push22

165
push22 = partial(push_n, num_bytes=22)

push23

166
push23 = partial(push_n, num_bytes=23)

push24

167
push24 = partial(push_n, num_bytes=24)

push25

168
push25 = partial(push_n, num_bytes=25)

push26

169
push26 = partial(push_n, num_bytes=26)

push27

170
push27 = partial(push_n, num_bytes=27)

push28

171
push28 = partial(push_n, num_bytes=28)

push29

172
push29 = partial(push_n, num_bytes=29)

push30

173
push30 = partial(push_n, num_bytes=30)

push31

174
push31 = partial(push_n, num_bytes=31)

push32

175
push32 = partial(push_n, num_bytes=32)

dup1

177
dup1 = partial(dup_n, item_number=0)

dup2

178
dup2 = partial(dup_n, item_number=1)

dup3

179
dup3 = partial(dup_n, item_number=2)

dup4

180
dup4 = partial(dup_n, item_number=3)

dup5

181
dup5 = partial(dup_n, item_number=4)

dup6

182
dup6 = partial(dup_n, item_number=5)

dup7

183
dup7 = partial(dup_n, item_number=6)

dup8

184
dup8 = partial(dup_n, item_number=7)

dup9

185
dup9 = partial(dup_n, item_number=8)

dup10

186
dup10 = partial(dup_n, item_number=9)

dup11

187
dup11 = partial(dup_n, item_number=10)

dup12

188
dup12 = partial(dup_n, item_number=11)

dup13

189
dup13 = partial(dup_n, item_number=12)

dup14

190
dup14 = partial(dup_n, item_number=13)

dup15

191
dup15 = partial(dup_n, item_number=14)

dup16

192
dup16 = partial(dup_n, item_number=15)

swap1

194
swap1 = partial(swap_n, item_number=1)

swap2

195
swap2 = partial(swap_n, item_number=2)

swap3

196
swap3 = partial(swap_n, item_number=3)

swap4

197
swap4 = partial(swap_n, item_number=4)

swap5

198
swap5 = partial(swap_n, item_number=5)

swap6

199
swap6 = partial(swap_n, item_number=6)

swap7

200
swap7 = partial(swap_n, item_number=7)

swap8

201
swap8 = partial(swap_n, item_number=8)

swap9

202
swap9 = partial(swap_n, item_number=9)

swap10

203
swap10 = partial(swap_n, item_number=10)

swap11

204
swap11 = partial(swap_n, item_number=11)

swap12

205
swap12 = partial(swap_n, item_number=12)

swap13

206
swap13 = partial(swap_n, item_number=13)

swap14

207
swap14 = partial(swap_n, item_number=14)

swap15

208
swap15 = partial(swap_n, item_number=15)

swap16

209
swap16 = partial(swap_n, item_number=16)