File: Yatom.h

package info (click to toggle)
yap 5.1.3-6
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 19,796 kB
  • sloc: ansic: 146,224; perl: 53,318; java: 2,638; sh: 2,578; makefile: 2,040; tcl: 352; python: 101; awk: 9; cpp: 6
file content (1489 lines) | stat: -rw-r--r-- 31,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
/*************************************************************************
*									 *
*	 YAP Prolog   %W% %G%
*									 *
*	Yap Prolog was developed at NCCUP - Universidade do Porto	 *
*									 *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 *
*									 *
**************************************************************************
*									 *
* File:		YAtom.h.m4						 *
* Last rev:	19/2/88							 *
* mods:									 *
* comments:	atom properties header file for YAP			 *
*									 *
*************************************************************************/

/* This code can only be defined *after* including Regs.h!!! */

#ifdef USE_OFFSETS

inline EXTERN Atom AbsAtom (AtomEntry * p);

inline EXTERN Atom
AbsAtom (AtomEntry * p)
{
  return (Atom) (Addr (p) - AtomBase);
}



inline EXTERN AtomEntry *RepAtom (Atom a);

inline EXTERN AtomEntry *
RepAtom (Atom a)
{
  return (AtomEntry *) (AtomBase + Unsigned (a));
}


#else

inline EXTERN Atom AbsAtom (AtomEntry * p);

inline EXTERN Atom
AbsAtom (AtomEntry * p)
{
  return (Atom) (p);
}



inline EXTERN AtomEntry *RepAtom (Atom a);

inline EXTERN AtomEntry *
RepAtom (Atom a)
{
  return (AtomEntry *) (a);
}


#endif

#if USE_OFFSETS_IN_PROPS

inline EXTERN Prop AbsProp (PropEntry * p);

inline EXTERN Prop
AbsProp (PropEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}



inline EXTERN PropEntry *RepProp (Prop p);

inline EXTERN PropEntry *
RepProp (Prop p)
{
  return (PropEntry *) (AtomBase + Unsigned (p));
}


#else

inline EXTERN Prop AbsProp (PropEntry * p);

inline EXTERN Prop
AbsProp (PropEntry * p)
{
  return (Prop) (p);
}



inline EXTERN PropEntry *RepProp (Prop p);

inline EXTERN PropEntry *
RepProp (Prop p)
{
  return (PropEntry *) (p);
}


#endif

#if USE_OFFSETS_IN_PROPS

inline EXTERN FunctorEntry *RepFunctorProp (Prop p);

