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-default-type* "lisp"
18 "The source file types which LOAD looks for by default.")
20 (defvar *load-truename* nil
22 "the TRUENAME of the file that LOAD is currently loading")
24 (defvar *load-pathname* nil
26 "the defaulted pathname that LOAD is currently loading")
28 (declaim (type (or pathname null) *load-truename* *load-pathname*))
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*)
40 (let ((results (multiple-value-list (eval sexpr))))
42 (format t "~{~S~^, ~}~%" results))
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
50 (declare (type (member nil :error) if-does-not-exist))
53 (error 'simple-file-error
55 :format-control "~S does not exist."
56 :format-arguments (list (namestring pathname)))
57 (return-from internal-load nil)))
59 (let ((*load-truename* truename)
60 (*load-pathname* pathname))
63 (with-open-file (stream truename
65 :if-does-not-exist if-does-not-exist)
66 (sloload stream verbose print)))
68 (with-open-file (stream truename
70 :if-does-not-exist if-does-not-exist
71 :element-type '(unsigned-byte 8))
72 (fasload stream verbose print)))
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*))
79 (>= (length (the simple-string first-line))
81 (string= first-line fhs :end1 (length fhs)))
82 (internal-load pathname truename if-does-not-exist verbose print
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
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.
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
101 (defun try-default-type (pathname type)
102 (let ((pn (make-pathname :type type :defaults pathname)))
103 (values pn (probe-file pn))))
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*)
116 (> (file-write-date src-tn) (file-write-date obj-tn)))
118 (error "The object file ~A is~@
119 older than the presumed source:~% ~A."
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
129 (object () :report "load object file"
130 (internal-load src-pn obj-tn if-does-not-exist verbose print
133 (internal-load obj-pn obj-tn if-does-not-exist verbose print :binary))
135 (internal-load src-pn src-tn if-does-not-exist verbose print :source))
137 (internal-load pathname nil if-does-not-exist verbose print nil))))))
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.
145 ;;; FIXME: ANSI specifies a &KEY :EXTERNAL-FORMAT argument.
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
153 (verbose *load-verbose*)
155 (if-does-not-exist t))
157 "Loads the file given by FILESPEC into the Lisp environment, returning
158 T on success. These options are defined:
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.
165 If true, print a line describing each file loaded. The default
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*."
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.
187 (if (streamp filespec)
188 (if (or (equal (stream-element-type filespec)
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)))
198 (error 'file-error :pathname filespec))
199 (dolist (file files t)
202 internal-if-does-not-exist
205 (let ((tn (probe-file pn)))
206 (if (or tn (pathname-type pn))
209 internal-if-does-not-exist
212 (internal-load-default-type
214 internal-if-does-not-exist
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.
221 (defun load-code (box-num code-length)
222 (declare (fixnum box-num code-length))
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
228 (declare (type index index))
229 #!-gengc (setf (%code-debug-info code) (pop-stack))
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)
238 #!+gengc (* code-length sb!vm:word-bytes)))
242 (defun load-code (box-num code-length)
243 (declare (fixnum box-num code-length))
245 (let ((stuff (list (pop-stack))))
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")))) )
263 (setq stuff (nreverse stuff))
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~%"
274 ;; FIXME: *LOAD-CODE-VERBOSE* should probably be #!+SB-SHOW.
275 (when *load-code-verbose*
276 (format t "stuff: ~S~%" stuff)
279 (sb!c::compiled-debug-info-p dbi)
280 (sb!c::debug-info-p dbi)
281 (sb!c::compiled-debug-info-name dbi)
283 (if load-to-dynamic-space
284 (format t " loading to the dynamic space~%")
285 (format t " loading to the static space~%")))
288 (if load-to-dynamic-space
289 (%primitive sb!c:allocate-dynamic-code-object
292 (%primitive sb!c:allocate-code-object
295 (index (+ sb!vm:code-trace-table-offset-slot box-num)))
296 (declare (type index index))
297 (when *load-code-verbose*
300 (sb!kernel::get-lisp-obj-address code)))
301 (setf (%code-debug-info code) (pop stuff))
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))
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))))
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__"
326 #!+freebsd "ldso_stub__"
328 *static-foreign-symbols*)
329 (sb!sys:get-dynamic-foreign-symbol-address foreign-symbol)
330 (error "unknown foreign symbol: ~S" foreign-symbol)))
332 (defun foreign-symbol-address (symbol)
333 (int-sap (foreign-symbol-address-as-integer (sb!vm:extern-alien-name symbol))))