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