Commit d94c62b2 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

fix the problem of the previous commit by reducing the stack usage

parent 03992c48
......@@ -13,29 +13,40 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
while(1)
{
assert(stack_top_a < STACK_SIZE_A);
switch (expr->type) {
case CT_LIT:
prepare_return(root_frame_ptr,
updateT(target, &((LitEntry*) expr)->thunk));
set_return(root_frame_ptr, updateT(target, &((LitEntry*) expr)->thunk));
destroy_stack_frame(root_frame_ptr);
return;
case CT_VAR:
if (expr->local_type == VAR_LOCAL) {
Thunk* thunk = forward_to(target, local(frame_ptr, ((VarEntry*) expr)->index));
// Destroy stack frame before eval, it is not needed any more
// Greatly reduces stack usage
destroy_stack_frame(root_frame_ptr);
// TODO: inline eval
if(force) thunk = eval(thunk);
prepare_return(root_frame_ptr, thunk);
set_return(root_frame_ptr, thunk);
return;
}else{
// Safe to destroy, the next call has no arguments
destroy_stack_frame(root_frame_ptr);
Desc* slice = get_slice(((VarEntry*) expr)->f, 0);
if(force && slice->type == FT_FUN)
if(force && slice->type == FT_FUN)
{
expr = ((FunEntry*)slice)->body;
continue;
}
else
{
prepare_return(root_frame_ptr,
updateF(target, get_slice(((VarEntry*) expr)->f, 0)));
set_return(root_frame_ptr, updateF(target, slice));
return;
}
}
......@@ -48,6 +59,8 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
if (var->base.local_type == VAR_LOCAL)
{
// TODO: force
Thunk* basethunk = eval(local(frame_ptr, var->index));
Desc* slice =
......@@ -68,7 +81,8 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
thunk->_args[basethunk->desc->arity + i] = pop_a();
}
prepare_return(root_frame_ptr, thunk);
set_return(root_frame_ptr, thunk);
destroy_stack_frame(root_frame_ptr);
return;
}
else
......@@ -83,9 +97,9 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, &args[i], true);
}
prepare_return(root_frame_ptr,
set_return(root_frame_ptr,
((PrimEntry*) slice)->exec(target));
destroy_stack_frame(root_frame_ptr);
return;
}
else if (force && slice->type == FT_FUN) {
......@@ -96,7 +110,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
push_a(NULL);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, NULL, is_strict_fun_arg((FunEntry*) slice, i));
}
expr = ((FunEntry*) slice)->body;
frame_ptr = new_frame_ptr;
continue;
......@@ -112,7 +126,8 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
thunk->_args[i] = pop_a();
}
prepare_return(root_frame_ptr, thunk);
set_return(root_frame_ptr, thunk);
destroy_stack_frame(root_frame_ptr);
return;
}
}
......
......@@ -29,7 +29,7 @@ int main ( int argc, char *argv[] )
init_desc();
init_prim();
char* input = "..\\tests\\Braun.bsapl";
char* input = "..\\tests\\nfib.bsapl";
if ( argc == 2 )
{
......
......@@ -14,7 +14,8 @@ extern Thunk* stack_a[STACK_SIZE_A];
#define push_a(r) stack_a[stack_top_a++]=(r)
#define local(base, idx) stack_a[base+idx-1]
#define prepare_return(base, r) stack_a[base-1]=(r); stack_top_a = base
#define set_return(base, r) stack_a[base-1]=(r)
#define destroy_stack_frame(base) stack_top_a = base
void init_mem();
void print_stat();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment