| 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 |
(library (ikarus load) |
| 18 |
(export load load-r6rs-script fasl-directory) |
| 19 |
(import |
| 20 |
(except (ikarus) fasl-directory load load-r6rs-script) |
| 21 |
(only (ikarus.compiler) compile-core-expr) |
| 22 |
(only (psyntax library-manager) |
| 23 |
serialize-all current-precompiled-library-loader) |
| 24 |
(only (psyntax expander) compile-r6rs-top-level) |
| 25 |
(only (ikarus.reader.annotated) read-script-source-file)) |
| 26 |
|
| 27 |
(define-struct serialized-library (contents)) |
| 28 |
|
| 29 |
(define fasl-extension |
| 30 |
(cond |
| 31 |
[(<= (fixnum-width) 32) ".ikarus-32bit-fasl"] |
| 32 |
[else ".ikarus-64bit-fasl"])) |
| 33 |
|
| 34 |
(define fasl-directory |
| 35 |
(make-parameter |
| 36 |
(cond |
| 37 |
[(getenv "IKARUS_FASL_DIRECTORY")] |
| 38 |
[(getenv "HOME") => |
| 39 |
(lambda (s) |
| 40 |
(string-append s "/.ikarus/precompiled"))] |
| 41 |
[else ""]) |
| 42 |
(lambda (s) |
| 43 |
(if (string? s) |
| 44 |
s |
| 45 |
(die 'fasl-directory "not a string" s))))) |
| 46 |
|
| 47 |
(define (fasl-path filename) |
| 48 |
(let ([d (fasl-directory)]) |
| 49 |
(and (not (string=? d "")) |
| 50 |
(string-append d (file-real-path filename) fasl-extension)))) |
| 51 |
|
| 52 |
(define (load-serialized-library filename sk) |
| 53 |
(let ([ikfasl (fasl-path filename)]) |
| 54 |
(cond |
| 55 |
[(or (not ikfasl) (not (file-exists? ikfasl))) #f] |
| 56 |
[(< (file-mtime ikfasl) (file-mtime filename)) |
| 57 |
(fprintf (current-error-port) |
| 58 |
"WARNING: not using fasl file ~s because it is older \ |
| 59 |
than the source file ~s\n" |
| 60 |
ikfasl |
| 61 |
filename) |
| 62 |
#f] |
| 63 |
[else |
| 64 |
(let ([x |
| 65 |
(let ([p (open-file-input-port ikfasl)]) |
| 66 |
(let ([x (fasl-read p)]) |
| 67 |
(close-input-port p) |
| 68 |
x))]) |
| 69 |
(if (serialized-library? x) |
| 70 |
(apply sk (serialized-library-contents x)) |
| 71 |
(begin |
| 72 |
(fprintf (current-error-port) |
| 73 |
"WARNING: not using fasl file ~s because it was \ |
| 74 |
compiled with a different instance of ikarus.\n" |
| 75 |
ikfasl) |
| 76 |
#f)))]))) |
| 77 |
|
| 78 |
(define (do-serialize-library filename contents) |
| 79 |
(let ([ikfasl (fasl-path filename)]) |
| 80 |
(cond |
| 81 |
[(not ikfasl) (void)] |
| 82 |
[else |
| 83 |
(fprintf (current-error-port) "Serializing ~s ...\n" ikfasl) |
| 84 |
(let-values ([(dir name) (split-file-name ikfasl)]) |
| 85 |
(make-directory* dir)) |
| 86 |
(let ([p (open-file-output-port ikfasl (file-options no-fail))]) |
| 87 |
(fasl-write (make-serialized-library contents) p) |
| 88 |
(close-output-port p))]))) |
| 89 |
|
| 90 |
(define load-handler |
| 91 |
(lambda (x) |
| 92 |
(eval x (interaction-environment)))) |
| 93 |
|
| 94 |
(define read-and-eval |
| 95 |
(lambda (p eval-proc) |
| 96 |
(let ([x (read p)]) |
| 97 |
(unless (eof-object? x) |
| 98 |
(eval-proc x) |
| 99 |
(read-and-eval p eval-proc))))) |
| 100 |
(define load |
| 101 |
(case-lambda |
| 102 |
[(x) (load x load-handler)] |
| 103 |
[(x eval-proc) |
| 104 |
(unless (string? x) |
| 105 |
(die 'load "not a string" x)) |
| 106 |
(unless (procedure? eval-proc) |
| 107 |
(die 'load "not a procedure" eval-proc)) |
| 108 |
(let ([ls (read-script-source-file x)]) |
| 109 |
(let f () |
| 110 |
(unless (null? ls) |
| 111 |
(let ([a (car ls)]) |
| 112 |
(set! ls (cdr ls)) |
| 113 |
(eval-proc a)) |
| 114 |
(f))))])) |
| 115 |
|
| 116 |
(define load-r6rs-script |
| 117 |
(lambda (filename serialize? run?) |
| 118 |
(unless (string? filename) |
| 119 |
(die 'load-r6rs-script "file name is not a string" filename)) |
| 120 |
(let ([prog (read-script-source-file filename)]) |
| 121 |
(let([thunk (compile-r6rs-top-level prog)]) |
| 122 |
(when serialize? |
| 123 |
(serialize-all |
| 124 |
(lambda (file-name contents) |
| 125 |
(do-serialize-library file-name contents)) |
| 126 |
(lambda (core-expr) |
| 127 |
(compile-core-expr core-expr)))) |
| 128 |
(when run? (thunk)))))) |
| 129 |
|
| 130 |
(current-precompiled-library-loader load-serialized-library) |
| 131 |
|
| 132 |
) |