Kill Type pretty-printer
[ghc.git] / includes / rts / storage / InfoTables.h
1 /* ----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2002
4 *
5 * Info Tables
6 *
7 * -------------------------------------------------------------------------- */
8
9 #ifndef RTS_STORAGE_INFOTABLES_H
10 #define RTS_STORAGE_INFOTABLES_H
11
12 /* ----------------------------------------------------------------------------
13 Relative pointers
14
15 Several pointer fields in info tables are expressed as offsets
16 relative to the info pointer, so that we can generate
17 position-independent code.
18
19 Note [x86-64-relative]
20 There is a complication on the x86_64 platform, where pointers are
21 64 bits, but the tools don't support 64-bit relative relocations.
22 However, the default memory model (small) ensures that all symbols
23 have values in the lower 2Gb of the address space, so offsets all
24 fit in 32 bits. Hence we can use 32-bit offset fields.
25
26 Somewhere between binutils-2.16.1 and binutils-2.16.91.0.6,
27 support for 64-bit PC-relative relocations was added, so maybe this
28 hackery can go away sometime.
29 ------------------------------------------------------------------------- */
30
31 #if x86_64_TARGET_ARCH
32 #define OFFSET_FIELD(n) StgHalfInt n; StgHalfWord __pad_##n
33 #else
34 #define OFFSET_FIELD(n) StgInt n
35 #endif
36
37 /* -----------------------------------------------------------------------------
38 Profiling info
39 -------------------------------------------------------------------------- */
40
41 typedef struct {
42 #ifndef TABLES_NEXT_TO_CODE
43 char *closure_type;
44 char *closure_desc;
45 #else
46 OFFSET_FIELD(closure_type_off);
47 OFFSET_FIELD(closure_desc_off);
48 #endif
49 } StgProfInfo;
50
51 /* -----------------------------------------------------------------------------
52 Closure flags
53 -------------------------------------------------------------------------- */
54
55 /* The type flags provide quick access to certain properties of a closure. */
56
57 #define _HNF (1<<0) /* head normal form? */
58 #define _BTM (1<<1) /* uses info->layout.bitmap */
59 #define _NS (1<<2) /* non-sparkable */
60 #define _STA (1<<3) /* static? */
61 #define _THU (1<<4) /* thunk? */
62 #define _MUT (1<<5) /* mutable? */
63 #define _UPT (1<<6) /* unpointed? */
64 #define _SRT (1<<7) /* has an SRT? */
65 #define _IND (1<<8) /* is an indirection? */
66
67 #define isSTATIC(flags) ((flags) &_STA)
68 #define isMUTABLE(flags) ((flags) &_MUT)
69 #define isBITMAP(flags) ((flags) &_BTM)
70 #define isTHUNK(flags) ((flags) &_THU)
71 #define isUNPOINTED(flags) ((flags) &_UPT)
72 #define hasSRT(flags) ((flags) &_SRT)
73
74 extern StgWord16 closure_flags[];
75
76 #define closureFlags(c) (closure_flags[get_itbl \
77 (UNTAG_CONST_CLOSURE(c))->type])
78
79 #define closure_HNF(c) ( closureFlags(c) & _HNF)
80 #define closure_BITMAP(c) ( closureFlags(c) & _BTM)
81 #define closure_NON_SPARK(c) ( (closureFlags(c) & _NS))
82 #define closure_SHOULD_SPARK(c) (!(closureFlags(c) & _NS))
83 #define closure_STATIC(c) ( closureFlags(c) & _STA)
84 #define closure_THUNK(c) ( closureFlags(c) & _THU)
85 #define closure_MUTABLE(c) ( closureFlags(c) & _MUT)
86 #define closure_UNPOINTED(c) ( closureFlags(c) & _UPT)
87 #define closure_SRT(c) ( closureFlags(c) & _SRT)
88 #define closure_IND(c) ( closureFlags(c) & _IND)
89
90 /* same as above but for info-ptr rather than closure */
91 #define ipFlags(ip) (closure_flags[ip->type])
92
93 #define ip_HNF(ip) ( ipFlags(ip) & _HNF)
94 #define ip_BITMAP(ip) ( ipFlags(ip) & _BTM)
95 #define ip_SHOULD_SPARK(ip) (!(ipFlags(ip) & _NS))
96 #define ip_STATIC(ip) ( ipFlags(ip) & _STA)
97 #define ip_THUNK(ip) ( ipFlags(ip) & _THU)
98 #define ip_MUTABLE(ip) ( ipFlags(ip) & _MUT)
99 #define ip_UNPOINTED(ip) ( ipFlags(ip) & _UPT)
100 #define ip_SRT(ip) ( ipFlags(ip) & _SRT)
101 #define ip_IND(ip) ( ipFlags(ip) & _IND)
102
103 /* -----------------------------------------------------------------------------
104 Bitmaps
105
106 These are used to describe the pointerhood of a sequence of words
107 (usually on the stack) to the garbage collector. The two primary
108 uses are for stack frames, and functions (where we need to describe
109 the layout of a PAP to the GC).
110
111 In these bitmaps: 0 == ptr, 1 == non-ptr.
112 -------------------------------------------------------------------------- */
113
114 /*
115 * Small bitmaps: for a small bitmap, we store the size and bitmap in
116 * the same word, using the following macros. If the bitmap doesn't
117 * fit in a single word, we use a pointer to an StgLargeBitmap below.
118 */
119 #define MK_SMALL_BITMAP(size,bits) (((bits)<<BITMAP_BITS_SHIFT) | (size))
120
121 #define BITMAP_SIZE(bitmap) ((bitmap) & BITMAP_SIZE_MASK)
122 #define BITMAP_BITS(bitmap) ((bitmap) >> BITMAP_BITS_SHIFT)
123
124 /*
125 * A large bitmap.
126 */
127 typedef struct {
128 StgWord size;
129 StgWord bitmap[];
130 } StgLargeBitmap;
131
132 /* -----------------------------------------------------------------------------
133 SRTs (Static Reference Tables)
134
135 These tables are used to keep track of the static objects referred
136 to by the code for a closure or stack frame, so that we can follow
137 static data references from code and thus accurately
138 garbage-collect CAFs.
139 -------------------------------------------------------------------------- */
140
141 /* An SRT is just an array of closure pointers: */
142 typedef StgClosure* StgSRT[];
143
144 /*
145 * Each info table refers to some subset of the closure pointers in an
146 * SRT. It does this using a pair of an StgSRT pointer and a
147 * half-word bitmap. If the half-word bitmap isn't large enough, then
148 * we fall back to a large SRT, including an unbounded bitmap. If the
149 * half-word bitmap is set to all ones (0xffff), then the StgSRT
150 * pointer instead points to an StgLargeSRT:
151 */
152 typedef struct StgLargeSRT_ {
153 StgSRT *srt;
154 StgLargeBitmap l;
155 } StgLargeSRT;
156
157 /* ----------------------------------------------------------------------------
158 Info Tables
159 ------------------------------------------------------------------------- */
160
161 /*
162 * Stuff describing the closure layout. Well, actually, it might
163 * contain the selector index for a THUNK_SELECTOR. This union is one
164 * word long.
165 */
166 typedef union {
167 struct { /* Heap closure payload layout: */
168 StgHalfWord ptrs; /* number of pointers */
169 StgHalfWord nptrs; /* number of non-pointers */
170 } payload;
171
172 StgWord bitmap; /* word-sized bit pattern describing */
173 /* a stack frame: see below */
174
175 #ifndef TABLES_NEXT_TO_CODE
176 StgLargeBitmap* large_bitmap; /* pointer to large bitmap structure */
177 #else
178 OFFSET_FIELD(large_bitmap_offset); /* offset from info table to large bitmap structure */
179 #endif
180
181 StgWord selector_offset; /* used in THUNK_SELECTORs */
182
183 } StgClosureInfo;
184
185
186 /*
187 * The "standard" part of an info table. Every info table has this bit.
188 */
189 typedef struct StgInfoTable_ {
190
191 #if !defined(TABLES_NEXT_TO_CODE)
192 StgFunPtr entry; /* pointer to the entry code */
193 #endif
194
195 #ifdef PROFILING
196 StgProfInfo prof;
197 #endif
198
199 StgClosureInfo layout; /* closure layout info (one word) */
200
201 StgHalfWord type; /* closure type */
202 StgHalfWord srt_bitmap;
203 /* In a CONSTR:
204 - the constructor tag
205 In a FUN/THUNK
206 - a bitmap of SRT entries
207 */
208
209 #ifdef TABLES_NEXT_TO_CODE
210 StgCode code[];
211 #endif
212 } *StgInfoTablePtr; // StgInfoTable defined in rts/Types.h
213
214
215 /* -----------------------------------------------------------------------------
216 Function info tables
217
218 This is the general form of function info tables. The compiler
219 will omit some of the fields in common cases:
220
221 - If fun_type is not ARG_GEN or ARG_GEN_BIG, then the slow_apply
222 and bitmap fields may be left out (they are at the end, so omitting
223 them doesn't affect the layout).
224
225 - If srt_bitmap (in the std info table part) is zero, then the srt
226 field needn't be set. This only applies if the slow_apply and
227 bitmap fields have also been omitted.
228 -------------------------------------------------------------------------- */
229
230 /*
231 Note [Encoding static reference tables]
232 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233
234 As static reference tables appear frequently in code, we use a special
235 compact encoding for the common case of a module defining only a few CAFs: We
236 produce one table containing a list of CAFs in the module and then include a
237 bitmap in each info table describing which entries of this table the closure
238 references.
239 */
240
241 typedef struct StgFunInfoExtraRev_ {
242 OFFSET_FIELD(slow_apply_offset); /* apply to args on the stack */
243 union {
244 StgWord bitmap;
245 OFFSET_FIELD(bitmap_offset); /* arg ptr/nonptr bitmap */
246 } b;
247 OFFSET_FIELD(srt_offset); /* pointer to the SRT table */
248 StgHalfWord fun_type; /* function type */
249 StgHalfWord arity; /* function arity */
250 } StgFunInfoExtraRev;
251
252 typedef struct StgFunInfoExtraFwd_ {
253 StgHalfWord fun_type; /* function type */
254 StgHalfWord arity; /* function arity */
255 StgSRT *srt; /* pointer to the SRT table */
256 union { /* union for compat. with TABLES_NEXT_TO_CODE version */
257 StgWord bitmap; /* arg ptr/nonptr bitmap */
258 } b;
259 StgFun *slow_apply; /* apply to args on the stack */
260 } StgFunInfoExtraFwd;
261
262 typedef struct {
263 #if defined(TABLES_NEXT_TO_CODE)
264 StgFunInfoExtraRev f;
265 StgInfoTable i;
266 #else
267 StgInfoTable i;
268 StgFunInfoExtraFwd f;
269 #endif
270 } StgFunInfoTable;
271
272 // canned bitmap for each arg type, indexed by constants in FunTypes.h
273 extern StgWord stg_arg_bitmaps[];
274
275 /* -----------------------------------------------------------------------------
276 Return info tables
277 -------------------------------------------------------------------------- */
278
279 /*
280 * When info tables are laid out backwards, we can omit the SRT
281 * pointer iff srt_bitmap is zero.
282 */
283
284 typedef struct {
285 #if defined(TABLES_NEXT_TO_CODE)
286 OFFSET_FIELD(srt_offset); /* offset to the SRT table */
287 StgInfoTable i;
288 #else
289 StgInfoTable i;
290 StgSRT *srt; /* pointer to the SRT table */
291 #endif
292 } StgRetInfoTable;
293
294 /* -----------------------------------------------------------------------------
295 Thunk info tables
296 -------------------------------------------------------------------------- */
297
298 /*
299 * When info tables are laid out backwards, we can omit the SRT
300 * pointer iff srt_bitmap is zero.
301 */
302
303 typedef struct StgThunkInfoTable_ {
304 #if !defined(TABLES_NEXT_TO_CODE)
305 StgInfoTable i;
306 #endif
307 #if defined(TABLES_NEXT_TO_CODE)
308 OFFSET_FIELD(srt_offset); /* offset to the SRT table */
309 #else
310 StgSRT *srt; /* pointer to the SRT table */
311 #endif
312 #if defined(TABLES_NEXT_TO_CODE)
313 StgInfoTable i;
314 #endif
315 } StgThunkInfoTable;
316
317 /* -----------------------------------------------------------------------------
318 Constructor info tables
319 -------------------------------------------------------------------------- */
320
321 typedef struct StgConInfoTable_ {
322 #if !defined(TABLES_NEXT_TO_CODE)
323 StgInfoTable i;
324 #endif
325
326 #if defined(TABLES_NEXT_TO_CODE)
327 OFFSET_FIELD(con_desc); // the name of the data constructor
328 // as: Package:Module.Name
329 #else
330 char *con_desc;
331 #endif
332
333 #if defined(TABLES_NEXT_TO_CODE)
334 StgInfoTable i;
335 #endif
336 } StgConInfoTable;
337
338
339 /* -----------------------------------------------------------------------------
340 Accessor macros for fields that might be offsets (C version)
341 -------------------------------------------------------------------------- */
342
343 /*
344 * GET_SRT(info)
345 * info must be a Stg[Ret|Thunk]InfoTable* (an info table that has a SRT)
346 */
347 #ifdef TABLES_NEXT_TO_CODE
348 #define GET_SRT(info) ((StgSRT*) (((StgWord) ((info)+1)) + (info)->srt_offset))
349 #else
350 #define GET_SRT(info) ((info)->srt)
351 #endif
352
353 /*
354 * GET_CON_DESC(info)
355 * info must be a StgConInfoTable*.
356 */
357 #ifdef TABLES_NEXT_TO_CODE
358 #define GET_CON_DESC(info) \
359 ((const char *)((StgWord)((info)+1) + (info->con_desc)))
360 #else
361 #define GET_CON_DESC(info) ((const char *)(info)->con_desc)
362 #endif
363
364 /*
365 * GET_FUN_SRT(info)
366 * info must be a StgFunInfoTable*
367 */
368 #ifdef TABLES_NEXT_TO_CODE
369 #define GET_FUN_SRT(info) ((StgSRT*) (((StgWord) ((info)+1)) + (info)->f.srt_offset))
370 #else
371 #define GET_FUN_SRT(info) ((info)->f.srt)
372 #endif
373
374 #ifdef TABLES_NEXT_TO_CODE
375 #define GET_LARGE_BITMAP(info) ((StgLargeBitmap*) (((StgWord) ((info)+1)) \
376 + (info)->layout.large_bitmap_offset))
377 #else
378 #define GET_LARGE_BITMAP(info) ((info)->layout.large_bitmap)
379 #endif
380
381 #ifdef TABLES_NEXT_TO_CODE
382 #define GET_FUN_LARGE_BITMAP(info) ((StgLargeBitmap*) (((StgWord) ((info)+1)) \
383 + (info)->f.b.bitmap_offset))
384 #else
385 #define GET_FUN_LARGE_BITMAP(info) ((StgLargeBitmap*) ((info)->f.b.bitmap))
386 #endif
387
388 /*
389 * GET_PROF_TYPE, GET_PROF_DESC
390 */
391 #ifdef TABLES_NEXT_TO_CODE
392 #define GET_PROF_TYPE(info) ((char *)((StgWord)((info)+1) + (info->prof.closure_type_off)))
393 #else
394 #define GET_PROF_TYPE(info) ((info)->prof.closure_type)
395 #endif
396 #ifdef TABLES_NEXT_TO_CODE
397 #define GET_PROF_DESC(info) ((char *)((StgWord)((info)+1) + (info->prof.closure_desc_off)))
398 #else
399 #define GET_PROF_DESC(info) ((info)->prof.closure_desc)
400 #endif
401
402 #endif /* RTS_STORAGE_INFOTABLES_H */