Replace references to SCM_NFALSEP, etc with scm_is_true, etc.
[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
3229f68b 55@c essay @subtitle $Id: data-rep.texi,v 1.17 2004-04-21 14:32:08 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
435
436@menu
437* General Rules::
438* Conservative GC::
abaec75d 439* Immediates vs Non-immediates::
38a93523
NJ
440* Immediate Datatypes::
441* Non-immediate Datatypes::
442* Signalling Type Errors::
505392ae 443* Unpacking the SCM type::
38a93523
NJ
444@end menu
445
446@node General Rules
447@subsection General Rules
448
449Any code which operates on Guile datatypes must @code{#include} the
450header file @code{<libguile.h>}. This file contains a definition for
451the @code{SCM} typedef (Guile's universal type, as in the examples
452above), and definitions and declarations for a host of macros and
453functions that operate on @code{SCM} values.
454
455All identifiers declared by @code{<libguile.h>} begin with @code{scm_}
456or @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
462The functions described here generally check the types of their
463@code{SCM} arguments, and signal an error if their arguments are of an
464inappropriate type. Macros generally do not, unless that is their
465specified purpose. You must verify their argument types beforehand, as
466necessary.
467
468Macros and functions that return a boolean value have names ending in
469@code{P} or @code{_p} (for ``predicate''). Those that return a negated
470boolean 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
480Aside from the latent typing, the major source of constraints on a
481Scheme implementation's data representation is the garbage collector.
482The collector must be able to traverse every live object in the heap, to
483determine which objects are not live.
484
485There are many ways to implement this, but Guile uses an algorithm
486called @dfn{mark and sweep}. The collector scans the system's global
487variables and the local variables on the stack to determine which
488objects are immediately accessible by the C code. It then scans those
489objects to find the objects they point to, @i{et cetera}. The collector
490sets a @dfn{mark bit} on each object it finds, so each object is
491traversed only once. This process is called @dfn{tracing}.
492
493When the collector can find no unmarked objects pointed to by marked
494objects, it assumes that any objects that are still unmarked will never
495be used by the program (since there is no path of dereferences from any
496global or local variable that reaches them) and deallocates them.
497
498In the above paragraphs, we did not specify how the garbage collector
499finds the global and local variables; as usual, there are many different
500approaches. Frequently, the programmer must maintain a list of pointers
501to all global variables that refer to the heap, and another list
502(adjusted upon entry to and exit from each function) of local variables,
503for the collector's benefit.
504
505The list of global variables is usually not too difficult to maintain,
506since global variables are relatively rare. However, an explicitly
507maintained list of local variables (in the author's personal experience)
508is a nightmare to maintain. Thus, Guile uses a technique called
509@dfn{conservative garbage collection}, to make the local variable list
510unnecessary.
511
512The trick to conservative collection is to treat the stack as an
513ordinary range of memory, and assume that @emph{every} word on the stack
514is a pointer into the heap. Thus, the collector marks all objects whose
515addresses appear anywhere in the stack, without knowing for sure how
516that word is meant to be interpreted.
517
518Obviously, such a system will occasionally retain objects that are
519actually garbage, and should be freed. In practice, this is not a
520problem. The alternative, an explicitly maintained list of local
521variable addresses, is effectively much less reliable, due to programmer
522error.
523
524To accommodate this technique, data must be represented so that the
525collector can accurately determine whether a given stack word is a
526pointer or not. Guile does this as follows:
38a93523 527
505392ae 528@itemize @bullet
38a93523
NJ
529@item
530Every heap object has a two-word header, called a @dfn{cell}. Some
531objects, like pairs, fit entirely in a cell's two words; others may
532store pointers to additional memory in either of the words. For
533example, strings and vectors store their length in the first word, and a
534pointer to their elements in the second.
535
536@item
537Guile allocates whole arrays of cells at a time, called @dfn{heap
538segments}. These segments are always allocated so that the cells they
539contain fall on eight-byte boundaries, or whatever is appropriate for
540the machine's word size. Guile keeps all cells in a heap segment
541initialized, whether or not they are currently in use.
542
543@item
544Guile maintains a sorted table of heap segments.
38a93523
NJ
545@end itemize
546
547Thus, given any random word @var{w} fetched from the stack, Guile's
548garbage collector can consult the table to see if @var{w} falls within a
549known heap segment, and check @var{w}'s alignment. If both tests pass,
550the collector knows that @var{w} is a valid pointer to a cell,
551intentional or not, and proceeds to trace the cell.
552
553Note that heap segments do not contain all the data Guile uses; cells
554for objects like vectors and strings contain pointers to other memory
555areas. However, since those pointers are internal, and not shared among
556many pieces of code, it is enough for the collector to find the cell,
557and then use the cell's type to find more pointers to trace.
558
559
abaec75d
NJ
560@node Immediates vs Non-immediates
561@subsection Immediates vs Non-immediates
38a93523
NJ
562
563Guile classifies Scheme objects into two kinds: those that fit entirely
564within an @code{SCM}, and those that require heap storage.
565
566The former class are called @dfn{immediates}. The class of immediates
567includes small integers, characters, boolean values, the empty list, the
568mysterious end-of-file object, and some others.
569
85a9b4ed 570The remaining types are called, not surprisingly, @dfn{non-immediates}.
38a93523
NJ
571They include pairs, procedures, strings, vectors, and all other data
572types in Guile.
573
574@deftypefn Macro int SCM_IMP (SCM @var{x})
575Return non-zero iff @var{x} is an immediate object.
576@end deftypefn
577
578@deftypefn Macro int SCM_NIMP (SCM @var{x})
579Return non-zero iff @var{x} is a non-immediate object. This is the
580exact complement of @code{SCM_IMP}, above.
38a93523
NJ
581@end deftypefn
582
ffda6093 583Note that for versions of Guile prior to 1.4 it was necessary to use the
abaec75d
NJ
584@code{SCM_NIMP} macro before calling a finer-grained predicate to
585determine @var{x}'s type, such as @code{SCM_CONSP} or
ffda6093
NJ
586@code{SCM_VECTORP}. This is no longer required: the definitions of all
587Guile type predicates now include a call to @code{SCM_NIMP} where
588necessary.
abaec75d 589
38a93523
NJ
590
591@node Immediate Datatypes
592@subsection Immediate Datatypes
593
594The following datatypes are immediate values; that is, they fit entirely
595within an @code{SCM} value. The @code{SCM_IMP} and @code{SCM_NIMP}
596macros will distinguish these from non-immediates; see @ref{Immediates
abaec75d 597vs Non-immediates} for an explanation of the distinction.
38a93523
NJ
598
599Note 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
505392ae 601establish that a value is immediate.
38a93523
NJ
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
613Here 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
618Bignums and floating-point numbers are non-immediate objects, and have
619their own, separate accessors. The functions here will not work on
620them. This is not as much of a problem as you might think, however,
621because the system never constructs bignums that could fit in an INUM,
622and never uses floating point values for exact integers.
623
624@deftypefn Macro int SCM_INUMP (SCM @var{x})
625Return non-zero iff @var{x} is a small integer value.
626@end deftypefn
627
628@deftypefn Macro int SCM_NINUMP (SCM @var{x})
629The complement of SCM_INUMP.
630@end deftypefn
631
632@deftypefn Macro int SCM_INUM (SCM @var{x})
633Return the value of @var{x} as an ordinary, C integer. If @var{x}
634is not an INUM, the result is undefined.
635@end deftypefn
636
637@deftypefn Macro SCM SCM_MAKINUM (int @var{i})
638Given a C integer @var{i}, return its representation as an @code{SCM}.
639This function does not check for overflow.
640@end deftypefn
641
642
643@node Character Data
644@subsubsection Characters
645
646Here are functions for operating on characters.
647
648@deftypefn Macro int SCM_CHARP (SCM @var{x})
649Return non-zero iff @var{x} is a character value.
650@end deftypefn
651
652@deftypefn Macro {unsigned int} SCM_CHAR (SCM @var{x})
653Return the value of @code{x} as a C character. If @var{x} is not a
654Scheme character, the result is undefined.
655@end deftypefn
656
657@deftypefn Macro SCM SCM_MAKE_CHAR (int @var{c})
658Given a C character @var{c}, return its representation as a Scheme
659character value.
660@end deftypefn
661
662
663@node Boolean Data
664@subsubsection Booleans
665
666Here are functions and macros for operating on booleans.
667
668@deftypefn Macro SCM SCM_BOOL_T
669@deftypefnx Macro SCM SCM_BOOL_F
670The Scheme true and false values.
671@end deftypefn
672
673@deftypefn Macro int SCM_NFALSEP (@var{x})
674Convert the Scheme boolean value to a C boolean. Since every object in
675Scheme 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})
681Return the boolean inverse of @var{x}. If @var{x} is not a
682Scheme boolean, the result is undefined.
683@end deftypefn
684
685
686@node Unique Values
687@subsubsection Unique Values
688
689The immediate values that are neither small integers, characters, nor
690booleans are all unique values --- that is, datatypes with only one
691instance.
692
693@deftypefn Macro SCM SCM_EOL
694The Scheme empty list object, or ``End Of List'' object, usually written
695in Scheme as @code{'()}.
696@end deftypefn
697
698@deftypefn Macro SCM SCM_EOF_VAL
699The Scheme end-of-file value. It has no standard written
700representation, for obvious reasons.
701@end deftypefn
702
703@deftypefn Macro SCM SCM_UNSPECIFIED
704The value returned by expressions which the Scheme standard says return
705an ``unspecified'' value.
706
707This is sort of a weirdly literal way to take things, but the standard
708read-eval-print loop prints nothing when the expression returns this
709value, so it's not a bad idea to return this when you can't think of
710anything else helpful.
711@end deftypefn
712
713@deftypefn Macro SCM SCM_UNDEFINED
714The ``undefined'' value. Its most important property is that is not
715equal to any valid Scheme value. This is put to various internal uses
716by C code interacting with Guile.
717
718For example, when you write a C function that is callable from Scheme
719and which takes optional arguments, the interpreter passes
720@code{SCM_UNDEFINED} for any arguments you did not receive.
721
722We also use this to mark unbound variables.
723@end deftypefn
724
725@deftypefn Macro int SCM_UNBNDP (SCM @var{x})
726Return true if @var{x} is @code{SCM_UNDEFINED}. Apply this to a
727symbol'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
734A non-immediate datatype is one which lives in the heap, either because
735it cannot fit entirely within a @code{SCM} word, or because it denotes a
cee2ed4f 736specific storage location (in the nomenclature of the Revised^5 Report
38a93523
NJ
737on Scheme).
738
739The @code{SCM_IMP} and @code{SCM_NIMP} macros will distinguish these
abaec75d 740from immediates; see @ref{Immediates vs Non-immediates}.
38a93523
NJ
741
742Given a cell, Guile distinguishes between pairs and other non-immediate
743types by storing special @dfn{tag} values in a non-pair cell's car, that
744cannot appear in normal pairs. A cell with a non-tag value in its car
745is an ordinary pair. The type of a cell with a tag in its car depends
746on the tag; the non-immediate type predicates test this value. If a tag
747value appears elsewhere (in a vector, for example), the heap may become
748corrupted.
749
505392ae
NJ
750Note how the type information for a non-immediate object is split
751between the @code{SCM} word and the cell that the @code{SCM} word points
752to. The @code{SCM} word itself only indicates that the object is
753non-immediate --- in other words stored in a heap cell. The tag stored
754in the first word of the heap cell indicates more precisely the type of
755that object.
756
ffda6093
NJ
757The 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
759establish that a value is non-immediate.
38a93523
NJ
760
761@menu
38a93523
NJ
762* Pair Data::
763* Vector Data::
764* Procedures::
765* Closures::
766* Subrs::
767* Port Data::
768@end menu
769
38a93523
NJ
770
771@node Pair Data
772@subsubsection Pairs
773
774Pairs are the essential building block of list structure in Scheme. A
775pair object has two fields, called the @dfn{car} and the @dfn{cdr}.
776
777It is conventional for a pair's @sc{car} to contain an element of a
778list, and the @sc{cdr} to point to the next pair in the list, or to
779contain @code{SCM_EOL}, indicating the end of the list. Thus, a set of
780pairs chained through their @sc{cdr}s constitutes a singly-linked list.
781Scheme and libguile define many functions which operate on lists
782constructed 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
784manipulate, and receive less support from the community.
785
786Guile implements pairs by mapping the @sc{car} and @sc{cdr} of a pair
787directly into the two words of the cell.
788
789
790@deftypefn Macro int SCM_CONSP (SCM @var{x})
791Return non-zero iff @var{x} is a Scheme pair object.
38a93523
NJ
792@end deftypefn
793
794@deftypefn Macro int SCM_NCONSP (SCM @var{x})
795The complement of SCM_CONSP.
796@end deftypefn
797
38a93523
NJ
798@deftypefun SCM scm_cons (SCM @var{car}, SCM @var{cdr})
799Allocate (``CONStruct'') a new pair, with @var{car} and @var{cdr} as its
800contents.
801@end deftypefun
802
85a9b4ed 803The macros below perform no type checking. The results are undefined if
38a93523
NJ
804@var{cell} is an immediate. However, since all non-immediate Guile
805objects are constructed from cells, and these macros simply return the
806first element of a cell, they actually can be useful on datatypes other
807than pairs. (Of course, it is not very modular to use them outside of
808the code which implements that datatype.)
809
810@deftypefn Macro SCM SCM_CAR (SCM @var{cell})
811Return the @sc{car}, or first field, of @var{cell}.
812@end deftypefn
813
814@deftypefn Macro SCM SCM_CDR (SCM @var{cell})
815Return 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})
819Set 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})
823Set 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})
830Return 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
838Vectors, strings, and symbols have some properties in common. They all
839have a length, and they all have an array of elements. In the case of a
840vector, the elements are @code{SCM} values; in the case of a string or
841symbol, the elements are characters.
842
843All 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
845their @sc{cdr}. Thus, the @code{SCM_CAR} and @code{SCM_CDR} macros
846are (somewhat) meaningful when applied to these datatypes.
847
848@deftypefn Macro int SCM_VECTORP (SCM @var{x})
849Return non-zero iff @var{x} is a vector.
38a93523
NJ
850@end deftypefn
851
852@deftypefn Macro int SCM_STRINGP (SCM @var{x})
853Return non-zero iff @var{x} is a string.
38a93523
NJ
854@end deftypefn
855
856@deftypefn Macro int SCM_SYMBOLP (SCM @var{x})
857Return non-zero iff @var{x} is a symbol.
38a93523
NJ
858@end deftypefn
859
cee2ed4f
MG
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})
863Return the length of the object @var{x}. The result is undefined if
864@var{x} is not a vector, string, or symbol, respectively.
38a93523
NJ
865@end deftypefn
866
cee2ed4f 867@deftypefn Macro {SCM *} SCM_VECTOR_BASE (SCM @var{x})
38a93523 868Return a pointer to the array of elements of the vector @var{x}.
505392ae 869The result is undefined if @var{x} is not a vector.
38a93523
NJ
870@end deftypefn
871
cee2ed4f
MG
872@deftypefn Macro {char *} SCM_STRING_CHARS (SCM @var{x})
873@deftypefnx Macro {char *} SCM_SYMBOL_CHARS (SCM @var{x})
874Return a pointer to the characters of @var{x}. The result is undefined
875if @var{x} is not a symbol or string, respectively.
38a93523
NJ
876@end deftypefn
877
878There are also a few magic values stuffed into memory before a symbol's
879characters, but you don't want to know about those. What cruft!
880
cf4e2dab
KR
881Note that @code{SCM_VECTOR_BASE}, @code{SCM_STRING_CHARS} and
882@code{SCM_SYMBOL_CHARS} return pointers to data within the respective
883object. Care must be taken that the object is not garbage collected
884while that data is still being accessed. This is the same as for a
885smob, @xref{Remembering During Operations}.
886
38a93523
NJ
887
888@node Procedures
889@subsubsection Procedures
890
891Guile provides two kinds of procedures: @dfn{closures}, which are the
892result of evaluating a @code{lambda} expression, and @dfn{subrs}, which
893are C functions packaged up as Scheme objects, to make them available to
894Scheme programmers.
895
896(There are actually other sorts of procedures: compiled closures, and
897continuations; see the source code for details about them.)
898
899@deftypefun SCM scm_procedure_p (SCM @var{x})
900Return @code{SCM_BOOL_T} iff @var{x} is a Scheme procedure object, of
901any 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
910A closure is a procedure object, generated as the value of a
911@code{lambda} expression in Scheme. The representation of a closure is
912straightforward --- it contains a pointer to the code of the lambda
913expression from which it was created, and a pointer to the environment
914it closes over.
915
916In Guile, each closure also has a property list, allowing the system to
917store information about the closure. I'm not sure what this is used for
918at the moment --- the debugger, maybe?
919
920@deftypefn Macro int SCM_CLOSUREP (SCM @var{x})
505392ae 921Return non-zero iff @var{x} is a closure.
38a93523
NJ
922@end deftypefn
923
924@deftypefn Macro SCM SCM_PROCPROPS (SCM @var{x})
925Return the property list of the closure @var{x}. The results are
926undefined if @var{x} is not a closure.
927@end deftypefn
928
929@deftypefn Macro void SCM_SETPROCPROPS (SCM @var{x}, SCM @var{p})
930Set the property list of the closure @var{x} to @var{p}. The results
931are undefined if @var{x} is not a closure.
932@end deftypefn
933
934@deftypefn Macro SCM SCM_CODE (SCM @var{x})
505392ae 935Return the code of the closure @var{x}. The result is undefined if
38a93523
NJ
936@var{x} is not a closure.
937
938This function should probably only be used internally by the
939interpreter, since the representation of the code is intimately
940connected with the interpreter's implementation.
941@end deftypefn
942
943@deftypefn Macro SCM SCM_ENV (SCM @var{x})
944Return the environment enclosed by @var{x}.
505392ae 945The result is undefined if @var{x} is not a closure.
38a93523
NJ
946
947This function should probably only be used internally by the
948interpreter, since the representation of the environment is intimately
949connected 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
958A subr is a pointer to a C function, packaged up as a Scheme object to
959make it callable by Scheme code. In addition to the function pointer,
960the subr also contains a pointer to the name of the function, and
85a9b4ed 961information about the number of arguments accepted by the C function, for
38a93523
NJ
962the sake of error checking.
963
964There is no single type predicate macro that recognizes subrs, as
965distinct 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})
505392ae 969Return the name of the subr @var{x}. The result is undefined if
38a93523
NJ
970@var{x} is not a subr.
971@end deftypefn
972
bcf009c3 973@deftypefun SCM scm_c_define_gsubr (char *@var{name}, int @var{req}, int @var{opt}, int @var{rest}, SCM (*@var{function})())
38a93523
NJ
974Create 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
976variable named @var{name}, and return the subr object.
977
978The subr object accepts @var{req} required arguments, @var{opt} optional
979arguments, and a @var{rest} argument iff @var{rest} is non-zero. The C
980function @var{function} should accept @code{@var{req} + @var{opt}}
981arguments, or @code{@var{req} + @var{opt} + 1} arguments if @code{rest}
982is non-zero.
983
984When a subr object is applied, it must be applied to at least @var{req}
985arguments, or else Guile signals an error. @var{function} receives the
986subr's first @var{req} arguments as its first @var{req} arguments. If
987there are fewer than @var{opt} arguments remaining, then @var{function}
988receives the value @code{SCM_UNDEFINED} for any missing optional
989arguments. 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
993Note that subrs can actually only accept a predefined set of
994combinations of required, optional, and rest arguments. For example, a
995subr can take one required argument, or one required and one optional
996argument, but a subr can't take one required and two optional arguments.
997It's bizarre, but that's the way the interpreter was written. If the
bcf009c3
NJ
998arguments to @code{scm_c_define_gsubr} do not fit one of the predefined
999patterns, then @code{scm_c_define_gsubr} will return a compiled closure
38a93523
NJ
1000object instead of a subr object.
1001@end deftypefun
1002
1003
1004@node Port Data
1005@subsubsection Ports
1006
1007Haven't written this yet, 'cos I don't understand ports yet.
1008
1009
1010@node Signalling Type Errors
1011@subsection Signalling Type Errors
1012
1013Every function visible at the Scheme level should aggressively check the
1014types of its arguments, to avoid misinterpreting a value, and perhaps
1015causing a segmentation fault. Guile provides some macros to make this
1016easier.
1017
813c57db
NJ
1018@deftypefn Macro void SCM_ASSERT (int @var{test}, SCM @var{obj}, unsigned int @var{position}, const char *@var{subr})
1019If @var{test} is zero, signal a ``wrong type argument'' error,
1020attributed to the subroutine named @var{subr}, operating on the value
1021@var{obj}, which is the @var{position}'th argument of @var{subr}.
38a93523
NJ
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
813c57db
NJ
1029@deftypefnx Macro int SCM_ARG6
1030@deftypefnx Macro int SCM_ARG7
1031One of the above values can be used for @var{position} to indicate the
1032number of the argument of @var{subr} which is being checked.
1033Alternatively, a positive integer number can be used, which allows to
1034check arguments after the seventh. However, for parameter numbers up to
1035seven it is preferable to use @code{SCM_ARGN} instead of the
1036corresponding raw number, since it will make the code easier to
1037understand.
38a93523
NJ
1038@end deftypefn
1039
1040@deftypefn Macro int SCM_ARGn
813c57db
NJ
1041Passing a value of zero or @code{SCM_ARGn} for @var{position} allows to
1042leave it unspecified which argument's type is incorrect. Again,
1043@code{SCM_ARGn} should be preferred over a raw zero constant.
38a93523
NJ
1044@end deftypefn
1045
1046
505392ae
NJ
1047@node Unpacking the SCM type
1048@subsection Unpacking the SCM Type
1049
1050The previous sections have explained how @code{SCM} values can refer to
1051immediate and non-immediate Scheme objects. For immediate objects, the
1052complete object value is stored in the @code{SCM} word itself, while for
1053non-immediates, the @code{SCM} word contains a pointer to a heap cell,
1054and further information about the object in question is stored in that
1055cell. This section describes how the @code{SCM} type is actually
1056represented and used at the C level.
1057
3229f68b
MV
1058In fact, there are two basic C data types to represent objects in
1059Guile: @code{SCM} and @code{scm_t_bits}.
505392ae
NJ
1060
1061@menu
9d5315b6 1062* Relationship between SCM and scm_t_bits::
505392ae
NJ
1063* Immediate objects::
1064* Non-immediate objects::
9d5315b6 1065* Allocating Cells::
505392ae
NJ
1066* Heap Cell Type Information::
1067* Accessing Cell Entries::
1068* Basic Rules for Accessing Cell Entries::
1069@end menu
1070
1071
9d5315b6
MV
1072@node Relationship between SCM and scm_t_bits
1073@subsubsection Relationship between @code{SCM} and @code{scm_t_bits}
505392ae
NJ
1074
1075A variable of type @code{SCM} is guaranteed to hold a valid Scheme
9d5315b6 1076object. A variable of type @code{scm_t_bits}, on the other hand, may
505392ae
NJ
1077hold a representation of a @code{SCM} value as a C integral type, but
1078may also hold any C value, even if it does not correspond to a valid
1079Scheme object.
1080
1081For a variable @var{x} of type @code{SCM}, the Scheme object's type
1082information is stored in a form that is not directly usable. To be able
1083to work on the type encoding of the scheme value, the @code{SCM}
1084variable has to be transformed into the corresponding representation as
9d5315b6 1085a @code{scm_t_bits} variable @var{y} by using the @code{SCM_UNPACK}
505392ae 1086macro. Once this has been done, the type of the scheme object @var{x}
9d5315b6 1087can be derived from the content of the bits of the @code{scm_t_bits}
505392ae
NJ
1088value @var{y}, in the way illustrated by the example earlier in this
1089chapter (@pxref{Cheaper Pairs}). Conversely, a valid bit encoding of a
9d5315b6 1090Scheme value as a @code{scm_t_bits} variable can be transformed into the
505392ae
NJ
1091corresponding @code{SCM} value using the @code{SCM_PACK} macro.
1092
505392ae
NJ
1093@node Immediate objects
1094@subsubsection Immediate objects
1095
1096A Scheme object may either be an immediate, i.e. carrying all necessary
1097information by itself, or it may contain a reference to a @dfn{cell}
1098with additional information on the heap. Although in general it should
1099be irrelevant for user code whether an object is an immediate or not,
1100within Guile's own code the distinction is sometimes of importance.
1101Thus, the following low level macro is provided:
1102
1103@deftypefn Macro int SCM_IMP (SCM @var{x})
1104A Scheme object is an immediate if it fulfills the @code{SCM_IMP}
1105predicate, otherwise it holds an encoded reference to a heap cell. The
1106result of the predicate is delivered as a C style boolean value. User
1107code and code that extends Guile should normally not be required to use
1108this macro.
1109@end deftypefn
1110
1111@noindent
1112Summary:
1113@itemize @bullet
1114@item
1115Given a Scheme object @var{x} of unknown type, check first
1116with @code{SCM_IMP (@var{x})} if it is an immediate object.
1117@item
1118If so, all of the type and value information can be determined from the
9d5315b6 1119@code{scm_t_bits} value that is delivered by @code{SCM_UNPACK
505392ae
NJ
1120(@var{x})}.
1121@end itemize
1122
1123
1124@node Non-immediate objects
1125@subsubsection Non-immediate objects
1126
85a9b4ed 1127A Scheme object of type @code{SCM} that does not fulfill the
505392ae
NJ
1128@code{SCM_IMP} predicate holds an encoded reference to a heap cell.
1129This 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)
228a24ef 1134@deftypefn Macro (scm_t_cell *) SCM2PTR (SCM @var{x})
505392ae
NJ
1135Extract and return the heap cell pointer from a non-immediate @code{SCM}
1136object @var{x}.
1137@end deftypefn
1138
1139@c (FIXME:: this name should be changed)
228a24ef 1140@deftypefn Macro SCM PTR2SCM (scm_t_cell * @var{x})
505392ae
NJ
1141Return a @code{SCM} value that encodes a reference to the heap cell
1142pointer @var{x}.
1143@end deftypefn
1144
1145Note that it is also possible to transform a non-immediate @code{SCM}
9d5315b6 1146value by using @code{SCM_UNPACK} into a @code{scm_t_bits} variable.
505392ae 1147However, the result of @code{SCM_UNPACK} may not be used as a pointer to
228a24ef 1148a @code{scm_t_cell}: only @code{SCM2PTR} is guaranteed to transform a
505392ae
NJ
1149@code{SCM} object into a valid pointer to a heap cell. Also, it is not
1150allowed to apply @code{PTR2SCM} to anything that is not a valid pointer
1151to a heap cell.
1152
1153@noindent
1154Summary:
1155@itemize @bullet
1156@item
1157Only use @code{SCM2PTR} on @code{SCM} values for which @code{SCM_IMP} is
1158false!
1159@item
228a24ef 1160Don't use @code{(scm_t_cell *) SCM_UNPACK (@var{x})}! Use @code{SCM2PTR
505392ae
NJ
1161(@var{x})} instead!
1162@item
1163Don't use @code{PTR2SCM} for anything but a cell pointer!
1164@end itemize
1165
9d5315b6
MV
1166@node Allocating Cells
1167@subsubsection Allocating Cells
1168
1169Guile provides both ordinary cells with two slots, and double cells
1170with four slots. The following two function are the most primitive
1171way to allocate such cells.
1172
1173If the caller intends to use it as a header for some other type, she
1174must pass an appropriate magic value in @var{word_0}, to mark it as a
1175member of that type, and pass whatever value as @var{word_1}, etc that
1176the type expects. You should generally not need these functions,
1177unless you are implementing a new datatype, and thoroughly understand
1178the code in @code{<libguile/tags.h>}.
1179
1180If you just want to allocate pairs, use @code{scm_cons}.
1181
228a24ef 1182@deftypefn Function SCM scm_cell (scm_t_bits word_0, scm_t_bits word_1)
9d5315b6
MV
1183Allocate a new cell, initialize the two slots with @var{word_0} and
1184@var{word_1}, and return it.
1185
1186Note that @var{word_0} and @var{word_1} are of type @code{scm_t_bits}.
1187If you want to pass a @code{SCM} object, you need to use
1188@code{SCM_UNPACK}.
1189@end deftypefn
1190
228a24ef
DH
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)
1192Like @code{scm_cell}, but allocates a double cell with four
9d5315b6
MV
1193slots.
1194@end deftypefn
505392ae
NJ
1195
1196@node Heap Cell Type Information
1197@subsubsection Heap Cell Type Information
1198
1199Heap cells contain a number of entries, each of which is either a scheme
9d5315b6 1200object of type @code{SCM} or a raw C value of type @code{scm_t_bits}.
505392ae
NJ
1201Which of the cell entries contain Scheme objects and which contain raw C
1202values is determined by the first entry of the cell, which holds the
1203cell type information.
1204
9d5315b6 1205@deftypefn Macro scm_t_bits SCM_CELL_TYPE (SCM @var{x})
505392ae
NJ
1206For a non-immediate Scheme object @var{x}, deliver the content of the
1207first entry of the heap cell referenced by @var{x}. This value holds
1208the information about the cell type.
1209@end deftypefn
1210
9d5315b6 1211@deftypefn Macro void SCM_SET_CELL_TYPE (SCM @var{x}, scm_t_bits @var{t})
505392ae
NJ
1212For a non-immediate Scheme object @var{x}, write the value @var{t} into
1213the 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
1221For a non-immediate Scheme object @var{x}, the object type can be
1222determined by reading the cell type entry using the @code{SCM_CELL_TYPE}
1223macro. For each different type of cell it is known which cell entries
1224hold Scheme objects and which cell entries hold raw C data. To access
1225the different cell entries appropriately, the following macros are
1226provided.
1227
9d5315b6 1228@deftypefn Macro scm_t_bits SCM_CELL_WORD (SCM @var{x}, unsigned int @var{n})
505392ae
NJ
1229Deliver the cell entry @var{n} of the heap cell referenced by the
1230non-immediate Scheme object @var{x} as raw data. It is illegal, to
1231access cell entries that hold Scheme objects by using these macros. For
1232convenience, the following macros are also provided.
230712c9 1233@itemize @bullet
505392ae
NJ
1234@item
1235SCM_CELL_WORD_0 (@var{x}) @result{} SCM_CELL_WORD (@var{x}, 0)
1236@item
1237SCM_CELL_WORD_1 (@var{x}) @result{} SCM_CELL_WORD (@var{x}, 1)
1238@item
1239@dots{}
1240@item
1241SCM_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})
1246Deliver the cell entry @var{n} of the heap cell referenced by the
1247non-immediate Scheme object @var{x} as a Scheme object. It is illegal,
1248to access cell entries that do not hold Scheme objects by using these
1249macros. For convenience, the following macros are also provided.
230712c9 1250@itemize @bullet
505392ae
NJ
1251@item
1252SCM_CELL_OBJECT_0 (@var{x}) @result{} SCM_CELL_OBJECT (@var{x}, 0)
1253@item
1254SCM_CELL_OBJECT_1 (@var{x}) @result{} SCM_CELL_OBJECT (@var{x}, 1)
1255@item
1256@dots{}
1257@item
1258SCM_CELL_OBJECT_@var{n} (@var{x}) @result{} SCM_CELL_OBJECT (@var{x},
1259@var{n})
1260@end itemize
1261@end deftypefn
1262
9d5315b6 1263@deftypefn Macro void SCM_SET_CELL_WORD (SCM @var{x}, unsigned int @var{n}, scm_t_bits @var{w})
505392ae
NJ
1264Write the raw C value @var{w} into entry number @var{n} of the heap cell
1265referenced by the non-immediate Scheme value @var{x}. Values that are
1266written 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
1268the @code{SCM_CELL_TYPE} macro. For the special case of cell entry 0 it
1269has to be made sure that @var{w} contains a cell type information which
1270does not describe a Scheme object. For convenience, the following
1271macros are also provided.
230712c9 1272@itemize @bullet
505392ae
NJ
1273@item
1274SCM_SET_CELL_WORD_0 (@var{x}, @var{w}) @result{} SCM_SET_CELL_WORD
1275(@var{x}, 0, @var{w})
1276@item
1277SCM_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
1282SCM_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})
1288Write the Scheme object @var{o} into entry number @var{n} of the heap
1289cell referenced by the non-immediate Scheme value @var{x}. Values that
1290are written into cells this way may only be read from the cells using
1291the @code{SCM_CELL_OBJECT} macros or, in case cell entry 0 is written,
1292using the @code{SCM_CELL_TYPE} macro. For the special case of cell
1293entry 0 the writing of a Scheme object into this cell is only allowed
1294if the cell forms a Scheme pair. For convenience, the following macros
1295are also provided.
230712c9 1296@itemize @bullet
505392ae
NJ
1297@item
1298SCM_SET_CELL_OBJECT_0 (@var{x}, @var{o}) @result{} SCM_SET_CELL_OBJECT
1299(@var{x}, 0, @var{o})
1300@item
1301SCM_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
1306SCM_SET_CELL_OBJECT_@var{n} (@var{x}, @var{o}) @result{}
1307SCM_SET_CELL_OBJECT (@var{x}, @var{n}, @var{o})
1308@end itemize
1309@end deftypefn
1310
1311@noindent
1312Summary:
1313@itemize @bullet
1314@item
1315For a non-immediate Scheme object @var{x} of unknown type, get the type
1316information by using @code{SCM_CELL_TYPE (@var{x})}.
1317@item
1318As soon as the cell type information is available, only use the
1319appropriate access methods to read and write data to the different cell
1320entries.
1321@end itemize
1322
1323
1324@node Basic Rules for Accessing Cell Entries
1325@subsubsection Basic Rules for Accessing Cell Entries
1326
1327For each cell type it is generally up to the implementation of that type
1328which of the corresponding cell entries hold Scheme objects and which
1329hold raw C values. However, there is one basic rule that has to be
1330followed: Scheme pairs consist of exactly two cell entries, which both
1331contain Scheme objects. Further, a cell which contains a Scheme object
1332in it first entry has to be a Scheme pair. In other words, it is not
1333allowed to store a Scheme object in the first cell entry and a non
1334Scheme 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})
1338Determine, whether the Scheme object @var{x} is a Scheme pair,
1339i.e. whether @var{x} references a heap cell consisting of exactly two
1340entries, where both entries contain a Scheme object. In this case, both
1341entries will have to be accessed using the @code{SCM_CELL_OBJECT}
c4d0cddd
NJ
1342macros. On the contrary, if the @code{SCM_CONSP} predicate is not
1343fulfilled, the first entry of the Scheme cell is guaranteed not to be a
1344Scheme value and thus the first cell entry must be accessed using the
505392ae
NJ
1345@code{SCM_CELL_WORD_0} macro.
1346@end deftypefn
1347
1348