add bigint to execute
This commit is contained in:
parent
e5f2858a5c
commit
82d3201374
48
Makefile
48
Makefile
@ -1,43 +1,11 @@
|
|||||||
# Makefile for a simple C program
|
# Makefile for a simple C program
|
||||||
|
build:
|
||||||
|
gcc -g -Wall -Ibigint/build/include -o support.o -c support.c
|
||||||
|
gcc -g -Wall -Ibigint/build/include -o njvm.o -c njvm.c
|
||||||
|
gcc -g -Wall -Lbigint/build/lib -o njvm njvm.o support.o -lbigint
|
||||||
|
|
||||||
# Compiler
|
run: build
|
||||||
CC = gcc
|
./njvm programs/prog1.bin
|
||||||
|
|
||||||
# program to Run
|
debug: build
|
||||||
F = programs/prog1.bin
|
./njvm --debug prog.bin
|
||||||
|
|
||||||
# Compiler flags
|
|
||||||
CFLAGS = -g -Wall -Ibigint/build/include
|
|
||||||
|
|
||||||
LDFLAGS = -g -Wall -Lbigint/build/lib
|
|
||||||
|
|
||||||
# Source file
|
|
||||||
SRC = njvm.c
|
|
||||||
|
|
||||||
# Executable name
|
|
||||||
TARGET = njvm
|
|
||||||
|
|
||||||
njvm.o:
|
|
||||||
$(CC) $(CFLAGS) -o njvm.o -c njvm.c
|
|
||||||
|
|
||||||
support.o:
|
|
||||||
$(CC) $(CFLAGS) -o support.o -c support.c
|
|
||||||
|
|
||||||
# Default target
|
|
||||||
all: njvm.o support.o
|
|
||||||
$(CC) $(LDFLAGS) -o $(TARGET) njvm.o support.o -lbigint
|
|
||||||
# Clean up
|
|
||||||
clean:
|
|
||||||
rm -f $(OBJ) $(TARGET)
|
|
||||||
|
|
||||||
debug: all
|
|
||||||
./$(TARGET) --debug $(F)
|
|
||||||
|
|
||||||
run: all
|
|
||||||
./$(TARGET) $(F)
|
|
||||||
|
|
||||||
nja: ./nja/nja$(V)
|
|
||||||
./nja/nja$(V) $(IN) $(OUT)
|
|
||||||
|
|
||||||
njc: ./njc/njc$(V)
|
|
||||||
./njc/njc$(V) $(IN) $(OUT)
|
|
||||||
2
consts.c
2
consts.c
@ -1,6 +1,6 @@
|
|||||||
#ifndef CONSTS
|
#ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
#define VERSION 5
|
#define VERSION 2
|
||||||
|
|
||||||
#endif /* ifndef CONSTS
|
#endif /* ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
|
|||||||
102
njvm.c
102
njvm.c
@ -44,7 +44,6 @@ void help(void) {
|
|||||||
|
|
||||||
void execute(struct program program) {
|
void execute(struct program program) {
|
||||||
int i;
|
int i;
|
||||||
int intInput;
|
|
||||||
unsigned int temp;
|
unsigned int temp;
|
||||||
char charInput;
|
char charInput;
|
||||||
StackSlot tempSlot;
|
StackSlot tempSlot;
|
||||||
@ -57,44 +56,54 @@ void execute(struct program program) {
|
|||||||
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, stackSlotWithObjRef(getIntObj(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
bigFromInt(SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
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", peek(stack, 2), peek(stack, 1));
|
||||||
bip.op1 = pop(stack).u.objRef;
|
|
||||||
bip.op2 = pop(stack).u.objRef;
|
bip.op2 = pop(stack).u.objRef;
|
||||||
|
bip.op1 = pop(stack).u.objRef;
|
||||||
bigAdd();
|
bigAdd();
|
||||||
push(stack, stackSlotWithObjRef(bip.res));
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
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 = getIntValfromStackSlot(pop(stack));
|
bip.op2 = pop(stack).u.objRef;
|
||||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) - temp)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
bigSub();
|
||||||
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
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, stackSlotWithObjRef(
|
bip.op2 = pop(stack).u.objRef;
|
||||||
getIntObj(getIntValfromStackSlot(pop(stack)) * getIntValfromStackSlot(pop(stack)))));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
bigMul();
|
||||||
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
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 = getIntValfromStackSlot(pop(stack));
|
bip.op2 = pop(stack).u.objRef;
|
||||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) / temp)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
bigDiv();
|
||||||
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
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 = getIntValfromStackSlot(pop(stack));
|
bip.op2 = pop(stack).u.objRef;
|
||||||
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)))));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
bigDiv();
|
||||||
|
push(stack, stackSlotWithObjRef(bip.rem));
|
||||||
break;
|
break;
|
||||||
case RDINT:
|
case RDINT:
|
||||||
if (debug == 1) printf("rdint\n");
|
if (debug == 1) printf("rdint\n");
|
||||||
scanf("%i", &intInput);
|
bigRead(stdin);
|
||||||
push(stack, stackSlotWithObjRef(getIntObj(intInput)));
|
push(stack, stackSlotWithObjRef(bip.res));
|
||||||
if (debug == 1) printf("pushed %i\n", intInput);
|
if (debug == 1) printf("pushed %i\n", peek(stack, 1));
|
||||||
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", getIntValfromStackSlot(pop(stack)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
bigPrint(stdout);
|
||||||
break;
|
break;
|
||||||
case RDCHR:
|
case RDCHR:
|
||||||
if (debug == 1) printf("rdchr\n");
|
if (debug == 1) printf("rdchr\n");
|
||||||
@ -104,7 +113,8 @@ void execute(struct program program) {
|
|||||||
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", getIntValfromStackSlot(pop(stack)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
printf("%c", bigToInt());
|
||||||
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])));
|
||||||
@ -137,58 +147,70 @@ 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,
|
bip.op2 = pop(stack).u.objRef;
|
||||||
stackSlotWithObjRef(
|
bip.op1 = pop(stack).u.objRef;
|
||||||
getIntObj(
|
if (bigCmp() != 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
|
||||||
1)));
|
else push(stack, stackSlotWithObjRef(getIntObj(false)));
|
||||||
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,
|
bip.op2 = pop(stack).u.objRef;
|
||||||
stackSlotWithObjRef(
|
bip.op1 = pop(stack).u.objRef;
|
||||||
getIntObj(
|
if (bigCmp() == 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
|
||||||
1)));
|
else push(stack, stackSlotWithObjRef(getIntObj(false)));
|
||||||
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));
|
bip.op2 = pop(stack).u.objRef;
|
||||||
if (getIntValfromStackSlot(pop(stack)) < temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
else push(stack, stackSlotWithObjRef(getIntObj(0)));
|
if (bigCmp() < 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
|
||||||
|
else push(stack, stackSlotWithObjRef(getIntObj(false)));
|
||||||
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));
|
bip.op2 = pop(stack).u.objRef;
|
||||||
if (getIntValfromStackSlot(pop(stack)) <= temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
else push(stack, stackSlotWithObjRef(getIntObj(0)));
|
if (bigCmp() <= 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
|
||||||
|
else push(stack, stackSlotWithObjRef(getIntObj(false)));
|
||||||
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));
|
bip.op2 = pop(stack).u.objRef;
|
||||||
if (getIntValfromStackSlot(pop(stack)) > temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
else push(stack, stackSlotWithObjRef(getIntObj(0)));
|
if (bigCmp() > 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
|
||||||
|
else push(stack, stackSlotWithObjRef(getIntObj(false)));
|
||||||
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));
|
bip.op2 = pop(stack).u.objRef;
|
||||||
if (getIntValfromStackSlot(pop(stack)) >= temp) push(stack, stackSlotWithObjRef(getIntObj(1)));
|
bip.op1 = pop(stack).u.objRef;
|
||||||
else push(stack, stackSlotWithObjRef(getIntObj(0)));
|
if (bigCmp() >= 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
|
||||||
|
else push(stack, stackSlotWithObjRef(getIntObj(false)));
|
||||||
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 (getIntValfromStackSlot(pop(stack)) == 0) {
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
int b = bigToInt();
|
||||||
|
if (b == false) {
|
||||||
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);
|
||||||
|
} else if (b != true) {
|
||||||
|
printf("Error: brf: %i\n", b);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
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 (getIntValfromStackSlot(pop(stack)) == 1) {
|
bip.op1 = pop(stack).u.objRef;
|
||||||
|
b = bigToInt();
|
||||||
|
if (b == true) {
|
||||||
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);
|
||||||
|
} else if (b != false) {
|
||||||
|
printf("Error: brt: %i\n", b);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case JMP:
|
case JMP:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user