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 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);
dgi->b_bottom = (LCL0 - (CELL*)B);
dgi->e = LCL0 - (CELL *)ENV;
dgi->a = LCL0 - (CELL *)ASP;
dgi->y = LCL0 - (CELL *)YENV;
if (out) {
dgi->EndSlot = LOCAL_CurSlot;
Yap_StartSlots();
@ -1865,7 +1866,11 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
P = dgi->p;
CP = dgi->cp;
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)
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,
@ -1881,7 +1886,7 @@ X_API Int YAP_RunGoal(Term t) {
gi.cp = CP;
gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP;
gi.y = LCL0-YENV;
gi.e = LCL0-ENV;
yhandle_t cslot = LOCAL_CurSlot;
BACKUP_MACHINE_REGS();
@ -1892,6 +1897,7 @@ X_API Int YAP_RunGoal(Term t) {
LOCAL_PrologMode = UserCCallMode;
// should we catch the exception or pass it through?
// We'll pass it through
SET_ASP(YENV, E_CB * sizeof(CELL));
RECOVER_MACHINE_REGS();
LOCAL_CurSlot = cslot;
return out;
@ -1972,7 +1978,7 @@ X_API Int YAP_RunGoalOnce(Term t) {
gi.cp = CP;
gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP;
gi.y = LCL0-YENV;
gi.e = LCL0-ENV;
Int oldPrologMode = LOCAL_PrologMode;
yhandle_t CSlot;
@ -1994,37 +2000,31 @@ X_API Int YAP_RunGoalOnce(Term t) {
// We'll pass it through
// Yap_RaiseException();
if (out) {
choiceptr cut_pt, ob;
choiceptr cut_pt;
ob = NULL;
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 */
if (POP_CHOICE_POINT(cut_pt->cp_b)) {
POP_EXECUTE();
}
ob = cut_pt;
cut_pt = cut_pt->cp_b;
}
#ifdef YAPOR
CUT_prune_to(cut_pt);
#endif
if (ob) {
B = ob;
Yap_TrimTrail();
cut_pt = cut_pt->cp_b;
}
B = cut_pt;
Yap_TrimTrail();
} else {
Yap_CloseSlots(CSlot);
}
ASP = LCL0-gi.a;
#ifdef YAPOR
CUT_prune_to(cut_pt);
#endif
ENV = LCL0-gi.e;
YENV = LCL0-gi.y;
B = (choiceptr)(LCL0-gi.b_top);
#ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH];
DEPTH = ENV[E_DEPTH];
#endif
P = gi.p;
CP = gi.cp;
SET_ASP(YENV, E_CB * sizeof(CELL));
LOCAL_AllowRestart = FALSE;
RECOVER_MACHINE_REGS();
return out;

View File

@ -801,14 +801,13 @@ static Int execute_in_mod(USES_REGS1) { /* '$execute'(Goal) */
*
* @method prune_inner_computation
*/
static void prune_inner_computation(choiceptr parent) {
static void prune_inner_computation(choiceptr parent, YAP_dogoalinfo *gi) {
/* code */
choiceptr cut_pt;
yamop *oP = P, *oCP = CP;
Int oENV = LCL0 - ENV;
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;
}
if (!cut_pt)
@ -819,9 +818,9 @@ static void prune_inner_computation(choiceptr parent) {
B = cut_pt;
Yap_TrimTrail();
LOCAL_AllowRestart = FALSE;
P = oP;
CP = oCP;
ENV = LCL0 - oENV;
P = gi->p;
CP = gi->cp;
ENV = LCL0-gi->e;
B = parent;
}
@ -830,7 +829,7 @@ static void prune_inner_computation(choiceptr parent) {
* after completing a 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;
if (myB == NULL) {
return;
@ -848,8 +847,11 @@ static void complete_inner_computation(choiceptr old_B) {
return;
}
// restore environment at call...
CP = myB->cp_cp;
ENV = myB->cp_env;
CP = gi->cp;
P = gi->p;
ENV = LCL0-gi->e;
YENV = LCL0-gi->y;
}
static Int Yap_ignore(Term t, bool fail USES_REGS) {
@ -858,23 +860,23 @@ static Int Yap_ignore(Term t, bool fail USES_REGS) {
gi.cp = CP;
gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP;
gi.y = LCL0-YENV;
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, &gi, false);
bool rc = Yap_RunTopGoal(t, &gi, false);
choiceptr B0 = (choiceptr)(LCL0-gi.b_top);
if (!rc) {
complete_inner_computation(B0);
complete_inner_computation(B0, &gi);
// We'll pass it through
} else {
prune_inner_computation(B0);
prune_inner_computation(B0, &gi);
}
Yap_popErrorContext(newxp, true);
ASP = LCL0-gi.a;
ENV = LCL0-gi.e;
B = (choiceptr)(LCL0-gi.b_top);
SET_ASP(ENV, E_CB * sizeof(CELL));
#ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH];
#endif
@ -1015,7 +1017,7 @@ static Int setup_call_catcher_cleanup(USES_REGS1) {
gi.cp = CP;
gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP;
gi.y = LCL0-YENV;
gi.e = LCL0-ENV;
bool rc;
Yap_DisableInterrupts(worker_id);
@ -1027,22 +1029,23 @@ static Int setup_call_catcher_cleanup(USES_REGS1) {
}
choiceptr B0=(choiceptr)(LCL0-gi.b_top);
if (!rc) {
complete_inner_computation(B0);
complete_inner_computation(B0, &gi);
// We'll pass it throughs
return false;
} else {
prune_inner_computation(B0);
prune_inner_computation(B0, &gi);
}
ASP = LCL0-gi.a;
ENV = LCL0-gi.e;
B = (choiceptr)(LCL0-gi.b_top);
YENV = LCL0-gi.y;
ENV = LCL0-gi.e;
B = (choiceptr)(LCL0-gi.b_top);
SET_ASP(YENV, E_CB * sizeof(CELL));
#ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH];
DEPTH = ENV[E_DEPTH];
#endif
P = gi.p;
CP = gi.cp;
return rc;
P = gi.p;
CP = gi.cp;
return rc;
}
static Int tag_cleanup(USES_REGS1) {
@ -1766,7 +1769,7 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) {
gi.cp = CP;
gi.b_top = LCL0-CellPtr(B);
gi.CurSlot = Yap_CurrentHandle();
gi.a = LCL0-ASP;
gi.y = LCL0-YENV;
gi.e = LCL0-ENV;
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);
CP = gi.cp;
P = gi.p;
ASP = LCL0-gi.a;
#ifdef DEPTH_LIMIT
DEPTH = ENV[E_DEPTH];
#endif
@ -1826,11 +1828,11 @@ bool Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS) {
#ifdef DEPTH_LIMIT
DEPTH = B->cp_depth;
#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 = 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);
SET_BB(B);
HB = PROTECT_FROZEN_H(B);

View File

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

View File

@ -138,7 +138,7 @@ typedef enum {
/* this should be opaque to the user */
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 EndSlot; //> variables at successful execution
struct yami *p; //> Program Counter at entry