File: BinaryInstructionEncoder.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (395 lines) | stat: -rw-r--r-- 16,023 bytes parent folder | download
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// swift-format-ignore-file
//// Automatically generated by Utilities/Sources/WasmGen.swift
//// DO NOT EDIT DIRECTLY

import WasmParser
import WasmTypes

/// An instruction encoder that is responsible for encoding opcodes and immediates
/// in Wasm binary format.
protocol BinaryInstructionEncoder: InstructionVisitor {
    /// Encodes an instruction opcode.
    mutating func encodeInstruction(_ opcode: [UInt8]) throws

    // MARK: - Immediates encoding
    mutating func encodeImmediates(blockType: BlockType) throws
    mutating func encodeImmediates(dataIndex: UInt32) throws
    mutating func encodeImmediates(elemIndex: UInt32) throws
    mutating func encodeImmediates(functionIndex: UInt32) throws
    mutating func encodeImmediates(globalIndex: UInt32) throws
    mutating func encodeImmediates(localIndex: UInt32) throws
    mutating func encodeImmediates(memarg: MemArg) throws
    mutating func encodeImmediates(memory: UInt32) throws
    mutating func encodeImmediates(relativeDepth: UInt32) throws
    mutating func encodeImmediates(table: UInt32) throws
    mutating func encodeImmediates(targets: BrTable) throws
    mutating func encodeImmediates(type: ReferenceType) throws
    mutating func encodeImmediates(type: ValueType) throws
    mutating func encodeImmediates(value: IEEE754.Float32) throws
    mutating func encodeImmediates(value: IEEE754.Float64) throws
    mutating func encodeImmediates(value: Int32) throws
    mutating func encodeImmediates(value: Int64) throws
    mutating func encodeImmediates(dstMem: UInt32, srcMem: UInt32) throws
    mutating func encodeImmediates(dstTable: UInt32, srcTable: UInt32) throws
    mutating func encodeImmediates(elemIndex: UInt32, table: UInt32) throws
    mutating func encodeImmediates(typeIndex: UInt32, tableIndex: UInt32) throws
}

