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 (:args (val :scs (any-reg descriptor-reg))
173 (symbol :scs (descriptor-reg)))
174 (:temporary (:scs (descriptor-reg)) temp)
176 (loadw temp symbol symbol-value-slot other-pointer-lowtag)
177 (inst addq bsp-tn (* 2 n-word-bytes) bsp-tn)
178 (storew temp bsp-tn (- binding-value-slot binding-size))
179 (storew symbol bsp-tn (- binding-symbol-slot binding-size))
180 (#!+gengc storew-and-remember-slot #!-gengc storew
181 val symbol symbol-value-slot other-pointer-lowtag)))
185 (:temporary (:scs (descriptor-reg)) symbol value)
187 (loadw symbol bsp-tn (- binding-symbol-slot binding-size))
188 (loadw value bsp-tn (- binding-value-slot binding-size))
189 (#!+gengc storew-and-remember-slot #!-gengc storew
190 value symbol symbol-value-slot other-pointer-lowtag)
191 (storew zero-tn bsp-tn (- binding-symbol-slot binding-size))
192 (storew zero-tn bsp-tn (- binding-value-slot binding-size))
193 (inst subq bsp-tn (* 2 n-word-bytes) bsp-tn)))
196 (define-vop (unbind-to-here)
197 (:args (arg :scs (descriptor-reg any-reg) :target where))
198 (:temporary (:scs (any-reg) :from (:argument 0)) where)
199 (:temporary (:scs (descriptor-reg)) symbol value)
200 (:temporary (:scs (non-descriptor-reg)) temp)
202 (let ((loop (gen-label))
206 (inst cmpeq where bsp-tn temp)
210 (loadw symbol bsp-tn (- binding-symbol-slot binding-size))
211 (loadw value bsp-tn (- binding-value-slot binding-size))
212 (inst beq symbol skip)
213 (#!+gengc storew-and-remember-slot #!-gengc storew
214 value symbol symbol-value-slot other-pointer-lowtag)
215 (storew zero-tn bsp-tn (- binding-symbol-slot binding-size))
218 (storew zero-tn bsp-tn (- binding-value-slot binding-size))
219 (inst subq bsp-tn (* 2 n-word-bytes) bsp-tn)
220 (inst cmpeq where bsp-tn temp)
225 ;;;; closure indexing
227 (define-full-reffer closure-index-ref *
228 closure-info-offset fun-pointer-lowtag
229 (descriptor-reg any-reg) * %closure-index-ref)
231 (define-full-setter set-funcallable-instance-info *
232 funcallable-instance-info-offset fun-pointer-lowtag
233 (descriptor-reg any-reg null zero) * %set-funcallable-instance-info)
235 (define-full-reffer funcallable-instance-info *
236 funcallable-instance-info-offset fun-pointer-lowtag
237 (descriptor-reg any-reg) * %funcallable-instance-info)
239 (define-vop (closure-ref slot-ref)
240 (:variant closure-info-offset fun-pointer-lowtag))
242 (define-vop (closure-init slot-set)
243 (:variant closure-info-offset fun-pointer-lowtag))
245 (define-vop (closure-init-from-fp)
246 (:args (object :scs (descriptor-reg)))
249 (storew cfp-tn object (+ closure-info-offset offset) fun-pointer-lowtag)))
251 ;;;; value cell hackery
253 (define-vop (value-cell-ref cell-ref)
254 (:variant value-cell-value-slot other-pointer-lowtag))
256 (define-vop (value-cell-set cell-set)
257 (:variant value-cell-value-slot other-pointer-lowtag))
259 ;;;; instance hackery
261 (define-vop (instance-length)
263 (:translate %instance-length)
264 (:args (struct :scs (descriptor-reg)))
265 (:results (res :scs (unsigned-reg)))
266 (:result-types positive-fixnum)
268 (loadw res struct 0 instance-pointer-lowtag)
269 (inst srl res n-widetag-bits res)))
271 (define-full-reffer instance-index-ref * instance-slots-offset
272 instance-pointer-lowtag (descriptor-reg any-reg) * %instance-ref)
274 (define-full-setter instance-index-set * instance-slots-offset
275 instance-pointer-lowtag (descriptor-reg any-reg null zero) * %instance-set)
277 ;;;; code object frobbing
279 (define-full-reffer code-header-ref * 0 other-pointer-lowtag
280 (descriptor-reg any-reg) * code-header-ref)
282 (define-full-setter code-header-set * 0 other-pointer-lowtag
283 (descriptor-reg any-reg null zero) * code-header-set)
285 ;;;; mutator accessing
290 (eval-when (:compile-toplevel :load-toplevel :execute)
291 ;; SBCL has never had GENGC. Now that we have Alpha support, it
292 ;; would probably be nice to restore GENGC support so that the Alpha
293 ;; doesn't have to crawl along with stop'n'copy. When we do, the CMU
294 ;; CL code below will need updating to the SBCL way of looking at
295 ;; things, e.g. at least using "SB-KERNEL" or "SB!KERNEL" instead of
296 ;; :KERNEL. -- WHN 2001-05-08
297 (error "This code is stale as of sbcl-0.6.12."))
299 (define-vop (mutator-ub32-ref)
302 (:results (res :scs (unsigned-reg)))
303 (:result-types unsigned-num)
306 (loadw res mutator-tn slot)))
308 (define-vop (mutator-descriptor-ref mutator-ub32-ref)
309 (:results (res :scs (any-reg descriptor-reg)))
312 (define-vop (mutator-sap-ref mutator-ub32-ref)
313 (:results (res :scs (sap-reg)))
314 (:result-types system-area-pointer))
317 (define-vop (mutator-ub32-set)
319 (:args (arg :scs (unsigned-reg) :target res))
320 (:arg-types unsigned-num)
321 (:results (res :scs (unsigned-reg)))
322 (:result-types unsigned-num)
325 (storew arg mutator-tn slot)
328 (define-vop (mutator-descriptor-set mutator-ub32-set)
329 (:args (arg :scs (any-reg descriptor-reg null zero) :target res))
331 (:results (res :scs (any-reg descriptor-reg)))
334 (define-vop (mutator-sap-set mutator-ub32-set)
335 (:args (arg :scs (sap-reg) :target res))
336 (:arg-types system-area-pointer)
337 (:results (res :scs (sap-reg)))
338 (:result-types system-area-pointer))
341 (macrolet ((define-mutator-accessors (slot type writable)
342 (let ((ref (symbolicate "MUTATOR-" slot "-REF"))
343 (set (and writable (symbolicate "MUTATOR-" slot "-SET")))
344 (offset (symbolicate "MUTATOR-" slot "-SLOT"))
346 (let ((*package* (find-package :kernel)))
347 (symbolicate "MUTATOR-" slot))))
349 (lisp-type ref-vop set-vop)
353 'mutator-descriptor-ref
354 'mutator-descriptor-set))
356 (values '(unsigned-byte 32)
360 (values 'system-area-pointer
364 (export ',fn :kernel)
365 (defknown ,fn () ,lisp-type (flushable))
366 (define-vop (,ref ,ref-vop)
370 `((defknown ((setf ,fn)) (,lisp-type) ,lisp-type
372 (define-vop (,set ,set-vop)
373 (:translate (setf ,fn))
374 (:variant ,offset)))))))))
375 (define-mutator-accessors thread :des t)
376 (define-mutator-accessors suspends-disabled-count :ub32 t)
377 (define-mutator-accessors suspend-pending :ub32 t)
378 (define-mutator-accessors control-stack-base :sap nil)
379 (define-mutator-accessors control-stack-end :sap nil)
380 (define-mutator-accessors current-unwind-protect :sap nil)
381 (define-mutator-accessors current-catch-block :sap nil)
382 (define-mutator-accessors binding-stack-base :sap nil)
383 (define-mutator-accessors binding-stack-end :sap nil)
384 (define-mutator-accessors number-stack-base :sap nil)
385 (define-mutator-accessors number-stack-end :sap nil)
386 (define-mutator-accessors nursery-start :sap nil)
387 (define-mutator-accessors nursery-end :sap nil)
388 (define-mutator-accessors storebuf-start :sap nil)
389 (define-mutator-accessors storebuf-end :sap nil)
390 (define-mutator-accessors words-consed :ub32 nil))
396 ;;;; raw instance slot accessors
398 (define-vop (raw-instance-ref/word)
399 (:translate %raw-instance-ref/word)
401 (:args (object :scs (descriptor-reg))
402 (index :scs (any-reg)))
403 (:arg-types * positive-fixnum)
404 (:results (value :scs (unsigned-reg)))
405 (:temporary (:scs (non-descriptor-reg)) offset)
406 (:temporary (:scs (interior-reg)) lip)
407 (:result-types unsigned-num)
409 (loadw offset object 0 instance-pointer-lowtag)
410 (inst srl offset n-widetag-bits offset)
411 (inst sll offset 2 offset)
412 (inst subq offset index offset)
413 (inst subq offset n-word-bytes offset)
414 (inst addq object offset lip)
417 (- (* instance-slots-offset n-word-bytes)
418 instance-pointer-lowtag)
420 (inst mskll value 4 value)))
422 (define-vop (raw-instance-set/word)
423 (:translate %raw-instance-set/word)
425 (:args (object :scs (descriptor-reg))
426 (index :scs (any-reg))
427 (value :scs (unsigned-reg)))
428 (:arg-types * positive-fixnum unsigned-num)
429 (:results (result :scs (unsigned-reg)))
430 (:temporary (:scs (non-descriptor-reg)) offset)
431 (:temporary (:scs (interior-reg)) lip)
432 (:result-types unsigned-num)
434 (loadw offset object 0 instance-pointer-lowtag)
435 (inst srl offset n-widetag-bits offset)
436 (inst sll offset 2 offset)
437 (inst subq offset index offset)
438 (inst subq offset n-word-bytes offset)
439 (inst addq object offset lip)
442 (- (* instance-slots-offset n-word-bytes)
443 instance-pointer-lowtag)
445 (move value result)))
447 (define-vop (raw-instance-ref/single)
448 (:translate %raw-instance-ref/single)
450 (:args (object :scs (descriptor-reg))
451 (index :scs (any-reg)))
452 (:arg-types * positive-fixnum)
453 (:results (value :scs (single-reg)))
454 (:temporary (:scs (non-descriptor-reg)) offset)
455 (:temporary (:scs (interior-reg)) lip)
456 (:result-types single-float)
458 (loadw offset object 0 instance-pointer-lowtag)
459 (inst srl offset n-widetag-bits offset)
460 (inst sll offset 2 offset)
461 (inst subq offset index offset)
462 (inst subq offset n-word-bytes offset)
463 (inst addq object offset lip)
466 (- (* instance-slots-offset n-word-bytes)
467 instance-pointer-lowtag)
470 (define-vop (raw-instance-set/single)
471 (:translate %raw-instance-set/single)
473 (:args (object :scs (descriptor-reg))
474 (index :scs (any-reg))
475 (value :scs (single-reg)))
476 (:arg-types * positive-fixnum single-float)
477 (:results (result :scs (single-reg)))
478 (:temporary (:scs (non-descriptor-reg)) offset)
479 (:temporary (:scs (interior-reg)) lip)
480 (:result-types single-float)
482 (loadw offset object 0 instance-pointer-lowtag)
483 (inst srl offset n-widetag-bits offset)
484 (inst sll offset 2 offset)
485 (inst subq offset index offset)
486 (inst subq offset n-word-bytes offset)
487 (inst addq object offset lip)
490 (- (* instance-slots-offset n-word-bytes)
491 instance-pointer-lowtag)
493 (unless (location= result value)
494 (inst fmove value result))))
496 (define-vop (raw-instance-ref/double)
497 (:translate %raw-instance-ref/double)
499 (:args (object :scs (descriptor-reg))
500 (index :scs (any-reg)))
501 (:arg-types * positive-fixnum)
502 (:results (value :scs (double-reg)))
503 (:temporary (:scs (non-descriptor-reg)) offset)
504 (:temporary (:scs (interior-reg)) lip)
505 (:result-types double-float)
507 (loadw offset object 0 instance-pointer-lowtag)
508 (inst srl offset n-widetag-bits offset)
509 (inst sll offset 2 offset)
510 (inst subq offset index offset)
511 (inst subq offset (* 2 n-word-bytes) offset)
512 (inst addq object offset lip)
515 (- (* instance-slots-offset n-word-bytes)
516 instance-pointer-lowtag)
519 (define-vop (raw-instance-set/double)
520 (:translate %raw-instance-set/double)
522 (:args (object :scs (descriptor-reg))
523 (index :scs (any-reg))
524 (value :scs (double-reg)))
525 (:arg-types * positive-fixnum double-float)
526 (:results (result :scs (double-reg)))
527 (:temporary (:scs (non-descriptor-reg)) offset)
528 (:temporary (:scs (interior-reg)) lip)
529 (:result-types double-float)
531 (loadw offset object 0 instance-pointer-lowtag)
532 (inst srl offset n-widetag-bits offset)
533 (inst sll offset 2 offset)
534 (inst subq offset index offset)
535 (inst subq offset (* 2 n-word-bytes) offset)
536 (inst addq object offset lip)
539 (- (* instance-slots-offset n-word-bytes)
540 instance-pointer-lowtag)
542 (unless (location= result value)
543 (inst fmove value result))))
545 (define-vop (raw-instance-ref/complex-single)
546 (:translate %raw-instance-ref/complex-single)
548 (:args (object :scs (descriptor-reg))
549 (index :scs (any-reg)))
550 (:arg-types * positive-fixnum)
551 (:results (value :scs (complex-single-reg)))
552 (:temporary (:scs (non-descriptor-reg)) offset)
553 (:temporary (:scs (interior-reg)) lip)
554 (:result-types complex-single-float)
556 (loadw offset object 0 instance-pointer-lowtag)
557 (inst srl offset n-widetag-bits offset)
558 (inst sll offset 2 offset)
559 (inst subq offset index offset)
560 (inst subq offset (* 2 n-word-bytes) offset)
561 (inst addq object offset lip)
563 (complex-double-reg-real-tn value)
564 (- (* instance-slots-offset n-word-bytes)
565 instance-pointer-lowtag)
568 (complex-double-reg-imag-tn value)
569 (- (* (1+ instance-slots-offset) n-word-bytes)
570 instance-pointer-lowtag)
573 (define-vop (raw-instance-set/complex-single)
574 (:translate %raw-instance-set/complex-single)
576 (:args (object :scs (descriptor-reg))
577 (index :scs (any-reg))
578 (value :scs (complex-single-reg)))
579 (:arg-types * positive-fixnum complex-single-float)
580 (:results (result :scs (complex-single-reg)))
581 (:temporary (:scs (non-descriptor-reg)) offset)
582 (:temporary (:scs (interior-reg)) lip)
583 (:result-types complex-single-float)
585 (loadw offset object 0 instance-pointer-lowtag)
586 (inst srl offset n-widetag-bits offset)
587 (inst sll offset 2 offset)
588 (inst subq offset index offset)
589 (inst subq offset (* 2 n-word-bytes) offset)
590 (inst addq object offset lip)
591 (let ((value-real (complex-single-reg-real-tn value))
592 (result-real (complex-single-reg-real-tn result)))
595 (- (* instance-slots-offset n-word-bytes)
596 instance-pointer-lowtag)
598 (unless (location= result-real value-real)
599 (inst fmove value-real result-real)))
600 (let ((value-imag (complex-single-reg-imag-tn value))
601 (result-imag (complex-single-reg-imag-tn result)))
604 (- (* (1+ instance-slots-offset) n-word-bytes)
605 instance-pointer-lowtag)
607 (unless (location= result-imag value-imag)
608 (inst fmove value-imag result-imag)))))
610 (define-vop (raw-instance-ref/complex-double)
611 (:translate %raw-instance-ref/complex-double)
613 (:args (object :scs (descriptor-reg))
614 (index :scs (any-reg)))
615 (:arg-types * positive-fixnum)
616 (:results (value :scs (complex-double-reg)))
617 (:temporary (:scs (non-descriptor-reg)) offset)
618 (:temporary (:scs (interior-reg)) lip)
619 (:result-types complex-double-float)
621 (loadw offset object 0 instance-pointer-lowtag)
622 (inst srl offset n-widetag-bits offset)
623 (inst sll offset 2 offset)
624 (inst subq offset index offset)
625 (inst subq offset (* 4 n-word-bytes) offset)
626 (inst addq object offset lip)
628 (complex-double-reg-real-tn value)
629 (- (* instance-slots-offset n-word-bytes)
630 instance-pointer-lowtag)
633 (complex-double-reg-imag-tn value)
634 (- (* (+ instance-slots-offset 2) n-word-bytes)
635 instance-pointer-lowtag)
638 (define-vop (raw-instance-set/complex-double)
639 (:translate %raw-instance-set/complex-double)
641 (:args (object :scs (descriptor-reg))
642 (index :scs (any-reg))
643 (value :scs (complex-double-reg)))
644 (:arg-types * positive-fixnum complex-double-float)
645 (:results (result :scs (complex-double-reg)))
646 (:temporary (:scs (non-descriptor-reg)) offset)
647 (:temporary (:scs (interior-reg)) lip)
648 (:result-types complex-double-float)
650 (loadw offset object 0 instance-pointer-lowtag)
651 (inst srl offset n-widetag-bits offset)
652 (inst sll offset 2 offset)
653 (inst subq offset index offset)
654 (inst subq offset (* 4 n-word-bytes) offset)
655 (inst addq object offset lip)
656 (let ((value-real (complex-double-reg-real-tn value))
657 (result-real (complex-double-reg-real-tn result)))
660 (- (* instance-slots-offset n-word-bytes)
661 instance-pointer-lowtag)
663 (unless (location= result-real value-real)
664 (inst fmove value-real result-real)))
665 (let ((value-imag (complex-double-reg-imag-tn value))
666 (result-imag (complex-double-reg-imag-tn result)))
669 (- (* (+ instance-slots-offset 2) n-word-bytes)
670 instance-pointer-lowtag)
672 (unless (location= result-imag value-imag)
673 (inst fmove value-imag result-imag)))))