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

@@ -20,6 +20,7 @@ module Sx_primitives = Sx.Sx_primitives
module Sx_runtime = Sx.Sx_runtime
module Sx_ref = Sx.Sx_ref
module Sx_render = Sx.Sx_render
module Sx_vm = Sx.Sx_vm
open Sx_types
@@ -778,6 +779,19 @@ let dispatch env cmd =
| Eval_error msg -> send_error msg
| exn -> send_error (Printexc.to_string exn))
| List [Symbol "vm-exec"; code_val] ->
(* Execute a bytecode module on the VM.
code_val is a dict with {bytecode, pool} from compiler.sx *)
(try
let code = Sx_vm.code_from_value code_val in
let globals = Hashtbl.create 256 in
Hashtbl.iter (fun k v -> Hashtbl.replace globals k v) env.bindings;
let result = Sx_vm.execute_module code globals in
send_ok_value result
with
| Eval_error msg -> send_error msg
| exn -> send_error (Printexc.to_string exn))
| List [Symbol "reset"] ->
(* Clear all bindings and rebuild env.
We can't reassign env, so clear and re-populate. *)