2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-15 11:37:43 -04:00
|
|
|
|
2008-02-14 17:45:15 -05:00
|
|
|
(library (ikarus.fasl.write)
|
2007-05-15 11:37:43 -04:00
|
|
|
(export fasl-write)
|
|
|
|
(import
|
2007-10-09 09:22:02 -04:00
|
|
|
(rnrs hashtables)
|
2007-05-15 11:37:43 -04:00
|
|
|
(ikarus system $codes)
|
2007-05-22 19:01:07 -04:00
|
|
|
(ikarus system $pairs)
|
2007-10-12 02:59:27 -04:00
|
|
|
(ikarus system $structs)
|
2007-05-18 18:12:48 -04:00
|
|
|
(ikarus system $bytevectors)
|
|
|
|
(ikarus system $fx)
|
2007-05-21 23:09:45 -04:00
|
|
|
(ikarus system $chars)
|
|
|
|
(ikarus system $strings)
|
2007-06-14 12:38:00 -04:00
|
|
|
(ikarus system $flonums)
|
|
|
|
(ikarus system $bignums)
|
2008-02-14 17:45:15 -05:00
|
|
|
(except (ikarus.code-objects) procedure-annotation)
|
2007-12-10 07:28:03 -05:00
|
|
|
(except (ikarus) fasl-write write-byte))
|
2008-01-02 23:22:55 -05:00
|
|
|
|
|
|
|
(module (wordsize)
|
|
|
|
(include "ikarus.config.ss"))
|
|
|
|
|
2008-02-10 05:46:58 -05:00
|
|
|
;;; (define-syntax fxshift
|
|
|
|
;;; (identifier-syntax
|
|
|
|
;;; (case wordsize
|
|
|
|
;;; [(4) 2]
|
|
|
|
;;; [(8) 3]
|
|
|
|
;;; [else (error 'fxshift "invalid wordsize" wordsize)])))
|
2008-01-02 23:22:55 -05:00
|
|
|
|
2008-02-10 05:46:58 -05:00
|
|
|
;;; (define-syntax intbits (identifier-syntax (* wordsize 8)))
|
2008-01-02 23:22:55 -05:00
|
|
|
|
2008-02-10 05:46:58 -05:00
|
|
|
;;; (define-syntax fxbits (identifier-syntax (- intbits fxshift)))
|
2008-01-02 23:22:55 -05:00
|
|
|
|
2008-02-10 05:46:58 -05:00
|
|
|
(define fxshift
|
|
|
|
(case wordsize
|
|
|
|
[(4) 2]
|
|
|
|
[(8) 3]
|
|
|
|
[else (error 'fxshift "invalid wordsize" wordsize)]))
|
2008-02-10 03:27:31 -05:00
|
|
|
|
2008-02-10 05:46:58 -05:00
|
|
|
(define intbits (* wordsize 8))
|
2008-02-10 03:27:31 -05:00
|
|
|
|
2008-02-10 05:46:58 -05:00
|
|
|
(define fxbits (- intbits fxshift))
|
2008-02-10 03:27:31 -05:00
|
|
|
|
2008-01-02 23:22:55 -05:00
|
|
|
(define (fx? x)
|
|
|
|
(and (or (fixnum? x) (bignum? x))
|
|
|
|
(<= (- (expt 2 (- fxbits 1)))
|
|
|
|
x
|
|
|
|
(- (expt 2 (- fxbits 1)) 1))))
|
|
|
|
|
|
|
|
(define (int? x)
|
|
|
|
(and (or (fixnum? x) (bignum? x))
|
|
|
|
(<= (- (expt 2 (- intbits 1)))
|
|
|
|
x
|
|
|
|
(- (expt 2 (- intbits 1)) 1))))
|
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
(define-syntax write-byte
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ byte port)
|
|
|
|
(put-u8 port byte)]))
|
|
|
|
|
2007-12-08 14:52:35 -05:00
|
|
|
(define (put-tag c p)
|
|
|
|
(write-byte (char->integer c) p))
|
|
|
|
|
2008-01-03 02:07:17 -05:00
|
|
|
(define write-int32
|
2007-05-15 11:37:43 -04:00
|
|
|
(lambda (x p)
|
2008-01-02 23:22:55 -05:00
|
|
|
(write-byte (bitwise-and x #xFF) p)
|
|
|
|
(write-byte (bitwise-and (sra x 8) #xFF) p)
|
|
|
|
(write-byte (bitwise-and (sra x 16) #xFF) p)
|
2008-01-03 02:07:17 -05:00
|
|
|
(write-byte (bitwise-and (sra x 24) #xFF) p)))
|
|
|
|
|
|
|
|
(define write-int
|
|
|
|
(lambda (x p)
|
|
|
|
(unless (int? x) (die 'write-int "not a int" x))
|
|
|
|
(write-int32 x p)
|
2008-01-02 23:22:55 -05:00
|
|
|
(when (eqv? wordsize 8)
|
2008-01-03 02:07:17 -05:00
|
|
|
(write-int32 (sra x 32) p))))
|
2008-01-02 23:22:55 -05:00
|
|
|
|
2007-05-15 11:37:43 -04:00
|
|
|
(define fasl-write-immediate
|
|
|
|
(lambda (x p)
|
|
|
|
(cond
|
2007-12-08 14:52:35 -05:00
|
|
|
[(null? x) (put-tag #\N p)]
|
2008-01-02 23:22:55 -05:00
|
|
|
[(fx? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\I p)
|
2008-01-02 23:22:55 -05:00
|
|
|
(write-int (bitwise-arithmetic-shift-left x fxshift) p)]
|
2007-05-15 11:37:43 -04:00
|
|
|
[(char? x)
|
2007-06-17 19:49:40 -04:00
|
|
|
(let ([n ($char->fixnum x)])
|
|
|
|
(if ($fx<= n 255)
|
|
|
|
(begin
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\c p)
|
2007-06-17 19:49:40 -04:00
|
|
|
(write-byte n p))
|
|
|
|
(begin
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\C p)
|
2008-01-03 02:07:17 -05:00
|
|
|
(write-int32 n p))))]
|
2007-05-15 11:37:43 -04:00
|
|
|
[(boolean? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag (if x #\T #\F) p)]
|
|
|
|
[(eof-object? x) (put-tag #\E p)]
|
|
|
|
[(eq? x (void)) (put-tag #\U p)]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'fasl-write "not a fasl-writable immediate" x)])))
|
2007-05-15 11:37:43 -04:00
|
|
|
|
2007-05-21 23:09:45 -04:00
|
|
|
(define (ascii-string? s)
|
|
|
|
(let f ([s s] [i 0] [n (string-length s)])
|
|
|
|
(or ($fx= i n)
|
|
|
|
(and ($char<= ($string-ref s i) ($fixnum->char 127))
|
|
|
|
(f s ($fxadd1 i) n)))))
|
2007-12-08 14:52:35 -05:00
|
|
|
|
2007-05-22 19:01:07 -04:00
|
|
|
(define (count-unshared-cdrs x h n)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(and (pair? x) (eq? (hashtable-ref h x #f) 0))
|
2007-05-22 19:01:07 -04:00
|
|
|
(count-unshared-cdrs ($cdr x) h ($fxadd1 n))]
|
|
|
|
[else n]))
|
2007-12-08 14:52:35 -05:00
|
|
|
|
2007-05-22 19:01:07 -04:00
|
|
|
(define (write-pairs x p h m n)
|
|
|
|
(cond
|
|
|
|
[($fx= n 0) (fasl-write-object x p h m)]
|
|
|
|
[else
|
|
|
|
(write-pairs (cdr x) p h
|
|
|
|
(fasl-write-object (car x) p h m)
|
|
|
|
($fxsub1 n))]))
|
|
|
|
|
2007-05-15 11:37:43 -04:00
|
|
|
(define do-write
|
|
|
|
(lambda (x p h m)
|
|
|
|
(cond
|
2007-05-22 19:01:07 -04:00
|
|
|
[(pair? x)
|
|
|
|
(let ([d ($cdr x)])
|
|
|
|
(let ([n (count-unshared-cdrs d h 0)])
|
|
|
|
(cond
|
|
|
|
[($fx= n 0)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\P p)
|
2007-05-22 19:01:07 -04:00
|
|
|
(fasl-write-object d p h
|
|
|
|
(fasl-write-object (car x) p h m))]
|
|
|
|
[else
|
|
|
|
(cond
|
|
|
|
[($fx<= n 255)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\l p)
|
2007-05-22 19:01:07 -04:00
|
|
|
(write-byte n p)]
|
|
|
|
[else
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\L p)
|
2007-05-22 19:01:07 -04:00
|
|
|
(write-int n p)])
|
|
|
|
(write-pairs d p h
|
|
|
|
(fasl-write-object (car x) p h m)
|
|
|
|
n)])))]
|
2007-05-15 11:37:43 -04:00
|
|
|
[(vector? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\V p)
|
2007-05-15 11:37:43 -04:00
|
|
|
(write-int (vector-length x) p)
|
|
|
|
(let f ([x x] [i 0] [n (vector-length x)] [m m])
|
|
|
|
(cond
|
|
|
|
[(fx= i n) m]
|
|
|
|
[else
|
|
|
|
(f x (fxadd1 i) n
|
|
|
|
(fasl-write-object (vector-ref x i) p h m))]))]
|
|
|
|
[(string? x)
|
2007-05-21 23:09:45 -04:00
|
|
|
(cond
|
|
|
|
[(ascii-string? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\s p)
|
2007-05-21 23:09:45 -04:00
|
|
|
(write-int (string-length x) p)
|
|
|
|
(let f ([x x] [i 0] [n (string-length x)])
|
|
|
|
(unless (fx= i n)
|
2007-12-08 14:52:35 -05:00
|
|
|
(write-byte (char->integer (string-ref x i)) p)
|
2007-05-21 23:09:45 -04:00
|
|
|
(f x (fxadd1 i) n)))]
|
|
|
|
[else
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\S p)
|
2007-05-21 23:09:45 -04:00
|
|
|
(write-int (string-length x) p)
|
|
|
|
(let f ([x x] [i 0] [n (string-length x)])
|
|
|
|
(unless (= i n)
|
2008-01-03 02:07:17 -05:00
|
|
|
(write-int32 (char->integer (string-ref x i)) p)
|
2007-05-21 23:09:45 -04:00
|
|
|
(f x (fxadd1 i) n)))])
|
|
|
|
m]
|
2007-05-15 11:37:43 -04:00
|
|
|
[(gensym? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\G p)
|
2007-05-15 11:37:43 -04:00
|
|
|
(fasl-write-object (gensym->unique-string x) p h
|
|
|
|
(fasl-write-object (symbol->string x) p h m))]
|
|
|
|
[(symbol? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\M p)
|
2007-05-15 11:37:43 -04:00
|
|
|
(fasl-write-object (symbol->string x) p h m)]
|
|
|
|
[(code? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\x p)
|
2007-05-15 11:37:43 -04:00
|
|
|
(write-int (code-size x) p)
|
2008-01-02 23:22:55 -05:00
|
|
|
(write-int (bitwise-arithmetic-shift-left
|
|
|
|
(code-freevars x)
|
|
|
|
fxshift)
|
|
|
|
p)
|
2007-10-10 04:41:11 -04:00
|
|
|
(let ([m (fasl-write-object ($code-annotation x) p h m)])
|
|
|
|
(let f ([i 0] [n (code-size x)])
|
|
|
|
(unless (fx= i n)
|
|
|
|
(write-byte (code-ref x i) p)
|
|
|
|
(f (fxadd1 i) n)))
|
|
|
|
(fasl-write-object (code-reloc-vector x) p h m))]
|
2008-12-23 21:40:09 -05:00
|
|
|
[(hashtable? x)
|
|
|
|
(let ([v (hashtable-ref h x #f)])
|
|
|
|
(if (eq? eq? (hashtable-equivalence-function x))
|
|
|
|
(put-tag #\h p)
|
|
|
|
(put-tag #\H p))
|
|
|
|
(fasl-write-object (vector-ref v 2) p h
|
|
|
|
(fasl-write-object (vector-ref v 1) p h m)))]
|
2007-10-12 02:59:27 -04:00
|
|
|
[(struct? x)
|
2008-02-26 03:53:00 -05:00
|
|
|
(cond
|
|
|
|
[(record-type-descriptor? x)
|
|
|
|
(put-tag #\W p)
|
|
|
|
(let* ([m (fasl-write-object (record-type-name x) p h m)]
|
|
|
|
[m (fasl-write-object (record-type-parent x) p h m)]
|
|
|
|
[m (fasl-write-object (record-type-uid x) p h m)])
|
|
|
|
(fasl-write-immediate (record-type-sealed? x) p)
|
|
|
|
(fasl-write-immediate (record-type-opaque? x) p)
|
|
|
|
(let* ([fields (record-type-field-names x)]
|
|
|
|
[n (vector-length fields)])
|
|
|
|
(fasl-write-immediate n p)
|
|
|
|
(let f ([i 0] [m m])
|
2007-05-15 11:37:43 -04:00
|
|
|
(cond
|
2008-02-26 03:53:00 -05:00
|
|
|
[(= i n) m]
|
2007-05-15 11:37:43 -04:00
|
|
|
[else
|
2008-02-26 03:53:00 -05:00
|
|
|
(fasl-write-immediate (record-field-mutable? x i) p)
|
|
|
|
(f (+ i 1)
|
|
|
|
(fasl-write-object (vector-ref fields i) p h m))]))))]
|
|
|
|
[else
|
|
|
|
(let ([rtd (struct-type-descriptor x)])
|
|
|
|
(cond
|
|
|
|
[(eq? rtd (base-rtd))
|
|
|
|
;;; rtd record
|
|
|
|
(put-tag #\R p)
|
|
|
|
(let ([names (struct-type-field-names x)]
|
|
|
|
[m
|
|
|
|
(fasl-write-object (struct-type-symbol x) p h
|
|
|
|
(fasl-write-object (struct-type-name x) p h m))])
|
|
|
|
(write-int (length names) p)
|
|
|
|
(let f ([names names] [m m])
|
|
|
|
(cond
|
|
|
|
[(null? names) m]
|
|
|
|
[else
|
|
|
|
(f (cdr names)
|
|
|
|
(fasl-write-object (car names) p h m))])))]
|
|
|
|
[else
|
|
|
|
;;; non-rtd record
|
|
|
|
(put-tag #\{ p)
|
|
|
|
(let ([n (struct-length x)])
|
|
|
|
(write-int n p)
|
|
|
|
(let f ([i 0]
|
|
|
|
[m (fasl-write-object rtd p h m)])
|
|
|
|
(cond
|
|
|
|
[(= i n) m]
|
|
|
|
[else
|
|
|
|
(f (+ i 1)
|
|
|
|
(fasl-write-object
|
|
|
|
(struct-ref x i)
|
|
|
|
p h m))])))]))])]
|
2007-05-15 11:37:43 -04:00
|
|
|
[(procedure? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\Q p)
|
2007-05-15 11:37:43 -04:00
|
|
|
(fasl-write-object ($closure-code x) p h m)]
|
2007-05-18 18:12:48 -04:00
|
|
|
[(bytevector? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\v p)
|
2007-05-18 18:12:48 -04:00
|
|
|
(let ([n ($bytevector-length x)])
|
|
|
|
(write-int n p)
|
|
|
|
(write-bytevector x 0 n p))
|
|
|
|
m]
|
2007-06-14 12:38:00 -04:00
|
|
|
[(flonum? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\f p)
|
2007-06-14 12:38:00 -04:00
|
|
|
(write-byte ($flonum-u8-ref x 7) p)
|
2007-06-14 12:48:57 -04:00
|
|
|
(write-byte ($flonum-u8-ref x 6) p)
|
|
|
|
(write-byte ($flonum-u8-ref x 5) p)
|
|
|
|
(write-byte ($flonum-u8-ref x 4) p)
|
|
|
|
(write-byte ($flonum-u8-ref x 3) p)
|
|
|
|
(write-byte ($flonum-u8-ref x 2) p)
|
|
|
|
(write-byte ($flonum-u8-ref x 1) p)
|
|
|
|
(write-byte ($flonum-u8-ref x 0) p)
|
2007-06-14 12:38:00 -04:00
|
|
|
m]
|
|
|
|
[(ratnum? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\r p)
|
2007-06-14 12:38:00 -04:00
|
|
|
(fasl-write-object (numerator x) p h
|
|
|
|
(fasl-write-object (denominator x) p h m))]
|
|
|
|
[(bignum? x)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\b p)
|
2007-06-14 12:38:00 -04:00
|
|
|
(let ([sz ($bignum-size x)])
|
|
|
|
(write-int (if ($bignum-positive? x) sz (- sz)) p)
|
|
|
|
(let f ([i 0])
|
|
|
|
(unless (fx= i sz)
|
|
|
|
(write-byte ($bignum-byte-ref x i) p)
|
|
|
|
(f (fxadd1 i)))))
|
|
|
|
m]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(or (compnum? x) (cflonum? x))
|
2008-05-21 03:40:42 -04:00
|
|
|
(put-tag #\i p)
|
|
|
|
(fasl-write-object (imag-part x) p h
|
|
|
|
(fasl-write-object (real-part x) p h m))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'fasl-write "not fasl-writable" x)])))
|
2007-05-18 18:12:48 -04:00
|
|
|
(define (write-bytevector x i j p)
|
|
|
|
(unless ($fx= i j)
|
2007-12-10 07:28:03 -05:00
|
|
|
(write-byte ($bytevector-u8-ref x i) p)
|
2007-05-18 18:12:48 -04:00
|
|
|
(write-bytevector x ($fxadd1 i) j p)))
|
2007-05-15 11:37:43 -04:00
|
|
|
(define fasl-write-object
|
|
|
|
(lambda (x p h m)
|
|
|
|
(cond
|
|
|
|
[(immediate? x) (fasl-write-immediate x p) m]
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2008-12-23 21:40:09 -05:00
|
|
|
(lambda (mk)
|
|
|
|
(let ([mark (if (fixnum? mk) mk (vector-ref mk 0))])
|
|
|
|
(cond
|
|
|
|
[(fx= mark 0) ; singly referenced
|
|
|
|
(do-write x p h m)]
|
|
|
|
[(fx> mark 0) ; marked but not written
|
|
|
|
(if (fixnum? mk)
|
|
|
|
(hashtable-set! h x (fx- 0 m))
|
|
|
|
(vector-set! mk 0 (fx- 0 m)))
|
|
|
|
(put-tag #\> p)
|
|
|
|
(write-int32 m p)
|
|
|
|
(do-write x p h (fxadd1 m))]
|
|
|
|
[else
|
|
|
|
(put-tag #\< p)
|
|
|
|
(write-int32 (fx- 0 mark) p)
|
|
|
|
m])))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'fasl-write "BUG: not in hash table" x)])))
|
2007-05-15 11:37:43 -04:00
|
|
|
(define make-graph
|
|
|
|
(lambda (x h)
|
|
|
|
(unless (immediate? x)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2007-05-15 11:37:43 -04:00
|
|
|
(lambda (i)
|
2008-12-23 21:40:09 -05:00
|
|
|
(if (vector? i)
|
|
|
|
(vector-set! i 0 (fxadd1 (vector-ref i 0)))
|
|
|
|
(hashtable-set! h x (fxadd1 i))))]
|
2007-05-15 11:37:43 -04:00
|
|
|
[else
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x 0)
|
2007-05-15 11:37:43 -04:00
|
|
|
(cond
|
|
|
|
[(pair? x)
|
|
|
|
(make-graph (car x) h)
|
|
|
|
(make-graph (cdr x) h)]
|
|
|
|
[(vector? x)
|
|
|
|
(let f ([x x] [i 0] [n (vector-length x)])
|
|
|
|
(unless (fx= i n)
|
|
|
|
(make-graph (vector-ref x i) h)
|
|
|
|
(f x (fxadd1 i) n)))]
|
|
|
|
[(symbol? x)
|
|
|
|
(make-graph (symbol->string x) h)
|
|
|
|
(when (gensym? x) (make-graph (gensym->unique-string x) h))]
|
|
|
|
[(string? x) (void)]
|
|
|
|
[(code? x)
|
2007-10-10 04:41:11 -04:00
|
|
|
(make-graph ($code-annotation x) h)
|
2007-05-15 11:37:43 -04:00
|
|
|
(make-graph (code-reloc-vector x) h)]
|
2008-12-23 21:40:09 -05:00
|
|
|
[(hashtable? x)
|
|
|
|
(when (hashtable-hash-function x)
|
|
|
|
(die 'fasl-write "not fasl-writable" x))
|
|
|
|
(let-values ([(keys vals) (hashtable-entries x)])
|
|
|
|
(make-graph keys h)
|
|
|
|
(make-graph vals h)
|
|
|
|
(hashtable-set! h x (vector 0 keys vals)))]
|
2007-10-12 02:59:27 -04:00
|
|
|
[(struct? x)
|
2008-02-26 03:53:00 -05:00
|
|
|
(cond
|
|
|
|
[(eq? x (base-rtd))
|
|
|
|
(die 'fasl-write "base-rtd is not writable")]
|
|
|
|
[(record-type-descriptor? x)
|
|
|
|
(make-graph (record-type-name x) h)
|
|
|
|
(make-graph (record-type-parent x) h)
|
|
|
|
(make-graph (record-type-uid x) h)
|
|
|
|
(vector-for-each
|
|
|
|
(lambda (x) (make-graph x h))
|
|
|
|
(record-type-field-names x))]
|
|
|
|
[else
|
|
|
|
(let ([rtd (struct-type-descriptor x)])
|
|
|
|
(cond
|
|
|
|
[(eq? rtd (base-rtd))
|
|
|
|
;;; this is a struct rtd
|
|
|
|
(make-graph (struct-type-name x) h)
|
|
|
|
(make-graph (struct-type-symbol x) h)
|
|
|
|
(for-each (lambda (x) (make-graph x h))
|
|
|
|
(struct-type-field-names x))]
|
|
|
|
[else
|
|
|
|
;;; this is a struct
|
|
|
|
(make-graph rtd h)
|
|
|
|
(let f ([i 0] [n (struct-length x)])
|
|
|
|
(unless (= i n)
|
|
|
|
(make-graph (struct-ref x i) h)
|
|
|
|
(f (+ i 1) n)))]))])]
|
2007-05-15 11:37:43 -04:00
|
|
|
[(procedure? x)
|
|
|
|
(let ([code ($closure-code x)])
|
|
|
|
(unless (fxzero? (code-freevars code))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fasl-write
|
2007-12-08 14:52:35 -05:00
|
|
|
"Cannot write a non-thunk procedure; \
|
|
|
|
the one given has free vars"
|
|
|
|
(code-freevars code)))
|
2007-05-15 11:37:43 -04:00
|
|
|
(make-graph code h))]
|
2007-05-18 18:12:48 -04:00
|
|
|
[(bytevector? x) (void)]
|
2007-06-14 12:38:00 -04:00
|
|
|
[(flonum? x) (void)]
|
|
|
|
[(bignum? x) (void)]
|
|
|
|
[(ratnum? x)
|
|
|
|
(make-graph (numerator x) h)
|
|
|
|
(make-graph (denominator x) h)]
|
2008-05-24 13:13:01 -04:00
|
|
|
[(or (compnum? x) (cflonum? x))
|
2008-05-21 03:40:42 -04:00
|
|
|
(make-graph (real-part x) h)
|
|
|
|
(make-graph (imag-part x) h)]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'fasl-write "not fasl-writable" x)])]))))
|
2007-05-15 11:37:43 -04:00
|
|
|
(define fasl-write-to-port
|
|
|
|
(lambda (x port)
|
2007-10-10 07:09:18 -04:00
|
|
|
(let ([h (make-eq-hashtable)])
|
2007-05-15 11:37:43 -04:00
|
|
|
(make-graph x h)
|
2007-12-08 14:52:35 -05:00
|
|
|
(put-tag #\# port)
|
|
|
|
(put-tag #\@ port)
|
|
|
|
(put-tag #\I port)
|
|
|
|
(put-tag #\K port)
|
|
|
|
(put-tag #\0 port)
|
2008-01-02 23:22:55 -05:00
|
|
|
(put-tag (if (= wordsize 4) #\1 #\2) port)
|
2007-05-15 11:37:43 -04:00
|
|
|
(fasl-write-object x port h 1)
|
|
|
|
(void))))
|
|
|
|
(define fasl-write
|
2007-12-08 14:52:35 -05:00
|
|
|
(case-lambda
|
2008-02-26 04:07:33 -05:00
|
|
|
[(x p)
|
|
|
|
(cond
|
|
|
|
[(not (output-port? p))
|
|
|
|
(die 'fasl-write "not an output port" p)]
|
|
|
|
[(not (binary-port? p))
|
|
|
|
(die 'fasl-write "not a binary port" p)]
|
|
|
|
[else (fasl-write-to-port x p)])])))
|