1.0.29.27: add shebang line to fasls
[sbcl.git] / src / code / target-load.lisp
1 ;;;; that part of the loader is only needed on the target system
2 ;;;; (which is basically synonymous with "that part of the loader
3 ;;;; which is not needed by GENESIS")
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!FASL")
15
16 (defvar *load-source-default-type* "lisp"
17   #!+sb-doc
18   "The source file types which LOAD looks for by default.")
19
20 (declaim (type (or pathname null) *load-truename* *load-pathname*))
21 (defvar *load-truename* nil
22   #!+sb-doc
23   "the TRUENAME of the file that LOAD is currently loading")
24 (defvar *load-pathname* nil
25   #!+sb-doc
26   "the defaulted pathname that LOAD is currently loading")
27 \f
28 ;;;; LOAD-AS-SOURCE
29
30 ;;; Load a text stream.  (Note that load-as-fasl is in another file.)
31 (defun load-as-source (stream verbose print)
32   (maybe-announce-load stream verbose)
33   (macrolet ((do-sexprs ((sexpr stream) &body body)
34                (aver (symbolp sexpr))
35                (with-unique-names (source-info)
36                  (once-only ((stream stream))
37                    `(if (handler-case (pathname stream)
38                           (error () nil))
39                         (let ((,source-info (sb!c::make-file-source-info
40                                             (pathname ,stream)
41                                             (stream-external-format ,stream))))
42                           (setf (sb!c::source-info-stream ,source-info) ,stream)
43                           (sb!c::do-forms-from-info ((,sexpr) ,source-info)
44                             ,@body))
45                         (do ((,sexpr (read ,stream nil *eof-object*)
46                                      (read ,stream nil *eof-object*)))
47                             ((eq ,sexpr *eof-object*))
48                           ,@body))))))
49     (do-sexprs (sexpr stream)
50       (if print
51           (let ((results (multiple-value-list (eval sexpr))))
52             (load-fresh-line)
53             (format t "~{~S~^, ~}~%" results))
54           (eval sexpr)))
55     t))
56 \f
57 ;;;; LOAD itself
58
59 (define-condition fasl-header-missing (invalid-fasl)
60   ((fhsss :reader invalid-fasl-fhsss :initarg :fhsss))
61   (:report
62    (lambda (condition stream)
63      (format stream "~@<File ~S has a fasl file type, but no fasl header:~%~
64                      Expected ~S, but got ~S.~:@>"
65              (invalid-fasl-stream condition)
66              (invalid-fasl-expected condition)
67              (invalid-fasl-fhsss condition)))))
68
69
70 ;;; The following comment preceded the pre 1.0.12.36 definition of
71 ;;; LOAD; it may no longer be accurate:
72
73 ;; FIXME: Daniel Barlow's ilsb.tar ILISP-for-SBCL patches contain an
74 ;; implementation of "DEFUN SOURCE-FILE" which claims, in a comment,
75 ;; that CMU CL does not correctly record source file information when
76 ;; LOADing a non-compiled file. Check whether this bug exists in SBCL
77 ;; and fix it if so.
78
79 (defun load (pathspec &key (verbose *load-verbose*) (print *load-print*)
80              (if-does-not-exist t) (external-format :default))
81   #!+sb-doc
82   "Load the file given by FILESPEC into the Lisp environment, returning
83    T on success."
84   (flet ((load-stream (stream faslp)
85            (let* (;; Bindings required by ANSI.
86                   (*readtable* *readtable*)
87                   (*package* (sane-package))
88                   ;; FIXME: we should probably document the circumstances
89                   ;; where *LOAD-PATHNAME* and *LOAD-TRUENAME* aren't
90                   ;; pathnames during LOAD.  ANSI makes no exceptions here.
91                   (*load-pathname* (handler-case (pathname stream)
92                                      ;; FIXME: it should probably be a type
93                                      ;; error to try to get a pathname for a
94                                      ;; stream that doesn't have one, but I
95                                      ;; don't know if we guarantee that.
96                                      (error () nil)))
97                   (*load-truename* (when *load-pathname*
98                                      (handler-case (truename stream)
99                                        (file-error () nil))))
100                   ;; Bindings used internally.
101                   (*load-depth* (1+ *load-depth*))
102                   ;; KLUDGE: I can't find in the ANSI spec where it says
103                   ;; that DECLAIM/PROCLAIM of optimization policy should
104                   ;; have file scope. CMU CL did this, and it seems
105                   ;; reasonable, but it might not be right; after all,
106                   ;; things like (PROCLAIM '(TYPE ..)) don't have file
107                   ;; scope, and I can't find anything under PROCLAIM or
108                   ;; COMPILE-FILE or LOAD or OPTIMIZE which justifies this
109                   ;; behavior. Hmm. -- WHN 2001-04-06
110                   (sb!c::*policy* sb!c::*policy*))
111              (return-from load
112                (if faslp
113                    (load-as-fasl stream verbose print)
114                    (load-as-source stream verbose print))))))
115     ;; Case 1: stream.
116     (when (streamp pathspec)
117       (return-from load (load-stream pathspec (fasl-header-p pathspec))))
118     (let ((pathname (pathname pathspec)))
119       ;; Case 2: Open as binary, try to process as a fasl.
120       (with-open-stream
121           (stream (or (open pathspec :element-type '(unsigned-byte 8)
122                             :if-does-not-exist nil)
123                       (when (null (pathname-type pathspec))
124                         (let ((defaulted-pathname
125                                (probe-load-defaults pathspec)))
126                           (if defaulted-pathname
127                               (progn (setq pathname defaulted-pathname)
128                                      (open pathname
129                                            :if-does-not-exist
130                                            (if if-does-not-exist :error nil)
131                                            :element-type '(unsigned-byte 8))))))
132                       (if if-does-not-exist
133                           (error 'simple-file-error
134                                  :pathname pathspec
135                                  :format-control
136                                  "~@<Couldn't load ~S: file does not exist.~@:>"
137                                  :format-arguments (list pathspec)))))
138         (unless stream
139           (return-from load nil))
140         (let* ((real (probe-file stream))
141                (should-be-fasl-p
142                 (and real (string= (pathname-type real) *fasl-file-type*))))
143           (when (fasl-header-p stream :errorp should-be-fasl-p)
144             (return-from load (load-stream stream t)))))
145       ;; Case 3: Open using the gived external format, process as source.
146       (with-open-file (stream pathname :external-format external-format)
147         (load-stream stream nil)))))
148
149 ;; This implements the defaulting SBCL seems to have inherited from
150 ;; CMU.  This routine does not try to perform any loading; all it does
151 ;; is return the pathname (not the truename) of a file to be loaded,
152 ;; or NIL if no such file can be found.  This routine is supposed to
153 ;; signal an error if a fasl's timestamp is older than its source
154 ;; file, but we protect against errors in PROBE-FILE, because any of
155 ;; the ways that we might fail to find a defaulted file are reasons
156 ;; not to load it, but not worth exposing to the user who didn't
157 ;; expicitly ask us to load a file with a made-up name (e.g., the
158 ;; defaulted filename might exceed filename length limits).
159 (defun probe-load-defaults (pathname)
160   (destructuring-bind (defaulted-source-pathname
161                        defaulted-source-truename
162                        defaulted-fasl-pathname
163                        defaulted-fasl-truename)
164       (loop for type in (list *load-source-default-type*
165                               *fasl-file-type*)
166             as probe-pathname = (make-pathname :type type
167                                                :defaults pathname)
168             collect probe-pathname
169             collect (handler-case (probe-file probe-pathname)
170                       (file-error () nil)))
171     (cond ((and defaulted-fasl-truename
172                 defaulted-source-truename
173                 (> (file-write-date defaulted-source-truename)
174                    (file-write-date defaulted-fasl-truename)))
175            (restart-case
176                (error "The object file ~A is~@
177                        older than the presumed source:~%  ~A."
178                       defaulted-fasl-truename
179                       defaulted-source-truename)
180              (source () :report "load source file"
181                      defaulted-source-pathname)
182              (object () :report "load object file"
183                      defaulted-fasl-pathname)))
184           (defaulted-fasl-truename defaulted-fasl-pathname)
185           (defaulted-source-truename defaulted-source-pathname))))
186 \f
187 ;;; Load a code object. BOX-NUM objects are popped off the stack for
188 ;;; the boxed storage section, then SIZE bytes of code are read in.
189 #!-x86
190 (defun load-code (box-num code-length)
191   (declare (fixnum box-num code-length))
192   (with-fop-stack t
193     (let ((code (sb!c:allocate-code-object box-num code-length))
194           (index (+ sb!vm:code-trace-table-offset-slot box-num)))
195       (declare (type index index))
196       (setf (%code-debug-info code) (pop-stack))
197       (dotimes (i box-num)
198         (declare (fixnum i))
199         (setf (code-header-ref code (decf index)) (pop-stack)))
200       (sb!sys:without-gcing
201         (read-n-bytes *fasl-input-stream*
202                       (code-instructions code)
203                       0
204                       code-length))
205       code)))
206
207 ;;; Moving native code during a GC or purify is not so trivial on the
208 ;;; x86 port.
209 ;;;
210 ;;; Our strategy for allowing the loading of x86 native code into the
211 ;;; dynamic heap requires that the addresses of fixups be saved for
212 ;;; all these code objects. After a purify these fixups can be
213 ;;; dropped. In CMU CL, this policy was enabled with
214 ;;; *ENABLE-DYNAMIC-SPACE-CODE*; in SBCL it's always used.
215 #!+x86
216 (defun load-code (box-num code-length)
217   (declare (fixnum box-num code-length))
218   (with-fop-stack t
219     (let ((stuff (list (pop-stack))))
220       (dotimes (i box-num)
221         (declare (fixnum i))
222         (push (pop-stack) stuff))
223       (let* ((dbi (car (last stuff)))   ; debug-info
224              (tto (first stuff)))       ; trace-table-offset
225
226         (setq stuff (nreverse stuff))
227
228         ;; FIXME: *LOAD-CODE-VERBOSE* should probably be #!+SB-SHOW.
229         (when *load-code-verbose*
230               (format t "stuff: ~S~%" stuff)
231               (format t
232                       "   : ~S ~S ~S ~S~%"
233                       (sb!c::compiled-debug-info-p dbi)
234                       (sb!c::debug-info-p dbi)
235                       (sb!c::compiled-debug-info-name dbi)
236                       tto)
237               (format t "   loading to the dynamic space~%"))
238
239         (let ((code (sb!c:allocate-code-object box-num code-length))
240               (index (+ sb!vm:code-trace-table-offset-slot box-num)))
241           (declare (type index index))
242           (when *load-code-verbose*
243             (format t
244                     "  obj addr=~X~%"
245                     (sb!kernel::get-lisp-obj-address code)))
246           (setf (%code-debug-info code) (pop stuff))
247           (dotimes (i box-num)
248             (declare (fixnum i))
249             (setf (code-header-ref code (decf index)) (pop stuff)))
250           (sb!sys:without-gcing
251            (read-n-bytes *fasl-input-stream*
252                          (code-instructions code)
253                          0
254                          code-length))
255           code)))))
256 \f
257 ;;;; linkage fixups
258
259 ;;; how we learn about assembler routines at startup
260 (defvar *!initial-assembler-routines*)
261
262 (defun !loader-cold-init ()
263   (/show0 "/!loader-cold-init")
264   (dolist (routine *!initial-assembler-routines*)
265     (setf (gethash (car routine) *assembler-routines*) (cdr routine))))