112c112
<   if ((ADDR)x > Yap_TrailTop-1024)
---
>   if ((ADDR)x > Yap_TrailTop-1024) {
113a114,117
> #if USE_SYSTEM_MALLOC
>     jump;
> #endif    
>   }
138c142
< static CELL_PTR *iptop;
---
> static CELL_PTR *iptop, *iptop_base;
288c292
<   if ((char *)gc_ma_h_top > Yap_TrailTop-1024)
---
>   if ((char *)gc_ma_h_top > Yap_TrailTop-1024) {
289a294,297
> #if USE_SYSTEM_MALLOC
> 	    jump;
> #endif    
>   }
480d487
<   CODEADDR lim;
905a913
> 
1025c1033
<       else if (IsUnboundVar(current)) vars[gc_var]++;
---
>       else if (IsUnboundVar((CELL)current)) vars[gc_var]++;
1092c1100,1101
< 	    *current = MARK_CELL((CELL)current);
---
> 	    *current = (CELL)current;
> 	    MARK(current);
1103a1113
> 	    UNMARK(next);
1118c1128,1129
< 	*current = UNMARK_CELL(cnext);
---
> 	*current = cnext;
> 	UNMARK(next);
1189a1201,1204
> #if GC_NO_TAGS
> 	MARK(next+1);
> 	MARK(next+2);
> #endif
1195a1211
> 	printf("marking %p: %x\n", next, *next);
1200a1217,1218
> 	MARK(next+1);
> 	MARK(next+2);
1202a1221,1223
> #if GC_NO_TAGS
> 	MARK(next+3);
> #endif
1217c1238,1241
< 	       i++)
---
> 	       i++) {
> #if GC_NO_TAGS
> 	    MARK(next+i);
> #endif
1218a1243,1246
> 	  }
> #if GC_NO_TAGS
> 	  MARK(next+i);
> #endif
1527c1555
< 	  if ((ADDR)nsTR > Yap_TrailTop-1024)
---
> 	  if ((ADDR)nsTR > Yap_TrailTop-1024) {
1528a1557,1560
> #if USE_SYSTEM_MALLOC
> 	    jump;
> #endif    
> 	  }
1687,1689c1719,1763
<       PredEntry *pe = Yap_PredForChoicePt(gc_B);
< 
<       if (pe == NULL) {
---
>       switch (opnum) {
>       case _retry_c:
>       case _or_else:
>       case _or_last:
>       case _Nstop:
>       case _retry_userc:
>       case _trust_logical_pred:
>       case _retry_profiled:
>       case _count_retry:
> 	{
> 	  Atom at;
> 	  UInt arity;
> 	  Term mod;
> 	  if (Yap_PredForCode(gc_B->cp_ap, &at, &arity, &mod)) {
> 	    if (arity) 
> 	      fprintf(Yap_stderr,"%%       %s/%ld marked %ld (%s)\n", RepAtom(at)->StrOfAE, (long int)arity, total_marked, op_names[opnum]);
> 	    else
> 	      fprintf(Yap_stderr,"%%       %s marked %ld (%s)\n", RepAtom(at)->StrOfAE, total_marked, op_names[opnum]);
> 	  } else
> 	    fprintf(Yap_stderr,"%%       marked %ld (%s)\n", total_marked, op_names[opnum]);
> 	}
> 	break;
> #ifdef TABLING
>       case _table_completion:
>       case _table_answer_resolution:
> 	{
> 	  PredEntry *pe = ENV_ToP(gc_B->cp_cp);
> 	  op_numbers caller_op = Yap_op_from_opcode(ENV_ToOp(gc_B->cp_cp));
> 	  /* first condition  checks if this was a meta-call */
> 	  if ((caller_op != _call  && caller_op != _fcall) || pe == NULL) {
> 	    fprintf(Yap_stderr,"%%       marked %ld (%s)\n", total_marked, op_names[opnum]);
> 	  } else
> 	    fprintf(Yap_stderr,"%%       %s/%d marked %ld (%s)\n", RepAtom(NameOfFunctor(pe->FunctorOfPred))->StrOfAE, pe->ArityOfPE, total_marked, op_names[opnum]);
> 	}
> 	break;
>       case _trie_retry_var:
>       case _trie_trust_var:
>       case _trie_retry_val:
>       case _trie_trust_val:
>       case _trie_retry_atom:
>       case _trie_trust_atom:
>       case _trie_retry_list:
>       case _trie_trust_list:
>       case _trie_retry_struct:
>       case _trie_trust_struct:
1691,1694c1765,1777
<       } else if (pe->ArityOfPE) {
< 	  fprintf(Yap_stderr,"%%       %s/%d marked %ld (%s)\n", RepAtom(NameOfFunctor(pe->FunctorOfPred))->StrOfAE, pe->ArityOfPE, total_marked, op_names[opnum]);
<       } else {
< 	  fprintf(Yap_stderr,"%%       %s marked %ld (%s)\n", RepAtom((Atom)(pe->FunctorOfPred))->StrOfAE, total_marked, op_names[opnum]);
---
> 	break;
> #endif
>       default:
> 	{
> 	  PredEntry *pe = (PredEntry *)gc_B->cp_ap->u.ld.p;
> 	  if (pe == NULL) {
> 	    fprintf(Yap_stderr,"%%       marked %ld (%s)\n", total_marked, op_names[opnum]);
> 	  } else
> 	    if (pe->ArityOfPE)
> 	      fprintf(Yap_stderr,"%%       %s/%d marked %ld (%s)\n", RepAtom(NameOfFunctor(pe->FunctorOfPred))->StrOfAE, pe->ArityOfPE, total_marked, op_names[opnum]);
> 	    else
> 	      fprintf(Yap_stderr,"%%       %s marked %ld (%s)\n", RepAtom((Atom)(pe->FunctorOfPred))->StrOfAE, total_marked, op_names[opnum]);
> 	}
1890,1898d1972
<       case _retry2:
< 	nargs = 2;
< 	break;
<       case _retry3:
< 	nargs = 3;
< 	break;
<       case _retry4:
< 	nargs = 4;
< 	break;
1962a2037,2040
> #if GC_NO_TAGS
>   RMARK(next);
>   *current = UNMARKED(cnext);
> #endif
1963a2042,2044
> #if GC_NO_TAGS
>     *next = current;
> #else
1966a2048
> #endif
1967a2050,2052
> #if GC_NO_TAGS
>     *next = AbsPair((CELL *)current);
> #else
1971a2057
> #endif
1972a2059,2061
> #if GC_NO_TAGS
>     *next = AbsAppl((CELL *)current);
> #else
1976a2066
> #endif
1983a2074,2082
> #if GC_NO_TAGS
>   if (RMARKED(next))
>     RMARK(current);
>   else
>     UNRMARK(current);
>   *current = *next;
>   *next = (CELL) current | current_tag;
>   RMARK(next);
> #else
1989a2089
> #endif /* GC_NO_TAGS */
2184,2190d2283
< 	/* be sure we don't overwrite before we read */
< 	int marked_ptr = MARKED_PTR(&TrailTerm(trail_ptr+2));
< 	int marked_old = MARKED_PTR(&TrailTerm(trail_ptr+1));
< #ifdef FROZEN_STACKS
< 	int marked_val_old = MARKED_PTR(&TrailVal(trail_ptr+1));
< 	int marked_val_ptr = MARKED_PTR(&TrailVal(trail_ptr+2));
< #endif
2192,2194c2285,2287
< 	if (marked_ptr) 
< 	  ptr = RepAppl(UNMARK_CELL(trail_cell));
< 	else
---
> 	if (MARKED_PTR(&TrailTerm(trail_ptr+2))) {
> 	  ptr = RepAppl(UNMARK_CELL(TrailTerm(trail_ptr+2)));
> 	} else {
2195a2289
> 	}
2198c2292,2293
< 	if (marked_old) {
---
> 	TrailTerm(dest+2) = TrailTerm(dest) = trail_cell;
> 	if (MARKED_PTR(&TrailTerm(trail_ptr+1))) {
2204d2298
< 	TrailTerm(dest+2) = TrailTerm(dest) = trail_cell;
2207c2301
< 	if (marked_val_old) {
---
> 	if (MARKED_PTR(&TrailVal(dest+1))) {
2214c2308
< 	if (marked_val_ptr) {
---
> 	if (MARKED_PTR(&TrailVal(dest+2))) {
2221c2315
< 	if (marked_ptr) {
---
> 	if (MARKED_PTR(&TrailTerm(trail_ptr+2))) {
2373,2395d2466
< static void
< sweep_b(choiceptr gc_B, UInt arity)
< {
<   register CELL_PTR saved_reg;
< 
<   sweep_environments(gc_B->cp_env,
< 		     EnvSize((CELL_PTR) (gc_B->cp_cp)),
< 		     EnvBMap((CELL_PTR) (gc_B->cp_cp)));
< 
<   /* for each saved register */
<   for (saved_reg = &gc_B->cp_a1;
<        saved_reg < &gc_B->cp_a1 + arity;
<        saved_reg++) {
<     CELL cp_cell = *saved_reg;
<     if (MARKED_PTR(saved_reg)) {
<       UNMARK(saved_reg);
<       if (HEAP_PTR(cp_cell)) {
< 	into_relocation_chain(saved_reg, GET_NEXT(cp_cell));
<       }
<     }
<   }
< }
< 
2611c2682
< 	      if (MARKED_PTR(aux_ptr)) {
---
> 	      if (MARKED(aux_ptr)) {
2621c2692
< 	      if (MARKED_PTR(aux_ptr)) {
---
> 	      if (MARKED(aux_ptr)) {
2632,2640d2702
<     case _retry2:
<       sweep_b(gc_B, 2);
<       break;
<     case _retry3:
<       sweep_b(gc_B, 3);
<       break;
<     case _retry4:
<       sweep_b(gc_B, 4);
<       break;
2661c2723,2742
<       sweep_b(gc_B,rtp->u.ld.s);
---
>       {
> 	register CELL_PTR saved_reg;
> 
> 	sweep_environments(gc_B->cp_env,
> 			   EnvSize((CELL_PTR) (gc_B->cp_cp)),
> 			   EnvBMap((CELL_PTR) (gc_B->cp_cp)));
> 
> 	/* for each saved register */
> 	for (saved_reg = &gc_B->cp_a1;
> 	     saved_reg < &gc_B->cp_a1 + rtp->u.ld.s;
> 	     saved_reg++) {
> 	  CELL cp_cell = *saved_reg;
> 	  if (MARKED_PTR(saved_reg)) {
> 	    UNMARK(saved_reg);
> 	    if (HEAP_PTR(cp_cell)) {
> 	      into_relocation_chain(saved_reg, GET_NEXT(cp_cell));
> 	    }
> 	  }
> 	}
>       }
2713c2794,2800
<   while (RMARKED(ccur)) {
---
>   while (
> #if GC_NO_TAGS
> 	 RMARKED(current)
> #else
> 	 RMARKED(ccur)
> #endif /* GC_NO_TAGS */
> 		 ) {
2716a2804,2812
> #if GC_NO_TAGS
>     ccur = *current = *next;
>     if (RMARKED(next))
>       RMARK(current);
>     else
>       UNRMARK(current);
>     *next = (CELL) dest | current_tag;
>     UNRMARK(next);
> #else
2722a2819
> #endif /* GC_NO_TAGS */
2798a2896
> 	    MARK(ptr+1);
2829c2927
< 	else if (current == next)	/* cell pointing to
---
> 	else if (current == next) {	/* cell pointing to
2830a2929,2932
> #if GC_NO_TAGS
> 	  UNRMARK(current);
> 	  *current = (CELL) dest;	/* no tag */
> #else
2831a2934,2935
> #endif
> 	}
2926c3030
<   CELL_PTR *top = iptop-1, *base = (CELL_PTR *)H;
---
>   CELL_PTR *top = iptop-1, *base = (CELL_PTR *)iptop_base;
2981c3085
<   CELL_PTR *iptr, *ibase = (CELL_PTR *)H;
---
>   CELL_PTR *iptr, *ibase = iptop_base;
3028a3133,3135
> #if GC_NO_TAGS
> 	*current = (CELL) (H0+(iptr-ibase));	/* no tag */
> #else
3029a3137
> #endif
3194,3195c3302,3303
<       != iptop-(CELL_PTR *)H && iptop < (CELL_PTR *)ASP -1024)
<     fprintf(Yap_stderr,"%% Oops on iptop-H (%ld) vs %ld\n", (unsigned long int)(iptop-(CELL_PTR *)H), total_marked);
---
>       != iptop-(CELL_PTR *)iptop_base && iptop < (CELL_PTR *)ASP -1024)
>     fprintf(Yap_stderr,"%% Oops on iptop-iptop_base (%ld) vs %ld\n", (unsigned long int)(iptop-(CELL_PTR *)iptop_base), total_marked);
3202c3310
<     quicksort((CELL_PTR *)H, 0, (iptop-(CELL_PTR *)H)-1);
---
>     quicksort(iptop_base, 0, (iptop-(CELL_PTR *)iptop_base)-1);
3211c3319
<       fprintf(stderr,"%% not using pointers (%d) ASP: %p, ip %p (expected %p) \n", effectiveness, ASP, iptop, H+total_marked);
---
>       fprintf(stderr,"%% not using pointers (%d) ASP: %p, ip %p (expected %p) \n", effectiveness, ASP, iptop, iptop_base+total_marked);
3269a3378
> #if !GC_NO_TAGS
3278a3388
> #endif
3321,3323c3431
< #ifdef HYBRID_SCHEME
<   iptop = (CELL_PTR *)H;
< #endif
---
>   iptop = iptop_base = (CELL_PTR *)H;
3336c3444,3445
<   } else
---
>   }
>   else
