1.0.24.35: Flag-setting VOPs on x86[-64] and conditional moves
[sbcl/pkhuong.git] / src / compiler / x86 / type-vops.lisp
blob9d53fe955d2d89efa997a4cfad3f725ccaf1186a
1 ;;;; type testing and checking VOPs for the x86 VM
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.
12 (in-package "SB!VM")
14 ;;;; test generation utilities
16 (defun generate-fixnum-test (value)
17 (emit-optimized-test-inst value 3))
19 (defun %test-fixnum (value target not-p)
20 (generate-fixnum-test value)
21 (inst jmp (if not-p :nz :z) target))
23 (defun %test-fixnum-and-headers (value target not-p headers)
24 (let ((drop-through (gen-label)))
25 (generate-fixnum-test value)
26 (inst jmp :z (if not-p drop-through target))
27 (%test-headers value target not-p nil headers drop-through)))
29 (defun %test-immediate (value target not-p immediate)
30 ;; Code a single instruction byte test if possible.
31 (let ((offset (tn-offset value)))
32 (cond ((and (sc-is value any-reg descriptor-reg)
33 (or (= offset eax-offset) (= offset ebx-offset)
34 (= offset ecx-offset) (= offset edx-offset)))
35 (inst cmp (make-random-tn :kind :normal
36 :sc (sc-or-lose 'byte-reg)
37 :offset offset)
38 immediate))
40 (move eax-tn value)
41 (inst cmp al-tn immediate))))
42 (inst jmp (if not-p :ne :e) target))
44 (defun %test-lowtag (value target not-p lowtag &optional al-loaded)
45 (unless al-loaded
46 (move eax-tn value)
47 (inst and al-tn lowtag-mask))
48 ;; FIXME: another 'optimization' which doesn't appear to work:
49 ;; prefetching the hypothetically pointed-to version should help,
50 ;; but this is in fact non-ideal in plenty of ways: we emit way too
51 ;; many of these prefetch instructions; pointed-to objects are very
52 ;; often in the cache anyway; etc. etc. Still, as proof-of-concept,
53 ;; not too bad. -- CSR, 2004-07-27
54 (when (member :prefetch *backend-subfeatures*)
55 (inst prefetchnta (make-ea :byte :base value :disp (- lowtag))))
56 (inst cmp al-tn lowtag)
57 (inst jmp (if not-p :ne :e) target))
59 (defun %test-headers (value target not-p function-p headers
60 &optional (drop-through (gen-label)) al-loaded)
61 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
62 (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
63 ;; EQUAL, LESS-OR-EQUAL and GREATER-OR-EQUAL are the conditions for
64 ;; branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
65 ;; labels to branch to when we know it's true and when we know
66 ;; it's false respectively.
67 (if not-p
68 (values :ne :a :b drop-through target)
69 (values :e :na :nb target drop-through))
70 (%test-lowtag value when-false t lowtag al-loaded)
71 (cond
72 ((and (null (cdr headers))
73 (numberp (car headers)))
74 ;; Optimize the common case: referencing the value from memory
75 ;; is slightly smaller than loading it and then doing the
76 ;; comparison. Doing this for other cases (e.g. range of
77 ;; [BIGNUM-WIDETAG..FOO-WIDETAG]) is also possible, but such
78 ;; opportunities don't come up very often and the code would
79 ;; get pretty hairy...
80 (inst cmp (make-ea :byte :base value :disp (- lowtag)) (car headers))
81 (inst jmp equal target))
83 (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
84 (do ((remaining headers (cdr remaining)))
85 ((null remaining))
86 (let ((header (car remaining))
87 (last (null (cdr remaining))))
88 (cond
89 ((atom header)
90 (cond
91 ((and (not last) (null (cddr remaining))
92 (atom (cadr remaining))
93 (= (logcount (logxor header (cadr remaining))) 1))
94 ;; BASE-STRING, (VECTOR NIL), BIT-VECTOR, (VECTOR T)
95 (inst and al-tn (ldb (byte 8 0) (logeqv header (cadr remaining))))
96 (inst cmp al-tn (ldb (byte 8 0) (logand header (cadr remaining))))
97 (inst jmp equal target)
98 (return))
100 (inst cmp al-tn header)
101 (if last
102 (inst jmp equal target)
103 (inst jmp :e when-true)))))
105 (let ((start (car header))
106 (end (cdr header)))
107 (cond
108 ;; LAST = don't need al-tn later
109 ((and last (not (= start bignum-widetag))
110 (= (+ start 4) end) (= (logcount (logxor start end)) 1))
111 ;; SIMPLE-STRING
112 (inst and al-tn (ldb (byte 8 0) (logeqv start end)))
113 (inst cmp al-tn (ldb (byte 8 0) (logand start end)))
114 (inst jmp equal target))
115 ((and (not last) (null (cddr remaining))
116 (= (+ start 4) end) (= (logcount (logxor start end)) 1)
117 (listp (cadr remaining))
118 (= (+ (caadr remaining) 4) (cdadr remaining))
119 (= (logcount (logxor (caadr remaining) (cdadr remaining))) 1)
120 (= (logcount (logxor (caadr remaining) start)) 1))
121 ;; STRING
122 (inst and al-tn (ldb (byte 8 0) (logeqv start (cdadr remaining))))
123 (inst cmp al-tn (ldb (byte 8 0) (logand start (cdadr remaining))))
124 (inst jmp equal target)
125 ;; we've shortcircuited the DO, so we must return.
126 ;; It's OK to do so, because (NULL (CDDR REMAINING))
127 ;; was true.
128 (return))
130 (unless (= start bignum-widetag)
131 (inst cmp al-tn start)
132 (if (= end complex-array-widetag)
133 (progn
134 (aver last)
135 (inst jmp greater-or-equal target))
136 (inst jmp :b when-false))) ; was :l
137 (unless (= end complex-array-widetag)
138 (inst cmp al-tn end)
139 (if last
140 (inst jmp less-or-equal target)
141 (inst jmp :be when-true)))))))))))) ; was :le
142 (emit-label drop-through))))
144 ;;;; type checking and testing
146 (define-vop (check-type)
147 (:args (value :target result :scs (any-reg descriptor-reg)))
148 (:results (result :scs (any-reg descriptor-reg)))
149 (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
150 (:ignore eax)
151 (:vop-var vop)
152 (:save-p :compute-only))
154 (define-vop (type-predicate)
155 (:args (value :scs (any-reg descriptor-reg)))
156 (:temporary (:sc unsigned-reg :offset eax-offset) eax)
157 (:ignore eax)
158 (:conditional)
159 (:info target not-p)
160 (:policy :fast-safe))
162 ;;; simpler VOP that don't need a temporary register
163 (define-vop (simple-check-type)
164 (:args (value :target result :scs (any-reg descriptor-reg)))
165 (:results (result :scs (any-reg descriptor-reg)
166 :load-if (not (and (sc-is value any-reg descriptor-reg)
167 (sc-is result control-stack)))))
168 (:vop-var vop)
169 (:save-p :compute-only))
171 (define-vop (simple-type-predicate)
172 (:args (value :scs (any-reg descriptor-reg control-stack)))
173 (:conditional)
174 (:info target not-p)
175 (:policy :fast-safe))
177 (defun cost-to-test-types (type-codes)
178 (+ (* 2 (length type-codes))
179 (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
181 (defmacro !define-type-vops (pred-name check-name ptype error-code
182 (&rest type-codes)
183 &key (variant nil variant-p) &allow-other-keys)
184 ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
185 ;; expansion?
186 (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
187 (prefix (if variant-p
188 (concatenate 'string (string variant) "-")
189 "")))
190 `(progn
191 ,@(when pred-name
192 `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
193 (:translate ,pred-name)
194 (:generator ,cost
195 (test-type value target not-p (,@type-codes))))))
196 ,@(when check-name
197 `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
198 (:generator ,cost
199 (let ((err-lab
200 (generate-error-code vop ',error-code value)))
201 (test-type value err-lab t (,@type-codes))
202 (move result value))))))
203 ,@(when ptype
204 `((primitive-type-vop ,check-name (:check) ,ptype))))))
206 ;;;; other integer ranges
208 (define-vop (fixnump/unsigned-byte-32 simple-type-predicate)
209 (:args (value :scs (unsigned-reg)))
210 (:info)
211 (:conditional :be)
212 (:arg-types unsigned-num)
213 (:translate fixnump)
214 (:generator 5
215 (inst cmp value #.sb!xc:most-positive-fixnum)))
217 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
218 ;;; exactly one digit.
220 (define-vop (signed-byte-32-p type-predicate)
221 (:translate signed-byte-32-p)
222 (:generator 45
223 (multiple-value-bind (yep nope)
224 (if not-p
225 (values not-target target)
226 (values target not-target))
227 (generate-fixnum-test value)
228 (inst jmp :e yep)
229 (move eax-tn value)
230 (inst and al-tn lowtag-mask)
231 (inst cmp al-tn other-pointer-lowtag)
232 (inst jmp :ne nope)
233 (loadw eax-tn value 0 other-pointer-lowtag)
234 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
235 (inst jmp (if not-p :ne :e) target))
236 NOT-TARGET))
238 (define-vop (check-signed-byte-32 check-type)
239 (:generator 45
240 (let ((nope (generate-error-code vop
241 'object-not-signed-byte-32-error
242 value)))
243 (generate-fixnum-test value)
244 (inst jmp :e yep)
245 (move eax-tn value)
246 (inst and al-tn lowtag-mask)
247 (inst cmp al-tn other-pointer-lowtag)
248 (inst jmp :ne nope)
249 (loadw eax-tn value 0 other-pointer-lowtag)
250 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
251 (inst jmp :ne nope))
253 (move result value)))
255 ;;; An (unsigned-byte 32) can be represented with either a positive
256 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
257 ;;; exactly two digits and the second digit all zeros.
258 (define-vop (unsigned-byte-32-p type-predicate)
259 (:translate unsigned-byte-32-p)
260 (:generator 45
261 (let ((not-target (gen-label))
262 (single-word (gen-label))
263 (fixnum (gen-label)))
264 (multiple-value-bind (yep nope)
265 (if not-p
266 (values not-target target)
267 (values target not-target))
268 ;; Is it a fixnum?
269 (generate-fixnum-test value)
270 (move eax-tn value)
271 (inst jmp :e fixnum)
273 ;; If not, is it an other pointer?
274 (inst and al-tn lowtag-mask)
275 (inst cmp al-tn other-pointer-lowtag)
276 (inst jmp :ne nope)
277 ;; Get the header.
278 (loadw eax-tn value 0 other-pointer-lowtag)
279 ;; Is it one?
280 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
281 (inst jmp :e single-word)
282 ;; If it's other than two, we can't be an (unsigned-byte 32)
283 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
284 (inst jmp :ne nope)
285 ;; Get the second digit.
286 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
287 ;; All zeros, its an (unsigned-byte 32).
288 (inst or eax-tn eax-tn)
289 (inst jmp :z yep)
290 (inst jmp nope)
292 (emit-label single-word)
293 ;; Get the single digit.
294 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
296 ;; positive implies (unsigned-byte 32).
297 (emit-label fixnum)
298 (inst or eax-tn eax-tn)
299 (inst jmp (if not-p :s :ns) target)
301 (emit-label not-target)))))
303 (define-vop (check-unsigned-byte-32 check-type)
304 (:generator 45
305 (let ((nope
306 (generate-error-code vop 'object-not-unsigned-byte-32-error value))
307 (yep (gen-label))
308 (fixnum (gen-label))
309 (single-word (gen-label)))
311 ;; Is it a fixnum?
312 (generate-fixnum-test value)
313 (move eax-tn value)
314 (inst jmp :e fixnum)
316 ;; If not, is it an other pointer?
317 (inst and al-tn lowtag-mask)
318 (inst cmp al-tn other-pointer-lowtag)
319 (inst jmp :ne nope)
320 ;; Get the header.
321 (loadw eax-tn value 0 other-pointer-lowtag)
322 ;; Is it one?
323 (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
324 (inst jmp :e single-word)
325 ;; If it's other than two, we can't be an (unsigned-byte 32)
326 (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
327 (inst jmp :ne nope)
328 ;; Get the second digit.
329 (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
330 ;; All zeros, its an (unsigned-byte 32).
331 (inst or eax-tn eax-tn)
332 (inst jmp :z yep)
333 (inst jmp nope)
335 (emit-label single-word)
336 ;; Get the single digit.
337 (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
339 ;; positive implies (unsigned-byte 32).
340 (emit-label fixnum)
341 (inst or eax-tn eax-tn)
342 (inst jmp :s nope)
344 (emit-label yep)
345 (move result value))))
347 ;;;; list/symbol types
349 ;;; symbolp (or symbol (eq nil))
350 ;;; consp (and list (not (eq nil)))
352 (define-vop (symbolp type-predicate)
353 (:translate symbolp)
354 (:generator 12
355 (let ((is-symbol-label (if not-p drop-thru target)))
356 (inst cmp value nil-value)
357 (inst jmp :e is-symbol-label)
358 (test-type value target not-p (symbol-header-widetag)))
359 DROP-THRU))
361 (define-vop (check-symbol check-type)
362 (:generator 12
363 (let ((error (generate-error-code vop 'object-not-symbol-error value)))
364 (inst cmp value nil-value)
365 (inst jmp :e drop-thru)
366 (test-type value error t (symbol-header-widetag)))
367 DROP-THRU
368 (move result value)))
370 (define-vop (consp type-predicate)
371 (:translate consp)
372 (:generator 8
373 (let ((is-not-cons-label (if not-p target drop-thru)))
374 (inst cmp value nil-value)
375 (inst jmp :e is-not-cons-label)
376 (test-type value target not-p (list-pointer-lowtag)))
377 DROP-THRU))
379 (define-vop (check-cons check-type)
380 (:generator 8
381 (let ((error (generate-error-code vop 'object-not-cons-error value)))
382 (inst cmp value nil-value)
383 (inst jmp :e error)
384 (test-type value error t (list-pointer-lowtag))
385 (move result value))))