1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-20 07:34:49 +00:00

Use flatter representation for function and environments.

This commit is contained in:
Calvin Rose 2018-02-13 16:14:55 -05:00
parent 2f4fd23884
commit 1551bf6b48
5 changed files with 27 additions and 36 deletions

View File

@ -217,7 +217,7 @@ DstFuncDef *dst_funcdef_alloc() {
def->defs_length = 0;
def->constants_length = 0;
def->bytecode_length = 0;
def->environments_length = 1;
def->environments_length = 0;
return def;
}
@ -225,7 +225,7 @@ DstFuncDef *dst_funcdef_alloc() {
DstFunction *dst_thunk(DstFuncDef *def) {
DstFunction *func = dst_gcalloc(DST_MEMORY_FUNCTION, sizeof(DstFunction));
func->def = def;
func->envs = NULL;
dst_assert(def->environments_length == 0, "tried to create thunk that needs upvalues");
return func;
}

View File

@ -230,6 +230,7 @@ void dst_fiber_cframe(DstFiber *fiber) {
newframe->prevframe = oldframe;
newframe->pc = NULL;
newframe->func = NULL;
newframe->env = NULL;
}
/* Pop a stack frame from the fiber. Returns the new stack frame, or

View File

@ -166,10 +166,9 @@ static void dst_mark_function(DstFunction *func) {
return;
dst_gc_mark(func);
numenvs = func->def->environments_length;
if (NULL != func->envs)
for (i = 0; i < numenvs; ++i)
if (NULL != func->envs[i])
dst_mark_funcenv(func->envs[i]);
for (i = 0; i < numenvs; ++i) {
dst_mark_funcenv(func->envs[i]);
}
dst_mark_funcdef(func->def);
}
@ -204,6 +203,7 @@ static void dst_deinit_block(DstGCMemoryHeader *block) {
DstAbstractHeader *h = (DstAbstractHeader *)mem;
switch (block->flags & DST_MEM_TYPEBITS) {
default:
case DST_MEMORY_FUNCTION:
break; /* Do nothing for non gc types */
case DST_MEMORY_SYMBOL:
dst_symbol_deinit((const uint8_t *)mem + 2 * sizeof(int32_t));
@ -220,9 +220,6 @@ static void dst_deinit_block(DstGCMemoryHeader *block) {
case DST_MEMORY_BUFFER:
dst_buffer_deinit((DstBuffer *) mem);
break;
case DST_MEMORY_FUNCTION:
free(((DstFunction *)mem)->envs);
break;
case DST_MEMORY_ABSTRACT:
if (h->type->gc) {
if (h->type->gc((void *)(h + 1), h->size)) {

View File

@ -495,36 +495,30 @@ static void *op_lookup[255] = {
{
DstFuncDef *fd;
DstFunction *fn;
int32_t elen;
vm_assert((int32_t)oparg(2, 0xFFFF) < func->def->defs_length, "invalid funcdef");
fd = func->def->defs[(int32_t)oparg(2, 0xFFFF)];
fn = dst_thunk(fd);
elen = fd->environments_length;
fn = dst_gcalloc(DST_MEMORY_FUNCTION, sizeof(DstFunction) + (elen * sizeof(DstFuncEnv *)));
fn->def = fd;
{
int32_t elen = fd->environments_length;
if (elen) {
int32_t i;
fn->envs = malloc(sizeof(DstFuncEnv *) * elen);
if (NULL == fn->envs) {
DST_OUT_OF_MEMORY;
}
for (i = 0; i < elen; ++i) {
int32_t inherit = fd->environments[i];
if (inherit == -1) {
DstStackFrame *frame = (DstStackFrame *)stack - 1;
if (!frame->env) {
/* Lazy capture of current stack frame */
DstFuncEnv *env = dst_gcalloc(DST_MEMORY_FUNCENV, sizeof(DstFuncEnv));
env->offset = dst_vm_fiber->frame;
env->as.fiber = dst_vm_fiber;
env->length = func->def->slotcount;
frame->env = env;
}
fn->envs[i] = frame->env;
} else {
fn->envs[i] = func->envs[inherit];
int32_t i;
for (i = 0; i < elen; ++i) {
int32_t inherit = fd->environments[i];
if (inherit == -1) {
DstStackFrame *frame = (DstStackFrame *)stack - 1;
if (!frame->env) {
/* Lazy capture of current stack frame */
DstFuncEnv *env = dst_gcalloc(DST_MEMORY_FUNCENV, sizeof(DstFuncEnv));
env->offset = dst_vm_fiber->frame;
env->as.fiber = dst_vm_fiber;
env->length = func->def->slotcount;
frame->env = env;
}
fn->envs[i] = frame->env;
} else {
fn->envs[i] = func->envs[inherit];
}
} else {
fn->envs = NULL;
}
}
stack[oparg(1, 0xFF)] = dst_wrap_function(fn);

View File

@ -402,8 +402,7 @@ struct DstFuncEnv {
/* A function */
struct DstFunction {
DstFuncDef *def;
/* Consider allocating envs with entire function struct */
DstFuncEnv **envs;
DstFuncEnv *envs[];
};
/* Defines an abstract type */