// BinaryInstructionEncoder implements the InstructionVisitor protocol to call the corresponding encode method.
extension BinaryInstructionEncoder {
    mutating func visitUnreachable() throws { try encodeInstruction([0x00]) }
    mutating func visitNop() throws { try encodeInstruction([0x01]) }
    mutating func visitBlock(blockType: BlockType) throws {
        try encodeInstruction([0x02])
        try encodeImmediates(blockType: blockType)
    }
    mutating func visitLoop(blockType: BlockType) throws {
        try encodeInstruction([0x03])
        try encodeImmediates(blockType: blockType)
    }
    mutating func visitIf(blockType: BlockType) throws {
        try encodeInstruction([0x04])
        try encodeImmediates(blockType: blockType)
    }
    mutating func visitElse() throws { try encodeInstruction([0x05]) }
    mutating func visitEnd() throws { try encodeInstruction([0x0B]) }
    mutating func visitBr(relativeDepth: UInt32) throws {
        try encodeInstruction([0x0C])
        try encodeImmediates(relativeDepth: relativeDepth)
    }
    mutating func visitBrIf(relativeDepth: UInt32) throws {
        try encodeInstruction([0x0D])
        try encodeImmediates(relativeDepth: relativeDepth)
    }
    mutating func visitBrTable(targets: BrTable) throws {
        try encodeInstruction([0x0E])
        try encodeImmediates(targets: targets)
    }
    mutating func visitReturn() throws { try encodeInstruction([0x0F]) }
    mutating func visitCall(functionIndex: UInt32) throws {
        try encodeInstruction([0x10])
        try encodeImmediates(functionIndex: functionIndex)
    }
    mutating func visitCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws {
        try encodeInstruction([0x11])
        try encodeImmediates(typeIndex: typeIndex, tableIndex: tableIndex)
    }
    mutating func visitReturnCall(functionIndex: UInt32) throws {
        try encodeInstruction([0x12])
        try encodeImmediates(functionIndex: functionIndex)
    }
    mutating func visitReturnCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws {
        try encodeInstruction([0x13])
        try encodeImmediates(typeIndex: typeIndex, tableIndex: tableIndex)
    }
    mutating func visitDrop() throws { try encodeInstruction([0x1A]) }
    mutating func visitSelect() throws { try encodeInstruction([0x1B]) }
    mutating func visitTypedSelect(type: ValueType) throws {
        try encodeInstruction([0x1C])
        try encodeImmediates(type: type)
    }
    mutating func visitLocalGet(localIndex: UInt32) throws {
        try encodeInstruction([0x20])
        try encodeImmediates(localIndex: localIndex)
    }
    mutating func visitLocalSet(localIndex: UInt32) throws {
        try encodeInstruction([0x21])
        try encodeImmediates(localIndex: localIndex)
    }
    mutating func visitLocalTee(localIndex: UInt32) throws {
        try encodeInstruction([0x22])
        try encodeImmediates(localIndex: localIndex)
    }
    mutating func visitGlobalGet(globalIndex: UInt32) throws {
        try encodeInstruction([0x23])
        try encodeImmediates(globalIndex: globalIndex)
    }
    mutating func visitGlobalSet(globalIndex: UInt32) throws {
        try encodeInstruction([0x24])
        try encodeImmediates(globalIndex: globalIndex)
    }
    mutating func visitLoad(_ load: Instruction.Load, memarg: MemArg) throws {
        let opcode: [UInt8]
        switch load {
        case .i32Load: opcode = [0x28]
        case .i64Load: opcode = [0x29]
        case .f32Load: opcode = [0x2A]
        case .f64Load: opcode = [0x2B]
        case .i32Load8S: opcode = [0x2C]
        case .i32Load8U: opcode = [0x2D]
        case .i32Load16S: opcode = [0x2E]
        case .i32Load16U: opcode = [0x2F]
        case .i64Load8S: opcode = [0x30]
        case .i64Load8U: opcode = [0x31]
        case .i64Load16S: opcode = [0x32]
        case .i64Load16U: opcode = [0x33]
        case .i64Load32S: opcode = [0x34]
        case .i64Load32U: opcode = [0x35]
        }

        try encodeInstruction(opcode)
        try encodeImmediates(memarg: memarg)
    }
    mutating func visitStore(_ store: Instruction.Store, memarg: MemArg) throws {
        let opcode: [UInt8]
        switch store {
        case .i32Store: opcode = [0x36]
        case .i64Store: opcode = [0x37]
        case .f32Store: opcode = [0x38]
        case .f64Store: opcode = [0x39]
        case .i32Store8: opcode = [0x3A]
        case .i32Store16: opcode = [0x3B]
        case .i64Store8: opcode = [0x3C]
        case .i64Store16: opcode = [0x3D]
        case .i64Store32: opcode = [0x3E]
        }

        try encodeInstruction(opcode)
        try encodeImmediates(memarg: memarg)
    }
    mutating func visitMemorySize(memory: UInt32) throws {
        try encodeInstruction([0x3F])
        try encodeImmediates(memory: memory)
    }
    mutating func visitMemoryGrow(memory: UInt32) throws {
        try encodeInstruction([0x40])
        try encodeImmediates(memory: memory)
    }
    mutating func visitI32Const(value: Int32) throws {
        try encodeInstruction([0x41])
        try encodeImmediates(value: value)
    }
    mutating func visitI64Const(value: Int64) throws {
        try encodeInstruction([0x42])
        try encodeImmediates(value: value)
    }
    mutating func visitF32Const(value: IEEE754.Float32) throws {
        try encodeInstruction([0x43])
        try encodeImmediates(value: value)
    }
    mutating func visitF64Const(value: IEEE754.Float64) throws {
        try encodeInstruction([0x44])
        try encodeImmediates(value: value)
    }
    mutating func visitRefNull(type: ReferenceType) throws {
        try encodeInstruction([0xD0])
        try encodeImmediates(type: type)
    }
    mutating func visitRefIsNull() throws { try encodeInstruction([0xD1]) }
    mutating func visitRefFunc(functionIndex: UInt32) throws {
        try encodeInstruction([0xD2])
        try encodeImmediates(functionIndex: functionIndex)
    }
    mutating func visitI32Eqz() throws { try encodeInstruction([0x45]) }
    mutating func visitCmp(_ cmp: Instruction.Cmp) throws {
        let opcode: [UInt8]
        switch cmp {
        case .i32Eq: opcode = [0x46]
        case .i32Ne: opcode = [0x47]
        case .i32LtS: opcode = [0x48]
        case .i32LtU: opcode = [0x49]
        case .i32GtS: opcode = [0x4A]
        case .i32GtU: opcode = [0x4B]
        case .i32LeS: opcode = [0x4C]
        case .i32LeU: opcode = [0x4D]
        case .i32GeS: opcode = [0x4E]
        case .i32GeU: opcode = [0x4F]
        case .i64Eq: opcode = [0x51]
        case .i64Ne: opcode = [0x52]
        case .i64LtS: opcode = [0x53]
        case .i64LtU: opcode = [0x54]
        case .i64GtS: opcode = [0x55]
        case .i64GtU: opcode = [0x56]
        case .i64LeS: opcode = [0x57]
        case .i64LeU: opcode = [0x58]
        case .i64GeS: opcode = [0x59]
        case .i64GeU: opcode = [0x5A]
        case .f32Eq: opcode = [0x5B]
        case .f32Ne: opcode = [0x5C]
        case .f32Lt: opcode = [0x5D]
        case .f32Gt: opcode = [0x5E]
        case .f32Le: opcode = [0x5F]
        case .f32Ge: opcode = [0x60]
        case .f64Eq: opcode = [0x61]
        case .f64Ne: opcode = [0x62]
        case .f64Lt: opcode = [0x63]
        case .f64Gt: opcode = [0x64]
        case .f64Le: opcode = [0x65]
        case .f64Ge: opcode = [0x66]
        }

        try encodeInstruction(opcode)
    }
    mutating func visitI64Eqz() throws { try encodeInstruction([0x50]) }
    mutating func visitUnary(_ unary: Instruction.Unary) throws {
        let opcode: [UInt8]
        switch unary {
        case .i32Clz: opcode = [0x67]
        case .i32Ctz: opcode = [0x68]
        case .i32Popcnt: opcode = [0x69]
        case .i64Clz: opcode = [0x79]
        case .i64Ctz: opcode = [0x7A]
        case .i64Popcnt: opcode = [0x7B]
        case .f32Abs: opcode = [0x8B]
        case .f32Neg: opcode = [0x8C]
        case .f32Ceil: opcode = [0x8D]
        case .f32Floor: opcode = [0x8E]
        case .f32Trunc: opcode = [0x8F]
        case .f32Nearest: opcode = [0x90]
        case .f32Sqrt: opcode = [0x91]
        case .f64Abs: opcode = [0x99]
        case .f64Neg: opcode = [0x9A]
        case .f64Ceil: opcode = [0x9B]
        case .f64Floor: opcode = [0x9C]
        case .f64Trunc: opcode = [0x9D]
        case .f64Nearest: opcode = [0x9E]
        case .f64Sqrt: opcode = [0x9F]
        case .i32Extend8S: opcode = [0xC0]
        case .i32Extend16S: opcode = [0xC1]
        case .i64Extend8S: opcode = [0xC2]
        case .i64Extend16S: opcode = [0xC3]
        case .i64Extend32S: opcode = [0xC4]
        }

        try encodeInstruction(opcode)
    }
    mutating func visitBinary(_ binary: Instruction.Binary) throws {
        let opcode: [UInt8]
        switch binary {
        case .i32Add: opcode = [0x6A]
        case .i32Sub: opcode = [0x6B]
        case .i32Mul: opcode = [0x6C]
        case .i32DivS: opcode = [0x6D]
        case .i32DivU: opcode = [0x6E]
        case .i32RemS: opcode = [0x6F]
        case .i32RemU: opcode = [0x70]
        case .i32And: opcode = [0x71]
        case .i32Or: opcode = [0x72]
        case .i32Xor: opcode = [0x73]
        case .i32Shl: opcode = [0x74]
        case .i32ShrS: opcode = [0x75]
        case .i32ShrU: opcode = [0x76]
        case .i32Rotl: opcode = [0x77]
        case .i32Rotr: opcode = [0x78]
        case .i64Add: opcode = [0x7C]
        case .i64Sub: opcode = [0x7D]
        case .i64Mul: opcode = [0x7E]
        case .i64DivS: opcode = [0x7F]
        case .i64DivU: opcode = [0x80]
        case .i64RemS: opcode = [0x81]
        case .i64RemU: opcode = [0x82]
        case .i64And: opcode = [0x83]
        case .i64Or: opcode = [0x84]
        case .i64Xor: opcode = [0x85]
        case .i64Shl: opcode = [0x86]
        case .i64ShrS: opcode = [0x87]
        case .i64ShrU: opcode = [0x88]
        case .i64Rotl: opcode = [0x89]
        case .i64Rotr: opcode = [0x8A]
        case .f32Add: opcode = [0x92]
        case .f32Sub: opcode = [0x93]
        case .f32Mul: opcode = [0x94]
        case .f32Div: opcode = [0x95]
        case .f32Min: opcode = [0x96]
        case .f32Max: opcode = [0x97]
        case .f32Copysign: opcode = [0x98]
        case .f64Add: opcode = [0xA0]
        case .f64Sub: opcode = [0xA1]
        case .f64Mul: opcode = [0xA2]
        case .f64Div: opcode = [0xA3]
        case .f64Min: opcode = [0xA4]
        case .f64Max: opcode = [0xA5]
        case .f64Copysign: opcode = [0xA6]
        }

        try encodeInstruction(opcode)
    }
    mutating func visitConversion(_ conversion: Instruction.Conversion) throws {
        let opcode: [UInt8]
        switch conversion {
        case .i32WrapI64: opcode = [0xA7]
        case .i32TruncF32S: opcode = [0xA8]
        case .i32TruncF32U: opcode = [0xA9]
        case .i32TruncF64S: opcode = [0xAA]
        case .i32TruncF64U: opcode = [0xAB]
        case .i64ExtendI32S: opcode = [0xAC]
        case .i64ExtendI32U: opcode = [0xAD]
        case .i64TruncF32S: opcode = [0xAE]
        case .i64TruncF32U: opcode = [0xAF]
        case .i64TruncF64S: opcode = [0xB0]
        case .i64TruncF64U: opcode = [0xB1]
        case .f32ConvertI32S: opcode = [0xB2]
        case .f32ConvertI32U: opcode = [0xB3]
        case .f32ConvertI64S: opcode = [0xB4]
        case .f32ConvertI64U: opcode = [0xB5]
        case .f32DemoteF64: opcode = [0xB6]
        case .f64ConvertI32S: opcode = [0xB7]
        case .f64ConvertI32U: opcode = [0xB8]
        case .f64ConvertI64S: opcode = [0xB9]
        case .f64ConvertI64U: opcode = [0xBA]
        case .f64PromoteF32: opcode = [0xBB]
        case .i32ReinterpretF32: opcode = [0xBC]
        case .i64ReinterpretF64: opcode = [0xBD]
        case .f32ReinterpretI32: opcode = [0xBE]
        case .f64ReinterpretI64: opcode = [0xBF]
        case .i32TruncSatF32S: opcode = [0xFC, 0x00]
        case .i32TruncSatF32U: opcode = [0xFC, 0x01]
        case .i32TruncSatF64S: opcode = [0xFC, 0x02]
        case .i32TruncSatF64U: opcode = [0xFC, 0x03]
        case .i64TruncSatF32S: opcode = [0xFC, 0x04]
        case .i64TruncSatF32U: opcode = [0xFC, 0x05]
        case .i64TruncSatF64S: opcode = [0xFC, 0x06]
        case .i64TruncSatF64U: opcode = [0xFC, 0x07]
        }

        try encodeInstruction(opcode)
    }
    mutating func visitMemoryInit(dataIndex: UInt32) throws {
        try encodeInstruction([0xFC, 0x08])
        try encodeImmediates(dataIndex: dataIndex)
    }
    mutating func visitDataDrop(dataIndex: UInt32) throws {
        try encodeInstruction([0xFC, 0x09])
        try encodeImmediates(dataIndex: dataIndex)
    }
    mutating func visitMemoryCopy(dstMem: UInt32, srcMem: UInt32) throws {
        try encodeInstruction([0xFC, 0x0A])
        try encodeImmediates(dstMem: dstMem, srcMem: srcMem)
    }
    mutating func visitMemoryFill(memory: UInt32) throws {
        try encodeInstruction([0xFC, 0x0B])
        try encodeImmediates(memory: memory)
    }
    mutating func visitTableInit(elemIndex: UInt32, table: UInt32) throws {
        try encodeInstruction([0xFC, 0x0C])
        try encodeImmediates(elemIndex: elemIndex, table: table)
    }
    mutating func visitElemDrop(elemIndex: UInt32) throws {
        try encodeInstruction([0xFC, 0x0D])
        try encodeImmediates(elemIndex: elemIndex)
    }
    mutating func visitTableCopy(dstTable: UInt32, srcTable: UInt32) throws {
        try encodeInstruction([0xFC, 0x0E])
        try encodeImmediates(dstTable: dstTable, srcTable: srcTable)
    }
    mutating func visitTableFill(table: UInt32) throws {
        try encodeInstruction([0xFC, 0x11])
        try encodeImmediates(table: table)
    }
    mutating func visitTableGet(table: UInt32) throws {
        try encodeInstruction([0x25])
        try encodeImmediates(table: table)
    }
    mutating func visitTableSet(table: UInt32) throws {
        try encodeInstruction([0x26])
        try encodeImmediates(table: table)
    }
    mutating func visitTableGrow(table: UInt32) throws {
        try encodeInstruction([0xFC, 0x0F])
        try encodeImmediates(table: table)
    }
    mutating func visitTableSize(table: UInt32) throws {
        try encodeInstruction([0xFC, 0x10])
        try encodeImmediates(table: table)
    }
}