-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.go
230 lines (207 loc) · 4.94 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package wasm_go
import "fmt"
type Interpreter struct {
frameStack stack[frame]
valueStack stack[Value]
store store
mod moduleInst
}
func NewInterpreter(bytes []byte) (Interpreter, error) {
p := newParser(bytes)
m, err := p.parse()
i := Interpreter{}
if err != nil {
return i, err
}
store, modInst, err := newStoreAndModuleInst(&i.valueStack, m)
if err != nil {
return i, err
}
i.store = store
i.mod = modInst
return i, nil
}
func (i *Interpreter) Execute() error {
for !i.frameStack.isEmpty() {
frame, _ := i.frameStack.Peek(0)
instr := frame.insts[frame.pc]
if err := instr.exec(&i.frameStack, &i.valueStack, &i.store); err != nil {
return err
}
}
return nil
}
func (i *Interpreter) GetFunc(fnName string) (func(args []Value) ([]Value, error), error) {
fnIdx := -1
for _, export := range i.mod.exports {
if export.name == fnName {
if export.value.kind != exportImportKindFunc {
return nil, fmt.Errorf("%s not a func", fnName)
}
fnIdx = int(export.value.idx)
break
}
}
if fnIdx < 0 {
return nil, fmt.Errorf("can't find %s func", fnName)
}
fnAddr := i.mod.funcAddrs[fnIdx]
fn := i.store.funcs[fnAddr]
if fn.kind == externalFunc {
// TODO: external func
}
return func(args []Value) ([]Value, error) {
i.frameStack.Push(frame{
pc: 0,
sp: i.valueStack.Len(),
insts: fn.internalFunc.code.body,
mod: &i.mod,
})
for x := len(args) - 1; x >= 0; x-- {
i.valueStack.Push(args[x])
}
err := i.Execute()
if err != nil {
// cleanup valueStack and frameStack
i.frameStack = stack[frame]{}
i.valueStack = stack[Value]{}
return nil, err
}
results := make([]Value, len(fn.funcType.results))
for x := 0; x < len(fn.funcType.results); x++ {
ret, _ := i.valueStack.Pop()
results[x] = ret
}
return results, nil
}, nil
}
// https://webassembly.github.io/spec/core/exec/runtime.html#store
type store struct {
funcs []funcInst
tables []tableInst
mems []memInst
globals []globalInst
elems []elemInst
datas []dataInst
}
func newStoreAndModuleInst(
valueStack *stack[Value],
m module,
) (store, moduleInst, error) {
s := store{}
modInst := moduleInst{}
eval := func(expr expr) (Value, error) {
frameStack := stack[frame]{}
// mock frame
frameStack.Push(frame{
pc: 0,
sp: valueStack.Len(),
mod: &modInst,
})
for _, i := range expr {
if err := i.exec(&frameStack, valueStack, &s); err != nil {
return Value{}, err
}
}
v, _ := valueStack.Pop()
frameStack.Pop()
return v, nil
}
for i, g := range m.globals {
gv, err := eval(g.initExpr)
if err != nil {
return s, modInst, err
}
modInst.globalAddrs = append(modInst.globalAddrs, uint32(i))
s.globals = append(s.globals, globalInst{
globalType: g.type_,
value: gv,
})
}
for i, f := range m.funcs {
modInst.funcAddrs = append(modInst.funcAddrs, uint32(i))
s.funcs = append(s.funcs, funcInst{
funcType: m.types[f.typeIdx],
kind: internalFunc,
internalFunc: internalFuncInst{
module: &modInst,
code: f,
},
})
}
for i, mem := range m.mems {
min := mem.limits.Min * uint32(PAGE_SIZE)
modInst.memAddrs = append(modInst.memAddrs, uint32(i))
s.mems = append(s.mems, memInst{
memType: memType{limits: mem.limits},
data: make([]byte, min),
})
}
for i := range m.elems {
modInst.elemAddrs = append(modInst.elemAddrs, uint32(i))
}
for i, tab := range m.tables {
elems := make([]ref, tab.limits.Min)
modInst.tableAddrs = append(modInst.tableAddrs, uint32(i))
for _, elem := range m.elems {
offsetVal, err := eval(elem.offset)
offset := int(offsetVal.I32())
if err != nil {
return s, modInst, err
}
if len(elems) <= offset+len(elem.init) {
originalElems := elems
elems = make([]ref, offset+len(elem.init))
copy(elems, originalElems)
}
for i, funcIdx := range elem.init {
elems[i+offset] = ref{addr: int(funcIdx), kind: refFunc}
}
}
s.tables = append(s.tables, tableInst{
tableType: tableType{
limits: tab.limits,
elemType: tab.elemType,
},
elems: elems,
})
}
for i, data := range m.datas {
modInst.dataAddrs = append(modInst.dataAddrs, uint32(i))
offsetVal, err := eval(data.offset)
if err != nil {
return s, modInst, err
}
offset := int(offsetVal.I32())
mem := s.mems[data.memIdx]
if len(mem.data) < offset+len(data.init) {
return s, modInst, fmt.Errorf("data is too large to fit in memory")
}
copy(mem.data[offset:], data.init)
}
for _, export := range m.exports {
modInst.exports = append(modInst.exports, exportInst{
name: export.name,
value: externalVal{
kind: export.kind,
idx: uint32(export.idx),
},
})
}
modInst.signatures = m.types
return s, modInst, nil
}
type frame struct {
// current instruction position.
pc int
// value stack pointer
sp int
// function instructions
insts []instr
// labels for if, loop, block
labels stack[label]
mod *moduleInst
}
func (f *frame) NextStep() {
f.pc += 1
}