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