Skip to content

Commit

Permalink
Subroutines (ethereum#2503)
Browse files Browse the repository at this point in the history
* Update eip-615.md

* Update eip-615.md

* Update eip-615.md

MD here is not MD there is junk everywhere.

* Update eip-615.md

* Update eip-615.md

* Add security issue caught by @holiman

* Reconcile opcodes and names with eip-615

* nudge

* email
  • Loading branch information
gcolvin authored and tkstanczak committed Nov 7, 2020
1 parent 7257f90 commit de51fa2
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions EIPS/eip-2315.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ title: Simple Subroutines for the EVM
status: Draft
type: Standards Track
category: Core
author: Greg Colvin
author: Greg Colvin ([email protected])
discussions-to: https://ethereum-magicians.org/t/eip-2315-simple-subroutines-for-the-evm/3941
created: 2019-10-17
---

## Abstract

This proposal introduces two opcodes to support subroutines: `JUMPSUB` and `RETSUB`.
This proposal introduces two opcodes to support subroutines: `JUMPSUB` and `RETURNSUB`.

## Motivation

Expand All @@ -22,10 +22,10 @@ The EVM does not provide subroutines as a primitive. Instead, calls must be syn
##### `JUMPSUB`
Jumps to the address on top of the stack, which must be the offset of a `JUMPDEST`.

##### `RETSUB`
Returns to the instruction after the most recently executed `JUMPSUB` instruction.
##### `RETURNSUB`
Returns to the most recently executed `JUMPSUB` and executes the following instruction.

A program may `JUMPSUB` at most 1023 times without an intervening `RETSUB`.
A program may `JUMPSUB` at most 1023 times without an intervening `RETURNSUB`. A program which executes `RETURNSUB` without no prior `BEGINSUB` will `STOP`.

## Rationale

Expand All @@ -38,19 +38,19 @@ These changes do not affect the semantics of existing EVM code.
## Test Cases
```
step op stack
0 PUSH1 3 []
1 JUMPSUB [3]
4 STOP []
2 JUMPDEST []
3 RETSUB []
0 PUSH1 3 []
1 JUMPSUB [3]
4 STOP []
2 JUMPDEST []
3 RETURNSUB []
```
This code should terminate after 5 steps with an empty stack.
This code should terminate after 4 steps with an empty stack.

## Implementations

No clients have implemented this proposal as of yet.

The new operators proposed here are implemented by the following pseudocode, which adds cases for `JUMPSUB` and `RETSUB` to a simple loop-and-switch interpreter.
The new operators proposed here are implemented by the following pseudocode, which adds cases for `JUMPSUB` and `RETURNSUB` to a simple loop-and-switch interpreter.
```
bytecode[code_size]
data_stack[1024]
Expand All @@ -65,22 +65,25 @@ loop: while PC < code_size {
push(return_stack, PC)
PC = pop(data_stack)
continue loop
case RETSUB:
case RETURNSUB:
PC = pop(return_stack)
}
++PC
}
```
Execution of EVM bytecode begins with one value on the return stack—the size of the bytecode. The virtual byte of 0 at this offset is the EVM `STOP` opcode, so executing a `RETSUB` with no prior `JUMPSUB` executes a `STOP`. A `STOP` or `RETURN` ends the execution of the subroutine and the program.
Execution of EVM bytecode begins with one value on the return stack—the size of the bytecode. The virtual byte of 0 at this offset is the EVM `STOP` opcode, so executing a `RETURNSUB` with no prior `JUMPSUB` executes a `STOP`. A `STOP` or `RETURN` ends the execution of the subroutine and the program.

We suggest the cost of `JUMPSUB` should be _low_, and `RETSUB` should be _verylow_.
### Costs and Codes

We suggest the cost of `JUMPSUB` should be _low_, and `RETURNSUB` should be _verylow_.
Measurement will tell. We suggest the following opcodes:
```
0xbe JUMPSUB
0xbf RETSUB
0xb3 JUMPSUB
0xb7 RETURNSUB
```
## Security Considerations

Program flow analysis frameworks will need to be updated to allow for a new type of branch -`JUMPSUB` - and new type of branching, since a `RETSUB` will cause a jump to a destination which is not a `JUMPDEST`.
Program flow analysis frameworks will need to be updated to allow for a new type of branch -`JUMPSUB` - and new type of branching - `RETURNSUB` will cause a jump to a destination which is
a `JUMPSUB`, not a `JUMPDEST`.

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

0 comments on commit de51fa2

Please sign in to comment.