@fraczak/k 8.9.0 → 9.0.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 +17 -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 +902 -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 +6 -4
- package/DOCS/README.md +3 -2
- package/DOCS/REPL.md +16 -12
- package/README.md +132 -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 -5
- package/backends/llvm/src/llvm.mjs +4 -111
- 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 +0 -16
- package/backends/wasm/README.md +2 -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 +10 -56
- 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/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 +482 -0
- package/ktvm/fixtures/integer-streaming.k +477 -0
- package/ktvm/index.mjs +119 -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 +543 -0
- package/ktvm/specialize.mjs +538 -0
- package/ktvm/test-conformance.mjs +597 -0
- package/ktvm/test-ktvm.mjs +1376 -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 +4413 -0
- package/ktvm/wasm/test-wasm.mjs +426 -0
- package/ktvm/wire.mjs +489 -0
- package/package.json +1 -9
- 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 +9 -229
- package/scripts/perf-int.mjs +14 -272
- package/scripts/perf-multiplication.mjs +8 -54
- 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,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kTVM Channel ADT (`BitDeque`), Kernel-Local LIFO `BitStack`, and `QuotaTracker`.
|
|
3
|
+
*
|
|
4
|
+
* Implements DOCS/KTVM_SPEC.md §3:
|
|
5
|
+
* - SPSC bit-addressable double-ended queue with power-of-2 Uint32Array ring buffer
|
|
6
|
+
* - Dual schema state `(prodSchema, consSchema)` + `aligned` boundary flag
|
|
7
|
+
* - Strictly monotone status lifecycle: `OPEN -> CLOSED | FAILED`
|
|
8
|
+
* - Global byte / node / step quota enforcement (`KTVMQuotaError`)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { KTVMQuotaError } from "./ast.mjs";
|
|
12
|
+
|
|
13
|
+
const MASKS = new Uint32Array(32);
|
|
14
|
+
for (let i = 1; i <= 30; i++) MASKS[i] = ((1 << i) - 1) >>> 0;
|
|
15
|
+
MASKS[31] = 0x7fffffff;
|
|
16
|
+
|
|
17
|
+
export const CHAN_STATUS = Object.freeze({
|
|
18
|
+
OPEN: "OPEN",
|
|
19
|
+
CLOSED: "CLOSED",
|
|
20
|
+
FAILED: "FAILED"
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export class QuotaTracker {
|
|
24
|
+
constructor({
|
|
25
|
+
maxBitDequeBytes = Infinity,
|
|
26
|
+
maxNodes = Infinity,
|
|
27
|
+
maxSteps = Infinity
|
|
28
|
+
} = {}) {
|
|
29
|
+
this.maxBitDequeBytes = maxBitDequeBytes;
|
|
30
|
+
this.maxNodes = maxNodes;
|
|
31
|
+
this.maxSteps = maxSteps;
|
|
32
|
+
|
|
33
|
+
this.liveBitDequeBytes = 0;
|
|
34
|
+
this.peakBitDequeBytes = 0;
|
|
35
|
+
this.liveNodes = 0;
|
|
36
|
+
this.peakNodes = 0;
|
|
37
|
+
this.steps = 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
addBufferBytes(delta) {
|
|
41
|
+
this.liveBitDequeBytes += delta;
|
|
42
|
+
if (this.liveBitDequeBytes > this.peakBitDequeBytes) {
|
|
43
|
+
this.peakBitDequeBytes = this.liveBitDequeBytes;
|
|
44
|
+
}
|
|
45
|
+
if (this.liveBitDequeBytes > this.maxBitDequeBytes) {
|
|
46
|
+
throw new KTVMQuotaError("maxBitDequeBytes", this.maxBitDequeBytes, this.liveBitDequeBytes);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
addNode() {
|
|
51
|
+
this.liveNodes += 1;
|
|
52
|
+
if (this.liveNodes > this.peakNodes) {
|
|
53
|
+
this.peakNodes = this.liveNodes;
|
|
54
|
+
}
|
|
55
|
+
if (this.liveNodes > this.maxNodes) {
|
|
56
|
+
throw new KTVMQuotaError("maxNodes", this.maxNodes, this.liveNodes);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
removeNode() {
|
|
61
|
+
if (this.liveNodes > 0) this.liveNodes -= 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
tickStep() {
|
|
65
|
+
this.steps += 1;
|
|
66
|
+
if (this.steps > this.maxSteps) {
|
|
67
|
+
throw new KTVMQuotaError("maxSteps", this.maxSteps, this.steps);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class BitDeque {
|
|
73
|
+
constructor(
|
|
74
|
+
id,
|
|
75
|
+
{
|
|
76
|
+
name = "",
|
|
77
|
+
prodSchema = null,
|
|
78
|
+
prodNodeId = 0,
|
|
79
|
+
consSchema = null,
|
|
80
|
+
consNodeId = 0,
|
|
81
|
+
aligned = true,
|
|
82
|
+
prod = null,
|
|
83
|
+
cons = null,
|
|
84
|
+
scope = 0,
|
|
85
|
+
quota = null,
|
|
86
|
+
initialWords = 4
|
|
87
|
+
} = {}
|
|
88
|
+
) {
|
|
89
|
+
this.id = id;
|
|
90
|
+
this.name = name || `%c${id}`;
|
|
91
|
+
this.status = CHAN_STATUS.OPEN;
|
|
92
|
+
|
|
93
|
+
// Dual schemas: upstream suffix obligation (`prodSchema`) vs. downstream view (`consSchema`)
|
|
94
|
+
this.prodSchema = prodSchema;
|
|
95
|
+
this.prodNodeId = prodNodeId;
|
|
96
|
+
this.consSchema = consSchema ?? prodSchema;
|
|
97
|
+
this.consNodeId = consSchema ? consNodeId : prodNodeId;
|
|
98
|
+
this.aligned = aligned;
|
|
99
|
+
|
|
100
|
+
this.prod = prod;
|
|
101
|
+
this.cons = cons;
|
|
102
|
+
this.scope = scope;
|
|
103
|
+
this.quota = quota;
|
|
104
|
+
this.retired = false;
|
|
105
|
+
|
|
106
|
+
this.words = new Uint32Array(initialWords);
|
|
107
|
+
this.wordMask = initialWords - 1;
|
|
108
|
+
this.capBits = initialWords << 5;
|
|
109
|
+
this.bitMask = this.capBits - 1;
|
|
110
|
+
this.headBit = 0;
|
|
111
|
+
this.lenBits = 0;
|
|
112
|
+
|
|
113
|
+
if (this.quota) {
|
|
114
|
+
this.quota.addBufferBytes(this.words.byteLength);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get length() {
|
|
119
|
+
return this.lenBits;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get isOpen() {
|
|
123
|
+
return this.status === CHAN_STATUS.OPEN;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get isClosed() {
|
|
127
|
+
return this.status === CHAN_STATUS.CLOSED;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get isFailed() {
|
|
131
|
+
return this.status === CHAN_STATUS.FAILED;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
hasBits(n = 1) {
|
|
135
|
+
return this.lenBits >= n;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_grow(minNeededBits) {
|
|
139
|
+
let newWordsLen = this.words.length << 1;
|
|
140
|
+
while ((newWordsLen << 5) < minNeededBits) newWordsLen <<= 1;
|
|
141
|
+
const oldWords = this.words;
|
|
142
|
+
const oldWordMask = this.wordMask;
|
|
143
|
+
const oldBitMask = this.bitMask;
|
|
144
|
+
const oldHead = this.headBit;
|
|
145
|
+
const oldLen = this.lenBits;
|
|
146
|
+
const oldBytes = oldWords.byteLength;
|
|
147
|
+
|
|
148
|
+
const newWords = new Uint32Array(newWordsLen);
|
|
149
|
+
if (this.quota) {
|
|
150
|
+
this.quota.addBufferBytes(newWords.byteLength - oldBytes);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let rem = oldLen;
|
|
154
|
+
let srcPos = oldHead;
|
|
155
|
+
let dstPos = 0;
|
|
156
|
+
while (rem > 0) {
|
|
157
|
+
const chunk = rem > 30 ? 30 : rem;
|
|
158
|
+
const wi = srcPos >>> 5;
|
|
159
|
+
const bo = srcPos & 31;
|
|
160
|
+
let val;
|
|
161
|
+
if (bo + chunk <= 32) {
|
|
162
|
+
val = (oldWords[wi] >>> (32 - bo - chunk)) & MASKS[chunk];
|
|
163
|
+
} else {
|
|
164
|
+
const lo = bo + chunk - 32;
|
|
165
|
+
const hiBits = 32 - bo;
|
|
166
|
+
const w1 = (wi + 1) & oldWordMask;
|
|
167
|
+
val = (((oldWords[wi] & MASKS[hiBits]) << lo) | (oldWords[w1] >>> (32 - lo))) >>> 0;
|
|
168
|
+
}
|
|
169
|
+
const dwi = dstPos >>> 5;
|
|
170
|
+
const dbo = dstPos & 31;
|
|
171
|
+
if (dbo + chunk <= 32) {
|
|
172
|
+
newWords[dwi] |= (val << (32 - dbo - chunk)) >>> 0;
|
|
173
|
+
} else {
|
|
174
|
+
const lo = dbo + chunk - 32;
|
|
175
|
+
newWords[dwi] |= (val >>> lo) >>> 0;
|
|
176
|
+
newWords[dwi + 1] |= (val << (32 - lo)) >>> 0;
|
|
177
|
+
}
|
|
178
|
+
srcPos = (srcPos + chunk) & oldBitMask;
|
|
179
|
+
dstPos += chunk;
|
|
180
|
+
rem -= chunk;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.words = newWords;
|
|
184
|
+
this.wordMask = newWordsLen - 1;
|
|
185
|
+
this.capBits = newWordsLen << 5;
|
|
186
|
+
this.bitMask = this.capBits - 1;
|
|
187
|
+
this.headBit = 0;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
_pushSmall(val, width) {
|
|
191
|
+
if (width <= 0) return;
|
|
192
|
+
if (this.lenBits + width > this.capBits) {
|
|
193
|
+
this._grow(this.lenBits + width);
|
|
194
|
+
}
|
|
195
|
+
const v = (val >>> 0) & MASKS[width];
|
|
196
|
+
const tailPos = (this.headBit + this.lenBits) & this.bitMask;
|
|
197
|
+
const wi = tailPos >>> 5;
|
|
198
|
+
const bo = tailPos & 31;
|
|
199
|
+
if (bo + width <= 32) {
|
|
200
|
+
const shift = 32 - bo - width;
|
|
201
|
+
const clearMask = ~((MASKS[width] << shift) >>> 0);
|
|
202
|
+
this.words[wi] = ((this.words[wi] & clearMask) | ((v << shift) >>> 0)) >>> 0;
|
|
203
|
+
} else {
|
|
204
|
+
const lo = bo + width - 32;
|
|
205
|
+
const hiBits = 32 - bo;
|
|
206
|
+
const w1 = (wi + 1) & this.wordMask;
|
|
207
|
+
const hiMask = ~MASKS[hiBits];
|
|
208
|
+
this.words[wi] = ((this.words[wi] & hiMask) | (v >>> lo)) >>> 0;
|
|
209
|
+
const loShift = 32 - lo;
|
|
210
|
+
const loMask = ~((MASKS[lo] << loShift) >>> 0);
|
|
211
|
+
this.words[w1] = ((this.words[w1] & loMask) | ((v << loShift) >>> 0)) >>> 0;
|
|
212
|
+
}
|
|
213
|
+
this.lenBits += width;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Append `width` bits (0..32, MSB-first) to the back of the buffer. */
|
|
217
|
+
pushBits(val, width) {
|
|
218
|
+
if (width <= 0) return;
|
|
219
|
+
if (width <= 30) {
|
|
220
|
+
this._pushSmall(val, width);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const hiWidth = width - 16;
|
|
224
|
+
this._pushSmall((val >>> 16) >>> 0, hiWidth);
|
|
225
|
+
this._pushSmall(val & 0xffff, 16);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
_unshiftSmall(val, width) {
|
|
229
|
+
if (width <= 0) return;
|
|
230
|
+
if (this.lenBits + width > this.capBits) {
|
|
231
|
+
this._grow(this.lenBits + width);
|
|
232
|
+
}
|
|
233
|
+
const v = (val >>> 0) & MASKS[width];
|
|
234
|
+
const newHead = (this.headBit - width) & this.bitMask;
|
|
235
|
+
const wi = newHead >>> 5;
|
|
236
|
+
const bo = newHead & 31;
|
|
237
|
+
if (bo + width <= 32) {
|
|
238
|
+
const shift = 32 - bo - width;
|
|
239
|
+
const clearMask = ~((MASKS[width] << shift) >>> 0);
|
|
240
|
+
this.words[wi] = ((this.words[wi] & clearMask) | ((v << shift) >>> 0)) >>> 0;
|
|
241
|
+
} else {
|
|
242
|
+
const lo = bo + width - 32;
|
|
243
|
+
const hiBits = 32 - bo;
|
|
244
|
+
const w1 = (wi + 1) & this.wordMask;
|
|
245
|
+
const hiMask = ~MASKS[hiBits];
|
|
246
|
+
this.words[wi] = ((this.words[wi] & hiMask) | (v >>> lo)) >>> 0;
|
|
247
|
+
const loShift = 32 - lo;
|
|
248
|
+
const loMask = ~((MASKS[lo] << loShift) >>> 0);
|
|
249
|
+
this.words[w1] = ((this.words[w1] & loMask) | ((v << loShift) >>> 0)) >>> 0;
|
|
250
|
+
}
|
|
251
|
+
this.headBit = newHead;
|
|
252
|
+
this.lenBits += width;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Prepend `width` bits (0..32, MSB-first) onto the front of the buffer. */
|
|
256
|
+
unshiftBits(val, width) {
|
|
257
|
+
if (width <= 0) return;
|
|
258
|
+
if (width <= 30) {
|
|
259
|
+
this._unshiftSmall(val, width);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
// Prepend low 16 bits first, then high bits in front of them
|
|
263
|
+
const hiWidth = width - 16;
|
|
264
|
+
this._unshiftSmall(val & 0xffff, 16);
|
|
265
|
+
this._unshiftSmall((val >>> 16) >>> 0, hiWidth);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
_peekSmallAt(bitOffset, width) {
|
|
269
|
+
if (width <= 0) return 0;
|
|
270
|
+
const pos = (this.headBit + bitOffset) & this.bitMask;
|
|
271
|
+
const wi = pos >>> 5;
|
|
272
|
+
const bo = pos & 31;
|
|
273
|
+
if (bo + width <= 32) {
|
|
274
|
+
return (this.words[wi] >>> (32 - bo - width)) & MASKS[width];
|
|
275
|
+
}
|
|
276
|
+
const lo = bo + width - 32;
|
|
277
|
+
const hiBits = 32 - bo;
|
|
278
|
+
const w1 = (wi + 1) & this.wordMask;
|
|
279
|
+
return (((this.words[wi] & MASKS[hiBits]) << lo) | (this.words[w1] >>> (32 - lo))) >>> 0;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Non-destructively read `width` bits (0..32) starting at `bitOffset` from front. */
|
|
283
|
+
peekBitsAt(bitOffset, width) {
|
|
284
|
+
if (width <= 0) return 0;
|
|
285
|
+
if (bitOffset < 0 || bitOffset + width > this.lenBits) {
|
|
286
|
+
throw new RangeError(
|
|
287
|
+
`BitDeque.peekBitsAt(${bitOffset}, ${width}) out of bounds (lenBits=${this.lenBits})`
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
if (width <= 30) return this._peekSmallAt(bitOffset, width);
|
|
291
|
+
const hiWidth = width - 16;
|
|
292
|
+
const hi = this._peekSmallAt(bitOffset, hiWidth);
|
|
293
|
+
const lo = this._peekSmallAt(bitOffset + hiWidth, 16);
|
|
294
|
+
return (((hi << 16) >>> 0) | lo) >>> 0;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
peekBits(width) {
|
|
298
|
+
return this.peekBitsAt(0, width);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Read and discard `width` bits (0..32) from front of buffer. */
|
|
302
|
+
shiftBits(width) {
|
|
303
|
+
if (width <= 0) return 0;
|
|
304
|
+
const val = this.peekBitsAt(0, width);
|
|
305
|
+
this.headBit = (this.headBit + width) & this.bitMask;
|
|
306
|
+
this.lenBits -= width;
|
|
307
|
+
return val;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Prepend all bits of `src` in front of `this`, emptying `src`. Returns bits moved. */
|
|
311
|
+
unshiftFromDeque(src) {
|
|
312
|
+
const total = src.lenBits;
|
|
313
|
+
if (total === 0) return 0;
|
|
314
|
+
if (this.lenBits + total > this.capBits) {
|
|
315
|
+
this._grow(this.lenBits + total);
|
|
316
|
+
}
|
|
317
|
+
// Walk backwards from end of `src` in 30-bit chunks, unshifting onto front of `this`.
|
|
318
|
+
let rem = total;
|
|
319
|
+
while (rem > 0) {
|
|
320
|
+
const chunk = rem > 30 ? 30 : rem;
|
|
321
|
+
const offset = rem - chunk;
|
|
322
|
+
const bits = src._peekSmallAt(offset, chunk);
|
|
323
|
+
this._unshiftSmall(bits, chunk);
|
|
324
|
+
rem -= chunk;
|
|
325
|
+
}
|
|
326
|
+
src.headBit = 0;
|
|
327
|
+
src.lenBits = 0;
|
|
328
|
+
return total;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Append all bits of `src` to the back of `this`, emptying `src`. Returns bits moved. */
|
|
332
|
+
pushFromDeque(src) {
|
|
333
|
+
const total = src.lenBits;
|
|
334
|
+
if (total === 0) return 0;
|
|
335
|
+
if (this.lenBits + total > this.capBits) {
|
|
336
|
+
this._grow(this.lenBits + total);
|
|
337
|
+
}
|
|
338
|
+
let moved = 0;
|
|
339
|
+
while (src.lenBits > 0) {
|
|
340
|
+
const chunk = src.lenBits > 30 ? 30 : src.lenBits;
|
|
341
|
+
const bits = src.shiftBits(chunk);
|
|
342
|
+
this._pushSmall(bits, chunk);
|
|
343
|
+
moved += chunk;
|
|
344
|
+
}
|
|
345
|
+
return moved;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Steal the backing buffer of `src` in O(1) (`0` bits copied), emptying `src`. */
|
|
349
|
+
adoptBufferFrom(src) {
|
|
350
|
+
if (this.quota) {
|
|
351
|
+
this.quota.addBufferBytes(-this.words.byteLength);
|
|
352
|
+
}
|
|
353
|
+
this.words = src.words;
|
|
354
|
+
this.wordMask = src.wordMask;
|
|
355
|
+
this.capBits = src.capBits;
|
|
356
|
+
this.bitMask = src.bitMask;
|
|
357
|
+
this.headBit = src.headBit;
|
|
358
|
+
this.lenBits = src.lenBits;
|
|
359
|
+
|
|
360
|
+
// Leave `src` with an empty minimal backing buffer (released cleanly on release())
|
|
361
|
+
src.words = new Uint32Array(0);
|
|
362
|
+
src.wordMask = 0;
|
|
363
|
+
src.capBits = 0;
|
|
364
|
+
src.bitMask = 0;
|
|
365
|
+
src.headBit = 0;
|
|
366
|
+
src.lenBits = 0;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
close() {
|
|
370
|
+
if (this.status === CHAN_STATUS.OPEN) {
|
|
371
|
+
this.status = CHAN_STATUS.CLOSED;
|
|
372
|
+
this.prod = null;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
fail() {
|
|
377
|
+
if (this.status === CHAN_STATUS.OPEN) {
|
|
378
|
+
this.status = CHAN_STATUS.FAILED;
|
|
379
|
+
this.prod = null;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
release() {
|
|
384
|
+
if (this.retired) return;
|
|
385
|
+
this.retired = true;
|
|
386
|
+
if (this.quota && this.words.byteLength > 0) {
|
|
387
|
+
this.quota.addBufferBytes(-this.words.byteLength);
|
|
388
|
+
}
|
|
389
|
+
this.words = new Uint32Array(0);
|
|
390
|
+
this.lenBits = 0;
|
|
391
|
+
this.capBits = 0;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** Debug helper: return unread bits as a '0'/'1' string. */
|
|
395
|
+
toBitString() {
|
|
396
|
+
let out = "";
|
|
397
|
+
for (let i = 0; i < this.lenBits; i++) {
|
|
398
|
+
out += this._peekSmallAt(i, 1) ? "1" : "0";
|
|
399
|
+
}
|
|
400
|
+
return out;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Kernel-local LIFO bit-stack (`stack s : bits`).
|
|
406
|
+
*
|
|
407
|
+
* Bits are stored sequentially in a growable Uint32Array; `push(word, k)` pushes
|
|
408
|
+
* LSB..MSB so `pop(k)` / `peekTop(k)` reads the top `k` bits MSB-first (so
|
|
409
|
+
* `push(0b10, 2); pop(2) === 0b10` and `push(0, 1); push(1, 1); pop(1) === 1`).
|
|
410
|
+
*/
|
|
411
|
+
export class BitStack {
|
|
412
|
+
constructor(quota = null, initialWords = 4) {
|
|
413
|
+
this.quota = quota;
|
|
414
|
+
this.words = new Uint32Array(initialWords);
|
|
415
|
+
this.lenBits = 0;
|
|
416
|
+
if (this.quota) {
|
|
417
|
+
this.quota.addBufferBytes(this.words.byteLength);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
get length() {
|
|
422
|
+
return this.lenBits;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
_ensure(neededBits) {
|
|
426
|
+
if (neededBits <= (this.words.length << 5)) return;
|
|
427
|
+
let newLen = this.words.length << 1;
|
|
428
|
+
while ((newLen << 5) < neededBits) newLen <<= 1;
|
|
429
|
+
const oldBytes = this.words.byteLength;
|
|
430
|
+
const next = new Uint32Array(newLen);
|
|
431
|
+
next.set(this.words);
|
|
432
|
+
this.words = next;
|
|
433
|
+
if (this.quota) {
|
|
434
|
+
this.quota.addBufferBytes(next.byteLength - oldBytes);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
push(word, k) {
|
|
439
|
+
if (k <= 0) return;
|
|
440
|
+
this._ensure(this.lenBits + k);
|
|
441
|
+
for (let i = 0; i < k; i++) {
|
|
442
|
+
const bit = (word >>> i) & 1;
|
|
443
|
+
const pos = this.lenBits++;
|
|
444
|
+
const wi = pos >>> 5;
|
|
445
|
+
const bo = pos & 31;
|
|
446
|
+
if (bit) this.words[wi] = (this.words[wi] | (1 << bo)) >>> 0;
|
|
447
|
+
else this.words[wi] = (this.words[wi] & ~(1 << bo)) >>> 0;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
peekTop(k) {
|
|
452
|
+
if (k <= 0) return 0;
|
|
453
|
+
if (k > this.lenBits) {
|
|
454
|
+
throw new RangeError(`BitStack.peekTop(${k}) underflow (lenBits=${this.lenBits})`);
|
|
455
|
+
}
|
|
456
|
+
let val = 0;
|
|
457
|
+
for (let i = 0; i < k; i++) {
|
|
458
|
+
const pos = this.lenBits - 1 - i;
|
|
459
|
+
const bit = (this.words[pos >>> 5] >>> (pos & 31)) & 1;
|
|
460
|
+
val = ((val << 1) | bit) >>> 0;
|
|
461
|
+
}
|
|
462
|
+
return val;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
pop(k) {
|
|
466
|
+
const val = this.peekTop(k);
|
|
467
|
+
this.lenBits -= k;
|
|
468
|
+
return val;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
clear() {
|
|
472
|
+
this.lenBits = 0;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
release() {
|
|
476
|
+
if (this.quota && this.words.byteLength > 0) {
|
|
477
|
+
this.quota.addBufferBytes(-this.words.byteLength);
|
|
478
|
+
}
|
|
479
|
+
this.words = new Uint32Array(0);
|
|
480
|
+
this.lenBits = 0;
|
|
481
|
+
}
|
|
482
|
+
}
|