From 7c16af4edfe39e48dabd84e09c14c2184cff8d85 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 1 May 2019 01:34:58 +0100 Subject: [PATCH] -Interface issues --- C/c_interface.c | 33 +++++++++-- C/globals.c | 83 ++++++++++++++-------------- C/grow.c | 113 +++++++++++++++++--------------------- C/index.c | 2 +- CXX/yapi.cpp | 19 ++++--- CXX/yapq.hh | 8 +-- cmake/Config.cmake | 1 + config.h.cmake | 5 ++ include/YapDefs.h | 2 +- os/sysbits.c | 14 +++++ os/writeterm.c | 1 + packages/python/pl2pl.c | 11 ++-- packages/python/py2pl.c | 3 - packages/python/py4yap.h | 21 ++++--- packages/python/pypreds.c | 53 +++++++++--------- 15 files changed, 201 insertions(+), 168 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index 9d4fac689..bd0496267 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1142,6 +1142,8 @@ X_API Int YAP_Execute(PredEntry *pe, CPredicate exec_code) { // if (pe->PredFlags & CArgsPredFlag) { // CurrentModule = pe->ModuleOfPred; //} + int lvl = push_text_stack(); + yhandle_t hdl = Yap_CurrentHandle(); if (pe->PredFlags & SWIEnvPredFlag) { CPredicateV codev = (CPredicateV)exec_code; struct foreign_context ctx; @@ -1161,12 +1163,15 @@ X_API Int YAP_Execute(PredEntry *pe, CPredicate exec_code) { // check for junk: open frames, etc */ if (ret) complete_exit(((choiceptr)(LCL0 - OASP)), FALSE, FALSE PASS_REGS); - else + else { complete_fail(((choiceptr)(LCL0 - OASP)), FALSE PASS_REGS); + } // CurrentModule = omod; if (!ret) { Yap_RaiseException(); } + Yap_RecoverHandles(0, hdl); + pop_text_stack( lvl ); return ret; } @@ -1739,17 +1744,18 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) { CACHE_REGS PredEntry *pe = ape; 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, // LOCAL_CurSlot); BACKUP_MACHINE_REGS(); + dgi->lvl = push_text_stack(); LOCAL_ActiveError->errorNo = YAP_NO_ERROR; LOCAL_PrologMode = UserMode; dgi->p = P; dgi->cp = CP; dgi->b0 = LCL0 - (CELL *)B; - printf("%ld\n", dgi->CurSlot); + dgi->env0 = LCL0 - ENV; // ensure our current ENV receives current P. 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; Yap_StartSlots(); } else { - printf("%ld\n", dgi->CurSlot); LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal } + pop_text_stack(dgi->lvl); RECOVER_MACHINE_REGS(); return out; } @@ -1785,6 +1791,7 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { bool out; BACKUP_MACHINE_REGS(); + dgi->lvl = push_text_stack(); myB = (choiceptr)(LCL0 - dgi->b_exit); myB0 = (choiceptr)(LCL0 - dgi->b_entry); CP = myB->cp_cp; @@ -1811,6 +1818,7 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal } + pop_text_stack(dgi->lvl); RECOVER_MACHINE_REGS(); 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, // LOCAL_CurSlot); BACKUP_MACHINE_REGS(); + + dgi->lvl = push_text_stack(); if (successful) { choiceptr nB = (choiceptr)(LCL0 - dgi->b_entry); if (B <= nB) { @@ -1834,13 +1844,17 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { Yap_signal(YAP_FAIL_SIGNAL); } B = (choiceptr)(LCL0 - dgi->b0); +#ifdef DEPTH_LIMIT + DEPTH = B->cp_depth; +#endif P = dgi->p; CP = dgi->cp; + YENV = ENV = LCL0-dgi->env0; LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal - printf("L %ld\n", dgi->CurSlot); + pop_text_stack(dgi->lvl); 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, // CP, LOCAL_CurSlot); return TRUE; @@ -1852,6 +1866,8 @@ X_API Int YAP_RunGoal(Term t) { yhandle_t cslot = LOCAL_CurSlot; BACKUP_MACHINE_REGS(); +int lvl = push_text_stack(); + LOCAL_AllowRestart = FALSE; LOCAL_PrologMode = UserMode; out = Yap_RunTopGoal(t, true); @@ -1860,6 +1876,7 @@ X_API Int YAP_RunGoal(Term t) { // We'll pass it through RECOVER_MACHINE_REGS(); LOCAL_CurSlot = cslot; + pop_text_stack(lvl); return out; } @@ -1938,6 +1955,7 @@ X_API Int YAP_RunGoalOnce(Term t) { yhandle_t CSlot; BACKUP_MACHINE_REGS(); + int lvl = push_text_stack(); CSlot = Yap_StartSlots(); LOCAL_PrologMode = UserMode; // Yap_heap_regs->yap_do_low_level_trace=true; @@ -1946,6 +1964,7 @@ X_API Int YAP_RunGoalOnce(Term t) { // Yap_CloseSlots(CSlot); if (!(oldPrologMode & UserCCallMode)) { /* called from top-level */ + pop_text_stack( lvl); LOCAL_AllowRestart = FALSE; RECOVER_MACHINE_REGS(); return out; @@ -1987,6 +2006,7 @@ X_API Int YAP_RunGoalOnce(Term t) { CP = old_CP; LOCAL_AllowRestart = FALSE; RECOVER_MACHINE_REGS(); + pop_text_stack( lvl); return out; } @@ -2262,6 +2282,7 @@ X_API char *YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) { } else { RECOVER_MACHINE_REGS(); if (buf == out.val.c) { + pop_text_stack(l); return buf; } else { if ( strlen(out.val.c ) < sze) { diff --git a/C/globals.c b/C/globals.c index 77e0d4868..cac4e43be 100644 --- a/C/globals.c +++ b/C/globals.c @@ -78,7 +78,7 @@ assert/1 or recorda/3. + The value lives on the Prolog (global) stack. This implies that lookup time is independent from the size of the term. 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. 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; WORKER_REGS(wid) - if (where == NULL || where == HR) { + // make sure we have enough room while (HR + size > ASP - 1024) { if (!Yap_gcl(size * sizeof(CELL), arity, ENV, P)) { Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); return TermNil; } } + if (where == NULL || where == HR) { t = CreateNewArena(HR, size); HR += size; } 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) { + size_t sz; LOCAL_ArenaOverflows++; if (size == 0) { 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) { size = 4096; } - if (pt == HR) { - if (HR + size > ASP - 1024) { + while (HR + size > ASP - 1024) { XREGS[arity + 1] = arena; if (!Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP))) { Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); - return FALSE; + return false; } 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); - HR += size; - } else { - XREGS[arity + 1] = arena; - /* try to recover some room */ - Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP)); - arena = XREGS[arity + 1]; - pt = ArenaLimit(arena); - if ((size = Yap_InsertInGlobal(pt, size * sizeof(CELL))) == 0) { - return FALSE; + pt = ArenaLimit(arena)+1; + + if (pt == HR) { + sz = old_size + size; + HR += size; + HR[ - 1] = EndSpecials; + } else { + if ((sz = Yap_InsertInGlobal(pt, sz * sizeof(CELL))) == 0) { + return FALSE; + } + pt[sz-1] = EndSpecials; } - size = size / sizeof(CELL); 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) { CACHE_REGS -restart : { Term arena = *arenap; CELL *max = ArenaLimit(arena); CELL *base = ArenaPt(arena); CELL *newH; UInt old_sz = ArenaSz(arena), new_size; - + while(true) { if (IN_BETWEEN(base, HR, max)) { base = HR; HR += cells; return base; } - if (base + cells > max - 1024) { - if (!GrowArena(arena, max, old_sz, old_sz + sizeof(CELL) * 1024, - arity PASS_REGS)) - return NULL; - goto restart; + if (base + cells > ASP - 1024) { + continue; } newH = base + cells; @@ -474,7 +469,9 @@ loop: HR += ap2[1] + 3; break; default: { - /* big int */ + + + /* big int */ UInt sz = (sizeof(MP_INT) + 3 * CellSize + ((MP_INT *)(ap2 + 2))->_mp_alloc * sizeof(mp_limb_t)) / CellSize, @@ -482,7 +479,7 @@ loop: if (HR > ASP - (MIN_ARENA_SIZE + sz)) { goto overflow; - } + } *ptf++ = AbsAppl(HR); HR[0] = (CELL)f; for (i = 1; i < sz; i++) { @@ -494,6 +491,7 @@ loop: continue; } *ptf = AbsAppl(HR); + ptf++; /* store the terms to visit */ #ifdef RATIONAL_TREES @@ -797,7 +795,7 @@ error_handler: case -1: if (arena == LOCAL_GlobalArena) 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); return 0L; } @@ -848,8 +846,8 @@ restart: HB = oldHB; if (arena == LOCAL_GlobalArena) LOCAL_GlobalArenaOverflows++; - if (!GrowArena(arena, old_top, old_size, Nar * sizeof(CELL), - arity + 2 PASS_REGS)) { + if ((arena=GrowArena(arena, old_top, old_size, Nar * sizeof(CELL), + arity + 2 PASS_REGS))==0) { Yap_Error(RESOURCE_ERROR_STACK, TermNil, "while creating large global term"); return 0L; @@ -1549,8 +1547,8 @@ static Int nb_queue(UInt arena_sz USES_REGS) { queue = Yap_MkApplTerm(FunctorNBQueue, QUEUE_FUNCTOR_ARITY, ar); if (!Yap_unify(queue, ARG1)) return FALSE; - if (arena_sz < 4 * 1024) - arena_sz = 4 * 1024; + if (arena_sz < 32 * 1024) + arena_sz = 32 * 1024; queue_arena = NewArena(arena_sz, worker_id, 1, NULL); if (queue_arena == 0L) { return FALSE; @@ -1694,8 +1692,8 @@ static Int p_nb_queue_enqueue(USES_REGS1) { } ARG3 = to; /* fprintf(stderr,"growing %ld cells\n",(unsigned long int)gsiz);*/ - if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) { - Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); + if ((arena=GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS))==0) { + Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); return 0L; } to = ARG3; @@ -2025,7 +2023,7 @@ restart: gsiz = 1024; } 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); 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) { CELL *qd = GetHeap(ARG1, "add_to_beam"), *oldH, *oldHB, *pt; UInt hsize, hmsize, old_sz; - Term arena, to, key; + Term arena, to, qsize, key; UInt mingrow; if (!qd) @@ -2377,9 +2375,9 @@ static Int p_nb_beam_add_to_beam(USES_REGS1) { oldHB = HB; HR = HB = ArenaPt(arena); old_sz = ArenaSz(arena); + qsize = IntegerOfTerm(qd[QUEUE_SIZE]); while (old_sz < MIN_ARENA_SIZE) { - UInt gsiz = hsize * 2; - + UInt gsiz = HR - RepPair(qd[QUEUE_HEAD]); HR = oldH; HB = oldHB; if (gsiz > 1024 * 1024) { @@ -2388,6 +2386,7 @@ static Int p_nb_beam_add_to_beam(USES_REGS1) { gsiz = 1024; } ARG3 = to; + /* fprintf(stderr,"growing %ld cells\n",(unsigned long int)gsiz);*/ if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) { Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); return 0L; diff --git a/C/grow.c b/C/grow.c index e80ee26f1..c41eb58d9 100755 --- a/C/grow.c +++ b/C/grow.c @@ -324,15 +324,15 @@ MoveGlobalWithHole( USES_REGS1 ) } static void -MoveHalfGlobal(CELL *OldPt USES_REGS) +MoveHalfGlobal(CELL *OldPt, size_t request USES_REGS) { /* * cpcellsd(To,From,NOfCells) - copy the cells downwards - in * absmi.asm */ UInt diff = LOCAL_OldH-OldPt; - CELL *NewPt = (CELL *)((char*)OldPt+LOCAL_GDiff); CELL *IntPt = (CELL *)((char*)OldPt+LOCAL_GDiff0); + CELL *NewPt = IntPt+request/sizeof(CELL); cpcellsd(NewPt, IntPt, diff); } @@ -393,7 +393,7 @@ AdjustTrail(bool adjusting_heap, bool thread_copying USES_REGS) register CELL reg = TrailTerm(ptt-1); #ifdef FROZEN_STACKS register CELL reg2 = TrailVal(ptt-1); -#endif + #endif ptt--; if (IsVarTerm(reg)) { @@ -436,6 +436,8 @@ fixPointerCells(CELL *pt, CELL *pt_bot, bool thread_copying USES_REGS) { while (pt > pt_bot) { 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 (IsOldLocal(reg)) *pt = LocalAdjust(reg); @@ -450,6 +452,8 @@ fixPointerCells(CELL *pt, CELL *pt_bot, bool thread_copying USES_REGS) } else if (IsPairTerm(reg)) { *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; ADDR old_GlobalBase = LOCAL_GlobalBase; UInt minimal_request = 0L; - Int size = request; + Int size = request/sizeof(CELL); char vb_msg1 = '\0', *vb_msg2; 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 size is how much space we allocate: it's negative if we just expand the delay stack. @@ -902,55 +908,44 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS) */ if (hsplit) { - /* just a little bit of sanity checking */ - 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 || + if (hsplit < H0 || hsplit > HR) - return FALSE; - else if (hsplit == (CELL *)omax) + return false; + if (hsplit == H0) hsplit = NULL; - if (size < 0 || - (Unsigned(HR)+size < Unsigned(ASP)-StackGap( PASS_REGS1 ) && - 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 (hsplit == HR && Unsigned(HR)+request < Unsigned(ASP)-StackGap( PASS_REGS1 )) { + return request; } } + if (size < 0 || + (Unsigned(HR)+request < Unsigned(ASP-StackGap( PASS_REGS1 )))) { + do_grow = false; + } if (do_grow) { - if (size < YAP_ALLOC_SIZE) - size = YAP_ALLOC_SIZE; - size = AdjustPageSize(size); + if (request < YAP_ALLOC_SIZE) + request = YAP_ALLOC_SIZE; + request = AdjustPageSize(request); } + + printf("grow=%d\n", do_grow); /* adjust to a multiple of 256) */ LOCAL_ErrorMessage = NULL; LOCAL_PrologMode |= GrowStackMode; start_growth_time = Yap_cputime(); + if (do_grow) { if (!GLOBAL_AllowGlobalExpansion) { LOCAL_ErrorMessage = "Global Stack crashed against Local Stack"; LeaveGrowMode(GrowStackMode); return 0; } - if (!GLOBAL_AllowGlobalExpansion || !Yap_ExtendWorkSpace(size)) { + if (!GLOBAL_AllowGlobalExpansion || !Yap_ExtendWorkSpace(request)) { /* always fails when using malloc */ LOCAL_ErrorMessage = NULL; - size += AdjustPageSize(((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)+MinHeapGap); - minimal_request = size; - size = Yap_ExtendWorkSpaceThroughHole(size); - if (size < 0) { + request += AdjustPageSize(((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)+MinHeapGap); + minimal_request = request; + request = Yap_ExtendWorkSpaceThroughHole(request); + if (request < 0) { LOCAL_ErrorMessage = "Global Stack crashed against Local Stack"; LeaveGrowMode(GrowStackMode); return 0; @@ -958,15 +953,11 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS) } } gc_verbose = Yap_is_gc_verbose(); - LOCAL_delay_overflows++; if (gc_verbose) { if (hsplit) { if (hsplit > H0) { vb_msg1 = 'H'; vb_msg2 = "Global Variable Space"; - } else { - vb_msg1 = 'D'; - vb_msg2 = "Global Variable Delay Space"; } } else { 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); #endif 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; YAPEnterCriticalSection(); /* 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 */ if (minimal_request) { /* we went over a hole */ - LOCAL_BaseDiff = size+((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)-minimal_request; - LOCAL_LDiff = LOCAL_TrDiff = size; + LOCAL_BaseDiff = request+((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)-minimal_request; + LOCAL_LDiff = LOCAL_TrDiff = request; } else { /* we may still have an overflow */ LOCAL_BaseDiff = LOCAL_GlobalBase - old_GlobalBase; /* if we grow, we need to move the stacks */ - LOCAL_LDiff = LOCAL_TrDiff = LOCAL_BaseDiff+size; + LOCAL_LDiff = LOCAL_TrDiff = LOCAL_BaseDiff+request; } } else { /* stay still */ LOCAL_LDiff = LOCAL_TrDiff = 0; LOCAL_BaseDiff = 0; } + printf("grow=%d\n", LOCAL_LDiff); /* now, remember we have delay -- global with a hole in delay or a hole in global */ if (!hsplit) { if (!do_grow) { - LOCAL_DelayDiff = LOCAL_GDiff = LOCAL_GDiff0 = size; + LOCAL_GDiff = LOCAL_GDiff0 = request; request = 0L; } 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 */ - LOCAL_DelayDiff = size-request; - LOCAL_GDiff = LOCAL_GDiff0 = size; + LOCAL_GDiff = LOCAL_GDiff0 = LOCAL_LDiff; + } } else { /* 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_GSplit = hsplit; LOCAL_XDiff = LOCAL_HDiff = 0; 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) { MoveLocalAndTrail( PASS_REGS1 ); if (hsplit) { @@ -1042,17 +1032,13 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS) *ptr = PtoLocAdjust(*ptr); } if (hsplit) { - if (insert_in_delays) { - /* we have things not quite where we want to have them */ - cpcellsd((CELL *)(omax+LOCAL_DelayDiff), (CELL *)(omax+LOCAL_GDiff0), (ADDR)hsplit-omax); - } else { - MoveHalfGlobal(hsplit PASS_REGS); - } + MoveHalfGlobal(hsplit, request PASS_REGS); + printf("done\n"); } YAPLeaveCriticalSection(); ASP += 256; if (minimal_request) { - Yap_AllocHole(minimal_request, size); + Yap_AllocHole(minimal_request, request); } growth_time = Yap_cputime()-start_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); if (hsplit) { return request; - } else + } else { return LOCAL_GDiff-LOCAL_BaseDiff; + } } static void @@ -1548,6 +1535,7 @@ UInt Yap_InsertInGlobal(CELL *where, size_t howmuch) { CACHE_REGS + printf("where=%p, hm=%d\n",where,howmuch); if ((howmuch = static_growglobal(howmuch, NULL, where PASS_REGS)) == 0) return 0; #ifdef TABLING @@ -1576,6 +1564,7 @@ Yap_growstack(size_t size) int res; LOCAL_PrologMode |= GrowStackMode; + printf("extra %dBs\n",size); res=growstack(size PASS_REGS); LeaveGrowMode(GrowStackMode); return res; diff --git a/C/index.c b/C/index.c index 03e9d41b0..23dd4da49 100755 --- a/C/index.c +++ b/C/index.c @@ -2039,7 +2039,7 @@ static UInt suspend_indexing(ClauseDef *min, ClauseDef *max, PredEntry *ap, /* give it some slack */ tels = cls + 4; } else { - tels = cls; + tels = cls+1; } sz = (UInt)NEXTOP((yamop *)NULL, sssllp) + tels * sizeof(yamop *); if ((ncode = (yamop *)Yap_AllocCodeSpace(sz)) == NULL) { diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index 9d6cd3d14..1056965dd 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -586,20 +586,18 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) { q.p = P; q.cp = CP; + q.b0 = LCL0-CellPtr(B); + q.env0 = LCL0-ENV; for (arity_t i = 0; i < arity; i++) XREGS[i + 1] = ts[i].term(); // allow Prolog style exceotion handling // 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(); - Yap_CloseHandles(q.CurSlot); - pop_text_stack(q.lvl + 1); - RECOVER_MACHINE_REGS(); return result; } @@ -613,6 +611,7 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) { // _save = PyEval_SaveThread(); #endif CACHE_REGS + YAP_dogoalinfo q; BACKUP_MACHINE_REGS(); Term *ts = nullptr; q.CurSlot = Yap_StartSlots(); @@ -662,13 +661,14 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) { void YAPEngine::release() { BACKUP_MACHINE_REGS(); - YAP_LeaveGoal(FALSE, &q); + // YAP_LeaveGoal(FALSE, &q); RECOVER_MACHINE_REGS(); } Term YAPEngine::fun(Term t) { CACHE_REGS BACKUP_MACHINE_REGS(); + YAP_dogoalinfo q; Term tmod = Yap_CurrentModule(), *ts = nullptr; PredEntry *ap; arity_t arity; @@ -1143,14 +1143,15 @@ std::stringstream s; void YAPEngine::reSet() { /* ignore flags for now */ if (B && B->cp_b && B->cp_ap != NOCODE) - YAP_LeaveGoal(false, &q); + // YAP_LeaveGoal(false, &q); LOCAL_ActiveError->errorNo = YAP_NO_ERROR; if (LOCAL_CommittedError) { LOCAL_CommittedError->errorNo = YAP_NO_ERROR; free(LOCAL_CommittedError); LOCAL_CommittedError = NULL; } - LOCAL_CurSlot = q.CurSlot; + pop_text_stack(0); + LOCAL_CurSlot = 0; } Term YAPEngine::top_level(std::string s) { diff --git a/CXX/yapq.hh b/CXX/yapq.hh index 9a13b7a38..9b1c1aafc 100644 --- a/CXX/yapq.hh +++ b/CXX/yapq.hh @@ -39,7 +39,6 @@ class X_API YAPQuery : public YAPPredicate { bool q_open; int q_state; yhandle_t q_handles; - struct yami *q_p, *q_cp; int q_flags; YAP_dogoalinfo q_h; YAPPairTerm names; @@ -54,10 +53,10 @@ class X_API YAPQuery : public YAPPredicate { q_state = 0; q_flags = true; // PL_Q_PASS_EXCEPTION; - q_p = P; - q_cp = CP; + q_h.p = P; + q_h.cp = CP; // make sure this is safe - q_handles = LOCAL_CurSlot; + q_h.CurSlot = LOCAL_CurSlot; }; void openQuery(); @@ -321,7 +320,6 @@ private: YAPCallback *_callback; YAPError yerror; void doInit(YAP_file_type_t BootMode, YAPEngineArgs *cargs); - YAP_dogoalinfo q; YAPError e; PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term &t, Term tmod); diff --git a/cmake/Config.cmake b/cmake/Config.cmake index 094d3ce42..2564ec571 100644 --- a/cmake/Config.cmake +++ b/cmake/Config.cmake @@ -297,6 +297,7 @@ check_function_exists(mkstemp HAVE_MKSTEMP) check_function_exists(mktemp HAVE_MKTEMP) check_function_exists(nanosleep HAVE_NANOSLEEP) check_function_exists(mktime HAVE_MKTIME) +check_function_exists(mtrace HAVE_MTRACE) check_function_exists(opendir HAVE_OPENDIR) if (NOT APPLE) check_function_exists(open_memstream HAVE_OPEN_MEMSTREAM) diff --git a/config.h.cmake b/config.h.cmake index bc2ab1242..822ad3116 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -942,6 +942,11 @@ function. */ #cmakedefine HAVE_MPI_H ${HAVE_MPI_H} #endif +/* Define to 1 if you have the glibc extension. */ +#ifndef HAVE_MPI_H +#cmakedefine HAVE_MTRACE ${HAVE_TRACE} +#endif + /* Older versions of MPZ didn't have XOR */ #ifndef HAVE_MPZ_XOR #cmakedefine HAVE_MPZ_XOR ${HAVE_MPZ_XOR} diff --git a/include/YapDefs.h b/include/YapDefs.h index 0cc49c885..aa3c49001 100755 --- a/include/YapDefs.h +++ b/include/YapDefs.h @@ -144,7 +144,7 @@ typedef struct { struct yami *p; //> Program Counter at entry struct yami *cp; //> Continuation PC at entry int lvl; - long env; + long env0; unsigned long tr, h; } YAP_dogoalinfo; diff --git a/os/sysbits.c b/os/sysbits.c index 966ee2eaf..97d103822 100644 --- a/os/sysbits.c +++ b/os/sysbits.c @@ -1933,6 +1933,19 @@ static Int p_sleep(USES_REGS1) { return FALSE: #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) { 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("sleep", 1, p_sleep, SyncPredFlag); Yap_InitCPred("make_directory", 1, make_directory, SyncPredFlag); + Yap_InitCPred("mtrace", 1, p_mtrace, SyncPredFlag); } diff --git a/os/writeterm.c b/os/writeterm.c index ce96bf572..ecaa14503 100644 --- a/os/writeterm.c +++ b/os/writeterm.c @@ -729,6 +729,7 @@ char *Yap_TermToBuffer(Term t, int flags) { t = Deref(t); GLOBAL_Stream[sno].encoding = LOCAL_encoding; GLOBAL_Stream[sno].status |= CloseOnException_Stream_f; + GLOBAL_Stream[sno].status &= ~FreeOnClose_Stream_f; Yap_plwrite(t, GLOBAL_Stream + sno, 0, flags, GLOBAL_MaxPriority); char *new = Yap_MemExportStreamPtr(sno); diff --git a/packages/python/pl2pl.c b/packages/python/pl2pl.c index b9661f772..b982bb652 100644 --- a/packages/python/pl2pl.c +++ b/packages/python/pl2pl.c @@ -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) { size_t sz, i; - PyErr_Clear(); + +PyErr_Clear(); PyObject *pyl = term_to_python(pyt, true, NULL, true); 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); } if (PL_is_variable(tlen)) { - PL_unify_int64(tlen, sz); -} else { -python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL); -} + PL_unify_int64(tlen, sz); + } else { + python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL); + } pyErrorAndReturn( true); } diff --git a/packages/python/py2pl.c b/packages/python/py2pl.c index 1d24f893a..27e89e81c 100644 --- a/packages/python/py2pl.c +++ b/packages/python/py2pl.c @@ -230,7 +230,6 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) { bool rc = true; PyErr_Print(); term_t inp = Yap_CurrentHandle(); - Yap_DebugPlWriteln(YAP_GetFromSlot(t)); context = find_obj(context, t, false); // Yap_DebugPlWriteln(yt); 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 if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) || !PL_get_nil(tail)) { - PL_reset_term_refs(i); - ; rc = false; } else { PyObject *i = term_to_python(t, true, NULL, false); diff --git a/packages/python/py4yap.h b/packages/python/py4yap.h index bae0be539..b2fc28a8d 100644 --- a/packages/python/py4yap.h +++ b/packages/python/py4yap.h @@ -23,10 +23,14 @@ #undef _XOPEN_SOURCE // python does its own thing #endif + +#undef _POSIX_C_SOURCE + #include #include + #include #ifdef HAVE_STAT #undef HAVE_STATa @@ -95,7 +99,7 @@ extern PyObject *py_Sys; extern X_API bool python_in_python; 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); 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 pyErrorHandler__(int line, const char *file, const char *code); -#define pyErrorHandler() \ - { \ - if (PyErr_Occurred()) { \ +#define PyStart() PyErr_Clear() + + +#define pyErrorHandler() if (PyErr_Occurred()) { \ pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \ } \ - } + #define pyErrorAndReturn(x) \ - { \ if (PyErr_Occurred()) { \ pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \ } \ - return (x); \ - } + return (x) + // #define pyErrorAndReturn( x, y ) return x - extern PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt); extern PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt); diff --git a/packages/python/pypreds.c b/packages/python/pypreds.c index 58ae72865..e4353430d 100644 --- a/packages/python/pypreds.c +++ b/packages/python/pypreds.c @@ -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) { Py_ssize_t len; PyObject *o; + PyStart(); o = term_to_python(tobj, true, NULL, true); 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) { term_t stackp = python_acquire_GIL(); PyObject *e; + PyStart(); e = term_to_python(tobj, false, NULL, false); if (e == NULL) { python_release_GIL(stackp); pyErrorAndReturn(false); } - bool b = python_assign(name, e, NULL); + foreign_t b = python_assign(name, e, NULL); python_release_GIL(stackp); pyErrorAndReturn(b); } @@ -53,6 +55,7 @@ static foreign_t python_clear_errors(void) { static foreign_t python_dir(term_t tobj, term_t tf) { PyObject *dir; PyObject *o; + PyStart(); o = term_to_python(tobj, true, NULL, true); if (o == NULL) { @@ -70,6 +73,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) { PyObject *i; PyObject *o; PyObject *f; + PyStart(); o = term_to_python(tobj, true, NULL, true); 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) { PyObject *o; + PyStart(); 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) { + PyStart(); PyObject *o; term_t lim = python_acquire_GIL(); o = term_to_python(tobj, true, NULL, true); python_release_GIL(lim); - bool rc = o != NULL; + foreign_t rc = o != NULL; 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); // Exp if (!pI || !p) { - { - pyErrorAndReturn(false); - } + pyErrorAndReturn(false); } else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) { PyErr_Print(); { 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, term_t tf) { + PyStart(); PyObject *pF; PyObject *pArgs, *pKeywords; 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(); pF = term_to_python(tin, true, NULL, true); - PyErr_Clear(); if (pF == NULL) { { 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) { - printf("A %ld\n", LOCAL_CurSlot); + PyStart(); term_t stackp = python_acquire_GIL(); 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); pyErrorAndReturn(false); } - bool b = python_assign(name, e, NULL); - printf("B %ld\n", LOCAL_CurSlot); + foreign_t b = python_assign(name, e, NULL); python_release_GIL(stackp); 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) { - PyErr_Clear(); + PyStart(); PyObject *pI, *pArgs, *pOut; PyObject *env; 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) { - PyErr_Clear(); + PyStart(); PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF; atom_t name; char *s = NULL; @@ -401,7 +404,7 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) { atom_t name; char *s; int arity; - + PyStart(); if (!PL_get_name_arity(att, &name, &arity)) { { 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) { { foreign_t rc; - PyErr_Clear(); + PyStart(); rc = address_to_term(py_Main, mod); pyErrorAndReturn(rc); } } static foreign_t python_function(term_t tobj) { - PyErr_Clear(); + PyStart(); PyObject *obj = term_to_python(tobj, true, NULL, true); 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 rc; - PyErr_Clear(); + PyStart(); rc = address_to_term(py_Builtin, out); pyErrorAndReturn(rc); } @@ -477,7 +480,7 @@ static foreign_t python_run_file(term_t file) { size_t len; char si[256]; s = si; - PyErr_Clear(); + PyStart(); if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) { #if PY_MAJOR_VERSION < 3 PyObject *PyFileObject = PyFile_FromString(si, "r"); @@ -500,11 +503,11 @@ extern PyThreadState *YAP_save; static foreign_t python_run_command(term_t cmd) { char *s; - bool rc = false; + foreign_t rc = false; size_t len; char si[256]; - PyErr_Clear(); + PyStart(); s = si; if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) { PyRun_SimpleString(s); @@ -520,7 +523,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) { PyObject *pArgs = NULL, *pValue; char *s; - PyErr_Clear(); + PyStart(); s = si; if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) && (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) { foreign_t rc = false; - PyErr_Clear(); + PyStart(); if (PL_is_functor(t, FUNCTOR_pointer1)) { void *ptr; 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) { PyObject *pName; - bool do_as = false; + foreign_t do_as = false; + PyStart(); char s0[MAXPATHLEN], *s = s0; 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) { PyObject *pVal; - PyErr_Clear(); + PyStart(); pVal = term_to_python(inp, true, NULL, true); if (pVal == NULL) pyErrorAndReturn(false); @@ -682,7 +686,7 @@ static foreign_t python_to_rhs(term_t inp, term_t t) { } // static PyThreadState *_saveP = NULL; -static bool _threaded = true; +static foreign_t _threaded = true; /* static YAP_Int @@ -708,7 +712,7 @@ PyThreadState *tstate; static YAP_Int p_python_threaded(void) { - PyErr_Clear(); + PyStart(); // PyEval_ReleaseThread(tstate); // _threaded = true; // _locked = 0; @@ -731,7 +735,6 @@ term_t python_acquire_GIL(void) { PL_put_integer(curSlot, gstatei++); return curSlot; } - bool python_release_GIL(term_t curBlock) { int gstateix; gstatei--;