EIP-2315 Simple Subroutines for the EVM

No call decision · EVM & execution · EIP status Withdrawn · created 2019-10-17

A proposal to use two dedicated instructions, RJUMPSUB and RETURNSUB, to handle subroutine calls and returns, making this safer and more gas efficient than the existing JUMP-based emulation and allowing code to be validated at deployment time.

Why it is needed

The EVM has no dedicated subroutine instructions, so until now subroutines have been emulated using the dynamic jumps JUMP and JUMPI, pushing the return address directly onto the data stack. This approach increases instruction count and therefore gas cost, and because the jump destination is only determined at runtime, it is difficult to check the safety of code before deployment. More seriously, dynamic jumps can branch to any point in the code, which can cause a path explosion when tracing control flow, as the number of paths grows exponentially. This can turn the validation process itself into a target for denial-of-service attacks, and verifying safety at initialization or compiling EVM code to machine code requires static control flow with fixed jump destinations.

What changes

Today

After EIP-2315

Who is affected

In depth

Behavior of RJUMPSUB and RETURNSUB

RJUMPSUB (0x5f) reads the 2-byte relative_offset following the PC, pushes the current PC+3 onto the return stack, and moves the PC to PC+relative_offset to enter the subroutine. RETURNSUB (0x5e) pops a value from the return stack and sets it as the PC, returning to just after the call site. relative_offset is a 2-byte signed integer stored MSB first. RJUMPSUB is priced as a low-cost opcode, and RETURNSUB as very-low-cost. The return stack itself is internal state not directly observable by EVM code or consensus rules, and can only be manipulated by RJUMPSUB and RETURNSUB, so there is no need to separately validate popped values.

Why a return stack separate from the data stack was chosen

The original text compares placing the return address on the data stack versus on a dedicated return stack. Some designs, such as those in the CDC, IBM, DEC, and Intel/ARM lineages, keep the return address on the data stack together with other values, but since the EVM has no arithmetic registers, this approach becomes highly inefficient. This proposal instead adopts a dedicated return stack, as used by Forth, Java, and Wasm. This separates computation from control flow, removing the risk that the return address could be tampered with by data stack operations, and it is efficient because it uses native arithmetic instead of 256-bit EVM arithmetic and does not consume data stack slots.

Static constraints that valid code must satisfy

The original text specifies four conditions code must satisfy to be valid: every instruction must be valid (JUMP and JUMPI are no longer valid), every RJUMP, RJUMPI, and RJUMPSUB must target a location within its own code section, the number of items on the data stack and return stack must always be positive, and the data stack height (the difference from the stack pointer at subroutine entry) must be the same for every path reaching a given PC and must not exceed 1024. All of these conditions must be verified before execution, in time and space that is nearly linear in code size.

Gas savings

In the example given in the original text, calling and returning from the SQUARE subroutine costs 41 gas with the JUMP approach, versus 19 gas with the RJUMPSUB/RETURNSUB approach, a 54% reduction. With tail call optimization applied, it is 17 gas versus 38 gas for the JUMP approach, a 55% reduction. The cost of wrapping code in a subroutine itself drops from 30 gas for the JUMP/PUSH/SWAP combination to 8 gas for RJUMPSUB/RETURNSUB, meaning the benefit grows the more code is broken into small subroutines.

Basis for gas cost pricing

RJUMPSUB is priced as low cost rather than JUMP's mid cost because it only needs to decode a 2-byte destination into the PC and push the return address onto the return stack using native arithmetic. RETURNSUB is very-low cost because it only pops a value from the return stack into the PC. JUMP, by contrast, is more expensive because it must go through the data stack, which emulates 256-bit arithmetic. The original text states that whether this cost balance is appropriate requires further benchmarking.

Backward compatibility and dependence on EOF

Invalidating JUMP and JUMPI means bytes that were previously interpreted as valid jump destinations could now be interpreted as immediate data, potentially changing the meaning of existing code. The original text explains that this is not a practical problem because the change is signaled via the Ethereum Object Format (EOF), and states explicitly that this proposal assumes EIP-3540 (EOF container), EIP-3670 (code validation), and EIP-4200 (RJUMP/RJUMPI).

Current status: Withdrawn

The status of this EIP is Withdrawn, and the withdrawal-reason states it was superseded by the EOF proposals. In other words, this design for handling subroutines with RJUMPSUB and RETURNSUB is no longer an active proposal, and the original text explicitly states that subsequent EOF-related proposals took over from it. The provided call records contain no inclusion decision or further debate on this proposal.

Original EIP · ← EIPs