summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2016-11-06 10:53:46 -0800
committerKeith Packard <keithp@keithp.com>2016-11-17 22:18:39 -0800
commitb97b9f8199d1f65d2304b035c4f99b5a1c76f61b (patch)
treedf65524df6271dde7107a32c6a3db93dd603f879
parent954bc72d5822e0f5666e80764e798c10b1eb541d (diff)
altos/lisp: convert GC to non-recursive
Use a boolean array to note cons cells which would otherwise recurse, then loop until that array is empty. Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--src/lisp/ao_lisp.h8
-rw-r--r--src/lisp/ao_lisp_atom.c2
-rw-r--r--src/lisp/ao_lisp_cons.c15
-rw-r--r--src/lisp/ao_lisp_eval.c14
-rw-r--r--src/lisp/ao_lisp_frame.c19
-rw-r--r--src/lisp/ao_lisp_mem.c140
-rw-r--r--src/lisp/ao_lisp_prim.c51
7 files changed, 170 insertions, 79 deletions
diff --git a/src/lisp/ao_lisp.h b/src/lisp/ao_lisp.h
index 0d179942..17f1e0f5 100644
--- a/src/lisp/ao_lisp.h
+++ b/src/lisp/ao_lisp.h
@@ -221,6 +221,10 @@ ao_lisp_mem_round(int size)
#define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
+static inline int ao_lisp_poly_base_type(ao_poly poly) {
+ return poly & AO_LISP_TYPE_MASK;
+}
+
static inline int ao_lisp_poly_type(ao_poly poly) {
int type = poly & AO_LISP_TYPE_MASK;
if (type == AO_LISP_OTHER)
@@ -384,11 +388,11 @@ void
ao_lisp_poly_patom(ao_poly p);
int
-ao_lisp_poly_mark(ao_poly p);
+ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
/* returns 1 if the object has already been moved */
int
-ao_lisp_poly_move(ao_poly *p);
+ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
/* eval */
diff --git a/src/lisp/ao_lisp_atom.c b/src/lisp/ao_lisp_atom.c
index 5f1bcda0..41ba97f5 100644
--- a/src/lisp/ao_lisp_atom.c
+++ b/src/lisp/ao_lisp_atom.c
@@ -48,7 +48,7 @@ static void atom_move(void *addr)
struct ao_lisp_atom *atom = addr;
for (;;) {
- if (ao_lisp_poly_move(&atom->next))
+ if (ao_lisp_poly_move(&atom->next, 0))
break;
atom = ao_lisp_poly_atom(atom->next);
}
diff --git a/src/lisp/ao_lisp_cons.c b/src/lisp/ao_lisp_cons.c
index 7d3ca68d..855079b8 100644
--- a/src/lisp/ao_lisp_cons.c
+++ b/src/lisp/ao_lisp_cons.c
@@ -21,7 +21,7 @@ static void cons_mark(void *addr)
struct ao_lisp_cons *cons = addr;
for (;;) {
- ao_lisp_poly_mark(cons->car);
+ ao_lisp_poly_mark(cons->car, 1);
cons = ao_lisp_poly_cons(cons->cdr);
if (!cons)
break;
@@ -44,10 +44,17 @@ static void cons_move(void *addr)
return;
for (;;) {
- (void) ao_lisp_poly_move(&cons->car);
- if (ao_lisp_poly_move(&cons->cdr))
+ struct ao_lisp_cons *cdr;
+ int ret;
+
+ (void) ao_lisp_poly_move(&cons->car, 1);
+ cdr = ao_lisp_poly_cons(cons->cdr);
+ ret = ao_lisp_move_memory((void **) &cdr, sizeof (struct ao_lisp_cons));
+ if (cdr != ao_lisp_poly_cons(cons->cdr))
+ cons->cdr = ao_lisp_cons_poly(cdr);
+ if (ret)
break;
- cons = ao_lisp_poly_cons(cons->cdr);
+ cons = cdr;
}
}
diff --git a/src/lisp/ao_lisp_eval.c b/src/lisp/ao_lisp_eval.c
index e3d653b9..a5c74250 100644
--- a/src/lisp/ao_lisp_eval.c
+++ b/src/lisp/ao_lisp_eval.c
@@ -79,10 +79,10 @@ stack_mark(void *addr)
{
struct ao_lisp_stack *stack = addr;
for (;;) {
- ao_lisp_poly_mark(stack->actuals);
- ao_lisp_poly_mark(stack->formals);
+ ao_lisp_poly_mark(stack->actuals, 0);
+ ao_lisp_poly_mark(stack->formals, 0);
/* no need to mark formals_tail */
- ao_lisp_poly_mark(stack->frame);
+ ao_lisp_poly_mark(stack->frame, 0);
stack = ao_lisp_poly_stack(stack->prev);
if (ao_lisp_mark_memory(stack, sizeof (struct ao_lisp_stack)))
break;
@@ -122,10 +122,10 @@ stack_move(void *addr)
while (stack) {
void *prev;
int ret;
- (void) ao_lisp_poly_move(&stack->actuals);
- (void) ao_lisp_poly_move(&stack->formals);
- (void) ao_lisp_poly_move(&stack->formals_tail);
- (void) ao_lisp_poly_move(&stack->frame);
+ (void) ao_lisp_poly_move(&stack->actuals, 0);
+ (void) ao_lisp_poly_move(&stack->formals, 0);
+ (void) ao_lisp_poly_move(&stack->formals_tail, 0);
+ (void) ao_lisp_poly_move(&stack->frame, 0);
prev = ao_lisp_poly_stack(stack->prev);
ret = ao_lisp_move(&ao_lisp_stack_type, &prev);
if (prev != ao_lisp_poly_stack(stack->prev))
diff --git a/src/lisp/ao_lisp_frame.c b/src/lisp/ao_lisp_frame.c
index 8bf98571..8791c4de 100644
--- a/src/lisp/ao_lisp_frame.c
+++ b/src/lisp/ao_lisp_frame.c
@@ -48,7 +48,7 @@ frame_mark(void *addr)
for (f = 0; f < frame->num; f++) {
struct ao_lisp_val *v = &frame->vals[f];
- ao_lisp_poly_mark(v->val);
+ ao_lisp_poly_mark(v->val, 0);
DBG ("\tframe mark atom %s %d val %d at %d\n",
ao_lisp_poly_atom(v->atom)->name,
OFFSET(v->atom), OFFSET(v->val), f);
@@ -69,19 +69,28 @@ frame_move(void *addr)
int f;
for (;;) {
+ struct ao_lisp_frame *next;
+ int ret;
+
DBG("frame move %p\n", frame);
if (!AO_LISP_IS_POOL(frame))
break;
for (f = 0; f < frame->num; f++) {
struct ao_lisp_val *v = &frame->vals[f];
- ao_lisp_poly_move(&v->atom);
+ ao_lisp_poly_move(&v->atom, 0);
DBG("moved atom %s\n", ao_lisp_poly_atom(v->atom)->name);
- ao_lisp_poly_move(&v->val);
+ ao_lisp_poly_move(&v->val, 0);
}
- if (ao_lisp_poly_move(&frame->next))
+ next = ao_lisp_poly_frame(frame->next);
+ ret = 1;
+ if (next)
+ ret = ao_lisp_move_memory((void **) &next, frame_size(next));
+ if (next != ao_lisp_poly_frame(frame->next))
+ frame->next = ao_lisp_frame_poly(next);
+ if (ret)
break;
- frame = ao_lisp_poly_frame(frame->next);
+ frame = next;
}
}
diff --git a/src/lisp/ao_lisp_mem.c b/src/lisp/ao_lisp_mem.c
index 6e656454..c11ec25d 100644
--- a/src/lisp/ao_lisp_mem.c
+++ b/src/lisp/ao_lisp_mem.c
@@ -41,7 +41,7 @@ uint8_t ao_lisp_pool[AO_LISP_POOL] __attribute__((aligned(4)));
#define DBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
#define DBG(...) printf(__VA_ARGS__)
#define DBG_DO(a) a
-static int move_dump;
+static int move_dump = 1;
static int move_depth;
#define DBG_RESET() (move_depth = 0)
#define DBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
@@ -68,8 +68,10 @@ struct ao_lisp_root {
static struct ao_lisp_root ao_lisp_root[AO_LISP_ROOT];
static uint8_t ao_lisp_busy[AO_LISP_POOL / 32];
-
static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
+static uint8_t ao_lisp_cons[AO_LISP_POOL / 32];
+static uint8_t ao_lisp_cons_last[AO_LISP_POOL / 32];
+static uint8_t ao_lisp_cons_noted;
uint16_t ao_lisp_top;
@@ -161,6 +163,17 @@ busy_object(uint8_t *tag, void *addr) {
return 0;
}
+static void
+note_cons(void *addr)
+{
+ DBG_MOVE("note cons %d\n", DBG_OFFSET(addr));
+ if (AO_LISP_IS_POOL(addr)) {
+ ao_lisp_cons_noted = 1;
+ mark(ao_lisp_cons, (uint8_t *) addr - ao_lisp_pool);
+ }
+}
+
+
static void *move_old, *move_new;
static int move_size;
@@ -173,11 +186,15 @@ move_object(void)
DBG_MOVE("move %d -> %d\n", DBG_OFFSET(move_old), DBG_OFFSET(move_new));
DBG_MOVE_IN();
memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
+ memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
+ ao_lisp_cons_noted = 0;
for (i = 0; i < AO_LISP_ROOT; i++) {
if (!ao_lisp_root[i].addr)
continue;
if (ao_lisp_root[i].type) {
- DBG_DO(void *addr = *ao_lisp_root[i].addr);
+ void *addr = *ao_lisp_root[i].addr;
+ if (!addr)
+ continue;
DBG_MOVE("root %d\n", DBG_OFFSET(addr));
if (!ao_lisp_move(ao_lisp_root[i].type,
ao_lisp_root[i].addr)) {
@@ -186,13 +203,30 @@ move_object(void)
*ao_lisp_root[i].addr);
}
} else {
- DBG_DO(ao_poly p = *(ao_poly *) ao_lisp_root[i].addr);
- if (!ao_lisp_poly_move((ao_poly *) ao_lisp_root[i].addr)) {
+ ao_poly p = *(ao_poly *) ao_lisp_root[i].addr;
+ if (!p)
+ continue;
+ if (!ao_lisp_poly_move((ao_poly *) ao_lisp_root[i].addr, 0)) {
DBG_MOVE("root poly move from %04x to %04x\n",
p, *(ao_poly *) ao_lisp_root[i].addr);
}
}
}
+ while (ao_lisp_cons_noted) {
+ memcpy(ao_lisp_cons_last, ao_lisp_cons, sizeof (ao_lisp_cons));
+ memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
+ ao_lisp_cons_noted = 0;
+ for (i = 0; i < AO_LISP_POOL; i += 4) {
+ if (busy(ao_lisp_cons_last, i)) {
+ void *addr = ao_lisp_pool + i;
+ DBG_MOVE("cons %d\n", DBG_OFFSET(addr));
+ if (!ao_lisp_move(&ao_lisp_cons_type, &addr)) {
+ DBG_MOVE("cons moves from %p to %p\n",
+ ao_lisp_pool + i, addr);
+ }
+ }
+ }
+ }
DBG_MOVE_OUT();
DBG_MOVE("move done\n");
}
@@ -220,25 +254,50 @@ dump_busy(void)
#define DUMP_BUSY()
#endif
+static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
+ [AO_LISP_CONS] = &ao_lisp_cons_type,
+ [AO_LISP_INT] = NULL,
+ [AO_LISP_STRING] = &ao_lisp_string_type,
+ [AO_LISP_OTHER] = (void *) 0x1,
+ [AO_LISP_ATOM] = &ao_lisp_atom_type,
+ [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
+ [AO_LISP_FRAME] = &ao_lisp_frame_type,
+};
+
+
static void
ao_lisp_mark_busy(void)
{
int i;
memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
+ memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
+ ao_lisp_cons_noted = 0;
DBG("mark\n");
for (i = 0; i < AO_LISP_ROOT; i++) {
if (ao_lisp_root[i].type) {
void **a = ao_lisp_root[i].addr, *v;
if (a && (v = *a)) {
- DBG("root %p\n", v);
+ DBG("root %d\n", DBG_OFFSET(v));
ao_lisp_mark(ao_lisp_root[i].type, v);
}
} else {
ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
if (a && (p = *a)) {
- DBG("root %04x\n", p);
- ao_lisp_poly_mark(p);
+ DBG("root 0x%04x\n", p);
+ ao_lisp_poly_mark(p, 0);
+ }
+ }
+ }
+ while (ao_lisp_cons_noted) {
+ memcpy(ao_lisp_cons_last, ao_lisp_cons, sizeof (ao_lisp_cons));
+ memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
+ ao_lisp_cons_noted = 0;
+ for (i = 0; i < AO_LISP_POOL; i += 4) {
+ if (busy(ao_lisp_cons_last, i)) {
+ void *v = ao_lisp_pool + i;
+ DBG("cons %d\n", DBG_OFFSET(v));
+ ao_lisp_mark(&ao_lisp_cons_type, v);
}
}
}
@@ -274,6 +333,10 @@ ao_lisp_collect(void)
abort();
clear_object(ao_lisp_busy, move_old, move_size);
mark_object(ao_lisp_busy, move_new, move_size);
+ if (busy_object(ao_lisp_cons, move_old)) {
+ clear_object(ao_lisp_cons, move_old, move_size);
+ mark_object(ao_lisp_cons, move_new, move_size);
+ }
i += move_size;
top += move_size;
DUMP_BUSY();
@@ -297,6 +360,24 @@ ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
}
int
+ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
+{
+ uint8_t type = ao_lisp_poly_type(p);
+
+ if (!p)
+ return 1;
+ if (type == AO_LISP_CONS && do_note_cons) {
+ note_cons(ao_lisp_ref(p));
+ return 0;
+ } else {
+ const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
+ if (lisp_type)
+ return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
+ return 1;
+ }
+}
+
+int
ao_lisp_mark_memory(void *addr, int size)
{
return mark_object(ao_lisp_busy, addr, size);
@@ -348,7 +429,7 @@ ao_lisp_move(const struct ao_lisp_type *type, void **ref)
return 1;
#endif
DBG_MOVE("object %d\n", DBG_OFFSET(addr));
- if (a < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= a)
+ if (!AO_LISP_IS_POOL(a))
abort();
DBG_MOVE_IN();
addr = check_move(addr, size);
@@ -389,6 +470,47 @@ ao_lisp_move_memory(void **ref, int size)
return 0;
}
+int
+ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
+{
+ uint8_t type;
+ ao_poly p = *ref;
+ const struct ao_lisp_type *lisp_type;
+ int ret;
+ void *addr;
+
+ if (!p)
+ return 1;
+
+ type = ao_lisp_poly_base_type(p);
+ addr = ao_lisp_ref(p);
+ if (type == AO_LISP_CONS && do_note_cons) {
+ note_cons(addr);
+ addr = check_move(addr, sizeof (struct ao_lisp_cons));
+ ret = 1;
+ } else {
+
+ if (type == AO_LISP_OTHER)
+ type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
+
+ if (type >= AO_LISP_NUM_TYPE)
+ abort();
+
+ lisp_type = ao_lisp_types[type];
+ if (!lisp_type)
+ return 1;
+ ret = ao_lisp_move(lisp_type, &addr);
+ }
+
+ if (addr != ao_lisp_ref(p)) {
+ ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
+ DBG("poly %d moved %04x -> %04x\n",
+ type, p, np);
+ *ref = np;
+ }
+ return ret;
+}
+
#ifdef DBG_POOL
static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
diff --git a/src/lisp/ao_lisp_prim.c b/src/lisp/ao_lisp_prim.c
index 82386a83..3c081ee8 100644
--- a/src/lisp/ao_lisp_prim.c
+++ b/src/lisp/ao_lisp_prim.c
@@ -76,54 +76,3 @@ ao_lisp_poly_patom(ao_poly p)
f->patom(p);
}
-static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
- [AO_LISP_CONS] = &ao_lisp_cons_type,
- [AO_LISP_INT] = NULL,
- [AO_LISP_STRING] = &ao_lisp_string_type,
- [AO_LISP_OTHER] = (void *) 0x1,
- [AO_LISP_ATOM] = &ao_lisp_atom_type,
- [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
- [AO_LISP_FRAME] = &ao_lisp_frame_type,
-};
-
-int
-ao_lisp_poly_mark(ao_poly p)
-{
- const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
- if (lisp_type)
- return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
- return 1;
-}
-
-int
-ao_lisp_poly_move(ao_poly *ref)
-{
- uint8_t type;
- ao_poly p = *ref;
- const struct ao_lisp_type *lisp_type;
- int ret;
- void *addr;
-
- if (!p)
- return 1;
-
- type = p & AO_LISP_TYPE_MASK;
- if (type == AO_LISP_OTHER)
- type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
-
- if (type >= AO_LISP_NUM_TYPE)
- abort();
-
- lisp_type = ao_lisp_types[type];
- if (!lisp_type)
- return 1;
- addr = ao_lisp_ref(p);
- ret = ao_lisp_move(lisp_type, &addr);
- if (addr != ao_lisp_ref(p)) {
- ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
- DBG("poly %d moved %04x -> %04x\n",
- type, p, np);
- *ref = np;
- }
- return ret;
-}