data.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Scheme 48's data representations, for writing heap images.
  3. ; Defines things needed by TRANSPORT (which is called by WRITE-IMAGE, which
  4. ; is called by the static linker).
  5. ; Adapted from vm/util/vm-utilities.scm and vm/data/data.scm.
  6. (define (low-bits n k)
  7. (bitwise-and n (- (arithmetic-shift 1 k) 1)))
  8. (define (high-bits n k)
  9. (arithmetic-shift n (- 0 k)))
  10. (define (adjoin-bits high low width)
  11. (bitwise-ior (arithmetic-shift high width) low))
  12. ; Data descriptions copied from DATA.SCM
  13. (define-enumeration tag
  14. (fixnum
  15. immediate
  16. header
  17. stob))
  18. (define-enumeration imm
  19. (false ; #f
  20. true ; #t
  21. char
  22. unspecific
  23. undefined
  24. eof
  25. null))
  26. (define bits-per-fixnum
  27. (- (if (< bits-per-cell s48-useful-bits-per-word)
  28. bits-per-cell
  29. s48-useful-bits-per-word)
  30. tag-field-width))
  31. (define least-fixnum-value (- 0 (arithmetic-shift 1 (- bits-per-fixnum 1))))
  32. (define greatest-fixnum-value (- (arithmetic-shift 1 (- bits-per-fixnum 1))
  33. 1))
  34. (define (fixnum? x)
  35. (and (integer? x)
  36. (>= x least-fixnum-value)
  37. (<= x greatest-fixnum-value)))
  38. (define (immediate? x)
  39. (or (fixnum? x)
  40. (char? x)
  41. (eq? x #t)
  42. (eq? x '()) ; I think order is important here as () is a literal while
  43. (eq? x #f) ; #F is generated with an instruction. If () = #f then
  44. ; we still lose on something like '(#F) (which is
  45. ; present in the system).
  46. ;; (eq? thing (unspecific))
  47. ))
  48. (define car-offset 0)
  49. (define cdr-offset 1)
  50. (define closure-template-offset 0)
  51. (define closure-env-offset 1)
  52. (define location-id-offset 0)
  53. ; Procedures for manipulating bits
  54. (define (make-descriptor tag data)
  55. (adjoin-bits data tag tag-field-width))
  56. (define (make-immediate type info)
  57. (make-descriptor (enum tag immediate)
  58. (adjoin-bits info type immediate-type-field-width)))
  59. (define vm-true (make-immediate (enum imm true) 0))
  60. (define vm-false (make-immediate (enum imm false) 0))
  61. (define vm-null (make-immediate (enum imm null) 0))
  62. (define vm-unspecific (make-immediate (enum imm unspecific) 0))
  63. (define vm-unbound (make-immediate (enum imm undefined) 1))
  64. (define vm-unassigned (make-immediate (enum imm undefined) 2))
  65. (define header-type-field-width (- immediate-type-field-width 1))
  66. (define (make-header type length-in-bytes)
  67. (make-descriptor (enum tag header) (adjoin-bits length-in-bytes
  68. type
  69. (+ 1 header-type-field-width))))
  70. (define (make-header-immutable header)
  71. (bitwise-ior header (arithmetic-shift 1 (+ tag-field-width
  72. header-type-field-width))))
  73. (define (make-stob-descriptor addr)
  74. (bitwise-ior (enum tag stob) addr))
  75. (define (bytes->cells bytes)
  76. (quotient (+ bytes (- bytes-per-cell 1))
  77. bytes-per-cell))
  78. (define (cells->bytes cells)
  79. (* cells bytes-per-cell))
  80. (define (cells->a-units cells)
  81. (adjoin-bits cells 0 unused-field-width))
  82. (define (a-units->cells cells)
  83. (high-bits cells unused-field-width))
  84. (define (bytes->a-units byte-count)
  85. (cells->a-units (bytes->cells byte-count)))
  86. ;; Unicode code points
  87. (define bytes-per-scalar-value-unit 4) ; must be >= 3
  88. (define (bytes->scalar-value-units byte-count)
  89. (quotient byte-count bytes-per-scalar-value-unit))
  90. (define (scalar-value-units->bytes units)
  91. (* units bytes-per-scalar-value-unit))