impotent/vm.h
2025-08-31 16:22:38 +03:00

118 lines
1.7 KiB
C

#pragma once
#include<stdbool.h>
#include<stdint.h>
#include<stddef.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"table.h"
typedef enum {
L_GETGLOBAL,
L_SETGLOBAL,
L_SETINT16,
L_SETFLOAT,
L_SETSTR,
L_SETTABLE,
L_SETBOOL,
L_SETNIL,
L_SETFUNC,
L_ADD,
L_SUB,
L_MUL,
L_DIV,
L_IDIV,
L_MOD,
L_POW,
L_BOR,
L_BAND,
L_BXOR,
L_COND_EQ,
L_COND_NEQ,
L_JUMP,
L_JNOTCOND,
L_RET,
L_MOVE,
L_CALL,
L_ADVANCETEST,
L_SETFIELD,
L_GETFIELD,
L_SETINT32,
} LOp;
typedef union __attribute__((packed)) {
struct {
uint8_t opcode;
union {
uint8_t argb[3];
struct __attribute__((packed)) {
uint8_t a;
union {
uint16_t bc;
struct __attribute__((packed)) {
uint8_t b;
uint8_t c;
};
};
};
};
};
struct {
uint32_t hahalol;
} extension;
} LInst;
struct LUnit;
struct LVM;
typedef size_t(*LFuncCallback)(struct LVM*, void *ud, size_t argn, LValue *args);
typedef struct LFunc {
struct LUnit *unit;
bool is_native;
uint8_t upvalue_count;
LValue *upvalues;
union {
struct {
LFuncCallback native_func;
void *ud;
};
struct {
LInst *lua_instrs;
LTable *env;
};
};
} LFunc;
typedef struct LUnit {
uint8_t *abyss;
size_t func_count;
LFunc *funcs;
} LUnit;
#define i_header
#define T set_LValueU, uint64_t
#include"stc/hashset.h"
#undef i_header
typedef struct LVM {
size_t unit_count;
LUnit *units;
set_LValueU gc_objects;
} LVM;
size_t lvm_run(LVM *L, LFunc *func, size_t arg_count, LValue *regs);
void lvm_gc_add(LVM *L, LValue lvalue);
LFunc *lvm_func_from_native(LFuncCallback, void *ud);
static inline void lvm_reset_regs(LValue *regs) {
for(int i = 0; i < 256; i++) {
regs[i] = lvalue_from_nil();
}
}