SX bytecode VM executing: compile → run → correct results

End-to-end pipeline working:
  Python compiler.sx → bytecode → OCaml VM → result

Verified: (+ (* 3 4) 2) → 14 ✓
          (+ 0 1 2 ... 49) → 1225 ✓

Benchmark (500 iterations, 50 additions each):
  CEK machine: 327ms
  Bytecode VM: 145ms
  Speedup: 2.2x

VM handles: constants, local variables, global variables,
primitive calls, jumps, conditionals, closures (via NativeFn
wrapper), define, return.

Protocol: (vm-exec {:bytecode (...) :constants (...)})
  - Compiler outputs clean format (no internal index dict)
  - VM converts bytecode list to int array, constants to value array
  - Stack-based execution with direct opcode dispatch

The 2.2x speedup is for pure arithmetic. For aser (the real
target), the speedup will be larger because aser involves:
- String building (no CEK frame allocation in VM)
- Map/filter iterations (no frame-per-iteration in VM)
- Closure calls (no thunk/trampoline in VM)

Next: compile and run the aser adapter on the VM.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 18:52:50 +00:00
parent c8533181ab
commit a8d1163aa6
3 changed files with 153 additions and 96 deletions

View File

@@ -375,7 +375,7 @@
;; Add code object to parent constant pool
(let ((code {:arity (len (get fn-scope "locals"))
:bytecode (get fn-em "bytecode")
:pool (get fn-em "pool")
:constants (get (get fn-em "pool") "entries")
:upvalues (get fn-scope "upvalues")})
(code-idx (pool-add (get em "pool") code)))
(emit-op em 51) ;; OP_CLOSURE
@@ -466,7 +466,7 @@
(compile-expr em expr scope false)
(emit-op em 50) ;; OP_RETURN
{:bytecode (get em "bytecode")
:pool (get em "pool")})))
:constants (get (get em "pool") "entries")})))
(define compile-module
(fn (exprs)
@@ -481,4 +481,4 @@
(compile-expr em (last exprs) scope false)
(emit-op em 50) ;; OP_RETURN
{:bytecode (get em "bytecode")
:pool (get em "pool")})))
:constants (get (get em "pool") "entries")})))