From 3935d2778b186ba514df4bb76a85c533824b9aba Mon Sep 17 00:00:00 2001 From: nils polek Date: Mon, 15 Jan 2024 15:32:02 +0000 Subject: [PATCH] alle rechenobjekte im stack sind jetzt pointer --- debugMenu.c | 15 ++++++++++++++- njvm.c | 44 +++++++++++++++++++++----------------------- reg.c | 48 ------------------------------------------------ stack.c | 22 +++++++++++++++++++++- 4 files changed, 56 insertions(+), 73 deletions(-) delete mode 100755 reg.c diff --git a/debugMenu.c b/debugMenu.c index d13b617..037ae1e 100644 --- a/debugMenu.c +++ b/debugMenu.c @@ -7,12 +7,25 @@ #include "stack.c" void inspect(struct stack s, int fp){ + //todo Does not work dont know why char input[20]; + char ref[20]; + char refStr[20]; printf("DEBUG [inspect]: stack, datam object?"); fgets(input,20,stdin); if (input[0] == 's') printStack(s, fp); if (input[0] == 'd'){/* todo */ } - if (input[0] == 'o'){/* todo */} + if (input[0] == 'o'){ + scanf("%19s",ref); + ObjRefContainer container; + container = getRefs(s); + for (int i = 0; i<= container.size; i++) { + sprintf(refStr, "%p", (void *)&container.refs[i]); + if(strcmp(ref, refStr) == 0) printf("Adress exists\n"); + else printf("Adress doeas not exist\n"); + printf("%s",refStr); + } + } } void list(){ //todo diff --git a/njvm.c b/njvm.c index e0a29de..ff6b17e 100644 --- a/njvm.c +++ b/njvm.c @@ -7,7 +7,6 @@ #include "program.c" #include "codeReader.c" #include "SDA.c" -#include "reg.c" #include "debugMenu.c" // Debug @@ -18,7 +17,7 @@ struct stack stack; #define SIZE 1000 //Register -struct reg reg; +struct stack reg; // Program struct program program; @@ -41,6 +40,7 @@ void execute(struct program program) { unsigned int temp; char charInput; StackSlot tempSlot; + ObjRef tempObj; for (i = 0; i < *program.size; ++i) { if (debug == 1) debugMenu(fp,stack,&debug); switch (program.program[i] >> 24) { @@ -113,7 +113,7 @@ void execute(struct program program) { *stack.current = fp; if (debug == 1) printf("pop: %i\n", peek(stack, 1)); tempSlot = pop(stack) ; - fp = getIntValfromStackSlot(tempSlot); + fp = tempSlot.u.number; break; case POPL: if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i]))); @@ -125,37 +125,37 @@ void execute(struct program program) { break; case NE: if (debug == 1) printf("ne: %i != %i\n", peek(stack, 2), peek(stack, 1)); - if (getIntValfromStackSlot(pop(stack)) != getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWitchNumber(1)); - else push(stack, stackSlotWitchNumber(0)); + if (getIntValfromStackSlot(pop(stack)) != getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWithObjRef(getIntObj(1))); + else push(stack, stackSlotWithObjRef(getIntObj(0))); break; case EQ: if (debug == 1) printf("eq: %i == %i\n", peek(stack, 2), peek(stack, 1)); - if (getIntValfromStackSlot(pop(stack)) == getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWitchNumber(1)); - else push(stack, stackSlotWitchNumber(0)); + if (getIntValfromStackSlot(pop(stack)) == getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWithObjRef(getIntObj(1))); + else push(stack, stackSlotWithObjRef(getIntObj(0))); break; case LT: if (debug == 1) printf("lt: %i < %i\n", peek(stack, 2), peek(stack, 1)); temp = getIntValfromStackSlot(pop(stack)); - if (getIntValfromStackSlot(pop(stack)) < temp) push(stack, stackSlotWitchNumber(1)); - else push(stack, stackSlotWitchNumber(0)); + if (getIntValfromStackSlot(pop(stack)) < temp) push(stack, stackSlotWithObjRef(getIntObj(1))); + else push(stack, stackSlotWithObjRef(getIntObj(0))); break; case LE: if (debug == 1) printf("le: %i <= %i\n", peek(stack, 2), peek(stack, 1)); temp = getIntValfromStackSlot(pop(stack)); - if (getIntValfromStackSlot(pop(stack)) <= temp) push(stack, stackSlotWitchNumber(1)); - else push(stack, stackSlotWitchNumber(0)); + if (getIntValfromStackSlot(pop(stack)) <= temp) push(stack, stackSlotWithObjRef(getIntObj(1))); + else push(stack, stackSlotWithObjRef(getIntObj(0))); break; case GT: if (debug == 1) printf("gt: %i > %i\n", peek(stack, 2), peek(stack, 1)); temp = getIntValfromStackSlot(pop(stack)); - if (getIntValfromStackSlot(pop(stack)) > temp) push(stack, stackSlotWitchNumber(1)); - else push(stack, stackSlotWitchNumber(0)); + if (getIntValfromStackSlot(pop(stack)) > temp) push(stack, stackSlotWithObjRef(getIntObj(1))); + else push(stack, stackSlotWithObjRef(getIntObj(0))); break; case GE: if (debug == 1) printf("ge: %i >= %i\n", peek(stack, 2), peek(stack, 1)); temp = getIntValfromStackSlot(pop(stack)); - if (getIntValfromStackSlot(pop(stack)) >= temp) push(stack, stackSlotWitchNumber(1)); - else push(stack, stackSlotWitchNumber(0)); + if (getIntValfromStackSlot(pop(stack)) >= temp) push(stack, stackSlotWithObjRef(getIntObj(1))); + else push(stack, stackSlotWithObjRef(getIntObj(0))); break; case BRF: if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i]))); @@ -197,19 +197,17 @@ void execute(struct program program) { break; case DUP: if (debug==1) printf("dup\n"); - temp = getIntValfromStackSlot(pop(stack)); - push(stack, stackSlotWitchNumber(temp)); - push(stack, stackSlotWitchNumber(temp)); + tempObj = pop(stack).u.objRef; + push(stack, stackSlotWithObjRef(tempObj)); + push(stack, stackSlotWithObjRef(tempObj)); break; case POPR: if (debug==1) printf("popr") ; - pushR(reg, getIntValfromStackSlot(pop(stack))); - if(debug == 1) printStackR(reg); + push(reg, pop(stack)); break; case PUSHR: if(debug == 1) printf("pushr"); - push(stack, stackSlotWitchNumber(popR(reg))); - if(debug == 1) printStackR(reg); + push(stack, pop(reg)); break; } } @@ -233,7 +231,7 @@ int main(int argc, char *argv[]) { // Initialize the registery int rSize = SIZE; int rCurrent = 0; - unsigned int r[SIZE]; + StackSlot r[SIZE]; reg.size = &rSize; reg.current = &rCurrent; reg.stack = r; diff --git a/reg.c b/reg.c deleted file mode 100755 index 20b71dc..0000000 --- a/reg.c +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by Nils on 29.10.2023. -// -#ifndef REG -#define REG - -#include -#include - -struct reg { - int* size; - int* current; - unsigned int *stack; -}; - -void printStackR(struct reg stack) { - printf("Regurn reg\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); - for (int i = *stack.current-1; i > 0; --i) { - printf("||%i||\n", stack.stack[i]); - } -} - -void pushR(struct reg s, unsigned int value) { - if (*s.current >= *s.size) { - printf("Stack Overflow\n"); - exit(EXIT_FAILURE); - } - s.stack[*s.current] = value; - *s.current=*s.current + 1; -} - -unsigned int popR(struct reg s) { - if (*s.current == 0) { - printf("Stack Underflow\n"); - exit(EXIT_FAILURE); - } - *s.current = *s.current -1; - return s.stack[*s.current]; -} - -unsigned int peekR(struct reg s, int steps) { - if (*s.current - steps < 0) { - printf("Stack Underflow\n"); - exit(EXIT_FAILURE); - } - return s.stack[*s.current - steps]; -} -#endif diff --git a/stack.c b/stack.c index 1aa0cfb..f7a6ee9 100755 --- a/stack.c +++ b/stack.c @@ -14,12 +14,17 @@ struct stack { StackSlot *stack; }; +typedef struct { + unsigned int size; + ObjRef *refs; +}ObjRefContainer; + void printStack(struct stack stack, int fp) { printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); for (int i = *stack.current -1; i >= 0; --i) { printf("%i\t",i); if(stack.stack[i].isObjRef){ - printf("|%p|", stack.stack[i].u.objRef); + printf("|%p|", (void *)stack.stack[i].u.objRef); if(stack.stack[i].u.objRef->size == sizeof(int)) printf("(%i)",*(int *)stack.stack[i].u.objRef->data); }else { @@ -31,6 +36,21 @@ void printStack(struct stack stack, int fp) { } } +ObjRefContainer getRefs(struct stack stack){ + ObjRefContainer continer; + int counter = 0; + for (int i = 0; i <= *stack.current; i++) { + if(stack.stack[i].isObjRef == true) counter++; + } + continer.size = counter; + ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef)); + for (int i = 0; i<= *stack.current; i++) + if(stack.stack[i].isObjRef == true) + list[counter--] = stack.stack[i].u.objRef; + continer.refs = list; + return continer; +} + void push(struct stack s, StackSlot value) { if (*s.current >= *s.size) { printf("Stack Overflow\n");