-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.go
71 lines (62 loc) · 1.15 KB
/
interpreter.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"io"
"log"
"wasmvm/leb128"
"wasmvm/module"
)
type VM struct {
r io.Reader
stack []int64
local []int64
}
func NewVM(r io.Reader) *VM {
vm := &VM{}
vm.r = r
vm.stack = []int64{}
vm.local = []int64{}
return vm
}
func Exec(index, param1, param2 uint32) {
seg := globalModule.Segments[module.SegmentIDCode]
code := seg.(*module.SegCode)
r := bytes.NewReader(code.FunctionBody[index].Code)
vm := NewVM(r)
vm.local = append(vm.local, int64(param1))
vm.local = append(vm.local, int64(param2))
vm.exec(r)
}
func (vm *VM) push(i int64) {
vm.stack = append(vm.stack, i)
}
func (vm *VM) pop() int64 {
i := vm.stack[len(vm.stack)-1]
vm.stack = vm.stack[:len(vm.stack)-1]
return i
}
func (vm *VM) exec(r io.Reader) {
localCount, err := leb128.ReadVarint32(r)
if nil != err {
panic(err)
}
log.Printf("[exec] localCount:%d", localCount)
for {
op, err := leb128.ReadVarUint32(r)
if nil != err {
panic(err)
}
log.Printf("[exec]opcode:0x%x", op)
switch op {
case GetLocal:
vm.GetLocal()
case I32Add:
vm.I32Add()
case End:
vm.End()
return
default:
log.Printf("unkown opcode")
}
}
}