altos/lisp: Share mark function for mark and move
authorKeith Packard <keithp@keithp.com>
Fri, 11 Nov 2016 08:28:57 +0000 (00:28 -0800)
committerKeith Packard <keithp@keithp.com>
Mon, 20 Feb 2017 19:16:50 +0000 (11:16 -0800)
These two operations both wanted to walk the referenced objects;
sharing is caring.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/lambdakey-v1.0/Makefile
src/lisp/ao_lisp_mem.c
src/test/hanoi.lisp

index ef03527ee2c2265eb5d71b33a15e92b77edcbc5a..1ac04f24159e7f02a94719967befe3ecc51a49d4 100644 (file)
@@ -56,6 +56,8 @@ IDPRODUCT=0x000a
 
 CFLAGS = $(PRODUCT_DEF) $(STMF0_CFLAGS) -Os -g
 
 
 CFLAGS = $(PRODUCT_DEF) $(STMF0_CFLAGS) -Os -g
 
+LDFLAGS=$(CFLAGS) -L$(TOPDIR)/stmf0 -Wl,-Tlambda.ld
+
 PROGNAME=lambdakey-v1.0
 PROG=$(PROGNAME)-$(VERSION).elf
 HEX=$(PROGNAME)-$(VERSION).ihx
 PROGNAME=lambdakey-v1.0
 PROG=$(PROGNAME)-$(VERSION).elf
 HEX=$(PROGNAME)-$(VERSION).ihx
@@ -65,7 +67,7 @@ OBJ=$(SRC:.c=.o)
 
 all: $(PROG) $(HEX)
 
 
 all: $(PROG) $(HEX)
 
-$(PROG): Makefile $(OBJ) altos.ld
+$(PROG): Makefile $(OBJ) lambda.ld altos.ld
        $(call quiet,CC) $(LDFLAGS) $(CFLAGS) -o $(PROG) $(OBJ) $(LIBS)
 
 $(OBJ): $(INC)
        $(call quiet,CC) $(LDFLAGS) $(CFLAGS) -o $(PROG) $(OBJ) $(LIBS)
 
 $(OBJ): $(INC)
index b763d78b14664fd5a3c0f995d8bb001422ef41e5..1fb1b4594f6d4619e3916a3e1f3f0cb8eee663c9 100644 (file)
@@ -79,7 +79,6 @@ uint16_t      ao_lisp_top;
 static inline void mark(uint8_t *tag, int offset) {
        int     byte = offset >> 5;
        int     bit = (offset >> 2) & 7;
 static inline void mark(uint8_t *tag, int offset) {
        int     byte = offset >> 5;
        int     bit = (offset >> 2) & 7;
-
        tag[byte] |= (1 << bit);
 }
 
        tag[byte] |= (1 << bit);
 }
 
@@ -176,42 +175,32 @@ note_cons(void *addr)
        }
 }
 
        }
 }
 
-
-static void    *move_old, *move_new;
-static int     move_size;
+/*
+ * Walk all referenced objects calling functions on each one
+ */
 
 static void
 
 static void
-move_object(void)
+walk_all(uint8_t *tag,
+        int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
+        int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
 {
 {
-       int     i;
+       int i;
 
 
-       MDBG_RESET();
-       MDBG_MOVE("move %d -> %d\n", MDBG_OFFSET(move_old), MDBG_OFFSET(move_new));
-       MDBG_MOVE_IN();
-       memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
+       memset(tag, '\0', sizeof (ao_lisp_busy));
        memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
        ao_lisp_cons_noted = 0;
        for (i = 0; i < AO_LISP_ROOT; i++) {
        memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
        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) {
                if (ao_lisp_root[i].type) {
-                       void *addr = *ao_lisp_root[i].addr;
-                       if (!addr)
-                               continue;
-                       MDBG_MOVE("root %d\n", MDBG_OFFSET(addr));
-                       if (!ao_lisp_move(ao_lisp_root[i].type,
-                                         ao_lisp_root[i].addr)) {
-                               MDBG_MOVE("root moves from %p to %p\n",
-                                        addr,
-                                        *ao_lisp_root[i].addr);
+                       void **a = ao_lisp_root[i].addr, *v;
+                       if (a && (v = *a)) {
+                               MDBG("root %d\n", MDBG_OFFSET(v));
+                               visit_addr(ao_lisp_root[i].type, a);
                        }
                } else {
                        }
                } else {
-                       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)) {
-                               MDBG_MOVE("root poly move from %04x to %04x\n",
-                                        p, *(ao_poly *) ao_lisp_root[i].addr);
+                       ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
+                       if (a && (p = *a)) {
+                               MDBG("root 0x%04x\n", p);
+                               visit_poly(a, 0);
                        }
                }
        }
                        }
                }
        }
