YAP 7.1.0
Yatom.h
1/*************************************************************************
2 * *
3 * YAP Prolog %W% %G%
4 * *
5 * Yap Prolog was developed at NCCUP - Universidade do Porto *
6 * *
7 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8 * *
9 **************************************************************************
10 * *
11 * File: YAtom.h.m4 *
12 * Last rev: 19/2/88 *
13 * mods: *
14 * comments: atom properties header file for YAP *
15 * *
16 *************************************************************************/
17
18/* This code can only be defined *after* including Regs.h!!! */
19
20#ifndef YATOM_H
21#define YATOM_H 1
22
23#include "inline-only.h"
24
25
26#ifdef USE_OFFSETS
27
28INLINE_ONLY Atom AbsAtom(AtomEntry *p);
29
30INLINE_ONLY Atom AbsAtom(AtomEntry *p) {
31 return (Atom)(Addr(p) - AtomBase);
32}
33
34INLINE_ONLY AtomEntry *RepAtom(Atom a);
35
36INLINE_ONLY AtomEntry *RepAtom(Atom a) {
37 return (AtomEntry *) (AtomBase + Unsigned (a);
38}
39
40#else
41
42INLINE_ONLY Atom AbsAtom(AtomEntry *p);
43
44INLINE_ONLY Atom AbsAtom(AtomEntry *p) { return (Atom)(p); }
45
46INLINE_ONLY AtomEntry *RepAtom(Atom a);
47
48INLINE_ONLY AtomEntry *RepAtom(Atom a) {
49 return (AtomEntry *)(a);
50}
51
52#endif
53
54#if USE_OFFSETS_IN_PROPS
55
56INLINE_ONLY Prop AbsProp(PropEntry *p);
57
58INLINE_ONLY Prop AbsProp(PropEntry *p) {
59 return (Prop)(Addr(p) - AtomBase);
60}
61
62INLINE_ONLY PropEntry *RepProp(Prop p);
63
64INLINE_ONLY PropEntry *RepProp(Prop p) {
65 return (PropEntry *)(AtomBase + Unsigned(p));
66}
67
68#else
69
70INLINE_ONLY Prop AbsProp(PropEntry *p);
71
72INLINE_ONLY Prop AbsProp(PropEntry *p) { return (Prop)(p); }
73
74INLINE_ONLY PropEntry *RepProp(Prop p);
75
76INLINE_ONLY PropEntry *RepProp(Prop p) {
77 return (PropEntry *)(p);
78}
79
80#endif
81
82#if USE_OFFSETS_IN_PROPS
83
84INLINE_ONLY FunctorEntry *RepFunctorProp(Prop p);
85
86INLINE_ONLY FunctorEntry *RepFunctorProp(Prop p) {
87 return (FunctorEntry *)(AtomBase + Unsigned(p));
88}
89
90INLINE_ONLY Prop AbsFunctorProp(FunctorEntry *p);
91
92INLINE_ONLY Prop AbsFunctorProp(FunctorEntry *p) {
93 return (Prop)(Addr(p) - AtomBase);
94}
95
96#else
97
98INLINE_ONLY FunctorEntry *RepFunctorProp(Prop p);
99
100INLINE_ONLY FunctorEntry *RepFunctorProp(Prop p) {
101 return (FunctorEntry *)(p);
102}
103
104INLINE_ONLY Prop AbsFunctorProp(FunctorEntry *p);
105
106INLINE_ONLY Prop AbsFunctorProp(FunctorEntry *p) {
107 return (Prop)(p);
108}
109
110#endif
111
112INLINE_ONLY arity_t ArityOfFunctor(Functor);
113
114INLINE_ONLY arity_t ArityOfFunctor(Functor Fun) {
115 return (arity_t)(((FunctorEntry *)Fun)->ArityOfFE);
116}
117
118INLINE_ONLY Atom NameOfFunctor(Functor);
119
120INLINE_ONLY Atom NameOfFunctor(Functor Fun) {
121 return (Atom)(((FunctorEntry *)Fun)->NameOfFE);
122}
123
124INLINE_ONLY PropFlags IsFunctorProperty(int);
125
126INLINE_ONLY PropFlags IsFunctorProperty(int flags) {
127 return (PropFlags)((flags == FunctorProperty));
128}
129
130/* summary of property codes used
131
132 00 00 predicate entry
133 80 00 db property
134 bb 00 functor entry
135 ff df sparse functor
136 ff ex arithmetic property
137 ff f4 translation
138 ff f5 blob
139 ff f6 hold
140 ff f7 array
141 ff f8 wide atom
142 ff fa module property
143 ff fb blackboard property
144 ff fc value property
145 ff fd global property
146 ff fe flag property
147 ff ff op property
148*/
149
150/* Global Variable property */
151typedef struct global_entry {
152 Prop NextOfPE; /* used to chain properties */
153 PropFlags KindOfPE; /* kind of property */
154#if defined(YAPOR) || defined(THREADS)
155 rwlock_t GRWLock; /* a simple lock to protect this entry */
156#if THREADS
157 unsigned int owner_id; /* owner thread */
158#endif
159#endif
160 struct AtomEntryStruct *AtomOfGE; /* parent atom for deletion */
161 struct global_entry *NextGE; /* linked list of global entries */
162 Term global; /* index in module table */
163 Term AttChain; /* index in module table */
165
166#if USE_OFFSETS_IN_PROPS
167
168INLINE_ONLY GlobalEntry *RepGlobalProp(Prop p);
169
170INLINE_ONLY GlobalEntry *RepGlobalProp(Prop p) {
171 return (GlobalEntry *)(AtomBase + Unsigned(p));
172}
173
174INLINE_ONLY Prop AbsGlobalProp(GlobalEntry *p);
175
176INLINE_ONLY Prop AbsGlobalProp(GlobalEntry *p) {
177 return (Prop)(Addr(p) - AtomBase);
178}
179
180#else
181
182INLINE_ONLY GlobalEntry *RepGlobalProp(Prop p);
183
184INLINE_ONLY GlobalEntry *RepGlobalProp(Prop p) {
185 return (GlobalEntry *)(p);
186}
187
188INLINE_ONLY Prop AbsGlobalProp(GlobalEntry *p);
189
190INLINE_ONLY Prop AbsGlobalProp(GlobalEntry *p) {
191 return (Prop)(p);
192}
193
194#endif
195
196#define GlobalProperty ((PropFlags)0xfffd)
197
198INLINE_ONLY PropFlags IsGlobalProperty(int);
199
200INLINE_ONLY PropFlags IsGlobalProperty(int flags) {
201 return (PropFlags)((flags == GlobalProperty));
202}
203
209typedef struct mod_entry {
210 Prop NextOfPE;
211 PropFlags KindOfPE;
216#if defined(YAPOR) || defined(THREADS)
217 rwlock_t ModRWLock;
218#endif
219 unsigned int flags;
221} ModEntry;
222
223#if USE_OFFSETS_IN_PROPS
224
225INLINE_ONLY ModEntry *RepModProp(Prop p);
226
227INLINE_ONLY ModEntry *RepModProp(Prop p) {
228 return (ModEntry *)(AtomBase + Unsigned(p));
229}
230
231INLINE_ONLY Prop AbsModProp(ModEntry *p);
232
233INLINE_ONLY Prop AbsModProp(ModEntry *p) {
234 return (Prop)(Addr(p) - AtomBase);
235}
236
237#else
238
239INLINE_ONLY ModEntry *RepModProp(Prop p);
240
241INLINE_ONLY ModEntry *RepModProp(Prop p) {
242 return (ModEntry *)(p);
243}
244
245INLINE_ONLY Prop AbsModProp(ModEntry *p);
246
247INLINE_ONLY Prop AbsModProp(ModEntry *p) { return (Prop)(p); }
248
249#define ModToTerm(m) (m == PROLOG_MODULE ? TermProlog : m)
250
251#endif
252
253#define ModProperty ((PropFlags)0xfffa)
254
255INLINE_ONLY bool IsModProperty(int);
256
257INLINE_ONLY bool IsModProperty(int flags) {
258 return flags == ModProperty;
259}
260
261/* Flags on module. Most of these flags are copied to the read context
262 in pl-read.c.
263*/
264#define M_SYSTEM (0x0001) /* system module */
265#define M_CHARESCAPE (0x0002) /* module */
266#define DBLQ_CHARS (0x0004) /* "ab" --> ['a', 'b'] */
267#define DBLQ_ATOM (0x0008) /* "ab" --> 'ab' */
268#define DBLQ_STRING (0x0010) /* "ab" --> "ab" */
269#define DBLQ_CODES (0x0020) /* "ab" --> [0'a, 0'b] */
270#define DBLQ_MASK (DBLQ_CHARS | DBLQ_ATOM | DBLQ_STRING | DBLQ_CODES)
271#define BCKQ_CHARS (0x0040) /* `ab` --> ['a', 'b'] */
272#define BCKQ_ATOM (0x0080) /* `ab` --> 'ab' */
273#define BCKQ_STRING (0x0100) /* `ab` --> "ab" */
274#define BCKQ_CODES (0x0200) /* `ab` --> [0'a, 0'b] */
275#define BCKQ_MASK (BCKQ_CHARS | BCKQ_ATOM | BCKQ_STRING | BCKQ_CODES)
276#define UNKNOWN_FAIL (0x0400) /* module */
277#define UNKNOWN_WARNING (0x0800) /* module */
278#define UNKNOWN_ERROR (0x1000) /* module */
279#define UNKNOWN_FAST_FAIL (0x2000) /* module */
280#define UNKNOWN_ABORT (0x4000) /* module */
281#define UNKNOWN_HALT (0x8000) /* module */
282#define UNKNOWN_MASK \
283 (UNKNOWN_ERROR | UNKNOWN_WARNING | UNKNOWN_FAIL | UNKNOWN_FAST_FAIL | \
284 UNKNOWN_ABORT | UNKNOWN_HALT)
285#define SNGQ_CHARS (0x10000) /* 'ab' --> [a, b] */
286#define SNGQ_ATOM (0x20000) /* 'ab' --> ab */
287#define SNGQ_STRING (0x40000) /* 'ab' --> "ab" */
288#define SNGQ_CODES (0x80000) /* 'ab' --> [0'a, 0'b] */
289#define SNGQ_MASK (BCKQ_CHARS | BCKQ_ATOM | BCKQ_STRING | BCKQ_CODES)
290
291Term Yap_getUnknownModule(ModEntry *m);
292void Yap_setModuleFlags(ModEntry *n, ModEntry *o);
293
294/* operator property entry structure */
295typedef struct operator_entry {
296 Prop NextOfPE; /* used to chain properties */
297 PropFlags KindOfPE; /* kind of property */
298#if defined(YAPOR) || defined(THREADS)
299 rwlock_t OpRWLock; /* a read-write lock to protect the entry */
300#endif
301 Atom OpName; /* atom name */
302 Term OpModule; /* module of predicate */
303 struct operator_entry *OpNext; /* next in list of operators */
304 struct operator_entry *NextForME; /* next in list of module operators */
305 BITS16 Prefix, Infix, Posfix;
306} OpEntry;
307#if USE_OFFSETS_IN_PROPS
308
309INLINE_ONLY OpEntry *RepOpProp(Prop p);
310
311INLINE_ONLY OpEntry *RepOpProp(Prop p) {
312 return (OpEntry *)(AtomBase + Unsigned(p));
313}
314
315INLINE_ONLY Prop AbsOpProp(OpEntry *p);
316
317INLINE_ONLY Prop AbsOpProp(OpEntry *p) {
318 return (Prop)(Addr(p) - AtomBase);
319}
320
321#else
322
323INLINE_ONLY OpEntry *RepOpProp(Prop p);
324
325INLINE_ONLY OpEntry *RepOpProp(Prop p) { return (OpEntry *)(p); }
326
327INLINE_ONLY Prop AbsOpProp(OpEntry *p);
328
329INLINE_ONLY Prop AbsOpProp(OpEntry *p) { return (Prop)(p); }
330
331#endif
332#define OpProperty ((PropFlags)0xffff)
333
334INLINE_ONLY bool IsOpProperty(PropFlags);
335
336INLINE_ONLY bool IsOpProperty(PropFlags flags) {
337 return flags == OpProperty;
338}
339
340typedef enum { INFIX_OP = 0, POSFIX_OP = 1, PREFIX_OP = 2 } op_type;
341
342OpEntry *Yap_GetOpProp(Atom, op_type, Term CACHE_TYPE);
343
344int Yap_IsPrefixOp(Atom, int *, int *);
345int Yap_IsOp(Atom);
346int Yap_IsInfixOp(Atom, int *, int *, int *);
347int Yap_IsPosfixOp(Atom, int *, int *);
348bool Yap_dup_op(OpEntry *op, ModEntry *she);
349
350/* defines related to operator specifications */
351#define MaskPrio 0x0fff
352#define DcrlpFlag 0x1000
353#define DcrrpFlag 0x2000
354
355typedef union arith_ret *eval_ret;
356
357/* expression property entry structure */
358typedef struct {
359 Prop NextOfPE; /* used to chain properties */
360 PropFlags KindOfPE; /* kind of property */
361 unsigned int ArityOfEE;
362 BITS16 ENoOfEE;
363 BITS16 FlagsOfEE;
364 /* operations that implement the expression */
365 int FOfEE;
366} ExpEntry;
367#if USE_OFFSETS_IN_PROPS
368
369INLINE_ONLY ExpEntry *RepExpProp(Prop p);
370
371INLINE_ONLY ExpEntry *RepExpProp(Prop p) {
372 return (ExpEntry *)(AtomBase + Unsigned(p));
373}
374
375INLINE_ONLY Prop AbsExpProp(ExpEntry *p);
376
377INLINE_ONLY Prop AbsExpProp(ExpEntry *p) {
378 return (Prop)(Addr(p) - AtomBase);
379}
380
381#else
382
383INLINE_ONLY ExpEntry *RepExpProp(Prop p);
384
385INLINE_ONLY ExpEntry *RepExpProp(Prop p) {
386 return (ExpEntry *)(p);
387}
388
389INLINE_ONLY Prop AbsExpProp(ExpEntry *p);
390
391INLINE_ONLY Prop AbsExpProp(ExpEntry *p) { return (Prop)(p); }
392
393#endif
394#define ExpProperty 0xffe0
395
396/* only unary and binary expressions are acceptable */
397
398INLINE_ONLY PropFlags IsExpProperty(int);
399
400INLINE_ONLY PropFlags IsExpProperty(int flags) {
401 return (PropFlags)((flags == ExpProperty));
402}
403
404/* value property entry structure */
405typedef struct {
406 Prop NextOfPE; /* used to chain properties */
407 PropFlags KindOfPE; /* kind of property */
408#if defined(YAPOR) || defined(THREADS)
409 rwlock_t VRWLock; /* a read-write lock to protect the entry */
410#endif
411 Term ValueOfVE; /* (atomic) value associated with the atom */
412} ValEntry;
413#if USE_OFFSETS_IN_PROPS
414
415INLINE_ONLY ValEntry *RepValProp(Prop p);
416
417INLINE_ONLY ValEntry *RepValProp(Prop p) {
418 return (ValEntry *)(AtomBase + Unsigned(p));
419}
420
421INLINE_ONLY Prop AbsValProp(ValEntry *p);
422
423INLINE_ONLY Prop AbsValProp(ValEntry *p) {
424 return (Prop)(Addr(p) - AtomBase);
425}
426
427#else
428
429INLINE_ONLY ValEntry *RepValProp(Prop p);
430
431INLINE_ONLY ValEntry *RepValProp(Prop p) {
432 return (ValEntry *)(p);
433}
434
435INLINE_ONLY Prop AbsValProp(ValEntry *p);
436
437INLINE_ONLY Prop AbsValProp(ValEntry *p) { return (Prop)(p); }
438
439#endif
440#define ValProperty ((PropFlags)0xfffc)
441
442/* predicate property entry structure */
443/* AsmPreds are things like var, nonvar, atom ...which are implemented
444 through dedicated machine instructions. In this case the 8 lower
445 bits of PredFlags are used to hold the machine instruction code
446 for the pred.
447 C_Preds are things write, read, ... implemented in C. In this case
448 CodeOfPred holds the address of the correspondent C-function.
449
450don't forget to also add in qly.h
451*/
453typedef uint64_t pred_flags_t;
454#define ProxyPredFlag \
455 ((pred_flags_t)0x4000000000) //< Predicate is a proxy for some other pred.
456#define UndefPredFlag \
457 ((pred_flags_t)0x4000000000) //< Predicate not explicitely defined.
458#define ProfiledPredFlag ((pred_flags_t)0x2000000000) //< pred is being profiled
459#define DiscontiguousPredFlag \
460 ((pred_flags_t)0x1000000000) //< predicates whose clauses may be all-over the
461 // place..
462#define SysExportPredFlag \
463 ((pred_flags_t)0x800000000) //< reuse export list to prolog module.
464#define NoTracePredFlag \
465 ((pred_flags_t)0x400000000) //< cannot trace this predicate
466#define NoSpyPredFlag ((pred_flags_t)0x200000000) //< cannot spy this predicate
467#define QuasiQuotationPredFlag \
468 ((pred_flags_t)0x100000000) //< SWI-like quasi quotations
469#define MegaClausePredFlag \
470 ((pred_flags_t)0x80000000) //< predicate is implemented as a mega-clause
471#define ThreadLocalPredFlag ((pred_flags_t)0x40000000) //< local to a thread
472#define MultiFileFlag ((pred_flags_t)0x20000000) //< is multi-file
473#define UserCPredFlag ((pred_flags_t)0x10000000) //< CPred defined by the user
474#define LogUpdatePredFlag \
475 ((pred_flags_t)0x08000000) //< dynamic predicate with log. upd. sem.
476#define InUsePredFlag ((pred_flags_t)0x04000000) //< count calls to pred
477#define CountPredFlag ((pred_flags_t)0x02000000) //< count calls to pred
478#define HiddenPredFlag ((pred_flags_t)0x01000000) //< invisible predicate
479#define CArgsPredFlag ((pred_flags_t)0x00800000) //< SWI-like C-interface pred.
480#define SourcePredFlag \
481 ((pred_flags_t)0x00400000) //< static predicate with source declaration
482#define MetaPredFlag \
483 ((pred_flags_t)0x00200000) //< predicate subject to a meta declaration
484#define SyncPredFlag \
485 ((pred_flags_t)0x00100000) //< has to synch before it can execute
486#define NumberDBPredFlag ((pred_flags_t)0x00080000) //< entry for an atom key
487#define AtomDBPredFlag ((pred_flags_t)0x00040000) //< entry for a number key
488// #define GoalExPredFlag ((pred_flags_t)0x00020000) /// predicate that is
489// called by goal_expand
490#define TestPredFlag ((pred_flags_t)0x00010000) //< is a test (optim. comit)
491#define AsmPredFlag ((pred_flags_t)0x00008000) //< inline
492#define StandardPredFlag ((pred_flags_t)0x00004000) //< system predicate
493#define DynamicPredFlag ((pred_flags_t)0x00002000) //< dynamic predicate
494#define CPredFlag ((pred_flags_t)0x00001000) //< written in C
495#define SafePredFlag ((pred_flags_t)0x00000800) //< does not alter arguments
496#define CompiledPredFlag ((pred_flags_t)0x00000400) //< is static
497#define IndexedPredFlag ((pred_flags_t)0x00000200) //< has indexing code
498#define SpiedPredFlag ((pred_flags_t)0x00000100) //< is a spy point
499#define BinaryPredFlag ((pred_flags_t)0x00000080) //< test predicate
500#define TabledPredFlag ((pred_flags_t)0x00000040) //< is tabled
501#define SequentialPredFlag \
502 ((pred_flags_t)0x00000020) //< may not create parallel choice points!
503#define BackCPredFlag ((pred_flags_t)0x00000008) //< Myddas Imported pred
504#define ModuleTransparentPredFlag ((pred_flags_t)0x00000004)
505#define SWIEnvPredFlag ((pred_flags_t)0x00000002) //< new SWI interface
506#define UDIPredFlag ((pred_flags_t)0x00000001) //< User Defined Indexing
507
508#define SystemPredFlags \
509 (AsmPredFlag | StandardPredFlag | CPredFlag | BinaryPredFlag | BackCPredFlag)
510#define ForeignPredFlags \
511 (AsmPredFlag | SWIEnvPredFlag | CPredFlag | BinaryPredFlag | UDIPredFlag | \
512 CArgsPredFlag | UserCPredFlag | SafePredFlag | BackCPredFlag)
513#define LivePredFlags \
514 (LogUpdatePredFlag | MultiFileFlag | TabledPredFlag | ForeignPredFlags)
515
516#define StatePredFlags \
517 (InUsePredFlag | CountPredFlag | SpiedPredFlag | IndexedPredFlag)
518#define is_system(pe) (pe->PredFlags & SystemPredFlags)
519#define is_dynamic(pe) (pe->PredFlags & DynamicPredFlag)
520#define is_foreign(pe) (pe->PredFlags & ForeignPredFlags)
521#define is_static(pe) (pe->PredFlags & CompiledPredFlag)
522#define is_logupd(pe) (pe->PredFlags & LogUpdatePredFlag)
523#define is_live(pe) (pe->PredFlags & LivePredFlags)
524#ifdef TABLING
525#define is_tabled(pe) (pe->PredFlags & TabledPredFlag)
526#endif /* TABLING */
527
528/* profile data */
529typedef struct {
530 UInt NOfEntries; /* nbr of times head unification succeeded */
531 UInt NOfHeadSuccesses; /* nbr of times head unification succeeded */
532 UInt NOfRetries; /* nbr of times a clause for the pred
533 was retried */
534#if defined(YAPOR) || defined(THREADS)
535 lockvar lock; /* a simple lock to protect this entry */
536#endif
538
539typedef enum { LUCALL_EXEC, LUCALL_ASSERT, LUCALL_RETRACT } timestamp_type;
540
541#define TIMESTAMP_EOT ((UInt)(~0L))
542#define TIMESTAMP_RESET (TIMESTAMP_EOT - 1024)
543
544typedef struct pred_entry {
545 Prop NextOfPE; /* used to chain properties */
546 PropFlags KindOfPE; /* kind of property */
547 struct yami *CodeOfPred;
548 OPCODE OpcodeOfPred; /* undefcode, indexcode, spycode, .... */
549 pred_flags_t PredFlags;
550 UInt ArityOfPE; /* arity of property */
551 union {
552 struct {
553 struct yami *TrueCodeOfPred; /* code address */
554 struct yami *FirstClause;
555 struct yami *LastClause;
556 UInt NOfClauses;
557 OPCODE ExpandCode;
558 } p_code;
559 CPredicate f_code;
560 CmpPredicate d_code;
561 } cs; /* if needing to spy or to lock */
562 Functor FunctorOfPred; /* functor for Predicate */
563 union {
564 Atom OwnerFile; /* File where the predicate was defined */
565 Int IndxId; /* Index for a certain key */
566 } src;
567#if defined(YAPOR) || defined(THREADS)
568 lockvar PELock; /* a simple lock to protect expansion */
569#endif
570#ifdef TABLING
571 struct table_entry * TableOfPred;
572#endif /* TABLING */
573#ifdef BEAM
574 struct Predicates *beamTable;
575#endif
576 struct yami *MetaEntryOfPred; /* allow direct access from meta-calls */
577 Term ModuleOfPred; /* module for this definition */
578 UInt TimeStampOfPred;
579 timestamp_type LastCallOfPred;
580 /* This must be at an odd number of cells, otherwise it
581 will not be aligned on RISC machines */
582 profile_data *StatisticsForPred; /* enable profiling for predicate */
583 struct pred_entry *NextPredOfModule; /* next pred for same module */
584 struct pred_entry *NextPredOfHash; /* next pred for same module */
585} PredEntry;
586#define PEProp ((PropFlags)(0x0000))
587
588#if USE_OFFSETS_IN_PROPS
589
590INLINE_ONLY PredEntry *RepPredProp(Prop p);
591
592INLINE_ONLY PredEntry *RepPredProp(Prop p) {
593 return (PredEntry *)(AtomBase + Unsigned(p));
594}
595
596INLINE_ONLY Prop AbsPredProp(PredEntry *p);
597
598INLINE_ONLY Prop AbsPredProp(PredEntry *p) {
599 return (Prop)(Addr(p) - AtomBase);
600}
601
602#else
603
604INLINE_ONLY PredEntry *RepPredProp(Prop p);
605
606INLINE_ONLY PredEntry *RepPredProp(Prop p) {
607
608 return (PredEntry *)(p);
609}
610
611INLINE_ONLY Prop AbsPredProp(PredEntry *p);
612
613INLINE_ONLY Prop AbsPredProp(PredEntry *p) { return (Prop)(p); }
614
615#endif
616
617INLINE_ONLY PropFlags IsPredProperty(int);
618
619INLINE_ONLY PropFlags IsPredProperty(int flags) {
620 return (PropFlags)((flags == PEProp));
621}
622
623
624extern Term IDB_MODULE;
625
626INLINE_ONLY Atom NameOfPred(PredEntry *pe) {
627 if (pe->ModuleOfPred == IDB_MODULE) {
628 return NULL;
629 } else if (pe->ArityOfPE == 0) {
630 return (Atom)pe->FunctorOfPred;
631 } else {
632 Functor f = pe->FunctorOfPred;
633 return NameOfFunctor(f);
634 }
635}
636
637extern const char *IndicatorOfPred(PredEntry *pe);
638
639extern PredEntry *Yap_get_pred(Term t, Term tmod, const char *pname);
640
641 extern PredEntry *Yap_new_pred(Term t, Term tmod, bool lu, const char *pname);
642
643profile_data *Yap_initProfiler(PredEntry *p);
644
645/* Flags for code or dbase entry */
646/* There are several flags for code and data base entries */
647typedef enum {
648 ExoMask = 0x1000000, /* is exo code */
649 FuncSwitchMask = 0x800000, /* is a switch of functors */
650 HasDBTMask = 0x400000, /* includes a pointer to a DBTerm */
651 MegaMask = 0x200000, /* mega clause */
652 FactMask = 0x100000, /* a fact */
653 SwitchRootMask = 0x80000, /* root for the index tree */
654 SwitchTableMask = 0x40000, /* switch table */
655 HasBlobsMask = 0x20000, /* blobs which may be in use */
656 ProfFoundMask = 0x10000, /* clause is being counted by profiler */
657 DynamicMask = 0x8000, /* dynamic predicate */
658 InUseMask = 0x4000, /* this block is being used */
659 ErasedMask = 0x2000, /* this block has been erased */
660 IndexMask = 0x1000, /* indexing code */
661 DBClMask = 0x0800, /* data base structure */
662 LogUpdRuleMask = 0x0400, /* code is for a log upd rule with env */
663 LogUpdMask = 0x0200, /* logic update index. */
664 StaticMask = 0x0100, /* static predicates */
665 DirtyMask = 0x0080, /* LUIndices */
666 HasCutMask = 0x0040, /* ! */
667 SrcMask = 0x0020, /* has a source term, only for static references */
668 /* other flags belong to DB */
669} dbentry_flags;
670
671/* predicate initialization */
672void Yap_InitCPred(const char *name, arity_t arity, CPredicate f,
673 pred_flags_t flags);
674void Yap_InitAsmPred(const char *name, arity_t arity, int code, CPredicate asmc,
675 pred_flags_t flags);
676void Yap_InitCmpPred(const char *name, arity_t arity, CmpPredicate cmp,
677 pred_flags_t flags);
678void Yap_InitCPredBack(const char *name, arity_t arity, arity_t extra,
679 CPredicate call, CPredicate retry, pred_flags_t flags);
680void Yap_InitCPredBackCut(const char *name, arity_t arity, arity_t extra,
681 CPredicate call, CPredicate retry, CPredicate cut,
682 pred_flags_t flags);
683void Yap_InitCPredBack_(const char *name, arity_t arity, arity_t extra,
684 CPredicate call, CPredicate retry, CPredicate cut,
685 pred_flags_t flags);
686
687/* *********************** DBrefs **************************************/
688
689typedef struct DB_TERM {
690#ifdef COROUTINING
691 union {
692 CELL attachments; /* attached terms */
693 Int line_number;
694 struct DB_TERM *NextDBT;
695 } ag;
696#endif
697 struct DB_STRUCT **DBRefs; /* pointer to other references */
698 CELL NOfCells; /* Size of Term */
699 CELL Entry; /* entry point */
700 Term Contents[MIN_ARRAY]; /* stored term */
701} DBTerm;
702
703INLINE_ONLY DBTerm *TermToDBTerm(Term);
704
705INLINE_ONLY DBTerm *TermToDBTerm(Term X) {
706 if (IsPairTerm(X)) {
707 return (DBTerm *)((unsigned char *)RepPair(X) - (CELL) &
708 (((DBTerm *)NULL)->Contents));
709 } else {
710 return (DBTerm *)((unsigned char *)RepAppl(X) - (CELL) &
711 (((DBTerm *)NULL)->Contents));
712 }
713}
714
715/* The ordering of the first 3 fields should be compatible with lu_clauses */
716typedef struct DB_STRUCT {
717 Functor id; /* allow pointers to this struct to id */
718 /* as dbref */
719 CELL Flags; /* Term Flags */
720#if defined(YAPOR) || defined(THREADS)
721 lockvar lock; /* a simple lock to protect this entry */
722#endif
723#if MULTIPLE_STACKS
724 Int ref_count; /* how many branches are using this entry */
725#endif
726 CELL NOfRefsTo; /* Number of references pointing here */
727 struct struct_dbentry *Parent; /* key of DBase reference */
728 struct yami *Code; /* pointer to code if this is a clause */
729 struct DB_STRUCT *Prev; /* Previous element in chain */
730 struct DB_STRUCT *Next; /* Next element in chain */
731 struct DB_STRUCT *p, *n; /* entry's age, negative if from recorda,
732 positive if it was recordz */
733 CELL Mask; /* parts that should be cleared */
734 CELL Key; /* A mask that can be used to check before
735 you unify */
736 DBTerm DBT;
737} DBStruct;
738
739#define DBStructFlagsToDBStruct(X) \
740 ((DBRef)((unsigned char *)(X) - (CELL) & (((DBRef)NULL)->Flags)))
741
742#if MULTIPLE_STACKS
743#define INIT_DBREF_COUNT(X) (X)->ref_count = 0
744#define INC_DBREF_COUNT(X) (X)->ref_count++
745#define DEC_DBREF_COUNT(X) (X)->ref_count--
746#define DBREF_IN_USE(X) ((X)->ref_count != 0)
747#else
748#define INIT_DBREF_COUNT(X)
749#define INC_DBREF_COUNT(X)
750#define DEC_DBREF_COUNT(X)
751#define DBREF_IN_USE(X) ((X)->Flags & InUseMask)
752#endif
753
754typedef DBStruct *DBRef;
755
756/* extern Functor FunctorDBRef; */
757
758INLINE_ONLY int IsDBRefTerm(Term);
759
760INLINE_ONLY int IsDBRefTerm(Term t) {
761 return (int)(IsApplTerm(t) && FunctorOfTerm(t) == FunctorDBRef);
762}
763
764INLINE_ONLY Term MkDBRefTerm(DBRef);
765
766INLINE_ONLY Term MkDBRefTerm(DBRef p) {
767 return (Term)((AbsAppl(((CELL *)(p)))));
768}
769
770INLINE_ONLY DBRef DBRefOfTerm(Term t);
771
772INLINE_ONLY DBRef DBRefOfTerm(Term t) {
773 return (DBRef)(((DBRef)(RepAppl(t))));
774}
775
776INLINE_ONLY int IsRefTerm(Term);
777
778INLINE_ONLY int IsRefTerm(Term t) {
779 return (int)(IsApplTerm(t) && FunctorOfTerm(t) == FunctorDBRef);
780}
781
782INLINE_ONLY CODEADDR RefOfTerm(Term t);
783
784INLINE_ONLY CODEADDR RefOfTerm(Term t) {
785 return (CODEADDR)(DBRefOfTerm(t));
786}
787
788typedef struct struct_dbentry {
789 Prop NextOfPE; /* used to chain properties */
790 PropFlags KindOfPE; /* kind of property */
791 unsigned int ArityOfDB; /* kind of property */
792 Functor FunctorOfDB; /* functor for this property */
793#if defined(YAPOR) || defined(THREADS)
794 rwlock_t DBRWLock; /* a simple lock to protect this entry */
795#endif
796 DBRef First; /* first DBase entry */
797 DBRef Last; /* last DBase entry */
798 Term ModuleOfDB; /* module for this definition */
799 DBRef F0, L0; /* everyone */
800} DBEntry;
801typedef DBEntry *DBProp;
802#define DBProperty ((PropFlags)0x8000)
803
804typedef struct {
805 Prop NextOfPE; /* used to chain properties */
806 PropFlags KindOfPE; /* kind of property */
807 unsigned int ArityOfDB; /* kind of property */
808 Functor FunctorOfDB; /* functor for this property */
809#if defined(YAPOR) || defined(THREADS)
810 rwlock_t DBRWLock; /* a simple lock to protect this entry */
811#endif
812 DBRef First; /* first DBase entry */
813 DBRef Last; /* last DBase entry */
814 Term ModuleOfDB; /* module for this definition */
815 Int NOfEntries; /* age counter */
816 DBRef Index; /* age counter */
819#define CodeDBBit 0x2
820
821#define CodeDBProperty (DBProperty | CodeDBBit)
822
823INLINE_ONLY PropFlags IsDBProperty(int);
824
825INLINE_ONLY PropFlags IsDBProperty(int flags) {
826 return (PropFlags)((flags & ~CodeDBBit) == DBProperty);
827}
828
829#if USE_OFFSETS_IN_PROPS
830
831INLINE_ONLY DBProp RepDBProp(Prop p);
832
833INLINE_ONLY DBProp RepDBProp(Prop p) {
834 return (DBProp)(AtomBase + Unsigned(p));
835}
836
837INLINE_ONLY Prop AbsDBProp(DBProp p);
838
839INLINE_ONLY Prop AbsDBProp(DBProp p) {
840 return (Prop)(Addr(p) - AtomBase);
841}
842
843#else
844
845INLINE_ONLY DBProp RepDBProp(Prop p);
846
847INLINE_ONLY DBProp RepDBProp(Prop p) { return (DBProp)(p); }
848
849INLINE_ONLY Prop AbsDBProp(DBProp p);
850
851INLINE_ONLY Prop AbsDBProp(DBProp p) { return (Prop)(p); }
852
853#endif
854
855/* These are the actual flags for DataBase terms */
856typedef enum {
857 DBAtomic = 0x1,
858 DBVar = 0x2,
859 DBNoVars = 0x4,
860 DBComplex = 0x8,
861 DBCode = 0x10,
862 DBNoCode = 0x20,
863 DBWithRefs = 0x40
864} db_term_flags;
865
866typedef struct {
867 Prop NextOfPE; /* used to chain properties */
868 PropFlags KindOfPE; /* kind of property */
869 Atom KeyOfBB; /* functor for this property */
870 Term Element; /* blackboard element */
871#if defined(YAPOR) || defined(THREADS)
872 rwlock_t BBRWLock; /* a read-write lock to protect the entry */
873#endif
874 Term ModuleOfBB; /* module for this definition */
876typedef BlackBoardEntry *BBProp;
877
878#if USE_OFFSETS_IN_PROPS
879
880INLINE_ONLY BlackBoardEntry *RepBBProp(Prop p);
881
882INLINE_ONLY BlackBoardEntry *RepBBProp(Prop p) {
883 return (BlackBoardEntry *)(AtomBase + Unsigned(p));
884}
885
886INLINE_ONLY Prop AbsBBProp(BlackBoardEntry *p);
887
888INLINE_ONLY Prop AbsBBProp(BlackBoardEntry *p) {
889 return (Prop)(Addr(p) - AtomBase);
890}
891
892#else
893
894INLINE_ONLY BlackBoardEntry *RepBBProp(Prop p);
895
896INLINE_ONLY BlackBoardEntry *RepBBProp(Prop p) {
897 return (BlackBoardEntry *)(p);
898}
899
900INLINE_ONLY Prop AbsBBProp(BlackBoardEntry *p);
901
902INLINE_ONLY Prop AbsBBProp(BlackBoardEntry *p) {
903 return (Prop)(p);
904}
905
906#endif
907
908#define BBProperty ((PropFlags)0xfffb)
909
910INLINE_ONLY PropFlags IsBBProperty(int);
911
912INLINE_ONLY PropFlags IsBBProperty(int flags) {
913 return (PropFlags)((flags == BBProperty));
914}
915
916/* hold property entry structure */
917typedef struct hold_entry {
918 Prop NextOfPE; /* used to chain properties */
919 PropFlags KindOfPE; /* kind of property */
920 UInt RefsOfPE; /* used to count the number of holds */
921} HoldEntry;
922
923#if USE_OFFSETS_IN_PROPS
924
925INLINE_ONLY HoldEntry *RepHoldProp(Prop p);
926
927INLINE_ONLY HoldEntry *RepHoldProp(Prop p) {
928 return (HoldEntry *)(AtomBase + Unsigned(p));
929}
930
931INLINE_ONLY Prop AbsHoldProp(HoldEntry *p);
932
933INLINE_ONLY Prop AbsHoldProp(HoldEntry *p) {
934 return (Prop)(Addr(p) - AtomBase);
935}
936
937#else
938
939INLINE_ONLY HoldEntry *RepHoldProp(Prop p);
940
941INLINE_ONLY HoldEntry *RepHoldProp(Prop p) {
942 return (HoldEntry *)(p);
943}
944
945INLINE_ONLY Prop AbsHoldProp(HoldEntry *p);
946
947INLINE_ONLY Prop AbsHoldProp(HoldEntry *p) { return (Prop)(p); }
948
949#endif
950
951#define HoldProperty 0xfff6
952
953/* translation property entry structure */
954typedef struct translation_entry {
955 Prop NextOfPE; /* used to chain properties */
956 PropFlags KindOfPE; /* kind of property */
957 arity_t arity; /* refers to atom (0) or functor(N > 0) */
958 Int Translation; /* used to hash the atom as an integer; */
960
961#if USE_OFFSETS_IN_PROPS
962
963INLINE_ONLY TranslationEntry *RepTranslationProp(Prop p);
964
965INLINE_ONLY TranslationEntry *RepTranslationProp(Prop p) {
966 return (TranslationEntry *)(AtomBase + Unsigned(p));
967}
968
969INLINE_ONLY Prop AbsTranslationProp(TranslationEntry *p);
970
971INLINE_ONLY Prop AbsTranslationProp(TranslationEntry *p) {
972 return (Prop)(Addr(p) - AtomBase);
973}
974
975#else
976
977INLINE_ONLY TranslationEntry *RepTranslationProp(Prop p);
978
979INLINE_ONLY TranslationEntry *RepTranslationProp(Prop p) {
980 return (TranslationEntry *)(p);
981}
982
983INLINE_ONLY Prop AbsTranslationProp(TranslationEntry *p);
984
985INLINE_ONLY Prop AbsTranslationProp(TranslationEntry *p) {
986 return (Prop)(p);
987}
988
989#endif
990#define TranslationProperty 0xfff4
991
992bool Yap_PutAtomTranslation(Atom a, arity_t arity, Int i);
993
994/* get translation prop for atom; */
995static inline TranslationEntry *Yap_GetTranslationProp(Atom at, arity_t arity) {
996 Prop p0;
997 AtomEntry *ae = RepAtom(at);
999
1000 READ_LOCK(ae->ARWLock);
1001 p = RepTranslationProp(p0 = ae->PropsOfAE);
1002 while (p0 && (p->KindOfPE != TranslationProperty || p->arity != arity))
1003 p = RepTranslationProp(p0 = p->NextOfPE);
1004 READ_UNLOCK(ae->ARWLock);
1005 if (p0 == NIL)
1006 return (TranslationEntry *)NULL;
1007 p->arity = arity;
1008 return p;
1009}
1010
1011INLINE_ONLY bool IsTranslationProperty(PropFlags);
1012
1013INLINE_ONLY bool IsTranslationProperty(PropFlags flags) {
1014 return flags == TranslationProperty;
1015}
1016
1017/*** handle named mutexes */
1018
1019/* named mutex property entry structure */
1020typedef struct mutex_entry {
1021 Prop NextOfPE; /* used to chain properties */
1022 PropFlags KindOfPE; /* kind of property */
1023 void *Mutex; /* used to hash the atom as an integer; */
1024} MutexEntry;
1025
1026#if USE_OFFSETS_IN_PROPS
1027
1028INLINE_ONLY MutexEntry *RepMutexProp(Prop p);
1029
1030INLINE_ONLY MutexEntry *RepMutexProp(Prop p) {
1031 return (MutexEntry *)(AtomBase + Unsigned(p));
1032}
1033
1034INLINE_ONLY Prop AbsMutexProp(MutexEntry *p);
1035
1036INLINE_ONLY Prop AbsMutexProp(MutexEntry *p) {
1037 return (Prop)(Addr(p) - AtomBase);
1038}
1039
1040#else
1041
1042INLINE_ONLY MutexEntry *RepMutexProp(Prop p);
1043
1044INLINE_ONLY MutexEntry *RepMutexProp(Prop p) {
1045 return (MutexEntry *)(p);
1046}
1047
1048INLINE_ONLY Prop AbsMutexProp(MutexEntry *p);
1049
1050INLINE_ONLY Prop AbsMutexProp(MutexEntry *p) { return (Prop)(p); }
1051
1052#endif
1053#define MutexProperty 0xfff5
1054
1055bool Yap_PutAtomMutex(Atom a, void *ptr);
1056
1057/* get mutex prop for atom; */
1058static inline void *Yap_GetMutexFromProp(Atom at) {
1059 Prop p0;
1060 AtomEntry *ae = RepAtom(at);
1061 MutexEntry *p;
1062
1063 READ_LOCK(ae->ARWLock);
1064 p = RepMutexProp(p0 = ae->PropsOfAE);
1065 while (p0 && p->KindOfPE != MutexProperty)
1066 p = RepMutexProp(p0 = p->NextOfPE);
1067 READ_UNLOCK(ae->ARWLock);
1068 if (p0 == NIL)
1069 return NULL;
1070 return p->Mutex;
1071}
1072
1073INLINE_ONLY bool IsMutexProperty(PropFlags);
1074
1075INLINE_ONLY bool IsMutexProperty(PropFlags flags) {
1076 return (PropFlags)((flags == MutexProperty));
1077}
1078
1079/* end of code for named mutexes */
1080
1081
1082#include "arrays.h"
1083
1084#if USE_OFFSETS_IN_PROPS
1085
1086INLINE_ONLY ArrayEntry *RepArrayProp(Prop p);
1087
1088INLINE_ONLY ArrayEntry *RepArrayProp(Prop p) {
1089 return (ArrayEntry *)(AtomBase + Unsigned(p));
1090}
1091
1092INLINE_ONLY Prop AbsArrayProp(ArrayEntry *p);
1093
1094INLINE_ONLY Prop AbsArrayProp(ArrayEntry *p) {
1095 return (Prop)(Addr(p) - AtomBase);
1096}
1097
1098INLINE_ONLY StaticArrayEntry *RepStaticArrayProp(Prop p);
1099
1100INLINE_ONLY StaticArrayEntry *RepStaticArrayProp(Prop p) {
1101 return (StaticArrayEntry *)(AtomBase + Unsigned(p));
1102}
1103
1104INLINE_ONLY Prop AbsStaticArrayProp(StaticArrayEntry *p);
1105
1106INLINE_ONLY Prop AbsStaticArrayProp(StaticArrayEntry *p) {
1107 return (Prop)(Addr(p) - AtomBase);
1108}
1109
1110#else
1111
1112INLINE_ONLY ArrayEntry *RepArrayProp(Prop p);
1113
1114INLINE_ONLY ArrayEntry *RepArrayProp(Prop p) {
1115 return (ArrayEntry *)(p);
1116}
1117
1118INLINE_ONLY Prop AbsArrayProp(ArrayEntry *p);
1119
1120INLINE_ONLY Prop AbsArrayProp(ArrayEntry *p) { return (Prop)(p); }
1121
1122INLINE_ONLY StaticArrayEntry *RepStaticArrayProp(Prop p);
1123
1124INLINE_ONLY StaticArrayEntry *RepStaticArrayProp(Prop p) {
1125 return (StaticArrayEntry *)(p);
1126}
1127
1128INLINE_ONLY Prop AbsStaticArrayProp(StaticArrayEntry *p);
1129
1130INLINE_ONLY Prop AbsStaticArrayProp(StaticArrayEntry *p) {
1131 return (Prop)(p);
1132}
1133
1134#endif
1135#define ArrayProperty ((PropFlags)0xfff7)
1136
1137INLINE_ONLY bool ArrayIsDynamic(ArrayEntry *);
1138
1139INLINE_ONLY bool ArrayIsDynamic(ArrayEntry *are) {
1140 return ((are)->TypeOfAE & DYNAMIC_ARRAY) != 0;
1141}
1142
1143INLINE_ONLY bool IsArrayProperty(PropFlags);
1144
1145INLINE_ONLY bool IsArrayProperty(PropFlags flags) {
1146 return flags == ArrayProperty;
1147}
1148
1149/* SWI Blob property */
1150typedef struct YAP_blob_prop_entry {
1151 Prop NextOfPE; /* used to chain properties */
1152 PropFlags KindOfPE; /* kind of property */
1153 struct _PL_blob_t *blob_type; /* type of blob */
1155
1156#if USE_OFFSETS_IN_PROPS
1157
1158INLINE_ONLY YAP_BlobPropEntry *RepBlobProp(Prop p);
1159
1160INLINE_ONLY YAP_BlobPropEntry *RepBlobProp(Prop p) {
1161 return (YAP_BlobPropEntry *)(AtomBase + Unsigned(p));
1162}
1163
1164INLINE_ONLY AtomEntry *AbsBlobProp(BlobPropEntry *p);
1165
1166INLINE_ONLY Prop AbsBlobProp(YAP_BlobPropEntry *p) {
1167 return (Prop)(Addr(p) - AtomBase);
1168}
1169
1170#else
1171
1172INLINE_ONLY YAP_BlobPropEntry *RepBlobProp(Prop p);
1173
1174INLINE_ONLY YAP_BlobPropEntry *RepBlobProp(Prop p) {
1175 return (YAP_BlobPropEntry *)(p);
1176}
1177
1178INLINE_ONLY Prop AbsBlobProp(YAP_BlobPropEntry *p);
1179
1180INLINE_ONLY Prop AbsBlobProp(YAP_BlobPropEntry *p) {
1181 return (Prop)(p);
1182}
1183
1184#endif
1185
1186#define BlobProperty ((PropFlags)0xfffe)
1187
1188INLINE_ONLY bool IsBlobProperty(PropFlags);
1189
1190INLINE_ONLY bool IsBlobProperty(PropFlags flags) {
1191 return flags == BlobProperty;
1192}
1193
1194INLINE_ONLY bool IsBlob(Atom);
1195
1196INLINE_ONLY bool IsBlob(Atom at) {
1197 return RepAtom(at)->PropsOfAE != NIL &&
1198 IsBlobProperty(RepBlobProp(RepAtom(at)->PropsOfAE)->KindOfPE);
1199}
1200
1201INLINE_ONLY bool IsValProperty(PropFlags);
1202
1203INLINE_ONLY bool IsValProperty(PropFlags flags) {
1204 return flags == ValProperty;
1205}
1206
1207/* flag property entry structure */
1208
1209typedef Term (*flag_func)(Term);
1210typedef bool (*flag_helper_func)(Term);
1211
1212typedef struct {
1213 Prop NextOfPE; /* used to chain properties */
1214 PropFlags KindOfPE; /* kind of property */
1215#if defined(YAPOR) || defined(THREADS)
1216 rwlock_t VRWLock; /* a read-write lock to protect the entry */
1217#endif
1218 int FlagOfVE; /* (atomic) value associated with the atom */
1219 bool global, atomic, rw;
1220 flag_func type;
1221 flag_helper_func helper;
1222} FlagEntry;
1223#if USE_OFFSETS_IN_PROPS
1224
1225INLINE_ONLY FlagEntry *RepFlagProp(Prop p);
1226
1227INLINE_ONLY FlagEntry *RepFlagProp(Prop p) {
1228 return (FlagEntry *)(AtomBase + Unsigned(p));
1229}
1230
1231INLINE_ONLY Prop AbsFlagProp(FlagEntry *p);
1232
1233INLINE_ONLY Prop AbsValProp(FlagEntry *p) {
1234 return (Prop)(Addr(p) - AtomBase);
1235}
1236
1237#else
1238
1239INLINE_ONLY FlagEntry *RepFlagProp(Prop p);
1240
1241INLINE_ONLY FlagEntry *RepFlagProp(Prop p) {
1242 return (FlagEntry *)(p);
1243}
1244
1245INLINE_ONLY Prop AbsFlagProp(FlagEntry *p);
1246
1247INLINE_ONLY Prop AbsFlagProp(FlagEntry *p) { return (Prop)(p); }
1248
1249#endif
1250#define FlagProperty ((PropFlags)0xfff9)
1251
1252INLINE_ONLY bool IsFlagProperty(PropFlags);
1253
1254INLINE_ONLY bool IsFlagProperty(PropFlags flags) {
1255 return flags == FlagProperty;
1256}
1257
1258/* Proto types */
1259
1260/* cdmgr.c */
1261int Yap_RemoveIndexation(PredEntry *);
1262void Yap_UpdateTimestamps(PredEntry *);
1263
1264/* dbase.c */
1265void Yap_ErDBE(DBRef);
1266DBTerm *Yap_StoreTermInDB(Term, int);
1267DBTerm *Yap_StoreTermInDBPlusExtraSpace(Term, UInt, UInt *);
1268Term Yap_FetchTermFromDB(void *);
1269Term Yap_FetchClauseTermFromDB(void *);
1270Term Yap_PopTermFromDB(void *);
1271void Yap_ReleaseTermFromDB(void *);
1272
1273/* init.c */
1274Atom Yap_GetOp(OpEntry *, int *, int);
1275
1276/* vsc: redefined to GetAProp to avoid conflicts with Windows header files */
1277Prop Yap_GetAProp(Atom, PropFlags);
1278Prop Yap_GetAPropHavingLock(AtomEntry *, PropFlags);
1279
1280#define PROLOG_MODULE 0
1281
1282#include "YapHeap.h"
1283
1284#define PredHashInitialSize ((UInt)1039)
1285#define PredHashIncrement ((UInt)7919)
1286
1287/*************************************************************************************************
1288 flag support
1289*************************************************************************************************/
1290
1291#include "YapFlags.h"
1292INLINE_ONLY UInt PRED_HASH(FunctorEntry *, Term, UInt);
1293
1294INLINE_ONLY UInt PRED_HASH(FunctorEntry *fe, Term cur_mod,
1295 UInt size) {
1296 return (((CELL)fe + cur_mod) >> 2) % size;
1297}
1298
1299INLINE_ONLY Prop GetPredPropByFuncAndModHavingLock(FunctorEntry *,
1300 Term);
1301INLINE_ONLY Prop PredPropByFuncAndMod(FunctorEntry *, Term);
1302INLINE_ONLY Prop PredPropByAtomAndMod(Atom, Term);
1303INLINE_ONLY Prop GetPredPropByFuncHavingLock(FunctorEntry *,
1304 Term);
1305INLINE_ONLY Prop PredPropByFunc(Functor fe, Term cur_mod);
1306INLINE_ONLY Prop PredPropByAtom(Atom at, Term cur_mod);
1307
1308#ifdef THREADS
1309
1310Prop Yap_NewThreadPred(struct pred_entry *CACHE_TYPE);
1311Prop Yap_NewPredPropByFunctor(Functor, Term);
1312INLINE_ONLY struct pred_entry *
1313Yap_GetThreadPred(struct pred_entry *CACHE_TYPE);
1314
1315INLINE_ONLY struct pred_entry *
1316Yap_GetThreadPred(struct pred_entry *ap USES_REGS) {
1317 Functor f = ap->FunctorOfPred;
1318 Term mod = ap->ModuleOfPred;
1319 Prop p0 = AbsPredProp(LOCAL_ThreadHandle.local_preds);
1320
1321 while (p0) {
1322 PredEntry *ap = RepPredProp(p0);
1323 if (ap->FunctorOfPred == f && ap->ModuleOfPred == mod)
1324 return ap;
1325 p0 = ap->NextOfPE;
1326 }
1327 return RepPredProp(Yap_NewThreadPred(ap PASS_REGS));
1328}
1329#endif
1330
1331INLINE_ONLY Prop GetPredPropByFuncHavingLock(FunctorEntry *fe,
1332 Term cur_mod) {
1333 PredEntry *p;
1334
1335 if (!(p = RepPredProp(fe->PropsOfFE))) {
1336 return NIL;
1337 }
1338 if ((p->ModuleOfPred == cur_mod || !(p->ModuleOfPred))) {
1339#ifdef THREADS
1340 /* Thread Local Predicates */
1341 if (p->PredFlags & ThreadLocalPredFlag) {
1342 return AbsPredProp(Yap_GetThreadPred(p INIT_REGS));
1343 }
1344#endif
1345 return AbsPredProp(p);
1346 }
1347 if (p->NextOfPE) {
1348 UInt hash = PRED_HASH(fe, cur_mod, PredHashTableSize);
1349 READ_LOCK(PredHashRWLock);
1350 p = PredHash[hash];
1351
1352 while (p) {
1353 if (p->FunctorOfPred == fe && p->ModuleOfPred == cur_mod) {
1354#ifdef THREADS
1355 /* Thread Local Predicates */
1356 if (p->PredFlags & ThreadLocalPredFlag) {
1357 READ_UNLOCK(PredHashRWLock);
1358 return AbsPredProp(Yap_GetThreadPred(p INIT_REGS));
1359 }
1360#endif
1361 READ_UNLOCK(PredHashRWLock);
1362 return AbsPredProp(p);
1363 }
1364 p = p->NextPredOfHash;
1365 }
1366 READ_UNLOCK(PredHashRWLock);
1367 }
1368 return NIL;
1369}
1370
1371INLINE_ONLY Prop PredPropByFunc(Functor fe, Term cur_mod)
1372/* get predicate entry for ap/arity; create it if neccessary. */
1373{
1374 Prop p0;
1375
1376 FUNC_WRITE_LOCK(fe);
1377 p0 = GetPredPropByFuncHavingLock(fe, cur_mod);
1378 if (p0) {
1379 FUNC_WRITE_UNLOCK(fe);
1380 return p0;
1381 }
1382 return Yap_NewPredPropByFunctor(fe, cur_mod);
1383}
1384
1385INLINE_ONLY Prop
1386GetPredPropByFuncAndModHavingLock(FunctorEntry *fe, Term cur_mod) {
1387 PredEntry *p;
1388
1389 if (!(p = RepPredProp(fe->PropsOfFE))) {
1390 return NIL;
1391 }
1392 if (p->ModuleOfPred == cur_mod || p->ModuleOfPred == 0) {
1393#ifdef THREADS
1394 /* Thread Local Predicates */
1395 if (p->PredFlags & ThreadLocalPredFlag) {
1396 return AbsPredProp(Yap_GetThreadPred(p INIT_REGS));
1397 }
1398#endif
1399 return AbsPredProp(p);
1400 }
1401 if (p->NextOfPE) {
1402 UInt hash = PRED_HASH(fe, cur_mod, PredHashTableSize);
1403 READ_LOCK(PredHashRWLock);
1404 p = PredHash[hash];
1405
1406 while (p) {
1407 if (p->FunctorOfPred == fe && p->ModuleOfPred == cur_mod) {
1408#ifdef THREADS
1409 /* Thread Local Predicates */
1410 if (p->PredFlags & ThreadLocalPredFlag) {
1411 READ_UNLOCK(PredHashRWLock);
1412 return AbsPredProp(Yap_GetThreadPred(p INIT_REGS));
1413 }
1414#endif
1415 READ_UNLOCK(PredHashRWLock);
1416 return AbsPredProp(p);
1417 }
1418 p = p->NextPredOfHash;
1419 }
1420 READ_UNLOCK(PredHashRWLock);
1421 }
1422 return NIL;
1423}
1424
1425INLINE_ONLY Prop PredPropByFuncAndMod(Functor fe, Term cur_mod)
1426/* get predicate entry for ap/arity; create it if neccessary. */
1427{
1428 Prop p0;
1429
1430 FUNC_WRITE_LOCK(fe);
1431 p0 = GetPredPropByFuncAndModHavingLock(fe, cur_mod);
1432 if (p0) {
1433 FUNC_WRITE_UNLOCK(fe);
1434 return p0;
1435 }
1436 return Yap_NewPredPropByFunctor(fe, cur_mod);
1437}
1438
1439INLINE_ONLY Prop PredPropByAtom(Atom at, Term cur_mod)
1440/* get predicate entry for ap/arity; create it if neccessary. */
1441{
1442 Prop p0;
1443 AtomEntry *ae = RepAtom(at);
1444
1445 WRITE_LOCK(ae->ARWLock);
1446 p0 = ae->PropsOfAE;
1447 while (p0) {
1448 PredEntry *pe = RepPredProp(p0);
1449 if (pe->KindOfPE == PEProp &&
1450 (pe->ModuleOfPred == cur_mod || !pe->ModuleOfPred)) {
1451#ifdef THREADS
1452 /* Thread Local Predicates */
1453 if (pe->PredFlags & ThreadLocalPredFlag) {
1454 WRITE_UNLOCK(ae->ARWLock);
1455 return AbsPredProp(Yap_GetThreadPred(pe INIT_REGS));
1456 }
1457#endif
1458 WRITE_UNLOCK(ae->ARWLock);
1459 return (p0);
1460 }
1461 p0 = pe->NextOfPE;
1462 }
1463 return Yap_NewPredPropByAtom(ae, cur_mod);
1464}
1465
1466INLINE_ONLY Prop PredPropByAtomAndMod(Atom at, Term cur_mod)
1467/* get predicate entry for ap/arity; create it if neccessary. */
1468{
1469 Prop p0;
1470 AtomEntry *ae = RepAtom(at);
1471
1472 WRITE_LOCK(ae->ARWLock);
1473 p0 = ae->PropsOfAE;
1474 while (p0) {
1475 PredEntry *pe = RepPredProp(p0);
1476 if (pe->KindOfPE == PEProp &&
1477 (pe->ModuleOfPred == cur_mod || pe->ModuleOfPred == 0)) {
1478#ifdef THREADS
1479 /* Thread Local Predicates */
1480 if (pe->PredFlags & ThreadLocalPredFlag) {
1481 WRITE_UNLOCK(ae->ARWLock);
1482 return AbsPredProp(Yap_GetThreadPred(pe INIT_REGS));
1483 }
1484#endif
1485 WRITE_UNLOCK(ae->ARWLock);
1486 return (p0);
1487 }
1488 p0 = pe->NextOfPE;
1489 }
1490 return Yap_NewPredPropByAtom(ae, cur_mod);
1491}
1492
1493#if DEBUG_PELOCKING
1494#define PELOCK(I, Z) \
1495 { \
1496 LOCK((Z)->PELock); \
1497 (Z)->StatisticsForPred->NOfEntries = (I); \
1498 (Z)->StatisticsForPred->NOfHeadSuccesses = pthread_self(); \
1499 }
1500#define UNLOCKPE(I, Z) \
1501 ((Z)->StatisticsForPred->NOfRetries = (I), UNLOCK((Z)->PELock))
1502#elif YAPOR || THREADS
1503#define PELOCK(I, Z) (LOCK((Z)->PELock))
1504#define UNLOCKPE(I, Z) (UNLOCK((Z)->PELock))
1505#else
1506#define PELOCK(I, Z)
1507#define UNLOCKPE(I, Z)
1508#endif
1509
1510INLINE_ONLY void AddPropToAtom(AtomEntry *, PropEntry *p);
1511
1512INLINE_ONLY void AddPropToAtom(AtomEntry *ae, PropEntry *p) {
1513 /* old properties should be always last, and wide atom properties
1514 should always be first */
1515 p->NextOfPE = ae->PropsOfAE;
1516 ae->PropsOfAE = AbsProp(p);
1517}
1518
1519// auxiliary functions
1520
1521INLINE_ONLY const char *AtomName(Atom at);
1522
1531INLINE_ONLY const char *AtomName(Atom at) {
1532 return RepAtom(at)->rep.uStrOfAE;
1533}
1534
1535INLINE_ONLY const char *AtomTermName(Term t);
1536
1548INLINE_ONLY const char *AtomTermName(Term t) {
1549 return RepAtom(AtomOfTerm(t))->rep.uStrOfAE;
1550}
1551
1554extern bool Yap_HasException(void);
1557INLINE_ONLY bool Yap_HasException(void) {
1558 return LOCAL_ActiveError->errorNo != 0L;
1559}
1560/* INLINE_ONLY void *Yap_RefToException(void) { */
1561/* void *dbt = Yap_StoreTermInDB(LOCAL_ActiveError->culprit,false); */
1562/* LOCAL_ActiveError->culprit = 0L; */
1563/* return dbt; */
1564/* } */
1565
1566bool Yap_RaiseException();
1567
1568#endif
bool Yap_ResetException(yap_error_descriptor_t *i)
clean up (notice that the code ensures ActiveError exists on exit
Definition: errors.c:1425
yap_error_descriptor_t * Yap_PeekException(void)
is an error active?
Definition: errors.c:1377
bool Yap_RaiseException()
let's go
Definition: errors.c:1410
bool Yap_RestartException(yap_error_descriptor_t *i)
clean up (notice that the code ensures ActiveError exists on exit
Definition: errors.c:1438
yap_error_descriptor_t * Yap_GetException(void)
clone Active Error
Definition: errors.c:1389
Definition: Yatom.h:689
Definition: Yatom.h:1150
Definition: arrays.h:92
Definition: Yatom.h:151
Definition: Yatom.h:917
A matrix.
Definition: matrix.c:68
Module property: low-level data used to manage modes.
Definition: Yatom.h:209
struct mod_entry * NextME
Module local flags (from SWI compat)
Definition: Yatom.h:220
Atom AtomOfME
index in operator table
Definition: Yatom.h:214
struct operator_entry * OpForME
index in module table
Definition: Yatom.h:213
Atom OwnerFile
module's name
Definition: Yatom.h:215
struct pred_entry * PredForME
kind of property
Definition: Yatom.h:212
PropFlags KindOfPE
chain of atom properties
Definition: Yatom.h:211
unsigned int flags
module's owner file
Definition: Yatom.h:219
Definition: Yatom.h:1020
Definition: Yatom.h:295
Definition: Yatom.h:544
all we need to know about an error/throw
Definition: YapError.h:205
Definition: arrays.h:76
Definition: tab.structs.h:22
Definition: Yatom.h:954
Definition: amidefs.h:264