1.0.0.18:
[sbcl.git] / src / code / fop.lisp
1 ;;;; FOP definitions
2
3 (in-package "SB!FASL")
4
5 ;;; Sometimes we want to skip over any FOPs with side-effects (like
6 ;;; function calls) while executing other FOPs. *SKIP-UNTIL* will
7 ;;; either contain the position where the skipping will stop, or
8 ;;; NIL if we're executing normally.
9 (defvar *skip-until* nil)
10
11 ;;; Define NAME as a fasl operation, with op-code FOP-CODE. PUSHP
12 ;;; describes what the body does to the fop stack:
13 ;;;   T
14 ;;;     The body might pop the fop stack. The result of the body is
15 ;;;     pushed on the fop stack.
16 ;;;   NIL
17 ;;;     The body might pop the fop stack. The result of the body is
18 ;;;     discarded.
19 ;;; STACKP describes whether or not the body interacts with the fop stack.
20 (defmacro define-fop ((name fop-code &key (pushp t) (stackp t)) &rest forms)
21   `(progn
22      (defun ,name ()
23        ,(if stackp
24             `(with-fop-stack ,pushp ,@forms)
25             `(progn ,@forms)))
26      (%define-fop ',name ,fop-code)))
27
28 (defun %define-fop (name code)
29   (let ((oname (svref *fop-names* code)))
30     (when (and oname (not (eq oname name)))
31       (error "multiple names for fop code ~D: ~S and ~S" code name oname)))
32   ;; KLUDGE: It's mnemonically suboptimal to use 'FOP-CODE as the name of the
33   ;; tag which associates names with codes when it's also used as one of
34   ;; the names. Perhaps the fops named FOP-CODE and FOP-SMALL-CODE could
35   ;; be renamed to something more mnemonic? -- WHN 19990902
36   (let ((ocode (get name 'fop-code)))
37     (when (and ocode (/= ocode code))
38       (error "multiple codes for fop name ~S: ~D and ~D" name code ocode)))
39   (setf (svref *fop-names* code) name
40         (get name 'fop-code) code
41         (svref *fop-funs* code) (symbol-function name))
42   (values))
43
44 ;;; Define a pair of fops which are identical except that one reads
45 ;;; a four-byte argument while the other reads a one-byte argument. The
46 ;;; argument can be accessed by using the CLONE-ARG macro.
47 ;;;
48 ;;; KLUDGE: It would be nice if the definition here encapsulated which
49 ;;; value ranges went with which fop variant, and chose the correct
50 ;;; fop code to use. Currently, since such logic isn't encapsulated,
51 ;;; we see callers doing stuff like
52 ;;;     (cond ((and (< num-consts #x100) (< total-length #x10000))
53 ;;;            (dump-fop 'sb!impl::fop-small-code file)
54 ;;;            (dump-byte num-consts file)
55 ;;;            (dump-integer-as-n-bytes total-length 2 file))
56 ;;;           (t
57 ;;;            (dump-fop 'sb!impl::fop-code file)
58 ;;;            (dump-word num-consts file)
59 ;;;            (dump-word total-length file))))
60 ;;; in several places. It would be cleaner if this could be replaced with
61 ;;; something like
62 ;;;     (dump-fop file fop-code num-consts total-length)
63 ;;; Some of this logic is already in DUMP-FOP*, but that still requires the
64 ;;; caller to know that it's a 1-byte-arg/4-byte-arg cloned fop pair, and to
65 ;;; know both the 1-byte-arg and the 4-byte-arg fop names. -- WHN 19990902
66 (defmacro define-cloned-fops ((name code &key (pushp t) (stackp t))
67                               (small-name small-code) &rest forms)
68   (aver (member pushp '(nil t)))
69   (aver (member stackp '(nil t)))
70   `(progn
71      (macrolet ((clone-arg () '(read-word-arg)))
72        (define-fop (,name ,code :pushp ,pushp :stackp ,stackp) ,@forms))
73      (macrolet ((clone-arg () '(read-byte-arg)))
74        (define-fop (,small-name ,small-code :pushp ,pushp :stackp stackp) ,@forms))))
75
76 ;;; a helper function for reading string values from FASL files: sort
77 ;;; of like READ-SEQUENCE specialized for files of (UNSIGNED-BYTE 8),
78 ;;; with an automatic conversion from (UNSIGNED-BYTE 8) into CHARACTER
79 ;;; for each element read
80 (declaim (ftype (function (stream simple-string &optional index) (values))
81                 read-string-as-bytes #!+sb-unicode read-string-as-words))
82 (defun read-string-as-bytes (stream string &optional (length (length string)))
83   (dotimes (i length)
84     (setf (aref string i)
85           (sb!xc:code-char (read-byte stream))))
86   ;; FIXME: The classic CMU CL code to do this was
87   ;;   (READ-N-BYTES FILE STRING START END).
88   ;; It was changed for SBCL because we needed a portable version for
89   ;; bootstrapping. Benchmark the non-portable version and see whether it's
90   ;; significantly better than the portable version here. If it is, then use
91   ;; it as an alternate definition, protected with #-SB-XC-HOST.
92   (values))
93 #!+sb-unicode
94 (defun read-string-as-words (stream string &optional (length (length string)))
95   #+sb-xc-host (bug "READ-STRING-AS-WORDS called")
96   (dotimes (i length)
97     (setf (aref string i)
98           (let ((code 0))
99             ;; FIXME: is this the same as READ-WORD-ARG?
100             (dotimes (k sb!vm:n-word-bytes (sb!xc:code-char code))
101               (setf code (logior code (ash (read-byte stream)
102                                            (* k sb!vm:n-byte-bits))))))))
103   (values))
104 \f
105 ;;;; miscellaneous fops
106
107 ;;; FIXME: POP-STACK should be called something more mnemonic. (POP-FOP-STACK?
108 ;;; But that would conflict with PUSH-FOP-TABLE. Something, anyway..)
109
110 ;;; Setting this variable causes execution of a FOP-NOP4 to produce
111 ;;; output to *DEBUG-IO*. This can be handy when trying to follow the
112 ;;; progress of FASL loading.
113 #!+sb-show
114 (defvar *show-fop-nop4-p* nil)
115
116 ;;; CMU CL had a single no-op fop, FOP-NOP, with fop code 0. Since 0
117 ;;; occurs disproportionately often in fasl files for other reasons,
118 ;;; FOP-NOP is less than ideal for writing human-readable patterns
119 ;;; into fasl files for debugging purposes. There's no shortage of
120 ;;; unused fop codes, so we add this second NOP, which reads 4
121 ;;; arbitrary bytes and discards them.
122 (define-fop (fop-nop4 137 :stackp nil)
123   (let ((arg (read-arg 4)))
124     (declare (ignorable arg))
125     #!+sb-show
126     (when *show-fop-nop4-p*
127       (format *debug-io* "~&/FOP-NOP4 ARG=~W=#X~X~%" arg arg))))
128
129 (define-fop (fop-nop 0 :stackp nil))
130 (define-fop (fop-pop 1 :pushp nil) (push-fop-table (pop-stack)))
131 (define-fop (fop-push 2) (svref *current-fop-table* (read-word-arg)))
132 (define-fop (fop-byte-push 3) (svref *current-fop-table* (read-byte-arg)))
133
134 (define-fop (fop-empty-list 4) ())
135 (define-fop (fop-truth 5) t)
136 ;;; CMU CL had FOP-POP-FOR-EFFECT as fop 65, but it was never used and seemed
137 ;;; to have no possible use.
138 (define-fop (fop-misc-trap 66)
139   #+sb-xc-host ; since xc host doesn't know how to compile %PRIMITIVE
140   (error "FOP-MISC-TRAP can't be defined without %PRIMITIVE.")
141   #-sb-xc-host
142   (%primitive sb!c:make-other-immediate-type 0 sb!vm:unbound-marker-widetag))
143
144 (define-cloned-fops (fop-character 68) (fop-short-character 69)
145   (code-char (clone-arg)))
146
147 (define-cloned-fops (fop-struct 48) (fop-small-struct 49)
148   (let* ((size (clone-arg))
149          (res (%make-instance size)))
150     (declare (type index size))
151     (let* ((layout (pop-stack))
152            (nuntagged (layout-n-untagged-slots layout))
153            (ntagged (- size nuntagged)))
154       (setf (%instance-ref res 0) layout)
155       (dotimes (n (1- ntagged))
156         (declare (type index n))
157         (setf (%instance-ref res (1+ n)) (pop-stack)))
158       (dotimes (n nuntagged)
159         (declare (type index n))
160         (setf (%raw-instance-ref/word res (- nuntagged n 1)) (pop-stack))))
161     res))
162
163 (define-fop (fop-layout 45)
164   (let ((nuntagged (pop-stack))
165         (length (pop-stack))
166         (depthoid (pop-stack))
167         (inherits (pop-stack))
168         (name (pop-stack)))
169     (find-and-init-or-check-layout name length inherits depthoid nuntagged)))
170
171 (define-fop (fop-end-group 64 :stackp nil)
172   (/show0 "THROWing FASL-GROUP-END")
173   (throw 'fasl-group-end t))
174
175 ;;; In the normal loader, we just ignore these. GENESIS overwrites
176 ;;; FOP-MAYBE-COLD-LOAD with something that knows whether to revert to
177 ;;; cold-loading or not.
178 (define-fop (fop-normal-load 81 :stackp nil))
179 (define-fop (fop-maybe-cold-load 82 :stackp nil))
180
181 (define-fop (fop-verify-table-size 62 :stackp nil)
182   (let ((expected-index (read-word-arg)))
183     (unless (= *current-fop-table-index* expected-index)
184       (bug "fasl table of improper size"))))
185 (define-fop (fop-verify-empty-stack 63 :stackp nil)
186   (unless (zerop (length *fop-stack*))
187     (bug "fasl stack not empty when it should be")))
188 \f
189 ;;;; fops for loading symbols
190
191 (macrolet (;; FIXME: Should all this code really be duplicated inside
192            ;; each fop? Perhaps it would be better for this shared
193            ;; code to live in FLET FROB1 and FLET FROB4 (for the
194            ;; two different sizes of counts).
195            (frob (name code name-size package)
196              (let ((n-package (gensym))
197                    (n-size (gensym))
198                    (n-buffer (gensym)))
199                `(define-fop (,name ,code)
200                   (prepare-for-fast-read-byte *fasl-input-stream*
201                     (let ((,n-package ,package)
202                           (,n-size (fast-read-u-integer ,name-size)))
203                       (when (> ,n-size (length *fasl-symbol-buffer*))
204                         (setq *fasl-symbol-buffer*
205                               (make-string (* ,n-size 2))))
206                       (done-with-fast-read-byte)
207                       (let ((,n-buffer *fasl-symbol-buffer*))
208                         #+sb-xc-host
209                         (read-string-as-bytes *fasl-input-stream*
210                                               ,n-buffer
211                                               ,n-size)
212                         #-sb-xc-host
213                         (#!+sb-unicode read-string-as-words
214                          #!-sb-unicode read-string-as-bytes
215                          *fasl-input-stream*
216                          ,n-buffer
217                          ,n-size)
218                         (push-fop-table (without-package-locks
219                                          (intern* ,n-buffer
220                                                   ,n-size
221                                                   ,n-package))))))))))
222
223   ;; Note: CMU CL had FOP-SYMBOL-SAVE and FOP-SMALL-SYMBOL-SAVE, but
224   ;; since they made the behavior of the fasloader depend on the
225   ;; *PACKAGE* variable, not only were they a pain to support (because
226   ;; they required various hacks to handle *PACKAGE*-manipulation
227   ;; forms) they were basically broken by design, because ANSI gives
228   ;; the user so much flexibility in manipulating *PACKAGE* at
229   ;; load-time that no reasonable hacks could possibly make things
230   ;; work right. The ones used in CMU CL certainly didn't, as shown by
231   ;; e.g.
232   ;;   (IN-PACKAGE :CL-USER)
233   ;;     (DEFVAR CL::*FOO* 'FOO-VALUE)
234   ;;     (EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
235   ;;       (SETF *PACKAGE* (FIND-PACKAGE :CL)))
236   ;; which in CMU CL 2.4.9 defines a variable CL-USER::*FOO* instead of
237   ;; defining CL::*FOO*. Therefore, we don't use those fops in SBCL.
238   ;;(frob fop-symbol-save               6 4 *package*)
239   ;;(frob fop-small-symbol-save   7 1 *package*)
240
241   (frob fop-lisp-symbol-save          75 #.sb!vm:n-word-bytes *cl-package*)
242   (frob fop-lisp-small-symbol-save    76 1 *cl-package*)
243   (frob fop-keyword-symbol-save       77 #.sb!vm:n-word-bytes *keyword-package*)
244   (frob fop-keyword-small-symbol-save 78 1 *keyword-package*)
245
246   ;; FIXME: Because we don't have FOP-SYMBOL-SAVE any more, an enormous number
247   ;; of symbols will fall through to this case, probably resulting in bloated
248   ;; fasl files. A new
249   ;; FOP-SYMBOL-IN-LAST-PACKAGE-SAVE/FOP-SMALL-SYMBOL-IN-LAST-PACKAGE-SAVE
250   ;; cloned fop pair could undo some of this bloat.
251   (frob fop-symbol-in-package-save 8 #.sb!vm:n-word-bytes
252     (svref *current-fop-table* (fast-read-u-integer #.sb!vm:n-word-bytes)))
253   (frob fop-small-symbol-in-package-save 9 1
254     (svref *current-fop-table* (fast-read-u-integer #.sb!vm:n-word-bytes)))
255   (frob fop-symbol-in-byte-package-save 10 #.sb!vm:n-word-bytes
256     (svref *current-fop-table* (fast-read-u-integer 1)))
257   (frob fop-small-symbol-in-byte-package-save 11 1
258     (svref *current-fop-table* (fast-read-u-integer 1))))
259
260 (define-cloned-fops (fop-uninterned-symbol-save 12)
261                     (fop-uninterned-small-symbol-save 13)
262   (let* ((arg (clone-arg))
263          (res (make-string arg)))
264     #!-sb-unicode
265     (read-string-as-bytes *fasl-input-stream* res)
266     #!+sb-unicode
267     (read-string-as-words *fasl-input-stream* res)
268     (push-fop-table (make-symbol res))))
269
270 (define-fop (fop-package 14)
271   (find-undeleted-package-or-lose (pop-stack)))
272 \f
273 ;;;; fops for loading numbers
274
275 ;;; Load a signed integer LENGTH bytes long from *FASL-INPUT-STREAM*.
276 (defun load-s-integer (length)
277   (declare (fixnum length))
278   ;; #+cmu (declare (optimize (inhibit-warnings 2)))
279   (do* ((index length (1- index))
280         (byte 0 (read-byte *fasl-input-stream*))
281         (result 0 (+ result (ash byte bits)))
282         (bits 0 (+ bits 8)))
283        ((= index 0)
284         (if (logbitp 7 byte)    ; look at sign bit
285             (- result (ash 1 bits))
286             result))
287     (declare (fixnum index byte bits))))
288
289 (define-cloned-fops (fop-integer 33) (fop-small-integer 34)
290   (load-s-integer (clone-arg)))
291
292 (define-fop (fop-word-integer 35)
293   (prepare-for-fast-read-byte *fasl-input-stream*
294     (prog1
295      (fast-read-s-integer #.sb!vm:n-word-bytes)
296      (done-with-fast-read-byte))))
297
298 (define-fop (fop-byte-integer 36)
299   (prepare-for-fast-read-byte *fasl-input-stream*
300     (prog1
301      (fast-read-s-integer 1)
302      (done-with-fast-read-byte))))
303
304 (define-fop (fop-ratio 70)
305   (let ((den (pop-stack)))
306     (%make-ratio (pop-stack) den)))
307
308 (define-fop (fop-complex 71)
309   (let ((im (pop-stack)))
310     (%make-complex (pop-stack) im)))
311
312 (macrolet ((fast-read-single-float ()
313              '(make-single-float (fast-read-s-integer 4)))
314            (fast-read-double-float ()
315              '(let ((lo (fast-read-u-integer 4)))
316                (make-double-float (fast-read-s-integer 4) lo))))
317   (macrolet ((define-complex-fop (name fop-code type)
318                (let ((reader (symbolicate "FAST-READ-" type)))
319                  `(define-fop (,name ,fop-code)
320                       (prepare-for-fast-read-byte *fasl-input-stream*
321                         (prog1
322                             (complex (,reader) (,reader))
323                           (done-with-fast-read-byte))))))
324              (define-float-fop (name fop-code type)
325                (let ((reader (symbolicate "FAST-READ-" type)))
326                  `(define-fop (,name ,fop-code)
327                       (prepare-for-fast-read-byte *fasl-input-stream*
328                         (prog1
329                             (,reader)
330                           (done-with-fast-read-byte)))))))
331     (define-complex-fop fop-complex-single-float 72 single-float)
332     (define-complex-fop fop-complex-double-float 73 double-float)
333     #!+long-float
334     (define-complex-fop fop-complex-long-float 67 long-float)
335     (define-float-fop fop-single-float 46 single-float)
336     (define-float-fop fop-double-float 47 double-float)
337     #!+long-float
338     (define-float-fop fop-long-float 52 long-float)))
339
340 \f
341 ;;;; loading lists
342
343 (define-fop (fop-list 15)
344   (do ((res () (cons (pop-stack) res))
345        (n (read-byte-arg) (1- n)))
346       ((zerop n) res)
347     (declare (type index n))))
348
349 (define-fop (fop-list* 16)
350   (do ((res (pop-stack) (cons (pop-stack) res))
351        (n (read-byte-arg) (1- n)))
352       ((zerop n) res)
353     (declare (type index n))))
354
355 (macrolet ((frob (name op fun n)
356              `(define-fop (,name ,op)
357                 (call-with-popped-args ,fun ,n))))
358
359   (frob fop-list-1 17 list 1)
360   (frob fop-list-2 18 list 2)
361   (frob fop-list-3 19 list 3)
362   (frob fop-list-4 20 list 4)
363   (frob fop-list-5 21 list 5)
364   (frob fop-list-6 22 list 6)
365   (frob fop-list-7 23 list 7)
366   (frob fop-list-8 24 list 8)
367
368   (frob fop-list*-1 25 list* 2)
369   (frob fop-list*-2 26 list* 3)
370   (frob fop-list*-3 27 list* 4)
371   (frob fop-list*-4 28 list* 5)
372   (frob fop-list*-5 29 list* 6)
373   (frob fop-list*-6 30 list* 7)
374   (frob fop-list*-7 31 list* 8)
375   (frob fop-list*-8 32 list* 9))
376 \f
377 ;;;; fops for loading arrays
378
379 (define-cloned-fops (fop-base-string 37) (fop-small-base-string 38)
380   (let* ((arg (clone-arg))
381          (res (make-string arg :element-type 'base-char)))
382     (read-string-as-bytes *fasl-input-stream* res)
383     res))
384
385 #!+sb-unicode
386 (progn
387   #+sb-xc-host
388   (define-cloned-fops (fop-character-string 161) (fop-small-character-string 162)
389     (bug "CHARACTER-STRING FOP encountered"))
390
391   #-sb-xc-host
392   (define-cloned-fops (fop-character-string 161) (fop-small-character-string 162)
393     (let* ((arg (clone-arg))
394            (res (make-string arg)))
395       (read-string-as-words *fasl-input-stream* res)
396       res)))
397
398 (define-cloned-fops (fop-vector 39) (fop-small-vector 40)
399   (let* ((size (clone-arg))
400          (res (make-array size)))
401     (declare (fixnum size))
402     (do ((n (1- size) (1- n)))
403         ((minusp n))
404       (setf (svref res n) (pop-stack)))
405     res))
406
407 (define-fop (fop-array 83)
408   (let* ((rank (read-word-arg))
409          (vec (pop-stack))
410          (length (length vec))
411          (res (make-array-header sb!vm:simple-array-widetag rank)))
412     (declare (simple-array vec)
413              (type (unsigned-byte #.(- sb!vm:n-word-bits sb!vm:n-widetag-bits)) rank))
414     (set-array-header res vec length nil 0
415                       (do ((i rank (1- i))
416                            (dimensions () (cons (pop-stack) dimensions)))
417                           ((zerop i) dimensions)
418                         (declare (type index i)))
419                       nil)
420     res))
421
422 (define-fop (fop-single-float-vector 84)
423   (let* ((length (read-word-arg))
424          (result (make-array length :element-type 'single-float)))
425     (read-n-bytes *fasl-input-stream* result 0 (* length 4))
426     result))
427
428 (define-fop (fop-double-float-vector 85)
429   (let* ((length (read-word-arg))
430          (result (make-array length :element-type 'double-float)))
431     (read-n-bytes *fasl-input-stream* result 0 (* length 8))
432     result))
433
434 (define-fop (fop-complex-single-float-vector 86)
435   (let* ((length (read-word-arg))
436          (result (make-array length :element-type '(complex single-float))))
437     (read-n-bytes *fasl-input-stream* result 0 (* length 8))
438     result))
439
440 (define-fop (fop-complex-double-float-vector 87)
441   (let* ((length (read-word-arg))
442          (result (make-array length :element-type '(complex double-float))))
443     (read-n-bytes *fasl-input-stream* result 0 (* length 16))
444     result))
445
446 ;;; CMU CL comment:
447 ;;;   *** NOT *** the FOP-INT-VECTOR as currently documented in rtguts.
448 ;;;   Size must be a directly supported I-vector element size, with no
449 ;;;   extra bits. This must be packed according to the local
450 ;;;   byte-ordering, allowing us to directly read the bits.
451 (define-fop (fop-int-vector 43)
452   (prepare-for-fast-read-byte *fasl-input-stream*
453     (let* ((len (fast-read-u-integer #.sb!vm:n-word-bytes))
454            (size (fast-read-byte))
455            (res (case size
456                   (0 (make-array len :element-type 'nil))
457                   (1 (make-array len :element-type 'bit))
458                   (2 (make-array len :element-type '(unsigned-byte 2)))
459                   (4 (make-array len :element-type '(unsigned-byte 4)))
460                   (7 (prog1 (make-array len :element-type '(unsigned-byte 7))
461                        (setf size 8)))
462                   (8 (make-array len :element-type '(unsigned-byte 8)))
463                   (15 (prog1 (make-array len :element-type '(unsigned-byte 15))
464                         (setf size 16)))
465                   (16 (make-array len :element-type '(unsigned-byte 16)))
466                   (31 (prog1 (make-array len :element-type '(unsigned-byte 31))
467                         (setf size 32)))
468                   (32 (make-array len :element-type '(unsigned-byte 32)))
469                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
470                   (63 (prog1 (make-array len :element-type '(unsigned-byte 63))
471                         (setf size 64)))
472                   (64 (make-array len :element-type '(unsigned-byte 64)))
473                   (t (bug "losing i-vector element size: ~S" size)))))
474       (declare (type index len))
475       (done-with-fast-read-byte)
476       (read-n-bytes *fasl-input-stream*
477                     res
478                     0
479                     (ceiling (the index (* size len)) sb!vm:n-byte-bits))
480       res)))
481
482 ;;; This is the same as FOP-INT-VECTOR, except this is for signed
483 ;;; SIMPLE-ARRAYs.
484 (define-fop (fop-signed-int-vector 50)
485   (prepare-for-fast-read-byte *fasl-input-stream*
486     (let* ((len (fast-read-u-integer #.sb!vm:n-word-bytes))
487            (size (fast-read-byte))
488            (res (case size
489                   (8 (make-array len :element-type '(signed-byte 8)))
490                   (16 (make-array len :element-type '(signed-byte 16)))
491                   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
492                   (29 (prog1 (make-array len :element-type '(unsigned-byte 29))
493                         (setf size 32)))
494                   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
495                   (30 (prog1 (make-array len :element-type '(signed-byte 30))
496                         (setf size 32)))
497                   (32 (make-array len :element-type '(signed-byte 32)))
498                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
499                   (60 (prog1 (make-array len :element-type '(unsigned-byte 60))
500                         (setf size 64)))
501                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
502                   (61 (prog1 (make-array len :element-type '(signed-byte 61))
503                         (setf size 64)))
504                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
505                   (64 (make-array len :element-type '(signed-byte 64)))
506                   (t (bug "losing si-vector element size: ~S" size)))))
507       (declare (type index len))
508       (done-with-fast-read-byte)
509       (read-n-bytes *fasl-input-stream*
510                     res
511                     0
512                     (ceiling (the index (* size len)) sb!vm:n-byte-bits))
513       res)))
514
515 (define-fop (fop-eval 53)
516   (if *skip-until*
517       (pop-stack)
518       (let ((result (eval (pop-stack))))
519         ;; FIXME: CMU CL had this code here:
520         ;;   (when *load-print*
521         ;;     (load-fresh-line)
522         ;;     (prin1 result)
523         ;;     (terpri))
524         ;; Unfortunately, this dependence on the *LOAD-PRINT* global
525         ;; variable is non-ANSI, so for now we've just punted printing in
526         ;; fasl loading.
527         result)))
528
529 (define-fop (fop-eval-for-effect 54 :pushp nil)
530   (if *skip-until*
531       (pop-stack)
532       (let ((result (eval (pop-stack))))
533         ;; FIXME: See the comment about *LOAD-PRINT* in FOP-EVAL.
534         (declare (ignore result))
535         #+nil (when *load-print*
536                 (load-fresh-line)
537                 (prin1 result)
538                 (terpri)))))
539
540 (define-fop (fop-funcall 55)
541   (let ((arg (read-byte-arg)))
542     (if *skip-until*
543         (dotimes (i (1+ arg))
544           (pop-stack))
545         (if (zerop arg)
546             (funcall (pop-stack))
547             (do ((args () (cons (pop-stack) args))
548                  (n arg (1- n)))
549                 ((zerop n) (apply (pop-stack) args))
550               (declare (type index n)))))))
551
552 (define-fop (fop-funcall-for-effect 56 :pushp nil)
553   (let ((arg (read-byte-arg)))
554     (if *skip-until*
555         (dotimes (i (1+ arg))
556           (pop-stack))
557         (if (zerop arg)
558             (funcall (pop-stack))
559             (do ((args () (cons (pop-stack) args))
560                  (n arg (1- n)))
561                 ((zerop n) (apply (pop-stack) args))
562               (declare (type index n)))))))
563 \f
564 ;;;; fops for fixing up circularities
565
566 (define-fop (fop-rplaca 200 :pushp nil)
567   (let ((obj (svref *current-fop-table* (read-word-arg)))
568         (idx (read-word-arg))
569         (val (pop-stack)))
570     (setf (car (nthcdr idx obj)) val)))
571
572 (define-fop (fop-rplacd 201 :pushp nil)
573   (let ((obj (svref *current-fop-table* (read-word-arg)))
574         (idx (read-word-arg))
575         (val (pop-stack)))
576     (setf (cdr (nthcdr idx obj)) val)))
577
578 (define-fop (fop-svset 202 :pushp nil)
579   (let* ((obi (read-word-arg))
580          (obj (svref *current-fop-table* obi))
581          (idx (read-word-arg))
582          (val (pop-stack)))
583     (if (%instancep obj)
584         (setf (%instance-ref obj idx) val)
585         (setf (svref obj idx) val))))
586
587 (define-fop (fop-structset 204 :pushp nil)
588   (setf (%instance-ref (svref *current-fop-table* (read-word-arg))
589                        (read-word-arg))
590         (pop-stack)))
591
592 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
593 ;;; to be T, even though that's what it defaults to in DEFINE-FOP.
594 (define-fop (fop-nthcdr 203)
595   (nthcdr (read-word-arg) (pop-stack)))
596 \f
597 ;;;; fops for loading functions
598
599 ;;; (In CMU CL there was a FOP-CODE-FORMAT (47) which was
600 ;;; conventionally placed at the beginning of each fasl file to test
601 ;;; for compatibility between the fasl file and the CMU CL which
602 ;;; loaded it. In SBCL, this functionality has been replaced by
603 ;;; putting the implementation and version in required fields in the
604 ;;; fasl file header.)
605
606 (define-fop (fop-code 58 :stackp nil)
607   (load-code (read-word-arg) (read-word-arg)))
608
609 (define-fop (fop-small-code 59 :stackp nil)
610   (load-code (read-byte-arg) (read-halfword-arg)))
611
612 (define-fop (fop-fdefinition 60)
613   (fdefinition-object (pop-stack) t))
614
615 (define-fop (fop-sanctify-for-execution 61)
616   (let ((component (pop-stack)))
617     (sb!vm:sanctify-for-execution component)
618     component))
619
620 (define-fop (fop-fset 74 :pushp nil)
621   ;; Ordinary, not-for-cold-load code shouldn't need to mess with this
622   ;; at all, since it's only used as part of the conspiracy between
623   ;; the cross-compiler and GENESIS to statically link FDEFINITIONs
624   ;; for cold init.
625   (warn "~@<FOP-FSET seen in ordinary load (not cold load) -- quite strange! ~
626 If you didn't do something strange to cause this, please report it as a ~
627 bug.~:@>")
628   ;; Unlike CMU CL, we don't treat this as a no-op in ordinary code.
629   ;; If the user (or, more likely, developer) is trying to reload
630   ;; compiled-for-cold-load code into a warm SBCL, we'll do a warm
631   ;; assignment. (This is partly for abstract tidiness, since the warm
632   ;; assignment is the closest analogy to what happens at cold load,
633   ;; and partly because otherwise our compiled-for-cold-load code will
634   ;; fail, since in SBCL things like compiled-for-cold-load %DEFUN
635   ;; depend more strongly than in CMU CL on FOP-FSET actually doing
636   ;; something.)
637   (let ((fn (pop-stack))
638         (name (pop-stack)))
639     (setf (fdefinition name) fn)))
640
641 ;;; Modify a slot in a CONSTANTS object.
642 (define-cloned-fops (fop-alter-code 140 :pushp nil) (fop-byte-alter-code 141)
643   (let ((value (pop-stack))
644         (code (pop-stack)))
645     (setf (code-header-ref code (clone-arg)) value)
646     (values)))
647
648 (define-fop (fop-fun-entry 142)
649   #+sb-xc-host ; since xc host doesn't know how to compile %PRIMITIVE
650   (error "FOP-FUN-ENTRY can't be defined without %PRIMITIVE.")
651   #-sb-xc-host
652   (let ((xrefs (pop-stack))
653         (type (pop-stack))
654         (arglist (pop-stack))
655         (name (pop-stack))
656         (code-object (pop-stack))
657         (offset (read-word-arg)))
658     (declare (type index offset))
659     (unless (zerop (logand offset sb!vm:lowtag-mask))
660       (bug "unaligned function object, offset = #X~X" offset))
661     (let ((fun (%primitive sb!c:compute-fun code-object offset)))
662       (setf (%simple-fun-self fun) fun)
663       (setf (%simple-fun-next fun) (%code-entry-points code-object))
664       (setf (%code-entry-points code-object) fun)
665       (setf (%simple-fun-name fun) name)
666       (setf (%simple-fun-arglist fun) arglist)
667       (setf (%simple-fun-type fun) type)
668       (setf (%simple-fun-xrefs fun) xrefs)
669       ;; FIXME: See the comment about *LOAD-PRINT* in FOP-EVAL.
670       #+nil (when *load-print*
671               (load-fresh-line)
672               (format t "~S defined~%" fun))
673       fun)))
674 \f
675 ;;;; Some Dylan FOPs used to live here. By 1 November 1998 the code
676 ;;;; was sufficiently stale that the functions it called were no
677 ;;;; longer defined, so I (William Harold Newman) deleted it.
678 ;;;;
679 ;;;; In case someone in the future is trying to make sense of FOP layout,
680 ;;;; it might be worth recording that the Dylan FOPs were
681 ;;;;    100 FOP-DYLAN-SYMBOL-SAVE
682 ;;;;    101 FOP-SMALL-DYLAN-SYMBOL-SAVE
683 ;;;;    102 FOP-DYLAN-KEYWORD-SAVE
684 ;;;;    103 FOP-SMALL-DYLAN-KEYWORD-SAVE
685 ;;;;    104 FOP-DYLAN-VARINFO-VALUE
686 \f
687 ;;;; assemblerish fops
688
689 (define-fop (fop-assembler-code 144)
690   (error "cannot load assembler code except at cold load"))
691
692 (define-fop (fop-assembler-routine 145)
693   (error "cannot load assembler code except at cold load"))
694
695 (define-fop (fop-foreign-fixup 147)
696   (let* ((kind (pop-stack))
697          (code-object (pop-stack))
698          (len (read-byte-arg))
699          (sym (make-string len :element-type 'base-char)))
700     (read-n-bytes *fasl-input-stream* sym 0 len)
701     (sb!vm:fixup-code-object code-object
702                              (read-word-arg)
703                              (foreign-symbol-address sym)
704                              kind)
705     code-object))
706
707 (define-fop (fop-assembler-fixup 148)
708   (let ((routine (pop-stack))
709         (kind (pop-stack))
710         (code-object (pop-stack)))
711     (multiple-value-bind (value found) (gethash routine *assembler-routines*)
712       (unless found
713         (error "undefined assembler routine: ~S" routine))
714       (sb!vm:fixup-code-object code-object (read-word-arg) value kind))
715     code-object))
716
717 (define-fop (fop-code-object-fixup 149)
718   (let ((kind (pop-stack))
719         (code-object (pop-stack)))
720     ;; Note: We don't have to worry about GC moving the code-object after
721     ;; the GET-LISP-OBJ-ADDRESS and before that value is deposited, because
722     ;; we can only use code-object fixups when code-objects don't move.
723     (sb!vm:fixup-code-object code-object (read-word-arg)
724                              (get-lisp-obj-address code-object) kind)
725     code-object))
726
727 #!+linkage-table
728 (define-fop (fop-foreign-dataref-fixup 150)
729   (let* ((kind (pop-stack))
730          (code-object (pop-stack))
731          (len (read-byte-arg))
732          (sym (make-string len :element-type 'base-char)))
733     (read-n-bytes *fasl-input-stream* sym 0 len)
734     (sb!vm:fixup-code-object code-object
735                              (read-word-arg)
736                              (foreign-symbol-address sym t)
737                              kind)
738     code-object))
739
740 ;;; FOPs needed for implementing an IF operator in a FASL
741
742 ;;; Skip until a FOP-MAYBE-STOP-SKIPPING with the same POSITION is
743 ;;; executed. While skipping, we execute most FOPs normally, except
744 ;;; for ones that a) funcall/eval b) start skipping. This needs to
745 ;;; be done to ensure that the fop table gets populated correctly
746 ;;; regardless of the execution path.
747 (define-fop (fop-skip 151 :pushp nil)
748   (let ((position (pop-stack)))
749     (unless *skip-until*
750       (setf *skip-until* position)))
751   (values))
752
753 ;;; As before, but only start skipping if the top of the FOP stack is NIL.
754 (define-fop (fop-skip-if-false 152 :pushp nil)
755   (let ((condition (pop-stack))
756         (position (pop-stack)))
757     (unless (or condition
758                 *skip-until*)
759       (setf *skip-until* position)))
760   (values))
761
762 ;;; If skipping, pop the top of the stack and discard it. Needed for
763 ;;; ensuring that the stack stays balanced when skipping.
764 (define-fop (fop-drop-if-skipping 153 :pushp nil)
765   (when *skip-until*
766     (pop-stack))
767   (values))
768
769 ;;; If skipping, push a dummy value on the stack. Needed for
770 ;;; ensuring that the stack stays balanced when skipping.
771 (define-fop (fop-push-nil-if-skipping 154 :pushp nil)
772   (when *skip-until*
773     (push-stack nil))
774   (values))
775
776 ;;; Stop skipping if the top of the stack matches *SKIP-UNTIL*
777 (define-fop (fop-maybe-stop-skipping 155 :pushp nil)
778   (let ((label (pop-stack)))
779     (when (eql *skip-until* label)
780       (setf *skip-until* nil)))
781   (values))