FIX #1110: the linker also needs the workaround
[ghc-hetmet.git] / includes / InfoTables.h
1 /* ----------------------------------------------------------------------------
2  * 
3  * (c) The GHC Team, 1998-2002
4  *
5  * Info Tables
6  *
7  * -------------------------------------------------------------------------- */
8
9 #ifndef INFOTABLES_H
10 #define INFOTABLES_H
11
12 /* -----------------------------------------------------------------------------
13    Profiling info
14    -------------------------------------------------------------------------- */
15
16 typedef struct {
17     char *closure_type;
18     char *closure_desc;
19 } StgProfInfo;
20
21 /* -----------------------------------------------------------------------------
22    Parallelism info
23    -------------------------------------------------------------------------- */
24
25 #if 0 && (defined(PAR) || defined(GRAN))
26
27 /* CURRENTLY UNUSED
28    ToDo: use this in StgInfoTable (mutually recursive) -- HWL */
29
30 typedef struct {
31   StgInfoTable *rbh_infoptr;     /* infoptr to the RBH  */
32 } StgParInfo;
33
34 #endif /* 0 */
35
36 /*
37    Copied from ghc-0.29; ToDo: check this code -- HWL
38
39    In the parallel system, all updatable closures have corresponding
40    revertible black holes.  When we are assembly-mangling, we guarantee
41    that the revertible black hole code precedes the normal entry code, so
42    that the RBH info table resides at a fixed offset from the normal info
43    table.  Otherwise, we add the RBH info table pointer to the end of the
44    normal info table and vice versa.
45
46    Currently has to use a !RBH_MAGIC_OFFSET setting.
47    Still todo: init of par.infoptr field in all infotables!!
48 */
49
50 #if defined(PAR) || defined(GRAN)
51
52 # ifdef RBH_MAGIC_OFFSET
53
54 #  error magic offset not yet implemented
55
56 #  define RBH_INFO_WORDS    0
57 #  define INCLUDE_RBH_INFO(infoptr)
58
59 #  define RBH_INFOPTR(infoptr)      (((P_)infoptr) - RBH_MAGIC_OFFSET)
60 #  define REVERT_INFOPTR(infoptr)   (((P_)infoptr) + RBH_MAGIC_OFFSET)
61
62 # else
63
64 #  define RBH_INFO_WORDS    1
65 #  define INCLUDE_RBH_INFO(info)    rbh_infoptr : &(info)
66
67 #  define RBH_INFOPTR(infoptr)      (((StgInfoTable *)(infoptr))->rbh_infoptr)
68 #  define REVERT_INFOPTR(infoptr)   (((StgInfoTable *)(infoptr))->rbh_infoptr)
69
70 # endif
71
72 /* see ParallelRts.h */
73 /*
74 EXTFUN(RBH_entry);
75 StgClosure *convertToRBH(StgClosure *closure);
76 #if defined(GRAN)
77 void convertFromRBH(StgClosure *closure);
78 #elif defined(PAR)
79 void convertToFetchMe(StgPtr closure, globalAddr *ga);
80 #endif
81 */
82
83 #endif
84
85 /* -----------------------------------------------------------------------------
86    Ticky info
87
88    There is no ticky-specific stuff in an info table at this time.
89    -------------------------------------------------------------------------- */
90
91 /* -----------------------------------------------------------------------------
92    Debugging info
93    -------------------------------------------------------------------------- */
94
95 #ifdef DEBUG_CLOSURE
96
97 typedef struct {
98         ... whatever ...
99 } StgDebugInfo;
100
101 #else /* !DEBUG_CLOSURE */
102
103 /* There is no DEBUG-specific stuff in an info table at this time. */
104
105 #endif /* DEBUG_CLOSURE */
106
107 /* -----------------------------------------------------------------------------
108    Closure flags
109    -------------------------------------------------------------------------- */
110
111 /* The type flags provide quick access to certain properties of a closure. */
112
113 #define _HNF (1<<0)  /* head normal form?    */
114 #define _BTM (1<<1)  /* bitmap-style layout? */
115 #define _NS  (1<<2)  /* non-sparkable        */
116 #define _STA (1<<3)  /* static?              */
117 #define _THU (1<<4)  /* thunk?               */
118 #define _MUT (1<<5)  /* mutable?             */
119 #define _UPT (1<<6)  /* unpointed?           */
120 #define _SRT (1<<7)  /* has an SRT?          */
121 #define _IND (1<<8)  /* is an indirection?   */
122
123 #define isSTATIC(flags)    ((flags) &_STA)
124 #define isMUTABLE(flags)   ((flags) &_MUT)
125 #define isBITMAP(flags)    ((flags) &_BTM)
126 #define isTHUNK(flags)     ((flags) &_THU)
127 #define isUNPOINTED(flags) ((flags) &_UPT)
128 #define hasSRT(flags)      ((flags) &_SRT)
129
130 extern StgWord16 closure_flags[];
131
132 #define closureFlags(c)         (closure_flags[get_itbl(c)->type])
133
134 #define closure_HNF(c)          (  closureFlags(c) & _HNF)
135 #define closure_BITMAP(c)       (  closureFlags(c) & _BTM)
136 #define closure_NON_SPARK(c)    ( (closureFlags(c) & _NS))
137 #define closure_SHOULD_SPARK(c) (!(closureFlags(c) & _NS))
138 #define closure_STATIC(c)       (  closureFlags(c) & _STA)
139 #define closure_THUNK(c)        (  closureFlags(c) & _THU)
140 #define closure_MUTABLE(c)      (  closureFlags(c) & _MUT)
141 #define closure_UNPOINTED(c)    (  closureFlags(c) & _UPT)
142 #define closure_SRT(c)          (  closureFlags(c) & _SRT)
143 #define closure_IND(c)          (  closureFlags(c) & _IND)
144
145 /* same as above but for info-ptr rather than closure */
146 #define ipFlags(ip)             (closure_flags[ip->type])
147
148 #define ip_HNF(ip)               (  ipFlags(ip) & _HNF)
149 #define ip_BITMAP(ip)            (  ipFlags(ip) & _BTM)
150 #define ip_SHOULD_SPARK(ip)      (!(ipFlags(ip) & _NS))
151 #define ip_STATIC(ip)            (  ipFlags(ip) & _STA)
152 #define ip_THUNK(ip)             (  ipFlags(ip) & _THU)
153 #define ip_MUTABLE(ip)           (  ipFlags(ip) & _MUT)
154 #define ip_UNPOINTED(ip)         (  ipFlags(ip) & _UPT)
155 #define ip_SRT(ip)               (  ipFlags(ip) & _SRT)
156 #define ip_IND(ip)               (  ipFlags(ip) & _IND)
157
158 /* -----------------------------------------------------------------------------
159    Bitmaps
160
161    These are used to describe the pointerhood of a sequence of words
162    (usually on the stack) to the garbage collector.  The two primary
163    uses are for stack frames, and functions (where we need to describe
164    the layout of a PAP to the GC).
165
166    In these bitmaps: 0 == ptr, 1 == non-ptr.
167    -------------------------------------------------------------------------- */
168
169 /*
170  * Small bitmaps:  for a small bitmap, we store the size and bitmap in 
171  * the same word, using the following macros.  If the bitmap doesn't
172  * fit in a single word, we use a pointer to an StgLargeBitmap below.
173  */
174 #define MK_SMALL_BITMAP(size,bits) (((bits)<<BITMAP_BITS_SHIFT) | (size))
175
176 #define BITMAP_SIZE(bitmap) ((bitmap) & BITMAP_SIZE_MASK)
177 #define BITMAP_BITS(bitmap) ((bitmap) >> BITMAP_BITS_SHIFT)
178
179 /*
180  * A large bitmap.
181  */
182 typedef struct {
183   StgWord size;
184   StgWord bitmap[FLEXIBLE_ARRAY];
185 } StgLargeBitmap;
186
187 /* -----------------------------------------------------------------------------
188    SRTs  (Static Reference Tables)
189
190    These tables are used to keep track of the static objects referred
191    to by the code for a closure or stack frame, so that we can follow
192    static data references from code and thus accurately
193    garbage-collect CAFs.
194    -------------------------------------------------------------------------- */
195
196 /* An SRT is just an array of closure pointers: */
197 typedef StgClosure* StgSRT[];
198
199 /*
200  * Each info table refers to some subset of the closure pointers in an
201  * SRT.  It does this using a pair of an StgSRT pointer and a
202  * half-word bitmap.  If the half-word bitmap isn't large enough, then
203  * we fall back to a large SRT, including an unbounded bitmap.  If the
204  * half-word bitmap is set to all ones (0xffff), then the StgSRT
205  * pointer instead points to an StgLargeSRT:
206  */
207 typedef struct StgLargeSRT_ {
208     StgSRT *srt;
209     StgLargeBitmap l;
210 } StgLargeSRT;
211
212 /* ----------------------------------------------------------------------------
213    Relative pointers
214
215    Several pointer fields in info tables are expressed as offsets
216    relative to the info pointer, so that we can generate
217    position-independent code.
218
219    Note [x86-64-relative]
220    There is a complication on the x86_64 platform, where pointeres are
221    64 bits, but the tools don't support 64-bit relative relocations.
222    However, the default memory model (small) ensures that all symbols
223    have values in the lower 2Gb of the address space, so offsets all
224    fit in 32 bits.  Hence we can use 32-bit offset fields.
225
226    When going via-C, the mangler arranges that we only generate
227    relative relocations between symbols in the same segment (the text
228    segment).  The NCG, however, puts things in the right sections and
229    uses 32-bit relative offsets instead.
230
231    Somewhere between binutils-2.16.1 and binutils-2.16.91.0.6,
232    support for 64-bit PC-relative relocations was added, so maybe this
233    hackery can go away sometime.
234    ------------------------------------------------------------------------- */
235
236 #if x86_64_TARGET_ARCH
237 #define OFFSET_FIELD(n) StgHalfInt n; StgHalfWord __pad_##n;
238 #else   
239 #define OFFSET_FIELD(n) StgInt n;
240 #endif
241
242 /* ----------------------------------------------------------------------------
243    Info Tables
244    ------------------------------------------------------------------------- */
245
246 /*
247  * Stuff describing the closure layout.  Well, actually, it might
248  * contain the selector index for a THUNK_SELECTOR.  This union is one
249  * word long.
250  */
251 typedef union {
252     struct {                    /* Heap closure payload layout: */
253         StgHalfWord ptrs;       /* number of pointers */
254         StgHalfWord nptrs;      /* number of non-pointers */
255     } payload;
256     
257     StgWord bitmap;               /* word-sized bit pattern describing */
258                                   /*  a stack frame: see below */
259
260 #ifndef TABLES_NEXT_TO_CODE
261     StgLargeBitmap* large_bitmap; /* pointer to large bitmap structure */
262 #else
263     OFFSET_FIELD( large_bitmap_offset );  /* offset from info table to large bitmap structure */
264 #endif
265     
266     StgWord selector_offset;      /* used in THUNK_SELECTORs */
267
268 } StgClosureInfo;
269
270
271 /*
272  * The "standard" part of an info table.  Every info table has this bit.
273  */
274 typedef struct _StgInfoTable {
275
276 #ifndef TABLES_NEXT_TO_CODE
277     StgFunPtr       entry;      /* pointer to the entry code */
278 #endif
279
280 #if defined(PAR) || defined(GRAN)
281     struct _StgInfoTable    *rbh_infoptr;
282 #endif
283 #ifdef PROFILING
284     StgProfInfo     prof;
285 #endif
286 #ifdef TICKY
287   /* Ticky-specific stuff would go here. */
288 #endif
289 #ifdef DEBUG_CLOSURE
290   /* Debug-specific stuff would go here. */
291 #endif
292
293     StgClosureInfo  layout;     /* closure layout info (one word) */
294
295     StgHalfWord     type;       /* closure type */
296     StgHalfWord     srt_bitmap;    /* number of entries in SRT (or constructor tag) */
297
298 #ifdef TABLES_NEXT_TO_CODE
299     StgCode         code[FLEXIBLE_ARRAY];
300 #endif
301 } StgInfoTable;
302
303
304 /* -----------------------------------------------------------------------------
305    Function info tables
306
307    This is the general form of function info tables.  The compiler
308    will omit some of the fields in common cases:
309
310    -  If fun_type is not ARG_GEN or ARG_GEN_BIG, then the slow_apply
311       and bitmap fields may be left out (they are at the end, so omitting
312       them doesn't affect the layout).
313       
314    -  If srt_bitmap (in the std info table part) is zero, then the srt
315       field may be omitted.  This only applies if the slow_apply and
316       bitmap fields have also been omitted.
317    -------------------------------------------------------------------------- */
318
319 typedef struct _StgFunInfoExtraRev {
320     OFFSET_FIELD ( slow_apply_offset ); /* apply to args on the stack */
321     union { 
322         StgWord bitmap;
323         OFFSET_FIELD ( bitmap_offset ); /* arg ptr/nonptr bitmap */
324     } b;
325     OFFSET_FIELD ( srt_offset ); /* pointer to the SRT table */
326     StgHalfWord    fun_type;    /* function type */
327     StgHalfWord    arity;       /* function arity */
328 } StgFunInfoExtraRev;
329
330 typedef struct _StgFunInfoExtraFwd {
331     StgHalfWord    fun_type;    /* function type */
332     StgHalfWord    arity;       /* function arity */
333     StgSRT         *srt;        /* pointer to the SRT table */
334     union { /* union for compat. with TABLES_NEXT_TO_CODE version */
335         StgWord        bitmap;  /* arg ptr/nonptr bitmap */
336     } b;
337     StgFun         *slow_apply; /* apply to args on the stack */
338 } StgFunInfoExtraFwd;
339
340 typedef struct {
341 #if defined(TABLES_NEXT_TO_CODE)
342     StgFunInfoExtraRev f;
343     StgInfoTable i;
344 #else
345     StgInfoTable i;
346     StgFunInfoExtraFwd f;
347 #endif
348 } StgFunInfoTable;
349
350 /* -----------------------------------------------------------------------------
351    Return info tables
352    -------------------------------------------------------------------------- */
353
354 /*
355  * When info tables are laid out backwards, we can omit the SRT
356  * pointer iff srt_bitmap is zero.
357  */
358
359 typedef struct {
360 #if defined(TABLES_NEXT_TO_CODE)
361     OFFSET_FIELD( srt_offset ); /* offset to the SRT table */
362     StgInfoTable i;
363 #else
364     StgInfoTable i;
365     StgSRT      *srt;   /* pointer to the SRT table */
366 #endif
367 } StgRetInfoTable;
368
369 /* -----------------------------------------------------------------------------
370    Thunk info tables
371    -------------------------------------------------------------------------- */
372
373 /*
374  * When info tables are laid out backwards, we can omit the SRT
375  * pointer iff srt_bitmap is zero.
376  */
377
378 typedef struct _StgThunkInfoTable {
379 #if !defined(TABLES_NEXT_TO_CODE)
380     StgInfoTable i;
381 #endif
382 #if defined(TABLES_NEXT_TO_CODE)
383     OFFSET_FIELD( srt_offset ); /* offset to the SRT table */
384 #else
385     StgSRT         *srt;        /* pointer to the SRT table */
386 #endif
387 #if defined(TABLES_NEXT_TO_CODE)
388     StgInfoTable i;
389 #endif
390 } StgThunkInfoTable;
391
392 /* -----------------------------------------------------------------------------
393    Constructor info tables
394    -------------------------------------------------------------------------- */
395
396 typedef struct _StgConInfoTable {
397 #if !defined(TABLES_NEXT_TO_CODE)
398     StgInfoTable i;
399 #endif
400
401     OFFSET_FIELD(con_desc) // the name of the data constructor 
402                            // as: Package:Module.Name
403
404 #if defined(TABLES_NEXT_TO_CODE)
405     StgInfoTable i;
406 #endif
407 } StgConInfoTable;
408
409
410 /* -----------------------------------------------------------------------------
411    Accessor macros for fields that might be offsets (C version)
412    -------------------------------------------------------------------------- */
413
414 /*
415  * GET_SRT(info)
416  * info must be a Stg[Ret|Thunk]InfoTable* (an info table that has a SRT)
417  */
418 #ifdef TABLES_NEXT_TO_CODE
419 #define GET_SRT(info) ((StgSRT*) (((StgWord) ((info)+1)) + (info)->srt_offset))
420 #else
421 #define GET_SRT(info) ((info)->srt)
422 #endif
423
424 /*
425  * GET_CON_DESC(info)
426  * info must be a StgConInfoTable*.
427  */
428 #ifdef TABLES_NEXT_TO_CODE
429 #define GET_CON_DESC(info) ((char *)((StgWord)((info)+1) + (info->con_desc)))
430 #else
431 #define GET_CON_DESC(info) ((info)->con_desc)
432 #endif
433
434 /*
435  * GET_FUN_SRT(info)
436  * info must be a StgFunInfoTable*
437  */
438 #ifdef TABLES_NEXT_TO_CODE
439 #define GET_FUN_SRT(info) ((StgSRT*) (((StgWord) ((info)+1)) + (info)->f.srt_offset))
440 #else
441 #define GET_FUN_SRT(info) ((info)->f.srt)
442 #endif
443
444 #ifdef TABLES_NEXT_TO_CODE
445 #define GET_LARGE_BITMAP(info) ((StgLargeBitmap*) (((StgWord) ((info)+1)) \
446                                         + (info)->layout.large_bitmap_offset))
447 #else
448 #define GET_LARGE_BITMAP(info) ((info)->layout.large_bitmap)
449 #endif
450
451 #ifdef TABLES_NEXT_TO_CODE
452 #define GET_FUN_LARGE_BITMAP(info) ((StgLargeBitmap*) (((StgWord) ((info)+1)) \
453                                         + (info)->f.b.bitmap_offset))
454 #else
455 #define GET_FUN_LARGE_BITMAP(info) ((StgLargeBitmap*) ((info)->f.b.bitmap))
456 #endif
457
458
459 #endif /* INFOTABLES_H */