2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-03-22 04:27:18 +00:00
|
|
|
#include <gst/gst.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";
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-04-19 13:02:12 +00:00
|
|
|
/* Start running the VM from where it left off. */
|
|
|
|
int gst_continue(Gst *vm) {
|
2017-02-26 16:47:50 +00:00
|
|
|
/* VM state */
|
|
|
|
GstValue *stack;
|
|
|
|
GstValue temp, v1, v2;
|
|
|
|
uint16_t *pc;
|
2017-03-08 20:08:46 +00:00
|
|
|
|
2017-04-19 13:02:12 +00:00
|
|
|
#define gst_exit(vm, r) return ((vm)->ret = (r), GST_RETURN_OK)
|
|
|
|
#define gst_error(vm, e) do { (vm)->ret = gst_string_cv((vm), (e)); goto vm_error; } while (0)
|
|
|
|
#define gst_crash(vm, e) return ((vm)->crash = (e), GST_RETURN_CRASH)
|
|
|
|
#define gst_assert(vm, cond, e) do {if (!(cond)){gst_error((vm), (e));}} while (0)
|
|
|
|
|
2017-03-10 05:17:34 +00:00
|
|
|
/* Intialize local state */
|
2017-04-24 22:09:23 +00:00
|
|
|
stack = gst_thread_stack(vm->thread);
|
2017-03-10 05:09:42 +00:00
|
|
|
pc = gst_frame_pc(stack);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
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-10 05:09:42 +00:00
|
|
|
default:
|
2017-03-10 05:17:34 +00:00
|
|
|
gst_error(vm, "unknown opcode");
|
2017-03-10 05:09:42 +00:00
|
|
|
break;
|
2017-04-12 14:31:50 +00:00
|
|
|
|
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-03-14 19:55:50 +00:00
|
|
|
continue;
|
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-03-14 19:55:50 +00:00
|
|
|
continue;
|
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-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_I16: /* Load Small Integer */
|
2017-04-24 20:02:54 +00:00
|
|
|
temp.type = GST_INTEGER;
|
|
|
|
temp.data.integer = ((int16_t *)(pc))[2];
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
2017-03-14 19:55:50 +00:00
|
|
|
continue;
|
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 */
|
|
|
|
{
|
|
|
|
GstValue *upv;
|
2017-03-10 05:09:42 +00:00
|
|
|
GstFunction *fn;
|
2017-02-26 16:47:50 +00:00
|
|
|
GstFuncEnv *env;
|
|
|
|
uint16_t level = pc[2];
|
2017-03-10 05:09:42 +00:00
|
|
|
temp = gst_frame_callee(stack);
|
|
|
|
gst_assert(vm, temp.type == GST_FUNCTION, GST_EXPECTED_FUNCTION);
|
|
|
|
fn = temp.data.function;
|
2017-02-26 16:47:50 +00:00
|
|
|
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-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-02-13 05:11:30 +00:00
|
|
|
|
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
|
|
|
}
|
2017-03-14 19:55:50 +00:00
|
|
|
continue;
|
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-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_CST: /* Load constant value */
|
2017-03-10 05:17:34 +00:00
|
|
|
v1 = gst_frame_callee(stack);
|
2017-03-10 05:09:42 +00:00
|
|
|
gst_assert(vm, v1.type == GST_FUNCTION, GST_EXPECTED_FUNCTION);
|
|
|
|
if (pc[2] > v1.data.function->def->literalsLen)
|
2017-03-08 20:08:46 +00:00
|
|
|
gst_error(vm, GST_NO_UPVALUE);
|
2017-03-10 05:09:42 +00:00
|
|
|
stack[pc[1]] = v1.data.function->def->literals[pc[2]];
|
2017-02-26 16:47:50 +00:00
|
|
|
pc += 3;
|
2017-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_I32: /* Load 32 bit integer */
|
2017-04-24 20:02:54 +00:00
|
|
|
temp.type = GST_INTEGER;
|
|
|
|
temp.data.integer = *((int32_t *)(pc + 2));
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 4;
|
2017-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-04-24 20:02:54 +00:00
|
|
|
case GST_OP_I64: /* Load 64 bit integer */
|
|
|
|
temp.type = GST_INTEGER;
|
|
|
|
temp.data.integer = (GstInteger) *((int64_t *)(pc + 2));
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 6;
|
|
|
|
continue;
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_F64: /* Load 64 bit float */
|
2017-04-24 20:02:54 +00:00
|
|
|
temp.type = GST_REAL;
|
|
|
|
temp.data.real = (GstReal) *((double *)(pc + 2));
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 6;
|
2017-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-02-13 05:11:30 +00:00
|
|
|
|
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-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-02-13 05:11:30 +00:00
|
|
|
|
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-03-10 05:09:42 +00:00
|
|
|
v1 = gst_frame_callee(stack);
|
|
|
|
if (v1.type != GST_FUNCTION)
|
2017-02-26 16:47:50 +00:00
|
|
|
gst_error(vm, GST_EXPECTED_FUNCTION);
|
2017-03-10 05:09:42 +00:00
|
|
|
if (gst_frame_env(stack) == NULL) {
|
|
|
|
gst_frame_env(stack) = gst_alloc(vm, sizeof(GstFuncEnv));
|
|
|
|
gst_frame_env(stack)->thread = vm->thread;
|
2017-04-17 04:15:18 +00:00
|
|
|
gst_frame_env(stack)->stackOffset = vm->thread->count;
|
2017-03-10 05:09:42 +00:00
|
|
|
gst_frame_env(stack)->values = NULL;
|
2017-02-26 16:47:50 +00:00
|
|
|
}
|
2017-03-10 05:09:42 +00:00
|
|
|
if (pc[2] > v1.data.function->def->literalsLen)
|
2017-03-08 20:08:46 +00:00
|
|
|
gst_error(vm, GST_NO_UPVALUE);
|
2017-03-10 05:09:42 +00:00
|
|
|
temp = v1.data.function->def->literals[pc[2]];
|
2017-03-19 16:16:40 +00:00
|
|
|
if (temp.type != GST_FUNCDEF)
|
2017-02-26 16:47:50 +00:00
|
|
|
gst_error(vm, "cannot create closure");
|
|
|
|
fn = gst_alloc(vm, sizeof(GstFunction));
|
2017-03-19 16:16:40 +00:00
|
|
|
fn->def = temp.data.def;
|
2017-03-10 05:09:42 +00:00
|
|
|
fn->parent = v1.data.function;
|
|
|
|
fn->env = gst_frame_env(stack);
|
2017-02-26 16:47:50 +00:00
|
|
|
temp.type = GST_FUNCTION;
|
|
|
|
temp.data.function = fn;
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
|
|
|
}
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-03-10 05:09:42 +00:00
|
|
|
case GST_OP_RTN: /* Return nil */
|
2017-04-17 04:15:18 +00:00
|
|
|
stack = gst_thread_popframe(vm, vm->thread);
|
2017-04-19 13:02:12 +00:00
|
|
|
if (vm->thread->count < GST_FRAME_SIZE) {
|
2017-03-12 22:23:27 +00:00
|
|
|
vm->ret.type = GST_NIL;
|
|
|
|
return GST_RETURN_OK;
|
|
|
|
}
|
2017-03-14 19:55:50 +00:00
|
|
|
pc = gst_frame_pc(stack);
|
2017-03-12 22:23:27 +00:00
|
|
|
stack[gst_frame_ret(stack)].type = GST_NIL;
|
2017-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-03-10 05:09:42 +00:00
|
|
|
|
|
|
|
case GST_OP_RET: /* Return */
|
2017-03-15 05:26:45 +00:00
|
|
|
temp = stack[pc[1]];
|
2017-04-17 04:15:18 +00:00
|
|
|
stack = gst_thread_popframe(vm, vm->thread);
|
2017-04-19 13:02:12 +00:00
|
|
|
if (vm->thread->count < GST_FRAME_SIZE) {
|
2017-03-12 22:23:27 +00:00
|
|
|
vm->ret = temp;
|
|
|
|
return GST_RETURN_OK;
|
2017-03-10 05:17:34 +00:00
|
|
|
}
|
2017-03-14 19:55:50 +00:00
|
|
|
pc = gst_frame_pc(stack);
|
2017-03-12 22:23:27 +00:00
|
|
|
stack[gst_frame_ret(stack)] = temp;
|
2017-03-14 19:55:50 +00:00
|
|
|
continue;
|
2017-03-10 05:09:42 +00:00
|
|
|
|
2017-04-19 13:02:12 +00:00
|
|
|
case GST_OP_PSK: /* Push stack */
|
2017-03-15 05:26:45 +00:00
|
|
|
{
|
2017-04-19 13:02:12 +00:00
|
|
|
uint16_t arity = pc[1];
|
|
|
|
uint16_t i;
|
|
|
|
uint16_t newBase = gst_frame_size(stack) + GST_FRAME_SIZE;
|
|
|
|
gst_frame_args(stack) = newBase;
|
|
|
|
gst_thread_ensure_extra(vm, vm->thread, GST_FRAME_SIZE + arity);
|
|
|
|
stack = gst_thread_stack(vm->thread);
|
|
|
|
gst_frame_size(stack) += GST_FRAME_SIZE + arity;
|
|
|
|
/* Nil stuff */
|
|
|
|
for (i = 0; i < GST_FRAME_SIZE; ++i)
|
|
|
|
stack[newBase + i - GST_FRAME_SIZE].type = GST_NIL;
|
2017-03-15 05:26:45 +00:00
|
|
|
/* Write arguments */
|
|
|
|
for (i = 0; i < arity; ++i)
|
2017-04-19 13:02:12 +00:00
|
|
|
stack[newBase + i] = stack[pc[2 + i]];
|
|
|
|
pc += 2 + arity;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_OP_PAR: /* Push array or tuple */
|
|
|
|
{
|
|
|
|
uint32_t count, i, oldsize;
|
|
|
|
const GstValue *data;
|
|
|
|
temp = stack[pc[1]];
|
|
|
|
if (temp.type == GST_TUPLE) {
|
|
|
|
count = gst_tuple_length(temp.data.tuple);
|
|
|
|
data = temp.data.tuple;
|
|
|
|
} else if (temp.type == GST_ARRAY){
|
|
|
|
count = temp.data.array->count;
|
|
|
|
data = temp.data.array->data;
|
2017-03-15 05:26:45 +00:00
|
|
|
} else {
|
2017-04-19 13:02:12 +00:00
|
|
|
gst_error(vm, "expected array or tuple");
|
2017-03-15 05:26:45 +00:00
|
|
|
}
|
2017-04-19 13:02:12 +00:00
|
|
|
oldsize = gst_frame_size(stack);
|
|
|
|
gst_thread_pushnil(vm, vm->thread, count);
|
|
|
|
stack = gst_thread_stack(vm->thread);
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
stack[oldsize + i] = data[i];
|
|
|
|
pc += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_OP_CAL: /* Call */
|
|
|
|
{
|
|
|
|
uint16_t newStackIndex = gst_frame_args(stack);
|
|
|
|
uint16_t size = gst_frame_size(stack);
|
|
|
|
temp = stack[pc[1]];
|
2017-04-24 22:42:08 +00:00
|
|
|
gst_frame_size(stack) = newStackIndex - GST_FRAME_SIZE;
|
2017-04-19 13:02:12 +00:00
|
|
|
gst_frame_ret(stack) = pc[2];
|
|
|
|
gst_frame_pc(stack) = pc + 3;
|
|
|
|
if (newStackIndex < GST_FRAME_SIZE)
|
|
|
|
gst_error(vm, "invalid call instruction");
|
|
|
|
vm->thread->count += newStackIndex;
|
|
|
|
stack = gst_thread_stack(vm->thread);
|
|
|
|
gst_frame_size(stack) = size - newStackIndex;
|
2017-04-24 17:12:55 +00:00
|
|
|
gst_frame_prevsize(stack) = newStackIndex - GST_FRAME_SIZE;
|
2017-04-19 13:02:12 +00:00
|
|
|
gst_frame_callee(stack) = temp;
|
|
|
|
}
|
|
|
|
goto common_function_call;
|
|
|
|
|
|
|
|
case GST_OP_TCL: /* Tail call */
|
|
|
|
{
|
|
|
|
uint16_t newStackIndex = gst_frame_args(stack);
|
|
|
|
uint16_t size = gst_frame_size(stack);
|
|
|
|
uint16_t i;
|
|
|
|
temp = stack[pc[1]];
|
|
|
|
/* Check for closures */
|
|
|
|
if (gst_frame_env(stack)) {
|
|
|
|
GstFuncEnv *env = gst_frame_env(stack);
|
|
|
|
env->thread = NULL;
|
|
|
|
env->stackOffset = size;
|
|
|
|
env->values = gst_alloc(vm, sizeof(GstValue) * size);
|
|
|
|
gst_memcpy(env->values, stack, sizeof(GstValue) * size);
|
|
|
|
}
|
|
|
|
if (newStackIndex)
|
|
|
|
for (i = 0; i < size - newStackIndex; ++i)
|
|
|
|
stack[i] = stack[newStackIndex + i];
|
|
|
|
gst_frame_size(stack) = size - newStackIndex;
|
|
|
|
gst_frame_callee(stack) = temp;
|
|
|
|
}
|
|
|
|
goto common_function_call;
|
|
|
|
|
|
|
|
/* Code common to all function calls */
|
|
|
|
common_function_call:
|
|
|
|
gst_frame_args(stack) = 0;
|
|
|
|
gst_frame_env(stack) = NULL;
|
|
|
|
gst_thread_endframe(vm, vm->thread);
|
|
|
|
stack = vm->thread->data + vm->thread->count;
|
|
|
|
temp = gst_frame_callee(stack);
|
|
|
|
if (temp.type == GST_FUNCTION) {
|
|
|
|
pc = temp.data.function->def->byteCode;
|
|
|
|
} else if (temp.type == GST_CFUNCTION) {
|
|
|
|
int status;
|
|
|
|
vm->ret.type = GST_NIL;
|
|
|
|
status = temp.data.cfunction(vm);
|
|
|
|
stack = gst_thread_popframe(vm, vm->thread);
|
|
|
|
if (status == GST_RETURN_OK) {
|
|
|
|
if (vm->thread->count < GST_FRAME_SIZE) {
|
|
|
|
return status;
|
|
|
|
} else {
|
|
|
|
stack[gst_frame_ret(stack)] = vm->ret;
|
|
|
|
pc = gst_frame_pc(stack);
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
2017-04-19 13:02:12 +00:00
|
|
|
} else {
|
|
|
|
goto vm_error;
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
2017-04-19 13:02:12 +00:00
|
|
|
} else {
|
|
|
|
gst_error(vm, GST_EXPECTED_FUNCTION);
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_OP_ARR: /* Array literal */
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t arrayLen = pc[2];
|
|
|
|
GstArray *array = gst_array(vm, arrayLen);
|
|
|
|
array->count = arrayLen;
|
|
|
|
for (i = 0; i < arrayLen; ++i)
|
|
|
|
array->data[i] = stack[pc[3 + i]];
|
|
|
|
temp.type = GST_ARRAY;
|
|
|
|
temp.data.array = array;
|
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3 + arrayLen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-25 01:00:56 +00:00
|
|
|
case GST_OP_DIC: /* Table literal */
|
2017-04-12 14:31:50 +00:00
|
|
|
{
|
|
|
|
uint32_t i = 3;
|
|
|
|
uint32_t kvs = pc[2];
|
2017-04-25 01:00:56 +00:00
|
|
|
GstTable *t = gst_table(vm, 2 * kvs);
|
2017-04-12 14:31:50 +00:00
|
|
|
kvs = kvs + 3;
|
|
|
|
while (i < kvs) {
|
|
|
|
v1 = stack[pc[i++]];
|
|
|
|
v2 = stack[pc[i++]];
|
2017-04-25 01:00:56 +00:00
|
|
|
gst_table_put(vm, t, v1, v2);
|
2017-03-15 05:26:45 +00:00
|
|
|
}
|
2017-04-25 01:00:56 +00:00
|
|
|
temp.type = GST_TABLE;
|
|
|
|
temp.data.table = t;
|
2017-04-12 14:31:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += kvs;
|
2017-03-15 05:26:45 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-04-12 14:31:50 +00:00
|
|
|
|
|
|
|
case GST_OP_TUP: /* Tuple literal */
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t len = pc[2];
|
2017-04-15 20:05:59 +00:00
|
|
|
GstValue *tuple = gst_tuple_begin(vm, len);
|
2017-04-12 14:31:50 +00:00
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
tuple[i] = stack[pc[3 + i]];
|
|
|
|
temp.type = GST_TUPLE;
|
2017-04-15 20:05:59 +00:00
|
|
|
temp.data.tuple = gst_tuple_end(vm, tuple);
|
2017-04-12 14:31:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3 + len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-19 16:56:29 +00:00
|
|
|
case GST_OP_YLD: /* Yield to new thread */
|
|
|
|
temp = stack[pc[1]];
|
|
|
|
v1 = stack[pc[2]];
|
|
|
|
gst_assert(vm, v1.type == GST_THREAD, "expected thread");
|
|
|
|
gst_assert(vm, v1.data.thread->status != GST_THREAD_DEAD, "cannot rejoin dead thread");
|
|
|
|
gst_frame_pc(stack) = pc + 3;
|
|
|
|
vm->thread = v1.data.thread;
|
|
|
|
vm->thread->status = GST_THREAD_ALIVE;
|
|
|
|
stack = vm->thread->data + vm->thread->count;
|
|
|
|
pc = gst_frame_pc(stack);
|
|
|
|
continue;
|
2017-03-10 05:09:42 +00:00
|
|
|
|
|
|
|
/* Handle errors from c functions and vm opcodes */
|
|
|
|
vm_error:
|
2017-04-19 13:02:12 +00:00
|
|
|
if (stack == NULL || vm->thread->parent == NULL)
|
2017-03-21 03:06:38 +00:00
|
|
|
return GST_RETURN_ERROR;
|
2017-04-19 13:02:12 +00:00
|
|
|
vm->thread->status = GST_THREAD_DEAD;
|
|
|
|
vm->thread = vm->thread->parent;
|
|
|
|
stack = vm->thread->data + vm->thread->count;
|
|
|
|
pc = gst_frame_pc(stack);
|
|
|
|
continue;
|
2017-03-10 05:09:42 +00:00
|
|
|
|
2017-03-14 19:55:50 +00:00
|
|
|
} /* end switch */
|
2017-03-10 05:09:42 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* Check for collection every cycle. If the instruction definitely does
|
|
|
|
* not allocate memory, it can use continue instead of break to
|
|
|
|
* skip this check */
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_maybe_collect(vm);
|
2017-03-10 05:09:42 +00:00
|
|
|
|
|
|
|
} /* end for */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Run the vm with a given function. This function is
|
|
|
|
* called to start the vm. */
|
2017-03-12 22:23:27 +00:00
|
|
|
int gst_run(Gst *vm, GstValue callee) {
|
2017-04-12 14:31:50 +00:00
|
|
|
int status;
|
2017-03-14 19:55:50 +00:00
|
|
|
GstValue *stack;
|
2017-03-12 22:23:27 +00:00
|
|
|
vm->thread = gst_thread(vm, callee, 64);
|
2017-03-14 19:55:50 +00:00
|
|
|
if (vm->thread == NULL)
|
|
|
|
return GST_RETURN_CRASH;
|
|
|
|
stack = gst_thread_stack(vm->thread);
|
|
|
|
/* If callee was not actually a function, get the delegate function */
|
|
|
|
callee = gst_frame_callee(stack);
|
|
|
|
if (callee.type == GST_CFUNCTION) {
|
2017-03-15 05:26:45 +00:00
|
|
|
vm->ret.type = GST_NIL;
|
|
|
|
status = callee.data.cfunction(vm);
|
|
|
|
gst_thread_popframe(vm, vm->thread);
|
|
|
|
return status;
|
2017-03-14 19:55:50 +00:00
|
|
|
} else {
|
|
|
|
return gst_continue(vm);
|
|
|
|
}
|
2017-03-10 05:09:42 +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-03-12 22:23:27 +00:00
|
|
|
GstValue *stack = gst_thread_stack(vm->thread);
|
2017-03-10 05:09:42 +00:00
|
|
|
uint16_t frameSize = gst_frame_size(stack);
|
2017-03-08 20:08:46 +00:00
|
|
|
if (frameSize <= index) {
|
2017-03-10 05:17:34 +00:00
|
|
|
GstValue ret;
|
|
|
|
ret.type = GST_NIL;
|
|
|
|
return ret;
|
2017-03-08 20:08:46 +00:00
|
|
|
}
|
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-03-12 22:23:27 +00:00
|
|
|
GstValue *stack = gst_thread_stack(vm->thread);
|
2017-03-10 05:09:42 +00:00
|
|
|
uint16_t frameSize = gst_frame_size(stack);
|
2017-03-10 05:17:34 +00:00
|
|
|
if (frameSize <= index) return;
|
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-03-12 22:23:27 +00:00
|
|
|
GstValue *stack = gst_thread_stack(vm->thread);
|
2017-03-10 05:09:42 +00:00
|
|
|
return gst_frame_size(stack);
|
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->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-04-18 20:55:03 +00:00
|
|
|
/* Setting memoryInterval to zero forces
|
2017-02-13 05:11:30 +00:00
|
|
|
* a collection pretty much every cycle, which is
|
2017-04-18 20:55:03 +00:00
|
|
|
* obviously horrible for performance, but helps ensure
|
2017-02-13 05:11:30 +00:00
|
|
|
* there are no memory bugs during dev */
|
2017-04-24 22:42:08 +00:00
|
|
|
vm->memoryInterval = 0;
|
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-04-15 20:05:59 +00:00
|
|
|
/* Set up scratch memory */
|
|
|
|
vm->scratch = NULL;
|
|
|
|
vm->scratch_len = 0;
|
|
|
|
/* Set up the cache */
|
|
|
|
vm->cache = gst_raw_calloc(1, 128 * sizeof(GstValue));
|
|
|
|
vm->cache_capacity = vm->cache == NULL ? 0 : 128;
|
|
|
|
vm->cache_count = 0;
|
|
|
|
vm->cache_deleted = 0;
|
2017-04-24 17:12:55 +00:00
|
|
|
/* Set up global env */
|
2017-04-25 01:00:56 +00:00
|
|
|
vm->modules = gst_table(vm, 10);
|
|
|
|
vm->registry = gst_table(vm, 10);
|
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-03-14 23:13:17 +00:00
|
|
|
vm->thread = NULL;
|
2017-04-18 20:55:03 +00:00
|
|
|
vm->modules = NULL;
|
|
|
|
vm->registry = NULL;
|
2017-03-14 23:13:17 +00:00
|
|
|
vm->ret.type = GST_NIL;
|
2017-04-15 20:05:59 +00:00
|
|
|
vm->scratch = NULL;
|
|
|
|
vm->scratch_len = 0;
|
|
|
|
/* Deinit the cache */
|
|
|
|
gst_raw_free(vm->cache);
|
|
|
|
vm->cache = NULL;
|
|
|
|
vm->cache_count = 0;
|
|
|
|
vm->cache_capacity = 0;
|
|
|
|
vm->cache_deleted = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|