diff --git a/njvm.c b/njvm.c index 1fffc07..c68f026 100644 --- a/njvm.c +++ b/njvm.c @@ -243,16 +243,16 @@ void execute(struct program program) { break; case NEW: if (debug == 1) printf("new"); - push(stack, newRecord(SIGN_EXTEND(IMMEDIATE(program.program[i])))) + push(stack, stackSlotWithObjRef(newRecord(SIGN_EXTEND(IMMEDIATE(program.program[i]))))); break; case GETF: if (debug == 1) printf("getf"); - push(stack, getField(pop(stack), SIGN_EXTEND(IMMEDIATE(program.program[i])))) + push(stack, stackSlotWithObjRef(getField(pop(stack).u.objRef,SIGN_EXTEND(IMMEDIATE(program.program[i]))))); break; case PUTF: if (debug == 1) printf("putf"); - tempObj = pop(stack) - setField(pop(stack), SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj) + tempObj = pop(stack).u.objRef; + setField(pop(stack).u.objRef, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj); break; } } diff --git a/njvm.o b/njvm.o index a92cc65..3005402 100644 Binary files a/njvm.o and b/njvm.o differ diff --git a/record.c b/record.c index eb49bec..4691353 100644 --- a/record.c +++ b/record.c @@ -7,19 +7,22 @@ #include "instruktion.c" ObjRef newRecord(int size){ ObjRef record; - if((record = malloc(*record + size * sizeof(void *)))== NULL){ - perror("malloc") + unsigned int objSize; + objSize = sizeof(*record) + (size * sizeof(void *)); + if((record = malloc(objSize))== NULL){ + perror("malloc"); } record->size = MSB & size; for(int i = 0; i < size; i++) { - GET_REFS_PTR(record)[i] = malloc(8) + GET_REFS_PTR(record)[i] = malloc(8); } + return record; } -ObjRef getField(ObjRef array, int point){ - return *(ObjRef *)GET_REFS_PTR(array)[point]->data +ObjRef getField(ObjRef arr, int point){ + return *(ObjRef *)GET_REFS_PTR(arr)[point]->data; } -setField(ObjRef array, int point, ObjRef value){ - (ObjRef *)GET_REFS_PTR(array)[point]->data = value +void setField(ObjRef arr, int point, ObjRef value){ + * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; } #endif \ No newline at end of file