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.
15 ;;; FIXME: Many of the functions in this file could probably be
16 ;;; byte-compiled, since they're one-pass, cons-heavy code.
20 ;;;; type predicate translation
22 ;;;; We maintain a bidirectional association between type predicates
23 ;;;; and the tested type. The presence of a predicate in this
24 ;;;; association implies that it is desirable to implement tests of
25 ;;;; this type using the predicate. These are either predicates that
26 ;;;; the back end is likely to have special knowledge about, or
27 ;;;; predicates so complex that the only reasonable implentation is
28 ;;;; via function call.
30 ;;;; Some standard types (such as SEQUENCE) are best tested by letting
31 ;;;; the TYPEP source transform do its thing with the expansion. These
32 ;;;; types (and corresponding predicates) are not maintained in this
33 ;;;; association. In this case, there need not be any predicate
34 ;;;; function unless it is required by the Common Lisp specification.
36 ;;;; The mapping between predicates and type structures is considered
37 ;;;; part of the backend; different backends can support different
38 ;;;; sets of predicates.
40 ;;; Establish an association between the type predicate NAME and the
41 ;;; corresponding TYPE. This causes the type predicate to be
42 ;;; recognized for purposes of optimization.
43 (defmacro define-type-predicate (name type)
44 `(%define-type-predicate ',name ',type))
45 (defun %define-type-predicate (name specifier)
46 (let ((type (specifier-type specifier)))
47 (setf (gethash name *backend-predicate-types*) type)
48 (setf *backend-type-predicates*
49 (cons (cons type name)
50 (remove name *backend-type-predicates*
52 (%deftransform name '(function (t) *) #'fold-type-predicate)
57 ;;; If we discover the type argument is constant during IR1
58 ;;; optimization, then give the source transform another chance. The
59 ;;; source transform can't pass, since we give it an explicit
60 ;;; constant. At worst, it will convert to %TYPEP, which will prevent
61 ;;; spurious attempts at transformation (and possible repeated
63 (deftransform typep ((object type))
64 (unless (constant-continuation-p type)
65 (give-up-ir1-transform "can't open-code test of non-constant type"))
66 `(typep object ',(continuation-value type)))
68 ;;; If the continuation OBJECT definitely is or isn't of the specified
69 ;;; type, then return T or NIL as appropriate. Otherwise quietly
70 ;;; GIVE-UP-IR1-TRANSFORM.
71 (defun ir1-transform-type-predicate (object type)
72 (declare (type continuation object) (type ctype type))
73 (let ((otype (continuation-type object)))
74 (cond ((not (types-equal-or-intersect otype type))
76 ((csubtypep otype type)
79 (give-up-ir1-transform)))))
81 ;;; Flush %TYPEP tests whose result is known at compile time.
82 (deftransform %typep ((object type))
83 (unless (constant-continuation-p type) (give-up-ir1-transform))
84 (ir1-transform-type-predicate
86 (specifier-type (continuation-value type))))
88 ;;; This is the IR1 transform for simple type predicates. It checks
89 ;;; whether the single argument is known to (not) be of the
90 ;;; appropriate type, expanding to T or NIL as appropriate.
91 (deftransform fold-type-predicate ((object) * * :node node :defun-only t)
92 (let ((ctype (gethash (leaf-name
95 (basic-combination-fun node))))
96 *backend-predicate-types*)))
98 (ir1-transform-type-predicate object ctype)))
100 ;;; If FIND-CLASS is called on a constant class, locate the CLASS-CELL
102 (deftransform find-class ((name) ((constant-argument symbol)) *
104 (let* ((name (continuation-value name))
105 (cell (find-class-cell name)))
106 `(or (class-cell-class ',cell)
107 (error "class not yet defined: ~S" name))))
109 ;;;; standard type predicates, i.e. those defined in package COMMON-LISP,
110 ;;;; plus at least one oddball (%INSTANCEP)
112 ;;;; Various other type predicates (e.g. low-level representation
113 ;;;; stuff like SIMPLE-ARRAY-SINGLE-FLOAT-P) are defined elsewhere.
115 ;;; FIXME: This function is only called once, at top level. Why not
116 ;;; just expand all its operations into toplevel code?
117 (defun !define-standard-type-predicates ()
118 (define-type-predicate arrayp array)
119 ; (The ATOM predicate is handled separately as (NOT CONS).)
120 (define-type-predicate bit-vector-p bit-vector)
121 (define-type-predicate characterp character)
122 (define-type-predicate compiled-function-p compiled-function)
123 (define-type-predicate complexp complex)
124 (define-type-predicate complex-rational-p (complex rational))
125 (define-type-predicate complex-float-p (complex float))
126 (define-type-predicate consp cons)
127 (define-type-predicate floatp float)
128 (define-type-predicate functionp function)
129 (define-type-predicate integerp integer)
130 (define-type-predicate keywordp keyword)
131 (define-type-predicate listp list)
132 (define-type-predicate null null)
133 (define-type-predicate numberp number)
134 (define-type-predicate rationalp rational)
135 (define-type-predicate realp real)
136 (define-type-predicate simple-bit-vector-p simple-bit-vector)
137 (define-type-predicate simple-string-p simple-string)
138 (define-type-predicate simple-vector-p simple-vector)
139 (define-type-predicate stringp string)
140 (define-type-predicate %instancep instance)
141 (define-type-predicate funcallable-instance-p funcallable-instance)
142 (define-type-predicate symbolp symbol)
143 (define-type-predicate vectorp vector))
144 (!define-standard-type-predicates)
146 ;;;; transforms for type predicates not implemented primitively
148 ;;;; See also VM dependent transforms.
150 (def-source-transform atom (x)
153 ;;;; TYPEP source transform
155 ;;; Return a form that tests the variable N-OBJECT for being in the
156 ;;; binds specified by TYPE. BASE is the name of the base type, for
157 ;;; declaration. We make SAFETY locally 0 to inhibit any checking of
159 #!-negative-zero-is-not-zero
160 (defun transform-numeric-bound-test (n-object type base)
161 (declare (type numeric-type type))
162 (let ((low (numeric-type-low type))
163 (high (numeric-type-high type)))
165 (declare (optimize (safety 0)))
168 `((> (the ,base ,n-object) ,(car low)))
169 `((>= (the ,base ,n-object) ,low))))
172 `((< (the ,base ,n-object) ,(car high)))
173 `((<= (the ,base ,n-object) ,high))))))))
175 #!+negative-zero-is-not-zero
176 (defun transform-numeric-bound-test (n-object type base)
177 (declare (type numeric-type type))
178 (let ((low (numeric-type-low type))
179 (high (numeric-type-high type))
180 (float-type-p (csubtypep type (specifier-type 'float)))
184 (declare (optimize (safety 0)))
187 `((let ((,x (the ,base ,n-object))
189 ,(if (not float-type-p)
191 `(if (and (zerop ,x) (zerop ,y))
192 (> (float-sign ,x) (float-sign ,y))
194 `((let ((,x (the ,base ,n-object))
196 ,(if (not float-type-p)
198 `(if (and (zerop ,x) (zerop ,y))
199 (>= (float-sign ,x) (float-sign ,y))
203 `((let ((,x (the ,base ,n-object))
205 ,(if (not float-type-p)
207 `(if (and (zerop ,x) (zerop ,y))
208 (< (float-sign ,x) (float-sign ,y))
210 `((let ((,x (the ,base ,n-object))
212 ,(if (not float-type-p)
214 `(if (and (zerop ,x) (zerop ,y))
215 (<= (float-sign ,x) (float-sign ,y))
218 ;;; Do source transformation of a test of a known numeric type. We can
219 ;;; assume that the type doesn't have a corresponding predicate, since
220 ;;; those types have already been picked off. In particular, CLASS
221 ;;; must be specified, since it is unspecified only in NUMBER and
222 ;;; COMPLEX. Similarly, we assume that COMPLEXP is always specified.
224 ;;; For non-complex types, we just test that the number belongs to the
225 ;;; base type, and then test that it is in bounds. When CLASS is
226 ;;; INTEGER, we check to see whether the range is no bigger than
227 ;;; FIXNUM. If so, we check for FIXNUM instead of INTEGER. This allows
228 ;;; us to use fixnum comparison to test the bounds.
230 ;;; For complex types, we must test for complex, then do the above on
231 ;;; both the real and imaginary parts. When CLASS is float, we need
232 ;;; only check the type of the realpart, since the format of the
233 ;;; realpart and the imagpart must be the same.
234 (defun source-transform-numeric-typep (object type)
235 (let* ((class (numeric-type-class type))
237 (integer (containing-integer-type type))
239 (float (or (numeric-type-format type) 'float))
241 (once-only ((n-object object))
242 (ecase (numeric-type-complexp type)
244 `(and (typep ,n-object ',base)
245 ,(transform-numeric-bound-test n-object type base)))
247 `(and (complexp ,n-object)
248 ,(once-only ((n-real `(realpart (the complex ,n-object)))
249 (n-imag `(imagpart (the complex ,n-object))))
252 (and (typep ,n-real ',base)
253 ,@(when (eq class 'integer)
254 `((typep ,n-imag ',base)))
255 ,(transform-numeric-bound-test n-real type base)
256 ,(transform-numeric-bound-test n-imag type
259 ;;; Do the source transformation for a test of a hairy type. AND,
260 ;;; SATISFIES and NOT are converted into the obvious code. We convert
261 ;;; unknown types to %TYPEP, emitting an efficiency note if
263 (defun source-transform-hairy-typep (object type)
264 (declare (type hairy-type type))
265 (let ((spec (hairy-type-specifier type)))
266 (cond ((unknown-type-p type)
267 (when (policy nil (> speed inhibit-warnings))
268 (compiler-note "can't open-code test of unknown type ~S"
269 (type-specifier type)))
270 `(%typep ,object ',spec))
273 (satisfies `(if (funcall #',(second spec) ,object) t nil))
275 (once-only ((n-obj object))
276 `(,(first spec) ,@(mapcar #'(lambda (x)
280 ;;; Do source transformation for TYPEP of a known union type. If a
281 ;;; union type contains LIST, then we pull that out and make it into a
282 ;;; single LISTP call. Note that if SYMBOL is in the union, then LIST
283 ;;; will be a subtype even without there being any (member NIL). We
284 ;;; just drop through to the general code in this case, rather than
285 ;;; trying to optimize it.
286 (defun source-transform-union-typep (object type)
287 (let* ((types (union-type-types type))
288 (ltype (specifier-type 'list))
289 (mtype (find-if #'member-type-p types)))
290 (if (and mtype (csubtypep ltype type))
291 (let ((members (member-type-members mtype)))
292 (once-only ((n-obj object))
295 '(or ,@(mapcar #'type-specifier
296 (remove (specifier-type 'cons)
297 (remove mtype types)))
298 (member ,@(remove nil members)))))))
299 (once-only ((n-obj object))
300 `(or ,@(mapcar (lambda (x)
301 `(typep ,n-obj ',(type-specifier x)))
304 ;;; Do source transformation for TYPEP of a known intersection type.
305 (defun source-transform-intersection-typep (object type)
306 (once-only ((n-obj object))
307 `(and ,@(mapcar (lambda (x)
308 `(typep ,n-obj ',(type-specifier x)))
309 (intersection-type-types type)))))
311 ;;; If necessary recurse to check the cons type.
312 (defun source-transform-cons-typep (object type)
313 (let* ((car-type (cons-type-car-type type))
314 (cdr-type (cons-type-cdr-type type)))
315 (let ((car-test-p (not (or (type= car-type *wild-type*)
316 (type= car-type (specifier-type t)))))
317 (cdr-test-p (not (or (type= cdr-type *wild-type*)
318 (type= cdr-type (specifier-type t))))))
319 (if (and (not car-test-p) (not cdr-test-p))
321 (once-only ((n-obj object))
324 `((typep (car ,n-obj)
325 ',(type-specifier car-type))))
327 `((typep (cdr ,n-obj)
328 ',(type-specifier cdr-type))))))))))
330 ;;; Return the predicate and type from the most specific entry in
331 ;;; *TYPE-PREDICATES* that is a supertype of TYPE.
332 (defun find-supertype-predicate (type)
333 (declare (type ctype type))
336 (dolist (x *backend-type-predicates*)
337 (let ((stype (car x)))
338 (when (and (csubtypep type stype)
340 (csubtypep stype res-type)))
341 (setq res-type stype)
342 (setq res (cdr x)))))
343 (values res res-type)))
345 ;;; Return forms to test that OBJ has the rank and dimensions
346 ;;; specified by TYPE, where STYPE is the type we have checked against
347 ;;; (which is the same but for dimensions.)
348 (defun test-array-dimensions (obj type stype)
349 (declare (type array-type type stype))
350 (let ((obj `(truly-the ,(type-specifier stype) ,obj))
351 (dims (array-type-dimensions type)))
354 (when (eq (array-type-dimensions stype) '*)
355 (res `(= (array-rank ,obj) ,(length dims))))
357 (dim dims (cdr dim)))
359 (let ((dim (car dim)))
361 (res `(= (array-dimension ,obj ,i) ,dim)))))
364 ;;; If we can find a type predicate that tests for the type w/o
365 ;;; dimensions, then use that predicate and test for dimensions.
366 ;;; Otherwise, just do %TYPEP.
367 (defun source-transform-array-typep (obj type)
368 (multiple-value-bind (pred stype) (find-supertype-predicate type)
369 (if (and (array-type-p stype)
370 ;; (If the element type hasn't been defined yet, it's
371 ;; not safe to assume here that it will eventually
372 ;; have (UPGRADED-ARRAY-ELEMENT-TYPE type)=T, so punt.)
373 (not (unknown-type-p (array-type-element-type type)))
374 (type= (array-type-specialized-element-type stype)
375 (array-type-specialized-element-type type))
376 (eq (array-type-complexp stype) (array-type-complexp type)))
377 (once-only ((n-obj obj))
379 ,@(test-array-dimensions n-obj type stype)))
380 `(%typep ,obj ',(type-specifier type)))))
382 ;;; Transform a type test against some instance type. The type test is
383 ;;; flushed if the result is known at compile time. If not properly
384 ;;; named, error. If sealed and has no subclasses, just test for
385 ;;; layout-EQ. If a structure then test for layout-EQ and then a
386 ;;; general test based on layout-inherits. If safety is important,
387 ;;; then we also check whether the layout for the object is invalid
388 ;;; and signal an error if so. Otherwise, look up the indirect
389 ;;; class-cell and call CLASS-CELL-TYPEP at runtime.
391 ;;; KLUDGE: The :WHEN :BOTH option here is probably a suboptimal
392 ;;; solution to the problem of %INSTANCE-TYPEP forms in byte compiled
393 ;;; code; it'd probably be better just to have %INSTANCE-TYPEP forms
394 ;;; never be generated in byte compiled code, or maybe to have a DEFUN
395 ;;; %INSTANCE-TYPEP somewhere to handle them if they are. But it's not
396 ;;; terribly important because mostly, %INSTANCE-TYPEP forms *aren't*
397 ;;; generated in byte compiled code. (As of sbcl-0.6.5, they could
398 ;;; sometimes be generated when byte compiling inline functions, but
399 ;;; it's quite uncommon.) -- WHN 20000523
400 (deftransform %instance-typep ((object spec) * * :when :both)
401 (aver (constant-continuation-p spec))
402 (let* ((spec (continuation-value spec))
403 (class (specifier-type spec))
404 (name (sb!xc:class-name class))
405 (otype (continuation-type object))
406 (layout (let ((res (info :type :compiler-layout name)))
407 (if (and res (not (layout-invalid res)))
411 ;; Flush tests whose result is known at compile time.
412 ((not (types-equal-or-intersect otype class))
414 ((csubtypep otype class)
416 ;; If not properly named, error.
417 ((not (and name (eq (sb!xc:find-class name) class)))
418 (compiler-error "can't compile TYPEP of anonymous or undefined ~
422 ;; Otherwise transform the type test.
423 (multiple-value-bind (pred get-layout)
425 ((csubtypep class (specifier-type 'funcallable-instance))
426 (values 'funcallable-instance-p '%funcallable-instance-layout))
427 ((csubtypep class (specifier-type 'instance))
428 (values '%instancep '%instance-layout))
430 (values '(lambda (x) (declare (ignore x)) t) 'layout-of)))
432 ((and (eq (class-state class) :sealed) layout
433 (not (class-subclasses class)))
434 ;; Sealed and has no subclasses.
435 (let ((n-layout (gensym)))
437 (let ((,n-layout (,get-layout object)))
438 ,@(when (policy nil (>= safety speed))
439 `((when (layout-invalid ,n-layout)
440 (%layout-invalid-error object ',layout))))
441 (eq ,n-layout ',layout)))))
442 ((and (typep class 'basic-structure-class) layout)
443 ;; structure type tests; hierarchical layout depths
444 (let ((depthoid (layout-depthoid layout))
447 (let ((,n-layout (,get-layout object)))
448 ,@(when (policy nil (>= safety speed))
449 `((when (layout-invalid ,n-layout)
450 (%layout-invalid-error object ',layout))))
451 (if (eq ,n-layout ',layout)
453 (and (> (layout-depthoid ,n-layout)
455 (locally (declare (optimize (safety 0)))
456 (eq (svref (layout-inherits ,n-layout)
460 (/noshow "default case -- ,PRED and CLASS-CELL-TYPEP")
462 (class-cell-typep (,get-layout object)
463 ',(find-class-cell name)
466 ;;; If the specifier argument is a quoted constant, then we consider
467 ;;; converting into a simple predicate or other stuff. If the type is
468 ;;; constant, but we can't transform the call, then we convert to
469 ;;; %TYPEP. We only pass when the type is non-constant. This allows us
470 ;;; to recognize between calls that might later be transformed
471 ;;; successfully when a constant type is discovered. We don't give an
472 ;;; efficiency note when we pass, since the IR1 transform will give
473 ;;; one if necessary and appropriate.
475 ;;; If the type is TYPE= to a type that has a predicate, then expand
476 ;;; to that predicate. Otherwise, we dispatch off of the type's type.
477 ;;; These transformations can increase space, but it is hard to tell
478 ;;; when, so we ignore policy and always do them. When byte-compiling,
479 ;;; we only do transforms that have potential for control
480 ;;; simplification. Instance type tests are converted to
481 ;;; %INSTANCE-TYPEP to allow type propagation.
482 (def-source-transform typep (object spec)
483 ;; KLUDGE: It looks bad to only do this on explicitly quoted forms,
484 ;; since that would overlook other kinds of constants. But it turns
485 ;; out that the DEFTRANSFORM for TYPEP detects any constant
486 ;; continuation, transforms it into a quoted form, and gives this
487 ;; source transform another chance, so it all works out OK, in a
488 ;; weird roundabout way. -- WHN 2001-03-18
489 (if (and (consp spec) (eq (car spec) 'quote))
490 (let ((type (specifier-type (cadr spec))))
491 (or (let ((pred (cdr (assoc type *backend-type-predicates*
493 (when pred `(,pred ,object)))
496 (source-transform-hairy-typep object type))
498 (source-transform-union-typep object type))
500 (source-transform-intersection-typep object type))
502 `(member ,object ',(member-type-members type)))
504 (compiler-warning "illegal type specifier for TYPEP: ~S"
506 `(%typep ,object ,spec))
508 (and (not (byte-compiling))
511 (source-transform-numeric-typep object type))
513 `(%instance-typep ,object ,spec))
515 (source-transform-array-typep object type))
517 (source-transform-cons-typep object type))
519 `(%typep ,object ,spec)))
524 ;;; old working version
525 (deftransform coerce ((x type) (* *) * :when :both)
526 (unless (constant-continuation-p type)
527 (give-up-ir1-transform))
528 (let ((tspec (specifier-type (continuation-value type))))
529 (if (csubtypep (continuation-type x) tspec)
531 `(the ,(continuation-value type)
532 ,(cond ((csubtypep tspec (specifier-type 'double-float))
534 ;; FIXME: If LONG-FLOAT is to be supported, we
535 ;; need to pick it off here before falling through
537 ((csubtypep tspec (specifier-type 'float))
540 (give-up-ir1-transform)))))))
542 ;;; KLUDGE: new broken version -- 20000504
543 ;;; FIXME: should be fixed or deleted
545 (deftransform coerce ((x type) (* *) * :when :both)
546 (unless (constant-continuation-p type)
547 (give-up-ir1-transform))
548 (let ((tspec (specifier-type (continuation-value type))))
549 (if (csubtypep (continuation-type x) tspec)
551 `(if #+nil (typep x type) #-nil nil
553 (the ,(continuation-value type)
554 ,(cond ((csubtypep tspec (specifier-type 'double-float))
556 ;; FIXME: If LONG-FLOAT is to be supported,
557 ;; we need to pick it off here before falling
558 ;; through to %SINGLE-FLOAT.
559 ((csubtypep tspec (specifier-type 'float))
562 ((csubtypep tspec (specifier-type 'list))
565 ((csubtypep tspec (specifier-type 'string))
566 '(coerce-to-simple-string x))
568 ((csubtypep tspec (specifier-type 'bit-vector))
569 '(coerce-to-bit-vector x))
571 ((csubtypep tspec (specifier-type 'vector))
572 '(coerce-to-vector x type))
574 (give-up-ir1-transform))))))))