2015-04-13 13:15:30 +01:00
|
|
|
/************************************************************************* *
|
2016-01-31 10:13:14 +00:00
|
|
|
* YAP Prolog *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.Santos Costa and Universidade do Porto 1985-- *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: c_interface.c *
|
|
|
|
* comments: c_interface primitives definition *
|
|
|
|
* *
|
|
|
|
* Last rev: $Date: 2008-08-07 20:51:21 $,$Author: vsc $
|
|
|
|
**
|
|
|
|
* $Log: not supported by cvs2svn $
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-05-25 20:48:06 +01:00
|
|
|
/**
|
|
|
|
@file c_interface.c
|
|
|
|
*/
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
#ifndef C_INTERFACE_C
|
2014-05-25 20:48:06 +01:00
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
#define C_INTERFACE_C 1
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
#include <stdlib.h>
|
2015-06-18 08:09:31 +01:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2014-12-04 07:59:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-03-02 18:35:49 +00:00
|
|
|
#if HAVE_STDARG_H
|
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
2015-04-13 13:15:30 +01:00
|
|
|
#if _MSC_VER || defined(__MINGW32__)
|
2003-02-24 14:11:54 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2013-11-25 10:22:07 +00:00
|
|
|
// we cannot consult YapInterface.h, that conflicts with what we declare, though
|
|
|
|
// it shouldn't
|
2016-04-18 16:40:17 +01:00
|
|
|
|
|
|
|
#include "Yap.h"
|
2014-05-25 20:48:06 +01:00
|
|
|
#include "YapInterface.h"
|
2013-12-08 19:12:24 +00:00
|
|
|
#include "YapText.h"
|
2016-04-18 16:40:17 +01:00
|
|
|
#include "attvar.h"
|
|
|
|
#include "clause.h"
|
|
|
|
#include "yapio.h"
|
2005-10-21 17:07:07 +01:00
|
|
|
#ifdef TABLING
|
|
|
|
#include "tab.macros.h"
|
|
|
|
#endif /* TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
|
|
|
#include "or.macros.h"
|
2015-10-22 00:45:47 +01:00
|
|
|
#endif /* YAPOR */
|
2005-11-18 18:52:41 +00:00
|
|
|
#include "cut_c.h"
|
2016-03-29 01:55:12 +01:00
|
|
|
#include "threads.h"
|
2009-02-09 22:45:50 +00:00
|
|
|
#if HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
2016-04-18 16:40:17 +01:00
|
|
|
#include "iopreds.h"
|
2016-07-31 05:10:39 +01:00
|
|
|
#include <libgen.h>
|
2005-11-18 18:52:41 +00:00
|
|
|
|
2016-02-11 14:00:56 +00:00
|
|
|
typedef void *atom_t;
|
|
|
|
typedef void *functor_t;
|
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
typedef enum {
|
|
|
|
FRG_FIRST_CALL = 0, /* Initial call */
|
|
|
|
FRG_CUTTED = 1, /* Context was cutted */
|
|
|
|
FRG_REDO = 2 /* Normal redo */
|
|
|
|
} frg_code;
|
2015-06-19 00:45:54 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
struct foreign_context {
|
|
|
|
uintptr_t context; /* context value */
|
|
|
|
frg_code control; /* FRG_* action */
|
|
|
|
struct PL_local_data *engine; /* invoking engine */
|
2015-06-19 00:45:54 +01:00
|
|
|
};
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_Reset(yap_reset_t mode);
|
2015-01-26 04:02:46 +00:00
|
|
|
|
2008-12-13 16:07:55 +00:00
|
|
|
#if !HAVE_STRNCPY
|
2015-10-22 00:45:47 +01:00
|
|
|
#define strncpy(X, Y, Z) strcpy(X, Y)
|
2008-12-13 16:07:55 +00:00
|
|
|
#endif
|
2010-03-05 09:51:52 +00:00
|
|
|
#if !HAVE_STRNCAT
|
2015-10-22 00:45:47 +01:00
|
|
|
#define strncat(X, Y, Z) strcat(X, Y)
|
2010-03-05 09:51:52 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2016-02-28 19:32:55 +00:00
|
|
|
#if defined(_WIN32) && !defined(X_API)
|
2001-04-09 20:54:03 +01:00
|
|
|
#define X_API __declspec(dllexport)
|
|
|
|
#endif
|
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
#define BootFilePath NULL
|
|
|
|
#if __ANDROID__
|
|
|
|
#define BOOT_FROM_SAVED_STATE true
|
|
|
|
#endif
|
|
|
|
static char BootFile[] = "boot.yap";
|
|
|
|
static char InitFile[] = "init.yap";
|
|
|
|
|
2014-05-14 17:59:08 +01:00
|
|
|
/**
|
2015-01-18 01:32:13 +00:00
|
|
|
@defgroup slotInterface Term Handles or Slots
|
2014-09-11 20:06:57 +01:00
|
|
|
@ingroup ChYInterface
|
2014-05-14 17:59:08 +01:00
|
|
|
@{
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Term handles correspond to SWI-Prolog's term_t datatype: they are a safe
|
|
|
|
representation
|
|
|
|
of terms. Slots are safe houses in the stack, the garbage collector and the
|
|
|
|
stack
|
2014-05-14 17:59:08 +01:00
|
|
|
shifter know about them and make sure they have correct values. In this
|
|
|
|
case, we use a slot to preserve _t_ during the execution of
|
|
|
|
YAP_RunGoal). When the execution of _t_ is over we read the
|
|
|
|
(possibly changed) value of _t_ back from the slot _sl_ and tell
|
|
|
|
YAP that the slot _sl_ is not needed and can be given back to the
|
|
|
|
system.
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
YAP supports storing and manipulating term_t like slots or handles, but in the
|
|
|
|
C
|
|
|
|
the programmer needs to take care as most operations still require unwrapping
|
|
|
|
the term
|
2014-05-14 17:59:08 +01:00
|
|
|
inside.
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
For implementation details and more information, please check term_t_slots in
|
|
|
|
the implementation section.
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
*/
|
2015-10-22 00:45:47 +01:00
|
|
|
/// @brief report the current position of the slots, assuming that they occupy
|
|
|
|
/// the top of the stack.
|
2014-05-14 17:59:08 +01:00
|
|
|
///
|
|
|
|
///
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API yhandle_t YAP_CurrentSlot(void);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief allocate n empty new slots
|
|
|
|
///
|
|
|
|
/// Return a handle to the system's default slot.
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API yhandle_t YAP_NewSlots(int NumberOfSlots);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief allocate n empty new slots
|
|
|
|
///
|
|
|
|
/// Allocate _NumberOfSlots_ from the stack and return an handle to the
|
|
|
|
/// last one. The other handle can be obtained by decrementing the handle.
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API yhandle_t YAP_InitSlot(YAP_Term t);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief read from a slot.
|
|
|
|
///
|
|
|
|
///
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Term YAP_GetFromSlot(YAP_handle_t slot);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief get the memory address of a slot
|
|
|
|
///
|
|
|
|
/// Return the address of slot _slot_: please use with care.
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Term *YAP_AddressFromSlot(YAP_handle_t);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief get the memory address of the term actually stored in a slot
|
|
|
|
///
|
|
|
|
///
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Term *YAP_AddressOfTermInSlot(YAP_handle_t);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief store term in a slot
|
|
|
|
///
|
|
|
|
///
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_PutInSlot(YAP_handle_t slot, YAP_Term t);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
/// @brief Succeeds if it recovers the space allocated for $n$ contiguous slots
|
|
|
|
/// starting at topSlot.
|
2014-05-14 17:59:08 +01:00
|
|
|
///
|
|
|
|
/// Set the contents of slot _slot_ to _t_.
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_RecoverSlots(int, YAP_handle_t topSlot);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @brief copies the first new n YAAM registers to slots
|
|
|
|
///
|
|
|
|
/// Store the current first _HowMany_ arguments in new slots.
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_handle_t YAP_ArgsToSlots(int HowMany);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
/// @brief copies n slots such that sl is copied to the last abstract ,achine
|
|
|
|
/// register.
|
2014-05-14 17:59:08 +01:00
|
|
|
///
|
|
|
|
/// Set the first _HowMany_ arguments to the _HowMany_ slots
|
|
|
|
// starting at _slot_.
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_SlotsToArgs(int HowMany, YAP_handle_t slot);
|
2014-05-14 17:59:08 +01:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/**
|
|
|
|
@addtogroup c-interface
|
|
|
|
@{
|
|
|
|
*/
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
static arity_t current_arity(void) {
|
2012-02-07 15:18:43 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
if (P && PREVOP(P, Osbpp)->opc == Yap_opcode(_call_usercpred)) {
|
|
|
|
return PREVOP(P, Osbpp)->y_u.Osbpp.p->ArityOfPE;
|
2012-02-02 23:25:09 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
static int doexpand(UInt sz) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2007-10-28 01:54:09 +01:00
|
|
|
UInt arity;
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
if (P && PREVOP(P, Osbpp)->opc == Yap_opcode(_call_usercpred)) {
|
|
|
|
arity = PREVOP(P, Osbpp)->y_u.Osbpp.p->ArityOfPE;
|
2007-10-28 01:54:09 +01:00
|
|
|
} else {
|
|
|
|
arity = 0;
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!Yap_gcl(sz, arity, ENV, gc_P(P, CP))) {
|
2007-10-28 01:54:09 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Term YAP_A(int i) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
return (Deref(XREGS[i]));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsIntTerm(YAP_Term t) { return IsIntegerTerm(t); }
|
2004-05-14 18:11:32 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsNumberTerm(YAP_Term t) {
|
2011-11-02 22:52:52 +00:00
|
|
|
return IsIntegerTerm(t) || IsIntTerm(t) || IsFloatTerm(t) || IsBigIntTerm(t);
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsLongIntTerm(YAP_Term t) { return IsLongIntTerm(t); }
|
2009-11-23 18:46:57 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsBigNumTerm(YAP_Term t) {
|
2004-05-14 18:11:32 +01:00
|
|
|
#if USE_GMP
|
2011-02-27 10:13:25 +00:00
|
|
|
CELL *pt;
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
if (!IsBigIntTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
pt = RepAppl(t);
|
|
|
|
return pt[1] == BIG_INT;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsRationalTerm(YAP_Term t) {
|
2011-02-27 10:13:25 +00:00
|
|
|
#if USE_GMP
|
|
|
|
CELL *pt;
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
if (!IsBigIntTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
pt = RepAppl(t);
|
|
|
|
return pt[1] == BIG_RATIONAL;
|
2004-05-14 18:11:32 +01:00
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsVarTerm(YAP_Term t) { return (IsVarTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsNonVarTerm(YAP_Term t) { return (IsNonVarTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsFloatTerm(Term t) { return (IsFloatTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsDbRefTerm(Term t) { return (IsDBRefTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsAtomTerm(Term t) { return (IsAtomTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsPairTerm(Term t) { return (IsPairTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsApplTerm(Term t) {
|
2016-04-18 01:09:10 +01:00
|
|
|
return IsApplTerm(t) && !IsExtensionFunctor(FunctorOfTerm(t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsCompoundTerm(Term t) {
|
2011-11-02 22:52:52 +00:00
|
|
|
return (IsApplTerm(t) && !IsExtensionFunctor(FunctorOfTerm(t))) ||
|
2015-10-22 00:45:47 +01:00
|
|
|
IsPairTerm(t);
|
2011-11-02 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkIntTerm(Int n) {
|
2013-03-26 20:01:52 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
Term I;
|
|
|
|
BACKUP_H();
|
|
|
|
|
|
|
|
I = MkIntegerTerm(n);
|
|
|
|
RECOVER_H();
|
2006-01-16 02:57:52 +00:00
|
|
|
return I;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_IntOfTerm(Term t) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if (!IsApplTerm(t))
|
2004-05-14 18:11:32 +01:00
|
|
|
return IntOfTerm(t);
|
|
|
|
else {
|
|
|
|
return LongIntOfTerm(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkBigNumTerm(void *big) {
|
2004-05-14 18:11:32 +01:00
|
|
|
#if USE_GMP
|
|
|
|
Term I;
|
|
|
|
BACKUP_H();
|
2006-01-18 15:34:54 +00:00
|
|
|
I = Yap_MkBigIntTerm((MP_INT *)big);
|
2004-05-14 18:11:32 +01:00
|
|
|
RECOVER_H();
|
|
|
|
return I;
|
|
|
|
#else
|
|
|
|
return TermNil;
|
|
|
|
#endif /* USE_GMP */
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_BigNumOfTerm(Term t, void *b) {
|
2004-05-14 18:11:32 +01:00
|
|
|
#if USE_GMP
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *bz = (MP_INT *)b;
|
2004-05-14 18:11:32 +01:00
|
|
|
if (IsVarTerm(t))
|
2014-05-25 20:48:06 +01:00
|
|
|
return FALSE;
|
2004-05-14 18:11:32 +01:00
|
|
|
if (!IsBigIntTerm(t))
|
2014-05-25 20:48:06 +01:00
|
|
|
return FALSE;
|
2015-10-22 00:45:47 +01:00
|
|
|
mpz_set(bz, Yap_BigIntOfTerm(t));
|
2014-05-25 20:48:06 +01:00
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
2004-05-14 18:11:32 +01:00
|
|
|
#endif /* USE_GMP */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkRationalTerm(void *big) {
|
2011-02-27 10:13:25 +00:00
|
|
|
#if USE_GMP
|
|
|
|
Term I;
|
|
|
|
BACKUP_H();
|
|
|
|
I = Yap_MkBigRatTerm((MP_RAT *)big);
|
|
|
|
RECOVER_H();
|
|
|
|
return I;
|
|
|
|
#else
|
|
|
|
return TermNil;
|
|
|
|
#endif /* USE_GMP */
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_RationalOfTerm(Term t, void *b) {
|
2011-02-27 10:13:25 +00:00
|
|
|
#if USE_GMP
|
|
|
|
MP_RAT *br = (MP_RAT *)b;
|
|
|
|
if (IsVarTerm(t))
|
2014-05-25 20:48:06 +01:00
|
|
|
return FALSE;
|
2011-02-27 10:13:25 +00:00
|
|
|
if (!IsBigIntTerm(t))
|
2014-05-25 20:48:06 +01:00
|
|
|
return FALSE;
|
2015-10-22 00:45:47 +01:00
|
|
|
mpq_set(br, Yap_BigRatOfTerm(t));
|
2014-05-25 20:48:06 +01:00
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
2011-02-27 10:13:25 +00:00
|
|
|
#endif /* USE_GMP */
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkBlobTerm(unsigned int sz) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2006-12-13 16:10:26 +00:00
|
|
|
Term I;
|
|
|
|
MP_INT *dst;
|
|
|
|
BACKUP_H();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
while (HR + (sz + sizeof(MP_INT) / sizeof(CELL) + 2) > ASP - 1024) {
|
|
|
|
if (!doexpand((sz + sizeof(MP_INT) / sizeof(CELL) + 2) * sizeof(CELL))) {
|
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, TermNil,
|
|
|
|
"YAP failed to grow the stack while constructing a blob: %s",
|
|
|
|
LOCAL_ErrorMessage);
|
2007-10-28 01:54:09 +01:00
|
|
|
return TermNil;
|
2008-10-29 18:19:15 +00:00
|
|
|
}
|
2007-10-28 01:54:09 +01:00
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
I = AbsAppl(HR);
|
|
|
|
HR[0] = (CELL)FunctorBigInt;
|
|
|
|
HR[1] = ARRAY_INT;
|
2015-10-22 00:45:47 +01:00
|
|
|
dst = (MP_INT *)(HR + 2);
|
2006-12-13 16:10:26 +00:00
|
|
|
dst->_mp_size = 0L;
|
|
|
|
dst->_mp_alloc = sz;
|
2015-10-22 00:45:47 +01:00
|
|
|
HR += (2 + sizeof(MP_INT) / sizeof(CELL));
|
2014-01-19 21:15:05 +00:00
|
|
|
HR[sz] = EndSpecials;
|
2015-10-22 00:45:47 +01:00
|
|
|
HR += sz + 1;
|
2006-12-13 16:10:26 +00:00
|
|
|
RECOVER_H();
|
|
|
|
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_BlobOfTerm(Term t) {
|
2006-12-13 16:10:26 +00:00
|
|
|
MP_INT *src;
|
2007-03-30 17:47:22 +01:00
|
|
|
|
2006-12-13 16:10:26 +00:00
|
|
|
if (IsVarTerm(t))
|
|
|
|
return NULL;
|
|
|
|
if (!IsBigIntTerm(t))
|
|
|
|
return NULL;
|
2015-10-22 00:45:47 +01:00
|
|
|
src = (MP_INT *)(RepAppl(t) + 2);
|
|
|
|
return (void *)(src + 1);
|
2006-12-13 16:10:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkFloatTerm(double n) {
|
2013-03-26 20:01:52 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t;
|
|
|
|
BACKUP_H();
|
|
|
|
|
|
|
|
t = MkFloatTerm(n);
|
|
|
|
|
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Float YAP_FloatOfTerm(YAP_Term t) { return (FloatOfTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkAtomTerm(Atom n) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t;
|
|
|
|
|
|
|
|
t = MkAtomTerm(n);
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Atom YAP_AtomOfTerm(Term t) { return (AtomOfTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_IsWideAtom(Atom a) { return IsWideAtom(a); }
|
2007-09-04 11:34:55 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API const char *YAP_AtomName(Atom a) {
|
2014-05-25 20:48:06 +01:00
|
|
|
const char *o;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
o = AtomName(a);
|
2015-10-22 00:45:47 +01:00
|
|
|
return (o);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API const wchar_t *YAP_WideAtomName(Atom a) { return RepAtom(a)->WStrOfAE; }
|
2007-09-04 11:34:55 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Atom YAP_LookupAtom(const char *c) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2005-03-01 22:25:09 +00:00
|
|
|
Atom a;
|
|
|
|
|
|
|
|
while (TRUE) {
|
2015-09-21 23:05:36 +01:00
|
|
|
a = Yap_LookupAtom(c);
|
2014-09-02 20:19:23 +01:00
|
|
|
if (a == NIL || Yap_get_signal(YAP_CDOVF_SIGNAL)) {
|
2014-04-23 22:41:12 +01:00
|
|
|
if (!Yap_locked_growheap(FALSE, 0, NULL)) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "YAP failed to grow heap: %s",
|
|
|
|
LOCAL_ErrorMessage);
|
2005-03-01 22:25:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return a;
|
2004-07-23 04:37:17 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-25 20:48:06 +01:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Atom YAP_LookupWideAtom(const wchar_t *c) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2007-09-04 11:34:55 +01:00
|
|
|
Atom a;
|
|
|
|
|
|
|
|
while (TRUE) {
|
2014-05-25 20:48:06 +01:00
|
|
|
a = Yap_LookupWideAtom((wchar_t *)c);
|
2014-09-02 20:19:23 +01:00
|
|
|
if (a == NIL || Yap_get_signal(YAP_CDOVF_SIGNAL)) {
|
2014-04-23 22:41:12 +01:00
|
|
|
if (!Yap_locked_growheap(FALSE, 0, NULL)) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "YAP failed to grow heap: %s",
|
|
|
|
LOCAL_ErrorMessage);
|
2007-09-04 11:34:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
2014-05-25 20:48:06 +01:00
|
|
|
return NULL;
|
2007-09-04 11:34:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Atom YAP_FullLookupAtom(const char *c) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
Atom at;
|
|
|
|
|
2005-03-01 22:25:09 +00:00
|
|
|
while (TRUE) {
|
2015-09-21 23:05:36 +01:00
|
|
|
at = Yap_FullLookupAtom(c);
|
2014-09-02 20:19:23 +01:00
|
|
|
if (at == NIL || Yap_get_signal(YAP_CDOVF_SIGNAL)) {
|
2014-04-23 22:41:12 +01:00
|
|
|
if (!Yap_locked_growheap(FALSE, 0, NULL)) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "YAP failed to grow heap: %s",
|
|
|
|
LOCAL_ErrorMessage);
|
2005-03-01 22:25:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return at;
|
2004-07-23 04:37:17 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-25 20:48:06 +01:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API size_t YAP_AtomNameLength(Atom at) {
|
2011-02-10 00:01:19 +00:00
|
|
|
if (IsBlob(at)) {
|
|
|
|
return RepAtom(at)->rep.blob->length;
|
|
|
|
}
|
2007-09-04 11:34:55 +01:00
|
|
|
if (IsWideAtom(at)) {
|
|
|
|
wchar_t *c = RepAtom(at)->WStrOfAE;
|
|
|
|
|
|
|
|
return wcslen(c);
|
|
|
|
} else {
|
2015-09-21 23:05:36 +01:00
|
|
|
unsigned char *c = RepAtom(at)->UStrOfAE;
|
2007-09-04 11:34:55 +01:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
return strlen((char *)c);
|
2007-09-04 11:34:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkVarTerm(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
CELL t;
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
|
|
|
t = MkVarTerm();
|
|
|
|
|
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkPairTerm(Term t1, Term t2) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
while (HR > ASP - 1024) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Int sl1 = Yap_InitSlot(t1);
|
|
|
|
Int sl2 = Yap_InitSlot(t2);
|
2011-10-01 21:00:00 +01:00
|
|
|
RECOVER_H();
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!Yap_dogc(0, NULL PASS_REGS)) {
|
2009-07-14 03:55:57 +01:00
|
|
|
return TermNil;
|
|
|
|
}
|
2011-10-01 21:00:00 +01:00
|
|
|
BACKUP_H();
|
2015-10-22 00:45:47 +01:00
|
|
|
t1 = Yap_GetFromSlot(sl1);
|
|
|
|
t2 = Yap_GetFromSlot(sl2);
|
2016-01-03 01:25:31 +00:00
|
|
|
Yap_RecoverSlots(2, sl2);
|
2009-07-14 03:55:57 +01:00
|
|
|
}
|
|
|
|
t = MkPairTerm(t1, t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkListFromTerms(Term *ta, Int sz) {
|
2011-11-18 16:26:11 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2011-11-18 16:26:11 +00:00
|
|
|
CELL *h;
|
|
|
|
if (sz == 0)
|
|
|
|
return TermNil;
|
|
|
|
BACKUP_H();
|
2015-10-22 00:45:47 +01:00
|
|
|
while (HR + sz * 2 > ASP - 1024) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Int sl1 = Yap_InitSlot((CELL)ta);
|
2011-11-18 16:26:11 +00:00
|
|
|
RECOVER_H();
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!Yap_dogc(0, NULL PASS_REGS)) {
|
2011-11-18 16:26:11 +00:00
|
|
|
return TermNil;
|
|
|
|
}
|
|
|
|
BACKUP_H();
|
2015-10-22 00:45:47 +01:00
|
|
|
ta = (CELL *)Yap_GetFromSlot(sl1);
|
2016-01-03 01:25:31 +00:00
|
|
|
Yap_RecoverSlots(1, sl1);
|
2011-11-18 16:26:11 +00:00
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
h = HR;
|
2011-11-18 16:26:11 +00:00
|
|
|
t = AbsPair(h);
|
|
|
|
while (sz--) {
|
|
|
|
Term ti = *ta++;
|
|
|
|
if (IsVarTerm(ti)) {
|
|
|
|
RESET_VARIABLE(h);
|
|
|
|
Yap_unify(ti, h[0]);
|
|
|
|
} else {
|
|
|
|
h[0] = ti;
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
h[1] = AbsPair(h + 2);
|
2011-11-18 16:26:11 +00:00
|
|
|
h += 2;
|
|
|
|
}
|
|
|
|
h[-1] = TermNil;
|
2014-01-19 21:15:05 +00:00
|
|
|
HR = h;
|
2011-11-18 16:26:11 +00:00
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkNewPairTerm() {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2002-05-14 19:24:34 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
if (HR > ASP - 1024)
|
2007-11-16 14:58:41 +00:00
|
|
|
t = TermNil;
|
|
|
|
else
|
|
|
|
t = Yap_MkNewPairTerm();
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2002-05-14 19:24:34 +01:00
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2002-05-14 19:24:34 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_HeadOfTerm(Term t) { return (HeadOfTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_TailOfTerm(Term t) { return (TailOfTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_SkipList(Term *l, Term **tailp) {
|
2012-01-09 23:29:31 +00:00
|
|
|
return Yap_SkipList(l, tailp);
|
2011-02-10 00:01:19 +00:00
|
|
|
Int length = 0;
|
|
|
|
Term *s; /* slow */
|
2015-10-22 00:45:47 +01:00
|
|
|
Term v; /* temporary */
|
2011-02-10 00:01:19 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
do_derefa(v, l, derefa_unk, derefa_nonvar);
|
2011-02-10 00:01:19 +00:00
|
|
|
s = l;
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
if (IsPairTerm(*l)) {
|
|
|
|
intptr_t power = 1, lam = 0;
|
|
|
|
do {
|
|
|
|
if (power == lam) {
|
|
|
|
s = l;
|
|
|
|
power *= 2;
|
|
|
|
lam = 0;
|
2011-02-10 00:01:19 +00:00
|
|
|
}
|
|
|
|
lam++;
|
|
|
|
length++;
|
2015-10-22 00:45:47 +01:00
|
|
|
l = RepPair(*l) + 1;
|
|
|
|
do_derefa(v, l, derefa2_unk, derefa2_nonvar);
|
|
|
|
} while (*l != *s && IsPairTerm(*l));
|
2011-02-10 00:01:19 +00:00
|
|
|
}
|
|
|
|
*tailp = l;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkApplTerm(Functor f, UInt arity, Term args[]) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
if (HR + arity > ASP - 1024)
|
2007-11-16 14:58:41 +00:00
|
|
|
t = TermNil;
|
|
|
|
else
|
|
|
|
t = Yap_MkApplTerm(f, arity, args);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_MkNewApplTerm(Functor f, UInt arity) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2002-05-06 16:33:05 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
if (HR + arity > ASP - 1024)
|
2007-11-16 14:58:41 +00:00
|
|
|
t = TermNil;
|
|
|
|
else
|
|
|
|
t = Yap_MkNewApplTerm(f, arity);
|
2002-05-06 16:33:05 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2002-05-06 16:33:05 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Functor YAP_FunctorOfTerm(Term t) { return (FunctorOfTerm(t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_ArgOfTerm(UInt n, Term t) { return (ArgOfTerm(n, t)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term *YAP_ArgsOfTerm(Term t) {
|
2007-06-04 13:28:02 +01:00
|
|
|
if (IsApplTerm(t))
|
2015-10-22 00:45:47 +01:00
|
|
|
return RepAppl(t) + 1;
|
2007-06-04 13:28:02 +01:00
|
|
|
else if (IsPairTerm(t))
|
|
|
|
return RepPair(t);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Functor YAP_MkFunctor(Atom a, UInt n) { return (Yap_MkFunctor(a, n)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Atom YAP_NameOfFunctor(Functor f) { return (NameOfFunctor(f)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API UInt YAP_ArityOfFunctor(Functor f) { return (ArityOfFunctor(f)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_ExtraSpaceCut(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2005-11-18 18:52:41 +00:00
|
|
|
void *ptr;
|
|
|
|
BACKUP_B();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
ptr = (void *)(((CELL *)(Yap_REGS.CUT_C_TOP)) -
|
|
|
|
(((yamop *)Yap_REGS.CUT_C_TOP->try_userc_cut_yamop)
|
|
|
|
->y_u.OtapFs.extra));
|
2005-11-18 18:52:41 +00:00
|
|
|
|
|
|
|
RECOVER_B();
|
2015-10-22 00:45:47 +01:00
|
|
|
return (ptr);
|
2005-11-18 18:52:41 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_ExtraSpace(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
void *ptr;
|
|
|
|
BACKUP_B();
|
2008-01-28 10:42:20 +00:00
|
|
|
BACKUP_H();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* find a pointer to extra space allocable */
|
2015-10-22 00:45:47 +01:00
|
|
|
ptr = (void *)((CELL *)(B + 1) + P->y_u.OtapFs.s);
|
2014-01-19 21:15:05 +00:00
|
|
|
B->cp_h = HR;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-01-28 10:42:20 +00:00
|
|
|
RECOVER_H();
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_B();
|
2015-10-22 00:45:47 +01:00
|
|
|
return (ptr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_cut_up(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_B();
|
2015-10-22 00:45:47 +01:00
|
|
|
{
|
|
|
|
while (POP_CHOICE_POINT(B->cp_b)) {
|
|
|
|
POP_EXECUTE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* This is complicated: make sure we can restore the ASP
|
|
|
|
pointer back to where cut_up called it. Slots depend on it. */
|
2011-07-12 05:14:26 +01:00
|
|
|
if (ENV > B->cp_env) {
|
2010-05-04 14:53:34 +01:00
|
|
|
ASP = B->cp_env;
|
2011-07-12 05:14:26 +01:00
|
|
|
}
|
2005-10-18 18:04:43 +01:00
|
|
|
#ifdef YAPOR
|
2005-10-21 17:07:07 +01:00
|
|
|
{
|
|
|
|
choiceptr cut_pt;
|
|
|
|
|
|
|
|
cut_pt = B->cp_b;
|
2011-07-09 12:56:11 +01:00
|
|
|
/* make sure we prune C-choicepoints */
|
2015-10-22 00:45:47 +01:00
|
|
|
if (POP_CHOICE_POINT(B->cp_b)) {
|
|
|
|
POP_EXECUTE();
|
|
|
|
}
|
2005-10-21 17:07:07 +01:00
|
|
|
CUT_prune_to(cut_pt);
|
2010-01-11 15:44:43 +00:00
|
|
|
Yap_TrimTrail();
|
2005-10-21 17:07:07 +01:00
|
|
|
B = cut_pt;
|
|
|
|
}
|
2010-01-11 15:44:43 +00:00
|
|
|
#else
|
2011-07-09 12:56:11 +01:00
|
|
|
/* make sure we prune C-choicepoints */
|
2015-10-22 00:45:47 +01:00
|
|
|
if (POP_CHOICE_POINT(B->cp_b)) {
|
|
|
|
POP_EXECUTE();
|
|
|
|
}
|
2010-01-11 15:44:43 +00:00
|
|
|
Yap_TrimTrail();
|
2015-10-22 00:45:47 +01:00
|
|
|
B = B->cp_b; /* cut_fail */
|
2005-10-21 17:07:07 +01:00
|
|
|
#endif
|
2002-01-14 22:26:53 +00:00
|
|
|
HB = B->cp_h; /* cut_fail */
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_B();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_Unify(Term t1, Term t2) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
out = Yap_unify(t1, t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
2007-01-08 08:27:19 +00:00
|
|
|
return out;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_Unifiable(Term t1, Term t2) {
|
2011-10-27 11:35:40 +01:00
|
|
|
int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
2011-11-02 22:52:52 +00:00
|
|
|
out = Yap_Unifiable(t1, t2);
|
2011-10-27 11:35:40 +01:00
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-08-02 19:48:17 +01:00
|
|
|
/* == */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_ExactlyEqual(Term t1, Term t2) {
|
2010-08-02 19:48:17 +01:00
|
|
|
int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
|
|
|
out = Yap_eq(t1, t2);
|
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* =@= */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_Variant(Term t1, Term t2) {
|
2010-08-02 19:48:17 +01:00
|
|
|
int out;
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
|
|
|
out = Yap_Variant(Deref(t1), Deref(t2));
|
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* =@= */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_TermHash(Term t, Int sz, Int depth, int variant) {
|
2010-08-02 19:48:17 +01:00
|
|
|
Int out;
|
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
|
|
|
out = Yap_TermHash(t, sz, depth, variant);
|
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_CurrentSlot(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2016-01-03 01:25:31 +00:00
|
|
|
return Yap_CurrentSlot();
|
2007-05-14 17:44:12 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_NewSlots(int n) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
return Yap_NewSlots(n);
|
2002-05-18 05:01:53 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_InitSlot(Term t) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
return Yap_InitSlot(t);
|
2002-05-18 05:01:53 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_RecoverSlots(int n, Int top_slot) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2016-01-03 01:25:31 +00:00
|
|
|
return Yap_RecoverSlots(n, top_slot);
|
2002-05-18 05:01:53 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_GetFromSlot(Int slot) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
return Yap_GetFromSlot(slot);
|
2002-05-18 05:01:53 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term *YAP_AddressFromSlot(Int slot) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-02-03 02:36:51 +00:00
|
|
|
return Yap_AddressFromSlot(slot);
|
2002-05-18 05:01:53 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term *YAP_AddressOfTermInSlot(Int slot) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-02-03 02:36:51 +00:00
|
|
|
Term *b = Yap_AddressFromSlot(slot);
|
2011-02-10 00:01:19 +00:00
|
|
|
Term a = *b;
|
2015-10-22 00:45:47 +01:00
|
|
|
restart:
|
2011-02-10 00:01:19 +00:00
|
|
|
if (!IsVarTerm(a)) {
|
2015-10-22 00:45:47 +01:00
|
|
|
return (b);
|
2011-02-10 00:01:19 +00:00
|
|
|
} else if (a == (CELL)b) {
|
2015-10-22 00:45:47 +01:00
|
|
|
return (b);
|
2011-02-10 00:01:19 +00:00
|
|
|
} else {
|
|
|
|
b = (CELL *)a;
|
|
|
|
a = *b;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_PutInSlot(Int slot, Term t) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2016-01-03 01:25:31 +00:00
|
|
|
Yap_PutInSlot(slot, t);
|
2002-05-18 05:01:53 +01:00
|
|
|
}
|
|
|
|
|
2011-03-07 16:02:55 +00:00
|
|
|
typedef Int (*CPredicate0)(void);
|
2015-01-30 07:25:34 +00:00
|
|
|
typedef Int (*CPredicate1)(yhandle_t);
|
2015-10-22 00:45:47 +01:00
|
|
|
typedef Int (*CPredicate2)(yhandle_t, yhandle_t);
|
|
|
|
typedef Int (*CPredicate3)(yhandle_t, yhandle_t, yhandle_t);
|
|
|
|
typedef Int (*CPredicate4)(yhandle_t, yhandle_t, yhandle_t, yhandle_t);
|
|
|
|
typedef Int (*CPredicate5)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t);
|
|
|
|
typedef Int (*CPredicate6)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t);
|
|
|
|
typedef Int (*CPredicate7)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t);
|
|
|
|
typedef Int (*CPredicate8)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t, yhandle_t);
|
|
|
|
typedef Int (*CPredicate9)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t);
|
|
|
|
typedef Int (*CPredicate10)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t);
|
|
|
|
typedef Int (*CPredicateV)(yhandle_t, yhandle_t, struct foreign_context *);
|
|
|
|
|
|
|
|
static Int execute_cargs(PredEntry *pe, CPredicate exec_code USES_REGS) {
|
2009-06-01 21:32:36 +01:00
|
|
|
switch (pe->ArityOfPE) {
|
2015-10-22 00:45:47 +01:00
|
|
|
case 0: {
|
|
|
|
CPredicate0 code0 = (CPredicate0)exec_code;
|
|
|
|
return code0();
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
CPredicate1 code1 = (CPredicate1)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(1, &ARG1);
|
|
|
|
return code1(a1);
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
CPredicate2 code2 = (CPredicate2)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(2, &ARG1);
|
|
|
|
return code2(a1, a1 + 1);
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
CPredicate3 code3 = (CPredicate3)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(3, &ARG1);
|
|
|
|
return code3(a1, a1 + 1, a1 + 2);
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
CPredicate4 code4 = (CPredicate4)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(4, &ARG1);
|
|
|
|
return code4(a1, a1 + 1, a1 + 2, a1 + 3);
|
|
|
|
}
|
|
|
|
case 5: {
|
|
|
|
CPredicate5 code5 = (CPredicate5)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(5, &ARG1);
|
|
|
|
return code5(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4);
|
|
|
|
}
|
|
|
|
case 6: {
|
|
|
|
CPredicate6 code6 = (CPredicate6)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(6, &ARG1);
|
|
|
|
return code6(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5);
|
|
|
|
}
|
|
|
|
case 7: {
|
|
|
|
CPredicate7 code7 = (CPredicate7)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(7, &ARG1);
|
|
|
|
return code7(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6);
|
|
|
|
}
|
|
|
|
case 8: {
|
|
|
|
CPredicate8 code8 = (CPredicate8)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(8, &ARG1);
|
|
|
|
return code8(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6, a1 + 7);
|
|
|
|
}
|
|
|
|
case 9: {
|
|
|
|
CPredicate9 code9 = (CPredicate9)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(9, &ARG1);
|
|
|
|
return code9(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6, a1 + 7,
|
|
|
|
a1 + 8);
|
|
|
|
}
|
|
|
|
case 10: {
|
|
|
|
CPredicate10 code10 = (CPredicate10)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(10, &ARG1);
|
|
|
|
return code10(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6, a1 + 7,
|
|
|
|
a1 + 8, a1 + 9);
|
|
|
|
}
|
2009-06-01 21:32:36 +01:00
|
|
|
default:
|
2015-10-22 00:45:47 +01:00
|
|
|
YAP_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
|
|
|
"YAP only supports SWI C-call with arity =< 10");
|
|
|
|
return (FALSE);
|
2009-06-01 21:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 07:25:34 +00:00
|
|
|
typedef uintptr_t (*CBPredicate0)(struct foreign_context *);
|
2015-10-22 00:45:47 +01:00
|
|
|
typedef uintptr_t (*CBPredicate1)(yhandle_t, struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate2)(yhandle_t, yhandle_t,
|
|
|
|
struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate3)(yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate4)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate5)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate6)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t,
|
|
|
|
struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate7)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate8)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate9)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, struct foreign_context *);
|
|
|
|
typedef uintptr_t (*CBPredicate10)(yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t, yhandle_t, yhandle_t,
|
|
|
|
yhandle_t, yhandle_t,
|
|
|
|
struct foreign_context *);
|
|
|
|
|
|
|
|
static uintptr_t execute_cargs_back(PredEntry *pe, CPredicate exec_code,
|
|
|
|
struct foreign_context *ctx USES_REGS) {
|
2010-01-06 13:25:15 +00:00
|
|
|
switch (pe->ArityOfPE) {
|
2015-10-22 00:45:47 +01:00
|
|
|
case 0: {
|
|
|
|
CBPredicate0 code0 = (CBPredicate0)exec_code;
|
|
|
|
return code0(ctx);
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
CBPredicate1 code1 = (CBPredicate1)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(1, &B->cp_a1);
|
|
|
|
return code1(a1, ctx);
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
CBPredicate2 code2 = (CBPredicate2)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(2, &B->cp_a1);
|
|
|
|
return code2(a1, a1 + 1, ctx);
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
CBPredicate3 code3 = (CBPredicate3)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(3, &B->cp_a1);
|
|
|
|
return code3(a1, a1 + 1, a1 + 2, ctx);
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
CBPredicate4 code4 = (CBPredicate4)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(4, &B->cp_a1);
|
|
|
|
return code4(a1, a1 + 1, a1 + 2, a1 + 3, ctx);
|
|
|
|
}
|
|
|
|
case 5: {
|
|
|
|
CBPredicate5 code5 = (CBPredicate5)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(5, &B->cp_a1);
|
|
|
|
return code5(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, ctx);
|
|
|
|
}
|
|
|
|
case 6: {
|
|
|
|
CBPredicate6 code6 = (CBPredicate6)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(6, &B->cp_a1);
|
|
|
|
return code6(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, ctx);
|
|
|
|
}
|
|
|
|
case 7: {
|
|
|
|
CBPredicate7 code7 = (CBPredicate7)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(7, &B->cp_a1);
|
|
|
|
return code7(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6, ctx);
|
|
|
|
}
|
|
|
|
case 8: {
|
|
|
|
CBPredicate8 code8 = (CBPredicate8)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(8, &B->cp_a1);
|
|
|
|
return code8(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6, a1 + 7,
|
|
|
|
ctx);
|
|
|
|
}
|
|
|
|
case 9: {
|
|
|
|
CBPredicate9 code9 = (CBPredicate9)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(9, &B->cp_a1);
|
|
|
|
return code9(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6, a1 + 7,
|
|
|
|
a1 + 8, ctx);
|
|
|
|
}
|
|
|
|
case 10: {
|
|
|
|
CBPredicate10 code10 = (CBPredicate10)exec_code;
|
|
|
|
yhandle_t a1 = Yap_InitSlots(10, &B->cp_a1);
|
|
|
|
return code10(a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5, a1 + 6, a1 + 7,
|
|
|
|
a1 + 8, a1 + 9, ctx);
|
|
|
|
}
|
2010-01-06 13:25:15 +00:00
|
|
|
default:
|
2015-10-22 00:45:47 +01:00
|
|
|
YAP_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
|
|
|
"YAP only supports SWI C-call with arity =< 10");
|
|
|
|
return (FALSE);
|
2010-01-06 13:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
static uintptr_t complete_fail(choiceptr ptr, int has_cp USES_REGS) {
|
2015-04-13 13:15:30 +01:00
|
|
|
// this case is easy, jut be sure to throw everything
|
2013-11-20 22:20:51 +00:00
|
|
|
// after the old B;
|
2016-03-29 01:55:12 +01:00
|
|
|
while (B && B->cp_b && B->cp_b <= ptr) {
|
2013-11-20 22:20:51 +00:00
|
|
|
B = B->cp_b;
|
|
|
|
}
|
|
|
|
if (has_cp)
|
2015-10-22 00:45:47 +01:00
|
|
|
return do_cut(FALSE);
|
2013-11-20 22:20:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
static uintptr_t complete_exit(choiceptr ptr, int has_cp,
|
|
|
|
int cut_all USES_REGS) {
|
2013-12-14 11:05:37 +00:00
|
|
|
// the user often leaves open frames, especially in forward execution
|
2015-10-22 00:45:47 +01:00
|
|
|
while (B && (!ptr || B < ptr)) {
|
|
|
|
if (cut_all || B->cp_ap == NOCODE) { /* separator */
|
|
|
|
do_cut(TRUE); // pushes B up
|
2013-11-20 22:20:51 +00:00
|
|
|
continue;
|
2015-04-13 13:15:30 +01:00
|
|
|
} else if (B->cp_ap->opc == RETRY_USERC_OPCODE && B->cp_b == ptr) {
|
2013-11-20 22:20:51 +00:00
|
|
|
// started the current choicepoint, I hope
|
2015-10-22 00:45:47 +01:00
|
|
|
return do_cut(TRUE);
|
2015-04-13 13:15:30 +01:00
|
|
|
} else
|
2013-11-20 22:20:51 +00:00
|
|
|
break; // oops, there is something else
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!ptr || B < ptr) {
|
2013-11-20 22:20:51 +00:00
|
|
|
// we're still not there yet
|
|
|
|
choiceptr new = B;
|
2015-10-22 00:45:47 +01:00
|
|
|
while (new &&new < ptr) {
|
2013-11-20 22:20:51 +00:00
|
|
|
if (new->cp_ap == NOCODE) /* separator */
|
2015-10-22 00:45:47 +01:00
|
|
|
new->cp_ap = FAILCODE; // there are choice-points above but at least,
|
|
|
|
// these won't harm innocent code
|
2015-04-13 13:15:30 +01:00
|
|
|
else if (new->cp_ap->opc == RETRY_USERC_OPCODE && new->cp_b == ptr) {
|
2015-10-22 00:45:47 +01:00
|
|
|
// I can't cut, but I can tag it as done
|
|
|
|
new->cp_ap = FAILCODE; // there are choice-points above but at least,
|
|
|
|
// these won't harm innocent code
|
2013-11-20 22:20:51 +00:00
|
|
|
}
|
|
|
|
new = new->cp_b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (has_cp) {
|
|
|
|
if (B == ptr) {
|
2015-10-22 00:45:47 +01:00
|
|
|
return do_cut(TRUE);
|
2013-11-20 22:20:51 +00:00
|
|
|
} else {
|
|
|
|
ptr->cp_ap = FAILCODE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-05-16 21:33:00 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Int YAP_Execute(PredEntry *pe, CPredicate exec_code) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-03-14 20:54:55 +00:00
|
|
|
Int ret;
|
2015-10-22 00:45:47 +01:00
|
|
|
Int OASP = LCL0 - (CELL *)B;
|
2013-01-18 14:30:35 +00:00
|
|
|
// Term omod = CurrentModule;
|
2015-10-22 00:45:47 +01:00
|
|
|
// if (pe->PredFlags & CArgsPredFlag) {
|
2013-01-18 14:30:35 +00:00
|
|
|
// CurrentModule = pe->ModuleOfPred;
|
|
|
|
//}
|
2009-03-13 19:36:38 +00:00
|
|
|
if (pe->PredFlags & SWIEnvPredFlag) {
|
2009-03-26 00:39:44 +00:00
|
|
|
CPredicateV codev = (CPredicateV)exec_code;
|
2009-06-01 21:32:36 +01:00
|
|
|
struct foreign_context ctx;
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2009-06-01 21:46:37 +01:00
|
|
|
ctx.engine = NULL;
|
2015-01-30 07:25:34 +00:00
|
|
|
yhandle_t s0 = Yap_InitSlots(pe->ArityOfPE, &ARG1);
|
2015-10-22 00:45:47 +01:00
|
|
|
PP = pe;
|
|
|
|
ret = codev(s0, 0, &ctx);
|
2011-03-14 20:54:55 +00:00
|
|
|
} else if (pe->PredFlags & CArgsPredFlag) {
|
2011-02-11 19:01:18 +00:00
|
|
|
PP = pe;
|
2015-10-22 00:45:47 +01:00
|
|
|
ret = execute_cargs(pe, exec_code PASS_REGS);
|
2002-05-16 21:33:00 +01:00
|
|
|
} else {
|
2011-03-14 20:54:55 +00:00
|
|
|
PP = pe;
|
2015-10-22 00:45:47 +01:00
|
|
|
ret = (exec_code)(PASS_REGS1);
|
2011-03-14 20:54:55 +00:00
|
|
|
}
|
|
|
|
PP = NULL;
|
2013-11-20 22:20:51 +00:00
|
|
|
// check for junk: open frames, etc */
|
|
|
|
if (ret)
|
2015-10-22 00:45:47 +01:00
|
|
|
complete_exit(((choiceptr)(LCL0 - OASP)), FALSE, FALSE PASS_REGS);
|
2013-11-20 22:20:51 +00:00
|
|
|
else
|
2015-10-22 00:45:47 +01:00
|
|
|
complete_fail(((choiceptr)(LCL0 - OASP)), FALSE PASS_REGS);
|
|
|
|
// CurrentModule = omod;
|
2011-03-14 20:54:55 +00:00
|
|
|
if (!ret) {
|
2016-04-05 02:20:16 +01:00
|
|
|
Yap_RaiseException();
|
|
|
|
}
|
|
|
|
return ret;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
#define FRG_REDO_MASK 0x00000003L
|
|
|
|
#define FRG_REDO_BITS 2
|
|
|
|
#define REDO_INT 0x02 /* Returned an integer */
|
|
|
|
#define REDO_PTR 0x03 /* returned a pointer */
|
2009-06-01 21:32:36 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Int YAP_ExecuteFirst(PredEntry *pe, CPredicate exec_code) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
CELL ocp = LCL0 - (CELL *)B;
|
2013-11-20 22:20:51 +00:00
|
|
|
/* for slots to work */
|
2015-10-22 00:45:47 +01:00
|
|
|
Int CurSlot = Yap_StartSlots();
|
|
|
|
if (pe->PredFlags &
|
|
|
|
(SWIEnvPredFlag | CArgsPredFlag | ModuleTransparentPredFlag)) {
|
2014-07-16 00:40:15 +01:00
|
|
|
uintptr_t val;
|
2009-06-01 21:32:36 +01:00
|
|
|
CPredicateV codev = (CPredicateV)exec_code;
|
2015-10-22 00:45:47 +01:00
|
|
|
struct foreign_context *ctx =
|
|
|
|
(struct foreign_context *)(&EXTRA_CBACK_ARG(pe->ArityOfPE, 1));
|
2010-11-26 15:21:55 +00:00
|
|
|
|
2011-02-11 19:01:18 +00:00
|
|
|
PP = pe;
|
2009-06-01 21:32:36 +01:00
|
|
|
ctx->control = FRG_FIRST_CALL;
|
|
|
|
ctx->engine = NULL; //(PL_local_data *)Yap_regp;
|
2013-11-25 10:22:07 +00:00
|
|
|
ctx->context = (uintptr_t)NULL;
|
2010-01-06 13:25:15 +00:00
|
|
|
if (pe->PredFlags & CArgsPredFlag) {
|
2011-03-07 16:02:55 +00:00
|
|
|
val = execute_cargs_back(pe, exec_code, ctx PASS_REGS);
|
2010-01-06 13:25:15 +00:00
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
val = codev(Yap_InitSlots(pe->ArityOfPE, &ARG1), 0, ctx);
|
2010-01-06 13:25:15 +00:00
|
|
|
}
|
2015-02-03 02:36:51 +00:00
|
|
|
Yap_CloseSlots(CurSlot);
|
2011-02-11 19:01:18 +00:00
|
|
|
PP = NULL;
|
2009-06-01 21:32:36 +01:00
|
|
|
if (val == 0) {
|
2016-03-29 01:55:12 +01:00
|
|
|
if (Yap_RaiseException()) {
|
|
|
|
return false;
|
2011-02-11 14:17:27 +00:00
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
return complete_fail(((choiceptr)(LCL0 - ocp)), TRUE PASS_REGS);
|
2009-06-01 21:32:36 +01:00
|
|
|
} else if (val == 1) { /* TRUE */
|
2015-10-22 00:45:47 +01:00
|
|
|
return complete_exit(((choiceptr)(LCL0 - ocp)), TRUE, FALSE PASS_REGS);
|
2009-06-01 21:32:36 +01:00
|
|
|
} else {
|
2011-02-11 01:22:07 +00:00
|
|
|
if ((val & REDO_PTR) == REDO_PTR)
|
2015-10-22 00:45:47 +01:00
|
|
|
ctx->context = (uintptr_t)(val & ~REDO_PTR);
|
2011-02-11 01:22:07 +00:00
|
|
|
else
|
2015-10-22 00:45:47 +01:00
|
|
|
ctx->context = (uintptr_t)((val & ~REDO_PTR) >> FRG_REDO_BITS);
|
2014-07-16 00:40:15 +01:00
|
|
|
/* fix dropped cps */
|
2015-10-22 00:45:47 +01:00
|
|
|
return complete_exit(((choiceptr)(LCL0 - ocp)), FALSE, FALSE PASS_REGS);
|
2009-06-01 21:32:36 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
Int ret = (exec_code)(PASS_REGS1);
|
2015-02-03 02:36:51 +00:00
|
|
|
Yap_CloseSlots(CurSlot);
|
2011-02-15 07:39:27 +00:00
|
|
|
if (!ret) {
|
2016-03-29 01:55:12 +01:00
|
|
|
Yap_RaiseException();
|
2016-04-05 02:20:16 +01:00
|
|
|
}
|
2011-02-15 07:39:27 +00:00
|
|
|
return ret;
|
2009-06-01 21:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Int YAP_ExecuteOnCut(PredEntry *pe, CPredicate exec_code,
|
|
|
|
struct cut_c_str *top) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2016-04-05 02:20:16 +01:00
|
|
|
Int oB = LCL0 - (CELL *)B;
|
2015-04-13 13:15:30 +01:00
|
|
|
Int val;
|
|
|
|
/* for slots to work */
|
2015-01-30 07:25:34 +00:00
|
|
|
yhandle_t CurSlot = Yap_StartSlots();
|
2011-03-22 11:07:49 +00:00
|
|
|
/* find out where we belong */
|
2016-03-29 01:55:12 +01:00
|
|
|
while (B < (choiceptr)top) {
|
|
|
|
oB = LCL0 - (CELL *)B;
|
2011-03-22 11:07:49 +00:00
|
|
|
B = B->cp_b;
|
2016-03-29 01:55:12 +01:00
|
|
|
}
|
2015-04-13 13:15:30 +01:00
|
|
|
PP = pe;
|
2015-10-22 00:45:47 +01:00
|
|
|
if (pe->PredFlags & (SWIEnvPredFlag | CArgsPredFlag)) {
|
2015-02-14 11:35:45 +00:00
|
|
|
// SWI Emulation
|
2011-02-14 14:59:15 +00:00
|
|
|
CPredicateV codev = (CPredicateV)exec_code;
|
2015-10-22 00:45:47 +01:00
|
|
|
struct foreign_context *ctx =
|
|
|
|
(struct foreign_context *)(&EXTRA_CBACK_ARG(pe->ArityOfPE, 1));
|
2011-03-22 11:07:49 +00:00
|
|
|
CELL *args = B->cp_args;
|
2011-02-14 14:59:15 +00:00
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
B = (choiceptr)(LCL0 - oB);
|
2011-02-14 14:59:15 +00:00
|
|
|
ctx->control = FRG_CUTTED;
|
|
|
|
ctx->engine = NULL; //(PL_local_data *)Yap_regp;
|
|
|
|
if (pe->PredFlags & CArgsPredFlag) {
|
2011-03-07 16:02:55 +00:00
|
|
|
val = execute_cargs_back(pe, exec_code, ctx PASS_REGS);
|
2011-02-14 14:59:15 +00:00
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
val = codev(Yap_InitSlots(pe->ArityOfPE, args), 0, ctx);
|
2011-02-14 14:59:15 +00:00
|
|
|
}
|
2015-02-14 11:35:45 +00:00
|
|
|
} else {
|
2016-03-29 01:55:12 +01:00
|
|
|
Int oYENV = LCL0 - YENV;
|
|
|
|
yamop *oP = P, *oCP = CP;
|
2015-02-14 11:35:45 +00:00
|
|
|
// YAP Native
|
2016-03-29 01:55:12 +01:00
|
|
|
B = (choiceptr)(LCL0 - oB);
|
2015-10-22 00:45:47 +01:00
|
|
|
val = exec_code(PASS_REGS1);
|
2016-03-29 01:55:12 +01:00
|
|
|
YENV = LCL0 - oYENV;
|
|
|
|
P = oP;
|
|
|
|
CP = oCP;
|
2015-02-14 11:35:45 +00:00
|
|
|
}
|
|
|
|
Yap_CloseSlots(CurSlot);
|
|
|
|
PP = NULL;
|
|
|
|
// B = LCL0-(CELL*)oB;
|
2016-03-29 01:55:12 +01:00
|
|
|
if (false && Yap_RaiseException()) {
|
|
|
|
return false;
|
2015-02-14 11:35:45 +00:00
|
|
|
} else { /* TRUE */
|
2016-03-29 01:55:12 +01:00
|
|
|
return true;
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2011-02-14 14:59:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Int YAP_ExecuteNext(PredEntry *pe, CPredicate exec_code) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
/* for slots to work */
|
|
|
|
Yap_StartSlots();
|
|
|
|
UInt ocp = LCL0 - (CELL *)B;
|
|
|
|
if (pe->PredFlags & (SWIEnvPredFlag | CArgsPredFlag)) {
|
2009-06-01 21:32:36 +01:00
|
|
|
Int val;
|
|
|
|
CPredicateV codev = (CPredicateV)exec_code;
|
2015-10-22 00:45:47 +01:00
|
|
|
struct foreign_context *ctx =
|
|
|
|
(struct foreign_context *)(&EXTRA_CBACK_ARG(pe->ArityOfPE, 1));
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2011-02-11 19:01:18 +00:00
|
|
|
PP = pe;
|
2009-06-01 21:32:36 +01:00
|
|
|
ctx->control = FRG_REDO;
|
2010-01-06 13:25:15 +00:00
|
|
|
if (pe->PredFlags & CArgsPredFlag) {
|
2011-03-07 16:02:55 +00:00
|
|
|
val = execute_cargs_back(pe, exec_code, ctx PASS_REGS);
|
2010-01-06 13:25:15 +00:00
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
val = codev(Yap_InitSlots(pe->ArityOfPE, &ARG1), 0, ctx);
|
2010-01-06 13:25:15 +00:00
|
|
|
}
|
2013-11-20 22:20:51 +00:00
|
|
|
/* we are below the original choice point ?? */
|
2011-02-14 14:59:15 +00:00
|
|
|
/* make sure we clean up the frames left by the user */
|
2011-02-11 19:01:18 +00:00
|
|
|
PP = NULL;
|
2009-06-01 21:32:36 +01:00
|
|
|
if (val == 0) {
|
2016-03-29 01:55:12 +01:00
|
|
|
if (Yap_RaiseException()) {
|
2015-10-22 00:45:47 +01:00
|
|
|
return FALSE;
|
2011-02-11 14:17:27 +00:00
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
return complete_fail(((choiceptr)(LCL0 - ocp)), TRUE PASS_REGS);
|
2011-02-11 14:17:27 +00:00
|
|
|
}
|
2009-06-01 21:32:36 +01:00
|
|
|
} else if (val == 1) { /* TRUE */
|
2015-10-22 00:45:47 +01:00
|
|
|
return complete_exit(((choiceptr)(LCL0 - ocp)), TRUE, FALSE PASS_REGS);
|
2009-06-01 21:32:36 +01:00
|
|
|
} else {
|
2011-03-15 23:49:28 +00:00
|
|
|
if ((val & REDO_PTR) == REDO_PTR)
|
2015-10-22 00:45:47 +01:00
|
|
|
ctx->context = (uintptr_t)(val & ~REDO_PTR);
|
2011-03-15 23:49:28 +00:00
|
|
|
else
|
2015-10-22 00:45:47 +01:00
|
|
|
ctx->context = (uintptr_t)((val & ~REDO_PTR) >> FRG_REDO_BITS);
|
2009-06-01 21:32:36 +01:00
|
|
|
}
|
2014-07-16 00:40:15 +01:00
|
|
|
/* fix dropped cps */
|
2015-10-22 00:45:47 +01:00
|
|
|
return complete_exit(((choiceptr)(LCL0 - ocp)), FALSE, FALSE PASS_REGS);
|
2011-02-15 07:39:27 +00:00
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
Int ret = (exec_code)(PASS_REGS1);
|
2011-02-15 07:39:27 +00:00
|
|
|
if (!ret) {
|
2016-04-05 02:20:16 +01:00
|
|
|
Yap_RaiseException();
|
|
|
|
}
|
2011-02-15 07:39:27 +00:00
|
|
|
return ret;
|
2009-06-01 21:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_ReallocSpaceFromYap(void *ptr, size_t size) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2009-11-19 21:09:22 +00:00
|
|
|
void *new_ptr;
|
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
while ((new_ptr = Yap_ReallocCodeSpace(ptr, size)) == NULL) {
|
2009-11-19 21:09:22 +00:00
|
|
|
if (!Yap_growheap(FALSE, size, NULL)) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
|
2009-11-19 21:09:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return new_ptr;
|
|
|
|
}
|
2016-03-29 01:55:12 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_AllocSpaceFromYap(size_t size) {
|
2015-04-13 13:15:30 +01:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
void *ptr;
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
2005-04-10 05:35:19 +01:00
|
|
|
while ((ptr = Yap_AllocCodeSpace(size)) == NULL) {
|
2004-01-23 02:23:51 +00:00
|
|
|
if (!Yap_growheap(FALSE, size, NULL)) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
|
2005-04-10 05:35:19 +01:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RECOVER_MACHINE_REGS();
|
2005-04-10 05:35:19 +01:00
|
|
|
return ptr;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_FreeSpaceFromYap(void *ptr) { Yap_FreeCodeSpace(ptr); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-11-05 16:59:30 +00:00
|
|
|
/* */
|
2015-11-09 18:22:43 +00:00
|
|
|
/**
|
2015-11-05 16:59:30 +00:00
|
|
|
* copy a string to a buffer, the buffer must have been malloced
|
2015-11-09 18:22:43 +00:00
|
|
|
*
|
2015-11-05 16:59:30 +00:00
|
|
|
* @param t the text, or string
|
|
|
|
* @param buf the user-provided buffer
|
|
|
|
* @param bufsize bu
|
2015-11-09 18:22:43 +00:00
|
|
|
*
|
|
|
|
* @return
|
2016-07-31 05:10:39 +01:00
|
|
|
*/ X_API char *
|
|
|
|
YAP_StringToBuffer(Term t, char *buf, unsigned int bufsize) {
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
|
|
|
inp.val.t = t;
|
2016-01-31 10:13:14 +00:00
|
|
|
inp.type = YAP_STRING_ATOMS_CODES | YAP_STRING_STRING | YAP_STRING_ATOM |
|
|
|
|
YAP_STRING_TRUNC | YAP_STRING_MALLOC;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.max = bufsize;
|
|
|
|
out.type = YAP_STRING_CHARS;
|
|
|
|
out.val.c = buf;
|
2015-11-05 16:59:30 +00:00
|
|
|
out.enc = ENC_ISO_UTF8;
|
2013-12-02 14:49:41 +00:00
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
2015-11-05 16:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
return out.val.c;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2001-05-21 21:00:05 +01:00
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_BufferToString(const char *s) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2001-05-21 21:00:05 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.c0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_CHARS;
|
|
|
|
out.type = YAP_STRING_CODES;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2001-05-21 21:00:05 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2008-07-24 17:02:04 +01:00
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_NBufferToString(const char *s, size_t len) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.c0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_CHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type = YAP_STRING_CODES | YAP_STRING_NCHARS | YAP_STRING_TRUNC;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_WideBufferToString(const wchar_t *s) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-10-22 00:45:47 +01:00
|
|
|
inp.val.w0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_WCHARS;
|
|
|
|
out.type = YAP_STRING_CODES;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_NWideBufferToString(const wchar_t *s, size_t len) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.w0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_WCHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type = YAP_STRING_CODES | YAP_STRING_NCHARS | YAP_STRING_TRUNC;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2004-05-14 17:33:47 +01:00
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_ReadBuffer(const char *s, Term *tp) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
Term t;
|
2004-05-14 17:33:47 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
LOCAL_ErrorMessage = NULL;
|
2016-01-31 10:13:14 +00:00
|
|
|
while (!(t = Yap_StringToTerm(s, strlen(s) + 1, &LOCAL_encoding,
|
|
|
|
GLOBAL_MaxPriority, tp))) {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_ErrorMessage) {
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!strcmp(LOCAL_ErrorMessage, "Stack Overflow")) {
|
|
|
|
if (!Yap_dogc(0, NULL PASS_REGS)) {
|
|
|
|
*tp = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
|
|
|
|
LOCAL_ErrorMessage = NULL;
|
|
|
|
RECOVER_H();
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(LOCAL_ErrorMessage, "Heap Overflow")) {
|
|
|
|
if (!Yap_growheap(FALSE, 0, NULL)) {
|
|
|
|
*tp = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
|
|
|
|
LOCAL_ErrorMessage = NULL;
|
|
|
|
RECOVER_H();
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(LOCAL_ErrorMessage, "Trail Overflow")) {
|
|
|
|
if (!Yap_growtrail(0, FALSE)) {
|
|
|
|
*tp = MkAtomTerm(Yap_LookupAtom(LOCAL_ErrorMessage));
|
|
|
|
LOCAL_ErrorMessage = NULL;
|
|
|
|
RECOVER_H();
|
|
|
|
return 0L;
|
|
|
|
}
|
2009-10-26 10:42:19 +00:00
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
RECOVER_H();
|
|
|
|
return 0L;
|
2009-10-26 10:42:19 +00:00
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = NULL;
|
2016-05-10 08:33:44 +01:00
|
|
|
return 0;
|
2009-10-26 10:42:19 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2007-06-04 13:28:02 +01:00
|
|
|
}
|
2004-05-14 17:33:47 +01:00
|
|
|
RECOVER_H();
|
2015-06-19 00:45:54 +01:00
|
|
|
return t;
|
2001-05-21 21:00:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Term YAP_BufferToAtomList(const char *s) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2001-05-21 21:00:05 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.c0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_CHARS;
|
|
|
|
out.type = YAP_STRING_ATOMS;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2001-05-21 21:00:05 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
2004-05-14 17:33:47 +01:00
|
|
|
return t;
|
2001-05-21 21:00:05 +01:00
|
|
|
}
|
|
|
|
|
2008-07-24 17:02:04 +01:00
|
|
|
/* copy a string of size len to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_NBufferToAtomList(const char *s, size_t len) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.c0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_CHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type = YAP_STRING_ATOMS | YAP_STRING_NCHARS | YAP_STRING_TRUNC;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_WideBufferToAtomList(const wchar_t *s) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.w0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_WCHARS;
|
|
|
|
out.type = YAP_STRING_ATOMS;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string of size len to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_NWideBufferToAtomList(const wchar_t *s, size_t len) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.w0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_WCHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type = YAP_STRING_ATOMS | YAP_STRING_NCHARS | YAP_STRING_TRUNC;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
2008-12-17 14:47:05 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string of size len to a buffer */
|
2016-03-29 01:55:12 +01:00
|
|
|
X_API Term YAP_NWideBufferToAtomDiffList(const wchar_t *s, Term t0,
|
|
|
|
size_t len) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-12-17 14:47:05 +00:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.w0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_WCHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type =
|
|
|
|
YAP_STRING_ATOMS | YAP_STRING_NCHARS | YAP_STRING_TRUNC | YAP_STRING_DIFF;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
out.dif = t0;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-12-17 14:47:05 +00:00
|
|
|
|
|
|
|
RECOVER_H();
|
2008-07-24 17:02:04 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_BufferToDiffList(const char *s, Term t0) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.c0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_CHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type = YAP_STRING_CODES | YAP_STRING_DIFF;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.dif = t0;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string of size len to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_NBufferToDiffList(const char *s, Term t0, size_t len) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.c0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_CHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type =
|
|
|
|
YAP_STRING_CODES | YAP_STRING_NCHARS | YAP_STRING_TRUNC | YAP_STRING_DIFF;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
out.dif = t0;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_WideBufferToDiffList(const wchar_t *s, Term t0) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.w0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_WCHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type = YAP_STRING_CODES | YAP_STRING_DIFF;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.dif = t0;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy a string of size len to a buffer */
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_NWideBufferToDiffList(const wchar_t *s, Term t0, size_t len) {
|
2015-04-13 13:15:30 +01:00
|
|
|
Term t;
|
2008-07-24 17:02:04 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
CACHE_REGS
|
|
|
|
seq_tv_t inp, out;
|
2015-08-07 22:57:53 +01:00
|
|
|
inp.val.w0 = s;
|
2013-12-02 14:49:41 +00:00
|
|
|
inp.type = YAP_STRING_WCHARS;
|
2015-10-22 00:45:47 +01:00
|
|
|
out.type =
|
|
|
|
YAP_STRING_CODES | YAP_STRING_NCHARS | YAP_STRING_TRUNC | YAP_STRING_DIFF;
|
2013-12-02 14:49:41 +00:00
|
|
|
out.sz = len;
|
|
|
|
out.max = len;
|
|
|
|
out.dif = t0;
|
|
|
|
if (!Yap_CVT_Text(&inp, &out PASS_REGS))
|
|
|
|
return 0L;
|
|
|
|
t = out.val.t;
|
2008-07-24 17:02:04 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_Error(int myerrno, Term t, const char *buf, ...) {
|
2005-03-02 18:35:49 +00:00
|
|
|
#define YAP_BUF_SIZE 512
|
|
|
|
va_list ap;
|
|
|
|
char tmpbuf[YAP_BUF_SIZE];
|
|
|
|
|
|
|
|
if (!myerrno)
|
2015-09-25 10:57:26 +01:00
|
|
|
myerrno = SYSTEM_ERROR_INTERNAL;
|
2005-03-02 18:35:49 +00:00
|
|
|
if (t == 0L)
|
|
|
|
t = TermNil;
|
|
|
|
if (buf != NULL) {
|
2015-10-22 00:45:47 +01:00
|
|
|
va_start(ap, buf);
|
|
|
|
#if HAVE_VSNPRINTF
|
|
|
|
(void)vsnprintf(tmpbuf, YAP_BUF_SIZE, buf, ap);
|
2005-03-02 18:35:49 +00:00
|
|
|
#else
|
2015-10-22 00:45:47 +01:00
|
|
|
(void)vsprintf(tmpbuf, buf, ap);
|
2005-03-02 18:35:49 +00:00
|
|
|
#endif
|
2015-10-22 00:45:47 +01:00
|
|
|
va_end(ap);
|
2005-03-02 18:35:49 +00:00
|
|
|
} else {
|
|
|
|
tmpbuf[0] = '\0';
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(myerrno, t, tmpbuf);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API PredEntry *YAP_FunctorToPred(Functor func) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2007-06-04 13:28:02 +01:00
|
|
|
return RepPredProp(PredPropByFunc(func, CurrentModule));
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API PredEntry *YAP_AtomToPred(Atom at) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
return RepPredProp(PredPropByAtom(at, CurrentModule));
|
2007-06-04 13:28:02 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API PredEntry *YAP_FunctorToPredInModule(Functor func, Term mod) {
|
2012-07-13 20:57:12 +01:00
|
|
|
return RepPredProp(PredPropByFunc(func, mod));
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API PredEntry *YAP_AtomToPredInModule(Atom at, Term mod) {
|
2015-04-13 13:15:30 +01:00
|
|
|
return RepPredProp(PredPropByAtom(at, mod));
|
2012-07-13 20:57:12 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
static int run_emulator(USES_REGS1) {
|
2007-06-04 13:28:02 +01:00
|
|
|
int out;
|
2016-07-31 05:10:39 +01:00
|
|
|
Yap_do_low_level_trace=1;
|
2015-10-22 00:45:47 +01:00
|
|
|
LOCAL_PrologMode &= ~(UserCCallMode | CCallMode);
|
2007-06-04 13:28:02 +01:00
|
|
|
out = Yap_absmi(0);
|
2013-12-10 22:58:02 +00:00
|
|
|
LOCAL_PrologMode |= UserCCallMode;
|
2007-06-04 13:28:02 +01:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_EnterGoal(PredEntry *pe, yhandle_t ptr, YAP_dogoalinfo *dgi) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-01-26 04:02:46 +00:00
|
|
|
bool out;
|
2007-06-04 13:28:02 +01:00
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
2013-12-15 08:34:52 +00:00
|
|
|
LOCAL_PrologMode = UserMode;
|
2007-06-04 13:28:02 +01:00
|
|
|
dgi->p = P;
|
2013-02-05 14:22:17 +00:00
|
|
|
dgi->cp = CP;
|
2013-11-20 22:20:51 +00:00
|
|
|
dgi->CurSlot = LOCAL_CurSlot;
|
2013-09-08 22:24:09 +01:00
|
|
|
// ensure our current ENV receives current P.
|
2014-06-04 22:08:37 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_PrepGoal(pe->ArityOfPE, Yap_AddressFromSlot(ptr), B PASS_REGS);
|
2013-09-08 22:24:09 +01:00
|
|
|
P = pe->CodeOfPred;
|
2015-10-22 00:45:47 +01:00
|
|
|
// __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),
|
|
|
|
// LOCAL_CurSlot);
|
|
|
|
dgi->b = LCL0 - (CELL *)B;
|
2015-04-13 13:15:30 +01:00
|
|
|
out = run_emulator(PASS_REGS1);
|
2007-10-16 00:48:46 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2013-11-20 22:20:51 +00:00
|
|
|
if (out) {
|
2014-07-16 00:40:15 +01:00
|
|
|
dgi->EndSlot = LOCAL_CurSlot;
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_StartSlots();
|
2014-06-11 19:35:45 +01:00
|
|
|
} else {
|
2015-10-22 00:45:47 +01:00
|
|
|
LOCAL_CurSlot =
|
|
|
|
dgi->CurSlot; // ignore any slots created within the called goal
|
2013-11-20 22:20:51 +00:00
|
|
|
}
|
2007-10-16 00:48:46 +01:00
|
|
|
return out;
|
2007-06-04 13:28:02 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2007-06-04 13:28:02 +01:00
|
|
|
choiceptr myB;
|
2015-01-26 04:02:46 +00:00
|
|
|
bool out;
|
2007-06-04 13:28:02 +01:00
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
myB = (choiceptr)(LCL0 - dgi->b);
|
2007-06-04 13:28:02 +01:00
|
|
|
CP = myB->cp_cp;
|
|
|
|
/* sanity check */
|
|
|
|
if (B >= myB) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
P = FAILCODE;
|
2013-03-10 16:37:27 +00:00
|
|
|
/* make sure we didn't leave live slots when we backtrack */
|
|
|
|
ASP = (CELL *)B;
|
2014-07-16 00:40:15 +01:00
|
|
|
LOCAL_CurSlot = dgi->EndSlot;
|
2015-04-13 13:15:30 +01:00
|
|
|
out = run_emulator(PASS_REGS1);
|
2009-06-15 16:29:31 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2013-11-20 22:20:51 +00:00
|
|
|
if (out) {
|
2016-07-31 05:10:39 +01:00
|
|
|
dgi->EndSlot = LOCAL_CurSlot;
|
|
|
|
} else {
|
|
|
|
LOCAL_CurSlot =
|
|
|
|
dgi->CurSlot; // ignore any slots created within the called goal
|
2013-11-20 22:20:51 +00:00
|
|
|
}
|
2009-06-15 16:29:31 +01:00
|
|
|
return out;
|
2007-06-04 13:28:02 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_LeaveGoal(int backtrack, YAP_dogoalinfo *dgi) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2007-06-04 13:28:02 +01:00
|
|
|
choiceptr myB;
|
|
|
|
|
2009-06-15 16:29:31 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
myB = (choiceptr)(LCL0 - dgi->b);
|
2007-11-01 20:50:31 +00:00
|
|
|
if (B > myB) {
|
|
|
|
/* someone cut us */
|
2007-06-04 13:28:02 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-11-01 20:50:31 +00:00
|
|
|
/* prune away choicepoints */
|
|
|
|
if (B != myB) {
|
2007-06-04 13:28:02 +01:00
|
|
|
#ifdef YAPOR
|
2007-11-01 20:50:31 +00:00
|
|
|
CUT_prune_to(myB);
|
2007-06-04 13:28:02 +01:00
|
|
|
#endif
|
2007-11-01 20:50:31 +00:00
|
|
|
B = myB;
|
|
|
|
}
|
|
|
|
/* if backtracking asked for, recover space and bindings */
|
2007-06-04 13:28:02 +01:00
|
|
|
if (backtrack) {
|
|
|
|
P = FAILCODE;
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_exec_absmi(true, YAP_EXEC_ABSMI);
|
2007-06-04 13:28:02 +01:00
|
|
|
/* recover stack space */
|
2014-01-19 21:15:05 +00:00
|
|
|
HR = B->cp_h;
|
2007-06-04 13:28:02 +01:00
|
|
|
TR = B->cp_tr;
|
|
|
|
#ifdef DEPTH_LIMIT
|
|
|
|
DEPTH = B->cp_depth;
|
|
|
|
#endif /* DEPTH_LIMIT */
|
|
|
|
YENV = ENV = B->cp_env;
|
2008-06-04 15:47:18 +01:00
|
|
|
} else {
|
|
|
|
Yap_TrimTrail();
|
2007-06-04 13:28:02 +01:00
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
/* recover local stack */
|
2013-02-05 14:22:17 +00:00
|
|
|
#ifdef DEPTH_LIMIT
|
2015-10-22 00:45:47 +01:00
|
|
|
DEPTH = ENV[E_DEPTH];
|
2013-02-05 14:22:17 +00:00
|
|
|
#endif
|
2011-07-09 12:56:11 +01:00
|
|
|
/* make sure we prune C-choicepoints */
|
2015-10-22 00:45:47 +01:00
|
|
|
if (POP_CHOICE_POINT(B->cp_b)) {
|
|
|
|
POP_EXECUTE();
|
|
|
|
}
|
|
|
|
ENV = (CELL *)(ENV[E_E]);
|
2013-02-05 14:22:17 +00:00
|
|
|
/* ASP should be set to the top of the local stack when we
|
|
|
|
did the call */
|
|
|
|
ASP = B->cp_env;
|
|
|
|
/* YENV should be set to the current environment */
|
2015-10-22 00:45:47 +01:00
|
|
|
YENV = ENV = (CELL *)((B->cp_env)[E_E]);
|
|
|
|
B = B->cp_b;
|
|
|
|
// SET_BB(B);
|
2013-02-05 14:22:17 +00:00
|
|
|
HB = PROTECT_FROZEN_H(B);
|
|
|
|
CP = dgi->cp;
|
2007-06-04 13:28:02 +01:00
|
|
|
P = dgi->p;
|
2013-11-20 22:20:51 +00:00
|
|
|
LOCAL_CurSlot = dgi->CurSlot;
|
2007-06-04 13:28:02 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_RunGoal(Term t) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2004-06-05 04:37:01 +01:00
|
|
|
Term out;
|
2002-06-18 03:27:53 +01:00
|
|
|
yamop *old_CP = CP;
|
2016-07-31 05:10:39 +01:00
|
|
|
yhandle_t cslot = LOCAL_CurSlot;
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_AllowRestart = FALSE;
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = UserMode;
|
2016-03-29 01:55:12 +01:00
|
|
|
out = Yap_RunTopGoal(t, true);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = UserCCallMode;
|
2015-06-19 00:45:54 +01:00
|
|
|
// should we catch the exception or pass it through?
|
|
|
|
// We'll pass it through
|
2016-03-29 01:55:12 +01:00
|
|
|
Yap_RaiseException();
|
2002-06-18 03:27:53 +01:00
|
|
|
if (out) {
|
|
|
|
P = (yamop *)ENV[E_CP];
|
|
|
|
ENV = (CELL *)ENV[E_E];
|
|
|
|
CP = old_CP;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_AllowRestart = TRUE;
|
2013-11-20 22:20:51 +00:00
|
|
|
// we are back to user code again, need slots */
|
2002-06-18 03:27:53 +01:00
|
|
|
} else {
|
2008-06-17 14:37:51 +01:00
|
|
|
ENV = B->cp_env;
|
2013-01-18 14:30:35 +00:00
|
|
|
ENV = (CELL *)ENV[E_E];
|
|
|
|
CP = old_CP;
|
2014-03-11 15:33:38 +00:00
|
|
|
HR = B->cp_h;
|
|
|
|
TR = B->cp_tr;
|
2008-06-04 14:58:42 +01:00
|
|
|
B = B->cp_b;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_AllowRestart = FALSE;
|
2015-10-22 00:45:47 +01:00
|
|
|
SET_ASP(ENV, E_CB * sizeof(CELL));
|
2014-03-11 15:33:38 +00:00
|
|
|
// make sure the slots are ok.
|
2002-06-18 03:27:53 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2016-07-31 05:10:39 +01:00
|
|
|
LOCAL_CurSlot = cslot;
|
2011-12-13 23:09:05 +00:00
|
|
|
return out;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_AllocExternalDataInStack(size_t bytes) {
|
2011-07-22 12:09:33 +01:00
|
|
|
Term t = Yap_AllocExternalDataInStack(EXTERNAL_BLOB, bytes);
|
2011-07-21 14:32:49 +01:00
|
|
|
if (t == TermNil)
|
|
|
|
return 0L;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsExternalDataInStackTerm(Term t) {
|
2011-07-22 12:09:33 +01:00
|
|
|
return IsExternalBlobTerm(t, EXTERNAL_BLOB);
|
2011-07-21 14:32:49 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_ExternalDataInStackFromTerm(Term t) {
|
|
|
|
return ExternalBlobFromTerm(t);
|
2011-07-21 14:32:49 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_opaque_tag_t YAP_NewOpaqueType(struct YAP_opaque_handler_struct *f) {
|
2011-07-22 12:09:33 +01:00
|
|
|
int i;
|
|
|
|
if (!GLOBAL_OpaqueHandlers) {
|
2015-10-22 00:45:47 +01:00
|
|
|
GLOBAL_OpaqueHandlers =
|
|
|
|
malloc(sizeof(opaque_handler_t) * (USER_BLOB_END - USER_BLOB_START));
|
2011-07-22 12:09:33 +01:00
|
|
|
if (!GLOBAL_OpaqueHandlers) {
|
|
|
|
/* no room */
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
} else if (GLOBAL_OpaqueHandlersCount == USER_BLOB_END - USER_BLOB_START) {
|
2011-07-22 12:09:33 +01:00
|
|
|
/* all types used */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i = GLOBAL_OpaqueHandlersCount++;
|
2015-10-22 00:45:47 +01:00
|
|
|
memcpy(GLOBAL_OpaqueHandlers + i, f, sizeof(opaque_handler_t));
|
|
|
|
return i + USER_BLOB_START;
|
2011-07-22 12:09:33 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Term YAP_NewOpaqueObject(YAP_opaque_tag_t tag, size_t bytes) {
|
2011-07-22 12:09:33 +01:00
|
|
|
Term t = Yap_AllocExternalDataInStack((CELL)tag, bytes);
|
|
|
|
if (t == TermNil)
|
|
|
|
return 0L;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API YAP_Bool YAP_IsOpaqueObjectTerm(Term t, YAP_opaque_tag_t tag) {
|
2011-07-22 12:09:33 +01:00
|
|
|
return IsExternalBlobTerm(t, (CELL)tag);
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_OpaqueObjectFromTerm(Term t) { return ExternalBlobFromTerm(t); }
|
2011-07-22 12:09:33 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API CELL *YAP_HeapStoreOpaqueTerm(Term t) {
|
2014-01-17 09:39:29 +00:00
|
|
|
return Yap_HeapStoreOpaqueTerm(t);
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_RunGoalOnce(Term t) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2008-06-04 14:58:42 +01:00
|
|
|
Term out;
|
|
|
|
yamop *old_CP = CP;
|
2011-08-31 21:59:30 +01:00
|
|
|
Int oldPrologMode = LOCAL_PrologMode;
|
2015-10-09 10:27:48 +01:00
|
|
|
yhandle_t CSlot;
|
2011-08-31 21:59:30 +01:00
|
|
|
|
2008-06-04 14:58:42 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_InitYaamRegs(0);
|
2015-10-09 10:27:48 +01:00
|
|
|
CSlot = Yap_StartSlots();
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = UserMode;
|
2015-06-19 00:45:54 +01:00
|
|
|
// Yap_heap_regs->yap_do_low_level_trace=true;
|
2016-03-29 01:55:12 +01:00
|
|
|
out = Yap_RunTopGoal(t, true);
|
2011-08-31 21:59:30 +01:00
|
|
|
LOCAL_PrologMode = oldPrologMode;
|
2015-10-09 10:27:48 +01:00
|
|
|
Yap_CloseSlots(CSlot);
|
2011-08-31 21:59:30 +01:00
|
|
|
if (!(oldPrologMode & UserCCallMode)) {
|
|
|
|
/* called from top-level */
|
|
|
|
LOCAL_AllowRestart = FALSE;
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return out;
|
|
|
|
}
|
2015-06-19 00:45:54 +01:00
|
|
|
// should we catch the exception or pass it through?
|
|
|
|
// We'll pass it through
|
2016-03-29 01:55:12 +01:00
|
|
|
Yap_RaiseException();
|
2008-06-04 14:58:42 +01:00
|
|
|
if (out) {
|
2011-07-12 05:14:26 +01:00
|
|
|
choiceptr cut_pt, ob;
|
2008-06-04 14:58:42 +01:00
|
|
|
|
2011-07-12 05:14:26 +01:00
|
|
|
ob = NULL;
|
2008-06-04 14:58:42 +01:00
|
|
|
cut_pt = B;
|
2015-10-22 00:45:47 +01:00
|
|
|
while (cut_pt->cp_ap != NOCODE) {
|
2011-07-09 12:56:11 +01:00
|
|
|
/* make sure we prune C-choicepoints */
|
2015-10-22 00:45:47 +01:00
|
|
|
if (POP_CHOICE_POINT(cut_pt->cp_b)) {
|
|
|
|
POP_EXECUTE();
|
|
|
|
}
|
2011-07-12 05:14:26 +01:00
|
|
|
ob = cut_pt;
|
2008-06-04 14:58:42 +01:00
|
|
|
cut_pt = cut_pt->cp_b;
|
|
|
|
}
|
|
|
|
#ifdef YAPOR
|
|
|
|
CUT_prune_to(cut_pt);
|
|
|
|
#endif
|
2011-07-12 05:14:26 +01:00
|
|
|
if (ob) {
|
|
|
|
B = ob;
|
|
|
|
Yap_TrimTrail();
|
|
|
|
}
|
2008-06-04 14:58:42 +01:00
|
|
|
B = cut_pt;
|
|
|
|
}
|
|
|
|
ASP = B->cp_env;
|
|
|
|
ENV = (CELL *)ASP[E_E];
|
|
|
|
B = (choiceptr)ASP[E_CB];
|
2015-10-22 00:45:47 +01:00
|
|
|
#ifdef DEPTH_LIMITxs
|
2008-06-04 14:58:42 +01:00
|
|
|
DEPTH = ASP[E_DEPTH];
|
|
|
|
#endif
|
|
|
|
P = (yamop *)ASP[E_CP];
|
|
|
|
CP = old_CP;
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_AllowRestart = FALSE;
|
2008-06-04 14:58:42 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2011-12-13 23:09:05 +00:00
|
|
|
return out;
|
2008-06-04 14:58:42 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_RestartGoal(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2016-04-18 01:09:10 +01:00
|
|
|
bool out;
|
2011-05-04 10:11:41 +01:00
|
|
|
if (LOCAL_AllowRestart) {
|
2007-03-22 11:12:22 +00:00
|
|
|
P = (yamop *)FAILCODE;
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = UserMode;
|
2014-09-22 18:10:51 +01:00
|
|
|
out = Yap_exec_absmi(TRUE, YAP_EXEC_ABSMI);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = UserCCallMode;
|
2007-03-22 11:12:22 +00:00
|
|
|
if (out == FALSE) {
|
|
|
|
/* cleanup */
|
|
|
|
Yap_trust_last();
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_AllowRestart = FALSE;
|
2007-03-22 11:12:22 +00:00
|
|
|
}
|
2015-04-13 13:15:30 +01:00
|
|
|
} else {
|
2007-03-22 11:12:22 +00:00
|
|
|
out = FALSE;
|
2002-05-14 19:24:34 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
return (out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_ShutdownGoal(int backtrack) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2007-05-14 17:44:12 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2011-05-04 10:11:41 +01:00
|
|
|
if (LOCAL_AllowRestart) {
|
2007-05-14 17:44:12 +01:00
|
|
|
choiceptr cut_pt;
|
|
|
|
|
|
|
|
cut_pt = B;
|
2015-10-22 00:45:47 +01:00
|
|
|
while (cut_pt->cp_ap != NOCODE) {
|
2011-07-09 12:56:11 +01:00
|
|
|
/* make sure we prune C-choicepoints */
|
2015-10-22 00:45:47 +01:00
|
|
|
if (POP_CHOICE_POINT(cut_pt->cp_b)) {
|
|
|
|
POP_EXECUTE();
|
|
|
|
}
|
2007-05-14 17:44:12 +01:00
|
|
|
cut_pt = cut_pt->cp_b;
|
|
|
|
}
|
|
|
|
#ifdef YAPOR
|
|
|
|
CUT_prune_to(cut_pt);
|
|
|
|
#endif
|
|
|
|
/* just force backtrack */
|
|
|
|
B = cut_pt;
|
2007-05-15 12:33:51 +01:00
|
|
|
if (backtrack) {
|
|
|
|
P = FAILCODE;
|
2014-09-22 18:10:51 +01:00
|
|
|
Yap_exec_absmi(TRUE, YAP_EXEC_ABSMI);
|
2007-05-15 12:33:51 +01:00
|
|
|
/* recover stack space */
|
2014-01-19 21:15:05 +00:00
|
|
|
HR = cut_pt->cp_h;
|
2007-05-15 12:33:51 +01:00
|
|
|
TR = cut_pt->cp_tr;
|
|
|
|
}
|
|
|
|
/* we can always recover the stack */
|
2008-06-04 14:58:42 +01:00
|
|
|
ASP = cut_pt->cp_env;
|
2007-05-14 17:44:12 +01:00
|
|
|
ENV = (CELL *)ASP[E_E];
|
|
|
|
B = (choiceptr)ASP[E_CB];
|
2008-06-04 15:47:18 +01:00
|
|
|
Yap_TrimTrail();
|
2015-10-22 00:45:47 +01:00
|
|
|
#ifdef DEPTH_LIMIT
|
2007-05-14 17:44:12 +01:00
|
|
|
DEPTH = ASP[E_DEPTH];
|
|
|
|
#endif
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_AllowRestart = FALSE;
|
2007-05-14 17:44:12 +01:00
|
|
|
}
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_ContinueGoal(void) {
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2015-01-26 04:02:46 +00:00
|
|
|
bool out;
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = UserMode;
|
2014-09-22 18:10:51 +01:00
|
|
|
out = Yap_exec_absmi(TRUE, YAP_EXEC_ABSMI);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = UserCCallMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
return (out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_PruneGoal(YAP_dogoalinfo *gi) {
|
2015-04-13 13:15:30 +01:00
|
|
|
CACHE_REGS
|
2013-02-05 14:22:17 +00:00
|
|
|
BACKUP_B();
|
2002-05-14 19:24:34 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
choiceptr myB = (choiceptr)(LCL0 - gi->b);
|
2013-02-05 14:22:17 +00:00
|
|
|
while (B != myB) {
|
2011-07-09 12:56:11 +01:00
|
|
|
/* make sure we prune C-choicepoints */
|
2015-10-22 00:45:47 +01:00
|
|
|
if (POP_CHOICE_POINT(B->cp_b)) {
|
|
|
|
POP_EXECUTE();
|
|
|
|
}
|
2012-09-26 22:08:17 +01:00
|
|
|
if (!B->cp_b)
|
|
|
|
break;
|
2002-05-14 19:24:34 +01:00
|
|
|
B = B->cp_b;
|
|
|
|
}
|
2016-05-10 08:33:44 +01:00
|
|
|
|
2009-12-04 22:58:46 +00:00
|
|
|
Yap_TrimTrail();
|
2002-05-14 19:24:34 +01:00
|
|
|
|
|
|
|
RECOVER_B();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API bool YAP_GoalHasException(Term *t) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2002-05-14 19:24:34 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2016-03-29 01:55:12 +01:00
|
|
|
if (t)
|
2016-04-05 02:20:16 +01:00
|
|
|
*t = Yap_PeekException();
|
2016-03-29 01:55:12 +01:00
|
|
|
return Yap_PeekException();
|
2016-04-05 02:20:16 +01:00
|
|
|
}
|
2005-08-17 14:35:52 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_ClearExceptions(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2012-12-13 18:12:50 +00:00
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
Yap_ResetException(worker_id);
|
2002-05-14 19:24:34 +01:00
|
|
|
}
|
|
|
|
|
2016-05-10 08:33:44 +01:00
|
|
|
X_API int YAP_InitConsult(int mode, const char *filename, char *full,
|
|
|
|
int *osnop) {
|
2015-12-15 09:28:43 +00:00
|
|
|
CACHE_REGS
|
2016-05-10 08:33:44 +01:00
|
|
|
FILE *f = NULL;
|
2015-06-19 00:45:54 +01:00
|
|
|
int sno;
|
2016-05-10 08:33:44 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-07-06 11:56:16 +01:00
|
|
|
if (mode == YAP_BOOT_MODE) {
|
|
|
|
mode = YAP_CONSULT_MODE;
|
|
|
|
}
|
2015-06-19 00:45:54 +01:00
|
|
|
bool consulted = (mode == YAP_CONSULT_MODE);
|
|
|
|
Yap_init_consult(consulted, filename);
|
2016-07-31 05:10:39 +01:00
|
|
|
const char *fl = Yap_findFile(filename, NULL, BootFilePath, full, true,
|
|
|
|
YAP_BOOT_PL, true, true);
|
2016-04-10 14:21:17 +01:00
|
|
|
if (!fl)
|
2016-02-11 14:00:56 +00:00
|
|
|
return -1;
|
2016-04-10 14:21:17 +01:00
|
|
|
f = fopen(fl, "r");
|
2015-06-19 00:45:54 +01:00
|
|
|
if (!f)
|
|
|
|
return -1;
|
2016-05-10 08:33:44 +01:00
|
|
|
if (!f) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
sno = Yap_OpenStream(f, NULL, TermNil, Input_Stream_f);
|
|
|
|
*osnop = Yap_CheckAlias(AtomLoopStream);
|
|
|
|
if (!Yap_AddAlias(AtomLoopStream, sno)) {
|
|
|
|
Yap_CloseStream(sno);
|
2015-09-29 23:06:49 +01:00
|
|
|
sno = -1;
|
|
|
|
}
|
2016-05-10 08:33:44 +01:00
|
|
|
GLOBAL_Stream[sno].name = Yap_LookupAtom(fl);
|
2015-10-22 00:45:47 +01:00
|
|
|
GLOBAL_Stream[sno].user_name = MkAtomTerm(Yap_LookupAtom(filename));
|
2016-05-10 08:33:44 +01:00
|
|
|
GLOBAL_Stream[sno].encoding = ENC_ISO_UTF8;
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2015-10-05 10:27:25 +01:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2015-06-19 00:45:54 +01:00
|
|
|
return sno;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API FILE *YAP_TermToStream(Term t) {
|
2011-05-25 13:10:39 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2016-04-10 14:21:17 +01:00
|
|
|
FILE *s;
|
2011-05-25 13:10:39 +01:00
|
|
|
|
2012-02-18 11:01:06 +00:00
|
|
|
if (IsVarTerm(t) || !IsAtomTerm(t))
|
|
|
|
return NULL;
|
2015-10-22 00:45:47 +01:00
|
|
|
if ((s = Yap_GetStreamHandle(t)->file)) {
|
2011-05-25 13:10:39 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_EndConsult(int sno, int *osnop) {
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-06-19 00:45:54 +01:00
|
|
|
Yap_CloseStream(sno);
|
2015-10-22 00:45:47 +01:00
|
|
|
if (osnop >= 0)
|
|
|
|
Yap_AddAlias(AtomLoopStream, *osnop);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_end_consult();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_Read(FILE *f) {
|
|
|
|
Term o;
|
|
|
|
int sno = Yap_OpenStream(f, NULL, TermNil, Input_Stream_f);
|
2013-11-25 10:22:07 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
o = Yap_read_term(sno, TermNil, 1);
|
2015-06-19 00:45:54 +01:00
|
|
|
Yap_ReleaseStream(sno);
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return o;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_ReadFromStream(int sno) {
|
|
|
|
Term o;
|
2011-02-15 07:39:27 +00:00
|
|
|
|
2015-06-19 00:45:54 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
o = Yap_read_term(sno, TermNil, 1);
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2015-06-19 00:45:54 +01:00
|
|
|
return o;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2016-02-21 19:06:25 +00:00
|
|
|
X_API Term YAP_ReadClauseFromStream(int sno) {
|
|
|
|
Term o;
|
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
o = Yap_read_term(sno, TermNil, -1);
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_Write(Term t, FILE *f, int flags) {
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
int sno = Yap_OpenStream(f, NULL, TermNil, Output_Stream_f);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-12-15 08:38:56 +00:00
|
|
|
Yap_plwrite(t, GLOBAL_Stream + sno, 0, flags, GLOBAL_MaxPriority);
|
2015-06-19 00:45:54 +01:00
|
|
|
Yap_ReleaseStream(sno);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_CopyTerm(Term t) {
|
2007-05-14 17:44:12 +01:00
|
|
|
Term tn;
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
|
|
|
tn = Yap_CopyTerm(t);
|
|
|
|
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
|
|
|
|
return tn;
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:19:58 +01:00
|
|
|
X_API char *YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) {
|
2015-06-19 00:45:54 +01:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
size_t length;
|
2012-09-18 23:06:26 +01:00
|
|
|
char *b;
|
|
|
|
|
2004-05-14 18:56:47 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2016-04-22 18:19:58 +01:00
|
|
|
if ((b = Yap_TermToString(t, &length, LOCAL_encoding, flags)) != buf) {
|
2012-09-18 23:06:26 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2016-04-22 18:19:58 +01:00
|
|
|
return b;
|
2012-09-18 23:06:26 +01:00
|
|
|
}
|
2004-05-14 18:56:47 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2016-04-22 18:19:58 +01:00
|
|
|
return buf;
|
2012-09-18 23:06:26 +01:00
|
|
|
}
|
|
|
|
|
2016-04-22 18:19:58 +01:00
|
|
|
/// write a a term to n user-provided buffer: make sure not tp
|
|
|
|
/// overflow the buffer even if the text is much larger.
|
|
|
|
X_API int YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze,
|
|
|
|
size_t *lengthp, encoding_t enc, int flags) {
|
2012-09-18 23:06:26 +01:00
|
|
|
char *b;
|
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
2016-04-22 18:19:58 +01:00
|
|
|
b = Yap_TermToString(t, lengthp, enc, flags);
|
|
|
|
if (*lengthp >= sze)
|
|
|
|
*lengthp = sze;
|
|
|
|
strncpy(buf, b, sze);
|
2012-09-18 23:06:26 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2016-04-22 18:19:58 +01:00
|
|
|
return true;
|
2004-05-14 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API char *YAP_CompileClause(Term t) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2002-12-27 16:53:09 +00:00
|
|
|
yamop *codeaddr;
|
2013-01-13 11:48:31 +00:00
|
|
|
Term mod = CurrentModule;
|
2004-12-08 00:56:35 +00:00
|
|
|
Term tn = TermNil;
|
2001-11-15 00:01:43 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
|
2009-05-12 23:01:53 +01:00
|
|
|
/* allow expansion during stack initialization */
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
ARG1 = t;
|
2004-06-29 20:04:46 +01:00
|
|
|
YAPEnterCriticalSection();
|
2015-10-22 00:45:47 +01:00
|
|
|
codeaddr = Yap_cclause(t, 0, mod, t);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (codeaddr != NULL) {
|
|
|
|
t = Deref(ARG1); /* just in case there was an heap overflow */
|
2016-02-21 11:30:13 +00:00
|
|
|
if (!Yap_addclause(t, codeaddr, TermAssertz, mod, &tn)) {
|
2004-11-18 22:32:40 +00:00
|
|
|
YAPLeaveCriticalSection();
|
2011-05-23 16:19:47 +01:00
|
|
|
return LOCAL_ErrorMessage;
|
2004-11-18 22:32:40 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2004-06-29 20:04:46 +01:00
|
|
|
YAPLeaveCriticalSection();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
if (Yap_get_signal(YAP_CDOVF_SIGNAL)) {
|
2014-04-23 22:41:12 +01:00
|
|
|
if (!Yap_locked_growheap(FALSE, 0, NULL)) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "YAP failed to grow heap: %s",
|
|
|
|
LOCAL_ErrorMessage);
|
2004-07-23 04:37:17 +01:00
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
return (LOCAL_ErrorMessage);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2010-01-26 12:20:46 +00:00
|
|
|
static int yap_lineno = 0;
|
|
|
|
|
|
|
|
/* do initial boot by consulting the file boot.yap */
|
2016-05-10 08:33:44 +01:00
|
|
|
static void do_bootfile(const char *bootfilename USES_REGS) {
|
2010-01-26 12:20:46 +00:00
|
|
|
Term t;
|
2015-09-29 23:06:49 +01:00
|
|
|
int bootfile, osno;
|
2015-10-22 00:45:47 +01:00
|
|
|
Functor functor_query = Yap_MkFunctor(Yap_LookupAtom("?-"), 1);
|
|
|
|
Functor functor_command1 = Yap_MkFunctor(Yap_LookupAtom(":-"), 1);
|
2016-05-10 08:33:44 +01:00
|
|
|
char full[YAP_FILENAME_MAX + 1];
|
2010-01-26 12:20:46 +00:00
|
|
|
|
|
|
|
/* consult boot.pl */
|
|
|
|
/* the consult mode does not matter here, really */
|
2016-05-10 08:33:44 +01:00
|
|
|
bootfile = YAP_InitConsult(YAP_BOOT_MODE, bootfilename, full, &osno);
|
2015-10-22 00:45:47 +01:00
|
|
|
if (bootfile < 0) {
|
|
|
|
fprintf(stderr, "[ FATAL ERROR: could not open bootfile %s ]\n",
|
|
|
|
bootfilename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
CACHE_REGS
|
|
|
|
YAP_Reset(YAP_FULL_RESET);
|
|
|
|
Yap_StartSlots();
|
2016-02-21 19:06:25 +00:00
|
|
|
t = YAP_ReadClauseFromStream(bootfile);
|
2016-07-31 05:10:39 +01:00
|
|
|
|
2016-04-05 02:20:16 +01:00
|
|
|
// Yap_DebugPlWriteln(t);
|
2015-10-22 00:45:47 +01:00
|
|
|
if (t == 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"[ SYNTAX ERROR: while parsing bootfile %s at line %d ]\n",
|
|
|
|
bootfilename, yap_lineno);
|
|
|
|
} else if (YAP_IsVarTerm(t) || t == TermNil) {
|
|
|
|
fprintf(stderr, "[ line %d: term cannot be compiled ]", yap_lineno);
|
|
|
|
} else if (YAP_IsPairTerm(t)) {
|
|
|
|
fprintf(stderr, "[ SYSTEM ERROR: consult not allowed in boot file ]\n");
|
|
|
|
fprintf(stderr, "error found at line %d and pos %d", yap_lineno,
|
|
|
|
fseek(GLOBAL_Stream[bootfile].file, 0L, SEEK_CUR));
|
|
|
|
} else if (IsApplTerm(t) && (FunctorOfTerm(t) == functor_query ||
|
|
|
|
FunctorOfTerm(t) == functor_command1)) {
|
|
|
|
YAP_RunGoalOnce(ArgOfTerm(1, t));
|
|
|
|
} else {
|
|
|
|
Term ts[2];
|
|
|
|
char *ErrorMessage;
|
|
|
|
Functor fun = Yap_MkFunctor(Yap_LookupAtom("$prepare_clause"), 2);
|
|
|
|
PredEntry *pe = RepPredProp(PredPropByFunc(fun, PROLOG_MODULE));
|
|
|
|
|
|
|
|
if (pe->OpcodeOfPred != UNDEF_OPCODE && pe->OpcodeOfPred != FAIL_OPCODE) {
|
|
|
|
ts[0] = t;
|
|
|
|
RESET_VARIABLE(ts + 1);
|
|
|
|
if (YAP_RunGoal(Yap_MkApplTerm(fun, 2, ts)))
|
|
|
|
t = ts[1];
|
|
|
|
}
|
|
|
|
ErrorMessage = YAP_CompileClause(t);
|
|
|
|
if (ErrorMessage) {
|
|
|
|
fprintf(stderr, "%s", ErrorMessage);
|
|
|
|
}
|
2011-02-15 07:39:27 +00:00
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
} while (t != TermEof);
|
|
|
|
|
|
|
|
YAP_EndConsult(bootfile, &osno);
|
2013-11-16 00:27:02 +00:00
|
|
|
#if DEBUG
|
2012-03-09 11:46:34 +00:00
|
|
|
if (Yap_output_msg)
|
2015-10-22 00:45:47 +01:00
|
|
|
fprintf(stderr, "Boot loaded\n");
|
2010-01-26 12:20:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
/**
|
|
|
|
YAP_DelayInit()
|
|
|
|
|
|
|
|
ensures initialization is done after engine creation.
|
|
|
|
It receives a pointer to function and a string describing
|
|
|
|
the module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool initialized = false;
|
|
|
|
static int n_mdelays = 0;
|
|
|
|
static YAP_delaymodule_t *m_delays;
|
|
|
|
|
|
|
|
X_API bool YAP_DelayInit(YAP_ModInit_t f, const char s[]) {
|
|
|
|
if (initialized)
|
|
|
|
return false;
|
|
|
|
if (m_delays) {
|
|
|
|
m_delays = realloc(m_delays, (n_mdelays + 1) * sizeof(YAP_delaymodule_t));
|
|
|
|
} else {
|
|
|
|
m_delays = malloc(sizeof(YAP_delaymodule_t));
|
|
|
|
}
|
|
|
|
m_delays[n_mdelays].f = f;
|
|
|
|
m_delays[n_mdelays].s = s;
|
|
|
|
n_mdelays++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start_modules(void) {
|
|
|
|
Term cm = CurrentModule;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < n_mdelays; i++) {
|
|
|
|
CurrentModule = MkAtomTerm(YAP_LookupAtom(m_delays[i].s));
|
|
|
|
m_delays[i].f();
|
|
|
|
}
|
|
|
|
CurrentModule = cm;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* this routine is supposed to be called from an external program
|
|
|
|
that wants to control Yap */
|
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
YAP_file_type_t YAP_Init(YAP_init_args *yap_init) {
|
|
|
|
YAP_file_type_t restore_result = yap_init->boot_file_type;
|
2016-05-10 08:33:44 +01:00
|
|
|
bool do_bootstrap = (restore_result & YAP_CONSULT_MODE);
|
2009-03-27 15:15:54 +00:00
|
|
|
CELL Trail = 0, Stack = 0, Heap = 0, Atts = 0;
|
2016-03-29 01:55:12 +01:00
|
|
|
char boot_file[YAP_FILENAME_MAX + 1];
|
2016-07-31 05:10:39 +01:00
|
|
|
|
2016-05-10 08:33:44 +01:00
|
|
|
Int rc;
|
2016-07-31 05:10:39 +01:00
|
|
|
const char *yroot;
|
2012-04-12 23:25:04 +01:00
|
|
|
|
|
|
|
/* ignore repeated calls to YAP_Init */
|
2015-11-05 16:38:18 +00:00
|
|
|
if (initialized)
|
2016-07-31 05:10:39 +01:00
|
|
|
return YAP_FOUND_BOOT_ERROR;
|
|
|
|
initialized = true;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2016-05-10 08:33:44 +01:00
|
|
|
Yap_page_size = Yap_InitPageSize(); /* init memory page size, required by
|
|
|
|
later functions */
|
2011-05-23 16:19:47 +01:00
|
|
|
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA)
|
|
|
|
Yap_init_yapor_global_local_memory();
|
|
|
|
#endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA */
|
2011-07-22 22:48:30 +01:00
|
|
|
GLOBAL_PrologShouldHandleInterrupts = yap_init->PrologShouldHandleInterrupts;
|
2015-11-09 18:22:43 +00:00
|
|
|
Yap_InitSysbits(0); /* init signal handling and time, required by later
|
2015-10-22 00:45:47 +01:00
|
|
|
functions */
|
2011-05-25 16:40:36 +01:00
|
|
|
GLOBAL_argv = yap_init->Argv;
|
|
|
|
GLOBAL_argc = yap_init->Argc;
|
2016-07-31 05:10:39 +01:00
|
|
|
if (0 && ((YAP_QLY && yap_init->SavedState) ||
|
|
|
|
(YAP_BOOT_PL && (yap_init->YapPrologBootFile)))) {
|
|
|
|
yroot = ".";
|
|
|
|
} else {
|
|
|
|
yroot = BootFilePath;
|
2016-05-10 08:33:44 +01:00
|
|
|
}
|
2016-07-31 05:10:39 +01:00
|
|
|
if (yap_init->SavedState == NULL) {
|
|
|
|
yap_init->SavedState = YAP_STARTUP;
|
2015-04-15 11:21:15 +01:00
|
|
|
}
|
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
if (yap_init->SavedState == NULL)
|
|
|
|
yap_init->SavedState = YAP_STARTUP;
|
|
|
|
#else
|
|
|
|
yap_init->SavedState = Yap_findFile(yap_init->SavedState, YAP_STARTUP, yroot,
|
|
|
|
boot_file, true, YAP_QLY, true, true);
|
|
|
|
#endif
|
|
|
|
if (yap_init->SavedState == NULL) {
|
|
|
|
restore_result = YAP_BOOT_PL;
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2016-07-31 05:10:39 +01:00
|
|
|
|
|
|
|
if (restore_result == YAP_BOOT_PL) {
|
|
|
|
#if USE_DL_MALLOC
|
|
|
|
if (yap_init->YapPrologBootFile == NULL)
|
|
|
|
yap_init->YapPrologBootFile = BootFile;
|
|
|
|
#else
|
|
|
|
yap_init->YapPrologBootFile =
|
|
|
|
Yap_findFile(yap_init->YapPrologBootFile, BootFile, yroot, boot_file,
|
|
|
|
true, YAP_BOOT_PL, true, true);
|
2008-04-02 22:44:07 +01:00
|
|
|
#endif
|
2016-07-31 05:10:39 +01:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (yap_init->TrailSize == 0) {
|
2016-07-31 05:10:39 +01:00
|
|
|
if (Trail == 0)
|
2001-04-09 20:54:03 +01:00
|
|
|
Trail = DefTrailSpace;
|
|
|
|
} else {
|
|
|
|
Trail = yap_init->TrailSize;
|
|
|
|
}
|
2016-07-31 05:10:39 +01:00
|
|
|
// Atts = yap_init->AttsSize;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (yap_init->StackSize == 0) {
|
2016-07-31 05:10:39 +01:00
|
|
|
Stack = DefStackSpace;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
Stack = yap_init->StackSize;
|
|
|
|
}
|
2016-07-31 05:10:39 +01:00
|
|
|
#ifndef USE_SYSTEM_MALLOC
|
2001-04-09 20:54:03 +01:00
|
|
|
if (yap_init->HeapSize == 0) {
|
|
|
|
if (Heap == 0)
|
2015-04-13 13:15:30 +01:00
|
|
|
Heap = DefHeapSpace;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
Heap = yap_init->HeapSize;
|
|
|
|
}
|
2016-07-31 05:10:39 +01:00
|
|
|
#endif
|
2016-04-05 02:20:16 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_InitWorkspace(Heap, Stack, Trail, Atts, yap_init->MaxTableSpaceSize,
|
|
|
|
yap_init->NumberWorkers, yap_init->SchedulerLoop,
|
|
|
|
yap_init->DelayedReleaseLoad);
|
2015-07-06 11:56:16 +01:00
|
|
|
//
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
CACHE_REGS
|
2008-03-13 18:41:52 +00:00
|
|
|
if (yap_init->QuietMode) {
|
2015-10-22 00:45:47 +01:00
|
|
|
setVerbosity(TermSilent);
|
2008-03-13 18:41:52 +00:00
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
{
|
|
|
|
if (yap_init->YapPrologRCFile != NULL) {
|
2010-08-17 13:02:28 +01:00
|
|
|
/*
|
2015-10-22 00:45:47 +01:00
|
|
|
This must be done before restore, otherwise
|
|
|
|
restore will print out messages ....
|
2010-08-17 13:02:28 +01:00
|
|
|
*/
|
2015-10-22 00:45:47 +01:00
|
|
|
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
|
|
|
|
yap_init->HaltAfterConsult);
|
2005-03-02 18:35:49 +00:00
|
|
|
}
|
2016-05-10 08:33:44 +01:00
|
|
|
/* tell the system who should cope with interrupts */
|
2010-08-17 13:02:28 +01:00
|
|
|
Yap_ExecutionMode = yap_init->ExecutionMode;
|
|
|
|
if (do_bootstrap) {
|
2016-07-31 05:10:39 +01:00
|
|
|
restore_result |= YAP_BOOT_PL;
|
2016-04-18 01:09:10 +01:00
|
|
|
} else { // try always to boot from the saved state.
|
2016-05-10 08:33:44 +01:00
|
|
|
if (restore_result == YAP_QLY) {
|
2016-04-18 16:40:17 +01:00
|
|
|
if (!Yap_SavedInfo(yap_init->SavedState, yap_init->YapLibDir, &Trail,
|
|
|
|
&Stack, &Heap)) {
|
2016-07-31 05:10:39 +01:00
|
|
|
restore_result = YAP_BOOT_PL;
|
2016-05-10 08:33:44 +01:00
|
|
|
} else {
|
|
|
|
restore_result =
|
|
|
|
Yap_Restore(yap_init->SavedState, yap_init->YapLibDir);
|
|
|
|
}
|
2016-07-31 05:10:39 +01:00
|
|
|
if (restore_result == YAP_FOUND_BOOT_ERROR) {
|
2016-05-10 08:33:44 +01:00
|
|
|
restore_result = YAP_BOOT_PL;
|
2016-04-18 16:40:17 +01:00
|
|
|
}
|
2016-04-18 01:09:10 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-19 00:45:54 +01:00
|
|
|
GLOBAL_FAST_BOOT_FLAG = yap_init->FastBoot;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if defined(YAPOR) || defined(TABLING)
|
2011-06-01 18:51:12 +01:00
|
|
|
Yap_init_root_frames();
|
2011-05-09 19:36:51 +01:00
|
|
|
#endif /* YAPOR || TABLING */
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef YAPOR
|
2011-06-01 18:51:12 +01:00
|
|
|
Yap_init_yapor_workers();
|
2013-12-17 15:57:24 +00:00
|
|
|
#if YAPOR_THREADS
|
|
|
|
if (Yap_thread_self() != 0) {
|
|
|
|
#else
|
2010-08-17 13:02:28 +01:00
|
|
|
if (worker_id != 0) {
|
2013-12-17 15:57:24 +00:00
|
|
|
#endif
|
2011-05-09 19:36:51 +01:00
|
|
|
#if defined(YAPOR_COPY) || defined(YAPOR_SBA)
|
2010-08-17 13:02:28 +01:00
|
|
|
/*
|
2015-10-22 00:45:47 +01:00
|
|
|
In the SBA we cannot just happily inherit registers
|
|
|
|
from the other workers
|
2010-08-17 13:02:28 +01:00
|
|
|
*/
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_InitYaamRegs(worker_id);
|
2011-05-09 19:36:51 +01:00
|
|
|
#endif /* YAPOR_COPY || YAPOR_SBA */
|
|
|
|
#ifndef YAPOR_THREADS
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_InitPreAllocCodeSpace(0);
|
2011-05-09 19:36:51 +01:00
|
|
|
#endif /* YAPOR_THREADS */
|
2010-08-17 13:02:28 +01:00
|
|
|
/* slaves, waiting for work */
|
|
|
|
CurrentModule = USER_MODULE;
|
|
|
|
P = GETWORK_FIRST_TIME;
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_exec_absmi(FALSE, YAP_EXEC_ABSMI);
|
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
|
|
|
"abstract machine unexpected exit (YAP_Init)");
|
2010-08-17 13:02:28 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* YAPOR */
|
2010-08-17 13:02:28 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
}
|
2009-05-12 23:01:53 +01:00
|
|
|
/* make sure we do this after restore */
|
|
|
|
if (yap_init->MaxStackSize) {
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_AllowLocalExpansion = FALSE;
|
2009-05-12 23:01:53 +01:00
|
|
|
} else {
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_AllowLocalExpansion = TRUE;
|
2009-05-12 23:01:53 +01:00
|
|
|
}
|
|
|
|
if (yap_init->MaxGlobalSize) {
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_AllowGlobalExpansion = FALSE;
|
2009-05-12 23:01:53 +01:00
|
|
|
} else {
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_AllowGlobalExpansion = TRUE;
|
2009-05-12 23:01:53 +01:00
|
|
|
}
|
|
|
|
if (yap_init->MaxTrailSize) {
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_AllowTrailExpansion = FALSE;
|
2009-05-12 23:01:53 +01:00
|
|
|
} else {
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_AllowTrailExpansion = TRUE;
|
2009-05-12 23:01:53 +01:00
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
if (yap_init->YapPrologRCFile) {
|
|
|
|
Yap_PutValue(AtomConsultOnBoot,
|
|
|
|
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologRCFile)));
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
This must be done again after restore, as yap_flags
|
|
|
|
has been overwritten ....
|
|
|
|
*/
|
2015-10-22 00:45:47 +01:00
|
|
|
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
|
|
|
|
yap_init->HaltAfterConsult);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-10-21 17:07:07 +01:00
|
|
|
if (yap_init->YapPrologTopLevelGoal) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_PutValue(AtomTopLevelGoal,
|
|
|
|
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologTopLevelGoal)));
|
2005-10-21 17:07:07 +01:00
|
|
|
}
|
|
|
|
if (yap_init->YapPrologGoal) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_PutValue(AtomInitGoal,
|
|
|
|
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologGoal)));
|
2005-10-21 17:07:07 +01:00
|
|
|
}
|
2005-10-28 18:38:50 +01:00
|
|
|
if (yap_init->YapPrologAddPath) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_PutValue(AtomExtendFileSearchPath,
|
|
|
|
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologAddPath)));
|
2005-10-28 18:38:50 +01:00
|
|
|
}
|
2008-03-13 18:41:52 +00:00
|
|
|
if (yap_init->QuietMode) {
|
2015-10-22 00:45:47 +01:00
|
|
|
setVerbosity(TermSilent);
|
2008-03-13 18:41:52 +00:00
|
|
|
}
|
2016-07-31 05:10:39 +01:00
|
|
|
if (restore_result == YAP_QLY) {
|
|
|
|
setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG,
|
|
|
|
MkAtomTerm(Yap_LookupAtom(yap_init->SavedState)));
|
2016-04-12 16:05:20 +01:00
|
|
|
LOCAL_PrologMode &= ~BootMode;
|
2016-07-31 05:10:39 +01:00
|
|
|
CurrentModule = LOCAL_SourceModule = USER_MODULE;
|
|
|
|
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, true);
|
2016-05-10 08:33:44 +01:00
|
|
|
} else {
|
|
|
|
if (!yap_init->YapPrologBootFile)
|
|
|
|
yap_init->YapPrologBootFile = BootFile;
|
2016-07-31 05:10:39 +01:00
|
|
|
rc = YAP_BOOT_PL;
|
|
|
|
do_bootfile(yap_init->YapPrologBootFile);
|
|
|
|
setAtomicGlobalPrologFlag(
|
|
|
|
RESOURCE_DATABASE_FLAG,
|
|
|
|
MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologBootFile)));
|
|
|
|
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, false);
|
|
|
|
}
|
|
|
|
start_modules();
|
2016-05-10 08:33:44 +01:00
|
|
|
return rc;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
#if (DefTrailSpace < MinTrailSpace)
|
|
|
|
#undef DefTrailSpace
|
|
|
|
#define DefTrailSpace MinTrailSpace
|
|
|
|
#endif
|
2014-04-29 18:17:23 +01:00
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
#if (DefStackSpace < MinStackSpace)
|
|
|
|
#undef DefStackSpace
|
|
|
|
#define DefStackSpace MinStackSpace
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (DefHeapSpace < MinHeapSpace)
|
|
|
|
#undef DefHeapSpace
|
|
|
|
#define DefHeapSpace MinHeapSpace
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEFAULT_NUMBERWORKERS 1
|
|
|
|
#define DEFAULT_SCHEDULERLOOP 10
|
|
|
|
#define DEFAULT_DELAYEDRELEASELOAD 3
|
|
|
|
|
|
|
|
X_API YAP_file_type_t YAP_FastInit(char saved_state[], int argc, char *argv[]) {
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_init_args init_args;
|
2016-07-31 05:10:39 +01:00
|
|
|
YAP_file_type_t out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
if ((out = Yap_InitDefaults(&init_args, saved_state, argc, argv)) !=
|
|
|
|
YAP_FOUND_BOOT_ERROR)
|
2014-04-29 18:17:23 +01:00
|
|
|
out = YAP_Init(&init_args);
|
2016-07-31 05:10:39 +01:00
|
|
|
if (out == YAP_FOUND_BOOT_ERROR) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(init_args.ErrorNo, TermNil, init_args.ErrorCause);
|
2005-03-02 18:35:49 +00:00
|
|
|
}
|
|
|
|
return out;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_PutValue(Atom at, Term t) { Yap_PutValue(at, t); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_GetValue(Atom at) { return (Yap_GetValue(at)); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_CompareTerms(Term t1, Term t2) {
|
2004-08-11 17:14:55 +01:00
|
|
|
return Yap_compare_terms(t1, t2);
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2004-08-11 17:14:55 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_Reset(yap_reset_t mode) {
|
2013-02-08 16:36:45 +00:00
|
|
|
int res = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2015-10-22 00:45:47 +01:00
|
|
|
res = Yap_Reset(mode);
|
2001-04-09 20:54:03 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
2013-03-10 16:37:27 +00:00
|
|
|
return res;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_Exit(int retval) { Yap_exit(retval); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_InitSocks(const char *host, long port) { return 0; }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_SetOutputMessage(void) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#if DEBUG
|
2011-06-20 14:49:24 +01:00
|
|
|
Yap_output_msg = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_StreamToFileNo(Term t) { return (Yap_StreamToFileNo(t)); }
|
2001-05-21 21:00:05 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_CloseAllOpenStreams(void) {
|
2001-05-21 21:00:05 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_CloseStreams(FALSE);
|
2001-05-21 21:00:05 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_FlushAllStreams(void) {
|
2008-07-11 18:02:10 +01:00
|
|
|
BACKUP_H();
|
|
|
|
|
2011-02-15 14:43:28 +00:00
|
|
|
// VSC?? Yap_FlushStreams();
|
2008-07-11 18:02:10 +01:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_Throw(Term t) {
|
2002-05-06 16:33:05 +01:00
|
|
|
BACKUP_MACHINE_REGS();
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_JumpToEnv(t);
|
2002-05-06 16:33:05 +01:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_AsyncThrow(Term t) {
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2010-12-04 19:05:13 +00:00
|
|
|
BACKUP_MACHINE_REGS();
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= AsyncIntMode;
|
2010-12-04 19:05:13 +00:00
|
|
|
Yap_JumpToEnv(t);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~AsyncIntMode;
|
2010-12-04 19:05:13 +00:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_Halt(int i) { Yap_exit(i); }
|
2002-05-06 16:33:05 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API CELL *YAP_TopOfLocalStack(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
return (ASP);
|
2002-05-06 16:33:05 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_Predicate(Atom a, UInt arity, Term m) {
|
2002-05-06 16:33:05 +01:00
|
|
|
if (arity == 0) {
|
2015-10-22 00:45:47 +01:00
|
|
|
return ((void *)RepPredProp(PredPropByAtom(a, m)));
|
2002-05-06 16:33:05 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Functor f = Yap_MkFunctor(a, arity);
|
2015-10-22 00:45:47 +01:00
|
|
|
return ((void *)RepPredProp(PredPropByFunc(f, m)));
|
2002-05-06 16:33:05 +01:00
|
|
|
}
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2002-05-06 16:33:05 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_PredicateInfo(void *p, Atom *a, UInt *arity, Term *m) {
|
2002-05-06 16:33:05 +01:00
|
|
|
PredEntry *pd = (PredEntry *)p;
|
|
|
|
if (pd->ArityOfPE) {
|
|
|
|
*arity = pd->ArityOfPE;
|
|
|
|
*a = NameOfFunctor(pd->FunctorOfPred);
|
|
|
|
} else {
|
|
|
|
*arity = 0;
|
|
|
|
*a = (Atom)(pd->FunctorOfPred);
|
|
|
|
}
|
2004-02-12 12:37:12 +00:00
|
|
|
if (pd->ModuleOfPred)
|
|
|
|
*m = pd->ModuleOfPred;
|
|
|
|
else
|
|
|
|
*m = TermProlog;
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2002-05-14 19:24:34 +01:00
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
X_API void YAP_UserCPredicate(const char *name, CPredicate def, arity_t arity) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred(name, arity, def, UserCPredFlag);
|
2002-09-09 18:40:12 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
X_API void YAP_UserBackCPredicate_(const char *name, CPredicate init,
|
|
|
|
CPredicate cont, arity_t arity,
|
|
|
|
arity_t extra) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_InitCPredBackCut(name, arity, extra, init, cont, NULL, UserCPredFlag);
|
2002-09-09 18:40:12 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_UserBackCutCPredicate(const char *name, CPredicate init,
|
|
|
|
CPredicate cont, CPredicate cut,
|
2016-03-29 01:55:12 +01:00
|
|
|
arity_t arity, arity_t extra) {
|
2005-11-18 18:52:41 +00:00
|
|
|
Yap_InitCPredBackCut(name, arity, extra, init, cont, cut, UserCPredFlag);
|
|
|
|
}
|
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
X_API void YAP_UserBackCPredicate(const char *name, CPredicate init,
|
|
|
|
CPredicate cont, arity_t arity,
|
|
|
|
arity_t extra) {
|
|
|
|
Yap_InitCPredBackCut(name, arity, extra, init, cont, NULL, UserCPredFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
X_API void YAP_UserCPredicateWithArgs(const char *a, CPredicate f,
|
|
|
|
arity_t arity, Term mod) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2002-05-16 21:33:00 +01:00
|
|
|
PredEntry *pe;
|
2004-02-12 12:37:12 +00:00
|
|
|
Term cm = CurrentModule;
|
2002-05-16 21:33:00 +01:00
|
|
|
CurrentModule = mod;
|
2015-10-22 00:45:47 +01:00
|
|
|
YAP_UserCPredicate(a, f, arity);
|
2002-05-16 21:33:00 +01:00
|
|
|
if (arity == 0) {
|
2015-10-22 00:45:47 +01:00
|
|
|
pe = RepPredProp(PredPropByAtom(Yap_LookupAtom(a), mod));
|
2002-05-16 21:33:00 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Functor f = Yap_MkFunctor(Yap_LookupAtom(a), arity);
|
2015-10-22 00:45:47 +01:00
|
|
|
pe = RepPredProp(PredPropByFunc(f, mod));
|
2002-05-16 21:33:00 +01:00
|
|
|
}
|
|
|
|
pe->PredFlags |= CArgsPredFlag;
|
|
|
|
CurrentModule = cm;
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2002-05-16 21:33:00 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_CurrentModule(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
return (CurrentModule);
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2002-09-09 18:40:12 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_SetCurrentModule(Term new) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-02-14 22:13:45 +00:00
|
|
|
Term omod = CurrentModule;
|
2013-11-13 10:38:20 +00:00
|
|
|
LOCAL_SourceModule = CurrentModule = new;
|
2011-02-14 22:13:45 +00:00
|
|
|
return omod;
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2011-02-14 22:13:45 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_CreateModule(Atom at) {
|
2007-04-19 00:01:16 +01:00
|
|
|
Term t;
|
2015-04-13 13:15:30 +01:00
|
|
|
WRITE_LOCK(RepAtom(at)->ARWLock);
|
2007-04-19 00:01:16 +01:00
|
|
|
t = Yap_Module(MkAtomTerm(at));
|
2015-04-13 13:15:30 +01:00
|
|
|
WRITE_UNLOCK(RepAtom(at)->ARWLock);
|
2007-04-19 00:01:16 +01:00
|
|
|
return t;
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2004-08-11 17:14:55 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_StripModule(Term t, Term *modp) {
|
2008-08-07 21:51:23 +01:00
|
|
|
return Yap_StripModule(t, modp);
|
2008-08-01 22:44:25 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_ThreadSelf(void) {
|
2010-06-08 10:40:55 +01:00
|
|
|
#if THREADS
|
2004-07-22 22:32:23 +01:00
|
|
|
return Yap_thread_self();
|
|
|
|
#else
|
2010-07-27 23:29:55 +01:00
|
|
|
return -2;
|
2004-07-22 22:32:23 +01:00
|
|
|
#endif
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2004-07-22 22:32:23 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_ThreadCreateEngine(struct YAP_thread_attr_struct *attr) {
|
2010-06-08 10:40:55 +01:00
|
|
|
#if THREADS
|
2004-07-22 22:32:23 +01:00
|
|
|
return Yap_thread_create_engine(attr);
|
|
|
|
#else
|
2010-07-23 15:54:13 +01:00
|
|
|
return -1;
|
2004-07-22 22:32:23 +01:00
|
|
|
#endif
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2004-07-22 22:32:23 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_ThreadAttachEngine(int wid) {
|
2010-06-08 10:40:55 +01:00
|
|
|
#if THREADS
|
2004-07-22 22:32:23 +01:00
|
|
|
return Yap_thread_attach_engine(wid);
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2012-12-11 22:07:10 +00:00
|
|
|
}
|
2004-07-22 22:32:23 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_ThreadDetachEngine(int wid) {
|
2010-06-08 10:40:55 +01:00
|
|
|
#if THREADS
|
2004-07-22 22:32:23 +01:00
|
|
|
return Yap_thread_detach_engine(wid);
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2004-07-22 22:32:23 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_ThreadDestroyEngine(int wid) {
|
2010-06-08 10:40:55 +01:00
|
|
|
#if THREADS
|
2004-07-22 22:32:23 +01:00
|
|
|
return Yap_thread_destroy_engine(wid);
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2004-07-22 22:32:23 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_TermNil(void) { return TermNil; }
|
2006-05-16 19:37:31 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_IsTermNil(Term t) { return t == TermNil; }
|
2011-10-24 22:46:16 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_AtomGetHold(Atom at) { return Yap_AtomIncreaseHold(at); }
|
2007-12-05 12:17:25 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_AtomReleaseHold(Atom at) { return Yap_AtomDecreaseHold(at); }
|
2007-12-05 12:17:25 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Agc_hook YAP_AGCRegisterHook(Agc_hook hook) {
|
2011-05-10 10:06:51 +01:00
|
|
|
Agc_hook old = GLOBAL_AGCHook;
|
|
|
|
GLOBAL_AGCHook = hook;
|
2008-02-12 17:03:59 +00:00
|
|
|
return old;
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2007-12-05 12:17:25 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_HaltRegisterHook(HaltHookFunc hook, void *closure) {
|
2010-09-24 14:00:53 +01:00
|
|
|
return Yap_HaltRegisterHook(hook, closure);
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2010-09-24 14:00:53 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API char *YAP_cwd(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2014-06-11 19:35:45 +01:00
|
|
|
char *buf = NULL;
|
2008-12-13 16:07:55 +00:00
|
|
|
int len;
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!Yap_getcwd(LOCAL_FileNameBuf, YAP_FILENAME_MAX))
|
2008-12-13 16:07:55 +00:00
|
|
|
return FALSE;
|
2011-05-23 16:19:47 +01:00
|
|
|
len = strlen(LOCAL_FileNameBuf);
|
2015-10-22 00:45:47 +01:00
|
|
|
buf = Yap_AllocCodeSpace(len + 1);
|
2008-12-13 16:07:55 +00:00
|
|
|
if (!buf)
|
|
|
|
return NULL;
|
2011-05-23 16:19:47 +01:00
|
|
|
strncpy(buf, LOCAL_FileNameBuf, len);
|
2008-12-13 16:07:55 +00:00
|
|
|
return buf;
|
|
|
|
}
|
2008-12-22 13:49:44 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_FloatsToList(double *dblp, size_t sz) {
|
2011-12-30 16:04:16 +00:00
|
|
|
CACHE_REGS
|
|
|
|
Term t;
|
|
|
|
CELL *oldH;
|
|
|
|
BACKUP_H();
|
|
|
|
|
|
|
|
if (!sz)
|
|
|
|
return TermNil;
|
2015-10-22 00:45:47 +01:00
|
|
|
while (ASP - 1024 < HR + sz * (2 + 2 + SIZEOF_DOUBLE / SIZEOF_INT_P)) {
|
2014-01-19 21:15:05 +00:00
|
|
|
if ((CELL *)dblp > H0 && (CELL *)dblp < HR) {
|
2011-12-30 16:04:16 +00:00
|
|
|
/* we are in trouble */
|
2015-10-22 00:45:47 +01:00
|
|
|
LOCAL_OpenArray = (CELL *)dblp;
|
2011-12-30 16:04:16 +00:00
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!Yap_dogc(0, NULL PASS_REGS)) {
|
2011-12-30 16:04:16 +00:00
|
|
|
RECOVER_H();
|
|
|
|
return 0L;
|
|
|
|
}
|
2012-01-09 23:29:31 +00:00
|
|
|
dblp = (double *)LOCAL_OpenArray;
|
2011-12-30 16:04:16 +00:00
|
|
|
LOCAL_OpenArray = NULL;
|
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
t = AbsPair(HR);
|
2011-12-30 16:04:16 +00:00
|
|
|
while (sz) {
|
2014-01-19 21:15:05 +00:00
|
|
|
oldH = HR;
|
2015-10-22 00:45:47 +01:00
|
|
|
HR += 2;
|
2011-12-30 16:04:16 +00:00
|
|
|
oldH[0] = MkFloatTerm(*dblp++);
|
2014-01-19 21:15:05 +00:00
|
|
|
oldH[1] = AbsPair(HR);
|
2011-12-30 16:04:16 +00:00
|
|
|
sz--;
|
|
|
|
}
|
|
|
|
oldH[1] = TermNil;
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_ListToFloats(Term t, double *dblp, size_t sz) {
|
2012-03-14 11:08:28 +00:00
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
t = Deref(t);
|
|
|
|
do {
|
|
|
|
Term hd;
|
|
|
|
if (IsVarTerm(t))
|
2015-10-22 00:45:47 +01:00
|
|
|
return -1;
|
2012-03-14 11:08:28 +00:00
|
|
|
if (t == TermNil)
|
|
|
|
return i;
|
|
|
|
if (!IsPairTerm(t))
|
|
|
|
return -1;
|
|
|
|
hd = HeadOfTerm(t);
|
2012-03-22 21:38:22 +00:00
|
|
|
if (IsFloatTerm(hd)) {
|
|
|
|
dblp[i++] = FloatOfTerm(hd);
|
|
|
|
} else {
|
|
|
|
extern double Yap_gmp_to_float(Term hd);
|
|
|
|
|
|
|
|
if (IsIntTerm(hd))
|
2015-10-22 00:45:47 +01:00
|
|
|
dblp[i++] = IntOfTerm(hd);
|
2012-03-22 21:38:22 +00:00
|
|
|
else if (IsLongIntTerm(hd))
|
2015-10-22 00:45:47 +01:00
|
|
|
dblp[i++] = LongIntOfTerm(hd);
|
2012-04-05 20:50:27 +01:00
|
|
|
#if USE_GMP
|
2012-03-22 21:38:22 +00:00
|
|
|
else if (IsBigIntTerm(hd))
|
2015-10-22 00:45:47 +01:00
|
|
|
dblp[i++] = Yap_gmp_to_float(hd);
|
2012-04-05 20:50:27 +01:00
|
|
|
#endif
|
2012-03-22 21:38:22 +00:00
|
|
|
else
|
2015-10-22 00:45:47 +01:00
|
|
|
return -1;
|
2012-03-22 21:38:22 +00:00
|
|
|
}
|
2012-03-14 11:08:28 +00:00
|
|
|
if (i == sz)
|
|
|
|
return sz;
|
|
|
|
t = TailOfTerm(t);
|
|
|
|
} while (TRUE);
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_IntsToList(Int *dblp, size_t sz) {
|
2012-03-14 11:08:28 +00:00
|
|
|
CACHE_REGS
|
|
|
|
Term t;
|
|
|
|
CELL *oldH;
|
|
|
|
BACKUP_H();
|
|
|
|
|
|
|
|
if (!sz)
|
|
|
|
return TermNil;
|
2015-10-22 00:45:47 +01:00
|
|
|
while (ASP - 1024 < HR + sz * 3) {
|
2014-01-19 21:15:05 +00:00
|
|
|
if ((CELL *)dblp > H0 && (CELL *)dblp < HR) {
|
2012-03-14 11:08:28 +00:00
|
|
|
/* we are in trouble */
|
2015-10-22 00:45:47 +01:00
|
|
|
LOCAL_OpenArray = (CELL *)dblp;
|
2012-03-14 11:08:28 +00:00
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!Yap_dogc(0, NULL PASS_REGS)) {
|
2012-03-14 11:08:28 +00:00
|
|
|
RECOVER_H();
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
dblp = (Int *)LOCAL_OpenArray;
|
|
|
|
LOCAL_OpenArray = NULL;
|
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
t = AbsPair(HR);
|
2012-03-14 11:08:28 +00:00
|
|
|
while (sz) {
|
2014-01-19 21:15:05 +00:00
|
|
|
oldH = HR;
|
2015-10-22 00:45:47 +01:00
|
|
|
HR += 2;
|
2012-03-14 11:08:28 +00:00
|
|
|
oldH[0] = MkIntegerTerm(*dblp++);
|
2014-01-19 21:15:05 +00:00
|
|
|
oldH[1] = AbsPair(HR);
|
2012-03-14 11:08:28 +00:00
|
|
|
sz--;
|
|
|
|
}
|
|
|
|
oldH[1] = TermNil;
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_ListToInts(Term t, Int *dblp, size_t sz) {
|
2012-03-14 11:08:28 +00:00
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
t = Deref(t);
|
|
|
|
do {
|
|
|
|
Term hd;
|
|
|
|
if (IsVarTerm(t))
|
2015-10-22 00:45:47 +01:00
|
|
|
return -1;
|
2012-03-14 11:08:28 +00:00
|
|
|
if (t == TermNil)
|
|
|
|
return i;
|
|
|
|
if (!IsPairTerm(t))
|
|
|
|
return -1;
|
|
|
|
hd = HeadOfTerm(t);
|
|
|
|
if (!IsIntTerm(hd))
|
|
|
|
return -1;
|
|
|
|
dblp[i++] = IntOfTerm(hd);
|
|
|
|
if (i == sz)
|
|
|
|
return sz;
|
|
|
|
t = TailOfTerm(t);
|
|
|
|
} while (TRUE);
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_OpenList(int n) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2008-12-22 13:49:44 +00:00
|
|
|
Term t;
|
|
|
|
BACKUP_H();
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
while (HR + 2 * n > ASP - 1024) {
|
|
|
|
if (!Yap_dogc(0, NULL PASS_REGS)) {
|
2009-06-02 01:28:30 +01:00
|
|
|
RECOVER_H();
|
2008-12-22 13:49:44 +00:00
|
|
|
return FALSE;
|
2009-06-02 01:28:30 +01:00
|
|
|
}
|
2008-12-22 13:49:44 +00:00
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
t = AbsPair(HR);
|
2015-10-22 00:45:47 +01:00
|
|
|
HR += 2 * n;
|
2008-12-22 13:49:44 +00:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_ExtendList(Term t0, Term inp) {
|
2008-12-22 13:49:44 +00:00
|
|
|
Term t;
|
|
|
|
CELL *ptr = RepPair(t0);
|
2009-06-15 16:29:31 +01:00
|
|
|
BACKUP_H();
|
2008-12-22 13:49:44 +00:00
|
|
|
|
|
|
|
ptr[0] = inp;
|
2015-10-22 00:45:47 +01:00
|
|
|
ptr[1] = AbsPair(ptr + 2);
|
|
|
|
t = AbsPair(ptr + 2);
|
2008-12-22 13:49:44 +00:00
|
|
|
|
|
|
|
RECOVER_H();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_CloseList(Term t0, Term tail) {
|
2008-12-22 13:49:44 +00:00
|
|
|
CELL *ptr = RepPair(t0);
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
RESET_VARIABLE(ptr - 1);
|
|
|
|
if (!Yap_unify((Term)(ptr - 1), tail))
|
2008-12-22 13:49:44 +00:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-02-09 22:45:50 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_IsAttVar(Term t) {
|
2013-03-26 21:10:03 +00:00
|
|
|
CACHE_REGS
|
2009-02-09 22:45:50 +00:00
|
|
|
t = Deref(t);
|
|
|
|
if (!IsVarTerm(t))
|
|
|
|
return FALSE;
|
2010-03-08 09:19:35 +00:00
|
|
|
return IsAttVar(VarOfTerm(t));
|
2009-02-09 22:45:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_AttsOfVar(Term t) {
|
2013-03-26 21:10:03 +00:00
|
|
|
CACHE_REGS
|
2009-02-09 22:45:50 +00:00
|
|
|
attvar_record *attv;
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2009-02-09 22:45:50 +00:00
|
|
|
t = Deref(t);
|
|
|
|
if (!IsVarTerm(t))
|
|
|
|
return TermNil;
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!IsAttVar(VarOfTerm(t)))
|
2009-02-09 22:45:50 +00:00
|
|
|
return TermNil;
|
2012-05-21 17:24:40 +01:00
|
|
|
attv = RepAttVar(VarOfTerm(t));
|
2009-02-09 22:45:50 +00:00
|
|
|
return attv->Atts;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_FileNoFromStream(Term t) {
|
2009-04-23 03:42:15 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
t = Deref(t);
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return -1;
|
|
|
|
return Yap_StreamToFileNo(t);
|
2009-04-23 03:42:15 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_FileDescriptorFromStream(Term t) {
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2009-04-22 22:41:41 +01:00
|
|
|
t = Deref(t);
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return NULL;
|
|
|
|
return Yap_FileDescriptorFromStream(t);
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void *YAP_Record(Term t) {
|
2011-01-03 03:16:29 +00:00
|
|
|
DBTerm *dbterm;
|
|
|
|
DBRecordList *dbt;
|
|
|
|
|
|
|
|
dbterm = Yap_StoreTermInDB(Deref(t), 0);
|
|
|
|
if (dbterm == NULL)
|
|
|
|
return NULL;
|
|
|
|
dbt = (struct record_list *)Yap_AllocCodeSpace(sizeof(struct record_list));
|
|
|
|
while (dbt == NULL) {
|
|
|
|
if (!Yap_growheap(FALSE, sizeof(struct record_list), NULL)) {
|
|
|
|
/* be a good neighbor */
|
|
|
|
Yap_FreeCodeSpace((void *)dbterm);
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "using YAP_Record");
|
2011-01-03 03:16:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Yap_Records) {
|
|
|
|
Yap_Records->prev_rec = dbt;
|
|
|
|
}
|
|
|
|
dbt->next_rec = Yap_Records;
|
|
|
|
dbt->prev_rec = NULL;
|
|
|
|
dbt->dbrecord = dbterm;
|
|
|
|
Yap_Records = dbt;
|
|
|
|
return dbt;
|
2010-08-02 19:48:17 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_Recorded(void *handle) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2011-01-03 03:16:29 +00:00
|
|
|
Term t;
|
|
|
|
DBTerm *dbterm = ((DBRecordList *)handle)->dbrecord;
|
|
|
|
|
|
|
|
BACKUP_MACHINE_REGS();
|
|
|
|
do {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2011-01-03 03:16:29 +00:00
|
|
|
t = Yap_FetchTermFromDB(dbterm);
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_Error_TYPE == YAP_NO_ERROR) {
|
2011-01-03 03:16:29 +00:00
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return t;
|
2015-09-25 10:57:26 +01:00
|
|
|
} else if (LOCAL_Error_TYPE == RESOURCE_ERROR_ATTRIBUTED_VARIABLES) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2011-01-03 03:16:29 +00:00
|
|
|
if (!Yap_growglobal(NULL)) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_ATTRIBUTED_VARIABLES, TermNil,
|
|
|
|
LOCAL_ErrorMessage);
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return FALSE;
|
2011-01-03 03:16:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!Yap_growstack(dbterm->NOfCells * CellSize)) {
|
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return FALSE;
|
2011-01-03 03:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (t == (CELL)0);
|
|
|
|
RECOVER_MACHINE_REGS();
|
|
|
|
return t;
|
2010-08-02 19:48:17 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_Erase(void *handle) {
|
2011-01-03 03:16:29 +00:00
|
|
|
DBRecordList *dbr = (DBRecordList *)handle;
|
2015-04-13 13:15:30 +01:00
|
|
|
if (dbr->next_rec)
|
2011-01-03 03:16:29 +00:00
|
|
|
dbr->next_rec->prev_rec = dbr->prev_rec;
|
2015-04-13 13:15:30 +01:00
|
|
|
if (dbr->prev_rec)
|
2011-03-11 23:21:23 +00:00
|
|
|
dbr->prev_rec->next_rec = dbr->next_rec;
|
2011-01-03 03:16:29 +00:00
|
|
|
else if (Yap_Records == dbr) {
|
|
|
|
Yap_Records = dbr->next_rec;
|
|
|
|
}
|
2011-03-11 23:21:23 +00:00
|
|
|
Yap_ReleaseTermFromDB(dbr->dbrecord);
|
2011-01-03 03:16:29 +00:00
|
|
|
Yap_FreeCodeSpace(handle);
|
2010-08-02 19:48:17 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API yhandle_t YAP_ArgsToSlots(int n) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
return Yap_NewSlots(n);
|
2010-08-04 17:36:20 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_SlotsToArgs(int n, yhandle_t slot) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-10-22 00:45:47 +01:00
|
|
|
CELL *ptr0 = Yap_AddressFromSlot(slot), *ptr1 = &ARG1;
|
2010-08-04 17:36:20 +01:00
|
|
|
while (n--) {
|
|
|
|
*ptr1++ = *ptr0++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API void YAP_signal(int sig) { Yap_signal(sig); }
|
2010-08-31 04:25:56 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_SetYAPFlag(Term flag, Term val) { return setYapFlag(flag, val); }
|
2010-08-31 04:25:56 +01:00
|
|
|
|
2014-06-16 14:41:54 +01:00
|
|
|
/* yhandle_t YAP_VarSlotToNumber(yhandle_t) */
|
|
|
|
yhandle_t YAP_VarSlotToNumber(yhandle_t s) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-04-13 13:15:30 +01:00
|
|
|
Term *t = (CELL *)Deref(Yap_GetFromSlot(s));
|
2014-01-19 21:15:05 +00:00
|
|
|
if (t < HR)
|
2015-10-22 00:45:47 +01:00
|
|
|
return t - H0;
|
|
|
|
return t - LCL0;
|
2011-02-10 00:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Term YAP_ModuleUser() */
|
2015-10-22 00:45:47 +01:00
|
|
|
Term YAP_ModuleUser(void) { return MkAtomTerm(AtomUser); }
|
2011-02-10 00:01:19 +00:00
|
|
|
|
|
|
|
/* int YAP_PredicateHasClauses() */
|
2014-06-16 14:41:54 +01:00
|
|
|
yhandle_t YAP_NumberOfClausesForPredicate(PredEntry *pe) {
|
2011-02-10 00:01:19 +00:00
|
|
|
return pe->cs.p_code.NOfClauses;
|
|
|
|
}
|
2010-08-02 19:48:17 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
int YAP_MaxOpPriority(Atom at, Term module) {
|
|
|
|
AtomEntry *ae = RepAtom(at);
|
|
|
|
OpEntry *info;
|
2011-02-10 21:14:38 +00:00
|
|
|
WRITE_LOCK(ae->ARWLock);
|
|
|
|
info = Yap_GetOpPropForAModuleHavingALock(ae, module);
|
|
|
|
if (!info) {
|
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int ret = info->Prefix;
|
|
|
|
if (info->Infix > ret)
|
|
|
|
ret = info->Infix;
|
|
|
|
if (info->Posfix > ret)
|
|
|
|
ret = info->Posfix;
|
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
int YAP_OpInfo(Atom at, Term module, int opkind, int *yap_type, int *prio) {
|
|
|
|
AtomEntry *ae = RepAtom(at);
|
|
|
|
OpEntry *info;
|
2011-02-10 21:14:38 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
WRITE_LOCK(ae->ARWLock);
|
|
|
|
info = Yap_GetOpPropForAModuleHavingALock(ae, module);
|
|
|
|
if (!info) {
|
2011-02-15 15:50:55 +00:00
|
|
|
/* try system operators */
|
|
|
|
info = Yap_GetOpPropForAModuleHavingALock(ae, PROLOG_MODULE);
|
|
|
|
if (!info) {
|
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-10 21:14:38 +00:00
|
|
|
}
|
|
|
|
if (opkind == PREFIX_OP) {
|
|
|
|
SMALLUNSGN p = info->Prefix;
|
|
|
|
if (!p) {
|
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (p & DcrrpFlag) {
|
|
|
|
n = 6;
|
|
|
|
*prio = (p ^ DcrrpFlag);
|
|
|
|
} else {
|
|
|
|
n = 7;
|
|
|
|
*prio = p;
|
|
|
|
}
|
|
|
|
} else if (opkind == INFIX_OP) {
|
|
|
|
SMALLUNSGN p = info->Infix;
|
|
|
|
if (!p) {
|
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((p & DcrrpFlag) && (p & DcrlpFlag)) {
|
|
|
|
n = 1;
|
|
|
|
*prio = (p ^ (DcrrpFlag | DcrlpFlag));
|
|
|
|
} else if (p & DcrrpFlag) {
|
|
|
|
n = 3;
|
|
|
|
*prio = (p ^ DcrrpFlag);
|
|
|
|
} else if (p & DcrlpFlag) {
|
|
|
|
n = 2;
|
|
|
|
*prio = (p ^ DcrlpFlag);
|
|
|
|
} else {
|
|
|
|
n = 4;
|
|
|
|
*prio = p;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SMALLUNSGN p = info->Posfix;
|
|
|
|
if (p & DcrlpFlag) {
|
|
|
|
n = 4;
|
|
|
|
*prio = (p ^ DcrlpFlag);
|
|
|
|
} else {
|
|
|
|
n = 5;
|
|
|
|
*prio = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*yap_type = n;
|
2011-03-01 10:32:25 +00:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
2011-02-10 21:14:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
int YAP_Argv(char ***argvp) {
|
2011-10-13 16:46:39 +01:00
|
|
|
if (argvp) {
|
|
|
|
*argvp = GLOBAL_argv;
|
|
|
|
}
|
|
|
|
return GLOBAL_argc;
|
|
|
|
}
|
2011-10-27 11:35:40 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
YAP_tag_t YAP_TagOfTerm(Term t) {
|
2011-10-27 11:35:40 +01:00
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
CELL *pt = VarOfTerm(t);
|
|
|
|
if (IsUnboundVar(pt)) {
|
2013-03-26 21:10:03 +00:00
|
|
|
CACHE_REGS
|
2011-10-27 11:35:40 +01:00
|
|
|
if (IsAttVar(pt))
|
2015-10-22 00:45:47 +01:00
|
|
|
return YAP_TAG_ATT;
|
2011-10-27 11:35:40 +01:00
|
|
|
return YAP_TAG_UNBOUND;
|
|
|
|
}
|
|
|
|
return YAP_TAG_REF;
|
|
|
|
}
|
|
|
|
if (IsPairTerm(t))
|
|
|
|
return YAP_TAG_PAIR;
|
|
|
|
if (IsAtomOrIntTerm(t)) {
|
|
|
|
if (IsAtomTerm(t))
|
|
|
|
return YAP_TAG_ATOM;
|
|
|
|
return YAP_TAG_INT;
|
|
|
|
} else {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2011-10-27 11:35:40 +01:00
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
if (f == FunctorDBRef) {
|
2015-10-22 00:45:47 +01:00
|
|
|
return YAP_TAG_DBREF;
|
2011-10-27 11:35:40 +01:00
|
|
|
}
|
|
|
|
if (f == FunctorLongInt) {
|
2015-10-22 00:45:47 +01:00
|
|
|
return YAP_TAG_LONG_INT;
|
2011-10-27 11:35:40 +01:00
|
|
|
}
|
|
|
|
if (f == FunctorBigInt) {
|
2015-10-22 00:45:47 +01:00
|
|
|
big_blob_type bt = RepAppl(t)[1];
|
|
|
|
switch (bt) {
|
|
|
|
case BIG_INT:
|
|
|
|
return YAP_TAG_BIG_INT;
|
|
|
|
case BIG_RATIONAL:
|
|
|
|
return YAP_TAG_RATIONAL;
|
|
|
|
default:
|
|
|
|
return YAP_TAG_OPAQUE;
|
|
|
|
}
|
2011-10-27 11:35:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return YAP_TAG_APPL;
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 22:52:52 +00:00
|
|
|
|
|
|
|
int YAP_BPROLOG_exception;
|
|
|
|
Term YAP_BPROLOG_curr_toam_status;
|
|
|
|
|
2015-11-09 18:22:43 +00:00
|
|
|
/**
|
2015-11-05 16:59:30 +00:00
|
|
|
* Output the number of bytes needed to represent a string in UTF-8
|
|
|
|
* Note that the terminating zero is not included. No error checking
|
|
|
|
* is performed (the programmer should have that done).
|
2015-11-09 18:22:43 +00:00
|
|
|
*
|
2015-11-05 16:59:30 +00:00
|
|
|
* @param t a list of codes, chars, string or atom.
|
2015-11-09 18:22:43 +00:00
|
|
|
*
|
2015-11-05 16:59:30 +00:00
|
|
|
* @return a positive number with the size, or 0.
|
|
|
|
*/
|
|
|
|
size_t YAP_UTF8_TextLength(Term t) {
|
|
|
|
utf8proc_uint8_t dst[8];
|
|
|
|
size_t sz = 0;
|
|
|
|
|
2016-01-31 10:13:14 +00:00
|
|
|
if (IsPairTerm(t)) {
|
2015-11-05 16:59:30 +00:00
|
|
|
while (t != TermNil) {
|
|
|
|
int c;
|
|
|
|
|
2016-01-31 10:13:14 +00:00
|
|
|
Term hd = HeadOfTerm(t);
|
2015-11-05 16:59:30 +00:00
|
|
|
if (IsAtomTerm(hd)) {
|
2016-01-31 10:13:14 +00:00
|
|
|
Atom at = AtomOfTerm(hd);
|
|
|
|
if (IsWideAtom(at))
|
|
|
|
c = RepAtom(at)->WStrOfAE[0];
|
|
|
|
else
|
|
|
|
c = RepAtom(at)->StrOfAE[0];
|
2015-11-05 16:59:30 +00:00
|
|
|
} else if (IsIntegerTerm(hd)) {
|
2016-01-31 10:13:14 +00:00
|
|
|
c = IntegerOfTerm(hd);
|
2015-11-05 16:59:30 +00:00
|
|
|
} else {
|
2016-01-31 10:13:14 +00:00
|
|
|
c = '\0';
|
2015-11-05 16:59:30 +00:00
|
|
|
}
|
|
|
|
sz += utf8proc_encode_char(c, dst);
|
|
|
|
t = TailOfTerm(t);
|
|
|
|
}
|
2016-01-31 10:13:14 +00:00
|
|
|
} else if (IsAtomTerm(t)) {
|
|
|
|
Atom at = AtomOfTerm(t);
|
|
|
|
if (IsWideAtom(at)) {
|
|
|
|
const wchar_t *s = RepAtom(at)->WStrOfAE;
|
|
|
|
int c;
|
|
|
|
while ((c = *s++)) {
|
|
|
|
sz += utf8proc_encode_char(c, dst);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const unsigned char *s = (const unsigned char *)RepAtom(at)->StrOfAE;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = *s++)) {
|
|
|
|
sz += utf8proc_encode_char(c, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (IsStringTerm(t)) {
|
|
|
|
sz = strlen(StringOfTerm(t));
|
2015-11-05 16:59:30 +00:00
|
|
|
}
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Int YAP_ListLength(Term t) {
|
2012-03-14 11:08:28 +00:00
|
|
|
Term *aux;
|
|
|
|
|
|
|
|
Int n = Yap_SkipList(&t, &aux);
|
|
|
|
if (IsVarTerm(*aux))
|
|
|
|
return -1;
|
|
|
|
if (*aux == TermNil)
|
|
|
|
return n;
|
|
|
|
return -1;
|
2011-11-02 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Int YAP_NumberVars(Term t, Int nbv) { return Yap_NumberVars(t, nbv, FALSE); }
|
2011-11-02 22:52:52 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
Term YAP_UnNumberVars(Term t) {
|
2011-11-04 09:28:33 +00:00
|
|
|
/* don't allow sharing of ground terms */
|
|
|
|
return Yap_UnNumberTerm(t, FALSE);
|
2011-11-02 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
int YAP_IsNumberedVariable(Term t) {
|
2015-12-15 08:48:53 +00:00
|
|
|
return IsApplTerm(t) && FunctorOfTerm(t) == FunctorDollarVar &&
|
2015-10-22 00:45:47 +01:00
|
|
|
IsIntegerTerm(ArgOfTerm(1, t));
|
2011-11-02 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API size_t YAP_ExportTerm(Term inp, char *buf, size_t len) {
|
2012-02-02 09:58:05 +00:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2012-02-03 16:31:49 +00:00
|
|
|
return Yap_ExportTerm(inp, buf, len, current_arity());
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API size_t YAP_SizeOfExportedTerm(char *buf) {
|
2012-02-03 16:31:49 +00:00
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
return Yap_SizeOfExportedTerm(buf);
|
2012-02-01 19:26:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Term YAP_ImportTerm(char *buf) { return Yap_ImportTerm(buf); }
|
2012-02-01 19:26:28 +00:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API int YAP_RequiresExtraStack(size_t sz) {
|
2012-04-11 22:08:02 +01:00
|
|
|
CACHE_REGS
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
if (sz < 16 * 1024)
|
|
|
|
sz = 16 * 1024;
|
|
|
|
if (HR <= ASP - sz) {
|
2012-03-30 09:49:36 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
BACKUP_H();
|
2015-10-22 00:45:47 +01:00
|
|
|
while (HR > ASP - sz) {
|
2012-03-30 09:49:36 +01:00
|
|
|
CACHE_REGS
|
|
|
|
RECOVER_H();
|
2015-10-22 00:45:47 +01:00
|
|
|
if (!Yap_dogc(0, NULL PASS_REGS)) {
|
2012-03-30 09:49:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
BACKUP_H();
|
|
|
|
}
|
|
|
|
RECOVER_H();
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-10-04 13:22:00 +01:00
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
atom_t *TR_Atoms;
|
|
|
|
functor_t *TR_Functors;
|
|
|
|
size_t AtomTranslations, MaxAtomTranslations;
|
|
|
|
size_t FunctorTranslations, MaxFunctorTranslations;
|
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_AtomToInt(Atom At) {
|
|
|
|
TranslationEntry *te = Yap_GetTranslationProp(At, 0);
|
|
|
|
if (te != NIL)
|
|
|
|
return te->Translation;
|
2016-02-11 14:00:56 +00:00
|
|
|
TR_Atoms[AtomTranslations] = At;
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_PutAtomTranslation(At, 0, AtomTranslations);
|
2013-10-04 13:22:00 +01:00
|
|
|
AtomTranslations++;
|
2013-10-08 13:04:30 +01:00
|
|
|
if (AtomTranslations == MaxAtomTranslations) {
|
2016-02-11 14:00:56 +00:00
|
|
|
atom_t *ot = TR_Atoms;
|
|
|
|
atom_t *nt = (atom_t *)malloc(sizeof(atom_t) * 2 * MaxAtomTranslations);
|
2013-10-08 13:04:30 +01:00
|
|
|
if (nt == NULL) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, MkAtomTerm(At),
|
|
|
|
"No more room for translations");
|
2013-10-08 13:04:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-02-11 14:00:56 +00:00
|
|
|
memcpy(nt, ot, sizeof(atom_t) * MaxAtomTranslations);
|
|
|
|
TR_Atoms = nt;
|
2015-10-22 00:45:47 +01:00
|
|
|
free(ot);
|
2013-10-08 13:04:30 +01:00
|
|
|
MaxAtomTranslations *= 2;
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
return AtomTranslations - 1;
|
2013-10-04 13:22:00 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 14:00:56 +00:00
|
|
|
X_API Atom YAP_IntToAtom(Int i) { return TR_Atoms[i]; }
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2015-10-22 00:45:47 +01:00
|
|
|
X_API Int YAP_FunctorToInt(Functor f) {
|
|
|
|
Atom At = NameOfFunctor(f);
|
|
|
|
arity_t arity = ArityOfFunctor(f);
|
2015-08-07 22:57:53 +01:00
|
|
|
TranslationEntry *te = Yap_GetTranslationProp(At, arity);
|
2015-10-22 00:45:47 +01:00
|
|
|
if (te != NIL)
|
|
|
|
return te->Translation;
|
2016-02-11 14:00:56 +00:00
|
|
|
TR_Functors[FunctorTranslations] = f;
|
2015-08-07 22:57:53 +01:00
|
|
|
Yap_PutAtomTranslation(At, arity, FunctorTranslations);
|
|
|
|
FunctorTranslations++;
|
|
|
|
if (FunctorTranslations == MaxFunctorTranslations) {
|
2016-03-29 01:55:12 +01:00
|
|
|
functor_t *nt = (functor_t *)malloc(sizeof(functor_t) * 2 *
|
|
|
|
MaxFunctorTranslations),
|
|
|
|
*ot = TR_Functors;
|
2015-08-07 22:57:53 +01:00
|
|
|
if (nt == NULL) {
|
2015-10-22 00:45:47 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, MkAtomTerm(At),
|
|
|
|
"No more room for translations");
|
2015-08-07 22:57:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2016-02-11 14:00:56 +00:00
|
|
|
memcpy(nt, ot, sizeof(functor_t) * MaxFunctorTranslations);
|
|
|
|
TR_Functors = nt;
|
2015-10-22 00:45:47 +01:00
|
|
|
free(ot);
|
2015-08-07 22:57:53 +01:00
|
|
|
MaxFunctorTranslations *= 2;
|
|
|
|
}
|
2015-10-22 00:45:47 +01:00
|
|
|
return FunctorTranslations - 1;
|
2015-08-07 22:57:53 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 14:00:56 +00:00
|
|
|
X_API Functor YAP_IntToFunctor(Int i) { return TR_Functors[i]; }
|
2015-08-07 22:57:53 +01:00
|
|
|
|
2016-07-31 05:10:39 +01:00
|
|
|
void yap_init(void) {}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
#endif // C_INTERFACE_C
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/**
|
|
|
|
@}
|
|
|
|
*/
|