Compare commits
32 Commits
old
...
37843d9356
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37843d9356 | ||
|
|
6cc2ff87ca | ||
|
|
f29f500d0c | ||
|
|
23e787eb5e | ||
|
|
94fb404c71 | ||
| 847f68bb29 | |||
| da06afd43b | |||
| 9421d34756 | |||
|
|
60bdfd7843 | ||
| bdb0ce63b8 | |||
| dcb665f5a6 | |||
|
|
82dc5f28cc | ||
| 8be99c5244 | |||
| 742f050b24 | |||
| 14025d3b08 | |||
| 3f12378110 | |||
| 2cb94eaf76 | |||
| 9d64b87a09 | |||
| b87ebbe841 | |||
| 829735eb8c | |||
| 44f70a1777 | |||
| 5a7338f91c | |||
| a8999f4ec2 | |||
| c0a5e694c0 | |||
|
|
61a138fdee | ||
| 55af679c39 | |||
| 27400289f5 | |||
| c0d25212af | |||
| 10a6883750 | |||
|
|
33a2994c29 | ||
|
|
7390df9ee0 | ||
|
|
83dae77e31 |
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)
|
||||
|
||||
21
SDA.c
21
SDA.c
@@ -4,24 +4,23 @@
|
||||
#ifndef SDA
|
||||
#define SDA
|
||||
#include <stdio.h>
|
||||
|
||||
#include "stackslot.c"
|
||||
struct sda {
|
||||
unsigned int *sda;
|
||||
int size;
|
||||
int *size;
|
||||
ObjRef *sda;
|
||||
};
|
||||
|
||||
int getSDA(unsigned int offset, struct sda *sda) {
|
||||
return sda->sda[offset];
|
||||
ObjRef getSDA(int i, struct sda s) {
|
||||
return s.sda[i];
|
||||
}
|
||||
|
||||
void setSDA(unsigned int offset, int value, struct sda *sda) {
|
||||
sda->sda[offset] = value;
|
||||
void setSDA(int point, ObjRef val, struct sda s) {
|
||||
s.sda[point] = val;
|
||||
}
|
||||
|
||||
void printSDA(struct sda *sda) {
|
||||
printf("SDA:\n");
|
||||
for (int i = 0; i < sda->size; ++i) {
|
||||
printf("[%d] = %d\n", i, sda->sda[i]);
|
||||
void printSDA(struct sda s) {
|
||||
for (int i = 0; i < *s.size; i++) {
|
||||
printf("%i\n", *(int *)getSDA(i, s)->data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
#include <stdlib.h>
|
||||
#include "program.c"
|
||||
|
||||
unsigned int fromFile(char *path, struct program *program) {
|
||||
unsigned int fromFile(char *path, struct program program) {
|
||||
unsigned int countInstructions;
|
||||
unsigned int staticVars;
|
||||
FILE *fptr;
|
||||
fptr = fopen(path, "r");
|
||||
if (fptr == NULL) {
|
||||
printf("Error: cannot open code file %s\n", path);
|
||||
printf("Error: cannot open code file %s", path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
unsigned int buffer[4];
|
||||
|
||||
6
consts.c
6
consts.c
@@ -1,5 +1,7 @@
|
||||
#ifndef CONSTS
|
||||
#define CONSTS
|
||||
#define VERSION 4
|
||||
#define VERSION 5
|
||||
|
||||
#endif
|
||||
#endif /* ifndef CONSTS
|
||||
#define CONSTS
|
||||
#define VERSION 2; */
|
||||
|
||||
40
debugMenu.c
Normal file
40
debugMenu.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef DEBUGMENU
|
||||
#define DEBUGMENU
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "stack.c"
|
||||
#include "stack.c"
|
||||
|
||||
void inspect(struct stack s, int fp){
|
||||
char input[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 */}
|
||||
}
|
||||
void list(){
|
||||
//todo
|
||||
}
|
||||
void breakpoint(){
|
||||
//todo
|
||||
}
|
||||
|
||||
void debugMenu(int fp, struct stack stack, int* debug){
|
||||
char input[20];
|
||||
while (true) {
|
||||
printf("DEBUG: inspect, list, breakpoint, run, step, quit?");
|
||||
fgets(input, 20, stdin);
|
||||
printf("%s",input);
|
||||
if(input[0] == 'i') {inspect(stack,fp);}
|
||||
if(input[0] == 'l') list();
|
||||
if(input[0] == 'b') breakpoint();
|
||||
if(input[0] == 's') break;
|
||||
if(input[0] == 'r') {*debug = 0; break;};
|
||||
if(input[0] == 'q') exit(0);
|
||||
strcpy(input, "");
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* ifndef DEBUGMENU */
|
||||
@@ -34,5 +34,8 @@
|
||||
#define CALL 26
|
||||
#define RET 27
|
||||
#define DROP 28
|
||||
#define PUSHR 29
|
||||
#define POPR 30
|
||||
#define DUP 31
|
||||
|
||||
#endif /* ifndef INSREUKTION */
|
||||
|
||||
386
njvm.c
386
njvm.c
@@ -1,303 +1,251 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "consts.c"
|
||||
#include "instruktion.c"
|
||||
#include "code.c"
|
||||
#include "stack.c"
|
||||
#include "program.c"
|
||||
#include "codeReader.c"
|
||||
#include "SDA.c"
|
||||
#include "reg.c"
|
||||
#include "debugMenu.c"
|
||||
|
||||
// Program
|
||||
struct program *program;
|
||||
|
||||
// SDA
|
||||
int fp;
|
||||
// Debug
|
||||
int debug = 0;
|
||||
|
||||
// Stack
|
||||
struct stack stack;
|
||||
#define SIZE 1000
|
||||
|
||||
//Register
|
||||
struct reg reg;
|
||||
|
||||
// Program
|
||||
struct program program;
|
||||
|
||||
// SDA
|
||||
struct sda sda;
|
||||
unsigned fp;
|
||||
|
||||
void version(void) {
|
||||
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", VERSION, __DATE__, __TIME__);
|
||||
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
|
||||
}
|
||||
|
||||
void help(void) {
|
||||
printf("Usage: ./njvm [options] <code file>\n\t--debug\tstart virtual machine in debug mode\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n");
|
||||
}
|
||||
|
||||
void execute(struct program *program, struct sda *sda) {
|
||||
struct stack stack;
|
||||
stack.size = 1000;
|
||||
stack.currentFrame = 0;
|
||||
struct stack callStack;
|
||||
callStack.size = 1000;
|
||||
callStack.currentFrame = 0;
|
||||
|
||||
for (int i = 0; i < stack.size; ++i) {
|
||||
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
|
||||
frame->fp = malloc(sizeof(int) * stack.size);
|
||||
frame->sp = malloc(sizeof(int) * stack.size);
|
||||
frame->bp = NULL;
|
||||
stack.frames[i] = frame;
|
||||
}
|
||||
|
||||
for (int i = 0; i < callStack.size; ++i) {
|
||||
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
|
||||
frame->fp = malloc(sizeof(int) * stack.size);
|
||||
frame->sp = malloc(sizeof(int) * stack.size);
|
||||
frame->bp = NULL;
|
||||
callStack.frames[i] = frame;
|
||||
}
|
||||
|
||||
struct stackFrame *currentFrame = NULL;
|
||||
//struct stackFrame *currentCallFrame = NULL;
|
||||
unsigned int tmp;
|
||||
void execute(struct program program) {
|
||||
int i;
|
||||
int intInput;
|
||||
unsigned int temp;
|
||||
char charInput;
|
||||
|
||||
for (int i = 0; i < program->size; ++i) {
|
||||
unsigned int instruction = program->program[i];
|
||||
if (i >= program->size) {
|
||||
printf("Error: Jump out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
switch (instruction >> 24) {
|
||||
StackSlot tempSlot;
|
||||
for (i = 0; i < *program.size; ++i) {
|
||||
if (debug == 1) debugMenu(fp,stack,&debug);
|
||||
switch (program.program[i] >> 24) {
|
||||
case HALT:
|
||||
if (debug == 1) printf("halt\n");
|
||||
goto end;
|
||||
case PUSHC:
|
||||
if (debug == 1) printf("PUSHC %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
push(&stack, SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
if (debug == 1) printf("pushc: %i\n", IMMEDIATE(program.program[i]));
|
||||
push(stack, stackSlotWithObjRef(getIntObj(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||
break;
|
||||
case ADD:
|
||||
if (debug == 1) printf("ADD\n");
|
||||
push(&stack, pop(&stack) + pop(&stack));
|
||||
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:
|
||||
tmp = pop(&stack);
|
||||
if (debug == 1) printf("SUB\n");
|
||||
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||
push(&stack, pop(&stack) - tmp);
|
||||
if (debug == 1) printf("sub: %i - %i\n", peek(stack, 2), peek(stack, 1));
|
||||
temp = getIntValfromStackSlot(pop(stack));
|
||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) - temp)));
|
||||
break;
|
||||
case MUL:
|
||||
if (debug == 1) printf("MUL\n");
|
||||
push(&stack, pop(&stack) * pop(&stack));
|
||||
if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
|
||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) * getIntValfromStackSlot(pop(stack)))));
|
||||
break;
|
||||
case DIV:
|
||||
tmp = pop(&stack);
|
||||
if (debug == 1) printf("DIV\n");
|
||||
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||
push(&stack, pop(&stack) / tmp);
|
||||
if (debug == 1) printf("div: %i / %i\n", peek(stack, 2), peek(stack, 1));
|
||||
temp = getIntValfromStackSlot(pop(stack));
|
||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) / temp)));
|
||||
break;
|
||||
case MOD:
|
||||
tmp = pop(&stack);
|
||||
if (debug == 1) printf("MOD\n");
|
||||
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||
push(&stack, pop(&stack) % tmp);
|
||||
if (debug == 1) printf("mod: %i %% %i\n", peek(stack, 2), peek(stack, 1));
|
||||
temp = getIntValfromStackSlot(pop(stack));
|
||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)))));
|
||||
break;
|
||||
case RDINT:
|
||||
if (debug == 1) printf("rdint\n");
|
||||
scanf("%i", &intInput);
|
||||
if (debug == 1) printf("RDINT %d\n", intInput);
|
||||
push(&stack, intInput);
|
||||
push(stack, stackSlotWithObjRef(getIntObj(intInput)));
|
||||
if (debug == 1) printf("pushed %i\n", intInput);
|
||||
break;
|
||||
case WRINT:
|
||||
if (debug == 1) printf("WRINT\n");
|
||||
printf("%i", pop(&stack));
|
||||
if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
|
||||
printf("%i", getIntValfromStackSlot(pop(stack)));
|
||||
break;
|
||||
case RDCHR:
|
||||
if (debug == 1) printf("rdchr\n");
|
||||
scanf("%c", &charInput);
|
||||
if (debug == 1) printf("RDCHR %c\n", charInput);
|
||||
push(&stack, charInput);
|
||||
push(stack, stackSlotWithObjRef(getIntObj(charInput)));
|
||||
if (debug == 1) printf("pushed %c\n", charInput);
|
||||
break;
|
||||
case WRCHR:
|
||||
if (debug == 1) printf("WRCHR\n");
|
||||
printf("%c", pop(&stack));
|
||||
if (debug == 1) printf("wrchr: %c\n", peek(stack, 1));
|
||||
printf("%c", getIntValfromStackSlot(pop(stack)));
|
||||
break;
|
||||
case PUSHG:
|
||||
if (debug == 1) printf("PUSHG %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
|
||||
*currentFrame->sp++ = getSDA(SIGN_EXTEND(IMMEDIATE(instruction)), sda);
|
||||
if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWithObjRef(getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)));
|
||||
break;
|
||||
case POPG:
|
||||
if (debug == 1) printf("POPG %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
|
||||
setSDA(SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack), sda);
|
||||
if (debug == 1) printf("popg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack).u.objRef, sda);
|
||||
break;
|
||||
case ASF:
|
||||
if (debug == 1) printf("ASF %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
push(&stack, *currentFrame->sp);
|
||||
*currentFrame->sp = stack.currentFrame;
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
|
||||
stack.currentFrame += SIGN_EXTEND(IMMEDIATE(instruction));
|
||||
if (debug == 1) printf("asf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWitchNumber(fp));
|
||||
fp = *stack.current;
|
||||
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||
break;
|
||||
case RSF:
|
||||
if (debug == 1) printf("RSF\n");
|
||||
stack.currentFrame = pop(&stack);
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = NULL;
|
||||
break;
|
||||
case PUSHL:
|
||||
printf("PUSHL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->bp = currentFrame->sp;
|
||||
int x = SIGN_EXTEND(IMMEDIATE(instruction));
|
||||
push(&stack, currentFrame->fp[x]);
|
||||
if (debug == 1) printf("rsf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
*stack.current = fp;
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
tempSlot = pop(stack) ;
|
||||
fp = getIntValfromStackSlot(tempSlot);
|
||||
break;
|
||||
case POPL:
|
||||
if (debug == 1) printf("POPL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
currentFrame = stack.frames[stack.currentFrame];
|
||||
currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))] = pop(&stack);
|
||||
if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
|
||||
break;
|
||||
case EQ:
|
||||
if (debug == 1) printf("EQ\n");
|
||||
if (pop(&stack) == pop(&stack)) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
case PUSHL:
|
||||
if (debug == 1) printf("pushl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
|
||||
break;
|
||||
case NE:
|
||||
if (debug == 1) printf("NE\n");
|
||||
if (pop(&stack) != pop(&stack)) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
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));
|
||||
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));
|
||||
break;
|
||||
case LT:
|
||||
if (debug == 1) printf("LT\n");
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) < tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
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));
|
||||
break;
|
||||
case LE:
|
||||
if (debug == 1) printf("LE\n");
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) <= tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
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));
|
||||
break;
|
||||
case GT:
|
||||
if (debug == 1) printf("GT\n");
|
||||
if (debug == 1) printf("peek(1): %d\n", peek(&stack, 1));
|
||||
if (debug == 1) printf("peek(2): %d\n", peek(&stack, 2));
|
||||
if (debug == 1) printf("peek(1) > peek(2): %d\n", peek(&stack, 2) > peek(&stack, 1));
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) > tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
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));
|
||||
break;
|
||||
case GE:
|
||||
if (debug == 1) printf("GE\n");
|
||||
tmp = pop(&stack);
|
||||
if (pop(&stack) >= tmp) {
|
||||
push(&stack, 1);
|
||||
} else {
|
||||
push(&stack, 0);
|
||||
}
|
||||
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));
|
||||
break;
|
||||
case JMP:
|
||||
if (debug == 1) printf("JMP %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (debug == 1) printf("JMP %d\n", j);
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: Jump out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = j;
|
||||
break;
|
||||
case BRF: // branch on false
|
||||
if (debug == 1) printf("BRF %d\nStack: %d\n", SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack));
|
||||
if (pop(&stack) == 0) {
|
||||
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: BRF out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = j;
|
||||
case BRF:
|
||||
if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
if (getIntValfromStackSlot(pop(stack)) == 0) {
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
}
|
||||
break;
|
||||
case BRT:
|
||||
if (debug == 1) printf("BRT %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||
if (pop(&stack) == 1) {
|
||||
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: BRT out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = j;
|
||||
if (debug == 1) printf("brt: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
if (getIntValfromStackSlot(pop(stack)) == 1) {
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
}
|
||||
break;
|
||||
case JMP:
|
||||
if (debug == 1) printf("jmp: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
break;
|
||||
case CALL:
|
||||
tmp = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||
if (j-- >= program->size) {
|
||||
printf("Error: Call out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
push(&callStack, i + 1);
|
||||
i = tmp;
|
||||
if (debug == 1) printf("call: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
push(stack, stackSlotWitchNumber(i));
|
||||
if (debug == 1) printf("push: %i\n", i + 1);
|
||||
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
break;
|
||||
case RET:
|
||||
if (debug == 1) printf("RET\n");
|
||||
tmp = pop(&callStack);
|
||||
if (tmp-- >= program->size) {
|
||||
printf("Error: Return out of program memory\n");
|
||||
goto end;
|
||||
}
|
||||
i = tmp;
|
||||
if (debug == 1) printf("ret\n");
|
||||
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||
i = getIntValfromStackSlot(pop(stack));
|
||||
if (debug == 1) printf("new i: %i\n", i);
|
||||
break;
|
||||
case DROP:
|
||||
tmp = SIGN_EXTEND(IMMEDIATE(instruction));
|
||||
if (debug == 1) printf("DROP %d\n", tmp);
|
||||
for (int b = 0; b < tmp; ++b) {
|
||||
pop(&stack);
|
||||
}
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
return;
|
||||
}
|
||||
|
||||
// run prog2.bin
|
||||
void tests(void) {
|
||||
printf("Test started\n");
|
||||
/*struct sda *sda = malloc(sizeof(struct sda));
|
||||
unsigned int s[1000];
|
||||
sda->size = 1000;
|
||||
sda->sda = s;
|
||||
fromFile("prog2.bin", program);
|
||||
execute(program, (struct sda *) &sda);*/
|
||||
printf("Test finished\n");
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Initialize the Stack
|
||||
struct stack stack;
|
||||
int size = SIZE;
|
||||
stack.size = size;
|
||||
stack.currentFrame = 0;
|
||||
int current = 0;
|
||||
StackSlot s[SIZE];
|
||||
stack.size = &size;
|
||||
stack.current = ¤t;
|
||||
stack.stack = s;
|
||||
|
||||
// Initialize the registery
|
||||
int rSize = SIZE;
|
||||
int rCurrent = 0;
|
||||
unsigned int r[SIZE];
|
||||
reg.size = &rSize;
|
||||
reg.current = &rCurrent;
|
||||
reg.stack = r;
|
||||
|
||||
// Initialize ProgrammSpeicher
|
||||
int psize = SIZE;
|
||||
int psize = 1000;
|
||||
int saveProgram = 0;
|
||||
unsigned int p[1000];
|
||||
program = malloc(sizeof(struct program));
|
||||
program->size = psize;
|
||||
program->program = p;
|
||||
program.size = &psize;
|
||||
program.program = p;
|
||||
program.saveProgram = &saveProgram;
|
||||
|
||||
// Initialize runtime variables
|
||||
int run = 0;
|
||||
int sizeSDA;
|
||||
|
||||
@@ -311,6 +259,12 @@ int main(int argc, char *argv[]) {
|
||||
} else if (strcmp(argv[i], "--help") == 0) {
|
||||
help();
|
||||
return 0;
|
||||
} else if (strcmp(argv[i], "--stack") == 0) {
|
||||
i++;
|
||||
// TODO: implement stack size
|
||||
} else if(strcmp(argv[i], "--heap") == 0) {
|
||||
i++;
|
||||
// TODO: implement heap size
|
||||
} else {
|
||||
sizeSDA = fromFile(argv[i], program);
|
||||
run = 1;
|
||||
@@ -318,28 +272,20 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Debug mode
|
||||
*/
|
||||
if (debug) {
|
||||
tests();
|
||||
}
|
||||
|
||||
/*
|
||||
* Run program
|
||||
*/
|
||||
if (run) {
|
||||
printf("Ninja Virtual Machine started\n");
|
||||
struct sda *sda = malloc(sizeof(struct sda));
|
||||
unsigned int s[sizeSDA];
|
||||
sda->size = sizeSDA;
|
||||
sda->sda = s;
|
||||
ObjRef s[sizeSDA];
|
||||
sda.size = &sizeSDA;
|
||||
sda.sda = s;
|
||||
if (debug == 1) printProgram(program);
|
||||
execute(program, (struct sda *) &sda);
|
||||
execute(program);
|
||||
printf("Ninja Virtual Machine stopped\n");
|
||||
} else {
|
||||
printf("Error: no code file specified\n");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
50
program.c
50
program.c
@@ -9,21 +9,23 @@
|
||||
#include <stdio.h>
|
||||
|
||||
struct program {
|
||||
int *size;
|
||||
unsigned int *program;
|
||||
int size;
|
||||
int *saveProgram;
|
||||
};
|
||||
|
||||
void copyToProgram(const unsigned int codeToCopy[], int size, struct program *program) {
|
||||
void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
program->program[i] = codeToCopy[i];
|
||||
program.program[i] = codeToCopy[i];
|
||||
}
|
||||
program->size = size;
|
||||
*program.size = size;
|
||||
*program.saveProgram = 1;
|
||||
}
|
||||
|
||||
void printProgram(struct program *program) {
|
||||
void printProgram(struct program program) {
|
||||
char c[10];
|
||||
for (int i = 0; i < program->size; i++) {
|
||||
switch (program->program[i] >> 24) {
|
||||
for (int i = 0; i < *program.size; i++) {
|
||||
switch (program.program[i] >> 24) {
|
||||
case PUSHC:
|
||||
strcpy(c, "pushc");
|
||||
break;
|
||||
@@ -76,43 +78,55 @@ void printProgram(struct program *program) {
|
||||
strcpy(c, "popl");
|
||||
break;
|
||||
case EQ:
|
||||
strcpy(c,"eq");
|
||||
strcpy(c, "eq");
|
||||
break;
|
||||
case NE:
|
||||
strcpy(c,"ne");
|
||||
strcpy(c, "ne");
|
||||
break;
|
||||
case LT:
|
||||
strcpy(c,"lt");
|
||||
strcpy(c, "lt");
|
||||
break;
|
||||
case LE:
|
||||
strcpy(c, "le");
|
||||
break;
|
||||
case GT:
|
||||
strcpy(c,"gt");
|
||||
strcpy(c, "gt");
|
||||
break;
|
||||
case GE:
|
||||
strcpy(c,"ge");
|
||||
strcpy(c, "ge");
|
||||
break;
|
||||
case JMP:
|
||||
strcpy(c,"jmp");
|
||||
strcpy(c, "jmp");
|
||||
break;
|
||||
case BRF:
|
||||
strcpy(c, "brf");
|
||||
break;
|
||||
case BRT:
|
||||
strcpy(c,"brt");
|
||||
strcpy(c, "brt");
|
||||
break;
|
||||
case CALL:
|
||||
strcpy(c,"call");
|
||||
strcpy(c, "call");
|
||||
break;
|
||||
case RET:
|
||||
strcpy(c,"ret");
|
||||
strcpy(c, "ret");
|
||||
break;
|
||||
case DROP:
|
||||
strcpy(c,"drop");
|
||||
strcpy(c, "drop");
|
||||
break;
|
||||
case PUSHR:
|
||||
strcpy(c, "pushr");
|
||||
break;
|
||||
case POPR:
|
||||
strcpy(c, "popr");
|
||||
break;
|
||||
case DUP:
|
||||
strcpy(c, "dup");
|
||||
break;
|
||||
default:
|
||||
strcpy(c, "ERROR");
|
||||
break;
|
||||
}
|
||||
IMMEDIATE(program->program[i]) ? printf("%03i:\t%s\t%i\n", i, c, SIGN_EXTEND(IMMEDIATE(program->program[i]))) : printf(
|
||||
IMMEDIATE(program.program[i]) ? printf("%03i:\t%s\t%i\n", i, c, SIGN_EXTEND(IMMEDIATE(program.program[i]))) : printf(
|
||||
"%03i:\t%s\n", i, c);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
programs/nja
Executable file
BIN
programs/nja
Executable file
Binary file not shown.
@@ -1,11 +0,0 @@
|
||||
pushc 3
|
||||
pushc 4
|
||||
add
|
||||
pushc 10
|
||||
pushc 6
|
||||
sub
|
||||
mul
|
||||
wrint
|
||||
pushc 10
|
||||
wrchr
|
||||
halt
|
||||
Binary file not shown.
@@ -1,9 +0,0 @@
|
||||
pushc -2
|
||||
rdint
|
||||
mul
|
||||
pushc 3
|
||||
add
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
halt
|
||||
Binary file not shown.
@@ -1,5 +0,0 @@
|
||||
rdchr
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
halt
|
||||
Binary file not shown.
28
programs/prog02.asm
Normal file
28
programs/prog02.asm
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// prog02.asm -- call/ret with args, but without ret value
|
||||
//
|
||||
|
||||
asf 3
|
||||
pushc 11
|
||||
pushc 33
|
||||
call proc
|
||||
drop 2
|
||||
rsf
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 2
|
||||
pushl -4
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushl -3
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
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
|
||||
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
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,61 +1,9 @@
|
||||
//
|
||||
// prog1.asm -- an assembler example with global variables
|
||||
//
|
||||
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// global Integer x;
|
||||
// global Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
// if (x > y) {
|
||||
// x = x - y;
|
||||
// } else {
|
||||
// y = y - x;
|
||||
// }
|
||||
// }
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popg 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popg 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushg 0 // 4
|
||||
pushg 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushg 0
|
||||
pushg 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushg 0
|
||||
pushg 1
|
||||
sub
|
||||
popg 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushg 1 // 17
|
||||
pushg 0
|
||||
sub
|
||||
popg 1
|
||||
L4:
|
||||
jmp L1 // 21
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushg 0 // 22
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 1
|
||||
asf 2
|
||||
asf 4
|
||||
pushc 23
|
||||
asf 3
|
||||
rsf
|
||||
rsf
|
||||
rsf
|
||||
halt
|
||||
|
||||
Binary file not shown.
@@ -1,12 +1,12 @@
|
||||
//
|
||||
// prog2.asm -- an assembler example with local variables
|
||||
// prog1.asm -- an assembler example with global variables
|
||||
//
|
||||
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// local Integer x;
|
||||
// local Integer y;
|
||||
// global Integer x;
|
||||
// global Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
@@ -19,45 +19,43 @@
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
|
||||
asf 2
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popl 0
|
||||
popg 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popl 1
|
||||
popg 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushl 0
|
||||
pushl 1
|
||||
pushg 0
|
||||
pushg 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushl 0
|
||||
pushl 1
|
||||
pushg 0
|
||||
pushg 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushl 0
|
||||
pushl 1
|
||||
pushg 0
|
||||
pushg 1
|
||||
sub
|
||||
popl 0
|
||||
popg 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushl 1
|
||||
pushl 0
|
||||
pushg 1
|
||||
pushg 0
|
||||
sub
|
||||
popl 1
|
||||
popg 1
|
||||
L4:
|
||||
jmp L1
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushl 0
|
||||
pushg 0
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
halt
|
||||
|
||||
Binary file not shown.
@@ -1,35 +1,63 @@
|
||||
//
|
||||
// prog01.asm -- call/ret without args, and without ret value
|
||||
// prog2.asm -- an assembler example with local variables
|
||||
//
|
||||
|
||||
asf 3
|
||||
pushc 11
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
call proc
|
||||
pushc 44
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// local Integer x;
|
||||
// local Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
// if (x > y) {
|
||||
// x = x - y;
|
||||
// } else {
|
||||
// y = y - x;
|
||||
// }
|
||||
// }
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
|
||||
asf 2
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popl 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popl 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushl 0
|
||||
pushl 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushl 0
|
||||
pushl 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushl 0
|
||||
pushl 1
|
||||
sub
|
||||
popl 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushl 1
|
||||
pushl 0
|
||||
sub
|
||||
popl 1
|
||||
L4:
|
||||
jmp L1
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushl 0
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 2
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
call proctwo
|
||||
rsf
|
||||
ret
|
||||
|
||||
proctwo:
|
||||
asf 2
|
||||
pushc 33
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
|
||||
Binary file not shown.
@@ -1,28 +1,61 @@
|
||||
//
|
||||
// prog02.asm -- call/ret with args, but without ret value
|
||||
// prog1.asm -- an assembler example with global variables
|
||||
//
|
||||
|
||||
asf 3
|
||||
pushc 11
|
||||
pushc 33
|
||||
call proc
|
||||
drop 2
|
||||
rsf
|
||||
//
|
||||
// compute the gcd of two positive numbers
|
||||
//
|
||||
// global Integer x;
|
||||
// global Integer y;
|
||||
// x = readInteger();
|
||||
// y = readInteger();
|
||||
// while (x != y) {
|
||||
// if (x > y) {
|
||||
// x = x - y;
|
||||
// } else {
|
||||
// y = y - x;
|
||||
// }
|
||||
// }
|
||||
// writeInteger(x);
|
||||
// writeCharacter('\n');
|
||||
|
||||
// x = readInteger();
|
||||
rdint
|
||||
popg 0
|
||||
// y = readInteger();
|
||||
rdint
|
||||
popg 1
|
||||
// while ...
|
||||
L1:
|
||||
// x != y
|
||||
pushg 0
|
||||
pushg 1
|
||||
ne
|
||||
brf L2
|
||||
// if ...
|
||||
pushg 0
|
||||
pushg 1
|
||||
gt
|
||||
brf L3
|
||||
// x = x - y
|
||||
pushg 0
|
||||
pushg 1
|
||||
sub
|
||||
popg 0
|
||||
jmp L4
|
||||
L3:
|
||||
// y = y - x
|
||||
pushg 1
|
||||
pushg 0
|
||||
sub
|
||||
popg 1
|
||||
L4:
|
||||
jmp L1
|
||||
L2:
|
||||
// writeInteger(x);
|
||||
pushg 0
|
||||
wrint
|
||||
// writeCharacter('\n');
|
||||
pushc '\n'
|
||||
wrchr
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 2
|
||||
pushl -4
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushl -3
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
|
||||
@@ -1,28 +1,17 @@
|
||||
//
|
||||
// prog02.asm -- call/ret with args, but without ret value
|
||||
// prog04.asm -- call/ret with args, and with ret value
|
||||
//
|
||||
|
||||
asf 3
|
||||
pushc 11
|
||||
pushc 33
|
||||
call proc
|
||||
drop 2
|
||||
rsf
|
||||
halt
|
||||
pushc 12
|
||||
rdint
|
||||
mul
|
||||
wrint
|
||||
call proc
|
||||
halt
|
||||
|
||||
proc:
|
||||
asf 2
|
||||
pushl 4
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushc 22
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
pushl 3
|
||||
wrint
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
asf 1
|
||||
pushc '\n'
|
||||
wrchr
|
||||
rsf
|
||||
ret
|
||||
|
||||
Binary file not shown.
48
reg.c
Executable file
48
reg.c
Executable file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// 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
|
||||
65
stack.c
65
stack.c
@@ -3,46 +3,57 @@
|
||||
//
|
||||
#ifndef STACK
|
||||
#define STACK
|
||||
#define SIZE 1000
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct stackFrame {
|
||||
int *fp; // Frame pointer
|
||||
int *sp; // Stack pointer
|
||||
int *bp; // Base pointer
|
||||
};
|
||||
#include "stackslot.c"
|
||||
|
||||
struct stack {
|
||||
int size;
|
||||
int currentFrame;
|
||||
struct stackFrame *frames[SIZE];
|
||||
int* size;
|
||||
int* current;
|
||||
StackSlot *stack;
|
||||
};
|
||||
|
||||
void printStack(struct stack *stack) {
|
||||
printf("Stack:\n");
|
||||
for (int i = 0; i < stack->size; ++i) {
|
||||
printf("[%d] = %d\n", i, stack->frames[stack->currentFrame]->sp[i]);
|
||||
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);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
void push(struct stack *stack, unsigned int value) {
|
||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||
*(currentFrame->sp) = value;
|
||||
currentFrame->sp++;
|
||||
void push(struct stack s, StackSlot value) {
|
||||
if (*s.current >= *s.size) {
|
||||
printf("Stack Overflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
s.stack[*s.current] = value;
|
||||
*s.current=*s.current + 1;
|
||||
}
|
||||
|
||||
int pop(struct stack *stack) {
|
||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||
currentFrame->sp--;
|
||||
return *(currentFrame->sp);
|
||||
StackSlot pop(struct stack s) {
|
||||
if (*s.current == 0) {
|
||||
printf("Stack Underflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*s.current = *s.current -1;
|
||||
return s.stack[*s.current];
|
||||
}
|
||||
|
||||
int peek(struct stack *stack, int steps) { // peek is pop without removing the value
|
||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||
return *(currentFrame->sp - steps);
|
||||
int peek(struct stack s, int steps) {
|
||||
if (*s.current - steps < 0) {
|
||||
printf("Stack Underflow\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return getIntValfromStackSlot(s.stack[*s.current - steps]);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
57
stackslot.c
Normal file
57
stackslot.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#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;
|
||||
intObject->size=sizeof(int);
|
||||
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){
|
||||
iref->size=sizeof(int);
|
||||
*(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
|
||||
Reference in New Issue
Block a user