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