Mark/compact: use a dynamically-sized mark stack, and don't do linear scan
[ghc-hetmet.git] / rts / sm / Evac.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2008
4  *
5  * Generational garbage collector: evacuation functions
6  *
7  * Documentation on the architecture of the Garbage Collector can be
8  * found in the online commentary:
9  * 
10  *   http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11  *
12  * ---------------------------------------------------------------------------*/
13
14 #include "PosixSource.h"
15 #include "Rts.h"
16
17 #include "Evac.h"
18 #include "Storage.h"
19 #include "GC.h"
20 #include "GCThread.h"
21 #include "GCUtils.h"
22 #include "Compact.h"
23 #include "MarkStack.h"
24 #include "Prelude.h"
25 #include "Trace.h"
26 #include "LdvProfile.h"
27
28 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
29 StgWord64 whitehole_spin = 0;
30 #endif
31
32 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
33 #define evacuate(p) evacuate1(p)
34 #define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
35 #endif
36
37 #if !defined(PARALLEL_GC)
38 #define copy_tag_nolock(p, info, src, size, stp, tag) \
39         copy_tag(p, info, src, size, stp, tag)
40 #endif
41
42 /* Used to avoid long recursion due to selector thunks
43  */
44 #define MAX_THUNK_SELECTOR_DEPTH 16
45
46 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
47 STATIC_INLINE void evacuate_large(StgPtr p);
48
49 /* -----------------------------------------------------------------------------
50    Allocate some space in which to copy an object.
51    -------------------------------------------------------------------------- */
52
53 STATIC_INLINE StgPtr
54 alloc_for_copy (nat size, step *stp)
55 {
56     StgPtr to;
57     step_workspace *ws;
58
59     /* Find out where we're going, using the handy "to" pointer in 
60      * the step of the source object.  If it turns out we need to
61      * evacuate to an older generation, adjust it here (see comment
62      * by evacuate()).
63      */
64     if (stp < gct->evac_step) {
65         if (gct->eager_promotion) {
66             stp = gct->evac_step;
67         } else {
68             gct->failed_to_evac = rtsTrue;
69         }
70     }
71     
72     ws = &gct->steps[stp->abs_no];
73     // this compiles to a single mem access to stp->abs_no only
74     
75     /* chain a new block onto the to-space for the destination step if
76      * necessary.
77      */
78     to = ws->todo_free;
79     ws->todo_free += size;
80     if (ws->todo_free > ws->todo_lim) {
81         to = todo_block_full(size, ws);
82     }
83     ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
84
85     return to;
86 }
87
88 /* -----------------------------------------------------------------------------
89    The evacuate() code
90    -------------------------------------------------------------------------- */
91
92 STATIC_INLINE GNUC_ATTR_HOT void
93 copy_tag(StgClosure **p, const StgInfoTable *info, 
94          StgClosure *src, nat size, step *stp, StgWord tag)
95 {
96     StgPtr to, from;
97     nat i;
98
99     to = alloc_for_copy(size,stp);
100     
101     from = (StgPtr)src;
102     to[0] = (W_)info;
103     for (i = 1; i < size; i++) { // unroll for small i
104         to[i] = from[i];
105     }
106
107 //  if (to+size+2 < bd->start + BLOCK_SIZE_W) {
108 //      __builtin_prefetch(to + size + 2, 1);
109 //  }
110
111 #if defined(PARALLEL_GC)
112     {
113         const StgInfoTable *new_info;
114         new_info = (const StgInfoTable *)cas((StgPtr)&src->header.info, (W_)info, MK_FORWARDING_PTR(to));
115         if (new_info != info) {
116             return evacuate(p); // does the failed_to_evac stuff
117         } else {
118             *p = TAG_CLOSURE(tag,(StgClosure*)to);
119         }
120     }
121 #else
122     src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
123     *p = TAG_CLOSURE(tag,(StgClosure*)to);
124 #endif
125
126 #ifdef PROFILING
127     // We store the size of the just evacuated object in the LDV word so that
128     // the profiler can guess the position of the next object later.
129     SET_EVACUAEE_FOR_LDV(from, size);
130 #endif
131 }
132
133 #if defined(PARALLEL_GC)
134 STATIC_INLINE void
135 copy_tag_nolock(StgClosure **p, const StgInfoTable *info, 
136          StgClosure *src, nat size, step *stp, StgWord tag)
137 {
138     StgPtr to, from;
139     nat i;
140
141     to = alloc_for_copy(size,stp);
142     *p = TAG_CLOSURE(tag,(StgClosure*)to);
143     src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
144     
145     from = (StgPtr)src;
146     to[0] = (W_)info;
147     for (i = 1; i < size; i++) { // unroll for small i
148         to[i] = from[i];
149     }
150
151 //  if (to+size+2 < bd->start + BLOCK_SIZE_W) {
152 //      __builtin_prefetch(to + size + 2, 1);
153 //  }
154
155 #ifdef PROFILING
156     // We store the size of the just evacuated object in the LDV word so that
157     // the profiler can guess the position of the next object later.
158     SET_EVACUAEE_FOR_LDV(from, size);
159 #endif
160 }
161 #endif
162
163 /* Special version of copy() for when we only want to copy the info
164  * pointer of an object, but reserve some padding after it.  This is
165  * used to optimise evacuation of BLACKHOLEs.
166  */
167 static rtsBool
168 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
169 {
170     StgPtr to, from;
171     nat i;
172     StgWord info;
173     
174 #if defined(PARALLEL_GC)
175 spin:
176         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
177         if (info == (W_)&stg_WHITEHOLE_info) {
178 #ifdef PROF_SPIN
179             whitehole_spin++;
180 #endif
181             goto spin;
182         }
183     if (IS_FORWARDING_PTR(info)) {
184         src->header.info = (const StgInfoTable *)info;
185         evacuate(p); // does the failed_to_evac stuff
186         return rtsFalse;
187     }
188 #else
189     info = (W_)src->header.info;
190 #endif
191
192     to = alloc_for_copy(size_to_reserve, stp);
193     *p = (StgClosure *)to;
194
195     from = (StgPtr)src;
196     to[0] = info;
197     for (i = 1; i < size_to_copy; i++) { // unroll for small i
198         to[i] = from[i];
199     }
200     
201 #if defined(PARALLEL_GC)
202     write_barrier();
203 #endif
204     src->header.info = (const StgInfoTable*)MK_FORWARDING_PTR(to);
205     
206 #ifdef PROFILING
207     // We store the size of the just evacuated object in the LDV word so that
208     // the profiler can guess the position of the next object later.
209     SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
210     // fill the slop
211     if (size_to_reserve - size_to_copy > 0)
212         LDV_FILL_SLOP(to + size_to_copy, (int)(size_to_reserve - size_to_copy));
213 #endif
214
215     return rtsTrue;
216 }
217
218
219 /* Copy wrappers that don't tag the closure after copying */
220 STATIC_INLINE GNUC_ATTR_HOT void
221 copy(StgClosure **p, const StgInfoTable *info, 
222      StgClosure *src, nat size, step *stp)
223 {
224     copy_tag(p,info,src,size,stp,0);
225 }
226
227 /* -----------------------------------------------------------------------------
228    Evacuate a large object
229
230    This just consists of removing the object from the (doubly-linked)
231    step->large_objects list, and linking it on to the (singly-linked)
232    step->new_large_objects list, from where it will be scavenged later.
233
234    Convention: bd->flags has BF_EVACUATED set for a large object
235    that has been evacuated, or unset otherwise.
236    -------------------------------------------------------------------------- */
237
238 STATIC_INLINE void
239 evacuate_large(StgPtr p)
240 {
241   bdescr *bd = Bdescr(p);
242   step *stp, *new_stp;
243   step_workspace *ws;
244     
245   stp = bd->step;
246   ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
247
248   // already evacuated? 
249   if (bd->flags & BF_EVACUATED) { 
250     /* Don't forget to set the gct->failed_to_evac flag if we didn't get
251      * the desired destination (see comments in evacuate()).
252      */
253     if (stp < gct->evac_step) {
254         gct->failed_to_evac = rtsTrue;
255         TICK_GC_FAILED_PROMOTION();
256     }
257     RELEASE_SPIN_LOCK(&stp->sync_large_objects);
258     return;
259   }
260
261   // remove from large_object list 
262   if (bd->u.back) {
263     bd->u.back->link = bd->link;
264   } else { // first object in the list 
265     stp->large_objects = bd->link;
266   }
267   if (bd->link) {
268     bd->link->u.back = bd->u.back;
269   }
270   
271   /* link it on to the evacuated large object list of the destination step
272    */
273   new_stp = stp->to;
274   if (new_stp < gct->evac_step) {
275       if (gct->eager_promotion) {
276           new_stp = gct->evac_step;
277       } else {
278           gct->failed_to_evac = rtsTrue;
279       }
280   }
281
282   ws = &gct->steps[new_stp->abs_no];
283
284   bd->flags |= BF_EVACUATED;
285   bd->step = new_stp;
286   bd->gen_no = new_stp->gen_no;
287
288   // If this is a block of pinned objects, we don't have to scan
289   // these objects, because they aren't allowed to contain any
290   // pointers.  For these blocks, we skip the scavenge stage and put
291   // them straight on the scavenged_large_objects list.
292   if (bd->flags & BF_PINNED) {
293       ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
294       if (new_stp != stp) { ACQUIRE_SPIN_LOCK(&new_stp->sync_large_objects); }
295       dbl_link_onto(bd, &new_stp->scavenged_large_objects);
296       new_stp->n_scavenged_large_blocks += bd->blocks;
297       if (new_stp != stp) { RELEASE_SPIN_LOCK(&new_stp->sync_large_objects); }
298   } else {
299       bd->link = ws->todo_large_objects;
300       ws->todo_large_objects = bd;
301   }
302
303   RELEASE_SPIN_LOCK(&stp->sync_large_objects);
304 }
305
306 /* ----------------------------------------------------------------------------
307    Evacuate
308
309    This is called (eventually) for every live object in the system.
310
311    The caller to evacuate specifies a desired generation in the
312    gct->evac_step thread-local variable.  The following conditions apply to
313    evacuating an object which resides in generation M when we're
314    collecting up to generation N
315
316    if  M >= gct->evac_step 
317            if  M > N     do nothing
318            else          evac to step->to
319
320    if  M < gct->evac_step      evac to gct->evac_step, step 0
321
322    if the object is already evacuated, then we check which generation
323    it now resides in.
324
325    if  M >= gct->evac_step     do nothing
326    if  M <  gct->evac_step     set gct->failed_to_evac flag to indicate that we
327                          didn't manage to evacuate this object into gct->evac_step.
328
329
330    OPTIMISATION NOTES:
331
332    evacuate() is the single most important function performance-wise
333    in the GC.  Various things have been tried to speed it up, but as
334    far as I can tell the code generated by gcc 3.2 with -O2 is about
335    as good as it's going to get.  We pass the argument to evacuate()
336    in a register using the 'regparm' attribute (see the prototype for
337    evacuate() near the top of this file).
338
339    Changing evacuate() to take an (StgClosure **) rather than
340    returning the new pointer seems attractive, because we can avoid
341    writing back the pointer when it hasn't changed (eg. for a static
342    object, or an object in a generation > N).  However, I tried it and
343    it doesn't help.  One reason is that the (StgClosure **) pointer
344    gets spilled to the stack inside evacuate(), resulting in far more
345    extra reads/writes than we save.
346    ------------------------------------------------------------------------- */
347
348 REGPARM1 GNUC_ATTR_HOT void 
349 evacuate(StgClosure **p)
350 {
351   bdescr *bd = NULL;
352   step *stp;
353   StgClosure *q;
354   const StgInfoTable *info;
355   StgWord tag;
356
357   q = *p;
358
359 loop:
360   /* The tag and the pointer are split, to be merged after evacing */
361   tag = GET_CLOSURE_TAG(q);
362   q = UNTAG_CLOSURE(q);
363
364   ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
365
366   if (!HEAP_ALLOCED_GC(q)) {
367
368       if (!major_gc) return;
369
370       info = get_itbl(q);
371       switch (info->type) {
372
373       case THUNK_STATIC:
374           if (info->srt_bitmap != 0) {
375               if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
376 #ifndef THREADED_RTS
377                   *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
378                   gct->static_objects = (StgClosure *)q;
379 #else
380                   StgPtr link;
381                   link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
382                                      (StgWord)NULL,
383                                      (StgWord)gct->static_objects);
384                   if (link == NULL) {
385                       gct->static_objects = (StgClosure *)q;
386                   }
387 #endif
388               }
389           }
390           return;
391
392       case FUN_STATIC:
393           if (info->srt_bitmap != 0 &&
394               *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
395 #ifndef THREADED_RTS
396               *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
397               gct->static_objects = (StgClosure *)q;
398 #else
399               StgPtr link;
400               link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
401                                  (StgWord)NULL,
402                                  (StgWord)gct->static_objects);
403               if (link == NULL) {
404                   gct->static_objects = (StgClosure *)q;
405               }
406 #endif
407           }
408           return;
409           
410       case IND_STATIC:
411           /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
412            * on the CAF list, so don't do anything with it here (we'll
413            * scavenge it later).
414            */
415           if (((StgIndStatic *)q)->saved_info == NULL) {
416               if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
417 #ifndef THREADED_RTS
418                   *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
419                   gct->static_objects = (StgClosure *)q;
420 #else
421                   StgPtr link;
422                   link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
423                                      (StgWord)NULL,
424                                      (StgWord)gct->static_objects);
425                   if (link == NULL) {
426                       gct->static_objects = (StgClosure *)q;
427                   }
428 #endif
429               }
430           }
431           return;
432           
433       case CONSTR_STATIC:
434           if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
435 #ifndef THREADED_RTS
436               *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
437               gct->static_objects = (StgClosure *)q;
438 #else
439               StgPtr link;
440               link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
441                                  (StgWord)NULL,
442                                  (StgWord)gct->static_objects);
443               if (link == NULL) {
444                   gct->static_objects = (StgClosure *)q;
445               }
446 #endif
447           }
448           /* I am assuming that static_objects pointers are not
449            * written to other objects, and thus, no need to retag. */
450           return;
451           
452       case CONSTR_NOCAF_STATIC:
453           /* no need to put these on the static linked list, they don't need
454            * to be scavenged.
455            */
456           return;
457           
458       default:
459           barf("evacuate(static): strange closure type %d", (int)(info->type));
460       }
461   }
462
463   bd = Bdescr((P_)q);
464
465   if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
466
467       // pointer into to-space: just return it.  It might be a pointer
468       // into a generation that we aren't collecting (> N), or it
469       // might just be a pointer into to-space.  The latter doesn't
470       // happen often, but allowing it makes certain things a bit
471       // easier; e.g. scavenging an object is idempotent, so it's OK to
472       // have an object on the mutable list multiple times.
473       if (bd->flags & BF_EVACUATED) {
474           // We aren't copying this object, so we have to check
475           // whether it is already in the target generation.  (this is
476           // the write barrier).
477           if (bd->step < gct->evac_step) {
478               gct->failed_to_evac = rtsTrue;
479               TICK_GC_FAILED_PROMOTION();
480           }
481           return;
482       }
483
484       /* evacuate large objects by re-linking them onto a different list.
485        */
486       if (bd->flags & BF_LARGE) {
487           info = get_itbl(q);
488           if (info->type == TSO && 
489               ((StgTSO *)q)->what_next == ThreadRelocated) {
490               q = (StgClosure *)((StgTSO *)q)->_link;
491               *p = q;
492               goto loop;
493           }
494           evacuate_large((P_)q);
495           return;
496       }
497       
498       /* If the object is in a step that we're compacting, then we
499        * need to use an alternative evacuate procedure.
500        */
501       if (!is_marked((P_)q,bd)) {
502           mark((P_)q,bd);
503           push_mark_stack((P_)q);
504       }
505       return;
506   }
507       
508   stp = bd->step->to;
509
510   info = q->header.info;
511   if (IS_FORWARDING_PTR(info))
512   {
513     /* Already evacuated, just return the forwarding address.
514      * HOWEVER: if the requested destination generation (gct->evac_step) is
515      * older than the actual generation (because the object was
516      * already evacuated to a younger generation) then we have to
517      * set the gct->failed_to_evac flag to indicate that we couldn't 
518      * manage to promote the object to the desired generation.
519      */
520     /* 
521      * Optimisation: the check is fairly expensive, but we can often
522      * shortcut it if either the required generation is 0, or the
523      * current object (the EVACUATED) is in a high enough generation.
524      * We know that an EVACUATED always points to an object in the
525      * same or an older generation.  stp is the lowest step that the
526      * current object would be evacuated to, so we only do the full
527      * check if stp is too low.
528      */
529       StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
530       *p = TAG_CLOSURE(tag,e);
531       if (stp < gct->evac_step) {  // optimisation 
532           if (Bdescr((P_)e)->step < gct->evac_step) {
533               gct->failed_to_evac = rtsTrue;
534               TICK_GC_FAILED_PROMOTION();
535           }
536       }
537       return;
538   }
539
540   switch (INFO_PTR_TO_STRUCT(info)->type) {
541
542   case WHITEHOLE:
543       goto loop;
544
545   case MUT_VAR_CLEAN:
546   case MUT_VAR_DIRTY:
547   case MVAR_CLEAN:
548   case MVAR_DIRTY:
549       copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
550       return;
551
552   case CONSTR_0_1:
553   { 
554       StgWord w = (StgWord)q->payload[0];
555       if (info == Czh_con_info &&
556           // unsigned, so always true:  (StgChar)w >= MIN_CHARLIKE &&  
557           (StgChar)w <= MAX_CHARLIKE) {
558           *p =  TAG_CLOSURE(tag,
559                             (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
560                            );
561       }
562       else if (info == Izh_con_info &&
563           (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
564           *p = TAG_CLOSURE(tag,
565                              (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
566                              );
567       }
568       else {
569           copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
570       }
571       return;
572   }
573
574   case FUN_0_1:
575   case FUN_1_0:
576   case CONSTR_1_0:
577       copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
578       return;
579
580   case THUNK_1_0:
581   case THUNK_0_1:
582       copy(p,info,q,sizeofW(StgThunk)+1,stp);
583       return;
584
585   case THUNK_1_1:
586   case THUNK_2_0:
587   case THUNK_0_2:
588 #ifdef NO_PROMOTE_THUNKS
589     if (bd->gen_no == 0 && 
590         bd->step->no != 0 &&
591         bd->step->no == generations[bd->gen_no].n_steps-1) {
592       stp = bd->step;
593     }
594 #endif
595     copy(p,info,q,sizeofW(StgThunk)+2,stp);
596     return;
597
598   case FUN_1_1:
599   case FUN_2_0:
600   case FUN_0_2:
601   case CONSTR_1_1:
602   case CONSTR_2_0:
603       copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
604       return;
605
606   case CONSTR_0_2:
607       copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
608       return;
609
610   case THUNK:
611       copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
612       return;
613
614   case FUN:
615   case IND_PERM:
616   case IND_OLDGEN_PERM:
617   case CONSTR:
618       copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
619       return;
620
621   case WEAK:
622   case STABLE_NAME:
623       copy_tag(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
624       return;
625
626   case BCO:
627       copy(p,info,q,bco_sizeW((StgBCO *)q),stp);
628       return;
629
630   case CAF_BLACKHOLE:
631   case BLACKHOLE:
632       copyPart(p,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
633       return;
634
635   case THUNK_SELECTOR:
636       eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
637       return;
638
639   case IND:
640   case IND_OLDGEN:
641     // follow chains of indirections, don't evacuate them 
642     q = ((StgInd*)q)->indirectee;
643     *p = q;
644     goto loop;
645
646   case RET_BCO:
647   case RET_SMALL:
648   case RET_BIG:
649   case RET_DYN:
650   case UPDATE_FRAME:
651   case STOP_FRAME:
652   case CATCH_FRAME:
653   case CATCH_STM_FRAME:
654   case CATCH_RETRY_FRAME:
655   case ATOMICALLY_FRAME:
656     // shouldn't see these 
657     barf("evacuate: stack frame at %p\n", q);
658
659   case PAP:
660       copy(p,info,q,pap_sizeW((StgPAP*)q),stp);
661       return;
662
663   case AP:
664       copy(p,info,q,ap_sizeW((StgAP*)q),stp);
665       return;
666
667   case AP_STACK:
668       copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
669       return;
670
671   case ARR_WORDS:
672       // just copy the block 
673       copy(p,info,q,arr_words_sizeW((StgArrWords *)q),stp);
674       return;
675
676   case MUT_ARR_PTRS_CLEAN:
677   case MUT_ARR_PTRS_DIRTY:
678   case MUT_ARR_PTRS_FROZEN:
679   case MUT_ARR_PTRS_FROZEN0:
680       // just copy the block 
681       copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
682       return;
683
684   case TSO:
685     {
686       StgTSO *tso = (StgTSO *)q;
687
688       /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
689        */
690       if (tso->what_next == ThreadRelocated) {
691         q = (StgClosure *)tso->_link;
692         *p = q;
693         goto loop;
694       }
695
696       /* To evacuate a small TSO, we need to adjust the stack pointer
697        */
698       {
699           StgTSO *new_tso;
700           StgPtr r, s;
701           rtsBool mine;
702
703           mine = copyPart(p,(StgClosure *)tso, tso_sizeW(tso), 
704                           sizeofW(StgTSO), stp);
705           if (mine) {
706               new_tso = (StgTSO *)*p;
707               move_TSO(tso, new_tso);
708               for (r = tso->sp, s = new_tso->sp;
709                    r < tso->stack+tso->stack_size;) {
710                   *s++ = *r++;
711               }
712           }
713           return;
714       }
715     }
716
717   case TREC_HEADER: 
718       copy(p,info,q,sizeofW(StgTRecHeader),stp);
719       return;
720
721   case TVAR_WATCH_QUEUE:
722       copy(p,info,q,sizeofW(StgTVarWatchQueue),stp);
723       return;
724
725   case TVAR:
726       copy(p,info,q,sizeofW(StgTVar),stp);
727       return;
728     
729   case TREC_CHUNK:
730       copy(p,info,q,sizeofW(StgTRecChunk),stp);
731       return;
732
733   case ATOMIC_INVARIANT:
734       copy(p,info,q,sizeofW(StgAtomicInvariant),stp);
735       return;
736
737   case INVARIANT_CHECK_QUEUE:
738       copy(p,info,q,sizeofW(StgInvariantCheckQueue),stp);
739       return;
740
741   default:
742     barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
743   }
744
745   barf("evacuate");
746 }
747
748 /* -----------------------------------------------------------------------------
749    Evaluate a THUNK_SELECTOR if possible.
750
751    p points to a THUNK_SELECTOR that we want to evaluate.  The
752    result of "evaluating" it will be evacuated and a pointer to the
753    to-space closure will be returned.
754
755    If the THUNK_SELECTOR could not be evaluated (its selectee is still
756    a THUNK, for example), then the THUNK_SELECTOR itself will be
757    evacuated.
758    -------------------------------------------------------------------------- */
759 static void
760 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
761 {
762     StgSelector *prev;
763
764     prev = NULL;
765     while (p)
766     {
767 #ifdef THREADED_RTS
768         ASSERT(p->header.info == &stg_WHITEHOLE_info);
769 #else
770         ASSERT(p->header.info == &stg_BLACKHOLE_info);
771 #endif
772         // val must be in to-space.  Not always: when we recursively
773         // invoke eval_thunk_selector(), the recursive calls will not 
774         // evacuate the value (because we want to select on the value,
775         // not evacuate it), so in this case val is in from-space.
776         // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
777
778         prev = (StgSelector*)((StgClosure *)p)->payload[0];
779
780         // Update the THUNK_SELECTOR with an indirection to the
781         // value.  The value is still in from-space at this stage.
782         //
783         // (old note: Why not do upd_evacuee(q,p)?  Because we have an
784         // invariant that an EVACUATED closure always points to an
785         // object in the same or an older generation (required by
786         // the short-cut test in the EVACUATED case, below).
787         if ((StgClosure *)p == val) {
788             // must be a loop; just leave a BLACKHOLE in place.  This
789             // can happen when we have a chain of selectors that
790             // eventually loops back on itself.  We can't leave an
791             // indirection pointing to itself, and we want the program
792             // to deadlock if it ever enters this closure, so
793             // BLACKHOLE is correct.
794             SET_INFO(p, &stg_BLACKHOLE_info);
795         } else {
796             ((StgInd *)p)->indirectee = val;
797             write_barrier();
798             SET_INFO(p, &stg_IND_info);
799         }
800
801         // For the purposes of LDV profiling, we have created an
802         // indirection.
803         LDV_RECORD_CREATE(p);
804
805         p = prev;
806     }
807 }
808
809 static void
810 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
811                  // NB. for legacy reasons, p & q are swapped around :(
812 {
813     nat field;
814     StgInfoTable *info;
815     StgWord info_ptr;
816     StgClosure *selectee;
817     StgSelector *prev_thunk_selector;
818     bdescr *bd;
819     StgClosure *val;
820     
821     prev_thunk_selector = NULL;
822     // this is a chain of THUNK_SELECTORs that we are going to update
823     // to point to the value of the current THUNK_SELECTOR.  Each
824     // closure on the chain is a BLACKHOLE, and points to the next in the
825     // chain with payload[0].
826
827 selector_chain:
828
829     bd = Bdescr((StgPtr)p);
830     if (HEAP_ALLOCED_GC(p)) {
831         // If the THUNK_SELECTOR is in to-space or in a generation that we
832         // are not collecting, then bale out early.  We won't be able to
833         // save any space in any case, and updating with an indirection is
834         // trickier in a non-collected gen: we would have to update the
835         // mutable list.
836         if (bd->flags & BF_EVACUATED) {
837             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
838             *q = (StgClosure *)p;
839             // shortcut, behave as for:  if (evac) evacuate(q);
840             if (evac && bd->step < gct->evac_step) {
841                 gct->failed_to_evac = rtsTrue;
842                 TICK_GC_FAILED_PROMOTION();
843             }
844             return;
845         }
846         // we don't update THUNK_SELECTORS in the compacted
847         // generation, because compaction does not remove the INDs
848         // that result, this causes confusion later
849         // (scavenge_mark_stack doesn't deal with IND).  BEWARE!  This
850         // bit is very tricky to get right.  If you make changes
851         // around here, test by compiling stage 3 with +RTS -c -RTS.
852         if (bd->flags & BF_MARKED) {
853             // must call evacuate() to mark this closure if evac==rtsTrue
854             *q = (StgClosure *)p;
855             if (evac) evacuate(q);
856             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
857             return;
858         }
859     }
860
861
862     // BLACKHOLE the selector thunk, since it is now under evaluation.
863     // This is important to stop us going into an infinite loop if
864     // this selector thunk eventually refers to itself.
865 #if defined(THREADED_RTS)
866     // In threaded mode, we'll use WHITEHOLE to lock the selector
867     // thunk while we evaluate it.
868     {
869         do {
870             info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
871         } while (info_ptr == (W_)&stg_WHITEHOLE_info);
872
873         // make sure someone else didn't get here first...
874         if (IS_FORWARDING_PTR(p) || 
875             INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
876             // v. tricky now.  The THUNK_SELECTOR has been evacuated
877             // by another thread, and is now either a forwarding ptr or IND.
878             // We need to extract ourselves from the current situation
879             // as cleanly as possible.
880             //   - unlock the closure
881             //   - update *q, we may have done *some* evaluation
882             //   - if evac, we need to call evacuate(), because we
883             //     need the write-barrier stuff.
884             //   - undo the chain we've built to point to p.
885             SET_INFO(p, (const StgInfoTable *)info_ptr);
886             *q = (StgClosure *)p;
887             if (evac) evacuate(q);
888             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
889             return;
890         }
891     }
892 #else
893     // Save the real info pointer (NOTE: not the same as get_itbl()).
894     info_ptr = (StgWord)p->header.info;
895     SET_INFO(p,&stg_BLACKHOLE_info);
896 #endif
897
898     field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
899
900     // The selectee might be a constructor closure,
901     // so we untag the pointer.
902     selectee = UNTAG_CLOSURE(p->selectee);
903
904 selector_loop:
905     // selectee now points to the closure that we're trying to select
906     // a field from.  It may or may not be in to-space: we try not to
907     // end up in to-space, but it's impractical to avoid it in
908     // general.  The compacting GC scatters to-space pointers in
909     // from-space during marking, for example.  We rely on the property
910     // that evacuate() doesn't mind if it gets passed a to-space pointer.
911
912     info = (StgInfoTable*)selectee->header.info;
913
914     if (IS_FORWARDING_PTR(info)) {
915         // We don't follow pointers into to-space; the constructor
916         // has already been evacuated, so we won't save any space
917         // leaks by evaluating this selector thunk anyhow.
918         goto bale_out;
919     }
920
921     info = INFO_PTR_TO_STRUCT(info);
922     switch (info->type) {
923       case WHITEHOLE:
924           goto bale_out; // about to be evacuated by another thread (or a loop).
925         
926       case CONSTR:
927       case CONSTR_1_0:
928       case CONSTR_0_1:
929       case CONSTR_2_0:
930       case CONSTR_1_1:
931       case CONSTR_0_2:
932       case CONSTR_STATIC:
933       case CONSTR_NOCAF_STATIC:
934           {
935               // check that the size is in range 
936               ASSERT(field <  (StgWord32)(info->layout.payload.ptrs + 
937                                           info->layout.payload.nptrs));
938           
939               // Select the right field from the constructor
940               val = selectee->payload[field];
941               
942 #ifdef PROFILING
943               // For the purposes of LDV profiling, we have destroyed
944               // the original selector thunk, p.
945               SET_INFO(p, (StgInfoTable *)info_ptr);
946               LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
947 #if defined(THREADED_RTS)
948               SET_INFO(p, &stg_WHITEHOLE_info);
949 #else
950               SET_INFO(p, &stg_BLACKHOLE_info);
951 #endif
952 #endif
953
954               // the closure in val is now the "value" of the
955               // THUNK_SELECTOR in p.  However, val may itself be a
956               // THUNK_SELECTOR, in which case we want to continue
957               // evaluating until we find the real value, and then
958               // update the whole chain to point to the value.
959           val_loop:
960               info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
961               if (!IS_FORWARDING_PTR(info_ptr))
962               {
963                   info = INFO_PTR_TO_STRUCT(info_ptr);
964                   switch (info->type) {
965                   case IND:
966                   case IND_PERM:
967                   case IND_OLDGEN:
968                   case IND_OLDGEN_PERM:
969                   case IND_STATIC:
970                       val = ((StgInd *)val)->indirectee;
971                       goto val_loop;
972                   case THUNK_SELECTOR:
973                       ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
974                       prev_thunk_selector = p;
975                       p = (StgSelector*)val;
976                       goto selector_chain;
977                   default:
978                       break;
979                   }
980               }
981               ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
982               prev_thunk_selector = p;
983
984               *q = val;
985
986               // update the other selectors in the chain *before*
987               // evacuating the value.  This is necessary in the case
988               // where the value turns out to be one of the selectors
989               // in the chain (i.e. we have a loop), and evacuating it
990               // would corrupt the chain.
991               unchain_thunk_selectors(prev_thunk_selector, val);
992
993               // evacuate() cannot recurse through
994               // eval_thunk_selector(), because we know val is not
995               // a THUNK_SELECTOR.
996               if (evac) evacuate(q);
997               return;
998           }
999
1000       case IND:
1001       case IND_PERM:
1002       case IND_OLDGEN:
1003       case IND_OLDGEN_PERM:
1004       case IND_STATIC:
1005           // Again, we might need to untag a constructor.
1006           selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1007           goto selector_loop;
1008
1009       case THUNK_SELECTOR:
1010       {
1011           StgClosure *val;
1012
1013           // recursively evaluate this selector.  We don't want to
1014           // recurse indefinitely, so we impose a depth bound.
1015           if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1016               goto bale_out;
1017           }
1018
1019           gct->thunk_selector_depth++;
1020           // rtsFalse says "don't evacuate the result".  It will,
1021           // however, update any THUNK_SELECTORs that are evaluated
1022           // along the way.
1023           eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1024           gct->thunk_selector_depth--;
1025
1026           // did we actually manage to evaluate it?
1027           if (val == selectee) goto bale_out;
1028
1029           // Of course this pointer might be tagged...
1030           selectee = UNTAG_CLOSURE(val);
1031           goto selector_loop;
1032       }
1033
1034       case AP:
1035       case AP_STACK:
1036       case THUNK:
1037       case THUNK_1_0:
1038       case THUNK_0_1:
1039       case THUNK_2_0:
1040       case THUNK_1_1:
1041       case THUNK_0_2:
1042       case THUNK_STATIC:
1043       case CAF_BLACKHOLE:
1044       case BLACKHOLE:
1045           // not evaluated yet 
1046           goto bale_out;
1047     
1048       default:
1049         barf("eval_thunk_selector: strange selectee %d",
1050              (int)(info->type));
1051     }
1052
1053 bale_out:
1054     // We didn't manage to evaluate this thunk; restore the old info
1055     // pointer.  But don't forget: we still need to evacuate the thunk itself.
1056     SET_INFO(p, (const StgInfoTable *)info_ptr);
1057     // THREADED_RTS: we just unlocked the thunk, so another thread
1058     // might get in and update it.  copy() will lock it again and
1059     // check whether it was updated in the meantime.
1060     *q = (StgClosure *)p;
1061     if (evac) {
1062         copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
1063     }
1064     unchain_thunk_selectors(prev_thunk_selector, *q);
1065     return;
1066 }