added a method for testing arrays
This commit is contained in:
parent
8c45b02769
commit
6ebc065a7a
2
GC.c
2
GC.c
@ -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
27
njvm.c
@ -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 = ¤t;
|
stack.current = ¤t;
|
||||||
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;
|
||||||
|
|||||||
9
record.c
9
record.c
@ -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;
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
BIN
test/tests/14.bin
Normal file
BIN
test/tests/14.bin
Normal file
Binary file not shown.
18
test/tests/arrTest.asm
Normal file
18
test/tests/arrTest.asm
Normal 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
|
||||||
Loading…
x
Reference in New Issue
Block a user