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