EIP-4573 Procedures for the EVM

No call decision · EVM & execution · EIP status Stagnant · created 2021-12-16

EIP-4573 introduces named 'procedure' units into EVM bytecode, adding five instructions that allow entry only through a fixed entry point and automatically allocate and free the memory needed for each call.

Why it is needed

EVM bytecode currently has no structure, so it is possible to jump anywhere in the code, and subroutines have no defined interface such as a fixed number of inputs and outputs. The memory needed for each call (the stack frame) is also not managed automatically, requiring compilers to emulate it themselves. EIP-4573 aims to partition code into procedure units and clearly define a calling convention, while preserving the safety guaranteed by EIP-3779, namely that execution does not halt with an exception except when gas runs out or the stack overflows recursively.

What changes

Today

After EIP-4573

Who is affected

In depth

Five new instructions

Adds five instructions: ENTERPROC, LEAVEPROC, CALLPROC, RETURNPROC, and FRAMEADDRESS. ENTERPROC takes five immediates, dest_section, dest_offset, n_inputs, n_outputs, and n_locals, marks the entry point of a procedure, and lowers FP by n_locals. LEAVEPROC restores FP and executes RETURNSUB to end the procedure. CALLPROC performs a JUMPSUB to the procedure specified by dest_section and dest_proc and allocates a frame. RETURNPROC returns like LEAVEPROC but is meant for early exit. FRAMEADDRESS pushes the address FP+offset onto the stack, allowing the local variables of the call frame to be read and written with MSTORE/MLOAD.

Redefining memory cost

Because the frame stack starts at address 0 and grows in the negative direction, the existing MSTORE memory size calculation must be revised. The original text proposes treating memory addresses as signed values, tracking positive_memory_size for the region at or above 0 and negative_memory_size for the region below 0 separately, and using their sum as the overall memory_size. This is an adjustment so that the frame stack is reflected in gas cost separately from the positive memory space.

The call frame stack and FP

Frame memory starts at address 0 and grows toward more negative addresses. Each time CALLPROC is called, FP is lowered by the n_locals words that procedure needs, allocating a new frame, and when returning via RETURNPROC or LEAVEPROC, FP moves back up and the frame is freed. If a procedure using two words is called and then, from within it, another procedure using three words is called, two frames stack up, and the original text illustrates with a diagram how they are freed in order starting from the innermost. FP is equivalent, under two's-complement arithmetic, to the common calling convention of starting from the highest address and moving downward.

Relationship to prior proposals

The original text describes this EIP as essentially a more finely broken-down version of EIP-615. ENTERPROC corresponds to EIP-615's BEGINSUB, and like EIP-615 it uses a frame stack tracked via FP to record call frame addresses, but like EIP-3336/3337 it places the call frame in memory rather than on the data stack. The call and return mechanism itself directly uses the JUMPSUB/RETURNSUB defined by EIP-2315. The design model follows the subroutine/procedure structure of Intel x86, with CALLPROC corresponding to a JUMPSUB to ENTERPROC and LEAVEPROC corresponding to executing RETURNSUB to end the procedure.

Safety rules enforced at validation time

The original text specifies that all safety checks must be completed at validation time, not at runtime. ENTERPROC and LEAVEPROC must follow the same safety rules as JUMPSUB/RETURNSUB from EIP-2315. Additionally, every ENTERPROC must end with LEAVEPROC, procedures cannot be nested, jumps from outside into a procedure body (including LEAVEPROC) are prohibited, and jumping to or falling through into ENTERPROC itself is also forbidden. It is also subject to validation that n_inputs specified at the CALLPROC call site must be ready on the data stack, and that n_outputs must be ready on the data stack at RETURNPROC.

Backward compatibility and current status

Since this proposal only adds a new opcode and does not change the behavior of existing opcodes, the original text states there are no backwards compatibility issues. However, according to the frontmatter, the current status is Stagnant, meaning the standardization process has stalled and not progressed further. The requires field specifies EIP-2315, 3540, 3670, 3779, and 4200, meaning this proposal only makes sense once these five proposals are in place.

Original EIP · ← EIPs