2017-02-09 20:02:59 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "vm.h"
|
|
|
|
#include "value.h"
|
2017-02-09 23:50:47 +00:00
|
|
|
#include "ds.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
static const char OOM[] = "Out of memory";
|
2017-02-11 19:01:06 +00:00
|
|
|
static const char NO_UPVALUE[] = "No upvalue";
|
2017-02-09 20:02:59 +00:00
|
|
|
static const char EXPECTED_FUNCTION[] = "Expected function";
|
|
|
|
static const char VMS_EXPECTED_NUMBER_ROP[] = "Expected right operand to be number";
|
|
|
|
static const char VMS_EXPECTED_NUMBER_LOP[] = "Expected left operand to be number";
|
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
/* The stack frame data */
|
|
|
|
typedef struct StackFrame StackFrame;
|
|
|
|
struct StackFrame {
|
|
|
|
Value callee;
|
|
|
|
uint16_t size;
|
|
|
|
uint16_t prevSize;
|
|
|
|
uint16_t ret;
|
|
|
|
FuncEnv * env;
|
|
|
|
uint16_t * pc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The size of a StackFrame in units of Values. */
|
|
|
|
static size_t FRAME_SIZE() {
|
|
|
|
return ((sizeof(StackFrame) + sizeof(Value) - 1) / sizeof(Value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the stack frame pointer for a thread */
|
|
|
|
static StackFrame * ThreadFrame(Array * thread) {
|
|
|
|
return (StackFrame *)(thread->data + thread->count - FRAME_SIZE());
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:50:47 +00:00
|
|
|
/* The metadata header associated with an allocated block of memory */
|
|
|
|
#define GCHeader(mem) ((GCMemoryHeader *)(mem) - 1)
|
|
|
|
|
|
|
|
/* Memory header struct. Node of a linked list of memory blocks. */
|
|
|
|
typedef struct GCMemoryHeader GCMemoryHeader;
|
|
|
|
struct GCMemoryHeader {
|
|
|
|
GCMemoryHeader * next;
|
|
|
|
uint32_t color;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
static void VMMark(VM * vm, Value * x);
|
|
|
|
|
|
|
|
/* Helper to mark function environments */
|
|
|
|
static void VMMarkFuncEnv(VM * vm, FuncEnv * env) {
|
|
|
|
if (GCHeader(env)->color != vm->black) {
|
|
|
|
Value temp;
|
|
|
|
GCHeader(env)->color = vm->black;
|
|
|
|
if (env->thread) {
|
|
|
|
temp.type = TYPE_THREAD;
|
|
|
|
temp.data.array = env->thread;
|
|
|
|
VMMark(vm, &temp);
|
|
|
|
} else {
|
|
|
|
uint32_t count = env->stackOffset;
|
|
|
|
uint32_t i;
|
|
|
|
GCHeader(env->values)->color = vm->black;
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
VMMark(vm, env->values + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark allocated memory associated with a value. This is
|
|
|
|
* the main function for doing garbage collection. */
|
|
|
|
static void VMMark(VM * vm, Value * x) {
|
|
|
|
switch (x->type) {
|
|
|
|
case TYPE_NIL:
|
|
|
|
case TYPE_BOOLEAN:
|
|
|
|
case TYPE_NUMBER:
|
|
|
|
case TYPE_CFUNCTION:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_STRING:
|
|
|
|
case TYPE_SYMBOL:
|
|
|
|
GCHeader(VStringRaw(x->data.string))->color = vm->black;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_BYTEBUFFER:
|
|
|
|
GCHeader(x->data.buffer)->color = vm->black;
|
|
|
|
GCHeader(x->data.buffer->data)->color = vm->black;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
case TYPE_FORM:
|
|
|
|
if (GCHeader(x->data.array)->color != vm->black) {
|
|
|
|
uint32_t i, count;
|
|
|
|
count = x->data.array->count;
|
|
|
|
GCHeader(x->data.array)->color = vm->black;
|
|
|
|
GCHeader(x->data.array->data)->color = vm->black;
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
VMMark(vm, x->data.array->data + i);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_THREAD:
|
|
|
|
if (GCHeader(x->data.array)->color != vm->black) {
|
2017-02-12 15:27:18 +00:00
|
|
|
uint32_t i;
|
|
|
|
Array * thread = x->data.array;
|
|
|
|
StackFrame * frame = (StackFrame *)thread->data;
|
|
|
|
StackFrame * end = (StackFrame *)(thread->data + thread->count - FRAME_SIZE());
|
|
|
|
GCHeader(thread)->color = vm->black;
|
|
|
|
GCHeader(thread->data)->color = vm->black;
|
|
|
|
while (frame <= end) {
|
|
|
|
Value * stack = (Value *)frame + FRAME_SIZE();
|
|
|
|
VMMark(vm, &frame->callee);
|
|
|
|
if (frame->env)
|
|
|
|
VMMarkFuncEnv(vm, frame->env);
|
|
|
|
for (i = 0; i < frame->size; ++i) {
|
|
|
|
VMMark(vm, stack + i);
|
|
|
|
}
|
|
|
|
frame = (StackFrame *)(stack + frame->size);
|
|
|
|
}
|
2017-02-09 23:50:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_FUNCTION:
|
|
|
|
if (GCHeader(x->data.func)->color != vm->black) {
|
|
|
|
Func * f = x->data.func;
|
|
|
|
GCHeader(f)->color = vm->black;
|
|
|
|
VMMarkFuncEnv(vm, f->env);
|
|
|
|
{
|
|
|
|
Value temp;
|
|
|
|
temp.type = TYPE_FUNCDEF;
|
|
|
|
temp.data.funcdef = x->data.funcdef;
|
|
|
|
VMMark(vm, &temp);
|
|
|
|
if (f->parent) {
|
|
|
|
temp.type = TYPE_FUNCTION;
|
|
|
|
temp.data.func = f->parent;
|
|
|
|
VMMark(vm, &temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_DICTIONARY:
|
|
|
|
if (GCHeader(x->data.dict)->color != vm->black) {
|
|
|
|
DictionaryIterator iter;
|
|
|
|
DictBucket * bucket;
|
|
|
|
GCHeader(x->data.dict)->color = vm->black;
|
|
|
|
GCHeader(x->data.dict->buckets)->color = vm->black;
|
|
|
|
DictIterate(x->data.dict, &iter);
|
|
|
|
while (DictIterateNext(&iter, &bucket)) {
|
|
|
|
GCHeader(bucket)->color = vm->black;
|
|
|
|
VMMark(vm, &bucket->key);
|
|
|
|
VMMark(vm, &bucket->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_FUNCDEF:
|
|
|
|
if (GCHeader(x->data.funcdef)->color != vm->black) {
|
|
|
|
GCHeader(x->data.funcdef->byteCode)->color = vm->black;
|
|
|
|
uint32_t count, i;
|
|
|
|
count = x->data.funcdef->literalsLen;
|
|
|
|
if (x->data.funcdef->literals) {
|
|
|
|
GCHeader(x->data.funcdef->literals)->color = vm->black;
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
VMMark(vm, x->data.funcdef->literals + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_FUNCENV:
|
|
|
|
VMMarkFuncEnv(vm, x->data.funcenv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate over all allocated memory, and free memory that is not
|
|
|
|
* marked as reachable. Flip the gc color flag for next sweep. */
|
|
|
|
static void VMSweep(VM * vm) {
|
|
|
|
GCMemoryHeader * previous = NULL;
|
|
|
|
GCMemoryHeader * current = vm->blocks;
|
|
|
|
while (current) {
|
|
|
|
if (current->color != vm->black) {
|
|
|
|
if (previous) {
|
|
|
|
previous->next = current->next;
|
|
|
|
} else {
|
|
|
|
vm->blocks = current->next;
|
|
|
|
}
|
|
|
|
free(current);
|
|
|
|
} else {
|
|
|
|
previous = current;
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
/* Rotate flag */
|
|
|
|
vm->black = !vm->black;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare a memory block */
|
|
|
|
static void * VMAllocPrepare(VM * vm, char * rawBlock, uint32_t size) {
|
|
|
|
GCMemoryHeader * mdata;
|
|
|
|
if (rawBlock == NULL) {
|
|
|
|
VMCrash(vm, OOM);
|
|
|
|
}
|
|
|
|
vm->nextCollection += size;
|
|
|
|
mdata = (GCMemoryHeader *) rawBlock;
|
|
|
|
mdata->next = vm->blocks;
|
|
|
|
vm->blocks = mdata;
|
|
|
|
mdata->color = !vm->black;
|
|
|
|
return rawBlock + sizeof(GCMemoryHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate some memory that is tracked for garbage collection */
|
|
|
|
void * VMAlloc(VM * vm, uint32_t size) {
|
|
|
|
uint32_t totalSize = size + sizeof(GCMemoryHeader);
|
|
|
|
return VMAllocPrepare(vm, malloc(totalSize), totalSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate some zeroed memory that is tracked for garbage collection */
|
|
|
|
void * VMZalloc(VM * vm, uint32_t size) {
|
2017-02-12 15:27:18 +00:00
|
|
|
uint32_t totalSize = size + sizeof(GCMemoryHeader);
|
2017-02-09 23:50:47 +00:00
|
|
|
return VMAllocPrepare(vm, calloc(1, totalSize), totalSize);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run garbage collection */
|
|
|
|
void VMCollect(VM * vm) {
|
2017-02-10 04:28:11 +00:00
|
|
|
if (vm->lock > 0) return;
|
2017-02-09 23:50:47 +00:00
|
|
|
Value thread;
|
|
|
|
thread.type = TYPE_THREAD;
|
|
|
|
thread.data.array = vm->thread;
|
|
|
|
VMMark(vm, &thread);
|
|
|
|
VMMark(vm, &vm->tempRoot);
|
|
|
|
VMSweep(vm);
|
|
|
|
vm->nextCollection = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run garbage collection if needed */
|
|
|
|
void VMMaybeCollect(VM * vm) {
|
2017-02-09 23:50:47 +00:00
|
|
|
if (vm->nextCollection >= vm->memoryInterval) {
|
2017-02-09 20:02:59 +00:00
|
|
|
VMCollect(vm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a stack frame onto a thread */
|
|
|
|
static void VMThreadPush(VM * vm, Array * thread, Value callee, uint32_t size) {
|
|
|
|
uint16_t oldSize;
|
|
|
|
uint32_t nextCount, i;
|
2017-02-12 15:27:18 +00:00
|
|
|
StackFrame * frame;
|
2017-02-09 20:02:59 +00:00
|
|
|
if (thread->count) {
|
2017-02-12 15:27:18 +00:00
|
|
|
frame = ThreadFrame(thread);
|
|
|
|
oldSize = frame->size;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
|
|
|
oldSize = 0;
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
nextCount = thread->count + oldSize + FRAME_SIZE();
|
2017-02-09 23:50:47 +00:00
|
|
|
ArrayEnsure(vm, thread, nextCount + size);
|
2017-02-12 15:27:18 +00:00
|
|
|
thread->count = nextCount;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Ensure values start out as nil so as to not confuse
|
|
|
|
* the garabage collector */
|
2017-02-12 15:27:18 +00:00
|
|
|
for (i = nextCount; i < nextCount + size; ++i)
|
2017-02-09 20:02:59 +00:00
|
|
|
thread->data[i].type = TYPE_NIL;
|
2017-02-12 15:27:18 +00:00
|
|
|
frame = ThreadFrame(thread);
|
|
|
|
/* Set up the new stack frame */
|
|
|
|
frame->prevSize = oldSize;
|
|
|
|
frame->size = size;
|
|
|
|
frame->env = NULL;
|
|
|
|
frame->callee = callee;
|
|
|
|
vm->base = thread->data + thread->count;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the current function stack to the current closure
|
|
|
|
environment */
|
|
|
|
static void VMThreadSplitStack(VM * vm, Array * thread) {
|
2017-02-12 15:27:18 +00:00
|
|
|
StackFrame * frame = ThreadFrame(thread);
|
|
|
|
FuncEnv * env = frame->env;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Check for closures */
|
|
|
|
if (env) {
|
2017-02-12 15:27:18 +00:00
|
|
|
uint32_t size = frame->size;
|
2017-02-09 20:02:59 +00:00
|
|
|
env->thread = NULL;
|
|
|
|
env->stackOffset = size;
|
2017-02-09 23:50:47 +00:00
|
|
|
env->values = VMAlloc(vm, sizeof(Value) * size);
|
2017-02-12 15:27:18 +00:00
|
|
|
memcpy(env->values, thread->data + thread->count, size * sizeof(Value));
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop the top-most stack frame from stack */
|
|
|
|
static void VMThreadPop(VM * vm, Array * thread) {
|
2017-02-12 15:27:18 +00:00
|
|
|
StackFrame * frame = ThreadFrame(thread);
|
|
|
|
uint32_t delta = FRAME_SIZE() + frame->prevSize;
|
2017-02-09 20:02:59 +00:00
|
|
|
if (thread->count) {
|
|
|
|
VMThreadSplitStack(vm, thread);
|
|
|
|
} else {
|
|
|
|
VMError(vm, "Nothing to pop from stack.");
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
thread->count -= delta;
|
|
|
|
vm->base -= delta;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an upvalue */
|
|
|
|
static Value * GetUpValue(VM * vm, Func * fn, uint16_t level, uint16_t index) {
|
|
|
|
FuncEnv * env;
|
|
|
|
Value * stack;
|
2017-02-11 19:01:06 +00:00
|
|
|
if (!level) {
|
|
|
|
return vm->base + index;
|
|
|
|
}
|
|
|
|
while (fn && --level)
|
2017-02-09 20:02:59 +00:00
|
|
|
fn = fn->parent;
|
|
|
|
VMAssert(vm, fn, NO_UPVALUE);
|
|
|
|
env = fn->env;
|
|
|
|
if (env->thread)
|
|
|
|
stack = env->thread->data + env->stackOffset;
|
|
|
|
else
|
|
|
|
stack = env->values;
|
|
|
|
return stack + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a constant */
|
2017-02-12 15:27:18 +00:00
|
|
|
static Value LoadConstant(VM * vm, Func * fn, uint16_t index) {
|
2017-02-09 20:02:59 +00:00
|
|
|
if (index > fn->def->literalsLen) {
|
|
|
|
VMError(vm, NO_UPVALUE);
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
return fn->def->literals[index];
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Boolean truth definition */
|
|
|
|
static int truthy(Value v) {
|
|
|
|
return v.type != TYPE_NIL && !(v.type == TYPE_BOOLEAN && !v.data.boolean);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return from the vm */
|
|
|
|
static void VMReturn(VM * vm, Value ret) {
|
2017-02-12 15:27:18 +00:00
|
|
|
Array * thread = vm->thread;
|
|
|
|
StackFrame * frame = ThreadFrame(thread);
|
|
|
|
VMThreadPop(vm, thread);
|
|
|
|
if (thread->count == 0) {
|
2017-02-09 20:02:59 +00:00
|
|
|
VMExit(vm, ret);
|
|
|
|
}
|
2017-02-12 15:49:45 +00:00
|
|
|
frame = ThreadFrame(thread);
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->pc = frame->pc;
|
|
|
|
vm->base[frame->ret] = ret;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Implementation of the opcode for function calls */
|
|
|
|
static void VMCallOp(VM * vm) {
|
2017-02-12 15:27:18 +00:00
|
|
|
Array * thread = vm->thread;
|
|
|
|
StackFrame * frame = ThreadFrame(thread);
|
|
|
|
Value callee = vm->base[vm->pc[1]];
|
|
|
|
uint32_t arity = vm->pc[3];
|
|
|
|
uint32_t oldCount = thread->count;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t i;
|
2017-02-12 15:27:18 +00:00
|
|
|
Value * oldBase;
|
|
|
|
frame->pc = vm->pc + 4 + arity;
|
|
|
|
frame->ret = vm->pc[2];
|
|
|
|
if (callee.type == TYPE_FUNCTION) {
|
|
|
|
Func * fn = callee.data.func;
|
|
|
|
VMThreadPush(vm, thread, callee, fn->def->locals);
|
|
|
|
} else if (callee.type == TYPE_CFUNCTION) {
|
|
|
|
VMThreadPush(vm, thread, callee, arity);
|
|
|
|
} else {
|
|
|
|
VMError(vm, EXPECTED_FUNCTION);
|
|
|
|
}
|
|
|
|
oldBase = thread->data + oldCount;
|
2017-02-09 20:02:59 +00:00
|
|
|
if (callee.type == TYPE_CFUNCTION) {
|
|
|
|
for (i = 0; i < arity; ++i)
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->base[i] = oldBase[vm->pc[4 + i]];
|
2017-02-10 04:28:11 +00:00
|
|
|
++vm->lock;
|
2017-02-09 20:02:59 +00:00
|
|
|
VMReturn(vm, callee.data.cfunction(vm));
|
2017-02-10 04:28:11 +00:00
|
|
|
--vm->lock;
|
2017-02-12 15:27:18 +00:00
|
|
|
} else {
|
2017-02-09 20:02:59 +00:00
|
|
|
Func * f = callee.data.func;
|
2017-02-12 15:27:18 +00:00
|
|
|
uint32_t locals = f->def->locals;
|
2017-02-09 20:02:59 +00:00
|
|
|
for (i = 0; i < arity; ++i)
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->base[i] = oldBase[vm->pc[4 + i]];
|
|
|
|
for (; i < locals; ++i)
|
|
|
|
vm->base[i].type = TYPE_NIL;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc = f->def->byteCode;
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
VMMaybeCollect(vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Implementation of the opcode for tail calls */
|
|
|
|
static void VMTailCallOp(VM * vm) {
|
|
|
|
Array * thread = vm->thread;
|
2017-02-12 15:27:18 +00:00
|
|
|
StackFrame * frame = ThreadFrame(thread);
|
|
|
|
Value callee = vm->base[vm->pc[1]];
|
|
|
|
uint32_t arity = vm->pc[2];
|
|
|
|
uint16_t newFrameSize, currentFrameSize;
|
2017-02-09 20:02:59 +00:00
|
|
|
uint32_t i;
|
|
|
|
/* Check for closures */
|
2017-02-12 15:27:18 +00:00
|
|
|
VMThreadSplitStack(vm, thread);
|
2017-02-09 20:02:59 +00:00
|
|
|
if (callee.type == TYPE_CFUNCTION) {
|
|
|
|
newFrameSize = arity;
|
|
|
|
} else if (callee.type == TYPE_FUNCTION) {
|
|
|
|
Func * f = callee.data.func;
|
|
|
|
newFrameSize = f->def->locals;
|
|
|
|
} else {
|
|
|
|
VMError(vm, EXPECTED_FUNCTION);
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Ensure stack has enough space for copies of arguments */
|
|
|
|
currentFrameSize = frame->size;
|
|
|
|
ArrayEnsure(vm, thread, thread->count + currentFrameSize + arity);
|
|
|
|
frame = ThreadFrame(thread);
|
|
|
|
vm->base = thread->data + thread->count;
|
|
|
|
/* Copy the arguments into the extra space */
|
2017-02-09 20:02:59 +00:00
|
|
|
for (i = 0; i < arity; ++i) {
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->base[currentFrameSize + i] = vm->base[vm->pc[3 + i]];
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
/* Copy the end of the stack to the parameter position */
|
2017-02-12 15:27:18 +00:00
|
|
|
memcpy(vm->base, vm->base + currentFrameSize, arity * sizeof(Value));
|
|
|
|
/* nil the non argument part of the stack for gc */
|
2017-02-09 20:02:59 +00:00
|
|
|
for (i = arity; i < newFrameSize; ++i) {
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->base[i].type = TYPE_NIL;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
/* Update the stack frame */
|
|
|
|
frame->size = newFrameSize;
|
|
|
|
frame->callee = callee;
|
|
|
|
frame->env = NULL;
|
2017-02-09 20:02:59 +00:00
|
|
|
if (callee.type == TYPE_CFUNCTION) {
|
2017-02-10 04:28:11 +00:00
|
|
|
++vm->lock;
|
2017-02-09 20:02:59 +00:00
|
|
|
VMReturn(vm, callee.data.cfunction(vm));
|
2017-02-10 04:28:11 +00:00
|
|
|
--vm->lock;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
|
|
|
Func * f = callee.data.func;
|
|
|
|
vm->pc = f->def->byteCode;
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
VMMaybeCollect(vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Instantiate a closure */
|
2017-02-12 15:27:18 +00:00
|
|
|
static Value VMMakeClosure(VM * vm, uint16_t literal) {
|
|
|
|
Array * thread = vm->thread;
|
|
|
|
StackFrame * frame = ThreadFrame(thread);
|
|
|
|
if (frame->callee.type != TYPE_FUNCTION) {
|
2017-02-09 20:02:59 +00:00
|
|
|
VMError(vm, EXPECTED_FUNCTION);
|
|
|
|
} else {
|
2017-02-12 15:27:18 +00:00
|
|
|
Value constant, ret;
|
2017-02-09 20:02:59 +00:00
|
|
|
Func * fn, * current;
|
2017-02-12 15:27:18 +00:00
|
|
|
FuncEnv * env = frame->env;
|
2017-02-09 20:02:59 +00:00
|
|
|
if (!env) {
|
2017-02-09 23:50:47 +00:00
|
|
|
env = VMAlloc(vm, sizeof(FuncEnv));
|
2017-02-09 20:02:59 +00:00
|
|
|
env->thread = thread;
|
|
|
|
env->stackOffset = thread->count;
|
|
|
|
env->values = NULL;
|
2017-02-12 15:27:18 +00:00
|
|
|
frame->env = env;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
current = frame->callee.data.func;
|
2017-02-09 20:02:59 +00:00
|
|
|
constant = LoadConstant(vm, current, literal);
|
2017-02-12 15:27:18 +00:00
|
|
|
if (constant.type != TYPE_FUNCDEF) {
|
2017-02-09 20:02:59 +00:00
|
|
|
VMError(vm, EXPECTED_FUNCTION);
|
|
|
|
}
|
2017-02-09 23:50:47 +00:00
|
|
|
fn = VMAlloc(vm, sizeof(Func));
|
2017-02-12 15:27:18 +00:00
|
|
|
fn->def = constant.data.funcdef;
|
2017-02-09 20:02:59 +00:00
|
|
|
fn->parent = current;
|
|
|
|
fn->env = env;
|
2017-02-12 15:27:18 +00:00
|
|
|
ret.type = TYPE_FUNCTION;
|
|
|
|
ret.data.func = fn;
|
|
|
|
return ret;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start running the VM */
|
|
|
|
int VMStart(VM * vm) {
|
|
|
|
|
|
|
|
/* Set jmp_buf to jump back to for return. */
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
if ((n = setjmp(vm->jump))) {
|
2017-02-11 19:01:06 +00:00
|
|
|
vm->lock = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Good return */
|
|
|
|
if (n == 1) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2017-02-09 23:50:47 +00:00
|
|
|
/* Error or crash. Handling TODO. */
|
2017-02-09 20:02:59 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
uint16_t opcode = *vm->pc;
|
|
|
|
|
|
|
|
switch (opcode) {
|
2017-02-12 15:27:18 +00:00
|
|
|
Value temp, v1, v2;
|
|
|
|
|
|
|
|
#define DO_BINARY_MATH(op) \
|
|
|
|
v1 = vm->base[vm->pc[2]]; \
|
|
|
|
v2 = vm->base[vm->pc[3]]; \
|
|
|
|
VMAssert(vm, v1.type == TYPE_NUMBER, VMS_EXPECTED_NUMBER_LOP); \
|
|
|
|
VMAssert(vm, v2.type == TYPE_NUMBER, VMS_EXPECTED_NUMBER_ROP); \
|
|
|
|
temp.type = TYPE_NUMBER; \
|
|
|
|
temp.data.number = v1.data.number op v2.data.number; \
|
|
|
|
vm->base[vm->pc[1]] = temp; \
|
|
|
|
vm->pc += 4; \
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_ADD: /* Addition */
|
|
|
|
DO_BINARY_MATH(+)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
case VM_OP_SUB: /* Subtraction */
|
2017-02-12 15:27:18 +00:00
|
|
|
DO_BINARY_MATH(-)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
case VM_OP_MUL: /* Multiplication */
|
2017-02-12 15:27:18 +00:00
|
|
|
DO_BINARY_MATH(*)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
case VM_OP_DIV: /* Division */
|
2017-02-12 15:27:18 +00:00
|
|
|
DO_BINARY_MATH(/)
|
|
|
|
|
|
|
|
#undef DO_BINARY_MATH
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-12 15:27:18 +00:00
|
|
|
case VM_OP_NOT: /* Boolean unary (Boolean not) */
|
|
|
|
temp.type = TYPE_BOOLEAN;
|
|
|
|
temp.data.boolean = !truthy(vm->base[vm->pc[2]]);
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_LD0: /* Load 0 */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NUMBER;
|
|
|
|
temp.data.number = 0;
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_LD1: /* Load 1 */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NUMBER;
|
|
|
|
temp.data.number = 1;
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_FLS: /* Load False */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_BOOLEAN;
|
|
|
|
temp.data.boolean = 0;
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_TRU: /* Load True */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_BOOLEAN;
|
|
|
|
temp.data.boolean = 1;
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_NIL: /* Load Nil */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NIL;
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_I16: /* Load Small Integer */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NUMBER;
|
|
|
|
temp.data.number = ((int16_t *)(vm->pc))[2];
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_UPV: /* Load Up Value */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp = ThreadFrame(vm->thread)->callee;
|
|
|
|
VMAssert(vm, temp.type == TYPE_FUNCTION, EXPECTED_FUNCTION);
|
|
|
|
vm->base[vm->pc[1]] = *GetUpValue(vm, temp.data.func, vm->pc[2], vm->pc[3]);
|
|
|
|
vm->pc += 4;
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_JIF: /* Jump If */
|
2017-02-12 15:27:18 +00:00
|
|
|
if (truthy(vm->base[vm->pc[1]])) {
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 4;
|
|
|
|
} else {
|
|
|
|
vm->pc += *((int32_t *)(vm->pc + 2));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_JMP: /* Jump */
|
|
|
|
vm->pc += *((int32_t *)(vm->pc + 1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_CAL: /* Call */
|
|
|
|
VMCallOp(vm);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_RET: /* Return */
|
2017-02-12 15:27:18 +00:00
|
|
|
VMReturn(vm, vm->base[vm->pc[1]]);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_SUV: /* Set Up Value */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp = ThreadFrame(vm->thread)->callee;
|
|
|
|
VMAssert(vm, temp.type == TYPE_FUNCTION, EXPECTED_FUNCTION);
|
|
|
|
*GetUpValue(vm, temp.data.func, vm->pc[2], vm->pc[3]) = vm->base[vm->pc[1]];
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_CST: /* Load constant value */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp = ThreadFrame(vm->thread)->callee;
|
|
|
|
VMAssert(vm, temp.type == TYPE_FUNCTION, EXPECTED_FUNCTION);
|
|
|
|
vm->base[vm->pc[1]] = LoadConstant(vm, temp.data.func, vm->pc[2]);
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_I32: /* Load 32 bit integer */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NUMBER;
|
|
|
|
temp.data.number = *((int32_t *)(vm->pc + 2));
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_F64: /* Load 64 bit float */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NUMBER;
|
|
|
|
temp.data.number = (Number) *((double *)(vm->pc + 2));
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_MOV: /* Move Values */
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->base[vm->pc[1]] = vm->base[vm->pc[2]];
|
|
|
|
vm->pc += 3;
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_CLN: /* Create closure from constant FuncDef */
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->base[vm->pc[1]] = VMMakeClosure(vm, vm->pc[2]);
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_EQL: /* Equality */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_BOOLEAN;
|
|
|
|
temp.data.boolean = ValueEqual(vm->base[vm->pc[2]], vm->base[vm->pc[3]]);
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_LTN: /* Less Than */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_BOOLEAN;
|
|
|
|
temp.data.boolean = (ValueCompare(vm->base[vm->pc[2]], vm->base[vm->pc[3]]) == -1);
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_LTE: /* Less Than or Equal to */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_BOOLEAN;
|
|
|
|
temp.data.boolean = (ValueEqual(vm->base[vm->pc[2]], vm->base[vm->pc[3]]) != 1);
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_ARR: /* Array literal */
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t arrayLen = vm->pc[2];
|
2017-02-09 23:50:47 +00:00
|
|
|
Array * array = ArrayNew(vm, arrayLen);
|
2017-02-09 20:02:59 +00:00
|
|
|
array->count = arrayLen;
|
|
|
|
for (i = 0; i < arrayLen; ++i)
|
2017-02-12 15:27:18 +00:00
|
|
|
array->data[i] = vm->base[vm->pc[3 + i]];
|
|
|
|
temp.type = TYPE_ARRAY;
|
|
|
|
temp.data.array = array;
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 3 + arrayLen;
|
|
|
|
VMMaybeCollect(vm);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_DIC: /* Dictionary literal */
|
|
|
|
{
|
|
|
|
uint32_t i = 3;
|
|
|
|
uint32_t kvs = vm->pc[2];
|
2017-02-09 23:50:47 +00:00
|
|
|
Dictionary * dict = DictNew(vm, kvs);
|
2017-02-11 19:01:06 +00:00
|
|
|
kvs = kvs + 3;
|
2017-02-09 20:02:59 +00:00
|
|
|
while (i < kvs) {
|
2017-02-12 15:27:18 +00:00
|
|
|
v1 = vm->base[vm->pc[i++]];
|
|
|
|
v2 = vm->base[vm->pc[i++]];
|
|
|
|
DictPut(vm, dict, v1, v2);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_DICTIONARY;
|
|
|
|
temp.data.dict = dict;
|
|
|
|
vm->base[vm->pc[1]] = temp;
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += kvs;
|
|
|
|
VMMaybeCollect(vm);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VM_OP_TCL: /* Tail call */
|
|
|
|
VMTailCallOp(vm);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Macro for generating some math operators */
|
|
|
|
#define DO_MULTI_MATH(op, start) { \
|
2017-02-12 15:27:18 +00:00
|
|
|
uint16_t count = vm->pc[2]; \
|
2017-02-09 20:02:59 +00:00
|
|
|
uint16_t i; \
|
|
|
|
Number accum = start; \
|
|
|
|
for (i = 0; i < count; ++i) { \
|
2017-02-12 15:27:18 +00:00
|
|
|
v1 = vm->base[vm->pc[3 + i]]; \
|
|
|
|
VMAssert(vm, v1.type == TYPE_NUMBER, "Expected number"); \
|
|
|
|
accum = accum op v1.data.number; \
|
2017-02-09 20:02:59 +00:00
|
|
|
} \
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NUMBER; \
|
|
|
|
temp.data.number = accum; \
|
|
|
|
vm->base[vm->pc[1]] = temp; \
|
2017-02-09 20:02:59 +00:00
|
|
|
vm->pc += 3 + count; \
|
2017-02-12 15:27:18 +00:00
|
|
|
break; \
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Vectorized math */
|
|
|
|
case VM_OP_ADM:
|
|
|
|
DO_MULTI_MATH(+, 0)
|
|
|
|
|
|
|
|
case VM_OP_SBM:
|
|
|
|
DO_MULTI_MATH(-, 0)
|
|
|
|
|
|
|
|
case VM_OP_MUM:
|
|
|
|
DO_MULTI_MATH(*, 1)
|
|
|
|
|
|
|
|
case VM_OP_DVM:
|
|
|
|
DO_MULTI_MATH(/, 1)
|
|
|
|
|
|
|
|
#undef DO_MULTI_MATH
|
|
|
|
|
|
|
|
case VM_OP_RTN: /* Return nil */
|
2017-02-12 15:27:18 +00:00
|
|
|
temp.type = TYPE_NIL;
|
|
|
|
VMReturn(vm, temp);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
VMError(vm, "Unknown opcode");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 04:28:11 +00:00
|
|
|
/* Get an argument from the stack */
|
|
|
|
Value VMGetArg(VM * vm, uint16_t index) {
|
2017-02-12 15:27:18 +00:00
|
|
|
uint16_t frameSize = ThreadFrame(vm->thread)->size;
|
2017-02-10 04:28:11 +00:00
|
|
|
VMAssert(vm, frameSize > index, "Cannot get arg out of stack bounds");
|
2017-02-12 15:27:18 +00:00
|
|
|
return vm->base[index];
|
2017-02-10 04:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a value on the stack */
|
|
|
|
void VMSetArg(VM * vm, uint16_t index, Value x) {
|
2017-02-12 15:27:18 +00:00
|
|
|
uint16_t frameSize = ThreadFrame(vm->thread)->size;
|
2017-02-10 04:28:11 +00:00
|
|
|
VMAssert(vm, frameSize > index, "Cannot set arg out of stack bounds");
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->base[index] = x;
|
2017-02-10 04:28:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:01:06 +00:00
|
|
|
/* Get the size of the VMStack */
|
|
|
|
uint16_t VMCountArgs(VM * vm) {
|
2017-02-12 15:27:18 +00:00
|
|
|
return ThreadFrame(vm->thread)->size;
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Initialize the VM */
|
|
|
|
void VMInit(VM * vm) {
|
|
|
|
vm->tempRoot.type = TYPE_NIL;
|
|
|
|
vm->base = NULL;
|
|
|
|
vm->pc = NULL;
|
|
|
|
vm->error = NULL;
|
2017-02-09 23:50:47 +00:00
|
|
|
/* Garbage collection */
|
|
|
|
vm->blocks = NULL;
|
|
|
|
vm->nextCollection = 0;
|
|
|
|
vm->memoryInterval = 1024 * 256;
|
|
|
|
vm->black = 0;
|
2017-02-10 04:28:11 +00:00
|
|
|
vm->lock = 0;
|
|
|
|
/* Create new thread */
|
|
|
|
vm->thread = ArrayNew(vm, 20);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load a function into the VM. The function will be called with
|
|
|
|
* no arguments when run */
|
|
|
|
void VMLoad(VM * vm, Func * func) {
|
|
|
|
Value callee;
|
|
|
|
callee.type = TYPE_FUNCTION;
|
|
|
|
callee.data.func = func;
|
2017-02-12 15:27:18 +00:00
|
|
|
vm->thread = ArrayNew(vm, 100);
|
2017-02-09 20:02:59 +00:00
|
|
|
VMThreadPush(vm, vm->thread, callee, func->def->locals);
|
|
|
|
vm->pc = func->def->byteCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear all memory associated with the VM */
|
|
|
|
void VMDeinit(VM * vm) {
|
2017-02-09 23:50:47 +00:00
|
|
|
GCMemoryHeader * current = vm->blocks;
|
|
|
|
while (current) {
|
|
|
|
GCMemoryHeader * next = current->next;
|
|
|
|
free(current);
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
vm->blocks = NULL;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|