EIP-4750 · Stagnant · Core · 2022-01-10
EIP-4750: EOF - Functions
Part of the EOF upgrade set: splits contract code into function sections called with CALLF/RETF, and bans dynamic JUMP/JUMPI inside EOF code.
What problem does it solve?
In today's EVM every jump is dynamic: the destination is a stack value, so the interpreter must keep a table of valid JUMPDESTs and check each jump at runtime. Compilers like Solidity already emit mostly "static" jumps (PUSH dest; JUMP), but the EVM cannot rely on that. Function calls are emulated by pushing a return address and jumping, which is opaque to analysis tools and blocks optimizations.
EIP-4200 adds relative static jumps, which cover most uses, but not calling into and returning from functions. EIP-4750 adds that missing piece for EVM Object Format (EIP-3540) contracts, so dynamic jumps can be removed entirely.
How it works
- Multiple code sections. An EOF container can hold several code sections, one per function. A type section stores 4 bytes per section:
inputs(uint8),outputs(uint8) andmax_stack_increase(uint16). Section 0 is the entry point: it takes no inputs and must be non-returning. - Return stack. The EVM gains a separate return stack (max 1024 items) holding
(code_section_index, offset)pairs. Functions cannot see the caller's stack items beyond their declared inputs. CALLF(0xe3), 5 gas: takes a 2-byte section index immediate, pushes the return location and jumps to byte 0 of that section. It halts if the call could overflow the 1024-item operand stack or the return stack is full.RETF(0xe4), 3 gas: pops the return stack and continues after theCALLF.- Removed in EOF code:
JUMP,JUMPIandPCbecome invalid;JUMPDESTbecomes a 1-gasNOP. JUMPDEST analysis is no longer needed. - Validation (with EIP-3670 and EIP-5450):
CALLFtargets must exist, relative jumps may not land insideCALLFimmediates, and every section must be reachable. Because stack heights are validated at deploy time, runtime underflow checks (and most overflow checks) are dropped.
In legacy (non-EOF) code CALLF and RETF remain undefined, so existing contracts are unaffected.
Sample code
Illustrative EOF layout with two sections, a main that calls a square function:
type section:
[0] inputs=0 outputs=0x80 (non-returning) max_stack_increase=2
[1] inputs=1 outputs=1 max_stack_increase=1
code section 0 (main):
PUSH1 0x07
CALLF 0x0001 ; e3 0001 -> call section 1 with 1 input
... ; result (0x31) is on the stack
STOP
code section 1 (square):
DUP1
MUL
RETF ; e4 -> back to main, 1 output
Things to know
- Status: Stagnant. It is one piece of the EOF bundle tracked in EIP-7692, which is also Stagnant; EOF has not been activated on Ethereum mainnet.
- Later EOF proposals in the same bundle add
JUMPF(tail calls) and non-returning functions; this EIP refers to those as "separate EIPs". - The spec reserves the high bit of
inputs/outputs, so functions are limited to 127 inputs and outputs, andoutputs == 0x80marks a non-returning function. - Its predecessor approach, subroutines in legacy code (EIP-2315), was withdrawn.