Skip to content

Commit

Permalink
* core: working to consolidate PNAsm and PNBytes into a single struc…
Browse files Browse the repository at this point in the history
…ture.
  • Loading branch information
_why committed Aug 10, 2009
1 parent 9891e2f commit b79ddf8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
6 changes: 2 additions & 4 deletions Makefile
Expand Up @@ -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}

Expand All @@ -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`
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions core/asm.c
Expand Up @@ -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))
Expand All @@ -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;
Expand All @@ -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;
}
1 change: 1 addition & 0 deletions core/asm.h
Expand Up @@ -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
2 changes: 1 addition & 1 deletion core/compile.c
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions core/gc.c
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
16 changes: 6 additions & 10 deletions core/potion.h
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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];
};

Expand Down
17 changes: 13 additions & 4 deletions core/string.c
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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';
Expand Down

0 comments on commit b79ddf8

Please sign in to comment.