EIP-3337 Frame pointer memory access operations (Frame pointer support for memory load and store operations)
No call decision · EVM & execution · EIP status Stagnant · created 2021-03-06
Adds four opcodes to the EVM, SETFP, GETFP, MLOADFP, and MSTOREFP, enabling memory to be read and written at positions relative to a frame pointer.
Why it is needed
The EVM currently has no opcode for reading or writing memory at a relative address. For a compiler to keep temporary data such as local variables in memory, each function must store a 'frame pointer' (the starting position of the current function's memory region) somewhere, then reload and add that value every time it reads or writes a value. This process goes through five opcodes, PUSHn, PUSH1 0, MLOAD, ADD, and MLOAD (or MSTORE), consuming 15 gas and at least 7 bytes of bytecode. Because of this cost, compilers come to prefer placing local variables on the EVM's evaluation stack instead of memory, and since the evaluation stack has a depth limit, this constrains the number of local variables a single function can use.
What changes
Today
- A frame-pointer-relative load requires 5 opcodes, PUSHn, PUSH1 0, MLOAD, ADD, MLOAD, 15 gas, and at least 7 bytes.
- A store likewise requires PUSHn, PUSH1 0, MLOAD, ADD, MSTORE, costing 15 gas and at least 7 bytes.
- This cost difference makes it advantageous to keep local variables on the evaluation stack instead of memory, tying the number of local variables per function to the stack depth.
After EIP-3337
- With PUSH1 x and MLOADFP, just 2 opcodes, a frame-pointer-relative load finishes at 6 gas and at least 3 bytes.
- With PUSHn x and MSTOREFP, just 2 opcodes, a store finishes at 6 gas and at least 3 bytes.
- SETFP and GETFP allow the frame pointer value to be set and read directly, bringing the cost of using memory as local variable storage close to that of the evaluation stack.
Who is affected
- Compiler and language developers: Compilers for EVM-targeting languages such as Solidity and Vyper can generate code that stores local variables in memory using less gas and bytecode.
- EVM client teams: The four new opcodes (SETFP, GETFP, MLOADFP, MSTOREFP) must be implemented and activated starting at FORK_BLOCK.
- Smart contract developers: They gain more freedom from the evaluation stack depth limit that constrained the number of local variables per function.
In depth
What is a frame pointer
This EIP adds a new variable called the 'frame pointer' to the EVM's internal state. It is a signed integer with an initial value of 0. It is used in memory address calculations, but only two opcodes, SETFP and GETFP, directly manipulate it.
SETFP (0x5c)
Pops a value from the stack and stores it as the new frame pointer. The cost is G_low, i.e., 3 gas.
GETFP (0x5d)
Takes no argument and pushes the current frame pointer value onto the stack as is. The cost is also G_low, 3 gas, same as SETFP.
MLOADFP (0x5e)
Behaves the same as MLOAD, but before reading the value from memory, adds the current frame pointer value to the address received from the stack. If the resulting address is negative, it is treated the same as an invalid opcode, consuming all remaining gas and reverting the current execution context. The gas cost is the same as MLOAD.
MSTOREFP (0x5f)
Behaves the same as MSTORE, but adds the frame pointer value to the address to store to. If the resulting address is negative, it is treated as an invalid opcode just like MLOADFP, consuming all gas and reverting the execution context. The gas cost is the same as MSTORE.
Rationale for gas pricing
MLOADFP and MSTOREFP perform only one additional operation, adding the frame pointer to the address, so they follow the same cost scheme as MLOAD and MSTORE. SETFP and GETFP are priced based on existing low-cost opcodes such as PUSH and POP.
Why MSTORE8FP was not included
A frame-pointer version of MSTORE8, which stores in 1-byte units, is expected to be used infrequently, and it was excluded from this proposal to conserve opcode space for the future.
Current status and prerequisites
This EIP's status is Stagnant, and the fork activation block (FORK_BLOCK) is still TBD, so no actual activation schedule has been set. In addition, the preamble's requires field lists EIP-3336, meaning this proposal is hard to stand on its own without EIP-3336's memory model change. No decision to include this EIP in a fork has been confirmed in call records.