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!IMPL")
24 ;;; FIXME: It's awkward having LOAD stuff in SB!IMPL and dump stuff in
25 ;;; SB!C. Among other things, it makes it hard to figure out where
26 ;;; *FASL-HEADER-STRING-START-STRING* and
27 ;;; *FASL-HEADER-STRING-STOP-CHAR-CODE* should go. Perhaps we should
28 ;;; make a package called SB-DUMP or SB-LD which includes all
29 ;;; knowledge of both loading and dumping.
31 ;;; This value is used to identify fasl files. Even though this is not
32 ;;; declared as a constant (because ANSI Common Lisp has no facility
33 ;;; for declaring values which are constant under EQUAL but not EQL),
34 ;;; obviously you shouldn't mess with it lightly. If you do set a new
35 ;;; value for some reason, keep these things in mind:
36 ;;; * To avoid confusion with the similar but incompatible CMU CL
37 ;;; fasl file format, the value should not be "FASL FILE", which
38 ;;; is what CMU CL used for the same purpose.
39 ;;; * Since its presence at the head of a file is used by LOAD to
40 ;;; decide whether a file is to be fasloaded or sloloaded, the value
41 ;;; should be something which can't legally appear at the head of a
43 ;;; * The value should not contain any line-terminating characters,
44 ;;; because they're hard to express portably and because the LOAD
45 ;;; code might reasonably use READ-LINE to get the value to compare
47 (defparameter sb!c:*fasl-header-string-start-string* "# FASL"
49 "a string which appears at the start of a fasl file header")
51 (defparameter sb!c:*fasl-header-string-stop-char-code* 255
53 "the code for a character which terminates a fasl file header")
55 (defvar *load-depth* 0
57 "the current number of recursive loads")
58 (declaim (type index *load-depth*))
60 ;;; the FASL file we're reading from
62 (declaim (type lisp-stream fasl-file))
64 (defvar *load-print* nil
66 "the default for the :PRINT argument to LOAD")
67 (defvar *load-verbose* nil
68 ;; Note that CMU CL's default for this was T, and ANSI says it's
69 ;; implementation-dependent. We choose NIL on the theory that it's
70 ;; a nicer default behavior for Unix programs.
72 "the default for the :VERBOSE argument to LOAD")
74 ;;;; miscellaneous load utilities
76 ;;; Output the current number of semicolons after a fresh-line.
77 ;;; FIXME: non-mnemonic name
78 (defun load-fresh-line ()
80 (let ((semicolons ";;;;;;;;;;;;;;;;"))
81 (do ((count *load-depth* (- count (length semicolons))))
82 ((< count (length semicolons))
83 (write-string semicolons *standard-output* :end count))
84 (declare (fixnum count))
85 (write-string semicolons))
86 (write-char #\space)))
88 ;;; If VERBOSE, output (to *STANDARD-OUTPUT*) a message about how we're
89 ;;; loading from STREAM-WE-ARE-LOADING-FROM.
90 ;;; FIXME: non-mnemonic name
91 (defun do-load-verbose (stream-we-are-loading-from verbose)
94 (let ((name #-sb-xc-host (file-name stream-we-are-loading-from)
97 (format t "loading ~S~%" name)
98 (format t "loading stuff from ~S~%" stream-we-are-loading-from)))))
100 ;;;; utilities for reading from fasl files
102 #!-sb-fluid (declaim (inline read-byte))
104 ;;; Expands into code to read an N-byte unsigned integer using
106 (defmacro fast-read-u-integer (n)
107 (declare (optimize (speed 0)))
108 (do ((res '(fast-read-byte)
109 `(logior (fast-read-byte)
114 ;;; Like Fast-Read-U-Integer, but the size may be determined at run time.
115 (defmacro fast-read-variable-u-integer (n)
116 (let ((n-pos (gensym))
119 `(do ((,n-pos 8 (+ ,n-pos 8))
120 (,n-cnt (1- ,n) (1- ,n-cnt))
123 (dpb (fast-read-byte) (byte 8 ,n-pos) ,n-res)))
124 ((zerop ,n-cnt) ,n-res)
125 (declare (type index ,n-pos ,n-cnt)))))
127 ;;; Read a signed integer.
128 (defmacro fast-read-s-integer (n)
129 (declare (optimize (speed 0)))
130 (let ((n-last (gensym)))
131 (do ((res `(let ((,n-last (fast-read-byte)))
132 (if (zerop (logand ,n-last #x80))
134 (logior ,n-last #x-100)))
135 `(logior (fast-read-byte)
136 (ash (the (signed-byte ,(* cnt 8)) ,res) 8)))
140 ;;; Read an N-byte unsigned integer from the *FASL-FILE*
141 (defmacro read-arg (n)
142 (declare (optimize (speed 0)))
144 `(the (unsigned-byte 8) (read-byte *fasl-file*))
145 `(prepare-for-fast-read-byte *fasl-file*
147 (fast-read-u-integer ,n)
148 (done-with-fast-read-byte)))))
149 ;;; FIXME: This deserves a more descriptive name, and should probably
150 ;;; be implemented as an ordinary function, not a macro.
152 ;;; (for the names: There seem to be only two cases, so it could be
153 ;;; named READ-U-INTEGER-8 and READ-U-INTEGER-32 or something.)
157 ;;; The table is implemented as a simple-vector indexed by the table
158 ;;; offset. We may need to have several, since LOAD can be called
161 (defvar *free-fop-tables* (list (make-array 1000))
163 "List of free fop tables for the fasloader.")
165 ;;; the current fop table
166 (defvar *current-fop-table*)
167 (declaim (simple-vector *current-fop-table*))
169 ;;; the length of the current fop table
170 (defvar *current-fop-table-size*)
171 (declaim (type index *current-fop-table-size*))
173 ;;; the index in the fop-table of the next entry to be used
174 (defvar *current-fop-table-index*)
175 (declaim (type index *current-fop-table-index*))
177 (defun grow-fop-table ()
178 (let* ((new-size (* *current-fop-table-size* 2))
179 (new-table (make-array new-size)))
180 (declare (fixnum new-size) (simple-vector new-table))
181 (replace new-table (the simple-vector *current-fop-table*))
182 (setq *current-fop-table* new-table)
183 (setq *current-fop-table-size* new-size)))
185 (defmacro push-fop-table (thing)
186 (let ((n-index (gensym)))
187 `(let ((,n-index *current-fop-table-index*))
188 (declare (fixnum ,n-index))
189 (when (= ,n-index (the fixnum *current-fop-table-size*))
191 (setq *current-fop-table-index* (1+ ,n-index))
192 (setf (svref *current-fop-table* ,n-index) ,thing))))
196 ;;; (This is in a simple-vector, but it grows down, since it is
197 ;;; somewhat cheaper to test for overflow that way.)
198 (defvar *fop-stack* (make-array 100)
200 "The fop stack (we only need one!).")
201 (declaim (simple-vector *fop-stack*))
203 ;;; the index of the most recently pushed item on the fop-stack
204 (defvar *fop-stack-pointer* 100)
206 ;;; the current index into the fop stack when we last recursively
208 (defvar *fop-stack-pointer-on-entry*)
209 (declaim (type index *fop-stack-pointer* *fop-stack-pointer-on-entry*))
211 (defun grow-fop-stack ()
212 (let* ((size (length (the simple-vector *fop-stack*)))
213 (new-size (* size 2))
214 (new-stack (make-array new-size)))
215 (declare (fixnum size new-size) (simple-vector new-stack))
216 (replace new-stack (the simple-vector *fop-stack*) :start1 size)
217 (incf *fop-stack-pointer-on-entry* size)
218 (setq *fop-stack-pointer* size)
219 (setq *fop-stack* new-stack)))
221 ;;; Cache information about the fop-stack in local variables. Define a
222 ;;; local macro to pop from the stack. Push the result of evaluation
224 (defmacro with-fop-stack (pushp &body forms)
225 (check-type pushp (member nil t :nope))
226 (let ((n-stack (gensym))
229 `(let ((,n-stack *fop-stack*)
230 (,n-index *fop-stack-pointer*))
231 (declare (simple-vector ,n-stack) (type index ,n-index))
232 (macrolet ((pop-stack ()
234 (svref ,',n-stack ,',n-index)
236 (call-with-popped-things (fun n)
237 (let ((n-start (gensym)))
238 `(let ((,n-start (+ ,',n-index ,n)))
239 (declare (type index ,n-start))
240 (setq ,',n-index ,n-start)
241 (,fun ,@(make-list n :initial-element
243 (decf ,n-start))))))))
245 `(let ((,n-res (progn ,@forms)))
246 (when (zerop ,n-index)
248 (setq ,n-index *fop-stack-pointer*
249 ,n-stack *fop-stack*))
251 (setq *fop-stack-pointer* ,n-index)
252 (setf (svref ,n-stack ,n-index) ,n-res))
255 (setq *fop-stack-pointer* ,n-index)))))))
259 ;;;; Note: FASLOAD is used not only by LOAD, but also (after suitable
260 ;;;; modification of the fop table) in genesis. Therefore, it's needed
261 ;;;; not only in the target Lisp, but also in the cross-compilation
264 ;;; a helper function for LOAD-FASL-GROUP
266 ;;; Return true if we successfully read a FASL header from the stream,
267 ;;; or NIL if EOF was hit before anything was read. Signal an error if
268 ;;; we encounter garbage.
269 (defun check-fasl-header (stream)
271 (let ((byte (read-byte stream nil)))
274 ;; Read the string part of the fasl header, or die.
275 (let* ((fhsss sb!c:*fasl-header-string-start-string*)
276 (fhsss-length (length fhsss)))
277 (unless (= byte (char-code (schar fhsss 0)))
278 (error "illegal fasl file header"))
279 (do ((byte (read-byte stream) (read-byte stream))
280 (count 1 (1+ count)))
281 ((= byte sb!c:*fasl-header-string-stop-char-code*)
283 (declare (fixnum byte count))
284 (when (and (< count fhsss-length)
285 (not (eql byte (char-code (schar fhsss count)))))
286 (error "illegal fasl file header"))))
288 ;; Read and validate implementation and version, or die.
289 (let* ((implementation-length (read-arg 4))
290 (implementation-string (make-string implementation-length))
291 (ignore (read-string-as-bytes stream implementation-string))
292 (implementation (keywordicate implementation-string))
293 ;; FIXME: The logic above to read a keyword from the fasl file
294 ;; could probably be shared with the read-a-keyword fop.
295 (version (read-arg 4)))
296 (declare (ignore ignore))
297 (flet ((check-version (impl vers)
298 (when (string= impl implementation)
299 (unless (= version vers)
300 (error "~S was compiled for fasl file format version ~S, ~
301 but we need version ~S."
306 (or (check-version #.sb!c:*backend-fasl-file-implementation*
307 #.sb!c:*backend-fasl-file-version*)
308 (check-version #.(sb!c:backend-byte-fasl-file-implementation)
309 sb!c:byte-fasl-file-version)
310 (error "~S was compiled for implementation ~A, but this is a ~A."
313 sb!c:*backend-fasl-file-implementation*)))))))
315 ;; Setting this variable gives you a trace of fops as they are loaded and
318 (defvar *show-fops-p* nil)
320 ;;; a helper function for FASLOAD
322 ;;; Return true if we successfully load a group from the stream, or NIL if EOF
323 ;;; was encountered while trying to read from the stream. Dispatch to the right
324 ;;; function for each fop. Special-case FOP-BYTE-PUSH since it is real common.
325 (defun load-fasl-group (stream)
326 (when (check-fasl-header stream)
327 (catch 'fasl-group-end
328 (let ((*current-fop-table-index* 0))
330 (let ((byte (read-byte stream)))
332 ;; Do some debugging output.
335 (let ((ptr *fop-stack-pointer*)
337 (fresh-line *trace-output*)
338 ;; The FOP operations are stack based, so it's sorta
339 ;; logical to display the operand before the operator.
340 ;; ("reverse Polish notation")
341 (unless (= ptr (length stack))
342 (write-char #\space *trace-output*)
343 (prin1 (svref stack ptr) *trace-output*)
344 (terpri *trace-output*))
345 ;; Display the operator.
346 (format *trace-output*
347 "~&~S (#X~X at ~D) (~S)~%"
348 (svref *fop-names* byte)
350 (1- (file-position stream))
351 (svref *fop-functions* byte))))
353 ;; Actually execute the fop.
355 ;; FIXME: This is the special case for FOP-BYTE-PUSH.
356 ;; Benchmark to see whether it's really worth special
357 ;; casing it. If it is, at least express the test in
358 ;; terms of a symbolic name for the FOP-BYTE-PUSH code,
359 ;; not a bare '3' (!). Failing that, remove the special
360 ;; case (and the comment at the head of this function
361 ;; which mentions it).
362 (let ((index *fop-stack-pointer*))
363 (declare (type index index))
366 (setq index *fop-stack-pointer*))
368 (setq *fop-stack-pointer* index)
369 (setf (svref *fop-stack* index)
370 (svref *current-fop-table* (read-byte stream))))
371 (funcall (the function (svref *fop-functions* byte))))))))))
373 (defun fasload (stream verbose print)
374 ;; KLUDGE: ANSI says it's good to do something with the :PRINT
375 ;; argument to LOAD when we're fasloading a file, but currently we
376 ;; don't. (CMU CL did, but implemented it in a non-ANSI way, and I
377 ;; just disabled that instead of rewriting it.) -- WHN 20000131
378 (declare (ignore print))
379 (when (zerop (file-length stream))
380 (error "attempt to load an empty FASL file:~% ~S" (namestring stream)))
381 (do-load-verbose stream verbose)
382 (let* ((*fasl-file* stream)
383 (*current-fop-table* (or (pop *free-fop-tables*) (make-array 1000)))
384 (*current-fop-table-size* (length *current-fop-table*))
385 (*fop-stack-pointer-on-entry* *fop-stack-pointer*))
387 ;; FIXME: This should probably become
388 ;; (LOOP WHILE (LOAD-FASL-GROUP-STREAM))
389 ;; but as a LOOP newbie I don't want to do that until I can
391 (do ((loaded-group (load-fasl-group stream) (load-fasl-group stream)))
392 ((not loaded-group)))
393 (setq *fop-stack-pointer* *fop-stack-pointer-on-entry*)
394 (push *current-fop-table* *free-fop-tables*)
395 ;; NIL out the stack and table, so that we don't hold onto garbage.
397 ;; FIXME: Couldn't we just get rid of the free fop table pool so
398 ;; that some of this NILing out would go away?
399 (fill *fop-stack* nil :end *fop-stack-pointer-on-entry*)
400 (fill *current-fop-table* nil)))
403 ;;;; stuff for debugging/tuning by collecting statistics on FOPs (?)
406 (defvar *fop-counts* (make-array 256 :initial-element 0))
407 (defvar *fop-times* (make-array 256 :initial-element 0))
408 (defvar *print-fops* nil)
410 (defun clear-counts ()
411 (fill (the simple-vector *fop-counts*) 0)
412 (fill (the simple-vector *fop-times*) 0)
415 (defun analyze-counts ()
420 (macrolet ((breakdown (lvar tvar vec)
424 (let ((n (svref ,vec i)))
425 (push (cons (svref *fop-names* i) n) ,lvar)
427 (setq ,lvar (subseq (sort ,lvar #'(lambda (x y)
428 (> (cdr x) (cdr y))))
431 (breakdown counts total-count *fop-counts*)
432 (breakdown times total-time *fop-times*)
433 (format t "Total fop count is ~D~%" total-count)
435 (format t "~30S: ~4D~%" (car c) (cdr c)))
436 (format t "~%Total fop time is ~D~%" (/ (float total-time) 60.0))
438 (format t "~30S: ~6,2F~%" (car m) (/ (float (cdr m)) 60.0))))))