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