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