Merge ssh://ssh.dcc.fc.up.pt:31064/home/vsc/yap

This commit is contained in:
Vitor Santos Costa 2019-05-02 21:03:57 +01:00
commit 644606d8bb
30 changed files with 746 additions and 547 deletions

1
.gitignore vendored
View File

@ -215,3 +215,4 @@ cmake/docs/Doxyfile
*.stackdump *.stackdump
*.gz *.gz
.Rproj.user

View File

@ -1,4 +1,4 @@
/************************************************************************* * /************************************************************************* *
* YAP Prolog * * YAP Prolog *
* Yap Prolog was developed at NCCUP - Universidade do Porto * * Yap Prolog was developed at NCCUP - Universidade do Porto *
* * * *
@ -1142,6 +1142,8 @@ X_API Int YAP_Execute(PredEntry *pe, CPredicate exec_code) {
// if (pe->PredFlags & CArgsPredFlag) { // if (pe->PredFlags & CArgsPredFlag) {
// CurrentModule = pe->ModuleOfPred; // CurrentModule = pe->ModuleOfPred;
//} //}
int lvl = push_text_stack();
yhandle_t hdl = Yap_CurrentHandle();
if (pe->PredFlags & SWIEnvPredFlag) { if (pe->PredFlags & SWIEnvPredFlag) {
CPredicateV codev = (CPredicateV)exec_code; CPredicateV codev = (CPredicateV)exec_code;
struct foreign_context ctx; struct foreign_context ctx;
@ -1161,12 +1163,15 @@ X_API Int YAP_Execute(PredEntry *pe, CPredicate exec_code) {
// check for junk: open frames, etc */ // check for junk: open frames, etc */
if (ret) if (ret)
complete_exit(((choiceptr)(LCL0 - OASP)), FALSE, FALSE PASS_REGS); complete_exit(((choiceptr)(LCL0 - OASP)), FALSE, FALSE PASS_REGS);
else else {
complete_fail(((choiceptr)(LCL0 - OASP)), FALSE PASS_REGS); complete_fail(((choiceptr)(LCL0 - OASP)), FALSE PASS_REGS);
}
// CurrentModule = omod; // CurrentModule = omod;
if (!ret) { if (!ret) {
Yap_RaiseException(); Yap_RaiseException();
} }
Yap_RecoverHandles(0, hdl);
pop_text_stack( lvl );
return ret; return ret;
} }
@ -1739,17 +1744,18 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
CACHE_REGS CACHE_REGS
PredEntry *pe = ape; PredEntry *pe = ape;
bool out; bool out;
// fprintf(stderr,"EnterGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p // fprintf(stderr,"1EnterGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p
// Slots=%d\n",HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, // Slots=%d\n",HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP,
// LOCAL_CurSlot); // LOCAL_CurSlot);
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
dgi->lvl = push_text_stack();
LOCAL_ActiveError->errorNo = YAP_NO_ERROR; LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
LOCAL_PrologMode = UserMode; LOCAL_PrologMode = UserMode;
dgi->p = P; dgi->p = P;
dgi->cp = CP; dgi->cp = CP;
dgi->b0 = LCL0 - (CELL *)B; dgi->b0 = LCL0 - (CELL *)B;
dgi->CurSlot = LOCAL_CurSlot; dgi->env0 = LCL0 - ENV;
// ensure our current ENV receives current P. // ensure our current ENV receives current P.
Yap_PrepGoal(pe->ArityOfPE, nullptr, B PASS_REGS); Yap_PrepGoal(pe->ArityOfPE, nullptr, B PASS_REGS);
@ -1757,7 +1763,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
// __android_log_print(ANDROID_LOG_INFO, "YAP ", "ap=%p %d %x %x args=%x,%x // __android_log_print(ANDROID_LOG_INFO, "YAP ", "ap=%p %d %x %x args=%x,%x
// slot=%d", pe, pe->CodeOfPred->opc, FAILCODE, Deref(ARG1), Deref(ARG2), // slot=%d", pe, pe->CodeOfPred->opc, FAILCODE, Deref(ARG1), Deref(ARG2),
// LOCAL_CurSlot); // LOCAL_CurSlot);
dgi->b = LCL0 - (CELL *)B; dgi->b_entry = LCL0 - (CELL *)B;
dgi->h = HR - H0; dgi->h = HR - H0;
dgi->tr = (CELL *)TR - LCL0; dgi->tr = (CELL *)TR - LCL0;
// fprintf(stderr,"PrepGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", // fprintf(stderr,"PrepGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n",
@ -1766,7 +1772,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
// fprintf(stderr,"EnterGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p // fprintf(stderr,"EnterGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p
// Slots=%d\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, // Slots=%d\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP,
// LOCAL_CurSlot); // LOCAL_CurSlot);
dgi->b = LCL0 - (CELL *)B; dgi->b_exit = LCL0 - (CELL *)B;
if (out) { if (out) {
dgi->EndSlot = LOCAL_CurSlot; dgi->EndSlot = LOCAL_CurSlot;
Yap_StartSlots(); Yap_StartSlots();
@ -1774,6 +1780,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
LOCAL_CurSlot = LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal dgi->CurSlot; // ignore any slots created within the called goal
} }
pop_text_stack(dgi->lvl);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
@ -1784,8 +1791,9 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
bool out; bool out;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
myB = (choiceptr)(LCL0 - dgi->b); dgi->lvl = push_text_stack();
myB0 = (choiceptr)(LCL0 - dgi->b0); myB = (choiceptr)(LCL0 - dgi->b_exit);
myB0 = (choiceptr)(LCL0 - dgi->b_entry);
CP = myB->cp_cp; CP = myB->cp_cp;
/* sanity check */ /* sanity check */
if (B >= myB0) { if (B >= myB0) {
@ -1804,52 +1812,49 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
out = Yap_exec_absmi(true, true ); out = Yap_exec_absmi(true, true );
if (out) { if (out) {
dgi->EndSlot = LOCAL_CurSlot; dgi->EndSlot = LOCAL_CurSlot;
dgi->b = LCL0 - (CELL *)B; dgi->b_exit = LCL0 - (CELL *)B;
} else { } else {
printf("F %ld\n", dgi->CurSlot);
LOCAL_CurSlot = LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal dgi->CurSlot; // ignore any slots created within the called goal
} }
pop_text_stack(dgi->lvl);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
} }
X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
CACHE_REGS CACHE_REGS
choiceptr myB, handler;
// fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d // fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%ld myB=%ld TR=%d
// P=%p CP=%p Slots=%d\n", // P=%p CP=%p Slots=%d\n",
// successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP, // successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,dgi->b0,(CELL*)TR-LCL0, P, CP,
// LOCAL_CurSlot); // LOCAL_CurSlot);
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
myB = (choiceptr)(LCL0 - dgi->b);
if (LOCAL_PrologMode & AsyncIntMode) { dgi->lvl = push_text_stack();
Yap_signal(YAP_FAIL_SIGNAL);
}
handler = B;
while (handler &&
LCL0 - LOCAL_CBorder > (CELL *)handler
//&& handler->cp_ap != NOCODE
&& handler->cp_b != NULL && handler != myB) {
if (handler < myB) {
handler->cp_ap = TRUSTFAILCODE;
}
B = handler;
handler = handler->cp_b;
if (successful) { if (successful) {
choiceptr nB = (choiceptr)(LCL0 - dgi->b_entry);
if (B <= nB) {
B = nB;
}
Yap_TrimTrail(); Yap_TrimTrail();
} else if (!(LOCAL_PrologMode & AsyncIntMode)) { B = B->cp_b;
P = FAILCODE; } else if (LOCAL_PrologMode & AsyncIntMode) {
Yap_exec_absmi(true, YAP_EXEC_ABSMI);
}
}
if (LOCAL_PrologMode & AsyncIntMode) {
Yap_signal(YAP_FAIL_SIGNAL); Yap_signal(YAP_FAIL_SIGNAL);
} }
B = (choiceptr)(LCL0 - dgi->b0);
#ifdef DEPTH_LIMIT
DEPTH = B->cp_depth;
#endif
P = dgi->p; P = dgi->p;
CP = dgi->cp; CP = dgi->cp;
YENV = ENV = LCL0-dgi->env0;
LOCAL_CurSlot =
dgi->CurSlot; // ignore any slots created within the called goal
pop_text_stack(dgi->lvl);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
// fprintf(stderr,"LeftGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p // 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, // Slots=%d\n", successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P,
// CP, LOCAL_CurSlot); // CP, LOCAL_CurSlot);
return TRUE; return TRUE;
@ -1861,6 +1866,8 @@ X_API Int YAP_RunGoal(Term t) {
yhandle_t cslot = LOCAL_CurSlot; yhandle_t cslot = LOCAL_CurSlot;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
int lvl = push_text_stack();
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
LOCAL_PrologMode = UserMode; LOCAL_PrologMode = UserMode;
out = Yap_RunTopGoal(t, true); out = Yap_RunTopGoal(t, true);
@ -1869,6 +1876,7 @@ X_API Int YAP_RunGoal(Term t) {
// We'll pass it through // We'll pass it through
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
LOCAL_CurSlot = cslot; LOCAL_CurSlot = cslot;
pop_text_stack(lvl);
return out; return out;
} }
@ -1942,11 +1950,12 @@ X_API CELL *YAP_HeapStoreOpaqueTerm(Term t) {
X_API Int YAP_RunGoalOnce(Term t) { X_API Int YAP_RunGoalOnce(Term t) {
CACHE_REGS CACHE_REGS
Term out; Term out;
yamop *old_CP = CP; yamop *old_CP = CP, *old_P = P;
Int oldPrologMode = LOCAL_PrologMode; Int oldPrologMode = LOCAL_PrologMode;
yhandle_t CSlot; yhandle_t CSlot;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
int lvl = push_text_stack();
CSlot = Yap_StartSlots(); CSlot = Yap_StartSlots();
LOCAL_PrologMode = UserMode; LOCAL_PrologMode = UserMode;
// Yap_heap_regs->yap_do_low_level_trace=true; // Yap_heap_regs->yap_do_low_level_trace=true;
@ -1955,6 +1964,7 @@ X_API Int YAP_RunGoalOnce(Term t) {
// Yap_CloseSlots(CSlot); // Yap_CloseSlots(CSlot);
if (!(oldPrologMode & UserCCallMode)) { if (!(oldPrologMode & UserCCallMode)) {
/* called from top-level */ /* called from top-level */
pop_text_stack( lvl);
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return out; return out;
@ -1989,13 +1999,14 @@ X_API Int YAP_RunGoalOnce(Term t) {
ASP = B->cp_env; ASP = B->cp_env;
ENV = (CELL *)ASP[E_E]; ENV = (CELL *)ASP[E_E];
B = (choiceptr)ASP[E_CB]; B = (choiceptr)ASP[E_CB];
#ifdef DEPTH_LIMITxs #ifdef DEPTH_LIMIT
DEPTH = ASP[E_DEPTH]; DEPTH = ASP[E_DEPTH];
#endif #endif
P = (yamop *)ASP[E_CP]; P = old_P;
CP = old_CP; CP = old_CP;
LOCAL_AllowRestart = FALSE; LOCAL_AllowRestart = FALSE;
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
pop_text_stack( lvl);
return out; return out;
} }
@ -2078,7 +2089,7 @@ X_API void YAP_PruneGoal(YAP_dogoalinfo *gi) {
CACHE_REGS CACHE_REGS
BACKUP_B(); BACKUP_B();
choiceptr myB = (choiceptr)(LCL0 - gi->b); choiceptr myB = (choiceptr)(LCL0 - gi->b_entry);
while (B != myB) { while (B != myB) {
/* make sure we prune C-choicepoints */ /* make sure we prune C-choicepoints */
if (POP_CHOICE_POINT(B->cp_b)) { if (POP_CHOICE_POINT(B->cp_b)) {
@ -2271,6 +2282,7 @@ X_API char *YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) {
} else { } else {
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
if (buf == out.val.c) { if (buf == out.val.c) {
pop_text_stack(l);
return buf; return buf;
} else { } else {
if ( strlen(out.val.c ) < sze) { if ( strlen(out.val.c ) < sze) {

View File

@ -804,11 +804,11 @@ static Int execute_in_mod(USES_REGS1) { /* '$execute'(Goal) */
static void prune_inner_computation(choiceptr parent) { static void prune_inner_computation(choiceptr parent) {
/* 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) { while (cut_pt && cut_pt->cp_b < parent) {
if (cut_pt->cp_ap == NOCODE)
break;
cut_pt = cut_pt->cp_b; cut_pt = cut_pt->cp_b;
} }
if (!cut_pt) if (!cut_pt)
@ -819,9 +819,6 @@ 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;
CP = oCP;
ENV = LCL0 - oENV;
B = parent; B = parent;
} }
@ -1062,8 +1059,6 @@ static Int cleanup_on_exit(USES_REGS1) {
complete_pt[0] = TermExit; complete_pt[0] = TermExit;
} }
Yap_ignore(cleanup, false); Yap_ignore(cleanup, false);
if (B0->cp_ap == NOCODE)
B0->cp_ap = TRUSTFAILCODE;
if (Yap_RaiseException()) { if (Yap_RaiseException()) {
return false; return false;
} }
@ -1617,7 +1612,6 @@ static bool exec_absmi(bool top, yap_reset_t reset_mode USES_REGS) {
CalculateStackGap(PASS_REGS1); CalculateStackGap(PASS_REGS1);
LOCAL_CBorder = OldBorder; LOCAL_CBorder = OldBorder;
LOCAL_RestartEnv = sighold; LOCAL_RestartEnv = sighold;
pop_text_stack(i + 1);
return out; return out;
} }
@ -2272,7 +2266,7 @@ void Yap_InitYaamRegs(int myworker_id, bool full_reset) {
REMOTE_AttsMutableList(myworker_id) = Yap_NewTimedVar(h0var); REMOTE_AttsMutableList(myworker_id) = Yap_NewTimedVar(h0var);
#endif #endif
size_t defsz = 128*1024; size_t defsz = 128*1024;
Yap_AllocateDefaultArena(defsz, myworker_id); Yap_AllocateDefaultArena(defsz, myworker_id, NULL);
} else { } else {
HR = Yap_ArenaLimit(REMOTE_GlobalArena(myworker_id)); HR = Yap_ArenaLimit(REMOTE_GlobalArena(myworker_id));
} }

View File

@ -78,7 +78,7 @@ assert/1 or recorda/3.
+ The value lives on the Prolog (global) stack. This implies + The value lives on the Prolog (global) stack. This implies
that lookup time is independent from the size of the term. that lookup time is independent from the size of the term.
This is particulary interesting for large data structures This is particulary interesting for large data structures
such as parsed XML documents or the CHR global constraint qqqsuch as parsed XML documents or the CHR global constraint
store. store.
They support both global assignment using nb_setval/2 and They support both global assignment using nb_setval/2 and
@ -139,6 +139,26 @@ threads that are created <em>after</em> the registration.
#define HEAP_ARENA 2 #define HEAP_ARENA 2
#define HEAP_START 3 #define HEAP_START 3
/// A cell_space is a short code region, where we want bindings to proceed locally.
/// It is used in copy_term,,,,,,,,,,,,,,,,,,,,,,,
///
typedef struct cell_space {
struct cell_space *parent; //`
CELL *oASP, *oH, *oHB;
} cell_space_t;
INLINE_ONLY void enter_cell_space(cell_space_t *cs) {
cs->oH = HR;
cs->oHB = HB;
cs->oASP = ASP;
}
INLINE_ONLY void exit_cell_space(cell_space_t *cs) {
HR = cs->oH;
HB = cs->oHB;
ASP = cs->oASP;
}
#define MIN_ARENA_SIZE (1048L) #define MIN_ARENA_SIZE (1048L)
#define MAX_ARENA_SIZE (2048 * 16) #define MAX_ARENA_SIZE (2048 * 16)
@ -186,33 +206,37 @@ static Term CreateNewArena(CELL *ptr, UInt size) {
return t; return t;
} }
static Term NewArena(UInt size, int wid, UInt arity, CELL *where) { static Term NewArena(UInt size, int wid, UInt arity, CELL *where, struct cell_space *cellSpace) {
Term t; Term t;
UInt new_size; UInt new_size;
WORKER_REGS(wid) WORKER_REGS(wid)
exit_cell_space(cellSpace);
if (where == NULL || where == HR) { // make sure we have enough room
while (HR + size > ASP - 1024) { while (HR + size > ASP - MIN_ARENA_SIZE) {
if (!Yap_gcl(size * sizeof(CELL), arity, ENV, P)) { if (!Yap_gcl(size * sizeof(CELL), arity, ENV, P)) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return TermNil; return TermNil;
} }
} }
if (where == NULL || where == HR) {
t = CreateNewArena(HR, size); t = CreateNewArena(HR, size);
HR += size; HR += size;
new_size = size;
} else { } else {
if ((new_size = Yap_InsertInGlobal(where, size * sizeof(CELL))) == 0) { if ((new_size = Yap_InsertInGlobal(where, size * sizeof(CELL))) == 0) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil,
"No Stack Space for Non-Backtrackable terms"); "No Stack Space for Non-Backtrackable terms");
return TermNil; return TermNil;
} }
size = new_size / sizeof(CELL); size = new_size / sizeof(CELL);
t = CreateNewArena(where, size); t = CreateNewArena(where, size);
} }
enter_cell_space(cellSpace);
return t; return t;
} }
static Int p_allocate_arena(USES_REGS1) { static Int p_allocate_arena(USES_REGS1) {
cell_space_t cspace;
Term t = Deref(ARG1); Term t = Deref(ARG1);
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "allocate_arena"); Yap_Error(INSTANTIATION_ERROR, t, "allocate_arena");
@ -221,15 +245,21 @@ static Int p_allocate_arena(USES_REGS1) {
Yap_Error(TYPE_ERROR_INTEGER, t, "allocate_arena"); Yap_Error(TYPE_ERROR_INTEGER, t, "allocate_arena");
return FALSE; return FALSE;
} }
return Yap_unify(ARG2, NewArena(IntegerOfTerm(t), worker_id, 1, NULL)); enter_cell_space(&cspace);
return Yap_unify(ARG2, NewArena(IntegerOfTerm(t), worker_id, 1, NULL, &cspace));
} }
static Int p_default_arena_size(USES_REGS1) { static Int p_default_arena_size(USES_REGS1) {
return Yap_unify(ARG1, MkIntegerTerm(ArenaSz(LOCAL_GlobalArena))); return Yap_unify(ARG1, MkIntegerTerm(ArenaSz(LOCAL_GlobalArena)));
} }
void Yap_AllocateDefaultArena(size_t gsize, int wid) { void Yap_AllocateDefaultArena(size_t gsize, int wid, void *cs) {
REMOTE_GlobalArena(wid) = NewArena(gsize, wid, 2, NULL); cell_space_t ics;
if (cs == NULL) {
enter_cell_space(&ics);
cs = &ics;
}
REMOTE_GlobalArena(wid) = NewArena(gsize, wid, 2, NULL, cs);
} }
static void adjust_cps(UInt size USES_REGS) { static void adjust_cps(UInt size USES_REGS) {
@ -241,8 +271,9 @@ static void adjust_cps(UInt size USES_REGS) {
} }
} }
static int GrowArena(Term arena, CELL *pt, size_t old_size, size_t size, static Term GrowArena(Term arena, CELL *pt, size_t old_size, size_t size,
UInt arity USES_REGS) { UInt arity, cell_space_t *cspace USES_REGS) {
size_t sz;
LOCAL_ArenaOverflows++; LOCAL_ArenaOverflows++;
if (size == 0) { if (size == 0) {
if (old_size < 128 * 1024) { if (old_size < 128 * 1024) {
@ -251,59 +282,52 @@ static int GrowArena(Term arena, CELL *pt, size_t old_size, size_t size,
size = old_size + 128 * 1024; size = old_size + 128 * 1024;
} }
} }
if (size < 4096) { if (size < 4*MIN_ARENA_SIZE) {
size = 4096; size = 4*MIN_ARENA_SIZE;
} }
if (pt == HR) { exit_cell_space(cspace);
if (HR + size > ASP - 1024) { while (HR + size > ASP - MIN_ARENA_SIZE) {
XREGS[arity + 1] = arena; XREGS[arity + 1] = arena;
if (!Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP))) { if (!Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP))) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return FALSE; return false;
} }
arena = XREGS[arity + 1]; arena = XREGS[arity + 1];
/* we don't know if the GC added junk on top of the global */ adjust_cps(size PASS_REGS);
pt = ArenaLimit(arena);
return GrowArena(arena, pt, old_size, size, arity PASS_REGS);
}
adjust_cps(size PASS_REGS);
HR += size;
} else {
XREGS[arity + 1] = arena;
/* try to recover some room */
Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP));
arena = XREGS[arity + 1];
pt = ArenaLimit(arena);
if ((size = Yap_InsertInGlobal(pt, size * sizeof(CELL))) == 0) {
return FALSE;
}
size = size / sizeof(CELL);
arena = XREGS[arity + 1];
} }
CreateNewArena(ArenaPt(arena), size + old_size); pt = ArenaLimit(arena)+1;
return TRUE; sz = old_size + size;
if (pt == HR) {
HR += size+1;
HR[ - 1] = EndSpecials;
} else {
if ((sz = Yap_InsertInGlobal(pt, size * sizeof(CELL))) == 0) {
return FALSE;
}
pt[size-1] = EndSpecials;
}
arena = XREGS[arity + 1];
MP_INT *dst = (MP_INT *)(RepAppl(arena) + 2);
dst->_mp_alloc+= size;
arena = XREGS[arity + 1];
return sz;
} }
CELL *Yap_GetFromArena(Term *arenap, UInt cells, UInt arity) { CELL *Yap_GetFromArena(Term *arenap, UInt cells, UInt arity) {
CACHE_REGS CACHE_REGS
restart : {
Term arena = *arenap; Term arena = *arenap;
CELL *max = ArenaLimit(arena); CELL *max = ArenaLimit(arena);
CELL *base = ArenaPt(arena); CELL *base = ArenaPt(arena);
CELL *newH; CELL *newH;
UInt old_sz = ArenaSz(arena), new_size; UInt old_sz = ArenaSz(arena), new_size;
while(true) {
if (IN_BETWEEN(base, HR, max)) { if (IN_BETWEEN(base, HR, max)) {
base = HR; base = HR;
HR += cells; HR += cells;
return base; return base;
} }
if (base + cells > max - 1024) { if (base + cells > ASP - 1024) {
if (!GrowArena(arena, max, old_sz, old_sz + sizeof(CELL) * 1024, continue;
arity PASS_REGS))
return NULL;
goto restart;
} }
newH = base + cells; newH = base + cells;
@ -313,17 +337,15 @@ restart : {
} }
} }
static void CloseArena(CELL *oldH, CELL *oldHB, CELL *oldASP, Term *oldArenaP, static void CloseArena(cell_space_t *region, Term *oldArenaP,
UInt old_size USES_REGS) { UInt old_size USES_REGS) {
UInt new_size; UInt new_size;
if (HR == oldH) if (HR == region->oH)
return; return;
new_size = old_size - (HR - RepAppl(*oldArenaP)); new_size = old_size - (HR - RepAppl(*oldArenaP));
*oldArenaP = CreateNewArena(HR, new_size); *oldArenaP = CreateNewArena(HR, new_size);
HR = oldH; exit_cell_space( region );
HB = oldHB;
ASP = oldASP;
} }
static inline void clean_dirty_tr(tr_fr_ptr TR0 USES_REGS) { static inline void clean_dirty_tr(tr_fr_ptr TR0 USES_REGS) {
@ -474,7 +496,9 @@ loop:
HR += ap2[1] + 3; HR += ap2[1] + 3;
break; break;
default: { default: {
/* big int */
/* big int */
UInt sz = (sizeof(MP_INT) + 3 * CellSize + UInt sz = (sizeof(MP_INT) + 3 * CellSize +
((MP_INT *)(ap2 + 2))->_mp_alloc * sizeof(mp_limb_t)) / ((MP_INT *)(ap2 + 2))->_mp_alloc * sizeof(mp_limb_t)) /
CellSize, CellSize,
@ -482,7 +506,7 @@ loop:
if (HR > ASP - (MIN_ARENA_SIZE + sz)) { if (HR > ASP - (MIN_ARENA_SIZE + sz)) {
goto overflow; goto overflow;
} }
*ptf++ = AbsAppl(HR); *ptf++ = AbsAppl(HR);
HR[0] = (CELL)f; HR[0] = (CELL)f;
for (i = 1; i < sz; i++) { for (i = 1; i < sz; i++) {
@ -494,6 +518,7 @@ loop:
continue; continue;
} }
*ptf = AbsAppl(HR); *ptf = AbsAppl(HR);
ptf++; ptf++;
/* store the terms to visit */ /* store the terms to visit */
#ifdef RATIONAL_TREES #ifdef RATIONAL_TREES
@ -643,9 +668,8 @@ static Term CopyTermToArena(Term t, Term arena, bool share, bool copy_att_vars,
UInt arity, Term *newarena, UInt arity, Term *newarena,
size_t min_grow USES_REGS) { size_t min_grow USES_REGS) {
size_t old_size = ArenaSz(arena); size_t old_size = ArenaSz(arena);
CELL *oldH = HR; cell_space_t cspace;
CELL *oldHB = HB; enter_cell_space( & cspace);
CELL *oldASP = ASP;
int res = 0; int res = 0;
Term tn; Term tn;
@ -664,12 +688,12 @@ restart:
if ((res = copy_complex_term(Hi - 2, Hi - 1, share, copy_att_vars, Hi, if ((res = copy_complex_term(Hi - 2, Hi - 1, share, copy_att_vars, Hi,
Hi PASS_REGS)) < 0) Hi PASS_REGS)) < 0)
goto error_handler; goto error_handler;
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cspace, newarena, old_size PASS_REGS);
return Hi[0]; return Hi[0];
} }
#endif #endif
if (share && VarOfTerm(t) > ArenaPt(arena)) { if (share && VarOfTerm(t) > ArenaPt(arena)) {
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cspace, newarena, old_size PASS_REGS);
return t; return t;
} }
tn = MkVarTerm(); tn = MkVarTerm();
@ -677,7 +701,7 @@ restart:
res = -1; res = -1;
goto error_handler; goto error_handler;
} }
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cspace, newarena, old_size PASS_REGS);
return tn; return tn;
} else if (IsAtomOrIntTerm(t)) { } else if (IsAtomOrIntTerm(t)) {
return t; return t;
@ -699,7 +723,7 @@ restart:
Hi PASS_REGS)) < 0) { Hi PASS_REGS)) < 0) {
goto error_handler; goto error_handler;
} }
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cspace, newarena, old_size PASS_REGS);
return tf; return tf;
} else { } else {
Functor f; Functor f;
@ -720,7 +744,7 @@ restart:
if (IsExtensionFunctor(f)) { if (IsExtensionFunctor(f)) {
switch ((CELL)f) { switch ((CELL)f) {
case (CELL) FunctorDBRef: case (CELL) FunctorDBRef:
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cspace, newarena, old_size PASS_REGS);
return t; return t;
case (CELL) FunctorLongInt: case (CELL) FunctorLongInt:
if (HR > ASP - (MIN_ARENA_SIZE + 3)) { if (HR > ASP - (MIN_ARENA_SIZE + 3)) {
@ -779,25 +803,23 @@ restart:
goto error_handler; goto error_handler;
} }
} }
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cspace, newarena, old_size PASS_REGS);
return tf; return tf;
} }
error_handler: error_handler:
HR = HB; HR = HB;
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cspace, newarena, old_size PASS_REGS);
XREGS[arity + 1] = t; XREGS[arity + 1] = t;
XREGS[arity + 2] = arena; XREGS[arity + 2] = arena;
XREGS[arity + 3] = (CELL)newarena; XREGS[arity + 3] = (CELL)newarena;
{ {
CELL *old_top = ArenaLimit(*newarena); CELL *old_top = ArenaLimit(*newarena);
ASP = oldASP; exit_cell_space(&cspace);
HR = oldH;
HB = oldHB;
switch (res) { switch (res) {
case -1: case -1:
if (arena == LOCAL_GlobalArena) if (arena == LOCAL_GlobalArena)
LOCAL_GlobalArenaOverflows++; LOCAL_GlobalArenaOverflows++;
if (!GrowArena(arena, old_top, old_size, min_grow, arity + 3 PASS_REGS)) { if ((arena=GrowArena(arena, old_top, old_size, min_grow, arity + 3, &cspace PASS_REGS))==0) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return 0L; return 0L;
} }
@ -807,9 +829,7 @@ error_handler:
} }
} }
oldH = HR; enter_cell_space(&cspace);
oldHB = HB;
oldASP = ASP;
newarena = (CELL *)XREGS[arity + 3]; newarena = (CELL *)XREGS[arity + 3];
arena = Deref(XREGS[arity + 2]); arena = Deref(XREGS[arity + 2]);
t = XREGS[arity + 1]; t = XREGS[arity + 1];
@ -819,10 +839,9 @@ error_handler:
static Term CreateTermInArena(Term arena, Atom Na, UInt Nar, UInt arity, static Term CreateTermInArena(Term arena, Atom Na, UInt Nar, UInt arity,
Term *newarena, Term init USES_REGS) { Term *newarena, Term init USES_REGS) {
cell_space_t cells;
UInt old_size = ArenaSz(arena); UInt old_size = ArenaSz(arena);
CELL *oldH = HR; enter_cell_space(&cells);
CELL *oldHB = HB;
CELL *oldASP = ASP;
Term tf; Term tf;
CELL *HB0; CELL *HB0;
Functor f = Yap_MkFunctor(Na, Nar); Functor f = Yap_MkFunctor(Na, Nar);
@ -838,26 +857,20 @@ restart:
if (HR > ASP - MIN_ARENA_SIZE) { if (HR > ASP - MIN_ARENA_SIZE) {
/* overflow */ /* overflow */
HR = HB; HR = HB;
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cells, newarena, old_size PASS_REGS);
XREGS[arity + 1] = arena; XREGS[arity + 1] = arena;
XREGS[arity + 2] = (CELL)newarena; XREGS[arity + 2] = (CELL)newarena;
{ {
CELL *old_top = ArenaLimit(*newarena); CELL *old_top = ArenaLimit(*newarena);
ASP = oldASP;
HR = oldH;
HB = oldHB;
if (arena == LOCAL_GlobalArena) if (arena == LOCAL_GlobalArena)
LOCAL_GlobalArenaOverflows++; LOCAL_GlobalArenaOverflows++;
if (!GrowArena(arena, old_top, old_size, Nar * sizeof(CELL), if ((arena=GrowArena(arena, old_top, old_size, Nar * sizeof(CELL),
arity + 2 PASS_REGS)) { arity + 2, &cells PASS_REGS))==0) {
Yap_Error(RESOURCE_ERROR_STACK, TermNil, Yap_Error(RESOURCE_ERROR_STACK, TermNil,
"while creating large global term"); "while creating large global term");
return 0L; return 0L;
} }
} }
oldH = HR;
oldHB = HB;
oldASP = ASP;
newarena = (CELL *)XREGS[arity + 2]; newarena = (CELL *)XREGS[arity + 2];
arena = Deref(XREGS[arity + 1]); arena = Deref(XREGS[arity + 1]);
old_size = ArenaSz(arena); old_size = ArenaSz(arena);
@ -872,7 +885,7 @@ restart:
HB0[i] = init; HB0[i] = init;
} }
} }
CloseArena(oldH, oldHB, oldASP, newarena, old_size PASS_REGS); CloseArena(&cells, newarena, old_size PASS_REGS);
return tf; return tf;
} }
@ -1534,9 +1547,10 @@ static Int p_nb_create2(USES_REGS1) {
static Int nb_queue(UInt arena_sz USES_REGS) { static Int nb_queue(UInt arena_sz USES_REGS) {
cell_space_t cspace;
Term queue_arena, queue, ar[QUEUE_FUNCTOR_ARITY], *nar; Term queue_arena, queue, ar[QUEUE_FUNCTOR_ARITY], *nar;
Term t = Deref(ARG1); Term t = Deref(ARG1);
enter_cell_space(&cspace);
LOCAL_DepthArenas++; LOCAL_DepthArenas++;
if (!IsVarTerm(t)) { if (!IsVarTerm(t)) {
if (!IsApplTerm(t)) { if (!IsApplTerm(t)) {
@ -1549,9 +1563,9 @@ static Int nb_queue(UInt arena_sz USES_REGS) {
queue = Yap_MkApplTerm(FunctorNBQueue, QUEUE_FUNCTOR_ARITY, ar); queue = Yap_MkApplTerm(FunctorNBQueue, QUEUE_FUNCTOR_ARITY, ar);
if (!Yap_unify(queue, ARG1)) if (!Yap_unify(queue, ARG1))
return FALSE; return FALSE;
if (arena_sz < 4 * 1024) if (arena_sz < 32 * 1024)
arena_sz = 4 * 1024; arena_sz = 32 * 1024;
queue_arena = NewArena(arena_sz, worker_id, 1, NULL); queue_arena = NewArena(arena_sz, worker_id, 1, NULL, &cspace);
if (queue_arena == 0L) { if (queue_arena == 0L) {
return FALSE; return FALSE;
} }
@ -1656,7 +1670,7 @@ static Int p_nb_queue_close(USES_REGS1) {
} }
static Int p_nb_queue_enqueue(USES_REGS1) { static Int p_nb_queue_enqueue(USES_REGS1) {
CELL *qd = GetQueue(ARG1, "enqueue"), *oldH, *oldHB; CELL *qd = GetQueue(ARG1, "enqueue");
UInt old_sz; UInt old_sz;
Term arena, qsize, to; Term arena, qsize, to;
UInt min_size; UInt min_size;
@ -1675,37 +1689,15 @@ static Int p_nb_queue_enqueue(USES_REGS1) {
min_size PASS_REGS); min_size PASS_REGS);
if (to == 0L) if (to == 0L)
return FALSE; return FALSE;
cell_space_t cspace;
qd = GetQueue(ARG1, "enqueue"); qd = GetQueue(ARG1, "enqueue");
arena = GetQueueArena(qd, "enqueue"); arena = GetQueueArena(qd, "enqueue");
/* garbage collection ? */ /* garbage collection ? */
oldH = HR; enter_cell_space(&cspace);
oldHB = HB;
HR = HB = ArenaPt(arena); HR = HB = ArenaPt(arena);
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
qsize = IntegerOfTerm(qd[QUEUE_SIZE]); qsize = IntegerOfTerm(qd[QUEUE_SIZE]);
while (old_sz < MIN_ARENA_SIZE) {
UInt gsiz = HR - RepPair(qd[QUEUE_HEAD]);
HR = oldH;
HB = oldHB;
if (gsiz > 1024 * 1024) {
gsiz = 1024 * 1024;
} else if (gsiz < 1024) {
gsiz = 1024;
}
ARG3 = to;
/* fprintf(stderr,"growing %ld cells\n",(unsigned long int)gsiz);*/
if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) {
Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage);
return 0L;
}
to = ARG3;
qd = RepAppl(Deref(ARG1)) + 1;
arena = GetQueueArena(qd, "enqueue");
oldH = HR;
oldHB = HB;
HR = HB = ArenaPt(arena);
old_sz = ArenaSz(arena);
}
qd[QUEUE_SIZE] = Global_MkIntegerTerm(qsize + 1); qd[QUEUE_SIZE] = Global_MkIntegerTerm(qsize + 1);
if (qsize == 0) { if (qsize == 0) {
qd[QUEUE_HEAD] = AbsPair(HR); qd[QUEUE_HEAD] = AbsPair(HR);
@ -1716,7 +1708,7 @@ static Int p_nb_queue_enqueue(USES_REGS1) {
RESET_VARIABLE(HR); RESET_VARIABLE(HR);
qd[QUEUE_TAIL] = (CELL)HR; qd[QUEUE_TAIL] = (CELL)HR;
HR++; HR++;
CloseArena(oldH, oldHB, ASP, qd + QUEUE_ARENA, old_sz PASS_REGS); CloseArena(&cspace, qd + QUEUE_ARENA, old_sz PASS_REGS);
return TRUE; return TRUE;
} }
@ -1724,7 +1716,7 @@ static Int p_nb_queue_dequeue(USES_REGS1) {
CELL *qd = GetQueue(ARG1, "dequeue"); CELL *qd = GetQueue(ARG1, "dequeue");
UInt old_sz, qsz; UInt old_sz, qsz;
Term arena, out; Term arena, out;
CELL *oldH, *oldHB; cell_space_t cspace;
if (!qd) if (!qd)
return FALSE; return FALSE;
@ -1738,10 +1730,9 @@ static Int p_nb_queue_dequeue(USES_REGS1) {
out = HeadOfTerm(qd[QUEUE_HEAD]); out = HeadOfTerm(qd[QUEUE_HEAD]);
qd[QUEUE_HEAD] = TailOfTerm(qd[QUEUE_HEAD]); qd[QUEUE_HEAD] = TailOfTerm(qd[QUEUE_HEAD]);
/* garbage collection ? */ /* garbage collection ? */
oldH = HR; enter_cell_space(&cspace);
oldHB = HB;
qd[QUEUE_SIZE] = Global_MkIntegerTerm(qsz - 1); qd[QUEUE_SIZE] = Global_MkIntegerTerm(qsz - 1);
CloseArena(oldH, oldHB, ASP, &arena, old_sz PASS_REGS); CloseArena(&cspace, &arena, old_sz PASS_REGS);
return Yap_unify(out, ARG2); return Yap_unify(out, ARG2);
} }
@ -1838,6 +1829,7 @@ static Term MkZeroApplTerm(Functor f, UInt sz USES_REGS) {
static Int p_nb_heap(USES_REGS1) { static Int p_nb_heap(USES_REGS1) {
Term heap_arena, heap, *ar, *nar; Term heap_arena, heap, *ar, *nar;
UInt hsize; UInt hsize;
cell_space_t cspace;
Term tsize = Deref(ARG1); Term tsize = Deref(ARG1);
UInt arena_sz = (ASP-HR) / 16; UInt arena_sz = (ASP-HR) / 16;
@ -1868,7 +1860,7 @@ static Int p_nb_heap(USES_REGS1) {
ar[HEAP_MAX] = tsize; ar[HEAP_MAX] = tsize;
if (arena_sz < 1024) if (arena_sz < 1024)
arena_sz = 1024; arena_sz = 1024;
heap_arena = NewArena(arena_sz, worker_id, 1, NULL); heap_arena = NewArena(arena_sz, worker_id, 1, NULL, &cspace);
if (heap_arena == 0L) { if (heap_arena == 0L) {
return FALSE; return FALSE;
} }
@ -1946,10 +1938,11 @@ static void DelHeapRoot(CELL *pt, UInt sz) {
} }
static Int p_nb_heap_add_to_heap(USES_REGS1) { static Int p_nb_heap_add_to_heap(USES_REGS1) {
CELL *qd = GetHeap(ARG1, "add_to_heap"), *oldH, *oldHB, *pt; CELL *qd = GetHeap(ARG1, "add_to_heap"), *pt;
UInt hsize, hmsize, old_sz; UInt hsize, hmsize, old_sz;
Term arena, to, key; Term arena, to, key;
UInt mingrow; UInt mingrow;
cell_space_t cspace;
if (!qd) if (!qd)
return FALSE; return FALSE;
@ -1971,7 +1964,8 @@ restart:
"No Stack Space for Non-Backtrackable terms"); "No Stack Space for Non-Backtrackable terms");
return FALSE; return FALSE;
} }
extra_size = extra_size / (2 * sizeof(CELL)); enter_cell_space(&cspace);
extra_size = extra_size / (2 * sizeof(CELL));
qd = GetHeap(ARG1, "add_to_heap"); qd = GetHeap(ARG1, "add_to_heap");
hmsize += extra_size; hmsize += extra_size;
if (!qd) if (!qd)
@ -1986,11 +1980,9 @@ restart:
} }
arena = qd[HEAP_ARENA]; arena = qd[HEAP_ARENA];
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
oldH = HR;
oldHB = HB;
HR = HB = ArenaPt(arena); HR = HB = ArenaPt(arena);
qd[HEAP_MAX] = Global_MkIntegerTerm(hmsize); qd[HEAP_MAX] = Global_MkIntegerTerm(hmsize);
CloseArena(oldH, oldHB, ASP, qd + HEAP_ARENA, old_sz PASS_REGS); CloseArena(&cspace, qd + HEAP_ARENA, old_sz PASS_REGS);
goto restart; goto restart;
} }
arena = qd[HEAP_ARENA]; arena = qd[HEAP_ARENA];
@ -2010,30 +2002,25 @@ restart:
qd = GetHeap(ARG1, "add_to_heap"); qd = GetHeap(ARG1, "add_to_heap");
arena = qd[HEAP_ARENA]; arena = qd[HEAP_ARENA];
/* garbage collection ? */ /* garbage collection ? */
oldH = HR; enter_cell_space(&cspace);
oldHB = HB;
HR = HB = ArenaPt(arena); HR = HB = ArenaPt(arena);
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
while (old_sz < MIN_ARENA_SIZE) { while (old_sz < MIN_ARENA_SIZE) {
UInt gsiz = hsize * 2; UInt gsiz = hsize * 2;
HR = oldH;
HB = oldHB;
if (gsiz > 1024 * 1024) { if (gsiz > 1024 * 1024) {
gsiz = 1024 * 1024; gsiz = 1024 * 1024;
} else if (gsiz < 1024) { } else if (gsiz < 1024) {
gsiz = 1024; gsiz = 1024;
} }
ARG3 = to; ARG3 = to;
if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) { if ((arena=GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3, &cspace PASS_REGS))==0) {
Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage);
return 0L; return 0L;
} }
to = ARG3; to = ARG3;
qd = RepAppl(Deref(ARG1)) + 1; qd = RepAppl(Deref(ARG1)) + 1;
arena = qd[HEAP_ARENA]; arena = qd[HEAP_ARENA];
oldH = HR;
oldHB = HB;
HR = HB = ArenaPt(arena); HR = HB = ArenaPt(arena);
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
} }
@ -2042,7 +2029,7 @@ restart:
pt[2 * hsize + 1] = to; pt[2 * hsize + 1] = to;
PushHeap(pt, hsize); PushHeap(pt, hsize);
qd[HEAP_SIZE] = Global_MkIntegerTerm(hsize + 1); qd[HEAP_SIZE] = Global_MkIntegerTerm(hsize + 1);
CloseArena(oldH, oldHB, ASP, qd + HEAP_ARENA, old_sz PASS_REGS); CloseArena(&cspace, qd + HEAP_ARENA, old_sz PASS_REGS);
return TRUE; return TRUE;
} }
@ -2100,6 +2087,7 @@ static Int p_nb_heap_size(USES_REGS1) {
} }
static Int p_nb_beam(USES_REGS1) { static Int p_nb_beam(USES_REGS1) {
cell_space_t cspace;
Term beam_arena, beam, *ar, *nar; Term beam_arena, beam, *ar, *nar;
UInt hsize; UInt hsize;
Term tsize = Deref(ARG1); Term tsize = Deref(ARG1);
@ -2130,7 +2118,7 @@ static Int p_nb_beam(USES_REGS1) {
ar[HEAP_MAX] = tsize; ar[HEAP_MAX] = tsize;
if (arena_sz < 1024) if (arena_sz < 1024)
arena_sz = 1024; arena_sz = 1024;
beam_arena = NewArena(arena_sz, worker_id, 1, NULL); beam_arena = NewArena(arena_sz, worker_id, 1, NULL, &cspace);
if (beam_arena == 0L) { if (beam_arena == 0L) {
return FALSE; return FALSE;
} }
@ -2339,10 +2327,11 @@ static Term DelBeamMin(CELL *pt, CELL *pt2, UInt sz) {
} }
static Int p_nb_beam_add_to_beam(USES_REGS1) { static Int p_nb_beam_add_to_beam(USES_REGS1) {
CELL *qd = GetHeap(ARG1, "add_to_beam"), *oldH, *oldHB, *pt; CELL *qd = GetHeap(ARG1, "add_to_beam"), *pt;
UInt hsize, hmsize, old_sz; UInt hsize, hmsize, old_sz;
Term arena, to, key; Term arena, to, qsize, key;
UInt mingrow; UInt mingrow;
cell_space_t cspace;
if (!qd) if (!qd)
return FALSE; return FALSE;
@ -2373,37 +2362,32 @@ static Int p_nb_beam_add_to_beam(USES_REGS1) {
qd = GetHeap(ARG1, "add_to_beam"); qd = GetHeap(ARG1, "add_to_beam");
arena = qd[HEAP_ARENA]; arena = qd[HEAP_ARENA];
/* garbage collection ? */ /* garbage collection ? */
oldH = HR; enter_cell_space(&cspace);
oldHB = HB;
HR = HB = ArenaPt(arena); HR = HB = ArenaPt(arena);
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
qsize = IntegerOfTerm(qd[QUEUE_SIZE]);
while (old_sz < MIN_ARENA_SIZE) { while (old_sz < MIN_ARENA_SIZE) {
UInt gsiz = hsize * 2; UInt gsiz = HR - RepPair(qd[QUEUE_HEAD]);
HR = oldH;
HB = oldHB;
if (gsiz > 1024 * 1024) { if (gsiz > 1024 * 1024) {
gsiz = 1024 * 1024; gsiz = 1024 * 1024;
} else if (gsiz < 1024) { } else if (gsiz < 1024) {
gsiz = 1024; gsiz = 1024;
} }
ARG3 = to; ARG3 = to;
if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3 PASS_REGS)) { /* fprintf(stderr,"growing %ld cells\n",(unsigned long int)gsiz);*/
if (!GrowArena(arena, ArenaLimit(arena), old_sz, gsiz, 3, &cspace PASS_REGS)) {
Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage); Yap_Error(RESOURCE_ERROR_STACK, arena, LOCAL_ErrorMessage);
return 0L; return 0L;
} }
to = ARG3; to = ARG3;
qd = RepAppl(Deref(ARG1)) + 1; qd = RepAppl(Deref(ARG1)) + 1;
arena = qd[HEAP_ARENA]; arena = qd[HEAP_ARENA];
oldH = HR;
oldHB = HB;
HR = HB = ArenaPt(arena);
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
} }
pt = qd + HEAP_START; pt = qd + HEAP_START;
PushBeam(pt, pt + 2 * hmsize, hsize, key, to); PushBeam(pt, pt + 2 * hmsize, hsize, key, to);
qd[HEAP_SIZE] = Global_MkIntegerTerm(hsize + 1); qd[HEAP_SIZE] = Global_MkIntegerTerm(hsize + 1);
CloseArena(oldH, oldHB, ASP, qd + HEAP_ARENA, old_sz PASS_REGS); CloseArena(&cspace, qd + HEAP_ARENA, old_sz PASS_REGS);
return TRUE; return TRUE;
} }
@ -2411,8 +2395,8 @@ static Int p_nb_beam_del(USES_REGS1) {
CELL *qd = GetHeap(ARG1, "debeam"); CELL *qd = GetHeap(ARG1, "debeam");
UInt old_sz, qsz; UInt old_sz, qsz;
Term arena; Term arena;
CELL *oldH, *oldHB;
Term tk, tv; Term tk, tv;
cell_space_t cspace;
if (!qd) if (!qd)
return FALSE; return FALSE;
@ -2424,10 +2408,9 @@ static Int p_nb_beam_del(USES_REGS1) {
return FALSE; return FALSE;
old_sz = ArenaSz(arena); old_sz = ArenaSz(arena);
/* garbage collection ? */ /* garbage collection ? */
oldH = HR; enter_cell_space(&cspace);
oldHB = HB;
qd[HEAP_SIZE] = Global_MkIntegerTerm(qsz - 1); qd[HEAP_SIZE] = Global_MkIntegerTerm(qsz - 1);
CloseArena(oldH, oldHB, ASP, &arena, old_sz PASS_REGS); CloseArena(&cspace, &arena, old_sz PASS_REGS);
tk = qd[HEAP_START]; tk = qd[HEAP_START];
tv = DelBeamMin(qd + HEAP_START, tv = DelBeamMin(qd + HEAP_START,
qd + (HEAP_START + 2 * IntegerOfTerm(qd[HEAP_MAX])), qsz); qd + (HEAP_START + 2 * IntegerOfTerm(qd[HEAP_MAX])), qsz);

113
C/grow.c
View File

@ -324,15 +324,15 @@ MoveGlobalWithHole( USES_REGS1 )
} }
static void static void
MoveHalfGlobal(CELL *OldPt USES_REGS) MoveHalfGlobal(CELL *OldPt, size_t request USES_REGS)
{ {
/* /*
* cpcellsd(To,From,NOfCells) - copy the cells downwards - in * cpcellsd(To,From,NOfCells) - copy the cells downwards - in
* absmi.asm * absmi.asm
*/ */
UInt diff = LOCAL_OldH-OldPt; UInt diff = LOCAL_OldH-OldPt;
CELL *NewPt = (CELL *)((char*)OldPt+LOCAL_GDiff);
CELL *IntPt = (CELL *)((char*)OldPt+LOCAL_GDiff0); CELL *IntPt = (CELL *)((char*)OldPt+LOCAL_GDiff0);
CELL *NewPt = IntPt+request/sizeof(CELL);
cpcellsd(NewPt, IntPt, diff); cpcellsd(NewPt, IntPt, diff);
} }
@ -393,7 +393,7 @@ AdjustTrail(bool adjusting_heap, bool thread_copying USES_REGS)
register CELL reg = TrailTerm(ptt-1); register CELL reg = TrailTerm(ptt-1);
#ifdef FROZEN_STACKS #ifdef FROZEN_STACKS
register CELL reg2 = TrailVal(ptt-1); register CELL reg2 = TrailVal(ptt-1);
#endif #endif
ptt--; ptt--;
if (IsVarTerm(reg)) { if (IsVarTerm(reg)) {
@ -436,6 +436,8 @@ fixPointerCells(CELL *pt, CELL *pt_bot, bool thread_copying USES_REGS)
{ {
while (pt > pt_bot) { while (pt > pt_bot) {
CELL reg = *--pt; CELL reg = *--pt;
// if (pt-pt_bot> 4300 && pt-pt_bot < 4500)
// printf("%d %d %lx\n", pt-pt_bot, pt-LOCAL_GSplit, reg);
if (IsVarTerm(reg)) { if (IsVarTerm(reg)) {
if (IsOldLocal(reg)) if (IsOldLocal(reg))
*pt = LocalAdjust(reg); *pt = LocalAdjust(reg);
@ -450,6 +452,8 @@ fixPointerCells(CELL *pt, CELL *pt_bot, bool thread_copying USES_REGS)
} else if (IsPairTerm(reg)) { } else if (IsPairTerm(reg)) {
*pt = AdjustPair(reg PASS_REGS); *pt = AdjustPair(reg PASS_REGS);
} }
// if (pt-pt_bot> 4300 && pt-pt_bot < 4500)
// printf("%lx\n", *pt);
} }
} }
@ -889,12 +893,14 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
char *omax = (char *)H0; char *omax = (char *)H0;
ADDR old_GlobalBase = LOCAL_GlobalBase; ADDR old_GlobalBase = LOCAL_GlobalBase;
UInt minimal_request = 0L; UInt minimal_request = 0L;
Int size = request; Int size = request/sizeof(CELL);
char vb_msg1 = '\0', *vb_msg2; char vb_msg1 = '\0', *vb_msg2;
bool do_grow = true; bool do_grow = true;
bool insert_in_delays = false;
printf("request=%ld at %p, shift %p, %ld <-> %ld\n",request, hsplit, ptr,hsplit-H0,HR-hsplit);
/* /*
request is the amount of memory we requested, in bytes; request is the amount of memory we requesd, in bytes;
base_move is the shift in global stacks we had to do base_move is the shift in global stacks we had to do
size is how much space we allocate: it's negative if we just expand size is how much space we allocate: it's negative if we just expand
the delay stack. the delay stack.
@ -902,55 +908,44 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
*/ */
if (hsplit) { if (hsplit) {
/* just a little bit of sanity checking */ if (hsplit < H0 ||
if (hsplit < H0 && hsplit > (CELL *)LOCAL_GlobalBase) {
insert_in_delays = TRUE;
/* expanding attributed variables */
if (omax - size > LOCAL_GlobalBase+4096*sizeof(CELL)) {
/* we can just ask for more room */
size = 0;
do_grow = FALSE;
}
} else if (hsplit < (CELL*)omax ||
hsplit > HR) hsplit > HR)
return FALSE; return false;
else if (hsplit == (CELL *)omax) if (hsplit == H0)
hsplit = NULL; hsplit = NULL;
if (size < 0 || if (hsplit == HR && Unsigned(HR)+request < Unsigned(ASP)-StackGap( PASS_REGS1 )) {
(Unsigned(HR)+size < Unsigned(ASP)-StackGap( PASS_REGS1 ) && return request;
hsplit > H0)) {
/* don't need to expand stacks */
insert_in_delays = FALSE;
do_grow = FALSE;
}
} else {
if (Unsigned(HR)+size < Unsigned(ASP)-CreepFlag) {
/* we can just ask for more room */
do_grow = FALSE;
} }
} }
if (size < 0 ||
(Unsigned(HR)+request < Unsigned(ASP-StackGap( PASS_REGS1 )))) {
do_grow = false;
}
if (do_grow) { if (do_grow) {
if (size < YAP_ALLOC_SIZE) if (request < YAP_ALLOC_SIZE)
size = YAP_ALLOC_SIZE; request = YAP_ALLOC_SIZE;
size = AdjustPageSize(size); request = AdjustPageSize(request);
} }
printf("grow=%d\n", do_grow);
/* adjust to a multiple of 256) */ /* adjust to a multiple of 256) */
LOCAL_ErrorMessage = NULL; LOCAL_ErrorMessage = NULL;
LOCAL_PrologMode |= GrowStackMode; LOCAL_PrologMode |= GrowStackMode;
start_growth_time = Yap_cputime(); start_growth_time = Yap_cputime();
if (do_grow) { if (do_grow) {
if (!GLOBAL_AllowGlobalExpansion) { if (!GLOBAL_AllowGlobalExpansion) {
LOCAL_ErrorMessage = "Global Stack crashed against Local Stack"; LOCAL_ErrorMessage = "Global Stack crashed against Local Stack";
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
return 0; return 0;
} }
if (!GLOBAL_AllowGlobalExpansion || !Yap_ExtendWorkSpace(size)) { if (!GLOBAL_AllowGlobalExpansion || !Yap_ExtendWorkSpace(request)) {
/* always fails when using malloc */ /* always fails when using malloc */
LOCAL_ErrorMessage = NULL; LOCAL_ErrorMessage = NULL;
size += AdjustPageSize(((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)+MinHeapGap); request += AdjustPageSize(((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)+MinHeapGap);
minimal_request = size; minimal_request = request;
size = Yap_ExtendWorkSpaceThroughHole(size); request = Yap_ExtendWorkSpaceThroughHole(request);
if (size < 0) { if (request < 0) {
LOCAL_ErrorMessage = "Global Stack crashed against Local Stack"; LOCAL_ErrorMessage = "Global Stack crashed against Local Stack";
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
return 0; return 0;
@ -958,15 +953,11 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
} }
} }
gc_verbose = Yap_is_gc_verbose(); gc_verbose = Yap_is_gc_verbose();
LOCAL_delay_overflows++;
if (gc_verbose) { if (gc_verbose) {
if (hsplit) { if (hsplit) {
if (hsplit > H0) { if (hsplit > H0) {
vb_msg1 = 'H'; vb_msg1 = 'H';
vb_msg2 = "Global Variable Space"; vb_msg2 = "Global Variable Space";
} else {
vb_msg1 = 'D';
vb_msg2 = "Global Variable Delay Space";
} }
} else { } else {
vb_msg1 = 'D'; vb_msg1 = 'D';
@ -976,8 +967,9 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
fprintf(stderr, "%% Worker Id %d:\n", worker_id); fprintf(stderr, "%% Worker Id %d:\n", worker_id);
#endif #endif
fprintf(stderr, "%% %cO %s Overflow %d\n", vb_msg1, vb_msg2, LOCAL_delay_overflows); fprintf(stderr, "%% %cO %s Overflow %d\n", vb_msg1, vb_msg2, LOCAL_delay_overflows);
fprintf(stderr, "%% %cO growing the stacks " UInt_FORMAT " bytes\n", vb_msg1, size); fprintf(stderr, "%% %cO growing the stacks " UInt_FORMAT " bytes\n", vb_msg1, request);
} }
printf("grow=%d %p\n", do_grow, ASP);
ASP -= 256; ASP -= 256;
YAPEnterCriticalSection(); YAPEnterCriticalSection();
/* we always shift the local and the stack by the same amount */ /* we always shift the local and the stack by the same amount */
@ -985,13 +977,13 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
/* we got over a hole */ /* we got over a hole */
if (minimal_request) { if (minimal_request) {
/* we went over a hole */ /* we went over a hole */
LOCAL_BaseDiff = size+((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)-minimal_request; LOCAL_BaseDiff = request+((CELL)LOCAL_TrailTop-(CELL)LOCAL_GlobalBase)-minimal_request;
LOCAL_LDiff = LOCAL_TrDiff = size; LOCAL_LDiff = LOCAL_TrDiff = request;
} else { } else {
/* we may still have an overflow */ /* we may still have an overflow */
LOCAL_BaseDiff = LOCAL_GlobalBase - old_GlobalBase; LOCAL_BaseDiff = LOCAL_GlobalBase - old_GlobalBase;
/* if we grow, we need to move the stacks */ /* if we grow, we need to move the stacks */
LOCAL_LDiff = LOCAL_TrDiff = LOCAL_BaseDiff+size; LOCAL_LDiff = LOCAL_TrDiff = LOCAL_BaseDiff+request;
} }
} else { } else {
/* stay still */ /* stay still */
@ -1002,25 +994,21 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
hole in global */ hole in global */
if (!hsplit) { if (!hsplit) {
if (!do_grow) { if (!do_grow) {
LOCAL_DelayDiff = LOCAL_GDiff = LOCAL_GDiff0 = size; LOCAL_GDiff = LOCAL_GDiff0 = request;
request = 0L; request = 0L;
} else { } else {
/* expand delay stack */
LOCAL_DelayDiff = LOCAL_GDiff = LOCAL_GDiff0 = LOCAL_LDiff;
}
} else if (insert_in_delays) {
/* we want to expand a hole for the delay stack */ /* we want to expand a hole for the delay stack */
LOCAL_DelayDiff = size-request; LOCAL_GDiff = LOCAL_GDiff0 = LOCAL_LDiff;
LOCAL_GDiff = LOCAL_GDiff0 = size; }
} else { } else {
/* we want to expand a hole for the delay stack */ /* we want to expand a hole for the delay stack */
LOCAL_GDiff0 = LOCAL_DelayDiff = LOCAL_BaseDiff; LOCAL_GDiff0 = LOCAL_BaseDiff;
LOCAL_GDiff = LOCAL_BaseDiff+request; LOCAL_GDiff = LOCAL_BaseDiff+request;
} }
LOCAL_GSplit = hsplit; LOCAL_GSplit = hsplit;
LOCAL_XDiff = LOCAL_HDiff = 0; LOCAL_XDiff = LOCAL_HDiff = 0;
LOCAL_GlobalBase = old_GlobalBase; LOCAL_GlobalBase = old_GlobalBase;
SetHeapRegs(FALSE PASS_REGS); SetHeapRegs(FALSE PASS_REGS);
if (do_grow) { if (do_grow) {
MoveLocalAndTrail( PASS_REGS1 ); MoveLocalAndTrail( PASS_REGS1 );
if (hsplit) { if (hsplit) {
@ -1042,17 +1030,13 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
*ptr = PtoLocAdjust(*ptr); *ptr = PtoLocAdjust(*ptr);
} }
if (hsplit) { if (hsplit) {
if (insert_in_delays) { MoveHalfGlobal(hsplit, request PASS_REGS);
/* we have things not quite where we want to have them */ printf("done\n");
cpcellsd((CELL *)(omax+LOCAL_DelayDiff), (CELL *)(omax+LOCAL_GDiff0), (ADDR)hsplit-omax);
} else {
MoveHalfGlobal(hsplit PASS_REGS);
}
} }
YAPLeaveCriticalSection(); YAPLeaveCriticalSection();
ASP += 256; ASP += 256;
if (minimal_request) { if (minimal_request) {
Yap_AllocHole(minimal_request, size); Yap_AllocHole(minimal_request, request);
} }
growth_time = Yap_cputime()-start_growth_time; growth_time = Yap_cputime()-start_growth_time;
LOCAL_total_delay_overflow_time += growth_time; LOCAL_total_delay_overflow_time += growth_time;
@ -1063,8 +1047,9 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS)
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
if (hsplit) { if (hsplit) {
return request; return request;
} else } else {
return LOCAL_GDiff-LOCAL_BaseDiff; return LOCAL_GDiff-LOCAL_BaseDiff;
}
} }
static void static void
@ -1503,7 +1488,7 @@ Yap_growheap(bool fix_code, size_t in_size, void *cip)
int int
Yap_growheap_in_parser(tr_fr_ptr *old_trp, TokEntry **tksp, VarEntry **vep) Yap_growheap_in_parser(tr_fr_ptr *old_trp, TokEntry **tksp, VarEntry **vep)
{ {
CACHE_REGS CACHE_REGS
int res; int res;
res=do_growheap(FALSE, 0L, NULL, old_trp, tksp, vep PASS_REGS); res=do_growheap(FALSE, 0L, NULL, old_trp, tksp, vep PASS_REGS);
@ -1548,6 +1533,7 @@ UInt
Yap_InsertInGlobal(CELL *where, size_t howmuch) Yap_InsertInGlobal(CELL *where, size_t howmuch)
{ {
CACHE_REGS CACHE_REGS
printf("where=%p, hm=%d\n",where,howmuch);
if ((howmuch = static_growglobal(howmuch, NULL, where PASS_REGS)) == 0) if ((howmuch = static_growglobal(howmuch, NULL, where PASS_REGS)) == 0)
return 0; return 0;
#ifdef TABLING #ifdef TABLING
@ -1576,6 +1562,7 @@ Yap_growstack(size_t size)
int res; int res;
LOCAL_PrologMode |= GrowStackMode; LOCAL_PrologMode |= GrowStackMode;
printf("extra %dBs\n",size);
res=growstack(size PASS_REGS); res=growstack(size PASS_REGS);
LeaveGrowMode(GrowStackMode); LeaveGrowMode(GrowStackMode);
return res; return res;

View File

@ -1,4 +1,4 @@
/************************************************************************** /*************************************************************************
* * * *
* YAP Prolog * * YAP Prolog *
* * * *
@ -4194,7 +4194,7 @@ call_gc(UInt gc_lim, Int predarity, CELL *current_env, yamop *nextop USES_REGS)
int gc_on = FALSE, gc_t = FALSE; int gc_on = FALSE, gc_t = FALSE;
if (Yap_GetValue(AtomGc) != TermNil) if (Yap_GetValue(AtomGc) != TermNil)
gc_on = TRUE; gc_on = false;
if (IsIntegerTerm(Tgc_margin = Yap_GetValue(AtomGcMargin)) && if (IsIntegerTerm(Tgc_margin = Yap_GetValue(AtomGcMargin)) &&
gc_margin > 0) { gc_margin > 0) {
gc_margin = (UInt)IntegerOfTerm(Tgc_margin); gc_margin = (UInt)IntegerOfTerm(Tgc_margin);
@ -4346,3 +4346,4 @@ Yap_inc_mark_variable()
CACHE_REGS CACHE_REGS
LOCAL_total_marked++; LOCAL_total_marked++;
} }

View File

@ -2039,7 +2039,7 @@ static UInt suspend_indexing(ClauseDef *min, ClauseDef *max, PredEntry *ap,
/* give it some slack */ /* give it some slack */
tels = cls + 4; tels = cls + 4;
} else { } else {
tels = cls; tels = cls+1;
} }
sz = (UInt)NEXTOP((yamop *)NULL, sssllp) + tels * sizeof(yamop *); sz = (UInt)NEXTOP((yamop *)NULL, sssllp) + tels * sizeof(yamop *);
if ((ncode = (yamop *)Yap_AllocCodeSpace(sz)) == NULL) { if ((ncode = (yamop *)Yap_AllocCodeSpace(sz)) == NULL) {

View File

@ -1949,11 +1949,12 @@
Op(p_arg_vv, xxx); Op(p_arg_vv, xxx);
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
HR[0] = XREG(PREG->y_u.xxx.x1); Term ts[3];
HR[1] = XREG(PREG->y_u.xxx.x2); ts[0] = XREG(PREG->y_u.xxx.x1);
RESET_VARIABLE(HR + 2); ts[1] = XREG(PREG->y_u.xxx.x2);
RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), HR); RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
BEGD(d0); BEGD(d0);
@ -2045,12 +2046,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
CELL *Ho = HR; CELL *Ho = HR;
Term ts[3];
Term t = MkIntegerTerm(PREG->y_u.xxn.c); Term t = MkIntegerTerm(PREG->y_u.xxn.c);
HR[0] = t; ts[0] = t;
HR[1] = XREG(PREG->y_u.xxn.xi); ts[1] = XREG(PREG->y_u.xxn.xi);
RESET_VARIABLE(HR + 2); RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), HR); RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), ts);
HR = Ho; HR = Ho;
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
@ -2118,12 +2120,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
Op(p_arg_y_vv, yxx); Op(p_arg_y_vv, yxx);
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
HR[0] = XREG(PREG->y_u.yxx.x1); Term ts[3];
HR[1] = XREG(PREG->y_u.yxx.x2); ts[0] = XREG(PREG->y_u.yxx.x1);
HR[2] = YREG[PREG->y_u.yxx.y]; ts[1] = XREG(PREG->y_u.yxx.x2);
RESET_VARIABLE(HR + 2); ts[2] = YREG[PREG->y_u.yxx.y];
RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), HR); RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
BEGD(d0); BEGD(d0);
@ -2216,13 +2219,14 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
CELL *Ho = HR; CELL *Ho = HR;
Term ts[3];
Term t = MkIntegerTerm(PREG->y_u.yxn.c); Term t = MkIntegerTerm(PREG->y_u.yxn.c);
HR[0] = t; ts[0] = t;
HR[1] = XREG(PREG->y_u.yxn.xi); ts[1] = XREG(PREG->y_u.yxn.xi);
HR[2] = YREG[PREG->y_u.yxn.y]; ts[2] = YREG[PREG->y_u.yxn.y];
RESET_VARIABLE(HR + 2); RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), HR); RepPredProp(Yap_GetPredPropByFunc(FunctorArg, 0)), ts);
HR = Ho; HR = Ho;
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
@ -2295,12 +2299,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
restart_func2s: restart_func2s:
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
RESET_VARIABLE(HR); Term ts[3];
HR[1] = XREG(PREG->y_u.xxx.x1); RESET_VARIABLE(ts);
HR[2] = XREG(PREG->y_u.xxx.x2); ts[1] = XREG(PREG->y_u.xxx.x1);
ts[2] = XREG(PREG->y_u.xxx.x2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
/* We have to build the structure */ /* We have to build the structure */
@ -2412,12 +2417,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
restart_func2s_cv: restart_func2s_cv:
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
RESET_VARIABLE(HR); Term ts[3];
HR[1] = PREG->y_u.xxc.c; RESET_VARIABLE(ts);
HR[2] = XREG(PREG->y_u.xxc.xi); ts[1] = PREG->y_u.xxc.c;
ts[2] = XREG(PREG->y_u.xxc.xi);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
BEGD(d0); BEGD(d0);
@ -2517,15 +2523,16 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
Term ti; Term ti;
Term ts[3];
CELL *hi = HR; CELL *hi = HR;
ti = MkIntegerTerm(PREG->y_u.xxn.c); ti = MkIntegerTerm(PREG->y_u.xxn.c);
RESET_VARIABLE(HR); RESET_VARIABLE(ts);
HR[1] = XREG(PREG->y_u.xxn.xi); ts[1] = XREG(PREG->y_u.xxn.xi);
HR[2] = ti; ts[2] = ti;
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
HR = hi; HR = hi;
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
@ -2611,12 +2618,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
restart_func2s_y: restart_func2s_y:
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
RESET_VARIABLE(HR); Term ts[3];
HR[1] = XREG(PREG->y_u.yxx.x1); RESET_VARIABLE(ts);
HR[2] = XREG(PREG->y_u.yxx.x2); ts[1] = XREG(PREG->y_u.yxx.x1);
ts[2] = XREG(PREG->y_u.yxx.x2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
/* We have to build the structure */ /* We have to build the structure */
@ -2735,12 +2743,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
restart_func2s_y_cv: restart_func2s_y_cv:
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
RESET_VARIABLE(HR); Term ts[3];
HR[1] = PREG->y_u.yxc.c; RESET_VARIABLE(ts);
HR[2] = XREG(PREG->y_u.yxc.xi); ts[1] = PREG->y_u.yxc.c;
ts[2] = XREG(PREG->y_u.yxc.xi);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
/* We have to build the structure */ /* We have to build the structure */
@ -2847,14 +2856,15 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
Term ti; Term ti;
CELL *hi = HR; CELL *hi = HR;
Term ts[3];
ti = MkIntegerTerm((Int)(PREG->y_u.yxn.c)); ti = MkIntegerTerm((Int)(PREG->y_u.yxn.c));
RESET_VARIABLE(HR); RESET_VARIABLE(ts);
HR[1] = XREG(PREG->y_u.yxn.xi); ts[1] = XREG(PREG->y_u.yxn.xi);
HR[2] = ti; ts[2] = ti;
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
HR = hi; HR = hi;
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
@ -2952,12 +2962,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
Op(p_func2f_xx, xxx); Op(p_func2f_xx, xxx);
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
HR[0] = XREG(PREG->y_u.xxx.x); Term ts[3];
RESET_VARIABLE(HR + 1); ts[0] = XREG(PREG->y_u.xxx.x);
RESET_VARIABLE(HR + 2); RESET_VARIABLE(ts + 1);
RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
BEGD(d0); BEGD(d0);
@ -3000,12 +3011,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
Op(p_func2f_xy, xxy); Op(p_func2f_xy, xxy);
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
HR[0] = XREG(PREG->y_u.xxy.x); Term ts[3];
RESET_VARIABLE(HR + 1); ts[0] = XREG(PREG->y_u.xxy.x);
RESET_VARIABLE(HR + 2); RESET_VARIABLE(ts + 1);
RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
BEGD(d0); BEGD(d0);
@ -3051,12 +3063,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
Op(p_func2f_yx, yxx); Op(p_func2f_yx, yxx);
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
HR[0] = XREG(PREG->y_u.yxx.x2); Term ts[3];
RESET_VARIABLE(HR + 1); ts[0] = XREG(PREG->y_u.yxx.x2);
RESET_VARIABLE(HR + 2); RESET_VARIABLE(ts + 1);
RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
BEGD(d0); BEGD(d0);
@ -3102,12 +3115,13 @@ Yap_AsmError( DOMAIN_ERROR_NOT_LESS_THAN_ZERO );
Op(p_func2f_yy, yyx); Op(p_func2f_yy, yyx);
#ifdef LOW_LEVEL_TRACER #ifdef LOW_LEVEL_TRACER
if (Yap_do_low_level_trace) { if (Yap_do_low_level_trace) {
HR[0] = XREG(PREG->y_u.yyx.x); Term ts[3];
RESET_VARIABLE(HR + 1); ts[0] = XREG(PREG->y_u.yyx.x);
RESET_VARIABLE(HR + 2); RESET_VARIABLE(ts + 1);
RESET_VARIABLE(ts + 2);
low_level_trace(enter_pred, low_level_trace(enter_pred,
RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)), RepPredProp(Yap_GetPredPropByFunc(FunctorFunctor, 0)),
HR); ts);
} }
#endif /* LOW_LEVEL_TRACE */ #endif /* LOW_LEVEL_TRACE */
BEGD(d0); BEGD(d0);

View File

@ -1319,6 +1319,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
TokEntry *t, *l, *p; TokEntry *t, *l, *p;
enum TokenKinds kind; enum TokenKinds kind;
int solo_flag = TRUE; int solo_flag = TRUE;
int lvl = push_text_stack();
int32_t ch, och = ' '; int32_t ch, och = ' ';
struct qq_struct_t *cur_qq = NULL; struct qq_struct_t *cur_qq = NULL;
int sign = 1; int sign = 1;
@ -1348,6 +1349,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); t = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
t->TokNext = NULL; t->TokNext = NULL;
if (t == NULL) { if (t == NULL) {
pop_text_stack(lvl);
return TrailSpaceError(p, l); return TrailSpaceError(p, l);
} }
if (!l) if (!l)
@ -1418,7 +1420,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE); imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE);
TokImage = Realloc(p0, imgsz); TokImage = Realloc(p0, imgsz);
if (TokImage == NULL) { if (TokImage == NULL) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
charp =(unsigned char *) TokImage + (charp - p0); charp =(unsigned char *) TokImage + (charp - p0);
} }
@ -1427,7 +1430,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
while (ch == '\'' && isvar && while (ch == '\'' && isvar &&
trueGlobalPrologFlag(VARIABLE_NAMES_MAY_END_WITH_QUOTES_FLAG)) { trueGlobalPrologFlag(VARIABLE_NAMES_MAY_END_WITH_QUOTES_FLAG)) {
if (charp == (unsigned char *)AuxSp - 1024) { if (charp == (unsigned char *)AuxSp - 1024) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
add_ch_to_buff(ch); add_ch_to_buff(ch);
ch = getchr(st); ch = getchr(st);
@ -1438,7 +1442,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
/* don't do this in iso */ /* don't do this in iso */
ae = Yap_LookupAtom(TokImage); ae = Yap_LookupAtom(TokImage);
if (ae == NIL) { if (ae == NIL) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
t->TokInfo = MkAtomTerm(ae); t->TokInfo = MkAtomTerm(ae);
if (ch == '(') if (ch == '(')
@ -1470,7 +1475,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->TokInfo = TermError; t->TokInfo = TermError;
} }
/* serious error now */ /* serious error now */
return l; pop_text_stack(lvl);
return l;
} }
ch = cha; ch = cha;
if (cherr) { if (cherr) {
@ -1480,7 +1486,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->TokLine = GetCurInpLine(st); t->TokLine = GetCurInpLine(st);
e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e == NULL) { if (e == NULL) {
return TrailSpaceError(p, l); pop_text_stack(lvl);
return TrailSpaceError(p, l);
} else { } else {
e->TokNext = NULL; e->TokNext = NULL;
@ -1506,7 +1513,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->TokLine = GetCurInpLine(st); t->TokLine = GetCurInpLine(st);
e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e2 == NULL) { if (e2 == NULL) {
return TrailSpaceError(p, l); pop_text_stack(lvl);
return TrailSpaceError(p, l);
} else { } else {
e2->TokNext = NULL; e2->TokNext = NULL;
} }
@ -1540,7 +1548,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->TokPos = GetCurInpPos(st); t->TokPos = GetCurInpPos(st);
e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); e2 = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e2 == NULL) { if (e2 == NULL) {
return TrailSpaceError(p, l); pop_text_stack(lvl);
return TrailSpaceError(p, l);
} else { } else {
e2->TokNext = NULL; e2->TokNext = NULL;
} }
@ -1570,7 +1579,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
size_t sz = charp-(unsigned char *)TokImage; size_t sz = charp-(unsigned char *)TokImage;
TokImage = Realloc(TokImage, (imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE))); TokImage = Realloc(TokImage, (imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE)));
if (TokImage == NULL) { if (TokImage == NULL) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
charp = (unsigned char *)TokImage+sz; charp = (unsigned char *)TokImage+sz;
break; break;
@ -1609,20 +1619,23 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->TokInfo = Yap_CharsToTDQ((char *)TokImage, CurrentModule, t->TokInfo = Yap_CharsToTDQ((char *)TokImage, CurrentModule,
LOCAL_encoding PASS_REGS); LOCAL_encoding PASS_REGS);
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
t->Tok = Ord(kind = String_tok); t->Tok = Ord(kind = String_tok);
} else if (quote == '`') { } else if (quote == '`') {
t->TokInfo = Yap_CharsToTBQ((char *)TokImage, CurrentModule, t->TokInfo = Yap_CharsToTBQ((char *)TokImage, CurrentModule,
LOCAL_encoding PASS_REGS); LOCAL_encoding PASS_REGS);
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
t->Tok = Ord(kind = String_tok); t->Tok = Ord(kind = String_tok);
} else { } else {
t->TokInfo = MkAtomTerm(Yap_LookupAtom(TokImage)); t->TokInfo = MkAtomTerm(Yap_LookupAtom(TokImage));
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
if (ch == '(') if (ch == '(')
@ -1642,7 +1655,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
} }
t->TokInfo = TermEof; t->TokInfo = TermEof;
return l; pop_text_stack(lvl);
return l;
} else } else
ch = getchr(st); ch = getchr(st);
break; break;
@ -1656,9 +1670,11 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
// consume... // consume...
if (pch == '%') { if (pch == '%') {
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
return l; pop_text_stack(lvl);
return l;
} }
return l; pop_text_stack(lvl);
return l;
} }
if (ch == '`') if (ch == '`')
goto quoted_string; goto quoted_string;
@ -1669,7 +1685,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
if (ch == '%') { if (ch == '%') {
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
return l; pop_text_stack(lvl);
return l;
} }
if (chtype(ch) == EF) { if (chtype(ch) == EF) {
mark_eof(st); mark_eof(st);
@ -1677,7 +1694,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
} else { } else {
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
} }
return l; pop_text_stack(lvl);
return l;
} }
} }
if (och == '/' && ch == '*') { if (och == '/' && ch == '*') {
@ -1726,7 +1744,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
if (ch == '%') { if (ch == '%') {
t->TokInfo = TermNewLine; t->TokInfo = TermNewLine;
return l; pop_text_stack(lvl);
return l;
} }
if (chtype(ch) == EF) { if (chtype(ch) == EF) {
mark_eof(st); mark_eof(st);
@ -1734,7 +1753,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
} else { } else {
t->TokInfo = TermNl; t->TokInfo = TermNl;
} }
return l; pop_text_stack(lvl);
return l;
} else { } else {
Atom ae; Atom ae;
charp = (unsigned char *)TokImage; charp = (unsigned char *)TokImage;
@ -1744,8 +1764,10 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
size_t sz = charp - (unsigned char *)TokImage; size_t sz = charp - (unsigned char *)TokImage;
imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE); imgsz = Yap_Min(imgsz * 2, imgsz + MBYTE);
TokImage = Realloc(TokImage, imgsz); TokImage = Realloc(TokImage, imgsz);
if (!TokImage) if (!TokImage) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
}
charp = (unsigned char *)TokImage+sz; charp = (unsigned char *)TokImage+sz;
} }
add_ch_to_buff(ch); add_ch_to_buff(ch);
@ -1753,11 +1775,13 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
add_ch_to_buff('\0'); add_ch_to_buff('\0');
ae = Yap_LookupAtom(TokImage); ae = Yap_LookupAtom(TokImage);
if (ae == NIL) { if (ae == NIL) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
t->TokInfo = MkAtomTerm(ae); t->TokInfo = MkAtomTerm(ae);
if (t->TokInfo == (CELL)NIL) { if (t->TokInfo == (CELL)NIL) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
t->Tok = Ord(kind = Name_tok); t->Tok = Ord(kind = Name_tok);
if (ch == '(') if (ch == '(')
@ -1820,15 +1844,16 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
LOCAL_ErrorMessage = "not enough heap space to read in quasi quote"; LOCAL_ErrorMessage = "not enough heap space to read in quasi quote";
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError; t->TokInfo = TermOutOfHeapError;
return l; pop_text_stack(lvl);
return l;
} }
if (cur_qq) { if (cur_qq) {
LOCAL_ErrorMessage = "quasi quote in quasi quote"; LOCAL_ErrorMessage = "quasi quote in quasi quote";
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError; t->TokInfo = TermOutOfHeapError;
Free(qq); pop_text_stack(lvl);
return l; return l;
} else { } else {
cur_qq = qq; cur_qq = qq;
} }
@ -1865,7 +1890,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
cur_qq = NULL; cur_qq = NULL;
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermError; t->TokInfo = TermError;
return l; pop_text_stack(lvl);
return l;
} }
cur_qq = NULL; cur_qq = NULL;
t->TokInfo = (CELL)qq; t->TokInfo = (CELL)qq;
@ -1910,7 +1936,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
LOCAL_ErrorMessage = "not enough heap space to read in quasi quote"; LOCAL_ErrorMessage = "not enough heap space to read in quasi quote";
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermOutOfHeapError; t->TokInfo = TermOutOfHeapError;
return l; pop_text_stack(lvl);
return l;
} }
strncpy((char *)mp, (const char *)TokImage, len + 1); strncpy((char *)mp, (const char *)TokImage, len + 1);
qq->text = (unsigned char *)mp; qq->text = (unsigned char *)mp;
@ -1923,7 +1950,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
qq->end.linepos = st->linepos - 1; qq->end.linepos = st->linepos - 1;
qq->end.charno = st->charcount - 1; qq->end.charno = st->charcount - 1;
if (!(t->TokInfo)) { if (!(t->TokInfo)) {
return CodeSpaceError(t, p, l); pop_text_stack(lvl);
return CodeSpaceError(t, p, l);
} }
Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage); Yap_ReleasePreAllocCodeSpace((CODEADDR)TokImage);
solo_flag = FALSE; solo_flag = FALSE;
@ -1936,7 +1964,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
mark_eof(st); mark_eof(st);
t->Tok = Ord(kind = eot_tok); t->Tok = Ord(kind = eot_tok);
t->TokInfo = TermEof; t->TokInfo = TermEof;
return l; pop_text_stack(lvl);
return l;
default: { default: {
kind = Error_tok; kind = Error_tok;
@ -1951,7 +1980,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
/* insert an error token to inform the system of what happened */ /* insert an error token to inform the system of what happened */
TokEntry *e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); TokEntry *e = (TokEntry *)AllocScannerMemory(sizeof(TokEntry));
if (e == NULL) { if (e == NULL) {
return TrailSpaceError(p, l); pop_text_stack(lvl);
return TrailSpaceError(p, l);
} }
p->TokNext = e; p->TokNext = e;
e->Tok = Error_tok; e->Tok = Error_tok;
@ -1963,6 +1993,7 @@ TokEntry *Yap_tokenizer(struct stream_desc *st, bool store_comments,
p = e; p = e;
} }
} while (kind != eot_tok); } while (kind != eot_tok);
pop_text_stack(lvl);
return (l); return (l);
} }

