[project @ 2004-11-18 09:56:07 by tharris]
[ghc-hetmet.git] / ghc / includes / Closures.h
1 /* ----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * Closures
6  *
7  * -------------------------------------------------------------------------- */
8
9 #ifndef CLOSURES_H
10 #define CLOSURES_H
11
12 /*
13  * The Layout of a closure header depends on which kind of system we're
14  * compiling for: profiling, parallel, ticky, etc.
15  */
16
17 /* -----------------------------------------------------------------------------
18    The profiling header
19    -------------------------------------------------------------------------- */
20
21 typedef struct {
22   CostCentreStack *ccs;
23   union {
24     struct _RetainerSet *rs;  // Retainer Set
25     StgWord ldvw;             // Lag/Drag/Void Word
26   } hp;
27 } StgProfHeader;
28
29 /* -----------------------------------------------------------------------------
30    The GranSim header
31    -------------------------------------------------------------------------- */
32
33 typedef struct {
34   StgWord procs; /* bitmask indicating on which PEs this closure resides */
35 } StgGranHeader;
36
37 /* -----------------------------------------------------------------------------
38    The full fixed-size closure header
39
40    The size of the fixed header is the sum of the optional parts plus a single
41    word for the entry code pointer.
42    -------------------------------------------------------------------------- */
43
44 typedef struct {
45         const struct _StgInfoTable* info;
46 #ifdef PROFILING
47         StgProfHeader         prof;
48 #endif
49 #ifdef GRAN
50         StgGranHeader         gran;
51 #endif
52 } StgHeader;
53
54 /* -----------------------------------------------------------------------------
55    Closure Types
56
57    For any given closure type (defined in InfoTables.h), there is a
58    corresponding structure defined below.  The name of the structure
59    is obtained by concatenating the closure type with '_closure'
60    -------------------------------------------------------------------------- */
61
62 /* All closures follow the generic format */
63
64 struct StgClosure_ {
65     StgHeader   header;
66     struct StgClosure_ *payload[FLEXIBLE_ARRAY];
67 };
68
69 /* What a stroke of luck - all our mutable closures follow the same
70  * basic layout, with the mutable link field as the second field after
71  * the header.  This means the following structure is the supertype of
72  * mutable closures.
73  */
74
75 typedef struct StgMutClosure_ {
76     StgHeader   header;
77     StgWord     padding;
78     struct StgMutClosure_ *mut_link;
79     struct StgClosure_ *payload[FLEXIBLE_ARRAY];
80 } StgMutClosure;
81
82 typedef struct {
83     StgHeader   header;
84     StgClosure *selectee;
85 } StgSelector;
86
87 typedef struct {
88     StgHeader   header;
89     StgHalfWord arity;          /* zero if it is an AP */
90     StgHalfWord n_args;
91     StgClosure *fun;            /* really points to a fun */
92     StgClosure *payload[FLEXIBLE_ARRAY];
93 } StgPAP;
94
95 // AP closures have the same layout, for convenience
96 typedef StgPAP StgAP;
97
98 typedef struct {
99     StgHeader   header;
100     StgWord     size;                    // number of words in payload
101     StgClosure *fun;
102     StgClosure *payload[FLEXIBLE_ARRAY]; // contains a chunk of *stack*
103 } StgAP_STACK;
104
105 typedef struct {
106     StgHeader   header;
107     StgClosure *indirectee;
108 } StgInd;
109
110 typedef struct {
111     StgHeader   header;
112     StgClosure *indirectee;
113     StgMutClosure *mut_link;
114 } StgIndOldGen;
115
116 typedef struct {
117     StgHeader     header;
118     StgClosure   *indirectee;
119     StgClosure   *static_link;
120     struct _StgInfoTable *saved_info;
121 } StgIndStatic;
122
123 typedef struct {
124     StgHeader  header;
125     StgWord    words;
126     StgWord    payload[FLEXIBLE_ARRAY];
127 } StgArrWords;
128
129 typedef struct {
130     StgHeader   header;
131     StgWord     ptrs;
132     StgMutClosure *mut_link;    /* mutable list */
133     StgClosure *payload[FLEXIBLE_ARRAY];
134 } StgMutArrPtrs;
135
136 typedef struct {
137     StgHeader   header;
138     StgClosure *var;
139     StgMutClosure *mut_link;
140 } StgMutVar;
141
142 typedef struct _StgUpdateFrame {
143     StgHeader  header;
144     StgClosure *updatee;
145 } StgUpdateFrame;
146
147 typedef struct {
148     StgHeader  header;
149     StgInt      exceptions_blocked;
150     StgClosure *handler;
151 } StgCatchFrame;
152
153 typedef struct {
154     StgHeader  header;
155 } StgStopFrame;  
156
157 typedef struct {
158     StgHeader   header;
159     StgClosure *evacuee;
160 } StgEvacuated;
161
162 typedef struct {
163   StgHeader header;
164   StgWord data;
165 } StgIntCharlikeClosure;
166
167 /* statically allocated */
168 typedef struct {
169   StgHeader  header;
170 } StgRetry;
171
172 typedef struct _StgForeignObj {
173   StgHeader      header;
174   StgAddr        data;          /* pointer to data in non-haskell-land */
175 } StgForeignObj;
176   
177 typedef struct _StgStableName {
178   StgHeader      header;
179   StgWord        sn;
180 } StgStableName;
181
182 typedef struct _StgWeak {       /* Weak v */
183   StgHeader header;
184   StgClosure *key;
185   StgClosure *value;            /* v */
186   StgClosure *finalizer;
187   struct _StgWeak *link;
188 } StgWeak;
189
190 typedef struct _StgDeadWeak {   /* Weak v */
191   StgHeader header;
192   struct _StgWeak *link;
193 } StgDeadWeak;
194
195 /* Byte code objects.  These are fixed size objects with pointers to
196  * four arrays, designed so that a BCO can be easily "re-linked" to
197  * other BCOs, to facilitate GHC's intelligent recompilation.  The
198  * array of instructions is static and not re-generated when the BCO
199  * is re-linked, but the other 3 arrays will be regenerated.
200  *
201  * A BCO represents either a function or a stack frame.  In each case,
202  * it needs a bitmap to describe to the garbage collector the
203  * pointerhood of its arguments/free variables respectively, and in
204  * the case of a function it also needs an arity.  These are stored
205  * directly in the BCO, rather than in the instrs array, for two
206  * reasons:
207  * (a) speed: we need to get at the bitmap info quickly when
208  *     the GC is examining APs and PAPs that point to this BCO
209  * (b) a subtle interaction with the compacting GC.  In compacting
210  *     GC, the info that describes the size/layout of a closure
211  *     cannot be in an object more than one level of indirection
212  *     away from the current object, because of the order in
213  *     which pointers are updated to point to their new locations.
214  */
215
216 typedef struct {
217     StgHeader      header;
218     StgArrWords   *instrs;      // a pointer to an ArrWords
219     StgArrWords   *literals;    // a pointer to an ArrWords
220     StgMutArrPtrs *ptrs;        // a pointer to a  MutArrPtrs
221     StgArrWords   *itbls;       // a pointer to an ArrWords
222     StgHalfWord   arity;        // arity of this BCO
223     StgHalfWord   size;         // size of this BCO (in words)
224     StgWord       bitmap[FLEXIBLE_ARRAY];  // an StgLargeBitmap
225 } StgBCO;
226
227 #define BCO_BITMAP(bco)      ((StgLargeBitmap *)((StgBCO *)(bco))->bitmap)
228 #define BCO_BITMAP_SIZE(bco) (BCO_BITMAP(bco)->size)
229 #define BCO_BITMAP_BITS(bco) (BCO_BITMAP(bco)->bitmap)
230 #define BCO_BITMAP_SIZEW(bco) ((BCO_BITMAP_SIZE(bco) + BITS_IN(StgWord) - 1) \
231                                 / BITS_IN(StgWord))
232
233 /* -----------------------------------------------------------------------------
234    Dynamic stack frames for generic heap checks.
235
236    These generic heap checks are slow, but have the advantage of being
237    usable in a variety of situations.
238
239    The one restriction is that any relevant SRTs must already be pointed
240    to from the stack.  The return address doesn't need to have an info
241    table attached: hence it can be any old code pointer.
242
243    The liveness mask contains a 1 at bit n, if register Rn contains a
244    non-pointer.  The contents of all 8 vanilla registers are always saved
245    on the stack; the liveness mask tells the GC which ones contain
246    pointers.
247
248    Good places to use a generic heap check: 
249
250         - case alternatives (the return address with an SRT is already
251           on the stack).
252
253         - primitives (no SRT required).
254
255    The stack frame layout for a RET_DYN is like this:
256
257           some pointers         |-- RET_DYN_PTRS(liveness) words
258           some nonpointers      |-- RET_DYN_NONPTRS(liveness) words
259                                
260           L1                    \
261           D1-2                  |-- RET_DYN_NONPTR_REGS_SIZE words
262           F1-4                  /
263                                
264           R1-8                  |-- RET_DYN_BITMAP_SIZE words
265                                
266           return address        \
267           liveness mask         |-- StgRetDyn structure
268           stg_gen_chk_info      /
269
270    we assume that the size of a double is always 2 pointers (wasting a
271    word when it is only one pointer, but avoiding lots of #ifdefs).
272
273    See Liveness.h for the macros (RET_DYN_PTRS() etc.).
274
275    NOTE: if you change the layout of RET_DYN stack frames, then you
276    might also need to adjust the value of RESERVED_STACK_WORDS in
277    Constants.h.
278    -------------------------------------------------------------------------- */
279
280 typedef struct {
281     const struct _StgInfoTable* info;
282     StgWord        liveness;
283     StgWord        ret_addr;
284     StgClosure *   payload[FLEXIBLE_ARRAY];
285 } StgRetDyn;
286
287 /* A function return stack frame: used when saving the state for a
288  * garbage collection at a function entry point.  The function
289  * arguments are on the stack, and we also save the function (its
290  * info table describes the pointerhood of the arguments).
291  *
292  * The stack frame size is also cached in the frame for convenience.
293  */
294 typedef struct {
295     const struct _StgInfoTable* info;
296     StgWord        size;
297     StgClosure *   fun;
298     StgClosure *   payload[FLEXIBLE_ARRAY];
299 } StgRetFun;
300
301 /* Concurrent communication objects */
302
303 typedef struct {
304   StgHeader       header;
305   struct StgTSO_ *head;
306   StgMutClosure  *mut_link;
307   struct StgTSO_ *tail;
308   StgClosure*     value;
309 } StgMVar;
310
311 /* STM data structures
312  *
313  *  StgTVar defines the only type that can be updated through the STM
314  *  interface.
315  * 
316  *  Note that various optimisations may be possible in order to use less
317  *  space for these data structures at the cost of more complexity in the
318  *  implementation:
319  *
320  *   - In StgTVar, current_value and first_wait_queue_entry could be held in
321  *     the same field: if any thread is waiting then its expected_value for
322  *     the tvar is the current value.  
323  *
324  *   - In StgTRecHeader, it might be worthwhile having separate chunks
325  *     of read-only and read-write locations.  This would save a
326  *     new_value field in the read-only locations.
327  */
328
329 typedef struct StgTVarWaitQueue_ {
330   StgHeader                  header;
331   struct StgTSO_            *waiting_tso;
332   StgMutClosure             *mut_link;
333   struct StgTVarWaitQueue_  *next_queue_entry;
334   struct StgTVarWaitQueue_  *prev_queue_entry;
335 } StgTVarWaitQueue;
336
337 typedef struct {
338   StgHeader                  header;
339   StgClosure                *current_value;
340   StgMutClosure             *mut_link;
341   StgTVarWaitQueue          *first_wait_queue_entry;
342 } StgTVar;
343
344 // new_value == expected_value for read-only accesses
345 // new_value is a StgTVarWaitQueue entry when trec in state TREC_WAITING
346 typedef struct {
347   StgTVar                   *tvar;
348   StgClosure                *expected_value;
349   StgClosure                *new_value; 
350 } TRecEntry;
351
352 #define TREC_CHUNK_NUM_ENTRIES 256
353
354 typedef struct StgTRecChunk_ {
355   StgHeader                  header;
356   struct StgTRecChunk_      *prev_chunk;
357   StgMutClosure             *mut_link;
358   StgWord                    next_entry_idx;
359   TRecEntry                  entries[TREC_CHUNK_NUM_ENTRIES];
360 } StgTRecChunk;
361
362 typedef enum { 
363   TREC_ACTIVE,        // Transaction in progress, outcome undecided
364   TREC_CANNOT_COMMIT, // Transaction in progress, inconsistent writes performed
365   TREC_MUST_ABORT,    // Transaction in progress, inconsistent / out of date reads
366   TREC_COMMITTED,     // Transaction has committed, now updating tvars
367   TREC_ABORTED,       // Transaction has aborted, now reverting tvars
368   TREC_WAITING,       // Transaction currently waiting
369 } TRecState;
370
371 typedef struct StgTRecHeader_ {
372   StgHeader                  header;
373   TRecState                  state;
374   StgMutClosure             *mut_link;
375   struct StgTRecHeader_     *enclosing_trec;
376   StgTRecChunk              *current_chunk;
377 } StgTRecHeader;
378
379 typedef struct {
380     StgHeader   header;
381     StgBool     waiting;
382     StgClosure *code;
383 } StgAtomicallyFrame;
384
385 typedef struct {
386     StgHeader   header;
387     StgClosure *handler;
388 } StgCatchSTMFrame;
389
390 typedef struct {
391     StgHeader      header;
392     StgBool        running_alt_code;
393     StgClosure    *first_code;
394     StgClosure    *alt_code;
395     StgTRecHeader *first_code_trec;
396 } StgCatchRetryFrame;
397
398 #if defined(PAR) || defined(GRAN)
399 /*
400   StgBlockingQueueElement is a ``collective type'' representing the types
401   of closures that can be found on a blocking queue: StgTSO, StgRBHSave,
402   StgBlockedFetch.  (StgRBHSave can only appear at the end of a blocking
403   queue).  Logically, this is a union type, but defining another struct
404   with a common layout is easier to handle in the code (same as for
405   StgMutClosures).  
406   Note that in the standard setup only StgTSOs can be on a blocking queue.
407   This is one of the main reasons for slightly different code in files
408   such as Schedule.c.
409 */
410 typedef struct StgBlockingQueueElement_ {
411   StgHeader                         header;
412   struct StgBlockingQueueElement_  *link;      /* next elem in BQ */
413   StgMutClosure                    *mut_link;  /* next elem in mutable list */
414   struct StgClosure_               *payload[FLEXIBLE_ARRAY];/* contents of the closure */
415 } StgBlockingQueueElement;
416
417 /* only difference to std code is type of the elem in the BQ */
418 typedef struct StgBlockingQueue_ {
419   StgHeader                 header;
420   struct StgBlockingQueueElement_ *blocking_queue; /* start of the BQ */
421   StgMutClosure            *mut_link;              /* next elem in mutable list */
422 } StgBlockingQueue;
423
424 /* this closure is hanging at the end of a blocking queue in (see RBH.c) */
425 typedef struct StgRBHSave_ {
426   StgHeader    header;
427   StgClosure  *payload[FLEXIBLE_ARRAY];     /* 2 words ripped out of the guts of the */
428 } StgRBHSave;                  /*  closure holding the blocking queue */
429  
430 typedef struct StgRBH_ {
431   StgHeader                         header;
432   struct StgBlockingQueueElement_  *blocking_queue; /* start of the BQ */
433   StgMutClosure                    *mut_link;       /* next elem in mutable list */
434 } StgRBH;
435
436 #else
437
438 typedef struct StgBlockingQueue_ {
439   StgHeader          header;
440   struct StgTSO_    *blocking_queue;
441   StgMutClosure     *mut_link;
442 } StgBlockingQueue;
443
444 #endif
445
446 #if defined(PAR)
447 /* global indirections aka FETCH_ME closures */
448 typedef struct StgFetchMe_ {
449   StgHeader              header;
450   globalAddr            *ga;        /* ptr to unique id for a closure */
451   StgMutClosure         *mut_link;  /* next elem in mutable list */
452 } StgFetchMe;
453
454 /* same contents as an ordinary StgBlockingQueue */
455 typedef struct StgFetchMeBlockingQueue_ {
456   StgHeader                          header;
457   struct StgBlockingQueueElement_   *blocking_queue; /* start of the BQ */
458   StgMutClosure                     *mut_link;       /* next elem in mutable list */
459 } StgFetchMeBlockingQueue;
460
461 /* This is an entry in a blocking queue. It indicates a fetch request from a 
462    TSO on another PE demanding the value of this closur. Note that a
463    StgBlockedFetch can only occur in a BQ. Once the node is evaluated and
464    updated with the result, the result will be sent back (the PE is encoded
465    in the globalAddr) and the StgBlockedFetch closure will be nuked.
466 */
467 typedef struct StgBlockedFetch_ {
468   StgHeader                         header;
469   struct StgBlockingQueueElement_  *link;     /* next elem in the BQ */
470   StgMutClosure                    *mut_link; /* next elem in mutable list */
471   StgClosure                       *node;     /* node to fetch */
472   globalAddr                        ga;       /* where to send the result to */
473 } StgBlockedFetch;                            /* NB: not just a ptr to a GA */
474 #endif
475
476 #endif /* CLOSURES_H */