8e92290db807f1812bf0a76dbdbc573939ef5230
[sbcl.git] / src / compiler / x86-64 / char.lisp
1 ;;;; x86-64 definition of character operations
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 \f
14 ;;; Space optimization: As the upper 32 bits of (tagged or untagged)
15 ;;; characters are always zero many operations can be done on 32-bit
16 ;;; registers. This often leads to smaller encodings as the REX prefix
17 ;;; is then only needed if registers R8 - R15 are used.
18
19 ;;;; moves and coercions
20
21 ;;; Move a tagged char to an untagged representation.
22 #!+sb-unicode
23 (define-vop (move-to-character)
24   (:args (x :scs (any-reg descriptor-reg) :target y
25             :load-if (not (location= x y))))
26   (:results (y :scs (character-reg)
27                :load-if (not (location= x y))))
28   (:note "character untagging")
29   (:generator 1
30     (cond ((and (sc-is y character-reg) (sc-is x any-reg descriptor-reg))
31            (let ((y-dword (make-dword-tn y)))
32              (unless (location= x y)
33                (inst mov y-dword (make-dword-tn x)))
34              (inst shr y-dword n-widetag-bits)))
35           (t
36            (move y x)
37            (inst shr y n-widetag-bits)))))
38 #!-sb-unicode
39 (define-vop (move-to-character)
40   (:args (x :scs (any-reg control-stack)))
41   (:results (y :scs (character-reg #+nil character-stack)))
42   (:note "character untagging")
43   (:generator 1
44     (let ((y-wide-tn (make-random-tn
45                       :kind :normal
46                       :sc (sc-or-lose 'any-reg)
47                       :offset (tn-offset y))))
48       (move y-wide-tn x)
49       (inst shr y-wide-tn 8)
50       (inst and y-wide-tn #xff))))
51 (define-move-vop move-to-character :move
52   (any-reg #!-sb-unicode control-stack)
53   (character-reg))
54
55 ;;; Move an untagged char to a tagged representation.
56 #!+sb-unicode
57 (define-vop (move-from-character)
58   (:args (x :scs (character-reg) :target y))
59   (:results (y :scs (any-reg descriptor-reg)))
60   (:note "character tagging")
61   (:generator 1
62     (let ((y-dword (make-dword-tn y)))
63       (unless (location= x y)
64         (inst mov y-dword (make-dword-tn x)))
65       (inst shl y-dword n-widetag-bits)
66       (inst or y-dword character-widetag))))
67 #!-sb-unicode
68 (define-vop (move-from-character)
69   (:args (x :scs (character-reg character-stack)))
70   (:results (y :scs (any-reg descriptor-reg #+nil control-stack)))
71   (:note "character tagging")
72   (:generator 1
73     (move (make-random-tn :kind :normal :sc (sc-or-lose 'character-reg)
74                           :offset (tn-offset y))
75           x)
76     (inst shl y n-widetag-bits)
77     (inst or y character-widetag)
78     (inst and y #xffff)))
79 (define-move-vop move-from-character :move
80   (character-reg)
81   (any-reg descriptor-reg #!-sb-unicode control-stack))
82
83 ;;; Move untagged character values.
84 (define-vop (character-move)
85   (:args (x :target y
86             :scs (character-reg)
87             :load-if (not (location= x y))))
88   (:results (y :scs (character-reg character-stack)
89                :load-if (not (location= x y))))
90   (:note "character move")
91   (:effects)
92   (:affected)
93   (:generator 0
94     (move y x)))
95 (define-move-vop character-move :move
96   (character-reg) (character-reg character-stack))
97
98 ;;; Move untagged character arguments/return-values.
99 (define-vop (move-character-arg)
100   (:args (x :target y
101             :scs (character-reg))
102          (fp :scs (any-reg)
103              :load-if (not (sc-is y character-reg))))
104   (:results (y))
105   (:note "character arg move")
106   (:generator 0
107     (sc-case y
108       (character-reg
109        (move y x))
110       (character-stack
111        #!-sb-unicode
112        (inst mov
113              ;; FIXME: naked 8 (should be... what?  n-register-bytes?
114              ;; n-word-bytes?  Dunno.
115              (make-ea :byte :base fp :disp (- (* (1+ (tn-offset y)) 8)))
116              x)
117        #!+sb-unicode
118        (if (= (tn-offset fp) esp-offset)
119            (storew x fp (tn-offset y)) ; c-call
120            (storew x fp (- (1+ (tn-offset y)))))))))
121 (define-move-vop move-character-arg :move-arg
122   (any-reg character-reg) (character-reg))
123
124 ;;; Use standard MOVE-ARG + coercion to move an untagged character
125 ;;; to a descriptor passing location.
126 (define-move-vop move-arg :move-arg
127   (character-reg) (any-reg descriptor-reg))
128 \f
129 ;;;; other operations
130
131 (define-vop (char-code)
132   (:translate char-code)
133   (:policy :fast-safe)
134   (:args #!-sb-unicode (ch :scs (character-reg character-stack))
135          #!+sb-unicode (ch :scs (character-reg character-stack) :target res))
136   (:arg-types character)
137   (:results (res :scs (unsigned-reg)))
138   (:result-types positive-fixnum)
139   (:generator 1
140     #!-sb-unicode
141     (inst movzx res ch)
142     #!+sb-unicode
143     (move res ch)))
144
145 #!+sb-unicode
146 (define-vop (code-char)
147   (:translate code-char)
148   (:policy :fast-safe)
149   (:args (code :scs (unsigned-reg unsigned-stack) :target res))
150   (:arg-types positive-fixnum)
151   (:results (res :scs (character-reg)))
152   (:result-types character)
153   (:generator 1
154     (move res code)))
155 #!-sb-unicode
156 (define-vop (code-char)
157   (:translate code-char)
158   (:policy :fast-safe)
159   (:args (code :scs (unsigned-reg unsigned-stack) :target eax))
160   (:arg-types positive-fixnum)
161   (:temporary (:sc unsigned-reg :offset rax-offset :target res
162                    :from (:argument 0) :to (:result 0))
163               eax)
164   (:results (res :scs (character-reg)))
165   (:result-types character)
166   (:generator 1
167     (move eax code)
168     (move res al-tn)))
169 \f
170 ;;; comparison of CHARACTERs
171 (define-vop (character-compare)
172   (:args (x :scs (character-reg character-stack))
173          (y :scs (character-reg)
174             :load-if (not (and (sc-is x character-reg)
175                                (sc-is y character-stack)))))
176   (:arg-types character character)
177   (:info)
178   (:policy :fast-safe)
179   (:note "inline comparison")
180   (:generator 3
181     (inst cmp x y)))
182
183 (define-vop (fast-char=/character character-compare)
184   (:translate char=)
185   (:conditional :e))
186
187 (define-vop (fast-char</character character-compare)
188   (:translate char<)
189   (:conditional :b))
190
191 (define-vop (fast-char>/character character-compare)
192   (:translate char>)
193   (:conditional :a))
194
195 (define-vop (character-compare/c)
196   (:args (x :scs (character-reg character-stack)))
197   (:arg-types character (:constant character))
198   (:info y)
199   (:policy :fast-safe)
200   (:note "inline constant comparison")
201   (:generator 2
202     (inst cmp x (sb!xc:char-code y))))
203
204 (define-vop (fast-char=/character/c character-compare/c)
205   (:translate char=)
206   (:conditional :e))
207
208 (define-vop (fast-char</character/c character-compare/c)
209   (:translate char<)
210   (:conditional :b))
211
212 (define-vop (fast-char>/character/c character-compare/c)
213   (:translate char>)
214   (:conditional :a))