@fraczak/k 9.1.0 → 9.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/backends/llvm/README.md +4 -0
- package/backends/llvm/runtime/krt.c +63 -291
- package/backends/llvm/src/executable.mjs +33 -19
- package/backends/llvm/tests/compaction-driver.c +33 -0
- package/backends/llvm/tests/integration.sh +18 -0
- package/codecs/runtime/codec-sdk.mjs +1 -0
- package/codecs/runtime/prefix-codec.mjs +3 -3
- package/package.json +1 -1
- package/tests/test-codecs.mjs +8 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,13 @@ All notable changes to this project will be documented here.
|
|
|
5
5
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
6
6
|
This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [9.1.1] — 2026-11-29 — LLVM Runtime and Wire Fixes
|
|
9
|
+
|
|
10
|
+
- Keep retained values valid when the LLVM kVM tail-call compaction arena grows.
|
|
11
|
+
- Report native kVM failures on stderr and give codec users a clear error when
|
|
12
|
+
stdin contains no k-wire data.
|
|
13
|
+
- Fix k-wire bit reads at offsets of 2³¹ bits and above.
|
|
14
|
+
|
|
8
15
|
## [9.1.0] — 2026-11-29 — Runtime Improvements and Published kTVM Paper
|
|
9
16
|
|
|
10
17
|
- Reuse LLVM arena blocks after same-block rollback in kVM, reducing repeated allocation.
|
package/backends/llvm/README.md
CHANGED
|
@@ -97,6 +97,10 @@ k_result k_main(k_rt *rt, k_value *input);
|
|
|
97
97
|
Unsupported operations must produce diagnostics rather than silently change
|
|
98
98
|
semantics. Backend coverage and artifact formats remain experimental.
|
|
99
99
|
|
|
100
|
+
Native executables report failures on stderr. Use `set -o pipefail` in Bash
|
|
101
|
+
to preserve a failing pipeline status when the executable is followed by a
|
|
102
|
+
codec.
|
|
103
|
+
|
|
100
104
|
## Verification and profiling
|
|
101
105
|
|
|
102
106
|
From the repository root:
|
|
@@ -88,7 +88,10 @@ static void *rt_alloc(k_rt *rt, size_t size) {
|
|
|
88
88
|
if (block == NULL) {
|
|
89
89
|
size_t capacity = size > K_ARENA_BLOCK_SIZE ? size : K_ARENA_BLOCK_SIZE;
|
|
90
90
|
block = malloc(sizeof(k_arena_block) + capacity);
|
|
91
|
-
if (block == NULL)
|
|
91
|
+
if (block == NULL) {
|
|
92
|
+
fprintf(stderr, "kVM: could not allocate a %zu-byte arena block\n", capacity);
|
|
93
|
+
abort();
|
|
94
|
+
}
|
|
92
95
|
block->next = rt->blocks;
|
|
93
96
|
block->used = 0;
|
|
94
97
|
block->capacity = capacity;
|
|
@@ -222,45 +225,13 @@ void k_rt_rewind(k_rt *rt, k_rt_checkpoint mark) {
|
|
|
222
225
|
rt->has_reusable_blocks = has_reusable;
|
|
223
226
|
}
|
|
224
227
|
|
|
225
|
-
typedef struct {
|
|
226
|
-
size_t *offsets;
|
|
227
|
-
size_t count;
|
|
228
|
-
size_t capacity;
|
|
229
|
-
} k_fixup_list;
|
|
230
|
-
|
|
231
|
-
static void fixup_add(k_fixup_list *list, size_t offset) {
|
|
232
|
-
if (list->count == list->capacity) {
|
|
233
|
-
size_t new_cap = list->capacity == 0 ? 1024 : list->capacity * 2;
|
|
234
|
-
size_t *new_offsets = (size_t *)realloc(list->offsets, new_cap * sizeof(size_t));
|
|
235
|
-
if (new_offsets == NULL) abort();
|
|
236
|
-
list->offsets = new_offsets;
|
|
237
|
-
list->capacity = new_cap;
|
|
238
|
-
}
|
|
239
|
-
list->offsets[list->count++] = offset;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
228
|
static int is_after_mark(k_rt *rt, const void *ptr, k_rt_checkpoint mark) {
|
|
243
229
|
if (rt == NULL || ptr == NULL) return 0;
|
|
244
|
-
const
|
|
245
|
-
k_arena_block *mb = (k_arena_block *)mark.block;
|
|
246
|
-
if (mb == NULL) {
|
|
247
|
-
for (k_arena_block *b = rt->blocks; b != NULL; b = b->next) {
|
|
248
|
-
if (p >= b->data && p < b->data + b->capacity) {
|
|
249
|
-
return 1;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return 0;
|
|
253
|
-
}
|
|
254
|
-
if (rt->blocks == mb) {
|
|
255
|
-
return (p >= mb->data + mark.used && p < mb->data + mb->capacity);
|
|
256
|
-
}
|
|
230
|
+
const uintptr_t p = (uintptr_t)ptr;
|
|
257
231
|
for (k_arena_block *b = rt->blocks; b != NULL; b = b->next) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
if (p >= b->data && p < b->data + b->capacity) {
|
|
262
|
-
return 1;
|
|
263
|
-
}
|
|
232
|
+
const size_t start = b == mark.block ? mark.used : 0;
|
|
233
|
+
if (p >= (uintptr_t)(b->data + start) && p < (uintptr_t)(b->data + b->capacity)) return 1;
|
|
234
|
+
if (b == mark.block) break;
|
|
264
235
|
}
|
|
265
236
|
return 0;
|
|
266
237
|
}
|
|
@@ -290,235 +261,65 @@ static size_t bytes_since_mark(k_rt *rt, k_rt_checkpoint mark) {
|
|
|
290
261
|
return total;
|
|
291
262
|
}
|
|
292
263
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
unsigned char *old_buf = *staging_ptr;
|
|
299
|
-
unsigned char *new_buf = (unsigned char *)realloc(old_buf, new_cap);
|
|
300
|
-
if (new_buf == NULL) abort();
|
|
301
|
-
if (new_buf != old_buf) {
|
|
302
|
-
ptrdiff_t realloc_diff = new_buf - old_buf;
|
|
303
|
-
for (size_t i = 0; i < fixups->count; i++) {
|
|
304
|
-
size_t off = fixups->offsets[i];
|
|
305
|
-
unsigned char **p = (unsigned char **)(new_buf + off);
|
|
306
|
-
*p += realloc_diff;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
*staging_ptr = new_buf;
|
|
310
|
-
*capacity = new_cap;
|
|
311
|
-
}
|
|
312
|
-
void *p = *staging_ptr + *used;
|
|
313
|
-
*used += size;
|
|
314
|
-
return p;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
static k_value *copy_value(k_rt *rt, k_value *root, unsigned char **staging_ptr, size_t *staging_used, size_t *staging_cap, k_fixup_list *fixups, k_rt_checkpoint mark) {
|
|
318
|
-
if (root == NULL || !is_after_mark(rt, root, mark)) {
|
|
319
|
-
return root;
|
|
320
|
-
}
|
|
321
|
-
if ((int)root->kind == K_VALUE_FORWARDED) {
|
|
322
|
-
return root->as.variant.payload;
|
|
323
|
-
}
|
|
264
|
+
/* The staging arena grows by adding blocks. Forwarding pointers and
|
|
265
|
+
* addresses into already copied values remain valid as it grows. */
|
|
266
|
+
static k_value *copy_value(k_rt *rt, k_value *root, k_rt *staging, k_rt_checkpoint mark) {
|
|
267
|
+
if (!is_after_mark(rt, root, mark)) return root;
|
|
268
|
+
if (root->kind == K_VALUE_FORWARDED) return root->as.variant.payload;
|
|
324
269
|
|
|
325
270
|
if (root->kind == K_VALUE_VARIANT) {
|
|
326
|
-
k_value *
|
|
327
|
-
k_value
|
|
271
|
+
k_value *result = NULL;
|
|
272
|
+
k_value **link = &result;
|
|
328
273
|
k_value *curr = root;
|
|
329
|
-
|
|
330
|
-
while (curr
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
if ((int)curr->kind == K_VALUE_FORWARDED) {
|
|
342
|
-
k_value *staged_target = curr->as.variant.payload;
|
|
343
|
-
if (prev_staged != NULL) {
|
|
344
|
-
prev_staged->as.variant.payload = staged_target;
|
|
345
|
-
fixup_add(fixups, (unsigned char *)&prev_staged->as.variant.payload - *staging_ptr);
|
|
346
|
-
}
|
|
347
|
-
if (first_staged == NULL) {
|
|
348
|
-
first_staged = staged_target;
|
|
349
|
-
}
|
|
350
|
-
break;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
if (curr->as.variant.tag != NULL) {
|
|
354
|
-
if (bytes_equal(curr->as.variant.tag, curr->as.variant.tag_length, "Float64", 7)) {
|
|
355
|
-
k_value *staged_v = (k_value *)staging_alloc(staging_ptr, staging_used, staging_cap, sizeof(k_value), fixups);
|
|
356
|
-
staged_v->kind = K_VALUE_VARIANT;
|
|
357
|
-
staged_v->rt = rt;
|
|
358
|
-
staged_v->as.variant.tag = curr->as.variant.tag;
|
|
359
|
-
staged_v->as.variant.tag_length = curr->as.variant.tag_length;
|
|
360
|
-
staged_v->as.variant.payload = NULL;
|
|
361
|
-
|
|
362
|
-
if (is_after_mark(rt, curr->as.variant.tag, mark)) {
|
|
363
|
-
char *staged_tag = (char *)staging_alloc(staging_ptr, staging_used, staging_cap, curr->as.variant.tag_length + 1, fixups);
|
|
364
|
-
memcpy(staged_tag, curr->as.variant.tag, curr->as.variant.tag_length);
|
|
365
|
-
staged_tag[curr->as.variant.tag_length] = 0;
|
|
366
|
-
staged_v->as.variant.tag = staged_tag;
|
|
367
|
-
fixup_add(fixups, (unsigned char *)&staged_v->as.variant.tag - *staging_ptr);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
if (curr->as.variant.payload != NULL && is_after_mark(rt, curr->as.variant.payload, mark)) {
|
|
371
|
-
void *raw = staging_alloc(staging_ptr, staging_used, staging_cap, sizeof(double), fixups);
|
|
372
|
-
memcpy(raw, curr->as.variant.payload, sizeof(double));
|
|
373
|
-
staged_v->as.variant.payload = (k_value *)raw;
|
|
374
|
-
fixup_add(fixups, (unsigned char *)&staged_v->as.variant.payload - *staging_ptr);
|
|
375
|
-
} else {
|
|
376
|
-
staged_v->as.variant.payload = curr->as.variant.payload;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
if (prev_staged != NULL) {
|
|
380
|
-
prev_staged->as.variant.payload = staged_v;
|
|
381
|
-
fixup_add(fixups, (unsigned char *)&prev_staged->as.variant.payload - *staging_ptr);
|
|
382
|
-
}
|
|
383
|
-
if (first_staged == NULL) {
|
|
384
|
-
first_staged = staged_v;
|
|
385
|
-
}
|
|
386
|
-
curr->kind = (k_value_kind)K_VALUE_FORWARDED;
|
|
387
|
-
curr->as.variant.payload = staged_v;
|
|
388
|
-
break;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
if (bytes_equal(curr->as.variant.tag, curr->as.variant.tag_length, "Int32", 5)) {
|
|
392
|
-
k_value *staged_v = (k_value *)staging_alloc(staging_ptr, staging_used, staging_cap, sizeof(k_value), fixups);
|
|
393
|
-
staged_v->kind = K_VALUE_VARIANT;
|
|
394
|
-
staged_v->rt = rt;
|
|
395
|
-
staged_v->as.variant.tag = curr->as.variant.tag;
|
|
396
|
-
staged_v->as.variant.tag_length = curr->as.variant.tag_length;
|
|
397
|
-
staged_v->as.variant.payload = NULL;
|
|
398
|
-
|
|
399
|
-
if (is_after_mark(rt, curr->as.variant.tag, mark)) {
|
|
400
|
-
char *staged_tag = (char *)staging_alloc(staging_ptr, staging_used, staging_cap, curr->as.variant.tag_length + 1, fixups);
|
|
401
|
-
memcpy(staged_tag, curr->as.variant.tag, curr->as.variant.tag_length);
|
|
402
|
-
staged_tag[curr->as.variant.tag_length] = 0;
|
|
403
|
-
staged_v->as.variant.tag = staged_tag;
|
|
404
|
-
fixup_add(fixups, (unsigned char *)&staged_v->as.variant.tag - *staging_ptr);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
if (curr->as.variant.payload != NULL && is_after_mark(rt, curr->as.variant.payload, mark)) {
|
|
408
|
-
void *raw = staging_alloc(staging_ptr, staging_used, staging_cap, sizeof(int32_t), fixups);
|
|
409
|
-
memcpy(raw, curr->as.variant.payload, sizeof(int32_t));
|
|
410
|
-
staged_v->as.variant.payload = (k_value *)raw;
|
|
411
|
-
fixup_add(fixups, (unsigned char *)&staged_v->as.variant.payload - *staging_ptr);
|
|
412
|
-
} else {
|
|
413
|
-
staged_v->as.variant.payload = curr->as.variant.payload;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
if (prev_staged != NULL) {
|
|
417
|
-
prev_staged->as.variant.payload = staged_v;
|
|
418
|
-
fixup_add(fixups, (unsigned char *)&prev_staged->as.variant.payload - *staging_ptr);
|
|
419
|
-
}
|
|
420
|
-
if (first_staged == NULL) {
|
|
421
|
-
first_staged = staged_v;
|
|
422
|
-
}
|
|
423
|
-
curr->kind = (k_value_kind)K_VALUE_FORWARDED;
|
|
424
|
-
curr->as.variant.payload = staged_v;
|
|
425
|
-
break;
|
|
426
|
-
}
|
|
274
|
+
// Copy consecutive variants without using a C stack frame for every tag.
|
|
275
|
+
while (is_after_mark(rt, curr, mark) && curr->kind == K_VALUE_VARIANT) {
|
|
276
|
+
k_value *copy = rt_alloc(staging, sizeof(*copy));
|
|
277
|
+
*copy = *curr;
|
|
278
|
+
const char *tag = curr->as.variant.tag;
|
|
279
|
+
size_t length = curr->as.variant.tag_length;
|
|
280
|
+
if (is_after_mark(rt, tag, mark)) copy->as.variant.tag = rt_memdup_text(staging, tag, length);
|
|
281
|
+
|
|
282
|
+
size_t raw_size = 0;
|
|
283
|
+
if (tag != NULL) {
|
|
284
|
+
if (bytes_equal(tag, length, "Float64", 7)) raw_size = sizeof(double);
|
|
285
|
+
else if (bytes_equal(tag, length, "Int32", 5)) raw_size = sizeof(int32_t);
|
|
427
286
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
287
|
+
k_value *payload = curr->as.variant.payload;
|
|
288
|
+
*link = copy;
|
|
289
|
+
curr->kind = K_VALUE_FORWARDED;
|
|
290
|
+
curr->as.variant.payload = copy;
|
|
291
|
+
if (raw_size) {
|
|
292
|
+
if (is_after_mark(rt, payload, mark)) {
|
|
293
|
+
void *raw = rt_alloc(staging, raw_size);
|
|
294
|
+
memcpy(raw, payload, raw_size);
|
|
295
|
+
copy->as.variant.payload = raw;
|
|
436
296
|
}
|
|
437
|
-
|
|
438
|
-
first_staged = staged_child;
|
|
439
|
-
}
|
|
440
|
-
break;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
k_value *staged_v = (k_value *)staging_alloc(staging_ptr, staging_used, staging_cap, sizeof(k_value), fixups);
|
|
444
|
-
staged_v->kind = K_VALUE_VARIANT;
|
|
445
|
-
staged_v->rt = rt;
|
|
446
|
-
staged_v->as.variant.tag = curr->as.variant.tag;
|
|
447
|
-
staged_v->as.variant.tag_length = curr->as.variant.tag_length;
|
|
448
|
-
staged_v->as.variant.payload = NULL;
|
|
449
|
-
|
|
450
|
-
if (curr->as.variant.tag != NULL && is_after_mark(rt, curr->as.variant.tag, mark)) {
|
|
451
|
-
char *staged_tag = (char *)staging_alloc(staging_ptr, staging_used, staging_cap, curr->as.variant.tag_length + 1, fixups);
|
|
452
|
-
memcpy(staged_tag, curr->as.variant.tag, curr->as.variant.tag_length);
|
|
453
|
-
staged_tag[curr->as.variant.tag_length] = 0;
|
|
454
|
-
staged_v->as.variant.tag = staged_tag;
|
|
455
|
-
fixup_add(fixups, (unsigned char *)&staged_v->as.variant.tag - *staging_ptr);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
if (prev_staged != NULL) {
|
|
459
|
-
prev_staged->as.variant.payload = staged_v;
|
|
460
|
-
fixup_add(fixups, (unsigned char *)&prev_staged->as.variant.payload - *staging_ptr);
|
|
461
|
-
}
|
|
462
|
-
if (first_staged == NULL) {
|
|
463
|
-
first_staged = staged_v;
|
|
297
|
+
return result;
|
|
464
298
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
curr->kind = (k_value_kind)K_VALUE_FORWARDED;
|
|
468
|
-
curr->as.variant.payload = staged_v;
|
|
469
|
-
|
|
470
|
-
prev_staged = staged_v;
|
|
471
|
-
curr = next;
|
|
299
|
+
curr = payload;
|
|
300
|
+
link = ©->as.variant.payload;
|
|
472
301
|
}
|
|
473
|
-
|
|
474
|
-
return
|
|
302
|
+
*link = copy_value(rt, curr, staging, mark);
|
|
303
|
+
return result;
|
|
475
304
|
}
|
|
476
305
|
|
|
477
|
-
size_t
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
fixup_add(fixups, (unsigned char *)&staged_p->as.product.fields - *staging_ptr);
|
|
491
|
-
|
|
492
|
-
k_field *old_fields = root->as.product.fields;
|
|
493
|
-
size_t count = root->as.product.count;
|
|
494
|
-
|
|
495
|
-
root->kind = (k_value_kind)K_VALUE_FORWARDED;
|
|
496
|
-
root->as.variant.payload = staged_p;
|
|
497
|
-
|
|
498
|
-
for (size_t i = 0; i < count; i++) {
|
|
499
|
-
staged_fields[i].label_length = old_fields[i].label_length;
|
|
500
|
-
staged_fields[i].label = old_fields[i].label;
|
|
501
|
-
if (old_fields[i].label != NULL && is_after_mark(rt, old_fields[i].label, mark)) {
|
|
502
|
-
char *staged_label = (char *)staging_alloc(staging_ptr, staging_used, staging_cap, old_fields[i].label_length + 1, fixups);
|
|
503
|
-
memcpy(staged_label, old_fields[i].label, old_fields[i].label_length);
|
|
504
|
-
staged_label[old_fields[i].label_length] = 0;
|
|
505
|
-
staged_fields[i].label = staged_label;
|
|
506
|
-
fixup_add(fixups, (unsigned char *)&staged_fields[i].label - *staging_ptr);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
k_value *child = copy_value(rt, old_fields[i].value, staging_ptr, staging_used, staging_cap, fixups, mark);
|
|
510
|
-
staged_fields[i].value = child;
|
|
511
|
-
if (child != NULL && (unsigned char *)child >= *staging_ptr && (unsigned char *)child < *staging_ptr + *staging_used) {
|
|
512
|
-
fixup_add(fixups, (unsigned char *)&staged_fields[i].value - *staging_ptr);
|
|
513
|
-
}
|
|
306
|
+
const size_t count = root->as.product.count;
|
|
307
|
+
const k_field *fields = root->as.product.fields;
|
|
308
|
+
k_value *copy = alloc_product_value(staging, count);
|
|
309
|
+
copy->rt = rt;
|
|
310
|
+
copy->kind = root->kind;
|
|
311
|
+
copy->as.product.count = count;
|
|
312
|
+
root->kind = K_VALUE_FORWARDED;
|
|
313
|
+
root->as.variant.payload = copy;
|
|
314
|
+
for (size_t i = 0; i < count; i++) {
|
|
315
|
+
k_field *field = ©->as.product.fields[i];
|
|
316
|
+
*field = fields[i];
|
|
317
|
+
if (is_after_mark(rt, field->label, mark)) {
|
|
318
|
+
field->label = rt_memdup_text(staging, field->label, field->label_length);
|
|
514
319
|
}
|
|
515
|
-
|
|
516
|
-
staged_p->as.product.fields = NULL;
|
|
517
|
-
root->kind = (k_value_kind)K_VALUE_FORWARDED;
|
|
518
|
-
root->as.variant.payload = staged_p;
|
|
320
|
+
field->value = copy_value(rt, field->value, staging, mark);
|
|
519
321
|
}
|
|
520
|
-
|
|
521
|
-
return staged_p;
|
|
322
|
+
return copy;
|
|
522
323
|
}
|
|
523
324
|
|
|
524
325
|
static void rt_rewind_and_free(k_rt *rt, k_rt_checkpoint mark) {
|
|
@@ -567,46 +368,17 @@ k_value *k_rt_compact(k_rt *rt, k_value *root, k_rt_tail_mark *tm) {
|
|
|
567
368
|
return root;
|
|
568
369
|
}
|
|
569
370
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (staging == NULL) abort();
|
|
573
|
-
size_t staging_used = 0;
|
|
574
|
-
|
|
575
|
-
k_fixup_list fixups = {NULL, 0, 0};
|
|
576
|
-
|
|
577
|
-
k_value *staged_root = copy_value(rt, root, &staging, &staging_used, &staging_cap, &fixups, tm->base_mark);
|
|
371
|
+
k_rt staging = {0};
|
|
372
|
+
k_value *new_root = copy_value(rt, root, &staging, tm->base_mark);
|
|
578
373
|
|
|
579
374
|
rt_rewind_and_free(rt, tm->base_mark);
|
|
580
375
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
void *arena_dest = rt_alloc(rt, staging_used);
|
|
589
|
-
if (arena_dest == NULL) abort();
|
|
590
|
-
|
|
591
|
-
ptrdiff_t diff = (unsigned char *)arena_dest - staging;
|
|
592
|
-
|
|
593
|
-
for (size_t i = 0; i < fixups.count; i++) {
|
|
594
|
-
size_t off = fixups.offsets[i];
|
|
595
|
-
unsigned char **p = (unsigned char **)(staging + off);
|
|
596
|
-
*p += diff;
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
memcpy(arena_dest, staging, staging_used);
|
|
600
|
-
|
|
601
|
-
k_value *new_root = (staged_root != NULL && (unsigned char *)staged_root >= staging && (unsigned char *)staged_root < staging + staging_used)
|
|
602
|
-
? (k_value *)((unsigned char *)staged_root + diff)
|
|
603
|
-
: staged_root;
|
|
604
|
-
|
|
605
|
-
free(staging);
|
|
606
|
-
if (fixups.offsets != NULL) free(fixups.offsets);
|
|
607
|
-
|
|
376
|
+
// Keep the copied values at their existing addresses after adopting the blocks.
|
|
377
|
+
k_arena_block **old_head = &staging.blocks;
|
|
378
|
+
while (*old_head != NULL) old_head = &(*old_head)->next;
|
|
379
|
+
*old_head = rt->blocks;
|
|
380
|
+
rt->blocks = staging.blocks;
|
|
608
381
|
tm->last_mark = k_rt_mark(rt);
|
|
609
|
-
|
|
610
382
|
return new_root;
|
|
611
383
|
}
|
|
612
384
|
|
|
@@ -195,6 +195,18 @@ export function compileAndRunLLVM(llvm, { input, expected = null, tmpPrefix = "k
|
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
+
const wireDiagnostics = [
|
|
199
|
+
"static int wire_error(int code, const char *message) {",
|
|
200
|
+
" fprintf(stderr, \"kVM: %s\\n\", message);",
|
|
201
|
+
" return code;",
|
|
202
|
+
"}",
|
|
203
|
+
"static int evaluation_error(k_result result) {",
|
|
204
|
+
" fprintf(stderr, \"kVM: evaluation failed before producing a k-wire result (runtime status %d)\\n\", result.status);",
|
|
205
|
+
" return 3;",
|
|
206
|
+
"}",
|
|
207
|
+
""
|
|
208
|
+
].join("\n");
|
|
209
|
+
|
|
198
210
|
export function stdioDriverSource({ inputPattern = null, outputPattern = null } = {}) {
|
|
199
211
|
if (inputPattern == null || outputPattern == null) {
|
|
200
212
|
return [
|
|
@@ -202,14 +214,15 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
202
214
|
"",
|
|
203
215
|
"extern k_result k_main(k_rt *rt, k_value *input);",
|
|
204
216
|
"",
|
|
217
|
+
wireDiagnostics,
|
|
205
218
|
"int main(void) {",
|
|
206
219
|
" k_rt *rt = k_rt_new();",
|
|
207
|
-
" if (rt == 0) return 1;",
|
|
220
|
+
" if (rt == 0) return wire_error(1, \"could not allocate runtime\");",
|
|
208
221
|
" k_value *input = k_read_wire(stdin, rt);",
|
|
209
|
-
" if (input == 0) return 2;",
|
|
222
|
+
" if (input == 0) return wire_error(2, \"could not decode k-wire input\");",
|
|
210
223
|
" k_result result = k_main(rt, input);",
|
|
211
|
-
" if (result.status != K_STATUS_OK) return
|
|
212
|
-
" if (!k_write_wire(stdout, result.value)) return 4;",
|
|
224
|
+
" if (result.status != K_STATUS_OK) return evaluation_error(result);",
|
|
225
|
+
" if (!k_write_wire(stdout, result.value)) return wire_error(4, \"could not write k-wire output\");",
|
|
213
226
|
" k_rt_free(rt);",
|
|
214
227
|
" return 0;",
|
|
215
228
|
"}",
|
|
@@ -231,6 +244,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
231
244
|
"",
|
|
232
245
|
"extern k_result k_main(k_rt *rt, k_value *input);",
|
|
233
246
|
"",
|
|
247
|
+
wireDiagnostics,
|
|
234
248
|
"static int read_exact(FILE *in, unsigned char *buffer, size_t length) {",
|
|
235
249
|
" size_t offset = 0;",
|
|
236
250
|
" while (offset < length) {",
|
|
@@ -283,7 +297,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
283
297
|
"",
|
|
284
298
|
"static int run_value(k_rt *rt, k_wire_prefix *output_prefix, k_value *input_value) {",
|
|
285
299
|
" k_result result = k_main(rt, input_value);",
|
|
286
|
-
" if (result.status != K_STATUS_OK) return
|
|
300
|
+
" if (result.status != K_STATUS_OK) return evaluation_error(result);",
|
|
287
301
|
" return write_frame(output_prefix, &compiled_output_pattern, result.value) ? 0 : 4;",
|
|
288
302
|
"}",
|
|
289
303
|
"",
|
|
@@ -306,14 +320,14 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
306
320
|
"",
|
|
307
321
|
"static int run_server(void) {",
|
|
308
322
|
" k_rt *rt = k_rt_new();",
|
|
309
|
-
" if (rt == 0) return 1;",
|
|
323
|
+
" if (rt == 0) return wire_error(1, \"could not allocate runtime\");",
|
|
310
324
|
" k_wire_prefix input_prefix = k_wire_prefix_for_pattern(&compiled_input_pattern);",
|
|
311
325
|
" k_wire_prefix output_prefix = k_wire_prefix_for_pattern(&compiled_output_pattern);",
|
|
312
326
|
" if (!input_prefix.ok || !output_prefix.ok) {",
|
|
313
327
|
" k_wire_prefix_free(input_prefix);",
|
|
314
328
|
" k_wire_prefix_free(output_prefix);",
|
|
315
329
|
" k_rt_free(rt);",
|
|
316
|
-
" return 8;",
|
|
330
|
+
" return wire_error(8, \"could not prepare k-wire schemas\");",
|
|
317
331
|
" }",
|
|
318
332
|
" for (;;) {",
|
|
319
333
|
" uint64_t t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;",
|
|
@@ -331,7 +345,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
331
345
|
" k_wire_prefix_free(input_prefix);",
|
|
332
346
|
" k_wire_prefix_free(output_prefix);",
|
|
333
347
|
" k_rt_free(rt);",
|
|
334
|
-
" return 6;",
|
|
348
|
+
" return wire_error(6, \"could not read server frame header\");",
|
|
335
349
|
" }",
|
|
336
350
|
" uint32_t length = ((uint32_t)header[0] << 24) | ((uint32_t)header[1] << 16) | ((uint32_t)header[2] << 8) | (uint32_t)header[3];",
|
|
337
351
|
" unsigned char *payload = malloc(length == 0 ? 1 : length);",
|
|
@@ -339,7 +353,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
339
353
|
" k_wire_prefix_free(input_prefix);",
|
|
340
354
|
" k_wire_prefix_free(output_prefix);",
|
|
341
355
|
" k_rt_free(rt);",
|
|
342
|
-
" return 7;",
|
|
356
|
+
" return wire_error(7, \"could not allocate server input buffer\");",
|
|
343
357
|
" }",
|
|
344
358
|
" int payload_status = read_exact(stdin, payload, length);",
|
|
345
359
|
" if (payload_status != 1) {",
|
|
@@ -347,7 +361,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
347
361
|
" k_wire_prefix_free(input_prefix);",
|
|
348
362
|
" k_wire_prefix_free(output_prefix);",
|
|
349
363
|
" k_rt_free(rt);",
|
|
350
|
-
" return 6;",
|
|
364
|
+
" return wire_error(6, \"could not read server frame payload\");",
|
|
351
365
|
" }",
|
|
352
366
|
" if (tracing) t1 = monotonic_ns();",
|
|
353
367
|
" k_value *in_val = k_decode_wire_value_with_prefix(payload, length, &input_prefix, &compiled_input_pattern, rt);",
|
|
@@ -365,7 +379,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
365
379
|
" k_wire_prefix_free(input_prefix);",
|
|
366
380
|
" k_wire_prefix_free(output_prefix);",
|
|
367
381
|
" k_rt_free(rt);",
|
|
368
|
-
" return 2;",
|
|
382
|
+
" return wire_error(2, \"invalid or incompatible k-wire input\");",
|
|
369
383
|
" }",
|
|
370
384
|
" if (tracing) t2 = monotonic_ns();",
|
|
371
385
|
" k_result result = k_main(rt, in_val);",
|
|
@@ -375,7 +389,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
375
389
|
" k_wire_prefix_free(input_prefix);",
|
|
376
390
|
" k_wire_prefix_free(output_prefix);",
|
|
377
391
|
" k_rt_free(rt);",
|
|
378
|
-
" return
|
|
392
|
+
" return evaluation_error(result);",
|
|
379
393
|
" }",
|
|
380
394
|
" size_t out_len = 0;",
|
|
381
395
|
" unsigned char *out_payload = k_encode_wire_as_with_prefix(&output_prefix, &compiled_output_pattern, result.value, &out_len);",
|
|
@@ -411,7 +425,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
411
425
|
" k_wire_prefix_free(input_prefix);",
|
|
412
426
|
" k_wire_prefix_free(output_prefix);",
|
|
413
427
|
" k_rt_free(rt);",
|
|
414
|
-
" return 4;",
|
|
428
|
+
" return wire_error(4, \"could not encode or write k-wire output\");",
|
|
415
429
|
" }",
|
|
416
430
|
" }",
|
|
417
431
|
"}",
|
|
@@ -456,7 +470,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
456
470
|
" k_wire_input_free(input);",
|
|
457
471
|
" k_rt_free(run_rt);",
|
|
458
472
|
" k_rt_free(input_rt);",
|
|
459
|
-
" return
|
|
473
|
+
" return evaluation_error(result);",
|
|
460
474
|
" }",
|
|
461
475
|
" k_rt_reset(run_rt);",
|
|
462
476
|
" }",
|
|
@@ -476,19 +490,19 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
476
490
|
" int tracing = is_trace_enabled();",
|
|
477
491
|
" if (tracing) t0 = monotonic_ns();",
|
|
478
492
|
" k_rt *rt = k_rt_new();",
|
|
479
|
-
" if (rt == 0) return 1;",
|
|
493
|
+
" if (rt == 0) return wire_error(1, \"could not allocate runtime\");",
|
|
480
494
|
" k_wire_prefix output_prefix = k_wire_prefix_for_pattern(&compiled_output_pattern);",
|
|
481
495
|
" k_wire_input input = k_read_wire_envelope(stdin, rt);",
|
|
482
496
|
" if (input.value == 0 || input.pattern == 0) {",
|
|
483
497
|
" k_wire_prefix_free(output_prefix);",
|
|
484
498
|
" k_rt_free(rt);",
|
|
485
|
-
" return 2;",
|
|
499
|
+
" return wire_error(2, \"could not decode k-wire input\");",
|
|
486
500
|
" }",
|
|
487
501
|
" if (!k_pattern_compatible(input.pattern, &compiled_input_pattern)) {",
|
|
488
502
|
" k_wire_input_free(input);",
|
|
489
503
|
" k_wire_prefix_free(output_prefix);",
|
|
490
504
|
" k_rt_free(rt);",
|
|
491
|
-
" return 5;",
|
|
505
|
+
" return wire_error(5, \"k-wire input schema does not match the compiled input\");",
|
|
492
506
|
" }",
|
|
493
507
|
" if (tracing) t1 = monotonic_ns();",
|
|
494
508
|
" k_result result = k_main(rt, input.value);",
|
|
@@ -497,7 +511,7 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
497
511
|
" k_wire_input_free(input);",
|
|
498
512
|
" k_wire_prefix_free(output_prefix);",
|
|
499
513
|
" k_rt_free(rt);",
|
|
500
|
-
" return
|
|
514
|
+
" return evaluation_error(result);",
|
|
501
515
|
" }",
|
|
502
516
|
" size_t output_length = 0;",
|
|
503
517
|
" unsigned char *output_payload = k_encode_wire_as_with_prefix(&output_prefix, &compiled_output_pattern, result.value, &output_length);",
|
|
@@ -519,8 +533,8 @@ export function stdioDriverSource({ inputPattern = null, outputPattern = null }
|
|
|
519
533
|
" free(output_payload);",
|
|
520
534
|
" k_wire_input_free(input);",
|
|
521
535
|
" k_wire_prefix_free(output_prefix);",
|
|
522
|
-
" if (!ok) return 4;",
|
|
523
536
|
" k_rt_free(rt);",
|
|
537
|
+
" if (!ok) return wire_error(4, \"could not encode or write k-wire output\");",
|
|
524
538
|
" return 0;",
|
|
525
539
|
"}",
|
|
526
540
|
""
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#include "krt.h"
|
|
2
|
+
#include <assert.h>
|
|
3
|
+
|
|
4
|
+
int main(void) {
|
|
5
|
+
k_rt *rt = k_rt_new();
|
|
6
|
+
assert(rt);
|
|
7
|
+
k_value *tail = k_unit(rt);
|
|
8
|
+
k_rt_checkpoint base_mark = k_rt_mark(rt);
|
|
9
|
+
|
|
10
|
+
const size_t depth = 180000;
|
|
11
|
+
k_value *chain = tail;
|
|
12
|
+
for (size_t i = 0; i < depth; i++) {
|
|
13
|
+
chain = k_variant_borrowed_direct_n(rt, "next", 4, chain);
|
|
14
|
+
}
|
|
15
|
+
k_value *pair = k_product(rt, 2);
|
|
16
|
+
k_product_set(pair, "left", chain);
|
|
17
|
+
k_product_set(pair, "right", chain);
|
|
18
|
+
|
|
19
|
+
// The retained graph is larger than the allocation since the tail mark.
|
|
20
|
+
k_rt_tail_mark mark = {base_mark, k_rt_mark(rt)};
|
|
21
|
+
assert(k_rt_alloc(rt, 4 * 1024 * 1024));
|
|
22
|
+
pair = k_rt_compact(rt, pair, &mark);
|
|
23
|
+
|
|
24
|
+
chain = k_product_get(pair, "left");
|
|
25
|
+
assert(chain && chain == k_product_get(pair, "right"));
|
|
26
|
+
for (size_t i = 0; i < depth; i++) {
|
|
27
|
+
assert(k_variant_tag_matches(chain, "next", 4));
|
|
28
|
+
chain = k_variant_payload(chain);
|
|
29
|
+
}
|
|
30
|
+
assert(chain == tail);
|
|
31
|
+
k_rt_free(rt);
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
@@ -9,6 +9,9 @@ command -v clang >/dev/null || {
|
|
|
9
9
|
exit 1
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
clang -Iruntime runtime/krt.c tests/compaction-driver.c -o "$TMP_DIR/compaction"
|
|
13
|
+
"$TMP_DIR/compaction"
|
|
14
|
+
|
|
12
15
|
node ../../objects/compile.mjs '()' "$TMP_DIR/id.ko"
|
|
13
16
|
node ./bin/k-llvm-compile.mjs "$TMP_DIR/id.ko" "$TMP_DIR/id.ll"
|
|
14
17
|
grep -q '@k_llvm_metadata' "$TMP_DIR/id.ll"
|
|
@@ -129,6 +132,21 @@ printf '987654321' \
|
|
|
129
132
|
| node ../../codecs/int.mjs --print \
|
|
130
133
|
| grep -qx '975461057789971041'
|
|
131
134
|
|
|
135
|
+
input_pattern='[["closed-union",[["a",1],["b",1]]],["closed-product",[]]]'
|
|
136
|
+
printf '"b"\n' | node ../../codecs/k-parse.mjs --input-type '?<{} a, {} b>' > "$TMP_DIR/b.wire"
|
|
137
|
+
node ./bin/k-llvm-build.mjs --input-pattern "$input_pattern" -o "$TMP_DIR/require-a" '/a'
|
|
138
|
+
if "$TMP_DIR/require-a" < "$TMP_DIR/b.wire" > "$TMP_DIR/failed.wire" 2> "$TMP_DIR/failed.err"; then
|
|
139
|
+
exit 1
|
|
140
|
+
else
|
|
141
|
+
test "$?" -eq 3
|
|
142
|
+
fi
|
|
143
|
+
test ! -s "$TMP_DIR/failed.wire"
|
|
144
|
+
grep -Fq 'kVM: evaluation failed' "$TMP_DIR/failed.err"
|
|
145
|
+
if node ../../codecs/int.mjs --print < "$TMP_DIR/failed.wire" 2> "$TMP_DIR/failed.err"; then
|
|
146
|
+
exit 1
|
|
147
|
+
fi
|
|
148
|
+
grep -Fq 'No k-wire input received on stdin' "$TMP_DIR/failed.err"
|
|
149
|
+
|
|
132
150
|
printf '{"x":"left","y":"right"}' \
|
|
133
151
|
| node ../../codecs/k-parse.mjs \
|
|
134
152
|
| node ./bin/k-llvm.mjs --cache-dir "$TMP_DIR/jit-cache" "$TMP_DIR/run.ko" \
|
|
@@ -84,6 +84,7 @@ function runCodecCLI(metaUrl, codec) {
|
|
|
84
84
|
stdout.write(wire);
|
|
85
85
|
} else {
|
|
86
86
|
const buf = await readAll(stdin);
|
|
87
|
+
if (buf.length === 0) throw new Error("No k-wire input received on stdin");
|
|
87
88
|
const { value } = decodeWire(buf);
|
|
88
89
|
const output = codec.print(value);
|
|
89
90
|
if (Buffer.isBuffer(output) || codec.addNewline === false) {
|
|
@@ -617,16 +617,16 @@ function validatePackedPayload(reader, pattern) {
|
|
|
617
617
|
|
|
618
618
|
function readBitsAt(buffer, bitOffset, width) {
|
|
619
619
|
if (width === 0) return 0;
|
|
620
|
-
if ((bitOffset + width - 1)
|
|
620
|
+
if (Math.floor((bitOffset + width - 1) / 8) >= buffer.length) {
|
|
621
621
|
throw new Error("Unexpected end of bitstream");
|
|
622
622
|
}
|
|
623
623
|
if (width === 1) {
|
|
624
|
-
return (buffer[bitOffset
|
|
624
|
+
return (buffer[Math.floor(bitOffset / 8)] >> (7 - (bitOffset & 7))) & 1;
|
|
625
625
|
}
|
|
626
626
|
let v = 0;
|
|
627
627
|
for (let i = 0; i < width; i++) {
|
|
628
628
|
const pos = bitOffset + i;
|
|
629
|
-
v = (v << 1) | ((buffer[pos
|
|
629
|
+
v = (v << 1) | ((buffer[Math.floor(pos / 8)] >> (7 - (pos & 7))) & 1);
|
|
630
630
|
}
|
|
631
631
|
return v;
|
|
632
632
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fraczak/k",
|
|
3
|
-
"version": "9.1.
|
|
3
|
+
"version": "9.1.1",
|
|
4
4
|
"description": "k — a language for content-addressable schemas and data transformations over algebraic data types (products and tagged unions); includes runtime, contract derivation, REPL, and codec toolchain",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
package/tests/test-codecs.mjs
CHANGED
|
@@ -13,6 +13,7 @@ import * as Utf8Codec from "../codecs/utf8.mjs";
|
|
|
13
13
|
import * as Utf16Codec from "../codecs/utf16.mjs";
|
|
14
14
|
import * as IeeeCodec from "../codecs/ieee.mjs";
|
|
15
15
|
import * as UnitCodec from "../codecs/unit.mjs";
|
|
16
|
+
import { readBitsAt } from "../codecs/runtime/prefix-codec.mjs";
|
|
16
17
|
import {
|
|
17
18
|
valueToK,
|
|
18
19
|
valueWithEnvelopeToK
|
|
@@ -213,6 +214,13 @@ console.log("7. Verifying lazy prefix-free wire decoding...");
|
|
|
213
214
|
{
|
|
214
215
|
assert.equal(typeof SDK.decodeWireLazy, "function");
|
|
215
216
|
|
|
217
|
+
// Exercise large bit offsets without allocating a 256 MiB buffer.
|
|
218
|
+
const byte = 2 ** 28;
|
|
219
|
+
const sparseWire = { length: byte + 2, [byte]: 0b10000001, [byte + 1]: 0b10000000 };
|
|
220
|
+
assert.equal(readBitsAt(sparseWire, byte * 8, 1), 1);
|
|
221
|
+
assert.equal(readBitsAt(sparseWire, byte * 8 + 7, 3), 0b110);
|
|
222
|
+
assert.throws(() => readBitsAt(sparseWire, sparseWire.length * 8, 1), /Unexpected end of bitstream/);
|
|
223
|
+
|
|
216
224
|
// Lazy decoding should instantiate root in sub-millisecond time
|
|
217
225
|
const sampleStr = "a".repeat(20000);
|
|
218
226
|
const parsedStr = Utf8Codec.parse(sampleStr);
|