1.0.20.12: :CACHED-CONSTANT TNs don't exist
[sbcl/tcr.git] / src / compiler / x86-64 / move.lisp
blob4f37f5a36fb436db6ee778ab3e7e520a1b58587a
1 ;;;; the x86-64 VM definition of operand loading/saving and the MOVE vop
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.
12 (in-package "SB!VM")
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)))
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)))
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))))
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))))))
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))))
60 (define-move-fun (load-character 1) (vop x y)
61 ((immediate) (character-reg))
62 (inst mov y (char-code (tn-value x))))
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))))
68 (define-move-fun (load-constant 5) (vop x y)
69 ((constant) (descriptor-reg any-reg))
70 (inst mov y x))
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))
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))
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))))
116 (define-move-vop move :move
117 (any-reg descriptor-reg immediate)
118 (any-reg descriptor-reg))
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)
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 31))
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))
139 (error "~A is not a register, no temporary given, and immediate ~A too large" target val))))
141 ;;; The MOVE-ARG VOP is used for moving descriptor values into
142 ;;; another frame for argument or known value passing.
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 ((or (signed-byte 29) (unsigned-byte 29))
163 (inst mov y (fixnumize val)))
164 (integer
165 (move-immediate y (fixnumize val)))
166 (symbol
167 (load-symbol y val))
168 (character
169 (inst mov y (logior (ash (char-code val) n-widetag-bits)
170 character-widetag)))))
171 (move y x)))
172 ((control-stack)
173 (if (sc-is x immediate)
174 (let ((val (tn-value x)))
175 (if (= (tn-offset fp) esp-offset)
176 ;; C-call
177 (etypecase val
178 (integer
179 (storew (fixnumize val) fp (tn-offset y)))
180 (symbol
181 (storew (+ nil-value (static-symbol-offset val))
182 fp (tn-offset y)))
183 (character
184 (storew (logior (ash (char-code val) n-widetag-bits)
185 character-widetag)
186 fp (tn-offset y))))
187 ;; Lisp stack
188 (etypecase val
189 (integer
190 (storew (fixnumize val) fp (- (1+ (tn-offset y)))))
191 (symbol
192 (storew (+ nil-value (static-symbol-offset val))
193 fp (- (1+ (tn-offset y)))))
194 (character
195 (storew (logior (ash (char-code val) n-widetag-bits)
196 character-widetag)
197 fp (- (1+ (tn-offset y))))))))
198 (if (= (tn-offset fp) esp-offset)
199 ;; C-call
200 (storew x fp (tn-offset y))
201 ;; Lisp stack
202 (storew x fp (- (1+ (tn-offset y))))))))))
204 (define-move-vop move-arg :move-arg
205 (any-reg descriptor-reg)
206 (any-reg descriptor-reg))
208 ;;;; ILLEGAL-MOVE
210 ;;; This VOP exists just to begin the lifetime of a TN that couldn't
211 ;;; be written legally due to a type error. An error is signalled
212 ;;; before this VOP is so we don't need to do anything (not that there
213 ;;; would be anything sensible to do anyway.)
214 (define-vop (illegal-move)
215 (:args (x) (type))
216 (:results (y))
217 (:ignore y)
218 (:vop-var vop)
219 (:save-p :compute-only)
220 (:generator 666
221 (error-call vop 'object-not-type-error x type)))
223 ;;;; moves and coercions
225 ;;; These MOVE-TO-WORD VOPs move a tagged integer to a raw full-word
226 ;;; representation. Similarly, the MOVE-FROM-WORD VOPs converts a raw
227 ;;; integer to a tagged bignum or fixnum.
229 ;;; Arg is a fixnum, so just shift it. We need a type restriction
230 ;;; because some possible arg SCs (control-stack) overlap with
231 ;;; possible bignum arg SCs.
232 (define-vop (move-to-word/fixnum)
233 (:args (x :scs (any-reg descriptor-reg) :target y
234 :load-if (not (location= x y))))
235 (:results (y :scs (signed-reg unsigned-reg)
236 :load-if (not (location= x y))))
237 (:arg-types tagged-num)
238 (:note "fixnum untagging")
239 (:generator 1
240 (move y x)
241 (inst sar y (1- n-lowtag-bits))))
242 (define-move-vop move-to-word/fixnum :move
243 (any-reg descriptor-reg) (signed-reg unsigned-reg))
245 ;;; Arg is a non-immediate constant, load it.
246 (define-vop (move-to-word-c)
247 (:args (x :scs (constant)))
248 (:results (y :scs (signed-reg unsigned-reg)))
249 (:note "constant load")
250 (:generator 1
251 (inst mov y (tn-value x))))
252 (define-move-vop move-to-word-c :move
253 (constant) (signed-reg unsigned-reg))
256 ;;; Arg is a fixnum or bignum, figure out which and load if necessary.
257 (define-vop (move-to-word/integer)
258 (:args (x :scs (descriptor-reg) :target eax))
259 (:results (y :scs (signed-reg unsigned-reg)))
260 (:note "integer to untagged word coercion")
261 (:temporary (:sc unsigned-reg :offset eax-offset
262 :from (:argument 0) :to (:result 0) :target y) eax)
263 (:generator 4
264 (move eax x)
265 (inst test al-tn 7) ; a symbolic constant for this
266 (inst jmp :z FIXNUM) ; would be nice
267 (loadw y eax bignum-digits-offset other-pointer-lowtag)
268 (inst jmp DONE)
269 FIXNUM
270 (inst sar eax (1- n-lowtag-bits))
271 (move y eax)
272 DONE))
273 (define-move-vop move-to-word/integer :move
274 (descriptor-reg) (signed-reg unsigned-reg))
277 ;;; Result is a fixnum, so we can just shift. We need the result type
278 ;;; restriction because of the control-stack ambiguity noted above.
279 (define-vop (move-from-word/fixnum)
280 (:args (x :scs (signed-reg unsigned-reg) :target y
281 :load-if (not (location= x y))))
282 (:results (y :scs (any-reg descriptor-reg)
283 :load-if (not (location= x y))))
284 (:result-types tagged-num)
285 (:note "fixnum tagging")
286 (:generator 1
287 (cond ((and (sc-is x signed-reg unsigned-reg)
288 (not (location= x y)))
289 ;; Uses 7 bytes, but faster on the Pentium
290 (inst lea y (make-ea :qword :index x :scale 8)))
292 ;; Uses: If x is a reg 2 + 3; if x = y uses only 3 bytes
293 (move y x)
294 (inst shl y (1- n-lowtag-bits))))))
295 (define-move-vop move-from-word/fixnum :move
296 (signed-reg unsigned-reg) (any-reg descriptor-reg))
298 ;;; Convert an untagged signed word to a lispobj -- fixnum or bignum
299 ;;; as the case may be. Fixnum case inline, bignum case in an assembly
300 ;;; routine.
301 (define-vop (move-from-signed)
302 (:args (x :scs (signed-reg unsigned-reg) :to :result))
303 (:results (y :scs (any-reg descriptor-reg) :from :argument))
304 (:note "signed word to integer coercion")
305 ;; Worst case cost to make sure people know they may be number consing.
306 (:generator 20
307 (aver (not (location= x y)))
308 (let ((done (gen-label)))
309 (inst imul y x #.(ash 1 n-fixnum-tag-bits))
310 (inst jmp :no done)
311 (inst mov y x)
312 (inst lea temp-reg-tn
313 (make-ea :qword :disp
314 (make-fixup (ecase (tn-offset y)
315 (#.rax-offset 'alloc-signed-bignum-in-rax)
316 (#.rcx-offset 'alloc-signed-bignum-in-rcx)
317 (#.rdx-offset 'alloc-signed-bignum-in-rdx)
318 (#.rbx-offset 'alloc-signed-bignum-in-rbx)
319 (#.rsi-offset 'alloc-signed-bignum-in-rsi)
320 (#.rdi-offset 'alloc-signed-bignum-in-rdi)
321 (#.r8-offset 'alloc-signed-bignum-in-r8)
322 (#.r9-offset 'alloc-signed-bignum-in-r9)
323 (#.r10-offset 'alloc-signed-bignum-in-r10)
324 (#.r12-offset 'alloc-signed-bignum-in-r12)
325 (#.r13-offset 'alloc-signed-bignum-in-r13)
326 (#.r14-offset 'alloc-signed-bignum-in-r14)
327 (#.r15-offset 'alloc-signed-bignum-in-r15))
328 :assembly-routine)))
329 (inst call temp-reg-tn)
330 (emit-label done))))
331 (define-move-vop move-from-signed :move
332 (signed-reg) (descriptor-reg))
334 ;;; Convert an untagged unsigned word to a lispobj -- fixnum or bignum
335 ;;; as the case may be. Fixnum case inline, bignum case in an assembly
336 ;;; routine.
337 (define-vop (move-from-unsigned)
338 (:args (x :scs (signed-reg unsigned-reg) :to :result))
339 (:results (y :scs (any-reg descriptor-reg) :from :argument))
340 (:note "unsigned word to integer coercion")
341 ;; Worst case cost to make sure people know they may be number consing.
342 (:generator 20
343 (aver (not (location= x y)))
344 (let ((done (gen-label)))
345 (inst mov y #.(ash lowtag-mask (- n-word-bits n-fixnum-tag-bits 1)))
346 ;; The assembly routines test the sign flag from this one, so if
347 ;; you change stuff here, make sure the sign flag doesn't get
348 ;; overwritten before the CALL!
349 (inst test x y)
350 ;; Faster but bigger then SHL Y 4. The cost of doing this
351 ;; speculatively should be noise compared to bignum consing if
352 ;; that is needed and saves one branch.
353 (inst lea y (make-ea :qword :index x :scale 8))
354 (inst jmp :z done)
355 (inst mov y x)
356 (inst lea temp-reg-tn
357 (make-ea :qword :disp
358 (make-fixup (ecase (tn-offset y)
359 (#.rax-offset 'alloc-unsigned-bignum-in-rax)
360 (#.rcx-offset 'alloc-unsigned-bignum-in-rcx)
361 (#.rdx-offset 'alloc-unsigned-bignum-in-rdx)
362 (#.rbx-offset 'alloc-unsigned-bignum-in-rbx)
363 (#.rsi-offset 'alloc-unsigned-bignum-in-rsi)
364 (#.rdi-offset 'alloc-unsigned-bignum-in-rdi)
365 (#.r8-offset 'alloc-unsigned-bignum-in-r8)
366 (#.r9-offset 'alloc-unsigned-bignum-in-r9)
367 (#.r10-offset 'alloc-unsigned-bignum-in-r10)
368 (#.r12-offset 'alloc-unsigned-bignum-in-r12)
369 (#.r13-offset 'alloc-unsigned-bignum-in-r13)
370 (#.r14-offset 'alloc-unsigned-bignum-in-r14)
371 (#.r15-offset 'alloc-unsigned-bignum-in-r15))
372 :assembly-routine)))
373 (inst call temp-reg-tn)
374 (emit-label done))))
375 (define-move-vop move-from-unsigned :move
376 (unsigned-reg) (descriptor-reg))
378 ;;; Move untagged numbers.
379 (define-vop (word-move)
380 (:args (x :scs (signed-reg unsigned-reg) :target y
381 :load-if (not (location= x y))))
382 (:results (y :scs (signed-reg unsigned-reg)
383 :load-if
384 (not (or (location= x y)
385 (and (sc-is x signed-reg unsigned-reg)
386 (sc-is y signed-stack unsigned-stack))))))
387 (:effects)
388 (:affected)
389 (:note "word integer move")
390 (:generator 0
391 (move y x)))
392 (define-move-vop word-move :move
393 (signed-reg unsigned-reg) (signed-reg unsigned-reg))
395 ;;; Move untagged number arguments/return-values.
396 (define-vop (move-word-arg)
397 (:args (x :scs (signed-reg unsigned-reg) :target y)
398 (fp :scs (any-reg) :load-if (not (sc-is y sap-reg))))
399 (:results (y))
400 (:note "word integer argument move")
401 (:generator 0
402 (sc-case y
403 ((signed-reg unsigned-reg)
404 (move y x))
405 ((signed-stack unsigned-stack)
406 (if (= (tn-offset fp) esp-offset)
407 (storew x fp (tn-offset y)) ; c-call
408 (storew x fp (- (1+ (tn-offset y)))))))))
409 (define-move-vop move-word-arg :move-arg
410 (descriptor-reg any-reg signed-reg unsigned-reg) (signed-reg unsigned-reg))
412 ;;; Use standard MOVE-ARG and coercion to move an untagged number
413 ;;; to a descriptor passing location.
414 (define-move-vop move-arg :move-arg
415 (signed-reg unsigned-reg) (any-reg descriptor-reg))