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