Simpify (X - (X & mask)) to (X & ~mask)
[sbcl.git] / src / code / target-load.lisp
blob168fbedb7d6caea4bdcfbdc9db50c8142c1c26da
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
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.
14 (in-package "SB!FASL")
16 (defvar *load-source-default-type* "lisp"
17 "The source file types which LOAD looks for by default.")
19 (declaim (type (or pathname null) *load-truename* *load-pathname*))
20 (defvar *load-truename* nil
21 "the TRUENAME of the file that LOAD is currently loading")
22 (defvar *load-pathname* nil
23 "the defaulted pathname that LOAD is currently loading")
25 ;;;; LOAD-AS-SOURCE
27 ;;; Load a text stream. (Note that load-as-fasl is in another file.)
28 ;; We'd like, when entering the debugger as a result of an EVAL error,
29 ;; that the condition be annotated with the stream position.
30 ;; One way to do it is catch all conditions and encapsulate them in
31 ;; something new such as a LOADER-EVAL-ERROR and re-signal.
32 ;; The printer for the encapsulated condition has the data it needs to
33 ;; show the original condition and the line/col. That would unfortunately
34 ;; interfere with handlers that were bound around LOAD, since they would
35 ;; only receive the encapsulated condition, and not be able to test for
36 ;; things they're interested in, such as which redefinition warnings to ignore.
37 ;; Instead, printing a herald for any SERIOUS-CONDITION approximates
38 ;; the desired behavior closely enough without printing something for warnings.
39 ;; TODO: It would be supremely cool if, for toplevel PROGN, we could
40 ;; indicate the position of the specific subform that failed
41 (defun load-as-source (stream &key verbose print (context "loading"))
42 (maybe-announce-load stream verbose)
43 (let* ((pathname (ignore-errors (translate-logical-pathname stream)))
44 (native (when pathname (native-namestring pathname))))
45 (with-simple-restart (abort "Abort ~A file ~S." context native)
46 (labels ((condition-herald (c)
47 (declare (ignore c)) ; propagates up
48 (when (form-tracking-stream-p stream)
49 (let* ((startpos
50 (form-tracking-stream-form-start-char-pos stream))
51 (point (line/col-from-charpos stream startpos)))
52 (format *error-output* "~&While evaluating the form ~
53 starting at line ~D, column ~D~% of ~S:"
54 (car point) (cdr point)
55 (or pathname stream)))))
56 (eval-form (form index)
57 (with-simple-restart (continue "Ignore error and continue ~A file ~S."
58 context native)
59 (loop
60 (handler-bind ((serious-condition #'condition-herald))
61 (with-simple-restart (retry "Retry EVAL of current toplevel form.")
62 (if print
63 (let ((results (multiple-value-list (eval-tlf form index))))
64 (load-fresh-line)
65 (format t "~{~S~^, ~}~%" results))
66 (eval-tlf form index)))
67 (return))))))
68 (if pathname
69 (let* ((info (sb!c::make-file-source-info
70 pathname (stream-external-format stream)))
71 (sb!c::*source-info* info))
72 (setf (sb!c::source-info-stream info) stream)
73 (sb!c::do-forms-from-info ((form current-index) info
74 'sb!c::input-error-in-load)
75 (sb!c::with-source-paths
76 (sb!c::find-source-paths form current-index)
77 (eval-form form current-index))))
78 (let ((sb!c::*source-info* nil))
79 (do ((form (read stream nil *eof-object*)
80 (read stream nil *eof-object*)))
81 ((eq form *eof-object*))
82 (sb!c::with-source-paths
83 (eval-form form nil))))))))
86 ;;;; LOAD itself
88 (define-condition fasl-header-missing (invalid-fasl)
89 ((fhsss :reader invalid-fasl-fhsss :initarg :fhsss))
90 (:report
91 (lambda (condition stream)
92 (format stream "~@<File ~S has a fasl file type, but no fasl header:~%~
93 Expected ~S, but got ~S.~:@>"
94 (invalid-fasl-stream condition)
95 (invalid-fasl-expected condition)
96 (invalid-fasl-fhsss condition)))))
99 ;;; The following comment preceded the pre 1.0.12.36 definition of
100 ;;; LOAD; it may no longer be accurate:
102 ;; FIXME: Daniel Barlow's ilsb.tar ILISP-for-SBCL patches contain an
103 ;; implementation of "DEFUN SOURCE-FILE" which claims, in a comment,
104 ;; that CMU CL does not correctly record source file information when
105 ;; LOADing a non-compiled file. Check whether this bug exists in SBCL
106 ;; and fix it if so.
108 (defun load (pathspec &key (verbose *load-verbose*) (print *load-print*)
109 (if-does-not-exist t) (external-format :default))
110 "Load the file given by FILESPEC into the Lisp environment, returning
111 T on success."
112 (flet ((load-stream (stream faslp)
113 (when (and (fd-stream-p stream)
114 (eq (sb!impl::fd-stream-fd-type stream) :directory))
115 (error 'simple-file-error
116 :pathname (pathname stream)
117 :format-control
118 "Can't LOAD a directory: ~s."
119 :format-arguments (list (pathname stream))))
120 (let* (;; Bindings required by ANSI.
121 (*readtable* *readtable*)
122 (*package* (sane-package))
123 ;; FIXME: we should probably document the circumstances
124 ;; where *LOAD-PATHNAME* and *LOAD-TRUENAME* aren't
125 ;; pathnames during LOAD. ANSI makes no exceptions here.
126 (*load-pathname* (handler-case (pathname stream)
127 ;; FIXME: it should probably be a type
128 ;; error to try to get a pathname for a
129 ;; stream that doesn't have one, but I
130 ;; don't know if we guarantee that.
131 (error () nil)))
132 (*load-truename* (when *load-pathname*
133 (handler-case (truename stream)
134 (file-error () nil))))
135 ;; Bindings used internally.
136 (*load-depth* (1+ *load-depth*))
137 ;; KLUDGE: I can't find in the ANSI spec where it says
138 ;; that DECLAIM/PROCLAIM of optimization policy should
139 ;; have file scope. CMU CL did this, and it seems
140 ;; reasonable, but it might not be right; after all,
141 ;; things like (PROCLAIM '(TYPE ..)) don't have file
142 ;; scope, and I can't find anything under PROCLAIM or
143 ;; COMPILE-FILE or LOAD or OPTIMIZE which justifies this
144 ;; behavior. Hmm. -- WHN 2001-04-06
145 (sb!c::*policy* sb!c::*policy*))
146 (return-from load
147 (if faslp
148 (prog1 (load-as-fasl stream verbose print)
149 ;; Try to ameliorate immobile heap fragmentation
150 ;; in case somehow nontoplevel code is garbage.
151 #!+immobile-code (gc))
152 (sb!c:with-compiler-error-resignalling
153 (load-as-source stream :verbose verbose
154 :print print)))))))
155 ;; Case 1: stream.
156 (when (streamp pathspec)
157 (return-from load (load-stream pathspec (fasl-header-p pathspec))))
158 (let ((pathname (pathname pathspec)))
159 ;; Case 2: Open as binary, try to process as a fasl.
160 (with-open-stream
161 (stream (or (open pathspec :element-type '(unsigned-byte 8)
162 :if-does-not-exist nil)
163 (when (null (pathname-type pathspec))
164 (let ((defaulted-pathname
165 (probe-load-defaults pathspec)))
166 (if defaulted-pathname
167 (progn (setq pathname defaulted-pathname)
168 (open pathname
169 :if-does-not-exist
170 (if if-does-not-exist :error nil)
171 :element-type '(unsigned-byte 8))))))
172 (if if-does-not-exist
173 (error 'simple-file-error
174 :pathname pathspec
175 :format-control
176 "~@<Couldn't load ~S: file does not exist.~@:>"
177 :format-arguments (list pathspec))
178 (return-from load nil))))
179 (let* ((real (probe-file stream))
180 (should-be-fasl-p
181 (and real (string-equal (pathname-type real) *fasl-file-type*))))
182 ;; Don't allow empty .fasls, and assume other empty files
183 ;; are source files.
184 (when (and (or should-be-fasl-p (not (eql 0 (file-length stream))))
185 (fasl-header-p stream :errorp should-be-fasl-p))
186 (return-from load (load-stream stream t)))))
187 ;; Case 3: Open using the given external format, process as source.
188 (with-open-file (stream pathname :external-format external-format
189 :class 'form-tracking-stream)
190 (load-stream stream nil)))))
192 ;; This implements the defaulting SBCL seems to have inherited from
193 ;; CMU. This routine does not try to perform any loading; all it does
194 ;; is return the pathname (not the truename) of a file to be loaded,
195 ;; or NIL if no such file can be found. This routine is supposed to
196 ;; signal an error if a fasl's timestamp is older than its source
197 ;; file, but we protect against errors in PROBE-FILE, because any of
198 ;; the ways that we might fail to find a defaulted file are reasons
199 ;; not to load it, but not worth exposing to the user who didn't
200 ;; expicitly ask us to load a file with a made-up name (e.g., the
201 ;; defaulted filename might exceed filename length limits).
202 (defun probe-load-defaults (pathname)
203 (destructuring-bind (defaulted-source-pathname
204 defaulted-source-truename
205 defaulted-fasl-pathname
206 defaulted-fasl-truename)
207 (loop for type in (list *load-source-default-type*
208 *fasl-file-type*)
209 as probe-pathname = (make-pathname :type type
210 :defaults pathname)
211 collect probe-pathname
212 collect (handler-case (probe-file probe-pathname)
213 (file-error () nil)))
214 (cond ((and defaulted-fasl-truename
215 defaulted-source-truename
216 (> (file-write-date defaulted-source-truename)
217 (file-write-date defaulted-fasl-truename)))
218 (restart-case
219 (error "The object file ~A is~@
220 older than the presumed source:~% ~A."
221 defaulted-fasl-truename
222 defaulted-source-truename)
223 (source () :report "load source file"
224 defaulted-source-pathname)
225 (object () :report "load object file"
226 defaulted-fasl-pathname)))
227 (defaulted-fasl-truename defaulted-fasl-pathname)
228 (defaulted-source-truename defaulted-source-pathname))))
230 ;;; Load a code object. BOX-NUM objects are popped off the stack for
231 ;;; the boxed storage section, then CODE-LENGTH bytes of code are read in.
232 (defun load-code (nfuns box-num code-length stack ptr fasl-input)
233 (declare (fixnum box-num code-length))
234 (declare (simple-vector stack) (type index ptr))
235 (let* ((debug-info-index (+ ptr box-num))
236 (toplevel-p (svref stack (1+ debug-info-index)))
237 (code (sb!c:allocate-code-object #!+immobile-code (not toplevel-p)
238 box-num code-length)))
239 (declare (ignorable toplevel-p))
240 (setf (%code-debug-info code) (svref stack debug-info-index))
241 (loop for i of-type index from sb!vm:code-constants-offset
242 for j of-type index from ptr below debug-info-index
243 do (setf (code-header-ref code i) (svref stack j)))
244 (without-gcing
245 ;; FIXME: can this be WITH-PINNED-OBJECTS? Probably.
246 ;; We must pin the range of bytes containing instructions,
247 ;; but we also must prevent scavenging the code object until
248 ;; the embedded simple-funs have been installed,
249 ;; otherwise GC could assert that the word referenced by
250 ;; a fun offset does not have the right widetag.
251 ;; This is achieved by not writing the 'nfuns' value
252 ;; until after the loop which stores the offsets.
253 (read-n-bytes (%fasl-input-stream fasl-input)
254 (code-instructions code) 0 code-length)
255 (loop for i from (1- nfuns) downto 0
256 do (sb!c::new-simple-fun code i (read-varint-arg fasl-input)
257 nfuns)))
258 code))
260 ;;;; linkage fixups
262 ;;; how we learn about assembler routines at startup
263 (defvar *!initial-assembler-routines*)
265 (defun !loader-cold-init ()
266 (/show0 "/!loader-cold-init")
267 (dovector (routine *!initial-assembler-routines*)
268 (setf (gethash (car routine) *assembler-routines*) (cdr routine))))