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-08 02:30:29 +08:00
|
|
|
struct MSVM {
|
2021-02-07 15:40:00 +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-08 02:30:29 +08:00
|
|
|
// VM's configurations.
|
|
|
|
MSConfiguration config;
|
|
|
|
|
2021-02-07 15:40:00 +08:00
|
|
|
// current compiler reference to mark it's heap allocated objects.
|
|
|
|
Compiler* compiler;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-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
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
#endif // VM_H
|