data.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. ;;;; Data representations
  4. ; This implementation of the data representations is particularly
  5. ; tuned for byte-addressable machines with 4 bytes per word.
  6. ; Good representations for other kinds of machines would necessarily
  7. ; look quite different; e.g. on a word-addressed machine you might
  8. ; want to put tag bits in the high end of a word, or even go to some
  9. ; kind of BIBOP system.
  10. (define (bytes->cells bytes)
  11. ; using shift instead of quotient for speed
  12. ; (quotient (+ bytes (- bytes-per-cell 1)) bytes-per-cell)
  13. (arithmetic-shift-right (+ bytes (- bytes-per-cell 1))
  14. log-bytes-per-cell))
  15. (define (cells->bytes cells)
  16. (* cells bytes-per-cell))
  17. ; Addresses
  18. ;
  19. ; An "addressing unit" is the smallest quantum of storage addressed by
  20. ; an address on a particular machine. On a DEC-20, 3600, or other
  21. ; word-addressed architecture there is one addressing unit per cell. On
  22. ; the VAX or 68000, though, the addressing unit is the byte, of which there
  23. ; are 4 to a cell.
  24. (define (cells->a-units cells)
  25. (adjoin-bits cells 0 unused-field-width))
  26. (define (a-units->cells cells)
  27. (high-bits cells unused-field-width))
  28. (define (bytes->a-units byte-count)
  29. (cells->a-units (bytes->cells byte-count)))
  30. ; Descriptors
  31. ; A descriptor describes a Scheme object.
  32. ; A descriptor is represented as an integer whose low two bits are
  33. ; tag bits. The high bits contain information whose format and
  34. ; meaning are dependent on the tag.
  35. (define (make-descriptor tag data)
  36. (adjoin-bits data tag tag-field-width))
  37. (define (descriptor-tag descriptor)
  38. (low-bits descriptor tag-field-width))
  39. (define (descriptor-data descriptor)
  40. (high-bits descriptor tag-field-width))
  41. (define (unsigned-descriptor-data descriptor)
  42. (unsigned-high-bits descriptor tag-field-width))
  43. (define (set-descriptor-tag proto-descriptor tag)
  44. (assert (= 0 (descriptor-tag proto-descriptor)))
  45. (+ proto-descriptor tag))
  46. (define vm-eq? =)
  47. ; The four tags are: fixnum, immediate (character, boolean, etc.),
  48. ; header (gives the type and size of a stored object), and stored
  49. ; (pointer into memory).
  50. ; The header and immediate tags could be multiplexed, thus freeing up
  51. ; one of the 4 type codes for some other purpose, but the
  52. ; implementation is simpler if they're not.
  53. (define-enumeration tag
  54. (fixnum
  55. immediate
  56. header
  57. stob))
  58. ;; (assert (>= (shift-left 1 tag-field-width)
  59. ;; (vector-length tag)))
  60. (define (fixnum? descriptor)
  61. (= (descriptor-tag descriptor) (enum tag fixnum)))
  62. (define (immediate? descriptor)
  63. (= (descriptor-tag descriptor) (enum tag immediate)))
  64. (define (header? descriptor)
  65. (= (descriptor-tag descriptor) (enum tag header)))
  66. (define (stob? descriptor)
  67. (= (descriptor-tag descriptor) (enum tag stob)))
  68. ; Fixnums
  69. (define bits-per-fixnum
  70. (- (if (< bits-per-cell c-useful-bits-per-word)
  71. bits-per-cell
  72. c-useful-bits-per-word)
  73. tag-field-width))
  74. ; Be careful not to get intermediate bignums
  75. (define greatest-fixnum-value (+ (* (- (shift-left 1 (- bits-per-fixnum 2)) 1) 2)
  76. 1))
  77. (define least-fixnum-value (- (- greatest-fixnum-value) 1))
  78. (define (too-big-for-fixnum? n)
  79. (> n greatest-fixnum-value))
  80. (define (unsigned-too-big-for-fixnum? n)
  81. (un> n (integer->unsigned greatest-fixnum-value)))
  82. (define (too-small-for-fixnum? n)
  83. (< n least-fixnum-value))
  84. (define (enter-fixnum n)
  85. (assert (not (or (too-big-for-fixnum? n)
  86. (too-small-for-fixnum? n))))
  87. (make-descriptor (enum tag fixnum) n))
  88. (define (extract-fixnum p)
  89. (assert (fixnum? p))
  90. (descriptor-data p))
  91. (define (descriptor->fixnum p)
  92. (enter-fixnum (descriptor-data p)))
  93. (define (fixnum->stob p)
  94. (make-descriptor (enum tag stob) (extract-fixnum p)))
  95. ; These happen to work out, given our representation for fixnums.
  96. (define fixnum= =)
  97. (define fixnum< <)
  98. (define fixnum> >)
  99. (define fixnum<= <=)
  100. (define fixnum>= >=)
  101. (define (fixnum-bitwise-not x)
  102. (bitwise-not (bitwise-ior x 3)))
  103. (define fixnum-bitwise-and bitwise-and)
  104. (define fixnum-bitwise-ior bitwise-ior)
  105. (define fixnum-bitwise-xor bitwise-xor)
  106. ;----------------
  107. ; Immediates
  108. (define (make-immediate type info)
  109. (make-descriptor (enum tag immediate)
  110. (adjoin-bits info type immediate-type-field-width)))
  111. (define (immediate-type imm)
  112. (assert (immediate? imm))
  113. (low-bits (descriptor-data imm)
  114. immediate-type-field-width))
  115. (define (immediate-info imm)
  116. (assert (immediate? imm))
  117. (high-bits (descriptor-data imm)
  118. immediate-type-field-width))
  119. (define (tag&immediate-type descriptor)
  120. (low-bits descriptor (+ tag-field-width immediate-type-field-width)))
  121. (define (make-tag&immediate-type type)
  122. (adjoin-bits type (enum tag immediate) tag-field-width))
  123. (define-enumeration imm
  124. (false ; #f
  125. true ; #t
  126. char
  127. unspecific
  128. undefined
  129. eof
  130. null
  131. unreleased))
  132. ;; (assert (>= (shift-left 1 immediate-type-field-width)
  133. ;; (vector-length imm)))
  134. (define (immediate-predicate type)
  135. (lambda (descriptor)
  136. ;; Check low 8 bits...
  137. (= (tag&immediate-type descriptor)
  138. (make-tag&immediate-type type))))
  139. (define bytes-per-scalar-value-unit 4) ; must be >= 3
  140. (define (bytes->scalar-value-units byte-count)
  141. (quotient byte-count bytes-per-scalar-value-unit))
  142. (define (scalar-value-units->bytes units)
  143. (* units bytes-per-scalar-value-unit))
  144. (define vm-char? (immediate-predicate (enum imm char)))
  145. (define undefined? (immediate-predicate (enum imm undefined)))
  146. (define true (make-immediate (enum imm true) 0))
  147. (define false (make-immediate (enum imm false) 0))
  148. (define eof-object (make-immediate (enum imm eof) 0))
  149. (define null (make-immediate (enum imm null) 0))
  150. (define unspecific-value (make-immediate (enum imm unspecific) 0))
  151. (define quiescent (make-immediate (enum imm undefined) 0))
  152. (define unbound-marker (make-immediate (enum imm undefined) 1))
  153. (define unassigned-marker (make-immediate (enum imm undefined) 2))
  154. (define unreleased-value (make-immediate (enum imm unreleased) 0))
  155. (define (false? x)
  156. (vm-eq? x false))
  157. (define (enter-boolean b)
  158. (if b true false))
  159. (define (extract-boolean b)
  160. (assert (vm-boolean? b))
  161. (if (false? b) #f #t))
  162. (define (vm-boolean? x)
  163. (or (vm-eq? x false)
  164. (vm-eq? x true)))
  165. ; Characters
  166. ; old:
  167. (define (enter-char c)
  168. (make-immediate (enum imm char) (char->ascii c)))
  169. (define (extract-char d)
  170. (assert (vm-char? d))
  171. (ascii->char (immediate-info d)))
  172. ; new:
  173. (define (scalar-value->char c)
  174. (make-immediate (enum imm char) c))
  175. (define (char->scalar-value d)
  176. (assert (vm-char? d))
  177. (immediate-info d))
  178. ; these work given the representations
  179. (define vm-char=? =)
  180. (define vm-char<? <)
  181. ; Headers
  182. (define header-type-field-width (- immediate-type-field-width 1))
  183. (define header-size-field-width (- data-field-width immediate-type-field-width))
  184. ; Assumes headers sizes are extracted as unsigned.
  185. (define max-stob-contents-size-in-cells
  186. (bytes->cells (- (shift-left 1 header-size-field-width) 1)))
  187. (define (make-header type length-in-bytes)
  188. (make-descriptor (enum tag header)
  189. (adjoin-bits length-in-bytes
  190. type
  191. (+ 1 header-type-field-width))))
  192. (define header-immutable-bit-mask
  193. (adjoin-bits 1 0 (+ header-type-field-width tag-field-width)))
  194. (define (make-header-immutable header)
  195. (bitwise-ior header header-immutable-bit-mask))
  196. (define (header-type h)
  197. (assert (header? h))
  198. (low-bits (descriptor-data h)
  199. header-type-field-width))
  200. (define (immutable-header? h)
  201. (assert (header? h))
  202. (not (= 0 (bitwise-and h header-immutable-bit-mask))))
  203. (define (header-length-in-bytes h)
  204. (assert (header? h))
  205. (unsigned-high-bits (unsigned-descriptor-data h)
  206. (+ 1 header-type-field-width)))
  207. (define (header-length-in-cells header)
  208. (bytes->cells (header-length-in-bytes header)))
  209. (define (header-length-in-a-units h)
  210. (cells->a-units (header-length-in-cells h)))
  211. (define (d-vector-header? h)
  212. (< (header-type h) least-b-vector-type))
  213. (define (b-vector-header? h)
  214. (and (header? h)
  215. (>= (header-type h) least-b-vector-type)))
  216. ; Stored objects
  217. ; The data field of a descriptor for a stored object contains the
  218. ; cell number of the first cell after the object's header cell.
  219. (define (add-stob-tag address-as-integer)
  220. (set-descriptor-tag address-as-integer (enum tag stob)))
  221. (define (remove-stob-tag stob)
  222. (- stob (enum tag stob)))