YAP 7.1.0
Tags_32Ops.h
1
2
3
4
5
6
7
8/*************************************************************************
9* *
10* YAP Prolog %W% %G% *
11* Yap Prolog was developed at NCCUP - Universidade do Porto *
12* *
13* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
14* *
15**************************************************************************
16* *
17* File: Tags_32Ops.h.m4 *
18* Last rev: December 90 *
19* mods: *
20* comments: Original Tag Scheme for machines with 32 bits adresses *
21* version: $Id: Tags_32Ops.h,v 1.3 2008-01-30 10:35:43 vsc Exp $ *
22*************************************************************************/
23
24/*
25
26 Version for 32 bit addresses machines,
27 Each term is represented internally as an unsigned 32 bit integer as
28 follows:
29 tag value
30 ints 1m1....01 numeric value
31 atoms 1m0....01 offset of atom entry
32 pairs 1mr....11 ptr to pair
33 aplied functor 1mr....00 ptr to functor followed by args
34 undefined 0mr....00 address of cell pointing to itself
35
36 functors are represented as ptrs to the functor entry in the atom
37property list
38
39 This version speeds up access to lists and to compound
40terms by using the XOR and NOT operations to build their tags. This
41saves operations on RISC machines.
42
43 As a further optimisation, only pairs or compound terms have
44the second lowest bit set. This allows one to recognise lists or
45compound terms with a single operation.
46
47 The main problem is that the default value of the M and R bits for GC
48are now 1 in compound terms and structures.
49
50*/
51
52#if SIZEOF_INT_P==4 && !defined(USE_LOW32_TAGS)
53
54#define TAGS_FAST_OPS 1
55
56#define SHIFT_HIGH_TAG 29
57
58#define MKTAG(HI,LO) ((((UInt) (HI))<<SHIFT_HIGH_TAG)|(LO))
59
60#define TagBits /* 0xb0000003L */ MKTAG(0x5,3)
61#define LowTagBits /* 0x00000003L */ MKTAG(0x0,3)
62#define LowBit /* 0x00000001L */ MKTAG(0x0,1)
63#define HighTagBits /* 0xf0000000L */ MKTAG(0x7,0)
64#define AdrHiBit /* 0x08000000L */ (((UInt)1) << (SHIFT_HIGH_TAG-1))
65#define MaskAdr /* 0x1ffffffcL */ ((((UInt)1) << (SHIFT_HIGH_TAG))-4)
66#define MaskPrim /* 0x0ffffffcL */ ((((UInt)1) << (SHIFT_HIGH_TAG))-4)
67#define NumberTag /* 0xb0000001L */ MKTAG(0x5,2)
68#define AtomTag /* 0x90000001L */ MKTAG(0x4,2)
69#define MAX_ABS_INT /* 0xfe00000LL */ ((Int)0x04000000L)
70
71/* bits that should not be used by anyone but us */
72#define YAP_PROTECTED_MASK 0xe0000000L
73
74#define MaskBits 4
75
76/*
77 UNIQUE_TAG_FOR_PAIR gives the representation for pair an
78 unique tag
79
80 This allows optimisation of switch_list
81
82*/
83#if defined(i386) || defined(sparc) || defined(_POWER) || defined(__POWERPC__) || defined(__sparc)
84#define UNIQUE_TAG_FOR_PAIRS 1
85#endif
86
87#if UNIQUE_TAG_FOR_PAIRS
88#define PairBit /* 0x00000001L */ 1
89#define ApplBit /* 0x00000000L */ 0
90#else
91#define PairBit /* 0x00000000L */ 0
92#define ApplBit /* 0x00000001L */ 1
93#endif
94
95#define TagOf(t) (Unsigned(t)&TagBits)
96#define LowTagOf(t) (Unsigned(t)&TagBits)
97#define NonTagPart(X) (Signed(X) & MaskPrim)
98#define TAGGEDA(TAG,V) (TAG | Unsigned(V))
99#define TAGGED(TAG,V) (TAG | NonTagPart(Unsigned(V)<<2))
100#define NONTAGGED(TAG,V) NonTagPart(Unsigned(V)<<2)
101#define BitOn(Bit,V) (Bit & Unsigned(V))
102#define CHKTAG(t,Tag) ((Unsigned(t)&TagBits)==Tag)
103
104/* never forget to surround arguments to a macro by brackets */
105
106#include "inline-only.h"
107INLINE_ONLY EXTERN int IsVarTerm (Term);
108
109INLINE_ONLY EXTERN int
110IsVarTerm (Term t)
111{
112 return (int) (Signed (t) >= 0);
113}
114
115
116
117INLINE_ONLY EXTERN int IsNonVarTerm (Term);
118
119INLINE_ONLY EXTERN int
120IsNonVarTerm (Term t)
121{
122 return (int) (Signed (t) < 0);
123}
124
125
126#if UNIQUE_TAG_FOR_PAIRS
127
128INLINE_ONLY EXTERN Term *RepPair (Term);
129
130INLINE_ONLY EXTERN Term *
131RepPair (Term t)
132{
133 return (Term *) ((~(t)));
134}
135
136
137
138INLINE_ONLY EXTERN Term AbsPair (Term *);
139
140INLINE_ONLY EXTERN Term
141AbsPair (Term * p)
142{
143 return (Term) ((~Unsigned (p)));
144}
145
146
147
148INLINE_ONLY EXTERN Int IsPairTerm (Term);
149
150INLINE_ONLY EXTERN Int
151IsPairTerm (Term t)
152{
153 return (Int) (((t) & PairBit));
154}
155
156
157
158INLINE_ONLY EXTERN Term *RepAppl (Term);
159
160INLINE_ONLY EXTERN Term *
161RepAppl (Term t)
162{
163 return (Term *) ((-Signed (t)));
164}
165
166
167
168INLINE_ONLY EXTERN Term AbsAppl (Term *);
169
170INLINE_ONLY EXTERN Term
171AbsAppl (Term * p)
172{
173 return (Term) ((-Signed (p)));
174}
175
176
177
178INLINE_ONLY EXTERN Int IsApplTerm (Term);
179
180INLINE_ONLY EXTERN Int
181IsApplTerm (Term t)
182{
183 return (Int) ((!((t) & LowTagBits)));
184}
185
186
187#else
188
189INLINE_ONLY EXTERN Term *RepPair (Term);
190
191INLINE_ONLY EXTERN Term *
192RepPair (Term t)
193{
194 return (Term *) ((-Signed (t)));
195}
196
197
198
199INLINE_ONLY EXTERN Term AbsPair (Term *);
200
201INLINE_ONLY EXTERN Term
202AbsPair (Term * p)
203{
204 return (Term) (((CELL) (-Signed (p))));
205}
206
207
208
209INLINE_ONLY EXTERN Int IsPairTerm (Term);
210
211INLINE_ONLY EXTERN Int
212IsPairTerm (Term t)
213{
214 return (Int) ((!((t) & LowTagBits)));
215}
216
217
218
219INLINE_ONLY EXTERN Term *RepAppl (Term);
220
221INLINE_ONLY EXTERN Term *
222RepAppl (Term t)
223{
224 return (Term *) ((~(t)));
225}
226
227
228
229INLINE_ONLY EXTERN Term AbsAppl (Term *);
230
231INLINE_ONLY EXTERN Term
232AbsAppl (Term * p)
233{
234 return (Term) ((~Unsigned (p)));
235}
236
237
238
239INLINE_ONLY EXTERN Int IsApplTerm (Term);
240
241INLINE_ONLY EXTERN Int
242IsApplTerm (Term t)
243{
244 return (Int) (((t) & ApplBit));
245}
246
247
248#endif
249
250INLINE_ONLY EXTERN Int IsAtomOrIntTerm (Term);
251
252INLINE_ONLY EXTERN Int
253IsAtomOrIntTerm (Term t)
254{
255 return (Int) (((Unsigned (t) & LowTagBits) == 0x2));
256}
257
258
259
260
261INLINE_ONLY EXTERN Int IntOfTerm (Term);
262
263INLINE_ONLY EXTERN Int
264IntOfTerm (Term t)
265{
266 return (Int) ((Int) (Unsigned (t) << 3) >> 5);
267}
268
269
270
271#if UNIQUE_TAG_FOR_PAIRS
272
273INLINE_ONLY EXTERN Term AdjustPtr (Term t, Term off);
274
275INLINE_ONLY EXTERN Term
276AdjustPtr (Term t, Term off)
277{
278 return (Term) (((IsVarTerm (t)
279 || IsAtomOrIntTerm (t)) ? (t) +
280 (off) : (IsPairTerm (t) ? (CELL)
281 AbsPair ((CELL *) ((CELL) RepPair (t) +
282 (off))) : (t) - (off))));
283}
284
285
286
287INLINE_ONLY EXTERN Term AdjustIDBPtr (Term t, Term off);
288
289INLINE_ONLY EXTERN Term
290AdjustIDBPtr (Term t, Term off)
291{
292 return (Term) (IsVarTerm (t) ? (t) + (off) : (t) - (off));
293}
294
295
296#else
297
298INLINE_ONLY EXTERN Term AdjustPtr (Term t, Term off);
299
300INLINE_ONLY EXTERN Term
301AdjustPtr (Term t, Term off)
302{
303 return (Term) (((IsVarTerm (t)
304 || IsAtomOrIntTerm (t)) ? (t) +
305 (off) : (IsApplTerm (t) ? (CELL)
306 AbsAppl ((CELL *) ((CELL) RepAppl (t) +
307 (off))) : (t) - (off))));
308}
309
310
311
312INLINE_ONLY EXTERN Term AdjustIDBPtr (Term t, Term off);
313
314INLINE_ONLY EXTERN Term
315AdjustIDBPtr (Term t, Term off)
316{
317 return (Term) (IsVarTerm (t) ? (t) +
318 (off) : (IsApplTerm (t) ? (CELL)
319 AbsAppl ((CELL *) ((CELL) RepAppl (t) +
320 (off))) : (t) - (off)));
321}
322
323
324#endif
325
326#endif /* SIZEOF_INT_P==4 */
327
328
329