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