* struct.h (scm_struct_i_setter): New constant.
[bpt/guile.git] / libguile / tags.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
3#ifndef TAGSH
4#define TAGSH
7dc6e754 5/* Copyright (C) 1995,1996,1997,1998 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
c2cb2500 193 * tc7 .........long length....Gxxxx1S1 ..........void *data............
0f2d19dd
JB
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 216 * Sometimes we choose the bottom seven bits carefully,
c2cb2500
JB
217 * so that the 2-valued bit (called S bit) can be masked
218 * off to reveal a common type.
8ce94504 219 *
0f2d19dd 220 * TYP7S(X) returns TYP7, but masking out the option bit S.
0f2d19dd 221 *
c2cb2500
JB
222 * For example, all strings have 0010 in the 'xxxx' bits
223 * in the diagram above, the S bit says whether it's a
b7f3516f 224 * substring.
8ce94504 225 *
0f2d19dd 226 * for example:
b7f3516f 227 * S
8ce94504 228 * scm_tc7_string = G0010101
527da704 229 * scm_tc7_substring = G0010111
8ce94504 230 *
c2cb2500 231 * TYP7S turns both string tags into tc7_string; thus,
b7f3516f 232 * testing TYP7S against tc7_string is a quick way to
c2cb2500 233 * test for any kind of string, shared or unshared.
0f2d19dd 234 *
0f2d19dd
JB
235 * Some TC7 types are subdivided into 256 subtypes giving
236 * rise to the macros:
237 *
238 * TYP16
239 * TYP16S
240 * GCTYP16
241 *
242 * TYP16S functions similarly wrt to TYP16 as TYP7S to TYP7,
243 * but a different option bit is used (bit 2 for TYP7S,
244 * bit 8 for TYP16S).
8ce94504 245 * */
0f2d19dd
JB
246
247
248
249\f
250/* {Non-immediate values.}
251 *
252 * If X is non-immediate, it is necessary to look at SCM_CAR (X) to
c2cb2500
JB
253 * figure out Xs type. X may be a cons pair, in which case the value
254 * SCM_CAR (x) will be either an immediate or non-immediate value. X
255 * may be something other than a cons pair, in which case the value
256 * SCM_CAR (x) will be a non-object value.
257 *
258 * All immediates and non-immediates have a 0 in bit 0. We
259 * additionally preserve the invariant that all non-object values
260 * stored in the SCM_CAR of a non-immediate object have a 1 in bit 1:
0f2d19dd
JB
261 */
262
263#define SCM_NCONSP(x) (1 & (int)SCM_CAR(x))
264#define SCM_CONSP(x) (!SCM_NCONSP(x))
265
266
ee4274a6
MD
267/* SCM_ECONSP should be used instead of SCM_CONSP at places where GLOCS
268 * can be expected to occur.
0f2d19dd 269 */
b2530d66
MD
270#define SCM_ECONSP(x) (SCM_CONSP (x) \
271 || (SCM_TYP3(x) == 1 \
272 && SCM_CDR (SCM_CAR (x) - 1) != 0))
273#define SCM_NECONSP(x) (SCM_NCONSP(x) \
274 && (SCM_TYP3(x) != 1 \
275 || SCM_CDR (SCM_CAR (x) - 1) == 0))
0f2d19dd
JB
276
277\f
278
279#define SCM_CELLP(x) (!SCM_NCELLP(x))
280#define SCM_NCELLP(x) ((sizeof(scm_cell)-1) & (int)(x))
281
8ce94504 282/* See numbers.h for macros relating to immediate integers.
0f2d19dd
JB
283 */
284
285#define SCM_ITAG3(x) (7 & (int)x)
286#define SCM_TYP3(x) (7 & (int)SCM_CAR(x))
287#define scm_tc3_cons 0
288#define scm_tc3_cons_gloc 1
289#define scm_tc3_int_1 2
290#define scm_tc3_closure 3
291#define scm_tc3_imm24 4
292#define scm_tc3_tc7_1 5
293#define scm_tc3_int_2 6
294#define scm_tc3_tc7_2 7
295
296
297/*
298 * Do not change the three bit tags.
299 */
300
301
c2cb2500
JB
302#define SCM_TYP7(x) ((int)SCM_CAR(x) & 0x7f)
303#define SCM_TYP7S(x) ((int)SCM_CAR(x) & (0x7f & ~2))
0f2d19dd
JB
304
305
306#define SCM_TYP16(x) (0xffff & (int)SCM_CAR(x))
307#define SCM_TYP16S(x) (0xfeff & (int)SCM_CAR(x))
308#define SCM_GCTYP16(x) (0xff7f & (int)SCM_CAR(x))
309
310
311
312/* Testing and Changing GC Marks in Various Standard Positions
313 */
314#define SCM_GCMARKP(x) (1 & (int)SCM_CDR(x))
315#define SCM_GC8MARKP(x) (0x80 & (int)SCM_CAR(x))
898a256f
MD
316#define SCM_SETGCMARK(x) SCM_SETOR_CDR (x,1)
317#define SCM_CLRGCMARK(x) SCM_SETAND_CDR (x, ~1L)
318#define SCM_SETGC8MARK(x) SCM_SETOR_CAR (x, 0x80)
319#define SCM_CLRGC8MARK(x) SCM_SETAND_CAR (x, ~0x80L)
0f2d19dd
JB
320
321
322\f
323
324/* couple */
325#define scm_tc7_ssymbol 5
326#define scm_tc7_msymbol 7
327
328/* couple */
329#define scm_tc7_vector 13
330#define scm_tc7_wvect 15
331
8a6d7c7c 332/* couple */
0f2d19dd 333#define scm_tc7_string 21
527da704 334#define scm_tc7_substring 23
0f2d19dd 335
8a6d7c7c
MD
336/* 29 and 31 are free! */
337
0f2d19dd
JB
338/* Many of the following should be turned
339 * into structs or smobs. We need back some
340 * of these 7 bit tags!
341 */
342#define scm_tc7_uvect 37
343#define scm_tc7_lvector 39
344#define scm_tc7_fvect 45
345#define scm_tc7_dvect 47
346#define scm_tc7_cvect 53
347#define scm_tc7_svect 55
348#define scm_tc7_contin 61
349#define scm_tc7_cclo 63
350#define scm_tc7_rpsubr 69
351#define scm_tc7_bvect 71
352#define scm_tc7_byvect 77
353#define scm_tc7_ivect 79
354#define scm_tc7_subr_0 85
355#define scm_tc7_subr_1 87
356#define scm_tc7_cxr 93
357#define scm_tc7_subr_3 95
358#define scm_tc7_subr_2 101
359#define scm_tc7_asubr 103
360#define scm_tc7_subr_1o 109
361#define scm_tc7_subr_2o 111
362#define scm_tc7_lsubr_2 117
363#define scm_tc7_lsubr 119
364
365
366/* There are 256 port subtypes. Here are the first four.
367 * These must agree with the init function in ports.c
368 */
369#define scm_tc7_port 125
370
371/* fports and pipes form an intended TYP16S equivelancy
372 * group (similar to a tc7 "couple".
373 */
374#define scm_tc16_fport (scm_tc7_port + 0*256L)
375#define scm_tc16_pipe (scm_tc7_port + 1*256L)
376
377#define scm_tc16_strport (scm_tc7_port + 2*256L)
378#define scm_tc16_sfport (scm_tc7_port + 3*256L)
379
380
381/* There are 256 smob subtypes. Here are the first four.
382 */
383
384#define scm_tc7_smob 127 /* DO NOT CHANGE [**] */
385
386/* [**] If you change scm_tc7_smob, you must also change
387 * the places it is hard coded in this file and possibly others.
388 */
389
390
ab256d39
JB
391/* scm_tc_free_cell is also the 0th smob type. We place this
392 * in free cells to tell the conservative marker not to trace it.
0f2d19dd
JB
393 */
394#define scm_tc_free_cell 127
395
396/* The 1st smob type:
397 */
398#define scm_tc16_flo 0x017f
399#define scm_tc_flo 0x017fL
400
401/* Some option bits begeinning at bit 16 of scm_tc16_flo:
402 */
403#define SCM_REAL_PART (1L<<16)
404#define SCM_IMAG_PART (2L<<16)
405#define scm_tc_dblr (scm_tc16_flo|SCM_REAL_PART)
406#define scm_tc_dblc (scm_tc16_flo|SCM_REAL_PART|SCM_IMAG_PART)
407
408
8ce94504 409/* Smob types 2 and 3:
0f2d19dd
JB
410 */
411#define scm_tc16_bigpos 0x027f
412#define scm_tc16_bigneg 0x037f
413
414
415\f
8ce94504 416/* {Immediate Values}
0f2d19dd
JB
417 */
418
419enum scm_tags
420{
421 scm_tc8_char = 0xf4,
4816f615 422 scm_tc8_iloc = 0xfc
0f2d19dd
JB
423};
424
425#define SCM_ITAG8(X) ((int)(X) & 0xff)
426#define SCM_MAKE_ITAG8(X, TAG) (((X)<<8) + TAG)
427#define SCM_ITAG8_DATA(X) ((X)>>8)
428
429
430\f
431/* Immediate Symbols, Special Symbols, Flags (various constants).
432 */
433
434/* SCM_ISYMP tests for ISPCSYM and ISYM */
435#define SCM_ISYMP(n) ((0x187 & (int)(n))==4)
436
437/* SCM_IFLAGP tests for ISPCSYM, ISYM and IFLAG */
438#define SCM_IFLAGP(n) ((0x87 & (int)(n))==4)
439#define SCM_ISYMNUM(n) ((int)((n)>>9))
08b5b88c 440#define SCM_ISYMCHARS(n) (scm_isymnames[SCM_ISYMNUM(n)])
0f2d19dd
JB
441#define SCM_MAKSPCSYM(n) (((n)<<9)+((n)<<3)+4L)
442#define SCM_MAKISYM(n) (((n)<<9)+0x74L)
443#define SCM_MAKIFLAG(n) (((n)<<9)+0x174L)
444
8ce94504 445/* This table must agree with the declarations
0f2d19dd
JB
446 * in repl.c: {Names of immediate symbols}.
447 *
448 * These are used only in eval but their values
449 * have to be allocated here.
450 *
451 */
452
453#define SCM_IM_AND SCM_MAKSPCSYM(0)
454#define SCM_IM_BEGIN SCM_MAKSPCSYM(1)
455#define SCM_IM_CASE SCM_MAKSPCSYM(2)
456#define SCM_IM_COND SCM_MAKSPCSYM(3)
457#define SCM_IM_DO SCM_MAKSPCSYM(4)
458#define SCM_IM_IF SCM_MAKSPCSYM(5)
459#define SCM_IM_LAMBDA SCM_MAKSPCSYM(6)
460#define SCM_IM_LET SCM_MAKSPCSYM(7)
461#define SCM_IM_LETSTAR SCM_MAKSPCSYM(8)
462#define SCM_IM_LETREC SCM_MAKSPCSYM(9)
463#define SCM_IM_OR SCM_MAKSPCSYM(10)
464#define SCM_IM_QUOTE SCM_MAKSPCSYM(11)
465#define SCM_IM_SET SCM_MAKSPCSYM(12)
466#define SCM_IM_DEFINE SCM_MAKSPCSYM(13)
467#define SCM_IM_APPLY SCM_MAKISYM(14)
468#define SCM_IM_CONT SCM_MAKISYM(15)
469#define SCM_BOOL_F SCM_MAKIFLAG(16)
470#define SCM_BOOL_T SCM_MAKIFLAG(17)
471#define SCM_UNDEFINED SCM_MAKIFLAG(18)
472#define SCM_EOF_VAL SCM_MAKIFLAG(19)
3c205827 473#define SCM_EOL SCM_MAKIFLAG(20)
0f2d19dd
JB
474#define SCM_UNSPECIFIED SCM_MAKIFLAG(21)
475
476
477#define SCM_UNBNDP(x) (SCM_UNDEFINED==(x))
478
479\f
480
481/* Dispatching aids:
482 */
483
484
8ce94504 485/* For cons pairs with immediate values in the CAR
0f2d19dd
JB
486 */
487
488#define scm_tcs_cons_imcar 2:case 4:case 6:case 10:\
489 case 12:case 14:case 18:case 20:\
490 case 22:case 26:case 28:case 30:\
491 case 34:case 36:case 38:case 42:\
492 case 44:case 46:case 50:case 52:\
493 case 54:case 58:case 60:case 62:\
494 case 66:case 68:case 70:case 74:\
495 case 76:case 78:case 82:case 84:\
496 case 86:case 90:case 92:case 94:\
497 case 98:case 100:case 102:case 106:\
498 case 108:case 110:case 114:case 116:\
499 case 118:case 122:case 124:case 126
500
501/* For cons pairs with non-immediate values in the SCM_CAR
502 */
503#define scm_tcs_cons_nimcar 0:case 8:case 16:case 24:\
504 case 32:case 40:case 48:case 56:\
505 case 64:case 72:case 80:case 88:\
506 case 96:case 104:case 112:case 120
507
508/* A CONS_GLOC occurs in code. It's CAR is a pointer to the
509 * CDR of a variable. The low order bits of the CAR are 001.
510 * The CDR of the gloc is the code continuation.
511 */
512#define scm_tcs_cons_gloc 1:case 9:case 17:case 25:\
513 case 33:case 41:case 49:case 57:\
514 case 65:case 73:case 81:case 89:\
515 case 97:case 105:case 113:case 121
516
517#define scm_tcs_closures 3:case 11:case 19:case 27:\
518 case 35:case 43:case 51:case 59:\
519 case 67:case 75:case 83:case 91:\
520 case 99:case 107:case 115:case 123
521
522#define scm_tcs_subrs scm_tc7_asubr:case scm_tc7_subr_0:case scm_tc7_subr_1:case scm_tc7_cxr:\
523 case scm_tc7_subr_3:case scm_tc7_subr_2:case scm_tc7_rpsubr:case scm_tc7_subr_1o:\
524 case scm_tc7_subr_2o:case scm_tc7_lsubr_2:case scm_tc7_lsubr
525
526#define scm_tcs_symbols scm_tc7_ssymbol:case scm_tc7_msymbol
527
528#define scm_tcs_bignums scm_tc16_bigpos:case scm_tc16_bigneg
529
0f2d19dd 530#endif /* TAGSH */