maintainer changed: was lord, now jimb; first import
[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.
6 *
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.
11 *
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.
16 *
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
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * As a special exception, the Free Software Foundation gives permission
22 * for additional uses of the text contained in its release of GUILE.
23 *
24 * The exception is that, if you link the GUILE library with other files
25 * to produce an executable, this does not by itself cause the
26 * resulting executable to be covered by the GNU General Public License.
27 * Your use of that executable is in no way restricted on account of
28 * linking the GUILE library code into it.
29 *
30 * This exception does not however invalidate any other reasons why
31 * the executable file might be covered by the GNU General Public License.
32 *
33 * This exception applies only to the code released by the
34 * Free Software Foundation under the name GUILE. If you copy
35 * code from other Free Software Foundation releases into a copy of
36 * GUILE, as the General Public License permits, the exception does
37 * not apply to the code that you add in this way. To avoid misleading
38 * anyone as to the status of such modified files, you must delete
39 * this exception notice from them.
40 *
41 * If you write modifications of your own for GUILE, it is your choice
42 * whether to permit this exception to apply to your modifications.
43 * If you do not wish that, delete this exception notice.
44 */
45\f
46
47/** This file defines the format of SCM values and cons pairs.
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 *
83 * Non-immediates -- meaning that the variable holds a (possibly tagged) pointer
84 * into the cons pair heap.
85 *
86 * Non-objects are distinguished from other values by careful coding only (i.e.,
87 * programmers must keep track of any SCM variables they create that don't contain
88 * ordinary scheme values).
89 *
90 * All immediates and non-immediates must have a 0 in bit 0. Only non-object
91 * values can have a 1 in bit 0. In some cases, bit 0 of a word in the heap
92 * is used for the GC tag so during garbage collection, that bit might be 1
93 * even in an immediate or non-immediate value. In other cases, bit 0 of a word
94 * in the heap is used to tag a pointer to a GLOC (VM global variable address)
95 * or the header of a struct. But whenever an SCM variable holds a normal Scheme
96 * value, bit 0 is 0.
97 *
98 * Immediates and non-immediates are distinguished by bits two and four.
99 * Immediate values must have a 1 in at least one of those bits. Does
100 * this (or any other detail of tagging) seem arbitrary? Try chaning it!
101 * (Not always impossible but it is fair to say that many details of tags
102 * are mutually dependent).
103 */
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
109 * SCM variables or in the heap.
110 *
111 * low bits meaning
112 *
113 *
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:
137 *
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 *
155 * Some of the 0110100 immediates are long instructions (they dispatch
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 *
199 * tc7_tags are 7 bit tags ending in 1x1. These tags occur
200 * only in the CAR of heap cells.
201 *
202 * SCM_LENGTH returns the bits in "length" (see the diagram).
203 * SCM_CHARS returns the data cast to "char *"
204 * SCM_CDR returns the data cast to "SCM"
205 * TYP7(X) returns bits 0...6 of SCM_CAR (X)
206 *
207 * For the interpretation of SCM_LENGTH and SCM_CHARS
208 * that applies to a particular type, see the header file
209 * for that type.
210 *
211 * TYP7S(X) returns TYP7, but masking out the option bit S.
212 * TYP7D(X) returns TYP7, but masking out the option bit D.
213 * TYP7SD(X) masks out both option bits.
214 *
215 * for example:
216 * D S
217 * scm_tc7_string = Gxxx0101
218 * scm_tc7_mb_string = Gxxx0111
219 * scm_tc7_substring = Gxxx1101
220 * scm_tc7_mb_substring = Gxxx1111
221 *
222 * TYP7S turns tc7_mb_string into tc7_string and
223 * tc7_mb_substring into tc7_substring.
224 *
225 * TYP7D turns tc7_mb_substring into tc7_mb_string and
226 * tc7_substring into tc7_string.
227 *
228 * TYP7DS turns all string tags into tc7_string.
229 *
230 * Some TC7 types are subdivided into 256 subtypes giving
231 * rise to the macros:
232 *
233 * TYP16
234 * TYP16S
235 * GCTYP16
236 *
237 * TYP16S functions similarly wrt to TYP16 as TYP7S to TYP7,
238 * but a different option bit is used (bit 2 for TYP7S,
239 * bit 8 for TYP16S).
240 *
241 */
242
243
244
245\f
246/* {Non-immediate values.}
247 *
248 * If X is non-immediate, it is necessary to look at SCM_CAR (X) to
249 * figure out Xs type. X may be a cons pair, in which case the
250 * value SCM_CAR (x) will be either an immediate or non-immediate value.
251 * X may be something other than a cons pair, in which case the value SCM_CAR (x)
252 * will be a non-object value.
253 *
254 * All immediates and non-immediates have a 0 in bit 0. We additionally preserve
255 * the invariant that all non-object values stored in the SCM_CAR of a non-immediate
256 * object have a 1 in bit 1:
257 */
258
259#define SCM_NCONSP(x) (1 & (int)SCM_CAR(x))
260#define SCM_CONSP(x) (!SCM_NCONSP(x))
261
262
263/* ECONSP is historical and, in fact, slightly buggy.
264 * There are two places to fix where structures and glocs can be confused.
265 * !!!
266 */
267#define SCM_ECONSP(x) (SCM_CONSP(x) || (1==SCM_TYP3(x)))
268#define SCM_NECONSP(x) (SCM_NCONSP(x) && (1 != SCM_TYP3(x)))
269
270\f
271
272#define SCM_CELLP(x) (!SCM_NCELLP(x))
273#define SCM_NCELLP(x) ((sizeof(scm_cell)-1) & (int)(x))
274
275/* See numbers.h for macros relating to immediate integers.
276 */
277
278#define SCM_ITAG3(x) (7 & (int)x)
279#define SCM_TYP3(x) (7 & (int)SCM_CAR(x))
280#define scm_tc3_cons 0
281#define scm_tc3_cons_gloc 1
282#define scm_tc3_int_1 2
283#define scm_tc3_closure 3
284#define scm_tc3_imm24 4
285#define scm_tc3_tc7_1 5
286#define scm_tc3_int_2 6
287#define scm_tc3_tc7_2 7
288
289
290/*
291 * Do not change the three bit tags.
292 */
293
294
295#define SCM_TYP7(x) (0x7f & (int)SCM_CAR(x))
296#define SCM_TYP7S(x) (0x7d & (int)SCM_CAR(x))
297#define SCM_TYP7SD(x) (0x75 & (int)SCM_CAR(x))
298#define SCM_TYP7D(x) (0x77 & (int)SCM_CAR(x))
299
300
301#define SCM_TYP16(x) (0xffff & (int)SCM_CAR(x))
302#define SCM_TYP16S(x) (0xfeff & (int)SCM_CAR(x))
303#define SCM_GCTYP16(x) (0xff7f & (int)SCM_CAR(x))
304
305
306
307/* Testing and Changing GC Marks in Various Standard Positions
308 */
309#define SCM_GCMARKP(x) (1 & (int)SCM_CDR(x))
310#define SCM_GC8MARKP(x) (0x80 & (int)SCM_CAR(x))
311#define SCM_SETGCMARK(x) (SCM_CDR(x) |= 1)
312#define SCM_CLRGCMARK(x) (SCM_CDR(x) &= ~1L)
313#define SCM_SETGC8MARK(x) (SCM_CAR(x) |= 0x80)
314#define SCM_CLRGC8MARK(x) (SCM_CAR(x) &= ~0x80L)
315
316
317\f
318
319/* couple */
320#define scm_tc7_ssymbol 5
321#define scm_tc7_msymbol 7
322
323/* couple */
324#define scm_tc7_vector 13
325#define scm_tc7_wvect 15
326
327/* a quad, two couples, two trists */
328#define scm_tc7_string 21
329#define scm_tc7_mb_string 23
330#define scm_tc7_substring 29
331#define scm_tc7_mb_substring 31
332
333/* Many of the following should be turned
334 * into structs or smobs. We need back some
335 * of these 7 bit tags!
336 */
337#define scm_tc7_uvect 37
338#define scm_tc7_lvector 39
339#define scm_tc7_fvect 45
340#define scm_tc7_dvect 47
341#define scm_tc7_cvect 53
342#define scm_tc7_svect 55
343#define scm_tc7_contin 61
344#define scm_tc7_cclo 63
345#define scm_tc7_rpsubr 69
346#define scm_tc7_bvect 71
347#define scm_tc7_byvect 77
348#define scm_tc7_ivect 79
349#define scm_tc7_subr_0 85
350#define scm_tc7_subr_1 87
351#define scm_tc7_cxr 93
352#define scm_tc7_subr_3 95
353#define scm_tc7_subr_2 101
354#define scm_tc7_asubr 103
355#define scm_tc7_subr_1o 109
356#define scm_tc7_subr_2o 111
357#define scm_tc7_lsubr_2 117
358#define scm_tc7_lsubr 119
359
360
361/* There are 256 port subtypes. Here are the first four.
362 * These must agree with the init function in ports.c
363 */
364#define scm_tc7_port 125
365
366/* fports and pipes form an intended TYP16S equivelancy
367 * group (similar to a tc7 "couple".
368 */
369#define scm_tc16_fport (scm_tc7_port + 0*256L)
370#define scm_tc16_pipe (scm_tc7_port + 1*256L)
371
372#define scm_tc16_strport (scm_tc7_port + 2*256L)
373#define scm_tc16_sfport (scm_tc7_port + 3*256L)
374
375
376/* There are 256 smob subtypes. Here are the first four.
377 */
378
379#define scm_tc7_smob 127 /* DO NOT CHANGE [**] */
380
381/* [**] If you change scm_tc7_smob, you must also change
382 * the places it is hard coded in this file and possibly others.
383 */
384
385
386/* scm_tc_free_cell is also the 0th smob type.
387 */
388#define scm_tc_free_cell 127
389
390/* The 1st smob type:
391 */
392#define scm_tc16_flo 0x017f
393#define scm_tc_flo 0x017fL
394
395/* Some option bits begeinning at bit 16 of scm_tc16_flo:
396 */
397#define SCM_REAL_PART (1L<<16)
398#define SCM_IMAG_PART (2L<<16)
399#define scm_tc_dblr (scm_tc16_flo|SCM_REAL_PART)
400#define scm_tc_dblc (scm_tc16_flo|SCM_REAL_PART|SCM_IMAG_PART)
401
402
403/* Smob types 2 and 3:
404 */
405#define scm_tc16_bigpos 0x027f
406#define scm_tc16_bigneg 0x037f
407
408
409\f
410/* {Immediate Values}
411 */
412
413enum scm_tags
414{
415 scm_tc8_char = 0xf4,
416 scm_tc8_iloc = 0xfc,
417};
418
419#define SCM_ITAG8(X) ((int)(X) & 0xff)
420#define SCM_MAKE_ITAG8(X, TAG) (((X)<<8) + TAG)
421#define SCM_ITAG8_DATA(X) ((X)>>8)
422
423
424\f
425/* Immediate Symbols, Special Symbols, Flags (various constants).
426 */
427
428/* SCM_ISYMP tests for ISPCSYM and ISYM */
429#define SCM_ISYMP(n) ((0x187 & (int)(n))==4)
430
431/* SCM_IFLAGP tests for ISPCSYM, ISYM and IFLAG */
432#define SCM_IFLAGP(n) ((0x87 & (int)(n))==4)
433#define SCM_ISYMNUM(n) ((int)((n)>>9))
434#define SCM_ISYMSCM_CHARS(n) (scm_isymnames[SCM_ISYMNUM(n)])
435#define SCM_MAKSPCSYM(n) (((n)<<9)+((n)<<3)+4L)
436#define SCM_MAKISYM(n) (((n)<<9)+0x74L)
437#define SCM_MAKIFLAG(n) (((n)<<9)+0x174L)
438
439/* This table must agree with the declarations
440 * in repl.c: {Names of immediate symbols}.
441 *
442 * These are used only in eval but their values
443 * have to be allocated here.
444 *
445 */
446
447#define SCM_IM_AND SCM_MAKSPCSYM(0)
448#define SCM_IM_BEGIN SCM_MAKSPCSYM(1)
449#define SCM_IM_CASE SCM_MAKSPCSYM(2)
450#define SCM_IM_COND SCM_MAKSPCSYM(3)
451#define SCM_IM_DO SCM_MAKSPCSYM(4)
452#define SCM_IM_IF SCM_MAKSPCSYM(5)
453#define SCM_IM_LAMBDA SCM_MAKSPCSYM(6)
454#define SCM_IM_LET SCM_MAKSPCSYM(7)
455#define SCM_IM_LETSTAR SCM_MAKSPCSYM(8)
456#define SCM_IM_LETREC SCM_MAKSPCSYM(9)
457#define SCM_IM_OR SCM_MAKSPCSYM(10)
458#define SCM_IM_QUOTE SCM_MAKSPCSYM(11)
459#define SCM_IM_SET SCM_MAKSPCSYM(12)
460#define SCM_IM_DEFINE SCM_MAKSPCSYM(13)
461#define SCM_IM_APPLY SCM_MAKISYM(14)
462#define SCM_IM_CONT SCM_MAKISYM(15)
463#define SCM_BOOL_F SCM_MAKIFLAG(16)
464#define SCM_BOOL_T SCM_MAKIFLAG(17)
465#define SCM_UNDEFINED SCM_MAKIFLAG(18)
466#define SCM_EOF_VAL SCM_MAKIFLAG(19)
467#define SCM_UNUSED_NIL_VALUE SCM_MAKIFLAG(20)
468#define SCM_UNSPECIFIED SCM_MAKIFLAG(21)
469
470
471#define SCM_UNBNDP(x) (SCM_UNDEFINED==(x))
472
473\f
474
475/* Dispatching aids:
476 */
477
478
479/* For cons pairs with immediate values in the CAR
480 */
481
482#define scm_tcs_cons_imcar 2:case 4:case 6:case 10:\
483 case 12:case 14:case 18:case 20:\
484 case 22:case 26:case 28:case 30:\
485 case 34:case 36:case 38:case 42:\
486 case 44:case 46:case 50:case 52:\
487 case 54:case 58:case 60:case 62:\
488 case 66:case 68:case 70:case 74:\
489 case 76:case 78:case 82:case 84:\
490 case 86:case 90:case 92:case 94:\
491 case 98:case 100:case 102:case 106:\
492 case 108:case 110:case 114:case 116:\
493 case 118:case 122:case 124:case 126
494
495/* For cons pairs with non-immediate values in the SCM_CAR
496 */
497#define scm_tcs_cons_nimcar 0:case 8:case 16:case 24:\
498 case 32:case 40:case 48:case 56:\
499 case 64:case 72:case 80:case 88:\
500 case 96:case 104:case 112:case 120
501
502/* A CONS_GLOC occurs in code. It's CAR is a pointer to the
503 * CDR of a variable. The low order bits of the CAR are 001.
504 * The CDR of the gloc is the code continuation.
505 */
506#define scm_tcs_cons_gloc 1:case 9:case 17:case 25:\
507 case 33:case 41:case 49:case 57:\
508 case 65:case 73:case 81:case 89:\
509 case 97:case 105:case 113:case 121
510
511#define scm_tcs_closures 3:case 11:case 19:case 27:\
512 case 35:case 43:case 51:case 59:\
513 case 67:case 75:case 83:case 91:\
514 case 99:case 107:case 115:case 123
515
516#define scm_tcs_subrs scm_tc7_asubr:case scm_tc7_subr_0:case scm_tc7_subr_1:case scm_tc7_cxr:\
517 case scm_tc7_subr_3:case scm_tc7_subr_2:case scm_tc7_rpsubr:case scm_tc7_subr_1o:\
518 case scm_tc7_subr_2o:case scm_tc7_lsubr_2:case scm_tc7_lsubr
519
520#define scm_tcs_symbols scm_tc7_ssymbol:case scm_tc7_msymbol
521
522#define scm_tcs_bignums scm_tc16_bigpos:case scm_tc16_bigneg
523
524\f
525#ifdef __STDC__
526
527#else /* STDC */
528
529#endif /* STDC */
530
531
532#endif /* TAGSH */