/* * 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 */