View File

@ -411,6 +411,23 @@ std::vector<Term> YAPPairTerm::listToArray() {
return o; return o;
} }
std::vector<YAPTerm> YAPPairTerm::listToVector() {
Term *tailp;
Term t1 = gt();
Int l = Yap_SkipList(&t1, &tailp);
if (l < 0) {
throw YAPError(SOURCE(), TYPE_ERROR_LIST, (t), nullptr);
}
std::vector<YAPTerm> o = *new std::vector<YAPTerm>(l);
int i = 0;
Term t = gt();
while (t != TermNil) {
o[i++] = YAPTerm(HeadOfTerm(t));
t = TailOfTerm(t);
}
return o;
}
YAP_tag_t YAPTerm::tag() { YAP_tag_t YAPTerm::tag() {
Term tt = gt(); Term tt = gt();
if (IsVarTerm(tt)) { if (IsVarTerm(tt)) {
@ -569,18 +586,18 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
q.p = P; q.p = P;
q.cp = CP; q.cp = CP;
q.b0 = LCL0-CellPtr(B);
q.env0 = LCL0-ENV;
for (arity_t i = 0; i < arity; i++) for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i].term(); XREGS[i + 1] = ts[i].term();
// allow Prolog style exceotion handling // allow Prolog style exceotion handling
// don't forget, on success these bindings will still be there); // don't forget, on success these bindings will still be there);
result = YAP_LeaveGoal(true, &q); result = YAP_EnterGoal(ap.ap, nullptr, &q);
YAP_LeaveGoal(result, &q);
YAPCatchError(); YAPCatchError();
Yap_CloseHandles(q.CurSlot);
pop_text_stack(q.lvl + 1);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return result; return result;
} }
@ -594,11 +611,14 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
// _save = PyEval_SaveThread(); // _save = PyEval_SaveThread();
#endif #endif
CACHE_REGS CACHE_REGS
YAP_dogoalinfo q;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term *ts = nullptr; Term *ts = nullptr;
q.CurSlot = Yap_StartSlots(); q.CurSlot = Yap_StartSlots();
q.p = P; q.p = P;
q.cp = CP; q.cp = CP;
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
Term omod = CurrentModule; Term omod = CurrentModule;
PredEntry *ap = nullptr; PredEntry *ap = nullptr;
if (IsStringTerm(tmod)) if (IsStringTerm(tmod))
@ -625,9 +645,10 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
//__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec "); //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
result = (bool)YAP_EnterGoal(ap, nullptr, &q); result = (bool)YAP_EnterGoal(ap, nullptr, &q);
// std::cerr << "mgoal " << YAPTerm(tmod).text() << ":" << YAPTerm(t).text() << "\n"; // std::cerr << "mgoal " << YAPTerm(tmod).text() << ":" << YAPTerm(t).text() << "\n
YAP_LeaveGoal(result && !release, &q); YAP_LeaveGoal(result && !release, &q);
ENV = LCL0-oenv;
B = (choiceptr)(LCL0-oB);
CurrentModule = LOCAL_SourceModule = omod; CurrentModule = LOCAL_SourceModule = omod;
// PyEval_RestoreThread(_save); // PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
@ -640,19 +661,25 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
void YAPEngine::release() { void YAPEngine::release() {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
YAP_LeaveGoal(FALSE, &q); // YAP_LeaveGoal(FALSE, &q);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
Term YAPEngine::fun(Term t) { Term YAPEngine::fun(Term t) {
CACHE_REGS CACHE_REGS
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
YAP_dogoalinfo q;
Term tmod = Yap_CurrentModule(), *ts = nullptr; Term tmod = Yap_CurrentModule(), *ts = nullptr;
PredEntry *ap; PredEntry *ap;
arity_t arity; arity_t arity;
Functor f; Functor f;
Atom name; Atom name;
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
Int oenv = LCL0-ENV;
Int oB = LCL0-CellPtr(B);
if (IsApplTerm(t)) { if (IsApplTerm(t)) {
ts = RepAppl(t) + 1; ts = RepAppl(t) + 1;
f = (Functor)ts[-1]; f = (Functor)ts[-1];
@ -684,9 +711,6 @@ Term YAPEngine::fun(Term t) {
Term g = (Yap_MkApplTerm(f, arity, ts)); Term g = (Yap_MkApplTerm(f, arity, ts));
ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred)); ap = rewriteUndefEngineQuery(ap, g, (ap->ModuleOfPred));
} }
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
// make sure this is safe // make sure this is safe
// allow Prolog style exception handling // allow Prolog style exception handling
//__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec "); //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec ");
@ -699,6 +723,8 @@ Term YAPEngine::fun(Term t) {
YAPCatchError(); YAPCatchError();
{ {
YAP_LeaveGoal(result, &q); YAP_LeaveGoal(result, &q);
ENV = LCL0-oenv;
B = (choiceptr)(LCL0-oB);
// PyEval_RestoreThread(_save); // PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return ot; return ot;
@ -727,6 +753,27 @@ YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm ts[])
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, Term ts[])
: YAPPredicate(f, mod) {
/* ignore flags for now */
BACKUP_MACHINE_REGS();
Term goal;
if (ts) {
size_t arity = f.arity();
goal = Yap_MkApplTerm(Yap_MkFunctor(f.name().asAtom(),arity), arity, ts);
nts = RepAppl(goal) + 1;
for (arity_t i = 0; i < arity; i++)
XREGS[i + 1] = ts[i];
} else {
goal = MkVarTerm();
}
openQuery();
names = YAPPairTerm(TermNil);
RECOVER_MACHINE_REGS();
}
#if 0 #if 0
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) { YAPQuery::YAPQuery(YAPFunctor f, YAPTerm ts[]) : YAPPredicate(f) {
/* ignore flags for now */ /* ignore flags for now */
@ -766,6 +813,9 @@ bool YAPQuery::next() {
CACHE_REGS CACHE_REGS
bool result = false; bool result = false;
// std::cerr << "next " << YAPTerm(goal).text() << "\n"; // std::cerr << "next " << YAPTerm(goal).text() << "\n";
q_h.CurSlot = Yap_StartSlots();
q_h.p = P;
q_h.cp = CP;
sigjmp_buf buf, *oldp = LOCAL_RestartEnv; sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
e = nullptr; e = nullptr;
@ -827,7 +877,7 @@ bool YAPQuery::deterministic() {
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (!q_open || q_state == 0) if (!q_open || q_state == 0)
return false; return false;
choiceptr myB = (choiceptr)(LCL0 - q_h.b); choiceptr myB = (choiceptr)(LCL0 - q_h.b_entry);
return (B >= myB); return (B >= myB);
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
@ -1093,13 +1143,15 @@ std::stringstream s;
void YAPEngine::reSet() { void YAPEngine::reSet() {
/* ignore flags for now */ /* ignore flags for now */
if (B && B->cp_b && B->cp_ap != NOCODE) if (B && B->cp_b && B->cp_ap != NOCODE)
YAP_LeaveGoal(false, &q); // YAP_LeaveGoal(false, &q);
LOCAL_ActiveError->errorNo = YAP_NO_ERROR; LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
if (LOCAL_CommittedError) { if (LOCAL_CommittedError) {
LOCAL_CommittedError->errorNo = YAP_NO_ERROR; LOCAL_CommittedError->errorNo = YAP_NO_ERROR;
free(LOCAL_CommittedError); free(LOCAL_CommittedError);
LOCAL_CommittedError = NULL; LOCAL_CommittedError = NULL;
} }
pop_text_stack(0);
LOCAL_CurSlot = 0;
} }
Term YAPEngine::top_level(std::string s) { Term YAPEngine::top_level(std::string s) {

View File

@ -39,7 +39,6 @@ class X_API YAPQuery : public YAPPredicate {
bool q_open; bool q_open;
int q_state; int q_state;
yhandle_t q_handles; yhandle_t q_handles;
struct yami *q_p, *q_cp;
int q_flags; int q_flags;
YAP_dogoalinfo q_h; YAP_dogoalinfo q_h;
YAPPairTerm names; YAPPairTerm names;
@ -54,10 +53,10 @@ class X_API YAPQuery : public YAPPredicate {
q_state = 0; q_state = 0;
q_flags = true; // PL_Q_PASS_EXCEPTION; q_flags = true; // PL_Q_PASS_EXCEPTION;
q_p = P; q_h.p = P;
q_cp = CP; q_h.cp = CP;
// make sure this is safe // make sure this is safe
q_handles = LOCAL_CurSlot; q_h.CurSlot = LOCAL_CurSlot;
}; };
void openQuery(); void openQuery();
@ -321,7 +320,6 @@ private:
YAPCallback *_callback; YAPCallback *_callback;
YAPError yerror; YAPError yerror;
void doInit(YAP_file_type_t BootMode, YAPEngineArgs *cargs); void doInit(YAP_file_type_t BootMode, YAPEngineArgs *cargs);
YAP_dogoalinfo q;
YAPError e; YAPError e;
PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term &t, Term tmod); PredEntry *rewriteUndefEngineQuery(PredEntry *ap, Term &t, Term tmod);

View File

@ -91,7 +91,6 @@ typedef YAP_Term Term;
#define CellSize sizeof(CELL) #define CellSize sizeof(CELL)
#define SmallSize sizeof(SMALLUNSGN) #define SmallSize sizeof(SMALLUNSGN)
typedef YAP_Int Int;
typedef YAP_Float Float; typedef YAP_Float Float;
typedef YAP_handle_t yhandle_t; typedef YAP_handle_t yhandle_t;

View File

@ -47,20 +47,20 @@ extern const void *MallocExportAsRO(const void *blk);
extern int AllocLevel(void); extern int AllocLevel(void);
#define push_text_stack() \ #define push_text_stack() \
(/* fprintf(stderr, " + *** %d %s:%s:%d\n", AllocLevel(),*/ \ ( fprintf(stderr, " + *** %d %s:%s:%d\n", AllocLevel(), \
/* __FILE__, __FUNCTION__, __LINE__), */ \ __FILE__, __FUNCTION__, __LINE__), \
push_text_stack__(PASS_REGS1)) push_text_stack__(PASS_REGS1))
extern int push_text_stack__(USES_REGS1); extern int push_text_stack__(USES_REGS1);
#define pop_text_stack(lvl) \ #define pop_text_stack(lvl) \
(/* fprintf(stderr, " - *** %d %s:%s:%d\n", AllocLevel(), __FILE__,*/ \ ( fprintf(stderr, " - *** %d %s:%s:%d\n", AllocLevel(), __FILE__, \
/* __FUNCTION__, __LINE__), */ \ __FUNCTION__, __LINE__), \
pop_text_stack__(lvl)) pop_text_stack__(lvl))
extern int pop_text_stack__(int lvl USES_REGS); extern int pop_text_stack__(int lvl USES_REGS);
#define pop_output_text_stack(lvl,p) \ #define pop_output_text_stack(lvl,p) \
(/*fprintf(stderr, "-- *** %d %s:%s:%d\n", AllocLevel(), __FILE__,*/ \ (fprintf(stderr, "-- *** %d %s:%s:%d\n", AllocLevel(), __FILE__, \
/* __FUNCTION__, __LINE__),*/ \ __FUNCTION__, __LINE__), \
pop_output_text_stack__(lvl,p)) pop_output_text_stack__(lvl,p))
extern void *pop_output_text_stack__(int lvl, const void *ox USES_REGS); extern void *pop_output_text_stack__(int lvl, const void *ox USES_REGS);

View File

@ -247,7 +247,7 @@ extern Term Yap_SaveTerm(Term);
extern Term Yap_SetGlobalVal(Atom, Term); extern Term Yap_SetGlobalVal(Atom, Term);
extern Term Yap_GetGlobal(Atom); extern Term Yap_GetGlobal(Atom);
extern Int Yap_DeleteGlobal(Atom); extern Int Yap_DeleteGlobal(Atom);
extern void Yap_AllocateDefaultArena(size_t gsize, int wid); extern void Yap_AllocateDefaultArena(size_t gsize, int wid, void *cs);
extern CELL *Yap_ArenaLimit(Term arena); extern CELL *Yap_ArenaLimit(Term arena);
/* grow.c */ /* grow.c */

View File

@ -297,6 +297,7 @@ check_function_exists(mkstemp HAVE_MKSTEMP)
check_function_exists(mktemp HAVE_MKTEMP) check_function_exists(mktemp HAVE_MKTEMP)
check_function_exists(nanosleep HAVE_NANOSLEEP) check_function_exists(nanosleep HAVE_NANOSLEEP)
check_function_exists(mktime HAVE_MKTIME) check_function_exists(mktime HAVE_MKTIME)
check_function_exists(mtrace HAVE_MTRACE)
check_function_exists(opendir HAVE_OPENDIR) check_function_exists(opendir HAVE_OPENDIR)
if (NOT APPLE) if (NOT APPLE)
check_function_exists(open_memstream HAVE_OPEN_MEMSTREAM) check_function_exists(open_memstream HAVE_OPEN_MEMSTREAM)

View File

@ -31,6 +31,7 @@ set (ENGINE_SOURCES
C/corout.c C/corout.c
C/cut_c.c C/cut_c.c
C/dbase.c C/dbase.c
C/dbload.c
C/dlmalloc.c C/dlmalloc.c
C/errors.c C/errors.c
C/eval.c C/eval.c

View File

@ -942,6 +942,11 @@ function. */
#cmakedefine HAVE_MPI_H ${HAVE_MPI_H} #cmakedefine HAVE_MPI_H ${HAVE_MPI_H}
#endif #endif
/* Define to 1 if you have the <mtrace> glibc extension. */
#ifndef HAVE_MPI_H
#cmakedefine HAVE_MTRACE ${HAVE_TRACE}
#endif
/* Older versions of MPZ didn't have XOR */ /* Older versions of MPZ didn't have XOR */
#ifndef HAVE_MPZ_XOR #ifndef HAVE_MPZ_XOR
#cmakedefine HAVE_MPZ_XOR ${HAVE_MPZ_XOR} #cmakedefine HAVE_MPZ_XOR ${HAVE_MPZ_XOR}

View File

@ -138,12 +138,13 @@ typedef enum {
/* this should be opaque to the user */ /* this should be opaque to the user */
typedef struct { typedef struct {
unsigned long b, b0; //> choice-point at entry unsigned long b0, b_entry, b_exit; //> 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
struct yami *cp; //> Continuation PC at entry struct yami *cp; //> Continuation PC at entry
int lvl; int lvl;
long env0;
unsigned long tr, h; unsigned long tr, h;
} YAP_dogoalinfo; } YAP_dogoalinfo;

View File

@ -73,7 +73,8 @@ Yap_Error__(false, __FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__)
{ if ( (TF = Yap_ensure_atom__(__FILE__, __FUNCTION__, __LINE__, T0 ) == 0L ) return false; \ { if ( (TF = Yap_ensure_atom__(__FILE__, __FUNCTION__, __LINE__, T0 ) == 0L ) return false; \
} }
INLINE_ONLY Term Yap_ensure_atom__(const char *fu, const char *fi, int line, //INLINE_ONLY
static Term Yap_ensure_atom__(const char *fu, const char *fi, int line,
Term in) { Term in) {
Term t = Deref(in); Term t = Deref(in);
// Term Context = Deref(ARG2); // Term Context = Deref(ARG2);

View File

@ -1933,6 +1933,19 @@ static Int p_sleep(USES_REGS1) {
return FALSE: return FALSE:
#endif #endif
} }
static Int
p_mtrace()
{
#ifdef HAVE_MTRACE
Term t = Deref(ARG1);
if (t == TermTrue) mtrace();
else if (t == TermFalse) return muntrace();
else return false;
#endif
return true;
}
void Yap_InitSysPreds(void) { void Yap_InitSysPreds(void) {
Yap_InitCPred("log_event", 1, p_log_event, SafePredFlag | SyncPredFlag); Yap_InitCPred("log_event", 1, p_log_event, SafePredFlag | SyncPredFlag);
@ -1972,5 +1985,6 @@ void Yap_InitSysPreds(void) {
Yap_InitCPred("rmdir", 2, p_rmdir, SyncPredFlag); Yap_InitCPred("rmdir", 2, p_rmdir, SyncPredFlag);
Yap_InitCPred("sleep", 1, p_sleep, SyncPredFlag); Yap_InitCPred("sleep", 1, p_sleep, SyncPredFlag);
Yap_InitCPred("make_directory", 1, make_directory, SyncPredFlag); Yap_InitCPred("make_directory", 1, make_directory, SyncPredFlag);
Yap_InitCPred("mtrace", 1, p_mtrace, SyncPredFlag);
} }

View File

@ -729,6 +729,7 @@ char *Yap_TermToBuffer(Term t, int flags) {
t = Deref(t); t = Deref(t);
GLOBAL_Stream[sno].encoding = LOCAL_encoding; GLOBAL_Stream[sno].encoding = LOCAL_encoding;
GLOBAL_Stream[sno].status |= CloseOnException_Stream_f; GLOBAL_Stream[sno].status |= CloseOnException_Stream_f;
GLOBAL_Stream[sno].status &= ~FreeOnClose_Stream_f;
Yap_plwrite(t, GLOBAL_Stream + sno, 0, flags, GLOBAL_MaxPriority); Yap_plwrite(t, GLOBAL_Stream + sno, 0, flags, GLOBAL_MaxPriority);
char *new = Yap_MemExportStreamPtr(sno); char *new = Yap_MemExportStreamPtr(sno);

View File

@ -0,0 +1,47 @@
Glucose -- Copyright (c) 2009-2014, Gilles Audemard, Laurent Simon
CRIL - Univ. Artois, France
LRI - Univ. Paris Sud, France (2009-2013)
Labri - Univ. Bordeaux, France
Syrup (Glucose Parallel) -- Copyright (c) 2013-2014, Gilles Audemard, Laurent Simon
CRIL - Univ. Artois, France
Labri - Univ. Bordeaux, France
Glucose sources are based on MiniSat (see below MiniSat copyrights). Permissions and copyrights of
Glucose (sources until 2013, Glucose 3.0, single core) are exactly the same as Minisat on which it
is based on. (see below).
Glucose-Syrup sources are based on another copyright. Permissions and copyrights for the parallel
version of Glucose-Syrup (the "Software") are granted, free of charge, to deal with the Software
without restriction, including the rights to use, copy, modify, merge, publish, distribute,
sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
- The above and below copyrights notices and this permission notice shall be included in all
copies or substantial portions of the Software;
- The parallel version of Glucose (all files modified since Glucose 3.0 releases, 2013) cannot
be used in any competitive event (sat competitions/evaluations) without the express permission of
the authors (Gilles Audemard / Laurent Simon). This is also the case for any competitive event
using Glucose Parallel as an embedded SAT engine (single core or not).
--------------- Original Minisat Copyrights
Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
Copyright (c) 2007-2010, Niklas Sorensson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,24 @@
Directory overview:
==================
mtl/ Minisat Template Library
core/ A core version of the solver glucose (no main here)
simp/ An extended solver with simplification capabilities
parallel/ A multicore version of glucose
README
LICENSE
Changelog
To build (release version: without assertions, statically linked, etc):
======================================================================
Like minisat....
cd { simp | parallel }
make rs
Usage:
======
in simp directory: ./glucose --help
in parallel directory: ./glucose-syrup --help

View File

@ -114,7 +114,8 @@ static foreign_t array_to_python_view(term_t addr, term_t type, term_t szt,
static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tlen) { static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tlen) {
size_t sz, i; size_t sz, i;
PyErr_Clear();
PyErr_Clear();
PyObject *pyl = term_to_python(pyt, true, NULL, true); PyObject *pyl = term_to_python(pyt, true, NULL, true);
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
@ -135,10 +136,10 @@ static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tle
PyList_SET_ITEM(pyl, i, t); PyList_SET_ITEM(pyl, i, t);
} }
if (PL_is_variable(tlen)) { if (PL_is_variable(tlen)) {
PL_unify_int64(tlen, sz); PL_unify_int64(tlen, sz);
} else { } else {
python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL); python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL);
} }
pyErrorAndReturn( true); pyErrorAndReturn( true);
} }

View File

@ -76,15 +76,15 @@ static Term python_to_term__(PyObject *pVal) {
t[1] = MkFloatTerm(PyComplex_ImagAsDouble(pVal)); t[1] = MkFloatTerm(PyComplex_ImagAsDouble(pVal));
return Yap_MkApplTerm(FunctorI, 2, t); return Yap_MkApplTerm(FunctorI, 2, t);
} }
else if (PyUnicode_Check(pVal)) { else if (PyUnicode_Check(pVal)) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
size_t sz = PyUnicode_GetSize(pVal) + 1; size_t sz = PyUnicode_GetSize(pVal) + 1;
wchar_t *s = malloc(sizeof(wchar_t) * sz); wchar_t *s = malloc(sizeof(wchar_t) * sz);
sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, a, sz - 1); sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, a, sz - 1);
free(ptr); free(ptr);
#else #else
const char *s = PyUnicode_AsUTF8(pVal); const char *s = PyUnicode_AsUTF8(pVal);
#endif #endif
#if 0 #if 0
if (false && Yap_AtomInUse(s)) if (false && Yap_AtomInUse(s))
@ -92,72 +92,72 @@ else if (PyUnicode_Check(pVal)) {
else else
#endif #endif
if (pyStringToString) if (pyStringToString)
return MkStringTerm(s); return MkStringTerm(s);
else else
return MkAtomTerm(Yap_LookupAtom(s)); return MkAtomTerm(Yap_LookupAtom(s));
} }
else if (PyByteArray_Check(pVal)) { else if (PyByteArray_Check(pVal)) {
return MkStringTerm(PyByteArray_AsString(pVal)); return MkStringTerm(PyByteArray_AsString(pVal));
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
} }
else if (PyString_Check(pVal)) { else if (PyString_Check(pVal)) {
return MkStringTerm(PyString_AsString(pVal)); return MkStringTerm(PyString_AsString(pVal));
#endif #endif
} }
else if (PyTuple_Check(pVal)) { else if (PyTuple_Check(pVal)) {
Py_ssize_t sz = PyTuple_Size(pVal); Py_ssize_t sz = PyTuple_Size(pVal);
const char *s; const char *s;
s = Py_TYPE(pVal)->tp_name; s = Py_TYPE(pVal)->tp_name;
if (s == NULL) if (s == NULL)
s = "t"; s = "t";
if (sz == 0) { if (sz == 0) {
return MkAtomTerm(YAP_LookupAtom(Py_TYPE(pVal)->tp_name)); return MkAtomTerm(YAP_LookupAtom(Py_TYPE(pVal)->tp_name));
}
else {
Functor f = Yap_MkFunctor(Yap_LookupAtom(s), sz);
Term t = Yap_MkNewApplTerm(f, sz);
long i;
CELL *ptr = RepAppl(t) + 1;
for (i = 0; i < sz; i++) {
PyObject *p = PyTuple_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
} }
*ptr++ = python_to_term__(p); else {
Functor f = Yap_MkFunctor(Yap_LookupAtom(s), sz);
Term t = Yap_MkNewApplTerm(f, sz);
long i;
CELL *ptr = RepAppl(t) + 1;
for (i = 0; i < sz; i++) {
PyObject *p = PyTuple_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
}
*ptr++ = python_to_term__(p);
}
return t;
}
// PL_reset_term_refs(to);
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// ||***\n",stderr);
} }
return t; else if (PyList_Check(pVal)) {
} Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
// PL_reset_term_refs(to); if (sz == 0)
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" return repr_term(pVal);
// ||***\n",stderr); Term t = TermNil;
} for (i = sz; i > 0; ) {
else if (PyList_Check(pVal)) { -- i;
Py_ssize_t i, sz = PyList_GET_SIZE(pVal); PyObject *p = PyList_GetItem(pVal, i);
if (sz == 0) if (p == NULL) {
return repr_term(pVal); PyErr_Clear();
Term t = TermNil; return false;
for (i = sz; i > 0; ) { }
-- i; if (!python_to_term__(p))
PyObject *p = PyList_GetItem(pVal, i); return false;
if (p == NULL) {
PyErr_Clear();
return false;
}
if (!python_to_term__(p))
return false;
t = MkPairTerm(python_to_term__(p), t); t = MkPairTerm(python_to_term__(p), t);
}
return t;
} }
return t; else if (PyDict_Check(pVal)) {
} Py_ssize_t pos = 0, tot = PyDict_Size(pVal);
else if (PyDict_Check(pVal)) { PyObject *key, *value;
Py_ssize_t pos = 0, tot = PyDict_Size(pVal); Term f, *opt = &f, t, to;
PyObject *key, *value; if (tot == 0)
Term f, *opt = &f, t, to; return MkAtomTerm( Yap_LookupAtom("{}"));
if (tot == 0) while (PyDict_Next(pVal, &pos, &key, &value)) {
return MkAtomTerm( Yap_LookupAtom("{}"));
while (PyDict_Next(pVal, &pos, &key, &value)) {
Term t0[2]; Term t0[2];
t0[0] = python_to_term__(key); t0[0] = python_to_term__(key);
t0[1] = python_to_term__(value); t0[1] = python_to_term__(value);
@ -169,9 +169,9 @@ else if (PyDict_Check(pVal)) {
*opt = t; *opt = t;
opt = pt+1; opt = pt+1;
} else { } else {
if (pos == 0) { if (pos == 0) {
return repr_term(pVal); return repr_term(pVal);
} }
*opt = to; *opt = to;
break; break;
@ -179,7 +179,7 @@ else if (PyDict_Check(pVal)) {
} }
return Yap_MkApplTerm(FunctorBraces, 1, &f); return Yap_MkApplTerm(FunctorBraces, 1, &f);
} }
return repr_term(pVal); return repr_term(pVal);
} }
@ -197,11 +197,13 @@ X_API YAP_Term pythonToYAP(PyObject *pVal) {
/* fputs("***>>\n", stderr); */ /* fputs("***>>\n", stderr); */
if (pVal == NULL) if (pVal == NULL)
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, 0, NULL); Yap_ThrowError(SYSTEM_ERROR_INTERNAL, 0, NULL);
yhandle_t h0 = Yap_CurrentHandle();
Term t = python_to_term__(pVal); Term t = python_to_term__(pVal);
/* fputs("<< *** ", stderr); */ /* fputs("<< *** ", stderr); */
/* Yap_DebugPlWrite(t); */ /* Yap_DebugPlWrite(t); */
/* fputs(" ***\n", stderr); */ /* fputs(" ***\n", stderr); */
// Py_DECREF(pVal); // Py_DECREF(pVal);
Yap_CloseHandles(h0);
return t; return t;
} }
@ -225,13 +227,18 @@ PyObject *py_Local, *py_Global;
*python_assign. *python_assign.
*/ */
bool python_assign(term_t t, PyObject *exp, PyObject *context) { bool python_assign(term_t t, PyObject *exp, PyObject *context) {
PyErr_Print(); bool rc = true;
PyErr_Print();
term_t inp = Yap_CurrentHandle();
context = find_obj(context, t, false); context = find_obj(context, t, false);
// Yap_DebugPlWriteln(yt); // Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) { switch (PL_term_type(t)) {
case PL_VARIABLE: { case PL_VARIABLE: {
if (context == NULL) // prevent a.V= N*N[N-1] if (context == NULL) // prevent a.V= N*N[N-1]
return python_to_term(exp, t); PL_reset_term_refs(inp);
rc = python_to_term(exp, t);
break;
} }
case PL_STRING: { case PL_STRING: {
@ -240,24 +247,34 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
PL_get_string_chars(t, &s,&l); PL_get_string_chars(t, &s,&l);
if (!context) if (!context)
context = py_Main; context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0) if (PyObject_SetAttrString(context, s, exp) == 0) {
return true; PL_reset_term_refs(inp);
PyErr_Print(); ;
return false; rc = true;
} else {
PyErr_Print();
PL_reset_term_refs(inp);
rc = false;
}
break;
} }
case PL_ATOM: { case PL_ATOM: {
char *s = NULL; char *s = NULL;
PL_get_atom_chars(t, &s); PL_get_atom_chars(t, &s);
if (!context) if (!context)
context = py_Main; context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0) if (PyObject_SetAttrString(context, s, exp) == 0){
return true; rc = true;
return false; } else {
rc = false;
}
break;
} }
case PL_INTEGER: case PL_INTEGER:
case PL_FLOAT: case PL_FLOAT:
// domain or type erro? // domain or type erro?
return false; rc = false;
break;
default: { default: {
term_t tail = PL_new_term_ref(), arg = PL_new_term_ref(); term_t tail = PL_new_term_ref(), arg = PL_new_term_ref();
size_t len, i; size_t len, i;
@ -271,90 +288,96 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyObject *p; PyObject *p;
if (!PL_get_list(t, arg, t)) { if (!PL_get_list(t, arg, t)) {
PL_reset_term_refs(tail);
p = Py_None; p = Py_None;
} rc = false;
if ((p = PySequence_GetItem(exp, i)) == NULL) } else {
p = Py_None; if ((p = PySequence_GetItem(exp, i)) == NULL)
if (!python_assign(arg, p, context)) { p = Py_None;
PL_reset_term_refs(tail); rc = rc && python_assign(arg, p, context);
} }
} }
} else {
while(PL_is_pair(t)) {
context = find_obj(context, t, false);
}
rc = python_assign(t, exp, context);
} }
} else { } else {
functor_t fun; functor_t fun;
if (!PL_get_functor(t, &fun)) { if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail); rc = false;
return false;
} }
if (fun == FUNCTOR_sqbrackets2) { if (fun == FUNCTOR_sqbrackets2) {
// tail is the object o // tail is the object o
if (!PL_get_arg(2, t, tail)) { if (!PL_get_arg(2, t, tail)) {
PL_reset_term_refs(tail); rc = false;
return false; } else {
} PyObject *o = term_to_python(tail, true, context, false);
PyObject *o = term_to_python(tail, true, context, false); // t now refers to the index
// t now refers to the index if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) ||
if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) || !PL_get_nil(tail)) {
!PL_get_nil(tail)) { rc = false;
PL_reset_term_refs(tail); } else {
return false; PyObject *i = term_to_python(t, true, NULL, false);
} // check numeric
PyObject *i = term_to_python(t, true, NULL, false); if (PySequence_Check(o) && PyLong_Check(i)) {
// check numeric long int j;
if (PySequence_Check(o) && PyLong_Check(i)) { j = PyLong_AsLong(i);
long int j; rc = PySequence_SetItem(o, j, exp) == 0;
j = PyLong_AsLong(i); }
return PySequence_SetItem(o, j, exp) == 0;
}
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (PySequence_Check(o) && PyInt_Check(i)) { if (PySequence_Check(o) && PyInt_Check(i)) {
long int j; long int j;
j = PyInt_AsLong(i); j = PyInt_AsLong(i);
return PySequence_SetItem(o, i, exp) == 0; szzb rc = PySequence_SetItem(o, i, exp) == 0;
} } else
#endif #endif
if (PyDict_Check(o)) { if (PyDict_Check(o)) {
if (PyDict_SetItem(o, i, exp) == 0) { if (PyDict_SetItem(o, i, exp) == 0) {
return true; PL_reset_term_refs(inp);
} ;
} rc = true;
if (PyObject_SetAttr(o, i, exp) == 0) { }
return true; }
} if (PyObject_SetAttr(o, i, exp) == 0) {
PL_reset_term_refs(inp);
;
rc = true;
}
}
}
} else { } else {
atom_t s; atom_t s;
int n, i; int n, i;
PL_get_name_arity(t, &s, &n); PL_get_name_arity(t, &s, &n);
PyObject *o = term_to_python(t, true, context, true); PyObject *o = term_to_python(t, true, context, true);
PyErr_Print(); PyErr_Print();
if (PySequence_Check(o) && PySequence_Length(o) == n) { if (PySequence_Check(o) && PySequence_Length(o) == n) {
for (i = 1; i <= n; i++) { for (i = 1; i <= n; i++) {
PyObject *p; PyObject *p;
if (!PL_get_arg(i, t, arg)) { if (!PL_get_arg(i, t, arg)) {
PL_reset_term_refs(tail); o = NULL;
o = false;
p = Py_None; p = Py_None;
} }
if ((p = PySequence_GetItem(exp, i - 1)) == NULL) if ((p = PySequence_GetItem(exp, i - 1)) == NULL)
p = Py_None; p = Py_None;
else if (!python_assign(arg, p, NULL)) { rc = python_assign(arg, p, NULL);
PL_reset_term_refs(tail);
o = NULL;
} else {
PyErr_Print();
}
} }
return true;
} }
} }
} }
PL_reset_term_refs(tail);
} }
PyErr_Print();
return NULL;
} }
PyErr_Print();
PL_reset_term_refs(inp);
return rc;
} }

