Debian Codegraph @

src/cmp/cmpprop.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
6 ;;;;
7 ;;;;
8 ;;;; See the file 'LICENSE' for the copyright details.
9 ;;;;
10
11 ;;;; CMPPROP Type propagation.
12
13 (in-package "COMPILER")
14
15 (eval-when (:execute :load-toplevel :compile-toplevel)
16 (defparameter *type-propagation-messages* nil)
17 (defmacro prop-message (string &rest args)
18 (when *type-propagation-messages*
19 `(format *standard-output* ,string ,@args))))
20
21 (defun p1ordinary (c1form form)
22 (declare (ignore c1form))
23 (p1propagate form))
24
25 (defun p1fset (c1form fun fname macro pprint c1forms)
26 (declare (ignore c1form fname macro pprint c1forms))
27 (p1propagate-function fun)
28 'function)
29
30 (defun p1propagate (form)
31 (unless form
32 (return-from p1propagate 'null))
33 (when (c1form-p form)
34 (let ((*cmp-env* (c1form-env form))
35 (*compile-file-pathname* (c1form-file form))
36 (*compile-file-position* (c1form-file-position form))
37 (*current-form* (c1form-form form))
38 (*current-toplevel-form* (c1form-toplevel-form form))
39 (name (c1form-name form)))
40 (ext:when-let ((propagator (gethash name *p1-dispatch-table*)))
41 (prop-message "~&;;; Entering type propagation for ~A" name)
42 (let ((new-type (apply propagator form (c1form-args form))))
43 (prop-message "~&;;; Propagating ~A gives type ~A" name new-type)
44 (return-from p1propagate
45 (setf (c1form-type form)
46 (values-type-and (c1form-type form) new-type)))))))
47 (cmpnote "Refusing to propagate ~A" form)
48 (c1form-type form))
49
50 (defun p1trivial (form &rest rest)
51 (declare (ignore rest))
52 (c1form-type form))
53
54 (defun p1var (form var loc)
55 ;; Use the type of C1FORM because it might have been coerced by a THE form.
56 (let* ((loc-type (if loc (loc-lisp-type loc) t))
57 (var-type (loc-lisp-type var))
58 (type (type-and (type-and loc-type var-type)
59 (c1form-primary-type form))))
60 (prop-message "~&;;; Querying variable ~A gives ~A" (var-name var) type)
61 type))
62
63 (defun p1values (form values)
64 (declare (ignore form))
65 (loop for v in values
66 collect (values-type-primary-type (p1propagate v))
67 into all-values
68 finally (return `(values ,@all-values))))
69
70 (defun p1propagate-list (list)
71 (loop with final-type = t
72 for f in list
73 do (setf final-type (p1propagate f))
74 finally (return final-type)))
75
76 (defun p1block (c1form blk body)
77 (declare (ignore c1form))
78 (setf (blk-type blk) nil)
79 (let ((normal-type (p1propagate body))
80 (blk-type (blk-type blk)))
81 (if blk-type
82 (values-type-or blk-type normal-type)
83 normal-type)))
84
85 (defun p1return-from (c1form blk return-type value)
86 (declare (ignore c1form return-type))
87 (let* ((values-type (p1propagate value))
88 (blk-type (blk-type blk)))
89 (setf (blk-type blk) (if blk-type
90 (values-type-or blk-type values-type)
91 values-type))
92 values-type))
93
94 (defun p1fcall (c1form fun args fun-val call-type)
95 (p1propagate fun)
96 (p1propagate-list args)
97 (ecase call-type
98 (:global
99 (propagate-types fun-val args))
100 (:local
101 (flet ((and-form-type (type form)
102 (and-form-type type form (c1form-form form)
103 :safe "In a call to ~a" (fun-name fun-val))))
104 (loop with local-arg-types = (get-local-arg-types fun-val)
105 for arg-form in args
106 for arg-type = (pop local-arg-types)
107 when arg-type
108 do (and-form-type arg-type arg-form)
109 do (p1propagate arg-form))
110 (let ((dcl-return (or (get-local-return-type fun-val) '(VALUES &REST T)))
111 (fun-return (fun-return-type fun-val)))
112 (and-call-type dcl-return c1form)
113 (and-call-type fun-return c1form)
114 (p1trivial c1form))))
115 (:unknown
116 (p1trivial c1form))))
117
118 (defun p1mcall (c1form fun args fun-val call-type)
119 (p1propagate fun)
120 (p1propagate-list args)
121 (ecase call-type
122 (:global (or (get-return-type fun-val) '(VALUES &REST T)))
123 (:local (or (get-local-return-type fun-val) '(VALUES &REST T)))
124 (:unknown (p1trivial c1form))))
125
126 (defun p1catch (c1form tag body)
127 (declare (ignore c1form))
128 (p1propagate tag)
129 (p1propagate body)
130 t)
131
132 (defun p1throw (c1form catch-value output-value)
133 (declare (ignore c1form))
134 (p1propagate catch-value)
135 (p1propagate output-value)
136 t)
137
138 (defun p1if (c1form fmla true-branch false-branch)
139 (declare (ignore c1form))
140 (let ((t0 (values-type-primary-type (p1propagate fmla))))
141 (cond ((type-true-p t0 *cmp-env*)
142 (p1propagate true-branch))
143 ((type-false-p t0 *cmp-env*)
144 (p1propagate false-branch))
145 (t (let ((t1 (p1propagate true-branch))
146 (t2 (p1propagate false-branch)))
147 (values-type-or t1 t2))))))
148
149 (defun p1fmla-not (c1form form)
150 (declare (ignore c1form))
151 (let ((t0 (values-type-primary-type (p1propagate form))))
152 (cond ((type-true-p t0 *cmp-env*)
153 '(eql nil))
154 ((type-false-p t0 *cmp-env*)
155 '(eql t))
156 (t
157 '(member t nil)))))
158
159 (defun p1fmla-and (c1form butlast last)
160 (declare (ignore c1form))
161 (loop with all-true = t
162 for form in butlast
163 for type = (p1propagate form)
164 for primary-type = (values-type-primary-type type)
165 do (when (type-false-p primary-type *cmp-env*)
166 (return-from p1fmla-and primary-type))
167 (unless (type-true-p primary-type *cmp-env*)
168 (setf all-true nil))
169 finally
170 (setf type (p1propagate last)
171 primary-type (values-type-primary-type type))
172 (return (if (or (type-false-p primary-type *cmp-env*)
173 (and (type-true-p primary-type *cmp-env*) all-true))
174 type
175 (values-type-or 'null type)))))
176
177 (defun p1fmla-or (c1form butlast last)
178 (declare (ignore c1form))
179 (loop for form in butlast
180 for type = (p1propagate form)
181 for primary-type = (values-type-primary-type type)
182 for output-type = primary-type then (type-or primary-type output-type)
183 do (when (type-true-p primary-type *cmp-env*)
184 (return-from p1fmla-or (type-and output-type '(not null))))
185 finally
186 (setf type (p1propagate last)
187 primary-type (values-type-primary-type type)
188 output-type (values-type-or type output-type))
189 (return (if (type-true-p primary-type *cmp-env*)
190 (values-type-and output-type '(not null))
191 output-type))))
192
193 (defun p1lambda (c1form lambda-list doc body &rest not-used)
194 (declare (ignore c1form lambda-list doc not-used))
195 (prop-message "~&;;;~&;;; Propagating function~&;;;")
196 (p1propagate body))
197
198 (defun p1propagate-function (fun)
199 (setf (fun-return-type fun) (p1propagate (fun-lambda fun))))
200
201 (defun p1let* (c1form vars forms body)
202 (declare (ignore c1form))
203 (loop with type
204 for v in vars
205 for f in forms
206 unless (or (global-var-p v) (var-set-nodes v))
207 do (progn
208 (setf type (p1propagate f))
209 (setf (var-type v)
210 (type-and (values-type-primary-type type) (var-type v)))
211 (prop-message "~&;;; Variable ~A assigned type ~A"
212 (var-name v) (var-type v))))
213 (p1propagate body))
214
215 (defun p1locals (c1form funs body labels)
216 (declare (ignore c1form labels))
217 (loop for f in funs
218 do (p1propagate-function f))
219 (p1propagate body))
220
221 (defun p1multiple-value-bind (c1form vars-list init-c1form body)
222 (declare (ignore c1form))
223 (let ((init-form-type (p1propagate init-c1form)))
224 (loop for v in vars-list
225 for type in (values-type-to-n-types init-form-type (length vars-list))
226 unless (or (global-var-p v)
227 (var-set-nodes v))
228 do (setf (var-type v) (type-and (var-type v) type)) and
229 do (prop-message "~&;;; Variable ~A assigned type ~A"
230 (var-name v) (var-type v)))
231 (p1propagate body)))
232
233 (defun p1multiple-value-setq (c1form vars-list value-c1form)
234 (declare (ignore c1form vars-list))
235 (p1propagate value-c1form))
236
237 (defun p1progn (c1form forms)
238 (declare (ignore c1form))
239 (p1propagate-list forms))
240
241 (defun p1compiler-typecase (c1form variable expressions)
242 (declare (ignore c1form))
243 (let ((var-type (var-type variable)))
244 (loop with output-type = t
245 for (a-type c1form) in expressions
246 for c1form-type = (p1propagate c1form)
247 when (or (member a-type '(t otherwise))
248 (subtypep var-type a-type *cmp-env*))
249 do (setf output-type c1form-type)
250 finally (return output-type))))
251
252 (defun p1checked-value (c1form type value let-form)
253 (declare (ignore c1form let-form))
254 (let ((value-type (p1propagate value))
255 ;;(alt-type (p1propagate let-form))
256 )
257 (if (subtypep value-type type *cmp-env*)
258 value-type
259 type)))
260
261 (defun p1progv (c1form variables values body)
262 (declare (ignore c1form))
263 (p1propagate variables)
264 (p1propagate values)
265 (p1propagate body))
266
267 (defun p1setq (c1form var value-c1form)
268 (declare (ignore c1form))
269 (let ((value-type (p1propagate value-c1form)))
270 (type-and (var-type var) (values-type-primary-type value-type))))
271
272 (defun p1psetq (c1form vars c1forms)
273 (declare (ignore c1form vars))
274 (loop for form in c1forms
275 do (p1propagate form))
276 'null)
277
278 (defun p1mv-prog1 (c1form form body)
279 (declare (ignore c1form))
280 (prog1 (p1propagate form)
281 (p1propagate-list body)))
282
283 (defvar *tagbody-depth* -1
284 "If n > 0, limit the number of passes to converge tagbody forms. If
285 -1, let the compiler do as many passes as it wishes. Complexity grows
286 as 2^*tagbody-limit* in the worst cases.")
287
288 (defun p1go (c1form tag-var return-type)
289 (declare (ignore c1form tag-var return-type))
290 t)
291
292 (defun p1tagbody (c1form tag-loc body)
293 (declare (ignore c1form tag-loc))
294 (prop-message "~&;;; P1TAGBODY-SIMPLE pass")
295 (loop for f in body do
296 (if (tag-p f)
297 (prop-message "~&;;; Label ~A found" (tag-name f))
298 (p1propagate f)))
299 'null)
300
301 (defun p1unwind-protect (c1form form body)
302 (declare (ignore c1form))
303 (let ((output-type (p1propagate form)))
304 (p1propagate body)
305 output-type))