2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: cdmgr.c *
|
|
|
|
* Last rev: 8/2/88 *
|
|
|
|
* mods: *
|
|
|
|
* comments: Code manager *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "@(#)cdmgr.c 1.1 05/02/98";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "clause.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#include "eval.h"
|
|
|
|
#include "tracer.h"
|
|
|
|
#ifdef YAPOR
|
|
|
|
#include "or.macros.h"
|
|
|
|
#endif /* YAPOR */
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-06-18 06:22:35 +01:00
|
|
|
STATIC_PROTO(void retract_all, (PredEntry *, int));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void add_first_static, (PredEntry *, CODEADDR, int));
|
|
|
|
STATIC_PROTO(void add_first_dynamic, (PredEntry *, CODEADDR, int));
|
|
|
|
STATIC_PROTO(void asserta_stat_clause, (PredEntry *, CODEADDR, int));
|
|
|
|
STATIC_PROTO(void asserta_dynam_clause, (PredEntry *, CODEADDR));
|
|
|
|
STATIC_PROTO(void assertz_stat_clause, (PredEntry *, CODEADDR, int));
|
|
|
|
STATIC_PROTO(void assertz_dynam_clause, (PredEntry *, CODEADDR));
|
|
|
|
STATIC_PROTO(void expand_consult, (void));
|
2001-12-12 19:36:51 +00:00
|
|
|
STATIC_PROTO(int not_was_reconsulted, (PredEntry *, Term, int));
|
2001-04-09 20:54:03 +01:00
|
|
|
#if EMACS
|
|
|
|
STATIC_PROTO(int last_clause_number, (PredEntry *));
|
|
|
|
#endif
|
|
|
|
STATIC_PROTO(int static_in_use, (PredEntry *, int));
|
|
|
|
#if !defined(YAPOR) && !defined(THREADS)
|
|
|
|
STATIC_PROTO(Int search_for_static_predicate_in_use, (PredEntry *, int));
|
|
|
|
STATIC_PROTO(void mark_pred, (int, PredEntry *));
|
|
|
|
STATIC_PROTO(void do_toggle_static_predicates_in_use, (int));
|
|
|
|
#endif
|
|
|
|
STATIC_PROTO(void recover_log_upd_clause, (Clause *));
|
|
|
|
STATIC_PROTO(Int p_number_of_clauses, (void));
|
|
|
|
STATIC_PROTO(Int p_compile, (void));
|
|
|
|
STATIC_PROTO(Int p_compile_dynamic, (void));
|
|
|
|
STATIC_PROTO(Int p_purge_clauses, (void));
|
|
|
|
STATIC_PROTO(Int p_setspy, (void));
|
|
|
|
STATIC_PROTO(Int p_rmspy, (void));
|
|
|
|
STATIC_PROTO(Int p_startconsult, (void));
|
|
|
|
STATIC_PROTO(Int p_showconslultlev, (void));
|
|
|
|
STATIC_PROTO(Int p_endconsult, (void));
|
|
|
|
STATIC_PROTO(Int p_undefined, (void));
|
|
|
|
STATIC_PROTO(Int p_in_use, (void));
|
|
|
|
STATIC_PROTO(Int p_new_multifile, (void));
|
|
|
|
STATIC_PROTO(Int p_is_multifile, (void));
|
|
|
|
STATIC_PROTO(Int p_optimizer_on, (void));
|
|
|
|
STATIC_PROTO(Int p_optimizer_off, (void));
|
|
|
|
STATIC_PROTO(Int p_in_this_f_before, (void));
|
|
|
|
STATIC_PROTO(Int p_first_cl_in_f, (void));
|
|
|
|
STATIC_PROTO(Int p_mk_cl_not_first, (void));
|
|
|
|
STATIC_PROTO(Int p_is_dynamic, (void));
|
|
|
|
STATIC_PROTO(Int p_kill_dynamic, (void));
|
|
|
|
STATIC_PROTO(Int p_compile_mode, (void));
|
|
|
|
STATIC_PROTO(Int p_is_profiled, (void));
|
|
|
|
STATIC_PROTO(Int p_profile_info, (void));
|
|
|
|
STATIC_PROTO(Int p_profile_reset, (void));
|
|
|
|
STATIC_PROTO(Int p_toggle_static_predicates_in_use, (void));
|
2002-02-26 17:49:09 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
STATIC_PROTO(void list_all_predicates_in_use, (void));
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#define PredArity(p) (p->ArityOfPE)
|
|
|
|
#define TRYCODE(G,F,N) ( (N)<5 ? (op_numbers)((int)F+(N)*3) : G)
|
|
|
|
#define NEXTOP(V,TYPE) ((yamop *)(&((V)->u.TYPE.next)))
|
|
|
|
|
|
|
|
static int compile_mode = 1;
|
|
|
|
|
|
|
|
static char ErrorSay[256];
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
|
|
|
|
EXECUTING PROLOG CLAUSES
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
static_in_use(PredEntry *p, int check_everything)
|
|
|
|
{
|
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
|
|
|
return(FALSE);
|
|
|
|
#else
|
|
|
|
CELL pflags = p->PredFlags;
|
|
|
|
if (pflags & (DynamicPredFlag|LogUpdatePredFlag)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (STATIC_PREDICATES_MARKED) {
|
2001-12-12 19:36:51 +00:00
|
|
|
return (p->StateOfPred & InUseMask);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
/* This code does not work for YAPOR or THREADS!!!!!!!! */
|
2002-02-26 20:16:36 +00:00
|
|
|
return(search_for_static_predicate_in_use(p, check_everything));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
|
|
|
|
ADDING AND REMOVE INFO TO A PROCEDURE
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we have three kinds of predicates: dynamic DynamicPredFlag
|
|
|
|
* static CompiledPredFlag fast FastPredFlag all the
|
|
|
|
* database predicates are supported for dynamic predicates only abolish and
|
|
|
|
* assertz are supported for static predicates no database predicates are
|
|
|
|
* supportted for fast predicates
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define is_dynamic(pe) (pe->PredFlags & DynamicPredFlag)
|
|
|
|
#define is_static(pe) (pe->PredFlags & CompiledPredFlag)
|
|
|
|
#define is_fast(pe) (pe->PredFlags & FastPredFlag)
|
|
|
|
#define is_logupd(pe) (pe->PredFlags & LogUpdatePredFlag)
|
|
|
|
#ifdef TABLING
|
|
|
|
#define is_tabled(pe) (pe->PredFlags & TabledPredFlag)
|
|
|
|
#endif /* TABLING */
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
|
|
|
|
Indexation Info
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
#define ByteAdr(X) ((Int) &(X))
|
|
|
|
|
|
|
|
/* Index a prolog pred, given its predicate entry */
|
|
|
|
/* ap is already locked, but IPred is the one who gets rid of the lock. */
|
|
|
|
void
|
|
|
|
IPred(CODEADDR sp)
|
|
|
|
{
|
|
|
|
PredEntry *ap;
|
|
|
|
CODEADDR BaseAddr;
|
|
|
|
int Arity;
|
|
|
|
Functor f;
|
|
|
|
|
|
|
|
ap = (PredEntry *) sp;
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(ap)) {
|
|
|
|
ap->CodeOfPred = ap->TrueCodeOfPred;
|
|
|
|
ap->OpcodeOfPred = ((yamop *)(ap->CodeOfPred))->opc;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* TABLING */
|
|
|
|
f = ap->FunctorOfPred;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (Option['i' - 'a' + 1]) {
|
|
|
|
Atom At = NameOfFunctor(f);
|
|
|
|
DebugPutc(c_output_stream,'\t');
|
|
|
|
plwrite(MkAtomTerm(At), DebugPutc, 0);
|
|
|
|
DebugPutc(c_output_stream,'/');
|
|
|
|
plwrite(MkIntTerm(ArityOfFunctor(f)), DebugPutc, 0);
|
|
|
|
DebugPutc(c_output_stream,'\n');
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Arity = ArityOfFunctor(f);
|
|
|
|
/* Do not try to index a dynamic predicate or one whithout args */
|
|
|
|
if (is_dynamic(ap)) {
|
|
|
|
WRITE_UNLOCK(ap->PRWLock);
|
|
|
|
Error(SYSTEM_ERROR,TermNil,"trying to index a dynamic predicate");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Arity == 0) {
|
|
|
|
WRITE_UNLOCK(ap->PRWLock);
|
|
|
|
Error(SYSTEM_ERROR,TermNil,
|
|
|
|
"trying to index a predicate with 0 arguments");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((BaseAddr = PredIsIndexable(ap)) != NIL) {
|
|
|
|
ap->TrueCodeOfPred = BaseAddr;
|
|
|
|
ap->PredFlags |= IndexedPredFlag;
|
|
|
|
}
|
|
|
|
if (ap->PredFlags & SpiedPredFlag) {
|
|
|
|
ap->StateOfPred = StaticMask | SpiedMask;
|
|
|
|
ap->OpcodeOfPred = opcode(_spy_pred);
|
|
|
|
ap->CodeOfPred = (CODEADDR)(&(ap->OpcodeOfPred));
|
|
|
|
} else {
|
|
|
|
ap->StateOfPred = 0;
|
|
|
|
ap->CodeOfPred = ap->TrueCodeOfPred;
|
|
|
|
ap->OpcodeOfPred = ((yamop *)(ap->CodeOfPred))->opc;
|
|
|
|
}
|
|
|
|
WRITE_UNLOCK(ap->PRWLock);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (Option['i' - 'a' + 1])
|
|
|
|
DebugPutc(c_output_stream,'\n');
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define GONEXT(TYPE) code_p = ((yamop *)(&(code_p->u.TYPE.next)))
|
|
|
|
|
|
|
|
static void
|
|
|
|
recover_log_upd_clause(Clause *cl)
|
|
|
|
{
|
|
|
|
LOCK(cl->ClLock);
|
|
|
|
if (cl->ClFlags & LogUpdRuleMask) {
|
|
|
|
if (--(cl->u2.ClExt->u.EC.ClRefs) == 0 &&
|
|
|
|
(cl->ClFlags & ErasedMask) &&
|
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
|
|
|
(cl->ref_count == 0)
|
|
|
|
#else
|
|
|
|
!(cl->ClFlags & InUseMask)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
ErCl(cl);
|
|
|
|
} else {
|
|
|
|
if (--(cl->u2.ClUse) == 0 &&
|
|
|
|
(cl->ClFlags & ErasedMask) &&
|
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
|
|
|
(cl->ref_count == 0)
|
|
|
|
#else
|
|
|
|
!(cl->ClFlags & InUseMask)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
ErCl(cl);
|
|
|
|
}
|
|
|
|
UNLOCK(cl->ClLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Clause *
|
|
|
|
ClauseBodyToClause(CODEADDR addr)
|
|
|
|
{
|
|
|
|
addr = addr - (Int)NEXTOP((yamop *)NULL,ld);
|
|
|
|
return(ClauseCodeToClause(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we already have a lock on the predicate */
|
|
|
|
void
|
|
|
|
RemoveLogUpdIndex(Clause *cl)
|
|
|
|
{
|
|
|
|
yamop *code_p;
|
|
|
|
OPCODE last = opcode(_trust_logical_pred);
|
|
|
|
|
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
|
|
|
if (cl->ref_count != 0)
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
if (cl->ClFlags & InUseMask)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
/* now the hard part, I must tell all other clauses they are free */
|
|
|
|
code_p = cl->u.ClVarChain;
|
|
|
|
/* skip try_log_update */
|
|
|
|
GONEXT(l);
|
|
|
|
recover_log_upd_clause(ClauseBodyToClause(code_p->u.ld.d));
|
|
|
|
GONEXT(ld);
|
|
|
|
while(code_p->opc != last) {
|
|
|
|
recover_log_upd_clause(ClauseBodyToClause(code_p->u.ld.d));
|
|
|
|
GONEXT(ld);
|
|
|
|
}
|
|
|
|
/* skip trust_log_update */
|
|
|
|
GONEXT(l);
|
|
|
|
recover_log_upd_clause(ClauseBodyToClause(code_p->u.ld.d));
|
|
|
|
FreeCodeSpace((char *) cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Routine used when wanting to remove the indexation */
|
|
|
|
/* ap is known to already have been locked for WRITING */
|
|
|
|
int
|
|
|
|
RemoveIndexation(PredEntry *ap)
|
|
|
|
{
|
|
|
|
register CODEADDR First;
|
|
|
|
int spied;
|
|
|
|
|
|
|
|
First = ap->FirstClause;
|
|
|
|
if (ap->OpcodeOfPred == INDEX_OPCODE) {
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
spied = ap->PredFlags & SpiedPredFlag;
|
|
|
|
if (ap->PredFlags & LogUpdatePredFlag)
|
|
|
|
RemoveLogUpdIndex(ClauseCodeToClause(ap->TrueCodeOfPred));
|
|
|
|
else {
|
|
|
|
Clause *cl = ClauseCodeToClause(ap->TrueCodeOfPred);
|
2002-02-26 20:16:36 +00:00
|
|
|
if (static_in_use(ap, FALSE)) {
|
2002-06-18 06:22:35 +01:00
|
|
|
/* This should never happen */
|
|
|
|
cl->u.NextCl = DeadClauses;
|
|
|
|
DeadClauses = cl;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
FreeCodeSpace((char *)cl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (First != ap->LastClause)
|
|
|
|
ap->TrueCodeOfPred = First;
|
|
|
|
ap->PredFlags ^= IndexedPredFlag;
|
|
|
|
if (First != NIL && spied) {
|
|
|
|
ap->OpcodeOfPred = opcode(_spy_pred);
|
|
|
|
ap->CodeOfPred = (CODEADDR)(&(ap->OpcodeOfPred));
|
|
|
|
ap->StateOfPred = StaticMask | SpiedMask;
|
|
|
|
} else {
|
|
|
|
ap->StateOfPred = StaticMask;
|
|
|
|
ap->OpcodeOfPred = ((yamop *)(ap->TrueCodeOfPred))->opc;
|
|
|
|
ap->CodeOfPred = ap->TrueCodeOfPred;
|
|
|
|
}
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
|
|
|
|
Adding clauses
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#define assertz 0
|
|
|
|
#define consult 1
|
|
|
|
#define asserta 2
|
|
|
|
|
|
|
|
/* p is already locked */
|
|
|
|
static void
|
2002-06-18 06:22:35 +01:00
|
|
|
retract_all(PredEntry *p, int in_use)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
CODEADDR q, q1;
|
|
|
|
int multifile_pred = p->PredFlags & MultiFileFlag;
|
|
|
|
CODEADDR fclause = NIL, lclause = NIL;
|
|
|
|
|
|
|
|
q = p->FirstClause;
|
|
|
|
if (q != NIL) {
|
|
|
|
do {
|
|
|
|
Clause *cl;
|
|
|
|
q1 = q;
|
|
|
|
q = NextClause(q);
|
|
|
|
cl = ClauseCodeToClause(q1);
|
|
|
|
if (multifile_pred && cl->Owner != YapConsultingFile()) {
|
|
|
|
if (fclause == NIL) {
|
|
|
|
fclause = q1;
|
|
|
|
} else {
|
|
|
|
yamop *clp = (yamop *)lclause;
|
|
|
|
clp->u.ld.d = q1;
|
|
|
|
}
|
|
|
|
lclause = q1;
|
|
|
|
} else {
|
|
|
|
if (p->PredFlags & LogUpdatePredFlag)
|
|
|
|
ErCl(cl);
|
2002-06-11 06:30:47 +01:00
|
|
|
else {
|
|
|
|
if (cl->ClFlags & HasBlobsMask) {
|
|
|
|
cl->u.NextCl = DeadClauses;
|
|
|
|
DeadClauses = cl;
|
|
|
|
} else {
|
|
|
|
FreeCodeSpace((char *)cl);
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} while (q1 != p->LastClause);
|
|
|
|
}
|
|
|
|
p->FirstClause = fclause;
|
|
|
|
p->LastClause = lclause;
|
|
|
|
if (fclause == NIL) {
|
|
|
|
p->OpcodeOfPred = UNDEF_OPCODE;
|
|
|
|
p->TrueCodeOfPred = p->CodeOfPred = (CODEADDR)(&(p->OpcodeOfPred));
|
|
|
|
p->StatisticsForPred.NOfEntries = 0;
|
|
|
|
p->StatisticsForPred.NOfHeadSuccesses = 0;
|
|
|
|
p->StatisticsForPred.NOfRetries = 0;
|
|
|
|
} else {
|
|
|
|
yamop *cpt = (yamop *)fclause;
|
|
|
|
cpt->opc = opcode(TRYCODE(_try_me, _try_me0, PredArity(p)));
|
|
|
|
if (fclause == lclause) {
|
|
|
|
p->TrueCodeOfPred = p->CodeOfPred = (CODEADDR)NEXTOP(cpt,ld);
|
|
|
|
p->OpcodeOfPred = NEXTOP(cpt,ld)->opc;
|
|
|
|
} else {
|
|
|
|
p->TrueCodeOfPred = p->CodeOfPred = fclause;
|
|
|
|
p->OpcodeOfPred = cpt->opc;
|
|
|
|
if (p->PredFlags & ProfiledPredFlag) {
|
|
|
|
((yamop *)lclause)->opc = opcode(_profiled_trust_me);
|
|
|
|
} else {
|
|
|
|
((yamop *)lclause)->opc = opcode(TRYCODE(_trust_me, _trust_me0, PredArity(p)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->PredFlags & SpiedPredFlag) {
|
|
|
|
p->StateOfPred |= StaticMask | SpiedMask;
|
|
|
|
p->OpcodeOfPred = opcode(_spy_pred);
|
|
|
|
p->CodeOfPred = (CODEADDR)(&(p->OpcodeOfPred));
|
|
|
|
} else if (p->PredFlags & IndexedPredFlag) {
|
|
|
|
p->OpcodeOfPred = INDEX_OPCODE;
|
|
|
|
p->CodeOfPred = (CODEADDR)(&(p->OpcodeOfPred));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PROFILING) {
|
|
|
|
p->PredFlags |= ProfiledPredFlag;
|
|
|
|
} else
|
|
|
|
p->PredFlags &= ~ProfiledPredFlag;
|
|
|
|
#ifdef YAPOR
|
|
|
|
if (SEQUENTIAL_IS_DEFAULT) {
|
|
|
|
p->PredFlags |= SequentialPredFlag;
|
|
|
|
}
|
|
|
|
#endif /* YAPOR */
|
|
|
|
PutValue(AtomAbol, MkAtomTerm(AtomTrue));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p is already locked */
|
|
|
|
static void
|
|
|
|
add_first_static(PredEntry *p, CODEADDR cp, int spy_flag)
|
|
|
|
{
|
|
|
|
yamop *pt = (yamop *)cp;
|
|
|
|
|
|
|
|
pt->u.ld.d = cp;
|
2002-05-16 21:33:00 +01:00
|
|
|
pt->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
|
|
|
if (SEQUENTIAL_IS_DEFAULT) {
|
|
|
|
p->PredFlags |= SequentialPredFlag;
|
|
|
|
PUT_YAMOP_SEQ(pt);
|
|
|
|
}
|
|
|
|
if (YAMOP_LTT(pt) != 1)
|
|
|
|
abort_optyap("YAMOP_LTT error in function add_first_static");
|
|
|
|
#endif /* YAPOR */
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(p)) {
|
|
|
|
pt->u.ld.te = p->TableOfPred;
|
|
|
|
pt->opc = opcode(_table_try_me_single);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* TABLING */
|
|
|
|
{
|
|
|
|
pt->opc = opcode(TRYCODE(_try_me, _try_me0, PredArity(p)));
|
|
|
|
pt = NEXTOP(pt, ld);
|
|
|
|
}
|
|
|
|
p->TrueCodeOfPred = (CODEADDR)pt;
|
|
|
|
p->FirstClause = p->LastClause = cp;
|
|
|
|
p->StatisticsForPred.NOfEntries = 0;
|
|
|
|
p->StatisticsForPred.NOfHeadSuccesses = 0;
|
|
|
|
p->StatisticsForPred.NOfRetries = 0;
|
|
|
|
if (PROFILING) {
|
|
|
|
p->PredFlags |= ProfiledPredFlag;
|
|
|
|
} else
|
|
|
|
p->PredFlags &= ~ProfiledPredFlag;
|
|
|
|
#ifdef YAPOR
|
|
|
|
p->PredFlags |= SequentialPredFlag;
|
|
|
|
PUT_YAMOP_SEQ((yamop *)cp);
|
|
|
|
#endif /* YAPOR */
|
|
|
|
if (spy_flag) {
|
|
|
|
p->StateOfPred |= StaticMask | SpiedMask;
|
|
|
|
p->OpcodeOfPred = opcode(_spy_pred);
|
|
|
|
p->CodeOfPred = (CODEADDR)(&(p->OpcodeOfPred));
|
|
|
|
} else if (is_fast(p)) {
|
|
|
|
p->StateOfPred |= StaticMask;
|
|
|
|
} else {
|
|
|
|
p->StateOfPred |= StaticMask;
|
|
|
|
}
|
|
|
|
if (yap_flags[SOURCE_MODE_FLAG]) {
|
|
|
|
p->PredFlags |= SourcePredFlag;
|
|
|
|
} else {
|
|
|
|
p->PredFlags &= ~SourcePredFlag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p is already locked */
|
|
|
|
static void
|
|
|
|
add_first_dynamic(PredEntry *p, CODEADDR cp, int spy_flag)
|
|
|
|
{
|
|
|
|
yamop *ncp = ((Clause *)NIL)->ClCode;
|
|
|
|
Clause *cl;
|
|
|
|
p->StatisticsForPred.NOfEntries = 0;
|
|
|
|
p->StatisticsForPred.NOfHeadSuccesses = 0;
|
|
|
|
p->StatisticsForPred.NOfRetries = 0;
|
|
|
|
if (PROFILING) {
|
|
|
|
p->PredFlags |= ProfiledPredFlag;
|
|
|
|
} else
|
|
|
|
p->PredFlags &= ~ProfiledPredFlag;
|
|
|
|
#ifdef YAPOR
|
|
|
|
p->PredFlags |= SequentialPredFlag;
|
|
|
|
#endif /* YAPOR */
|
|
|
|
/* allocate starter block, containing info needed to start execution,
|
|
|
|
* that is a try_mark to start the code and a fail to finish things up */
|
|
|
|
cl =
|
|
|
|
(Clause *) AllocCodeSpace((Int)NEXTOP(NEXTOP(NEXTOP(ncp,ld),e),e));
|
|
|
|
if (cl == NIL) {
|
|
|
|
Error(SYSTEM_ERROR,TermNil,"Heap crashed against Stacks");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cl->Owner = p->OwnerFile;
|
|
|
|
/* skip the first entry, this contains the back link and will always be
|
|
|
|
empty for this entry */
|
|
|
|
ncp = (yamop *)(((CELL *)ncp)+1);
|
|
|
|
/* next we have the flags. For this block mainly say whether we are
|
|
|
|
* being spied */
|
|
|
|
if (spy_flag) {
|
|
|
|
cl->ClFlags = DynamicMask | SpiedMask;
|
|
|
|
ncp = cl->ClCode;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cl->ClFlags = DynamicMask;
|
|
|
|
ncp = cl->ClCode;
|
|
|
|
}
|
|
|
|
INIT_LOCK(cl->ClLock);
|
|
|
|
INIT_CLREF_COUNT(cl);
|
|
|
|
/* next, set the first instruction to execute in the dyamic
|
|
|
|
* predicate */
|
|
|
|
if (spy_flag)
|
|
|
|
p->OpcodeOfPred = ncp->opc = opcode(_spy_or_trymark);
|
|
|
|
else
|
|
|
|
p->OpcodeOfPred = ncp->opc = opcode(_try_and_mark);
|
|
|
|
ncp->u.ld.s = p->ArityOfPE;
|
2002-05-16 21:33:00 +01:00
|
|
|
ncp->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
ncp->u.ld.d = cp;
|
|
|
|
#ifdef YAPOR
|
|
|
|
INIT_YAMOP_LTT(ncp, 1);
|
|
|
|
PUT_YAMOP_SEQ(ncp);
|
|
|
|
#endif /* YAPOR */
|
|
|
|
/* This is the point we enter the code */
|
|
|
|
p->TrueCodeOfPred = p->CodeOfPred = (CODEADDR)ncp;
|
|
|
|
/* set the first clause to have a retry and mark which will
|
|
|
|
* backtrack to the previous block */
|
|
|
|
if (p->PredFlags & ProfiledPredFlag)
|
|
|
|
((yamop *)cp)->opc = opcode(_profiled_retry_and_mark);
|
|
|
|
else
|
|
|
|
((yamop *)cp)->opc = opcode(_retry_and_mark);
|
|
|
|
((yamop *)cp)->u.ld.s = p->ArityOfPE;
|
2002-05-16 21:33:00 +01:00
|
|
|
((yamop *)cp)->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
((yamop *)cp)->u.ld.d = (CODEADDR)ncp;
|
|
|
|
#ifdef KEEP_ENTRY_AGE
|
|
|
|
/* also, keep a backpointer for the days you delete the clause */
|
|
|
|
ClauseCodeToClause(cp)->u.ClPrevious = ncp;
|
|
|
|
#endif
|
|
|
|
/* Don't forget to say who is the only clause for the predicate so
|
|
|
|
far */
|
|
|
|
p->LastClause = p->FirstClause = cp;
|
|
|
|
/* we're only missing what to do when we actually exit the procedure
|
|
|
|
*/
|
|
|
|
ncp = NEXTOP(ncp,ld);
|
|
|
|
/* and the last instruction to execute to exit the predicate, note
|
|
|
|
the retry is pointing to this pseudo clause */
|
|
|
|
ncp->opc = opcode(_trust_fail);
|
|
|
|
/* we're only missing what to do when we actually exit the procedure
|
|
|
|
*/
|
|
|
|
/* and close the code */
|
|
|
|
ncp = NEXTOP(ncp,e);
|
|
|
|
ncp->opc = opcode(_Ystop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p is already locked */
|
|
|
|
static void
|
|
|
|
asserta_stat_clause(PredEntry *p, CODEADDR cp, int spy_flag)
|
|
|
|
{
|
|
|
|
yamop *q = (yamop *)cp;
|
|
|
|
q->u.ld.d = p->FirstClause;
|
2002-05-16 21:33:00 +01:00
|
|
|
q->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
|
|
|
PUT_YAMOP_LTT(q, YAMOP_LTT((yamop *)(p->FirstClause)) + 1);
|
|
|
|
#endif /* YAPOR */
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(p))
|
|
|
|
q->opc = opcode(_table_try_me);
|
|
|
|
else
|
|
|
|
#endif /* TABLING */
|
|
|
|
q->opc = opcode(TRYCODE(_try_me, _try_me0, PredArity(p)));
|
|
|
|
q = (yamop *)(p->FirstClause);
|
|
|
|
if (p->PredFlags & ProfiledPredFlag) {
|
|
|
|
if (p->FirstClause == p->LastClause)
|
|
|
|
q->opc = opcode(_profiled_trust_me);
|
|
|
|
else
|
|
|
|
q->opc = opcode(_profiled_retry_me);
|
|
|
|
} else {
|
|
|
|
if (p->FirstClause == p->LastClause) {
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(p))
|
|
|
|
q->opc = opcode(_table_trust_me);
|
|
|
|
else
|
|
|
|
#endif /* TABLING */
|
|
|
|
q->opc = opcode(TRYCODE(_trust_me, _trust_me0, PredArity(p)));
|
|
|
|
} else {
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(p))
|
|
|
|
q->opc = opcode(_table_retry_me);
|
|
|
|
else
|
|
|
|
#endif /* TABLING */
|
|
|
|
q->opc = opcode(TRYCODE(_retry_me, _retry_me0, PredArity(p)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p->TrueCodeOfPred = p->FirstClause = cp;
|
|
|
|
q = ((yamop *)p->LastClause);
|
|
|
|
q->u.ld.d = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p is already locked */
|
|
|
|
static void
|
|
|
|
asserta_dynam_clause(PredEntry *p, CODEADDR cp)
|
|
|
|
{
|
|
|
|
yamop *q;
|
|
|
|
q = (yamop *)cp;
|
|
|
|
LOCK(ClauseCodeToClause(p->FirstClause)->ClLock);
|
|
|
|
#ifdef KEEP_ENTRY_AGE
|
|
|
|
/* also, keep backpointers for the days we'll delete all the clause */
|
|
|
|
ClauseCodeToClause(p->FirstClause)->u.ClPrevious = q;
|
|
|
|
ClauseCodeToClause(cp)->u.ClPrevious = (yamop *)(p->CodeOfPred);
|
|
|
|
#endif
|
|
|
|
UNLOCK(ClauseCodeToClause(p->FirstClause)->ClLock);
|
|
|
|
q->u.ld.d = p->FirstClause;
|
|
|
|
q->u.ld.s = p->ArityOfPE;
|
2002-05-16 21:33:00 +01:00
|
|
|
q->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (p->PredFlags & ProfiledPredFlag)
|
|
|
|
((yamop *)cp)->opc = opcode(_retry_and_mark);
|
|
|
|
else
|
|
|
|
((yamop *)cp)->opc = opcode(_profiled_retry_and_mark);
|
|
|
|
((yamop *)cp)->u.ld.s = p->ArityOfPE;
|
2002-05-16 21:33:00 +01:00
|
|
|
((yamop *)cp)->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
p->FirstClause = cp;
|
|
|
|
q = (yamop *)p->CodeOfPred;
|
|
|
|
q->u.ld.d = cp;
|
|
|
|
q->u.ld.s = p->ArityOfPE;
|
2002-05-16 21:33:00 +01:00
|
|
|
q->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* p is already locked */
|
|
|
|
static void
|
|
|
|
assertz_stat_clause(PredEntry *p, CODEADDR cp, int spy_flag)
|
|
|
|
{
|
|
|
|
yamop *pt;
|
|
|
|
pt = (yamop *)(p->LastClause);
|
|
|
|
if (p->PredFlags & ProfiledPredFlag) {
|
|
|
|
if (p->FirstClause == p->LastClause) {
|
|
|
|
pt->opc = opcode(TRYCODE(_try_me, _try_me0, PredArity(p)));
|
|
|
|
p->TrueCodeOfPred = p->FirstClause;
|
|
|
|
} else
|
|
|
|
pt->opc = opcode(_profiled_retry_me);
|
|
|
|
} else {
|
|
|
|
if (p->FirstClause == p->LastClause) {
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(p))
|
|
|
|
pt->opc = opcode(_table_try_me);
|
|
|
|
else
|
|
|
|
#endif /* TABLING */
|
|
|
|
pt->opc = opcode(TRYCODE(_try_me, _try_me0, PredArity(p)));
|
|
|
|
p->TrueCodeOfPred = p->FirstClause;
|
|
|
|
} else {
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(p))
|
|
|
|
pt->opc = opcode(_table_retry_me);
|
|
|
|
else
|
|
|
|
#endif /* TABLING */
|
|
|
|
pt->opc = opcode(TRYCODE(_retry_me, _retry_me0, PredArity(p)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pt->u.ld.d = cp;
|
|
|
|
p->LastClause = cp;
|
|
|
|
pt = (yamop *)cp;
|
|
|
|
if (p->PredFlags & ProfiledPredFlag) {
|
|
|
|
pt->opc = opcode(_profiled_trust_me);
|
|
|
|
} else {
|
|
|
|
#ifdef TABLING
|
|
|
|
if (is_tabled(p))
|
|
|
|
pt->opc = opcode(_table_trust_me);
|
|
|
|
else
|
|
|
|
#endif /* TABLING */
|
|
|
|
pt->opc = opcode(TRYCODE(_trust_me, _trust_me0, PredArity(p)));
|
|
|
|
}
|
|
|
|
pt->u.ld.d = p->FirstClause;
|
|
|
|
#ifdef YAPOR
|
|
|
|
{
|
|
|
|
CODEADDR code;
|
|
|
|
|
|
|
|
code = p->FirstClause;
|
|
|
|
while (code != p->LastClause){
|
|
|
|
PUT_YAMOP_LTT((yamop *)code, YAMOP_LTT((yamop *)code) + 1);
|
|
|
|
code = NextClause(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* YAPOR */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p is already locked */
|
|
|
|
static void
|
|
|
|
assertz_dynam_clause(PredEntry *p, CODEADDR cp)
|
|
|
|
{
|
|
|
|
yamop *q;
|
|
|
|
|
|
|
|
q = (yamop *)(p->LastClause);
|
|
|
|
LOCK(ClauseCodeToClause(q)->ClLock);
|
|
|
|
q->u.ld.d = cp;
|
|
|
|
p->LastClause = cp;
|
|
|
|
#ifdef KEEP_ENTRY_AGE
|
|
|
|
/* also, keep backpointers for the days we'll delete all the clause */
|
|
|
|
ClauseCodeToClause(cp)->u.ClPrevious = q;
|
|
|
|
#endif
|
|
|
|
UNLOCK(ClauseCodeToClause(q)->ClLock);
|
|
|
|
q = (yamop *)cp;
|
|
|
|
if (p->PredFlags & ProfiledPredFlag)
|
|
|
|
q->opc = opcode(_profiled_retry_and_mark);
|
|
|
|
else
|
|
|
|
q->opc = opcode(_retry_and_mark);
|
|
|
|
q->u.ld.d = p->CodeOfPred;
|
|
|
|
q->u.ld.s = p->ArityOfPE;
|
2002-05-16 21:33:00 +01:00
|
|
|
q->u.ld.p = p;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void expand_consult(void)
|
|
|
|
{
|
2002-01-01 05:26:25 +00:00
|
|
|
consult_obj *new_cl, *new_cb, *new_cs;
|
|
|
|
UInt OldConsultCapacity = ConsultCapacity;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-01-01 05:26:25 +00:00
|
|
|
/* now double consult capacity */
|
2001-04-09 20:54:03 +01:00
|
|
|
ConsultCapacity += InitialConsultCapacity;
|
|
|
|
/* I assume it always works ;-) */
|
2002-02-27 02:10:01 +00:00
|
|
|
while ((new_cl = (consult_obj *)AllocCodeSpace(sizeof(consult_obj)*ConsultCapacity)) == NULL) {
|
|
|
|
if (!growheap(FALSE)) {
|
|
|
|
Error(SYSTEM_ERROR,TermNil,"Could not expand consult space: Heap crashed against Stacks");
|
|
|
|
return;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-12-15 04:47:41 +00:00
|
|
|
new_cs = new_cl + (InitialConsultCapacity+1);
|
2002-01-01 05:26:25 +00:00
|
|
|
new_cb = new_cs + (ConsultBase-ConsultSp);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* start copying */
|
2002-01-01 05:26:25 +00:00
|
|
|
memcpy((void *)(new_cs), (void *)(ConsultSp), OldConsultCapacity*sizeof(consult_obj));
|
2001-04-09 20:54:03 +01:00
|
|
|
/* copying done, release old space */
|
|
|
|
FreeCodeSpace((char *)ConsultLow);
|
|
|
|
/* next, set up pointers correctly */
|
|
|
|
ConsultSp = new_cs;
|
|
|
|
ConsultBase = new_cb;
|
|
|
|
ConsultLow = new_cl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p was already locked */
|
|
|
|
static int
|
2001-12-12 19:36:51 +00:00
|
|
|
not_was_reconsulted(PredEntry *p, Term t, int mode)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
register consult_obj *fp;
|
|
|
|
Prop p0 = AbsProp((PropEntry *)p);
|
|
|
|
|
|
|
|
for (fp = ConsultSp; fp < ConsultBase; ++fp)
|
|
|
|
if (fp->p == p0)
|
|
|
|
break;
|
|
|
|
if (fp != ConsultBase)
|
|
|
|
return (FALSE);
|
|
|
|
if (mode) {
|
|
|
|
if (ConsultSp == ConsultLow+1)
|
|
|
|
expand_consult();
|
|
|
|
--ConsultSp;
|
|
|
|
ConsultSp->p = p0;
|
2001-12-12 19:36:51 +00:00
|
|
|
if (ConsultBase[1].mode) /* we are in reconsult mode */ {
|
2002-06-18 06:22:35 +01:00
|
|
|
retract_all(p, static_in_use(p,TRUE));
|
2001-12-12 19:36:51 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (!(p->PredFlags & MultiFileFlag)) {
|
|
|
|
p->OwnerFile = YapConsultingFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (TRUE); /* careful */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-02 16:55:24 +00:00
|
|
|
addcl_permission_error(AtomEntry *ap, Int Arity, int in_use)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t, ti[2];
|
|
|
|
|
|
|
|
ti[0] = MkAtomTerm(AbsAtom(ap));
|
|
|
|
ti[1] = MkIntegerTerm(Arity);
|
|
|
|
t = MkApplTerm(MkFunctor(LookupAtom("/"),2), 2, ti);
|
|
|
|
ErrorMessage = ErrorSay;
|
|
|
|
Error_Term = t;
|
|
|
|
Error_TYPE = PERMISSION_ERROR_MODIFY_STATIC_PROCEDURE;
|
2002-01-02 16:55:24 +00:00
|
|
|
if (in_use) {
|
|
|
|
if (Arity == 0)
|
|
|
|
sprintf(ErrorMessage, "static predicate %s is in use", ap->StrOfAE);
|
|
|
|
else
|
|
|
|
sprintf(ErrorMessage,
|
|
|
|
#if SHORT_INTS
|
|
|
|
"static predicate %s/%ld is in use",
|
|
|
|
#else
|
|
|
|
"static predicate %s/%d is in use",
|
|
|
|
#endif
|
|
|
|
ap->StrOfAE, Arity);
|
|
|
|
} else {
|
|
|
|
if (Arity == 0)
|
|
|
|
sprintf(ErrorMessage, "system predicate %s", ap->StrOfAE);
|
|
|
|
else
|
|
|
|
sprintf(ErrorMessage,
|
2001-04-09 20:54:03 +01:00
|
|
|
#if SHORT_INTS
|
2002-01-02 16:55:24 +00:00
|
|
|
"system predicate %s/%ld",
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-01-02 16:55:24 +00:00
|
|
|
"system predicate %s/%d",
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2002-01-02 16:55:24 +00:00
|
|
|
ap->StrOfAE, Arity);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2001-11-15 00:01:43 +00:00
|
|
|
addclause(Term t, CODEADDR cp, int mode, int mod)
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
* mode 0 assertz 1 consult 2 asserta
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
PredEntry *p;
|
|
|
|
int spy_flag = FALSE;
|
2001-11-15 00:01:43 +00:00
|
|
|
Atom at;
|
|
|
|
UInt Arity;
|
2001-10-30 16:42:05 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsApplTerm(t) && FunctorOfTerm(t) == FunctorAssert)
|
|
|
|
t = ArgOfTerm(1, t);
|
|
|
|
if (IsAtomTerm(t)) {
|
2001-11-15 00:01:43 +00:00
|
|
|
at = AtomOfTerm(t);
|
|
|
|
p = RepPredProp(PredPropByAtom(at, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
Arity = 0;
|
|
|
|
} else {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
Arity = ArityOfFunctor(f);
|
2001-11-15 00:01:43 +00:00
|
|
|
at = NameOfFunctor(f);
|
|
|
|
p = RepPredProp(PredPropByFunc(f, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
PutValue(AtomAbol, TermNil);
|
|
|
|
WRITE_LOCK(p->PRWLock);
|
2001-10-30 16:42:05 +00:00
|
|
|
/* we are redefining a prolog module predicate */
|
2001-12-12 19:36:51 +00:00
|
|
|
if (p->ModuleOfPred == 0 && mod != 0) {
|
2001-11-15 00:01:43 +00:00
|
|
|
WRITE_UNLOCK(p->PRWLock);
|
2002-01-02 16:55:24 +00:00
|
|
|
addcl_permission_error(RepAtom(at), Arity, FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* The only problem we have now is when we need to throw away
|
|
|
|
Indexing blocks
|
|
|
|
*/
|
|
|
|
if (p->PredFlags & IndexedPredFlag) {
|
|
|
|
if (!RemoveIndexation(p)) {
|
|
|
|
/* should never happen */
|
|
|
|
WRITE_UNLOCK(p->PRWLock);
|
2002-01-02 16:55:24 +00:00
|
|
|
addcl_permission_error(RepAtom(at),Arity,TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->PredFlags & SpiedPredFlag)
|
|
|
|
spy_flag = TRUE;
|
|
|
|
if (mode == consult)
|
2001-12-12 19:36:51 +00:00
|
|
|
not_was_reconsulted(p, t, TRUE);
|
2002-03-07 05:47:24 +00:00
|
|
|
/* always check if we have a valid error first */
|
|
|
|
if (ErrorMessage && Error_TYPE == PERMISSION_ERROR_MODIFY_STATIC_PROCEDURE)
|
2002-02-26 21:01:32 +00:00
|
|
|
return;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (!is_dynamic(p)) {
|
|
|
|
Clause *clp = ClauseCodeToClause(cp);
|
|
|
|
clp->ClFlags |= StaticMask;
|
|
|
|
if (compile_mode)
|
|
|
|
p->PredFlags |= CompiledPredFlag | FastPredFlag;
|
|
|
|
else
|
|
|
|
p->PredFlags |= CompiledPredFlag;
|
|
|
|
if ((GetValue(AtomIndex) != TermNil) &&
|
|
|
|
(p->FirstClause != NIL) &&
|
|
|
|
(Arity != 0)) {
|
|
|
|
p->OpcodeOfPred = INDEX_OPCODE;
|
|
|
|
p->CodeOfPred = (CODEADDR)(&(p->OpcodeOfPred));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->FirstClause == NIL) {
|
|
|
|
if (!(p->PredFlags & DynamicPredFlag)) {
|
|
|
|
add_first_static(p, cp, spy_flag);
|
|
|
|
/* make sure we have a place to jump to */
|
|
|
|
if (p->OpcodeOfPred == UNDEF_OPCODE) {
|
|
|
|
p->CodeOfPred = p->TrueCodeOfPred;
|
|
|
|
p->OpcodeOfPred = ((yamop *)(p->CodeOfPred))->opc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
add_first_dynamic(p, cp, spy_flag);
|
|
|
|
}
|
|
|
|
} else if (mode == asserta) {
|
|
|
|
if (p->PredFlags & DynamicPredFlag)
|
|
|
|
asserta_dynam_clause(p, cp);
|
|
|
|
else
|
|
|
|
asserta_stat_clause(p, cp, spy_flag);
|
|
|
|
} else if (p->PredFlags & DynamicPredFlag)
|
|
|
|
assertz_dynam_clause(p, cp);
|
|
|
|
else {
|
|
|
|
assertz_stat_clause(p, cp, spy_flag);
|
|
|
|
if (p->OpcodeOfPred != INDEX_OPCODE &&
|
|
|
|
p->OpcodeOfPred != opcode(_spy_pred)) {
|
|
|
|
p->CodeOfPred = p->TrueCodeOfPred;
|
|
|
|
p->OpcodeOfPred = ((yamop *)(p->CodeOfPred))->opc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WRITE_UNLOCK(p->PRWLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_in_this_f_before(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$in_this_file_before'(N,A,M) */
|
2001-04-09 20:54:03 +01:00
|
|
|
unsigned int arity;
|
|
|
|
Atom at;
|
|
|
|
Term t;
|
|
|
|
register consult_obj *fp;
|
|
|
|
Prop p0;
|
2001-11-15 00:01:43 +00:00
|
|
|
SMALLUNSGN mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-04-03 20:33:38 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG1)) || !IsAtomTerm(t))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
at = AtomOfTerm(t);
|
2002-04-03 20:33:38 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG2)) || !IsIntTerm(t))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
arity = IntOfTerm(t);
|
2002-04-03 20:33:38 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG3)) || !IsAtomTerm(t))
|
2001-11-15 00:01:43 +00:00
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
mod = LookupModule(t);
|
|
|
|
if (arity)
|
|
|
|
p0 = PredPropByFunc(MkFunctor(at, arity),CurrentModule);
|
|
|
|
else
|
|
|
|
p0 = PredPropByAtom(at, CurrentModule);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (ConsultSp == ConsultBase || (fp = ConsultSp)->p == p0)
|
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
fp++;
|
|
|
|
for (; fp < ConsultBase; ++fp)
|
|
|
|
if (fp->p == p0)
|
|
|
|
break;
|
|
|
|
if (fp != ConsultBase)
|
|
|
|
return (TRUE);
|
|
|
|
else
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_first_cl_in_f(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$first_cl_in_file'(+N,+Ar,+Mod) */
|
2001-04-09 20:54:03 +01:00
|
|
|
unsigned int arity;
|
|
|
|
Atom at;
|
|
|
|
Term t;
|
|
|
|
register consult_obj *fp;
|
|
|
|
Prop p0;
|
2001-11-15 00:01:43 +00:00
|
|
|
SMALLUNSGN mod;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-04-03 20:33:38 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG1)) || !IsAtomTerm(t))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
at = AtomOfTerm(t);
|
2002-04-03 20:33:38 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG2)) || !IsIntTerm(t))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
arity = IntOfTerm(t);
|
2002-04-03 20:33:38 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG3)) || !IsAtomTerm(t))
|
2001-11-15 00:01:43 +00:00
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
mod = LookupModule(t);
|
|
|
|
if (arity)
|
|
|
|
p0 = PredPropByFunc(MkFunctor(at, arity),mod);
|
|
|
|
else
|
|
|
|
p0 = PredPropByAtom(at, mod);
|
2001-04-09 20:54:03 +01:00
|
|
|
for (fp = ConsultSp; fp < ConsultBase; ++fp)
|
|
|
|
if (fp->p == p0)
|
|
|
|
break;
|
|
|
|
if (fp != ConsultBase)
|
|
|
|
return (FALSE);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_mk_cl_not_first(void)
|
|
|
|
{ /* '$mk_cl_not_first'(+N,+Ar) */
|
|
|
|
unsigned int arity;
|
|
|
|
Atom at;
|
|
|
|
Term t;
|
|
|
|
Prop p0;
|
|
|
|
|
|
|
|
if (IsVarTerm(t = Deref(ARG1)) && !IsAtomTerm(t))
|
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
at = AtomOfTerm(t);
|
|
|
|
if (IsVarTerm(t = Deref(ARG2)) && !IsIntTerm(t))
|
|
|
|
return (FALSE);
|
|
|
|
else
|
|
|
|
arity = IntOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (arity)
|
|
|
|
p0 = PredPropByFunc(MkFunctor(at, arity),CurrentModule);
|
|
|
|
else
|
|
|
|
p0 = PredPropByAtom(at, CurrentModule);
|
2001-04-09 20:54:03 +01:00
|
|
|
--ConsultSp;
|
|
|
|
ConsultSp->p = p0;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EMACS
|
|
|
|
static int
|
|
|
|
last_clause_number(p)
|
|
|
|
PredEntry *p;
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
CODEADDR q = p->FirstClause;
|
|
|
|
|
|
|
|
if (q == NIL)
|
|
|
|
return (0);
|
|
|
|
while (q != p->LastClause) {
|
|
|
|
q = NextClause(q);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the place where one would add a new clause for the propriety pred_prop
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
where_new_clause(pred_prop, mode)
|
|
|
|
Prop pred_prop;
|
|
|
|
int mode;
|
|
|
|
{
|
|
|
|
PredEntry *p = RepPredProp(pred_prop);
|
|
|
|
|
2001-12-12 19:36:51 +00:00
|
|
|
if (mode == consult && not_was_reconsulted(p, TermNil, FALSE))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (last_clause_number(p) + 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_compile(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$compile'(+C,+Flags, Mod) */
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Term t1 = Deref(ARG2);
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t3 = Deref(ARG3);
|
2001-04-09 20:54:03 +01:00
|
|
|
CODEADDR codeadr;
|
2001-11-15 00:01:43 +00:00
|
|
|
Int mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t1) || !IsIntTerm(t1))
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(t3) || !IsAtomTerm(t3))
|
|
|
|
return (FALSE);
|
|
|
|
mod = LookupModule(t3);
|
|
|
|
codeadr = cclause(t, 2, mod); /* vsc: give the number of arguments
|
2001-04-09 20:54:03 +01:00
|
|
|
to cclause in case there is overflow */
|
|
|
|
t = Deref(ARG1); /* just in case there was an heap overflow */
|
|
|
|
if (!ErrorMessage)
|
2001-11-15 00:01:43 +00:00
|
|
|
addclause(t, codeadr, (int) (IntOfTerm(t1) & 3), mod);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (ErrorMessage) {
|
|
|
|
if (IntOfTerm(t1) & 4) {
|
|
|
|
Error(Error_TYPE, Error_Term,
|
2002-06-17 16:28:01 +01:00
|
|
|
"in line %d, %s", FirstLineInParse(), ErrorMessage);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
Error(Error_TYPE, Error_Term, ErrorMessage);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_compile_dynamic(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$compile_dynamic'(+C,+Flags,Mod,-Ref) */
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Term t1 = Deref(ARG2);
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t3 = Deref(ARG3);
|
2001-04-09 20:54:03 +01:00
|
|
|
Clause *cl;
|
|
|
|
CODEADDR code_adr;
|
|
|
|
int old_optimize;
|
2001-11-15 00:01:43 +00:00
|
|
|
Int mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t1) || !IsIntTerm(t1))
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(t3) || !IsAtomTerm(t3))
|
|
|
|
return (FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
old_optimize = optimizer_on;
|
|
|
|
optimizer_on = FALSE;
|
2001-11-15 00:01:43 +00:00
|
|
|
mod = LookupModule(t3);
|
|
|
|
code_adr = cclause(t, 3, mod); /* vsc: give the number of arguments to
|
2001-04-09 20:54:03 +01:00
|
|
|
cclause() in case there is a overflow */
|
|
|
|
t = Deref(ARG1); /* just in case there was an heap overflow */
|
|
|
|
if (!ErrorMessage) {
|
|
|
|
|
|
|
|
optimizer_on = old_optimize;
|
|
|
|
cl = ClauseCodeToClause(code_adr);
|
2001-11-15 00:01:43 +00:00
|
|
|
addclause(t, code_adr, (int) (IntOfTerm(t1) & 3), mod);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (ErrorMessage) {
|
|
|
|
if (IntOfTerm(t1) & 4) {
|
2002-06-17 16:28:01 +01:00
|
|
|
Error(Error_TYPE, Error_Term, "line %d, %s", FirstLineInParse(), ErrorMessage);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
Error(Error_TYPE, Error_Term, ErrorMessage);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
cl = ClauseCodeToClause(code_adr);
|
|
|
|
if (!(cl->ClFlags & LogUpdMask))
|
|
|
|
cl->ClFlags = DynamicMask;
|
|
|
|
t = MkIntegerTerm((Int)code_adr);
|
2001-11-15 00:01:43 +00:00
|
|
|
return(unify(ARG4, t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int consult_level = 0;
|
|
|
|
|
|
|
|
Atom
|
|
|
|
YapConsultingFile (void)
|
|
|
|
{
|
|
|
|
if (consult_level == 0) {
|
|
|
|
return(LookupAtom("user"));
|
|
|
|
} else {
|
|
|
|
return(LookupAtom(ConsultBase[2].filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* consult file *file*, *mode* may be one of either consult or reconsult */
|
|
|
|
void
|
|
|
|
init_consult(int mode, char *file)
|
|
|
|
{
|
|
|
|
ConsultSp--;
|
|
|
|
ConsultSp->filename = file;
|
|
|
|
ConsultSp--;
|
|
|
|
ConsultSp->mode = mode;
|
|
|
|
ConsultSp--;
|
2002-01-01 05:26:25 +00:00
|
|
|
ConsultSp->c = (ConsultBase-ConsultSp);
|
2001-04-09 20:54:03 +01:00
|
|
|
ConsultBase = ConsultSp;
|
|
|
|
#if !defined(YAPOR) && !defined(SBA)
|
2002-02-26 15:51:54 +00:00
|
|
|
/* if (consult_level == 0)
|
|
|
|
do_toggle_static_predicates_in_use(TRUE); */
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
consult_level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_startconsult(void)
|
|
|
|
{ /* '$start_consult'(+Mode) */
|
|
|
|
Term t;
|
|
|
|
char *smode = RepAtom(AtomOfTerm(Deref(ARG1)))->StrOfAE;
|
|
|
|
int mode;
|
|
|
|
|
|
|
|
mode = strcmp("consult",smode);
|
|
|
|
init_consult(mode, RepAtom(AtomOfTerm(Deref(ARG2)))->StrOfAE);
|
|
|
|
t = MkIntTerm(consult_level);
|
|
|
|
return (unify_constant(ARG3, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_showconslultlev(void)
|
|
|
|
{
|
|
|
|
Term t;
|
|
|
|
|
|
|
|
t = MkIntTerm(consult_level);
|
|
|
|
return (unify_constant(ARG1, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
end_consult(void)
|
|
|
|
{
|
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
|
|
|
consult_obj *fp;
|
|
|
|
|
|
|
|
/* force indexing for static and dynamic update predicates
|
|
|
|
after consult and not when all hell may break loose ! */
|
|
|
|
for (fp = ConsultSp; fp < ConsultBase; ++fp) {
|
|
|
|
PredEntry *pred = RepPredProp(fp->p);
|
|
|
|
WRITE_LOCK(pred->PRWLock);
|
|
|
|
if (pred->OpcodeOfPred == INDEX_OPCODE) {
|
|
|
|
IPred((CODEADDR)pred);
|
|
|
|
/* IPred does the unlocking */
|
|
|
|
} else {
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ConsultSp = ConsultBase;
|
2002-01-01 05:26:25 +00:00
|
|
|
ConsultBase = ConsultSp+ConsultSp->c;
|
2001-04-09 20:54:03 +01:00
|
|
|
ConsultSp += 3;
|
|
|
|
consult_level--;
|
|
|
|
#if !defined(YAPOR) && !defined(SBA)
|
2002-02-26 15:51:54 +00:00
|
|
|
/* if (consult_level == 0)
|
|
|
|
do_toggle_static_predicates_in_use(FALSE);*/
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_endconsult(void)
|
|
|
|
{ /* '$end_consult' */
|
|
|
|
end_consult();
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_purge_clauses(void)
|
|
|
|
{ /* '$purge_clauses'(+Func) */
|
|
|
|
PredEntry *pred;
|
|
|
|
Term t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2 = Deref(ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
CODEADDR q, q1;
|
2002-06-18 06:22:35 +01:00
|
|
|
SMALLUNSGN mod;
|
|
|
|
int in_use;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
PutValue(AtomAbol, MkAtomTerm(AtomNil));
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(t2) || !IsAtomTerm(t2)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
mod = LookupModule(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsAtomTerm(t)) {
|
2001-10-03 14:39:16 +01:00
|
|
|
Atom at = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pred = RepPredProp(PredPropByAtom(at, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
|
|
|
Functor fun = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pred = RepPredProp(PredPropByFunc(fun, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
|
|
|
WRITE_LOCK(pred->PRWLock);
|
|
|
|
if (pred->PredFlags & StandardPredFlag) {
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
Error(PERMISSION_ERROR_MODIFY_STATIC_PROCEDURE, t, "assert/1");
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (pred->PredFlags & IndexedPredFlag)
|
|
|
|
RemoveIndexation(pred);
|
|
|
|
PutValue(AtomAbol, MkAtomTerm(AtomTrue));
|
|
|
|
q = pred->FirstClause;
|
2002-06-18 06:22:35 +01:00
|
|
|
in_use = static_in_use(pred,FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (q != NIL)
|
|
|
|
do {
|
|
|
|
q1 = q;
|
|
|
|
q = NextClause(q);
|
|
|
|
if (pred->PredFlags & LogUpdatePredFlag)
|
|
|
|
ErCl(ClauseCodeToClause(q1));
|
2002-06-11 06:30:47 +01:00
|
|
|
else {
|
|
|
|
Clause *cl = ClauseCodeToClause(q1);
|
2002-06-18 06:22:35 +01:00
|
|
|
if (cl->ClFlags & HasBlobsMask || in_use) {
|
2002-06-11 06:30:47 +01:00
|
|
|
cl->u.NextCl = DeadClauses;
|
|
|
|
DeadClauses = cl;
|
|
|
|
} else {
|
|
|
|
FreeCodeSpace((char *)cl);
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
} while (q1 != pred->LastClause);
|
|
|
|
pred->FirstClause = pred->LastClause = NIL;
|
|
|
|
pred->OpcodeOfPred = UNDEF_OPCODE;
|
|
|
|
pred->TrueCodeOfPred =
|
|
|
|
pred->CodeOfPred =
|
|
|
|
(CODEADDR)(&(pred->OpcodeOfPred));
|
|
|
|
pred->OwnerFile = AtomNil;
|
|
|
|
if (pred->PredFlags & MultiFileFlag)
|
|
|
|
pred->PredFlags ^= MultiFileFlag;
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
|
|
|
|
MANAGING SPY-POINTS
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_setspy(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$set_spy'(+Fun,+M) */
|
2001-04-09 20:54:03 +01:00
|
|
|
Atom at;
|
|
|
|
PredEntry *pred;
|
|
|
|
CELL fg;
|
|
|
|
Term t;
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2;
|
|
|
|
SMALLUNSGN mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
at = FullLookupAtom("$spy");
|
2001-11-15 00:01:43 +00:00
|
|
|
pred = RepPredProp(PredPropByFunc(MkFunctor(at, 1),0));
|
2001-10-30 16:42:05 +00:00
|
|
|
SpyCode = pred;
|
2001-04-09 20:54:03 +01:00
|
|
|
t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
t2 = Deref(ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsVarTerm(t))
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(t2) || !IsAtomTerm(t2))
|
|
|
|
return (FALSE);
|
|
|
|
mod = LookupModule(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsAtomTerm(t)) {
|
2001-10-03 14:39:16 +01:00
|
|
|
Atom at = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pred = RepPredProp(PredPropByAtom(at, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
2001-10-03 14:39:16 +01:00
|
|
|
Functor fun = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pred = RepPredProp(PredPropByFunc(fun, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
restart_spy:
|
|
|
|
WRITE_LOCK(pred->PRWLock);
|
|
|
|
if (pred->PredFlags & (CPredFlag | SafePredFlag)) {
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (pred->OpcodeOfPred == UNDEF_OPCODE) {
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (pred->OpcodeOfPred == INDEX_OPCODE) {
|
|
|
|
IPred((CODEADDR)pred);
|
|
|
|
goto restart_spy;
|
|
|
|
}
|
|
|
|
fg = pred->PredFlags;
|
|
|
|
if (fg & DynamicPredFlag) {
|
|
|
|
pred->OpcodeOfPred =
|
|
|
|
((yamop *)(pred->CodeOfPred))->opc =
|
|
|
|
opcode(_spy_or_trymark);
|
|
|
|
} else {
|
|
|
|
pred->OpcodeOfPred = opcode(_spy_pred);
|
|
|
|
pred->CodeOfPred = (CODEADDR)(&(pred->OpcodeOfPred));
|
|
|
|
}
|
|
|
|
pred->StateOfPred |= SpiedMask;
|
|
|
|
pred->PredFlags |= SpiedPredFlag;
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_rmspy(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$rm_spy'(+T,+Mod) */
|
2001-04-09 20:54:03 +01:00
|
|
|
Atom at;
|
|
|
|
PredEntry *pred;
|
|
|
|
Term t;
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2;
|
|
|
|
SMALLUNSGN mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
t2 = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t2) || !IsAtomTerm(t2))
|
|
|
|
return (FALSE);
|
|
|
|
mod = LookupModule(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsVarTerm(t))
|
|
|
|
return (FALSE);
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
at = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pred = RepPredProp(PredPropByAtom(at, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
2001-10-03 14:39:16 +01:00
|
|
|
Functor fun = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pred = RepPredProp(PredPropByFunc(fun, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
|
|
|
WRITE_LOCK(pred->PRWLock);
|
|
|
|
if (!(pred->PredFlags & SpiedPredFlag)) {
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!(pred->PredFlags & DynamicPredFlag)) {
|
|
|
|
if ((pred->StateOfPred ^= SpiedMask) & InUseMask)
|
|
|
|
pred->CodeOfPred = pred->TrueCodeOfPred;
|
|
|
|
else
|
|
|
|
pred->CodeOfPred = pred->TrueCodeOfPred;
|
|
|
|
pred->OpcodeOfPred = ((yamop *)(pred->CodeOfPred))->opc;
|
|
|
|
} else if (pred->OpcodeOfPred == opcode(_spy_or_trymark)) {
|
|
|
|
pred->OpcodeOfPred = opcode(_try_and_mark);
|
|
|
|
} else
|
|
|
|
return (FALSE);
|
|
|
|
pred->PredFlags ^= SpiedPredFlag;
|
|
|
|
WRITE_UNLOCK(pred->PRWLock);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
|
|
|
|
INFO ABOUT PREDICATES
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_number_of_clauses(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$number_of_clauses'(Predicate,M,N) */
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2 = Deref(ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
int ncl = 0;
|
|
|
|
Prop pe;
|
|
|
|
CODEADDR q;
|
|
|
|
int testing;
|
2001-11-15 00:01:43 +00:00
|
|
|
int mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(t2) || !IsAtomTerm(t2)) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
mod = LookupModule(t2);
|
2001-10-03 14:39:16 +01:00
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
Atom a = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = PredPropByAtom(a, mod);
|
2001-10-03 14:39:16 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
register Functor f = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = PredPropByFunc(f, mod);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
|
|
|
q = RepPredProp(pe)->FirstClause;
|
|
|
|
READ_LOCK(RepPredProp(pe)->PRWLock);
|
|
|
|
if (q != NIL) {
|
|
|
|
if (RepPredProp(pe)->PredFlags & DynamicPredFlag)
|
|
|
|
testing = TRUE;
|
|
|
|
else
|
|
|
|
testing = FALSE;
|
|
|
|
while (q != RepPredProp(pe)->LastClause) {
|
|
|
|
if (!testing ||
|
|
|
|
!(ClauseCodeToClause(q)->ClFlags & ErasedMask))
|
|
|
|
ncl++;
|
|
|
|
q = NextClause(q);
|
|
|
|
}
|
|
|
|
if (!testing ||
|
|
|
|
!(ClauseCodeToClause(q)->ClFlags & ErasedMask))
|
|
|
|
ncl++;
|
|
|
|
}
|
|
|
|
READ_UNLOCK(RepPredProp(pe)->PRWLock);
|
|
|
|
t = MkIntTerm(ncl);
|
|
|
|
return (unify_constant(ARG3, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_in_use(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$in_use'(+P,+Mod) */
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2 = Deref(ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
PredEntry *pe;
|
|
|
|
Int out;
|
2001-11-15 00:01:43 +00:00
|
|
|
int mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(t2) || !IsAtomTerm(t2))
|
|
|
|
return (FALSE);
|
|
|
|
mod = LookupModule(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsAtomTerm(t)) {
|
2001-10-03 14:39:16 +01:00
|
|
|
Atom at = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(PredPropByAtom(at, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
|
|
|
Functor fun = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(PredPropByFunc(fun, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
|
|
|
READ_LOCK(pe->PRWLock);
|
|
|
|
out = static_in_use(pe,TRUE);
|
|
|
|
READ_UNLOCK(pe->PRWLock);
|
|
|
|
return(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_new_multifile(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$new_multifile'(+N,+Ar,+Mod) */
|
2001-04-09 20:54:03 +01:00
|
|
|
Atom at;
|
|
|
|
int arity;
|
|
|
|
PredEntry *pe;
|
|
|
|
Term t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
SMALLUNSGN mod = LookupModule(Deref(ARG3));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return (FALSE);
|
|
|
|
if (IsAtomTerm(t))
|
|
|
|
at = AtomOfTerm(t);
|
|
|
|
else
|
|
|
|
return (FALSE);
|
|
|
|
t = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return (FALSE);
|
|
|
|
if (IsIntTerm(t))
|
|
|
|
arity = IntOfTerm(t);
|
|
|
|
else
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (arity == 0)
|
|
|
|
pe = RepPredProp(PredPropByAtom(at, mod));
|
|
|
|
else
|
|
|
|
pe = RepPredProp(PredPropByFunc(MkFunctor(at, arity),mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_LOCK(pe->PRWLock);
|
|
|
|
pe->PredFlags |= MultiFileFlag;
|
|
|
|
WRITE_UNLOCK(pe->PRWLock);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_is_multifile(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$is_multifile'(+S,+Mod) */
|
2001-04-09 20:54:03 +01:00
|
|
|
PredEntry *pe;
|
|
|
|
Term t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2 = Deref(ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
Int out;
|
2001-11-15 00:01:43 +00:00
|
|
|
int mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(t2))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (!IsAtomTerm(t2))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
mod = LookupModule(t2);
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
pe = RepPredProp(PredPropByAtom(AtomOfTerm(t), mod));
|
|
|
|
} else if (IsApplTerm(t)) {
|
|
|
|
pe = RepPredProp(PredPropByFunc(FunctorOfTerm(t), mod));
|
|
|
|
} else
|
|
|
|
return(FALSE);
|
|
|
|
if (EndOfPAEntr(pe))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
READ_LOCK(pe->PRWLock);
|
|
|
|
out = (pe->PredFlags & MultiFileFlag);
|
|
|
|
READ_UNLOCK(pe->PRWLock);
|
|
|
|
return(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_is_dynamic(void)
|
|
|
|
{ /* '$is_dynamic'(+P) */
|
|
|
|
PredEntry *pe;
|
|
|
|
Term t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2 = Deref(ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
Int out;
|
2001-11-15 00:01:43 +00:00
|
|
|
SMALLUNSGN mod = LookupModule(t2);
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
return (FALSE);
|
|
|
|
} else if (IsAtomTerm(t)) {
|
2001-10-03 14:39:16 +01:00
|
|
|
Atom at = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(PredPropByAtom(at, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
|
|
|
Functor fun = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(PredPropByFunc(fun, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
|
|
|
if (pe == NIL)
|
|
|
|
return (FALSE);
|
|
|
|
READ_LOCK(pe->PRWLock);
|
|
|
|
out = (pe->PredFlags & (DynamicPredFlag|LogUpdatePredFlag));
|
|
|
|
READ_UNLOCK(pe->PRWLock);
|
|
|
|
return(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_set_pred_module(void)
|
|
|
|
{ /* '$set_pred_module'(+P,+Mod) */
|
|
|
|
PredEntry *pe;
|
|
|
|
Term t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
SMALLUNSGN mod = CurrentModule;
|
|
|
|
|
|
|
|
restart_set_pred:
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
return (FALSE);
|
|
|
|
} else if (IsAtomTerm(t)) {
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(PredPropByAtom(AtomOfTerm(t), mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
|
|
|
Functor fun = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (fun == FunctorModule) {
|
|
|
|
Term tmod = ArgOfTerm(1, t);
|
|
|
|
if (IsVarTerm(tmod) ) {
|
|
|
|
Error(INSTANTIATION_ERROR,ARG1,"set_pred_module/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(tmod) ) {
|
|
|
|
Error(TYPE_ERROR_ATOM,ARG1,"set_pred_module/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
mod = LookupModule(tmod);
|
|
|
|
t = ArgOfTerm(2, t);
|
|
|
|
goto restart_set_pred;
|
|
|
|
}
|
|
|
|
pe = RepPredProp(PredPropByFunc(fun, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (EndOfPAEntr(pe))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (FALSE);
|
|
|
|
WRITE_LOCK(pe->PRWLock);
|
2001-10-30 16:42:05 +00:00
|
|
|
{
|
|
|
|
SMALLUNSGN mod = LookupModule(Deref(ARG2));
|
2001-11-15 00:01:43 +00:00
|
|
|
pe->ModuleOfPred = mod;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(pe->PRWLock);
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_undefined(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$undefined'(P,Mod) */
|
2001-04-09 20:54:03 +01:00
|
|
|
PredEntry *pe;
|
|
|
|
Term t;
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2;
|
|
|
|
SMALLUNSGN mod;
|
2001-10-30 16:42:05 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
t = Deref(ARG1);
|
2001-11-15 00:01:43 +00:00
|
|
|
t2 = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t2)) {
|
|
|
|
Error(INSTANTIATION_ERROR,ARG2,"undefined/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t2)) {
|
|
|
|
Error(TYPE_ERROR_ATOM,ARG2,"undefined/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
mod = LookupModule(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
restart_undefined:
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Error(INSTANTIATION_ERROR,ARG1,"undefined/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (IsAtomTerm(t)) {
|
2001-10-03 14:39:16 +01:00
|
|
|
Atom at = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(GetPredPropByAtom(at,mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
|
|
|
Functor funt = FunctorOfTerm(t);
|
|
|
|
if (funt == FunctorModule) {
|
2001-11-15 00:01:43 +00:00
|
|
|
Term tmod = ArgOfTerm(1, t);
|
|
|
|
if (IsVarTerm(tmod) ) {
|
2001-10-30 16:42:05 +00:00
|
|
|
Error(INSTANTIATION_ERROR,ARG1,"undefined/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2001-11-15 00:01:43 +00:00
|
|
|
if (!IsAtomTerm(tmod) ) {
|
2001-10-30 16:42:05 +00:00
|
|
|
Error(TYPE_ERROR_ATOM,ARG1,"undefined/1");
|
|
|
|
return(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-11-15 00:01:43 +00:00
|
|
|
mod = LookupModule(tmod);
|
2001-10-30 16:42:05 +00:00
|
|
|
t = ArgOfTerm(2, t);
|
|
|
|
goto restart_undefined;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(GetPredPropByFunc(funt, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (pe == RepPredProp(NIL))
|
|
|
|
return (TRUE);
|
|
|
|
READ_LOCK(pe->PRWLock);
|
2002-05-28 17:26:00 +01:00
|
|
|
if (pe->PredFlags & (CPredFlag|UserCPredFlag|TestPredFlag|AsmPredFlag|DynamicPredFlag)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
READ_UNLOCK(pe->PRWLock);
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (pe->OpcodeOfPred == UNDEF_OPCODE) {
|
|
|
|
READ_UNLOCK(pe->PRWLock);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
READ_UNLOCK(pe->PRWLock);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this predicate should only be called when all clauses for the dynamic
|
|
|
|
* predicate were remove, otherwise chaos will follow!!
|
|
|
|
*/
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_kill_dynamic(void)
|
2001-11-15 00:01:43 +00:00
|
|
|
{ /* '$kill_dynamic'(P,M) */
|
2001-04-09 20:54:03 +01:00
|
|
|
PredEntry *pe;
|
|
|
|
Term t;
|
2001-11-15 00:01:43 +00:00
|
|
|
Term t2;
|
|
|
|
SMALLUNSGN mod;
|
|
|
|
|
|
|
|
t2 = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t2)) {
|
|
|
|
Error(INSTANTIATION_ERROR,ARG2,"undefined/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t2)) {
|
|
|
|
Error(TYPE_ERROR_ATOM,ARG2,"undefined/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
mod = LookupModule(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
t = Deref(ARG1);
|
2001-10-03 14:39:16 +01:00
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
Atom at = AtomOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(PredPropByAtom(at, mod));
|
2001-10-03 14:39:16 +01:00
|
|
|
} else if (IsApplTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Functor funt = FunctorOfTerm(t);
|
2001-11-15 00:01:43 +00:00
|
|
|
pe = RepPredProp(PredPropByFunc(funt, mod));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (EndOfPAEntr(pe))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (TRUE);
|
|
|
|
WRITE_LOCK(pe->PRWLock);
|
|
|
|
if (!(pe->PredFlags & DynamicPredFlag)) {
|
|
|
|
WRITE_UNLOCK(pe->PRWLock);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (pe->LastClause != pe->FirstClause) {
|
|
|
|
WRITE_UNLOCK(pe->PRWLock);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
pe->LastClause = pe->FirstClause = NIL;
|
|
|
|
pe->OpcodeOfPred = UNDEF_OPCODE;
|
|
|
|
pe->TrueCodeOfPred = pe->CodeOfPred = (CODEADDR)(&(pe->OpcodeOfPred));
|
|
|
|
pe->PredFlags = 0L;
|
|
|
|
WRITE_UNLOCK(pe->PRWLock);
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_optimizer_on(void)
|
|
|
|
{ /* '$optimizer_on' */
|
|
|
|
optimizer_on = TRUE;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_optimizer_off(void)
|
|
|
|
{ /* '$optimizer_off' */
|
|
|
|
optimizer_on = FALSE;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_compile_mode(void)
|
|
|
|
{ /* $compile_mode(Old,New) */
|
|
|
|
Term t2, t3 = MkIntTerm(compile_mode);
|
|
|
|
if (!unify_constant(ARG1, t3))
|
|
|
|
return (FALSE);
|
|
|
|
t2 = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t2) || !IsIntTerm(t2))
|
|
|
|
return (FALSE);
|
|
|
|
compile_mode = IntOfTerm(t2) & 1;
|
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(YAPOR) && !defined(THREADS)
|
2002-02-26 20:16:36 +00:00
|
|
|
static yamop *next_clause(PredEntry *pe, CODEADDR codeptr)
|
|
|
|
{
|
|
|
|
CODEADDR clcode, cl;
|
|
|
|
clcode = pe->FirstClause;
|
|
|
|
cl = (CODEADDR)ClauseCodeToClause(clcode);
|
|
|
|
do {
|
|
|
|
if (clcode == pe->LastClause)
|
|
|
|
break;
|
|
|
|
if (codeptr > cl && codeptr <= cl + SizeOfBlock(cl)) {
|
|
|
|
return((yamop *)NextClause(clcode));
|
|
|
|
}
|
|
|
|
cl = (CODEADDR)ClauseCodeToClause(clcode = NextClause(clcode));
|
|
|
|
} while (TRUE);
|
|
|
|
Error(SYSTEM_ERROR,TermNil,"could not find clause for indexing code");
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static yamop *cur_clause(PredEntry *pe, CODEADDR codeptr)
|
|
|
|
{
|
|
|
|
CODEADDR clcode, cl;
|
|
|
|
clcode = pe->FirstClause;
|
|
|
|
cl = (CODEADDR)ClauseCodeToClause(clcode);
|
|
|
|
do {
|
|
|
|
if (codeptr > cl && codeptr <= cl + SizeOfBlock(cl)) {
|
|
|
|
return((yamop *)clcode);
|
|
|
|
}
|
|
|
|
if (clcode == pe->LastClause)
|
|
|
|
break;
|
|
|
|
cl = (CODEADDR)ClauseCodeToClause(clcode = NextClause(clcode));
|
|
|
|
} while (TRUE);
|
|
|
|
Error(SYSTEM_ERROR,TermNil,"could not find clause for indexing code");
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
|
|
|
search_for_static_predicate_in_use(PredEntry *p, int check_everything)
|
|
|
|
{
|
|
|
|
choiceptr b_ptr = B;
|
|
|
|
CELL *env_ptr = ENV;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* check first environments that are younger than our latest choicepoint */
|
|
|
|
if (check_everything) {
|
|
|
|
/*
|
|
|
|
I do not need to check environments for asserts,
|
|
|
|
only for retracts
|
|
|
|
*/
|
|
|
|
while (b_ptr > (choiceptr)env_ptr) {
|
|
|
|
PredEntry *pe = EnvPreg(env_ptr[E_CP]);
|
|
|
|
if (p == pe) return(TRUE);
|
|
|
|
if (env_ptr != NULL)
|
|
|
|
env_ptr = (CELL *)(env_ptr[E_E]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* now mark the choicepoint */
|
|
|
|
if (b_ptr != NULL) {
|
|
|
|
PredEntry *pe;
|
|
|
|
op_numbers opnum = op_from_opcode(b_ptr->cp_ap->opc);
|
|
|
|
|
|
|
|
restart_cp:
|
|
|
|
switch(opnum) {
|
|
|
|
case _or_else:
|
|
|
|
case _or_last:
|
|
|
|
if (!check_everything) {
|
|
|
|
b_ptr = b_ptr->cp_b;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef YAPOR
|
2002-02-26 15:51:54 +00:00
|
|
|
pe = b_ptr->cp_cp->u.ldl.p;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-02-26 15:51:54 +00:00
|
|
|
pe = b_ptr->cp_cp->u.sla.p0;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* YAPOR */
|
|
|
|
break;
|
|
|
|
case _retry_profiled:
|
|
|
|
opnum = op_from_opcode(NEXTOP(b_ptr->cp_ap,l)->opc);
|
|
|
|
goto restart_cp;
|
|
|
|
default:
|
|
|
|
pe = (PredEntry *)(b_ptr->cp_ap->u.ld.p);
|
|
|
|
}
|
|
|
|
if (pe == p) {
|
2002-02-26 20:16:36 +00:00
|
|
|
if (check_everything)
|
|
|
|
return(TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
READ_LOCK(pe->PRWLock);
|
|
|
|
if (p->PredFlags & IndexedPredFlag) {
|
|
|
|
CODEADDR code_p = (CODEADDR)(b_ptr->cp_ap);
|
|
|
|
if (code_p >= p->TrueCodeOfPred &&
|
2002-02-26 20:16:36 +00:00
|
|
|
code_p <= p->TrueCodeOfPred + SizeOfBlock((CODEADDR)ClauseCodeToClause(p->TrueCodeOfPred))) {
|
|
|
|
yamop *prev;
|
|
|
|
/* fix the choicepoint */
|
|
|
|
switch(opnum) {
|
|
|
|
case _switch_last:
|
|
|
|
case _switch_l_list:
|
|
|
|
{
|
|
|
|
prev = (yamop *)((CODEADDR)(code_p)-(CELL)NEXTOP((yamop *)NIL,ld));
|
|
|
|
/* previous clause must be a try or a retry */
|
|
|
|
b_ptr->cp_ap = next_clause(pe, prev->u.ld.d);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
b_ptr->cp_ap = cur_clause(pe, b_ptr->cp_ap->u.ld.d);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
READ_UNLOCK(pe->PRWLock);
|
|
|
|
}
|
2002-02-26 20:16:36 +00:00
|
|
|
} else {
|
|
|
|
READ_UNLOCK(pe->PRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
env_ptr = b_ptr->cp_env;
|
|
|
|
b_ptr = b_ptr->cp_b;
|
|
|
|
}
|
|
|
|
} while (b_ptr != NULL);
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
2002-02-26 17:49:09 +00:00
|
|
|
#ifdef DEBUG
|
2002-02-26 15:51:54 +00:00
|
|
|
#ifndef ANALYST
|
|
|
|
|
|
|
|
static char *op_names[_std_top + 1] =
|
|
|
|
{
|
|
|
|
#define OPCODE(OP,TYPE) #OP
|
|
|
|
#include "YapOpcodes.h"
|
|
|
|
#undef OPCODE
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2002-02-26 17:49:09 +00:00
|
|
|
list_all_predicates_in_use(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
choiceptr b_ptr = B;
|
|
|
|
CELL *env_ptr = ENV;
|
|
|
|
|
2002-02-26 17:49:09 +00:00
|
|
|
do {
|
|
|
|
/*
|
|
|
|
I do not need to check environments for asserts,
|
|
|
|
only for retracts
|
|
|
|
*/
|
|
|
|
while (b_ptr > (choiceptr)env_ptr) {
|
|
|
|
PredEntry *pe = EnvPreg(env_ptr[E_CP]);
|
|
|
|
op_numbers op = op_from_opcode(ENV_ToOp(env_ptr[E_CP]));
|
|
|
|
if (pe->ArityOfPE)
|
|
|
|
YP_fprintf(YP_stderr," ENV %p %s/%d %s\n", env_ptr, RepAtom(NameOfFunctor(pe->FunctorOfPred))->StrOfAE, pe->ArityOfPE, op_names[op]);
|
|
|
|
else
|
|
|
|
YP_fprintf(YP_stderr," ENV %p %s %s\n", env_ptr, RepAtom((Atom)(pe->FunctorOfPred))->StrOfAE, op_names[op]);
|
|
|
|
env_ptr = (CELL *)(env_ptr[E_E]);
|
|
|
|
}
|
|
|
|
restart_cp:
|
|
|
|
/* now mark the choicepoint */
|
|
|
|
if (b_ptr != NULL) {
|
|
|
|
op_numbers opnum = op_from_opcode(b_ptr->cp_ap->opc);
|
|
|
|
|
2002-02-26 15:51:54 +00:00
|
|
|
switch (opnum) {
|
|
|
|
case _or_else:
|
|
|
|
case _or_last:
|
|
|
|
case _Nstop:
|
|
|
|
case _switch_last:
|
|
|
|
case _switch_l_list:
|
|
|
|
case _retry_c:
|
|
|
|
case _retry_userc:
|
|
|
|
case _trust_logical_pred:
|
|
|
|
case _retry_profiled:
|
|
|
|
{
|
|
|
|
Atom at;
|
|
|
|
Int arity;
|
|
|
|
SMALLUNSGN mod;
|
|
|
|
if (PredForCode((CODEADDR)b_ptr->cp_ap, &at, &arity, &mod)) {
|
|
|
|
if (arity)
|
2002-02-26 17:49:09 +00:00
|
|
|
YP_fprintf(YP_stderr,"CP %p %s/%d (%s)\n", b_ptr, RepAtom(at)->StrOfAE, arity, op_names[opnum]);
|
2002-02-26 15:51:54 +00:00
|
|
|
else
|
2002-02-26 17:49:09 +00:00
|
|
|
YP_fprintf(YP_stderr,"CP %p %s (%s)\n", b_ptr, RepAtom(at)->StrOfAE, op_names[opnum]);
|
2002-02-26 15:51:54 +00:00
|
|
|
} else
|
2002-02-26 17:49:09 +00:00
|
|
|
YP_fprintf(YP_stderr,"CP %p (%s)\n", op_names[opnum], b_ptr);
|
2002-02-26 15:51:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
PredEntry *pe = (PredEntry *)b_ptr->cp_ap->u.ld.p;
|
|
|
|
if (pe == NULL) {
|
2002-02-26 17:49:09 +00:00
|
|
|
YP_fprintf(YP_stderr,"CP %p (%s)\n", b_ptr, op_names[opnum]);
|
2002-02-26 15:51:54 +00:00
|
|
|
} else
|
|
|
|
if (pe->ArityOfPE)
|
2002-02-26 17:49:09 +00:00
|
|
|
YP_fprintf(YP_stderr,"CP %p %s/%d (%s)\n", b_ptr, RepAtom(NameOfFunctor(pe->FunctorOfPred))->StrOfAE, pe->ArityOfPE, op_names[opnum]);
|
2002-02-26 15:51:54 +00:00
|
|
|
else
|
2002-02-26 17:49:09 +00:00
|
|
|
YP_fprintf(YP_stderr,"CP %p %d (%s)\n", b_ptr, RepAtom((Atom)(pe->FunctorOfPred))->StrOfAE, op_names[opnum]);
|
2002-02-26 15:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-26 17:49:09 +00:00
|
|
|
if (opnum == _retry_profiled) {
|
|
|
|
opnum = op_from_opcode(NEXTOP(b_ptr->cp_ap,l)->opc);
|
|
|
|
goto restart_cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
env_ptr = b_ptr->cp_env;
|
|
|
|
b_ptr = b_ptr->cp_b;
|
|
|
|
} while (b_ptr != NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
mark_pred(int mark, PredEntry *pe)
|
|
|
|
{
|
|
|
|
/* if the predicate is static mark it */
|
|
|
|
if (pe->ModuleOfPred) {
|
|
|
|
WRITE_LOCK(pe->PRWLock);
|
|
|
|
if (mark) {
|
|
|
|
pe->StateOfPred |= InUseMask;
|
|
|
|
} else {
|
|
|
|
pe->StateOfPred &= ~InUseMask;
|
2002-02-26 15:51:54 +00:00
|
|
|
}
|
2002-02-26 17:49:09 +00:00
|
|
|
WRITE_UNLOCK(pe->PRWLock);
|
2002-02-26 15:51:54 +00:00
|
|
|
}
|
2002-02-26 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* go up the chain of choice_points and environments,
|
|
|
|
marking all static predicates that current execution is depending
|
|
|
|
upon */
|
|
|
|
static void
|
|
|
|
do_toggle_static_predicates_in_use(int mask)
|
|
|
|
{
|
|
|
|
choiceptr b_ptr = B;
|
|
|
|
CELL *env_ptr = ENV;
|
|
|
|
|
|
|
|
if (b_ptr == NULL)
|
|
|
|
return;
|
2002-02-26 15:51:54 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
do {
|
2001-12-12 19:36:51 +00:00
|
|
|
PredEntry *pe;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* check first environments that are younger than our latest choicepoint */
|
|
|
|
while (b_ptr > (choiceptr)env_ptr) {
|
|
|
|
PredEntry *pe = EnvPreg(env_ptr[E_CP]);
|
2002-02-26 15:51:54 +00:00
|
|
|
|
2002-02-26 17:49:09 +00:00
|
|
|
mark_pred(mask, pe);
|
2001-04-09 20:54:03 +01:00
|
|
|
env_ptr = (CELL *)(env_ptr[E_E]);
|
|
|
|
}
|
|
|
|
/* now mark the choicepoint */
|
|
|
|
{
|
|
|
|
op_numbers opnum;
|
2001-12-12 19:36:51 +00:00
|
|
|
restart_cp:
|
2001-04-09 20:54:03 +01:00
|
|
|
opnum = op_from_opcode(b_ptr->cp_ap->opc);
|
|
|
|
|
2001-12-12 19:36:51 +00:00
|
|
|
switch(opnum) {
|
|
|
|
case _or_else:
|
|
|
|
case _or_last:
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
2002-02-26 15:51:54 +00:00
|
|
|
pe = b_ptr->cp_cp->u.ldl.p;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-02-26 15:51:54 +00:00
|
|
|
pe = b_ptr->cp_cp->u.sla.p0;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* YAPOR */
|
2001-12-12 19:36:51 +00:00
|
|
|
break;
|
|
|
|
case _Nstop:
|
|
|
|
pe = NULL;
|
|
|
|
break;
|
|
|
|
case _retry_profiled:
|
|
|
|
opnum = op_from_opcode(NEXTOP(b_ptr->cp_ap,l)->opc);
|
|
|
|
goto restart_cp;
|
|
|
|
default:
|
2001-04-09 20:54:03 +01:00
|
|
|
pe = (PredEntry *)(b_ptr->cp_ap->u.ld.p);
|
|
|
|
}
|
2001-12-12 19:36:51 +00:00
|
|
|
if (pe != NULL)
|
2001-04-09 20:54:03 +01:00
|
|
|
mark_pred(mask, pe);
|
|
|
|
env_ptr = b_ptr->cp_env;
|
|
|
|
b_ptr = b_ptr->cp_b;
|
|
|
|
}
|
|
|
|
} while (b_ptr != NULL);
|
2001-12-12 19:36:51 +00:00
|
|
|
/* mark or unmark all predicates */
|
|
|
|
STATIC_PREDICATES_MARKED = mask;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This predicate is to be used by reconsult to mark all predicates
|
|
|
|
currently in use as being executed.
|
|
|
|
|
|
|
|
The idea is to go up the chain of choice_points and environments.
|
|
|
|
|
|
|
|
*/
|
|
|
|
static Int
|
|
|
|
p_toggle_static_predicates_in_use(void)
|
|
|
|
{
|
|
|
|
#if !defined(YAPOR) && !defined(THREADS)
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Int mask;
|
|
|
|
|
|
|
|
/* find out whether we need to mark or unmark */
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Error(INSTANTIATION_ERROR,t,"toggle_static_predicates_in_use/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntTerm(t)) {
|
|
|
|
Error(TYPE_ERROR_INTEGER,t,"toggle_static_predicates_in_use/1");
|
|
|
|
return(FALSE);
|
|
|
|
} else {
|
|
|
|
mask = IntOfTerm(t);
|
|
|
|
}
|
|
|
|
do_toggle_static_predicates_in_use(mask);
|
|
|
|
#endif
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Int
|
2001-12-11 19:12:41 +00:00
|
|
|
code_in_pred(PredEntry *pp, Atom *pat, Int *parity, CODEADDR codeptr) {
|
|
|
|
CODEADDR clcode, cl;
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
READ_LOCK(pp->PRWLock);
|
|
|
|
clcode = pp->FirstClause;
|
|
|
|
if (clcode != NIL) {
|
|
|
|
/* check if the codeptr comes from the indexing code */
|
|
|
|
if ((pp->PredFlags & IndexedPredFlag) &&
|
|
|
|
codeptr > pp->TrueCodeOfPred &&
|
|
|
|
codeptr <= pp->TrueCodeOfPred + SizeOfBlock(pp->TrueCodeOfPred)) {
|
|
|
|
*parity = pp->ArityOfPE;
|
|
|
|
if (pp->ArityOfPE) {
|
|
|
|
*pat = NameOfFunctor(pp->FunctorOfPred);
|
2001-12-12 19:36:51 +00:00
|
|
|
} else {
|
|
|
|
*pat = (Atom)(pp->FunctorOfPred);
|
2001-12-11 19:12:41 +00:00
|
|
|
}
|
|
|
|
READ_UNLOCK(pp->PRWLock);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
cl = (CODEADDR)ClauseCodeToClause(clcode);
|
|
|
|
do {
|
|
|
|
if (codeptr > cl && codeptr <= cl + SizeOfBlock(cl)) {
|
|
|
|
/* we found it */
|
2001-04-09 20:54:03 +01:00
|
|
|
*parity = pp->ArityOfPE;
|
2001-12-11 19:12:41 +00:00
|
|
|
if (pp->ArityOfPE) {
|
|
|
|
*pat = NameOfFunctor(pp->FunctorOfPred);
|
2001-12-12 19:36:51 +00:00
|
|
|
} else {
|
|
|
|
*pat = (Atom)(pp->FunctorOfPred);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-12-11 19:12:41 +00:00
|
|
|
READ_UNLOCK(pp->PRWLock);
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
if (clcode == pp->LastClause)
|
|
|
|
break;
|
|
|
|
cl = (CODEADDR)ClauseCodeToClause(clcode = NextClause(clcode));
|
|
|
|
i++;
|
|
|
|
} while (TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-12-11 19:12:41 +00:00
|
|
|
READ_UNLOCK(pp->PRWLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Int
|
|
|
|
PredForCode(CODEADDR codeptr, Atom *pat, Int *parity, SMALLUNSGN *pmodule) {
|
2001-12-12 19:36:51 +00:00
|
|
|
Int found = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
Int i_table;
|
2001-12-11 19:12:41 +00:00
|
|
|
|
2001-12-12 19:36:51 +00:00
|
|
|
/* should we allow the user to see hidden predicates? */
|
|
|
|
for (i_table = NoOfModules-1; i_table >= 0; --i_table) {
|
2001-12-11 19:12:41 +00:00
|
|
|
PredEntry *pp = ModulePred[i_table];
|
|
|
|
while (pp != NULL) {
|
|
|
|
if ((found = code_in_pred(pp, pat, parity, codeptr)) != 0) {
|
2001-12-12 19:36:51 +00:00
|
|
|
*pmodule = i_table;
|
|
|
|
return(found);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-12-11 19:12:41 +00:00
|
|
|
pp = pp->NextPredOfModule;
|
2001-05-21 21:00:05 +01:00
|
|
|
}
|
|
|
|
}
|
2001-12-12 19:36:51 +00:00
|
|
|
return(0);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_is_profiled(void)
|
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Term ta;
|
|
|
|
|
|
|
|
if (PROFILING) ta = MkAtomTerm(LookupAtom("on"));
|
|
|
|
else ta = MkAtomTerm(LookupAtom("off"));
|
|
|
|
BIND((CELL *)t,ta,bind_is_profiled);
|
|
|
|
#ifdef COROUTINING
|
|
|
|
DO_TRAIL(CellPtr(t), ta);
|
|
|
|
if (CellPtr(t) < H0) WakeUp((CELL *)t);
|
|
|
|
bind_is_profiled:
|
|
|
|
#endif
|
|
|
|
return(TRUE);
|
|
|
|
} else if (!IsAtomTerm(t)) return(FALSE);
|
|
|
|
s = RepAtom(AtomOfTerm(t))->StrOfAE;
|
|
|
|
if (strcmp(s,"on") == 0) {
|
|
|
|
PROFILING = TRUE;
|
|
|
|
return(TRUE);
|
|
|
|
} else if (strcmp(s,"off") == 0) {
|
|
|
|
PROFILING = FALSE;
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_profile_info(void)
|
|
|
|
{
|
2001-11-15 00:01:43 +00:00
|
|
|
Term tmod = Deref(ARG1);
|
|
|
|
Term tfun = Deref(ARG2);
|
|
|
|
int mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
Term out;
|
|
|
|
PredEntry *pe;
|
|
|
|
Term p[3];
|
|
|
|
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(tmod) || !IsAtomTerm(tmod))
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
mod = LookupModule(tmod);
|
|
|
|
if (IsVarTerm(tfun)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
} else if (IsApplTerm(tfun)) {
|
|
|
|
Functor f = FunctorOfTerm(tfun);
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
pe = RepPredProp(GetPredPropByFunc(f, mod));
|
|
|
|
} else if (IsAtomTerm(tfun)) {
|
|
|
|
pe = RepPredProp(GetPredPropByAtom(AtomOfTerm(tfun), mod));
|
|
|
|
} else {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (EndOfPAEntr(pe))
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
LOCK(pe->StatisticsForPred.lock);
|
|
|
|
if (!(pe->StatisticsForPred.NOfEntries)) {
|
|
|
|
UNLOCK(pe->StatisticsForPred.lock);
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
p[0] = MkIntegerTerm(pe->StatisticsForPred.NOfEntries);
|
|
|
|
p[1] = MkIntegerTerm(pe->StatisticsForPred.NOfHeadSuccesses);
|
|
|
|
p[2] = MkIntegerTerm(pe->StatisticsForPred.NOfRetries);
|
|
|
|
UNLOCK(pe->StatisticsForPred.lock);
|
|
|
|
out = MkApplTerm(MkFunctor(AtomProfile,3),3,p);
|
|
|
|
return(unify(ARG3,out));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_profile_reset(void)
|
|
|
|
{
|
2001-11-15 00:01:43 +00:00
|
|
|
Term tmod = Deref(ARG1);
|
|
|
|
Term tfun = Deref(ARG2);
|
|
|
|
int mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
PredEntry *pe;
|
|
|
|
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsVarTerm(tmod) || !IsAtomTerm(tmod))
|
|
|
|
return(FALSE);
|
|
|
|
mod = LookupModule(tmod);
|
|
|
|
if (IsVarTerm(tfun)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
} else if (IsApplTerm(tfun)) {
|
|
|
|
Functor f = FunctorOfTerm(tfun);
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
pe = RepPredProp(GetPredPropByFunc(f, mod));
|
|
|
|
} else if (IsAtomTerm(tfun)) {
|
|
|
|
pe = RepPredProp(GetPredPropByAtom(AtomOfTerm(tfun), mod));
|
|
|
|
} else {
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
}
|
|
|
|
if (EndOfPAEntr(pe))
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
LOCK(pe->StatisticsForPred.lock);
|
|
|
|
pe->StatisticsForPred.NOfEntries = 0;
|
|
|
|
pe->StatisticsForPred.NOfHeadSuccesses = 0;
|
|
|
|
pe->StatisticsForPred.NOfRetries = 0;
|
|
|
|
UNLOCK(pe->StatisticsForPred.lock);
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_clean_up_dead_clauses(void)
|
|
|
|
{
|
|
|
|
while (DeadClauses != NULL) {
|
|
|
|
char *pt = (char *)DeadClauses;
|
|
|
|
DeadClauses = DeadClauses->u.NextCl;
|
|
|
|
FreeCodeSpace(pt);
|
|
|
|
}
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2001-08-08 22:17:27 +01:00
|
|
|
static Int /* $parent_pred(Module, Name, Arity) */
|
|
|
|
p_parent_pred(void)
|
|
|
|
{
|
2001-09-12 16:52:28 +01:00
|
|
|
/* This predicate is called from the debugger.
|
|
|
|
We assume a sequence of the form a -> b */
|
2001-08-08 22:17:27 +01:00
|
|
|
Atom at;
|
|
|
|
Int arity;
|
|
|
|
SMALLUNSGN module;
|
2001-09-12 16:52:28 +01:00
|
|
|
if (!PredForCode((CODEADDR)P_before_spy, &at, &arity, &module)) {
|
2001-08-08 22:17:27 +01:00
|
|
|
return(unify(ARG1, MkIntTerm(0)) &&
|
|
|
|
unify(ARG2, MkAtomTerm(AtomMetaCall)) &&
|
|
|
|
unify(ARG3, MkIntTerm(0)));
|
|
|
|
}
|
|
|
|
return(unify(ARG1, MkIntTerm(module)) &&
|
|
|
|
unify(ARG2, MkAtomTerm(at)) &&
|
|
|
|
unify(ARG3, MkIntTerm(arity)));
|
|
|
|
}
|
|
|
|
|
2001-11-15 00:01:43 +00:00
|
|
|
static Int /* $system_predicate(P) */
|
2001-10-30 16:42:05 +00:00
|
|
|
p_system_pred(void)
|
|
|
|
{
|
|
|
|
PredEntry *pe;
|
|
|
|
|
|
|
|
Term t1 = Deref(ARG1);
|
2002-01-08 05:22:40 +00:00
|
|
|
SMALLUNSGN mod = LookupModule(Deref(ARG2));
|
|
|
|
|
2001-11-15 00:01:43 +00:00
|
|
|
restart_system_pred:
|
2001-10-30 16:42:05 +00:00
|
|
|
if (IsVarTerm(t1))
|
|
|
|
return (FALSE);
|
|
|
|
if (IsAtomTerm(t1)) {
|
2002-01-08 05:22:40 +00:00
|
|
|
pe = RepPredProp(GetPredPropByAtom(AtomOfTerm(t1), mod));
|
2001-10-30 16:42:05 +00:00
|
|
|
} else if (IsApplTerm(t1)) {
|
|
|
|
Functor funt = FunctorOfTerm(t1);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (IsExtensionFunctor(funt)) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2002-02-22 06:12:18 +00:00
|
|
|
if (funt == FunctorModule) {
|
2001-10-30 16:42:05 +00:00
|
|
|
Term nmod = ArgOfTerm(1, t1);
|
|
|
|
if (IsVarTerm(nmod)) {
|
|
|
|
Error(INSTANTIATION_ERROR,ARG1,"system_predicate/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(nmod)) {
|
|
|
|
Error(TYPE_ERROR_ATOM,ARG1,"system_predicate/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
t1 = ArgOfTerm(2, t1);
|
2001-11-15 00:01:43 +00:00
|
|
|
goto restart_system_pred;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
2002-01-08 05:22:40 +00:00
|
|
|
pe = RepPredProp(GetPredPropByFunc(funt, mod));
|
2002-03-04 15:55:13 +00:00
|
|
|
} else if (IsPairTerm(t1)) {
|
|
|
|
return (TRUE);
|
2001-10-30 16:42:05 +00:00
|
|
|
} else
|
|
|
|
return (FALSE);
|
2001-11-15 00:01:43 +00:00
|
|
|
if (EndOfPAEntr(pe))
|
|
|
|
return(FALSE);
|
2002-05-28 17:26:00 +01:00
|
|
|
return(pe->ModuleOfPred == 0 || pe->PredFlags & (UserCPredFlag|CPredFlag|BinaryTestPredFlag|AsmPredFlag|TestPredFlag));
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
|
|
|
|
2001-12-11 16:40:51 +00:00
|
|
|
static Int /* $cut_transparent(P) */
|
|
|
|
p_cut_transparent(void)
|
|
|
|
{
|
|
|
|
PredEntry *pe;
|
|
|
|
|
|
|
|
Term t1 = Deref(ARG1);
|
|
|
|
restart_system_pred:
|
|
|
|
if (IsVarTerm(t1))
|
|
|
|
return (FALSE);
|
|
|
|
if (IsAtomTerm(t1)) {
|
|
|
|
pe = RepPredProp(GetPredPropByAtom(AtomOfTerm(t1), 0));
|
|
|
|
} else if (IsApplTerm(t1)) {
|
|
|
|
Functor funt = FunctorOfTerm(t1);
|
|
|
|
if (IsExtensionFunctor(funt)) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2002-02-22 06:12:18 +00:00
|
|
|
if (funt == FunctorModule) {
|
2001-12-11 16:40:51 +00:00
|
|
|
Term nmod = ArgOfTerm(1, t1);
|
|
|
|
if (IsVarTerm(nmod)) {
|
|
|
|
Error(INSTANTIATION_ERROR,ARG1,"system_predicate/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(nmod)) {
|
|
|
|
Error(TYPE_ERROR_ATOM,ARG1,"system_predicate/1");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
t1 = ArgOfTerm(2, t1);
|
|
|
|
goto restart_system_pred;
|
|
|
|
}
|
|
|
|
pe = RepPredProp(GetPredPropByFunc(funt, 0));
|
|
|
|
} else
|
|
|
|
return (FALSE);
|
|
|
|
if (EndOfPAEntr(pe))
|
|
|
|
return(FALSE);
|
|
|
|
pe->PredFlags |= CutTransparentPredFlag;
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2002-06-11 06:30:47 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
|
|
|
InitCdMgr(void)
|
|
|
|
{
|
|
|
|
InitCPred("$compile_mode", 2, p_compile_mode, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$start_consult", 3, p_startconsult, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$show_consult_level", 1, p_showconslultlev, SafePredFlag);
|
|
|
|
InitCPred("$end_consult", 0, p_endconsult, SafePredFlag|SyncPredFlag);
|
2001-11-15 00:01:43 +00:00
|
|
|
InitCPred("$set_spy", 2, p_setspy, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$rm_spy", 2, p_rmspy, SafePredFlag|SyncPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* gc() may happen during compilation, hence these predicates are
|
|
|
|
now unsafe */
|
2001-11-15 00:01:43 +00:00
|
|
|
InitCPred("$compile", 3, p_compile, SyncPredFlag);
|
|
|
|
InitCPred("$compile_dynamic", 4, p_compile_dynamic, SyncPredFlag);
|
|
|
|
InitCPred("$purge_clauses", 2, p_purge_clauses, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$in_use", 2, p_in_use, TestPredFlag | SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$is_dynamic", 2, p_is_dynamic, TestPredFlag | SafePredFlag);
|
|
|
|
InitCPred("$number_of_clauses", 3, p_number_of_clauses, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$undefined", 2, p_undefined, SafePredFlag|TestPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
InitCPred("$optimizer_on", 0, p_optimizer_on, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$clean_up_dead_clauses", 0, p_clean_up_dead_clauses, SyncPredFlag);
|
|
|
|
InitCPred("$optimizer_off", 0, p_optimizer_off, SafePredFlag|SyncPredFlag);
|
2001-11-16 20:27:06 +00:00
|
|
|
InitCPred("$kill_dynamic", 2, p_kill_dynamic, SafePredFlag|SyncPredFlag);
|
2001-11-15 00:01:43 +00:00
|
|
|
InitCPred("$in_this_file_before", 3, p_in_this_f_before, SafePredFlag);
|
|
|
|
InitCPred("$first_clause_in_file", 3, p_first_cl_in_f, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
InitCPred("$mk_cl_not_first", 2, p_mk_cl_not_first, SafePredFlag);
|
2001-11-15 00:01:43 +00:00
|
|
|
InitCPred("$new_multifile", 3, p_new_multifile, SafePredFlag|SyncPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
InitCPred("$is_multifile", 2, p_is_multifile, TestPredFlag | SafePredFlag);
|
|
|
|
InitCPred("$is_profiled", 1, p_is_profiled, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$profile_info", 3, p_profile_info, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$profile_reset", 2, p_profile_reset, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$toggle_static_predicates_in_use", 0, p_toggle_static_predicates_in_use, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$set_pred_module", 2, p_set_pred_module, SafePredFlag);
|
2001-08-08 22:17:27 +01:00
|
|
|
InitCPred("$parent_pred", 3, p_parent_pred, SafePredFlag);
|
2002-01-08 05:22:40 +00:00
|
|
|
InitCPred("$system_predicate", 2, p_system_pred, SafePredFlag);
|
2001-12-11 16:40:51 +00:00
|
|
|
InitCPred("$cut_transparent", 1, p_cut_transparent, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-10-30 16:42:05 +00:00
|
|
|
|