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.
- cat/backend/backend.h: the CAT_BACKEND_Backend contract (name,
description, native flag, emit(), compile()).
- cat/backend/registry.c: the single table of targets; cat_backend_find/
default/count/at. Adding a backend = one file + one line here.
- cat/backend/c.c: exposes cat_backend_c_backend(); body unchanged.
- cat/backend/x86_64.c: the native x86-64 backend descriptor (lowering
stubbed for now).
- main.c: --backend <name> --emit/--build, --list-backends; --emit-c and
--build stay as shortcuts for the default (c) backend.
- Makefile: build the new files. Suite 151 -> 155.
- new cat/backend/c.{h,c}: lower a compiled CAT_BYTECODE_Function to a
self-contained C translation unit that rebuilds the bytecode and runs
it on the catscript runtime. The tree is emitted in pre-order with
forward declarations; constants (int/float/text/bool) are recreated by
the matching constructors; jumps keep their absolute targets.
- Makefile: also build libcatscript.a (all objects but main.o) so an AOT
program links the runtime without clashing on `main`.
- main.c: `--emit-c <file>` prints the generated C; `--build <file> <out>`
emits it, invokes the system `cc`, and produces the executable.
- tests/aot.cat + suite: the native binary's stdout must equal the
interpreter's (arithmetic, recursion, objects, interfaces, `is`,
concat, comparisons). Suite 151 -> 152.
The backend is faithful by construction: it lowers the very bytecode the
interpreter runs, so native output matches the interpreter exactly
(including current language quirks, e.g. upvalues and `try`/`catch`).