Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix and perf improvement #34

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
18 changes: 14 additions & 4 deletions src/bigint.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,20 +1270,30 @@ void fprintBigInt(FILE *f, BigInt *x) {
fprintf(f, "<null>");
return;
}
bigint_fprint(f, &x->bi);
if (bigint_flag) {
bigint_fprint(f, &x->bi);
} else {
fprintf(f, "%d", x->little);
}
}

void sprintBigInt(char *s, BigInt *x) {
if (x == NULL) {
sprintf(s, "<null>");
return;
}
int size = bigint_write_size(&x->bi, 10);
bigint_write(s, size, &x->bi);
if (bigint_flag) {
int size = bigint_write_size(&x->bi, 10);
bigint_write(s, size, &x->bi);
} else {
sprintf(s, "%d", x->little);
}
}

int cmpBigInt(BigInt *a, BigInt *b) {
return bigint_cmp(&a->bi, &b->bi);
if (bigint_flag)
return bigint_cmp(&a->bi, &b->bi);
return a->little < b->little ? -1 : a->little == b->little ? 0 : 1;
}

static BigInt *_opBigInt(bigint_binop op, BigInt *a, BigInt *b) {
Expand Down
1 change: 1 addition & 0 deletions src/bigint.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "memory.h"

/* any unsigned integer type */
Expand Down
60 changes: 4 additions & 56 deletions src/bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string.h>

#include "bytecode.h"
#include "debug.h"
#include "common.h"

#ifdef DEBUG_BYTECODE
Expand Down Expand Up @@ -58,7 +59,7 @@ static void growCapacity(ByteCodeArray *byteCodes, int newCapacity) {
byteCodes->capacity = newCapacity;
}

static inline void reserve(ByteCodeArray *b, size_t size) {
static void reserve(ByteCodeArray *b, size_t size) {
while ((b->count + size) >= b->capacity) {
growCapacity(b, b->capacity < 8 ? 8 : b->capacity * 2);
}
Expand Down Expand Up @@ -132,61 +133,6 @@ static void addBig(ByteCodeArray *b, bigint bi) {
b->count += nBytes;
}

byte readByte(ByteCodeArray *b, int *i) {
return b->entries[(*i)++];
}

static inline void _readWord(ByteCodeArray *b, int *i, word *a) {
memcpy(a, &b->entries[*i], sizeof(word));
(*i) += sizeof(word);
}

word readWord(ByteCodeArray *b, int *i) {
word a;
_readWord(b, i, &a);
return a;
}

static inline void _readInt(ByteCodeArray *b, int *i, int *a) {
memcpy(a, &b->entries[*i], sizeof(int));
(*i) += sizeof(int);
}

int readInt(ByteCodeArray *b, int *i) {
int a;
_readInt(b, i, &a);
return a;
}

int readOffset(ByteCodeArray *b, int *i) {
int ii = *i;
int offset = readWord(b, i);
return ii + offset;
}

int readOffsetAt(ByteCodeArray *b, int i, int step) {
int ii = i + step * sizeof(word);
int offset = readWord(b, &ii);
return i + offset + step * sizeof(word);
}

bigint readBigint(ByteCodeArray *b, int *i) {
bigint a;
bigint_init(&a);
int size;
int capacity;
_readInt(b, i, &size);
_readInt(b, i, &capacity);
int neg = readByte(b, i);
bigint_reserve(&a, capacity);
int nbytes = capacity * sizeof(bigint_word);
memcpy(a.words, &b->entries[*i], nbytes);
(*i) += nbytes;
a.size = size;
a.neg = neg;
return a;
}

////////////////////////////////////////////////////////////////////////////

void writeAexpLam(AexpLam *x, ByteCodeArray *b) {
Expand Down Expand Up @@ -447,6 +393,8 @@ void writeCexpIntCond(CexpIntCondCases *x, ByteCodeArray *b) {
ENTER(writeCexpIntCond);
addByte(b, BYTECODE_INTCOND);
int numCases = countCexpIntCondCases(x);
// fprintf(stderr, "writeCexpIntCond size %d\n", numCases);
// printCexpIntCondCases(x);
numCases--; // don't count the default case
if (numCases <= 0) {
cant_happen("zero cases in writeCexpIntCond");
Expand Down
61 changes: 55 additions & 6 deletions src/bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,60 @@ void writeExp(Exp *x, ByteCodeArray *b);

void writeEnd(ByteCodeArray *b);

byte readByte(ByteCodeArray *b, int *i);
word readWord(ByteCodeArray *b, int *i);
int readInt(ByteCodeArray *b, int *i);
bigint readBigint(ByteCodeArray *b, int *i);
int readOffset(ByteCodeArray *b, int *i);
int readOffsetAt(ByteCodeArray *b, int i, int step);
static inline byte readByte(ByteCodeArray *b, int *i) {
return b->entries[(*i)++];
}

static inline void _readWord(ByteCodeArray *b, int *i, word *a) {
memcpy(a, &b->entries[*i], sizeof(word));
(*i) += sizeof(word);
}

static inline word readWord(ByteCodeArray *b, int *i) {
word a;
_readWord(b, i, &a);
return a;
}

static inline void _readInt(ByteCodeArray *b, int *i, int *a) {
memcpy(a, &b->entries[*i], sizeof(int));
(*i) += sizeof(int);
}

static inline int readInt(ByteCodeArray *b, int *i) {
int a;
_readInt(b, i, &a);
return a;
}

static inline int readOffset(ByteCodeArray *b, int *i) {
int ii = *i;
int offset = readWord(b, i);
return ii + offset;
}

static inline int readOffsetAt(ByteCodeArray *b, int i, int step) {
int ii = i + step * sizeof(word);
int offset = readWord(b, &ii);
return i + offset + step * sizeof(word);
}

static inline bigint readBigint(ByteCodeArray *b, int *i) {
bigint a;
bigint_init(&a);
int size;
int capacity;
_readInt(b, i, &size);
_readInt(b, i, &capacity);
int neg = readByte(b, i);
bigint_reserve(&a, capacity);
int nbytes = capacity * sizeof(bigint_word);
memcpy(a.words, &b->entries[*i], nbytes);
(*i) += nbytes;
a.size = size;
a.neg = neg;
return a;
}


#endif
13 changes: 9 additions & 4 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,15 @@ void dumpByteCode(ByteCodeArray *b) {
int count = readWord(b, &i);
fprintf(stderr, "INTCOND [%d]", count);
while (count > 0) {
bigint bi = readBigint(b, &i);
fprintf(stderr, " ");
bigint_fprint(stderr, &bi);
bigint_free(&bi);
if (bigint_flag) {
bigint bi = readBigint(b, &i);
fprintf(stderr, " ");
bigint_fprint(stderr, &bi);
bigint_free(&bi);
} else {
int li = readInt(b, &i);
fprintf(stderr, " %d", li);
}
int offset = readOffset(b, &i);
fprintf(stderr, ":[%04x]", offset);
count--;
Expand Down
2 changes: 2 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void printMatchList(MatchList *x);
void printCexpIf(CexpIf *x);
void printCexpCond(CexpCond *x);
void printCexpCondCases(CexpCondCases *x);
void printCexpIntCondCases(CexpIntCondCases *x);
void printCexpCharCondCases(CexpCharCondCases *x);
void printCexpLetRec(CexpLetRec *x);
void printCTEnv(CTEnv *x);
void printExp(Exp *x);
Expand Down
9 changes: 7 additions & 2 deletions src/step.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,9 +836,14 @@ static void step() {
int here = state.C;
for (int c = 0; c < size; c++) {
printf(" ");
BigInt *bigInt = readCurrentBigInt();
if (bigint_flag) {
BigInt *bigInt = readCurrentBigInt();
fprintBigInt(stdout, bigInt);
} else {
int Int = readCurrentInt();
printf("%d", Int);
}
int offset = readCurrentOffset();
fprintBigInt(stdout, bigInt);
printf(":[%04x]", offset);
}
printf("\n");
Expand Down