EIP 663: Unlimited SWAP and DUP instructions Source

AuthorAlex Beregszaszi
StatusDraft
TypeStandards Track
CategoryCore
Created2017-07-03

Abstract

SWAP and DUP instructions are limited to a stack depth of 16. Introduce two new instructions, SWAPn and DUPn, which lift this limitation and allow accessing the stack up to its full depth of 1024 items.

Motivation

Implementing higher level constructs, such as functions, on top of EVM will result in a list of input and output parameters as well as an instruction offset to return to.

The number of these arguments (or stack items) can easily exceed 16 and thus will require extra care from a compiler to lay them out in a way that all of them are still accessible.

Introducing SWAPn and DUPn will provide an option to compilers to simplify accessing deep stack items at the price of possibly increased gas costs.

Specification

Instructions DUPn (0xb0) and SWAPn (0xb1) are introduced, which take the top item from stack (referred to as n).

If n exceeds 1024 or the current stack depth is less than n, then a stack underflow exception is issued. If the current stack depth is at the limit, a stack overflow exception is issued.

Otherwise

  • for DUPn the stack item at depth n is duplicated at the top of the stack
  • for SWAPn the top stack item is swapped with the item at depth n

The gas cost for both instructions is set at 3. In reality the cost for such an operation is 6 including the required PUSH.

Since both of these instructions require the top stack item to contain the position, it is still only possible to reach more than 16 stack items if there is at least one free stack slot.

Rationale

TBA

Backwards Compatibility

This has no effect on backwards compatibility.

Test Cases

  • executing 602a600160026003600460056006600760086009600a600b600c600d600e600f60106011b0 should have 42 as the top stack item
  • executing 602a600160026003600460056006600760086009600a600b600c600d600e600f60106011b1 should have 42 as the top stack item

Implementation

TBA

Copyright and related rights waived via CC0.