yield op code for yielding coroutine computation

This commit is contained in:
Brandon Roberts 2023-09-20 17:40:35 -07:00
parent e959d6cee1
commit 9fa627cd54
4 changed files with 29 additions and 1 deletions

View file

@ -817,6 +817,26 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
return ret, nil return ret, nil
} }
func opYield(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
// Save current Environment as coroutine
coroutine := NewCoroutine(*pc + 1, *scope.Stack)
// Push coroutine to the stack
scope.PushCoroutine(coroutine)
log.Printf("Coroutine %d yielded with %v", coroutine.PC, coroutine.Stack)
// Call the next coroutine
nextCoroutine, err := scope.PopCoroutine()
if err != nil {
return nil, err
}
ret, err := nextCoroutine.ExecuteCoroutine(interpreter, scope)
log.Printf("Coroutine %d returned with %v", nextCoroutine.PC, ret)
return nil, errStopToken
}
func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
offset, size := scope.Stack.pop(), scope.Stack.pop() offset, size := scope.Stack.pop(), scope.Stack.pop()
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))

View file

@ -275,7 +275,6 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
err = nil // clear stop token error err = nil // clear stop token error
} }
log2.Println("returning from run with", res, err)
// Res is array of bytes located in scope.Memory // Res is array of bytes located in scope.Memory
return res, err return res, err
} }

View file

@ -1044,6 +1044,12 @@ func newFrontierInstructionSet() JumpTable {
maxStack: maxStack(7, 1), maxStack: maxStack(7, 1),
memorySize: memoryCall, memorySize: memoryCall,
}, },
YIELD: {
execute: opYield,
constantGas: 1, //TODO: set this to something reasonable
minStack: minStack(0, 0),
maxStack: maxStack(0, 0),
},
RETURN: { RETURN: {
execute: opReturn, execute: opReturn,
dynamicGas: gasReturn, dynamicGas: gasReturn,

View file

@ -219,6 +219,7 @@ const (
STATICCALL OpCode = 0xfa STATICCALL OpCode = 0xfa
SPAWN OpCode = 0xfb SPAWN OpCode = 0xfb
YIELD OpCode = 0xfc
REVERT OpCode = 0xfd REVERT OpCode = 0xfd
INVALID OpCode = 0xfe INVALID OpCode = 0xfe
SELFDESTRUCT OpCode = 0xff SELFDESTRUCT OpCode = 0xff
@ -393,6 +394,7 @@ var opCodeToString = map[OpCode]string{
DELEGATECALL: "DELEGATECALL", DELEGATECALL: "DELEGATECALL",
CREATE2: "CREATE2", CREATE2: "CREATE2",
STATICCALL: "STATICCALL", STATICCALL: "STATICCALL",
YIELD: "YIELD",
REVERT: "REVERT", REVERT: "REVERT",
INVALID: "INVALID", INVALID: "INVALID",
SELFDESTRUCT: "SELFDESTRUCT", SELFDESTRUCT: "SELFDESTRUCT",
@ -448,6 +450,7 @@ var stringToOp = map[string]OpCode{
"BLOBHASH": BLOBHASH, "BLOBHASH": BLOBHASH,
"DELEGATECALL": DELEGATECALL, "DELEGATECALL": DELEGATECALL,
"STATICCALL": STATICCALL, "STATICCALL": STATICCALL,
"YIELD": YIELD,
"CODESIZE": CODESIZE, "CODESIZE": CODESIZE,
"CODECOPY": CODECOPY, "CODECOPY": CODECOPY,
"GASPRICE": GASPRICE, "GASPRICE": GASPRICE,