ethereum.forks.prague.vm.instructions.stackethereum.forks.osaka.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:
25 | """ |
---|---|
26 | Remove item from stack. |
27 |
|
28 | Parameters |
29 | ---------- |
30 | evm : |
31 | The current EVM frame. |
32 |
|
33 | """ |
34 | # STACK |
35 | stack.pop(evm.stack) |
36 | |
37 | # GAS |
38 | charge_gas(evm, GAS_BASE) |
39 | |
40 | # OPERATION |
41 | pass |
42 | |
43 | # PROGRAM COUNTER |
44 | 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:
48 | """ |
---|---|
49 | Pushes a N-byte immediate onto the stack. Push zero if num_bytes is zero. |
50 |
|
51 | Parameters |
52 | ---------- |
53 | evm : |
54 | The current EVM frame. |
55 |
|
56 | num_bytes : |
57 | The number of immediate bytes to be read from the code and pushed to |
58 | the stack. Push zero if num_bytes is zero. |
59 |
|
60 | """ |
61 | # STACK |
62 | pass |
63 | |
64 | # GAS |
65 | if num_bytes == 0: |
66 | charge_gas(evm, GAS_BASE) |
67 | else: |
68 | charge_gas(evm, GAS_VERY_LOW) |
69 | |
70 | # OPERATION |
71 | data_to_push = U256.from_be_bytes( |
72 | buffer_read(evm.code, U256(evm.pc + Uint(1)), U256(num_bytes)) |
73 | ) |
74 | stack.push(evm.stack, data_to_push) |
75 | |
76 | # PROGRAM COUNTER |
77 | 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:
81 | """ |
---|---|
82 | Duplicate the Nth stack item (from top of the stack) to the top of stack. |
83 |
|
84 | Parameters |
85 | ---------- |
86 | evm : |
87 | The current EVM frame. |
88 |
|
89 | item_number : |
90 | The stack item number (0-indexed from top of stack) to be duplicated |
91 | to the top of stack. |
92 |
|
93 | """ |
94 | # STACK |
95 | pass |
96 | |
97 | # GAS |
98 | charge_gas(evm, GAS_VERY_LOW) |
99 | if item_number >= len(evm.stack): |
100 | raise StackUnderflowError |
101 | data_to_duplicate = evm.stack[len(evm.stack) - 1 - item_number] |
102 | stack.push(evm.stack, data_to_duplicate) |
103 | |
104 | # PROGRAM COUNTER |
105 | 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:
109 | """ |
---|---|
110 | Swap the top and the `item_number` element of the stack, where |
111 | the top of the stack is position zero. |
112 |
|
113 | If `item_number` is zero, this function does nothing (which should not be |
114 | possible, since there is no `SWAP0` instruction). |
115 |
|
116 | Parameters |
117 | ---------- |
118 | evm : |
119 | The current EVM frame. |
120 |
|
121 | item_number : |
122 | The stack item number (0-indexed from top of stack) to be swapped |
123 | with the top of stack element. |
124 |
|
125 | """ |
126 | # STACK |
127 | pass |
128 | |
129 | # GAS |
130 | charge_gas(evm, GAS_VERY_LOW) |
131 | if item_number >= len(evm.stack): |
132 | raise StackUnderflowError |
133 | evm.stack[-1], evm.stack[-1 - item_number] = ( |
134 | evm.stack[-1 - item_number], |
135 | evm.stack[-1], |
136 | ) |
137 | |
138 | # PROGRAM COUNTER |
139 | evm.pc += Uint(1) |
push0
142 | push0 = partial(push_n, num_bytes=0) |
---|
push1
143 | push1 = partial(push_n, num_bytes=1) |
---|
push2
144 | push2 = partial(push_n, num_bytes=2) |
---|
push3
145 | push3 = partial(push_n, num_bytes=3) |
---|
push4
146 | push4 = partial(push_n, num_bytes=4) |
---|
push5
147 | push5 = partial(push_n, num_bytes=5) |
---|
push6
148 | push6 = partial(push_n, num_bytes=6) |
---|
push7
149 | push7 = partial(push_n, num_bytes=7) |
---|
push8
150 | push8 = partial(push_n, num_bytes=8) |
---|
push9
151 | push9 = partial(push_n, num_bytes=9) |
---|
push10
152 | push10 = partial(push_n, num_bytes=10) |
---|
push11
153 | push11 = partial(push_n, num_bytes=11) |
---|
push12
154 | push12 = partial(push_n, num_bytes=12) |
---|
push13
155 | push13 = partial(push_n, num_bytes=13) |
---|
push14
156 | push14 = partial(push_n, num_bytes=14) |
---|
push15
157 | push15 = partial(push_n, num_bytes=15) |
---|
push16
158 | push16 = partial(push_n, num_bytes=16) |
---|
push17
159 | push17 = partial(push_n, num_bytes=17) |
---|
push18
160 | push18 = partial(push_n, num_bytes=18) |
---|
push19
161 | push19 = partial(push_n, num_bytes=19) |
---|
push20
162 | push20 = partial(push_n, num_bytes=20) |
---|
push21
163 | push21 = partial(push_n, num_bytes=21) |
---|
push22
164 | push22 = partial(push_n, num_bytes=22) |
---|
push23
165 | push23 = partial(push_n, num_bytes=23) |
---|
push24
166 | push24 = partial(push_n, num_bytes=24) |
---|
push25
167 | push25 = partial(push_n, num_bytes=25) |
---|
push26
168 | push26 = partial(push_n, num_bytes=26) |
---|
push27
169 | push27 = partial(push_n, num_bytes=27) |
---|
push28
170 | push28 = partial(push_n, num_bytes=28) |
---|
push29
171 | push29 = partial(push_n, num_bytes=29) |
---|
push30
172 | push30 = partial(push_n, num_bytes=30) |
---|
push31
173 | push31 = partial(push_n, num_bytes=31) |
---|
push32
174 | push32 = partial(push_n, num_bytes=32) |
---|
dup1
176 | dup1 = partial(dup_n, item_number=0) |
---|
dup2
177 | dup2 = partial(dup_n, item_number=1) |
---|
dup3
178 | dup3 = partial(dup_n, item_number=2) |
---|
dup4
179 | dup4 = partial(dup_n, item_number=3) |
---|
dup5
180 | dup5 = partial(dup_n, item_number=4) |
---|
dup6
181 | dup6 = partial(dup_n, item_number=5) |
---|
dup7
182 | dup7 = partial(dup_n, item_number=6) |
---|
dup8
183 | dup8 = partial(dup_n, item_number=7) |
---|
dup9
184 | dup9 = partial(dup_n, item_number=8) |
---|
dup10
185 | dup10 = partial(dup_n, item_number=9) |
---|
dup11
186 | dup11 = partial(dup_n, item_number=10) |
---|
dup12
187 | dup12 = partial(dup_n, item_number=11) |
---|
dup13
188 | dup13 = partial(dup_n, item_number=12) |
---|
dup14
189 | dup14 = partial(dup_n, item_number=13) |
---|
dup15
190 | dup15 = partial(dup_n, item_number=14) |
---|
dup16
191 | dup16 = partial(dup_n, item_number=15) |
---|
swap1
193 | swap1 = partial(swap_n, item_number=1) |
---|
swap2
194 | swap2 = partial(swap_n, item_number=2) |
---|
swap3
195 | swap3 = partial(swap_n, item_number=3) |
---|
swap4
196 | swap4 = partial(swap_n, item_number=4) |
---|
swap5
197 | swap5 = partial(swap_n, item_number=5) |
---|
swap6
198 | swap6 = partial(swap_n, item_number=6) |
---|
swap7
199 | swap7 = partial(swap_n, item_number=7) |
---|
swap8
200 | swap8 = partial(swap_n, item_number=8) |
---|
swap9
201 | swap9 = partial(swap_n, item_number=9) |
---|
swap10
202 | swap10 = partial(swap_n, item_number=10) |
---|
swap11
203 | swap11 = partial(swap_n, item_number=11) |
---|
swap12
204 | swap12 = partial(swap_n, item_number=12) |
---|
swap13
205 | swap13 = partial(swap_n, item_number=13) |
---|
swap14
206 | swap14 = partial(swap_n, item_number=14) |
---|
swap15
207 | swap15 = partial(swap_n, item_number=15) |
---|
swap16
208 | swap16 = partial(swap_n, item_number=16) |
---|