View File

@ -23,10 +23,14 @@
#undef _XOPEN_SOURCE // python does its own thing #undef _XOPEN_SOURCE // python does its own thing
#endif #endif
#undef _POSIX_C_SOURCE
#include <Python.h> #include <Python.h>
#include <Yap.h> #include <Yap.h>
#include <SWI-Prolog.h> #include <SWI-Prolog.h>
#ifdef HAVE_STAT #ifdef HAVE_STAT
#undef HAVE_STATa #undef HAVE_STATa
@ -95,7 +99,7 @@ extern PyObject *py_Sys;
extern X_API bool python_in_python; extern X_API bool python_in_python;
extern bool pyStringToString; extern bool pyStringToString;
extern bool python_release_GIL(term_t gstate); extern bool python_release_GIL(term_t gstate);
extern term_t python_acquire_GIL(void); extern term_t python_acquire_GIL(void);
static inline Py_ssize_t get_p_int(PyObject *o, Py_ssize_t def) { static inline Py_ssize_t get_p_int(PyObject *o, Py_ssize_t def) {
@ -201,22 +205,21 @@ extern PyObject *YEC(PyObject *c,PyObject *a ,PyObject *d , int line, const char
extern void YEM(const char *ex, int line, const char *file, const char *code); extern void YEM(const char *ex, int line, const char *file, const char *code);
extern void pyErrorHandler__(int line, const char *file, const char *code); extern void pyErrorHandler__(int line, const char *file, const char *code);
#define pyErrorHandler() \ #define PyStart() PyErr_Clear()
{ \
if (PyErr_Occurred()) { \
#define pyErrorHandler() if (PyErr_Occurred()) { \
pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \ pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \
} \ } \
}
#define pyErrorAndReturn(x) \ #define pyErrorAndReturn(x) \
{ \
if (PyErr_Occurred()) { \ if (PyErr_Occurred()) { \
pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \ pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \
} \ } \
return (x); \ return (x)
}
// #define pyErrorAndReturn( x, y ) return x // #define pyErrorAndReturn( x, y ) return x
extern PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt); extern PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt);
extern PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt); extern PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt);

