@fraczak/k 8.9.0 → 9.1.0
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/CHANGELOG.md +28 -0
- package/DOCS/ARCHITECTURE.md +5 -4
- package/DOCS/DICTIONARY.md +2 -2
- package/DOCS/FILE_FORMATS.md +11 -9
- package/DOCS/KIR_V1.md +5 -1
- package/DOCS/KTVM_SPEC.md +907 -0
- package/DOCS/KVM_EXECUTION_MODEL.md +3 -2
- package/DOCS/KVM_VS_KTVM_COMPARATIVE_STUDY.md +127 -0
- package/DOCS/PROFILING_AND_TRACING.md +13 -4
- package/DOCS/README.md +3 -2
- package/DOCS/REPL.md +16 -12
- package/README.md +138 -2
- package/backend-api.mjs +31 -16
- package/backends/llvm/README.md +4 -5
- package/backends/llvm/bin/k-llvm-build.mjs +7 -19
- package/backends/llvm/bin/k-llvm-compile.mjs +6 -18
- package/backends/llvm/bin/k-llvm.mjs +11 -33
- package/backends/llvm/src/cli.mjs +2 -12
- package/backends/llvm/src/executable.mjs +7 -95
- package/backends/llvm/src/kvm2llvm.mjs +1 -7
- package/backends/llvm/src/llvm.mjs +4 -113
- package/backends/llvm/tests/k-llvm-run.mjs +1 -6
- package/backends/llvm/tests/perf-support.mjs +3 -5
- package/backends/llvm/tests/test-llvm.mjs +2 -16
- package/backends/wasm/README.md +7 -3
- package/backends/wasm/bin/k-wasm-compile.mjs +4 -7
- package/backends/wasm/bin/k-wasm.mjs +4 -6
- package/backends/wasm/src/cli.mjs +8 -24
- package/backends/wasm/src/wasm.mjs +60 -57
- package/backends/wasm/tests/test-wasm.mjs +41 -2
- package/browser/repl-browser.mjs +9 -8
- package/browser/shims/buffer.mjs +31 -2
- package/browser/shims/child_process.mjs +26 -0
- package/browser/shims/fs.mjs +3 -1
- package/codecs/runtime/prefix-codec.mjs +67 -1
- package/conformance/run.mjs +17 -2
- package/harness/adapters/ktvm-llvm.mjs +118 -0
- package/harness/adapters/ktvm-wasm.mjs +104 -0
- package/harness/adapters/ktvm.mjs +133 -0
- package/harness/registry.mjs +9 -0
- package/ktvm/ast.mjs +218 -0
- package/ktvm/bit-deque.mjs +497 -0
- package/ktvm/fixtures/integer-streaming.k +477 -0
- package/ktvm/index.mjs +130 -0
- package/ktvm/llvm/index.mjs +202 -0
- package/ktvm/llvm/lower-c.mjs +1258 -0
- package/ktvm/llvm/runtime.h +3542 -0
- package/ktvm/llvm/test-llvm.mjs +476 -0
- package/ktvm/lower-kir.mjs +2221 -0
- package/ktvm/optimize/fuse-kernel.mjs +2111 -0
- package/ktvm/optimize/index.mjs +116 -0
- package/ktvm/optimize/mwtr.mjs +283 -0
- package/ktvm/optimize/simplify-procs.mjs +5250 -0
- package/ktvm/optimize/synthesize-tables.mjs +1105 -0
- package/ktvm/parser.mjs +1101 -0
- package/ktvm/printer.mjs +281 -0
- package/ktvm/schema.mjs +1134 -0
- package/ktvm/specialize.mjs +538 -0
- package/ktvm/test-conformance.mjs +597 -0
- package/ktvm/test-ktvm.mjs +1805 -0
- package/ktvm/test-optimize.mjs +598 -0
- package/ktvm/validator.mjs +1280 -0
- package/ktvm/vm.mjs +2225 -0
- package/ktvm/wasm/index.mjs +331 -0
- package/ktvm/wasm/lower-wasm.mjs +1272 -0
- package/ktvm/wasm/runtime.wat +4404 -0
- package/ktvm/wasm/test-wasm.mjs +429 -0
- package/ktvm/wire.mjs +679 -0
- package/package.json +1 -9
- package/paper/Makefile +8 -5
- package/paper/k-transducer-compiler.pdf +0 -0
- package/paper/k-transducer-compiler.tex +658 -0
- package/paper/replay-diagnostic.mjs +63 -0
- package/repl.mjs +101 -20
- package/scripts/build-repl-html.mjs +40 -18
- package/scripts/perf-bigint-compare.mjs +12 -16
- package/scripts/perf-ieee.mjs +14 -232
- package/scripts/perf-int.mjs +16 -274
- package/scripts/perf-multiplication.mjs +116 -121
- package/scripts/perf-poly.mjs +9 -263
- package/scripts/run-tests.mjs +5 -2
- package/scripts/test-package.mjs +15 -3
- package/tests/README.md +3 -3
- package/tests/test-repl-html.mjs +72 -15
- package/tests/test-repl.mjs +24 -17
- package/DOCS/POLYMORPHIC_IR.md +0 -86
- package/access-table.mjs +0 -343
- package/pkir.mjs +0 -772
- package/tests/test-access-table.mjs +0 -213
- package/tests/test-pkir.mjs +0 -530
|
@@ -0,0 +1,4404 @@
|
|
|
1
|
+
;; ============================================================================
|
|
2
|
+
;; kTVM Standalone WebAssembly Runtime (`runtime.wat`)
|
|
3
|
+
;;
|
|
4
|
+
;; Implements the complete strictly iterative `kTVM` mono operational semantics
|
|
5
|
+
;; in linear memory:
|
|
6
|
+
;; - Flat bump arena (1ns reset between runs) + global memory/node/step quotas
|
|
7
|
+
;; - `BitDeque` (`<= 128b` inline ring buffer + growable power-of-2 ring buffer)
|
|
8
|
+
;; - `BitStack` (`<= 128b` inline LIFO bit stack + growable stack)
|
|
9
|
+
;; - Shared Moore-minimized schema DFA + resumable DPDA (`skip`, `copy`, `coerce`)
|
|
10
|
+
;; - O(1) zero-copy `_splice`, same-scope `mux`/`demux` fusion, single-survivor `tee`
|
|
11
|
+
;; - Strict `barrier`/`gate`/`drain`, `spec` rollback & upstream abort, `after %tok`
|
|
12
|
+
;; - Hot-burst `chunk_table` (with SWAR mask compression), `stack_table`, `fsm_table`
|
|
13
|
+
;; - Strictly iterative demand-driven pull scheduler (`drive_to_completion`)
|
|
14
|
+
;; ============================================================================
|
|
15
|
+
|
|
16
|
+
(memory (export "memory") 256 16384)
|
|
17
|
+
|
|
18
|
+
;; Function table types for indirect dispatch
|
|
19
|
+
(type $block_fn (func (param i32 i32))) ;; (scopePtr, envPtr)
|
|
20
|
+
(type $kernel_fn (func (param i32) (result i32))) ;; (nodePtr) -> 0=ok, 1=needPull, 2=yielded
|
|
21
|
+
|
|
22
|
+
;; Runtime mutable globals
|
|
23
|
+
(global $g_heap_base (mut i32) (i32.const 0))
|
|
24
|
+
(global $g_heap_ptr (mut i32) (i32.const 0))
|
|
25
|
+
(global $g_heap_limit (mut i32) (i32.const 0))
|
|
26
|
+
(global $g_error_code (mut i32) (i32.const 0))
|
|
27
|
+
(global $g_need_pull (mut i32) (i32.const 0))
|
|
28
|
+
(global $g_need_step_node (mut i32) (i32.const 0))
|
|
29
|
+
|
|
30
|
+
;; Quotas & telemetry
|
|
31
|
+
(global $g_max_steps (mut i32) (i32.const 50000000))
|
|
32
|
+
(global $g_max_nodes (mut i32) (i32.const 200000))
|
|
33
|
+
(global $g_max_bytes (mut i32) (i32.const 67108864))
|
|
34
|
+
|
|
35
|
+
(global $g_steps (mut i32) (i32.const 0))
|
|
36
|
+
(global $g_live_nodes (mut i32) (i32.const 0))
|
|
37
|
+
(global $g_peak_nodes (mut i32) (i32.const 0))
|
|
38
|
+
(global $g_buf_bytes (mut i32) (i32.const 0))
|
|
39
|
+
(global $g_peak_bytes (mut i32) (i32.const 0))
|
|
40
|
+
(global $g_table_lookups (mut i32) (i32.const 0))
|
|
41
|
+
(global $g_splices (mut i32) (i32.const 0))
|
|
42
|
+
(global $g_mux_demux_fusions (mut i32) (i32.const 0))
|
|
43
|
+
(global $g_spec_rollbacks (mut i32) (i32.const 0))
|
|
44
|
+
(global $g_spec_upstream_aborts (mut i32) (i32.const 0))
|
|
45
|
+
(global $g_spec_tail_splices (mut i32) (i32.const 0))
|
|
46
|
+
(global $g_after_settlements (mut i32) (i32.const 0))
|
|
47
|
+
|
|
48
|
+
;; Demand stack (8 bytes per frame: [kind (0=chan,1=node), ptr])
|
|
49
|
+
(global $g_dstack_ptr (mut i32) (i32.const 0))
|
|
50
|
+
(global $g_dstack_cap (mut i32) (i32.const 0))
|
|
51
|
+
(global $g_dstack_len (mut i32) (i32.const 0))
|
|
52
|
+
|
|
53
|
+
;; Reusable `$scope_destroy` scratch stacks, unified power-of-2 slab free list,
|
|
54
|
+
;; switch scratch tags, and safepoint deferred-reclamation lists
|
|
55
|
+
(global $g_sc_stk_ptr (mut i32) (i32.const 0))
|
|
56
|
+
(global $g_sc_ord_ptr (mut i32) (i32.const 0))
|
|
57
|
+
(global $g_sc_cap (mut i32) (i32.const 0))
|
|
58
|
+
(global $g_slab_fl_ptr (mut i32) (i32.const 0))
|
|
59
|
+
(global $g_dead_nodes (mut i32) (i32.const 0))
|
|
60
|
+
(global $g_dead_chans (mut i32) (i32.const 0))
|
|
61
|
+
(global $g_dead_scopes (mut i32) (i32.const 0))
|
|
62
|
+
(global $g_sw_tags_ptr (mut i32) (i32.const 0))
|
|
63
|
+
(global $g_sw_tags_cap (mut i32) (i32.const 0))
|
|
64
|
+
|
|
65
|
+
;; Error codes:
|
|
66
|
+
;; 0 = OK
|
|
67
|
+
;; -1 = FAILED (normal K partiality failure)
|
|
68
|
+
;; -2 = QUOTA_STEPS
|
|
69
|
+
;; -3 = QUOTA_NODES
|
|
70
|
+
;; -4 = QUOTA_BYTES
|
|
71
|
+
;; -5 = OUT_CAP
|
|
72
|
+
;; -6 = MALFORMED_WIRE
|
|
73
|
+
;; -7 = DEADLOCK
|
|
74
|
+
;; -8 = OOM_ARENA
|
|
75
|
+
|
|
76
|
+
;; Node kind constants:
|
|
77
|
+
;; 1=const_unit, 2=splice, 3=coerce, 4=wrap, 5=match, 6=demux, 7=mux,
|
|
78
|
+
;; 8=tee, 9=drain, 10=gate, 11=switch, 12=spec, 13=tap, 14=spawn, 15=kernel
|
|
79
|
+
|
|
80
|
+
;; Channel status constants:
|
|
81
|
+
;; 0=OPEN, 1=CLOSED, 2=FAILED, 3=RELEASED
|
|
82
|
+
|
|
83
|
+
;; ============================================================================
|
|
84
|
+
;; 1. Arena Allocator, Quotas & Bit Helpers
|
|
85
|
+
;; ============================================================================
|
|
86
|
+
|
|
87
|
+
(func $mask32 (param $w i32) (result i32)
|
|
88
|
+
(if (i32.le_s (local.get $w) (i32.const 0))
|
|
89
|
+
(then (return (i32.const 0))))
|
|
90
|
+
(if (i32.ge_s (local.get $w) (i32.const 32))
|
|
91
|
+
(then (return (i32.const -1))))
|
|
92
|
+
(i32.sub (i32.shl (i32.const 1) (local.get $w)) (i32.const 1))
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
;; Unified O(1) power-of-2 slab allocator (buckets 0..16 => 16B..1MiB; 8-byte header [bucket, live])
|
|
96
|
+
(func $alloc (param $bytes i32) (result i32)
|
|
97
|
+
(local $b i32)
|
|
98
|
+
(local $payloadSz i32)
|
|
99
|
+
(local $slot i32)
|
|
100
|
+
(local $ptr i32)
|
|
101
|
+
(local $raw i32)
|
|
102
|
+
(local $total i32)
|
|
103
|
+
(local $next i32)
|
|
104
|
+
(local $needPages i32)
|
|
105
|
+
(local $curPages i32)
|
|
106
|
+
|
|
107
|
+
(if (i32.le_s (local.get $bytes) (i32.const 16))
|
|
108
|
+
(then
|
|
109
|
+
(local.set $b (i32.const 0))
|
|
110
|
+
(local.set $payloadSz (i32.const 16))
|
|
111
|
+
)
|
|
112
|
+
(else
|
|
113
|
+
(local.set $b (i32.sub (i32.const 28) (i32.clz (i32.sub (local.get $bytes) (i32.const 1)))))
|
|
114
|
+
(if (i32.lt_s (local.get $b) (i32.const 17))
|
|
115
|
+
(then (local.set $payloadSz (i32.shl (i32.const 16) (local.get $b))))
|
|
116
|
+
(else
|
|
117
|
+
(local.set $b (i32.const -1))
|
|
118
|
+
(local.set $payloadSz (i32.and (i32.add (local.get $bytes) (i32.const 7)) (i32.const -8)))
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
;; Fast path: pop recycled block from slab free list
|
|
125
|
+
(if (i32.and (i32.ge_s (local.get $b) (i32.const 0)) (i32.ne (global.get $g_slab_fl_ptr) (i32.const 0)))
|
|
126
|
+
(then
|
|
127
|
+
(local.set $slot (i32.add (global.get $g_slab_fl_ptr) (i32.shl (local.get $b) (i32.const 2))))
|
|
128
|
+
(local.set $ptr (i32.load (local.get $slot)))
|
|
129
|
+
(if (i32.ne (local.get $ptr) (i32.const 0))
|
|
130
|
+
(then
|
|
131
|
+
(i32.store (local.get $slot) (i32.load (local.get $ptr)))
|
|
132
|
+
(i32.store (i32.sub (local.get $ptr) (i32.const 4)) (i32.const 1)) ;; live = 1
|
|
133
|
+
(if (i32.gt_s (local.get $bytes) (i32.const 0))
|
|
134
|
+
(then (memory.fill (local.get $ptr) (i32.const 0) (local.get $bytes))))
|
|
135
|
+
(return (local.get $ptr))
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
;; Cold path: bump-allocate [8-byte header + payloadSz]
|
|
142
|
+
(local.set $total (i32.add (local.get $payloadSz) (i32.const 8)))
|
|
143
|
+
(local.set $raw (global.get $g_heap_ptr))
|
|
144
|
+
(local.set $next (i32.add (local.get $raw) (local.get $total)))
|
|
145
|
+
(if (i32.or (i32.lt_u (local.get $next) (local.get $raw)) (i32.gt_u (local.get $next) (global.get $g_heap_limit)))
|
|
146
|
+
(then
|
|
147
|
+
(if (i32.lt_u (local.get $next) (local.get $raw))
|
|
148
|
+
(then
|
|
149
|
+
(global.set $g_error_code (i32.const -8))
|
|
150
|
+
(return (i32.const 0))))
|
|
151
|
+
(local.set $needPages
|
|
152
|
+
(i32.shr_u
|
|
153
|
+
(i32.add (i32.sub (local.get $next) (global.get $g_heap_limit)) (i32.const 65535))
|
|
154
|
+
(i32.const 16)))
|
|
155
|
+
(if (i32.lt_s (local.get $needPages) (i32.const 16))
|
|
156
|
+
(then (local.set $needPages (i32.const 16))))
|
|
157
|
+
(local.set $curPages (memory.grow (local.get $needPages)))
|
|
158
|
+
(if (i32.eq (local.get $curPages) (i32.const -1))
|
|
159
|
+
(then
|
|
160
|
+
(global.set $g_error_code (i32.const -8))
|
|
161
|
+
(return (i32.const 0))))
|
|
162
|
+
(global.set $g_heap_limit (i32.shl (memory.size) (i32.const 16)))
|
|
163
|
+
)
|
|
164
|
+
)
|
|
165
|
+
(global.set $g_heap_ptr (local.get $next))
|
|
166
|
+
(i32.store (local.get $raw) (local.get $b))
|
|
167
|
+
(i32.store (i32.add (local.get $raw) (i32.const 4)) (i32.const 1)) ;; live = 1
|
|
168
|
+
(local.set $ptr (i32.add (local.get $raw) (i32.const 8)))
|
|
169
|
+
(if (i32.gt_s (local.get $bytes) (i32.const 0))
|
|
170
|
+
(then (memory.fill (local.get $ptr) (i32.const 0) (local.get $bytes))))
|
|
171
|
+
(local.get $ptr)
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
(func $free (param $ptr i32)
|
|
175
|
+
(local $b i32)
|
|
176
|
+
(local $slot i32)
|
|
177
|
+
(if (i32.or (i32.eqz (local.get $ptr)) (i32.eqz (global.get $g_slab_fl_ptr))) (then (return)))
|
|
178
|
+
(if (i32.ne (i32.load (i32.sub (local.get $ptr) (i32.const 4))) (i32.const 1)) (then (return)))
|
|
179
|
+
(i32.store (i32.sub (local.get $ptr) (i32.const 4)) (i32.const 0)) ;; live = 0
|
|
180
|
+
(local.set $b (i32.load (i32.sub (local.get $ptr) (i32.const 8))))
|
|
181
|
+
(if (i32.and (i32.ge_s (local.get $b) (i32.const 0)) (i32.lt_s (local.get $b) (i32.const 17)))
|
|
182
|
+
(then
|
|
183
|
+
(local.set $slot (i32.add (global.get $g_slab_fl_ptr) (i32.shl (local.get $b) (i32.const 2))))
|
|
184
|
+
(i32.store (local.get $ptr) (i32.load (local.get $slot)))
|
|
185
|
+
(i32.store (local.get $slot) (local.get $ptr))
|
|
186
|
+
)
|
|
187
|
+
)
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
(func $quota_add_bytes (param $delta i32)
|
|
191
|
+
(local $cur i32)
|
|
192
|
+
(local.set $cur (i32.add (global.get $g_buf_bytes) (local.get $delta)))
|
|
193
|
+
(if (i32.lt_s (local.get $cur) (i32.const 0))
|
|
194
|
+
(then (local.set $cur (i32.const 0))))
|
|
195
|
+
(global.set $g_buf_bytes (local.get $cur))
|
|
196
|
+
(if (i32.gt_s (local.get $cur) (global.get $g_peak_bytes))
|
|
197
|
+
(then (global.set $g_peak_bytes (local.get $cur))))
|
|
198
|
+
(if (i32.gt_s (local.get $cur) (global.get $g_max_bytes))
|
|
199
|
+
(then
|
|
200
|
+
(if (i32.eq (global.get $g_error_code) (i32.const 0))
|
|
201
|
+
(then (global.set $g_error_code (i32.const -4))))))
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
(func $quota_add_node
|
|
205
|
+
(local $cur i32)
|
|
206
|
+
(local.set $cur (i32.add (global.get $g_live_nodes) (i32.const 1)))
|
|
207
|
+
(global.set $g_live_nodes (local.get $cur))
|
|
208
|
+
(if (i32.gt_s (local.get $cur) (global.get $g_peak_nodes))
|
|
209
|
+
(then (global.set $g_peak_nodes (local.get $cur))))
|
|
210
|
+
(if (i32.gt_s (local.get $cur) (global.get $g_max_nodes))
|
|
211
|
+
(then
|
|
212
|
+
(if (i32.eq (global.get $g_error_code) (i32.const 0))
|
|
213
|
+
(then (global.set $g_error_code (i32.const -3))))))
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
(func $quota_remove_node
|
|
217
|
+
(if (i32.gt_s (global.get $g_live_nodes) (i32.const 0))
|
|
218
|
+
(then (global.set $g_live_nodes (i32.sub (global.get $g_live_nodes) (i32.const 1)))))
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
(func $quota_tick_step (result i32)
|
|
222
|
+
(local $s i32)
|
|
223
|
+
(local.set $s (i32.add (global.get $g_steps) (i32.const 1)))
|
|
224
|
+
(global.set $g_steps (local.get $s))
|
|
225
|
+
(if (i32.gt_s (local.get $s) (global.get $g_max_steps))
|
|
226
|
+
(then
|
|
227
|
+
(if (i32.eq (global.get $g_error_code) (i32.const 0))
|
|
228
|
+
(then (global.set $g_error_code (i32.const -2))))
|
|
229
|
+
(return (i32.const 1))))
|
|
230
|
+
(i32.ne (global.get $g_error_code) (i32.const 0))
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
;; ============================================================================
|
|
234
|
+
;; 2. Schema DFA Helpers (Global Moore-Minimized Schema Table)
|
|
235
|
+
;; ============================================================================
|
|
236
|
+
;; Node layout (16 bytes): +0 kind (0=prod,1=union), +4 width, +8 numEdges, +12 edgesPtr
|
|
237
|
+
;; Edge layout (8 bytes): +0 tagId, +4 targetNodeId
|
|
238
|
+
|
|
239
|
+
(func $schema_kind (param $sid i32) (result i32)
|
|
240
|
+
(i32.load (i32.add (global.get $DATA_SCHEMA_NODES) (i32.shl (local.get $sid) (i32.const 4))))
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
(func $schema_width (param $sid i32) (result i32)
|
|
244
|
+
(i32.load (i32.add (i32.add (global.get $DATA_SCHEMA_NODES) (i32.shl (local.get $sid) (i32.const 4))) (i32.const 4)))
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
(func $schema_num_edges (param $sid i32) (result i32)
|
|
248
|
+
(i32.load (i32.add (i32.add (global.get $DATA_SCHEMA_NODES) (i32.shl (local.get $sid) (i32.const 4))) (i32.const 8)))
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
(func $schema_edges_ptr (param $sid i32) (result i32)
|
|
252
|
+
(i32.load (i32.add (i32.add (global.get $DATA_SCHEMA_NODES) (i32.shl (local.get $sid) (i32.const 4))) (i32.const 12)))
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
(func $schema_edge_tag (param $sid i32) (param $ord i32) (result i32)
|
|
256
|
+
(i32.load (i32.add (call $schema_edges_ptr (local.get $sid)) (i32.shl (local.get $ord) (i32.const 3))))
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
(func $schema_edge_target (param $sid i32) (param $ord i32) (result i32)
|
|
260
|
+
(i32.load (i32.add (i32.add (call $schema_edges_ptr (local.get $sid)) (i32.shl (local.get $ord) (i32.const 3))) (i32.const 4)))
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
;; Find ordinal of tagId in union schema `sid`, or -1 if not found.
|
|
264
|
+
(func $schema_find_tag (param $sid i32) (param $tagId i32) (result i32)
|
|
265
|
+
(local $n i32)
|
|
266
|
+
(local $ep i32)
|
|
267
|
+
(local $i i32)
|
|
268
|
+
(local.set $n (call $schema_num_edges (local.get $sid)))
|
|
269
|
+
(local.set $ep (call $schema_edges_ptr (local.get $sid)))
|
|
270
|
+
(local.set $i (i32.const 0))
|
|
271
|
+
(block $brk
|
|
272
|
+
(loop $lp
|
|
273
|
+
(br_if $brk (i32.ge_s (local.get $i) (local.get $n)))
|
|
274
|
+
(if (i32.eq (i32.load (i32.add (local.get $ep) (i32.shl (local.get $i) (i32.const 3)))) (local.get $tagId))
|
|
275
|
+
(then (return (local.get $i))))
|
|
276
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
277
|
+
(br $lp)
|
|
278
|
+
)
|
|
279
|
+
)
|
|
280
|
+
(i32.const -1)
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
;; ============================================================================
|
|
284
|
+
;; 3. BitDeque (Channel) ADT (`<= 128b` inline + growable power-of-2 ring)
|
|
285
|
+
;; ============================================================================
|
|
286
|
+
|
|
287
|
+
;; Channel layout (72 bytes):
|
|
288
|
+
;; +0 status, +4 prodSchema, +8 consSchema, +12 aligned, +16 producer, +20 consumer,
|
|
289
|
+
;; +24 scopePtr, +28 nextInScope, +32 bufPtr, +36 capWords, +40 head, +44 len,
|
|
290
|
+
;; +48..63 inlineBuf[4], +64 prevInScope, +68 pinCount
|
|
291
|
+
(func $try_queue_chan (param $ch i32)
|
|
292
|
+
(if (i32.and
|
|
293
|
+
(i32.eq (i32.load (local.get $ch)) (i32.const 3))
|
|
294
|
+
(i32.and
|
|
295
|
+
(i32.le_s (i32.load (i32.add (local.get $ch) (i32.const 68))) (i32.const 0))
|
|
296
|
+
(i32.and
|
|
297
|
+
(i32.eqz (i32.load (i32.add (local.get $ch) (i32.const 16))))
|
|
298
|
+
(i32.eqz (i32.load (i32.add (local.get $ch) (i32.const 20)))))))
|
|
299
|
+
(then
|
|
300
|
+
(i32.store (local.get $ch) (i32.const 4)) ;; QUEUED
|
|
301
|
+
(i32.store (i32.add (local.get $ch) (i32.const 28)) (global.get $g_dead_chans))
|
|
302
|
+
(global.set $g_dead_chans (local.get $ch))
|
|
303
|
+
)
|
|
304
|
+
)
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
(func $chan_pin (param $ch i32)
|
|
308
|
+
(if (i32.ne (local.get $ch) (i32.const 0))
|
|
309
|
+
(then (i32.store (i32.add (local.get $ch) (i32.const 68)) (i32.add (i32.load (i32.add (local.get $ch) (i32.const 68))) (i32.const 1)))))
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
(func $chan_unpin (param $ch i32)
|
|
313
|
+
(local $cnt i32)
|
|
314
|
+
(if (i32.eqz (local.get $ch)) (then (return)))
|
|
315
|
+
(local.set $cnt (i32.sub (i32.load (i32.add (local.get $ch) (i32.const 68))) (i32.const 1)))
|
|
316
|
+
(i32.store (i32.add (local.get $ch) (i32.const 68)) (local.get $cnt))
|
|
317
|
+
(if (i32.and (i32.le_s (local.get $cnt) (i32.const 0)) (i32.eq (i32.load (local.get $ch)) (i32.const 3)))
|
|
318
|
+
(then (call $try_queue_chan (local.get $ch))))
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
(func $scope_unlink_chan (param $ch i32)
|
|
322
|
+
(local $sc i32)
|
|
323
|
+
(local $prev i32)
|
|
324
|
+
(local $next i32)
|
|
325
|
+
(local.set $sc (i32.load (i32.add (local.get $ch) (i32.const 24))))
|
|
326
|
+
(if (i32.eqz (local.get $sc)) (then (return)))
|
|
327
|
+
(local.set $prev (i32.load (i32.add (local.get $ch) (i32.const 64))))
|
|
328
|
+
(local.set $next (i32.load (i32.add (local.get $ch) (i32.const 28))))
|
|
329
|
+
(if (i32.ne (local.get $prev) (i32.const 0))
|
|
330
|
+
(then (i32.store (i32.add (local.get $prev) (i32.const 28)) (local.get $next)))
|
|
331
|
+
(else
|
|
332
|
+
(if (i32.eq (i32.load (i32.add (local.get $sc) (i32.const 16))) (local.get $ch))
|
|
333
|
+
(then (i32.store (i32.add (local.get $sc) (i32.const 16)) (local.get $next))))))
|
|
334
|
+
(if (i32.ne (local.get $next) (i32.const 0))
|
|
335
|
+
(then (i32.store (i32.add (local.get $next) (i32.const 64)) (local.get $prev))))
|
|
336
|
+
(i32.store (i32.add (local.get $ch) (i32.const 24)) (i32.const 0))
|
|
337
|
+
(i32.store (i32.add (local.get $ch) (i32.const 28)) (i32.const 0))
|
|
338
|
+
(i32.store (i32.add (local.get $ch) (i32.const 64)) (i32.const 0))
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
(func $chan_alloc (param $scopePtr i32) (param $schemaId i32) (result i32)
|
|
342
|
+
(local $ch i32)
|
|
343
|
+
(local $oldFirst i32)
|
|
344
|
+
(local.set $ch (call $alloc (i32.const 72)))
|
|
345
|
+
(if (i32.eqz (local.get $ch)) (then (return (i32.const 0))))
|
|
346
|
+
(i32.store (local.get $ch) (i32.const 0)) ;; status = OPEN
|
|
347
|
+
(i32.store (i32.add (local.get $ch) (i32.const 4)) (local.get $schemaId)) ;; prodSchema
|
|
348
|
+
(i32.store (i32.add (local.get $ch) (i32.const 8)) (local.get $schemaId)) ;; consSchema
|
|
349
|
+
(i32.store (i32.add (local.get $ch) (i32.const 12)) (i32.const 1)) ;; aligned = 1
|
|
350
|
+
(i32.store (i32.add (local.get $ch) (i32.const 24)) (local.get $scopePtr))
|
|
351
|
+
(i32.store (i32.add (local.get $ch) (i32.const 32)) (i32.add (local.get $ch) (i32.const 48))) ;; inline bufPtr
|
|
352
|
+
(i32.store (i32.add (local.get $ch) (i32.const 36)) (i32.const 4)) ;; capWords = 4 (128 bits)
|
|
353
|
+
(call $quota_add_bytes (i32.const 16))
|
|
354
|
+
(if (i32.ne (local.get $scopePtr) (i32.const 0))
|
|
355
|
+
(then
|
|
356
|
+
(local.set $oldFirst (i32.load (i32.add (local.get $scopePtr) (i32.const 16))))
|
|
357
|
+
(i32.store (i32.add (local.get $ch) (i32.const 28)) (local.get $oldFirst))
|
|
358
|
+
(if (i32.ne (local.get $oldFirst) (i32.const 0))
|
|
359
|
+
(then (i32.store (i32.add (local.get $oldFirst) (i32.const 64)) (local.get $ch))))
|
|
360
|
+
(i32.store (i32.add (local.get $scopePtr) (i32.const 16)) (local.get $ch))
|
|
361
|
+
)
|
|
362
|
+
)
|
|
363
|
+
(local.get $ch)
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
(func $bd_close (param $ch i32)
|
|
367
|
+
(if (i32.eqz (local.get $ch)) (then (return)))
|
|
368
|
+
(if (i32.eq (i32.load (local.get $ch)) (i32.const 0))
|
|
369
|
+
(then
|
|
370
|
+
(i32.store (local.get $ch) (i32.const 1))
|
|
371
|
+
(i32.store (i32.add (local.get $ch) (i32.const 16)) (i32.const 0))
|
|
372
|
+
)
|
|
373
|
+
)
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
(func $bd_fail (param $ch i32)
|
|
377
|
+
(if (i32.eqz (local.get $ch)) (then (return)))
|
|
378
|
+
(if (i32.eq (i32.load (local.get $ch)) (i32.const 0))
|
|
379
|
+
(then
|
|
380
|
+
(i32.store (local.get $ch) (i32.const 2))
|
|
381
|
+
(i32.store (i32.add (local.get $ch) (i32.const 16)) (i32.const 0))
|
|
382
|
+
)
|
|
383
|
+
)
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
(func $bd_release (param $ch i32)
|
|
387
|
+
(local $capW i32)
|
|
388
|
+
(local $bufPtr i32)
|
|
389
|
+
(if (i32.eqz (local.get $ch)) (then (return)))
|
|
390
|
+
(if (i32.ge_s (i32.load (local.get $ch)) (i32.const 3)) (then (return)))
|
|
391
|
+
(local.set $capW (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
392
|
+
(local.set $bufPtr (i32.load (i32.add (local.get $ch) (i32.const 32))))
|
|
393
|
+
(if (i32.gt_s (local.get $capW) (i32.const 0))
|
|
394
|
+
(then
|
|
395
|
+
(call $quota_add_bytes (i32.sub (i32.const 0) (i32.shl (local.get $capW) (i32.const 2))))
|
|
396
|
+
(if (i32.and (i32.ne (local.get $bufPtr) (i32.const 0))
|
|
397
|
+
(i32.ne (local.get $bufPtr) (i32.add (local.get $ch) (i32.const 48))))
|
|
398
|
+
(then (call $free (local.get $bufPtr))))
|
|
399
|
+
)
|
|
400
|
+
)
|
|
401
|
+
(i32.store (local.get $ch) (i32.const 3))
|
|
402
|
+
(i32.store (i32.add (local.get $ch) (i32.const 32)) (i32.const 0))
|
|
403
|
+
(i32.store (i32.add (local.get $ch) (i32.const 36)) (i32.const 0))
|
|
404
|
+
(i32.store (i32.add (local.get $ch) (i32.const 44)) (i32.const 0))
|
|
405
|
+
(call $scope_unlink_chan (local.get $ch))
|
|
406
|
+
(call $try_queue_chan (local.get $ch))
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
(func $bd_grow (param $ch i32) (param $minBits i32)
|
|
410
|
+
(local $oldWords i32)
|
|
411
|
+
(local $oldBuf i32)
|
|
412
|
+
(local $oldWordMask i32)
|
|
413
|
+
(local $oldBitMask i32)
|
|
414
|
+
(local $oldHead i32)
|
|
415
|
+
(local $oldLen i32)
|
|
416
|
+
(local $newWords i32)
|
|
417
|
+
(local $newBuf i32)
|
|
418
|
+
(local $rem i32)
|
|
419
|
+
(local $srcPos i32)
|
|
420
|
+
(local $dstPos i32)
|
|
421
|
+
(local $chunk i32)
|
|
422
|
+
(local $wi i32)
|
|
423
|
+
(local $bo i32)
|
|
424
|
+
(local $val i32)
|
|
425
|
+
(local $lo i32)
|
|
426
|
+
(local $hiBits i32)
|
|
427
|
+
(local $w1 i32)
|
|
428
|
+
(local $dwi i32)
|
|
429
|
+
(local $dbo i32)
|
|
430
|
+
|
|
431
|
+
(if (i32.ge_s (i32.load (local.get $ch)) (i32.const 3)) (then (return)))
|
|
432
|
+
(local.set $oldBuf (i32.load (i32.add (local.get $ch) (i32.const 32))))
|
|
433
|
+
(local.set $oldWords (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
434
|
+
(local.set $oldHead (i32.load (i32.add (local.get $ch) (i32.const 40))))
|
|
435
|
+
(local.set $oldLen (i32.load (i32.add (local.get $ch) (i32.const 44))))
|
|
436
|
+
|
|
437
|
+
(local.set $newWords (i32.shl (local.get $oldWords) (i32.const 1)))
|
|
438
|
+
(if (i32.lt_s (local.get $newWords) (i32.const 8))
|
|
439
|
+
(then (local.set $newWords (i32.const 8))))
|
|
440
|
+
(block $brkW
|
|
441
|
+
(loop $lpW
|
|
442
|
+
(br_if $brkW (i32.ge_s (i32.shl (local.get $newWords) (i32.const 5)) (local.get $minBits)))
|
|
443
|
+
(local.set $newWords (i32.shl (local.get $newWords) (i32.const 1)))
|
|
444
|
+
(br $lpW)
|
|
445
|
+
)
|
|
446
|
+
)
|
|
447
|
+
|
|
448
|
+
(local.set $newBuf (call $alloc (i32.shl (local.get $newWords) (i32.const 2))))
|
|
449
|
+
(if (i32.eqz (local.get $newBuf)) (then (return)))
|
|
450
|
+
(call $quota_add_bytes (i32.shl (i32.sub (local.get $newWords) (local.get $oldWords)) (i32.const 2)))
|
|
451
|
+
|
|
452
|
+
(local.set $oldWordMask (i32.sub (local.get $oldWords) (i32.const 1)))
|
|
453
|
+
(local.set $oldBitMask (i32.sub (i32.shl (local.get $oldWords) (i32.const 5)) (i32.const 1)))
|
|
454
|
+
(local.set $rem (local.get $oldLen))
|
|
455
|
+
(local.set $srcPos (local.get $oldHead))
|
|
456
|
+
(local.set $dstPos (i32.const 0))
|
|
457
|
+
|
|
458
|
+
(block $brkC
|
|
459
|
+
(loop $lpC
|
|
460
|
+
(br_if $brkC (i32.le_s (local.get $rem) (i32.const 0)))
|
|
461
|
+
(local.set $chunk (select (i32.const 30) (local.get $rem) (i32.gt_s (local.get $rem) (i32.const 30))))
|
|
462
|
+
(local.set $wi (i32.shr_u (local.get $srcPos) (i32.const 5)))
|
|
463
|
+
(local.set $bo (i32.and (local.get $srcPos) (i32.const 31)))
|
|
464
|
+
(if (i32.le_s (i32.add (local.get $bo) (local.get $chunk)) (i32.const 32))
|
|
465
|
+
(then
|
|
466
|
+
(local.set $val
|
|
467
|
+
(i32.and
|
|
468
|
+
(i32.shr_u
|
|
469
|
+
(i32.load (i32.add (local.get $oldBuf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
470
|
+
(i32.sub (i32.const 32) (i32.add (local.get $bo) (local.get $chunk))))
|
|
471
|
+
(call $mask32 (local.get $chunk))))
|
|
472
|
+
)
|
|
473
|
+
(else
|
|
474
|
+
(local.set $lo (i32.sub (i32.add (local.get $bo) (local.get $chunk)) (i32.const 32)))
|
|
475
|
+
(local.set $hiBits (i32.sub (i32.const 32) (local.get $bo)))
|
|
476
|
+
(local.set $w1 (i32.and (i32.add (local.get $wi) (i32.const 1)) (local.get $oldWordMask)))
|
|
477
|
+
(local.set $val
|
|
478
|
+
(i32.or
|
|
479
|
+
(i32.shl
|
|
480
|
+
(i32.and
|
|
481
|
+
(i32.load (i32.add (local.get $oldBuf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
482
|
+
(call $mask32 (local.get $hiBits)))
|
|
483
|
+
(local.get $lo))
|
|
484
|
+
(i32.shr_u
|
|
485
|
+
(i32.load (i32.add (local.get $oldBuf) (i32.shl (local.get $w1) (i32.const 2))))
|
|
486
|
+
(i32.sub (i32.const 32) (local.get $lo)))))
|
|
487
|
+
)
|
|
488
|
+
)
|
|
489
|
+
(local.set $dwi (i32.shr_u (local.get $dstPos) (i32.const 5)))
|
|
490
|
+
(local.set $dbo (i32.and (local.get $dstPos) (i32.const 31)))
|
|
491
|
+
(if (i32.le_s (i32.add (local.get $dbo) (local.get $chunk)) (i32.const 32))
|
|
492
|
+
(then
|
|
493
|
+
(i32.store
|
|
494
|
+
(i32.add (local.get $newBuf) (i32.shl (local.get $dwi) (i32.const 2)))
|
|
495
|
+
(i32.or
|
|
496
|
+
(i32.load (i32.add (local.get $newBuf) (i32.shl (local.get $dwi) (i32.const 2))))
|
|
497
|
+
(i32.shl (local.get $val) (i32.sub (i32.const 32) (i32.add (local.get $dbo) (local.get $chunk))))))
|
|
498
|
+
)
|
|
499
|
+
(else
|
|
500
|
+
(local.set $lo (i32.sub (i32.add (local.get $dbo) (local.get $chunk)) (i32.const 32)))
|
|
501
|
+
(i32.store
|
|
502
|
+
(i32.add (local.get $newBuf) (i32.shl (local.get $dwi) (i32.const 2)))
|
|
503
|
+
(i32.or
|
|
504
|
+
(i32.load (i32.add (local.get $newBuf) (i32.shl (local.get $dwi) (i32.const 2))))
|
|
505
|
+
(i32.shr_u (local.get $val) (local.get $lo))))
|
|
506
|
+
(i32.store
|
|
507
|
+
(i32.add (local.get $newBuf) (i32.shl (i32.add (local.get $dwi) (i32.const 1)) (i32.const 2)))
|
|
508
|
+
(i32.or
|
|
509
|
+
(i32.load (i32.add (local.get $newBuf) (i32.shl (i32.add (local.get $dwi) (i32.const 1)) (i32.const 2))))
|
|
510
|
+
(i32.shl (local.get $val) (i32.sub (i32.const 32) (local.get $lo)))))
|
|
511
|
+
)
|
|
512
|
+
)
|
|
513
|
+
(local.set $srcPos (i32.and (i32.add (local.get $srcPos) (local.get $chunk)) (local.get $oldBitMask)))
|
|
514
|
+
(local.set $dstPos (i32.add (local.get $dstPos) (local.get $chunk)))
|
|
515
|
+
(local.set $rem (i32.sub (local.get $rem) (local.get $chunk)))
|
|
516
|
+
(br $lpC)
|
|
517
|
+
)
|
|
518
|
+
)
|
|
519
|
+
|
|
520
|
+
(if (i32.and (i32.ne (local.get $oldBuf) (i32.const 0))
|
|
521
|
+
(i32.ne (local.get $oldBuf) (i32.add (local.get $ch) (i32.const 48))))
|
|
522
|
+
(then (call $free (local.get $oldBuf))))
|
|
523
|
+
(i32.store (i32.add (local.get $ch) (i32.const 32)) (local.get $newBuf))
|
|
524
|
+
(i32.store (i32.add (local.get $ch) (i32.const 36)) (local.get $newWords))
|
|
525
|
+
(i32.store (i32.add (local.get $ch) (i32.const 40)) (i32.const 0))
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
(func $bd_push_small (param $ch i32) (param $val i32) (param $w i32)
|
|
529
|
+
(local $len i32)
|
|
530
|
+
(local $capW i32)
|
|
531
|
+
(local $capBits i32)
|
|
532
|
+
(local $buf i32)
|
|
533
|
+
(local $v i32)
|
|
534
|
+
(local $tailPos i32)
|
|
535
|
+
(local $wi i32)
|
|
536
|
+
(local $bo i32)
|
|
537
|
+
(local $sh i32)
|
|
538
|
+
(local $clr i32)
|
|
539
|
+
(local $lo i32)
|
|
540
|
+
(local $hiBits i32)
|
|
541
|
+
(local $w1 i32)
|
|
542
|
+
(local $addr i32)
|
|
543
|
+
|
|
544
|
+
(if (i32.or (i32.le_s (local.get $w) (i32.const 0)) (i32.ge_s (i32.load (local.get $ch)) (i32.const 3))) (then (return)))
|
|
545
|
+
(local.set $len (i32.load (i32.add (local.get $ch) (i32.const 44))))
|
|
546
|
+
(local.set $capW (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
547
|
+
(local.set $capBits (i32.shl (local.get $capW) (i32.const 5)))
|
|
548
|
+
(if (i32.gt_s (i32.add (local.get $len) (local.get $w)) (local.get $capBits))
|
|
549
|
+
(then
|
|
550
|
+
(call $bd_grow (local.get $ch) (i32.add (local.get $len) (local.get $w)))
|
|
551
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return)))
|
|
552
|
+
(local.set $capW (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
553
|
+
(local.set $capBits (i32.shl (local.get $capW) (i32.const 5)))
|
|
554
|
+
)
|
|
555
|
+
)
|
|
556
|
+
(local.set $buf (i32.load (i32.add (local.get $ch) (i32.const 32))))
|
|
557
|
+
(local.set $v (i32.and (local.get $val) (call $mask32 (local.get $w))))
|
|
558
|
+
(local.set $tailPos
|
|
559
|
+
(i32.and
|
|
560
|
+
(i32.add (i32.load (i32.add (local.get $ch) (i32.const 40))) (local.get $len))
|
|
561
|
+
(i32.sub (local.get $capBits) (i32.const 1))))
|
|
562
|
+
(local.set $wi (i32.shr_u (local.get $tailPos) (i32.const 5)))
|
|
563
|
+
(local.set $bo (i32.and (local.get $tailPos) (i32.const 31)))
|
|
564
|
+
|
|
565
|
+
(if (i32.le_s (i32.add (local.get $bo) (local.get $w)) (i32.const 32))
|
|
566
|
+
(then
|
|
567
|
+
(local.set $sh (i32.sub (i32.const 32) (i32.add (local.get $bo) (local.get $w))))
|
|
568
|
+
(local.set $clr (i32.xor (i32.shl (call $mask32 (local.get $w)) (local.get $sh)) (i32.const -1)))
|
|
569
|
+
(local.set $addr (i32.add (local.get $buf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
570
|
+
(i32.store (local.get $addr)
|
|
571
|
+
(i32.or (i32.and (i32.load (local.get $addr)) (local.get $clr)) (i32.shl (local.get $v) (local.get $sh))))
|
|
572
|
+
)
|
|
573
|
+
(else
|
|
574
|
+
(local.set $lo (i32.sub (i32.add (local.get $bo) (local.get $w)) (i32.const 32)))
|
|
575
|
+
(local.set $hiBits (i32.sub (i32.const 32) (local.get $bo)))
|
|
576
|
+
(local.set $w1 (i32.and (i32.add (local.get $wi) (i32.const 1)) (i32.sub (local.get $capW) (i32.const 1))))
|
|
577
|
+
(local.set $addr (i32.add (local.get $buf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
578
|
+
(i32.store (local.get $addr)
|
|
579
|
+
(i32.or
|
|
580
|
+
(i32.and (i32.load (local.get $addr)) (i32.xor (call $mask32 (local.get $hiBits)) (i32.const -1)))
|
|
581
|
+
(i32.shr_u (local.get $v) (local.get $lo))))
|
|
582
|
+
(local.set $sh (i32.sub (i32.const 32) (local.get $lo)))
|
|
583
|
+
(local.set $addr (i32.add (local.get $buf) (i32.shl (local.get $w1) (i32.const 2))))
|
|
584
|
+
(i32.store (local.get $addr)
|
|
585
|
+
(i32.or
|
|
586
|
+
(i32.and (i32.load (local.get $addr)) (i32.xor (i32.shl (call $mask32 (local.get $lo)) (local.get $sh)) (i32.const -1)))
|
|
587
|
+
(i32.shl (local.get $v) (local.get $sh))))
|
|
588
|
+
)
|
|
589
|
+
)
|
|
590
|
+
(i32.store (i32.add (local.get $ch) (i32.const 44)) (i32.add (local.get $len) (local.get $w)))
|
|
591
|
+
)
|
|
592
|
+
|
|
593
|
+
(func $bd_push_bits (param $ch i32) (param $val i32) (param $w i32)
|
|
594
|
+
(if (i32.le_s (local.get $w) (i32.const 0)) (then (return)))
|
|
595
|
+
(if (i32.le_s (local.get $w) (i32.const 30))
|
|
596
|
+
(then
|
|
597
|
+
(call $bd_push_small (local.get $ch) (local.get $val) (local.get $w))
|
|
598
|
+
(return)))
|
|
599
|
+
(call $bd_push_small (local.get $ch) (i32.shr_u (local.get $val) (i32.const 16)) (i32.sub (local.get $w) (i32.const 16)))
|
|
600
|
+
(call $bd_push_small (local.get $ch) (i32.and (local.get $val) (i32.const 65535)) (i32.const 16))
|
|
601
|
+
)
|
|
602
|
+
|
|
603
|
+
(func $bd_unshift_small (param $ch i32) (param $val i32) (param $w i32)
|
|
604
|
+
(local $len i32)
|
|
605
|
+
(local $capW i32)
|
|
606
|
+
(local $capBits i32)
|
|
607
|
+
(local $buf i32)
|
|
608
|
+
(local $v i32)
|
|
609
|
+
(local $newHead i32)
|
|
610
|
+
(local $wi i32)
|
|
611
|
+
(local $bo i32)
|
|
612
|
+
(local $sh i32)
|
|
613
|
+
(local $clr i32)
|
|
614
|
+
(local $lo i32)
|
|
615
|
+
(local $hiBits i32)
|
|
616
|
+
(local $w1 i32)
|
|
617
|
+
(local $addr i32)
|
|
618
|
+
|
|
619
|
+
(if (i32.or (i32.le_s (local.get $w) (i32.const 0)) (i32.ge_s (i32.load (local.get $ch)) (i32.const 3))) (then (return)))
|
|
620
|
+
(local.set $len (i32.load (i32.add (local.get $ch) (i32.const 44))))
|
|
621
|
+
(local.set $capW (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
622
|
+
(local.set $capBits (i32.shl (local.get $capW) (i32.const 5)))
|
|
623
|
+
(if (i32.gt_s (i32.add (local.get $len) (local.get $w)) (local.get $capBits))
|
|
624
|
+
(then
|
|
625
|
+
(call $bd_grow (local.get $ch) (i32.add (local.get $len) (local.get $w)))
|
|
626
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return)))
|
|
627
|
+
(local.set $capW (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
628
|
+
(local.set $capBits (i32.shl (local.get $capW) (i32.const 5)))
|
|
629
|
+
)
|
|
630
|
+
)
|
|
631
|
+
(local.set $buf (i32.load (i32.add (local.get $ch) (i32.const 32))))
|
|
632
|
+
(local.set $v (i32.and (local.get $val) (call $mask32 (local.get $w))))
|
|
633
|
+
(local.set $newHead
|
|
634
|
+
(i32.and
|
|
635
|
+
(i32.sub (i32.load (i32.add (local.get $ch) (i32.const 40))) (local.get $w))
|
|
636
|
+
(i32.sub (local.get $capBits) (i32.const 1))))
|
|
637
|
+
(local.set $wi (i32.shr_u (local.get $newHead) (i32.const 5)))
|
|
638
|
+
(local.set $bo (i32.and (local.get $newHead) (i32.const 31)))
|
|
639
|
+
|
|
640
|
+
(if (i32.le_s (i32.add (local.get $bo) (local.get $w)) (i32.const 32))
|
|
641
|
+
(then
|
|
642
|
+
(local.set $sh (i32.sub (i32.const 32) (i32.add (local.get $bo) (local.get $w))))
|
|
643
|
+
(local.set $clr (i32.xor (i32.shl (call $mask32 (local.get $w)) (local.get $sh)) (i32.const -1)))
|
|
644
|
+
(local.set $addr (i32.add (local.get $buf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
645
|
+
(i32.store (local.get $addr)
|
|
646
|
+
(i32.or (i32.and (i32.load (local.get $addr)) (local.get $clr)) (i32.shl (local.get $v) (local.get $sh))))
|
|
647
|
+
)
|
|
648
|
+
(else
|
|
649
|
+
(local.set $lo (i32.sub (i32.add (local.get $bo) (local.get $w)) (i32.const 32)))
|
|
650
|
+
(local.set $hiBits (i32.sub (i32.const 32) (local.get $bo)))
|
|
651
|
+
(local.set $w1 (i32.and (i32.add (local.get $wi) (i32.const 1)) (i32.sub (local.get $capW) (i32.const 1))))
|
|
652
|
+
(local.set $addr (i32.add (local.get $buf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
653
|
+
(i32.store (local.get $addr)
|
|
654
|
+
(i32.or
|
|
655
|
+
(i32.and (i32.load (local.get $addr)) (i32.xor (call $mask32 (local.get $hiBits)) (i32.const -1)))
|
|
656
|
+
(i32.shr_u (local.get $v) (local.get $lo))))
|
|
657
|
+
(local.set $sh (i32.sub (i32.const 32) (local.get $lo)))
|
|
658
|
+
(local.set $addr (i32.add (local.get $buf) (i32.shl (local.get $w1) (i32.const 2))))
|
|
659
|
+
(i32.store (local.get $addr)
|
|
660
|
+
(i32.or
|
|
661
|
+
(i32.and (i32.load (local.get $addr)) (i32.xor (i32.shl (call $mask32 (local.get $lo)) (local.get $sh)) (i32.const -1)))
|
|
662
|
+
(i32.shl (local.get $v) (local.get $sh))))
|
|
663
|
+
)
|
|
664
|
+
)
|
|
665
|
+
(i32.store (i32.add (local.get $ch) (i32.const 40)) (local.get $newHead))
|
|
666
|
+
(i32.store (i32.add (local.get $ch) (i32.const 44)) (i32.add (local.get $len) (local.get $w)))
|
|
667
|
+
)
|
|
668
|
+
|
|
669
|
+
(func $bd_unshift_bits (param $ch i32) (param $val i32) (param $w i32)
|
|
670
|
+
(if (i32.le_s (local.get $w) (i32.const 0)) (then (return)))
|
|
671
|
+
(if (i32.le_s (local.get $w) (i32.const 30))
|
|
672
|
+
(then
|
|
673
|
+
(call $bd_unshift_small (local.get $ch) (local.get $val) (local.get $w))
|
|
674
|
+
(return)))
|
|
675
|
+
(call $bd_unshift_small (local.get $ch) (i32.and (local.get $val) (i32.const 65535)) (i32.const 16))
|
|
676
|
+
(call $bd_unshift_small (local.get $ch) (i32.shr_u (local.get $val) (i32.const 16)) (i32.sub (local.get $w) (i32.const 16)))
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
(func $bd_peek_small_at (param $ch i32) (param $bitOff i32) (param $w i32) (result i32)
|
|
680
|
+
(local $buf i32)
|
|
681
|
+
(local $capW i32)
|
|
682
|
+
(local $pos i32)
|
|
683
|
+
(local $wi i32)
|
|
684
|
+
(local $bo i32)
|
|
685
|
+
(local $lo i32)
|
|
686
|
+
(local $hiBits i32)
|
|
687
|
+
(local $w1 i32)
|
|
688
|
+
|
|
689
|
+
(if (i32.le_s (local.get $w) (i32.const 0)) (then (return (i32.const 0))))
|
|
690
|
+
(local.set $buf (i32.load (i32.add (local.get $ch) (i32.const 32))))
|
|
691
|
+
(local.set $capW (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
692
|
+
(local.set $pos
|
|
693
|
+
(i32.and
|
|
694
|
+
(i32.add (i32.load (i32.add (local.get $ch) (i32.const 40))) (local.get $bitOff))
|
|
695
|
+
(i32.sub (i32.shl (local.get $capW) (i32.const 5)) (i32.const 1))))
|
|
696
|
+
(local.set $wi (i32.shr_u (local.get $pos) (i32.const 5)))
|
|
697
|
+
(local.set $bo (i32.and (local.get $pos) (i32.const 31)))
|
|
698
|
+
|
|
699
|
+
(if (i32.le_s (i32.add (local.get $bo) (local.get $w)) (i32.const 32))
|
|
700
|
+
(then
|
|
701
|
+
(return
|
|
702
|
+
(i32.and
|
|
703
|
+
(i32.shr_u
|
|
704
|
+
(i32.load (i32.add (local.get $buf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
705
|
+
(i32.sub (i32.const 32) (i32.add (local.get $bo) (local.get $w))))
|
|
706
|
+
(call $mask32 (local.get $w))))
|
|
707
|
+
)
|
|
708
|
+
)
|
|
709
|
+
(local.set $lo (i32.sub (i32.add (local.get $bo) (local.get $w)) (i32.const 32)))
|
|
710
|
+
(local.set $hiBits (i32.sub (i32.const 32) (local.get $bo)))
|
|
711
|
+
(local.set $w1 (i32.and (i32.add (local.get $wi) (i32.const 1)) (i32.sub (local.get $capW) (i32.const 1))))
|
|
712
|
+
(i32.or
|
|
713
|
+
(i32.shl
|
|
714
|
+
(i32.and
|
|
715
|
+
(i32.load (i32.add (local.get $buf) (i32.shl (local.get $wi) (i32.const 2))))
|
|
716
|
+
(call $mask32 (local.get $hiBits)))
|
|
717
|
+
(local.get $lo))
|
|
718
|
+
(i32.shr_u
|
|
719
|
+
(i32.load (i32.add (local.get $buf) (i32.shl (local.get $w1) (i32.const 2))))
|
|
720
|
+
(i32.sub (i32.const 32) (local.get $lo))))
|
|
721
|
+
)
|
|
722
|
+
|
|
723
|
+
(func $bd_peek_bits_at (param $ch i32) (param $bitOff i32) (param $w i32) (result i32)
|
|
724
|
+
(local $hiW i32)
|
|
725
|
+
(if (i32.le_s (local.get $w) (i32.const 0)) (then (return (i32.const 0))))
|
|
726
|
+
(if (i32.le_s (local.get $w) (i32.const 30))
|
|
727
|
+
(then (return (call $bd_peek_small_at (local.get $ch) (local.get $bitOff) (local.get $w)))))
|
|
728
|
+
(local.set $hiW (i32.sub (local.get $w) (i32.const 16)))
|
|
729
|
+
(i32.or
|
|
730
|
+
(i32.shl (call $bd_peek_small_at (local.get $ch) (local.get $bitOff) (local.get $hiW)) (i32.const 16))
|
|
731
|
+
(call $bd_peek_small_at (local.get $ch) (i32.add (local.get $bitOff) (local.get $hiW)) (i32.const 16)))
|
|
732
|
+
)
|
|
733
|
+
|
|
734
|
+
(func $bd_shift_bits (param $ch i32) (param $w i32) (result i32)
|
|
735
|
+
(local $val i32)
|
|
736
|
+
(local $capW i32)
|
|
737
|
+
(if (i32.le_s (local.get $w) (i32.const 0)) (then (return (i32.const 0))))
|
|
738
|
+
(local.set $val (call $bd_peek_bits_at (local.get $ch) (i32.const 0) (local.get $w)))
|
|
739
|
+
(local.set $capW (i32.load (i32.add (local.get $ch) (i32.const 36))))
|
|
740
|
+
(i32.store (i32.add (local.get $ch) (i32.const 40))
|
|
741
|
+
(i32.and
|
|
742
|
+
(i32.add (i32.load (i32.add (local.get $ch) (i32.const 40))) (local.get $w))
|
|
743
|
+
(i32.sub (i32.shl (local.get $capW) (i32.const 5)) (i32.const 1))))
|
|
744
|
+
(i32.store (i32.add (local.get $ch) (i32.const 44))
|
|
745
|
+
(i32.sub (i32.load (i32.add (local.get $ch) (i32.const 44))) (local.get $w)))
|
|
746
|
+
(local.get $val)
|
|
747
|
+
)
|
|
748
|
+
|
|
749
|
+
(func $bd_push_from_deque (param $dst i32) (param $src i32)
|
|
750
|
+
(local $srcLen i32)
|
|
751
|
+
(local $chunk i32)
|
|
752
|
+
(local $bits i32)
|
|
753
|
+
(local.set $srcLen (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
754
|
+
(if (i32.le_s (local.get $srcLen) (i32.const 0)) (then (return)))
|
|
755
|
+
(if (i32.gt_s
|
|
756
|
+
(i32.add (i32.load (i32.add (local.get $dst) (i32.const 44))) (local.get $srcLen))
|
|
757
|
+
(i32.shl (i32.load (i32.add (local.get $dst) (i32.const 36))) (i32.const 5)))
|
|
758
|
+
(then
|
|
759
|
+
(call $bd_grow (local.get $dst) (i32.add (i32.load (i32.add (local.get $dst) (i32.const 44))) (local.get $srcLen)))
|
|
760
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return)))))
|
|
761
|
+
(block $brk
|
|
762
|
+
(loop $lp
|
|
763
|
+
(local.set $srcLen (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
764
|
+
(br_if $brk (i32.le_s (local.get $srcLen) (i32.const 0)))
|
|
765
|
+
(local.set $chunk (select (i32.const 30) (local.get $srcLen) (i32.gt_s (local.get $srcLen) (i32.const 30))))
|
|
766
|
+
(local.set $bits (call $bd_shift_bits (local.get $src) (local.get $chunk)))
|
|
767
|
+
(call $bd_push_small (local.get $dst) (local.get $bits) (local.get $chunk))
|
|
768
|
+
(br $lp)
|
|
769
|
+
)
|
|
770
|
+
)
|
|
771
|
+
)
|
|
772
|
+
|
|
773
|
+
(func $bd_unshift_from_deque (param $dst i32) (param $src i32)
|
|
774
|
+
(local $rem i32)
|
|
775
|
+
(local $chunk i32)
|
|
776
|
+
(local $bits i32)
|
|
777
|
+
(local.set $rem (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
778
|
+
(if (i32.le_s (local.get $rem) (i32.const 0)) (then (return)))
|
|
779
|
+
(if (i32.gt_s
|
|
780
|
+
(i32.add (i32.load (i32.add (local.get $dst) (i32.const 44))) (local.get $rem))
|
|
781
|
+
(i32.shl (i32.load (i32.add (local.get $dst) (i32.const 36))) (i32.const 5)))
|
|
782
|
+
(then
|
|
783
|
+
(call $bd_grow (local.get $dst) (i32.add (i32.load (i32.add (local.get $dst) (i32.const 44))) (local.get $rem)))
|
|
784
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return)))))
|
|
785
|
+
(block $brk
|
|
786
|
+
(loop $lp
|
|
787
|
+
(br_if $brk (i32.le_s (local.get $rem) (i32.const 0)))
|
|
788
|
+
(local.set $chunk (select (i32.const 30) (local.get $rem) (i32.gt_s (local.get $rem) (i32.const 30))))
|
|
789
|
+
(local.set $bits (call $bd_peek_small_at (local.get $src) (i32.sub (local.get $rem) (local.get $chunk)) (local.get $chunk)))
|
|
790
|
+
(call $bd_unshift_small (local.get $dst) (local.get $bits) (local.get $chunk))
|
|
791
|
+
(local.set $rem (i32.sub (local.get $rem) (local.get $chunk)))
|
|
792
|
+
(br $lp)
|
|
793
|
+
)
|
|
794
|
+
)
|
|
795
|
+
(i32.store (i32.add (local.get $src) (i32.const 40)) (i32.const 0))
|
|
796
|
+
(i32.store (i32.add (local.get $src) (i32.const 44)) (i32.const 0))
|
|
797
|
+
)
|
|
798
|
+
|
|
799
|
+
;; O(1) adopt buffer from `src` into empty `dst`
|
|
800
|
+
(func $bd_adopt_buffer (param $dst i32) (param $src i32)
|
|
801
|
+
(local $dstBuf i32)
|
|
802
|
+
(local $dstCapW i32)
|
|
803
|
+
(local $srcBuf i32)
|
|
804
|
+
(local $srcCapW i32)
|
|
805
|
+
(local.set $dstBuf (i32.load (i32.add (local.get $dst) (i32.const 32))))
|
|
806
|
+
(local.set $dstCapW (i32.load (i32.add (local.get $dst) (i32.const 36))))
|
|
807
|
+
(local.set $srcBuf (i32.load (i32.add (local.get $src) (i32.const 32))))
|
|
808
|
+
(local.set $srcCapW (i32.load (i32.add (local.get $src) (i32.const 36))))
|
|
809
|
+
|
|
810
|
+
;; Release dst's quota accounting and external buffer if any
|
|
811
|
+
(if (i32.gt_s (local.get $dstCapW) (i32.const 0))
|
|
812
|
+
(then
|
|
813
|
+
(call $quota_add_bytes (i32.sub (i32.const 0) (i32.shl (local.get $dstCapW) (i32.const 2))))
|
|
814
|
+
(if (i32.and (i32.ne (local.get $dstBuf) (i32.const 0))
|
|
815
|
+
(i32.ne (local.get $dstBuf) (i32.add (local.get $dst) (i32.const 48))))
|
|
816
|
+
(then (call $free (local.get $dstBuf))))
|
|
817
|
+
)
|
|
818
|
+
)
|
|
819
|
+
|
|
820
|
+
(if (i32.eq (local.get $srcBuf) (i32.add (local.get $src) (i32.const 48)))
|
|
821
|
+
(then
|
|
822
|
+
;; `src` uses inline 128-bit buffer -> copy 4 u32 words (16 bytes) into dst's inline buffer!
|
|
823
|
+
(i32.store (i32.add (local.get $dst) (i32.const 32)) (i32.add (local.get $dst) (i32.const 48)))
|
|
824
|
+
(i32.store (i32.add (local.get $dst) (i32.const 48)) (i32.load (i32.add (local.get $src) (i32.const 48))))
|
|
825
|
+
(i32.store (i32.add (local.get $dst) (i32.const 52)) (i32.load (i32.add (local.get $src) (i32.const 52))))
|
|
826
|
+
(i32.store (i32.add (local.get $dst) (i32.const 56)) (i32.load (i32.add (local.get $src) (i32.const 56))))
|
|
827
|
+
(i32.store (i32.add (local.get $dst) (i32.const 60)) (i32.load (i32.add (local.get $src) (i32.const 60))))
|
|
828
|
+
)
|
|
829
|
+
(else
|
|
830
|
+
;; External buffer -> zero-copy O(1) pointer transfer!
|
|
831
|
+
(i32.store (i32.add (local.get $dst) (i32.const 32)) (local.get $srcBuf))
|
|
832
|
+
)
|
|
833
|
+
)
|
|
834
|
+
(i32.store (i32.add (local.get $dst) (i32.const 36)) (local.get $srcCapW))
|
|
835
|
+
(i32.store (i32.add (local.get $dst) (i32.const 40)) (i32.load (i32.add (local.get $src) (i32.const 40))))
|
|
836
|
+
(i32.store (i32.add (local.get $dst) (i32.const 44)) (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
837
|
+
|
|
838
|
+
;; Empty src without double-freeing quota or external buffer
|
|
839
|
+
(i32.store (i32.add (local.get $src) (i32.const 32)) (i32.const 0))
|
|
840
|
+
(i32.store (i32.add (local.get $src) (i32.const 36)) (i32.const 0))
|
|
841
|
+
(i32.store (i32.add (local.get $src) (i32.const 40)) (i32.const 0))
|
|
842
|
+
(i32.store (i32.add (local.get $src) (i32.const 44)) (i32.const 0))
|
|
843
|
+
)
|
|
844
|
+
|
|
845
|
+
;; ============================================================================
|
|
846
|
+
;; 4. BitStack ADT (`<= 128b` inline + growable u32 LIFO stack)
|
|
847
|
+
;; ============================================================================
|
|
848
|
+
;; Layout (32 bytes): +0 bufPtr, +4 capWords, +8 lenBits, +12 released, +16..31 inline 4*u32
|
|
849
|
+
|
|
850
|
+
(func $bs_alloc (result i32)
|
|
851
|
+
(local $st i32)
|
|
852
|
+
(local.set $st (call $alloc (i32.const 32)))
|
|
853
|
+
(if (i32.eqz (local.get $st)) (then (return (i32.const 0))))
|
|
854
|
+
(i32.store (local.get $st) (i32.add (local.get $st) (i32.const 16)))
|
|
855
|
+
(i32.store (i32.add (local.get $st) (i32.const 4)) (i32.const 4))
|
|
856
|
+
(call $quota_add_bytes (i32.const 16))
|
|
857
|
+
(local.get $st)
|
|
858
|
+
)
|
|
859
|
+
|
|
860
|
+
(func $bs_release (param $st i32)
|
|
861
|
+
(local $capW i32)
|
|
862
|
+
(local $bufPtr i32)
|
|
863
|
+
(if (i32.eqz (local.get $st)) (then (return)))
|
|
864
|
+
(if (i32.ne (i32.load (i32.add (local.get $st) (i32.const 12))) (i32.const 0)) (then (return)))
|
|
865
|
+
(local.set $capW (i32.load (i32.add (local.get $st) (i32.const 4))))
|
|
866
|
+
(local.set $bufPtr (i32.load (local.get $st)))
|
|
867
|
+
(if (i32.gt_s (local.get $capW) (i32.const 0))
|
|
868
|
+
(then
|
|
869
|
+
(call $quota_add_bytes (i32.sub (i32.const 0) (i32.shl (local.get $capW) (i32.const 2))))
|
|
870
|
+
(if (i32.and (i32.ne (local.get $bufPtr) (i32.const 0))
|
|
871
|
+
(i32.ne (local.get $bufPtr) (i32.add (local.get $st) (i32.const 16))))
|
|
872
|
+
(then (call $free (local.get $bufPtr))))
|
|
873
|
+
)
|
|
874
|
+
)
|
|
875
|
+
(i32.store (local.get $st) (i32.const 0))
|
|
876
|
+
(i32.store (i32.add (local.get $st) (i32.const 12)) (i32.const 1))
|
|
877
|
+
(i32.store (i32.add (local.get $st) (i32.const 4)) (i32.const 0))
|
|
878
|
+
(i32.store (i32.add (local.get $st) (i32.const 8)) (i32.const 0))
|
|
879
|
+
)
|
|
880
|
+
|
|
881
|
+
(func $bs_ensure (param $st i32) (param $neededBits i32)
|
|
882
|
+
(local $oldW i32)
|
|
883
|
+
(local $newW i32)
|
|
884
|
+
(local $oldBuf i32)
|
|
885
|
+
(local $newBuf i32)
|
|
886
|
+
(local.set $oldW (i32.load (i32.add (local.get $st) (i32.const 4))))
|
|
887
|
+
(if (i32.le_s (local.get $neededBits) (i32.shl (local.get $oldW) (i32.const 5)))
|
|
888
|
+
(then (return)))
|
|
889
|
+
(local.set $newW (i32.shl (local.get $oldW) (i32.const 1)))
|
|
890
|
+
(block $brk
|
|
891
|
+
(loop $lp
|
|
892
|
+
(br_if $brk (i32.ge_s (i32.shl (local.get $newW) (i32.const 5)) (local.get $neededBits)))
|
|
893
|
+
(local.set $newW (i32.shl (local.get $newW) (i32.const 1)))
|
|
894
|
+
(br $lp)
|
|
895
|
+
)
|
|
896
|
+
)
|
|
897
|
+
(local.set $oldBuf (i32.load (local.get $st)))
|
|
898
|
+
(local.set $newBuf (call $alloc (i32.shl (local.get $newW) (i32.const 2))))
|
|
899
|
+
(if (i32.eqz (local.get $newBuf)) (then (return)))
|
|
900
|
+
(memory.copy (local.get $newBuf) (local.get $oldBuf) (i32.shl (local.get $oldW) (i32.const 2)))
|
|
901
|
+
(if (i32.and (i32.ne (local.get $oldBuf) (i32.const 0))
|
|
902
|
+
(i32.ne (local.get $oldBuf) (i32.add (local.get $st) (i32.const 16))))
|
|
903
|
+
(then (call $free (local.get $oldBuf))))
|
|
904
|
+
(call $quota_add_bytes (i32.shl (i32.sub (local.get $newW) (local.get $oldW)) (i32.const 2)))
|
|
905
|
+
(i32.store (local.get $st) (local.get $newBuf))
|
|
906
|
+
(i32.store (i32.add (local.get $st) (i32.const 4)) (local.get $newW))
|
|
907
|
+
)
|
|
908
|
+
|
|
909
|
+
(func $bs_push (param $st i32) (param $word i32) (param $k i32)
|
|
910
|
+
(local $len i32)
|
|
911
|
+
(local $buf i32)
|
|
912
|
+
(local $i i32)
|
|
913
|
+
(local $pos i32)
|
|
914
|
+
(local $addr i32)
|
|
915
|
+
(local $mask i32)
|
|
916
|
+
(if (i32.le_s (local.get $k) (i32.const 0)) (then (return)))
|
|
917
|
+
(local.set $len (i32.load (i32.add (local.get $st) (i32.const 8))))
|
|
918
|
+
(call $bs_ensure (local.get $st) (i32.add (local.get $len) (local.get $k)))
|
|
919
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return)))
|
|
920
|
+
(local.set $buf (i32.load (local.get $st)))
|
|
921
|
+
(local.set $i (i32.const 0))
|
|
922
|
+
(block $brk
|
|
923
|
+
(loop $lp
|
|
924
|
+
(br_if $brk (i32.ge_s (local.get $i) (local.get $k)))
|
|
925
|
+
(local.set $pos (i32.add (local.get $len) (local.get $i)))
|
|
926
|
+
(local.set $addr (i32.add (local.get $buf) (i32.shl (i32.shr_u (local.get $pos) (i32.const 5)) (i32.const 2))))
|
|
927
|
+
(local.set $mask (i32.shl (i32.const 1) (i32.and (local.get $pos) (i32.const 31))))
|
|
928
|
+
(if (i32.and (i32.shr_u (local.get $word) (local.get $i)) (i32.const 1))
|
|
929
|
+
(then (i32.store (local.get $addr) (i32.or (i32.load (local.get $addr)) (local.get $mask))))
|
|
930
|
+
(else (i32.store (local.get $addr) (i32.and (i32.load (local.get $addr)) (i32.xor (local.get $mask) (i32.const -1)))))
|
|
931
|
+
)
|
|
932
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
933
|
+
(br $lp)
|
|
934
|
+
)
|
|
935
|
+
)
|
|
936
|
+
(i32.store (i32.add (local.get $st) (i32.const 8)) (i32.add (local.get $len) (local.get $k)))
|
|
937
|
+
)
|
|
938
|
+
|
|
939
|
+
(func $bs_peek_top (param $st i32) (param $k i32) (result i32)
|
|
940
|
+
(local $len i32)
|
|
941
|
+
(local $buf i32)
|
|
942
|
+
(local $val i32)
|
|
943
|
+
(local $i i32)
|
|
944
|
+
(local $pos i32)
|
|
945
|
+
(local $bit i32)
|
|
946
|
+
(if (i32.le_s (local.get $k) (i32.const 0)) (then (return (i32.const 0))))
|
|
947
|
+
(local.set $len (i32.load (i32.add (local.get $st) (i32.const 8))))
|
|
948
|
+
(local.set $buf (i32.load (local.get $st)))
|
|
949
|
+
(local.set $val (i32.const 0))
|
|
950
|
+
(local.set $i (i32.const 0))
|
|
951
|
+
(block $brk
|
|
952
|
+
(loop $lp
|
|
953
|
+
(br_if $brk (i32.ge_s (local.get $i) (local.get $k)))
|
|
954
|
+
(local.set $pos (i32.sub (i32.sub (local.get $len) (i32.const 1)) (local.get $i)))
|
|
955
|
+
(local.set $bit
|
|
956
|
+
(i32.and
|
|
957
|
+
(i32.shr_u
|
|
958
|
+
(i32.load (i32.add (local.get $buf) (i32.shl (i32.shr_u (local.get $pos) (i32.const 5)) (i32.const 2))))
|
|
959
|
+
(i32.and (local.get $pos) (i32.const 31)))
|
|
960
|
+
(i32.const 1)))
|
|
961
|
+
(local.set $val (i32.or (i32.shl (local.get $val) (i32.const 1)) (local.get $bit)))
|
|
962
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
963
|
+
(br $lp)
|
|
964
|
+
)
|
|
965
|
+
)
|
|
966
|
+
(local.get $val)
|
|
967
|
+
)
|
|
968
|
+
|
|
969
|
+
(func $bs_pop (param $st i32) (param $k i32) (result i32)
|
|
970
|
+
(local $val i32)
|
|
971
|
+
(local.set $val (call $bs_peek_top (local.get $st) (local.get $k)))
|
|
972
|
+
(i32.store (i32.add (local.get $st) (i32.const 8))
|
|
973
|
+
(i32.sub (i32.load (i32.add (local.get $st) (i32.const 8))) (local.get $k)))
|
|
974
|
+
(local.get $val)
|
|
975
|
+
)
|
|
976
|
+
|
|
977
|
+
(func $bs_clear (param $st i32)
|
|
978
|
+
(i32.store (i32.add (local.get $st) (i32.const 8)) (i32.const 0))
|
|
979
|
+
)
|
|
980
|
+
|
|
981
|
+
;; ============================================================================
|
|
982
|
+
;; 5. Resumable 1-State DPDA Steppers (`skip`, `copy`, `coerce`)
|
|
983
|
+
;; ============================================================================
|
|
984
|
+
;; DPDA result: 0=DONE, 1=NEED_BITS, 2=FAILED
|
|
985
|
+
|
|
986
|
+
(func $dpda_alloc (param $kind i32) (param $srcSid i32) (param $dstSid i32) (result i32)
|
|
987
|
+
(local $d i32)
|
|
988
|
+
(local $stk i32)
|
|
989
|
+
(local $cap i32)
|
|
990
|
+
(local.set $cap (i32.const 32))
|
|
991
|
+
(local.set $d (call $alloc (i32.const 24)))
|
|
992
|
+
(if (i32.eqz (local.get $d)) (then (return (i32.const 0))))
|
|
993
|
+
(local.set $stk (call $alloc (select (i32.shl (local.get $cap) (i32.const 3)) (i32.shl (local.get $cap) (i32.const 2)) (i32.eq (local.get $kind) (i32.const 2)))))
|
|
994
|
+
(if (i32.eqz (local.get $stk)) (then (return (i32.const 0))))
|
|
995
|
+
(i32.store (local.get $d) (local.get $kind))
|
|
996
|
+
(i32.store (i32.add (local.get $d) (i32.const 4)) (local.get $stk))
|
|
997
|
+
(i32.store (i32.add (local.get $d) (i32.const 8)) (local.get $cap))
|
|
998
|
+
(i32.store (i32.add (local.get $d) (i32.const 12)) (i32.const 1))
|
|
999
|
+
(i32.store (local.get $stk) (local.get $srcSid))
|
|
1000
|
+
(if (i32.eq (local.get $kind) (i32.const 2))
|
|
1001
|
+
(then (i32.store (i32.add (local.get $stk) (i32.const 4)) (local.get $dstSid))))
|
|
1002
|
+
(local.get $d)
|
|
1003
|
+
)
|
|
1004
|
+
|
|
1005
|
+
(func $dpda_free (param $d i32)
|
|
1006
|
+
(if (i32.eqz (local.get $d)) (then (return)))
|
|
1007
|
+
(call $free (i32.load (i32.add (local.get $d) (i32.const 4))))
|
|
1008
|
+
(call $free (local.get $d))
|
|
1009
|
+
)
|
|
1010
|
+
|
|
1011
|
+
(func $dpda_push1 (param $d i32) (param $sid i32)
|
|
1012
|
+
(local $len i32)
|
|
1013
|
+
(local $cap i32)
|
|
1014
|
+
(local $stk i32)
|
|
1015
|
+
(local $newCap i32)
|
|
1016
|
+
(local $newStk i32)
|
|
1017
|
+
(local.set $len (i32.load (i32.add (local.get $d) (i32.const 12))))
|
|
1018
|
+
(local.set $cap (i32.load (i32.add (local.get $d) (i32.const 8))))
|
|
1019
|
+
(local.set $stk (i32.load (i32.add (local.get $d) (i32.const 4))))
|
|
1020
|
+
(if (i32.ge_s (local.get $len) (local.get $cap))
|
|
1021
|
+
(then
|
|
1022
|
+
(local.set $newCap (i32.shl (local.get $cap) (i32.const 1)))
|
|
1023
|
+
(local.set $newStk (call $alloc (i32.shl (local.get $newCap) (i32.const 2))))
|
|
1024
|
+
(if (i32.eqz (local.get $newStk)) (then (return)))
|
|
1025
|
+
(memory.copy (local.get $newStk) (local.get $stk) (i32.shl (local.get $len) (i32.const 2)))
|
|
1026
|
+
(call $free (local.get $stk))
|
|
1027
|
+
(local.set $stk (local.get $newStk))
|
|
1028
|
+
(i32.store (i32.add (local.get $d) (i32.const 4)) (local.get $stk))
|
|
1029
|
+
(i32.store (i32.add (local.get $d) (i32.const 8)) (local.get $newCap))
|
|
1030
|
+
)
|
|
1031
|
+
)
|
|
1032
|
+
(i32.store (i32.add (local.get $stk) (i32.shl (local.get $len) (i32.const 2))) (local.get $sid))
|
|
1033
|
+
(i32.store (i32.add (local.get $d) (i32.const 12)) (i32.add (local.get $len) (i32.const 1)))
|
|
1034
|
+
)
|
|
1035
|
+
|
|
1036
|
+
(func $dpda_push2 (param $d i32) (param $sSid i32) (param $dSid i32)
|
|
1037
|
+
(local $len i32)
|
|
1038
|
+
(local $cap i32)
|
|
1039
|
+
(local $stk i32)
|
|
1040
|
+
(local $newCap i32)
|
|
1041
|
+
(local $newStk i32)
|
|
1042
|
+
(local.set $len (i32.load (i32.add (local.get $d) (i32.const 12))))
|
|
1043
|
+
(local.set $cap (i32.load (i32.add (local.get $d) (i32.const 8))))
|
|
1044
|
+
(local.set $stk (i32.load (i32.add (local.get $d) (i32.const 4))))
|
|
1045
|
+
(if (i32.ge_s (local.get $len) (local.get $cap))
|
|
1046
|
+
(then
|
|
1047
|
+
(local.set $newCap (i32.shl (local.get $cap) (i32.const 1)))
|
|
1048
|
+
(local.set $newStk (call $alloc (i32.shl (local.get $newCap) (i32.const 3))))
|
|
1049
|
+
(if (i32.eqz (local.get $newStk)) (then (return)))
|
|
1050
|
+
(memory.copy (local.get $newStk) (local.get $stk) (i32.shl (local.get $len) (i32.const 3)))
|
|
1051
|
+
(call $free (local.get $stk))
|
|
1052
|
+
(local.set $stk (local.get $newStk))
|
|
1053
|
+
(i32.store (i32.add (local.get $d) (i32.const 4)) (local.get $stk))
|
|
1054
|
+
(i32.store (i32.add (local.get $d) (i32.const 8)) (local.get $newCap))
|
|
1055
|
+
)
|
|
1056
|
+
)
|
|
1057
|
+
(i32.store (i32.add (local.get $stk) (i32.shl (local.get $len) (i32.const 3))) (local.get $sSid))
|
|
1058
|
+
(i32.store (i32.add (i32.add (local.get $stk) (i32.shl (local.get $len) (i32.const 3))) (i32.const 4)) (local.get $dSid))
|
|
1059
|
+
(i32.store (i32.add (local.get $d) (i32.const 12)) (i32.add (local.get $len) (i32.const 1)))
|
|
1060
|
+
)
|
|
1061
|
+
|
|
1062
|
+
;; Step DPDA skip (dst = 0) or copy (dst != 0)
|
|
1063
|
+
(func $dpda_step_skip_copy (param $src i32) (param $dst i32) (param $d i32) (result i32)
|
|
1064
|
+
(local $len i32)
|
|
1065
|
+
(local $stk i32)
|
|
1066
|
+
(local $sid i32)
|
|
1067
|
+
(local $kind i32)
|
|
1068
|
+
(local $n i32)
|
|
1069
|
+
(local $w i32)
|
|
1070
|
+
(local $ord i32)
|
|
1071
|
+
(local $i i32)
|
|
1072
|
+
(block $brk
|
|
1073
|
+
(loop $lp
|
|
1074
|
+
(local.set $len (i32.load (i32.add (local.get $d) (i32.const 12))))
|
|
1075
|
+
(br_if $brk (i32.le_s (local.get $len) (i32.const 0)))
|
|
1076
|
+
(local.set $stk (i32.load (i32.add (local.get $d) (i32.const 4))))
|
|
1077
|
+
(local.set $sid (i32.load (i32.add (local.get $stk) (i32.shl (i32.sub (local.get $len) (i32.const 1)) (i32.const 2)))))
|
|
1078
|
+
(local.set $kind (call $schema_kind (local.get $sid)))
|
|
1079
|
+
(local.set $n (call $schema_num_edges (local.get $sid)))
|
|
1080
|
+
|
|
1081
|
+
(if (i32.eq (local.get $kind) (i32.const 0))
|
|
1082
|
+
(then
|
|
1083
|
+
;; closed-product: pop sid, push children in reverse order
|
|
1084
|
+
(i32.store (i32.add (local.get $d) (i32.const 12)) (i32.sub (local.get $len) (i32.const 1)))
|
|
1085
|
+
(local.set $i (i32.sub (local.get $n) (i32.const 1)))
|
|
1086
|
+
(block $bp
|
|
1087
|
+
(loop $lpp
|
|
1088
|
+
(br_if $bp (i32.lt_s (local.get $i) (i32.const 0)))
|
|
1089
|
+
(call $dpda_push1 (local.get $d) (call $schema_edge_target (local.get $sid) (local.get $i)))
|
|
1090
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (i32.const 2))))
|
|
1091
|
+
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
|
|
1092
|
+
(br $lpp)
|
|
1093
|
+
)
|
|
1094
|
+
)
|
|
1095
|
+
(br $lp)
|
|
1096
|
+
)
|
|
1097
|
+
)
|
|
1098
|
+
;; closed-union
|
|
1099
|
+
(if (i32.le_s (local.get $n) (i32.const 0)) (then (return (i32.const 2))))
|
|
1100
|
+
(local.set $w (call $schema_width (local.get $sid)))
|
|
1101
|
+
(local.set $ord (i32.const 0))
|
|
1102
|
+
(if (i32.gt_s (local.get $w) (i32.const 0))
|
|
1103
|
+
(then
|
|
1104
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $src) (i32.const 44))) (local.get $w))
|
|
1105
|
+
(then (return (i32.const 1)))) ;; NEED_BITS
|
|
1106
|
+
(local.set $ord (call $bd_peek_bits_at (local.get $src) (i32.const 0) (local.get $w)))
|
|
1107
|
+
(if (i32.ge_u (local.get $ord) (local.get $n)) (then (return (i32.const 2)))) ;; FAILED
|
|
1108
|
+
(drop (call $bd_shift_bits (local.get $src) (local.get $w)))
|
|
1109
|
+
(if (i32.ne (local.get $dst) (i32.const 0))
|
|
1110
|
+
(then
|
|
1111
|
+
(call $bd_push_bits (local.get $dst) (local.get $ord) (local.get $w))
|
|
1112
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (i32.const 2))))))
|
|
1113
|
+
)
|
|
1114
|
+
)
|
|
1115
|
+
;; Replace top of stack with target node
|
|
1116
|
+
(i32.store
|
|
1117
|
+
(i32.add (local.get $stk) (i32.shl (i32.sub (local.get $len) (i32.const 1)) (i32.const 2)))
|
|
1118
|
+
(call $schema_edge_target (local.get $sid) (local.get $ord)))
|
|
1119
|
+
(br $lp)
|
|
1120
|
+
)
|
|
1121
|
+
)
|
|
1122
|
+
(i32.const 0) ;; DONE
|
|
1123
|
+
)
|
|
1124
|
+
|
|
1125
|
+
(func $dpda_step_coerce (param $src i32) (param $dst i32) (param $d i32) (result i32)
|
|
1126
|
+
(local $len i32)
|
|
1127
|
+
(local $stk i32)
|
|
1128
|
+
(local $sSid i32)
|
|
1129
|
+
(local $dSid i32)
|
|
1130
|
+
(local $sKind i32)
|
|
1131
|
+
(local $dKind i32)
|
|
1132
|
+
(local $sN i32)
|
|
1133
|
+
(local $dN i32)
|
|
1134
|
+
(local $sW i32)
|
|
1135
|
+
(local $dW i32)
|
|
1136
|
+
(local $sOrd i32)
|
|
1137
|
+
(local $dOrd i32)
|
|
1138
|
+
(local $tagId i32)
|
|
1139
|
+
(local $i i32)
|
|
1140
|
+
(local $entryAddr i32)
|
|
1141
|
+
|
|
1142
|
+
(block $brk
|
|
1143
|
+
(loop $lp
|
|
1144
|
+
(local.set $len (i32.load (i32.add (local.get $d) (i32.const 12))))
|
|
1145
|
+
(br_if $brk (i32.le_s (local.get $len) (i32.const 0)))
|
|
1146
|
+
(local.set $stk (i32.load (i32.add (local.get $d) (i32.const 4))))
|
|
1147
|
+
(local.set $entryAddr (i32.add (local.get $stk) (i32.shl (i32.sub (local.get $len) (i32.const 1)) (i32.const 3))))
|
|
1148
|
+
(local.set $sSid (i32.load (local.get $entryAddr)))
|
|
1149
|
+
(local.set $dSid (i32.load (i32.add (local.get $entryAddr) (i32.const 4))))
|
|
1150
|
+
(local.set $sKind (call $schema_kind (local.get $sSid)))
|
|
1151
|
+
(local.set $dKind (call $schema_kind (local.get $dSid)))
|
|
1152
|
+
(if (i32.ne (local.get $sKind) (local.get $dKind)) (then (return (i32.const 2))))
|
|
1153
|
+
|
|
1154
|
+
(local.set $sN (call $schema_num_edges (local.get $sSid)))
|
|
1155
|
+
(local.set $dN (call $schema_num_edges (local.get $dSid)))
|
|
1156
|
+
|
|
1157
|
+
(if (i32.eq (local.get $sKind) (i32.const 0))
|
|
1158
|
+
(then
|
|
1159
|
+
(if (i32.ne (local.get $sN) (local.get $dN)) (then (return (i32.const 2))))
|
|
1160
|
+
(i32.store (i32.add (local.get $d) (i32.const 12)) (i32.sub (local.get $len) (i32.const 1)))
|
|
1161
|
+
(local.set $i (i32.sub (local.get $sN) (i32.const 1)))
|
|
1162
|
+
(block $bp
|
|
1163
|
+
(loop $lpp
|
|
1164
|
+
(br_if $bp (i32.lt_s (local.get $i) (i32.const 0)))
|
|
1165
|
+
(call $dpda_push2 (local.get $d)
|
|
1166
|
+
(call $schema_edge_target (local.get $sSid) (local.get $i))
|
|
1167
|
+
(call $schema_edge_target (local.get $dSid) (local.get $i)))
|
|
1168
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (i32.const 2))))
|
|
1169
|
+
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
|
|
1170
|
+
(br $lpp)
|
|
1171
|
+
)
|
|
1172
|
+
)
|
|
1173
|
+
(br $lp)
|
|
1174
|
+
)
|
|
1175
|
+
)
|
|
1176
|
+
|
|
1177
|
+
;; closed-union coercion by tagId
|
|
1178
|
+
(if (i32.or (i32.le_s (local.get $sN) (i32.const 0)) (i32.le_s (local.get $dN) (i32.const 0)))
|
|
1179
|
+
(then (return (i32.const 2))))
|
|
1180
|
+
(local.set $sW (call $schema_width (local.get $sSid)))
|
|
1181
|
+
(local.set $sOrd (i32.const 0))
|
|
1182
|
+
(if (i32.gt_s (local.get $sW) (i32.const 0))
|
|
1183
|
+
(then
|
|
1184
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $src) (i32.const 44))) (local.get $sW))
|
|
1185
|
+
(then (return (i32.const 1))))
|
|
1186
|
+
(local.set $sOrd (call $bd_peek_bits_at (local.get $src) (i32.const 0) (local.get $sW)))
|
|
1187
|
+
(if (i32.ge_u (local.get $sOrd) (local.get $sN)) (then (return (i32.const 2))))
|
|
1188
|
+
)
|
|
1189
|
+
)
|
|
1190
|
+
(local.set $tagId (call $schema_edge_tag (local.get $sSid) (local.get $sOrd)))
|
|
1191
|
+
(local.set $dOrd (call $schema_find_tag (local.get $dSid) (local.get $tagId)))
|
|
1192
|
+
(if (i32.lt_s (local.get $dOrd) (i32.const 0)) (then (return (i32.const 2))))
|
|
1193
|
+
(if (i32.gt_s (local.get $sW) (i32.const 0))
|
|
1194
|
+
(then (drop (call $bd_shift_bits (local.get $src) (local.get $sW)))))
|
|
1195
|
+
(local.set $dW (call $schema_width (local.get $dSid)))
|
|
1196
|
+
(if (i32.gt_s (local.get $dW) (i32.const 0))
|
|
1197
|
+
(then
|
|
1198
|
+
(call $bd_push_bits (local.get $dst) (local.get $dOrd) (local.get $dW))
|
|
1199
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (i32.const 2))))))
|
|
1200
|
+
(i32.store (local.get $entryAddr) (call $schema_edge_target (local.get $sSid) (local.get $sOrd)))
|
|
1201
|
+
(i32.store (i32.add (local.get $entryAddr) (i32.const 4)) (call $schema_edge_target (local.get $dSid) (local.get $dOrd)))
|
|
1202
|
+
(br $lp)
|
|
1203
|
+
)
|
|
1204
|
+
)
|
|
1205
|
+
(i32.const 0)
|
|
1206
|
+
)
|
|
1207
|
+
|
|
1208
|
+
;; ============================================================================
|
|
1209
|
+
;; 6. Scopes, Nodes, Barriers, Tokens & O(1) Channel Splice
|
|
1210
|
+
;; ============================================================================
|
|
1211
|
+
|
|
1212
|
+
;; Scope layout (28 bytes):
|
|
1213
|
+
;; +0 parent, +4 firstChild, +8 nextSibling, +12 firstNode, +16 firstChan, +20 destroyed, +24 prevSibling
|
|
1214
|
+
(func $scope_create (param $parentPtr i32) (result i32)
|
|
1215
|
+
(local $sc i32)
|
|
1216
|
+
(local $oldFirst i32)
|
|
1217
|
+
(local.set $sc (call $alloc (i32.const 28)))
|
|
1218
|
+
(if (i32.eqz (local.get $sc)) (then (return (i32.const 0))))
|
|
1219
|
+
(i32.store (local.get $sc) (local.get $parentPtr))
|
|
1220
|
+
(if (i32.ne (local.get $parentPtr) (i32.const 0))
|
|
1221
|
+
(then
|
|
1222
|
+
(local.set $oldFirst (i32.load (i32.add (local.get $parentPtr) (i32.const 4))))
|
|
1223
|
+
(i32.store (i32.add (local.get $sc) (i32.const 8)) (local.get $oldFirst))
|
|
1224
|
+
(if (i32.ne (local.get $oldFirst) (i32.const 0))
|
|
1225
|
+
(then (i32.store (i32.add (local.get $oldFirst) (i32.const 24)) (local.get $sc))))
|
|
1226
|
+
(i32.store (i32.add (local.get $parentPtr) (i32.const 4)) (local.get $sc))
|
|
1227
|
+
)
|
|
1228
|
+
)
|
|
1229
|
+
(local.get $sc)
|
|
1230
|
+
)
|
|
1231
|
+
|
|
1232
|
+
(func $scope_unlink_child (param $sc i32)
|
|
1233
|
+
(local $parent i32)
|
|
1234
|
+
(local $prev i32)
|
|
1235
|
+
(local $next i32)
|
|
1236
|
+
(local.set $parent (i32.load (local.get $sc)))
|
|
1237
|
+
(if (i32.eqz (local.get $parent)) (then (return)))
|
|
1238
|
+
(local.set $prev (i32.load (i32.add (local.get $sc) (i32.const 24))))
|
|
1239
|
+
(local.set $next (i32.load (i32.add (local.get $sc) (i32.const 8))))
|
|
1240
|
+
(if (i32.ne (local.get $prev) (i32.const 0))
|
|
1241
|
+
(then (i32.store (i32.add (local.get $prev) (i32.const 8)) (local.get $next)))
|
|
1242
|
+
(else
|
|
1243
|
+
(if (i32.eq (i32.load (i32.add (local.get $parent) (i32.const 4))) (local.get $sc))
|
|
1244
|
+
(then (i32.store (i32.add (local.get $parent) (i32.const 4)) (local.get $next))))))
|
|
1245
|
+
(if (i32.ne (local.get $next) (i32.const 0))
|
|
1246
|
+
(then (i32.store (i32.add (local.get $next) (i32.const 24)) (local.get $prev))))
|
|
1247
|
+
(i32.store (local.get $sc) (i32.const 0))
|
|
1248
|
+
(i32.store (i32.add (local.get $sc) (i32.const 8)) (i32.const 0))
|
|
1249
|
+
(i32.store (i32.add (local.get $sc) (i32.const 24)) (i32.const 0))
|
|
1250
|
+
)
|
|
1251
|
+
|
|
1252
|
+
(func $barrier_alloc (result i32)
|
|
1253
|
+
(call $alloc (i32.const 16))
|
|
1254
|
+
)
|
|
1255
|
+
|
|
1256
|
+
(func $token_alloc (param $outPtr i32) (param $outCount i32) (result i32)
|
|
1257
|
+
(local $tok i32)
|
|
1258
|
+
(local $copy i32)
|
|
1259
|
+
(local $i i32)
|
|
1260
|
+
(local.set $tok (call $alloc (i32.const 16)))
|
|
1261
|
+
(if (i32.eqz (local.get $tok)) (then (return (i32.const 0))))
|
|
1262
|
+
(if (i32.gt_s (local.get $outCount) (i32.const 0))
|
|
1263
|
+
(then
|
|
1264
|
+
(local.set $copy (call $alloc (i32.shl (local.get $outCount) (i32.const 2))))
|
|
1265
|
+
(if (i32.eqz (local.get $copy)) (then (return (i32.const 0))))
|
|
1266
|
+
(memory.copy (local.get $copy) (local.get $outPtr) (i32.shl (local.get $outCount) (i32.const 2)))
|
|
1267
|
+
(local.set $i (i32.const 0))
|
|
1268
|
+
(block $bp
|
|
1269
|
+
(loop $lp
|
|
1270
|
+
(br_if $bp (i32.ge_s (local.get $i) (local.get $outCount)))
|
|
1271
|
+
(call $chan_pin (i32.load (i32.add (local.get $copy) (i32.shl (local.get $i) (i32.const 2)))))
|
|
1272
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1273
|
+
(br $lp)
|
|
1274
|
+
)
|
|
1275
|
+
)
|
|
1276
|
+
)
|
|
1277
|
+
)
|
|
1278
|
+
(i32.store (i32.add (local.get $tok) (i32.const 4)) (local.get $copy))
|
|
1279
|
+
(i32.store (i32.add (local.get $tok) (i32.const 8)) (local.get $outCount))
|
|
1280
|
+
(local.get $tok)
|
|
1281
|
+
)
|
|
1282
|
+
|
|
1283
|
+
;; Node layout (64 bytes):
|
|
1284
|
+
;; +0 kind, +4 retired, +8 scopePtr, +12 nextInScope, +16 inPtr, +20 inCount,
|
|
1285
|
+
;; +24 outPtr, +28 outCount, +32..+48 node-specific, +52 prevDrain, +56 prevInScope, +60 dstackRefs
|
|
1286
|
+
(func $node_alloc (param $scopePtr i32) (param $kind i32) (param $inCount i32) (param $outCount i32) (result i32)
|
|
1287
|
+
(local $n i32)
|
|
1288
|
+
(local $inPtr i32)
|
|
1289
|
+
(local $outPtr i32)
|
|
1290
|
+
(local $oldFirst i32)
|
|
1291
|
+
(call $quota_add_node)
|
|
1292
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (i32.const 0))))
|
|
1293
|
+
(local.set $n (call $alloc (i32.const 64)))
|
|
1294
|
+
(if (i32.eqz (local.get $n)) (then (return (i32.const 0))))
|
|
1295
|
+
(i32.store (local.get $n) (local.get $kind))
|
|
1296
|
+
(i32.store (i32.add (local.get $n) (i32.const 8)) (local.get $scopePtr))
|
|
1297
|
+
(if (i32.gt_s (local.get $inCount) (i32.const 0))
|
|
1298
|
+
(then
|
|
1299
|
+
(local.set $inPtr (call $alloc (i32.shl (local.get $inCount) (i32.const 2))))
|
|
1300
|
+
(if (i32.eqz (local.get $inPtr)) (then (return (i32.const 0))))
|
|
1301
|
+
(i32.store (i32.add (local.get $n) (i32.const 16)) (local.get $inPtr))
|
|
1302
|
+
(i32.store (i32.add (local.get $n) (i32.const 20)) (local.get $inCount))
|
|
1303
|
+
)
|
|
1304
|
+
)
|
|
1305
|
+
(if (i32.gt_s (local.get $outCount) (i32.const 0))
|
|
1306
|
+
(then
|
|
1307
|
+
(local.set $outPtr (call $alloc (i32.shl (local.get $outCount) (i32.const 2))))
|
|
1308
|
+
(if (i32.eqz (local.get $outPtr)) (then (return (i32.const 0))))
|
|
1309
|
+
(i32.store (i32.add (local.get $n) (i32.const 24)) (local.get $outPtr))
|
|
1310
|
+
(i32.store (i32.add (local.get $n) (i32.const 28)) (local.get $outCount))
|
|
1311
|
+
)
|
|
1312
|
+
)
|
|
1313
|
+
(if (i32.ne (local.get $scopePtr) (i32.const 0))
|
|
1314
|
+
(then
|
|
1315
|
+
(local.set $oldFirst (i32.load (i32.add (local.get $scopePtr) (i32.const 12))))
|
|
1316
|
+
(i32.store (i32.add (local.get $n) (i32.const 12)) (local.get $oldFirst))
|
|
1317
|
+
(if (i32.ne (local.get $oldFirst) (i32.const 0))
|
|
1318
|
+
(then (i32.store (i32.add (local.get $oldFirst) (i32.const 56)) (local.get $n))))
|
|
1319
|
+
(i32.store (i32.add (local.get $scopePtr) (i32.const 12)) (local.get $n))
|
|
1320
|
+
)
|
|
1321
|
+
)
|
|
1322
|
+
(local.get $n)
|
|
1323
|
+
)
|
|
1324
|
+
|
|
1325
|
+
(func $node_set_in (param $n i32) (param $idx i32) (param $ch i32)
|
|
1326
|
+
(local $slot i32)
|
|
1327
|
+
(local $old i32)
|
|
1328
|
+
(local.set $slot (i32.add (i32.load (i32.add (local.get $n) (i32.const 16))) (i32.shl (local.get $idx) (i32.const 2))))
|
|
1329
|
+
(local.set $old (i32.load (local.get $slot)))
|
|
1330
|
+
(if (i32.ne (local.get $old) (i32.const 0)) (then (call $chan_unpin (local.get $old))))
|
|
1331
|
+
(i32.store (local.get $slot) (local.get $ch))
|
|
1332
|
+
(if (i32.ne (local.get $ch) (i32.const 0))
|
|
1333
|
+
(then
|
|
1334
|
+
(call $chan_pin (local.get $ch))
|
|
1335
|
+
(i32.store (i32.add (local.get $ch) (i32.const 20)) (local.get $n))))
|
|
1336
|
+
)
|
|
1337
|
+
|
|
1338
|
+
(func $node_set_out (param $n i32) (param $idx i32) (param $ch i32)
|
|
1339
|
+
(local $slot i32)
|
|
1340
|
+
(local $old i32)
|
|
1341
|
+
(local.set $slot (i32.add (i32.load (i32.add (local.get $n) (i32.const 24))) (i32.shl (local.get $idx) (i32.const 2))))
|
|
1342
|
+
(local.set $old (i32.load (local.get $slot)))
|
|
1343
|
+
(if (i32.ne (local.get $old) (i32.const 0)) (then (call $chan_unpin (local.get $old))))
|
|
1344
|
+
(i32.store (local.get $slot) (local.get $ch))
|
|
1345
|
+
(if (i32.ne (local.get $ch) (i32.const 0))
|
|
1346
|
+
(then
|
|
1347
|
+
(call $chan_pin (local.get $ch))
|
|
1348
|
+
(i32.store (i32.add (local.get $ch) (i32.const 16)) (local.get $n))))
|
|
1349
|
+
)
|
|
1350
|
+
|
|
1351
|
+
(func $node_get_in (param $n i32) (param $idx i32) (result i32)
|
|
1352
|
+
(i32.load (i32.add (i32.load (i32.add (local.get $n) (i32.const 16))) (i32.shl (local.get $idx) (i32.const 2))))
|
|
1353
|
+
)
|
|
1354
|
+
|
|
1355
|
+
(func $node_get_out (param $n i32) (param $idx i32) (result i32)
|
|
1356
|
+
(i32.load (i32.add (i32.load (i32.add (local.get $n) (i32.const 24))) (i32.shl (local.get $idx) (i32.const 2))))
|
|
1357
|
+
)
|
|
1358
|
+
|
|
1359
|
+
(func $scope_unlink_node (param $n i32)
|
|
1360
|
+
(local $sc i32)
|
|
1361
|
+
(local $prev i32)
|
|
1362
|
+
(local $next i32)
|
|
1363
|
+
(local.set $sc (i32.load (i32.add (local.get $n) (i32.const 8))))
|
|
1364
|
+
(if (i32.eqz (local.get $sc)) (then (return)))
|
|
1365
|
+
(local.set $prev (i32.load (i32.add (local.get $n) (i32.const 56))))
|
|
1366
|
+
(local.set $next (i32.load (i32.add (local.get $n) (i32.const 12))))
|
|
1367
|
+
(if (i32.ne (local.get $prev) (i32.const 0))
|
|
1368
|
+
(then (i32.store (i32.add (local.get $prev) (i32.const 12)) (local.get $next)))
|
|
1369
|
+
(else
|
|
1370
|
+
(if (i32.eq (i32.load (i32.add (local.get $sc) (i32.const 12))) (local.get $n))
|
|
1371
|
+
(then (i32.store (i32.add (local.get $sc) (i32.const 12)) (local.get $next))))))
|
|
1372
|
+
(if (i32.ne (local.get $next) (i32.const 0))
|
|
1373
|
+
(then (i32.store (i32.add (local.get $next) (i32.const 56)) (local.get $prev))))
|
|
1374
|
+
(i32.store (i32.add (local.get $n) (i32.const 8)) (i32.const 0))
|
|
1375
|
+
(i32.store (i32.add (local.get $n) (i32.const 12)) (i32.const 0))
|
|
1376
|
+
(i32.store (i32.add (local.get $n) (i32.const 56)) (i32.const 0))
|
|
1377
|
+
)
|
|
1378
|
+
|
|
1379
|
+
(func $bar_unlink_drain (param $n i32)
|
|
1380
|
+
(local $bar i32)
|
|
1381
|
+
(local $prev i32)
|
|
1382
|
+
(local $next i32)
|
|
1383
|
+
(local.set $bar (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
1384
|
+
(if (i32.eqz (local.get $bar)) (then (return)))
|
|
1385
|
+
(local.set $prev (i32.load (i32.add (local.get $n) (i32.const 52))))
|
|
1386
|
+
(local.set $next (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
1387
|
+
(if (i32.ne (local.get $prev) (i32.const 0))
|
|
1388
|
+
(then (i32.store (i32.add (local.get $prev) (i32.const 40)) (local.get $next)))
|
|
1389
|
+
(else
|
|
1390
|
+
(if (i32.eq (i32.load (i32.add (local.get $bar) (i32.const 8))) (local.get $n))
|
|
1391
|
+
(then (i32.store (i32.add (local.get $bar) (i32.const 8)) (local.get $next))))))
|
|
1392
|
+
(if (i32.ne (local.get $next) (i32.const 0))
|
|
1393
|
+
(then (i32.store (i32.add (local.get $next) (i32.const 52)) (local.get $prev))))
|
|
1394
|
+
(i32.store (i32.add (local.get $n) (i32.const 36)) (i32.const 0))
|
|
1395
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
1396
|
+
(i32.store (i32.add (local.get $n) (i32.const 52)) (i32.const 0))
|
|
1397
|
+
)
|
|
1398
|
+
|
|
1399
|
+
(func $try_queue_node (param $n i32)
|
|
1400
|
+
(if (i32.and
|
|
1401
|
+
(i32.eq (i32.load (i32.add (local.get $n) (i32.const 4))) (i32.const 1))
|
|
1402
|
+
(i32.le_s (i32.load (i32.add (local.get $n) (i32.const 60))) (i32.const 0)))
|
|
1403
|
+
(then
|
|
1404
|
+
(i32.store (i32.add (local.get $n) (i32.const 4)) (i32.const 2)) ;; QUEUED
|
|
1405
|
+
(i32.store (i32.add (local.get $n) (i32.const 12)) (global.get $g_dead_nodes))
|
|
1406
|
+
(global.set $g_dead_nodes (local.get $n))
|
|
1407
|
+
)
|
|
1408
|
+
)
|
|
1409
|
+
)
|
|
1410
|
+
|
|
1411
|
+
(func $node_unpin (param $n i32)
|
|
1412
|
+
(local $cnt i32)
|
|
1413
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
1414
|
+
(local.set $cnt (i32.sub (i32.load (i32.add (local.get $n) (i32.const 60))) (i32.const 1)))
|
|
1415
|
+
(i32.store (i32.add (local.get $n) (i32.const 60)) (local.get $cnt))
|
|
1416
|
+
(if (i32.and (i32.le_s (local.get $cnt) (i32.const 0)) (i32.eq (i32.load (i32.add (local.get $n) (i32.const 4))) (i32.const 1)))
|
|
1417
|
+
(then (call $try_queue_node (local.get $n))))
|
|
1418
|
+
)
|
|
1419
|
+
|
|
1420
|
+
(func $spec_free_active (param $ac i32)
|
|
1421
|
+
(local $taps i32)
|
|
1422
|
+
(local $tapCnt i32)
|
|
1423
|
+
(local $staged i32)
|
|
1424
|
+
(local $stagedCnt i32)
|
|
1425
|
+
(local $i i32)
|
|
1426
|
+
(local $te i32)
|
|
1427
|
+
(local $se i32)
|
|
1428
|
+
(if (i32.eqz (local.get $ac)) (then (return)))
|
|
1429
|
+
(local.set $taps (i32.load (i32.add (local.get $ac) (i32.const 4))))
|
|
1430
|
+
(local.set $tapCnt (i32.load (i32.add (local.get $ac) (i32.const 8))))
|
|
1431
|
+
(local.set $i (i32.const 0))
|
|
1432
|
+
(block $bt
|
|
1433
|
+
(loop $lt
|
|
1434
|
+
(br_if $bt (i32.ge_s (local.get $i) (local.get $tapCnt)))
|
|
1435
|
+
(local.set $te (i32.add (local.get $taps) (i32.shl (local.get $i) (i32.const 4))))
|
|
1436
|
+
(call $chan_unpin (i32.load (local.get $te)))
|
|
1437
|
+
(call $chan_unpin (i32.load (i32.add (local.get $te) (i32.const 4))))
|
|
1438
|
+
(call $chan_unpin (i32.load (i32.add (local.get $te) (i32.const 8))))
|
|
1439
|
+
;; Unpin tapNode (+60 pinCount)
|
|
1440
|
+
(call $node_unpin (i32.load (i32.add (local.get $te) (i32.const 12))))
|
|
1441
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1442
|
+
(br $lt)
|
|
1443
|
+
)
|
|
1444
|
+
)
|
|
1445
|
+
(local.set $staged (i32.load (i32.add (local.get $ac) (i32.const 12))))
|
|
1446
|
+
(local.set $stagedCnt (i32.load (i32.add (local.get $ac) (i32.const 16))))
|
|
1447
|
+
(local.set $i (i32.const 0))
|
|
1448
|
+
(block $bs
|
|
1449
|
+
(loop $ls
|
|
1450
|
+
(br_if $bs (i32.ge_s (local.get $i) (local.get $stagedCnt)))
|
|
1451
|
+
(local.set $se (i32.add (local.get $staged) (i32.shl (local.get $i) (i32.const 3))))
|
|
1452
|
+
(call $chan_unpin (i32.load (local.get $se)))
|
|
1453
|
+
(call $chan_unpin (i32.load (i32.add (local.get $se) (i32.const 4))))
|
|
1454
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1455
|
+
(br $ls)
|
|
1456
|
+
)
|
|
1457
|
+
)
|
|
1458
|
+
(call $free (local.get $taps))
|
|
1459
|
+
(call $free (local.get $staged))
|
|
1460
|
+
(call $free (local.get $ac))
|
|
1461
|
+
)
|
|
1462
|
+
|
|
1463
|
+
(func $free_node_mem (param $n i32)
|
|
1464
|
+
(local $kind i32)
|
|
1465
|
+
(local $cnt i32)
|
|
1466
|
+
(local $arr i32)
|
|
1467
|
+
(local $i i32)
|
|
1468
|
+
(local $ks i32)
|
|
1469
|
+
(local $sArr i32)
|
|
1470
|
+
(local $sCnt i32)
|
|
1471
|
+
|
|
1472
|
+
;; Unpin and free input array
|
|
1473
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
1474
|
+
(local.set $arr (i32.load (i32.add (local.get $n) (i32.const 16))))
|
|
1475
|
+
(if (i32.ne (local.get $arr) (i32.const 0))
|
|
1476
|
+
(then
|
|
1477
|
+
(local.set $i (i32.const 0))
|
|
1478
|
+
(block $bi
|
|
1479
|
+
(loop $li
|
|
1480
|
+
(br_if $bi (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1481
|
+
(call $chan_unpin (i32.load (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2)))))
|
|
1482
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1483
|
+
(br $li)
|
|
1484
|
+
)
|
|
1485
|
+
)
|
|
1486
|
+
(call $free (local.get $arr))
|
|
1487
|
+
)
|
|
1488
|
+
)
|
|
1489
|
+
|
|
1490
|
+
;; Unpin and free output array
|
|
1491
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
1492
|
+
(local.set $arr (i32.load (i32.add (local.get $n) (i32.const 24))))
|
|
1493
|
+
(if (i32.ne (local.get $arr) (i32.const 0))
|
|
1494
|
+
(then
|
|
1495
|
+
(local.set $i (i32.const 0))
|
|
1496
|
+
(block $bo
|
|
1497
|
+
(loop $lo
|
|
1498
|
+
(br_if $bo (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1499
|
+
(call $chan_unpin (i32.load (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2)))))
|
|
1500
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1501
|
+
(br $lo)
|
|
1502
|
+
)
|
|
1503
|
+
)
|
|
1504
|
+
(call $free (local.get $arr))
|
|
1505
|
+
)
|
|
1506
|
+
)
|
|
1507
|
+
|
|
1508
|
+
;; Free kind-specific owned allocations
|
|
1509
|
+
(local.set $kind (i32.load (local.get $n)))
|
|
1510
|
+
(if (i32.or (i32.eq (local.get $kind) (i32.const 3)) (i32.eq (local.get $kind) (i32.const 6)))
|
|
1511
|
+
(then (call $dpda_free (i32.load (i32.add (local.get $n) (i32.const 40))))))
|
|
1512
|
+
(if (i32.eq (local.get $kind) (i32.const 9))
|
|
1513
|
+
(then (call $dpda_free (i32.load (i32.add (local.get $n) (i32.const 44))))))
|
|
1514
|
+
(if (i32.eq (local.get $kind) (i32.const 11))
|
|
1515
|
+
(then
|
|
1516
|
+
(call $free (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
1517
|
+
(call $free (i32.load (i32.add (local.get $n) (i32.const 44))))
|
|
1518
|
+
)
|
|
1519
|
+
)
|
|
1520
|
+
(if (i32.eq (local.get $kind) (i32.const 12))
|
|
1521
|
+
(then
|
|
1522
|
+
(call $spec_free_active (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
1523
|
+
)
|
|
1524
|
+
)
|
|
1525
|
+
(if (i32.eq (local.get $kind) (i32.const 15))
|
|
1526
|
+
(then
|
|
1527
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
1528
|
+
(if (i32.ne (local.get $ks) (i32.const 0))
|
|
1529
|
+
(then
|
|
1530
|
+
(call $free (i32.load (local.get $ks)))
|
|
1531
|
+
(local.set $sArr (i32.load (i32.add (local.get $ks) (i32.const 4))))
|
|
1532
|
+
(local.set $sCnt (i32.load (i32.add (local.get $ks) (i32.const 8))))
|
|
1533
|
+
(if (i32.ne (local.get $sArr) (i32.const 0))
|
|
1534
|
+
(then
|
|
1535
|
+
(local.set $i (i32.const 0))
|
|
1536
|
+
(block $bks
|
|
1537
|
+
(loop $lks
|
|
1538
|
+
(br_if $bks (i32.ge_s (local.get $i) (local.get $sCnt)))
|
|
1539
|
+
(call $free (i32.load (i32.add (local.get $sArr) (i32.shl (local.get $i) (i32.const 2)))))
|
|
1540
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1541
|
+
(br $lks)
|
|
1542
|
+
)
|
|
1543
|
+
)
|
|
1544
|
+
(call $free (local.get $sArr))
|
|
1545
|
+
)
|
|
1546
|
+
)
|
|
1547
|
+
(call $free (i32.load (i32.add (local.get $ks) (i32.const 12))))
|
|
1548
|
+
(call $free (i32.load (i32.add (local.get $ks) (i32.const 20))))
|
|
1549
|
+
(call $dpda_free (i32.load (i32.add (local.get $ks) (i32.const 24))))
|
|
1550
|
+
(call $free (local.get $ks))
|
|
1551
|
+
)
|
|
1552
|
+
)
|
|
1553
|
+
)
|
|
1554
|
+
)
|
|
1555
|
+
)
|
|
1556
|
+
|
|
1557
|
+
;; Strictly O(1)-amortized safepoint reclamation: every item on a dead list
|
|
1558
|
+
;; is already guaranteed unpinned & detached (zero re-scanning!).
|
|
1559
|
+
(func $flush_dead
|
|
1560
|
+
(local $cur i32)
|
|
1561
|
+
(local $next i32)
|
|
1562
|
+
|
|
1563
|
+
;; 1. Reclaim queued retired nodes (may unpin & queue newly freed channels!)
|
|
1564
|
+
(local.set $cur (global.get $g_dead_nodes))
|
|
1565
|
+
(global.set $g_dead_nodes (i32.const 0))
|
|
1566
|
+
(block $bn
|
|
1567
|
+
(loop $ln
|
|
1568
|
+
(br_if $bn (i32.eqz (local.get $cur)))
|
|
1569
|
+
(local.set $next (i32.load (i32.add (local.get $cur) (i32.const 12))))
|
|
1570
|
+
(call $free_node_mem (local.get $cur))
|
|
1571
|
+
(call $free (local.get $cur))
|
|
1572
|
+
(local.set $cur (local.get $next))
|
|
1573
|
+
(br $ln)
|
|
1574
|
+
)
|
|
1575
|
+
)
|
|
1576
|
+
|
|
1577
|
+
;; 2. Reclaim queued released channels
|
|
1578
|
+
(local.set $cur (global.get $g_dead_chans))
|
|
1579
|
+
(global.set $g_dead_chans (i32.const 0))
|
|
1580
|
+
(block $bc
|
|
1581
|
+
(loop $lc
|
|
1582
|
+
(br_if $bc (i32.eqz (local.get $cur)))
|
|
1583
|
+
(local.set $next (i32.load (i32.add (local.get $cur) (i32.const 28))))
|
|
1584
|
+
(call $free (local.get $cur))
|
|
1585
|
+
(local.set $cur (local.get $next))
|
|
1586
|
+
(br $lc)
|
|
1587
|
+
)
|
|
1588
|
+
)
|
|
1589
|
+
|
|
1590
|
+
;; 3. Reclaim destroyed child scopes
|
|
1591
|
+
(local.set $cur (global.get $g_dead_scopes))
|
|
1592
|
+
(global.set $g_dead_scopes (i32.const 0))
|
|
1593
|
+
(block $bs
|
|
1594
|
+
(loop $ls
|
|
1595
|
+
(br_if $bs (i32.eqz (local.get $cur)))
|
|
1596
|
+
(local.set $next (i32.load (i32.add (local.get $cur) (i32.const 8))))
|
|
1597
|
+
(call $free (local.get $cur))
|
|
1598
|
+
(local.set $cur (local.get $next))
|
|
1599
|
+
(br $ls)
|
|
1600
|
+
)
|
|
1601
|
+
)
|
|
1602
|
+
)
|
|
1603
|
+
|
|
1604
|
+
(func $retire_node (param $n i32)
|
|
1605
|
+
(local $i i32)
|
|
1606
|
+
(local $cnt i32)
|
|
1607
|
+
(local $ch i32)
|
|
1608
|
+
(local $ks i32)
|
|
1609
|
+
(local $arr i32)
|
|
1610
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
1611
|
+
(if (i32.ne (i32.load (i32.add (local.get $n) (i32.const 4))) (i32.const 0)) (then (return)))
|
|
1612
|
+
(i32.store (i32.add (local.get $n) (i32.const 4)) (i32.const 1))
|
|
1613
|
+
(call $quota_remove_node)
|
|
1614
|
+
|
|
1615
|
+
;; Release kernel-local stacks & unshift buffers
|
|
1616
|
+
(if (i32.eq (i32.load (local.get $n)) (i32.const 15))
|
|
1617
|
+
(then
|
|
1618
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
1619
|
+
(if (i32.ne (local.get $ks) (i32.const 0))
|
|
1620
|
+
(then
|
|
1621
|
+
(local.set $cnt (i32.load (i32.add (local.get $ks) (i32.const 8))))
|
|
1622
|
+
(local.set $arr (i32.load (i32.add (local.get $ks) (i32.const 4))))
|
|
1623
|
+
(local.set $i (i32.const 0))
|
|
1624
|
+
(block $bs1
|
|
1625
|
+
(loop $ls1
|
|
1626
|
+
(br_if $bs1 (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1627
|
+
(call $bs_release (i32.load (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2)))))
|
|
1628
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1629
|
+
(br $ls1)
|
|
1630
|
+
)
|
|
1631
|
+
)
|
|
1632
|
+
(local.set $cnt (i32.load (i32.add (local.get $ks) (i32.const 16))))
|
|
1633
|
+
(local.set $arr (i32.load (i32.add (local.get $ks) (i32.const 12))))
|
|
1634
|
+
(local.set $i (i32.const 0))
|
|
1635
|
+
(block $bs2
|
|
1636
|
+
(loop $ls2
|
|
1637
|
+
(br_if $bs2 (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1638
|
+
(call $bd_release (i32.load (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2)))))
|
|
1639
|
+
(i32.store (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2))) (i32.const 0))
|
|
1640
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1641
|
+
(br $ls2)
|
|
1642
|
+
)
|
|
1643
|
+
)
|
|
1644
|
+
)
|
|
1645
|
+
)
|
|
1646
|
+
)
|
|
1647
|
+
)
|
|
1648
|
+
|
|
1649
|
+
;; Detach input consumer pointers
|
|
1650
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
1651
|
+
(local.set $i (i32.const 0))
|
|
1652
|
+
(block $bi
|
|
1653
|
+
(loop $li
|
|
1654
|
+
(br_if $bi (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1655
|
+
(local.set $ch (call $node_get_in (local.get $n) (local.get $i)))
|
|
1656
|
+
(if (i32.ne (local.get $ch) (i32.const 0))
|
|
1657
|
+
(then
|
|
1658
|
+
(if (i32.eq (i32.load (i32.add (local.get $ch) (i32.const 20))) (local.get $n))
|
|
1659
|
+
(then
|
|
1660
|
+
(i32.store (i32.add (local.get $ch) (i32.const 20)) (i32.const 0))
|
|
1661
|
+
(call $try_queue_chan (local.get $ch))))))
|
|
1662
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1663
|
+
(br $li)
|
|
1664
|
+
)
|
|
1665
|
+
)
|
|
1666
|
+
|
|
1667
|
+
;; Detach output producer pointers
|
|
1668
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
1669
|
+
(local.set $i (i32.const 0))
|
|
1670
|
+
(block $bo
|
|
1671
|
+
(loop $lo
|
|
1672
|
+
(br_if $bo (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1673
|
+
(local.set $ch (call $node_get_out (local.get $n) (local.get $i)))
|
|
1674
|
+
(if (i32.ne (local.get $ch) (i32.const 0))
|
|
1675
|
+
(then
|
|
1676
|
+
(if (i32.eq (i32.load (i32.add (local.get $ch) (i32.const 16))) (local.get $n))
|
|
1677
|
+
(then
|
|
1678
|
+
(i32.store (i32.add (local.get $ch) (i32.const 16)) (i32.const 0))
|
|
1679
|
+
(call $try_queue_chan (local.get $ch))))))
|
|
1680
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1681
|
+
(br $lo)
|
|
1682
|
+
)
|
|
1683
|
+
)
|
|
1684
|
+
|
|
1685
|
+
;; Unlink from scope & barrier (if drain), then defer memory reclamation until safepoint
|
|
1686
|
+
(call $scope_unlink_node (local.get $n))
|
|
1687
|
+
(if (i32.eq (i32.load (local.get $n)) (i32.const 9))
|
|
1688
|
+
(then (call $bar_unlink_drain (local.get $n))))
|
|
1689
|
+
(call $try_queue_node (local.get $n))
|
|
1690
|
+
)
|
|
1691
|
+
|
|
1692
|
+
(func $cancel_input (param $ch i32)
|
|
1693
|
+
(if (i32.eqz (local.get $ch)) (then (return)))
|
|
1694
|
+
(i32.store (i32.add (local.get $ch) (i32.const 20)) (i32.const 0))
|
|
1695
|
+
(call $bd_release (local.get $ch))
|
|
1696
|
+
)
|
|
1697
|
+
|
|
1698
|
+
(func $rebind_output (param $prodNode i32) (param $oldCh i32) (param $newCh i32)
|
|
1699
|
+
(local $cnt i32)
|
|
1700
|
+
(local $arr i32)
|
|
1701
|
+
(local $i i32)
|
|
1702
|
+
(local $kind i32)
|
|
1703
|
+
(local $ac i32)
|
|
1704
|
+
(if (i32.eqz (local.get $prodNode)) (then (return)))
|
|
1705
|
+
;; Update outputs array
|
|
1706
|
+
(local.set $cnt (i32.load (i32.add (local.get $prodNode) (i32.const 28))))
|
|
1707
|
+
(local.set $arr (i32.load (i32.add (local.get $prodNode) (i32.const 24))))
|
|
1708
|
+
(local.set $i (i32.const 0))
|
|
1709
|
+
(block $b1
|
|
1710
|
+
(loop $l1
|
|
1711
|
+
(br_if $b1 (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1712
|
+
(if (i32.eq (i32.load (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2)))) (local.get $oldCh))
|
|
1713
|
+
(then
|
|
1714
|
+
(call $chan_unpin (local.get $oldCh))
|
|
1715
|
+
(call $chan_pin (local.get $newCh))
|
|
1716
|
+
(i32.store (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2))) (local.get $newCh))))
|
|
1717
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1718
|
+
(br $l1)
|
|
1719
|
+
)
|
|
1720
|
+
)
|
|
1721
|
+
(local.set $kind (i32.load (local.get $prodNode)))
|
|
1722
|
+
;; If SWITCH, update captured envPtr slots!
|
|
1723
|
+
(if (i32.eq (local.get $kind) (i32.const 11))
|
|
1724
|
+
(then
|
|
1725
|
+
(local.set $arr (i32.load (i32.add (local.get $prodNode) (i32.const 36))))
|
|
1726
|
+
(local.set $cnt (i32.load (i32.add (local.get $prodNode) (i32.const 40))))
|
|
1727
|
+
(local.set $i (i32.const 0))
|
|
1728
|
+
(block $b2
|
|
1729
|
+
(loop $l2
|
|
1730
|
+
(br_if $b2 (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1731
|
+
(if (i32.eq (i32.load (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2)))) (local.get $oldCh))
|
|
1732
|
+
(then (i32.store (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2))) (local.get $newCh))))
|
|
1733
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1734
|
+
(br $l2)
|
|
1735
|
+
)
|
|
1736
|
+
)
|
|
1737
|
+
)
|
|
1738
|
+
)
|
|
1739
|
+
;; If SPEC with activeChild, update staged parentOut pointers!
|
|
1740
|
+
(if (i32.eq (local.get $kind) (i32.const 12))
|
|
1741
|
+
(then
|
|
1742
|
+
(local.set $ac (i32.load (i32.add (local.get $prodNode) (i32.const 40))))
|
|
1743
|
+
(if (i32.ne (local.get $ac) (i32.const 0))
|
|
1744
|
+
(then
|
|
1745
|
+
;; ActiveChild: +12 stagedPtr (pairs [childOut, parentOut]), +16 stagedCount
|
|
1746
|
+
(local.set $arr (i32.load (i32.add (local.get $ac) (i32.const 12))))
|
|
1747
|
+
(local.set $cnt (i32.load (i32.add (local.get $ac) (i32.const 16))))
|
|
1748
|
+
(local.set $i (i32.const 0))
|
|
1749
|
+
(block $b3
|
|
1750
|
+
(loop $l3
|
|
1751
|
+
(br_if $b3 (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
1752
|
+
(if (i32.eq (i32.load (i32.add (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 3))) (i32.const 4))) (local.get $oldCh))
|
|
1753
|
+
(then
|
|
1754
|
+
(call $chan_unpin (local.get $oldCh))
|
|
1755
|
+
(call $chan_pin (local.get $newCh))
|
|
1756
|
+
(i32.store (i32.add (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 3))) (i32.const 4)) (local.get $newCh))))
|
|
1757
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
1758
|
+
(br $l3)
|
|
1759
|
+
)
|
|
1760
|
+
)
|
|
1761
|
+
)
|
|
1762
|
+
)
|
|
1763
|
+
)
|
|
1764
|
+
)
|
|
1765
|
+
)
|
|
1766
|
+
|
|
1767
|
+
;; Section 3.3 O(1) Zero-Copy Channel Splice
|
|
1768
|
+
(func $splice (param $src i32) (param $dst i32)
|
|
1769
|
+
(local $srcLen i32)
|
|
1770
|
+
(local $dstLen i32)
|
|
1771
|
+
(local $srcProd i32)
|
|
1772
|
+
(if (i32.eq (local.get $src) (local.get $dst)) (then (return)))
|
|
1773
|
+
(global.set $g_splices (i32.add (global.get $g_splices) (i32.const 1)))
|
|
1774
|
+
|
|
1775
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 2))
|
|
1776
|
+
(then
|
|
1777
|
+
(call $bd_fail (local.get $dst))
|
|
1778
|
+
(i32.store (i32.add (local.get $src) (i32.const 16)) (i32.const 0))
|
|
1779
|
+
(i32.store (i32.add (local.get $src) (i32.const 20)) (i32.const 0))
|
|
1780
|
+
(call $bd_release (local.get $src))
|
|
1781
|
+
(return)
|
|
1782
|
+
)
|
|
1783
|
+
)
|
|
1784
|
+
|
|
1785
|
+
(local.set $srcLen (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
1786
|
+
(local.set $dstLen (i32.load (i32.add (local.get $dst) (i32.const 44))))
|
|
1787
|
+
(if (i32.eqz (local.get $dstLen))
|
|
1788
|
+
(then (call $bd_adopt_buffer (local.get $dst) (local.get $src)))
|
|
1789
|
+
(else
|
|
1790
|
+
(if (i32.gt_s (local.get $srcLen) (i32.const 0))
|
|
1791
|
+
(then
|
|
1792
|
+
(if (i32.le_s (local.get $dstLen) (local.get $srcLen))
|
|
1793
|
+
(then
|
|
1794
|
+
(call $bd_unshift_from_deque (local.get $src) (local.get $dst))
|
|
1795
|
+
(call $bd_adopt_buffer (local.get $dst) (local.get $src))
|
|
1796
|
+
)
|
|
1797
|
+
(else
|
|
1798
|
+
(call $bd_push_from_deque (local.get $dst) (local.get $src))
|
|
1799
|
+
)
|
|
1800
|
+
)
|
|
1801
|
+
)
|
|
1802
|
+
)
|
|
1803
|
+
)
|
|
1804
|
+
)
|
|
1805
|
+
|
|
1806
|
+
;; Transfer producer obligation, status, and upstream node pointer
|
|
1807
|
+
(i32.store (i32.add (local.get $dst) (i32.const 4)) (i32.load (i32.add (local.get $src) (i32.const 4))))
|
|
1808
|
+
(i32.store (local.get $dst) (i32.load (local.get $src)))
|
|
1809
|
+
(local.set $srcProd (i32.load (i32.add (local.get $src) (i32.const 16))))
|
|
1810
|
+
(i32.store (i32.add (local.get $dst) (i32.const 16)) (local.get $srcProd))
|
|
1811
|
+
(if (i32.ne (local.get $srcProd) (i32.const 0))
|
|
1812
|
+
(then (call $rebind_output (local.get $srcProd) (local.get $src) (local.get $dst))))
|
|
1813
|
+
|
|
1814
|
+
(i32.store (i32.add (local.get $src) (i32.const 16)) (i32.const 0))
|
|
1815
|
+
(i32.store (i32.add (local.get $src) (i32.const 20)) (i32.const 0))
|
|
1816
|
+
(call $bd_release (local.get $src))
|
|
1817
|
+
)
|
|
1818
|
+
|
|
1819
|
+
;; Strictly iterative scope destruction (destroys nested child scopes safely)
|
|
1820
|
+
(func $scope_destroy (param $rootSc i32)
|
|
1821
|
+
(local $stk i32)
|
|
1822
|
+
(local $stkCap i32)
|
|
1823
|
+
(local $stkLen i32)
|
|
1824
|
+
(local $ord i32)
|
|
1825
|
+
(local $ordLen i32)
|
|
1826
|
+
(local $sc i32)
|
|
1827
|
+
(local $child i32)
|
|
1828
|
+
(local $i i32)
|
|
1829
|
+
(local $n i32)
|
|
1830
|
+
(local $nextN i32)
|
|
1831
|
+
(local $ch i32)
|
|
1832
|
+
(local $nextCh i32)
|
|
1833
|
+
(local $ac i32)
|
|
1834
|
+
(local $taps i32)
|
|
1835
|
+
(local $tapCnt i32)
|
|
1836
|
+
(local $t i32)
|
|
1837
|
+
|
|
1838
|
+
(if (i32.eqz (local.get $rootSc)) (then (return)))
|
|
1839
|
+
(if (i32.ne (i32.load (i32.add (local.get $rootSc) (i32.const 20))) (i32.const 0)) (then (return)))
|
|
1840
|
+
|
|
1841
|
+
(if (i32.eqz (global.get $g_sc_cap))
|
|
1842
|
+
(then
|
|
1843
|
+
(global.set $g_sc_cap (i32.const 256))
|
|
1844
|
+
(global.set $g_sc_stk_ptr (call $alloc (i32.const 1024)))
|
|
1845
|
+
(global.set $g_sc_ord_ptr (call $alloc (i32.const 1024)))
|
|
1846
|
+
)
|
|
1847
|
+
)
|
|
1848
|
+
(local.set $stkCap (global.get $g_sc_cap))
|
|
1849
|
+
(local.set $stk (global.get $g_sc_stk_ptr))
|
|
1850
|
+
(local.set $ord (global.get $g_sc_ord_ptr))
|
|
1851
|
+
(if (i32.or (i32.eqz (local.get $stk)) (i32.eqz (local.get $ord))) (then (return)))
|
|
1852
|
+
|
|
1853
|
+
(i32.store (local.get $stk) (local.get $rootSc))
|
|
1854
|
+
(local.set $stkLen (i32.const 1))
|
|
1855
|
+
(local.set $ordLen (i32.const 0))
|
|
1856
|
+
|
|
1857
|
+
(block $bw
|
|
1858
|
+
(loop $lw
|
|
1859
|
+
(br_if $bw (i32.le_s (local.get $stkLen) (i32.const 0)))
|
|
1860
|
+
(local.set $stkLen (i32.sub (local.get $stkLen) (i32.const 1)))
|
|
1861
|
+
(local.set $sc (i32.load (i32.add (local.get $stk) (i32.shl (local.get $stkLen) (i32.const 2)))))
|
|
1862
|
+
(if (i32.ne (i32.load (i32.add (local.get $sc) (i32.const 20))) (i32.const 0)) (then (br $lw)))
|
|
1863
|
+
(if (i32.ge_s (local.get $ordLen) (local.get $stkCap))
|
|
1864
|
+
(then
|
|
1865
|
+
(local.set $t (call $alloc (i32.shl (local.get $stkCap) (i32.const 3))))
|
|
1866
|
+
(if (i32.eqz (local.get $t)) (then (return)))
|
|
1867
|
+
(memory.copy (local.get $t) (local.get $ord) (i32.shl (local.get $ordLen) (i32.const 2)))
|
|
1868
|
+
(call $free (local.get $ord))
|
|
1869
|
+
(local.set $ord (local.get $t))
|
|
1870
|
+
(local.set $t (call $alloc (i32.shl (local.get $stkCap) (i32.const 3))))
|
|
1871
|
+
(if (i32.eqz (local.get $t)) (then (return)))
|
|
1872
|
+
(memory.copy (local.get $t) (local.get $stk) (i32.shl (local.get $stkLen) (i32.const 2)))
|
|
1873
|
+
(call $free (local.get $stk))
|
|
1874
|
+
(local.set $stk (local.get $t))
|
|
1875
|
+
(local.set $stkCap (i32.shl (local.get $stkCap) (i32.const 1)))
|
|
1876
|
+
(global.set $g_sc_stk_ptr (local.get $stk))
|
|
1877
|
+
(global.set $g_sc_ord_ptr (local.get $ord))
|
|
1878
|
+
(global.set $g_sc_cap (local.get $stkCap))
|
|
1879
|
+
)
|
|
1880
|
+
)
|
|
1881
|
+
(i32.store (i32.add (local.get $ord) (i32.shl (local.get $ordLen) (i32.const 2))) (local.get $sc))
|
|
1882
|
+
(local.set $ordLen (i32.add (local.get $ordLen) (i32.const 1)))
|
|
1883
|
+
(local.set $child (i32.load (i32.add (local.get $sc) (i32.const 4))))
|
|
1884
|
+
(block $bc
|
|
1885
|
+
(loop $lc
|
|
1886
|
+
(br_if $bc (i32.eqz (local.get $child)))
|
|
1887
|
+
(if (i32.ge_s (local.get $stkLen) (local.get $stkCap))
|
|
1888
|
+
(then
|
|
1889
|
+
(local.set $t (call $alloc (i32.shl (local.get $stkCap) (i32.const 3))))
|
|
1890
|
+
(if (i32.eqz (local.get $t)) (then (return)))
|
|
1891
|
+
(memory.copy (local.get $t) (local.get $stk) (i32.shl (local.get $stkLen) (i32.const 2)))
|
|
1892
|
+
(call $free (local.get $stk))
|
|
1893
|
+
(local.set $stk (local.get $t))
|
|
1894
|
+
(local.set $t (call $alloc (i32.shl (local.get $stkCap) (i32.const 3))))
|
|
1895
|
+
(if (i32.eqz (local.get $t)) (then (return)))
|
|
1896
|
+
(memory.copy (local.get $t) (local.get $ord) (i32.shl (local.get $ordLen) (i32.const 2)))
|
|
1897
|
+
(call $free (local.get $ord))
|
|
1898
|
+
(local.set $ord (local.get $t))
|
|
1899
|
+
(local.set $stkCap (i32.shl (local.get $stkCap) (i32.const 1)))
|
|
1900
|
+
(global.set $g_sc_stk_ptr (local.get $stk))
|
|
1901
|
+
(global.set $g_sc_ord_ptr (local.get $ord))
|
|
1902
|
+
(global.set $g_sc_cap (local.get $stkCap))
|
|
1903
|
+
)
|
|
1904
|
+
)
|
|
1905
|
+
(i32.store (i32.add (local.get $stk) (i32.shl (local.get $stkLen) (i32.const 2))) (local.get $child))
|
|
1906
|
+
(local.set $stkLen (i32.add (local.get $stkLen) (i32.const 1)))
|
|
1907
|
+
(local.set $child (i32.load (i32.add (local.get $child) (i32.const 8))))
|
|
1908
|
+
(br $lc)
|
|
1909
|
+
)
|
|
1910
|
+
)
|
|
1911
|
+
(br $lw)
|
|
1912
|
+
)
|
|
1913
|
+
)
|
|
1914
|
+
|
|
1915
|
+
;; Tear down in reverse order (children before parents)
|
|
1916
|
+
(local.set $i (i32.sub (local.get $ordLen) (i32.const 1)))
|
|
1917
|
+
(block $bd
|
|
1918
|
+
(loop $ld
|
|
1919
|
+
(br_if $bd (i32.lt_s (local.get $i) (i32.const 0)))
|
|
1920
|
+
(local.set $sc (i32.load (i32.add (local.get $ord) (i32.shl (local.get $i) (i32.const 2)))))
|
|
1921
|
+
(i32.store (i32.add (local.get $sc) (i32.const 20)) (i32.const 1))
|
|
1922
|
+
|
|
1923
|
+
;; Retire all nodes in scope (capture next BEFORE $retire_node unlinks & reuses n+12!)
|
|
1924
|
+
(local.set $n (i32.load (i32.add (local.get $sc) (i32.const 12))))
|
|
1925
|
+
(block $bn
|
|
1926
|
+
(loop $ln
|
|
1927
|
+
(br_if $bn (i32.eqz (local.get $n)))
|
|
1928
|
+
(local.set $nextN (i32.load (i32.add (local.get $n) (i32.const 12))))
|
|
1929
|
+
;; If SPEC with activeChild, release replay deques and free activeChild!
|
|
1930
|
+
(if (i32.eq (i32.load (local.get $n)) (i32.const 12))
|
|
1931
|
+
(then
|
|
1932
|
+
(local.set $ac (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
1933
|
+
(if (i32.ne (local.get $ac) (i32.const 0))
|
|
1934
|
+
(then
|
|
1935
|
+
(local.set $taps (i32.load (i32.add (local.get $ac) (i32.const 4))))
|
|
1936
|
+
(local.set $tapCnt (i32.load (i32.add (local.get $ac) (i32.const 8))))
|
|
1937
|
+
(local.set $t (i32.const 0))
|
|
1938
|
+
(block $bt
|
|
1939
|
+
(loop $lt
|
|
1940
|
+
(br_if $bt (i32.ge_s (local.get $t) (local.get $tapCnt)))
|
|
1941
|
+
;; TapEntry (16 bytes): +0 parentCh, +4 childCh, +8 replayCh, +12 tapNode
|
|
1942
|
+
(call $bd_release (i32.load (i32.add (i32.add (local.get $taps) (i32.shl (local.get $t) (i32.const 4))) (i32.const 8))))
|
|
1943
|
+
(local.set $t (i32.add (local.get $t) (i32.const 1)))
|
|
1944
|
+
(br $lt)
|
|
1945
|
+
)
|
|
1946
|
+
)
|
|
1947
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
1948
|
+
(call $spec_free_active (local.get $ac))
|
|
1949
|
+
)
|
|
1950
|
+
)
|
|
1951
|
+
)
|
|
1952
|
+
)
|
|
1953
|
+
(call $retire_node (local.get $n))
|
|
1954
|
+
(local.set $n (local.get $nextN))
|
|
1955
|
+
(br $ln)
|
|
1956
|
+
)
|
|
1957
|
+
)
|
|
1958
|
+
|
|
1959
|
+
;; Release all channels still attached to this scope (capture next BEFORE $bd_release!)
|
|
1960
|
+
(local.set $ch (i32.load (i32.add (local.get $sc) (i32.const 16))))
|
|
1961
|
+
(block $bch
|
|
1962
|
+
(loop $lch
|
|
1963
|
+
(br_if $bch (i32.eqz (local.get $ch)))
|
|
1964
|
+
(local.set $nextCh (i32.load (i32.add (local.get $ch) (i32.const 28))))
|
|
1965
|
+
(if (i32.eq (i32.load (i32.add (local.get $ch) (i32.const 24))) (local.get $sc))
|
|
1966
|
+
(then
|
|
1967
|
+
(i32.store (i32.add (local.get $ch) (i32.const 16)) (i32.const 0))
|
|
1968
|
+
(i32.store (i32.add (local.get $ch) (i32.const 20)) (i32.const 0))
|
|
1969
|
+
(call $bd_release (local.get $ch))
|
|
1970
|
+
)
|
|
1971
|
+
)
|
|
1972
|
+
(local.set $ch (local.get $nextCh))
|
|
1973
|
+
(br $lch)
|
|
1974
|
+
)
|
|
1975
|
+
)
|
|
1976
|
+
|
|
1977
|
+
;; Unlink child scopes from parent and defer-free them
|
|
1978
|
+
(if (i32.ne (i32.load (local.get $sc)) (i32.const 0))
|
|
1979
|
+
(then
|
|
1980
|
+
(call $scope_unlink_child (local.get $sc))
|
|
1981
|
+
(i32.store (i32.add (local.get $sc) (i32.const 8)) (global.get $g_dead_scopes))
|
|
1982
|
+
(global.set $g_dead_scopes (local.get $sc))
|
|
1983
|
+
)
|
|
1984
|
+
)
|
|
1985
|
+
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
|
|
1986
|
+
(br $ld)
|
|
1987
|
+
)
|
|
1988
|
+
)
|
|
1989
|
+
)
|
|
1990
|
+
|
|
1991
|
+
;; ============================================================================
|
|
1992
|
+
;; 7. Node Instantiation Helpers (Called by Compiled Proc Blocks!)
|
|
1993
|
+
;; ============================================================================
|
|
1994
|
+
|
|
1995
|
+
(func $emit_const_unit (param $sc i32) (param $dst i32)
|
|
1996
|
+
(local $n i32)
|
|
1997
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 1) (i32.const 0) (i32.const 1)))
|
|
1998
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
1999
|
+
(call $node_set_out (local.get $n) (i32.const 0) (local.get $dst))
|
|
2000
|
+
)
|
|
2001
|
+
|
|
2002
|
+
(func $emit_splice (param $sc i32) (param $src i32) (param $dst i32)
|
|
2003
|
+
(local $n i32)
|
|
2004
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 2) (i32.const 1) (i32.const 1)))
|
|
2005
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
2006
|
+
(call $node_set_in (local.get $n) (i32.const 0) (local.get $src))
|
|
2007
|
+
(call $node_set_out (local.get $n) (i32.const 0) (local.get $dst))
|
|
2008
|
+
)
|
|
2009
|
+
|
|
2010
|
+
(func $emit_coerce (param $sc i32) (param $src i32) (param $dst i32) (param $sSid i32) (param $dSid i32)
|
|
2011
|
+
(local $n i32)
|
|
2012
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 3) (i32.const 1) (i32.const 1)))
|
|
2013
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
2014
|
+
(call $node_set_in (local.get $n) (i32.const 0) (local.get $src))
|
|
2015
|
+
(call $node_set_out (local.get $n) (i32.const 0) (local.get $dst))
|
|
2016
|
+
(i32.store (i32.add (local.get $n) (i32.const 32)) (local.get $sSid))
|
|
2017
|
+
(i32.store (i32.add (local.get $n) (i32.const 36)) (local.get $dSid))
|
|
2018
|
+
)
|
|
2019
|
+
|
|
2020
|
+
(func $emit_wrap (param $sc i32) (param $src i32) (param $dst i32) (param $tagId i32)
|
|
2021
|
+
(local $n i32)
|
|
2022
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 4) (i32.const 1) (i32.const 1)))
|
|
2023
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
2024
|
+
(call $node_set_in (local.get $n) (i32.const 0) (local.get $src))
|
|
2025
|
+
(call $node_set_out (local.get $n) (i32.const 0) (local.get $dst))
|
|
2026
|
+
(i32.store (i32.add (local.get $n) (i32.const 32)) (local.get $tagId))
|
|
2027
|
+
)
|
|
2028
|
+
|
|
2029
|
+
(func $emit_match (param $sc i32) (param $src i32) (param $dst i32) (param $tagId i32)
|
|
2030
|
+
(local $n i32)
|
|
2031
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 5) (i32.const 1) (i32.const 1)))
|
|
2032
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
2033
|
+
(call $node_set_in (local.get $n) (i32.const 0) (local.get $src))
|
|
2034
|
+
(call $node_set_out (local.get $n) (i32.const 0) (local.get $dst))
|
|
2035
|
+
(i32.store (i32.add (local.get $n) (i32.const 32)) (local.get $tagId))
|
|
2036
|
+
)
|
|
2037
|
+
|
|
2038
|
+
(func $emit_drain (param $sc i32) (param $src i32) (param $schemaId i32) (param $bar i32)
|
|
2039
|
+
(local $n i32)
|
|
2040
|
+
(local $oldFirst i32)
|
|
2041
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 9) (i32.const 1) (i32.const 0)))
|
|
2042
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
2043
|
+
(call $node_set_in (local.get $n) (i32.const 0) (local.get $src))
|
|
2044
|
+
(i32.store (i32.add (local.get $n) (i32.const 32)) (local.get $schemaId))
|
|
2045
|
+
(i32.store (i32.add (local.get $n) (i32.const 36)) (local.get $bar))
|
|
2046
|
+
(i32.store (i32.add (local.get $n) (i32.const 44)) (call $dpda_alloc (i32.const 0) (local.get $schemaId) (i32.const 0)))
|
|
2047
|
+
(if (i32.ne (local.get $bar) (i32.const 0))
|
|
2048
|
+
(then
|
|
2049
|
+
(i32.store (i32.add (local.get $bar) (i32.const 4)) (i32.add (i32.load (i32.add (local.get $bar) (i32.const 4))) (i32.const 1)))
|
|
2050
|
+
(local.set $oldFirst (i32.load (i32.add (local.get $bar) (i32.const 8))))
|
|
2051
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (local.get $oldFirst))
|
|
2052
|
+
(if (i32.ne (local.get $oldFirst) (i32.const 0))
|
|
2053
|
+
(then (i32.store (i32.add (local.get $oldFirst) (i32.const 52)) (local.get $n))))
|
|
2054
|
+
(i32.store (i32.add (local.get $bar) (i32.const 8)) (local.get $n))
|
|
2055
|
+
)
|
|
2056
|
+
)
|
|
2057
|
+
)
|
|
2058
|
+
|
|
2059
|
+
(func $emit_gate (param $sc i32) (param $bar i32) (param $src i32) (param $dst i32)
|
|
2060
|
+
(local $n i32)
|
|
2061
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 10) (i32.const 1) (i32.const 1)))
|
|
2062
|
+
(if (i32.eqz (local.get $n)) (then (return)))
|
|
2063
|
+
(call $node_set_in (local.get $n) (i32.const 0) (local.get $src))
|
|
2064
|
+
(call $node_set_out (local.get $n) (i32.const 0) (local.get $dst))
|
|
2065
|
+
(i32.store (i32.add (local.get $n) (i32.const 32)) (local.get $bar))
|
|
2066
|
+
)
|
|
2067
|
+
|
|
2068
|
+
;; Instantiate a kernel node (regs/stacks/unshiftBufs/temps allocated from meta!)
|
|
2069
|
+
(func $emit_kernel (param $sc i32) (param $kernelFuncIdx i32) (param $inCount i32) (param $outCount i32) (param $numRegs i32) (param $numStacks i32) (param $numTemps i32) (param $regInitPtr i32) (result i32)
|
|
2070
|
+
(local $n i32)
|
|
2071
|
+
(local $ks i32)
|
|
2072
|
+
(local $arr i32)
|
|
2073
|
+
(local $i i32)
|
|
2074
|
+
(local.set $n (call $node_alloc (local.get $sc) (i32.const 15) (local.get $inCount) (local.get $outCount)))
|
|
2075
|
+
(if (i32.eqz (local.get $n)) (then (return (i32.const 0))))
|
|
2076
|
+
(i32.store (i32.add (local.get $n) (i32.const 32)) (local.get $kernelFuncIdx))
|
|
2077
|
+
(i32.store (i32.add (local.get $n) (i32.const 36)) (i32.const 0)) ;; pc = 0 (entry)
|
|
2078
|
+
|
|
2079
|
+
(local.set $ks (call $alloc (i32.const 48)))
|
|
2080
|
+
(if (i32.eqz (local.get $ks)) (then (return (i32.const 0))))
|
|
2081
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (local.get $ks))
|
|
2082
|
+
|
|
2083
|
+
(if (i32.gt_s (local.get $numRegs) (i32.const 0))
|
|
2084
|
+
(then
|
|
2085
|
+
(local.set $arr (call $alloc (i32.shl (local.get $numRegs) (i32.const 2))))
|
|
2086
|
+
(if (i32.ne (local.get $regInitPtr) (i32.const 0))
|
|
2087
|
+
(then (memory.copy (local.get $arr) (local.get $regInitPtr) (i32.shl (local.get $numRegs) (i32.const 2)))))
|
|
2088
|
+
(i32.store (local.get $ks) (local.get $arr))
|
|
2089
|
+
)
|
|
2090
|
+
)
|
|
2091
|
+
(if (i32.gt_s (local.get $numStacks) (i32.const 0))
|
|
2092
|
+
(then
|
|
2093
|
+
(local.set $arr (call $alloc (i32.shl (local.get $numStacks) (i32.const 2))))
|
|
2094
|
+
(i32.store (i32.add (local.get $ks) (i32.const 4)) (local.get $arr))
|
|
2095
|
+
(i32.store (i32.add (local.get $ks) (i32.const 8)) (local.get $numStacks))
|
|
2096
|
+
(local.set $i (i32.const 0))
|
|
2097
|
+
(block $bs
|
|
2098
|
+
(loop $ls
|
|
2099
|
+
(br_if $bs (i32.ge_s (local.get $i) (local.get $numStacks)))
|
|
2100
|
+
(i32.store (i32.add (local.get $arr) (i32.shl (local.get $i) (i32.const 2))) (call $bs_alloc))
|
|
2101
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
2102
|
+
(br $ls)
|
|
2103
|
+
)
|
|
2104
|
+
)
|
|
2105
|
+
)
|
|
2106
|
+
)
|
|
2107
|
+
(if (i32.gt_s (local.get $outCount) (i32.const 0))
|
|
2108
|
+
(then
|
|
2109
|
+
(local.set $arr (call $alloc (i32.shl (local.get $outCount) (i32.const 2))))
|
|
2110
|
+
(i32.store (i32.add (local.get $ks) (i32.const 12)) (local.get $arr))
|
|
2111
|
+
(i32.store (i32.add (local.get $ks) (i32.const 16)) (local.get $outCount))
|
|
2112
|
+
)
|
|
2113
|
+
)
|
|
2114
|
+
(if (i32.gt_s (local.get $numTemps) (i32.const 0))
|
|
2115
|
+
(then
|
|
2116
|
+
(i32.store (i32.add (local.get $ks) (i32.const 20)) (call $alloc (i32.shl (local.get $numTemps) (i32.const 2))))
|
|
2117
|
+
)
|
|
2118
|
+
)
|
|
2119
|
+
(i32.store (i32.add (local.get $ks) (i32.const 28)) (i32.const 64)) ;; yieldThreshold = 64
|
|
2120
|
+
(i32.store (i32.add (local.get $ks) (i32.const 32)) (call $mask32 (local.get $inCount)))
|
|
2121
|
+
(i32.store (i32.add (local.get $ks) (i32.const 36)) (call $mask32 (local.get $outCount)))
|
|
2122
|
+
(local.get $n)
|
|
2123
|
+
)
|
|
2124
|
+
|
|
2125
|
+
;; ============================================================================
|
|
2126
|
+
;; 8. Kernel Runtime Support Helpers (Called by Compiled Kernel Functions!)
|
|
2127
|
+
;; ============================================================================
|
|
2128
|
+
|
|
2129
|
+
(func $k_get_unshift_buf (param $n i32) (param $outIdx i32) (result i32)
|
|
2130
|
+
(local $ks i32)
|
|
2131
|
+
(local $arr i32)
|
|
2132
|
+
(local $ub i32)
|
|
2133
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2134
|
+
(local.set $arr (i32.load (i32.add (local.get $ks) (i32.const 12))))
|
|
2135
|
+
(local.set $ub (i32.load (i32.add (local.get $arr) (i32.shl (local.get $outIdx) (i32.const 2)))))
|
|
2136
|
+
(if (i32.eqz (local.get $ub))
|
|
2137
|
+
(then
|
|
2138
|
+
(local.set $ub (call $chan_alloc (i32.const 0) (i32.const 0)))
|
|
2139
|
+
(i32.store (i32.add (local.get $arr) (i32.shl (local.get $outIdx) (i32.const 2))) (local.get $ub))
|
|
2140
|
+
)
|
|
2141
|
+
)
|
|
2142
|
+
(local.get $ub)
|
|
2143
|
+
)
|
|
2144
|
+
|
|
2145
|
+
(func $k_flush_unshift_buf (param $n i32) (param $outIdx i32) (param $dst i32)
|
|
2146
|
+
(local $ks i32)
|
|
2147
|
+
(local $arr i32)
|
|
2148
|
+
(local $ub i32)
|
|
2149
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2150
|
+
(local.set $arr (i32.load (i32.add (local.get $ks) (i32.const 12))))
|
|
2151
|
+
(local.set $ub (i32.load (i32.add (local.get $arr) (i32.shl (local.get $outIdx) (i32.const 2)))))
|
|
2152
|
+
(if (i32.eqz (local.get $ub)) (then (return)))
|
|
2153
|
+
(i32.store (i32.add (local.get $arr) (i32.shl (local.get $outIdx) (i32.const 2))) (i32.const 0))
|
|
2154
|
+
(if (i32.gt_s (i32.load (i32.add (local.get $ub) (i32.const 44))) (i32.const 0))
|
|
2155
|
+
(then
|
|
2156
|
+
(if (i32.eqz (i32.load (i32.add (local.get $dst) (i32.const 44))))
|
|
2157
|
+
(then (call $bd_adopt_buffer (local.get $dst) (local.get $ub)))
|
|
2158
|
+
(else (call $bd_push_from_deque (local.get $dst) (local.get $ub)))
|
|
2159
|
+
)
|
|
2160
|
+
)
|
|
2161
|
+
)
|
|
2162
|
+
(call $bd_release (local.get $ub))
|
|
2163
|
+
)
|
|
2164
|
+
|
|
2165
|
+
(func $k_fail (param $n i32) (result i32)
|
|
2166
|
+
(local $ks i32)
|
|
2167
|
+
(local $inMask i32)
|
|
2168
|
+
(local $outMask i32)
|
|
2169
|
+
(local $cnt i32)
|
|
2170
|
+
(local $i i32)
|
|
2171
|
+
(local $ch i32)
|
|
2172
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2173
|
+
(local.set $inMask (i32.load (i32.add (local.get $ks) (i32.const 32))))
|
|
2174
|
+
(local.set $outMask (i32.load (i32.add (local.get $ks) (i32.const 36))))
|
|
2175
|
+
;; Snapshot before retire
|
|
2176
|
+
(call $retire_node (local.get $n))
|
|
2177
|
+
|
|
2178
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
2179
|
+
(local.set $i (i32.const 0))
|
|
2180
|
+
(block $bo
|
|
2181
|
+
(loop $lo
|
|
2182
|
+
(br_if $bo (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
2183
|
+
(if (i32.and (i32.shr_u (local.get $outMask) (local.get $i)) (i32.const 1))
|
|
2184
|
+
(then
|
|
2185
|
+
(local.set $ch (call $node_get_out (local.get $n) (local.get $i)))
|
|
2186
|
+
(if (i32.ne (local.get $ch) (i32.const 0)) (then (call $bd_fail (local.get $ch))))))
|
|
2187
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
2188
|
+
(br $lo)
|
|
2189
|
+
)
|
|
2190
|
+
)
|
|
2191
|
+
|
|
2192
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
2193
|
+
(local.set $i (i32.const 0))
|
|
2194
|
+
(block $bi
|
|
2195
|
+
(loop $li
|
|
2196
|
+
(br_if $bi (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
2197
|
+
(if (i32.and (i32.shr_u (local.get $inMask) (local.get $i)) (i32.const 1))
|
|
2198
|
+
(then
|
|
2199
|
+
(local.set $ch (call $node_get_in (local.get $n) (local.get $i)))
|
|
2200
|
+
(call $cancel_input (local.get $ch))))
|
|
2201
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
2202
|
+
(br $li)
|
|
2203
|
+
)
|
|
2204
|
+
)
|
|
2205
|
+
(i32.const 0)
|
|
2206
|
+
)
|
|
2207
|
+
|
|
2208
|
+
;; Normal kernel retire: release all still-owned input channels (not called by handoff!)
|
|
2209
|
+
(func $k_retire (param $n i32)
|
|
2210
|
+
(local $ks i32)
|
|
2211
|
+
(local $inMask i32)
|
|
2212
|
+
(local $cnt i32)
|
|
2213
|
+
(local $i i32)
|
|
2214
|
+
(local $ch i32)
|
|
2215
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2216
|
+
(if (i32.ne (local.get $ks) (i32.const 0))
|
|
2217
|
+
(then
|
|
2218
|
+
(local.set $inMask (i32.load (i32.add (local.get $ks) (i32.const 32))))
|
|
2219
|
+
(i32.store (i32.add (local.get $ks) (i32.const 32)) (i32.const 0))
|
|
2220
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
2221
|
+
(local.set $i (i32.const 0))
|
|
2222
|
+
(block $bi
|
|
2223
|
+
(loop $li
|
|
2224
|
+
(br_if $bi (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
2225
|
+
(if (i32.and (i32.shr_u (local.get $inMask) (local.get $i)) (i32.const 1))
|
|
2226
|
+
(then
|
|
2227
|
+
(local.set $ch (call $node_get_in (local.get $n) (local.get $i)))
|
|
2228
|
+
(call $cancel_input (local.get $ch))))
|
|
2229
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
2230
|
+
(br $li)
|
|
2231
|
+
)
|
|
2232
|
+
)
|
|
2233
|
+
)
|
|
2234
|
+
)
|
|
2235
|
+
(call $retire_node (local.get $n))
|
|
2236
|
+
)
|
|
2237
|
+
|
|
2238
|
+
;; Kernel `drain_guard`: returns 0=done, 1=needPull, 2=failed
|
|
2239
|
+
(func $k_drain_guard (param $n i32) (param $inIdx i32) (param $schemaId i32) (result i32)
|
|
2240
|
+
(local $ks i32)
|
|
2241
|
+
(local $ch i32)
|
|
2242
|
+
(local $dpda i32)
|
|
2243
|
+
(local $res i32)
|
|
2244
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2245
|
+
(local.set $ch (call $node_get_in (local.get $n) (local.get $inIdx)))
|
|
2246
|
+
(local.set $dpda (i32.load (i32.add (local.get $ks) (i32.const 24))))
|
|
2247
|
+
(if (i32.eqz (local.get $dpda))
|
|
2248
|
+
(then
|
|
2249
|
+
(local.set $dpda (call $dpda_alloc (i32.const 0) (local.get $schemaId) (i32.const 0)))
|
|
2250
|
+
(i32.store (i32.add (local.get $ks) (i32.const 24)) (local.get $dpda))
|
|
2251
|
+
)
|
|
2252
|
+
)
|
|
2253
|
+
(if (i32.eqz (i32.load (i32.add (local.get $dpda) (i32.const 16))))
|
|
2254
|
+
(then
|
|
2255
|
+
(local.set $res (call $dpda_step_skip_copy (local.get $ch) (i32.const 0) (local.get $dpda)))
|
|
2256
|
+
(if (i32.eq (local.get $res) (i32.const 1))
|
|
2257
|
+
(then
|
|
2258
|
+
(if (i32.eq (i32.load (local.get $ch)) (i32.const 0))
|
|
2259
|
+
(then
|
|
2260
|
+
(global.set $g_need_pull (local.get $ch))
|
|
2261
|
+
(return (i32.const 1))))
|
|
2262
|
+
(return (i32.const 2))))
|
|
2263
|
+
(if (i32.eq (local.get $res) (i32.const 2)) (then (return (i32.const 2))))
|
|
2264
|
+
(i32.store (i32.add (local.get $dpda) (i32.const 16)) (i32.const 1))
|
|
2265
|
+
)
|
|
2266
|
+
)
|
|
2267
|
+
(if (i32.eq (i32.load (local.get $ch)) (i32.const 0))
|
|
2268
|
+
(then
|
|
2269
|
+
(global.set $g_need_pull (local.get $ch))
|
|
2270
|
+
(return (i32.const 1))))
|
|
2271
|
+
(if (i32.or
|
|
2272
|
+
(i32.eq (i32.load (local.get $ch)) (i32.const 2))
|
|
2273
|
+
(i32.gt_s (i32.load (i32.add (local.get $ch) (i32.const 44))) (i32.const 0)))
|
|
2274
|
+
(then (return (i32.const 2))))
|
|
2275
|
+
(call $dpda_free (local.get $dpda))
|
|
2276
|
+
(i32.store (i32.add (local.get $ks) (i32.const 24)) (i32.const 0))
|
|
2277
|
+
;; Mark input disowned and release
|
|
2278
|
+
(i32.store (i32.add (local.get $ks) (i32.const 32))
|
|
2279
|
+
(i32.and (i32.load (i32.add (local.get $ks) (i32.const 32))) (i32.xor (i32.shl (i32.const 1) (local.get $inIdx)) (i32.const -1))))
|
|
2280
|
+
(i32.store (i32.add (local.get $ch) (i32.const 20)) (i32.const 0))
|
|
2281
|
+
(call $bd_release (local.get $ch))
|
|
2282
|
+
(i32.const 0)
|
|
2283
|
+
)
|
|
2284
|
+
|
|
2285
|
+
;; Kernel `dpda_copy`: returns 0=done, 1=needPull, 2=failed
|
|
2286
|
+
(func $k_dpda_copy (param $n i32) (param $inIdx i32) (param $outIdx i32) (param $schemaId i32) (result i32)
|
|
2287
|
+
(local $ks i32)
|
|
2288
|
+
(local $src i32)
|
|
2289
|
+
(local $dst i32)
|
|
2290
|
+
(local $dpda i32)
|
|
2291
|
+
(local $res i32)
|
|
2292
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2293
|
+
(local.set $src (call $node_get_in (local.get $n) (local.get $inIdx)))
|
|
2294
|
+
(local.set $dst (call $node_get_out (local.get $n) (local.get $outIdx)))
|
|
2295
|
+
(local.set $dpda (i32.load (i32.add (local.get $ks) (i32.const 24))))
|
|
2296
|
+
(if (i32.eqz (local.get $dpda))
|
|
2297
|
+
(then
|
|
2298
|
+
(local.set $dpda (call $dpda_alloc (i32.const 1) (local.get $schemaId) (i32.const 0)))
|
|
2299
|
+
(i32.store (i32.add (local.get $ks) (i32.const 24)) (local.get $dpda))
|
|
2300
|
+
)
|
|
2301
|
+
)
|
|
2302
|
+
(local.set $res (call $dpda_step_skip_copy (local.get $src) (local.get $dst) (local.get $dpda)))
|
|
2303
|
+
(if (i32.eq (local.get $res) (i32.const 1))
|
|
2304
|
+
(then
|
|
2305
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
2306
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
2307
|
+
(return (i32.const 2))))
|
|
2308
|
+
(if (i32.eq (local.get $res) (i32.const 2)) (then (return (i32.const 2))))
|
|
2309
|
+
(call $dpda_free (local.get $dpda))
|
|
2310
|
+
(i32.store (i32.add (local.get $ks) (i32.const 24)) (i32.const 0))
|
|
2311
|
+
(i32.const 0)
|
|
2312
|
+
)
|
|
2313
|
+
|
|
2314
|
+
;; Kernel `dpda_coerce`: returns 0=done, 1=needPull, 2=failed
|
|
2315
|
+
(func $k_dpda_coerce (param $n i32) (param $inIdx i32) (param $outIdx i32) (param $sSid i32) (param $dSid i32) (result i32)
|
|
2316
|
+
(local $ks i32)
|
|
2317
|
+
(local $src i32)
|
|
2318
|
+
(local $dst i32)
|
|
2319
|
+
(local $dpda i32)
|
|
2320
|
+
(local $res i32)
|
|
2321
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2322
|
+
(local.set $src (call $node_get_in (local.get $n) (local.get $inIdx)))
|
|
2323
|
+
(local.set $dst (call $node_get_out (local.get $n) (local.get $outIdx)))
|
|
2324
|
+
(local.set $dpda (i32.load (i32.add (local.get $ks) (i32.const 24))))
|
|
2325
|
+
(if (i32.eqz (local.get $dpda))
|
|
2326
|
+
(then
|
|
2327
|
+
(local.set $dpda (call $dpda_alloc (i32.const 2) (local.get $sSid) (local.get $dSid)))
|
|
2328
|
+
(i32.store (i32.add (local.get $ks) (i32.const 24)) (local.get $dpda))
|
|
2329
|
+
)
|
|
2330
|
+
)
|
|
2331
|
+
(local.set $res (call $dpda_step_coerce (local.get $src) (local.get $dst) (local.get $dpda)))
|
|
2332
|
+
(if (i32.eq (local.get $res) (i32.const 1))
|
|
2333
|
+
(then
|
|
2334
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
2335
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
2336
|
+
(return (i32.const 2))))
|
|
2337
|
+
(if (i32.eq (local.get $res) (i32.const 2)) (then (return (i32.const 2))))
|
|
2338
|
+
(call $dpda_free (local.get $dpda))
|
|
2339
|
+
(i32.store (i32.add (local.get $ks) (i32.const 24)) (i32.const 0))
|
|
2340
|
+
(i32.const 0)
|
|
2341
|
+
)
|
|
2342
|
+
|
|
2343
|
+
(func $k_splice (param $n i32) (param $inIdx i32) (param $outIdx i32)
|
|
2344
|
+
(local $ks i32)
|
|
2345
|
+
(local $src i32)
|
|
2346
|
+
(local $dst i32)
|
|
2347
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2348
|
+
(local.set $src (call $node_get_in (local.get $n) (local.get $inIdx)))
|
|
2349
|
+
(local.set $dst (call $node_get_out (local.get $n) (local.get $outIdx)))
|
|
2350
|
+
(call $k_flush_unshift_buf (local.get $n) (local.get $outIdx) (local.get $dst))
|
|
2351
|
+
(i32.store (i32.add (local.get $ks) (i32.const 32))
|
|
2352
|
+
(i32.and (i32.load (i32.add (local.get $ks) (i32.const 32))) (i32.xor (i32.shl (i32.const 1) (local.get $inIdx)) (i32.const -1))))
|
|
2353
|
+
(i32.store (i32.add (local.get $ks) (i32.const 36))
|
|
2354
|
+
(i32.and (i32.load (i32.add (local.get $ks) (i32.const 36))) (i32.xor (i32.shl (i32.const 1) (local.get $outIdx)) (i32.const -1))))
|
|
2355
|
+
(if (i32.eq (i32.load (i32.add (local.get $src) (i32.const 20))) (local.get $n))
|
|
2356
|
+
(then (i32.store (i32.add (local.get $src) (i32.const 20)) (i32.const 0))))
|
|
2357
|
+
(if (i32.eq (i32.load (i32.add (local.get $dst) (i32.const 16))) (local.get $n))
|
|
2358
|
+
(then (i32.store (i32.add (local.get $dst) (i32.const 16)) (i32.const 0))))
|
|
2359
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
2360
|
+
)
|
|
2361
|
+
|
|
2362
|
+
(func $k_close (param $n i32) (param $outIdx i32)
|
|
2363
|
+
(local $ks i32)
|
|
2364
|
+
(local $dst i32)
|
|
2365
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2366
|
+
(local.set $dst (call $node_get_out (local.get $n) (local.get $outIdx)))
|
|
2367
|
+
(call $k_flush_unshift_buf (local.get $n) (local.get $outIdx) (local.get $dst))
|
|
2368
|
+
(i32.store (i32.add (local.get $ks) (i32.const 36))
|
|
2369
|
+
(i32.and (i32.load (i32.add (local.get $ks) (i32.const 36))) (i32.xor (i32.shl (i32.const 1) (local.get $outIdx)) (i32.const -1))))
|
|
2370
|
+
(if (i32.eq (i32.load (i32.add (local.get $dst) (i32.const 16))) (local.get $n))
|
|
2371
|
+
(then (i32.store (i32.add (local.get $dst) (i32.const 16)) (i32.const 0))))
|
|
2372
|
+
(call $bd_close (local.get $dst))
|
|
2373
|
+
)
|
|
2374
|
+
|
|
2375
|
+
;; Exact SWAR PEXT bit compression matching `vm.mjs` and `synthesize-tables.mjs`
|
|
2376
|
+
(func $compress_bits (param $raw i32) (param $rawBits i32) (param $mask i32) (result i32)
|
|
2377
|
+
(local $out i32)
|
|
2378
|
+
(local $bitPos i32)
|
|
2379
|
+
(local $i i32)
|
|
2380
|
+
(if (i32.eq (local.get $mask) (i32.const -1)) (then (return (local.get $raw))))
|
|
2381
|
+
(local.set $out (i32.const 0))
|
|
2382
|
+
(local.set $bitPos (i32.const 0))
|
|
2383
|
+
(local.set $i (i32.const 0))
|
|
2384
|
+
(block $brk
|
|
2385
|
+
(loop $lp
|
|
2386
|
+
(br_if $brk (i32.ge_s (local.get $i) (local.get $rawBits)))
|
|
2387
|
+
(if (i32.and (i32.shr_u (local.get $mask) (local.get $i)) (i32.const 1))
|
|
2388
|
+
(then
|
|
2389
|
+
(local.set $out
|
|
2390
|
+
(i32.or (local.get $out)
|
|
2391
|
+
(i32.shl (i32.and (i32.shr_u (local.get $raw) (local.get $i)) (i32.const 1)) (local.get $bitPos))))
|
|
2392
|
+
(local.set $bitPos (i32.add (local.get $bitPos) (i32.const 1)))
|
|
2393
|
+
)
|
|
2394
|
+
)
|
|
2395
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
2396
|
+
(br $lp)
|
|
2397
|
+
)
|
|
2398
|
+
)
|
|
2399
|
+
(local.get $out)
|
|
2400
|
+
)
|
|
2401
|
+
|
|
2402
|
+
;; Hot-burst `table_step` execution:
|
|
2403
|
+
;; Returns: 0 = burst done (continue), 1 = needPull, 2 = yielded, 3 = short, 4 = fail, >= 100 = exit (100 + exitId)
|
|
2404
|
+
(func $exec_chunk_table_step
|
|
2405
|
+
(param $n i32) (param $meta i32) (param $regIdx i32)
|
|
2406
|
+
(param $in0Idx i32) (param $in1Idx i32) (param $out0Idx i32) (param $out1Idx i32)
|
|
2407
|
+
(result i32)
|
|
2408
|
+
(local $ks i32)
|
|
2409
|
+
(local $regsPtr i32)
|
|
2410
|
+
(local $entriesPtr i32)
|
|
2411
|
+
(local $numEntries i32)
|
|
2412
|
+
(local $numIn i32)
|
|
2413
|
+
(local $numOut i32)
|
|
2414
|
+
(local $raw0 i32)
|
|
2415
|
+
(local $eff0 i32)
|
|
2416
|
+
(local $mask0 i32)
|
|
2417
|
+
(local $raw1 i32)
|
|
2418
|
+
(local $eff1 i32)
|
|
2419
|
+
(local $mask1 i32)
|
|
2420
|
+
(local $mode0 i32)
|
|
2421
|
+
(local $mode1 i32)
|
|
2422
|
+
(local $in0 i32)
|
|
2423
|
+
(local $in1 i32)
|
|
2424
|
+
(local $out0 i32)
|
|
2425
|
+
(local $out1 i32)
|
|
2426
|
+
(local $yThresh i32)
|
|
2427
|
+
(local $iter i32)
|
|
2428
|
+
(local $idx i32)
|
|
2429
|
+
(local $ent i32)
|
|
2430
|
+
(local $kind i32)
|
|
2431
|
+
(local $sh i32)
|
|
2432
|
+
(local $elen i32)
|
|
2433
|
+
(local $eword i32)
|
|
2434
|
+
|
|
2435
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2436
|
+
(local.set $regsPtr (i32.load (local.get $ks)))
|
|
2437
|
+
(local.set $yThresh (i32.load (i32.add (local.get $ks) (i32.const 28))))
|
|
2438
|
+
(local.set $entriesPtr (i32.load (local.get $meta)))
|
|
2439
|
+
(local.set $numEntries (i32.load (i32.add (local.get $meta) (i32.const 4))))
|
|
2440
|
+
(if (i32.le_s (local.get $numEntries) (i32.const 0)) (then (return (i32.const 3))))
|
|
2441
|
+
|
|
2442
|
+
(local.set $numIn (i32.load (i32.add (local.get $meta) (i32.const 8))))
|
|
2443
|
+
(local.set $numOut (i32.load (i32.add (local.get $meta) (i32.const 12))))
|
|
2444
|
+
(local.set $raw0 (i32.load (i32.add (local.get $meta) (i32.const 16))))
|
|
2445
|
+
(local.set $eff0 (i32.load (i32.add (local.get $meta) (i32.const 20))))
|
|
2446
|
+
(local.set $mask0 (i32.load (i32.add (local.get $meta) (i32.const 24))))
|
|
2447
|
+
(local.set $raw1 (i32.load (i32.add (local.get $meta) (i32.const 28))))
|
|
2448
|
+
(local.set $eff1 (i32.load (i32.add (local.get $meta) (i32.const 32))))
|
|
2449
|
+
(local.set $mask1 (i32.load (i32.add (local.get $meta) (i32.const 36))))
|
|
2450
|
+
(local.set $mode0 (i32.load (i32.add (local.get $meta) (i32.const 40))))
|
|
2451
|
+
(local.set $mode1 (i32.load (i32.add (local.get $meta) (i32.const 44))))
|
|
2452
|
+
|
|
2453
|
+
(local.set $in0 (call $node_get_in (local.get $n) (local.get $in0Idx)))
|
|
2454
|
+
(if (i32.gt_s (local.get $numIn) (i32.const 1))
|
|
2455
|
+
(then (local.set $in1 (call $node_get_in (local.get $n) (local.get $in1Idx)))))
|
|
2456
|
+
(local.set $out0 (call $node_get_out (local.get $n) (local.get $out0Idx)))
|
|
2457
|
+
(if (i32.gt_s (local.get $numOut) (i32.const 1))
|
|
2458
|
+
(then (local.set $out1 (call $node_get_out (local.get $n) (local.get $out1Idx)))))
|
|
2459
|
+
|
|
2460
|
+
(local.set $iter (i32.const 0))
|
|
2461
|
+
(block $brk
|
|
2462
|
+
(loop $lp
|
|
2463
|
+
(br_if $brk (i32.ge_s (local.get $iter) (i32.const 64)))
|
|
2464
|
+
|
|
2465
|
+
;; Cooperative yield check (ONLY for push outputs with downstream consumer!)
|
|
2466
|
+
(if (i32.and (i32.eqz (local.get $mode0))
|
|
2467
|
+
(i32.and
|
|
2468
|
+
(i32.ne (i32.load (i32.add (local.get $out0) (i32.const 20))) (i32.const 0))
|
|
2469
|
+
(i32.ge_s (i32.load (i32.add (local.get $out0) (i32.const 44))) (local.get $yThresh))))
|
|
2470
|
+
(then (return (i32.const 2))))
|
|
2471
|
+
(if (i32.gt_s (local.get $numOut) (i32.const 1))
|
|
2472
|
+
(then
|
|
2473
|
+
(if (i32.and (i32.eqz (local.get $mode1))
|
|
2474
|
+
(i32.and
|
|
2475
|
+
(i32.ne (i32.load (i32.add (local.get $out1) (i32.const 20))) (i32.const 0))
|
|
2476
|
+
(i32.ge_s (i32.load (i32.add (local.get $out1) (i32.const 44))) (local.get $yThresh))))
|
|
2477
|
+
(then (return (i32.const 2))))))
|
|
2478
|
+
|
|
2479
|
+
;; Check input 0 buffer
|
|
2480
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $in0) (i32.const 44))) (local.get $raw0))
|
|
2481
|
+
(then
|
|
2482
|
+
(if (i32.eq (i32.load (local.get $in0)) (i32.const 2)) (then (return (i32.const 4))))
|
|
2483
|
+
(if (i32.eq (i32.load (local.get $in0)) (i32.const 0))
|
|
2484
|
+
(then (global.set $g_need_pull (local.get $in0)) (return (i32.const 1))))
|
|
2485
|
+
(return (i32.const 3))))
|
|
2486
|
+
|
|
2487
|
+
;; Check input 1 buffer
|
|
2488
|
+
(if (i32.gt_s (local.get $numIn) (i32.const 1))
|
|
2489
|
+
(then
|
|
2490
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $in1) (i32.const 44))) (local.get $raw1))
|
|
2491
|
+
(then
|
|
2492
|
+
(if (i32.eq (i32.load (local.get $in1)) (i32.const 2)) (then (return (i32.const 4))))
|
|
2493
|
+
(if (i32.eq (i32.load (local.get $in1)) (i32.const 0))
|
|
2494
|
+
(then (global.set $g_need_pull (local.get $in1)) (return (i32.const 1))))
|
|
2495
|
+
(return (i32.const 3))))))
|
|
2496
|
+
|
|
2497
|
+
;; Build flat table index
|
|
2498
|
+
(local.set $idx (i32.load (i32.add (local.get $regsPtr) (i32.shl (local.get $regIdx) (i32.const 2)))))
|
|
2499
|
+
(local.set $idx
|
|
2500
|
+
(i32.or
|
|
2501
|
+
(i32.shl (local.get $idx) (local.get $eff0))
|
|
2502
|
+
(call $compress_bits (call $bd_peek_bits_at (local.get $in0) (i32.const 0) (local.get $raw0)) (local.get $raw0) (local.get $mask0))))
|
|
2503
|
+
(if (i32.gt_s (local.get $numIn) (i32.const 1))
|
|
2504
|
+
(then
|
|
2505
|
+
(local.set $idx
|
|
2506
|
+
(i32.or
|
|
2507
|
+
(i32.shl (local.get $idx) (local.get $eff1))
|
|
2508
|
+
(call $compress_bits (call $bd_peek_bits_at (local.get $in1) (i32.const 0) (local.get $raw1)) (local.get $raw1) (local.get $mask1))))))
|
|
2509
|
+
|
|
2510
|
+
(global.set $g_table_lookups (i32.add (global.get $g_table_lookups) (i32.const 1)))
|
|
2511
|
+
(if (i32.ge_u (local.get $idx) (local.get $numEntries)) (then (return (i32.const 3))))
|
|
2512
|
+
|
|
2513
|
+
(local.set $ent (i32.add (local.get $entriesPtr) (i32.shl (local.get $idx) (i32.const 4))))
|
|
2514
|
+
(local.set $kind (i32.load8_u (local.get $ent)))
|
|
2515
|
+
(if (i32.eq (local.get $kind) (i32.const 2)) (then (return (i32.const 3)))) ;; NEED_MORE -> short
|
|
2516
|
+
(if (i32.eq (local.get $kind) (i32.const 3)) (then (return (i32.const 4)))) ;; FAIL
|
|
2517
|
+
|
|
2518
|
+
;; Apply shifts
|
|
2519
|
+
(local.set $sh (i32.load8_u (i32.add (local.get $ent) (i32.const 2))))
|
|
2520
|
+
(if (i32.gt_s (local.get $sh) (i32.const 0)) (then (drop (call $bd_shift_bits (local.get $in0) (local.get $sh)))))
|
|
2521
|
+
(if (i32.gt_s (local.get $numIn) (i32.const 1))
|
|
2522
|
+
(then
|
|
2523
|
+
(local.set $sh (i32.load8_u (i32.add (local.get $ent) (i32.const 3))))
|
|
2524
|
+
(if (i32.gt_s (local.get $sh) (i32.const 0)) (then (drop (call $bd_shift_bits (local.get $in1) (local.get $sh)))))))
|
|
2525
|
+
|
|
2526
|
+
;; Apply emits
|
|
2527
|
+
(local.set $elen (i32.load8_u (i32.add (local.get $ent) (i32.const 4))))
|
|
2528
|
+
(if (i32.gt_s (local.get $elen) (i32.const 0))
|
|
2529
|
+
(then
|
|
2530
|
+
(local.set $eword (i32.load (i32.add (local.get $ent) (i32.const 8))))
|
|
2531
|
+
(if (i32.eqz (local.get $mode0))
|
|
2532
|
+
(then (call $bd_push_bits (local.get $out0) (local.get $eword) (local.get $elen)))
|
|
2533
|
+
(else (call $bd_unshift_bits (call $k_get_unshift_buf (local.get $n) (local.get $out0Idx)) (local.get $eword) (local.get $elen))))))
|
|
2534
|
+
|
|
2535
|
+
(if (i32.gt_s (local.get $numOut) (i32.const 1))
|
|
2536
|
+
(then
|
|
2537
|
+
(local.set $elen (i32.load8_u (i32.add (local.get $ent) (i32.const 5))))
|
|
2538
|
+
(if (i32.gt_s (local.get $elen) (i32.const 0))
|
|
2539
|
+
(then
|
|
2540
|
+
(local.set $eword (i32.load (i32.add (local.get $ent) (i32.const 12))))
|
|
2541
|
+
(if (i32.eqz (local.get $mode1))
|
|
2542
|
+
(then (call $bd_push_bits (local.get $out1) (local.get $eword) (local.get $elen)))
|
|
2543
|
+
(else (call $bd_unshift_bits (call $k_get_unshift_buf (local.get $n) (local.get $out1Idx)) (local.get $eword) (local.get $elen))))))))
|
|
2544
|
+
|
|
2545
|
+
;; Update reg or EXIT!
|
|
2546
|
+
(if (i32.eq (local.get $kind) (i32.const 1))
|
|
2547
|
+
(then
|
|
2548
|
+
(return (i32.add (i32.const 100) (i32.load8_u (i32.add (local.get $ent) (i32.const 1)))))))
|
|
2549
|
+
|
|
2550
|
+
(i32.store (i32.add (local.get $regsPtr) (i32.shl (local.get $regIdx) (i32.const 2)))
|
|
2551
|
+
(i32.load8_u (i32.add (local.get $ent) (i32.const 1))))
|
|
2552
|
+
|
|
2553
|
+
(if (call $quota_tick_step) (then (return (i32.const 0))))
|
|
2554
|
+
(local.set $iter (i32.add (local.get $iter) (i32.const 1)))
|
|
2555
|
+
(br $lp)
|
|
2556
|
+
)
|
|
2557
|
+
)
|
|
2558
|
+
(i32.const 0)
|
|
2559
|
+
)
|
|
2560
|
+
|
|
2561
|
+
;; Hot-burst `stack_table_step`:
|
|
2562
|
+
;; Returns: 0=continue, 1=needPull, 2=yielded, 3=short, 4=fail, >= 100 = exit
|
|
2563
|
+
(func $exec_stack_table_step
|
|
2564
|
+
(param $n i32) (param $meta i32) (param $regIdx i32) (param $inIdx i32) (param $stackIdx i32) (param $outIdx i32)
|
|
2565
|
+
(result i32)
|
|
2566
|
+
(local $ks i32)
|
|
2567
|
+
(local $regsPtr i32)
|
|
2568
|
+
(local $st i32)
|
|
2569
|
+
(local $inCh i32)
|
|
2570
|
+
(local $outCh i32)
|
|
2571
|
+
(local $entriesPtr i32)
|
|
2572
|
+
(local $numEntries i32)
|
|
2573
|
+
(local $wStream i32)
|
|
2574
|
+
(local $wStack i32)
|
|
2575
|
+
(local $stEnc i32)
|
|
2576
|
+
(local $stStride i32)
|
|
2577
|
+
(local $outMode i32)
|
|
2578
|
+
(local $yThresh i32)
|
|
2579
|
+
(local $iter i32)
|
|
2580
|
+
(local $q i32)
|
|
2581
|
+
(local $sBits i32)
|
|
2582
|
+
(local $stLen i32)
|
|
2583
|
+
(local $d i32)
|
|
2584
|
+
(local $stCode i32)
|
|
2585
|
+
(local $idx i32)
|
|
2586
|
+
(local $ent i32)
|
|
2587
|
+
(local $kind i32)
|
|
2588
|
+
(local $sh i32)
|
|
2589
|
+
(local $popN i32)
|
|
2590
|
+
(local $pushLen i32)
|
|
2591
|
+
(local $elen i32)
|
|
2592
|
+
|
|
2593
|
+
(local.set $ks (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2594
|
+
(local.set $regsPtr (i32.load (local.get $ks)))
|
|
2595
|
+
(local.set $yThresh (i32.load (i32.add (local.get $ks) (i32.const 28))))
|
|
2596
|
+
(local.set $st (i32.load (i32.add (i32.load (i32.add (local.get $ks) (i32.const 4))) (i32.shl (local.get $stackIdx) (i32.const 2)))))
|
|
2597
|
+
(local.set $inCh (call $node_get_in (local.get $n) (local.get $inIdx)))
|
|
2598
|
+
(local.set $outCh (call $node_get_out (local.get $n) (local.get $outIdx)))
|
|
2599
|
+
|
|
2600
|
+
(local.set $entriesPtr (i32.load (local.get $meta)))
|
|
2601
|
+
(local.set $numEntries (i32.load (i32.add (local.get $meta) (i32.const 4))))
|
|
2602
|
+
(if (i32.le_s (local.get $numEntries) (i32.const 0)) (then (return (i32.const 3))))
|
|
2603
|
+
(local.set $wStream (i32.load (i32.add (local.get $meta) (i32.const 8))))
|
|
2604
|
+
(local.set $wStack (i32.load (i32.add (local.get $meta) (i32.const 12))))
|
|
2605
|
+
(local.set $stEnc (i32.load (i32.add (local.get $meta) (i32.const 16))))
|
|
2606
|
+
(local.set $stStride (i32.load (i32.add (local.get $meta) (i32.const 20))))
|
|
2607
|
+
(local.set $outMode (i32.load (i32.add (local.get $meta) (i32.const 24))))
|
|
2608
|
+
|
|
2609
|
+
(local.set $iter (i32.const 0))
|
|
2610
|
+
(block $brk
|
|
2611
|
+
(loop $lp
|
|
2612
|
+
(br_if $brk (i32.ge_s (local.get $iter) (i32.const 64)))
|
|
2613
|
+
(if (i32.and (i32.eqz (local.get $outMode))
|
|
2614
|
+
(i32.and
|
|
2615
|
+
(i32.ne (i32.load (i32.add (local.get $outCh) (i32.const 20))) (i32.const 0))
|
|
2616
|
+
(i32.ge_s (i32.load (i32.add (local.get $outCh) (i32.const 44))) (local.get $yThresh))))
|
|
2617
|
+
(then (return (i32.const 2))))
|
|
2618
|
+
|
|
2619
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $inCh) (i32.const 44))) (local.get $wStream))
|
|
2620
|
+
(then
|
|
2621
|
+
(if (i32.eq (i32.load (local.get $inCh)) (i32.const 2)) (then (return (i32.const 4))))
|
|
2622
|
+
(if (i32.eq (i32.load (local.get $inCh)) (i32.const 0))
|
|
2623
|
+
(then (global.set $g_need_pull (local.get $inCh)) (return (i32.const 1))))
|
|
2624
|
+
(return (i32.const 3))))
|
|
2625
|
+
|
|
2626
|
+
(local.set $q (i32.load (i32.add (local.get $regsPtr) (i32.shl (local.get $regIdx) (i32.const 2)))))
|
|
2627
|
+
(local.set $sBits (call $bd_peek_bits_at (local.get $inCh) (i32.const 0) (local.get $wStream)))
|
|
2628
|
+
(local.set $stLen (i32.load (i32.add (local.get $st) (i32.const 8))))
|
|
2629
|
+
(local.set $d (select (local.get $wStack) (local.get $stLen) (i32.gt_s (local.get $stLen) (local.get $wStack))))
|
|
2630
|
+
|
|
2631
|
+
(if (i32.eq (local.get $stEnc) (i32.const 1))
|
|
2632
|
+
(then (local.set $stCode (local.get $d)))
|
|
2633
|
+
(else
|
|
2634
|
+
(if (i32.gt_s (local.get $d) (i32.const 0))
|
|
2635
|
+
(then
|
|
2636
|
+
(local.set $stCode
|
|
2637
|
+
(i32.add
|
|
2638
|
+
(i32.sub (i32.shl (i32.const 1) (local.get $d)) (i32.const 1))
|
|
2639
|
+
(call $bs_peek_top (local.get $st) (local.get $d)))))
|
|
2640
|
+
(else (local.set $stCode (i32.const 0))))))
|
|
2641
|
+
|
|
2642
|
+
(local.set $idx
|
|
2643
|
+
(i32.add
|
|
2644
|
+
(i32.mul (i32.or (i32.shl (local.get $q) (local.get $wStream)) (local.get $sBits)) (local.get $stStride))
|
|
2645
|
+
(local.get $stCode)))
|
|
2646
|
+
|
|
2647
|
+
(global.set $g_table_lookups (i32.add (global.get $g_table_lookups) (i32.const 1)))
|
|
2648
|
+
(if (i32.ge_u (local.get $idx) (local.get $numEntries)) (then (return (i32.const 3))))
|
|
2649
|
+
|
|
2650
|
+
(local.set $ent (i32.add (local.get $entriesPtr) (i32.shl (local.get $idx) (i32.const 4))))
|
|
2651
|
+
(local.set $kind (i32.load8_u (local.get $ent)))
|
|
2652
|
+
(if (i32.eq (local.get $kind) (i32.const 2)) (then (return (i32.const 3))))
|
|
2653
|
+
(if (i32.eq (local.get $kind) (i32.const 3)) (then (return (i32.const 4))))
|
|
2654
|
+
|
|
2655
|
+
(local.set $sh (i32.load8_u (i32.add (local.get $ent) (i32.const 2))))
|
|
2656
|
+
(if (i32.gt_s (local.get $sh) (i32.const 0)) (then (drop (call $bd_shift_bits (local.get $inCh) (local.get $sh)))))
|
|
2657
|
+
(local.set $popN (i32.load8_u (i32.add (local.get $ent) (i32.const 3))))
|
|
2658
|
+
(if (i32.gt_s (local.get $popN) (i32.const 0)) (then (drop (call $bs_pop (local.get $st) (local.get $popN)))))
|
|
2659
|
+
(local.set $pushLen (i32.load8_u (i32.add (local.get $ent) (i32.const 4))))
|
|
2660
|
+
(if (i32.gt_s (local.get $pushLen) (i32.const 0))
|
|
2661
|
+
(then (call $bs_push (local.get $st) (i32.load (i32.add (local.get $ent) (i32.const 8))) (local.get $pushLen))))
|
|
2662
|
+
(local.set $elen (i32.load8_u (i32.add (local.get $ent) (i32.const 5))))
|
|
2663
|
+
(if (i32.gt_s (local.get $elen) (i32.const 0))
|
|
2664
|
+
(then
|
|
2665
|
+
(if (i32.eqz (local.get $outMode))
|
|
2666
|
+
(then (call $bd_push_bits (local.get $outCh) (i32.load (i32.add (local.get $ent) (i32.const 12))) (local.get $elen)))
|
|
2667
|
+
(else (call $bd_unshift_bits (call $k_get_unshift_buf (local.get $n) (local.get $outIdx)) (i32.load (i32.add (local.get $ent) (i32.const 12))) (local.get $elen))))))
|
|
2668
|
+
|
|
2669
|
+
(if (i32.eq (local.get $kind) (i32.const 1))
|
|
2670
|
+
(then (return (i32.add (i32.const 100) (i32.load8_u (i32.add (local.get $ent) (i32.const 1)))))))
|
|
2671
|
+
|
|
2672
|
+
(i32.store (i32.add (local.get $regsPtr) (i32.shl (local.get $regIdx) (i32.const 2)))
|
|
2673
|
+
(i32.load8_u (i32.add (local.get $ent) (i32.const 1))))
|
|
2674
|
+
|
|
2675
|
+
(if (call $quota_tick_step) (then (return (i32.const 0))))
|
|
2676
|
+
(local.set $iter (i32.add (local.get $iter) (i32.const 1)))
|
|
2677
|
+
(br $lp)
|
|
2678
|
+
)
|
|
2679
|
+
)
|
|
2680
|
+
(i32.const 0)
|
|
2681
|
+
)
|
|
2682
|
+
|
|
2683
|
+
;; `fsm_table_step`:
|
|
2684
|
+
;; Returns: 1=needPull, 3=short, 4=fail, >= 100 = exit
|
|
2685
|
+
(func $exec_fsm_table_step
|
|
2686
|
+
(param $n i32) (param $meta i32) (param $inIdx i32) (param $out0Idx i32) (param $out1Idx i32)
|
|
2687
|
+
(result i32)
|
|
2688
|
+
(local $entriesPtr i32)
|
|
2689
|
+
(local $numEntries i32)
|
|
2690
|
+
(local $wRaw i32)
|
|
2691
|
+
(local $numOut i32)
|
|
2692
|
+
(local $mode0 i32)
|
|
2693
|
+
(local $mode1 i32)
|
|
2694
|
+
(local $inCh i32)
|
|
2695
|
+
(local $out0 i32)
|
|
2696
|
+
(local $out1 i32)
|
|
2697
|
+
(local $idx i32)
|
|
2698
|
+
(local $ent i32)
|
|
2699
|
+
(local $kind i32)
|
|
2700
|
+
(local $sh i32)
|
|
2701
|
+
(local $elen i32)
|
|
2702
|
+
|
|
2703
|
+
(local.set $entriesPtr (i32.load (local.get $meta)))
|
|
2704
|
+
(local.set $numEntries (i32.load (i32.add (local.get $meta) (i32.const 4))))
|
|
2705
|
+
(if (i32.le_s (local.get $numEntries) (i32.const 0)) (then (return (i32.const 3))))
|
|
2706
|
+
(local.set $wRaw (i32.load (i32.add (local.get $meta) (i32.const 8))))
|
|
2707
|
+
(local.set $numOut (i32.load (i32.add (local.get $meta) (i32.const 12))))
|
|
2708
|
+
(local.set $mode0 (i32.load (i32.add (local.get $meta) (i32.const 16))))
|
|
2709
|
+
(local.set $mode1 (i32.load (i32.add (local.get $meta) (i32.const 20))))
|
|
2710
|
+
|
|
2711
|
+
(local.set $inCh (call $node_get_in (local.get $n) (local.get $inIdx)))
|
|
2712
|
+
(local.set $out0 (call $node_get_out (local.get $n) (local.get $out0Idx)))
|
|
2713
|
+
(if (i32.gt_s (local.get $numOut) (i32.const 1))
|
|
2714
|
+
(then (local.set $out1 (call $node_get_out (local.get $n) (local.get $out1Idx)))))
|
|
2715
|
+
|
|
2716
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $inCh) (i32.const 44))) (local.get $wRaw))
|
|
2717
|
+
(then
|
|
2718
|
+
(if (i32.eq (i32.load (local.get $inCh)) (i32.const 2)) (then (return (i32.const 4))))
|
|
2719
|
+
(if (i32.eq (i32.load (local.get $inCh)) (i32.const 0))
|
|
2720
|
+
(then (global.set $g_need_pull (local.get $inCh)) (return (i32.const 1))))
|
|
2721
|
+
(return (i32.const 3))))
|
|
2722
|
+
|
|
2723
|
+
(local.set $idx (call $bd_peek_bits_at (local.get $inCh) (i32.const 0) (local.get $wRaw)))
|
|
2724
|
+
(global.set $g_table_lookups (i32.add (global.get $g_table_lookups) (i32.const 1)))
|
|
2725
|
+
(if (i32.ge_u (local.get $idx) (local.get $numEntries)) (then (return (i32.const 3))))
|
|
2726
|
+
(local.set $ent (i32.add (local.get $entriesPtr) (i32.shl (local.get $idx) (i32.const 4))))
|
|
2727
|
+
(local.set $kind (i32.load8_u (local.get $ent)))
|
|
2728
|
+
(if (i32.eq (local.get $kind) (i32.const 2)) (then (return (i32.const 3))))
|
|
2729
|
+
(if (i32.eq (local.get $kind) (i32.const 3)) (then (return (i32.const 4))))
|
|
2730
|
+
|
|
2731
|
+
(local.set $sh (i32.load8_u (i32.add (local.get $ent) (i32.const 2))))
|
|
2732
|
+
(if (i32.gt_s (local.get $sh) (i32.const 0)) (then (drop (call $bd_shift_bits (local.get $inCh) (local.get $sh)))))
|
|
2733
|
+
(local.set $elen (i32.load8_u (i32.add (local.get $ent) (i32.const 4))))
|
|
2734
|
+
(if (i32.gt_s (local.get $elen) (i32.const 0))
|
|
2735
|
+
(then
|
|
2736
|
+
(if (i32.eqz (local.get $mode0))
|
|
2737
|
+
(then (call $bd_push_bits (local.get $out0) (i32.load (i32.add (local.get $ent) (i32.const 8))) (local.get $elen)))
|
|
2738
|
+
(else (call $bd_unshift_bits (call $k_get_unshift_buf (local.get $n) (local.get $out0Idx)) (i32.load (i32.add (local.get $ent) (i32.const 8))) (local.get $elen))))))
|
|
2739
|
+
(if (i32.gt_s (local.get $numOut) (i32.const 1))
|
|
2740
|
+
(then
|
|
2741
|
+
(local.set $elen (i32.load8_u (i32.add (local.get $ent) (i32.const 5))))
|
|
2742
|
+
(if (i32.gt_s (local.get $elen) (i32.const 0))
|
|
2743
|
+
(then
|
|
2744
|
+
(if (i32.eqz (local.get $mode1))
|
|
2745
|
+
(then (call $bd_push_bits (local.get $out1) (i32.load (i32.add (local.get $ent) (i32.const 12))) (local.get $elen)))
|
|
2746
|
+
(else (call $bd_unshift_bits (call $k_get_unshift_buf (local.get $n) (local.get $out1Idx)) (i32.load (i32.add (local.get $ent) (i32.const 12))) (local.get $elen))))))))
|
|
2747
|
+
|
|
2748
|
+
(i32.add (i32.const 100) (i32.load8_u (i32.add (local.get $ent) (i32.const 1))))
|
|
2749
|
+
)
|
|
2750
|
+
|
|
2751
|
+
;; ============================================================================
|
|
2752
|
+
;; 9. Primitive Node Small-Step Transitions (100% Matching `vm.mjs`)
|
|
2753
|
+
;; ============================================================================
|
|
2754
|
+
;; Return convention: 0 = progress, 1 = needPull ($g_need_pull), 2 = yielded, 3 = needStepNode ($g_need_step_node)
|
|
2755
|
+
|
|
2756
|
+
(func $step_const_unit (param $n i32) (result i32)
|
|
2757
|
+
(local $out i32)
|
|
2758
|
+
(local.set $out (call $node_get_out (local.get $n) (i32.const 0)))
|
|
2759
|
+
(call $retire_node (local.get $n))
|
|
2760
|
+
(call $bd_close (local.get $out))
|
|
2761
|
+
(i32.const 0)
|
|
2762
|
+
)
|
|
2763
|
+
|
|
2764
|
+
(func $step_splice (param $n i32) (result i32)
|
|
2765
|
+
(local $src i32)
|
|
2766
|
+
(local $dst i32)
|
|
2767
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
2768
|
+
(local.set $dst (call $node_get_out (local.get $n) (i32.const 0)))
|
|
2769
|
+
(call $retire_node (local.get $n))
|
|
2770
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
2771
|
+
(i32.const 0)
|
|
2772
|
+
)
|
|
2773
|
+
|
|
2774
|
+
(func $step_coerce (param $n i32) (result i32)
|
|
2775
|
+
(local $src i32)
|
|
2776
|
+
(local $dst i32)
|
|
2777
|
+
(local $dpda i32)
|
|
2778
|
+
(local $res i32)
|
|
2779
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
2780
|
+
(local.set $dst (call $node_get_out (local.get $n) (i32.const 0)))
|
|
2781
|
+
(local.set $dpda (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
2782
|
+
(if (i32.eqz (local.get $dpda))
|
|
2783
|
+
(then
|
|
2784
|
+
(if (i32.and
|
|
2785
|
+
(i32.ne (i32.load (i32.add (local.get $src) (i32.const 12))) (i32.const 0))
|
|
2786
|
+
(i32.eq (i32.load (i32.add (local.get $src) (i32.const 8))) (i32.load (i32.add (local.get $dst) (i32.const 4)))))
|
|
2787
|
+
(then
|
|
2788
|
+
(call $retire_node (local.get $n))
|
|
2789
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
2790
|
+
(return (i32.const 0))))
|
|
2791
|
+
(local.set $dpda (call $dpda_alloc (i32.const 2) (i32.load (i32.add (local.get $src) (i32.const 8))) (i32.load (i32.add (local.get $dst) (i32.const 4)))))
|
|
2792
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (local.get $dpda))
|
|
2793
|
+
)
|
|
2794
|
+
)
|
|
2795
|
+
(local.set $res (call $dpda_step_coerce (local.get $src) (local.get $dst) (local.get $dpda)))
|
|
2796
|
+
(if (i32.eq (local.get $res) (i32.const 1))
|
|
2797
|
+
(then
|
|
2798
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
2799
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
2800
|
+
(call $retire_node (local.get $n))
|
|
2801
|
+
(call $bd_fail (local.get $dst))
|
|
2802
|
+
(call $cancel_input (local.get $src))
|
|
2803
|
+
(return (i32.const 0))))
|
|
2804
|
+
(if (i32.eq (local.get $res) (i32.const 2))
|
|
2805
|
+
(then
|
|
2806
|
+
(call $retire_node (local.get $n))
|
|
2807
|
+
(call $bd_fail (local.get $dst))
|
|
2808
|
+
(call $cancel_input (local.get $src))
|
|
2809
|
+
(return (i32.const 0))))
|
|
2810
|
+
;; DONE: wait for src CLOSED with 0 leftover bits
|
|
2811
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
2812
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
2813
|
+
(if (i32.or (i32.eq (i32.load (local.get $src)) (i32.const 2)) (i32.gt_s (i32.load (i32.add (local.get $src) (i32.const 44))) (i32.const 0)))
|
|
2814
|
+
(then
|
|
2815
|
+
(call $retire_node (local.get $n))
|
|
2816
|
+
(call $bd_fail (local.get $dst))
|
|
2817
|
+
(call $cancel_input (local.get $src))
|
|
2818
|
+
(return (i32.const 0))))
|
|
2819
|
+
(call $retire_node (local.get $n))
|
|
2820
|
+
(call $bd_release (local.get $src))
|
|
2821
|
+
(call $bd_close (local.get $dst))
|
|
2822
|
+
(i32.const 0)
|
|
2823
|
+
)
|
|
2824
|
+
|
|
2825
|
+
(func $step_wrap (param $n i32) (result i32)
|
|
2826
|
+
(local $src i32)
|
|
2827
|
+
(local $dst i32)
|
|
2828
|
+
(local $tagId i32)
|
|
2829
|
+
(local $dSid i32)
|
|
2830
|
+
(local $ord i32)
|
|
2831
|
+
(local $w i32)
|
|
2832
|
+
(local $targetSid i32)
|
|
2833
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
2834
|
+
(local.set $dst (call $node_get_out (local.get $n) (i32.const 0)))
|
|
2835
|
+
(local.set $tagId (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
2836
|
+
(local.set $dSid (i32.load (i32.add (local.get $dst) (i32.const 4))))
|
|
2837
|
+
(local.set $ord (call $schema_find_tag (local.get $dSid) (local.get $tagId)))
|
|
2838
|
+
(if (i32.lt_s (local.get $ord) (i32.const 0))
|
|
2839
|
+
(then
|
|
2840
|
+
(call $retire_node (local.get $n))
|
|
2841
|
+
(call $bd_fail (local.get $dst))
|
|
2842
|
+
(call $cancel_input (local.get $src))
|
|
2843
|
+
(return (i32.const 0))))
|
|
2844
|
+
(local.set $w (call $schema_width (local.get $dSid)))
|
|
2845
|
+
(if (i32.gt_s (local.get $w) (i32.const 0))
|
|
2846
|
+
(then (call $bd_push_bits (local.get $dst) (local.get $ord) (local.get $w))))
|
|
2847
|
+
(local.set $targetSid (call $schema_edge_target (local.get $dSid) (local.get $ord)))
|
|
2848
|
+
(i32.store (i32.add (local.get $dst) (i32.const 4)) (local.get $targetSid))
|
|
2849
|
+
|
|
2850
|
+
(if (i32.and
|
|
2851
|
+
(i32.ne (i32.load (i32.add (local.get $src) (i32.const 12))) (i32.const 0))
|
|
2852
|
+
(i32.eq (i32.load (i32.add (local.get $src) (i32.const 8))) (local.get $targetSid)))
|
|
2853
|
+
(then
|
|
2854
|
+
(call $retire_node (local.get $n))
|
|
2855
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
2856
|
+
(return (i32.const 0))))
|
|
2857
|
+
;; Degrade in-place to COERCE
|
|
2858
|
+
(i32.store (local.get $n) (i32.const 3))
|
|
2859
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
2860
|
+
(i32.const 0)
|
|
2861
|
+
)
|
|
2862
|
+
|
|
2863
|
+
(func $step_match (param $n i32) (result i32)
|
|
2864
|
+
(local $src i32)
|
|
2865
|
+
(local $dst i32)
|
|
2866
|
+
(local $tagId i32)
|
|
2867
|
+
(local $sSid i32)
|
|
2868
|
+
(local $ord i32)
|
|
2869
|
+
(local $w i32)
|
|
2870
|
+
(local $got i32)
|
|
2871
|
+
(local $targetSid i32)
|
|
2872
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
2873
|
+
(local.set $dst (call $node_get_out (local.get $n) (i32.const 0)))
|
|
2874
|
+
(local.set $tagId (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
2875
|
+
(local.set $sSid (i32.load (i32.add (local.get $src) (i32.const 8))))
|
|
2876
|
+
(local.set $ord (call $schema_find_tag (local.get $sSid) (local.get $tagId)))
|
|
2877
|
+
(if (i32.lt_s (local.get $ord) (i32.const 0))
|
|
2878
|
+
(then
|
|
2879
|
+
(call $retire_node (local.get $n))
|
|
2880
|
+
(call $bd_fail (local.get $dst))
|
|
2881
|
+
(call $cancel_input (local.get $src))
|
|
2882
|
+
(return (i32.const 0))))
|
|
2883
|
+
(local.set $w (call $schema_width (local.get $sSid)))
|
|
2884
|
+
(if (i32.gt_s (local.get $w) (i32.const 0))
|
|
2885
|
+
(then
|
|
2886
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $src) (i32.const 44))) (local.get $w))
|
|
2887
|
+
(then
|
|
2888
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
2889
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
2890
|
+
(call $retire_node (local.get $n))
|
|
2891
|
+
(call $bd_fail (local.get $dst))
|
|
2892
|
+
(call $cancel_input (local.get $src))
|
|
2893
|
+
(return (i32.const 0))))
|
|
2894
|
+
(local.set $got (call $bd_peek_bits_at (local.get $src) (i32.const 0) (local.get $w)))
|
|
2895
|
+
(if (i32.ne (local.get $got) (local.get $ord))
|
|
2896
|
+
(then
|
|
2897
|
+
(call $retire_node (local.get $n))
|
|
2898
|
+
(call $bd_fail (local.get $dst))
|
|
2899
|
+
(call $cancel_input (local.get $src))
|
|
2900
|
+
(return (i32.const 0))))
|
|
2901
|
+
(drop (call $bd_shift_bits (local.get $src) (local.get $w)))
|
|
2902
|
+
)
|
|
2903
|
+
)
|
|
2904
|
+
(local.set $targetSid (call $schema_edge_target (local.get $sSid) (local.get $ord)))
|
|
2905
|
+
(i32.store (i32.add (local.get $src) (i32.const 8)) (local.get $targetSid))
|
|
2906
|
+
(i32.store (i32.add (local.get $src) (i32.const 12)) (i32.const 1))
|
|
2907
|
+
|
|
2908
|
+
(if (i32.eq (local.get $targetSid) (i32.load (i32.add (local.get $dst) (i32.const 4))))
|
|
2909
|
+
(then
|
|
2910
|
+
(call $retire_node (local.get $n))
|
|
2911
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
2912
|
+
(return (i32.const 0))))
|
|
2913
|
+
;; Degrade in-place to COERCE
|
|
2914
|
+
(i32.store (local.get $n) (i32.const 3))
|
|
2915
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
2916
|
+
(i32.const 0)
|
|
2917
|
+
)
|
|
2918
|
+
|
|
2919
|
+
(func $fail_remaining_demux (param $n i32) (param $fromIdx i32)
|
|
2920
|
+
(local $src i32)
|
|
2921
|
+
(local $cnt i32)
|
|
2922
|
+
(local $j i32)
|
|
2923
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
2924
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
2925
|
+
(call $retire_node (local.get $n))
|
|
2926
|
+
(local.set $j (local.get $fromIdx))
|
|
2927
|
+
(block $brk
|
|
2928
|
+
(loop $lp
|
|
2929
|
+
(br_if $brk (i32.ge_s (local.get $j) (local.get $cnt)))
|
|
2930
|
+
(call $bd_fail (call $node_get_out (local.get $n) (local.get $j)))
|
|
2931
|
+
(local.set $j (i32.add (local.get $j) (i32.const 1)))
|
|
2932
|
+
(br $lp)
|
|
2933
|
+
)
|
|
2934
|
+
)
|
|
2935
|
+
(call $cancel_input (local.get $src))
|
|
2936
|
+
)
|
|
2937
|
+
|
|
2938
|
+
(func $step_demux (param $n i32) (result i32)
|
|
2939
|
+
(local $src i32)
|
|
2940
|
+
(local $sid i32)
|
|
2941
|
+
(local $fIdx i32)
|
|
2942
|
+
(local $nFields i32)
|
|
2943
|
+
(local $muxNode i32)
|
|
2944
|
+
(local $i i32)
|
|
2945
|
+
(local $dst i32)
|
|
2946
|
+
(local $dpda i32)
|
|
2947
|
+
(local $res i32)
|
|
2948
|
+
|
|
2949
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
2950
|
+
(local.set $sid (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
2951
|
+
(local.set $fIdx (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
2952
|
+
(local.set $nFields (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
2953
|
+
|
|
2954
|
+
;; Same-scope MUX -> DEMUX O(1) bundle fusion!
|
|
2955
|
+
(if (i32.and (i32.eqz (local.get $fIdx))
|
|
2956
|
+
(i32.and (i32.eqz (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
2957
|
+
(i32.ne (i32.load (i32.add (local.get $src) (i32.const 12))) (i32.const 0))))
|
|
2958
|
+
(then
|
|
2959
|
+
(local.set $muxNode (i32.load (i32.add (local.get $src) (i32.const 16))))
|
|
2960
|
+
(if (i32.and (i32.ne (local.get $muxNode) (i32.const 0))
|
|
2961
|
+
(i32.and (i32.eq (i32.load (local.get $muxNode)) (i32.const 7))
|
|
2962
|
+
(i32.and (i32.eq (i32.load (i32.add (local.get $muxNode) (i32.const 8))) (i32.load (i32.add (local.get $n) (i32.const 8))))
|
|
2963
|
+
(i32.and (i32.eqz (i32.load (i32.add (local.get $muxNode) (i32.const 36))))
|
|
2964
|
+
(i32.and
|
|
2965
|
+
(i32.eq (i32.load (i32.add (local.get $src) (i32.const 8))) (i32.load (i32.add (local.get $muxNode) (i32.const 32))))
|
|
2966
|
+
(i32.eq (local.get $sid) (i32.load (i32.add (local.get $muxNode) (i32.const 32)))))))))
|
|
2967
|
+
(then
|
|
2968
|
+
(if (i32.or (i32.eqz (local.get $nFields))
|
|
2969
|
+
(i32.eqz (i32.load (i32.add (call $node_get_out (local.get $n) (i32.const 0)) (i32.const 44)))))
|
|
2970
|
+
(then
|
|
2971
|
+
(call $retire_node (local.get $muxNode))
|
|
2972
|
+
(call $retire_node (local.get $n))
|
|
2973
|
+
(i32.store (i32.add (local.get $src) (i32.const 16)) (i32.const 0))
|
|
2974
|
+
(i32.store (i32.add (local.get $src) (i32.const 20)) (i32.const 0))
|
|
2975
|
+
(call $bd_release (local.get $src))
|
|
2976
|
+
(local.set $i (i32.const 0))
|
|
2977
|
+
(block $bf
|
|
2978
|
+
(loop $lf
|
|
2979
|
+
(br_if $bf (i32.ge_s (local.get $i) (local.get $nFields)))
|
|
2980
|
+
(call $splice (call $node_get_in (local.get $muxNode) (local.get $i)) (call $node_get_out (local.get $n) (local.get $i)))
|
|
2981
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
2982
|
+
(br $lf)
|
|
2983
|
+
)
|
|
2984
|
+
)
|
|
2985
|
+
(global.set $g_mux_demux_fusions (i32.add (global.get $g_mux_demux_fusions) (i32.const 1)))
|
|
2986
|
+
(return (i32.const 0))
|
|
2987
|
+
)
|
|
2988
|
+
)
|
|
2989
|
+
)
|
|
2990
|
+
)
|
|
2991
|
+
)
|
|
2992
|
+
)
|
|
2993
|
+
|
|
2994
|
+
(if (i32.eqz (local.get $nFields))
|
|
2995
|
+
(then
|
|
2996
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
2997
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
2998
|
+
(call $retire_node (local.get $n))
|
|
2999
|
+
(call $bd_release (local.get $src))
|
|
3000
|
+
(return (i32.const 0))))
|
|
3001
|
+
|
|
3002
|
+
(if (i32.eq (local.get $nFields) (i32.const 1))
|
|
3003
|
+
(then
|
|
3004
|
+
(i32.store (i32.add (local.get $src) (i32.const 8)) (call $schema_edge_target (local.get $sid) (i32.const 0)))
|
|
3005
|
+
(i32.store (i32.add (local.get $src) (i32.const 12)) (i32.const 1))
|
|
3006
|
+
(local.set $dst (call $node_get_out (local.get $n) (i32.const 0)))
|
|
3007
|
+
(call $retire_node (local.get $n))
|
|
3008
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
3009
|
+
(return (i32.const 0))))
|
|
3010
|
+
|
|
3011
|
+
;; Copy fields 0 .. nFields - 2
|
|
3012
|
+
(block $bm
|
|
3013
|
+
(loop $lm
|
|
3014
|
+
(local.set $fIdx (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3015
|
+
(br_if $bm (i32.ge_s (local.get $fIdx) (i32.sub (local.get $nFields) (i32.const 1))))
|
|
3016
|
+
(local.set $dst (call $node_get_out (local.get $n) (local.get $fIdx)))
|
|
3017
|
+
(local.set $dpda (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
3018
|
+
(if (i32.eqz (local.get $dpda))
|
|
3019
|
+
(then
|
|
3020
|
+
(local.set $dpda (call $dpda_alloc (i32.const 1) (call $schema_edge_target (local.get $sid) (local.get $fIdx)) (i32.const 0)))
|
|
3021
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (local.get $dpda))
|
|
3022
|
+
)
|
|
3023
|
+
)
|
|
3024
|
+
(local.set $res (call $dpda_step_skip_copy (local.get $src) (local.get $dst) (local.get $dpda)))
|
|
3025
|
+
(if (i32.eq (local.get $res) (i32.const 1))
|
|
3026
|
+
(then
|
|
3027
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
3028
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
3029
|
+
(call $fail_remaining_demux (local.get $n) (local.get $fIdx))
|
|
3030
|
+
(return (i32.const 0))))
|
|
3031
|
+
(if (i32.eq (local.get $res) (i32.const 2))
|
|
3032
|
+
(then
|
|
3033
|
+
(call $fail_remaining_demux (local.get $n) (local.get $fIdx))
|
|
3034
|
+
(return (i32.const 0))))
|
|
3035
|
+
(call $bd_close (local.get $dst))
|
|
3036
|
+
(call $dpda_free (local.get $dpda))
|
|
3037
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
3038
|
+
(i32.store (i32.add (local.get $n) (i32.const 36)) (i32.add (local.get $fIdx) (i32.const 1)))
|
|
3039
|
+
(br $lm)
|
|
3040
|
+
)
|
|
3041
|
+
)
|
|
3042
|
+
|
|
3043
|
+
;; Final field nFields - 1: O(1) tail splice!
|
|
3044
|
+
(local.set $fIdx (i32.sub (local.get $nFields) (i32.const 1)))
|
|
3045
|
+
(i32.store (i32.add (local.get $src) (i32.const 8)) (call $schema_edge_target (local.get $sid) (local.get $fIdx)))
|
|
3046
|
+
(i32.store (i32.add (local.get $src) (i32.const 12)) (i32.const 1))
|
|
3047
|
+
(local.set $dst (call $node_get_out (local.get $n) (local.get $fIdx)))
|
|
3048
|
+
(call $retire_node (local.get $n))
|
|
3049
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
3050
|
+
(i32.const 0)
|
|
3051
|
+
)
|
|
3052
|
+
|
|
3053
|
+
(func $step_mux (param $n i32) (result i32)
|
|
3054
|
+
(local $dst i32)
|
|
3055
|
+
(local $sid i32)
|
|
3056
|
+
(local $fIdx i32)
|
|
3057
|
+
(local $nFields i32)
|
|
3058
|
+
(local $demuxNode i32)
|
|
3059
|
+
(local $i i32)
|
|
3060
|
+
(local $j i32)
|
|
3061
|
+
(local $src i32)
|
|
3062
|
+
|
|
3063
|
+
(local.set $dst (call $node_get_out (local.get $n) (i32.const 0)))
|
|
3064
|
+
(local.set $sid (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
3065
|
+
(local.set $fIdx (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3066
|
+
(local.set $nFields (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
3067
|
+
|
|
3068
|
+
;; Zero-copy strict symmetric MUX -> DEMUX fusion:
|
|
3069
|
+
;; Wait for non-tail fields 0..nFields-2 to close in-place (preventing O(N^1.585)
|
|
3070
|
+
;; lazy thunk pileup), then O(1) splice every field directly without DPDA bit-copying!
|
|
3071
|
+
(if (i32.and (i32.eqz (local.get $fIdx))
|
|
3072
|
+
(i32.and (i32.eqz (i32.load (i32.add (local.get $dst) (i32.const 44))))
|
|
3073
|
+
(i32.ne (i32.load (i32.add (local.get $dst) (i32.const 12))) (i32.const 0))))
|
|
3074
|
+
(then
|
|
3075
|
+
(local.set $demuxNode (i32.load (i32.add (local.get $dst) (i32.const 20))))
|
|
3076
|
+
(if (i32.and (i32.ne (local.get $demuxNode) (i32.const 0))
|
|
3077
|
+
(i32.and (i32.eq (i32.load (local.get $demuxNode)) (i32.const 6))
|
|
3078
|
+
(i32.and (i32.eq (i32.load (i32.add (local.get $demuxNode) (i32.const 8))) (i32.load (i32.add (local.get $n) (i32.const 8))))
|
|
3079
|
+
(i32.and (i32.eqz (i32.load (i32.add (local.get $demuxNode) (i32.const 36))))
|
|
3080
|
+
(i32.and
|
|
3081
|
+
(i32.eq (i32.load (i32.add (local.get $dst) (i32.const 8))) (local.get $sid))
|
|
3082
|
+
(i32.eq (i32.load (i32.add (local.get $demuxNode) (i32.const 32))) (local.get $sid)))))))
|
|
3083
|
+
(then
|
|
3084
|
+
(if (i32.or (i32.eqz (local.get $nFields))
|
|
3085
|
+
(i32.eqz (i32.load (i32.add (call $node_get_out (local.get $demuxNode) (i32.const 0)) (i32.const 44)))))
|
|
3086
|
+
(then
|
|
3087
|
+
;; Ensure non-tail fields 0..nFields-2 are settled (CLOSED) before zero-copy splice
|
|
3088
|
+
(local.set $i (i32.const 0))
|
|
3089
|
+
(block $bfchk
|
|
3090
|
+
(loop $lfchk
|
|
3091
|
+
(br_if $bfchk (i32.ge_s (local.get $i) (i32.sub (local.get $nFields) (i32.const 1))))
|
|
3092
|
+
(local.set $src (call $node_get_in (local.get $n) (local.get $i)))
|
|
3093
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
3094
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
3095
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 2))
|
|
3096
|
+
(then
|
|
3097
|
+
(call $retire_node (local.get $n))
|
|
3098
|
+
(call $bd_fail (local.get $dst))
|
|
3099
|
+
(local.set $j (i32.const 0))
|
|
3100
|
+
(block $bcf (loop $lcf (br_if $bcf (i32.ge_s (local.get $j) (local.get $nFields)))
|
|
3101
|
+
(call $cancel_input (call $node_get_in (local.get $n) (local.get $j)))
|
|
3102
|
+
(local.set $j (i32.add (local.get $j) (i32.const 1))) (br $lcf)))
|
|
3103
|
+
(return (i32.const 0))))
|
|
3104
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3105
|
+
(br $lfchk)
|
|
3106
|
+
)
|
|
3107
|
+
)
|
|
3108
|
+
(call $retire_node (local.get $n))
|
|
3109
|
+
(call $retire_node (local.get $demuxNode))
|
|
3110
|
+
(i32.store (i32.add (local.get $dst) (i32.const 16)) (i32.const 0))
|
|
3111
|
+
(i32.store (i32.add (local.get $dst) (i32.const 20)) (i32.const 0))
|
|
3112
|
+
(call $bd_release (local.get $dst))
|
|
3113
|
+
(local.set $i (i32.const 0))
|
|
3114
|
+
(block $bf
|
|
3115
|
+
(loop $lf
|
|
3116
|
+
(br_if $bf (i32.ge_s (local.get $i) (local.get $nFields)))
|
|
3117
|
+
(call $splice (call $node_get_in (local.get $n) (local.get $i)) (call $node_get_out (local.get $demuxNode) (local.get $i)))
|
|
3118
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3119
|
+
(br $lf)
|
|
3120
|
+
)
|
|
3121
|
+
)
|
|
3122
|
+
(global.set $g_mux_demux_fusions (i32.add (global.get $g_mux_demux_fusions) (i32.const 1)))
|
|
3123
|
+
(return (i32.const 0))
|
|
3124
|
+
)
|
|
3125
|
+
)
|
|
3126
|
+
)
|
|
3127
|
+
)
|
|
3128
|
+
)
|
|
3129
|
+
)
|
|
3130
|
+
|
|
3131
|
+
(if (i32.eqz (local.get $nFields))
|
|
3132
|
+
(then
|
|
3133
|
+
(call $retire_node (local.get $n))
|
|
3134
|
+
(call $bd_close (local.get $dst))
|
|
3135
|
+
(return (i32.const 0))))
|
|
3136
|
+
|
|
3137
|
+
(if (i32.eq (local.get $nFields) (i32.const 1))
|
|
3138
|
+
(then
|
|
3139
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
3140
|
+
(i32.store (i32.add (local.get $dst) (i32.const 4)) (call $schema_edge_target (local.get $sid) (i32.const 0)))
|
|
3141
|
+
(call $retire_node (local.get $n))
|
|
3142
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
3143
|
+
(return (i32.const 0))))
|
|
3144
|
+
|
|
3145
|
+
(block $bm
|
|
3146
|
+
(loop $lm
|
|
3147
|
+
(local.set $fIdx (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3148
|
+
(br_if $bm (i32.ge_s (local.get $fIdx) (i32.sub (local.get $nFields) (i32.const 1))))
|
|
3149
|
+
(local.set $src (call $node_get_in (local.get $n) (local.get $fIdx)))
|
|
3150
|
+
(if (i32.gt_s (i32.load (i32.add (local.get $src) (i32.const 44))) (i32.const 0))
|
|
3151
|
+
(then (call $bd_push_from_deque (local.get $dst) (local.get $src))))
|
|
3152
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 2))
|
|
3153
|
+
(then
|
|
3154
|
+
(call $retire_node (local.get $n))
|
|
3155
|
+
(call $bd_fail (local.get $dst))
|
|
3156
|
+
(local.set $j (local.get $fIdx))
|
|
3157
|
+
(block $bc
|
|
3158
|
+
(loop $lc
|
|
3159
|
+
(br_if $bc (i32.ge_s (local.get $j) (local.get $nFields)))
|
|
3160
|
+
(call $cancel_input (call $node_get_in (local.get $n) (local.get $j)))
|
|
3161
|
+
(local.set $j (i32.add (local.get $j) (i32.const 1)))
|
|
3162
|
+
(br $lc)
|
|
3163
|
+
)
|
|
3164
|
+
)
|
|
3165
|
+
(return (i32.const 0))))
|
|
3166
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
3167
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
3168
|
+
(i32.store (i32.add (local.get $src) (i32.const 20)) (i32.const 0))
|
|
3169
|
+
(call $bd_release (local.get $src))
|
|
3170
|
+
(i32.store (i32.add (local.get $n) (i32.const 36)) (i32.add (local.get $fIdx) (i32.const 1)))
|
|
3171
|
+
(br $lm)
|
|
3172
|
+
)
|
|
3173
|
+
)
|
|
3174
|
+
|
|
3175
|
+
;; Final field: O(1) tail splice!
|
|
3176
|
+
(local.set $fIdx (i32.sub (local.get $nFields) (i32.const 1)))
|
|
3177
|
+
(local.set $src (call $node_get_in (local.get $n) (local.get $fIdx)))
|
|
3178
|
+
(i32.store (i32.add (local.get $dst) (i32.const 4)) (call $schema_edge_target (local.get $sid) (local.get $fIdx)))
|
|
3179
|
+
(call $retire_node (local.get $n))
|
|
3180
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
3181
|
+
(i32.const 0)
|
|
3182
|
+
)
|
|
3183
|
+
|
|
3184
|
+
(func $step_tee (param $n i32) (result i32)
|
|
3185
|
+
(local $src i32)
|
|
3186
|
+
(local $cnt i32)
|
|
3187
|
+
(local $i i32)
|
|
3188
|
+
(local $o i32)
|
|
3189
|
+
(local $liveCnt i32)
|
|
3190
|
+
(local $only i32)
|
|
3191
|
+
(local $burst i32)
|
|
3192
|
+
(local $srcLen i32)
|
|
3193
|
+
(local $chunk i32)
|
|
3194
|
+
(local $bits i32)
|
|
3195
|
+
|
|
3196
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
3197
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
3198
|
+
(local.set $liveCnt (i32.const 0))
|
|
3199
|
+
(local.set $only (i32.const 0))
|
|
3200
|
+
(local.set $i (i32.const 0))
|
|
3201
|
+
(block $bl
|
|
3202
|
+
(loop $ll
|
|
3203
|
+
(br_if $bl (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
3204
|
+
(local.set $o (call $node_get_out (local.get $n) (local.get $i)))
|
|
3205
|
+
(if (i32.and (i32.ne (local.get $o) (i32.const 0)) (i32.eq (i32.load (local.get $o)) (i32.const 0)))
|
|
3206
|
+
(then
|
|
3207
|
+
(local.set $liveCnt (i32.add (local.get $liveCnt) (i32.const 1)))
|
|
3208
|
+
(local.set $only (local.get $o))))
|
|
3209
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3210
|
+
(br $ll)
|
|
3211
|
+
)
|
|
3212
|
+
)
|
|
3213
|
+
|
|
3214
|
+
(if (i32.eqz (local.get $liveCnt))
|
|
3215
|
+
(then
|
|
3216
|
+
(call $retire_node (local.get $n))
|
|
3217
|
+
(call $cancel_input (local.get $src))
|
|
3218
|
+
(return (i32.const 0))))
|
|
3219
|
+
|
|
3220
|
+
;; Single-survivor O(1) splice degradation
|
|
3221
|
+
(if (i32.and (i32.eq (local.get $liveCnt) (i32.const 1))
|
|
3222
|
+
(i32.and (i32.ne (i32.load (i32.add (local.get $src) (i32.const 12))) (i32.const 0))
|
|
3223
|
+
(i32.eq (i32.load (i32.add (local.get $src) (i32.const 8))) (i32.load (i32.add (local.get $only) (i32.const 4))))))
|
|
3224
|
+
(then
|
|
3225
|
+
(call $retire_node (local.get $n))
|
|
3226
|
+
(call $splice (local.get $src) (local.get $only))
|
|
3227
|
+
(return (i32.const 0))))
|
|
3228
|
+
|
|
3229
|
+
(local.set $srcLen (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
3230
|
+
(if (i32.gt_s (local.get $srcLen) (i32.const 0))
|
|
3231
|
+
(then
|
|
3232
|
+
(local.set $burst (i32.const 0))
|
|
3233
|
+
(block $bb
|
|
3234
|
+
(loop $lb
|
|
3235
|
+
(local.set $srcLen (i32.load (i32.add (local.get $src) (i32.const 44))))
|
|
3236
|
+
(br_if $bb (i32.or (i32.le_s (local.get $srcLen) (i32.const 0)) (i32.ge_s (local.get $burst) (i32.const 4096))))
|
|
3237
|
+
(local.set $chunk (select (i32.const 30) (local.get $srcLen) (i32.gt_s (local.get $srcLen) (i32.const 30))))
|
|
3238
|
+
(local.set $bits (call $bd_shift_bits (local.get $src) (local.get $chunk)))
|
|
3239
|
+
(local.set $i (i32.const 0))
|
|
3240
|
+
(block $bp
|
|
3241
|
+
(loop $lp
|
|
3242
|
+
(br_if $bp (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
3243
|
+
(local.set $o (call $node_get_out (local.get $n) (local.get $i)))
|
|
3244
|
+
(if (i32.and (i32.ne (local.get $o) (i32.const 0)) (i32.eq (i32.load (local.get $o)) (i32.const 0)))
|
|
3245
|
+
(then (call $bd_push_bits (local.get $o) (local.get $bits) (local.get $chunk))))
|
|
3246
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3247
|
+
(br $lp)
|
|
3248
|
+
)
|
|
3249
|
+
)
|
|
3250
|
+
(local.set $burst (i32.add (local.get $burst) (local.get $chunk)))
|
|
3251
|
+
(br $lb)
|
|
3252
|
+
)
|
|
3253
|
+
)
|
|
3254
|
+
(if (i32.gt_s (i32.load (i32.add (local.get $src) (i32.const 44))) (i32.const 0))
|
|
3255
|
+
(then (return (i32.const 0))))
|
|
3256
|
+
)
|
|
3257
|
+
)
|
|
3258
|
+
|
|
3259
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
3260
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
3261
|
+
|
|
3262
|
+
(local.set $i (i32.load (local.get $src))) ;; 1=CLOSED, 2=FAILED
|
|
3263
|
+
(call $retire_node (local.get $n))
|
|
3264
|
+
(call $bd_release (local.get $src))
|
|
3265
|
+
(local.set $burst (i32.const 0))
|
|
3266
|
+
(block $bf
|
|
3267
|
+
(loop $lf
|
|
3268
|
+
(br_if $bf (i32.ge_s (local.get $burst) (local.get $cnt)))
|
|
3269
|
+
(local.set $o (call $node_get_out (local.get $n) (local.get $burst)))
|
|
3270
|
+
(if (i32.and (i32.ne (local.get $o) (i32.const 0)) (i32.eq (i32.load (local.get $o)) (i32.const 0)))
|
|
3271
|
+
(then
|
|
3272
|
+
(if (i32.eq (local.get $i) (i32.const 1))
|
|
3273
|
+
(then (call $bd_close (local.get $o)))
|
|
3274
|
+
(else (call $bd_fail (local.get $o))))))
|
|
3275
|
+
(local.set $burst (i32.add (local.get $burst) (i32.const 1)))
|
|
3276
|
+
(br $lf)
|
|
3277
|
+
)
|
|
3278
|
+
)
|
|
3279
|
+
(i32.const 0)
|
|
3280
|
+
)
|
|
3281
|
+
|
|
3282
|
+
(func $fail_barrier_drain (param $n i32)
|
|
3283
|
+
(local $src i32)
|
|
3284
|
+
(local $bar i32)
|
|
3285
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
3286
|
+
(local.set $bar (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3287
|
+
(call $retire_node (local.get $n))
|
|
3288
|
+
(call $cancel_input (local.get $src))
|
|
3289
|
+
(if (i32.ne (local.get $bar) (i32.const 0))
|
|
3290
|
+
(then
|
|
3291
|
+
(i32.store (i32.add (local.get $bar) (i32.const 4)) (i32.sub (i32.load (i32.add (local.get $bar) (i32.const 4))) (i32.const 1)))
|
|
3292
|
+
(i32.store (local.get $bar) (i32.const 2)) ;; FAILED
|
|
3293
|
+
)
|
|
3294
|
+
)
|
|
3295
|
+
)
|
|
3296
|
+
|
|
3297
|
+
(func $step_drain (param $n i32) (result i32)
|
|
3298
|
+
(local $src i32)
|
|
3299
|
+
(local $bar i32)
|
|
3300
|
+
(local $dpda i32)
|
|
3301
|
+
(local $res i32)
|
|
3302
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
3303
|
+
(local.set $bar (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3304
|
+
(local.set $dpda (i32.load (i32.add (local.get $n) (i32.const 44))))
|
|
3305
|
+
|
|
3306
|
+
(if (i32.eqz (i32.load (i32.add (local.get $n) (i32.const 48))))
|
|
3307
|
+
(then
|
|
3308
|
+
(local.set $res (call $dpda_step_skip_copy (local.get $src) (i32.const 0) (local.get $dpda)))
|
|
3309
|
+
(if (i32.eq (local.get $res) (i32.const 1))
|
|
3310
|
+
(then
|
|
3311
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
3312
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
3313
|
+
(call $fail_barrier_drain (local.get $n))
|
|
3314
|
+
(return (i32.const 0))))
|
|
3315
|
+
(if (i32.eq (local.get $res) (i32.const 2))
|
|
3316
|
+
(then
|
|
3317
|
+
(call $fail_barrier_drain (local.get $n))
|
|
3318
|
+
(return (i32.const 0))))
|
|
3319
|
+
(i32.store (i32.add (local.get $n) (i32.const 48)) (i32.const 1))
|
|
3320
|
+
)
|
|
3321
|
+
)
|
|
3322
|
+
(if (i32.eq (i32.load (local.get $src)) (i32.const 0))
|
|
3323
|
+
(then (global.set $g_need_pull (local.get $src)) (return (i32.const 1))))
|
|
3324
|
+
(if (i32.or (i32.eq (i32.load (local.get $src)) (i32.const 2)) (i32.gt_s (i32.load (i32.add (local.get $src) (i32.const 44))) (i32.const 0)))
|
|
3325
|
+
(then
|
|
3326
|
+
(call $fail_barrier_drain (local.get $n))
|
|
3327
|
+
(return (i32.const 0))))
|
|
3328
|
+
|
|
3329
|
+
(call $retire_node (local.get $n))
|
|
3330
|
+
(call $bd_release (local.get $src))
|
|
3331
|
+
(if (i32.ne (local.get $bar) (i32.const 0))
|
|
3332
|
+
(then
|
|
3333
|
+
(local.set $res (i32.sub (i32.load (i32.add (local.get $bar) (i32.const 4))) (i32.const 1)))
|
|
3334
|
+
(i32.store (i32.add (local.get $bar) (i32.const 4)) (local.get $res))
|
|
3335
|
+
(if (i32.and (i32.eqz (i32.load (local.get $bar))) (i32.le_s (local.get $res) (i32.const 0)))
|
|
3336
|
+
(then (i32.store (local.get $bar) (i32.const 1)))) ;; PASSED
|
|
3337
|
+
)
|
|
3338
|
+
)
|
|
3339
|
+
(i32.const 0)
|
|
3340
|
+
)
|
|
3341
|
+
|
|
3342
|
+
(func $step_gate (param $n i32) (result i32)
|
|
3343
|
+
(local $bar i32)
|
|
3344
|
+
(local $src i32)
|
|
3345
|
+
(local $dst i32)
|
|
3346
|
+
(local $st i32)
|
|
3347
|
+
(local $g i32)
|
|
3348
|
+
(local.set $bar (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
3349
|
+
(local.set $src (call $node_get_in (local.get $n) (i32.const 0)))
|
|
3350
|
+
(local.set $dst (call $node_get_out (local.get $n) (i32.const 0)))
|
|
3351
|
+
(local.set $st (i32.load (local.get $bar)))
|
|
3352
|
+
|
|
3353
|
+
(if (i32.eqz (local.get $st))
|
|
3354
|
+
(then
|
|
3355
|
+
;; Find first non-retired guard drain in barrier list
|
|
3356
|
+
(local.set $g (i32.load (i32.add (local.get $bar) (i32.const 8))))
|
|
3357
|
+
(block $bg
|
|
3358
|
+
(loop $lg
|
|
3359
|
+
(br_if $bg (i32.eqz (local.get $g)))
|
|
3360
|
+
(if (i32.eqz (i32.load (i32.add (local.get $g) (i32.const 4))))
|
|
3361
|
+
(then
|
|
3362
|
+
(global.set $g_need_step_node (local.get $g))
|
|
3363
|
+
(return (i32.const 3))))
|
|
3364
|
+
(local.set $g (i32.load (i32.add (local.get $g) (i32.const 40))))
|
|
3365
|
+
(br $lg)
|
|
3366
|
+
)
|
|
3367
|
+
)
|
|
3368
|
+
(i32.store (local.get $bar) (i32.const 1))
|
|
3369
|
+
(local.set $st (i32.const 1))
|
|
3370
|
+
)
|
|
3371
|
+
)
|
|
3372
|
+
|
|
3373
|
+
(if (i32.eq (local.get $st) (i32.const 1))
|
|
3374
|
+
(then
|
|
3375
|
+
(call $retire_node (local.get $n))
|
|
3376
|
+
(call $splice (local.get $src) (local.get $dst))
|
|
3377
|
+
(return (i32.const 0))))
|
|
3378
|
+
|
|
3379
|
+
;; FAILED
|
|
3380
|
+
(call $retire_node (local.get $n))
|
|
3381
|
+
(call $bd_fail (local.get $dst))
|
|
3382
|
+
(call $cancel_input (local.get $src))
|
|
3383
|
+
(i32.const 0)
|
|
3384
|
+
)
|
|
3385
|
+
|
|
3386
|
+
(func $fail_switch (param $n i32) (result i32)
|
|
3387
|
+
(local $i i32)
|
|
3388
|
+
(local $cnt i32)
|
|
3389
|
+
(call $retire_node (local.get $n))
|
|
3390
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
3391
|
+
(local.set $i (i32.const 0))
|
|
3392
|
+
(block $bo
|
|
3393
|
+
(loop $lo
|
|
3394
|
+
(br_if $bo (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
3395
|
+
(call $bd_fail (call $node_get_out (local.get $n) (local.get $i)))
|
|
3396
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3397
|
+
(br $lo)
|
|
3398
|
+
)
|
|
3399
|
+
)
|
|
3400
|
+
(local.set $cnt (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
3401
|
+
(local.set $i (i32.const 0))
|
|
3402
|
+
(block $bi
|
|
3403
|
+
(loop $li
|
|
3404
|
+
(br_if $bi (i32.ge_s (local.get $i) (local.get $cnt)))
|
|
3405
|
+
(call $cancel_input (call $node_get_in (local.get $n) (local.get $i)))
|
|
3406
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3407
|
+
(br $li)
|
|
3408
|
+
)
|
|
3409
|
+
)
|
|
3410
|
+
(i32.const 0)
|
|
3411
|
+
)
|
|
3412
|
+
|
|
3413
|
+
(func $step_switch (param $n i32) (result i32)
|
|
3414
|
+
(local $desc i32)
|
|
3415
|
+
(local $numInsp i32)
|
|
3416
|
+
(local $numArms i32)
|
|
3417
|
+
(local $defFn i32)
|
|
3418
|
+
(local $testedMask i32)
|
|
3419
|
+
(local $inspPtr i32)
|
|
3420
|
+
(local $tagsPtr i32)
|
|
3421
|
+
(local $j i32)
|
|
3422
|
+
(local $ch i32)
|
|
3423
|
+
(local $sid i32)
|
|
3424
|
+
(local $w i32)
|
|
3425
|
+
(local $ord i32)
|
|
3426
|
+
(local $a i32)
|
|
3427
|
+
(local $armPtr i32)
|
|
3428
|
+
(local $armStride i32)
|
|
3429
|
+
(local $ok i32)
|
|
3430
|
+
(local $pat i32)
|
|
3431
|
+
(local $chosenFn i32)
|
|
3432
|
+
(local $sc i32)
|
|
3433
|
+
(local $env i32)
|
|
3434
|
+
|
|
3435
|
+
(local.set $desc (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
3436
|
+
(local.set $numInsp (i32.load (local.get $desc)))
|
|
3437
|
+
(local.set $numArms (i32.load (i32.add (local.get $desc) (i32.const 4))))
|
|
3438
|
+
(local.set $defFn (i32.load (i32.add (local.get $desc) (i32.const 8))))
|
|
3439
|
+
(local.set $testedMask (i32.load (i32.add (local.get $desc) (i32.const 12))))
|
|
3440
|
+
(local.set $inspPtr (i32.load (i32.add (local.get $n) (i32.const 44))))
|
|
3441
|
+
|
|
3442
|
+
;; Reusable global scratch tag array (zero per-step allocations!)
|
|
3443
|
+
(if (i32.gt_s (local.get $numInsp) (global.get $g_sw_tags_cap))
|
|
3444
|
+
(then
|
|
3445
|
+
(call $free (global.get $g_sw_tags_ptr))
|
|
3446
|
+
(global.set $g_sw_tags_cap (select (local.get $numInsp) (i32.const 16) (i32.gt_s (local.get $numInsp) (i32.const 16))))
|
|
3447
|
+
(global.set $g_sw_tags_ptr (call $alloc (i32.shl (global.get $g_sw_tags_cap) (i32.const 2))))
|
|
3448
|
+
)
|
|
3449
|
+
)
|
|
3450
|
+
(local.set $tagsPtr (global.get $g_sw_tags_ptr))
|
|
3451
|
+
(if (i32.and (i32.gt_s (local.get $numInsp) (i32.const 0)) (i32.eqz (local.get $tagsPtr)))
|
|
3452
|
+
(then (return (i32.const 0))))
|
|
3453
|
+
|
|
3454
|
+
(local.set $j (i32.const 0))
|
|
3455
|
+
(block $bi
|
|
3456
|
+
(loop $li
|
|
3457
|
+
(br_if $bi (i32.ge_s (local.get $j) (local.get $numInsp)))
|
|
3458
|
+
(if (i32.and (i32.shr_u (local.get $testedMask) (local.get $j)) (i32.const 1))
|
|
3459
|
+
(then
|
|
3460
|
+
(local.set $ch (i32.load (i32.add (local.get $inspPtr) (i32.shl (local.get $j) (i32.const 2)))))
|
|
3461
|
+
(local.set $sid (i32.load (i32.add (local.get $ch) (i32.const 8))))
|
|
3462
|
+
(if (i32.or (i32.ne (call $schema_kind (local.get $sid)) (i32.const 1))
|
|
3463
|
+
(i32.le_s (call $schema_num_edges (local.get $sid)) (i32.const 0)))
|
|
3464
|
+
(then (return (call $fail_switch (local.get $n)))))
|
|
3465
|
+
(local.set $w (call $schema_width (local.get $sid)))
|
|
3466
|
+
(local.set $ord (i32.const 0))
|
|
3467
|
+
(if (i32.gt_s (local.get $w) (i32.const 0))
|
|
3468
|
+
(then
|
|
3469
|
+
(if (i32.lt_s (i32.load (i32.add (local.get $ch) (i32.const 44))) (local.get $w))
|
|
3470
|
+
(then
|
|
3471
|
+
(if (i32.eq (i32.load (local.get $ch)) (i32.const 0))
|
|
3472
|
+
(then (global.set $g_need_pull (local.get $ch)) (return (i32.const 1))))
|
|
3473
|
+
(return (call $fail_switch (local.get $n)))))
|
|
3474
|
+
(local.set $ord (call $bd_peek_bits_at (local.get $ch) (i32.const 0) (local.get $w)))
|
|
3475
|
+
)
|
|
3476
|
+
)
|
|
3477
|
+
(if (i32.ge_u (local.get $ord) (call $schema_num_edges (local.get $sid)))
|
|
3478
|
+
(then (return (call $fail_switch (local.get $n)))))
|
|
3479
|
+
(i32.store (i32.add (local.get $tagsPtr) (i32.shl (local.get $j) (i32.const 2)))
|
|
3480
|
+
(call $schema_edge_tag (local.get $sid) (local.get $ord)))
|
|
3481
|
+
)
|
|
3482
|
+
)
|
|
3483
|
+
(local.set $j (i32.add (local.get $j) (i32.const 1)))
|
|
3484
|
+
(br $li)
|
|
3485
|
+
)
|
|
3486
|
+
)
|
|
3487
|
+
|
|
3488
|
+
;; Match first matching arm
|
|
3489
|
+
(local.set $chosenFn (i32.const -1))
|
|
3490
|
+
(local.set $armStride (i32.shl (i32.add (local.get $numInsp) (i32.const 1)) (i32.const 2)))
|
|
3491
|
+
(local.set $a (i32.const 0))
|
|
3492
|
+
(block $ba
|
|
3493
|
+
(loop $la
|
|
3494
|
+
(br_if $ba (i32.ge_s (local.get $a) (local.get $numArms)))
|
|
3495
|
+
(local.set $armPtr (i32.add (i32.add (local.get $desc) (i32.const 16)) (i32.mul (local.get $a) (local.get $armStride))))
|
|
3496
|
+
(local.set $ok (i32.const 1))
|
|
3497
|
+
(local.set $j (i32.const 0))
|
|
3498
|
+
(block $bp
|
|
3499
|
+
(loop $lp
|
|
3500
|
+
(br_if $bp (i32.ge_s (local.get $j) (local.get $numInsp)))
|
|
3501
|
+
(local.set $pat (i32.load (i32.add (i32.add (local.get $armPtr) (i32.const 4)) (i32.shl (local.get $j) (i32.const 2)))))
|
|
3502
|
+
(if (i32.and (i32.ge_s (local.get $pat) (i32.const 0))
|
|
3503
|
+
(i32.ne (local.get $pat) (i32.load (i32.add (local.get $tagsPtr) (i32.shl (local.get $j) (i32.const 2))))))
|
|
3504
|
+
(then
|
|
3505
|
+
(local.set $ok (i32.const 0))
|
|
3506
|
+
(br $bp)))
|
|
3507
|
+
(local.set $j (i32.add (local.get $j) (i32.const 1)))
|
|
3508
|
+
(br $lp)
|
|
3509
|
+
)
|
|
3510
|
+
)
|
|
3511
|
+
(if (local.get $ok)
|
|
3512
|
+
(then
|
|
3513
|
+
(local.set $chosenFn (i32.load (local.get $armPtr)))
|
|
3514
|
+
(br $ba)))
|
|
3515
|
+
(local.set $a (i32.add (local.get $a) (i32.const 1)))
|
|
3516
|
+
(br $la)
|
|
3517
|
+
)
|
|
3518
|
+
)
|
|
3519
|
+
|
|
3520
|
+
(if (i32.lt_s (local.get $chosenFn) (i32.const 0))
|
|
3521
|
+
(then (local.set $chosenFn (local.get $defFn))))
|
|
3522
|
+
(if (i32.lt_s (local.get $chosenFn) (i32.const 0))
|
|
3523
|
+
(then (return (call $fail_switch (local.get $n)))))
|
|
3524
|
+
|
|
3525
|
+
(local.set $sc (i32.load (i32.add (local.get $n) (i32.const 8))))
|
|
3526
|
+
(local.set $env (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3527
|
+
(call $retire_node (local.get $n))
|
|
3528
|
+
(call_indirect (type $block_fn) (local.get $sc) (local.get $env) (local.get $chosenFn))
|
|
3529
|
+
(i32.const 0)
|
|
3530
|
+
)
|
|
3531
|
+
|
|
3532
|
+
(func $step_tap (param $n i32) (result i32)
|
|
3533
|
+
(local $parentCh i32)
|
|
3534
|
+
(local $childCh i32)
|
|
3535
|
+
(local $replay i32)
|
|
3536
|
+
(local $len i32)
|
|
3537
|
+
(local $burst i32)
|
|
3538
|
+
(local $chunk i32)
|
|
3539
|
+
(local $bits i32)
|
|
3540
|
+
(local.set $parentCh (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
3541
|
+
(local.set $replay (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3542
|
+
(local.set $childCh (call $node_get_out (local.get $n) (i32.const 0)))
|
|
3543
|
+
|
|
3544
|
+
(local.set $len (i32.load (i32.add (local.get $parentCh) (i32.const 44))))
|
|
3545
|
+
(if (i32.gt_s (local.get $len) (i32.const 0))
|
|
3546
|
+
(then
|
|
3547
|
+
(local.set $burst (i32.const 0))
|
|
3548
|
+
(block $bt
|
|
3549
|
+
(loop $lt
|
|
3550
|
+
(local.set $len (i32.load (i32.add (local.get $parentCh) (i32.const 44))))
|
|
3551
|
+
(br_if $bt (i32.or (i32.le_s (local.get $len) (i32.const 0)) (i32.ge_s (local.get $burst) (i32.const 4096))))
|
|
3552
|
+
(local.set $chunk (select (i32.const 30) (local.get $len) (i32.gt_s (local.get $len) (i32.const 30))))
|
|
3553
|
+
(local.set $bits (call $bd_shift_bits (local.get $parentCh) (local.get $chunk)))
|
|
3554
|
+
(call $bd_push_bits (local.get $replay) (local.get $bits) (local.get $chunk))
|
|
3555
|
+
(call $bd_push_bits (local.get $childCh) (local.get $bits) (local.get $chunk))
|
|
3556
|
+
(local.set $burst (i32.add (local.get $burst) (local.get $chunk)))
|
|
3557
|
+
(br $lt)
|
|
3558
|
+
)
|
|
3559
|
+
)
|
|
3560
|
+
(return (i32.const 0))))
|
|
3561
|
+
|
|
3562
|
+
(if (i32.eq (i32.load (local.get $parentCh)) (i32.const 0))
|
|
3563
|
+
(then (global.set $g_need_pull (local.get $parentCh)) (return (i32.const 1))))
|
|
3564
|
+
|
|
3565
|
+
(if (i32.eq (i32.load (local.get $parentCh)) (i32.const 1))
|
|
3566
|
+
(then
|
|
3567
|
+
(call $retire_node (local.get $n))
|
|
3568
|
+
(call $bd_close (local.get $childCh))
|
|
3569
|
+
(return (i32.const 0))))
|
|
3570
|
+
|
|
3571
|
+
;; parentCh FAILED
|
|
3572
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 1)) ;; sawUpstreamFail = 1
|
|
3573
|
+
(call $retire_node (local.get $n))
|
|
3574
|
+
(call $bd_fail (local.get $childCh))
|
|
3575
|
+
(i32.const 0)
|
|
3576
|
+
)
|
|
3577
|
+
|
|
3578
|
+
;; ActiveChild struct (20 bytes):
|
|
3579
|
+
;; +0 childScope, +4 tapsPtr (16B each: parentCh, childCh, replayCh, tapNode),
|
|
3580
|
+
;; +8 tapCount, +12 stagedPtr (8B each: childOut, parentOut), +16 stagedCount
|
|
3581
|
+
(func $step_spec (param $n i32) (result i32)
|
|
3582
|
+
(local $desc i32)
|
|
3583
|
+
(local $numBr i32)
|
|
3584
|
+
(local $brIdx i32)
|
|
3585
|
+
(local $inCnt i32)
|
|
3586
|
+
(local $outCnt i32)
|
|
3587
|
+
(local $i i32)
|
|
3588
|
+
(local $j i32)
|
|
3589
|
+
(local $pIn i32)
|
|
3590
|
+
(local $pOut i32)
|
|
3591
|
+
(local $ac i32)
|
|
3592
|
+
(local $childSc i32)
|
|
3593
|
+
(local $taps i32)
|
|
3594
|
+
(local $staged i32)
|
|
3595
|
+
(local $env i32)
|
|
3596
|
+
(local $replay i32)
|
|
3597
|
+
(local $childIn i32)
|
|
3598
|
+
(local $tapNode i32)
|
|
3599
|
+
(local $childOut i32)
|
|
3600
|
+
(local $anyFail i32)
|
|
3601
|
+
(local $upFail i32)
|
|
3602
|
+
(local $allClosed i32)
|
|
3603
|
+
(local $nextOpen i32)
|
|
3604
|
+
(local $te i32)
|
|
3605
|
+
(local $se i32)
|
|
3606
|
+
(local $fnIdx i32)
|
|
3607
|
+
(local $sc i32)
|
|
3608
|
+
|
|
3609
|
+
(local.set $desc (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
3610
|
+
(local.set $numBr (i32.load (local.get $desc)))
|
|
3611
|
+
(local.set $brIdx (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3612
|
+
(local.set $inCnt (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
3613
|
+
(local.set $outCnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
3614
|
+
|
|
3615
|
+
;; Empty choice -> fail immediately
|
|
3616
|
+
(if (i32.le_s (local.get $numBr) (i32.const 0))
|
|
3617
|
+
(then
|
|
3618
|
+
(call $retire_node (local.get $n))
|
|
3619
|
+
(local.set $i (i32.const 0))
|
|
3620
|
+
(block $be1 (loop $le1 (br_if $be1 (i32.ge_s (local.get $i) (local.get $outCnt)))
|
|
3621
|
+
(call $bd_fail (call $node_get_out (local.get $n) (local.get $i)))
|
|
3622
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $le1)))
|
|
3623
|
+
(local.set $i (i32.const 0))
|
|
3624
|
+
(block $be2 (loop $le2 (br_if $be2 (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3625
|
+
(call $cancel_input (call $node_get_in (local.get $n) (local.get $i)))
|
|
3626
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $le2)))
|
|
3627
|
+
(return (i32.const 0))))
|
|
3628
|
+
|
|
3629
|
+
;; Immediate upstream failure check
|
|
3630
|
+
(local.set $i (i32.const 0))
|
|
3631
|
+
(block $bu
|
|
3632
|
+
(loop $lu
|
|
3633
|
+
(br_if $bu (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3634
|
+
(local.set $pIn (call $node_get_in (local.get $n) (local.get $i)))
|
|
3635
|
+
(if (i32.eq (i32.load (local.get $pIn)) (i32.const 2))
|
|
3636
|
+
(then
|
|
3637
|
+
(global.set $g_spec_upstream_aborts (i32.add (global.get $g_spec_upstream_aborts) (i32.const 1)))
|
|
3638
|
+
(local.set $ac (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
3639
|
+
(if (i32.ne (local.get $ac) (i32.const 0))
|
|
3640
|
+
(then
|
|
3641
|
+
(local.set $taps (i32.load (i32.add (local.get $ac) (i32.const 4))))
|
|
3642
|
+
(local.set $j (i32.const 0))
|
|
3643
|
+
(block $bua0 (loop $lua0 (br_if $bua0 (i32.ge_s (local.get $j) (local.get $inCnt)))
|
|
3644
|
+
(call $bd_release (i32.load (i32.add (i32.add (local.get $taps) (i32.shl (local.get $j) (i32.const 4))) (i32.const 8))))
|
|
3645
|
+
(local.set $j (i32.add (local.get $j) (i32.const 1))) (br $lua0)))
|
|
3646
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
3647
|
+
(call $scope_destroy (i32.load (local.get $ac)))
|
|
3648
|
+
(call $spec_free_active (local.get $ac))
|
|
3649
|
+
)
|
|
3650
|
+
)
|
|
3651
|
+
(call $retire_node (local.get $n))
|
|
3652
|
+
(local.set $i (i32.const 0))
|
|
3653
|
+
(block $buf (loop $luf (br_if $buf (i32.ge_s (local.get $i) (local.get $outCnt)))
|
|
3654
|
+
(call $bd_fail (call $node_get_out (local.get $n) (local.get $i)))
|
|
3655
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $luf)))
|
|
3656
|
+
(local.set $i (i32.const 0))
|
|
3657
|
+
(block $bufi (loop $lufi (br_if $bufi (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3658
|
+
(call $cancel_input (call $node_get_in (local.get $n) (local.get $i)))
|
|
3659
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lufi)))
|
|
3660
|
+
(return (i32.const 0))))
|
|
3661
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3662
|
+
(br $lu)
|
|
3663
|
+
)
|
|
3664
|
+
)
|
|
3665
|
+
|
|
3666
|
+
(local.set $ac (i32.load (i32.add (local.get $n) (i32.const 40))))
|
|
3667
|
+
(if (i32.eqz (local.get $ac))
|
|
3668
|
+
(then
|
|
3669
|
+
;; Rule 3: Last-branch tail splice!
|
|
3670
|
+
(if (i32.eq (local.get $brIdx) (i32.sub (local.get $numBr) (i32.const 1)))
|
|
3671
|
+
(then
|
|
3672
|
+
(global.set $g_spec_tail_splices (i32.add (global.get $g_spec_tail_splices) (i32.const 1)))
|
|
3673
|
+
(local.set $fnIdx (i32.load (i32.add (i32.add (local.get $desc) (i32.const 4)) (i32.shl (local.get $brIdx) (i32.const 2)))))
|
|
3674
|
+
(local.set $sc (i32.load (i32.add (local.get $n) (i32.const 8))))
|
|
3675
|
+
(local.set $env (call $alloc (i32.shl (i32.add (local.get $inCnt) (local.get $outCnt)) (i32.const 2))))
|
|
3676
|
+
(if (i32.gt_s (local.get $inCnt) (i32.const 0))
|
|
3677
|
+
(then (memory.copy (local.get $env) (i32.load (i32.add (local.get $n) (i32.const 16))) (i32.shl (local.get $inCnt) (i32.const 2)))))
|
|
3678
|
+
(if (i32.gt_s (local.get $outCnt) (i32.const 0))
|
|
3679
|
+
(then (memory.copy (i32.add (local.get $env) (i32.shl (local.get $inCnt) (i32.const 2))) (i32.load (i32.add (local.get $n) (i32.const 24))) (i32.shl (local.get $outCnt) (i32.const 2)))))
|
|
3680
|
+
(call $retire_node (local.get $n))
|
|
3681
|
+
(call_indirect (type $block_fn) (local.get $sc) (local.get $env) (local.get $fnIdx))
|
|
3682
|
+
(call $free (local.get $env))
|
|
3683
|
+
(return (i32.const 0))))
|
|
3684
|
+
|
|
3685
|
+
;; Speculative branch i < k - 1: create child scope + TAPs + staging outputs
|
|
3686
|
+
(local.set $childSc (call $scope_create (i32.load (i32.add (local.get $n) (i32.const 8)))))
|
|
3687
|
+
(local.set $ac (call $alloc (i32.const 20)))
|
|
3688
|
+
(local.set $taps (call $alloc (i32.shl (local.get $inCnt) (i32.const 4))))
|
|
3689
|
+
(local.set $staged (call $alloc (i32.shl (local.get $outCnt) (i32.const 3))))
|
|
3690
|
+
(local.set $env (call $alloc (i32.shl (i32.add (local.get $inCnt) (local.get $outCnt)) (i32.const 2))))
|
|
3691
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (i32.const 0))))
|
|
3692
|
+
|
|
3693
|
+
(local.set $i (i32.const 0))
|
|
3694
|
+
(block $bt1
|
|
3695
|
+
(loop $lt1
|
|
3696
|
+
(br_if $bt1 (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3697
|
+
(local.set $pIn (call $node_get_in (local.get $n) (local.get $i)))
|
|
3698
|
+
(local.set $replay (call $chan_alloc (i32.const 0) (i32.load (i32.add (local.get $pIn) (i32.const 8)))))
|
|
3699
|
+
(local.set $childIn (call $chan_alloc (local.get $childSc) (i32.load (i32.add (local.get $pIn) (i32.const 8)))))
|
|
3700
|
+
(i32.store (i32.add (local.get $childIn) (i32.const 12)) (i32.load (i32.add (local.get $pIn) (i32.const 12))))
|
|
3701
|
+
(local.set $tapNode (call $node_alloc (local.get $childSc) (i32.const 13) (i32.const 0) (i32.const 1)))
|
|
3702
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (i32.const 0))))
|
|
3703
|
+
(call $node_set_out (local.get $tapNode) (i32.const 0) (local.get $childIn))
|
|
3704
|
+
(i32.store (i32.add (local.get $tapNode) (i32.const 32)) (local.get $pIn))
|
|
3705
|
+
(i32.store (i32.add (local.get $tapNode) (i32.const 36)) (local.get $replay))
|
|
3706
|
+
(i32.store (i32.add (local.get $pIn) (i32.const 20)) (local.get $tapNode))
|
|
3707
|
+
(local.set $te (i32.add (local.get $taps) (i32.shl (local.get $i) (i32.const 4))))
|
|
3708
|
+
(call $chan_pin (local.get $pIn))
|
|
3709
|
+
(call $chan_pin (local.get $childIn))
|
|
3710
|
+
(call $chan_pin (local.get $replay))
|
|
3711
|
+
(i32.store (local.get $te) (local.get $pIn))
|
|
3712
|
+
(i32.store (i32.add (local.get $te) (i32.const 4)) (local.get $childIn))
|
|
3713
|
+
(i32.store (i32.add (local.get $te) (i32.const 8)) (local.get $replay))
|
|
3714
|
+
(i32.store (i32.add (local.get $te) (i32.const 12)) (local.get $tapNode))
|
|
3715
|
+
(i32.store (i32.add (local.get $tapNode) (i32.const 60)) (i32.add (i32.load (i32.add (local.get $tapNode) (i32.const 60))) (i32.const 1)))
|
|
3716
|
+
(i32.store (i32.add (local.get $env) (i32.shl (local.get $i) (i32.const 2))) (local.get $childIn))
|
|
3717
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3718
|
+
(br $lt1)
|
|
3719
|
+
)
|
|
3720
|
+
)
|
|
3721
|
+
|
|
3722
|
+
(local.set $i (i32.const 0))
|
|
3723
|
+
(block $bs1
|
|
3724
|
+
(loop $ls1
|
|
3725
|
+
(br_if $bs1 (i32.ge_s (local.get $i) (local.get $outCnt)))
|
|
3726
|
+
(local.set $pOut (call $node_get_out (local.get $n) (local.get $i)))
|
|
3727
|
+
(local.set $childOut (call $chan_alloc (local.get $childSc) (i32.load (i32.add (local.get $pOut) (i32.const 4)))))
|
|
3728
|
+
(local.set $se (i32.add (local.get $staged) (i32.shl (local.get $i) (i32.const 3))))
|
|
3729
|
+
(call $chan_pin (local.get $childOut))
|
|
3730
|
+
(call $chan_pin (local.get $pOut))
|
|
3731
|
+
(i32.store (local.get $se) (local.get $childOut))
|
|
3732
|
+
(i32.store (i32.add (local.get $se) (i32.const 4)) (local.get $pOut))
|
|
3733
|
+
(i32.store (i32.add (local.get $env) (i32.shl (i32.add (local.get $inCnt) (local.get $i)) (i32.const 2))) (local.get $childOut))
|
|
3734
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3735
|
+
(br $ls1)
|
|
3736
|
+
)
|
|
3737
|
+
)
|
|
3738
|
+
|
|
3739
|
+
(i32.store (local.get $ac) (local.get $childSc))
|
|
3740
|
+
(i32.store (i32.add (local.get $ac) (i32.const 4)) (local.get $taps))
|
|
3741
|
+
(i32.store (i32.add (local.get $ac) (i32.const 8)) (local.get $inCnt))
|
|
3742
|
+
(i32.store (i32.add (local.get $ac) (i32.const 12)) (local.get $staged))
|
|
3743
|
+
(i32.store (i32.add (local.get $ac) (i32.const 16)) (local.get $outCnt))
|
|
3744
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (local.get $ac))
|
|
3745
|
+
|
|
3746
|
+
(local.set $fnIdx (i32.load (i32.add (i32.add (local.get $desc) (i32.const 4)) (i32.shl (local.get $brIdx) (i32.const 2)))))
|
|
3747
|
+
(call_indirect (type $block_fn) (local.get $childSc) (local.get $env) (local.get $fnIdx))
|
|
3748
|
+
(call $free (local.get $env))
|
|
3749
|
+
)
|
|
3750
|
+
)
|
|
3751
|
+
|
|
3752
|
+
;; Inspect activeChild status
|
|
3753
|
+
(local.set $childSc (i32.load (local.get $ac)))
|
|
3754
|
+
(local.set $taps (i32.load (i32.add (local.get $ac) (i32.const 4))))
|
|
3755
|
+
(local.set $staged (i32.load (i32.add (local.get $ac) (i32.const 12))))
|
|
3756
|
+
|
|
3757
|
+
(local.set $anyFail (i32.const 0))
|
|
3758
|
+
(local.set $allClosed (i32.const 1))
|
|
3759
|
+
(local.set $nextOpen (i32.const 0))
|
|
3760
|
+
(local.set $i (i32.const 0))
|
|
3761
|
+
(block $bchk
|
|
3762
|
+
(loop $lchk
|
|
3763
|
+
(br_if $bchk (i32.ge_s (local.get $i) (local.get $outCnt)))
|
|
3764
|
+
(local.set $childOut (i32.load (i32.add (local.get $staged) (i32.shl (local.get $i) (i32.const 3)))))
|
|
3765
|
+
(if (i32.eq (i32.load (local.get $childOut)) (i32.const 2)) (then (local.set $anyFail (i32.const 1))))
|
|
3766
|
+
(if (i32.ne (i32.load (local.get $childOut)) (i32.const 1)) (then (local.set $allClosed (i32.const 0))))
|
|
3767
|
+
(if (i32.and (i32.eqz (local.get $nextOpen)) (i32.eq (i32.load (local.get $childOut)) (i32.const 0)))
|
|
3768
|
+
(then (local.set $nextOpen (local.get $childOut))))
|
|
3769
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3770
|
+
(br $lchk)
|
|
3771
|
+
)
|
|
3772
|
+
)
|
|
3773
|
+
|
|
3774
|
+
(if (local.get $anyFail)
|
|
3775
|
+
(then
|
|
3776
|
+
;; Check upstream failure
|
|
3777
|
+
(local.set $upFail (i32.const 0))
|
|
3778
|
+
(local.set $i (i32.const 0))
|
|
3779
|
+
(block $buf2
|
|
3780
|
+
(loop $luf2
|
|
3781
|
+
(br_if $buf2 (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3782
|
+
(local.set $te (i32.add (local.get $taps) (i32.shl (local.get $i) (i32.const 4))))
|
|
3783
|
+
(local.set $pIn (i32.load (local.get $te)))
|
|
3784
|
+
(local.set $tapNode (i32.load (i32.add (local.get $te) (i32.const 12))))
|
|
3785
|
+
(if (i32.or (i32.eq (i32.load (local.get $pIn)) (i32.const 2))
|
|
3786
|
+
(i32.ne (i32.load (i32.add (local.get $tapNode) (i32.const 40))) (i32.const 0)))
|
|
3787
|
+
(then (local.set $upFail (i32.const 1)) (br $buf2)))
|
|
3788
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3789
|
+
(br $luf2)
|
|
3790
|
+
)
|
|
3791
|
+
)
|
|
3792
|
+
|
|
3793
|
+
(if (local.get $upFail)
|
|
3794
|
+
(then
|
|
3795
|
+
;; Rule 1: Upstream failure abort!
|
|
3796
|
+
(global.set $g_spec_upstream_aborts (i32.add (global.get $g_spec_upstream_aborts) (i32.const 1)))
|
|
3797
|
+
(local.set $i (i32.const 0))
|
|
3798
|
+
(block $bua (loop $lua (br_if $bua (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3799
|
+
(local.set $te (i32.add (local.get $taps) (i32.shl (local.get $i) (i32.const 4))))
|
|
3800
|
+
(local.set $pIn (i32.load (local.get $te)))
|
|
3801
|
+
(if (i32.eq (i32.load (i32.add (local.get $pIn) (i32.const 20))) (i32.load (i32.add (local.get $te) (i32.const 12))))
|
|
3802
|
+
(then (i32.store (i32.add (local.get $pIn) (i32.const 20)) (i32.const 0))))
|
|
3803
|
+
(call $bd_release (i32.load (i32.add (local.get $te) (i32.const 8))))
|
|
3804
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lua)))
|
|
3805
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
3806
|
+
(call $scope_destroy (local.get $childSc))
|
|
3807
|
+
(call $spec_free_active (local.get $ac))
|
|
3808
|
+
(call $retire_node (local.get $n))
|
|
3809
|
+
(local.set $i (i32.const 0))
|
|
3810
|
+
(block $bua2 (loop $lua2 (br_if $bua2 (i32.ge_s (local.get $i) (local.get $outCnt)))
|
|
3811
|
+
(call $bd_fail (call $node_get_out (local.get $n) (local.get $i)))
|
|
3812
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lua2)))
|
|
3813
|
+
(local.set $i (i32.const 0))
|
|
3814
|
+
(block $bua3 (loop $lua3 (br_if $bua3 (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3815
|
+
(call $cancel_input (call $node_get_in (local.get $n) (local.get $i)))
|
|
3816
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lua3)))
|
|
3817
|
+
(return (i32.const 0))))
|
|
3818
|
+
|
|
3819
|
+
;; Rule 2: Local mismatch rollback with bit-exact replay!
|
|
3820
|
+
(global.set $g_spec_rollbacks (i32.add (global.get $g_spec_rollbacks) (i32.const 1)))
|
|
3821
|
+
(local.set $i (i32.const 0))
|
|
3822
|
+
(block $brb1 (loop $lrb1 (br_if $brb1 (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3823
|
+
(local.set $te (i32.add (local.get $taps) (i32.shl (local.get $i) (i32.const 4))))
|
|
3824
|
+
(local.set $pIn (i32.load (local.get $te)))
|
|
3825
|
+
(if (i32.eq (i32.load (i32.add (local.get $pIn) (i32.const 20))) (i32.load (i32.add (local.get $te) (i32.const 12))))
|
|
3826
|
+
(then (i32.store (i32.add (local.get $pIn) (i32.const 20)) (local.get $n))))
|
|
3827
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lrb1)))
|
|
3828
|
+
|
|
3829
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
3830
|
+
(call $scope_destroy (local.get $childSc))
|
|
3831
|
+
|
|
3832
|
+
(local.set $i (i32.const 0))
|
|
3833
|
+
(block $brb2 (loop $lrb2 (br_if $brb2 (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3834
|
+
(local.set $te (i32.add (local.get $taps) (i32.shl (local.get $i) (i32.const 4))))
|
|
3835
|
+
(local.set $pIn (i32.load (local.get $te)))
|
|
3836
|
+
(local.set $replay (i32.load (i32.add (local.get $te) (i32.const 8))))
|
|
3837
|
+
(call $bd_unshift_from_deque (local.get $pIn) (local.get $replay))
|
|
3838
|
+
(call $bd_release (local.get $replay))
|
|
3839
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lrb2)))
|
|
3840
|
+
|
|
3841
|
+
(call $spec_free_active (local.get $ac))
|
|
3842
|
+
(i32.store (i32.add (local.get $n) (i32.const 36)) (i32.add (local.get $brIdx) (i32.const 1)))
|
|
3843
|
+
(return (i32.const 0))
|
|
3844
|
+
)
|
|
3845
|
+
)
|
|
3846
|
+
|
|
3847
|
+
;; Commit when all staged outputs are CLOSED
|
|
3848
|
+
(if (local.get $allClosed)
|
|
3849
|
+
(then
|
|
3850
|
+
(local.set $i (i32.const 0))
|
|
3851
|
+
(block $bcm1 (loop $lcm1 (br_if $bcm1 (i32.ge_s (local.get $i) (local.get $inCnt)))
|
|
3852
|
+
(local.set $te (i32.add (local.get $taps) (i32.shl (local.get $i) (i32.const 4))))
|
|
3853
|
+
(local.set $pIn (i32.load (local.get $te)))
|
|
3854
|
+
(if (i32.eq (i32.load (i32.add (local.get $pIn) (i32.const 20))) (i32.load (i32.add (local.get $te) (i32.const 12))))
|
|
3855
|
+
(then (i32.store (i32.add (local.get $pIn) (i32.const 20)) (i32.const 0))))
|
|
3856
|
+
(call $bd_release (i32.load (i32.add (local.get $te) (i32.const 8))))
|
|
3857
|
+
(call $bd_release (local.get $pIn))
|
|
3858
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lcm1)))
|
|
3859
|
+
|
|
3860
|
+
;; Detach staged outputs from childScope so scope_destroy won't release them prematurely
|
|
3861
|
+
(local.set $i (i32.const 0))
|
|
3862
|
+
(block $bcm2 (loop $lcm2 (br_if $bcm2 (i32.ge_s (local.get $i) (local.get $outCnt)))
|
|
3863
|
+
(local.set $childOut (i32.load (i32.add (local.get $staged) (i32.shl (local.get $i) (i32.const 3)))))
|
|
3864
|
+
(call $scope_unlink_chan (local.get $childOut))
|
|
3865
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lcm2)))
|
|
3866
|
+
|
|
3867
|
+
(i32.store (i32.add (local.get $n) (i32.const 40)) (i32.const 0))
|
|
3868
|
+
(call $retire_node (local.get $n))
|
|
3869
|
+
|
|
3870
|
+
(local.set $i (i32.const 0))
|
|
3871
|
+
(block $bcm3 (loop $lcm3 (br_if $bcm3 (i32.ge_s (local.get $i) (local.get $outCnt)))
|
|
3872
|
+
(local.set $se (i32.add (local.get $staged) (i32.shl (local.get $i) (i32.const 3))))
|
|
3873
|
+
(call $splice (i32.load (local.get $se)) (i32.load (i32.add (local.get $se) (i32.const 4))))
|
|
3874
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lcm3)))
|
|
3875
|
+
|
|
3876
|
+
(call $scope_destroy (local.get $childSc))
|
|
3877
|
+
(call $spec_free_active (local.get $ac))
|
|
3878
|
+
(return (i32.const 0))
|
|
3879
|
+
)
|
|
3880
|
+
)
|
|
3881
|
+
|
|
3882
|
+
;; Pull first still-OPEN staged output
|
|
3883
|
+
(global.set $g_need_pull (local.get $nextOpen))
|
|
3884
|
+
(i32.const 1)
|
|
3885
|
+
)
|
|
3886
|
+
|
|
3887
|
+
(func $step_spawn (param $n i32) (result i32)
|
|
3888
|
+
(local $tok i32)
|
|
3889
|
+
(local $tokOutPtr i32)
|
|
3890
|
+
(local $tokOutCnt i32)
|
|
3891
|
+
(local $i i32)
|
|
3892
|
+
(local $ch i32)
|
|
3893
|
+
(local $fnIdx i32)
|
|
3894
|
+
(local $sc i32)
|
|
3895
|
+
(local $inCnt i32)
|
|
3896
|
+
(local $outCnt i32)
|
|
3897
|
+
(local $env i32)
|
|
3898
|
+
|
|
3899
|
+
;; Check `after %tok` settlement first!
|
|
3900
|
+
(local.set $tok (i32.load (i32.add (local.get $n) (i32.const 36))))
|
|
3901
|
+
(if (i32.and (i32.ne (local.get $tok) (i32.const 0)) (i32.eqz (i32.load (local.get $tok))))
|
|
3902
|
+
(then
|
|
3903
|
+
(local.set $tokOutPtr (i32.load (i32.add (local.get $tok) (i32.const 4))))
|
|
3904
|
+
(local.set $tokOutCnt (i32.load (i32.add (local.get $tok) (i32.const 8))))
|
|
3905
|
+
(local.set $i (i32.const 0))
|
|
3906
|
+
(block $bt
|
|
3907
|
+
(loop $lt
|
|
3908
|
+
(br_if $bt (i32.ge_s (local.get $i) (local.get $tokOutCnt)))
|
|
3909
|
+
(local.set $ch (i32.load (i32.add (local.get $tokOutPtr) (i32.shl (local.get $i) (i32.const 2)))))
|
|
3910
|
+
(if (i32.eq (i32.load (local.get $ch)) (i32.const 0))
|
|
3911
|
+
(then
|
|
3912
|
+
(global.set $g_need_pull (local.get $ch))
|
|
3913
|
+
(return (i32.const 1))))
|
|
3914
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3915
|
+
(br $lt)
|
|
3916
|
+
)
|
|
3917
|
+
)
|
|
3918
|
+
(i32.store (local.get $tok) (i32.const 1))
|
|
3919
|
+
;; Unpin settled token outputs and free token output array
|
|
3920
|
+
(local.set $i (i32.const 0))
|
|
3921
|
+
(block $btu
|
|
3922
|
+
(loop $ltu
|
|
3923
|
+
(br_if $btu (i32.ge_s (local.get $i) (local.get $tokOutCnt)))
|
|
3924
|
+
(call $chan_unpin (i32.load (i32.add (local.get $tokOutPtr) (i32.shl (local.get $i) (i32.const 2)))))
|
|
3925
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
3926
|
+
(br $ltu)
|
|
3927
|
+
)
|
|
3928
|
+
)
|
|
3929
|
+
(call $free (local.get $tokOutPtr))
|
|
3930
|
+
(i32.store (i32.add (local.get $tok) (i32.const 4)) (i32.const 0))
|
|
3931
|
+
(i32.store (i32.add (local.get $tok) (i32.const 8)) (i32.const 0))
|
|
3932
|
+
(global.set $g_after_settlements (i32.add (global.get $g_after_settlements) (i32.const 1)))
|
|
3933
|
+
)
|
|
3934
|
+
)
|
|
3935
|
+
|
|
3936
|
+
(local.set $fnIdx (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
3937
|
+
(local.set $sc (i32.load (i32.add (local.get $n) (i32.const 8))))
|
|
3938
|
+
(local.set $inCnt (i32.load (i32.add (local.get $n) (i32.const 20))))
|
|
3939
|
+
(local.set $outCnt (i32.load (i32.add (local.get $n) (i32.const 28))))
|
|
3940
|
+
(local.set $env (call $alloc (i32.shl (i32.add (local.get $inCnt) (local.get $outCnt)) (i32.const 2))))
|
|
3941
|
+
(if (i32.gt_s (local.get $inCnt) (i32.const 0))
|
|
3942
|
+
(then (memory.copy (local.get $env) (i32.load (i32.add (local.get $n) (i32.const 16))) (i32.shl (local.get $inCnt) (i32.const 2)))))
|
|
3943
|
+
(if (i32.gt_s (local.get $outCnt) (i32.const 0))
|
|
3944
|
+
(then (memory.copy (i32.add (local.get $env) (i32.shl (local.get $inCnt) (i32.const 2))) (i32.load (i32.add (local.get $n) (i32.const 24))) (i32.shl (local.get $outCnt) (i32.const 2)))))
|
|
3945
|
+
|
|
3946
|
+
(call $retire_node (local.get $n))
|
|
3947
|
+
(call_indirect (type $block_fn) (local.get $sc) (local.get $env) (local.get $fnIdx))
|
|
3948
|
+
(call $free (local.get $env))
|
|
3949
|
+
(i32.const 0)
|
|
3950
|
+
)
|
|
3951
|
+
|
|
3952
|
+
(func $step_node (param $n i32) (result i32)
|
|
3953
|
+
(if (call $quota_tick_step) (then (return (i32.const 0))))
|
|
3954
|
+
(global.set $g_need_pull (i32.const 0))
|
|
3955
|
+
(global.set $g_need_step_node (i32.const 0))
|
|
3956
|
+
(block $b15
|
|
3957
|
+
(block $b14
|
|
3958
|
+
(block $b13
|
|
3959
|
+
(block $b12
|
|
3960
|
+
(block $b11
|
|
3961
|
+
(block $b10
|
|
3962
|
+
(block $b9
|
|
3963
|
+
(block $b8
|
|
3964
|
+
(block $b7
|
|
3965
|
+
(block $b6
|
|
3966
|
+
(block $b5
|
|
3967
|
+
(block $b4
|
|
3968
|
+
(block $b3
|
|
3969
|
+
(block $b2
|
|
3970
|
+
(block $b1
|
|
3971
|
+
(block $b0
|
|
3972
|
+
(br_table $b0 $b1 $b2 $b3 $b4 $b5 $b6 $b7 $b8 $b9 $b10 $b11 $b12 $b13 $b14 $b15
|
|
3973
|
+
(i32.load (local.get $n)))
|
|
3974
|
+
)
|
|
3975
|
+
(return (i32.const 0)) ;; 0 invalid
|
|
3976
|
+
)
|
|
3977
|
+
(return (call $step_const_unit (local.get $n))) ;; 1
|
|
3978
|
+
)
|
|
3979
|
+
(return (call $step_splice (local.get $n))) ;; 2
|
|
3980
|
+
)
|
|
3981
|
+
(return (call $step_coerce (local.get $n))) ;; 3
|
|
3982
|
+
)
|
|
3983
|
+
(return (call $step_wrap (local.get $n))) ;; 4
|
|
3984
|
+
)
|
|
3985
|
+
(return (call $step_match (local.get $n))) ;; 5
|
|
3986
|
+
)
|
|
3987
|
+
(return (call $step_demux (local.get $n))) ;; 6
|
|
3988
|
+
)
|
|
3989
|
+
(return (call $step_mux (local.get $n))) ;; 7
|
|
3990
|
+
)
|
|
3991
|
+
(return (call $step_tee (local.get $n))) ;; 8
|
|
3992
|
+
)
|
|
3993
|
+
(return (call $step_drain (local.get $n))) ;; 9
|
|
3994
|
+
)
|
|
3995
|
+
(return (call $step_gate (local.get $n))) ;; 10
|
|
3996
|
+
)
|
|
3997
|
+
(return (call $step_switch (local.get $n))) ;; 11
|
|
3998
|
+
)
|
|
3999
|
+
(return (call $step_spec (local.get $n))) ;; 12
|
|
4000
|
+
)
|
|
4001
|
+
(return (call $step_tap (local.get $n))) ;; 13
|
|
4002
|
+
)
|
|
4003
|
+
(return (call $step_spawn (local.get $n))) ;; 14
|
|
4004
|
+
)
|
|
4005
|
+
;; 15: KERNEL
|
|
4006
|
+
(call_indirect (type $kernel_fn) (local.get $n) (i32.load (i32.add (local.get $n) (i32.const 32))))
|
|
4007
|
+
)
|
|
4008
|
+
|
|
4009
|
+
;; ============================================================================
|
|
4010
|
+
;; 10. Strictly Iterative Demand-Driven Pull Scheduler & Wire Framing
|
|
4011
|
+
;; ============================================================================
|
|
4012
|
+
|
|
4013
|
+
(func $dstack_push (param $kind i32) (param $ptr i32)
|
|
4014
|
+
(local $len i32)
|
|
4015
|
+
(local $cap i32)
|
|
4016
|
+
(local $p i32)
|
|
4017
|
+
(local $newCap i32)
|
|
4018
|
+
(local $newP i32)
|
|
4019
|
+
(local.set $len (global.get $g_dstack_len))
|
|
4020
|
+
(local.set $cap (global.get $g_dstack_cap))
|
|
4021
|
+
(local.set $p (global.get $g_dstack_ptr))
|
|
4022
|
+
(if (i32.ge_s (local.get $len) (local.get $cap))
|
|
4023
|
+
(then
|
|
4024
|
+
(local.set $newCap (select (i32.shl (local.get $cap) (i32.const 1)) (i32.const 256) (i32.gt_s (local.get $cap) (i32.const 0))))
|
|
4025
|
+
(local.set $newP (call $alloc (i32.shl (local.get $newCap) (i32.const 3))))
|
|
4026
|
+
(if (i32.eqz (local.get $newP)) (then (return)))
|
|
4027
|
+
(if (i32.gt_s (local.get $len) (i32.const 0))
|
|
4028
|
+
(then (memory.copy (local.get $newP) (local.get $p) (i32.shl (local.get $len) (i32.const 3)))))
|
|
4029
|
+
(call $free (local.get $p))
|
|
4030
|
+
(local.set $p (local.get $newP))
|
|
4031
|
+
(global.set $g_dstack_ptr (local.get $p))
|
|
4032
|
+
(global.set $g_dstack_cap (local.get $newCap))
|
|
4033
|
+
)
|
|
4034
|
+
)
|
|
4035
|
+
(if (i32.eqz (local.get $kind))
|
|
4036
|
+
(then (call $chan_pin (local.get $ptr)))
|
|
4037
|
+
(else (i32.store (i32.add (local.get $ptr) (i32.const 60)) (i32.add (i32.load (i32.add (local.get $ptr) (i32.const 60))) (i32.const 1)))))
|
|
4038
|
+
(i32.store (i32.add (local.get $p) (i32.shl (local.get $len) (i32.const 3))) (local.get $kind))
|
|
4039
|
+
(i32.store (i32.add (i32.add (local.get $p) (i32.shl (local.get $len) (i32.const 3))) (i32.const 4)) (local.get $ptr))
|
|
4040
|
+
(global.set $g_dstack_len (i32.add (local.get $len) (i32.const 1)))
|
|
4041
|
+
)
|
|
4042
|
+
|
|
4043
|
+
(func $dstack_pop
|
|
4044
|
+
(local $len i32)
|
|
4045
|
+
(local $addr i32)
|
|
4046
|
+
(local $kind i32)
|
|
4047
|
+
(local $ptr i32)
|
|
4048
|
+
(local.set $len (global.get $g_dstack_len))
|
|
4049
|
+
(if (i32.le_s (local.get $len) (i32.const 0)) (then (return)))
|
|
4050
|
+
(local.set $len (i32.sub (local.get $len) (i32.const 1)))
|
|
4051
|
+
(global.set $g_dstack_len (local.get $len))
|
|
4052
|
+
(local.set $addr (i32.add (global.get $g_dstack_ptr) (i32.shl (local.get $len) (i32.const 3))))
|
|
4053
|
+
(local.set $kind (i32.load (local.get $addr)))
|
|
4054
|
+
(local.set $ptr (i32.load (i32.add (local.get $addr) (i32.const 4))))
|
|
4055
|
+
(if (i32.eqz (local.get $kind))
|
|
4056
|
+
(then (call $chan_unpin (local.get $ptr)))
|
|
4057
|
+
(else (call $node_unpin (local.get $ptr))))
|
|
4058
|
+
)
|
|
4059
|
+
|
|
4060
|
+
(func $dstack_clear
|
|
4061
|
+
(block $bc
|
|
4062
|
+
(loop $lc
|
|
4063
|
+
(br_if $bc (i32.le_s (global.get $g_dstack_len) (i32.const 0)))
|
|
4064
|
+
(call $dstack_pop)
|
|
4065
|
+
(br $lc)
|
|
4066
|
+
)
|
|
4067
|
+
)
|
|
4068
|
+
)
|
|
4069
|
+
|
|
4070
|
+
(func $drive_to_completion (param $rootOut i32)
|
|
4071
|
+
(local $topAddr i32)
|
|
4072
|
+
(local $topKind i32)
|
|
4073
|
+
(local $topPtr i32)
|
|
4074
|
+
(local $targetNode i32)
|
|
4075
|
+
(local $outcome i32)
|
|
4076
|
+
(local $pullCh i32)
|
|
4077
|
+
|
|
4078
|
+
(block $bouter
|
|
4079
|
+
(loop $louter
|
|
4080
|
+
(br_if $bouter (i32.ne (global.get $g_error_code) (i32.const 0)))
|
|
4081
|
+
(br_if $bouter (i32.ne (i32.load (local.get $rootOut)) (i32.const 0)))
|
|
4082
|
+
(if (i32.eqz (i32.load (i32.add (local.get $rootOut) (i32.const 16))))
|
|
4083
|
+
(then
|
|
4084
|
+
(global.set $g_error_code (i32.const -7))
|
|
4085
|
+
(call $dstack_clear)
|
|
4086
|
+
(return)))
|
|
4087
|
+
(call $dstack_clear)
|
|
4088
|
+
(call $dstack_push (i32.const 0) (local.get $rootOut))
|
|
4089
|
+
|
|
4090
|
+
(block $binner
|
|
4091
|
+
(loop $linner
|
|
4092
|
+
;; Safepoint: reclaim retired nodes, released channels, and destroyed child scopes
|
|
4093
|
+
(if (i32.or
|
|
4094
|
+
(i32.or (i32.ne (global.get $g_dead_nodes) (i32.const 0)) (i32.ne (global.get $g_dead_chans) (i32.const 0)))
|
|
4095
|
+
(i32.ne (global.get $g_dead_scopes) (i32.const 0)))
|
|
4096
|
+
(then (call $flush_dead)))
|
|
4097
|
+
|
|
4098
|
+
(br_if $binner (i32.ne (global.get $g_error_code) (i32.const 0)))
|
|
4099
|
+
(br_if $binner (i32.le_s (global.get $g_dstack_len) (i32.const 0)))
|
|
4100
|
+
(local.set $topAddr (i32.add (global.get $g_dstack_ptr) (i32.shl (i32.sub (global.get $g_dstack_len) (i32.const 1)) (i32.const 3))))
|
|
4101
|
+
(local.set $topKind (i32.load (local.get $topAddr)))
|
|
4102
|
+
(local.set $topPtr (i32.load (i32.add (local.get $topAddr) (i32.const 4))))
|
|
4103
|
+
|
|
4104
|
+
(if (i32.eqz (local.get $topKind))
|
|
4105
|
+
(then
|
|
4106
|
+
;; Channel frame
|
|
4107
|
+
(if (i32.or (i32.ne (i32.load (local.get $topPtr)) (i32.const 0))
|
|
4108
|
+
(i32.eqz (i32.load (i32.add (local.get $topPtr) (i32.const 16)))))
|
|
4109
|
+
(then
|
|
4110
|
+
(call $dstack_pop)
|
|
4111
|
+
(br $linner)))
|
|
4112
|
+
(local.set $targetNode (i32.load (i32.add (local.get $topPtr) (i32.const 16))))
|
|
4113
|
+
)
|
|
4114
|
+
(else
|
|
4115
|
+
;; Node frame
|
|
4116
|
+
(if (i32.ne (i32.load (i32.add (local.get $topPtr) (i32.const 4))) (i32.const 0))
|
|
4117
|
+
(then
|
|
4118
|
+
(call $dstack_pop)
|
|
4119
|
+
(br $linner)))
|
|
4120
|
+
(local.set $targetNode (local.get $topPtr))
|
|
4121
|
+
)
|
|
4122
|
+
)
|
|
4123
|
+
|
|
4124
|
+
(local.set $outcome (call $step_node (local.get $targetNode)))
|
|
4125
|
+
(br_if $binner (i32.ne (global.get $g_error_code) (i32.const 0)))
|
|
4126
|
+
|
|
4127
|
+
(if (i32.eq (local.get $outcome) (i32.const 1))
|
|
4128
|
+
(then
|
|
4129
|
+
(local.set $pullCh (global.get $g_need_pull))
|
|
4130
|
+
(if (i32.and (i32.eq (i32.load (local.get $pullCh)) (i32.const 0))
|
|
4131
|
+
(i32.eqz (i32.load (i32.add (local.get $pullCh) (i32.const 16)))))
|
|
4132
|
+
(then
|
|
4133
|
+
(global.set $g_error_code (i32.const -7))
|
|
4134
|
+
(call $dstack_clear)
|
|
4135
|
+
(return)))
|
|
4136
|
+
(call $dstack_push (i32.const 0) (local.get $pullCh))
|
|
4137
|
+
(br $linner)))
|
|
4138
|
+
|
|
4139
|
+
(if (i32.eq (local.get $outcome) (i32.const 3))
|
|
4140
|
+
(then
|
|
4141
|
+
(call $dstack_push (i32.const 1) (global.get $g_need_step_node))
|
|
4142
|
+
(br $linner)))
|
|
4143
|
+
|
|
4144
|
+
(if (i32.eq (local.get $outcome) (i32.const 2))
|
|
4145
|
+
(then
|
|
4146
|
+
(call $dstack_pop)
|
|
4147
|
+
(br $linner)))
|
|
4148
|
+
|
|
4149
|
+
;; Normal progress
|
|
4150
|
+
(if (i32.eqz (local.get $topKind))
|
|
4151
|
+
(then
|
|
4152
|
+
(if (i32.or (i32.ne (i32.load (local.get $topPtr)) (i32.const 0))
|
|
4153
|
+
(i32.and (i32.gt_s (global.get $g_dstack_len) (i32.const 1))
|
|
4154
|
+
(i32.gt_s (i32.load (i32.add (local.get $topPtr) (i32.const 44))) (i32.const 0))))
|
|
4155
|
+
(then (call $dstack_pop)))
|
|
4156
|
+
)
|
|
4157
|
+
(else
|
|
4158
|
+
(if (i32.ne (i32.load (i32.add (local.get $topPtr) (i32.const 4))) (i32.const 0))
|
|
4159
|
+
(then (call $dstack_pop)))
|
|
4160
|
+
)
|
|
4161
|
+
)
|
|
4162
|
+
(br $linner)
|
|
4163
|
+
)
|
|
4164
|
+
)
|
|
4165
|
+
(br $louter)
|
|
4166
|
+
)
|
|
4167
|
+
)
|
|
4168
|
+
(call $dstack_clear)
|
|
4169
|
+
)
|
|
4170
|
+
|
|
4171
|
+
;; Bulk load raw bytes into a BitDeque starting at `bitOffset` for `totalBits` bits
|
|
4172
|
+
(func $bd_load_bytes_at (param $ch i32) (param $bytePtr i32) (param $bitOff i32) (param $totalBits i32)
|
|
4173
|
+
(local $rem i32)
|
|
4174
|
+
(local $pos i32)
|
|
4175
|
+
(local $bIdx i32)
|
|
4176
|
+
(local $bOff i32)
|
|
4177
|
+
(local $avail i32)
|
|
4178
|
+
(local $take i32)
|
|
4179
|
+
(local $byteVal i32)
|
|
4180
|
+
(local $bits i32)
|
|
4181
|
+
(local $acc i32)
|
|
4182
|
+
(local $accW i32)
|
|
4183
|
+
|
|
4184
|
+
;; Grow once upfront if needed!
|
|
4185
|
+
(if (i32.gt_s (local.get $totalBits) (i32.shl (i32.load (i32.add (local.get $ch) (i32.const 36))) (i32.const 5)))
|
|
4186
|
+
(then
|
|
4187
|
+
(call $bd_grow (local.get $ch) (local.get $totalBits))
|
|
4188
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return)))))
|
|
4189
|
+
|
|
4190
|
+
(local.set $rem (local.get $totalBits))
|
|
4191
|
+
(local.set $pos (local.get $bitOff))
|
|
4192
|
+
(local.set $acc (i32.const 0))
|
|
4193
|
+
(local.set $accW (i32.const 0))
|
|
4194
|
+
|
|
4195
|
+
(block $brk
|
|
4196
|
+
(loop $lp
|
|
4197
|
+
(br_if $brk (i32.le_s (local.get $rem) (i32.const 0)))
|
|
4198
|
+
(local.set $bIdx (i32.shr_u (local.get $pos) (i32.const 3)))
|
|
4199
|
+
(local.set $bOff (i32.and (local.get $pos) (i32.const 7)))
|
|
4200
|
+
(local.set $avail (i32.sub (i32.const 8) (local.get $bOff)))
|
|
4201
|
+
(local.set $take (select (local.get $rem) (local.get $avail) (i32.lt_s (local.get $rem) (local.get $avail))))
|
|
4202
|
+
(local.set $byteVal (i32.load8_u (i32.add (local.get $bytePtr) (local.get $bIdx))))
|
|
4203
|
+
(local.set $bits
|
|
4204
|
+
(i32.and
|
|
4205
|
+
(i32.shr_u (local.get $byteVal) (i32.sub (local.get $avail) (local.get $take)))
|
|
4206
|
+
(call $mask32 (local.get $take))))
|
|
4207
|
+
(local.set $acc (i32.or (i32.shl (local.get $acc) (local.get $take)) (local.get $bits)))
|
|
4208
|
+
(local.set $accW (i32.add (local.get $accW) (local.get $take)))
|
|
4209
|
+
(local.set $pos (i32.add (local.get $pos) (local.get $take)))
|
|
4210
|
+
(local.set $rem (i32.sub (local.get $rem) (local.get $take)))
|
|
4211
|
+
(if (i32.ge_s (local.get $accW) (i32.const 24))
|
|
4212
|
+
(then
|
|
4213
|
+
(call $bd_push_small (local.get $ch) (local.get $acc) (local.get $accW))
|
|
4214
|
+
(local.set $acc (i32.const 0))
|
|
4215
|
+
(local.set $accW (i32.const 0))))
|
|
4216
|
+
(br $lp)
|
|
4217
|
+
)
|
|
4218
|
+
)
|
|
4219
|
+
(if (i32.gt_s (local.get $accW) (i32.const 0))
|
|
4220
|
+
(then (call $bd_push_small (local.get $ch) (local.get $acc) (local.get $accW))))
|
|
4221
|
+
)
|
|
4222
|
+
|
|
4223
|
+
;; Write BitDeque contents as zero-padded bytes to `outPtr` (returns byte length or -5 if > outCap)
|
|
4224
|
+
(func $bd_store_padded_bytes (param $ch i32) (param $outPtr i32) (param $outCap i32) (result i32)
|
|
4225
|
+
(local $totalBits i32)
|
|
4226
|
+
(local $nBytes i32)
|
|
4227
|
+
(local $i i32)
|
|
4228
|
+
(local $rem i32)
|
|
4229
|
+
(local $b i32)
|
|
4230
|
+
(local.set $totalBits (i32.load (i32.add (local.get $ch) (i32.const 44))))
|
|
4231
|
+
(local.set $nBytes (i32.shr_u (i32.add (local.get $totalBits) (i32.const 7)) (i32.const 3)))
|
|
4232
|
+
(if (i32.gt_s (local.get $nBytes) (local.get $outCap))
|
|
4233
|
+
(then (return (i32.const -5))))
|
|
4234
|
+
(local.set $i (i32.const 0))
|
|
4235
|
+
(block $brk
|
|
4236
|
+
(loop $lp
|
|
4237
|
+
(local.set $rem (i32.load (i32.add (local.get $ch) (i32.const 44))))
|
|
4238
|
+
(br_if $brk (i32.le_s (local.get $rem) (i32.const 0)))
|
|
4239
|
+
(if (i32.ge_s (local.get $rem) (i32.const 8))
|
|
4240
|
+
(then (local.set $b (call $bd_shift_bits (local.get $ch) (i32.const 8))))
|
|
4241
|
+
(else (local.set $b (i32.shl (call $bd_shift_bits (local.get $ch) (local.get $rem)) (i32.sub (i32.const 8) (local.get $rem))))))
|
|
4242
|
+
(i32.store8 (i32.add (local.get $outPtr) (local.get $i)) (local.get $b))
|
|
4243
|
+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
|
4244
|
+
(br $lp)
|
|
4245
|
+
)
|
|
4246
|
+
)
|
|
4247
|
+
(local.get $nBytes)
|
|
4248
|
+
)
|
|
4249
|
+
|
|
4250
|
+
;; ============================================================================
|
|
4251
|
+
;; 11. Exported Entrypoints, Quota Setters & Telemetry Getters
|
|
4252
|
+
;; ============================================================================
|
|
4253
|
+
|
|
4254
|
+
(func (export "ktvm_set_quotas") (param $maxSteps i32) (param $maxNodes i32) (param $maxBytes i32)
|
|
4255
|
+
(global.set $g_max_steps (local.get $maxSteps))
|
|
4256
|
+
(global.set $g_max_nodes (local.get $maxNodes))
|
|
4257
|
+
(global.set $g_max_bytes (local.get $maxBytes))
|
|
4258
|
+
)
|
|
4259
|
+
|
|
4260
|
+
(func (export "ktvm_get_io_base") (result i32)
|
|
4261
|
+
(global.get $DATA_IO_BASE)
|
|
4262
|
+
)
|
|
4263
|
+
|
|
4264
|
+
(func (export "ktvm_get_steps") (result i32) (global.get $g_steps))
|
|
4265
|
+
(func (export "ktvm_get_peak_nodes") (result i32) (global.get $g_peak_nodes))
|
|
4266
|
+
(func (export "ktvm_get_peak_bytes") (result i32) (global.get $g_peak_bytes))
|
|
4267
|
+
(func (export "ktvm_get_table_lookups") (result i32) (global.get $g_table_lookups))
|
|
4268
|
+
(func (export "ktvm_get_splices") (result i32) (global.get $g_splices))
|
|
4269
|
+
(func (export "ktvm_get_mux_demux_fusions") (result i32) (global.get $g_mux_demux_fusions))
|
|
4270
|
+
(func (export "ktvm_get_spec_rollbacks") (result i32) (global.get $g_spec_rollbacks))
|
|
4271
|
+
(func (export "ktvm_get_spec_upstream_aborts") (result i32) (global.get $g_spec_upstream_aborts))
|
|
4272
|
+
(func (export "ktvm_get_spec_tail_splices") (result i32) (global.get $g_spec_tail_splices))
|
|
4273
|
+
(func (export "ktvm_get_after_settlements") (result i32) (global.get $g_after_settlements))
|
|
4274
|
+
|
|
4275
|
+
;; Reserve `ioBytes` bytes starting at `$DATA_IO_BASE` for host input+output wire buffers,
|
|
4276
|
+
;; placing `$g_heap_base` right after the I/O region and growing linear memory if needed.
|
|
4277
|
+
(func (export "ktvm_reserve_io") (param $ioBytes i32) (result i32)
|
|
4278
|
+
(local $end i32)
|
|
4279
|
+
(local $limit i32)
|
|
4280
|
+
(local $needPages i32)
|
|
4281
|
+
(local.set $end
|
|
4282
|
+
(i32.and
|
|
4283
|
+
(i32.add (i32.add (global.get $DATA_IO_BASE) (local.get $ioBytes)) (i32.const 15))
|
|
4284
|
+
(i32.const -16)))
|
|
4285
|
+
(local.set $limit (i32.shl (memory.size) (i32.const 16)))
|
|
4286
|
+
(if (i32.gt_u (i32.add (local.get $end) (i32.const 65536)) (local.get $limit))
|
|
4287
|
+
(then
|
|
4288
|
+
(local.set $needPages
|
|
4289
|
+
(i32.shr_u
|
|
4290
|
+
(i32.add (i32.sub (i32.add (local.get $end) (i32.const 65536)) (local.get $limit)) (i32.const 65535))
|
|
4291
|
+
(i32.const 16)))
|
|
4292
|
+
(if (i32.eq (memory.grow (local.get $needPages)) (i32.const -1))
|
|
4293
|
+
(then (return (i32.const 0))))))
|
|
4294
|
+
(global.set $g_heap_base (local.get $end))
|
|
4295
|
+
(global.get $DATA_IO_BASE)
|
|
4296
|
+
)
|
|
4297
|
+
|
|
4298
|
+
;; Reset arena + telemetry before each run (1ns reset!)
|
|
4299
|
+
(func $reset_runtime
|
|
4300
|
+
(if (i32.eqz (global.get $g_heap_base))
|
|
4301
|
+
(then (global.set $g_heap_base (global.get $DATA_HEAP_BASE))))
|
|
4302
|
+
(global.set $g_heap_ptr (global.get $g_heap_base))
|
|
4303
|
+
(global.set $g_heap_limit (i32.shl (memory.size) (i32.const 16)))
|
|
4304
|
+
(global.set $g_error_code (i32.const 0))
|
|
4305
|
+
(global.set $g_need_pull (i32.const 0))
|
|
4306
|
+
(global.set $g_need_step_node (i32.const 0))
|
|
4307
|
+
(global.set $g_steps (i32.const 0))
|
|
4308
|
+
(global.set $g_live_nodes (i32.const 0))
|
|
4309
|
+
(global.set $g_peak_nodes (i32.const 0))
|
|
4310
|
+
(global.set $g_buf_bytes (i32.const 0))
|
|
4311
|
+
(global.set $g_peak_bytes (i32.const 0))
|
|
4312
|
+
(global.set $g_table_lookups (i32.const 0))
|
|
4313
|
+
(global.set $g_splices (i32.const 0))
|
|
4314
|
+
(global.set $g_mux_demux_fusions (i32.const 0))
|
|
4315
|
+
(global.set $g_spec_rollbacks (i32.const 0))
|
|
4316
|
+
(global.set $g_spec_upstream_aborts (i32.const 0))
|
|
4317
|
+
(global.set $g_spec_tail_splices (i32.const 0))
|
|
4318
|
+
(global.set $g_after_settlements (i32.const 0))
|
|
4319
|
+
(global.set $g_dstack_ptr (i32.const 0))
|
|
4320
|
+
(global.set $g_dstack_cap (i32.const 0))
|
|
4321
|
+
(global.set $g_dstack_len (i32.const 0))
|
|
4322
|
+
(global.set $g_sc_stk_ptr (i32.const 0))
|
|
4323
|
+
(global.set $g_sc_ord_ptr (i32.const 0))
|
|
4324
|
+
(global.set $g_sc_cap (i32.const 0))
|
|
4325
|
+
(global.set $g_slab_fl_ptr (i32.const 0))
|
|
4326
|
+
(global.set $g_dead_nodes (i32.const 0))
|
|
4327
|
+
(global.set $g_dead_chans (i32.const 0))
|
|
4328
|
+
(global.set $g_dead_scopes (i32.const 0))
|
|
4329
|
+
(global.set $g_sw_tags_ptr (i32.const 0))
|
|
4330
|
+
(global.set $g_sw_tags_cap (i32.const 0))
|
|
4331
|
+
(global.set $g_slab_fl_ptr (call $alloc (i32.const 68)))
|
|
4332
|
+
)
|
|
4333
|
+
|
|
4334
|
+
;; Run entry on raw payload bits at `(inPtr, inBitOff, inBitLen)`, optionally validating exact
|
|
4335
|
+
;; DPDA payload length & zero trailing pad (`validatePad=1`), and write complete enveloped
|
|
4336
|
+
;; k-wire (`[outHeaderBits][payloadBits][0-pad]`) to `(outPtr, outCap)`.
|
|
4337
|
+
;; Returns: >= 0 output byte length, or negative error code (-1=FAILED, -2..-7).
|
|
4338
|
+
(func (export "ktvm_run_wire")
|
|
4339
|
+
(param $inPtr i32) (param $inBitOff i32) (param $inBitLen i32) (param $validatePad i32)
|
|
4340
|
+
(param $outPtr i32) (param $outCap i32) (param $frameHeader i32)
|
|
4341
|
+
(result i32)
|
|
4342
|
+
(local $rootSc i32)
|
|
4343
|
+
(local $rootIn i32)
|
|
4344
|
+
(local $rootOut i32)
|
|
4345
|
+
(local $dpda i32)
|
|
4346
|
+
(local $res i32)
|
|
4347
|
+
(local $rem i32)
|
|
4348
|
+
(local $framed i32)
|
|
4349
|
+
(local $n i32)
|
|
4350
|
+
|
|
4351
|
+
(call $reset_runtime)
|
|
4352
|
+
(local.set $rootSc (call $scope_create (i32.const 0)))
|
|
4353
|
+
(local.set $rootIn (call $chan_alloc (local.get $rootSc) (global.get $DATA_IN_SCHEMA_ID)))
|
|
4354
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (global.get $g_error_code))))
|
|
4355
|
+
|
|
4356
|
+
;; Validate through the input channel cursor, then rewind over the same buffer.
|
|
4357
|
+
(call $bd_load_bytes_at (local.get $rootIn) (local.get $inPtr) (local.get $inBitOff) (local.get $inBitLen))
|
|
4358
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (global.get $g_error_code))))
|
|
4359
|
+
(if (local.get $validatePad)
|
|
4360
|
+
(then
|
|
4361
|
+
(local.set $dpda (call $dpda_alloc (i32.const 0) (global.get $DATA_IN_SCHEMA_ID) (i32.const 0)))
|
|
4362
|
+
(local.set $res (call $dpda_step_skip_copy (local.get $rootIn) (i32.const 0) (local.get $dpda)))
|
|
4363
|
+
(call $dpda_free (local.get $dpda))
|
|
4364
|
+
(if (i32.ne (local.get $res) (i32.const 0)) (then (return (i32.const -6))))
|
|
4365
|
+
(local.set $rem (i32.load (i32.add (local.get $rootIn) (i32.const 44))))
|
|
4366
|
+
(if (i32.ge_s (local.get $rem) (i32.const 8)) (then (return (i32.const -6))))
|
|
4367
|
+
(if (i32.ne (call $bd_shift_bits (local.get $rootIn) (local.get $rem)) (i32.const 0))
|
|
4368
|
+
(then (return (i32.const -6))))
|
|
4369
|
+
(i32.store (i32.add (local.get $rootIn) (i32.const 40)) (i32.const 0))
|
|
4370
|
+
(i32.store (i32.add (local.get $rootIn) (i32.const 44)) (i32.sub (local.get $inBitLen) (local.get $rem)))))
|
|
4371
|
+
(call $bd_close (local.get $rootIn))
|
|
4372
|
+
|
|
4373
|
+
(local.set $rootOut (call $chan_alloc (i32.const 0) (global.get $DATA_OUT_SCHEMA_ID)))
|
|
4374
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (global.get $g_error_code))))
|
|
4375
|
+
|
|
4376
|
+
;; Spawn entry proc or instantiate entry kernel
|
|
4377
|
+
(call $start_entry (local.get $rootSc) (local.get $rootIn) (local.get $rootOut))
|
|
4378
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (global.get $g_error_code))))
|
|
4379
|
+
|
|
4380
|
+
(call $drive_to_completion (local.get $rootOut))
|
|
4381
|
+
(if (i32.ne (global.get $g_error_code) (i32.const 0)) (then (return (global.get $g_error_code))))
|
|
4382
|
+
|
|
4383
|
+
(if (i32.eq (i32.load (local.get $rootOut)) (i32.const 2))
|
|
4384
|
+
(then
|
|
4385
|
+
(call $scope_destroy (local.get $rootSc))
|
|
4386
|
+
(return (i32.const -1)))) ;; FAILED
|
|
4387
|
+
|
|
4388
|
+
;; Optionally prepend static output k-wire header bits
|
|
4389
|
+
(if (local.get $frameHeader)
|
|
4390
|
+
(then
|
|
4391
|
+
(local.set $framed (call $chan_alloc (i32.const 0) (i32.const 0)))
|
|
4392
|
+
(call $bd_load_bytes_at (local.get $framed) (global.get $DATA_OUT_HDR_PTR) (i32.const 0) (global.get $DATA_OUT_HDR_BITS))
|
|
4393
|
+
(call $bd_push_from_deque (local.get $framed) (local.get $rootOut))
|
|
4394
|
+
(local.set $res (call $bd_store_padded_bytes (local.get $framed) (local.get $outPtr) (local.get $outCap)))
|
|
4395
|
+
(call $bd_release (local.get $framed))
|
|
4396
|
+
)
|
|
4397
|
+
(else
|
|
4398
|
+
(local.set $res (call $bd_store_padded_bytes (local.get $rootOut) (local.get $outPtr) (local.get $outCap)))
|
|
4399
|
+
)
|
|
4400
|
+
)
|
|
4401
|
+
(call $bd_release (local.get $rootOut))
|
|
4402
|
+
(call $scope_destroy (local.get $rootSc))
|
|
4403
|
+
(local.get $res)
|
|
4404
|
+
)
|