Commit 459d73d0 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

more general way of setting create_thunk functions in Code struct

parent 200f4dcc
...@@ -102,25 +102,32 @@ struct Thunk* create_thunk_thunk(Code* expr, int frame_ptr) ...@@ -102,25 +102,32 @@ struct Thunk* create_thunk_thunk(Code* expr, int frame_ptr)
return &((ThunkEntry*) expr)->thunk; return &((ThunkEntry*) expr)->thunk;
} }
Thunk* (*create_thunk_funs[10]) (Code* expr, int frame_ptr); void set_create_thunk_fun(Code* code)
void init_code()
{
create_thunk_funs[CT_VAR] = create_thunk_var;
create_thunk_funs[CT_VAR_STRICT] = create_thunk_var_strict;
create_thunk_funs[CT_APP_THUNK] = create_thunk_app_static;
create_thunk_funs[CT_APP_PRIM1] = create_thunk_app_static;
create_thunk_funs[CT_APP_PRIM2] = create_thunk_app_static;
create_thunk_funs[CT_APP_FUN] = create_thunk_app_static;
create_thunk_funs[CT_APP_DYN] = create_thunk_app_dyn;
create_thunk_funs[CT_SELECT] = NULL;
create_thunk_funs[CT_IF] = NULL;
create_thunk_funs[CT_THUNK] = create_thunk_thunk;
}
create_thunk_fun get_create_thunk_fun(CodeType type)
{ {
return create_thunk_funs[type]; switch(code->type)
{
case CT_APP_PRIM1:
case CT_APP_PRIM2:
case CT_APP_FUN:
case CT_APP_THUNK:
code->create_thunk = create_thunk_app_static;
break;
case CT_APP_DYN:
code->create_thunk = create_thunk_app_dyn;
break;
case CT_VAR:
code->create_thunk = create_thunk_var;
break;
case CT_VAR_STRICT:
code->create_thunk = create_thunk_var_strict;
break;
case CT_THUNK:
code->create_thunk = create_thunk_thunk;
break;
default:
code->create_thunk = NULL;
break;
}
} }
void exec(Code* expr, int frame_ptr, int root_frame_ptr) void exec(Code* expr, int frame_ptr, int root_frame_ptr)
......
...@@ -67,8 +67,7 @@ struct IfEntry { ...@@ -67,8 +67,7 @@ struct IfEntry {
typedef Thunk* (*create_thunk_fun)(Code*, int); typedef Thunk* (*create_thunk_fun)(Code*, int);
void init_code(); void set_create_thunk_fun(Code* code);
create_thunk_fun get_create_thunk_fun(CodeType type);
void exec(Code* expr, int frame_ptr, int root_frame_ptr); void exec(Code* expr, int frame_ptr, int root_frame_ptr);
struct Thunk* eval(Thunk* thunk); struct Thunk* eval(Thunk* thunk);
......
...@@ -28,7 +28,6 @@ int main ( int argc, char *argv[] ) ...@@ -28,7 +28,6 @@ int main ( int argc, char *argv[] )
init_mem(); init_mem();
init_desc(); init_desc();
init_prim(); init_prim();
init_code();
char* input = "..\\tests\\Braun.bsapl"; char* input = "..\\tests\\Braun.bsapl";
......
...@@ -236,7 +236,6 @@ ThunkEntry* parseLit(char **ptr) { ...@@ -236,7 +236,6 @@ ThunkEntry* parseLit(char **ptr) {
struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry) + strlen); struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry) + strlen);
entry->base.type = CT_THUNK; entry->base.type = CT_THUNK;
entry->base.create_thunk = get_create_thunk_fun(CT_THUNK);
switch (type) { switch (type) {
case 'I': // Int case 'I': // Int
...@@ -275,6 +274,7 @@ ThunkEntry* parseLit(char **ptr) { ...@@ -275,6 +274,7 @@ ThunkEntry* parseLit(char **ptr) {
} }
} }
set_create_thunk_fun((Code*) entry);
return entry; return entry;
} }
...@@ -287,12 +287,10 @@ VarEntry* parseVar(char **ptr, VarEntry* target) { ...@@ -287,12 +287,10 @@ VarEntry* parseVar(char **ptr, VarEntry* target) {
switch (type) { switch (type) {
case 'L': // Local var case 'L': // Local var
entry->base.type = CT_VAR; entry->base.type = CT_VAR;
entry->base.create_thunk = get_create_thunk_fun(CT_VAR);
entry->base.strict = false; entry->base.strict = false;
break; break;
case 'S': // Strict local var case 'S': // Strict local var
entry->base.type = CT_VAR_STRICT; entry->base.type = CT_VAR_STRICT;
entry->base.create_thunk = get_create_thunk_fun(CT_VAR_STRICT);
entry->base.strict = true; entry->base.strict = true;
break; break;
default: default:
...@@ -300,6 +298,7 @@ VarEntry* parseVar(char **ptr, VarEntry* target) { ...@@ -300,6 +298,7 @@ VarEntry* parseVar(char **ptr, VarEntry* target) {
} }
if (!parseInt(ptr, &entry->index)) return 0; if (!parseInt(ptr, &entry->index)) return 0;
set_create_thunk_fun((Code*) entry);
return entry; return entry;
} }
...@@ -328,8 +327,9 @@ Code* parseApp(char **ptr, bool dynamic) { ...@@ -328,8 +327,9 @@ Code* parseApp(char **ptr, bool dynamic) {
{ {
struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry)); struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry));
entry->base.type = CT_THUNK; entry->base.type = CT_THUNK;
entry->base.create_thunk = get_create_thunk_fun(CT_THUNK);
entry->thunk.desc = desc; entry->thunk.desc = desc;
set_create_thunk_fun((Code*) entry);
return (Code*) entry; return (Code*) entry;
} }
...@@ -338,7 +338,6 @@ Code* parseApp(char **ptr, bool dynamic) { ...@@ -338,7 +338,6 @@ Code* parseApp(char **ptr, bool dynamic) {
// TODO: CAS // TODO: CAS
entry->base.type = CT_APP_FUN; entry->base.type = CT_APP_FUN;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_FUN);
} }
else else
{ {
...@@ -353,7 +352,6 @@ Code* parseApp(char **ptr, bool dynamic) { ...@@ -353,7 +352,6 @@ Code* parseApp(char **ptr, bool dynamic) {
{ {
parseVar(ptr, &entry->var); parseVar(ptr, &entry->var);
entry->base.type = CT_APP_DYN; entry->base.type = CT_APP_DYN;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_DYN);
} }
else else
{ {
...@@ -366,27 +364,24 @@ Code* parseApp(char **ptr, bool dynamic) { ...@@ -366,27 +364,24 @@ Code* parseApp(char **ptr, bool dynamic) {
if(desc->type == FT_PRIM1) if(desc->type == FT_PRIM1)
{ {
entry->base.type = CT_APP_PRIM1; entry->base.type = CT_APP_PRIM1;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_PRIM1);
} }
else if(desc->type == FT_PRIM2) else if(desc->type == FT_PRIM2)
{ {
entry->base.type = CT_APP_PRIM2; entry->base.type = CT_APP_PRIM2;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_PRIM2);
} }
else if(desc->type == FT_FUN) else if(desc->type == FT_FUN)
{ {
entry->base.type = CT_APP_FUN; entry->base.type = CT_APP_FUN;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_FUN);
} }
else else
{ {
entry->base.type = CT_APP_THUNK; entry->base.type = CT_APP_THUNK;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_THUNK);
} }
} }
} }
entry->base.nr_args = nrArgs; entry->base.nr_args = nrArgs;
set_create_thunk_fun((Code*) entry);
return (Code*) entry; return (Code*) entry;
} }
...@@ -398,7 +393,6 @@ SelectEntry* parseSelect(char **ptr) { ...@@ -398,7 +393,6 @@ SelectEntry* parseSelect(char **ptr) {
struct SelectEntry* entry = (SelectEntry*) alloc_code(sizeof (SelectEntry) + sizeof (SelectCaseEntry) * nrCases); struct SelectEntry* entry = (SelectEntry*) alloc_code(sizeof (SelectEntry) + sizeof (SelectCaseEntry) * nrCases);
entry->base.type = CT_SELECT; entry->base.type = CT_SELECT;
entry->base.create_thunk = get_create_thunk_fun(CT_SELECT);
entry->base.nr_cases = nrCases; entry->base.nr_cases = nrCases;
entry->expr = expr; entry->expr = expr;
...@@ -424,17 +418,18 @@ SelectEntry* parseSelect(char **ptr) { ...@@ -424,17 +418,18 @@ SelectEntry* parseSelect(char **ptr) {
entry->cases[i].body = (Code*) parseTerm(ptr); entry->cases[i].body = (Code*) parseTerm(ptr);
} }
set_create_thunk_fun((Code*) entry);
return entry; return entry;
} }
IfEntry* parseIf(char **ptr) { IfEntry* parseIf(char **ptr) {
struct IfEntry* entry = (IfEntry*) alloc_code(sizeof (IfEntry)); struct IfEntry* entry = (IfEntry*) alloc_code(sizeof (IfEntry));
entry->base.type = CT_IF; entry->base.type = CT_IF;
entry->base.create_thunk = get_create_thunk_fun(CT_IF);
entry->cond = parseTerm(ptr); entry->cond = parseTerm(ptr);
entry->texpr = parseTerm(ptr); entry->texpr = parseTerm(ptr);
entry->fexpr = parseTerm(ptr); entry->fexpr = parseTerm(ptr);
set_create_thunk_fun((Code*) entry);
return entry; return entry;
} }
......
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