2024-08-19 15:44:39 -04:00
|
|
|
#ifndef VM_H
|
|
|
|
#define VM_H
|
|
|
|
|
|
|
|
#define STACK_MAX 1024
|
2024-08-20 13:18:35 -04:00
|
|
|
#define GC_THRESHOLD 256
|
2024-08-19 15:44:39 -04:00
|
|
|
|
|
|
|
// Primitive types for garbage collection
|
|
|
|
enum garbageData {
|
|
|
|
GARBAGE_INT,
|
|
|
|
GARBAGE_PAIR,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Holds type and value data for the virtual machine
|
|
|
|
struct garbageObject {
|
|
|
|
enum garbageData type;
|
|
|
|
struct garbageObject *next;
|
2024-08-19 16:20:25 -04:00
|
|
|
unsigned char mark;
|
2024-08-19 15:44:39 -04:00
|
|
|
|
|
|
|
union {
|
|
|
|
int value;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct garbageObject *head;
|
|
|
|
struct garbageObject *tail;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Virtual machine hold the stack to trace references
|
|
|
|
struct virtualMachine {
|
|
|
|
int stackSize;
|
2024-08-20 10:55:53 -04:00
|
|
|
unsigned refCount;
|
|
|
|
unsigned refMax;
|
2024-08-19 15:44:39 -04:00
|
|
|
struct garbageObject *head;
|
|
|
|
struct garbageObject *stack[STACK_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a new garbageObject
|
|
|
|
struct garbageObject *initGarbage(struct virtualMachine *vm,
|
|
|
|
enum garbageData type);
|
|
|
|
|
|
|
|
// Create a new VM
|
2024-08-20 13:28:11 -04:00
|
|
|
struct virtualMachine *initVM(void);
|
2024-08-19 15:44:39 -04:00
|
|
|
|
|
|
|
// Generic function to push data onto the VM stack
|
|
|
|
void push(struct virtualMachine *vm, struct garbageObject *value);
|
|
|
|
|
|
|
|
// Pop data from the VM stack
|
|
|
|
struct garbageObject *pop(struct virtualMachine *vm);
|
|
|
|
|
|
|
|
// Push a GARBAGE_INT onto the VM stack
|
|
|
|
void pushInt(struct virtualMachine *vm, int value);
|
|
|
|
|
|
|
|
// Push a GARBAGE_PAIR onto the VM stack
|
|
|
|
struct garbageObject *pushPair(struct virtualMachine *vm);
|
|
|
|
|
|
|
|
#endif /* VM_H */
|