mirror of
https://github.com/janet-lang/janet
synced 2025-11-18 08:15:13 +00:00
Use flatter representation for function and environments.
This commit is contained in:
@@ -217,7 +217,7 @@ DstFuncDef *dst_funcdef_alloc() {
|
|||||||
def->defs_length = 0;
|
def->defs_length = 0;
|
||||||
def->constants_length = 0;
|
def->constants_length = 0;
|
||||||
def->bytecode_length = 0;
|
def->bytecode_length = 0;
|
||||||
def->environments_length = 1;
|
def->environments_length = 0;
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,7 +225,7 @@ DstFuncDef *dst_funcdef_alloc() {
|
|||||||
DstFunction *dst_thunk(DstFuncDef *def) {
|
DstFunction *dst_thunk(DstFuncDef *def) {
|
||||||
DstFunction *func = dst_gcalloc(DST_MEMORY_FUNCTION, sizeof(DstFunction));
|
DstFunction *func = dst_gcalloc(DST_MEMORY_FUNCTION, sizeof(DstFunction));
|
||||||
func->def = def;
|
func->def = def;
|
||||||
func->envs = NULL;
|
dst_assert(def->environments_length == 0, "tried to create thunk that needs upvalues");
|
||||||
return func;
|
return func;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -230,6 +230,7 @@ void dst_fiber_cframe(DstFiber *fiber) {
|
|||||||
newframe->prevframe = oldframe;
|
newframe->prevframe = oldframe;
|
||||||
newframe->pc = NULL;
|
newframe->pc = NULL;
|
||||||
newframe->func = NULL;
|
newframe->func = NULL;
|
||||||
|
newframe->env = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pop a stack frame from the fiber. Returns the new stack frame, or
|
/* Pop a stack frame from the fiber. Returns the new stack frame, or
|
||||||
|
|||||||
@@ -166,10 +166,9 @@ static void dst_mark_function(DstFunction *func) {
|
|||||||
return;
|
return;
|
||||||
dst_gc_mark(func);
|
dst_gc_mark(func);
|
||||||
numenvs = func->def->environments_length;
|
numenvs = func->def->environments_length;
|
||||||
if (NULL != func->envs)
|
for (i = 0; i < numenvs; ++i) {
|
||||||
for (i = 0; i < numenvs; ++i)
|
dst_mark_funcenv(func->envs[i]);
|
||||||
if (NULL != func->envs[i])
|
}
|
||||||
dst_mark_funcenv(func->envs[i]);
|
|
||||||
dst_mark_funcdef(func->def);
|
dst_mark_funcdef(func->def);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,6 +203,7 @@ static void dst_deinit_block(DstGCMemoryHeader *block) {
|
|||||||
DstAbstractHeader *h = (DstAbstractHeader *)mem;
|
DstAbstractHeader *h = (DstAbstractHeader *)mem;
|
||||||
switch (block->flags & DST_MEM_TYPEBITS) {
|
switch (block->flags & DST_MEM_TYPEBITS) {
|
||||||
default:
|
default:
|
||||||
|
case DST_MEMORY_FUNCTION:
|
||||||
break; /* Do nothing for non gc types */
|
break; /* Do nothing for non gc types */
|
||||||
case DST_MEMORY_SYMBOL:
|
case DST_MEMORY_SYMBOL:
|
||||||
dst_symbol_deinit((const uint8_t *)mem + 2 * sizeof(int32_t));
|
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:
|
case DST_MEMORY_BUFFER:
|
||||||
dst_buffer_deinit((DstBuffer *) mem);
|
dst_buffer_deinit((DstBuffer *) mem);
|
||||||
break;
|
break;
|
||||||
case DST_MEMORY_FUNCTION:
|
|
||||||
free(((DstFunction *)mem)->envs);
|
|
||||||
break;
|
|
||||||
case DST_MEMORY_ABSTRACT:
|
case DST_MEMORY_ABSTRACT:
|
||||||
if (h->type->gc) {
|
if (h->type->gc) {
|
||||||
if (h->type->gc((void *)(h + 1), h->size)) {
|
if (h->type->gc((void *)(h + 1), h->size)) {
|
||||||
|
|||||||
@@ -495,36 +495,30 @@ static void *op_lookup[255] = {
|
|||||||
{
|
{
|
||||||
DstFuncDef *fd;
|
DstFuncDef *fd;
|
||||||
DstFunction *fn;
|
DstFunction *fn;
|
||||||
|
int32_t elen;
|
||||||
vm_assert((int32_t)oparg(2, 0xFFFF) < func->def->defs_length, "invalid funcdef");
|
vm_assert((int32_t)oparg(2, 0xFFFF) < func->def->defs_length, "invalid funcdef");
|
||||||
fd = func->def->defs[(int32_t)oparg(2, 0xFFFF)];
|
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;
|
int32_t i;
|
||||||
if (elen) {
|
for (i = 0; i < elen; ++i) {
|
||||||
int32_t i;
|
int32_t inherit = fd->environments[i];
|
||||||
fn->envs = malloc(sizeof(DstFuncEnv *) * elen);
|
if (inherit == -1) {
|
||||||
if (NULL == fn->envs) {
|
DstStackFrame *frame = (DstStackFrame *)stack - 1;
|
||||||
DST_OUT_OF_MEMORY;
|
if (!frame->env) {
|
||||||
}
|
/* Lazy capture of current stack frame */
|
||||||
for (i = 0; i < elen; ++i) {
|
DstFuncEnv *env = dst_gcalloc(DST_MEMORY_FUNCENV, sizeof(DstFuncEnv));
|
||||||
int32_t inherit = fd->environments[i];
|
env->offset = dst_vm_fiber->frame;
|
||||||
if (inherit == -1) {
|
env->as.fiber = dst_vm_fiber;
|
||||||
DstStackFrame *frame = (DstStackFrame *)stack - 1;
|
env->length = func->def->slotcount;
|
||||||
if (!frame->env) {
|
frame->env = 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];
|
|
||||||
}
|
}
|
||||||
|
fn->envs[i] = frame->env;
|
||||||
|
} else {
|
||||||
|
fn->envs[i] = func->envs[inherit];
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
fn->envs = NULL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stack[oparg(1, 0xFF)] = dst_wrap_function(fn);
|
stack[oparg(1, 0xFF)] = dst_wrap_function(fn);
|
||||||
|
|||||||
@@ -402,8 +402,7 @@ struct DstFuncEnv {
|
|||||||
/* A function */
|
/* A function */
|
||||||
struct DstFunction {
|
struct DstFunction {
|
||||||
DstFuncDef *def;
|
DstFuncDef *def;
|
||||||
/* Consider allocating envs with entire function struct */
|
DstFuncEnv *envs[];
|
||||||
DstFuncEnv **envs;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Defines an abstract type */
|
/* Defines an abstract type */
|
||||||
|
|||||||
Reference in New Issue
Block a user