@fraczak/k 8.8.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 +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 +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/codecs/djson.mjs +2 -3
- package/codecs/int.mjs +66 -14
- 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/lib/djson.k +175 -173
- package/lib/generic.k +82 -97
- package/lib/ieee.k +110 -115
- package/lib/integer.k +25 -31
- package/package.json +1 -9
- package/repl.mjs +101 -20
- package/scripts/build-repl-html.mjs +40 -18
- package/scripts/perf-bigint-compare.mjs +275 -0
- 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-djson-methods.mjs +0 -1
- package/tests/test-djson.mjs +0 -1
- package/tests/test-generic.mjs +5 -7
- package/tests/test-repl-html.mjs +72 -15
- package/tests/test-repl.mjs +27 -20
- package/tests/test-structural-values.mjs +1 -1
- 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,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Benchmark harness adapter for kTVM Native C/LLVM executables (`ktvm-llvm`, `ktvm-llvm-opt`).
|
|
3
|
+
*
|
|
4
|
+
* Uses the same `PersistentExecutable` (`--server` + `K_TRACE=1`) framing protocol
|
|
5
|
+
* as `harness/adapters/llvm.mjs` for apples-to-apples head-to-head comparison.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
|
|
11
|
+
import { BackendAdapter, BackendRunner } from "../backend.mjs";
|
|
12
|
+
import { PersistentExecutable } from "../persistent.mjs";
|
|
13
|
+
import { decodeWire } from "../../backend-api.mjs";
|
|
14
|
+
import {
|
|
15
|
+
compileObjectToKTVMExecutable,
|
|
16
|
+
isClangAvailable
|
|
17
|
+
} from "../../ktvm/llvm/index.mjs";
|
|
18
|
+
|
|
19
|
+
class KTVMLLVMRunner extends BackendRunner {
|
|
20
|
+
constructor(backendId, servers = new Map()) {
|
|
21
|
+
super(backendId);
|
|
22
|
+
this.servers = servers;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getServer(exePath) {
|
|
26
|
+
let server = this.servers.get(exePath);
|
|
27
|
+
if (!server) {
|
|
28
|
+
server = new PersistentExecutable(exePath);
|
|
29
|
+
this.servers.set(exePath, server);
|
|
30
|
+
}
|
|
31
|
+
return server;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async runSingle(testCase, options = {}) {
|
|
35
|
+
const server = this.getServer(testCase.exePath);
|
|
36
|
+
if (options.trace) {
|
|
37
|
+
const res = await server.requestWithTrace(testCase.inputWire);
|
|
38
|
+
const decoded = decodeWire(res.payload);
|
|
39
|
+
return {
|
|
40
|
+
outputWire: res.payload,
|
|
41
|
+
outputVal: decoded.value,
|
|
42
|
+
outputPattern: decoded.pattern,
|
|
43
|
+
trace: res.trace
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const payload = await server.request(testCase.inputWire);
|
|
47
|
+
const decoded = decodeWire(payload);
|
|
48
|
+
return {
|
|
49
|
+
outputWire: payload,
|
|
50
|
+
outputVal: decoded.value,
|
|
51
|
+
outputPattern: decoded.pattern
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async runBatch(compiledCases) {
|
|
56
|
+
for (const c of compiledCases) {
|
|
57
|
+
const server = this.getServer(c.exePath);
|
|
58
|
+
await server.request(c.inputWire);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async close() {
|
|
63
|
+
for (const server of this.servers.values()) {
|
|
64
|
+
server.close();
|
|
65
|
+
}
|
|
66
|
+
this.servers.clear();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class KTVMLLVMAdapter extends BackendAdapter {
|
|
71
|
+
constructor({ optimize = true, clangOpt = "-O3", ...options } = {}) {
|
|
72
|
+
super(
|
|
73
|
+
optimize ? "ktvm-llvm-opt" : "ktvm-llvm",
|
|
74
|
+
optimize
|
|
75
|
+
? `kTVM Native C/LLVM Optimized (${clangOpt}, persistent)`
|
|
76
|
+
: `kTVM Native C/LLVM (${clangOpt}, persistent)`,
|
|
77
|
+
options
|
|
78
|
+
);
|
|
79
|
+
this.optimize = optimize;
|
|
80
|
+
this.clangOpt = clangOpt;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
isAvailable() {
|
|
84
|
+
return isClangAvailable("clang");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async compile(testCase, options = {}) {
|
|
88
|
+
const cacheDir = options.cacheDir || path.join(process.cwd(), ".cache");
|
|
89
|
+
fs.mkdirSync(cacheDir, { recursive: true });
|
|
90
|
+
const tag = this.optimize ? "opt" : "raw";
|
|
91
|
+
const name = testCase.op || testCase.relName || "case";
|
|
92
|
+
const exePath = path.join(cacheDir, `harness_ktvm_llvm_${tag}_${name}`);
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const relation = testCase.relHash || testCase.relName || testCase.op || testCase.object.main;
|
|
96
|
+
compileObjectToKTVMExecutable(testCase.object, relation, testCase.inputPattern, exePath, {
|
|
97
|
+
optimize: this.optimize,
|
|
98
|
+
clangOpt: this.clangOpt
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
status: "ok",
|
|
102
|
+
exePath,
|
|
103
|
+
inputWire: testCase.inputWire,
|
|
104
|
+
inputVal: testCase.inputVal,
|
|
105
|
+
outputPattern: testCase.outputPattern
|
|
106
|
+
};
|
|
107
|
+
} catch (error) {
|
|
108
|
+
return {
|
|
109
|
+
status: "failed",
|
|
110
|
+
error: error.stack || error.message || String(error)
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async createRunner() {
|
|
116
|
+
return new KTVMLLVMRunner(this.id);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compileObjectToKTVMWasm,
|
|
3
|
+
decodeWire,
|
|
4
|
+
instantiateKTVMWasm
|
|
5
|
+
} from "../../backend-api.mjs";
|
|
6
|
+
import { BackendAdapter, BackendRunner } from "../backend.mjs";
|
|
7
|
+
|
|
8
|
+
class KTVMWasmRunner extends BackendRunner {
|
|
9
|
+
constructor(backendId) {
|
|
10
|
+
super(backendId);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async runSingle(testCase) {
|
|
14
|
+
const t0 = process.hrtime.bigint();
|
|
15
|
+
const res = testCase.runner.executeWire(testCase.inputWire, { decodeValue: false });
|
|
16
|
+
const tEval = process.hrtime.bigint();
|
|
17
|
+
const evalNs = Number(tEval - t0);
|
|
18
|
+
|
|
19
|
+
let outputVal;
|
|
20
|
+
let outputWire = null;
|
|
21
|
+
if (res.status === "OK") {
|
|
22
|
+
outputWire = res.wire;
|
|
23
|
+
outputVal = decodeWire(res.wire, { eager: true }).value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const trace = {
|
|
27
|
+
ipcReadNs: 0,
|
|
28
|
+
decodeNs: 0,
|
|
29
|
+
flatInNs: 0,
|
|
30
|
+
evalNs,
|
|
31
|
+
flatOutNs: 0,
|
|
32
|
+
encodeNs: 0,
|
|
33
|
+
ipcWriteNs: 0,
|
|
34
|
+
totalNs: evalNs
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
outputVal,
|
|
39
|
+
outputWire,
|
|
40
|
+
trace,
|
|
41
|
+
profile: null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async runBatch(compiledCases) {
|
|
46
|
+
for (const c of compiledCases) {
|
|
47
|
+
c.runner.executeWire(c.inputWire, { decodeValue: false });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class KTVMWasmAdapter extends BackendAdapter {
|
|
53
|
+
constructor(options = {}) {
|
|
54
|
+
const optimize = options.optimize !== false;
|
|
55
|
+
const id = options.id || (optimize ? "ktvm-wasm-opt" : "ktvm-wasm");
|
|
56
|
+
const name = options.name || (
|
|
57
|
+
optimize
|
|
58
|
+
? "kTVM WebAssembly Optimized (fused + MWTR tables, direct k-wire)"
|
|
59
|
+
: "kTVM WebAssembly (direct k-wire)"
|
|
60
|
+
);
|
|
61
|
+
super(id, name, options);
|
|
62
|
+
this.optimize = optimize;
|
|
63
|
+
this.budgetBytes = options.budgetBytes;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
isAvailable() {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async compile(testCase) {
|
|
71
|
+
try {
|
|
72
|
+
const inputPattern = testCase.inputPattern || (testCase.inputWire ? decodeWire(testCase.inputWire).pattern : null);
|
|
73
|
+
const artifact = await compileObjectToKTVMWasm(
|
|
74
|
+
testCase.object,
|
|
75
|
+
testCase.relName || testCase.op,
|
|
76
|
+
inputPattern,
|
|
77
|
+
{
|
|
78
|
+
optimize: this.optimize,
|
|
79
|
+
budgetBytes: this.budgetBytes,
|
|
80
|
+
validate: true,
|
|
81
|
+
verifyTables: true
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
const runner = await instantiateKTVMWasm(artifact);
|
|
85
|
+
return {
|
|
86
|
+
status: "ok",
|
|
87
|
+
artifact,
|
|
88
|
+
runner,
|
|
89
|
+
inputVal: testCase.inputVal,
|
|
90
|
+
inputWire: testCase.inputWire,
|
|
91
|
+
outputPattern: artifact.outPlist
|
|
92
|
+
};
|
|
93
|
+
} catch (error) {
|
|
94
|
+
return {
|
|
95
|
+
status: "failed",
|
|
96
|
+
error: error.stack || error.message || String(error)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async createRunner() {
|
|
102
|
+
return new KTVMWasmRunner(this.id);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compileKIRToKTVM,
|
|
3
|
+
decodeWire,
|
|
4
|
+
executeKTVM,
|
|
5
|
+
executeKTVMWire,
|
|
6
|
+
optimizeKTVM
|
|
7
|
+
} from "../../backend-api.mjs";
|
|
8
|
+
import { BackendAdapter, BackendRunner } from "../backend.mjs";
|
|
9
|
+
|
|
10
|
+
class KTVMRunner extends BackendRunner {
|
|
11
|
+
constructor(backendId, options = {}) {
|
|
12
|
+
super(backendId);
|
|
13
|
+
this.wire = options.wire !== false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async runSingle(testCase) {
|
|
17
|
+
const t0 = process.hrtime.bigint();
|
|
18
|
+
let outputVal;
|
|
19
|
+
let outputWire = null;
|
|
20
|
+
let evalNs = 0;
|
|
21
|
+
|
|
22
|
+
if (this.wire && testCase.inputWire) {
|
|
23
|
+
const res = executeKTVMWire(testCase.compiled.module, testCase.inputWire, {
|
|
24
|
+
entry: testCase.compiled.entry,
|
|
25
|
+
validate: false,
|
|
26
|
+
decodeValue: true
|
|
27
|
+
});
|
|
28
|
+
const tEnd = process.hrtime.bigint();
|
|
29
|
+
evalNs = Number(tEnd - t0);
|
|
30
|
+
outputVal = res.status === "OK" ? res.value : undefined;
|
|
31
|
+
outputWire = res.status === "OK" ? res.wire : null;
|
|
32
|
+
} else {
|
|
33
|
+
const res = executeKTVM(testCase.compiled.module, [testCase.inputVal], {
|
|
34
|
+
entry: testCase.compiled.entry,
|
|
35
|
+
validate: false
|
|
36
|
+
});
|
|
37
|
+
const tEnd = process.hrtime.bigint();
|
|
38
|
+
evalNs = Number(tEnd - t0);
|
|
39
|
+
outputVal = res.status === "OK" ? res.value : undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const trace = {
|
|
43
|
+
ipcReadNs: 0,
|
|
44
|
+
decodeNs: 0,
|
|
45
|
+
flatInNs: 0,
|
|
46
|
+
evalNs,
|
|
47
|
+
flatOutNs: 0,
|
|
48
|
+
encodeNs: 0,
|
|
49
|
+
ipcWriteNs: 0,
|
|
50
|
+
totalNs: evalNs
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
outputVal,
|
|
55
|
+
outputWire,
|
|
56
|
+
trace,
|
|
57
|
+
profile: null
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async runBatch(compiledCases) {
|
|
62
|
+
for (const c of compiledCases) {
|
|
63
|
+
if (this.wire && c.inputWire) {
|
|
64
|
+
executeKTVMWire(c.compiled.module, c.inputWire, {
|
|
65
|
+
entry: c.compiled.entry,
|
|
66
|
+
validate: false,
|
|
67
|
+
decodeValue: false
|
|
68
|
+
});
|
|
69
|
+
} else {
|
|
70
|
+
executeKTVM(c.compiled.module, [c.inputVal], {
|
|
71
|
+
entry: c.compiled.entry,
|
|
72
|
+
validate: false,
|
|
73
|
+
decodeValues: false
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export class KTVMAdapter extends BackendAdapter {
|
|
81
|
+
constructor(state = null, options = {}) {
|
|
82
|
+
const wire = options.wire !== false;
|
|
83
|
+
const optimize = Boolean(options.optimize);
|
|
84
|
+
const id = options.id || (optimize ? "ktvm-opt" : (wire ? "ktvm" : "ktvm-pure"));
|
|
85
|
+
const name = options.name || (
|
|
86
|
+
optimize
|
|
87
|
+
? "kTVM Optimized (fused kernels + MWTR tables)"
|
|
88
|
+
: (wire ? "kTVM Reference VM (direct k-wire)" : "kTVM Reference VM (in-memory)")
|
|
89
|
+
);
|
|
90
|
+
super(id, name, options);
|
|
91
|
+
this.state = state;
|
|
92
|
+
this.wire = wire;
|
|
93
|
+
this.optimize = optimize;
|
|
94
|
+
this.budgetBytes = options.budgetBytes;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
setState(state) {
|
|
98
|
+
this.state = state;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async compile(testCase) {
|
|
102
|
+
try {
|
|
103
|
+
const inputPattern = testCase.inputPattern || (testCase.inputWire ? decodeWire(testCase.inputWire).pattern : null);
|
|
104
|
+
let compiled = compileKIRToKTVM(testCase.object, testCase.relName || testCase.op, inputPattern, {
|
|
105
|
+
validate: true
|
|
106
|
+
});
|
|
107
|
+
if (this.optimize) {
|
|
108
|
+
const opt = optimizeKTVM(compiled.module, {
|
|
109
|
+
budgetBytes: this.budgetBytes,
|
|
110
|
+
validate: true,
|
|
111
|
+
verifyTables: true
|
|
112
|
+
});
|
|
113
|
+
compiled = { ...compiled, module: opt.module, optStats: opt.stats };
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
status: "ok",
|
|
117
|
+
compiled,
|
|
118
|
+
inputVal: testCase.inputVal,
|
|
119
|
+
inputWire: testCase.inputWire,
|
|
120
|
+
outputPattern: compiled.outputPlist
|
|
121
|
+
};
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return {
|
|
124
|
+
status: "failed",
|
|
125
|
+
error: error.stack || error.message || String(error)
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async createRunner() {
|
|
131
|
+
return new KTVMRunner(this.id, { wire: this.wire });
|
|
132
|
+
}
|
|
133
|
+
}
|
package/harness/registry.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { NativeAdapter } from "./adapters/native.mjs";
|
|
2
2
|
import { KVMAdapter } from "./adapters/kvm.mjs";
|
|
3
|
+
import { KTVMAdapter } from "./adapters/ktvm.mjs";
|
|
4
|
+
import { KTVMWasmAdapter } from "./adapters/ktvm-wasm.mjs";
|
|
5
|
+
import { KTVMLLVMAdapter } from "./adapters/ktvm-llvm.mjs";
|
|
3
6
|
import { WasmAdapter } from "./adapters/wasm.mjs";
|
|
4
7
|
import { LLVMAdapter } from "./adapters/llvm.mjs";
|
|
5
8
|
|
|
@@ -13,6 +16,12 @@ export function createRegistry(config = {}) {
|
|
|
13
16
|
register("native-aware", () => new NativeAdapter("aware", config.state));
|
|
14
17
|
register("native-free", () => new NativeAdapter("free", config.state));
|
|
15
18
|
register("kvm", () => new KVMAdapter(config.state));
|
|
19
|
+
register("ktvm", () => new KTVMAdapter(config.state));
|
|
20
|
+
register("ktvm-opt", () => new KTVMAdapter(config.state, { optimize: true }));
|
|
21
|
+
register("ktvm-wasm", () => new KTVMWasmAdapter({ optimize: false }));
|
|
22
|
+
register("ktvm-wasm-opt", () => new KTVMWasmAdapter({ optimize: true }));
|
|
23
|
+
register("ktvm-llvm", () => new KTVMLLVMAdapter({ optimize: false, ...(config.llvm || {}) }));
|
|
24
|
+
register("ktvm-llvm-opt", () => new KTVMLLVMAdapter({ optimize: true, ...(config.llvm || {}) }));
|
|
16
25
|
register("wasm", () => new WasmAdapter(config.wasm || {}));
|
|
17
26
|
register("wasm-in-process", () => new WasmAdapter({ id: "wasm-in-process", mode: "in-process", ...(config.wasm || {}) }));
|
|
18
27
|
register("llvm", () => new LLVMAdapter(config.llvm || {}));
|
package/ktvm/ast.mjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kTVM AST definitions, canonical JSON artifact conversion, and error types.
|
|
3
|
+
*
|
|
4
|
+
* Implements the dual canonical representation specified in DOCS/KTVM_SPEC.md §9:
|
|
5
|
+
* - Shared AST manipulated by parser, printer, validator, specializer, and VM
|
|
6
|
+
* - 1-to-1 JSON artifact (`format: "k-tvm"`, `version: 1`)
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const KTVM_FORMAT = "k-tvm";
|
|
10
|
+
export const KTVM_VERSION = 1;
|
|
11
|
+
|
|
12
|
+
export class KTVMSyntaxError extends SyntaxError {
|
|
13
|
+
constructor(message, { line = 1, col = 1 } = {}) {
|
|
14
|
+
super(`${message} (at ${line}:${col})`);
|
|
15
|
+
this.name = "KTVMSyntaxError";
|
|
16
|
+
this.line = line;
|
|
17
|
+
this.col = col;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class KTVMValidationError extends Error {
|
|
22
|
+
constructor(errors) {
|
|
23
|
+
const list = Array.isArray(errors) ? errors : [errors];
|
|
24
|
+
super(`kTVM validation failed:\n - ${list.join("\n - ")}`);
|
|
25
|
+
this.name = "KTVMValidationError";
|
|
26
|
+
this.errors = list;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class KTVMSchemaError extends TypeError {
|
|
31
|
+
constructor(message) {
|
|
32
|
+
super(`SchemaError: ${message}`);
|
|
33
|
+
this.name = "KTVMSchemaError";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class KTVMQuotaError extends RangeError {
|
|
38
|
+
constructor(kind, limit, observed) {
|
|
39
|
+
super(`QuotaError: ${kind} limit exceeded (${observed} > ${limit})`);
|
|
40
|
+
this.name = "KTVMQuotaError";
|
|
41
|
+
this.quotaKind = kind;
|
|
42
|
+
this.limit = limit;
|
|
43
|
+
this.observed = observed;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// --- Schema AST Constructors ---
|
|
48
|
+
|
|
49
|
+
export const S = {
|
|
50
|
+
unit: () => ({ kind: "unit" }),
|
|
51
|
+
any: () => ({ kind: "any" }),
|
|
52
|
+
product: (fields = []) => ({ kind: "product", fields }),
|
|
53
|
+
openProduct: (fields = []) => ({ kind: "open_product", fields }),
|
|
54
|
+
union: (fields = []) => ({ kind: "union", fields }),
|
|
55
|
+
openUnion: (fields = []) => ({ kind: "open_union", fields }),
|
|
56
|
+
ref: (name, args = []) => ({ kind: "ref", name, args })
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export function createModule({
|
|
60
|
+
version = "0.1",
|
|
61
|
+
layer = "mono",
|
|
62
|
+
entry = null,
|
|
63
|
+
schemas = [],
|
|
64
|
+
procs = [],
|
|
65
|
+
kernels = [],
|
|
66
|
+
tables = []
|
|
67
|
+
} = {}) {
|
|
68
|
+
return {
|
|
69
|
+
format: KTVM_FORMAT,
|
|
70
|
+
formatVersion: KTVM_VERSION,
|
|
71
|
+
version,
|
|
72
|
+
layer,
|
|
73
|
+
entry,
|
|
74
|
+
schemas,
|
|
75
|
+
procs,
|
|
76
|
+
kernels,
|
|
77
|
+
tables
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function cloneAst(node) {
|
|
82
|
+
return structuredClone(node);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Export a kTVM module AST to its canonical JSON artifact (`format: "k-tvm"`).
|
|
87
|
+
*/
|
|
88
|
+
export function moduleToJSON(mod) {
|
|
89
|
+
return {
|
|
90
|
+
format: KTVM_FORMAT,
|
|
91
|
+
formatVersion: KTVM_VERSION,
|
|
92
|
+
version: mod.version || "0.1",
|
|
93
|
+
layer: mod.layer,
|
|
94
|
+
entry: mod.entry ?? null,
|
|
95
|
+
schemas: cloneAst(mod.schemas || []),
|
|
96
|
+
procs: cloneAst(mod.procs || []),
|
|
97
|
+
kernels: cloneAst(mod.kernels || []),
|
|
98
|
+
tables: cloneAst(mod.tables || [])
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Hydrate a canonical `format: "k-tvm"` JSON artifact back into a kTVM module AST.
|
|
104
|
+
*/
|
|
105
|
+
export function moduleFromJSON(obj) {
|
|
106
|
+
if (!obj || typeof obj !== "object") {
|
|
107
|
+
throw new Error("Invalid k-tvm JSON artifact: expected object");
|
|
108
|
+
}
|
|
109
|
+
if (obj.format !== KTVM_FORMAT) {
|
|
110
|
+
throw new Error(`Invalid k-tvm JSON artifact format: ${obj.format}`);
|
|
111
|
+
}
|
|
112
|
+
if (obj.formatVersion !== KTVM_VERSION) {
|
|
113
|
+
throw new Error(`Unsupported k-tvm formatVersion: ${obj.formatVersion}`);
|
|
114
|
+
}
|
|
115
|
+
if (obj.layer !== "poly" && obj.layer !== "mono") {
|
|
116
|
+
throw new Error(`Invalid k-tvm layer: ${obj.layer}`);
|
|
117
|
+
}
|
|
118
|
+
return createModule({
|
|
119
|
+
version: obj.version || "0.1",
|
|
120
|
+
layer: obj.layer,
|
|
121
|
+
entry: obj.entry ?? null,
|
|
122
|
+
schemas: cloneAst(obj.schemas || []),
|
|
123
|
+
procs: cloneAst(obj.procs || []),
|
|
124
|
+
kernels: cloneAst(obj.kernels || []),
|
|
125
|
+
tables: cloneAst(obj.tables || [])
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Encode/decode dense integer arrays for `packed_base64` table blocks (little-endian Int32).
|
|
131
|
+
*/
|
|
132
|
+
export function packInt32Base64(int32Array) {
|
|
133
|
+
const arr = int32Array instanceof Int32Array ? int32Array : Int32Array.from(int32Array);
|
|
134
|
+
const bytes = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
135
|
+
return Buffer.from(bytes).toString("base64");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function unpackInt32Base64(base64Str) {
|
|
139
|
+
const buf = Buffer.from(base64Str, "base64");
|
|
140
|
+
if ((buf.byteLength & 3) !== 0) {
|
|
141
|
+
throw new Error(`Invalid packed_base64 Int32 byte length: ${buf.byteLength}`);
|
|
142
|
+
}
|
|
143
|
+
const out = new Int32Array(buf.byteLength / 4);
|
|
144
|
+
for (let i = 0; i < out.length; i++) {
|
|
145
|
+
out[i] = buf.readInt32LE(i * 4);
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Compute the exact sets of outer channels (`cons`, `prod`) consumed and produced
|
|
152
|
+
* by a network statement block (such as an arm of `switch`), excluding local
|
|
153
|
+
* `chan` declarations inside the block.
|
|
154
|
+
*/
|
|
155
|
+
export function collectSwitchOuterIO(stmts, outerChanMap) {
|
|
156
|
+
const locals = new Set();
|
|
157
|
+
const cons = new Set();
|
|
158
|
+
const prod = new Set();
|
|
159
|
+
|
|
160
|
+
const addCons = (v) => {
|
|
161
|
+
if (v && !locals.has(v) && outerChanMap.has(v)) cons.add(v);
|
|
162
|
+
};
|
|
163
|
+
const addProd = (v) => {
|
|
164
|
+
if (v && !locals.has(v) && outerChanMap.has(v)) prod.add(v);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
function walk(list) {
|
|
168
|
+
for (const s of list || []) {
|
|
169
|
+
switch (s.op) {
|
|
170
|
+
case "chan":
|
|
171
|
+
locals.add(s.name);
|
|
172
|
+
break;
|
|
173
|
+
case "drain":
|
|
174
|
+
addCons(s.src);
|
|
175
|
+
break;
|
|
176
|
+
case "gate":
|
|
177
|
+
case "splice":
|
|
178
|
+
case "coerce":
|
|
179
|
+
case "wrap":
|
|
180
|
+
case "match":
|
|
181
|
+
case "project":
|
|
182
|
+
case "guard":
|
|
183
|
+
addCons(s.src);
|
|
184
|
+
addProd(s.dst);
|
|
185
|
+
break;
|
|
186
|
+
case "const_unit":
|
|
187
|
+
addProd(s.dst);
|
|
188
|
+
break;
|
|
189
|
+
case "tee":
|
|
190
|
+
addCons(s.src);
|
|
191
|
+
for (const d of s.dsts) addProd(d);
|
|
192
|
+
break;
|
|
193
|
+
case "demux":
|
|
194
|
+
addCons(s.src);
|
|
195
|
+
for (const f of s.fields) addProd(f.name);
|
|
196
|
+
break;
|
|
197
|
+
case "mux":
|
|
198
|
+
for (const f of s.fields) addCons(f.name);
|
|
199
|
+
addProd(s.dst);
|
|
200
|
+
break;
|
|
201
|
+
case "spawn":
|
|
202
|
+
case "exec":
|
|
203
|
+
case "spec":
|
|
204
|
+
for (const i of s.inputs) addCons(i);
|
|
205
|
+
for (const o of s.outputs) addProd(o);
|
|
206
|
+
break;
|
|
207
|
+
case "switch": {
|
|
208
|
+
const inner = s.arms.length > 0 ? s.arms[0].body : (s.defaultBody || []);
|
|
209
|
+
walk(inner);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
walk(stmts);
|
|
217
|
+
return { cons, prod };
|
|
218
|
+
}
|