655b6e9c2ef52025058d5fee64bcbb65562cb521
[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     res))
422
423 (define-fop (fop-single-float-vector 84)
424   (let* ((length (read-word-arg))
425          (result (make-array length :element-type 'single-float)))
426     (read-n-bytes *fasl-input-stream* result 0 (* length 4))
427     result))
428
429 (define-fop (fop-double-float-vector 85)
430   (let* ((length (read-word-arg))
431          (result (make-array length :element-type 'double-float)))
432     (read-n-bytes *fasl-input-stream* result 0 (* length 8))
433     result))
434
435 (define-fop (fop-complex-single-float-vector 86)
436   (let* ((length (read-word-arg))
437          (result (make-array length :element-type '(complex single-float))))
438     (read-n-bytes *fasl-input-stream* result 0 (* length 8))
439     result))
440
441 (define-fop (fop-complex-double-float-vector 87)
442   (let* ((length (read-word-arg))
443          (result (make-array length :element-type '(complex double-float))))
444     (read-n-bytes *fasl-input-stream* result 0 (* length 16))
445     result))
446
447 ;;; CMU CL comment:
448 ;;;   *** NOT *** the FOP-INT-VECTOR as currently documented in rtguts.
449 ;;;   Size must be a directly supported I-vector element size, with no
450 ;;;   extra bits. This must be packed according to the local
451 ;;;   byte-ordering, allowing us to directly read the bits.
452 (define-fop (fop-int-vector 43)
453   (prepare-for-fast-read-byte *fasl-input-stream*
454     (let* ((len (fast-read-u-integer #.sb!vm:n-word-bytes))
455            (size (fast-read-byte))
456            (res (case size
457                   (0 (make-array len :element-type 'nil))
458                   (1 (make-array len :element-type 'bit))
459                   (2 (make-array len :element-type '(unsigned-byte 2)))
460                   (4 (make-array len :element-type '(unsigned-byte 4)))
461                   (7 (prog1 (make-array len :element-type '(unsigned-byte 7))
462                        (setf size 8)))
463                   (8 (make-array len :element-type '(unsigned-byte 8)))
464                   (15 (prog1 (make-array len :element-type '(unsigned-byte 15))
465                         (setf size 16)))
466                   (16 (make-array len :element-type '(unsigned-byte 16)))
467                   (31 (prog1 (make-array len :element-type '(unsigned-byte 31))
468                         (setf size 32)))
469                   (32 (make-array len :element-type '(unsigned-byte 32)))
470                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
471                   (63 (prog1 (make-array len :element-type '(unsigned-byte 63))
472                         (setf size 64)))
473                   (64 (make-array len :element-type '(unsigned-byte 64)))
474                   (t (bug "losing i-vector element size: ~S" size)))))
475       (declare (type index len))
476       (done-with-fast-read-byte)
477       (read-n-bytes *fasl-input-stream*
478                     res
479                     0
480                     (ceiling (the index (* size len)) sb!vm:n-byte-bits))
481       res)))
482
483 ;;; This is the same as FOP-INT-VECTOR, except this is for signed
484 ;;; SIMPLE-ARRAYs.
485 (define-fop (fop-signed-int-vector 50)
486   (prepare-for-fast-read-byte *fasl-input-stream*
487     (let* ((len (fast-read-u-integer #.sb!vm:n-word-bytes))
488            (size (fast-read-byte))
489            (res (case size
490                   (8 (make-array len :element-type '(signed-byte 8)))
491                   (16 (make-array len :element-type '(signed-byte 16)))
492                   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
493                   (29 (prog1 (make-array len :element-type '(unsigned-byte 29))
494                         (setf size 32)))
495                   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
496                   (30 (prog1 (make-array len :element-type '(signed-byte 30))
497                         (setf size 32)))
498                   (32 (make-array len :element-type '(signed-byte 32)))
499                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
500                   (60 (prog1 (make-array len :element-type '(unsigned-byte 60))
501                         (setf size 64)))
502                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
503                   (61 (prog1 (make-array len :element-type '(signed-byte 61))
504                         (setf size 64)))
505                   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
506                   (64 (make-array len :element-type '(signed-byte 64)))
507                   (t (bug "losing si-vector element size: ~S" size)))))
508       (declare (type index len))
509       (done-with-fast-read-byte)
510       (read-n-bytes *fasl-input-stream*
511                     res
512                     0
513                     (ceiling (the index (* size len)) sb!vm:n-byte-bits))
514       res)))
515
516 (define-fop (fop-eval 53)
517   (if *skip-until*
518       (pop-stack)
519       (let ((result (eval (pop-stack))))
520         ;; FIXME: CMU CL had this code here:
521         ;;   (when *load-print*
522         ;;     (load-fresh-line)
523         ;;     (prin1 result)
524         ;;     (terpri))
525         ;; Unfortunately, this dependence on the *LOAD-PRINT* global
526         ;; variable is non-ANSI, so for now we've just punted printing in
527         ;; fasl loading.
528         result)))
529
530 (define-fop (fop-eval-for-effect 54 :pushp nil)
531   (if *skip-until*
532       (pop-stack)
533       (let ((result (eval (pop-stack))))
534         ;; FIXME: See the comment about *LOAD-PRINT* in FOP-EVAL.
535         (declare (ignore result))
536         #+nil (when *load-print*
537                 (load-fresh-line)
538                 (prin1 result)
539                 (terpri)))))
540
541 (define-fop (fop-funcall 55)
542   (let ((arg (read-byte-arg)))
543     (if *skip-until*
544         (dotimes (i (1+ arg))
545           (pop-stack))
546         (if (zerop arg)
547             (funcall (pop-stack))
548             (do ((args () (cons (pop-stack) args))
549                  (n arg (1- n)))
550                 ((zerop n) (apply (pop-stack) args))
551               (declare (type index n)))))))
552
553 (define-fop (fop-funcall-for-effect 56 :pushp nil)
554   (let ((arg (read-byte-arg)))
555     (if *skip-until*
556         (dotimes (i (1+ arg))
557           (pop-stack))
558         (if (zerop arg)
559             (funcall (pop-stack))
560             (do ((args () (cons (pop-stack) args))
561                  (n arg (1- n)))
562                 ((zerop n) (apply (pop-stack) args))
563               (declare (type index n)))))))
564 \f
565 ;;;; fops for fixing up circularities
566
567 (define-fop (fop-rplaca 200 :pushp nil)
568   (let ((obj (svref *current-fop-table* (read-word-arg)))
569         (idx (read-word-arg))
570         (val (pop-stack)))
571     (setf (car (nthcdr idx obj)) val)))
572
573 (define-fop (fop-rplacd 201 :pushp nil)
574   (let ((obj (svref *current-fop-table* (read-word-arg)))
575         (idx (read-word-arg))
576         (val (pop-stack)))
577     (setf (cdr (nthcdr idx obj)) val)))
578
579 (define-fop (fop-svset 202 :pushp nil)
580   (let* ((obi (read-word-arg))
581          (obj (svref *current-fop-table* obi))
582          (idx (read-word-arg))
583          (val (pop-stack)))
584     (if (%instancep obj)
585         (setf (%instance-ref obj idx) val)
586         (setf (svref obj idx) val))))
587
588 (define-fop (fop-structset 204 :pushp nil)
589   (setf (%instance-ref (svref *current-fop-table* (read-word-arg))
590                        (read-word-arg))
591         (pop-stack)))
592
593 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
594 ;;; to be T, even though that's what it defaults to in DEFINE-FOP.
595 (define-fop (fop-nthcdr 203)
596   (nthcdr (read-word-arg) (pop-stack)))
597 \f
598 ;;;; fops for loading functions
599
600 ;;; (In CMU CL there was a FOP-CODE-FORMAT (47) which was
601 ;;; conventionally placed at the beginning of each fasl file to test
602 ;;; for compatibility between the fasl file and the CMU CL which
603 ;;; loaded it. In SBCL, this functionality has been replaced by
604 ;;; putting the implementation and version in required fields in the
605 ;;; fasl file header.)
606
607 (define-fop (fop-code 58 :stackp nil)
608   (load-code (read-word-arg) (read-word-arg)))
609
610 (define-fop (fop-small-code 59 :stackp nil)
611   (load-code (read-byte-arg) (read-halfword-arg)))
612
613 (define-fop (fop-fdefinition 60)
614   (fdefinition-object (pop-stack) t))
615
616 (define-fop (fop-sanctify-for-execution 61)
617   (let ((component (pop-stack)))
618     (sb!vm:sanctify-for-execution component)
619     component))
620
621 (define-fop (fop-fset 74 :pushp nil)
622   ;; Ordinary, not-for-cold-load code shouldn't need to mess with this
623   ;; at all, since it's only used as part of the conspiracy between
624   ;; the cross-compiler and GENESIS to statically link FDEFINITIONs
625   ;; for cold init.
626   (warn "~@<FOP-FSET seen in ordinary load (not cold load) -- quite strange! ~
627 If you didn't do something strange to cause this, please report it as a ~
628 bug.~:@>")
629   ;; Unlike CMU CL, we don't treat this as a no-op in ordinary code.
630   ;; If the user (or, more likely, developer) is trying to reload
631   ;; compiled-for-cold-load code into a warm SBCL, we'll do a warm
632   ;; assignment. (This is partly for abstract tidiness, since the warm
633   ;; assignment is the closest analogy to what happens at cold load,
634   ;; and partly because otherwise our compiled-for-cold-load code will
635   ;; fail, since in SBCL things like compiled-for-cold-load %DEFUN
636   ;; depend more strongly than in CMU CL on FOP-FSET actually doing
637   ;; something.)
638   (let ((fn (pop-stack))
639         (name (pop-stack)))
640     (setf (fdefinition name) fn)))
641
642 ;;; Modify a slot in a CONSTANTS object.
643 (define-cloned-fops (fop-alter-code 140 :pushp nil) (fop-byte-alter-code 141)
644   (let ((value (pop-stack))
645         (code (pop-stack)))
646     (setf (code-header-ref code (clone-arg)) value)
647     (values)))
648
649 (define-fop (fop-fun-entry 142)
650   #+sb-xc-host ; since xc host doesn't know how to compile %PRIMITIVE
651   (error "FOP-FUN-ENTRY can't be defined without %PRIMITIVE.")
652   #-sb-xc-host
653   (let ((xrefs (pop-stack))
654         (type (pop-stack))
655         (arglist (pop-stack))
656         (name (pop-stack))
657         (code-object (pop-stack))
658         (offset (read-word-arg)))
659     (declare (type index offset))
660     (unless (zerop (logand offset sb!vm:lowtag-mask))
661       (bug "unaligned function object, offset = #X~X" offset))
662     (let ((fun (%primitive sb!c:compute-fun code-object offset)))
663       (setf (%simple-fun-self fun) fun)
664       (setf (%simple-fun-next fun) (%code-entry-points code-object))
665       (setf (%code-entry-points code-object) fun)
666       (setf (%simple-fun-name fun) name)
667       (setf (%simple-fun-arglist fun) arglist)
668       (setf (%simple-fun-type fun) type)
669       (setf (%simple-fun-xrefs fun) xrefs)
670       ;; FIXME: See the comment about *LOAD-PRINT* in FOP-EVAL.
671       #+nil (when *load-print*
672               (load-fresh-line)
673               (format t "~S defined~%" fun))
674       fun)))
675 \f
676 ;;;; Some Dylan FOPs used to live here. By 1 November 1998 the code
677 ;;;; was sufficiently stale that the functions it called were no
678 ;;;; longer defined, so I (William Harold Newman) deleted it.
679 ;;;;
680 ;;;; In case someone in the future is trying to make sense of FOP layout,
681 ;;;; it might be worth recording that the Dylan FOPs were
682 ;;;;    100 FOP-DYLAN-SYMBOL-SAVE
683 ;;;;    101 FOP-SMALL-DYLAN-SYMBOL-SAVE
684 ;;;;    102 FOP-DYLAN-KEYWORD-SAVE
685 ;;;;    103 FOP-SMALL-DYLAN-KEYWORD-SAVE
686 ;;;;    104 FOP-DYLAN-VARINFO-VALUE
687 \f
688 ;;;; assemblerish fops
689
690 (define-fop (fop-assembler-code 144)
691   (error "cannot load assembler code except at cold load"))
692
693 (define-fop (fop-assembler-routine 145)
694   (error "cannot load assembler code except at cold load"))
695
696 (define-fop (fop-foreign-fixup 147)
697   (let* ((kind (pop-stack))
698          (code-object (pop-stack))
699          (len (read-byte-arg))
700          (sym (make-string len :element-type 'base-char)))
701     (read-n-bytes *fasl-input-stream* sym 0 len)
702     (sb!vm:fixup-code-object code-object
703                              (read-word-arg)
704                              (foreign-symbol-address sym)
705                              kind)
706     code-object))
707
708 (define-fop (fop-assembler-fixup 148)
709   (let ((routine (pop-stack))
710         (kind (pop-stack))
711         (code-object (pop-stack)))
712     (multiple-value-bind (value found) (gethash routine *assembler-routines*)
713       (unless found
714         (error "undefined assembler routine: ~S" routine))
715       (sb!vm:fixup-code-object code-object (read-word-arg) value kind))
716     code-object))
717
718 (define-fop (fop-code-object-fixup 149)
719   (let ((kind (pop-stack))
720         (code-object (pop-stack)))
721     ;; Note: We don't have to worry about GC moving the code-object after
722     ;; the GET-LISP-OBJ-ADDRESS and before that value is deposited, because
723     ;; we can only use code-object fixups when code-objects don't move.
724     (sb!vm:fixup-code-object code-object (read-word-arg)
725                              (get-lisp-obj-address code-object) kind)
726     code-object))
727
728 #!+linkage-table
729 (define-fop (fop-foreign-dataref-fixup 150)
730   (let* ((kind (pop-stack))
731          (code-object (pop-stack))
732          (len (read-byte-arg))
733          (sym (make-string len :element-type 'base-char)))
734     (read-n-bytes *fasl-input-stream* sym 0 len)
735     (sb!vm:fixup-code-object code-object
736                              (read-word-arg)
737                              (foreign-symbol-address sym t)
738                              kind)
739     code-object))
740
741 ;;; FOPs needed for implementing an IF operator in a FASL
742
743 ;;; Skip until a FOP-MAYBE-STOP-SKIPPING with the same POSITION is
744 ;;; executed. While skipping, we execute most FOPs normally, except
745 ;;; for ones that a) funcall/eval b) start skipping. This needs to
746 ;;; be done to ensure that the fop table gets populated correctly
747 ;;; regardless of the execution path.
748 (define-fop (fop-skip 151 :pushp nil)
749   (let ((position (pop-stack)))
750     (unless *skip-until*
751       (setf *skip-until* position)))
752   (values))
753
754 ;;; As before, but only start skipping if the top of the FOP stack is NIL.
755 (define-fop (fop-skip-if-false 152 :pushp nil)
756   (let ((condition (pop-stack))
757         (position (pop-stack)))
758     (unless (or condition
759                 *skip-until*)
760       (setf *skip-until* position)))
761   (values))
762
763 ;;; If skipping, pop the top of the stack and discard it. Needed for
764 ;;; ensuring that the stack stays balanced when skipping.
765 (define-fop (fop-drop-if-skipping 153 :pushp nil)
766   (when *skip-until*
767     (pop-stack))
768   (values))
769
770 ;;; If skipping, push a dummy value on the stack. Needed for
771 ;;; ensuring that the stack stays balanced when skipping.
772 (define-fop (fop-push-nil-if-skipping 154 :pushp nil)
773   (when *skip-until*
774     (push-stack nil))
775   (values))
776
777 ;;; Stop skipping if the top of the stack matches *SKIP-UNTIL*
778 (define-fop (fop-maybe-stop-skipping 155 :pushp nil)
779   (let ((label (pop-stack)))
780     (when (eql *skip-until* label)
781       (setf *skip-until* nil)))
782   (values))