YAP 7.1.0
clause.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: clause.h *
12* Last rev: *
13* mods: *
14* comments: clause info *
15* *
16*************************************************************************/
17
18#ifndef CLAUSE_H
19#define CLAUSE_H 1
20
21#include "Yatom.h"
22#include "YapHeap.h"
23
24/* consulting files */
25
26typedef union CONSULT_OBJ {
27 const unsigned char *f_name;
28 int mode;
29 Prop p;
30 UInt c;
31 Term r;
33
34/* Either we are assembling clauses or indexing code */
35
36#define ASSEMBLING_CLAUSE 0
37#define ASSEMBLING_INDEX 1
38#define ASSEMBLING_EINDEX 2
39
40#define NextDynamicClause(X) (((yamop *)X)->y_u.Otapl.d)
41
42#define PredFirstClause 0
43#define PredMiddleClause 1
44#define PredLastClause 2
45
46typedef struct logic_upd_index {
47 CELL ClFlags;
48 UInt ClRefCount;
49#if defined(YAPOR) || defined(THREADS)
50/* A lock for manipulating the clause */
51// lockvar ClLock;
52#endif
53 UInt ClSize;
54 struct logic_upd_index *ParentIndex;
55 struct logic_upd_index *SiblingIndex;
56 struct logic_upd_index *PrevSiblingIndex;
57 struct logic_upd_index *ChildIndex;
58 /* The instructions, at least one of the form sl */
59 PredEntry *ClPred;
60 yamop ClCode[MIN_ARRAY];
62
63/* The ordering of the first 3 fields should be compatible with dbrefs */
64typedef struct logic_upd_clause {
65 Functor Id; /* allow pointers to this struct to id */
66 /* as dbref */
67 /* A set of flags describing info on the clause */
68 /* A set of flags describing info on the clause */
69 CELL ClFlags;
70#if defined(YAPOR) || defined(THREADS)
71/* A lock for manipulating the clause */
72// lockvar ClLock;
73#endif
74 UInt ClSize;
75 /* extra clause information for logical update indices and facts */
76 /* indices that may still backtrack to this clause */
77 UInt ClRefCount;
78 /* data for clauses with environments */
79 yamop *ClExt;
80 union {
81 DBTerm *ClSource;
82 Int ClLine;
83 } lusl;
84 /* doubly linked list of clauses */
85 struct logic_upd_clause *ClPrev, *ClNext;
86 /* parent pointer */
87 PredEntry *ClPred;
88 UInt ClTimeStart, ClTimeEnd;
89 /* The instructions, at least one of the form sl */
90 yamop ClCode[MIN_ARRAY];
92
93#include "inline-only.h"
94INLINE_ONLY int VALID_TIMESTAMP(UInt, struct logic_upd_clause *);
95
96INLINE_ONLY int VALID_TIMESTAMP(UInt timestamp,
97 struct logic_upd_clause *cl) {
98 return IN_BETWEEN(cl->ClTimeStart, timestamp, cl->ClTimeEnd);
99}
100
101typedef struct dynamic_clause {
102 /* A set of flags describing info on the clause */
103 CELL ClFlags;
104#if defined(YAPOR) || defined(THREADS)
105 /* A lock for manipulating the clause */
106 lockvar ClLock;
107#endif
108 UInt ClSize;
109 Int ClLine;
110 UInt ClRefCount;
111 yamop *ClPrevious; /* immediate update clause */
112 /* The instructions, at least one of the form sl */
113 yamop ClCode[MIN_ARRAY];
115
116typedef struct static_index {
117 /* A set of flags describing info on the clause */
118 CELL ClFlags;
119 UInt ClSize;
120 struct static_index *SiblingIndex;
121 struct static_index *ChildIndex;
122 /* The instructions, at least one of the form sl */
123 PredEntry *ClPred;
124 yamop ClCode[MIN_ARRAY];
126
127typedef struct static_clause {
128 /* A set of flags describing info on the clause */
129 CELL ClFlags;
130 UInt ClSize;
131 union {
132 DBTerm *ClSource;
133 Int ClLine;
134 } usc;
135 struct static_clause *ClNext;
136 /* The instructions, at least one of the form sl */
137 yamop ClCode[MIN_ARRAY];
139
140typedef struct static_mega_clause {
141 /* A set of flags describing info on the clause */
142 CELL ClFlags;
143 UInt ClSize;
144 PredEntry *ClPred;
145 UInt ClItemSize;
146 Int ClLine;
147 struct static_mega_clause *ClNext;
148 /* The instructions, at least one of the form sl */
149 yamop ClCode[MIN_ARRAY];
150} MegaClause;
151
152typedef union clause_obj {
153 struct logic_upd_clause luc;
154 struct logic_upd_index lui;
155 struct dynamic_clause ic;
156 struct static_clause sc;
157 struct static_mega_clause mc;
158 struct static_index si;
160
161typedef union clause_ptr {
162 struct logic_upd_clause *luc;
163 struct logic_upd_index *lui;
164 struct dynamic_clause *ic;
165 struct static_clause *sc;
166 struct static_mega_clause *mc;
167 struct static_index *si;
169
170typedef struct index_t {
171 struct index_t *next, *prev;
172 UInt nels;
173 UInt arity;
174 PredEntry *ap;
175 CELL bmap;
176 int is_key;
177 int is_udi;
178 UInt ncollisions;
179 UInt max_col_count;
180 UInt ntrys;
181 UInt nentries;
182 UInt hsize;
183 BITS32 *key;
184 CELL *cls, *bcls;
185 BITS32 *links;
186 size_t size;
187 yamop *code;
188 BITS32 *udi_data;
189 void *udi_first, *udi_next;
190 UInt udi_free_args;
191 UInt udi_arg;
192} Index_t;
193
194INLINE_ONLY BITS32 EXO_ADDRESS_TO_OFFSET(struct index_t *it,
195 CELL *ptr);
196
197INLINE_ONLY BITS32 EXO_ADDRESS_TO_OFFSET(struct index_t *it,
198 CELL *ptr) {
199 return (ptr - it->cls) / it->arity + 1;
200}
201
202INLINE_ONLY CELL *EXO_OFFSET_TO_ADDRESS(struct index_t *it,
203 BITS32 off);
204
205INLINE_ONLY CELL *EXO_OFFSET_TO_ADDRESS(struct index_t *it,
206 BITS32 off) {
207 if (off == 0L)
208 return (CELL *)NULL;
209 return (it->cls) + (off - 1) * it->arity;
210}
211
212
213INLINE_ONLY BITS32 ADDRESS_TO_LINK(struct index_t *it,
214 BITS32 *ptr) {
215 return ptr - it->links;
216}
217
218
219INLINE_ONLY BITS32 *LINK_TO_ADDRESS(struct index_t *it,
220 BITS32 off) {
221 return it->links + off;
222}
223
224typedef void (*CRefitExoIndex)(struct index_t **ip, UInt b[] USES_REGS);
225typedef yamop *(*CEnterExoIndex)(struct index_t *it USES_REGS);
226typedef int (*CRetryExoIndex)(struct index_t *it USES_REGS);
227
228typedef struct dbterm_list {
229 /* a list of dbterms associated with a clause */
230 DBTerm *dbterms;
231 yamop *clause_code;
232 PredEntry *p;
233 struct dbterm_list *next_dbl;
234} DBTermList;
235
236#define ClauseCodeToDynamicClause(p) \
237 ((DynamicClause *)((CODEADDR)(p) - (CELL)(((DynamicClause *)NULL)->ClCode)))
238#define ClauseCodeToStaticClause(p) \
239 ((StaticClause *)((CODEADDR)(p) - (CELL)(((StaticClause *)NULL)->ClCode)))
240#define ClauseCodeToLogUpdClause(p) \
241 ((LogUpdClause *)((CODEADDR)(p) - (CELL)(((LogUpdClause *)NULL)->ClCode)))
242#define ClauseCodeToMegaClause(p) \
243 ((MegaClause *)((CODEADDR)(p) - (CELL)(((MegaClause *)NULL)->ClCode)))
244#define ClauseCodeToLogUpdIndex(p) \
245 ((LogUpdIndex *)((CODEADDR)(p) - (CELL)(((LogUpdIndex *)NULL)->ClCode)))
246#define ClauseCodeToStaticIndex(p) \
247 ((StaticIndex *)((CODEADDR)(p) - (CELL)(((StaticIndex *)NULL)->ClCode)))
248
249#define ClauseFlagsToDynamicClause(p) ((DynamicClause *)(p))
250#define ClauseFlagsToLogUpdClause(p) \
251 ((LogUpdClause *)((CODEADDR)(p) - (CELL)(&(((LogUpdClause *)NULL)->ClFlags))))
252#define ClauseFlagsToLogUpdIndex(p) \
253 ((LogUpdIndex *)((CODEADDR)(p) - (CELL)(&(((LogUpdIndex *)NULL)->ClFlags))))
254#define ClauseFlagsToStaticClause(p) ((StaticClause *)(p))
255
256#define DynamicFlags(X) (ClauseCodeToDynamicClause(X)->ClFlags)
257
258#define DynamicLock(X) (ClauseCodeToDynamicClause(X)->ClLock)
259
260#if MULTIPLE_STACKS
261#define INIT_CLREF_COUNT(X) (X)->ClRefCount = 0
262#define INC_CLREF_COUNT(X) (X)->ClRefCount++
263#define DEC_CLREF_COUNT(X) (X)->ClRefCount--
264
265#define CL_IN_USE(X) ((X)->ClRefCount)
266#else
267#define INIT_CLREF_COUNT(X)
268#define INC_CLREF_COUNT(X)
269#define DEC_CLREF_COUNT(X)
270#define CL_IN_USE(X) ((X)->ClFlags & InUseMask || (X)->ClRefCount)
271#endif
272
273/* amasm.c */
274wamreg Yap_emit_x(CELL);
275COUNT Yap_compile_cmp_flags(PredEntry *);
276void Yap_InitComma(void);
277yamop *Yap_InitCommaContinuation(PredEntry *);
278
279/* cdmgr.c */
280void Yap_IPred(PredEntry *, UInt, yamop *);
281bool Yap_addclause(Term, yamop *, Term, Term, Term *);
282void Yap_add_logupd_clause(PredEntry *, LogUpdClause *, int);
283void Yap_kill_iblock(ClauseUnion *, ClauseUnion *, PredEntry *);
284void Yap_EraseStaticClause(StaticClause *, PredEntry *, Term);
285ClauseUnion *Yap_find_owner_index(yamop *, PredEntry *);
286
287/* dbase.c */
288void Yap_ErCl(DynamicClause *);
289void Yap_ErLogUpdCl(LogUpdClause *);
290void Yap_ErLogUpdIndex(LogUpdIndex *);
291Int Yap_Recordz(Atom, Term);
292Int Yap_db_nth_recorded(PredEntry *, Int USES_REGS);
293Int Yap_unify_immediate_ref(DBRef ref USES_REGS);
294
295/* exec.c */
296Term Yap_cp_as_integer(choiceptr);
297
298/* index.c */
299yamop *Yap_PredIsIndexable(PredEntry *, UInt, yamop *);
300yamop *Yap_ExpandIndex(PredEntry *, UInt);
301void Yap_CleanUpIndex(struct logic_upd_index *);
302void Yap_CleanKids(struct logic_upd_index *);
303void Yap_AddClauseToIndex(PredEntry *, yamop *, int);
304void Yap_RemoveClauseFromIndex(PredEntry *, yamop *);
305LogUpdClause *Yap_NthClause(PredEntry *, Int);
306LogUpdClause *Yap_FollowIndexingCode(PredEntry *, yamop *, yhandle_t, yamop *,
307 yamop *);
308
309/* exo.c */
310yamop *Yap_ExoLookup(PredEntry *ap USES_REGS);
311CELL Yap_NextExo(choiceptr cpt, struct index_t *it);
312
313#
314#if USE_THREADED_CODE
315
316#define OP_HASH_SIZE 2048
317
318
319
320/*************************************************************************************************
321 reverse lookup of instructions
322*************************************************************************************************/
323typedef struct opcode_optab_entry {
324 OPCODE opc;
325 op_numbers opnum;
326} op_entry;
327
328
329INLINE_ONLY int rtable_hash_op(OPCODE opc, int hash_mask) {
330 return ((((CELL)opc) >> 3) & hash_mask);
331}
332
333
334/* given an opcode find the corresponding opnumber. This should make
335 switches on ops a much easier operation */
336INLINE_ONLY op_numbers Yap_op_from_opcode(OPCODE opc) {
337 int j = rtable_hash_op(opc, OP_HASH_SIZE - 1);
338
339 while (OP_RTABLE[j].opc != opc) {
340 if (!OP_RTABLE[j].opc)
341 return _Nstop;
342 if (j == OP_HASH_SIZE - 1) {
343 j = 0;
344 } else {
345 j++;
346 }
347 }
348 return OP_RTABLE[j].opnum;
349}
350#else
351static inline op_numbers Yap_op_from_opcode(OPCODE opc) {
352 return ((op_numbers)opc);
353}
354#endif /* USE_THREADED_CODE */
355
356#if defined(YAPOR) || defined(THREADS)
357static inline int same_lu_block(yamop **, yamop *);
358
359static inline int same_lu_block(yamop **paddr, yamop *p) {
360 yamop *np = *paddr;
361 if (np != p) {
362 OPCODE jmp_op = Yap_opcode(_jump_if_nonvar);
363
364 while (np->opc == jmp_op) {
365 np = NEXTOP(np, xll);
366 if (np == p)
367 return TRUE;
368 }
369 return FALSE;
370 } else {
371 return TRUE;
372 }
373}
374#endif
375
376#define Yap_MkStaticRefTerm(cp, ap) __Yap_MkStaticRefTerm((cp), (ap)PASS_REGS)
377
378static inline Term __Yap_MkStaticRefTerm(StaticClause *cp,
379 PredEntry *ap USES_REGS) {
380 Term t[2];
381 t[0] = MkIntegerTerm((Int)cp);
382 t[1] = MkIntegerTerm((Int)ap);
383 return Yap_MkApplTerm(FunctorStaticClause, 2, t);
384}
385
386static inline StaticClause *Yap_ClauseFromTerm(Term t) {
387 return (StaticClause *)IntegerOfTerm(ArgOfTerm(1, t));
388}
389
390#define Yap_MkMegaRefTerm(ap, ipc) __Yap_MkMegaRefTerm((ap), (ipc)PASS_REGS)
391
392static inline Term __Yap_MkMegaRefTerm(PredEntry *ap, yamop *ipc USES_REGS) {
393 Term t[2];
394 t[0] = MkIntegerTerm((Int)ap);
395 t[1] = MkIntegerTerm((Int)ipc);
396 return Yap_MkApplTerm(FunctorMegaClause, 2, t);
397}
398
399static inline yamop *Yap_MegaClauseFromTerm(Term t) {
400 return (yamop *)IntegerOfTerm(ArgOfTerm(2, t));
401}
402
403static inline PredEntry *Yap_MegaClausePredicateFromTerm(Term t) {
404 return (PredEntry *)IntegerOfTerm(ArgOfTerm(1, t));
405}
406
407#define Yap_MkExoRefTerm(ap, i) __Yap_MkExoRefTerm((ap), (i)PASS_REGS)
408
409static inline Term __Yap_MkExoRefTerm(PredEntry *ap, Int i USES_REGS) {
410 Term t[2];
411 t[0] = MkIntegerTerm((Int)ap);
412 t[1] = MkIntegerTerm((Int)i);
413 return Yap_MkApplTerm(FunctorExoClause, 2, t);
414}
415
416static inline Int Yap_ExoClauseFromTerm(Term t) {
417 return IntegerOfTerm(ArgOfTerm(2, t));
418}
419
420static inline PredEntry *Yap_ExoClausePredicateFromTerm(Term t) {
421 return (PredEntry *)IntegerOfTerm(ArgOfTerm(1, t));
422}
423
424/******************************************************************
425
426 EXECUTING PROLOG CLAUSES
427
428******************************************************************/
429
430bool Yap_search_for_static_predicate_in_use(PredEntry *p,
431 bool check_everything);
432
433static inline bool Yap_static_in_use(PredEntry *p, bool check_everything) {
434#if defined(YAPOR) || defined(THREADS)
435 return TRUE;
436#else
437 pred_flags_t pflags = p->PredFlags;
438 if (pflags & (DynamicPredFlag | LogUpdatePredFlag)) {
439 return FALSE;
440 }
441 if (STATIC_PREDICATES_MARKED) {
442 return (p->PredFlags & InUsePredFlag);
443 } else {
444 /* This code does not work for YAPOR or THREADS!!!!!!!! */
445 return Yap_search_for_static_predicate_in_use(p, check_everything);
446 }
447#endif
448
449#define DEAD_REF(ref) FALSE
450}
451
452typedef enum {
453 FIND_PRED_FROM_ANYWHERE,
454 FIND_PRED_FROM_CP,
455 FIND_PRED_FROM_ENV
456} find_pred_type;
457
458PredEntry * Yap_PredForCode(yamop *, find_pred_type, Int *cl);
459PredEntry *Yap_PredEntryForCode(yamop *, find_pred_type, void **, void **);
460LogUpdClause *Yap_new_ludbe(Term, PredEntry *, UInt);
461Term Yap_LUInstance(LogUpdClause *, UInt);
462
463/* udi.c */
464int Yap_new_udi_clause(PredEntry *, yamop *, Term);
465yamop *Yap_udi_search(PredEntry *);
466
467Term Yap_bug_location(yamop *p, yamop *cp, choiceptr b_ptr, CELL *env);
468Term Yap_pc_location(yamop *p, choiceptr b_ptr, CELL *env);
469Term Yap_env_location(yamop *p, choiceptr b_ptr, CELL *env, Int ignore_first);
470
471#if LOW_PROF
472void Yap_InformOfRemoval(void *);
473void Yap_dump_code_area_for_profiler(void);
474#else
475#define Yap_InformOfRemoval(X)
476#endif
477
478static inline void clean_tr(tr_fr_ptr TR0 USES_REGS) {
479 tr_fr_ptr pt0 = TR;
480 while (pt0 != TR0) {
481 Term p = TrailTerm(--pt0);
482 if (IsApplTerm(p)) {
483 CELL *pt = RepAppl(p);
484#ifdef FROZEN_STACKS
485 pt[0] = TrailVal(pt0);
486#else
487 pt[0] = TrailTerm(pt0 - 1);
488 pt0--;
489#endif /* FROZEN_STACKS */
490 } else {
491 RESET_VARIABLE(p);
492 }
493 }
494 TR = TR0;
495}
496
497#include "alloc.h"
498#include "terms.h"
499
500extern PredEntry * Yap_track_cpred( op_numbers opcode, yamop *p, size_t min, void *i );
501
502
503#endif
PredEntry * Yap_PredForCode(yamop *codeptr, find_pred_type hint, Int *cl)
given an arbitrary code point codeptr search the database for the owner predicate pp identifying the ...
Definition: stack.c:730
Definition: Yatom.h:689
Definition: Yatom.h:544
Definition: amidefs.h:264