EEVM.Interpreter.Stack (eevm v0.1.0)

Copy Markdown View Source

The EVM stack — a LIFO data structure with a max depth of 1024.

Summary

Functions

Returns the current stack depth.

Returns the maximum stack depth (1024).

Creates a new empty stack.

Peeks at the element at the given depth (0 = top of stack).

Pops the top value from the stack.

Pushes a value onto the stack.

Swaps the top element with the element at the given depth.

Converts the stack to a list (top element first) for inspection.

Types

t()

@type t() :: %EEVM.Interpreter.Stack{
  elements: [non_neg_integer()],
  size: non_neg_integer()
}

Functions

depth(stack)

@spec depth(t()) :: non_neg_integer()

Returns the current stack depth.

max_depth()

@spec max_depth() :: pos_integer()

Returns the maximum stack depth (1024).

new()

@spec new() :: t()

Creates a new empty stack.

peek(stack, depth)

@spec peek(t(), non_neg_integer()) ::
  {:ok, non_neg_integer()} | {:error, :stack_underflow}

Peeks at the element at the given depth (0 = top of stack).

Used by DUP and SWAP instructions.

pop(stack)

@spec pop(t()) :: {:ok, non_neg_integer(), t()} | {:error, :stack_underflow}

Pops the top value from the stack.

Returns {:ok, value, new_stack} or {:error, :stack_underflow}.

push(stack, value)

@spec push(t(), non_neg_integer()) :: {:ok, t()} | {:error, :stack_overflow}

Pushes a value onto the stack.

Returns {:ok, stack} or {:error, :stack_overflow} if the stack is full.

EVM values are 256-bit unsigned integers. We enforce this with a bitmask.

swap(stack, depth)

@spec swap(t(), pos_integer()) :: {:ok, t()} | {:error, :stack_underflow}

Swaps the top element with the element at the given depth.

SWAP1 swaps positions 0 and 1, SWAP2 swaps 0 and 2, etc.

to_list(stack)

@spec to_list(t()) :: [non_neg_integer()]

Converts the stack to a list (top element first) for inspection.