This commit is contained in:
Elias Bennour 2024-01-28 20:33:23 +01:00
parent 958c383f85
commit f4b1ce03c5
11 changed files with 166 additions and 16 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

160
njvm.c
View File

@ -8,14 +8,29 @@
#include "debugMenu.c" #include "debugMenu.c"
#include "bigint.h" #include "bigint.h"
#include "record.c" #include "record.c"
#include "GC.c" #include "SDA.c"
// Debug // Debug
int debug = 0; int debug = 0;
// Purge Flag
int purgeFlag = false;
// Heap Size
int argVheapSizeKiB = 8192;
// Program // Program
struct program program; struct program program;
// SDA
struct sda sda;
// Stack
struct stack stack;
#define SIZE 10000
//Register
struct stack reg;
unsigned fp; unsigned fp;
@ -36,7 +51,7 @@ void execute(struct program program) {
ObjRef tempObj2; ObjRef tempObj2;
int tempInt; int tempInt;
for (i = 0; i < *program.size; ++i) { for (i = 0; i < *program.size; ++i) {
if (debug == 1 || bp == i) debugMenu(fp, stack, &debug, i, &bp); // if (debug == 1 || bp == i) debugMenu(fp, stack, &debug, i, &bp);
if (debug == 1) printf("(%i)", i); if (debug == 1) printf("(%i)", i);
switch (program.program[i] >> 24) { switch (program.program[i] >> 24) {
case HALT: case HALT:
@ -305,8 +320,135 @@ void execute(struct program program) {
void tests(void) { void tests(void) {
} }
int main(int argc, char *argv[]) { /*
* Garbage Collection
*/
ObjRef *heap;
int heapSizeKiB;
char *memTargetPtr;
char *freeHeapPtr;
char *halfHeapPtr;
int gcCount = 0;
#define MSB (1 << (8 * sizeof(unsigned int) - 1))
#define IS_PRIMITIVE(objRef) (((objRef)->size & MSB) == 0)
#define GET_SIZE(objRef)((objRef)->size& ~MSB)
#define GET_REFS(objRef)((ObjRef *)((objRef)->data))
ObjRef copyObjectToFreeMem(ObjRef orig) {
int size;
if (IS_PRIMITIVE(orig)) {
size = GET_SIZE(orig);
} else {
size = sizeof(void *) + sizeof(unsigned int) + sizeof(bool) + GET_SIZE(orig) * sizeof(ObjRef *);
}
memcpy(freeHeapPtr, orig, size);
ObjRef oldPtr = (ObjRef) freeHeapPtr;
freeHeapPtr += size;
return oldPtr;
}
ObjRef relocate(ObjRef orig) {
ObjRef copy;
if (orig == NULL) {
copy = NULL;
} else {
if (orig->brokenHeart == 1) {
copy = orig->forwardPointer;
} else {
copy = copyObjectToFreeMem(orig);
orig->brokenHeart = true;
orig->forwardPointer = copy;
}
}
return copy;
}
void scan(void) {
char *scanPtr = memTargetPtr;
while (scanPtr < freeHeapPtr) {
ObjRef scanOr = (ObjRef) scanPtr;
if (!IS_PRIMITIVE(scanOr)) {
for (int i = 0; i < GET_SIZE(scanOr); i++) {
GET_REFS(scanOr)[i] = relocate(GET_REFS(scanOr)[i]);
}
scanPtr += sizeof(bool) + (GET_SIZE(scanOr) * sizeof(ObjRef *)) + sizeof(unsigned int) + sizeof(void *);
} else {
scanPtr += GET_SIZE(scanOr);
}
}
}
void swap() {
if (memTargetPtr == (char *) heap) {
freeHeapPtr = halfHeapPtr;
memTargetPtr = halfHeapPtr;
halfHeapPtr = ((char *) heap) + (heapSizeKiB * 1024);
} else {
freeHeapPtr = ((char *) heap);
memTargetPtr = ((char *) heap);
halfHeapPtr = ((char *) heap) + ((heapSizeKiB * 1024) / 2);
}
}
void garbageCollector() {
char *memToPurgePtr = halfHeapPtr - ((heapSizeKiB * 1024) / 2);
swap();
for (int i = 0; i < *stack.size; i++) {
if (stack.stack[i].isObjRef) {
stack.stack[i].u.objRef = relocate(stack.stack[i].u.objRef);
}
}
for (int i = 0; i < *sda.size; i++) {
sda.sda[i] = relocate(sda.sda[i]);
}
scan();
if (purgeFlag) {
memset(memToPurgePtr, 0, heapSizeKiB * 1024 / 2);
}
gcCount++;
}
ObjRef alloc(unsigned int size) {
if (freeHeapPtr + size > halfHeapPtr) {
garbageCollector();
if (freeHeapPtr + size > halfHeapPtr) {
fprintf(stderr, "Heap is full\n");
exit(1);
}
}
ObjRef or;
or = (ObjRef) freeHeapPtr;
freeHeapPtr = freeHeapPtr + size;
return or;
}
void heapAllocator(int n) {
heapSizeKiB = n;
if ((heap = alloc(n * 1024)) == NULL) {
perror("malloc");
exit(1);
}
memTargetPtr = (char *) heap;
freeHeapPtr = memTargetPtr;
halfHeapPtr = (memTargetPtr + (n * 1024) / 2);
}
/*
* Main
*/
int main(int argc, char *argv[]) {
// Init the Heap
//initHeap(1000);
// Initialize the Stack // Initialize the Stack
int size = SIZE; int size = SIZE;
@ -337,6 +479,7 @@ int main(int argc, char *argv[]) {
int run = 0; int run = 0;
int sizeSDA; int sizeSDA;
if (argc > 1) { if (argc > 1) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--debug") == 0) { if (strcmp(argv[i], "--debug") == 0) {
@ -349,17 +492,22 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} else if (strcmp(argv[i], "--stack") == 0) { } else if (strcmp(argv[i], "--stack") == 0) {
i++; i++;
// TODO: implement stack size size = atoi(argv[i]);
} else if (strcmp(argv[i], "--heap") == 0) { } else if (strcmp(argv[i], "--heap") == 0) {
i++; i++;
// TODO: implement heap size argVheapSizeKiB = atoi(argv[i]);
} else if (strcmp(argv[i], "--gcpurge") == 0) { } else if (strcmp(argv[i], "--gcpurge") == 0) {
// TODO: implement gcpurge purgeFlag = true;
} else { } else {
sizeSDA = fromFile(argv[i], program); sizeSDA = fromFile(argv[i], program);
run = 1; run = 1;
} }
} }
//heapAllocator(argVheapSizeKiB);
// Init the sda
ObjRef s[sizeSDA];
sda.size = &sizeSDA;
sda.sda = s;
} }
if (debug) { if (debug) {

BIN
njvm.o

Binary file not shown.

View File

@ -6,6 +6,8 @@
typedef struct ObjRef{ typedef struct ObjRef{
unsigned int size; unsigned int size;
unsigned char data[1]; unsigned char data[1];
bool brokenHeart;
struct ObjRef *forwardPointer;
} *ObjRef; } *ObjRef;
#endif /* ifndef OBJREF #endif /* ifndef OBJREF

BIN
support.o

Binary file not shown.