Commit 200f4dcc authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

very basic distinction between prims of 1 or 2 arguments

parent ddf4739e
......@@ -102,14 +102,15 @@ struct Thunk* create_thunk_thunk(Code* expr, int frame_ptr)
return &((ThunkEntry*) expr)->thunk;
}
Thunk* (*create_thunk_funs[9]) (Code* expr, int frame_ptr);
Thunk* (*create_thunk_funs[10]) (Code* expr, int frame_ptr);
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_PRIM] = 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;
......@@ -136,16 +137,26 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
// TODO: enforce strictness in ADT/Record
switch (expr->type) {
case CT_APP_PRIM:
{
Desc* slice = ((AppEntry*) expr)->f;
case CT_APP_PRIM1:
{
push_a(alloc_b());
exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
for (int i = 0; i < expr->nr_args; i++) {
push_a(alloc_b());
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
}
((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);
destroy_stack_frame(root_frame_ptr);
destroy_stack_frame_b(root_frame_ptr_b);
return;
}
case CT_APP_PRIM2:
{
push_a(alloc_b());
exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
push_a(alloc_b());
exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);
((PrimEntry*) slice)->exec(root_frame_ptr);
destroy_stack_frame(root_frame_ptr);
destroy_stack_frame_b(root_frame_ptr_b);
return;
......@@ -193,7 +204,8 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);
switch(slice->type) {
case FT_PRIM:
case FT_PRIM1:
case FT_PRIM2:
{
for (int i = 0; i < basethunk->desc->arity; i++) {
if(basethunk->_args[i]->desc->hnf)
......@@ -307,7 +319,8 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
expr = ((FunEntry*) thunk->desc)->body;
continue;
}
case FT_PRIM:
case FT_PRIM1:
case FT_PRIM2:
{
for (int i = 0; i < thunk->desc->arity; i++) {
......@@ -422,7 +435,8 @@ struct Thunk* eval(Thunk* thunk) {
thunk = pop_a();
return thunk;
}
case FT_PRIM:
case FT_PRIM1:
case FT_PRIM2:
{
push_a(thunk);
int frame_ptr = stack_top_a;
......
......@@ -5,7 +5,8 @@
enum CodeType {
CT_VAR, CT_VAR_STRICT,
CT_APP_THUNK, CT_APP_PRIM, CT_APP_FUN, CT_APP_DYN,
CT_APP_PRIM1, CT_APP_PRIM2,
CT_APP_THUNK, CT_APP_FUN, CT_APP_DYN,
CT_SELECT, CT_IF,
CT_THUNK
};
......
......@@ -68,7 +68,8 @@ int printDesc(Desc* f) {
case FT_SLICE:
printDesc(((SliceEntry*) f)->forward_ptr);
return f->arity;
case FT_PRIM:
case FT_PRIM1:
case FT_PRIM2:
printf("%s", ((PrimEntry*) f)->name);
return f->arity;
case FT_FUN:
......
......@@ -2,11 +2,11 @@
#define __DESC_BASE_H
enum FunType {
FT_BOXED_LIT, FT_RECORD, FT_ADT, FT_CAF, FT_CAF_REDUCED, FT_FUN, FT_SLICE, FT_PRIM
FT_BOXED_LIT, FT_RECORD, FT_ADT, FT_CAF, FT_CAF_REDUCED, FT_FUN, FT_SLICE, FT_PRIM1, FT_PRIM2
};
struct Desc {
FunType type : 3;
FunType type : 4;
unsigned int arity : 8; // LIMITATION: maximum 32 arguments
unsigned int thunk_size : 10; // It gives false result for strings and arrays
unsigned int unboxable : 1;
......
......@@ -363,11 +363,16 @@ Code* parseApp(char **ptr, bool dynamic) {
if(desc != NULL) desc = get_slice(desc, nrArgs);
entry->f = desc;
if(desc->type == FT_PRIM)
if(desc->type == FT_PRIM1)
{
entry->base.type = CT_APP_PRIM;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_PRIM);
entry->base.type = CT_APP_PRIM1;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_PRIM1);
}
else if(desc->type == FT_PRIM2)
{
entry->base.type = CT_APP_PRIM2;
entry->base.create_thunk = get_create_thunk_fun(CT_APP_PRIM2);
}
else if(desc->type == FT_FUN)
{
entry->base.type = CT_APP_FUN;
......
......@@ -56,7 +56,7 @@ void add_prim(int arity, int strictness, char* name, void (*exec)(int)) {
SliceEntry* entry_base = (SliceEntry*) alloc_desc(sizeof (SliceEntry) * arity + sizeof (PrimEntry) + nameLength + 1);
PrimEntry* entry = (PrimEntry*) (entry_base + arity);
entry->base.type = FT_PRIM;
entry->base.type = arity == 1 ? FT_PRIM1 : FT_PRIM2;
entry->base.arity = arity;
entry->strictness = strictness;
entry->base.thunk_size = thunk_size_f(arity);
......
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