This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
/
Branch.go
313 lines (288 loc) · 7.83 KB
/
Branch.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package instructions
import (
"fmt"
"github.com/iotaledger/wart/wasm/consts/op"
"github.com/iotaledger/wart/wasm/consts/value"
"github.com/iotaledger/wart/wasm/executors/context"
"github.com/iotaledger/wart/wasm/instructions/helper"
)
type Branch struct {
Base
labelIndex uint32
table []uint32
}
func NewBranch() helper.Instruction {
return &Branch{}
}
func (o *Branch) Analyze(a *context.Analyzer) {
switch o.opcode {
//@formatter:off
case op.BR : o.analyzeBr(a)
case op.BR_IF : o.analyzeBrIf(a)
case op.BR_TABLE: o.analyzeBrTable(a)
case op.RETURN : o.analyzeReturn(a)
//@formatter:on
default:
panic("Invalid branch opcode")
}
}
func (o *Branch) analyzeBr(a *context.Analyzer) {
if /* o.labelIndex < 0 || */ o.labelIndex >= uint32(len(a.Labels)) {
a.Error = o.fail("unknown label")
return
}
targetLabel := a.Labels[o.labelIndex]
valueTypes := targetLabel.BlockType.ResultTypes
if targetLabel.BlockInst != nil && targetLabel.BlockInst.Opcode() == op.LOOP {
valueTypes = targetLabel.BlockType.ParamTypes
}
a.PopMulti(valueTypes)
if a.Error != nil {
return
}
resultSP := a.SP
currentLabel := a.Labels[0]
if currentLabel.Unreachable {
// unreachable, no need to set up a runner
return
}
// unconditional jump, rest of block became unreachable
currentLabel.Unreachable = true
a.SP = a.BlockMark
// now set up the runner for this branch instruction
if targetLabel.Target == nil {
// this is a return from function, no need to unwind
// anything, just point to result when returning
o.run = func(vm *Runner) { vm.SP = resultSP }
o.next = nil
return
}
o.next = targetLabel.Target.(ctxInstruction)
if resultSP == targetLabel.UnwindSP {
// nothing to move, nothing to do
o.run = func(vm *Runner) {}
return
}
if resultSP+1 == targetLabel.UnwindSP {
// move single value, directly copy that single value
o.unwindSingleValue(a, resultSP, targetLabel.UnwindSP)
return
}
// slower copy loop, only done when moving multiple values
dst := targetLabel.UnwindSP
resultTypes := valueTypes
o.run = func(vm *Runner) {
for i := 0; i < len(resultTypes); i++ {
vm.Frame[dst+i].Copy(&vm.Frame[resultSP+i], resultTypes[i])
}
}
}
func (o *Branch) analyzeBrIf(a *context.Analyzer) {
if /* o.labelIndex < 0 || */ o.labelIndex >= uint32(len(a.Labels)) {
a.Error = o.fail("unknown label")
return
}
targetLabel := a.Labels[o.labelIndex]
valueTypes := targetLabel.BlockType.ResultTypes
if targetLabel.BlockInst != nil && targetLabel.BlockInst.Opcode() == op.LOOP {
valueTypes = targetLabel.BlockType.ParamTypes
}
conditionSP := o.SP
a.PopMulti(valueTypes)
if a.Error != nil {
return
}
resultSP := a.SP
a.PushMulti(valueTypes)
currentLabel := a.Labels[0]
if currentLabel.Unreachable {
// unreachable, no need to set up a runner
return
}
// now set up the runner for this branch instruction
nextInstruction := o.next
if targetLabel.Target == nil {
// this is a return from function, no need to unwind
// anything, just point to result when returning
o.run = func(vm *Runner) {
if vm.Frame[conditionSP].I32 == 0 {
// continue to next instruction instead of branching to label target
vm.Next = nextInstruction
return
}
vm.SP = resultSP
}
o.next = nil
return
}
o.next = targetLabel.Target.(ctxInstruction)
if resultSP == targetLabel.UnwindSP {
// nothing to unwind
o.run = func(vm *Runner) {
if vm.Frame[conditionSP].I32 == 0 {
// continue to next instruction instead of branching to label target
vm.Next = nextInstruction
}
}
return
}
dst := targetLabel.UnwindSP
resultTypes := valueTypes
o.run = func(vm *Runner) {
if vm.Frame[conditionSP].I32 == 0 {
// continue to next instruction instead of branching to label target
vm.Next = nextInstruction
return
}
// unwind stack
for i := 0; i < len(resultTypes); i++ {
vm.Frame[dst+i].Copy(&vm.Frame[resultSP+i], resultTypes[i])
}
}
}
func (o *Branch) analyzeBrTable(a *context.Analyzer) {
if /* o.labelIndex < 0 || */ o.labelIndex >= uint32(len(a.Labels)) {
a.Error = o.fail("unknown label")
return
}
targetLabel := a.Labels[o.labelIndex]
valueTypes := targetLabel.BlockType.ResultTypes
if targetLabel.BlockInst != nil && targetLabel.BlockInst.Opcode() == op.LOOP {
valueTypes = targetLabel.BlockType.ParamTypes
}
for _, index := range o.table {
if /* index < 0 || */ index >= uint32(len(a.Labels)) {
a.Error = o.fail("unknown label")
return
}
// all labels should have the same target type
tableLabel := a.Labels[index]
tableTypes := tableLabel.BlockType.ResultTypes
if tableLabel.BlockInst != nil && tableLabel.BlockInst.Opcode() == op.LOOP {
tableTypes = tableLabel.BlockType.ParamTypes
}
if len(tableTypes) != len(valueTypes) {
a.Error = o.fail("type mismatch")
return
}
for i := 0; i < len(tableTypes); i++ {
if tableTypes[i] != valueTypes[i] {
a.Error = o.fail("type mismatch")
return
}
}
}
a.PopMulti(valueTypes)
if a.Error != nil {
return
}
resultSP := a.SP
currentLabel := a.Labels[0]
if currentLabel.Unreachable {
// unreachable, no need to set up a runner
return
}
// unconditional jump, rest of block became unreachable
currentLabel.Unreachable = true
a.SP = a.BlockMark
// now set up the runner for this branch instruction
targets := make([]*context.Label, len(a.Labels))
for i := 0; i < len(targets); i++ {
targets[i] = a.Labels[i]
}
table := o.table
sp := o.SP
labelIndex := o.labelIndex
resultTypes := valueTypes
o.run = func(vm *Runner) {
targetIndex := labelIndex
index := uint32(vm.Frame[sp].I32)
if /* index >= 0 && */ index < uint32(len(table)) {
targetIndex = table[index]
}
target := targets[targetIndex]
vm.Next = target.Target
if vm.Next == nil {
// this is a return from function, no need to unwind
// anything, just point to result when returning
vm.SP = resultSP
return
}
// unwind stack
for i := 0; i < len(resultTypes); i++ {
vm.Frame[target.UnwindSP+i].Copy(&vm.Frame[resultSP+i], resultTypes[i])
}
}
}
func (o *Branch) analyzeReturn(a *context.Analyzer) {
// shorthand for break to outermost block
o.labelIndex = uint32(len(a.Labels) - 1)
o.analyzeBr(a)
}
func (o *Branch) Read(r *context.Reader) {
switch o.opcode {
case op.BR, op.BR_IF:
o.labelIndex = r.GetU32()
case op.BR_TABLE:
count := r.GetU32()
if r.Error != nil {
return
}
o.table = make([]uint32, 0, count)
for ; count != 0; count-- {
index := r.GetU32()
if r.Error != nil {
return
}
o.table = append(o.table, index)
}
o.labelIndex = r.GetU32()
case op.RETURN:
default:
panic("Invalid branch opcode")
}
}
func (o *Branch) String() string {
switch o.opcode {
case op.BR, op.BR_IF:
return fmt.Sprintf("%s %d", o.Mnemonic(), o.labelIndex)
case op.BR_TABLE:
// create "[x, y,..., z]"
values := fmt.Sprintf("%v", o.table)
// strip off brackets [ and ]
values = values[1 : len(values)-1]
return fmt.Sprintf("%s %s %d", o.Mnemonic(), values, o.labelIndex)
case op.RETURN:
return o.Mnemonic()
default:
panic("Invalid branch opcode")
}
}
func (o *Branch) unwindSingleValue(a *context.Analyzer, src int, dst int) {
switch a.Frame[src] {
//@formatter:off
case value.I32: o.run = func(vm *Runner) { vm.Frame[dst].I32 = vm.Frame[src].I32 }
case value.I64: o.run = func(vm *Runner) { vm.Frame[dst].I64 = vm.Frame[src].I64 }
case value.F32: o.run = func(vm *Runner) { vm.Frame[dst].F32 = vm.Frame[src].F32 }
case value.F64: o.run = func(vm *Runner) { vm.Frame[dst].F64 = vm.Frame[src].F64 }
//@formatter:on
default:
panic("Invalid utils.Errordata type")
}
}
func (o *Branch) Write(w *context.Writer) {
switch o.opcode {
case op.BR, op.BR_IF:
w.PutU32(o.labelIndex)
case op.BR_TABLE:
w.PutU32(uint32(len(o.table)))
for _, index := range o.table {
w.PutU32(index)
}
w.PutU32(o.labelIndex)
case op.RETURN:
return
default:
panic("Invalid branch opcode")
}
}