2017-03-07 20:29:40 +00:00
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
#include "vm.h"
|
2017-03-01 01:20:29 +00:00
|
|
|
#include "util.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
#include "value.h"
|
2017-02-09 23:50:47 +00:00
|
|
|
#include "ds.h"
|
2017-02-23 22:21:13 +00:00
|
|
|
#include "gc.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
static const char GST_NO_UPVALUE[] = "no upvalue";
|
|
|
|
static const char GST_EXPECTED_FUNCTION[] = "expected function";
|
|
|
|
static const char GST_EXPECTED_NUMBER_ROP[] = "expected right operand to be number";
|
|
|
|
static const char GST_EXPECTED_NUMBER_LOP[] = "expected left operand to be number";
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
/* Get a literal */
|
|
|
|
static GstValue gst_vm_literal(Gst *vm, GstFunction *fn, uint16_t index) {
|
2017-02-09 20:02:59 +00:00
|
|
|
if (index > fn->def->literalsLen) {
|
2017-02-23 22:21:13 +00:00
|
|
|
gst_error(vm, GST_NO_UPVALUE);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
return fn->def->literals[index];
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:54:50 +00:00
|
|
|
/* Load a function into the VM. The function will be called with
|
|
|
|
* no arguments when run */
|
|
|
|
static void gst_load(Gst *vm, GstValue callee) {
|
|
|
|
uint32_t startCapacity;
|
|
|
|
uint32_t locals, i;
|
|
|
|
uint16_t *pc;
|
|
|
|
GstStackFrame *frame;
|
|
|
|
GstThread *thread = gst_alloc(vm, sizeof(GstThread));
|
|
|
|
if (callee.type == GST_FUNCTION) {
|
|
|
|
locals = callee.data.function->def->locals;
|
|
|
|
pc = callee.data.function->def->byteCode;
|
|
|
|
} else if (callee.type == GST_CFUNCTION) {
|
|
|
|
locals = 0;
|
|
|
|
pc = NULL;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startCapacity = locals + GST_FRAME_SIZE + 10;
|
|
|
|
thread->data = gst_alloc(vm, sizeof(GstValue) * startCapacity);
|
|
|
|
thread->capacity = startCapacity;
|
|
|
|
thread->count = GST_FRAME_SIZE;
|
|
|
|
vm->thread = thread;
|
|
|
|
frame = (GstStackFrame *)thread->data;
|
|
|
|
frame->prevSize = 0;
|
|
|
|
frame->size = locals;
|
|
|
|
frame->callee = callee;
|
|
|
|
frame->errorJump = NULL;
|
|
|
|
frame->env = NULL;
|
|
|
|
frame->pc = pc;
|
|
|
|
/* Nil arguments */
|
|
|
|
for (i = 0; i < locals; ++i)
|
|
|
|
thread->data[GST_FRAME_SIZE + i].type = GST_NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Start running the VM */
|
2017-03-08 15:54:50 +00:00
|
|
|
int gst_start(Gst *vm, GstValue func) {
|
2017-02-26 16:47:50 +00:00
|
|
|
/* VM state */
|
2017-03-08 15:54:50 +00:00
|
|
|
GstThread thread;
|
2017-02-26 16:47:50 +00:00
|
|
|
GstValue *stack;
|
|
|
|
GstStackFrame frame;
|
|
|
|
GstValue temp, v1, v2;
|
|
|
|
uint16_t *pc;
|
2017-03-08 15:54:50 +00:00
|
|
|
/* Load the callee */
|
|
|
|
gst_load(vm, func);
|
|
|
|
thread = *vm->thread;
|
2017-02-26 16:47:50 +00:00
|
|
|
stack = thread.data + thread.count;
|
|
|
|
frame = *((GstStackFrame *)(stack - GST_FRAME_SIZE));
|
|
|
|
pc = frame.pc;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
/* Set jmp_buf to jump back to for return. */
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
if ((n = setjmp(vm->jump))) {
|
|
|
|
/* Good return */
|
|
|
|
if (n == 1) {
|
|
|
|
return 0;
|
2017-02-19 16:19:39 +00:00
|
|
|
} else if (n == 2) {
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Error. */
|
2017-02-26 16:47:50 +00:00
|
|
|
while (!frame.errorJump) {
|
|
|
|
/* Check for closure */
|
|
|
|
if (frame.env) {
|
|
|
|
frame.env->thread = NULL;
|
|
|
|
frame.env->stackOffset = frame.size;
|
|
|
|
frame.env->values = gst_alloc(vm, sizeof(GstValue) * frame.size);
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(frame.env->values,
|
2017-02-26 16:47:50 +00:00
|
|
|
thread.data + thread.count,
|
|
|
|
frame.size * sizeof(GstValue));
|
|
|
|
}
|
|
|
|
stack -= frame.prevSize + GST_FRAME_SIZE;
|
2017-02-27 03:23:08 +00:00
|
|
|
if (stack <= thread.data) {
|
|
|
|
thread.count = 0;
|
2017-02-26 16:47:50 +00:00
|
|
|
break;
|
2017-02-27 03:23:08 +00:00
|
|
|
}
|
2017-02-26 16:47:50 +00:00
|
|
|
frame = *((GstStackFrame *)(stack - GST_FRAME_SIZE));
|
2017-02-22 23:19:46 +00:00
|
|
|
}
|
2017-02-26 16:47:50 +00:00
|
|
|
if (thread.count < GST_FRAME_SIZE)
|
2017-02-22 23:19:46 +00:00
|
|
|
return n;
|
2017-02-19 16:19:39 +00:00
|
|
|
/* Jump to the error location */
|
2017-02-26 16:47:50 +00:00
|
|
|
pc = frame.errorJump;
|
2017-02-22 23:19:46 +00:00
|
|
|
/* Set error */
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[frame.errorSlot] = vm->error;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-19 16:19:39 +00:00
|
|
|
/* Crash. just return */
|
2017-02-09 20:02:59 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* Main interpreter loop */
|
2017-02-09 20:02:59 +00:00
|
|
|
for (;;) {
|
2017-02-26 16:47:50 +00:00
|
|
|
|
|
|
|
switch (*pc) {
|
2017-02-12 15:27:18 +00:00
|
|
|
|
2017-03-07 20:29:40 +00:00
|
|
|
#define OP_BINARY_MATH(op) \
|
2017-02-26 16:47:50 +00:00
|
|
|
v1 = stack[pc[2]]; \
|
|
|
|
v2 = stack[pc[3]]; \
|
2017-02-23 22:21:13 +00:00
|
|
|
gst_assert(vm, v1.type == GST_NUMBER, GST_EXPECTED_NUMBER_LOP); \
|
|
|
|
gst_assert(vm, v2.type == GST_NUMBER, GST_EXPECTED_NUMBER_ROP); \
|
2017-02-16 02:02:00 +00:00
|
|
|
temp.type = GST_NUMBER; \
|
2017-02-13 05:11:30 +00:00
|
|
|
temp.data.number = v1.data.number op v2.data.number; \
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp; \
|
|
|
|
pc += 4; \
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-12 15:27:18 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_ADD: /* Addition */
|
2017-03-07 20:29:40 +00:00
|
|
|
OP_BINARY_MATH(+)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_SUB: /* Subtraction */
|
2017-03-07 20:29:40 +00:00
|
|
|
OP_BINARY_MATH(-)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_MUL: /* Multiplication */
|
2017-03-07 20:29:40 +00:00
|
|
|
OP_BINARY_MATH(*)
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_DIV: /* Division */
|
2017-03-07 20:29:40 +00:00
|
|
|
OP_BINARY_MATH(/)
|
2017-02-12 15:27:18 +00:00
|
|
|
|
2017-03-07 20:29:40 +00:00
|
|
|
#undef OP_BINARY_MATH
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_NOT: /* Boolean unary (Boolean not) */
|
|
|
|
temp.type = GST_BOOLEAN;
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.data.boolean = !gst_truthy(stack[pc[2]]);
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-03-08 15:21:09 +00:00
|
|
|
case GST_OP_NEG: /* Unary negation */
|
|
|
|
v1 = stack[pc[2]];
|
|
|
|
gst_assert(vm, v1.type == GST_NUMBER, GST_EXPECTED_NUMBER_LOP);
|
|
|
|
temp.type = GST_NUMBER;
|
|
|
|
temp.data.number = -v1.data.number;
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_OP_INV: /* Unary multiplicative inverse */
|
|
|
|
v1 = stack[pc[2]];
|
|
|
|
gst_assert(vm, v1.type == GST_NUMBER, GST_EXPECTED_NUMBER_LOP);
|
|
|
|
temp.type = GST_NUMBER;
|
|
|
|
temp.data.number = 1 / v1.data.number;
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_FLS: /* Load False */
|
|
|
|
temp.type = GST_BOOLEAN;
|
2017-02-13 05:11:30 +00:00
|
|
|
temp.data.boolean = 0;
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 2;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_TRU: /* Load True */
|
|
|
|
temp.type = GST_BOOLEAN;
|
2017-02-13 05:11:30 +00:00
|
|
|
temp.data.boolean = 1;
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 2;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_NIL: /* Load Nil */
|
|
|
|
temp.type = GST_NIL;
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 2;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_I16: /* Load Small Integer */
|
|
|
|
temp.type = GST_NUMBER;
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.data.number = ((int16_t *)(pc))[2];
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_UPV: /* Load Up Value */
|
2017-02-26 16:47:50 +00:00
|
|
|
case GST_OP_SUV: /* Set Up Value */
|
|
|
|
gst_assert(vm, frame.callee.type == GST_FUNCTION, GST_EXPECTED_FUNCTION);
|
|
|
|
{
|
|
|
|
GstValue *upv;
|
|
|
|
GstFunction *fn = frame.callee.data.function;
|
|
|
|
GstFuncEnv *env;
|
|
|
|
uint16_t level = pc[2];
|
|
|
|
if (level == 0)
|
|
|
|
upv = stack + pc[3];
|
|
|
|
else {
|
|
|
|
while (fn && --level)
|
|
|
|
fn = fn->parent;
|
|
|
|
gst_assert(vm, fn, GST_NO_UPVALUE);
|
|
|
|
env = fn->env;
|
|
|
|
if (env->thread)
|
|
|
|
upv = env->thread->data + env->stackOffset + pc[3];
|
|
|
|
else
|
|
|
|
upv = env->values + pc[3];
|
|
|
|
}
|
|
|
|
if (pc[0] == GST_OP_UPV) {
|
|
|
|
stack[pc[1]] = *upv;
|
|
|
|
} else {
|
|
|
|
*upv = stack[pc[1]];
|
|
|
|
}
|
|
|
|
pc += 4;
|
|
|
|
}
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_JIF: /* Jump If */
|
2017-02-26 16:47:50 +00:00
|
|
|
if (gst_truthy(stack[pc[1]])) {
|
|
|
|
pc += 4;
|
2017-02-13 05:11:30 +00:00
|
|
|
} else {
|
2017-02-26 16:47:50 +00:00
|
|
|
pc += *((int32_t *)(pc + 2));
|
2017-02-13 05:11:30 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_JMP: /* Jump */
|
2017-02-26 16:47:50 +00:00
|
|
|
pc += *((int32_t *)(pc + 1));
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_CAL: /* Call */
|
2017-02-26 16:47:50 +00:00
|
|
|
{
|
|
|
|
temp = stack[pc[1]];
|
|
|
|
uint32_t arity = pc[3];
|
|
|
|
uint32_t oldCount = thread.count;
|
|
|
|
uint32_t i, locals;
|
|
|
|
GstValue *oldBase;
|
|
|
|
frame.pc = pc + 4 + arity;
|
|
|
|
frame.ret = pc[2];
|
|
|
|
|
|
|
|
/* Save current stack frame */
|
|
|
|
*((GstStackFrame *)(stack - GST_FRAME_SIZE)) = frame;
|
|
|
|
|
|
|
|
/* Get the size of next stack frame */
|
|
|
|
if (temp.type == GST_FUNCTION) {
|
|
|
|
GstFunction *fn = temp.data.function;
|
|
|
|
locals = fn->def->locals;
|
|
|
|
} else if (temp.type == GST_CFUNCTION) {
|
|
|
|
locals = arity;
|
|
|
|
} else {
|
|
|
|
gst_error(vm, GST_EXPECTED_FUNCTION);
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* Push next stack frame */
|
|
|
|
{
|
|
|
|
uint32_t nextCount = thread.count + frame.size + GST_FRAME_SIZE;
|
|
|
|
/* Ensure capacity */
|
|
|
|
if (nextCount + locals > thread.capacity) {
|
|
|
|
uint32_t newCap = (nextCount + locals) * 2;
|
|
|
|
GstValue *newData = gst_alloc(vm, sizeof(GstValue) * newCap);
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(newData, thread.data, thread.capacity * sizeof(GstValue));
|
2017-02-26 16:47:50 +00:00
|
|
|
thread.data = newData;
|
|
|
|
thread.capacity = newCap;
|
|
|
|
}
|
|
|
|
thread.count = nextCount;
|
|
|
|
|
|
|
|
/* Ensure values start out as nil so as to not confuse
|
|
|
|
* the garabage collector */
|
|
|
|
for (i = nextCount; i < nextCount + locals; ++i)
|
|
|
|
thread.data[i].type = GST_NIL;
|
|
|
|
|
|
|
|
/* Set up the new stack frame */
|
|
|
|
stack = thread.data + thread.count;
|
|
|
|
frame.prevSize = frame.size;
|
|
|
|
frame.size = locals;
|
|
|
|
frame.env = NULL;
|
|
|
|
frame.callee = temp;
|
|
|
|
frame.errorJump = NULL;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* Prepare to copy arguments to next stack frame */
|
|
|
|
oldBase = thread.data + oldCount;
|
|
|
|
|
|
|
|
/* Write arguments to new stack */
|
|
|
|
for (i = 0; i < arity; ++i)
|
|
|
|
stack[i] = oldBase[pc[4 + i]];
|
|
|
|
|
|
|
|
/* Call the function */
|
|
|
|
if (temp.type == GST_CFUNCTION) {
|
|
|
|
/* Save current state to vm thread */
|
|
|
|
*((GstStackFrame *)(stack - GST_FRAME_SIZE)) = frame;
|
|
|
|
*vm->thread = thread;
|
|
|
|
v2 = temp.data.cfunction(vm);
|
|
|
|
goto ret;
|
|
|
|
} else {
|
|
|
|
for (; i < locals; ++i)
|
|
|
|
stack[i].type = GST_NIL;
|
|
|
|
pc = temp.data.function->def->byteCode;
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
case GST_OP_RET: /* Return */
|
|
|
|
v2 = stack[pc[1]];
|
|
|
|
goto ret;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_CST: /* Load constant value */
|
2017-02-26 16:47:50 +00:00
|
|
|
gst_assert(vm, frame.callee.type == GST_FUNCTION, GST_EXPECTED_FUNCTION);
|
|
|
|
stack[pc[1]] = gst_vm_literal(vm, frame.callee.data.function, pc[2]);
|
|
|
|
pc += 3;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_I32: /* Load 32 bit integer */
|
|
|
|
temp.type = GST_NUMBER;
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.data.number = *((int32_t *)(pc + 2));
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 4;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_F64: /* Load 64 bit float */
|
|
|
|
temp.type = GST_NUMBER;
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.data.number = (GstNumber) *((double *)(pc + 2));
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 6;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_MOV: /* Move Values */
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = stack[pc[2]];
|
|
|
|
pc += 3;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_CLN: /* Create closure from constant FuncDef */
|
2017-02-26 16:47:50 +00:00
|
|
|
{
|
2017-03-07 20:29:40 +00:00
|
|
|
GstFunction *fn;
|
2017-02-26 16:47:50 +00:00
|
|
|
if (frame.callee.type != GST_FUNCTION)
|
|
|
|
gst_error(vm, GST_EXPECTED_FUNCTION);
|
|
|
|
if (!frame.env) {
|
|
|
|
frame.env = gst_alloc(vm, sizeof(GstFuncEnv));
|
|
|
|
*vm->thread = thread;
|
|
|
|
frame.env->thread = vm->thread;
|
|
|
|
frame.env->stackOffset = thread.count;
|
|
|
|
frame.env->values = NULL;
|
|
|
|
}
|
2017-03-07 20:29:40 +00:00
|
|
|
temp = gst_vm_literal(vm, frame.callee.data.function, pc[2]);
|
2017-02-26 16:47:50 +00:00
|
|
|
if (temp.type != GST_NIL)
|
|
|
|
gst_error(vm, "cannot create closure");
|
|
|
|
fn = gst_alloc(vm, sizeof(GstFunction));
|
|
|
|
fn->def = (GstFuncDef *) temp.data.pointer;
|
2017-03-07 20:29:40 +00:00
|
|
|
fn->parent = frame.callee.data.function;
|
2017-02-26 16:47:50 +00:00
|
|
|
fn->env = frame.env;
|
|
|
|
temp.type = GST_FUNCTION;
|
|
|
|
temp.data.function = fn;
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
|
|
|
}
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_EQL: /* Equality */
|
|
|
|
temp.type = GST_BOOLEAN;
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.data.boolean = gst_equals(stack[pc[2]], stack[pc[3]]);
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 4;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_LTN: /* Less Than */
|
|
|
|
temp.type = GST_BOOLEAN;
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.data.boolean = (gst_compare(stack[pc[2]], stack[pc[3]]) == -1);
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 4;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_LTE: /* Less Than or Equal to */
|
|
|
|
temp.type = GST_BOOLEAN;
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.data.boolean = (gst_compare(stack[pc[2]], stack[pc[3]]) != 1);
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 4;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_ARR: /* Array literal */
|
2017-02-13 05:11:30 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2017-02-26 16:47:50 +00:00
|
|
|
uint32_t arrayLen = pc[2];
|
2017-02-16 02:02:00 +00:00
|
|
|
GstArray *array = gst_array(vm, arrayLen);
|
2017-02-13 05:11:30 +00:00
|
|
|
array->count = arrayLen;
|
|
|
|
for (i = 0; i < arrayLen; ++i)
|
2017-02-26 16:47:50 +00:00
|
|
|
array->data[i] = stack[pc[3 + i]];
|
2017-02-16 02:02:00 +00:00
|
|
|
temp.type = GST_ARRAY;
|
2017-02-13 05:11:30 +00:00
|
|
|
temp.data.array = array;
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3 + arrayLen;
|
2017-02-13 05:11:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_DIC: /* Object literal */
|
2017-02-13 05:11:30 +00:00
|
|
|
{
|
|
|
|
uint32_t i = 3;
|
2017-02-26 16:47:50 +00:00
|
|
|
uint32_t kvs = pc[2];
|
2017-02-16 02:02:00 +00:00
|
|
|
GstObject *o = gst_object(vm, kvs + 2);
|
2017-02-13 05:11:30 +00:00
|
|
|
kvs = kvs + 3;
|
|
|
|
while (i < kvs) {
|
2017-02-26 16:47:50 +00:00
|
|
|
v1 = stack[pc[i++]];
|
|
|
|
v2 = stack[pc[i++]];
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_object_put(vm, o, v1, v2);
|
2017-02-12 20:16:55 +00:00
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
temp.type = GST_OBJECT;
|
|
|
|
temp.data.object = o;
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += kvs;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-03-07 20:29:40 +00:00
|
|
|
|
|
|
|
case GST_OP_TUP: /* Tuple literal */
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t len = pc[2];
|
|
|
|
GstValue *tuple = gst_tuple(vm, len);
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
tuple[i] = stack[pc[3 + i]];
|
|
|
|
temp.type = GST_TUPLE;
|
|
|
|
temp.data.tuple = tuple;
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3 + len;
|
|
|
|
}
|
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_TCL: /* Tail call */
|
2017-02-26 16:47:50 +00:00
|
|
|
{
|
|
|
|
temp = stack[pc[1]];
|
|
|
|
uint32_t arity = pc[2];
|
|
|
|
uint16_t locals;
|
|
|
|
uint32_t i, workspace, totalCapacity;
|
|
|
|
|
|
|
|
/* Check for closures */
|
|
|
|
if (frame.env) {
|
|
|
|
frame.env->thread = NULL;
|
|
|
|
frame.env->stackOffset = frame.size;
|
|
|
|
frame.env->values = gst_alloc(vm, sizeof(GstValue) * frame.size);
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(frame.env->values,
|
2017-02-26 16:47:50 +00:00
|
|
|
thread.data + thread.count,
|
|
|
|
frame.size * sizeof(GstValue));
|
|
|
|
frame.env = NULL;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* Get size of new stack frame */
|
|
|
|
if (temp.type == GST_CFUNCTION) {
|
|
|
|
locals = arity;
|
|
|
|
} else if (temp.type == GST_FUNCTION) {
|
|
|
|
locals = temp.data.function->def->locals;
|
|
|
|
} else {
|
|
|
|
gst_error(vm, GST_EXPECTED_FUNCTION);
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* Get enough space for manipulating args */
|
|
|
|
if (arity > frame.size) {
|
|
|
|
workspace = arity;
|
|
|
|
} else {
|
|
|
|
workspace = frame.size;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* Ensure stack has enough space for copies of arguments */
|
|
|
|
totalCapacity = thread.count + arity + workspace + locals;
|
|
|
|
if (totalCapacity > thread.capacity) {
|
|
|
|
uint32_t newCap = totalCapacity * 2;
|
|
|
|
GstValue *newData = gst_alloc(vm, sizeof(GstValue) * newCap);
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(newData, thread.data, thread.capacity * sizeof(GstValue));
|
2017-02-26 16:47:50 +00:00
|
|
|
thread.data = newData;
|
|
|
|
thread.capacity = newCap;
|
|
|
|
stack = thread.data + thread.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the arguments into the extra space */
|
|
|
|
for (i = 0; i < arity; ++i)
|
|
|
|
stack[workspace + i] = stack[pc[3 + i]];
|
|
|
|
|
|
|
|
/* Copy the end of the stack to the parameter position */
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(stack, stack + workspace, arity * sizeof(GstValue));
|
2017-02-26 16:47:50 +00:00
|
|
|
|
|
|
|
/* Update the stack frame */
|
|
|
|
frame.callee = temp;
|
|
|
|
frame.size = locals;
|
|
|
|
|
|
|
|
/* Nil the non argument part of the stack for gc */
|
|
|
|
for (i = arity; i < frame.size; ++i)
|
|
|
|
stack[i].type = GST_NIL;
|
|
|
|
|
|
|
|
/* Call the function */
|
|
|
|
if (temp.type == GST_CFUNCTION) {
|
|
|
|
/* Save current state to vm thread */
|
|
|
|
*((GstStackFrame *)(stack - GST_FRAME_SIZE)) = frame;
|
|
|
|
*vm->thread = thread;
|
|
|
|
v2 = temp.data.cfunction(vm);
|
|
|
|
goto ret;
|
|
|
|
} else {
|
|
|
|
pc = temp.data.function->def->byteCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-02-12 20:53:52 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_RTN: /* Return nil */
|
2017-02-26 16:47:50 +00:00
|
|
|
v2.type = GST_NIL;
|
|
|
|
goto ret;
|
2017-02-13 05:11:30 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_GET:
|
2017-02-26 16:47:50 +00:00
|
|
|
temp = gst_get(vm, stack[pc[2]], stack[pc[3]]);
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 4;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
2017-02-12 20:53:52 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_SET:
|
2017-02-26 16:47:50 +00:00
|
|
|
gst_set(vm, stack[pc[1]], stack[pc[2]], stack[pc[3]]);
|
|
|
|
pc += 4;
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-22 23:19:46 +00:00
|
|
|
case GST_OP_ERR:
|
2017-02-26 16:47:50 +00:00
|
|
|
vm->error = stack[pc[1]];
|
2017-02-22 23:19:46 +00:00
|
|
|
longjmp(vm->jump, 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_OP_TRY:
|
2017-02-26 16:47:50 +00:00
|
|
|
frame.errorSlot = pc[1];
|
|
|
|
frame.errorJump = pc + *(uint32_t *)(pc + 2);
|
|
|
|
pc += 4;
|
2017-02-22 23:19:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_OP_UTY:
|
2017-02-26 16:47:50 +00:00
|
|
|
frame.errorJump = NULL;
|
|
|
|
pc++;
|
2017-02-22 23:19:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-13 05:11:30 +00:00
|
|
|
default:
|
2017-02-19 16:19:39 +00:00
|
|
|
gst_error(vm, "unknown opcode");
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-26 16:47:50 +00:00
|
|
|
/* Label for return */
|
|
|
|
ret:
|
|
|
|
{
|
|
|
|
/* Check for closure */
|
|
|
|
if (frame.env) {
|
|
|
|
frame.env->thread = NULL;
|
|
|
|
frame.env->stackOffset = frame.size;
|
|
|
|
frame.env->values = gst_alloc(vm, sizeof(GstValue) * frame.size);
|
2017-03-01 01:20:29 +00:00
|
|
|
gst_memcpy(frame.env->values,
|
2017-02-26 16:47:50 +00:00
|
|
|
thread.data + thread.count,
|
|
|
|
frame.size * sizeof(GstValue));
|
|
|
|
}
|
|
|
|
if (thread.count <= GST_FRAME_SIZE)
|
|
|
|
gst_exit(vm, v2);
|
|
|
|
stack -= frame.prevSize + GST_FRAME_SIZE;
|
|
|
|
thread.count -= frame.prevSize + GST_FRAME_SIZE;
|
|
|
|
frame = *((GstStackFrame *)(stack - GST_FRAME_SIZE));
|
|
|
|
pc = frame.pc;
|
|
|
|
stack[frame.ret] = v2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* TODO: Move collection only to places that allocate memory */
|
2017-02-23 22:21:13 +00:00
|
|
|
/* This, however, is good for testing to ensure no memory leaks */
|
2017-02-26 16:47:50 +00:00
|
|
|
*vm->thread = thread;
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_maybe_collect(vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 04:28:11 +00:00
|
|
|
/* Get an argument from the stack */
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue gst_arg(Gst *vm, uint16_t index) {
|
2017-02-26 16:47:50 +00:00
|
|
|
GstValue *stack = vm->thread->data + vm->thread->count;
|
|
|
|
GstStackFrame *frame = (GstStackFrame *)(stack - GST_FRAME_SIZE);
|
|
|
|
uint16_t frameSize = frame->size;
|
2017-02-19 16:19:39 +00:00
|
|
|
gst_assert(vm, frameSize > index, "cannot get arg out of stack bounds");
|
2017-02-26 16:47:50 +00:00
|
|
|
return stack[index];
|
2017-02-10 04:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a value on the stack */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_set_arg(Gst* vm, uint16_t index, GstValue x) {
|
2017-02-26 16:47:50 +00:00
|
|
|
GstValue *stack = vm->thread->data + vm->thread->count;
|
|
|
|
GstStackFrame *frame = (GstStackFrame *)(stack - GST_FRAME_SIZE);
|
|
|
|
uint16_t frameSize = frame->size;
|
2017-02-19 16:19:39 +00:00
|
|
|
gst_assert(vm, frameSize > index, "cannot set arg out of stack bounds");
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[index] = x;
|
2017-02-10 04:28:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:01:06 +00:00
|
|
|
/* Get the size of the VMStack */
|
2017-02-16 02:02:00 +00:00
|
|
|
uint16_t gst_count_args(Gst *vm) {
|
2017-02-26 16:47:50 +00:00
|
|
|
GstValue *stack = vm->thread->data + vm->thread->count;
|
|
|
|
GstStackFrame *frame = (GstStackFrame *)(stack - GST_FRAME_SIZE);
|
|
|
|
return frame->size;
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Initialize the VM */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_init(Gst *vm) {
|
|
|
|
vm->ret.type = GST_NIL;
|
2017-02-22 23:19:46 +00:00
|
|
|
vm->error.type = GST_NIL;
|
|
|
|
vm->crash = NULL;
|
2017-02-12 20:16:55 +00:00
|
|
|
/* Garbage collection */
|
2017-02-09 23:50:47 +00:00
|
|
|
vm->blocks = NULL;
|
|
|
|
vm->nextCollection = 0;
|
2017-02-13 05:11:30 +00:00
|
|
|
/* Setting memoryInterval to zero currently forces
|
|
|
|
* a collection pretty much every cycle, which is
|
|
|
|
* obviously horrible for performance. It helps ensure
|
|
|
|
* there are no memory bugs during dev */
|
2017-02-23 22:21:13 +00:00
|
|
|
vm->memoryInterval = 2000;
|
2017-02-09 23:50:47 +00:00
|
|
|
vm->black = 0;
|
2017-02-13 02:54:18 +00:00
|
|
|
/* Add thread */
|
2017-02-13 03:25:17 +00:00
|
|
|
vm->thread = NULL;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear all memory associated with the VM */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_deinit(Gst *vm) {
|
2017-02-23 22:21:13 +00:00
|
|
|
gst_clear_memory(vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|