#include #include #include "gc.h" #include "vm.h" struct virtualMachine *initVM() { struct virtualMachine *vm = malloc(sizeof(struct virtualMachine)); vm->stackSize = 0; vm->refCount = 0; vm->refMax = GC_THRESHOLD; vm->head = NULL; return vm; } void push(struct virtualMachine *vm, struct garbageObject *value) { if (vm->stackSize >= STACK_MAX) { fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n"); return; } vm->stack[vm->stackSize++] = value; } struct garbageObject *pop(struct virtualMachine *vm) { if (vm->stackSize < 0) { fprintf(stderr, "ERROR: pop(): Stack size cannot be negative!"); return NULL; } return vm->stack[--vm->stackSize]; } struct garbageObject *initGarbage(struct virtualMachine *vm, enum garbageData type) { if (vm->refCount >= vm->refMax) { collect(vm); } struct garbageObject *obj = malloc(sizeof(struct garbageObject)); if (obj == NULL) { fprintf(stderr, "initGarbage(): Allocation failure!"); return obj; } obj->type = type; obj->mark = 0; obj->next = vm->head; vm->head = obj; vm->refCount++; return obj; } void pushInt(struct virtualMachine *vm, int value) { struct garbageObject *obj = initGarbage(vm, GARBAGE_INT); if (obj == NULL) { fprintf(stderr, "pushInt(): Unable to create GARBAGE_INT - out of memory?"); } obj->value = value; push(vm, obj); } struct garbageObject *pushPair(struct virtualMachine *vm) { struct garbageObject *obj = initGarbage(vm, GARBAGE_PAIR); if (obj == NULL) { fprintf(stderr, "pushPair(): Unable to create GARBAGE_PAIR - out of memory?"); } obj->tail = pop(vm); obj->head = pop(vm); push(vm, obj); return obj; }