EIPs Explained

EIP-8024 · Last Call · Core · 2025-08-16

EIP-8024: Backward compatible SWAPN, DUPN, EXCHANGE

Adds DUPN, SWAPN and EXCHANGE opcodes so compilers can reach stack items deeper than 16, using a clever immediate encoding that keeps existing bytecode's jump targets unchanged.

What problem does it solve?

The EVM stack holds up to 1024 items, but DUP1–DUP16 and SWAP1–SWAP16 can only reach the top 16. Anyone who has seen Solidity's "stack too deep" error has hit this limit. Compilers work around it by spilling variables to memory, which makes code bigger, slower and harder to analyse. Reordering items for the next operation also often takes several instructions (SWAP2 SWAP3 SWAP2) when the client could do it with one pointer swap.

Similar opcodes were proposed before for EOF (EIP-663), which relied on EOF's bytecode validation. EIP-8024 brings them to the existing EVM without EOF.

How it works

Three opcodes, each followed by a one-byte immediate. Each costs 3 gas, the same as DUP/SWAP:

Opcode Byte Effect
DUPN n 0xe6 Push a copy of the nth item (n = 17…235)
SWAPN n 0xe7 Swap the top with item n + 1 (n = 17…235)
EXCHANGE n m 0xe8 Swap items n + 1 and m + 1 (1 ≤ n < m, n + m ≤ 30)

The backward-compatibility trick. Old bytecode may contain the bytes e6 5b (INVALID JUMPDEST). If the new opcodes simply treated the next byte as data, that JUMPDEST would stop being a valid jump target and existing contracts could break. So jump-destination analysis is left unchanged, and immediates that would be 0x5b (JUMPDEST) or 0x60–0x7f (PUSH1–PUSH32) are forbidden. Executing such a byte halts exceptionally. To avoid wasting the useful range, the raw byte is mapped to n with modular arithmetic:

def decode_single(x):          # DUPN / SWAPN
    assert 0 <= x <= 90 or 128 <= x <= 255
    return (x + 145) % 256     # 17..235

def encode_single(n):
    assert 17 <= n <= 235
    return (n + 111) % 256

EXCHANGE packs two indices into one byte with a separate decode_pair / encode_pair scheme, defined in the spec.

Sample code

Test vectors from the spec (hex bytecode → disassembly):

e680      DUPN 17
e7db      SWAPN 108
e89d      EXCHANGE 2 3
e82f      EXCHANGE 1 19
e6805b    DUPN 17, JUMPDEST
e75b      INVALID_SWAPN, JUMPDEST   ; 0x5b immediate is not allowed
e6605b    INVALID_DUPN, PUSH1 0x5b  ; PUSH range is not allowed

Execution example: 600060016002e88e pushes 0, 1, 2 and then EXCHANGE 1 2 (immediate 0x8e), leaving the stack top-to-bottom as [2, 0, 1].

Things to know

  • Status: Last Call. The spec itself does not name an activation fork. Check the fork meta-EIPs before relying on it.
  • DUPN/SWAPN start at depth 17 because 1–16 are already covered by DUP1–DUP16/SWAP1–SWAP16.
  • Operands are immediate, never taken from the stack, on purpose: dynamic stack access would make static analysis and auditing much harder.
  • Disassemblers must decode an invalid immediate as INVALID_* followed by the next byte as its own instruction.
  • EXCHANGE n m is defined to equal SWAPn SWAPm SWAPn, which explains its "off by one" operands.

#evm #opcodes #stack #compilers #core

Details

Authors
Francisco Giordano (@frangio), Charles Cooper (@charles-cooper), Alex Beregszaszi (@axic), Paweł Bylica (@chfast)
Status
Last Call
Type
Standards Track / Core
Created
2025-08-16

Machine-readable: Markdown · JSON