run-program: support I/O redirection to binary streams on win32.
[sbcl.git] / src / code / fop.lisp
blob36a24f863c23bcaa3451683dd1991e2c6edec947
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 `(let* (,@(when stack-expr
12 (list `(,stack-var (the simple-vector ,stack-expr))))
13 (,ptr-var (truly-the index (fop-stack-pop-n ,stack-var ,count))))
14 (macrolet ((fop-stack-ref (i)
15 `(locally
16 #-sb-xc-host
17 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
18 (svref ,',stack-var (truly-the index ,i)))))
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) 2))
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 3) n-operands)) ; 0, 1, or 2 are allowed
65 (let ((n-slots (expt 4 n-operands)))
66 (unless (zerop (mod base-opcode n-slots))
67 (error "Opcode for fop ~S must be a multiple of ~D" name n-slots))
68 (loop for opcode from base-opcode below (+ base-opcode n-slots)
69 when (functionp (svref **fop-funs** opcode))
70 do (let ((oname (svref **fop-names** opcode)))
71 (when (and oname (not (eq oname name)))
72 (error "fop ~S with opcode ~D conflicts with fop ~S."
73 name opcode oname))))
74 (let ((existing-opcode (get name 'opcode)))
75 (when (and existing-opcode (/= existing-opcode base-opcode))
76 (error "multiple codes for fop name ~S: ~D and ~D"
77 name base-opcode existing-opcode)))
78 (setf (get name 'opcode) base-opcode)
79 ;; The low 2 bits of the opcode comprise the length modifier if there is
80 ;; exactly one operand. Such opcodes are aligned in blocks of 4.
81 ;; 2-operand fops occupy 16 slots in a reserved range of the function table.
82 (dotimes (j n-slots)
83 (let ((opcode (+ base-opcode j)))
84 (setf (svref **fop-names** opcode) name
85 (svref **fop-funs** opcode) (symbol-function name)
86 (sbit (car **fop-signatures**) (ash opcode -2)) (signum n-operands)
87 (sbit (cdr **fop-signatures**) opcode) pushp))))
88 name)
90 ;;; a helper function for reading string values from FASL files: sort
91 ;;; of like READ-SEQUENCE specialized for files of (UNSIGNED-BYTE 8),
92 ;;; with an automatic conversion from (UNSIGNED-BYTE 8) into CHARACTER
93 ;;; for each element read
94 (defun read-string-as-bytes (stream string &optional (length (length string)))
95 (declare (type (simple-array character (*)) string)
96 (type index length)
97 (optimize speed))
98 (with-fast-read-byte ((unsigned-byte 8) stream)
99 (dotimes (i length)
100 (setf (aref string i)
101 (sb!xc:code-char (fast-read-byte)))))
102 string)
103 (defun read-base-string-as-bytes (stream string &optional (length (length string)))
104 (declare (type (simple-array base-char (*)) string)
105 (type index length)
106 (optimize speed))
107 (with-fast-read-byte ((unsigned-byte 8) stream)
108 (dotimes (i length)
109 (setf (aref string i)
110 (sb!xc:code-char (fast-read-byte)))))
111 string)
112 #!+(and sb-unicode (host-feature sb-xc))
113 (defun read-string-as-unsigned-byte-32
114 (stream string &optional (length (length string)))
115 (declare (type (simple-array character (*)) string)
116 (type index length)
117 (optimize speed))
118 (with-fast-read-byte ((unsigned-byte 8) stream)
119 (dotimes (i length)
120 (setf (aref string i)
121 (sb!xc:code-char (fast-read-u-integer 4)))))
122 string)
124 ;;;; miscellaneous fops
126 ;;; Setting this variable causes execution of a FOP-NOP4 to produce
127 ;;; output to *DEBUG-IO*. This can be handy when trying to follow the
128 ;;; progress of FASL loading.
129 #!+sb-show
130 (defvar *show-fop-nop4-p* nil)
132 ;;; CMU CL had a single no-op fop, FOP-NOP, with fop code 0. Since 0
133 ;;; occurs disproportionately often in fasl files for other reasons,
134 ;;; FOP-NOP is less than ideal for writing human-readable patterns
135 ;;; into fasl files for debugging purposes. There's no shortage of
136 ;;; unused fop codes, so we add this second NOP, which reads 4
137 ;;; arbitrary bytes and discards them.
138 (!define-fop 137 (fop-nop4 () nil)
139 (let ((arg (read-arg 4 (fasl-input-stream))))
140 (declare (ignorable arg))
141 #!+sb-show
142 (when *show-fop-nop4-p*
143 (format *debug-io* "~&/FOP-NOP4 ARG=~W=#X~X~%" arg arg))))
145 (!define-fop 0 (fop-nop () nil))
146 (!define-fop 1 (fop-pop (x) nil) (push-fop-table x (fasl-input)))
147 (!define-fop 2 (fop-empty-list) nil)
148 (!define-fop 3 (fop-truth) t)
149 (!define-fop 4 (fop-push ((:operands index)))
150 (ref-fop-table (fasl-input) index))
151 (!define-fop 9 (fop-move-to-table (x))
152 (push-fop-table x (fasl-input))
155 (!define-fop 66 :not-host (fop-misc-trap)
156 (%primitive sb!c:make-unbound-marker))
158 (!define-fop 76 (fop-character ((:operands char-code)))
159 (code-char char-code))
161 ;; %MAKE-INSTANCE does not exist on the host.
162 (!define-fop 48 :not-host (fop-struct ((:operands size) layout))
163 (let* ((res (%make-instance size)) ; number of words excluding header
164 ;; Compute count of elements to pop from stack, sans layout.
165 ;; If instance-data-start is 0, then size is the count,
166 ;; otherwise subtract 1 because the layout consumes a slot.
167 (n-data-words (- size sb!vm:instance-data-start)))
168 (declare (type index size))
169 (with-fop-stack ((stack (operand-stack)) ptr n-data-words)
170 (let ((ptr (+ ptr n-data-words)))
171 (declare (type index ptr))
172 (setf (%instance-layout res) layout)
173 (let ((metadata (layout-untagged-bitmap layout)))
174 (do ((i sb!vm:instance-data-start (1+ i)))
175 ((>= i size))
176 (declare (type index i))
177 (let ((val (fop-stack-ref (decf ptr))))
178 (if (logbitp i metadata)
179 (setf (%raw-instance-ref/word res i) val)
180 (setf (%instance-ref res i) val)))))))
181 res))
183 (!define-fop 45 (fop-layout (name inherits depthoid length metadata))
184 (find-and-init-or-check-layout name length inherits depthoid metadata))
186 ;; Allocate a CLOS object. This is used when the compiler detects that
187 ;; MAKE-LOAD-FORM returned a simple use of MAKE-LOAD-FORM-SAVING-SLOTS,
188 ;; or possibly a hand-written equivalent (however unlikely).
189 (!define-fop 68 :not-host (fop-allocate-instance (name) nil)
190 (let ((instance (allocate-instance (find-class (the symbol name)))))
191 (push-fop-table instance (fasl-input))))
193 ;; Fill in object slots as dictated by the second return value from
194 ;; MAKE-LOAD-FORM-SAVING-SLOTS.
195 ;; This wants a 'count' as the first item in the SLOT-NAMES argument
196 ;; rather than using read-arg because many calls of this might share
197 ;; the list, which must be constructed into the fop-table no matter what.
198 (!define-fop 69 :not-host (fop-initialize-instance (slot-names obj) nil)
199 (let* ((n-slots (pop slot-names))
200 (stack (operand-stack))
201 (ptr (fop-stack-pop-n stack n-slots)))
202 (dotimes (i n-slots)
203 (let ((val (svref stack (+ ptr i)))
204 (slot-name (pop slot-names)))
205 (if (eq val 'sb!pcl::..slot-unbound..)
206 ;; SLOT-MAKUNBOUND-USING-CLASS might do something nonstandard.
207 (slot-makunbound obj slot-name)
208 (setf (slot-value obj slot-name) val))))))
210 (!define-fop 64 (fop-end-group () nil)
211 (/show0 "THROWing FASL-GROUP-END")
212 (throw 'fasl-group-end t))
214 ;;; We used to have FOP-NORMAL-LOAD as 81 and FOP-MAYBE-COLD-LOAD as
215 ;;; 82 until GENESIS learned how to work with host symbols and
216 ;;; packages directly instead of piggybacking on the host code.
218 (!define-fop 62 (fop-verify-table-size () nil)
219 (let ((expected-index (read-word-arg (fasl-input-stream))))
220 (unless (= (svref (%fasl-input-table (fasl-input)) 0) expected-index)
221 (bug "fasl table of improper size"))))
222 (!define-fop 63 (fop-verify-empty-stack () nil)
223 (unless (fop-stack-empty-p (operand-stack))
224 (bug "fasl stack not empty when it should be")))
226 ;;;; fops for loading symbols
228 (defstruct (undefined-package (:copier nil))
229 (error nil :read-only t))
230 (declaim (freeze-type undefined-package))
232 ;; cold loader has its own implementation of this and all symbol fops.
233 #-sb-xc-host
234 (defun aux-fop-intern (size package fasl-input)
235 (declare (optimize speed))
236 (let ((input-stream (%fasl-input-stream fasl-input))
237 (buffer (make-string size)))
238 #!+sb-unicode (read-string-as-unsigned-byte-32 input-stream buffer size)
239 #!-sb-unicode (read-string-as-bytes input-stream buffer size)
240 (if (undefined-package-p package)
241 (error 'simple-package-error
242 :format-control "Error finding package for symbol ~s:~% ~a"
243 :format-arguments
244 (list (subseq buffer 0 size)
245 (undefined-package-error package)))
246 (push-fop-table (without-package-locks
247 (%intern buffer size package nil))
248 fasl-input))))
250 (!define-fop 80 :not-host (fop-lisp-symbol-save ((:operands namelen)))
251 (aux-fop-intern namelen *cl-package* (fasl-input)))
252 (!define-fop 84 :not-host (fop-keyword-symbol-save ((:operands namelen)))
253 (aux-fop-intern namelen *keyword-package* (fasl-input)))
255 ;; But srsly? Most of the space is wasted by UCS4 encoding of ASCII.
256 ;; An extra word per symbol for the package is nothing by comparison.
257 ;; FIXME: Because we don't have FOP-SYMBOL-SAVE any more, an
258 ;; enormous number of symbols will fall through to this case,
259 ;; probably resulting in bloated fasl files. A new
260 ;; FOP-SYMBOL-IN-LAST-PACKAGE-SAVE/FOP-SMALL-SYMBOL-IN-LAST-PACKAGE-SAVE
261 ;; cloned fop pair could undo some of this bloat.
262 (!define-fop #xF0 :not-host (fop-symbol-in-package-save ((:operands pkg-index namelen)))
263 (aux-fop-intern namelen (ref-fop-table (fasl-input) pkg-index) (fasl-input)))
265 ;;; Symbol-hash is usually computed lazily and memoized into a symbol.
266 ;;; Laziness slightly improves the speed of allocation.
267 ;;; But when loading fasls, the time spent in the loader totally swamps
268 ;;; any time savings of not precomputing symbol-hash.
269 ;;; INTERN hashes everything anyway, so let's be consistent
270 ;;; and precompute the hashes of uninterned symbols too.
271 (macrolet ((ensure-hashed (symbol-form)
272 `(let ((symbol ,symbol-form))
273 (ensure-symbol-hash symbol)
274 symbol)))
275 (!define-fop 96 :not-host (fop-uninterned-symbol-save ((:operands namelen)))
276 (let ((res (make-string namelen)))
277 #!-sb-unicode (read-string-as-bytes (fasl-input-stream) res)
278 #!+sb-unicode (read-string-as-unsigned-byte-32 (fasl-input-stream) res)
279 (push-fop-table (ensure-hashed (make-symbol res))
280 (fasl-input))))
282 (!define-fop 104 :not-host (fop-copy-symbol-save ((:operands table-index)))
283 (push-fop-table (ensure-hashed
284 (copy-symbol (ref-fop-table (fasl-input) table-index)))
285 (fasl-input))))
287 (!define-fop 44 (fop-package (pkg-designator))
288 (find-undeleted-package-or-lose pkg-designator))
290 (!define-fop 156 :not-host (fop-named-package-save ((:operands length)) nil)
291 (let ((package-name (make-string length)))
292 #!-sb-unicode (read-string-as-bytes (fasl-input-stream) package-name)
293 #!+sb-unicode (read-string-as-unsigned-byte-32 (fasl-input-stream) package-name)
294 (push-fop-table
295 (handler-case (find-undeleted-package-or-lose package-name)
296 (simple-package-error (c)
297 (make-undefined-package :error (princ-to-string c))))
298 (fasl-input))))
300 ;;;; fops for loading numbers
302 ;;; Load a signed integer LENGTH bytes long from FASL-INPUT-STREAM.
303 (defun load-s-integer (length fasl-input-stream)
304 (declare (fixnum length)
305 (optimize speed))
306 (with-fast-read-byte ((unsigned-byte 8) fasl-input-stream)
307 (do* ((index length (1- index))
308 (byte 0 (fast-read-byte))
309 (result 0 (+ result (ash byte bits)))
310 (bits 0 (+ bits 8)))
311 ((= index 0)
312 (if (logbitp 7 byte) ; look at sign bit
313 (- result (ash 1 bits))
314 result))
315 (declare (fixnum index byte bits)))))
317 (!define-fop 36 (fop-integer ((:operands n-bytes)))
318 (load-s-integer n-bytes (fasl-input-stream)))
320 (!define-fop 34 (fop-word-integer)
321 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
322 (fast-read-s-integer #.sb!vm:n-word-bytes)))
324 (!define-fop 35 (fop-byte-integer)
325 ;; FIXME: WITH-FAST-READ-BYTE for exactly 1 byte is not really faster/better
326 ;; than regular READ-BYTE. The expansion of READ-ARG corroborates this claim.
327 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
328 (fast-read-s-integer 1)))
330 ;; No %MAKE-RATIO on host
331 (!define-fop 70 :not-host (fop-ratio (num den)) (%make-ratio num den))
333 ;; No %MAKE-COMPLEX on host
334 (!define-fop 71 :not-host (fop-complex (realpart imagpart))
335 (%make-complex realpart imagpart))
337 (macrolet ((fast-read-single-float ()
338 '(make-single-float (fast-read-s-integer 4)))
339 (fast-read-double-float ()
340 '(let ((lo (fast-read-u-integer 4)))
341 (make-double-float (fast-read-s-integer 4) lo))))
342 (macrolet ((define-complex-fop (opcode name type)
343 (let ((reader (symbolicate "FAST-READ-" type)))
344 `(!define-fop ,opcode (,name)
345 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
346 (complex (,reader) (,reader))))))
347 (define-float-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 (,reader))))))
352 (define-complex-fop 72 fop-complex-single-float single-float)
353 (define-complex-fop 73 fop-complex-double-float double-float)
354 #!+long-float
355 (define-complex-fop 67 fop-complex-long-float long-float)
356 (define-float-fop 46 fop-single-float single-float)
357 (define-float-fop 47 fop-double-float double-float)
358 #!+long-float
359 (define-float-fop 52 fop-long-float long-float)))
361 #!+sb-simd-pack
362 (!define-fop 88 (fop-simd-pack)
363 (with-fast-read-byte ((unsigned-byte 8) (fasl-input-stream))
364 (%make-simd-pack (fast-read-s-integer 8)
365 (fast-read-u-integer 8)
366 (fast-read-u-integer 8))))
368 ;;;; loading lists
370 (defun fop-list-from-stack (stack n)
371 ;; N is 0-255 when called from FOP-LIST,
372 ;; but it is as large as ARRAY-RANK-LIMIT in FOP-ARRAY.
373 (declare (type (unsigned-byte 16) n)
374 (optimize (speed 3)))
375 (with-fop-stack ((stack) ptr n)
376 (do* ((i (+ ptr n) (1- i))
377 (res () (cons (fop-stack-ref i) res)))
378 ((= i ptr) res)
379 (declare (type index i)))))
381 (!define-fop 33 (fop-list)
382 (fop-list-from-stack (operand-stack) (read-byte-arg (fasl-input-stream))))
383 (!define-fop 16 (fop-list*)
384 ;; N is the number of cons cells (0 is ok)
385 (let ((n (read-byte-arg (fasl-input-stream))))
386 (with-fop-stack ((stack (operand-stack)) ptr (1+ n))
387 (do* ((i (+ ptr n) (1- i))
388 (res (fop-stack-ref (+ ptr n))
389 (cons (fop-stack-ref i) res)))
390 ((= i ptr) res)
391 (declare (type index i))))))
393 (macrolet ((frob (name op fun n)
394 (let ((args (make-gensym-list n)))
395 `(!define-fop ,op (,name ,args) (,fun ,@args)))))
397 (frob fop-list-1 17 list 1)
398 (frob fop-list-2 18 list 2)
399 (frob fop-list-3 19 list 3)
400 (frob fop-list-4 20 list 4)
401 (frob fop-list-5 21 list 5)
402 (frob fop-list-6 22 list 6)
403 (frob fop-list-7 23 list 7)
404 (frob fop-list-8 24 list 8)
406 (frob fop-list*-1 25 list* 2)
407 (frob fop-list*-2 26 list* 3)
408 (frob fop-list*-3 27 list* 4)
409 (frob fop-list*-4 28 list* 5)
410 (frob fop-list*-5 29 list* 6)
411 (frob fop-list*-6 30 list* 7)
412 (frob fop-list*-7 31 list* 8)
413 (frob fop-list*-8 32 list* 9))
415 ;;;; fops for loading arrays
417 (!define-fop 100 (fop-base-string ((:operands length)))
418 (read-base-string-as-bytes (fasl-input-stream)
419 (make-string length :element-type 'base-char)))
421 ;; FIXME: can save space by UTF-8 encoding, or use 1 bit to indicate pure ASCII
422 ;; in the fasl even though the result will be a non-base string.
423 #!+sb-unicode
424 (!define-fop 160 :not-host (fop-character-string ((:operands length)))
425 (read-string-as-unsigned-byte-32 (fasl-input-stream)
426 (make-string length)))
428 (!define-fop 92 (fop-vector ((:operands size)))
429 (let ((res (make-array size))
430 (stack (operand-stack)))
431 (declare (fixnum size))
432 (unless (zerop size)
433 (let ((ptr (fop-stack-pop-n stack size)))
434 (replace res stack :start2 ptr)))
435 res))
437 ;; No MAKE-ARRAY-HEADER on host
438 (!define-fop 89 :not-host (fop-array (vec))
439 (let* ((rank (read-word-arg (fasl-input-stream)))
440 (length (length vec))
441 (res (make-array-header sb!vm:simple-array-widetag rank)))
442 (declare (simple-array vec)
443 (type (unsigned-byte #.(- sb!vm:n-word-bits sb!vm:n-widetag-bits)) rank))
444 (set-array-header res vec length nil 0
445 (fop-list-from-stack (operand-stack) rank)
446 nil t)
447 res))
449 (defglobal **saetp-bits-per-length**
450 (let ((array (make-array (1+ sb!vm:widetag-mask)
451 :element-type '(unsigned-byte 8)
452 :initial-element 255)))
453 (loop for saetp across sb!vm:*specialized-array-element-type-properties*
455 (setf (aref array (sb!vm:saetp-typecode saetp))
456 (sb!vm:saetp-n-bits saetp)))
457 array)
458 #!+sb-doc
459 "255 means bad entry.")
460 (declaim (type (simple-array (unsigned-byte 8) (#.(1+ sb!vm:widetag-mask)))
461 **saetp-bits-per-length**))
463 ;; No ALLOCATE-VECTOR on host (nor READ-N-BYTES)
464 (!define-fop 43 :not-host (fop-spec-vector)
465 (let* ((length (read-word-arg (fasl-input-stream)))
466 (widetag (read-byte-arg (fasl-input-stream)))
467 (bits-per-length (aref **saetp-bits-per-length** widetag))
468 (bits (progn (aver (< bits-per-length 255))
469 (* length bits-per-length)))
470 (bytes (ceiling bits sb!vm:n-byte-bits))
471 (words (ceiling bytes sb!vm:n-word-bytes))
472 (vector (allocate-vector widetag length words)))
473 (declare (type index length bytes words)
474 (type word bits))
475 (read-n-bytes (fasl-input-stream) vector 0 bytes)
476 vector))
478 (!define-fop 53 (fop-eval (expr)) ; This seems to be unused
479 (if (skip-until)
480 expr
481 (eval expr)))
483 (!define-fop 54 (fop-eval-for-effect (expr) nil) ; This seems to be unused
484 (unless (skip-until)
485 (eval expr))
486 nil)
488 (defun fop-funcall* (input-stream stack skipping)
489 (let ((argc (read-byte-arg input-stream)))
490 (with-fop-stack ((stack) ptr (1+ argc))
491 (unless skipping
492 (do ((i (+ ptr argc))
493 (args))
494 ((= i ptr) (apply (fop-stack-ref i) args))
495 (declare (type index i))
496 (push (fop-stack-ref i) args)
497 (decf i))))))
499 ;; FIXME: there should be a syntax to share these identical bodies
500 (!define-fop 55 (fop-funcall)
501 (fop-funcall* (fasl-input-stream) (operand-stack) (skip-until)))
502 (!define-fop 56 (fop-funcall-for-effect () nil)
503 (fop-funcall* (fasl-input-stream) (operand-stack) (skip-until)))
505 ;;;; fops for fixing up circularities
507 (!define-fop 200 (fop-rplaca (val) nil)
508 (let ((obj (ref-fop-table (fasl-input) (read-word-arg (fasl-input-stream))))
509 (idx (read-word-arg (fasl-input-stream))))
510 (setf (car (nthcdr idx obj)) val)))
512 (!define-fop 201 (fop-rplacd (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 (cdr (nthcdr idx obj)) val)))
517 (!define-fop 202 (fop-svset (val) nil)
518 (let* ((obi (read-word-arg (fasl-input-stream)))
519 (obj (ref-fop-table (fasl-input) obi))
520 (idx (read-word-arg (fasl-input-stream))))
521 (if (%instancep obj) ; suspicious. should have been FOP-STRUCTSET
522 #+sb-xc (setf (%instance-ref obj idx) val)
523 #-sb-xc (bug "%instance-set?")
524 (setf (svref obj idx) val))))
526 (!define-fop 204 :not-host (fop-structset (val) nil)
527 (setf (%instance-ref (ref-fop-table (fasl-input)
528 (read-word-arg (fasl-input-stream)))
529 (read-word-arg (fasl-input-stream)))
530 val))
532 ;;; In the original CMUCL code, this actually explicitly declared PUSHP
533 ;;; to be T, even though that's what it defaults to in DEFINE-FOP.
534 (!define-fop 203 (fop-nthcdr (obj))
535 (nthcdr (read-word-arg (fasl-input-stream)) obj))
537 ;;;; fops for loading functions
539 ;;; (In CMU CL there was a FOP-CODE-FORMAT (47) which was
540 ;;; conventionally placed at the beginning of each fasl file to test
541 ;;; for compatibility between the fasl file and the CMU CL which
542 ;;; loaded it. In SBCL, this functionality has been replaced by
543 ;;; putting the implementation and version in required fields in the
544 ;;; fasl file header.)
546 ;; Cold-load calls COLD-LOAD-CODE instead
547 (!define-fop #xE0 :not-host (fop-code ((:operands n-boxed-words n-unboxed-bytes)))
548 (with-fop-stack ((stack (operand-stack)) ptr (1+ n-boxed-words))
549 (load-code n-boxed-words n-unboxed-bytes stack ptr (fasl-input-stream))))
551 ;; this gets you an #<fdefn> object, not the result of (FDEFINITION x)
552 ;; cold-loader uses COLD-FDEFINITION-OBJECT instead.
553 (!define-fop 60 :not-host (fop-fdefn (name))
554 (awhen (deprecated-thing-p 'function name) ; returns the stage of deprecation
555 (pushnew (list* it name :function)
556 (%fasl-input-deprecated-stuff (fasl-input)) :test 'equal))
557 (find-or-create-fdefn name))
559 (!define-fop 65 :not-host (fop-known-fun (name))
560 (%coerce-name-to-fun name))
562 #!-(or x86 x86-64)
563 (!define-fop 61 :not-host (fop-sanctify-for-execution (component))
564 (sb!vm:sanctify-for-execution component)
565 component)
567 (!define-fop 174 (fop-note-debug-source (debug-source) nil)
568 (warn "~@<FOP-NOTE-DEBUG-SOURCE seen in ordinary load (not cold load) -- ~
569 very strange! If you didn't do something to cause this, please report it as ~
570 a bug.~@:>")
571 ;; we are going to be lenient with coming across this fop in a warm SBCL.
572 (setf (sb!c::debug-source-compiled debug-source) (get-universal-time)
573 (sb!c::debug-source-created debug-source)
574 (file-write-date (sb!c::debug-source-namestring debug-source))))
576 ;;; Modify a slot in a CONSTANTS object.
577 (!define-fop 140 :not-host (fop-alter-code ((:operands index) code value) nil)
578 (setf (code-header-ref code index) value)
579 (values))
581 (!define-fop 139 :not-host (fop-fun-entry (code-object name arglist type info))
582 (let ((offset (read-word-arg (fasl-input-stream))))
583 (declare (type index offset))
584 (unless (zerop (logand offset sb!vm:lowtag-mask))
585 (bug "unaligned function object, offset = #X~X" offset))
586 (let ((fun (%primitive sb!c:compute-fun code-object offset)))
587 (setf (%simple-fun-self fun) fun)
588 (setf (%simple-fun-next fun) (%code-entry-points code-object))
589 (setf (%code-entry-points code-object) fun)
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 (!define-fop 147 :not-host (fop-foreign-fixup (code-object kind))
624 (let* ((len (read-byte-arg (fasl-input-stream)))
625 (sym (make-string len :element-type 'base-char)))
626 (read-n-bytes (fasl-input-stream) sym 0 len)
627 (sb!vm:fixup-code-object code-object
628 (read-word-arg (fasl-input-stream))
629 (foreign-symbol-address sym)
630 kind)
631 code-object))
633 (!define-fop 148 :not-host (fop-assembler-fixup (code-object kind routine))
634 (multiple-value-bind (value found) (gethash routine *assembler-routines*)
635 (unless found
636 (error "undefined assembler routine: ~S" routine))
637 (sb!vm:fixup-code-object code-object (read-word-arg (fasl-input-stream))
638 value kind))
639 code-object)
641 (!define-fop 149 :not-host (fop-code-object-fixup (code-object kind))
642 ;; Note: We don't have to worry about GC moving the code-object after
643 ;; the GET-LISP-OBJ-ADDRESS and before that value is deposited, because
644 ;; we can only use code-object fixups when code-objects don't move.
645 (sb!vm:fixup-code-object code-object (read-word-arg (fasl-input-stream))
646 (get-lisp-obj-address code-object) kind)
647 code-object)
649 #!+linkage-table
650 (!define-fop 150 :not-host (fop-foreign-dataref-fixup (code-object kind))
651 (let* ((len (read-byte-arg (fasl-input-stream)))
652 (sym (make-string len :element-type 'base-char)))
653 (read-n-bytes (fasl-input-stream) sym 0 len)
654 (sb!vm:fixup-code-object code-object
655 (read-word-arg (fasl-input-stream))
656 (foreign-symbol-address sym t)
657 kind)
658 code-object))
660 ;;; FOPs needed for implementing an IF operator in a FASL
662 ;;; Skip until a FOP-MAYBE-STOP-SKIPPING with the same POSITION is
663 ;;; executed. While skipping, we execute most FOPs normally, except
664 ;;; for ones that a) funcall/eval b) start skipping. This needs to
665 ;;; be done to ensure that the fop table gets populated correctly
666 ;;; regardless of the execution path.
667 (!define-fop 151 (fop-skip (position) nil)
668 (unless (skip-until)
669 (setf (skip-until) position))
670 (values))
672 ;;; As before, but only start skipping if the top of the FOP stack is NIL.
673 (!define-fop 152 (fop-skip-if-false (position condition) nil)
674 (unless (or condition (skip-until))
675 (setf (skip-until) position))
676 (values))
678 ;;; If skipping, pop the top of the stack and discard it. Needed for
679 ;;; ensuring that the stack stays balanced when skipping.
680 (!define-fop 153 (fop-drop-if-skipping () nil)
681 (when (skip-until)
682 (fop-stack-pop-n (operand-stack) 1))
683 (values))
685 ;;; If skipping, push a dummy value on the stack. Needed for
686 ;;; ensuring that the stack stays balanced when skipping.
687 (!define-fop 154 (fop-push-nil-if-skipping () nil)
688 (when (skip-until)
689 (push-fop-stack nil (fasl-input)))
690 (values))
692 ;;; Stop skipping if the top of the stack matches SKIP-UNTIL
693 (!define-fop 155 (fop-maybe-stop-skipping (label) nil)
694 (when (eql (skip-until) label)
695 (setf (skip-until) nil))
696 (values))
698 ;;; Primordial layouts.
699 ;;; At the rate the opcode space is filling up, it might be wise to
700 ;;; rethink using 16 each for FOP-CODE and -SYMBOL-IN-PACKAGE-SAVE.
701 (macrolet ((frob (&rest specs)
702 `(progn
703 (defun known-layout-fop (name)
704 (case name
705 ,@(mapcar (lambda (spec) `((,(cadr spec)) ,(car spec)))
706 specs)))
707 ,@(mapcar (lambda (spec)
708 `(!define-fop ,(car spec)
709 (,(symbolicate "FOP-LAYOUT-OF-"
710 (cadr spec)))
711 (find-layout ',(cadr spec))))
712 specs))))
713 (frob (#x6c t)
714 (#x6d structure-object)
715 (#x6e structure!object)
716 (#x6f definition-source-location)
717 (#x70 sb!c::debug-fun)
718 (#x71 sb!c::compiled-debug-fun)
719 (#x72 sb!c::debug-info)
720 (#x73 sb!c::compiled-debug-info)
721 (#x74 sb!c::debug-source)
722 (#x75 defstruct-description)
723 (#x76 defstruct-slot-description)))