0.8.1.31:
[sbcl.git] / src / compiler / generic / early-type-vops.lisp
1 ;;;; generic type testing and checking apparatus
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 (in-package "SB!VM")
12 \f
13 (defparameter *immediate-types*
14   (list unbound-marker-widetag base-char-widetag))
15
16 (defparameter *fun-header-widetags*
17   (list funcallable-instance-header-widetag
18         simple-fun-header-widetag
19         closure-fun-header-widetag
20         closure-header-widetag))
21
22 (defun canonicalize-headers (headers)
23   (collect ((results))
24     (let ((start nil)
25           (prev nil)
26           (delta (- other-immediate-1-lowtag other-immediate-0-lowtag)))
27       (flet ((emit-test ()
28                (results (if (= start prev)
29                             start
30                             (cons start prev)))))
31         (dolist (header (sort headers #'<))
32           (cond ((null start)
33                  (setf start header)
34                  (setf prev header))
35                 ((= header (+ prev delta))
36                  (setf prev header))
37                 (t
38                  (emit-test)
39                  (setf start header)
40                  (setf prev header))))
41         (emit-test)))
42     (results)))
43
44 (defmacro test-type (value target not-p
45                      (&rest type-codes)
46                      &rest other-args
47                      &key &allow-other-keys)
48   ;; Determine what interesting combinations we need to test for.
49   (let* ((type-codes (mapcar #'eval type-codes))
50          (fixnump (and (member even-fixnum-lowtag type-codes)
51                        (member odd-fixnum-lowtag type-codes)
52                        t))
53          (lowtags (remove lowtag-limit type-codes :test #'<))
54          (extended (remove lowtag-limit type-codes :test #'>))
55          (immediates (intersection extended *immediate-types* :test #'eql))
56          (headers (set-difference extended *immediate-types* :test #'eql))
57          (function-p (if (intersection headers *fun-header-widetags*)
58                          (if (subsetp headers *fun-header-widetags*)
59                              t
60                              (error "can't test for mix of function subtypes ~
61                                      and normal header types"))
62                          nil)))
63     (unless type-codes
64       (error "At least one type must be supplied for TEST-TYPE."))
65     (cond
66       (fixnump
67        (when (remove-if (lambda (x)
68                           (or (= x even-fixnum-lowtag)
69                               (= x odd-fixnum-lowtag)))
70                         lowtags)
71          (error "can't mix fixnum testing with other lowtags"))
72        (when function-p
73          (error "can't mix fixnum testing with function subtype testing"))
74        (when immediates
75          (error "can't mix fixnum testing with other immediates"))
76        (if headers
77            `(%test-fixnum-and-headers ,value ,target ,not-p
78              ',(canonicalize-headers headers)
79              ,@other-args)
80            `(%test-fixnum ,value ,target ,not-p
81              ,@other-args)))
82       (immediates
83        (when headers
84          (error "can't mix testing of immediates with testing of headers"))
85        (when lowtags
86          (error "can't mix testing of immediates with testing of lowtags"))
87        (when (cdr immediates)
88          (error "can't test multiple immediates at the same time"))
89        `(%test-immediate ,value ,target ,not-p ,(car immediates)
90          ,@other-args))
91       (lowtags
92        (when (cdr lowtags)
93          (error "can't test multiple lowtags at the same time"))
94        (when headers
95          (error "can't test non-fixnum lowtags and headers at the same time"))
96        `(%test-lowtag ,value ,target ,not-p ,(car lowtags) ,@other-args))
97       (headers
98        `(%test-headers ,value ,target ,not-p ,function-p
99          ',(canonicalize-headers headers)
100          ,@other-args))
101       (t
102        (error "nothing to test?")))))
103