1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
; Part of Scheme 48 1.9. See file COPYING for notices and license.
; Authors: Richard Kelsey, Jonathan Rees, Marcus Crestani, David Frese,
; Mike Sperber, Martin Gasbichler
; This is file struct.scm.
; This file defines a level of abstraction for storage somewhat higher
; than that of d-vectors and b-vectors: pairs, symbols, and other datatypes.
(define (stob-maker type maker)
(lambda (length key)
(maker type length key)))
(define (stob-predicate type)
(lambda (obj) (stob-of-type? obj type)))
; data for these comes from STOB-DATA in arch.scm
(define-shared-primitive-data-type pair #t)
(define-shared-primitive-data-type symbol #t #f
make-symbol ; hidden from RTS
()
(symbol-next set-symbol-next!)) ; hidden from RTS
(define-shared-primitive-data-type closure #f #f)
(define-shared-primitive-data-type location)
(define-shared-primitive-data-type cell)
(define-shared-primitive-data-type weak-pointer)
(define-shared-primitive-data-type transport-link-cell)
(define-shared-primitive-data-type shared-binding #f #f
#f
()
(shared-binding-next set-shared-binding-next!)) ; hidden from RTS
(define-shared-primitive-data-type port)
(define-shared-primitive-data-type channel #f #f
make-channel ; hidden from RTS
(;; these setters are hidden from the RTS
(channel-status set-channel-status!)
(channel-id set-channel-id!)
(channel-os-index set-channel-os-index!)
(channel-close-silently? set-channel-close-silently?!))
;; none of these are visible to the RTS
(channel-next set-channel-next!)
;; this is
;; false - if there's nothing going on
;; true - if there's an operation pending
;; the number of bytes transferred - if error? (below) is false
;; the error code - if error? (below) is true
(channel-os-status set-channel-os-status!)
(channel-error? set-channel-error?!))
; Vectors and so on
(define-vector-data-type vector #t)
(define-vector-data-type record)
(define-vector-data-type extended-number)
(define make-bignum (stob-maker (enum stob byte-vector) make-b-vector))
(define bignum? (stob-predicate (enum stob bignum)))
(define bignum-length b-vector-length)
(define bignum-ref b-vector-ref)
(define bignum-set! b-vector-set!)
(define (bignum-size len)
(+ stob-overhead (bytes->cells len)))
(define-vector-data-type continuation)
(define-vector-data-type template)
(define (vm-make-vector+gc len)
(let ((vector (maybe-make-d-vector+gc (enum stob vector)
len)))
(if (false? vector)
(error "Out of space, unable to allocate"))
vector))
(define (vm-vector-fill! v val)
(do ((i 0 (+ i 1)))
((= i (vm-vector-length v)) v)
(vm-vector-set! v i val)))
; We use D-VECTOR-INIT! because continuations in the heap are only initialized,
; never modified.
(define-syntax define-cont-field
(syntax-rules ()
((define-cont-field ref set offset)
(begin
(define (ref c) (continuation-ref c offset))
(define (set c val) (d-vector-init! c offset val))))))
(define-cont-field continuation-cont set-continuation-cont!
continuation-cont-index)
(define-cont-field continuation-pc set-continuation-pc!
continuation-pc-index)
(define-cont-field continuation-code set-continuation-code!
continuation-code-index)
(define (template-code tem) (template-ref tem 0))
(define (template-byte-code tem) (template-ref tem 1))
(define (template-name tem) (template-ref tem 2))
; Code vectors
(define make-code-vector (stob-maker (enum stob byte-vector) make-b-vector))
(define code-vector? (stob-predicate (enum stob byte-vector)))
(define code-vector-length b-vector-length)
(define code-vector-ref b-vector-ref)
(define code-vector-set! b-vector-set!)
(define (code-vector-size len)
(+ stob-overhead (bytes->cells len)))
; for small strings only
(define (vm-make-string length key)
(make-b-vector (enum stob string)
(scalar-value-units->bytes length)
key))
(define (vm-make-string+gc length)
(let ((string (maybe-make-b-vector+gc (enum stob string)
(scalar-value-units->bytes length))))
(if (false? string)
(error "Out of space, unable to allocate"))
string))
(define vm-string? (stob-predicate (enum stob string)))
(define (vm-string-length x)
(bytes->scalar-value-units (b-vector-length x)))
; deals in code points, not PreScheme characters
; #### This should be rewritten as a loop the PreScheme compiler can unroll
(define (vm-string-ref s i)
(let ((base (scalar-value-units->bytes i)))
(do ((bits 0 (+ bits bits-per-byte))
(j 0 (+ 1 j))
(scalar-value 0
(adjoin-bits (b-vector-ref s (+ base j))
scalar-value
bits)))
((>= j bytes-per-scalar-value-unit)
scalar-value))))
;; #### ditto
(define (vm-string-set! s i c)
(let ((base (scalar-value-units->bytes i)))
(do ((bits 0 (+ bits bits-per-byte))
(j 0 (+ 1 j))
(shifted c (unsigned-high-bits shifted bits-per-byte)))
((>= j bytes-per-scalar-value-unit))
(b-vector-set! s (+ base j)
(low-bits shifted bits-per-byte)))
(unspecific))) ; avoid type problem
(define (vm-string-size length)
(+ stob-overhead (bytes->cells (scalar-value-units->bytes length))))
; Converting external (C, Latin-1) strings to S48 strings.
; for small strings only
(define (enter-string string key)
(let* ((len (string-length string))
(v (vm-make-string len key)))
(copy-string-to-vm-string/latin-1! string len v)
v))
(define (enter-string+gc-n string len)
(let ((v (vm-make-string+gc len)))
(copy-string-to-vm-string/latin-1! string len v)
v))
(define (enter-string+gc string)
(enter-string+gc-n string (string-length string)))
(define (copy-string-to-vm-string/latin-1! string len v)
(do ((i 0 (+ i 1)))
((>= i len))
(vm-string-set! v i (char->ascii (string-ref string i))))
(unspecific))
(define (copy-vm-string-to-string/latin-1! vm-string start count string)
(do ((i 0 (+ 1 i)))
((>= i count))
(let ((c (vm-string-ref vm-string i)))
(string-set! string (+ i start)
(if (<= c 255)
(ascii->char c)
#\?))))
(unspecific))
(define (copy-vm-string-chars! from from-index to to-index count)
(copy-memory! (address+ (address-after-header from)
(* from-index bytes-per-scalar-value-unit))
(address+ (address-after-header to)
(* to-index bytes-per-scalar-value-unit))
(* count bytes-per-scalar-value-unit)))
; This depends on our having 0 bytes at the end of strings.
; We should really be doing the NUL termination here, but
; DEFINE-CONSING-PRIMITIVE doesn't let us do it easily.
(define (extract-low-string code-vector) ; used by OPEN
(assert (code-vector? code-vector))
(fetch-nul-terminated-string (address-after-header code-vector)))
(define (vm-string=? s1 s2)
(assert (and (vm-string? s1) (vm-string? s2)))
(let ((len (b-vector-length s1)))
(and (= len (b-vector-length s2))
(memory-equal? (address-after-header s1)
(address-after-header s2)
len))))
;; This is only a very crude approximation for debugging purposes.
(define (write-vm-string vm-string out)
(do ((size (vm-string-length vm-string))
(i 0 (+ 1 i)))
((>= i size) 0) ; make type checker happy
(write-char (ascii->char (vm-string-ref vm-string i)) out)))
; Number predicates
;(define bignum? (stob-predicate (enum stob bignum)))
(define ratnum? (stob-predicate (enum stob ratnum)))
(define double? (stob-predicate (enum stob double)))
; Doubles
(define (extract-double double)
(fetch-flonum (address-after-header double)))
(define double-bytes 8)
(define double-size
(+ stob-overhead (bytes->cells double-bytes)))
(define (enter-double value key)
(let ((double (make-b-vector (enum stob double) double-bytes key)))
(store-flonum! (address-after-header double) value)
double))
; Hashing
; The hash function used here is taken from srfi-13.
;; biggest Unicode scalar value
(define greatest-character-code #x10FFFF)
;; BOUND has to be the biggest power of two that fulfils the following
;; equation to make sure that the intermediate calculations of
;; STRING-HASH are always fixnums:
;;
;; (<= (+ greatest-character-code (* 37 (- BOUND 1)))
;; greatest-fixnum-value)
(define bound
(let ((x (+ (quotient (- greatest-fixnum-value
greatest-character-code) 37) 1)))
(let lp ((i #x10000))
(if (>= i x)
i
(lp (+ i i))))))
;; bitmask to cover (- BOUND 1)
(define mask (- bound 1))
;; string hash
(define (vm-string-hash s)
(let ((end (vm-string-length s)))
(let lp ((i 0) (ans 0))
(if (>= i end)
(remainder ans bound)
(lp (+ i 1)
(bitwise-and mask (+ (* 37 ans) (vm-string-ref s i))))))))
|