rename <dynamic-wind> to <dynwind>
[bpt/guile.git] / module / language / tree-il / analyze.scm
1 ;;; TREE-IL -> GLIL compiler
2
3 ;; Copyright (C) 2001,2008,2009,2010 Free Software Foundation, Inc.
4
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;;; Code:
20
21 (define-module (language tree-il analyze)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-9)
24 #:use-module (srfi srfi-11)
25 #:use-module (ice-9 vlist)
26 #:use-module (system base syntax)
27 #:use-module (system base message)
28 #:use-module (system vm program)
29 #:use-module (language tree-il)
30 #:use-module (system base pmatch)
31 #:export (analyze-lexicals
32 analyze-tree
33 unused-variable-analysis
34 unused-toplevel-analysis
35 unbound-variable-analysis
36 arity-analysis))
37
38 ;; Allocation is the process of assigning storage locations for lexical
39 ;; variables. A lexical variable has a distinct "address", or storage
40 ;; location, for each procedure in which it is referenced.
41 ;;
42 ;; A variable is "local", i.e., allocated on the stack, if it is
43 ;; referenced from within the procedure that defined it. Otherwise it is
44 ;; a "closure" variable. For example:
45 ;;
46 ;; (lambda (a) a) ; a will be local
47 ;; `a' is local to the procedure.
48 ;;
49 ;; (lambda (a) (lambda () a))
50 ;; `a' is local to the outer procedure, but a closure variable with
51 ;; respect to the inner procedure.
52 ;;
53 ;; If a variable is ever assigned, it needs to be heap-allocated
54 ;; ("boxed"). This is so that closures and continuations capture the
55 ;; variable's identity, not just one of the values it may have over the
56 ;; course of program execution. If the variable is never assigned, there
57 ;; is no distinction between value and identity, so closing over its
58 ;; identity (whether through closures or continuations) can make a copy
59 ;; of its value instead.
60 ;;
61 ;; Local variables are stored on the stack within a procedure's call
62 ;; frame. Their index into the stack is determined from their linear
63 ;; postion within a procedure's binding path:
64 ;; (let (0 1)
65 ;; (let (2 3) ...)
66 ;; (let (2) ...))
67 ;; (let (2 3 4) ...))
68 ;; etc.
69 ;;
70 ;; This algorithm has the problem that variables are only allocated
71 ;; indices at the end of the binding path. If variables bound early in
72 ;; the path are not used in later portions of the path, their indices
73 ;; will not be recycled. This problem is particularly egregious in the
74 ;; expansion of `or':
75 ;;
76 ;; (or x y z)
77 ;; -> (let ((a x)) (if a a (let ((b y)) (if b b z))))
78 ;;
79 ;; As you can see, the `a' binding is only used in the ephemeral
80 ;; `consequent' clause of the first `if', but its index would be
81 ;; reserved for the whole of the `or' expansion. So we have a hack for
82 ;; this specific case. A proper solution would be some sort of liveness
83 ;; analysis, and not our linear allocation algorithm.
84 ;;
85 ;; Closure variables are captured when a closure is created, and stored in a
86 ;; vector inline to the closure object itself. Each closure variable has a
87 ;; unique index into that vector.
88 ;;
89 ;; There is one more complication. Procedures bound by <fix> may, in
90 ;; some cases, be rendered inline to their parent procedure. That is to
91 ;; say,
92 ;;
93 ;; (letrec ((lp (lambda () (lp)))) (lp))
94 ;; => (fix ((lp (lambda () (lp)))) (lp))
95 ;; => goto FIX-BODY; LP: goto LP; FIX-BODY: goto LP;
96 ;; ^ jump over the loop ^ the fixpoint lp ^ starting off the loop
97 ;;
98 ;; The upshot is that we don't have to allocate any space for the `lp'
99 ;; closure at all, as it can be rendered inline as a loop. So there is
100 ;; another kind of allocation, "label allocation", in which the
101 ;; procedure is simply a label, placed at the start of the lambda body.
102 ;; The label is the gensym under which the lambda expression is bound.
103 ;;
104 ;; The analyzer checks to see that the label is called with the correct
105 ;; number of arguments. Calls to labels compile to rename + goto.
106 ;; Lambda, the ultimate goto!
107 ;;
108 ;;
109 ;; The return value of `analyze-lexicals' is a hash table, the
110 ;; "allocation".
111 ;;
112 ;; The allocation maps gensyms -- recall that each lexically bound
113 ;; variable has a unique gensym -- to storage locations ("addresses").
114 ;; Since one gensym may have many storage locations, if it is referenced
115 ;; in many procedures, it is a two-level map.
116 ;;
117 ;; The allocation also stored information on how many local variables
118 ;; need to be allocated for each procedure, lexicals that have been
119 ;; translated into labels, and information on what free variables to
120 ;; capture from its lexical parent procedure.
121 ;;
122 ;; In addition, we have a conflation: while we're traversing the code,
123 ;; recording information to pass to the compiler, we take the
124 ;; opportunity to generate labels for each lambda-case clause, so that
125 ;; generated code can skip argument checks at runtime if they match at
126 ;; compile-time.
127 ;;
128 ;; Also, while we're a-traversing and an-allocating, we check prompt
129 ;; handlers to see if the "continuation" argument is used. If not, we
130 ;; mark the prompt as being "escape-only". This allows us to implement
131 ;; `catch' and `throw' using `prompt' and `control', but without causing
132 ;; a continuation to be reified. Heh heh.
133 ;;
134 ;; That is:
135 ;;
136 ;; sym -> {lambda -> address}
137 ;; lambda -> (labels . free-locs)
138 ;; lambda-case -> (gensym . nlocs)
139 ;; prompt -> escape-only?
140 ;;
141 ;; address ::= (local? boxed? . index)
142 ;; labels ::= ((sym . lambda) ...)
143 ;; free-locs ::= ((sym0 . address0) (sym1 . address1) ...)
144 ;; free variable addresses are relative to parent proc.
145
146 (define (make-hashq k v)
147 (let ((res (make-hash-table)))
148 (hashq-set! res k v)
149 res))
150
151 (define (analyze-lexicals x)
152 ;; bound-vars: lambda -> (sym ...)
153 ;; all identifiers bound within a lambda
154 (define bound-vars (make-hash-table))
155 ;; free-vars: lambda -> (sym ...)
156 ;; all identifiers referenced in a lambda, but not bound
157 ;; NB, this includes identifiers referenced by contained lambdas
158 (define free-vars (make-hash-table))
159 ;; assigned: sym -> #t
160 ;; variables that are assigned
161 (define assigned (make-hash-table))
162 ;; refcounts: sym -> count
163 ;; allows us to detect the or-expansion in O(1) time
164 (define refcounts (make-hash-table))
165 ;; labels: sym -> lambda
166 ;; for determining if fixed-point procedures can be rendered as
167 ;; labels.
168 (define labels (make-hash-table))
169
170 ;; returns variables referenced in expr
171 (define (analyze! x proc labels-in-proc tail? tail-call-args)
172 (define (step y) (analyze! y proc labels-in-proc #f #f))
173 (define (step-tail y) (analyze! y proc labels-in-proc tail? #f))
174 (define (step-tail-call y args) (analyze! y proc labels-in-proc #f
175 (and tail? args)))
176 (define (recur/labels x new-proc labels)
177 (analyze! x new-proc (append labels labels-in-proc) #t #f))
178 (define (recur x new-proc) (analyze! x new-proc '() tail? #f))
179 (record-case x
180 ((<application> proc args)
181 (apply lset-union eq? (step-tail-call proc args)
182 (map step args)))
183
184 ((<conditional> test consequent alternate)
185 (lset-union eq? (step test) (step-tail consequent) (step-tail alternate)))
186
187 ((<lexical-ref> gensym)
188 (hashq-set! refcounts gensym (1+ (hashq-ref refcounts gensym 0)))
189 (if (not (and tail-call-args
190 (memq gensym labels-in-proc)
191 (let ((p (hashq-ref labels gensym)))
192 (and p
193 (let lp ((c (lambda-body p)))
194 (and c (lambda-case? c)
195 (or
196 ;; for now prohibit optional &
197 ;; keyword arguments; can relax this
198 ;; restriction later
199 (and (= (length (lambda-case-req c))
200 (length tail-call-args))
201 (not (lambda-case-opt c))
202 (not (lambda-case-kw c))
203 (not (lambda-case-rest c)))
204 (lp (lambda-case-alternate c)))))))))
205 (hashq-set! labels gensym #f))
206 (list gensym))
207
208 ((<lexical-set> gensym exp)
209 (hashq-set! assigned gensym #t)
210 (hashq-set! labels gensym #f)
211 (lset-adjoin eq? (step exp) gensym))
212
213 ((<module-set> exp)
214 (step exp))
215
216 ((<toplevel-set> exp)
217 (step exp))
218
219 ((<toplevel-define> exp)
220 (step exp))
221
222 ((<sequence> exps)
223 (let lp ((exps exps) (ret '()))
224 (cond ((null? exps) '())
225 ((null? (cdr exps))
226 (lset-union eq? ret (step-tail (car exps))))
227 (else
228 (lp (cdr exps) (lset-union eq? ret (step (car exps))))))))
229
230 ((<lambda> body)
231 ;; order is important here
232 (hashq-set! bound-vars x '())
233 (let ((free (recur body x)))
234 (hashq-set! bound-vars x (reverse! (hashq-ref bound-vars x)))
235 (hashq-set! free-vars x free)
236 free))
237
238 ((<lambda-case> opt kw inits vars body alternate)
239 (hashq-set! bound-vars proc
240 (append (reverse vars) (hashq-ref bound-vars proc)))
241 (lset-union
242 eq?
243 (lset-difference eq?
244 (lset-union eq?
245 (apply lset-union eq? (map step inits))
246 (step-tail body))
247 vars)
248 (if alternate (step-tail alternate) '())))
249
250 ((<let> vars vals body)
251 (hashq-set! bound-vars proc
252 (append (reverse vars) (hashq-ref bound-vars proc)))
253 (lset-difference eq?
254 (apply lset-union eq? (step-tail body) (map step vals))
255 vars))
256
257 ((<letrec> vars vals body)
258 (hashq-set! bound-vars proc
259 (append (reverse vars) (hashq-ref bound-vars proc)))
260 (for-each (lambda (sym) (hashq-set! assigned sym #t)) vars)
261 (lset-difference eq?
262 (apply lset-union eq? (step-tail body) (map step vals))
263 vars))
264
265 ((<fix> vars vals body)
266 ;; Try to allocate these procedures as labels.
267 (for-each (lambda (sym val) (hashq-set! labels sym val))
268 vars vals)
269 (hashq-set! bound-vars proc
270 (append (reverse vars) (hashq-ref bound-vars proc)))
271 ;; Step into subexpressions.
272 (let* ((var-refs
273 (map
274 ;; Since we're trying to label-allocate the lambda,
275 ;; pretend it's not a closure, and just recurse into its
276 ;; body directly. (Otherwise, recursing on a closure
277 ;; that references one of the fix's bound vars would
278 ;; prevent label allocation.)
279 (lambda (x)
280 (record-case x
281 ((<lambda> body)
282 ;; just like the closure case, except here we use
283 ;; recur/labels instead of recur
284 (hashq-set! bound-vars x '())
285 (let ((free (recur/labels body x vars)))
286 (hashq-set! bound-vars x (reverse! (hashq-ref bound-vars x)))
287 (hashq-set! free-vars x free)
288 free))))
289 vals))
290 (vars-with-refs (map cons vars var-refs))
291 (body-refs (recur/labels body proc vars)))
292 (define (delabel-dependents! sym)
293 (let ((refs (assq-ref vars-with-refs sym)))
294 (if refs
295 (for-each (lambda (sym)
296 (if (hashq-ref labels sym)
297 (begin
298 (hashq-set! labels sym #f)
299 (delabel-dependents! sym))))
300 refs))))
301 ;; Stepping into the lambdas and the body might have made some
302 ;; procedures not label-allocatable -- which might have
303 ;; knock-on effects. For example:
304 ;; (fix ((a (lambda () (b)))
305 ;; (b (lambda () a)))
306 ;; (a))
307 ;; As far as `a' is concerned, both `a' and `b' are
308 ;; label-allocatable. But `b' references `a' not in a proc-tail
309 ;; position, which makes `a' not label-allocatable. The
310 ;; knock-on effect is that, when back-propagating this
311 ;; information to `a', `b' will also become not
312 ;; label-allocatable, as it is referenced within `a', which is
313 ;; allocated as a closure. This is a transitive relationship.
314 (for-each (lambda (sym)
315 (if (not (hashq-ref labels sym))
316 (delabel-dependents! sym)))
317 vars)
318 ;; Now lift bound variables with label-allocated lambdas to the
319 ;; parent procedure.
320 (for-each
321 (lambda (sym val)
322 (if (hashq-ref labels sym)
323 ;; Remove traces of the label-bound lambda. The free
324 ;; vars will propagate up via the return val.
325 (begin
326 (hashq-set! bound-vars proc
327 (append (hashq-ref bound-vars val)
328 (hashq-ref bound-vars proc)))
329 (hashq-remove! bound-vars val)
330 (hashq-remove! free-vars val))))
331 vars vals)
332 (lset-difference eq?
333 (apply lset-union eq? body-refs var-refs)
334 vars)))
335
336 ((<let-values> exp body)
337 (lset-union eq? (step exp) (step body)))
338
339 ((<dynwind> body winder unwinder)
340 (lset-union eq? (step body) (step winder) (step unwinder)))
341
342 ((<prompt> tag body handler pre-unwind-handler)
343 (lset-union eq? (step tag) (step handler)
344 (if pre-unwind-handler (step pre-unwind-handler) '())))
345
346 ((<control> tag type args)
347 (apply lset-union eq? (step tag) (map step args)))
348
349 (else '())))
350
351 ;; allocation: sym -> {lambda -> address}
352 ;; lambda -> (nlocs labels . free-locs)
353 (define allocation (make-hash-table))
354
355 (define (allocate! x proc n)
356 (define (recur y) (allocate! y proc n))
357 (record-case x
358 ((<application> proc args)
359 (apply max (recur proc) (map recur args)))
360
361 ((<conditional> test consequent alternate)
362 (max (recur test) (recur consequent) (recur alternate)))
363
364 ((<lexical-set> exp)
365 (recur exp))
366
367 ((<module-set> exp)
368 (recur exp))
369
370 ((<toplevel-set> exp)
371 (recur exp))
372
373 ((<toplevel-define> exp)
374 (recur exp))
375
376 ((<sequence> exps)
377 (apply max (map recur exps)))
378
379 ((<lambda> body)
380 ;; allocate closure vars in order
381 (let lp ((c (hashq-ref free-vars x)) (n 0))
382 (if (pair? c)
383 (begin
384 (hashq-set! (hashq-ref allocation (car c))
385 x
386 `(#f ,(hashq-ref assigned (car c)) . ,n))
387 (lp (cdr c) (1+ n)))))
388
389 (let ((nlocs (allocate! body x 0))
390 (free-addresses
391 (map (lambda (v)
392 (hashq-ref (hashq-ref allocation v) proc))
393 (hashq-ref free-vars x)))
394 (labels (filter cdr
395 (map (lambda (sym)
396 (cons sym (hashq-ref labels sym)))
397 (hashq-ref bound-vars x)))))
398 ;; set procedure allocations
399 (hashq-set! allocation x (cons labels free-addresses)))
400 n)
401
402 ((<lambda-case> opt kw inits vars body alternate)
403 (max
404 (let lp ((vars vars) (n n))
405 (if (null? vars)
406 (let ((nlocs (apply
407 max
408 (allocate! body proc n)
409 ;; inits not logically at the end, but they
410 ;; are the list...
411 (map (lambda (x) (allocate! x proc n)) inits))))
412 ;; label and nlocs for the case
413 (hashq-set! allocation x (cons (gensym ":LCASE") nlocs))
414 nlocs)
415 (begin
416 (hashq-set! allocation (car vars)
417 (make-hashq
418 proc `(#t ,(hashq-ref assigned (car vars)) . ,n)))
419 (lp (cdr vars) (1+ n)))))
420 (if alternate (allocate! alternate proc n) n)))
421
422 ((<let> vars vals body)
423 (let ((nmax (apply max (map recur vals))))
424 (cond
425 ;; the `or' hack
426 ((and (conditional? body)
427 (= (length vars) 1)
428 (let ((v (car vars)))
429 (and (not (hashq-ref assigned v))
430 (= (hashq-ref refcounts v 0) 2)
431 (lexical-ref? (conditional-test body))
432 (eq? (lexical-ref-gensym (conditional-test body)) v)
433 (lexical-ref? (conditional-consequent body))
434 (eq? (lexical-ref-gensym (conditional-consequent body)) v))))
435 (hashq-set! allocation (car vars)
436 (make-hashq proc `(#t #f . ,n)))
437 ;; the 1+ for this var
438 (max nmax (1+ n) (allocate! (conditional-alternate body) proc n)))
439 (else
440 (let lp ((vars vars) (n n))
441 (if (null? vars)
442 (max nmax (allocate! body proc n))
443 (let ((v (car vars)))
444 (hashq-set!
445 allocation v
446 (make-hashq proc
447 `(#t ,(hashq-ref assigned v) . ,n)))
448 (lp (cdr vars) (1+ n)))))))))
449
450 ((<letrec> vars vals body)
451 (let lp ((vars vars) (n n))
452 (if (null? vars)
453 (let ((nmax (apply max
454 (map (lambda (x)
455 (allocate! x proc n))
456 vals))))
457 (max nmax (allocate! body proc n)))
458 (let ((v (car vars)))
459 (hashq-set!
460 allocation v
461 (make-hashq proc
462 `(#t ,(hashq-ref assigned v) . ,n)))
463 (lp (cdr vars) (1+ n))))))
464
465 ((<fix> vars vals body)
466 (let lp ((in vars) (n n))
467 (if (null? in)
468 (let lp ((vars vars) (vals vals) (nmax n))
469 (cond
470 ((null? vars)
471 (max nmax (allocate! body proc n)))
472 ((hashq-ref labels (car vars))
473 ;; allocate lambda body inline to proc
474 (lp (cdr vars)
475 (cdr vals)
476 (record-case (car vals)
477 ((<lambda> body)
478 (max nmax (allocate! body proc n))))))
479 (else
480 ;; allocate closure
481 (lp (cdr vars)
482 (cdr vals)
483 (max nmax (allocate! (car vals) proc n))))))
484
485 (let ((v (car in)))
486 (cond
487 ((hashq-ref assigned v)
488 (error "fixpoint procedures may not be assigned" x))
489 ((hashq-ref labels v)
490 ;; no binding, it's a label
491 (lp (cdr in) n))
492 (else
493 ;; allocate closure binding
494 (hashq-set! allocation v (make-hashq proc `(#t #f . ,n)))
495 (lp (cdr in) (1+ n))))))))
496
497 ((<let-values> exp body)
498 (max (recur exp) (recur body)))
499
500 ((<dynwind> body winder unwinder)
501 (max (recur body) (recur winder) (recur unwinder)))
502
503 ((<prompt> tag body handler pre-unwind-handler)
504 (let ((cont-var (and (lambda-case? handler)
505 (pair? (lambda-case-vars handler))
506 (car (lambda-case-vars handler)))))
507 (hashq-set! allocation x
508 (and cont-var (zero? (hashq-ref refcounts cont-var 0))))
509 (max (recur tag) (recur body) (recur handler)
510 (if pre-unwind-handler (recur pre-unwind-handler) 0))))
511
512 ((<control> tag type args)
513 (apply max (recur tag) (map recur args)))
514
515 (else n)))
516
517 (analyze! x #f '() #t #f)
518 (allocate! x #f 0)
519
520 allocation)
521
522 \f
523 ;;;
524 ;;; Tree analyses for warnings.
525 ;;;
526
527 (define-record-type <tree-analysis>
528 (make-tree-analysis leaf down up post init)
529 tree-analysis?
530 (leaf tree-analysis-leaf) ;; (lambda (x result env locs) ...)
531 (down tree-analysis-down) ;; (lambda (x result env locs) ...)
532 (up tree-analysis-up) ;; (lambda (x result env locs) ...)
533 (post tree-analysis-post) ;; (lambda (result env) ...)
534 (init tree-analysis-init)) ;; arbitrary value
535
536 (define (analyze-tree analyses tree env)
537 "Run all tree analyses listed in ANALYSES on TREE for ENV, using
538 `tree-il-fold'. Return TREE. The leaf/down/up procedures of each analysis are
539 passed a ``location stack', which is the stack of `tree-il-src' values for each
540 parent tree (a list); it can be used to approximate source location when
541 accurate information is missing from a given `tree-il' element."
542
543 (define (traverse proc update-locs)
544 ;; Return a tree traversing procedure that returns a list of analysis
545 ;; results prepended by the location stack.
546 (lambda (x results)
547 (let ((locs (update-locs x (car results))))
548 (cons locs ;; the location stack
549 (map (lambda (analysis result)
550 ((proc analysis) x result env locs))
551 analyses
552 (cdr results))))))
553
554 ;; Keeping/extending/shrinking the location stack.
555 (define (keep-locs x locs) locs)
556 (define (extend-locs x locs) (cons (tree-il-src x) locs))
557 (define (shrink-locs x locs) (cdr locs))
558
559 (let ((results
560 (tree-il-fold (traverse tree-analysis-leaf keep-locs)
561 (traverse tree-analysis-down extend-locs)
562 (traverse tree-analysis-up shrink-locs)
563 (cons '() ;; empty location stack
564 (map tree-analysis-init analyses))
565 tree)))
566
567 (for-each (lambda (analysis result)
568 ((tree-analysis-post analysis) result env))
569 analyses
570 (cdr results)))
571
572 tree)
573
574 \f
575 ;;;
576 ;;; Unused variable analysis.
577 ;;;
578
579 ;; <binding-info> records are used during tree traversals in
580 ;; `unused-variable-analysis'. They contain a list of the local vars
581 ;; currently in scope, and a list of locals vars that have been referenced.
582 (define-record-type <binding-info>
583 (make-binding-info vars refs)
584 binding-info?
585 (vars binding-info-vars) ;; ((GENSYM NAME LOCATION) ...)
586 (refs binding-info-refs)) ;; (GENSYM ...)
587
588 (define unused-variable-analysis
589 ;; Report unused variables in the given tree.
590 (make-tree-analysis
591 (lambda (x info env locs)
592 ;; X is a leaf: extend INFO's refs accordingly.
593 (let ((refs (binding-info-refs info))
594 (vars (binding-info-vars info)))
595 (record-case x
596 ((<lexical-ref> gensym)
597 (make-binding-info vars (vhash-consq gensym #t refs)))
598 (else info))))
599
600 (lambda (x info env locs)
601 ;; Going down into X: extend INFO's variable list
602 ;; accordingly.
603 (let ((refs (binding-info-refs info))
604 (vars (binding-info-vars info))
605 (src (tree-il-src x)))
606 (define (extend inner-vars inner-names)
607 (fold (lambda (var name vars)
608 (vhash-consq var (list name src) vars))
609 vars
610 inner-vars
611 inner-names))
612
613 (record-case x
614 ((<lexical-set> gensym)
615 (make-binding-info vars (vhash-consq gensym #t refs)))
616 ((<lambda-case> req opt inits rest kw vars)
617 (let ((names `(,@req
618 ,@(or opt '())
619 ,@(if rest (list rest) '())
620 ,@(if kw (map cadr (cdr kw)) '()))))
621 (make-binding-info (extend vars names) refs)))
622 ((<let> vars names)
623 (make-binding-info (extend vars names) refs))
624 ((<letrec> vars names)
625 (make-binding-info (extend vars names) refs))
626 ((<fix> vars names)
627 (make-binding-info (extend vars names) refs))
628 (else info))))
629
630 (lambda (x info env locs)
631 ;; Leaving X's scope: shrink INFO's variable list
632 ;; accordingly and reported unused nested variables.
633 (let ((refs (binding-info-refs info))
634 (vars (binding-info-vars info)))
635 (define (shrink inner-vars refs)
636 (vlist-for-each
637 (lambda (var)
638 (let ((gensym (car var)))
639 ;; Don't report lambda parameters as unused.
640 (if (and (memq gensym inner-vars)
641 (not (vhash-assq gensym refs))
642 (not (lambda-case? x)))
643 (let ((name (cadr var))
644 ;; We can get approximate source location by going up
645 ;; the LOCS location stack.
646 (loc (or (caddr var)
647 (find pair? locs))))
648 (warning 'unused-variable loc name)))))
649 vars)
650 (vlist-drop vars (length inner-vars)))
651
652 ;; For simplicity, we leave REFS untouched, i.e., with
653 ;; names of variables that are now going out of scope.
654 ;; It doesn't hurt as these are unique names, it just
655 ;; makes REFS unnecessarily fat.
656 (record-case x
657 ((<lambda-case> vars)
658 (make-binding-info (shrink vars refs) refs))
659 ((<let> vars)
660 (make-binding-info (shrink vars refs) refs))
661 ((<letrec> vars)
662 (make-binding-info (shrink vars refs) refs))
663 ((<fix> vars)
664 (make-binding-info (shrink vars refs) refs))
665 (else info))))
666
667 (lambda (result env) #t)
668 (make-binding-info vlist-null vlist-null)))
669
670 \f
671 ;;;
672 ;;; Unused top-level variable analysis.
673 ;;;
674
675 ;; <reference-graph> record top-level definitions that are made, references to
676 ;; top-level definitions and their context (the top-level definition in which
677 ;; the reference appears), as well as the current context (the top-level
678 ;; definition we're currently in). The second part (`refs' below) is
679 ;; effectively a graph from which we can determine unused top-level definitions.
680 (define-record-type <reference-graph>
681 (make-reference-graph refs defs toplevel-context)
682 reference-graph?
683 (defs reference-graph-defs) ;; ((NAME . LOC) ...)
684 (refs reference-graph-refs) ;; ((REF-CONTEXT REF ...) ...)
685 (toplevel-context reference-graph-toplevel-context)) ;; NAME | #f
686
687 (define (graph-reachable-nodes root refs reachable)
688 ;; Add to REACHABLE the nodes reachable from ROOT in graph REFS. REFS is a
689 ;; vhash mapping nodes to the list of their children: for instance,
690 ;; ((A -> (B C)) (B -> (A)) (C -> ())) corresponds to
691 ;;
692 ;; ,-------.
693 ;; v |
694 ;; A ----> B
695 ;; |
696 ;; v
697 ;; C
698 ;;
699 ;; REACHABLE is a vhash of nodes known to be otherwise reachable.
700
701 (let loop ((root root)
702 (path vlist-null)
703 (result reachable))
704 (if (or (vhash-assq root path)
705 (vhash-assq root result))
706 result
707 (let* ((children (or (and=> (vhash-assq root refs) cdr) '()))
708 (path (vhash-consq root #t path))
709 (result (fold (lambda (kid result)
710 (loop kid path result))
711 result
712 children)))
713 (fold (lambda (kid result)
714 (vhash-consq kid #t result))
715 result
716 children)))))
717
718 (define (graph-reachable-nodes* roots refs)
719 ;; Return the list of nodes in REFS reachable from the nodes listed in ROOTS.
720 (vlist-fold (lambda (root+true result)
721 (let* ((root (car root+true))
722 (reachable (graph-reachable-nodes root refs result)))
723 (vhash-consq root #t reachable)))
724 vlist-null
725 roots))
726
727 (define (partition* pred vhash)
728 ;; Partition VHASH according to PRED. Return the two resulting vhashes.
729 (let ((result
730 (vlist-fold (lambda (k+v result)
731 (let ((k (car k+v))
732 (v (cdr k+v))
733 (r1 (car result))
734 (r2 (cdr result)))
735 (if (pred k)
736 (cons (vhash-consq k v r1) r2)
737 (cons r1 (vhash-consq k v r2)))))
738 (cons vlist-null vlist-null)
739 vhash)))
740 (values (car result) (cdr result))))
741
742 (define unused-toplevel-analysis
743 ;; Report unused top-level definitions that are not exported.
744 (let ((add-ref-from-context
745 (lambda (graph name)
746 ;; Add an edge CTX -> NAME in GRAPH.
747 (let* ((refs (reference-graph-refs graph))
748 (defs (reference-graph-defs graph))
749 (ctx (reference-graph-toplevel-context graph))
750 (ctx-refs (or (and=> (vhash-assq ctx refs) cdr) '())))
751 (make-reference-graph (vhash-consq ctx (cons name ctx-refs) refs)
752 defs ctx)))))
753 (define (macro-variable? name env)
754 (and (module? env)
755 (let ((var (module-variable env name)))
756 (and var (variable-bound? var)
757 (macro? (variable-ref var))))))
758
759 (make-tree-analysis
760 (lambda (x graph env locs)
761 ;; X is a leaf.
762 (let ((ctx (reference-graph-toplevel-context graph)))
763 (record-case x
764 ((<toplevel-ref> name src)
765 (add-ref-from-context graph name))
766 (else graph))))
767
768 (lambda (x graph env locs)
769 ;; Going down into X.
770 (let ((ctx (reference-graph-toplevel-context graph))
771 (refs (reference-graph-refs graph))
772 (defs (reference-graph-defs graph)))
773 (record-case x
774 ((<toplevel-define> name src)
775 (let ((refs refs)
776 (defs (vhash-consq name (or src (find pair? locs))
777 defs)))
778 (make-reference-graph refs defs name)))
779 ((<toplevel-set> name src)
780 (add-ref-from-context graph name))
781 (else graph))))
782
783 (lambda (x graph env locs)
784 ;; Leaving X's scope.
785 (record-case x
786 ((<toplevel-define>)
787 (let ((refs (reference-graph-refs graph))
788 (defs (reference-graph-defs graph)))
789 (make-reference-graph refs defs #f)))
790 (else graph)))
791
792 (lambda (graph env)
793 ;; Process the resulting reference graph: determine all private definitions
794 ;; not reachable from any public definition. Macros
795 ;; (syntax-transformers), which are globally bound, never considered
796 ;; unused since we can't tell whether a macro is actually used; in
797 ;; addition, macros are considered roots of the graph since they may use
798 ;; private bindings. FIXME: The `make-syntax-transformer' calls don't
799 ;; contain any literal `toplevel-ref' of the global bindings they use so
800 ;; this strategy fails.
801 (define (exported? name)
802 (if (module? env)
803 (module-variable (module-public-interface env) name)
804 #t))
805
806 (let-values (((public-defs private-defs)
807 (partition* (lambda (name)
808 (or (exported? name)
809 (macro-variable? name env)))
810 (reference-graph-defs graph))))
811 (let* ((roots (vhash-consq #f #t public-defs))
812 (refs (reference-graph-refs graph))
813 (reachable (graph-reachable-nodes* roots refs))
814 (unused (vlist-filter (lambda (name+src)
815 (not (vhash-assq (car name+src)
816 reachable)))
817 private-defs)))
818 (vlist-for-each (lambda (name+loc)
819 (let ((name (car name+loc))
820 (loc (cdr name+loc)))
821 (warning 'unused-toplevel loc name)))
822 unused))))
823
824 (make-reference-graph vlist-null vlist-null #f))))
825
826 \f
827 ;;;
828 ;;; Unbound variable analysis.
829 ;;;
830
831 ;; <toplevel-info> records are used during tree traversal in search of
832 ;; possibly unbound variable. They contain a list of references to
833 ;; potentially unbound top-level variables, and a list of the top-level
834 ;; defines that have been encountered.
835 (define-record-type <toplevel-info>
836 (make-toplevel-info refs defs)
837 toplevel-info?
838 (refs toplevel-info-refs) ;; ((VARIABLE-NAME . LOCATION) ...)
839 (defs toplevel-info-defs)) ;; (VARIABLE-NAME ...)
840
841 (define (goops-toplevel-definition proc args env)
842 ;; If application of PROC to ARGS is a GOOPS top-level definition, return
843 ;; the name of the variable being defined; otherwise return #f. This
844 ;; assumes knowledge of the current implementation of `define-class' et al.
845 (define (toplevel-define-arg args)
846 (and (pair? args) (pair? (cdr args)) (null? (cddr args))
847 (record-case (car args)
848 ((<const> exp)
849 (and (symbol? exp) exp))
850 (else #f))))
851
852 (record-case proc
853 ((<module-ref> mod public? name)
854 (and (equal? mod '(oop goops))
855 (not public?)
856 (eq? name 'toplevel-define!)
857 (toplevel-define-arg args)))
858 ((<toplevel-ref> name)
859 ;; This may be the result of expanding one of the GOOPS macros within
860 ;; `oop/goops.scm'.
861 (and (eq? name 'toplevel-define!)
862 (eq? env (resolve-module '(oop goops)))
863 (toplevel-define-arg args)))
864 (else #f)))
865
866 (define unbound-variable-analysis
867 ;; Report possibly unbound variables in the given tree.
868 (make-tree-analysis
869 (lambda (x info env locs)
870 ;; X is a leaf: extend INFO's refs accordingly.
871 (let ((refs (toplevel-info-refs info))
872 (defs (toplevel-info-defs info)))
873 (define (bound? name)
874 (or (and (module? env)
875 (module-variable env name))
876 (vhash-assq name defs)))
877
878 (record-case x
879 ((<toplevel-ref> name src)
880 (if (bound? name)
881 info
882 (let ((src (or src (find pair? locs))))
883 (make-toplevel-info (vhash-consq name src refs)
884 defs))))
885 (else info))))
886
887 (lambda (x info env locs)
888 ;; Going down into X.
889 (let* ((refs (toplevel-info-refs info))
890 (defs (toplevel-info-defs info))
891 (src (tree-il-src x)))
892 (define (bound? name)
893 (or (and (module? env)
894 (module-variable env name))
895 (vhash-assq name defs)))
896
897 (record-case x
898 ((<toplevel-set> name src)
899 (if (bound? name)
900 (make-toplevel-info refs defs)
901 (let ((src (find pair? locs)))
902 (make-toplevel-info (vhash-consq name src refs)
903 defs))))
904 ((<toplevel-define> name)
905 (make-toplevel-info (vhash-delete name refs eq?)
906 (vhash-consq name #t defs)))
907
908 ((<application> proc args)
909 ;; Check for a dynamic top-level definition, as is
910 ;; done by code expanded from GOOPS macros.
911 (let ((name (goops-toplevel-definition proc args
912 env)))
913 (if (symbol? name)
914 (make-toplevel-info (vhash-delete name refs
915 eq?)
916 (vhash-consq name #t defs))
917 (make-toplevel-info refs defs))))
918 (else
919 (make-toplevel-info refs defs)))))
920
921 (lambda (x info env locs)
922 ;; Leaving X's scope.
923 info)
924
925 (lambda (toplevel env)
926 ;; Post-process the result.
927 (vlist-for-each (lambda (name+loc)
928 (let ((name (car name+loc))
929 (loc (cdr name+loc)))
930 (warning 'unbound-variable loc name)))
931 (vlist-reverse (toplevel-info-refs toplevel))))
932
933 (make-toplevel-info vlist-null vlist-null)))
934
935 \f
936 ;;;
937 ;;; Arity analysis.
938 ;;;
939
940 ;; <arity-info> records contain information about lexical definitions of
941 ;; procedures currently in scope, top-level procedure definitions that have
942 ;; been encountered, and calls to top-level procedures that have been
943 ;; encountered.
944 (define-record-type <arity-info>
945 (make-arity-info toplevel-calls lexical-lambdas toplevel-lambdas)
946 arity-info?
947 (toplevel-calls toplevel-procedure-calls) ;; ((NAME . APPLICATION) ...)
948 (lexical-lambdas lexical-lambdas) ;; ((GENSYM . DEFINITION) ...)
949 (toplevel-lambdas toplevel-lambdas)) ;; ((NAME . DEFINITION) ...)
950
951 (define (validate-arity proc application lexical?)
952 ;; Validate the argument count of APPLICATION, a tree-il application of
953 ;; PROC, emitting a warning in case of argument count mismatch.
954
955 (define (filter-keyword-args keywords allow-other-keys? args)
956 ;; Filter keyword arguments from ARGS and return the resulting list.
957 ;; KEYWORDS is the list of allowed keywords, and ALLOW-OTHER-KEYS?
958 ;; specified whethere keywords not listed in KEYWORDS are allowed.
959 (let loop ((args args)
960 (result '()))
961 (if (null? args)
962 (reverse result)
963 (let ((arg (car args)))
964 (if (and (const? arg)
965 (or (memq (const-exp arg) keywords)
966 (and allow-other-keys?
967 (keyword? (const-exp arg)))))
968 (loop (if (pair? (cdr args))
969 (cddr args)
970 '())
971 result)
972 (loop (cdr args)
973 (cons arg result)))))))
974
975 (define (arities proc)
976 ;; Return the arities of PROC, which can be either a tree-il or a
977 ;; procedure.
978 (define (len x)
979 (or (and (or (null? x) (pair? x))
980 (length x))
981 0))
982 (cond ((program? proc)
983 (values (program-name proc)
984 (map (lambda (a)
985 (list (arity:nreq a) (arity:nopt a) (arity:rest? a)
986 (map car (arity:kw a))
987 (arity:allow-other-keys? a)))
988 (program-arities proc))))
989 ((procedure? proc)
990 (let ((arity (procedure-property proc 'arity)))
991 (values (procedure-name proc)
992 (list (list (car arity) (cadr arity) (caddr arity)
993 #f #f)))))
994 (else
995 (let loop ((name #f)
996 (proc proc)
997 (arities '()))
998 (if (not proc)
999 (values name (reverse arities))
1000 (record-case proc
1001 ((<lambda-case> req opt rest kw alternate)
1002 (loop name alternate
1003 (cons (list (len req) (len opt) rest
1004 (and (pair? kw) (map car (cdr kw)))
1005 (and (pair? kw) (car kw)))
1006 arities)))
1007 ((<lambda> meta body)
1008 (loop (assoc-ref meta 'name) body arities))
1009 (else
1010 (values #f #f))))))))
1011
1012 (let ((args (application-args application))
1013 (src (tree-il-src application)))
1014 (call-with-values (lambda () (arities proc))
1015 (lambda (name arities)
1016 (define matches?
1017 (find (lambda (arity)
1018 (pmatch arity
1019 ((,req ,opt ,rest? ,kw ,aok?)
1020 (let ((args (if (pair? kw)
1021 (filter-keyword-args kw aok? args)
1022 args)))
1023 (if (and req opt)
1024 (let ((count (length args)))
1025 (and (>= count req)
1026 (or rest?
1027 (<= count (+ req opt)))))
1028 #t)))
1029 (else #t)))
1030 arities))
1031
1032 (if (not matches?)
1033 (warning 'arity-mismatch src
1034 (or name (with-output-to-string (lambda () (write proc))))
1035 lexical?)))))
1036 #t)
1037
1038 (define arity-analysis
1039 ;; Report arity mismatches in the given tree.
1040 (make-tree-analysis
1041 (lambda (x info env locs)
1042 ;; X is a leaf.
1043 info)
1044 (lambda (x info env locs)
1045 ;; Down into X.
1046 (define (extend lexical-name val info)
1047 ;; If VAL is a lambda, add NAME to the lexical-lambdas of INFO.
1048 (let ((toplevel-calls (toplevel-procedure-calls info))
1049 (lexical-lambdas (lexical-lambdas info))
1050 (toplevel-lambdas (toplevel-lambdas info)))
1051 (record-case val
1052 ((<lambda> body)
1053 (make-arity-info toplevel-calls
1054 (vhash-consq lexical-name val
1055 lexical-lambdas)
1056 toplevel-lambdas))
1057 ((<lexical-ref> gensym)
1058 ;; lexical alias
1059 (let ((val* (vhash-assq gensym lexical-lambdas)))
1060 (if (pair? val*)
1061 (extend lexical-name (cdr val*) info)
1062 info)))
1063 ((<toplevel-ref> name)
1064 ;; top-level alias
1065 (make-arity-info toplevel-calls
1066 (vhash-consq lexical-name val
1067 lexical-lambdas)
1068 toplevel-lambdas))
1069 (else info))))
1070
1071 (let ((toplevel-calls (toplevel-procedure-calls info))
1072 (lexical-lambdas (lexical-lambdas info))
1073 (toplevel-lambdas (toplevel-lambdas info)))
1074
1075 (record-case x
1076 ((<toplevel-define> name exp)
1077 (record-case exp
1078 ((<lambda> body)
1079 (make-arity-info toplevel-calls
1080 lexical-lambdas
1081 (vhash-consq name exp toplevel-lambdas)))
1082 ((<toplevel-ref> name)
1083 ;; alias for another toplevel
1084 (let ((proc (vhash-assq name toplevel-lambdas)))
1085 (make-arity-info toplevel-calls
1086 lexical-lambdas
1087 (vhash-consq (toplevel-define-name x)
1088 (if (pair? proc)
1089 (cdr proc)
1090 exp)
1091 toplevel-lambdas))))
1092 (else info)))
1093 ((<let> vars vals)
1094 (fold extend info vars vals))
1095 ((<letrec> vars vals)
1096 (fold extend info vars vals))
1097 ((<fix> vars vals)
1098 (fold extend info vars vals))
1099
1100 ((<application> proc args src)
1101 (record-case proc
1102 ((<lambda> body)
1103 (validate-arity proc x #t)
1104 info)
1105 ((<toplevel-ref> name)
1106 (make-arity-info (vhash-consq name x toplevel-calls)
1107 lexical-lambdas
1108 toplevel-lambdas))
1109 ((<lexical-ref> gensym)
1110 (let ((proc (vhash-assq gensym lexical-lambdas)))
1111 (if (pair? proc)
1112 (record-case (cdr proc)
1113 ((<toplevel-ref> name)
1114 ;; alias to toplevel
1115 (make-arity-info (vhash-consq name x toplevel-calls)
1116 lexical-lambdas
1117 toplevel-lambdas))
1118 (else
1119 (validate-arity (cdr proc) x #t)
1120 info))
1121
1122 ;; If GENSYM wasn't found, it may be because it's an
1123 ;; argument of the procedure being compiled.
1124 info)))
1125 (else info)))
1126 (else info))))
1127
1128 (lambda (x info env locs)
1129 ;; Up from X.
1130 (define (shrink name val info)
1131 ;; Remove NAME from the lexical-lambdas of INFO.
1132 (let ((toplevel-calls (toplevel-procedure-calls info))
1133 (lexical-lambdas (lexical-lambdas info))
1134 (toplevel-lambdas (toplevel-lambdas info)))
1135 (make-arity-info toplevel-calls
1136 (if (vhash-assq name lexical-lambdas)
1137 (vlist-tail lexical-lambdas)
1138 lexical-lambdas)
1139 toplevel-lambdas)))
1140
1141 (let ((toplevel-calls (toplevel-procedure-calls info))
1142 (lexical-lambdas (lexical-lambdas info))
1143 (toplevel-lambdas (toplevel-lambdas info)))
1144 (record-case x
1145 ((<let> vars vals)
1146 (fold shrink info vars vals))
1147 ((<letrec> vars vals)
1148 (fold shrink info vars vals))
1149 ((<fix> vars vals)
1150 (fold shrink info vars vals))
1151
1152 (else info))))
1153
1154 (lambda (result env)
1155 ;; Post-processing: check all top-level procedure calls that have been
1156 ;; encountered.
1157 (let ((toplevel-calls (toplevel-procedure-calls result))
1158 (toplevel-lambdas (toplevel-lambdas result)))
1159 (vlist-for-each
1160 (lambda (name+application)
1161 (let* ((name (car name+application))
1162 (application (cdr name+application))
1163 (proc
1164 (or (and=> (vhash-assq name toplevel-lambdas) cdr)
1165 (and (module? env)
1166 (false-if-exception
1167 (module-ref env name)))))
1168 (proc*
1169 ;; handle toplevel aliases
1170 (if (toplevel-ref? proc)
1171 (let ((name (toplevel-ref-name proc)))
1172 (and (module? env)
1173 (false-if-exception
1174 (module-ref env name))))
1175 proc)))
1176 (if (or (lambda? proc*) (procedure? proc*))
1177 (validate-arity proc* application (lambda? proc*)))))
1178 toplevel-calls)))
1179
1180 (make-arity-info vlist-null vlist-null vlist-null)))