17a69b714c8b183edb654d9d1d9f6a022637dfb9
[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!IMPL")
15
16 (defvar *load-source-default-type* "lisp"
17   #!+sb-doc
18   "The source file types which LOAD looks for by default.")
19
20 (defvar *load-truename* nil
21   #!+sb-doc
22   "the TRUENAME of the file that LOAD is currently loading")
23
24 (defvar *load-pathname* nil
25   #!+sb-doc
26   "the defaulted pathname that LOAD is currently loading")
27
28 (declaim (type (or pathname null) *load-truename* *load-pathname*))
29 \f
30 ;;;; SLOLOAD
31
32 ;;; Load a text file.
33 (defun sloload (stream verbose print)
34   (do-load-verbose stream verbose)
35   (do ((sexpr (read stream nil *eof-object*)
36               (read stream nil *eof-object*)))
37       ((eq sexpr *eof-object*)
38        t)
39     (if print
40         (let ((results (multiple-value-list (eval sexpr))))
41           (load-fresh-line)
42           (format t "~{~S~^, ~}~%" results))
43         (eval sexpr))))
44 \f
45 ;;;; LOAD itself
46
47 ;;; a helper function for LOAD: Load the stuff in a file when we have the name.
48 (defun internal-load (pathname truename if-does-not-exist verbose print
49                       &optional contents)
50   (declare (type (member nil :error) if-does-not-exist))
51   (unless truename
52     (if if-does-not-exist
53         (error 'simple-file-error
54                :pathname pathname
55                :format-control "~S does not exist."
56                :format-arguments (list (namestring pathname)))
57         (return-from internal-load nil)))
58
59   (let ((*load-truename* truename)
60         (*load-pathname* pathname))
61     (case contents
62       (:source
63        (with-open-file (stream truename
64                                :direction :input
65                                :if-does-not-exist if-does-not-exist)
66          (sloload stream verbose print)))
67       (:binary
68        (with-open-file (stream truename
69                                :direction :input
70                                :if-does-not-exist if-does-not-exist
71                                :element-type '(unsigned-byte 8))
72          (fasload stream verbose print)))
73       (t
74        (let ((first-line (with-open-file (stream truename :direction :input)
75                            (read-line stream nil)))
76              (fhs sb!c:*fasl-header-string-start-string*))
77          (cond
78           ((and first-line
79                 (>= (length (the simple-string first-line))
80                     (length fhs))
81                 (string= first-line fhs :end1 (length fhs)))
82            (internal-load pathname truename if-does-not-exist verbose print
83                           :binary))
84           (t
85            (when (string= (pathname-type truename)
86                           sb!c:*backend-fasl-file-type*)
87              (error "File has a fasl file type, but no fasl file header:~%  ~S"
88                     (namestring truename)))
89            (internal-load pathname truename if-does-not-exist verbose print
90                           :source))))))))
91
92 ;;; a helper function for INTERNAL-LOAD-DEFAULT-TYPE: Try the default
93 ;;; file type TYPE and return (VALUES PATHNAME TRUENAME) for a match,
94 ;;; or (VALUES PATHNAME NIL) if the file doesn't exist.
95 ;;;
96 ;;; This is analogous to CMU CL's TRY-DEFAULT-TYPES, but we only try a
97 ;;; single type. By avoiding CMU CL's generality here, we avoid having
98 ;;; to worry about some annoying ambiguities. (E.g. what if the
99 ;;; possible types are ".lisp" and ".cl", and both "foo.lisp" and
100 ;;; "foo.cl" exist?)
101 (defun try-default-type (pathname type)
102   (let ((pn (make-pathname :type type :defaults pathname)))
103     (values pn (probe-file pn))))
104
105 ;;; a helper function for LOAD: Handle the case of INTERNAL-LOAD where
106 ;;; the file does not exist.
107 (defun internal-load-default-type (pathname if-does-not-exist verbose print)
108   (declare (type (member nil :error) if-does-not-exist))
109   (multiple-value-bind (src-pn src-tn)
110       (try-default-type pathname *load-source-default-type*)
111     (multiple-value-bind (obj-pn obj-tn)
112         (try-default-type pathname sb!c:*backend-fasl-file-type*)
113       (cond
114        ((and obj-tn
115              src-tn
116              (> (file-write-date src-tn) (file-write-date obj-tn)))
117         (restart-case
118          (error "The object file ~A is~@
119                 older than the presumed source:~%  ~A."
120                 (namestring obj-tn)
121                 (namestring src-tn))
122          ;; FIXME: In CMU CL one of these was a CONTINUE case.
123          ;; There's not one now. I don't remember how restart-case
124          ;; works very well, make sure that it doesn't do anything
125          ;; weird when we don't specify the CONTINUE case.
126          (source () :report "load source file"
127            (internal-load src-pn src-tn if-does-not-exist verbose print
128                           :source))
129          (object () :report "load object file"
130             (internal-load src-pn obj-tn if-does-not-exist verbose print
131                            :binary))))
132        (obj-tn
133         (internal-load obj-pn obj-tn if-does-not-exist verbose print :binary))
134        (src-pn
135         (internal-load src-pn src-tn if-does-not-exist verbose print :source))
136        (t
137         (internal-load pathname nil if-does-not-exist verbose print nil))))))
138
139 ;;; This function mainly sets up special bindings and then calls
140 ;;; sub-functions. We conditionally bind the switches with PROGV so
141 ;;; that people can set them in their init files and have the values
142 ;;; take effect. If the compiler is loaded, we make the
143 ;;; compiler-policy local to LOAD by binding it to itself.
144 ;;;
145 ;;; FIXME: ANSI specifies an EXTERNAL-FORMAT keyword argument.
146 ;;;
147 ;;; FIXME: Daniel Barlow's ilsb.tar ILISP-for-SBCL patches contain an
148 ;;; implementation of "DEFUN SOURCE-FILE" which claims, in a comment, that CMU
149 ;;; CL does not correctly record source file information when LOADing a
150 ;;; non-compiled file. Check whether this bug exists in SBCL and fix it if so.
151 (defun load (filespec
152              &key
153              (verbose *load-verbose*)
154              (print *load-print*)
155              (if-does-not-exist t))
156   #!+sb-doc
157   "Loads the file given by FILESPEC into the Lisp environment, returning
158    T on success. These options are defined:
159
160    :IF-DOES-NOT-EXIST
161        What should we do if the file can't be located? If true (the
162        default), signal an error. If NIL, simply return NIL.
163
164    :VERBOSE
165        If true, print a line describing each file loaded. The default
166        is *LOAD-VERBOSE*.
167
168    :PRINT
169        If true, print information about loaded values. When loading the
170        source, the result of evaluating each top-level form is printed.
171        The default is *LOAD-PRINT*."
172
173   (let ((sb!c::*policy* sb!c::*policy*)
174         (sb!c::*interface-policy* sb!c::*interface-policy*)
175         (*package* (sane-package))
176         (*readtable* *readtable*)
177         (*load-depth* (1+ *load-depth*))
178         ;; The old CMU CL LOAD function used an IF-DOES-NOT-EXIST argument of
179         ;; (MEMBER :ERROR NIL) type. ANSI constrains us to accept a generalized
180         ;; boolean argument value for this externally-visible function, but the
181         ;; internal functions still use the old convention.
182         (internal-if-does-not-exist (if if-does-not-exist :error nil)))
183     ;; FIXME: This VALUES wrapper is inherited from CMU CL.
184     ;; Once SBCL gets function return type checking right, we can
185     ;; achieve a similar effect better by adding FTYPE declarations.
186     (values
187      (if (streamp filespec)
188          (if (or (equal (stream-element-type filespec)
189                         '(unsigned-byte 8)))
190              (fasload filespec verbose print)
191              (sloload filespec verbose print))
192          (let ((pn (merge-pathnames (pathname filespec)
193                                     *default-pathname-defaults*)))
194            (if (wild-pathname-p pn)
195                (let ((files (directory pn)))
196                  #!+high-security
197                  (when (null files)
198                    (error 'file-error :pathname filespec))
199                  (dolist (file files t)
200                    (internal-load pn
201                                   file
202                                   internal-if-does-not-exist
203                                   verbose
204                                   print)))
205                (let ((tn (probe-file pn)))
206                  (if (or tn (pathname-type pn))
207                      (internal-load pn
208                                     tn
209                                     internal-if-does-not-exist
210                                     verbose
211                                     print)
212                      (internal-load-default-type
213                       pn
214                       internal-if-does-not-exist
215                       verbose
216                       print)))))))))
217 \f
218 ;;; Load a code object. BOX-NUM objects are popped off the stack for
219 ;;; the boxed storage section, then SIZE bytes of code are read in.
220 #!-x86
221 (defun load-code (box-num code-length)
222   (declare (fixnum box-num code-length))
223   (with-fop-stack t
224     (let ((code (%primitive sb!c:allocate-code-object box-num code-length))
225           (index (+ #!-gengc sb!vm:code-trace-table-offset-slot
226                     #!+gengc sb!vm:code-debug-info-slot
227                     box-num)))
228       (declare (type index index))
229       #!-gengc (setf (%code-debug-info code) (pop-stack))
230       (dotimes (i box-num)
231         (declare (fixnum i))
232         (setf (code-header-ref code (decf index)) (pop-stack)))
233       (sb!sys:without-gcing
234         (read-n-bytes *fasl-file*
235                       (code-instructions code)
236                       0
237                       #!-gengc code-length
238                       #!+gengc (* code-length sb!vm:word-bytes)))
239       code)))
240
241 #!+x86
242 (defun load-code (box-num code-length)
243   (declare (fixnum box-num code-length))
244   (with-fop-stack t
245     (let ((stuff (list (pop-stack))))
246       (dotimes (i box-num)
247         (declare (fixnum i))
248         (push (pop-stack) stuff))
249       (let* ((dbi (car (last stuff)))   ; debug-info
250              (tto (first stuff))        ; trace-table-offset
251              (load-to-dynamic-space
252               (or *enable-dynamic-space-code*
253                   ;; definitely byte-compiled code?
254                   (and *load-byte-compiled-code-to-dynamic-space*
255                        (sb!c::debug-info-p dbi)
256                        (not (sb!c::compiled-debug-info-p dbi)))
257                   ;; or a x86 top level form?
258                   (and *load-x86-tlf-to-dynamic-space*
259                        (sb!c::compiled-debug-info-p dbi)
260                        (string= (sb!c::compiled-debug-info-name dbi)
261                                 "top-level form")))) )
262
263         (setq stuff (nreverse stuff))
264
265         ;; Check that tto is always a list for byte-compiled
266         ;; code. Could be used an alternate check.
267         (when (and (typep tto 'list)
268                    (not (and (sb!c::debug-info-p dbi)
269                              (not (sb!c::compiled-debug-info-p dbi)))))
270           ;; FIXME: What is this for?
271           (format t "* tto list on non-bc code: ~S~% ~S ~S~%"
272                   stuff dbi tto))
273         
274         ;; FIXME: *LOAD-CODE-VERBOSE* should probably be #!+SB-SHOW.
275         (when *load-code-verbose*
276               (format t "stuff: ~S~%" stuff)
277               (format t
278                       "   : ~S ~S ~S ~S~%"
279                       (sb!c::compiled-debug-info-p dbi)
280                       (sb!c::debug-info-p dbi)
281                       (sb!c::compiled-debug-info-name dbi)
282                       tto)
283               (if load-to-dynamic-space
284                   (format t "   loading to the dynamic space~%")
285                   (format t "   loading to the static space~%")))
286
287         (let ((code
288                (if load-to-dynamic-space
289                    (%primitive sb!c:allocate-dynamic-code-object
290                                box-num
291                                code-length)
292                    (%primitive sb!c:allocate-code-object
293                                box-num
294                                code-length)))
295               (index (+ sb!vm:code-trace-table-offset-slot box-num)))
296           (declare (type index index))
297           (when *load-code-verbose*
298             (format t
299                     "  obj addr=~X~%"
300                     (sb!kernel::get-lisp-obj-address code)))
301           (setf (%code-debug-info code) (pop stuff))
302           (dotimes (i box-num)
303             (declare (fixnum i))
304             (setf (code-header-ref code (decf index)) (pop stuff)))
305           (sb!sys:without-gcing
306            (read-n-bytes *fasl-file* (code-instructions code) 0 code-length))
307           code)))))
308 \f
309 ;;;; linkage fixups
310
311 ;;; how we learn about assembler routines and foreign symbols at startup
312 (defvar *!initial-assembler-routines*)
313 (defvar *!initial-foreign-symbols*)
314 (defun !loader-cold-init ()
315   (dolist (routine *!initial-assembler-routines*)
316     (setf (gethash (car routine) *assembler-routines*) (cdr routine)))
317   (dolist (symbol *!initial-foreign-symbols*)
318     (setf (gethash (car symbol) *static-foreign-symbols*) (cdr symbol))))
319
320 (declaim (ftype (function (string) sb!vm:word) foreign-symbol-address-as-integer))
321 (defun foreign-symbol-address-as-integer (foreign-symbol)
322   (or (gethash foreign-symbol *static-foreign-symbols*)
323       (gethash (concatenate 'simple-string
324                             #!+linux "ldso_stub__"
325                             #!+openbsd "_"
326                             #!+freebsd "ldso_stub__"
327                             foreign-symbol)
328                *static-foreign-symbols*)
329       (sb!sys:get-dynamic-foreign-symbol-address foreign-symbol)
330       (error "unknown foreign symbol: ~S" foreign-symbol)))
331
332 (defun foreign-symbol-address (symbol)
333   (int-sap (foreign-symbol-address-as-integer (sb!vm:extern-alien-name symbol))))