EEVM.Interpreter.Memory (eevm v0.1.0)

Copy Markdown View Source

EVM memory — a byte-addressable, dynamically expanding linear memory.

Memory expands in 32-byte chunks. Reads or writes beyond the current size auto-expand and are zero-filled. Gas cost increases quadratically with memory size (see EEVM.Gas.Memory).

Summary

Functions

Copies length bytes from src to dst within memory (memmove semantics).

Loads a 256-bit (32-byte) word from the given byte offset.

Creates a new empty memory.

Reads a range of bytes from memory as a binary.

Returns the current memory size in bytes (always a multiple of 32).

Stores a 256-bit (32-byte) word at the given byte offset.

Stores a single byte at the given offset.

Types

t()

@type t() :: %EEVM.Interpreter.Memory{
  data: %{required(non_neg_integer()) => byte()},
  size: non_neg_integer()
}

Functions

copy(memory, dst, src, length)

@spec copy(t(), non_neg_integer(), non_neg_integer(), non_neg_integer()) :: t()

Copies length bytes from src to dst within memory (memmove semantics).

This is the MCOPY operation (EIP-5656). Handles overlapping regions correctly by reading all source bytes before writing — similar to C's memmove.

load(memory, offset)

@spec load(t(), non_neg_integer()) :: {non_neg_integer(), t()}

Loads a 256-bit (32-byte) word from the given byte offset.

This is the MLOAD operation. Unwritten bytes read as zero.

new()

@spec new() :: t()

Creates a new empty memory.

read_bytes(memory, offset, length)

@spec read_bytes(t(), non_neg_integer(), non_neg_integer()) :: {binary(), t()}

Reads a range of bytes from memory as a binary.

Used by RETURN, REVERT, and LOG operations.

size(memory)

@spec size(t()) :: non_neg_integer()

Returns the current memory size in bytes (always a multiple of 32).

store(memory, offset, value)

@spec store(t(), non_neg_integer(), non_neg_integer()) :: t()

Stores a 256-bit (32-byte) word at the given byte offset.

This is the MSTORE operation. The value is stored big-endian, which is the EVM's byte ordering convention.

store_byte(memory, offset, value)

@spec store_byte(t(), non_neg_integer(), non_neg_integer()) :: t()

Stores a single byte at the given offset.

This is the MSTORE8 operation.