EIP-6206 EOF Tail Calls and Non-returning Functions (EOF - JUMPF and non-returning functions)
No call decision · EVM & execution · EIP status Stagnant · created 2022-12-21
Adds a JUMPF instruction that, within an EOF (EIP-4750) function, jumps directly to another code section without altering the return stack, optimizing the case where a function ends by simply calling another function.
Why it is needed
EOF functions currently only call other functions with CALLF and return with RETF. However, a pattern where a function ends by just calling another function and finishing there (a tail call) is common. In particular, when short helper functions used in common across many call sites, such as error handling, end with REVERT, the current approach requires pushing a new return frame before the call and carefully adjusting the values left on the stack. This EIP adds a JUMPF instruction that does not push a return stack frame, along with a way to declare that a function never returns, allowing such code to be made smaller and cheaper in gas.
What changes
Today
- Calling another function at the end of a function requires pushing a new return frame via CALLF
- Before calling a common error helper, the values left on the stack must be adjusted precisely
- Functions that never return are validated the same way as functions that can return, creating unnecessary constraints
After EIP-6206
- JUMPF can jump directly to another code section without touching the return stack
- Declaring a section as 'non-returning' reduces the burden of adjusting the stack before the call
- Compilers can generate smaller, cheaper bytecode for tail calls and common error handling
Who is affected
- EOF compiler and language developers: Tail calls and common error helpers can be implemented with less code and gas.
- EVM client implementation teams: The JUMPF instruction and non-returning section validation rules must be newly implemented in the EOF container validation logic.
- Smart contract developers: The cost of calls at the end of functions decreases in EOF-based contracts.
In depth
JUMPF Instruction Specification
JUMPF (0xe5) takes a single 16-bit big-endian immediate, target_section_index. If the operand stack size would exceed 1024 - type[target_section_index].max_stack_increase, it is treated as an exceptional halt because it could exceed the global stack height limit. When executed, it changes current_section_index to target_section_index and PC to 0, but does not touch the return stack. The gas cost is 5, and it does not pop or push any values on the operand stack.
Declaring a Non-returning Section (outputs=0x80)
Setting the special value 0x80 in the outputs field of the type section means that code section is a 'non-returning' section that never returns control to the caller. The first code section must have inputs of 0 and must be non-returning.
JUMPF Stack Validation Rules
If the destination is a returning section, both stack_height_min and stack_height_max must equal type[current].outputs - type[target].outputs + type[target].inputs. If the destination is a non-returning section (outputs=0x80), stack_height_min need only be at least type[target].inputs. In either case, stack_height_max must be at most 1024 - types[target].max_stack_increase.
CALLF Validation Extension
If CALLF's immediate target_section_index points to a non-returning section (outputs=0x80), the code is treated as invalid. Since a non-returning section never returns, it cannot be called via CALLF and can only be entered via JUMPF.
Rules for Determining Non-returning Status
For a section to be non-returning, it must contain no RETF instruction and no JUMPF targeting a returning section. Under this rule, a section that only JUMPFs to non-returning sections automatically becomes non-returning itself.
Terminating Instruction Handling and RJUMP* Constraints
JUMPF is treated as a terminating instruction, so code validation does not require a following instruction, and it can be the last instruction in a section. Additionally, if an EIP-4200 RJUMP* offset points to the two bytes immediately following JUMPF, that code fails validation.
Allowing Sections with Different Output Counts and Legacy Compatibility
The outputs of a JUMPF destination section may be fewer than those of the current section, provided the current section leaves that many extra values directly on the stack. This allows the same helper section to be shared across multiple functions with different numbers of outputs. In legacy bytecode, encountering JUMPF is treated as an exceptional halt, leaving existing behavior unchanged, and since EOF prohibits the use of undefined instructions, this change is backwards compatible. This EIP requires EIP-4750 and EIP-5450.