ethereum.forks.prague.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 | """ |
|---|---|
| 31 | Stores a word to memory. |
| 32 | This also expands the memory, if the memory is |
| 33 | insufficient to store the word. |
| 34 | |
| 35 | Parameters |
| 36 | ---------- |
| 37 | evm : |
| 38 | The current EVM frame. |
| 39 | |
| 40 | """ |
| 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 | """ |
|---|---|
| 62 | Stores a byte to memory. |
| 63 | This also expands the memory, if the memory is |
| 64 | insufficient to store the word. |
| 65 | |
| 66 | Parameters |
| 67 | ---------- |
| 68 | evm : |
| 69 | The current EVM frame. |
| 70 | |
| 71 | """ |
| 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 | """ |
|---|---|
| 94 | Loads a word from memory. |
| 95 | |
| 96 | Parameters |
| 97 | ---------- |
| 98 | evm : |
| 99 | The current EVM frame. |
| 100 | |
| 101 | """ |
| 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.
def msize(evm: Evm) -> None:
| 123 | """ |
|---|---|
| 124 | Pushes the size of active memory in bytes onto the stack. |
| 125 | |
| 126 | Parameters |
| 127 | ---------- |
| 128 | evm : |
| 129 | The current EVM frame. |
| 130 | |
| 131 | """ |
| 132 | # STACK |
| 133 | pass |
| 134 | |
| 135 | # GAS |
| 136 | charge_gas(evm, GasCosts.OPCODE_MSIZE) |
| 137 | |
| 138 | # OPERATION |
| 139 | push(evm.stack, U256(len(evm.memory))) |
| 140 | |
| 141 | # PROGRAM COUNTER |
| 142 | evm.pc += Uint(1) |
mcopy ¶
Copies the bytes in memory from one location to another.
Parameters
evm : The current EVM frame.
def mcopy(evm: Evm) -> None:
| 146 | """ |
|---|---|
| 147 | Copies the bytes in memory from one location to another. |
| 148 | |
| 149 | Parameters |
| 150 | ---------- |
| 151 | evm : |
| 152 | The current EVM frame. |
| 153 | |
| 154 | """ |
| 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) |