[MLton-commit] r4061

Matthew Fluet MLton@mlton.org
Sat, 3 Sep 2005 19:08:42 -0700


More framework
----------------------------------------------------------------------

U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/Makefile
U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/TODO
U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/cheney-copy.c
A   mlton/branches/on-20050822-x86_64-branch/runtime/gc/frame.h
U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/gc_state.h
U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/model.h
U   mlton/branches/on-20050822-x86_64-branch/runtime/gc/object.h
A   mlton/branches/on-20050822-x86_64-branch/runtime/gc/stack.h
A   mlton/branches/on-20050822-x86_64-branch/runtime/gc/thread.h
A   mlton/branches/on-20050822-x86_64-branch/runtime/gc/weak.c
A   mlton/branches/on-20050822-x86_64-branch/runtime/gc/weak.h

----------------------------------------------------------------------

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/Makefile
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/Makefile	2005-09-04 00:58:04 UTC (rev 4060)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/Makefile	2005-09-04 02:08:38 UTC (rev 4061)
@@ -58,8 +58,12 @@
 HFILES = 								\
 	gc_prefix.h							\
 	util.h								\
+	model.h								\
 	object.h							\
-	model.h								\
+	stack.h								\
+	frame.h								\
+	thread.h							\
+	weak.h								\
 	heap.h								\
 	gc_state.h							\
 	gc_suffix.h

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/TODO
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/TODO	2005-09-04 00:58:04 UTC (rev 4060)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/TODO	2005-09-04 02:08:38 UTC (rev 4061)
@@ -3,4 +3,9 @@
 * eliminate STRING_TYPE_INDEX, STRING_TYPE_HEADER in favor or WORD8.
 * fix semantics of numNonPointers for normal objects to mean bytes of
         non-pointer data, rather than number of 32-bit words of
