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