724ab938dea6ebf5df1ad6bd4caba9155eec71d4
[sbcl.git] / src / compiler / hppa / move.lisp
1 ;;;; the HPPA 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 zero immediate)
16    (any-reg descriptor-reg))
17   (let ((val (tn-value x)))
18     (etypecase val
19       (integer
20        (inst li (fixnumize val) y))
21       (null
22        (move null-tn y))
23       (symbol
24        (load-symbol y val))
25       (character
26        (inst li (logior (ash (char-code val) n-widetag-bits)
27                         character-widetag) y)))))
28
29 (define-move-fun (load-number 1) (vop x y)
30   ((zero immediate)
31    (signed-reg unsigned-reg))
32   (inst li (tn-value x) y))
33
34 (define-move-fun (load-character 1) (vop x y)
35   ((immediate) (character-reg))
36   (inst li (char-code (tn-value x)) y))
37
38 (define-move-fun (load-system-area-pointer 1) (vop x y)
39   ((immediate) (sap-reg))
40   (inst li (sap-int (tn-value x)) y))
41
42 (define-move-fun (load-constant 5) (vop x y)
43   ((constant) (descriptor-reg any-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 null zero) (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 control-stack)
77                :load-if (not (location= x y))))
78   (:effects)
79   (:affected)
80   (:generator 0
81     (unless (location= x y)
82       (sc-case y
83         ((any-reg descriptor-reg)
84           (inst move x y))
85         (control-stack
86           (store-stack-tn y x))))))
87
88 (define-move-vop move :move
89   (any-reg descriptor-reg zero null)
90   (any-reg descriptor-reg))
91
92 ;;; Make MOVE the check VOP for T so that type check generation
93 ;;; doesn't think it is a hairy type.  This also allows checking of a
94 ;;; few of the values in a continuation to fall out.
95 (primitive-type-vop move (:check) t)
96
97 ;;; The MOVE-ARG VOP is used for moving descriptor values into another
98 ;;; frame for argument or known value passing.
99 (define-vop (move-arg)
100   (:args (x :target y
101             :scs (any-reg descriptor-reg null zero))
102          (fp :scs (any-reg)
103              :load-if (not (sc-is y any-reg descriptor-reg))))
104   (:results (y))
105   (:generator 0
106     (sc-case y
107       ((any-reg descriptor-reg)
108        (move x y))
109       (control-stack
110        (storew x fp (tn-offset y))))))
111 (define-move-vop move-arg :move-arg
112   (any-reg descriptor-reg null zero)
113   (any-reg descriptor-reg))
114
115 \f
116 ;;;; ILLEGAL-MOVE
117
118 ;;; This VOP exists just to begin the lifetime of a TN that couldn't
119 ;;; be written legally due to a type error.  An error is signalled
120 ;;; before this VOP is so we don't need to do anything (not that there
121 ;;; would be anything sensible to do anyway.)
122 (define-vop (illegal-move)
123   (:args (x) (type))
124   (:results (y))
125   (:ignore y)
126   (:vop-var vop)
127   (:save-p :compute-only)
128   (:generator 666
129     (error-call vop object-not-type-error x type)))
130 \f
131 ;;;; Moves and coercions:
132
133 ;;; These MOVE-TO-WORD VOPs move a tagged integer to a raw full-word
134 ;;; representation.  Similarly, the MOVE-FROM-WORD VOPs converts a raw integer
135 ;;; to a tagged bignum or fixnum.
136
137 ;;; ARG is a fixnum, so just shift it.  We need a type restriction
138 ;;; because some possible arg SCs (control-stack) overlap with
139 ;;; possible bignum arg SCs.
140 (define-vop (move-to-word/fixnum)
141   (:args (x :scs (any-reg descriptor-reg)))
142   (:results (y :scs (signed-reg unsigned-reg)))
143   (:arg-types tagged-num)
144   (:note "fixnum untagging")
145   (:generator 1
146     (inst sra x 2 y)))
147
148 (define-move-vop move-to-word/fixnum :move
149   (any-reg descriptor-reg) (signed-reg unsigned-reg))
150
151 ;;; ARG is a non-immediate constant, load it.
152 (define-vop (move-to-word-c)
153   (:args (x :scs (constant)))
154   (:results (y :scs (signed-reg unsigned-reg)))
155   (:note "constant load")
156   (:generator 1
157     (inst li (tn-value x) y)))
158
159 (define-move-vop move-to-word-c :move
160   (constant) (signed-reg unsigned-reg))
161
162 ;;; ARG is a fixnum or bignum, figure out which and load if necessary.
163 (define-vop (move-to-word/integer)
164   (:args (x :scs (descriptor-reg)))
165   (:results (y :scs (signed-reg unsigned-reg)))
166   (:note "integer to untagged word coercion")
167   (:generator 3
168     (inst sra x 2 y)
169     (inst extru x 31 2 zero-tn :=)
170     (loadw y x bignum-digits-offset other-pointer-lowtag)))
171
172 (define-move-vop move-to-word/integer :move
173   (descriptor-reg) (signed-reg unsigned-reg))
174
175 ;;; RESULT is a fixnum, so we can just shift.  We need the result type
176 ;;; restriction because of the control-stack ambiguity noted above.
177 (define-vop (move-from-word/fixnum)
178   (:args (x :scs (signed-reg unsigned-reg)))
179   (:results (y :scs (any-reg descriptor-reg)))
180   (:result-types tagged-num)
181   (:note "fixnum tagging")
182   (:generator 1
183     (inst sll x 2 y)))
184
185 (define-move-vop move-from-word/fixnum :move
186   (signed-reg unsigned-reg) (any-reg descriptor-reg))
187
188 ;;; RESULT may be a bignum, so we have to check.  Use a worst-case
189 ;;; cost to make sure people know they may be number consing.
190 (define-vop (move-from-signed)
191   (:args (arg :scs (signed-reg unsigned-reg) :target x))
192   (:results (y :scs (any-reg descriptor-reg)))
193   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) x temp)
194   (:note "signed word to integer coercion")
195   (:generator 18
196     (move arg x)
197     (let ((done (gen-label)))
198       ;; Extract the top three bits.
199       (inst extrs x 2 3 temp :=)
200       ;; Invert them (unless they are already zero).
201       (inst uaddcm zero-tn temp temp)
202       ;; If we are left with zero, it will fit in a fixnum.  So branch around
203       ;; the bignum-construction, doing the shift in the delay slot.
204       (inst comb := temp zero-tn done)
205       (inst sll x 2 y)
206       ;; Make a single-digit bignum.
207       (with-fixed-allocation
208           (y nil temp bignum-widetag (1+ bignum-digits-offset) nil)
209         (storew x y bignum-digits-offset other-pointer-lowtag))
210       (emit-label done))))
211
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   (:note "unsigned word to integer coercion")
220   (:args (arg :scs (signed-reg unsigned-reg) :target x))
221   (:results (y :scs (any-reg descriptor-reg)))
222   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) x temp)
223   (:generator 20
224     (move arg x)
225     (inst srl x 29 temp)
226     (inst comb := temp zero-tn done)
227     (inst sll x 2 y)
228     (pseudo-atomic (:extra (pad-data-block (+ bignum-digits-offset 2)))
229       (set-lowtag other-pointer-lowtag alloc-tn y)
230       (inst xor temp temp temp)
231       (inst comclr x zero-tn zero-tn :>=)
232       (inst li 1 temp)
233       (inst sll temp n-widetag-bits temp)
234       (inst addi (logior (ash 1 n-widetag-bits) bignum-widetag) temp temp)
235       (storew temp y 0 other-pointer-lowtag))
236
237     (storew x y bignum-digits-offset other-pointer-lowtag)
238     DONE))
239
240 (define-move-vop move-from-unsigned :move
241   (unsigned-reg) (descriptor-reg))
242
243 ;;; Move untagged numbers.
244 (define-vop (word-move)
245   (:args (x :target y
246             :scs (signed-reg unsigned-reg)
247             :load-if (not (location= x y))))
248   (:results (y :scs (signed-reg unsigned-reg)
249                :load-if (not (location= x y))))
250   (:effects)
251   (:affected)
252   (:note "word integer move")
253   (:generator 0
254     (move x y)))
255
256 (define-move-vop word-move :move
257   (signed-reg unsigned-reg) (signed-reg unsigned-reg))
258
259 ;;; Move untagged number args/return-values.
260 (define-vop (move-word-arg)
261   (:args (x :target y
262             :scs (signed-reg unsigned-reg))
263          (fp :scs (any-reg)
264              :load-if (not (sc-is y sap-reg))))
265   (:results (y))
266   (:note "word integer argument move")
267   (:generator 0
268     (sc-case y
269       ((signed-reg unsigned-reg)
270        (move x y))
271       ((signed-stack unsigned-stack)
272        (storew x fp (tn-offset y))))))
273
274 (define-move-vop move-word-arg :move-arg
275   (descriptor-reg any-reg signed-reg unsigned-reg) (signed-reg unsigned-reg))
276
277 ;;; Use standard MOVE-ARG + coercion to move an untagged number to a
278 ;;; descriptor passing location.
279 (define-move-vop move-arg :move-arg
280   (signed-reg unsigned-reg) (any-reg descriptor-reg))