1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2002
7 * -------------------------------------------------------------------------- */
12 /* ----------------------------------------------------------------------------
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.
19 Note [x86-64-relative]
20 There is a complication on the x86_64 platform, where pointeres 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.
26 When going via-C, the mangler arranges that we only generate
27 relative relocations between symbols in the same segment (the text
28 segment). The NCG, however, puts things in the right sections and
29 uses 32-bit relative offsets instead.
31 Somewhere between binutils-2.16.1 and binutils-2.16.91.0.6,
32 support for 64-bit PC-relative relocations was added, so maybe this
33 hackery can go away sometime.
34 ------------------------------------------------------------------------- */
36 #if x86_64_TARGET_ARCH
37 #define OFFSET_FIELD(n) StgHalfInt n; StgHalfWord __pad_##n;
39 #define OFFSET_FIELD(n) StgInt n;
42 /* -----------------------------------------------------------------------------
44 -------------------------------------------------------------------------- */
47 #ifndef TABLES_NEXT_TO_CODE
51 OFFSET_FIELD(closure_type_off);
52 OFFSET_FIELD(closure_desc_off);
56 /* -----------------------------------------------------------------------------
58 -------------------------------------------------------------------------- */
60 #if 0 && (defined(PAR) || defined(GRAN))
63 ToDo: use this in StgInfoTable (mutually recursive) -- HWL */
66 StgInfoTable *rbh_infoptr; /* infoptr to the RBH */
72 Copied from ghc-0.29; ToDo: check this code -- HWL
74 In the parallel system, all updatable closures have corresponding
75 revertible black holes. When we are assembly-mangling, we guarantee
76 that the revertible black hole code precedes the normal entry code, so
77 that the RBH info table resides at a fixed offset from the normal info
78 table. Otherwise, we add the RBH info table pointer to the end of the
79 normal info table and vice versa.
81 Currently has to use a !RBH_MAGIC_OFFSET setting.
82 Still todo: init of par.infoptr field in all infotables!!
85 #if defined(PAR) || defined(GRAN)
87 # ifdef RBH_MAGIC_OFFSET
89 # error magic offset not yet implemented
91 # define RBH_INFO_WORDS 0
92 # define INCLUDE_RBH_INFO(infoptr)
94 # define RBH_INFOPTR(infoptr) (((P_)infoptr) - RBH_MAGIC_OFFSET)
95 # define REVERT_INFOPTR(infoptr) (((P_)infoptr) + RBH_MAGIC_OFFSET)
99 # define RBH_INFO_WORDS 1
100 # define INCLUDE_RBH_INFO(info) rbh_infoptr : &(info)
102 # define RBH_INFOPTR(infoptr) (((StgInfoTable *)(infoptr))->rbh_infoptr)
103 # define REVERT_INFOPTR(infoptr) (((StgInfoTable *)(infoptr))->rbh_infoptr)
107 /* see ParallelRts.h */
110 StgClosure *convertToRBH(StgClosure *closure);
112 void convertFromRBH(StgClosure *closure);
114 void convertToFetchMe(StgPtr closure, globalAddr *ga);
120 /* -----------------------------------------------------------------------------
123 There is no ticky-specific stuff in an info table at this time.
124 -------------------------------------------------------------------------- */
126 /* -----------------------------------------------------------------------------
128 -------------------------------------------------------------------------- */
136 #else /* !DEBUG_CLOSURE */
138 /* There is no DEBUG-specific stuff in an info table at this time. */
140 #endif /* DEBUG_CLOSURE */
142 /* -----------------------------------------------------------------------------
144 -------------------------------------------------------------------------- */
146 /* The type flags provide quick access to certain properties of a closure. */
148 #define _HNF (1<<0) /* head normal form? */
149 #define _BTM (1<<1) /* bitmap-style layout? */
150 #define _NS (1<<2) /* non-sparkable */
151 #define _STA (1<<3) /* static? */
152 #define _THU (1<<4) /* thunk? */
153 #define _MUT (1<<5) /* mutable? */
154 #define _UPT (1<<6) /* unpointed? */
155 #define _SRT (1<<7) /* has an SRT? */
156 #define _IND (1<<8) /* is an indirection? */
158 #define isSTATIC(flags) ((flags) &_STA)
159 #define isMUTABLE(flags) ((flags) &_MUT)
160 #define isBITMAP(flags) ((flags) &_BTM)
161 #define isTHUNK(flags) ((flags) &_THU)
162 #define isUNPOINTED(flags) ((flags) &_UPT)
163 #define hasSRT(flags) ((flags) &_SRT)
165 extern StgWord16 closure_flags[];
167 #define closureFlags(c) (closure_flags[get_itbl(UNTAG_CLOSURE(c))->type])
169 #define closure_HNF(c) ( closureFlags(c) & _HNF)
170 #define closure_BITMAP(c) ( closureFlags(c) & _BTM)
171 #define closure_NON_SPARK(c) ( (closureFlags(c) & _NS))
172 #define closure_SHOULD_SPARK(c) (!(closureFlags(c) & _NS))
173 #define closure_STATIC(c) ( closureFlags(c) & _STA)
174 #define closure_THUNK(c) ( closureFlags(c) & _THU)
175 #define closure_MUTABLE(c) ( closureFlags(c) & _MUT)
176 #define closure_UNPOINTED(c) ( closureFlags(c) & _UPT)
177 #define closure_SRT(c) ( closureFlags(c) & _SRT)
178 #define closure_IND(c) ( closureFlags(c) & _IND)
180 /* same as above but for info-ptr rather than closure */
181 #define ipFlags(ip) (closure_flags[ip->type])
183 #define ip_HNF(ip) ( ipFlags(ip) & _HNF)
184 #define ip_BITMAP(ip) ( ipFlags(ip) & _BTM)
185 #define ip_SHOULD_SPARK(ip) (!(ipFlags(ip) & _NS))
186 #define ip_STATIC(ip) ( ipFlags(ip) & _STA)
187 #define ip_THUNK(ip) ( ipFlags(ip) & _THU)
188 #define ip_MUTABLE(ip) ( ipFlags(ip) & _MUT)
189 #define ip_UNPOINTED(ip) ( ipFlags(ip) & _UPT)
190 #define ip_SRT(ip) ( ipFlags(ip) & _SRT)
191 #define ip_IND(ip) ( ipFlags(ip) & _IND)
193 /* -----------------------------------------------------------------------------
196 These are used to describe the pointerhood of a sequence of words
197 (usually on the stack) to the garbage collector. The two primary
198 uses are for stack frames, and functions (where we need to describe
199 the layout of a PAP to the GC).
201 In these bitmaps: 0 == ptr, 1 == non-ptr.
202 -------------------------------------------------------------------------- */
205 * Small bitmaps: for a small bitmap, we store the size and bitmap in
206 * the same word, using the following macros. If the bitmap doesn't
207 * fit in a single word, we use a pointer to an StgLargeBitmap below.
209 #define MK_SMALL_BITMAP(size,bits) (((bits)<<BITMAP_BITS_SHIFT) | (size))
211 #define BITMAP_SIZE(bitmap) ((bitmap) & BITMAP_SIZE_MASK)
212 #define BITMAP_BITS(bitmap) ((bitmap) >> BITMAP_BITS_SHIFT)
219 StgWord bitmap[FLEXIBLE_ARRAY];
222 /* -----------------------------------------------------------------------------
223 SRTs (Static Reference Tables)
225 These tables are used to keep track of the static objects referred
226 to by the code for a closure or stack frame, so that we can follow
227 static data references from code and thus accurately
228 garbage-collect CAFs.
229 -------------------------------------------------------------------------- */
231 /* An SRT is just an array of closure pointers: */
232 typedef StgClosure* StgSRT[];
235 * Each info table refers to some subset of the closure pointers in an
236 * SRT. It does this using a pair of an StgSRT pointer and a
237 * half-word bitmap. If the half-word bitmap isn't large enough, then
238 * we fall back to a large SRT, including an unbounded bitmap. If the
239 * half-word bitmap is set to all ones (0xffff), then the StgSRT
240 * pointer instead points to an StgLargeSRT:
242 typedef struct StgLargeSRT_ {
247 /* ----------------------------------------------------------------------------
249 ------------------------------------------------------------------------- */
252 * Stuff describing the closure layout. Well, actually, it might
253 * contain the selector index for a THUNK_SELECTOR. This union is one
257 struct { /* Heap closure payload layout: */
258 StgHalfWord ptrs; /* number of pointers */
259 StgHalfWord nptrs; /* number of non-pointers */
262 StgWord bitmap; /* word-sized bit pattern describing */
263 /* a stack frame: see below */
265 #ifndef TABLES_NEXT_TO_CODE
266 StgLargeBitmap* large_bitmap; /* pointer to large bitmap structure */
268 OFFSET_FIELD( large_bitmap_offset ); /* offset from info table to large bitmap structure */
271 StgWord selector_offset; /* used in THUNK_SELECTORs */
277 * The "standard" part of an info table. Every info table has this bit.
279 typedef struct _StgInfoTable {
281 #ifndef TABLES_NEXT_TO_CODE
282 StgFunPtr entry; /* pointer to the entry code */
285 #if defined(PAR) || defined(GRAN)
286 struct _StgInfoTable *rbh_infoptr;
292 /* Ticky-specific stuff would go here. */
295 /* Debug-specific stuff would go here. */
298 StgClosureInfo layout; /* closure layout info (one word) */
300 StgHalfWord type; /* closure type */
301 StgHalfWord srt_bitmap; /* number of entries in SRT (or constructor tag) */
303 #ifdef TABLES_NEXT_TO_CODE
304 StgCode code[FLEXIBLE_ARRAY];
309 /* -----------------------------------------------------------------------------
312 This is the general form of function info tables. The compiler
313 will omit some of the fields in common cases:
315 - If fun_type is not ARG_GEN or ARG_GEN_BIG, then the slow_apply
316 and bitmap fields may be left out (they are at the end, so omitting
317 them doesn't affect the layout).
319 - If srt_bitmap (in the std info table part) is zero, then the srt
320 field may be omitted. This only applies if the slow_apply and
321 bitmap fields have also been omitted.
322 -------------------------------------------------------------------------- */
324 typedef struct _StgFunInfoExtraRev {
325 OFFSET_FIELD ( slow_apply_offset ); /* apply to args on the stack */
328 OFFSET_FIELD ( bitmap_offset ); /* arg ptr/nonptr bitmap */
330 OFFSET_FIELD ( srt_offset ); /* pointer to the SRT table */
331 StgHalfWord fun_type; /* function type */
332 StgHalfWord arity; /* function arity */
333 } StgFunInfoExtraRev;
335 typedef struct _StgFunInfoExtraFwd {
336 StgHalfWord fun_type; /* function type */
337 StgHalfWord arity; /* function arity */
338 StgSRT *srt; /* pointer to the SRT table */
339 union { /* union for compat. with TABLES_NEXT_TO_CODE version */
340 StgWord bitmap; /* arg ptr/nonptr bitmap */
342 StgFun *slow_apply; /* apply to args on the stack */
343 } StgFunInfoExtraFwd;
346 #if defined(TABLES_NEXT_TO_CODE)
347 StgFunInfoExtraRev f;
351 StgFunInfoExtraFwd f;
355 /* -----------------------------------------------------------------------------
357 -------------------------------------------------------------------------- */
360 * When info tables are laid out backwards, we can omit the SRT
361 * pointer iff srt_bitmap is zero.
365 #if defined(TABLES_NEXT_TO_CODE)
366 OFFSET_FIELD( srt_offset ); /* offset to the SRT table */
370 StgSRT *srt; /* pointer to the SRT table */
374 /* -----------------------------------------------------------------------------
376 -------------------------------------------------------------------------- */
379 * When info tables are laid out backwards, we can omit the SRT
380 * pointer iff srt_bitmap is zero.
383 typedef struct _StgThunkInfoTable {
384 #if !defined(TABLES_NEXT_TO_CODE)
387 #if defined(TABLES_NEXT_TO_CODE)
388 OFFSET_FIELD( srt_offset ); /* offset to the SRT table */
390 StgSRT *srt; /* pointer to the SRT table */
392 #if defined(TABLES_NEXT_TO_CODE)
397 /* -----------------------------------------------------------------------------
398 Constructor info tables
399 -------------------------------------------------------------------------- */
401 typedef struct _StgConInfoTable {
402 #if !defined(TABLES_NEXT_TO_CODE)
406 #ifndef TABLES_NEXT_TO_CODE
409 OFFSET_FIELD(con_desc) // the name of the data constructor
410 // as: Package:Module.Name
413 #if defined(TABLES_NEXT_TO_CODE)
419 /* -----------------------------------------------------------------------------
420 Accessor macros for fields that might be offsets (C version)
421 -------------------------------------------------------------------------- */
425 * info must be a Stg[Ret|Thunk]InfoTable* (an info table that has a SRT)
427 #ifdef TABLES_NEXT_TO_CODE
428 #define GET_SRT(info) ((StgSRT*) (((StgWord) ((info)+1)) + (info)->srt_offset))
430 #define GET_SRT(info) ((info)->srt)
435 * info must be a StgConInfoTable*.
437 #ifdef TABLES_NEXT_TO_CODE
438 #define GET_CON_DESC(info) ((char *)((StgWord)((info)+1) + (info->con_desc)))
440 #define GET_CON_DESC(info) ((info)->con_desc)
445 * info must be a StgFunInfoTable*
447 #ifdef TABLES_NEXT_TO_CODE
448 #define GET_FUN_SRT(info) ((StgSRT*) (((StgWord) ((info)+1)) + (info)->f.srt_offset))
450 #define GET_FUN_SRT(info) ((info)->f.srt)
453 #ifdef TABLES_NEXT_TO_CODE
454 #define GET_LARGE_BITMAP(info) ((StgLargeBitmap*) (((StgWord) ((info)+1)) \
455 + (info)->layout.large_bitmap_offset))
457 #define GET_LARGE_BITMAP(info) ((info)->layout.large_bitmap)
460 #ifdef TABLES_NEXT_TO_CODE
461 #define GET_FUN_LARGE_BITMAP(info) ((StgLargeBitmap*) (((StgWord) ((info)+1)) \
462 + (info)->f.b.bitmap_offset))
464 #define GET_FUN_LARGE_BITMAP(info) ((StgLargeBitmap*) ((info)->f.b.bitmap))
468 * GET_PROF_TYPE, GET_PROF_DESC
470 #ifdef TABLES_NEXT_TO_CODE
471 #define GET_PROF_TYPE(info) ((char *)((StgWord)((info)+1) + (info->prof.closure_type_off)))
473 #define GET_PROF_TYPE(info) ((info)->prof.closure_type)
475 #ifdef TABLES_NEXT_TO_CODE
476 #define GET_PROF_DESC(info) ((char *)((StgWord)((info)+1) + (info->prof.closure_desc_off)))
478 #define GET_PROF_DESC(info) ((info)->prof.closure_desc)
480 #endif /* INFOTABLES_H */