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 *big-compiler-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 ;;; FIXME: why do all of these reading functions and macros declare
55 ;;; (SPEED 0)? was there some bug in the compiler which has since
56 ;;; been fixed? --njf, 2004-09-08
58 ;;; This expands into code to read an N-byte unsigned integer using
60 (defmacro fast-read-u-integer
(n)
61 (declare (optimize (speed 0)))
62 (do ((res '(fast-read-byte)
63 `(logior (fast-read-byte)
68 ;;; like FAST-READ-U-INTEGER, but the size may be determined at run time
69 (defmacro fast-read-var-u-integer
(n)
70 (let ((n-pos (gensym))
73 `(do ((,n-pos
8 (+ ,n-pos
8))
74 (,n-cnt
(1- ,n
) (1- ,n-cnt
))
77 (dpb (fast-read-byte) (byte 8 ,n-pos
) ,n-res
)))
78 ((zerop ,n-cnt
) ,n-res
)
79 (declare (type index
,n-pos
,n-cnt
)))))
81 ;;; Read a signed integer.
82 (defmacro fast-read-s-integer
(n)
83 (declare (optimize (speed 0)))
84 (let ((n-last (gensym)))
85 (do ((res `(let ((,n-last
(fast-read-byte)))
86 (if (zerop (logand ,n-last
#x80
))
88 (logior ,n-last
#x-100
)))
89 `(logior (fast-read-byte)
90 (ash (the (signed-byte ,(* cnt
8)) ,res
) 8)))
94 ;;; Read an N-byte unsigned integer from the *FASL-INPUT-STREAM*.
95 (defmacro read-arg
(n)
96 (declare (optimize (speed 0)))
98 `(the (unsigned-byte 8) (read-byte *fasl-input-stream
*))
99 `(prepare-for-fast-read-byte *fasl-input-stream
*
101 (fast-read-u-integer ,n
)
102 (done-with-fast-read-byte)))))
104 (declaim (inline read-byte-arg read-halfword-arg read-word-arg
))
105 (defun read-byte-arg ()
106 (declare (optimize (speed 0)))
109 (defun read-halfword-arg ()
110 (declare (optimize (speed 0)))
111 (read-arg #.
(/ sb
!vm
:n-word-bytes
2)))
113 (defun read-word-arg ()
114 (declare (optimize (speed 0)))
115 (read-arg #.sb
!vm
:n-word-bytes
))
117 (defun read-unsigned-byte-32-arg ()
118 (declare (optimize (speed 0)))
124 ;;; The table is implemented as a simple-vector indexed by the table
125 ;;; offset. We may need to have several, since LOAD can be called
128 ;;; a list of free fop tables for the fasloader
130 ;;; FIXME: Is it really a win to have this permanently bound?
131 ;;; Couldn't we just bind it on entry to LOAD-AS-FASL?
132 (defvar *free-fop-tables
* (list (make-array 1000)))
134 ;;; the current fop table
135 (defvar *current-fop-table
*)
136 (declaim (simple-vector *current-fop-table
*))
138 ;;; the length of the current fop table
139 (defvar *current-fop-table-size
*)
140 (declaim (type index
*current-fop-table-size
*))
142 ;;; the index in the fop-table of the next entry to be used
143 (defvar *current-fop-table-index
*)
144 (declaim (type index
*current-fop-table-index
*))
146 (defun grow-fop-table ()
147 (let* ((new-size (* *current-fop-table-size
* 2))
148 (new-table (make-array new-size
)))
149 (declare (fixnum new-size
) (simple-vector new-table
))
150 (replace new-table
(the simple-vector
*current-fop-table
*))
151 (setq *current-fop-table
* new-table
)
152 (setq *current-fop-table-size
* new-size
)))
154 (defmacro push-fop-table
(thing)
155 (let ((n-index (gensym)))
156 `(let ((,n-index
*current-fop-table-index
*))
157 (declare (fixnum ,n-index
))
158 (when (= ,n-index
(the fixnum
*current-fop-table-size
*))
160 (setq *current-fop-table-index
* (1+ ,n-index
))
161 (setf (svref *current-fop-table
* ,n-index
) ,thing
))))
165 ;;; (This is to be bound by LOAD to an adjustable (VECTOR T) with
166 ;;; FILL-POINTER, for use as a stack with VECTOR-PUSH-EXTEND.)
168 (declaim (type (vector t
) *fop-stack
*))
170 ;;; Cache information about the fop stack in local variables. Define a
171 ;;; local macro to pop from the stack. Push the result of evaluation
173 (defmacro with-fop-stack
(pushp &body forms
)
174 (aver (member pushp
'(nil t
:nope
)))
175 (with-unique-names (fop-stack)
176 `(let ((,fop-stack
*fop-stack
*))
177 (declare (type (vector t
) ,fop-stack
)
178 (ignorable ,fop-stack
))
179 (macrolet ((pop-stack ()
180 `(vector-pop ,',fop-stack
))
182 `(vector-push-extend ,value
,',fop-stack
))
183 (call-with-popped-args (fun n
)
184 `(%call-with-popped-args
,fun
,n
,',fop-stack
)))
186 `(vector-push-extend (progn ,@forms
) ,fop-stack
)
187 `(progn ,@forms
))))))
189 ;;; Call FUN with N arguments popped from STACK.
190 (defmacro %call-with-popped-args
(fun n stack
)
191 ;; N's integer value must be known at macroexpansion time.
192 (declare (type index n
))
193 (with-unique-names (n-stack old-length new-length
)
194 (let ((argtmps (make-gensym-list n
)))
195 `(let* ((,n-stack
,stack
)
196 (,old-length
(fill-pointer ,n-stack
))
197 (,new-length
(- ,old-length
,n
))
198 ,@(loop for i from
0 below n collecting
200 (aref ,n-stack
(+ ,new-length
,i
)))))
201 (declare (type (vector t
) ,n-stack
))
202 (setf (fill-pointer ,n-stack
) ,new-length
)
203 ;; (For some applications it might be appropriate to FILL the
204 ;; popped area with NIL here, to avoid holding onto garbage. For
205 ;; sbcl-0.8.7.something, though, it shouldn't matter, because
206 ;; we're using this only to pop stuff off *FOP-STACK*, and the
207 ;; entire *FOP-STACK* can be GCed as soon as LOAD returns.)
210 ;;;; Conditions signalled on invalid fasls (wrong fasl version, etc),
211 ;;;; so that user code (esp. ASDF) can reasonably handle attempts to
212 ;;;; load such fasls by recompiling them, etc. For simplicity's sake
213 ;;;; make only condition INVALID-FASL part of the public interface,
214 ;;;; and keep the guts internal.
216 (define-condition invalid-fasl
(error)
217 ((stream :reader invalid-fasl-stream
:initarg
:stream
)
218 (expected :reader invalid-fasl-expected
:initarg
:expected
))
220 (lambda (condition stream
)
221 (format stream
"~S is an invalid fasl file."
222 (invalid-fasl-stream condition
)))))
224 (define-condition invalid-fasl-header
(invalid-fasl)
225 ((byte :reader invalid-fasl-byte
:initarg
:byte
)
226 (byte-nr :reader invalid-fasl-byte-nr
:initarg
:byte-nr
))
228 (lambda (condition stream
)
229 (format stream
"~@<~S contains an illegal byte in the FASL header at ~
230 position ~A: Expected ~A, got ~A.~:@>"
231 (invalid-fasl-stream condition
)
232 (invalid-fasl-byte-nr condition
)
233 (invalid-fasl-byte condition
)
234 (invalid-fasl-expected condition
)))))
236 (define-condition invalid-fasl-version
(invalid-fasl)
237 ((version :reader invalid-fasl-version
:initarg
:version
))
239 (lambda (condition stream
)
240 (format stream
"~@<~S is a fasl file compiled with SBCL ~W, and ~
241 can't be loaded into SBCL ~W.~:@>"
242 (invalid-fasl-stream condition
)
243 (invalid-fasl-version condition
)
244 (invalid-fasl-expected condition
)))))
246 (define-condition invalid-fasl-implementation
(invalid-fasl)
247 ((implementation :reader invalid-fasl-implementation
248 :initarg
:implementation
))
250 (lambda (condition stream
)
251 (format stream
"~S was compiled for implementation ~A, but this is a ~A."
252 (invalid-fasl-stream condition
)
253 (invalid-fasl-implementation condition
)
254 (invalid-fasl-expected condition
)))))
256 (define-condition invalid-fasl-features
(invalid-fasl)
257 ((potential-features :reader invalid-fasl-potential-features
258 :initarg
:potential-features
)
259 (features :reader invalid-fasl-features
:initarg
:features
))
261 (lambda (condition stream
)
262 (format stream
"~@<incompatible ~S in fasl file ~S: ~2I~_~
263 Of features affecting binary compatibility, ~4I~_~S~2I~_~
264 the fasl has ~4I~_~A,~2I~_~
265 while the runtime expects ~4I~_~A.~:>"
267 (invalid-fasl-stream condition
)
268 (invalid-fasl-potential-features condition
)
269 (invalid-fasl-features condition
)
270 (invalid-fasl-expected condition
)))))
274 ;;;; Note: LOAD-AS-FASL is used not only by LOAD, but also (with
275 ;;;; suitable modification of the fop table) in GENESIS. Therefore,
276 ;;;; it's needed not only in the target Lisp, but also in the
277 ;;;; cross-compilation host.
279 ;;; a helper function for LOAD-FASL-GROUP
281 ;;; Return true if we successfully read a FASL header from the stream,
282 ;;; or NIL if EOF was hit before anything was read. Signal an error if
283 ;;; we encounter garbage.
284 (defun check-fasl-header (stream)
285 (let ((byte (read-byte stream nil
)))
287 ;; Read and validate constant string prefix in fasl header.
288 (let* ((fhsss *fasl-header-string-start-string
*)
289 (fhsss-length (length fhsss
)))
290 (unless (= byte
(char-code (schar fhsss
0)))
291 (error 'invalid-fasl-header
295 :expected
(char-code (schar fhsss
0))))
296 (do ((byte (read-byte stream
) (read-byte stream
))
297 (count 1 (1+ count
)))
298 ((= byte
+fasl-header-string-stop-char-code
+)
300 (declare (fixnum byte count
))
301 (when (and (< count fhsss-length
)
302 (not (eql byte
(char-code (schar fhsss count
)))))
303 (error 'invalid-fasl-header
307 :expected
(char-code (schar fhsss count
))))))
308 ;; Read and validate version-specific compatibility stuff.
309 (flet ((string-from-stream ()
310 (let* ((length (read-unsigned-byte-32-arg))
311 (result (make-string length
)))
312 (read-string-as-bytes stream result
)
314 ;; Read and validate implementation and version.
315 (let ((implementation (keywordicate (string-from-stream)))
316 (expected-implementation +backend-fasl-file-implementation
+))
317 (unless (string= expected-implementation implementation
)
318 (error 'invalid-fasl-implementation
320 :implementation implementation
321 :expected expected-implementation
)))
322 (let* ((fasl-version (read-word-arg))
323 (sbcl-version (if (<= fasl-version
76)
325 (string-from-stream)))
326 (expected-version (sb!xc
:lisp-implementation-version
)))
327 (unless (string= expected-version sbcl-version
)
329 (error 'invalid-fasl-version
331 :version sbcl-version
332 :expected expected-version
)
333 (continue () :report
"Load the fasl file anyway"))))
334 ;; Read and validate *FEATURES* which affect binary compatibility.
335 (let ((faff-in-this-file (string-from-stream)))
336 (unless (string= faff-in-this-file
*features-affecting-fasl-format
*)
337 (error 'invalid-fasl-features
339 :potential-features
*features-potentially-affecting-fasl-format
*
340 :expected
*features-affecting-fasl-format
*
341 :features faff-in-this-file
)))
345 ;; Setting this variable gives you a trace of fops as they are loaded and
348 (defvar *show-fops-p
* nil
)
350 ;; buffer for loading symbols
351 (defvar *fasl-symbol-buffer
*)
352 (declaim (simple-string *fasl-symbol-buffer
*))
355 ;;; a helper function for LOAD-AS-FASL
357 ;;; Return true if we successfully load a group from the stream, or
358 ;;; NIL if EOF was encountered while trying to read from the stream.
359 ;;; Dispatch to the right function for each fop.
360 (defun load-fasl-group (stream)
361 (when (check-fasl-header stream
)
362 (catch 'fasl-group-end
363 (let ((*current-fop-table-index
* 0)
365 (declare (special *skip-until
*))
367 (let ((byte (read-byte stream
)))
368 ;; Do some debugging output.
371 (let* ((stack *fop-stack
*)
372 (ptr (1- (fill-pointer *fop-stack
*))))
373 (fresh-line *trace-output
*)
374 ;; The FOP operations are stack based, so it's sorta
375 ;; logical to display the operand before the operator.
376 ;; ("reverse Polish notation")
378 (write-char #\space
*trace-output
*)
379 (prin1 (aref stack ptr
) *trace-output
*)
380 (terpri *trace-output
*))
381 ;; Display the operator.
382 (format *trace-output
*
383 "~&~S (#X~X at ~D) (~S)~%"
384 (aref *fop-names
* byte
)
386 (1- (file-position stream
))
387 (svref *fop-funs
* byte
))))
389 ;; Actually execute the fop.
390 (funcall (the function
(svref *fop-funs
* byte
)))))))))
392 (defun load-as-fasl (stream verbose print
)
393 ;; KLUDGE: ANSI says it's good to do something with the :PRINT
394 ;; argument to LOAD when we're fasloading a file, but currently we
395 ;; don't. (CMU CL did, but implemented it in a non-ANSI way, and I
396 ;; just disabled that instead of rewriting it.) -- WHN 20000131
397 (declare (ignore print
))
398 (when (zerop (file-length stream
))
399 (error "attempt to load an empty FASL file:~% ~S" (namestring stream
)))
400 (maybe-announce-load stream verbose
)
401 (sb!thread
:with-recursive-lock
(sb!c
::*big-compiler-lock
*)
402 (let* ((*fasl-input-stream
* stream
)
403 (*fasl-symbol-buffer
* (make-string 100))
404 (*current-fop-table
* (or (pop *free-fop-tables
*) (make-array 1000)))
405 (*current-fop-table-size
* (length *current-fop-table
*))
406 (*fop-stack
* (make-array 100 :fill-pointer
0 :adjustable t
)))
408 (loop while
(load-fasl-group stream
))
409 (push *current-fop-table
* *free-fop-tables
*)
410 ;; NIL out the table, so that we don't hold onto garbage.
412 ;; FIXME: Could we just get rid of the free fop table pool so
413 ;; that this would go away?
414 (fill *current-fop-table
* nil
))))
417 ;;;; stuff for debugging/tuning by collecting statistics on FOPs (?)
420 (defvar *fop-counts
* (make-array 256 :initial-element
0))
421 (defvar *fop-times
* (make-array 256 :initial-element
0))
422 (defvar *print-fops
* nil
)
424 (defun clear-counts ()
425 (fill (the simple-vector
*fop-counts
*) 0)
426 (fill (the simple-vector
*fop-times
*) 0)
429 (defun analyze-counts ()
434 (macrolet ((breakdown (lvar tvar vec
)
438 (let ((n (svref ,vec i
)))
439 (push (cons (svref *fop-names
* i
) n
) ,lvar
)
441 (setq ,lvar
(subseq (sort ,lvar
(lambda (x y
)
442 (> (cdr x
) (cdr y
))))
445 (breakdown counts total-count
*fop-counts
*)
446 (breakdown times total-time
*fop-times
*)
447 (format t
"Total fop count is ~D~%" total-count
)
449 (format t
"~30S: ~4D~%" (car c
) (cdr c
)))
450 (format t
"~%Total fop time is ~D~%" (/ (float total-time
) 60.0))
452 (format t
"~30S: ~6,2F~%" (car m
) (/ (float (cdr m
)) 60.0))))))