EIPs Explained

EIP-4200 · Stagnant · Core · 2021-07-16

EIP-4200: EOF - Static relative jumps

Adds RJUMP, RJUMPI and RJUMPV to EOF-formatted EVM code: jumps whose targets are fixed relative offsets in the bytecode, so they are validated at deploy time, need no JUMPDEST and cost less gas.

What problem does it solve?

The EVM has only dynamic jumps: JUMP and JUMPI take their destination from the stack. That is flexible, but it means the target can only be checked at run time, every destination needs a JUMPDEST marker, and static analysis of bytecode (for tools, JITs or validators) is hard because any jump could in principle go anywhere.

Yet most control flow a compiler emits (if/else, loops, many function calls) has a target known at compile time. EIP-4200 gives compilers static jumps for those cases, as part of the EVM Object Format (EOF) container introduced by EIP-3540.

How it works

Three opcodes, available only in valid EOF code (in legacy bytecode they cause an exceptional halt, so existing contracts are unaffected):

Opcode Immediate Behavior
RJUMP (0xe0) 16-bit signed offset Jump to PC_post_instruction + offset
RJUMPI (0xe1) 16-bit signed offset Pop condition; jump if non-zero, else fall through
RJUMPV (0xe2) max_index (u8) + max_index+1 offsets Pop case; jump via table, fall through if case > max_index
  • Offsets are two's-complement big-endian and relative to the byte after the whole instruction, so code is relocatable.
  • Deploy-time validation (extending EIP-3670) checks that every target is the start of an instruction: not inside PUSH data or another jump's immediate, and not outside the code section.
  • Because targets are pre-validated, gas is lower: RJUMP costs 2, RJUMPI and RJUMPV cost 4. Targets do not need a JUMPDEST, saving deployment bytes and the 1 gas it costs to pass through one.
  • A 16-bit signed offset covers jumps of up to 32767 bytes, which the authors consider enough given the code size limits of EIP-170 and EIP-3860.
  • Dynamic JUMP is not removed by this EIP; it was expected to remain for things like returning to a caller.

Sample code

A loop that counts down from 3, in EOF assembly with byte offsets shown:

00: PUSH1 0x03        ; counter
02: DUP1              ; loop:  [c, c]
03: ISZERO            ;        [c==0, c]
04: RJUMPI +7         ; if c==0 -> 0x0e (0x07 + 7)
07: PUSH1 0x01
09: SWAP1
0a: SUB               ; c = c - 1
0b: RJUMP -12         ; back to loop at 0x02 (0x0e - 12)
0e: STOP

Encoding of the instructions themselves:

RJUMP  -12        -> e0 ff f4
RJUMPI +7         -> e1 00 07
RJUMPV 1, +3, +9  -> e2 01 00 03 00 09   ; 2-entry table

Note: offsets are computed from the address just after the full instruction (PC_post_instruction); compilers and assemblers do this arithmetic for you.

Things to know

  • Status is Stagnant. It depends on EOF (EIP-3540, EIP-3670), which is also Stagnant; the full EOF package is tracked in EIP-7692.
  • RJUMPV 0 offset behaves as an inverted RJUMPI (jump when the value is zero).
  • If RJUMPV's case is out of range, execution simply continues with the next instruction, so the default branch usually follows it.
  • Relative addressing made the PC opcode unnecessary in EOF code, according to the rationale.

#evm #eof #opcodes #core

Details

Authors
Alex Beregszaszi (@axic), Andrei Maiboroda (@gumb0), Paweł Bylica (@chfast)
Status
Stagnant
Type
Standards Track / Core
Created
2021-07-16
Requires
Required by

Machine-readable: Markdown · JSON