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:
| 27 | <snip> |
|---|---|
| 38 | # STACK |
| 39 | start_position = pop(evm.stack) |
| 40 | value = pop(evm.stack).to_be_bytes32() |
| 41 | |
| 42 | # GAS |
| 43 | extend_memory = calculate_gas_extend_memory( |
| 44 | evm.memory, [(start_position, U256(len(value)))] |
| 45 | ) |
| 46 | |
| 47 | charge_gas(evm, GasCosts.OPCODE_MSTORE_BASE + extend_memory.cost) |
| 48 | |
| 49 | # OPERATION |
| 50 | evm.memory += b"\x00" * extend_memory.expand_by |
| 51 | memory_write(evm.memory, start_position, value) |
| 52 | |
| 53 | # PROGRAM COUNTER |
| 54 | 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:
| 58 | <snip> |
|---|---|
| 69 | # STACK |
| 70 | start_position = pop(evm.stack) |
| 71 | value = pop(evm.stack) |
| 72 | |
| 73 | # GAS |
| 74 | extend_memory = calculate_gas_extend_memory( |
| 75 | evm.memory, [(start_position, U256(1))] |
| 76 | ) |
| 77 | |
| 78 | charge_gas(evm, GasCosts.OPCODE_MSTORE8_BASE + extend_memory.cost) |
| 79 | |
| 80 | # OPERATION |
| 81 | evm.memory += b"\x00" * extend_memory.expand_by |
| 82 | normalized_bytes_value = Bytes([value & U256(0xFF)]) |
| 83 | memory_write(evm.memory, start_position, normalized_bytes_value) |
| 84 | |
| 85 | # PROGRAM COUNTER |
| 86 | evm.pc += Uint(1) |
mload ¶
Loads a word from memory.
Parameters
evm : The current EVM frame.
def mload(evm: Evm) -> None:
| 90 | <snip> |
|---|---|
| 99 | # STACK |
| 100 | start_position = pop(evm.stack) |
| 101 | |
| 102 | # GAS |
| 103 | extend_memory = calculate_gas_extend_memory( |
| 104 | evm.memory, [(start_position, U256(32))] |
| 105 | ) |
| 106 | charge_gas(evm, GasCosts.OPCODE_MLOAD_BASE + extend_memory.cost) |
| 107 | |
| 108 | # OPERATION |
| 109 | evm.memory += b"\x00" * extend_memory.expand_by |
| 110 | value = U256.from_be_bytes( |
| 111 | memory_read_bytes(evm.memory, start_position, U256(32)) |
| 112 | ) |
| 113 | push(evm.stack, value) |
| 114 | |
| 115 | # PROGRAM COUNTER |
| 116 | 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:
| 143 | <snip> |
|---|---|
| 152 | # STACK |
| 153 | destination = pop(evm.stack) |
| 154 | source = pop(evm.stack) |
| 155 | length = pop(evm.stack) |
| 156 | |
| 157 | # GAS |
| 158 | words = ceil32(Uint(length)) // Uint(32) |
| 158 | copy_gas_cost = GasCosts.OPCODE_COPY_PER_WORD * words |
| 159 | copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words) |
| 160 | |
| 161 | extend_memory = calculate_gas_extend_memory( |
| 162 | evm.memory, [(source, length), (destination, length)] |
| 163 | ) |
| 164 | charge_gas( |
| 165 | evm, |
| 166 | GasCosts.OPCODE_MCOPY_BASE + copy_gas_cost + extend_memory.cost, |
| 167 | ) |
| 168 | |
| 169 | # OPERATION |
| 170 | evm.memory += b"\x00" * extend_memory.expand_by |
| 171 | value = memory_read_bytes(evm.memory, source, length) |
| 172 | memory_write(evm.memory, destination, value) |
| 173 | |
| 174 | # PROGRAM COUNTER |
| 175 | evm.pc += Uint(1) |