22#ifndef TERMEXT_H_INCLUDED
23#define TERMEXT_H_INCLUDED
27#ifdef USE_SYSTEM_MALLOC
28#define SF_STORE (&(Yap_heap_regs->funcs))
30#define SF_STORE ((special_functors *)HEAP_INIT_BASE)
35extern Atom AtomFoundVar, AtomFreeTerm, AtomNil, AtomDot;
36#elif defined(USE_OFFSETS)
37#define AtomFoundVar ((Atom)(&(((special_functors *)(NULL))->AtFoundVar)))
38#define AtomFreeTerm ((Atom)(&(((special_functors *)(NULL))->AtFreeTerm)))
39#define AtomNil ((Atom)(&(((special_functors *)(NULL))->AtNil)))
40#define AtomDot ((Atom)(&(((special_functors *)(NULL))->AtDot)))
41#elif OLD_STYLE_INITIAL_ATOMS
42#define AtomFoundVar AbsAtom((AtomEntry *)&(SF_STORE->AtFoundVar))
43#define AtomFreeTerm AbsAtom((AtomEntry *)&(SF_STORE->AtFreeTerm))
44#define AtomNil AbsAtom((AtomEntry *)&(SF_STORE->AtNil))
45#define AtomDot AbsAtom((AtomEntry *)&(SF_STORE->AtDot))
47#define AtomFoundVar AbsAtom(SF_STORE->AtFoundVar)
48#define AtomFreeTerm AbsAtom(SF_STORE->AtFreeTerm)
49#define AtomNil AbsAtom(SF_STORE->AtNil)
50#define AtomDot AbsAtom(SF_STORE->AtDot)
53#define TermFoundVar MkAtomTerm(AtomFoundVar)
54#define TermFreeTerm MkAtomTerm(AtomFreeTerm)
55#define TermNil MkAtomTerm(AtomNil)
56#define TermDot MkAtomTerm(AtomDot)
61 double_e = 3 *
sizeof(
Functor *),
62 long_int_e = 4 *
sizeof(
Functor *),
63 big_int_e = 5 *
sizeof(
Functor *),
64 string_e = 6 *
sizeof(
Functor *)
66#define end_e (8 * sizeof(Functor *))
68#define FunctorDBRef ((Functor)(db_ref_e))
69#define FunctorDouble ((Functor)(double_e))
70#define FunctorLongInt ((Functor)(long_int_e))
71#define FunctorBigInt ((Functor)(big_int_e))
72#define FunctorString ((Functor)(string_e))
73#define FunctorBlob ((Functor)(blob_e))
75#include "inline-only.h"
85 EXTERNAL_BLOB = 0x0A0,
86 GOAL_CUT_POINT = 0x0A1,
87 USER_BLOB_START = 0x0100,
88 USER_BLOB_END = 0x0200
91INLINE_ONLY blob_type BlobOfFunctor(
Functor f);
93INLINE_ONLY blob_type BlobOfFunctor(
Functor f) {
94 return (blob_type)((CELL)f);
102 void (*bind_op)(Term *, Term CACHE_TYPE);
104 int (*copy_term_op)(CELL *,
void*, CELL *CACHE_TYPE);
106 Term (*to_term_op)(CELL *);
107 int (*term_to_op)(Term, Term CACHE_TYPE);
109 void (*mark_op)(CELL *);
114 empty_ext = 0 *
sizeof(
ext_op),
115 attvars_ext = 1 *
sizeof(
ext_op)
122#define CloseExtension(x) MkAtomTerm((Atom)(x))
124#define GetStartOfExtension(x) ((CELL*)AtomOfTerm(*x))
126inline static bool IsEndExtension(CELL *x) {
128 if (!IsAtomTerm(c))
return false;
129 Atom a = AtomOfTerm(c);
131 if (ca < H0 || ca >= HR)
143typedef struct special_functors_struct {
161SizeOfOpaqueTerm(Term *next, CELL cnext);
163INLINE_ONLY Float CpFloatUnaligned(CELL *ptr);
165#define MkFloatTerm(fl) __MkFloatTerm((fl)PASS_REGS)
167INLINE_ONLY Term __MkFloatTerm(Float USES_REGS);
169INLINE_ONLY Float FloatOfTerm(Term t);
171#if SIZEOF_DOUBLE == SIZEOF_INT_P
173INLINE_ONLY Term __MkFloatTerm(Float dbl USES_REGS) {
174 return (Term)((HR[0] = (CELL)FunctorDouble, *(Float *)(HR + 1) = dbl,
175 HR[2] = CloseExtension(HR), HR += 3, AbsAppl(HR - 3)));
178INLINE_ONLY Float FloatOfTerm(Term t) {
179 return (Float)(*(Float *)(RepAppl(t) + 1));
182#define InitUnalignedFloat()
184INLINE_ONLY Float CpFloatUnaligned(CELL *ptr) {
185 return *((Float *)ptr);
190#if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P
192#define DOUBLE_ALIGNED(ADDR) ((CELL)(ADDR)&0x4)
194INLINE_ONLY
inline void AlignGlobalForDouble(USES_REGS1);
196INLINE_ONLY
inline void AlignGlobalForDouble(USES_REGS1) {
199 if (!DOUBLE_ALIGNED(HR)) {
206INLINE_ONLY Float CpFloatUnaligned(CELL *ptr) {
207 return *((Float *)(ptr + 1));
212INLINE_ONLY Float CpFloatUnaligned(CELL *ptr) {
224INLINE_ONLY Term __MkFloatTerm(Float dbl USES_REGS) {
225 return (Term)((AlignGlobalForDouble(PASS_REGS1), HR[0] = (CELL)FunctorDouble,
226 *(Float *)(HR + 1) = dbl, HR[3] = CloseExtension(HR), HR += 4,
230INLINE_ONLY Float FloatOfTerm(Term t) {
231 return (Float)((DOUBLE_ALIGNED(RepAppl(t)) ? *(Float *)(RepAppl(t) + 1)
232 : CpFloatUnaligned(RepAppl(t))));
249INLINE_ONLY
bool IsFloatTerm(Term);
251INLINE_ONLY
bool IsFloatTerm(Term t) {
252 return (
int)(IsApplTerm(t) && FunctorOfTerm(t) == FunctorDouble);
257#define MkLongIntTerm(i) __MkLongIntTerm((i)PASS_REGS)
259INLINE_ONLY Term __MkLongIntTerm(Int USES_REGS);
261INLINE_ONLY Term __MkLongIntTerm(Int i USES_REGS) {
262 HR[0] = (CELL)FunctorLongInt;
264 HR[2] = CloseExtension(HR);
266 return AbsAppl(HR - 3);
269INLINE_ONLY Int LongIntOfTerm(Term t);
271INLINE_ONLY Int LongIntOfTerm(Term t) {
272 return (Int)(RepAppl(t)[1]);
275INLINE_ONLY
bool IsLongIntTerm(Term);
277INLINE_ONLY
bool IsLongIntTerm(Term t) {
278 return IsApplTerm(t) &&
279 FunctorOfTerm(t) == FunctorLongInt;
290#define MkStringTerm(i) __MkStringTerm((i)PASS_REGS)
293__MkStringTerm(
const char *s USES_REGS);
295INLINE_ONLY Term __MkStringTerm(
const char *s USES_REGS) {
296 Term t = AbsAppl(HR);
297 size_t sz = s[0] ==
'\0' ? 1 : strlen((
const char *) s) + 1;
298 size_t request = (sz + CELLSIZE - 1) / CELLSIZE;
299 HR[0] = (CELL) FunctorString;
302 memcpy((HR + 2), s, sz);
303 HR[2 + request] = CloseExtension(HR);
308#define MkUStringTerm(i) __MkStringTerm((const char *)(i)PASS_REGS)
311INLINE_ONLY
const unsigned char *UStringOfTerm(Term t);
313INLINE_ONLY
const unsigned char *UStringOfTerm(Term t) {
314 return (
const unsigned char *)(RepAppl(t) + 2);
317INLINE_ONLY
const char *StringOfTerm(Term t);
319INLINE_ONLY
const char *StringOfTerm(Term t) {
320 return (
const char *)(RepAppl(t) + 2);
323INLINE_ONLY
bool IsStringTerm(Term);
325INLINE_ONLY
bool IsStringTerm(Term t) {
326 return IsApplTerm(t) &&
327 FunctorOfTerm(t) == FunctorString;
337#if !defined(__cplusplus)
343typedef UInt mp_limb_t;
346 Int _mp_size, _mp_alloc;
357INLINE_ONLY
bool IsBigIntTerm(Term t) {
358 return IsApplTerm(t) &&
359 FunctorOfTerm(t) == FunctorBigInt;
362INLINE_ONLY
bool IsBlobTerm(Term t) {
363 return IsApplTerm(t) &&
364 FunctorOfTerm(t) == FunctorBlob;
369Term Yap_MkBigIntTerm(
MP_INT *);
370MP_INT *Yap_BigIntOfTerm(Term);
372Term Yap_MkBigRatTerm(
MP_RAT *);
373MP_RAT *Yap_BigRatOfTerm(Term);
375INLINE_ONLY
void MPZ_SET(mpz_t,
MP_INT *);
377INLINE_ONLY
void MPZ_SET(mpz_t dest,
MP_INT *src) {
378 dest->_mp_size = src->_mp_size;
379 dest->_mp_alloc = src->_mp_alloc;
380 dest->_mp_d = src->_mp_d;
383INLINE_ONLY
bool IsLargeIntTerm(Term);
385INLINE_ONLY
bool IsLargeIntTerm(Term t) {
386 return IsApplTerm(t) &&
387 ((FunctorOfTerm(t) <= FunctorBigInt) &&
388 (FunctorOfTerm(t) >= FunctorLongInt));
391INLINE_ONLY UInt Yap_SizeOfExtensiont(Term);
399INLINE_ONLY UInt Yap_SizeOfBigInt(Term t) {
401 CELL *pt = RepAppl(t) + 1;
402 if (pt[0 ] == BIG_RATIONAL) {
404 (
sizeof(
MP_INT) + (((
MP_INT *)(pt+1))->_mp_alloc *
sizeof(mp_limb_t))) /
406 (
sizeof(
MP_INT) + ((((
MP_INT *)(pt+1))+1)->_mp_alloc *
sizeof(mp_limb_t))) /
411 (
sizeof(
MP_INT) + (((
MP_INT *)(pt+1))->_mp_alloc *
sizeof(mp_limb_t))) /
417INLINE_ONLY
int IsLargeIntTerm(Term);
419INLINE_ONLY
int IsLargeIntTerm(Term t) {
420 return (
int)(IsApplTerm(t) && FunctorOfTerm(t) == FunctorLongInt);
427INLINE_ONLY
bool IsLargeNumTerm(Term);
429INLINE_ONLY
bool IsLargeNumTerm(Term t) {
430 return IsApplTerm(t) &&
431 ((FunctorOfTerm(t) <= FunctorBigInt) &&
432 (FunctorOfTerm(t) >= FunctorDouble));
435INLINE_ONLY
bool IsExternalBlobTerm(Term, CELL);
437INLINE_ONLY
bool IsExternalBlobTerm(Term t, CELL tag) {
438 return IsApplTerm(t) &&
439 FunctorOfTerm(t) == FunctorBlob &&
440 RepAppl(t)[1] == tag;
443INLINE_ONLY
void *ExternalBlobFromTerm(Term);
445INLINE_ONLY
void *ExternalBlobFromTerm(Term t) {
449INLINE_ONLY
bool IsNumTerm(Term);
451INLINE_ONLY
bool IsNumTerm(Term t) {
452 return (IsIntTerm(t) || IsLargeNumTerm(t));
455INLINE_ONLY
bool IsAtomicTerm(Term);
457INLINE_ONLY
bool IsAtomicTerm(Term t) {
458 return IsAtomOrIntTerm(t) ||
463INLINE_ONLY
bool IsExtensionFunctor(
Functor);
465INLINE_ONLY
bool IsExtensionFunctor(
Functor f) {
469INLINE_ONLY
bool IsBlobFunctor(
Functor);
471INLINE_ONLY
bool IsBlobFunctor(
Functor f) {
472 return (f <= FunctorString &&
476INLINE_ONLY
bool IsPrimitiveTerm(Term);
478INLINE_ONLY
bool IsPrimitiveTerm(Term t) {
479 return (IsAtomOrIntTerm(t) ||
481 IsBlobFunctor(FunctorOfTerm(t))));
484INLINE_ONLY exts ExtFromCell(CELL *);
486INLINE_ONLY exts ExtFromCell(CELL *pt) {
return attvars_ext; }
488INLINE_ONLY Int Yap_BlobTag(Term t);
490INLINE_ONLY Int Yap_BlobTag(Term t) {
491 CELL *pt = RepAppl(t);
496INLINE_ONLY
void *Yap_BlobInfo(Term t);
498INLINE_ONLY
void *Yap_BlobInfo(Term t) {
499 CELL *pt = RepAppl(t);
506INLINE_ONLY
bool unify_extension(
Functor, CELL, CELL *, CELL);
508EXTERN
bool unify_extension(
Functor, CELL, CELL *, CELL);
510int Yap_gmp_tcmp_big_big(Term, Term);
512INLINE_ONLY
bool unify_extension(
Functor f, CELL d0, CELL *pt0, CELL d1)
514 switch (BlobOfFunctor(f)) {
518 return (pt0[1] == RepAppl(d1)[1]);
520 return (pt0[2] == RepAppl(d1)[2] && !memcmp(pt0+3, RepAppl(d1)+3, pt0[2]*
sizeof(CELL) ) );
522 return strcmp((
char *)(pt0 + 2), (
char *)(RepAppl(d1) + 2)) == 0;
525 return (Yap_gmp_tcmp_big_big(d0, d1) == 0);
530 CELL *pt1 = RepAppl(d1);
531 return (pt0[1] == pt1[1]
532#
if SIZEOF_DOUBLE == 2 * SIZEOF_INT_P
541static inline CELL Yap_IntP_key(CELL *pt) {
543 if (((
Functor)pt[-1] == FunctorBigInt)) {
544 MP_INT *b1 = Yap_BigIntOfTerm(AbsAppl(pt - 1));
546 CELL val = ((CELL *)(b1 + 1))[0];
547 return MkIntTerm(val & (MAX_ABS_INT - 1));
550 return MkIntTerm(pt[0] & (MAX_ABS_INT - 1));
553static inline CELL Yap_Int_key(Term t) {
return Yap_IntP_key(RepAppl(t) + 1); }
555static inline CELL Yap_DoubleP_key(CELL *pt) {
556#if SIZEOF_DOUBLE1 == 2 * SIZEOF_INT_P
557 CELL val = pt[0] ^ pt[1];
561 return MkIntTerm(val & (MAX_ABS_INT - 1));
564static inline CELL Yap_Double_key(Term t) {
565 return Yap_DoubleP_key(RepAppl(t) + 1);
568static inline CELL Yap_StringP_key(CELL *pt) {
571 for (i = 1; i < n; i++) {
574 return MkIntTerm(val & (MAX_ABS_INT - 1));
577static inline CELL Yap_String_key(Term t) {
578 return Yap_StringP_key(RepAppl(t) + 1);