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