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

simple letrec works

parent 4f6c6a9b
......@@ -38,25 +38,24 @@
} \
else \
{ \
push_a(arg->create_thunk(arg, frame_ptr)); \
arg->create_thunk(arg, &stack_a[stack_top_a++], frame_ptr); \
} \
argmask <<= 1;
struct Thunk* create_thunk_app_static(Code* expr, int frame_ptr)
void create_thunk_app_static(Code* expr, Thunk** target, int frame_ptr)
{
Thunk* thunk = (Thunk*) alloc_heap(((AppEntry*) expr)->f->thunk_size);
*target = thunk;
thunk->desc = ((AppEntry*) expr)->f;
assert(thunk->desc->arity == expr->nr_args);
for (int i = 0; i < expr->nr_args; i++) {
thunk->_args[i] = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
}
return thunk;
((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[i], frame_ptr);
}
}
struct Thunk* create_thunk_app_dyn(Code* expr, int frame_ptr)
void create_thunk_app_dyn(Code* expr, Thunk** target, int frame_ptr)
{
push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));
......@@ -73,44 +72,42 @@ struct Thunk* create_thunk_app_dyn(Code* expr, int frame_ptr)
Thunk* thunk = (Thunk*) alloc_heap(slice->thunk_size);
thunk->desc = slice;
*target = thunk;
assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);
memcpy(&thunk->_args, &basethunk->_args, sizeof(Thunk*) * basethunk->desc->arity);
for (int i = 0; i < expr->nr_args; i++) {
thunk->_args[basethunk->desc->arity + i]
= ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
}
return thunk;
((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[basethunk->desc->arity + i], frame_ptr);
}
}
struct Thunk* create_thunk_var(Code* expr, int frame_ptr)
void create_thunk_var(Code* expr, Thunk** target, int frame_ptr)
{
return local(frame_ptr, ((VarEntry*) expr)->index);
*target = local(frame_ptr, ((VarEntry*) expr)->index);
}
struct Thunk* create_thunk_var_unboxed(Code* expr, int frame_ptr)
void create_thunk_var_unboxed(Code* expr, Thunk** target, int frame_ptr)
{
Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
if(instackb(arg))
{
// The likely case
Thunk* target = (Thunk*) alloc_heap(sizeof (Thunk));
memcpy(target, arg, sizeof(Thunk));
return target;
Thunk* newthunk = (Thunk*) alloc_heap(sizeof (Thunk));
memcpy(newthunk, arg, sizeof(Thunk));
*target = newthunk;
}
else
{
return arg;
*target = arg;
}
}
struct Thunk* create_thunk_thunk(Code* expr, int frame_ptr)
void create_thunk_thunk(Code* expr, Thunk** target, int frame_ptr)
{
return &((ThunkEntry*) expr)->thunk;
*target = &((ThunkEntry*) expr)->thunk;
}
void set_create_thunk_fun(Code* code)
......@@ -299,9 +296,8 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
assert(thunk->desc->arity == expr->nr_args);
for (int i = 0; i < expr->nr_args; i++) {
thunk->_args[i]
= ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
for (int i = 0; i < expr->nr_args; i++) {
((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[i], frame_ptr);
}
destroy_stack_frame(root_frame_ptr);
......@@ -383,8 +379,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
memcpy(&thunk->_args, &(*bt)->_args, sizeof(Thunk*) * (*bt)->desc->arity);
for (int i = 0; i < expr->nr_args; i++) {
thunk->_args[(*bt)->desc->arity + i]
= ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[(*bt)->desc->arity + i], frame_ptr);
}
destroy_stack_frame(root_frame_ptr);
......@@ -596,7 +591,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
// Normal
if(binding->type == 0)
{
push_a(binding->body->create_thunk(binding->body, frame_ptr));
binding->body->create_thunk(binding->body, &stack_a[stack_top_a++], frame_ptr);
}
else // strict (including unboxed))
{
......
......@@ -20,7 +20,7 @@ struct Code {
unsigned int nr_bindings : 8; // used in LetEntry
unsigned int strict : 1; // used in VarEntry
unsigned int arg_pattern : 3;
struct Thunk* (*create_thunk)(Code*, int);
void (*create_thunk)(Code*, struct Thunk**, int);
};
struct ThunkEntry {
......
#ifndef DEBUG_H
#define DEBUG_H
#define DEBUG
//#define DEBUG
#define BENCHMARK
#ifndef DEBUG
......
[_predefined._Cons [1] [_predefined._Cons [1] [_predefined._Cons [1] [_predefined._Cons [1] [_predefined._Cons [1] [_predefined._Nil]]]]]]
\ No newline at end of file
main = lettests.Start
lettests.Start = StdList.take 5 lettests.test5
lettests.test5 = let x_1_0 = _predefined._Cons 1 x_1_0 in x_1_0
:: _predefined._List = _predefined._Cons a1 a2 | _predefined._Nil
StdList.take !n_0::I xs_1 = if (not (ltI 0 n_0)) _predefined._Nil (StdList.take_88 n_0 xs_1)
StdList.take_88 !n_0::I !_x_1 = select _x_1 (_predefined._Cons x_1_0 xs_1_1 -> if (not (ltI 1 n_0)) (_predefined._Cons x_1_0 _predefined._Nil) (_predefined._Cons x_1_0 (StdList.take_88 (subI n_0 1) xs_1_1))) (_predefined._Nil -> _predefined._Nil)
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