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")
5 ;;;; This software is part of the SBCL system. See the README file for
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.
14 (in-package "SB!IMPL")
16 (defvar *load-source-types* '("lisp" "l" "cl" "lsp")
18 "The source file types which LOAD recognizes.")
20 (defvar *load-object-types*
21 '(#.sb!c:*backend-fasl-file-type*
22 #.(sb!c:backend-byte-fasl-file-type)
25 "A list of the object file types recognized by LOAD.")
27 (declaim (list *load-source-types* *load-object-types*))
29 (defvar *load-truename* nil
31 "the TRUENAME of the file that LOAD is currently loading")
33 (defvar *load-pathname* nil
35 "the defaulted pathname that LOAD is currently loading")
37 (declaim (type (or pathname null) *load-truename* *load-pathname*))
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*)
49 (let ((results (multiple-value-list (eval sexpr))))
51 (format t "~{~S~^, ~}~%" results))
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
59 (declare (type (member nil :error) if-does-not-exist))
62 (error 'simple-file-error
64 :format-control "~S does not exist."
65 :format-arguments (list (namestring pathname)))
66 (return-from internal-load nil)))
68 (let ((*load-truename* truename)
69 (*load-pathname* pathname))
72 (with-open-file (stream truename
74 :if-does-not-exist if-does-not-exist)
75 (sloload stream verbose print)))
77 (with-open-file (stream truename
79 :if-does-not-exist if-does-not-exist
80 :element-type '(unsigned-byte 8))
81 (fasload stream verbose print)))
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*))
88 (>= (length (the simple-string first-line))
90 (string= first-line fhs :end1 (length fhs)))
91 (internal-load pathname truename if-does-not-exist verbose print
94 (when (member (pathname-type truename)
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
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.
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)))
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)
117 (return (values pn tn))))))))
119 ;;; a helper function for LOAD: Handle the case of INTERNAL-LOAD where the file
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")
132 (> (file-write-date src-tn) (file-write-date obj-tn)))
134 (error "The object file ~A is~@
135 older than the presumed source:~% ~A."
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
145 (object () :report "load object file"
146 (internal-load src-pn obj-tn if-does-not-exist verbose print
149 (internal-load obj-pn obj-tn if-does-not-exist verbose print :binary))
151 (internal-load src-pn src-tn if-does-not-exist verbose print :source))
153 (internal-load pathname nil if-does-not-exist verbose print nil))))))
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.
160 ;;; FIXME: ANSI specifies an EXTERNAL-FORMAT keyword argument.
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
168 (verbose *load-verbose*)
170 (if-does-not-exist t))
172 "Loads the file given by FILESPEC into the Lisp environment, returning
173 T on success. These options are defined:
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.
180 If true, print a line describing each file loaded. The default
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*."
188 (let ((sb!c::*default-cookie* sb!c::*default-cookie*)
189 (sb!c::*default-interface-cookie* sb!c::*default-interface-cookie*)
190 (*package* (sane-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.
202 (if (streamp filespec)
203 (if (or (equal (stream-element-type filespec)
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)))
213 (error 'file-error :pathname filespec))
214 (dolist (file files t)
217 internal-if-does-not-exist
220 (let ((tn (probe-file pn)))
221 (if (or tn (pathname-type pn))
224 internal-if-does-not-exist
227 (internal-load-default-type
229 internal-if-does-not-exist
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.
236 (defun load-code (box-num code-length)
237 (declare (fixnum box-num code-length))
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
243 (declare (type index index))
244 #!-gengc (setf (%code-debug-info code) (pop-stack))
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)
253 #!+gengc (* code-length sb!vm:word-bytes)))
257 (defun load-code (box-num code-length)
258 (declare (fixnum box-num code-length))
260 (let ((stuff (list (pop-stack))))
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")))) )
278 (setq stuff (nreverse stuff))
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~%"
289 ;; FIXME: *LOAD-CODE-VERBOSE* should probably be #!+SB-SHOW.
290 (when *load-code-verbose*
291 (format t "stuff: ~S~%" stuff)
294 (sb!c::compiled-debug-info-p dbi)
295 (sb!c::debug-info-p dbi)
296 (sb!c::compiled-debug-info-name dbi)
298 (if load-to-dynamic-space
299 (format t " loading to the dynamic space~%")
300 (format t " loading to the static space~%")))
303 (if load-to-dynamic-space
304 (%primitive sb!c:allocate-dynamic-code-object
307 (%primitive sb!c:allocate-code-object
310 (index (+ sb!vm:code-trace-table-offset-slot box-num)))
311 (declare (type index index))
312 (when *load-code-verbose*
315 (sb!kernel::get-lisp-obj-address code)))
316 (setf (%code-debug-info code) (pop stuff))
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))
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))))
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__"
341 #!+freebsd "ldso_stub__"
343 *static-foreign-symbols*)
344 (sb!sys:get-dynamic-foreign-symbol-address foreign-symbol)
345 (error "unknown foreign symbol: ~S" foreign-symbol)))
347 (defun foreign-symbol-address (symbol)
348 (int-sap (foreign-symbol-address-as-integer (sb!vm:extern-alien-name symbol))))