gc
This commit is contained in:
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.
@@ -50,5 +50,6 @@
|
||||
#define PUSHN 39
|
||||
#define REFEQ 40
|
||||
#define REFNE 41
|
||||
#define TGC 42
|
||||
|
||||
#endif /* ifndef INSREUKTION */
|
||||
|
||||
16
njvm.c
16
njvm.c
@@ -34,6 +34,8 @@ struct stack reg;
|
||||
|
||||
unsigned fp;
|
||||
|
||||
void garbageCollector(void);
|
||||
|
||||
void version(void) {
|
||||
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
|
||||
}
|
||||
@@ -64,9 +66,10 @@ void execute(struct program program) {
|
||||
break;
|
||||
case ADD:
|
||||
if (debug == 1) printf("add: %i + %i\n", peek(stack, 2), peek(stack, 1));
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bip.op1 = pop(stack).u.objRef;
|
||||
bip.op2 = pop(stack).u.objRef;
|
||||
bigAdd();
|
||||
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case SUB:
|
||||
@@ -311,6 +314,10 @@ void execute(struct program program) {
|
||||
else bigFromInt(false);
|
||||
push(stack, stackSlotWithObjRef(bip.res));
|
||||
break;
|
||||
case TGC:
|
||||
if (debug == 1) printf("tgc\n");
|
||||
garbageCollector();
|
||||
break;
|
||||
}
|
||||
}
|
||||
end:
|
||||
@@ -366,7 +373,6 @@ ObjRef relocate(ObjRef orig) {
|
||||
return copy;
|
||||
}
|
||||
|
||||
|
||||
void scan(void) {
|
||||
char *scanPtr = memTargetPtr;
|
||||
while (scanPtr < freeHeapPtr) {
|
||||
@@ -399,6 +405,11 @@ void garbageCollector() {
|
||||
char *memToPurgePtr = halfHeapPtr - ((heapSizeKiB * 1024) / 2);
|
||||
swap();
|
||||
|
||||
bip.op1 = relocate(bip.op1);
|
||||
bip.op2 = relocate(bip.op2);
|
||||
bip.res = relocate(bip.res);
|
||||
bip.rem = relocate(bip.rem);
|
||||
|
||||
for (int i = 0; i < *stack.size; i++) {
|
||||
if (stack.stack[i].isObjRef) {
|
||||
stack.stack[i].u.objRef = relocate(stack.stack[i].u.objRef);
|
||||
@@ -408,7 +419,6 @@ void garbageCollector() {
|
||||
for (int i = 0; i < *sda.size; i++) {
|
||||
sda.sda[i] = relocate(sda.sda[i]);
|
||||
}
|
||||
|
||||
scan();
|
||||
|
||||
if (purgeFlag) {
|
||||
|
||||
6
record.c
6
record.c
@@ -5,11 +5,12 @@
|
||||
#define RECORD
|
||||
#include "stackslot.c"
|
||||
#include "instruktion.c"
|
||||
#include "njvm.h"
|
||||
ObjRef newRecord(int size){
|
||||
ObjRef record;
|
||||
unsigned int objSize;
|
||||
objSize = sizeof(*record) + (size * sizeof(void *));
|
||||
if((record = malloc(objSize)) == NULL){
|
||||
if((record = alloc(objSize)) == NULL){
|
||||
perror("malloc");
|
||||
}
|
||||
record->size = MSB;
|
||||
@@ -25,6 +26,7 @@ int getSize(ObjRef arr){
|
||||
}
|
||||
ObjRef getField(ObjRef arr, int point){
|
||||
if(arr == NULL) perror("Record is null");
|
||||
if(arr->brokenHeart==true) perror("Broken Heart");
|
||||
if(0 > point || point > getSize(arr)){
|
||||
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
|
||||
}
|
||||
@@ -47,7 +49,7 @@ void setField(ObjRef arr, int point, ObjRef value){
|
||||
else
|
||||
size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
|
||||
}
|
||||
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc");
|
||||
if((GET_REFS_PTR(arr)[point] = alloc(size)) == NULL) perror("malloc");
|
||||
GET_REFS_PTR(arr)[point] ->size = size;
|
||||
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "objref.c"
|
||||
#include "njvm.h"
|
||||
|
||||
void fatalError(char *msg){
|
||||
printf("Fatal error: %s\n", msg);
|
||||
@@ -13,7 +14,7 @@ void fatalError(char *msg){
|
||||
void * newPrimObject(int dataSize) {
|
||||
ObjRef bigObjRef;
|
||||
|
||||
bigObjRef = malloc(sizeof(unsigned int) +
|
||||
bigObjRef = alloc(sizeof(unsigned int) +
|
||||
dataSize * sizeof(unsigned char));
|
||||
if (bigObjRef == NULL) {
|
||||
fatalError("newPrimObject() got no memory");
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
new 3
|
||||
popg 0
|
||||
new 3 // new Array[3]
|
||||
popg 0 // save on sda 0
|
||||
|
||||
pushg 0
|
||||
pushc 10
|
||||
putf 0
|
||||
pushg 0
|
||||
pushc 11
|
||||
putf 1
|
||||
pushg 0 // array auf stack
|
||||
pushc 10 // 10 auf stack
|
||||
putf 0 // array[0] = 10
|
||||
pushg 0 // array auf stack
|
||||
pushc 11 // 11 auf stack
|
||||
putf 1 // array[1] = 11
|
||||
tgc // garbage collector
|
||||
|
||||
pushg 0
|
||||
getf 0
|
||||
pushg 0
|
||||
getf 1
|
||||
add
|
||||
wrint
|
||||
halt
|
||||
pushg 0 // array auf stack
|
||||
getf 0 // array[0] auf stack
|
||||
pushg 0 // array auf stack
|
||||
getf 1 // array[1] auf stack
|
||||
add // array[0] + array[1] auf stack
|
||||
wrint // ausgabe
|
||||
halt // ende
|
||||
// Sollte 10 + 11 ergeben
|
||||
|
||||
4
test/tests/test.asm
Normal file
4
test/tests/test.asm
Normal file
@@ -0,0 +1,4 @@
|
||||
pushc 1
|
||||
pushc 2
|
||||
add
|
||||
wrint
|
||||
Reference in New Issue
Block a user