rename <control> to <abort>
[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 ((<dynlet> fluids vals body)
343 (apply lset-union eq? (step body) (map step (append fluids vals))))
344
345 ((<prompt> tag body handler)
346 (lset-union eq? (step tag) (step handler)))
347
348 ((<abort> tag args)
349 (apply lset-union eq? (step tag) (map step args)))
350
351 (else '())))
352
353 ;; allocation: sym -> {lambda -> address}
354 ;; lambda -> (nlocs labels . free-locs)
355 (define allocation (make-hash-table))
356
357 (define (allocate! x proc n)
358 (define (recur y) (allocate! y proc n))
359 (record-case x
360 ((<application> proc args)
361 (apply max (recur proc) (map recur args)))
362
363 ((<conditional> test consequent alternate)
364 (max (recur test) (recur consequent) (recur alternate)))
365
366 ((<lexical-set> exp)
367 (recur exp))
368
369 ((<module-set> exp)
370 (recur exp))
371
372 ((<toplevel-set> exp)
373 (recur exp))
374
375 ((<toplevel-define> exp)
376 (recur exp))
377
378 ((<sequence> exps)
379 (apply max (map recur exps)))
380
381 ((<lambda> body)
382 ;; allocate closure vars in order
383 (let lp ((c (hashq-ref free-vars x)) (n 0))
384 (if (pair? c)
385 (begin
386 (hashq-set! (hashq-ref allocation (car c))
387 x
388 `(#f ,(hashq-ref assigned (car c)) . ,n))
389 (lp (cdr c) (1+ n)))))
390
391 (let ((nlocs (allocate! body x 0))
392 (free-addresses
393 (map (lambda (v)
394 (hashq-ref (hashq-ref allocation v) proc))
395 (hashq-ref free-vars x)))
396 (labels (filter cdr
397 (map (lambda (sym)
398 (cons sym (hashq-ref labels sym)))
399 (hashq-ref bound-vars x)))))
400 ;; set procedure allocations
401 (hashq-set! allocation x (cons labels free-addresses)))
402 n)
403
404 ((<lambda-case> opt kw inits vars body alternate)
405 (max
406 (let lp ((vars vars) (n n))
407 (if (null? vars)
408 (let ((nlocs (apply
409 max
410 (allocate! body proc n)
411 ;; inits not logically at the end, but they
412 ;; are the list...
413 (map (lambda (x) (allocate! x proc n)) inits))))
414 ;; label and nlocs for the case
415 (hashq-set! allocation x (cons (gensym ":LCASE") nlocs))
416 nlocs)
417 (begin
418 (hashq-set! allocation (car vars)
419 (make-hashq
420 proc `(#t ,(hashq-ref assigned (car vars)) . ,n)))
421 (lp (cdr vars) (1+ n)))))
422 (if alternate (allocate! alternate proc n) n)))
423
424 ((<let> vars vals body)
425 (let ((nmax (apply max (map recur vals))))
426 (cond
427 ;; the `or' hack
428 ((and (conditional? body)
429 (= (length vars) 1)
430 (let ((v (car vars)))
431 (and (not (hashq-ref assigned v))
432 (= (hashq-ref refcounts v 0) 2)
433 (lexical-ref? (conditional-test body))
434 (eq? (lexical-ref-gensym (conditional-test body)) v)
435 (lexical-ref? (conditional-consequent body))
436 (eq? (lexical-ref-gensym (conditional-consequent body)) v))))
437 (hashq-set! allocation (car vars)
438 (make-hashq proc `(#t #f . ,n)))
439 ;; the 1+ for this var
440 (max nmax (1+ n) (allocate! (conditional-alternate body) proc n)))
441 (else
442 (let lp ((vars vars) (n n))
443 (if (null? vars)
444 (max nmax (allocate! body proc n))
445 (let ((v (car vars)))
446 (hashq-set!
447 allocation v
448 (make-hashq proc
449 `(#t ,(hashq-ref assigned v) . ,n)))
450 (lp (cdr vars) (1+ n)))))))))
451
452 ((<letrec> vars vals body)
453 (let lp ((vars vars) (n n))
454 (if (null? vars)
455 (let ((nmax (apply max
456 (map (lambda (x)
457 (allocate! x proc n))
458 vals))))
459 (max nmax (allocate! body proc n)))
460 (let ((v (car vars)))
461 (hashq-set!
462 allocation v
463 (make-hashq proc
464 `(#t ,(hashq-ref assigned v) . ,n)))
465 (lp (cdr vars) (1+ n))))))
466
467 ((<fix> vars vals body)
468 (let lp ((in vars) (n n))
469 (if (null? in)
470 (let lp ((vars vars) (vals vals) (nmax n))
471 (cond
472 ((null? vars)
473 (max nmax (allocate! body proc n)))
474 ((hashq-ref labels (car vars))
475 ;; allocate lambda body inline to proc
476 (lp (cdr vars)
477 (cdr vals)
478 (record-case (car vals)
479 ((<lambda> body)
480 (max nmax (allocate! body proc n))))))
481 (else
482 ;; allocate closure
483 (lp (cdr vars)
484 (cdr vals)
485 (max nmax (allocate! (car vals) proc n))))))
486
487 (let ((v (car in)))
488 (cond
489 ((hashq-ref assigned v)
490 (error "fixpoint procedures may not be assigned" x))
491 ((hashq-ref labels v)
492 ;; no binding, it's a label
493 (lp (cdr in) n))
494 (else
495 ;; allocate closure binding
496 (hashq-set! allocation v (make-hashq proc `(#t #f . ,n)))
497 (lp (cdr in) (1+ n))))))))
498
499 ((<let-values> exp body)
500 (max (recur exp) (recur body)))
501
502 ((<dynwind> body winder unwinder)
503 (max (recur body) (recur winder) (recur unwinder)))
504
505 ((<dynlet> fluids vals body)
506 (apply max (recur body) (map recur (append fluids vals))))
507
508 ((<prompt> tag body handler)
509 (let ((cont-var (and (lambda-case? handler)
510 (pair? (lambda-case-vars handler))
511 (car (lambda-case-vars handler)))))
512 (hashq-set! allocation x
513 (and cont-var (zero? (hashq-ref refcounts cont-var 0))))
514 (max (recur tag) (recur body) (recur handler))))
515
516 ((<abort> tag args)
517 (apply max (recur tag) (map recur args)))
518
519 (else n)))
520
521 (analyze! x #f '() #t #f)
522 (allocate! x #f 0)
523
524 allocation)
525
526 \f
527 ;;;
528 ;;; Tree analyses for warnings.
529 ;;;
530
531 (define-record-type <tree-analysis>
532 (make-tree-analysis leaf down up post init)
533 tree-analysis?
534 (leaf tree-analysis-leaf) ;; (lambda (x result env locs) ...)
535 (down tree-analysis-down) ;; (lambda (x result env locs) ...)
536 (up tree-analysis-up) ;; (lambda (x result env locs) ...)
537 (post tree-analysis-post) ;; (lambda (result env) ...)
538 (init tree-analysis-init)) ;; arbitrary value
539
540 (define (analyze-tree analyses tree env)
541 "Run all tree analyses listed in ANALYSES on TREE for ENV, using
542 `tree-il-fold'. Return TREE. The leaf/down/up procedures of each analysis are
543 passed a ``location stack', which is the stack of `tree-il-src' values for each
544 parent tree (a list); it can be used to approximate source location when
545 accurate information is missing from a given `tree-il' element."
546
547 (define (traverse proc update-locs)
548 ;; Return a tree traversing procedure that returns a list of analysis
549 ;; results prepended by the location stack.
550 (lambda (x results)
551 (let ((locs (update-locs x (car results))))
552 (cons locs ;; the location stack
553 (map (lambda (analysis result)
554 ((proc analysis) x result env locs))
555 analyses
556 (cdr results))))))
557
558 ;; Keeping/extending/shrinking the location stack.
559 (define (keep-locs x locs) locs)
560 (define (extend-locs x locs) (cons (tree-il-src x) locs))
561 (define (shrink-locs x locs) (cdr locs))
562
563 (let ((results
564 (tree-il-fold (traverse tree-analysis-leaf keep-locs)
565 (traverse tree-analysis-down extend-locs)
566 (traverse tree-analysis-up shrink-locs)
567 (cons '() ;; empty location stack
568 (map tree-analysis-init analyses))
569 tree)))
570
571 (for-each (lambda (analysis result)
572 ((tree-analysis-post analysis) result env))
573 analyses
574 (cdr results)))
575
576 tree)
577
578 \f
579 ;;;
580 ;;; Unused variable analysis.
581 ;;;
582
583 ;; <binding-info> records are used during tree traversals in
584 ;; `unused-variable-analysis'. They contain a list of the local vars
585 ;; currently in scope, and a list of locals vars that have been referenced.
586 (define-record-type <binding-info>
587 (make-binding-info vars refs)
588 binding-info?
589 (vars binding-info-vars) ;; ((GENSYM NAME LOCATION) ...)
590 (refs binding-info-refs)) ;; (GENSYM ...)
591
592 (define unused-variable-analysis
593 ;; Report unused variables in the given tree.
594 (make-tree-analysis
595 (lambda (x info env locs)
596 ;; X is a leaf: extend INFO's refs accordingly.
597 (let ((refs (binding-info-refs info))
598 (vars (binding-info-vars info)))
599 (record-case x
600 ((<lexical-ref> gensym)
601 (make-binding-info vars (vhash-consq gensym #t refs)))
602 (else info))))
603
604 (lambda (x info env locs)
605 ;; Going down into X: extend INFO's variable list
606 ;; accordingly.
607 (let ((refs (binding-info-refs info))
608 (vars (binding-info-vars info))
609 (src (tree-il-src x)))
610 (define (extend inner-vars inner-names)
611 (fold (lambda (var name vars)
612 (vhash-consq var (list name src) vars))
613 vars
614 inner-vars
615 inner-names))
616
617 (record-case x
618 ((<lexical-set> gensym)
619 (make-binding-info vars (vhash-consq gensym #t refs)))
620 ((<lambda-case> req opt inits rest kw vars)
621 (let ((names `(,@req
622 ,@(or opt '())
623 ,@(if rest (list rest) '())
624 ,@(if kw (map cadr (cdr kw)) '()))))
625 (make-binding-info (extend vars names) refs)))
626 ((<let> vars names)
627 (make-binding-info (extend vars names) refs))
628 ((<letrec> vars names)
629 (make-binding-info (extend vars names) refs))
630 ((<fix> vars names)
631 (make-binding-info (extend vars names) refs))
632 (else info))))
633
634 (lambda (x info env locs)
635 ;; Leaving X's scope: shrink INFO's variable list
636 ;; accordingly and reported unused nested variables.
637 (let ((refs (binding-info-refs info))
638 (vars (binding-info-vars info)))
639 (define (shrink inner-vars refs)
640 (vlist-for-each
641 (lambda (var)
642 (let ((gensym (car var)))
643 ;; Don't report lambda parameters as unused.
644 (if (and (memq gensym inner-vars)
645 (not (vhash-assq gensym refs))
646 (not (lambda-case? x)))
647 (let ((name (cadr var))
648 ;; We can get approximate source location by going up
649 ;; the LOCS location stack.
650 (loc (or (caddr var)
651 (find pair? locs))))
652 (warning 'unused-variable loc name)))))
653 vars)
654 (vlist-drop vars (length inner-vars)))
655
656 ;; For simplicity, we leave REFS untouched, i.e., with
657 ;; names of variables that are now going out of scope.
658 ;; It doesn't hurt as these are unique names, it just
659 ;; makes REFS unnecessarily fat.
660 (record-case x
661 ((<lambda-case> vars)
662 (make-binding-info (shrink vars refs) refs))
663 ((<let> vars)
664 (make-binding-info (shrink vars refs) refs))
665 ((<letrec> vars)
666 (make-binding-info (shrink vars refs) refs))
667 ((<fix> vars)
668 (make-binding-info (shrink vars refs) refs))
669 (else info))))
670
671 (lambda (result env) #t)
672 (make-binding-info vlist-null vlist-null)))
673
674 \f
675 ;;;
676 ;;; Unused top-level variable analysis.
677 ;;;
678
679 ;; <reference-graph> record top-level definitions that are made, references to
680 ;; top-level definitions and their context (the top-level definition in which
681 ;; the reference appears), as well as the current context (the top-level
682 ;; definition we're currently in). The second part (`refs' below) is
683 ;; effectively a graph from which we can determine unused top-level definitions.
684 (define-record-type <reference-graph>
685 (make-reference-graph refs defs toplevel-context)
686 reference-graph?
687 (defs reference-graph-defs) ;; ((NAME . LOC) ...)
688 (refs reference-graph-refs) ;; ((REF-CONTEXT REF ...) ...)
689 (toplevel-context reference-graph-toplevel-context)) ;; NAME | #f
690
691 (define (graph-reachable-nodes root refs reachable)
692 ;; Add to REACHABLE the nodes reachable from ROOT in graph REFS. REFS is a
693 ;; vhash mapping nodes to the list of their children: for instance,
694 ;; ((A -> (B C)) (B -> (A)) (C -> ())) corresponds to
695 ;;
696 ;; ,-------.
697 ;; v |
698 ;; A ----> B
699 ;; |
700 ;; v
701 ;; C
702 ;;
703 ;; REACHABLE is a vhash of nodes known to be otherwise reachable.
704
705 (let loop ((root root)
706 (path vlist-null)
707 (result reachable))
708 (if (or (vhash-assq root path)
709 (vhash-assq root result))
710 result
711 (let* ((children (or (and=> (vhash-assq root refs) cdr) '()))
712 (path (vhash-consq root #t path))
713 (result (fold (lambda (kid result)
714 (loop kid path result))
715 result
716 children)))
717 (fold (lambda (kid result)
718 (vhash-consq kid #t result))
719 result
720 children)))))
721
722 (define (graph-reachable-nodes* roots refs)
723 ;; Return the list of nodes in REFS reachable from the nodes listed in ROOTS.
724 (vlist-fold (lambda (root+true result)
725 (let* ((root (car root+true))
726 (reachable (graph-reachable-nodes root refs result)))
727 (vhash-consq root #t reachable)))
728 vlist-null
729 roots))
730
731 (define (partition* pred vhash)
732 ;; Partition VHASH according to PRED. Return the two resulting vhashes.
733 (let ((result
734 (vlist-fold (lambda (k+v result)
735 (let ((k (car k+v))
736 (v (cdr k+v))
737 (r1 (car result))
738 (r2 (cdr result)))
739 (if (pred k)
740 (cons (vhash-consq k v r1) r2)
741 (cons r1 (vhash-consq k v r2)))))
742 (cons vlist-null vlist-null)
743 vhash)))
744 (values (car result) (cdr result))))
745
746 (define unused-toplevel-analysis
747 ;; Report unused top-level definitions that are not exported.
748 (let ((add-ref-from-context
749 (lambda (graph name)
750 ;; Add an edge CTX -> NAME in GRAPH.
751 (let* ((refs (reference-graph-refs graph))
752 (defs (reference-graph-defs graph))
753 (ctx (reference-graph-toplevel-context graph))
754 (ctx-refs (or (and=> (vhash-assq ctx refs) cdr) '())))
755 (make-reference-graph (vhash-consq ctx (cons name ctx-refs) refs)
756 defs ctx)))))
757 (define (macro-variable? name env)
758 (and (module? env)
759 (let ((var (module-variable env name)))
760 (and var (variable-bound? var)
761 (macro? (variable-ref var))))))
762
763 (make-tree-analysis
764 (lambda (x graph env locs)
765 ;; X is a leaf.
766 (let ((ctx (reference-graph-toplevel-context graph)))
767 (record-case x
768 ((<toplevel-ref> name src)
769 (add-ref-from-context graph name))
770 (else graph))))
771
772 (lambda (x graph env locs)
773 ;; Going down into X.
774 (let ((ctx (reference-graph-toplevel-context graph))
775 (refs (reference-graph-refs graph))
776 (defs (reference-graph-defs graph)))
777 (record-case x
778 ((<toplevel-define> name src)
779 (let ((refs refs)
780 (defs (vhash-consq name (or src (find pair? locs))
781 defs)))
782 (make-reference-graph refs defs name)))
783 ((<toplevel-set> name src)
784 (add-ref-from-context graph name))
785 (else graph))))
786
787 (lambda (x graph env locs)
788 ;; Leaving X's scope.
789 (record-case x
790 ((<toplevel-define>)
791 (let ((refs (reference-graph-refs graph))
792 (defs (reference-graph-defs graph)))
793 (make-reference-graph refs defs #f)))
794 (else graph)))
795
796 (lambda (graph env)
797 ;; Process the resulting reference graph: determine all private definitions
798 ;; not reachable from any public definition. Macros
799 ;; (syntax-transformers), which are globally bound, never considered
800 ;; unused since we can't tell whether a macro is actually used; in
801 ;; addition, macros are considered roots of the graph since they may use
802 ;; private bindings. FIXME: The `make-syntax-transformer' calls don't
803 ;; contain any literal `toplevel-ref' of the global bindings they use so
804 ;; this strategy fails.
805 (define (exported? name)
806 (if (module? env)
807 (module-variable (module-public-interface env) name)
808 #t))
809
810 (let-values (((public-defs private-defs)
811 (partition* (lambda (name)
812 (or (exported? name)
813 (macro-variable? name env)))
814 (reference-graph-defs graph))))
815 (let* ((roots (vhash-consq #f #t public-defs))
816 (refs (reference-graph-refs graph))
817 (reachable (graph-reachable-nodes* roots refs))
818 (unused (vlist-filter (lambda (name+src)
819 (not (vhash-assq (car name+src)
820 reachable)))
821 private-defs)))
822 (vlist-for-each (lambda (name+loc)
823 (let ((name (car name+loc))
824 (loc (cdr name+loc)))
825 (warning 'unused-toplevel loc name)))
826 unused))))
827
828 (make-reference-graph vlist-null vlist-null #f))))
829
830 \f
831 ;;;
832 ;;; Unbound variable analysis.
833 ;;;
834
835 ;; <toplevel-info> records are used during tree traversal in search of
836 ;; possibly unbound variable. They contain a list of references to
837 ;; potentially unbound top-level variables, and a list of the top-level
838 ;; defines that have been encountered.
839 (define-record-type <toplevel-info>
840 (make-toplevel-info refs defs)
841 toplevel-info?
842 (refs toplevel-info-refs) ;; ((VARIABLE-NAME . LOCATION) ...)
843 (defs toplevel-info-defs)) ;; (VARIABLE-NAME ...)
844
845 (define (goops-toplevel-definition proc args env)
846 ;; If application of PROC to ARGS is a GOOPS top-level definition, return
847 ;; the name of the variable being defined; otherwise return #f. This
848 ;; assumes knowledge of the current implementation of `define-class' et al.
849 (define (toplevel-define-arg args)
850 (and (pair? args) (pair? (cdr args)) (null? (cddr args))
851 (record-case (car args)
852 ((<const> exp)
853 (and (symbol? exp) exp))
854 (else #f))))
855
856 (record-case proc
857 ((<module-ref> mod public? name)
858 (and (equal? mod '(oop goops))
859 (not public?)
860 (eq? name 'toplevel-define!)
861 (toplevel-define-arg args)))
862 ((<toplevel-ref> name)
863 ;; This may be the result of expanding one of the GOOPS macros within
864 ;; `oop/goops.scm'.
865 (and (eq? name 'toplevel-define!)
866 (eq? env (resolve-module '(oop goops)))
867 (toplevel-define-arg args)))
868 (else #f)))
869
870 (define unbound-variable-analysis
871 ;; Report possibly unbound variables in the given tree.
872 (make-tree-analysis
873 (lambda (x info env locs)
874 ;; X is a leaf: extend INFO's refs accordingly.
875 (let ((refs (toplevel-info-refs info))
876 (defs (toplevel-info-defs info)))
877 (define (bound? name)
878 (or (and (module? env)
879 (module-variable env name))
880 (vhash-assq name defs)))
881
882 (record-case x
883 ((<toplevel-ref> name src)
884 (if (bound? name)
885 info
886 (let ((src (or src (find pair? locs))))
887 (make-toplevel-info (vhash-consq name src refs)
888 defs))))
889 (else info))))
890
891 (lambda (x info env locs)
892 ;; Going down into X.
893 (let* ((refs (toplevel-info-refs info))
894 (defs (toplevel-info-defs info))
895 (src (tree-il-src x)))
896 (define (bound? name)
897 (or (and (module? env)
898 (module-variable env name))
899 (vhash-assq name defs)))
900
901 (record-case x
902 ((<toplevel-set> name src)
903 (if (bound? name)
904 (make-toplevel-info refs defs)
905 (let ((src (find pair? locs)))
906 (make-toplevel-info (vhash-consq name src refs)
907 defs))))
908 ((<toplevel-define> name)
909 (make-toplevel-info (vhash-delete name refs eq?)
910 (vhash-consq name #t defs)))
911
912 ((<application> proc args)
913 ;; Check for a dynamic top-level definition, as is
914 ;; done by code expanded from GOOPS macros.
915 (let ((name (goops-toplevel-definition proc args
916 env)))
917 (if (symbol? name)
918 (make-toplevel-info (vhash-delete name refs
919 eq?)
920 (vhash-consq name #t defs))
921 (make-toplevel-info refs defs))))
922 (else
923 (make-toplevel-info refs defs)))))
924
925 (lambda (x info env locs)
926 ;; Leaving X's scope.
927 info)
928
929 (lambda (toplevel env)
930 ;; Post-process the result.
931 (vlist-for-each (lambda (name+loc)
932 (let ((name (car name+loc))
933 (loc (cdr name+loc)))
934 (warning 'unbound-variable loc name)))
935 (vlist-reverse (toplevel-info-refs toplevel))))
936
937 (make-toplevel-info vlist-null vlist-null)))
938
939 \f
940 ;;;
941 ;;; Arity analysis.
942 ;;;
943
944 ;; <arity-info> records contain information about lexical definitions of
945 ;; procedures currently in scope, top-level procedure definitions that have
946 ;; been encountered, and calls to top-level procedures that have been
947 ;; encountered.
948 (define-record-type <arity-info>
949 (make-arity-info toplevel-calls lexical-lambdas toplevel-lambdas)
950 arity-info?
951 (toplevel-calls toplevel-procedure-calls) ;; ((NAME . APPLICATION) ...)
952 (lexical-lambdas lexical-lambdas) ;; ((GENSYM . DEFINITION) ...)
953 (toplevel-lambdas toplevel-lambdas)) ;; ((NAME . DEFINITION) ...)
954
955 (define (validate-arity proc application lexical?)
956 ;; Validate the argument count of APPLICATION, a tree-il application of
957 ;; PROC, emitting a warning in case of argument count mismatch.
958
959 (define (filter-keyword-args keywords allow-other-keys? args)
960 ;; Filter keyword arguments from ARGS and return the resulting list.
961 ;; KEYWORDS is the list of allowed keywords, and ALLOW-OTHER-KEYS?
962 ;; specified whethere keywords not listed in KEYWORDS are allowed.
963 (let loop ((args args)
964 (result '()))
965 (if (null? args)
966 (reverse result)
967 (let ((arg (car args)))
968 (if (and (const? arg)
969 (or (memq (const-exp arg) keywords)
970 (and allow-other-keys?
971 (keyword? (const-exp arg)))))
972 (loop (if (pair? (cdr args))
973 (cddr args)
974 '())
975 result)
976 (loop (cdr args)
977 (cons arg result)))))))
978
979 (define (arities proc)
980 ;; Return the arities of PROC, which can be either a tree-il or a
981 ;; procedure.
982 (define (len x)
983 (or (and (or (null? x) (pair? x))
984 (length x))
985 0))
986 (cond ((program? proc)
987 (values (program-name proc)
988 (map (lambda (a)
989 (list (arity:nreq a) (arity:nopt a) (arity:rest? a)
990 (map car (arity:kw a))
991 (arity:allow-other-keys? a)))
992 (program-arities proc))))
993 ((procedure? proc)
994 (let ((arity (procedure-property proc 'arity)))
995 (values (procedure-name proc)
996 (list (list (car arity) (cadr arity) (caddr arity)
997 #f #f)))))
998 (else
999 (let loop ((name #f)
1000 (proc proc)
1001 (arities '()))
1002 (if (not proc)
1003 (values name (reverse arities))
1004 (record-case proc
1005 ((<lambda-case> req opt rest kw alternate)
1006 (loop name alternate
1007 (cons (list (len req) (len opt) rest
1008 (and (pair? kw) (map car (cdr kw)))
1009 (and (pair? kw) (car kw)))
1010 arities)))
1011 ((<lambda> meta body)
1012 (loop (assoc-ref meta 'name) body arities))
1013 (else
1014 (values #f #f))))))))
1015
1016 (let ((args (application-args application))
1017 (src (tree-il-src application)))
1018 (call-with-values (lambda () (arities proc))
1019 (lambda (name arities)
1020 (define matches?
1021 (find (lambda (arity)
1022 (pmatch arity
1023 ((,req ,opt ,rest? ,kw ,aok?)
1024 (let ((args (if (pair? kw)
1025 (filter-keyword-args kw aok? args)
1026 args)))
1027 (if (and req opt)
1028 (let ((count (length args)))
1029 (and (>= count req)
1030 (or rest?
1031 (<= count (+ req opt)))))
1032 #t)))
1033 (else #t)))
1034 arities))
1035
1036 (if (not matches?)
1037 (warning 'arity-mismatch src
1038 (or name (with-output-to-string (lambda () (write proc))))
1039 lexical?)))))
1040 #t)
1041
1042 (define arity-analysis
1043 ;; Report arity mismatches in the given tree.
1044 (make-tree-analysis
1045 (lambda (x info env locs)
1046 ;; X is a leaf.
1047 info)
1048 (lambda (x info env locs)
1049 ;; Down into X.
1050 (define (extend lexical-name val info)
1051 ;; If VAL is a lambda, add NAME to the lexical-lambdas of INFO.
1052 (let ((toplevel-calls (toplevel-procedure-calls info))
1053 (lexical-lambdas (lexical-lambdas info))
1054 (toplevel-lambdas (toplevel-lambdas info)))
1055 (record-case val
1056 ((<lambda> body)
1057 (make-arity-info toplevel-calls
1058 (vhash-consq lexical-name val
1059 lexical-lambdas)
1060 toplevel-lambdas))
1061 ((<lexical-ref> gensym)
1062 ;; lexical alias
1063 (let ((val* (vhash-assq gensym lexical-lambdas)))
1064 (if (pair? val*)
1065 (extend lexical-name (cdr val*) info)
1066 info)))
1067 ((<toplevel-ref> name)
1068 ;; top-level alias
1069 (make-arity-info toplevel-calls
1070 (vhash-consq lexical-name val
1071 lexical-lambdas)
1072 toplevel-lambdas))
1073 (else info))))
1074
1075 (let ((toplevel-calls (toplevel-procedure-calls info))
1076 (lexical-lambdas (lexical-lambdas info))
1077 (toplevel-lambdas (toplevel-lambdas info)))
1078
1079 (record-case x
1080 ((<toplevel-define> name exp)
1081 (record-case exp
1082 ((<lambda> body)
1083 (make-arity-info toplevel-calls
1084 lexical-lambdas
1085 (vhash-consq name exp toplevel-lambdas)))
1086 ((<toplevel-ref> name)
1087 ;; alias for another toplevel
1088 (let ((proc (vhash-assq name toplevel-lambdas)))
1089 (make-arity-info toplevel-calls
1090 lexical-lambdas
1091 (vhash-consq (toplevel-define-name x)
1092 (if (pair? proc)
1093 (cdr proc)
1094 exp)
1095 toplevel-lambdas))))
1096 (else info)))
1097 ((<let> vars vals)
1098 (fold extend info vars vals))
1099 ((<letrec> vars vals)
1100 (fold extend info vars vals))
1101 ((<fix> vars vals)
1102 (fold extend info vars vals))
1103
1104 ((<application> proc args src)
1105 (record-case proc
1106 ((<lambda> body)
1107 (validate-arity proc x #t)
1108 info)
1109 ((<toplevel-ref> name)
1110 (make-arity-info (vhash-consq name x toplevel-calls)
1111 lexical-lambdas
1112 toplevel-lambdas))
1113 ((<lexical-ref> gensym)
1114 (let ((proc (vhash-assq gensym lexical-lambdas)))
1115 (if (pair? proc)
1116 (record-case (cdr proc)
1117 ((<toplevel-ref> name)
1118 ;; alias to toplevel
1119 (make-arity-info (vhash-consq name x toplevel-calls)
1120 lexical-lambdas
1121 toplevel-lambdas))
1122 (else
1123 (validate-arity (cdr proc) x #t)
1124 info))
1125
1126 ;; If GENSYM wasn't found, it may be because it's an
1127 ;; argument of the procedure being compiled.
1128 info)))
1129 (else info)))
1130 (else info))))
1131
1132 (lambda (x info env locs)
1133 ;; Up from X.
1134 (define (shrink name val info)
1135 ;; Remove NAME from the lexical-lambdas of INFO.
1136 (let ((toplevel-calls (toplevel-procedure-calls info))
1137 (lexical-lambdas (lexical-lambdas info))
1138 (toplevel-lambdas (toplevel-lambdas info)))
1139 (make-arity-info toplevel-calls
1140 (if (vhash-assq name lexical-lambdas)
1141 (vlist-tail lexical-lambdas)
1142 lexical-lambdas)
1143 toplevel-lambdas)))
1144
1145 (let ((toplevel-calls (toplevel-procedure-calls info))
1146 (lexical-lambdas (lexical-lambdas info))
1147 (toplevel-lambdas (toplevel-lambdas info)))
1148 (record-case x
1149 ((<let> vars vals)
1150 (fold shrink info vars vals))
1151 ((<letrec> vars vals)
1152 (fold shrink info vars vals))
1153 ((<fix> vars vals)
1154 (fold shrink info vars vals))
1155
1156 (else info))))
1157
1158 (lambda (result env)
1159 ;; Post-processing: check all top-level procedure calls that have been
1160 ;; encountered.
1161 (let ((toplevel-calls (toplevel-procedure-calls result))
1162 (toplevel-lambdas (toplevel-lambdas result)))
1163 (vlist-for-each
1164 (lambda (name+application)
1165 (let* ((name (car name+application))
1166 (application (cdr name+application))
1167 (proc
1168 (or (and=> (vhash-assq name toplevel-lambdas) cdr)
1169 (and (module? env)
1170 (false-if-exception
1171 (module-ref env name)))))
1172 (proc*
1173 ;; handle toplevel aliases
1174 (if (toplevel-ref? proc)
1175 (let ((name (toplevel-ref-name proc)))
1176 (and (module? env)
1177 (false-if-exception
1178 (module-ref env name))))
1179 proc)))
1180 (if (or (lambda? proc*) (procedure? proc*))
1181 (validate-arity proc* application (lambda? proc*)))))
1182 toplevel-calls)))
1183
1184 (make-arity-info vlist-null vlist-null vlist-null)))