summaryrefslogtreecommitdiff
path: root/vm/vm.go
blob: 7a6178f8a27b992cad2030b48c89a0239922d9a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main

import (
	"fmt"
	"github.com/holiman/uint256"
)

type Evm struct {
	code Rom
	stack Stack
	memory Memory
	pc uint64
	stopped bool
	returndata []byte
}

func NewEvm(_code []byte) *Evm {
	return &Evm{
		pc: 0,
		stopped: true,
		stack : Stack{},
		code: _code,
		returndata: nil,
	}
}

func (vm *Evm) Run() []byte {
	vm.stopped = false
	fmt.Printf("code: ", vm.code)
	for !(vm.stopped) {
		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(byte(op))
		}

		if (op == RETURN) {
			return vm.returndata
		} 
	}
	return nil
}

func (vm *Evm) Execute(op byte) { 
	Instructions[op].handler(vm)
}