added sth

This commit is contained in:
nilspolek 2024-01-23 21:10:06 +01:00
parent dc5a98c401
commit 1c0d85b5c9
4 changed files with 15 additions and 10 deletions

View File

@ -27,10 +27,10 @@ void inspect(struct stack s, int fp){
} }
} }
} }
void list(){ void list(void){
//todo //todo
} }
void breakpoint(){ void breakpoint(void){
//todo //todo
} }

5
njvm.c
View File

@ -251,14 +251,12 @@ void execute(struct program program) {
case GETF: case GETF:
if (debug == 1) printf("getf"); if (debug == 1) printf("getf");
tempObj = pop(stack).u.objRef; tempObj = pop(stack).u.objRef;
if (SIGN_EXTEND(IMMEDIATE(program.program[i])) < 0 || SIGN_EXTEND(IMMEDIATE(program.program[i])) > GET_ELEMENT_COUNT(tempObj)) exit(1);
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");
tempObj = pop(stack).u.objRef; tempObj = pop(stack).u.objRef;
tempObj2 = pop(stack).u.objRef; tempObj2 = pop(stack).u.objRef;
if (SIGN_EXTEND(IMMEDIATE(program.program[i])) < 0 || SIGN_EXTEND(IMMEDIATE(program.program[i])) > GET_ELEMENT_COUNT(tempObj2)) exit(1);
setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj); setField(tempObj2, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj);
break; break;
case NEWA: case NEWA:
@ -271,7 +269,6 @@ void execute(struct program program) {
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;
if (tempInt < 0 || tempInt > GET_ELEMENT_COUNT(tempObj)) exit(1);
push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt()))); push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt())));
break; break;
case PUTFA: case PUTFA:
@ -280,7 +277,6 @@ void execute(struct program program) {
tempObj2 = pop(stack).u.objRef; tempObj2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef; bip.op1 = pop(stack).u.objRef;
tempInt = bigToInt(); tempInt = bigToInt();
if (tempInt < 0 || tempInt > GET_ELEMENT_COUNT(tempObj2)) exit(1);
setField(tempObj2, tempInt,tempObj); setField(tempObj2, tempInt,tempObj);
break; break;
case GETSZ: case GETSZ:
@ -313,7 +309,6 @@ void execute(struct program program) {
} }
void tests(void) { void tests(void) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {

BIN
njvm.o

Binary file not shown.

View File

@ -9,20 +9,30 @@ 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 = malloc(objSize)) == NULL){
perror("malloc"); perror("malloc");
} }
record->size = MSB & size; record->size = (1 << ((sizeof(int) * 8) - 1));
record->size = record->size + size;
for(int i = 0; i < size; i++) { for(int i = 0; i < size; i++) {
GET_REFS_PTR(record)[i] = malloc(8); GET_REFS_PTR(record)[i] = malloc(8);
} }
return record; return record;
} }
int getSize(ObjRef arr){
return GET_ELEMENT_COUNT(arr);
}
ObjRef getField(ObjRef arr, int point){ ObjRef getField(ObjRef arr, int point){
if(0 > point || point > getSize(arr)){
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
}
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)){
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
}
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
} }
#endif #endif