-Interface issues

This commit is contained in:
Vitor Santos Costa 2019-05-01 01:34:58 +01:00
parent 54a222e5df
commit 7c16af4edf
15 changed files with 201 additions and 168 deletions

View File

@ -1142,6 +1142,8 @@ X_API Int YAP_Execute(PredEntry *pe, CPredicate exec_code) {
// if (pe->PredFlags & CArgsPredFlag) { // if (pe->PredFlags & CArgsPredFlag) {
// CurrentModule = pe->ModuleOfPred; // CurrentModule = pe->ModuleOfPred;
//} //}
int lvl = push_text_stack();
yhandle_t hdl = Yap_CurrentHandle();
if (pe->PredFlags & SWIEnvPredFlag) { if (pe->PredFlags & SWIEnvPredFlag) {
CPredicateV codev = (CPredicateV)exec_code; CPredicateV codev = (CPredicateV)exec_code;
struct foreign_context ctx; struct foreign_context ctx;
@ -1161,12 +1163,15 @@ X_API Int YAP_Execute(PredEntry *pe, CPredicate exec_code) {
// check for junk: open frames, etc */ // check for junk: open frames, etc */
if (ret) if (ret)
complete_exit(((choiceptr)(LCL0 - OASP)), FALSE, FALSE PASS_REGS); complete_exit(((choiceptr)(LCL0 - OASP)), FALSE, FALSE PASS_REGS);
else else {
complete_fail(((choiceptr)(LCL0 - OASP)), FALSE PASS_REGS); complete_fail(((choiceptr)(LCL0 - OASP)), FALSE PASS_REGS);
}
// CurrentModule = omod; // CurrentModule = omod;
if (!ret) { if (!ret) {
Yap_RaiseException(); Yap_RaiseException();
} }
Yap_RecoverHandles(0, hdl);
pop_text_stack( lvl );
return ret; return ret;
} }
@ -1739,17 +1744,18 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
CACHE_REGS CACHE_REGS
PredEntry *pe = ape; PredEntry *pe = ape;
bool out; bool out;
// fprintf(stderr,"EnterGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p // fprintf(stderr,"1EnterGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p
// Slots=%d\n",HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, // Slots=%d\n",HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP,
// LOCAL_CurSlot); // LOCAL_CurSlot);
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
dgi->lvl = push_text_stack();
LOCAL_ActiveError->errorNo = YAP_NO_ERROR; LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
LOCAL_PrologMode = UserMode; LOCAL_PrologMode = UserMode;
dgi->p = P; dgi->p = P;
dgi->cp = CP; dgi->cp = CP;
dgi->b0 = LCL0 - (CELL *)B; dgi->b0 = LCL0 - (CELL *)B;
printf("%ld\n", dgi->CurSlot); dgi->env0 = LCL0 - ENV;
// ensure our current ENV receives current P. // ensure our current ENV receives current P.
Yap_PrepGoal(pe->ArityOfPE, nullptr, B PASS_REGS); Yap_PrepGoal(pe->ArityOfPE, nullptr, B PASS_REGS);
@ -1771,10 +1777,10 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
dgi->EndSlot = LOCAL_CurSlot; dgi->EndSlot = LOCAL_CurSlot;
Yap_StartSlots(); Yap_StartSlots();
} else { } else {
printf("%ld\n", dgi->CurSlot);
LOCAL_CurSlot = LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal dgi->CurSlot; // ignore any slots created within the called goal
} }
pop_text_stack(dgi->lvl);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
@ -1785,6 +1791,7 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
bool out; bool out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
dgi->lvl = push_text_stack();
myB = (choiceptr)(LCL0 - dgi->b_exit); myB = (choiceptr)(LCL0 - dgi->b_exit);
myB0 = (choiceptr)(LCL0 - dgi->b_entry); myB0 = (choiceptr)(LCL0 - dgi->b_entry);
CP = myB->cp_cp; CP = myB->cp_cp;
@ -1811,6 +1818,7 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
LOCAL_CurSlot = LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal dgi->CurSlot; // ignore any slots created within the called goal
} }
pop_text_stack(dgi->lvl);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
@ -1823,6 +1831,8 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
// successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP, // successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP,
// LOCAL_CurSlot); // LOCAL_CurSlot);
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
dgi->lvl = push_text_stack();
if (successful) { if (successful) {
choiceptr nB = (choiceptr)(LCL0 - dgi->b_entry); choiceptr nB = (choiceptr)(LCL0 - dgi->b_entry);
if (B <= nB) { if (B <= nB) {
@ -1834,13 +1844,17 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
Yap_signal(YAP_FAIL_SIGNAL); Yap_signal(YAP_FAIL_SIGNAL);
} }
B = (choiceptr)(LCL0 - dgi->b0); B = (choiceptr)(LCL0 - dgi->b0);
#ifdef DEPTH_LIMIT
DEPTH = B->cp_depth;
#endif
P = dgi->p; P = dgi->p;
CP = dgi->cp; CP = dgi->cp;
YENV = ENV = LCL0-dgi->env0;
LOCAL_CurSlot = LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal dgi->CurSlot; // ignore any slots created within the called goal
printf("L %ld\n", dgi->CurSlot); pop_text_stack(dgi->lvl);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
// fprintf(stderr,"LeftGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p // fprintf(stderr," LeftGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p
// Slots=%d\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, // Slots=%d\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P,
// CP, LOCAL_CurSlot); // CP, LOCAL_CurSlot);
return TRUE; return TRUE;
@ -1852,6 +1866,8 @@ X_API Int YAP_RunGoal(Term t) {
yhandle_t cslot = LOCAL_CurSlot; yhandle_t cslot = LOCAL_CurSlot;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
int lvl = push_text_stack();
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
LOCAL_PrologMode = UserMode; LOCAL_PrologMode = UserMode;
out = Yap_RunTopGoal(t, true); out = Yap_RunTopGoal(t, true);
@ -1860,6 +1876,7 @@ X_API Int YAP_RunGoal(Term t) {
// We'll pass it through // We'll pass it through
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
LOCAL_CurSlot = cslot; LOCAL_CurSlot = cslot;
pop_text_stack(lvl);
return out; return out;
} }
@ -1938,6 +1955,7 @@ X_API Int YAP_RunGoalOnce(Term t) {
yhandle_t CSlot; yhandle_t CSlot;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
int lvl = push_text_stack();
CSlot = Yap_StartSlots(); CSlot = Yap_StartSlots();
LOCAL_PrologMode = UserMode; LOCAL_PrologMode = UserMode;
// Yap_heap_regs->yap_do_low_level_trace=true; // Yap_heap_regs->yap_do_low_level_trace=true;
@ -1946,6 +1964,7 @@ X_API Int YAP_RunGoalOnce(Term t) {
// Yap_CloseSlots(CSlot); // Yap_CloseSlots(CSlot);
if (!(oldPrologMode & UserCCallMode)) { if (!(oldPrologMode & UserCCallMode)) {
/* called from top-level */ /* called from top-level */
pop_text_stack( lvl);
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
@ -1987,6 +2006,7 @@ X_API Int YAP_RunGoalOnce(Term t) {
CP = old_CP; CP = old_CP;
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
pop_text_stack( lvl);
return out; return out;
} }
@ -2262,6 +2282,7 @@ X_API char *YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) {
} else { } else {
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
if (buf == out.val.c) { if (buf == out.val.c) {
pop_text_stack(l);
return buf; return buf;
} else { } else {
if ( strlen(out.val.c ) < sze) { if ( strlen(out.val.c ) < sze) {

View File

@ -78,7 +78,7 @@ assert/1 or recorda/3.
+ The value lives on the Prolog (global) stack. This implies + The value lives on the Prolog (global) stack. This implies
that lookup time is independent from the size of the term. that lookup time is independent from the size of the term.
This is particulary interesting for large data structures This is particulary interesting for large data structures
such as parsed XML documents or the CHR global constraint qqqsuch as parsed XML documents or the CHR global constraint
store. store.
They support both global assignment using nb_setval/2 and They support both global assignment using nb_setval/2 and
@ -191,13 +191,14 @@ static Term NewArena(UInt size, int wid, UInt arity, CELL *where) {
UInt new_size; UInt new_size;
WORKER_REGS(wid) WORKER_REGS(wid)
if (where == NULL || where == HR) { // make sure we have enough room
while (HR + size > ASP - 1024) { while (HR + size > ASP - 1024) {
if (!Yap_gcl(size * sizeof(CELL), arity, ENV, P)) { if (!Yap_gcl(size * sizeof(CELL), arity, ENV, P)) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return TermNil; return TermNil;
} }
} }
if (where == NULL || where == HR) {
t = CreateNewArena(HR, size); t = CreateNewArena(HR, size);
HR += size; HR += size;
} else { } else {
@ -241,8 +242,9 @@ static void adjust_cps(UInt size USES_REGS) {
} }
} }
static int GrowArena(Term arena, CELL *pt, size_t old_size, size_t size, static Term GrowArena(Term arena, CELL *pt, size_t old_size, size_t size,
UInt arity USES_REGS) { UInt arity USES_REGS) {
size_t sz;
LOCAL_ArenaOverflows++; LOCAL_ArenaOverflows++;
if (size == 0) { if (size == 0) {
if (old_size < 128 * 1024) { if (old_size < 128 * 1024) {
@ -254,56 +256,49 @@ static int GrowArena(Term arena, CELL *pt, size_t old_size, size_t size,
if (size < 4096) { if (size < 4096) {
size = 4096; size = 4096;
} }
if (pt == HR) { while (HR + size > ASP - 1024) {
if (HR + size > ASP - 1024) {
XREGS[arity + 1] = arena; XREGS[arity + 1] = arena;
if (!Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP))) { if (!Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP))) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return FALSE; return false;
} }
arena = XREGS[arity + 1]; arena = XREGS[arity + 1];
/* we don't know if the GC added junk on top of the global */
pt = ArenaLimit(arena);
return GrowArena(arena, pt, old_size, size, arity PASS_REGS);
}
adjust_cps(size PASS_REGS); adjust_cps(size PASS_REGS);
HR += size; pt = ArenaLimit(arena)+1;
} else {
XREGS[arity + 1] = arena; if (pt == HR) {
/* try to recover some room */ sz = old_size + size;
Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP)); HR += size;
arena = XREGS[arity + 1]; HR[ - 1] = EndSpecials;
pt = ArenaLimit(arena); } else {
if ((size = Yap_InsertInGlobal(pt, size * sizeof(CELL))) == 0) { if ((sz = Yap_InsertInGlobal(pt, sz * sizeof(CELL))) == 0) {
return FALSE; return FALSE;
}
pt[sz-1] = EndSpecials;
} }
size = size / sizeof(CELL);
arena = XREGS[arity + 1]; arena = XREGS[arity + 1];
MP_INT *dst = (MP_INT *)(RepAppl(arena) + 2);
dst->_mp_alloc+= size/sizeof(CELL);
return sz;
} }
CreateNewArena(ArenaPt(arena), size + old_size);
return TRUE;
} }
CELL *Yap_GetFromArena(Term *arenap, UInt cells, UInt arity) { CELL *Yap_GetFromArena(Term *arenap, UInt cells, UInt arity) {
CACHE_REGS CACHE_REGS
restart : {
Term arena = *arenap; Term arena = *arenap;
CELL *max = ArenaLimit(arena); CELL *max = ArenaLimit(arena);
CELL *base = ArenaPt(arena); CELL *base = ArenaPt(arena);
CELL *newH; CELL *newH;
UInt old_sz = ArenaSz(arena), new_size; UInt old_sz = ArenaSz(arena), new_size;
while(true) {
if (IN_BETWEEN(base, HR, max)) { if (IN_BETWEEN(base, HR, max)) {
base = HR; base = HR;
HR += cells; HR += cells;
return base; return base;
} }
if (base + cells > max - 1024) { if (base + cells > ASP - 1024) {
if (!GrowArena(arena, max, old_sz, old_sz + sizeof(CELL) * 1024, continue;
arity PASS_REGS))
return NULL;
goto restart;
} }
newH = base + cells; newH = base + cells;
@ -474,7 +469,9 @@ loop:
HR += ap2[1] + 3; HR += ap2[1] + 3;
break; break;
default: { default: {
/* big int */
/* big int */
UInt sz = (sizeof(MP_INT) + 3 * CellSize + UInt sz = (sizeof(MP_INT) + 3 * CellSize +
((MP_INT *)(ap2 + 2))->_mp_alloc * sizeof(mp_limb_t)) / ((MP_INT *)(ap2 + 2))->_mp_alloc * sizeof(mp_limb_t)) /
CellSize, CellSize,
@ -482,7 +479,7 @@ loop:
if (HR > ASP - (MIN_ARENA_SIZE + sz)) { if (HR > ASP - (MIN_ARENA_SIZE + sz)) {
goto overflow; goto overflow;
} }
*ptf++ = AbsAppl(HR); *ptf++ = AbsAppl(HR);
HR[0] = (CELL)f; HR[0] = (CELL)f;
for (i = 1; i < sz; i++) { for (i = 1; i < sz; i++) {
@ -494,6 +491,7 @@ loop:
continue; continue;
} }
*ptf = AbsAppl(HR); *ptf = AbsAppl(HR);
ptf++; ptf++;
/* store the terms to visit */ /* store the terms to visit */
#ifdef RATIONAL_TREES #ifdef RATIONAL_TREES
@ -797,7 +795,7 @@ error_handler:
case -1: case -1:
if (arena == LOCAL_GlobalArena) if (arena == LOCAL_GlobalArena)
LOCAL_GlobalArenaOverflows++; LOCAL_GlobalArenaOverflows++;
if (!GrowArena(arena, old_top, old_size, min_grow, arity + 3 PASS_REGS)) { if ((arena=GrowArena(arena, old_top, old_size, min_grow, arity + 3 PASS_REGS))==0) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return 0L; return 0L;
} }
@ -848,8 +846,8 @@ restart:
HB = oldHB; HB = oldHB;
if (arena == LOCAL_GlobalArena) if (arena == LOCAL_GlobalArena)
LOCAL_GlobalArenaOverflows++; LOCAL_GlobalArenaOverflows++;
if (!GrowArena(arena, old_top, old_size, Nar * sizeof(CELL), if ((arena=GrowArena(arena, old_top, old_size, Nar * sizeof(CELL),
arity + 2 PASS_REGS)) { arity + 2 PASS_REGS))==0) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, Yap_Error(RESOURCE_ERROR_STACK, TermNil,
"while creating large global term"); "while creating large global term");
return 0L; return 0L;
@ -1549,8 +1547,8 @@ static Int nb_queue(UInt arena_sz USES_REGS) {
queue = Yap_MkApplTerm(FunctorNBQueue, QUEUE_FUNCTOR_ARITY, ar); queue = Yap_MkApplTerm(FunctorNBQueue, QUEUE_FUNCTOR_ARITY, ar);
if (!Yap_unify(queue, ARG1)) if (!Yap_unify(queue, ARG1))
return FALSE; return FALSE;
if (arena_sz < 4 * 1024) if (arena_sz < 32 * 1024)
arena_sz = 4 * 1024; arena_sz = 32 * 1024;
queue_arena = NewArena(arena_sz, worker_id, 1, NULL); queue_arena = NewArena(arena_sz, worker_id, 1, NULL);
if (queue_arena == 0L) { if (queue_arena == 0L) {
return FALSE; return FALSE;
@ -1694,8 +1692,8 @@ static Int p_nb_queue_enqueue(USES_REGS1) {
} }
ARG3 = to; ARG3 = to;
/* fprintf(stderr,"growing %ld cells\n",(unsigned long int)gsiz);*/ /* fprintf(stderr,"growing %ld cells\n",(unsigned long int)gsiz);*/
if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) { if ((arena=GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS))==0) {
Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage);
return 0L; return 0L;
} }
to = ARG3; to = ARG3;
@ -2025,7 +2023,7 @@ restart:
gsiz = 1024; gsiz = 1024;
} }
ARG3 = to; ARG3 = to;
if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) { if ((arena=GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS))==0) {
Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage);
return 0L; return 0L;
} }
@ -2341,7 +2339,7 @@ static Term DelBeamMin(CELL *pt, CELL *pt2, UInt sz) {
static Int p_nb_beam_add_to_beam(USES_REGS1) { static Int p_nb_beam_add_to_beam(USES_REGS1) {
CELL *qd = GetHeap(ARG1, "add_to_beam"), *oldH, *oldHB, *pt; CELL *qd = GetHeap(ARG1, "add_to_beam"), *oldH, *oldHB, *pt;
UInt hsize, hmsize, old_sz; UInt hsize, hmsize, old_sz;
Term arena, to, key; Term arena, to, qsize, key;
UInt mingrow; UInt mingrow;
if (!qd) if (!qd)
@ -2377,9 +2375,9 @@ static Int p_nb_beam_add_to_beam(USES_REGS1) {
oldHB = HB; oldHB = HB;
HR = HB = ArenaPt(arena); HR = HB = ArenaPt(arena);
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
qsize = IntegerOfTerm(qd[QUEUE_SIZE]);
while (old_sz < MIN_ARENA_SIZE) { while (old_sz < MIN_ARENA_SIZE) {
UInt gsiz = hsize * 2; UInt gsiz = HR - RepPair(qd[QUEUE_HEAD]);
HR = oldH; HR = oldH;
HB = oldHB; HB = oldHB;
if (gsiz > 1024 * 1024) { if (gsiz > 1024 * 1024) {
@ -2388,6 +2386,7 @@ static Int p_nb_beam_add_to_beam(USES_REGS1) {
gsiz = 1024; gsiz = 1024;
} }
ARG3 = to; ARG3 = to;
/* fprintf(stderr,"growing %ld cells\n",(unsigned long int)gsiz);*/
if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) { if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) {
Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage);
return 0L; return 0L;

113
C/grow.c
View File

@ -324,15 +324,15 @@ MoveGlobalWithHole( USES_REGS1 )
} }
static void static void
MoveHalfGlobal(CELL *OldPt USES_REGS) MoveHalfGlobal(CELL *OldPt, size_t request USES_REGS)
{ {
/* /*
* cpcellsd(To,From,NOfCells) - copy the cells downwards - in * cpcellsd(To,From,NOfCells) - copy the cells downwards - in
* absmi.asm * absmi.asm
*/ */
UInt diff = LOCAL_OldH-OldPt; UInt diff = LOCAL_OldH-OldPt;
CELL *NewPt = (CELL *)((char*)OldPt+LOCAL_GDiff);
CELL *IntPt = (CELL *)((char*)OldPt+LOCAL_GDiff0); CELL *IntPt = (CELL *)((char*)OldPt+LOCAL_GDiff0);
CELL *NewPt = IntPt+request/sizeof(CELL);
cpcellsd(NewPt, IntPt, diff); cpcellsd(NewPt, IntPt, diff);
} }
@ -393,7 +393,7 @@ AdjustTrail(bool adjusting_heap, bool thread_copying USES_REGS)
register CELL reg = TrailTerm(ptt-1); register CELL reg = TrailTerm(ptt-1);
#ifdef FROZEN_STACKS #ifdef FROZEN_STACKS
register CELL reg2 = TrailVal(ptt-1); register CELL reg2 = TrailVal(ptt-1);
#endif #endif
ptt--; ptt--;
if (IsVarTerm(reg)) { if (IsVarTerm(reg)) {
@ -436,6 +436,8 @@ fixPointerCells(CELL *pt, CELL *pt_bot, bool thread_copying USES_REGS)
{ {
while (pt > pt_bot) { while (pt > pt_bot) {
CELL reg = *--pt; CELL reg = *--pt;
// if (pt-pt_bot> 4300 && pt-pt_bot < 4500)
// printf("%d %d %lx\n", pt-pt_bot, pt-LOCAL_GSplit, reg);
if (IsVarTerm(reg)) { if (IsVarTerm(reg)) {
if (IsOldLocal(reg)) if (IsOldLocal(reg))
*pt = LocalAdjust(reg); *pt = LocalAdjust(reg);
@ -450,6 +452,8 @@ fixPointerCells(CELL *pt, CELL *pt_bot, bool thread_copying USES_REGS)
} else if (IsPairTerm(reg)) { } else if (IsPairTerm(reg)) {
*pt = AdjustPair(reg PASS_REGS); *pt = AdjustPair(reg PASS_REGS);
} }
// if (pt-pt_bot> 4300 && pt-pt_bot < 4500)
// printf("%lx\n", *pt);
} }
} }
@ -889,12 +893,14 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
char *omax = (char *)H0; char *omax = (char *)H0;
ADDR old_GlobalBase = LOCAL_GlobalBase; ADDR old_GlobalBase = LOCAL_GlobalBase;
UInt minimal_request = 0L; UInt minimal_request = 0L;
Int size = request; Int size = request/sizeof(CELL);
char vb_msg1 = '\0', *vb_msg2; char vb_msg1 = '\0', *vb_msg2;
bool do_grow = true; bool do_grow = true;
bool insert_in_delays = false;
printf("request=%ld at %p, shift %p, %ld <-> %ld\n",request, hsplit, ptr,hsplit-H0,HR-hsplit);
/* /*
request is the amount of memory we requested, in bytes; request is the amount of memory we requesd, in bytes;
base_move is the shift in global stacks we had to do base_move is the shift in global stacks we had to do
size is how much space we allocate: it's negative if we just expand size is how much space we allocate: it's negative if we just expand
the delay stack. the delay stack.
@ -902,55 +908,44 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
*/ */
if (hsplit) { if (hsplit) {
/* just a little bit of sanity checking */ if (hsplit < H0 ||
if (hsplit < H0 && hsplit > (CELL *)LOCAL_GlobalBase) {
insert_in_delays = TRUE;
/* expanding attributed variables */
if (omax - size > LOCAL_GlobalBase+4096*sizeof(CELL)) {
/* we can just ask for more room */
size = 0;
do_grow = FALSE;
}
} else if (hsplit < (CELL*)omax ||
hsplit > HR) hsplit > HR)
return FALSE; return false;
else if (hsplit == (CELL *)omax) if (hsplit == H0)
hsplit = NULL; hsplit = NULL;
if (size < 0 || if (hsplit == HR && Unsigned(HR)+request < Unsigned(ASP)-StackGap( PASS_REGS1 )) {
(Unsigned(HR)+size < Unsigned(ASP)-StackGap( PASS_REGS1 ) && return request;
hsplit > H0)) {
/* don't need to expand stacks */
insert_in_delays = FALSE;
do_grow = FALSE;
}
} else {
if (Unsigned(HR)+size < Unsigned(ASP)-CreepFlag) {
/* we can just ask for more room */
do_grow = FALSE;
} }
} }
if (size < 0 ||
(Unsigned(HR)+request < Unsigned(ASP-StackGap( PASS_REGS1 )))) {
do_grow = false;
}
if (do_grow) { if (do_grow) {
if (size < YAP_ALLOC_SIZE) if (request < YAP_ALLOC_SIZE)
size = YAP_ALLOC_SIZE; request = YAP_ALLOC_SIZE;
size = AdjustPageSize(size); request = AdjustPageSize(request);
} }
printf("grow=%d\n", do_grow);
/* adjust to a multiple of 256) */ /* adjust to a multiple of 256) */
LOCAL_ErrorMessage = NULL; LOCAL_ErrorMessage = NULL;
LOCAL_PrologMode |= GrowStackMode; LOCAL_PrologMode |= GrowStackMode;
start_growth_time = Yap_cputime(); start_growth_time = Yap_cputime();
if (do_grow) { if (do_grow) {
if (!GLOBAL_AllowGlobalExpansion) { if (!GLOBAL_AllowGlobalExpansion) {
LOCAL_ErrorMessage = "Global Stack crashed against Local Stack"; LOCAL_ErrorMessage = "Global Stack crashed against Local Stack";
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
return 0; return 0;
} }
if (!GLOBAL_AllowGlobalExpansion || !Yap_ExtendWorkSpace(size)) { if (!GLOBAL_AllowGlobalExpansion || !Yap_ExtendWorkSpace(request)) {
/* always fails when using malloc */ /* always fails when using malloc */
LOCAL_ErrorMessage = NULL; LOCAL_ErrorMessage = NULL;
size += AdjustPageSize(((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)+MinHeapGap); request += AdjustPageSize(((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)+MinHeapGap);
minimal_request = size; minimal_request = request;
size = Yap_ExtendWorkSpaceThroughHole(size); request = Yap_ExtendWorkSpaceThroughHole(request);
if (size < 0) { if (request < 0) {
LOCAL_ErrorMessage = "Global Stack crashed against Local Stack"; LOCAL_ErrorMessage = "Global Stack crashed against Local Stack";
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
return 0; return 0;
@ -958,15 +953,11 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
} }
} }
gc_verbose = Yap_is_gc_verbose(); gc_verbose = Yap_is_gc_verbose();
LOCAL_delay_overflows++;
if (gc_verbose) { if (gc_verbose) {
if (hsplit) { if (hsplit) {
if (hsplit > H0) { if (hsplit > H0) {
vb_msg1 = 'H'; vb_msg1 = 'H';
vb_msg2 = "Global Variable Space"; vb_msg2 = "Global Variable Space";
} else {
vb_msg1 = 'D';
vb_msg2 = "Global Variable Delay Space";
} }
} else { } else {
vb_msg1 = 'D'; vb_msg1 = 'D';
@ -976,8 +967,9 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
fprintf(stderr, "%% Worker Id %d:\n", worker_id); fprintf(stderr, "%% Worker Id %d:\n", worker_id);
#endif #endif
fprintf(stderr, "%% %cO %s Overflow %d\n", vb_msg1, vb_msg2, LOCAL_delay_overflows); fprintf(stderr, "%% %cO %s Overflow %d\n", vb_msg1, vb_msg2, LOCAL_delay_overflows);
fprintf(stderr, "%% %cO growing the stacks " UInt_FORMAT " bytes\n", vb_msg1, size); fprintf(stderr, "%% %cO growing the stacks " UInt_FORMAT " bytes\n", vb_msg1, request);
} }
printf("grow=%d %p\n", do_grow, ASP);
ASP -= 256; ASP -= 256;
YAPEnterCriticalSection(); YAPEnterCriticalSection();
/* we always shift the local and the stack by the same amount */ /* we always shift the local and the stack by the same amount */
@ -985,42 +977,40 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
/* we got over a hole */ /* we got over a hole */
if (minimal_request) { if (minimal_request) {
/* we went over a hole */ /* we went over a hole */
LOCAL_BaseDiff = size+((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)-minimal_request; LOCAL_BaseDiff = request+((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)-minimal_request;
LOCAL_LDiff = LOCAL_TrDiff = size; LOCAL_LDiff = LOCAL_TrDiff = request;
} else { } else {
/* we may still have an overflow */ /* we may still have an overflow */
LOCAL_BaseDiff = LOCAL_GlobalBase - old_GlobalBase; LOCAL_BaseDiff = LOCAL_GlobalBase - old_GlobalBase;
/* if we grow, we need to move the stacks */ /* if we grow, we need to move the stacks */
LOCAL_LDiff = LOCAL_TrDiff = LOCAL_BaseDiff+size; LOCAL_LDiff = LOCAL_TrDiff = LOCAL_BaseDiff+request;
} }
} else { } else {
/* stay still */ /* stay still */
LOCAL_LDiff = LOCAL_TrDiff = 0; LOCAL_LDiff = LOCAL_TrDiff = 0;
LOCAL_BaseDiff = 0; LOCAL_BaseDiff = 0;
} }
printf("grow=%d\n", LOCAL_LDiff);
/* now, remember we have delay -- global with a hole in delay or a /* now, remember we have delay -- global with a hole in delay or a
hole in global */ hole in global */
if (!hsplit) { if (!hsplit) {
if (!do_grow) { if (!do_grow) {
LOCAL_DelayDiff = LOCAL_GDiff = LOCAL_GDiff0 = size; LOCAL_GDiff = LOCAL_GDiff0 = request;
request = 0L; request = 0L;
} else { } else {
/* expand delay stack */
LOCAL_DelayDiff = LOCAL_GDiff = LOCAL_GDiff0 = LOCAL_LDiff;
}
} else if (insert_in_delays) {
/* we want to expand a hole for the delay stack */ /* we want to expand a hole for the delay stack */
LOCAL_DelayDiff = size-request; LOCAL_GDiff = LOCAL_GDiff0 = LOCAL_LDiff;
LOCAL_GDiff = LOCAL_GDiff0 = size; }
} else { } else {
/* we want to expand a hole for the delay stack */ /* we want to expand a hole for the delay stack */
LOCAL_GDiff0 = LOCAL_DelayDiff = LOCAL_BaseDiff; LOCAL_GDiff0 = LOCAL_BaseDiff;
LOCAL_GDiff = LOCAL_BaseDiff+request; LOCAL_GDiff = LOCAL_BaseDiff+request;
} }
LOCAL_GSplit = hsplit; LOCAL_GSplit = hsplit;
LOCAL_XDiff = LOCAL_HDiff = 0; LOCAL_XDiff = LOCAL_HDiff = 0;
LOCAL_GlobalBase = old_GlobalBase; LOCAL_GlobalBase = old_GlobalBase;
SetHeapRegs(FALSE PASS_REGS); fprintf(stderr,"off %d 5d %d\n", LOCAL_GDiff0, LOCAL_GDiff, LOCAL_LDiff);
SetHeapRegs(FALSE PASS_REGS);
if (do_grow) { if (do_grow) {
MoveLocalAndTrail( PASS_REGS1 ); MoveLocalAndTrail( PASS_REGS1 );
if (hsplit) { if (hsplit) {
@ -1042,17 +1032,13 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
*ptr = PtoLocAdjust(*ptr); *ptr = PtoLocAdjust(*ptr);
} }
if (hsplit) { if (hsplit) {
if (insert_in_delays) { MoveHalfGlobal(hsplit, request PASS_REGS);
/* we have things not quite where we want to have them */ printf("done\n");
cpcellsd((CELL *)(omax+LOCAL_DelayDiff), (CELL *)(omax+LOCAL_GDiff0), (ADDR)hsplit-omax);
} else {
MoveHalfGlobal(hsplit PASS_REGS);
}
} }
YAPLeaveCriticalSection(); YAPLeaveCriticalSection();
ASP += 256; ASP += 256;
if (minimal_request) { if (minimal_request) {
Yap_AllocHole(minimal_request, size); Yap_AllocHole(minimal_request, request);
} }
growth_time = Yap_cputime()-start_growth_time; growth_time = Yap_cputime()-start_growth_time;
LOCAL_total_delay_overflow_time += growth_time; LOCAL_total_delay_overflow_time += growth_time;
@ -1063,8 +1049,9 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
if (hsplit) { if (hsplit) {
return request; return request;
} else } else {
return LOCAL_GDiff-LOCAL_BaseDiff; return LOCAL_GDiff-LOCAL_BaseDiff;
}
} }
static void static void
@ -1548,6 +1535,7 @@ UInt
Yap_InsertInGlobal(CELL *where, size_t howmuch) Yap_InsertInGlobal(CELL *where, size_t howmuch)
{ {
CACHE_REGS CACHE_REGS
printf("where=%p, hm=%d\n",where,howmuch);
if ((howmuch = static_growglobal(howmuch, NULL, where PASS_REGS)) == 0) if ((howmuch = static_growglobal(howmuch, NULL, where PASS_REGS)) == 0)
return 0; return 0;
#ifdef TABLING #ifdef TABLING
@ -1576,6 +1564,7 @@ Yap_growstack(size_t size)
int res; int res;
LOCAL_PrologMode |= GrowStackMode; LOCAL_PrologMode |= GrowStackMode;
printf("extra %dBs\n",size);
res=growstack(size PASS_REGS); res=growstack(size PASS_REGS);
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
return res; return res;

View File

@ -2039,7 +2039,7 @@ static UInt suspend_indexing(ClauseDef *min, ClauseDef *max, PredEntry *ap,
/* give it some slack */ /* give it some slack */
tels = cls + 4; tels = cls + 4;
} else { } else {
tels = cls; tels = cls+1;
} }
sz = (UInt)NEXTOP((yamop *)NULL, sssllp) + tels * sizeof(yamop *); sz = (UInt)NEXTOP((yamop *)NULL, sssllp) + tels * sizeof(yamop *);
if ((ncode = (yamop *)Yap_AllocCodeSpace(sz)) == NULL) { if ((ncode = (yamop *)Yap_AllocCodeSpace(sz)) == NULL) {

View File

@ -586,20 +586,18 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
q.p = P; q.p = P;
q.cp = CP; q.cp = CP;
q.b0 = LCL0-CellPtr(B);
q.env0 = LCL0-ENV;
for (arity_t i = 0; i < arity; i++) for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i].term(); XREGS[i + 1] = ts[i].term();
// allow Prolog style exceotion handling // allow Prolog style exceotion handling
// don't forget, on success these bindings will still be there); // don't forget, on success these bindings will still be there);
result = YAP_LeaveGoal(true, &q); result = YAP_EnterGoal(ap.ap, nullptr, &q);
YAP_LeaveGoal(result, &q);
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
YAPCatchError(); YAPCatchError();
Yap_CloseHandles(q.CurSlot);
pop_text_stack(q.lvl + 1);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return result; return result;
} }
@ -613,6 +611,7 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
// _save = PyEval_SaveThread(); // _save = PyEval_SaveThread();
#endif #endif
CACHE_REGS CACHE_REGS
YAP_dogoalinfo q;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term *ts = nullptr; Term *ts = nullptr;
q.CurSlot = Yap_StartSlots(); q.CurSlot = Yap_StartSlots();
@ -662,13 +661,14 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
void YAPEngine::release() { void YAPEngine::release() {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
YAP_LeaveGoal(FALSE, &q); // YAP_LeaveGoal(FALSE, &q);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
Term YAPEngine::fun(Term t) { Term YAPEngine::fun(Term t) {
CACHE_REGS CACHE_REGS
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
YAP_dogoalinfo q;
Term tmod = Yap_CurrentModule(), *ts = nullptr; Term tmod = Yap_CurrentModule(), *ts = nullptr;
PredEntry *ap; PredEntry *ap;
arity_t arity; arity_t arity;
@ -1143,14 +1143,15 @@ std::stringstream s;
void YAPEngine::reSet() { void YAPEngine::reSet() {
/* ignore flags for now */ /* ignore flags for now */
if (B && B->cp_b && B->cp_ap != NOCODE) if (B && B->cp_b && B->cp_ap != NOCODE)
YAP_LeaveGoal(false, &q); // YAP_LeaveGoal(false, &q);
LOCAL_ActiveError->errorNo = YAP_NO_ERROR; LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
if (LOCAL_CommittedError) { if (LOCAL_CommittedError) {
LOCAL_CommittedError->errorNo = YAP_NO_ERROR; LOCAL_CommittedError->errorNo = YAP_NO_ERROR;
free(LOCAL_CommittedError); free(LOCAL_CommittedError);
LOCAL_CommittedError = NULL; LOCAL_CommittedError = NULL;
} }
LOCAL_CurSlot = q.CurSlot; pop_text_stack(0);
LOCAL_CurSlot = 0;
} }
Term YAPEngine::top_level(std::string s) { Term YAPEngine::top_level(std::string s) {

View File

@ -39,7 +39,6 @@ class X_API YAPQuery : public YAPPredicate {
bool q_open; bool q_open;
int q_state; int q_state;
yhandle_t q_handles; yhandle_t q_handles;
struct yami *q_p, *q_cp;
int q_flags; int q_flags;
YAP_dogoalinfo q_h; YAP_dogoalinfo q_h;
YAPPairTerm names; YAPPairTerm names;
@ -54,10 +53,10 @@ class X_API YAPQuery : public YAPPredicate {
q_state = 0; q_state = 0;
q_flags = true; // PL_Q_PASS_EXCEPTION; q_flags = true; // PL_Q_PASS_EXCEPTION;
q_p = P; q_h.p = P;
q_cp = CP; q_h.cp = CP;
// make sure this is safe // make sure this is safe
q_handles = LOCAL_CurSlot; q_h.CurSlot = LOCAL_CurSlot;
}; };
void openQuery(); void openQuery();
@ -321,7 +320,6 @@ private:
YAPCallback *_callback; YAPCallback *_callback;
YAPError yerror; YAPError yerror;
void doInit(YAP_file_type_t BootMode, YAPEngineArgs *cargs); void doInit(YAP_file_type_t BootMode, YAPEngineArgs *cargs);
YAP_dogoalinfo q;
YAPError e; YAPError e;
PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term &t, Term tmod); PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term &t, Term tmod);

View File

@ -297,6 +297,7 @@ check_function_exists(mkstemp HAVE_MKSTEMP)
check_function_exists(mktemp HAVE_MKTEMP) check_function_exists(mktemp HAVE_MKTEMP)
check_function_exists(nanosleep HAVE_NANOSLEEP) check_function_exists(nanosleep HAVE_NANOSLEEP)
check_function_exists(mktime HAVE_MKTIME) check_function_exists(mktime HAVE_MKTIME)
check_function_exists(mtrace HAVE_MTRACE)
check_function_exists(opendir HAVE_OPENDIR) check_function_exists(opendir HAVE_OPENDIR)
if (NOT APPLE) if (NOT APPLE)
check_function_exists(open_memstream HAVE_OPEN_MEMSTREAM) check_function_exists(open_memstream HAVE_OPEN_MEMSTREAM)

View File

@ -942,6 +942,11 @@ function. */
#cmakedefine HAVE_MPI_H ${HAVE_MPI_H} #cmakedefine HAVE_MPI_H ${HAVE_MPI_H}
#endif #endif
/* Define to 1 if you have the <mtrace> glibc extension. */
#ifndef HAVE_MPI_H
#cmakedefine HAVE_MTRACE ${HAVE_TRACE}
#endif
/* Older versions of MPZ didn't have XOR */ /* Older versions of MPZ didn't have XOR */
#ifndef HAVE_MPZ_XOR #ifndef HAVE_MPZ_XOR
#cmakedefine HAVE_MPZ_XOR ${HAVE_MPZ_XOR} #cmakedefine HAVE_MPZ_XOR ${HAVE_MPZ_XOR}

View File

@ -144,7 +144,7 @@ typedef struct {
struct yami *p; //> Program Counter at entry struct yami *p; //> Program Counter at entry
struct yami *cp; //> Continuation PC at entry struct yami *cp; //> Continuation PC at entry
int lvl; int lvl;
long env; long env0;
unsigned long tr, h; unsigned long tr, h;
} YAP_dogoalinfo; } YAP_dogoalinfo;

View File

@ -1933,6 +1933,19 @@ static Int p_sleep(USES_REGS1) {
return FALSE: return FALSE:
#endif #endif
} }
static Int
p_mtrace()
{
#ifdef HAVE_MTRACE
Term t = Deref(ARG1);
if (t == TermTrue) mtrace();
else if (t == TermFalse) return muntrace();
else return false;
#endif
return true;
}
void Yap_InitSysPreds(void) { void Yap_InitSysPreds(void) {
Yap_InitCPred("log_event", 1, p_log_event, SafePredFlag | SyncPredFlag); Yap_InitCPred("log_event", 1, p_log_event, SafePredFlag | SyncPredFlag);
@ -1972,5 +1985,6 @@ void Yap_InitSysPreds(void) {
Yap_InitCPred("rmdir", 2, p_rmdir, SyncPredFlag); Yap_InitCPred("rmdir", 2, p_rmdir, SyncPredFlag);
Yap_InitCPred("sleep", 1, p_sleep, SyncPredFlag); Yap_InitCPred("sleep", 1, p_sleep, SyncPredFlag);
Yap_InitCPred("make_directory", 1, make_directory, SyncPredFlag); Yap_InitCPred("make_directory", 1, make_directory, SyncPredFlag);
Yap_InitCPred("mtrace", 1, p_mtrace, SyncPredFlag);
} }

View File

@ -729,6 +729,7 @@ char *Yap_TermToBuffer(Term t, int flags) {
t = Deref(t); t = Deref(t);
GLOBAL_Stream[sno].encoding = LOCAL_encoding; GLOBAL_Stream[sno].encoding = LOCAL_encoding;
GLOBAL_Stream[sno].status |= CloseOnException_Stream_f; GLOBAL_Stream[sno].status |= CloseOnException_Stream_f;
GLOBAL_Stream[sno].status &= ~FreeOnClose_Stream_f;
Yap_plwrite(t, GLOBAL_Stream + sno, 0, flags, GLOBAL_MaxPriority); Yap_plwrite(t, GLOBAL_Stream + sno, 0, flags, GLOBAL_MaxPriority);
char *new = Yap_MemExportStreamPtr(sno); char *new = Yap_MemExportStreamPtr(sno);

View File

@ -114,7 +114,8 @@ static foreign_t array_to_python_view(term_t addr, term_t type, term_t szt,
static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tlen) { static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tlen) {
size_t sz, i; size_t sz, i;
PyErr_Clear();
PyErr_Clear();
PyObject *pyl = term_to_python(pyt, true, NULL, true); PyObject *pyl = term_to_python(pyt, true, NULL, true);
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
@ -135,10 +136,10 @@ static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tle
PyList_SET_ITEM(pyl, i, t); PyList_SET_ITEM(pyl, i, t);
} }
if (PL_is_variable(tlen)) { if (PL_is_variable(tlen)) {
PL_unify_int64(tlen, sz); PL_unify_int64(tlen, sz);
} else { } else {
python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL); python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL);
} }
pyErrorAndReturn( true); pyErrorAndReturn( true);
} }

View File

@ -230,7 +230,6 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
bool rc = true; bool rc = true;
PyErr_Print(); PyErr_Print();
term_t inp = Yap_CurrentHandle(); term_t inp = Yap_CurrentHandle();
Yap_DebugPlWriteln(YAP_GetFromSlot(t));
context = find_obj(context, t, false); context = find_obj(context, t, false);
// Yap_DebugPlWriteln(yt); // Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) { switch (PL_term_type(t)) {
@ -320,8 +319,6 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
// t now refers to the index // t now refers to the index
if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) || if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) ||
!PL_get_nil(tail)) { !PL_get_nil(tail)) {
PL_reset_term_refs(i);
;
rc = false; rc = false;
} else { } else {
PyObject *i = term_to_python(t, true, NULL, false); PyObject *i = term_to_python(t, true, NULL, false);

View File

@ -23,10 +23,14 @@
#undef _XOPEN_SOURCE // python does its own thing #undef _XOPEN_SOURCE // python does its own thing
#endif #endif
#undef _POSIX_C_SOURCE
#include <Python.h> #include <Python.h>
#include <Yap.h> #include <Yap.h>
#include <SWI-Prolog.h> #include <SWI-Prolog.h>
#ifdef HAVE_STAT #ifdef HAVE_STAT
#undef HAVE_STATa #undef HAVE_STATa
@ -95,7 +99,7 @@ extern PyObject *py_Sys;
extern X_API bool python_in_python; extern X_API bool python_in_python;
extern bool pyStringToString; extern bool pyStringToString;
extern bool python_release_GIL(term_t gstate); extern bool python_release_GIL(term_t gstate);
extern term_t python_acquire_GIL(void); extern term_t python_acquire_GIL(void);
static inline Py_ssize_t get_p_int(PyObject *o, Py_ssize_t def) { static inline Py_ssize_t get_p_int(PyObject *o, Py_ssize_t def) {
@ -201,22 +205,21 @@ extern PyObject *YEC(PyObject *c,PyObject *a ,PyObject *d , int line, const char
extern void YEM(const char *ex, int line, const char *file, const char *code); extern void YEM(const char *ex, int line, const char *file, const char *code);
extern void pyErrorHandler__(int line, const char *file, const char *code); extern void pyErrorHandler__(int line, const char *file, const char *code);
#define pyErrorHandler() \ #define PyStart() PyErr_Clear()
{ \
if (PyErr_Occurred()) { \
#define pyErrorHandler() if (PyErr_Occurred()) { \
pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \ pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \
} \ } \
}
#define pyErrorAndReturn(x) \ #define pyErrorAndReturn(x) \
{ \
if (PyErr_Occurred()) { \ if (PyErr_Occurred()) { \
pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \ pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \
} \ } \
return (x); \ return (x)
}
// #define pyErrorAndReturn( x, y ) return x // #define pyErrorAndReturn( x, y ) return x
extern PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt); extern PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt);
extern PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt); extern PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt);

View File

@ -20,6 +20,7 @@ void pyErrorHandler__(int line, const char *file, const char *code) {
static foreign_t python_len(term_t tobj, term_t tf) { static foreign_t python_len(term_t tobj, term_t tf) {
Py_ssize_t len; Py_ssize_t len;
PyObject *o; PyObject *o;
PyStart();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
if (o == NULL) { if (o == NULL) {
@ -32,13 +33,14 @@ static foreign_t python_len(term_t tobj, term_t tf) {
static foreign_t python_represent( term_t name, term_t tobj) { static foreign_t python_represent( term_t name, term_t tobj) {
term_t stackp = python_acquire_GIL(); term_t stackp = python_acquire_GIL();
PyObject *e; PyObject *e;
PyStart();
e = term_to_python(tobj, false, NULL, false); e = term_to_python(tobj, false, NULL, false);
if (e == NULL) { if (e == NULL) {
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(false); pyErrorAndReturn(false);
} }
bool b = python_assign(name, e, NULL); foreign_t b = python_assign(name, e, NULL);
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(b); pyErrorAndReturn(b);
} }
@ -53,6 +55,7 @@ static foreign_t python_clear_errors(void) {
static foreign_t python_dir(term_t tobj, term_t tf) { static foreign_t python_dir(term_t tobj, term_t tf) {
PyObject *dir; PyObject *dir;
PyObject *o; PyObject *o;
PyStart();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
if (o == NULL) { if (o == NULL) {
@ -70,6 +73,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
PyObject *i; PyObject *i;
PyObject *o; PyObject *o;
PyObject *f; PyObject *f;
PyStart();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
if (o == NULL) { if (o == NULL) {
@ -96,6 +100,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
static foreign_t python_is(term_t tobj, term_t tf) { static foreign_t python_is(term_t tobj, term_t tf) {
PyObject *o; PyObject *o;
PyStart();
term_t lim = python_acquire_GIL(); term_t lim = python_acquire_GIL();
@ -112,12 +117,13 @@ static foreign_t python_is(term_t tobj, term_t tf) {
} }
static foreign_t python_proc(term_t tobj) { static foreign_t python_proc(term_t tobj) {
PyStart();
PyObject *o; PyObject *o;
term_t lim = python_acquire_GIL(); term_t lim = python_acquire_GIL();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
python_release_GIL(lim); python_release_GIL(lim);
bool rc = o != NULL; foreign_t rc = o != NULL;
pyErrorAndReturn(rc); pyErrorAndReturn(rc);
} }
@ -133,9 +139,7 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
p = term_to_python(parent, true, NULL, true); p = term_to_python(parent, true, NULL, true);
// Exp // Exp
if (!pI || !p) { if (!pI || !p) {
{ pyErrorAndReturn(false);
pyErrorAndReturn(false);
}
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) { } else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
PyErr_Print(); PyErr_Print();
{ pyErrorAndReturn(false); } { pyErrorAndReturn(false); }
@ -152,6 +156,7 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
static foreign_t python_apply(term_t tin, term_t targs, term_t keywds, static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t tf) { term_t tf) {
PyStart();
PyObject *pF; PyObject *pF;
PyObject *pArgs, *pKeywords; PyObject *pArgs, *pKeywords;
PyObject *pValue; PyObject *pValue;
@ -161,7 +166,6 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
pF = term_to_python(tin, true, NULL, true); pF = term_to_python(tin, true, NULL, true);
PyErr_Clear();
if (pF == NULL) { if (pF == NULL) {
{ {
pyErrorAndReturn(false); pyErrorAndReturn(false);
@ -236,7 +240,7 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
} }
static foreign_t assign_python(term_t exp, term_t name) { static foreign_t assign_python(term_t exp, term_t name) {
printf("A %ld\n", LOCAL_CurSlot); PyStart();
term_t stackp = python_acquire_GIL(); term_t stackp = python_acquire_GIL();
PyObject *e = term_to_python(exp, true, NULL, true); PyObject *e = term_to_python(exp, true, NULL, true);
@ -244,8 +248,7 @@ static foreign_t assign_python(term_t exp, term_t name) {
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(false); pyErrorAndReturn(false);
} }
bool b = python_assign(name, e, NULL); foreign_t b = python_assign(name, e, NULL);
printf("B %ld\n", LOCAL_CurSlot);
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(b); pyErrorAndReturn(b);
} }
@ -270,7 +273,7 @@ static foreign_t python_string_to(term_t f) {
} }
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) { static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
PyErr_Clear(); PyStart();
PyObject *pI, *pArgs, *pOut; PyObject *pI, *pArgs, *pOut;
PyObject *env; PyObject *env;
atom_t name; atom_t name;
@ -332,7 +335,7 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
} }
static foreign_t python_access(term_t obj, term_t f, term_t out) { static foreign_t python_access(term_t obj, term_t f, term_t out) {
PyErr_Clear(); PyStart();
PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF; PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF;
atom_t name; atom_t name;
char *s = NULL; char *s = NULL;
@ -401,7 +404,7 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
atom_t name; atom_t name;
char *s; char *s;
int arity; int arity;
PyStart();
if (!PL_get_name_arity(att, &name, &arity)) { if (!PL_get_name_arity(att, &name, &arity)) {
{ {
pyErrorAndReturn(false); pyErrorAndReturn(false);
@ -449,14 +452,14 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
static foreign_t python_main_module(term_t mod) { static foreign_t python_main_module(term_t mod) {
{ {
foreign_t rc; foreign_t rc;
PyErr_Clear(); PyStart();
rc = address_to_term(py_Main, mod); rc = address_to_term(py_Main, mod);
pyErrorAndReturn(rc); pyErrorAndReturn(rc);
} }
} }
static foreign_t python_function(term_t tobj) { static foreign_t python_function(term_t tobj) {
PyErr_Clear(); PyStart();
PyObject *obj = term_to_python(tobj, true, NULL, true); PyObject *obj = term_to_python(tobj, true, NULL, true);
foreign_t rc = PyFunction_Check(obj); foreign_t rc = PyFunction_Check(obj);
@ -466,7 +469,7 @@ static foreign_t python_function(term_t tobj) {
foreign_t python_builtin(term_t out) { foreign_t python_builtin(term_t out) {
{ {
foreign_t rc; foreign_t rc;
PyErr_Clear(); PyStart();
rc = address_to_term(py_Builtin, out); rc = address_to_term(py_Builtin, out);
pyErrorAndReturn(rc); pyErrorAndReturn(rc);
} }
@ -477,7 +480,7 @@ static foreign_t python_run_file(term_t file) {
size_t len; size_t len;
char si[256]; char si[256];
s = si; s = si;
PyErr_Clear(); PyStart();
if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) { if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
PyObject *PyFileObject = PyFile_FromString(si, "r"); PyObject *PyFileObject = PyFile_FromString(si, "r");
@ -500,11 +503,11 @@ extern PyThreadState *YAP_save;
static foreign_t python_run_command(term_t cmd) { static foreign_t python_run_command(term_t cmd) {
char *s; char *s;
bool rc = false; foreign_t rc = false;
size_t len; size_t len;
char si[256]; char si[256];
PyErr_Clear(); PyStart();
s = si; s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) { if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
PyRun_SimpleString(s); PyRun_SimpleString(s);
@ -520,7 +523,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
PyObject *pArgs = NULL, *pValue; PyObject *pArgs = NULL, *pValue;
char *s; char *s;
PyErr_Clear(); PyStart();
s = si; s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) && if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) &&
(s = sf) != NULL && (s = sf) != NULL &&
@ -572,7 +575,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
static foreign_t python_export(term_t t, term_t pl) { static foreign_t python_export(term_t t, term_t pl) {
foreign_t rc = false; foreign_t rc = false;
PyErr_Clear(); PyStart();
if (PL_is_functor(t, FUNCTOR_pointer1)) { if (PL_is_functor(t, FUNCTOR_pointer1)) {
void *ptr; void *ptr;
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
@ -601,7 +604,8 @@ static foreign_t python_export(term_t t, term_t pl) {
*/ */
static int python_import(term_t mname, term_t mod) { static int python_import(term_t mname, term_t mod) {
PyObject *pName; PyObject *pName;
bool do_as = false; foreign_t do_as = false;
PyStart();
char s0[MAXPATHLEN], *s = s0; char s0[MAXPATHLEN], *s = s0;
s[0] = '\0'; s[0] = '\0';
@ -674,7 +678,7 @@ static int python_import(term_t mname, term_t mod) {
static foreign_t python_to_rhs(term_t inp, term_t t) { static foreign_t python_to_rhs(term_t inp, term_t t) {
PyObject *pVal; PyObject *pVal;
PyErr_Clear(); PyStart();
pVal = term_to_python(inp, true, NULL, true); pVal = term_to_python(inp, true, NULL, true);
if (pVal == NULL) if (pVal == NULL)
pyErrorAndReturn(false); pyErrorAndReturn(false);
@ -682,7 +686,7 @@ static foreign_t python_to_rhs(term_t inp, term_t t) {
} }
// static PyThreadState *_saveP = NULL; // static PyThreadState *_saveP = NULL;
static bool _threaded = true; static foreign_t _threaded = true;
/* /*
static YAP_Int static YAP_Int
@ -708,7 +712,7 @@ PyThreadState *tstate;
static YAP_Int p_python_threaded(void) { static YAP_Int p_python_threaded(void) {
PyErr_Clear(); PyStart();
// PyEval_ReleaseThread(tstate); // PyEval_ReleaseThread(tstate);
// _threaded = true; // _threaded = true;
// _locked = 0; // _locked = 0;
@ -731,7 +735,6 @@ term_t python_acquire_GIL(void) {
PL_put_integer(curSlot, gstatei++); PL_put_integer(curSlot, gstatei++);
return curSlot; return curSlot;
} }
bool python_release_GIL(term_t curBlock) { bool python_release_GIL(term_t curBlock) {
int gstateix; int gstateix;
gstatei--; gstatei--;