423a0b967c2413c21f11f1e8820a11f5ffc2e905
[sbcl.git] / src / compiler / x86-64 / move.lisp
1 ;;;; the x86-64 VM definition of operand loading/saving and the MOVE vop
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
14 (defun make-byte-tn (tn)
15   (aver (sc-is tn any-reg descriptor-reg unsigned-reg signed-reg))
16   (make-random-tn :kind :normal
17                   :sc (sc-or-lose 'byte-reg)
18                   :offset (tn-offset tn)))
19
20 (defun make-dword-tn (tn)
21   (aver (sc-is tn any-reg descriptor-reg character-reg
22                unsigned-reg signed-reg))
23   (make-random-tn :kind :normal
24                   :sc (sc-or-lose 'dword-reg)
25                   :offset (tn-offset tn)))
26
27 (defun zeroize (tn)
28   (let ((offset (tn-offset tn)))
29     ;; Using the 32-bit instruction accomplishes the same thing and is
30     ;; one byte shorter.
31     (if (<= offset edi-offset)
32         (let ((tn (make-random-tn :kind :normal
33                                   :sc (sc-or-lose 'dword-reg)
34                                   :offset offset)))
35           (inst xor tn tn))
36         (inst xor tn tn))))
37
38 (define-move-fun (load-immediate 1) (vop x y)
39   ((immediate)
40    (any-reg descriptor-reg))
41   (let ((val (tn-value x)))
42     (etypecase val
43       (integer
44        (if (zerop val)
45            (zeroize y)
46          (inst mov y (fixnumize val))))
47       (symbol
48        (load-symbol y val))
49       (character
50        (inst mov y (logior (ash (char-code val) n-widetag-bits)
51                            character-widetag))))))
52
53 (define-move-fun (load-number 1) (vop x y)
54   ((immediate) (signed-reg unsigned-reg))
55   (let ((val (tn-value x)))
56     (if (zerop val)
57         (zeroize y)
58         (inst mov y val))))
59
60 (define-move-fun (load-character 1) (vop x y)
61   ((immediate) (character-reg))
62   (inst mov y (char-code (tn-value x))))
63
64 (define-move-fun (load-system-area-pointer 1) (vop x y)
65   ((immediate) (sap-reg))
66   (inst mov y (sap-int (tn-value x))))
67
68 (define-move-fun (load-constant 5) (vop x y)
69   ((constant) (descriptor-reg any-reg))
70   (inst mov y x))
71
72 (define-move-fun (load-stack 5) (vop x y)
73   ((control-stack) (any-reg descriptor-reg)
74    (character-stack) (character-reg)
75    (sap-stack) (sap-reg)
76    (signed-stack) (signed-reg)
77    (unsigned-stack) (unsigned-reg))
78   (inst mov y x))
79
80 (define-move-fun (store-stack 5) (vop x y)
81   ((any-reg descriptor-reg) (control-stack)
82    (character-reg) (character-stack)
83    (sap-reg) (sap-stack)
84    (signed-reg) (signed-stack)
85    (unsigned-reg) (unsigned-stack))
86   (inst mov y x))
87 \f
88 ;;;; the MOVE VOP
89 (define-vop (move)
90   (:args (x :scs (any-reg descriptor-reg immediate) :target y
91             :load-if (not (location= x y))))
92   (:results (y :scs (any-reg descriptor-reg)
93                :load-if
94                (not (or (location= x y)
95                         (and (sc-is x any-reg descriptor-reg immediate)
96                              (sc-is y control-stack))))))
97   (:temporary (:sc unsigned-reg) temp)
98   (:effects)
99   (:affected)
100   (:generator 0
101     (if (and (sc-is x immediate)
102              (sc-is y any-reg descriptor-reg control-stack))
103         (let ((val (tn-value x)))
104           (etypecase val
105             (integer
106              (if (and (zerop val) (sc-is y any-reg descriptor-reg))
107                  (zeroize y)
108                  (move-immediate y (fixnumize val) temp)))
109             (symbol
110              (inst mov y (+ nil-value (static-symbol-offset val))))
111             (character
112              (inst mov y (logior (ash (char-code val) n-widetag-bits)
113                                  character-widetag)))))
114         (move y x))))
115
116 (define-move-vop move :move
117   (any-reg descriptor-reg immediate)
118   (any-reg descriptor-reg))
119
120 ;;; Make MOVE the check VOP for T so that type check generation
121 ;;; doesn't think it is a hairy type. This also allows checking of a
122 ;;; few of the values in a continuation to fall out.
123 (primitive-type-vop move (:check) t)
124
125 (defun move-immediate (target val &optional tmp-tn)
126   (cond
127     ;; If target is a register, we can just mov it there directly
128     ((and (tn-p target)
129           (sc-is target signed-reg unsigned-reg descriptor-reg any-reg))
130      (inst mov target val))
131     ;; Likewise if the value is small enough.
132     ((typep val '(signed-byte 32))
133      (inst mov target val))
134     ;; Otherwise go through the temporary register
135     (tmp-tn
136      (inst mov tmp-tn val)
137      (inst mov target tmp-tn))
138     (t
139      (error "~A is not a register, no temporary given, and immediate ~A too large" target val))))
140
141 ;;; The MOVE-ARG VOP is used for moving descriptor values into
142 ;;; another frame for argument or known value passing.
143 ;;;
144 ;;; Note: It is not going to be possible to move a constant directly
145 ;;; to another frame, except if the destination is a register and in
146 ;;; this case the loading works out.
147 (define-vop (move-arg)
148   (:args (x :scs (any-reg descriptor-reg immediate) :target y
149             :load-if (not (and (sc-is y any-reg descriptor-reg)
150                                (sc-is x control-stack))))
151          (fp :scs (any-reg)
152              :load-if (not (sc-is y any-reg descriptor-reg))))
153   (:results (y))
154   (:generator 0
155     (sc-case y
156       ((any-reg descriptor-reg)
157        (if (sc-is x immediate)
158            (let ((val (tn-value x)))
159              (etypecase val
160                ((integer 0 0)
161                 (zeroize y))
162                (integer
163                 (inst mov y (fixnumize val)))
164                (symbol
165                 (load-symbol y val))
166                (character
167                 (inst mov y (logior (ash (char-code val) n-widetag-bits)
168                                     character-widetag)))))
169            (move y x)))
170       ((control-stack)
171        (if (sc-is x immediate)
172            (let ((val (tn-value x)))
173              (if (= (tn-offset fp) esp-offset)
174                  ;; C-call
175                  (etypecase val
176                    (integer
177                     (storew (fixnumize val) fp (tn-offset y)))
178                    (symbol
179                     (storew (+ nil-value (static-symbol-offset val))
180                             fp (tn-offset y)))
181                    (character
182                     (storew (logior (ash (char-code val) n-widetag-bits)
183                                     character-widetag)
184                             fp (tn-offset y))))
185                ;; Lisp stack
186                (etypecase val
187                  (integer
188                   (storew (fixnumize val) fp (frame-word-offset (tn-offset y))))
189                  (symbol
190                   (storew (+ nil-value (static-symbol-offset val))
191                           fp (frame-word-offset (tn-offset y))))
192                  (character
193                   (storew (logior (ash (char-code val) n-widetag-bits)
194                                   character-widetag)
195                           fp (frame-word-offset (tn-offset y)))))))
196          (if (= (tn-offset fp) esp-offset)
197              ;; C-call
198              (storew x fp (tn-offset y))
199            ;; Lisp stack
200            (storew x fp (frame-word-offset (tn-offset y)))))))))
201
202 (define-move-vop move-arg :move-arg
203   (any-reg descriptor-reg)
204   (any-reg descriptor-reg))
205 \f
206 ;;;; ILLEGAL-MOVE
207
208 ;;; This VOP exists just to begin the lifetime of a TN that couldn't
209 ;;; be written legally due to a type error. An error is signalled
210 ;;; before this VOP is so we don't need to do anything (not that there
211 ;;; would be anything sensible to do anyway.)
212 (define-vop (illegal-move)
213   (:args (x) (type))
214   (:results (y))
215   (:ignore y)
216   (:vop-var vop)
217   (:save-p :compute-only)
218   (:generator 666
219     (error-call vop 'object-not-type-error x type)))
220 \f
221 ;;;; moves and coercions
222
223 ;;; These MOVE-TO-WORD VOPs move a tagged integer to a raw full-word
224 ;;; representation. Similarly, the MOVE-FROM-WORD VOPs converts a raw
225 ;;; integer to a tagged bignum or fixnum.
226
227 ;;; Arg is a fixnum, so just shift it. We need a type restriction
228 ;;; because some possible arg SCs (control-stack) overlap with
229 ;;; possible bignum arg SCs.
230 (define-vop (move-to-word/fixnum)
231   (:args (x :scs (any-reg descriptor-reg) :target y
232             :load-if (not (location= x y))))
233   (:results (y :scs (signed-reg unsigned-reg)
234                :load-if (not (location= x y))))
235   (:arg-types tagged-num)
236   (:note "fixnum untagging")
237   (:generator 1
238     (move y x)
239     (inst sar y n-fixnum-tag-bits)))
240 (define-move-vop move-to-word/fixnum :move
241   (any-reg descriptor-reg) (signed-reg unsigned-reg))
242
243 ;;; Arg is a non-immediate constant, load it.
244 (define-vop (move-to-word-c)
245   (:args (x :scs (constant)))
246   (:results (y :scs (signed-reg unsigned-reg)))
247   (:note "constant load")
248   (:generator 1
249     (cond ((sb!c::tn-leaf x)
250            (inst mov y (tn-value x)))
251           (t
252            (inst mov y x)
253            (inst sar y n-fixnum-tag-bits)))))
254 (define-move-vop move-to-word-c :move
255   (constant) (signed-reg unsigned-reg))
256
257
258 ;;; Arg is a fixnum or bignum, figure out which and load if necessary.
259 #-#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
260 (define-vop (move-to-word/integer)
261   (:args (x :scs (descriptor-reg) :target rax))
262   (:results (y :scs (signed-reg unsigned-reg)))
263   (:note "integer to untagged word coercion")
264   ;; I'm not convinced that increasing the demand for rAX is
265   ;; better than adding 1 byte to some instruction encodings.
266   ;; I'll leave it alone though.
267   (:temporary (:sc unsigned-reg :offset rax-offset
268                :from (:argument 0) :to (:result 0) :target y) rax)
269   (:generator 4
270     (move rax x)
271     (inst test al-tn fixnum-tag-mask)
272     (inst jmp :z FIXNUM)
273     (loadw y rax bignum-digits-offset other-pointer-lowtag)
274     (inst jmp DONE)
275     FIXNUM
276     (inst sar rax n-fixnum-tag-bits)
277     (move y rax)
278     DONE))
279
280 #+#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
281 (define-vop (move-to-word/integer)
282   (:args (x :scs (descriptor-reg) :target y))
283   (:results (y :scs (signed-reg unsigned-reg)))
284   (:note "integer to untagged word coercion")
285   (:temporary (:sc unsigned-reg) backup)
286   (:generator 4
287     (move y x)
288     (if (location= x y)
289         ;; It would be great if a principled way existed to advise GC of
290         ;; algebraic transforms such as 2*R being a conservative root.
291         ;; Until that is possible, emit straightforward code that uses
292         ;; a copy of the potential reference.
293         (move backup x)
294         (setf backup x))
295     (inst sar y 1)      ; optimistically assume it's a fixnum
296     (inst jmp :nc DONE) ; no carry implies tag was 0
297     (loadw y backup bignum-digits-offset other-pointer-lowtag)
298     DONE))
299
300 (define-move-vop move-to-word/integer :move
301   (descriptor-reg) (signed-reg unsigned-reg))
302
303 ;;; Result is a fixnum, so we can just shift. We need the result type
304 ;;; restriction because of the control-stack ambiguity noted above.
305 (define-vop (move-from-word/fixnum)
306   (:args (x :scs (signed-reg unsigned-reg) :target y
307             :load-if (not (location= x y))))
308   (:results (y :scs (any-reg descriptor-reg)
309                :load-if (not (location= x y))))
310   (:result-types tagged-num)
311   (:note "fixnum tagging")
312   (:generator 1
313     (cond ((and (sc-is x signed-reg unsigned-reg)
314                 (not (location= x y)))
315            (if (= n-fixnum-tag-bits 1)
316                (inst lea y (make-ea :qword :base x :index x))
317                (inst lea y (make-ea :qword :index x
318                                     :scale (ash 1 n-fixnum-tag-bits)))))
319           (t
320            ;; Uses: If x is a reg 2 + 3; if x = y uses only 3 bytes
321            (move y x)
322            (inst shl y n-fixnum-tag-bits)))))
323 (define-move-vop move-from-word/fixnum :move
324   (signed-reg unsigned-reg) (any-reg descriptor-reg))
325
326 ;;; Convert an untagged signed word to a lispobj -- fixnum or bignum
327 ;;; as the case may be. Fixnum case inline, bignum case in an assembly
328 ;;; routine.
329 (define-vop (move-from-signed)
330   (:args (x :scs (signed-reg unsigned-reg) :to :result))
331   (:results (y :scs (any-reg descriptor-reg) :from :argument))
332   (:note "signed word to integer coercion")
333   ;; Worst case cost to make sure people know they may be number consing.
334   (:generator 20
335      (aver (not (location= x y)))
336      (let ((done (gen-label)))
337        (inst imul y x #.(ash 1 n-fixnum-tag-bits))
338        (inst jmp :no done)
339        (inst mov y x)
340        (inst lea temp-reg-tn
341              (make-ea :qword :disp
342                       (make-fixup (ecase (tn-offset y)
343                                     (#.rax-offset 'alloc-signed-bignum-in-rax)
344                                     (#.rcx-offset 'alloc-signed-bignum-in-rcx)
345                                     (#.rdx-offset 'alloc-signed-bignum-in-rdx)
346                                     (#.rbx-offset 'alloc-signed-bignum-in-rbx)
347                                     (#.rsi-offset 'alloc-signed-bignum-in-rsi)
348                                     (#.rdi-offset 'alloc-signed-bignum-in-rdi)
349                                     (#.r8-offset  'alloc-signed-bignum-in-r8)
350                                     (#.r9-offset  'alloc-signed-bignum-in-r9)
351                                     (#.r10-offset 'alloc-signed-bignum-in-r10)
352                                     (#.r12-offset 'alloc-signed-bignum-in-r12)
353                                     (#.r13-offset 'alloc-signed-bignum-in-r13)
354                                     (#.r14-offset 'alloc-signed-bignum-in-r14)
355                                     (#.r15-offset 'alloc-signed-bignum-in-r15))
356                                   :assembly-routine)))
357        (inst call temp-reg-tn)
358        (emit-label done))))
359 (define-move-vop move-from-signed :move
360   (signed-reg) (descriptor-reg))
361
362 ;;; Convert an untagged unsigned word to a lispobj -- fixnum or bignum
363 ;;; as the case may be. Fixnum case inline, bignum case in an assembly
364 ;;; routine.
365 (define-vop (move-from-unsigned)
366   (:args (x :scs (signed-reg unsigned-reg) :to :result))
367   (:results (y :scs (any-reg descriptor-reg) :from :argument))
368   (:note "unsigned word to integer coercion")
369   ;; Worst case cost to make sure people know they may be number consing.
370   (:generator 20
371     (aver (not (location= x y)))
372     (let ((done (gen-label)))
373       (inst mov y #.(ash (1- (ash 1 (1+ n-fixnum-tag-bits)))
374                          n-positive-fixnum-bits))
375       ;; The assembly routines test the sign flag from this one, so if
376       ;; you change stuff here, make sure the sign flag doesn't get
377       ;; overwritten before the CALL!
378       (inst test x y)
379       ;; Using LEA is faster but bigger than MOV+SHL; it also doesn't
380       ;; twiddle the sign flag.  The cost of doing this speculatively
381       ;; should be noise compared to bignum consing if that is needed
382       ;; and saves one branch.
383       (if (= n-fixnum-tag-bits 1)
384           (inst lea y (make-ea :qword :base x :index x))
385           (inst lea y (make-ea :qword :index x
386                                :scale (ash 1 n-fixnum-tag-bits))))
387       (inst jmp :z done)
388       (inst mov y x)
389       (inst lea temp-reg-tn
390             (make-ea :qword :disp
391                      (make-fixup (ecase (tn-offset y)
392                                    (#.rax-offset 'alloc-unsigned-bignum-in-rax)
393                                    (#.rcx-offset 'alloc-unsigned-bignum-in-rcx)
394                                    (#.rdx-offset 'alloc-unsigned-bignum-in-rdx)
395                                    (#.rbx-offset 'alloc-unsigned-bignum-in-rbx)
396                                    (#.rsi-offset 'alloc-unsigned-bignum-in-rsi)
397                                    (#.rdi-offset 'alloc-unsigned-bignum-in-rdi)
398                                    (#.r8-offset  'alloc-unsigned-bignum-in-r8)
399                                    (#.r9-offset  'alloc-unsigned-bignum-in-r9)
400                                    (#.r10-offset 'alloc-unsigned-bignum-in-r10)
401                                    (#.r12-offset 'alloc-unsigned-bignum-in-r12)
402                                    (#.r13-offset 'alloc-unsigned-bignum-in-r13)
403                                    (#.r14-offset 'alloc-unsigned-bignum-in-r14)
404                                    (#.r15-offset 'alloc-unsigned-bignum-in-r15))
405                                  :assembly-routine)))
406       (inst call temp-reg-tn)
407       (emit-label done))))
408 (define-move-vop move-from-unsigned :move
409   (unsigned-reg) (descriptor-reg))
410
411 ;;; Move untagged numbers.
412 (define-vop (word-move)
413   (:args (x :scs (signed-reg unsigned-reg) :target y
414             :load-if (not (location= x y))))
415   (:results (y :scs (signed-reg unsigned-reg)
416                :load-if
417                (not (or (location= x y)
418                         (and (sc-is x signed-reg unsigned-reg)
419                              (sc-is y signed-stack unsigned-stack))))))
420   (:effects)
421   (:affected)
422   (:note "word integer move")
423   (:generator 0
424     (move y x)))
425 (define-move-vop word-move :move
426   (signed-reg unsigned-reg) (signed-reg unsigned-reg))
427
428 ;;; Move untagged number arguments/return-values.
429 (define-vop (move-word-arg)
430   (:args (x :scs (signed-reg unsigned-reg) :target y)
431          (fp :scs (any-reg) :load-if (not (sc-is y sap-reg))))
432   (:results (y))
433   (:note "word integer argument move")
434   (:generator 0
435     (sc-case y
436       ((signed-reg unsigned-reg)
437        (move y x))
438       ((signed-stack unsigned-stack)
439        (if (= (tn-offset fp) esp-offset)
440            (storew x fp (tn-offset y))  ; c-call
441            (storew x fp (frame-word-offset (tn-offset y))))))))
442 (define-move-vop move-word-arg :move-arg
443   (descriptor-reg any-reg signed-reg unsigned-reg) (signed-reg unsigned-reg))
444
445 ;;; Use standard MOVE-ARG and coercion to move an untagged number
446 ;;; to a descriptor passing location.
447 (define-move-vop move-arg :move-arg
448   (signed-reg unsigned-reg) (any-reg descriptor-reg))