This commit is contained in:
Elias Bennour 2024-01-18 16:21:34 +01:00
parent ac57431ac4
commit dbafdbeea5
22 changed files with 8 additions and 1464 deletions

View File

@ -7,7 +7,7 @@ CC = gcc
F = prog.bin F = prog.bin
# Compiler flags # Compiler flags
CFLAGS = -I ./bigint/build/include -L ./bigint/build/lib -g -Wall -std=c99 -pedantic CFLAGS = -I ./bigint -L ./bigint -g -Wall -std=c99 -pedantic -v -lbigint -lm
# Source file # Source file
SRC = njvm.c SRC = njvm.c

View File

@ -1,22 +0,0 @@
#
# Makefile for big integer library and test
#
DIRS = src tst
all:
for i in $(DIRS) ; do \
$(MAKE) -C $$i install ; \
done
clean:
for i in $(DIRS) ; do \
$(MAKE) -C $$i clean ; \
done
rm -rf ./build
rm -f *~
dist: clean
(cd .. ; \
tar -cvf bigint.tar bigint ; \
gzip -f bigint.tar)

BIN
bigint/bigint.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
bigint/libbigint.a Normal file

Binary file not shown.

View File

@ -1,26 +0,0 @@
#
# Makefile for big integer library
#
BUILD = ../build
CC = gcc
CFLAGS = -g -Wall
all: support.h bigint.h libbigint.a
install: all
mkdir -p $(BUILD)/include
cp support.h $(BUILD)/include
cp bigint.h $(BUILD)/include
mkdir -p $(BUILD)/lib
cp libbigint.a $(BUILD)/lib
libbigint.a: bigint.o
ar -crs libbigint.a bigint.o
bigint.o: bigint.c bigint.h support.h
$(CC) $(CFLAGS) -o bigint.o -c bigint.c
clean:
rm -f *~ bigint.o libbigint.a

View File

@ -1,60 +0,0 @@
/*
* bigint.h -- big integer library
*/
#ifndef _BIGINT_H_
#define _BIGINT_H_
/* object representation */
typedef void* BigObjRef;
#include <stdio.h>
typedef struct {
int nd; /* number of digits; array may be bigger */
/* nd = 0 exactly when number = 0 */
unsigned char sign; /* one of BIG_NEGATIVE or BIG_POSITIVE */
/* zero always has BIG_POSITIVE here */
unsigned char digits[1]; /* the digits proper; number base is 256 */
/* LS digit first; MS digit is not zero */
} Big;
#include "support.h"
/* big integer processor registers */
typedef struct {
BigObjRef op1; /* first (or single) operand */
BigObjRef op2; /* second operand (if present) */
BigObjRef res; /* result of operation */
BigObjRef rem; /* remainder in case of division */
} BIP;
extern BIP bip; /* registers of the processor */
/* big integer processor functions */
int bigSgn(void); /* sign */
int bigCmp(void); /* comparison */
void bigNeg(void); /* negation */
void bigAdd(void); /* addition */
void bigSub(void); /* subtraction */
void bigMul(void); /* multiplication */
void bigDiv(void); /* division */
void bigFromInt(int n); /* conversion int --> big */
int bigToInt(void); /* conversion big --> int */
void bigRead(FILE *in); /* read a big integer */
void bigPrint(FILE *out); /* print a big integer */
void bigDump(FILE *out, BigObjRef bigObjRef); /* dump a big integer object */
#endif /* _BIGINT_H_ */

Binary file not shown.

Binary file not shown.

View File

@ -1,15 +0,0 @@
/*
* support.h -- object representation and support functions
*/
#ifndef _SUPPORT_H_
#define _SUPPORT_H_
/* support functions */
void fatalError(char *msg); /* print a message and exit */
void * newPrimObject(int dataSize); /* create a new primitive object */
void * getPrimObjectDataPointer(void * primObject);
#endif /* _SUPPORT_H_ */

View File

@ -1,28 +0,0 @@
#
# Makefile for big integer test
#
BUILD = ../build
CC = gcc
CFLAGS = -g -Wall -I$(BUILD)/include
LDFLAGS = -g -Wall -L$(BUILD)/lib
LDLIBS = -lbigint
all: testbip
install: all
mkdir -p $(BUILD)/bin
cp testbip $(BUILD)/bin
testbip: testbip.o support.o
$(CC) $(LDFLAGS) -o testbip testbip.o support.o $(LDLIBS)
testbip.o: testbip.c
$(CC) $(CFLAGS) -o testbip.o -c testbip.c
support.o: support.c
$(CC) $(CFLAGS) -o support.o -c support.c
clean:
rm -f *~ testbip.o support.o testbip

View File

@ -1,55 +0,0 @@
/*
* support.c -- support functions for big integer library
*/
#include <stdio.h>
#include <stdlib.h>
#include "support.h"
typedef struct {
unsigned int size; /* byte count of payload data */
unsigned char data[1]; /* payload data, size as needed */
} *ObjRef;
/*
* This routine is called in case a fatal error has occurred.
* It should print the error message and terminate the program.
*/
void fatalError(char *msg) {
printf("Fatal error: %s\n", msg);
exit(1);
}
/*
* This function is called whenever a new primitive object with
* a certain amount of internal memory is needed. It should return
* an object reference to a regular object, which contains a freely
* usable memory area of at least the requested size (measured in
* bytes). The memory area need not be initialized in any way.
*
* Note that this function may move all objects in memory at will
* (due to, e.g., garbage collection), as long as the pointers in
* the global "bip" structure point to the correct objects when
* the function returns.
*/
void * newPrimObject(int dataSize) {
ObjRef bigObjRef;
bigObjRef = malloc(sizeof(unsigned int) +
dataSize * sizeof(unsigned char));
if (bigObjRef == NULL) {
fatalError("newPrimObject() got no memory");
}
bigObjRef->size = dataSize;
return bigObjRef;
}
void * getPrimObjectDataPointer(void * obj){
ObjRef oo = ((ObjRef) (obj));
return oo->data;
}

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

5
njvm.c
View File

@ -8,6 +8,8 @@
#include "codeReader.c" #include "codeReader.c"
#include "SDA.c" #include "SDA.c"
#include "debugMenu.c" #include "debugMenu.c"
#include "bigint.h"
#include "support.h"
// Debug // Debug
int debug = 0; int debug = 0;
@ -49,7 +51,8 @@ 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(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));

View File

@ -44,9 +44,9 @@ ObjRefContainer getRefs(struct stack stack){
} }
continer.size = counter; continer.size = counter;
ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef)); ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef));
for (int i = 0; i<= *stack.current; i++) for (int j = 0; j<= *stack.current; j++)
if(stack.stack[i].isObjRef == true) if(stack.stack[j].isObjRef == true)
list[counter--] = stack.stack[i].u.objRef; list[counter--] = stack.stack[j].u.objRef;
continer.refs = list; continer.refs = list;
return continer; return continer;
} }