alle rechenobjekte im stack sind jetzt pointer

This commit is contained in:
nils polek 2024-01-15 15:32:02 +00:00
parent 37843d9356
commit 3935d2778b
4 changed files with 56 additions and 73 deletions

View File

@ -7,12 +7,25 @@
#include "stack.c" #include "stack.c"
void inspect(struct stack s, int fp){ void inspect(struct stack s, int fp){
//todo Does not work dont know why
char input[20]; char input[20];
char ref[20];
char refStr[20];
printf("DEBUG [inspect]: stack, datam object?"); printf("DEBUG [inspect]: stack, datam object?");
fgets(input,20,stdin); fgets(input,20,stdin);
if (input[0] == 's') printStack(s, fp); if (input[0] == 's') printStack(s, fp);
if (input[0] == 'd'){/* todo */ } 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(){ void list(){
//todo //todo

44
njvm.c
View File

@ -7,7 +7,6 @@
#include "program.c" #include "program.c"
#include "codeReader.c" #include "codeReader.c"
#include "SDA.c" #include "SDA.c"
#include "reg.c"
#include "debugMenu.c" #include "debugMenu.c"
// Debug // Debug
@ -18,7 +17,7 @@ struct stack stack;
#define SIZE 1000 #define SIZE 1000
//Register //Register
struct reg reg; struct stack reg;
// Program // Program
struct program program; struct program program;
@ -41,6 +40,7 @@ void execute(struct program program) {
unsigned int temp; unsigned int temp;
char charInput; char charInput;
StackSlot tempSlot; StackSlot tempSlot;
ObjRef tempObj;
for (i = 0; i < *program.size; ++i) { for (i = 0; i < *program.size; ++i) {
if (debug == 1) debugMenu(fp,stack,&debug); if (debug == 1) debugMenu(fp,stack,&debug);
switch (program.program[i] >> 24) { switch (program.program[i] >> 24) {
@ -113,7 +113,7 @@ void execute(struct program program) {
*stack.current = fp; *stack.current = fp;
if (debug == 1) printf("pop: %i\n", peek(stack, 1)); if (debug == 1) printf("pop: %i\n", peek(stack, 1));
tempSlot = pop(stack) ; tempSlot = pop(stack) ;
fp = getIntValfromStackSlot(tempSlot); fp = tempSlot.u.number;
break; break;
case POPL: case POPL:
if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i]))); if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
@ -125,37 +125,37 @@ void execute(struct program program) {
break; break;
case NE: case NE:
if (debug == 1) printf("ne: %i != %i\n", peek(stack, 2), peek(stack, 1)); 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)); if (getIntValfromStackSlot(pop(stack)) != getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWitchNumber(0)); else push(stack, stackSlotWithObjRef(getIntObj(0)));
break; break;
case EQ: case EQ:
if (debug == 1) printf("eq: %i == %i\n", peek(stack, 2), peek(stack, 1)); 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)); if (getIntValfromStackSlot(pop(stack)) == getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWitchNumber(0)); else push(stack, stackSlotWithObjRef(getIntObj(0)));
break; break;
case LT: case LT:
if (debug == 1) printf("lt: %i < %i\n", peek(stack, 2), peek(stack, 1)); if (debug == 1) printf("lt: %i < %i\n", peek(stack, 2), peek(stack, 1));
temp = getIntValfromStackSlot(pop(stack)); temp = getIntValfromStackSlot(pop(stack));
if (getIntValfromStackSlot(pop(stack)) < temp) push(stack, stackSlotWitchNumber(1)); if (getIntValfromStackSlot(pop(stack)) < temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWitchNumber(0)); else push(stack, stackSlotWithObjRef(getIntObj(0)));
break; break;
case LE: case LE:
if (debug == 1) printf("le: %i <= %i\n", peek(stack, 2), peek(stack, 1)); if (debug == 1) printf("le: %i <= %i\n", peek(stack, 2), peek(stack, 1));
temp = getIntValfromStackSlot(pop(stack)); temp = getIntValfromStackSlot(pop(stack));
if (getIntValfromStackSlot(pop(stack)) <= temp) push(stack, stackSlotWitchNumber(1)); if (getIntValfromStackSlot(pop(stack)) <= temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWitchNumber(0)); else push(stack, stackSlotWithObjRef(getIntObj(0)));
break; break;
case GT: case GT:
if (debug == 1) printf("gt: %i > %i\n", peek(stack, 2), peek(stack, 1)); if (debug == 1) printf("gt: %i > %i\n", peek(stack, 2), peek(stack, 1));
temp = getIntValfromStackSlot(pop(stack)); temp = getIntValfromStackSlot(pop(stack));
if (getIntValfromStackSlot(pop(stack)) > temp) push(stack, stackSlotWitchNumber(1)); if (getIntValfromStackSlot(pop(stack)) > temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWitchNumber(0)); else push(stack, stackSlotWithObjRef(getIntObj(0)));
break; break;
case GE: case GE:
if (debug == 1) printf("ge: %i >= %i\n", peek(stack, 2), peek(stack, 1)); if (debug == 1) printf("ge: %i >= %i\n", peek(stack, 2), peek(stack, 1));
temp = getIntValfromStackSlot(pop(stack)); temp = getIntValfromStackSlot(pop(stack));
if (getIntValfromStackSlot(pop(stack)) >= temp) push(stack, stackSlotWitchNumber(1)); if (getIntValfromStackSlot(pop(stack)) >= temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWitchNumber(0)); else push(stack, stackSlotWithObjRef(getIntObj(0)));
break; break;
case BRF: case BRF:
if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i]))); if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
@ -197,19 +197,17 @@ void execute(struct program program) {
break; break;
case DUP: case DUP:
if (debug==1) printf("dup\n"); if (debug==1) printf("dup\n");
temp = getIntValfromStackSlot(pop(stack)); tempObj = pop(stack).u.objRef;
push(stack, stackSlotWitchNumber(temp)); push(stack, stackSlotWithObjRef(tempObj));
push(stack, stackSlotWitchNumber(temp)); push(stack, stackSlotWithObjRef(tempObj));
break; break;
case POPR: case POPR:
if (debug==1) printf("popr") ; if (debug==1) printf("popr") ;
pushR(reg, getIntValfromStackSlot(pop(stack))); push(reg, pop(stack));
if(debug == 1) printStackR(reg);
break; break;
case PUSHR: case PUSHR:
if(debug == 1) printf("pushr"); if(debug == 1) printf("pushr");
push(stack, stackSlotWitchNumber(popR(reg))); push(stack, pop(reg));
if(debug == 1) printStackR(reg);
break; break;
} }
} }
@ -233,7 +231,7 @@ int main(int argc, char *argv[]) {
// Initialize the registery // Initialize the registery
int rSize = SIZE; int rSize = SIZE;
int rCurrent = 0; int rCurrent = 0;
unsigned int r[SIZE]; StackSlot r[SIZE];
reg.size = &rSize; reg.size = &rSize;
reg.current = &rCurrent; reg.current = &rCurrent;
reg.stack = r; reg.stack = r;

48
reg.c
View File

@ -1,48 +0,0 @@
//
// Created by Nils on 29.10.2023.
//
#ifndef REG
#define REG
#include <stdio.h>
#include <stdlib.h>
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

22
stack.c
View File

@ -14,12 +14,17 @@ struct stack {
StackSlot *stack; StackSlot *stack;
}; };
typedef struct {
unsigned int size;
ObjRef *refs;
}ObjRefContainer;
void printStack(struct stack stack, int fp) { void printStack(struct stack stack, int fp) {
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
for (int i = *stack.current -1; i >= 0; --i) { for (int i = *stack.current -1; i >= 0; --i) {
printf("%i\t",i); printf("%i\t",i);
if(stack.stack[i].isObjRef){ 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)) if(stack.stack[i].u.objRef->size == sizeof(int))
printf("(%i)",*(int *)stack.stack[i].u.objRef->data); printf("(%i)",*(int *)stack.stack[i].u.objRef->data);
}else { }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) { void push(struct stack s, StackSlot value) {
if (*s.current >= *s.size) { if (*s.current >= *s.size) {
printf("Stack Overflow\n"); printf("Stack Overflow\n");