Import Upstream version 20180207
[hcoop/debian/mlton.git] / runtime / gc / object.h
1 /* Copyright (C) 2016 Matthew Fluet.
2 * Copyright (C) 1999-2007 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 * Copyright (C) 1997-2000 NEC Research Institute.
5 *
6 * MLton is released under a BSD-style license.
7 * See the file MLton-LICENSE for details.
8 */
9
10 #if (defined (MLTON_GC_INTERNAL_TYPES))
11
12 /*
13 * There are four kinds of ML objects:
14 * array, normal (fixed size), stack, and weak.
15 */
16 typedef enum {
17 /* The tag indices here must agree with those in declareObjectTypes()
18 * in codegen/c-codegen/c-codegen.fun. */
19 ARRAY_TAG = 0,
20 NORMAL_TAG = 1,
21 STACK_TAG = 2,
22 WEAK_TAG = 3,
23 } GC_objectTypeTag;
24
25 #endif /* (defined (MLTON_GC_INTERNAL_TYPES)) */
26
27 #if (defined (MLTON_GC_INTERNAL_FUNCS))
28
29 static const char* objectTypeTagToString (GC_objectTypeTag tag);
30
31 #endif /* (defined (MLTON_GC_INTERNAL_FUNCS)) */
32
33
34 #if (defined (MLTON_GC_INTERNAL_TYPES))
35
36 /*
37 * Each object has a header, which immediately precedes the object data.
38 * A header has the following bit layout:
39 *
40 * 00 : 1
41 * 01 - 19 : type index bits, index into GC_state->objectTypes.
42 * 20 - 30 : counter bits, used by mark compact GC (initially 0)
43 * 31 : mark bit, used by mark compact GC (initially 0)
44 * 32 - 63 : 0wx00000000 (only w/ 64-bit header)
45 */
46
47 #define GC_HEADER_TYPE__(z) uint ## z ## _t
48 #define GC_HEADER_TYPE_(z) GC_HEADER_TYPE__(z)
49 #define GC_HEADER_TYPE GC_HEADER_TYPE_(GC_MODEL_HEADER_SIZE)
50 typedef GC_HEADER_TYPE GC_header;
51 #define GC_HEADER_SIZE sizeof(GC_header)
52 #define PRIxHDR__(z) PRIx ## z
53 #define PRIxHDR_(z) PRIxHDR__(z)
54 #define PRIxHDR PRIxHDR_(GC_MODEL_HEADER_SIZE)
55 #define FMTHDR "%08"PRIxHDR
56
57 COMPILE_TIME_ASSERT(sizeof_objptr__eq__sizeof_header,
58 sizeof(objptr) == sizeof(GC_header));
59
60 #define GC_VALID_HEADER_MASK ((GC_header)0x1)
61 #define TYPE_INDEX_BITS 19
62 #define TYPE_INDEX_MASK ((GC_header)0x000FFFFE)
63 #define TYPE_INDEX_SHIFT 1
64 #define COUNTER_BITS 10
65 #define COUNTER_MASK ((GC_header)0x7FF00000)
66 #define COUNTER_SHIFT 20
67 #define MARK_BITS 1
68 #define MARK_MASK ((GC_header)0x80000000)
69 #define MARK_SHIFT 31
70
71 #endif /* (defined (MLTON_GC_INTERNAL_TYPES)) */
72
73 #if (defined (MLTON_GC_INTERNAL_FUNCS))
74
75 static inline GC_header* getHeaderp (pointer p);
76 static inline GC_header getHeader (pointer p);
77 static inline GC_header buildHeaderFromTypeIndex (uint32_t t);
78
79 #endif /* (defined (MLTON_GC_INTERNAL_FUNCS)) */
80
81
82 #if (defined (MLTON_GC_INTERNAL_TYPES))
83
84 /*
85 * Normal objects have the following layout:
86 *
87 * header ::
88 * (non heap-pointers)* ::
89 * (heap pointers)*
90 *
91 * Note that the non heap-pointers denote a sequence of primitive data
92 * values. These data values need not map directly to values of the
93 * native word size. MLton's aggressive representation strategies may
94 * pack multiple primitive values into the same native word.
95 * Likewise, a primitive value may span multiple native words (e.g.,
96 * Word64.word on an x86).
97 */
98 #define GC_NORMAL_METADATA_SIZE (GC_HEADER_SIZE)
99
100 #endif /* (defined (MLTON_GC_INTERNAL_TYPES)) */
101
102
103 /* Array objects are described in "array.h" */
104
105 /* Stack objects are described in "stack.h" */
106
107 /* Weak objects are described in "weak.h" */
108
109
110 #if (defined (MLTON_GC_INTERNAL_TYPES))
111
112 /*
113 * The type index of a header is an index into an array of object
114 * types, where each element describes the layout of an object. The
115 * object types array is declared as:
116 *
117 * GC_objectType *objectTypes;
118 *
119 * The objectTypes pointer is initialized to point to a static array
120 * of object types that is emitted for each compiled program. The
121 * hasIdentity field indicates whether or not the object has mutable
122 * fields, in which case it may not be hash-cons-ed. In a normal
123 * object, the bytesNonObjptrs field indicates the number of bytes of
124 * non heap-pointer data, while the numObjptrs field indicates the
125 * number of heap pointers. In an array object, the bytesNonObjptrs
126 * field indicates the number of bytes of non heap-pointer data in a
127 * single array element, while the numObjptrs field indicates the
128 * number of heap pointers in a single array element. In a stack
129 * object, the bytesNonObjptrs and numObjptrs fields are irrelevant.
130 * In a weak object, the bytesNonObjptrs and numObjptrs fields are
131 * interpreted as in a normal object.
132 */
133 typedef struct GC_objectType {
134 /* Keep tag first, at zero offset, since it is referenced most often. */
135 GC_objectTypeTag tag;
136 bool hasIdentity;
137 uint16_t bytesNonObjptrs;
138 uint16_t numObjptrs;
139 } *GC_objectType;
140 enum {
141 /* The type indices here must agree with those in backend/rep-type.fun. */
142 STACK_TYPE_INDEX = 0,
143 THREAD_TYPE_INDEX = 1,
144 WEAK_GONE_TYPE_INDEX = 2,
145 WORD8_VECTOR_TYPE_INDEX = 3,
146 WORD32_VECTOR_TYPE_INDEX = 4,
147 WORD16_VECTOR_TYPE_INDEX = 5,
148 WORD64_VECTOR_TYPE_INDEX = 6,
149 };
150
151 #endif /* (defined (MLTON_GC_INTERNAL_TYPES)) */
152
153 #if (defined (MLTON_GC_INTERNAL_FUNCS))
154
155 #define GC_STACK_HEADER buildHeaderFromTypeIndex (STACK_TYPE_INDEX)
156 #define GC_THREAD_HEADER buildHeaderFromTypeIndex (THREAD_TYPE_INDEX)
157 #define GC_WEAK_GONE_HEADER buildHeaderFromTypeIndex (WEAK_GONE_TYPE_INDEX)
158 #define GC_WORD8_VECTOR_HEADER buildHeaderFromTypeIndex (WORD8_VECTOR_TYPE_INDEX)
159 #define GC_WORD16_VECTOR_HEADER buildHeaderFromTypeIndex (WORD16_VECTOR_TYPE_INDEX)
160 #define GC_WORD32_VECTOR_HEADER buildHeaderFromTypeIndex (WORD32_VECTOR_TYPE_INDEX)
161 #define GC_WORD64_VECTOR_HEADER buildHeaderFromTypeIndex (WORD64_VECTOR_TYPE_INDEX)
162
163 static inline void splitHeader (GC_state s, GC_header header,
164 GC_objectTypeTag *tagRet, bool *hasIdentityRet,
165 uint16_t *bytesNonObjptrsRet, uint16_t *numObjptrsRet);
166 static inline pointer advanceToObjectData (GC_state s, pointer p);
167
168 #endif /* (defined (MLTON_GC_INTERNAL_FUNCS)) */