ethereum.forks.bpo5.vm.instructions.memoryethereum.forks.amsterdam.vm.instructions.memory
Ethereum Virtual Machine (EVM) Memory Instructions.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM Memory instructions.
mstore ¶
Stores a word to memory. This also expands the memory, if the memory is insufficient to store the word.
Parameters
evm : The current EVM frame.
def mstore(evm: Evm) -> None:
| 30 | <snip> |
|---|---|
| 41 | # STACK |
| 42 | start_position = pop(evm.stack) |
| 43 | value = pop(evm.stack).to_be_bytes32() |
| 44 | |
| 45 | # GAS |
| 46 | extend_memory = calculate_gas_extend_memory( |
| 47 | evm.memory, [(start_position, U256(len(value)))] |
| 48 | ) |
| 49 | |
| 50 | charge_gas(evm, GasCosts.OPCODE_MSTORE_BASE + extend_memory.cost) |
| 51 | |
| 52 | # OPERATION |
| 53 | evm.memory += b"\x00" * extend_memory.expand_by |
| 54 | memory_write(evm.memory, start_position, value) |
| 55 | |
| 56 | # PROGRAM COUNTER |
| 57 | evm.pc += Uint(1) |
mstore8 ¶
Stores a byte to memory. This also expands the memory, if the memory is insufficient to store the word.
Parameters
evm : The current EVM frame.
def mstore8(evm: Evm) -> None:
| 61 | <snip> |
|---|---|
| 72 | # STACK |
| 73 | start_position = pop(evm.stack) |
| 74 | value = pop(evm.stack) |
| 75 | |
| 76 | # GAS |
| 77 | extend_memory = calculate_gas_extend_memory( |
| 78 | evm.memory, [(start_position, U256(1))] |
| 79 | ) |
| 80 | |
| 81 | charge_gas(evm, GasCosts.OPCODE_MSTORE8_BASE + extend_memory.cost) |
| 82 | |
| 83 | # OPERATION |
| 84 | evm.memory += b"\x00" * extend_memory.expand_by |
| 85 | normalized_bytes_value = Bytes([value & U256(0xFF)]) |
| 86 | memory_write(evm.memory, start_position, normalized_bytes_value) |
| 87 | |
| 88 | # PROGRAM COUNTER |
| 89 | evm.pc += Uint(1) |
mload ¶
Loads a word from memory.
Parameters
evm : The current EVM frame.
def mload(evm: Evm) -> None:
| 93 | <snip> |
|---|---|
| 102 | # STACK |
| 103 | start_position = pop(evm.stack) |
| 104 | |
| 105 | # GAS |
| 106 | extend_memory = calculate_gas_extend_memory( |
| 107 | evm.memory, [(start_position, U256(32))] |
| 108 | ) |
| 109 | charge_gas(evm, GasCosts.OPCODE_MLOAD_BASE + extend_memory.cost) |
| 110 | |
| 111 | # OPERATION |
| 112 | evm.memory += b"\x00" * extend_memory.expand_by |
| 113 | value = U256.from_be_bytes( |
| 114 | memory_read_bytes(evm.memory, start_position, U256(32)) |
| 115 | ) |
| 116 | push(evm.stack, value) |
| 117 | |
| 118 | # PROGRAM COUNTER |
| 119 | evm.pc += Uint(1) |
msize ¶
Pushes the size of active memory in bytes onto the stack.
Parameters
evm : The current EVM frame.
mcopy ¶
Copies the bytes in memory from one location to another.
Parameters
evm : The current EVM frame.
def mcopy(evm: Evm) -> None:
| 146 | <snip> |
|---|---|
| 155 | # STACK |
| 156 | destination = pop(evm.stack) |
| 157 | source = pop(evm.stack) |
| 158 | length = pop(evm.stack) |
| 159 | |
| 160 | # GAS |
| 161 | words = ceil32(Uint(length)) // Uint(32) |
| 162 | copy_gas_cost = GasCosts.OPCODE_COPY_PER_WORD * words |
| 163 | |
| 164 | extend_memory = calculate_gas_extend_memory( |
| 165 | evm.memory, [(source, length), (destination, length)] |
| 166 | ) |
| 167 | charge_gas( |
| 168 | evm, |
| 169 | GasCosts.OPCODE_MCOPY_BASE + copy_gas_cost + extend_memory.cost, |
| 170 | ) |
| 171 | |
| 172 | # OPERATION |
| 173 | evm.memory += b"\x00" * extend_memory.expand_by |
| 174 | value = memory_read_bytes(evm.memory, source, length) |
| 175 | memory_write(evm.memory, destination, value) |
| 176 | |
| 177 | # PROGRAM COUNTER |
| 178 | evm.pc += Uint(1) |