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

clean up

parent 6e5493c4
/*
* Some properties:
* - get_dst(root_frame_ptr) can be NULL, but never a FORWARD_PTR (at least I hope so)
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
......@@ -18,8 +25,8 @@
#define forward_thunk(thunk, frame_ptr) \
Thunk* dst = get_dst(frame_ptr); \
if(dst != NULL){ \
dst->desc = (Desc*) __FORWARD_PTR__; \
dst->_forward_ptr = thunk; \
dst->desc = (Desc*) __FORWARD_PTR__; \
dst->_forward_ptr = thunk; \
}
#define placeholder() \
......@@ -114,8 +121,7 @@ void set_create_thunk_fun(Code* code)
{
switch(code->type)
{
case CT_APP_PRIM1:
case CT_APP_PRIM2:
case CT_APP_PRIM_FAST:
case CT_APP_PRIM:
case CT_APP_FUN:
case CT_APP_FUN_TR:
......@@ -171,40 +177,18 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
// TODO: enforce strictness in ADT/Record
switch (expr->type) {
case CT_APP_PRIM1:
{
switch(expr->arg_pattern)
{
case 1:
push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));
break;
case 2:
push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);
break;
default:
placeholder();
exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
break;
}
if(get_dst(root_frame_ptr) == NULL && (((AppEntry*) expr)->f)->unboxable_return)
{
Thunk* tmp = (Thunk*) alloc_heap(sizeof(Thunk));
set_return(root_frame_ptr, tmp);
}
((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:
case CT_APP_PRIM_FAST:
{
// careful, "exec" may trigger garbage collection
// read local variables only after the last exec
switch(expr->arg_pattern)
{
case 0:
placeholder();
exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
placeholder();
exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
break;
case 1:
push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));
push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[1])->thunk);
......@@ -237,11 +221,16 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[1])->index));
break;
default:
placeholder();
exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
case 8:
push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));
break;
case 9:
push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);
break;
default: // 10
placeholder();
exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
break;
}
if(get_dst(root_frame_ptr) == NULL && (((AppEntry*) expr)->f)->unboxable_return)
......@@ -266,14 +255,13 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
if(desc->boxingMap & argmask)
{
placeholder();
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
}
else
{
push_a(NULL);
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
}
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
argmask <<= 1;
}
......@@ -357,16 +345,16 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
if (thunk == NULL)
{
thunk = (Thunk*) alloc_heap(newsize);
set_return(root_frame_ptr, thunk);
}
else if (thunk->desc->thunk_size < newsize) {
else if (thunk->desc->thunk_size < newsize)
{
Thunk* target = thunk;
thunk = (Thunk*) alloc_heap(newsize);
target->desc = (Desc*) __FORWARD_PTR__;
target->_forward_ptr = thunk;
set_return(root_frame_ptr, thunk);
}
set_return(root_frame_ptr, thunk);
thunk->desc = slice;
assert(thunk->desc->arity == expr->nr_args);
......@@ -384,8 +372,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));
Thunk** bt = &peek_a();
(*bt)->desc->eval();
(*bt)->desc->eval(); // eval follows FORWARD_PTRs
Desc* baseDesc = (*bt)->desc->type == FT_SLICE ?
((SliceEntry*) (*bt)->desc)->forward_ptr : (*bt)->desc;
......@@ -405,14 +392,25 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
{
for (int i = 0; i < (*bt)->desc->arity; i++) {
push_a((*bt)->_args[i]);
/*if(!basethunk->_args[i]->desc->hnf)*/ (*bt)->_args[i]->desc->eval();
(*bt)->_args[i]->desc->eval();
}
int argmask = 1;
for (int i = 0; i < expr->nr_args; i++) {
placeholder();
if(((PrimEntry*) slice)->boxingMap & argmask)
{
placeholder();
}
else
{
push_a(NULL);
}
exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
}
argmask <<= 1;
}
if(get_dst(root_frame_ptr) == NULL && slice->unboxable_return)
{
......@@ -432,7 +430,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
for (int i = 0; i < (*bt)->desc->arity; i++) {
push_a((*bt)->_args[i]);
if(((FunEntry*) (slice))->strictness & argmask /*&& !arg->desc->hnf*/)
if(((FunEntry*) (slice))->strictness & argmask)
{
(*bt)->_args[i]->desc->eval();
}
......@@ -458,16 +456,16 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
if (thunk == NULL)
{
thunk = (Thunk*) alloc_heap(newsize);
set_return(root_frame_ptr, thunk);
}
else if (thunk->desc->thunk_size < newsize) {
else if (thunk->desc->thunk_size < newsize)
{
Thunk* target = thunk;
thunk = (Thunk*) alloc_heap(newsize);
target->desc = (Desc*) __FORWARD_PTR__;
target->_forward_ptr = thunk;
set_return(root_frame_ptr, thunk);
}
set_return(root_frame_ptr, thunk);
thunk->desc = slice;
assert(thunk->desc->arity == (*bt)->desc->arity + expr->nr_args);
......@@ -537,7 +535,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
for (int i = 0; i < thunk->desc->arity; i++) {
push_a(thunk->_args[i]);
if(((FunEntry*) (thunk->desc))->strictness & argmask /*&& !arg->desc->hnf*/)
if(((FunEntry*) (thunk->desc))->strictness & argmask)
{
thunk->_args[i]->desc->eval();
thunk = stack_a[root_frame_ptr-1];
......@@ -550,13 +548,14 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
}
case FT_PRIM:
{
for (int i = 0; i < thunk->desc->arity; i++) {
push_a(thunk->_args[i]);
thunk->_args[i]->desc->eval();
thunk = stack_a[root_frame_ptr-1];
}
// get_dst(root_frame_ptr) cannot be NULL, no need to check
((PrimEntry*) thunk->desc)->exec(root_frame_ptr);
destroy_stack_frame(root_frame_ptr);
......
......@@ -4,25 +4,24 @@
#include "thunk.h"
enum CodeType {
CT_VAR, // on heap
CT_VAR_STRICT, // on heap
CT_VAR_UNBOXED, // on heap or B stack
CT_APP_PRIM1, // primitive functions with 1 unboxable arg
CT_APP_PRIM2, // primitive functions with 2 unboxable args
CT_APP_PRIM, // primitive functions with array (string) arg(s)
CT_APP_THUNK, // ADT, record, or not saturated app
CT_APP_DYN, // function part is a variable
CT_VAR, // on heap
CT_VAR_STRICT, // on heap
CT_VAR_UNBOXED, // on heap or B stack
CT_APP_PRIM_FAST, // primitive functions with 1 or 2 unboxable arg(s))
CT_APP_PRIM, // primitive functions with array (string) arg(s)
CT_APP_THUNK, // ADT, record, or not saturated app
CT_APP_DYN, // function part is a variable
CT_APP_FUN,
CT_APP_FUN1,
CT_APP_FUN2,
CT_APP_FUN_TR, // tail recursive
CT_APP_FUN_TR, // tail recursive
CT_SELECT_ADT,
CT_SELECT_LIT,
CT_SELECT_STR,
CT_SELECT_REC, // Record field selection
CT_SELECT_REC, // Record field selection
CT_IF,
CT_LET,
CT_THUNK // constant, always fits the B stack
CT_THUNK // constant, always fits the B stack
};
struct Code {
......@@ -31,7 +30,7 @@ struct Code {
unsigned int nr_cases : 5; // used in SelectEntry
unsigned int nr_bindings : 8; // used in LetEntry
unsigned int strict : 1; // used in VarEntry
unsigned int arg_pattern : 3;
unsigned int arg_pattern : 4;
void (*create_thunk)(Code*, struct Thunk**, int);
};
......
......@@ -392,19 +392,19 @@ Code* parseApp(char **ptr, bool dynamic, bool tr) {
{
int arg0strict = entry->args[0]->type == CT_VAR_STRICT || entry->args[0]->type == CT_VAR_UNBOXED;
entry->base.type = CT_APP_PRIM1;
entry->base.type = CT_APP_PRIM_FAST;
if(arg0strict)
{
entry->base.arg_pattern = 1;
entry->base.arg_pattern = 8;
}
else if(entry->args[0]->type == CT_THUNK)
{
entry->base.arg_pattern = 2;
entry->base.arg_pattern = 9;
}
else
{
entry->base.arg_pattern = 0;
entry->base.arg_pattern = 10;
}
}
else if(desc->type == FT_PRIM && desc->arity == 2 && ((PrimEntry*) desc)->boxingMap == 0b11)
......@@ -412,7 +412,7 @@ Code* parseApp(char **ptr, bool dynamic, bool tr) {
int arg0strict = entry->args[0]->type == CT_VAR_STRICT || entry->args[0]->type == CT_VAR_UNBOXED;
int arg1strict = entry->args[1]->type == CT_VAR_STRICT || entry->args[1]->type == CT_VAR_UNBOXED;
entry->base.type = CT_APP_PRIM2;
entry->base.type = CT_APP_PRIM_FAST;
if(arg0strict && entry->args[1]->type == CT_THUNK)
{
......
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