EIP-4750 Separation of EOF Function Sections (EOF - Functions)
No call decision · EVM & execution · EIP status Stagnant · created 2022-01-10
Places multiple code sections within EOF bytecode, each used as a single function, calling and returning functions via CALLF and RETF, while prohibiting dynamic jumps (JUMP, JUMPI).
Why it is needed
In the current EVM, every jump is a dynamic jump. Since the destination is determined by a stack value, the interpreter must analyze the code before execution to find valid jump destinations, which makes optimization and reducing jump costs difficult. Languages like Solidity mostly generate static jumps in the form of PUSHn..JUMP, but the EVM cannot take advantage of this. EIP-4200 introduced static jump instructions that solved most jump use cases, but did not solve function calls and returns. This EIP eliminates dynamic jumps entirely, instead providing dedicated instructions to call and return from functions, recording the number of inputs and outputs of each function in the specification to make analysis easier, and separating the stack per function so that callers and callees cannot see each other's stacks.
What changes
Today
- All jumps are dynamic jumps like JUMP/JUMPI, requiring the destination to be verified at runtime
- Even when compilers generate static jump patterns, the EVM cannot take advantage of them
- The caller's and callee's stacks are not separated during function calls
- Valid jump destinations must be found via JUMPDEST analysis before execution
After EIP-4750
- Multiple code sections are placed in the EOF container, each used as an independent function
- Functions are called and returned via CALLF/RETF, and JUMP/JUMPI are invalidated
- The type section records the number of inputs and outputs and the maximum stack increase for each function
- JUMPDEST analysis becomes unnecessary, and JUMPDEST is renamed to NOP
Who is affected
- Client teams: EOF code validation, CALLF/RETF execution, and return stack handling must be newly implemented.
- Compiler developers: Code generators in languages like Solidity must be changed to generate CALLF/RETF instead of JUMP for function calls.
- Smart contract developers: EOF code cannot use dynamic jumps and must observe the input/output counts of each function.
- Off-chain tooling developers: Can remove JUMPDEST analysis logic, and can use the NOP instruction for code alignment or benchmarking.
In depth
Type Section
The type section of an EOF container must have a size of n*4 bytes for n code sections. Each entry consists of three values: uint8 inputs, uint8 outputs, and uint16 max_stack_increase. Since inputs and outputs reserve their upper bits, only up to 127 can effectively be used, and the value 0x80 for outputs is used by a separate EIP to mark non-returning functions. Code section 0 must have 0 inputs and must be non-returning. The precise definition of max_stack_increase is covered in EIP-5450.
The return stack and new execution state
A new return stack, separate from the operand stack, is introduced. Each entry consists of the code section index to return to after function execution ends and the offset (PC value) within that section, and the specific encoding method is left to the implementation. The return stack is limited to a maximum of 1024 entries. The EVM also tracks the index of the currently executing code section as current_section_index.
CALLF instruction
CALLF (0xe3) takes a 16-bit big-endian immediate argument target_section_index and costs 5 gas. It does not pop or push values from the operand stack directly, but the input values for the function being called must already be on the stack. If the operand stack size exceeds 1024 minus the target function's max_stack_increase, an exceptional halt occurs, which guarantees that the global stack height limit (1024) is not exceeded after the call. If the return stack is already full with 1024 items, this also causes an exceptional halt. On successful execution, the current section index and the position immediately after the immediate argument are pushed onto the return stack, current_section_index is set to target_section_index, and PC is set to 0, continuing execution in the called section.
The RETF instruction
RETF (0xe4) takes no immediate argument and costs 3 gas. It does not push or pop values on the operand stack directly; instead it pops one item from the return stack and restores current_section_index and PC to that value. Thanks to the validation rule requiring code section 0 to be non-returning, the return stack can never be empty at the point RETF executes.
Code Validation Rules
In addition to EIP-3670's code validation rules applying to all code sections, a code section is invalid if CALLF's immediate argument is greater than or equal to the total number of code sections. It is also invalid if the relative offsets of RJUMP, RJUMPI, or RJUMPV fall outside the section's bounds or point into the two bytes immediately following a CALLF instruction (the immediate argument region). Additionally, there must be no code section unreachable from code section 0 via CALLF or JUMPF. In other words, every code section must be reachable.
Disallowed instructions and the change to JUMPDEST
The dynamic jump instructions JUMP (0x56) and JUMPI (0x57) become invalid, with their opcodes changed to undefined. JUMPDEST (0x5b) keeps its behavior (no effect on the operand stack, PC increments, 1 gas) but is renamed to NOP (no operation). The PC (0x58) instruction also becomes invalid, with its opcode changed to undefined. With dynamic jumps gone, the JUMPDEST analysis procedure that checks in advance whether a jump destination lies inside PUSH data is no longer needed at all.
Code section count and instruction size
The number of code sections is limited to 1024, which is why CALLF uses a 2-byte immediate argument. A 1-byte immediate argument allowing only up to 256 was also discussed, but it was not adopted out of concern that it would leave insufficient room for future expansion.
Current status
This EIP's status is marked Stagnant, and discussion is not actively progressing. The call records provided in the text do not show a separate decision on fork inclusion. This EIP depends on EIP-3540, EIP-3670, and EIP-5450, so it cannot stand on its own without these three EIPs.