Commit b521e8b6 authored by Ömer Sinan Ağacan's avatar Ömer Sinan Ağacan Committed by Marge Bot
Browse files

Refactor Compact.c:

- Remove forward declarations
- Introduce UNTAG_PTR and GET_PTR_TAG for dealing with pointer tags
  without having to cast arguments to StgClosure*
- Remove dead code
- Use W_ instead of StgWord
- Use P_ instead of StgPtr
parent 9b2a5008
...@@ -70,40 +70,47 @@ ...@@ -70,40 +70,47 @@
if we throw away some of the tags). if we throw away some of the tags).
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
STATIC_INLINE W_
UNTAG_PTR(W_ p)
{
return p & ~TAG_MASK;
}
STATIC_INLINE W_
GET_PTR_TAG(W_ p)
{
return p & TAG_MASK;
}
STATIC_INLINE void STATIC_INLINE void
thread (StgClosure **p) thread (StgClosure **p)
{ {
StgClosure *q0; StgClosure *q0 = *p;
StgPtr q; P_ q = (P_)UNTAG_CLOSURE(q0);
StgWord iptr;
bdescr *bd;
q0 = *p;
q = (StgPtr)UNTAG_CLOSURE(q0);
// It doesn't look like a closure at the moment, because the info // It doesn't look like a closure at the moment, because the info
// ptr is possibly threaded: // ptr is possibly threaded:
// ASSERT(LOOKS_LIKE_CLOSURE_PTR(q)); // ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
if (HEAP_ALLOCED(q)) { if (HEAP_ALLOCED(q)) {
bd = Bdescr(q); bdescr *bd = Bdescr(q);
if (bd->flags & BF_MARKED) if (bd->flags & BF_MARKED)
{ {
iptr = *q; W_ iptr = *q;
switch (GET_CLOSURE_TAG((StgClosure *)iptr)) switch (GET_PTR_TAG(iptr))
{ {
case 0: case 0:
// this is the info pointer; we are creating a new chain. // this is the info pointer; we are creating a new chain.
// save the original tag at the end of the chain. // save the original tag at the end of the chain.
*p = (StgClosure *)((StgWord)iptr + GET_CLOSURE_TAG(q0)); *p = (StgClosure *)((W_)iptr + GET_CLOSURE_TAG(q0));
*q = (StgWord)p + 1; *q = (W_)p + 1;
break; break;
case 1: case 1:
case 2: case 2:
// this is a chain of length 1 or more // this is a chain of length 1 or more
*p = (StgClosure *)iptr; *p = (StgClosure *)iptr;
*q = (StgWord)p + 2; *q = (W_)p + 2;
break; break;
} }
} }
...@@ -121,30 +128,31 @@ thread_root (void *user STG_UNUSED, StgClosure **p) ...@@ -121,30 +128,31 @@ thread_root (void *user STG_UNUSED, StgClosure **p)
STATIC_INLINE void thread_ (void *p) { thread((StgClosure **)p); } STATIC_INLINE void thread_ (void *p) { thread((StgClosure **)p); }
STATIC_INLINE void STATIC_INLINE void
unthread( StgPtr p, StgWord free ) unthread( P_ p, W_ free )
{ {
StgWord q, r; W_ q = *p;
StgPtr q0;
q = *p;
loop: loop:
switch (GET_CLOSURE_TAG((StgClosure *)q)) switch (GET_PTR_TAG(q))
{ {
case 0: case 0:
// nothing to do; the chain is length zero // nothing to do; the chain is length zero
return; return;
case 1: case 1:
q0 = (StgPtr)(q-1); {
r = *q0; // r is the info ptr, tagged with the pointer-tag P_ q0 = (P_)(q-1);
W_ r = *q0; // r is the info ptr, tagged with the pointer-tag
*q0 = free; *q0 = free;
*p = (StgWord)UNTAG_CLOSURE((StgClosure *)r); *p = (W_)UNTAG_PTR(r);
return; return;
}
case 2: case 2:
q0 = (StgPtr)(q-2); {
r = *q0; P_ q0 = (P_)(q-2);
W_ r = *q0;
*q0 = free; *q0 = free;
q = r; q = r;
goto loop; goto loop;
}
default: default:
barf("unthread"); barf("unthread");
} }
...@@ -154,28 +162,25 @@ loop: ...@@ -154,28 +162,25 @@ loop:
// The info pointer is also tagged with the appropriate pointer tag // The info pointer is also tagged with the appropriate pointer tag
// for this closure, which should be attached to the pointer // for this closure, which should be attached to the pointer
// subsequently passed to unthread(). // subsequently passed to unthread().
STATIC_INLINE StgWord STATIC_INLINE W_
get_threaded_info( StgPtr p ) get_threaded_info( P_ p )
{ {
StgWord q; W_ q = (W_)GET_INFO(UNTAG_CLOSURE((StgClosure *)p));
q = (W_)GET_INFO(UNTAG_CLOSURE((StgClosure *)p));
loop: loop:
switch (GET_CLOSURE_TAG((StgClosure *)q)) switch (GET_PTR_TAG(q))
{ {
case 0: case 0:
ASSERT(LOOKS_LIKE_INFO_PTR(q)); ASSERT(LOOKS_LIKE_INFO_PTR(q));
return q; return q;
case 1: case 1:
{ {
StgWord r = *(StgPtr)(q-1); W_ r = *(P_)(q-1);
ASSERT(LOOKS_LIKE_INFO_PTR((StgWord) ASSERT(LOOKS_LIKE_INFO_PTR((W_)UNTAG_CONST_CLOSURE((StgClosure *)r)));
UNTAG_CONST_CLOSURE((StgClosure *)r)));
return r; return r;
} }
case 2: case 2:
q = *(StgPtr)(q-2); q = *(P_)(q-2);
goto loop; goto loop;
default: default:
barf("get_threaded_info"); barf("get_threaded_info");
...@@ -185,7 +190,7 @@ loop: ...@@ -185,7 +190,7 @@ loop:
// A word-aligned memmove will be faster for small objects than libc's or gcc's. // A word-aligned memmove will be faster for small objects than libc's or gcc's.
// Remember, the two regions *might* overlap, but: to <= from. // Remember, the two regions *might* overlap, but: to <= from.
STATIC_INLINE void STATIC_INLINE void
move(StgPtr to, StgPtr from, StgWord size) move(P_ to, P_ from, W_ size)
{ {
for(; size > 0; --size) { for(; size > 0; --size) {
*to++ = *from++; *to++ = *from++;
...@@ -195,13 +200,11 @@ move(StgPtr to, StgPtr from, StgWord size) ...@@ -195,13 +200,11 @@ move(StgPtr to, StgPtr from, StgWord size)
static void static void
thread_static( StgClosure* p ) thread_static( StgClosure* p )
{ {
const StgInfoTable *info;
// keep going until we've threaded all the objects on the linked // keep going until we've threaded all the objects on the linked
// list... // list...
while (p != END_OF_STATIC_OBJECT_LIST) { while (p != END_OF_STATIC_OBJECT_LIST) {
p = UNTAG_STATIC_LIST_PTR(p); p = UNTAG_STATIC_LIST_PTR(p);
info = get_itbl(p); const StgInfoTable *info = get_itbl(p);
switch (info->type) { switch (info->type) {
case IND_STATIC: case IND_STATIC:
...@@ -233,14 +236,11 @@ thread_static( StgClosure* p ) ...@@ -233,14 +236,11 @@ thread_static( StgClosure* p )
} }
STATIC_INLINE void STATIC_INLINE void
thread_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, StgWord size ) thread_large_bitmap( P_ p, StgLargeBitmap *large_bitmap, W_ size )
{ {
W_ i, b; W_ b = 0;
StgWord bitmap; W_ bitmap = large_bitmap->bitmap[b];
for (W_ i = 0; i < size; ) {
b = 0;
bitmap = large_bitmap->bitmap[b];
for (i = 0; i < size; ) {
if ((bitmap & 1) == 0) { if ((bitmap & 1) == 0) {
thread((StgClosure **)p); thread((StgClosure **)p);
} }
...@@ -255,8 +255,8 @@ thread_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, StgWord size ) ...@@ -255,8 +255,8 @@ thread_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, StgWord size )
} }
} }
STATIC_INLINE StgPtr STATIC_INLINE P_
thread_small_bitmap (StgPtr p, StgWord size, StgWord bitmap) thread_small_bitmap (P_ p, W_ size, W_ bitmap)
{ {
while (size > 0) { while (size > 0) {
if ((bitmap & 1) == 0) { if ((bitmap & 1) == 0) {
...@@ -269,14 +269,13 @@ thread_small_bitmap (StgPtr p, StgWord size, StgWord bitmap) ...@@ -269,14 +269,13 @@ thread_small_bitmap (StgPtr p, StgWord size, StgWord bitmap)
return p; return p;
} }
STATIC_INLINE StgPtr STATIC_INLINE P_
thread_arg_block (StgFunInfoTable *fun_info, StgClosure **args) thread_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
{ {
StgPtr p; W_ bitmap;
StgWord bitmap; W_ size;
StgWord size;
p = (StgPtr)args; P_ p = (P_)args;
switch (fun_info->f.fun_type) { switch (fun_info->f.fun_type) {
case ARG_GEN: case ARG_GEN:
bitmap = BITMAP_BITS(fun_info->f.b.bitmap); bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
...@@ -298,12 +297,8 @@ thread_arg_block (StgFunInfoTable *fun_info, StgClosure **args) ...@@ -298,12 +297,8 @@ thread_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
} }
static void static void
thread_stack(StgPtr p, StgPtr stack_end) thread_stack(P_ p, P_ stack_end)
{ {
const StgRetInfoTable* info;
StgWord bitmap;
StgWord size;
// highly similar to scavenge_stack, but we do pointer threading here. // highly similar to scavenge_stack, but we do pointer threading here.
while (p < stack_end) { while (p < stack_end) {
...@@ -312,7 +307,7 @@ thread_stack(StgPtr p, StgPtr stack_end) ...@@ -312,7 +307,7 @@ thread_stack(StgPtr p, StgPtr stack_end)
// record. All activation records have 'bitmap' style layout // record. All activation records have 'bitmap' style layout
// info. // info.
// //
info = get_ret_itbl((StgClosure *)p); const StgRetInfoTable *info = get_ret_itbl((StgClosure *)p);
switch (info->i.type) { switch (info->i.type) {
...@@ -325,22 +320,22 @@ thread_stack(StgPtr p, StgPtr stack_end) ...@@ -325,22 +320,22 @@ thread_stack(StgPtr p, StgPtr stack_end)
case STOP_FRAME: case STOP_FRAME:
case CATCH_FRAME: case CATCH_FRAME:
case RET_SMALL: case RET_SMALL:
bitmap = BITMAP_BITS(info->i.layout.bitmap); {
size = BITMAP_SIZE(info->i.layout.bitmap); W_ bitmap = BITMAP_BITS(info->i.layout.bitmap);
W_ size = BITMAP_SIZE(info->i.layout.bitmap);
p++; p++;
// NOTE: the payload starts immediately after the info-ptr, we // NOTE: the payload starts immediately after the info-ptr, we
// don't have an StgHeader in the same sense as a heap closure. // don't have an StgHeader in the same sense as a heap closure.
p = thread_small_bitmap(p, size, bitmap); p = thread_small_bitmap(p, size, bitmap);
continue; continue;
}
case RET_BCO: { case RET_BCO: {
StgBCO *bco;
p++; p++;
bco = (StgBCO *)*p; StgBCO *bco = (StgBCO *)*p;
thread((StgClosure **)p); thread((StgClosure **)p);
p++; p++;
size = BCO_BITMAP_SIZE(bco); W_ size = BCO_BITMAP_SIZE(bco);
thread_large_bitmap(p, BCO_BITMAP(bco), size); thread_large_bitmap(p, BCO_BITMAP(bco), size);
p += size; p += size;
continue; continue;
...@@ -349,7 +344,7 @@ thread_stack(StgPtr p, StgPtr stack_end) ...@@ -349,7 +344,7 @@ thread_stack(StgPtr p, StgPtr stack_end)
// large bitmap (> 32 entries, or 64 on a 64-bit machine) // large bitmap (> 32 entries, or 64 on a 64-bit machine)
case RET_BIG: case RET_BIG:
p++; p++;
size = GET_LARGE_BITMAP(&info->i)->size; W_ size = GET_LARGE_BITMAP(&info->i)->size;
thread_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size); thread_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
p += size; p += size;
continue; continue;
...@@ -357,10 +352,9 @@ thread_stack(StgPtr p, StgPtr stack_end) ...@@ -357,10 +352,9 @@ thread_stack(StgPtr p, StgPtr stack_end)
case RET_FUN: case RET_FUN:
{ {
StgRetFun *ret_fun = (StgRetFun *)p; StgRetFun *ret_fun = (StgRetFun *)p;
StgFunInfoTable *fun_info; StgFunInfoTable *fun_info =
FUN_INFO_PTR_TO_STRUCT((StgInfoTable *)UNTAG_PTR(
fun_info = FUN_INFO_PTR_TO_STRUCT((StgInfoTable *)UNTAG_CLOSURE((StgClosure *) get_threaded_info((P_)ret_fun->fun)));
get_threaded_info((StgPtr)ret_fun->fun)));
// *before* threading it! // *before* threading it!
thread(&ret_fun->fun); thread(&ret_fun->fun);
p = thread_arg_block(fun_info, ret_fun->payload); p = thread_arg_block(fun_info, ret_fun->payload);
...@@ -374,19 +368,16 @@ thread_stack(StgPtr p, StgPtr stack_end) ...@@ -374,19 +368,16 @@ thread_stack(StgPtr p, StgPtr stack_end)
} }
} }
STATIC_INLINE StgPtr STATIC_INLINE P_
thread_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size) thread_PAP_payload (StgClosure *fun, StgClosure **payload, W_ size)
{ {
StgPtr p; StgFunInfoTable *fun_info =
StgWord bitmap; FUN_INFO_PTR_TO_STRUCT((StgInfoTable *)UNTAG_PTR(get_threaded_info((P_)fun)));
StgFunInfoTable *fun_info;
fun_info = FUN_INFO_PTR_TO_STRUCT((StgInfoTable *)UNTAG_CLOSURE((StgClosure *)
get_threaded_info((StgPtr)fun)));
ASSERT(fun_info->i.type != PAP); ASSERT(fun_info->i.type != PAP);
p = (StgPtr)payload; P_ p = (P_)payload;
W_ bitmap;
switch (fun_info->f.fun_type) { switch (fun_info->f.fun_type) {
case ARG_GEN: case ARG_GEN:
bitmap = BITMAP_BITS(fun_info->f.b.bitmap); bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
...@@ -396,7 +387,7 @@ thread_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size) ...@@ -396,7 +387,7 @@ thread_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size)
p += size; p += size;
break; break;
case ARG_BCO: case ARG_BCO:
thread_large_bitmap((StgPtr)payload, BCO_BITMAP(fun), size); thread_large_bitmap((P_)payload, BCO_BITMAP(fun), size);
p += size; p += size;
break; break;
default: default:
...@@ -409,25 +400,23 @@ thread_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size) ...@@ -409,25 +400,23 @@ thread_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size)
return p; return p;
} }
STATIC_INLINE StgPtr STATIC_INLINE P_
thread_PAP (StgPAP *pap) thread_PAP (StgPAP *pap)
{ {
StgPtr p; P_ p = thread_PAP_payload(pap->fun, pap->payload, pap->n_args);
p = thread_PAP_payload(pap->fun, pap->payload, pap->n_args);
thread(&pap->fun); thread(&pap->fun);
return p; return p;
} }
STATIC_INLINE StgPtr STATIC_INLINE P_
thread_AP (StgAP *ap) thread_AP (StgAP *ap)
{ {
StgPtr p; P_ p = thread_PAP_payload(ap->fun, ap->payload, ap->n_args);
p = thread_PAP_payload(ap->fun, ap->payload, ap->n_args);
thread(&ap->fun); thread(&ap->fun);
return p; return p;
} }
STATIC_INLINE StgPtr STATIC_INLINE P_
thread_AP_STACK (StgAP_STACK *ap) thread_AP_STACK (StgAP_STACK *ap)
{ {
thread(&ap->fun); thread(&ap->fun);
...@@ -435,7 +424,7 @@ thread_AP_STACK (StgAP_STACK *ap) ...@@ -435,7 +424,7 @@ thread_AP_STACK (StgAP_STACK *ap)
return (P_)ap + sizeofW(StgAP_STACK) + ap->size; return (P_)ap + sizeofW(StgAP_STACK) + ap->size;
} }
static StgPtr static P_
thread_TSO (StgTSO *tso) thread_TSO (StgTSO *tso)
{ {
thread_(&tso->_link); thread_(&tso->_link);
...@@ -455,24 +444,21 @@ thread_TSO (StgTSO *tso) ...@@ -455,24 +444,21 @@ thread_TSO (StgTSO *tso)
thread_(&tso->trec); thread_(&tso->trec);
thread_(&tso->stackobj); thread_(&tso->stackobj);
return (StgPtr)tso + sizeofW(StgTSO); return (P_)tso + sizeofW(StgTSO);
} }
static void static void
update_fwd_large( bdescr *bd ) update_fwd_large( bdescr *bd )
{ {
StgPtr p;
const StgInfoTable* info;
for (; bd != NULL; bd = bd->link) { for (; bd != NULL; bd = bd->link) {
// nothing to do in a pinned block; it might not even have an object // nothing to do in a pinned block; it might not even have an object
// at the beginning. // at the beginning.
if (bd->flags & BF_PINNED) continue; if (bd->flags & BF_PINNED) continue;
p = bd->start; P_ p = bd->start;
info = get_itbl((StgClosure *)p); const StgInfoTable *info = get_itbl((StgClosure *)p);
switch (info->type) { switch (info->type) {
...@@ -502,9 +488,7 @@ update_fwd_large( bdescr *bd ) ...@@ -502,9 +488,7 @@ update_fwd_large( bdescr *bd )
case SMALL_MUT_ARR_PTRS_FROZEN_DIRTY: case SMALL_MUT_ARR_PTRS_FROZEN_DIRTY:
// follow everything // follow everything
{ {
StgSmallMutArrPtrs *a; StgSmallMutArrPtrs *a = (StgSmallMutArrPtrs*)p;
a = (StgSmallMutArrPtrs*)p;
for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) { for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) {
thread((StgClosure **)p); thread((StgClosure **)p);
} }
...@@ -528,11 +512,10 @@ update_fwd_large( bdescr *bd ) ...@@ -528,11 +512,10 @@ update_fwd_large( bdescr *bd )
case TREC_CHUNK: case TREC_CHUNK:
{ {
StgWord i;
StgTRecChunk *tc = (StgTRecChunk *)p; StgTRecChunk *tc = (StgTRecChunk *)p;
TRecEntry *e = &(tc -> entries[0]); TRecEntry *e = &(tc -> entries[0]);
thread_(&tc->prev_chunk); thread_(&tc->prev_chunk);
for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) { for (W_ i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
thread_(&e->tvar); thread_(&e->tvar);
thread(&e->expected_value); thread(&e->expected_value);
thread(&e->new_value); thread(&e->new_value);
...@@ -547,8 +530,8 @@ update_fwd_large( bdescr *bd ) ...@@ -547,8 +530,8 @@ update_fwd_large( bdescr *bd )
} }
// ToDo: too big to inline // ToDo: too big to inline
static /* STATIC_INLINE */ StgPtr static /* STATIC_INLINE */ P_
thread_obj (const StgInfoTable *info, StgPtr p) thread_obj (const StgInfoTable *info, P_ p)
{ {
switch (info->type) { switch (info->type) {
case THUNK_0_1: case THUNK_0_1:
...@@ -604,10 +587,7 @@ thread_obj (const StgInfoTable *info, StgPtr p) ...@@ -604,10 +587,7 @@ thread_obj (const StgInfoTable *info, StgPtr p)
case THUNK: case THUNK:
{ {
StgPtr end; P_ end = (P_)((StgThunk *)p)->payload + info->layout.payload.ptrs;
end = (P_)((StgThunk *)p)->payload +
info->layout.payload.ptrs;
for (p = (P_)((StgThunk *)p)->payload; p < end; p++) { for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
thread((StgClosure **)p); thread((StgClosure **)p);
} }
...@@ -625,10 +605,7 @@ thread_obj (const StgInfoTable *info, StgPtr p) ...@@ -625,10 +605,7 @@ thread_obj (const StgInfoTable *info, StgPtr p)
case BLACKHOLE: case BLACKHOLE:
case BLOCKING_QUEUE: case BLOCKING_QUEUE:
{ {
StgPtr end; P_ end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
end = (P_)((StgClosure *)p)->payload +
info->layout.payload.ptrs;
for (p = (P_)((StgClosure *)p)->payload; p < end; p++) { for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
thread((StgClosure **)p); thread((StgClosure **)p);
} }
...@@ -687,14 +664,12 @@ thread_obj (const StgInfoTable *info, StgPtr p) ...@@ -687,14 +664,12 @@ thread_obj (const StgInfoTable *info, StgPtr p)
case MUT_ARR_PTRS_FROZEN_DIRTY: case MUT_ARR_PTRS_FROZEN_DIRTY:
// follow everything // follow everything
{ {
StgMutArrPtrs *a; StgMutArrPtrs *a = (StgMutArrPtrs *)p;
a = (StgMutArrPtrs *)p;
for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) { for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) {