added sth

This commit is contained in:
nilspolek 2024-01-23 20:04:06 +01:00
parent c174108e09
commit dc5a98c401
5 changed files with 57 additions and 5 deletions

View File

@ -1,6 +1,6 @@
#ifndef CONSTS
#define CONSTS
#define VERSION 6
#define VERSION 7
#endif /* ifndef CONSTS
#define CONSTS

58
njvm.c
View File

@ -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;
}
}

BIN
njvm.o

Binary file not shown.

BIN
prog.bin

Binary file not shown.