EIP-663 SWAPN, DUPN, and EXCHANGE instructions (SWAPN, DUPN and EXCHANGE instructions)

No call decision · EVM & execution · EIP status Stagnant · created 2017-07-03

Introduces three new instructions (SWAPN, DUPN, EXCHANGE) that allow working with values deeper than position 16 in the EVM stack. They only operate in EOF bytecode.

Why it is needed

The EVM stack can hold up to 1024 items, but the existing DUP1-DUP16 and SWAP1-SWAP16 instructions can only access the top 16 items of the stack. When trying to implement structures with many input and output arguments, like functions, on top of the EVM, the number of arguments easily exceeds 16, forcing compilers to resort to workarounds that write values to memory, which complicates the bytecode and increases execution cost. In addition, compiler optimizations that efficiently arrange variables on the stack (the stack equivalent of register allocation) also need the ability to swap two items that are not at the top, which currently requires combining several SWAP instructions. This proposal eliminates these inefficiencies by making deeper stack access and arbitrary-position swaps each possible with a single instruction.

What changes

Today

After EIP-663

Who is affected

In depth

Opcode and immediate encoding

Adds three opcodes: DUPN (0xe6), SWAPN (0xe7), and EXCHANGE (0xe8). Each instruction is followed by an 8-bit immediate value imm (0-255). DUPN and SWAPN define n = imm + 1. EXCHANGE uses the upper nibble of imm to define n = (imm >> 4) + 1 and the lower nibble to define m = (imm & 0x0F) + 1, specifying two depths in a single byte.

EOF-only, no change in legacy

When these three opcodes are encountered in legacy bytecode, they are still treated as an exceptional halt, so existing behavior does not change. The new rules apply only to valid EOF1 code. Because the immediate encoding could conflict with existing jumpdest analysis, it was designed to be unusable outside EOF.

Code validation extension

Validation rules are extended so that relative jump instructions (RJUMP, RJUMPI, RJUMPV) cannot target the immediate values of DUPN, SWAPN, and EXCHANGE as jump destinations. Additionally, EIP-5450's stack validation algorithm is extended so that code is treated as invalid if the stack height is less than n before executing DUPN, less than n+1 before executing SWAPN, or less than n+m+1 before executing EXCHANGE.

Execution Rules

DUPN duplicates the nth item of the stack (1-indexed) onto the top. SWAPN swaps the (n+1)th item of the stack with the top item. EXCHANGE swaps the (n+1)th item with the (n+m+1)th item. After DUPN executes, the stack height increases by 1; after SWAPN and EXCHANGE execute, the stack height does not change.

Gas cost

All three instructions have a fixed gas cost of 3. This is the same cost as the existing DUP*/SWAP*, because the actual implementation merely swaps pointers, so greater depth incurs no additional computational cost.

Reason for allowing only immediate values and the choice of size

Allowing the swap/duplicate target to be chosen dynamically at runtime would make the static analysis used in security audits difficult, so the target must always be specified as a fixed immediate value. DUPN and SWAPN can reach depths of up to 256 with an 8-bit immediate value; expanding to 16 bits to cover the full 1024-item stack was also considered, but it was decided that the additional n < 1024 check and the cost of using one more byte outweighed the benefit of 256 depth, so 8 bits was chosen. EXCHANGE uses two nibbles to reach depths of up to 32.

Proposal status

This proposal's status is Stagnant, left on hold without active discussion. It requires EIP-3540 (the EOF container format) and EIP-5450 (the stack validation algorithm), so these two proposals must be adopted first before it can apply.

Original EIP · ← EIPs