Skip to content

Commit 8fd3df7

Browse files
committed
Improve stack printing by resolving closures
1 parent a63d3b1 commit 8fd3df7

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

runtime.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
const fs = require('fs').promises;
22

3-
const CLS_ID_TABLE = {
4-
0: 'I',
5-
1: 'K',
6-
2: 'S'
7-
}
8-
93
Promise.all([
104
fs.readFile('./stack-machine.wasm'),
115
fs.readFile('./program.wasm')
@@ -21,29 +15,39 @@ Promise.all([
2115
}
2216
))
2317
.then(result => {
18+
const STACK_SIZE = result.instance.exports.STACK_SIZE.value;
19+
const STACK_FRAME_SIZE = result.instance.exports.STACK_FRAME_SIZE.value;
20+
21+
const CLS_SIZE = result.instance.exports.CLS_SIZE.value;
22+
const CLS_HEAP_ADDR = result.instance.exports.CLS_HEAP_ADDR.value;
23+
2424
const workingMemoryOffset = result.instance.exports.WM_ADDR.value;
2525
console.log(`Working Memory address: ${workingMemoryOffset}`);
2626
const programMem = new Uint8Array(result.instance.exports.mem.buffer, 0, workingMemoryOffset);
27+
2728
const stackTopPointer = result.instance.exports.stack_top_pointer.value;
2829
console.log(`Stack Top pointer: ${stackTopPointer}`);
2930
const stackMem = new Uint8Array(result.instance.exports.mem.buffer, workingMemoryOffset, stackTopPointer - workingMemoryOffset);
30-
const heapMem = new Uint8Array(result.instance.exports.mem.buffer, workingMemoryOffset + 1024, 13 * 20);
31+
32+
const clsHeapPointer = result.instance.exports.cls_heap_pointer.value;
33+
const closureMemoryTotal = clsHeapPointer - CLS_HEAP_ADDR;
34+
console.log(`Heap pointer: ${clsHeapPointer}, ${closureMemoryTotal / CLS_SIZE} closures ⨉ ${CLS_SIZE} bytes`);
35+
36+
const heapMem = new Uint8Array(result.instance.exports.mem.buffer, workingMemoryOffset + STACK_SIZE, closureMemoryTotal);
3137
console.log(`Program Memory: ${programMem}\n`);
3238

3339
const stackClss = [];
3440

35-
for (let i = 0; i < stackMem.length; i += 4) {
36-
const buffer = Buffer.from(stackMem.subarray(i, i + 4));
37-
const clsId = buffer.readUIntLE(0, 4);
41+
for (let i = 0; i < stackMem.length; i += STACK_FRAME_SIZE) {
42+
const buffer = Buffer.from(stackMem.subarray(i, i + STACK_FRAME_SIZE));
43+
const clsId = buffer.readUIntLE(0, buffer.length);
3844
stackClss.push(clsId);
3945
}
4046

41-
console.log(`Stack:\n ${stackClss.map((id, index) => `#${index} ${CLS_ID_TABLE[id]}`)}\n`);
42-
4347
const heapClss = [];
4448

45-
for (let i = 0; i < heapMem.length; i += 13) {
46-
const buffer = Buffer.from(heapMem.subarray(i, i + 13));
49+
for (let i = 0; i < heapMem.length; i += CLS_SIZE) {
50+
const buffer = Buffer.from(heapMem.subarray(i, i + CLS_SIZE));
4751
const clsId = buffer.readUIntLE(0, 4);
4852
const tag = buffer.readUIntLE(4, 1);
4953
const data1 = buffer.readUIntLE(5, 4)
@@ -55,6 +59,8 @@ Promise.all([
5559
for (const heapCls of heapClss) {
5660
console.log(` ${formatHeapCLS(heapCls)}`)
5761
}
62+
63+
console.log(`\nStack:\n ${stackClss.map((closureIndex, id) => formatStackFrame(heapClss, id, closureIndex))}`);
5864
})
5965
)
6066

@@ -77,3 +83,7 @@ function formatHeapCLS({ clsId, tag, data1, data2 }) {
7783
}
7884
return `#${clsId} ${formatCLS()}`
7985
}
86+
87+
function formatStackFrame (closures, id, closureIndex) {
88+
return `#${id} ${formatHeapCLS(closures[closureIndex])}`
89+
}

stack-machine.wasm

78 Bytes
Binary file not shown.

stack-machine.wat

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@
99

1010
;; exports
1111
(export "mem" (memory $mem))
12+
1213
(export "WM_ADDR" (global $WM_ADDR))
14+
(export "STACK_SIZE" (global $STACK_SIZE))
15+
(export "STACK_FRAME_SIZE" (global $STACK_FRAME_SIZE))
16+
(export "CLS_SIZE" (global $CLS_SIZE))
17+
(export "CLS_HEAP_ADDR" (global $CLS_HEAP_ADDR))
18+
1319
(export "stack_top_pointer" (global $stack_top_pointer))
20+
(export "cls_heap_pointer" (global $cls_heap_pointer))
21+
1422
(export "main" (func $main))
1523

1624
;; closure encoding tags
@@ -35,23 +43,23 @@
3543
(global $CLS_HEAP_ADDR (mut i32) (i32.const 0)) ;; current closure heap address (initialized with $WM_ADDR + $STACK_SIZE)
3644

3745
(global $stack_top_pointer (mut i32) (i32.const 0)) ;; current stack top pointer (initialized with $wm_offset)
38-
(global $cls_heap_offset (mut i32) (i32.const 0)) ;; current closure heap offset (initialized with $wm_offset + $STACK_SIZE)
46+
(global $cls_heap_pointer (mut i32) (i32.const 0)) ;; current closure heap offset (initialized with $wm_offset + $STACK_SIZE)
3947
(global $cls_id (mut i32) (i32.const 0)) ;; next closure id
4048

4149
(func $write_cls (param $tag i32) (param $data i32)
4250
;; TODO: speed-up with shifts
4351
(local $offset i32)
44-
(local.set $offset (global.get $cls_heap_offset))
52+
(local.set $offset (global.get $cls_heap_pointer))
4553
(i32.store (local.get $offset) (global.get $cls_id)) ;; cls_id
4654
(global.set $cls_id (i32.add (global.get $cls_id) (i32.const 1))) ;; inc cls_id
4755
(i32.store8 (i32.add (local.get $offset) (i32.const 4)) (local.get $tag)) ;; tag
4856
(i32.store (i32.add (local.get $offset) (i32.const 5)) (local.get $data)) ;; data
49-
(global.set $cls_heap_offset (i32.add (local.get $offset) (global.get $CLS_SIZE)))
57+
(global.set $cls_heap_pointer (i32.add (local.get $offset) (global.get $CLS_SIZE)))
5058
)
5159

5260
(func $write_cls_ext (param $tag i32) (param $data1 i32) (param $data2 i32)
5361
(local $offset i32)
54-
(local.set $offset (global.get $cls_heap_offset))
62+
(local.set $offset (global.get $cls_heap_pointer))
5563
(call $write_cls (local.get $tag) (local.get $data1))
5664
(i32.store (i32.add (local.get $offset) (i32.const 9)) (local.get $data2)) ;; data2
5765
)
@@ -220,7 +228,7 @@
220228

221229
;; init memory pointers
222230
(global.set $stack_top_pointer (global.get $WM_ADDR))
223-
(global.set $cls_heap_offset (i32.add (global.get $WM_ADDR) (global.get $STACK_SIZE)))
231+
(global.set $cls_heap_pointer (i32.add (global.get $WM_ADDR) (global.get $STACK_SIZE)))
224232

225233
;; init closure heap
226234
(call $write_cls (global.get $CLS_I) (i32.const 0))

0 commit comments

Comments
 (0)