1 ;;;; the VM definition of various primitive memory access VOPs for the
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;;; data object ref/set stuff
18 (:args (object :scs (descriptor-reg)))
19 (:info name offset lowtag)
21 (:results (result :scs (descriptor-reg any-reg)))
23 (loadw result object offset lowtag)))
25 (define-vop (set-slot)
26 (:args (object :scs (descriptor-reg))
27 (value :scs (descriptor-reg any-reg null zero)))
28 (:info name offset lowtag #!+gengc remember)
34 (storew-and-remember-slot value object offset lowtag)
35 (storew value object offset lowtag))
37 (storew value object offset lowtag)))
39 (define-vop (init-slot set-slot))
41 ;;;; symbol hacking VOPs
43 ;;; The compiler likes to be able to directly SET symbols.
44 (define-vop (set cell-set)
45 (:variant symbol-value-slot other-pointer-lowtag))
47 ;;; Do a cell ref with an error check for being unbound.
48 (define-vop (checked-cell-ref)
49 (:args (object :scs (descriptor-reg) :target obj-temp))
50 (:results (value :scs (descriptor-reg any-reg)))
53 (:save-p :compute-only)
54 (:temporary (:scs (non-descriptor-reg)) temp)
55 (:temporary (:scs (descriptor-reg) :from (:argument 0)) obj-temp))
57 ;;; With SYMBOL-VALUE, we check that the value isn't the trap object.
58 ;;; So SYMBOL-VALUE of NIL is NIL.
59 (define-vop (symbol-value checked-cell-ref)
60 (:translate symbol-value)
62 (move object obj-temp)
63 (loadw value obj-temp symbol-value-slot other-pointer-lowtag)
64 (let ((err-lab (generate-error-code vop unbound-symbol-error obj-temp)))
65 (inst xor value unbound-marker-widetag temp)
66 (inst beq temp err-lab))))
68 ;;; like CHECKED-CELL-REF, only we are a predicate to see if the cell
70 (define-vop (boundp-frob)
71 (:args (object :scs (descriptor-reg)))
75 (:temporary (:scs (descriptor-reg)) value)
76 (:temporary (:scs (non-descriptor-reg)) temp))
78 (define-vop (boundp boundp-frob)
81 (loadw value object symbol-value-slot other-pointer-lowtag)
82 (inst xor value unbound-marker-widetag temp)
84 (inst beq temp target)
85 (inst bne temp target))))
87 (define-vop (fast-symbol-value cell-ref)
88 (:variant symbol-value-slot other-pointer-lowtag)
90 (:translate symbol-value))
92 (define-vop (symbol-hash)
94 (:translate symbol-hash)
95 (:args (symbol :scs (descriptor-reg)))
96 (:results (res :scs (any-reg)))
97 (:result-types positive-fixnum)
99 ;; The symbol-hash slot of NIL holds NIL because it is also the
100 ;; cdr slot, so we have to strip off the two low bits to make sure
101 ;; it is a fixnum. The lowtag selection magic that is required to
102 ;; ensure this is explained in the comment in objdef.lisp
103 (loadw res symbol symbol-hash-slot other-pointer-lowtag)
104 (inst bic res #.(ash lowtag-mask -1) res)))
106 ;;; On unithreaded builds these are just copies of the non-global versions.
107 (define-vop (%set-symbol-global-value set))
108 (define-vop (symbol-global-value symbol-value)
109 (:translate symbol-global-value))
110 (define-vop (fast-symbol-global-value fast-symbol-value)
111 (:translate symbol-global-value))
113 ;;;; fdefinition (FDEFN) objects
115 (define-vop (fdefn-fun cell-ref)
116 (:variant fdefn-fun-slot other-pointer-lowtag))
118 (define-vop (safe-fdefn-fun)
119 (:args (object :scs (descriptor-reg) :target obj-temp))
120 (:results (value :scs (descriptor-reg any-reg)))
122 (:save-p :compute-only)
123 (:temporary (:scs (descriptor-reg) :from (:argument 0)) obj-temp)
124 (:temporary (:scs (non-descriptor-reg)) temp)
126 (move object obj-temp)
127 (loadw value obj-temp fdefn-fun-slot other-pointer-lowtag)
128 (let ((err-lab (generate-error-code vop undefined-fun-error obj-temp)))
129 (inst cmpeq value null-tn temp)
130 (inst bne temp err-lab))))
132 (define-vop (set-fdefn-fun)
134 (:translate (setf fdefn-fun))
135 (:args (function :scs (descriptor-reg) :target result)
136 (fdefn :scs (descriptor-reg)))
137 (:temporary (:scs (interior-reg)) lip)
138 (:temporary (:scs (non-descriptor-reg)) type)
139 (:results (result :scs (descriptor-reg)))
141 (let ((normal-fn (gen-label)))
142 (load-type type function (- fun-pointer-lowtag))
143 (inst xor type simple-fun-header-widetag type)
145 (- (ash simple-fun-code-offset word-shift) fun-pointer-lowtag)
147 (inst beq type normal-fn)
148 (inst li (make-fixup "closure_tramp" :foreign) lip)
149 (emit-label normal-fn)
150 (storew lip fdefn fdefn-raw-addr-slot other-pointer-lowtag)
151 (storew function fdefn fdefn-fun-slot other-pointer-lowtag)
152 (move function result))))
155 (define-vop (fdefn-makunbound)
157 (:translate fdefn-makunbound)
158 (:args (fdefn :scs (descriptor-reg) :target result))
159 (:temporary (:scs (non-descriptor-reg)) temp)
160 (:results (result :scs (descriptor-reg)))
162 (storew null-tn fdefn fdefn-fun-slot other-pointer-lowtag)
163 (inst li (make-fixup "undefined_tramp" :foreign) temp)
165 (storew temp fdefn fdefn-raw-addr-slot other-pointer-lowtag)))
167 ;;;; binding and Unbinding
169 ;;; Establish VAL as a binding for SYMBOL. Save the old value and the
170 ;;; symbol on the binding stack and stuff the new value into the symbol.
172 ;;; See the "Chapter 9: Specials" of the SBCL Internals Manual.
175 (:args (val :scs (any-reg descriptor-reg))
176 (symbol :scs (descriptor-reg)))
177 (:temporary (:scs (descriptor-reg)) temp)
179 (loadw temp symbol symbol-value-slot other-pointer-lowtag)
180 (inst addq bsp-tn (* 2 n-word-bytes) bsp-tn)
181 (storew temp bsp-tn (- binding-value-slot binding-size))
182 (storew symbol bsp-tn (- binding-symbol-slot binding-size))
183 (#!+gengc storew-and-remember-slot #!-gengc storew
184 val symbol symbol-value-slot other-pointer-lowtag)))
188 (:temporary (:scs (descriptor-reg)) symbol value)
190 (loadw symbol bsp-tn (- binding-symbol-slot binding-size))
191 (loadw value bsp-tn (- binding-value-slot binding-size))
192 (#!+gengc storew-and-remember-slot #!-gengc storew
193 value symbol symbol-value-slot other-pointer-lowtag)
194 (storew zero-tn bsp-tn (- binding-symbol-slot binding-size))
195 (storew zero-tn bsp-tn (- binding-value-slot binding-size))
196 (inst subq bsp-tn (* 2 n-word-bytes) bsp-tn)))
199 (define-vop (unbind-to-here)
200 (:args (arg :scs (descriptor-reg any-reg) :target where))
201 (:temporary (:scs (any-reg) :from (:argument 0)) where)
202 (:temporary (:scs (descriptor-reg)) symbol value)
203 (:temporary (:scs (non-descriptor-reg)) temp)
205 (let ((loop (gen-label))
209 (inst cmpeq where bsp-tn temp)
213 (loadw symbol bsp-tn (- binding-symbol-slot binding-size))
214 (loadw value bsp-tn (- binding-value-slot binding-size))
215 (inst beq symbol skip)
216 (#!+gengc storew-and-remember-slot #!-gengc storew
217 value symbol symbol-value-slot other-pointer-lowtag)
218 (storew zero-tn bsp-tn (- binding-symbol-slot binding-size))
221 (storew zero-tn bsp-tn (- binding-value-slot binding-size))
222 (inst subq bsp-tn (* 2 n-word-bytes) bsp-tn)
223 (inst cmpeq where bsp-tn temp)
228 ;;;; closure indexing
230 (define-full-reffer closure-index-ref *
231 closure-info-offset fun-pointer-lowtag
232 (descriptor-reg any-reg) * %closure-index-ref)
234 (define-full-setter set-funcallable-instance-info *
235 funcallable-instance-info-offset fun-pointer-lowtag
236 (descriptor-reg any-reg null zero) * %set-funcallable-instance-info)
238 (define-full-reffer funcallable-instance-info *
239 funcallable-instance-info-offset fun-pointer-lowtag
240 (descriptor-reg any-reg) * %funcallable-instance-info)
242 (define-vop (closure-ref slot-ref)
243 (:variant closure-info-offset fun-pointer-lowtag))
245 (define-vop (closure-init slot-set)
246 (:variant closure-info-offset fun-pointer-lowtag))
248 (define-vop (closure-init-from-fp)
249 (:args (object :scs (descriptor-reg)))
252 (storew cfp-tn object (+ closure-info-offset offset) fun-pointer-lowtag)))
254 ;;;; value cell hackery
256 (define-vop (value-cell-ref cell-ref)
257 (:variant value-cell-value-slot other-pointer-lowtag))
259 (define-vop (value-cell-set cell-set)
260 (:variant value-cell-value-slot other-pointer-lowtag))
262 ;;;; instance hackery
264 (define-vop (instance-length)
266 (:translate %instance-length)
267 (:args (struct :scs (descriptor-reg)))
268 (:results (res :scs (unsigned-reg)))
269 (:result-types positive-fixnum)
271 (loadw res struct 0 instance-pointer-lowtag)
272 (inst srl res n-widetag-bits res)))
274 (define-full-reffer instance-index-ref * instance-slots-offset
275 instance-pointer-lowtag (descriptor-reg any-reg) * %instance-ref)
277 (define-full-setter instance-index-set * instance-slots-offset
278 instance-pointer-lowtag (descriptor-reg any-reg null zero) * %instance-set)
280 ;;;; code object frobbing
282 (define-full-reffer code-header-ref * 0 other-pointer-lowtag
283 (descriptor-reg any-reg) * code-header-ref)
285 (define-full-setter code-header-set * 0 other-pointer-lowtag
286 (descriptor-reg any-reg null zero) * code-header-set)
288 ;;;; mutator accessing
293 (eval-when (:compile-toplevel :load-toplevel :execute)
294 ;; SBCL has never had GENGC. Now that we have Alpha support, it
295 ;; would probably be nice to restore GENGC support so that the Alpha
296 ;; doesn't have to crawl along with stop'n'copy. When we do, the CMU
297 ;; CL code below will need updating to the SBCL way of looking at
298 ;; things, e.g. at least using "SB-KERNEL" or "SB!KERNEL" instead of
299 ;; :KERNEL. -- WHN 2001-05-08
300 (error "This code is stale as of sbcl-0.6.12."))
302 (define-vop (mutator-ub32-ref)
305 (:results (res :scs (unsigned-reg)))
306 (:result-types unsigned-num)
309 (loadw res mutator-tn slot)))
311 (define-vop (mutator-descriptor-ref mutator-ub32-ref)
312 (:results (res :scs (any-reg descriptor-reg)))
315 (define-vop (mutator-sap-ref mutator-ub32-ref)
316 (:results (res :scs (sap-reg)))
317 (:result-types system-area-pointer))
320 (define-vop (mutator-ub32-set)
322 (:args (arg :scs (unsigned-reg) :target res))
323 (:arg-types unsigned-num)
324 (:results (res :scs (unsigned-reg)))
325 (:result-types unsigned-num)
328 (storew arg mutator-tn slot)
331 (define-vop (mutator-descriptor-set mutator-ub32-set)
332 (:args (arg :scs (any-reg descriptor-reg null zero) :target res))
334 (:results (res :scs (any-reg descriptor-reg)))
337 (define-vop (mutator-sap-set mutator-ub32-set)
338 (:args (arg :scs (sap-reg) :target res))
339 (:arg-types system-area-pointer)
340 (:results (res :scs (sap-reg)))
341 (:result-types system-area-pointer))
344 (macrolet ((define-mutator-accessors (slot type writable)
345 (let ((ref (symbolicate "MUTATOR-" slot "-REF"))
346 (set (and writable (symbolicate "MUTATOR-" slot "-SET")))
347 (offset (symbolicate "MUTATOR-" slot "-SLOT"))
349 (let ((*package* (find-package :kernel)))
350 (symbolicate "MUTATOR-" slot))))
352 (lisp-type ref-vop set-vop)
356 'mutator-descriptor-ref
357 'mutator-descriptor-set))
359 (values '(unsigned-byte 32)
363 (values 'system-area-pointer
367 (export ',fn :kernel)
368 (defknown ,fn () ,lisp-type (flushable))
369 (define-vop (,ref ,ref-vop)
373 `((defknown ((setf ,fn)) (,lisp-type) ,lisp-type
375 (define-vop (,set ,set-vop)
376 (:translate (setf ,fn))
377 (:variant ,offset)))))))))
378 (define-mutator-accessors thread :des t)
379 (define-mutator-accessors suspends-disabled-count :ub32 t)
380 (define-mutator-accessors suspend-pending :ub32 t)
381 (define-mutator-accessors control-stack-base :sap nil)
382 (define-mutator-accessors control-stack-end :sap nil)
383 (define-mutator-accessors current-unwind-protect :sap nil)
384 (define-mutator-accessors current-catch-block :sap nil)
385 (define-mutator-accessors binding-stack-base :sap nil)
386 (define-mutator-accessors binding-stack-end :sap nil)
387 (define-mutator-accessors number-stack-base :sap nil)
388 (define-mutator-accessors number-stack-end :sap nil)
389 (define-mutator-accessors nursery-start :sap nil)
390 (define-mutator-accessors nursery-end :sap nil)
391 (define-mutator-accessors storebuf-start :sap nil)
392 (define-mutator-accessors storebuf-end :sap nil)
393 (define-mutator-accessors words-consed :ub32 nil))
399 ;;;; raw instance slot accessors
401 (define-vop (raw-instance-ref/word)
402 (:translate %raw-instance-ref/word)
404 (:args (object :scs (descriptor-reg))
405 (index :scs (any-reg)))
406 (:arg-types * positive-fixnum)
407 (:results (value :scs (unsigned-reg)))
408 (:temporary (:scs (non-descriptor-reg)) offset)
409 (:temporary (:scs (interior-reg)) lip)
410 (:result-types unsigned-num)
412 (loadw offset object 0 instance-pointer-lowtag)
413 (inst srl offset n-widetag-bits offset)
414 (inst sll offset 2 offset)
415 (inst subq offset index offset)
416 (inst subq offset n-word-bytes offset)
417 (inst addq object offset lip)
420 (- (* instance-slots-offset n-word-bytes)
421 instance-pointer-lowtag)
423 (inst mskll value 4 value)))
425 (define-vop (raw-instance-set/word)
426 (:translate %raw-instance-set/word)
428 (:args (object :scs (descriptor-reg))
429 (index :scs (any-reg))
430 (value :scs (unsigned-reg)))
431 (:arg-types * positive-fixnum unsigned-num)
432 (:results (result :scs (unsigned-reg)))
433 (:temporary (:scs (non-descriptor-reg)) offset)
434 (:temporary (:scs (interior-reg)) lip)
435 (:result-types unsigned-num)
437 (loadw offset object 0 instance-pointer-lowtag)
438 (inst srl offset n-widetag-bits offset)
439 (inst sll offset 2 offset)
440 (inst subq offset index offset)
441 (inst subq offset n-word-bytes offset)
442 (inst addq object offset lip)
445 (- (* instance-slots-offset n-word-bytes)
446 instance-pointer-lowtag)
448 (move value result)))
450 (define-vop (raw-instance-ref/single)
451 (:translate %raw-instance-ref/single)
453 (:args (object :scs (descriptor-reg))
454 (index :scs (any-reg)))
455 (:arg-types * positive-fixnum)
456 (:results (value :scs (single-reg)))
457 (:temporary (:scs (non-descriptor-reg)) offset)
458 (:temporary (:scs (interior-reg)) lip)
459 (:result-types single-float)
461 (loadw offset object 0 instance-pointer-lowtag)
462 (inst srl offset n-widetag-bits offset)
463 (inst sll offset 2 offset)
464 (inst subq offset index offset)
465 (inst subq offset n-word-bytes offset)
466 (inst addq object offset lip)
469 (- (* instance-slots-offset n-word-bytes)
470 instance-pointer-lowtag)
473 (define-vop (raw-instance-set/single)
474 (:translate %raw-instance-set/single)
476 (:args (object :scs (descriptor-reg))
477 (index :scs (any-reg))
478 (value :scs (single-reg)))
479 (:arg-types * positive-fixnum single-float)
480 (:results (result :scs (single-reg)))
481 (:temporary (:scs (non-descriptor-reg)) offset)
482 (:temporary (:scs (interior-reg)) lip)
483 (:result-types single-float)
485 (loadw offset object 0 instance-pointer-lowtag)
486 (inst srl offset n-widetag-bits offset)
487 (inst sll offset 2 offset)
488 (inst subq offset index offset)
489 (inst subq offset n-word-bytes offset)
490 (inst addq object offset lip)
493 (- (* instance-slots-offset n-word-bytes)
494 instance-pointer-lowtag)
496 (unless (location= result value)
497 (inst fmove value result))))
499 (define-vop (raw-instance-ref/double)
500 (:translate %raw-instance-ref/double)
502 (:args (object :scs (descriptor-reg))
503 (index :scs (any-reg)))
504 (:arg-types * positive-fixnum)
505 (:results (value :scs (double-reg)))
506 (:temporary (:scs (non-descriptor-reg)) offset)
507 (:temporary (:scs (interior-reg)) lip)
508 (:result-types double-float)
510 (loadw offset object 0 instance-pointer-lowtag)
511 (inst srl offset n-widetag-bits offset)
512 (inst sll offset 2 offset)
513 (inst subq offset index offset)
514 (inst subq offset (* 2 n-word-bytes) offset)
515 (inst addq object offset lip)
518 (- (* instance-slots-offset n-word-bytes)
519 instance-pointer-lowtag)
522 (define-vop (raw-instance-set/double)
523 (:translate %raw-instance-set/double)
525 (:args (object :scs (descriptor-reg))
526 (index :scs (any-reg))
527 (value :scs (double-reg)))
528 (:arg-types * positive-fixnum double-float)
529 (:results (result :scs (double-reg)))
530 (:temporary (:scs (non-descriptor-reg)) offset)
531 (:temporary (:scs (interior-reg)) lip)
532 (:result-types double-float)
534 (loadw offset object 0 instance-pointer-lowtag)
535 (inst srl offset n-widetag-bits offset)
536 (inst sll offset 2 offset)
537 (inst subq offset index offset)
538 (inst subq offset (* 2 n-word-bytes) offset)
539 (inst addq object offset lip)
542 (- (* instance-slots-offset n-word-bytes)
543 instance-pointer-lowtag)
545 (unless (location= result value)
546 (inst fmove value result))))
548 (define-vop (raw-instance-ref/complex-single)
549 (:translate %raw-instance-ref/complex-single)
551 (:args (object :scs (descriptor-reg))
552 (index :scs (any-reg)))
553 (:arg-types * positive-fixnum)
554 (:results (value :scs (complex-single-reg)))
555 (:temporary (:scs (non-descriptor-reg)) offset)
556 (:temporary (:scs (interior-reg)) lip)
557 (:result-types complex-single-float)
559 (loadw offset object 0 instance-pointer-lowtag)
560 (inst srl offset n-widetag-bits offset)
561 (inst sll offset 2 offset)
562 (inst subq offset index offset)
563 (inst subq offset (* 2 n-word-bytes) offset)
564 (inst addq object offset lip)
566 (complex-double-reg-real-tn value)
567 (- (* instance-slots-offset n-word-bytes)
568 instance-pointer-lowtag)
571 (complex-double-reg-imag-tn value)
572 (- (* (1+ instance-slots-offset) n-word-bytes)
573 instance-pointer-lowtag)
576 (define-vop (raw-instance-set/complex-single)
577 (:translate %raw-instance-set/complex-single)
579 (:args (object :scs (descriptor-reg))
580 (index :scs (any-reg))
581 (value :scs (complex-single-reg)))
582 (:arg-types * positive-fixnum complex-single-float)
583 (:results (result :scs (complex-single-reg)))
584 (:temporary (:scs (non-descriptor-reg)) offset)
585 (:temporary (:scs (interior-reg)) lip)
586 (:result-types complex-single-float)
588 (loadw offset object 0 instance-pointer-lowtag)
589 (inst srl offset n-widetag-bits offset)
590 (inst sll offset 2 offset)
591 (inst subq offset index offset)
592 (inst subq offset (* 2 n-word-bytes) offset)
593 (inst addq object offset lip)
594 (let ((value-real (complex-single-reg-real-tn value))
595 (result-real (complex-single-reg-real-tn result)))
598 (- (* instance-slots-offset n-word-bytes)
599 instance-pointer-lowtag)
601 (unless (location= result-real value-real)
602 (inst fmove value-real result-real)))
603 (let ((value-imag (complex-single-reg-imag-tn value))
604 (result-imag (complex-single-reg-imag-tn result)))
607 (- (* (1+ instance-slots-offset) n-word-bytes)
608 instance-pointer-lowtag)
610 (unless (location= result-imag value-imag)
611 (inst fmove value-imag result-imag)))))
613 (define-vop (raw-instance-ref/complex-double)
614 (:translate %raw-instance-ref/complex-double)
616 (:args (object :scs (descriptor-reg))
617 (index :scs (any-reg)))
618 (:arg-types * positive-fixnum)
619 (:results (value :scs (complex-double-reg)))
620 (:temporary (:scs (non-descriptor-reg)) offset)
621 (:temporary (:scs (interior-reg)) lip)
622 (:result-types complex-double-float)
624 (loadw offset object 0 instance-pointer-lowtag)
625 (inst srl offset n-widetag-bits offset)
626 (inst sll offset 2 offset)
627 (inst subq offset index offset)
628 (inst subq offset (* 4 n-word-bytes) offset)
629 (inst addq object offset lip)
631 (complex-double-reg-real-tn value)
632 (- (* instance-slots-offset n-word-bytes)
633 instance-pointer-lowtag)
636 (complex-double-reg-imag-tn value)
637 (- (* (+ instance-slots-offset 2) n-word-bytes)
638 instance-pointer-lowtag)
641 (define-vop (raw-instance-set/complex-double)
642 (:translate %raw-instance-set/complex-double)
644 (:args (object :scs (descriptor-reg))
645 (index :scs (any-reg))
646 (value :scs (complex-double-reg)))
647 (:arg-types * positive-fixnum complex-double-float)
648 (:results (result :scs (complex-double-reg)))
649 (:temporary (:scs (non-descriptor-reg)) offset)
650 (:temporary (:scs (interior-reg)) lip)
651 (:result-types complex-double-float)
653 (loadw offset object 0 instance-pointer-lowtag)
654 (inst srl offset n-widetag-bits offset)
655 (inst sll offset 2 offset)
656 (inst subq offset index offset)
657 (inst subq offset (* 4 n-word-bytes) offset)
658 (inst addq object offset lip)
659 (let ((value-real (complex-double-reg-real-tn value))
660 (result-real (complex-double-reg-real-tn result)))
663 (- (* instance-slots-offset n-word-bytes)
664 instance-pointer-lowtag)
666 (unless (location= result-real value-real)
667 (inst fmove value-real result-real)))
668 (let ((value-imag (complex-double-reg-imag-tn value))
669 (result-imag (complex-double-reg-imag-tn result)))
672 (- (* (+ instance-slots-offset 2) n-word-bytes)
673 instance-pointer-lowtag)
675 (unless (location= result-imag value-imag)
676 (inst fmove value-imag result-imag)))))