@fraczak/k 9.0.0 → 9.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/DOCS/KTVM_SPEC.md +45 -40
- package/DOCS/PROFILING_AND_TRACING.md +7 -0
- package/README.md +6 -0
- package/backends/llvm/README.md +4 -0
- package/backends/llvm/runtime/krt.c +63 -291
- package/backends/llvm/src/executable.mjs +33 -19
- package/backends/llvm/src/kvm2llvm.mjs +0 -2
- package/backends/llvm/src/llvm.mjs +0 -2
- package/backends/llvm/tests/compaction-driver.c +33 -0
- package/backends/llvm/tests/integration.sh +18 -0
- package/backends/llvm/tests/test-llvm.mjs +2 -0
- package/backends/wasm/README.md +5 -0
- package/backends/wasm/src/wasm.mjs +50 -1
- package/backends/wasm/tests/test-wasm.mjs +41 -2
- package/codecs/runtime/codec-sdk.mjs +1 -0
- package/codecs/runtime/prefix-codec.mjs +70 -4
- package/ktvm/bit-deque.mjs +19 -4
- package/ktvm/index.mjs +11 -0
- package/ktvm/schema.mjs +594 -3
- package/ktvm/test-ktvm.mjs +430 -1
- package/ktvm/wasm/runtime.wat +10 -19
- package/ktvm/wasm/test-wasm.mjs +3 -0
- package/ktvm/wire.mjs +222 -32
- package/package.json +1 -1
- 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/scripts/perf-ieee.mjs +5 -3
- package/scripts/perf-int.mjs +2 -2
- package/scripts/perf-multiplication.mjs +109 -68
- package/tests/test-codecs.mjs +8 -0
package/ktvm/wire.mjs
CHANGED
|
@@ -20,7 +20,12 @@ import {
|
|
|
20
20
|
stringValueToText
|
|
21
21
|
} from "../codecs/runtime/unicode-string.mjs";
|
|
22
22
|
import { BitDeque } from "./bit-deque.mjs";
|
|
23
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
compareLabels,
|
|
25
|
+
normalizeNodeList,
|
|
26
|
+
nodeListComplete,
|
|
27
|
+
stepNodeListTag
|
|
28
|
+
} from "./schema.mjs";
|
|
24
29
|
|
|
25
30
|
export const DPDA_RESULT = Object.freeze({
|
|
26
31
|
DONE: "DONE",
|
|
@@ -371,17 +376,49 @@ function encodeKStringValue(str) {
|
|
|
371
376
|
return textToStringValue(str);
|
|
372
377
|
}
|
|
373
378
|
|
|
374
|
-
// --- Strictly Iterative, Resumable 1-State Pushdown Steppers ---
|
|
379
|
+
// --- Strictly Iterative, Resumable 1-State Pushdown Steppers & Schema Cursor ---
|
|
375
380
|
|
|
376
|
-
|
|
377
|
-
|
|
381
|
+
/**
|
|
382
|
+
* Create a resumable 1-state schema DPDA cursor initialized with a single node
|
|
383
|
+
* ID or a front-to-back residual node list `[v_1, ..., v_k]`.
|
|
384
|
+
*
|
|
385
|
+
* Internally stores `stack` in LIFO order (head `v_1` at `stack[stack.length - 1]`).
|
|
386
|
+
*/
|
|
387
|
+
export function createSchemaCursor(plist, nodes = [0]) {
|
|
388
|
+
const list = typeof nodes === "number" ? [nodes] : [...nodes];
|
|
389
|
+
const stack = new Array(list.length);
|
|
390
|
+
for (let i = 0; i < list.length; i++) {
|
|
391
|
+
stack[i] = list[list.length - 1 - i];
|
|
392
|
+
}
|
|
393
|
+
return { plist, stack };
|
|
378
394
|
}
|
|
379
395
|
|
|
380
|
-
|
|
381
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Return the cursor's current residual node list in front-to-back order `[v_1, ..., v_k]`.
|
|
398
|
+
*/
|
|
399
|
+
export function cursorNodeList(cursor) {
|
|
400
|
+
const { stack } = cursor;
|
|
401
|
+
const out = new Array(stack.length);
|
|
402
|
+
for (let i = 0; i < stack.length; i++) {
|
|
403
|
+
out[i] = stack[stack.length - 1 - i];
|
|
404
|
+
}
|
|
405
|
+
return out;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Unified strictly iterative 1-state schema DPDA stepper.
|
|
410
|
+
*
|
|
411
|
+
* Advances `cursor` across `srcDeque` according to the node-list prefix
|
|
412
|
+
* derivative rules; if `dstDeque` is provided, mirrors every shifted tag code
|
|
413
|
+
* into `dstDeque` (providing both `dpda_skip` and `dpda_copy` from one core loop).
|
|
414
|
+
*/
|
|
415
|
+
export function stepSchemaCursor(srcDeque, cursor, dstDeque = null) {
|
|
416
|
+
const { plist, stack } = cursor;
|
|
382
417
|
while (stack.length > 0) {
|
|
383
418
|
const nodeId = stack[stack.length - 1];
|
|
384
|
-
const
|
|
419
|
+
const node = plist[nodeId];
|
|
420
|
+
if (!node) return DPDA_RESULT.FAILED;
|
|
421
|
+
const [kind, edges] = node;
|
|
385
422
|
if (kind === "closed-product") {
|
|
386
423
|
stack.pop();
|
|
387
424
|
for (let i = edges.length - 1; i >= 0; i--) {
|
|
@@ -399,6 +436,7 @@ export function stepDpdaSkip(srcDeque, state) {
|
|
|
399
436
|
srcDeque.aligned = false;
|
|
400
437
|
}
|
|
401
438
|
if (ord >= edges.length) return DPDA_RESULT.FAILED;
|
|
439
|
+
if (dstDeque && w > 0) dstDeque.pushBits(ord, w);
|
|
402
440
|
stack.pop();
|
|
403
441
|
stack.push(edges[ord][1]);
|
|
404
442
|
} else {
|
|
@@ -408,40 +446,192 @@ export function stepDpdaSkip(srcDeque, state) {
|
|
|
408
446
|
return DPDA_RESULT.DONE;
|
|
409
447
|
}
|
|
410
448
|
|
|
449
|
+
export function createDpdaSkip(plist, rootId = 0) {
|
|
450
|
+
return createSchemaCursor(plist, rootId);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export function stepDpdaSkip(srcDeque, state) {
|
|
454
|
+
return stepSchemaCursor(srcDeque, state, null);
|
|
455
|
+
}
|
|
456
|
+
|
|
411
457
|
export function createDpdaCopy(plist, rootId = 0) {
|
|
412
|
-
return
|
|
458
|
+
return createSchemaCursor(plist, rootId);
|
|
413
459
|
}
|
|
414
460
|
|
|
415
461
|
export function stepDpdaCopy(srcDeque, dstDeque, state) {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
462
|
+
return stepSchemaCursor(srcDeque, state, dstDeque);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function readBitsFromBuffer(bytes, bitOffset, width, limitBits) {
|
|
466
|
+
if (width <= 0) return 0;
|
|
467
|
+
if (bitOffset < 0 || bitOffset + width > limitBits) return -1;
|
|
468
|
+
let val = 0;
|
|
469
|
+
for (let i = 0; i < width; i++) {
|
|
470
|
+
const pos = bitOffset + i;
|
|
471
|
+
const bit = (bytes[pos >>> 3] >>> (7 - (pos & 7))) & 1;
|
|
472
|
+
val = ((val << 1) | bit) >>> 0;
|
|
473
|
+
}
|
|
474
|
+
return val;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Scan an immutable packed `k-wire` byte buffer starting at any aligned bit
|
|
479
|
+
* offset `(startBitOffset, nodes)` represented as a residual node list
|
|
480
|
+
* `[v_1, ..., v_k]` of `plist` (Definition 2.3).
|
|
481
|
+
*
|
|
482
|
+
* Modes:
|
|
483
|
+
* - `mode: "tag"` — normalizes leading products, reads the head union tag at
|
|
484
|
+
* `startBitOffset`, and returns the updated aligned offset
|
|
485
|
+
* and normalized residual node list.
|
|
486
|
+
* - `mode: "value"` — skips the complete sub-value conforming to `nodes[0]`
|
|
487
|
+
* via the 1-state schema DPDA without copying into a
|
|
488
|
+
* `BitDeque`, returning the new aligned offset and remaining
|
|
489
|
+
* continuation `nodes.slice(1)` (normalized if `normalize: true`).
|
|
490
|
+
*/
|
|
491
|
+
export function scanBufferAlignedOffset(
|
|
492
|
+
bytes,
|
|
493
|
+
startBitOffset,
|
|
494
|
+
plist,
|
|
495
|
+
nodes = [0],
|
|
496
|
+
{ mode = "value", totalBits = null, normalize = false } = {}
|
|
497
|
+
) {
|
|
498
|
+
const buf = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
499
|
+
const limitBits = totalBits === null ? buf.length * 8 : totalBits;
|
|
500
|
+
if (!Number.isInteger(startBitOffset) || startBitOffset < 0 || startBitOffset > limitBits) {
|
|
501
|
+
return {
|
|
502
|
+
ok: false,
|
|
503
|
+
reason: `startBitOffset ${startBitOffset} out of bounds [0..${limitBits}]`
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
const list = typeof nodes === "number" ? [nodes] : [...nodes];
|
|
507
|
+
|
|
508
|
+
if (mode === "tag") {
|
|
509
|
+
let norm;
|
|
510
|
+
try {
|
|
511
|
+
norm = normalizeNodeList(plist, list);
|
|
512
|
+
} catch (err) {
|
|
513
|
+
return { ok: false, reason: err.message };
|
|
514
|
+
}
|
|
515
|
+
if (norm.length === 0) {
|
|
516
|
+
return { ok: false, reason: "aligned offset is already at end of value" };
|
|
517
|
+
}
|
|
518
|
+
const headId = norm[0];
|
|
519
|
+
const headNode = plist[headId];
|
|
520
|
+
if (!headNode || headNode[0] !== "closed-union") {
|
|
521
|
+
return {
|
|
522
|
+
ok: false,
|
|
523
|
+
reason: `expected closed-union at node ${headId}, got '${headNode ? headNode[0] : "missing"}'`
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
const edges = headNode[1];
|
|
527
|
+
if (edges.length === 0) {
|
|
528
|
+
return { ok: false, reason: `empty closed-union at node ${headId}` };
|
|
529
|
+
}
|
|
530
|
+
const w = choiceWidth(edges.length);
|
|
531
|
+
const ord = w > 0 ? readBitsFromBuffer(buf, startBitOffset, w, limitBits) : 0;
|
|
532
|
+
if (ord < 0) {
|
|
533
|
+
return {
|
|
534
|
+
ok: false,
|
|
535
|
+
reason: `truncated k-wire buffer: needed ${w} bits at bit offset ${startBitOffset}`
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
const step = stepNodeListTag(plist, norm, ord);
|
|
539
|
+
if (!step.ok) return step;
|
|
540
|
+
return {
|
|
541
|
+
ok: true,
|
|
542
|
+
bitOffset: startBitOffset + w,
|
|
543
|
+
headNodeId: step.headNodeId,
|
|
544
|
+
label: step.label,
|
|
545
|
+
ordinal: step.ordinal,
|
|
546
|
+
width: step.width,
|
|
547
|
+
targetId: step.targetId,
|
|
548
|
+
nodes: step.nextNodes,
|
|
549
|
+
complete: step.nextNodes.length === 0
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (mode === "value") {
|
|
554
|
+
if (list.length === 0) {
|
|
555
|
+
return { ok: true, bitOffset: startBitOffset, headNodeId: null, nodes: [], complete: true };
|
|
556
|
+
}
|
|
557
|
+
const headNodeId = list[0];
|
|
558
|
+
const rest = list.slice(1);
|
|
559
|
+
const stack = [headNodeId];
|
|
560
|
+
let bitOffset = startBitOffset;
|
|
561
|
+
const zeroBitUnionSeen = new Set();
|
|
562
|
+
|
|
563
|
+
while (stack.length > 0) {
|
|
564
|
+
const nodeId = stack[stack.length - 1];
|
|
565
|
+
const node = plist[nodeId];
|
|
566
|
+
if (!node) {
|
|
567
|
+
return { ok: false, reason: `invalid schema node ${nodeId}` };
|
|
424
568
|
}
|
|
425
|
-
|
|
426
|
-
if (
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
569
|
+
const [kind, edges] = node;
|
|
570
|
+
if (kind === "closed-product") {
|
|
571
|
+
// Normalize leading products with cycle protection
|
|
572
|
+
let normHead;
|
|
573
|
+
try {
|
|
574
|
+
normHead = normalizeNodeList(plist, [nodeId]);
|
|
575
|
+
} catch (err) {
|
|
576
|
+
return { ok: false, reason: err.message };
|
|
432
577
|
}
|
|
433
|
-
|
|
434
|
-
|
|
578
|
+
stack.pop();
|
|
579
|
+
for (let i = normHead.length - 1; i >= 0; i--) {
|
|
580
|
+
stack.push(normHead[i]);
|
|
581
|
+
}
|
|
582
|
+
} else if (kind === "closed-union") {
|
|
583
|
+
if (edges.length === 0) {
|
|
584
|
+
return { ok: false, reason: `empty closed-union at node ${nodeId}` };
|
|
585
|
+
}
|
|
586
|
+
const w = choiceWidth(edges.length);
|
|
587
|
+
let ord = 0;
|
|
588
|
+
if (w > 0) {
|
|
589
|
+
ord = readBitsFromBuffer(buf, bitOffset, w, limitBits);
|
|
590
|
+
if (ord < 0) {
|
|
591
|
+
return {
|
|
592
|
+
ok: false,
|
|
593
|
+
reason: `truncated k-wire buffer: needed ${w} bits at bit offset ${bitOffset}`
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
bitOffset += w;
|
|
597
|
+
zeroBitUnionSeen.clear();
|
|
598
|
+
} else {
|
|
599
|
+
if (zeroBitUnionSeen.has(nodeId)) {
|
|
600
|
+
return { ok: false, reason: `unguarded 0-bit union cycle at node ${nodeId}` };
|
|
601
|
+
}
|
|
602
|
+
zeroBitUnionSeen.add(nodeId);
|
|
603
|
+
}
|
|
604
|
+
if (ord >= edges.length) {
|
|
605
|
+
return {
|
|
606
|
+
ok: false,
|
|
607
|
+
reason: `invalid union ordinal ${ord} (expected < ${edges.length}) at node ${nodeId}`
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
stack.pop();
|
|
611
|
+
stack.push(edges[ord][1]);
|
|
612
|
+
} else {
|
|
613
|
+
return { ok: false, reason: `cannot scan non-closed schema kind '${kind}'` };
|
|
435
614
|
}
|
|
436
|
-
if (ord >= edges.length) return DPDA_RESULT.FAILED;
|
|
437
|
-
if (w > 0) dstDeque.pushBits(ord, w);
|
|
438
|
-
stack.pop();
|
|
439
|
-
stack.push(edges[ord][1]);
|
|
440
|
-
} else {
|
|
441
|
-
return DPDA_RESULT.FAILED;
|
|
442
615
|
}
|
|
616
|
+
|
|
617
|
+
let outNodes;
|
|
618
|
+
let complete;
|
|
619
|
+
try {
|
|
620
|
+
outNodes = normalize ? normalizeNodeList(plist, rest) : rest;
|
|
621
|
+
complete = nodeListComplete(plist, outNodes);
|
|
622
|
+
} catch (err) {
|
|
623
|
+
return { ok: false, reason: err.message };
|
|
624
|
+
}
|
|
625
|
+
return {
|
|
626
|
+
ok: true,
|
|
627
|
+
bitOffset,
|
|
628
|
+
headNodeId,
|
|
629
|
+
nodes: outNodes,
|
|
630
|
+
complete
|
|
631
|
+
};
|
|
443
632
|
}
|
|
444
|
-
|
|
633
|
+
|
|
634
|
+
return { ok: false, reason: `unknown scan mode '${mode}'` };
|
|
445
635
|
}
|
|
446
636
|
|
|
447
637
|
export function createDpdaCoerce(srcPlist, srcRoot, dstPlist, dstRoot) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fraczak/k",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.1.1",
|
|
4
4
|
"description": "k — a language for content-addressable schemas and data transformations over algebraic data types (products and tagged unions); includes runtime, contract derivation, REPL, and codec toolchain",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
package/paper/Makefile
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
PAPER = k-paper
|
|
2
|
+
PAPERS = k-paper k-transducer-compiler
|
|
2
3
|
TEX = pdflatex -interaction=nonstopmode
|
|
3
4
|
|
|
4
5
|
.PHONY: all pdf clean arxiv
|
|
5
6
|
|
|
6
7
|
all: pdf
|
|
7
8
|
|
|
8
|
-
pdf: $(
|
|
9
|
-
$(TEX) $(PAPER).tex
|
|
10
|
-
$(TEX) $(PAPER).tex
|
|
9
|
+
pdf: $(addsuffix .pdf,$(PAPERS))
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
%.pdf: %.tex
|
|
12
|
+
$(TEX) $<
|
|
13
|
+
$(TEX) $<
|
|
14
|
+
|
|
15
|
+
arxiv: $(PAPER).pdf
|
|
13
16
|
tar -czvf arxiv-submission.tar.gz $(PAPER).tex
|
|
14
17
|
@echo "Created arxiv-submission.tar.gz for arXiv upload"
|
|
15
18
|
|
|
16
19
|
clean:
|
|
17
|
-
rm -f $(
|
|
20
|
+
rm -f $(foreach p,$(PAPERS),$(p).aux $(p).log $(p).out $(p).toc $(p).pdf) arxiv-submission.tar.gz
|
|
Binary file
|