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