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

refactoring

parent d2cf64e4
......@@ -7,7 +7,7 @@
#define CT_VAR 2
#define CT_APP 3
struct CodeFlags
struct Code
{
unsigned int type : 3;
unsigned int local_type : 3;
......@@ -28,7 +28,7 @@ struct CleanString
struct LitEntry
{
struct CodeFlags flags;
struct Code base;
union
{
int _int;
......@@ -45,19 +45,19 @@ struct LitEntry
struct VarEntry
{
struct CodeFlags flags;
struct Code base;
union
{
int index; // index on the stack
FunFlags* f;
struct Desc* f;
};
};
struct AppEntry
{
struct CodeFlags flags;
struct Code base;
struct VarEntry* var;
struct CodeFlags* args[];
struct Code* args[];
};
#endif // __CODE_H
\ No newline at end of file
......@@ -16,18 +16,18 @@
// in this instance 33 is arbitrary symbolic name for a hashtable
// that contains string keys and void* values.
const int khStrPtr = 33;
KHASH_MAP_INIT_STR(khStrPtr, FunFlags*) // setup khash to handle string key with an arbitrary pointer payload
KHASH_MAP_INIT_STR(khStrPtr, Desc*) // setup khash to handle string key with an arbitrary pointer payload
// create a hashtable
khash_t(khStrPtr) *funHash = kh_init(khStrPtr);
void add_desc(char* fn, FunFlags* desc)
void add_desc(char* fn, Desc* desc)
{
khiter_t k; // used by the macros
kh_set(khStrPtr, funHash, fn, desc);
}
FunFlags* find_desc(char* fn)
Desc* find_desc(char* fn)
{
khiter_t k; // used by the macros
return kh_get_val(khStrPtr, funHash, fn);
......@@ -37,8 +37,8 @@ FunEntry* alloc_prim(char* name)
{
int len = strlen(name);
FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + len + 1);
entry->flags.type = FT_FUN;
entry->flags.arity = 0;
entry->base.type = FT_FUN;
entry->base.arity = 0;
memcpy(entry->name, name, len + 1);
return entry;
}
......@@ -48,8 +48,8 @@ void gen_slices(void* dest, void* forward_ptr, int arity)
for(int i=0; i<arity; i++)
{
SliceEntry* slice = (SliceEntry*) dest + sizeof(SliceEntry) * i;
slice->flags.type = FT_SLICE;
slice->flags.arity = i;
slice->base.type = FT_SLICE;
slice->base.arity = i;
slice->forward_ptr = forward_ptr;
}
}
......
#ifndef __DESC_H
#define __DESC_H
#include "thunk.h"
#include "code.h"
#define FT_RECORD 1
#define FT_ADT 2
#define FT_CAF 3
......@@ -9,7 +12,7 @@
#define FT_SLICE 6
#define FT_PRIM 7
struct FunFlags
struct Desc
{
unsigned int type : 3;
unsigned int arity : 8;
......@@ -17,43 +20,44 @@ struct FunFlags
struct FunEntry
{
struct FunFlags flags;
struct Desc base;
int strictness;
void* body;
union
{
char* parseCont;
Code* body;
};
char name[];
};
// an array of these is just before an ADTEntry/FunEntry (as many as arity)
struct SliceEntry
{
struct FunFlags flags;
struct Desc base;
void* forward_ptr; // FunEntry or ADTEntry
};
struct ADTEntry
{
struct FunFlags flags;
struct Desc base;
int strictness;
char name[];
};
struct CAFEntry
{
struct FunFlags flags;
void* body;
char name[];
};
struct CAFReducedEntry
{
struct FunFlags flags;
void* value;
struct Desc base;
union
{
char* parseCont;
Code* body;
Thunk* value;
};
char name[];
};
struct RecordEntry
{
struct FunFlags flags;
struct Desc base;
int strictness;
char** fields;
char name[];
......@@ -61,7 +65,7 @@ struct RecordEntry
struct PrimEntry
{
struct FunFlags flags;
struct Desc base;
int strictness;
void (*exec)();
char name[];
......@@ -71,8 +75,8 @@ void gen_slices(void* dest, void* forward_ptr, int arity);
void init_desc();
void add_desc(char* fn, FunFlags* desc);
FunFlags* find_desc(char* fn);
void add_desc(char* fn, Desc* desc);
Desc* find_desc(char* fn);
extern struct FunEntry* __INT__;
extern struct FunEntry* __BOOL__;
......
......@@ -19,7 +19,7 @@ int main()
printf("%d", res);
// add_fun("jimbo", (FunFlags*) "artist");
// add_fun("jimbo", (Desc*) "artist");
// char *tval = (char*) find_fun("jimbo");
// printf ("shortcut tval for jimbo = %s\n", tval);
}
......@@ -81,8 +81,8 @@ int parseDef1(char** ptr)
void* entry_base = alloc_desc(sizeof(SliceEntry) * arity + sizeof(FunEntry) + nameLength + 1);
FunEntry* entry = (FunEntry*) (entry_base + sizeof(SliceEntry) * arity);
entry->flags.type = FT_FUN;
entry->flags.arity = arity;
entry->base.type = FT_FUN;
entry->base.arity = arity;
// now the name can be copied into the FunEntry
memcpy(entry->name, namePtr, nameLength);
......@@ -92,12 +92,12 @@ int parseDef1(char** ptr)
if(!parseInt(ptr, &entry->strictness)) return 0;
// set the continuation for stage 2
entry->body = *ptr;
entry->parseCont = *ptr;
// generate slices. avoid function call if arity is zero
if(arity>0) gen_slices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
add_desc(entry->name, (Desc*) entry);
break;
}
......@@ -110,15 +110,15 @@ int parseDef1(char** ptr)
if(!parseInt(ptr, &nameLength)) return 0;
CAFEntry* entry = (CAFEntry*) alloc_desc(sizeof(CAFEntry) + nameLength + 1);
entry->flags.type = FT_CAF;
entry->flags.arity = 0;
entry->base.type = FT_CAF;
entry->base.arity = 0;
copyStringAndForward(entry->name, ptr, nameLength);
// set the continuation for stage 2
entry->body = *ptr;
entry->parseCont = *ptr;
add_desc(entry->name, (FunFlags*) entry);
add_desc(entry->name, (Desc*) entry);
break;
}
......@@ -149,8 +149,8 @@ int parseDef1(char** ptr)
void* entry_base = alloc_desc(sizeof(SliceEntry) * arity + sizeof(ADTEntry) + nameLength + 1);
ADTEntry* entry = (ADTEntry*) (entry_base + sizeof(SliceEntry) * arity);
entry->flags.type = FT_ADT;
entry->flags.arity = arity;
entry->base.type = FT_ADT;
entry->base.arity = arity;
// now the name can be copied into the ADTEntry
memcpy(entry->name, namePtr, nameLength);
......@@ -162,7 +162,7 @@ int parseDef1(char** ptr)
// generate slices. avoid function call if arity is zero
if(arity>0) gen_slices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
add_desc(entry->name, (Desc*) entry);
}
break;
......@@ -177,13 +177,13 @@ int parseDef1(char** ptr)
if(!parseInt(ptr, &nameLength)) return 0;
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->flags.type = FT_RECORD;
entry->base.type = FT_RECORD;
copyStringAndForward(entry->name, ptr, nameLength);
int arity;
if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity;
entry->base.arity = arity;
if(!parseInt(ptr, &entry->strictness)) return 0;
entry->fields = (char**) alloc_desc(sizeof(char*) * arity);
......@@ -196,7 +196,7 @@ int parseDef1(char** ptr)
copyStringAndForward(entry->fields[i], ptr, fieldNameLength);
}
add_desc(entry->name, (FunFlags*) entry);
add_desc(entry->name, (Desc*) entry);
break;
}
......@@ -220,27 +220,27 @@ LitEntry* parseLit(char **ptr)
}
struct LitEntry* entry = (LitEntry*) alloc_code(sizeof(LitEntry) + strlen + 1);
entry->flags.type = CT_LIT;
entry->base.type = CT_LIT;
switch(type)
{
case 'I': // Int
{
entry->flags.local_type = LIT_INT;
entry->base.local_type = LIT_INT;
if(!parseInt(ptr, &entry->_int)) return 0;
break;
}
case 'C': // Char
{
entry->flags.local_type = LIT_CHAR;
entry->base.local_type = LIT_CHAR;
entry->_char = *(*ptr)++;
break;
}
case 'R': // Real
{
entry->flags.local_type = LIT_REAL;
entry->base.local_type = LIT_REAL;
if(!parseReal(ptr, &entry->_real)) return 0;
break;
}
......@@ -248,14 +248,14 @@ LitEntry* parseLit(char **ptr)
case '0': // Bool
case '1':
{
entry->flags.local_type = LIT_BOOL;
entry->base.local_type = LIT_BOOL;
entry->_bool = type == '1';
break;
}
case 'S': // String
{
entry->flags.local_type = LIT_STRING;
entry->base.local_type = LIT_STRING;
entry->_string.length = strlen;
copyStringAndForward(entry->_string.chars, ptr, strlen);
}
......@@ -270,27 +270,27 @@ VarEntry* parseVar(char **ptr)
char type = *(*ptr)++;
struct VarEntry* entry = (VarEntry*) alloc_code(sizeof(VarEntry));
entry->flags.type = CT_VAR;
entry->base.type = CT_VAR;
switch(type)
{
case 'A': // Argument
{
entry->flags.local_type = VAR_ARG;
entry->base.local_type = VAR_ARG;
if(!parseInt(ptr, &entry->index)) return 0;
break;
}
case 'L': // Local var
{
entry->flags.local_type = VAR_LOCAL;
entry->base.local_type = VAR_LOCAL;
if(!parseInt(ptr, &entry->index)) return 0;
break;
}
case 'F': // Function
{
entry->flags.local_type = VAR_FN;
entry->base.local_type = VAR_FN;
char name[MAX_IDENTIFIER_LENGTH];
......@@ -298,7 +298,7 @@ VarEntry* parseVar(char **ptr)
if(!parseInt(ptr, &nameLength)) return 0;
copyStringAndForward(name, ptr, nameLength);
entry->f = (FunFlags*) find_desc(name); // can fail
entry->f = (Desc*) find_desc(name); // can fail
if(entry->f == 0)
{
......@@ -313,7 +313,7 @@ VarEntry* parseVar(char **ptr)
return entry;
}
CodeFlags* parseTerm(char **ptr);
Code* parseTerm(char **ptr);
AppEntry* parseApp(char **ptr)
{
......@@ -323,7 +323,7 @@ AppEntry* parseApp(char **ptr)
if(!parseInt(ptr, &nrArgs)) return 0;
struct AppEntry* entry = (AppEntry*) alloc_code(sizeof(AppEntry) + sizeof(void*) * nrArgs);
entry->flags.type = CT_APP;
entry->base.type = CT_APP;
entry->var = var;
for(int i = 0; i < nrArgs; i++)
......@@ -335,7 +335,7 @@ AppEntry* parseApp(char **ptr)
return entry;
}
CodeFlags* parseTerm(char **ptr)
Code* parseTerm(char **ptr)
{
// 1. Type char
char type = *(*ptr)++;
......@@ -343,11 +343,11 @@ CodeFlags* parseTerm(char **ptr)
switch(type)
{
case 'L': // Literal
return (CodeFlags*) parseLit(ptr);
return (Code*) parseLit(ptr);
case 'V': // Variable
return (CodeFlags*) parseVar(ptr);
return (Code*) parseVar(ptr);
case 'A': // Application
return (CodeFlags*) parseApp(ptr);
return (Code*) parseApp(ptr);
}
}
......@@ -375,7 +375,7 @@ int parseDef2(char** ptr)
FunEntry* entry = (FunEntry*) find_desc(name); // should not fail, just added to the map in the 1. phase
// read continuation
*ptr = (char*) entry->body;
*ptr = entry->parseCont;
// parse body
entry->body = parseTerm(ptr);
......@@ -392,7 +392,7 @@ int parseDef2(char** ptr)
CAFEntry* entry = (CAFEntry*) find_desc(name); // should not fail, just added to the map in the 1. phase
// read continuation
*ptr = (char*) entry->body;
*ptr = entry->parseCont;
// parse body
entry->body = parseTerm(ptr);
......
......@@ -18,8 +18,8 @@ PrimEntry* add_prim(int arity, int strictness, char* name, void (*exec)())
void* entry_base = alloc_desc(sizeof(SliceEntry) * arity + sizeof(PrimEntry) + nameLength + 1);
PrimEntry* entry = (PrimEntry*) (entry_base + sizeof(SliceEntry) * arity);
entry->flags.type = FT_PRIM;
entry->flags.arity = arity;
entry->base.type = FT_PRIM;
entry->base.arity = arity;
entry->strictness = strictness;
entry->exec = exec;
......@@ -30,7 +30,7 @@ PrimEntry* add_prim(int arity, int strictness, char* name, void (*exec)())
// generate slices. avoid function call if arity is zero
if(arity>0) gen_slices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
add_desc(entry->name, (Desc*) entry);
}
void init_prim()
......
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