@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
|
@@ -17,8 +17,7 @@ import {
|
|
|
17
17
|
} from "../src/cli.mjs";
|
|
18
18
|
import {
|
|
19
19
|
compileKVMToExecutable,
|
|
20
|
-
compileObjectToExecutable
|
|
21
|
-
compilePKIRToExecutable
|
|
20
|
+
compileObjectToExecutable
|
|
22
21
|
} from "../src/executable.mjs";
|
|
23
22
|
|
|
24
23
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
@@ -27,7 +26,7 @@ function helpText() {
|
|
|
27
26
|
const prog = argv[1] || "k-llvm.mjs";
|
|
28
27
|
const defaultCache = path.join(os.tmpdir(), "k-llvm-cache");
|
|
29
28
|
return [
|
|
30
|
-
"Compile k source, .ko,
|
|
29
|
+
"Compile k source, .ko, or .kvm to native machine code via",
|
|
31
30
|
"LLVM and execute it over a binary pattern+value wire stream.",
|
|
32
31
|
"",
|
|
33
32
|
`Usage: ${prog} [options]`,
|
|
@@ -35,14 +34,13 @@ function helpText() {
|
|
|
35
34
|
"",
|
|
36
35
|
"Arguments:",
|
|
37
36
|
" source-snippet Inline k source snippet (in the style of k.mjs).",
|
|
38
|
-
" input-file Source .k, .ko, .klib,
|
|
37
|
+
" input-file Source .k, .ko, .klib, or .kvm file.",
|
|
39
38
|
" input-wire-file Optional binary wire input (default: stdin).",
|
|
40
39
|
"",
|
|
41
40
|
"Options:",
|
|
42
41
|
" --lib file Load one .klib or .k library before compiling.",
|
|
43
42
|
" --use spec Use library alias in scope (repeatable).",
|
|
44
43
|
" spec is 'name' or 'libname:localname'.",
|
|
45
|
-
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
46
44
|
" --retype rel Relation to specialize (default: object main).",
|
|
47
45
|
" --cache-dir path Directory to store compiled native executables.",
|
|
48
46
|
" --no-cache Compile in a temporary directory and discard.",
|
|
@@ -95,7 +93,6 @@ function computeCacheKey({
|
|
|
95
93
|
inputKey,
|
|
96
94
|
relation,
|
|
97
95
|
inputPattern,
|
|
98
|
-
usePKIR,
|
|
99
96
|
clangOpt
|
|
100
97
|
}) {
|
|
101
98
|
const hash = crypto.createHash("sha256");
|
|
@@ -106,7 +103,7 @@ function computeCacheKey({
|
|
|
106
103
|
hash.update("\0");
|
|
107
104
|
hash.update(JSON.stringify(inputPattern || null));
|
|
108
105
|
hash.update("\0");
|
|
109
|
-
hash.update(
|
|
106
|
+
hash.update("monomorphic\0");
|
|
110
107
|
hash.update(String(clangOpt || ""));
|
|
111
108
|
hash.update("\0");
|
|
112
109
|
hash.update(inputKey);
|
|
@@ -174,7 +171,7 @@ async function main() {
|
|
|
174
171
|
}
|
|
175
172
|
}
|
|
176
173
|
|
|
177
|
-
const { libraries, useSpecs
|
|
174
|
+
const { libraries, useSpecs } =
|
|
178
175
|
parseCompileOptions(remainingArgs);
|
|
179
176
|
const programInput = resolveProgramInput(remainingArgs);
|
|
180
177
|
const inputWirePath = remainingArgs.shift();
|
|
@@ -207,28 +204,18 @@ async function main() {
|
|
|
207
204
|
inputKey,
|
|
208
205
|
relation,
|
|
209
206
|
inputPattern: inputEnvelopePattern,
|
|
210
|
-
usePKIR,
|
|
211
207
|
clangOpt
|
|
212
208
|
});
|
|
213
209
|
exePath = path.join(cacheDir, `${key}.exe`);
|
|
214
210
|
if (!fs.existsSync(exePath)) {
|
|
215
211
|
const tmpPath = path.join(cacheDir, `${key}.${process.pid}.tmp`);
|
|
216
|
-
if (programInput.kind === "
|
|
217
|
-
const pkir = JSON.parse(
|
|
218
|
-
fs.readFileSync(programInput.path, "utf8")
|
|
219
|
-
);
|
|
220
|
-
compilePKIRToExecutable(pkir, tmpPath, {
|
|
221
|
-
inputPattern: inputEnvelopePattern,
|
|
222
|
-
clangOpt
|
|
223
|
-
});
|
|
224
|
-
} else if (programInput.kind === "kvm") {
|
|
212
|
+
if (programInput.kind === "kvm") {
|
|
225
213
|
const kvm = JSON.parse(
|
|
226
214
|
fs.readFileSync(programInput.path, "utf8")
|
|
227
215
|
);
|
|
228
216
|
compileKVMToExecutable(kvm, tmpPath, {
|
|
229
217
|
inputPattern: inputEnvelopePattern,
|
|
230
|
-
clangOpt
|
|
231
|
-
usePKIR
|
|
218
|
+
clangOpt
|
|
232
219
|
});
|
|
233
220
|
} else {
|
|
234
221
|
const object = await compileProgramInputToObject(programInput, {
|
|
@@ -239,8 +226,7 @@ async function main() {
|
|
|
239
226
|
compileObjectToExecutable(object, tmpPath, {
|
|
240
227
|
relation: relation || object.main,
|
|
241
228
|
inputPattern: inputEnvelopePattern,
|
|
242
|
-
clangOpt
|
|
243
|
-
usePKIR
|
|
229
|
+
clangOpt
|
|
244
230
|
});
|
|
245
231
|
}
|
|
246
232
|
fs.renameSync(tmpPath, exePath);
|
|
@@ -248,18 +234,11 @@ async function main() {
|
|
|
248
234
|
} else {
|
|
249
235
|
tempExeDir = fs.mkdtempSync(path.join(os.tmpdir(), "k-llvm-run-"));
|
|
250
236
|
exePath = path.join(tempExeDir, "prog.exe");
|
|
251
|
-
if (programInput.kind === "
|
|
252
|
-
const pkir = JSON.parse(fs.readFileSync(programInput.path, "utf8"));
|
|
253
|
-
compilePKIRToExecutable(pkir, exePath, {
|
|
254
|
-
inputPattern: inputEnvelopePattern,
|
|
255
|
-
clangOpt
|
|
256
|
-
});
|
|
257
|
-
} else if (programInput.kind === "kvm") {
|
|
237
|
+
if (programInput.kind === "kvm") {
|
|
258
238
|
const kvm = JSON.parse(fs.readFileSync(programInput.path, "utf8"));
|
|
259
239
|
compileKVMToExecutable(kvm, exePath, {
|
|
260
240
|
inputPattern: inputEnvelopePattern,
|
|
261
|
-
clangOpt
|
|
262
|
-
usePKIR
|
|
241
|
+
clangOpt
|
|
263
242
|
});
|
|
264
243
|
} else {
|
|
265
244
|
const object = await compileProgramInputToObject(programInput, {
|
|
@@ -270,8 +249,7 @@ async function main() {
|
|
|
270
249
|
compileObjectToExecutable(object, exePath, {
|
|
271
250
|
relation: relation || object.main,
|
|
272
251
|
inputPattern: inputEnvelopePattern,
|
|
273
|
-
clangOpt
|
|
274
|
-
usePKIR
|
|
252
|
+
clangOpt
|
|
275
253
|
});
|
|
276
254
|
}
|
|
277
255
|
}
|
|
@@ -37,7 +37,6 @@ export function parseCompileOptions(args) {
|
|
|
37
37
|
const libraries = [];
|
|
38
38
|
const useSpecs = [];
|
|
39
39
|
let loadedLibrary = false;
|
|
40
|
-
let usePKIR = false;
|
|
41
40
|
|
|
42
41
|
while (args.length > 0) {
|
|
43
42
|
if (args[0] === "--") {
|
|
@@ -62,9 +61,6 @@ export function parseCompileOptions(args) {
|
|
|
62
61
|
);
|
|
63
62
|
}
|
|
64
63
|
useSpecs.push(spec);
|
|
65
|
-
} else if (args[0] === "--pkir") {
|
|
66
|
-
args.shift();
|
|
67
|
-
usePKIR = true;
|
|
68
64
|
} else if (args[0].startsWith("--")) {
|
|
69
65
|
break;
|
|
70
66
|
} else {
|
|
@@ -72,7 +68,7 @@ export function parseCompileOptions(args) {
|
|
|
72
68
|
}
|
|
73
69
|
}
|
|
74
70
|
|
|
75
|
-
return { libraries, useSpecs
|
|
71
|
+
return { libraries, useSpecs };
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
export function fileInputType(filePath) {
|
|
@@ -80,7 +76,6 @@ export function fileInputType(filePath) {
|
|
|
80
76
|
if (ext === ".ko") return "ko";
|
|
81
77
|
if (ext === ".kvm") return "kvm";
|
|
82
78
|
if (ext === ".klib") return "klib";
|
|
83
|
-
if (ext === ".pkir") return "pkir";
|
|
84
79
|
return "source-file";
|
|
85
80
|
}
|
|
86
81
|
|
|
@@ -97,7 +92,7 @@ export function resolveProgramInput(args, { allowStdinSource = false } = {}) {
|
|
|
97
92
|
return { kind, path: inputArg, sourceName: inputArg };
|
|
98
93
|
}
|
|
99
94
|
|
|
100
|
-
if ([".k", ".ko", ".kvm", ".klib"
|
|
95
|
+
if ([".k", ".ko", ".kvm", ".klib"].includes(ext)) {
|
|
101
96
|
throw new Error(`Input file not found: ${inputArg}`);
|
|
102
97
|
}
|
|
103
98
|
|
|
@@ -149,11 +144,6 @@ export async function compileProgramInputToObject(
|
|
|
149
144
|
"Input is already lowered to .kvm bytecode; use " +
|
|
150
145
|
"compileLLVMArtifactFromKVM or compileKVMToExecutable."
|
|
151
146
|
);
|
|
152
|
-
} else if (input.kind === "pkir") {
|
|
153
|
-
throw new Error(
|
|
154
|
-
"Input is already lowered to .pkir IR; use " +
|
|
155
|
-
"compileLLVMArtifactFromPKIR or compilePKIRToExecutable."
|
|
156
|
-
);
|
|
157
147
|
} else {
|
|
158
148
|
throw new Error(`Unsupported input kind: ${input.kind}`);
|
|
159
149
|
}
|
|
@@ -7,8 +7,7 @@ import { isProduct, isVariant } from "@fraczak/k/Value.mjs";
|
|
|
7
7
|
import { exportPatternGraph } from "@fraczak/k/codecs/runtime/codec.mjs";
|
|
8
8
|
import { patternToPropertyList } from "@fraczak/k/codecs/runtime/pattern-json.mjs";
|
|
9
9
|
import { specializeKVM } from "@fraczak/k/kvm.mjs";
|
|
10
|
-
import {
|
|
11
|
-
import { compileObjectToLLVM, compileLLVMArtifactFromPKIR } from "./llvm.mjs";
|
|
10
|
+
import { compileObjectToLLVM } from "./llvm.mjs";
|
|
12
11
|
import { compileKVMModuleToLLVM } from "./kvm2llvm.mjs";
|
|
13
12
|
|
|
14
13
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
@@ -557,8 +556,7 @@ export function compileObjectToExecutable(
|
|
|
557
556
|
relation = object.main,
|
|
558
557
|
inputPattern = null,
|
|
559
558
|
runtimeMode = "fast",
|
|
560
|
-
clangOpt = "-O3"
|
|
561
|
-
usePKIR = false
|
|
559
|
+
clangOpt = "-O3"
|
|
562
560
|
} = {}
|
|
563
561
|
) {
|
|
564
562
|
const { inputPattern: compiledInputPattern, outputPattern, llvm } =
|
|
@@ -566,8 +564,7 @@ export function compileObjectToExecutable(
|
|
|
566
564
|
relation,
|
|
567
565
|
inputPattern:
|
|
568
566
|
inputPattern || inputPatternForObjectRelation(object, relation),
|
|
569
|
-
runtimeMode
|
|
570
|
-
usePKIR
|
|
567
|
+
runtimeMode
|
|
571
568
|
});
|
|
572
569
|
compileLLVMToExecutable(llvm, outputPath, {
|
|
573
570
|
driver: stdioDriverSource({
|
|
@@ -582,8 +579,7 @@ export function compileKVMToExecutable(kvmInput, outputPath, {
|
|
|
582
579
|
entry = null,
|
|
583
580
|
inputPattern = null,
|
|
584
581
|
runtimeMode = "fast",
|
|
585
|
-
clangOpt = "-O3"
|
|
586
|
-
usePKIR = false
|
|
582
|
+
clangOpt = "-O3"
|
|
587
583
|
} = {}) {
|
|
588
584
|
let kvm = kvmInput;
|
|
589
585
|
if (kvm?.format === "k-vm") {
|
|
@@ -602,8 +598,7 @@ export function compileKVMToExecutable(kvmInput, outputPath, {
|
|
|
602
598
|
relation: entryFunc,
|
|
603
599
|
inputPattern: compiledInputPattern,
|
|
604
600
|
outputPattern,
|
|
605
|
-
runtimeMode
|
|
606
|
-
usePKIR
|
|
601
|
+
runtimeMode
|
|
607
602
|
});
|
|
608
603
|
compileLLVMToExecutable(llvm, outputPath, {
|
|
609
604
|
driver: stdioDriverSource({
|
|
@@ -614,102 +609,19 @@ export function compileKVMToExecutable(kvmInput, outputPath, {
|
|
|
614
609
|
});
|
|
615
610
|
}
|
|
616
611
|
|
|
617
|
-
export function compilePKIRToExecutable(
|
|
618
|
-
pkirInput,
|
|
619
|
-
outputPath,
|
|
620
|
-
{
|
|
621
|
-
entry = null,
|
|
622
|
-
inputPattern = null,
|
|
623
|
-
runtimeMode = "fast",
|
|
624
|
-
clangOpt = "-O3"
|
|
625
|
-
} = {}
|
|
626
|
-
) {
|
|
627
|
-
let kvmProgram;
|
|
628
|
-
let entryFunc;
|
|
629
|
-
if (
|
|
630
|
-
(pkirInput.format === "pk-ir" || pkirInput.format === "k-pkir") &&
|
|
631
|
-
pkirInput.functions
|
|
632
|
-
) {
|
|
633
|
-
kvmProgram = {};
|
|
634
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
635
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
636
|
-
}
|
|
637
|
-
entryFunc =
|
|
638
|
-
entry ||
|
|
639
|
-
pkirInput.entry ||
|
|
640
|
-
Object.keys(kvmProgram)[0] ||
|
|
641
|
-
"__main__";
|
|
642
|
-
} else if (pkirInput.instructions) {
|
|
643
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
644
|
-
entryFunc = pkirInput.name || entry || "__main__";
|
|
645
|
-
kvmProgram = { [entryFunc]: kvm };
|
|
646
|
-
const scan = (insts) => {
|
|
647
|
-
for (const inst of insts || []) {
|
|
648
|
-
if (inst.op === "call" && inst.func && !kvmProgram[inst.func]) {
|
|
649
|
-
kvmProgram[inst.func] = kvm;
|
|
650
|
-
}
|
|
651
|
-
if (inst.branches) {
|
|
652
|
-
for (const b of inst.branches) scan(b.body);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
scan(kvm.body);
|
|
657
|
-
} else if (pkirInput.functions) {
|
|
658
|
-
kvmProgram = {};
|
|
659
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
660
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
661
|
-
}
|
|
662
|
-
entryFunc =
|
|
663
|
-
entry ||
|
|
664
|
-
pkirInput.entry ||
|
|
665
|
-
Object.keys(kvmProgram)[0] ||
|
|
666
|
-
"__main__";
|
|
667
|
-
} else {
|
|
668
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
669
|
-
entryFunc = pkirInput.name || entry || "__main__";
|
|
670
|
-
kvmProgram = { [entryFunc]: kvm };
|
|
671
|
-
}
|
|
672
|
-
return compileKVMToExecutable(
|
|
673
|
-
{
|
|
674
|
-
format: "k-vm",
|
|
675
|
-
layer: "KVM",
|
|
676
|
-
entry: entryFunc,
|
|
677
|
-
functions: kvmProgram
|
|
678
|
-
},
|
|
679
|
-
outputPath,
|
|
680
|
-
{
|
|
681
|
-
entry: entryFunc,
|
|
682
|
-
inputPattern,
|
|
683
|
-
runtimeMode,
|
|
684
|
-
clangOpt,
|
|
685
|
-
usePKIR: true
|
|
686
|
-
}
|
|
687
|
-
);
|
|
688
|
-
}
|
|
689
|
-
|
|
690
612
|
export function compileObjectAndRun(
|
|
691
613
|
object,
|
|
692
614
|
{
|
|
693
615
|
relation = object.main,
|
|
694
616
|
input,
|
|
695
617
|
expected = null,
|
|
696
|
-
inputPattern = null
|
|
697
|
-
usePKIR = false
|
|
618
|
+
inputPattern = null
|
|
698
619
|
}
|
|
699
620
|
) {
|
|
700
621
|
const { llvm } = compileObjectToLLVM(object, {
|
|
701
622
|
relation,
|
|
702
623
|
inputPattern:
|
|
703
|
-
inputPattern || inputPatternForObjectValue(object, input, relation)
|
|
704
|
-
usePKIR
|
|
624
|
+
inputPattern || inputPatternForObjectValue(object, input, relation)
|
|
705
625
|
});
|
|
706
626
|
return compileAndRunLLVM(llvm, { input, expected });
|
|
707
627
|
}
|
|
708
|
-
|
|
709
|
-
export function compilePKIRAndRun(
|
|
710
|
-
pkirInput,
|
|
711
|
-
{ input, expected = null, options = {} } = {}
|
|
712
|
-
) {
|
|
713
|
-
const llvm = compileLLVMArtifactFromPKIR(pkirInput, options);
|
|
714
|
-
return compileAndRunLLVM(llvm, { input, expected });
|
|
715
|
-
}
|
|
@@ -457,9 +457,7 @@ function getFieldEdgeIndex(inst, regPatterns, kvmFunc, ctx = {}) {
|
|
|
457
457
|
const srcPattern = regPatterns.get(srcReg) || (srcReg === "in" ? kvmFunc.inputPattern : null);
|
|
458
458
|
if (Array.isArray(srcPattern) && srcPattern.length > 0) {
|
|
459
459
|
const root = srcPattern[0];
|
|
460
|
-
const allowed =
|
|
461
|
-
? root[0] === "closed-product"
|
|
462
|
-
: (root[0] === "open-product" || root[0] === "closed-product");
|
|
460
|
+
const allowed = root[0] === "open-product" || root[0] === "closed-product";
|
|
463
461
|
if (allowed && Array.isArray(root[1])) {
|
|
464
462
|
const idx = root[1].findIndex(([l]) => l === inst.label);
|
|
465
463
|
if (idx >= 0) return idx;
|
|
@@ -518,7 +516,6 @@ export function compileKVMModuleToLLVM(entryName, kvmProgram, options = {}) {
|
|
|
518
516
|
labels,
|
|
519
517
|
functionNames,
|
|
520
518
|
runtimeMode,
|
|
521
|
-
usePKIR: Boolean(options.usePKIR),
|
|
522
519
|
armIndex: 0,
|
|
523
520
|
unionExprIndex: 0,
|
|
524
521
|
syntheticFunctions: [],
|
|
@@ -589,7 +586,6 @@ function lowerKVMFunction(kvmFunc, symbol, funcName, moduleCtx, linkage = "", op
|
|
|
589
586
|
labels: moduleCtx.labels,
|
|
590
587
|
functionNames: moduleCtx.functionNames,
|
|
591
588
|
runtimeMode: moduleCtx.runtimeMode,
|
|
592
|
-
usePKIR: moduleCtx.usePKIR,
|
|
593
589
|
allocas: [],
|
|
594
590
|
lines: [],
|
|
595
591
|
currentBlock: "entry",
|
|
@@ -7,11 +7,7 @@ import {
|
|
|
7
7
|
objectToKVMArtifact,
|
|
8
8
|
specializeKVM,
|
|
9
9
|
lowerKIRToKVM,
|
|
10
|
-
objectToKIRP
|
|
11
|
-
compileRelationToPKIR,
|
|
12
|
-
lowerPKIRToKVM,
|
|
13
|
-
exportPatternGraph,
|
|
14
|
-
patternToPropertyList
|
|
10
|
+
objectToKIRP
|
|
15
11
|
} from "@fraczak/k/backend-api.mjs";
|
|
16
12
|
import { compileKVMModuleToLLVM } from "./kvm2llvm.mjs";
|
|
17
13
|
|
|
@@ -1024,26 +1020,7 @@ export function compileObjectToLLVM(object, options = {}) {
|
|
|
1024
1020
|
let entryInputPattern = inputPattern;
|
|
1025
1021
|
let outputPattern = null;
|
|
1026
1022
|
|
|
1027
|
-
if (
|
|
1028
|
-
const rel = object.rels?.[relation] ||
|
|
1029
|
-
(object.relAlias?.[relation] &&
|
|
1030
|
-
object.rels?.[object.relAlias[relation]]) ||
|
|
1031
|
-
object.rels?.[object.main];
|
|
1032
|
-
if (rel?.typePatternGraph) {
|
|
1033
|
-
const graph = rel.typePatternGraph;
|
|
1034
|
-
if (!entryInputPattern &&
|
|
1035
|
-
rel.def?.patterns?.[0] !== undefined) {
|
|
1036
|
-
const inId = graph.find(rel.def.patterns[0]);
|
|
1037
|
-
entryInputPattern =
|
|
1038
|
-
patternToPropertyList(exportPatternGraph(graph, inId));
|
|
1039
|
-
}
|
|
1040
|
-
if (rel.def?.patterns?.[1] !== undefined) {
|
|
1041
|
-
const outId = graph.find(rel.def.patterns[1]);
|
|
1042
|
-
outputPattern =
|
|
1043
|
-
patternToPropertyList(exportPatternGraph(graph, outId));
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
} else if (inputPattern) {
|
|
1023
|
+
if (inputPattern) {
|
|
1047
1024
|
const retyped = retypeObjectRelationForBackend(
|
|
1048
1025
|
object, relation, inputPattern, {
|
|
1049
1026
|
source: options.source || "<k-llvm>"
|
|
@@ -1054,35 +1031,9 @@ export function compileObjectToLLVM(object, options = {}) {
|
|
|
1054
1031
|
outputPattern = retyped.outputPattern;
|
|
1055
1032
|
}
|
|
1056
1033
|
|
|
1057
|
-
let functions;
|
|
1058
|
-
let retypedKir = null;
|
|
1059
1034
|
const entry = relation || targetObject.main || "__main__";
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
functions = {};
|
|
1063
|
-
for (const [name, relDef] of Object.entries(targetObject.rels || {})) {
|
|
1064
|
-
const pkir = compileRelationToPKIR(relDef, name);
|
|
1065
|
-
functions[name] = lowerPKIRToKVM(pkir);
|
|
1066
|
-
}
|
|
1067
|
-
for (const [alias, target] of Object.entries(targetObject.relAlias || {})) {
|
|
1068
|
-
if (functions[target] && !functions[alias]) {
|
|
1069
|
-
functions[alias] = functions[target];
|
|
1070
|
-
}
|
|
1071
|
-
const stripped = target.startsWith("@") ? target.slice(1) : target;
|
|
1072
|
-
if (functions[stripped] && !functions[alias]) {
|
|
1073
|
-
functions[alias] = functions[stripped];
|
|
1074
|
-
}
|
|
1075
|
-
if (functions[alias] && !functions[target]) {
|
|
1076
|
-
functions[target] = functions[alias];
|
|
1077
|
-
}
|
|
1078
|
-
}
|
|
1079
|
-
if (relation && !functions[relation] && functions[targetObject.main]) {
|
|
1080
|
-
functions[relation] = functions[targetObject.main];
|
|
1081
|
-
}
|
|
1082
|
-
} else {
|
|
1083
|
-
retypedKir = objectToKIRP(targetObject);
|
|
1084
|
-
functions = lowerKIRToKVM(retypedKir);
|
|
1085
|
-
}
|
|
1035
|
+
const retypedKir = objectToKIRP(targetObject);
|
|
1036
|
+
const functions = lowerKIRToKVM(retypedKir);
|
|
1086
1037
|
|
|
1087
1038
|
const specializedKvm = {
|
|
1088
1039
|
format: "k-vm",
|
|
@@ -1120,63 +1071,6 @@ export function compileLLVMArtifactFromKVM(kvmInput, options = {}) {
|
|
|
1120
1071
|
return compileKVMModuleToLLVM(entry, functions, options);
|
|
1121
1072
|
}
|
|
1122
1073
|
|
|
1123
|
-
export function compileLLVMArtifactFromPKIR(pkirInput, options = {}) {
|
|
1124
|
-
let kvmProgram;
|
|
1125
|
-
let entry;
|
|
1126
|
-
if (
|
|
1127
|
-
(pkirInput.format === "pk-ir" || pkirInput.format === "k-pkir") &&
|
|
1128
|
-
pkirInput.functions
|
|
1129
|
-
) {
|
|
1130
|
-
entry =
|
|
1131
|
-
pkirInput.entry ||
|
|
1132
|
-
options.entry ||
|
|
1133
|
-
Object.keys(pkirInput.functions)[0] ||
|
|
1134
|
-
"__main__";
|
|
1135
|
-
kvmProgram = {};
|
|
1136
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
1137
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
1138
|
-
}
|
|
1139
|
-
} else if (pkirInput.instructions) {
|
|
1140
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
1141
|
-
entry = pkirInput.name || options.entry || "__main__";
|
|
1142
|
-
kvmProgram = { [entry]: kvm };
|
|
1143
|
-
const scan = (insts) => {
|
|
1144
|
-
for (const inst of insts || []) {
|
|
1145
|
-
if (inst.op === "call" && inst.func && !kvmProgram[inst.func]) {
|
|
1146
|
-
kvmProgram[inst.func] = kvm;
|
|
1147
|
-
}
|
|
1148
|
-
if (inst.branches) {
|
|
1149
|
-
for (const b of inst.branches) scan(b.body);
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
};
|
|
1153
|
-
scan(kvm.body);
|
|
1154
|
-
} else if (pkirInput.functions) {
|
|
1155
|
-
entry =
|
|
1156
|
-
pkirInput.entry ||
|
|
1157
|
-
options.entry ||
|
|
1158
|
-
Object.keys(pkirInput.functions)[0] ||
|
|
1159
|
-
"__main__";
|
|
1160
|
-
kvmProgram = {};
|
|
1161
|
-
for (const [name, f] of Object.entries(pkirInput.functions)) {
|
|
1162
|
-
kvmProgram[name] = lowerPKIRToKVM(f);
|
|
1163
|
-
}
|
|
1164
|
-
} else {
|
|
1165
|
-
const kvm = lowerPKIRToKVM(pkirInput);
|
|
1166
|
-
entry = pkirInput.name || options.entry || "__main__";
|
|
1167
|
-
kvmProgram = { [entry]: kvm };
|
|
1168
|
-
}
|
|
1169
|
-
return compileLLVMArtifactFromKVM(
|
|
1170
|
-
{
|
|
1171
|
-
format: "k-vm",
|
|
1172
|
-
layer: "KVM",
|
|
1173
|
-
entry,
|
|
1174
|
-
functions: kvmProgram
|
|
1175
|
-
},
|
|
1176
|
-
options
|
|
1177
|
-
);
|
|
1178
|
-
}
|
|
1179
|
-
|
|
1180
1074
|
export function compileLLVMArtifactFromObject(object, options = {}) {
|
|
1181
1075
|
return compileObjectToLLVM(object, options);
|
|
1182
1076
|
}
|
|
@@ -1192,7 +1086,6 @@ export default {
|
|
|
1192
1086
|
compileBufferToLLVM,
|
|
1193
1087
|
compileObjectToLLVM,
|
|
1194
1088
|
compileLLVMArtifactFromKVM,
|
|
1195
|
-
compileLLVMArtifactFromPKIR,
|
|
1196
1089
|
compileLLVMArtifactFromObject,
|
|
1197
1090
|
emitLLVMModule,
|
|
1198
1091
|
llvmIdentifier
|
|
@@ -21,7 +21,6 @@ function helpText() {
|
|
|
21
21
|
" --retype rel Relation to specialize (default: main).",
|
|
22
22
|
" --input-pattern value Input pattern property-list JSON or file.",
|
|
23
23
|
" --expect value Expected output value JSON/text or file.",
|
|
24
|
-
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
25
24
|
" -h, --help Show this help.",
|
|
26
25
|
"",
|
|
27
26
|
"Without --expect, the runner prints the result as compact JSON.",
|
|
@@ -66,7 +65,6 @@ try {
|
|
|
66
65
|
let relation = null;
|
|
67
66
|
let inputPattern = null;
|
|
68
67
|
let expectedText = null;
|
|
69
|
-
let usePKIR = false;
|
|
70
68
|
while (args.length > 0 && args[0].startsWith("--")) {
|
|
71
69
|
const option = args.shift();
|
|
72
70
|
if (option === "--retype") {
|
|
@@ -82,8 +80,6 @@ try {
|
|
|
82
80
|
if (!expectedText) {
|
|
83
81
|
throw new Error("--expect requires a value or file path");
|
|
84
82
|
}
|
|
85
|
-
} else if (option === "--pkir") {
|
|
86
|
-
usePKIR = true;
|
|
87
83
|
} else {
|
|
88
84
|
throw new Error(`Unknown option: ${option}`);
|
|
89
85
|
}
|
|
@@ -106,8 +102,7 @@ try {
|
|
|
106
102
|
relation: relation || object.main,
|
|
107
103
|
input,
|
|
108
104
|
expected,
|
|
109
|
-
inputPattern: readPattern(inputPattern)
|
|
110
|
-
usePKIR
|
|
105
|
+
inputPattern: readPattern(inputPattern)
|
|
111
106
|
});
|
|
112
107
|
stdout.write(expected == null ? result.stdout : "OK\n");
|
|
113
108
|
} catch (error) {
|
|
@@ -156,8 +156,7 @@ export function compileCaseExecutable({
|
|
|
156
156
|
cacheDir,
|
|
157
157
|
sourceLabel,
|
|
158
158
|
runtimeMode = "fast",
|
|
159
|
-
clangOpt = "-O3"
|
|
160
|
-
usePKIR = false
|
|
159
|
+
clangOpt = "-O3"
|
|
161
160
|
}) {
|
|
162
161
|
const key = sha256([
|
|
163
162
|
"k-llvm-perf-v1",
|
|
@@ -168,7 +167,7 @@ export function compileCaseExecutable({
|
|
|
168
167
|
relationName,
|
|
169
168
|
relHash,
|
|
170
169
|
JSON.stringify(inputPattern),
|
|
171
|
-
|
|
170
|
+
"kir"
|
|
172
171
|
].join("\0"));
|
|
173
172
|
const exePath = path.join(cacheDir, `${key}.exe`);
|
|
174
173
|
if (!fs.existsSync(exePath)) {
|
|
@@ -180,8 +179,7 @@ export function compileCaseExecutable({
|
|
|
180
179
|
relation: relationName,
|
|
181
180
|
inputPattern,
|
|
182
181
|
runtimeMode,
|
|
183
|
-
clangOpt
|
|
184
|
-
usePKIR
|
|
182
|
+
clangOpt
|
|
185
183
|
}
|
|
186
184
|
);
|
|
187
185
|
fs.renameSync(tmpPath, exePath);
|
|
@@ -3,12 +3,10 @@ import { compileObjectBuffer, decodeObject } from "@fraczak/k/object.mjs";
|
|
|
3
3
|
import {
|
|
4
4
|
compileObjectToLLVM,
|
|
5
5
|
compileLLVMArtifactFromKVM,
|
|
6
|
-
compileLLVMArtifactFromPKIR,
|
|
7
6
|
emitLLVMModule,
|
|
8
7
|
llvmIdentifier
|
|
9
8
|
} from "../src/llvm.mjs";
|
|
10
9
|
import { objectToKVMArtifact } from "@fraczak/k/kvm.mjs";
|
|
11
|
-
import { compileRelationToPKIR } from "@fraczak/k/backend-api.mjs";
|
|
12
10
|
|
|
13
11
|
function kirP(body, { patternGraph = null } = {}) {
|
|
14
12
|
return {
|
|
@@ -297,18 +295,4 @@ assert.match(kvmLLVM, /source_filename = "k-llvm:__main__"/);
|
|
|
297
295
|
assert.match(kvmLLVM, /define %k_result @k_main/);
|
|
298
296
|
assert.match(kvmLLVM, /store ptr %product_fields\d+, ptr %ptr_slot\d+/);
|
|
299
297
|
|
|
300
|
-
// Test PKIR lowering via compileObjectToLLVM with usePKIR: true
|
|
301
|
-
const pkirObjectLLVM = compileObjectToLLVM(productObject, {
|
|
302
|
-
usePKIR: true
|
|
303
|
-
});
|
|
304
|
-
assert.match(pkirObjectLLVM.llvm, /source_filename = "k-llvm:__main__"/);
|
|
305
|
-
assert.match(pkirObjectLLVM.llvm, /define %k_result @k_main/);
|
|
306
|
-
|
|
307
|
-
// Test compileLLVMArtifactFromPKIR directly
|
|
308
|
-
const mainRel = productObject.rels[productObject.main || "__main__"];
|
|
309
|
-
const pkirFromRel = compileRelationToPKIR(mainRel, "__main__");
|
|
310
|
-
const pkirLLVM = compileLLVMArtifactFromPKIR(pkirFromRel);
|
|
311
|
-
assert.match(pkirLLVM, /source_filename = "k-llvm:__main__"/);
|
|
312
|
-
assert.match(pkirLLVM, /define %k_result @k_main/);
|
|
313
|
-
|
|
314
298
|
console.log("OK");
|
package/backends/wasm/README.md
CHANGED
|
@@ -34,7 +34,7 @@ node codecs/unit.mjs --parse |
|
|
|
34
34
|
|
|
35
35
|
| Command | Purpose |
|
|
36
36
|
| --- | --- |
|
|
37
|
-
| `k-wasm` | Compile and execute a source expression/file, `.ko`,
|
|
37
|
+
| `k-wasm` | Compile and execute a source expression/file, `.ko`, or `.kvm` |
|
|
38
38
|
| `k-wasm-compile` | Produce a `.wasm` artifact from those program forms |
|
|
39
39
|
| `k-wasm-run` | Execute a previously compiled `.wasm` artifact |
|
|
40
40
|
|
|
@@ -45,8 +45,7 @@ input.
|
|
|
45
45
|
|
|
46
46
|
Compile commands accept one `--lib file` and repeated `--use alias` or
|
|
47
47
|
`--use libraryAlias:localAlias` options before the program. Libraries can
|
|
48
|
-
be `.k` source or `.klib` objects. `--
|
|
49
|
-
lowering path; `-o` / `--output` selects an output path.
|
|
48
|
+
be `.k` source or `.klib` objects. `-o` / `--output` selects an output path.
|
|
50
49
|
|
|
51
50
|
Existing paths are read as files. Recognized missing filenames are errors;
|
|
52
51
|
other program arguments are treated as inline source.
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
function helpText() {
|
|
13
13
|
const prog = argv[1] || "k-wasm-compile.mjs";
|
|
14
14
|
return [
|
|
15
|
-
"Compile k source, .ko,
|
|
15
|
+
"Compile k source, .ko, or .kvm into a WebAssembly (.wasm)",
|
|
16
16
|
"artifact.",
|
|
17
17
|
"",
|
|
18
18
|
`Usage: ${prog} [options]`,
|
|
@@ -20,13 +20,12 @@ function helpText() {
|
|
|
20
20
|
"",
|
|
21
21
|
"Arguments:",
|
|
22
22
|
" source-snippet Inline k source snippet (in the style of k.mjs).",
|
|
23
|
-
" input-file Source .k, .ko,
|
|
23
|
+
" input-file Source .k, .ko, or .kvm file.",
|
|
24
24
|
" Reads UTF-8 source from stdin when omitted.",
|
|
25
25
|
" output-wasm Output .wasm path. Writes to stdout if omitted.",
|
|
26
26
|
"",
|
|
27
27
|
"Options:",
|
|
28
28
|
" -o, --output path Specify output file path explicitly.",
|
|
29
|
-
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
30
29
|
" --lib file Load one .klib or .k library before compiling.",
|
|
31
30
|
" --use spec Use library alias in scope (repeatable).",
|
|
32
31
|
" spec is 'name' or 'libname:localname'.",
|
|
@@ -35,7 +34,6 @@ function helpText() {
|
|
|
35
34
|
"Examples:",
|
|
36
35
|
` ${prog} 'x = |x; x x' program.wasm`,
|
|
37
36
|
` ${prog} program.k -o program.wasm`,
|
|
38
|
-
` ${prog} --pkir program.k -o program.wasm`,
|
|
39
37
|
` ${prog} --lib core.klib --use add program.k program.wasm`
|
|
40
38
|
].join("\n");
|
|
41
39
|
}
|
|
@@ -66,7 +64,7 @@ try {
|
|
|
66
64
|
}
|
|
67
65
|
}
|
|
68
66
|
|
|
69
|
-
const { libraries, useSpecs
|
|
67
|
+
const { libraries, useSpecs } =
|
|
70
68
|
parseCompileOptions(remainingArgs);
|
|
71
69
|
const input = resolveProgramInput(remainingArgs, {
|
|
72
70
|
allowStdinSource: true
|
|
@@ -77,8 +75,7 @@ try {
|
|
|
77
75
|
const artifact = await compileProgramInput(input, {
|
|
78
76
|
libraries,
|
|
79
77
|
useSpecs,
|
|
80
|
-
stdin
|
|
81
|
-
usePKIR
|
|
78
|
+
stdin
|
|
82
79
|
});
|
|
83
80
|
if (outputPath == null) {
|
|
84
81
|
stdout.write(artifact);
|