From bde725e0160f0cf5f2d2a2a74d82a436b4adfeaf Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Mon, 17 Sep 2018 13:39:41 +0100 Subject: [PATCH] fixes --- C/c_interface.c | 111 +++++++++++++++++++------------------- CXX/yapi.cpp | 37 +++++++------ CXX/yapq.hh | 2 +- packages/python/pypreds.c | 37 +++---------- pl/boot.yap | 2 +- pl/bootlists.yap | 24 ++++----- 6 files changed, 99 insertions(+), 114 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index 1a30a292a..dfa56f02c 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1801,67 +1801,16 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { return out; } -X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { - CACHE_REGS - choiceptr myB, handler; - - // fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d P=%p CP=%p Slots=%d\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); - BACKUP_MACHINE_REGS(); - myB = (choiceptr)(LCL0 - dgi->b0); - handler = B; - while (handler - //&& LOCAL_CBorder > LCL0 - (CELL *)handler - //&& handler->cp_ap != NOCODE - && handler->cp_b != NULL - && handler != myB - ) { - handler->cp_ap = TRUSTFAILCODE; - handler = handler->cp_b; - } - if (LOCAL_PrologMode & AsyncIntMode) { - Yap_signal(YAP_FAIL_SIGNAL); - } - B = handler; - if (successful) { - Yap_TrimTrail(); - CP = dgi->cp; - P = dgi->p; - } else { - Yap_exec_absmi(true, YAP_EXEC_ABSMI); - LOCAL_CurSlot = dgi->CurSlot; - ENV = YENV = B->cp_env; - HR = B->cp_h; - TR = B->cp_tr; - // use the current choicepoint - // B=B->cp_b; - ASP=(CELL*)B; - } - RECOVER_MACHINE_REGS(); - // 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; -} - -X_API Int YAP_RunGoal(Term t) { - CACHE_REGS - Term out; - yamop *old_CP = CP; - yhandle_t cslot = LOCAL_CurSlot; - BACKUP_MACHINE_REGS(); - - LOCAL_AllowRestart = FALSE; - LOCAL_PrologMode = UserMode; - out = Yap_RunTopGoal(t, true); - LOCAL_PrologMode = UserCCallMode; - // should we catch the exception or pass it through? - // We'll pass it through - Yap_RaiseException(); - if (out) { - P = (yamop *)ENV[E_CP]; +static void completeInnerCall( bool on_cut, yamop *old_CP, yamop *old_P) +{ + if (on_cut) { + P = old_P; ENV = (CELL *)ENV[E_E]; CP = old_CP; LOCAL_AllowRestart = TRUE; // we are back to user code again, need slots */ } else { + P = old_P; ENV = B->cp_env; ENV = (CELL *)ENV[E_E]; CP = old_CP; @@ -1872,7 +1821,57 @@ X_API Int YAP_RunGoal(Term t) { SET_ASP(ENV, E_CB * sizeof(CELL)); // make sure the slots are ok. } + +} + +X_API bool YAP_LeaveGoal(bool on_cut, YAP_dogoalinfo *dgi) { + CACHE_REGS + choiceptr myB, handler; + + // fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d P=%p CP=%p Slots=%d\n", 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); + if (LOCAL_PrologMode & AsyncIntMode) { + Yap_signal(YAP_FAIL_SIGNAL); + } + handler = B; + while (handler + //&& LOCAL_CBorder > LCL0 - (CELL *)handler + //&& handler->cp_ap != NOCODE + && handler <= myB + ) { + if (handler < myB) + handler->cp_ap = TRUSTFAILCODE; + B = handler; + handler = handler->cp_b; + RECOVER_MACHINE_REGS(); + if (on_cut) { + Yap_TrimTrail(); + } else if (!(LOCAL_PrologMode & AsyncIntMode)) { + P=FAILCODE; + Yap_exec_absmi(true, YAP_EXEC_ABSMI); + } + BACKUP_MACHINE_REGS(); + } + completeInnerCall( on_cut, dgi->cp, dgi->p); RECOVER_MACHINE_REGS(); + // 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; +} + +X_API Int YAP_RunGoal(Term t) { + CACHE_REGS + Term out; + yhandle_t cslot = LOCAL_CurSlot; + BACKUP_MACHINE_REGS(); + + LOCAL_AllowRestart = FALSE; + LOCAL_PrologMode = UserMode; + out = Yap_RunTopGoal(t, true); + LOCAL_PrologMode = UserCCallMode; + // should we catch the exception or pass it through? + // We'll pass it through + RECOVER_MACHINE_REGS(); LOCAL_CurSlot = cslot; return out; } diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index cc2587370..66602b0c4 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -640,39 +640,45 @@ void YAPEngine::release() { Term YAPEngine::fun(Term t) { CACHE_REGS BACKUP_MACHINE_REGS(); - Term tmod = Yap_CurrentModule(), *ts = nullptr; + Term tmod = Yap_CurrentModule(), *ts = NULL; PredEntry *ap; arity_t arity; Functor f; Atom name; + RESET_VARIABLE(HR); if (IsApplTerm(t)) { - ts = RepAppl(t) + 1; - f = (Functor)ts[-1]; + CELL *p = RepAppl(t); + f = (Functor)*p; name = NameOfFunctor(f); arity = ArityOfFunctor(f); + p++; for (arity_t i = 0; i < arity; i++) - XREGS[i + 1] = ts[i]; + XREGS[i+1] = p[i]; + arity += 1; + XREGS[arity] = (CELL)HR; + HR+= 1; } else if (IsAtomTerm(t)) { name = AtomOfTerm(t); - f = nullptr; - arity = 0; + XREGS[1] = (CELL)HR; + HR+=1; + arity = 1; } else if (IsPairTerm(t)) { XREGS[1] = ts[0]; XREGS[2] = ts[1]; - arity = 2; + arity = 3; name = AtomDot; - f = FunctorDot; + arity++; + HR += arity; } else { throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0); return 0L; } - XREGS[arity + 1] = MkVarTerm(); - arity++; f = Yap_MkFunctor(name, arity); ap = (PredEntry *)(PredPropByFunc(f, tmod)); + yhandle_t k = Yap_InitHandle(HR[-1]); if (ap == nullptr || ap->OpcodeOfPred == UNDEF_OPCODE) { - Term g = (Yap_MkApplTerm(f, arity, ts)); + Term g = Yap_MkApplTerm(f, arity, ts); ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred)); } q.CurSlot = Yap_StartSlots(); @@ -681,15 +687,16 @@ Term YAPEngine::fun(Term t) { // make sure this is safe // allow Prolog style exception handling //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec "); - bool result = (bool)YAP_EnterGoal(ap, nullptr, &q); YAPCatchError(); + Term out = 0; + if (result) { + out = Yap_GetFromHandle(k); + } { YAP_LeaveGoal(result, &q); - // PyEval_RestoreThread(_save); - RECOVER_MACHINE_REGS(); - return result; } + return out; } YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[]) diff --git a/CXX/yapq.hh b/CXX/yapq.hh index 6fcaba6bc..7fcf764c0 100644 --- a/CXX/yapq.hh +++ b/CXX/yapq.hh @@ -367,7 +367,7 @@ public: //> output. YAPTerm funCall(YAPTerm t) { return YAPTerm(fun(t.term())); }; Term fun(Term t); - Term fun(YAPTerm t) { return fun(t.term()); }; + //Term fun(YAPTerm t) { return fun(t.term()); }; //> set a StringFlag, usually a path //> bool setStringFlag(std::string arg, std::string path) { diff --git a/packages/python/pypreds.c b/packages/python/pypreds.c index 3f3179588..9dc6d4cbf 100644 --- a/packages/python/pypreds.c +++ b/packages/python/pypreds.c @@ -8,7 +8,6 @@ PyObject *py_Main; void pyErrorHandler__(int line, const char *file, const char *code) { // this code is called if a Python error is found. // int lvl = push_text_stack(); - PyObject *type; // PyErr_Fetch(&type, &val, NULL); // PyErr_Print(); // Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python @@ -235,18 +234,19 @@ static foreign_t assign_python(term_t exp, term_t name) { static foreign_t python_string_to(term_t f) { if (PL_is_atom(f)) { char *s = NULL; - if (!PL_get_chars(f, &s, CVT_ALL | CVT_EXCEPTION | REP_UTF8)) { + if (!PL_get_chars(f, &s, CVT_ATOM |CVT_STRING | CVT_EXCEPTION | REP_UTF8)) { pyErrorAndReturn(false); } - if (!strcmp(s,"atom")) + if (!strcmp(s,"atom")) { pyStringToString = false; - if (!strcmp(s,"string")) + return true; + } + if (!strcmp(s,"string")) { pyStringToString = true; - else - return false; - return true; + return true; + } } - return true; + return false; } static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) { @@ -570,27 +570,6 @@ static foreign_t python_export(term_t t, term_t pl) { pyErrorAndReturn(rc); } -static bool get_mod(const char *s0) -{ - PyObject *pName; - term_t t0 = python_acquire_GIL(); -#if PY_MAJOR_VERSION < 3 - pName = PyString_FromString(s0); -#else - pName = PyUnicode_FromString(s0); -#endif - if (pName == NULL) { - python_release_GIL(t0); - } - - PyObject *pModule = PyImport_Import(pName); - - Py_XDECREF(pName); - python_release_GIL(t0); - - return pModule; -} - /** * @pred python_import(MName, Mod) * Import a python module to the YAP environment. diff --git a/pl/boot.yap b/pl/boot.yap index 9d9ac605d..cb5591395 100644 --- a/pl/boot.yap +++ b/pl/boot.yap @@ -479,7 +479,7 @@ If this hook preodicate succeeds it must instantiate the _Action_ argument to t :- yap_flag(user:unknown,error). -:- ensure_loaded('../android.yap'). +%:- ensure_loaded('../android.yap'). %% @} diff --git a/pl/bootlists.yap b/pl/bootlists.yap index c944d9fe6..471fbad9d 100644 --- a/pl/bootlists.yap +++ b/pl/bootlists.yap @@ -91,6 +91,18 @@ lists:delete([Head|List], Elem, Residue) :- lists:delete([Head|List], Elem, [Head|Residue]) :- lists:delete(List, Elem, Residue). + +% reverse(List, Reversed) +% is true when List and Reversed are lists with the same elements +% but in opposite orders. rev/2 is a synonym for reverse/2. + +lists:reverse(List, Reversed) :- + lists:reverse(List, [], Reversed). + +lists:reverse([], Reversed, Reversed). +lists:reverse([Head|Tail], Sofar, Reversed) :- + lists:reverse(Tail, [Head|Sofar], Reversed). + :- set_prolog_flag(source, false). % disable source. @@ -135,15 +147,3 @@ prolog:length(L, M) :- M is N + 1, NL = [_|L], '$$_length2'(L, O, M) ). %% @} - -% reverse(List, Reversed) -% is true when List and Reversed are lists with the same elements -% but in opposite orders. rev/2 is a synonym for reverse/2. - -lists:reverse(List, Reversed) :- - lists:reverse(List, [], Reversed). - -lists:reverse([], Reversed, Reversed). -lists:reversae([Head|Tail], Sofar, Reversed) :- - lists:reverse(Tail, [Head|Sofar], Reversed). -