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*))
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 '(()))
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)
54 (let ((results (multiple-value-list (eval sexpr))))
56 (format t "~{~S~^, ~}~%" results))
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
64 (declare (type (member nil :error) if-does-not-exist))
67 (error 'simple-file-error
69 :format-control "~S does not exist."
70 :format-arguments (list (namestring pathname)))
71 (return-from internal-load nil)))
73 (let ((*load-truename* truename)
74 (*load-pathname* pathname))
77 (with-open-file (stream truename
79 :if-does-not-exist if-does-not-exist)
80 (sloload stream verbose print)))
82 (with-open-file (stream truename
84 :if-does-not-exist if-does-not-exist
85 :element-type '(unsigned-byte 8))
86 (fasload stream verbose print)))
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*))
93 (>= (length (the simple-string first-line))
95 (string= first-line fhs :end1 (length fhs)))
96 (internal-load pathname truename if-does-not-exist verbose print
99 (when (member (pathname-type truename)
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
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.
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)))
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)
122 (return (values pn tn))))))))
124 ;;; a helper function for LOAD: Handle the case of INTERNAL-LOAD where the file
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")
137 (> (file-write-date src-tn) (file-write-date obj-tn)))
139 (error "The object file ~A is~@
140 older than the presumed source:~% ~A."
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
150 (object () :report "load object file"
151 (internal-load src-pn obj-tn if-does-not-exist verbose print
154 (internal-load obj-pn obj-tn if-does-not-exist verbose print :binary))
156 (internal-load src-pn src-tn if-does-not-exist verbose print :source))
158 (internal-load pathname nil if-does-not-exist verbose print nil))))))
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.
165 ;;; FIXME: ANSI specifies an EXTERNAL-FORMAT keyword argument.
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
173 (verbose *load-verbose*)
175 (if-does-not-exist t))
177 "Loads the file given by FILESPEC into the Lisp environment, returning
178 T on success. These options are defined:
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.
185 If true, print a line describing each file loaded. The default
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*."
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.
207 (if (streamp filespec)
208 (if (or (equal (stream-element-type filespec)
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)))
218 (error 'file-error :pathname filespec))
219 (dolist (file files t)
222 internal-if-does-not-exist
225 (let ((tn (probe-file pn)))
226 (if (or tn (pathname-type pn))
229 internal-if-does-not-exist
232 (internal-load-default-type
234 internal-if-does-not-exist
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.
241 (defun load-code (box-num code-length)
242 (declare (fixnum box-num code-length))
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
248 (declare (type index index))
249 #!-gengc (setf (%code-debug-info code) (pop-stack))
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)
258 #!+gengc (* code-length sb!vm:word-bytes)))
262 (defun load-code (box-num code-length)
263 (declare (fixnum box-num code-length))
265 (let ((stuff (list (pop-stack))))
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")))) )
283 (setq stuff (nreverse stuff))
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~%"
294 ;; FIXME: *LOAD-CODE-VERBOSE* should probably be #!+SB-SHOW.
295 (when *load-code-verbose*
296 (format t "stuff: ~S~%" stuff)
299 (sb!c::compiled-debug-info-p dbi)
300 (sb!c::debug-info-p dbi)
301 (sb!c::compiled-debug-info-name dbi)
303 (if load-to-dynamic-space
304 (format t " loading to the dynamic space~%")
305 (format t " loading to the static space~%")))
308 (if load-to-dynamic-space
309 (%primitive sb!c:allocate-dynamic-code-object
312 (%primitive sb!c:allocate-code-object
315 (index (+ sb!vm:code-trace-table-offset-slot box-num)))
316 (declare (type index index))
317 (when *load-code-verbose*
320 (sb!kernel::get-lisp-obj-address code)))
321 (setf (%code-debug-info code) (pop stuff))
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))
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))))
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__"
346 #!+freebsd "ldso_stub__"
348 *static-foreign-symbols*)
349 (sb!sys:get-dynamic-foreign-symbol-address foreign-symbol)
350 (error "unknown foreign symbol: ~S" foreign-symbol)))
352 (defun foreign-symbol-address (symbol)
353 (int-sap (foreign-symbol-address-as-integer (sb!vm:extern-alien-name symbol))))