1 ;;;; parts of the loader which make sense in the cross-compilation
2 ;;;; host (and which are useful in the host, because they're used by
5 ;;;; based on the CMU CL load.lisp code, written by Skef Wholey and
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!FASL")
19 ;;;; There looks to be an exciting amount of state being modified
20 ;;;; here: certainly enough that I (dan, 2003.1.22) don't want to mess
21 ;;;; around deciding how to thread-safetify it. So we use a Big Lock.
22 ;;;; Because this code is mutually recursive with the compiler, we use
23 ;;;; the **WORLD-LOCK**.
25 ;;;; miscellaneous load utilities
27 ;;; Output the current number of semicolons after a fresh-line.
28 ;;; FIXME: non-mnemonic name
29 (defun load-fresh-line ()
31 (let ((semicolons ";;;;;;;;;;;;;;;;"))
32 (do ((count *load-depth* (- count (length semicolons))))
33 ((< count (length semicolons))
34 (write-string semicolons *standard-output* :end count))
35 (declare (fixnum count))
36 (write-string semicolons))
37 (write-char #\space)))
39 ;;; If VERBOSE, output (to *STANDARD-OUTPUT*) a message about how
40 ;;; we're loading from STREAM-WE-ARE-LOADING-FROM.
41 (defun maybe-announce-load (stream-we-are-loading-from verbose)
44 (let ((name #-sb-xc-host (file-name stream-we-are-loading-from)
47 (format t "loading ~S~%" name)
48 (format t "loading stuff from ~S~%" stream-we-are-loading-from)))))
50 ;;;; utilities for reading from fasl files
52 #!-sb-fluid (declaim (inline read-byte))
54 ;;; This expands into code to read an N-byte unsigned integer using
56 (defmacro fast-read-u-integer (n)
58 `(let ,(loop for i from 0 below n
59 collect (let ((name (gensym "B")))
61 `(,name ,(if (zerop i)
63 `(ash (fast-read-byte) ,(* i 8))))))
66 ;;; like FAST-READ-U-INTEGER, but the size may be determined at run time
67 (defmacro fast-read-var-u-integer (n)
68 (let ((n-pos (gensym))
71 `(do ((,n-pos 8 (+ ,n-pos 8))
72 (,n-cnt (1- ,n) (1- ,n-cnt))
75 (dpb (fast-read-byte) (byte 8 ,n-pos) ,n-res)))
76 ((zerop ,n-cnt) ,n-res)
77 (declare (type index ,n-pos ,n-cnt)))))
79 ;;; Read a signed integer.
80 (defmacro fast-read-s-integer (n)
81 (declare (optimize (speed 0)))
82 (let ((n-last (gensym)))
83 (do ((res `(let ((,n-last (fast-read-byte)))
84 (if (zerop (logand ,n-last #x80))
86 (logior ,n-last #x-100)))
87 `(logior (fast-read-byte)
88 (ash (the (signed-byte ,(* cnt 8)) ,res) 8)))
92 ;;; Read an N-byte unsigned integer from the *FASL-INPUT-STREAM*.
93 (defmacro read-arg (n)
94 (declare (optimize (speed 0)))
96 `(the (unsigned-byte 8) (read-byte *fasl-input-stream*))
97 `(with-fast-read-byte ((unsigned-byte 8) *fasl-input-stream*)
98 (fast-read-u-integer ,n))))
100 (declaim (inline read-byte-arg read-halfword-arg read-word-arg))
101 (defun read-byte-arg ()
102 (declare (optimize (speed 0)))
105 (defun read-halfword-arg ()
106 (declare (optimize (speed 0)))
107 (read-arg #.(/ sb!vm:n-word-bytes 2)))
109 (defun read-word-arg ()
110 (declare (optimize (speed 0)))
111 (read-arg #.sb!vm:n-word-bytes))
113 (defun read-unsigned-byte-32-arg ()
114 (declare (optimize (speed 0)))
120 ;;; The table is implemented as a simple-vector indexed by the table
121 ;;; offset. The offset is kept in at index 0 of the vector.
123 ;;; FOPs use the table to save stuff, other FOPs refer to the table by
124 ;;; direct indexes via REF-FOP-TABLE.
127 (declaim (simple-vector *fop-table*))
129 (declaim (inline ref-fop-table))
130 (defun ref-fop-table (index)
131 (declare (type index index))
132 (svref *fop-table* (the index (+ index 1))))
134 (defun get-fop-table-index ()
135 (svref *fop-table* 0))
137 (defun reset-fop-table ()
138 (setf (svref *fop-table* 0) 0))
140 (defun push-fop-table (thing)
141 (let* ((table *fop-table*)
142 (index (+ (the index (aref table 0)) 1)))
143 (declare (fixnum index)
144 (simple-vector table))
145 (when (eql index (length table))
146 (setf table (grow-fop-vector table index)
148 (setf (aref table 0) index
149 (aref table index) thing)))
151 ;;; These three routines are used for both the stack and the table.
152 (defun make-fop-vector (size)
153 (declare (type index size))
154 (let ((vector (make-array size)))
155 (setf (aref vector 0) 0)
158 (defun grow-fop-vector (old-vector old-size)
159 (declare (simple-vector old-vector)
160 (type index old-size))
161 (let* ((new-size (* old-size 2))
162 (new-vector (make-array new-size)))
163 (declare (fixnum new-size)
164 (simple-vector new-vector old-vector))
165 (replace new-vector old-vector)
166 (nuke-fop-vector old-vector)
169 (defun nuke-fop-vector (vector)
170 (declare (simple-vector vector)
171 #!-gencgc (ignore vector)
173 ;; Make sure we don't keep any garbage.
180 ;;; Much like the table, this is bound to a simple vector whose first
181 ;;; element is the current index.
183 (declaim (simple-vector *fop-stack*))
185 (defun fop-stack-empty-p ()
186 (eql 0 (svref *fop-stack* 0)))
188 (defun pop-fop-stack ()
189 (let* ((stack *fop-stack*)
190 (top (svref stack 0)))
191 (declare (type index top))
193 (error "FOP stack empty"))
194 (setf (svref stack 0) (1- top))
197 (defun push-fop-stack (value)
198 (let* ((stack *fop-stack*)
199 (next (1+ (the index (svref stack 0)))))
200 (declare (type index next))
201 (when (eql (length stack) next)
202 (setf stack (grow-fop-vector stack next)
204 (setf (svref stack 0) next
205 (svref stack next) value)))
207 ;;; Define a local macro to pop from the stack. Push the result of evaluation
209 (defmacro with-fop-stack (pushp &body forms)
210 (aver (member pushp '(nil t :nope)))
211 `(macrolet ((pop-stack ()
214 `(push-fop-stack ,value)))
216 `(push-fop-stack (progn ,@forms))
219 ;;; Call FUN with N arguments popped from STACK.
220 (defmacro call-with-popped-args (fun n)
221 ;; N's integer value must be known at macroexpansion time.
222 (declare (type index n))
223 (with-unique-names (n-stack old-top new-top)
224 (let ((argtmps (make-gensym-list n)))
225 `(let* ((,n-stack *fop-stack*)
226 (,old-top (svref ,n-stack 0))
227 (,new-top (- ,old-top ,n))
228 ,@(loop for i from 1 upto n collecting
229 `(,(nth (1- i) argtmps)
230 (aref ,n-stack (+ ,new-top ,i)))))
231 (declare (simple-vector ,n-stack))
232 (setf (svref ,n-stack 0) ,new-top)
233 ;; (For some applications it might be appropriate to FILL the
234 ;; popped area with NIL here, to avoid holding onto garbage. For
235 ;; sbcl-0.8.7.something, though, it shouldn't matter, because
236 ;; we're using this only to pop stuff off *FOP-STACK*, and the
237 ;; entire *FOP-STACK* can be GCed as soon as LOAD returns.)
240 ;;;; Conditions signalled on invalid fasls (wrong fasl version, etc),
241 ;;;; so that user code (esp. ASDF) can reasonably handle attempts to
242 ;;;; load such fasls by recompiling them, etc. For simplicity's sake
243 ;;;; make only condition INVALID-FASL part of the public interface,
244 ;;;; and keep the guts internal.
246 (define-condition invalid-fasl (error)
247 ((stream :reader invalid-fasl-stream :initarg :stream)
248 (expected :reader invalid-fasl-expected :initarg :expected))
250 (lambda (condition stream)
251 (format stream "~S is an invalid fasl file."
252 (invalid-fasl-stream condition)))))
254 (define-condition invalid-fasl-header (invalid-fasl)
255 ((byte :reader invalid-fasl-byte :initarg :byte)
256 (byte-nr :reader invalid-fasl-byte-nr :initarg :byte-nr))
258 (lambda (condition stream)
259 (format stream "~@<~S contains an illegal byte in the FASL header at ~
260 position ~A: Expected ~A, got ~A.~:@>"
261 (invalid-fasl-stream condition)
262 (invalid-fasl-byte-nr condition)
263 (invalid-fasl-expected condition)
264 (invalid-fasl-byte condition)))))
266 (define-condition invalid-fasl-version (invalid-fasl)
267 ((version :reader invalid-fasl-version :initarg :version))
269 (lambda (condition stream)
270 (format stream "~@<~S is a fasl file compiled with SBCL ~W, and ~
271 can't be loaded into SBCL ~W.~:@>"
272 (invalid-fasl-stream condition)
273 (invalid-fasl-version condition)
274 (invalid-fasl-expected condition)))))
276 (define-condition invalid-fasl-implementation (invalid-fasl)
277 ((implementation :reader invalid-fasl-implementation
278 :initarg :implementation))
280 (lambda (condition stream)
281 (format stream "~S was compiled for implementation ~A, but this is a ~A."
282 (invalid-fasl-stream condition)
283 (invalid-fasl-implementation condition)
284 (invalid-fasl-expected condition)))))
286 (define-condition invalid-fasl-features (invalid-fasl)
287 ((potential-features :reader invalid-fasl-potential-features
288 :initarg :potential-features)
289 (features :reader invalid-fasl-features :initarg :features))
291 (lambda (condition stream)
292 (format stream "~@<incompatible ~S in fasl file ~S: ~2I~_~
293 Of features affecting binary compatibility, ~4I~_~S~2I~_~
294 the fasl has ~4I~_~A,~2I~_~
295 while the runtime expects ~4I~_~A.~:>"
297 (invalid-fasl-stream condition)
298 (invalid-fasl-potential-features condition)
299 (invalid-fasl-features condition)
300 (invalid-fasl-expected condition)))))
302 ;;; Skips past the shebang line on stream, if any.
303 (defun maybe-skip-shebang-line (stream)
304 (let ((p (file-position stream)))
305 (flet ((next () (read-byte stream nil)))
307 (when (and (eq (next) (char-code #\#))
308 (eq (next) (char-code #\!)))
311 until (or (not x) (eq x (char-code #\newline)))))
313 (file-position stream p))))
316 ;;; Returns T if the stream is a binary input stream with a FASL header.
317 (defun fasl-header-p (stream &key errorp)
318 (unless (member (stream-element-type stream) '(character base-char))
319 (let ((p (file-position stream)))
321 (let* ((header *fasl-header-string-start-string*)
322 (buffer (make-array (length header) :element-type '(unsigned-byte 8)))
325 (maybe-skip-shebang-line stream)
326 (setf n (read-sequence buffer stream))))
329 (or (ignore-errors (scan))
330 ;; no a binary input stream
331 (return-from fasl-header-p nil))))
332 (if (mismatch buffer header
333 :test #'(lambda (code char) (= code (char-code char))))
334 ;; Immediate EOF is valid -- we want to match what
335 ;; CHECK-FASL-HEADER does...
338 (error 'fasl-header-missing
343 (file-position stream p)))))
348 ;;;; Note: LOAD-AS-FASL is used not only by LOAD, but also (with
349 ;;;; suitable modification of the fop table) in GENESIS. Therefore,
350 ;;;; it's needed not only in the target Lisp, but also in the
351 ;;;; cross-compilation host.
353 ;;; a helper function for LOAD-FASL-GROUP
355 ;;; Return true if we successfully read a FASL header from the stream, or NIL
356 ;;; if EOF was hit before anything except the optional shebang line was read.
357 ;;; Signal an error if we encounter garbage.
358 (defun check-fasl-header (stream)
359 (maybe-skip-shebang-line stream)
360 (let ((byte (read-byte stream nil)))
362 ;; Read and validate constant string prefix in fasl header.
363 (let* ((fhsss *fasl-header-string-start-string*)
364 (fhsss-length (length fhsss)))
365 (unless (= byte (char-code (schar fhsss 0)))
366 (error 'invalid-fasl-header
370 :expected (char-code (schar fhsss 0))))
371 (do ((byte (read-byte stream) (read-byte stream))
372 (count 1 (1+ count)))
373 ((= byte +fasl-header-string-stop-char-code+)
375 (declare (fixnum byte count))
376 (when (and (< count fhsss-length)
377 (not (eql byte (char-code (schar fhsss count)))))
378 (error 'invalid-fasl-header
382 :expected (char-code (schar fhsss count))))))
383 ;; Read and validate version-specific compatibility stuff.
384 (flet ((string-from-stream ()
385 (let* ((length (read-unsigned-byte-32-arg))
386 (result (make-string length)))
387 (read-string-as-bytes stream result)
389 ;; Read and validate implementation and version.
390 (let ((implementation (keywordicate (string-from-stream)))
391 (expected-implementation +backend-fasl-file-implementation+))
392 (unless (string= expected-implementation implementation)
393 (error 'invalid-fasl-implementation
395 :implementation implementation
396 :expected expected-implementation)))
397 (let* ((fasl-version (read-word-arg))
398 (sbcl-version (if (<= fasl-version 76)
400 (string-from-stream)))
401 (expected-version (sb!xc:lisp-implementation-version)))
402 (unless (string= expected-version sbcl-version)
404 (error 'invalid-fasl-version
406 :version sbcl-version
407 :expected expected-version)
408 (continue () :report "Load the fasl file anyway"))))
409 ;; Read and validate *FEATURES* which affect binary compatibility.
410 (let ((faff-in-this-file (string-from-stream)))
411 (unless (string= faff-in-this-file *features-affecting-fasl-format*)
412 (error 'invalid-fasl-features
414 :potential-features *features-potentially-affecting-fasl-format*
415 :expected *features-affecting-fasl-format*
416 :features faff-in-this-file)))
420 ;; Setting this variable gives you a trace of fops as they are loaded and
423 (defvar *show-fops-p* nil)
426 ;;; a helper function for LOAD-AS-FASL
428 ;;; Return true if we successfully load a group from the stream, or
429 ;;; NIL if EOF was encountered while trying to read from the stream.
430 ;;; Dispatch to the right function for each fop.
431 (defun load-fasl-group (stream)
432 (when (check-fasl-header stream)
433 (catch 'fasl-group-end
435 (let ((*skip-until* nil))
436 (declare (special *skip-until*))
438 (let ((byte (read-byte stream)))
439 ;; Do some debugging output.
442 (let* ((stack *fop-stack*)
443 (ptr (svref stack 0)))
444 (fresh-line *trace-output*)
445 ;; The FOP operations are stack based, so it's sorta
446 ;; logical to display the operand before the operator.
447 ;; ("reverse Polish notation")
449 (write-char #\space *trace-output*)
450 (prin1 (aref stack ptr) *trace-output*)
451 (terpri *trace-output*))
452 ;; Display the operator.
453 (format *trace-output*
454 "~&~S (#X~X at ~D) (~S)~%"
455 (aref *fop-names* byte)
457 (1- (file-position stream))
458 (svref *fop-funs* byte))))
460 ;; Actually execute the fop.
461 (funcall (the function (svref *fop-funs* byte)))))))))
463 (defun load-as-fasl (stream verbose print)
464 ;; KLUDGE: ANSI says it's good to do something with the :PRINT
465 ;; argument to LOAD when we're fasloading a file, but currently we
466 ;; don't. (CMU CL did, but implemented it in a non-ANSI way, and I
467 ;; just disabled that instead of rewriting it.) -- WHN 20000131
468 (declare (ignore print))
469 (when (zerop (file-length stream))
470 (error "attempt to load an empty FASL file:~% ~S" (namestring stream)))
471 (maybe-announce-load stream verbose)
472 (let* ((*fasl-input-stream* stream)
473 (*fop-table* (make-fop-vector 1000))
474 (*fop-stack* (make-fop-vector 100)))
476 (loop while (load-fasl-group stream))
477 ;; Nuke the table and stack to avoid keeping garbage on
478 ;; conservatively collected platforms.
479 (nuke-fop-vector *fop-table*)
480 (nuke-fop-vector *fop-stack*)))
483 (declaim (notinline read-byte)) ; Why is it even *declaimed* inline above?
485 ;;;; stuff for debugging/tuning by collecting statistics on FOPs (?)
488 (defvar *fop-counts* (make-array 256 :initial-element 0))
489 (defvar *fop-times* (make-array 256 :initial-element 0))
490 (defvar *print-fops* nil)
492 (defun clear-counts ()
493 (fill (the simple-vector *fop-counts*) 0)
494 (fill (the simple-vector *fop-times*) 0)
497 (defun analyze-counts ()
502 (macrolet ((breakdown (lvar tvar vec)
506 (let ((n (svref ,vec i)))
507 (push (cons (svref *fop-names* i) n) ,lvar)
509 (setq ,lvar (subseq (sort ,lvar (lambda (x y)
510 (> (cdr x) (cdr y))))
513 (breakdown counts total-count *fop-counts*)
514 (breakdown times total-time *fop-times*)
515 (format t "Total fop count is ~D~%" total-count)
517 (format t "~30S: ~4D~%" (car c) (cdr c)))
518 (format t "~%Total fop time is ~D~%" (/ (float total-time) 60.0))
520 (format t "~30S: ~6,2F~%" (car m) (/ (float (cdr m)) 60.0))))))