/[cmucl]/src/compiler/checkgen.lisp
ViewVC logotype

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.33 - (hide annotations)
Thu May 8 14:52:04 2003 UTC (10 years, 11 months ago) by gerd
Branch: MAIN
CVS Tags: release-19b-pre1, release-19b-pre2, snapshot-2003-10, snapshot-2004-10, snapshot-2004-08, snapshot-2004-09, snapshot-2004-05, snapshot-2004-06, snapshot-2004-07, release-19c, dynamic-extent-base, release-19c-base, mod-arith-base, sparc_gencgc_merge, snapshot-2004-12, snapshot-2004-11, amd64-merge-start, prm-before-macosx-merge-tag, snapshot-2003-11, snapshot-2005-07, release-19a-base, sparc_gencgc, snapshot-2003-12, release-19a-pre1, release-19a-pre3, release-19a-pre2, release-19a, snapshot-2005-03, release-19b-base, snapshot-2005-11, snapshot-2004-04, snapshot-2005-10, snapshot-2005-12, snapshot-2005-01, release-19c-pre1, snapshot-2005-06, snapshot-2005-05, snapshot-2005-04, ppc_gencgc_snap_2005-05-14, snapshot-2005-02, snapshot-2005-09, snapshot-2005-08, lisp-executable-base
Branch point for: release-19b-branch, mod-arith-branch, sparc_gencgc_branch, dynamic-extent, ppc_gencgc_branch, lisp-executable, release-19a-branch, release-19c-branch
Changes since 1.32: +59 -41 lines
	Fix problems with THE detected by Paul Dietz' tests.  This also
	fixes the problem that CMUCL generated RETURN-MULTIPLE without a
	good reason.

	* src/compiler/checkgen.lisp (values-types-asserted): Use
	coerce-to-values again.  Add fixme comments and #+nil code for
	known problems; these aren't new problem, though.

	* src/code/type.lisp (coerce-to-values): Don't add &rest t,
	which is the business of THE.
	(values) <type translator>: Disallow &key and &allow-other-keys.

	* src/compiler/ir1tran.lisp (the) <IR1 translator>: Add &rest t
	where appropriate, or-in null type into required types, for
	missing values.
