85620445a05f462f2b8e70bdd2a5db1f4f2b508c
[sbcl.git] / src / code / load.lisp
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
3 ;;;; GENESIS)
4 ;;;;
5 ;;;; based on the CMU CL load.lisp code, written by Skef Wholey and
6 ;;;; Rob Maclachlan
7
8 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; more information.
10 ;;;;
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.
16
17 (in-package "SB!FASL")
18 \f
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*
24
25 ;;;; miscellaneous load utilities
26
27 ;;; Output the current number of semicolons after a fresh-line.
28 ;;; FIXME: non-mnemonic name
29 (defun load-fresh-line ()
30   (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)))
38
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)
42   (when verbose
43     (load-fresh-line)
44     (let ((name #-sb-xc-host (file-name stream-we-are-loading-from)
45                 #+sb-xc-host nil))
46       (if name
47           (format t "loading ~S~%" name)
48           (format t "loading stuff from ~S~%" stream-we-are-loading-from)))))
49 \f
50 ;;;; utilities for reading from fasl files
51
52 #!-sb-fluid (declaim (inline read-byte))
53
54 ;;; This expands into code to read an N-byte unsigned integer using
55 ;;; FAST-READ-BYTE.
56 (defmacro fast-read-u-integer (n)
57   (declare (optimize (speed 0)))
58   (do ((res '(fast-read-byte)
59             `(logior (fast-read-byte)
60                      (ash ,res 8)))
61        (cnt 1 (1+ cnt)))
62       ((>= cnt n) res)))
63
64 ;;; like FAST-READ-U-INTEGER, but the size may be determined at run time
65 (defmacro fast-read-var-u-integer (n)
66   (let ((n-pos (gensym))
67         (n-res (gensym))
68         (n-cnt (gensym)))
69     `(do ((,n-pos 8 (+ ,n-pos 8))
70           (,n-cnt (1- ,n) (1- ,n-cnt))
71           (,n-res
72            (fast-read-byte)
73            (dpb (fast-read-byte) (byte 8 ,n-pos) ,n-res)))
74          ((zerop ,n-cnt) ,n-res)
75        (declare (type index ,n-pos ,n-cnt)))))
76
77 ;;; Read a signed integer.
78 (defmacro fast-read-s-integer (n)
79   (declare (optimize (speed 0)))
80   (let ((n-last (gensym)))
81     (do ((res `(let ((,n-last (fast-read-byte)))
82                  (if (zerop (logand ,n-last #x80))
83                      ,n-last
84                      (logior ,n-last #x-100)))
85               `(logior (fast-read-byte)
86                        (ash (the (signed-byte ,(* cnt 8)) ,res) 8)))
87          (cnt 1 (1+ cnt)))
88         ((>= cnt n) res))))
89
90 ;;; Read an N-byte unsigned integer from the *FASL-INPUT-STREAM*
91 (defmacro read-arg (n)
92   (declare (optimize (speed 0)))
93   (if (= n 1)
94       `(the (unsigned-byte 8) (read-byte *fasl-input-stream*))
95       `(prepare-for-fast-read-byte *fasl-input-stream*
96          (prog1
97           (fast-read-u-integer ,n)
98           (done-with-fast-read-byte)))))
99
100 ;;; FIXME: This deserves a more descriptive name, and should probably
101 ;;; be implemented as an ordinary function, not a macro.
102 ;;;
103 ;;; (for the names: There seem to be only two cases, so it could be
104 ;;; named READ-U-INTEGER-8 and READ-U-INTEGER-32 or something.)
105 \f
106 ;;;; the fop table
107
108 ;;; The table is implemented as a simple-vector indexed by the table
109 ;;; offset. We may need to have several, since LOAD can be called
110 ;;; recursively.
111
112 ;;; a list of free fop tables for the fasloader
113 ;;;
114 ;;; FIXME: Is it really a win to have this permanently bound?
115 ;;; Couldn't we just bind it on entry to LOAD-AS-FASL?
116 (defvar *free-fop-tables* (list (make-array 1000)))
117
118 ;;; the current fop table
119 (defvar *current-fop-table*)
120 (declaim (simple-vector *current-fop-table*))
121
122 ;;; the length of the current fop table
123 (defvar *current-fop-table-size*)
124 (declaim (type index *current-fop-table-size*))
125
126 ;;; the index in the fop-table of the next entry to be used
127 (defvar *current-fop-table-index*)
128 (declaim (type index *current-fop-table-index*))
129
130 (defun grow-fop-table ()
131   (let* ((new-size (* *current-fop-table-size* 2))
132          (new-table (make-array new-size)))
133     (declare (fixnum new-size) (simple-vector new-table))
134     (replace new-table (the simple-vector *current-fop-table*))
135     (setq *current-fop-table* new-table)
136     (setq *current-fop-table-size* new-size)))
137
138 (defmacro push-fop-table (thing)
139   (let ((n-index (gensym)))
140     `(let ((,n-index *current-fop-table-index*))
141        (declare (fixnum ,n-index))
142        (when (= ,n-index (the fixnum *current-fop-table-size*))
143          (grow-fop-table))
144        (setq *current-fop-table-index* (1+ ,n-index))
145        (setf (svref *current-fop-table* ,n-index) ,thing))))
146 \f
147 ;;;; the fop stack
148
149 ;;; (This is to be bound by LOAD to an adjustable (VECTOR T) with
150 ;;; FILL-POINTER, for use as a stack with VECTOR-PUSH-EXTEND.)
151 (defvar *fop-stack*)
152 (declaim (type (vector t) *fop-stack*))
153
154 ;;; Cache information about the fop stack in local variables. Define a
155 ;;; local macro to pop from the stack. Push the result of evaluation
156 ;;; if PUSHP.
157 (defmacro with-fop-stack (pushp &body forms)
158   (aver (member pushp '(nil t :nope)))
159   (with-unique-names (fop-stack)
160     `(let ((,fop-stack *fop-stack*))
161        (declare (type (vector t) ,fop-stack))
162        (macrolet ((pop-stack ()
163                     `(vector-pop ,',fop-stack))
164                   (call-with-popped-args (fun n)
165                     `(%call-with-popped-args ,fun ,n ,',fop-stack)))
166          ,(if pushp
167               `(vector-push-extend (progn ,@forms) ,fop-stack)
168               `(progn ,@forms))))))
169
170 ;;; Call FUN with N arguments popped from STACK.
171 (defmacro %call-with-popped-args (fun n stack)
172   ;; N's integer value must be known at macroexpansion time.
173   (declare (type index n))
174   (with-unique-names (n-stack old-length new-length)
175     (let ((argtmps (make-gensym-list n)))
176       `(let* ((,n-stack ,stack)
177               (,old-length (fill-pointer ,n-stack))
178               (,new-length (- ,old-length ,n))
179               ,@(loop for i from 0 below n collecting
180                       `(,(nth i argtmps)
181                         (aref ,n-stack (+ ,new-length ,i)))))
182         (declare (type (vector t) ,n-stack))
183         (setf (fill-pointer ,n-stack) ,new-length)
184         ;; (For some applications it might be appropriate to FILL the
185         ;; popped area with NIL here, to avoid holding onto garbage. For
186         ;; sbcl-0.8.7.something, though, it shouldn't matter, because
187         ;; we're using this only to pop stuff off *FOP-STACK*, and the
188         ;; entire *FOP-STACK* can be GCed as soon as LOAD returns.)
189         (,fun ,@argtmps)))))
190 \f
191 ;;;; Conditions signalled on invalid fasls (wrong fasl version, etc),
192 ;;;; so that user code (esp. ASDF) can reasonably handle attempts to
193 ;;;; load such fasls by recompiling them, etc. For simplicity's sake
194 ;;;; make only condition INVALID-FASL part of the public interface,
195 ;;;; and keep the guts internal.
196
197 (define-condition invalid-fasl (error)
198   ((stream :reader invalid-fasl-stream :initarg :stream)
199    (expected :reader invalid-fasl-expected :initarg :expected))
200   (:report
201    (lambda (condition stream)
202      (format stream "~S is an invalid fasl file."
203              (invalid-fasl-stream condition)))))
204
205 (define-condition invalid-fasl-header (invalid-fasl)
206   ((byte :reader invalid-fasl-byte :initarg :byte)
207    (byte-nr :reader invalid-fasl-byte-nr :initarg :byte-nr))
208   (:report
209    (lambda (condition stream)
210      (format stream "~@<~S contains an illegal byte in the FASL header at ~
211                      position ~A: Expected ~A, got ~A.~:@>"
212              (invalid-fasl-stream condition)
213              (invalid-fasl-byte-nr condition)
214              (invalid-fasl-byte condition)
215              (invalid-fasl-expected condition)))))
216
217 (define-condition invalid-fasl-version (invalid-fasl)
218   ((variant :reader invalid-fasl-variant :initarg :variant)
219    (version :reader invalid-fasl-version :initarg :version))
220   (:report
221    (lambda (condition stream)
222      (format stream "~@<~S is in ~A fasl file format version ~W, ~
223                     but this version of SBCL uses format version ~W.~:@>"
224              (invalid-fasl-stream condition)
225              (invalid-fasl-variant condition)
226              (invalid-fasl-version condition)
227              (invalid-fasl-expected condition)))))
228
229 (define-condition invalid-fasl-implementation (invalid-fasl)
230   ((implementation :reader invalid-fasl-implementation
231                    :initarg :implementation))
232   (:report 
233    (lambda (condition stream)
234      (format stream "~S was compiled for implementation ~A, but this is a ~A."
235              (invalid-fasl-stream condition)
236              (invalid-fasl-implementation condition)
237              (invalid-fasl-expected condition)))))
238
239 (define-condition invalid-fasl-features (invalid-fasl)
240   ((potential-features :reader invalid-fasl-potential-features
241                        :initarg :potential-features)
242    (features :reader invalid-fasl-features :initarg :features))
243   (:report
244    (lambda (condition stream)
245      (format stream "~@<incompatible ~S in fasl file ~S: ~2I~_~
246                      Of features affecting binary compatibility, ~4I~_~S~2I~_~
247                      the fasl has ~4I~_~A,~2I~_~
248                      while the runtime expects ~4I~_~A.~:>"
249              '*features* 
250              (invalid-fasl-stream condition)
251              (invalid-fasl-potential-features condition)
252              (invalid-fasl-features condition)
253              (invalid-fasl-expected condition)))))
254
255 ;;;; LOAD-AS-FASL
256 ;;;;
257 ;;;; Note: LOAD-AS-FASL is used not only by LOAD, but also (with
258 ;;;; suitable modification of the fop table) in GENESIS. Therefore,
259 ;;;; it's needed not only in the target Lisp, but also in the
260 ;;;; cross-compilation host.
261
262 ;;; a helper function for LOAD-FASL-GROUP
263 ;;;
264 ;;; Return true if we successfully read a FASL header from the stream,
265 ;;; or NIL if EOF was hit before anything was read. Signal an error if
266 ;;; we encounter garbage.
267 (defun check-fasl-header (stream)
268
269   (let ((byte (read-byte stream nil)))
270     (when byte
271
272       ;; Read and validate constant string prefix in fasl header.
273       (let* ((fhsss *fasl-header-string-start-string*)
274              (fhsss-length (length fhsss)))
275         (unless (= byte (char-code (schar fhsss 0)))
276           (error 'invalid-fasl-header
277                  :stream stream
278                  :first-byte-p t
279                  :byte byte
280                  :expected (char-code (schar fhsss 0))))
281         (do ((byte (read-byte stream) (read-byte stream))
282              (count 1 (1+ count)))
283             ((= byte +fasl-header-string-stop-char-code+)
284              t)
285           (declare (fixnum byte count))
286           (when (and (< count fhsss-length)
287                      (not (eql byte (char-code (schar fhsss count)))))
288             (error 'invalid-fasl-header
289                    :stream stream
290                    :byte-nr count
291                    :byte byte
292                    :expected (char-code (schar fhsss count))))))
293
294       ;; Read and validate version-specific compatibility stuff.
295       (flet ((string-from-stream ()
296                (let* ((length (read-arg 4))
297                       (result (make-string length)))
298                  (read-string-as-bytes stream result)
299                  result)))
300         ;; Read and validate implementation and version.
301         (let* ((implementation (keywordicate (string-from-stream)))
302                ;; FIXME: The logic above to read a keyword from the fasl file
303                ;; could probably be shared with the read-a-keyword fop.
304                (version (read-arg 4)))
305           (flet ((check-version (variant
306                                  possible-implementation
307                                  needed-version)
308                    (when (string= possible-implementation implementation)
309                      (or (= version needed-version)
310                          (error 'invalid-fasl-version
311                                 ;; :error :wrong-version
312                                 :stream stream
313                                 :variant variant
314                                 :version version
315                                 :expected needed-version)))))
316             (or (check-version "native code"
317                                +backend-fasl-file-implementation+
318                                +fasl-file-version+)
319                 (error 'invalid-fasl-implementation
320                        :stream stream
321                        :implementation implementation
322                        :expected +backend-fasl-file-implementation+))))
323         ;; Read and validate *FEATURES* which affect binary compatibility.
324         (let ((faff-in-this-file (string-from-stream)))
325           (unless (string= faff-in-this-file *features-affecting-fasl-format*)
326             (error 'invalid-fasl-features
327                    :stream stream
328                    :potential-features *features-potentially-affecting-fasl-format*
329                    :expected *features-affecting-fasl-format*
330                    :features faff-in-this-file)))
331         ;; success
332         t))))
333
334 ;; Setting this variable gives you a trace of fops as they are loaded and
335 ;; executed.
336 #!+sb-show
337 (defvar *show-fops-p* nil)
338
339 ;;; a helper function for LOAD-AS-FASL
340 ;;;
341 ;;; Return true if we successfully load a group from the stream, or
342 ;;; NIL if EOF was encountered while trying to read from the stream.
343 ;;; Dispatch to the right function for each fop. 
344 (defun load-fasl-group (stream)
345   (when (check-fasl-header stream)
346     (catch 'fasl-group-end
347       (let ((*current-fop-table-index* 0))
348         (loop
349           (let ((byte (read-byte stream)))
350
351             ;; Do some debugging output.
352             #!+sb-show
353             (when *show-fops-p*
354               (let* ((stack *fop-stack*)
355                      (ptr (1- (fill-pointer *fop-stack*))))
356                 (fresh-line *trace-output*)
357                 ;; The FOP operations are stack based, so it's sorta
358                 ;; logical to display the operand before the operator.
359                 ;; ("reverse Polish notation")
360                 (unless (= ptr -1)
361                   (write-char #\space *trace-output*)
362                   (prin1 (aref stack ptr) *trace-output*)
363                   (terpri *trace-output*))
364                 ;; Display the operator.
365                 (format *trace-output*
366                         "~&~S (#X~X at ~D) (~S)~%"
367                         (aref *fop-names* byte)
368                         byte
369                         (1- (file-position stream))
370                         (svref *fop-funs* byte))))
371
372             ;; Actually execute the fop.
373             (funcall (the function (svref *fop-funs* byte)))))))))
374
375 (defun load-as-fasl (stream verbose print)
376   ;; KLUDGE: ANSI says it's good to do something with the :PRINT
377   ;; argument to LOAD when we're fasloading a file, but currently we
378   ;; don't. (CMU CL did, but implemented it in a non-ANSI way, and I
379   ;; just disabled that instead of rewriting it.) -- WHN 20000131
380   (declare (ignore print))
381   (when (zerop (file-length stream))
382     (error "attempt to load an empty FASL file:~%  ~S" (namestring stream)))
383   (maybe-announce-load stream verbose)
384   (sb!thread:with-recursive-lock (sb!c::*big-compiler-lock*)
385     (let* ((*fasl-input-stream* stream)
386            (*current-fop-table* (or (pop *free-fop-tables*) (make-array 1000)))
387            (*current-fop-table-size* (length *current-fop-table*))
388            (*fop-stack* (make-array 100 :fill-pointer 0 :adjustable t)))
389       (unwind-protect
390            (loop while (load-fasl-group stream))
391         (push *current-fop-table* *free-fop-tables*)
392         ;; NIL out the table, so that we don't hold onto garbage.
393         ;;
394         ;; FIXME: Could we just get rid of the free fop table pool so
395         ;; that this would go away?
396         (fill *current-fop-table* nil))))
397   t)
398
399 ;;; This is used in in target-load and also genesis, using
400 ;;; *COLD-FOREIGN-SYMBOL-TABLE*. All the speculative prefix-adding
401 ;;; code for foreign symbol lookup should be here.
402 (defun find-foreign-symbol-in-table (name table)
403   (let ((prefixes
404          #!+(or osf1 sunos linux freebsd darwin) #("" "ldso_stub__")
405          #!+openbsd #("")))
406     (declare (notinline some)) ; to suppress bug 117 bogowarning
407     (some (lambda (prefix)
408             (gethash (concatenate 'string prefix name)
409                      table
410                      nil))
411           prefixes)))
412 \f
413 ;;;; stuff for debugging/tuning by collecting statistics on FOPs (?)
414
415 #|
416 (defvar *fop-counts* (make-array 256 :initial-element 0))
417 (defvar *fop-times* (make-array 256 :initial-element 0))
418 (defvar *print-fops* nil)
419
420 (defun clear-counts ()
421   (fill (the simple-vector *fop-counts*) 0)
422   (fill (the simple-vector *fop-times*) 0)
423   t)
424
425 (defun analyze-counts ()
426   (let ((counts ())
427         (total-count 0)
428         (times ())
429         (total-time 0))
430     (macrolet ((breakdown (lvar tvar vec)
431                  `(progn
432                    (dotimes (i 255)
433                      (declare (fixnum i))
434                      (let ((n (svref ,vec i)))
435                        (push (cons (svref *fop-names* i) n) ,lvar)
436                        (incf ,tvar n)))
437                    (setq ,lvar (subseq (sort ,lvar (lambda (x y)
438                                                      (> (cdr x) (cdr y))))
439                                        0 10)))))
440
441       (breakdown counts total-count *fop-counts*)
442       (breakdown times total-time *fop-times*)
443       (format t "Total fop count is ~D~%" total-count)
444       (dolist (c counts)
445         (format t "~30S: ~4D~%" (car c) (cdr c)))
446       (format t "~%Total fop time is ~D~%" (/ (float total-time) 60.0))
447       (dolist (m times)
448         (format t "~30S: ~6,2F~%" (car m) (/ (float (cdr m)) 60.0))))))
449 |#
450