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"
|
2023-12-15 18:30:20 +00:00
|
|
|
#include "tysy.h"
|
2023-12-17 18:10:17 +00:00
|
|
|
#include "sym.h"
|
2023-12-20 19:54:58 +00:00
|
|
|
#include "program.h"
|
2023-12-23 20:17:12 +00:00
|
|
|
#include "heap.h"
|
|
|
|
#include "locals.h"
|
2023-12-17 18:10:17 +00:00
|
|
|
|
|
|
|
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-23 20:17:12 +00:00
|
|
|
fun_t* fun;
|
|
|
|
locals_t* locals;
|
2023-12-20 19:54:58 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
size_t size;
|
|
|
|
size_t cap;
|
|
|
|
value_t** data;
|
|
|
|
} alloc;
|
|
|
|
|
2023-12-17 18:10:17 +00:00
|
|
|
} frame_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
sym_t* sym;
|
|
|
|
tysy_t* tysy;
|
2023-12-11 17:01:22 +00:00
|
|
|
err_t* err;
|
2023-12-17 18:10:17 +00:00
|
|
|
frame_t* top_frame;
|
2023-12-23 20:17:12 +00:00
|
|
|
heap_t heap;
|
|
|
|
locals_t locals;
|
2023-12-09 17:24:41 +00:00
|
|
|
} vm_t;
|
|
|
|
|
2023-12-17 18:10:17 +00:00
|
|
|
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);
|
|
|
|
|
2023-12-17 18:10:17 +00:00
|
|
|
void vm_frame_init(frame_t* frame);
|
|
|
|
void vm_frame_free(frame_t* frame);
|
|
|
|
|
2023-12-23 20:17:12 +00:00
|
|
|
void vm_frame_push(vm_t* vm, fun_t* fun);
|
2023-12-17 18:10:17 +00:00
|
|
|
int vm_frame_pop(vm_t* vm);
|
|
|
|
|
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);
|
|
|
|
|
2023-12-11 20:12:02 +00:00
|
|
|
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
|