@fraczak/k 7.0.1 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/DOCS/BIG_PICTURE.md +5 -5
  3. package/DOCS/FILE_FORMATS.md +241 -18
  4. package/DOCS/OBJECT_FILE_AND_PATTERN.md +9 -9
  5. package/DOCS/PATTERNS.md +3 -3
  6. package/DOCS/POLYMORPHIC_IR.md +104 -0
  7. package/DOCS/PROFILING_AND_TRACING.md +60 -72
  8. package/DOCS/REPL.md +3 -3
  9. package/DOCS/book/03-types-as-finite-automata.md +4 -4
  10. package/DOCS/book/06-values-in-memory.md +9 -9
  11. package/DOCS/book/08-operational-semantics-and-execution.md +1 -1
  12. package/DOCS/book/09-compiler-architecture.md +2 -2
  13. package/DOCS/book/10-from-ast-to-intermediate-representation.md +2 -2
  14. package/DOCS/book/14-canonical-serialization.md +1 -1
  15. package/Examples/arithmetics.k +2 -2
  16. package/Examples/bnat.k +1 -1
  17. package/Examples/karatsuba-mult.k +2 -2
  18. package/JOURNAL.md +288 -0
  19. package/README.md +13 -10
  20. package/access-table.mjs +343 -0
  21. package/backend-api.mjs +18 -0
  22. package/backends/llvm/README.md +20 -21
  23. package/backends/llvm/bin/k-llvm-build.mjs +105 -32
  24. package/backends/llvm/bin/k-llvm-compile.mjs +63 -24
  25. package/backends/llvm/bin/k-llvm.mjs +292 -0
  26. package/backends/llvm/runtime/krt.c +41 -0
  27. package/backends/llvm/runtime/krt.h +2 -0
  28. package/backends/llvm/src/cli.mjs +95 -17
  29. package/backends/llvm/src/executable.mjs +132 -15
  30. package/backends/llvm/src/kvm2llvm.mjs +50 -23
  31. package/backends/llvm/src/llvm.mjs +133 -20
  32. package/backends/llvm/tests/integration.sh +11 -11
  33. package/backends/llvm/{bin → tests}/k-llvm-run.mjs +48 -22
  34. package/backends/llvm/tests/perf-support.mjs +25 -58
  35. package/backends/llvm/tests/test-llvm.mjs +25 -4
  36. package/backends/wasm/bin/k-wasm-compile.mjs +46 -20
  37. package/backends/wasm/bin/k-wasm-run.mjs +61 -20
  38. package/backends/wasm/bin/k-wasm.mjs +47 -13
  39. package/backends/wasm/src/cli.mjs +141 -19
  40. package/backends/wasm/src/wasm.mjs +132 -24
  41. package/backends/wasm/tests/test-wasm.mjs +5 -5
  42. package/browser/repl-browser.mjs +16 -5
  43. package/browser/vfs-data.mjs +5 -5
  44. package/codecs/k-parse.mjs +29 -8
  45. package/codecs/k-print.mjs +22 -6
  46. package/codecs/runtime/codec-sdk.mjs +8 -4
  47. package/codecs/show.mjs +20 -6
  48. package/core.k +8 -8
  49. package/harness/format.mjs +2 -2
  50. package/harness/registry.mjs +0 -2
  51. package/k.mjs +56 -16
  52. package/kir.mjs +200 -75
  53. package/kvm.mjs +49 -12
  54. package/objects/compile.mjs +37 -23
  55. package/objects/decompile.mjs +40 -10
  56. package/objects/extract-aliases.mjs +44 -14
  57. package/objects/inspect.mjs +19 -14
  58. package/objects/validate.mjs +18 -11
  59. package/package.json +10 -7
  60. package/pkir.mjs +772 -0
  61. package/repl.mjs +36 -18
  62. package/scripts/benchmark-idempotency.mjs +12 -8
  63. package/scripts/benchmark.mjs +5 -9
  64. package/scripts/build-repl-html.mjs +16 -4
  65. package/scripts/perf-ieee.mjs +253 -103
  66. package/scripts/perf-int.mjs +585 -297
  67. package/scripts/perf-multiplication.mjs +66 -25
  68. package/scripts/perf-poly.mjs +292 -164
  69. package/scripts/run-tests.mjs +3 -0
  70. package/tests/code-derivation/pattern-envelope.mjs +4 -4
  71. package/tests/test-access-table.mjs +213 -0
  72. package/tests/test-kvm-polymorphic.mjs +2 -2
  73. package/tests/test-pkir.mjs +530 -0
  74. package/tests/test-polymorphic-tdd.mjs +196 -0
  75. package/tests/test-repl-html.mjs +15 -2
  76. package/tests/test-repl.mjs +30 -1
  77. package/backends/arm64/README.md +0 -103
  78. package/backends/arm64/bin/k-arm64-compile.mjs +0 -93
  79. package/backends/arm64/bin/k-arm64-run.mjs +0 -58
  80. package/backends/arm64/bin/k-arm64.mjs +0 -88
  81. package/backends/arm64/scripts/run-tests.mjs +0 -57
  82. package/backends/arm64/src/arm64.mjs +0 -229
  83. package/backends/arm64/src/cli.mjs +0 -155
  84. package/backends/arm64/src/kvm2arm64.mjs +0 -906
  85. package/backends/arm64/src/runtime.c +0 -744
  86. package/backends/arm64/tests/integration.sh +0 -112
  87. package/backends/arm64/tests/test-arm64.mjs +0 -240
  88. package/backends/llvm/bin/k-llvm-jit.mjs +0 -160
  89. package/harness/adapters/arm64.mjs +0 -105
