interfacing

This commit is contained in:
Vitor Santos Costa 2019-04-12 17:32:26 +01:00
parent 8070e65354
commit 9172f2994b
4 changed files with 82 additions and 94 deletions

View File

@ -1,3 +1,4 @@
/************************************************************************* * /************************************************************************* *
* YAP Prolog * * YAP Prolog *
* Yap Prolog was developed at NCCUP - Universidade do Porto * * Yap Prolog was developed at NCCUP - Universidade do Porto *
@ -1783,7 +1784,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
// LOCAL_CurSlot); // LOCAL_CurSlot);
dgi->b_bottom = (LCL0 - (CELL*)B); dgi->b_bottom = (LCL0 - (CELL*)B);
dgi->e = LCL0 - (CELL *)ENV; dgi->e = LCL0 - (CELL *)ENV;
dgi->a = LCL0 - (CELL *)ASP; dgi->y = LCL0 - (CELL *)YENV;
if (out) { if (out) {
dgi->EndSlot = LOCAL_CurSlot; dgi->EndSlot = LOCAL_CurSlot;
Yap_StartSlots(); Yap_StartSlots();
@ -1865,7 +1866,11 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
P = dgi->p; P = dgi->p;
CP = dgi->cp; CP = dgi->cp;
ENV = LCL0-dgi->e; ENV = LCL0-dgi->e;
ASP = LCL0-dgi->a; YENV = LCL0-dgi->y;
/* ASP should be set to the top of the local stack when we
did the call */
SET_ASP(YENV, E_CB * sizeof(CELL));
B = (choiceptr)(LCL0-dgi->b_top) B = (choiceptr)(LCL0-dgi->b_top)
RECOVER_MACHINE_REGS(); 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, 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,
@ -1881,7 +1886,7 @@ X_API Int YAP_RunGoal(Term t) {
gi.cp = CP; gi.cp = CP;
gi.b_top = LCL0-CellPtr(B); gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle(); gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP; gi.y = LCL0-YENV;
gi.e = LCL0-ENV; gi.e = LCL0-ENV;
yhandle_t cslot = LOCAL_CurSlot; yhandle_t cslot = LOCAL_CurSlot;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
@ -1892,6 +1897,7 @@ X_API Int YAP_RunGoal(Term t) {
LOCAL_PrologMode = UserCCallMode; LOCAL_PrologMode = UserCCallMode;
// should we catch the exception or pass it through? // should we catch the exception or pass it through?
// We'll pass it through // We'll pass it through
SET_ASP(YENV, E_CB * sizeof(CELL));
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
LOCAL_CurSlot = cslot; LOCAL_CurSlot = cslot;
return out; return out;
@ -1972,7 +1978,7 @@ X_API Int YAP_RunGoalOnce(Term t) {
gi.cp = CP; gi.cp = CP;
gi.b_top = LCL0-CellPtr(B); gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle(); gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP; gi.y = LCL0-YENV;
gi.e = LCL0-ENV; gi.e = LCL0-ENV;
Int oldPrologMode = LOCAL_PrologMode; Int oldPrologMode = LOCAL_PrologMode;
yhandle_t CSlot; yhandle_t CSlot;
@ -1994,37 +2000,31 @@ X_API Int YAP_RunGoalOnce(Term t) {
// We'll pass it through // We'll pass it through
// Yap_RaiseException(); // Yap_RaiseException();
if (out) { if (out) {
choiceptr cut_pt, ob; choiceptr cut_pt;
ob = NULL;
cut_pt = B; cut_pt = B;
while (cut_pt->cp_ap != NOCODE) { B = (choiceptr)(LCL0-gi.b_top);
while (cut_pt->cp_ap != NOCODE && cut_pt < B) {
/* make sure we prune C-choicepoints */ /* make sure we prune C-choicepoints */
if (POP_CHOICE_POINT(cut_pt->cp_b)) {
POP_EXECUTE();
}
ob = cut_pt;
cut_pt = cut_pt->cp_b; cut_pt = cut_pt->cp_b;
} }
B = cut_pt;
Yap_TrimTrail();
} else {
Yap_CloseSlots(CSlot);
}
#ifdef YAPOR #ifdef YAPOR
CUT_prune_to(cut_pt); CUT_prune_to(cut_pt);
#endif #endif
if (ob) {
B = ob;
Yap_TrimTrail();
}
B = cut_pt;
} else {
Yap_CloseSlots(CSlot);
}
ASP = LCL0-gi.a;
ENV = LCL0-gi.e; ENV = LCL0-gi.e;
YENV = LCL0-gi.y;
B = (choiceptr)(LCL0-gi.b_top); B = (choiceptr)(LCL0-gi.b_top);
#ifdef DEPTH_LIMIT #ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH]; DEPTH = ENV[E_DEPTH];
#endif #endif
P = gi.p; P = gi.p;
CP = gi.cp; CP = gi.cp;
SET_ASP(YENV, E_CB * sizeof(CELL));
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;

View File

@ -801,14 +801,13 @@ static Int execute_in_mod(USES_REGS1) { /* '$execute'(Goal) */
* *
* @method prune_inner_computation * @method prune_inner_computation
*/ */
static void prune_inner_computation(choiceptr parent) { static void prune_inner_computation(choiceptr parent, YAP_dogoalinfo *gi) {
/* code */ /* code */
choiceptr cut_pt; choiceptr cut_pt;
yamop *oP = P, *oCP = CP;
Int oENV = LCL0 - ENV;
cut_pt = B; cut_pt = B;
while (cut_pt && cut_pt->cp_b < parent && cut_pt->cp_b->cp_ap != NOCODE) { while (cut_pt && cut_pt->cp_b
&& cut_pt->cp_ap != NOCODE &&
cut_pt->cp_b <= parent) {
cut_pt = cut_pt->cp_b; cut_pt = cut_pt->cp_b;
} }
if (!cut_pt) if (!cut_pt)
@ -819,9 +818,9 @@ static void prune_inner_computation(choiceptr parent) {
B = cut_pt; B = cut_pt;
Yap_TrimTrail(); Yap_TrimTrail();
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
P = oP; P = gi->p;
CP = oCP; CP = gi->cp;
ENV = LCL0 - oENV; ENV = LCL0-gi->e;
B = parent; B = parent;
} }
@ -830,7 +829,7 @@ static void prune_inner_computation(choiceptr parent) {
* after completing a computation. * after completing a computation.
* @method complete_inner_computation * @method complete_inner_computation
*/ */
static void complete_inner_computation(choiceptr old_B) { static void complete_inner_computation(choiceptr old_B, YAP_dogoalinfo *gi) {
choiceptr myB = B; choiceptr myB = B;
if (myB == NULL) { if (myB == NULL) {
return; return;
@ -848,8 +847,11 @@ static void complete_inner_computation(choiceptr old_B) {
return; return;
} }
// restore environment at call... // restore environment at call...
CP = myB->cp_cp; CP = gi->cp;
ENV = myB->cp_env; P = gi->p;
ENV = LCL0-gi->e;
YENV = LCL0-gi->y;
} }
static Int Yap_ignore(Term t, bool fail USES_REGS) { static Int Yap_ignore(Term t, bool fail USES_REGS) {
@ -858,7 +860,7 @@ static Int Yap_ignore(Term t, bool fail USES_REGS) {
gi.cp = CP; gi.cp = CP;
gi.b_top = LCL0-CellPtr(B); gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle(); gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP; gi.y = LCL0-YENV;
gi.e = LCL0-ENV; gi.e = LCL0-ENV;
// gi. = LCL0-YENV; // gi. = LCL0-YENV;
yap_error_descriptor_t *ctx = malloc(sizeof(yap_error_descriptor_t)); yap_error_descriptor_t *ctx = malloc(sizeof(yap_error_descriptor_t));
@ -866,15 +868,15 @@ static Int Yap_ignore(Term t, bool fail USES_REGS) {
bool rc = Yap_RunTopGoal(t, &gi, false); bool rc = Yap_RunTopGoal(t, &gi, false);
choiceptr B0 = (choiceptr)(LCL0-gi.b_top); choiceptr B0 = (choiceptr)(LCL0-gi.b_top);
if (!rc) { if (!rc) {
complete_inner_computation(B0); complete_inner_computation(B0, &gi);
// We'll pass it through // We'll pass it through
} else { } else {
prune_inner_computation(B0); prune_inner_computation(B0, &gi);
} }
Yap_popErrorContext(newxp, true); Yap_popErrorContext(newxp, true);
ASP = LCL0-gi.a;
ENV = LCL0-gi.e; ENV = LCL0-gi.e;
B = (choiceptr)(LCL0-gi.b_top); B = (choiceptr)(LCL0-gi.b_top);
SET_ASP(ENV, E_CB * sizeof(CELL));
#ifdef DEPTH_LIMIT #ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH]; DEPTH = ASP[E_DEPTH];
#endif #endif
@ -1015,7 +1017,7 @@ static Int setup_call_catcher_cleanup(USES_REGS1) {
gi.cp = CP; gi.cp = CP;
gi.b_top = LCL0-CellPtr(B); gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle(); gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP; gi.y = LCL0-YENV;
gi.e = LCL0-ENV; gi.e = LCL0-ENV;
bool rc; bool rc;
Yap_DisableInterrupts(worker_id); Yap_DisableInterrupts(worker_id);
@ -1027,18 +1029,19 @@ static Int setup_call_catcher_cleanup(USES_REGS1) {
} }
choiceptr B0=(choiceptr)(LCL0-gi.b_top); choiceptr B0=(choiceptr)(LCL0-gi.b_top);
if (!rc) { if (!rc) {
complete_inner_computation(B0); complete_inner_computation(B0, &gi);
// We'll pass it throughs // We'll pass it throughs
return false; return false;
} else { } else {
prune_inner_computation(B0); prune_inner_computation(B0, &gi);
} }
ASP = LCL0-gi.a; YENV = LCL0-gi.y;
ENV = LCL0-gi.e; ENV = LCL0-gi.e;
B = (choiceptr)(LCL0-gi.b_top); B = (choiceptr)(LCL0-gi.b_top);
SET_ASP(YENV, E_CB * sizeof(CELL));
#ifdef DEPTH_LIMIT #ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH]; DEPTH = ENV[E_DEPTH];
#endif #endif
P = gi.p; P = gi.p;
CP = gi.cp; CP = gi.cp;
@ -1766,7 +1769,7 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) {
gi.cp = CP; gi.cp = CP;
gi.b_top = LCL0-CellPtr(B); gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle(); gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP; gi.y = LCL0-YENV;
gi.e = LCL0-ENV; gi.e = LCL0-ENV;
LOCAL_PrologMode |= TopGoalMode; LOCAL_PrologMode |= TopGoalMode;
@ -1797,7 +1800,6 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) {
B = (choiceptr)(LCL0-gi.b_top); B = (choiceptr)(LCL0-gi.b_top);
CP = gi.cp; CP = gi.cp;
P = gi.p; P = gi.p;
ASP = LCL0-gi.a;
#ifdef DEPTH_LIMIT #ifdef DEPTH_LIMIT
DEPTH = ENV[E_DEPTH]; DEPTH = ENV[E_DEPTH];
#endif #endif
@ -1826,11 +1828,11 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) {
#ifdef DEPTH_LIMIT #ifdef DEPTH_LIMIT
DEPTH = B->cp_depth; DEPTH = B->cp_depth;
#endif #endif
/* ASP should be set to the top of the local stack when we
did the call */
ASP = LCL0-gi.a;
/* YENV should be set to the current environment */ /* YENV should be set to the current environment */
YENV = ENV = LCL0-gi.e; YENV = ENV = LCL0-gi.e;
/* ASP should be set to the top of the local stack when we
did the call */
SET_ASP(YENV, E_CB * sizeof(CELL));
B =(choiceptr)(LCL0-gi.b_top); B =(choiceptr)(LCL0-gi.b_top);
SET_BB(B); SET_BB(B);
HB = PROTECT_FROZEN_H(B); HB = PROTECT_FROZEN_H(B);

View File

@ -613,6 +613,7 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
CACHE_REGS CACHE_REGS
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
bool rc = YAP_RunGoalOnce(t); bool rc = YAP_RunGoalOnce(t);
RECOVER_MACHINE_REGS();
return rc; return rc;
} }
/** /**
@ -630,65 +631,50 @@ Term YAPEngine::fun(Term t) {
CACHE_REGS CACHE_REGS
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term tmod = Yap_CurrentModule(), *ts = nullptr; Term tmod = Yap_CurrentModule(), *ts = nullptr;
PredEntry *ap;
arity_t arity; arity_t arity;
Functor f; Functor f;
Atom name; Atom name;
YAP_dogoalinfo backup = q;
yhandle_t yt = Yap_NewHandles(1);
if (IsApplTerm(t)) { if (IsApplTerm(t)) {
ts = RepAppl(t) + 1; ts = RepAppl(t) + 1;
f = (Functor)ts[-1]; f = (Functor)ts[-1];
name = NameOfFunctor(f); name = NameOfFunctor(f);
arity = ArityOfFunctor(f); arity = ArityOfFunctor(f);
for (arity_t i = 0; i < arity; i++) t = AbsAppl(HR);
XREGS[i + 1] = ts[i]; HR[0] = (CELL)Yap_MkFunctor(name, arity+1);
for (arity_t i = 0; i < arity; i++) {
HR[i + 1] = ts[i];
}
HR[arity] = Yap_GetFromHandle(yt);
HR += (arity+2);
} else if (IsAtomTerm(t)) { } else if (IsAtomTerm(t)) {
name = AtomOfTerm(t); name = AtomOfTerm(t);
f = nullptr; t = AbsAppl(HR);
arity = 0; HR[0] = (CELL)Yap_MkFunctor(name, 1);
HR[1] = Yap_GetFromHandle(yt);
HR += 2;
} else if (IsPairTerm(t)) { } else if (IsPairTerm(t)) {
XREGS[1] = ts[0]; HR[0] = (CELL)Yap_MkFunctor(AtomDot, 3);
XREGS[2] = ts[1]; HR[1] = ts[0];
arity = 2; HR[2] = ts[1];
name = AtomDot; HR[3] = Yap_GetFromHandle(yt);
f = FunctorDot;
} else { } else {
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0); throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
return 0L; return 0L;
} }
Term ot = XREGS[arity + 1] = MkVarTerm(); CACHE_REGS
yhandle_t h = Yap_InitHandle(ot); BACKUP_MACHINE_REGS();
arity++; bool rc = YAP_RunGoalOnce(t);
HR += arity; Term ot;
f = Yap_MkFunctor(name, arity); if (rc)
ap = (PredEntry *)(PredPropByFunc(f, tmod)); ot = Yap_GetFromHandle(yt);
if (ap == nullptr || ap->OpcodeOfPred == UNDEF_OPCODE) {
Term g = (Yap_MkApplTerm(f, arity, ts));
ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred));
}
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
// 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);
if (result)
ot = Yap_GetFromHandle(h);
else else
ot = TermNone; ot = TermNone;
YAPCatchError();
{
YAP_LeaveGoal(result, &q);
// PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
q = backup;
return ot; return ot;
} }
}
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[]) YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
: YAPPredicate(f, mod) { : YAPPredicate(f, mod) {

View File

@ -138,7 +138,7 @@ typedef enum {
/* this should be opaque to the user */ /* this should be opaque to the user */
typedef struct goal_info { typedef struct goal_info {
unsigned long b_top, b_bottom, m, e, a; //> choice-point at entry unsigned long b_top, b_bottom, m, e, y; //> choice-point at entry
YAP_handle_t CurSlot; //> variables at entry YAP_handle_t CurSlot; //> variables at entry
YAP_handle_t EndSlot; //> variables at successful execution YAP_handle_t EndSlot; //> variables at successful execution
struct yami *p; //> Program Counter at entry struct yami *p; //> Program Counter at entry