From acc806d444a8db098e39dbff6fb720fb7fd6def3 Mon Sep 17 00:00:00 2001 From: Bill Hails Date: Wed, 13 Dec 2023 18:17:19 +0000 Subject: [PATCH] bugfix and perf improvement --- fn/{fib.fn => fib40.fn} | 0 src/bigint.c | 18 +++++++++--- src/bigint.h | 1 + src/bytecode.c | 60 +++------------------------------------- src/bytecode.h | 61 +++++++++++++++++++++++++++++++++++++---- src/debug.c | 13 ++++++--- src/debug.h | 2 ++ src/step.c | 9 ++++-- 8 files changed, 92 insertions(+), 72 deletions(-) rename fn/{fib.fn => fib40.fn} (100%) diff --git a/fn/fib.fn b/fn/fib40.fn similarity index 100% rename from fn/fib.fn rename to fn/fib40.fn diff --git a/src/bigint.c b/src/bigint.c index 09fe646..4b26563 100644 --- a/src/bigint.c +++ b/src/bigint.c @@ -1270,7 +1270,11 @@ void fprintBigInt(FILE *f, BigInt *x) { fprintf(f, ""); 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) { @@ -1278,12 +1282,18 @@ void sprintBigInt(char *s, BigInt *x) { sprintf(s, ""); 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) { diff --git a/src/bigint.h b/src/bigint.h index d6605bc..3948faf 100644 --- a/src/bigint.h +++ b/src/bigint.h @@ -8,6 +8,7 @@ extern "C" { #include #include #include +#include #include "memory.h" /* any unsigned integer type */ diff --git a/src/bytecode.c b/src/bytecode.c index 0a8fbc7..c78b054 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -24,6 +24,7 @@ #include #include "bytecode.h" +#include "debug.h" #include "common.h" #ifdef DEBUG_BYTECODE @@ -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); } @@ -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) { @@ -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"); diff --git a/src/bytecode.h b/src/bytecode.h index 616032b..fc2766a 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -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 diff --git a/src/debug.c b/src/debug.c index ad58b6b..4fa8263 100644 --- a/src/debug.c +++ b/src/debug.c @@ -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--; diff --git a/src/debug.h b/src/debug.h index 4ebaa30..0fc6486 100644 --- a/src/debug.h +++ b/src/debug.h @@ -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); diff --git a/src/step.c b/src/step.c index f440501..b9c855a 100644 --- a/src/step.c +++ b/src/step.c @@ -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");