fixes to support copy_term and nb_

This commit is contained in:
Vitor Santos Costa 2010-03-12 08:24:58 +00:00
parent 3d10482cc7
commit 30a4f3cfe7
3 changed files with 89 additions and 62 deletions

View File

@ -96,7 +96,7 @@ CopyAttVar(CELL *orig, struct cp_frame **to_visit_ptr, CELL *res)
to_visit->start_cp = vt-1;
to_visit->end_cp = vt;
if (IsVarTerm(attv->Atts)) {
newv->Atts = (CELL)H;
Bind(&newv->Atts, (CELL)H);
to_visit->to = H;
H++;
} else {
@ -123,7 +123,7 @@ TermToAttVar(Term attvar, Term to)
attvar_record *attv = BuildNewAttVar();
if (!attv)
return FALSE;
attv->Atts = attvar;
Bind(&attv->Atts, attvar);
*VarOfTerm(to) = AbsAttVar(attv);
return TRUE;
}
@ -254,11 +254,7 @@ AddNewModule(attvar_record *attv, Term t, int new, int do_it)
if (!do_it)
return;
if (IsVarTerm(attv->Atts)) {
if (new) {
attv->Atts = t;
} else {
Bind(&(attv->Atts),t);
}
Bind(&(attv->Atts),t);
} else {
Term *wherep = &attv->Atts;
@ -480,7 +476,7 @@ p_put_att_term(void) {
}
if (new) {
Bind(VarOfTerm(inp), AbsAttVar(attv));
attv->Atts = Deref(ARG2);
Bind(&attv->Atts, Deref(ARG2));
} else {
MaBind(&(attv->Atts), Deref(ARG2));
}

View File

@ -224,12 +224,17 @@ Yap_GetFromArena(Term *arenap, UInt cells, UInt arity)
CELL *newH;
UInt old_sz = ArenaSz(arena), new_size;
if (IN_BETWEEN(base, H, max)) {
base = H;
H += cells;
return base;
}
if (base+cells > ASP-1024) {
if (!GrowArena(arena, max, old_sz, old_sz+sizeof(CELL)*1024, arity))
return NULL;
goto restart;
}
newH = base+cells;
new_size = old_sz - cells;
*arenap = CreateNewArena(newH, new_size);
@ -280,6 +285,9 @@ copy_complex_term(register CELL *pt0, register CELL *pt0_end, int share, int cop
CELL *HB0 = HB;
tr_fr_ptr TR0 = TR;
int ground = TRUE;
#ifdef COROUTINING
CELL *dvarsmin = NULL, *dvarsmax=NULL;
#endif
HB = HLow;
to_visit0 = to_visit;
@ -455,52 +463,50 @@ copy_complex_term(register CELL *pt0, register CELL *pt0_end, int share, int cop
*ptf++ = (CELL) ptd0;
} else {
#if COROUTINING
if (IsAttVar(ptd0) && copy_att_vars) {
attvar_record *newv = (attvar_record *)H;
newv->AttFunc = FunctorAttVar;
RESET_VARIABLE(&newv->Done);
*ptf = AbsAttVar(newv);
ptf++;
/* store the terms to visit */
#ifdef RATIONAL_TREES
if (to_visit+1 >= (struct cp_frame *)AuxSp) {
goto heap_overflow;
}
to_visit->start_cp = pt0;
to_visit->end_cp = pt0_end;
to_visit->to = ptf;
to_visit->oldv = *pt0;
to_visit->ground = ground;
/* fool the system into thinking we had a variable there */
*pt0 = AbsAppl(H);
to_visit ++;
#else
if (pt0 < pt0_end) {
if (to_visit ++ >= (CELL **)AuxSp) {
goto heap_overflow;
if (copy_att_vars && IsAttachedTerm((CELL)ptd0)) {
/* if unbound, call the standard copy term routine */
struct cp_frame *bp;
if (IN_BETWEEN(dvarsmin, ptd0, dvarsmax)) {
*ptf++ = (CELL) ptd0;
} else {
CELL new;
bp = to_visit;
if (!attas[ExtFromCell(ptd0)].copy_term_op(ptd0, &bp, ptf)) {
goto overflow;
}
to_visit->start_cp = pt0;
to_visit->end_cp = pt0_end;
to_visit->to = ptf;
to_visit->ground = ground;
to_visit ++;
}
#endif
pt0 = ptd0+(1-1);
pt0_end = ptd0 + (ATT_RECORD_ARITY-1);
/* store the functor for the new term */
ptf = H+2;
H = CellPtr(newv+1);
if (H > ASP - MIN_ARENA_SIZE) {
goto overflow;
to_visit = bp;
new = *ptf;
if (TR > (tr_fr_ptr)Yap_TrailTop - 256) {
/* Trail overflow */
if (!Yap_growtrail((TR-TR0)*sizeof(tr_fr_ptr *), TRUE)) {
goto trail_overflow;
}
}
Bind_and_Trail(ptd0, new);
if (dvarsmin == NULL) {
dvarsmin = CellPtr(new);
} else {
*dvarsmax = (CELL)(CellPtr(new)+1);
}
dvarsmax = CellPtr(new)+1;
ptf++;
}
} else {
*ptf++ = d0;
#endif
/* first time we met this term */
RESET_VARIABLE(ptf);
if ((ADDR)TR > Yap_TrailTop-MIN_ARENA_SIZE)
goto trail_overflow;
Bind_and_Trail(ptd0, (CELL)ptf);
ptf++;
#ifdef COROUTINING
}
continue;
#endif
}
}
/* Do we still have compound terms to visit */
if (to_visit > to_visit0) {
to_visit --;
@ -554,6 +560,24 @@ copy_complex_term(register CELL *pt0, register CELL *pt0_end, int share, int cop
#endif
reset_trail(TR0);
return -2;
trail_overflow:
/* oops, we're in trouble */
H = HLow;
/* we've done it */
/* restore our nice, friendly, term to its original state */
HB = HB0;
#ifdef RATIONAL_TREES
while (to_visit > to_visit0) {
to_visit--;
pt0 = to_visit->start_cp;
pt0_end = to_visit->end_cp;
ptf = to_visit->to;
*pt0 = to_visit->oldv;
}
#endif
reset_trail(TR0);
return -4;
}
static Term

View File

@ -65,18 +65,7 @@ clean_dirty_tr(tr_fr_ptr TR0) {
do {
Term p = TrailTerm(pt++);
if (IsVarTerm(p)) {
RESET_VARIABLE(p);
} else {
/* copy downwards */
#ifdef FROZEN_STACKS
#else
TrailTerm(TR0+1) = TrailTerm(pt);
TrailTerm(TR0) = TrailTerm(TR0+2) = p;
#endif
pt+=2;
TR0 += 3;
}
RESET_VARIABLE(p);
} while (pt != TR);
TR = TR0;
}
@ -270,6 +259,8 @@ copy_complex_term(CELL *pt0, CELL *pt0_end, int share, int newattvs, CELL *ptf,
Bind(ptd0, new);
if (dvarsmin == NULL) {
dvarsmin = CellPtr(new);
} else {
*dvarsmax = (CELL)(CellPtr(new)+1);
}
dvarsmax = CellPtr(new)+1;
ptf++;
@ -316,7 +307,23 @@ copy_complex_term(CELL *pt0, CELL *pt0_end, int share, int newattvs, CELL *ptf,
/* restore our nice, friendly, term to its original state */
clean_dirty_tr(TR0);
HB = HB0;
/* follow chain of multi-assigned variables */
if (dvarsmin) {
fprintf(stderr,"%ld--%ld\n", dvarsmin-H0,dvarsmax-H0);
dvarsmin += 1;
do {
CELL *newv;
fprintf(stderr,"mabind %ld %p %p\n", dvarsmin-H0, TR, dvarsmin+1);
Bind(dvarsmin+1, dvarsmin[1]);
fprintf(stderr,"redone %p\n", TR);
if (IsUnboundVar(dvarsmin))
break;
newv = CellPtr(*dvarsmin);
RESET_VARIABLE(dvarsmin);
dvarsmin = newv;
} while (TRUE);
HB = HB0;
}
return ground;
overflow:
@ -516,7 +523,7 @@ Yap_CopyTermNoShare(Term inp) {
static Int
p_copy_term(void) /* copy term t to a new instance */
{
v Term t = CopyTerm(ARG1, 2, TRUE, TRUE);
Term t = CopyTerm(ARG1, 2, TRUE, TRUE);
if (t == 0L)
return FALSE;
/* be careful, there may be a stack shift here */