1 ;;;; This file contains stuff that implements the portable IR1
2 ;;;; semantics of type tests and coercion. The main thing we do is
3 ;;;; convert complex type operations into simpler code that can be
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
17 ;;;; type predicate translation
19 ;;;; We maintain a bidirectional association between type predicates
20 ;;;; and the tested type. The presence of a predicate in this
21 ;;;; association implies that it is desirable to implement tests of
22 ;;;; this type using the predicate. These are either predicates that
23 ;;;; the back end is likely to have special knowledge about, or
24 ;;;; predicates so complex that the only reasonable implentation is
25 ;;;; via function call.
27 ;;;; Some standard types (such as SEQUENCE) are best tested by letting
28 ;;;; the TYPEP source transform do its thing with the expansion. These
29 ;;;; types (and corresponding predicates) are not maintained in this
30 ;;;; association. In this case, there need not be any predicate
31 ;;;; function unless it is required by the Common Lisp specification.
33 ;;;; The mapping between predicates and type structures is considered
34 ;;;; part of the backend; different backends can support different
35 ;;;; sets of predicates.
37 ;;; Establish an association between the type predicate NAME and the
38 ;;; corresponding TYPE. This causes the type predicate to be
39 ;;; recognized for purposes of optimization.
40 (defmacro define-type-predicate (name type)
41 `(%define-type-predicate ',name ',type))
42 (defun %define-type-predicate (name specifier)
43 (let ((type (specifier-type specifier)))
44 (setf (gethash name *backend-predicate-types*) type)
45 (setf *backend-type-predicates*
46 (cons (cons type name)
47 (remove name *backend-type-predicates*
49 (%deftransform name '(function (t) *) #'fold-type-predicate)
54 ;;; If we discover the type argument is constant during IR1
55 ;;; optimization, then give the source transform another chance. The
56 ;;; source transform can't pass, since we give it an explicit
57 ;;; constant. At worst, it will convert to %TYPEP, which will prevent
58 ;;; spurious attempts at transformation (and possible repeated
60 (deftransform typep ((object type))
61 (unless (constant-continuation-p type)
62 (give-up-ir1-transform "can't open-code test of non-constant type"))
63 `(typep object ',(continuation-value type)))
65 ;;; If the continuation OBJECT definitely is or isn't of the specified
66 ;;; type, then return T or NIL as appropriate. Otherwise quietly
67 ;;; GIVE-UP-IR1-TRANSFORM.
68 (defun ir1-transform-type-predicate (object type)
69 (declare (type continuation object) (type ctype type))
70 (let ((otype (continuation-type object)))
71 (cond ((not (types-equal-or-intersect otype type))
73 ((csubtypep otype type)
76 (give-up-ir1-transform)))))
78 ;;; Flush %TYPEP tests whose result is known at compile time.
79 (deftransform %typep ((object type))
80 (unless (constant-continuation-p type)
81 (give-up-ir1-transform))
82 (ir1-transform-type-predicate
84 (specifier-type (continuation-value type))))
86 ;;; This is the IR1 transform for simple type predicates. It checks
87 ;;; whether the single argument is known to (not) be of the
88 ;;; appropriate type, expanding to T or NIL as appropriate.
89 (deftransform fold-type-predicate ((object) * * :node node :defun-only t)
90 (let ((ctype (gethash (leaf-name
93 (basic-combination-fun node))))
94 *backend-predicate-types*)))
96 (ir1-transform-type-predicate object ctype)))
98 ;;; If FIND-CLASS is called on a constant class, locate the CLASS-CELL
100 (deftransform find-class ((name) ((constant-argument symbol)) *
102 (let* ((name (continuation-value name))
103 (cell (find-class-cell name)))
104 `(or (class-cell-class ',cell)
105 (error "class not yet defined: ~S" name))))
107 ;;;; standard type predicates, i.e. those defined in package COMMON-LISP,
108 ;;;; plus at least one oddball (%INSTANCEP)
110 ;;;; Various other type predicates (e.g. low-level representation
111 ;;;; stuff like SIMPLE-ARRAY-SINGLE-FLOAT-P) are defined elsewhere.
113 ;;; FIXME: This function is only called once, at top level. Why not
114 ;;; just expand all its operations into toplevel code?
115 (defun !define-standard-type-predicates ()
116 (define-type-predicate arrayp array)
117 ; (The ATOM predicate is handled separately as (NOT CONS).)
118 (define-type-predicate bit-vector-p bit-vector)
119 (define-type-predicate characterp character)
120 (define-type-predicate compiled-function-p compiled-function)
121 (define-type-predicate complexp complex)
122 (define-type-predicate complex-rational-p (complex rational))
123 (define-type-predicate complex-float-p (complex float))
124 (define-type-predicate consp cons)
125 (define-type-predicate floatp float)
126 (define-type-predicate functionp function)
127 (define-type-predicate integerp integer)
128 (define-type-predicate keywordp keyword)
129 (define-type-predicate listp list)
130 (define-type-predicate null null)
131 (define-type-predicate numberp number)
132 (define-type-predicate rationalp rational)
133 (define-type-predicate realp real)
134 (define-type-predicate simple-bit-vector-p simple-bit-vector)
135 (define-type-predicate simple-string-p simple-string)
136 (define-type-predicate simple-vector-p simple-vector)
137 (define-type-predicate stringp string)
138 (define-type-predicate %instancep instance)
139 (define-type-predicate funcallable-instance-p funcallable-instance)
140 (define-type-predicate symbolp symbol)
141 (define-type-predicate vectorp vector))
142 (!define-standard-type-predicates)
144 ;;;; transforms for type predicates not implemented primitively
146 ;;;; See also VM dependent transforms.
148 (def-source-transform atom (x)
151 ;;;; TYPEP source transform
153 ;;; Return a form that tests the variable N-OBJECT for being in the
154 ;;; binds specified by TYPE. BASE is the name of the base type, for
155 ;;; declaration. We make SAFETY locally 0 to inhibit any checking of
157 #!-negative-zero-is-not-zero
158 (defun transform-numeric-bound-test (n-object type base)
159 (declare (type numeric-type type))
160 (let ((low (numeric-type-low type))
161 (high (numeric-type-high type)))
163 (declare (optimize (safety 0)))
166 `((> (the ,base ,n-object) ,(car low)))
167 `((>= (the ,base ,n-object) ,low))))
170 `((< (the ,base ,n-object) ,(car high)))
171 `((<= (the ,base ,n-object) ,high))))))))
173 #!+negative-zero-is-not-zero
174 (defun transform-numeric-bound-test (n-object type base)
175 (declare (type numeric-type type))
176 (let ((low (numeric-type-low type))
177 (high (numeric-type-high type))
178 (float-type-p (csubtypep type (specifier-type 'float)))
182 (declare (optimize (safety 0)))
185 `((let ((,x (the ,base ,n-object))
187 ,(if (not float-type-p)
189 `(if (and (zerop ,x) (zerop ,y))
190 (> (float-sign ,x) (float-sign ,y))
192 `((let ((,x (the ,base ,n-object))
194 ,(if (not float-type-p)
196 `(if (and (zerop ,x) (zerop ,y))
197 (>= (float-sign ,x) (float-sign ,y))
201 `((let ((,x (the ,base ,n-object))
203 ,(if (not float-type-p)
205 `(if (and (zerop ,x) (zerop ,y))
206 (< (float-sign ,x) (float-sign ,y))
208 `((let ((,x (the ,base ,n-object))
210 ,(if (not float-type-p)
212 `(if (and (zerop ,x) (zerop ,y))
213 (<= (float-sign ,x) (float-sign ,y))
216 ;;; Do source transformation of a test of a known numeric type. We can
217 ;;; assume that the type doesn't have a corresponding predicate, since
218 ;;; those types have already been picked off. In particular, CLASS
219 ;;; must be specified, since it is unspecified only in NUMBER and
220 ;;; COMPLEX. Similarly, we assume that COMPLEXP is always specified.
222 ;;; For non-complex types, we just test that the number belongs to the
223 ;;; base type, and then test that it is in bounds. When CLASS is
224 ;;; INTEGER, we check to see whether the range is no bigger than
225 ;;; FIXNUM. If so, we check for FIXNUM instead of INTEGER. This allows
226 ;;; us to use fixnum comparison to test the bounds.
228 ;;; For complex types, we must test for complex, then do the above on
229 ;;; both the real and imaginary parts. When CLASS is float, we need
230 ;;; only check the type of the realpart, since the format of the
231 ;;; realpart and the imagpart must be the same.
232 (defun source-transform-numeric-typep (object type)
233 (let* ((class (numeric-type-class type))
235 (integer (containing-integer-type type))
237 (float (or (numeric-type-format type) 'float))
239 (once-only ((n-object object))
240 (ecase (numeric-type-complexp type)
242 `(and (typep ,n-object ',base)
243 ,(transform-numeric-bound-test n-object type base)))
245 `(and (complexp ,n-object)
246 ,(once-only ((n-real `(realpart (the complex ,n-object)))
247 (n-imag `(imagpart (the complex ,n-object))))
250 (and (typep ,n-real ',base)
251 ,@(when (eq class 'integer)
252 `((typep ,n-imag ',base)))
253 ,(transform-numeric-bound-test n-real type base)
254 ,(transform-numeric-bound-test n-imag type
257 ;;; Do the source transformation for a test of a hairy type. AND,
258 ;;; SATISFIES and NOT are converted into the obvious code. We convert
259 ;;; unknown types to %TYPEP, emitting an efficiency note if
261 (defun source-transform-hairy-typep (object type)
262 (declare (type hairy-type type))
263 (let ((spec (hairy-type-specifier type)))
264 (cond ((unknown-type-p type)
265 (when (policy *lexenv* (> speed inhibit-warnings))
266 (compiler-note "can't open-code test of unknown type ~S"
267 (type-specifier type)))
268 `(%typep ,object ',spec))
271 (satisfies `(if (funcall #',(second spec) ,object) t nil))
273 (once-only ((n-obj object))
274 `(,(first spec) ,@(mapcar #'(lambda (x)
278 ;;; Do source transformation for TYPEP of a known union type. If a
279 ;;; union type contains LIST, then we pull that out and make it into a
280 ;;; single LISTP call. Note that if SYMBOL is in the union, then LIST
281 ;;; will be a subtype even without there being any (member NIL). We
282 ;;; just drop through to the general code in this case, rather than
283 ;;; trying to optimize it.
284 (defun source-transform-union-typep (object type)
285 (let* ((types (union-type-types type))
286 (ltype (specifier-type 'list))
287 (mtype (find-if #'member-type-p types)))
288 (if (and mtype (csubtypep ltype type))
289 (let ((members (member-type-members mtype)))
290 (once-only ((n-obj object))
293 '(or ,@(mapcar #'type-specifier
294 (remove (specifier-type 'cons)
295 (remove mtype types)))
296 (member ,@(remove nil members)))))))
297 (once-only ((n-obj object))
298 `(or ,@(mapcar (lambda (x)
299 `(typep ,n-obj ',(type-specifier x)))
302 ;;; Do source transformation for TYPEP of a known intersection type.
303 (defun source-transform-intersection-typep (object type)
304 (once-only ((n-obj object))
305 `(and ,@(mapcar (lambda (x)
306 `(typep ,n-obj ',(type-specifier x)))
307 (intersection-type-types type)))))
309 ;;; If necessary recurse to check the cons type.
310 (defun source-transform-cons-typep (object type)
311 (let* ((car-type (cons-type-car-type type))
312 (cdr-type (cons-type-cdr-type type)))
313 (let ((car-test-p (not (or (type= car-type *wild-type*)
314 (type= car-type (specifier-type t)))))
315 (cdr-test-p (not (or (type= cdr-type *wild-type*)
316 (type= cdr-type (specifier-type t))))))
317 (if (and (not car-test-p) (not cdr-test-p))
319 (once-only ((n-obj object))
322 `((typep (car ,n-obj)
323 ',(type-specifier car-type))))
325 `((typep (cdr ,n-obj)
326 ',(type-specifier cdr-type))))))))))
328 ;;; Return the predicate and type from the most specific entry in
329 ;;; *TYPE-PREDICATES* that is a supertype of TYPE.
330 (defun find-supertype-predicate (type)
331 (declare (type ctype type))
334 (dolist (x *backend-type-predicates*)
335 (let ((stype (car x)))
336 (when (and (csubtypep type stype)
338 (csubtypep stype res-type)))
339 (setq res-type stype)
340 (setq res (cdr x)))))
341 (values res res-type)))
343 ;;; Return forms to test that OBJ has the rank and dimensions
344 ;;; specified by TYPE, where STYPE is the type we have checked against
345 ;;; (which is the same but for dimensions.)
346 (defun test-array-dimensions (obj type stype)
347 (declare (type array-type type stype))
348 (let ((obj `(truly-the ,(type-specifier stype) ,obj))
349 (dims (array-type-dimensions type)))
352 (when (eq (array-type-dimensions stype) '*)
353 (res `(= (array-rank ,obj) ,(length dims))))
355 (dim dims (cdr dim)))
357 (let ((dim (car dim)))
359 (res `(= (array-dimension ,obj ,i) ,dim)))))
362 ;;; If we can find a type predicate that tests for the type without
363 ;;; dimensions, then use that predicate and test for dimensions.
364 ;;; Otherwise, just do %TYPEP.
365 (defun source-transform-array-typep (obj type)
366 (multiple-value-bind (pred stype) (find-supertype-predicate type)
367 (if (and (array-type-p stype)
368 ;; (If the element type hasn't been defined yet, it's
369 ;; not safe to assume here that it will eventually
370 ;; have (UPGRADED-ARRAY-ELEMENT-TYPE type)=T, so punt.)
371 (not (unknown-type-p (array-type-element-type type)))
372 (type= (array-type-specialized-element-type stype)
373 (array-type-specialized-element-type type))
374 (eq (array-type-complexp stype) (array-type-complexp type)))
375 (once-only ((n-obj obj))
377 ,@(test-array-dimensions n-obj type stype)))
378 `(%typep ,obj ',(type-specifier type)))))
380 ;;; Transform a type test against some instance type. The type test is
381 ;;; flushed if the result is known at compile time. If not properly
382 ;;; named, error. If sealed and has no subclasses, just test for
383 ;;; layout-EQ. If a structure then test for layout-EQ and then a
384 ;;; general test based on layout-inherits. If safety is important,
385 ;;; then we also check whether the layout for the object is invalid
386 ;;; and signal an error if so. Otherwise, look up the indirect
387 ;;; class-cell and call CLASS-CELL-TYPEP at runtime.
388 (deftransform %instance-typep ((object spec) (* *) * :node node :when :both)
389 (aver (constant-continuation-p spec))
390 (let* ((spec (continuation-value spec))
391 (class (specifier-type spec))
392 (name (sb!xc:class-name class))
393 (otype (continuation-type object))
394 (layout (let ((res (info :type :compiler-layout name)))
395 (if (and res (not (layout-invalid res)))
399 ;; Flush tests whose result is known at compile time.
400 ((not (types-equal-or-intersect otype class))
402 ((csubtypep otype class)
404 ;; If not properly named, error.
405 ((not (and name (eq (sb!xc:find-class name) class)))
406 (compiler-error "can't compile TYPEP of anonymous or undefined ~
410 ;; Delay the type transform to give type propagation a chance.
411 (delay-ir1-transform node :constraint)
413 ;; Otherwise transform the type test.
414 (multiple-value-bind (pred get-layout)
416 ((csubtypep class (specifier-type 'funcallable-instance))
417 (values 'funcallable-instance-p '%funcallable-instance-layout))
418 ((csubtypep class (specifier-type 'instance))
419 (values '%instancep '%instance-layout))
421 (values '(lambda (x) (declare (ignore x)) t) 'layout-of)))
423 ((and (eq (class-state class) :sealed) layout
424 (not (class-subclasses class)))
425 ;; Sealed and has no subclasses.
426 (let ((n-layout (gensym)))
428 (let ((,n-layout (,get-layout object)))
429 ,@(when (policy *lexenv* (>= safety speed))
430 `((when (layout-invalid ,n-layout)
431 (%layout-invalid-error object ',layout))))
432 (eq ,n-layout ',layout)))))
433 ((and (typep class 'basic-structure-class) layout)
434 ;; structure type tests; hierarchical layout depths
435 (let ((depthoid (layout-depthoid layout))
438 (let ((,n-layout (,get-layout object)))
439 ,@(when (policy *lexenv* (>= safety speed))
440 `((when (layout-invalid ,n-layout)
441 (%layout-invalid-error object ',layout))))
442 (if (eq ,n-layout ',layout)
444 (and (> (layout-depthoid ,n-layout)
446 (locally (declare (optimize (safety 0)))
447 (eq (svref (layout-inherits ,n-layout)
450 ((and layout (>= (layout-depthoid layout) 0))
451 ;; hierarchical layout depths for other things (e.g.
453 (let ((depthoid (layout-depthoid layout))
455 (n-inherits (gensym)))
457 (let ((,n-layout (,get-layout object)))
458 ,@(when (policy *lexenv* (>= safety speed))
459 `((when (layout-invalid ,n-layout)
460 (%layout-invalid-error object ',layout))))
461 (if (eq ,n-layout ',layout)
463 (let ((,n-inherits (layout-inherits ,n-layout)))
464 (declare (optimize (safety 0)))
465 (and (> (length ,n-inherits) ,depthoid)
466 (eq (svref ,n-inherits ,depthoid)
469 (/noshow "default case -- ,PRED and CLASS-CELL-TYPEP")
471 (class-cell-typep (,get-layout object)
472 ',(find-class-cell name)
475 ;;; If the specifier argument is a quoted constant, then we consider
476 ;;; converting into a simple predicate or other stuff. If the type is
477 ;;; constant, but we can't transform the call, then we convert to
478 ;;; %TYPEP. We only pass when the type is non-constant. This allows us
479 ;;; to recognize between calls that might later be transformed
480 ;;; successfully when a constant type is discovered. We don't give an
481 ;;; efficiency note when we pass, since the IR1 transform will give
482 ;;; one if necessary and appropriate.
484 ;;; If the type is TYPE= to a type that has a predicate, then expand
485 ;;; to that predicate. Otherwise, we dispatch off of the type's type.
486 ;;; These transformations can increase space, but it is hard to tell
487 ;;; when, so we ignore policy and always do them.
488 (def-source-transform typep (object spec)
489 ;; KLUDGE: It looks bad to only do this on explicitly quoted forms,
490 ;; since that would overlook other kinds of constants. But it turns
491 ;; out that the DEFTRANSFORM for TYPEP detects any constant
492 ;; continuation, transforms it into a quoted form, and gives this
493 ;; source transform another chance, so it all works out OK, in a
494 ;; weird roundabout way. -- WHN 2001-03-18
495 (if (and (consp spec) (eq (car spec) 'quote))
496 (let ((type (specifier-type (cadr spec))))
497 (or (let ((pred (cdr (assoc type *backend-type-predicates*
499 (when pred `(,pred ,object)))
502 (source-transform-hairy-typep object type))
504 (source-transform-union-typep object type))
506 (source-transform-intersection-typep object type))
508 `(member ,object ',(member-type-members type)))
510 (compiler-warning "illegal type specifier for TYPEP: ~S"
512 `(%typep ,object ,spec))
516 (source-transform-numeric-typep object type))
518 `(%instance-typep ,object ,spec))
520 (source-transform-array-typep object type))
522 (source-transform-cons-typep object type))
524 `(%typep ,object ,spec)))
529 (deftransform coerce ((x type) (* *) * :when :both)
530 (unless (constant-continuation-p type)
531 (give-up-ir1-transform))
532 (let ((tspec (specifier-type (continuation-value type))))
533 (if (csubtypep (continuation-type x) tspec)
535 ;; Note: The THE here makes sure that specifiers like
536 ;; (SINGLE-FLOAT 0.0 1.0) can raise a TYPE-ERROR.
537 `(the ,(continuation-value type)
539 ((csubtypep tspec (specifier-type 'double-float))
541 ;; FIXME: #!+long-float (t ,(error "LONG-FLOAT case needed"))
542 ((csubtypep tspec (specifier-type 'float))
544 ((csubtypep tspec (specifier-type 'simple-vector))
545 '(coerce-to-simple-vector x))
547 (give-up-ir1-transform)))))))