0.9.1.38:
[sbcl.git] / src / compiler / x86 / cell.lisp
1 ;;;; various primitive memory access VOPs for the x86 VM
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; data object ref/set stuff
15
16 (define-vop (slot)
17   (:args (object :scs (descriptor-reg)))
18   (:info name offset lowtag)
19   (:ignore name)
20   (:results (result :scs (descriptor-reg any-reg)))
21   (:generator 1
22     (loadw result object offset lowtag)))
23
24 (define-vop (set-slot)
25   (:args (object :scs (descriptor-reg))
26          (value :scs (descriptor-reg any-reg immediate)))
27   (:info name offset lowtag)
28   (:ignore name)
29   (:results)
30   (:generator 1
31      (if (sc-is value immediate)
32         (let ((val (tn-value value)))
33            (etypecase val
34               (integer
35                (inst mov
36                      (make-ea :dword :base object
37                               :disp (- (* offset n-word-bytes) lowtag))
38                      (fixnumize val)))
39               (symbol
40                (inst mov
41                      (make-ea :dword :base object
42                               :disp (- (* offset n-word-bytes) lowtag))
43                      (+ nil-value (static-symbol-offset val))))
44               (character
45                (inst mov
46                      (make-ea :dword :base object
47                               :disp (- (* offset n-word-bytes) lowtag))
48                      (logior (ash (char-code val) n-widetag-bits)
49                              character-widetag)))))
50        ;; Else, value not immediate.
51        (storew value object offset lowtag))))
52 \f
53
54
55 ;;;; symbol hacking VOPs
56
57 ;;; these next two cf the sparc version, by jrd.
58 ;;; FIXME: Deref this ^ reference.
59
60
61 ;;; The compiler likes to be able to directly SET symbols.
62 #!+sb-thread
63 (define-vop (set)
64   (:args (symbol :scs (descriptor-reg))
65          (value :scs (descriptor-reg any-reg)))
66   (:temporary (:sc descriptor-reg) tls)
67   ;;(:policy :fast-safe)
68   (:generator 4
69     (let ((global-val (gen-label))
70           (done (gen-label)))
71       (loadw tls symbol symbol-tls-index-slot other-pointer-lowtag)
72       (inst or tls tls)
73       (inst jmp :z global-val)
74       (inst fs-segment-prefix)
75       (inst cmp (make-ea :dword :scale 1 :index tls) unbound-marker-widetag)
76       (inst jmp :z global-val)
77       (inst fs-segment-prefix)
78       (inst mov (make-ea :dword :scale 1 :index tls) value)
79       (inst jmp done)
80       (emit-label global-val)
81       (storew value symbol symbol-value-slot other-pointer-lowtag)
82       (emit-label done))))
83
84 ;; unithreaded it's a lot simpler ...
85 #!-sb-thread 
86 (define-vop (set cell-set)
87   (:variant symbol-value-slot other-pointer-lowtag))
88
89 ;;; Do a cell ref with an error check for being unbound.
90 ;;; XXX stil used? I can't see where -dan
91 (define-vop (checked-cell-ref)
92   (:args (object :scs (descriptor-reg) :target obj-temp))
93   (:results (value :scs (descriptor-reg any-reg)))
94   (:policy :fast-safe)
95   (:vop-var vop)
96   (:save-p :compute-only)
97   (:temporary (:sc descriptor-reg :from (:argument 0)) obj-temp))
98
99 ;;; With Symbol-Value, we check that the value isn't the trap object. So
100 ;;; Symbol-Value of NIL is NIL.
101 #!+sb-thread
102 (define-vop (symbol-value)
103   (:translate symbol-value)
104   (:policy :fast-safe)
105   (:args (object :scs (descriptor-reg) :to (:result 1)))
106   (:results (value :scs (descriptor-reg any-reg)))
107   (:vop-var vop)
108   (:save-p :compute-only)
109   (:generator 9
110     (let* ((err-lab (generate-error-code vop unbound-symbol-error object))
111            (ret-lab (gen-label)))
112       (loadw value object symbol-tls-index-slot other-pointer-lowtag)
113       (inst fs-segment-prefix)
114       (inst mov value (make-ea :dword :index value :scale 1))
115       (inst cmp value unbound-marker-widetag)
116       (inst jmp :ne ret-lab)
117       (loadw value object symbol-value-slot other-pointer-lowtag)
118       (inst cmp value unbound-marker-widetag)
119       (inst jmp :e err-lab)
120       (emit-label ret-lab))))
121
122 #!+sb-thread
123 (define-vop (fast-symbol-value symbol-value)
124   ;; KLUDGE: not really fast, in fact, because we're going to have to
125   ;; do a full lookup of the thread-local area anyway.  But half of
126   ;; the meaning of FAST-SYMBOL-VALUE is "do not signal an error if
127   ;; unbound", which is used in the implementation of COPY-SYMBOL.  --
128   ;; CSR, 2003-04-22
129   (:policy :fast)
130   (:translate symbol-value)
131   (:generator 8
132     (let ((ret-lab (gen-label)))
133       (loadw value object symbol-tls-index-slot other-pointer-lowtag)
134       (inst fs-segment-prefix)
135       (inst mov value (make-ea :dword :index value :scale 1))
136       (inst cmp value unbound-marker-widetag)
137       (inst jmp :ne ret-lab)
138       (loadw value object symbol-value-slot other-pointer-lowtag)
139       (emit-label ret-lab))))
140
141 #!-sb-thread
142 (define-vop (symbol-value)
143   (:translate symbol-value)
144   (:policy :fast-safe)
145   (:args (object :scs (descriptor-reg) :to (:result 1)))
146   (:results (value :scs (descriptor-reg any-reg)))
147   (:vop-var vop)
148   (:save-p :compute-only)
149   (:generator 9
150     (let ((err-lab (generate-error-code vop unbound-symbol-error object)))
151       (loadw value object symbol-value-slot other-pointer-lowtag)
152       (inst cmp value unbound-marker-widetag)
153       (inst jmp :e err-lab))))
154
155 #!-sb-thread
156 (define-vop (fast-symbol-value cell-ref)
157   (:variant symbol-value-slot other-pointer-lowtag)
158   (:policy :fast)
159   (:translate symbol-value))
160
161 (defknown locked-symbol-global-value-add (symbol fixnum) fixnum ())
162
163 (define-vop (locked-symbol-global-value-add)
164     (:args (object :scs (descriptor-reg) :to :result)
165            (value :scs (any-reg) :target result))
166   (:arg-types * tagged-num)
167   (:results (result :scs (any-reg) :from (:argument 1)))
168   (:policy :fast)
169   (:translate locked-symbol-global-value-add)
170   (:result-types tagged-num)
171   (:policy :fast-safe)
172   (:generator 4
173     (move result value)
174     (inst lock)
175     (inst add (make-ea :dword :base object
176                        :disp (- (* symbol-value-slot n-word-bytes)
177                                 other-pointer-lowtag))
178           value)))
179
180 #!+sb-thread
181 (define-vop (boundp)
182   (:translate boundp)
183   (:policy :fast-safe)
184   (:args (object :scs (descriptor-reg)))
185   (:conditional)
186   (:info target not-p)
187   (:temporary (:sc descriptor-reg #+nil(:from (:argument 0))) value)
188   (:generator 9
189     (if not-p
190         (let ((not-target (gen-label)))
191           (loadw value object symbol-value-slot other-pointer-lowtag)
192           (inst cmp value unbound-marker-widetag)
193           (inst jmp :ne not-target)
194           (loadw value object symbol-tls-index-slot other-pointer-lowtag)
195           (inst fs-segment-prefix)
196           (inst cmp (make-ea :dword :index value :scale 1) unbound-marker-widetag)
197           (inst jmp  :e  target)
198           (emit-label not-target))
199         (progn
200           (loadw value object symbol-value-slot other-pointer-lowtag)
201           (inst cmp value unbound-marker-widetag)
202           (inst jmp :ne target)
203           (loadw value object symbol-tls-index-slot other-pointer-lowtag)
204           (inst fs-segment-prefix)
205           (inst cmp (make-ea :dword :index value :scale 1) unbound-marker-widetag)
206           (inst jmp  :ne  target)))))
207
208 #!-sb-thread
209 (define-vop (boundp)
210   (:translate boundp)
211   (:policy :fast-safe)
212   (:args (object :scs (descriptor-reg)))
213   (:conditional)
214   (:info target not-p)
215   (:temporary (:sc descriptor-reg :from (:argument 0)) value)
216   (:generator 9
217     (loadw value object symbol-value-slot other-pointer-lowtag)
218     (inst cmp value unbound-marker-widetag)
219     (inst jmp (if not-p :e :ne) target)))
220
221
222 (define-vop (symbol-hash)
223   (:policy :fast-safe)
224   (:translate symbol-hash)
225   (:args (symbol :scs (descriptor-reg)))
226   (:results (res :scs (any-reg)))
227   (:result-types positive-fixnum)
228   (:generator 2
229     ;; The symbol-hash slot of NIL holds NIL because it is also the
230     ;; cdr slot, so we have to strip off the two low bits to make sure
231     ;; it is a fixnum.  The lowtag selection magic that is required to
232     ;; ensure this is explained in the comment in objdef.lisp
233     (loadw res symbol symbol-hash-slot other-pointer-lowtag)
234     (inst and res (lognot #b11))))
235 \f
236 ;;;; fdefinition (FDEFN) objects
237
238 (define-vop (fdefn-fun cell-ref)        ; /pfw - alpha
239   (:variant fdefn-fun-slot other-pointer-lowtag))
240
241 (define-vop (safe-fdefn-fun)
242   (:args (object :scs (descriptor-reg) :to (:result 1)))
243   (:results (value :scs (descriptor-reg any-reg)))
244   (:vop-var vop)
245   (:save-p :compute-only)
246   (:generator 10
247     (loadw value object fdefn-fun-slot other-pointer-lowtag)
248     (inst cmp value nil-value)
249     (let ((err-lab (generate-error-code vop undefined-fun-error object)))
250       (inst jmp :e err-lab))))
251
252 (define-vop (set-fdefn-fun)
253   (:policy :fast-safe)
254   (:translate (setf fdefn-fun))
255   (:args (function :scs (descriptor-reg) :target result)
256          (fdefn :scs (descriptor-reg)))
257   (:temporary (:sc unsigned-reg) raw)
258   (:temporary (:sc byte-reg) type)
259   (:results (result :scs (descriptor-reg)))
260   (:generator 38
261     (load-type type function (- fun-pointer-lowtag))
262     (inst lea raw
263           (make-ea :byte :base function
264                    :disp (- (* simple-fun-code-offset n-word-bytes)
265                             fun-pointer-lowtag)))
266     (inst cmp type simple-fun-header-widetag)
267     (inst jmp :e normal-fn)
268     (inst lea raw (make-fixup "closure_tramp" :foreign))
269     NORMAL-FN
270     (storew function fdefn fdefn-fun-slot other-pointer-lowtag)
271     (storew raw fdefn fdefn-raw-addr-slot other-pointer-lowtag)
272     (move result function)))
273
274 (define-vop (fdefn-makunbound)
275   (:policy :fast-safe)
276   (:translate fdefn-makunbound)
277   (:args (fdefn :scs (descriptor-reg) :target result))
278   (:results (result :scs (descriptor-reg)))
279   (:generator 38
280     (storew nil-value fdefn fdefn-fun-slot other-pointer-lowtag)
281     (storew (make-fixup "undefined_tramp" :foreign)
282             fdefn fdefn-raw-addr-slot other-pointer-lowtag)
283     (move result fdefn)))
284 \f
285 ;;;; binding and unbinding
286
287 ;;; BIND -- Establish VAL as a binding for SYMBOL. Save the old value and
288 ;;; the symbol on the binding stack and stuff the new value into the
289 ;;; symbol.
290
291 #!+sb-thread
292 (define-vop (bind)
293   (:args (val :scs (any-reg descriptor-reg))
294          (symbol :scs (descriptor-reg)))
295   (:temporary (:sc unsigned-reg) tls-index temp bsp)
296   (:generator 5
297     (let ((tls-index-valid (gen-label)))
298       (load-tl-symbol-value bsp *binding-stack-pointer*)
299       (loadw tls-index symbol symbol-tls-index-slot other-pointer-lowtag)
300       (inst add bsp (* binding-size n-word-bytes))
301       (store-tl-symbol-value bsp *binding-stack-pointer* temp)
302       
303       (inst or tls-index tls-index)
304       (inst jmp :ne tls-index-valid)
305       ;; allocate a new tls-index
306       (load-symbol-value tls-index *free-tls-index*)
307       (inst add tls-index 4)            ;XXX surely we can do this more
308       (store-symbol-value tls-index *free-tls-index*) ;succintly
309       (inst sub tls-index 4)
310       (storew tls-index symbol symbol-tls-index-slot other-pointer-lowtag)
311       (emit-label tls-index-valid)
312       (inst fs-segment-prefix) 
313       (inst mov temp (make-ea :dword :scale 1 :index tls-index))
314       (storew temp bsp (- binding-value-slot binding-size))
315       (storew symbol bsp (- binding-symbol-slot binding-size))
316       (inst fs-segment-prefix)
317       (inst mov (make-ea :dword :scale 1 :index tls-index) val))))
318
319 #!-sb-thread
320 (define-vop (bind)
321   (:args (val :scs (any-reg descriptor-reg))
322          (symbol :scs (descriptor-reg)))
323   (:temporary (:sc unsigned-reg) temp bsp)
324   (:generator 5
325     (load-symbol-value bsp *binding-stack-pointer*)
326     (loadw temp symbol symbol-value-slot other-pointer-lowtag)
327     (inst add bsp (* binding-size n-word-bytes))
328     (store-symbol-value bsp *binding-stack-pointer*)
329     (storew temp bsp (- binding-value-slot binding-size))
330     (storew symbol bsp (- binding-symbol-slot binding-size))
331     (storew val symbol symbol-value-slot other-pointer-lowtag)))
332
333
334 #!+sb-thread
335 (define-vop (unbind)
336     ;; four temporaries?   
337   (:temporary (:sc unsigned-reg) symbol value bsp tls-index)
338   (:generator 0
339     (load-tl-symbol-value bsp *binding-stack-pointer*)
340     (loadw symbol bsp (- binding-symbol-slot binding-size))
341     (loadw value bsp (- binding-value-slot binding-size))
342
343     (loadw tls-index symbol symbol-tls-index-slot other-pointer-lowtag)    
344     (inst fs-segment-prefix)
345     (inst mov (make-ea :dword :scale 1 :index tls-index) value)
346
347     (storew 0 bsp (- binding-symbol-slot binding-size))
348     (inst sub bsp (* binding-size n-word-bytes))
349     ;; we're done with value, so we can use it as a temp here
350     (store-tl-symbol-value bsp *binding-stack-pointer* value)))
351
352 #!-sb-thread
353 (define-vop (unbind)
354   (:temporary (:sc unsigned-reg) symbol value bsp)
355   (:generator 0
356     (load-symbol-value bsp *binding-stack-pointer*)
357     (loadw symbol bsp (- binding-symbol-slot binding-size))
358     (loadw value bsp (- binding-value-slot binding-size))
359     (storew value symbol symbol-value-slot other-pointer-lowtag)
360     (storew 0 bsp (- binding-symbol-slot binding-size))
361     (inst sub bsp (* binding-size n-word-bytes))
362     (store-symbol-value bsp *binding-stack-pointer*)))
363
364
365 (define-vop (unbind-to-here)
366   (:args (where :scs (descriptor-reg any-reg)))
367   (:temporary (:sc unsigned-reg) symbol value bsp #!+sb-thread tls-index)
368   (:generator 0
369     (load-tl-symbol-value bsp *binding-stack-pointer*)
370     (inst cmp where bsp)
371     (inst jmp :e done)
372
373     LOOP
374     (loadw symbol bsp (- binding-symbol-slot binding-size))
375     (inst or symbol symbol)
376     (inst jmp :z skip)
377     (loadw value bsp (- binding-value-slot binding-size))
378     #!-sb-thread (storew value symbol symbol-value-slot other-pointer-lowtag)
379
380     #!+sb-thread (loadw
381                   tls-index symbol symbol-tls-index-slot other-pointer-lowtag)
382     #!+sb-thread (inst fs-segment-prefix)
383     #!+sb-thread (inst mov (make-ea :dword :scale 1 :index tls-index) value)
384     (storew 0 bsp (- binding-symbol-slot binding-size))
385
386     SKIP
387     (inst sub bsp (* binding-size n-word-bytes))
388     (inst cmp where bsp)
389     (inst jmp :ne loop)
390     ;; we're done with value, so can use it as a temporary
391     (store-tl-symbol-value bsp *binding-stack-pointer* value)
392
393     DONE))
394 \f
395
396 \f
397 ;;;; closure indexing
398
399 (define-full-reffer closure-index-ref *
400   closure-info-offset fun-pointer-lowtag
401   (any-reg descriptor-reg) * %closure-index-ref)
402
403 (define-full-setter set-funcallable-instance-info *
404   funcallable-instance-info-offset fun-pointer-lowtag
405   (any-reg descriptor-reg) * %set-funcallable-instance-info)
406
407 (define-full-reffer funcallable-instance-info *
408   funcallable-instance-info-offset fun-pointer-lowtag
409   (descriptor-reg any-reg) * %funcallable-instance-info)
410
411 (define-vop (funcallable-instance-lexenv cell-ref)
412   (:variant funcallable-instance-lexenv-slot fun-pointer-lowtag))
413
414 (define-vop (closure-ref slot-ref)
415   (:variant closure-info-offset fun-pointer-lowtag))
416
417 (define-vop (closure-init slot-set)
418   (:variant closure-info-offset fun-pointer-lowtag))
419 \f
420 ;;;; value cell hackery
421
422 (define-vop (value-cell-ref cell-ref)
423   (:variant value-cell-value-slot other-pointer-lowtag))
424
425 (define-vop (value-cell-set cell-set)
426   (:variant value-cell-value-slot other-pointer-lowtag))
427 \f
428 ;;;; structure hackery
429
430 (define-vop (instance-length)
431   (:policy :fast-safe)
432   (:translate %instance-length)
433   (:args (struct :scs (descriptor-reg)))
434   (:results (res :scs (unsigned-reg)))
435   (:result-types positive-fixnum)
436   (:generator 4
437     (loadw res struct 0 instance-pointer-lowtag)
438     (inst shr res n-widetag-bits)))
439
440 (define-vop (instance-ref slot-ref)
441   (:variant instance-slots-offset instance-pointer-lowtag)
442   (:policy :fast-safe)
443   (:translate %instance-ref)
444   (:arg-types instance (:constant index)))
445
446 (define-vop (instance-set slot-set)
447   (:policy :fast-safe)
448   (:translate %instance-set)
449   (:variant instance-slots-offset instance-pointer-lowtag)
450   (:arg-types instance (:constant index) *))
451
452 (define-full-reffer instance-index-ref * instance-slots-offset
453   instance-pointer-lowtag (any-reg descriptor-reg) * %instance-ref)
454
455 (define-full-setter instance-index-set * instance-slots-offset
456   instance-pointer-lowtag (any-reg descriptor-reg) * %instance-set)
457
458
459 (defknown %instance-set-conditional (instance index t t) t
460           (unsafe))
461
462 (define-vop (instance-set-conditional)
463   (:translate %instance-set-conditional)
464   (:args (object :scs (descriptor-reg) :to :eval)
465          (slot :scs (any-reg) :to :result)
466          (old-value :scs (descriptor-reg any-reg) :target eax)
467          (new-value :scs (descriptor-reg any-reg)))
468   (:arg-types instance positive-fixnum * *)
469   (:temporary (:sc descriptor-reg :offset eax-offset
470                    :from (:argument 2) :to :result :target result)  eax)
471   (:results (result :scs (descriptor-reg any-reg)))
472   ;(:guard (backend-featurep :i486))
473   (:policy :fast-safe)
474   (:generator 5
475     (move eax old-value)
476     (inst lock)
477     (inst cmpxchg (make-ea :dword :base object :index slot :scale 1
478                            :disp (- (* instance-slots-offset n-word-bytes)
479                                     instance-pointer-lowtag))
480           new-value)
481     (move result eax)))
482
483
484 \f
485 ;;;; code object frobbing
486
487 (define-full-reffer code-header-ref * 0 other-pointer-lowtag
488   (any-reg descriptor-reg) * code-header-ref)
489
490 (define-full-setter code-header-set * 0 other-pointer-lowtag
491   (any-reg descriptor-reg) * code-header-set)
492
493
494 \f
495 ;;;; raw instance slot accessors
496
497 (define-vop (raw-instance-ref/word)
498   (:translate %raw-instance-ref/word)
499   (:policy :fast-safe)
500   (:args (object :scs (descriptor-reg)) (index :scs (any-reg)))
501   (:arg-types * tagged-num)
502   (:temporary (:sc unsigned-reg) tmp)
503   (:results (value :scs (unsigned-reg)))
504   (:result-types unsigned-num)
505   (:generator 5
506     (loadw tmp object 0 instance-pointer-lowtag)
507     (inst shr tmp n-widetag-bits)
508     (inst shl tmp 2)
509     (inst sub tmp index)
510     (inst mov
511           value
512           (make-ea :dword
513                    :base object
514                    :index tmp
515                    :disp (- (* (1- instance-slots-offset) n-word-bytes)
516                             instance-pointer-lowtag)))))
517
518 (define-vop (raw-instance-set/word)
519   (:translate %raw-instance-set/word)
520   (:policy :fast-safe)
521   (:args (object :scs (descriptor-reg))
522          (index :scs (any-reg))
523          (value :scs (unsigned-reg) :target result))
524   (:arg-types * tagged-num unsigned-num)
525   (:temporary (:sc unsigned-reg) tmp)
526   (:results (result :scs (unsigned-reg)))
527   (:result-types unsigned-num)
528   (:generator 5
529     (loadw tmp object 0 instance-pointer-lowtag)
530     (inst shr tmp n-widetag-bits)
531     (inst shl tmp 2)
532     (inst sub tmp index)
533     (inst mov
534           (make-ea :dword
535                    :base object
536                    :index tmp
537                    :disp (- (* (1- instance-slots-offset) n-word-bytes)
538                             instance-pointer-lowtag))
539           value)
540     (move result value)))
541
542 (define-vop (raw-instance-ref/single)
543   (:translate %raw-instance-ref/single)
544   (:policy :fast-safe)
545   (:args (object :scs (descriptor-reg)) (index :scs (any-reg)))
546   (:arg-types * tagged-num)
547   (:temporary (:sc unsigned-reg) tmp)
548   (:results (value :scs (single-reg)))
549   (:result-types single-float)
550   (:generator 5
551     (loadw tmp object 0 instance-pointer-lowtag)
552     (inst shr tmp n-widetag-bits)
553     (inst shl tmp 2)
554     (inst sub tmp index)
555     (with-empty-tn@fp-top(value)
556       (inst fld
557             (make-ea :dword
558                      :base object
559                      :index tmp
560                      :disp (- (* (1- instance-slots-offset) n-word-bytes)
561                               instance-pointer-lowtag))))))
562
563 (define-vop (raw-instance-set/single)
564   (:translate %raw-instance-set/single)
565   (:policy :fast-safe)
566   (:args (object :scs (descriptor-reg))
567          (index :scs (any-reg))
568          (value :scs (single-reg) :target result))
569   (:arg-types * tagged-num single-float)
570   (:temporary (:sc unsigned-reg) tmp)
571   (:results (result :scs (single-reg)))
572   (:result-types single-float)
573   (:generator 5
574     (loadw tmp object 0 instance-pointer-lowtag)
575     (inst shr tmp n-widetag-bits)
576     (inst shl tmp 2)
577     (inst sub tmp index)
578     (unless (zerop (tn-offset value))
579       (inst fxch value))
580     (inst fst
581           (make-ea :dword
582                    :base object
583                    :index tmp
584                    :disp (- (* (1- instance-slots-offset) n-word-bytes)
585                             instance-pointer-lowtag)))
586     (cond
587       ((zerop (tn-offset value))
588         (unless (zerop (tn-offset result))
589           (inst fst result)))
590       ((zerop (tn-offset result))
591         (inst fst value))
592       (t
593         (unless (location= value result)
594           (inst fst result))
595         (inst fxch value)))))
596
597 (define-vop (raw-instance-ref/double)
598   (:translate %raw-instance-ref/double)
599   (:policy :fast-safe)
600   (:args (object :scs (descriptor-reg)) (index :scs (any-reg)))
601   (:arg-types * tagged-num)
602   (:temporary (:sc unsigned-reg) tmp)
603   (:results (value :scs (double-reg)))
604   (:result-types double-float)
605   (:generator 5
606     (loadw tmp object 0 instance-pointer-lowtag)
607     (inst shr tmp n-widetag-bits)
608     (inst shl tmp 2)
609     (inst sub tmp index)
610     (with-empty-tn@fp-top(value)
611       (inst fldd
612             (make-ea :dword
613                      :base object
614                      :index tmp
615                      :disp (- (* (- instance-slots-offset 2) n-word-bytes)
616                               instance-pointer-lowtag))))))
617
618 (define-vop (raw-instance-set/double)
619   (:translate %raw-instance-set/double)
620   (:policy :fast-safe)
621   (:args (object :scs (descriptor-reg))
622          (index :scs (any-reg))
623          (value :scs (double-reg) :target result))
624   (:arg-types * tagged-num double-float)
625   (:temporary (:sc unsigned-reg) tmp)
626   (:results (result :scs (double-reg)))
627   (:result-types double-float)
628   (:generator 5
629     (loadw tmp object 0 instance-pointer-lowtag)
630     (inst shr tmp n-widetag-bits)
631     (inst shl tmp 2)
632     (inst sub tmp index)
633     (unless (zerop (tn-offset value))
634       (inst fxch value))
635     (inst fstd
636           (make-ea :dword
637                    :base object
638                    :index tmp
639                    :disp (- (* (- instance-slots-offset 2) n-word-bytes)
640                             instance-pointer-lowtag)))
641     (cond
642       ((zerop (tn-offset value))
643         (unless (zerop (tn-offset result))
644           (inst fstd result)))
645       ((zerop (tn-offset result))
646         (inst fstd value))
647       (t
648         (unless (location= value result)
649           (inst fstd result))
650         (inst fxch value)))))
651
652 (define-vop (raw-instance-ref/complex-single)
653   (:translate %raw-instance-ref/complex-single)
654   (:policy :fast-safe)
655   (:args (object :scs (descriptor-reg))
656          (index :scs (any-reg)))
657   (:arg-types * positive-fixnum)
658   (:temporary (:sc unsigned-reg) tmp)
659   (:results (value :scs (complex-single-reg)))
660   (:result-types complex-single-float)
661   (:generator 5
662     (loadw tmp object 0 instance-pointer-lowtag)
663     (inst shr tmp n-widetag-bits)
664     (inst shl tmp 2)
665     (inst sub tmp index)
666     (let ((real-tn (complex-single-reg-real-tn value)))
667       (with-empty-tn@fp-top (real-tn)
668         (inst fld (make-ea :dword
669                            :base object
670                            :index tmp
671                            :disp (- (* (- instance-slots-offset 2)
672                                        n-word-bytes)
673                                     instance-pointer-lowtag)))))
674     (let ((imag-tn (complex-single-reg-imag-tn value)))
675       (with-empty-tn@fp-top (imag-tn)
676         (inst fld (make-ea :dword
677                            :base object
678                            :index tmp
679                            :disp (- (* (1- instance-slots-offset)
680                                        n-word-bytes)
681                                     instance-pointer-lowtag)))))))
682
683 (define-vop (raw-instance-set/complex-single)
684   (:translate %raw-instance-set/complex-single)
685   (:policy :fast-safe)
686   (:args (object :scs (descriptor-reg))
687          (index :scs (any-reg))
688          (value :scs (complex-single-reg) :target result))
689   (:arg-types * positive-fixnum complex-single-float)
690   (:temporary (:sc unsigned-reg) tmp)
691   (:results (result :scs (complex-single-reg)))
692   (:result-types complex-single-float)
693   (:generator 5
694     (loadw tmp object 0 instance-pointer-lowtag)
695     (inst shr tmp n-widetag-bits)
696     (inst shl tmp 2)
697     (inst sub tmp index)
698     (let ((value-real (complex-single-reg-real-tn value))
699           (result-real (complex-single-reg-real-tn result)))
700       (cond ((zerop (tn-offset value-real))
701              ;; Value is in ST0.
702              (inst fst (make-ea :dword
703                                 :base object
704                                 :index tmp
705                                 :disp (- (* (- instance-slots-offset 2)
706                                             n-word-bytes)
707                                          instance-pointer-lowtag)))
708              (unless (zerop (tn-offset result-real))
709                ;; Value is in ST0 but not result.
710                (inst fst result-real)))
711             (t
712              ;; Value is not in ST0.
713              (inst fxch value-real)
714              (inst fst (make-ea :dword
715                                 :base object
716                                 :index tmp
717                                 :disp (- (* (- instance-slots-offset 2)
718                                             n-word-bytes)
719                                          instance-pointer-lowtag)))
720              (cond ((zerop (tn-offset result-real))
721                     ;; The result is in ST0.
722                     (inst fst value-real))
723                    (t
724                     ;; Neither value or result are in ST0
725                     (unless (location= value-real result-real)
726                       (inst fst result-real))
727                     (inst fxch value-real))))))
728     (let ((value-imag (complex-single-reg-imag-tn value))
729           (result-imag (complex-single-reg-imag-tn result)))
730       (inst fxch value-imag)
731       (inst fst (make-ea :dword
732                          :base object
733                          :index tmp
734                          :disp (- (* (1- instance-slots-offset)
735                                      n-word-bytes)
736                                   instance-pointer-lowtag)))
737       (unless (location= value-imag result-imag)
738         (inst fst result-imag))
739       (inst fxch value-imag))))
740
741 (define-vop (raw-instance-ref/complex-double)
742   (:translate %raw-instance-ref/complex-double)
743   (:policy :fast-safe)
744   (:args (object :scs (descriptor-reg))
745          (index :scs (any-reg)))
746   (:arg-types * positive-fixnum)
747   (:temporary (:sc unsigned-reg) tmp)
748   (:results (value :scs (complex-double-reg)))
749   (:result-types complex-double-float)
750   (:generator 7
751     (loadw tmp object 0 instance-pointer-lowtag)
752     (inst shr tmp n-widetag-bits)
753     (inst shl tmp 2)
754     (inst sub tmp index)
755     (let ((real-tn (complex-double-reg-real-tn value)))
756       (with-empty-tn@fp-top (real-tn)
757         (inst fldd (make-ea :dword
758                             :base object
759                             :index tmp
760                             :disp (- (* (- instance-slots-offset 4)
761                                         n-word-bytes)
762                                      instance-pointer-lowtag)))))
763     (let ((imag-tn (complex-double-reg-imag-tn value)))
764       (with-empty-tn@fp-top (imag-tn)
765         (inst fldd (make-ea :dword
766                             :base object
767                             :index tmp
768                             :disp (- (* (- instance-slots-offset 2)
769                                         n-word-bytes)
770                                      instance-pointer-lowtag)))))))
771
772 (define-vop (raw-instance-set/complex-double)
773   (:translate %raw-instance-set/complex-double)
774   (:policy :fast-safe)
775   (:args (object :scs (descriptor-reg))
776          (index :scs (any-reg))
777          (value :scs (complex-double-reg) :target result))
778   (:arg-types * positive-fixnum complex-double-float)
779   (:temporary (:sc unsigned-reg) tmp)
780   (:results (result :scs (complex-double-reg)))
781   (:result-types complex-double-float)
782   (:generator 20
783     (loadw tmp object 0 instance-pointer-lowtag)
784     (inst shr tmp n-widetag-bits)
785     (inst shl tmp 2)
786     (inst sub tmp index)
787     (let ((value-real (complex-double-reg-real-tn value))
788           (result-real (complex-double-reg-real-tn result)))
789       (cond ((zerop (tn-offset value-real))
790              ;; Value is in ST0.
791              (inst fstd (make-ea :dword
792                                  :base object
793                                  :index tmp
794                                  :disp (- (* (- instance-slots-offset 4)
795                                              n-word-bytes)
796                                           instance-pointer-lowtag)))
797              (unless (zerop (tn-offset result-real))
798                ;; Value is in ST0 but not result.
799                (inst fstd result-real)))
800             (t
801              ;; Value is not in ST0.
802              (inst fxch value-real)
803              (inst fstd (make-ea :dword
804                                  :base object
805                                  :index tmp
806                                  :disp (- (* (- instance-slots-offset 4)
807                                              n-word-bytes)
808                                           instance-pointer-lowtag)))
809              (cond ((zerop (tn-offset result-real))
810                     ;; The result is in ST0.
811                     (inst fstd value-real))
812                    (t
813                     ;; Neither value or result are in ST0
814                     (unless (location= value-real result-real)
815                       (inst fstd result-real))
816                     (inst fxch value-real))))))
817     (let ((value-imag (complex-double-reg-imag-tn value))
818           (result-imag (complex-double-reg-imag-tn result)))
819       (inst fxch value-imag)
820       (inst fstd (make-ea :dword
821                           :base object
822                           :index tmp
823                           :disp (- (* (- instance-slots-offset 2)
824                                       n-word-bytes)
825                                    instance-pointer-lowtag)))
826       (unless (location= value-imag result-imag)
827         (inst fstd result-imag))
828       (inst fxch value-imag))))