@@ -221,17 +210,21 @@ move_object(void)
                ao_lisp_cons_noted = 0;
                for (i = 0; i < AO_LISP_POOL; i += 4) {
                        if (busy(ao_lisp_cons_last, i)) {
                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;
-                               MDBG_MOVE("cons %d\n", MDBG_OFFSET(addr));
-                               if (!ao_lisp_move(&ao_lisp_cons_type, &addr)) {
-                                       MDBG_MOVE("cons moves from %p to %p\n",
-                                                ao_lisp_pool + i, addr);
-                               }
+                               void *v = ao_lisp_pool + i;
+                               MDBG("cons %d\n", MDBG_OFFSET(v));
+                               visit_addr(&ao_lisp_cons_type, &v);
                        }
                }
        }
                        }
                }
        }
-       MDBG_MOVE_OUT();
-       MDBG_MOVE("move done\n");
+}
+
+static void    *move_old, *move_new;
+static int     move_size;
+
+static void
+move_object(void)
+{
+       walk_all(ao_lisp_moving, ao_lisp_move, ao_lisp_poly_move);
 }
 
 #if MDBG_DUMP
 }
 
 #if MDBG_DUMP
@@ -268,43 +261,22 @@ static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
        [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
 };
 
        [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
 };
 
+static int
+ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
+{
+       return ao_lisp_mark(type, *ref);
+}
+
+static int
+ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
+{
+       return ao_lisp_poly_mark(*p, do_note_cons);
+}
 
 static void
 ao_lisp_mark_busy(void)
 {
 
 static void
 ao_lisp_mark_busy(void)
 {
-       int i;
-
-       memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
-       memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
-       ao_lisp_cons_noted = 0;
-       MDBG("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)) {
-                               MDBG("root %d\n", MDBG_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)) {
-                               MDBG("root 0x%04x\n", p);
-                               ao_lisp_poly_mark(p, 0);
-                       }
-               }
-       }
-       while (ao_lisp_cons_noted) {
-               memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
-               memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
-               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;
-                               MDBG("cons %d\n", MDBG_OFFSET(v));
-                               ao_lisp_mark(&ao_lisp_cons_type, v);
-                       }
-               }
-       }
+       walk_all(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
 }
 
 void
 }
 
 void
index 93594c434339654961362b4d7f79087d562caf51..09a3611ca58f93ff5b201bb1d281edacc0daa3fd 100644 (file)
@@ -60,8 +60,8 @@
 
 (defun display ()
   (display-stacks 0 20 stacks)
 
 (defun display ()
   (display-stacks 0 20 stacks)
-  (move-to 1 1)
-  (patom "\n")
+  (move-to 1 21)
+  (flush)
   )
 
 (defun length (l)
   )
 
 (defun length (l)
     (setq stacks (replace stacks from from-stack))
     (setq stacks (replace stacks to to-stack))
     (display)
     (setq stacks (replace stacks from from-stack))
     (setq stacks (replace stacks to to-stack))
     (display)
-    (delay 500)
+    (delay 100)
     )
   )
 
     )
   )
 
   (clear)
   (_hanoi len 0 1 2)
   )
   (clear)
   (_hanoi len 0 1 2)
   )
+
+(hanoi)