Store a destination step in the block descriptor
[ghc.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 initBdescr(bd, new_stp);
286
287 // If this is a block of pinned objects, we don't have to scan
288 // these objects, because they aren't allowed to contain any
289 // pointers. For these blocks, we skip the scavenge stage and put
290 // them straight on the scavenged_large_objects list.
291 if (bd->flags & BF_PINNED) {
292 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
293 if (new_stp != stp) { ACQUIRE_SPIN_LOCK(&new_stp->sync_large_objects); }
294 dbl_link_onto(bd, &new_stp->scavenged_large_objects);
295 new_stp->n_scavenged_large_blocks += bd->blocks;
296 if (new_stp != stp) { RELEASE_SPIN_LOCK(&new_stp->sync_large_objects); }
297 } else {
298 bd->link = ws->todo_large_objects;
299 ws->todo_large_objects = bd;
300 }
301
302 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
303 }
304
305 /* ----------------------------------------------------------------------------
306 Evacuate
307
308 This is called (eventually) for every live object in the system.
309
310 The caller to evacuate specifies a desired generation in the
311 gct->evac_step thread-local variable. The following conditions apply to
312 evacuating an object which resides in generation M when we're
313 collecting up to generation N
314
315 if M >= gct->evac_step
316 if M > N do nothing
317 else evac to step->to
318
319 if M < gct->evac_step evac to gct->evac_step, step 0
320
321 if the object is already evacuated, then we check which generation
322 it now resides in.
323
324 if M >= gct->evac_step do nothing
325 if M < gct->evac_step set gct->failed_to_evac flag to indicate that we
326 didn't manage to evacuate this object into gct->evac_step.
327
328
329 OPTIMISATION NOTES:
330
331 evacuate() is the single most important function performance-wise
332 in the GC. Various things have been tried to speed it up, but as
333 far as I can tell the code generated by gcc 3.2 with -O2 is about
334 as good as it's going to get. We pass the argument to evacuate()
335 in a register using the 'regparm' attribute (see the prototype for
336 evacuate() near the top of this file).
337
338 Changing evacuate() to take an (StgClosure **) rather than
339 returning the new pointer seems attractive, because we can avoid
340 writing back the pointer when it hasn't changed (eg. for a static
341 object, or an object in a generation > N). However, I tried it and
342 it doesn't help. One reason is that the (StgClosure **) pointer
343 gets spilled to the stack inside evacuate(), resulting in far more
344 extra reads/writes than we save.
345 ------------------------------------------------------------------------- */
346
347 REGPARM1 GNUC_ATTR_HOT void
348 evacuate(StgClosure **p)
349 {
350 bdescr *bd = NULL;
351 step *stp;
352 StgClosure *q;
353 const StgInfoTable *info;
354 StgWord tag;
355
356 q = *p;
357
358 loop:
359 /* The tag and the pointer are split, to be merged after evacing */
360 tag = GET_CLOSURE_TAG(q);
361 q = UNTAG_CLOSURE(q);
362
363 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
364
365 if (!HEAP_ALLOCED_GC(q)) {
366
367 if (!major_gc) return;
368
369 info = get_itbl(q);
370 switch (info->type) {
371
372 case THUNK_STATIC:
373 if (info->srt_bitmap != 0) {
374 if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
375 #ifndef THREADED_RTS
376 *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
377 gct->static_objects = (StgClosure *)q;
378 #else
379 StgPtr link;
380 link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
381 (StgWord)NULL,
382 (StgWord)gct->static_objects);
383 if (link == NULL) {
384 gct->static_objects = (StgClosure *)q;
385 }
386 #endif
387 }
388 }
389 return;
390
391 case FUN_STATIC:
392 if (info->srt_bitmap != 0 &&
393 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
394 #ifndef THREADED_RTS
395 *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
396 gct->static_objects = (StgClosure *)q;
397 #else
398 StgPtr link;
399 link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
400 (StgWord)NULL,
401 (StgWord)gct->static_objects);
402 if (link == NULL) {
403 gct->static_objects = (StgClosure *)q;
404 }
405 #endif
406 }
407 return;
408
409 case IND_STATIC:
410 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
411 * on the CAF list, so don't do anything with it here (we'll
412 * scavenge it later).
413 */
414 if (((StgIndStatic *)q)->saved_info == NULL) {
415 if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
416 #ifndef THREADED_RTS
417 *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
418 gct->static_objects = (StgClosure *)q;
419 #else
420 StgPtr link;
421 link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
422 (StgWord)NULL,
423 (StgWord)gct->static_objects);
424 if (link == NULL) {
425 gct->static_objects = (StgClosure *)q;
426 }
427 #endif
428 }
429 }
430 return;
431
432 case CONSTR_STATIC:
433 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
434 #ifndef THREADED_RTS
435 *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
436 gct->static_objects = (StgClosure *)q;
437 #else
438 StgPtr link;
439 link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
440 (StgWord)NULL,
441 (StgWord)gct->static_objects);
442 if (link == NULL) {
443 gct->static_objects = (StgClosure *)q;
444 }
445 #endif
446 }
447 /* I am assuming that static_objects pointers are not
448 * written to other objects, and thus, no need to retag. */
449 return;
450
451 case CONSTR_NOCAF_STATIC:
452 /* no need to put these on the static linked list, they don't need
453 * to be scavenged.
454 */
455 return;
456
457 default:
458 barf("evacuate(static): strange closure type %d", (int)(info->type));
459 }
460 }
461
462 bd = Bdescr((P_)q);
463
464 if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
465
466 // pointer into to-space: just return it. It might be a pointer
467 // into a generation that we aren't collecting (> N), or it
468 // might just be a pointer into to-space. The latter doesn't
469 // happen often, but allowing it makes certain things a bit
470 // easier; e.g. scavenging an object is idempotent, so it's OK to
471 // have an object on the mutable list multiple times.
472 if (bd->flags & BF_EVACUATED) {
473 // We aren't copying this object, so we have to check
474 // whether it is already in the target generation. (this is
475 // the write barrier).
476 if (bd->step < gct->evac_step) {
477 gct->failed_to_evac = rtsTrue;
478 TICK_GC_FAILED_PROMOTION();
479 }
480 return;
481 }
482
483 /* evacuate large objects by re-linking them onto a different list.
484 */
485 if (bd->flags & BF_LARGE) {
486 info = get_itbl(q);
487 if (info->type == TSO &&
488 ((StgTSO *)q)->what_next == ThreadRelocated) {
489 q = (StgClosure *)((StgTSO *)q)->_link;
490 *p = q;
491 goto loop;
492 }
493 evacuate_large((P_)q);
494 return;
495 }
496
497 /* If the object is in a step that we're compacting, then we
498 * need to use an alternative evacuate procedure.
499 */
500 if (!is_marked((P_)q,bd)) {
501 mark((P_)q,bd);
502 push_mark_stack((P_)q);
503 }
504 return;
505 }
506
507 stp = bd->dest;
508
509 info = q->header.info;
510 if (IS_FORWARDING_PTR(info))
511 {
512 /* Already evacuated, just return the forwarding address.
513 * HOWEVER: if the requested destination generation (gct->evac_step) is
514 * older than the actual generation (because the object was
515 * already evacuated to a younger generation) then we have to
516 * set the gct->failed_to_evac flag to indicate that we couldn't
517 * manage to promote the object to the desired generation.
518 */
519 /*
520 * Optimisation: the check is fairly expensive, but we can often
521 * shortcut it if either the required generation is 0, or the
522 * current object (the EVACUATED) is in a high enough generation.
523 * We know that an EVACUATED always points to an object in the
524 * same or an older generation. stp is the lowest step that the
525 * current object would be evacuated to, so we only do the full
526 * check if stp is too low.
527 */
528 StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
529 *p = TAG_CLOSURE(tag,e);
530 if (stp < gct->evac_step) { // optimisation
531 if (Bdescr((P_)e)->step < gct->evac_step) {
532 gct->failed_to_evac = rtsTrue;
533 TICK_GC_FAILED_PROMOTION();
534 }
535 }
536 return;
537 }
538
539 switch (INFO_PTR_TO_STRUCT(info)->type) {
540
541 case WHITEHOLE:
542 goto loop;
543
544 case MUT_VAR_CLEAN:
545 case MUT_VAR_DIRTY:
546 case MVAR_CLEAN:
547 case MVAR_DIRTY:
548 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
549 return;
550
551 // For ints and chars of low value, save space by replacing references to
552 // these with closures with references to common, shared ones in the RTS.
553 //
554 // * Except when compiling into Windows DLLs which don't support cross-package
555 // data references very well.
556 //
557 case CONSTR_0_1:
558 {
559 #if defined(__PIC__) && defined(mingw32_HOST_OS)
560 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
561 #else
562 StgWord w = (StgWord)q->payload[0];
563 if (info == Czh_con_info &&
564 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
565 (StgChar)w <= MAX_CHARLIKE) {
566 *p = TAG_CLOSURE(tag,
567 (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
568 );
569 }
570 else if (info == Izh_con_info &&
571 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
572 *p = TAG_CLOSURE(tag,
573 (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
574 );
575 }
576 else {
577 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
578 }
579 #endif
580 return;
581 }
582
583 case FUN_0_1:
584 case FUN_1_0:
585 case CONSTR_1_0:
586 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
587 return;
588
589 case THUNK_1_0:
590 case THUNK_0_1:
591 copy(p,info,q,sizeofW(StgThunk)+1,stp);
592 return;
593
594 case THUNK_1_1:
595 case THUNK_2_0:
596 case THUNK_0_2:
597 #ifdef NO_PROMOTE_THUNKS
598 if (bd->gen_no == 0 &&
599 bd->step->no != 0 &&
600 bd->step->no == generations[bd->gen_no].n_steps-1) {
601 stp = bd->step;
602 }
603 #endif
604 copy(p,info,q,sizeofW(StgThunk)+2,stp);
605 return;
606
607 case FUN_1_1:
608 case FUN_2_0:
609 case FUN_0_2:
610 case CONSTR_1_1:
611 case CONSTR_2_0:
612 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
613 return;
614
615 case CONSTR_0_2:
616 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
617 return;
618
619 case THUNK:
620 copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
621 return;
622
623 case FUN:
624 case IND_PERM:
625 case IND_OLDGEN_PERM:
626 case CONSTR:
627 copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
628 return;
629
630 case WEAK:
631 case STABLE_NAME:
632 copy_tag(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
633 return;
634
635 case BCO:
636 copy(p,info,q,bco_sizeW((StgBCO *)q),stp);
637 return;
638
639 case CAF_BLACKHOLE:
640 case BLACKHOLE:
641 copyPart(p,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
642 return;
643
644 case THUNK_SELECTOR:
645 eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
646 return;
647
648 case IND:
649 case IND_OLDGEN:
650 // follow chains of indirections, don't evacuate them
651 q = ((StgInd*)q)->indirectee;
652 *p = q;
653 goto loop;
654
655 case RET_BCO:
656 case RET_SMALL:
657 case RET_BIG:
658 case RET_DYN:
659 case UPDATE_FRAME:
660 case STOP_FRAME:
661 case CATCH_FRAME:
662 case CATCH_STM_FRAME:
663 case CATCH_RETRY_FRAME:
664 case ATOMICALLY_FRAME:
665 // shouldn't see these
666 barf("evacuate: stack frame at %p\n", q);
667
668 case PAP:
669 copy(p,info,q,pap_sizeW((StgPAP*)q),stp);
670 return;
671
672 case AP:
673 copy(p,info,q,ap_sizeW((StgAP*)q),stp);
674 return;
675
676 case AP_STACK:
677 copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
678 return;
679
680 case ARR_WORDS:
681 // just copy the block
682 copy(p,info,q,arr_words_sizeW((StgArrWords *)q),stp);
683 return;
684
685 case MUT_ARR_PTRS_CLEAN:
686 case MUT_ARR_PTRS_DIRTY:
687 case MUT_ARR_PTRS_FROZEN:
688 case MUT_ARR_PTRS_FROZEN0:
689 // just copy the block
690 copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
691 return;
692
693 case TSO:
694 {
695 StgTSO *tso = (StgTSO *)q;
696
697 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
698 */
699 if (tso->what_next == ThreadRelocated) {
700 q = (StgClosure *)tso->_link;
701 *p = q;
702 goto loop;
703 }
704
705 /* To evacuate a small TSO, we need to adjust the stack pointer
706 */
707 {
708 StgTSO *new_tso;
709 StgPtr r, s;
710 rtsBool mine;
711
712 mine = copyPart(p,(StgClosure *)tso, tso_sizeW(tso),
713 sizeofW(StgTSO), stp);
714 if (mine) {
715 new_tso = (StgTSO *)*p;
716 move_TSO(tso, new_tso);
717 for (r = tso->sp, s = new_tso->sp;
718 r < tso->stack+tso->stack_size;) {
719 *s++ = *r++;
720 }
721 }
722 return;
723 }
724 }
725
726 case TREC_HEADER:
727 copy(p,info,q,sizeofW(StgTRecHeader),stp);
728 return;
729
730 case TVAR_WATCH_QUEUE:
731 copy(p,info,q,sizeofW(StgTVarWatchQueue),stp);
732 return;
733
734 case TVAR:
735 copy(p,info,q,sizeofW(StgTVar),stp);
736 return;
737
738 case TREC_CHUNK:
739 copy(p,info,q,sizeofW(StgTRecChunk),stp);
740 return;
741
742 case ATOMIC_INVARIANT:
743 copy(p,info,q,sizeofW(StgAtomicInvariant),stp);
744 return;
745
746 case INVARIANT_CHECK_QUEUE:
747 copy(p,info,q,sizeofW(StgInvariantCheckQueue),stp);
748 return;
749
750 default:
751 barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
752 }
753
754 barf("evacuate");
755 }
756
757 /* -----------------------------------------------------------------------------
758 Evaluate a THUNK_SELECTOR if possible.
759
760 p points to a THUNK_SELECTOR that we want to evaluate. The
761 result of "evaluating" it will be evacuated and a pointer to the
762 to-space closure will be returned.
763
764 If the THUNK_SELECTOR could not be evaluated (its selectee is still
765 a THUNK, for example), then the THUNK_SELECTOR itself will be
766 evacuated.
767 -------------------------------------------------------------------------- */
768 static void
769 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
770 {
771 StgSelector *prev;
772
773 prev = NULL;
774 while (p)
775 {
776 #ifdef THREADED_RTS
777 ASSERT(p->header.info == &stg_WHITEHOLE_info);
778 #else
779 ASSERT(p->header.info == &stg_BLACKHOLE_info);
780 #endif
781 // val must be in to-space. Not always: when we recursively
782 // invoke eval_thunk_selector(), the recursive calls will not
783 // evacuate the value (because we want to select on the value,
784 // not evacuate it), so in this case val is in from-space.
785 // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
786
787 prev = (StgSelector*)((StgClosure *)p)->payload[0];
788
789 // Update the THUNK_SELECTOR with an indirection to the
790 // value. The value is still in from-space at this stage.
791 //
792 // (old note: Why not do upd_evacuee(q,p)? Because we have an
793 // invariant that an EVACUATED closure always points to an
794 // object in the same or an older generation (required by
795 // the short-cut test in the EVACUATED case, below).
796 if ((StgClosure *)p == val) {
797 // must be a loop; just leave a BLACKHOLE in place. This
798 // can happen when we have a chain of selectors that
799 // eventually loops back on itself. We can't leave an
800 // indirection pointing to itself, and we want the program
801 // to deadlock if it ever enters this closure, so
802 // BLACKHOLE is correct.
803 SET_INFO(p, &stg_BLACKHOLE_info);
804 } else {
805 ((StgInd *)p)->indirectee = val;
806 write_barrier();
807 SET_INFO(p, &stg_IND_info);
808 }
809
810 // For the purposes of LDV profiling, we have created an
811 // indirection.
812 LDV_RECORD_CREATE(p);
813
814 p = prev;
815 }
816 }
817
818 static void
819 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
820 // NB. for legacy reasons, p & q are swapped around :(
821 {
822 nat field;
823 StgInfoTable *info;
824 StgWord info_ptr;
825 StgClosure *selectee;
826 StgSelector *prev_thunk_selector;
827 bdescr *bd;
828 StgClosure *val;
829
830 prev_thunk_selector = NULL;
831 // this is a chain of THUNK_SELECTORs that we are going to update
832 // to point to the value of the current THUNK_SELECTOR. Each
833 // closure on the chain is a BLACKHOLE, and points to the next in the
834 // chain with payload[0].
835
836 selector_chain:
837
838 bd = Bdescr((StgPtr)p);
839 if (HEAP_ALLOCED_GC(p)) {
840 // If the THUNK_SELECTOR is in to-space or in a generation that we
841 // are not collecting, then bale out early. We won't be able to
842 // save any space in any case, and updating with an indirection is
843 // trickier in a non-collected gen: we would have to update the
844 // mutable list.
845 if (bd->flags & BF_EVACUATED) {
846 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
847 *q = (StgClosure *)p;
848 // shortcut, behave as for: if (evac) evacuate(q);
849 if (evac && bd->step < gct->evac_step) {
850 gct->failed_to_evac = rtsTrue;
851 TICK_GC_FAILED_PROMOTION();
852 }
853 return;
854 }
855 // we don't update THUNK_SELECTORS in the compacted
856 // generation, because compaction does not remove the INDs
857 // that result, this causes confusion later
858 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
859 // bit is very tricky to get right. If you make changes
860 // around here, test by compiling stage 3 with +RTS -c -RTS.
861 if (bd->flags & BF_MARKED) {
862 // must call evacuate() to mark this closure if evac==rtsTrue
863 *q = (StgClosure *)p;
864 if (evac) evacuate(q);
865 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
866 return;
867 }
868 }
869
870
871 // BLACKHOLE the selector thunk, since it is now under evaluation.
872 // This is important to stop us going into an infinite loop if
873 // this selector thunk eventually refers to itself.
874 #if defined(THREADED_RTS)
875 // In threaded mode, we'll use WHITEHOLE to lock the selector
876 // thunk while we evaluate it.
877 {
878 do {
879 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
880 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
881
882 // make sure someone else didn't get here first...
883 if (IS_FORWARDING_PTR(p) ||
884 INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
885 // v. tricky now. The THUNK_SELECTOR has been evacuated
886 // by another thread, and is now either a forwarding ptr or IND.
887 // We need to extract ourselves from the current situation
888 // as cleanly as possible.
889 // - unlock the closure
890 // - update *q, we may have done *some* evaluation
891 // - if evac, we need to call evacuate(), because we
892 // need the write-barrier stuff.
893 // - undo the chain we've built to point to p.
894 SET_INFO(p, (const StgInfoTable *)info_ptr);
895 *q = (StgClosure *)p;
896 if (evac) evacuate(q);
897 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
898 return;
899 }
900 }
901 #else
902 // Save the real info pointer (NOTE: not the same as get_itbl()).
903 info_ptr = (StgWord)p->header.info;
904 SET_INFO(p,&stg_BLACKHOLE_info);
905 #endif
906
907 field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
908
909 // The selectee might be a constructor closure,
910 // so we untag the pointer.
911 selectee = UNTAG_CLOSURE(p->selectee);
912
913 selector_loop:
914 // selectee now points to the closure that we're trying to select
915 // a field from. It may or may not be in to-space: we try not to
916 // end up in to-space, but it's impractical to avoid it in
917 // general. The compacting GC scatters to-space pointers in
918 // from-space during marking, for example. We rely on the property
919 // that evacuate() doesn't mind if it gets passed a to-space pointer.
920
921 info = (StgInfoTable*)selectee->header.info;
922
923 if (IS_FORWARDING_PTR(info)) {
924 // We don't follow pointers into to-space; the constructor
925 // has already been evacuated, so we won't save any space
926 // leaks by evaluating this selector thunk anyhow.
927 goto bale_out;
928 }
929
930 info = INFO_PTR_TO_STRUCT(info);
931 switch (info->type) {
932 case WHITEHOLE:
933 goto bale_out; // about to be evacuated by another thread (or a loop).
934
935 case CONSTR:
936 case CONSTR_1_0:
937 case CONSTR_0_1:
938 case CONSTR_2_0:
939 case CONSTR_1_1:
940 case CONSTR_0_2:
941 case CONSTR_STATIC:
942 case CONSTR_NOCAF_STATIC:
943 {
944 // check that the size is in range
945 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
946 info->layout.payload.nptrs));
947
948 // Select the right field from the constructor
949 val = selectee->payload[field];
950
951 #ifdef PROFILING
952 // For the purposes of LDV profiling, we have destroyed
953 // the original selector thunk, p.
954 SET_INFO(p, (StgInfoTable *)info_ptr);
955 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
956 #if defined(THREADED_RTS)
957 SET_INFO(p, &stg_WHITEHOLE_info);
958 #else
959 SET_INFO(p, &stg_BLACKHOLE_info);
960 #endif
961 #endif
962
963 // the closure in val is now the "value" of the
964 // THUNK_SELECTOR in p. However, val may itself be a
965 // THUNK_SELECTOR, in which case we want to continue
966 // evaluating until we find the real value, and then
967 // update the whole chain to point to the value.
968 val_loop:
969 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
970 if (!IS_FORWARDING_PTR(info_ptr))
971 {
972 info = INFO_PTR_TO_STRUCT(info_ptr);
973 switch (info->type) {
974 case IND:
975 case IND_PERM:
976 case IND_OLDGEN:
977 case IND_OLDGEN_PERM:
978 case IND_STATIC:
979 val = ((StgInd *)val)->indirectee;
980 goto val_loop;
981 case THUNK_SELECTOR:
982 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
983 prev_thunk_selector = p;
984 p = (StgSelector*)val;
985 goto selector_chain;
986 default:
987 break;
988 }
989 }
990 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
991 prev_thunk_selector = p;
992
993 *q = val;
994
995 // update the other selectors in the chain *before*
996 // evacuating the value. This is necessary in the case
997 // where the value turns out to be one of the selectors
998 // in the chain (i.e. we have a loop), and evacuating it
999 // would corrupt the chain.
1000 unchain_thunk_selectors(prev_thunk_selector, val);
1001
1002 // evacuate() cannot recurse through
1003 // eval_thunk_selector(), because we know val is not
1004 // a THUNK_SELECTOR.
1005 if (evac) evacuate(q);
1006 return;
1007 }
1008
1009 case IND:
1010 case IND_PERM:
1011 case IND_OLDGEN:
1012 case IND_OLDGEN_PERM:
1013 case IND_STATIC:
1014 // Again, we might need to untag a constructor.
1015 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1016 goto selector_loop;
1017
1018 case THUNK_SELECTOR:
1019 {
1020 StgClosure *val;
1021
1022 // recursively evaluate this selector. We don't want to
1023 // recurse indefinitely, so we impose a depth bound.
1024 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1025 goto bale_out;
1026 }
1027
1028 gct->thunk_selector_depth++;
1029 // rtsFalse says "don't evacuate the result". It will,
1030 // however, update any THUNK_SELECTORs that are evaluated
1031 // along the way.
1032 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1033 gct->thunk_selector_depth--;
1034
1035 // did we actually manage to evaluate it?
1036 if (val == selectee) goto bale_out;
1037
1038 // Of course this pointer might be tagged...
1039 selectee = UNTAG_CLOSURE(val);
1040 goto selector_loop;
1041 }
1042
1043 case AP:
1044 case AP_STACK:
1045 case THUNK:
1046 case THUNK_1_0:
1047 case THUNK_0_1:
1048 case THUNK_2_0:
1049 case THUNK_1_1:
1050 case THUNK_0_2:
1051 case THUNK_STATIC:
1052 case CAF_BLACKHOLE:
1053 case BLACKHOLE:
1054 // not evaluated yet
1055 goto bale_out;
1056
1057 default:
1058 barf("eval_thunk_selector: strange selectee %d",
1059 (int)(info->type));
1060 }
1061
1062 bale_out:
1063 // We didn't manage to evaluate this thunk; restore the old info
1064 // pointer. But don't forget: we still need to evacuate the thunk itself.
1065 SET_INFO(p, (const StgInfoTable *)info_ptr);
1066 // THREADED_RTS: we just unlocked the thunk, so another thread
1067 // might get in and update it. copy() will lock it again and
1068 // check whether it was updated in the meantime.
1069 *q = (StgClosure *)p;
1070 if (evac) {
1071 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->dest);
1072 }
1073 unchain_thunk_selectors(prev_thunk_selector, *q);
1074 return;
1075 }