bag of changes

- spacing
- exception handling should be seen carefully.
This commit is contained in:
Vítor Santos Costa 2015-06-19 00:29:14 +01:00
parent 524a9ec738
commit b13f742f02

154
C/exec.c
View File

@ -1,19 +1,19 @@
/************************************************************************* /*************************************************************************
* * * *
* YAP Prolog * * YAP Prolog *
* * * *
* Yap Prolog was developed at NCCUP - Universidade do Porto * * Yap Prolog was developed at NCCUP - Universidade do Porto *
* * * *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* * * *
************************************************************************** **************************************************************************
* * * *
* File: exec.c * * File: exec.c *
* Last rev: 8/2/88 * * Last rev: 8/2/88 *
* mods: * * mods: *
* comments: Execute Prolog code * * comments: Execute Prolog code *
* * * *
*************************************************************************/ *************************************************************************/
#ifdef SCCS #ifdef SCCS
static char SccsId[] = "@(#)cdmgr.c 1.1 05/02/98"; static char SccsId[] = "@(#)cdmgr.c 1.1 05/02/98";
#endif #endif
@ -131,7 +131,7 @@ Yap_PredicateIndicator(Term t, Term mod)
static Int static Int
CallError(yap_error_number err, Term t, Term mod USES_REGS) CallError(yap_error_number err, Term t, Term mod USES_REGS)
{ {
if (yap_flags[LANGUAGE_MODE_FLAG] == 1) { if (isoLanguageFlag()) {
return(CallMetaCall(t, mod PASS_REGS)); return(CallMetaCall(t, mod PASS_REGS));
} else { } else {
if (err == TYPE_ERROR_CALLABLE) { if (err == TYPE_ERROR_CALLABLE) {
@ -198,7 +198,7 @@ do_execute(Term t, Term mod USES_REGS)
!(LOCAL_PrologMode & (AbortMode|InterruptMode|SystemMode))) { !(LOCAL_PrologMode & (AbortMode|InterruptMode|SystemMode))) {
return EnterCreepMode(t, mod PASS_REGS); return EnterCreepMode(t, mod PASS_REGS);
} }
restart_exec: restart_exec:
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
return CallError(INSTANTIATION_ERROR, t0, mod PASS_REGS); return CallError(INSTANTIATION_ERROR, t0, mod PASS_REGS);
} else if (IsApplTerm(t)) { } else if (IsApplTerm(t)) {
@ -245,7 +245,7 @@ do_execute(Term t, Term mod USES_REGS)
#if YAPOR_SBA #if YAPOR_SBA
Term d0 = *pt++; Term d0 = *pt++;
if (d0 == 0) if (d0 == 0)
` XREGS[i] = (CELL)(pt-1); ` XREGS[i] = (CELL)(pt-1);
else else
XREGS[i] = d0; XREGS[i] = d0;
#else #else
@ -321,7 +321,7 @@ do_execute_n(Term t, Term mod, unsigned int n USES_REGS)
int j = -n; int j = -n;
Term t0 = t; Term t0 = t;
restart_exec: restart_exec:
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
return CallError(INSTANTIATION_ERROR, t0, mod PASS_REGS); return CallError(INSTANTIATION_ERROR, t0, mod PASS_REGS);
} else if (IsAtomTerm(t)) { } else if (IsAtomTerm(t)) {
@ -608,7 +608,7 @@ p_execute_clause( USES_REGS1 )
yamop *code; yamop *code;
Term clt = Deref(ARG3); Term clt = Deref(ARG3);
restart_exec: restart_exec:
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,ARG3,"call/1"); Yap_Error(INSTANTIATION_ERROR,ARG3,"call/1");
return FALSE; return FALSE;
@ -686,7 +686,7 @@ p_do_goal_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, cmod) ) ) && if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, cmod) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) { Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE; out = TRUE;
ARG3 = ARG2; ARG3 = ARG2;
goto complete; goto complete;
@ -695,7 +695,7 @@ p_do_goal_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, SYSTEM_MODULE ) ) ) && if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, SYSTEM_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) { Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE; out = TRUE;
ARG3 = ARG2; ARG3 = ARG2;
goto complete; goto complete;
@ -706,7 +706,7 @@ p_do_goal_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion, USER_MODULE ) ) ) && if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion, USER_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) { Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE; out = TRUE;
goto complete; goto complete;
} }
@ -716,11 +716,11 @@ p_do_goal_expansion( USES_REGS1 )
(pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, USER_MODULE ) ) ) && (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, USER_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) { Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
ARG3 = ARG2; ARG3 = ARG2;
out = TRUE; out = TRUE;
} }
complete: complete:
if (creeping) { if (creeping) {
Yap_signal( YAP_CREEP_SIGNAL ); Yap_signal( YAP_CREEP_SIGNAL );
} }
@ -739,7 +739,7 @@ p_do_term_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, cmod) ) ) && if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, cmod) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) { Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE; out = TRUE;
goto complete; goto complete;
} }
@ -747,7 +747,7 @@ p_do_term_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, SYSTEM_MODULE ) ) ) && if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, SYSTEM_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) { Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE; out = TRUE;
goto complete; goto complete;
} }
@ -756,10 +756,10 @@ p_do_term_expansion( USES_REGS1 )
(pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, USER_MODULE ) ) ) && (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, USER_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) { Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE; out = TRUE;
} }
complete: complete:
if (creeping) { if (creeping) {
Yap_signal(YAP_CREEP_SIGNAL); Yap_signal(YAP_CREEP_SIGNAL);
} }
@ -778,7 +778,7 @@ p_execute0( USES_REGS1 )
!LOCAL_InterruptsDisabled) { !LOCAL_InterruptsDisabled) {
return EnterCreepMode(t, mod PASS_REGS); return EnterCreepMode(t, mod PASS_REGS);
} }
restart_exec: restart_exec:
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,ARG3,"call/1"); Yap_Error(INSTANTIATION_ERROR,ARG3,"call/1");
return FALSE; return FALSE;
@ -844,7 +844,7 @@ p_execute_nonstop( USES_REGS1 )
unsigned int arity; unsigned int arity;
Prop pe; Prop pe;
restart_exec: restart_exec:
if (IsVarTerm(mod)) { if (IsVarTerm(mod)) {
mod = CurrentModule; mod = CurrentModule;
} else if (!IsAtomTerm(mod)) { } else if (!IsAtomTerm(mod)) {
@ -1251,7 +1251,7 @@ Yap_fail_all( choiceptr bb USES_REGS )
} }
int int
Yap_execute_pred(PredEntry *ppe, CELL *pt USES_REGS) Yap_execute_pred(PredEntry *ppe, CELL *pt, bool pass_ex USES_REGS)
{ {
yamop *saved_p, *saved_cp; yamop *saved_p, *saved_cp;
yamop *CodeAdr; yamop *CodeAdr;
@ -1305,6 +1305,14 @@ Yap_execute_pred(PredEntry *ppe, CELL *pt USES_REGS)
if (B != NULL) if (B != NULL)
HB = B->cp_h; HB = B->cp_h;
YENV = ENV; YENV = ENV;
// should we catch the exception or pass it through?
// We'll pass it through
if (EX && pass_ex) {
Term ball = Yap_PopTermFromDB( EX );
EX = NULL;
Yap_JumpToEnv( ball );
return FALSE;
}
return TRUE; return TRUE;
} else if (out == 0) { } else if (out == 0) {
P = saved_p; P = saved_p;
@ -1321,6 +1329,14 @@ Yap_execute_pred(PredEntry *ppe, CELL *pt USES_REGS)
B = B->cp_b; B = B->cp_b;
SET_BB(B); SET_BB(B);
HB = PROTECT_FROZEN_H(B); HB = PROTECT_FROZEN_H(B);
// should we catch the exception or pass it through?
// We'll pass it through
if (EX && pass_ex) {
Term ball = Yap_PopTermFromDB( EX );
EX = NULL;
Yap_JumpToEnv( ball );
return FALSE;
}
return(FALSE); return(FALSE);
} else { } else {
Yap_Error(SYSTEM_ERROR,TermNil,"emulator crashed"); Yap_Error(SYSTEM_ERROR,TermNil,"emulator crashed");
@ -1329,7 +1345,7 @@ Yap_execute_pred(PredEntry *ppe, CELL *pt USES_REGS)
} }
Int Int
Yap_execute_goal(Term t, int nargs, Term mod) Yap_execute_goal(Term t, int nargs, Term mod, bool pass_ex)
{ {
CACHE_REGS CACHE_REGS
Prop pe; Prop pe;
@ -1364,7 +1380,7 @@ Yap_execute_goal(Term t, int nargs, Term mod)
if (pe == NIL) { if (pe == NIL) {
return CallMetaCall(t, mod PASS_REGS); return CallMetaCall(t, mod PASS_REGS);
} }
return Yap_execute_pred(ppe, pt PASS_REGS); return Yap_execute_pred(ppe, pt, pass_ex PASS_REGS);
} }
@ -1401,7 +1417,7 @@ Yap_RunTopGoal(Term t)
Term mod = CurrentModule; Term mod = CurrentModule;
Term goal_out = 0; Term goal_out = 0;
restart_runtopgoal: restart_runtopgoal:
if (IsAtomTerm(t)) { if (IsAtomTerm(t)) {
Atom a = AtomOfTerm(t); Atom a = AtomOfTerm(t);
pt = NULL; pt = NULL;
@ -1447,6 +1463,7 @@ Yap_RunTopGoal(Term t)
PELOCK(82,ppe); PELOCK(82,ppe);
CodeAdr = ppe->CodeOfPred; CodeAdr = ppe->CodeOfPred;
UNLOCK(ppe->PELock); UNLOCK(ppe->PELock);
#if !USE_SYSTEM_MALLOC #if !USE_SYSTEM_MALLOC
if (LOCAL_TrailTop - HeapTop < 2048) { if (LOCAL_TrailTop - HeapTop < 2048) {
LOCAL_PrologMode = BootMode; LOCAL_PrologMode = BootMode;
@ -1616,6 +1633,39 @@ p_cut_up_to_next_disjunction( USES_REGS1 ) {
return TRUE; return TRUE;
} }
bool
Yap_Reset(yap_reset_t mode)
{
CACHE_REGS
int res = TRUE;
if (EX) {
LOCAL_BallTerm = EX;
}
EX = NULL;
Yap_ResetExceptionTerm( 0 );
LOCAL_UncaughtThrow = FALSE;
/* first, backtrack to the root */
while (B->cp_b) {
B = B->cp_b;
}
// B shoul lead to CP with _ystop0,,
P = FAILCODE;
res = Yap_exec_absmi( true, mode );
/* reinitialise the engine */
// Yap_InitYaamRegs( worker_id );
GLOBAL_Initialised = true;
ENV = LCL0;
ASP = (CELL *)B;
/* the first real choice-point will also have AP=FAIL */
/* always have an empty slots for people to use */
P = CP = YESCODE;
// ensure that we have slots where we need them
Yap_RebootSlots( worker_id );
return res;
}
static int is_cleanup_cp(choiceptr cp_b) static int is_cleanup_cp(choiceptr cp_b)
{ {
PredEntry *pe; PredEntry *pe;
@ -1659,38 +1709,13 @@ JumpToEnv(Term t USES_REGS) {
env1 = ENV; env1 = ENV;
do { do {
/* find the first choicepoint that may be a catch */ /* find the first choicepoint that may be a catch */
while (handler != NULL && handler->cp_ap != pos) { while (handler &&
handler->cp_ap != pos) {
/* we are already doing a catch */ /* we are already doing a catch */
if (handler->cp_ap == catchpos) { if (handler->cp_ap == catchpos) {
P = (yamop *)FAILCODE; P = (yamop *)FAILCODE;
return TRUE; return TRUE;
}
/* we have a cleanup handler in the middle */
if (is_cleanup_cp(handler)) {
/* keep it around */
if (previous == NULL) {
B = handler;
} else {
previous->cp_b = handler;
}
previous = handler;
#ifdef TABLING
} else {
if (handler->cp_ap != NOCODE) {
abolish_incomplete_subgoals(handler);
}
#endif /* TABLING */
}
/* we reached C-Code */
if (handler->cp_ap == NOCODE) {
/* up to the C-code to deal with this! */
LOCAL_UncaughtThrow = TRUE;
if (previous == NULL)
B = handler;
else
previous->cp_b = handler;
EX = LOCAL_BallTerm;
LOCAL_BallTerm = NULL;
P = (yamop *)FAILCODE; P = (yamop *)FAILCODE;
/* make sure failure will be seen at next port */ /* make sure failure will be seen at next port */
if (LOCAL_PrologMode & AsyncIntMode) { if (LOCAL_PrologMode & AsyncIntMode) {
@ -1718,7 +1743,7 @@ JumpToEnv(Term t USES_REGS) {
} }
/* yes, we found it ! */ /* yes, we found it ! */
// while (env < ENV) // while (env < ENV)
// env = ENV_Parent(env); // env = ENV_Paren t(env);
if (env == env1) { if (env == env1) {
break; break;
} }
@ -1791,7 +1816,6 @@ Yap_InitYaamRegs( int myworker_id )
if (myworker_id) { if (myworker_id) {
REGSTORE *rs = REMOTE_ThreadHandle(myworker_id).default_yaam_regs; REGSTORE *rs = REMOTE_ThreadHandle(myworker_id).default_yaam_regs;
pthread_setspecific(Yap_yaamregs_key, (const void *)rs); pthread_setspecific(Yap_yaamregs_key, (const void *)rs);
REMOTE_PL_local_data_p(myworker_id)->reg_cache = rs;
REMOTE_ThreadHandle(myworker_id).current_yaam_regs = rs; REMOTE_ThreadHandle(myworker_id).current_yaam_regs = rs;
REFRESH_CACHE_REGS REFRESH_CACHE_REGS
} }