Commit 45f84d5f authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

in preparation for string handling

parent c05c0657
......@@ -116,6 +116,7 @@ void set_create_thunk_fun(Code* code)
{
case CT_APP_PRIM1:
case CT_APP_PRIM2:
case CT_APP_PRIM:
case CT_APP_FUN:
case CT_APP_FUN_TR:
case CT_APP_FUN1:
......@@ -242,6 +243,11 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
destroy_stack_frame_b(root_frame_ptr_b);
return;
}
case CT_APP_PRIM:
{
// TODO
abort("not implemented");
}
case CT_APP_FUN1:
{
Desc* slice = ((AppEntry*) expr)->f;
......@@ -352,8 +358,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
get_slice(baseDesc, newArity);
switch(slice->type) {
case FT_PRIM1:
case FT_PRIM2:
case FT_PRIM:
{
for (int i = 0; i < (*bt)->desc->arity; i++) {
push_a((*bt)->_args[i]);
......@@ -491,8 +496,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
expr = ((FunEntry*) thunk->desc)->body;
continue;
}
case FT_PRIM1:
case FT_PRIM2:
case FT_PRIM:
{
for (int i = 0; i < thunk->desc->arity; i++) {
......@@ -614,7 +618,7 @@ void exec(Code* expr, int frame_ptr, int root_frame_ptr)
exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
Thunk* cond = pop_a();
// safe to do it before read as nothing can overwrite it inbetween
// safe to do it before read as nothing can overwrite it in between
stack_top_b--;
if (readB(cond)) {
......@@ -722,8 +726,7 @@ void set_eval_fun(Desc* desc)
case FT_FUN:
desc->eval = eval_fun;
break;
case FT_PRIM1:
case FT_PRIM2:
case FT_PRIM:
desc->eval = eval_prim;
break;
}
......
......@@ -7,8 +7,9 @@ enum CodeType {
CT_VAR, // on heap
CT_VAR_STRICT, // on heap
CT_VAR_UNBOXED, // on heap or B stack
CT_APP_PRIM1,
CT_APP_PRIM2,
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_APP_FUN,
......
......@@ -47,7 +47,6 @@ FunEntry* alloc_prim(char* name) {
entry->base.arity = 0;
entry->base.thunk_size = sizeof(Thunk);
entry->base.hnf = 1;
entry->base.unboxable = true;
memcpy(entry->name, name, len + 1);
set_eval_fun((Desc*) entry);
......@@ -61,7 +60,6 @@ void gen_slices(SliceEntry* dest, Desc* forward_ptr, int arity) {
slice->base.arity = i;
slice->base.thunk_size = thunk_size_f(i);
slice->base.hnf = true;
slice->base.unboxable = false;
slice->forward_ptr = forward_ptr;
set_eval_fun((Desc*) slice);
......@@ -73,8 +71,7 @@ int printDesc(Desc* f) {
case FT_SLICE:
printDesc(((SliceEntry*) f)->forward_ptr);
return f->arity;
case FT_PRIM1:
case FT_PRIM2:
case FT_PRIM:
printf("%s", ((PrimEntry*) f)->name);
return f->arity;
case FT_FUN:
......@@ -103,7 +100,6 @@ void init_desc() {
__CHAR__ = alloc_prim("CHAR");
__REAL__ = alloc_prim("REAL");
__STRING__ = alloc_prim("STRING");
__STRING_PTR__ = alloc_prim("STRING");
__ARRAY__ = alloc_prim("ARRAY");
}
......@@ -113,7 +109,6 @@ struct FunEntry* __BOOL__;
struct FunEntry* __CHAR__;
struct FunEntry* __REAL__;
struct FunEntry* __STRING__;
struct FunEntry* __STRING_PTR__;
struct FunEntry* __ARRAY__;
......
......@@ -56,6 +56,7 @@ struct RecordEntry {
struct PrimEntry {
struct Desc base;
int boxingMap;
void (*exec)(int frame_ptr);
char name[];
};
......@@ -76,10 +77,9 @@ extern struct FunEntry* __BOOL__;
extern struct FunEntry* __CHAR__;
extern struct FunEntry* __REAL__;
extern struct FunEntry* __STRING__;
// For constants, always points to the code area
extern struct FunEntry* __STRING_PTR__;
extern struct FunEntry* __ARRAY__;
extern struct FunEntry* __ARRAY__; // Including strings
extern struct FunEntry* __FORWARD_PTR__;
......
......@@ -2,7 +2,14 @@
#define __DESC_BASE_H
enum FunType {
FT_BOXED_LIT, FT_RECORD, FT_ADT, FT_CAF, FT_CAF_REDUCED, FT_FUN, FT_SLICE, FT_PRIM1, FT_PRIM2
FT_BOXED_LIT, // Including arrays
FT_RECORD,
FT_ADT,
FT_CAF,
FT_CAF_REDUCED,
FT_FUN,
FT_SLICE,
FT_PRIM
};
struct Desc {
......@@ -11,8 +18,7 @@ struct Desc {
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; // TODO: not used, remove?
unsigned int hnf : 1;
unsigned int hnf : 1; // TODO: unused, remove?
};
void (*eval)();
......
......@@ -91,7 +91,6 @@ int parseDef1(char** ptr) {
entry->base.type = FT_FUN;
entry->base.arity = arity;
entry->base.thunk_size = thunk_size_f(arity);
entry->base.unboxable = false;
entry->base.hnf = false;
set_eval_fun((Desc*) entry);
......@@ -126,7 +125,6 @@ int parseDef1(char** ptr) {
entry->base.type = FT_CAF;
entry->base.arity = 0;
entry->base.thunk_size = thunk_size_f(0);
entry->base.unboxable = false;
entry->base.hnf = false;
set_eval_fun((Desc*) entry);
......@@ -168,7 +166,6 @@ int parseDef1(char** ptr) {
entry->base.type = FT_ADT;
entry->base.arity = arity;
entry->base.thunk_size = thunk_size_f(arity);
entry->base.unboxable = false;
entry->base.hnf = true;
entry->idx = i;
entry->nrConses = conNum;
......@@ -212,7 +209,6 @@ int parseDef1(char** ptr) {
if (!parseInt(ptr, &entry->boxing)) return 0;
entry->base.thunk_size = thunk_size_f(arity);
entry->base.unboxable = false;
entry->base.hnf = true;
set_eval_fun((Desc*) entry);
......@@ -392,7 +388,7 @@ Code* parseApp(char **ptr, bool dynamic, bool tr) {
if(desc != NULL) desc = get_slice(desc, nrArgsToParse);
entry->f = desc;
if(desc->type == FT_PRIM1)
if(desc->type == FT_PRIM && ((PrimEntry*) desc)->boxingMap == 0b1)
{
int arg0strict = entry->args[0]->type == CT_VAR_STRICT || entry->args[0]->type == CT_VAR_UNBOXED;
......@@ -411,7 +407,7 @@ Code* parseApp(char **ptr, bool dynamic, bool tr) {
entry->base.arg_pattern = 0;
}
}
else if(desc->type == FT_PRIM2)
else if(desc->type == FT_PRIM && ((PrimEntry*) desc)->boxingMap == 0b11)
{
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;
......@@ -451,6 +447,10 @@ Code* parseApp(char **ptr, bool dynamic, bool tr) {
entry->base.arg_pattern = 0;
}
}
else if(desc->type == FT_PRIM)
{
entry->base.type = CT_APP_PRIM;
}
else if(desc->type == FT_FUN && !tr)
{
if(nrArgsToParse == 1)
......
......@@ -109,17 +109,18 @@ void __C2I(int dst_idx) {
target->_int = readC(arg(1));
}
void add_prim(int arity, char* name, void (*exec)(int)) {
void add_prim(int arity, int boxingMap, char* name, void (*exec)(int)) {
int nameLength = strlen(name);
// before the PrimEntry there are "arity" number of SliceEntries
SliceEntry* entry_base = (SliceEntry*) alloc_desc(sizeof (SliceEntry) * arity + sizeof (PrimEntry) + nameLength + 1);
PrimEntry* entry = (PrimEntry*) (entry_base + arity);
entry->base.type = arity == 1 ? FT_PRIM1 : FT_PRIM2;
entry->base.type = FT_PRIM;
entry->base.arity = arity;
entry->base.thunk_size = thunk_size_f(arity);
entry->base.hnf = false;
entry->boxingMap = boxingMap;
entry->exec = exec;
// TODO: should it be copied at all?
......@@ -133,21 +134,21 @@ void add_prim(int arity, char* name, void (*exec)(int)) {
}
void init_prim() {
add_prim(2, "addI", &__addI);
add_prim(2, "subI", &__subI);
add_prim(2, "multI", &__multI);
add_prim(2, "divI", &__divI);
add_prim(2, "gtI", &__gtI);
add_prim(2, "geI", &__geI);
add_prim(2, "geC", &__geC);
add_prim(2, "ltI", &__ltI);
add_prim(2, "eqI", &__eqI);
add_prim(2, "neqI", &__neqI);
add_prim(2, "eqB", &__eqB);
add_prim(2, "eqC", &__eqC);
add_prim(1, "not", &__not);
add_prim(2, "and", &__and);
add_prim(2, "or", &__or);
add_prim(2, "mod", &__mod);
add_prim(1, "C2I", &__C2I);
add_prim(2, 0b011, "addI", &__addI);
add_prim(2, 0b011, "subI", &__subI);
add_prim(2, 0b011, "multI", &__multI);
add_prim(2, 0b011, "divI", &__divI);
add_prim(2, 0b011, "gtI", &__gtI);
add_prim(2, 0b011, "geI", &__geI);
add_prim(2, 0b011, "geC", &__geC);
add_prim(2, 0b011, "ltI", &__ltI);
add_prim(2, 0b011, "eqI", &__eqI);
add_prim(2, 0b011, "neqI", &__neqI);
add_prim(2, 0b011, "eqB", &__eqB);
add_prim(2, 0b011, "eqC", &__eqC);
add_prim(1, 0b001, "not", &__not);
add_prim(2, 0b011, "and", &__and);
add_prim(2, 0b011, "or", &__or);
add_prim(2, 0b011, "mod", &__mod);
add_prim(1, 0b001, "C2I", &__C2I);
}
......@@ -76,10 +76,10 @@ void print(bool force) {
{
printf("%c", thunk->_string_ptr->chars[i]);
}
} else if ((FunEntry*) thunk->desc == __STRING__) {
for(int i=0; i< thunk->_string.length; i++)
} else if ((FunEntry*) thunk->desc == __STRING_PTR__) {
for(int i=0; i< thunk->_string_ptr->length; i++)
{
printf("%c", thunk->_string.chars[i]);
printf("%c", thunk->_string_ptr->chars[i]);
}
} else {
printf("print: unhandled BOXED LIT\n");
......
......@@ -10,11 +10,28 @@
#pragma pack(push, 1)
struct Thunk;
typedef struct __attribute__((packed)) CleanString {
int length;
char chars[];
} CleanString;
typedef struct __attribute__((packed)) Array {
struct
{
unsigned int is_string : 1;
unsigned int is_boxed : 1;
unsigned int bytes_per_elem : 3;
unsigned int length : 27; // LIMITATION :)
};
union
{
char _chars[];
Thunk* _elems[]; //
};
} Array;
typedef struct __attribute__((packed)) Thunk {
struct Desc* desc;
......@@ -25,7 +42,7 @@ typedef struct __attribute__((packed)) Thunk {
char _char;
int _bool;
struct CleanString* _string_ptr; // For CT_THUNK
struct CleanString _string;
struct Array _array;
Thunk* _args[];
};
} 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