package/backend-api.mjs CHANGED
@@ -27,3 +27,21 @@ export {
27
27
  isIdentityIntrinsic,
28
28
  unsupportedIntrinsic
29
29
  } from "./intrinsics.mjs";
30
+ export { buildAccessTable, AccessTable } from "./access-table.mjs";
31
+ export {
32
+ PKIR_FORMAT,
33
+ PKIR_VERSION,
34
+ compileRelationToPKIR,
35
+ executePKIR,
36
+ specializePKIR,
37
+ valueToKVal,
38
+ kvalToValue,
39
+ lowerPKIRToKVM
40
+ } from "./pkir.mjs";
41
+ export {
42
+ compileWasmArtifactFromPKIR
43
+ } from "./backends/wasm/src/wasm.mjs";
44
+ export {
45
+ compileLLVMArtifactFromPKIR
46
+ } from "./backends/llvm/src/llvm.mjs";
47
+
@@ -46,34 +46,33 @@ status in `k_result`. These operations can compose through KIR `comp`.
46
46
  ```sh
47
47
  k-llvm-build [options] [source-snippet | input-file [output-exe]]
48
48
  k-llvm-compile [options] [source-snippet | input-file [output.ll]]
49
- k-llvm-jit [options] object.ko
50
- k-llvm-run [options] object.ko [input.kv]
49
+ k-llvm [options] ( source-snippet | input-file ) [ input-wire-file ]
51
50
  ```
52
51
 
53
52
  Options:
54
53
 
55
54
  - `-o, --output path`: specify output file path explicitly.
56
55
  - `--lib file`: load one `.klib` or `.k` dependency before compiling.
57
- - `--export spec`: export a library alias into source scope (`name` or `libname:localname`). May be repeated.
58
- - `--input-pattern json-or-file`: for `k-llvm-run`, KIR property-list input pattern.
59
- - `--retype rel`: for `k-llvm-run`, relation to specialize (defaults to object main).
60
- - `--expect value-or-file`: for `k-llvm-run`, compare the output value against expected value text.
61
- - `-h`, `--help`: show usage.
62
-
63
- The compile commands follow the unified k CLI input convention: pass inline
64
- source, an existing `.k` source file, or a `.ko`/`.klib` object. Reads UTF-8
65
- source from stdin when input is omitted. Output paths can be specified as a
66
- positional argument or explicitly with `-o, --output`.
67
- Without `--expect`, `k-llvm-run` prints the result value as compact JSON.
56
+ - `--export spec`: export a library alias into scope. Repeatable.
57
+ - `--pkir`: lower via Polymorphic K IR (PKIR).
58
+ - `--retype rel`: relation to specialize (defaults to object main).
59
+ - `--opt flag`: Clang optimization flag (-O0, -O1, -O2, -O3).
60
+ - `-h, --help`: show usage.
61
+
62
+ The compile commands follow the unified k CLI input convention: pass
63
+ inline source, an existing `.k` source file, or a `.ko`/`.klib` object.
64
+ Reads UTF-8 source from stdin when input is omitted. Output paths can be
65
+ specified as a positional argument or explicitly with `-o, --output`.
68
66
  `k-llvm-build` emits a native executable that reads and writes the binary
