- VM refactor: resumable CAT_VM_Frame (ip/sp/locals/handlers) - yield/next/generators: calling a yield-function returns a generator - for..in over any iterator, ends on explicit exhaustion status - raise/try/except: structured error values, nearest-handler unwinding - slots: C-declared member layout (offsetof), PyTypeObject/tp_members style - File now exposes path/mode/closed via slots - examples: generators.cat, errors.cat, slots.cat; 40/40 tests
118 lines
4.5 KiB
C
118 lines
4.5 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_NIL, /* push nil */
|
|
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 nil */
|
|
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 nil */
|
|
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 */
|
|
};
|
|
|
|
/* 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 */
|
|
} 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 */
|