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