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