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