2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* 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:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* 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-09-09 18:39:51 +00:00
|
|
|
#include "internal.h"
|
2017-11-01 21:53:43 +00:00
|
|
|
#include "wrap.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
static const char DST_NO_UPVALUE[] = "no upvalue";
|
|
|
|
static const char DST_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. */
|
2017-09-09 18:39:51 +00:00
|
|
|
int dst_continue(Dst *vm) {
|
2017-02-26 16:47:50 +00:00
|
|
|
/* VM state */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue *stack;
|
2017-02-26 16:47:50 +00:00
|
|
|
uint16_t *pc;
|
2017-03-08 20:08:46 +00:00
|
|
|
|
2017-05-09 23:21:30 +00:00
|
|
|
/* Some temporary values */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue temp, v1, v2;
|
2017-05-09 23:21:30 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
#define dst_exit(vm, r) return ((vm)->ret = (r), DST_RETURN_OK)
|
|
|
|
#define dst_error(vm, e) do { (vm)->ret = dst_string_cv((vm), (e)); goto vm_error; } while (0)
|
|
|
|
#define dst_assert(vm, cond, e) do {if (!(cond)){dst_error((vm), (e));}} while (0)
|
2017-04-19 13:02:12 +00:00
|
|
|
|
2017-03-10 05:17:34 +00:00
|
|
|
/* Intialize local state */
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->thread->status = DST_THREAD_ALIVE;
|
|
|
|
stack = dst_thread_stack(vm->thread);
|
|
|
|
pc = dst_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-07-02 01:51:16 +00:00
|
|
|
|
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-09-09 18:39:51 +00:00
|
|
|
dst_error(vm, "unknown opcode");
|
2017-03-10 05:09:42 +00:00
|
|
|
break;
|
2017-07-02 01:51:16 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_FLS: /* Load False */
|
|
|
|
temp.type = DST_BOOLEAN;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_TRU: /* Load True */
|
|
|
|
temp.type = DST_BOOLEAN;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_NIL: /* Load Nil */
|
|
|
|
temp.type = DST_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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_I16: /* Load Small Integer */
|
|
|
|
temp.type = DST_INTEGER;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_UPV: /* Load Up Value */
|
|
|
|
case DST_OP_SUV: /* Set Up Value */
|
2017-02-26 16:47:50 +00:00
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue *upv;
|
|
|
|
DstFunction *fn;
|
|
|
|
DstFuncEnv *env;
|
2017-02-26 16:47:50 +00:00
|
|
|
uint16_t level = pc[2];
|
2017-09-09 18:39:51 +00:00
|
|
|
temp = dst_frame_callee(stack);
|
|
|
|
dst_assert(vm, temp.type == DST_FUNCTION, DST_EXPECTED_FUNCTION);
|
2017-11-01 21:53:43 +00:00
|
|
|
fn = temp.as.function;
|
2017-02-26 16:47:50 +00:00
|
|
|
if (level == 0)
|
|
|
|
upv = stack + pc[3];
|
|
|
|
else {
|
|
|
|
while (fn && --level)
|
|
|
|
fn = fn->parent;
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_assert(vm, fn, DST_NO_UPVALUE);
|
2017-02-26 16:47:50 +00:00
|
|
|
env = fn->env;
|
|
|
|
if (env->thread)
|
|
|
|
upv = env->thread->data + env->stackOffset + pc[3];
|
|
|
|
else
|
|
|
|
upv = env->values + pc[3];
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (pc[0] == DST_OP_UPV) {
|
2017-02-26 16:47:50 +00:00
|
|
|
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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_JIF: /* Jump If */
|
|
|
|
if (dst_value_truthy(stack[pc[1]])) {
|
2017-02-26 16:47:50 +00:00
|
|
|
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-09-09 18:39:51 +00:00
|
|
|
case DST_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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_CST: /* Load constant value */
|
|
|
|
v1 = dst_frame_callee(stack);
|
|
|
|
dst_assert(vm, v1.type == DST_FUNCTION, DST_EXPECTED_FUNCTION);
|
2017-11-01 21:53:43 +00:00
|
|
|
if (pc[2] > v1.as.function->def->literalsLen)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_error(vm, DST_NO_UPVALUE);
|
2017-11-01 21:53:43 +00:00
|
|
|
stack[pc[1]] = v1.as.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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_I32: /* Load 32 bit integer */
|
|
|
|
temp.type = DST_INTEGER;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_I64: /* Load 64 bit integer */
|
|
|
|
temp.type = DST_INTEGER;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.integer = *((int64_t *)(pc + 2));
|
2017-04-24 20:02:54 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 6;
|
|
|
|
continue;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_F64: /* Load 64 bit float */
|
|
|
|
temp.type = DST_REAL;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.real = *((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-09-09 18:39:51 +00:00
|
|
|
case DST_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-09-09 18:39:51 +00:00
|
|
|
case DST_OP_CLN: /* Create closure from constant FuncDef */
|
2017-02-26 16:47:50 +00:00
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
DstFunction *fn;
|
|
|
|
v1 = dst_frame_callee(stack);
|
2017-11-01 21:53:43 +00:00
|
|
|
temp = v1.as.function->def->literals[pc[2]];
|
2017-09-09 18:39:51 +00:00
|
|
|
if (temp.type != DST_FUNCDEF)
|
|
|
|
dst_error(vm, "cannot create closure from non-funcdef");
|
2017-11-01 21:53:43 +00:00
|
|
|
fn = dst_mem_resumegc(dst_alloc(vm, sizeof(DstFunction)));
|
|
|
|
fn->def = temp.as.def;
|
|
|
|
/* Don't always set the parent. We might want to let the gc get it */
|
|
|
|
if (temp.as.def->flags & DST_FUNCDEF_FLAG_NEEDSPARENT)
|
|
|
|
fn->parent = v1.as.function;
|
2017-05-12 01:30:18 +00:00
|
|
|
else
|
|
|
|
fn->parent = NULL;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (v1.type != DST_FUNCTION)
|
|
|
|
dst_error(vm, DST_EXPECTED_FUNCTION);
|
|
|
|
if (dst_frame_env(stack) == NULL && (fn->def->flags & DST_FUNCDEF_FLAG_NEEDSENV)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_frame_env(stack) = dst_mem_resumegc(dst_alloc(vm, sizeof(DstFuncEnv)));
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_frame_env(stack)->thread = vm->thread;
|
|
|
|
dst_frame_env(stack)->stackOffset = vm->thread->count;
|
|
|
|
dst_frame_env(stack)->values = NULL;
|
2017-02-26 16:47:50 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
if (pc[2] > v1.as.function->def->literalsLen)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_error(vm, DST_NO_UPVALUE);
|
|
|
|
if (fn->def->flags & DST_FUNCDEF_FLAG_NEEDSENV)
|
|
|
|
fn->env = dst_frame_env(stack);
|
2017-05-11 21:08:29 +00:00
|
|
|
else
|
2017-05-12 01:30:18 +00:00
|
|
|
fn->env = NULL;
|
2017-09-09 18:39:51 +00:00
|
|
|
temp.type = DST_FUNCTION;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.function = fn;
|
2017-02-26 16:47:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3;
|
|
|
|
}
|
2017-02-13 05:11:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_RTN: /* Return nil */
|
|
|
|
temp.type = DST_NIL;
|
2017-05-09 23:21:30 +00:00
|
|
|
goto vm_return;
|
2017-03-10 05:09:42 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_RET: /* Return */
|
2017-03-15 05:26:45 +00:00
|
|
|
temp = stack[pc[1]];
|
2017-05-09 23:21:30 +00:00
|
|
|
goto vm_return;
|
2017-03-10 05:09:42 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_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;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint16_t newBase = dst_frame_size(stack) + DST_FRAME_SIZE;
|
|
|
|
dst_frame_args(stack) = newBase;
|
|
|
|
dst_thread_ensure_extra(vm, vm->thread, DST_FRAME_SIZE + arity);
|
|
|
|
stack = dst_thread_stack(vm->thread);
|
|
|
|
dst_frame_size(stack) += DST_FRAME_SIZE + arity;
|
2017-04-19 13:02:12 +00:00
|
|
|
/* Nil stuff */
|
2017-09-09 18:39:51 +00:00
|
|
|
for (i = 0; i < DST_FRAME_SIZE; ++i)
|
|
|
|
stack[newBase + i - DST_FRAME_SIZE].type = DST_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;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_PAR: /* Push array or tuple */
|
2017-04-19 13:02:12 +00:00
|
|
|
{
|
|
|
|
uint32_t count, i, oldsize;
|
2017-09-09 18:39:51 +00:00
|
|
|
const DstValue *data;
|
2017-04-19 13:02:12 +00:00
|
|
|
temp = stack[pc[1]];
|
2017-09-09 18:39:51 +00:00
|
|
|
if (temp.type == DST_TUPLE) {
|
2017-11-01 21:53:43 +00:00
|
|
|
count = dst_tuple_length(temp.as.tuple);
|
|
|
|
data = temp.as.tuple;
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (temp.type == DST_ARRAY){
|
2017-11-01 21:53:43 +00:00
|
|
|
count = temp.as.array->count;
|
|
|
|
data = temp.as.array->data;
|
2017-03-15 05:26:45 +00:00
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_error(vm, "expected array or tuple");
|
2017-03-15 05:26:45 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
oldsize = dst_frame_size(stack);
|
|
|
|
dst_thread_pushnil(vm, vm->thread, count);
|
|
|
|
stack = dst_thread_stack(vm->thread);
|
2017-04-19 13:02:12 +00:00
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
stack[oldsize + i] = data[i];
|
2017-09-09 18:39:51 +00:00
|
|
|
/*dst_frame_size(stack) += count;*/
|
2017-04-19 13:02:12 +00:00
|
|
|
pc += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_CAL: /* Call */
|
2017-04-19 13:02:12 +00:00
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
uint16_t newStackIndex = dst_frame_args(stack);
|
|
|
|
uint16_t size = dst_frame_size(stack);
|
2017-04-19 13:02:12 +00:00
|
|
|
temp = stack[pc[1]];
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_frame_size(stack) = newStackIndex - DST_FRAME_SIZE;
|
|
|
|
dst_frame_ret(stack) = pc[2];
|
|
|
|
dst_frame_pc(stack) = pc + 3;
|
|
|
|
if (newStackIndex < DST_FRAME_SIZE)
|
|
|
|
dst_error(vm, "invalid call instruction");
|
2017-04-19 13:02:12 +00:00
|
|
|
vm->thread->count += newStackIndex;
|
2017-09-09 18:39:51 +00:00
|
|
|
stack = dst_thread_stack(vm->thread);
|
|
|
|
dst_frame_size(stack) = size - newStackIndex;
|
|
|
|
dst_frame_prevsize(stack) = newStackIndex - DST_FRAME_SIZE;
|
|
|
|
dst_frame_callee(stack) = temp;
|
2017-04-19 13:02:12 +00:00
|
|
|
}
|
|
|
|
goto common_function_call;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_TCL: /* Tail call */
|
2017-04-19 13:02:12 +00:00
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
uint16_t newStackIndex = dst_frame_args(stack);
|
|
|
|
uint16_t size = dst_frame_size(stack);
|
2017-04-19 13:02:12 +00:00
|
|
|
uint16_t i;
|
|
|
|
temp = stack[pc[1]];
|
|
|
|
/* Check for closures */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_frame_env(stack)) {
|
|
|
|
DstFuncEnv *env = dst_frame_env(stack);
|
2017-04-19 13:02:12 +00:00
|
|
|
env->thread = NULL;
|
|
|
|
env->stackOffset = size;
|
2017-11-01 21:53:43 +00:00
|
|
|
env->values = dst_mem_resumegc(dst_alloc(vm, sizeof(DstValue) * size));
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_memcpy(env->values, stack, sizeof(DstValue) * size);
|
2017-04-19 13:02:12 +00:00
|
|
|
}
|
|
|
|
if (newStackIndex)
|
|
|
|
for (i = 0; i < size - newStackIndex; ++i)
|
|
|
|
stack[i] = stack[newStackIndex + i];
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_frame_size(stack) = size - newStackIndex;
|
|
|
|
dst_frame_callee(stack) = temp;
|
2017-04-19 13:02:12 +00:00
|
|
|
}
|
|
|
|
goto common_function_call;
|
|
|
|
|
|
|
|
/* Code common to all function calls */
|
|
|
|
common_function_call:
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_frame_args(stack) = 0;
|
|
|
|
dst_frame_env(stack) = NULL;
|
|
|
|
dst_thread_endframe(vm, vm->thread);
|
2017-04-19 13:02:12 +00:00
|
|
|
stack = vm->thread->data + vm->thread->count;
|
2017-09-09 18:39:51 +00:00
|
|
|
temp = dst_frame_callee(stack);
|
|
|
|
if (temp.type == DST_FUNCTION) {
|
2017-11-01 21:53:43 +00:00
|
|
|
pc = temp.as.function->def->byteCode;
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (temp.type == DST_CFUNCTION) {
|
2017-04-19 13:02:12 +00:00
|
|
|
int status;
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->ret.type = DST_NIL;
|
2017-11-01 21:53:43 +00:00
|
|
|
status = temp.as.cfunction(vm);
|
2017-09-09 18:39:51 +00:00
|
|
|
if (status) {
|
|
|
|
goto vm_error;
|
|
|
|
} else {
|
2017-05-09 23:21:30 +00:00
|
|
|
temp = vm->ret;
|
|
|
|
goto vm_return;
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
2017-04-19 13:02:12 +00:00
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_error(vm, DST_EXPECTED_FUNCTION);
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_ARR: /* Array literal */
|
2017-04-12 14:31:50 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t arrayLen = pc[2];
|
2017-09-09 18:39:51 +00:00
|
|
|
DstArray *array = dst_make_array(vm, arrayLen);
|
2017-04-12 14:31:50 +00:00
|
|
|
array->count = arrayLen;
|
|
|
|
for (i = 0; i < arrayLen; ++i)
|
|
|
|
array->data[i] = stack[pc[3 + i]];
|
2017-09-09 18:39:51 +00:00
|
|
|
temp.type = DST_ARRAY;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.array = array;
|
2017-04-12 14:31:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3 + arrayLen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_DIC: /* Table literal */
|
2017-04-12 14:31:50 +00:00
|
|
|
{
|
|
|
|
uint32_t i = 3;
|
|
|
|
uint32_t kvs = pc[2];
|
2017-09-09 18:39:51 +00:00
|
|
|
DstTable *t = dst_make_table(vm, 2 * kvs);
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_mem_suspendgc(t);
|
|
|
|
dst_mem_suspendgc(t->data);
|
2017-04-12 14:31:50 +00:00
|
|
|
kvs = kvs + 3;
|
|
|
|
while (i < kvs) {
|
|
|
|
v1 = stack[pc[i++]];
|
|
|
|
v2 = stack[pc[i++]];
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_table_put(vm, t, v1, v2);
|
2017-03-15 05:26:45 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
temp.type = DST_TABLE;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.table = t;
|
2017-04-12 14:31:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_mem_resumegc(t);
|
|
|
|
dst_mem_resumegc(t->data);
|
2017-04-12 14:31:50 +00:00
|
|
|
pc += kvs;
|
2017-03-15 05:26:45 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-07-02 01:51:16 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_TUP: /* Tuple literal */
|
2017-04-12 14:31:50 +00:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t len = pc[2];
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue *tuple = dst_tuple_begin(vm, len);
|
2017-04-12 14:31:50 +00:00
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
tuple[i] = stack[pc[3 + i]];
|
2017-09-09 18:39:51 +00:00
|
|
|
temp.type = DST_TUPLE;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.tuple = dst_tuple_end(vm, tuple);
|
2017-04-12 14:31:50 +00:00
|
|
|
stack[pc[1]] = temp;
|
|
|
|
pc += 3 + len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_OP_TRN: /* Transfer */
|
2017-05-09 23:21:30 +00:00
|
|
|
temp = stack[pc[2]]; /* The thread */
|
|
|
|
v1 = stack[pc[3]]; /* The value to pass in */
|
2017-09-09 18:39:51 +00:00
|
|
|
if (temp.type != DST_THREAD && temp.type != DST_NIL)
|
|
|
|
dst_error(vm, "expected thread");
|
|
|
|
if (temp.type == DST_NIL && vm->thread->parent) {
|
|
|
|
temp.type = DST_THREAD;
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.thread = vm->thread->parent;
|
2017-07-01 15:22:10 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (temp.type == DST_THREAD) {
|
2017-11-01 21:53:43 +00:00
|
|
|
if (temp.as.thread->status != DST_THREAD_PENDING)
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_error(vm, "can only enter pending thread");
|
2017-07-01 15:17:29 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_frame_ret(stack) = pc[1];
|
|
|
|
vm->thread->status = DST_THREAD_PENDING;
|
|
|
|
dst_frame_pc(stack) = pc + 4;
|
|
|
|
if (temp.type == DST_NIL) {
|
2017-07-01 15:17:29 +00:00
|
|
|
vm->ret = v1;
|
2017-09-09 18:39:51 +00:00
|
|
|
return 0;
|
2017-07-01 15:17:29 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
temp.as.thread->status = DST_THREAD_ALIVE;
|
|
|
|
vm->thread = temp.as.thread;
|
|
|
|
stack = dst_thread_stack(temp.as.thread);
|
2017-09-09 18:39:51 +00:00
|
|
|
if (dst_frame_callee(stack).type != DST_FUNCTION)
|
2017-05-12 01:30:18 +00:00
|
|
|
goto vm_return;
|
2017-09-09 18:39:51 +00:00
|
|
|
stack[dst_frame_ret(stack)] = v1;
|
|
|
|
pc = dst_frame_pc(stack);
|
2017-05-09 23:21:30 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Handle returning from stack frame. Expect return value in temp. */
|
|
|
|
vm_return:
|
2017-09-09 18:39:51 +00:00
|
|
|
stack = dst_thread_popframe(vm, vm->thread);
|
|
|
|
while (vm->thread->count < DST_FRAME_SIZE ||
|
|
|
|
vm->thread->status == DST_THREAD_DEAD ||
|
|
|
|
vm->thread->status == DST_THREAD_ERROR) {
|
|
|
|
vm->thread->status = DST_THREAD_DEAD;
|
2017-05-09 23:21:30 +00:00
|
|
|
if (vm->thread->parent) {
|
|
|
|
vm->thread = vm->thread->parent;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (vm->thread->status == DST_THREAD_ALIVE) {
|
2017-07-04 02:20:39 +00:00
|
|
|
/* If the parent thread is still alive,
|
|
|
|
we are inside a cfunction */
|
|
|
|
vm->ret = temp;
|
2017-09-09 18:39:51 +00:00
|
|
|
return 0;
|
2017-07-04 02:20:39 +00:00
|
|
|
}
|
2017-05-09 23:21:30 +00:00
|
|
|
stack = vm->thread->data + vm->thread->count;
|
|
|
|
} else {
|
|
|
|
vm->ret = temp;
|
2017-09-09 18:39:51 +00:00
|
|
|
return 0;
|
2017-05-09 23:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->thread->status = DST_THREAD_ALIVE;
|
|
|
|
pc = dst_frame_pc(stack);
|
|
|
|
stack[dst_frame_ret(stack)] = temp;
|
2017-04-19 16:56:29 +00:00
|
|
|
continue;
|
2017-03-10 05:09:42 +00:00
|
|
|
|
|
|
|
/* Handle errors from c functions and vm opcodes */
|
|
|
|
vm_error:
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->thread->status = DST_THREAD_ERROR;
|
|
|
|
while (vm->thread->count < DST_FRAME_SIZE ||
|
|
|
|
vm->thread->status == DST_THREAD_DEAD ||
|
|
|
|
vm->thread->status == DST_THREAD_ERROR) {
|
|
|
|
if (vm->thread->parent == NULL)
|
|
|
|
return 1;
|
|
|
|
vm->thread = vm->thread->parent;
|
|
|
|
if (vm->thread->status == DST_THREAD_ALIVE) {
|
2017-07-04 02:20:39 +00:00
|
|
|
/* If the parent thread is still alive,
|
|
|
|
we are inside a cfunction */
|
2017-09-09 18:39:51 +00:00
|
|
|
return 1;
|
2017-07-04 02:20:39 +00:00
|
|
|
}
|
2017-06-30 01:57:09 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->thread->status = DST_THREAD_ALIVE;
|
2017-04-19 13:02:12 +00:00
|
|
|
stack = vm->thread->data + vm->thread->count;
|
2017-09-09 18:39:51 +00:00
|
|
|
stack[dst_frame_ret(stack)] = vm->ret;
|
|
|
|
pc = dst_frame_pc(stack);
|
2017-04-19 13:02:12 +00:00
|
|
|
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-09-09 18:39:51 +00:00
|
|
|
dst_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-09-09 18:39:51 +00:00
|
|
|
int dst_run(Dst *vm, DstValue callee) {
|
2017-07-01 15:17:29 +00:00
|
|
|
int result;
|
|
|
|
if (vm->thread &&
|
2017-09-09 18:39:51 +00:00
|
|
|
(vm->thread->status == DST_THREAD_DEAD ||
|
|
|
|
vm->thread->status == DST_THREAD_ALIVE)) {
|
2017-06-30 01:57:09 +00:00
|
|
|
/* Reuse old thread */
|
2017-09-09 18:39:51 +00:00
|
|
|
dst_thread_reset(vm, vm->thread, callee);
|
2017-07-01 15:17:29 +00:00
|
|
|
} else {
|
|
|
|
/* Create new thread */
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->thread = dst_thread(vm, callee, 64);
|
2017-06-30 01:57:09 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
if (callee.type == DST_CFUNCTION) {
|
|
|
|
vm->ret.type = DST_NIL;
|
2017-11-01 21:53:43 +00:00
|
|
|
result = callee.as.cfunction(vm);
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (callee.type == DST_FUNCTION) {
|
|
|
|
result = dst_continue(vm);
|
2017-05-23 18:48:54 +00:00
|
|
|
} else {
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->ret = dst_string_cv(vm, "expected function");
|
|
|
|
return 1;
|
2017-03-14 19:55:50 +00:00
|
|
|
}
|
2017-07-01 15:17:29 +00:00
|
|
|
/* Handle yields */
|
2017-09-09 18:39:51 +00:00
|
|
|
while (!result && vm->thread->status == DST_THREAD_PENDING) {
|
2017-07-01 15:17:29 +00:00
|
|
|
/* Send back in the value yielded - TODO - do something useful with this */
|
2017-09-09 18:39:51 +00:00
|
|
|
DstValue *stack = dst_thread_stack(vm->thread);
|
|
|
|
stack[dst_frame_ret(stack)] = vm->ret;
|
2017-07-01 15:17:29 +00:00
|
|
|
/* Resume */
|
2017-09-09 18:39:51 +00:00
|
|
|
result = dst_continue(vm);
|
2017-07-01 15:17:29 +00:00
|
|
|
}
|
|
|
|
return result;
|
2017-03-10 05:09:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Setup functions */
|
|
|
|
Dst *dst_init() {
|
2017-11-01 21:53:43 +00:00
|
|
|
Dst *vm = malloc(sizeof(Dst));
|
|
|
|
if (NULL == vm) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
vm->ret.type = DST_NIL;
|
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-07-03 00:51:52 +00:00
|
|
|
* 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-04-15 20:05:59 +00:00
|
|
|
/* Set up the cache */
|
2017-11-01 21:53:43 +00:00
|
|
|
vm->cache = calloc(1, 128 * sizeof(DstValue));
|
2017-04-15 20:05:59 +00:00
|
|
|
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-09-09 18:39:51 +00:00
|
|
|
vm->modules = dst_make_table(vm, 10);
|
|
|
|
vm->registry = dst_make_table(vm, 10);
|
|
|
|
vm->env = dst_make_table(vm, 10);
|
|
|
|
/* Set thread */
|
|
|
|
vm->thread = dst_thread(vm, vm->ret, 100);
|
|
|
|
dst_thread_pushnil(vm, vm->thread, 10);
|
|
|
|
return vm;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear all memory associated with the VM */
|
2017-09-09 18:39:51 +00:00
|
|
|
void dst_deinit(Dst *vm) {
|
|
|
|
dst_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-09-09 18:39:51 +00:00
|
|
|
vm->ret.type = DST_NIL;
|
2017-04-15 20:05:59 +00:00
|
|
|
/* Deinit the cache */
|
2017-11-01 21:53:43 +00:00
|
|
|
free(vm->cache);
|
2017-04-15 20:05:59 +00:00
|
|
|
vm->cache = NULL;
|
|
|
|
vm->cache_count = 0;
|
|
|
|
vm->cache_capacity = 0;
|
|
|
|
vm->cache_deleted = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Free the vm */
|
2017-11-01 21:53:43 +00:00
|
|
|
free(vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|