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