Stack uses only references and equation objects
This commit is contained in:
parent
f29f500d0c
commit
6cc2ff87ca
8
Makefile
8
Makefile
@ -3,6 +3,9 @@
|
|||||||
# Compiler
|
# Compiler
|
||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
|
# program to Run
|
||||||
|
F = prog.bin
|
||||||
|
|
||||||
# Compiler flags
|
# Compiler flags
|
||||||
CFLAGS = -g -Wall -std=c99 -pedantic
|
CFLAGS = -g -Wall -std=c99 -pedantic
|
||||||
|
|
||||||
@ -19,8 +22,11 @@ all:
|
|||||||
clean:
|
clean:
|
||||||
rm -f $(OBJ) $(TARGET)
|
rm -f $(OBJ) $(TARGET)
|
||||||
|
|
||||||
|
debug: all
|
||||||
|
./$(TARGET) --debug $(F)
|
||||||
|
|
||||||
run: all
|
run: all
|
||||||
./$(TARGET)
|
./$(TARGET) $(F)
|
||||||
|
|
||||||
nja: ./nja/nja$(V)
|
nja: ./nja/nja$(V)
|
||||||
./nja/nja$(V) $(IN) $(OUT)
|
./nja/nja$(V) $(IN) $(OUT)
|
||||||
|
|||||||
2
SDA.c
2
SDA.c
@ -20,7 +20,7 @@ void setSDA(int point, ObjRef val, struct sda s) {
|
|||||||
|
|
||||||
void printSDA(struct sda s) {
|
void printSDA(struct sda s) {
|
||||||
for (int i = 0; i < *s.size; i++) {
|
for (int i = 0; i < *s.size; i++) {
|
||||||
printf("%i\n", getSDA(i, s));
|
printf("%i\n", *(int *)getSDA(i, s)->data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
njvm.c
21
njvm.c
@ -10,7 +10,7 @@
|
|||||||
#include "reg.c"
|
#include "reg.c"
|
||||||
|
|
||||||
// Debug
|
// Debug
|
||||||
int debug = 1;
|
int debug = 0;
|
||||||
|
|
||||||
// Stack
|
// Stack
|
||||||
struct stack stack;
|
struct stack stack;
|
||||||
@ -54,7 +54,7 @@ void execute(struct program program) {
|
|||||||
push(stack, stackSlotWithObjRef(getIntObj(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
push(stack, stackSlotWithObjRef(getIntObj(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||||
break;
|
break;
|
||||||
case ADD:
|
case ADD:
|
||||||
if (debug == 1) printf("add: %i + %i\n", * (int *)peek(stack, 2).u.objRef->data, * (int *)peek(stack, 1).u.objRef);
|
if (debug == 1) printf("add: %i + %i\n",peek(stack, 2),peek(stack, 1));
|
||||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) + getIntValfromStackSlot(pop(stack)))));
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) + getIntValfromStackSlot(pop(stack)))));
|
||||||
break;
|
break;
|
||||||
case SUB:
|
case SUB:
|
||||||
@ -194,19 +194,24 @@ void execute(struct program program) {
|
|||||||
if (debug == 1) printf("new i: %i\n", i);
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
break;
|
break;
|
||||||
case DROP:
|
case DROP:
|
||||||
|
if(debug ==1) printf("drop\n");
|
||||||
*stack.current = *stack.current - SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
*stack.current = *stack.current - SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||||
break;
|
break;
|
||||||
case DUP:
|
case DUP:
|
||||||
|
if (debug==1) printf("dup\n");
|
||||||
temp = getIntValfromStackSlot(pop(stack));
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
push(stack, stackSlotWitchNumber(temp));
|
push(stack, stackSlotWitchNumber(temp));
|
||||||
push(stack, stackSlotWitchNumber(temp));
|
push(stack, stackSlotWitchNumber(temp));
|
||||||
break;
|
break;
|
||||||
case PUSHR:
|
|
||||||
//TODO
|
|
||||||
pushR(reg, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
|
||||||
break;
|
|
||||||
case POPR:
|
case POPR:
|
||||||
push(stack, stackSlotWitchNumber(popR(reg)));
|
if (debug==1) printf("popr") ;
|
||||||
|
pushR(reg, getIntValfromStackSlot(pop(stack)));
|
||||||
|
if(debug == 1) printStackR(reg);
|
||||||
|
break;
|
||||||
|
case PUSHR:
|
||||||
|
if(debug == 1) printf("pushr");
|
||||||
|
push(stack, stackSlotWitchNumber(popR(reg)));
|
||||||
|
if(debug == 1) printStackR(reg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -226,7 +231,7 @@ int main(int argc, char *argv[]) {
|
|||||||
// Initialize the Stack
|
// Initialize the Stack
|
||||||
int size = SIZE;
|
int size = SIZE;
|
||||||
int current = 0;
|
int current = 0;
|
||||||
unsigned int s[SIZE];
|
StackSlot s[SIZE];
|
||||||
stack.size = &size;
|
stack.size = &size;
|
||||||
stack.current = ¤t;
|
stack.current = ¤t;
|
||||||
stack.stack = s;
|
stack.stack = s;
|
||||||
|
|||||||
31
programs/prog1.asm?inline=false
Normal file
31
programs/prog1.asm?inline=false
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// prog1.asm -- an assembler example with global variables
|
||||||
|
//
|
||||||
|
|
||||||
|
// global Integer x;
|
||||||
|
// global Integer y;
|
||||||
|
// x = 2;
|
||||||
|
// y = x + 3;
|
||||||
|
// x = 7 * y + x;
|
||||||
|
// writeInteger(x + -33);
|
||||||
|
// writeCharacter('\n');
|
||||||
|
|
||||||
|
pushc 2
|
||||||
|
popg 0
|
||||||
|
pushg 0
|
||||||
|
pushc 3
|
||||||
|
add
|
||||||
|
popg 1
|
||||||
|
pushc 7
|
||||||
|
pushg 1
|
||||||
|
mul
|
||||||
|
pushg 0
|
||||||
|
add
|
||||||
|
popg 0
|
||||||
|
pushg 0
|
||||||
|
pushc -33
|
||||||
|
add
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
9
reg.c
9
reg.c
@ -13,11 +13,10 @@ struct reg {
|
|||||||
unsigned int *stack;
|
unsigned int *stack;
|
||||||
};
|
};
|
||||||
|
|
||||||
void printStackR(struct reg stack, int fp) {
|
void printStackR(struct reg stack) {
|
||||||
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
printf("Regurn reg\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||||
for (int i = 0; i < *stack.current; ++i) {
|
for (int i = *stack.current-1; i > 0; --i) {
|
||||||
if(fp == i) printf("|FP|\n");
|
printf("||%i||\n", stack.stack[i]);
|
||||||
printf("|%i|\n", stack.stack[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
stack.c
19
stack.c
@ -16,9 +16,18 @@ struct stack {
|
|||||||
|
|
||||||
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 = 0; i < *stack.current; ++i) {
|
for (int i = *stack.current -1; i >= 0; --i) {
|
||||||
if(fp == i) printf("|FP|\n");
|
printf("%i\t",i);
|
||||||
printf("|%i|\n", stack.stack[i]);
|
if(stack.stack[i].isObjRef){
|
||||||
|
printf("|%p|", stack.stack[i].u.objRef);
|
||||||
|
if(stack.stack[i].u.objRef->size == sizeof(int))
|
||||||
|
printf("(%i)",*(int *)stack.stack[i].u.objRef->data);
|
||||||
|
}else {
|
||||||
|
printf("|%i|", getIntValfromStackSlot(stack.stack[i]));
|
||||||
|
}
|
||||||
|
if(fp == i) printf("<-\tFP\n");
|
||||||
|
else if(*stack.current == i) printf("<-\tSP\n");
|
||||||
|
else printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,11 +49,11 @@ StackSlot pop(struct stack s) {
|
|||||||
return s.stack[*s.current];
|
return s.stack[*s.current];
|
||||||
}
|
}
|
||||||
|
|
||||||
StackSlot peek(struct stack s, int steps) {
|
int peek(struct stack s, int steps) {
|
||||||
if (*s.current - steps < 0) {
|
if (*s.current - steps < 0) {
|
||||||
printf("Stack Underflow\n");
|
printf("Stack Underflow\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
return s.stack[*s.current - steps];
|
return getIntValfromStackSlot(s.stack[*s.current - steps]);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -24,6 +24,7 @@ ObjRef getIntObj(int val){
|
|||||||
perror("malloc");
|
perror("malloc");
|
||||||
}
|
}
|
||||||
*(int *)intObject->data=val;
|
*(int *)intObject->data=val;
|
||||||
|
intObject->size=sizeof(int);
|
||||||
return intObject;
|
return intObject;
|
||||||
}
|
}
|
||||||
int getValFromIntObj(ObjRef iref){
|
int getValFromIntObj(ObjRef iref){
|
||||||
@ -36,6 +37,7 @@ int getIntValfromStackSlot(StackSlot s){
|
|||||||
return s.u.number;
|
return s.u.number;
|
||||||
}
|
}
|
||||||
void setValIntObj(ObjRef iref, int val){
|
void setValIntObj(ObjRef iref, int val){
|
||||||
|
iref->size=sizeof(int);
|
||||||
*(int *)iref->data=val;
|
*(int *)iref->data=val;
|
||||||
}
|
}
|
||||||
StackSlot stackSlotWithObjRef(ObjRef val){
|
StackSlot stackSlotWithObjRef(ObjRef val){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user