@fraczak/k 9.0.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 +18 -0
- package/DOCS/KTVM_SPEC.md +45 -40
- package/DOCS/PROFILING_AND_TRACING.md +7 -0
- package/README.md +6 -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/src/kvm2llvm.mjs +0 -2
- package/backends/llvm/src/llvm.mjs +0 -2
- package/backends/llvm/tests/compaction-driver.c +33 -0
- package/backends/llvm/tests/integration.sh +18 -0
- package/backends/llvm/tests/test-llvm.mjs +2 -0
- package/backends/wasm/README.md +5 -0
- package/backends/wasm/src/wasm.mjs +50 -1
- package/backends/wasm/tests/test-wasm.mjs +41 -2
- package/codecs/runtime/codec-sdk.mjs +1 -0
- package/codecs/runtime/prefix-codec.mjs +70 -4
- package/ktvm/bit-deque.mjs +19 -4
- package/ktvm/index.mjs +11 -0
- package/ktvm/schema.mjs +594 -3
- package/ktvm/test-ktvm.mjs +430 -1
- package/ktvm/wasm/runtime.wat +10 -19
- package/ktvm/wasm/test-wasm.mjs +3 -0
- package/ktvm/wire.mjs +222 -32
- package/package.json +1 -1
- package/paper/Makefile +8 -5
- package/paper/k-transducer-compiler.pdf +0 -0
- package/paper/k-transducer-compiler.tex +658 -0
- package/paper/replay-diagnostic.mjs +63 -0
- package/scripts/perf-ieee.mjs +5 -3
- package/scripts/perf-int.mjs +2 -2
- package/scripts/perf-multiplication.mjs +109 -68
- package/tests/test-codecs.mjs +8 -0
|
@@ -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
|
""
|
|
@@ -27,7 +27,6 @@ const K_RT_BLOCKS_OFFSET = 0;
|
|
|
27
27
|
const K_RT_UNIT_CACHE_OFFSET = 8;
|
|
28
28
|
const K_RT_BIT0_CACHE_OFFSET = 16;
|
|
29
29
|
const K_RT_BIT1_CACHE_OFFSET = 24;
|
|
30
|
-
const K_RT_HAS_REUSABLE_BLOCKS_OFFSET = 32;
|
|
31
30
|
const K_ARENA_BLOCK_USED_OFFSET = 8;
|
|
32
31
|
|
|
33
32
|
function alignArenaSize(size) {
|
|
@@ -303,7 +302,6 @@ function lowerRtRewind(ctx, mark) {
|
|
|
303
302
|
storePtrAt(ctx, "%rt", K_RT_UNIT_CACHE_OFFSET, "null");
|
|
304
303
|
storePtrAt(ctx, "%rt", K_RT_BIT0_CACHE_OFFSET, "null");
|
|
305
304
|
storePtrAt(ctx, "%rt", K_RT_BIT1_CACHE_OFFSET, "null");
|
|
306
|
-
storeI32At(ctx, "%rt", K_RT_HAS_REUSABLE_BLOCKS_OFFSET, 0);
|
|
307
305
|
ctx.lines.push(` br label %${doneBlock}`);
|
|
308
306
|
|
|
309
307
|
ctx.lines.push(`${slowBlock}:`);
|
|
@@ -35,7 +35,6 @@ const K_RT_BLOCKS_OFFSET = 0;
|
|
|
35
35
|
const K_RT_UNIT_CACHE_OFFSET = 8;
|
|
36
36
|
const K_RT_BIT0_CACHE_OFFSET = 16;
|
|
37
37
|
const K_RT_BIT1_CACHE_OFFSET = 24;
|
|
38
|
-
const K_RT_HAS_REUSABLE_BLOCKS_OFFSET = 32;
|
|
39
38
|
const K_ARENA_BLOCK_USED_OFFSET = 8;
|
|
40
39
|
const K_ARENA_BLOCK_CAPACITY_OFFSET = 16;
|
|
41
40
|
const K_ARENA_BLOCK_DATA_OFFSET = 24;
|
|
@@ -414,7 +413,6 @@ function lowerRtRewind(ctx, mark) {
|
|
|
414
413
|
storePtrAt(ctx, "%rt", K_RT_UNIT_CACHE_OFFSET, "null");
|
|
415
414
|
storePtrAt(ctx, "%rt", K_RT_BIT0_CACHE_OFFSET, "null");
|
|
416
415
|
storePtrAt(ctx, "%rt", K_RT_BIT1_CACHE_OFFSET, "null");
|
|
417
|
-
storeI32At(ctx, "%rt", K_RT_HAS_REUSABLE_BLOCKS_OFFSET, 0);
|
|
418
416
|
ctx.lines.push(` br label %${doneBlock}`);
|
|
419
417
|
|
|
420
418
|
ctx.lines.push(`${slowBlock}:`);
|
|
@@ -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
|
+
}
|