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
|
||||
CC = gcc
|
||||
|
||||
# program to Run
|
||||
F = prog.bin
|
||||
|
||||
# Compiler flags
|
||||
CFLAGS = -g -Wall -std=c99 -pedantic
|
||||
|
||||
@ -19,8 +22,11 @@ all:
|
||||
clean:
|
||||
rm -f $(OBJ) $(TARGET)
|
||||
|
||||
debug: all
|
||||
./$(TARGET) --debug $(F)
|
||||
|
||||
run: all
|
||||
./$(TARGET)
|
||||
./$(TARGET) $(F)
|
||||
|
||||
nja: ./nja/nja$(V)
|
||||
./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) {
|
||||
for (int i = 0; i < *s.size; i++) {
|
||||
printf("%i\n", getSDA(i, s));
|
||||
printf("%i\n", *(int *)getSDA(i, s)->data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
njvm.c
19
njvm.c
@ -10,7 +10,7 @@
|
||||
#include "reg.c"
|
||||
|
||||
// Debug
|
||||
int debug = 1;
|
||||
int debug = 0;
|
||||
|
||||
// Stack
|
||||
struct stack stack;
|
||||
@ -54,7 +54,7 @@ void execute(struct program program) {
|
||||
push(stack, stackSlotWithObjRef(getIntObj(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||
break;
|
||||
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)))));
|
||||
break;
|
||||
case SUB:
|
||||
@ -194,19 +194,24 @@ void execute(struct program program) {
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
break;
|
||||
case DROP:
|
||||
if(debug ==1) printf("drop\n");
|
||||
*stack.current = *stack.current - SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||
break;
|
||||
case DUP:
|
||||
if (debug==1) printf("dup\n");
|
||||
temp = getIntValfromStackSlot(pop(stack));
|
||||
push(stack, stackSlotWitchNumber(temp));
|
||||
push(stack, stackSlotWitchNumber(temp));
|
||||
break;
|
||||
case PUSHR:
|
||||
//TODO
|
||||
pushR(reg, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
break;
|
||||
case POPR:
|
||||
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;
|
||||
|
||||
}
|
||||
@ -226,7 +231,7 @@ int main(int argc, char *argv[]) {
|
||||
// Initialize the Stack
|
||||
int size = SIZE;
|
||||
int current = 0;
|
||||
unsigned int s[SIZE];
|
||||
StackSlot s[SIZE];
|
||||
stack.size = &size;
|
||||
stack.current = ¤t;
|
||||
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;
|
||||
};
|
||||
|
||||
void printStackR(struct reg stack, int fp) {
|
||||
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||
for (int i = 0; i < *stack.current; ++i) {
|
||||
if(fp == i) printf("|FP|\n");
|
||||
printf("|%i|\n", stack.stack[i]);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
stack.c
19
stack.c
@ -16,9 +16,18 @@ struct stack {
|
||||
|
||||
void printStack(struct stack stack, int fp) {
|
||||
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||
for (int i = 0; i < *stack.current; ++i) {
|
||||
if(fp == i) printf("|FP|\n");
|
||||
printf("|%i|\n", stack.stack[i]);
|
||||
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);
|
||||
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];
|
||||
}
|
||||
|
||||
StackSlot peek(struct stack s, int steps) {
|
||||
int peek(struct stack s, int steps) {
|
||||
if (*s.current - steps < 0) {
|
||||
printf("Stack Underflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return s.stack[*s.current - steps];
|
||||
return getIntValfromStackSlot(s.stack[*s.current - steps]);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -24,6 +24,7 @@ ObjRef getIntObj(int val){
|
||||
perror("malloc");
|
||||
}
|
||||
*(int *)intObject->data=val;
|
||||
intObject->size=sizeof(int);
|
||||
return intObject;
|
||||
}
|
||||
int getValFromIntObj(ObjRef iref){
|
||||
@ -36,6 +37,7 @@ int getIntValfromStackSlot(StackSlot s){
|
||||
return s.u.number;
|
||||
}
|
||||
void setValIntObj(ObjRef iref, int val){
|
||||
iref->size=sizeof(int);
|
||||
*(int *)iref->data=val;
|
||||
}
|
||||
StackSlot stackSlotWithObjRef(ObjRef val){
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user