diff --git a/GC.c b/GC.c index b0c2f5d..bde1048 100644 --- a/GC.c +++ b/GC.c @@ -3,97 +3,103 @@ #include #include #include -#include "stackslot.c" -#include "stack.c" -#include "bigint.h" +#include +#include "objref.c" #include "instruktion.c" -#include "record.c" -#include "SDA.c" -typedef struct { - unsigned int freiZeiger; - unsigned int size; - unsigned char *data; -} *heapPartRef; +bool halbspeicher1IsActiv = true; +unsigned char* halbspeicher1; +unsigned char* halbspeicher2; +unsigned char* freizeiger; +unsigned int groesse; -// SDA -struct sda sda; +void initHeap(unsigned int size){ + groesse = size*1024/2; + if((halbspeicher1 = malloc(groesse)) == NULL) perror("malloc"); + if((halbspeicher2 = malloc(groesse)) == NULL) perror("malloc"); + freizeiger = halbspeicher1; +} +ObjRef copyObjToFreeMem(ObjRef obj){ + ObjRef copy = (ObjRef) halbspeicher2[freizeiger]; + unsigned int size = (IS_PRIMITIVE(obj))? obj->size : GET_ELEMENT_COUNT(obj)* sizeof(void *); + memcpy((void *) halbspeicher2[freizeiger], obj, size); + freizeiger += size; + return copy; +} +void setBrokenHeart(ObjRef obj, bool val){ + //TODO +} +bool getBrokenHeart(ObjRef obj){ + //TODO + return true; +} +void setForwardPointer(ObjRef obj, void* ref){ + //TODO +} +void *getForwardPointer(ObjRef obj){ + //TODO + return malloc(0); +} -// Stack -struct stack stack; -#define SIZE 64 +ObjRef reallocate(ObjRef orig){ + ObjRef copy; + if(orig == NULL){ + copy == NULL; + }else{ + if(getBrokenHeart(orig)){ + setForwardPointer(copy,orig); + }else{ + copy = copyObjToFreeMem(orig); + setBrokenHeart(orig,true); + setForwardPointer(orig,copy); + } + } + return copy; +} +unsigned int getSize(ObjRef obj){ + return (IS_PRIMITIVE(obj))? obj->size : GET_ELEMENT_COUNT(obj)* sizeof(void *); +} +void scan(void){ + void *scan = halbspeicher1; + while (scan < freizeiger){ + if(!IS_PRIMITIVE((ObjRef)scan)){ + for (unsigned int i = 0; i < GET_ELEMENT_COUNT((ObjRef)scan); i += getSize(GET_REFS_PTR((ObjRef)scan)[i])) { + GET_REFS_PTR((ObjRef)scan)[i] = reallocate(GET_REFS_PTR((ObjRef)scan)[i]); + } + } + scan += getSize((ObjRef)scan); + } +} +void *alloc(unsigned int size){ + if((&freizeiger - &halbspeicher1)/8 + size > groesse) { + printf("Heap Overflow\n"); + exit(EXIT_FAILURE); + } + // Set broken heart to false + setBrokenHeart((ObjRef)freizeiger,false); + freizeiger += sizeof(bool) + sizeof(void *); + void *ptr = &freizeiger; + freizeiger += size; + return ptr; +} +void swap(void){ + unsigned char *tmp = halbspeicher1; + halbspeicher1 = halbspeicher2; + halbspeicher2 = tmp; + freizeiger = halbspeicher1; + halbspeicher1IsActiv = !halbspeicher1IsActiv; +} +void triggerGC(void){ + printf("GC triggered\n"); + //TODO +} +void copy(){ + ObjRef *test; + unsigned int objResSize = 2; + if((test = malloc(2*sizeof(test)))==NULL) perror("malloc"); + for (int i = 0; i < objResSize; ++i) { -//Register -struct stack reg; + } +} - -//heapPartRef primary; -//heapPartRef secondary; -//unsigned int heapSize; -//void initHeap(int size){ -// heapSize = 2 * sizeof (unsigned int) + size; -// if ((primary = malloc(heapSize)) == NULL) perror("malloc"); -// if ((secondary = malloc(heapSize)) == NULL) perror("malloc"); -//} -// -// -//// nimmt obj und copier es in den secondary Speicher -//void copy(ObjRef obj){ -// if(obj->brokenHeart == true) return; -// obj->brokenHeart = true; -// if (IS_PRIMITIVE(obj)){ -// if(obj->size > secondary->size-secondary->freiZeiger) perror("Heap is to Small"); -// obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, obj->size); -// secondary->freiZeiger += obj->size; -// } else { -// if(sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)) > secondary->size-secondary->freiZeiger) perror("Heap is to Small"); -// GET_ELEMENT_COUNT(obj); -// obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *))); -// secondary->freiZeiger += sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)); -// for (int i = 0; i < GET_ELEMENT_COUNT(obj); ++i) { -// copy(getField(obj,i)); -// } -// } -//} -// -//void runGC(){ -// int rootSize = *sda.size + *reg.size + *stack.size + 4; -// ObjRef* rootObjs = malloc(sizeof(ObjRef) * rootSize); -// if(rootObjs == NULL) perror("malloc"); -// int counter = 0; -// //Bip -// rootObjs[0] = bip.op1; -// rootObjs[counter++] = bip.op2; -// rootObjs[counter++] = bip.res; -// rootObjs[counter++] = bip.rem; -// //SDA -// for (int i = 0; i < *sda.size; ++i) { -// rootObjs[counter++] = sda.sda[i]; -// } -// //REG -// for (int i = 0; i < *reg.size; ++i) { -// rootObjs[counter++] = reg.stack[i].u.objRef; -// } -// //STACK -// for (int i = 0; i < *stack.size; ++i) { -// rootObjs[counter++] = stack.stack[i].u.objRef; -// } -// -// for (int i = 0; i < rootSize; ++i) { -// if(rootObjs[i] == NULL) continue; -// copy(rootObjs[i]); -// } -// heapPartRef temp = primary; -// primary = secondary; -// secondary = temp; -// -//} -// -//void *alloc(size_t size){ -// if(primary->size-primary->freiZeiger < size){ -// runGC(); -// } -// primary->freiZeiger += size; -// return (void *) primary->data[primary->freiZeiger - size]; -//} #endif diff --git a/njvm.c b/njvm.c index 39feb49..c4dbc51 100644 --- a/njvm.c +++ b/njvm.c @@ -9,6 +9,7 @@ #include "bigint.h" #include "record.c" #include "GC.c" +#include "SDA.c" // Debug int debug = 0; @@ -16,6 +17,15 @@ int debug = 0; // Program struct program program; +// SDA +struct sda sda; + +// Stack +struct stack stack; +#define SIZE 64 + +//Register +struct stack reg; unsigned fp; @@ -306,7 +316,8 @@ void tests(void) { } int main(int argc, char *argv[]) { - + // Init the Heap + initHeap(1000); // Initialize the Stack int size = SIZE; @@ -337,6 +348,7 @@ int main(int argc, char *argv[]) { int run = 0; int sizeSDA; + if (argc > 1) { for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--debug") == 0) { @@ -360,6 +372,10 @@ int main(int argc, char *argv[]) { run = 1; } } + // Init the sda + ObjRef s[sizeSDA]; + sda.size = &sizeSDA; + sda.sda = s; } if (debug) { diff --git a/njvm.o b/njvm.o index 9c3f0c4..faea6d5 100644 Binary files a/njvm.o and b/njvm.o differ diff --git a/objref.c b/objref.c index c540871..19a75d1 100644 --- a/objref.c +++ b/objref.c @@ -3,11 +3,17 @@ #include typedef struct ObjRef{ - bool brokenHeart; +// if brkokenHeart and forward_pointer added at this point bip throws errors +// bool brokenHeart; // struct ObjRef *forward_pointer; unsigned int size; unsigned char data[1]; } *ObjRef; +typedef struct{ + unsigned char brokenHeart; + ObjRef forward_pointer; +} *ObjRefGC; + #endif /* ifndef OBJREF #define OBJREF */ diff --git a/record.c b/record.c index e67ca85..3d8086c 100644 --- a/record.c +++ b/record.c @@ -5,6 +5,7 @@ #define RECORD #include "stackslot.c" #include "instruktion.c" +#include "GC.c" ObjRef newRecord(int size){ ObjRef record; unsigned int objSize; diff --git a/support.c b/support.c index cdd2d80..20d43cb 100644 --- a/support.c +++ b/support.c @@ -4,7 +4,7 @@ #include #include "objref.c" - +#include "GC.c" void fatalError(char *msg){ printf("Fatal error: %s\n", msg); exit(1); diff --git a/support.o b/support.o deleted file mode 100644 index 2056c06..0000000 Binary files a/support.o and /dev/null differ