1 
2 
3 
4 
5 
6 
7 
8 /*************************************************************************
9 *									 *
10 *	 YAP Prolog   %W% %G%
11 *									 *
12 *	Yap Prolog was developed at NCCUP - Universidade do Porto	 *
13 *									 *
14 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 *
15 *									 *
16 **************************************************************************
17 *									 *
18 * File:		YAtom.h.m4						 *
19 * Last rev:	19/2/88							 *
20 * mods:									 *
21 * comments:	atom properties header file for YAP			 *
22 *									 *
23 *************************************************************************/
24 
25 /* This code can only be defined *after* including Regs.h!!! */
26 
27 #if USE_OFFSETS
28 
29 inline EXTERN Atom AbsAtom(AtomEntry * p);
30 
AbsAtom(AtomEntry * p)31 inline EXTERN Atom AbsAtom(AtomEntry * p)
32 {
33 	return (Atom) (Addr(p) - AtomBase);
34 }
35 
36 
37 
38 inline EXTERN AtomEntry * RepAtom(Atom a);
39 
RepAtom(Atom a)40 inline EXTERN AtomEntry * RepAtom(Atom a)
41 {
42 	return (AtomEntry *) (AtomBase + Unsigned(a));
43 }
44 
45 
46 #else
47 
48 inline EXTERN Atom AbsAtom(AtomEntry * p);
49 
AbsAtom(AtomEntry * p)50 inline EXTERN Atom AbsAtom(AtomEntry * p)
51 {
52 	return (Atom) (p);
53 }
54 
55 
56 
57 inline EXTERN AtomEntry * RepAtom(Atom a);
58 
RepAtom(Atom a)59 inline EXTERN AtomEntry * RepAtom(Atom a)
60 {
61 	return (AtomEntry *) (a);
62 }
63 
64 
65 #endif
66 
67 #if USE_OFFSETS_IN_PROPS
68 
69 inline EXTERN Prop AbsProp(PropEntry * p);
70 
AbsProp(PropEntry * p)71 inline EXTERN Prop AbsProp(PropEntry * p)
72 {
73 	return (Prop) (Addr(p)-AtomBase);
74 }
75 
76 
77 
78 inline EXTERN PropEntry * RepProp(Prop p);
79 
RepProp(Prop p)80 inline EXTERN PropEntry * RepProp(Prop p)
81 {
82 	return (PropEntry *) (AtomBase+Unsigned(p));
83 }
84 
85 
86 #else
87 
88 inline EXTERN Prop AbsProp(PropEntry * p);
89 
AbsProp(PropEntry * p)90 inline EXTERN Prop AbsProp(PropEntry * p)
91 {
92 	return (Prop) (p);
93 }
94 
95 
96 
97 inline EXTERN PropEntry * RepProp(Prop p);
98 
RepProp(Prop p)99 inline EXTERN PropEntry * RepProp(Prop p)
100 {
101 	return (PropEntry *) (p);
102 }
103 
104 
105 #endif
106 
107 #if USE_OFFSETS_IN_PROPS
108 
109 inline EXTERN FunctorEntry * RepFunctorProp(Prop p);
110 
RepFunctorProp(Prop p)111 inline EXTERN FunctorEntry * RepFunctorProp(Prop p)
112 {
113 	return (FunctorEntry *) (AtomBase + Unsigned(p));
114 }
115 
116 
117 
118 inline EXTERN Prop AbsFunctorProp(FunctorEntry * p);
119 
AbsFunctorProp(FunctorEntry * p)120 inline EXTERN Prop AbsFunctorProp(FunctorEntry * p)
121 {
122 	return (Prop) (Addr(p)-AtomBase);
123 }
124 
125 
126 #else
127 
128 inline EXTERN FunctorEntry * RepFunctorProp(Prop p);
129 
RepFunctorProp(Prop p)130 inline EXTERN FunctorEntry * RepFunctorProp(Prop p)
131 {
132 	return (FunctorEntry *) (p);
133 }
134 
135 
136 
137 inline EXTERN Prop AbsFunctorProp(FunctorEntry * p);
138 
AbsFunctorProp(FunctorEntry * p)139 inline EXTERN Prop AbsFunctorProp(FunctorEntry * p)
140 {
141 	return (Prop) (p);
142 }
143 
144 
145 #endif
146 
147 
148 inline EXTERN Int ArityOfFunctor(Functor);
149 
ArityOfFunctor(Functor Fun)150 inline EXTERN Int ArityOfFunctor(Functor Fun)
151 {
152 	return (Int) (((FunctorEntry *)Fun)->ArityOfFE);
153 }
154 
155 
156 
157 inline EXTERN Atom NameOfFunctor(Functor);
158 
NameOfFunctor(Functor Fun)159 inline EXTERN Atom NameOfFunctor(Functor Fun)
160 {
161 	return (Atom) (((FunctorEntry *)Fun)->NameOfFE);
162 }
163 
164 
165 
166 
167 inline EXTERN PropFlags IsFunctorProperty(int);
168 
IsFunctorProperty(int flags)169 inline EXTERN PropFlags IsFunctorProperty(int flags)
170 {
171 	return (PropFlags) ((flags == FunctorProperty) );
172 }
173 
174 
175 
176 /* summary of property codes used
177 
178 	00 00	predicate entry
179 	80 00	db property
180 	bb 00	functor entry
181 	ff df	sparse functor
182 	ff ex	arithmetic property
183 	ff f7   array
184 	ff fa   module property
185 	ff fb   blackboard property
186 	ff fc	value property
187 	ff ff	op property
188 */
189 
190 /*	Module property 						*/
191 typedef struct {
192     Prop   	NextOfPE;	/* used	to chain properties		*/
193     PropFlags  	KindOfPE;	/* kind	of property			*/
194     SMALLUNSGN  IndexOfMod;     /* indec in module table		*/
195 } ModEntry;
196 
197 #if USE_OFFSETS_IN_PROPS
198 
199 inline EXTERN ModEntry * RepModProp(Prop p);
200 
RepModProp(Prop p)201 inline EXTERN ModEntry * RepModProp(Prop p)
202 {
203 	return (ModEntry *) (AtomBase + Unsigned(p));
204 }
205 
206 
207 
208 inline EXTERN Prop AbsModProp(ModEntry * p);
209 
AbsModProp(ModEntry * p)210 inline EXTERN Prop AbsModProp(ModEntry * p)
211 {
212 	return (Prop) (Addr(p)-AtomBase);
213 }
214 
215 
216 #else
217 
218 inline EXTERN ModEntry * RepModProp(Prop p);
219 
RepModProp(Prop p)220 inline EXTERN ModEntry * RepModProp(Prop p)
221 {
222 	return (ModEntry *) (p);
223 }
224 
225 
226 
227 inline EXTERN Prop AbsModProp(ModEntry * p);
228 
AbsModProp(ModEntry * p)229 inline EXTERN Prop AbsModProp(ModEntry * p)
230 {
231 	return (Prop) (p);
232 }
233 
234 
235 #endif
236 
237 #define ModProperty	((PropFlags)0xfffa)
238 
239 
240 inline EXTERN PropFlags IsModProperty(int);
241 
IsModProperty(int flags)242 inline EXTERN PropFlags IsModProperty(int flags)
243 {
244 	return (PropFlags) ((flags == ModProperty));
245 }
246 
247 
248 
249 /*	    operator property entry structure				*/
250 typedef	struct {
251     Prop	NextOfPE;		/* used	to chain properties	*/
252     PropFlags   KindOfPE;		/* kind	of property		*/
253 #if defined(YAPOR) || defined(THREADS)
254     rwlock_t OpRWLock;            /* a read-write lock to protect the entry */
255 #endif
256     BITS16  Prefix, Infix, Posfix;      /* precedences			*/
257     } OpEntry;
258 #if USE_OFFSETS_IN_PROPS
259 
260 inline EXTERN OpEntry * RepOpProp(Prop p);
261 
RepOpProp(Prop p)262 inline EXTERN OpEntry * RepOpProp(Prop p)
263 {
264 	return (OpEntry *) (AtomBase + Unsigned(p));
265 }
266 
267 
268 
269 inline EXTERN Prop AbsOpProp(OpEntry * p);
270 
AbsOpProp(OpEntry * p)271 inline EXTERN Prop AbsOpProp(OpEntry * p)
272 {
273 	return (Prop) (Addr(p)-AtomBase);
274 }
275 
276 
277 #else
278 
279 inline EXTERN OpEntry * RepOpProp(Prop p);
280 
RepOpProp(Prop p)281 inline EXTERN OpEntry * RepOpProp(Prop p)
282 {
283 	return (OpEntry *) (p);
284 }
285 
286 
287 
288 inline EXTERN Prop AbsOpProp(OpEntry * p);
289 
AbsOpProp(OpEntry * p)290 inline EXTERN Prop AbsOpProp(OpEntry * p)
291 {
292 	return (Prop) (p);
293 }
294 
295 
296 #endif
297 #define	OpProperty  ((PropFlags)0xffff)
298 
299 
300 inline EXTERN PropFlags IsOpProperty(int);
301 
IsOpProperty(int flags)302 inline EXTERN PropFlags IsOpProperty(int flags)
303 {
304 	return (PropFlags) ((flags == OpProperty) );
305 }
306 
307 
308 
309 /* defines related to operator specifications				*/
310 #define	MaskPrio  0x0fff
311 #define	DcrlpFlag 0x1000
312 #define	DcrrpFlag 0x2000
313 
314 typedef union arith_ret *eval_ret;
315 
316 /*	    expression property	entry structure			*/
317 typedef	struct {
318   Prop	NextOfPE;	/* used	to chain properties		*/
319   PropFlags   KindOfPE;	/* kind	of property			*/
320   unsigned int ArityOfEE;
321   BITS16	ENoOfEE;
322   BITS16	FlagsOfEE;
323   /* operations that implement the expression */
324   union {
325     blob_type    (*constant)(eval_ret);
326     blob_type    (*unary)(Term, eval_ret);
327     blob_type    (*binary)(Term, Term, eval_ret);
328   } FOfEE;
329 } ExpEntry;
330 #if USE_OFFSETS_IN_PROPS
331 
332 inline EXTERN ExpEntry * RepExpProp(Prop p);
333 
RepExpProp(Prop p)334 inline EXTERN ExpEntry * RepExpProp(Prop p)
335 {
336 	return (ExpEntry *) (AtomBase + Unsigned(p));
337 }
338 
339 
340 
341 inline EXTERN Prop AbsExpProp(ExpEntry * p);
342 
AbsExpProp(ExpEntry * p)343 inline EXTERN Prop AbsExpProp(ExpEntry * p)
344 {
345 	return (Prop) (Addr(p)-AtomBase);
346 }
347 
348 
349 #else
350 
351 inline EXTERN ExpEntry * RepExpProp(Prop p);
352 
RepExpProp(Prop p)353 inline EXTERN ExpEntry * RepExpProp(Prop p)
354 {
355 	return (ExpEntry *) (p);
356 }
357 
358 
359 
360 inline EXTERN Prop AbsExpProp(ExpEntry * p);
361 
AbsExpProp(ExpEntry * p)362 inline EXTERN Prop AbsExpProp(ExpEntry * p)
363 {
364 	return (Prop) (p);
365 }
366 
367 
368 #endif
369 #define	ExpProperty  0xffe0
370 
371 /* only unary and binary expressions are acceptable */
372 
373 inline EXTERN PropFlags IsExpProperty(int);
374 
IsExpProperty(int flags)375 inline EXTERN PropFlags IsExpProperty(int flags)
376 {
377 	return (PropFlags) ((flags == ExpProperty) );
378 }
379 
380 
381 
382 
383 /*		value property entry structure				*/
384 typedef	struct {
385     Prop	NextOfPE;	/* used	to chain properties		*/
386     PropFlags   KindOfPE;	/* kind of property			*/
387 #if defined(YAPOR) || defined(THREADS)
388     rwlock_t VRWLock;            /* a read-write lock to protect the entry */
389 #endif
390     Term	ValueOfVE;	/* (atomic) value associated with the atom */
391     } ValEntry;
392 #if USE_OFFSETS_IN_PROPS
393 
394 inline EXTERN ValEntry * RepValProp(Prop p);
395 
RepValProp(Prop p)396 inline EXTERN ValEntry * RepValProp(Prop p)
397 {
398 	return (ValEntry *) (AtomBase + Unsigned(p));
399 }
400 
401 
402 
403 inline EXTERN Prop AbsValProp(ValEntry * p);
404 
AbsValProp(ValEntry * p)405 inline EXTERN Prop AbsValProp(ValEntry * p)
406 {
407 	return (Prop) (Addr(p)-AtomBase);
408 }
409 
410 
411 #else
412 
413 inline EXTERN ValEntry * RepValProp(Prop p);
414 
RepValProp(Prop p)415 inline EXTERN ValEntry * RepValProp(Prop p)
416 {
417 	return (ValEntry *) (p);
418 }
419 
420 
421 
422 inline EXTERN Prop AbsValProp(ValEntry * p);
423 
AbsValProp(ValEntry * p)424 inline EXTERN Prop AbsValProp(ValEntry * p)
425 {
426 	return (Prop) (p);
427 }
428 
429 
430 #endif
431 #define	ValProperty ((PropFlags)0xfffc)
432 
433 
434 inline EXTERN PropFlags IsValProperty(int);
435 
IsValProperty(int flags)436 inline EXTERN PropFlags IsValProperty(int flags)
437 {
438 	return (PropFlags) ((flags == ValProperty) );
439 }
440 
441 
442 
443 /*	    predicate property entry structure				*/
444 /*  AsmPreds are things like var, nonvar, atom ...which are implemented
445 	    through dedicated machine instructions. In this case the 8 lower
446 	    bits of PredFlags are used to hold the machine instruction code
447 	    for	the pred.
448     C_Preds are	things write, read, ...	implemented in C. In this case
449 	    CodeOfPred holds the address of the	correspondent C-function.
450 */
451 typedef enum {
452   CArgsPredFlag =    0x1000000L,	/* ! should ! across */
453   CutTransparentPredFlag =   0x800000L,	/* ! should ! across */
454   SourcePredFlag =   0x400000L,	/* static predicate with source declaration */
455   MetaPredFlag =     0x200000L,	/* predicate subject to a meta declaration */
456   SyncPredFlag =     0x100000L,	/* has to synch before it can execute*/
457   UserCPredFlag =    0x080000L,	/* CPred defined by the user	*/
458   MultiFileFlag =    0x040000L,	/* is multi-file		*/
459   FastPredFlag =     0x020000L,	/* is "compiled"		*/
460   TestPredFlag =     0x010000L,	/* is a test (optim. comit)	*/
461   AsmPredFlag =      0x008000L,	/* inline			*/
462   StandardPredFlag=  0x004000L,	/* system predicate		*/
463   DynamicPredFlag=   0x002000L,	/* dynamic predicate		*/
464   CPredFlag =        0x001000L,	/* written in C			*/
465   SafePredFlag =     0x000800L,	/* does not alter arguments	*/
466   CompiledPredFlag=  0x000400L,	/* is static			*/
467   IndexedPredFlag=   0x000200L,	/* has indexing code		*/
468   SpiedPredFlag =    0x000100L,	/* is a spy point		*/
469   BinaryTestPredFlag=0x000080L,	/* test predicate.              */
470 #ifdef TABLING
471   TabledPredFlag =   0x000040L,	/* is tabled			*/
472 #endif /* TABLING */
473 #ifdef YAPOR
474   SequentialPredFlag=0x000020L,	/* may not create par. choice points!*/
475 #endif /* YAPOR */
476   ProfiledPredFlag = 0x000010L, /* pred is being profiled	 */
477   LogUpdatePredFlag= 0x000008L	/* dynamic predicate with log. upd. sem.*/
478 } pred_flag;
479 
480 /* profile data */
481 typedef struct {
482 #if defined(YAPOR) || defined(THREADS)
483   lockvar lock;                 /* a simple lock to protect this entry */
484 #endif
485   Int	NOfEntries;		/* nbr of times head unification succeeded*/
486   Int   NOfHeadSuccesses;       /* nbr of times head unification succeeded*/
487   Int   NOfRetries;             /* nbr of times a clause for the pred
488 				   was retried */
489 } profile_data;
490 
491 typedef	struct pred_entry {
492   Prop	NextOfPE;	/* used to chain properties	    	*/
493   PropFlags	KindOfPE;	/* kind of property		    	*/
494   unsigned int  ArityOfPE;	/* arity of property		    	*/
495   CELL	        PredFlags;
496   CODEADDR	CodeOfPred;	/* code address		    		*/
497   CODEADDR	TrueCodeOfPred;	/* if needing to spy or to lock 	*/
498   Functor       FunctorOfPred;	/* functor for Predicate        	*/
499   CODEADDR	FirstClause, LastClause;
500   Atom	        OwnerFile;	/* File where the predicate was defined */
501   struct pred_entry *NextPredOfModule; /* next pred for same module   */
502 #if defined(YAPOR) || defined(THREADS)
503   rwlock_t      PRWLock;        /* a simple lock to protect this entry */
504 #endif
505 #ifdef TABLING
506   tab_ent_ptr   TableOfPred;
507 #endif /* TABLING */
508   SMALLUNSGN	ModuleOfPred;	/* module for this definition		*/
509   OPCODE        OpcodeOfPred;	/* undefcode, indexcode, spycode, ....  */
510   profile_data  StatisticsForPred; /* enable profiling for predicate  */
511   SMALLUNSGN	StateOfPred;	/* actual state of predicate 		*/
512 } PredEntry;
513 #define PEProp   ((PropFlags)(0x0000))
514 
515 #if USE_OFFSETS_IN_PROPS
516 
517 inline EXTERN PredEntry * RepPredProp(Prop p);
518 
RepPredProp(Prop p)519 inline EXTERN PredEntry * RepPredProp(Prop p)
520 {
521 	return (PredEntry *) (AtomBase + Unsigned(p));
522 }
523 
524 
525 
526 inline EXTERN Prop AbsPredProp(PredEntry * p);
527 
AbsPredProp(PredEntry * p)528 inline EXTERN Prop AbsPredProp(PredEntry * p)
529 {
530 	return (Prop) (Addr(p)-AtomBase);
531 }
532 
533 
534 #else
535 
536 inline EXTERN PredEntry * RepPredProp(Prop p);
537 
RepPredProp(Prop p)538 inline EXTERN PredEntry * RepPredProp(Prop p)
539 {
540 	return (PredEntry *) (p);
541 }
542 
543 
544 
545 inline EXTERN Prop AbsPredProp(PredEntry * p);
546 
AbsPredProp(PredEntry * p)547 inline EXTERN Prop AbsPredProp(PredEntry * p)
548 {
549 	return (Prop) (p);
550 }
551 
552 
553 #endif
554 
555 
556 inline EXTERN PropFlags IsPredProperty(int);
557 
IsPredProperty(int flags)558 inline EXTERN PropFlags IsPredProperty(int flags)
559 {
560 	return (PropFlags) ((flags == PEProp) );
561 }
562 
563 
564 
565 /********* maximum number of C-written predicates and cmp funcs ******************/
566 
567 #define MAX_C_PREDS    360
568 #define MAX_CMP_FUNCS   20
569 
570 typedef struct {
571 	PredEntry     *p;
572 	CmpPredicate   f;
573 } cmp_entry;
574 
575 extern CPredicate    c_predicates[MAX_C_PREDS];
576 extern cmp_entry     cmp_funcs[MAX_CMP_FUNCS];
577 
578 
579 /* Flags for code or dbase entry */
580 /* There are several flags for code and data base entries */
581 typedef enum {
582   GcFoundMask   = 0x10000, /* informs this is a dynamic predicate */
583   DynamicMask   =  0x8000, /* informs this is a dynamic predicate */
584   InUseMask     =  0x4000, /* informs this block is being used */
585   ErasedMask    =  0x2000, /* informs this block has been erased */
586   IndexMask     =  0x1000, /* informs this is indexing code */
587   DBClMask      =  0x0800, /* informs this is a data base structure */
588   LogUpdRuleMask=  0x0400, /* informs the code is for a log upd rule with env */
589   LogUpdMask    =  0x0200, /* informs this is a logic update index. */
590   StaticMask    =  0x0100, /* dealing with static predicates */
591   SpiedMask     =  0x0080  /* this predicate is being spied */
592 /* other flags belong to DB */
593 } dbentry_flags;
594 
595 /* *********************** DBrefs **************************************/
596 
597 #define KEEP_ENTRY_AGE 1
598 
599 typedef struct DB_STRUCT {
600   Functor id;		/* allow pointers to this struct to id  */
601 			/*   as dbref                           */
602   Term	EntryTerm;	/* cell bound to itself			*/
603   SMALLUNSGN Flags;	/* Term Flags				*/
604   SMALLUNSGN NOfRefsTo;	/* Number of references pointing here	*/
605   struct struct_dbentry  *Parent;	/* key of DBase reference		*/
606   CODEADDR Code;	/* pointer to code if this is a clause 	*/
607   struct DB_STRUCT **DBRefs; /* pointer to other references 	*/
608   struct DB_STRUCT *Prev; /* Previous element in chain            */
609   struct DB_STRUCT *Next; /* Next element in chain                */
610 #if defined(YAPOR) || defined(THREADS)
611   lockvar   lock;         /* a simple lock to protect this entry */
612   Int       ref_count;    /* how many branches are using this entry */
613 #endif
614 #ifdef KEEP_ENTRY_AGE
615   Int age;                /* entry's age, negative if from recorda,
616 			     positive if it was recordz  */
617 #endif /* KEEP_ENTRY_AGE */
618 #ifdef COROUTINING
619   CELL    attachments;   /* attached terms */
620 #endif
621   CELL Mask;		/* parts that should be cleared		*/
622   CELL Key;		/* A mask that can be used to check before
623 			   you unify */
624   CELL NOfCells;	/* Size of Term				*/
625   CELL Entry;		/* entry point 				*/
626   Term Contents[MIN_ARRAY]; /* stored term	       		*/
627 } DBStruct;
628 
629 #define DBStructFlagsToDBStruct(X) ((DBRef)((X) - (CELL) &(((DBRef) NIL)->Flags)))
630 
631 #if defined(YAPOR) || defined(THREADS)
632 #define INIT_DBREF_COUNT(X) (X)->ref_count = 0
633 #define  INC_DBREF_COUNT(X) (X)->ref_count++
634 #define  DEC_DBREF_COUNT(X) (X)->ref_count--
635 #define     DBREF_IN_USE(X) ((X)->ref_count != 0)
636 #else
637 #define INIT_DBREF_COUNT(X)
638 #define  INC_DBREF_COUNT(X)
639 #define  DEC_DBREF_COUNT(X)
640 #define     DBREF_IN_USE(X) ((X)->Flags & InUseMask)
641 #endif
642 
643 typedef DBStruct *DBRef;
644 
645 /* extern Functor FunctorDBRef; */
646 
647 inline EXTERN int IsDBRefTerm(Term);
648 
IsDBRefTerm(Term t)649 inline EXTERN int IsDBRefTerm(Term t)
650 {
651 	return (int) (IsApplTerm(t) && FunctorOfTerm(t) == FunctorDBRef);
652 }
653 
654 
655 
656 inline EXTERN Term MkDBRefTerm(DBRef);
657 
MkDBRefTerm(DBRef p)658 inline EXTERN Term MkDBRefTerm(DBRef p)
659 {
660 	return (Term) ((AbsAppl(((CELL *)(p)))));
661 }
662 
663 
664 
665 inline EXTERN DBRef DBRefOfTerm(Term t);
666 
DBRefOfTerm(Term t)667 inline EXTERN DBRef DBRefOfTerm(Term t)
668 {
669 	return (DBRef) (((DBRef)(RepAppl(t))));
670 }
671 
672 
673 
674 
675 inline EXTERN int IsRefTerm(Term);
676 
IsRefTerm(Term t)677 inline EXTERN int IsRefTerm(Term t)
678 {
679 	return (int) (IsApplTerm(t) && FunctorOfTerm(t) == FunctorDBRef);
680 }
681 
682 
683 
684 inline EXTERN CODEADDR RefOfTerm(Term t);
685 
RefOfTerm(Term t)686 inline EXTERN CODEADDR RefOfTerm(Term t)
687 {
688 	return (CODEADDR) (DBRefOfTerm(t));
689 }
690 
691 
692 
693 typedef	struct struct_dbentry {
694   Prop	NextOfPE;	/* used	to chain properties		*/
695   PropFlags	KindOfPE;	/* kind of property			*/
696   unsigned int	ArityOfDB;	/* kind of property			*/
697   Functor	FunctorOfDB;	/* functor for this property		*/
698 #if defined(YAPOR) || defined(THREADS)
699   rwlock_t    DBRWLock;         /* a simple lock to protect this entry */
700 #endif
701   DBRef	First;		/* first DBase entry			*/
702   DBRef	Last;		/* last DBase entry			*/
703   SMALLUNSGN	ModuleOfDB;	/* module for this definition		*/
704 #ifdef KEEP_ENTRY_AGE
705   Int         age;		/* age counter                          */
706 #else
707   DBRef	FirstNEr;	/* first non-erased DBase entry		*/
708 #endif /* KEEP_ENTRY_AGE */
709 } DBEntry;
710 typedef DBEntry *DBProp;
711 #define	DBProperty	   ((PropFlags)0x8000)
712 
713 typedef	struct {
714   Prop	NextOfPE;	/* used	to chain properties		*/
715   PropFlags	KindOfPE;	/* kind of property			*/
716   unsigned int	ArityOfDB;	/* kind of property			*/
717   Functor	FunctorOfDB;	/* functor for this property		*/
718 #if defined(YAPOR) || defined(THREADS)
719   rwlock_t    DBRWLock;         /* a simple lock to protect this entry */
720 #endif
721   DBRef	First;		/* first DBase entry			*/
722   DBRef	Last;		/* last DBase entry			*/
723   SMALLUNSGN	ModuleOfDB;	/* module for this definition		*/
724   Int         NOfEntries;	/* age counter                          */
725   DBRef       Index;		/* age counter                          */
726 } LogUpdDBEntry;
727 typedef LogUpdDBEntry *LogUpdDBProp;
728 #define	LogUpdDBBit        0x1
729 #define	CodeDBBit          0x2
730 
731 #define	LogUpdDBProperty   ((PropFlags)(0x8000|LogUpdDBBit))
732 #define	CodeLogUpdDBProperty   (DBProperty|LogUpdDBBit|CodeDBBit)
733 #define	CodeDBProperty     (DBProperty|CodeDBBit)
734 
735 
736 inline EXTERN PropFlags IsDBProperty(int);
737 
IsDBProperty(int flags)738 inline EXTERN PropFlags IsDBProperty(int flags)
739 {
740 	return (PropFlags) (((flags & ~(LogUpdDBBit|CodeDBBit))  == DBProperty) );
741 }
742 
743 
744 
745 #if USE_OFFSETS_IN_PROPS
746 
747 inline EXTERN DBProp RepDBProp(Prop p);
748 
RepDBProp(Prop p)749 inline EXTERN DBProp RepDBProp(Prop p)
750 {
751 	return (DBProp) (AtomBase + Unsigned(p));
752 }
753 
754 
755 
756 inline EXTERN Prop AbsDBProp(DBProp p);
757 
AbsDBProp(DBProp p)758 inline EXTERN Prop AbsDBProp(DBProp p)
759 {
760 	return (Prop) (Addr(p)-AtomBase);
761 }
762 
763 
764 #else
765 
766 inline EXTERN DBProp RepDBProp(Prop p);
767 
RepDBProp(Prop p)768 inline EXTERN DBProp RepDBProp(Prop p)
769 {
770 	return (DBProp) (p);
771 }
772 
773 
774 
775 inline EXTERN Prop AbsDBProp(DBProp p);
776 
AbsDBProp(DBProp p)777 inline EXTERN Prop AbsDBProp(DBProp p)
778 {
779 	return (Prop) (p);
780 }
781 
782 
783 #endif
784 
785 
786 /* These are the actual flags for DataBase terms */
787 typedef enum {
788   DBAtomic   =  0x1,
789   DBVar      =  0x2,
790   DBNoVars   =  0x4,
791   DBComplex  =  0x8,
792   DBCode     = 0x10,
793   DBNoCode   = 0x20,
794   DBWithRefs = 0x40
795 } db_term_flags;
796 
797 #define MaxModules	256
798 
799 typedef	struct {
800   Prop	NextOfPE;	     /* used to chain properties		*/
801   PropFlags	KindOfPE;    /* kind of property			*/
802   Atom	KeyOfBB;	     /* functor for this property		*/
803   DBRef	Element;	     /* blackboard element			*/
804 #if defined(YAPOR) || defined(THREADS)
805   rwlock_t BBRWLock;            /* a read-write lock to protect the entry */
806 #endif
807   SMALLUNSGN	ModuleOfBB;   /* module for this definition		*/
808 } BlackBoardEntry;
809 typedef BlackBoardEntry *BBProp;
810 
811 #if USE_OFFSETS_IN_PROPS
812 
813 inline EXTERN BlackBoardEntry * RepBBProp(Prop p);
814 
RepBBProp(Prop p)815 inline EXTERN BlackBoardEntry * RepBBProp(Prop p)
816 {
817 	return (BlackBoardEntry *) (AtomBase + Unsigned(p));
818 }
819 
820 
821 
822 inline EXTERN Prop AbsBBProp(BlackBoardEntry * p);
823 
AbsBBProp(BlackBoardEntry * p)824 inline EXTERN Prop AbsBBProp(BlackBoardEntry * p)
825 {
826 	return (Prop) (Addr(p)-AtomBase);
827 }
828 
829 
830 #else
831 
832 inline EXTERN BlackBoardEntry * RepBBProp(Prop p);
833 
RepBBProp(Prop p)834 inline EXTERN BlackBoardEntry * RepBBProp(Prop p)
835 {
836 	return (BlackBoardEntry *) (p);
837 }
838 
839 
840 
841 inline EXTERN Prop AbsBBProp(BlackBoardEntry * p);
842 
AbsBBProp(BlackBoardEntry * p)843 inline EXTERN Prop AbsBBProp(BlackBoardEntry * p)
844 {
845 	return (Prop) (p);
846 }
847 
848 
849 #endif
850 
851 #define BBProperty	((PropFlags)0xfffb)
852 
853 
854 inline EXTERN PropFlags IsBBProperty(int);
855 
IsBBProperty(int flags)856 inline EXTERN PropFlags IsBBProperty(int flags)
857 {
858 	return (PropFlags) ((flags == BBProperty));
859 }
860 
861 
862 
863 
864 /*		array property entry structure				*/
865 /*		first case is for dynamic arrays */
866 typedef	struct array_entry {
867   Prop	NextOfPE;	/* used	to chain properties		*/
868   PropFlags   KindOfPE;	/* kind of property			*/
869   Int		ArrayEArity;	/* Arity of Array (positive)		*/
870 #if defined(YAPOR) || defined(THREADS)
871   rwlock_t ArRWLock;            /* a read-write lock to protect the entry */
872 #endif
873   struct array_entry  *NextArrayE; /* Pointer to the actual array		*/
874  Term  ValueOfVE;		/* Pointer to the actual array		*/
875 } ArrayEntry;
876 
877 /* second case is for static arrays */
878 
879 /* first, the valid types */
880 typedef enum {
881   array_of_ints,
882   array_of_chars,
883   array_of_uchars,
884   array_of_doubles,
885   array_of_ptrs,
886   array_of_atoms,
887   array_of_dbrefs,
888   array_of_terms
889 } static_array_types;
890 
891 typedef union  {
892   Int *ints;
893   char *chars;
894   unsigned char *uchars;
895   Float *floats;
896   AtomEntry **ptrs;
897   Term   *atoms;
898   Term  *dbrefs;
899   DBRef   *terms;
900 } statarray_elements;
901 
902 /* next, the actual data structure */
903 typedef	struct {
904   Prop	NextOfPE;	/* used	to chain properties		*/
905   PropFlags   KindOfPE;	/* kind of property			*/
906   Int		ArrayEArity;	/* Arity of Array (negative)		*/
907 #if defined(YAPOR) || defined(THREADS)
908   rwlock_t ArRWLock;            /* a read-write lock to protect the entry */
909 #endif
910   static_array_types		ArrayType;	/* Type of Array Elements.		*/
911   statarray_elements      ValueOfVE;	/* Pointer to the Array itself  */
912 } StaticArrayEntry;
913 
914 
915 #if USE_OFFSETS_IN_PROPS
916 
917 inline EXTERN ArrayEntry * RepArrayProp(Prop p);
918 
RepArrayProp(Prop p)919 inline EXTERN ArrayEntry * RepArrayProp(Prop p)
920 {
921 	return (ArrayEntry *) (AtomBase + Unsigned(p));
922 }
923 
924 
925 
926 inline EXTERN Prop AbsArrayProp(ArrayEntry * p);
927 
AbsArrayProp(ArrayEntry * p)928 inline EXTERN Prop AbsArrayProp(ArrayEntry * p)
929 {
930 	return (Prop) (Addr(p)-AtomBase);
931 }
932 
933 
934 
935 inline EXTERN StaticArrayEntry * RepStaticArrayProp(Prop p);
936 
RepStaticArrayProp(Prop p)937 inline EXTERN StaticArrayEntry * RepStaticArrayProp(Prop p)
938 {
939 	return (StaticArrayEntry *) (AtomBase + Unsigned(p));
940 }
941 
942 
943 
944 inline EXTERN Prop AbsStaticArrayProp(StaticArrayEntry * p);
945 
AbsStaticArrayProp(StaticArrayEntry * p)946 inline EXTERN Prop AbsStaticArrayProp(StaticArrayEntry * p)
947 {
948 	return (Prop) (Addr(p)-AtomBase);
949 }
950 
951 
952 #else
953 
954 inline EXTERN ArrayEntry * RepArrayProp(Prop p);
955 
RepArrayProp(Prop p)956 inline EXTERN ArrayEntry * RepArrayProp(Prop p)
957 {
958 	return (ArrayEntry *) (p);
959 }
960 
961 
962 
963 inline EXTERN Prop AbsArrayProp(ArrayEntry * p);
964 
AbsArrayProp(ArrayEntry * p)965 inline EXTERN Prop AbsArrayProp(ArrayEntry * p)
966 {
967 	return (Prop) (p);
968 }
969 
970 
971 
972 inline EXTERN StaticArrayEntry * RepStaticArrayProp(Prop p);
973 
RepStaticArrayProp(Prop p)974 inline EXTERN StaticArrayEntry * RepStaticArrayProp(Prop p)
975 {
976 	return (StaticArrayEntry *) (p);
977 }
978 
979 
980 
981 inline EXTERN Prop AbsStaticArrayProp(StaticArrayEntry * p);
982 
AbsStaticArrayProp(StaticArrayEntry * p)983 inline EXTERN Prop AbsStaticArrayProp(StaticArrayEntry * p)
984 {
985 	return (Prop) (p);
986 }
987 
988 
989 #endif
990 #define	ArrayProperty ((PropFlags)0xfff7)
991 
992 
993 inline EXTERN int ArrayIsDynamic(ArrayEntry *);
994 
ArrayIsDynamic(ArrayEntry * are)995 inline EXTERN int ArrayIsDynamic(ArrayEntry * are)
996 {
997 	return (int) (((are)->ArrayEArity > 0 ));
998 }
999 
1000 
1001 
1002 
1003 inline EXTERN PropFlags IsArrayProperty(int);
1004 
IsArrayProperty(int flags)1005 inline EXTERN PropFlags IsArrayProperty(int flags)
1006 {
1007 	return (PropFlags) ((flags == ArrayProperty) );
1008 }
1009 
1010 
1011 
1012 /* Proto types */
1013 
1014 /* cdmgr.c */
1015 int		STD_PROTO(RemoveIndexation,(PredEntry *));
1016 
1017 /* dbase.c */
1018 void		STD_PROTO(ErDBE,(DBRef));
1019 DBRef		STD_PROTO(StoreTermInDB,(int,int));
1020 Term		STD_PROTO(FetchTermFromDB,(DBRef,int));
1021 void		STD_PROTO(ReleaseTermFromDB,(DBRef));
1022 
1023 /* .c */
1024 CODEADDR	STD_PROTO(PredIsIndexable,(PredEntry *));
1025 
1026 /* init.c */
1027 Atom		STD_PROTO(GetOp,(OpEntry *,int *,int));
1028 
1029 /* vsc: redefined to GetAProp to avoid conflicts with Windows header files */
1030 Prop	STD_PROTO(GetAProp,(Atom,PropFlags));
1031 Prop	STD_PROTO(GetAPropHavingLock,(AtomEntry *,PropFlags));
1032 
1033 EXTERN inline Prop
PredPropByFunc(Functor f,SMALLUNSGN cur_mod)1034 PredPropByFunc(Functor f, SMALLUNSGN cur_mod)
1035 /* get predicate entry for ap/arity; create it if neccessary.              */
1036 {
1037   Prop p0;
1038   FunctorEntry *fe = (FunctorEntry *)f;
1039 
1040   WRITE_LOCK(fe->FRWLock);
1041   p0 = fe->PropsOfFE;
1042   while (p0) {
1043     PredEntry *p = RepPredProp(p0);
1044     if (/* p->KindOfPE != 0 || only props */
1045 	(p->ModuleOfPred == cur_mod || !(p->ModuleOfPred))) {
1046       WRITE_UNLOCK(f->FRWLock);
1047       return (p0);
1048     }
1049     p0 = p->NextOfPE;
1050   }
1051   return(NewPredPropByFunctor(fe,cur_mod));
1052 }
1053 
1054 EXTERN inline Prop
PredPropByAtom(Atom at,SMALLUNSGN cur_mod)1055 PredPropByAtom(Atom at, SMALLUNSGN cur_mod)
1056 /* get predicate entry for ap/arity; create it if neccessary.              */
1057 {
1058   Prop p0;
1059   AtomEntry *ae = RepAtom(at);
1060 
1061   WRITE_LOCK(ae->ARWLock);
1062   p0 = ae->PropsOfAE;
1063   while (p0) {
1064     PredEntry *pe = RepPredProp(p0);
1065     if ( pe->KindOfPE == PEProp &&
1066 	 (pe->ModuleOfPred == cur_mod || !pe->ModuleOfPred)) {
1067       WRITE_UNLOCK(ae->ARWLock);
1068       return(p0);
1069     }
1070     p0 = pe->NextOfPE;
1071   }
1072   return(NewPredPropByAtom(ae,cur_mod));
1073 }
1074 
1075 #if defined(YAPOR) || defined(THREADS)
1076 void    STD_PROTO(ReleasePreAllocCodeSpace, (ADDR));
1077 #else
1078 #define ReleasePreAllocCodeSpace(x)
1079 #endif
1080