1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-15 05:04:49 +00:00
janet/core/thread.c

195 lines
6.7 KiB
C
Raw Normal View History

2017-03-22 04:27:18 +00:00
#include <gst/gst.h>
2017-03-12 22:23:27 +00:00
/* Create a new thread */
GstThread *gst_thread(Gst *vm, GstValue callee, uint32_t capacity) {
GstThread *thread = gst_alloc(vm, sizeof(GstThread));
2017-03-15 05:26:45 +00:00
GstValue *data, *stack;
if (capacity < GST_FRAME_SIZE) capacity = GST_FRAME_SIZE;
data = gst_alloc(vm, sizeof(GstValue) * capacity);
thread->capacity = capacity;
thread->count = GST_FRAME_SIZE;
thread->data = data;
thread->status = GST_THREAD_PENDING;
stack = data + GST_FRAME_SIZE;
gst_frame_size(stack) = 0;
gst_frame_prevsize(stack) = 0;
gst_frame_ret(stack) = 0;
gst_frame_errloc(stack) = 0;
gst_frame_pc(stack) = NULL;
gst_frame_env(stack) = NULL;
gst_frame_errjmp(stack) = NULL;
gst_frame_callee(stack) = callee;
2017-03-15 05:26:45 +00:00
gst_thread_endframe(vm, thread);
thread->parent = NULL;
2017-03-12 22:23:27 +00:00
return thread;
}
/* Ensure that the thread has enough EXTRA capacity */
void gst_thread_ensure_extra(Gst *vm, GstThread *thread, uint32_t extra) {
2017-03-15 05:26:45 +00:00
GstValue *newData, *stack;
uint32_t usedCapacity, neededCapacity, newCapacity;
stack = thread->data + thread->count;
usedCapacity = thread->count + gst_frame_size(stack) + GST_FRAME_SIZE;
neededCapacity = usedCapacity + extra;
if (thread->capacity >= neededCapacity) return;
newCapacity = 2 * neededCapacity;
newData = gst_alloc(vm, sizeof(GstValue) * newCapacity);
gst_memcpy(newData, thread->data, sizeof(GstValue) * usedCapacity);
thread->data = newData;
thread->capacity = newCapacity;
2017-03-12 22:23:27 +00:00
}
/* Push a value on the current stack frame*/
void gst_thread_push(Gst *vm, GstThread *thread, GstValue x) {
GstValue *stack;
2017-03-15 05:26:45 +00:00
gst_thread_ensure_extra(vm, thread, 1);
stack = thread->data + thread->count;
stack[gst_frame_size(stack)++] = x;
2017-03-12 22:23:27 +00:00
}
/* Push n nils onto the stack */
void gst_thread_pushnil(Gst *vm, GstThread *thread, uint32_t n) {
GstValue *stack, *current, *end;
2017-03-15 05:26:45 +00:00
gst_thread_ensure_extra(vm, thread, n);
stack = thread->data + thread->count;
current = stack + gst_frame_size(stack);
end = current + n;
for (; current < end; ++current) {
current->type = GST_NIL;
}
gst_frame_size(stack) += n;
2017-03-12 22:23:27 +00:00
}
/* Package up extra args after and including n into tuple at n*/
void gst_thread_tuplepack(Gst *vm, GstThread *thread, uint32_t n) {
2017-03-15 05:26:45 +00:00
GstValue *stack = thread->data + thread->count;
uint32_t size = gst_frame_size(stack);
if (n >= size) {
gst_thread_pushnil(vm, thread, n - size + 1);
stack = thread->data + thread->count;
stack[n].type = GST_TUPLE;
2017-04-15 20:05:59 +00:00
stack[n].data.tuple = gst_tuple_end(vm, gst_tuple_begin(vm, 0));
2017-03-15 05:26:45 +00:00
} else {
uint32_t i;
2017-04-15 20:05:59 +00:00
GstValue *tuple = gst_tuple_begin(vm, size - n);
2017-03-15 05:26:45 +00:00
for (i = n; i < size; ++i)
tuple[i - n] = stack[i];
stack[n].type = GST_TUPLE;
2017-04-15 20:05:59 +00:00
stack[n].data.tuple = gst_tuple_end(vm, tuple);
2017-03-15 05:26:45 +00:00
gst_frame_size(stack) = n + 1;
}
2017-03-12 22:23:27 +00:00
}
/* Push a stack frame to a thread, with space for arity arguments. Returns the new
* stack. */
GstValue *gst_thread_beginframe(Gst *vm, GstThread *thread, GstValue callee, uint32_t arity) {
uint32_t frameOffset;
2017-03-12 22:23:27 +00:00
GstValue *oldStack, *newStack;
2017-03-15 05:26:45 +00:00
/* Push the frame */
gst_thread_ensure_extra(vm, thread, GST_FRAME_SIZE + arity + 4);
oldStack = thread->data + thread->count;
frameOffset = gst_frame_size(oldStack) + GST_FRAME_SIZE;
newStack = oldStack + frameOffset;
gst_frame_prevsize(newStack) = gst_frame_size(oldStack);
gst_frame_env(newStack) = NULL;
gst_frame_errjmp(newStack) = NULL;
gst_frame_size(newStack) = 0;
gst_frame_callee(newStack) = callee;
2017-03-15 05:26:45 +00:00
thread->count += frameOffset;
2017-03-15 05:26:45 +00:00
/* Ensure the extra space and initialize to nil */
2017-03-12 22:23:27 +00:00
gst_thread_pushnil(vm, thread, arity);
2017-03-15 05:26:45 +00:00
/* Return ok */
2017-03-12 22:23:27 +00:00
return thread->data + thread->count;
}
/* After pushing arguments to a stack frame created with gst_thread_beginframe, call this
* to finalize the frame before starting a function call. */
void gst_thread_endframe(Gst *vm, GstThread *thread) {
2017-03-15 05:26:45 +00:00
GstValue *stack = thread->data + thread->count;
GstValue callee = gst_frame_callee(stack);
if (callee.type == GST_FUNCTION) {
GstFunction *fn = callee.data.function;
gst_frame_pc(stack) = fn->def->byteCode;
if (fn->def->flags & GST_FUNCDEF_FLAG_VARARG) {
uint32_t arity = fn->def->arity;
gst_thread_tuplepack(vm, thread, arity);
} else {
uint32_t locals = fn->def->locals;
if (gst_frame_size(stack) < locals) {
gst_thread_pushnil(vm, thread, locals - gst_frame_size(stack));
}
}
}
2017-03-12 22:23:27 +00:00
}
/* Pop a stack frame from the thread. Returns the new stack frame, or
* NULL if there are no more frames */
GstValue *gst_thread_popframe(Gst *vm, GstThread *thread) {
2017-03-15 05:26:45 +00:00
GstValue *stack = thread->data + thread->count;
uint32_t prevsize = gst_frame_prevsize(stack);
GstValue *nextstack = stack - GST_FRAME_SIZE - prevsize;
GstFuncEnv *env = gst_frame_env(stack);
2017-03-12 22:23:27 +00:00
2017-03-15 05:26:45 +00:00
/* Check for closures */
2017-03-12 22:23:27 +00:00
if (env != NULL) {
uint32_t size = gst_frame_size(stack);
env->thread = NULL;
env->stackOffset = size;
2017-03-15 05:26:45 +00:00
env->values = gst_alloc(vm, sizeof(GstValue) * size);
2017-03-12 22:23:27 +00:00
gst_memcpy(env->values, stack, sizeof(GstValue) * size);
}
/* Shrink stack */
thread->count -= GST_FRAME_SIZE + prevsize;
2017-03-15 05:26:45 +00:00
/* Check if the stack is empty, and if so, return null */
if (thread->count)
2017-03-12 22:23:27 +00:00
return nextstack;
else
return NULL;
}
/* Move the current stack frame over its parent stack frame, allowing
* for primitive tail calls. */
GstValue *gst_thread_tail(Gst *vm, GstThread *thread) {
GstFuncEnv *env;
2017-03-15 05:26:45 +00:00
GstValue *stack = thread->data + thread->count;
GstValue *nextStack = gst_thread_popframe(vm, thread);
uint32_t i;
2017-03-12 22:23:27 +00:00
2017-03-15 05:26:45 +00:00
if (nextStack == NULL) return NULL;
env = gst_frame_env(nextStack);
2017-03-12 22:23:27 +00:00
2017-03-15 05:26:45 +00:00
/* Check for old closures */
2017-03-12 22:23:27 +00:00
if (env != NULL) {
uint32_t size = gst_frame_size(stack);
env->thread = NULL;
env->stackOffset = size;
2017-03-15 05:26:45 +00:00
env->values = gst_alloc(vm, sizeof(GstValue) * size);
2017-03-12 22:23:27 +00:00
gst_memcpy(env->values, stack, sizeof(GstValue) * size);
}
/* Modify new closure */
env = gst_frame_env(stack);
if (env != NULL) {
2017-03-15 05:26:45 +00:00
env->stackOffset = thread->count;
2017-03-12 22:23:27 +00:00
}
2017-03-15 05:26:45 +00:00
/* Copy over (some of) stack frame. Leave ret and prevsize untouched. */
gst_frame_env(nextStack) = env;
gst_frame_size(nextStack) = gst_frame_size(stack);
gst_frame_pc(nextStack) = gst_frame_pc(stack);
gst_frame_errjmp(nextStack) = gst_frame_errjmp(stack);
gst_frame_errloc(nextStack) = gst_frame_errloc(stack);
gst_frame_callee(nextStack) = gst_frame_callee(stack);
2017-03-12 22:23:27 +00:00
2017-03-15 05:26:45 +00:00
/* Copy stack arguments */
for (i = 0; i < gst_frame_size(nextStack); ++i)
nextStack[i] = stack[i];
2017-03-12 22:23:27 +00:00
2017-03-15 05:26:45 +00:00
return nextStack;
2017-03-12 22:23:27 +00:00
}