Make AND and OR not be recursively expanded in syntax
[sbcl.git] / src / code / fop.lisp
blobf71c384ce3b7d7763dcbe975c499656f137c3789
1 ;;;; FOP definitions
3 (in-package "SB!FASL")
5 ;;; Bind STACK-VAR and PTR-VAR to the start of a subsequence of
6 ;;; the fop stack of length COUNT, then execute BODY.
7 ;;; Within the body, FOP-STACK-REF is used in lieu of SVREF
8 ;;; to elide bounds checking.
9 (defmacro with-fop-stack (((stack-var &optional stack-expr) ptr-var count)
10 &body body)
11 `(macrolet ((fop-stack-ref (i)
12 `(locally
13 #-sb-xc-host
14 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
15 (svref ,',stack-var (truly-the index ,i)))))
16 (let* (,@(when stack-expr
17 (list `(,stack-var (the simple-vector ,stack-expr))))
18 (,ptr-var (truly-the index (fop-stack-pop-n ,stack-var ,count))))
19 ,@body)))
21 ;;; Define NAME as a fasl operation, with op-code FOP-CODE.
22 ;;; PUSHP describes what the body does to the fop stack:
23 ;;; T - The result of the body is pushed on the fop stack.
24 ;;; NIL - The result of the body is discarded.
25 ;;; In either case, the body is permitted to pop the stack.
26 ;;;
27 (defmacro !define-fop (fop-code &rest stuff)
28 (multiple-value-bind (name allowp operands stack-args pushp forms)
29 (let ((allowp (if (eq (car stuff) :not-host)
30 (progn (pop stuff) (or #+sb-xc t))
31 t)))
32 (destructuring-bind ((name &optional arglist (pushp t)) . forms) stuff
33 (aver (member pushp '(nil t)))
34 (multiple-value-bind (operands stack-args)
35 (if (atom (car arglist))
36 (values nil arglist)
37 (ecase (caar arglist)
38 (:operands (values (cdar arglist) (cdr arglist)))))
39 (assert (<= (length operands) 3))
40 (values name allowp operands stack-args pushp forms))))
41 `(progn
42 (defun ,name (.fasl-input. ,@operands)
43 (declare (ignorable .fasl-input.))
44 ,@(if allowp
45 `((macrolet
46 ((fasl-input () '(truly-the fasl-input .fasl-input.))
47 (fasl-input-stream () '(%fasl-input-stream (fasl-input)))
48 (operand-stack () '(%fasl-input-stack (fasl-input)))
49 (skip-until () '(%fasl-input-skip-until (fasl-input))))
50 ,@(if (null stack-args)
51 forms
52 (with-unique-names (stack ptr)
53 `((with-fop-stack ((,stack (operand-stack))
54 ,ptr ,(length stack-args))
55 (multiple-value-bind ,stack-args
56 (values ,@(loop for i below (length stack-args)
57 collect `(fop-stack-ref (+ ,ptr ,i))))
58 ,@forms)))))))
59 `((declare (ignore ,@operands))
60 (error ,(format nil "Not-host fop invoked: ~A" name)))))
61 (!%define-fop ',name ,fop-code ,(length operands) ,(if pushp 1 0)))))
63 (defun !%define-fop (name base-opcode n-operands pushp)
64 (declare (type (mod 4) n-operands))
65 ;; If at least one non-stack operand is present, the same fop function
66 ;; appears in 4 consecutive cells in the fop table, with the low 2 bits
67 ;; of the fopcode determining the number of additional bytes to read
68 ;; for the first operand. The second and third operands are varint-encoded.
69 (let ((n-slots (if (plusp n-operands) 4 1)))
70 (unless (zerop (mod base-opcode n-slots))
71 (error "Opcode for fop ~S must be a multiple of ~D" name n-slots))
72 (loop for opcode from base-opcode below (+ base-opcode n-slots)
73 when (functionp (svref **fop-funs** opcode))
74 do (let ((oname (svref **fop-names** opcode)))
75 (when (and oname (not (eq oname name)))
76 (error "fop ~S with opcode ~D conflicts with fop ~S."
77 name opcode oname))))
78 (let ((existing-opcode (get name 'opcode)))
79 (when (and existing-opcode (/= existing-opcode base-opcode))
80 (error "multiple codes for fop name ~S: ~D and ~D"
81 name base-opcode existing-opcode)))
82 (setf (get name 'opcode) base-opcode)
83 ;; The low 2 bits of the opcode comprise the length modifier if there is
84 ;; at least one non-stack integer operand.
85 ;; If there is more than 1, they follow, using varint encoding.
86 (dotimes (j n-slots)
87 (let ((opcode (+ base-opcode j)))
88 (setf (svref **fop-names** opcode) name
89 (svref **fop-funs** opcode) (symbol-function name)
90 (aref (car **fop-signatures**) opcode) n-operands
91 (sbit (cdr **fop-signatures**) opcode) pushp))))
92 name)
94 ;;; a helper function for reading string values from FASL files: sort
95 ;;; of like READ-SEQUENCE specialized for files of (UNSIGNED-BYTE 8),
96 ;;; with an automatic conversion from (UNSIGNED-BYTE 8) into CHARACTER
97 ;;; for each element read
98 (defun read-string-as-bytes (stream string &optional (length (length string)))
99 (declare (type (simple-array character (*)) string)
100 (type index length)
101 (optimize speed))
102 (with-fast-read-byte ((unsigned-byte 8) stream)
103 (dotimes (i length)
104 (setf (aref string i)
105 (sb!xc:code-char (fast-read-byte)))))
106 string)
107 (defun read-base-string-as-bytes (stream string &optional (length (length string)))
108 (declare (type (simple-array base-char (*)) string)
109 (type index length)
110 (optimize speed))
111 (with-fast-read-byte ((unsigned-byte 8) stream)
112 (dotimes (i length)
113 (setf (aref string i)
114 (sb!xc:code-char (fast-read-byte)))))
115 string)
116 #!+(and sb-unicode (host-feature sb-xc))
117 (defun read-string-as-unsigned-byte-32
118 (stream string &optional (length (length string)))
119 (declare (type (simple-array character (*)) string)
120 (type index length)
121 (optimize speed))
122 (with-fast-read-byte ((unsigned-byte 8) stream)
123 (dotimes (i length)
124 (setf (aref string i)
125 (sb!xc:code-char (fast-read-u-integer 4)))))
126 string)
128 ;;;; miscellaneous fops
130 ;;; Setting this variable causes execution of a FOP-NOP4 to produce
131 ;;; output to *DEBUG-IO*. This can be handy when trying to follow the
132 ;;; progress of FASL loading.
133 #!+sb-show
134 (defvar *show-fop-nop4-p* nil)
136 ;;; CMU CL had a single no-op fop, FOP-NOP, with fop code 0. Since 0
137 ;;; occurs disproportionately often in fasl files for other reasons,
138 ;;; FOP-NOP is less than ideal for writing human-readable patterns
139 ;;; into fasl files for debugging purposes. There's no shortage of
140 ;;; unused fop codes, so we add this second NOP, which reads 4
141 ;;; arbitrary bytes and discards them.
142 (!define-fop 137 (fop-nop4 () nil)
143 (let ((arg (read-arg 4 (fasl-input-stream))))
144 (declare (ignorable arg))
145 #!+sb-show
146 (when *show-fop-nop4-p*
147 (format *debug-io* "~&/FOP-NOP4 ARG=~W=#X~X~%" arg arg))))
149 (!define-fop 0 (fop-nop () nil))
150 (!define-fop 1 (fop-pop (x) nil) (push-fop-table x (fasl-input)))
151 (!define-fop 2 (fop-empty-list) nil)
152 (!define-fop 3 (fop-truth) t)
153 (!define-fop 4 (fop-push ((:operands index)))
154 (ref-fop-table (fasl-input) index))
155 (!define-fop 9 (fop-move-to-table (x))
156 (push-fop-table x (fasl-input))
159 (!define-fop 66 :not-host (fop-misc-trap)
160 (%primitive sb!c:make-unbound-marker))
162 (!define-fop 76 (fop-character ((:operands char-code)))
163 (code-char char-code))
165 ;; %MAKE-INSTANCE does not exist on the host.
166 (!define-fop 48 :not-host (fop-struct ((:operands size) layout))
167 (let ((res (%make-instance size)) ; number of words excluding header
168 ;; Discount the layout from number of user-visible words.
169 (n-data-words (- size sb!vm:instance-data-start)))
170 (setf (%instance-layout res) layout)
171 (with-fop-stack ((stack (operand-stack)) ptr n-data-words)
172 (declare (type index ptr))
173 (let ((bitmap (layout-bitmap layout)))
174 ;; Values on the stack are in the same order as in the structure itself.
175 (do ((i sb!vm:instance-data-start (1+ i)))
176 ((>= i size))
177 (declare (type index i))
178 (let ((val (fop-stack-ref ptr)))
179 (if (logbitp i bitmap)
180 (setf (%instance-ref res i) val)
181 (setf (%raw-instance-ref/word res i) val))
182 (incf ptr)))))
183 res))
185 (!define-fop 45 (fop-layout (name inherits depthoid length bitmap))
186 (find-and-init-or-check-layout name length inherits depthoid bitmap))
188 ;; Allocate a CLOS object. This is used when the compiler detects that
189 ;; MAKE-LOAD-FORM returned a simple use of MAKE-LOAD-FORM-SAVING-SLOTS,
190 ;; or possibly a hand-written equivalent (however unlikely).
191 (!define-fop 68 :not-host (fop-allocate-instance (name) nil)
192 (let ((instance (allocate-instance (find-class (the symbol name)))))
193 (push-fop-table instance (fasl-input))))
195 ;; Fill in object slots as dictated by the second return value from
196 ;; MAKE-LOAD-FORM-SAVING-SLOTS.
197 ;; This wants a 'count' as the first item in the SLOT-NAMES argument
198 ;; rather than using read-arg because many calls of this might share
199 ;; the list, which must be constructed into the fop-table no matter what.
200 (!define-fop 69 :not-host (fop-set-slot-values (slot-names obj) nil)
201 (let* ((n-slots (pop slot-names))
202 (stack (operand-stack))
203 (ptr (fop-stack-pop-n stack n-slots)))
204 (dotimes (i n-slots)
205 (let ((val (svref stack (+ ptr i)))
206 (slot-name (pop slot-names)))
207 (if (eq val sb!pcl:+slot-unbound+)
208 ;; SLOT-MAKUNBOUND-USING-CLASS might do something nonstandard.
209 (slot-makunbound obj slot-name)
210 (setf (slot-value obj slot-name) val))))))
212 (!define-fop 64 (fop-end-group () nil)
213 (/show0 "THROWing FASL-GROUP-END")
214 (throw 'fasl-group-end t))
216 ;;; We used to have FOP-NORMAL-LOAD as 81 and FOP-MAYBE-COLD-LOAD as
217 ;;; 82 until GENESIS learned how to work with host symbols and
218 ;;; packages directly instead of piggybacking on the host code.
220 (!define-fop 62 (fop-verify-table-size () nil)
221 (let ((expected-index (read-word-arg (fasl-input-stream))))
222 (unless (= (svref (%fasl-input-table (fasl-input)) 0) expected-index)
223 (bug "fasl table of improper size"))))
224 (!define-fop 63 (fop-verify-empty-stack () nil)
225 (unless (fop-stack-empty-p (operand-stack))
226 (bug "fasl stack not empty when it should be")))
228 ;;;; fops for loading symbols
230 (defstruct (undefined-package (:copier nil))
231 (error nil :read-only t))
232 (declaim (freeze-type undefined-package))
234 ;; Cold load has its own implementation of all symbol fops,
235 ;; but we have to execute define-fop now to assign their numbers.
236 (labels #+sb-xc-host ()
237 #-sb-xc-host
238 ((read-symbol-name (length+flag fasl-input)
239 (let* ((namelen (ash (the fixnum length+flag) -1))
240 (base-p (logand length+flag 1))
241 (elt-type (if (eql base-p 1) 'base-char 'character))
242 (buffer (%fasl-input-name-buffer fasl-input))
243 (string (the string (svref buffer base-p))))
244 (when (< (length string) namelen) ; grow
245 (setf string (make-string namelen :element-type elt-type)
246 (svref buffer base-p) string))
247 (funcall (if (eql base-p 1)
248 'read-base-string-as-bytes
249 'read-string-as-unsigned-byte-32)
250 (%fasl-input-stream fasl-input) string namelen)
251 (values string namelen elt-type)))
252 (aux-fop-intern (length+flag package fasl-input)
253 (multiple-value-bind (name length elt-type)
254 (read-symbol-name length+flag fasl-input)
255 (if (undefined-package-p package)
256 (error 'simple-package-error
257 :format-control "Error finding package for symbol ~s:~% ~a"
258 :format-arguments
259 (list (subseq name 0 length)
260 (undefined-package-error package)))
261 (push-fop-table (without-package-locks
262 (%intern name length package elt-type))
263 fasl-input))))
264 ;; Symbol-hash is usually computed lazily and memoized into a symbol.
265 ;; Laziness slightly improves the speed of allocation.
266 ;; But when loading fasls, the time spent in the loader totally swamps
267 ;; any time savings of not precomputing symbol-hash.
268 ;; INTERN hashes everything anyway, so let's be consistent
269 ;; and precompute the hashes of uninterned symbols too.
270 (ensure-hashed (symbol)
271 (ensure-symbol-hash symbol)
272 symbol))
274 #-sb-xc-host (declare (inline ensure-hashed))
275 (!define-fop 80 :not-host (fop-lisp-symbol-save ((:operands length+flag)))
276 (aux-fop-intern length+flag *cl-package* (fasl-input)))
277 (!define-fop 84 :not-host (fop-keyword-symbol-save ((:operands length+flag)))
278 (aux-fop-intern length+flag *keyword-package* (fasl-input)))
279 (!define-fop #xF0 :not-host (fop-symbol-in-package-save ((:operands length+flag pkg-index)))
280 (aux-fop-intern length+flag (ref-fop-table (fasl-input) pkg-index) (fasl-input)))
282 (!define-fop 96 :not-host (fop-uninterned-symbol-save ((:operands length+flag)))
283 (multiple-value-bind (name len) (read-symbol-name length+flag (fasl-input))
284 (push-fop-table (ensure-hashed (make-symbol (subseq name 0 len)))
285 (fasl-input))))
287 (!define-fop 104 :not-host (fop-copy-symbol-save ((:operands table-index)))
288 (push-fop-table (ensure-hashed
289 (copy-symbol (ref-fop-table (fasl-input) table-index)))
290 (fasl-input))))
292 (!define-fop 44 (fop-package (pkg-designator))
293 (find-undeleted-package-or-lose pkg-designator))
295 (!define-fop 156 :not-host (fop-named-package-save ((:operands length)) nil)
296 (let ((package-name (make-string length)))
297 #!-sb-unicode (read-string-as-bytes (fasl-input-stream) package-name)
298 #!+sb-unicode (read-string-as-unsigned-byte-32 (fasl-input-stream) package-name)
299 (push-fop-table
300 (handler-case (find-undeleted-package-or-lose package-name)
301 (simple-package-error (c)
302 (make-undefined-package :error (princ-to-string c))))
303 (fasl-input))))
305 ;;;; fops for loading numbers
307 ;;; Load a signed integer LENGTH bytes long from FASL-INPUT-STREAM.
308 (defun load-s-integer (length fasl-input-stream)
309 (declare (fixnum length)
310 (optimize speed))
311 (with-fast-read-byte ((unsigned-byte 8) fasl-input-stream)
312 (do* ((index length (1- index))
313 (byte 0 (fast-read-byte))
314 (result 0 (+ result (ash byte bits)))
315 (bits 0 (+ bits 8)))
316 ((= index 0)
317 (if (logbitp 7 byte) ; look at sign bit
318 (- result (ash 1 bits))
319 result))
320 (declare (fixnum index byte bits)))))
322 (!define-fop 36 (fop-integer ((:operands n-bytes)))
323 (load-s-integer n-bytes (fasl-input-stream)))
325 (!define-fop 34 (fop-word-integer)
326 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
327 (fast-read-s-integer #.sb!vm:n-word-bytes)))
329 (!define-fop 35 (fop-byte-integer)
330 ;; FIXME: WITH-FAST-READ-BYTE for exactly 1 byte is not really faster/better
331 ;; than regular READ-BYTE. The expansion of READ-ARG corroborates this claim.
332 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
333 (fast-read-s-integer 1)))
335 ;; No %MAKE-RATIO on host
336 (!define-fop 70 :not-host (fop-ratio (num den)) (%make-ratio num den))
338 ;; No %MAKE-COMPLEX on host
339 (!define-fop 71 :not-host (fop-complex (realpart imagpart))
340 (%make-complex realpart imagpart))
342 (macrolet ((fast-read-single-float ()
343 '(make-single-float (fast-read-s-integer 4)))
344 (fast-read-double-float ()
345 '(let ((lo (fast-read-u-integer 4)))
346 (make-double-float (fast-read-s-integer 4) lo))))
347 (macrolet ((define-complex-fop (opcode name type)
348 (let ((reader (symbolicate "FAST-READ-" type)))
349 `(!define-fop ,opcode (,name)
350 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
351 (complex (,reader) (,reader))))))
352 (define-float-fop (opcode name type)
353 (let ((reader (symbolicate "FAST-READ-" type)))
354 `(!define-fop ,opcode (,name)
355 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
356 (,reader))))))
357 (define-complex-fop 72 fop-complex-single-float single-float)
358 (define-complex-fop 73 fop-complex-double-float double-float)
359 #!+long-float
360 (define-complex-fop 67 fop-complex-long-float long-float)
361 (define-float-fop 46 fop-single-float single-float)
362 (define-float-fop 47 fop-double-float double-float)
363 #!+long-float
364 (define-float-fop 52 fop-long-float long-float)))
366 #!+sb-simd-pack
367 (!define-fop 88 (fop-simd-pack)
368 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
369 (%make-simd-pack (fast-read-s-integer 8)
370 (fast-read-u-integer 8)
371 (fast-read-u-integer 8))))
373 ;;;; loading lists
375 (defun fop-list-from-stack (stack n)
376 ;; N is 0-255 when called from FOP-LIST,
377 ;; but it is as large as ARRAY-RANK-LIMIT in FOP-ARRAY.
378 (declare (type (unsigned-byte 16) n)
379 (optimize (speed 3)))
380 (with-fop-stack ((stack) ptr n)
381 (do* ((i (+ ptr n) (1- i))
382 (res () (cons (fop-stack-ref i) res)))
383 ((= i ptr) res)
384 (declare (type index i)))))
386 (!define-fop 33 (fop-list)
387 (fop-list-from-stack (operand-stack) (read-byte-arg (fasl-input-stream))))
388 (!define-fop 16 (fop-list*)
389 ;; N is the number of cons cells (0 is ok)
390 (let ((n (read-byte-arg (fasl-input-stream))))
391 (with-fop-stack ((stack (operand-stack)) ptr (1+ n))
392 (do* ((i (+ ptr n) (1- i))
393 (res (fop-stack-ref (+ ptr n))
394 (cons (fop-stack-ref i) res)))
395 ((= i ptr) res)
396 (declare (type index i))))))
398 (macrolet ((frob (name op fun n)
399 (let ((args (make-gensym-list n)))
400 `(!define-fop ,op (,name ,args) (,fun ,@args)))))
402 (frob fop-list-1 17 list 1)
403 (frob fop-list-2 18 list 2)
404 (frob fop-list-3 19 list 3)
405 (frob fop-list-4 20 list 4)
406 (frob fop-list-5 21 list 5)
407 (frob fop-list-6 22 list 6)
408 (frob fop-list-7 23 list 7)
409 (frob fop-list-8 24 list 8)
411 (frob fop-list*-1 25 list* 2)
412 (frob fop-list*-2 26 list* 3)
413 (frob fop-list*-3 27 list* 4)
414 (frob fop-list*-4 28 list* 5)
415 (frob fop-list*-5 29 list* 6)
416 (frob fop-list*-6 30 list* 7)
417 (frob fop-list*-7 31 list* 8)
418 (frob fop-list*-8 32 list* 9))
420 ;;;; fops for loading arrays
422 (!define-fop 100 (fop-base-string ((:operands length)))
423 (read-base-string-as-bytes (fasl-input-stream)
424 (make-string length :element-type 'base-char)))
426 ;; FIXME: can save space by UTF-8 encoding, or use 1 bit to indicate pure ASCII
427 ;; in the fasl even though the result will be a non-base string.
428 #!+sb-unicode
429 (!define-fop 160 :not-host (fop-character-string ((:operands length)))
430 (read-string-as-unsigned-byte-32 (fasl-input-stream)
431 (make-string length)))
433 (!define-fop 92 (fop-vector ((:operands size)))
434 (let ((res (make-array size))
435 (stack (operand-stack)))
436 (declare (fixnum size))
437 (unless (zerop size)
438 (let ((ptr (fop-stack-pop-n stack size)))
439 (replace res stack :start2 ptr)))
440 res))
442 ;; No MAKE-ARRAY-HEADER on host
443 (!define-fop 89 :not-host (fop-array (vec))
444 (let* ((rank (read-word-arg (fasl-input-stream)))
445 (length (length vec))
446 (res (make-array-header sb!vm:simple-array-widetag rank)))
447 (declare (simple-array vec)
448 (type (unsigned-byte #.(- sb!vm:n-word-bits sb!vm:n-widetag-bits)) rank))
449 (set-array-header res vec length nil 0
450 (fop-list-from-stack (operand-stack) rank)
451 nil t)
452 res))
454 (defglobal **saetp-bits-per-length**
455 (let ((array (make-array (1+ sb!vm:widetag-mask)
456 :element-type '(unsigned-byte 8)
457 :initial-element 255)))
458 (loop for saetp across sb!vm:*specialized-array-element-type-properties*
460 (setf (aref array (sb!vm:saetp-typecode saetp))
461 (sb!vm:saetp-n-bits saetp)))
462 array)
463 #!+sb-doc
464 "255 means bad entry.")
465 (declaim (type (simple-array (unsigned-byte 8) (#.(1+ sb!vm:widetag-mask)))
466 **saetp-bits-per-length**))
468 ;; No ALLOCATE-VECTOR on host (nor READ-N-BYTES)
469 (!define-fop 43 :not-host (fop-spec-vector)
470 (let* ((length (read-word-arg (fasl-input-stream)))
471 (widetag (read-byte-arg (fasl-input-stream)))
472 (bits-per-length (aref **saetp-bits-per-length** widetag))
473 (bits (progn (aver (< bits-per-length 255))
474 (* length bits-per-length)))
475 (bytes (ceiling bits sb!vm:n-byte-bits))
476 (words (ceiling bytes sb!vm:n-word-bytes))
477 (vector (allocate-vector widetag length words)))
478 (declare (type index length bytes words)
479 (type word bits))
480 (read-n-bytes (fasl-input-stream) vector 0 bytes)
481 vector))
483 (!define-fop 53 (fop-eval (expr)) ; This seems to be unused
484 (if (skip-until)
485 expr
486 (eval expr)))
488 (!define-fop 54 (fop-eval-for-effect (expr) nil) ; This seems to be unused
489 (unless (skip-until)
490 (eval expr))
491 nil)
493 (defun fop-funcall* (input-stream stack skipping)
494 (let ((argc (read-byte-arg input-stream)))
495 (with-fop-stack ((stack) ptr (1+ argc))
496 (unless skipping
497 (do ((i (+ ptr argc))
498 (args))
499 ((= i ptr) (apply (fop-stack-ref i) args))
500 (declare (type index i))
501 (push (fop-stack-ref i) args)
502 (decf i))))))
504 ;; FIXME: there should be a syntax to share these identical bodies
505 (!define-fop 55 (fop-funcall)
506 (fop-funcall* (fasl-input-stream) (operand-stack) (skip-until)))
507 (!define-fop 56 (fop-funcall-for-effect () nil)
508 (fop-funcall* (fasl-input-stream) (operand-stack) (skip-until)))
510 ;;;; fops for fixing up circularities
512 (!define-fop 200 (fop-rplaca (val) nil)
513 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
514 (idx (read-word-arg (fasl-input-stream))))
515 (setf (car (nthcdr idx obj)) val)))
517 (!define-fop 201 (fop-rplacd (val) nil)
518 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
519 (idx (read-word-arg (fasl-input-stream))))
520 (setf (cdr (nthcdr idx obj)) val)))
522 (!define-fop 202 (fop-svset (val) nil)
523 (let* ((obi (read-word-arg (fasl-input-stream)))
524 (obj (ref-fop-table (fasl-input) obi))
525 (idx (read-word-arg (fasl-input-stream))))
526 (if (%instancep obj) ; suspicious. should have been FOP-STRUCTSET
527 #+sb-xc (setf (%instance-ref obj idx) val)
528 #-sb-xc (bug "%instance-set?")
529 (setf (svref obj idx) val))))
531 (!define-fop 204 :not-host (fop-structset (val) nil)
532 (setf (%instance-ref (ref-fop-table (fasl-input)
533 (read-word-arg (fasl-input-stream)))
534 (read-word-arg (fasl-input-stream)))
535 val))
537 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
538 ;;; to be T, even though that's what it defaults to in DEFINE-FOP.
539 (!define-fop 203 (fop-nthcdr (obj))
540 (nthcdr (read-word-arg (fasl-input-stream)) obj))
542 ;;;; fops for loading functions
544 ;;; (In CMU CL there was a FOP-CODE-FORMAT (47) which was
545 ;;; conventionally placed at the beginning of each fasl file to test
546 ;;; for compatibility between the fasl file and the CMU CL which
547 ;;; loaded it. In SBCL, this functionality has been replaced by
548 ;;; putting the implementation and version in required fields in the
549 ;;; fasl file header.)
551 ;; Cold-load calls COLD-LOAD-CODE instead
552 (!define-fop #xE0 :not-host (fop-code ((:operands n-code-bytes n-boxed-words nfuns)))
553 ;; add 1 word for the toplevel-p flag and one for the debug-info
554 (with-fop-stack ((stack (operand-stack)) ptr (+ n-boxed-words 2))
555 (load-code nfuns n-boxed-words n-code-bytes stack ptr (fasl-input))))
557 ;; this gets you an #<fdefn> object, not the result of (FDEFINITION x)
558 ;; cold-loader uses COLD-FDEFINITION-OBJECT instead.
559 (!define-fop 60 :not-host (fop-fdefn (name))
560 (awhen (deprecated-thing-p 'function name) ; returns the stage of deprecation
561 (pushnew (list* it name :function)
562 (%fasl-input-deprecated-stuff (fasl-input)) :test 'equal))
563 (find-or-create-fdefn name))
565 (!define-fop 65 :not-host (fop-known-fun (name))
566 (%coerce-name-to-fun name))
568 #!-(or x86 x86-64)
569 (!define-fop 61 :not-host (fop-sanctify-for-execution (component))
570 (sb!vm:sanctify-for-execution component)
571 component)
573 (!define-fop 174 (fop-note-debug-source (debug-source) nil)
574 (warn "~@<FOP-NOTE-DEBUG-SOURCE seen in ordinary load (not cold load) -- ~
575 very strange! If you didn't do something to cause this, please report it as ~
576 a bug.~@:>")
577 ;; we are going to be lenient with coming across this fop in a warm SBCL.
578 (setf (sb!c::debug-source-compiled debug-source) (get-universal-time)
579 (sb!c::debug-source-created debug-source)
580 (file-write-date (sb!c::debug-source-namestring debug-source))))
582 ;;; Modify a slot in a CONSTANTS object.
583 (!define-fop 140 :not-host (fop-alter-code ((:operands index) code value) nil)
584 (setf (code-header-ref code index) value)
585 (values))
587 (!define-fop #xFC :not-host (fop-fun-entry ((:operands fun-index)
588 code-object name arglist type info))
589 (let ((fun (%code-entry-point code-object fun-index)))
590 (setf (%simple-fun-name fun) name)
591 (setf (%simple-fun-arglist fun) arglist)
592 (setf (%simple-fun-type fun) type)
593 (setf (%simple-fun-info fun) info)
594 fun))
596 ;;;; Some Dylan FOPs used to live here. By 1 November 1998 the code
597 ;;;; was sufficiently stale that the functions it called were no
598 ;;;; longer defined, so I (William Harold Newman) deleted it.
599 ;;;;
600 ;;;; In case someone in the future is trying to make sense of FOP layout,
601 ;;;; it might be worth recording that the Dylan FOPs were
602 ;;;; 100 FOP-DYLAN-SYMBOL-SAVE
603 ;;;; 101 FOP-SMALL-DYLAN-SYMBOL-SAVE
604 ;;;; 102 FOP-DYLAN-KEYWORD-SAVE
605 ;;;; 103 FOP-SMALL-DYLAN-KEYWORD-SAVE
606 ;;;; 104 FOP-DYLAN-VARINFO-VALUE
608 ;;;; assemblerish fops
610 (!define-fop 144 (fop-assembler-code)
611 (error "cannot load assembler code except at cold load"))
613 (!define-fop 145 (fop-assembler-routine)
614 (error "cannot load assembler code except at cold load"))
616 (!define-fop 146 :not-host (fop-symbol-tls-fixup (code-object kind symbol))
617 (sb!vm:fixup-code-object code-object
618 (read-word-arg (fasl-input-stream))
619 (ensure-symbol-tls-index symbol)
620 kind)
621 code-object)
623 #!+immobile-code
624 (!define-fop 135 :not-host (fop-static-call-fixup (code-object kind name))
625 (sb!vm:fixup-code-object code-object
626 (read-word-arg (fasl-input-stream))
627 (sb!vm::function-raw-address name)
628 kind)
629 code-object)
631 (!define-fop 147 :not-host (fop-foreign-fixup (code-object kind))
632 (let* ((len (read-byte-arg (fasl-input-stream)))
633 (sym (make-string len :element-type 'base-char)))
634 (read-n-bytes (fasl-input-stream) sym 0 len)
635 (sb!vm:fixup-code-object code-object
636 (read-word-arg (fasl-input-stream))
637 (foreign-symbol-address sym)
638 kind)
639 code-object))
641 (!define-fop 148 :not-host (fop-assembler-fixup (code-object kind routine))
642 (multiple-value-bind (value found) (gethash routine *assembler-routines*)
643 (unless found
644 (error "undefined assembler routine: ~S" routine))
645 (sb!vm:fixup-code-object code-object (read-word-arg (fasl-input-stream))
646 value kind))
647 code-object)
649 (!define-fop 149 :not-host (fop-code-object-fixup (code-object kind))
650 ;; Note: We don't have to worry about GC moving the code-object after
651 ;; the GET-LISP-OBJ-ADDRESS and before that value is deposited, because
652 ;; we can only use code-object fixups when code-objects don't move.
653 (sb!vm:fixup-code-object code-object (read-word-arg (fasl-input-stream))
654 (get-lisp-obj-address code-object) kind)
655 code-object)
657 #!+linkage-table
658 (!define-fop 150 :not-host (fop-foreign-dataref-fixup (code-object kind))
659 (let* ((len (read-byte-arg (fasl-input-stream)))
660 (sym (make-string len :element-type 'base-char)))
661 (read-n-bytes (fasl-input-stream) sym 0 len)
662 (sb!vm:fixup-code-object code-object
663 (read-word-arg (fasl-input-stream))
664 (foreign-symbol-address sym t)
665 kind)
666 code-object))
668 ;;; FOPs needed for implementing an IF operator in a FASL
670 ;;; Skip until a FOP-MAYBE-STOP-SKIPPING with the same POSITION is
671 ;;; executed. While skipping, we execute most FOPs normally, except
672 ;;; for ones that a) funcall/eval b) start skipping. This needs to
673 ;;; be done to ensure that the fop table gets populated correctly
674 ;;; regardless of the execution path.
675 (!define-fop 151 (fop-skip (position) nil)
676 (unless (skip-until)
677 (setf (skip-until) position))
678 (values))
680 ;;; As before, but only start skipping if the top of the FOP stack is NIL.
681 (!define-fop 152 (fop-skip-if-false (position condition) nil)
682 (unless (or condition (skip-until))
683 (setf (skip-until) position))
684 (values))
686 ;;; If skipping, pop the top of the stack and discard it. Needed for
687 ;;; ensuring that the stack stays balanced when skipping.
688 (!define-fop 153 (fop-drop-if-skipping () nil)
689 (when (skip-until)
690 (fop-stack-pop-n (operand-stack) 1))
691 (values))
693 ;;; If skipping, push a dummy value on the stack. Needed for
694 ;;; ensuring that the stack stays balanced when skipping.
695 (!define-fop 154 (fop-push-nil-if-skipping () nil)
696 (when (skip-until)
697 (push-fop-stack nil (fasl-input)))
698 (values))
700 ;;; Stop skipping if the top of the stack matches SKIP-UNTIL
701 (!define-fop 155 (fop-maybe-stop-skipping (label) nil)
702 (when (eql (skip-until) label)
703 (setf (skip-until) nil))
704 (values))
706 ;;; Primordial layouts.
707 (macrolet ((frob (&rest specs)
708 `(progn
709 (defun known-layout-fop (name)
710 (case name
711 ,@(mapcar (lambda (spec) `((,(cadr spec)) ,(car spec)))
712 specs)))
713 ,@(mapcar (lambda (spec)
714 `(!define-fop ,(car spec)
715 (,(symbolicate "FOP-LAYOUT-OF-"
716 (cadr spec)))
717 (find-layout ',(cadr spec))))
718 specs))))
719 (frob (#x6c t)
720 (#x6d structure-object)
721 (#x6f definition-source-location)
722 (#x70 sb!c::debug-fun)
723 (#x71 sb!c::compiled-debug-fun)
724 (#x72 sb!c::debug-info)
725 (#x73 sb!c::compiled-debug-info)
726 (#x74 sb!c::debug-source)
727 (#x75 defstruct-description)
728 (#x76 defstruct-slot-description)))