EIPs Explained

EIP-7912 · Stagnant · Core · 2025-03-25

EIP-7912: Pragmatic stack manipulation tools

Proposes deeper stack access for the EVM (SWAP17–24, DUP17–24, SWAPN, DUPN, EXCHANGE) without EOF, by reading operands from an immediately preceding PUSH instead of new immediates.

What problem does it solve?

EVM instructions can only reach the top 16 stack items (DUP1–DUP16, SWAP1–SWAP16). Compilers such as Solidity regularly hit "stack too deep" errors when a function has many local variables, forcing workarounds like spilling to memory.

Earlier fixes either required code versioning through the EVM Object Format (EOF), or added opcodes with immediate bytes, which would change how some already-deployed bytecode is interpreted. EIP-7912 takes a pragmatic route: new instructions that reuse familiar semantics and take their operand from a normal PUSH.

How it works

Counting top - 0 as the most recently pushed item:

  • Fixed opcodes: SWAP17–SWAP24 (0xb0–0xb7) and DUP17–DUP24 (0xb8–0xbf) behave just like the existing SWAP/DUP, only reaching further.
  • SWAPN (0xc0) and DUPN (0xc1): pop N from the stack, then swap/duplicate at depth N. N must come from a PUSH1 directly before the instruction; otherwise, or if N is 0, execution fails as out-of-gas.
  • EXCHANGE (0xc2): pops one argument, splits it into two 4-bit indices N and M, and swaps those two stack items (one-indexed, so EXCHANGE(1, 2) swaps the top two). It must be preceded by a PUSH2.
  • All new instructions cost 3 gas, plus the gas of the preceding push.

Because the operand travels through a real PUSH, no existing bytecode changes meaning.

Sample code

Bytecode reaching the 20th stack item two ways:

; fixed opcode, no operand bytes
DUP20                 ; 0xbb

; dynamic form: 2 extra bytes, any depth expressible in one byte
PUSH1 0x14            ; 0x60 0x14  (N = 20)
DUPN                  ; 0xc1

Things to know

  • Status is Stagnant: there is no active work toward including it in a fork, and the test cases are still TODO.
  • Clients must check that the preceding PUSH is a real instruction and not data inside a longer push (the spec suggests checking that pc-2 is a valid jump destination).
  • EIP-8024 ("Backward compatible SWAPN, DUPN, EXCHANGE") pursues the same goal without EOF and is listed as scheduled for the Glamsterdam upgrade in EIP-7773; look there for the version being adopted.
  • Earlier approach: EIP-663 defines SWAPN/DUPN/EXCHANGE with immediates inside EOF (EIP-3540).

#evm #opcodes #stack #compilers #core

Details

Authors
lightclient (@lightclient)
Status
Stagnant
Type
Standards Track / Core
Created
2025-03-25

Machine-readable: Markdown · JSON