-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subroutines and Static Jumps for the EVM #184
Comments
Discussion of concepts here. Edits of live Google Doc here: |
Subroutine Max Frame Size ExtensionIn the current version of the draft we have check for stack overflow in runtime for every executing instruction (in practice it means at every jump). To lower the overhead, we can keep information how much stack is a subrountine using at most and do not allow to call it if the call could cause the stack overflow. That will limit runtime checks only to Specification
|
I'm torn between increasing efficiency and not complicating the interface. Especially don't want to make programmers keep track of the stack use of every routine, even when they are not otherwise tight on stack. One thing to consider is that validation guarantees that determining max frame sizes can be done in one linear-time traversal of the code, just before execution. One such pass is likely anyway for a high-performance EVM. |
I need to digest this some more, but initial reading, I think that while it definitely makes the EVM more complex, it also has the potential to make it both more performant and potentially more safe since formal verification will likely benefit from this. |
Simple Summary
Subroutines.
Abstract
This proposal introduces two opcodes to support subroutines:
JUMPSUB
andRETSUB
.Motivation
The EVM does not provide subroutines as a primitive. Instead, calls must be synthesized by fetching, adjusting and pushing the current program counter and jumping to the subroutine address; returns must be synthesized by getting the return address to the top of stack and jumping back to it.
Specification
Proposal
JUMPSUB
RETSUB
Backwards Compatibility
These changes do not affect existing VM code..
Rationale
This is the smallest possible change that provides native subroutines without breaking backwards compatibility. It does not enforce validity like #615, but code that
_ pushes constants argument before every
JUMP
andJUMPI
and_ ensures that the data stack is the same size at the end of every basic block
is valid and will meet all of the #615 safety conditions. In particular, the Yellow paper defines five exceptional halting states. Valid code cannot violate states 3, 4, or 5.
Implementation
Jumps to and returns from subroutines are described here in terms of
PC
which is the byte offset of the currently executing instruction.Execution of EVM bytecode begins with one value on the return stack—
code_size
. Executing the virtual byte of 0 at this offset causes an EVM to stop. Thus executing aRETSUB
with no priorJUMPSUB
executes aSTOP
. ASTOP
orRETURN
ends the execution of a subroutine.Semantics are defined by the following pseudo-C code.
Costs & Codes
We suggest the cost of
JUMPSUB
should be low, andRETSUB
should be verylow.Measurement will tell.
We suggest the following opcodes:
Copyright and related rights waived via CC0.
The text was updated successfully, but these errors were encountered: