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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (hide annotations)
Fri Dec 9 15:50:20 2005 UTC (8 years, 4 months ago) by rtoy
Branch: MAIN
CVS Tags: double-double-array-base, merged-unicode-utf16-extfmt-2009-06-11, double-double-init-sparc-2, unicode-utf16-extfmt-2009-03-27, double-double-base, snapshot-2007-09, snapshot-2007-08, snapshot-2008-08, snapshot-2008-09, sse2-packed-2008-11-12, snapshot-2008-05, snapshot-2008-06, snapshot-2008-07, snapshot-2007-05, snapshot-2008-01, snapshot-2008-02, snapshot-2008-03, snapshot-2006-11, snapshot-2006-10, double-double-init-sparc, snapshot-2006-12, unicode-string-buffer-impl-base, sse2-base, unicode-string-buffer-base, sse2-packed-base, amd64-dd-start, release-19f-pre1, snapshot-2008-12, snapshot-2008-11, intl-2-branch-base, snapshot-2007-01, snapshot-2007-02, release-19e, release-19d, double-double-init-ppc, unicode-utf16-sync-2008-12, label-2009-03-16, release-19f-base, merge-sse2-packed, merge-with-19f, unicode-snapshot-2009-05, unicode-snapshot-2009-06, double-double-init-%make-sparc, unicode-utf16-sync-2008-07, unicode-utf16-sync-2008-09, unicode-utf16-extfmts-sync-2008-12, snapshot-2008-04, unicode-utf16-sync-label-2009-03-16, RELEASE_19f, snapshot-2007-03, release-20a-base, unicode-utf16-char-support-2009-03-26, unicode-utf16-char-support-2009-03-25, unicode-utf16-extfmts-pre-sync-2008-11, snapshot-2008-10, snapshot-2007-04, unicode-utf16-sync-2008-11, snapshot-2007-07, snapshot-2007-06, pre-merge-intl-branch, double-double-array-checkpoint, double-double-reader-checkpoint-1, release-19d-base, release-19e-pre1, double-double-irrat-end, release-19e-pre2, snapshot-2010-01, snapshot-2010-03, snapshot-2010-02, release-19d-pre2, release-19d-pre1, double-double-init-checkpoint-1, double-double-reader-base, label-2009-03-25, double-double-init-x86, sse2-checkpoint-2008-10-01, double-double-sparc-checkpoint-1, sse2-merge-with-2008-11, sse2-merge-with-2008-10, RELEASE_20a, release-20a-pre1, snapshot-2009-11, snapshot-2009-12, unicode-utf16-extfmt-2009-06-11, portable-clx-import-2009-06-16, unicode-utf16-string-support, release-19e-base, intl-branch-base, double-double-irrat-start, unicode-utf16-base, portable-clx-base, snapshot-2009-08, snapshot-2007-12, snapshot-2007-10, snapshot-2007-11, snapshot-2009-02, snapshot-2009-01, snapshot-2009-07, snapshot-2009-05, snapshot-2009-04, snapshot-2006-02, snapshot-2006-03, snapshot-2006-01, snapshot-2006-06, snapshot-2006-07, snapshot-2006-04, snapshot-2006-05, pre-telent-clx, snapshot-2006-08, snapshot-2006-09
Branch point for: double-double-reader-branch, double-double-array-branch, RELEASE-19F-BRANCH, portable-clx-branch, unicode-string-buffer-branch, release-19d-branch, sse2-packed-branch, RELEASE-20A-BRANCH, amd64-dd-branch, double-double-branch, unicode-string-buffer-impl-branch, intl-branch, unicode-utf16-branch, release-19e-branch, sse2-branch, intl-2-branch, unicode-utf16-extfmt-branch
Changes since 1.33: +4 -3 lines
Fix bug reported by Drew McDermott, cmucl-help, 2005/12/07.

(defun spunk (a b)
   (let (k m p q)
      (multiple-value-setq
         (k m p q)
        (the (values t t t t)
          (punk a b)))))


Remove the assertion that the values type must have no required args
and adjust maybe-negate-check appropriately.  (This last from Alexey
Dejneka.)
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 rtoy 1.34 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/checkgen.lisp,v 1.34 2005/12/09 15:50:20 rtoy Rel $")
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 rtoy 1.34 (maybe-negate-check cont (append (args-type-required atype)
333     (args-type-optional atype))
334     force-hairy))
335 dtc 1.30 ((or (exit-p dest) (return-p dest) (mv-combination-p dest))
336     (values :too-hairy nil))
337 wlott 1.1 (t
338 dtc 1.30 (maybe-negate-check cont (list (single-value-type atype)) t))))))
339 wlott 1.1
340    
341     ;;; Probable-Type-Check-P -- Internal
342     ;;;
343     ;;; Return true if Cont is a continuation whose type the back end is likely
344     ;;; to want to check. Since we don't know what template the back end is going
345     ;;; to choose to implement the continuation's DEST, we use a heuristic. We
346     ;;; always return T unless:
347     ;;; -- Nobody uses the value, or
348 ram 1.3 ;;; -- Safety is totally unimportant, or
349 wlott 1.1 ;;; -- the continuation is an argument to an unknown function, or
350     ;;; -- the continuation is an argument to a known function that has no
351     ;;; IR2-Convert method or :fast-safe templates that are compatible with the
352     ;;; call's type.
353     ;;;
354     ;;; We must only return nil when it is *certain* that a check will not be done,
355     ;;; since if we pass up this chance to do the check, it will be too late. The
356     ;;; penalty for being too conservative is duplicated type checks.
357     ;;;
358 ram 1.13 ;;; If there is a compile-time type error, then we always return true unless
359     ;;; the DEST is a full call. With a full call, the theory is that the type
360     ;;; error is probably from a declaration in (or on) the callee, so the callee
361     ;;; should be able to do the check. We want to let the callee do the check,
362     ;;; because it is possible that the error is really in the callee, not the
363     ;;; caller. We don't want to make people recompile all calls to a function
364     ;;; when they were originally compiled with a bad declaration (or an old type
365     ;;; assertion derived from a definition appearing after the call.)
366 wlott 1.1 ;;;
367     (defun probable-type-check-p (cont)
368     (declare (type continuation cont))
369     (let ((dest (continuation-dest cont)))
370 ram 1.13 (cond ((eq (continuation-type-check cont) :error)
371 ram 1.22 (if (and (combination-p dest) (eq (combination-kind dest) :error))
372 gerd 1.32 (policy dest (= safety 3))
373 ram 1.13 t))
374 wlott 1.1 ((or (not dest)
375 ram 1.3 (policy dest (zerop safety)))
376 wlott 1.1 nil)
377     ((basic-combination-p dest)
378     (let ((kind (basic-combination-kind dest)))
379     (cond ((eq cont (basic-combination-fun dest)) t)
380     ((eq kind :local) t)
381 ram 1.22 ((member kind '(:full :error)) nil)
382 wlott 1.1 ((function-info-ir2-convert kind) t)
383     (t
384     (dolist (template (function-info-templates kind) nil)
385 ram 1.6 (when (eq (template-policy template) :fast-safe)
386     (multiple-value-bind
387     (val win)
388     (valid-function-use dest (template-type template))
389     (when (or val (not win)) (return t)))))))))
390 wlott 1.1 (t t))))
391    
392    
393     ;;; Make-Type-Check-Form -- Internal
394     ;;;
395     ;;; Return a form that we can convert to do a hairy type check of the
396     ;;; specified Types. Types is a list of the format returned by
397     ;;; Continuation-Check-Types in the :HAIRY case. In place of the actual
398     ;;; value(s) we are to check, we use 'Dummy. This constant reference is later
399     ;;; replaced with the actual values continuation.
400     ;;;
401     ;;; Note that we don't attempt to check for required values being unsupplied.
402     ;;; Such checking is impossible to efficiently do at the source level because
403     ;;; our fixed-values conventions are optimized for the common MV-Bind case.
404     ;;;
405     ;;; We can always use Multiple-Value-Bind, since the macro is clever about
406     ;;; binding a single variable.
407     ;;;
408     (defun make-type-check-form (types)
409     (collect ((temps))
410     (dotimes (i (length types))
411     (temps (gensym)))
412    
413     `(multiple-value-bind ,(temps)
414     'dummy
415     ,@(mapcar #'(lambda (temp type)
416 ram 1.10 (let* ((spec
417     (let ((*unparse-function-type-simplify* t))
418     (type-specifier (second type))))
419 wlott 1.1 (test (if (first type) `(not ,spec) spec)))
420     `(unless (typep ,temp ',test)
421     (%type-check-error
422     ,temp
423     ',(type-specifier (third type))))))
424     (temps) types)
425     (values ,@(temps)))))
426    
427    
428     ;;; Convert-Type-Check -- Internal
429     ;;;
430     ;;; Splice in explicit type check code immediately before the node that its
431     ;;; Cont's Dest. This code receives the value(s) that were being passed to
432     ;;; Cont, checks the type(s) of the value(s), then passes them on to Cont.
433     ;;; We:
434     ;;; -- Ensure that Cont starts a block, so that we can freely manipulate its
435     ;;; uses.
436     ;;; -- Make a new continuation and move Cont's uses to it. Set type set
437     ;;; Type-Check in Cont to :DELETED to indicate that the check has been
438     ;;; done.
439     ;;; -- Make the Dest node start its block so that we can splice in the type
440     ;;; check code.
441     ;;; -- Splice in a new block before the Dest block, giving it all the Dest's
442     ;;; predecessors.
443     ;;; -- Convert the check form, using the new block start as Start and a dummy
444     ;;; continuation as Cont.
445     ;;; -- Set the new block's start and end cleanups to the *start* cleanup of
446     ;;; Prev's block. This overrides the incorrect default from
447     ;;; With-IR1-Environment.
448     ;;; -- Finish off the dummy continuation's block, and change the use to a use
449     ;;; of Cont. (we need to use the dummy continuation to get the control
450     ;;; transfer right, since we want to go to Prev's block, not Cont's.)
451     ;;; Link the new block to Prev's block.
452     ;;; -- Substitute the new continuation for the dummy placeholder argument.
453     ;;; Since no let conversion has been done yet, we can find the placeholder.
454     ;;; The [mv-]combination node from the mv-bind in the check form will be
455     ;;; the Use of the new check continuation. We substitute for the first
456     ;;; argument of this node.
457     ;;; -- Invoke local call analysis to convert the call to a let.
458     ;;;
459     (defun convert-type-check (cont types)
460     (declare (type continuation cont) (list types))
461     (with-ir1-environment (continuation-dest cont)
462     (ensure-block-start cont)
463     (let* ((new-start (make-continuation))
464     (dest (continuation-dest cont))
465     (prev (node-prev dest)))
466     (continuation-starts-block new-start)
467     (substitute-continuation-uses new-start cont)
468     (setf (continuation-%type-check cont) :deleted)
469    
470     (when (continuation-use prev)
471     (node-ends-block (continuation-use prev)))
472    
473     (let* ((prev-block (continuation-block prev))
474     (new-block (continuation-block new-start))
475     (dummy (make-continuation)))
476     (dolist (block (block-pred prev-block))
477     (change-block-successor block prev-block new-block))
478     (ir1-convert new-start dummy (make-type-check-form types))
479     (assert (eq (continuation-block dummy) new-block))
480    
481     (let ((node (continuation-use dummy)))
482     (setf (block-last new-block) node)
483     (delete-continuation-use node)
484     (add-continuation-use node cont))
485     (link-blocks new-block prev-block))
486    
487     (let* ((node (continuation-use cont))
488     (args (basic-combination-args node))
489     (victim (first args)))
490     (assert (and (= (length args) 1)
491     (eq (constant-value
492     (ref-leaf
493     (continuation-use victim)))
494     'dummy)))
495     (substitute-continuation new-start victim)))
496    
497     (local-call-analyze *current-component*))
498    
499     (undefined-value))
500    
501    
502 ram 1.13 ;;; DO-TYPE-WARNING -- Internal
503     ;;;
504     ;;; Emit a type warning for Node. If the value of node is being used for a
505     ;;; variable binding, we figure out which one for source context. If the value
506 ram 1.14 ;;; is a constant, we print it specially. We ignore nodes whose type is NIL,
507     ;;; since they are supposed to never return.
508 ram 1.13 ;;;
509     (defun do-type-warning (node)
510     (declare (type node node))
511     (let* ((*compiler-error-context* node)
512     (cont (node-cont node))
513     (atype-spec (type-specifier (continuation-asserted-type cont)))
514     (dtype (node-derived-type node))
515     (dest (continuation-dest cont))
516     (what (when (and (combination-p dest)
517     (eq (combination-kind dest) :local))
518     (let ((lambda (combination-lambda dest))
519 ram 1.23 (pos (eposition cont (combination-args dest))))
520 ram 1.13 (format nil "~:[A possible~;The~] binding of ~S"
521     (and (continuation-use cont)
522     (eq (functional-kind lambda) :let))
523     (leaf-name (elt (lambda-vars lambda) pos)))))))
524 ram 1.14 (cond ((eq dtype *empty-type*))
525     ((and (ref-p node) (constant-p (ref-leaf node)))
526     (compiler-warning "~:[This~;~:*~A~] is not a ~<~%~9T~:;~S:~>~% ~S"
527 ram 1.13 what atype-spec (constant-value (ref-leaf node))))
528 ram 1.14 (t
529     (compiler-warning
530     "~:[Result~;~:*~A~] is a ~S, ~<~%~9T~:;not a ~S.~>"
531     what (type-specifier dtype) atype-spec))))
532 ram 1.13 (undefined-value))
533    
534    
535     ;;; MARK-ERROR-CONTINUATION -- Internal
536     ;;;
537     ;;; Mark Cont as being a continuation with a manifest type error. We set
538     ;;; the kind to :ERROR, and clear any FUNCTION-INFO if the continuation is an
539     ;;; argument to a known call. The last is done so that the back end doesn't
540     ;;; have to worry about type errors in arguments to known functions. This
541     ;;; clearing is inhibited for things with IR2-CONVERT methods, since we can't
542     ;;; do a full call to funny functions.
543     ;;;
544     (defun mark-error-continuation (cont)
545     (declare (type continuation cont))
546     (setf (continuation-%type-check cont) :error)
547     (let ((dest (continuation-dest cont)))
548     (when (and (combination-p dest)
549 ram 1.22 (let ((kind (basic-combination-kind dest)))
550     (or (eq kind :full)
551     (and (function-info-p kind)
552     (not (function-info-ir2-convert kind))))))
553     (setf (basic-combination-kind dest) :error)))
554 ram 1.13 (undefined-value))
555    
556    
557 wlott 1.1 ;;; Generate-Type-Checks -- Interface
558     ;;;
559     ;;; Loop over all blocks in Component that have TYPE-CHECK set, looking for
560     ;;; continuations with TYPE-CHECK T. We do two mostly unrelated things: detect
561     ;;; compile-time type errors and determine if and how to do run-time type
562     ;;; checks.
563     ;;;
564 ram 1.13 ;;; If there is a compile-time type error, then we mark the continuation and
565     ;;; emit a warning if appropriate. This part loops over all the uses of the
566     ;;; continuation, since after we convert the check, the :DELETED kind will
567     ;;; inhibit warnings about the types of other uses.
568 ram 1.2 ;;;
569 wlott 1.1 ;;; If a continuation is too complex to be checked by the back end, or is
570     ;;; better checked with explicit code, then convert to an explicit test.
571     ;;; Assertions that can checked by the back end are passed through. Assertions
572     ;;; that can't be tested are flamed about and marked as not needing to be
573     ;;; checked.
574     ;;;
575     ;;; If we determine that a type check won't be done, then we set TYPE-CHECK
576     ;;; to :NO-CHECK. In the non-hairy cases, this is just to prevent us from
577     ;;; wasting time coming to the same conclusion again on a later iteration. In
578     ;;; the hairy case, we must indicate to LTN that it must choose a safe
579     ;;; implementation, since IR2 conversion will choke on the check.
580     ;;;
581 dtc 1.25 ;;; The generation of the type checks is delayed until all the type
582     ;;; check decisions have been made because the generation of the type
583     ;;; checks creates new nodes who's derived types aren't always updated
584     ;;; which may lead to inappropriate template choices due to the
585     ;;; modification of argument types.
586     ;;;
587 wlott 1.1 (defun generate-type-checks (component)
588 dtc 1.25 (collect ((conts))
589     (do-blocks (block component)
590     (when (block-type-check block)
591     (do-nodes (node cont block)
592     (let ((type-check (continuation-type-check cont)))
593     (unless (member type-check '(nil :error :deleted))
594     (let ((atype (continuation-asserted-type cont)))
595     (do-uses (use cont)
596     (unless (values-types-intersect (node-derived-type use)
597     atype)
598     (mark-error-continuation cont)
599     (unless (policy node (= brevity 3))
600     (do-type-warning use))))))
601     (when (and (eq type-check t)
602     (not *byte-compiling*))
603     (if (probable-type-check-p cont)
604     (conts cont)
605     (setf (continuation-%type-check cont) :no-check)))))
606    
607     (setf (block-type-check block) nil)))
608    
609     (dolist (cont (conts))
610     (multiple-value-bind (check types)
611 gerd 1.32 (continuation-check-types
612     cont
613     (and (eq (continuation-%type-check cont) :error)
614     (policy (continuation-dest cont) (= safety 3))))
615 dtc 1.25 (ecase check
616     (:simple)
617     (:hairy
618     (convert-type-check cont types))
619     (:too-hairy
620     (let* ((context (continuation-dest cont))
621     (*compiler-error-context* context))
622     (when (policy context (>= safety brevity))
623     (compiler-note
624     "Type assertion too complex to check:~% ~S."
625     (type-specifier (continuation-asserted-type cont)))))
626     (setf (continuation-%type-check cont) :deleted))))))
627    
628 wlott 1.1 (undefined-value))

  ViewVC Help
Powered by ViewVC 1.1.5