api.txt 9.0 KB

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