From 8f0ce4bb94c8acbba98bcfe3b50010bf336f9a50 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sun, 26 Oct 2014 01:50:18 +0100 Subject: [PATCH] YAP meets exo --- C/exo.c | 90 +++++++++++++++++++++++++++++++++++++----- include/YapInterface.h | 17 +++++++- 2 files changed, 96 insertions(+), 11 deletions(-) diff --git a/C/exo.c b/C/exo.c index 11ba5337a..24fa355cc 100755 --- a/C/exo.c +++ b/C/exo.c @@ -31,6 +31,15 @@ #if HAVE_STRING_H #include #endif +#if HAVE_STRING_H +#include +#endif +#if HAVE_STDBOOL_H +#include +#endif + +bool YAP_NewExo( PredEntry *ap, size_t data, struct udi_info *udi); +bool YAP_ExoAssert( PredEntry *pe, const Term *ts, size_t m); //static int exo_write=FALSE; @@ -38,11 +47,21 @@ #define MAX_ARITY 256 -#define FNV32_PRIME ((UInt)16777619) -#define FNV64_PRIME ((UInt)1099511628211) - -#define FNV32_OFFSET ((UInt)0x811c9dc5) -#define FNV64_OFFSET ((UInt)14695981039346656037) +#if SIZEOF_INT_P==4 +#define FNV32_PRIME (16777619UL) +#define FNV32_OFFSET (0x811c9dc5UL) +#define FNV_PRIME FNV32_PRIME +#define FNV_OFFSET FNV32_OFFSET +#elif SIZEOF_INT_P==8 +#define FNV64_PRIME (1099511628211) +#if SIZEOF_LONG_INT==4 +#define FNV64_OFFSET (14695981039346656037ULL) +#else +#define FNV64_OFFSET (14695981039346656037UL) +#endif +#define FNV_PRIME FNV64_PRIME +#define FNV_OFFSET FNV64_OFFSET +#endif /*MurmurHash3 from: https://code.google.com/p/smhasher/wiki/MurmurHash3*/ BITS32 rotl32 ( BITS32, int8_t); @@ -79,7 +98,7 @@ HASH_MURMUR3_32 (UInt arity, CELL *cl, UInt bnds[], UInt sz) const BITS32 c1 = 0xcc9e2d51; const BITS32 c2 = 0x1b873593; - hash = FNV32_OFFSET; /*did not find what seed to use yet*/ + hash = FNV_OFFSET; /*did not find what seed to use yet*/ while (j < arity) { if (bnds[j]) { @@ -189,7 +208,7 @@ HASH_FVN_1A(UInt arity, CELL *cl, UInt bnds[], UInt sz) UInt hash; UInt j=0; - hash = FNV32_OFFSET; + hash = FNV_OFFSET; while (j < arity) { if (bnds[j]) { unsigned char *i=(unsigned char*)(cl+j); @@ -197,7 +216,7 @@ HASH_FVN_1A(UInt arity, CELL *cl, UInt bnds[], UInt sz) while (i < m) { hash = hash ^ i[0]; - hash = hash * FNV32_PRIME; + hash = hash * FNV_PRIME; i++; } } @@ -635,6 +654,46 @@ exodb_get_space( Term t, Term mod, Term tn ) return mcl; } +bool +YAP_NewExo( PredEntry *ap, size_t data, struct udi_info *udi) +{ + MegaClause *mcl; + size_t required; + struct index_t **li; + + if (data <= ap->ArityOfPE*sizeof(CELL)) { + return false; + } + // data = ncls*arity*sizeof(CELL); + required = data+sizeof(MegaClause)+2*sizeof(struct index_t *); + while (!(mcl = (MegaClause *)Yap_AllocCodeSpace(required))) { + if (!Yap_growheap(FALSE, required, NULL)) { + /* just fail, the system will keep on going */ + return false; + } + } + Yap_ClauseSpace += required; + /* cool, it's our turn to do the conversion */ + mcl->ClFlags = MegaMask|ExoMask; + mcl->ClSize = required; + mcl->ClPred = ap; + mcl->ClItemSize = ap->ArityOfPE*sizeof(CELL); + mcl->ClNext = NULL; + li = (struct index_t **)(mcl->ClCode); + li[0] = li[1] = NULL; + ap->cs.p_code.FirstClause = + ap->cs.p_code.LastClause = + mcl->ClCode; + ap->PredFlags |= MegaClausePredFlag; + ap->cs.p_code.NOfClauses = 0; + if (ap->PredFlags & (SpiedPredFlag|CountPredFlag|ProfiledPredFlag)) { + ap->OpcodeOfPred = Yap_opcode(_spy_pred); + } else { + ap->OpcodeOfPred = Yap_opcode(_enter_exo); + } + ap->CodeOfPred = ap->cs.p_code.TrueCodeOfPred = (yamop *)(&(ap->OpcodeOfPred)); + return true; +} static Int p_exodb_get_space( USES_REGS1 ) @@ -648,7 +707,7 @@ p_exodb_get_space( USES_REGS1 ) } #define DerefAndCheck(t, V) \ - t = Deref(V); if(IsVarTerm(t) || !(IsAtomOrIntTerm(t))) Yap_Error(TYPE_ERROR_ATOM, t0, "load_db"); + t = Deref(V); if(IsVarTerm(t) || !(IsAtomOrIntTerm(t))) Yap_Error(TYPE_ERROR_ATOMIC, t0, "load_db"); static Int store_exo(yamop *pc, UInt arity, Term t0) @@ -666,6 +725,19 @@ store_exo(yamop *pc, UInt arity, Term t0) return TRUE; } +bool +YAP_ExoAssert( PredEntry *pe, const Term *ts, size_t m) +{ + MegaClause *mcl = ClauseCodeToMegaClause(pe->cs.p_code.FirstClause); + size_t i, n = pe->cs.p_code.NOfClauses; + ADDR base = (ADDR)mcl->ClCode+2*sizeof(struct index_t *); + for (i=0; iClItemSize)); + store_exo( ptr, pe->ArityOfPE, ts[i]); + } + return true; +} + static void exoassert( void *handle, Int n, Term term ) { /* '$number_of_clauses'(Predicate,M,N) */ diff --git a/include/YapInterface.h b/include/YapInterface.h index 9fb6d0c1b..b29d632d5 100755 --- a/include/YapInterface.h +++ b/include/YapInterface.h @@ -1,6 +1,6 @@ /************************************************************************* * * - * YAP Prolog @(#)c_interface.h 2.2 * +* YAP Prolog @(#)c_interface.h 2.2 * * Yap Prolog was developed at NCCUP - Universidade do Porto * * * * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * @@ -923,7 +923,7 @@ array of terms _Array_. _infop_ is the address of a goal handle that can be used to backtrack and to recover space. Succeeds if a solution was found. -Notice that you cannot create new slots if an YAP_EnterGoal goal is open. +Notice that you cannot create new slots if an YAP_ExnterGoal goal is open.
  • `YAP_Bool` YAP_RetryGoal(`YAP_dogoalinfo \*` _infop_) @anchor YAP_RetryGoal @@ -1420,6 +1420,15 @@ The following C-functions are available from YAP: Compile the Prolog term _Clause_ and assert it as the last clause for the corresponding procedure. + + YAP_MkExo(`YAP_PredEntryPtr` _pred_, `size_t` _sz_, `void *` _uid_) +Predicate _pred_ is an exo-predicate that needs _sz_ bytes of +contiguous storage. If _uid_ is non-null associate user-defined +code with _pred_. + + + YAP_AssertTuples(`YAP_PredEntryPtr` pred, `const YAP_Term *` _Facts_, `size_t` nb) +Add the array of _nb_ Prolog term `Facts` to the table +`Predicate`. + + `int` YAP_ContinueGoal(`void`) Continue execution from the point where it stopped. @@ -1846,6 +1855,10 @@ extern X_API YAP_Term YAP_CopyTerm(YAP_Term); /* char *YAP_CompileClause(YAP_Term) */ extern X_API char *YAP_CompileClause(YAP_Term); +extern X_API int YAP_NewExo( YAP_PredEntryPtr ap, size_t data, void *user_di); + +extern X_API int YAP_AssertTuples( YAP_PredEntryPtr pred, const YAP_Term *ts, size_t sz); + /* int YAP_Init(YAP_init_args *) */ extern X_API YAP_Int YAP_Init(YAP_init_args *);