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