@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,477 @@
|
|
|
1
|
+
-- =============================================================================
|
|
2
|
+
-- ktvm/fixtures/integer-streaming.k
|
|
3
|
+
-- Streaming (Tail-Modulo-Constructor) & Direct-Recursion Integer Arithmetic
|
|
4
|
+
--
|
|
5
|
+
-- Semantically identical drop-in alternative to `lib/integer.k` written for a
|
|
6
|
+
-- pull-driven streaming transducer network rather than a call-stack VM:
|
|
7
|
+
--
|
|
8
|
+
-- Tail-Modulo-Constructor (TMC / guarded streaming) upgrades:
|
|
9
|
+
-- 1. `lsb_shl` / `_lsb_shl_nz` : guards x != _ once, then streams |0 online
|
|
10
|
+
-- 2. `lsb_add` / `_lsb_add` : streams sum bits online in O(1) FIFO memory,
|
|
11
|
+
-- O(1) tail-splices .x/.y on carry=0, drops
|
|
12
|
+
-- `acc` and `msb_drop_zeros rev_bits`
|
|
13
|
+
-- 3. `half_max_tally` : streams |0 online directly (no `acc` helper)
|
|
14
|
+
-- 4. `lsb_trim` / `_lsb_trim` : single-pass online trailing-zero trimmer
|
|
15
|
+
-- (buffers only active '0' runs; zero reversals)
|
|
16
|
+
-- 5. `lsb_split` / `_lsb_split_raw` : multi-output TMC streaming `lo` + online
|
|
17
|
+
-- `lsb_trim`, passing `hi` through in O(1)
|
|
18
|
+
-- 6. `nat_divmod` / `_nat_divmod` : streams quotient `q` MSB-first via TMC
|
|
19
|
+
-- (no `q_rev` accumulator, no `rev_bits` on `q`)
|
|
20
|
+
--
|
|
21
|
+
-- Direct Tree Recursion (no manual continuation stack):
|
|
22
|
+
-- 7. `lsb_mul` : textbook 3-way Karatsuba without `_k_stack[]`,
|
|
23
|
+
-- `_k_frame[]`, `_k_state[]`, `|eval`/`|apply`
|
|
24
|
+
--
|
|
25
|
+
-- Kept tail-recursive (inherently stateful / reversing / MSB-dependent):
|
|
26
|
+
-- * `trim_bits`, `_rev_bits`, `_cmp_msb`, `_lsb_sub`, `_lsb_mul_small`, `_gcd`
|
|
27
|
+
-- =============================================================================
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
-- -----------------------------------------------------------------------------
|
|
31
|
+
-- 1. Core Types, Constants, Normalization, and Predicates
|
|
32
|
+
-- -----------------------------------------------------------------------------
|
|
33
|
+
|
|
34
|
+
bit[] = < {} 0, {} 1 >;
|
|
35
|
+
bits[] = < {} _, B 0, B 1 > = B;
|
|
36
|
+
|
|
37
|
+
order[] = < {} lt, {} eq, {} gt >;
|
|
38
|
+
|
|
39
|
+
int[] = < bits[] +, bits[] - >;
|
|
40
|
+
|
|
41
|
+
0 = {}|_|+ ?int[];
|
|
42
|
+
1 = {}|_|1|+ ?int[];
|
|
43
|
+
-1 = {}|_|1|- ?int[];
|
|
44
|
+
|
|
45
|
+
-- Strips redundant leading '0' tags (already O(1) tail-recursive).
|
|
46
|
+
trim_bits = ?bits[] <
|
|
47
|
+
/_ |_,
|
|
48
|
+
/1 |1,
|
|
49
|
+
/0 trim_bits
|
|
50
|
+
> ?bits[];
|
|
51
|
+
msb_drop_zeros = trim_bits;
|
|
52
|
+
|
|
53
|
+
-- Lifts unsigned bits[] magnitude (MSB at root, '_' at leaf) to canonical non-negative int[].
|
|
54
|
+
nat = ?bits[] trim_bits |+ ?int[];
|
|
55
|
+
|
|
56
|
+
-- Canonicalizes a signed int[] (strips leading zeros and normalizes -0 to +0).
|
|
57
|
+
int = ?int[] <
|
|
58
|
+
/+ trim_bits |+,
|
|
59
|
+
/- trim_bits <
|
|
60
|
+
/_ |_|+,
|
|
61
|
+
/1 |1|-
|
|
62
|
+
>
|
|
63
|
+
> ?int[];
|
|
64
|
+
|
|
65
|
+
-- Identity filters (succeed unchanged on matching values, fail otherwise).
|
|
66
|
+
zero? = int { /+/_ _x, () x } .x ?int[];
|
|
67
|
+
pos? = int /+/1 |1|+ ?int[];
|
|
68
|
+
neg? = int /- |- ?int[];
|
|
69
|
+
|
|
70
|
+
-- Unsigned reversal (inherently O(n) buffering).
|
|
71
|
+
rev_bits = ?bits[] { () in, {} |_ acc } _rev_bits ?bits[];
|
|
72
|
+
_rev_bits = ?{ bits[] in, bits[] acc } <
|
|
73
|
+
{ .in /_ _in, .acc acc } .acc,
|
|
74
|
+
<
|
|
75
|
+
{ .in /0 in, .acc |0 acc },
|
|
76
|
+
{ .in /1 in, .acc |1 acc }
|
|
77
|
+
> _rev_bits
|
|
78
|
+
> ?bits[];
|
|
79
|
+
|
|
80
|
+
-- [TMC] Single-pass online LSB trailing-zero trimmer (replaces `rev_bits msb_drop_zeros rev_bits`).
|
|
81
|
+
-- mode=0 (scan): defers consecutive '0's into tally `z`; drops `z` on '_'; emits immediately on '1'.
|
|
82
|
+
-- mode=1 (flush): streams deferred '0's from `z`, then emits '1' and returns to scan.
|
|
83
|
+
lsb_trim = ?bits[] { () in, {} |_ z, {} |0 mode } _lsb_trim ?bits[];
|
|
84
|
+
_lsb_trim = ?{ bits[] in, bits[] z, bit[] mode } <
|
|
85
|
+
{ .in in, .z < /0, /1 > z, .mode /1 _m }
|
|
86
|
+
{ .in in, .z z, {} |1 mode } _lsb_trim |0,
|
|
87
|
+
{ .in in, .z /_ _z, .mode /1 _m }
|
|
88
|
+
{ .in in, {} |_ z, {} |0 mode } _lsb_trim |1,
|
|
89
|
+
{ .in /_ _in, .z _z, .mode /0 _m } {} |_,
|
|
90
|
+
{ .in /0 in, .z z, .mode /0 _m }
|
|
91
|
+
{ .in in, .z |0 z, {} |0 mode } _lsb_trim,
|
|
92
|
+
{ .in /1 in, .z /_ _z, .mode /0 _m }
|
|
93
|
+
{ .in in, {} |_ z, {} |0 mode } _lsb_trim |1,
|
|
94
|
+
{ .in /1 in, .z < /0, /1 > z, .mode /0 _m }
|
|
95
|
+
{ .in in, .z z, {} |1 mode } _lsb_trim |0
|
|
96
|
+
> ?bits[];
|
|
97
|
+
|
|
98
|
+
msb_to_lsb = ?bits[] msb_drop_zeros rev_bits ?bits[];
|
|
99
|
+
lsb_to_msb = ?bits[] rev_bits trim_bits ?bits[];
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
-- -----------------------------------------------------------------------------
|
|
103
|
+
-- 2. Magnitude Comparison & Bit-Shift Helpers
|
|
104
|
+
-- -----------------------------------------------------------------------------
|
|
105
|
+
|
|
106
|
+
-- Single-pass MSB-first comparison of two canonical trimmed bits[] values.
|
|
107
|
+
cmp_msb = ?{ bits[] x, bits[] y } { .x x, .y y, {} |eq ord } _cmp_msb ?order[];
|
|
108
|
+
_cmp_msb = ?{ bits[] x, bits[] y, order[] ord } <
|
|
109
|
+
{ .x /_ _x, .y /_ _y, .ord ord } .ord,
|
|
110
|
+
.x /_ |lt,
|
|
111
|
+
.y /_ |gt,
|
|
112
|
+
<
|
|
113
|
+
{ .x /0 x, .y /0 y, .ord ord },
|
|
114
|
+
{ .x /1 x, .y /1 y, .ord ord },
|
|
115
|
+
{ .x /0 x, .y /1 y, .ord </eq |lt, ()> ord },
|
|
116
|
+
{ .x /1 x, .y /0 y, .ord </eq |gt, ()> ord }
|
|
117
|
+
> _cmp_msb
|
|
118
|
+
> ?order[];
|
|
119
|
+
|
|
120
|
+
-- Shifts a trimmed LSB-first bits[] left by 1 bit or by a tally `k`.
|
|
121
|
+
lsb_shl1 = ?bits[] < /_ |_, |0 > ?bits[];
|
|
122
|
+
|
|
123
|
+
-- [TMC] Guard `x` non-empty ONCE, then stream `|0` directly forward for each token of `k`
|
|
124
|
+
-- and zero-copy splice `x` at `k == _`.
|
|
125
|
+
lsb_shl = ?{ bits[] x, bits[] k } <
|
|
126
|
+
.x /_ |_,
|
|
127
|
+
{ .x < /0 |0, /1 |1 > x, .k k } _lsb_shl_nz
|
|
128
|
+
> ?bits[];
|
|
129
|
+
|
|
130
|
+
_lsb_shl_nz = ?{ bits[] x, bits[] k } <
|
|
131
|
+
{ .x x, .k /_ _k } .x,
|
|
132
|
+
{ .x x, .k < /0, /1 > k } _lsb_shl_nz |0
|
|
133
|
+
> ?bits[];
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
-- -----------------------------------------------------------------------------
|
|
137
|
+
-- 3. Unsigned LSB Addition & Subtraction
|
|
138
|
+
-- -----------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
-- [TMC] Online ripple-carry addition on trimmed LSB-first bits[] (O(1) FIFO memory,
|
|
141
|
+
-- zero `acc`, zero `rev_bits`, and O(1) zero-copy tail splicing when carry == 0).
|
|
142
|
+
lsb_add = ?{ bits[] x, bits[] y } { .x x, .y y, {} |0 c } _lsb_add ?bits[];
|
|
143
|
+
|
|
144
|
+
_lsb_add = ?{ bits[] x, bits[] y, bit[] c } <
|
|
145
|
+
{ .x /_ _x, .y /_ _y, .c /0 _c } {} |_,
|
|
146
|
+
{ .x /_ _x, .y /_ _y, .c /1 _c } {} |_ |1,
|
|
147
|
+
{ .x /_ _x, .y < /0 |0, /1 |1 > y, .c /0 _c } .y,
|
|
148
|
+
{ .x < /0 |0, /1 |1 > x, .y /_ _y, .c /0 _c } .x,
|
|
149
|
+
{ .x _pop_lsb_bit px, .y _pop_lsb_bit py, .c c } {
|
|
150
|
+
.px .rest x,
|
|
151
|
+
.py .rest y,
|
|
152
|
+
{ .px .b a, .py .b b, .c c } _full_add_bit s
|
|
153
|
+
} <
|
|
154
|
+
{ .x x, .y y, .s .carry c, .s .sum /0 _sum } { .x x, .y y, .c c } _lsb_add |0,
|
|
155
|
+
{ .x x, .y y, .s .carry c, .s .sum /1 _sum } { .x x, .y y, .c c } _lsb_add |1
|
|
156
|
+
>
|
|
157
|
+
> ?bits[];
|
|
158
|
+
|
|
159
|
+
_full_add_bit = ?{ bit[] a, bit[] b, bit[] c } <
|
|
160
|
+
{ .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 sum, {} |0 carry },
|
|
161
|
+
{ .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 sum, {} |0 carry },
|
|
162
|
+
{ .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
|
|
163
|
+
{ .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
|
|
164
|
+
{ .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
|
|
165
|
+
{ .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
|
|
166
|
+
{ .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 sum, {} |1 carry },
|
|
167
|
+
{ .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 sum, {} |1 carry }
|
|
168
|
+
> ?{ bit[] sum, bit[] carry };
|
|
169
|
+
|
|
170
|
+
-- Subtracts y from x assuming x >= y (fails if underflow occurs).
|
|
171
|
+
lsb_sub = ?{ bits[] x, bits[] y }
|
|
172
|
+
{ .x x, .y y, {} |0 b, {} |_ acc } _lsb_sub { .b /0 _b, .r r } .r ?bits[];
|
|
173
|
+
|
|
174
|
+
-- Tries x - y: returns { b: 0, r: x - y } if x >= y, or { b: 1, r: _ } if x < y.
|
|
175
|
+
lsb_try_sub = ?{ bits[] x, bits[] y }
|
|
176
|
+
{ .x x, .y y, {} |0 b, {} |_ acc } _lsb_sub ?{ bit[] b, bits[] r };
|
|
177
|
+
|
|
178
|
+
-- Tail-recursive ripple-borrow subtraction (must buffer because underflow & trimming depend on MSB).
|
|
179
|
+
_lsb_sub = ?{ bits[] x, bits[] y, bit[] b, bits[] acc } <
|
|
180
|
+
{ .x /_ _x, .y /_ _y, .b /0 _b, .acc acc }
|
|
181
|
+
{ {} |0 b, .acc msb_drop_zeros rev_bits r },
|
|
182
|
+
.x /_ { {} |1 b, {} |_ r },
|
|
183
|
+
{ .x < /0 |0, /1 |1 > x, .y /_ _y, .b /0 _b, .acc acc } {
|
|
184
|
+
{} |0 b,
|
|
185
|
+
{ .x in, .acc acc } _rev_bits msb_drop_zeros rev_bits r
|
|
186
|
+
},
|
|
187
|
+
{ .x < /0 |0, /1 |1 > _pop_lsb_bit px, .y _pop_lsb_bit py, .b b, .acc acc } {
|
|
188
|
+
.px .rest x,
|
|
189
|
+
.py .rest y,
|
|
190
|
+
{ .px .b a, .py .b b, .b c } _full_sub_bit s,
|
|
191
|
+
.acc acc
|
|
192
|
+
} {
|
|
193
|
+
.x x,
|
|
194
|
+
.y y,
|
|
195
|
+
.s .borrow b,
|
|
196
|
+
{ .s .diff b, .acc acc } _push_bit_acc acc
|
|
197
|
+
} _lsb_sub
|
|
198
|
+
> ?{ bit[] b, bits[] r };
|
|
199
|
+
|
|
200
|
+
_full_sub_bit = ?{ bit[] a, bit[] b, bit[] c } <
|
|
201
|
+
{ .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
|
|
202
|
+
{ .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 diff, {} |1 borrow },
|
|
203
|
+
{ .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 diff, {} |1 borrow },
|
|
204
|
+
{ .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 diff, {} |1 borrow },
|
|
205
|
+
{ .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 diff, {} |0 borrow },
|
|
206
|
+
{ .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 diff, {} |0 borrow },
|
|
207
|
+
{ .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
|
|
208
|
+
{ .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 diff, {} |1 borrow }
|
|
209
|
+
> ?{ bit[] diff, bit[] borrow };
|
|
210
|
+
|
|
211
|
+
_pop_lsb_bit = ?bits[] <
|
|
212
|
+
/_ { {} |0 b, {} |_ rest },
|
|
213
|
+
/0 { {} |0 b, () rest },
|
|
214
|
+
/1 { {} |1 b, () rest }
|
|
215
|
+
> ?{ bit[] b, bits[] rest };
|
|
216
|
+
|
|
217
|
+
_push_bit_acc = ?{ bit[] b, bits[] acc } <
|
|
218
|
+
{ .b /0 _b, .acc acc } .acc |0,
|
|
219
|
+
{ .b /1 _b, .acc acc } .acc |1
|
|
220
|
+
> ?bits[];
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
-- -----------------------------------------------------------------------------
|
|
224
|
+
-- 4. Fast Multiplication (Direct Natural Karatsuba + Schoolbook Base Case)
|
|
225
|
+
-- -----------------------------------------------------------------------------
|
|
226
|
+
-- No `_k_frame[]`, `_k_stack[]`, `_k_state[]`, `|eval`, `|apply`, or `|cons`!
|
|
227
|
+
-- Three direct sibling recursive calls to `lsb_mul` feed `_karatsuba_combine`.
|
|
228
|
+
|
|
229
|
+
lsb_mul = ?{ bits[] x, bits[] y } {
|
|
230
|
+
.x x,
|
|
231
|
+
.y y,
|
|
232
|
+
.x _classify_bits cx,
|
|
233
|
+
.y _classify_bits cy
|
|
234
|
+
} <
|
|
235
|
+
{ .x _x, .y _y, .cx /zero _cx, .cy _cy } {} |_,
|
|
236
|
+
{ .x _x, .y _y, .cx < /short, /long > _cx, .cy /zero _cy } {} |_,
|
|
237
|
+
{ .x x, .y y, .cx /short _cx, .cy < /short, /long > _cy }
|
|
238
|
+
{ .x iter, .y src, {} |_ acc } _lsb_mul_small,
|
|
239
|
+
{ .x x, .y y, .cx /long _cx, .cy /short _cy }
|
|
240
|
+
{ .y iter, .x src, {} |_ acc } _lsb_mul_small,
|
|
241
|
+
{ .x x, .y y, .cx /long _cx, .cy /long _cy } {
|
|
242
|
+
.x x,
|
|
243
|
+
.y y,
|
|
244
|
+
{ .x x, .y y } half_max_tally k
|
|
245
|
+
} {
|
|
246
|
+
.k k,
|
|
247
|
+
{ .x x, .k k } lsb_split sx,
|
|
248
|
+
{ .y x, .k k } lsb_split sy
|
|
249
|
+
} {
|
|
250
|
+
.k k,
|
|
251
|
+
{ .sx .lo x, .sy .lo y } lsb_mul z0,
|
|
252
|
+
{ .sx .hi x, .sy .hi y } lsb_mul z2,
|
|
253
|
+
{
|
|
254
|
+
{ .sx .lo x, .sx .hi y } lsb_add x,
|
|
255
|
+
{ .sy .lo x, .sy .hi y } lsb_add y
|
|
256
|
+
} lsb_mul z3
|
|
257
|
+
} _karatsuba_combine
|
|
258
|
+
> ?bits[];
|
|
259
|
+
|
|
260
|
+
_karatsuba_combine = ?{ bits[] k, bits[] z0, bits[] z2, bits[] z3 } {
|
|
261
|
+
.z0 x,
|
|
262
|
+
{
|
|
263
|
+
{ { { .z3 x, .z2 y } lsb_sub x, .z0 y } lsb_sub x, .k k } lsb_shl x,
|
|
264
|
+
{ { .z2 x, .k k } lsb_shl x, .k k } lsb_shl y
|
|
265
|
+
} lsb_add y
|
|
266
|
+
} lsb_add ?bits[];
|
|
267
|
+
|
|
268
|
+
-- Base-case schoolbook shift-and-add over an operand of <= 16 bits.
|
|
269
|
+
_lsb_mul_small = ?{ bits[] iter, bits[] src, bits[] acc } <
|
|
270
|
+
{ .iter /_ _i, .src _s, .acc acc } .acc,
|
|
271
|
+
{ .iter < /0, /1 > _i, .src /_ _s, .acc acc } .acc,
|
|
272
|
+
<
|
|
273
|
+
{ .iter /0 iter, .src < /0 |0, /1 |1 > src, .acc acc } {
|
|
274
|
+
.iter iter,
|
|
275
|
+
.src lsb_shl1 src,
|
|
276
|
+
.acc acc
|
|
277
|
+
},
|
|
278
|
+
{ .iter /1 iter, .src < /0 |0, /1 |1 > src, .acc acc } {
|
|
279
|
+
.iter iter,
|
|
280
|
+
.src lsb_shl1 src,
|
|
281
|
+
{ .acc x, .src y } lsb_add acc
|
|
282
|
+
}
|
|
283
|
+
> _lsb_mul_small
|
|
284
|
+
> ?bits[];
|
|
285
|
+
|
|
286
|
+
-- O(1) prefix classifier separating zero, <= 16 bits, and > 16 bits.
|
|
287
|
+
_classify_bits = <
|
|
288
|
+
/_ {} |zero,
|
|
289
|
+
/0 _drop16 < /_ {} |short, /0 {} |long, /1 {} |long >,
|
|
290
|
+
/1 _drop16 < /_ {} |short, /0 {} |long, /1 {} |long >
|
|
291
|
+
>;
|
|
292
|
+
_drop16 = _drop4 _drop4 _drop4 _drop4;
|
|
293
|
+
_drop4 = _drop2 _drop2;
|
|
294
|
+
_drop2 = _drop1 _drop1;
|
|
295
|
+
_drop1 = < /_ |_, /0, /1 >;
|
|
296
|
+
|
|
297
|
+
-- [TMC] Streams floor(max(|x|, |y|) / 2) as a zero-tally bits[] online in O(1) memory.
|
|
298
|
+
half_max_tally = ?{ bits[] x, bits[] y } <
|
|
299
|
+
{ .x _le1_bit? _x, .y _le1_bit? _y } {} |_,
|
|
300
|
+
{ .x _drop2 x, .y _drop2 y } half_max_tally |0
|
|
301
|
+
> ?bits[];
|
|
302
|
+
|
|
303
|
+
-- Matches any bit-string of length <= 1 (base condition for half_max_tally).
|
|
304
|
+
_le1_bit? = ?bits[] { < /_, /0 /_, /1 /_ > _b, () x } .x ?bits[];
|
|
305
|
+
|
|
306
|
+
-- [TMC] Multi-output streaming split: streams raw `lo` bits forward online and splices `hi` at k==_.
|
|
307
|
+
_lsb_split_raw = ?{ bits[] x, bits[] k } <
|
|
308
|
+
{ .k /_ _k, .x x } { {} |_ lo, .x hi },
|
|
309
|
+
{ .k < /0, /1 > _k, .x /_ _x } { {} |_ lo, {} |_ hi },
|
|
310
|
+
{ .x /0 x, .k < /0, /1 > k } _lsb_split_raw { .lo |0 lo, .hi hi },
|
|
311
|
+
{ .x /1 x, .k < /0, /1 > k } _lsb_split_raw { .lo |1 lo, .hi hi }
|
|
312
|
+
> ?{ bits[] lo, bits[] hi };
|
|
313
|
+
|
|
314
|
+
-- Splits a trimmed LSB-first bits[] at tally `k` into trimmed { lo, hi } with zero reversals.
|
|
315
|
+
lsb_split = ?{ bits[] x, bits[] k }
|
|
316
|
+
_lsb_split_raw { .lo lsb_trim lo, .hi hi } ?{ bits[] lo, bits[] hi };
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
-- -----------------------------------------------------------------------------
|
|
320
|
+
-- 5. Fast Binary Long Division (MSB-First Shift-and-Subtract with TMC Quotient)
|
|
321
|
+
-- -----------------------------------------------------------------------------
|
|
322
|
+
|
|
323
|
+
-- Unsigned quotient and remainder on canonical MSB bits[] (fails when y == 0).
|
|
324
|
+
-- [TMC] `_nat_divmod` streams quotient bits `q` directly MSB-first (no `q_rev`, no `rev_bits` on `q`).
|
|
325
|
+
nat_divmod = ?{ bits[] x, bits[] y } {
|
|
326
|
+
.x trim_bits x,
|
|
327
|
+
.y trim_bits msb_to_lsb < /0 |0, /1 |1 > y_lsb
|
|
328
|
+
} {
|
|
329
|
+
.x x,
|
|
330
|
+
.y_lsb y_lsb,
|
|
331
|
+
{} |_ r_lsb
|
|
332
|
+
} _nat_divmod {
|
|
333
|
+
.q trim_bits q,
|
|
334
|
+
.r r
|
|
335
|
+
} ?{ bits[] q, bits[] r };
|
|
336
|
+
|
|
337
|
+
_nat_divmod = ?{ bits[] x, bits[] y_lsb, bits[] r_lsb } <
|
|
338
|
+
{ .x /_ _x, .y_lsb _y, .r_lsb r } {
|
|
339
|
+
{} |_ q,
|
|
340
|
+
.r lsb_to_msb r
|
|
341
|
+
},
|
|
342
|
+
<
|
|
343
|
+
{ .x /0 x_tail, .y_lsb y_lsb, .r_lsb r } {
|
|
344
|
+
.x_tail x_tail,
|
|
345
|
+
.y_lsb y_lsb,
|
|
346
|
+
.r _lsb_push_0 r1
|
|
347
|
+
},
|
|
348
|
+
{ .x /1 x_tail, .y_lsb y_lsb, .r_lsb r } {
|
|
349
|
+
.x_tail x_tail,
|
|
350
|
+
.y_lsb y_lsb,
|
|
351
|
+
.r _lsb_push_1 r1
|
|
352
|
+
}
|
|
353
|
+
> {
|
|
354
|
+
.x_tail x_tail,
|
|
355
|
+
.y_lsb y_lsb,
|
|
356
|
+
.r1 r1,
|
|
357
|
+
{ .r1 x, .y_lsb y } lsb_try_sub sub
|
|
358
|
+
} <
|
|
359
|
+
{
|
|
360
|
+
.x_tail x,
|
|
361
|
+
.y_lsb y_lsb,
|
|
362
|
+
.r1 _r1,
|
|
363
|
+
.sub .b /0 _b,
|
|
364
|
+
.sub .r rem_next
|
|
365
|
+
} {
|
|
366
|
+
.x x,
|
|
367
|
+
.y_lsb y_lsb,
|
|
368
|
+
.rem_next r_lsb
|
|
369
|
+
} _nat_divmod { .q |1 q, .r r },
|
|
370
|
+
{
|
|
371
|
+
.x_tail x,
|
|
372
|
+
.y_lsb y_lsb,
|
|
373
|
+
.r1 r1,
|
|
374
|
+
.sub .b /1 _b,
|
|
375
|
+
.sub .r _r
|
|
376
|
+
} {
|
|
377
|
+
.x x,
|
|
378
|
+
.y_lsb y_lsb,
|
|
379
|
+
.r1 r_lsb
|
|
380
|
+
} _nat_divmod { .q |0 q, .r r }
|
|
381
|
+
>
|
|
382
|
+
> ?{ bits[] q, bits[] r };
|
|
383
|
+
|
|
384
|
+
-- Pushes MSB bit '0' or '1' as the new LSB onto a trimmed LSB-first remainder.
|
|
385
|
+
_lsb_push_0 = ?bits[] < /_ |_, /0 |0 |0, /1 |1 |0 > ?bits[];
|
|
386
|
+
_lsb_push_1 = ?bits[] < /_ |_ |1, /0 |0 |1, /1 |1 |1 > ?bits[];
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
-- -----------------------------------------------------------------------------
|
|
390
|
+
-- 6. Signed Integer Operations
|
|
391
|
+
-- -----------------------------------------------------------------------------
|
|
392
|
+
|
|
393
|
+
abs = ?int[] int < /+ |+, /- |+ > ?int[];
|
|
394
|
+
|
|
395
|
+
negate = ?int[] int <
|
|
396
|
+
/+ < /_ |_|+, |- >,
|
|
397
|
+
/- |+
|
|
398
|
+
> ?int[];
|
|
399
|
+
|
|
400
|
+
-- Signed comparison returning order[] (lt | eq | gt).
|
|
401
|
+
cmp = ?{ int[] x, int[] y } { .x int x, .y int y } <
|
|
402
|
+
{ .x /+ x, .y /+ y } { .x x, .y y } cmp_msb,
|
|
403
|
+
{ .x /+ _x, .y /- _y } {} |gt,
|
|
404
|
+
{ .x /- _x, .y /+ _y } {} |lt,
|
|
405
|
+
{ .x /- x, .y /- y } { .x y, .y x } cmp_msb
|
|
406
|
+
> ?order[];
|
|
407
|
+
|
|
408
|
+
lt? = ?{ int[] x, int[] y } { cmp /lt _ord, () xy } .xy ?{ int[] x, int[] y };
|
|
409
|
+
le? = ?{ int[] x, int[] y } { cmp < /lt, /eq > _ord, () xy } .xy ?{ int[] x, int[] y };
|
|
410
|
+
eq? = ?{ int[] x, int[] y } { cmp /eq _ord, () xy } .xy ?{ int[] x, int[] y };
|
|
411
|
+
gt? = ?{ int[] x, int[] y } { cmp /gt _ord, () xy } .xy ?{ int[] x, int[] y };
|
|
412
|
+
ge? = ?{ int[] x, int[] y } { cmp < /gt, /eq > _ord, () xy } .xy ?{ int[] x, int[] y };
|
|
413
|
+
|
|
414
|
+
plus = ?{ int[] x, int[] y } { .x int x, .y int y } <
|
|
415
|
+
{ .x /+ x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_add lsb_to_msb |+ int,
|
|
416
|
+
{ .x /- x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_add lsb_to_msb |- int,
|
|
417
|
+
{ .x /+ x, .y /- y } { .x x, .y y } _sub_signed_msb,
|
|
418
|
+
{ .x /- x, .y /+ y } { .x y, .y x } _sub_signed_msb
|
|
419
|
+
> ?int[];
|
|
420
|
+
|
|
421
|
+
-- Subtracts two unsigned MSB bits[], tagging '+' if x >= y else '-' on |y - x|.
|
|
422
|
+
_sub_signed_msb = ?{ bits[] x, bits[] y } {
|
|
423
|
+
.x x,
|
|
424
|
+
.y y,
|
|
425
|
+
{ .x x, .y y } cmp_msb ord
|
|
426
|
+
} <
|
|
427
|
+
{ .ord /eq _ord, .x _x, .y _y } 0,
|
|
428
|
+
{ .ord /gt _ord, .x x, .y y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_sub lsb_to_msb |+,
|
|
429
|
+
{ .ord /lt _ord, .x x, .y y } { .y msb_to_lsb x, .x msb_to_lsb y } lsb_sub lsb_to_msb |-
|
|
430
|
+
> ?int[];
|
|
431
|
+
|
|
432
|
+
minus = ?{ int[] x, int[] y } { .x x, .y negate y } plus ?int[];
|
|
433
|
+
|
|
434
|
+
inc = ?int[] { () x, 1 y } plus ?int[];
|
|
435
|
+
dec = ?int[] { () x, 1 y } minus ?int[];
|
|
436
|
+
|
|
437
|
+
times = ?{ int[] x, int[] y } { .x int x, .y int y } <
|
|
438
|
+
{ .x /+ x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |+,
|
|
439
|
+
{ .x /- x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |+,
|
|
440
|
+
{ .x /+ x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |- int,
|
|
441
|
+
{ .x /- x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |- int
|
|
442
|
+
> ?int[];
|
|
443
|
+
|
|
444
|
+
-- Truncating signed division & remainder matching JS BigInt (/ and %):
|
|
445
|
+
-- x = q * y + r, sign(q) = sign(x) * sign(y), sign(r) = sign(x), y != 0.
|
|
446
|
+
divmod = ?{ int[] x, int[] y } { .x int x, .y int y } <
|
|
447
|
+
{ .x /+ x, .y /+ y } { .x x, .y y } nat_divmod { .q |+ q, .r |+ r },
|
|
448
|
+
{ .x /+ x, .y /- y } { .x x, .y y } nat_divmod { .q |- int q, .r |+ r },
|
|
449
|
+
{ .x /- x, .y /+ y } { .x x, .y y } nat_divmod { .q |- int q, .r |- int r },
|
|
450
|
+
{ .x /- x, .y /- y } { .x x, .y y } nat_divmod { .q |+ q, .r |- int r }
|
|
451
|
+
> ?{ int[] q, int[] r };
|
|
452
|
+
|
|
453
|
+
div = ?{ int[] x, int[] y } divmod .q ?int[];
|
|
454
|
+
mod = ?{ int[] x, int[] y } divmod .r ?int[];
|
|
455
|
+
|
|
456
|
+
gcd = ?{ int[] x, int[] y } { .x abs /+ a, .y abs /+ b } _gcd ?int[];
|
|
457
|
+
|
|
458
|
+
-- Non-negative greatest common divisor via Euclidean algorithm.
|
|
459
|
+
_gcd = ?{ bits[] a, bits[] b } <
|
|
460
|
+
{ .a a, .b /_ |_ _b } .a |+,
|
|
461
|
+
{ .a a, .b /1 b } {
|
|
462
|
+
.b |1 a,
|
|
463
|
+
{ .a x, .b |1 y } nat_divmod .r b
|
|
464
|
+
} _gcd
|
|
465
|
+
> ?int[];
|
|
466
|
+
|
|
467
|
+
lcm = ?{ int[] x, int[] y } { .x abs x, .y abs y } <
|
|
468
|
+
{ .x /+ /_ |_ _x, .y _y } 0,
|
|
469
|
+
{ .x /+ /1 _x, .y /+ /_ |_ _y } 0,
|
|
470
|
+
{ .x /+ /1 x, .y /+ /1 y } {
|
|
471
|
+
{
|
|
472
|
+
.x |1 |+ x,
|
|
473
|
+
{ .x |1 |+ x, .y |1 |+ y } gcd y
|
|
474
|
+
} div x,
|
|
475
|
+
.y |1 |+ y
|
|
476
|
+
} times
|
|
477
|
+
> ?int[];
|
package/ktvm/index.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public API for @fraczak/k's kTVM (Transducer Virtual Machine) backend.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
KTVM_FORMAT,
|
|
7
|
+
KTVM_VERSION,
|
|
8
|
+
KTVMSyntaxError,
|
|
9
|
+
KTVMValidationError,
|
|
10
|
+
KTVMSchemaError,
|
|
11
|
+
KTVMQuotaError,
|
|
12
|
+
S,
|
|
13
|
+
createModule,
|
|
14
|
+
cloneAst,
|
|
15
|
+
moduleToJSON,
|
|
16
|
+
moduleFromJSON,
|
|
17
|
+
packInt32Base64,
|
|
18
|
+
unpackInt32Base64
|
|
19
|
+
} from "./ast.mjs";
|
|
20
|
+
|
|
21
|
+
export { parseModule, tokenize } from "./parser.mjs";
|
|
22
|
+
export { printModule, printSchemaExpr } from "./printer.mjs";
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
compareLabels,
|
|
26
|
+
isClosedSchemaKind,
|
|
27
|
+
isClosedPlist,
|
|
28
|
+
substituteSchemaExpr,
|
|
29
|
+
compileSchemaExprToPlist,
|
|
30
|
+
canonicalizePlistOrder,
|
|
31
|
+
subPlist,
|
|
32
|
+
mooreMinimize,
|
|
33
|
+
mooreKey,
|
|
34
|
+
schemasEquivalent,
|
|
35
|
+
isUnitSchema,
|
|
36
|
+
unionTagInfo,
|
|
37
|
+
unionOrdinalInfo,
|
|
38
|
+
productFields,
|
|
39
|
+
productFieldInfo,
|
|
40
|
+
plistToSchemaAst,
|
|
41
|
+
unifyContractWithConcrete,
|
|
42
|
+
intersectSchemas
|
|
43
|
+
} from "./schema.mjs";
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
CHAN_STATUS,
|
|
47
|
+
QuotaTracker,
|
|
48
|
+
BitDeque,
|
|
49
|
+
BitStack
|
|
50
|
+
} from "./bit-deque.mjs";
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
DPDA_RESULT,
|
|
54
|
+
encodePayloadToDeque,
|
|
55
|
+
decodeDequeToValue,
|
|
56
|
+
writeBytesToDeque,
|
|
57
|
+
dequeToPaddedBytes,
|
|
58
|
+
splitEnvelopedWire,
|
|
59
|
+
frameEnvelopedWire,
|
|
60
|
+
encodeValueToEnvelopedWire,
|
|
61
|
+
decodeEnvelopedWireToValue,
|
|
62
|
+
createDpdaSkip,
|
|
63
|
+
stepDpdaSkip,
|
|
64
|
+
createDpdaCopy,
|
|
65
|
+
stepDpdaCopy,
|
|
66
|
+
createDpdaCoerce,
|
|
67
|
+
stepDpdaCoerce
|
|
68
|
+
} from "./wire.mjs";
|
|
69
|
+
|
|
70
|
+
export {
|
|
71
|
+
validateModule,
|
|
72
|
+
verifyTableBisimulation
|
|
73
|
+
} from "./validator.mjs";
|
|
74
|
+
|
|
75
|
+
export {
|
|
76
|
+
specializeKTVM,
|
|
77
|
+
clearSpecializationCache
|
|
78
|
+
} from "./specialize.mjs";
|
|
79
|
+
|
|
80
|
+
export {
|
|
81
|
+
KTVMRuntime,
|
|
82
|
+
executeKTVM,
|
|
83
|
+
executeKTVMWire
|
|
84
|
+
} from "./vm.mjs";
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
lowerKIRToKTVMP,
|
|
88
|
+
compileKIRToKTVM,
|
|
89
|
+
compileObjectToKTVM,
|
|
90
|
+
executeObjectWithKTVM
|
|
91
|
+
} from "./lower-kir.mjs";
|
|
92
|
+
|
|
93
|
+
export {
|
|
94
|
+
DEFAULT_L1_BUDGET_BYTES,
|
|
95
|
+
computeMWTR,
|
|
96
|
+
budgetClassForBytes,
|
|
97
|
+
allocateTableBudgets,
|
|
98
|
+
simplifyProcs,
|
|
99
|
+
fuseKernels,
|
|
100
|
+
synthesizeTables,
|
|
101
|
+
optimizeKTVM
|
|
102
|
+
} from "./optimize/index.mjs";
|
|
103
|
+
|
|
104
|
+
export {
|
|
105
|
+
compileKTVMToWat,
|
|
106
|
+
assembleWatToWasm,
|
|
107
|
+
compileKTVMToWasm,
|
|
108
|
+
instantiateKTVMWasm,
|
|
109
|
+
compileObjectToKTVMWasm
|
|
110
|
+
} from "./wasm/index.mjs";
|
|
111
|
+
|
|
112
|
+
export {
|
|
113
|
+
isClangAvailable,
|
|
114
|
+
compileKTVMToC,
|
|
115
|
+
compileCToExecutable,
|
|
116
|
+
compileKTVMToExecutable,
|
|
117
|
+
compileObjectToKTVMExecutable,
|
|
118
|
+
executeKTVMExecutable
|
|
119
|
+
} from "./llvm/index.mjs";
|