1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
7 * -------------------------------------------------------------------------- */
13 * The Layout of a closure header depends on which kind of system we're
14 * compiling for: profiling, parallel, ticky, etc.
17 /* -----------------------------------------------------------------------------
19 -------------------------------------------------------------------------- */
24 struct _RetainerSet *rs; /* Retainer Set */
25 StgWord ldvw; /* Lag/Drag/Void Word */
29 /* -----------------------------------------------------------------------------
31 -------------------------------------------------------------------------- */
34 StgWord procs; /* bitmask indicating on which PEs this closure resides */
37 /* -----------------------------------------------------------------------------
40 In SMP mode, we have an extra word of padding in a thunk's header.
41 (Note: thunks only; other closures do not have this padding word).
42 -------------------------------------------------------------------------- */
48 /* -----------------------------------------------------------------------------
49 The full fixed-size closure header
51 The size of the fixed header is the sum of the optional parts plus a single
52 word for the entry code pointer.
53 -------------------------------------------------------------------------- */
56 const struct _StgInfoTable* info;
66 * In SMP mode, a thunk has a padding word to take the updated value.
67 * This is so that the update doesn't overwrite the payload, so we can
68 * avoid needing to lock the thunk during entry and update.
70 * Note: this doesn't apply to THUNK_STATICs, which have no payload.
73 const struct _StgInfoTable* info;
81 StgSMPThunkHeader smp;
85 /* -----------------------------------------------------------------------------
88 For any given closure type (defined in InfoTables.h), there is a
89 corresponding structure defined below. The name of the structure
90 is obtained by concatenating the closure type with '_closure'
91 -------------------------------------------------------------------------- */
93 /* All closures follow the generic format */
97 struct StgClosure_ *payload[FLEXIBLE_ARRAY];
101 StgThunkHeader header;
102 struct StgClosure_ *payload[FLEXIBLE_ARRAY];
106 StgThunkHeader header;
107 StgClosure *selectee;
112 StgHalfWord arity; /* zero if it is an AP */
114 StgClosure *fun; /* really points to a fun */
115 StgClosure *payload[FLEXIBLE_ARRAY];
119 StgThunkHeader header;
120 StgHalfWord arity; /* zero if it is an AP */
122 StgClosure *fun; /* really points to a fun */
123 StgClosure *payload[FLEXIBLE_ARRAY];
127 StgThunkHeader header;
128 StgWord size; /* number of words in payload */
130 StgClosure *payload[FLEXIBLE_ARRAY]; /* contains a chunk of *stack* */
135 StgClosure *indirectee;
140 StgClosure *indirectee;
141 StgClosure *static_link;
142 struct _StgInfoTable *saved_info;
148 StgWord payload[FLEXIBLE_ARRAY];
154 StgClosure *payload[FLEXIBLE_ARRAY];
162 typedef struct _StgUpdateFrame {
169 StgInt exceptions_blocked;
185 } StgIntCharlikeClosure;
187 /* statically allocated */
192 typedef struct _StgStableName {
197 typedef struct _StgWeak { /* Weak v */
200 StgClosure *value; /* v */
201 StgClosure *finalizer;
202 struct _StgWeak *link;
205 typedef struct _StgDeadWeak { /* Weak v */
207 struct _StgWeak *link;
210 /* Byte code objects. These are fixed size objects with pointers to
211 * four arrays, designed so that a BCO can be easily "re-linked" to
212 * other BCOs, to facilitate GHC's intelligent recompilation. The
213 * array of instructions is static and not re-generated when the BCO
214 * is re-linked, but the other 3 arrays will be regenerated.
216 * A BCO represents either a function or a stack frame. In each case,
217 * it needs a bitmap to describe to the garbage collector the
218 * pointerhood of its arguments/free variables respectively, and in
219 * the case of a function it also needs an arity. These are stored
220 * directly in the BCO, rather than in the instrs array, for two
222 * (a) speed: we need to get at the bitmap info quickly when
223 * the GC is examining APs and PAPs that point to this BCO
224 * (b) a subtle interaction with the compacting GC. In compacting
225 * GC, the info that describes the size/layout of a closure
226 * cannot be in an object more than one level of indirection
227 * away from the current object, because of the order in
228 * which pointers are updated to point to their new locations.
233 StgArrWords *instrs; /* a pointer to an ArrWords */
234 StgArrWords *literals; /* a pointer to an ArrWords */
235 StgMutArrPtrs *ptrs; /* a pointer to a MutArrPtrs */
236 StgArrWords *itbls; /* a pointer to an ArrWords */
237 StgHalfWord arity; /* arity of this BCO */
238 StgHalfWord size; /* size of this BCO (in words) */
239 StgWord bitmap[FLEXIBLE_ARRAY]; /* an StgLargeBitmap */
242 #define BCO_BITMAP(bco) ((StgLargeBitmap *)((StgBCO *)(bco))->bitmap)
243 #define BCO_BITMAP_SIZE(bco) (BCO_BITMAP(bco)->size)
244 #define BCO_BITMAP_BITS(bco) (BCO_BITMAP(bco)->bitmap)
245 #define BCO_BITMAP_SIZEW(bco) ((BCO_BITMAP_SIZE(bco) + BITS_IN(StgWord) - 1) \
248 /* -----------------------------------------------------------------------------
249 Dynamic stack frames for generic heap checks.
251 These generic heap checks are slow, but have the advantage of being
252 usable in a variety of situations.
254 The one restriction is that any relevant SRTs must already be pointed
255 to from the stack. The return address doesn't need to have an info
256 table attached: hence it can be any old code pointer.
258 The liveness mask contains a 1 at bit n, if register Rn contains a
259 non-pointer. The contents of all 8 vanilla registers are always saved
260 on the stack; the liveness mask tells the GC which ones contain
263 Good places to use a generic heap check:
265 - case alternatives (the return address with an SRT is already
268 - primitives (no SRT required).
270 The stack frame layout for a RET_DYN is like this:
272 some pointers |-- RET_DYN_PTRS(liveness) words
273 some nonpointers |-- RET_DYN_NONPTRS(liveness) words
276 D1-2 |-- RET_DYN_NONPTR_REGS_SIZE words
279 R1-8 |-- RET_DYN_BITMAP_SIZE words
282 liveness mask |-- StgRetDyn structure
285 we assume that the size of a double is always 2 pointers (wasting a
286 word when it is only one pointer, but avoiding lots of #ifdefs).
288 See Liveness.h for the macros (RET_DYN_PTRS() etc.).
290 NOTE: if you change the layout of RET_DYN stack frames, then you
291 might also need to adjust the value of RESERVED_STACK_WORDS in
293 -------------------------------------------------------------------------- */
296 const struct _StgInfoTable* info;
299 StgClosure * payload[FLEXIBLE_ARRAY];
302 /* A function return stack frame: used when saving the state for a
303 * garbage collection at a function entry point. The function
304 * arguments are on the stack, and we also save the function (its
305 * info table describes the pointerhood of the arguments).
307 * The stack frame size is also cached in the frame for convenience.
310 const struct _StgInfoTable* info;
313 StgClosure * payload[FLEXIBLE_ARRAY];
316 /* Concurrent communication objects */
320 struct StgTSO_ *head;
321 struct StgTSO_ *tail;
326 /* STM data structures
328 * StgTVar defines the only type that can be updated through the STM
331 * Note that various optimisations may be possible in order to use less
332 * space for these data structures at the cost of more complexity in the
335 * - In StgTVar, current_value and first_wait_queue_entry could be held in
336 * the same field: if any thread is waiting then its expected_value for
337 * the tvar is the current value.
339 * - In StgTRecHeader, it might be worthwhile having separate chunks
340 * of read-only and read-write locations. This would save a
341 * new_value field in the read-only locations.
344 typedef struct StgTVarWaitQueue_ {
346 struct StgTSO_ *waiting_tso;
347 struct StgTVarWaitQueue_ *next_queue_entry;
348 struct StgTVarWaitQueue_ *prev_queue_entry;
353 StgClosure *volatile current_value;
354 StgTVarWaitQueue *volatile first_wait_queue_entry;
356 struct StgTRecHeader_ *volatile last_update_by;
360 /* new_value == expected_value for read-only accesses */
361 /* new_value is a StgTVarWaitQueue entry when trec in state TREC_WAITING */
364 StgClosure *expected_value;
365 StgClosure *new_value;
367 struct StgTRecHeader_ *saw_update_by;
371 #define TREC_CHUNK_NUM_ENTRIES 256
373 typedef struct StgTRecChunk_ {
375 struct StgTRecChunk_ *prev_chunk;
376 StgWord next_entry_idx;
377 TRecEntry entries[TREC_CHUNK_NUM_ENTRIES];
381 TREC_ACTIVE, /* Transaction in progress, outcome undecided */
382 TREC_CONDEMNED, /* Transaction in progress, inconsistent / out of date reads */
383 TREC_COMMITTED, /* Transaction has committed, now updating tvars */
384 TREC_ABORTED, /* Transaction has aborted, now reverting tvars */
385 TREC_WAITING, /* Transaction currently waiting */
388 typedef struct StgTRecHeader_ {
391 struct StgTRecHeader_ *enclosing_trec;
392 StgTRecChunk *current_chunk;
399 } StgAtomicallyFrame;
408 StgBool running_alt_code;
409 StgClosure *first_code;
410 StgClosure *alt_code;
411 StgTRecHeader *first_code_trec;
412 } StgCatchRetryFrame;
414 #if defined(PAR) || defined(GRAN)
416 StgBlockingQueueElement is a ``collective type'' representing the types
417 of closures that can be found on a blocking queue: StgTSO, StgRBHSave,
418 StgBlockedFetch. (StgRBHSave can only appear at the end of a blocking
419 queue). Logically, this is a union type, but defining another struct
420 with a common layout is easier to handle in the code.
421 Note that in the standard setup only StgTSOs can be on a blocking queue.
422 This is one of the main reasons for slightly different code in files
425 typedef struct StgBlockingQueueElement_ {
427 struct StgBlockingQueueElement_ *link; /* next elem in BQ */
428 struct StgClosure_ *payload[FLEXIBLE_ARRAY];/* contents of the closure */
429 } StgBlockingQueueElement;
431 /* only difference to std code is type of the elem in the BQ */
432 typedef struct StgBlockingQueue_ {
434 struct StgBlockingQueueElement_ *blocking_queue; /* start of the BQ */
437 /* this closure is hanging at the end of a blocking queue in (see RBH.c) */
438 typedef struct StgRBHSave_ {
440 StgClosure *payload[FLEXIBLE_ARRAY]; /* 2 words ripped out of the guts of the */
441 } StgRBHSave; /* closure holding the blocking queue */
443 typedef struct StgRBH_ {
445 struct StgBlockingQueueElement_ *blocking_queue; /* start of the BQ */
451 /* global indirections aka FETCH_ME closures */
452 typedef struct StgFetchMe_ {
454 globalAddr *ga; /* ptr to unique id for a closure */
457 /* same contents as an ordinary StgBlockingQueue */
458 typedef struct StgFetchMeBlockingQueue_ {
460 struct StgBlockingQueueElement_ *blocking_queue; /* start of the BQ */
461 } StgFetchMeBlockingQueue;
463 /* This is an entry in a blocking queue. It indicates a fetch request from a
464 TSO on another PE demanding the value of this closur. Note that a
465 StgBlockedFetch can only occur in a BQ. Once the node is evaluated and
466 updated with the result, the result will be sent back (the PE is encoded
467 in the globalAddr) and the StgBlockedFetch closure will be nuked.
469 typedef struct StgBlockedFetch_ {
471 struct StgBlockingQueueElement_ *link; /* next elem in the BQ */
472 StgClosure *node; /* node to fetch */
473 globalAddr ga; /* where to send the result to */
474 } StgBlockedFetch; /* NB: not just a ptr to a GA */
477 #endif /* CLOSURES_H */