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