Big reorganization of the whole manual to give it a simpler structure.
[bpt/guile.git] / doc / ref / data-rep.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @c essay \input texinfo
8 @c essay @c -*-texinfo-*-
9 @c essay @c %**start of header
10 @c essay @setfilename data-rep.info
11 @c essay @settitle Data Representation in Guile
12 @c essay @c %**end of header
13
14 @c essay @include version.texi
15
16 @c essay @dircategory The Algorithmic Language Scheme
17 @c essay @direntry
18 @c essay * data-rep: (data-rep). Data Representation in Guile --- how to use
19 @c essay Guile objects in your C code.
20 @c essay @end direntry
21
22 @c essay @setchapternewpage off
23
24 @c essay @ifinfo
25 @c essay Data Representation in Guile
26
27 @c essay Copyright (C) 1998, 1999, 2000, 2003 Free Software Foundation
28
29 @c essay Permission is granted to make and distribute verbatim copies of
30 @c essay this manual provided the copyright notice and this permission notice
31 @c essay are preserved on all copies.
32
33 @c essay @ignore
34 @c essay Permission is granted to process this file through TeX and print the
35 @c essay results, provided the printed document carries copying permission
36 @c essay notice identical to this one except for the removal of this paragraph
37 @c essay (this paragraph not being relevant to the printed manual).
38 @c essay @end ignore
39
40 @c essay Permission is granted to copy and distribute modified versions of this
41 @c essay manual under the conditions for verbatim copying, provided that the entire
42 @c essay resulting derived work is distributed under the terms of a permission
43 @c essay notice identical to this one.
44
45 @c essay Permission is granted to copy and distribute translations of this manual
46 @c essay into another language, under the above conditions for modified versions,
47 @c essay except that this permission notice may be stated in a translation approved
48 @c essay by the Free Software Foundation.
49 @c essay @end ifinfo
50
51 @c essay @titlepage
52 @c essay @sp 10
53 @c essay @comment The title is printed in a large font.
54 @c essay @title Data Representation in Guile
55 @c essay @subtitle $Id: data-rep.texi,v 1.17 2004-04-21 14:32:08 mvo Exp $
56 @c essay @subtitle For use with Guile @value{VERSION}
57 @c essay @author Jim Blandy
58 @c essay @author Free Software Foundation
59 @c essay @author @email{jimb@@red-bean.com}
60 @c essay @c The following two commands start the copyright page.
61 @c essay @page
62 @c essay @vskip 0pt plus 1filll
63 @c essay @vskip 0pt plus 1filll
64 @c essay Copyright @copyright{} 1998 Free Software Foundation
65
66 @c essay Permission is granted to make and distribute verbatim copies of
67 @c essay this manual provided the copyright notice and this permission notice
68 @c essay are preserved on all copies.
69
70 @c essay Permission is granted to copy and distribute modified versions of this
71 @c essay manual under the conditions for verbatim copying, provided that the entire
72 @c essay resulting derived work is distributed under the terms of a permission
73 @c essay notice identical to this one.
74
75 @c essay Permission is granted to copy and distribute translations of this manual
76 @c essay into another language, under the above conditions for modified versions,
77 @c essay except that this permission notice may be stated in a translation approved
78 @c essay by Free Software Foundation.
79 @c essay @end titlepage
80
81 @c essay @c @smallbook
82 @c essay @c @finalout
83 @c essay @headings double
84
85
86 @c essay @node Top, Data Representation in Scheme, (dir), (dir)
87 @c essay @top Data Representation in Guile
88
89 @c essay @ifinfo
90 @c essay This essay is meant to provide the background necessary to read and
91 @c essay write C code that manipulates Scheme values in a way that conforms to
92 @c essay libguile's interface. If you would like to write or maintain a
93 @c essay Guile-based application in C or C++, this is the first information you
94 @c essay need.
95
96 @c essay In order to make sense of Guile's @code{SCM_} functions, or read
97 @c essay libguile's source code, it's essential to have a good grasp of how Guile
98 @c essay actually represents Scheme values. Otherwise, a lot of the code, and
99 @c essay the conventions it follows, won't make very much sense.
100
101 @c essay We assume you know both C and Scheme, but we do not assume you are
102 @c essay familiar with Guile's C interface.
103 @c essay @end ifinfo
104
105
106 @node Data Representation
107 @appendix Data Representation in Guile
108
109 @strong{by Jim Blandy}
110
111 [Due to the rather non-orthogonal and performance-oriented nature of the
112 SCM interface, you need to understand SCM internals *before* you can use
113 the SCM API. That's why this chapter comes first.]
114
115 [NOTE: this is Jim Blandy's essay almost entirely unmodified. It has to
116 be adapted to fit this manual smoothly.]
117
118 In order to make sense of Guile's SCM_ functions, or read libguile's
119 source code, it's essential to have a good grasp of how Guile actually
120 represents Scheme values. Otherwise, a lot of the code, and the
121 conventions it follows, won't make very much sense. This essay is meant
122 to provide the background necessary to read and write C code that
123 manipulates Scheme values in a way that is compatible with libguile.
124
125 We assume you know both C and Scheme, but we do not assume you are
126 familiar with Guile's implementation.
127
128 @menu
129 * Data Representation in Scheme:: Why things aren't just totally
130 straightforward, in general terms.
131 * How Guile does it:: How to write C code that manipulates
132 Guile values, with an explanation
133 of Guile's garbage collector.
134 @end menu
135
136 @node Data Representation in Scheme
137 @section Data Representation in Scheme
138
139 Scheme is a latently-typed language; this means that the system cannot,
140 in general, determine the type of a given expression at compile time.
141 Types only become apparent at run time. Variables do not have fixed
142 types; a variable may hold a pair at one point, an integer at the next,
143 and a thousand-element vector later. Instead, values, not variables,
144 have fixed types.
145
146 In order to implement standard Scheme functions like @code{pair?} and
147 @code{string?} and provide garbage collection, the representation of
148 every value must contain enough information to accurately determine its
149 type at run time. Often, Scheme systems also use this information to
150 determine whether a program has attempted to apply an operation to an
151 inappropriately typed value (such as taking the @code{car} of a string).
152
153 Because variables, pairs, and vectors may hold values of any type,
154 Scheme implementations use a uniform representation for values --- a
155 single type large enough to hold either a complete value or a pointer
156 to a complete value, along with the necessary typing information.
157
158 The following sections will present a simple typing system, and then
159 make some refinements to correct its major weaknesses. However, this is
160 not a description of the system Guile actually uses. It is only an
161 illustration of the issues Guile's system must address. We provide all
162 the information one needs to work with Guile's data in @ref{How Guile
163 does it}.
164
165
166 @menu
167 * A Simple Representation::
168 * Faster Integers::
169 * Cheaper Pairs::
170 * Guile Is Hairier::
171 @end menu
172
173 @node A Simple Representation
174 @subsection A Simple Representation
175
176 The simplest way to meet the above requirements in C would be to
177 represent each value as a pointer to a structure containing a type
178 indicator, followed by a union carrying the real value. Assuming that
179 @code{SCM} is the name of our universal type, we can write:
180
181 @example
182 enum type @{ integer, pair, string, vector, ... @};
183
184 typedef struct value *SCM;
185
186 struct value @{
187 enum type type;
188 union @{
189 int integer;
190 struct @{ SCM car, cdr; @} pair;
191 struct @{ int length; char *elts; @} string;
192 struct @{ int length; SCM *elts; @} vector;
193 ...
194 @} value;
195 @};
196 @end example
197 with the ellipses replaced with code for the remaining Scheme types.
198
199 This representation is sufficient to implement all of Scheme's
200 semantics. If @var{x} is an @code{SCM} value:
201 @itemize @bullet
202 @item
203 To test if @var{x} is an integer, we can write @code{@var{x}->type == integer}.
204 @item
205 To find its value, we can write @code{@var{x}->value.integer}.
206 @item
207 To test if @var{x} is a vector, we can write @code{@var{x}->type == vector}.
208 @item
209 If we know @var{x} is a vector, we can write
210 @code{@var{x}->value.vector.elts[0]} to refer to its first element.
211 @item
212 If we know @var{x} is a pair, we can write
213 @code{@var{x}->value.pair.car} to extract its car.
214 @end itemize
215
216
217 @node Faster Integers
218 @subsection Faster Integers
219
220 Unfortunately, the above representation has a serious disadvantage. In
221 order to return an integer, an expression must allocate a @code{struct
222 value}, initialize it to represent that integer, and return a pointer to
223 it. Furthermore, fetching an integer's value requires a memory
224 reference, which is much slower than a register reference on most
225 processors. Since integers are extremely common, this representation is
226 too costly, in both time and space. Integers should be very cheap to
227 create and manipulate.
228
229 One possible solution comes from the observation that, on many
230 architectures, structures must be aligned on a four-byte boundary.
231 (Whether or not the machine actually requires it, we can write our own
232 allocator for @code{struct value} objects that assures this is true.)
233 In this case, the lower two bits of the structure's address are known to
234 be zero.
235
236 This gives us the room we need to provide an improved representation
237 for integers. We make the following rules:
238 @itemize @bullet
239 @item
240 If the lower two bits of an @code{SCM} value are zero, then the SCM
241 value is a pointer to a @code{struct value}, and everything proceeds as
242 before.
243 @item
244 Otherwise, the @code{SCM} value represents an integer, whose value
245 appears in its upper bits.
246 @end itemize
247
248 Here is C code implementing this convention:
249 @example
250 enum type @{ pair, string, vector, ... @};
251
252 typedef struct value *SCM;
253
254 struct value @{
255 enum type type;
256 union @{
257 struct @{ SCM car, cdr; @} pair;
258 struct @{ int length; char *elts; @} string;
259 struct @{ int length; SCM *elts; @} vector;
260 ...
261 @} value;
262 @};
263
264 #define POINTER_P(x) (((int) (x) & 3) == 0)
265 #define INTEGER_P(x) (! POINTER_P (x))
266
267 #define GET_INTEGER(x) ((int) (x) >> 2)
268 #define MAKE_INTEGER(x) ((SCM) (((x) << 2) | 1))
269 @end example
270
271 Notice that @code{integer} no longer appears as an element of @code{enum
272 type}, and the union has lost its @code{integer} member. Instead, we
273 use the @code{POINTER_P} and @code{INTEGER_P} macros to make a coarse
274 classification of values into integers and non-integers, and do further
275 type testing as before.
276
277 Here's how we would answer the questions posed above (again, assume
278 @var{x} is an @code{SCM} value):
279 @itemize @bullet
280 @item
281 To test if @var{x} is an integer, we can write @code{INTEGER_P (@var{x})}.
282 @item
283 To find its value, we can write @code{GET_INTEGER (@var{x})}.
284 @item
285 To test if @var{x} is a vector, we can write:
286 @example
287 @code{POINTER_P (@var{x}) && @var{x}->type == vector}
288 @end example
289 Given the new representation, we must make sure @var{x} is truly a
290 pointer before we dereference it to determine its complete type.
291 @item
292 If we know @var{x} is a vector, we can write
293 @code{@var{x}->value.vector.elts[0]} to refer to its first element, as
294 before.
295 @item
296 If we know @var{x} is a pair, we can write
297 @code{@var{x}->value.pair.car} to extract its car, just as before.
298 @end itemize
299
300 This representation allows us to operate more efficiently on integers
301 than the first. For example, if @var{x} and @var{y} are known to be
302 integers, we can compute their sum as follows:
303 @example
304 MAKE_INTEGER (GET_INTEGER (@var{x}) + GET_INTEGER (@var{y}))
305 @end example
306 Now, integer math requires no allocation or memory references. Most
307 real Scheme systems actually use an even more efficient representation,
308 but this essay isn't about bit-twiddling. (Hint: what if pointers had
309 @code{01} in their least significant bits, and integers had @code{00}?)
310
311
312 @node Cheaper Pairs
313 @subsection Cheaper Pairs
314
315 However, there is yet another issue to confront. Most Scheme heaps
316 contain more pairs than any other type of object; Jonathan Rees says
317 that pairs occupy 45% of the heap in his Scheme implementation, Scheme
318 48. However, our representation above spends three @code{SCM}-sized
319 words per pair --- one for the type, and two for the @sc{car} and
320 @sc{cdr}. Is there any way to represent pairs using only two words?
321
322 Let us refine the convention we established earlier. Let us assert
323 that:
324 @itemize @bullet
325 @item
326 If the bottom two bits of an @code{SCM} value are @code{#b00}, then
327 it is a pointer, as before.
328 @item
329 If the bottom two bits are @code{#b01}, then the upper bits are an
330 integer. This is a bit more restrictive than before.
331 @item
332 If the bottom two bits are @code{#b10}, then the value, with the bottom
333 two bits masked out, is the address of a pair.
334 @end itemize
335
336 Here is the new C code:
337 @example
338 enum type @{ string, vector, ... @};
339
340 typedef struct value *SCM;
341
342 struct value @{
343 enum type type;
344 union @{
345 struct @{ int length; char *elts; @} string;
346 struct @{ int length; SCM *elts; @} vector;
347 ...
348 @} value;
349 @};
350
351 struct pair @{
352 SCM car, cdr;
353 @};
354
355 #define POINTER_P(x) (((int) (x) & 3) == 0)
356
357 #define INTEGER_P(x) (((int) (x) & 3) == 1)
358 #define GET_INTEGER(x) ((int) (x) >> 2)
359 #define MAKE_INTEGER(x) ((SCM) (((x) << 2) | 1))
360
361 #define PAIR_P(x) (((int) (x) & 3) == 2)
362 #define GET_PAIR(x) ((struct pair *) ((int) (x) & ~3))
363 @end example
364
365 Notice that @code{enum type} and @code{struct value} now only contain
366 provisions for vectors and strings; both integers and pairs have become
367 special cases. The code above also assumes that an @code{int} is large
368 enough to hold a pointer, which isn't generally true.
369
370
371 Our list of examples is now as follows:
372 @itemize @bullet
373 @item
374 To test if @var{x} is an integer, we can write @code{INTEGER_P
375 (@var{x})}; this is as before.
376 @item
377 To find its value, we can write @code{GET_INTEGER (@var{x})}, as
378 before.
379 @item
380 To test if @var{x} is a vector, we can write:
381 @example
382 @code{POINTER_P (@var{x}) && @var{x}->type == vector}
383 @end example
384 We must still make sure that @var{x} is a pointer to a @code{struct
385 value} before dereferencing it to find its type.
386 @item
387 If we know @var{x} is a vector, we can write
388 @code{@var{x}->value.vector.elts[0]} to refer to its first element, as
389 before.
390 @item
391 We can write @code{PAIR_P (@var{x})} to determine if @var{x} is a
392 pair, and then write @code{GET_PAIR (@var{x})->car} to refer to its
393 car.
394 @end itemize
395
396 This change in representation reduces our heap size by 15%. It also
397 makes it cheaper to decide if a value is a pair, because no memory
398 references are necessary; it suffices to check the bottom two bits of
399 the @code{SCM} value. This may be significant when traversing lists, a
400 common activity in a Scheme system.
401
402 Again, most real Scheme systems use a slightly different implementation;
403 for example, if GET_PAIR subtracts off the low bits of @code{x}, instead
404 of masking them off, the optimizer will often be able to combine that
405 subtraction with the addition of the offset of the structure member we
406 are referencing, making a modified pointer as fast to use as an
407 unmodified pointer.
408
409
410 @node Guile Is Hairier
411 @subsection Guile Is Hairier
412
413 We originally started with a very simple typing system --- each object
414 has a field that indicates its type. Then, for the sake of efficiency
415 in both time and space, we moved some of the typing information directly
416 into the @code{SCM} value, and left the rest in the @code{struct value}.
417 Guile itself employs a more complex hierarchy, storing finer and finer
418 gradations of type information in different places, depending on the
419 object's coarser type.
420
421 In the author's opinion, Guile could be simplified greatly without
422 significant loss of efficiency, but the simplified system would still be
423 more complex than what we've presented above.
424
425
426 @node How Guile does it
427 @section How Guile does it
428
429 Here we present the specifics of how Guile represents its data. We
430 don't go into complete detail; an exhaustive description of Guile's
431 system would be boring, and we do not wish to encourage people to write
432 code which depends on its details anyway. We do, however, present
433 everything one need know to use Guile's data.
434
435
436 @menu
437 * General Rules::
438 * Conservative GC::
439 * Immediates vs Non-immediates::
440 * Immediate Datatypes::
441 * Non-immediate Datatypes::
442 * Signalling Type Errors::
443 * Unpacking the SCM type::
444 @end menu
445
446 @node General Rules
447 @subsection General Rules
448
449 Any code which operates on Guile datatypes must @code{#include} the
450 header file @code{<libguile.h>}. This file contains a definition for
451 the @code{SCM} typedef (Guile's universal type, as in the examples
452 above), and definitions and declarations for a host of macros and
453 functions that operate on @code{SCM} values.
454
455 All identifiers declared by @code{<libguile.h>} begin with @code{scm_}
456 or @code{SCM_}.
457
458 @c [[I wish this were true, but I don't think it is at the moment. -JimB]]
459 @c Macros do not evaluate their arguments more than once, unless documented
460 @c to do so.
461
462 The functions described here generally check the types of their
463 @code{SCM} arguments, and signal an error if their arguments are of an
464 inappropriate type. Macros generally do not, unless that is their
465 specified purpose. You must verify their argument types beforehand, as
466 necessary.
467
468 Macros and functions that return a boolean value have names ending in
469 @code{P} or @code{_p} (for ``predicate''). Those that return a negated
470 boolean value have names starting with @code{SCM_N}. For example,
471 @code{SCM_IMP (@var{x})} is a predicate which returns non-zero iff
472 @var{x} is an immediate value (an @code{IM}). @code{SCM_NCONSP
473 (@var{x})} is a predicate which returns non-zero iff @var{x} is
474 @emph{not} a pair object (a @code{CONS}).
475
476
477 @node Conservative GC
478 @subsection Conservative Garbage Collection
479
480 Aside from the latent typing, the major source of constraints on a
481 Scheme implementation's data representation is the garbage collector.
482 The collector must be able to traverse every live object in the heap, to
483 determine which objects are not live.
484
485 There are many ways to implement this, but Guile uses an algorithm
486 called @dfn{mark and sweep}. The collector scans the system's global
487 variables and the local variables on the stack to determine which
488 objects are immediately accessible by the C code. It then scans those
489 objects to find the objects they point to, @i{et cetera}. The collector
490 sets a @dfn{mark bit} on each object it finds, so each object is
491 traversed only once. This process is called @dfn{tracing}.
492
493 When the collector can find no unmarked objects pointed to by marked
494 objects, it assumes that any objects that are still unmarked will never
495 be used by the program (since there is no path of dereferences from any
496 global or local variable that reaches them) and deallocates them.
497
498 In the above paragraphs, we did not specify how the garbage collector
499 finds the global and local variables; as usual, there are many different
500 approaches. Frequently, the programmer must maintain a list of pointers
501 to all global variables that refer to the heap, and another list
502 (adjusted upon entry to and exit from each function) of local variables,
503 for the collector's benefit.
504
505 The list of global variables is usually not too difficult to maintain,
506 since global variables are relatively rare. However, an explicitly
507 maintained list of local variables (in the author's personal experience)
508 is a nightmare to maintain. Thus, Guile uses a technique called
509 @dfn{conservative garbage collection}, to make the local variable list
510 unnecessary.
511
512 The trick to conservative collection is to treat the stack as an
513 ordinary range of memory, and assume that @emph{every} word on the stack
514 is a pointer into the heap. Thus, the collector marks all objects whose
515 addresses appear anywhere in the stack, without knowing for sure how
516 that word is meant to be interpreted.
517
518 Obviously, such a system will occasionally retain objects that are
519 actually garbage, and should be freed. In practice, this is not a
520 problem. The alternative, an explicitly maintained list of local
521 variable addresses, is effectively much less reliable, due to programmer
522 error.
523
524 To accommodate this technique, data must be represented so that the
525 collector can accurately determine whether a given stack word is a
526 pointer or not. Guile does this as follows:
527
528 @itemize @bullet
529 @item
530 Every heap object has a two-word header, called a @dfn{cell}. Some
531 objects, like pairs, fit entirely in a cell's two words; others may
532 store pointers to additional memory in either of the words. For
533 example, strings and vectors store their length in the first word, and a
534 pointer to their elements in the second.
535
536 @item
537 Guile allocates whole arrays of cells at a time, called @dfn{heap
538 segments}. These segments are always allocated so that the cells they
539 contain fall on eight-byte boundaries, or whatever is appropriate for
540 the machine's word size. Guile keeps all cells in a heap segment
541 initialized, whether or not they are currently in use.
542
543 @item
544 Guile maintains a sorted table of heap segments.
545 @end itemize
546
547 Thus, given any random word @var{w} fetched from the stack, Guile's
548 garbage collector can consult the table to see if @var{w} falls within a
549 known heap segment, and check @var{w}'s alignment. If both tests pass,
550 the collector knows that @var{w} is a valid pointer to a cell,
551 intentional or not, and proceeds to trace the cell.
552
553 Note that heap segments do not contain all the data Guile uses; cells
554 for objects like vectors and strings contain pointers to other memory
555 areas. However, since those pointers are internal, and not shared among
556 many pieces of code, it is enough for the collector to find the cell,
557 and then use the cell's type to find more pointers to trace.
558
559
560 @node Immediates vs Non-immediates
561 @subsection Immediates vs Non-immediates
562
563 Guile classifies Scheme objects into two kinds: those that fit entirely
564 within an @code{SCM}, and those that require heap storage.
565
566 The former class are called @dfn{immediates}. The class of immediates
567 includes small integers, characters, boolean values, the empty list, the
568 mysterious end-of-file object, and some others.
569
570 The remaining types are called, not surprisingly, @dfn{non-immediates}.
571 They include pairs, procedures, strings, vectors, and all other data
572 types in Guile.
573
574 @deftypefn Macro int SCM_IMP (SCM @var{x})
575 Return non-zero iff @var{x} is an immediate object.
576 @end deftypefn
577
578 @deftypefn Macro int SCM_NIMP (SCM @var{x})
579 Return non-zero iff @var{x} is a non-immediate object. This is the
580 exact complement of @code{SCM_IMP}, above.
581 @end deftypefn
582
583 Note that for versions of Guile prior to 1.4 it was necessary to use the
584 @code{SCM_NIMP} macro before calling a finer-grained predicate to
585 determine @var{x}'s type, such as @code{SCM_CONSP} or
586 @code{SCM_VECTORP}. This is no longer required: the definitions of all
587 Guile type predicates now include a call to @code{SCM_NIMP} where
588 necessary.
589
590
591 @node Immediate Datatypes
592 @subsection Immediate Datatypes
593
594 The following datatypes are immediate values; that is, they fit entirely
595 within an @code{SCM} value. The @code{SCM_IMP} and @code{SCM_NIMP}
596 macros will distinguish these from non-immediates; see @ref{Immediates
597 vs Non-immediates} for an explanation of the distinction.
598
599 Note that the type predicates for immediate values work correctly on any
600 @code{SCM} value; you do not need to call @code{SCM_IMP} first, to
601 establish that a value is immediate.
602
603 @menu
604 * Integer Data::
605 * Character Data::
606 * Boolean Data::
607 * Unique Values::
608 @end menu
609
610 @node Integer Data
611 @subsubsection Integers
612
613 Here are functions for operating on small integers, that fit within an
614 @code{SCM}. Such integers are called @dfn{immediate numbers}, or
615 @dfn{INUMs}. In general, INUMs occupy all but two bits of an
616 @code{SCM}.
617
618 Bignums and floating-point numbers are non-immediate objects, and have
619 their own, separate accessors. The functions here will not work on
620 them. This is not as much of a problem as you might think, however,
621 because the system never constructs bignums that could fit in an INUM,
622 and never uses floating point values for exact integers.
623
624 @deftypefn Macro int SCM_INUMP (SCM @var{x})
625 Return non-zero iff @var{x} is a small integer value.
626 @end deftypefn
627
628 @deftypefn Macro int SCM_NINUMP (SCM @var{x})
629 The complement of SCM_INUMP.
630 @end deftypefn
631
632 @deftypefn Macro int SCM_INUM (SCM @var{x})
633 Return the value of @var{x} as an ordinary, C integer. If @var{x}
634 is not an INUM, the result is undefined.
635 @end deftypefn
636
637 @deftypefn Macro SCM SCM_MAKINUM (int @var{i})
638 Given a C integer @var{i}, return its representation as an @code{SCM}.
639 This function does not check for overflow.
640 @end deftypefn
641
642
643 @node Character Data
644 @subsubsection Characters
645
646 Here are functions for operating on characters.
647
648 @deftypefn Macro int SCM_CHARP (SCM @var{x})
649 Return non-zero iff @var{x} is a character value.
650 @end deftypefn
651
652 @deftypefn Macro {unsigned int} SCM_CHAR (SCM @var{x})
653 Return the value of @code{x} as a C character. If @var{x} is not a
654 Scheme character, the result is undefined.
655 @end deftypefn
656
657 @deftypefn Macro SCM SCM_MAKE_CHAR (int @var{c})
658 Given a C character @var{c}, return its representation as a Scheme
659 character value.
660 @end deftypefn
661
662
663 @node Boolean Data
664 @subsubsection Booleans
665
666 Here are functions and macros for operating on booleans.
667
668 @deftypefn Macro SCM SCM_BOOL_T
669 @deftypefnx Macro SCM SCM_BOOL_F
670 The Scheme true and false values.
671 @end deftypefn
672
673 @deftypefn Macro int SCM_NFALSEP (@var{x})
674 Convert the Scheme boolean value to a C boolean. Since every object in
675 Scheme except @code{#f} is true, this amounts to comparing @var{x} to
676 @code{#f}; hence the name.
677 @c Noel feels a chill here.
678 @end deftypefn
679
680 @deftypefn Macro SCM SCM_BOOL_NOT (@var{x})
681 Return the boolean inverse of @var{x}. If @var{x} is not a
682 Scheme boolean, the result is undefined.
683 @end deftypefn
684
685
686 @node Unique Values
687 @subsubsection Unique Values
688
689 The immediate values that are neither small integers, characters, nor
690 booleans are all unique values --- that is, datatypes with only one
691 instance.
692
693 @deftypefn Macro SCM SCM_EOL
694 The Scheme empty list object, or ``End Of List'' object, usually written
695 in Scheme as @code{'()}.
696 @end deftypefn
697
698 @deftypefn Macro SCM SCM_EOF_VAL
699 The Scheme end-of-file value. It has no standard written
700 representation, for obvious reasons.
701 @end deftypefn
702
703 @deftypefn Macro SCM SCM_UNSPECIFIED
704 The value returned by expressions which the Scheme standard says return
705 an ``unspecified'' value.
706
707 This is sort of a weirdly literal way to take things, but the standard
708 read-eval-print loop prints nothing when the expression returns this
709 value, so it's not a bad idea to return this when you can't think of
710 anything else helpful.
711 @end deftypefn
712
713 @deftypefn Macro SCM SCM_UNDEFINED
714 The ``undefined'' value. Its most important property is that is not
715 equal to any valid Scheme value. This is put to various internal uses
716 by C code interacting with Guile.
717
718 For example, when you write a C function that is callable from Scheme
719 and which takes optional arguments, the interpreter passes
720 @code{SCM_UNDEFINED} for any arguments you did not receive.
721
722 We also use this to mark unbound variables.
723 @end deftypefn
724
725 @deftypefn Macro int SCM_UNBNDP (SCM @var{x})
726 Return true if @var{x} is @code{SCM_UNDEFINED}. Apply this to a
727 symbol's value to see if it has a binding as a global variable.
728 @end deftypefn
729
730
731 @node Non-immediate Datatypes
732 @subsection Non-immediate Datatypes
733
734 A non-immediate datatype is one which lives in the heap, either because
735 it cannot fit entirely within a @code{SCM} word, or because it denotes a
736 specific storage location (in the nomenclature of the Revised^5 Report
737 on Scheme).
738
739 The @code{SCM_IMP} and @code{SCM_NIMP} macros will distinguish these
740 from immediates; see @ref{Immediates vs Non-immediates}.
741
742 Given a cell, Guile distinguishes between pairs and other non-immediate
743 types by storing special @dfn{tag} values in a non-pair cell's car, that
744 cannot appear in normal pairs. A cell with a non-tag value in its car
745 is an ordinary pair. The type of a cell with a tag in its car depends
746 on the tag; the non-immediate type predicates test this value. If a tag
747 value appears elsewhere (in a vector, for example), the heap may become
748 corrupted.
749
750 Note how the type information for a non-immediate object is split
751 between the @code{SCM} word and the cell that the @code{SCM} word points
752 to. The @code{SCM} word itself only indicates that the object is
753 non-immediate --- in other words stored in a heap cell. The tag stored
754 in the first word of the heap cell indicates more precisely the type of
755 that object.
756
757 The type predicates for non-immediate values work correctly on any
758 @code{SCM} value; you do not need to call @code{SCM_NIMP} first, to
759 establish that a value is non-immediate.
760
761 @menu
762 * Pair Data::
763 * Vector Data::
764 * Procedures::
765 * Closures::
766 * Subrs::
767 * Port Data::
768 @end menu
769
770
771 @node Pair Data
772 @subsubsection Pairs
773
774 Pairs are the essential building block of list structure in Scheme. A
775 pair object has two fields, called the @dfn{car} and the @dfn{cdr}.
776
777 It is conventional for a pair's @sc{car} to contain an element of a
778 list, and the @sc{cdr} to point to the next pair in the list, or to
779 contain @code{SCM_EOL}, indicating the end of the list. Thus, a set of
780 pairs chained through their @sc{cdr}s constitutes a singly-linked list.
781 Scheme and libguile define many functions which operate on lists
782 constructed in this fashion, so although lists chained through the
783 @sc{car}s of pairs will work fine too, they may be less convenient to
784 manipulate, and receive less support from the community.
785
786 Guile implements pairs by mapping the @sc{car} and @sc{cdr} of a pair
787 directly into the two words of the cell.
788
789
790 @deftypefn Macro int SCM_CONSP (SCM @var{x})
791 Return non-zero iff @var{x} is a Scheme pair object.
792 @end deftypefn
793
794 @deftypefn Macro int SCM_NCONSP (SCM @var{x})
795 The complement of SCM_CONSP.
796 @end deftypefn
797
798 @deftypefun SCM scm_cons (SCM @var{car}, SCM @var{cdr})
799 Allocate (``CONStruct'') a new pair, with @var{car} and @var{cdr} as its
800 contents.
801 @end deftypefun
802
803 The macros below perform no type checking. The results are undefined if
804 @var{cell} is an immediate. However, since all non-immediate Guile
805 objects are constructed from cells, and these macros simply return the
806 first element of a cell, they actually can be useful on datatypes other
807 than pairs. (Of course, it is not very modular to use them outside of
808 the code which implements that datatype.)
809
810 @deftypefn Macro SCM SCM_CAR (SCM @var{cell})
811 Return the @sc{car}, or first field, of @var{cell}.
812 @end deftypefn
813
814 @deftypefn Macro SCM SCM_CDR (SCM @var{cell})
815 Return the @sc{cdr}, or second field, of @var{cell}.
816 @end deftypefn
817
818 @deftypefn Macro void SCM_SETCAR (SCM @var{cell}, SCM @var{x})
819 Set the @sc{car} of @var{cell} to @var{x}.
820 @end deftypefn
821
822 @deftypefn Macro void SCM_SETCDR (SCM @var{cell}, SCM @var{x})
823 Set the @sc{cdr} of @var{cell} to @var{x}.
824 @end deftypefn
825
826 @deftypefn Macro SCM SCM_CAAR (SCM @var{cell})
827 @deftypefnx Macro SCM SCM_CADR (SCM @var{cell})
828 @deftypefnx Macro SCM SCM_CDAR (SCM @var{cell}) @dots{}
829 @deftypefnx Macro SCM SCM_CDDDDR (SCM @var{cell})
830 Return the @sc{car} of the @sc{car} of @var{cell}, the @sc{car} of the
831 @sc{cdr} of @var{cell}, @i{et cetera}.
832 @end deftypefn
833
834
835 @node Vector Data
836 @subsubsection Vectors, Strings, and Symbols
837
838 Vectors, strings, and symbols have some properties in common. They all
839 have a length, and they all have an array of elements. In the case of a
840 vector, the elements are @code{SCM} values; in the case of a string or
841 symbol, the elements are characters.
842
843 All these types store their length (along with some tagging bits) in the
844 @sc{car} of their header cell, and store a pointer to the elements in
845 their @sc{cdr}. Thus, the @code{SCM_CAR} and @code{SCM_CDR} macros
846 are (somewhat) meaningful when applied to these datatypes.
847
848 @deftypefn Macro int SCM_VECTORP (SCM @var{x})
849 Return non-zero iff @var{x} is a vector.
850 @end deftypefn
851
852 @deftypefn Macro int SCM_STRINGP (SCM @var{x})
853 Return non-zero iff @var{x} is a string.
854 @end deftypefn
855
856 @deftypefn Macro int SCM_SYMBOLP (SCM @var{x})
857 Return non-zero iff @var{x} is a symbol.
858 @end deftypefn
859
860 @deftypefn Macro int SCM_VECTOR_LENGTH (SCM @var{x})
861 @deftypefnx Macro int SCM_STRING_LENGTH (SCM @var{x})
862 @deftypefnx Macro int SCM_SYMBOL_LENGTH (SCM @var{x})
863 Return the length of the object @var{x}. The result is undefined if
864 @var{x} is not a vector, string, or symbol, respectively.
865 @end deftypefn
866
867 @deftypefn Macro {SCM *} SCM_VECTOR_BASE (SCM @var{x})
868 Return a pointer to the array of elements of the vector @var{x}.
869 The result is undefined if @var{x} is not a vector.
870 @end deftypefn
871
872 @deftypefn Macro {char *} SCM_STRING_CHARS (SCM @var{x})
873 @deftypefnx Macro {char *} SCM_SYMBOL_CHARS (SCM @var{x})
874 Return a pointer to the characters of @var{x}. The result is undefined
875 if @var{x} is not a symbol or string, respectively.
876 @end deftypefn
877
878 There are also a few magic values stuffed into memory before a symbol's
879 characters, but you don't want to know about those. What cruft!
880
881 Note that @code{SCM_VECTOR_BASE}, @code{SCM_STRING_CHARS} and
882 @code{SCM_SYMBOL_CHARS} return pointers to data within the respective
883 object. Care must be taken that the object is not garbage collected
884 while that data is still being accessed. This is the same as for a
885 smob, @xref{Remembering During Operations}.
886
887
888 @node Procedures
889 @subsubsection Procedures
890
891 Guile provides two kinds of procedures: @dfn{closures}, which are the
892 result of evaluating a @code{lambda} expression, and @dfn{subrs}, which
893 are C functions packaged up as Scheme objects, to make them available to
894 Scheme programmers.
895
896 (There are actually other sorts of procedures: compiled closures, and
897 continuations; see the source code for details about them.)
898
899 @deftypefun SCM scm_procedure_p (SCM @var{x})
900 Return @code{SCM_BOOL_T} iff @var{x} is a Scheme procedure object, of
901 any sort. Otherwise, return @code{SCM_BOOL_F}.
902 @end deftypefun
903
904
905 @node Closures
906 @subsubsection Closures
907
908 [FIXME: this needs to be further subbed, but texinfo has no subsubsub]
909
910 A closure is a procedure object, generated as the value of a
911 @code{lambda} expression in Scheme. The representation of a closure is
912 straightforward --- it contains a pointer to the code of the lambda
913 expression from which it was created, and a pointer to the environment
914 it closes over.
915
916 In Guile, each closure also has a property list, allowing the system to
917 store information about the closure. I'm not sure what this is used for
918 at the moment --- the debugger, maybe?
919
920 @deftypefn Macro int SCM_CLOSUREP (SCM @var{x})
921 Return non-zero iff @var{x} is a closure.
922 @end deftypefn
923
924 @deftypefn Macro SCM SCM_PROCPROPS (SCM @var{x})
925 Return the property list of the closure @var{x}. The results are
926 undefined if @var{x} is not a closure.
927 @end deftypefn
928
929 @deftypefn Macro void SCM_SETPROCPROPS (SCM @var{x}, SCM @var{p})
930 Set the property list of the closure @var{x} to @var{p}. The results
931 are undefined if @var{x} is not a closure.
932 @end deftypefn
933
934 @deftypefn Macro SCM SCM_CODE (SCM @var{x})
935 Return the code of the closure @var{x}. The result is undefined if
936 @var{x} is not a closure.
937
938 This function should probably only be used internally by the
939 interpreter, since the representation of the code is intimately
940 connected with the interpreter's implementation.
941 @end deftypefn
942
943 @deftypefn Macro SCM SCM_ENV (SCM @var{x})
944 Return the environment enclosed by @var{x}.
945 The result is undefined if @var{x} is not a closure.
946
947 This function should probably only be used internally by the
948 interpreter, since the representation of the environment is intimately
949 connected with the interpreter's implementation.
950 @end deftypefn
951
952
953 @node Subrs
954 @subsubsection Subrs
955
956 [FIXME: this needs to be further subbed, but texinfo has no subsubsub]
957
958 A subr is a pointer to a C function, packaged up as a Scheme object to
959 make it callable by Scheme code. In addition to the function pointer,
960 the subr also contains a pointer to the name of the function, and
961 information about the number of arguments accepted by the C function, for
962 the sake of error checking.
963
964 There is no single type predicate macro that recognizes subrs, as
965 distinct from other kinds of procedures. The closest thing is
966 @code{scm_procedure_p}; see @ref{Procedures}.
967
968 @deftypefn Macro {char *} SCM_SNAME (@var{x})
969 Return the name of the subr @var{x}. The result is undefined if
970 @var{x} is not a subr.
971 @end deftypefn
972
973 @deftypefun SCM scm_c_define_gsubr (char *@var{name}, int @var{req}, int @var{opt}, int @var{rest}, SCM (*@var{function})())
974 Create a new subr object named @var{name}, based on the C function
975 @var{function}, make it visible to Scheme the value of as a global
976 variable named @var{name}, and return the subr object.
977
978 The subr object accepts @var{req} required arguments, @var{opt} optional
979 arguments, and a @var{rest} argument iff @var{rest} is non-zero. The C
980 function @var{function} should accept @code{@var{req} + @var{opt}}
981 arguments, or @code{@var{req} + @var{opt} + 1} arguments if @code{rest}
982 is non-zero.
983
984 When a subr object is applied, it must be applied to at least @var{req}
985 arguments, or else Guile signals an error. @var{function} receives the
986 subr's first @var{req} arguments as its first @var{req} arguments. If
987 there are fewer than @var{opt} arguments remaining, then @var{function}
988 receives the value @code{SCM_UNDEFINED} for any missing optional
989 arguments. If @var{rst} is non-zero, then any arguments after the first
990 @code{@var{req} + @var{opt}} are packaged up as a list as passed as
991 @var{function}'s last argument.
992
993 Note that subrs can actually only accept a predefined set of
994 combinations of required, optional, and rest arguments. For example, a
995 subr can take one required argument, or one required and one optional
996 argument, but a subr can't take one required and two optional arguments.
997 It's bizarre, but that's the way the interpreter was written. If the
998 arguments to @code{scm_c_define_gsubr} do not fit one of the predefined
999 patterns, then @code{scm_c_define_gsubr} will return a compiled closure
1000 object instead of a subr object.
1001 @end deftypefun
1002
1003
1004 @node Port Data
1005 @subsubsection Ports
1006
1007 Haven't written this yet, 'cos I don't understand ports yet.
1008
1009
1010 @node Signalling Type Errors
1011 @subsection Signalling Type Errors
1012
1013 Every function visible at the Scheme level should aggressively check the
1014 types of its arguments, to avoid misinterpreting a value, and perhaps
1015 causing a segmentation fault. Guile provides some macros to make this
1016 easier.
1017
1018 @deftypefn Macro void SCM_ASSERT (int @var{test}, SCM @var{obj}, unsigned int @var{position}, const char *@var{subr})
1019 If @var{test} is zero, signal a ``wrong type argument'' error,
1020 attributed to the subroutine named @var{subr}, operating on the value
1021 @var{obj}, which is the @var{position}'th argument of @var{subr}.
1022 @end deftypefn
1023
1024 @deftypefn Macro int SCM_ARG1
1025 @deftypefnx Macro int SCM_ARG2
1026 @deftypefnx Macro int SCM_ARG3
1027 @deftypefnx Macro int SCM_ARG4
1028 @deftypefnx Macro int SCM_ARG5
1029 @deftypefnx Macro int SCM_ARG6
1030 @deftypefnx Macro int SCM_ARG7
1031 One of the above values can be used for @var{position} to indicate the
1032 number of the argument of @var{subr} which is being checked.
1033 Alternatively, a positive integer number can be used, which allows to
1034 check arguments after the seventh. However, for parameter numbers up to
1035 seven it is preferable to use @code{SCM_ARGN} instead of the
1036 corresponding raw number, since it will make the code easier to
1037 understand.
1038 @end deftypefn
1039
1040 @deftypefn Macro int SCM_ARGn
1041 Passing a value of zero or @code{SCM_ARGn} for @var{position} allows to
1042 leave it unspecified which argument's type is incorrect. Again,
1043 @code{SCM_ARGn} should be preferred over a raw zero constant.
1044 @end deftypefn
1045
1046
1047 @node Unpacking the SCM type
1048 @subsection Unpacking the SCM Type
1049
1050 The previous sections have explained how @code{SCM} values can refer to
1051 immediate and non-immediate Scheme objects. For immediate objects, the
1052 complete object value is stored in the @code{SCM} word itself, while for
1053 non-immediates, the @code{SCM} word contains a pointer to a heap cell,
1054 and further information about the object in question is stored in that
1055 cell. This section describes how the @code{SCM} type is actually
1056 represented and used at the C level.
1057
1058 In fact, there are two basic C data types to represent objects in
1059 Guile: @code{SCM} and @code{scm_t_bits}.
1060
1061 @menu
1062 * Relationship between SCM and scm_t_bits::
1063 * Immediate objects::
1064 * Non-immediate objects::
1065 * Allocating Cells::
1066 * Heap Cell Type Information::
1067 * Accessing Cell Entries::
1068 * Basic Rules for Accessing Cell Entries::
1069 @end menu
1070
1071
1072 @node Relationship between SCM and scm_t_bits
1073 @subsubsection Relationship between @code{SCM} and @code{scm_t_bits}
1074
1075 A variable of type @code{SCM} is guaranteed to hold a valid Scheme
1076 object. A variable of type @code{scm_t_bits}, on the other hand, may
1077 hold a representation of a @code{SCM} value as a C integral type, but
1078 may also hold any C value, even if it does not correspond to a valid
1079 Scheme object.
1080
1081 For a variable @var{x} of type @code{SCM}, the Scheme object's type
1082 information is stored in a form that is not directly usable. To be able
1083 to work on the type encoding of the scheme value, the @code{SCM}
1084 variable has to be transformed into the corresponding representation as
1085 a @code{scm_t_bits} variable @var{y} by using the @code{SCM_UNPACK}
1086 macro. Once this has been done, the type of the scheme object @var{x}
1087 can be derived from the content of the bits of the @code{scm_t_bits}
1088 value @var{y}, in the way illustrated by the example earlier in this
1089 chapter (@pxref{Cheaper Pairs}). Conversely, a valid bit encoding of a
1090 Scheme value as a @code{scm_t_bits} variable can be transformed into the
1091 corresponding @code{SCM} value using the @code{SCM_PACK} macro.
1092
1093 @node Immediate objects
1094 @subsubsection Immediate objects
1095
1096 A Scheme object may either be an immediate, i.e. carrying all necessary
1097 information by itself, or it may contain a reference to a @dfn{cell}
1098 with additional information on the heap. Although in general it should
1099 be irrelevant for user code whether an object is an immediate or not,
1100 within Guile's own code the distinction is sometimes of importance.
1101 Thus, the following low level macro is provided:
1102
1103 @deftypefn Macro int SCM_IMP (SCM @var{x})
1104 A Scheme object is an immediate if it fulfills the @code{SCM_IMP}
1105 predicate, otherwise it holds an encoded reference to a heap cell. The
1106 result of the predicate is delivered as a C style boolean value. User
1107 code and code that extends Guile should normally not be required to use
1108 this macro.
1109 @end deftypefn
1110
1111 @noindent
1112 Summary:
1113 @itemize @bullet
1114 @item
1115 Given a Scheme object @var{x} of unknown type, check first
1116 with @code{SCM_IMP (@var{x})} if it is an immediate object.
1117 @item
1118 If so, all of the type and value information can be determined from the
1119 @code{scm_t_bits} value that is delivered by @code{SCM_UNPACK
1120 (@var{x})}.
1121 @end itemize
1122
1123
1124 @node Non-immediate objects
1125 @subsubsection Non-immediate objects
1126
1127 A Scheme object of type @code{SCM} that does not fulfill the
1128 @code{SCM_IMP} predicate holds an encoded reference to a heap cell.
1129 This reference can be decoded to a C pointer to a heap cell using the
1130 @code{SCM2PTR} macro. The encoding of a pointer to a heap cell into a
1131 @code{SCM} value is done using the @code{PTR2SCM} macro.
1132
1133 @c (FIXME:: this name should be changed)
1134 @deftypefn Macro (scm_t_cell *) SCM2PTR (SCM @var{x})
1135 Extract and return the heap cell pointer from a non-immediate @code{SCM}
1136 object @var{x}.
1137 @end deftypefn
1138
1139 @c (FIXME:: this name should be changed)
1140 @deftypefn Macro SCM PTR2SCM (scm_t_cell * @var{x})
1141 Return a @code{SCM} value that encodes a reference to the heap cell
1142 pointer @var{x}.
1143 @end deftypefn
1144
1145 Note that it is also possible to transform a non-immediate @code{SCM}
1146 value by using @code{SCM_UNPACK} into a @code{scm_t_bits} variable.
1147 However, the result of @code{SCM_UNPACK} may not be used as a pointer to
1148 a @code{scm_t_cell}: only @code{SCM2PTR} is guaranteed to transform a
1149 @code{SCM} object into a valid pointer to a heap cell. Also, it is not
1150 allowed to apply @code{PTR2SCM} to anything that is not a valid pointer
1151 to a heap cell.
1152
1153 @noindent
1154 Summary:
1155 @itemize @bullet
1156 @item
1157 Only use @code{SCM2PTR} on @code{SCM} values for which @code{SCM_IMP} is
1158 false!
1159 @item
1160 Don't use @code{(scm_t_cell *) SCM_UNPACK (@var{x})}! Use @code{SCM2PTR
1161 (@var{x})} instead!
1162 @item
1163 Don't use @code{PTR2SCM} for anything but a cell pointer!
1164 @end itemize
1165
1166 @node Allocating Cells
1167 @subsubsection Allocating Cells
1168
1169 Guile provides both ordinary cells with two slots, and double cells
1170 with four slots. The following two function are the most primitive
1171 way to allocate such cells.
1172
1173 If the caller intends to use it as a header for some other type, she
1174 must pass an appropriate magic value in @var{word_0}, to mark it as a
1175 member of that type, and pass whatever value as @var{word_1}, etc that
1176 the type expects. You should generally not need these functions,
1177 unless you are implementing a new datatype, and thoroughly understand
1178 the code in @code{<libguile/tags.h>}.
1179
1180 If you just want to allocate pairs, use @code{scm_cons}.
1181
1182 @deftypefn Function SCM scm_cell (scm_t_bits word_0, scm_t_bits word_1)
1183 Allocate a new cell, initialize the two slots with @var{word_0} and
1184 @var{word_1}, and return it.
1185
1186 Note that @var{word_0} and @var{word_1} are of type @code{scm_t_bits}.
1187 If you want to pass a @code{SCM} object, you need to use
1188 @code{SCM_UNPACK}.
1189 @end deftypefn
1190
1191 @deftypefn Function SCM scm_double_cell (scm_t_bits word_0, scm_t_bits word_1, scm_t_bits word_2, scm_t_bits word_3)
1192 Like @code{scm_cell}, but allocates a double cell with four
1193 slots.
1194 @end deftypefn
1195
1196 @node Heap Cell Type Information
1197 @subsubsection Heap Cell Type Information
1198
1199 Heap cells contain a number of entries, each of which is either a scheme
1200 object of type @code{SCM} or a raw C value of type @code{scm_t_bits}.
1201 Which of the cell entries contain Scheme objects and which contain raw C
1202 values is determined by the first entry of the cell, which holds the
1203 cell type information.
1204
1205 @deftypefn Macro scm_t_bits SCM_CELL_TYPE (SCM @var{x})
1206 For a non-immediate Scheme object @var{x}, deliver the content of the
1207 first entry of the heap cell referenced by @var{x}. This value holds
1208 the information about the cell type.
1209 @end deftypefn
1210
1211 @deftypefn Macro void SCM_SET_CELL_TYPE (SCM @var{x}, scm_t_bits @var{t})
1212 For a non-immediate Scheme object @var{x}, write the value @var{t} into
1213 the first entry of the heap cell referenced by @var{x}. The value
1214 @var{t} must hold a valid cell type.
1215 @end deftypefn
1216
1217
1218 @node Accessing Cell Entries
1219 @subsubsection Accessing Cell Entries
1220
1221 For a non-immediate Scheme object @var{x}, the object type can be
1222 determined by reading the cell type entry using the @code{SCM_CELL_TYPE}
1223 macro. For each different type of cell it is known which cell entries
1224 hold Scheme objects and which cell entries hold raw C data. To access
1225 the different cell entries appropriately, the following macros are
1226 provided.
1227
1228 @deftypefn Macro scm_t_bits SCM_CELL_WORD (SCM @var{x}, unsigned int @var{n})
1229 Deliver the cell entry @var{n} of the heap cell referenced by the
1230 non-immediate Scheme object @var{x} as raw data. It is illegal, to
1231 access cell entries that hold Scheme objects by using these macros. For
1232 convenience, the following macros are also provided.
1233 @itemize @bullet
1234 @item
1235 SCM_CELL_WORD_0 (@var{x}) @result{} SCM_CELL_WORD (@var{x}, 0)
1236 @item
1237 SCM_CELL_WORD_1 (@var{x}) @result{} SCM_CELL_WORD (@var{x}, 1)
1238 @item
1239 @dots{}
1240 @item
1241 SCM_CELL_WORD_@var{n} (@var{x}) @result{} SCM_CELL_WORD (@var{x}, @var{n})
1242 @end itemize
1243 @end deftypefn
1244
1245 @deftypefn Macro SCM SCM_CELL_OBJECT (SCM @var{x}, unsigned int @var{n})
1246 Deliver the cell entry @var{n} of the heap cell referenced by the
1247 non-immediate Scheme object @var{x} as a Scheme object. It is illegal,
1248 to access cell entries that do not hold Scheme objects by using these
1249 macros. For convenience, the following macros are also provided.
1250 @itemize @bullet
1251 @item
1252 SCM_CELL_OBJECT_0 (@var{x}) @result{} SCM_CELL_OBJECT (@var{x}, 0)
1253 @item
1254 SCM_CELL_OBJECT_1 (@var{x}) @result{} SCM_CELL_OBJECT (@var{x}, 1)
1255 @item
1256 @dots{}
1257 @item
1258 SCM_CELL_OBJECT_@var{n} (@var{x}) @result{} SCM_CELL_OBJECT (@var{x},
1259 @var{n})
1260 @end itemize
1261 @end deftypefn
1262
1263 @deftypefn Macro void SCM_SET_CELL_WORD (SCM @var{x}, unsigned int @var{n}, scm_t_bits @var{w})
1264 Write the raw C value @var{w} into entry number @var{n} of the heap cell
1265 referenced by the non-immediate Scheme value @var{x}. Values that are
1266 written into cells this way may only be read from the cells using the
1267 @code{SCM_CELL_WORD} macros or, in case cell entry 0 is written, using
1268 the @code{SCM_CELL_TYPE} macro. For the special case of cell entry 0 it
1269 has to be made sure that @var{w} contains a cell type information which
1270 does not describe a Scheme object. For convenience, the following
1271 macros are also provided.
1272 @itemize @bullet
1273 @item
1274 SCM_SET_CELL_WORD_0 (@var{x}, @var{w}) @result{} SCM_SET_CELL_WORD
1275 (@var{x}, 0, @var{w})
1276 @item
1277 SCM_SET_CELL_WORD_1 (@var{x}, @var{w}) @result{} SCM_SET_CELL_WORD
1278 (@var{x}, 1, @var{w})
1279 @item
1280 @dots{}
1281 @item
1282 SCM_SET_CELL_WORD_@var{n} (@var{x}, @var{w}) @result{} SCM_SET_CELL_WORD
1283 (@var{x}, @var{n}, @var{w})
1284 @end itemize
1285 @end deftypefn
1286
1287 @deftypefn Macro void SCM_SET_CELL_OBJECT (SCM @var{x}, unsigned int @var{n}, SCM @var{o})
1288 Write the Scheme object @var{o} into entry number @var{n} of the heap
1289 cell referenced by the non-immediate Scheme value @var{x}. Values that
1290 are written into cells this way may only be read from the cells using
1291 the @code{SCM_CELL_OBJECT} macros or, in case cell entry 0 is written,
1292 using the @code{SCM_CELL_TYPE} macro. For the special case of cell
1293 entry 0 the writing of a Scheme object into this cell is only allowed
1294 if the cell forms a Scheme pair. For convenience, the following macros
1295 are also provided.
1296 @itemize @bullet
1297 @item
1298 SCM_SET_CELL_OBJECT_0 (@var{x}, @var{o}) @result{} SCM_SET_CELL_OBJECT
1299 (@var{x}, 0, @var{o})
1300 @item
1301 SCM_SET_CELL_OBJECT_1 (@var{x}, @var{o}) @result{} SCM_SET_CELL_OBJECT
1302 (@var{x}, 1, @var{o})
1303 @item
1304 @dots{}
1305 @item
1306 SCM_SET_CELL_OBJECT_@var{n} (@var{x}, @var{o}) @result{}
1307 SCM_SET_CELL_OBJECT (@var{x}, @var{n}, @var{o})
1308 @end itemize
1309 @end deftypefn
1310
1311 @noindent
1312 Summary:
1313 @itemize @bullet
1314 @item
1315 For a non-immediate Scheme object @var{x} of unknown type, get the type
1316 information by using @code{SCM_CELL_TYPE (@var{x})}.
1317 @item
1318 As soon as the cell type information is available, only use the
1319 appropriate access methods to read and write data to the different cell
1320 entries.
1321 @end itemize
1322
1323
1324 @node Basic Rules for Accessing Cell Entries
1325 @subsubsection Basic Rules for Accessing Cell Entries
1326
1327 For each cell type it is generally up to the implementation of that type
1328 which of the corresponding cell entries hold Scheme objects and which
1329 hold raw C values. However, there is one basic rule that has to be
1330 followed: Scheme pairs consist of exactly two cell entries, which both
1331 contain Scheme objects. Further, a cell which contains a Scheme object
1332 in it first entry has to be a Scheme pair. In other words, it is not
1333 allowed to store a Scheme object in the first cell entry and a non
1334 Scheme object in the second cell entry.
1335
1336 @c Fixme:shouldn't this rather be SCM_PAIRP / SCM_PAIR_P ?
1337 @deftypefn Macro int SCM_CONSP (SCM @var{x})
1338 Determine, whether the Scheme object @var{x} is a Scheme pair,
1339 i.e. whether @var{x} references a heap cell consisting of exactly two
1340 entries, where both entries contain a Scheme object. In this case, both
1341 entries will have to be accessed using the @code{SCM_CELL_OBJECT}
1342 macros. On the contrary, if the @code{SCM_CONSP} predicate is not
1343 fulfilled, the first entry of the Scheme cell is guaranteed not to be a
1344 Scheme value and thus the first cell entry must be accessed using the
1345 @code{SCM_CELL_WORD_0} macro.
1346 @end deftypefn
1347
1348