term to term

This commit is contained in:
Vitor Santos Costa 2019-01-23 14:31:31 +00:00
parent a6d709dabf
commit 2a090f3484
5 changed files with 251 additions and 217 deletions

View File

@ -374,7 +374,7 @@ static Term CopyTermToArena(Term t, Term arena, bool share, bool copy_att_vars,
*HR = t; *HR = t;
Hi = HR + 1; Hi = HR + 1;
HR += 2; HR += 2;
if ((res = Yap_copy_complex_term(Hi - 2, Hi - 1, share, copy_att_vars, Hi, if ((res = Yap_copy_complex_term(Hi - 2, Hi - 1, share, NULL, copy_att_vars, Hi,
Hi PASS_REGS)) < 0) Hi PASS_REGS)) < 0)
goto error_handler; goto error_handler;
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS);
@ -408,7 +408,7 @@ static Term CopyTermToArena(Term t, Term arena, bool share, bool copy_att_vars,
Hi = HR; Hi = HR;
tf = AbsPair(HR); tf = AbsPair(HR);
HR += 2; HR += 2;
if ((res = Yap_copy_complex_term(ap - 1, ap + 1, share, copy_att_vars, Hi, if ((res = Yap_copy_complex_term(ap - 1, ap + 1, share, NULL, copy_att_vars, Hi,
Hi PASS_REGS)) < 0) { Hi PASS_REGS)) < 0) {
goto error_handler; goto error_handler;
} }
@ -487,7 +487,7 @@ static Term CopyTermToArena(Term t, Term arena, bool share, bool copy_att_vars,
goto error_handler; goto error_handler;
} }
if ((res = Yap_copy_complex_term(ap, ap + ArityOfFunctor(f), share, if ((res = Yap_copy_complex_term(ap, ap + ArityOfFunctor(f), share,
copy_att_vars, HB0 + 1, HB0 PASS_REGS)) < NULL, copy_att_vars, HB0 + 1, HB0 PASS_REGS)) <
0) { 0) {
goto error_handler; goto error_handler;
} }

View File

