add bigint to execute

This commit is contained in:
Elias Bennour 2024-01-18 18:53:46 +01:00
parent e5f2858a5c
commit 82d3201374
4 changed files with 71 additions and 81 deletions

View File

@ -1,43 +1,11 @@
# 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
CC = gcc
run: build
./njvm programs/prog1.bin
# program to Run
F = programs/prog1.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)
debug: build
./njvm --debug prog.bin

View File

@ -1,6 +1,6 @@
#ifndef CONSTS
#define CONSTS
#define VERSION 5
#define VERSION 2
#endif /* ifndef CONSTS
#define CONSTS

102
njvm.c
View File

@ -44,7 +44,6 @@ void help(void) {
void execute(struct program program) {
int i;
int intInput;
unsigned int temp;
char charInput;
StackSlot tempSlot;
@ -57,44 +56,54 @@ void execute(struct program program) {
goto end;
case PUSHC:
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;
case ADD:
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.op1 = pop(stack).u.objRef;
bigAdd();
push(stack, stackSlotWithObjRef(bip.res));
break;
case SUB:
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)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
bigSub();
push(stack, stackSlotWithObjRef(bip.res));
break;
case MUL:
if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
push(stack, stackSlotWithObjRef(
getIntObj(getIntValfromStackSlot(pop(stack)) * getIntValfromStackSlot(pop(stack)))));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
bigMul();
push(stack, stackSlotWithObjRef(bip.res));
break;
case DIV:
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)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
bigDiv();
push(stack, stackSlotWithObjRef(bip.res));
break;
case MOD:
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)))));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
bigDiv();
push(stack, stackSlotWithObjRef(bip.rem));
break;
case RDINT:
if (debug == 1) printf("rdint\n");
scanf("%i", &intInput);
push(stack, stackSlotWithObjRef(getIntObj(intInput)));
if (debug == 1) printf("pushed %i\n", intInput);
bigRead(stdin);
push(stack, stackSlotWithObjRef(bip.res));
if (debug == 1) printf("pushed %i\n", peek(stack, 1));
break;
case WRINT:
if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
printf("%i", getIntValfromStackSlot(pop(stack)));
bip.op1 = pop(stack).u.objRef;
bigPrint(stdout);
break;
case RDCHR:
if (debug == 1) printf("rdchr\n");
@ -104,7 +113,8 @@ void execute(struct program program) {
break;
case WRCHR:
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;
case PUSHG:
if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
@ -137,58 +147,70 @@ void execute(struct program program) {
break;
case NE:
if (debug == 1) printf("ne: %i != %i\n", peek(stack, 2), peek(stack, 1));
if (getIntValfromStackSlot(pop(stack)) != getIntValfromStackSlot(pop(stack))) push(stack,
stackSlotWithObjRef(
getIntObj(
1)));
else push(stack, stackSlotWithObjRef(getIntObj(0)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
if (bigCmp() != 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
else push(stack, stackSlotWithObjRef(getIntObj(false)));
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,
stackSlotWithObjRef(
getIntObj(
1)));
else push(stack, stackSlotWithObjRef(getIntObj(0)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
if (bigCmp() == 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
else push(stack, stackSlotWithObjRef(getIntObj(false)));
break;
case LT:
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, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWithObjRef(getIntObj(0)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
if (bigCmp() < 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
else push(stack, stackSlotWithObjRef(getIntObj(false)));
break;
case LE:
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, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWithObjRef(getIntObj(0)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
if (bigCmp() <= 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
else push(stack, stackSlotWithObjRef(getIntObj(false)));
break;
case GT:
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, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWithObjRef(getIntObj(0)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
if (bigCmp() > 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
else push(stack, stackSlotWithObjRef(getIntObj(false)));
break;
case GE:
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, stackSlotWithObjRef(getIntObj(1)));
else push(stack, stackSlotWithObjRef(getIntObj(0)));
bip.op2 = pop(stack).u.objRef;
bip.op1 = pop(stack).u.objRef;
if (bigCmp() >= 0) push(stack, stackSlotWithObjRef(getIntObj(true)));
else push(stack, stackSlotWithObjRef(getIntObj(false)));
break;
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) {
bip.op1 = pop(stack).u.objRef;
int b = bigToInt();
if (b == false) {
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
if (debug == 1) printf("new i: %i\n", i);
} else if (b != true) {
printf("Error: brf: %i\n", b);
exit(EXIT_FAILURE);
}
break;
case BRT:
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) {
bip.op1 = pop(stack).u.objRef;
b = bigToInt();
if (b == true) {
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
if (debug == 1) printf("new i: %i\n", i);
} else if (b != false) {
printf("Error: brt: %i\n", b);
exit(EXIT_FAILURE);
}
break;
case JMP:

BIN
njvm.o

Binary file not shown.