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

+112
View File
@@ -0,0 +1,112 @@
/*
* cat/backend/x86_64.h -- runtime glue for the native x86-64 backend.
*
* Naming rule: cat_backend_x86_64_* / CAT_BACKEND_X86_64_*.
*
* Each bytecode instruction lowers to a call into one helper here (one
* per opcode); control flow becomes real jumps in the generated code.
* The assembly never needs struct offsets: it just sets arguments and
* calls a symbol.
*
* Helper convention (SysV): f in rdi, operands a/b/c in rsi/rdx/rcx.
* Return in eax: 0 ok; 1 ok-take-branch (popcond/fornext); -1 error
* (vm->err set), and the generated code runs its .Lonerror block.
*/
#ifndef CAT_BACKEND_X86_64_H
#define CAT_BACKEND_X86_64_H
#include "cat/value.h"
#include "cat/vm.h"
#include "cat/bytecode.h"
#include "cat/types/function.h"
typedef struct CAT_BACKEND_X86_64_Handler {
void *target; /* code address of the catch block */
int sp;
} CAT_BACKEND_X86_64_Handler;
typedef struct CAT_BACKEND_X86_64_Frame {
CAT_VM_State *vm;
CAT_BYTECODE_Function *fn;
CAT_VALUE_Value **stack;
CAT_VALUE_Value *locals[256];
int sp;
int nhandlers;
CAT_BACKEND_X86_64_Handler handlers[CAT_VM_MAXHANDLER];
CAT_VALUE_Value *ret;
} CAT_BACKEND_X86_64_Frame;
/* A generated entry point. Returns 0 (f->ret set) or -1 (vm->err set). */
typedef int (*CAT_BACKEND_X86_64_Entry)(CAT_BACKEND_X86_64_Frame *f);
/* Bridge used by cat_vm_exec_closure for a prototype with a native entry. */
int cat_backend_x86_64_top_call(CAT_VM_State *vm,
CAT_TYPES_FUNCTION_Closure *cl,
CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value **out);
/* Frame lifecycle (prologue/epilogue helpers). */
void cat_backend_x86_64_frame_init(CAT_BACKEND_X86_64_Frame *f,
CAT_VM_State *vm,
CAT_TYPES_FUNCTION_Closure *cl,
CAT_VALUE_Value **args, long nargs);
void cat_backend_x86_64_frame_done(CAT_BACKEND_X86_64_Frame *f);
/* On error: resume the innermost handler (its code address), else NULL. */
void *cat_backend_x86_64_handle_error(CAT_BACKEND_X86_64_Frame *f);
/* Constant constructors (rebuild the pool in generated `main`). */
CAT_VALUE_Value *cat_backend_x86_64_const_int(long long v);
CAT_VALUE_Value *cat_backend_x86_64_const_float(double v);
CAT_VALUE_Value *cat_backend_x86_64_const_text(const char *s);
CAT_VALUE_Value *cat_backend_x86_64_const_bool(long t);
CAT_VALUE_Value *cat_backend_x86_64_const_empty(void);
/* One helper per opcode. */
int cat_backend_x86_64_const(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_empty(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_true(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_false(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_getlocal(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_setlocal(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_getglobal(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_setglobal(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_pop(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_dup(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_arith(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_neg(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_not(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_cmp(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_concat(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_getattr(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_setattr(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_getidx(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_setidx(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_newtable(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_closure(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_call(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_callmethod(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_print(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_printexpr(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_is(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_next(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_popcond(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_fornext(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_return(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_trysetup(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_tryend(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
int cat_backend_x86_64_raise(CAT_BACKEND_X86_64_Frame *f, long a, long b, long c);
/* Rebuild bytecode (used by generated `main`). */
CAT_BYTECODE_Function *cat_backend_x86_64_fn_new(const char *name);
void cat_backend_x86_64_fn_setup(CAT_BYTECODE_Function *fn, long nparams,
long nlocals, long is_generator);
void cat_backend_x86_64_fn_add_const(CAT_BYTECODE_Function *fn,
CAT_VALUE_Value *v);
void cat_backend_x86_64_fn_emit(CAT_BYTECODE_Function *fn, long op, long a,
long b, long c, long line);
void cat_backend_x86_64_fn_add_proto(CAT_BYTECODE_Function *fn,
CAT_BYTECODE_Function *p);
void cat_backend_x86_64_fn_set_entry(CAT_BYTECODE_Function *fn, void *entry);
#endif /* CAT_BACKEND_X86_64_H */
+1
View File
@@ -97,6 +97,7 @@ typedef struct CAT_BYTECODE_Function {
int nlocals; /* number of local slots */
int nparams; /* number of parameters */
int is_generator; /* contains yield: calls produce a generator */
void *nat_entry; /* native code entry (x86_64 backend), or NULL */
} CAT_BYTECODE_Function;
CAT_BYTECODE_Function *cat_bytecode_function_new(const char *name);
+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