Initial revision
[sbcl.git] / src / compiler / ir1final.lisp
1 ;;;; This file implements the IR1 finalize phase, which checks for
2 ;;;; various semantic errors.
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C")
14
15 (file-comment
16   "$Header$")
17
18 ;;; Give the user grief about optimizations that we weren't able to do. It
19 ;;; is assumed that they want to hear, or there wouldn't be any entries in the
20 ;;; table. If the node has been deleted or is no longer a known call, then do
21 ;;; nothing; some other optimization must have gotten to it.
22 (defun note-failed-optimization (node failures)
23   (declare (type combination node) (list failures))
24   (unless (or (node-deleted node)
25               (not (function-info-p (combination-kind node))))
26     (let ((*compiler-error-context* node))
27       (dolist (failure failures)
28         (let ((what (cdr failure))
29               (note (transform-note (car failure))))
30           (cond
31            ((consp what)
32             (compiler-note "unable to ~A because:~%~6T~?"
33                            note (first what) (rest what)))
34            ((valid-function-use node what
35                                 :argument-test #'types-intersect
36                                 :result-test #'values-types-intersect)
37             (collect ((messages))
38               (flet ((frob (string &rest stuff)
39                        (messages string)
40                        (messages stuff)))
41                 (valid-function-use node what
42                                     :warning-function #'frob
43                                     :error-function #'frob))
44
45               (compiler-note "unable to ~A due to type uncertainty:~@
46                               ~{~6T~?~^~&~}"
47                              note (messages))))))))))
48
49 ;;; For each named function with an XEP, note the definition of that
50 ;;; name, and add derived type information to the info environment. We
51 ;;; also delete the FUNCTIONAL from *FREE-FUNCTIONS* to eliminate the
52 ;;; possibility that new references might be converted to it.
53 (defun finalize-xep-definition (fun)
54   (let* ((leaf (functional-entry-function fun))
55          (name (leaf-name leaf))
56          (dtype (definition-type leaf)))
57     (setf (leaf-type leaf) dtype)
58     (when (or (and name (symbolp name))
59               (and (consp name) (eq (car name) 'setf)))
60       (let* ((where (info :function :where-from name))
61              (*compiler-error-context* (lambda-bind (main-entry leaf)))
62              (global-def (gethash name *free-functions*))
63              (global-p
64               (and (defined-function-p global-def)
65                    (eq (defined-function-functional global-def) leaf))))
66         (note-name-defined name :function)
67         (when global-p
68           (remhash name *free-functions*))
69         (ecase where
70           (:assumed
71            (let ((approx-type (info :function :assumed-type name)))
72              (when (and approx-type (function-type-p dtype))
73                (valid-approximate-type approx-type dtype))
74              (setf (info :function :type name) dtype)
75              (setf (info :function :assumed-type name) nil))
76            (setf (info :function :where-from name) :defined))
77           (:declared); Just keep declared type.
78           (:defined
79            (when global-p
80              (setf (info :function :type name) dtype)))))))
81   (values))
82
83 ;;; Find all calls in Component to assumed functions and update the assumed
84 ;;; type information. This is delayed until now so that we have the best
85 ;;; possible information about the actual argument types.
86 (defun note-assumed-types (component name var)
87   (when (and (eq (leaf-where-from var) :assumed)
88              (not (and (defined-function-p var)
89                        (eq (defined-function-inlinep var) :notinline)))
90              (eq (info :function :where-from name) :assumed)
91              (eq (info :function :kind name) :function))
92     (let ((atype (info :function :assumed-type name)))
93       (dolist (ref (leaf-refs var))
94         (let ((dest (continuation-dest (node-cont ref))))
95           (when (and (eq (block-component (node-block ref)) component)
96                      (combination-p dest)
97                      (eq (continuation-use (basic-combination-fun dest)) ref))
98             (setq atype (note-function-use dest atype)))))
99       (setf (info :function :assumed-type name) atype))))
100
101 ;;; Do miscellaneous things that we want to do once all optimization has
102 ;;; been done:
103 ;;;  -- Record the derived result type before the back-end trashes the
104 ;;;     flow graph.
105 ;;;  -- Note definition of any entry points.
106 ;;;  -- Note any failed optimizations.
107 (defun ir1-finalize (component)
108   (declare (type component component))
109   (dolist (fun (component-lambdas component))
110     (case (functional-kind fun)
111       (:external
112        (finalize-xep-definition fun))
113       ((nil)
114        (setf (leaf-type fun) (definition-type fun)))))
115
116   (maphash #'note-failed-optimization
117            (component-failed-optimizations component))
118
119   (maphash #'(lambda (k v)
120                (note-assumed-types component k v))
121            *free-functions*)
122   (values))