Commit 5924e7ef authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

precompute thunk sizes indicated by a descriptor

parent 2adde4a4
......@@ -6,6 +6,7 @@
#include "desc.h"
#include "khash.h"
#include "mem.h"
#include "thunk.h"
// shorthand way to get the key from hashtable or null (0) if not found
#define kh_get_val(kname, hash, key) ({k=kh_get(kname, hash, key);(k!=kh_end(hash)?kh_val(hash,k):0);})
......@@ -48,6 +49,7 @@ FunEntry* alloc_prim(char* name) {
FunEntry* entry = (FunEntry*) alloc_desc(sizeof (FunEntry) + len + 1);
entry->base.type = FT_BOXED_LIT;
entry->base.arity = 0;
entry->base.thunk_size = sizeof(Thunk);
memcpy(entry->name, name, len + 1);
return entry;
}
......@@ -57,6 +59,7 @@ void gen_slices(SliceEntry* dest, Desc* forward_ptr, int arity) {
SliceEntry* slice = dest + i;
slice->base.type = FT_SLICE;
slice->base.arity = i;
slice->base.thunk_size = thunk_size_f(i);
slice->forward_ptr = forward_ptr;
}
}
......
......@@ -5,8 +5,6 @@
#include "thunk.h"
#include "code.h"
// LIMITATION: maximum 32 arguments
struct FunEntry {
struct Desc base;
int strictness;
......
......@@ -12,7 +12,8 @@
struct Desc {
unsigned int type : 3;
unsigned int arity : 8;
unsigned int arity : 8; // LIMITATION: maximum 32 arguments
unsigned int thunk_size : 10;
};
#endif // __DESC_H
\ No newline at end of file
......@@ -89,6 +89,7 @@ int parseDef1(char** ptr) {
FunEntry* entry = (FunEntry*) (entry_base + arity);
entry->base.type = FT_FUN;
entry->base.arity = arity;
entry->base.thunk_size = thunk_size_f(arity);
// now the name can be copied into the FunEntry
memcpy(entry->name, namePtr, nameLength);
......@@ -118,6 +119,7 @@ int parseDef1(char** ptr) {
CAFEntry* entry = (CAFEntry*) alloc_desc(sizeof (CAFEntry) + nameLength + 1);
entry->base.type = FT_CAF;
entry->base.arity = 0;
entry->base.thunk_size = thunk_size_f(0);
copyStringAndForward(entry->name, ptr, nameLength);
......@@ -156,7 +158,8 @@ int parseDef1(char** ptr) {
ADTEntry* entry = (ADTEntry*) (entry_base + arity);
entry->base.type = FT_ADT;
entry->base.arity = arity;
entry->base.thunk_size = thunk_size_f(arity);
// now the name can be copied into the ADTEntry
memcpy(entry->name, namePtr, nameLength);
entry->name[nameLength] = '\0';
......@@ -191,6 +194,8 @@ int parseDef1(char** ptr) {
entry->base.arity = arity;
if (!parseInt(ptr, &entry->strictness)) return 0;
entry->base.thunk_size = thunk_size_f(arity);
entry->fields = (char**) alloc_desc(sizeof (char*) * arity);
for (int i = 0; i < arity; i++) {
int fieldNameLength;
......
......@@ -8,11 +8,6 @@
#include "mem.h"
#include "desc.h"
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
Thunk* forward_to(Thunk* target, Thunk* thunk) {
assert(thunk != NULL);
......@@ -27,16 +22,12 @@ Thunk* forward_to(Thunk* target, Thunk* thunk) {
int thunk_size(Thunk* thunk) {
assert(thunk != NULL);
if (thunk->desc == NULL || thunk->desc->type == FT_BOXED_LIT) {
if (thunk->desc == (Desc*) __STRING__ || thunk->desc == (Desc*) __ARRAY__) {
printf("thunk_size: unhandled literal type\n");
exit(-1);
}
return sizeof (Thunk);
} else {
return max(sizeof (Thunk), sizeof (Desc*) + sizeof (Thunk*) * thunk->desc->arity);
if (thunk->desc == (Desc*) __STRING__ || thunk->desc == (Desc*) __ARRAY__) {
printf("thunk_size: unhandled literal type\n");
exit(-1);
}
return thunk->desc->thunk_size;
}
#ifdef DEBUG
......@@ -95,14 +86,14 @@ struct Thunk* updateF(Thunk* target, Desc* f) {
assert(f != NULL);
Thunk* thunk = target;
int newsize = max(sizeof (Thunk), sizeof (Desc*) + sizeof (Thunk*) * f->arity);
int newsize = f->thunk_size;
if (thunk == NULL) {
thunk = (Thunk*) alloc_heap(newsize);
} else {
if (thunk_size(target) < newsize) {
thunk = (Thunk*) alloc_heap(newsize);
target->desc = NULL;
target->desc = (Desc*) __FORWARD_PTR__;
target->_forward_ptr = thunk;
}
}
......@@ -116,7 +107,7 @@ struct Thunk* updateF(Thunk* target, Desc* f) {
struct Thunk* createF(Desc* f) {
assert(f != NULL);
Thunk* thunk = (Thunk*) alloc_heap(max(sizeof (Thunk), sizeof (Desc*) + sizeof (Thunk*) * f->arity));
Thunk* thunk = (Thunk*) alloc_heap(f->thunk_size);
thunk->desc = f;
return thunk;
}
......
......@@ -3,6 +3,11 @@
#include "desc_base.h"
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#pragma pack(push, 1)
typedef struct __attribute__((packed)) CleanString {
......@@ -27,7 +32,7 @@ typedef struct __attribute__((packed)) Thunk {
#pragma pack(pop)
Thunk* forward_to(Thunk* target, Thunk* thunk);
#define thunk_size_f(arity) max(sizeof (Thunk), sizeof (Desc*) + sizeof (Thunk*) * arity);
#ifdef DEBUG
......@@ -41,6 +46,8 @@ int readB(Thunk* thunk);
#endif
Thunk* forward_to(Thunk* target, Thunk* thunk);
struct Thunk* updateI(Thunk* target, int i);
struct Thunk* updateB(Thunk* target, int b);
struct Thunk* updateT(Thunk* target, Thunk* source);
......
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