added some shit
This commit is contained in:
parent
23e787eb5e
commit
f29f500d0c
8
SDA.c
8
SDA.c
@ -4,17 +4,17 @@
|
|||||||
#ifndef SDA
|
#ifndef SDA
|
||||||
#define SDA
|
#define SDA
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "stackslot.c"
|
||||||
struct sda {
|
struct sda {
|
||||||
int *size;
|
int *size;
|
||||||
unsigned int *sda;
|
ObjRef *sda;
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int getSDA(int i, struct sda s) {
|
ObjRef getSDA(int i, struct sda s) {
|
||||||
return s.sda[i];
|
return s.sda[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSDA(int point, int val, struct sda s) {
|
void setSDA(int point, ObjRef val, struct sda s) {
|
||||||
s.sda[point] = val;
|
s.sda[point] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
consts.c
2
consts.c
@ -1,6 +1,6 @@
|
|||||||
#ifndef CONSTS
|
#ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
#define VERSION 8
|
#define VERSION 5
|
||||||
|
|
||||||
#endif /* ifndef CONSTS
|
#endif /* ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
|
|||||||
98
njvm.c
98
njvm.c
@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "instruktion.c"
|
#include "instruktion.c"
|
||||||
#include "code.c"
|
#include "code.c"
|
||||||
@ -6,16 +7,17 @@
|
|||||||
#include "program.c"
|
#include "program.c"
|
||||||
#include "codeReader.c"
|
#include "codeReader.c"
|
||||||
#include "SDA.c"
|
#include "SDA.c"
|
||||||
|
#include "reg.c"
|
||||||
|
|
||||||
// Debug
|
// Debug
|
||||||
int debug = 0;
|
int debug = 1;
|
||||||
|
|
||||||
// Stack
|
// Stack
|
||||||
struct stack stack;
|
struct stack stack;
|
||||||
#define SIZE 1000
|
#define SIZE 1000
|
||||||
|
|
||||||
//Register
|
//Register
|
||||||
struct stack reg;
|
struct reg reg;
|
||||||
|
|
||||||
// Program
|
// Program
|
||||||
struct program program;
|
struct program program;
|
||||||
@ -37,73 +39,74 @@ void execute(struct program program) {
|
|||||||
int intInput;
|
int intInput;
|
||||||
unsigned int temp;
|
unsigned int temp;
|
||||||
char charInput;
|
char charInput;
|
||||||
|
StackSlot tempSlot;
|
||||||
for (i = 0; i < *program.size; ++i) {
|
for (i = 0; i < *program.size; ++i) {
|
||||||
if (debug == 1) printf("=== DEBUG: Stack before instruction %i ===\n", i);
|
if (debug == 1) printf("=== DEBUG: Stack before instruction %i ===\n", i);
|
||||||
if (debug == 1) printStack(stack, fp);
|
if (debug == 1) printStack(stack, fp);
|
||||||
if (debug == 1) printf("=== DEBUG: Instruction %i ===\n", i);
|
if (debug == 1) printf("=== DEBUG: Instruction %i ===\n", i);
|
||||||
|
|
||||||
switch (program.program[i] >> 24) {
|
switch (program.program[i] >> 24) {
|
||||||
case HALT:
|
case HALT:
|
||||||
if (debug == 1) printf("halt\n");
|
if (debug == 1) printf("halt\n");
|
||||||
goto end;
|
goto end;
|
||||||
case PUSHC:
|
case PUSHC:
|
||||||
if (debug == 1) printf("pushc: %i\n", IMMEDIATE(program.program[i]));
|
if (debug == 1) printf("pushc: %i\n", IMMEDIATE(program.program[i]));
|
||||||
push(stack, 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", peek(stack, 2), peek(stack, 1));
|
if (debug == 1) printf("add: %i + %i\n", * (int *)peek(stack, 2).u.objRef->data, * (int *)peek(stack, 1).u.objRef);
|
||||||
push(stack, pop(stack) + pop(stack));
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) + getIntValfromStackSlot(pop(stack)))));
|
||||||
break;
|
break;
|
||||||
case SUB:
|
case SUB:
|
||||||
if (debug == 1) printf("sub: %i - %i\n", peek(stack, 2), peek(stack, 1));
|
if (debug == 1) printf("sub: %i - %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
temp = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
push(stack, pop(stack) - temp);
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) - temp)));
|
||||||
break;
|
break;
|
||||||
case MUL:
|
case MUL:
|
||||||
if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
|
if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
push(stack, pop(stack) * pop(stack));
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) * getIntValfromStackSlot(pop(stack)))));
|
||||||
break;
|
break;
|
||||||
case DIV:
|
case DIV:
|
||||||
if (debug == 1) printf("div: %i / %i\n", peek(stack, 2), peek(stack, 1));
|
if (debug == 1) printf("div: %i / %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
temp = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
push(stack, pop(stack) / temp);
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) / temp)));
|
||||||
break;
|
break;
|
||||||
case MOD:
|
case MOD:
|
||||||
if (debug == 1) printf("mod: %i %% %i\n", peek(stack, 2), peek(stack, 1));
|
if (debug == 1) printf("mod: %i %% %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
temp = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
push(stack, pop(stack) % temp);
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)))));
|
||||||
break;
|
break;
|
||||||
case RDINT:
|
case RDINT:
|
||||||
if (debug == 1) printf("rdint\n");
|
if (debug == 1) printf("rdint\n");
|
||||||
scanf("%i", &intInput);
|
scanf("%i", &intInput);
|
||||||
push(stack, intInput);
|
push(stack, stackSlotWithObjRef(getIntObj(intInput)));
|
||||||
if (debug == 1) printf("pushed %i\n", intInput);
|
if (debug == 1) printf("pushed %i\n", intInput);
|
||||||
break;
|
break;
|
||||||
case WRINT:
|
case WRINT:
|
||||||
if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
|
if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
|
||||||
printf("%i", pop(stack));
|
printf("%i", getIntValfromStackSlot(pop(stack)));
|
||||||
break;
|
break;
|
||||||
case RDCHR:
|
case RDCHR:
|
||||||
if (debug == 1) printf("rdchr\n");
|
if (debug == 1) printf("rdchr\n");
|
||||||
scanf("%c", &charInput);
|
scanf("%c", &charInput);
|
||||||
push(stack, charInput);
|
push(stack, stackSlotWithObjRef(getIntObj(charInput)));
|
||||||
if (debug == 1) printf("pushed %c\n", charInput);
|
if (debug == 1) printf("pushed %c\n", charInput);
|
||||||
break;
|
break;
|
||||||
case WRCHR:
|
case WRCHR:
|
||||||
if (debug == 1) printf("wrchr: %c\n", peek(stack, 1));
|
if (debug == 1) printf("wrchr: %c\n", peek(stack, 1));
|
||||||
printf("%c", pop(stack));
|
printf("%c", getIntValfromStackSlot(pop(stack)));
|
||||||
break;
|
break;
|
||||||
case PUSHG:
|
case PUSHG:
|
||||||
if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda));
|
push(stack, stackSlotWithObjRef(getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)));
|
||||||
break;
|
break;
|
||||||
case POPG:
|
case POPG:
|
||||||
if (debug == 1) printf("popg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
if (debug == 1) printf("popg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda);
|
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack).u.objRef, sda);
|
||||||
break;
|
break;
|
||||||
case ASF:
|
case ASF:
|
||||||
if (debug == 1) printf("asf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
if (debug == 1) printf("asf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
push(stack, fp);
|
push(stack, stackSlotWitchNumber(fp));
|
||||||
fp = *stack.current;
|
fp = *stack.current;
|
||||||
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||||
break;
|
break;
|
||||||
@ -111,7 +114,8 @@ void execute(struct program program) {
|
|||||||
if (debug == 1) printf("rsf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
if (debug == 1) printf("rsf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
*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));
|
||||||
fp = pop(stack);
|
tempSlot = pop(stack) ;
|
||||||
|
fp = getIntValfromStackSlot(tempSlot);
|
||||||
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])));
|
||||||
@ -123,42 +127,42 @@ 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 (pop(stack) != pop(stack)) push(stack, 1);
|
if (getIntValfromStackSlot(pop(stack)) != getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWitchNumber(1));
|
||||||
else push(stack, 0);
|
else push(stack, stackSlotWitchNumber(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 (pop(stack) == pop(stack)) push(stack, 1);
|
if (getIntValfromStackSlot(pop(stack)) == getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWitchNumber(1));
|
||||||
else push(stack, 0);
|
else push(stack, stackSlotWitchNumber(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 = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
if (pop(stack) < temp) push(stack, 1);
|
if (getIntValfromStackSlot(pop(stack)) < temp) push(stack, stackSlotWitchNumber(1));
|
||||||
else push(stack, 0);
|
else push(stack, stackSlotWitchNumber(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 = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
if (pop(stack) <= temp) push(stack, 1);
|
if (getIntValfromStackSlot(pop(stack)) <= temp) push(stack, stackSlotWitchNumber(1));
|
||||||
else push(stack, 0);
|
else push(stack, stackSlotWitchNumber(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 = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
if (pop(stack) > temp) push(stack, 1);
|
if (getIntValfromStackSlot(pop(stack)) > temp) push(stack, stackSlotWitchNumber(1));
|
||||||
else push(stack, 0);
|
else push(stack, stackSlotWitchNumber(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 = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
if (pop(stack) >= temp) push(stack, 1);
|
if (getIntValfromStackSlot(pop(stack)) >= temp) push(stack, stackSlotWitchNumber(1));
|
||||||
else push(stack, 0);
|
else push(stack, stackSlotWitchNumber(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])));
|
||||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||||
if (pop(stack) == 0) {
|
if (getIntValfromStackSlot(pop(stack)) == 0) {
|
||||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||||
if (debug == 1) printf("new i: %i\n", i);
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
}
|
}
|
||||||
@ -166,7 +170,7 @@ void execute(struct program program) {
|
|||||||
case BRT:
|
case BRT:
|
||||||
if (debug == 1) printf("brt: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
if (debug == 1) printf("brt: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||||
if (pop(stack) == 1) {
|
if (getIntValfromStackSlot(pop(stack)) == 1) {
|
||||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||||
if (debug == 1) printf("new i: %i\n", i);
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
}
|
}
|
||||||
@ -178,7 +182,7 @@ void execute(struct program program) {
|
|||||||
break;
|
break;
|
||||||
case CALL:
|
case CALL:
|
||||||
if (debug == 1) printf("call: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
if (debug == 1) printf("call: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
push(stack, i);
|
push(stack, stackSlotWitchNumber(i));
|
||||||
if (debug == 1) printf("push: %i\n", i + 1);
|
if (debug == 1) printf("push: %i\n", i + 1);
|
||||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||||
if (debug == 1) printf("new i: %i\n", i);
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
@ -186,23 +190,23 @@ void execute(struct program program) {
|
|||||||
case RET:
|
case RET:
|
||||||
if (debug == 1) printf("ret\n");
|
if (debug == 1) printf("ret\n");
|
||||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||||
i = pop(stack);
|
i = getIntValfromStackSlot(pop(stack));
|
||||||
if (debug == 1) printf("new i: %i\n", i);
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
break;
|
break;
|
||||||
case DROP:
|
case DROP:
|
||||||
*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:
|
||||||
temp = pop(stack);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
push(stack, temp);
|
push(stack, stackSlotWitchNumber(temp));
|
||||||
push(stack, temp);
|
push(stack, stackSlotWitchNumber(temp));
|
||||||
break;
|
break;
|
||||||
case PUSHR:
|
case PUSHR:
|
||||||
//TODO
|
//TODO
|
||||||
push(reg, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
pushR(reg, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
break;
|
break;
|
||||||
case POPR:
|
case POPR:
|
||||||
push(stack, pop(reg));
|
push(stack, stackSlotWitchNumber(popR(reg)));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -275,7 +279,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
if (run) {
|
if (run) {
|
||||||
printf("Ninja Virtual Machine started\n");
|
printf("Ninja Virtual Machine started\n");
|
||||||
unsigned int s[sizeSDA];
|
ObjRef s[sizeSDA];
|
||||||
sda.size = &sizeSDA;
|
sda.size = &sizeSDA;
|
||||||
sda.sda = s;
|
sda.sda = s;
|
||||||
if (debug == 1) printProgram(program);
|
if (debug == 1) printProgram(program);
|
||||||
|
|||||||
32
programs/prog04.asm
Normal file
32
programs/prog04.asm
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// prog04.asm -- call/ret with args, and with ret value
|
||||||
|
//
|
||||||
|
|
||||||
|
asf 3
|
||||||
|
pushc 11
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
pushc 11
|
||||||
|
pushc 33
|
||||||
|
call proc
|
||||||
|
drop 2
|
||||||
|
pushr
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
pushc 33
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
rsf
|
||||||
|
halt
|
||||||
|
|
||||||
|
proc:
|
||||||
|
asf 2
|
||||||
|
pushl -3
|
||||||
|
pushl -4
|
||||||
|
sub
|
||||||
|
popr
|
||||||
|
rsf
|
||||||
|
ret
|
||||||
49
reg.c
Executable file
49
reg.c
Executable file
@ -0,0 +1,49 @@
|
|||||||
|
//
|
||||||
|
// 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, 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 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
|
||||||
9
stack.c
9
stack.c
@ -6,11 +6,12 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "stackslot.c"
|
||||||
|
|
||||||
struct stack {
|
struct stack {
|
||||||
int* size;
|
int* size;
|
||||||
int* current;
|
int* current;
|
||||||
unsigned int *stack;
|
StackSlot *stack;
|
||||||
};
|
};
|
||||||
|
|
||||||
void printStack(struct stack stack, int fp) {
|
void printStack(struct stack stack, int fp) {
|
||||||
@ -21,7 +22,7 @@ void printStack(struct stack stack, int fp) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(struct stack s, unsigned int 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");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -30,7 +31,7 @@ void push(struct stack s, unsigned int value) {
|
|||||||
*s.current=*s.current + 1;
|
*s.current=*s.current + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int pop(struct stack s) {
|
StackSlot pop(struct stack s) {
|
||||||
if (*s.current == 0) {
|
if (*s.current == 0) {
|
||||||
printf("Stack Underflow\n");
|
printf("Stack Underflow\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -39,7 +40,7 @@ unsigned int pop(struct stack s) {
|
|||||||
return s.stack[*s.current];
|
return s.stack[*s.current];
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int peek(struct stack s, int steps) {
|
StackSlot 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);
|
||||||
|
|||||||
55
stackslot.c
Normal file
55
stackslot.c
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifndef STACKSLOT
|
||||||
|
#define STACKSLOT
|
||||||
|
typedef int Object;
|
||||||
|
typedef struct {
|
||||||
|
unsigned int size;
|
||||||
|
unsigned char data[1];
|
||||||
|
} *ObjRef;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
bool isObjRef;
|
||||||
|
union {
|
||||||
|
ObjRef objRef;
|
||||||
|
int number;
|
||||||
|
} u;
|
||||||
|
} StackSlot;
|
||||||
|
|
||||||
|
ObjRef getIntObj(int val){
|
||||||
|
ObjRef intObject;
|
||||||
|
unsigned int objSize = sizeof(unsigned int) + sizeof(int);
|
||||||
|
if ((intObject = malloc(objSize)) == NULL) {
|
||||||
|
perror("malloc");
|
||||||
|
}
|
||||||
|
*(int *)intObject->data=val;
|
||||||
|
return intObject;
|
||||||
|
}
|
||||||
|
int getValFromIntObj(ObjRef iref){
|
||||||
|
return *(int *)iref->data;
|
||||||
|
}
|
||||||
|
int getIntValfromStackSlot(StackSlot s){
|
||||||
|
if(s.isObjRef){
|
||||||
|
return *(int *)s.u.objRef->data;
|
||||||
|
}
|
||||||
|
return s.u.number;
|
||||||
|
}
|
||||||
|
void setValIntObj(ObjRef iref, int val){
|
||||||
|
*(int *)iref->data=val;
|
||||||
|
}
|
||||||
|
StackSlot stackSlotWithObjRef(ObjRef val){
|
||||||
|
StackSlot *stackSlot;
|
||||||
|
stackSlot=malloc(sizeof(StackSlot));
|
||||||
|
stackSlot->isObjRef=true;
|
||||||
|
stackSlot->u.objRef=val;
|
||||||
|
return *stackSlot;
|
||||||
|
}
|
||||||
|
StackSlot stackSlotWitchNumber(int val){
|
||||||
|
StackSlot *stackSlot;
|
||||||
|
stackSlot= malloc(sizeof(StackSlot));
|
||||||
|
stackSlot->isObjRef=false;
|
||||||
|
stackSlot->u.number=val;
|
||||||
|
return *stackSlot;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user