forked from go-interpreter/wagon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.go
377 lines (341 loc) · 13 KB
/
compile.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2017 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package compile is used internally by wagon to convert standard structured
// WebAssembly bytecode into an unstructured form suitable for execution by
// it's VM.
// The conversion process consists of translating block instruction sequences
// and branch operators (br, br_if, br_table) to absolute jumps to PC values.
// For instance, an instruction sequence like:
// loop
// i32.const 1
// get_local 0
// i32.add
// set_local 0
// get_local 1
// i32.const 1
// i32.add
// tee_local 1
// get_local 2
// i32.eq
// br_if 0
// end
// Is "compiled" to:
// i32.const 1
// i32.add
// set_local 0
// get_local 1
// i32.const 1
// i32.add
// tee_local 1
// get_local 2
// i32.eq
// jmpnz <addr> <preserve> <discard>
// Where jmpnz is a jump-if-not-zero operator that takes certain arguments
// plus the jump address as immediates.
// This is in contrast with original WebAssembly bytecode, where the target
// of branch operators are relative block depths instead.
package compile
import (
"bytes"
"encoding/binary"
"github.com/SummerCash/wagon/disasm"
ops "github.com/SummerCash/wagon/wasm/operators"
)
// A small note on the usage of discard instructions:
// A control operator sequence isn't allowed to access nor modify (pop) operands
// that were pushed outside it. Therefore, each sequence has its own stack
// that may or may not push a value to the original stack, depending on the
// block's signature.
// Instead of creating a new stack every time we enter a control structure,
// we record the current stack height on encountering a control operator.
// After we leave the sequence, the stack height is restored using the discard
// operator. A block with a signature will push a value of that type on the parent
// stack (that is, the stack of the parent block where this block started). The
// OpDiscardPreserveTop operator allows us to preserve this value while
// discarding the remaining ones.
// Branches are rewritten as
// <jmp> <addr>
// Where the address is an 8 byte address, initially set to zero. It is
// later "patched" by patchOffset.
var (
// OpJmp unconditionally jumps to the provided address.
OpJmp byte = 0x0c
// OpJmpZ jumps to the given address if the value at the top of the stack is zero.
OpJmpZ byte = 0x03
// OpJmpNz jumps to the given address if the value at the top of the
// stack is not zero. It also discards elements and optionally preserves
// the topmost value on the stack
OpJmpNz byte = 0x0d
// OpDiscard discards a given number of elements from the execution stack.
OpDiscard byte = 0x0b
// OpDiscardPreserveTop discards a given number of elements from the
// execution stack, while preserving the value on the top of the stack.
OpDiscardPreserveTop byte = 0x05
)
// Target is the "target" of a br_table instruction.
// Unlike other control instructions, br_table does jumps and discarding all
// by itself.
type Target struct {
Addr int64 // The absolute address of the target
Discard int64 // The number of elements to discard
PreserveTop bool // Whether the top of the stack is to be preserved
Return bool // Whether to return in order to take this branch/target
}
// BranchTable is the structure pointed to by a rewritten br_table instruction.
// A rewritten br_table instruction is of the format:
// br_table <table_index>
// where <table_index> is the index to an array of
// BranchTable objects stored by the VM.
type BranchTable struct {
Targets []Target // A list of targets, br_table pops an int value, and jumps to Targets[val]
DefaultTarget Target // If val > len(Targets), the VM will jump here
patchedAddrs []int64 // A list of already patched addresses
blocksLen int // The length of the blocks map in Compile when this table was initialized
}
// block stores the information relevant for a block created by a control operator
// sequence (if...else...end, loop...end, and block...end)
type block struct {
// the byte offset to which the continuation of the label
// created by the block operator is located
// for 'loop', this is the offset of the loop operator itself
// for 'if', 'else', 'block', this is the 'end' operator
offset int64
// Whether this block is created by an 'if' operator
// in that case, the 'offset' field is set to the byte offset
// of the else branch, once the else operator is reached.
ifBlock bool
// if ... else ... end is compiled to
// jmpnz <else-addr> ... jmp <end-addr> ... <discard>
// elseAddrOffset is the byte offset of the else-addr address
// in the new/compiled byte buffer.
elseAddrOffset int64
// Whether this block is created by a 'loop' operator
// in that case, the 'offset' field is set at the end of the block
loopBlock bool
patchOffsets []int64 // A list of offsets in the bytecode stream that need to be patched with the correct jump addresses
discard disasm.StackInfo // Information about the stack created in this block, used while creating Discard instructions
branchTables []*BranchTable // All branch tables that were defined in this block.
}
// Compile rewrites WebAssembly bytecode from its disassembly.
// TODO(vibhavp): Add options for optimizing code. Operators like i32.reinterpret/f32
// are no-ops, and can be safely removed.
func Compile(disassembly []disasm.Instr) ([]byte, []*BranchTable) {
buffer := new(bytes.Buffer)
branchTables := []*BranchTable{}
curBlockDepth := -1
blocks := make(map[int]*block) // maps nesting depths (labels) to blocks
blocks[-1] = &block{}
for _, instr := range disassembly {
if instr.Unreachable {
continue
}
switch instr.Op.Code {
case ops.I32Load, ops.I64Load, ops.F32Load, ops.F64Load, ops.I32Load8s, ops.I32Load8u, ops.I32Load16s, ops.I32Load16u, ops.I64Load8s, ops.I64Load8u, ops.I64Load16s, ops.I64Load16u, ops.I64Load32s, ops.I64Load32u, ops.I32Store, ops.I64Store, ops.F32Store, ops.F64Store, ops.I32Store8, ops.I32Store16, ops.I64Store8, ops.I64Store16, ops.I64Store32:
// memory_immediate has two fields, the alignment and the offset.
// The former is simply an optimization hint and can be safely
// discarded.
instr.Immediates = []interface{}{instr.Immediates[1].(uint32)}
case ops.If:
curBlockDepth++
buffer.WriteByte(OpJmpZ)
blocks[curBlockDepth] = &block{
ifBlock: true,
elseAddrOffset: int64(buffer.Len()),
}
// the address to jump to if the condition for `if` is false
// (i.e when the value on the top of the stack is 0)
binary.Write(buffer, binary.LittleEndian, int64(0))
continue
case ops.Loop:
// there is no condition for entering a loop block
curBlockDepth++
blocks[curBlockDepth] = &block{
offset: int64(buffer.Len()),
ifBlock: false,
loopBlock: true,
discard: *instr.NewStack,
}
continue
case ops.Block:
curBlockDepth++
blocks[curBlockDepth] = &block{
ifBlock: false,
discard: *instr.NewStack,
}
continue
case ops.Else:
ifInstr := disassembly[instr.Block.ElseIfIndex] // the corresponding `if` instruction for this else
if ifInstr.NewStack != nil && ifInstr.NewStack.StackTopDiff != 0 {
// add code for jumping out of a taken if branch
if ifInstr.NewStack.PreserveTop {
buffer.WriteByte(OpDiscardPreserveTop)
} else {
buffer.WriteByte(OpDiscard)
}
binary.Write(buffer, binary.LittleEndian, ifInstr.NewStack.StackTopDiff)
}
buffer.WriteByte(OpJmp)
ifBlockEndOffset := int64(buffer.Len())
binary.Write(buffer, binary.LittleEndian, int64(0))
curOffset := int64(buffer.Len())
ifBlock := blocks[curBlockDepth]
code := buffer.Bytes()
buffer = patchOffset(code, ifBlock.elseAddrOffset, curOffset)
// this is no longer an if block
ifBlock.ifBlock = false
ifBlock.patchOffsets = append(ifBlock.patchOffsets, ifBlockEndOffset)
continue
case ops.End:
depth := curBlockDepth
block := blocks[depth]
if instr.NewStack.StackTopDiff != 0 {
// when exiting a block, discard elements to
// restore stack height.
if instr.NewStack.PreserveTop {
// this is true when the block has a
// signature, and therefore pushes
// a value on to the stack
buffer.WriteByte(OpDiscardPreserveTop)
} else {
buffer.WriteByte(OpDiscard)
}
binary.Write(buffer, binary.LittleEndian, instr.NewStack.StackTopDiff)
}
if !block.loopBlock { // is a normal block
block.offset = int64(buffer.Len())
if block.ifBlock {
code := buffer.Bytes()
buffer = patchOffset(code, block.elseAddrOffset, int64(block.offset))
}
}
for _, offset := range block.patchOffsets {
code := buffer.Bytes()
buffer = patchOffset(code, offset, block.offset)
}
for _, table := range block.branchTables {
table.patchTable(table.blocksLen-depth-1, int64(block.offset))
}
delete(blocks, curBlockDepth)
curBlockDepth--
continue
case ops.Br:
if instr.NewStack != nil && instr.NewStack.StackTopDiff != 0 {
if instr.NewStack.PreserveTop {
buffer.WriteByte(OpDiscardPreserveTop)
} else {
buffer.WriteByte(OpDiscard)
}
binary.Write(buffer, binary.LittleEndian, instr.NewStack.StackTopDiff)
}
buffer.WriteByte(OpJmp)
label := int(instr.Immediates[0].(uint32))
block := blocks[curBlockDepth-int(label)]
block.patchOffsets = append(block.patchOffsets, int64(buffer.Len()))
// write the jump address
binary.Write(buffer, binary.LittleEndian, int64(0))
continue
case ops.BrIf:
buffer.WriteByte(OpJmpNz)
label := int(instr.Immediates[0].(uint32))
block := blocks[curBlockDepth-int(label)]
block.patchOffsets = append(block.patchOffsets, int64(buffer.Len()))
// write the jump address
binary.Write(buffer, binary.LittleEndian, int64(0))
var stackTopDiff int64
// write whether we need to preserve the top
if instr.NewStack == nil || !instr.NewStack.PreserveTop || instr.NewStack.StackTopDiff == 0 {
buffer.WriteByte(byte(0))
} else {
stackTopDiff = instr.NewStack.StackTopDiff
buffer.WriteByte(byte(1))
}
// write the number of elements on the stack we need to discard
binary.Write(buffer, binary.LittleEndian, stackTopDiff)
continue
case ops.BrTable:
branchTable := &BranchTable{
// we subtract one for the implicit block created by
// the function body
blocksLen: len(blocks) - 1,
}
targetCount := instr.Immediates[0].(uint32)
branchTable.Targets = make([]Target, targetCount)
for i := range branchTable.Targets {
// The first immediates is the number of targets, so we ignore that
label := int64(instr.Immediates[i+1].(uint32))
branchTable.Targets[i].Addr = label
branch := instr.Branches[i]
branchTable.Targets[i].Return = branch.IsReturn
branchTable.Targets[i].Discard = branch.StackTopDiff
branchTable.Targets[i].PreserveTop = branch.PreserveTop
}
defaultLabel := int64(instr.Immediates[len(instr.Immediates)-1].(uint32))
branchTable.DefaultTarget.Addr = defaultLabel
defaultBranch := instr.Branches[targetCount]
branchTable.DefaultTarget.Return = defaultBranch.IsReturn
branchTable.DefaultTarget.Discard = defaultBranch.StackTopDiff
branchTable.DefaultTarget.PreserveTop = defaultBranch.PreserveTop
branchTables = append(branchTables, branchTable)
for _, block := range blocks {
block.branchTables = append(block.branchTables, branchTable)
}
buffer.WriteByte(ops.BrTable)
binary.Write(buffer, binary.LittleEndian, int64(len(branchTables)-1))
}
buffer.WriteByte(instr.Op.Code)
for _, imm := range instr.Immediates {
err := binary.Write(buffer, binary.LittleEndian, imm)
if err != nil {
panic(err)
}
}
}
// writing nop as the last instructions allows us to branch out of the
// function (ie, return)
addr := buffer.Len()
buffer.WriteByte(ops.Nop)
// patch all references to the "root" block of the function body
for _, offset := range blocks[-1].patchOffsets {
code := buffer.Bytes()
buffer = patchOffset(code, offset, int64(addr))
}
for _, table := range branchTables {
table.patchedAddrs = nil
}
return buffer.Bytes(), branchTables
}
// replace the address starting at start with addr
func patchOffset(code []byte, start int64, addr int64) *bytes.Buffer {
var shift uint
for i := int64(0); i < 8; i++ {
code[start+i] = byte(addr >> shift)
shift += 8
}
buf := new(bytes.Buffer)
buf.Write(code)
return buf
}
func (table *BranchTable) patchTable(block int, addr int64) {
if block < 0 {
panic("Invalid block value")
}
for i, target := range table.Targets {
if !table.isAddr(target.Addr) && target.Addr == int64(block) {
table.Targets[i].Addr = addr
}
}
if table.DefaultTarget.Addr == int64(block) {
table.DefaultTarget.Addr = addr
}
table.patchedAddrs = append(table.patchedAddrs, addr)
}
// Whether the given value is an instruction (or the block depth)
func (table *BranchTable) isAddr(addr int64) bool {
for _, t := range table.patchedAddrs {
if t == addr {
return true
}
}
return false
}