Commit 786ff4fa authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

implement tail recursions -> While works now

parent 7285c2ff
......@@ -117,6 +117,7 @@ void set_create_thunk_fun(Code* code)
case CT_APP_PRIM1:
case CT_APP_PRIM2:
case CT_APP_FUN:
case CT_APP_FUN_TR:
case CT_APP_FUN1:
case CT_APP_FUN2:
case CT_APP_THUNK:
......@@ -273,6 +274,24 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
frame_ptr = new_frame_ptr;
continue;
}
case CT_APP_FUN_TR:
{
Desc* slice = ((AppEntry*) expr)->f;
// TODO: B stack?
int new_frame_ptr = stack_top_a;
int argmask = 1;
for (int i = 0; i < expr->nr_args; i++) {
arg_from_code(slice, ((AppEntry*) expr)->args[i]);
}
memcpy(&stack_a[frame_ptr], &stack_a[new_frame_ptr], sizeof(void*) * expr->nr_args);
stack_top_a = frame_ptr + expr->nr_args;
expr = ((FunEntry*) slice)->body;
continue;
}
case CT_APP_THUNK:
{
Desc* slice = ((AppEntry*) expr)->f;
......
......@@ -8,7 +8,7 @@ enum CodeType {
CT_APP_PRIM1,
CT_APP_PRIM2,
CT_APP_THUNK, CT_APP_DYN,
CT_APP_FUN, CT_APP_FUN1, CT_APP_FUN2,
CT_APP_FUN, CT_APP_FUN1, CT_APP_FUN2, CT_APP_FUN_TR,
CT_SELECT_ADT, CT_SELECT_LIT, CT_IF, CT_LET,
CT_THUNK
};
......
......@@ -21,24 +21,6 @@ void gc()
// gc_enabled = 0;
debug_cnt++;
/*
for(int i=0; i<stack_top_a; i++)
{
Thunk* ptr = stack_a[i];
while (ptr->desc == (Desc*) __FORWARD_PTR__)
{
ptr = ptr->_forward_ptr;
}
push_a(ptr);
print(false);
printf("\n");
}
*/
//printf("gc\n");
char* allocptr = heap_base_swap;
char* scanptr = heap_base_swap;
......@@ -61,7 +43,7 @@ void gc()
if(inheap(ptr))
{
if(ptr->desc == NULL)
if(ptr->desc == NULL) // already copied
{
stack_a[i] = ptr->_forward_ptr;
}
......@@ -116,25 +98,6 @@ void gc()
}
}
}
/*
for(int i=0; i<stack_top_a; i++)
{
Thunk* ptr = stack_a[i];
while (ptr->desc == (Desc*) __FORWARD_PTR__)
{
ptr = ptr->_forward_ptr;
}
push_a(ptr);
print(false);
printf("\n");
}
printf("done\n");
fflush(stdout);
*/
heap_curr = allocptr;
char *tmp = heap_base_curr;
......
......@@ -324,9 +324,10 @@ Code* parseSelectBody(char **ptr, Code* fallback, int fallback_nrargs);
* It is very messy, because it handles 3 cases:
* 1. Dynamic app: the function part is a local variable or argument
* 2. Static app, where the function is a zero arg data constructor, or a non-zero parameter function with zero arguments
* 3. Other static cases
* 3. Tail recursive
* 4. Other static cases
*/
Code* parseApp(char **ptr, bool dynamic) {
Code* parseApp(char **ptr, bool dynamic, bool tr) {
int nrArgs;
if (!parseInt(ptr, &nrArgs)) return 0;
......@@ -436,7 +437,7 @@ Code* parseApp(char **ptr, bool dynamic) {
entry->base.arg_pattern = 0;
}
}
else if(desc->type == FT_FUN)
else if(desc->type == FT_FUN && !tr)
{
if(nrArgs == 1)
{
......@@ -451,6 +452,10 @@ Code* parseApp(char **ptr, bool dynamic) {
entry->base.type = CT_APP_FUN;
}
}
else if(desc->type == FT_FUN && tr)
{
entry->base.type = CT_APP_FUN_TR;
}
else
{
entry->base.type = CT_APP_THUNK;
......@@ -624,11 +629,11 @@ Code* parseTerm(char **ptr) {
case 'V': // Variable
return (Code*) parseVar(ptr, NULL);
case 'A': // Static application
return (Code*) parseApp(ptr, false);
return (Code*) parseApp(ptr, false, false);
case 'T': // Tail recursive application
return (Code*) parseApp(ptr, false);
return (Code*) parseApp(ptr, false, true);
case 'D': // Dynamic application
return (Code*) parseApp(ptr, true);
return (Code*) parseApp(ptr, true, false);
case 'S': // Select
return (Code*) parseSelect(ptr, NULL, 0);
case 'I': // If
......
[40]
\ No newline at end of file
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