From 8070e65354290ea18b4246f914191fed434662c1 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 12 Apr 2019 10:48:01 +0100 Subject: [PATCH] goal --- C/absmi_insts.h | 1 + C/c_interface.c | 67 ++++++++++++++++------------- C/exec.c | 104 ++++++++++++++++++++++++++-------------------- C/stack.c | 2 + CXX/yapi.cpp | 3 +- H/Yapproto.h | 2 +- H/absmi.h | 2 +- include/YapDefs.h | 2 +- pl/boot.yap | 2 +- 9 files changed, 107 insertions(+), 78 deletions(-) diff --git a/C/absmi_insts.h b/C/absmi_insts.h index a26eb830b..6aeaff66f 100644 --- a/C/absmi_insts.h +++ b/C/absmi_insts.h @@ -25,6 +25,7 @@ ENDBOp(); BOp(Nstop, e); + //B= B->cp_b; //SET_ASP(YREG, E_CB * sizeof(CELL)); saveregs(); #if PUSH_REGS diff --git a/C/c_interface.c b/C/c_interface.c index 6efe45e64..a538433c8 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1755,7 +1755,7 @@ static int run_emulator(USES_REGS1) { X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) { CACHE_REGS PredEntry *pe = ape; - bool out; + bool out; fprintf(stderr,"EnterGoal: H=%ld ENV=%ld B=%ld TR=%ld P=%p CP=%p, Slots=%ld\n",HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); @@ -1764,7 +1764,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) { LOCAL_PrologMode = UserMode; dgi->p = P; dgi->cp = CP; - dgi->b0 = LCL0 - (CELL *)B; + dgi->b_top = LCL0 - (CELL *)B; dgi->e = LCL0-ENV; dgi->CurSlot = LOCAL_CurSlot; // ensure our current ENV receives current P. @@ -1774,17 +1774,15 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) { // __android_log_print(ANDROID_LOG_INFO, "YAP ", "ap=%p %ld %x %x args=%x,%x // slot=%ld", pe, pe->CodeOfPred->opc, FAILCODE, Deref(ARG1), Deref(ARG2), // LOCAL_CurSlot); - dgi->b = LCL0 - (CELL *)B; dgi->h = HR - H0; dgi->tr = (CELL *)TR - LCL0; - // fprintf(stderr,"PrepGoal: H=%ld ENV=%p B=%ld TR=%ld P=%p CP=%p Slots=%ld\n", - // HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); - out = Yap_exec_absmi(true, false); - // fprintf(stderr,"EnterGoal success=%ld: H=%ld ENV=%p B=%ld TR=%ld P=%p CP=%p - // Slots=%ld\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, - // LOCAL_CurSlot); - dgi->b = (LCL0 - (CELL*)B); - dgi->e = LCL0 - (CELL *)B; + // HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P11= (LCL0 - (CELL*)B); + out = Yap_exec_absmi(true, false); + // fprintf(stderr,"EnterGoal success=%ld: H=%ld ENV=%p B=%ld TR=%ld P=%p CP=%p + // Slots=%ld\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, + // LOCAL_CurSlot); + dgi->b_bottom = (LCL0 - (CELL*)B); + dgi->e = LCL0 - (CELL *)ENV; dgi->a = LCL0 - (CELL *)ASP; if (out) { dgi->EndSlot = LOCAL_CurSlot; @@ -1803,8 +1801,8 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { bool out; BACKUP_MACHINE_REGS(); - myB = (choiceptr)(LCL0 - dgi->b); - myB0 = (choiceptr)(LCL0 - dgi->b0); + myB = (choiceptr)(LCL0 - dgi->b_top); + myB0 = (choiceptr)(LCL0 - dgi->b_bottom); CP = myB->cp_cp; /* sanity check */ if (B >= myB0) { @@ -1822,7 +1820,7 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { out = Yap_exec_absmi(true, true ); if (out) { dgi->EndSlot = LOCAL_CurSlot; - dgi->b = LCL0-CellPtr(myB); + dgi->b_bottom = LCL0-CellPtr(myB); } else { LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal @@ -1840,7 +1838,7 @@ 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(); - myB = (choiceptr)(LCL0 - dgi->b); + myB = (choiceptr)(LCL0 - dgi->b_bottom); if (LOCAL_PrologMode & AsyncIntMode) { Yap_signal(YAP_FAIL_SIGNAL); } @@ -1868,7 +1866,7 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { CP = dgi->cp; ENV = LCL0-dgi->e; ASP = LCL0-dgi->a; - B = (choiceptr)(LCL0-dgi->b) + B = (choiceptr)(LCL0-dgi->b_top) RECOVER_MACHINE_REGS(); fprintf(stderr,"LeftGoal success=%d: H=%ld ENV=%ld B=%ld TR=%ld P=%p CP=%p, Slots=%ld\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); @@ -1878,12 +1876,19 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { X_API Int YAP_RunGoal(Term t) { CACHE_REGS Term out; - yhandle_t cslot = LOCAL_CurSlot; + YAP_dogoalinfo gi; + gi.p = P; + gi.cp = CP; + gi.b_top = LCL0-CellPtr(B); + gi.CurSlot = Yap_CurrentHandle(); + gi.a = LCL0-ASP; + gi.e = LCL0-ENV; + yhandle_t cslot = LOCAL_CurSlot; BACKUP_MACHINE_REGS(); LOCAL_AllowRestart = FALSE; LOCAL_PrologMode = UserMode; - out = Yap_RunTopGoal(t, true); + out = Yap_RunTopGoal(t, &gi, true); LOCAL_PrologMode = UserCCallMode; // should we catch the exception or pass it through? // We'll pass it through @@ -1962,15 +1967,21 @@ X_API CELL *YAP_HeapStoreOpaqueTerm(Term t) { X_API Int YAP_RunGoalOnce(Term t) { CACHE_REGS Term out; - yamop *old_CP = CP; - Int oldPrologMode = LOCAL_PrologMode; + YAP_dogoalinfo gi; + gi.p = P; + gi.cp = CP; + gi.b_top = LCL0-CellPtr(B); + gi.CurSlot = Yap_CurrentHandle(); + gi.a = LCL0-ASP; + gi.e = LCL0-ENV; + Int oldPrologMode = LOCAL_PrologMode; yhandle_t CSlot; BACKUP_MACHINE_REGS(); CSlot = Yap_StartSlots(); LOCAL_PrologMode = UserMode; // Yap_heap_regs->yap_do_low_level_trace=true; - out = Yap_RunTopGoal(t, true); + out = Yap_RunTopGoal(t, &gi, true); LOCAL_PrologMode = oldPrologMode; // Yap_CloseSlots(CSlot); if (!(oldPrologMode & UserCCallMode)) { @@ -2006,14 +2017,14 @@ X_API Int YAP_RunGoalOnce(Term t) { } else { Yap_CloseSlots(CSlot); } - ASP = B->cp_env; - ENV = (CELL *)ASP[E_E]; - B = (choiceptr)ASP[E_CB]; -#ifdef DEPTH_LIMITxs + ASP = LCL0-gi.a; + ENV = LCL0-gi.e; + B = (choiceptr)(LCL0-gi.b_top); +#ifdef DEPTH_LIMIT DEPTH = ASP[E_DEPTH]; #endif - P = (yamop *)ASP[E_CP]; - CP = old_CP; + P = gi.p; + CP = gi.cp; LOCAL_AllowRestart = FALSE; RECOVER_MACHINE_REGS(); return out; @@ -2098,7 +2109,7 @@ X_API void YAP_PruneGoal(YAP_dogoalinfo *gi) { CACHE_REGS BACKUP_B(); - choiceptr myB = (choiceptr)(LCL0 - gi->b); + choiceptr myB = (choiceptr)(LCL0 - gi->b_top); while (B != myB) { /* make sure we prune C-choicepoints */ if (POP_CHOICE_POINT(B->cp_b)) { diff --git a/C/exec.c b/C/exec.c index c0c51f72c..13cf8b254 100755 --- a/C/exec.c +++ b/C/exec.c @@ -808,7 +808,7 @@ static void prune_inner_computation(choiceptr parent) { Int oENV = LCL0 - ENV; cut_pt = B; - while (cut_pt && cut_pt->cp_b < parent) { + while (cut_pt && cut_pt->cp_b < parent && cut_pt->cp_b->cp_ap != NOCODE) { cut_pt = cut_pt->cp_b; } if (!cut_pt) @@ -853,25 +853,34 @@ static void complete_inner_computation(choiceptr old_B) { } static Int Yap_ignore(Term t, bool fail USES_REGS) { - yamop *oP = P, *oCP = CP; - Int oENV = LCL0 - ENV; - Int oYENV = LCL0 - YENV; - Int oB = LCL0 - (CELL *)B; + YAP_dogoalinfo gi; + gi.p = P; + gi.cp = CP; + gi.b_top = LCL0-CellPtr(B); + gi.CurSlot = Yap_CurrentHandle(); + gi.a = LCL0-ASP; + gi.e = LCL0-ENV; + // gi. = LCL0-YENV; yap_error_descriptor_t *ctx = malloc(sizeof(yap_error_descriptor_t)); bool newxp = Yap_pushErrorContext(true, ctx); - bool rc = Yap_RunTopGoal(t, false); + bool rc = Yap_RunTopGoal(t, &gi, false); + choiceptr B0 = (choiceptr)(LCL0-gi.b_top); if (!rc) { - complete_inner_computation((choiceptr)(LCL0 - oB)); + complete_inner_computation(B0); // We'll pass it through } else { - prune_inner_computation((choiceptr)(LCL0 - oB)); + prune_inner_computation(B0); } Yap_popErrorContext(newxp, true); - P = oP; - CP = oCP; - ENV = LCL0 - oENV; - YENV = LCL0 - oYENV; - B = (choiceptr)(LCL0 - oB); + ASP = LCL0-gi.a; + ENV = LCL0-gi.e; + B = (choiceptr)(LCL0-gi.b_top); +#ifdef DEPTH_LIMIT + DEPTH = ASP[E_DEPTH]; +#endif + P = gi.p; + CP = gi.cp; + //YENV? return true; } @@ -1001,18 +1010,22 @@ static bool watch_retry(Term d0 USES_REGS) { static Int setup_call_catcher_cleanup(USES_REGS1) { Term Setup = Deref(ARG1); - choiceptr B0 = B; - yamop *oP = P, *oCP = CP; - Int oENV = LCL0 - ENV; - Int oYENV = LCL0 - YENV; - bool rc; + YAP_dogoalinfo gi; + gi.p = P; + gi.cp = CP; + gi.b_top = LCL0-CellPtr(B); + gi.CurSlot = Yap_CurrentHandle(); + gi.a = LCL0-ASP; + gi.e = LCL0-ENV; + bool rc; Yap_DisableInterrupts(worker_id); - rc = Yap_RunTopGoal(Setup, false); + rc = Yap_RunTopGoal(Setup, &gi, false); Yap_EnableInterrupts(worker_id); if (Yap_RaiseException()) { return false; } + choiceptr B0=(choiceptr)(LCL0-gi.b_top); if (!rc) { complete_inner_computation(B0); // We'll pass it throughs @@ -1021,11 +1034,15 @@ static Int setup_call_catcher_cleanup(USES_REGS1) { } else { prune_inner_computation(B0); } - P = oP; - CP = oCP; - ENV = LCL0 - oENV; - YENV = LCL0 - oYENV; - return rc; + ASP = LCL0-gi.a; + ENV = LCL0-gi.e; + B = (choiceptr)(LCL0-gi.b_top); +#ifdef DEPTH_LIMIT + DEPTH = ASP[E_DEPTH]; +#endif + P = gi.p; + CP = gi.cp; + return rc; } static Int tag_cleanup(USES_REGS1) { @@ -1628,7 +1645,7 @@ void Yap_PrepGoal(arity_t arity, CELL *pt, YAP_dogoalinfo *gip USES_REGS) { Yap_ResetException(worker_id); // sl = Yap_InitSlot(t); YENV = ASP; - YENV[E_CP] = (CELL)YESCODE; + YENV[E_CP] = (CELL)CP; YENV[E_CB] = (CELL)B; YENV[E_E] = (CELL)ENV; #ifdef TABLING @@ -1648,20 +1665,24 @@ void Yap_PrepGoal(arity_t arity, CELL *pt, YAP_dogoalinfo *gip USES_REGS) { XREGS[i + 1] = *pt++; } } + choiceptr oB = B; B = (choiceptr)ASP; B--; B->cp_h = HR; B->cp_tr = TR; - B->cp_cp = CP; + B->cp_cp = YESCODE; B->cp_ap = NOCODE; B->cp_env = ENV; + B->cp_b = oB; #ifdef DEPTH_LIMIT B->cp_depth = DEPTH; #endif /* DEPTH_LIMIT */ - YENV = ASP = (CELL *)B; - YENV[E_CB] = (CELL)B; + ASP = (CELL *)B; + ASP[E_CB] = (CELL)B; HB = HR; - gip->b = LCL0-CellPtr(B); + CP = YESCODE; + ASP -= EnvSizeInCells; + gip->b_bottom = LCL0-CellPtr(B); } static bool do_goal(yamop *CodeAdr, int arity, CELL *pt, YAP_dogoalinfo *gi, bool top USES_REGS) { @@ -1743,7 +1764,7 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) { YAP_dogoalinfo gi; gi.p = P; gi.cp = CP; - gi.b0 = LCL0-CellPtr(B); + gi.b_top = LCL0-CellPtr(B); gi.CurSlot = Yap_CurrentHandle(); gi.a = LCL0-ASP; gi.e = LCL0-ENV; @@ -1773,7 +1794,7 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) { #endif } #endif /* TABLING */ - B = (choiceptr)(LCL0-gi.b0); + B = (choiceptr)(LCL0-gi.b_top); CP = gi.cp; P = gi.p; ASP = LCL0-gi.a; @@ -1810,7 +1831,7 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) { ASP = LCL0-gi.a; /* YENV should be set to the current environment */ YENV = ENV = LCL0-gi.e; - B =(choiceptr)(LCL0-gi.b); + B =(choiceptr)(LCL0-gi.b_top); SET_BB(B); HB = PROTECT_FROZEN_H(B); // should we catch the exception or pass it through? @@ -1885,7 +1906,7 @@ void Yap_trust_last(void) { } } -Term Yap_RunTopGoal(Term t, bool handle_errors) { +Term Yap_RunTopGoal(Term t, YAP_dogoalinfo *gip, bool handle_errors) { CACHE_REGS yamop *CodeAdr; Prop pe; @@ -1894,13 +1915,6 @@ Term Yap_RunTopGoal(Term t, bool handle_errors) { UInt arity; Term tmod = CurrentModule; Term goal_out = 0; - YAP_dogoalinfo gi; - gi.p = P; - gi.cp = CP; - gi.b0 = gi.b = LCL0-CellPtr(B); - gi.CurSlot = Yap_CurrentHandle(); - gi.a = LCL0-ASP; - gi.e = LCL0-ENV; LOCAL_PrologMode |= TopGoalMode; t = Yap_YapStripModule(t, &tmod); @@ -1971,7 +1985,7 @@ Term Yap_RunTopGoal(Term t, bool handle_errors) { "unable to boot because of too little Trail space"); } #endif - goal_out = do_goal(CodeAdr, arity, pt, &gi, handle_errors PASS_REGS); + goal_out = do_goal(CodeAdr, arity, pt, gip, handle_errors PASS_REGS); return goal_out; } @@ -2137,11 +2151,13 @@ bool Yap_Reset(yap_reset_t mode, bool hard) { Yap_ResetException(worker_id); /* first, backtrack to the root */ - while (B) { + while (B->cp_b) { + if (B->cp_ap == NOCODE) + break; + B = B->cp_b; + } P = FAILCODE; Yap_exec_absmi(true, mode); - B = B->cp_b; - } /* reinitialize the engine */ Yap_InitYaamRegs(worker_id, false); GLOBAL_Initialised = true; diff --git a/C/stack.c b/C/stack.c index 62e411d85..ef8bdc5c0 100644 --- a/C/stack.c +++ b/C/stack.c @@ -304,6 +304,8 @@ bool Yap_search_for_static_predicate_in_use(PredEntry *p, return true; } env_ptr = b_ptr->cp_env; + if (b_ptr->cp_ap == NOCODE) + return false; b_ptr = b_ptr->cp_b; } while (b_ptr != NULL); return (FALSE); diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index c0a907381..d8b592cbe 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -254,7 +254,6 @@ YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) { arity_t arity = ArityOfFunctor(f.f); Term o = AbsAppl(HR); *HR++ = (CELL)f.f; - Term *tt = RepAppl(o) + 1; for (arity_t i = 0; i < arity; i++) *HR++ = ts[i].term(); mk(o); @@ -834,7 +833,7 @@ bool YAPQuery::deterministic() { BACKUP_MACHINE_REGS(); if (!q_open || q_state == 0) return false; - choiceptr myB = (choiceptr)(LCL0 - q_h.b); + choiceptr myB = (choiceptr)(LCL0 - q_h.b_top); return (B >= myB); RECOVER_MACHINE_REGS(); } diff --git a/H/Yapproto.h b/H/Yapproto.h index 2e27c7f29..a41236919 100755 --- a/H/Yapproto.h +++ b/H/Yapproto.h @@ -203,7 +203,7 @@ extern void Yap_fail_all(choiceptr bb USES_REGS); extern Term Yap_ExecuteCallMetaCall(Term,Term); extern void Yap_InitExecFs(void); extern bool Yap_JumpToEnv(void); -extern Term Yap_RunTopGoal(Term, bool); +extern Term Yap_RunTopGoal(Term, struct goal_info *, bool); extern bool Yap_execute_goal(Term, int, Term, bool); extern bool Yap_exec_absmi(bool, yap_reset_t); extern void Yap_trust_last(void); diff --git a/H/absmi.h b/H/absmi.h index b05baa93a..30bfafd19 100755 --- a/H/absmi.h +++ b/H/absmi.h @@ -2322,7 +2322,7 @@ static inline void prune(choiceptr cp USES_REGS) { POP_EXECUTE(); } if (B->cp_b == NULL) - break; + return; B = B->cp_b; } if (POP_CHOICE_POINT(B->cp_b)) { diff --git a/include/YapDefs.h b/include/YapDefs.h index b2b684e1e..43ac14687 100755 --- a/include/YapDefs.h +++ b/include/YapDefs.h @@ -138,7 +138,7 @@ typedef enum { /* this should be opaque to the user */ typedef struct goal_info { - unsigned long b, b0, e, a; //> choice-point at entry + unsigned long b_top, b_bottom, m, e, a; //> choice-point at entry YAP_handle_t CurSlot; //> variables at entry YAP_handle_t EndSlot; //> variables at successful execution struct yami *p; //> Program Counter at entry diff --git a/pl/boot.yap b/pl/boot.yap index 89188051e..941e1c8c7 100644 --- a/pl/boot.yap +++ b/pl/boot.yap @@ -267,7 +267,7 @@ initialize_prolog :- :- c_compile( 'protect.yap' ). :- ['absf.yap']. - +%:- stop_low_level_trace. :- use_module('error.yap'). :- [