0.9.9.36:
[sbcl.git] / src / compiler / ppc / move.lisp
1 ;;;; the PPC 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   ((null immediate zero)
16    (any-reg descriptor-reg))
17   (let ((val (tn-value x)))
18     (etypecase val
19       (integer
20        (inst lr y (fixnumize val)))
21       (null
22        (move y null-tn))
23       (symbol
24        (load-symbol y val))
25       (character
26        (inst lr 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 zero)
31    (signed-reg unsigned-reg))
32   (inst lr y (tn-value x)))
33
34 (define-move-fun (load-character 1) (vop x y)
35   ((immediate) (character-reg))
36   (inst li y (char-code (tn-value x))))
37
38 (define-move-fun (load-system-area-pointer 1) (vop x y)
39   ((immediate) (sap-reg))
40   (inst lr y (sap-int (tn-value x))))
41
42 (define-move-fun (load-constant 5) (vop x y)
43   ((constant) (descriptor-reg))
44   (loadw y code-tn (tn-offset x) other-pointer-lowtag))
45
46 (define-move-fun (load-stack 5) (vop x y)
47   ((control-stack) (any-reg descriptor-reg))
48   (load-stack-tn y x))
49
50 (define-move-fun (load-number-stack 5) (vop x y)
51   ((character-stack) (character-reg)
52    (sap-stack) (sap-reg)
53    (signed-stack) (signed-reg)
54    (unsigned-stack) (unsigned-reg))
55   (let ((nfp (current-nfp-tn vop)))
56     (loadw y nfp (tn-offset x))))
57
58 (define-move-fun (store-stack 5) (vop x y)
59   ((any-reg descriptor-reg) (control-stack))
60   (store-stack-tn y x))
61
62 (define-move-fun (store-number-stack 5) (vop x y)
63   ((character-reg) (character-stack)
64    (sap-reg) (sap-stack)
65    (signed-reg) (signed-stack)
66    (unsigned-reg) (unsigned-stack))
67   (let ((nfp (current-nfp-tn vop)))
68     (storew x nfp (tn-offset y))))
69
70 \f
71 ;;;; The Move VOP:
72 (define-vop (move)
73   (:args (x :target y
74             :scs (any-reg descriptor-reg zero null)
75             :load-if (not (location= x y))))
76   (:results (y :scs (any-reg descriptor-reg)
77                :load-if (not (location= x y))))
78   (:effects)
79   (:affected)
80   (:generator 0
81     (move y x)))
82
83 (define-move-vop move :move
84   (any-reg descriptor-reg)
85   (any-reg descriptor-reg))
86
87 ;;; Make MOVE the check VOP for T so that type check generation
88 ;;; doesn't think it is a hairy type.  This also allows checking of a
89 ;;; few of the values in a continuation to fall out.
90 (primitive-type-vop move (:check) t)
91
92 ;;; The MOVE-ARG VOP is used for moving descriptor values into another
93 ;;; frame for argument or known value passing.
94 (define-vop (move-arg)
95   (:args (x :target y
96             :scs (any-reg descriptor-reg zero null))
97          (fp :scs (any-reg)
98              :load-if (not (sc-is y any-reg descriptor-reg))))
99   (:results (y))
100   (:generator 0
101     (sc-case y
102       ((any-reg descriptor-reg)
103        (move y x))
104       (control-stack
105        (storew x fp (tn-offset y))))))
106 ;;;
107 (define-move-vop move-arg :move-arg
108   (any-reg descriptor-reg)
109   (any-reg descriptor-reg))
110
111
112 \f
113 ;;;; ILLEGAL-MOVE
114
115 ;;; This VOP exists just to begin the lifetime of a TN that couldn't
116 ;;; be written legally due to a type error.  An error is signalled
117 ;;; before this VOP is so we don't need to do anything (not that there
118 ;;; would be anything sensible to do anyway.)
119 (define-vop (illegal-move)
120   (:args (x) (type))
121   (:results (y))
122   (:ignore y)
123   (:vop-var vop)
124   (:save-p :compute-only)
125   (:generator 666
126     (error-call vop object-not-type-error x type)))
127
128
129 \f
130 ;;;; Moves and coercions:
131
132 ;;; These MOVE-TO-WORD VOPs move a tagged integer to a raw full-word
133 ;;; representation.  Similarly, the MOVE-FROM-WORD VOPs converts a raw integer
134 ;;; to a tagged bignum or fixnum.
135
136 ;;; ARG is a fixnum, so just shift it.  We need a type restriction because some
137 ;;; possible arg SCs (control-stack) overlap with possible bignum arg SCs.
138 (define-vop (move-to-word/fixnum)
139   (:args (x :scs (any-reg descriptor-reg)))
140   (:results (y :scs (signed-reg unsigned-reg)))
141   (:arg-types tagged-num)
142   (:note "fixnum untagging")
143   (:generator 1
144     (inst srawi y x 2)))
145 (define-move-vop move-to-word/fixnum :move
146   (any-reg descriptor-reg) (signed-reg unsigned-reg))
147
148 ;;; ARG is a non-immediate constant; load it.
149 (define-vop (move-to-word-c)
150   (:args (x :scs (constant)))
151   (:results (y :scs (signed-reg unsigned-reg)))
152   (:note "constant load")
153   (:generator 1
154     (inst lr y (tn-value x))))
155 (define-move-vop move-to-word-c :move
156   (constant) (signed-reg unsigned-reg))
157
158 ;;; ARG is a fixnum or bignum; figure out which and load if necessary.
159 (define-vop (move-to-word/integer)
160   (:args (x :scs (descriptor-reg)))
161   (:results (y :scs (signed-reg unsigned-reg)))
162   (:note "integer to untagged word coercion")
163   (:temporary (:scs (non-descriptor-reg)) temp)
164   (:generator 4
165     (let ((done (gen-label)))
166       (inst andi. temp x 3)
167       (sc-case y
168         (signed-reg
169          (inst srawi y x 2))
170         (unsigned-reg
171          (inst srwi y x 2)))
172
173       (inst beq done)
174       (loadw y x bignum-digits-offset other-pointer-lowtag)
175
176       (emit-label done))))
177 (define-move-vop move-to-word/integer :move
178   (descriptor-reg) (signed-reg unsigned-reg))
179
180 ;;; RESULT is a fixnum, so we can just shift.  We need the result type
181 ;;; restriction because of the control-stack ambiguity noted above.
182 (define-vop (move-from-word/fixnum)
183   (:args (x :scs (signed-reg unsigned-reg)))
184   (:results (y :scs (any-reg descriptor-reg)))
185   (:result-types tagged-num)
186   (:note "fixnum tagging")
187   (:generator 1
188     (inst slwi y x 2)))
189 (define-move-vop move-from-word/fixnum :move
190   (signed-reg unsigned-reg) (any-reg descriptor-reg))
191
192 ;;; RESULT may be a bignum, so we have to check.  Use a worst-case
193 ;;; cost to make sure people know they may be number consing.
194 (define-vop (move-from-signed)
195   (:args (arg :scs (signed-reg unsigned-reg) :target x))
196   (:results (y :scs (any-reg descriptor-reg)))
197   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) x temp)
198   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
199   (:note "signed word to integer coercion")
200   (:generator 20
201     (move x arg)
202     (let ((done (gen-label)))
203       (inst mtxer zero-tn)              ; clear sticky overflow bit in XER, CR0
204       (inst addo temp x x)              ; set XER OV if top two bits differ
205       (inst addo. temp temp temp)       ; set CR0 SO if any top three bits differ
206       (inst slwi y x 2)                 ; assume fixnum (tagged ok, maybe lost some high bits)
207       (inst bns done)
208
209       (with-fixed-allocation (y pa-flag temp bignum-widetag (1+ bignum-digits-offset))
210         (storew x y bignum-digits-offset other-pointer-lowtag))
211       (emit-label done))))
212 (define-move-vop move-from-signed :move
213   (signed-reg) (descriptor-reg))
214
215 ;;; Check for fixnum, and possibly allocate one or two word bignum
216 ;;; result.  Use a worst-case cost to make sure people know they may
217 ;;; be number consing.
218 (define-vop (move-from-unsigned)
219   (:args (arg :scs (signed-reg unsigned-reg) :target x))
220   (:results (y :scs (any-reg descriptor-reg)))
221   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) x temp)
222   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
223   (:note "unsigned word to integer coercion")
224   (:generator 20
225     (move x arg)
226     (let ((done (gen-label))
227           (one-word (gen-label)))
228       (inst srawi. temp x 29)
229       (inst slwi y x 2)
230       (inst beq done)
231
232       (with-fixed-allocation
233           (y pa-flag temp bignum-widetag (+ 2 bignum-digits-offset))
234         (inst cmpwi x 0)
235         (inst li temp (logior (ash 1 n-widetag-bits) bignum-widetag))
236         (inst bge one-word)
237         (inst li temp (logior (ash 2 n-widetag-bits) bignum-widetag))
238         (emit-label one-word)
239         (storew temp y 0 other-pointer-lowtag)
240         (storew x y bignum-digits-offset other-pointer-lowtag))
241       (emit-label done))))
242 (define-move-vop move-from-unsigned :move
243   (unsigned-reg) (descriptor-reg))
244
245
246 ;;; Move untagged numbers.
247 (define-vop (word-move)
248   (:args (x :target y
249             :scs (signed-reg unsigned-reg)
250             :load-if (not (location= x y))))
251   (:results (y :scs (signed-reg unsigned-reg)
252                :load-if (not (location= x y))))
253   (:effects)
254   (:affected)
255   (:note "word integer move")
256   (:generator 0
257     (move y x)))
258 (define-move-vop word-move :move
259   (signed-reg unsigned-reg) (signed-reg unsigned-reg))
260
261
262 ;;; Move untagged number arguments/return-values.
263 (define-vop (move-word-arg)
264   (:args (x :target y
265             :scs (signed-reg unsigned-reg))
266          (fp :scs (any-reg)
267              :load-if (not (sc-is y sap-reg))))
268   (:results (y))
269   (:note "word integer argument move")
270   (:generator 0
271     (sc-case y
272       ((signed-reg unsigned-reg)
273        (move y x))
274       ((signed-stack unsigned-stack)
275        (storew x fp (tn-offset y))))))
276 (define-move-vop move-word-arg :move-arg
277   (descriptor-reg any-reg signed-reg unsigned-reg) (signed-reg unsigned-reg))
278
279 ;;; Use standard MOVE-ARG + coercion to move an untagged number to a
280 ;;; descriptor passing location.
281 (define-move-vop move-arg :move-arg
282   (signed-reg unsigned-reg) (any-reg descriptor-reg))