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