Introduce SWAPN and DUPN which take an immediate value for the depth
Table of Contents
DUP instructions are limited to a stack depth of 16. Introduce two new instructions,
DUPN, which lift this limitation and allow accessing the stack up to depth of 256 items.
While the stack is 1024 items deep, easy access is only possible for the top 16 items. Supporting more local variables is possible via manually keeping them in memory or through a “stack to memory elevation” in a compiler. This can result in complex and inefficient code.
Furthermore, 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.
DUPN will provide an option to compilers to simplify accessing deep stack items at the price of possibly increased gas costs.
We introduce two new instructions:
If the code is legacy bytecode, both of these instructions result in an exceptional halt. (Note: This means no change to behaviour.)
If the code is valid EOF1, the following execution rules apply:
These instructions are followed by an 8-bit immediate value, which we call
imm, and can have a value of 0 to 255. We introduce the variable
n which equals to
imm + 1.
If 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. In both of these cases the EVM stops and all gas is consumed.
DUPNthe stack item at depth
n - 1is duplicated at the top of the stack
SWAPNthe top stack item is swapped with the item at depth
The gas cost for both instructions is set at 3.
This has no effect on backwards compatibility because the opcodes were not previously allocated.
Copyright and related rights waived via CC0.
Please cite this document as:
Alex Beregszaszi, "EIP-663: Unlimited SWAP and DUP instructions [DRAFT]," Ethereum Improvement Proposals, no. 663, July 2017. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-663.