native x86-64 backend (pluggable, per-opcode lowering)

A second backend that lowers each bytecode instruction to a machine-code
call into a runtime helper, with control flow as real branches.

- cat/backend/x86_64_glue.c: one helper per opcode, mirroring cat/vm.c
  exactly (frame lifecycle, arith, cmp, attr/idx, call/callmethod, is,
  next/fornext, try/raise).  Helpers use the SysV convention (f in rdi,
  a/b/c in rsi/rdx/rcx) and return 0/1/-1.
- cat/backend/x86_64.c: emits AT&T assembly (one labelled block per
  instruction, one call per helper, real jumps for JUMP/JUMPIF/JUMPIFNOT/
  FORNEXT/RETURN, an .Lonerror block that resumes try handlers), plus a
  small generated _main.c that rebuilds the bytecode and boots the VM.
  Builds with as + cc linked against libcatscript.a.
- generators are not lowered: their prototype carries no nat_entry, so
  the VM runs them (their bytecode is rebuilt by the generated main), and
  the emitted code simply falls back to the interpreter.
- cat_vm_exec_closure dispatches to a prototype's nat_entry when present.
- tests: native-vs-interpreter equivalence check.  Suite 155 -> 156.

Verified: hello, control, classes, generators, arith, errors, text, oop,
slots, reflect, toml, files, ffi all match the interpreter natively.
This commit is contained in:
Minus committed 2026-11-18 20:19:54 +00:00
1 parent 1832326a3d
commit 77ea433215
8 files changed
+1221 -48

No files matched your search

+16
View File
@@ -69,6 +69,17 @@ CAT_VM_State *cat_vm_new(void);
int cat_vm_run(CAT_VM_State *vm, CAT_BYTECODE_Function *fn,
CAT_VALUE_Value **out);
/* Call any callable value by the VM's own rules: a closure runs its
* prototype (or yields a generator), a native runs its C callback, a
* table with a `call` member is invoked. Exposed for out-of-line code
* generators (e.g. the native backend's CALL helper). */
int cat_vm_call_value(CAT_VM_State *vm, CAT_VALUE_Value *fnv,
CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out,
CAT_ERROR_Error *err);
/* Print a value the way `print` does (used by code generators). */
void cat_vm_print_value(CAT_VALUE_Value *v);
/* Register a native function (name, C callback) into globals. */
int cat_vm_register_native(CAT_VM_State *vm, const char *name,
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int,
@@ -78,6 +89,11 @@ int cat_vm_register_native(CAT_VM_State *vm, const char *name,
* closures/natives invoked through the object system. */
CAT_VM_State *cat_vm_current(void);
/* Install/restore the thread-local current VM. Out-of-line code
* generators (the native backend) call this around native execution so
* natives invoked from generated code see the active VM. */
void cat_vm_set_current(CAT_VM_State *vm);
/* Run (or resume) a frame. Returns 0 when the frame returns (*out set),
* 1 when it yields (*out set, frame left resumable), or -1 on error
* (vm->err set). The frame owns neither its operand stack nor its