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 ATOM) are best tested by letting the
28 ;;;; 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) * * :node node)
61 (unless (constant-lvar-p type)
62 (give-up-ir1-transform "can't open-code test of non-constant type"))
63 (multiple-value-bind (expansion fail-p)
64 (source-transform-typep 'object (lvar-value type))
69 ;;; If the lvar OBJECT definitely is or isn't of the specified
70 ;;; type, then return T or NIL as appropriate. Otherwise quietly
71 ;;; GIVE-UP-IR1-TRANSFORM.
72 (defun ir1-transform-type-predicate (object type)
73 (declare (type lvar object) (type ctype type))
74 (let ((otype (lvar-type object)))
75 (cond ((not (types-equal-or-intersect otype type))
77 ((csubtypep otype type)
79 ((eq type *empty-type*)
82 (give-up-ir1-transform)))))
84 ;;; Flush %TYPEP tests whose result is known at compile time.
85 (deftransform %typep ((object type))
86 (unless (constant-lvar-p type)
87 (give-up-ir1-transform))
88 (ir1-transform-type-predicate
90 (ir1-transform-specifier-type (lvar-value type))))
92 ;;; This is the IR1 transform for simple type predicates. It checks
93 ;;; whether the single argument is known to (not) be of the
94 ;;; appropriate type, expanding to T or NIL as appropriate.
95 (deftransform fold-type-predicate ((object) * * :node node :defun-only t)
96 (let ((ctype (gethash (leaf-source-name
99 (basic-combination-fun node))))
100 *backend-predicate-types*)))
102 (ir1-transform-type-predicate object ctype)))
104 ;;; If FIND-CLASSOID is called on a constant class, locate the
105 ;;; CLASSOID-CELL at load time.
106 (deftransform find-classoid ((name) ((constant-arg symbol)) *)
107 (let* ((name (lvar-value name))
108 (cell (find-classoid-cell name :create t)))
109 `(or (classoid-cell-classoid ',cell)
110 (error "class not yet defined: ~S" name))))
112 ;;;; standard type predicates, i.e. those defined in package COMMON-LISP,
113 ;;;; plus at least one oddball (%INSTANCEP)
115 ;;;; Various other type predicates (e.g. low-level representation
116 ;;;; stuff like SIMPLE-ARRAY-SINGLE-FLOAT-P) are defined elsewhere.
118 ;;; FIXME: This function is only called once, at top level. Why not
119 ;;; just expand all its operations into toplevel code?
120 (defun !define-standard-type-predicates ()
121 (define-type-predicate arrayp array)
122 ; (The ATOM predicate is handled separately as (NOT CONS).)
123 (define-type-predicate bit-vector-p bit-vector)
124 (define-type-predicate characterp character)
125 (define-type-predicate compiled-function-p compiled-function)
126 (define-type-predicate complexp complex)
127 (define-type-predicate complex-rational-p (complex rational))
128 (define-type-predicate complex-float-p (complex float))
129 (define-type-predicate consp cons)
130 (define-type-predicate floatp float)
131 (define-type-predicate functionp function)
132 (define-type-predicate integerp integer)
133 (define-type-predicate keywordp keyword)
134 (define-type-predicate listp list)
135 (define-type-predicate null null)
136 (define-type-predicate numberp number)
137 (define-type-predicate rationalp rational)
138 (define-type-predicate realp real)
139 (define-type-predicate sequencep sequence)
140 (define-type-predicate extended-sequence-p extended-sequence)
141 (define-type-predicate simple-bit-vector-p simple-bit-vector)
142 (define-type-predicate simple-string-p simple-string)
143 (define-type-predicate simple-vector-p simple-vector)
144 (define-type-predicate stringp string)
145 (define-type-predicate %instancep instance)
146 (define-type-predicate funcallable-instance-p funcallable-instance)
147 (define-type-predicate symbolp symbol)
148 (define-type-predicate vectorp vector))
149 (!define-standard-type-predicates)
151 ;;;; transforms for type predicates not implemented primitively
153 ;;;; See also VM dependent transforms.
155 (define-source-transform atom (x)
158 (define-source-transform base-char-p (x)
159 `(typep ,x 'base-char))
161 ;;;; TYPEP source transform
163 ;;; Return a form that tests the variable N-OBJECT for being in the
164 ;;; binds specified by TYPE. BASE is the name of the base type, for
165 ;;; declaration. We make SAFETY locally 0 to inhibit any checking of
167 (defun transform-numeric-bound-test (n-object type base)
168 (declare (type numeric-type type))
169 (let ((low (numeric-type-low type))
170 (high (numeric-type-high type)))
172 (declare (optimize (safety 0)))
175 `((> (truly-the ,base ,n-object) ,(car low)))
176 `((>= (truly-the ,base ,n-object) ,low))))
179 `((< (truly-the ,base ,n-object) ,(car high)))
180 `((<= (truly-the ,base ,n-object) ,high))))))))
182 ;;; Do source transformation of a test of a known numeric type. We can
183 ;;; assume that the type doesn't have a corresponding predicate, since
184 ;;; those types have already been picked off. In particular, CLASS
185 ;;; must be specified, since it is unspecified only in NUMBER and
186 ;;; COMPLEX. Similarly, we assume that COMPLEXP is always specified.
188 ;;; For non-complex types, we just test that the number belongs to the
189 ;;; base type, and then test that it is in bounds. When CLASS is
190 ;;; INTEGER, we check to see whether the range is no bigger than
191 ;;; FIXNUM. If so, we check for FIXNUM instead of INTEGER. This allows
192 ;;; us to use fixnum comparison to test the bounds.
194 ;;; For complex types, we must test for complex, then do the above on
195 ;;; both the real and imaginary parts. When CLASS is float, we need
196 ;;; only check the type of the realpart, since the format of the
197 ;;; realpart and the imagpart must be the same.
198 (defun source-transform-numeric-typep (object type)
199 (let* ((class (numeric-type-class type))
201 (integer (containing-integer-type
202 (if (numeric-type-complexp type)
203 (modified-numeric-type type
207 (float (or (numeric-type-format type) 'float))
209 (once-only ((n-object object))
210 (ecase (numeric-type-complexp type)
212 `(and (typep ,n-object ',base)
213 ,(transform-numeric-bound-test n-object type base)))
215 `(and (complexp ,n-object)
216 ,(once-only ((n-real `(realpart (truly-the complex ,n-object)))
217 (n-imag `(imagpart (truly-the complex ,n-object))))
220 (and (typep ,n-real ',base)
221 ,@(when (eq class 'integer)
222 `((typep ,n-imag ',base)))
223 ,(transform-numeric-bound-test n-real type base)
224 ,(transform-numeric-bound-test n-imag type
227 ;;; Do the source transformation for a test of a hairy type. AND,
228 ;;; SATISFIES and NOT are converted into the obvious code. We convert
229 ;;; unknown types to %TYPEP, emitting an efficiency note if
231 (defun source-transform-hairy-typep (object type)
232 (declare (type hairy-type type))
233 (let ((spec (hairy-type-specifier type)))
234 (cond ((unknown-type-p type)
235 (when (policy *lexenv* (> speed inhibit-warnings))
236 (compiler-notify "can't open-code test of unknown type ~S"
237 (type-specifier type)))
238 `(%typep ,object ',spec))
241 (satisfies `(if (funcall #',(second spec) ,object) t nil))
243 (once-only ((n-obj object))
244 `(,(first spec) ,@(mapcar (lambda (x)
248 (defun source-transform-negation-typep (object type)
249 (declare (type negation-type type))
250 (let ((spec (type-specifier (negation-type-type type))))
251 `(not (typep ,object ',spec))))
253 ;;; Do source transformation for TYPEP of a known union type. If a
254 ;;; union type contains LIST, then we pull that out and make it into a
255 ;;; single LISTP call. Note that if SYMBOL is in the union, then LIST
256 ;;; will be a subtype even without there being any (member NIL). We
257 ;;; currently just drop through to the general code in this case,
258 ;;; rather than trying to optimize it (but FIXME CSR 2004-04-05: it
259 ;;; wouldn't be hard to optimize it after all).
260 (defun source-transform-union-typep (object type)
261 (let* ((types (union-type-types type))
262 (type-cons (specifier-type 'cons))
263 (mtype (find-if #'member-type-p types))
264 (members (when mtype (member-type-members mtype))))
267 (memq type-cons types))
268 (once-only ((n-obj object))
271 '(or ,@(mapcar #'type-specifier
273 (remove mtype types)))
274 (member ,@(remove nil members))))))
275 (once-only ((n-obj object))
276 `(or ,@(mapcar (lambda (x)
277 `(typep ,n-obj ',(type-specifier x)))
280 ;;; Do source transformation for TYPEP of a known intersection type.
281 (defun source-transform-intersection-typep (object type)
282 (once-only ((n-obj object))
283 `(and ,@(mapcar (lambda (x)
284 `(typep ,n-obj ',(type-specifier x)))
285 (intersection-type-types type)))))
287 ;;; If necessary recurse to check the cons type.
288 (defun source-transform-cons-typep (object type)
289 (let* ((car-type (cons-type-car-type type))
290 (cdr-type (cons-type-cdr-type type)))
291 (let ((car-test-p (not (type= car-type *universal-type*)))
292 (cdr-test-p (not (type= cdr-type *universal-type*))))
293 (if (and (not car-test-p) (not cdr-test-p))
295 (once-only ((n-obj object))
298 `((typep (car ,n-obj)
299 ',(type-specifier car-type))))
301 `((typep (cdr ,n-obj)
302 ',(type-specifier cdr-type))))))))))
304 (defun source-transform-character-set-typep (object type)
305 (let ((pairs (character-set-type-pairs type)))
306 (if (and (= (length pairs) 1)
308 (= (cdar pairs) (1- sb!xc:char-code-limit)))
309 `(characterp ,object)
310 (once-only ((n-obj object))
311 (let ((n-code (gensym "CODE")))
312 `(and (characterp ,n-obj)
313 (let ((,n-code (sb!xc:char-code ,n-obj)))
315 ,@(loop for pair in pairs
317 `(<= ,(car pair) ,n-code ,(cdr pair)))))))))))
319 ;;; Return the predicate and type from the most specific entry in
320 ;;; *TYPE-PREDICATES* that is a supertype of TYPE.
321 (defun find-supertype-predicate (type)
322 (declare (type ctype type))
325 (dolist (x *backend-type-predicates*)
326 (let ((stype (car x)))
327 (when (and (csubtypep type stype)
329 (csubtypep stype res-type)))
330 (setq res-type stype)
331 (setq res (cdr x)))))
332 (values res res-type)))
334 ;;; Return forms to test that OBJ has the rank and dimensions
335 ;;; specified by TYPE, where STYPE is the type we have checked against
336 ;;; (which is the same but for dimensions and element type).
337 (defun test-array-dimensions (obj type stype)
338 (declare (type array-type type stype))
339 (let ((obj `(truly-the ,(type-specifier stype) ,obj))
340 (dims (array-type-dimensions type)))
341 (unless (or (eq dims '*)
342 (equal dims (array-type-dimensions stype)))
344 `((array-header-p ,obj)
345 ,@(when (eq (array-type-dimensions stype) '*)
346 `((= (%array-rank ,obj) ,(length dims))))
347 ,@(loop for d in dims
350 collect `(= (%array-dimension ,obj ,i) ,d))))
351 ((and dims (csubtypep stype (specifier-type 'simple-array)))
352 `((not (array-header-p ,obj))
353 ,@(unless (eq '* (car dims))
354 `((= (vector-length ,obj) ,@dims)))))
355 ((and dims (csubtypep stype (specifier-type '(and array (not simple-array)))))
356 `((array-header-p ,obj)
357 ,@(unless (eq '* (car dims))
358 `((= (%array-dimension ,obj 0) ,@dims)))))
360 (unless (eq '* (car dims))
361 `((if (array-header-p ,obj)
362 (= (%array-dimension ,obj 0) ,@dims)
363 (= (vector-length ,obj) ,@dims)))))))))
365 ;;; Return forms to test that OBJ has the element-type specified by
366 ;;; type specified by TYPE, where STYPE is the type we have checked
367 ;;; against (which is the same but for dimensions and element type).
368 (defun test-array-element-type (obj type stype)
369 (declare (type array-type type stype))
370 (let ((obj `(truly-the ,(type-specifier stype) ,obj))
371 (eltype (array-type-specialized-element-type type)))
372 (unless (type= eltype (array-type-specialized-element-type stype))
373 (with-unique-names (data)
374 `((do ((,data ,obj (%array-data-vector ,data)))
375 ((not (array-header-p ,data))
376 ;; KLUDGE: this isn't in fact maximally efficient,
377 ;; because though we know that DATA is a (SIMPLE-ARRAY *
378 ;; (*)), we will still check to see if the lowtag is
381 '(simple-array ,(type-specifier eltype) (*))))))))))
383 ;;; If we can find a type predicate that tests for the type without
384 ;;; dimensions, then use that predicate and test for dimensions.
385 ;;; Otherwise, just do %TYPEP.
386 (defun source-transform-array-typep (obj type)
387 (multiple-value-bind (pred stype) (find-supertype-predicate type)
388 (if (and (array-type-p stype)
389 ;; (If the element type hasn't been defined yet, it's
390 ;; not safe to assume here that it will eventually
391 ;; have (UPGRADED-ARRAY-ELEMENT-TYPE type)=T, so punt.)
392 (not (unknown-type-p (array-type-element-type type)))
393 (eq (array-type-complexp stype) (array-type-complexp type)))
394 (once-only ((n-obj obj))
396 ,@(test-array-dimensions n-obj type stype)
397 ,@(test-array-element-type n-obj type stype)))
398 `(%typep ,obj ',(type-specifier type)))))
400 ;;; Transform a type test against some instance type. The type test is
401 ;;; flushed if the result is known at compile time. If not properly
402 ;;; named, error. If sealed and has no subclasses, just test for
403 ;;; layout-EQ. If a structure then test for layout-EQ and then a
404 ;;; general test based on layout-inherits. If safety is important,
405 ;;; then we also check whether the layout for the object is invalid
406 ;;; and signal an error if so. Otherwise, look up the indirect
407 ;;; class-cell and call CLASS-CELL-TYPEP at runtime.
408 (deftransform %instance-typep ((object spec) (* *) * :node node)
409 (aver (constant-lvar-p spec))
410 (let* ((spec (lvar-value spec))
411 (class (specifier-type spec))
412 (name (classoid-name class))
413 (otype (lvar-type object))
414 (layout (let ((res (info :type :compiler-layout name)))
415 (if (and res (not (layout-invalid res)))
419 ;; Flush tests whose result is known at compile time.
420 ((not (types-equal-or-intersect otype class))
422 ((csubtypep otype class)
424 ;; If not properly named, error.
425 ((not (and name (eq (find-classoid name) class)))
426 (compiler-error "can't compile TYPEP of anonymous or undefined ~
430 ;; Delay the type transform to give type propagation a chance.
431 (delay-ir1-transform node :constraint)
433 ;; Otherwise transform the type test.
434 (multiple-value-bind (pred get-layout)
436 ((csubtypep class (specifier-type 'funcallable-instance))
437 (values 'funcallable-instance-p '%funcallable-instance-layout))
438 ((csubtypep class (specifier-type 'instance))
439 (values '%instancep '%instance-layout))
441 (values '(lambda (x) (declare (ignore x)) t) 'layout-of)))
443 ((and (eq (classoid-state class) :sealed) layout
444 (not (classoid-subclasses class)))
445 ;; Sealed and has no subclasses.
446 (let ((n-layout (gensym)))
448 (let ((,n-layout (,get-layout object)))
449 ,@(when (policy *lexenv* (>= safety speed))
450 `((when (layout-invalid ,n-layout)
451 (%layout-invalid-error object ',layout))))
452 (eq ,n-layout ',layout)))))
453 ((and (typep class 'structure-classoid) layout)
454 ;; structure type tests; hierarchical layout depths
455 (let ((depthoid (layout-depthoid layout))
458 (let ((,n-layout (,get-layout object)))
459 ;; we used to check for invalid layouts here,
460 ;; but in fact that's both unnecessary and
461 ;; wrong; it's unnecessary because structure
462 ;; classes can't be redefined, and it's wrong
463 ;; because it is quite legitimate to pass an
464 ;; object with an invalid layout to a structure
466 (if (eq ,n-layout ',layout)
468 (and (> (layout-depthoid ,n-layout)
470 (locally (declare (optimize (safety 0)))
471 ;; Use DATA-VECTOR-REF directly,
472 ;; since that's what SVREF in a
473 ;; SAFETY 0 lexenv will eventually be
474 ;; transformed to. This can give a
475 ;; large compilation speedup, since
476 ;; %INSTANCE-TYPEPs are frequently
477 ;; created during GENERATE-TYPE-CHECKS,
478 ;; and the normal aref transformation path
480 (eq (data-vector-ref (layout-inherits ,n-layout)
483 ((and layout (>= (layout-depthoid layout) 0))
484 ;; hierarchical layout depths for other things (e.g.
485 ;; CONDITION, STREAM)
486 (let ((depthoid (layout-depthoid layout))
488 (n-inherits (gensym)))
490 (let ((,n-layout (,get-layout object)))
491 (when (layout-invalid ,n-layout)
492 (setq ,n-layout (update-object-layout-or-invalid
494 (if (eq ,n-layout ',layout)
496 (let ((,n-inherits (layout-inherits ,n-layout)))
497 (declare (optimize (safety 0)))
498 (and (> (length ,n-inherits) ,depthoid)
500 (eq (data-vector-ref ,n-inherits ,depthoid)
503 (/noshow "default case -- ,PRED and CLASS-CELL-TYPEP")
505 (classoid-cell-typep (,get-layout object)
506 ',(find-classoid-cell name :create t)
509 ;;; If the specifier argument is a quoted constant, then we consider
510 ;;; converting into a simple predicate or other stuff. If the type is
511 ;;; constant, but we can't transform the call, then we convert to
512 ;;; %TYPEP. We only pass when the type is non-constant. This allows us
513 ;;; to recognize between calls that might later be transformed
514 ;;; successfully when a constant type is discovered. We don't give an
515 ;;; efficiency note when we pass, since the IR1 transform will give
516 ;;; one if necessary and appropriate.
518 ;;; If the type is TYPE= to a type that has a predicate, then expand
519 ;;; to that predicate. Otherwise, we dispatch off of the type's type.
520 ;;; These transformations can increase space, but it is hard to tell
521 ;;; when, so we ignore policy and always do them.
522 (defun source-transform-typep (object type)
523 (let ((ctype (careful-specifier-type type)))
524 (or (when (not ctype)
525 (compiler-warn "illegal type specifier for TYPEP: ~S" type)
526 (return-from source-transform-typep (values nil t)))
527 (let ((pred (cdr (assoc ctype *backend-type-predicates*
529 (when pred `(,pred ,object)))
532 (source-transform-hairy-typep object ctype))
534 (source-transform-negation-typep object ctype))
536 (source-transform-union-typep object ctype))
538 (source-transform-intersection-typep object ctype))
540 `(if (member ,object ',(member-type-members ctype)) t))
542 (compiler-warn "illegal type specifier for TYPEP: ~S" type)
543 (return-from source-transform-typep (values nil t)))
547 (source-transform-numeric-typep object ctype))
549 `(%instance-typep ,object ',type))
551 (source-transform-array-typep object ctype))
553 (source-transform-cons-typep object ctype))
555 (source-transform-character-set-typep object ctype))
557 `(%typep ,object ',type))))
559 (define-source-transform typep (object spec)
560 ;; KLUDGE: It looks bad to only do this on explicitly quoted forms,
561 ;; since that would overlook other kinds of constants. But it turns
562 ;; out that the DEFTRANSFORM for TYPEP detects any constant
563 ;; lvar, transforms it into a quoted form, and gives this
564 ;; source transform another chance, so it all works out OK, in a
565 ;; weird roundabout way. -- WHN 2001-03-18
566 (if (and (consp spec)
567 (eq (car spec) 'quote)
568 (or (not *allow-instrumenting*)
569 (policy *lexenv* (= store-coverage-data 0))))
570 (source-transform-typep object (cadr spec))
575 ;;; Constant-folding.
578 (defoptimizer (coerce optimizer) ((x type) node)
579 (when (and (constant-lvar-p x) (constant-lvar-p type))
580 (let ((value (lvar-value x)))
581 (when (or (numberp value) (characterp value))
582 (constant-fold-call node)
585 (deftransform coerce ((x type) (* *) * :node node)
586 (unless (constant-lvar-p type)
587 (give-up-ir1-transform))
588 (let ((tspec (ir1-transform-specifier-type (lvar-value type))))
589 (if (csubtypep (lvar-type x) tspec)
591 ;; Note: The THE here makes sure that specifiers like
592 ;; (SINGLE-FLOAT 0.0 1.0) can raise a TYPE-ERROR.
593 `(the ,(lvar-value type)
595 ((csubtypep tspec (specifier-type 'double-float))
597 ;; FIXME: #!+long-float (t ,(error "LONG-FLOAT case needed"))
598 ((csubtypep tspec (specifier-type 'float))
600 ((and (csubtypep tspec (specifier-type 'simple-vector))
601 ;; Can we avoid checking for dimension issues like
602 ;; (COERCE FOO '(SIMPLE-VECTOR 5)) returning a
603 ;; vector of length 6?
604 (or (policy node (< safety 3)) ; no need in unsafe code
605 (and (array-type-p tspec) ; no need when no dimensions
606 (equal (array-type-dimensions tspec) '(*)))))
607 `(if (simple-vector-p x)
609 (replace (make-array (length x)) x)))
610 ;; FIXME: other VECTOR types?
612 (give-up-ir1-transform)))))))