69
- k pattern+value envelope used by `codecs/k-parse.mjs` and `codecs/k-print.mjs`.
70
- The executable checks the stdin envelope pattern against the compiled input
71
- pattern and encodes stdout with the compiled output pattern.
72
- `k-llvm-jit` reads the stdin binary envelope first, specializes the object with
73
- that input pattern, caches the native executable, and writes the compiled
74
- output envelope to stdout.
75
- The native envelope codec supports the k Unicode string encoding used for
76
- labels and tags.
67
+ k pattern+value envelope used by `codecs/k-parse.mjs` and
68
+ `codecs/k-print.mjs`. The executable checks the stdin envelope pattern
69
+ against the compiled input pattern and encodes stdout with the compiled
70
+ output pattern.
71
+ `k-llvm` reads the binary wire envelope, specializes the relation to
72
+ that input pattern, caches the compiled native executable, and writes
73
+ the compiled output envelope to stdout.
74
+ The native envelope codec supports the k Unicode string encoding used
75
+ for labels and tags.
77
76
 
78
77
  ## Tests
79
78
 
@@ -9,35 +9,61 @@ import {
9
9
  } from "../src/cli.mjs";
10
10
  import {
11
11
  compileKVMToExecutable,
12
- compileObjectToExecutable
12
+ compileObjectToExecutable,
13
+ compilePKIRToExecutable
13
14
  } from "../src/executable.mjs";
14
15
 
