Skip to content

Extend DUP1-16 / SWAP1-16 With DUPN / SWAPN #174

@mattdf

Description

@mattdf

Motivation

The fixed limit on stack referencing through the current DUP and SWAP operations limits the usability of the stack when writing compilers that target the EVM, as at most you can only access memory 17 levels deep. A solution that makes efficient use of the stack and allows for a higher number of local variables becomes needlessly complicated as beyond 17 levels the implementation needs to start storing things in memory instead.

Specification

Introduce two new opcodes, DUPN and SWAPN that take one integral argument, N, where DUPN duplicates the N'th stack item to the top of the stack, and SWAPN swaps the first stack item with the N'th stack item.

An argument N > the number of items on the stack should cause the program to terminate with a stack underflow, as it does currently for the SWAP#/DUP# set of instructions.

The gas cost for these operations should remain Wverylow, seeing as there is no real difference as the stack for a contract needs to be held in memory while executing either way, and the gas limit would likely be hit before the stack size would become a problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions