watr 5.0.0 → 5.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/watr.js +825 -907
- package/dist/watr.min.js +5 -6
- package/dist/watr.wasm +0 -0
- package/package.json +3 -3
- package/readme.md +19 -5
- package/src/compile.js +236 -110
- package/src/const.js +125 -131
- package/src/encode.js +28 -3
- package/src/optimize.js +2909 -421
- package/src/print.js +8 -3
- package/src/util.js +33 -9
- package/types/src/compile.d.ts +1 -0
- package/types/src/compile.d.ts.map +1 -1
- package/types/src/const.d.ts +2 -1
- package/types/src/const.d.ts.map +1 -1
- package/types/src/encode.d.ts +5 -26
- package/types/src/encode.d.ts.map +1 -1
- package/types/src/optimize.d.ts +41 -33
- package/types/src/optimize.d.ts.map +1 -1
- package/types/src/print.d.ts.map +1 -1
- package/types/src/util.d.ts +5 -10
- package/types/src/util.d.ts.map +1 -1
package/src/const.js
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
// https://webassembly.github.io/spec/core/appendix/index-instructions.html
|
|
2
|
-
// Format: 'name' or 'name
|
|
2
|
+
// Format: a flat dense list — a STRING 'name' or 'name immtype' takes the next opcode;
|
|
3
|
+
// a NUMBER repositions the cursor (plain opcode, or (prefix << 16) | subopcode for the
|
|
4
|
+
// multi-byte pages 0xfb GC / 0xfc bulk+rounding / 0xfd SIMD / 0xfe atomics).
|
|
3
5
|
// Immediate types: blocktype, labelidx, funcidx, typeidx, tableidx, memoryidx, globalidx, localidx, dataidx, elemidx, multi
|
|
4
6
|
// Value types: i32, i64, f32, f64, v128
|
|
5
|
-
|
|
7
|
+
const TABLE = [
|
|
6
8
|
// 0x00-0x0a: control
|
|
7
|
-
'unreachable', 'nop', 'block block', 'loop block', 'if block', 'else
|
|
9
|
+
'unreachable', 'nop', 'block block', 'loop block', 'if block', 'else', 'then', 0x08, 'throw tagidx', 0x0a, 'throw_ref',
|
|
8
10
|
// 0x0b-0x19: control
|
|
9
|
-
'end end', 'br labelidx', 'br_if labelidx', 'br_table br_table', 'return', 'call funcidx', 'call_indirect call_indirect', 'return_call funcidx', 'return_call_indirect call_indirect', 'call_ref typeidx', 'return_call_ref typeidx',
|
|
11
|
+
'end end', 'br labelidx', 'br_if labelidx', 'br_table br_table', 'return', 'call funcidx', 'call_indirect call_indirect', 'return_call funcidx', 'return_call_indirect call_indirect', 'call_ref typeidx', 'return_call_ref typeidx',
|
|
10
12
|
// 0x1a-0x1f: parametric
|
|
11
|
-
'drop', 'select select',
|
|
13
|
+
0x1a, 'drop', 'select select', 0x1f, 'try_table try_table',
|
|
12
14
|
// 0x20-0x27: variable
|
|
13
|
-
'local.get localidx', 'local.set localidx', 'local.tee localidx', 'global.get globalidx', 'global.set globalidx', 'table.get tableidx', 'table.set tableidx',
|
|
15
|
+
'local.get localidx', 'local.set localidx', 'local.tee localidx', 'global.get globalidx', 'global.set globalidx', 'table.get tableidx', 'table.set tableidx',
|
|
14
16
|
// 0x28-0x3e: memory
|
|
15
|
-
'i32.load memarg', 'i64.load memarg', 'f32.load memarg', 'f64.load memarg',
|
|
17
|
+
0x28, 'i32.load memarg', 'i64.load memarg', 'f32.load memarg', 'f64.load memarg',
|
|
16
18
|
'i32.load8_s memarg', 'i32.load8_u memarg', 'i32.load16_s memarg', 'i32.load16_u memarg',
|
|
17
19
|
'i64.load8_s memarg', 'i64.load8_u memarg', 'i64.load16_s memarg', 'i64.load16_u memarg', 'i64.load32_s memarg', 'i64.load32_u memarg',
|
|
18
20
|
'i32.store memarg', 'i64.store memarg', 'f32.store memarg', 'f64.store memarg',
|
|
@@ -45,135 +47,127 @@ export const INSTR = [
|
|
|
45
47
|
'f64.convert_i32_s', 'f64.convert_i32_u', 'f64.convert_i64_s', 'f64.convert_i64_u', 'f64.promote_f32',
|
|
46
48
|
'i32.reinterpret_f32', 'i64.reinterpret_f64', 'f32.reinterpret_i32', 'f64.reinterpret_i64',
|
|
47
49
|
// 0xc0-0xc4: sign extension
|
|
48
|
-
'i32.extend8_s', 'i32.extend16_s', 'i64.extend8_s', 'i64.extend16_s', 'i64.extend32_s',
|
|
50
|
+
'i32.extend8_s', 'i32.extend16_s', 'i64.extend8_s', 'i64.extend16_s', 'i64.extend32_s',
|
|
49
51
|
// 0xd0-0xd6: reference
|
|
50
|
-
'ref.null ref_null', 'ref.is_null', 'ref.func funcidx', 'ref.eq', 'ref.as_non_null', 'br_on_null labelidx', 'br_on_non_null labelidx',
|
|
51
|
-
// 0xd7-0xdf: padding (9 empty slots)
|
|
52
|
-
, , , , , , , , ,
|
|
52
|
+
0xd0, 'ref.null ref_null', 'ref.is_null', 'ref.func funcidx', 'ref.eq', 'ref.as_non_null', 'br_on_null labelidx', 'br_on_non_null labelidx',
|
|
53
53
|
// 0xe0-0xe6: stack switching (Phase 3)
|
|
54
|
-
'cont.new typeidx', 'cont.bind cont_bind', 'suspend tagidx', 'resume resume', 'resume_throw resume_throw', 'resume_throw_ref resume_throw_ref', 'switch switch_cont',
|
|
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
|
-
'i32x4.abs', 'i32x4.neg', , 'i32x4.all_true', 'i32x4.bitmask', , , 'i32x4.extend_low_i16x8_s', 'i32x4.extend_high_i16x8_s',
|
|
137
|
-
'i32x4.extend_low_i16x8_u', 'i32x4.extend_high_i16x8_u', 'i32x4.shl', 'i32x4.shr_s', 'i32x4.shr_u', 'i32x4.add', , , 'i32x4.sub', , , ,
|
|
138
|
-
'i32x4.mul', 'i32x4.min_s', 'i32x4.min_u', 'i32x4.max_s', 'i32x4.max_u', 'i32x4.dot_i16x8_s', ,
|
|
139
|
-
'i32x4.extmul_low_i16x8_s', 'i32x4.extmul_high_i16x8_s', 'i32x4.extmul_low_i16x8_u', 'i32x4.extmul_high_i16x8_u',
|
|
140
|
-
'i64x2.abs', 'i64x2.neg', , 'i64x2.all_true', 'i64x2.bitmask', , , 'i64x2.extend_low_i32x4_s', 'i64x2.extend_high_i32x4_s',
|
|
141
|
-
'i64x2.extend_low_i32x4_u', 'i64x2.extend_high_i32x4_u', 'i64x2.shl', 'i64x2.shr_s', 'i64x2.shr_u', 'i64x2.add', , , 'i64x2.sub', , , ,
|
|
142
|
-
'i64x2.mul', 'i64x2.eq', 'i64x2.ne', 'i64x2.lt_s', 'i64x2.gt_s', 'i64x2.le_s', 'i64x2.ge_s',
|
|
143
|
-
'i64x2.extmul_low_i32x4_s', 'i64x2.extmul_high_i32x4_s', 'i64x2.extmul_low_i32x4_u', 'i64x2.extmul_high_i32x4_u',
|
|
144
|
-
'f32x4.abs', 'f32x4.neg', , 'f32x4.sqrt', 'f32x4.add', 'f32x4.sub', 'f32x4.mul', 'f32x4.div', 'f32x4.min', 'f32x4.max', 'f32x4.pmin', 'f32x4.pmax',
|
|
145
|
-
'f64x2.abs', 'f64x2.neg', , 'f64x2.sqrt', 'f64x2.add', 'f64x2.sub', 'f64x2.mul', 'f64x2.div', 'f64x2.min', 'f64x2.max', 'f64x2.pmin', 'f64x2.pmax',
|
|
146
|
-
'i32x4.trunc_sat_f32x4_s', 'i32x4.trunc_sat_f32x4_u', 'f32x4.convert_i32x4_s', 'f32x4.convert_i32x4_u',
|
|
147
|
-
'i32x4.trunc_sat_f64x2_s_zero', 'i32x4.trunc_sat_f64x2_u_zero', 'f64x2.convert_low_i32x4_s', 'f64x2.convert_low_i32x4_u',
|
|
148
|
-
'i8x16.relaxed_swizzle', 'i32x4.relaxed_trunc_f32x4_s', 'i32x4.relaxed_trunc_f32x4_u', 'i32x4.relaxed_trunc_f64x2_s_zero',
|
|
149
|
-
'i32x4.relaxed_trunc_f64x2_u_zero', 'f32x4.relaxed_madd', 'f32x4.relaxed_nmadd', 'f64x2.relaxed_madd', 'f64x2.relaxed_nmadd',
|
|
150
|
-
'i8x16.relaxed_laneselect', 'i16x8.relaxed_laneselect', 'i32x4.relaxed_laneselect', 'i64x2.relaxed_laneselect',
|
|
151
|
-
'f32x4.relaxed_min', 'f32x4.relaxed_max', 'f64x2.relaxed_min', 'f64x2.relaxed_max',
|
|
152
|
-
'i16x8.relaxed_q15mulr_s', 'i16x8.relaxed_dot_i8x16_i7x16_s', 'i32x4.relaxed_dot_i8x16_i7x16_add_s'
|
|
153
|
-
],
|
|
54
|
+
0xe0, 'cont.new typeidx', 'cont.bind cont_bind', 'suspend tagidx', 'resume resume', 'resume_throw resume_throw', 'resume_throw_ref resume_throw_ref', 'switch switch_cont',
|
|
55
|
+
// 0xfb: GC instructions
|
|
56
|
+
0xfb0000, 'struct.new typeidx', 'struct.new_default typeidx', 'struct.get typeidx_field', 'struct.get_s typeidx_field', 'struct.get_u typeidx_field', 'struct.set typeidx_field',
|
|
57
|
+
'array.new typeidx', 'array.new_default typeidx', 'array.new_fixed typeidx_multi', 'array.new_data typeidx_dataidx', 'array.new_elem typeidx_elemidx',
|
|
58
|
+
'array.get typeidx', 'array.get_s typeidx', 'array.get_u typeidx', 'array.set typeidx', 'array.len', 'array.fill typeidx', 'array.copy typeidx_typeidx',
|
|
59
|
+
'array.init_data typeidx_dataidx', 'array.init_elem typeidx_elemidx', 'ref.test reftype', 'ref.test_null reftype', 'ref.cast reftype', 'ref.cast_null reftype', 'br_on_cast reftype2', 'br_on_cast_fail reftype2',
|
|
60
|
+
'any.convert_extern', 'extern.convert_any', 'ref.i31', 'i31.get_s', 'i31.get_u',
|
|
61
|
+
// custom descriptors (Phase 3): 0xFB 0x20-0x26
|
|
62
|
+
0xfb0020, 'struct.new_desc typeidx', 'struct.new_default_desc typeidx', 'ref.get_desc typeidx', 'ref.cast_desc_eq reftype', 0xfb0025, 'br_on_cast_desc_eq reftype2', 'br_on_cast_desc_eq_fail reftype2',
|
|
63
|
+
// stringref: 0xFB 0x80-0xB7
|
|
64
|
+
0xfb0080, 'string.new_utf8 memoryidx?', 'string.new_wtf16 memoryidx?', 'string.const stringidx', 'string.measure_utf8', 'string.measure_wtf8', 'string.measure_wtf16',
|
|
65
|
+
'string.encode_utf8 memoryidx?', 'string.encode_wtf16 memoryidx?', 'string.concat', 'string.eq', 'string.is_usv_sequence',
|
|
66
|
+
'string.new_lossy_utf8 memoryidx?', 'string.new_wtf8 memoryidx?', 'string.encode_lossy_utf8 memoryidx?', 'string.encode_wtf8 memoryidx?',
|
|
67
|
+
0xfb0090, 'string.as_wtf8', 'stringview_wtf8.advance', 'stringview_wtf8.encode_utf8 memoryidx?', 'stringview_wtf8.slice',
|
|
68
|
+
'stringview_wtf8.encode_lossy_utf8 memoryidx?', 'stringview_wtf8.encode_wtf8 memoryidx?',
|
|
69
|
+
0xfb0098, 'string.as_wtf16', 'stringview_wtf16.length', 'stringview_wtf16.get_codeunit', 'stringview_wtf16.encode memoryidx?', 'stringview_wtf16.slice',
|
|
70
|
+
0xfb00a0, 'string.as_iter', 'stringview_iter.next', 'stringview_iter.advance', 'stringview_iter.rewind', 'stringview_iter.slice',
|
|
71
|
+
0xfb00b0, 'string.new_utf8_array', 'string.new_wtf16_array', 'string.encode_utf8_array', 'string.encode_wtf16_array',
|
|
72
|
+
'string.new_lossy_utf8_array', 'string.new_wtf8_array', 'string.encode_lossy_utf8_array', 'string.encode_wtf8_array',
|
|
73
|
+
// 0xfc: Bulk memory/table operations + rounding mode control
|
|
74
|
+
0xfc0000, 'i32.trunc_sat_f32_s', 'i32.trunc_sat_f32_u', 'i32.trunc_sat_f64_s', 'i32.trunc_sat_f64_u',
|
|
75
|
+
'i64.trunc_sat_f32_s', 'i64.trunc_sat_f32_u', 'i64.trunc_sat_f64_s', 'i64.trunc_sat_f64_u',
|
|
76
|
+
'memory.init dataidx_memoryidx', 'data.drop dataidx', 'memory.copy memoryidx_memoryidx', 'memory.fill memoryidx?',
|
|
77
|
+
'table.init reversed', 'elem.drop elemidx', 'table.copy tableidx_tableidx', 'table.grow tableidx', 'table.size tableidx', 'table.fill tableidx',
|
|
78
|
+
0xfc0013, 'i64.add128', 'i64.sub128', 'i64.mul_wide_s', 'i64.mul_wide_u',
|
|
79
|
+
// 0x80-0xBB: rounding mode control
|
|
80
|
+
0xfc0080, 'f32.sqrt_ceil', 'f32.add_ceil', 'f32.sub_ceil', 'f32.mul_ceil', 'f32.div_ceil',
|
|
81
|
+
'f64.sqrt_ceil', 'f64.add_ceil', 'f64.sub_ceil', 'f64.mul_ceil', 'f64.div_ceil',
|
|
82
|
+
'f32.convert_ceil_i32_s', 'f32.convert_ceil_i32_u', 'f32.convert_ceil_i64_s', 'f32.convert_ceil_i64_u', 'f32.demote_ceil_f64',
|
|
83
|
+
'f64.convert_ceil_i32_s', 'f64.convert_ceil_i32_u', 'f64.convert_ceil_i64_s', 'f64.convert_ceil_i64_u', 'f64.promote_ceil_f32',
|
|
84
|
+
'f32.sqrt_floor', 'f32.add_floor', 'f32.sub_floor', 'f32.mul_floor', 'f32.div_floor',
|
|
85
|
+
'f64.sqrt_floor', 'f64.add_floor', 'f64.sub_floor', 'f64.mul_floor', 'f64.div_floor',
|
|
86
|
+
'f32.convert_floor_i32_s', 'f32.convert_floor_i32_u', 'f32.convert_floor_i64_s', 'f32.convert_floor_i64_u', 'f32.demote_floor_f64',
|
|
87
|
+
'f64.convert_floor_i32_s', 'f64.convert_floor_i32_u', 'f64.convert_floor_i64_s', 'f64.convert_floor_i64_u', 'f64.promote_floor_f32',
|
|
88
|
+
'f32.sqrt_trunc', 'f32.add_trunc', 'f32.sub_trunc', 'f32.mul_trunc', 'f32.div_trunc',
|
|
89
|
+
'f64.sqrt_trunc', 'f64.add_trunc', 'f64.sub_trunc', 'f64.mul_trunc', 'f64.div_trunc',
|
|
90
|
+
'f32.convert_trunc_i32_s', 'f32.convert_trunc_i32_u', 'f32.convert_trunc_i64_s', 'f32.convert_trunc_i64_u', 'f32.demote_trunc_f64',
|
|
91
|
+
'f64.convert_trunc_i32_s', 'f64.convert_trunc_i32_u', 'f64.convert_trunc_i64_s', 'f64.convert_trunc_i64_u', 'f64.promote_trunc_f32',
|
|
92
|
+
// 0xfd: SIMD instructions
|
|
93
|
+
0xfd0000, 'v128.load memarg', 'v128.load8x8_s memarg', 'v128.load8x8_u memarg', 'v128.load16x4_s memarg', 'v128.load16x4_u memarg',
|
|
94
|
+
'v128.load32x2_s memarg', 'v128.load32x2_u memarg', 'v128.load8_splat memarg', 'v128.load16_splat memarg', 'v128.load32_splat memarg',
|
|
95
|
+
'v128.load64_splat memarg', 'v128.store memarg', 'v128.const v128const', 'i8x16.shuffle shuffle',
|
|
96
|
+
'i8x16.swizzle', 'i8x16.splat', 'i16x8.splat', 'i32x4.splat', 'i64x2.splat', 'f32x4.splat', 'f64x2.splat',
|
|
97
|
+
'i8x16.extract_lane_s laneidx', 'i8x16.extract_lane_u laneidx', 'i8x16.replace_lane laneidx',
|
|
98
|
+
'i16x8.extract_lane_s laneidx', 'i16x8.extract_lane_u laneidx', 'i16x8.replace_lane laneidx',
|
|
99
|
+
'i32x4.extract_lane laneidx', 'i32x4.replace_lane laneidx', 'i64x2.extract_lane laneidx', 'i64x2.replace_lane laneidx',
|
|
100
|
+
'f32x4.extract_lane laneidx', 'f32x4.replace_lane laneidx', 'f64x2.extract_lane laneidx', 'f64x2.replace_lane laneidx',
|
|
101
|
+
'i8x16.eq', 'i8x16.ne', 'i8x16.lt_s', 'i8x16.lt_u', 'i8x16.gt_s', 'i8x16.gt_u', 'i8x16.le_s', 'i8x16.le_u', 'i8x16.ge_s', 'i8x16.ge_u',
|
|
102
|
+
'i16x8.eq', 'i16x8.ne', 'i16x8.lt_s', 'i16x8.lt_u', 'i16x8.gt_s', 'i16x8.gt_u', 'i16x8.le_s', 'i16x8.le_u', 'i16x8.ge_s', 'i16x8.ge_u',
|
|
103
|
+
'i32x4.eq', 'i32x4.ne', 'i32x4.lt_s', 'i32x4.lt_u', 'i32x4.gt_s', 'i32x4.gt_u', 'i32x4.le_s', 'i32x4.le_u', 'i32x4.ge_s', 'i32x4.ge_u',
|
|
104
|
+
'f32x4.eq', 'f32x4.ne', 'f32x4.lt', 'f32x4.gt', 'f32x4.le', 'f32x4.ge', 'f64x2.eq', 'f64x2.ne', 'f64x2.lt', 'f64x2.gt', 'f64x2.le', 'f64x2.ge',
|
|
105
|
+
'v128.not', 'v128.and', 'v128.andnot', 'v128.or', 'v128.xor', 'v128.bitselect', 'v128.any_true',
|
|
106
|
+
'v128.load8_lane memlane', 'v128.load16_lane memlane', 'v128.load32_lane memlane', 'v128.load64_lane memlane',
|
|
107
|
+
'v128.store8_lane memlane', 'v128.store16_lane memlane', 'v128.store32_lane memlane', 'v128.store64_lane memlane',
|
|
108
|
+
'v128.load32_zero memarg', 'v128.load64_zero memarg', 'f32x4.demote_f64x2_zero', 'f64x2.promote_low_f32x4',
|
|
109
|
+
'i8x16.abs', 'i8x16.neg', 'i8x16.popcnt', 'i8x16.all_true', 'i8x16.bitmask', 'i8x16.narrow_i16x8_s', 'i8x16.narrow_i16x8_u',
|
|
110
|
+
'f32x4.ceil', 'f32x4.floor', 'f32x4.trunc', 'f32x4.nearest', 'i8x16.shl', 'i8x16.shr_s', 'i8x16.shr_u',
|
|
111
|
+
'i8x16.add', 'i8x16.add_sat_s', 'i8x16.add_sat_u', 'i8x16.sub', 'i8x16.sub_sat_s', 'i8x16.sub_sat_u',
|
|
112
|
+
'f64x2.ceil', 'f64x2.floor', 'i8x16.min_s', 'i8x16.min_u', 'i8x16.max_s', 'i8x16.max_u', 'f64x2.trunc', 'i8x16.avgr_u',
|
|
113
|
+
'i16x8.extadd_pairwise_i8x16_s', 'i16x8.extadd_pairwise_i8x16_u', 'i32x4.extadd_pairwise_i16x8_s', 'i32x4.extadd_pairwise_i16x8_u',
|
|
114
|
+
'i16x8.abs', 'i16x8.neg', 'i16x8.q15mulr_sat_s', 'i16x8.all_true', 'i16x8.bitmask', 'i16x8.narrow_i32x4_s', 'i16x8.narrow_i32x4_u',
|
|
115
|
+
'i16x8.extend_low_i8x16_s', 'i16x8.extend_high_i8x16_s', 'i16x8.extend_low_i8x16_u', 'i16x8.extend_high_i8x16_u',
|
|
116
|
+
'i16x8.shl', 'i16x8.shr_s', 'i16x8.shr_u', 'i16x8.add', 'i16x8.add_sat_s', 'i16x8.add_sat_u', 'i16x8.sub', 'i16x8.sub_sat_s', 'i16x8.sub_sat_u',
|
|
117
|
+
'f64x2.nearest', 'i16x8.mul', 'i16x8.min_s', 'i16x8.min_u', 'i16x8.max_s', 'i16x8.max_u', 0xfd009b, 'i16x8.avgr_u',
|
|
118
|
+
'i16x8.extmul_low_i8x16_s', 'i16x8.extmul_high_i8x16_s', 'i16x8.extmul_low_i8x16_u', 'i16x8.extmul_high_i8x16_u',
|
|
119
|
+
'i32x4.abs', 'i32x4.neg', 0xfd00a3, 'i32x4.all_true', 'i32x4.bitmask', 0xfd00a7, 'i32x4.extend_low_i16x8_s', 'i32x4.extend_high_i16x8_s',
|
|
120
|
+
'i32x4.extend_low_i16x8_u', 'i32x4.extend_high_i16x8_u', 'i32x4.shl', 'i32x4.shr_s', 'i32x4.shr_u', 'i32x4.add', 0xfd00b1, 'i32x4.sub',
|
|
121
|
+
0xfd00b5, 'i32x4.mul', 'i32x4.min_s', 'i32x4.min_u', 'i32x4.max_s', 'i32x4.max_u', 'i32x4.dot_i16x8_s',
|
|
122
|
+
0xfd00bc, 'i32x4.extmul_low_i16x8_s', 'i32x4.extmul_high_i16x8_s', 'i32x4.extmul_low_i16x8_u', 'i32x4.extmul_high_i16x8_u',
|
|
123
|
+
'i64x2.abs', 'i64x2.neg', 0xfd00c3, 'i64x2.all_true', 'i64x2.bitmask', 0xfd00c7, 'i64x2.extend_low_i32x4_s', 'i64x2.extend_high_i32x4_s',
|
|
124
|
+
'i64x2.extend_low_i32x4_u', 'i64x2.extend_high_i32x4_u', 'i64x2.shl', 'i64x2.shr_s', 'i64x2.shr_u', 'i64x2.add', 0xfd00d1, 'i64x2.sub',
|
|
125
|
+
0xfd00d5, 'i64x2.mul', 'i64x2.eq', 'i64x2.ne', 'i64x2.lt_s', 'i64x2.gt_s', 'i64x2.le_s', 'i64x2.ge_s',
|
|
126
|
+
'i64x2.extmul_low_i32x4_s', 'i64x2.extmul_high_i32x4_s', 'i64x2.extmul_low_i32x4_u', 'i64x2.extmul_high_i32x4_u',
|
|
127
|
+
'f32x4.abs', 'f32x4.neg', 0xfd00e3, 'f32x4.sqrt', 'f32x4.add', 'f32x4.sub', 'f32x4.mul', 'f32x4.div', 'f32x4.min', 'f32x4.max', 'f32x4.pmin', 'f32x4.pmax',
|
|
128
|
+
'f64x2.abs', 'f64x2.neg', 0xfd00ef, 'f64x2.sqrt', 'f64x2.add', 'f64x2.sub', 'f64x2.mul', 'f64x2.div', 'f64x2.min', 'f64x2.max', 'f64x2.pmin', 'f64x2.pmax',
|
|
129
|
+
'i32x4.trunc_sat_f32x4_s', 'i32x4.trunc_sat_f32x4_u', 'f32x4.convert_i32x4_s', 'f32x4.convert_i32x4_u',
|
|
130
|
+
'i32x4.trunc_sat_f64x2_s_zero', 'i32x4.trunc_sat_f64x2_u_zero', 'f64x2.convert_low_i32x4_s', 'f64x2.convert_low_i32x4_u',
|
|
131
|
+
'i8x16.relaxed_swizzle', 'i32x4.relaxed_trunc_f32x4_s', 'i32x4.relaxed_trunc_f32x4_u', 'i32x4.relaxed_trunc_f64x2_s_zero',
|
|
132
|
+
'i32x4.relaxed_trunc_f64x2_u_zero', 'f32x4.relaxed_madd', 'f32x4.relaxed_nmadd', 'f64x2.relaxed_madd', 'f64x2.relaxed_nmadd',
|
|
133
|
+
'i8x16.relaxed_laneselect', 'i16x8.relaxed_laneselect', 'i32x4.relaxed_laneselect', 'i64x2.relaxed_laneselect',
|
|
134
|
+
'f32x4.relaxed_min', 'f32x4.relaxed_max', 'f64x2.relaxed_min', 'f64x2.relaxed_max',
|
|
135
|
+
'i16x8.relaxed_q15mulr_s', 'i16x8.relaxed_dot_i8x16_i7x16_s', 'i32x4.relaxed_dot_i8x16_i7x16_add_s',
|
|
154
136
|
// 0xfe: atomic/thread instructions
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
'i32.atomic.rmw.cmpxchg memarg', 'i64.atomic.rmw.cmpxchg memarg', 'i32.atomic.rmw8.cmpxchg_u memarg', 'i32.atomic.rmw16.cmpxchg_u memarg',
|
|
173
|
-
'i64.atomic.rmw8.cmpxchg_u memarg', 'i64.atomic.rmw16.cmpxchg_u memarg', 'i64.atomic.rmw32.cmpxchg_u memarg'
|
|
174
|
-
]
|
|
137
|
+
0xfe0000, 'memory.atomic.notify memarg', 'memory.atomic.wait32 memarg', 'memory.atomic.wait64 memarg', 'atomic.fence opt_memory',
|
|
138
|
+
0xfe0010, 'i32.atomic.load memarg', 'i64.atomic.load memarg', 'i32.atomic.load8_u memarg', 'i32.atomic.load16_u memarg',
|
|
139
|
+
'i64.atomic.load8_u memarg', 'i64.atomic.load16_u memarg', 'i64.atomic.load32_u memarg', 'i32.atomic.store memarg',
|
|
140
|
+
'i64.atomic.store memarg', 'i32.atomic.store8 memarg', 'i32.atomic.store16 memarg', 'i64.atomic.store8 memarg',
|
|
141
|
+
'i64.atomic.store16 memarg', 'i64.atomic.store32 memarg', 'i32.atomic.rmw.add memarg', 'i64.atomic.rmw.add memarg',
|
|
142
|
+
'i32.atomic.rmw8.add_u memarg', 'i32.atomic.rmw16.add_u memarg', 'i64.atomic.rmw8.add_u memarg', 'i64.atomic.rmw16.add_u memarg',
|
|
143
|
+
'i64.atomic.rmw32.add_u memarg', 'i32.atomic.rmw.sub memarg', 'i64.atomic.rmw.sub memarg', 'i32.atomic.rmw8.sub_u memarg',
|
|
144
|
+
'i32.atomic.rmw16.sub_u memarg', 'i64.atomic.rmw8.sub_u memarg', 'i64.atomic.rmw16.sub_u memarg', 'i64.atomic.rmw32.sub_u memarg',
|
|
145
|
+
'i32.atomic.rmw.and memarg', 'i64.atomic.rmw.and memarg', 'i32.atomic.rmw8.and_u memarg', 'i32.atomic.rmw16.and_u memarg',
|
|
146
|
+
'i64.atomic.rmw8.and_u memarg', 'i64.atomic.rmw16.and_u memarg', 'i64.atomic.rmw32.and_u memarg', 'i32.atomic.rmw.or memarg',
|
|
147
|
+
'i64.atomic.rmw.or memarg', 'i32.atomic.rmw8.or_u memarg', 'i32.atomic.rmw16.or_u memarg', 'i64.atomic.rmw8.or_u memarg',
|
|
148
|
+
'i64.atomic.rmw16.or_u memarg', 'i64.atomic.rmw32.or_u memarg', 'i32.atomic.rmw.xor memarg', 'i64.atomic.rmw.xor memarg',
|
|
149
|
+
'i32.atomic.rmw8.xor_u memarg', 'i32.atomic.rmw16.xor_u memarg', 'i64.atomic.rmw8.xor_u memarg', 'i64.atomic.rmw16.xor_u memarg',
|
|
150
|
+
'i64.atomic.rmw32.xor_u memarg', 'i32.atomic.rmw.xchg memarg', 'i64.atomic.rmw.xchg memarg', 'i32.atomic.rmw8.xchg_u memarg',
|
|
151
|
+
'i32.atomic.rmw16.xchg_u memarg', 'i64.atomic.rmw8.xchg_u memarg', 'i64.atomic.rmw16.xchg_u memarg', 'i64.atomic.rmw32.xchg_u memarg',
|
|
152
|
+
'i32.atomic.rmw.cmpxchg memarg', 'i64.atomic.rmw.cmpxchg memarg', 'i32.atomic.rmw8.cmpxchg_u memarg', 'i32.atomic.rmw16.cmpxchg_u memarg',
|
|
153
|
+
'i64.atomic.rmw8.cmpxchg_u memarg', 'i64.atomic.rmw16.cmpxchg_u memarg', 'i64.atomic.rmw32.cmpxchg_u memarg',
|
|
175
154
|
]
|
|
176
155
|
|
|
156
|
+
// Lookup tables derived from TABLE eagerly at module load — OPCODE: name → packed opcode
|
|
157
|
+
// (uleb applied at emit); IMM: name → immediate type, keying compile.js's HANDLER
|
|
158
|
+
// encoders. One flat non-recursive scan over const data, results hold only strings/ints —
|
|
159
|
+
// fresh dictionaries, no mutation of TABLE, no function values — the shape a static
|
|
160
|
+
// evaluator (jz self-host) folds to plain data at compile time.
|
|
161
|
+
export const OPCODE = {}, IMM = {}
|
|
162
|
+
for (let i = 0, code = 0; i < TABLE.length; i++) {
|
|
163
|
+
const item = TABLE[i]
|
|
164
|
+
if (typeof item === 'number') { code = item; continue }
|
|
165
|
+
const sp = item.indexOf(' ')
|
|
166
|
+
const nm = sp < 0 ? item : item.slice(0, sp)
|
|
167
|
+
if (sp >= 0) IMM[nm] = item.slice(sp + 1)
|
|
168
|
+
OPCODE[nm] = code++
|
|
169
|
+
}
|
|
170
|
+
|
|
177
171
|
/**
|
|
178
172
|
* Result value-type of an instruction, inferred from its name — the single
|
|
179
173
|
* source of truth for both constant folding and import type-inference.
|
package/src/encode.js
CHANGED
|
@@ -70,8 +70,19 @@ export function uleb5(value) {
|
|
|
70
70
|
* @param {number[]} [buffer=[]] - Output buffer
|
|
71
71
|
* @returns {number[]} Encoded bytes
|
|
72
72
|
*/
|
|
73
|
+
// repeated literal strings (a self-hosted module reuses the same constants
|
|
74
|
+
// thousands of times) parse once — the memo resets when it grows unbounded
|
|
75
|
+
const I32_MEMO = new Map()
|
|
73
76
|
export function i32(n, buffer = []) {
|
|
74
|
-
if (typeof n === 'string')
|
|
77
|
+
if (typeof n === 'string') {
|
|
78
|
+
let v = I32_MEMO.get(n)
|
|
79
|
+
if (v === undefined) {
|
|
80
|
+
v = i32.parse(n)
|
|
81
|
+
if (I32_MEMO.size > 65536) I32_MEMO.clear()
|
|
82
|
+
I32_MEMO.set(n, v)
|
|
83
|
+
}
|
|
84
|
+
n = v
|
|
85
|
+
}
|
|
75
86
|
|
|
76
87
|
while (true) {
|
|
77
88
|
const byte = Number(n & 0x7F)
|
|
@@ -148,7 +159,7 @@ i64.parse = n => {
|
|
|
148
159
|
}
|
|
149
160
|
|
|
150
161
|
const F32_SIGN = 0x80000000, F32_NAN = 0x7f800000, F32_QUIET = 0x400000
|
|
151
|
-
export function f32(input, value, idx) {
|
|
162
|
+
export function f32(input, out, value, idx) {
|
|
152
163
|
// Plain `nan` / `-nan` (with optional `:0xPAYLOAD`) — set the bit pattern explicitly.
|
|
153
164
|
if (typeof input === 'string' && (idx = input.indexOf('nan')) >= 0) {
|
|
154
165
|
if (input[idx + 3] === ':') {
|
|
@@ -164,11 +175,20 @@ export function f32(input, value, idx) {
|
|
|
164
175
|
_f32[0] = value
|
|
165
176
|
}
|
|
166
177
|
|
|
178
|
+
if (out) { out.push(_u8[0], _u8[1], _u8[2], _u8[3]); return }
|
|
167
179
|
return [_u8[0], _u8[1], _u8[2], _u8[3]]
|
|
168
180
|
}
|
|
169
181
|
|
|
170
182
|
const F64_SIGN = 0x8000000000000000n, F64_NAN = 0x7ff0000000000000n, F64_QUIET = 0x8000000000000n
|
|
171
|
-
|
|
183
|
+
const F64_MEMO = new Map()
|
|
184
|
+
export function f64(input, out, value, idx) {
|
|
185
|
+
if (typeof input === 'string') {
|
|
186
|
+
const m = F64_MEMO.get(input)
|
|
187
|
+
if (m !== undefined) {
|
|
188
|
+
if (out) { out.push(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7]); return }
|
|
189
|
+
return m.slice()
|
|
190
|
+
}
|
|
191
|
+
}
|
|
172
192
|
// Plain `nan` / `-nan` (with optional `:0xPAYLOAD`) — set the bit pattern explicitly.
|
|
173
193
|
if (typeof input === 'string' && (idx = input.indexOf('nan')) >= 0) {
|
|
174
194
|
if (input[idx + 3] === ':') {
|
|
@@ -184,6 +204,11 @@ export function f64(input, value, idx) {
|
|
|
184
204
|
_f64[0] = value
|
|
185
205
|
}
|
|
186
206
|
|
|
207
|
+
if (typeof input === 'string') {
|
|
208
|
+
if (F64_MEMO.size > 65536) F64_MEMO.clear()
|
|
209
|
+
F64_MEMO.set(input, [_u8[0], _u8[1], _u8[2], _u8[3], _u8[4], _u8[5], _u8[6], _u8[7]])
|
|
210
|
+
}
|
|
211
|
+
if (out) { out.push(_u8[0], _u8[1], _u8[2], _u8[3], _u8[4], _u8[5], _u8[6], _u8[7]); return }
|
|
187
212
|
return [_u8[0], _u8[1], _u8[2], _u8[3], _u8[4], _u8[5], _u8[6], _u8[7]]
|
|
188
213
|
}
|
|
189
214
|
|