From 700d6ae707013dd73c9825fd316ae9e83220b60d Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 25 Jan 2019 13:54:02 +0000 Subject: [PATCH] fixes --- C/globals.c | 121 ++---------------- C/utilpreds.c | 269 +++++++++-------------------------------- C/write.c | 17 ++- regression/cyclics.yap | 4 +- 4 files changed, 77 insertions(+), 334 deletions(-) diff --git a/C/globals.c b/C/globals.c index 6d6e06d16..56aa0a41e 100644 --- a/C/globals.c +++ b/C/globals.c @@ -331,27 +331,6 @@ static void CloseArena(CELL *oldH, CELL *oldHB, CELL *oldASP, Term *oldArenaP, ASP = oldASP; } -static inline void clean_dirty_tr(tr_fr_ptr TR0 USES_REGS) { - if (TR != TR0) { - tr_fr_ptr pt = TR0; - - do { - Term p = TrailTerm(pt++); - if (IsVarTerm(p)) { - RESET_VARIABLE(p); - } else { - - /* copy downwards */ - TrailTerm(TR0 + 1) = TrailTerm(pt); - TrailTerm(TR0) = TrailTerm(TR0 + 2) = p; - pt += 2; - TR0 += 3; - } - } while (pt != TR); - TR = TR0; - } -} - static Term CopyTermToArena(Term t, Term arena, bool share, bool copy_att_vars, UInt arity, Term *newarena, size_t min_grow USES_REGS) { @@ -394,106 +373,20 @@ static Term CopyTermToArena(Term t, Term arena, bool share, bool copy_att_vars, return tn; } else if (IsAtomOrIntTerm(t)) { return t; - } else if (IsPairTerm(t)) { - Term tf; - CELL *ap; + } else { CELL *Hi; - if (share && ArenaPt(arena) > RepPair(t)) { - return t; - } + Hi = HR; + HR++; + oldH = HR; HR = HB = ArenaPt(arena); ASP = ArenaLimit(arena); - ap = RepPair(t); - Hi = HR; - tf = AbsPair(HR); - HR += 2; - if ((res = Yap_copy_complex_term(ap - 1, ap + 1, share, NULL, copy_att_vars, Hi, - Hi PASS_REGS)) < 0) { + if ((res = Yap_copy_complex_term(&t - 1, &t, share, NULL, copy_att_vars, Hi, + HR PASS_REGS)) < 0) { goto error_handler; } CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); - return tf; - } else { - Functor f; - Term tf; - CELL *HB0; - CELL *ap; - - if (share && ArenaPt(arena) > RepAppl(t)) { - return t; - } - HR = HB = ArenaPt(arena); - ASP = ArenaLimit(arena); - f = FunctorOfTerm(t); - HB0 = HR; - ap = RepAppl(t); - tf = AbsAppl(HR); - HR[0] = (CELL)f; - if (IsExtensionFunctor(f)) { - switch ((CELL)f) { - case (CELL) FunctorDBRef: - CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); - return t; - case (CELL) FunctorLongInt: - if (HR > ASP - (MIN_ARENA_SIZE + 3)) { - res = -1; - goto error_handler; - } - HR[1] = ap[1]; - HR[2] = EndSpecials; - HR += 3; - break; - case (CELL) FunctorDouble: - if (HR > ASP - (MIN_ARENA_SIZE + (2 + SIZEOF_DOUBLE / sizeof(CELL)))) { - res = -1; - goto error_handler; - } - HR[1] = ap[1]; -#if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P - HR[2] = ap[2]; - HR[3] = EndSpecials; - HR += 4; -#else - HR[2] = EndSpecials; - HR += 3; -#endif - break; - case (CELL) FunctorString: - if (HR > ASP - (MIN_ARENA_SIZE + 3 + ap[1])) { - res = -1; - goto error_handler; - } - memmove(HR, ap, sizeof(CELL) * (3 + ap[1])); - HR += ap[1] + 3; - break; - default: { - UInt sz = ArenaSz(t), i; - - if (HR > ASP - (MIN_ARENA_SIZE + sz)) { - res = -1; - goto error_handler; - } - for (i = 1; i < sz; i++) { - HR[i] = ap[i]; - } - HR += sz; - } - } - } else { - HR += 1 + ArityOfFunctor(f); - if (HR > ASP - MIN_ARENA_SIZE) { - res = -1; - goto error_handler; - } - if ((res = Yap_copy_complex_term(ap, ap + ArityOfFunctor(f), share, - NULL, copy_att_vars, HB0 + 1, HB0 PASS_REGS)) < - 0) { - goto error_handler; - } - } - CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); - return tf; + return Hi[0]; } error_handler: HR = HB; diff --git a/C/utilpreds.c b/C/utilpreds.c index 4c424d928..938aa8ca3 100644 --- a/C/utilpreds.c +++ b/C/utilpreds.c @@ -265,7 +265,6 @@ int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, continue; } *ptf = AbsPair(HR); - ptf++; if (to_visit >= to_visit_max-32) { expand_stack(to_visit0, to_visit, to_visit_max, struct cp_frame); } @@ -273,7 +272,7 @@ int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, to_visit->end_cp = pt0_end; to_visit->to = ptf; to_visit->curp = headp; - to_visit->oldv = head; + d0 = to_visit->oldv = head; to_visit->ground = ground; to_visit++; // move to new list @@ -468,7 +467,6 @@ int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, DO_TRAIL(ptd0, (CELL)ptf); *ptd0 = (CELL)ptf; ptf++; - continue; } } @@ -566,85 +564,27 @@ static Term CopyTerm(Term inp, UInt arity, int share, int newattvs USES_REGS) { Term t = Deref(inp); tr_fr_ptr TR0 = TR; - - if (IsVarTerm(t)) { -#if COROUTINING - if (newattvs && IsAttachedTerm(t)) { - CELL *Hi; - int res; - restart_attached: - - *HR = t; - Hi = HR+1; - HR += 2; - if ((res = Yap_copy_complex_term(Hi-2, Hi-1, share, NULL, newattvs, Hi, Hi PASS_REGS)) < 0) { - HR = Hi-1; - if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L) - return FALSE; - goto restart_attached; - } - return Hi[0]; - } -#endif - return MkVarTerm(); - } else if (IsPrimitiveTerm(t)) { - return t; - } else if (IsPairTerm(t)) { - Term tf; - CELL *ap; CELL *Hi; - restart_list: - ap = RepPair(t); + if (IsPrimitiveTerm(t)) { + return t; + } + while( true ) { + int res; Hi = HR; - tf = AbsPair(HR); - HR += 2; - { - int res; - if ((res = Yap_copy_complex_term(ap-1, ap+1, share, NULL, newattvs, Hi, Hi PASS_REGS)) < 0) { + HR ++; + + if ((res = Yap_copy_complex_term((&t)-1, &t, share, NULL, newattvs, Hi, HR PASS_REGS)) < 0) { HR = Hi; if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L) return FALSE; - goto restart_list; } else if (res && share) { HR = Hi; return t; } + return Hi[0]; } - return tf; - } else { - Functor f = FunctorOfTerm(t); - Term tf; - CELL *HB0; - CELL *ap; - - restart_appl: - f = FunctorOfTerm(t); - HB0 = HR; - ap = RepAppl(t); - tf = AbsAppl(HR); - HR[0] = (CELL)f; - HR += 1+ArityOfFunctor(f); - if (HR > ASP-128) { - HR = HB0; - if ((t = handle_cp_overflow(-1, TR0, arity, t))== 0L) - return FALSE; - goto restart_appl; - } else { - int res; - - if ((res = Yap_copy_complex_term(ap, ap+ArityOfFunctor(f), share, NULL, newattvs, HB0+1, HB0 PASS_REGS)) < 0) { - HR = HB0; - if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L) - return FALSE; - goto restart_appl; - } else if (res && share && FunctorOfTerm(t) != FunctorMutable) { - HR = HB0; - return t; - } - } - return tf; - } + return 0; } Term @@ -1866,25 +1806,9 @@ p_variables_in_term( USES_REGS1 ) /* variables in term t */ } do { Term t = Deref(ARG1); - if (IsVarTerm(t)) { - out = AbsPair(HR); - HR += 2; - RESET_VARIABLE(HR-2); - RESET_VARIABLE(HR-1); - Yap_unify((CELL)(HR-2),ARG1); - Yap_unify((CELL)(HR-1),ARG2); - } else if (IsPrimitiveTerm(t)) - out = ARG2; - else if (IsPairTerm(t)) { - out = vars_in_complex_term(RepPair(t)-1, - RepPair(t)+1, ARG2 PASS_REGS); - } - else { - Functor f = FunctorOfTerm(t); - out = vars_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), ARG2 PASS_REGS); - } + out = vars_in_complex_term(&(t)-1, + &(t), + ARG2 PASS_REGS); if (out == 0L) { if (!expand_vts( 3 PASS_REGS )) return FALSE; @@ -1898,6 +1822,7 @@ p_variables_in_term( USES_REGS1 ) /* variables in term t */ static Int p_term_variables( USES_REGS1 ) /* variables in term t */ { + Term t = Deref(ARG1); Term out; if (!Yap_IsListOrPartialListTerm(ARG2)) { @@ -1905,30 +1830,16 @@ p_term_variables( USES_REGS1 ) /* variables in term t */ return FALSE; } - do { - Term t = Deref(ARG1); - if (IsVarTerm(t)) { - Term out = Yap_MkNewPairTerm(); - return - Yap_unify(t,HeadOfTerm(out)) && - Yap_unify(TermNil, TailOfTerm(out)) && - Yap_unify(out, ARG2); - } else if (IsPrimitiveTerm(t)) { + if (IsPrimitiveTerm(t)) { return Yap_unify(TermNil, ARG2); - } else if (IsPairTerm(t)) { - out = vars_in_complex_term(RepPair(t)-1, - RepPair(t)+1, TermNil PASS_REGS); - } - else { - Functor f = FunctorOfTerm(t); - out = vars_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), TermNil PASS_REGS); - } - if (out == 0L) { - if (!expand_vts( 3 PASS_REGS )) - return FALSE; - } + } + do { + out = vars_in_complex_term(&(t)-1, + &(t), TermNil PASS_REGS); + if (out == 0L) { + if (!expand_vts( 3 PASS_REGS )) + return FALSE; + } } while (out == 0L); return Yap_unify(ARG2,out); } @@ -1948,17 +1859,11 @@ Yap_TermVariables( Term t, UInt arity USES_REGS ) /* variables in term t */ t = Deref(t); if (IsVarTerm(t)) { return MkPairTerm(t, TermNil); - } else if (IsPrimitiveTerm(t)) { + } else if (IsPrimitiveTerm(t)) { return TermNil; - } else if (IsPairTerm(t)) { - out = vars_in_complex_term(RepPair(t)-1, - RepPair(t)+1, TermNil PASS_REGS); - } - else { - Functor f = FunctorOfTerm(t); - out = vars_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), TermNil PASS_REGS); + } else { + out = vars_in_complex_term(&(t)-1, + &(t), TermNil PASS_REGS); } if (out == 0L) { if (!expand_vts( arity PASS_REGS )) @@ -2067,29 +1972,15 @@ p_term_attvars( USES_REGS1 ) /* variables in term t */ do { Term t = Deref(ARG1); - if (IsVarTerm(t)) { - out = attvars_in_complex_term(VarOfTerm(t)-1, - VarOfTerm(t), TermNil PASS_REGS); - } else if (IsPrimitiveTerm(t)) { - return Yap_unify(TermNil, ARG2); - } else if (IsPairTerm(t)) { - out = attvars_in_complex_term(RepPair(t)-1, - RepPair(t)+1, TermNil PASS_REGS); - } - else { - Functor f = FunctorOfTerm(t); - if (IsExtensionFunctor(f)) - return Yap_unify(TermNil, ARG2); - RepAppl(t)[0] = TermNil; - out = attvars_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), TermNil PASS_REGS); - RepAppl(t)[0] = (CELL)f; - } - if (out == 0L) { + if (IsPrimitiveTerm(t)) { + return Yap_unify(TermNil, ARG2); + } + out = attvars_in_complex_term(&(t)-1, + &(t), TermNil PASS_REGS); + if (out == 0L) { if (!expand_vts( 3 PASS_REGS )) - return FALSE; - } + return false; + } } while (out == 0L); return Yap_unify(ARG2,out); } @@ -2109,15 +2000,9 @@ p_term_variables3( USES_REGS1 ) /* variables in term t */ Yap_unify(out, ARG2); } else if (IsPrimitiveTerm(t)) { return Yap_unify(ARG2, ARG3); - } else if (IsPairTerm(t)) { - out = vars_in_complex_term(RepPair(t)-1, - RepPair(t)+1, ARG3 PASS_REGS); - } - else { - Functor f = FunctorOfTerm(t); - out = vars_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), ARG3 PASS_REGS); + } else { + out = vars_in_complex_term(&(t)-1, + &(t), ARG3 PASS_REGS); } if (out == 0L) { if (!expand_vts( 3 PASS_REGS )) @@ -2217,21 +2102,11 @@ p_variables_within_term( USES_REGS1 ) /* variables within term t */ do { Term t = Deref(ARG2); - if (IsVarTerm(t)) { - out = vars_within_complex_term(VarOfTerm(t)-1, - VarOfTerm(t), Deref(ARG1) PASS_REGS); - - } else if (IsPrimitiveTerm(t)) + if (IsPrimitiveTerm(t)) out = TermNil; - else if (IsPairTerm(t)) { - out = vars_within_complex_term(RepPair(t)-1, - RepPair(t)+1, Deref(ARG1) PASS_REGS); - } - else { - Functor f = FunctorOfTerm(t); - out = vars_within_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), Deref(ARG1) PASS_REGS); + else { + out = vars_within_complex_term(&(t)-1, + &(t), Deref(ARG1) PASS_REGS); } if (out == 0L) { if (!expand_vts( 3 PASS_REGS )) @@ -2335,21 +2210,11 @@ p_new_variables_in_term( USES_REGS1 ) /* variables within term t */ do { Term t = Deref(ARG2); - if (IsVarTerm(t)) { - out = new_vars_in_complex_term(VarOfTerm(t)-1, - VarOfTerm(t), Deref(ARG1) PASS_REGS); - - } else if (IsPrimitiveTerm(t)) + if (IsPrimitiveTerm(t)) out = TermNil; - else if (IsPairTerm(t)) { - out = new_vars_in_complex_term(RepPair(t)-1, - RepPair(t)+1, Deref(ARG1) PASS_REGS); - } - else { - Functor f = FunctorOfTerm(t); - out = new_vars_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), Deref(ARG1) PASS_REGS); + else { + out = new_vars_in_complex_term(&(t)-1, + &(t), Deref(ARG1) PASS_REGS); } if (out == 0L) { if (!expand_vts( 3 PASS_REGS )) @@ -2592,21 +2457,11 @@ p_free_variables_in_term( USES_REGS1 ) /* variables within term t */ } t = ArgOfTerm(2,t); } - if (IsVarTerm(t)) { - out = free_vars_in_complex_term(VarOfTerm(t)-1, - VarOfTerm(t), TR0 PASS_REGS); - - } else if (IsPrimitiveTerm(t)) + if (IsPrimitiveTerm(t)) out = TermNil; - else if (IsPairTerm(t)) { - out = free_vars_in_complex_term(RepPair(t)-1, - RepPair(t)+1, TR0 PASS_REGS); - } else { - Functor f = FunctorOfTerm(t); - out = free_vars_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(f), TR0 PASS_REGS); + out = free_vars_in_complex_term(&(t)-1, + &(t), TR0 PASS_REGS); } if (out == 0L) { trail_overflow: @@ -2705,13 +2560,9 @@ p_non_singletons_in_term( USES_REGS1 ) /* non_singletons in term t */ out = ARG2; } else if (IsPrimitiveTerm(t)) { out = ARG2; - } else if (IsPairTerm(t)) { - out = non_singletons_in_complex_term(RepPair(t)-1, - RepPair(t)+1 PASS_REGS); } else { - out = non_singletons_in_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(FunctorOfTerm(t)) PASS_REGS); + out = non_singletons_in_complex_term(&(t)-1, + &(t) PASS_REGS); } if (out != 0L) { return Yap_unify(ARG3,out); @@ -2785,22 +2636,11 @@ bool Yap_IsGroundTerm(Term t) return FALSE; } else if (IsPrimitiveTerm(t)) { return TRUE; - } else if (IsPairTerm(t)) { - if ((out =ground_complex_term(RepPair(t)-1, - RepPair(t)+1 PASS_REGS)) >= 0) { - return out != 0; - } } else { - Functor fun = FunctorOfTerm(t); - - if (IsExtensionFunctor(fun)) - return TRUE; - else if ((out = ground_complex_term(RepAppl(t), - RepAppl(t)+ - ArityOfFunctor(fun) PASS_REGS)) >= 0) { + if ((out =ground_complex_term(&(t)-1, + &(t) PASS_REGS)) >= 0) { return out != 0; } - } if (out < 0) { *HR++ = t; if (!Yap_ExpandPreAllocCodeSpace(0, NULL, TRUE)) { @@ -2810,6 +2650,7 @@ bool Yap_IsGroundTerm(Term t) t = *--HR; } } + } } static Int diff --git a/C/write.c b/C/write.c index d61217857..4849bbf24 100644 --- a/C/write.c +++ b/C/write.c @@ -105,6 +105,7 @@ static bool callPortray(Term t, int sno USES_REGS) { #define PROTECT(t, F) \ { \ F; \ + t = Yap_GetFromSlot(wglb->sl); \ } static void wrputn(Int, struct write_globs *); static void wrputf(Float, struct write_globs *); @@ -703,7 +704,9 @@ static void write_var(CELL *t, struct write_globs *wglb, wrputs("$AT(", wglb->stream); write_var(t, wglb, rwt); wrputc(',', wglb->stream); - PROTECT(*t, writeTerm(*l, 999, 1, FALSE, wglb, &nrwt)); + CELL tt = (CELL)t; + PROTECT(tt, writeTerm(*l, 999, 1, FALSE, wglb, &nrwt)); + t = (CELL *)tt; attv = RepAttVar(t); wrputc(',', wglb->stream); l++; @@ -756,6 +759,8 @@ static void write_list__(Term t, yhandle_t sl, int direction, int depth, break; if (!IsPairTerm(ti)) break; + if (check_for_loops(ti,wglb)) return; + wglb->sl = Yap_InitHandle(ti); ndirection = RepPair(ti) - RepPair(t); /* make sure we're not trapped in loops */ if (ndirection > 0) { @@ -806,6 +811,7 @@ static void write_list(Term t, int direction, int depth, write_list__(t, sl, direction, depth, wglb, rwt); Yap_PopHandle(sl); + wglb->sl = sl-1; } @@ -849,7 +855,7 @@ static void writeTerm__(Term t, yhandle_t sl, int p, int depth, int rinfixarg, wrputc('[', wglb->stream); lastw = separator; /* we assume t was already saved in the stack */ - write_list(t, 0, depth, wglb, rwt); + write_list__(t, wglb->sl, 0, depth, wglb, rwt); wrputc(']', wglb->stream); lastw = separator; } @@ -1125,7 +1131,8 @@ static void writeTerm(Term t, int p, int depth, int rinfixarg, yhandle_t sl = wglb->sl = Yap_InitHandle(t); writeTerm__(t, sl, p, depth, rinfixarg, wglb, rwt); - Yap_PopHandle(sl); + Yap_PopHandle(sl); + wglb->sl = sl-1; } void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags, @@ -1176,9 +1183,9 @@ void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags, /* protect slots for portray */ yhandle_t sl; wglb.sl0 = (sl = wglb.sl = Yap_InitHandle(t)) -1; - wglb.protectedEntry = false; \ + wglb.protectedEntry = false; writeTerm(t, priority, 1, FALSE, &wglb, &rwt); - t = Yap_PopHandle(sl); \ + t = Yap_PopHandle(sl); if (flags & New_Line_f) { if (flags & Fullstop_f) { wrputc('.', wglb.stream); diff --git a/regression/cyclics.yap b/regression/cyclics.yap index de0981bf7..cc04e8eb2 100644 --- a/regression/cyclics.yap +++ b/regression/cyclics.yap @@ -1,11 +1,13 @@ :- X = [], copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X = [_A], copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). -:- X = [a,A], copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). +:- X = [a,_A], copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X = [X], copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X = [_|X], copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X= f(X), copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X= f(X,X), copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X= f(_,X), copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). +:- X= f(A,A,X), copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). +:- X= f(A,g(X,[A|A]),X), copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X= f(X,[X,X]), copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X= f(X,[X,g(X)]), copy_term(X,Y), writeln('....'), writeln(X), writeln(Y). :- X=f(_,X/[X]),copy_term(X,Y), writeln('....'),writeln(X),writeln(Y). \ No newline at end of file