From 20621e073562ee5d423b636fae8b6aa8e38275fa Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 24 May 2023 22:29:46 -0400 Subject: readme and cleanup --- vm/vm.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'vm/vm.go') diff --git a/vm/vm.go b/vm/vm.go index 209ab71..7a6178f 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -11,6 +11,7 @@ type Evm struct { memory Memory pc uint64 stopped bool + returndata []byte } func NewEvm(_code []byte) *Evm { @@ -19,25 +20,36 @@ func NewEvm(_code []byte) *Evm { stopped: true, stack : Stack{}, code: _code, + returndata: nil, } } -func (vm *Evm) Start() { +func (vm *Evm) Run() []byte { vm.stopped = false + fmt.Printf("code: ", vm.code) for !(vm.stopped) { - op := vm.code.Fetch(&(vm.pc), 1)[0] - fmt.Printf("pc: %d | opcode: %x -> string: %s\n", vm.pc, op, Instructions[op].name) - if op >= PUSH1 && op <= PUSH32 { - nb := op - PUSH1 + 1 - fmt.Printf("pushing %d byte value to the stack!\n", vm.pc, nb) - b := vm.code.Fetch(&(vm.pc), uint64(nb)) + s := vm.code.Fetch(&(vm.pc), 1) + op := Opcode(STOP) + if (s != nil) { + op = Opcode(s[0]) + } + + fmt.Printf("pc: %d | opcode: 0x%x -> string: %s\n", vm.pc, op, Instructions[op].name) + push := (&op).IsPush() + if (push != 0) { + b := vm.code.Fetch(&(vm.pc), uint64(push)) x := uint256.NewInt(0) x = x.SetBytes(b) vm.stack.Push(x) } else { - vm.Execute(op) + vm.Execute(byte(op)) } + + if (op == RETURN) { + return vm.returndata + } } + return nil } func (vm *Evm) Execute(op byte) { -- cgit v1.2.3