@ -184,13 +184,13 @@ clean_dirty_tr(tr_fr_ptr TR0 USES_REGS) {
#define MIN_ARENA_SIZE (1048L) #define MIN_ARENA_SIZE (1048L)
int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end,
bool share, bool copy_att_vars, CELL *ptf, bool share, Term *split, bool copy_att_vars, CELL *ptf,
CELL *HLow USES_REGS) { CELL *HLow USES_REGS) {
// fprintf(stderr,"+++++++++\n"); // fprintf(stderr,"+++++++++\n");
//CELL *x = pt0; while(x != pt0_end) Yap_DebugPlWriteln(*++ x); //CELL *x = pt0; while(x != pt0_end) Yap_DebugPlWriteln(*++ x);
int lvl = push_text_stack(); int lvl = push_text_stack();
Term o = TermNil;
struct cp_frame *to_visit0, struct cp_frame *to_visit0,
*to_visit = Malloc(1024*sizeof(struct cp_frame)); *to_visit = Malloc(1024*sizeof(struct cp_frame));
struct cp_frame *to_visit_max; struct cp_frame *to_visit_max;
@ -214,229 +214,264 @@ int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end,
copy_term_nvar : { copy_term_nvar : {
if (IsPairTerm(d0)) { if (IsPairTerm(d0)) {
CELL *headp = RepPair(d0); CELL *headp = RepPair(d0);
if (//(share && headp < HB) || if (IsPairTerm(*headp) && RepPair(*headp) >= HB && RepPair(*headp) < HR) {
(IsPairTerm(*headp) && RepPair(*headp) >= HB && RepPair(*headp) < HR)) { if (split) {
Term v = Yap_MkNewApplTerm(FunctorEq, 2);
RepAppl(v)[1] = *headp;
*headp = *ptf++ = RepAppl(v)[0];
o = MkPairTerm( v, o );
} else {
/* If this is newer than the current term, just reuse */
*ptf++ = (CELL)RepAppl(*headp);
}
}
else if (IsApplTerm(*headp) && RepAppl(*headp) >= HB && RepAppl(*headp) < HR) {
*ptf++ = AbsPair(RepAppl(*headp));
continue;
}
if (to_visit >= to_visit_max-32) {
expand_stack(to_visit0, to_visit, to_visit_max, struct cp_frame);
}
*ptf = AbsPair(HR);
ptf++;
to_visit->start_cp = pt0;
to_visit->end_cp = pt0_end;
to_visit->to = ptf;
to_visit->curp = headp;
d0 = *headp;
to_visit->oldv = d0;
to_visit->ground = ground;
to_visit++;
// move to new list
if (share) {
TrailedMaBind(headp,AbsPair(HR));
} else {
*headp = AbsPair(HR);
}
pt0 = headp;
pt0_end = headp + 1;
ptf = HR;
ground = true;
HR += 2;
if (HR > ASP - MIN_ARENA_SIZE) {
goto overflow;
}
ptd0 = pt0;
goto deref;
} else if (IsApplTerm(d0)) {
register Functor f;
register CELL *headp;
/* store the terms to visit */
headp = RepAppl(d0);
if (IsPairTerm(*headp)//(share && headp < HB) ||
) {
if (split) {
Term v = Yap_MkNewApplTerm(FunctorEq, 2);
RepAppl(v)[1] = *headp;
*headp = *ptf++ = RepAppl(v)[0];
o = MkPairTerm( v, o );
} else {
/* If this is newer than the current term, just reuse */ /* If this is newer than the current term, just reuse */
*ptf++ = *headp; *ptf++ = AbsPair(RepAppl(*headp));
continue;
} }
if (to_visit >= to_visit_max-32) { continue;
expand_stack(to_visit0, to_visit, to_visit_max, struct cp_frame); }
} f = (Functor)(*headp);
*ptf = AbsPair(HR);
ptf++;
to_visit->start_cp = pt0;
to_visit->end_cp = pt0_end;
to_visit->to = ptf;
to_visit->ground = ground;
to_visit++;
// move to new list
d0 = *headp;
TrailedMaBind(headp, AbsPair(HR));
pt0 = headp;
pt0_end = headp + 1;
ptf = HR;
ground = true;
HR += 2;
if (HR > ASP - MIN_ARENA_SIZE) {
goto overflow;
}
ptd0 = pt0;
goto deref;
} else if (IsApplTerm(d0)) {
register Functor f;
register CELL *headp;
/* store the terms to visit */
headp = RepAppl(d0);
if (IsApplTerm(*headp)//(share && headp < HB) ||
) {
/* If this is newer than the current term, just reuse */
*ptf++ = *headp;
continue;
}
f = (Functor)(*headp);
if (IsExtensionFunctor(f)) { if (IsExtensionFunctor(f)) {
switch ((CELL)f) { if (share) {
case (CELL) FunctorDBRef: *ptf++ = d0;
case (CELL) FunctorAttVar: continue;
*ptf++ = d0; }
break; switch ((CELL)f) {
case (CELL) FunctorLongInt: case (CELL) FunctorDBRef:
if (HR > ASP - (MIN_ARENA_SIZE + 3)) { case (CELL) FunctorAttVar:
goto overflow; *ptf++ = d0;
} break;
*ptf++ = AbsAppl(HR); case (CELL) FunctorLongInt:
HR[0] = (CELL)f; if (HR > ASP - (MIN_ARENA_SIZE + 3)) {
HR[1] = headp[1]; goto overflow;
HR[2] = EndSpecials; }
HR += 3; *ptf++ = AbsAppl(HR);
if (HR > ASP - MIN_ARENA_SIZE) { HR[0] = (CELL)f;
goto overflow; HR[1] = headp[1];
} HR[2] = EndSpecials;
break; HR += 3;
case (CELL) FunctorDouble: if (HR > ASP - MIN_ARENA_SIZE) {
if (HR > goto overflow;
ASP - (MIN_ARENA_SIZE + (2 + SIZEOF_DOUBLE / sizeof(CELL)))) { }
goto overflow; break;
} case (CELL) FunctorDouble:
*ptf++ = AbsAppl(HR); if (HR >
HR[0] = (CELL)f; ASP - (MIN_ARENA_SIZE + (2 + SIZEOF_DOUBLE / sizeof(CELL)))) {
HR[1] = headp[1]; goto overflow;
}
*ptf++ = AbsAppl(HR);
HR[0] = (CELL)f;
HR[1] = headp[1];
#if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P #if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P
HR[2] = headp[2]; HR[2] = headp[2];
HR[3] = EndSpecials; HR[3] = EndSpecials;
HR += 4; HR += 4;
#else #else
HR[2] = EndSpecials; HR[2] = EndSpecials;
HR += 3; HR += 3;
#endif #endif
break; break;
case (CELL) FunctorString: case (CELL) FunctorString:
if (ASP - HR < MIN_ARENA_SIZE + 3 + headp[1]) { if (ASP - HR < MIN_ARENA_SIZE + 3 + headp[1]) {
goto overflow; goto overflow;
}
*ptf++ = AbsAppl(HR);
memmove(HR, headp, sizeof(CELL) * (3 + headp[1]));
HR += headp[1] + 3;
break;
default: {
/* big int */
size_t sz = (sizeof(MP_INT) + 3 * CellSize +
((MP_INT *)(headp + 2))->_mp_alloc * sizeof(mp_limb_t)) /
CellSize,
i;
if (HR > ASP - (MIN_ARENA_SIZE + sz)) {
goto overflow;
}
*ptf++ = AbsAppl(HR);
HR[0] = (CELL)f;
for (i = 1; i < sz; i++) {
HR[i] = headp[i];
}
HR += sz;
} }
*ptf++ = AbsAppl(HR);
memmove(HR, headp, sizeof(CELL) * (3 + headp[1]));
HR += headp[1] + 3;
break;
default: {
/* big int */
size_t sz = (sizeof(MP_INT) + 3 * CellSize +
((MP_INT *)(headp + 2))->_mp_alloc * sizeof(mp_limb_t)) /
CellSize,
i;
if (HR > ASP - (MIN_ARENA_SIZE + sz)) {
goto overflow;
} }
continue; *ptf++ = AbsAppl(HR);
} HR[0] = (CELL)f;
*ptf = AbsAppl(HR); for (i = 1; i < sz; i++) {
ptf++; HR[i] = headp[i];
/* store the terms to visit */
to_visit->start_cp = pt0; }
to_visit->end_cp = pt0_end; HR += sz;
to_visit->to = ptf;
to_visit->ground = ground;
if (++to_visit >= to_visit_max-32) {
expand_stack(to_visit0, to_visit, to_visit_max, struct cp_frame);
} }
TrailedMaBind(headp,AbsAppl(HR));
ptf = HR;
*ptf++ = (CELL)f;
ground = true;
arity_t a = ArityOfFunctor(f);
HR = ptf+a;
if (HR > ASP - MIN_ARENA_SIZE) {
goto overflow;
} }
pt0 = headp; continue;
pt0_end = headp+a;
ground = (f != FunctorMutable);
} else {
/* just copy atoms or integers */
*ptf++ = d0;
} }
continue; *ptf = AbsAppl(HR);
} ptf++;
/* store the terms to visit */
derefa_body(d0, ptd0, copy_term_unk, copy_term_nvar); to_visit->start_cp = pt0;
ground = false; to_visit->end_cp = pt0_end;
/* don't need to copy variables if we want to share the global term */ to_visit->to = ptf;
if (//(share && ptd0 < HB && ptd0 > H0) || to_visit->curp = headp;
(ptd0 >= HLow && ptd0 < HR)) { d0 = *headp;
/* we have already found this cell */ to_visit->oldv = d0;
*ptf++ = (CELL)ptd0; to_visit->ground = ground;
if (++to_visit >= to_visit_max-32) {
expand_stack(to_visit0, to_visit, to_visit_max, struct cp_frame);
}
if (share) {
TrailedMaBind(headp,AbsPair(HR));
} else {
*headp = AbsPair(HR);
}
ptf = HR;
ptf[-1] = (CELL)f;
ground = true;
arity_t a = ArityOfFunctor(f);
HR = ptf+a;
if (HR > ASP - MIN_ARENA_SIZE) {
goto overflow;
}
pt0 = headp;
pt0_end = headp+a;
ground = (f != FunctorMutable);
} else { } else {
if (copy_att_vars && GlobalIsAttachedTerm((CELL)ptd0)) { /* just copy atoms or integers */
/* if unbound, call the standard copy term routine */ *ptf++ = d0;
struct cp_frame *bp; }
CELL new; continue;
}
bp = to_visit; derefa_body(d0, ptd0, copy_term_unk, copy_term_nvar);
if (!GLOBAL_attas[ExtFromCell(ptd0)].copy_term_op(ptd0, &bp, ground = false;
ptf PASS_REGS)) { /* don't need to copy variables if we want to share the global term */
goto overflow; if (//(share && ptd0 < HB && ptd0 > H0) ||
} (ptd0 >= HLow && ptd0 < HR)) {
to_visit = bp; /* we have already found this cell */
new = *ptf; *ptf++ = (CELL)ptd0;
if (TR > (tr_fr_ptr)LOCAL_TrailTop - 256) { } else {
/* Trail overflow */ if (copy_att_vars && GlobalIsAttachedTerm((CELL)ptd0)) {
if (!Yap_growtrail((TR - TR0) * sizeof(tr_fr_ptr *), TRUE)) { /* if unbound, call the standard copy term routine */
goto trail_overflow; struct cp_frame *bp;
} CELL new;
}
TrailedMaBind(ptd0, new); bp = to_visit;
ptf++; if (!GLOBAL_attas[ExtFromCell(ptd0)].copy_term_op(ptd0, &bp,
} else { ptf PASS_REGS)) {
/* first time we met this term */ goto overflow;
RESET_VARIABLE(ptf);
if ((ADDR)TR > LOCAL_TrailTop - MIN_ARENA_SIZE)
goto trail_overflow;
TrailedMaBind(ptd0, (CELL)ptf);
ptf++;
} }
to_visit = bp;
new = *ptf;
if (TR > (tr_fr_ptr)LOCAL_TrailTop - 256) {
/* Trail overflow */
if (!Yap_growtrail((TR - TR0) * sizeof(tr_fr_ptr *), TRUE)) {
goto trail_overflow;
}
}
TrailedMaBind(ptd0, new);
ptf++;
} else {
/* first time we met this term */
RESET_VARIABLE(ptf);
if ((ADDR)TR > LOCAL_TrailTop - MIN_ARENA_SIZE)
goto trail_overflow;
TrailedMaBind(ptd0, (CELL)ptf);
ptf++;
} }
} }
}
/* Do we still have compound terms to visit */ /* Do we still have compound terms to visit */
if (to_visit > to_visit0) { if (to_visit > to_visit0) {
to_visit--; to_visit--;
pt0 = to_visit->start_cp; if (!share)
pt0_end = to_visit->end_cp; *to_visit->curp = to_visit->oldv;
ptf = to_visit->to; pt0 = to_visit->start_cp;
ground = (ground && to_visit->ground); pt0_end = to_visit->end_cp;
goto loop; ptf = to_visit->to;
} ground = (ground && to_visit->ground);
goto loop;
}
/* restore our nice, friendly, term to its original state */ /* restore our nice, friendly, term to its original state */
clean_dirty_tr(TR0 PASS_REGS); clean_dirty_tr(TR0 PASS_REGS);
/* follow chain of multi-assigned variables */ /* follow chain of multi-assigned variables */
pop_text_stack(lvl); pop_text_stack(lvl);
return 0; return 0;
overflow: overflow:
/* oops, we're in trouble */ /* oops, we're in trouble */
HR = HLow; HR = HLow;
/* we've done it */ /* we've done it */
/* restore our nice, friendly, term to its original state */ /* restore our nice, friendly, term to its original state */
HB = HB0; HB = HB0;
while (to_visit > to_visit0) { while (to_visit > to_visit0) {
to_visit--; to_visit--;
pt0 = to_visit->start_cp; pt0 = to_visit->start_cp;
pt0_end = to_visit->end_cp; pt0_end = to_visit->end_cp;
ptf = to_visit->to; ptf = to_visit->to;
} }
reset_trail(TR0); reset_trail(TR0);
pop_text_stack(lvl); pop_text_stack(lvl);
return -1; return -1;
trail_overflow: trail_overflow:
/* oops, we're in trouble */ /* oops, we're in trouble */
HR = HLow; HR = HLow;
/* we've done it */ /* we've done it */
/* restore our nice, friendly, term to its original state */ /* restore our nice, friendly, term to its original state */
HB = HB0; HB = HB0;
while (to_visit > to_visit0) { while (to_visit > to_visit0) {
to_visit--; to_visit--;
pt0 = to_visit->start_cp; pt0 = to_visit->start_cp;
pt0_end = to_visit->end_cp; pt0_end = to_visit->end_cp;
ptf = to_visit->to; ptf = to_visit->to;
} }
reset_trail(TR0); reset_trail(TR0);
pop_text_stack(lvl); pop_text_stack(lvl);
return -4; return -4;
} }
@ -492,7 +527,7 @@ CopyTerm(Term inp, UInt arity, int share, int newattvs USES_REGS) {
*HR = t; *HR = t;
Hi = HR+1; Hi = HR+1;
HR += 2; HR += 2;
if ((res = Yap_copy_complex_term(Hi-2, Hi-1, share, newattvs, Hi, Hi PASS_REGS)) < 0) { if ((res = Yap_copy_complex_term(Hi-2, Hi-1, share, NULL, newattvs, Hi, Hi PASS_REGS)) < 0) {
HR = Hi-1; HR = Hi-1;
if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L) if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L)
return FALSE; return FALSE;
@ -516,7 +551,7 @@ CopyTerm(Term inp, UInt arity, int share, int newattvs USES_REGS) {
HR += 2; HR += 2;
{ {
int res; int res;
if ((res = Yap_copy_complex_term(ap-1, ap+1, share, newattvs, Hi, Hi PASS_REGS)) < 0) { if ((res = Yap_copy_complex_term(ap-1, ap+1, share, NULL, newattvs, Hi, Hi PASS_REGS)) < 0) {
HR = Hi; HR = Hi;
if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L) if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L)
return FALSE; return FALSE;
@ -548,7 +583,7 @@ CopyTerm(Term inp, UInt arity, int share, int newattvs USES_REGS) {
} else { } else {
int res; int res;
if ((res = Yap_copy_complex_term(ap, ap+ArityOfFunctor(f), share, newattvs, HB0+1, HB0 PASS_REGS)) < 0) { if ((res = Yap_copy_complex_term(ap, ap+ArityOfFunctor(f), share, NULL, newattvs, HB0+1, HB0 PASS_REGS)) < 0) {
HR = HB0; HR = HB0;
if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L) if ((t = handle_cp_overflow(res, TR0, arity, t))== 0L)
return FALSE; return FALSE;

View File

@ -111,10 +111,9 @@ typedef struct cp_frame {
CELL *start_cp; CELL *start_cp;
CELL *end_cp; CELL *end_cp;
CELL *to; CELL *to;
#ifdef RATIONAL_TREES CELL *curp;
CELL oldv; CELL oldv;
int ground; int ground;
#endif
} copy_frame; } copy_frame;
#ifdef COROUTINING #ifdef COROUTINING

View File

@ -478,7 +478,7 @@ extern void Yap_InitUserBacks(void);
/* utilpreds.c */ /* utilpreds.c */
int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end,
bool share, bool copy_att_vars, CELL *ptf, bool share, Term *split, bool copy_att_vars, CELL *ptf,
CELL *HLow USES_REGS); CELL *HLow USES_REGS);
extern Term Yap_CopyTerm(Term); extern Term Yap_CopyTerm(Term);
extern bool Yap_Variant(Term, Term); extern bool Yap_Variant(Term, Term);

View File

@ -48,12 +48,12 @@ refactoring (trivial):
#define JPL_C_LIB_VERSION_PATCH 4 #define JPL_C_LIB_VERSION_PATCH 4
#define JPL_C_LIB_VERSION_STATUS "alpha" #define JPL_C_LIB_VERSION_STATUS "alpha"
#define JPL_DEBUG //#define JPL_DEBUG
#ifndef JPL_DEBUG #ifndef JPL_DEBUG
/*#define DEBUG(n, g) ((void)0) */ /*#define DEBUG(n, g) ((void)0) */
#define DEBUG_LEVEL 4 #define DEBUG_LEVEL 4
#define JPL_DEBUG(n, g) ( n >= DEBUG_LEVEL ? g : (void)0 ) #define JPL_DEBUG(n, g) ( false && n >= DEBUG_LEVEL ? g : (void)0 )
#endif #endif
/* disable type-of-ref caching (at least until GC issues are resolved) */ /* disable type-of-ref caching (at least until GC issues are resolved) */
@ -642,7 +642,7 @@ static JNIEnv*
jni_env(void) /* economically gets a JNIEnv pointer, valid for this thread */ jni_env(void) /* economically gets a JNIEnv pointer, valid for this thread */
{ JNIEnv *env; { JNIEnv *env;
switch( (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_9) ) switch( (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_2) )
{ case JNI_OK: { case JNI_OK:
return env; return env;
case JNI_EDETACHED: case JNI_EDETACHED:
@ -1826,7 +1826,7 @@ jni_create_jvm_c(
JNIEnv *env; JNIEnv *env;
JPL_DEBUG(1, Sdprintf( "[creating JVM with 'java.class.path=%s']\n", classpath)); JPL_DEBUG(1, Sdprintf( "[creating JVM with 'java.class.path=%s']\n", classpath));
vm_args.version = JNI_VERSION_1_6; /* "Java 1.2 please" */ vm_args.version = JNI_VERSION_1_2; /* "Java 1.2 please" */
if ( classpath ) if ( classpath )
{ {
cpoptp = (char *)malloc(strlen(classpath) + strlen("-Djava.class.path=")+1); cpoptp = (char *)malloc(strlen(classpath) + strlen("-Djava.class.path=")+1);