Files
catscript/include/cat/bytecode.h
T
Minus 77ea433215 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.
2026-11-18 20:19:54 +00:00

121 lines
4.7 KiB
C

/*
* cat/bytecode.h -- the catscript bytecode format.
*
* Naming rule: symbols here are cat_bytecode_* / CAT_BYTECODE_*.
*
* A compiled module is one CAT_BYTECODE_Function. Instructions are a
* flat array of CAT_BYTECODE_Instr. The VM (cat/vm.h) executes them.
*
* Operand conventions:
* - A/B/C are small integer operands (register-free, stack based).
* - constants live in `consts[]`; locals in the current frame;s slots.
*/
#ifndef CAT_BYTECODE_H
#define CAT_BYTECODE_H
#include "cat/value.h"
#include <stdio.h>
/* Opcodes (documented, stable). */
enum CAT_BYTECODE_Op {
CAT_BYTECODE_OP_NOP = 0,
CAT_BYTECODE_OP_CONST, /* A: index into consts[] */
CAT_BYTECODE_OP_EMPTY, /* push the empty object */
CAT_BYTECODE_OP_TRUE, /* push true */
CAT_BYTECODE_OP_FALSE, /* push false */
CAT_BYTECODE_OP_GETLOCAL, /* A: local slot -> stack */
CAT_BYTECODE_OP_SETLOCAL, /* A: pop -> local slot */
CAT_BYTECODE_OP_GETGLOBAL, /* A: const name -> stack */
CAT_BYTECODE_OP_SETGLOBAL, /* A: const name, pop value */
CAT_BYTECODE_OP_ADD, /* pop b, a -> push a+b */
CAT_BYTECODE_OP_SUB,
CAT_BYTECODE_OP_MUL,
CAT_BYTECODE_OP_DIV,
CAT_BYTECODE_OP_REM,
CAT_BYTECODE_OP_NEG, /* unary minus */
CAT_BYTECODE_OP_NOT,
CAT_BYTECODE_OP_EQ,
CAT_BYTECODE_OP_NE,
CAT_BYTECODE_OP_LT,
CAT_BYTECODE_OP_LE,
CAT_BYTECODE_OP_GT,
CAT_BYTECODE_OP_GE,
CAT_BYTECODE_OP_GETATTR, /* A: const name; pop obj -> stack */
CAT_BYTECODE_OP_SETATTR, /* A: const name; pop val, obj */
CAT_BYTECODE_OP_NEWTABLE, /* push empty table */
CAT_BYTECODE_OP_CALL, /* A: nargs; pop fn+args */
CAT_BYTECODE_OP_CALLMETHOD, /* A: nargs; B: const name */
CAT_BYTECODE_OP_JUMP, /* A: absolute target */
CAT_BYTECODE_OP_JUMPIF, /* A: target; pop cond */
CAT_BYTECODE_OP_JUMPIFNOT, /* A: target; pop cond */
CAT_BYTECODE_OP_RETURN, /* pop -> return */
CAT_BYTECODE_OP_RETURN0, /* return the empty object */
CAT_BYTECODE_OP_POP, /* discard top */
CAT_BYTECODE_OP_PRINT, /* pop, print */
CAT_BYTECODE_OP_CLOSURE, /* A: index into protos[] */
CAT_BYTECODE_OP_GETUPVAL, /* A: upvalue slot */
CAT_BYTECODE_OP_CONCAT, /* string concat fast path */
CAT_BYTECODE_OP_DUP, /* duplicate top of stack */
CAT_BYTECODE_OP_GETIDX, /* pop idx, obj -> push obj[idx] */
CAT_BYTECODE_OP_SETIDX, /* pop val, idx, obj -> obj[idx]=val */
CAT_BYTECODE_OP_YIELD, /* suspend a generator, yielding top */
CAT_BYTECODE_OP_NEXT, /* pop it -> push next(it) or empty */
CAT_BYTECODE_OP_TRYSETUP, /* A: handler target addr */
CAT_BYTECODE_OP_TRYEND, /* pop the innermost handler */
CAT_BYTECODE_OP_RAISE, /* pop value -> raise it */
CAT_BYTECODE_OP_FORNEXT, /* A: exit target; pop it -> push next or jump */
CAT_BYTECODE_OP_PRINTEXPR, /* pop, print its repr (REPL echo) */
CAT_BYTECODE_OP_IS, /* pop iface, obj -> push obj is iface (structural) */
};
/* One instruction: opcode + three integer operands. */
typedef struct CAT_BYTECODE_Instr {
unsigned char op; /* a CAT_BYTECODE_Op */
int a;
int b;
int c;
int line; /* source line for diagnostics */
} CAT_BYTECODE_Instr;
struct CAT_BYTECODE_Function;
/* A constant pool entry: a value plus (for names) a string. */
typedef struct CAT_BYTECODE_Constant {
CAT_VALUE_Value *value;
} CAT_BYTECODE_Constant;
/* CAT_BYTECODE_Function -- a compiled chunk.
* `protos` holds nested function prototypes (for `function` literals). */
typedef struct CAT_BYTECODE_Function {
char *name;
CAT_BYTECODE_Instr *code;
int ncode, capcode;
CAT_BYTECODE_Constant *consts;
int nconsts, capconsts;
struct CAT_BYTECODE_Function **protos;
int nprotos, capprotos;
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);
/* Emit one instruction, returning its index. */
int cat_bytecode_emit(CAT_BYTECODE_Function *fn, unsigned char op,
int a, int b, int c, int line);
/* Add a constant; returns its index. */
int cat_bytecode_add_const(CAT_BYTECODE_Function *fn, CAT_VALUE_Value *v);
/* Add a nested prototype; returns its index. */
int cat_bytecode_add_proto(CAT_BYTECODE_Function *fn, CAT_BYTECODE_Function *p);
/* Human-readable disassembly (used by `--dump`). */
void cat_bytecode_dump(CAT_BYTECODE_Function *fn, FILE *out);
void cat_bytecode_function_free(CAT_BYTECODE_Function *fn);
#endif /* CAT_BYTECODE_H */