*** empty log message ***
[bpt/guile.git] / libguile / tags.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
3#ifndef TAGSH
4#define TAGSH
5/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
8ce94504 6 *
0f2d19dd
JB
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
8ce94504 11 *
0f2d19dd
JB
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
8ce94504 16 *
0f2d19dd
JB
17 * You should have received a copy of the GNU General Public License
18 * along with this software; see the file COPYING. If not, write to
82892bed
JB
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
0f2d19dd
JB
21 *
22 * As a special exception, the Free Software Foundation gives permission
23 * for additional uses of the text contained in its release of GUILE.
24 *
25 * The exception is that, if you link the GUILE library with other files
26 * to produce an executable, this does not by itself cause the
27 * resulting executable to be covered by the GNU General Public License.
28 * Your use of that executable is in no way restricted on account of
29 * linking the GUILE library code into it.
30 *
31 * This exception does not however invalidate any other reasons why
32 * the executable file might be covered by the GNU General Public License.
33 *
34 * This exception applies only to the code released by the
35 * Free Software Foundation under the name GUILE. If you copy
36 * code from other Free Software Foundation releases into a copy of
37 * GUILE, as the General Public License permits, the exception does
38 * not apply to the code that you add in this way. To avoid misleading
39 * anyone as to the status of such modified files, you must delete
40 * this exception notice from them.
41 *
42 * If you write modifications of your own for GUILE, it is your choice
43 * whether to permit this exception to apply to your modifications.
82892bed 44 * If you do not wish that, delete this exception notice. */
0f2d19dd
JB
45\f
46
8ce94504 47/** This file defines the format of SCM values and cons pairs.
0f2d19dd
JB
48 ** It is here that tag bits are assigned for various purposes.
49 **/
50
51\f
52
53/* In the beginning was the Word:
54 */
55typedef long SCM;
56
57
58
59/* Cray machines have pointers that are incremented once for each word,
60 * rather than each byte, the 3 most significant bits encode the byte
61 * within the word. The following macros deal with this by storing the
62 * native Cray pointers like the ones that looks like scm expects. This
63 * is done for any pointers that might appear in the car of a scm_cell, pointers
64 * to scm_vector elts, functions, &c are not munged.
65 */
66#ifdef _UNICOS
67# define SCM2PTR(x) ((int)(x) >> 3)
68# define PTR2SCM(x) (((SCM)(x)) << 3)
69# define SCM_POINTERS_MUNGED
70#else
71# define SCM2PTR(x) (x)
72# define PTR2SCM(x) ((SCM)(x))
73#endif /* def _UNICOS */
74
75\f
76/* SCM variables can contain:
77 *
78 * Non-objects -- meaning that the tag-related macros don't apply to them
79 * in the usual way.
80 *
81 * Immediates -- meaning that the variable contains an entire Scheme object.
82 *
3c205827
JB
83 * Non-immediates -- meaning that the variable holds a (possibly
84 * tagged) pointer into the cons pair heap.
85 *
86 * Non-objects are distinguished from other values by careful coding
87 * only (i.e., programmers must keep track of any SCM variables they
88 * create that don't contain ordinary scheme values).
89 *
90 * All immediates and non-immediates must have a 0 in bit 0. Only
91 * non-object values can have a 1 in bit 0. In some cases, bit 0 of a
92 * word in the heap is used for the GC tag so during garbage
93 * collection, that bit might be 1 even in an immediate or
94 * non-immediate value. In other cases, bit 0 of a word in the heap
95 * is used to tag a pointer to a GLOC (VM global variable address) or
96 * the header of a struct. But whenever an SCM variable holds a
97 * normal Scheme value, bit 0 is 0.
0f2d19dd
JB
98 *
99 * Immediates and non-immediates are distinguished by bits two and four.
100 * Immediate values must have a 1 in at least one of those bits. Does
c6c790ed 101 * this (or any other detail of tagging) seem arbitrary? Try changing it!
0f2d19dd 102 * (Not always impossible but it is fair to say that many details of tags
3c205827 103 * are mutually dependent). */
0f2d19dd
JB
104
105#define SCM_IMP(x) (6 & (int)(x))
106#define SCM_NIMP(x) (!SCM_IMP(x))
107
108/* Here is a summary of tagging in SCM values as they might occur in
8ce94504 109 * SCM variables or in the heap.
0f2d19dd
JB
110 *
111 * low bits meaning
112 *
8ce94504 113 *
0f2d19dd
JB
114 * 0 Most objects except...
115 * 1 ...glocs and structs (this tag valid only in a SCM_CAR or
116 * in the header of a struct's data).
117 *
118 * 00 heap addresses and many immediates (not integers)
119 * 01 glocs/structs, some tc7_ codes
120 * 10 immediate integers
121 * 11 various tc7_ codes including, tc16_ codes.
122 *
123 *
124 * 000 heap address
125 * 001 glocs/structs
126 * 010 integer
127 * 011 closure
128 * 100 immediates
129 * 101 tc7_
130 * 110 integer
131 * 111 tc7_
132 *
133 *
134 * 100 --- IMMEDIATES
135 *
136 * Looking at the seven final bits of an immediate:
8ce94504 137 *
0f2d19dd
JB
138 * 0000-100 short instruction
139 * 0001-100 short instruction
140 * 0010-100 short instruction
141 * 0011-100 short instruction
142 * 0100-100 short instruction
143 * 0101-100 short instruction
144 * 0110-100 various immediates and long instructions
145 * 0111-100 short instruction
146 * 1000-100 short instruction
147 * 1001-100 short instruction
148 * 1010-100 short instruction
149 * 1011-100 short instruction
150 * 1100-100 short instruction
151 * 1101-100 short instruction
152 * 1110-100 immediate characters
153 * 1111-100 ilocs
154 *
8ce94504 155 * Some of the 0110100 immediates are long instructions (they dispatch
0f2d19dd
JB
156 * in two steps compared to one step for a short instruction).
157 * The two steps are, (1) dispatch on 7 bits to the long instruction
158 * handler, (2) dispatch on 7 additional bits.
159 *
160 * One way to think of it is that there are 128 short instructions,
161 * with the 13 immediates above being some of the most interesting.
162 *
163 * Also noteworthy are the groups of 16 7-bit instructions implied by
164 * some of the 3-bit tags. For example, closure references consist
165 * of an 8-bit aligned address tagged with 011. There are 16 identical 7-bit
166 * instructions, all ending 011, which are invoked by evaluating closures.
167 *
168 * In other words, if you hand the evaluator a closure, the evaluator
169 * treats the closure as a graph of virtual machine instructions.
170 * A closure is a pair with a pointer to the body of the procedure
171 * in the CDR and a pointer to the environment of the closure in the CAR.
172 * The environment pointer is tagged 011 which implies that the least
173 * significant 7 bits of the environment pointer also happen to be
174 * a virtual machine instruction we could call "SELF" (for self-evaluating
175 * object).
176 *
177 * A less trivial example are the 16 instructions ending 000. If those
178 * bits tag the CAR of a pair, then evidently the pair is an ordinary
179 * cons pair and should be evaluated as a procedure application. The sixteen,
180 * 7-bit 000 instructions are all "NORMAL-APPLY" (Things get trickier.
181 * For example, if the CAR of a procedure application is a symbol, the NORMAL-APPLY
182 * instruction will, as a side effect, overwrite that CAR with a new instruction
183 * that contains a cached address for the variable named by the symbol.)
184 *
185 * Here is a summary of tags in the CAR of a non-immediate:
186 *
187 * HEAP CELL: G=gc_mark; 1 during mark, 0 other times.
188 *
189 * cons ..........SCM car..............0 ...........SCM cdr.............G
190 * gloc ..........SCM vcell..........001 ...........SCM cdr.............G
191 * struct ..........void * type........001 ...........void * data.........G
192 * closure ..........SCM code...........011 ...........SCM env.............G
193 * tc7 .........long length....GxxxD1S1 ..........void *data............
194 *
195 *
196 *
197 * 101 & 111 --- tc7_ types
198 *
8ce94504
JB
199 * tc7_tags are 7 bit tags ending in 1x1. These tags
200 * occur only in the CAR of heap cells, and have the
201 * handy property that all bits of the CAR above the
202 * bottom eight can be used to store a length, thus
203 * saving a word in the body itself. Thus, we use them
204 * for strings, symbols, and vectors (among other
205 * things).
0f2d19dd
JB
206 *
207 * SCM_LENGTH returns the bits in "length" (see the diagram).
208 * SCM_CHARS returns the data cast to "char *"
209 * SCM_CDR returns the data cast to "SCM"
210 * TYP7(X) returns bits 0...6 of SCM_CAR (X)
211 *
212 * For the interpretation of SCM_LENGTH and SCM_CHARS
213 * that applies to a particular type, see the header file
214 * for that type.
215 *
8ce94504
JB
216 * Sometimes we choose the bottom seven bits carefully,
217 * so that the 4- and 1-valued bits (called the D and S
218 * bits) can be masked off to reveal a common type.
219 *
0f2d19dd
JB
220 * TYP7S(X) returns TYP7, but masking out the option bit S.
221 * TYP7D(X) returns TYP7, but masking out the option bit D.
222 * TYP7SD(X) masks out both option bits.
223 *
8ce94504
JB
224 * For example, all strings have 001 in the 'xxx' bits in
225 * the diagram above, the D bit says whether it's a
226 * substring, and the S bit says whether it's a multibyte
227 * character string.
228 *
0f2d19dd
JB
229 * for example:
230 * D S
8ce94504
JB
231 * scm_tc7_string = G0010101
232 * scm_tc7_mb_string = G0010111
233 * scm_tc7_substring = G0011101
234 * scm_tc7_mb_substring = G0011111
235 *
236 * TYP7DS turns all string tags into tc7_string; thus,
237 * testing TYP7DS against tc7_string is a quick way to
238 * test for any kind of string.
0f2d19dd 239 *
8ce94504 240 * TYP7S turns tc7_mb_string into tc7_string and
0f2d19dd
JB
241 * tc7_mb_substring into tc7_substring.
242 *
8ce94504 243 * TYP7D turns tc7_mb_substring into tc7_mb_string and
0f2d19dd
JB
244 * tc7_substring into tc7_string.
245 *
0f2d19dd
JB
246 * Some TC7 types are subdivided into 256 subtypes giving
247 * rise to the macros:
248 *
249 * TYP16
250 * TYP16S
251 * GCTYP16
252 *
253 * TYP16S functions similarly wrt to TYP16 as TYP7S to TYP7,
254 * but a different option bit is used (bit 2 for TYP7S,
255 * bit 8 for TYP16S).
8ce94504 256 * */
0f2d19dd
JB
257
258
259
260\f
261/* {Non-immediate values.}
262 *
263 * If X is non-immediate, it is necessary to look at SCM_CAR (X) to
264 * figure out Xs type. X may be a cons pair, in which case the
265 * value SCM_CAR (x) will be either an immediate or non-immediate value.
266 * X may be something other than a cons pair, in which case the value SCM_CAR (x)
8ce94504 267 * will be a non-object value.
0f2d19dd
JB
268 *
269 * All immediates and non-immediates have a 0 in bit 0. We additionally preserve
270 * the invariant that all non-object values stored in the SCM_CAR of a non-immediate
271 * object have a 1 in bit 1:
272 */
273
274#define SCM_NCONSP(x) (1 & (int)SCM_CAR(x))
275#define SCM_CONSP(x) (!SCM_NCONSP(x))
276
277
ee4274a6
MD
278/* SCM_ECONSP should be used instead of SCM_CONSP at places where GLOCS
279 * can be expected to occur.
0f2d19dd 280 */
b2530d66
MD
281#define SCM_ECONSP(x) (SCM_CONSP (x) \
282 || (SCM_TYP3(x) == 1 \
283 && SCM_CDR (SCM_CAR (x) - 1) != 0))
284#define SCM_NECONSP(x) (SCM_NCONSP(x) \
285 && (SCM_TYP3(x) != 1 \
286 || SCM_CDR (SCM_CAR (x) - 1) == 0))
0f2d19dd
JB
287
288\f
289
290#define SCM_CELLP(x) (!SCM_NCELLP(x))
291#define SCM_NCELLP(x) ((sizeof(scm_cell)-1) & (int)(x))
292
8ce94504 293/* See numbers.h for macros relating to immediate integers.
0f2d19dd
JB
294 */
295
296#define SCM_ITAG3(x) (7 & (int)x)
297#define SCM_TYP3(x) (7 & (int)SCM_CAR(x))
298#define scm_tc3_cons 0
299#define scm_tc3_cons_gloc 1
300#define scm_tc3_int_1 2
301#define scm_tc3_closure 3
302#define scm_tc3_imm24 4
303#define scm_tc3_tc7_1 5
304#define scm_tc3_int_2 6
305#define scm_tc3_tc7_2 7
306
307
308/*
309 * Do not change the three bit tags.
310 */
311
312
313#define SCM_TYP7(x) (0x7f & (int)SCM_CAR(x))
314#define SCM_TYP7S(x) (0x7d & (int)SCM_CAR(x))
315#define SCM_TYP7SD(x) (0x75 & (int)SCM_CAR(x))
316#define SCM_TYP7D(x) (0x77 & (int)SCM_CAR(x))
317
318
319#define SCM_TYP16(x) (0xffff & (int)SCM_CAR(x))
320#define SCM_TYP16S(x) (0xfeff & (int)SCM_CAR(x))
321#define SCM_GCTYP16(x) (0xff7f & (int)SCM_CAR(x))
322
323
324
325/* Testing and Changing GC Marks in Various Standard Positions
326 */
327#define SCM_GCMARKP(x) (1 & (int)SCM_CDR(x))
328#define SCM_GC8MARKP(x) (0x80 & (int)SCM_CAR(x))
898a256f
MD
329#define SCM_SETGCMARK(x) SCM_SETOR_CDR (x,1)
330#define SCM_CLRGCMARK(x) SCM_SETAND_CDR (x, ~1L)
331#define SCM_SETGC8MARK(x) SCM_SETOR_CAR (x, 0x80)
332#define SCM_CLRGC8MARK(x) SCM_SETAND_CAR (x, ~0x80L)
0f2d19dd
JB
333
334
335\f
336
337/* couple */
338#define scm_tc7_ssymbol 5
339#define scm_tc7_msymbol 7
340
341/* couple */
342#define scm_tc7_vector 13
343#define scm_tc7_wvect 15
344
345/* a quad, two couples, two trists */
346#define scm_tc7_string 21
347#define scm_tc7_mb_string 23
348#define scm_tc7_substring 29
349#define scm_tc7_mb_substring 31
350
351/* Many of the following should be turned
352 * into structs or smobs. We need back some
353 * of these 7 bit tags!
354 */
355#define scm_tc7_uvect 37
356#define scm_tc7_lvector 39
357#define scm_tc7_fvect 45
358#define scm_tc7_dvect 47
359#define scm_tc7_cvect 53
360#define scm_tc7_svect 55
361#define scm_tc7_contin 61
362#define scm_tc7_cclo 63
363#define scm_tc7_rpsubr 69
364#define scm_tc7_bvect 71
365#define scm_tc7_byvect 77
366#define scm_tc7_ivect 79
367#define scm_tc7_subr_0 85
368#define scm_tc7_subr_1 87
369#define scm_tc7_cxr 93
370#define scm_tc7_subr_3 95
371#define scm_tc7_subr_2 101
372#define scm_tc7_asubr 103
373#define scm_tc7_subr_1o 109
374#define scm_tc7_subr_2o 111
375#define scm_tc7_lsubr_2 117
376#define scm_tc7_lsubr 119
377
378
379/* There are 256 port subtypes. Here are the first four.
380 * These must agree with the init function in ports.c
381 */
382#define scm_tc7_port 125
383
384/* fports and pipes form an intended TYP16S equivelancy
385 * group (similar to a tc7 "couple".
386 */
387#define scm_tc16_fport (scm_tc7_port + 0*256L)
388#define scm_tc16_pipe (scm_tc7_port + 1*256L)
389
390#define scm_tc16_strport (scm_tc7_port + 2*256L)
391#define scm_tc16_sfport (scm_tc7_port + 3*256L)
392
393
394/* There are 256 smob subtypes. Here are the first four.
395 */
396
397#define scm_tc7_smob 127 /* DO NOT CHANGE [**] */
398
399/* [**] If you change scm_tc7_smob, you must also change
400 * the places it is hard coded in this file and possibly others.
401 */
402
403
404/* scm_tc_free_cell is also the 0th smob type.
405 */
406#define scm_tc_free_cell 127
407
408/* The 1st smob type:
409 */
410#define scm_tc16_flo 0x017f
411#define scm_tc_flo 0x017fL
412
413/* Some option bits begeinning at bit 16 of scm_tc16_flo:
414 */
415#define SCM_REAL_PART (1L<<16)
416#define SCM_IMAG_PART (2L<<16)
417#define scm_tc_dblr (scm_tc16_flo|SCM_REAL_PART)
418#define scm_tc_dblc (scm_tc16_flo|SCM_REAL_PART|SCM_IMAG_PART)
419
420
8ce94504 421/* Smob types 2 and 3:
0f2d19dd
JB
422 */
423#define scm_tc16_bigpos 0x027f
424#define scm_tc16_bigneg 0x037f
425
426
427\f
8ce94504 428/* {Immediate Values}
0f2d19dd
JB
429 */
430
431enum scm_tags
432{
433 scm_tc8_char = 0xf4,
4816f615 434 scm_tc8_iloc = 0xfc
0f2d19dd
JB
435};
436
437#define SCM_ITAG8(X) ((int)(X) & 0xff)
438#define SCM_MAKE_ITAG8(X, TAG) (((X)<<8) + TAG)
439#define SCM_ITAG8_DATA(X) ((X)>>8)
440
441
442\f
443/* Immediate Symbols, Special Symbols, Flags (various constants).
444 */
445
446/* SCM_ISYMP tests for ISPCSYM and ISYM */
447#define SCM_ISYMP(n) ((0x187 & (int)(n))==4)
448
449/* SCM_IFLAGP tests for ISPCSYM, ISYM and IFLAG */
450#define SCM_IFLAGP(n) ((0x87 & (int)(n))==4)
451#define SCM_ISYMNUM(n) ((int)((n)>>9))
08b5b88c 452#define SCM_ISYMCHARS(n) (scm_isymnames[SCM_ISYMNUM(n)])
0f2d19dd
JB
453#define SCM_MAKSPCSYM(n) (((n)<<9)+((n)<<3)+4L)
454#define SCM_MAKISYM(n) (((n)<<9)+0x74L)
455#define SCM_MAKIFLAG(n) (((n)<<9)+0x174L)
456
8ce94504 457/* This table must agree with the declarations
0f2d19dd
JB
458 * in repl.c: {Names of immediate symbols}.
459 *
460 * These are used only in eval but their values
461 * have to be allocated here.
462 *
463 */
464
465#define SCM_IM_AND SCM_MAKSPCSYM(0)
466#define SCM_IM_BEGIN SCM_MAKSPCSYM(1)
467#define SCM_IM_CASE SCM_MAKSPCSYM(2)
468#define SCM_IM_COND SCM_MAKSPCSYM(3)
469#define SCM_IM_DO SCM_MAKSPCSYM(4)
470#define SCM_IM_IF SCM_MAKSPCSYM(5)
471#define SCM_IM_LAMBDA SCM_MAKSPCSYM(6)
472#define SCM_IM_LET SCM_MAKSPCSYM(7)
473#define SCM_IM_LETSTAR SCM_MAKSPCSYM(8)
474#define SCM_IM_LETREC SCM_MAKSPCSYM(9)
475#define SCM_IM_OR SCM_MAKSPCSYM(10)
476#define SCM_IM_QUOTE SCM_MAKSPCSYM(11)
477#define SCM_IM_SET SCM_MAKSPCSYM(12)
478#define SCM_IM_DEFINE SCM_MAKSPCSYM(13)
479#define SCM_IM_APPLY SCM_MAKISYM(14)
480#define SCM_IM_CONT SCM_MAKISYM(15)
481#define SCM_BOOL_F SCM_MAKIFLAG(16)
482#define SCM_BOOL_T SCM_MAKIFLAG(17)
483#define SCM_UNDEFINED SCM_MAKIFLAG(18)
484#define SCM_EOF_VAL SCM_MAKIFLAG(19)
3c205827 485#define SCM_EOL SCM_MAKIFLAG(20)
0f2d19dd
JB
486#define SCM_UNSPECIFIED SCM_MAKIFLAG(21)
487
488
489#define SCM_UNBNDP(x) (SCM_UNDEFINED==(x))
490
491\f
492
493/* Dispatching aids:
494 */
495
496
8ce94504 497/* For cons pairs with immediate values in the CAR
0f2d19dd
JB
498 */
499
500#define scm_tcs_cons_imcar 2:case 4:case 6:case 10:\
501 case 12:case 14:case 18:case 20:\
502 case 22:case 26:case 28:case 30:\
503 case 34:case 36:case 38:case 42:\
504 case 44:case 46:case 50:case 52:\
505 case 54:case 58:case 60:case 62:\
506 case 66:case 68:case 70:case 74:\
507 case 76:case 78:case 82:case 84:\
508 case 86:case 90:case 92:case 94:\
509 case 98:case 100:case 102:case 106:\
510 case 108:case 110:case 114:case 116:\
511 case 118:case 122:case 124:case 126
512
513/* For cons pairs with non-immediate values in the SCM_CAR
514 */
515#define scm_tcs_cons_nimcar 0:case 8:case 16:case 24:\
516 case 32:case 40:case 48:case 56:\
517 case 64:case 72:case 80:case 88:\
518 case 96:case 104:case 112:case 120
519
520/* A CONS_GLOC occurs in code. It's CAR is a pointer to the
521 * CDR of a variable. The low order bits of the CAR are 001.
522 * The CDR of the gloc is the code continuation.
523 */
524#define scm_tcs_cons_gloc 1:case 9:case 17:case 25:\
525 case 33:case 41:case 49:case 57:\
526 case 65:case 73:case 81:case 89:\
527 case 97:case 105:case 113:case 121
528
529#define scm_tcs_closures 3:case 11:case 19:case 27:\
530 case 35:case 43:case 51:case 59:\
531 case 67:case 75:case 83:case 91:\
532 case 99:case 107:case 115:case 123
533
534#define scm_tcs_subrs scm_tc7_asubr:case scm_tc7_subr_0:case scm_tc7_subr_1:case scm_tc7_cxr:\
535 case scm_tc7_subr_3:case scm_tc7_subr_2:case scm_tc7_rpsubr:case scm_tc7_subr_1o:\
536 case scm_tc7_subr_2o:case scm_tc7_lsubr_2:case scm_tc7_lsubr
537
538#define scm_tcs_symbols scm_tc7_ssymbol:case scm_tc7_msymbol
539
540#define scm_tcs_bignums scm_tc16_bigpos:case scm_tc16_bigneg
541
0f2d19dd 542#endif /* TAGSH */