View File

@ -112,12 +112,12 @@ find_obj(PyObject *ob, term_t l, bool eval) {
YAP_Term hd, yt; YAP_Term hd, yt;
py_Context = NULL; py_Context = NULL;
yt = YAP_GetFromSlot(l);
// Yap_DebugPlWriteln(yt); // Yap_DebugPlWriteln(yt);
while (YAP_IsPairTerm(yt)) { if (l == 0)
return Py_None;
while (YAP_IsPairTerm((yt = YAP_GetFromSlot(l)))) {
hd = YAP_HeadOfTerm(yt); hd = YAP_HeadOfTerm(yt);
yt = YAP_TailOfTerm(yt); Yap_PutInHandle(l, YAP_TailOfTerm(yt));
YAP_PutInSlot(l, yt);
ob = yap_to_python(hd, true, ob, false); ob = yap_to_python(hd, true, ob, false);
if (!ob) { if (!ob) {
return Py_None; return Py_None;

View File

@ -20,6 +20,7 @@ void pyErrorHandler__(int line, const char *file, const char *code) {
static foreign_t python_len(term_t tobj, term_t tf) { static foreign_t python_len(term_t tobj, term_t tf) {
Py_ssize_t len; Py_ssize_t len;
PyObject *o; PyObject *o;
PyStart();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
if (o == NULL) { if (o == NULL) {
@ -32,13 +33,14 @@ static foreign_t python_len(term_t tobj, term_t tf) {
static foreign_t python_represent( term_t name, term_t tobj) { static foreign_t python_represent( term_t name, term_t tobj) {
term_t stackp = python_acquire_GIL(); term_t stackp = python_acquire_GIL();
PyObject *e; PyObject *e;
PyStart();
e = term_to_python(tobj, false, NULL, false); e = term_to_python(tobj, false, NULL, false);
if (e == NULL) { if (e == NULL) {
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(false); pyErrorAndReturn(false);
} }
bool b = python_assign(name, e, NULL); foreign_t b = python_assign(name, e, NULL);
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(b); pyErrorAndReturn(b);
} }
@ -53,6 +55,7 @@ static foreign_t python_clear_errors(void) {
static foreign_t python_dir(term_t tobj, term_t tf) { static foreign_t python_dir(term_t tobj, term_t tf) {
PyObject *dir; PyObject *dir;
PyObject *o; PyObject *o;
PyStart();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
if (o == NULL) { if (o == NULL) {
@ -70,6 +73,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
PyObject *i; PyObject *i;
PyObject *o; PyObject *o;
PyObject *f; PyObject *f;
PyStart();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
if (o == NULL) { if (o == NULL) {
@ -96,6 +100,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
static foreign_t python_is(term_t tobj, term_t tf) { static foreign_t python_is(term_t tobj, term_t tf) {
PyObject *o; PyObject *o;
PyStart();
term_t lim = python_acquire_GIL(); term_t lim = python_acquire_GIL();
@ -112,13 +117,13 @@ static foreign_t python_is(term_t tobj, term_t tf) {
} }
static foreign_t python_proc(term_t tobj) { static foreign_t python_proc(term_t tobj) {
PyStart();
PyObject *o; PyObject *o;
term_t lim = python_acquire_GIL(); term_t lim = python_acquire_GIL();
o = term_to_python(tobj, true, NULL, true); o = term_to_python(tobj, true, NULL, true);
python_release_GIL(lim); python_release_GIL(lim);
bool rc = o != NULL; foreign_t rc = o != NULL;
pyErrorAndReturn(rc); pyErrorAndReturn(rc);
} }
@ -134,9 +139,7 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
p = term_to_python(parent, true, NULL, true); p = term_to_python(parent, true, NULL, true);
// Exp // Exp
if (!pI || !p) { if (!pI || !p) {
{ pyErrorAndReturn(false);
pyErrorAndReturn(false);
}
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) { } else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
PyErr_Print(); PyErr_Print();
{ pyErrorAndReturn(false); } { pyErrorAndReturn(false); }
@ -153,6 +156,7 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
static foreign_t python_apply(term_t tin, term_t targs, term_t keywds, static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t tf) { term_t tf) {
PyStart();
PyObject *pF; PyObject *pF;
PyObject *pArgs, *pKeywords; PyObject *pArgs, *pKeywords;
PyObject *pValue; PyObject *pValue;
@ -162,7 +166,6 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
pF = term_to_python(tin, true, NULL, true); pF = term_to_python(tin, true, NULL, true);
PyErr_Clear();
if (pF == NULL) { if (pF == NULL) {
{ {
pyErrorAndReturn(false); pyErrorAndReturn(false);
@ -237,6 +240,7 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
} }
static foreign_t assign_python(term_t exp, term_t name) { static foreign_t assign_python(term_t exp, term_t name) {
PyStart();
term_t stackp = python_acquire_GIL(); term_t stackp = python_acquire_GIL();
PyObject *e = term_to_python(exp, true, NULL, true); PyObject *e = term_to_python(exp, true, NULL, true);
@ -244,7 +248,7 @@ static foreign_t assign_python(term_t exp, term_t name) {
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(false); pyErrorAndReturn(false);
} }
bool b = python_assign(name, e, NULL); foreign_t b = python_assign(name, e, NULL);
python_release_GIL(stackp); python_release_GIL(stackp);
pyErrorAndReturn(b); pyErrorAndReturn(b);
} }
@ -269,7 +273,7 @@ static foreign_t python_string_to(term_t f) {
} }
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) { static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
PyErr_Clear(); PyStart();
PyObject *pI, *pArgs, *pOut; PyObject *pI, *pArgs, *pOut;
PyObject *env; PyObject *env;
atom_t name; atom_t name;
@ -331,7 +335,7 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
} }
static foreign_t python_access(term_t obj, term_t f, term_t out) { static foreign_t python_access(term_t obj, term_t f, term_t out) {
PyErr_Clear(); PyStart();
PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF; PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF;
atom_t name; atom_t name;
char *s = NULL; char *s = NULL;
@ -400,7 +404,7 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
atom_t name; atom_t name;
char *s; char *s;
int arity; int arity;
PyStart();
if (!PL_get_name_arity(att, &name, &arity)) { if (!PL_get_name_arity(att, &name, &arity)) {
{ {
pyErrorAndReturn(false); pyErrorAndReturn(false);
@ -448,14 +452,14 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
static foreign_t python_main_module(term_t mod) { static foreign_t python_main_module(term_t mod) {
{ {
foreign_t rc; foreign_t rc;
PyErr_Clear(); PyStart();
rc = address_to_term(py_Main, mod); rc = address_to_term(py_Main, mod);
pyErrorAndReturn(rc); pyErrorAndReturn(rc);
} }
} }
static foreign_t python_function(term_t tobj) { static foreign_t python_function(term_t tobj) {
PyErr_Clear(); PyStart();
PyObject *obj = term_to_python(tobj, true, NULL, true); PyObject *obj = term_to_python(tobj, true, NULL, true);
foreign_t rc = PyFunction_Check(obj); foreign_t rc = PyFunction_Check(obj);
@ -465,7 +469,7 @@ static foreign_t python_function(term_t tobj) {
foreign_t python_builtin(term_t out) { foreign_t python_builtin(term_t out) {
{ {
foreign_t rc; foreign_t rc;
PyErr_Clear(); PyStart();
rc = address_to_term(py_Builtin, out); rc = address_to_term(py_Builtin, out);
pyErrorAndReturn(rc); pyErrorAndReturn(rc);
} }
@ -476,7 +480,7 @@ static foreign_t python_run_file(term_t file) {
size_t len; size_t len;
char si[256]; char si[256];
s = si; s = si;
PyErr_Clear(); PyStart();
if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) { if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
PyObject *PyFileObject = PyFile_FromString(si, "r"); PyObject *PyFileObject = PyFile_FromString(si, "r");
@ -499,11 +503,11 @@ extern PyThreadState *YAP_save;
static foreign_t python_run_command(term_t cmd) { static foreign_t python_run_command(term_t cmd) {
char *s; char *s;
bool rc = false; foreign_t rc = false;
size_t len; size_t len;
char si[256]; char si[256];
PyErr_Clear(); PyStart();
s = si; s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) { if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
PyRun_SimpleString(s); PyRun_SimpleString(s);
@ -519,7 +523,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
PyObject *pArgs = NULL, *pValue; PyObject *pArgs = NULL, *pValue;
char *s; char *s;
PyErr_Clear(); PyStart();
s = si; s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) && if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) &&
(s = sf) != NULL && (s = sf) != NULL &&
@ -571,7 +575,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
static foreign_t python_export(term_t t, term_t pl) { static foreign_t python_export(term_t t, term_t pl) {
foreign_t rc = false; foreign_t rc = false;
PyErr_Clear(); PyStart();
if (PL_is_functor(t, FUNCTOR_pointer1)) { if (PL_is_functor(t, FUNCTOR_pointer1)) {
void *ptr; void *ptr;
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
@ -600,7 +604,8 @@ static foreign_t python_export(term_t t, term_t pl) {
*/ */
static int python_import(term_t mname, term_t mod) { static int python_import(term_t mname, term_t mod) {
PyObject *pName; PyObject *pName;
bool do_as = false; foreign_t do_as = false;
PyStart();
char s0[MAXPATHLEN], *s = s0; char s0[MAXPATHLEN], *s = s0;
s[0] = '\0'; s[0] = '\0';
@ -673,7 +678,7 @@ static int python_import(term_t mname, term_t mod) {
static foreign_t python_to_rhs(term_t inp, term_t t) { static foreign_t python_to_rhs(term_t inp, term_t t) {
PyObject *pVal; PyObject *pVal;
PyErr_Clear(); PyStart();
pVal = term_to_python(inp, true, NULL, true); pVal = term_to_python(inp, true, NULL, true);
if (pVal == NULL) if (pVal == NULL)
pyErrorAndReturn(false); pyErrorAndReturn(false);
@ -681,7 +686,7 @@ static foreign_t python_to_rhs(term_t inp, term_t t) {
} }
// static PyThreadState *_saveP = NULL; // static PyThreadState *_saveP = NULL;
static bool _threaded = true; static foreign_t _threaded = true;
/* /*
static YAP_Int static YAP_Int
@ -707,7 +712,7 @@ PyThreadState *tstate;
static YAP_Int p_python_threaded(void) { static YAP_Int p_python_threaded(void) {
PyErr_Clear(); PyStart();
// PyEval_ReleaseThread(tstate); // PyEval_ReleaseThread(tstate);
// _threaded = true; // _threaded = true;
// _locked = 0; // _locked = 0;
@ -730,7 +735,6 @@ term_t python_acquire_GIL(void) {
PL_put_integer(curSlot, gstatei++); PL_put_integer(curSlot, gstatei++);
return curSlot; return curSlot;
} }
bool python_release_GIL(term_t curBlock) { bool python_release_GIL(term_t curBlock) {
int gstateix; int gstateix;
gstatei--; gstatei--;

View File

@ -334,7 +334,7 @@ prolog_file_name(File, PrologFileName) :-
system:true_file_name(File, PrologFileName). system:true_file_name(File, PrologFileName).
prolog_file_name(File, PrologFileName) :- prolog_file_name(File, PrologFileName) :-
'$do_error'(type_error(atom,File), prolog_file_name(File, PrologFileName)). '$do_error'(type_error(atom,File), prolog_file_name(File, PrologFileName)).
:- start_low_level_trace.
/** /**
@pred path(-Directories:list) is det,deprecated @pred path(-Directories:list) is det,deprecated
@ -344,6 +344,7 @@ prolog_file_name(File, PrologFileName) :-
*/ */
path(Path) :- path(Path) :-
findall(X,'$in_path'(X),Path). findall(X,'$in_path'(X),Path).
:- stop_low_level_trace.
'$in_path'(X) :- '$in_path'(X) :-
recorded('$path',Path,_), recorded('$path',Path,_),

View File

@ -133,7 +133,7 @@ undefined_query(G0, M0, Cut) :-
% we found an import, and call again % we found an import, and call again
% we have user code in the unknown_predicate % we have user code in the unknown_predicate
% we fail, output a message, and just generate an exception. % we fail, output a message, and just generate an exception.
'$undefp'([M0|G0],ok) :- '$undefp'([M0|G0],_) :-
'$search_undef'(M0:G0, M:G), '$search_undef'(M0:G0, M:G),
'$trace'(M:G). '$trace'(M:G).