diff --git a/consts.c b/consts.c index ce16cb4..ae8797c 100644 --- a/consts.c +++ b/consts.c @@ -1,6 +1,6 @@ #ifndef CONSTS #define CONSTS -#define VERSION 6 +#define VERSION 7 #endif /* ifndef CONSTS #define CONSTS diff --git a/njvm.c b/njvm.c index c68f026..c9b5faa 100644 --- a/njvm.c +++ b/njvm.c @@ -40,6 +40,8 @@ void execute(struct program program) { char charInput; StackSlot tempSlot; ObjRef tempObj; + ObjRef tempObj2; + int tempInt; for (i = 0; i < *program.size; ++i) { if (debug == 1) debugMenu(fp, stack, &debug, i); switch (program.program[i] >> 24) { @@ -100,7 +102,8 @@ void execute(struct program program) { case RDCHR: if (debug == 1) printf("rdchr\n"); scanf("%c", &charInput); - push(stack, stackSlotWithObjRef(getIntObj(charInput))); + bigFromInt(charInput); + push(stack, stackSlotWithObjRef(bip.res)); if (debug == 1) printf("pushed %c\n", charInput); break; case WRCHR: @@ -247,12 +250,61 @@ void execute(struct program program) { break; case GETF: if (debug == 1) printf("getf"); - push(stack, stackSlotWithObjRef(getField(pop(stack).u.objRef,SIGN_EXTEND(IMMEDIATE(program.program[i]))))); + 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]))))); break; case PUTF: if (debug == 1) printf("putf"); tempObj = pop(stack).u.objRef; - setField(pop(stack).u.objRef, SIGN_EXTEND(IMMEDIATE(program.program[i])),tempObj); + 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); + break; + case NEWA: + if(debug == 1) printf("newa"); + bip.op1 = pop(stack).u.objRef; + push(stack, stackSlotWithObjRef(newRecord(bigToInt()))); + break; + case GETFA: + if(debug == 1) printf("getfa"); + bip.op1 = pop(stack).u.objRef; + tempInt = bigToInt(); + tempObj = pop(stack).u.objRef; + if (tempInt < 0 || tempInt > GET_ELEMENT_COUNT(tempObj)) exit(1); + push(stack, stackSlotWithObjRef(getField(tempObj,bigToInt()))); + break; + case PUTFA: + if (debug == 1) printf("putfa"); + tempObj = pop(stack).u.objRef; + tempObj2 = pop(stack).u.objRef; + bip.op1 = pop(stack).u.objRef; + tempInt = bigToInt(); + if (tempInt < 0 || tempInt > GET_ELEMENT_COUNT(tempObj2)) exit(1); + setField(tempObj2, tempInt,tempObj); + break; + case GETSZ: + if (debug == 1) printf("getsz"); + 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"); + push(stack, stackSlotWithObjRef(NULL)); + break; + case REFEQ: + if (debug == 1) printf("refeq"); + 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(pop(stack).u.objRef != pop(stack).u.objRef) bigFromInt(true); + else bigFromInt(false); + push(stack, stackSlotWithObjRef(bip.res)); break; } } diff --git a/njvm.o b/njvm.o index 3005402..2cee315 100644 Binary files a/njvm.o and b/njvm.o differ diff --git a/prog.bin b/prog.bin index da3fc02..a7d3220 100644 Binary files a/prog.bin and b/prog.bin differ diff --git a/test.asm b/test.asm index cd43d4a..9eb4224 100644 --- a/test.asm +++ b/test.asm @@ -152,4 +152,4 @@ _main: drop 1 __0: rsf - ret \ No newline at end of file + ret