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

Contents of /src/compiler/checkgen.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5