added a method for testing arrays

This commit is contained in:
nilspolek 2024-01-27 22:43:34 +01:00
parent 8c45b02769
commit 6ebc065a7a
13 changed files with 44 additions and 17 deletions

2
GC.c
View File

@ -21,7 +21,7 @@ struct sda sda;
// Stack
struct stack stack;
#define SIZE 100000
#define SIZE 64
//Register
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));
break;
case POPR:
if (debug == 1) printf("popr");
if (debug == 1) printf("popr\n");
push(reg, pop(stack));
break;
case PUSHR:
if (debug == 1) printf("pushr");
if (debug == 1) printf("pushr\n");
push(stack, pop(reg));
break;
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])))));
break;
case GETF:
if (debug == 1) printf("getf");
if (debug == 1) printf("getf\n");
tempObj = pop(stack).u.objRef;
push(stack, stackSlotWithObjRef(getField(tempObj,SIGN_EXTEND(IMMEDIATE(program.program[i])))));
break;
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;
tempObj2 = pop(stack).u.objRef;
setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj);
break;
case NEWA:
if(debug == 1) printf("newa");
if(debug == 1) printf("newa\n");
bip.op1 = pop(stack).u.objRef;
push(stack, stackSlotWithObjRef(newRecord(bigToInt())));
break;
case GETFA:
if(debug == 1) printf("getfa");
if(debug == 1) printf("getfa\n");
bip.op1 = pop(stack).u.objRef;
tempInt = bigToInt();
tempObj = pop(stack).u.objRef;
push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt())));
break;
case PUTFA:
if (debug == 1) printf("putfa");
if (debug == 1) printf("putfa\n");
tempObj = pop(stack).u.objRef; // Value
tempObj2 = pop(stack).u.objRef; // Index
bip.op1 = tempObj2;
@ -272,24 +272,24 @@ void execute(struct program program) {
setField(pop(stack).u.objRef, tempInt,tempObj);
break;
case GETSZ:
if (debug == 1) printf("getsz");
if (debug == 1) printf("getsz\n");
tempObj = pop(stack).u.objRef;
if(IS_PRIMITIVE(tempObj)) bigFromInt(-1);
else bigFromInt(GET_ELEMENT_COUNT(tempObj));
push(stack, stackSlotWithObjRef(bip.res));
break;
case PUSHN:
if (debug == 1) printf("pushn");
if (debug == 1) printf("pushn\n");
push(stack, stackSlotWithObjRef(NULL));
break;
case REFEQ:
if (debug == 1) printf("refeq");
if (debug == 1) printf("refeq\n");
if(pop(stack).u.objRef == pop(stack).u.objRef) bigFromInt(true);
else bigFromInt(false);
push(stack, stackSlotWithObjRef(bip.res));
break;
case REFNE:
if (debug == 1) printf("refeq");
if (debug == 1) printf("refeq\n");
if(pop(stack).u.objRef != pop(stack).u.objRef) bigFromInt(true);
else bigFromInt(false);
push(stack, stackSlotWithObjRef(bip.res));
@ -312,6 +312,9 @@ int main(int argc, char *argv[]) {
stack.size = &size;
stack.current = &current;
stack.stack = malloc(SIZE * 1024);
if (stack.stack == NULL) {
perror("malloc");
}
// Initialize the registery
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;
}
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));
exit(EXIT_FAILURE);
}
if(IS_PRIMITIVE(arr)) perror("Record is a primitive");
if(IS_PRIMITIVE(value)){
// printf("Size of value is %i\n", 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;
}else{
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;
}
* (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 *stackSlot;
stackSlot = malloc(sizeof(StackSlot));
if(stackSlot == NULL) perror("malloc");
stackSlot->isObjRef = true;
stackSlot->u.objRef = val;
return *stackSlot;
@ -56,6 +57,7 @@ StackSlot stackSlotWithObjRef(ObjRef val) {
StackSlot stackSlotWitchNumber(int val) {
StackSlot *stackSlot;
stackSlot = malloc(sizeof(StackSlot));
if(stackSlot == NULL) perror("malloc");
stackSlot->isObjRef = false;
stackSlot->u.number = val;
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
popl 0
pushl 0
pushc 10
putf 0
pushl 0
pushc 11
putf 1
pushl 0
getf 0
pushl 0
getf 1
add
wrint
halt
// Sollte 10 + 11 ergeben