Skip to content

Commit

Permalink
Unlimited SWAP and DUP instructions (ethereum#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
axic authored and ilanolkies committed Nov 12, 2019
1 parent b21e977 commit 9b9224e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions EIPS/eip-663.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
eip: 663
title: Unlimited SWAP and DUP instructions
author: Alex Beregszaszi (@axic)
type: Standards Track
category: Core
status: Draft
created: 2017-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

Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).

0 comments on commit 9b9224e

Please sign in to comment.