15
- function usage(stream = console.error) {
16
+ function helpText() {
16
17
  const prog = argv[1] || "k-llvm-build.mjs";
17
- stream(`Usage: node ${prog} [options] [source-snippet | input-file [output-exe]]`);
18
- stream("Compile k source, .ko, .klib, or .kvm input into a native executable.");
19
- stream("");
20
- stream("Arguments:");
21
- stream(" source-snippet Inline k source, in the same style as k.mjs.");
22
- stream(" input-file Source .k, .ko, .klib, or .kvm file. Reads UTF-8 source from stdin when omitted.");
23
- stream(" output-exe Output executable path.");
24
- stream("");
25
- stream("The executable reads a binary k pattern+value envelope from stdin and");
26
- stream("writes a binary k pattern+value envelope to stdout.");
27
- stream("The stdin envelope pattern must match the compiled input pattern;");
28
- stream("stdout is encoded with the compiled output pattern.");
29
- stream("");
30
- stream("Options:");
31
- stream(" -o, --output path Specify output file path explicitly.");
32
- stream(" --input-pattern pattern Input pattern JSON or file path for specialization.");
33
- stream(" --lib file Load one .klib dependency before compiling.");
34
- stream(" --export spec Export a library alias into source scope. May be repeated.");
35
- stream(" spec is 'name' or 'libname:localname'.");
36
- stream(" -h, --help Show this help.");
18
+ return [
19
+ "Compile k source, .ko, .klib, .kvm, or .pkir into a native",
20
+ "standalone executable.",
21
+ "",
22
+ `Usage: ${prog} [options]`,
23
+ " [source-snippet | input-file [output-exe]]",
24
+ "",
25
+ "Arguments:",
26
+ " source-snippet Inline k source snippet (in the style of k.mjs).",
27
+ " input-file Source .k, .ko, .klib, .kvm, or .pkir file.",
28
+ " Reads UTF-8 source from stdin when omitted.",
29
+ " output-exe Output executable path.",
30
+ "",
31
+ "The executable reads a binary k pattern+value envelope from stdin",
32
+ "and writes a binary k pattern+value envelope to stdout.",
33
+ "The stdin envelope pattern must match the compiled input pattern;",
34
+ "stdout is encoded with the compiled output pattern.",
35
+ "",
36
+ "Options:",
37
+ " -o, --output path Specify output file path explicitly.",
38
+ " --pkir Lower via Polymorphic K IR (PKIR).",
39
+ " --input-pattern pattern Input pattern JSON or file for spec.",
40
+ " --retype rel Relation to specialize (default: main).",
41
+ " --lib file Load .klib or .k library before build.",
42
+ " --export spec Export library alias into scope. Repeatable.",
43
+ " spec is 'name' or 'libname:localname'.",
44
+ " --opt flag Clang optimization flag (default: -O3).",
45
+ " -h, --help Show this help.",
46
+ "",
47
+ "Environment Variables:",
48
+ " CLANG, CC Path to Clang compiler (default: clang).",
49
+ " K_LLVM_CLANG_OPT Clang optimization flag (default: -O3).",
50
+ " K_LLVM_RUNTIME_MODE Runtime mode: fast, compact (default: fast).",
51
+ "",
52
+ "Examples:",
53
+ ` ${prog} 'x = |x; x x' -o program.exe`,
54
+ ` ${prog} program.k program.exe`,
55
+ ` ${prog} --lib Examples/poly.k --export concat 's' -o s.exe`
56
+ ].join("\n");
57
+ }
58
+
59
+ function usage(stream = console.error) {
60
+ stream(helpText());
37
61
  }
38
62
 
39
63
  function readMaybeFile(textOrPath) {
40
- return fs.existsSync(textOrPath) ? fs.readFileSync(textOrPath, "utf8") : textOrPath;
64
+ return fs.existsSync(textOrPath)
65
+ ? fs.readFileSync(textOrPath, "utf8")
66
+ : textOrPath;
41
67
  }
42
68
 
43
69
  function readPattern(inputPattern) {
@@ -54,17 +80,38 @@ try {
54
80
 
55
81
  let explicitOutput = null;
56
82
  let rawInputPattern = null;
83
+ let relation = null;
84
+ let clangOpt = process.env.K_LLVM_CLANG_OPT || "-O3";
85
+ let runtimeMode = process.env.K_LLVM_RUNTIME_MODE || "fast";
57
86
  const remainingArgs = [];
58
87
  while (args.length > 0) {
59
88
  const arg = args[0];
60
89
  if (arg === "-o" || arg === "--output") {
61
90
  args.shift();
62
91
  explicitOutput = args.shift();
63
- if (!explicitOutput) throw new Error("-o/--output requires a file argument");
92
+ if (!explicitOutput) {
93
+ throw new Error("-o/--output requires a file argument");
94
+ }
64
95
  } else if (arg === "--input-pattern") {
65
96
  args.shift();
66
97
  rawInputPattern = args.shift();
67
- if (!rawInputPattern) throw new Error("--input-pattern requires a JSON or file argument");
98
+ if (!rawInputPattern) {
99
+ throw new Error(
100
+ "--input-pattern requires a JSON or file argument"
101
+ );
102
+ }
103
+ } else if (arg === "--retype") {
104
+ args.shift();
105
+ relation = args.shift();
106
+ if (!relation) {
107
+ throw new Error("--retype requires a relation name");
108
+ }
109
+ } else if (arg === "--opt") {
110
+ args.shift();
111
+ clangOpt = args.shift();
112
+ if (!clangOpt) {
113
+ throw new Error("--opt requires a flag (e.g. -O3)");
114
+ }
68
115
  } else {
69
116
  remainingArgs.push(args.shift());
70
117
  }
@@ -72,17 +119,38 @@ try {
72
119
 
73
120
  const inputPattern = readPattern(rawInputPattern);
74
121
 
75
- const { libraries, exportSpecs } = parseCompileOptions(remainingArgs);
76
- const input = resolveProgramInput(remainingArgs, { allowStdinSource: true });
122
+ const { libraries, exportSpecs, usePKIR } =
123
+ parseCompileOptions(remainingArgs);
124
+ const input = resolveProgramInput(remainingArgs, {
125
+ allowStdinSource: true
126
+ });
77
127
  const positionalOutput = remainingArgs.shift();
78
128
  if (remainingArgs.length > 0) throw new Error("Too many arguments");
79
129
 
80
130
  const outputPath = explicitOutput || positionalOutput || null;
81
- if (outputPath == null) throw new Error("Output executable path is required (pass output-exe or -o/--output)");
131
+ if (outputPath == null) {
132
+ throw new Error(
133
+ "Output executable path required (pass output-exe or -o/--output)"
134
+ );
135
+ }
82
136
 
83
- if (input.kind === "kvm") {
137
+ if (input.kind === "pkir") {
138
+ const pkir = JSON.parse(fs.readFileSync(input.path, "utf8"));
139
+ compilePKIRToExecutable(pkir, outputPath, {
140
+ entry: relation,
141
+ inputPattern,
142
+ clangOpt,
143
+ runtimeMode
144
+ });
145
+ } else if (input.kind === "kvm") {
84
146
  const kvm = JSON.parse(fs.readFileSync(input.path, "utf8"));
85
- compileKVMToExecutable(kvm, outputPath, { inputPattern });
147
+ compileKVMToExecutable(kvm, outputPath, {
148
+ entry: relation,
149
+ inputPattern,
150
+ clangOpt,
151
+ runtimeMode,
152
+ usePKIR
153
+ });
86
154
  } else {
87
155
  const object = await compileProgramInputToObject(input, {
88
156
  libraries,
@@ -90,10 +158,15 @@ try {
90
158
  stdin
91
159
  });
92
160
 
93
- compileObjectToExecutable(object, outputPath, { inputPattern });
161
+ compileObjectToExecutable(object, outputPath, {
162
+ relation: relation || object.main,
163
+ inputPattern,
164
+ clangOpt,
165
+ runtimeMode,
166
+ usePKIR
167
+ });
94
168
  }
95
169
  } catch (error) {
96
170
  console.error(error.stack || error.message || String(error));
97
- usage();
98
171
  exit(1);
99
172
  }
@@ -9,26 +9,44 @@ import {
9
9
  } from "../src/cli.mjs";
10
10
  import {
11
11
  compileObjectToLLVM,
12
- compileLLVMArtifactFromKVM
12
+ compileLLVMArtifactFromKVM,
13
+ compileLLVMArtifactFromPKIR
13
14
  } from "../src/llvm.mjs";
14
15
  import { inputPatternForObjectRelation } from "../src/executable.mjs";
15
16
 
16
- function usage(stream = console.error) {
17
+ function helpText() {
17
18
  const prog = argv[1] || "k-llvm-compile.mjs";
18
- stream(`Usage: node ${prog} [options] [source-snippet | input-file [output.ll]]`);
19
- stream("Compile a k .ko/.klib object, source, or .kvm input into prototype LLVM IR.");
20
- stream("");
21
- stream("Arguments:");
22
- stream(" source-snippet Inline k source, in the same style as k.mjs.");
23
- stream(" input-file Source .k, .ko, .klib, or .kvm file. Reads UTF-8 source from stdin when omitted.");
24
- stream(" output.ll Output LLVM IR path. Writes to stdout when omitted.");
25
- stream("");
26
- stream("Options:");
27
- stream(" -o, --output path Specify output file path explicitly.");
28
- stream(" --lib file Load one .klib dependency before compiling.");
29
- stream(" --export spec Export a library alias into source scope. May be repeated.");
30
- stream(" spec is 'name' or 'libname:localname'.");
31
- stream(" -h, --help Show this help.");
19
+ return [
20
+ "Compile a k .ko/.klib object, source, .kvm, or .pkir into LLVM IR.",
21
+ "",
22
+ `Usage: ${prog} [options]`,
23
+ " [source-snippet | input-file [output.ll]]",
24
+ "",
25
+ "Arguments:",
26
+ " source-snippet Inline k source snippet (in the style of k.mjs).",
27
+ " input-file Source .k, .ko, .klib, .kvm, or .pkir file.",
28
+ " Reads UTF-8 source from stdin when omitted.",
29
+ " output.ll Output LLVM IR path (default: stdout).",
30
+ "",
31
+ "Options:",
32
+ " -o, --output path Specify output file path explicitly.",
33
+ " --pkir Lower via Polymorphic K IR (PKIR).",
34
+ " --retype rel Relation to compile (default: object main).",
35
+ " --lib file Load one .klib or .k library before compiling.",
36
+ " --export spec Export library alias into scope. Repeatable.",
37
+ " spec is 'name' or 'libname:localname'.",
38
+ " -h, --help Show this help.",
39
+ "",
40
+ "Examples:",
41
+ ` ${prog} 'x = |x; x x' program.ll`,
42
+ ` ${prog} program.k -o program.ll`,
43
+ ` ${prog} --pkir program.k -o program.ll`,
44
+ ` ${prog} --lib core.klib --export add program.k program.ll`
45
+ ].join("\n");
46
+ }
47
+
48
+ function usage(stream = console.error) {
49
+ stream(helpText());
32
50
  }
33
51
 
34
52
  try {
@@ -39,29 +57,49 @@ try {
39
57
  }
40
58
 
41
59
  let explicitOutput = null;
60
+ let relation = null;
42
61
  const remainingArgs = [];
43
62
  while (args.length > 0) {
44
63
  const arg = args[0];
45
64
  if (arg === "-o" || arg === "--output") {
46
65
  args.shift();
47
66
  explicitOutput = args.shift();
48
- if (!explicitOutput) throw new Error("-o/--output requires a file argument");
67
+ if (!explicitOutput) {
68
+ throw new Error("-o/--output requires a file argument");
69
+ }
70
+ } else if (arg === "--retype") {
71
+ args.shift();
72
+ relation = args.shift();
73
+ if (!relation) {
74
+ throw new Error("--retype requires a relation name");
75
+ }
49
76
  } else {
50
77
  remainingArgs.push(args.shift());
51
78
  }
52
79
  }
53
80
 
54
- const { libraries, exportSpecs } = parseCompileOptions(remainingArgs);
55
- const input = resolveProgramInput(remainingArgs, { allowStdinSource: true });
81
+ const { libraries, exportSpecs, usePKIR } =
82
+ parseCompileOptions(remainingArgs);
83
+ const input = resolveProgramInput(remainingArgs, {
84
+ allowStdinSource: true
85
+ });
56
86
  const positionalOutput = remainingArgs.shift();
57
87
  if (remainingArgs.length > 0) throw new Error("Too many arguments");
58
88
 
59
89
  const outputPath = explicitOutput || positionalOutput || null;
60
90
 
61
91
  let llvm;
62
- if (input.kind === "kvm") {
92
+ if (input.kind === "pkir") {
93
+ const pkir = JSON.parse(fs.readFileSync(input.path, "utf8"));
94
+ const entry = relation || pkir.entry || pkir.name || "__main__";
95
+ llvm = compileLLVMArtifactFromPKIR(pkir, {
96
+ relation: entry,
97
+ inputPattern: pkir.inputPattern,
98
+ outputPattern: pkir.outputPattern
99
+ });
100
+ } else if (input.kind === "kvm") {
63
101
  const kvm = JSON.parse(fs.readFileSync(input.path, "utf8"));
64
- const entry = kvm.entry || kvm.relation || "__main__";
102
+ const entry = relation || kvm.entry || kvm.relation || "__main__";
65
103
  const mainFunc = kvm.functions?.[entry];
66
104
  const artifact = compileLLVMArtifactFromKVM(kvm, {
67
105
  relation: entry,
@@ -76,9 +114,11 @@ try {
76
114
  stdin
77
115
  });
78
116
 
117
+ const targetRel = relation || object.main;
79
118
  const res = compileObjectToLLVM(object, {
80
- relation: object.main,
81
- inputPattern: inputPatternForObjectRelation(object, object.main)
119
+ relation: targetRel,
120
+ inputPattern: inputPatternForObjectRelation(object, targetRel),
121
+ usePKIR
82
122
  });
83
123
  llvm = res.llvm;
84
124
  }
@@ -90,6 +130,5 @@ try {
90
130
  }
91
131
  } catch (error) {
92
132
  console.error(error.stack || error.message || String(error));
93
- usage();
94
133
  exit(1);
95
134
  }