1 wlott 1.1 ;;; -*- Package: C; Log: C.Log -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.17 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 gerd 1.33 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/checkgen.lisp,v 1.33 2003/05/08 14:52:04 gerd Exp $")
9 ram 1.17 ;;;
10 wlott 1.1 ;;; **********************************************************************
11     ;;;
12     ;;; This file implements type check generation. This is a phase that runs
13     ;;; at the very end of IR1. If a type check is too complex for the back end to
14     ;;; directly emit in-line, then we transform the check into an explicit
15     ;;; conditional using TYPEP.
16     ;;;
17     ;;; Written by Rob MacLachlan
18     ;;;
19 ram 1.22 (in-package "C")
20 wlott 1.1
21    
22     ;;;; Cost estimation:
23    
24    
25     ;;; Function-Cost -- Internal
26     ;;;
27     ;;; Return some sort of guess about the cost of a call to a function. If
28     ;;; the function has some templates, we return the cost of the cheapest one,
29     ;;; otherwise we return the cost of CALL-NAMED. Calling this with functions
30     ;;; that have transforms can result in relatively meaningless results
31     ;;; (exaggerated costs.)
32     ;;;
33     ;;; We randomly special-case NULL, since it does have a source tranform and is
34     ;;; interesting to us.
35     ;;;
36     (defun function-cost (name)
37     (declare (symbol name))
38     (let ((info (info function info name))
39 wlott 1.15 (call-cost (template-cost (template-or-lose 'call-named *backend*))))
40 wlott 1.1 (if info
41     (let ((templates (function-info-templates info)))
42     (if templates
43     (template-cost (first templates))
44     (case name
45 wlott 1.15 (null (template-cost (template-or-lose 'if-eq *backend*)))
46 wlott 1.1 (t call-cost))))
47     call-cost)))
48    
49    
50     ;;; Type-Test-Cost -- Internal
51     ;;;
52     ;;; Return some sort of guess for the cost of doing a test against TYPE.
53     ;;; The result need not be precise as long as it isn't way out in space. The
54     ;;; units are based on the costs specified for various templates in the VM
55     ;;; definition.
56     ;;;
57     (defun type-test-cost (type)
58     (declare (type ctype type))
59     (or (let ((check (type-check-template type)))
60     (if check
61     (template-cost check)
62 wlott 1.19 (let ((found (cdr (assoc type (backend-type-predicates *backend*)
63     :test #'type=))))
64 wlott 1.1 (if found
65 ram 1.16 (+ (function-cost found) (function-cost 'eq))
66 wlott 1.1 nil))))
67     (typecase type
68     (union-type
69     (collect ((res 0 +))
70     (dolist (mem (union-type-types type))
71     (res (type-test-cost mem)))
72     (res)))
73 gerd 1.31 (intersection-type
74     (collect ((res 0 +))
75     (dolist (mem (intersection-type-types type))
76     (res (type-test-cost mem)))
77     (res)))
78 wlott 1.1 (member-type
79     (* (length (member-type-members type))
80     (function-cost 'eq)))
81     (numeric-type
82     (* (if (numeric-type-complexp type) 2 1)
83     (function-cost
84     (if (csubtypep type (specifier-type 'fixnum)) 'fixnump 'numberp))
85     (+ 1
86     (if (numeric-type-low type) 1 0)
87     (if (numeric-type-high type) 1 0))))
88 dtc 1.27 (cons-type
89     (+ (type-test-cost (specifier-type 'cons))
90     (function-cost 'car)
91     (type-test-cost (cons-type-car-type type))
92     (function-cost 'cdr)
93     (type-test-cost (cons-type-cdr-type type))))
94 wlott 1.1 (t
95     (function-cost 'typep)))))
96    
97    
98     ;;;; Checking strategy determination:
99    
100    
101 ram 1.3 ;;; MAYBE-WEAKEN-CHECK -- Internal
102     ;;;
103     ;;; Return the type we should test for when we really want to check for
104     ;;; Type. If speed, space or compilation speed is more important than safety,
105     ;;; then we return a weaker type if it is easier to check. First we try the
106     ;;; defined type weakenings, then look for any predicate that is cheaper.
107     ;;;
108     ;;; If the supertype is equal in cost to the type, we prefer the supertype.
109     ;;; This produces a closer approximation of the right thing in the presence of
110     ;;; poor cost info.
111     ;;;
112     (defun maybe-weaken-check (type cont)
113     (declare (type ctype type) (type continuation cont))
114     (cond ((policy (continuation-dest cont)
115     (<= speed safety) (<= space safety) (<= cspeed safety))
116     type)
117     (t
118     (let ((min-cost (type-test-cost type))
119     (min-type type)
120     (found-super nil))
121 wlott 1.19 (dolist (x (backend-type-predicates *backend*))
122 ram 1.3 (let ((stype (car x)))
123 ram 1.5 (when (and (csubtypep type stype)
124     (not (union-type-p stype))) ;Not #!% COMMON type.
125 ram 1.3 (let ((stype-cost (type-test-cost stype)))
126 ram 1.8 (when (or (< stype-cost min-cost)
127     (type= stype type))
128 ram 1.5 (setq found-super t)
129 ram 1.3 (setq min-type stype min-cost stype-cost))))))
130     (if found-super
131     min-type
132     *universal-type*)))))
133    
134    
135 dtc 1.28 ;;; NO-FUNCTION-TYPES -- Internal
136 ram 1.9 ;;;
137 dtc 1.28 ;;; Mash any complex function types to FUNCTION.
138 ram 1.9 ;;;
139 dtc 1.28 (defun no-function-types (types)
140     (declare (type list types))
141     (mapcar #'(lambda (type)
142     (if (function-type-p type)
143     (specifier-type 'function)
144     type))
145     types))
146 ram 1.9
147    
148 dtc 1.29 ;;; Values-types-asserted -- Internal
149     ;;;
150     ;;; Like values-types, but when an argument is proven to be delivered,
151     ;;; convert asserted optional and rest arguments to required arguments. This
152     ;;; makes it clear that these required arguments may all be type checked.
153     ;;;
154     (defun values-types-asserted (atype ptype)
155     (declare (type ctype atype ptype))
156 gerd 1.33 (flet ((give-up ()
157     (return-from values-types-asserted (values nil :unknown))))
158     (cond ((eq atype *wild-type*)
159     (give-up))
160     ((not (values-type-p atype))
161     (values (list atype) 1))
162     ((or (values-type-keyp atype)
163     (values-type-allowp atype))
164     (give-up))
165     ;;
166     ;; FIXME: Values type checking is done with a form like
167     ;;
168     ;; (multiple-value-bind (x y z) <form>
169     ;; <type checks for x y z>
170     ;; (values x y z))
171     ;;
172     ;; see Make-Type-Check-Form. This has the unfortunate
173     ;; effect of chopping values when <form> actually returns
174     ;; more values than are being checked. The downside of
175     ;; including this is that it produces a lot of notes.
176     #+nil
177     ((or (eq *wild-type* ptype)
178     (and (values-type-p ptype)
179     (or (values-type-optional ptype)
180     (values-type-rest ptype))))
181     (give-up))
182     (t
183     (let* ((ptype (kernel::coerce-to-values ptype))
184     (preq (values-type-required ptype))
185     (popt (values-type-optional ptype))
186     (prest (values-type-rest ptype)))
187     ;;
188     ;; FIXME: ptype = * is not handled right, I think
189     ;; because * = (VALUES &REST T). It never was
190     ;; handled right. Gerd 2003-05-08.
191     (collect ((types))
192     (dolist (type (values-type-required atype))
193     (if (or (pop preq) (pop popt) prest)
194     (types (single-value-type type))
195     (give-up)))
196     (dolist (type (values-type-optional atype))
197     (if (pop preq)
198     (types (single-value-type type))
199     (give-up)))
200     (let ((arest (values-type-rest atype)))
201     (when arest
202     (loop with rest-type = (single-value-type arest)
203     for arg = (pop preq) while arg do
204     (types rest-type))
205     (when (or popt prest)
206     (give-up))))
207     (values (types) (length (types)))))))))
208 dtc 1.29
209    
210 ram 1.18 ;;; Switch to disable check complementing, for evaluation.
211     ;;;
212     (defvar *complement-type-checks* t)
213    
214 wlott 1.1 ;;; MAYBE-NEGATE-CHECK -- Internal
215     ;;;
216     ;;; Cont is a continuation we are doing a type check on and Types is a list
217     ;;; of types that we are checking its values against. If we have proven
218     ;;; that Cont generates a fixed number of values, then for each value, we check
219 dtc 1.26 ;;; whether it is cheaper to then difference between the proven type and
220 wlott 1.1 ;;; the corresponding type in Types. If so, we opt for a :HAIRY check with
221     ;;; that test negated. Otherwise, we try to do a simple test, and if that is
222 ram 1.13 ;;; impossible, we do a hairy test with non-negated types. If true,
223     ;;; Force-Hairy forces a hairy type check.
224 wlott 1.1 ;;;
225 ram 1.16 ;;; When doing a non-negated check, we call MAYBE-WEAKEN-CHECK to weaken the
226     ;;; test to a convenient supertype (conditional on policy.) If debug-info is
227     ;;; not particularly important (debug <= 1) or speed is 3, then we allow
228     ;;; weakened checks to be simple, resulting in less informative error messages,
229     ;;; but saving space and possibly time.
230 ram 1.7 ;;;
231 ram 1.13 (defun maybe-negate-check (cont types force-hairy)
232 wlott 1.1 (declare (type continuation cont) (list types))
233 ram 1.9 (multiple-value-bind
234     (ptypes count)
235 dtc 1.28 (values-types (continuation-proven-type cont))
236 wlott 1.1 (if (eq count :unknown)
237 ram 1.13 (if (and (every #'type-check-template types) (not force-hairy))
238 wlott 1.1 (values :simple types)
239 ram 1.3 (values :hairy
240     (mapcar #'(lambda (x)
241     (list nil (maybe-weaken-check x cont) x))
242     types)))
243 wlott 1.1 (let ((res (mapcar #'(lambda (p c)
244 dtc 1.28 (if (csubtypep p c)
245     (list nil *universal-type* c)
246     (let ((diff (type-difference p c))
247     (weak (maybe-weaken-check c cont)))
248     (if (and diff
249     (< (type-test-cost diff)
250     (type-test-cost weak))
251     *complement-type-checks*)
252     (list t diff c)
253     (list nil weak c)))))
254     (no-function-types ptypes) types)))
255 ram 1.16 (cond ((or force-hairy (find-if #'first res))
256     (values :hairy res))
257     ((every #'type-check-template types)
258     (values :simple types))
259     ((policy (continuation-dest cont)
260     (or (<= debug 1) (and (= speed 3) (/= debug 3))))
261     (let ((weakened (mapcar #'second res)))
262     (if (every #'type-check-template weakened)
263     (values :simple weakened)
264     (values :hairy res))))
265     (t
266     (values :hairy res)))))))
267 wlott 1.1
268    
269     ;;; CONTINUATION-CHECK-TYPES -- Interface
270     ;;;
271     ;;; Determines whether Cont's assertion is:
272     ;;; -- Checkable by the back end (:SIMPLE), or
273     ;;; -- Not checkable by the back end, but checkable via an explicit test in
274     ;;; type check conversion (:HAIRY), or
275     ;;; -- not reasonably checkable at all (:TOO-HAIRY).
276     ;;;
277     ;;; A type is checkable if it either represents a fixed number of values (as
278     ;;; determined by VALUES-TYPES), or it is the assertion for an MV-Bind. A type
279     ;;; is simply checkable if all the type assertions have a TYPE-CHECK-TEMPLATE.
280     ;;; In this :SIMPLE case, the second value is a list of the type restrictions
281     ;;; specified for the leading positional values.
282     ;;;
283 ram 1.13 ;;; We force a check to be hairy even when there are fixed values if we are in
284     ;;; a context where we may be forced to use the unknown values convention
285     ;;; anyway. This is because IR2tran can't generate type checks for unknown
286     ;;; values continuations but people could still be depending on the check being
287     ;;; done. We only care about EXIT and RETURN (not MV-COMBINATION) since these
288     ;;; are the only contexts where the ultimate values receiver
289     ;;;
290 wlott 1.1 ;;; In the :HAIRY case, the second value is a list of triples of the form:
291     ;;; (Not-P Type Original-Type)
292     ;;;
293     ;;; If true, the Not-P flag indicates a test that the corresponding value is
294     ;;; *not* of the specified Type. Original-Type is the type asserted on this
295     ;;; value in the continuation, for use in error messages. When Not-P is true,
296     ;;; this will be different from Type.
297     ;;;
298     ;;; This allows us to take what has been proven about Cont's type into
299     ;;; consideration. If it is cheaper to test for the difference between the
300     ;;; derived type and the asserted type, then we check for the negation of this
301     ;;; type instead.
302     ;;;
303 dtc 1.30 ;;; When the proven type represents an unknown number of values, but Cont's
304     ;;; destination receives only a single value, a :hairy type check is
305     ;;; generated for the single-values-type of the asserted type.
306     ;;;
307 gerd 1.32 (defun continuation-check-types (cont &optional force-hairy)
308 wlott 1.1 (declare (type continuation cont))
309 dtc 1.28 (let ((atype (continuation-asserted-type cont))
310 gerd 1.33 (dest (continuation-dest cont))
311     (proven (continuation-proven-type cont)))
312 dtc 1.28 (assert (not (eq atype *wild-type*)))
313 wlott 1.1 (multiple-value-bind (types count)
314 gerd 1.33 (values-types-asserted atype proven)
315 wlott 1.1 (cond ((not (eq count :unknown))
316 dtc 1.28 (let ((types (no-function-types types)))
317     (if (or (exit-p dest)
318     (and (return-p dest)
319     (multiple-value-bind
320     (ignore count)
321     (values-types (return-result-type dest))
322     (declare (ignore ignore))
323     (eq count :unknown))))
324     (maybe-negate-check cont types t)
325 gerd 1.32 (maybe-negate-check cont types force-hairy))))
326 gerd 1.33 #+nil
327     ((eq *wild-type* proven)
328     (values :too-hairy nil))
329 wlott 1.1 ((and (mv-combination-p dest)
330     (eq (basic-combination-kind dest) :local))
331 dtc 1.28 (assert (values-type-p atype))
332     (assert (null (args-type-required atype)))
333 gerd 1.32 (maybe-negate-check cont (args-type-optional atype) force-hairy))
334 dtc 1.30 ((or (exit-p dest) (return-p dest) (mv-combination-p dest))
335     (values :too-hairy nil))
336 wlott 1.1 (t
337 dtc 1.30 (maybe-negate-check cont (list (single-value-type atype)) t))))))
338 wlott 1.1
339    
340     ;;; Probable-Type-Check-P -- Internal
341     ;;;
342     ;;; Return true if Cont is a continuation whose type the back end is likely
343     ;;; to want to check. Since we don't know what template the back end is going
344     ;;; to choose to implement the continuation's DEST, we use a heuristic. We
345     ;;; always return T unless:
346     ;;; -- Nobody uses the value, or
347 ram 1.3 ;;; -- Safety is totally unimportant, or
348 wlott 1.1 ;;; -- the continuation is an argument to an unknown function, or
349     ;;; -- the continuation is an argument to a known function that has no
350     ;;; IR2-Convert method or :fast-safe templates that are compatible with the
351     ;;; call's type.
352     ;;;
353     ;;; We must only return nil when it is *certain* that a check will not be done,
354     ;;; since if we pass up this chance to do the check, it will be too late. The
355     ;;; penalty for being too conservative is duplicated type checks.
356     ;;;
357 ram 1.13 ;;; If there is a compile-time type error, then we always return true unless
358     ;;; the DEST is a full call. With a full call, the theory is that the type
359     ;;; error is probably from a declaration in (or on) the callee, so the callee
360     ;;; should be able to do the check. We want to let the callee do the check,
361     ;;; because it is possible that the error is really in the callee, not the
362     ;;; caller. We don't want to make people recompile all calls to a function
363     ;;; when they were originally compiled with a bad declaration (or an old type
364     ;;; assertion derived from a definition appearing after the call.)
365 wlott 1.1 ;;;
366     (defun probable-type-check-p (cont)
367     (declare (type continuation cont))
368     (let ((dest (continuation-dest cont)))
369 ram 1.13 (cond ((eq (continuation-type-check cont) :error)
370 ram 1.22 (if (and (combination-p dest) (eq (combination-kind dest) :error))
371 gerd 1.32 (policy dest (= safety 3))
372 ram 1.13 t))
373 wlott 1.1 ((or (not dest)
374 ram 1.3 (policy dest (zerop safety)))
375 wlott 1.1 nil)
376     ((basic-combination-p dest)
377     (let ((kind (basic-combination-kind dest)))
378     (cond ((eq cont (basic-combination-fun dest)) t)
379     ((eq kind :local) t)
380 ram 1.22 ((member kind '(:full :error)) nil)
381 wlott 1.1 ((function-info-ir2-convert kind) t)
382     (t
383     (dolist (template (function-info-templates kind) nil)
384 ram 1.6 (when (eq (template-policy template) :fast-safe)
385     (multiple-value-bind
386     (val win)
387     (valid-function-use dest (template-type template))
388     (when (or val (not win)) (return t)))))))))
389 wlott 1.1 (t t))))
390    
391    
392     ;;; Make-Type-Check-Form -- Internal
393     ;;;
394     ;;; Return a form that we can convert to do a hairy type check of the
395     ;;; specified Types. Types is a list of the format returned by
396     ;;; Continuation-Check-Types in the :HAIRY case. In place of the actual
397     ;;; value(s) we are to check, we use 'Dummy. This constant reference is later
398     ;;; replaced with the actual values continuation.
399     ;;;
400     ;;; Note that we don't attempt to check for required values being unsupplied.
401     ;;; Such checking is impossible to efficiently do at the source level because
402     ;;; our fixed-values conventions are optimized for the common MV-Bind case.
403     ;;;
404     ;;; We can always use Multiple-Value-Bind, since the macro is clever about
405     ;;; binding a single variable.
406     ;;;
407     (defun make-type-check-form (types)
408     (collect ((temps))
409     (dotimes (i (length types))
410     (temps (gensym)))
411    
412     `(multiple-value-bind ,(temps)
413     'dummy
414     ,@(mapcar #'(lambda (temp type)
415 ram 1.10 (let* ((spec
416     (let ((*unparse-function-type-simplify* t))
417     (type-specifier (second type))))
418 wlott 1.1 (test (if (first type) `(not ,spec) spec)))
419     `(unless (typep ,temp ',test)
420     (%type-check-error
421     ,temp
422     ',(type-specifier (third type))))))
423     (temps) types)
424     (values ,@(temps)))))
425    
426    
427     ;;; Convert-Type-Check -- Internal
428     ;;;
429     ;;; Splice in explicit type check code immediately before the node that its
430     ;;; Cont's Dest. This code receives the value(s) that were being passed to
431     ;;; Cont, checks the type(s) of the value(s), then passes them on to Cont.
432     ;;; We:
433     ;;; -- Ensure that Cont starts a block, so that we can freely manipulate its
434     ;;; uses.
435     ;;; -- Make a new continuation and move Cont's uses to it. Set type set
436     ;;; Type-Check in Cont to :DELETED to indicate that the check has been
437     ;;; done.
438     ;;; -- Make the Dest node start its block so that we can splice in the type
439     ;;; check code.
440     ;;; -- Splice in a new block before the Dest block, giving it all the Dest's
441     ;;; predecessors.
442     ;;; -- Convert the check form, using the new block start as Start and a dummy
443     ;;; continuation as Cont.
444     ;;; -- Set the new block's start and end cleanups to the *start* cleanup of
445     ;;; Prev's block. This overrides the incorrect default from
446     ;;; With-IR1-Environment.
447     ;;; -- Finish off the dummy continuation's block, and change the use to a use
448     ;;; of Cont. (we need to use the dummy continuation to get the control
449     ;;; transfer right, since we want to go to Prev's block, not Cont's.)
450     ;;; Link the new block to Prev's block.
451     ;;; -- Substitute the new continuation for the dummy placeholder argument.
452     ;;; Since no let conversion has been done yet, we can find the placeholder.
453     ;;; The [mv-]combination node from the mv-bind in the check form will be
454     ;;; the Use of the new check continuation. We substitute for the first
455     ;;; argument of this node.
456     ;;; -- Invoke local call analysis to convert the call to a let.
457     ;;;
458     (defun convert-type-check (cont types)
459     (declare (type continuation cont) (list types))
460     (with-ir1-environment (continuation-dest cont)
461     (ensure-block-start cont)
462     (let* ((new-start (make-continuation))
463     (dest (continuation-dest cont))
464     (prev (node-prev dest)))
465     (continuation-starts-block new-start)
466     (substitute-continuation-uses new-start cont)
467     (setf (continuation-%type-check cont) :deleted)
468    
469     (when (continuation-use prev)
470     (node-ends-block (continuation-use prev)))
471    
472     (let* ((prev-block (continuation-block prev))
473     (new-block (continuation-block new-start))
474     (dummy (make-continuation)))
475     (dolist (block (block-pred prev-block))
476     (change-block-successor block prev-block new-block))
477     (ir1-convert new-start dummy (make-type-check-form types))
478     (assert (eq (continuation-block dummy) new-block))
479    
480     (let ((node (continuation-use dummy)))
481     (setf (block-last new-block) node)
482     (delete-continuation-use node)
483     (add-continuation-use node cont))
484     (link-blocks new-block prev-block))
485    
486     (let* ((node (continuation-use cont))
487     (args (basic-combination-args node))
488     (victim (first args)))
489     (assert (and (= (length args) 1)
490     (eq (constant-value
491     (ref-leaf
492     (continuation-use victim)))
493     'dummy)))
494     (substitute-continuation new-start victim)))
495    
496     (local-call-analyze *current-component*))
497    
498     (undefined-value))
499    
500    
501 ram 1.13 ;;; DO-TYPE-WARNING -- Internal
502     ;;;
503     ;;; Emit a type warning for Node. If the value of node is being used for a
504     ;;; variable binding, we figure out which one for source context. If the value
505 ram 1.14 ;;; is a constant, we print it specially. We ignore nodes whose type is NIL,
506     ;;; since they are supposed to never return.
507 ram 1.13 ;;;
508     (defun do-type-warning (node)
509     (declare (type node node))
510     (let* ((*compiler-error-context* node)
511     (cont (node-cont node))
512     (atype-spec (type-specifier (continuation-asserted-type cont)))
513     (dtype (node-derived-type node))
514     (dest (continuation-dest cont))
515     (what (when (and (combination-p dest)
516     (eq (combination-kind dest) :local))
517     (let ((lambda (combination-lambda dest))
518 ram 1.23 (pos (eposition cont (combination-args dest))))
519 ram 1.13 (format nil "~:[A possible~;The~] binding of ~S"
520     (and (continuation-use cont)
521     (eq (functional-kind lambda) :let))
522     (leaf-name (elt (lambda-vars lambda) pos)))))))
523 ram 1.14 (cond ((eq dtype *empty-type*))
524     ((and (ref-p node) (constant-p (ref-leaf node)))
525     (compiler-warning "~:[This~;~:*~A~] is not a ~<~%~9T~:;~S:~>~% ~S"
526 ram 1.13 what atype-spec (constant-value (ref-leaf node))))
527 ram 1.14 (t
528     (compiler-warning
529     "~:[Result~;~:*~A~] is a ~S, ~<~%~9T~:;not a ~S.~>"
530     what (type-specifier dtype) atype-spec))))
531 ram 1.13 (undefined-value))
532    
533    
534     ;;; MARK-ERROR-CONTINUATION -- Internal
535     ;;;
536     ;;; Mark Cont as being a continuation with a manifest type error. We set
537     ;;; the kind to :ERROR, and clear any FUNCTION-INFO if the continuation is an
538     ;;; argument to a known call. The last is done so that the back end doesn't
539     ;;; have to worry about type errors in arguments to known functions. This
540     ;;; clearing is inhibited for things with IR2-CONVERT methods, since we can't
541     ;;; do a full call to funny functions.
542     ;;;
543     (defun mark-error-continuation (cont)
544     (declare (type continuation cont))
545     (setf (continuation-%type-check cont) :error)
546     (let ((dest (continuation-dest cont)))
547     (when (and (combination-p dest)
548 ram 1.22 (let ((kind (basic-combination-kind dest)))
549     (or (eq kind :full)
550     (and (function-info-p kind)
551     (not (function-info-ir2-convert kind))))))
552     (setf (basic-combination-kind dest) :error)))
553 ram 1.13 (undefined-value))
554    
555    
556 wlott 1.1 ;;; Generate-Type-Checks -- Interface
557     ;;;
558     ;;; Loop over all blocks in Component that have TYPE-CHECK set, looking for
559     ;;; continuations with TYPE-CHECK T. We do two mostly unrelated things: detect
560     ;;; compile-time type errors and determine if and how to do run-time type
561     ;;; checks.
562     ;;;
563 ram 1.13 ;;; If there is a compile-time type error, then we mark the continuation and
564     ;;; emit a warning if appropriate. This part loops over all the uses of the
565     ;;; continuation, since after we convert the check, the :DELETED kind will
566     ;;; inhibit warnings about the types of other uses.
567 ram 1.2 ;;;
568 wlott 1.1 ;;; If a continuation is too complex to be checked by the back end, or is
569     ;;; better checked with explicit code, then convert to an explicit test.
570     ;;; Assertions that can checked by the back end are passed through. Assertions
571     ;;; that can't be tested are flamed about and marked as not needing to be
572     ;;; checked.
573     ;;;
574     ;;; If we determine that a type check won't be done, then we set TYPE-CHECK
575     ;;; to :NO-CHECK. In the non-hairy cases, this is just to prevent us from
576     ;;; wasting time coming to the same conclusion again on a later iteration. In
577     ;;; the hairy case, we must indicate to LTN that it must choose a safe
578     ;;; implementation, since IR2 conversion will choke on the check.
579     ;;;
580 dtc 1.25 ;;; The generation of the type checks is delayed until all the type
581     ;;; check decisions have been made because the generation of the type
582     ;;; checks creates new nodes who's derived types aren't always updated
583     ;;; which may lead to inappropriate template choices due to the
584     ;;; modification of argument types.
585     ;;;
586 wlott 1.1 (defun generate-type-checks (component)
587 dtc 1.25 (collect ((conts))
588     (do-blocks (block component)
589     (when (block-type-check block)
590     (do-nodes (node cont block)
591     (let ((type-check (continuation-type-check cont)))
592     (unless (member type-check '(nil :error :deleted))
593     (let ((atype (continuation-asserted-type cont)))
594     (do-uses (use cont)
595     (unless (values-types-intersect (node-derived-type use)
596     atype)
597     (mark-error-continuation cont)
598     (unless (policy node (= brevity 3))
599     (do-type-warning use))))))
600     (when (and (eq type-check t)
601     (not *byte-compiling*))
602     (if (probable-type-check-p cont)
603     (conts cont)
604     (setf (continuation-%type-check cont) :no-check)))))
605    
606     (setf (block-type-check block) nil)))
607    
608     (dolist (cont (conts))
609     (multiple-value-bind (check types)
610 gerd 1.32 (continuation-check-types
611     cont
612     (and (eq (continuation-%type-check cont) :error)
613     (policy (continuation-dest cont) (= safety 3))))
614 dtc 1.25 (ecase check
615     (:simple)
616     (:hairy
617     (convert-type-check cont types))
618     (:too-hairy
619     (let* ((context (continuation-dest cont))
620     (*compiler-error-context* context))
621     (when (policy context (>= safety brevity))
622     (compiler-note
623     "Type assertion too complex to check:~% ~S."
624     (type-specifier (continuation-asserted-type cont)))))
625     (setf (continuation-%type-check cont) :deleted))))))
626    
627 wlott 1.1 (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5