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