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 was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: exec.c *
* Last rev: 8/2/88 *
* mods: *
* comments: Execute Prolog code *
* *
*************************************************************************/
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: exec.c *
* Last rev: 8/2/88 *
* mods: *
* comments: Execute Prolog code *
* *
*************************************************************************/
#ifdef SCCS
static char SccsId[] = "@(#)cdmgr.c 1.1 05/02/98";
#endif
@ -131,7 +131,7 @@ Yap_PredicateIndicator(Term t, Term mod)
static Int
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));
} else {
if (err == TYPE_ERROR_CALLABLE) {
@ -198,7 +198,7 @@ do_execute(Term t, Term mod USES_REGS)
!(LOCAL_PrologMode & (AbortMode|InterruptMode|SystemMode))) {
return EnterCreepMode(t, mod PASS_REGS);
}
restart_exec:
restart_exec:
if (IsVarTerm(t)) {
return CallError(INSTANTIATION_ERROR, t0, mod PASS_REGS);
} else if (IsApplTerm(t)) {
@ -245,7 +245,7 @@ do_execute(Term t, Term mod USES_REGS)
#if YAPOR_SBA
Term d0 = *pt++;
if (d0 == 0)
` XREGS[i] = (CELL)(pt-1);
` XREGS[i] = (CELL)(pt-1);
else
XREGS[i] = d0;
#else
@ -321,7 +321,7 @@ do_execute_n(Term t, Term mod, unsigned int n USES_REGS)
int j = -n;
Term t0 = t;
restart_exec:
restart_exec:
if (IsVarTerm(t)) {
return CallError(INSTANTIATION_ERROR, t0, mod PASS_REGS);
} else if (IsAtomTerm(t)) {
@ -608,7 +608,7 @@ p_execute_clause( USES_REGS1 )
yamop *code;
Term clt = Deref(ARG3);
restart_exec:
restart_exec:
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,ARG3,"call/1");
return FALSE;
@ -686,7 +686,7 @@ p_do_goal_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, cmod) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE;
ARG3 = ARG2;
goto complete;
@ -695,7 +695,7 @@ p_do_goal_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, SYSTEM_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE;
ARG3 = ARG2;
goto complete;
@ -706,7 +706,7 @@ p_do_goal_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion, USER_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE;
goto complete;
}
@ -716,11 +716,11 @@ p_do_goal_expansion( USES_REGS1 )
(pe = RepPredProp(Yap_GetPredPropByFunc(FunctorGoalExpansion2, USER_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
ARG3 = ARG2;
out = TRUE;
}
complete:
complete:
if (creeping) {
Yap_signal( YAP_CREEP_SIGNAL );
}
@ -739,7 +739,7 @@ p_do_term_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, cmod) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE;
goto complete;
}
@ -747,7 +747,7 @@ p_do_term_expansion( USES_REGS1 )
if ( (pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, SYSTEM_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE;
goto complete;
}
@ -756,10 +756,10 @@ p_do_term_expansion( USES_REGS1 )
(pe = RepPredProp(Yap_GetPredPropByFunc(FunctorTermExpansion, USER_MODULE ) ) ) &&
pe->OpcodeOfPred != FAIL_OPCODE &&
pe->OpcodeOfPred != UNDEF_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
Yap_execute_pred(pe, NULL PASS_REGS, false) ) {
out = TRUE;
}
complete:
complete:
if (creeping) {
Yap_signal(YAP_CREEP_SIGNAL);
}
@ -778,7 +778,7 @@ p_execute0( USES_REGS1 )
!LOCAL_InterruptsDisabled) {
return EnterCreepMode(t, mod PASS_REGS);
}
restart_exec:
restart_exec:
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,ARG3,"call/1");
return FALSE;
@ -844,7 +844,7 @@ p_execute_nonstop( USES_REGS1 )
unsigned int arity;
Prop pe;
restart_exec:
restart_exec:
if (IsVarTerm(mod)) {
mod = CurrentModule;
} else if (!IsAtomTerm(mod)) {
@ -1251,7 +1251,7 @@ Yap_fail_all( choiceptr bb USES_REGS )
}
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 *CodeAdr;
@ -1305,6 +1305,14 @@ Yap_execute_pred(PredEntry *ppe, CELL *pt USES_REGS)
if (B != NULL)
HB = B->cp_h;
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;
} else if (out == 0) {
P = saved_p;
@ -1321,6 +1329,14 @@ Yap_execute_pred(PredEntry *ppe, CELL *pt USES_REGS)
B = B->cp_b;
SET_BB(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);
} else {
Yap_Error(SYSTEM_ERROR,TermNil,"emulator crashed");
@ -1329,7 +1345,7 @@ Yap_execute_pred(PredEntry *ppe, CELL *pt USES_REGS)
}
Int
Yap_execute_goal(Term t, int nargs, Term mod)
Yap_execute_goal(Term t, int nargs, Term mod, bool pass_ex)
{
CACHE_REGS
Prop pe;
@ -1364,7 +1380,7 @@ Yap_execute_goal(Term t, int nargs, Term mod)
if (pe == NIL) {
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 goal_out = 0;
restart_runtopgoal:
restart_runtopgoal:
if (IsAtomTerm(t)) {
Atom a = AtomOfTerm(t);
pt = NULL;
@ -1447,6 +1463,7 @@ Yap_RunTopGoal(Term t)
PELOCK(82,ppe);
CodeAdr = ppe->CodeOfPred;
UNLOCK(ppe->PELock);
#if !USE_SYSTEM_MALLOC
if (LOCAL_TrailTop - HeapTop < 2048) {
LOCAL_PrologMode = BootMode;
@ -1616,6 +1633,39 @@ p_cut_up_to_next_disjunction( USES_REGS1 ) {
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)
{
PredEntry *pe;
@ -1659,38 +1709,13 @@ JumpToEnv(Term t USES_REGS) {
env1 = ENV;
do {
/* 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 */
if (handler->cp_ap == catchpos) {
P = (yamop *)FAILCODE;
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;
/* make sure failure will be seen at next port */
if (LOCAL_PrologMode & AsyncIntMode) {
@ -1718,7 +1743,7 @@ JumpToEnv(Term t USES_REGS) {
}
/* yes, we found it ! */
// while (env < ENV)
// env = ENV_Parent(env);
// env = ENV_Paren t(env);
if (env == env1) {
break;
}
@ -1791,7 +1816,6 @@ Yap_InitYaamRegs( int myworker_id )
if (myworker_id) {
REGSTORE *rs = REMOTE_ThreadHandle(myworker_id).default_yaam_regs;
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;
REFRESH_CACHE_REGS
}