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) andDUP17–DUP24(0xb8–0xbf) behave just like the existing SWAP/DUP, only reaching further. SWAPN(0xc0) andDUPN(0xc1): popNfrom the stack, then swap/duplicate at depthN.Nmust come from aPUSH1directly before the instruction; otherwise, or ifNis 0, execution fails as out-of-gas.EXCHANGE(0xc2): pops one argument, splits it into two 4-bit indicesNandM, and swaps those two stack items (one-indexed, soEXCHANGE(1, 2)swaps the top two). It must be preceded by aPUSH2.- 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
PUSHis a real instruction and not data inside a longer push (the spec suggests checking thatpc-2is 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/EXCHANGEwith immediates inside EOF (EIP-3540).