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

+ remove "target" parameter of exec. it is on the A stack now

+ primitive functions set return value directly instead of returning it
parent d94c62b2
......@@ -7,7 +7,7 @@
#include "mem.h"
#include "desc.h"
void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool force)
void exec(Code* expr, int frame_ptr, int root_frame_ptr, bool force)
{
assert(expr != NULL);
......@@ -17,12 +17,12 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
switch (expr->type) {
case CT_LIT:
set_return(root_frame_ptr, updateT(target, &((LitEntry*) expr)->thunk));
set_return(root_frame_ptr, updateT(get_dst(root_frame_ptr), &((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));
Thunk* thunk = forward_to(get_dst(root_frame_ptr), local(frame_ptr, ((VarEntry*) expr)->index));
// Destroy stack frame before eval, it is not needed any more
// Greatly reduces stack usage
......@@ -46,7 +46,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
}
else
{
set_return(root_frame_ptr, updateF(target, slice));
set_return(root_frame_ptr, updateF(get_dst(root_frame_ptr), slice));
return;
}
}
......@@ -67,7 +67,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* thunk = updateF(target, slice);
Thunk* thunk = updateF(get_dst(root_frame_ptr), slice);
assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);
......@@ -77,7 +77,7 @@ 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);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, false);
thunk->_args[basethunk->desc->arity + i] = pop_a();
}
......@@ -94,11 +94,10 @@ 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(&args[i]);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, &args[i], true);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, true);
}
set_return(root_frame_ptr,
((PrimEntry*) slice)->exec(target));
((PrimEntry*) slice)->exec(root_frame_ptr);
destroy_stack_frame(root_frame_ptr);
return;
}
......@@ -108,7 +107,7 @@ 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, is_strict_fun_arg((FunEntry*) slice, i));
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, is_strict_fun_arg((FunEntry*) slice, i));
}
expr = ((FunEntry*) slice)->body;
......@@ -116,13 +115,13 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
continue;
}
else {
Thunk* thunk = updateF(target, slice);
Thunk* thunk = updateF(get_dst(root_frame_ptr), 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);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, false);
thunk->_args[i] = pop_a();
}
......@@ -136,7 +135,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);
exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a, true);
Thunk* pattern = eval(pop_a());
bool handled = false;
......@@ -178,7 +177,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool for
tmp.desc = (Desc*) __BOOL__;
push_a(&tmp);
exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a, &tmp, true);
exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a, true);
Thunk* cond = eval(pop_a());
if (readB(cond)) {
......
......@@ -72,6 +72,6 @@ struct IfEntry {
struct Code* fexpr;
};
void exec(Code* expr, int frame_ptr, int root_frame_ptr, struct Thunk* target, bool force);
void exec(Code* expr, int frame_ptr, int root_frame_ptr, bool force);
#endif // __CODE_H
\ No newline at end of file
......@@ -52,7 +52,7 @@ struct RecordEntry {
struct PrimEntry {
struct Desc base;
int strictness;
struct Thunk* (*exec)(Thunk*);
void (*exec)(int frame_ptr);
char name[];
};
......
......@@ -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 )
{
......@@ -85,7 +85,7 @@ int main ( int argc, char *argv[] )
#endif
push_a(NULL);
exec(expr, stack_top_a, stack_top_a, NULL, true);
exec(expr, stack_top_a, stack_top_a, true);
Thunk* res = eval(pop_a());
#ifdef DEBUG
......
......@@ -16,6 +16,7 @@ extern Thunk* stack_a[STACK_SIZE_A];
#define local(base, idx) stack_a[base+idx-1]
#define set_return(base, r) stack_a[base-1]=(r)
#define destroy_stack_frame(base) stack_top_a = base
#define get_dst(base) stack_a[base-1]
void init_mem();
void print_stat();
......
......@@ -7,43 +7,43 @@
#define arg(idx) stack_a[stack_top_a - idx]
struct Thunk* __add(Thunk* target) {
return updateI(target, readI(arg(2)) + readI(arg(1)));
void __add(int frame_ptr) {
set_return(frame_ptr, updateI(get_dst(frame_ptr), readI(arg(2)) + readI(arg(1))));
}
struct Thunk* __sub(Thunk* target) {
return updateI(target, readI(arg(2)) - readI(arg(1)));
void __sub(int frame_ptr) {
set_return(frame_ptr, updateI(get_dst(frame_ptr), readI(arg(2)) - readI(arg(1))));
}
struct Thunk* __gt(Thunk* target) {
return updateB(target, readI(arg(2)) > readI(arg(1)));
void __gt(int frame_ptr) {
set_return(frame_ptr, updateB(get_dst(frame_ptr), readI(arg(2)) > readI(arg(1))));
}
struct Thunk* __lt(Thunk* target) {
return updateB(target, readI(arg(2)) < readI(arg(1)));
void __lt(int frame_ptr) {
set_return(frame_ptr, updateB(get_dst(frame_ptr), readI(arg(2)) < readI(arg(1))));
}
struct Thunk* __eqI(Thunk* target) {
void __eqI(int frame_ptr) {
Thunk* arg1 = arg(2);
Thunk* arg2 = arg(1);
return updateB(target, readI(arg1) == readI(arg2));
set_return(frame_ptr, updateB(get_dst(frame_ptr), readI(arg1) == readI(arg2)));
}
struct Thunk* __eqB(Thunk* target) {
void __eqB(int frame_ptr) {
Thunk* arg1 = arg(2);
Thunk* arg2 = arg(1);
return updateB(target, readB(arg1) == readB(arg2));
set_return(frame_ptr, updateB(get_dst(frame_ptr), readB(arg1) == readB(arg2)));
}
struct Thunk* __not(Thunk* target) {
void __not(int frame_ptr) {
Thunk* arg1 = arg(1);
return updateB(target, !readB(arg1));
set_return(frame_ptr, updateB(get_dst(frame_ptr), !readB(arg1)));
}
void add_prim(int arity, int strictness, char* name, Thunk* (*exec)(Thunk*)) {
void add_prim(int arity, int strictness, char* name, void (*exec)(int)) {
int nameLength = strlen(name);
// before the PrimEntry there are "arity" number of SliceEntries
......
......@@ -129,19 +129,21 @@ struct Thunk* eval(Thunk* thunk) {
// TODO: handle strictness
push_a(thunk->_args[i]);
}
exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr, thunk, true);
exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr, true);
thunk = pop_a();
}
else if(thunk->desc->type == FT_PRIM) {
push_a(thunk);
int frame_ptr = stack_top_a;
for (int i = 0; i < thunk->desc->arity; i++) {
push_a(thunk->_args[i]);
}
thunk = ((PrimEntry*) thunk->desc)->exec(thunk);
((PrimEntry*) thunk->desc)->exec(frame_ptr);
stack_top_a = frame_ptr;
thunk = pop_a();
}
return thunk;
......
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