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