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/>.
|
|
|
|
|
2006-11-23 19:44:29 -05:00
|
|
|
|
|
|
|
;;; FASL
|
|
|
|
;;;
|
|
|
|
;;; A fasl object is a header followed by one or more objects followed by an
|
|
|
|
;;; end-of-fasl marker
|
|
|
|
;;;
|
|
|
|
;;; The header is the string "#@IK01"
|
|
|
|
;;; The end of fasl marker is "@"
|
|
|
|
;;;
|
|
|
|
;;; An object is either:
|
|
|
|
;;; "N" : denoting the empty list
|
|
|
|
;;; "T" : denoting #t
|
|
|
|
;;; "F" : denoting #f
|
|
|
|
;;; "E" : denoting the end of file object
|
|
|
|
;;; "U" : denoting the unspecified value
|
|
|
|
;;; "I" + 4-bytes : denoting a fixnum (in host byte order)
|
2007-06-17 19:49:40 -04:00
|
|
|
;;; "c" + 1-byte : denoting a small character (<= 255)
|
|
|
|
;;; "C" + 4-byte word: big char.
|
2006-11-23 19:44:29 -05:00
|
|
|
;;; "P" + object1 + object2 : a pair
|
2007-05-18 18:12:48 -04:00
|
|
|
;;; "V" + 4-bytes(n) + object ... : a vector of length n followed by n objects
|
|
|
|
;;; "v" + 4-byte(n) + octet ... : a bytevector of length n followed by n octets
|
2007-05-19 13:54:13 -04:00
|
|
|
;;; "s" + 4-bytes(n) + octet ... : an ascii string
|
|
|
|
;;; "S" + 4-bytes(n) + int ... : a unicode string
|
2006-11-23 19:44:29 -05:00
|
|
|
;;; "M" + symbol-name : a symbol
|
|
|
|
;;; "G" + pretty-name + unique-name : a gensym
|
|
|
|
;;; "R" + rtd-name + rtd-symbol + field-count + field-names
|
|
|
|
;;; "{" + field-count + rtd + fields
|
|
|
|
;;; ">" + 4-bytes(i) : mark the next object with index i
|
|
|
|
;;; "<" + 4-bytes(i) : dereference the object marked with index i
|
2006-12-03 15:32:40 -05:00
|
|
|
;;; "x" : denotes code
|
|
|
|
;;; "T" : Thunk; followed by code.
|
2007-06-14 12:38:00 -04:00
|
|
|
;;; "r" + numerator + denominator : ratnum
|
|
|
|
;;; "f" + 8-byte : IEEE flonum
|
|
|
|
;;; "b" + 4-byte(n) + n-bytes denotes a bignum (sign is sign of n).
|
2006-11-23 19:44:29 -05:00
|
|
|
|
|
|
|
|
2007-04-29 21:25:31 -04:00
|
|
|
|
2006-11-23 19:44:29 -05:00
|
|
|
|
2006-12-25 22:21:07 -05:00
|
|
|
|
2007-04-29 21:25:31 -04:00
|
|
|
|
|
|
|
(library (ikarus fasl read)
|
2007-05-09 19:37:24 -04:00
|
|
|
(export fasl-read)
|
2008-02-18 02:02:00 -05:00
|
|
|
(import (except (ikarus) fasl-read)
|
2008-02-14 17:45:15 -05:00
|
|
|
(except (ikarus.code-objects) procedure-annotation)
|
2007-05-09 19:37:24 -04:00
|
|
|
(ikarus system $codes)
|
2007-10-12 02:59:27 -04:00
|
|
|
(ikarus system $structs))
|
2007-04-29 21:25:31 -04:00
|
|
|
|
2006-12-25 22:21:07 -05:00
|
|
|
(define who 'fasl-read)
|
2008-02-18 02:02:00 -05:00
|
|
|
|
|
|
|
(define (read-u8 p)
|
|
|
|
(let ([b (get-u8 p)])
|
|
|
|
(when (eof-object? b)
|
|
|
|
(error who "invalid eof encountered" p))
|
|
|
|
b))
|
|
|
|
|
|
|
|
(define (read-u8-as-char p)
|
|
|
|
(integer->char (read-u8 p)))
|
|
|
|
|
2006-12-25 22:21:07 -05:00
|
|
|
(define (assert-eq? x y)
|
|
|
|
(unless (eq? x y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who
|
2007-10-25 14:32:26 -04:00
|
|
|
(format "Expected ~s, got ~s\n" y x))))
|
2008-02-18 02:02:00 -05:00
|
|
|
|
2006-12-25 22:32:59 -05:00
|
|
|
(define (char->int x)
|
|
|
|
(if (char? x)
|
|
|
|
(char->integer x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "unexpected eof inside a fasl object")))
|
2008-02-18 02:02:00 -05:00
|
|
|
|
2006-12-26 03:25:48 -05:00
|
|
|
(define (read-fixnum p)
|
2008-02-18 02:02:00 -05:00
|
|
|
(let* ([c0 (read-u8 p)]
|
|
|
|
[c1 (read-u8 p)]
|
|
|
|
[c2 (read-u8 p)]
|
|
|
|
[c3 (read-u8 p)])
|
2006-12-26 03:25:48 -05:00
|
|
|
(cond
|
|
|
|
[(fx<= c3 127)
|
|
|
|
(fxlogor (fxlogor (fxsra c0 2) (fxsll c1 6))
|
|
|
|
(fxlogor (fxsll c2 14) (fxsll c3 22)))]
|
|
|
|
[else
|
|
|
|
(let ([c0 (fxlogand #xFF (fxlognot c0))]
|
|
|
|
[c1 (fxlogand #xFF (fxlognot c1))]
|
|
|
|
[c2 (fxlogand #xFF (fxlognot c2))]
|
|
|
|
[c3 (fxlogand #xFF (fxlognot c3))])
|
|
|
|
(fx- -1
|
|
|
|
(fxlogor (fxlogor (fxsra c0 2)
|
|
|
|
(fxsll c1 6))
|
|
|
|
(fxlogor (fxsll c2 14)
|
|
|
|
(fxsll c3 22)))))])))
|
|
|
|
(define (read-int p)
|
2008-02-18 02:02:00 -05:00
|
|
|
(let* ([c0 (char->int (read-u8-as-char p))]
|
|
|
|
[c1 (char->int (read-u8-as-char p))]
|
|
|
|
[c2 (char->int (read-u8-as-char p))]
|
|
|
|
[c3 (char->int (read-u8-as-char p))])
|
2006-12-26 03:25:48 -05:00
|
|
|
(cond
|
|
|
|
[(fx<= c3 127)
|
|
|
|
(fxlogor (fxlogor c0 (fxsll c1 8))
|
|
|
|
(fxlogor (fxsll c2 16) (fxsll c3 24)))]
|
|
|
|
[else
|
|
|
|
(let ([c0 (fxlogand #xFF (fxlognot c0))]
|
|
|
|
[c1 (fxlogand #xFF (fxlognot c1))]
|
|
|
|
[c2 (fxlogand #xFF (fxlognot c2))]
|
|
|
|
[c3 (fxlogand #xFF (fxlognot c3))])
|
|
|
|
(fx- -1
|
|
|
|
(fxlogor (fxlogor c0
|
|
|
|
(fxsll c1 8))
|
|
|
|
(fxlogor (fxsll c2 16)
|
|
|
|
(fxsll c3 24)))))])))
|
2006-12-25 22:21:07 -05:00
|
|
|
(define (do-read p)
|
2006-12-26 03:25:48 -05:00
|
|
|
(define marks (make-vector 1 #f))
|
|
|
|
(define (max x y)
|
|
|
|
(if (fx> x y) x y))
|
|
|
|
(define (put-mark m obj)
|
|
|
|
(cond
|
|
|
|
[(fx< m (vector-length marks))
|
|
|
|
(when (vector-ref marks m)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fasl-read "mark set twice" m))
|
2006-12-26 03:25:48 -05:00
|
|
|
(vector-set! marks m obj)]
|
|
|
|
[else
|
|
|
|
(let ([n (vector-length marks)])
|
|
|
|
(let ([v (make-vector
|
|
|
|
(max (fx* n 2) (fx+ m 1))
|
|
|
|
#f)])
|
|
|
|
(let f ([i 0])
|
|
|
|
(cond
|
|
|
|
[(fx= i n)
|
|
|
|
(set! marks v)
|
|
|
|
(vector-set! marks m obj)]
|
|
|
|
[else
|
|
|
|
(vector-set! v i (vector-ref marks i))
|
|
|
|
(f (fxadd1 i))]))))]))
|
|
|
|
(define (read) (read/mark #f))
|
|
|
|
(define (read-code code-m clos-m)
|
|
|
|
(let* ([code-size (read-int p)]
|
|
|
|
[freevars (read-fixnum p)])
|
|
|
|
(let ([code (make-code code-size freevars)])
|
|
|
|
(when code-m (put-mark code-m code))
|
2008-02-18 19:15:47 -05:00
|
|
|
(let ([annotation (read)])
|
|
|
|
(set-code-annotation! code annotation))
|
|
|
|
(let f ([i 0])
|
|
|
|
(unless (fx= i code-size)
|
|
|
|
(code-set! code i (char->int (read-u8-as-char p)))
|
|
|
|
(f (fxadd1 i))))
|
|
|
|
(cond
|
|
|
|
[clos-m
|
|
|
|
(let ([clos ($code->closure code)])
|
|
|
|
(put-mark clos-m clos)
|
2006-12-26 03:25:48 -05:00
|
|
|
(set-code-reloc-vector! code (read))
|
2008-02-18 19:15:47 -05:00
|
|
|
code)]
|
|
|
|
[else
|
|
|
|
(set-code-reloc-vector! code (read))
|
|
|
|
code]))))
|
|
|
|
(define (read-procedure m)
|
2008-02-18 02:02:00 -05:00
|
|
|
(let ([c (read-u8-as-char p)])
|
2006-12-26 03:25:48 -05:00
|
|
|
(case c
|
|
|
|
[(#\x)
|
2006-12-26 04:03:43 -05:00
|
|
|
(let ([code (read-code #f m)])
|
|
|
|
(if m (vector-ref marks m) ($code->closure code)))]
|
2006-12-26 03:25:48 -05:00
|
|
|
[(#\<)
|
|
|
|
(let ([cm (read-int p)])
|
|
|
|
(unless (fx< cm (vector-length marks))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid mark" m))
|
2006-12-26 03:25:48 -05:00
|
|
|
(let ([code (vector-ref marks cm)])
|
|
|
|
(let ([proc ($code->closure code)])
|
|
|
|
(when m (put-mark m proc))
|
|
|
|
proc)))]
|
|
|
|
[(#\>)
|
|
|
|
(let ([cm (read-int p)])
|
2008-02-18 02:02:00 -05:00
|
|
|
(assert-eq? (read-u8-as-char p) #\x)
|
2006-12-26 04:03:43 -05:00
|
|
|
(let ([code (read-code cm m)])
|
|
|
|
(if m (vector-ref marks m) ($code->closure code))))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "invalid code header" c)])))
|
2006-12-26 03:25:48 -05:00
|
|
|
(define (read/mark m)
|
|
|
|
(define (nom)
|
2007-12-15 08:22:49 -05:00
|
|
|
(when m (die who "unhandled mark")))
|
2008-02-18 02:02:00 -05:00
|
|
|
(let ([h (read-u8-as-char p)])
|
2006-12-25 22:32:59 -05:00
|
|
|
(case h
|
2006-12-26 03:25:48 -05:00
|
|
|
[(#\I)
|
|
|
|
(read-fixnum p)]
|
2006-12-25 22:32:59 -05:00
|
|
|
[(#\P)
|
2006-12-26 03:25:48 -05:00
|
|
|
(if m
|
|
|
|
(let ([x (cons #f #f)])
|
|
|
|
(put-mark m x)
|
|
|
|
(set-car! x (read))
|
|
|
|
(set-cdr! x (read))
|
|
|
|
x)
|
|
|
|
(let ([a (read)])
|
|
|
|
(cons a (read))))]
|
2006-12-25 22:32:59 -05:00
|
|
|
[(#\N) '()]
|
2006-12-26 03:25:48 -05:00
|
|
|
[(#\T) #t]
|
|
|
|
[(#\F) #f]
|
|
|
|
[(#\E) (eof-object)]
|
|
|
|
[(#\U) (void)]
|
2008-02-18 02:02:00 -05:00
|
|
|
[(#\s) ;;; string
|
2006-12-26 03:25:48 -05:00
|
|
|
(let ([n (read-int p)])
|
|
|
|
(let ([str (make-string n)])
|
|
|
|
(let f ([i 0])
|
|
|
|
(unless (fx= i n)
|
2008-02-18 02:02:00 -05:00
|
|
|
(let ([c (read-u8-as-char p)])
|
2006-12-26 03:25:48 -05:00
|
|
|
(string-set! str i c)
|
|
|
|
(f (fxadd1 i)))))
|
|
|
|
(when m (put-mark m str))
|
|
|
|
str))]
|
|
|
|
[(#\M) ;;; symbol
|
|
|
|
(let ([str (read)])
|
|
|
|
(let ([sym (string->symbol str)])
|
|
|
|
(when m (put-mark m sym))
|
|
|
|
sym))]
|
|
|
|
[(#\G)
|
|
|
|
(let* ([pretty (read)]
|
|
|
|
[unique (read)])
|
2008-02-18 02:02:00 -05:00
|
|
|
(let ([g (foreign-call "ikrt_strings_to_gensym" pretty unique)])
|
|
|
|
(when m (put-mark m g))
|
|
|
|
g))]
|
2006-12-26 03:25:48 -05:00
|
|
|
[(#\V) ;;; vector
|
|
|
|
(let ([n (read-int p)])
|
|
|
|
(let ([v (make-vector n)])
|
|
|
|
(when m (put-mark m v))
|
|
|
|
(let f ([i 0])
|
|
|
|
(unless (fx= i n)
|
|
|
|
(vector-set! v i (read))
|
|
|
|
(f (fxadd1 i))))
|
|
|
|
v))]
|
2008-02-18 02:02:00 -05:00
|
|
|
[(#\v) ;;; bytevector
|
|
|
|
(let ([n (read-int p)])
|
|
|
|
(let ([v (make-bytevector n)])
|
|
|
|
(when m (put-mark m v))
|
|
|
|
(let f ([i 0])
|
|
|
|
(unless (fx= i n)
|
|
|
|
(bytevector-u8-set! v i (read-u8 p))
|
|
|
|
(f (fxadd1 i))))
|
|
|
|
v))]
|
2006-12-26 03:25:48 -05:00
|
|
|
[(#\x) ;;; code
|
|
|
|
(read-code m #f)]
|
2008-02-18 19:15:47 -05:00
|
|
|
[(#\Q) ;;; procedure
|
|
|
|
(read-procedure m)]
|
2006-12-26 03:25:48 -05:00
|
|
|
[(#\R)
|
|
|
|
(let* ([rtd-name (read)]
|
|
|
|
[rtd-symbol (read)]
|
|
|
|
[field-count (read-int p)])
|
|
|
|
(let ([fields
|
|
|
|
(let f ([i 0])
|
|
|
|
(cond
|
|
|
|
[(fx= i field-count) '()]
|
|
|
|
[else
|
|
|
|
(let ([a (read)])
|
|
|
|
(cons a (f (fxadd1 i))))]))])
|
2007-10-12 02:59:27 -04:00
|
|
|
(let ([rtd (make-struct-type
|
2006-12-26 03:25:48 -05:00
|
|
|
rtd-name fields rtd-symbol)])
|
|
|
|
(when m (put-mark m rtd))
|
|
|
|
rtd)))]
|
|
|
|
[(#\{)
|
|
|
|
(let ([n (read-int p)])
|
|
|
|
(let ([rtd (read)])
|
2007-10-12 02:59:27 -04:00
|
|
|
(let ([x ($make-struct rtd n)])
|
2006-12-26 03:25:48 -05:00
|
|
|
(when m (put-mark m x))
|
|
|
|
(let f ([i 0])
|
|
|
|
(unless (fx= i n)
|
2007-10-12 02:59:27 -04:00
|
|
|
($struct-set! x i (read))
|
2006-12-26 03:25:48 -05:00
|
|
|
(f (fxadd1 i))))
|
|
|
|
x)))]
|
2008-02-18 19:15:47 -05:00
|
|
|
[(#\C) (integer->char (read-int p))]
|
|
|
|
[(#\c) (read-u8-as-char p)]
|
2006-12-26 03:25:48 -05:00
|
|
|
[(#\>)
|
|
|
|
(let ([m (read-int p)])
|
|
|
|
(read/mark m))]
|
|
|
|
[(#\<)
|
|
|
|
(let ([m (read-int p)])
|
|
|
|
(unless (fx< m (vector-length marks))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid mark" m))
|
2008-02-18 02:02:00 -05:00
|
|
|
(or (vector-ref marks m)
|
|
|
|
(error who "uninitialized mark" m)))]
|
|
|
|
[(#\l) ;;; list of length <= 255
|
|
|
|
(let ([n (read-u8 p)])
|
|
|
|
(let f ([n n])
|
|
|
|
(cond
|
|
|
|
[(< n 0) (read)]
|
|
|
|
[else
|
|
|
|
(let ([x (read)])
|
|
|
|
(cons x (f (- n 1))))])))]
|
2006-12-26 03:25:48 -05:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "Unexpected char as a fasl object header" h)])))
|
2006-12-25 22:32:59 -05:00
|
|
|
(read))
|
2007-05-05 23:10:47 -04:00
|
|
|
(define $fasl-read
|
2006-12-25 22:21:07 -05:00
|
|
|
(lambda (p)
|
2008-02-18 02:02:00 -05:00
|
|
|
(assert-eq? (read-u8-as-char p) #\#)
|
|
|
|
(assert-eq? (read-u8-as-char p) #\@)
|
|
|
|
(assert-eq? (read-u8-as-char p) #\I)
|
|
|
|
(assert-eq? (read-u8-as-char p) #\K)
|
|
|
|
(assert-eq? (read-u8-as-char p) #\0)
|
|
|
|
(assert-eq? (read-u8-as-char p) #\1)
|
|
|
|
(let ([v (do-read p)])
|
|
|
|
(unless (port-eof? p)
|
|
|
|
(printf "port did not reach eof\n"))
|
|
|
|
v)))
|
2007-05-09 19:37:24 -04:00
|
|
|
|
|
|
|
(define fasl-read
|
|
|
|
(case-lambda
|
|
|
|
[(p)
|
|
|
|
(if (input-port? p)
|
|
|
|
($fasl-read p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fasl-read "not an input port" p))]))
|
2007-05-09 19:37:24 -04:00
|
|
|
|
|
|
|
)
|
2006-12-25 22:32:59 -05:00
|
|
|
|