inline EXTERN FunctorEntry *
RepFunctorProp (Prop p)
{
  return (FunctorEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsFunctorProp (FunctorEntry * p);

inline EXTERN Prop
AbsFunctorProp (FunctorEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN FunctorEntry *RepFunctorProp (Prop p);

inline EXTERN FunctorEntry *
RepFunctorProp (Prop p)
{
  return (FunctorEntry *) (p);
}



inline EXTERN Prop AbsFunctorProp (FunctorEntry * p);

inline EXTERN Prop
AbsFunctorProp (FunctorEntry * p)
{
  return (Prop) (p);
}


#endif


inline EXTERN Int ArityOfFunctor (Functor);

inline EXTERN Int
ArityOfFunctor (Functor Fun)
{
  return (Int) (((FunctorEntry *) Fun)->ArityOfFE);
}



inline EXTERN Atom NameOfFunctor (Functor);

inline EXTERN Atom
NameOfFunctor (Functor Fun)
{
  return (Atom) (((FunctorEntry *) Fun)->NameOfFE);
}




inline EXTERN PropFlags IsFunctorProperty (int);

inline EXTERN PropFlags
IsFunctorProperty (int flags)
{
  return (PropFlags) ((flags == FunctorProperty));
}



/* summary of property codes used

	00 00	predicate entry
	80 00	db property
	bb 00	functor entry 
	ff df	sparse functor
	ff ex	arithmetic property
	ff f6   hold
	ff f7   array
	ff f8   wide atom
	ff fa   module property
	ff fb   blackboard property
	ff fc	value property
	ff fd	global property
	ff ff	op property
*/


/*	Global Variable property						*/
typedef struct global_entry
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t GRWLock;		/* a simple lock to protect this entry */
#if THREADS
  unsigned int owner_id;        /* owner thread */
#endif
#endif
  struct AtomEntryStruct *AtomOfGE; /* parent atom for deletion */
  struct global_entry *NextGE; /* linked list of global entries */
  Term  global;	/* index in module table                */
} GlobalEntry;


#if USE_OFFSETS_IN_PROPS

inline EXTERN GlobalEntry *RepGlobalProp (Prop p);

inline EXTERN GlobalEntry *
RepGlobalProp (Prop p)
{
  return (GlobalEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsGlobalProp (GlobalEntry * p);

inline EXTERN Prop
AbsGlobalProp (GlobalEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN GlobalEntry *RepGlobalProp (Prop p);

inline EXTERN GlobalEntry *
RepGlobalProp (Prop p)
{
  return (GlobalEntry *) (p);
}

inline EXTERN Prop AbsGlobalProp (GlobalEntry * p);

inline EXTERN Prop
AbsGlobalProp (GlobalEntry * p)
{
  return (Prop) (p);
}


#endif

#define GlobalProperty	((PropFlags)0xfffd)

inline EXTERN PropFlags IsGlobalProperty (int);

inline EXTERN PropFlags
IsGlobalProperty (int flags)
{
  return (PropFlags) ((flags == GlobalProperty));
}


/*	Wide Atom property 						*/
typedef struct
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
  UInt  SizeOfAtom;	        /* index in module table                */
} WideAtomEntry;

#if USE_OFFSETS_IN_PROPS

inline EXTERN WideAtomEntry *RepWideAtomProp (Prop p);

inline EXTERN WideAtomEntry *
RepWideAtomProp (Prop p)
{
  return (WideAtomEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsWideAtomProp (WideAtomEntry * p);

inline EXTERN Prop
AbsWideAtomProp (WideAtomEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN WideAtomEntry *RepWideAtomProp (Prop p);

inline EXTERN WideAtomEntry *
RepWideAtomProp (Prop p)
{
  return (WideAtomEntry *) (p);
}



inline EXTERN Prop AbsWideAtomProp (WideAtomEntry * p);

inline EXTERN Prop
AbsWideAtomProp (WideAtomEntry * p)
{
  return (Prop) (p);
}


#endif

#define WideAtomProperty	((PropFlags)0xfff8)


inline EXTERN PropFlags IsWideAtomProperty (int);

inline EXTERN PropFlags
IsWideAtomProperty (int flags)
{
  return (PropFlags) ((flags == WideAtomProperty));
}

inline EXTERN int IsWideAtom (Atom);

inline EXTERN int
IsWideAtom (Atom at)
{
  return RepAtom(at)->PropsOfAE &&
    IsWideAtomProperty(RepWideAtomProp(RepAtom(at)->PropsOfAE)->KindOfPE);
}


/*	Module property 						*/
typedef struct mod_entry
{
  Prop NextOfPE;		 /* used to chain properties            */
  PropFlags KindOfPE;		 /* kind of property                    */
  struct pred_entry *PredForME;  /* index in module table               */
  Atom   AtomOfME;		 /* module's name	                */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t ModRWLock;		/* a read-write lock to protect the entry */
#endif
  struct  mod_entry *NextME;   /* next module                         */
} ModEntry;

#if USE_OFFSETS_IN_PROPS

inline EXTERN ModEntry *RepModProp (Prop p);

inline EXTERN ModEntry *
RepModProp (Prop p)
{
  return (ModEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsModProp (ModEntry * p);

inline EXTERN Prop
AbsModProp (ModEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN ModEntry *RepModProp (Prop p);

inline EXTERN ModEntry *
RepModProp (Prop p)
{
  return (ModEntry *) (p);
}



inline EXTERN Prop AbsModProp (ModEntry * p);

inline EXTERN Prop
AbsModProp (ModEntry * p)
{
  return (Prop) (p);
}


#endif

#define ModProperty	((PropFlags)0xfffa)


inline EXTERN PropFlags IsModProperty (int);

inline EXTERN PropFlags
IsModProperty (int flags)
{
  return (PropFlags) ((flags == ModProperty));
}



/*	    operator property entry structure				*/
typedef struct operator_entry
{
  Prop NextOfPE;		/* used to chain properties     */
  PropFlags KindOfPE;		/* kind of property             */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t OpRWLock;		/* a read-write lock to protect the entry */
#endif
  Atom OpName;			/* atom name		        */
  Term OpModule;		/* module of predicate          */
  struct operator_entry  *OpNext; /* next in list of operators  */
  BITS16 Prefix, Infix, Posfix;	/* precedences                  */
} OpEntry;
#if USE_OFFSETS_IN_PROPS

inline EXTERN OpEntry *RepOpProp (Prop p);

inline EXTERN OpEntry *
RepOpProp (Prop p)
{
  return (OpEntry *) (AtomBase + Unsigned (p));
}

inline EXTERN Prop AbsOpProp (OpEntry * p);

inline EXTERN Prop
AbsOpProp (OpEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN OpEntry *RepOpProp (Prop p);

inline EXTERN OpEntry *
RepOpProp (Prop p)
{
  return (OpEntry *) (p);
}



inline EXTERN Prop AbsOpProp (OpEntry * p);

inline EXTERN Prop
AbsOpProp (OpEntry * p)
{
  return (Prop) (p);
}


#endif
#define	OpProperty  ((PropFlags)0xffff)


inline EXTERN PropFlags IsOpProperty (int);

inline EXTERN PropFlags
IsOpProperty (int flags)
{
  return (PropFlags) ((flags == OpProperty));
}

OpEntry   *STD_PROTO(Yap_GetOpProp,(Atom));

int	STD_PROTO(Yap_IsPrefixOp,(OpEntry *,int *,int *));
int	STD_PROTO(Yap_IsInfixOp,(OpEntry *,int *,int *,int *));
int	STD_PROTO(Yap_IsPosfixOp,(OpEntry *,int *,int *));

/* defines related to operator specifications				*/
#define	MaskPrio  0x0fff
#define	DcrlpFlag 0x1000
#define	DcrrpFlag 0x2000

typedef union arith_ret *eval_ret;

/*	    expression property	entry structure			*/
typedef struct
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
  unsigned int ArityOfEE;
  BITS16 ENoOfEE;
  BITS16 FlagsOfEE;
  /* operations that implement the expression */
  union
  {
    blob_type (*constant) (eval_ret);
    blob_type (*unary) (Term, eval_ret);
    blob_type (*binary) (Term, Term, eval_ret);
  } FOfEE;
} ExpEntry;
#if USE_OFFSETS_IN_PROPS

inline EXTERN ExpEntry *RepExpProp (Prop p);

inline EXTERN ExpEntry *
RepExpProp (Prop p)
{
  return (ExpEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsExpProp (ExpEntry * p);

inline EXTERN Prop
AbsExpProp (ExpEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN ExpEntry *RepExpProp (Prop p);

inline EXTERN ExpEntry *
RepExpProp (Prop p)
{
  return (ExpEntry *) (p);
}



inline EXTERN Prop AbsExpProp (ExpEntry * p);

inline EXTERN Prop
AbsExpProp (ExpEntry * p)
{
  return (Prop) (p);
}


#endif
#define	ExpProperty  0xffe0

/* only unary and binary expressions are acceptable */

inline EXTERN PropFlags IsExpProperty (int);

inline EXTERN PropFlags
IsExpProperty (int flags)
{
  return (PropFlags) ((flags == ExpProperty));
}




/*		value property entry structure				*/
typedef struct
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t VRWLock;		/* a read-write lock to protect the entry */
#endif
  Term ValueOfVE;		/* (atomic) value associated with the atom */
} ValEntry;
#if USE_OFFSETS_IN_PROPS

inline EXTERN ValEntry *RepValProp (Prop p);

inline EXTERN ValEntry *
RepValProp (Prop p)
{
  return (ValEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsValProp (ValEntry * p);

inline EXTERN Prop
AbsValProp (ValEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN ValEntry *RepValProp (Prop p);

inline EXTERN ValEntry *
RepValProp (Prop p)
{
  return (ValEntry *) (p);
}



inline EXTERN Prop AbsValProp (ValEntry * p);

inline EXTERN Prop
AbsValProp (ValEntry * p)
{
  return (Prop) (p);
}


#endif
#define	ValProperty ((PropFlags)0xfffc)


inline EXTERN PropFlags IsValProperty (int);

inline EXTERN PropFlags
IsValProperty (int flags)
{
  return (PropFlags) ((flags == ValProperty));
}



/*	    predicate property entry structure				*/
/*  AsmPreds are things like var, nonvar, atom ...which are implemented
	    through dedicated machine instructions. In this case the 8 lower
	    bits of PredFlags are used to hold the machine instruction code
	    for	the pred.
    C_Preds are	things write, read, ...	implemented in C. In this case
	    CodeOfPred holds the address of the	correspondent C-function.
*/
typedef enum
{
  MegaClausePredFlag = 0x80000000L,	/* predicate is implemented as a mega-clause */
  ThreadLocalPredFlag = 0x40000000L,	/* local to a thread */
  MultiFileFlag = 0x20000000L,	/* is multi-file */
  UserCPredFlag = 0x10000000L,	/* CPred defined by the user */
  LogUpdatePredFlag = 0x08000000L,	/* dynamic predicate with log. upd. sem. */
  InUsePredFlag = 0x04000000L,	/* count calls to pred */
  CountPredFlag = 0x02000000L,	/* count calls to pred */
  HiddenPredFlag = 0x01000000L,	/* invisible predicate */
  CArgsPredFlag = 0x00800000L,	/* SWI-like C-interface pred. */
  SourcePredFlag = 0x00400000L,	/* static predicate with source declaration */
  MetaPredFlag = 0x00200000L,	/* predicate subject to a meta declaration */
  SyncPredFlag = 0x00100000L,	/* has to synch before it can execute */
  NumberDBPredFlag = 0x00080000L,	/* entry for a number key */
  AtomDBPredFlag = 0x00040000L,	/* entry for an atom key */
  GoalExPredFlag = 0x00020000L,	/* predicate that is called by goal_expand */
  TestPredFlag = 0x00010000L,	/* is a test (optim. comit) */
  AsmPredFlag = 0x00008000L,	/* inline */
  StandardPredFlag = 0x00004000L,	/* system predicate */
  DynamicPredFlag = 0x00002000L,	/* dynamic predicate */
  CPredFlag = 0x00001000L,	/* written in C */
  SafePredFlag = 0x00000800L,	/* does not alter arguments */
  CompiledPredFlag = 0x00000400L,	/* is static */
  IndexedPredFlag = 0x00000200L,	/* has indexing code */
  SpiedPredFlag = 0x00000100L,	/* is a spy point */
  BinaryTestPredFlag = 0x00000080L,	/* test predicate */
  TabledPredFlag = 0x00000040L,	/* is tabled */
  SequentialPredFlag = 0x00000020L,	/* may not create parallel choice points! */
  ProfiledPredFlag = 0x00000010L,	/* pred is being profiled   */
  MyddasPredFlag = 0x00000008L,	/* Myddas Imported pred  */
  ModuleTransparentPredFlag = 0x00000004L,	/* ModuleTransparent pred  */
} pred_flag;

/* profile data */
typedef struct
{
  YAP_ULONG_LONG NOfEntries;	/* nbr of times head unification succeeded */
  YAP_ULONG_LONG NOfHeadSuccesses;	/* nbr of times head unification succeeded */
  YAP_ULONG_LONG NOfRetries;	/* nbr of times a clause for the pred
				   was retried */
#if defined(YAPOR) || defined(THREADS)
  lockvar lock;			/* a simple lock to protect this entry */
#endif
} profile_data;

typedef enum {
  LUCALL_EXEC,
  LUCALL_ASSERT,
  LUCALL_RETRACT
} timestamp_type;

#define TIMESTAMP_EOT   ((UInt)(~0L))
#define TIMESTAMP_RESET (TIMESTAMP_EOT-1024)

typedef struct pred_entry
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
  struct yami *CodeOfPred;
  OPCODE OpcodeOfPred;		/* undefcode, indexcode, spycode, ....  */
  CELL PredFlags;
  unsigned int ArityOfPE;	/* arity of property                    */
  union
  {
    struct
    {
      struct yami *TrueCodeOfPred;	/* code address                         */
      struct yami *FirstClause;
      struct yami *LastClause;
      UInt NOfClauses;
      OPCODE ExpandCode;
    } p_code;
    CPredicate f_code;
    CmpPredicate d_code;
  } cs;				/* if needing to spy or to lock         */
  Functor FunctorOfPred;	/* functor for Predicate                */
  union
  {
    Atom OwnerFile;		/* File where the predicate was defined */
    Int IndxId;			/* Index for a certain key */
    struct mfile *file_srcs;	/* for multifile predicates */
  } src;
#if defined(YAPOR) || defined(THREADS)
  lockvar PELock;		/* a simple lock to protect expansion */
#endif
#ifdef TABLING
  tab_ent_ptr TableOfPred;
#endif				/* TABLING */
#ifdef BEAM
  struct Predicates *beamTable;
#endif
  Term ModuleOfPred;		/* module for this definition           */
  UInt TimeStampOfPred;
  timestamp_type LastCallOfPred; 
  /* This must be at an odd number of cells, otherwise it
     will not be aligned on RISC machines */
  profile_data StatisticsForPred;	/* enable profiling for predicate  */
  struct pred_entry *NextPredOfModule;	/* next pred for same module   */
} PredEntry;
#define PEProp   ((PropFlags)(0x0000))

#if USE_OFFSETS_IN_PROPS

inline EXTERN PredEntry *RepPredProp (Prop p);

inline EXTERN PredEntry *
RepPredProp (Prop p)
{
  return (PredEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsPredProp (PredEntry * p);

inline EXTERN Prop
AbsPredProp (PredEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN PredEntry *RepPredProp (Prop p);

inline EXTERN PredEntry *
RepPredProp (Prop p)
{
  return (PredEntry *) (p);
}



inline EXTERN Prop AbsPredProp (PredEntry * p);

inline EXTERN Prop
AbsPredProp (PredEntry * p)
{
  return (Prop) (p);
}


#endif


inline EXTERN PropFlags IsPredProperty (int);

inline EXTERN PropFlags
IsPredProperty (int flags)
{
  return (PropFlags) ((flags == PEProp));
}



/* Flags for code or dbase entry */
/* There are several flags for code and data base entries */
typedef enum
{
  FuncSwitchMask = 0x800000,	/* is a switch of functors */
  HasDBTMask = 0x400000,	/* includes a pointer to a DBTerm */
  MegaMask = 0x200000,		/* mega clause */
  FactMask = 0x100000,		/* a fact */
  SwitchRootMask = 0x80000,	/* root for the index tree */
  SwitchTableMask = 0x40000,	/* switch table */
  HasBlobsMask = 0x20000,	/* blobs which may be in use */
  ProfFoundMask = 0x10000,	/* clause is being counted by profiler */
  DynamicMask = 0x8000,		/* dynamic predicate */
  InUseMask = 0x4000,		/* this block is being used */
  ErasedMask = 0x2000,		/* this block has been erased */
  IndexMask = 0x1000,		/* indexing code */
  DBClMask = 0x0800,		/* data base structure */
  LogUpdRuleMask = 0x0400,	/* code is for a log upd rule with env */
  LogUpdMask = 0x0200,		/* logic update index. */
  StaticMask = 0x0100,		/* static predicates */
  DirtyMask = 0x0080		/* LUIndices  */
/* other flags belong to DB */
} dbentry_flags;

/* *********************** DBrefs **************************************/

typedef struct DB_TERM
{
#ifdef COROUTINING
  union {
    CELL attachments;		/* attached terms */
    struct DB_TERM *NextDBT;
  } ag;
#endif
  struct DB_STRUCT **DBRefs;	/* pointer to other references     */
  CELL NOfCells;		/* Size of Term                         */
  CELL Entry;			/* entry point                          */
  Term Contents[MIN_ARRAY];	/* stored term                      */
} DBTerm;

inline EXTERN DBTerm *TermToDBTerm(Term);

inline EXTERN DBTerm *TermToDBTerm(Term X)
{
  if (IsPairTerm(X)) {
    return(DBTerm *)((char *)RepPair(X) - (CELL) &(((DBTerm *) NULL)->Contents));
  } else {
    return(DBTerm *)((char *)RepAppl(X) - (CELL) &(((DBTerm *) NULL)->Contents));
  }
}


/* The ordering of the first 3 fields should be compatible with lu_clauses */
typedef struct DB_STRUCT
{
  Functor id;			/* allow pointers to this struct to id  */
  /*   as dbref                           */
  CELL Flags;			/* Term Flags                           */
#if defined(YAPOR) || defined(THREADS)
  lockvar lock;			/* a simple lock to protect this entry */
  Int ref_count;		/* how many branches are using this entry */
#endif
  CELL NOfRefsTo;		/* Number of references pointing here   */
  struct struct_dbentry *Parent;	/* key of DBase reference               */
  struct yami *Code;		/* pointer to code if this is a clause  */
  struct DB_STRUCT *Prev;	/* Previous element in chain            */
  struct DB_STRUCT *Next;	/* Next element in chain                */
  struct DB_STRUCT *p, *n;	/* entry's age, negative if from recorda,
				   positive if it was recordz  */
  CELL Mask;			/* parts that should be cleared         */
  CELL Key;			/* A mask that can be used to check before
				   you unify */
  DBTerm DBT;
} DBStruct;

#define DBStructFlagsToDBStruct(X) ((DBRef)((char *)(X) - (CELL) &(((DBRef) NULL)->Flags)))

#if defined(YAPOR) || defined(THREADS)
#define INIT_DBREF_COUNT(X) (X)->ref_count = 0
#define  INC_DBREF_COUNT(X) (X)->ref_count++
#define  DEC_DBREF_COUNT(X) (X)->ref_count--
#define     DBREF_IN_USE(X) ((X)->ref_count != 0)
#else
#define INIT_DBREF_COUNT(X)
#define  INC_DBREF_COUNT(X)
#define  DEC_DBREF_COUNT(X)
#define     DBREF_IN_USE(X) ((X)->Flags & InUseMask)
#endif

typedef DBStruct *DBRef;

/* extern Functor FunctorDBRef; */

inline EXTERN int IsDBRefTerm (Term);

inline EXTERN int
IsDBRefTerm (Term t)
{
  return (int) (IsApplTerm (t) && FunctorOfTerm (t) == FunctorDBRef);
}



inline EXTERN Term MkDBRefTerm (DBRef);

inline EXTERN Term
MkDBRefTerm (DBRef p)
{
  return (Term) ((AbsAppl (((CELL *) (p)))));
}



inline EXTERN DBRef DBRefOfTerm (Term t);

inline EXTERN DBRef
DBRefOfTerm (Term t)
{
  return (DBRef) (((DBRef) (RepAppl (t))));
}




inline EXTERN int IsRefTerm (Term);

inline EXTERN int
IsRefTerm (Term t)
{
  return (int) (IsApplTerm (t) && FunctorOfTerm (t) == FunctorDBRef);
}



inline EXTERN CODEADDR RefOfTerm (Term t);

inline EXTERN CODEADDR
RefOfTerm (Term t)
{
  return (CODEADDR) (DBRefOfTerm (t));
}



typedef struct struct_dbentry
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
  unsigned int ArityOfDB;	/* kind of property                     */
  Functor FunctorOfDB;		/* functor for this property            */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t DBRWLock;		/* a simple lock to protect this entry */
#endif
  DBRef First;			/* first DBase entry                    */
  DBRef Last;			/* last DBase entry                     */
  Term ModuleOfDB;		/* module for this definition           */
  DBRef F0, L0;			/* everyone                          */
} DBEntry;
typedef DBEntry *DBProp;
#define	DBProperty	   ((PropFlags)0x8000)

typedef struct
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
  unsigned int ArityOfDB;	/* kind of property                     */
  Functor FunctorOfDB;		/* functor for this property            */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t DBRWLock;		/* a simple lock to protect this entry */
#endif
  DBRef First;			/* first DBase entry                    */
  DBRef Last;			/* last DBase entry                     */
  Term ModuleOfDB;		/* module for this definition           */
  Int NOfEntries;		/* age counter                          */
  DBRef Index;			/* age counter                          */
} LogUpdDBEntry;
typedef LogUpdDBEntry *LogUpdDBProp;
#define	CodeDBBit          0x2

#define	CodeDBProperty     (DBProperty|CodeDBBit)


inline EXTERN PropFlags IsDBProperty (int);

inline EXTERN PropFlags
IsDBProperty (int flags)
{
  return (PropFlags) ((flags & ~CodeDBBit) == DBProperty);
}



#if USE_OFFSETS_IN_PROPS

inline EXTERN DBProp RepDBProp (Prop p);

inline EXTERN DBProp
RepDBProp (Prop p)
{
  return (DBProp) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsDBProp (DBProp p);

inline EXTERN Prop
AbsDBProp (DBProp p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN DBProp RepDBProp (Prop p);

inline EXTERN DBProp
RepDBProp (Prop p)
{
  return (DBProp) (p);
}



inline EXTERN Prop AbsDBProp (DBProp p);

inline EXTERN Prop
AbsDBProp (DBProp p)
{
  return (Prop) (p);
}


#endif


/* These are the actual flags for DataBase terms */
typedef enum
{
  DBAtomic = 0x1,
  DBVar = 0x2,
  DBNoVars = 0x4,
  DBComplex = 0x8,
  DBCode = 0x10,
  DBNoCode = 0x20,
  DBWithRefs = 0x40
} db_term_flags;

typedef struct
{
  Prop NextOfPE;		/* used to chain properties                */
  PropFlags KindOfPE;		/* kind of property                        */
  Atom KeyOfBB;			/* functor for this property               */
  Term Element;			/* blackboard element                      */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t BBRWLock;		/* a read-write lock to protect the entry */
#endif
  Term ModuleOfBB;		/* module for this definition             */
} BlackBoardEntry;
typedef BlackBoardEntry *BBProp;

#if USE_OFFSETS_IN_PROPS

inline EXTERN BlackBoardEntry *RepBBProp (Prop p);

inline EXTERN BlackBoardEntry *
RepBBProp (Prop p)
{
  return (BlackBoardEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsBBProp (BlackBoardEntry * p);

inline EXTERN Prop
AbsBBProp (BlackBoardEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN BlackBoardEntry *RepBBProp (Prop p);

inline EXTERN BlackBoardEntry *
RepBBProp (Prop p)
{
  return (BlackBoardEntry *) (p);
}



inline EXTERN Prop AbsBBProp (BlackBoardEntry * p);

inline EXTERN Prop
AbsBBProp (BlackBoardEntry * p)
{
  return (Prop) (p);
}


#endif

#define BBProperty	((PropFlags)0xfffb)


inline EXTERN PropFlags IsBBProperty (int);

inline EXTERN PropFlags
IsBBProperty (int flags)
{
  return (PropFlags) ((flags == BBProperty));
}


/*		hold property entry structure				*/
typedef struct hold_entry
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
} HoldEntry;

#if USE_OFFSETS_IN_PROPS

inline EXTERN HoldEntry *RepHoldProp (Prop p);

inline EXTERN HoldEntry *
RepHoldProp (Prop p)
{
  return (HoldEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsHoldProp (HoldEntry * p);

inline EXTERN Prop
AbsHoldProp (HoldEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN HoldEntry *RepHoldProp (Prop p);

inline EXTERN HoldEntry *
RepHoldProp (Prop p)
{
  return (HoldEntry *) (p);
}



inline EXTERN Prop AbsHoldProp (HoldEntry * p);

inline EXTERN Prop
AbsHoldProp (HoldEntry * p)
{
  return (Prop) (p);
}


#endif
#define	HoldProperty  0xfff6

/* only unary and binary expressions are acceptable */

inline EXTERN PropFlags IsHoldProperty (int);

inline EXTERN PropFlags
IsHoldProperty (int flags)
{
  return (PropFlags) ((flags == HoldProperty));
}




/*		array property entry structure				*/
/*		first case is for dynamic arrays */
typedef struct array_entry
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
  Int ArrayEArity;		/* Arity of Array (positive)            */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t ArRWLock;		/* a read-write lock to protect the entry */
#if THREADS
  unsigned int owner_id;
#endif
#endif
  struct array_entry *NextAE;
  Term ValueOfVE;		/* Pointer to the actual array          */
} ArrayEntry;

/* second case is for static arrays */

/* first, the valid types */
typedef enum
{
  array_of_ints,
  array_of_chars,
  array_of_uchars,
  array_of_doubles,
  array_of_ptrs,
  array_of_atoms,
  array_of_dbrefs,
  array_of_nb_terms,
  array_of_terms
} static_array_types;

typedef  struct {
  Term tlive;
  Term tstore;
} live_term;


typedef union
{
  Int *ints;
  char *chars;
  unsigned char *uchars;
  Float *floats;
  AtomEntry **ptrs;
  Term *atoms;
  Term *dbrefs;
  DBTerm **terms;
  live_term *lterms;
} statarray_elements;

/* next, the actual data structure */
typedef struct static_array_entry
{
  Prop NextOfPE;		/* used to chain properties             */
  PropFlags KindOfPE;		/* kind of property                     */
  Int ArrayEArity;		/* Arity of Array (negative)            */
#if defined(YAPOR) || defined(THREADS)
  rwlock_t ArRWLock;		/* a read-write lock to protect the entry */
#endif
  struct static_array_entry *NextAE;
  static_array_types ArrayType;	/* Type of Array Elements.              */
  statarray_elements ValueOfVE;	/* Pointer to the Array itself  */
} StaticArrayEntry;


#if USE_OFFSETS_IN_PROPS

inline EXTERN ArrayEntry *RepArrayProp (Prop p);

inline EXTERN ArrayEntry *
RepArrayProp (Prop p)
{
  return (ArrayEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsArrayProp (ArrayEntry * p);

inline EXTERN Prop
AbsArrayProp (ArrayEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}



inline EXTERN StaticArrayEntry *RepStaticArrayProp (Prop p);

inline EXTERN StaticArrayEntry *
RepStaticArrayProp (Prop p)
{
  return (StaticArrayEntry *) (AtomBase + Unsigned (p));
}



inline EXTERN Prop AbsStaticArrayProp (StaticArrayEntry * p);

inline EXTERN Prop
AbsStaticArrayProp (StaticArrayEntry * p)
{
  return (Prop) (Addr (p) - AtomBase);
}


#else

inline EXTERN ArrayEntry *RepArrayProp (Prop p);

inline EXTERN ArrayEntry *
RepArrayProp (Prop p)
{
  return (ArrayEntry *) (p);
}



inline EXTERN Prop AbsArrayProp (ArrayEntry * p);

inline EXTERN Prop
AbsArrayProp (ArrayEntry * p)
{
  return (Prop) (p);
}



inline EXTERN StaticArrayEntry *RepStaticArrayProp (Prop p);

inline EXTERN StaticArrayEntry *
RepStaticArrayProp (Prop p)
{
  return (StaticArrayEntry *) (p);
}



inline EXTERN Prop AbsStaticArrayProp (StaticArrayEntry * p);

inline EXTERN Prop
AbsStaticArrayProp (StaticArrayEntry * p)
{
  return (Prop) (p);
}


#endif
#define	ArrayProperty ((PropFlags)0xfff7)


inline EXTERN int ArrayIsDynamic (ArrayEntry *);

inline EXTERN int
ArrayIsDynamic (ArrayEntry * are)
{
  return (int) (((are)->ArrayEArity > 0));
}




inline EXTERN PropFlags IsArrayProperty (int);

inline EXTERN PropFlags
IsArrayProperty (int flags)
{
  return (PropFlags) ((flags == ArrayProperty));
}



/* Proto types */

/* cdmgr.c */
int STD_PROTO (Yap_RemoveIndexation, (PredEntry *));
void STD_PROTO (Yap_UpdateTimestamps, (PredEntry *));

/* dbase.c */
void STD_PROTO (Yap_ErDBE, (DBRef));
DBTerm *STD_PROTO (Yap_StoreTermInDB, (Term, int));
DBTerm *STD_PROTO (Yap_StoreTermInDBPlusExtraSpace, (Term, UInt, UInt *));
Term STD_PROTO (Yap_FetchTermFromDB, (DBTerm *));
void STD_PROTO (Yap_ReleaseTermFromDB, (DBTerm *));

/* init.c */
Atom STD_PROTO (Yap_GetOp, (OpEntry *, int *, int));

/* vsc: redefined to GetAProp to avoid conflicts with Windows header files */
Prop STD_PROTO (Yap_GetAProp, (Atom, PropFlags));
Prop STD_PROTO (Yap_GetAPropHavingLock, (AtomEntry *, PropFlags));

#ifdef THREADS
EXTERN inline PredEntry *STD_PROTO (Yap_GetThreadPred, (PredEntry *));
#endif

typedef enum
{
  PROLOG_MODULE = 0,
  USER_MODULE = 1,
  IDB_MODULE = 2,
  ATTRIBUTES_MODULE = 3,
  CHARSIO_MODULE = 4,
  TERMS_MODULE = 5
} default_modules;

#include "Heap.h"

EXTERN inline UInt STD_PROTO(PRED_HASH, (FunctorEntry *, Term, UInt));

EXTERN inline UInt
PRED_HASH(FunctorEntry *fe, Term cur_mod, UInt size)
{
  return (((CELL)fe+cur_mod)>>2) % size;
}

EXTERN inline Prop STD_PROTO(GetPredPropByFuncHavingLock, (FunctorEntry *, Term));

EXTERN inline Prop
GetPredPropByFuncHavingLock (FunctorEntry *fe, Term cur_mod)
{
  PredEntry *p;

  if (!(p = RepPredProp(fe->PropsOfFE))) {
    return NIL;
  }
  if ((p->ModuleOfPred == cur_mod || !(p->ModuleOfPred))) {
#ifdef THREADS
    /* Thread Local Predicates */
    if (p->PredFlags & ThreadLocalPredFlag) {
      return AbsPredProp (Yap_GetThreadPred (p));
    }
#endif
    return AbsPredProp(p);
  }
  if (p->NextOfPE) {
    UInt hash = PRED_HASH(fe,cur_mod,PredHashTableSize);
    READ_LOCK(PredHashRWLock);
    p = PredHash[hash];
    
    while (p) {
      if (p->FunctorOfPred == fe &&
	  p->ModuleOfPred == cur_mod)
	{
#ifdef THREADS
	  /* Thread Local Predicates */
	  if (p->PredFlags & ThreadLocalPredFlag) {
	    READ_UNLOCK(PredHashRWLock);
	    return AbsPredProp (Yap_GetThreadPred (p));
	  }
#endif
	  READ_UNLOCK(PredHashRWLock);
	  return AbsPredProp(p);
	}
      p = RepPredProp(p->NextOfPE);
    }
    READ_UNLOCK(PredHashRWLock);
  }
  return NIL;
}

EXTERN inline Prop
PredPropByFunc (Functor fe, Term cur_mod)
/* get predicate entry for ap/arity; create it if neccessary.              */
{
  Prop p0;

  WRITE_LOCK (fe->FRWLock);
  p0 = GetPredPropByFuncHavingLock(fe, cur_mod);
  if (p0) {
    WRITE_UNLOCK (fe->FRWLock);
    return p0;
  }
  return Yap_NewPredPropByFunctor (fe, cur_mod);
}

EXTERN inline Prop
PredPropByAtom (Atom at, Term cur_mod)
/* get predicate entry for ap/arity; create it if neccessary.              */
{
  Prop p0;
  AtomEntry *ae = RepAtom (at);

  WRITE_LOCK (ae->ARWLock);
  p0 = ae->PropsOfAE;
  while (p0)
    {
      PredEntry *pe = RepPredProp (p0);
      if (pe->KindOfPE == PEProp &&
	  (pe->ModuleOfPred == cur_mod || !pe->ModuleOfPred))
	{
#ifdef THREADS
	  /* Thread Local Predicates */
	  if (pe->PredFlags & ThreadLocalPredFlag)
	    {
	      WRITE_UNLOCK (ae->ARWLock);
	      return AbsPredProp (Yap_GetThreadPred (pe));
	    }
#endif
	  WRITE_UNLOCK (ae->ARWLock);
	  return (p0);
	}
      p0 = pe->NextOfPE;
    }
  return Yap_NewPredPropByAtom (ae, cur_mod);
}