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)
75 ((eq type *empty-type*)
78 (give-up-ir1-transform)))))
80 ;;; Flush %TYPEP tests whose result is known at compile time.
81 (deftransform %typep ((object type))
82 (unless (constant-continuation-p type)
83 (give-up-ir1-transform))
84 (ir1-transform-type-predicate
86 (ir1-transform-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-source-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-classoid ((name) ((constant-arg symbol)) *)
103 (let* ((name (continuation-value name))
104 (cell (find-classoid-cell name)))
105 `(or (classoid-cell-classoid ',cell)
106 (error "class not yet defined: ~S" name))))
108 ;;;; standard type predicates, i.e. those defined in package COMMON-LISP,
109 ;;;; plus at least one oddball (%INSTANCEP)
111 ;;;; Various other type predicates (e.g. low-level representation
112 ;;;; stuff like SIMPLE-ARRAY-SINGLE-FLOAT-P) are defined elsewhere.
114 ;;; FIXME: This function is only called once, at top level. Why not
115 ;;; just expand all its operations into toplevel code?
116 (defun !define-standard-type-predicates ()
117 (define-type-predicate arrayp array)
118 ; (The ATOM predicate is handled separately as (NOT CONS).)
119 (define-type-predicate bit-vector-p bit-vector)
120 (define-type-predicate characterp character)
121 (define-type-predicate compiled-function-p compiled-function)
122 (define-type-predicate complexp complex)
123 (define-type-predicate complex-rational-p (complex rational))
124 (define-type-predicate complex-float-p (complex float))
125 (define-type-predicate consp cons)
126 (define-type-predicate floatp float)
127 (define-type-predicate functionp function)
128 (define-type-predicate integerp integer)
129 (define-type-predicate keywordp keyword)
130 (define-type-predicate listp list)
131 (define-type-predicate null null)
132 (define-type-predicate numberp number)
133 (define-type-predicate rationalp rational)
134 (define-type-predicate realp real)
135 (define-type-predicate simple-bit-vector-p simple-bit-vector)
136 (define-type-predicate simple-string-p simple-string)
137 (define-type-predicate simple-vector-p simple-vector)
138 (define-type-predicate stringp string)
139 (define-type-predicate %instancep instance)
140 (define-type-predicate funcallable-instance-p funcallable-instance)
141 (define-type-predicate symbolp symbol)
142 (define-type-predicate vectorp vector))
143 (!define-standard-type-predicates)
145 ;;;; transforms for type predicates not implemented primitively
147 ;;;; See also VM dependent transforms.
149 (define-source-transform atom (x)
152 ;;;; TYPEP source transform
154 ;;; Return a form that tests the variable N-OBJECT for being in the
155 ;;; binds specified by TYPE. BASE is the name of the base type, for
156 ;;; declaration. We make SAFETY locally 0 to inhibit any checking of
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 `((> (truly-the ,base ,n-object) ,(car low)))
167 `((>= (truly-the ,base ,n-object) ,low))))
170 `((< (truly-the ,base ,n-object) ,(car high)))
171 `((<= (truly-the ,base ,n-object) ,high))))))))
173 ;;; Do source transformation of a test of a known numeric type. We can
174 ;;; assume that the type doesn't have a corresponding predicate, since
175 ;;; those types have already been picked off. In particular, CLASS
176 ;;; must be specified, since it is unspecified only in NUMBER and
177 ;;; COMPLEX. Similarly, we assume that COMPLEXP is always specified.
179 ;;; For non-complex types, we just test that the number belongs to the
180 ;;; base type, and then test that it is in bounds. When CLASS is
181 ;;; INTEGER, we check to see whether the range is no bigger than
182 ;;; FIXNUM. If so, we check for FIXNUM instead of INTEGER. This allows
183 ;;; us to use fixnum comparison to test the bounds.
185 ;;; For complex types, we must test for complex, then do the above on
186 ;;; both the real and imaginary parts. When CLASS is float, we need
187 ;;; only check the type of the realpart, since the format of the
188 ;;; realpart and the imagpart must be the same.
189 (defun source-transform-numeric-typep (object type)
190 (let* ((class (numeric-type-class type))
192 (integer (containing-integer-type type))
194 (float (or (numeric-type-format type) 'float))
196 (once-only ((n-object object))
197 (ecase (numeric-type-complexp type)
199 `(and (typep ,n-object ',base)
200 ,(transform-numeric-bound-test n-object type base)))
202 `(and (complexp ,n-object)
203 ,(once-only ((n-real `(realpart (truly-the complex ,n-object)))
204 (n-imag `(imagpart (truly-the complex ,n-object))))
207 (and (typep ,n-real ',base)
208 ,@(when (eq class 'integer)
209 `((typep ,n-imag ',base)))
210 ,(transform-numeric-bound-test n-real type base)
211 ,(transform-numeric-bound-test n-imag type
214 ;;; Do the source transformation for a test of a hairy type. AND,
215 ;;; SATISFIES and NOT are converted into the obvious code. We convert
216 ;;; unknown types to %TYPEP, emitting an efficiency note if
218 (defun source-transform-hairy-typep (object type)
219 (declare (type hairy-type type))
220 (let ((spec (hairy-type-specifier type)))
221 (cond ((unknown-type-p type)
222 (when (policy *lexenv* (> speed inhibit-warnings))
223 (compiler-note "can't open-code test of unknown type ~S"
224 (type-specifier type)))
225 `(%typep ,object ',spec))
228 (satisfies `(if (funcall #',(second spec) ,object) t nil))
230 (once-only ((n-obj object))
231 `(,(first spec) ,@(mapcar (lambda (x)
235 (defun source-transform-negation-typep (object type)
236 (declare (type negation-type type))
237 (let ((spec (type-specifier (negation-type-type type))))
238 `(not (typep ,object ',spec))))
240 ;;; Do source transformation for TYPEP of a known union type. If a
241 ;;; union type contains LIST, then we pull that out and make it into a
242 ;;; single LISTP call. Note that if SYMBOL is in the union, then LIST
243 ;;; will be a subtype even without there being any (member NIL). We
244 ;;; just drop through to the general code in this case, rather than
245 ;;; trying to optimize it.
246 (defun source-transform-union-typep (object type)
247 (let* ((types (union-type-types type))
248 (ltype (specifier-type 'list))
249 (mtype (find-if #'member-type-p types)))
250 (if (and mtype (csubtypep ltype type))
251 (let ((members (member-type-members mtype)))
252 (once-only ((n-obj object))
255 '(or ,@(mapcar #'type-specifier
256 (remove (specifier-type 'cons)
257 (remove mtype types)))
258 (member ,@(remove nil members)))))))
259 (once-only ((n-obj object))
260 `(or ,@(mapcar (lambda (x)
261 `(typep ,n-obj ',(type-specifier x)))
264 ;;; Do source transformation for TYPEP of a known intersection type.
265 (defun source-transform-intersection-typep (object type)
266 (once-only ((n-obj object))
267 `(and ,@(mapcar (lambda (x)
268 `(typep ,n-obj ',(type-specifier x)))
269 (intersection-type-types type)))))
271 ;;; If necessary recurse to check the cons type.
272 (defun source-transform-cons-typep (object type)
273 (let* ((car-type (cons-type-car-type type))
274 (cdr-type (cons-type-cdr-type type)))
275 (let ((car-test-p (not (type= car-type *universal-type*)))
276 (cdr-test-p (not (type= cdr-type *universal-type*))))
277 (if (and (not car-test-p) (not cdr-test-p))
279 (once-only ((n-obj object))
282 `((typep (car ,n-obj)
283 ',(type-specifier car-type))))
285 `((typep (cdr ,n-obj)
286 ',(type-specifier cdr-type))))))))))
288 ;;; Return the predicate and type from the most specific entry in
289 ;;; *TYPE-PREDICATES* that is a supertype of TYPE.
290 (defun find-supertype-predicate (type)
291 (declare (type ctype type))
294 (dolist (x *backend-type-predicates*)
295 (let ((stype (car x)))
296 (when (and (csubtypep type stype)
298 (csubtypep stype res-type)))
299 (setq res-type stype)
300 (setq res (cdr x)))))
301 (values res res-type)))
303 ;;; Return forms to test that OBJ has the rank and dimensions
304 ;;; specified by TYPE, where STYPE is the type we have checked against
305 ;;; (which is the same but for dimensions.)
306 (defun test-array-dimensions (obj type stype)
307 (declare (type array-type type stype))
308 (let ((obj `(truly-the ,(type-specifier stype) ,obj))
309 (dims (array-type-dimensions type)))
312 (when (eq (array-type-dimensions stype) '*)
313 (res `(= (array-rank ,obj) ,(length dims))))
315 (dim dims (cdr dim)))
317 (let ((dim (car dim)))
319 (res `(= (array-dimension ,obj ,i) ,dim)))))
322 ;;; If we can find a type predicate that tests for the type without
323 ;;; dimensions, then use that predicate and test for dimensions.
324 ;;; Otherwise, just do %TYPEP.
325 (defun source-transform-array-typep (obj type)
326 (multiple-value-bind (pred stype) (find-supertype-predicate type)
327 (if (and (array-type-p stype)
328 ;; (If the element type hasn't been defined yet, it's
329 ;; not safe to assume here that it will eventually
330 ;; have (UPGRADED-ARRAY-ELEMENT-TYPE type)=T, so punt.)
331 (not (unknown-type-p (array-type-element-type type)))
332 (type= (array-type-specialized-element-type stype)
333 (array-type-specialized-element-type type))
334 (eq (array-type-complexp stype) (array-type-complexp type)))
335 (once-only ((n-obj obj))
337 ,@(test-array-dimensions n-obj type stype)))
338 `(%typep ,obj ',(type-specifier type)))))
340 ;;; Transform a type test against some instance type. The type test is
341 ;;; flushed if the result is known at compile time. If not properly
342 ;;; named, error. If sealed and has no subclasses, just test for
343 ;;; layout-EQ. If a structure then test for layout-EQ and then a
344 ;;; general test based on layout-inherits. If safety is important,
345 ;;; then we also check whether the layout for the object is invalid
346 ;;; and signal an error if so. Otherwise, look up the indirect
347 ;;; class-cell and call CLASS-CELL-TYPEP at runtime.
348 (deftransform %instance-typep ((object spec) (* *) * :node node)
349 (aver (constant-continuation-p spec))
350 (let* ((spec (continuation-value spec))
351 (class (specifier-type spec))
352 (name (classoid-name class))
353 (otype (continuation-type object))
354 (layout (let ((res (info :type :compiler-layout name)))
355 (if (and res (not (layout-invalid res)))
359 ;; Flush tests whose result is known at compile time.
360 ((not (types-equal-or-intersect otype class))
362 ((csubtypep otype class)
364 ;; If not properly named, error.
365 ((not (and name (eq (find-classoid name) class)))
366 (compiler-error "can't compile TYPEP of anonymous or undefined ~
370 ;; Delay the type transform to give type propagation a chance.
371 (delay-ir1-transform node :constraint)
373 ;; Otherwise transform the type test.
374 (multiple-value-bind (pred get-layout)
376 ((csubtypep class (specifier-type 'funcallable-instance))
377 (values 'funcallable-instance-p '%funcallable-instance-layout))
378 ((csubtypep class (specifier-type 'instance))
379 (values '%instancep '%instance-layout))
381 (values '(lambda (x) (declare (ignore x)) t) 'layout-of)))
383 ((and (eq (classoid-state class) :sealed) layout
384 (not (classoid-subclasses class)))
385 ;; Sealed and has no subclasses.
386 (let ((n-layout (gensym)))
388 (let ((,n-layout (,get-layout object)))
389 ,@(when (policy *lexenv* (>= safety speed))
390 `((when (layout-invalid ,n-layout)
391 (%layout-invalid-error object ',layout))))
392 (eq ,n-layout ',layout)))))
393 ((and (typep class 'basic-structure-classoid) layout)
394 ;; structure type tests; hierarchical layout depths
395 (let ((depthoid (layout-depthoid layout))
398 (let ((,n-layout (,get-layout object)))
399 ,@(when (policy *lexenv* (>= safety speed))
400 `((when (layout-invalid ,n-layout)
401 (%layout-invalid-error object ',layout))))
402 (if (eq ,n-layout ',layout)
404 (and (> (layout-depthoid ,n-layout)
406 (locally (declare (optimize (safety 0)))
407 (eq (svref (layout-inherits ,n-layout)
410 ((and layout (>= (layout-depthoid layout) 0))
411 ;; hierarchical layout depths for other things (e.g.
413 (let ((depthoid (layout-depthoid layout))
415 (n-inherits (gensym)))
417 (let ((,n-layout (,get-layout object)))
418 ,@(when (policy *lexenv* (>= safety speed))
419 `((when (layout-invalid ,n-layout)
420 (%layout-invalid-error object ',layout))))
421 (if (eq ,n-layout ',layout)
423 (let ((,n-inherits (layout-inherits ,n-layout)))
424 (declare (optimize (safety 0)))
425 (and (> (length ,n-inherits) ,depthoid)
426 (eq (svref ,n-inherits ,depthoid)
429 (/noshow "default case -- ,PRED and CLASS-CELL-TYPEP")
431 (classoid-cell-typep (,get-layout object)
432 ',(find-classoid-cell name)
435 ;;; If the specifier argument is a quoted constant, then we consider
436 ;;; converting into a simple predicate or other stuff. If the type is
437 ;;; constant, but we can't transform the call, then we convert to
438 ;;; %TYPEP. We only pass when the type is non-constant. This allows us
439 ;;; to recognize between calls that might later be transformed
440 ;;; successfully when a constant type is discovered. We don't give an
441 ;;; efficiency note when we pass, since the IR1 transform will give
442 ;;; one if necessary and appropriate.
444 ;;; If the type is TYPE= to a type that has a predicate, then expand
445 ;;; to that predicate. Otherwise, we dispatch off of the type's type.
446 ;;; These transformations can increase space, but it is hard to tell
447 ;;; when, so we ignore policy and always do them.
448 (define-source-transform typep (object spec)
449 ;; KLUDGE: It looks bad to only do this on explicitly quoted forms,
450 ;; since that would overlook other kinds of constants. But it turns
451 ;; out that the DEFTRANSFORM for TYPEP detects any constant
452 ;; continuation, transforms it into a quoted form, and gives this
453 ;; source transform another chance, so it all works out OK, in a
454 ;; weird roundabout way. -- WHN 2001-03-18
455 (if (and (consp spec) (eq (car spec) 'quote))
456 (let ((type (careful-specifier-type (cadr spec))))
458 (compiler-warn "illegal type specifier for TYPEP: ~S"
460 `(%typep ,object ,spec))
461 (let ((pred (cdr (assoc type *backend-type-predicates*
463 (when pred `(,pred ,object)))
466 (source-transform-hairy-typep object type))
468 (source-transform-negation-typep object type))
470 (source-transform-union-typep object type))
472 (source-transform-intersection-typep object type))
474 `(member ,object ',(member-type-members type)))
476 (compiler-warn "illegal type specifier for TYPEP: ~S"
478 `(%typep ,object ,spec))
482 (source-transform-numeric-typep object type))
484 `(%instance-typep ,object ,spec))
486 (source-transform-array-typep object type))
488 (source-transform-cons-typep object type))
490 `(%typep ,object ,spec)))
495 (deftransform coerce ((x type) (* *) * :node node)
496 (unless (constant-continuation-p type)
497 (give-up-ir1-transform))
498 (let ((tspec (ir1-transform-specifier-type (continuation-value type))))
499 (if (csubtypep (continuation-type x) tspec)
501 ;; Note: The THE here makes sure that specifiers like
502 ;; (SINGLE-FLOAT 0.0 1.0) can raise a TYPE-ERROR.
503 `(the ,(continuation-value type)
505 ((csubtypep tspec (specifier-type 'double-float))
507 ;; FIXME: #!+long-float (t ,(error "LONG-FLOAT case needed"))
508 ((csubtypep tspec (specifier-type 'float))
510 ((and (csubtypep tspec (specifier-type 'simple-vector))
511 ;; Can we avoid checking for dimension issues like
512 ;; (COERCE FOO '(SIMPLE-VECTOR 5)) returning a
513 ;; vector of length 6?
514 (or (policy node (< safety 3)) ; no need in unsafe code
515 (and (array-type-p tspec) ; no need when no dimensions
516 (equal (array-type-dimensions tspec) '(*)))))
517 `(if (simple-vector-p x)
519 (replace (make-array (length x)) x)))
520 ;; FIXME: other VECTOR types?
522 (give-up-ir1-transform)))))))