Commit 03992c48 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

in transition to the new stack frame. BROKEN

parent fe1778ef
......@@ -15,15 +15,14 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
{
switch (expr->type) {
case CT_LIT:
stack_top_a = root_frame_ptr;
push_a(updateT(target, &((LitEntry*) expr)->thunk));
prepare_return(root_frame_ptr,
updateT(target, &((LitEntry*) expr)->thunk));
return;
case CT_VAR:
stack_top_a = root_frame_ptr;
if (expr->local_type == VAR_LOCAL) {
Thunk* thunk = forward_to(target, local(frame_ptr, ((VarEntry*) expr)->index));
if(force) push_a(eval(thunk)); else push_a(thunk);
if(force) thunk = eval(thunk);
prepare_return(root_frame_ptr, thunk);
return;
}else{
Desc* slice = get_slice(((VarEntry*) expr)->f, 0);
......@@ -34,8 +33,9 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
continue;
}
else
{
push_a(updateF(target, get_slice(((VarEntry*) expr)->f, 0)));
{
prepare_return(root_frame_ptr,
updateF(target, get_slice(((VarEntry*) expr)->f, 0)));
return;
}
}
......@@ -45,7 +45,6 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
// TODO: enforce strictness in ADT/Record
VarEntry* var = &((AppEntry*) expr)->var;
Thunk* thunk;
if (var->base.local_type == VAR_LOCAL)
{
......@@ -55,7 +54,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
get_slice(basethunk->desc->type == FT_SLICE ?
((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);
thunk = updateF(target, slice);
Thunk* thunk = updateF(target, slice);
assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);
......@@ -64,29 +63,29 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
}
for (int i = 0; i < expr->nr_args; i++) {
push_a(NULL);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, NULL, false);
thunk->_args[basethunk->desc->arity + i] = pop_a();
}
stack_top_a = root_frame_ptr;
push_a(thunk);
prepare_return(root_frame_ptr, thunk);
return;
}
else
{
Desc* slice = get_slice(var->f, expr->nr_args);
if (force && slice->type == FT_PRIM) {
Thunk args[expr->nr_args];
for (int i = 0; i < expr->nr_args; i++) {
push_a(&args[i]);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, &args[i], true);
}
thunk = ((PrimEntry*) slice)->exec(target);
prepare_return(root_frame_ptr,
((PrimEntry*) slice)->exec(target));
stack_top_a = root_frame_ptr;
push_a(thunk);
return;
}
else if (force && slice->type == FT_FUN) {
......@@ -94,6 +93,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
int new_frame_ptr = stack_top_a;
for (int i = 0; i < expr->nr_args; i++) {
push_a(NULL);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, NULL, is_strict_fun_arg((FunEntry*) slice, i));
}
......@@ -102,17 +102,17 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
continue;
}
else {
thunk = updateF(target, slice);
Thunk* thunk = updateF(target, slice);
assert(thunk->desc->arity == expr->nr_args);
for (int i = 0; i < expr->nr_args; i++) {
push_a(NULL);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, NULL, false);
thunk->_args[i] = pop_a();
}
stack_top_a = root_frame_ptr;
push_a(thunk);
prepare_return(root_frame_ptr, thunk);
return;
}
}
......@@ -120,6 +120,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
}
case CT_SELECT:
{
push_a(NULL);
exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a, NULL, true);
Thunk* pattern = eval(pop_a());
......@@ -128,6 +129,8 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
for (int i = 0; i < expr->nr_cases; i++) {
SelectCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];
assert(pattern->desc->type == FT_ADT);
if (caseEntry->type == SC_CONS) {
// Pattern match
if ((Desc*) caseEntry->cons != pattern->desc) continue;
......@@ -135,11 +138,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
// Put the constructor arguments to the stack if matches
for (int i = 0; i < pattern->desc->arity; i++) {
push_a(pattern->_args[i]);
}
handled = true;
expr = caseEntry->body;
break;
}
}
else if (caseEntry->type == SC_LIT) {
printf("Exec: Unhandled entry type in CT_SELECT (SC_LIT)");
......@@ -163,6 +162,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
Thunk tmp;
tmp.desc = (Desc*) __BOOL__;
push_a(&tmp);
exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a, &tmp, true);
Thunk* cond = eval(pop_a());
......
#ifndef DEBUG_H
#define DEBUG_H
//#define DEBUG
#define DEBUG
#ifndef DEBUG
#define NDEBUG
......
......@@ -29,7 +29,7 @@ int main ( int argc, char *argv[] )
init_desc();
init_prim();
char* input = "..\\tests\\nfib.bsapl";
char* input = "..\\tests\\Braun.bsapl";
if ( argc == 2 )
{
......@@ -84,6 +84,7 @@ int main ( int argc, char *argv[] )
gettimeofday(&t1, NULL);
#endif
push_a(NULL);
exec(expr, stack_top_a, stack_top_a, NULL, true);
Thunk* res = eval(pop_a());
......
......@@ -14,6 +14,7 @@ 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
void init_mem();
void print_stat();
......
......@@ -122,6 +122,7 @@ struct Thunk* eval(Thunk* thunk) {
}
if (thunk->desc->type == FT_FUN) {
push_a(thunk);
int frame_ptr = stack_top_a;
for (int i = 0; i < thunk->desc->arity; i++) {
......@@ -138,7 +139,7 @@ struct Thunk* eval(Thunk* thunk) {
push_a(thunk->_args[i]);
}
((PrimEntry*) thunk->desc)->exec(thunk);
thunk = ((PrimEntry*) thunk->desc)->exec(thunk);
stack_top_a = frame_ptr;
}
......
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