Debian Codegraph @

scheme/ikarus.fasl.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 ;;; FASL
18 ;;;
19 ;;; A fasl object is a header followed by one or more objects followed by an
20 ;;; end-of-fasl marker
21 ;;;
22 ;;; The header is the string "#@IK01"
23 ;;; The end of fasl marker is "@"
24 ;;;
25 ;;; An object is either:
26 ;;; "N" : denoting the empty list
27 ;;; "T" : denoting #t
28 ;;; "F" : denoting #f
29 ;;; "E" : denoting the end of file object
30 ;;; "U" : denoting the unspecified value
31 ;;; "I" + 4-bytes : denoting a fixnum (in host byte order)
32 ;;; "c" + 1-byte : denoting a small character (<= 255)
33 ;;; "C" + 4-byte word: big char.
34 ;;; "P" + object1 + object2 : a pair
35 ;;; "V" + 4-bytes(n) + object ... : a vector of length n followed by n objects
36 ;;; "v" + 4-byte(n) + octet ... : a bytevector of length n followed by n octets
37 ;;; "s" + 4-bytes(n) + octet ... : an ascii string
38 ;;; "S" + 4-bytes(n) + int ... : a unicode string
39 ;;; "M" + symbol-name : a symbol
40 ;;; "G" + pretty-name + unique-name : a gensym
41 ;;; "R" + rtd-name + rtd-symbol + field-count + field-names
42 ;;; "{" + field-count + rtd + fields
43 ;;; ">" + 4-bytes(i) : mark the next object with index i
44 ;;; "<" + 4-bytes(i) : dereference the object marked with index i
45 ;;; "x" : denotes code
46 ;;; "T" : Thunk; followed by code.
47 ;;; "r" + numerator + denominator : ratnum
48 ;;; "f" + 8-byte : IEEE flonum
49 ;;; "b" + 4-byte(n) + n-bytes denotes a bignum (sign is sign of n).
50
51
52
53
54
55
56 (library (ikarus fasl read)
57 (export fasl-read)
58 (import (except (ikarus) fasl-read)
59 (except (ikarus.code-objects) procedure-annotation)
60 (ikarus system $codes)
61 (ikarus system $structs))
62
63 (define who 'fasl-read)
64
65 (define (make-struct rtd n)
66 (import (ikarus system $fx))
67 (let f ([i 0] [n n] [s ($make-struct rtd n)])
68 (cond
69 [($fx= i n) s]
70 [else
71 ($struct-set! s i 0)
72 (f ($fx+ i 1) n s)])))
73
74 (define (read-u8 p)
75 (let ([b (get-u8 p)])
76 (when (eof-object? b)
77 (error who "invalid eof encountered" p))
78 b))
79
80 (define (read-u8-as-char p)
81 (integer->char (read-u8 p)))
82
83 (define (assert-eq? x y)
84 (unless (eq? x y)
85 (die who
86 (format "Expected ~s, got ~s\n" y x))))
87
88 (define (char->int x)
89 (if (char? x)
90 (char->integer x)
91 (die who "unexpected eof inside a fasl object")))
92
93 (define (read-u32 p)
94 (let* ([c0 (read-u8 p)]
95 [c1 (read-u8 p)]
96 [c2 (read-u8 p)]
97 [c3 (read-u8 p)])
98 (bitwise-ior c0 (sll c1 8) (sll c2 16) (sll c3 24))))
99
100 (define (read-fixnum p)
101 (case (fixnum-width)
102 [(30)
103 (let* ([c0 (read-u8 p)]
104 [c1 (read-u8 p)]
105 [c2 (read-u8 p)]
106 [c3 (read-u8 p)])
107 (cond
108 [(fx<= c3 127)
109 (fxlogor (fxlogor (fxsra c0 2) (fxsll c1 6))
110 (fxlogor (fxsll c2 14) (fxsll c3 22)))]
111 [else
112 (let ([c0 (fxlogand #xFF (fxlognot c0))]
113 [c1 (fxlogand #xFF (fxlognot c1))]
114 [c2 (fxlogand #xFF (fxlognot c2))]
115 [c3 (fxlogand #xFF (fxlognot c3))])
116 (fx- -1
117 (fxlogor (fxlogor (fxsra c0 2)
118 (fxsll c1 6))
119 (fxlogor (fxsll c2 14)
120 (fxsll c3 22)))))]))]
121 [else
122 (let* ([u0 (read-u32 p)]
123 [u1 (read-u32 p)])
124 (if (<= u1 #x7FFFFFF)
125 (sra (bitwise-ior u0 (sll u1 32)) 3)
126 (let ([u0 (fxlogand #xFFFFFFFF (fxlognot u0))]
127 [u1 (fxlogand #xFFFFFFFF (fxlognot u1))])
128 (fx- -1
129 (fxlogor (fxsra u0 3) (fxsll u1 29))))))]))
130
131 (define (read-int p)
132 (case (fixnum-width)
133 [(30)
134 (let* ([c0 (char->int (read-u8-as-char p))]
135 [c1 (char->int (read-u8-as-char p))]
136 [c2 (char->int (read-u8-as-char p))]
137 [c3 (char->int (read-u8-as-char p))])
138 (cond
139 [(fx<= c3 127)
140 (fxlogor (fxlogor c0 (fxsll c1 8))
141 (fxlogor (fxsll c2 16) (fxsll c3 24)))]
142 [else
143 (let ([c0 (fxlogand #xFF (fxlognot c0))]
144 [c1 (fxlogand #xFF (fxlognot c1))]
145 [c2 (fxlogand #xFF (fxlognot c2))]
146 [c3 (fxlogand #xFF (fxlognot c3))])
147 (fx- -1
148 (fxlogor (fxlogor c0
149 (fxsll c1 8))
150 (fxlogor (fxsll c2 16)
151 (fxsll c3 24)))))]))]
152 [else
153 (let* ([u0 (read-u32 p)]
154 [u1 (read-u32 p)])
155 (if (<= u1 #x7FFFFFF)
156 (bitwise-ior u0 (sll u1 32))
157 (let ([u0 (fxlogand #xFFFFFFFF (fxlognot u0))]
158 [u1 (fxlogand #xFFFFFFFF (fxlognot u1))])
159 (- -1 (bitwise-ior u0 (sll u1 32))))))]))
160
161 (define (do-read p)
162 (define marks (make-vector 1 #f))
163 (define (max x y)
164 (if (fx> x y) x y))
165 (define (put-mark m obj)
166 (cond
167 [(fx< m (vector-length marks))
168 (when (vector-ref marks m)
169 (die 'fasl-read "mark set twice" m))
170 (vector-set! marks m obj)]
171 [else
172 (let ([n (vector-length marks)])
173 (let ([v (make-vector
174 (max (fx* n 2) (fx+ m 1))
175 #f)])
176 (let f ([i 0])
177 (cond
178 [(fx= i n)
179 (set! marks v)
180 (vector-set! marks m obj)]
181 [else
182 (vector-set! v i (vector-ref marks i))
183 (f (fxadd1 i))]))))]))
184 (define (read) (read/mark #f))
185 (define (read-code code-m clos-m)
186 (let* ([code-size (read-int p)]
187 [freevars (read-fixnum p)])
188 (let ([code (make-code code-size freevars)])
189 (when code-m (put-mark code-m code))
190 (let ([annotation (read)])
191 (set-code-annotation! code annotation))
192 (let f ([i 0])
193 (unless (fx= i code-size)
194 (code-set! code i (char->int (read-u8-as-char p)))
195 (f (fxadd1 i))))
196 (cond
197 [clos-m
198 (let ([clos ($code->closure code)])
199 (put-mark clos-m clos)
200 (set-code-reloc-vector! code (read))
201 code)]
202 [else
203 (set-code-reloc-vector! code (read))
204 code]))))
205 (define (read-procedure m)
206 (let ([c (read-u8-as-char p)])
207 (case c
208 [(#\x)
209 (let ([code (read-code #f m)])
210 (if m (vector-ref marks m) ($code->closure code)))]
211 [(#\<)
212 (let ([cm (read-u32 p)])
213 (unless (fx< cm (vector-length marks))
214 (die who "invalid mark" m))
215 (let ([code (vector-ref marks cm)])
216 (let ([proc ($code->closure code)])
217 (when m (put-mark m proc))
218 proc)))]
219 [(#\>)
220 (let ([cm (read-u32 p)])
221 (assert-eq? (read-u8-as-char p) #\x)
222 (let ([code (read-code cm m)])
223 (if m (vector-ref marks m) ($code->closure code))))]
224 [else (die who "invalid code header" c)])))
225
226 (define (read-list len m)
227 (let ([ls (make-list (+ len 1))])
228 (when m (put-mark m ls))
229 (let f ([ls ls])
230 (set-car! ls (read))
231 (let ([d (cdr ls)])
232 (if (null? d)
233 (set-cdr! ls (read))
234 (f d))))
235 ls))
236
237 (define (read/mark m)
238 (define (nom)
239 (when m (die who "unhandled mark")))
240 (let ([h (read-u8-as-char p)])
241 (case h
242 [(#\I)
243 (read-fixnum p)]
244 [(#\P)
245 (if m
246 (let ([x (cons #f #f)])
247 (put-mark m x)
248 (set-car! x (read))
249 (set-cdr! x (read))
250 x)
251 (let ([a (read)])
252 (cons a (read))))]
253 [(#\N) '()]
254 [(#\T) #t]
255 [(#\F) #f]
256 [(#\E) (eof-object)]
257 [(#\U) (void)]
258 [(#\s) ;;; string
259 (let ([n (read-int p)])
260 (let ([str (make-string n)])
261 (let f ([i 0])
262 (unless (fx= i n)
263 (let ([c (read-u8-as-char p)])
264 (string-set! str i c)
265 (f (fxadd1 i)))))
266 (when m (put-mark m str))
267 str))]
268 [(#\S) ;;; unicode string
269 (let ([n (read-int p)])
270 (let ([str (make-string n)])
271 (let f ([i 0])
272 (unless (fx= i n)
273 (let ([c (integer->char (read-u32 p))])
274 (string-set! str i c)
275 (f (fxadd1 i)))))
276 (when m (put-mark m str))
277 str))]
278 [(#\M) ;;; symbol
279 (let ([str (read)])
280 (let ([sym (string->symbol str)])
281 (when m (put-mark m sym))
282 sym))]
283 [(#\G)
284 (let* ([pretty (read)]
285 [unique (read)])
286 (let ([g (foreign-call "ikrt_strings_to_gensym" pretty unique)])
287 (when m (put-mark m g))
288 g))]
289 [(#\V) ;;; vector
290 (let ([n (read-int p)])
291 (let ([v (make-vector n)])
292 (when m (put-mark m v))
293 (let f ([i 0])
294 (unless (fx= i n)
295 (vector-set! v i (read))
296 (f (fxadd1 i))))
297 v))]
298 [(#\v) ;;; bytevector
299 (let ([n (read-int p)])
300 (let ([v (make-bytevector n)])
301 (when m (put-mark m v))
302 (let f ([i 0])
303 (unless (fx= i n)
304 (bytevector-u8-set! v i (read-u8 p))
305 (f (fxadd1 i))))
306 v))]
307 [(#\x) ;;; code
308 (read-code m #f)]
309 [(#\Q) ;;; procedure
310 (read-procedure m)]
311 [(#\R)
312 (let* ([rtd-name (read)]
313 [rtd-symbol (read)]
314 [field-count (read-int p)])
315 (let ([fields
316 (let f ([i 0])
317 (cond
318 [(fx= i field-count) '()]
319 [else
320 (let ([a (read)])
321 (cons a (f (fxadd1 i))))]))])
322 (let ([rtd (make-struct-type
323 rtd-name fields rtd-symbol)])
324 (when m (put-mark m rtd))
325 rtd)))]
326 [(#\{)
327 (let ([n (read-int p)])
328 (let ([rtd (read)])
329 (let ([x (make-struct rtd n)])
330 (when m (put-mark m x))
331 (let f ([i 0])
332 (unless (fx= i n)
333 ($struct-set! x i (read))
334 (f (fxadd1 i))))
335 x)))]
336 [(#\C) (integer->char (read-u32 p))]
337 [(#\c) (read-u8-as-char p)]
338 [(#\>)
339 (let ([m (read-u32 p)])
340 (read/mark m))]
341 [(#\<)
342 (let ([m (read-u32 p)])
343 (unless (fx< m (vector-length marks))
344 (die who "invalid mark" m))
345 (or (vector-ref marks m)
346 (error who "uninitialized mark" m)))]
347 [(#\l) ;;; list of length <= 255
348 (read-list (read-u8 p) m)]
349 [(#\L) ;;; list of length > 255
350 (read-list (read-int p) m)]
351 [(#\W) ;;; r6rs record type descriptor
352 (let* ([name (read)]
353 [parent (read)]
354 [uid (read)]
355 [sealed? (read)]
356 [opaque? (read)]
357 [n (read)]
358 [fields (make-vector n)])
359 (let f ([i 0])
360 (cond
361 [(= i n)
362 (let ([rtd (make-record-type-descriptor
363 name parent uid sealed? opaque?
364 fields)])
365 (when m (put-mark m rtd))
366 rtd)]
367 [else
368 (let* ([field-mutable? (read)]
369 [field-name (read)])
370 (vector-set! fields i
371 (list (if field-mutable? 'mutable 'immutable) field-name))
372 (f (+ i 1)))])))]
373 [(#\b) ;;; bignum
374 (let ([i (read-int p)])
375 (let ([bytes (if (< i 0) (- i) i)])
376 (let ([bv (get-bytevector-n p bytes)])
377 (let ([n (bytevector-uint-ref bv 0 'little bytes)])
378 (let ([n (if (< i 0) (- n) n)])
379 (when m (put-mark m n))
380 n)))))]
381 [(#\f) ;;; flonum
382 (let ()
383 (import (ikarus system $flonums))
384 (let ([x ($make-flonum)])
385 ($flonum-set! x 7 (get-u8 p))
386 ($flonum-set! x 6 (get-u8 p))
387 ($flonum-set! x 5 (get-u8 p))
388 ($flonum-set! x 4 (get-u8 p))
389 ($flonum-set! x 3 (get-u8 p))
390 ($flonum-set! x 2 (get-u8 p))
391 ($flonum-set! x 1 (get-u8 p))
392 ($flonum-set! x 0 (get-u8 p))
393 (when m (put-mark m x))
394 x))]
395 [(#\r) ;;; ratnum
396 (let* ([den (read)]
397 [num (read)])
398 (let ([x (/ num den)])
399 (when m (put-mark m x))
400 x))]
401 [(#\i) ;;; compnum
402 (let* ([real (read)]
403 [imag (read)])
404 (let ([x (make-rectangular real imag)])
405 (when m (put-mark m x))
406 x))]
407 [(#\h #\H)
408 (let ([x (if (eqv? h #\h) (make-eq-hashtable) (make-eqv-hashtable))])
409 (when m (put-mark m x))
410 (let* ([keys (read)] [vals (read)])
411 (vector-for-each
412 (lambda (k v) (hashtable-set! x k v))
413 keys vals))
414 x)]
415 [else
416 (die who "Unexpected char as a fasl object header" h p)])))
417 (read))
418 (define $fasl-read
419 (lambda (p)
420 (assert-eq? (read-u8-as-char p) #\#)
421 (assert-eq? (read-u8-as-char p) #\@)
422 (assert-eq? (read-u8-as-char p) #\I)
423 (assert-eq? (read-u8-as-char p) #\K)
424 (assert-eq? (read-u8-as-char p) #\0)
425 (case (fixnum-width)
426 [(30) (assert-eq? (read-u8-as-char p) #\1)]
427 [else (assert-eq? (read-u8-as-char p) #\2)])
428 (let ([v (do-read p)])
429 (unless (port-eof? p)
430 (printf "port did not reach eof\n"))
431 v)))
432
433 (define fasl-read
434 (case-lambda
435 [(p)
436 (if (input-port? p)
437 ($fasl-read p)
438 (die 'fasl-read "not an input port" p))]))
439
440 )
441