Compare commits

...

2 Commits

Author SHA1 Message Date
nilspolek
38f06b1575 updated the test 2024-01-27 22:52:29 +01:00
nilspolek
6ebc065a7a added a method for testing arrays 2024-01-27 22:43:34 +01:00
13 changed files with 44 additions and 17 deletions

2
GC.c
View File

@ -21,7 +21,7 @@ struct sda sda;
// Stack // Stack
struct stack stack; struct stack stack;
#define SIZE 100000 #define SIZE 64
//Register //Register
struct stack reg; struct stack reg;

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

27
njvm.c
View File

@ -229,42 +229,42 @@ void execute(struct program program) {
push(stack, stackSlotWithObjRef(tempObj)); push(stack, stackSlotWithObjRef(tempObj));
break; break;
case POPR: case POPR:
if (debug == 1) printf("popr"); if (debug == 1) printf("popr\n");
push(reg, pop(stack)); push(reg, pop(stack));
break; break;
case PUSHR: case PUSHR:
if (debug == 1) printf("pushr"); if (debug == 1) printf("pushr\n");
push(stack, pop(reg)); push(stack, pop(reg));
break; break;
case NEW: case NEW:
if (debug == 1) printf("new"); if (debug == 1) printf("new\t%i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, stackSlotWithObjRef(newRecord(SIGN_EXTEND(IMMEDIATE(program.program[i]))))); push(stack, stackSlotWithObjRef(newRecord(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
break; break;
case GETF: case GETF:
if (debug == 1) printf("getf"); if (debug == 1) printf("getf\n");
tempObj = pop(stack).u.objRef; tempObj = pop(stack).u.objRef;
push(stack, stackSlotWithObjRef(getField(tempObj,SIGN_EXTEND(IMMEDIATE(program.program[i]))))); push(stack, stackSlotWithObjRef(getField(tempObj,SIGN_EXTEND(IMMEDIATE(program.program[i])))));
break; break;
case PUTF: case PUTF:
if (debug == 1) printf("putf"); if (debug == 1) printf("putf\t%i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
tempObj = pop(stack).u.objRef; tempObj = pop(stack).u.objRef;
tempObj2 = pop(stack).u.objRef; tempObj2 = pop(stack).u.objRef;
setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj); setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj);
break; break;
case NEWA: case NEWA:
if(debug == 1) printf("newa"); if(debug == 1) printf("newa\n");
bip.op1 = pop(stack).u.objRef; bip.op1 = pop(stack).u.objRef;
push(stack, stackSlotWithObjRef(newRecord(bigToInt()))); push(stack, stackSlotWithObjRef(newRecord(bigToInt())));
break; break;
case GETFA: case GETFA:
if(debug == 1) printf("getfa"); if(debug == 1) printf("getfa\n");
bip.op1 = pop(stack).u.objRef; bip.op1 = pop(stack).u.objRef;
tempInt = bigToInt(); tempInt = bigToInt();
tempObj = pop(stack).u.objRef; tempObj = pop(stack).u.objRef;
push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt()))); push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt())));
break; break;
case PUTFA: case PUTFA:
if (debug == 1) printf("putfa"); if (debug == 1) printf("putfa\n");
tempObj = pop(stack).u.objRef; // Value tempObj = pop(stack).u.objRef; // Value
tempObj2 = pop(stack).u.objRef; // Index tempObj2 = pop(stack).u.objRef; // Index
bip.op1 = tempObj2; bip.op1 = tempObj2;
@ -272,24 +272,24 @@ void execute(struct program program) {
setField(pop(stack).u.objRef, tempInt,tempObj); setField(pop(stack).u.objRef, tempInt,tempObj);
break; break;
case GETSZ: case GETSZ:
if (debug == 1) printf("getsz"); if (debug == 1) printf("getsz\n");
tempObj = pop(stack).u.objRef; tempObj = pop(stack).u.objRef;
if(IS_PRIMITIVE(tempObj)) bigFromInt(-1); if(IS_PRIMITIVE(tempObj)) bigFromInt(-1);
else bigFromInt(GET_ELEMENT_COUNT(tempObj)); else bigFromInt(GET_ELEMENT_COUNT(tempObj));
push(stack, stackSlotWithObjRef(bip.res)); push(stack, stackSlotWithObjRef(bip.res));
break; break;
case PUSHN: case PUSHN:
if (debug == 1) printf("pushn"); if (debug == 1) printf("pushn\n");
push(stack, stackSlotWithObjRef(NULL)); push(stack, stackSlotWithObjRef(NULL));
break; break;
case REFEQ: case REFEQ:
if (debug == 1) printf("refeq"); if (debug == 1) printf("refeq\n");
if(pop(stack).u.objRef == pop(stack).u.objRef) bigFromInt(true); if(pop(stack).u.objRef == pop(stack).u.objRef) bigFromInt(true);
else bigFromInt(false); else bigFromInt(false);
push(stack, stackSlotWithObjRef(bip.res)); push(stack, stackSlotWithObjRef(bip.res));
break; break;
case REFNE: case REFNE:
if (debug == 1) printf("refeq"); if (debug == 1) printf("refeq\n");
if(pop(stack).u.objRef != pop(stack).u.objRef) bigFromInt(true); if(pop(stack).u.objRef != pop(stack).u.objRef) bigFromInt(true);
else bigFromInt(false); else bigFromInt(false);
push(stack, stackSlotWithObjRef(bip.res)); push(stack, stackSlotWithObjRef(bip.res));
@ -312,6 +312,9 @@ int main(int argc, char *argv[]) {
stack.size = &size; stack.size = &size;
stack.current = &current; stack.current = &current;
stack.stack = malloc(SIZE * 1024); stack.stack = malloc(SIZE * 1024);
if (stack.stack == NULL) {
perror("malloc");
}
// Initialize the registery // Initialize the registery
int rSize = 100000; int rSize = 100000;

BIN
njvm.o

Binary file not shown.

BIN
prog.bin

Binary file not shown.

View File

@ -29,17 +29,18 @@ ObjRef getField(ObjRef arr, int point){
return *(ObjRef *)GET_REFS_PTR(arr)[point]->data; return *(ObjRef *)GET_REFS_PTR(arr)[point]->data;
} }
void setField(ObjRef arr, int point, ObjRef value){ void setField(ObjRef arr, int point, ObjRef value){
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));
exit(EXIT_FAILURE);
} }
if(IS_PRIMITIVE(arr)) perror("Record is a primitive");
if(IS_PRIMITIVE(value)){ if(IS_PRIMITIVE(value)){
// printf("Size of value is %i\n", value->size);
int size = value->size; int size = value->size;
GET_REFS_PTR(arr)[point] = malloc(size); if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc");
GET_REFS_PTR(arr)[point]->size = size; GET_REFS_PTR(arr)[point]->size = size;
}else{ }else{
int size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *)); int size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
GET_REFS_PTR(arr)[point] = malloc(size); if((GET_REFS_PTR(arr)[point] = malloc(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

@ -48,6 +48,7 @@ void setValIntObj(ObjRef iref, int val) {
StackSlot stackSlotWithObjRef(ObjRef val) { StackSlot stackSlotWithObjRef(ObjRef val) {
StackSlot *stackSlot; StackSlot *stackSlot;
stackSlot = malloc(sizeof(StackSlot)); stackSlot = malloc(sizeof(StackSlot));
if(stackSlot == NULL) perror("malloc");
stackSlot->isObjRef = true; stackSlot->isObjRef = true;
stackSlot->u.objRef = val; stackSlot->u.objRef = val;
return *stackSlot; return *stackSlot;
@ -56,6 +57,7 @@ StackSlot stackSlotWithObjRef(ObjRef val) {
StackSlot stackSlotWitchNumber(int val) { StackSlot stackSlotWitchNumber(int val) {
StackSlot *stackSlot; StackSlot *stackSlot;
stackSlot = malloc(sizeof(StackSlot)); stackSlot = malloc(sizeof(StackSlot));
if(stackSlot == NULL) perror("malloc");
stackSlot->isObjRef = false; stackSlot->isObjRef = false;
stackSlot->u.number = val; stackSlot->u.number = val;
return *stackSlot; return *stackSlot;

3
test.nj Normal file
View File

@ -0,0 +1,3 @@
void main(){
}

BIN
test/tests/14.bin Normal file

Binary file not shown.

18
test/tests/arrTest.asm Normal file
View File

@ -0,0 +1,18 @@
new 3
popg 0
pushg 0
pushc 10
putf 0
pushg 0
pushc 11
putf 1
pushg 0
getf 0
pushg 0
getf 1
add
wrint
halt
// Sollte 10 + 11 ergeben