1 /*************************************************************************
2  *									 *
3  *	 YAP Prolog    @(#)amidefs.h	1.3 3/15/90                      *
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:		amidefs.h						 *
12  * comments:	Abstract machine peculiarities				 *
13  *									 *
14  * Last rev:     $Date: 2008-07-22 23:34:49 $							 *
15  * $Log: not supported by cvs2svn $
16  * Revision 1.33  2007/11/26 23:43:09  vsc
17  * fixes to support threads and assert correctly, even if inefficiently.
18  *
19  * Revision 1.32  2006/10/10 14:08:17  vsc
20  * small fixes on threaded implementation.
21  *
22  * Revision 1.31  2006/09/20 20:03:51  vsc
23  * improve indexing on floats
24  * fix sending large lists to DB
25  *
26  * Revision 1.30  2005/12/17 03:25:39  vsc
27  * major changes to support online event-based profiling
28  * improve error discovery and restart on scanner.
29  *
30  * Revision 1.29  2005/07/06 15:10:15  vsc
31  * improvements to compiler: merged instructions and fixes for ->
32  *
33  * Revision 1.28  2005/05/30 06:07:35  vsc
34  * changes to support more tagging schemes from tabulation.
35  *
36  * Revision 1.27  2005/04/10 04:01:13  vsc
37  * bug fixes, I hope!
38  *
39  * Revision 1.26  2004/09/30 21:37:41  vsc
40  * fixes for thread support
41  *
42  * Revision 1.25  2004/09/27 20:45:04  vsc
43  * Mega clauses
44  * Fixes to sizeof(expand_clauses) which was being overestimated
45  * Fixes to profiling+indexing
46  * Fixes to reallocation of memory after restoring
47  * Make sure all clauses, even for C, end in _Ystop
48  * Don't reuse space for Streams
49  * Fix Stream_F on StreaNo+1
50  *
51  * Revision 1.24  2004/04/14 19:10:40  vsc
52  * expand_clauses: keep a list of clauses to expand
53  * fix new trail scheme for multi-assignment variables
54  *
55  * Revision 1.23  2004/03/31 01:03:10  vsc
56  * support expand group of clauses
57  *
58  * Revision 1.22  2004/03/10 14:59:55  vsc
59  * optimise -> for type tests
60  *									 *
61  *									 *
62  *************************************************************************/
63 
64 #ifndef NULL
65 #include <stdio.h>
66 #endif
67 
68 #ifdef FROZEN_STACKS
69 #ifdef SBA
70 #define PROTECT_FROZEN_H(CPTR)                                  \
71        ((Unsigned((Int)((CPTR)->cp_h)-(Int)(H_FZ)) <            \
72 	 Unsigned((Int)(B_FZ)-(Int)(H_FZ))) ?                   \
73 	(CPTR)->cp_h : H_FZ)
74 #define PROTECT_FROZEN_B(CPTR)                                  \
75        ((Unsigned((Int)(CPTR)-(Int)(H_FZ)) <                    \
76 	 Unsigned((Int)(B_FZ)-(Int)(H_FZ)))  ?                  \
77 	(CPTR) : B_FZ)
78 	 /*
79 #define PROTECT_FROZEN_H(CPTR) ((CPTR)->cp_h > H_FZ && (CPTR)->cp_h < (CELL *)B_FZ ? (CPTR)->cp_h : H_FZ )
80 
81 #define PROTECT_FROZEN_B(CPTR)  ((CPTR) < B_FZ && (CPTR) > (choiceptr)H_FZ ? (CPTR) : B_FZ )
82 	 */
83 #else /* TABLING */
84 #define PROTECT_FROZEN_B(CPTR)  (YOUNGER_CP(CPTR, B_FZ) ? CPTR        : B_FZ)
85 #define PROTECT_FROZEN_H(CPTR)  (((CPTR)->cp_h > H_FZ) ? (CPTR)->cp_h : H_FZ)
86 #endif /* SBA */
87 #else
88 #define PROTECT_FROZEN_B(CPTR)  (CPTR)
89 #define PROTECT_FROZEN_H(CPTR)  (CPTR)->cp_h
90 #endif /* FROZEN_STACKS */
91 
92 #if ALIGN_LONGS
93 /*   */ typedef Int DISPREG;
94 /*   */ typedef CELL SMALLUNSGN;
95 /*   */ typedef Int  OPREG;
96 /*   */ typedef CELL UOPREG;
97 
98 #else
99 /*   */ typedef Short DISPREG;
100 /*   */ typedef BITS16 SMALLUNSGN;
101 /*   */ typedef SBITS16 OPREG;
102 /*   */ typedef SBITS16 UOPREG;
103 
104 #endif
105 
106 
107 typedef Int (*CPredicate)(void);
108 
109 typedef Int (*CmpPredicate)(Term, Term);
110 
111 
112 #define OpRegSize    sizeof(OPREG)
113 
114 /*
115   Possible arguments to YAP emulator:
116   wamreg describes an A or X register;
117   yslot describes an Y slot
118   COUNT is a small number (eg, number of arguments to a choicepoint,
119   number of permanent variables in a environment
120 */
121 
122 typedef OPREG  wamreg;
123 typedef OPREG  yslot;
124 typedef OPREG  COUNT;
125 
126 
127 /*
128   This is a table with the codes for YAP instructions
129 */
130 typedef enum {
131 #define OPCODE(OP,TYPE) _##OP
132 #include "YapOpcodes.h"
133 #undef  OPCODE
134 } op_numbers;
135 
136 #define _std_top	_or_last
137 
138 /* use similar trick for keeping instruction names */
139 #if defined(ANALYST) || defined(DEBUG)
140 extern char *Yap_op_names[_std_top + 1];
141 #endif
142 
143 typedef enum {
144   _atom,
145   _atomic,
146   _integer,
147   _compound,
148   _float,
149   _nonvar,
150   _number,
151   _var,
152   _cut_by,
153   _db_ref,
154   _primitive,
155   _dif,
156   _eq,
157   _equal,
158   _plus,
159   _minus,
160   _times,
161   _div,
162   _and,
163   _or,
164   _sll,
165   _slr,
166   _arg,
167   _functor,
168   _p_put_fi,
169   _p_put_i,
170   _p_put_f,
171   _p_a_eq_float,
172   _p_a_eq_int,
173   _p_a_eq,
174   _p_ltc_float,
175   _p_ltc_int,
176   _p_lt,
177   _p_gtc_float,
178   _p_gtc_int,
179   _p_get_fi,
180   _p_get_i,
181   _p_get_f,
182   _p_add_float_c,
183   _p_add_int_c,
184   _p_add,
185   _p_sub_float_c,
186   _p_sub_int_c,
187   _p_sub,
188   _p_mul_float_c,
189   _p_mul_int_c,
190   _p_mul,
191   _p_fdiv_c1,
192   _p_fdiv_c2,
193   _p_fdiv,
194   _p_idiv_c1,
195   _p_idiv_c2,
196   _p_idiv,
197   _p_mod_c1,
198   _p_mod_c2,
199   _p_mod,
200   _p_rem_c1,
201   _p_rem_c2,
202   _p_rem,
203   _p_land_c,
204   _p_land,
205   _p_lor_c,
206   _p_lor,
207   _p_xor_c,
208   _p_xor,
209   _p_uminus,
210   _p_sr_c1,
211   _p_sr_c2,
212   _p_sr,
213   _p_sl_c1,
214   _p_sl_c2,
215   _p_sl,
216   _p_label_ctl
217 } basic_preds;
218 
219 #if USE_THREADED_CODE
220 
221 #if ALIGN_LONGS
222 /*   */ typedef CELL OPCODE;
223 #else
224 
225 #if LOW_ABSMI
226 /*   */ typedef BITS16 OPCODE;
227 #else
228 /*   */ typedef CELL OPCODE;
229 #endif
230 #endif /* ALIGN_LONGS */
231 #else /* if does not USE_THREADED_CODE */
232 /*   */ typedef op_numbers OPCODE;
233 #endif
234 #define OpCodeSize   sizeof(OPCODE)
235 
236 
237 /*
238 
239   Types of possible YAAM instructions.
240 
241   The meaning and type of the symbols in a abstract machine instruction is:
242 
243   b: arity (Int)
244   b: bitmap (CELL *)
245   c: constant, is a Term
246   d: double (functor + unaligned double)
247   f: functor
248   F: Function, CPredicate
249   i: large integer (functor + long)
250   I: logic upd index (struct logic_upd_index *)
251   l: label, yamop *
252   L: logic upd clause, logic_upd_clause *
253   m: module, Term
254   n: number, Integer
255   N: bigint, Blob (Term)
256   o: opcode, OPCODE
257   O: OR-parallel information, used by YAPOR, unsigned int
258   p: predicate, struct pred_entry *
259   s: small integer, COUNT
260   t: pointer to table entry, used by yaptab, struct table_entry *
261   x: wam register, wamreg
262   y: environment slot
263 
264 */
265 /* This declaration is going to be parsed by a Prolog program, so:
266    comments are welcome, but they should take a whole line,
267    every field declaration should also take a single line,
268    please check the Prolog program if you come up with a complicated C-type that does not start by unsigned or struct.
269 */
270 typedef struct yami {
271   OPCODE opc;
272   union {
273     struct {
274       CELL next;
275     } e;
276     struct {
277       Term                c;
278       CELL next;
279     } c;
280     struct {
281       Term                D;
282       CELL next;
283     } D;
284     struct {
285       Term                b;
286       CELL next;
287     } N;
288     struct {
289       Term                c1;
290       Term                c2;
291       CELL next;
292     } cc;
293     struct {
294       Term                c1;
295       Term                c2;
296       Term                c3;
297       CELL next;
298     } ccc;
299     struct {
300       Term                c1;
301       Term                c2;
302       Term                c3;
303       Term                c4;
304       CELL next;
305     } cccc;
306     struct {
307       Term                c1;
308       Term                c2;
309       Term                c3;
310       Term                c4;
311       Term                c5;
312       CELL next;
313     } ccccc;
314     struct {
315       Term                c1;
316       Term                c2;
317       Term                c3;
318       Term                c4;
319       Term                c5;
320       Term                c6;
321       CELL next;
322     } cccccc;
323     struct {
324       Term                c;
325       struct yami        *l1;
326       struct yami        *l2;
327       struct yami        *l3;
328       CELL next;
329     } clll;
330     struct {
331       CELL    d[1+SIZEOF_DOUBLE/SIZEOF_INT_P];
332       CELL next;
333     } d;
334     struct {
335       struct logic_upd_clause *ClBase;
336       CELL  next;
337     } L;
338     struct {
339       Functor             f;
340       Int                 a;
341       CELL next;
342     } fa;
343     struct {
344       CELL    i[2];
345       CELL next;
346     } i;
347     struct {
348       struct logic_upd_index  *I;
349       struct yami             *l1;
350       struct yami             *l2;
351       COUNT                    s;
352       CELL next;
353     } Ills;
354     struct {
355       struct yami   *l;
356       CELL next;
357     } l;
358     struct {
359 #ifdef YAPOR
360       unsigned int        or_arg;
361 #endif /* YAPOR */
362 #ifdef TABLING
363       struct table_entry *te; /* pointer to table entry */
364 #endif /* TABLING */
365       Int               s;
366       struct pred_entry  *p;
367       struct yami              *d;
368       CELL next;
369     } Otapl;
370     struct {
371       /* call counter */
372       COUNT                n;
373       /* native code pointer */
374       CPredicate           native;
375       /* next instruction to execute after native code if the predicate was not fully compiled */
376       struct yami *native_next;
377       /* Pointer to pred */
378       struct pred_entry   *p;
379       CELL next;
380     } aFlp;
381     /* The next two instructions are twin: they both correspond to the old ldd. */
382     /* The first one, aLl, handles try_logical and retry_logical, */
383     /* Ill handles trust_logical. */
384     /* They must have the same fields. */
385 
386     struct {
387 #ifdef YAPOR
388       unsigned int               or_arg;
389 #endif /* YAPOR */
390 #ifdef TABLING
391       /* pointer to table entry */
392       struct table_entry        *te;
393 #endif
394       /* number of arguments */
395       COUNT                    s;
396       struct logic_upd_clause   *d;
397       struct yami               *n;
398       CELL                       next;
399     } OtaLl;
400     struct {
401 #ifdef YAPOR
402       unsigned int               or_arg;
403 #endif
404 #ifdef TABLING
405       /* pointer to table entry */
406       struct table_entry        *te;
407 #endif /* TABLING */
408       /* number of arguments */
409       struct logic_upd_index  *block;
410       struct logic_upd_clause   *d;
411       struct yami               *n;
412       CELL                       next;
413     } OtILl;
414     struct {
415 #ifdef YAPOR
416       unsigned int        or_arg;
417 #endif
418 #ifdef TABLING
419        /* pointer to table entry */
420       struct table_entry *te;
421 #endif
422       Int               s;
423       struct pred_entry  *p;
424       CPredicate          f;
425       COUNT               extra;
426       CELL next;
427     } OtapFs;
428     struct {
429       struct yami               *l1;
430       struct yami               *l2;
431       struct yami               *l3;
432       CELL next;
433     } lll;
434     struct {
435       struct yami               *l1;
436       struct yami               *l2;
437       struct yami               *l3;
438       struct yami               *l4;
439       CELL next;
440     } llll;
441     struct {
442       wamreg                     x;
443       struct yami               *l1;
444       struct yami               *l2;
445       struct yami               *l3;
446       struct yami               *l4;
447       CELL next;
448     } xllll;
449     struct {
450       COUNT                      s;
451       struct yami               *l1;
452       struct yami               *l2;
453       struct yami               *l3;
454       struct yami               *l4;
455       CELL next;
456     } sllll;
457     struct {
458       struct pred_entry    *p;
459       struct yami          *f;
460       wamreg                x1;
461       wamreg                x2;
462       COUNT                flags;
463       CELL next;
464     } plxxs;
465     struct {
466       struct pred_entry    *p;
467       struct yami          *f;
468       wamreg                x;
469       yslot                 y;
470       COUNT                 flags;
471       CELL next;
472     } plxys;
473     struct {
474       struct pred_entry    *p;
475       struct yami          *f;
476       wamreg                y1;
477       yslot                 y2;
478       COUNT                 flags;
479       CELL next;
480     } plyys;
481     struct {
482       OPCODE              pop;
483       struct yami               *l1;
484       struct yami               *l2;
485       struct yami               *l3;
486       struct yami               *l4;
487       CELL next;
488     } ollll;
489     struct {
490       Int		  i;
491       struct pred_entry  *p;
492       CELL next;
493     } ip;
494     struct {
495       struct yami        *l;
496       struct pred_entry  *p;
497       CELL next;
498     } lp;
499     struct {
500       OPCODE              opcw;
501       CELL next;
502     } o;
503     struct {
504       OPCODE              opcw;
505       Term                c;
506       CELL next;
507     } oc;
508     struct {
509       OPCODE              opcw;
510       Term                b;
511       CELL next;
512     } oN;
513     struct {
514       OPCODE              opcw;
515       CELL    d[1+SIZEOF_DOUBLE/SIZEOF_INT_P];
516       CELL next;
517     } od;
518     struct {
519       OPCODE              opcw;
520       Term                  D;
521       CELL next;
522     } oD;
523     struct {
524       OPCODE              opcw;
525       Functor             f;
526       Int                 a;
527       CELL next;
528     } ofa;
529     struct {
530       OPCODE              opcw;
531       CELL		     i[2];
532       CELL next;
533     } oi;
534     struct {
535       OPCODE              opcw;
536       COUNT               s;
537       CELL                c;
538       CELL next;
539     } osc;
540     struct {
541       OPCODE              opcw;
542       COUNT               s;
543       CELL next;
544     } os;
545     struct {
546       OPCODE              opcw;
547       wamreg                x;
548       CELL next;
549     } ox;
550     struct {
551       OPCODE              opcw;
552       wamreg                xl;
553       wamreg                xr;
554       CELL next;
555     } oxx;
556     struct {
557       OPCODE              opcw;
558       yslot                y;
559       CELL next;
560     } oy;
561     struct {
562       struct pred_entry   *p;
563       CELL next;
564     } p;
565     struct {
566       struct pred_entry   *p;
567       struct pred_entry   *p0;
568       CELL next;
569     } pp;
570     struct {
571       COUNT               s;
572       CELL next;
573     } s;
574     /* format of expand_clauses */
575     struct {
576       COUNT               s1;
577       COUNT               s2;
578       COUNT               s3;
579       struct yami  *sprev;
580       struct yami  *snext;
581       struct pred_entry  *p;
582       CELL next;
583     } sssllp;
584     struct {
585       COUNT               s;
586       CELL                c;
587       CELL next;
588     } sc;
589     struct {
590       COUNT               s;
591       CELL    d[1+SIZEOF_DOUBLE/SIZEOF_INT_P];
592       struct yami        *F;
593       struct yami        *T;
594       CELL next;
595     } sdll;
596     struct {
597       COUNT               s;
598       struct yami        *l;
599       struct pred_entry  *p;
600       CELL next;
601     } slp;
602     struct {
603       COUNT               s;
604       Int                 I;
605       struct yami        *F;
606       struct yami        *T;
607       CELL next;
608     } snll;
609     struct {
610       COUNT               s0;
611       COUNT               s1;
612       CELL    d[1+SIZEOF_DOUBLE/SIZEOF_INT_P];
613       CELL next;
614     } ssd;
615     struct {
616       COUNT               s0;
617       COUNT               s1;
618       Int		  n;
619       CELL next;
620     } ssn;
621     struct {
622       COUNT               s0;
623       COUNT               s1;
624       COUNT               s2;
625       CELL next;
626     } sss;
627     struct {
628       COUNT               s1;
629       COUNT               s2;
630       struct yami        *F;
631       struct yami        *T;
632       CELL next;
633     } ssll;
634     struct {
635       COUNT               s;
636       wamreg              x;
637       struct yami        *l;
638       CELL next;
639     } sxl;
640     struct {
641       COUNT               s;
642       wamreg              x;
643       struct yami        *F;
644       struct yami        *T;
645       CELL next;
646     } sxll;
647     struct {
648       COUNT               s;
649       yslot               y;
650       struct yami        *l;
651       CELL next;
652     } syl;
653     struct {
654       COUNT               s;
655       yslot               y;
656       struct yami        *F;
657       struct yami        *T;
658       CELL next;
659     } syll;
660     /* the next 3 instructions must have same size and have fields in same order! */
661     /* also check env for yes and trustfail code before making any changes */
662     /* last, Osblp is known to the buildops script */
663     struct {
664 #ifdef YAPOR
665       unsigned int        or_arg;
666 #endif
667       COUNT               s;
668       CELL               *bmap;
669       struct yami *l;
670       struct pred_entry  *p0;
671       CELL next;
672     } Osblp;
673     struct {
674 #ifdef YAPOR
675       unsigned int        or_arg;
676 #endif
677       COUNT               s;
678       CELL               *bmap;
679       struct pred_entry  *p;
680       Int		  i;
681       CELL next;
682     } Osbpi;
683     struct {
684 #ifdef YAPOR
685       unsigned int        or_arg;
686 #endif
687       COUNT               s;
688       CELL               *bmap;
689       struct pred_entry  *p;
690       struct pred_entry  *p0;
691       CELL next;
692     } Osbpp;
693     struct {
694 #ifdef YAPOR
695       unsigned int        or_arg;
696 #endif
697       COUNT               s;
698       CELL               *bmap;
699       Term  mod;
700       struct pred_entry  *p0;
701       CELL next;
702     } Osbmp;
703     struct {
704       /* size of table */
705       COUNT               s;
706       /* live entries */
707       COUNT               e;
708       /* pending suspended blocks */
709       COUNT               w;
710       struct yami        *l;
711       CELL next;
712     } sssl;
713     struct {
714       wamreg                x;
715       CELL next;
716     } x;
717     struct {
718       wamreg                x;
719       struct pred_entry    *p0;
720       CELL next;
721     } xp;
722     struct {
723       wamreg                x;
724       CELL                  c;
725       CELL next;
726     } xc;
727     struct {
728       wamreg                x;
729       Term                  b;
730       CELL next;
731     } xN;
732     struct {
733       wamreg                x;
734       CELL    d[1+SIZEOF_DOUBLE/SIZEOF_INT_P];
735       CELL next;
736     } xd;
737     struct {
738       wamreg                x;
739       Term                  D;
740       CELL next;
741     } xD;
742     struct {
743       wamreg                x;
744       Functor             f;
745       Int                 a;
746       CELL next;
747     } xfa;
748     struct {
749       wamreg                x;
750       struct yami          *F;
751       CELL next;
752     } xl;
753     struct {
754       wamreg                x;
755       CELL    i[2];
756       CELL next;
757     } xi;
758     struct {
759       wamreg                x;
760       struct yami	       *l1;
761       struct yami	       *l2;
762       CELL next;
763     } xll;
764     struct {
765       wamreg                xl;
766       wamreg                xr;
767       CELL next;
768     } xx;
769     struct {
770       wamreg                x;
771       wamreg                xi;
772       Term                  c;
773       CELL next;
774     } xxc;
775     struct {
776       wamreg                x;
777       wamreg                xi;
778       Int                   c;
779       CELL next;
780     } xxn;
781     struct {
782       wamreg                x;
783       wamreg                x1;
784       wamreg                x2;
785       CELL next;
786     } xxx;
787     struct {
788       wamreg                xl1;
789       wamreg                xl2;
790       wamreg                xr1;
791       wamreg                xr2;
792       CELL next;
793     } xxxx;
794     struct {
795       wamreg                x;
796       wamreg                x1;
797       yslot                y2;
798       CELL next;
799     } xxy;
800     struct {
801       yslot                y;
802       CELL next;
803     } y;
804     struct {
805       yslot                y;
806       struct pred_entry   *p0;
807       CELL next;
808     } yp;
809     struct {
810       yslot                y;
811       struct yami         *F;
812       CELL next;
813     } yl;
814     struct {
815       yslot                y;
816       wamreg                x;
817       CELL next;
818     } yx;
819     struct {
820       yslot                y;
821       wamreg                x1;
822       wamreg                x2;
823       CELL next;
824     } yxx;
825     struct {
826       yslot                y1;
827       yslot                y2;
828       wamreg                x;
829       CELL next;
830     } yyx;
831     struct {
832       yslot                y1;
833       yslot                y2;
834       wamreg               x1;
835       wamreg               x2;
836       CELL next;
837     } yyxx;
838     struct {
839       yslot                y;
840       yslot                y1;
841       yslot                y2;
842       CELL next;
843     } yyy;
844     struct {
845       yslot                y;
846       wamreg               xi;
847       Int                  c;
848       CELL next;
849     } yxn;
850   } u;
851 } yamop;
852 
853 typedef yamop yamopp;
854 
855 #define OPCR                opc
856 #define OPCW                u.ox.opcw
857 
858 
859 #define NEXTOP(V,TYPE)    ((yamop *)(&((V)->u.TYPE.next)))
860 
861 #define PREVOP(V,TYPE)    ((yamop *)((CODEADDR)(V)-(CELL)NEXTOP((yamop *)NULL,TYPE)))
862 
863 #if defined(TABLING) || defined(SBA)
864 typedef struct trail_frame {
865   Term term;
866   CELL value;
867 } *tr_fr_ptr;
868 
869 #define TrailTerm(X)   ((X)->term)
870 #define TrailVal(X)    ((X)->value)
871 #else
872 typedef Term *tr_fr_ptr;
873 
874 #define TrailTerm(X)   (*(X))
875 #define TrailVal(X)    OOOOOOPS: this program should not compile
876 #endif /* TABLING || SBA  */
877 
878 
879 /*
880   Choice Point Structure
881 
882   6 fixed fields (TR,AP,H,B,ENV,CP) plus arguments
883 */
884 
885 #ifdef DETERMINISTIC_TABLING
886 struct deterministic_choicept {
887   yamop *cp_ap;
888   struct choicept *cp_b;
889   tr_fr_ptr cp_tr;
890 #ifdef DEPTH_LIMIT
891   CELL cp_depth;
892 #endif /* DEPTH_LIMIT */
893 #ifdef YAPOR
894   int cp_lub;           /* local untried branches */
895   struct or_frame *cp_or_fr;  /* or-frame pointer */
896 #endif /* YAPOR */
897   CELL *cp_h;  /* necessary, otherwise we get in trouble */
898 };
899 
900 typedef struct choicept {
901   yamop *cp_ap;
902   struct choicept *cp_b;
903   tr_fr_ptr cp_tr;
904 #ifdef DEPTH_LIMIT
905   CELL cp_depth;
906 #endif /* DEPTH_LIMIT */
907 #ifdef YAPOR
908   int cp_lub;           /* local untried branches */
909   struct or_frame *cp_or_fr;  /* or-frame pointer */
910 #endif /* YAPOR */
911   CELL *cp_h;
912   yamop *cp_cp;
913 #else
914 typedef struct choicept {
915   tr_fr_ptr cp_tr;
916   CELL *cp_h;
917   struct choicept *cp_b;
918 #ifdef DEPTH_LIMIT
919   CELL cp_depth;
920 #endif /* DEPTH_LIMIT */
921   yamop *cp_cp;
922 #ifdef YAPOR
923   int cp_lub;           /* local untried branches */
924   struct or_frame *cp_or_fr;  /* or-frame pointer */
925 #endif /* YAPOR */
926   yamop *cp_ap;
927 #endif /* DETERMINISTIC_TABLING */
928 #if MIN_ARRAY == 0
929   CELL *cp_env;
930   /* GNUCC understands empty arrays */
931   CELL cp_args[MIN_ARRAY];
932 #define cp_a1		cp_args[0]
933 #define cp_a2		cp_args[1]
934 #define cp_a3		cp_args[2]
935 #define cp_a4		cp_args[3]
936 #define cp_a5		cp_args[4]
937 #define cp_a6		cp_args[5]
938 #define cp_a7		cp_args[6]
939 #define cp_a8		cp_args[7]
940 #define EXTRA_CBACK_ARG(Arity,Offset)  B->cp_args[(Arity)+(Offset)-1]
941 #else
942   /* Otherwise, we need a very dirty trick to access the arguments */
943   union {
944     CELL *cp_uenv;
945     CELL  cp_args[1];
946   } cp_last;
947 #define cp_env		cp_last.cp_uenv
948 #define cp_a1		cp_last.cp_args[1]
949 #define cp_a2		cp_last.cp_args[2]
950 #define cp_a3		cp_last.cp_args[3]
951 #define cp_a4		cp_last.cp_args[4]
952 #define cp_a5		cp_last.cp_args[5]
953 #define cp_a6		cp_last.cp_args[6]
954 #define cp_a7		cp_last.cp_args[7]
955 #define cp_a8		cp_last.cp_args[8]
956 #define EXTRA_CBACK_ARG(Arity,Offset)  B->cp_last.cp_args[(Arity)+(Offset)]
957 #endif
958 } *choiceptr;
959 
960 /* This has problems with \+ \+ a, !, b. */
961 #define SHOULD_CUT_UP_TO(X,Y)  ((X) != (Y))
962 /* #define SHOULD_CUT_UP_TO(X,Y)  ((X)  (Y)) */
963 
964 #ifdef SBA
965 #define SHARED_CP(CP)  ((CP) >= B_FZ || (CP) < (choiceptr)H_FZ)
966 
967 #define YOUNGER_CP(CP1, CP2)						\
968   (SHARED_CP(CP1) ?							\
969    (SHARED_CP(CP2) ? OrFr_depth((CP1)->cp_or_fr) > OrFr_depth((CP2)->cp_or_fr) : FALSE)	\
970    :									\
971    (SHARED_CP(CP2) ? TRUE : CP1 < CP2)					\
972    )
973 
974 #define EQUAL_OR_YOUNGER_CP(CP1, CP2)					\
975   (SHARED_CP(CP1) ?							\
976    (SHARED_CP(CP2) ? OrFr_depth((CP1)->cp_or_fr) >= OrFr_depth((CP2)->cp_or_fr) : FALSE) \
977    :									\
978    (SHARED_CP(CP2) ? TRUE : CP1 <= CP2)					\
979    )
980 
981 #define YOUNGER_H(H1, H2) FIXMEE!!!!
982 
983 
984 #else /* ENV_COPY || ACOW */
985 #define YOUNGER_CP(CP1, CP2)           ((CP1) <  (CP2))
986 #define EQUAL_OR_YOUNGER_CP(CP1, CP2)  ((CP1) <= (CP2))
987 
988 #define YOUNGER_H(H1, H2)           ((CELL *)(H1) > (CELL *)(H2))
989 
990 #endif /* SBA */
991 
992 #define YOUNGEST_CP(CP1, CP2)           (YOUNGER_CP(CP1,CP2) ? (CP1) : (CP2))
993 
994 #define YOUNGEST_H(H1, H2)           (YOUNGER_H(H1,H2) ? (CELL *)(H1) : (CELL *)(H2))
995 
996 
997 
998 /*
999   Environment Structure (CP, E, and CUT_B). Yap always saves the B
1000   where to cut to, even if not needed.
1001 */
1002 #define E_CP		-1
1003 #define E_E		-2
1004 #define E_CB		-3
1005 #ifdef TABLING
1006 #define E_B		-4
1007 #ifdef  DEPTH_LIMIT
1008 #define E_DEPTH         -5
1009 #define EnvSizeInCells   5
1010 #else
1011 #define EnvSizeInCells   4
1012 #endif  /* DEPTH_LIMIT */
1013 #else   /* TABLING */
1014 #ifdef  DEPTH_LIMIT
1015 #define E_DEPTH         -4
1016 #define EnvSizeInCells   4
1017 #else
1018 #define EnvSizeInCells   3
1019 #endif  /* DEPTH_LIMIT */
1020 #endif  /* TABLING */
1021 
1022 #if MSHIFTOFFS
1023 #define FixedEnvSize		EnvSizeInCells
1024 #else
1025 #define FixedEnvSize		(EnvSizeInCells*sizeof(CELL))
1026 #endif
1027 #define RealEnvSize	(EnvSizeInCells*sizeof(CELL))
1028 
1029 static inline
ENV_Parent(CELL * env)1030 CELL *ENV_Parent(CELL *env)
1031 {
1032   return (CELL *)env[E_E];
1033 }
1034 
1035 static inline
ENV_Size(yamop * cp)1036 UInt ENV_Size(yamop *cp)
1037 {
1038   return (((yamop *)((CODEADDR)(cp) - (CELL)NEXTOP((yamop *)NULL,Osbpp)))->u.Osbpp.s);
1039 }
1040 
1041 static inline
ENV_ToP(yamop * cp)1042 struct pred_entry *ENV_ToP(yamop *cp)
1043 {
1044   return (((yamop *)((CODEADDR)(cp) - (CELL)NEXTOP((yamop *)NULL,Osbpp)))->u.Osbpp.p);
1045 }
1046 
1047 static inline
ENV_ToOp(yamop * cp)1048 OPCODE ENV_ToOp(yamop *cp)
1049 {
1050   return (((yamop *)((CODEADDR)(cp) - (CELL)NEXTOP((yamop *)NULL,Osbpp)))->opc);
1051 }
1052 
1053 static inline
EnvSize(yamop * cp)1054 UInt EnvSize(yamop *cp)
1055 {
1056   return ((-ENV_Size(cp))/(OPREG)sizeof(CELL));
1057 }
1058 
1059 static inline
EnvBMap(yamop * p)1060 CELL *EnvBMap(yamop *p)
1061 {
1062   return (((yamop *)((CODEADDR)(p) - (CELL)NEXTOP((yamop *)NULL,Osbpp)))->u.Osbpp.bmap);
1063 }
1064 
1065 static inline
EnvPreg(yamop * p)1066 struct pred_entry *EnvPreg(yamop *p)
1067 {
1068   return (((yamop *)((CODEADDR)(p) - (CELL)NEXTOP((yamop *)NULL,Osbpp)))->u.Osbpp.p0);
1069 }
1070 
1071 /* access to instructions */
1072 
1073 #if USE_THREADED_CODE
1074 extern void **Yap_ABSMI_OPCODES;
1075 
1076 #define absmadr(i) ((OPCODE)(Yap_ABSMI_OPCODES[(i)]))
1077 #else
1078 #define absmadr(i) ((OPCODE)(i))
1079 #endif
1080 
1081 /* used to find out how many instructions of each kind are executed */
1082 #ifdef ANALYST
1083 extern YAP_ULONG_LONG Yap_opcount[_std_top + 1];
1084 
1085 extern YAP_ULONG_LONG Yap_2opcount[_std_top + 1][_std_top + 1];
1086 #endif /* ANALYST */
1087 
1088 #if DEPTH_LIMIT
1089 /*
1090   Make this into an even number so that the system will know
1091   it should ignore the depth limit
1092 */
1093 #define RESET_DEPTH() MkIntTerm(MAX_ABS_INT-1)
1094 #else
1095 
1096 #endif
1097 
1098 
1099