pocketlang/src/vm.h

126 lines
3.4 KiB
C
Raw Normal View History

2021-02-07 15:40:00 +08:00
/*
* Copyright (c) 2021 Thakee Nathees
* Licensed under: MIT License
*/
#ifndef VM_H
#define VM_H
2021-02-08 02:30:29 +08:00
#include "miniscript.h"
2021-02-07 15:40:00 +08:00
#include "common.h"
#include "compiler.h"
#include "var.h"
// The maximum number of temporary object reference to protect them from being
// garbage collected.
#define MAX_TEMP_REFERENCE 8
2021-02-12 01:35:43 +08:00
// The maximum number of script cache can vm hold at once.
#define MAX_SCRIPT_CACHE 128
2021-02-09 16:21:10 +08:00
typedef enum {
2021-02-12 01:35:43 +08:00
#define OPCODE(name, _, __) OP_##name,
#include "opcodes.h"
#undef OPCODE
2021-02-09 16:21:10 +08:00
} Opcode;
2021-02-11 01:23:48 +08:00
typedef struct {
2021-02-12 01:35:43 +08:00
uint8_t* ip; //< Pointer to the next instruction byte code.
Function* fn; //< Function of the frame.
Var* rbp; //< Stack base pointer. (%rbp)
2021-02-11 01:23:48 +08:00
} CallFrame;
2021-02-08 02:30:29 +08:00
struct MSVM {
2021-02-07 15:40:00 +08:00
2021-02-12 01:35:43 +08:00
// The first object in the link list of all heap allocated objects.
Object* first;
size_t bytes_allocated;
// A stack of temporary object references to ensure that the object
// doesn't garbage collected.
Object* temp_reference[MAX_TEMP_REFERENCE];
int temp_reference_count;
2021-02-07 15:40:00 +08:00
2021-02-12 01:35:43 +08:00
// VM's configurations.
MSConfiguration config;
2021-02-07 15:40:00 +08:00
2021-02-12 01:35:43 +08:00
// Current compiler reference to mark it's heap allocated objects.
Compiler* compiler;
2021-02-07 15:40:00 +08:00
2021-02-16 02:51:00 +08:00
// Std scripts array.
Script* std_scripts[MAX_SCRIPT_CACHE];
// Std scripts count.
int std_count;
2021-02-12 01:35:43 +08:00
// Execution variables ////////////////////////////////////////////////////
2021-02-08 02:30:29 +08:00
2021-02-12 01:35:43 +08:00
// Compiled script cache.
Script* scripts[MAX_SCRIPT_CACHE];
2021-02-11 01:23:48 +08:00
2021-02-12 01:35:43 +08:00
// Number of script cache.
int script_count;
2021-02-11 01:23:48 +08:00
2021-02-12 01:35:43 +08:00
// The stack of the execution holding locals and temps. A heap allocated
// Will and grow as needed.
Var* stack;
2021-02-11 01:23:48 +08:00
2021-02-12 01:35:43 +08:00
// The stack pointer (%rsp) pointing to the stack top.
Var* sp;
2021-02-11 01:23:48 +08:00
2021-02-12 01:35:43 +08:00
// The stack base pointer of the current frame. It'll be updated before
// calling a native function.
Var* rbp;
2021-02-11 01:23:48 +08:00
2021-02-12 01:35:43 +08:00
// Size of the allocated stack.
int stack_size;
2021-02-11 01:23:48 +08:00
2021-02-12 01:35:43 +08:00
// Heap allocated array of call frames will grow as needed.
CallFrame* frames;
2021-02-11 01:23:48 +08:00
2021-02-12 01:35:43 +08:00
// Capacity of the frames array.
int frame_capacity;
// Number of frame entry in frames.
int frame_count;
// Runtime error initially NULL, heap allocated.
String* error;
2021-02-07 15:40:00 +08:00
};
// A realloc wrapper which handles memory allocations of the VM.
// - To allocate new memory pass NULL to parameter [memory] and 0 to
// parameter [old_size] on failure it'll return NULL.
// - To free an already allocated memory pass 0 to parameter [old_size]
// and it'll returns NULL.
// - The [old_size] parameter is required to keep track of the VM's
// allocations to trigger the garbage collections.
2021-02-11 01:23:48 +08:00
// If deallocating (free) using vmRealloc the old_size should be 0 as it's not
// going to track deallocated bytes, instead use garbage collector to do it.
2021-02-08 02:30:29 +08:00
void* vmRealloc(MSVM* self, void* memory, size_t old_size, size_t new_size);
2021-02-07 15:40:00 +08:00
2021-02-11 01:23:48 +08:00
// Initialize the vm and update the configuration. If config is NULL it'll use
// the default configuration.
void vmInit(MSVM* self, MSConfiguration* config);
2021-02-07 15:40:00 +08:00
// Push the object to temporary references stack.
2021-02-08 02:30:29 +08:00
void vmPushTempRef(MSVM* self, Object* obj);
2021-02-07 15:40:00 +08:00
// Pop the top most object from temporary reference stack.
2021-02-08 02:30:29 +08:00
void vmPopTempRef(MSVM* self);
2021-02-07 15:40:00 +08:00
2021-02-16 02:51:00 +08:00
// Add a std script to vm when initializing core.
void vmAddStdScript(MSVM* self, Script* script);
// Returns the std script with the name [name]. Note that the name shouldn't
// be start with "std:" but the actual name of the script. If not found
// returns NULL.
Script* vmGetStdScript(MSVM* self, const char* name);
2021-02-11 01:23:48 +08:00
// Runs the script and return result.
MSInterpretResult vmRunScript(MSVM* vm, Script* script);
2021-02-12 01:35:43 +08:00
#endif // VM_H