This commit is contained in:
nilspolek 2024-01-28 21:59:21 +01:00
parent f0c7f31655
commit 815db27637
15 changed files with 40 additions and 21 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.

View File

@ -50,5 +50,6 @@
#define PUSHN 39 #define PUSHN 39
#define REFEQ 40 #define REFEQ 40
#define REFNE 41 #define REFNE 41
#define TGC 42
#endif /* ifndef INSREUKTION */ #endif /* ifndef INSREUKTION */

16
njvm.c
View File

@ -34,6 +34,8 @@ struct stack reg;
unsigned fp; unsigned fp;
void garbageCollector(void);
void version(void) { void version(void) {
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__); printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
} }
@ -64,9 +66,10 @@ void execute(struct program program) {
break; break;
case ADD: case ADD:
if (debug == 1) printf("add: %i + %i\n", peek(stack, 2), peek(stack, 1)); 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.op1 = pop(stack).u.objRef;
bip.op2 = pop(stack).u.objRef;
bigAdd(); bigAdd();
push(stack, stackSlotWithObjRef(bip.res)); push(stack, stackSlotWithObjRef(bip.res));
break; break;
case SUB: case SUB:
@ -311,6 +314,10 @@ void execute(struct program program) {
else bigFromInt(false); else bigFromInt(false);
push(stack, stackSlotWithObjRef(bip.res)); push(stack, stackSlotWithObjRef(bip.res));
break; break;
case TGC:
if (debug == 1) printf("tgc\n");
garbageCollector();
break;
} }
} }
end: end:
@ -366,7 +373,6 @@ ObjRef relocate(ObjRef orig) {
return copy; return copy;
} }
void scan(void) { void scan(void) {
char *scanPtr = memTargetPtr; char *scanPtr = memTargetPtr;
while (scanPtr < freeHeapPtr) { while (scanPtr < freeHeapPtr) {
@ -399,6 +405,11 @@ void garbageCollector() {
char *memToPurgePtr = halfHeapPtr - ((heapSizeKiB * 1024) / 2); char *memToPurgePtr = halfHeapPtr - ((heapSizeKiB * 1024) / 2);
swap(); 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++) { for (int i = 0; i < *stack.size; i++) {
if (stack.stack[i].isObjRef) { if (stack.stack[i].isObjRef) {
stack.stack[i].u.objRef = relocate(stack.stack[i].u.objRef); 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++) { for (int i = 0; i < *sda.size; i++) {
sda.sda[i] = relocate(sda.sda[i]); sda.sda[i] = relocate(sda.sda[i]);
} }
scan(); scan();
if (purgeFlag) { if (purgeFlag) {

BIN
njvm.o

Binary file not shown.

View File

@ -5,11 +5,12 @@
#define RECORD #define RECORD
#include "stackslot.c" #include "stackslot.c"
#include "instruktion.c" #include "instruktion.c"
#include "njvm.h"
ObjRef newRecord(int size){ ObjRef newRecord(int size){
ObjRef record; ObjRef record;
unsigned int objSize; unsigned int objSize;
objSize = sizeof(*record) + (size * sizeof(void *)); objSize = sizeof(*record) + (size * sizeof(void *));
if((record = malloc(objSize)) == NULL){ if((record = alloc(objSize)) == NULL){
perror("malloc"); perror("malloc");
} }
record->size = MSB; record->size = MSB;
@ -25,6 +26,7 @@ int getSize(ObjRef arr){
} }
ObjRef getField(ObjRef arr, int point){ ObjRef getField(ObjRef arr, int point){
if(arr == NULL) perror("Record is null"); if(arr == NULL) perror("Record is null");
if(arr->brokenHeart==true) perror("Broken Heart");
if(0 > point || point > getSize(arr)){ if(0 > point || point > getSize(arr)){
printf("Index %i out of bounds for length %i\n",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 else
size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *)); 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; GET_REFS_PTR(arr)[point] ->size = size;
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
} }

View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "objref.c" #include "objref.c"
#include "njvm.h"
void fatalError(char *msg){ void fatalError(char *msg){
printf("Fatal error: %s\n", msg); printf("Fatal error: %s\n", msg);
@ -13,7 +14,7 @@ void fatalError(char *msg){
void * newPrimObject(int dataSize) { void * newPrimObject(int dataSize) {
ObjRef bigObjRef; ObjRef bigObjRef;
bigObjRef = malloc(sizeof(unsigned int) + bigObjRef = alloc(sizeof(unsigned int) +
dataSize * sizeof(unsigned char)); dataSize * sizeof(unsigned char));
if (bigObjRef == NULL) { if (bigObjRef == NULL) {
fatalError("newPrimObject() got no memory"); fatalError("newPrimObject() got no memory");

BIN
support.o

Binary file not shown.

View File

@ -1,18 +1,19 @@
new 3 new 3 // new Array[3]
popg 0 popg 0 // save on sda 0
pushg 0 pushg 0 // array auf stack
pushc 10 pushc 10 // 10 auf stack
putf 0 putf 0 // array[0] = 10
pushg 0 pushg 0 // array auf stack
pushc 11 pushc 11 // 11 auf stack
putf 1 putf 1 // array[1] = 11
tgc // garbage collector
pushg 0 pushg 0 // array auf stack
getf 0 getf 0 // array[0] auf stack
pushg 0 pushg 0 // array auf stack
getf 1 getf 1 // array[1] auf stack
add add // array[0] + array[1] auf stack
wrint wrint // ausgabe
halt halt // ende
// Sollte 10 + 11 ergeben // Sollte 10 + 11 ergeben

4
test/tests/test.asm Normal file
View File

@ -0,0 +1,4 @@
pushc 1
pushc 2
add
wrint