EIP-2315 Simple Subroutines for the EVM

Ready for Review:
Proposal: EIP-2315 Simple Subroutines for the EVM
Requires: EIP-4200: Static relative jumps

This proposal introduces two opcodes to support calling and returning from subroutines:

  • RJUMPSUB relative_offset – relative jump to subroutine
  • RETURNSUB – return to PC after most recent RJUMPSUB.

It deprecates JUMP and JUMPI and ensures, at initialization time, that valid code will not execute invalid instructions or jump to invalid locations, will not underflow stack, will maintain consistent numbers of inputs and outputs for subroutines, and will have bounded stack height in the absence of recursion.

These are very nearly the same guarantees as EIP-615 and EIP-4750 with less complexity and better opportunities for optimized code.

This proposal does not impose any syntax – a subroutine is not a contiguous sequence of bytecode, it is a subgraph of the control-flow graph. This provides more opportunities for optimization, especially for compact layout of code. Since we wish to support one-pass compilers of EVM code to machine code it is crucial that the EVM code be as well optimized as possible.

I still prefer that EOF code sections represent Modules containing multiple procedures rather than being a single Function. This allows for low-level optimizations within a module, but no control flow between modules except via defined interfaces. But this is a discussion for elsewhere

My priorities are:

  1. EIP-4200
  2. EIP-2315
  3. EIP-4750

I will certainly support whatever kind of subroutines we can get. We cannot write one-pass compilers without them.

1 Like