Replace $letrec with $rec
[bpt/guile.git] / doc / ref / api.txt
CommitLineData
a0e07ba4
NJ
1Scheme objects
2==============
3
4There are two basic C data types to represent objects in guile:
5
6- SCM: SCM is the user level abstract C type that is used to represent all of
7guile's scheme objects, no matter what the scheme object type is. No C
8operation except assignment is guaranteed to work with variables of type SCM.
9Only use macros and functions to work with SCM values. Values are converted
10between C data types and the SCM type with utility functions and macros.
11
12- scm_bits_t: An integral data type that is guaranteed to be large enough to
13hold all information that is required to represent any scheme object. While
14this data type is used to implement guile internals, the use of this type is
15also necessary to write certain kinds of extensions to guile.
16
17
18Relationship between SCM and scm_bits_t
19=======================================
20
21A variable of type SCM is guaranteed to hold a valid scheme object. A
22variable of type scm_bits_t, however, may either hold a representation of a
23SCM value as a C integral type, but may also hold any C value, even if it does
24not correspond to a valid scheme object.
25
26For a variable x of type SCM, the scheme object's type information is stored
27in a form that is not directly usable. To be able to work on the type
28encoding of the scheme value, the SCM variable has to be transformed into the
29corresponding representation as a scm_bits_t variable y by using the
30SCM_UNPACK macro. After this has been done, the type of the scheme object x
31can be derived from the content of the bits of the scm_bits_t value y, as is
32described in -->data-rep. A valid bit encoding of a scheme value as a
33scm_bits_t variable can be transformed into the corresponding SCM value by
34using the SCM_PACK macro.
35
36- scm_bits_t SCM_UNPACK (SCM x): Transforms the SCM value x into it's
37representation as an integral type. Only after applying SCM_UNPACK it is
38possible to access the bits and contents of the SCM value.
39
40- SCM SCM_PACK (scm_bits_t x): Takes a valid integral representation of a
41scheme object and transforms it into its representation as a SCM value.
42
43
44Immediate objects
45=================
46
47A scheme object may either be an immediate, i. e. carrying all necessary
48information by itself, or it may contain a reference to a 'cell' with
49additional information on the heap. While the fact, whether an object is an
50immediate or not should be irrelevant for user code, within guile's own code
51the distinction is sometimes of importance. Thus, the following low level
52macro is provided:
53
54- int SCM_IMP (SCM x): A scheme object is an immediate if it fullfills the
55SCM_IMP predicate, otherwise it holds an encoded reference to a heap cell.
56The result of the predicate is delivered as a C style boolean value. User
57code and code that extends guile should normally not be required to use this
58macro.
59
60Summary:
61* For a scheme object x of unknown type, check first with SCM_IMP (x) if it is
62an immediate object. If so, all of the type and value information can be
63determined from the scm_bits_t value that is delivered by SCM_UNPACK (x).
64
65
66Non immediate objects
67=====================
68
228a24ef
DH
69- (scm_t_cell *) SCM2PTR (SCM x) (FIXME:: this name should be changed)
70- SCM PTR2SCM (scm_t_cell * x) (FIXME:: this name should be changed)
a0e07ba4
NJ
71
72A scheme object of type SCM that does not fullfill the SCM_IMP predicate holds
73an encoded reference to a heap cell. This reference can be decoded to a C
74pointer to a heap cell using the SCM2PTR macro. The encoding of a pointer to
75a heap cell into a SCM value is done using the PTR2SCM macro.
76
77Note that it is also possible to transform a non immediate SCM value by using
78SCM_UNPACK into a scm_bits_t variable. Hower, the result of SCM_UNPACK may
228a24ef 79not be used as a pointer to a scm_t_cell: Only SCM2PTR is guaranteed to
a0e07ba4
NJ
80transform a SCM object into a valid pointer to a heap cell. Also, it is not
81allowed to apply PTR2SCM to anything that is not a valid pointer to a heap
82cell.
83
84Summary:
85* Only use SCM2PTR for SCM values for which SCM_IMP is false!
228a24ef 86* Don't use '(scm_t_cell*) SCM_UNPACK (x)'! Use 'SCM2PTR (x)' instead!
a0e07ba4
NJ
87* Don't use PTR2SCM for anything but a cell pointer!
88
89
90Heap Cell Type Information
91==========================
92
93Heap cells contain a number of entries, each of which is either a scheme
94object of type SCM or a raw C value of type scm_bits_t. Which of the cell
95entries contain scheme objects and which contain raw C values is determined by
96the first entry of the cell, which holds the cell type information.
97
98- scm_bits_t SCM_CELL_TYPE (SCM x): For a non immediate scheme object x,
99deliver the content of the first entry of the heap cell referenced by x. This
100value holds the information about the cell type as described in -->data-rep.
101
102- void SCM_SET_CELL_TYPE (SCM x, scm_bits_t t): For a non immediate scheme
103object x, write the value t into the first entry of the heap cell referenced
104by x. The value t must hold a valid cell type as described in -->data-rep.
105
106
107Accessing Cell Entries
108======================
109
110For a non immediate scheme object x, the object type can be determined by
111reading the cell type entry using the SCM_CELL_TYPE macro. For the different
112types of cells it is known which cell entry holds scheme objects and which cell
113entry holds raw C data. To access the different cell entries appropriately,
114the following macros are provided:
115
116- scm_bits_t SCM_CELL_WORD (SCM x, unsigned int n): Deliver the cell entry n
117of the heap cell referenced by the non immediate scheme object x as raw data.
118It is illegal, to access cell entries that hold scheme objects by using these
119macros. For convenience, the following macros are also provided:
120 SCM_CELL_WORD_0 (x) --> SCM_CELL_WORD (x, 0)
121 SCM_CELL_WORD_1 (x) --> SCM_CELL_WORD (x, 1)
122 ...
123 SCM_CELL_WORD_n (x) --> SCM_CELL_WORD (x, n)
124
125- SCM SCM_CELL_OBJECT (SCM x, unsigned int n): Deliver the cell entry n of
126the heap cell referenced by the non immediate scheme object x as a scheme
127object. It is illegal, to access cell entries that do not hold scheme objects
128by using these macros. For convenience, the following macros are also
129provided:
130 SCM_CELL_OBJECT_0 (x) --> SCM_CELL_OBJECT (x, 0)
131 SCM_CELL_OBJECT_1 (x) --> SCM_CELL_OBJECT (x, 1)
132 ...
133 SCM_CELL_OBJECT_n (x) --> SCM_CELL_OBJECT (x, n)
134
135- void SCM_SET_CELL_WORD (SCM x, unsigned int n, scm_bits_t w): Write the raw
136C value w into entry number n of the heap cell referenced by the non immediate
137scheme value x. Values that are written into cells this way may only be read
138from the cells using the SCM_CELL_WORD macros or, in case cell entry 0 is
139written, using the SCM_CELL_TYPE macro. For the special case of cell entry 0
140it has to be made sure that w contains a cell type information (see
141-->data-rep) which does not describe a scheme object. For convenience, the
142following macros are also provided:
143 SCM_SET_CELL_WORD_0 (x, w) --> SCM_SET_CELL_WORD (x, 0, w)
144 SCM_SET_CELL_WORD_1 (x, w) --> SCM_SET_CELL_WORD (x, 1, w)
145 ...
146 SCM_SET_CELL_WORD_n (x, w) --> SCM_SET_CELL_WORD (x, n, w)
147
148- void SCM_SET_CELL_OBJECT (SCM x, unsigned int n, SCM o): Write the scheme
149object o into entry number n of the heap cell referenced by the non immediate
150scheme value x. Values that are written into cells this way may only be read
151from the cells using the SCM_CELL_OBJECT macros or, in case cell entry 0 is
152written, using the SCM_CELL_TYPE macro. For the special case of cell entry 0
153the writing of a scheme object into this cell is only allowed, if the cell
154forms a scheme pair. For convenience, the following macros are also provided:
155 SCM_SET_CELL_OBJECT_0 (x, o) --> SCM_SET_CELL_OBJECT (x, 0, o)
156 SCM_SET_CELL_OBJECT_1 (x, o) --> SCM_SET_CELL_OBJECT (x, 1, o)
157 ...
158 SCM_SET_CELL_OBJECT_n (x, o) --> SCM_SET_CELL_OBJECT (x, n, o)
159
160Summary:
161* For a non immediate scheme object x of unknown type, get the type
162 information by using SCM_CELL_TYPE (x).
163* As soon as the cell type information is available, only use the appropriate
164 access methods to read and write data to the different cell entries.
165
166
167Basic Rules for Accessing Cell Entries
168======================================
169
170For each cell type it is generally up to the implementation of that type which
171of the corresponding cell entries hold scheme objects and which hold raw C
172values. However, there is one basic rules that has to be followed: Scheme
173pairs consist of exactly two cell entries, which both contain scheme objects.
174Further, a cell which contains a scheme object in it first entry has to be a
175scheme pair. In other words, it is not allowed to store a scheme object in
176the first cell entry and a non scheme object in the second cell entry.
177
178Fixme:shouldn't this rather be SCM_PAIRP / SCM_PAIR_P ?
179- int SCM_CONSP (SCM x): Determine, whether the scheme object x is a scheme
180pair, i. e. whether x references a heap cell consisting of exactly two
181entries, where both entries contain a scheme object. In this case, both
182entries will have to be accessed using the SCM_CELL_OBJECT macros. On the
183contrary, if the SCM_CONSP predicate is not fulfilled, the first entry of the
184scheme cell is guaranteed not to be a scheme value and thus the first cell
185entry must be accessed using the SCM_CELL_WORD_0 macro.