From b79ddf8c0647546f05efb7c75408b69db142807b Mon Sep 17 00:00:00 2001 From: _why Date: Sun, 9 Aug 2009 21:48:47 -0700 Subject: [PATCH] * core: working to consolidate PNAsm and PNBytes into a single structure. --- Makefile | 6 ++---- core/asm.c | 15 ++++++++++++--- core/asm.h | 1 + core/compile.c | 2 +- core/gc.c | 3 +-- core/potion.h | 16 ++++++---------- core/string.c | 17 +++++++++++++---- 7 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 4cdb60cd..186444d2 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,6 @@ OBJ_POTION = core/potion.o OBJ_TEST = test/api/potion-test.o test/api/CuTest.o OBJ_GC_TEST = test/api/gc-test.o test/api/CuTest.o OBJ_GC_BENCH = test/api/gc-bench.o -OBJ_GREG = tools/greg.o tools/compile.o tools/tree.o DOC = doc/start.textile DOCHTML = ${DOC:.textile=.html} @@ -23,7 +22,6 @@ STRIP ?= `./tools/config.sh ${CC} strip` # TODO: -O2 doesn't include -fno-stack-protector DEBUGFLAGS = `${ECHO} "${DEBUG}" | sed "s/0/-O2/; s/1/-g -DDEBUG/"` -GREGFLAGS = `${ECHO} "${DEBUG}" | sed "s/0/-O3 -DNDEBUG/; s/1/-g -Wall -DNDEBUG/"` CFLAGS += ${DEBUGFLAGS} VERSION = `./tools/config.sh ${CC} version` @@ -122,9 +120,9 @@ core/syntax.c: tools/greg core/syntax.g @${ECHO} GREG core/syntax.g @${GREG} core/syntax.g > $@ -tools/greg: ${OBJ_GREG} +tools/greg: tools/greg.c tools/compile.c tools/tree.c @${ECHO} CC $@ - @${CC} ${GREGFLAGS} -o $@ ${OBJ_GREG} -Itools + @${CC} -O3 -DNDEBUG -o $@ tools/greg.c tools/compile.c tools/tree.c -Itools core/pn-gram.c: tools/lemon core/pn-gram.y @${ECHO} LEMON core/pn-gram.y diff --git a/core/asm.c b/core/asm.c index 01cc1c0c..a1eb5780 100644 --- a/core/asm.c +++ b/core/asm.c @@ -14,13 +14,13 @@ PNAsm *potion_asm_new(Potion *P) { int siz = ASM_UNIT - sizeof(PNAsm); - PNAsm * volatile asmb = PN_FLEX_NEW(asmb, PN_TFLEXB, PNAsm, siz); + PNAsm * volatile asmb = PN_FLEX_NEW(asmb, PN_TBYTES, PNAsm, siz); return asmb; } PNAsm *potion_asm_put(Potion *P, PNAsm * volatile asmb, PN val, size_t len) { u8 *ptr; - PN_FLEX_NEEDS(len, asmb, PN_TFLEXB, PNAsm, ASM_UNIT); + PN_FLEX_NEEDS(len, asmb, PN_TBYTES, PNAsm, ASM_UNIT); ptr = asmb->ptr + asmb->len; if (len == sizeof(u8)) @@ -36,7 +36,7 @@ PNAsm *potion_asm_put(Potion *P, PNAsm * volatile asmb, PN val, size_t len) { PNAsm *potion_asm_op(Potion *P, PNAsm * volatile asmb, u8 ins, int _a, int _b) { PN_OP *pos; - PN_FLEX_NEEDS(sizeof(PN_OP), asmb, PN_TFLEXB, PNAsm, ASM_UNIT); + PN_FLEX_NEEDS(sizeof(PN_OP), asmb, PN_TBYTES, PNAsm, ASM_UNIT); pos = (PN_OP *)(asmb->ptr + asmb->len); pos->code = ins; @@ -46,3 +46,12 @@ PNAsm *potion_asm_op(Potion *P, PNAsm * volatile asmb, u8 ins, int _a, int _b) { asmb->len += sizeof(PN_OP); return asmb; } + +PNAsm *potion_asm_write(Potion *P, PNAsm * volatile asmb, char *str, size_t len) { + char *ptr; + PN_FLEX_NEEDS(len, asmb, PN_TBYTES, PNAsm, ASM_UNIT); + ptr = (char *)asmb->ptr + asmb->len; + PN_MEMCPY_N(ptr, str, char, len); + asmb->len += len; + return asmb; +} diff --git a/core/asm.h b/core/asm.h index 17a2bd9c..fc5cc905 100644 --- a/core/asm.h +++ b/core/asm.h @@ -100,5 +100,6 @@ typedef struct { PNAsm *potion_asm_new(Potion *); PNAsm *potion_asm_put(Potion *, PNAsm *, PN, size_t); PNAsm *potion_asm_op(Potion *, PNAsm *, u8, int, int); +PNAsm *potion_asm_write(Potion *, PNAsm *, char *, size_t); #endif diff --git a/core/compile.c b/core/compile.c index cb3cc836..75601805 100644 --- a/core/compile.c +++ b/core/compile.c @@ -732,7 +732,7 @@ PN potion_proto_load(Potion *P, PN up, u8 pn, u8 **ptr) { f->protos = READ_PROTOS(pn, *ptr); len = READ_PN(pn, *ptr); - PN_FLEX_NEW(asmb, PN_TFLEXB, PNAsm, len); + PN_FLEX_NEW(asmb, PN_TBYTES, PNAsm, len); PN_MEMCPY_N(asmb->ptr, *ptr, u8, len); asmb->len = len; diff --git a/core/gc.c b/core/gc.c index e64f2d6c..02cbe29b 100644 --- a/core/gc.c +++ b/core/gc.c @@ -291,7 +291,7 @@ PN_SIZE potion_type_size(Potion *P, const struct PNObject *ptr) { sz = sizeof(struct PNSource) + (3 * sizeof(PN)); break; case PN_TBYTES: - sz = sizeof(struct PNBytes) + ((struct PNBytes *)ptr)->len + 1; + sz = sizeof(struct PNBytes) + ((struct PNBytes *)ptr)->siz; break; case PN_TPROTO: sz = sizeof(struct PNProto); @@ -303,7 +303,6 @@ PN_SIZE potion_type_size(Potion *P, const struct PNObject *ptr) { sz = sizeof(struct PNTable) + kh_mem(str, ptr); break; case PN_TFLEX: - case PN_TFLEXB: sz = sizeof(PNFlex) + ((PNFlex *)ptr)->siz; break; case PN_TCONT: diff --git a/core/potion.h b/core/potion.h index 24833f27..4b6099f6 100644 --- a/core/potion.h +++ b/core/potion.h @@ -61,11 +61,10 @@ struct PNVtable; #define PN_TTABLE (15+PN_TNIL) #define PN_TLICK (16+PN_TNIL) #define PN_TFLEX (17+PN_TNIL) -#define PN_TFLEXB (18+PN_TNIL) -#define PN_TSTRINGS (19+PN_TNIL) -#define PN_TERROR (20+PN_TNIL) -#define PN_TCONT (21+PN_TNIL) -#define PN_TUSER (22+PN_TNIL) +#define PN_TSTRINGS (18+PN_TNIL) +#define PN_TERROR (19+PN_TNIL) +#define PN_TCONT (20+PN_TNIL) +#define PN_TUSER (21+PN_TNIL) #define vPN(t) struct PN##t * volatile #define PN_TYPE(x) potion_type((PN)(x)) @@ -115,7 +114,7 @@ struct PNVtable; #define PN_TOUCH(x) potion_gc_update(P, (PN)(x)) #define PN_ALIGN(o, x) (((((o) - 1) / (x)) + 1) * (x)) -#define PN_FLEX(N, T) typedef struct { PN_OBJECT_HEADER PN_SIZE siz; PN_SIZE len; T ptr[0]; } N; +#define PN_FLEX(N, T) typedef struct { PN_OBJECT_HEADER PN_SIZE len; PN_SIZE siz; T ptr[0]; } N; #define PN_FLEX_AT(N, I) ((PNFlex *)(N))->ptr[I] #define PN_FLEX_SIZE(N) ((PNFlex *)(N))->len @@ -201,14 +200,11 @@ struct PNString { // // byte strings are raw character data, // volatile, may be appended/changed. -// (although this struct is identical -// to PNString, they have deviated -// periodically, so it's handy to have -// them separate.) // struct PNBytes { PN_OBJECT_HEADER PN_SIZE len; + PN_SIZE siz; char chars[0]; }; diff --git a/core/string.c b/core/string.c index b5324934..f59abaec 100644 --- a/core/string.c +++ b/core/string.c @@ -149,15 +149,15 @@ static PN potion_str_at(Potion *P, PN closure, PN self, PN index) { PN potion_byte_str(Potion *P, const char *str) { size_t len = strlen(str); - vPN(Bytes) s = (struct PNBytes *)potion_bytes(P, len + 1); + vPN(Bytes) s = (struct PNBytes *)potion_bytes(P, len); PN_MEMCPY_N(s->chars, str, char, len); - s->len = len; s->chars[len] = '\0'; return (PN)s; } PN potion_bytes(Potion *P, size_t len) { vPN(Bytes) s = PN_ALLOC_N(PN_TBYTES, struct PNBytes, len + 1); + s->siz = len + 1; s->len = (PN_SIZE)len; return (PN)s; } @@ -170,7 +170,11 @@ PN_SIZE pn_printf(Potion *P, PN bytes, const char *format, ...) { va_start(args, format); len = (PN_SIZE)vsnprintf(NULL, 0, format, args); va_end(args); - PN_REALLOC(s, PN_TBYTES, struct PNBytes, s->len + len + 1); + + if (s->len + len + 1 > s->siz) { + PN_REALLOC(s, PN_TBYTES, struct PNBytes, s->len + len + 1); + s->siz = s->len + len + 1; + } va_start(args, format); vsnprintf(s->chars + s->len, len + 1, format, args); @@ -188,7 +192,12 @@ PN potion_bytes_append(Potion *P, PN closure, PN self, PN str) { vPN(Bytes) s = (struct PNBytes *)potion_fwd(self); PN fstr = potion_fwd(str); PN_SIZE len = PN_STR_LEN(fstr); - PN_REALLOC(s, PN_TBYTES, struct PNBytes, s->len + len + 1); + + if (s->len + len + 1 > s->siz) { + PN_REALLOC(s, PN_TBYTES, struct PNBytes, s->len + len + 1); + s->siz = s->len + len + 1; + } + PN_MEMCPY_N(s->chars + s->len, PN_STR_PTR(fstr), char, len); s->len += len; s->chars[s->len] = '\0';