roza/lib/vm.h

73 lines
1.4 KiB
C
Raw Normal View History

2023-12-09 17:24:41 +00:00
#ifndef RZ_VM_H
#define RZ_VM_H
#include "commons.h"
#include "opcodes.h"
#include "mod.h"
2023-12-11 17:01:22 +00:00
#include "err.h"
#include "tysy.h"
#include "sym.h"
2023-12-20 19:54:58 +00:00
#include "program.h"
2023-12-09 17:24:41 +00:00
typedef struct {
int id;
param_t addr;
} local_t;
typedef struct stack_frame{
struct frame* prev;
2023-12-20 19:54:58 +00:00
program_t* program;
value_t* stack[RZ_STACK_LIMIT];
int allocated[RZ_STACK_LIMIT];
2023-12-09 17:24:41 +00:00
size_t pc;
size_t bp;
size_t sp;
2023-12-11 17:01:22 +00:00
struct {
size_t size;
size_t cap;
local_t** data;
} locals;
2023-12-20 19:54:58 +00:00
struct {
size_t size;
size_t cap;
value_t** data;
} alloc;
} frame_t;
typedef struct {
sym_t* sym;
tysy_t* tysy;
2023-12-11 17:01:22 +00:00
err_t* err;
frame_t* top_frame;
2023-12-09 17:24:41 +00:00
} vm_t;
void vm_init(vm_t* vm, sym_t* sym, tysy_t* tysy, err_t* err);
2023-12-09 17:24:41 +00:00
void vm_free(vm_t* vm);
void vm_frame_init(frame_t* frame);
void vm_frame_free(frame_t* frame);
2023-12-20 19:54:58 +00:00
void vm_frame_push(vm_t* vm, program_t* program);
int vm_frame_pop(vm_t* vm);
local_t* vm_try_local_load(vm_t* vm, int id);
void vm_local_store(vm_t* vm, int id, param_t addr);
2023-12-20 19:54:58 +00:00
void vm_push_new(vm_t* vm, value_t* value);
void vm_push(vm_t* vm, value_t* value);
value_t* vm_pop(vm_t* vm);
2023-12-11 17:01:22 +00:00
2023-12-09 17:24:41 +00:00
size_t vm_stack_str(vm_t* vm, char* buffer, size_t size);
int vm_exec_mod(vm_t* vm, mod_t* mod);
2023-12-20 19:54:58 +00:00
int vm_exec_instr(vm_t* vm, Opcode op, param_t param);
2023-12-15 20:32:17 +00:00
void vm_ensure_type(vm_t* vm, value_t* value, TypeKind want);
2023-12-09 17:24:41 +00:00
2023-12-20 19:54:58 +00:00
void vm_push_alloc(frame_t* frame, value_t* value);
void vm_clear_alloc(frame_t* frame);
2023-12-09 17:24:41 +00:00
#endif