Set arity in native compiler and bytecode modules
Sx_compiler.compile/compile_module now emit arity (local slot count) in the bytecode dict. MCP sx_build_bytecode serializes arity into .sxbc.json. sx-platform.js passes arity through to K.loadModule(). Without this, the VM allocated only 16 local slots per module frame. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -635,7 +635,11 @@ let rec handle_tool name args =
|
||||
let consts = match Sx_runtime.get code (String "constants") with
|
||||
| List l | ListRef { contents = l } -> String.concat "," (List.map const_to_json l)
|
||||
| _ -> "" in
|
||||
Printf.sprintf "{\"t\":\"code\",\"v\":{\"bytecode\":[%s],\"constants\":[%s]}}" bc consts
|
||||
let arity = match Sx_runtime.get code (String "arity") with
|
||||
| Number n -> int_of_float n | _ -> 0 in
|
||||
let uvc = match Sx_runtime.get code (String "upvalue-count") with
|
||||
| Number n -> int_of_float n | _ -> 0 in
|
||||
Printf.sprintf "{\"t\":\"code\",\"v\":{\"arity\":%d,\"upvalue-count\":%d,\"bytecode\":[%s],\"constants\":[%s]}}" arity uvc bc consts
|
||||
and json_escape s =
|
||||
let buf = Buffer.create (String.length s + 2) in
|
||||
Buffer.add_char buf '"';
|
||||
@@ -666,8 +670,10 @@ let rec handle_tool name args =
|
||||
let consts = match Sx_runtime.get code (String "constants") with
|
||||
| List l | ListRef { contents = l } -> String.concat "," (List.map const_to_json l)
|
||||
| _ -> "" in
|
||||
let json = Printf.sprintf "{\"magic\":\"SXBC\",\"version\":1,\"hash\":\"%s\",\"module\":{\"bytecode\":[%s],\"constants\":[%s]}}"
|
||||
hash bc consts in
|
||||
let arity = match Sx_runtime.get code (String "arity") with
|
||||
| Number n -> int_of_float n | _ -> 0 in
|
||||
let json = Printf.sprintf "{\"magic\":\"SXBC\",\"version\":1,\"hash\":\"%s\",\"module\":{\"arity\":%d,\"bytecode\":[%s],\"constants\":[%s]}}"
|
||||
hash arity bc consts in
|
||||
let json_path = (String.sub src_path 0 (String.length src_path - 3)) ^ ".sxbc.json" in
|
||||
Out_channel.with_open_text json_path (fun oc -> output_string oc json);
|
||||
let kb = String.length json / 1024 in
|
||||
|
||||
@@ -39,10 +39,10 @@ let rec skip_annotations items =
|
||||
| _ -> Nil
|
||||
|
||||
(* compile_match: uses local recursion (letrec) that the transpiler can't handle.
|
||||
Delegates to the SX-level compile-match via a ref set at init time. *)
|
||||
let _compile_match_fn : (value -> value -> value -> value -> value) ref =
|
||||
ref (fun _em _args _scope _tail_p -> Nil)
|
||||
let compile_match em args scope tail_p = !_compile_match_fn em args scope tail_p
|
||||
Falls back to CEK evaluation at runtime. *)
|
||||
let compile_match em args scope tail_p =
|
||||
let fn = Sx_ref.eval_expr (Symbol "compile-match") (Env (Sx_types.make_env ())) in
|
||||
Sx_ref.cek_call fn (List [em; args; scope; tail_p])
|
||||
|
||||
|
||||
(* === Transpiled from bytecode compiler === *)
|
||||
@@ -204,9 +204,9 @@ and compile_call em head args scope tail_p =
|
||||
|
||||
(* compile *)
|
||||
and compile expr =
|
||||
(let () = ignore ((String "Compile a single SX expression to a bytecode module.")) in (let em = (make_emitter ()) in let scope = (make_scope (Nil)) in (let () = ignore ((compile_expr (em) (expr) (scope) ((Bool false)))) in (let () = ignore ((emit_op (em) ((Number 50.0)))) in (let _d = Hashtbl.create 2 in Hashtbl.replace _d "constants" (get ((get (em) ((String "pool")))) ((String "entries"))); Hashtbl.replace _d "bytecode" (get (em) ((String "bytecode"))); Dict _d)))))
|
||||
(let () = ignore ((String "Compile a single SX expression to a bytecode module.")) in (let em = (make_emitter ()) in let scope = (make_scope (Nil)) in (let () = ignore ((compile_expr (em) (expr) (scope) ((Bool false)))) in (let () = ignore ((emit_op (em) ((Number 50.0)))) in (let _d = Hashtbl.create 3 in let () = ignore (Hashtbl.replace _d "arity" (get (scope) (String "next-slot"))) in Hashtbl.replace _d "constants" (get ((get (em) ((String "pool")))) ((String "entries"))); Hashtbl.replace _d "bytecode" (get (em) ((String "bytecode"))); Dict _d)))))
|
||||
|
||||
(* compile-module *)
|
||||
and compile_module exprs =
|
||||
(let () = ignore ((String "Compile a list of top-level expressions to a bytecode module.")) in (let em = (make_emitter ()) in let scope = (make_scope (Nil)) in (let () = ignore ((List.iter (fun expr -> ignore ((let () = ignore ((compile_expr (em) (expr) (scope) ((Bool false)))) in (emit_op (em) ((Number 5.0)))))) (sx_to_list (init (exprs))); Nil)) in (let () = ignore ((compile_expr (em) ((last (exprs))) (scope) ((Bool false)))) in (let () = ignore ((emit_op (em) ((Number 50.0)))) in (let _d = Hashtbl.create 2 in Hashtbl.replace _d "constants" (get ((get (em) ((String "pool")))) ((String "entries"))); Hashtbl.replace _d "bytecode" (get (em) ((String "bytecode"))); Dict _d))))))
|
||||
(let () = ignore ((String "Compile a list of top-level expressions to a bytecode module.")) in (let em = (make_emitter ()) in let scope = (make_scope (Nil)) in (let () = ignore ((List.iter (fun expr -> ignore ((let () = ignore ((compile_expr (em) (expr) (scope) ((Bool false)))) in (emit_op (em) ((Number 5.0)))))) (sx_to_list (init (exprs))); Nil)) in (let () = ignore ((compile_expr (em) ((last (exprs))) (scope) ((Bool false)))) in (let () = ignore ((emit_op (em) ((Number 50.0)))) in (let _d = Hashtbl.create 3 in let () = ignore (Hashtbl.replace _d "arity" (get (scope) (String "next-slot"))) in Hashtbl.replace _d "constants" (get ((get (em) ((String "pool")))) ((String "entries"))); Hashtbl.replace _d "bytecode" (get (em) ((String "bytecode"))); Dict _d))))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user