0.9.14.28:
[sbcl.git] / src / compiler / x86 / move.lisp
1 ;;;; the x86 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 (define-move-fun (load-immediate 1) (vop x y)
15   ((immediate)
16    (any-reg descriptor-reg))
17   (let ((val (tn-value x)))
18     (etypecase val
19       (integer
20        (if (zerop val)
21            (inst xor y y)
22          (inst mov y (fixnumize val))))
23       (symbol
24        (load-symbol y val))
25       (character
26        (inst mov y (logior (ash (char-code val) n-widetag-bits)
27                            character-widetag))))))
28
29 (define-move-fun (load-number 1) (vop x y)
30   ((immediate) (signed-reg unsigned-reg))
31   (let ((val (tn-value x)))
32     (if (zerop val)
33         (inst xor y y)
34         (inst mov y val))))
35
36 (define-move-fun (load-character 1) (vop x y)
37   ((immediate) (character-reg))
38   (inst mov y (char-code (tn-value x))))
39
40 (define-move-fun (load-system-area-pointer 1) (vop x y)
41   ((immediate) (sap-reg))
42   (inst mov y (sap-int (tn-value x))))
43
44 (define-move-fun (load-constant 5) (vop x y)
45   ((constant) (descriptor-reg any-reg))
46   (inst mov y x))
47
48 (define-move-fun (load-stack 5) (vop x y)
49   ((control-stack) (any-reg descriptor-reg)
50    (character-stack) (character-reg)
51    (sap-stack) (sap-reg)
52    (signed-stack) (signed-reg)
53    (unsigned-stack) (unsigned-reg))
54   (inst mov y x))
55
56 (define-move-fun (store-stack 5) (vop x y)
57   ((any-reg descriptor-reg) (control-stack)
58    (character-reg) (character-stack)
59    (sap-reg) (sap-stack)
60    (signed-reg) (signed-stack)
61    (unsigned-reg) (unsigned-stack))
62   (inst mov y x))
63 \f
64 ;;;; the MOVE VOP
65 (define-vop (move)
66   (:args (x :scs (any-reg descriptor-reg immediate) :target y
67             :load-if (not (location= x y))))
68   (:results (y :scs (any-reg descriptor-reg)
69                :load-if
70                (not (or (location= x y)
71                         (and (sc-is x any-reg descriptor-reg immediate)
72                              (sc-is y control-stack))))))
73   (:effects)
74   (:affected)
75   (:generator 0
76     (if (and (sc-is x immediate)
77              (sc-is y any-reg descriptor-reg control-stack))
78         (let ((val (tn-value x)))
79           (etypecase val
80             (integer
81              (if (and (zerop val) (sc-is y any-reg descriptor-reg))
82                  (inst xor y y)
83                (inst mov y (fixnumize val))))
84             (symbol
85              (inst mov y (+ nil-value (static-symbol-offset val))))
86             (character
87              (inst mov y (logior (ash (char-code val) n-widetag-bits)
88                                  character-widetag)))))
89       (move y x))))
90
91 (define-move-vop move :move
92   (any-reg descriptor-reg immediate)
93   (any-reg descriptor-reg))
94
95 ;;; Make MOVE the check VOP for T so that type check generation
96 ;;; doesn't think it is a hairy type. This also allows checking of a
97 ;;; few of the values in a continuation to fall out.
98 (primitive-type-vop move (:check) t)
99
100 ;;; The MOVE-ARG VOP is used for moving descriptor values into
101 ;;; another frame for argument or known value passing.
102 ;;;
103 ;;; Note: It is not going to be possible to move a constant directly
104 ;;; to another frame, except if the destination is a register and in
105 ;;; this case the loading works out.
106 (define-vop (move-arg)
107   (:args (x :scs (any-reg descriptor-reg immediate) :target y
108             :load-if (not (and (sc-is y any-reg descriptor-reg)
109                                (sc-is x control-stack))))
110          (fp :scs (any-reg)
111              :load-if (not (sc-is y any-reg descriptor-reg))))
112   (:results (y))
113   (:generator 0
114     (sc-case y
115       ((any-reg descriptor-reg)
116        (if (sc-is x immediate)
117            (let ((val (tn-value x)))
118              (etypecase val
119               (integer
120                (if (zerop val)
121                    (inst xor y y)
122                  (inst mov y (fixnumize val))))
123               (symbol
124                (load-symbol y val))
125               (character
126                (inst mov y (logior (ash (char-code val) n-widetag-bits)
127                                    character-widetag)))))
128          (move y x)))
129       ((control-stack)
130        (if (sc-is x immediate)
131            (let ((val (tn-value x)))
132              (if (= (tn-offset fp) esp-offset)
133                  ;; C-call
134                  (etypecase val
135                    (integer
136                     (storew (fixnumize val) fp (tn-offset y)))
137                    (symbol
138                     (storew (+ nil-value (static-symbol-offset val))
139                             fp (tn-offset y)))
140                    (character
141                     (storew (logior (ash (char-code val) n-widetag-bits)
142                                     character-widetag)
143                             fp (tn-offset y))))
144                ;; Lisp stack
145                (etypecase val
146                  (integer
147                   (storew (fixnumize val) fp (- (1+ (tn-offset y)))))
148                  (symbol
149                   (storew (+ nil-value (static-symbol-offset val))
150                           fp (- (1+ (tn-offset y)))))
151                  (character
152                   (storew (logior (ash (char-code val) n-widetag-bits)
153                                   character-widetag)
154                           fp (- (1+ (tn-offset y))))))))
155          (if (= (tn-offset fp) esp-offset)
156              ;; C-call
157              (storew x fp (tn-offset y))
158            ;; Lisp stack
159            (storew x fp (- (1+ (tn-offset y))))))))))
160
161 (define-move-vop move-arg :move-arg
162   (any-reg descriptor-reg)
163   (any-reg descriptor-reg))
164 \f
165 ;;;; ILLEGAL-MOVE
166
167 ;;; This VOP exists just to begin the lifetime of a TN that couldn't
168 ;;; be written legally due to a type error. An error is signalled
169 ;;; before this VOP is so we don't need to do anything (not that there
170 ;;; would be anything sensible to do anyway.)
171 (define-vop (illegal-move)
172   (:args (x) (type))
173   (:results (y))
174   (:ignore y)
175   (:vop-var vop)
176   (:save-p :compute-only)
177   (:generator 666
178     (error-call vop object-not-type-error x type)))
179 \f
180 ;;;; moves and coercions
181
182 ;;; These MOVE-TO-WORD VOPs move a tagged integer to a raw full-word
183 ;;; representation. Similarly, the MOVE-FROM-WORD VOPs converts a raw
184 ;;; integer to a tagged bignum or fixnum.
185
186 ;;; Arg is a fixnum, so just shift it. We need a type restriction
187 ;;; because some possible arg SCs (control-stack) overlap with
188 ;;; possible bignum arg SCs.
189 (define-vop (move-to-word/fixnum)
190   (:args (x :scs (any-reg descriptor-reg) :target y
191             :load-if (not (location= x y))))
192   (:results (y :scs (signed-reg unsigned-reg)
193                :load-if (not (location= x y))))
194   (:arg-types tagged-num)
195   (:note "fixnum untagging")
196   (:generator 1
197     (move y x)
198     (inst sar y 2)))
199 (define-move-vop move-to-word/fixnum :move
200   (any-reg descriptor-reg) (signed-reg unsigned-reg))
201
202 ;;; Arg is a non-immediate constant, load it.
203 (define-vop (move-to-word-c)
204   (:args (x :scs (constant)))
205   (:results (y :scs (signed-reg unsigned-reg)))
206   (:note "constant load")
207   (:generator 1
208     (inst mov y (tn-value x))))
209 (define-move-vop move-to-word-c :move
210   (constant) (signed-reg unsigned-reg))
211
212
213 ;;; Arg is a fixnum or bignum, figure out which and load if necessary.
214 (define-vop (move-to-word/integer)
215   (:args (x :scs (descriptor-reg) :target eax))
216   (:results (y :scs (signed-reg unsigned-reg)))
217   (:note "integer to untagged word coercion")
218   (:temporary (:sc unsigned-reg :offset eax-offset
219                    :from (:argument 0) :to (:result 0) :target y) eax)
220   (:generator 4
221     (move eax x)
222     (inst test al-tn 3)
223     (inst jmp :z fixnum)
224     (loadw y eax bignum-digits-offset other-pointer-lowtag)
225     (inst jmp done)
226     FIXNUM
227     (inst sar eax 2)
228     (move y eax)
229     DONE))
230 (define-move-vop move-to-word/integer :move
231   (descriptor-reg) (signed-reg unsigned-reg))
232
233
234 ;;; Result is a fixnum, so we can just shift. We need the result type
235 ;;; restriction because of the control-stack ambiguity noted above.
236 (define-vop (move-from-word/fixnum)
237   (:args (x :scs (signed-reg unsigned-reg) :target y
238             :load-if (not (location= x y))))
239   (:results (y :scs (any-reg descriptor-reg)
240                :load-if (not (location= x y))))
241   (:result-types tagged-num)
242   (:note "fixnum tagging")
243   (:generator 1
244     (cond ((and (sc-is x signed-reg unsigned-reg)
245                 (not (location= x y)))
246            ;; Uses 7 bytes, but faster on the Pentium
247            (inst lea y (make-ea :dword :index x :scale 4)))
248           (t
249            ;; Uses: If x is a reg 2 + 3; if x = y uses only 3 bytes
250            (move y x)
251            (inst shl y 2)))))
252 (define-move-vop move-from-word/fixnum :move
253   (signed-reg unsigned-reg) (any-reg descriptor-reg))
254
255 ;;; Result may be a bignum, so we have to check. Use a worst-case cost
256 ;;; to make sure people know they may be number consing.
257 ;;;
258 ;;; KLUDGE: I assume this is suppressed in favor of the "faster inline
259 ;;; version" below. (See also mysterious comment "we don't want a VOP
260 ;;; on this one" on DEFINE-ASSEMBLY-ROUTINE (MOVE-FROM-SIGNED) in
261 ;;; "src/assembly/x86/alloc.lisp".) -- WHN 19990916
262 #+nil
263 (define-vop (move-from-signed)
264   (:args (x :scs (signed-reg unsigned-reg) :target eax))
265   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)) eax)
266   (:temporary (:sc unsigned-reg :offset ebx-offset :to (:result 0) :target y)
267               ebx)
268   (:temporary (:sc unsigned-reg :offset ecx-offset
269                    :from (:argument 0) :to (:result 0)) ecx)
270   (:ignore ecx)
271   (:results (y :scs (any-reg descriptor-reg)))
272   (:note "signed word to integer coercion")
273   (:generator 20
274     (move eax x)
275     (inst call (make-fixup 'move-from-signed :assembly-routine))
276     (move y ebx)))
277 ;;; Faster inline version,
278 ;;; KLUDGE: Do we really want the faster inline version? It's sorta big.
279 ;;; It is nice that it doesn't use any temporaries, though. -- WHN 19990916
280 (define-vop (move-from-signed)
281   (:args (x :scs (signed-reg unsigned-reg) :to :result))
282   (:results (y :scs (any-reg descriptor-reg) :from :argument))
283   (:note "signed word to integer coercion")
284   (:node-var node)
285   (:generator 20
286      (aver (not (location= x y)))
287      (let ((bignum (gen-label))
288            (done (gen-label)))
289        (inst mov y x)
290        (inst shl y 1)
291        (inst jmp :o bignum)
292        (inst shl y 1)
293        (inst jmp :o bignum)
294        (emit-label done)
295        ;; KLUDGE: The sequence above leaves a DESCRIPTOR-REG Y in a
296        ;; non-descriptor state for a while. Does that matter? Does it
297        ;; matter in GENGC but not in GENCGC? Is this written down
298        ;; anywhere?
299        ;;   -- WHN 19990916
300        ;;
301        ;; Also, the sequence above seems rather twisty. Why not something
302        ;; more obvious along the lines of
303        ;;   inst move y x
304        ;;   inst tst x #xc0000000
305        ;;   inst jmp :nz bignum
306        ;;   inst shl y 2
307        ;;   emit-label done
308
309        (assemble (*elsewhere*)
310           (emit-label bignum)
311           (with-fixed-allocation
312               (y bignum-widetag (+ bignum-digits-offset 1) node)
313             (storew x y bignum-digits-offset other-pointer-lowtag))
314           (inst jmp done)))))
315 (define-move-vop move-from-signed :move
316   (signed-reg) (descriptor-reg))
317
318 ;;; Check for fixnum, and possibly allocate one or two word bignum
319 ;;; result. Use a worst-case cost to make sure people know they may be
320 ;;; number consing.
321 #+nil
322 (define-vop (move-from-unsigned)
323   (:args (x :scs (signed-reg unsigned-reg) :target eax))
324   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)) eax)
325   (:temporary (:sc unsigned-reg :offset ebx-offset :to (:result 0) :target y)
326               ebx)
327   (:temporary (:sc unsigned-reg :offset ecx-offset
328                    :from (:argument 0) :to (:result 0)) ecx)
329   (:ignore ecx)
330   (:results (y :scs (any-reg descriptor-reg)))
331   (:note "unsigned word to integer coercion")
332   (:generator 20
333     (move eax x)
334     (inst call (make-fixup 'move-from-unsigned :assembly-routine))
335     (move y ebx)))
336 ;;; Faster inline version.
337 ;;; KLUDGE: Do we really want the faster inline version? It seems awfully big..
338 ;;; If we really want speed, most likely it's only important in the non-consing
339 ;;; case, so how about about making the *ELSEWHERE* stuff into a subroutine? --
340 ;;; WHN 19990916
341 (define-vop (move-from-unsigned)
342   (:args (x :scs (signed-reg unsigned-reg) :to :save))
343   (:temporary (:sc unsigned-reg) alloc)
344   (:results (y :scs (any-reg descriptor-reg)))
345   (:node-var node)
346   (:note "unsigned word to integer coercion")
347   (:generator 20
348     (aver (not (location= x y)))
349     (aver (not (location= x alloc)))
350     (aver (not (location= y alloc)))
351     (let ((bignum (gen-label))
352           (done (gen-label))
353           (one-word-bignum (gen-label))
354           (L1 (gen-label)))
355       (inst test x #xe0000000)
356       (inst jmp :nz bignum)
357       ;; Fixnum.
358       (inst lea y (make-ea :dword :index x :scale 4)) ; Faster but bigger.
359       ;(inst mov y x)
360       ;(inst shl y 2)
361       (emit-label done)
362
363       (assemble (*elsewhere*)
364          (emit-label bignum)
365          ;; Note: As on the mips port, space for a two word bignum is
366          ;; always allocated and the header size is set to either one
367          ;; or two words as appropriate.
368          (inst jmp :ns one-word-bignum)
369          ;; two word bignum
370          (inst mov y (logior (ash (1- (+ bignum-digits-offset 2))
371                                   n-widetag-bits)
372                              bignum-widetag))
373          (inst jmp L1)
374          (emit-label one-word-bignum)
375          (inst mov y (logior (ash (1- (+ bignum-digits-offset 1))
376                                   n-widetag-bits)
377                              bignum-widetag))
378          (emit-label L1)
379          (pseudo-atomic
380           (allocation alloc (pad-data-block (+ bignum-digits-offset 2)) node)
381           (storew y alloc)
382           (inst lea y (make-ea :byte :base alloc :disp other-pointer-lowtag))
383           (storew x y bignum-digits-offset other-pointer-lowtag))
384          (inst jmp done)))))
385 (define-move-vop move-from-unsigned :move
386   (unsigned-reg) (descriptor-reg))
387
388 ;;; Move untagged numbers.
389 (define-vop (word-move)
390   (:args (x :scs (signed-reg unsigned-reg) :target y
391             :load-if (not (location= x y))))
392   (:results (y :scs (signed-reg unsigned-reg)
393                :load-if
394                (not (or (location= x y)
395                         (and (sc-is x signed-reg unsigned-reg)
396                              (sc-is y signed-stack unsigned-stack))))))
397   (:effects)
398   (:affected)
399   (:note "word integer move")
400   (:generator 0
401     (move y x)))
402 (define-move-vop word-move :move
403   (signed-reg unsigned-reg) (signed-reg unsigned-reg))
404
405 ;;; Move untagged number arguments/return-values.
406 (define-vop (move-word-arg)
407   (:args (x :scs (signed-reg unsigned-reg) :target y)
408          (fp :scs (any-reg) :load-if (not (sc-is y sap-reg))))
409   (:results (y))
410   (:note "word integer argument move")
411   (:generator 0
412     (sc-case y
413       ((signed-reg unsigned-reg)
414        (move y x))
415       ((signed-stack unsigned-stack)
416        (if (= (tn-offset fp) esp-offset)
417            (storew x fp (tn-offset y))  ; c-call
418            (storew x fp (- (1+ (tn-offset y)))))))))
419 (define-move-vop move-word-arg :move-arg
420   (descriptor-reg any-reg signed-reg unsigned-reg) (signed-reg unsigned-reg))
421
422 ;;; Use standard MOVE-ARG and coercion to move an untagged number
423 ;;; to a descriptor passing location.
424 (define-move-vop move-arg :move-arg
425   (signed-reg unsigned-reg) (any-reg descriptor-reg))