Replace cons_t with struct

This commit is contained in:
Lassi Kortela 2019-08-09 19:28:14 +03:00
parent d6f1579e17
commit 193ced5e73
6 changed files with 36 additions and 34 deletions

View File

@ -49,7 +49,7 @@ static value_t fl_nconc(value_t *args, u_int32_t nargs)
return FL_NIL; return FL_NIL;
value_t lst, first = FL_NIL; value_t lst, first = FL_NIL;
value_t *pcdr = &first; value_t *pcdr = &first;
cons_t *c; struct cons *c;
uint32_t i = 0; uint32_t i = 0;
while (1) { while (1) {
lst = args[i++]; lst = args[i++];
@ -57,9 +57,9 @@ static value_t fl_nconc(value_t *args, u_int32_t nargs)
break; break;
if (iscons(lst)) { if (iscons(lst)) {
*pcdr = lst; *pcdr = lst;
c = (cons_t *)ptr(lst); c = (struct cons *)ptr(lst);
while (iscons(c->cdr)) while (iscons(c->cdr))
c = (cons_t *)ptr(c->cdr); c = (struct cons *)ptr(c->cdr);
pcdr = &c->cdr; pcdr = &c->cdr;
} else if (lst != FL_NIL) { } else if (lst != FL_NIL) {
type_error("nconc", "cons", lst); type_error("nconc", "cons", lst);
@ -89,7 +89,7 @@ static value_t fl_memq(value_t *args, u_int32_t nargs)
{ {
argcount("memq", nargs, 2); argcount("memq", nargs, 2);
while (iscons(args[1])) { while (iscons(args[1])) {
cons_t *c = (cons_t *)ptr(args[1]); struct cons *c = (struct cons *)ptr(args[1]);
if (c->car == args[0]) if (c->car == args[0])
return args[1]; return args[1];
args[1] = c->cdr; args[1] = c->cdr;

View File

@ -249,7 +249,7 @@ void bounds_error(char *fname, value_t arr, value_t ind)
return (ctype)cnvt(v); \ return (ctype)cnvt(v); \
type_error(fname, #type, v); \ type_error(fname, #type, v); \
} }
SAFECAST_OP(cons, cons_t *, ptr) SAFECAST_OP(cons, struct cons *, ptr)
SAFECAST_OP(symbol, symbol_t *, ptr) SAFECAST_OP(symbol, symbol_t *, ptr)
SAFECAST_OP(fixnum, fixnum_t, numval) SAFECAST_OP(fixnum, fixnum_t, numval)
SAFECAST_OP(cvalue, cvalue_t *, ptr) SAFECAST_OP(cvalue, cvalue_t *, ptr)
@ -360,12 +360,12 @@ void gc(int mustgrow);
static value_t mk_cons(void) static value_t mk_cons(void)
{ {
cons_t *c; struct cons *c;
if (__unlikely(curheap > lim)) if (__unlikely(curheap > lim))
gc(0); gc(0);
c = (cons_t *)curheap; c = (struct cons *)curheap;
curheap += sizeof(cons_t); curheap += sizeof(struct cons);
return tagptr(c, TAG_CONS); return tagptr(c, TAG_CONS);
} }
@ -389,7 +389,7 @@ static value_t *alloc_words(int n)
// allocate n consecutive conses // allocate n consecutive conses
#define cons_reserve(n) tagptr(alloc_words((n)*2), TAG_CONS) #define cons_reserve(n) tagptr(alloc_words((n)*2), TAG_CONS)
#define cons_index(c) (((cons_t *)ptr(c)) - ((cons_t *)fromspace)) #define cons_index(c) (((struct cons *)ptr(c)) - ((struct cons *)fromspace))
#define ismarked(c) bitvector_get(consflags, cons_index(c)) #define ismarked(c) bitvector_get(consflags, cons_index(c))
#define mark_cons(c) bitvector_set(consflags, cons_index(c), 1) #define mark_cons(c) bitvector_set(consflags, cons_index(c), 1)
#define unmark_cons(c) bitvector_set(consflags, cons_index(c), 0) #define unmark_cons(c) bitvector_set(consflags, cons_index(c), 0)
@ -459,8 +459,8 @@ static value_t relocate(value_t v)
*pcdr = cdr_(v); *pcdr = cdr_(v);
return first; return first;
} }
*pcdr = nc = tagptr((cons_t *)curheap, TAG_CONS); *pcdr = nc = tagptr((struct cons *)curheap, TAG_CONS);
curheap += sizeof(cons_t); curheap += sizeof(struct cons);
d = cdr_(v); d = cdr_(v);
car_(v) = TAG_FWD; car_(v) = TAG_FWD;
cdr_(v) = nc; cdr_(v) = nc;
@ -561,9 +561,9 @@ void gc(int mustgrow)
curheap = tospace; curheap = tospace;
if (grew) if (grew)
lim = curheap + heapsize * 2 - sizeof(cons_t); lim = curheap + heapsize * 2 - sizeof(struct cons);
else else
lim = curheap + heapsize - sizeof(cons_t); lim = curheap + heapsize - sizeof(struct cons);
if (fl_throwing_frame > curr_frame) { if (fl_throwing_frame > curr_frame) {
top = fl_throwing_frame - 4; top = fl_throwing_frame - 4;
@ -608,7 +608,8 @@ void gc(int mustgrow)
#ifdef VERBOSEGC #ifdef VERBOSEGC
printf("GC: found %d/%d live conses\n", printf("GC: found %d/%d live conses\n",
(curheap - tospace) / sizeof(cons_t), heapsize / sizeof(cons_t)); (curheap - tospace) / sizeof(struct cons),
heapsize / sizeof(struct cons));
#endif #endif
temp = tospace; temp = tospace;
tospace = fromspace; tospace = fromspace;
@ -625,7 +626,7 @@ void gc(int mustgrow)
if (grew) { if (grew) {
heapsize *= 2; heapsize *= 2;
temp = temp =
bitvector_resize(consflags, 0, heapsize / sizeof(cons_t), 1); bitvector_resize(consflags, 0, heapsize / sizeof(struct cons), 1);
if (temp == NULL) if (temp == NULL)
fl_raise(memory_exception_value); fl_raise(memory_exception_value);
consflags = (uint32_t *)temp; consflags = (uint32_t *)temp;
@ -720,8 +721,8 @@ value_t fl_listn(size_t n, ...)
value_t a = va_arg(ap, value_t); value_t a = va_arg(ap, value_t);
PUSH(a); PUSH(a);
} }
cons_t *c = (cons_t *)alloc_words(n * 2); struct cons *c = (struct cons *)alloc_words(n * 2);
cons_t *l = c; struct cons *l = c;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
c->car = Stack[si++]; c->car = Stack[si++];
c->cdr = tagptr(c + 1, TAG_CONS); c->cdr = tagptr(c + 1, TAG_CONS);
@ -738,7 +739,7 @@ value_t fl_list2(value_t a, value_t b)
{ {
PUSH(a); PUSH(a);
PUSH(b); PUSH(b);
cons_t *c = (cons_t *)alloc_words(4); struct cons *c = (struct cons *)alloc_words(4);
b = POP(); b = POP();
a = POP(); a = POP();
c[0].car = a; c[0].car = a;
@ -786,11 +787,11 @@ int fl_isnumber(value_t v)
static value_t _list(value_t *args, uint32_t nargs, int star) static value_t _list(value_t *args, uint32_t nargs, int star)
{ {
cons_t *c; struct cons *c;
uint32_t i; uint32_t i;
value_t v; value_t v;
v = cons_reserve(nargs); v = cons_reserve(nargs);
c = (cons_t *)ptr(v); c = (struct cons *)ptr(v);
for (i = 0; i < nargs; i++) { for (i = 0; i < nargs; i++) {
c->car = args[i]; c->car = args[i];
c->cdr = tagptr(c + 1, TAG_CONS); c->cdr = tagptr(c + 1, TAG_CONS);
@ -979,7 +980,7 @@ static value_t apply_cl(uint32_t nargs)
#endif #endif
uint32_t i; uint32_t i;
symbol_t *sym; symbol_t *sym;
static cons_t *c; static struct cons *c;
static value_t *pv; static value_t *pv;
static int64_t accum; static int64_t accum;
static value_t func, v, e; static value_t func, v, e;
@ -1359,8 +1360,8 @@ apply_cl_top:
OP(OP_CONS) OP(OP_CONS)
if (curheap > lim) if (curheap > lim)
gc(0); gc(0);
c = (cons_t *)curheap; c = (struct cons *)curheap;
curheap += sizeof(cons_t); curheap += sizeof(struct cons);
c->car = Stack[SP - 2]; c->car = Stack[SP - 2];
c->cdr = Stack[SP - 1]; c->cdr = Stack[SP - 1];
Stack[SP - 2] = tagptr(c, TAG_CONS); Stack[SP - 2] = tagptr(c, TAG_CONS);
@ -2333,7 +2334,7 @@ value_t fl_append(value_t *args, u_int32_t nargs)
first = lst; first = lst;
else else
cdr_(lastcons) = lst; cdr_(lastcons) = lst;
lastcons = tagptr((((cons_t *)curheap) - 1), TAG_CONS); lastcons = tagptr((((struct cons *)curheap) - 1), TAG_CONS);
} else if (lst != NIL) { } else if (lst != NIL) {
type_error("append", "cons", lst); type_error("append", "cons", lst);
} }
@ -2472,8 +2473,8 @@ static void lisp_init(size_t initial_heapsize)
fromspace = LLT_ALLOC(heapsize); fromspace = LLT_ALLOC(heapsize);
tospace = LLT_ALLOC(heapsize); tospace = LLT_ALLOC(heapsize);
curheap = fromspace; curheap = fromspace;
lim = curheap + heapsize - sizeof(cons_t); lim = curheap + heapsize - sizeof(struct cons);
consflags = bitvector_new(heapsize / sizeof(cons_t), 1); consflags = bitvector_new(heapsize / sizeof(struct cons), 1);
htable_new(&printconses, 32); htable_new(&printconses, 32);
comparehash_init(); comparehash_init();
N_STACK = 262144; N_STACK = 262144;

View File

@ -10,10 +10,10 @@ typedef uint_t ufixnum_t;
#define T_FIXNUM T_INT32 #define T_FIXNUM T_INT32
#endif #endif
typedef struct { struct cons {
value_t car; value_t car;
value_t cdr; value_t cdr;
} cons_t; };
typedef struct _symbol_t { typedef struct _symbol_t {
uptrint_t flags; uptrint_t flags;
@ -88,8 +88,8 @@ struct gensym {
#define vector_elt(v, i) (((value_t *)ptr(v))[1 + (i)]) #define vector_elt(v, i) (((value_t *)ptr(v))[1 + (i)])
#define vector_grow_amt(x) ((x) < 8 ? 5 : 6 * ((x) >> 3)) #define vector_grow_amt(x) ((x) < 8 ? 5 : 6 * ((x) >> 3))
// functions ending in _ are unsafe, faster versions // functions ending in _ are unsafe, faster versions
#define car_(v) (((cons_t *)ptr(v))->car) #define car_(v) (((struct cons *)ptr(v))->car)
#define cdr_(v) (((cons_t *)ptr(v))->cdr) #define cdr_(v) (((struct cons *)ptr(v))->cdr)
#define car(v) (tocons((v), "car")->car) #define car(v) (tocons((v), "car")->car)
#define cdr(v) (tocons((v), "cdr")->cdr) #define cdr(v) (tocons((v), "cdr")->cdr)
#define fn_bcode(f) (((value_t *)ptr(f))[0]) #define fn_bcode(f) (((value_t *)ptr(f))[0])
@ -156,7 +156,7 @@ uptrint_t hash_lispvalue(value_t a);
int isnumtok_base(char *tok, value_t *pval, int base); int isnumtok_base(char *tok, value_t *pval, int base);
/* safe casts */ /* safe casts */
cons_t *tocons(value_t v, char *fname); struct cons *tocons(value_t v, char *fname);
symbol_t *tosymbol(value_t v, char *fname); symbol_t *tosymbol(value_t v, char *fname);
fixnum_t tofixnum(value_t v, char *fname); fixnum_t tofixnum(value_t v, char *fname);
char *tostring(value_t v, char *fname); char *tostring(value_t v, char *fname);

View File

@ -844,7 +844,8 @@ void fl_print(struct ios *f, value_t v)
fl_print_child(f, v); fl_print_child(f, v);
if (print_level >= 0 || print_length >= 0) { if (print_level >= 0 || print_length >= 0) {
memset(consflags, 0, 4 * bitvector_nwords(heapsize / sizeof(cons_t))); memset(consflags, 0,
4 * bitvector_nwords(heapsize / sizeof(struct cons)));
} }
if ((iscons(v) || isvector(v) || isfunction(v) || iscvalue(v)) && if ((iscons(v) || isvector(v) || isfunction(v) || iscvalue(v)) &&

View File

@ -592,7 +592,7 @@ static value_t do_read_sexpr(value_t label)
listwith: listwith:
v = cons_reserve(2); v = cons_reserve(2);
car_(v) = *head; car_(v) = *head;
cdr_(v) = tagptr(((cons_t *)ptr(v)) + 1, TAG_CONS); cdr_(v) = tagptr(((struct cons *)ptr(v)) + 1, TAG_CONS);
car_(cdr_(v)) = cdr_(cdr_(v)) = NIL; car_(cdr_(v)) = cdr_(cdr_(v)) = NIL;
PUSH(v); PUSH(v);
if (label != UNBOUND) if (label != UNBOUND)

View File

@ -192,7 +192,7 @@ value_t fl_string_split(value_t *args, u_int32_t nargs)
if (last == FL_NIL) if (last == FL_NIL)
first = c; // first time, save first cons first = c; // first time, save first cons
else else
((cons_t *)ptr(last))->cdr = c; ((struct cons *)ptr(last))->cdr = c;
// note this tricky condition: if the string ends with a // note this tricky condition: if the string ends with a
// delimiter, we need to go around one more time to add an // delimiter, we need to go around one more time to add an