-        non-pointer data.
+        non-pointer data.  Rename to sizeNonPointers.
+* the unused field in GC_weak appears to be for alignment; is there a
+        way to have it work well with 64-bits?  Yes -- it requires
+        choosing the representation for Weaks based on the model and
+        the alignment; also, the GC will need to bump the pointer to
+        the word after the header to get GC_weak to overlay properly.

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/cheney-copy.c
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/cheney-copy.c	2005-09-04 00:58:04 UTC (rev 4060)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/cheney-copy.c	2005-09-04 02:08:38 UTC (rev 4061)
@@ -152,12 +152,12 @@
 }
 
 static void swapSemis (GC_state s) {
-        struct GC_heap h;
-
-        h = s->heap2;
-        s->heap2 = s->heap;
-        s->heap = h;
-        setCardMapForMutator (s);
+  struct GC_heap tempHeap;
+  
+  tempHeap = s->secondaryHeap;
+  s->secondaryHeap = s->heap;
+  s->heap = tempHeap;
+  setCardMapForMutator (s);
 }
 
 static inline bool detailedGCTime (GC_state s) {
@@ -172,8 +172,8 @@
         if (detailedGCTime (s))
                 startTiming (&ru_start);
         s->numCopyingGCs++;
-        s->toSpace = s->heap2.start;
-        s->toLimit = s->heap2.start + s->heap2.size;
+        s->toSpace = s->secondaryHeap.start;
+        s->toLimit = s->secondaryHeap.start + s->secondaryHeap.size;
         if (DEBUG or s->messages) {
                 fprintf (stderr, "Major copying GC.\n");
                 fprintf (stderr, "fromSpace = 0x%08x of size %s\n", 

Copied: mlton/branches/on-20050822-x86_64-branch/runtime/gc/frame.h (from rev 4025, mlton/branches/on-20050822-x86_64-branch/runtime/gc.h)
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc.h	2005-08-22 22:48:34 UTC (rev 4025)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/frame.h	2005-09-04 02:08:38 UTC (rev 4061)
@@ -0,0 +1,39 @@
+/* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
+ *    Jagannathan, and Stephen Weeks.
+ * Copyright (C) 1997-2000 NEC Research Institute.
+ *
+ * MLton is released under a BSD-style license.
+ * See the file MLton-LICENSE for details.
+ */
+
+/*
+ * The "... reserved bytes ..." of a stack object constitute a linear
+ * sequence of frames.  For the purposes of garbage collection, we
+ * must be able to recover the size and offsets of live heap-pointers
+ * for each frame.  This data is declared as follows:
+ *
+ *  GC_frameLayout *frameLayouts;
+ * 
+ * The frameLayouts pointer is initialized to point to a static array
+ * of frame layouts that is emitted for each compiled program.  The
+ * isC field identified whether or not the frame is for a C
+ * call. (Note: The ML stack is distinct from the system stack.  A C
+ * call executes on the system stack.  The frame left on the ML stack
+ * is just a marker.)  The numBytes field indicates the size of the
+ * frame, including space for the return address.  The offsets field
+ * points to an array (the zeroeth element recording the size of the
+ * array) whose elements record byte offsets from the bottom of the
+ * frame at which live heap pointers are located.
+ */
+typedef uint16_t *GC_offsets;
+
+typedef struct GC_frameLayout {
+  /* Identifies whether or not the frame is for a C call.  */
+  bool isC;
+  /* Number of bytes in frame, including space for return address. */
+  uint16_t numBytes;
+  /* Offsets from stackTop pointing at bottom of frame at which
+   * pointers are located.
+   */
+  GC_offsets offsets;
+} GC_frameLayout;

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/gc_state.h
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/gc_state.h	2005-09-04 00:58:04 UTC (rev 4060)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/gc_state.h	2005-09-04 02:08:38 UTC (rev 4061)
@@ -4,4 +4,5 @@
   struct GC_heap secondaryHeap; /* Used for major copying collection. */
   GC_objectType *objectTypes; /* Array of object types. */
   uint32_t objectTypesSize; /* Cardinality of objectTypes array. */
+  GC_weak weaks; /* Linked list of (live) weak pointers */
 } *GC_state;

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/model.h
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/model.h	2005-09-04 00:58:04 UTC (rev 4060)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/model.h	2005-09-04 02:08:38 UTC (rev 4061)
@@ -143,4 +143,4 @@
 #define OBJPTR_TYPE_(z) OBJPTR_TYPE__(z)
 #define OBJPTR_TYPE OBJPTR_TYPE_(GC_MODEL_Z)
 typedef OBJPTR_TYPE objptr;
-#define OBJPTR_SIZE (sizeof(objptr) / 4)
+#define OBJPTR_SIZE sizeof(objptr)

Modified: mlton/branches/on-20050822-x86_64-branch/runtime/gc/object.h
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc/object.h	2005-09-04 00:58:04 UTC (rev 4060)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/object.h	2005-09-04 02:08:38 UTC (rev 4061)
@@ -30,7 +30,7 @@
 #define PRIxHDR PRIx32
 #define FMTHDR "0x%08"PRIxHDR
 enum {
-  GC_HEADER_SIZE =   4,
+  GC_HEADER_SIZE =   sizeof(GC_header),
   TYPE_INDEX_BITS =  19,
   TYPE_INDEX_MASK =  0x000FFFFE,
   TYPE_INDEX_SHIFT = 1,
@@ -78,9 +78,9 @@
   GC_ARRAY_HEADER_SIZE =  GC_ARRAY_COUNTER_SIZE + GC_ARRAY_LENGTH_SIZE + GC_HEADER_SIZE,
 };
 
-/* Stack objects are described in stack.h */
+/* Stack objects are described in "stack.h" */
 
-/* Weak objects are described in weak.h */
+/* Weak objects are described in "weak.h" */
 
 
 /* 

Copied: mlton/branches/on-20050822-x86_64-branch/runtime/gc/stack.h (from rev 4025, mlton/branches/on-20050822-x86_64-branch/runtime/gc.h)
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc.h	2005-08-22 22:48:34 UTC (rev 4025)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/stack.h	2005-09-04 02:08:38 UTC (rev 4061)
@@ -0,0 +1,48 @@
+/* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
+ *    Jagannathan, and Stephen Weeks.
+ * Copyright (C) 1997-2000 NEC Research Institute.
+ *
+ * MLton is released under a BSD-style license.
+ * See the file MLton-LICENSE for details.
+ */
+
+/*
+ * Stack objects have the following layout:
+ * 
+ * header word ::
+ * markTop pointer ::
+ * markIndex word ::
+ * reserved word ::
+ * used word ::
+ * ... reserved bytes ...
+ *
+ * The markTop pointer and markIndex word are used by mark compact GC.
+ * The reserved word gives the number of bytes for the stack (before
+ * the next ML object).  The used word gives the number of bytes
+ * currently used by the stack.  The sequence of reserved bytes
+ * correspond to ML stack frames, which will be discussed in more
+ * detail in "frame.h".
+*/
+typedef struct GC_stack {       
+  /* markTop and markIndex are only used during marking.  They record
+   * the current pointer in the stack that is being followed.  markTop
+   * points to the top of the stack frame containing the pointer and
+   * markI is the index in that frames frameOffsets of the pointer
+   * slot.  So, when the GC pointer reversal gets back to the stack,
+   * it can continue with the next pointer (either in the current
+   * frame or the next frame).
+   */
+  pointer markTop;
+  uint32_t markIndex;
+  /* reserved is the number of bytes reserved for stack, 
+   * i.e. its maximum size.
+   */
+  uint32_t reserved;
+  /* used is the number of bytes used by the stack.  
+   * Stacks with used == reserved are continuations.
+   */
+  uint32_t used;      
+  /* The next address is the bottom of the stack, and the following
+   * reserved bytes hold space for the stack.
+   */
+} *GC_stack;

Copied: mlton/branches/on-20050822-x86_64-branch/runtime/gc/thread.h (from rev 4025, mlton/branches/on-20050822-x86_64-branch/runtime/gc.h)
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc.h	2005-08-22 22:48:34 UTC (rev 4025)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/thread.h	2005-09-04 02:08:38 UTC (rev 4061)
@@ -0,0 +1,23 @@
+/* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
+ *    Jagannathan, and Stephen Weeks.
+ * Copyright (C) 1997-2000 NEC Research Institute.
+ *
+ * MLton is released under a BSD-style license.
+ * See the file MLton-LICENSE for details.
+ */
+
+typedef struct GC_thread {
+  /* The order of these fields is important.  The nonpointer fields
+   * must be first, because this object must appear to be a normal
+   * heap object.
+   * Furthermore, the exnStack field must be first, because the native
+   * codegen depends on this (which is bad and should be fixed).
+   */
+  uint32_t exnStack;     /* An offset added to stackBottom that specifies 
+                          * where the top of the exnStack is.
+                          */
+  uint32_t bytesNeeded;  /* The number of bytes needed when returning
+                          * to this thread.
+                          */
+  objptr stack;          /* The stack for this thread. */
+} *GC_thread;

Copied: mlton/branches/on-20050822-x86_64-branch/runtime/gc/weak.c (from rev 4025, mlton/branches/on-20050822-x86_64-branch/runtime/gc.c)
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc.c	2005-08-22 22:48:34 UTC (rev 4025)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/weak.c	2005-09-04 02:08:38 UTC (rev 4061)
@@ -0,0 +1,40 @@
+/* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
+ *    Jagannathan, and Stephen Weeks.
+ * Copyright (C) 1997-2000 NEC Research Institute.
+ *
+ * MLton is released under a BSD-style license.
+ * See the file MLton-LICENSE for details.
+ */
+
+
+bool GC_weakCanGet (pointer p) {
+        Bool res;
+
+        res = WEAK_GONE_HEADER != GC_getHeader (p);
+        if (DEBUG_WEAK)
+                fprintf (stderr, "%s = GC_weakCanGet (0x%08x)\n",
+                                boolToString (res), (uint)p);
+        return res;
+}
+
+Pointer GC_weakGet (Pointer p) {
+        pointer res;
+
+        res = ((GC_weak)p)->object;
+        if (DEBUG_WEAK)
+                fprintf (stderr, "0x%08x = GC_weakGet (0x%08x)\n",
+                                (uint)res, (uint)p);
+        return res;
+}
+
+Pointer GC_weakNew (GC_state s, Word32 header, Pointer p) {
+        pointer res;
+
+        res = object (s, header, GC_NORMAL_HEADER_SIZE + 3 * WORD_SIZE, 
+                        FALSE, FALSE);
+        ((GC_weak)res)->object = p;
+        if (DEBUG_WEAK)
+                fprintf (stderr, "0x%08x = GC_weakNew (0x%08x, 0x%08x)\n",
+                                (uint)res, (uint)header, (uint)p);
+        return res;
+}

Copied: mlton/branches/on-20050822-x86_64-branch/runtime/gc/weak.h (from rev 4025, mlton/branches/on-20050822-x86_64-branch/runtime/gc.h)
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/runtime/gc.h	2005-08-22 22:48:34 UTC (rev 4025)
+++ mlton/branches/on-20050822-x86_64-branch/runtime/gc/weak.h	2005-09-04 02:08:38 UTC (rev 4061)
@@ -0,0 +1,33 @@
+/* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
+ *    Jagannathan, and Stephen Weeks.
+ * Copyright (C) 1997-2000 NEC Research Institute.
+ *
+ * MLton is released under a BSD-style license.
+ * See the file MLton-LICENSE for details.
+ */
+
+/*
+ * Weak objects have the following layout:
+ * 
+ * header word ::
+ * unused word ::
+ * link word ::
+ * heap-pointer
+ *
+ * The object type indexed by the header determines whether the weak
+ * is valid or not.  If the type has numPointers == 1, then the weak
+ * pointer is valid.  Otherwise, the type has numPointers == 0 and the
+ * weak pointer is not valid.
+ *
+ * The first word is unused; present for alignment purposes
+ *
+ * The second word is used to chain the live weaks together during a copying gc
+ * and is otherwise unused.
+ *
+ * The third word is the weak pointer.
+ */ 
+typedef struct GC_weak {
+  uint32_t unused;
+  struct GC_weak *link;
+  objptr object;
+} *GC_weak;