EIP-4200 EOF Static Relative Jumps (EOF - Static relative jumps)
No call decision · EVM & execution · EIP status Stagnant · created 2021-07-16
Adds the RJUMP, RJUMPI, and RJUMPV instructions to the EOF format code, which encode jump destinations as fixed values within the code, providing a branching method that is easier to analyze and cheaper in gas.
Why it is needed
The Ethereum Virtual Machine (EVM) provides only two branch (jump) instructions, JUMP and JUMPI, both of which are dynamic jumps that pop the destination from the stack and compute it during execution. This flexibility makes code analysis complex and requires a JUMPDEST marker instruction to be embedded in the code at every jump destination. In practice, however, a large share of jumps in smart contract code are static branches whose destination is already fixed at compile time. Paying the same verification cost and gas as a dynamic jump in these cases is inefficient. This EIP does not aim to eliminate all dynamic jumps; rather, its goal is to additionally provide compilers with cheaper, easier-to-analyze instructions for branches that resolve statically.
What changes
Today
- The EVM has only two jump instructions, JUMP and JUMPI, both of which are dynamic jumps that compute the destination during execution
- Jump destinations require an embedded JUMPDEST marker, consuming extra bytecode space and gas
- Static branches whose destination is fixed at compile time bear the same analysis and cost burden as dynamic jumps
- Code analyzers (JIT/AOT, etc.) have difficulty tracking jump destinations
After EIP-4200
- Three instructions, RJUMP, RJUMPI, and RJUMPV, are added, specifying the destination as a fixed relative offset within the code
- The destination is verified in advance during code validation, so no runtime check is needed and gas cost is lower (RJUMP 2, RJUMPI/RJUMPV 4)
- Jumps are possible without a JUMPDEST marker, saving 200 gas per destination at deployment
- Works only in EOF1 format code; legacy bytecode is unaffected (no change to halting behavior)
Who is affected
- Compiler developers: Control flow that is determined statically can be expressed with cheaper, easier-to-analyze instructions.
- EVM client implementers: Must add destination validity checks for the new instructions to the EOF container validation logic.
- Smart contract developers: Can reduce code deployment and execution gas costs when using the EOF format.
- Legacy bytecode users: Not affected by this EIP; existing behavior remains unchanged.
In depth
Three new instructions
RJUMP (0xe0), RJUMPI (0xe1), and RJUMPV (0xe2) are introduced at the same block number at which EIP-3540 is activated. RJUMP relative_offset sets the PC to PC_post_instruction + relative_offset. RJUMPI relative_offset pops the condition value from the stack; if it is 0, execution continues as is, otherwise it moves by relative_offset. RJUMPV max_index relative_offset+ pops the case value from the stack; if case is greater than max_index (default path), execution falls through, otherwise it moves by the corresponding relative_offset in the table. In legacy bytecode, all three instructions cause an exceptional halt, so existing behavior is unchanged.
Relative offset encoding method
relative_offset is encoded as a 16-bit signed (two's complement) big-endian value, and PC_post_instruction refers to the PC position after reading the entire immediate value. RJUMPV's encoding is more specific: an 8-bit unsigned max_index value sets the maximum index of the table, followed by max_index+1 relative_offset values, allowing a table of up to 256 entries. Since RJUMPV must have at least one relative_offset, its minimum size is 4 bytes. When max_index is 0, it can effectively serve as an inverted RJUMPI, replacing ISZERO RJUMPI relative_offset.
Destination validation rules
Extends EIP-3670's validation algorithm to confirm that the relative_offset of RJUMP, RJUMPI, and RJUMPV must point to a single instruction. It cannot point into the immediate data portion of a PUSHn or of RJUMP/RJUMPI/RJUMPV itself, nor can it point outside the code section. Pointing to a JUMPDEST is permitted but not required. Because this validation is completed before the code is deployed on chain, there is no need to recheck destination validity at execution time.
Why gas cost is lower
Because the destination is already fixed during pre-deployment validation, no runtime check is needed during execution, so RJUMP is priced at 2 gas and RJUMPI and RJUMPV at 4 gas. In addition, since the JUMPDEST marker itself is no longer needed, 200 gas is saved per jump destination at deployment, and the 1 gas JUMPDEST would otherwise consume on every jump is also saved.
Why relative addressing was chosen
Relative rather than absolute addressing was chosen so that code remains relocatable, meaning it continues to work even if moved, which also makes it possible to insert code fragments at other locations. Previously, achieving the same effect required patterns like PUSHn PC ADD JUMPI; relative addressing removes the need for this workaround and also opens the path to deprecating the PC instruction itself.
Why a 16-bit immediate size was chosen
A 16-bit signed immediate can move up to 32767, so starting an RJUMP at PC=0 can reach PC=32770. Given EIP-170's MAX_CODE_SIZE=24576 and EIP-3860's MAX_INITCODE_SIZE=49152, 16 bits was judged sufficient. An 8-bit immediate would only move 125 to 127 bytes forward or backward, deemed insufficient for jumps between functions, and the design leaves open the option of adding a new opcode, similar to the PUSH instructions, if a larger encoding is ever needed.
Relationship to dynamic jumps
This EIP is not intended to eliminate the existing JUMP and JUMPI entirely but to complement them. Situations that still require dynamic jumps remain, such as returning to a caller, and compilers are expected to mostly use RJUMP and RJUMPI while continuing to use JUMP for returning to callers. This EIP also does not preclude introducing other jump-table-based dynamic branching mechanisms later.
Scope of application and current status
These three instructions work only in EOF1 format code as defined by EIP-3540 and presuppose the extension of the validation algorithm defined by EIP-3670 (requires 3540, 3670). In legacy bytecode, encountering these instructions causes an exceptional halt, so existing behavior is unaffected. According to the original frontmatter, this EIP is currently in Stagnant status and is not an actively progressing proposal.