Commit 68bb16b1 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

first stub on overwriting thunks

parent f76c202c
......@@ -6,7 +6,7 @@
#include "thunk.h"
#include "mem.h"
struct Thunk* exec(Code* expr, int frame_ptr)
struct Thunk* exec(Code* expr, int frame_ptr, Thunk* target)
{
switch(expr->type)
{
......@@ -14,8 +14,15 @@ struct Thunk* exec(Code* expr, int frame_ptr)
switch(expr->local_type)
{
case LIT_INT:
return createI(((LitEntry*) expr)->_int);
break;
if(target == NULL)
{
return createI(((LitEntry*) expr)->_int);
}
else
{
updateI(target, ((LitEntry*) expr)->_int);
return target;
}
default:
printf("Exec: Unhandled LIT type");
exit(-1);
......@@ -47,7 +54,7 @@ struct Thunk* exec(Code* expr, int frame_ptr)
thunk = createF(var->f, expr->nr_args);
for(int i=0; i<thunk->desc->arity; i++)
{
thunk->_args[i] = exec(((AppEntry*)expr)->args[i], frame_ptr);
thunk->_args[i] = exec(((AppEntry*)expr)->args[i], frame_ptr, NULL);
}
return thunk;
case VAR_ARG:
......@@ -64,7 +71,7 @@ struct Thunk* exec(Code* expr, int frame_ptr)
for(int i=0; i<expr->nr_args; i++)
{
thunk->_args[basethunk->desc->arity + i] = exec(((AppEntry*)expr)->args[i], frame_ptr);
thunk->_args[basethunk->desc->arity + i] = exec(((AppEntry*)expr)->args[i], frame_ptr, NULL);
}
return thunk;
default:
......
......@@ -61,6 +61,6 @@ struct AppEntry
struct Code* args[];
};
struct Thunk* exec(Code* expr, int frame_ptr);
struct Thunk* exec(Code* expr, int frame_ptr, Thunk* target);
#endif // __CODE_H
\ No newline at end of file
......@@ -71,7 +71,7 @@ struct PrimEntry
{
struct Desc base;
int strictness;
struct Thunk* (*exec)();
void (*exec)(Thunk*);
char name[];
};
......
......@@ -55,11 +55,9 @@ int main()
char *exprstream = "VF13 example.Start";
Code* expr = parseTerm(&exprstream);
Thunk* res = exec(expr, stack_top);
Thunk* res = exec(expr, stack_top, NULL);
print(eval(res));
// add_fun("jimbo", (Desc*) "artist");
// char *tval = (char*) find_fun("jimbo");
// printf ("shortcut tval for jimbo = %s\n", tval);
print_stat();
}
#include <stdlib.h>
#include <stdio.h>
#include "mem.h"
......@@ -9,6 +10,14 @@ int heap_alloc;
int stack_top;
Thunk* stack[STACK_SIZE];
void print_stat()
{
printf("\n\nallocation:\n");
printf("desc: %d\n", desc_alloc);
printf("code: %d\n", code_alloc);
printf("heap: %d\n", heap_alloc);
}
void init_mem()
{
desc_alloc = 0;
......
......@@ -14,6 +14,7 @@ extern Thunk* stack[STACK_SIZE];
#define pop(i) stack_top -= i
void init_mem();
void print_stat();
// TODO: inline
void* alloc_desc(int size);
......
......@@ -6,12 +6,12 @@
#include "thunk.h"
#include "mem.h"
Thunk* __add()
void __add(Thunk* target)
{
return createI(readI(eval(stack[stack_top - 2])) + readI(eval(stack[stack_top - 1])));
return updateI(target, readI(eval(stack[stack_top - 2])) + readI(eval(stack[stack_top - 1])));
}
PrimEntry* add_prim(int arity, int strictness, char* name, Thunk* (*exec)())
PrimEntry* add_prim(int arity, int strictness, char* name, void (*exec)(Thunk*))
{
int nameLength = strlen(name);
......
......@@ -14,6 +14,13 @@ struct Thunk* createI(int i)
return thunk;
}
void updateI(Thunk* target, int i)
{
// always can be overwritten with boxed integer
target->desc = (Desc*) __INT__;
target->_int = i;
}
int readI(Thunk* thunk)
{
if(thunk->desc != (Desc*) __INT__)
......@@ -51,7 +58,7 @@ Thunk* eval(Thunk* thunk)
{
pushs(thunk->_args[i]);
}
thunk = exec(((FunEntry*) thunk->desc)->body, stack_top - 1);
thunk = exec(((FunEntry*) thunk->desc)->body, stack_top - 1, thunk);
stack_top = old_top;
break;
......@@ -63,7 +70,7 @@ Thunk* eval(Thunk* thunk)
pushs(thunk->_args[i]);
}
thunk = ((PrimEntry*) thunk->desc)->exec();
((PrimEntry*) thunk->desc)->exec(thunk);
stack_top = old_top;
break;
......
......@@ -20,6 +20,7 @@ typedef struct Thunk
} Thunk;
struct Thunk* createI(int i);
void updateI(Thunk* target, int i);
int readI(Thunk* thunk);
struct Thunk* createF(Desc* f, int nrargs);
......
Supports Markdown
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