Remove "free" field of $fun
[bpt/guile.git] / module / language / cps / prune-top-level-scopes.scm
1 ;;; Continuation-passing style (CPS) intermediate language (IL)
2
3 ;; Copyright (C) 2014, 2015 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 ;;; Commentary:
20 ;;;
21 ;;; A simple pass to prune unneeded top-level scopes.
22 ;;;
23 ;;; Code:
24
25 (define-module (language cps prune-top-level-scopes)
26 #:use-module (ice-9 match)
27 #:use-module (language cps)
28 #:export (prune-top-level-scopes))
29
30 (define (compute-referenced-scopes fun)
31 (let ((scope-name->used? (make-hash-table))
32 (scope-var->used? (make-hash-table))
33 (k->scope-var (make-hash-table)))
34 ;; Visit uses before defs. That way we know when visiting defs
35 ;; whether the scope is used or not.
36 (define (visit-cont cont)
37 (match cont
38 (($ $cont k ($ $kargs (name) (var) body))
39 (visit-term body)
40 (when (hashq-get-handle scope-var->used? var)
41 (hashq-set! k->scope-var k var)))
42 (($ $cont k ($ $kargs names syms body))
43 (visit-term body))
44 (($ $cont k ($ $kfun src meta self tail clause))
45 (when clause (visit-cont clause)))
46 (($ $cont k ($ $kclause arity body alternate))
47 (visit-cont body)
48 (when alternate (visit-cont alternate)))
49 (($ $cont k ($ $kreceive))
50 #t)))
51 (define (visit-term term)
52 (match term
53 (($ $letk conts body)
54 (for-each visit-cont conts)
55 (visit-term body))
56 (($ $continue k src exp)
57 (match exp
58 (($ $fun) (visit-fun exp))
59 (($ $rec names syms funs)
60 (for-each visit-fun funs))
61 (($ $primcall 'cached-toplevel-box (scope name bound?))
62 (hashq-set! scope-var->used? scope #t))
63 (($ $primcall 'cache-current-module! (module scope))
64 (hashq-set! scope-var->used? scope #f))
65 (($ $const val)
66 ;; If there is an entry in the table for "k", it means "val"
67 ;; is a scope symbol, bound for use by cached-toplevel-box
68 ;; or cache-current-module!, or possibly both (though this
69 ;; is not currently the case).
70 (and=> (hashq-ref k->scope-var k)
71 (lambda (scope-var)
72 (when (hashq-ref scope-var->used? scope-var)
73 ;; We have a use via cached-toplevel-box. Mark
74 ;; this scope as used.
75 (hashq-set! scope-name->used? val #t))
76 (when (and (hashq-ref scope-name->used? val)
77 (not (hashq-ref scope-var->used? scope-var)))
78 ;; There is a use, and this sym is used by
79 ;; cache-current-module!.
80 (hashq-set! scope-var->used? scope-var #t)))))
81 (_ #t)))))
82 (define (visit-fun fun)
83 (match fun
84 (($ $fun body)
85 (visit-cont body))))
86
87 (visit-cont fun)
88 scope-var->used?))
89
90 (define (prune-top-level-scopes fun)
91 (let ((scope-var->used? (compute-referenced-scopes fun)))
92 (define (visit-cont cont)
93 (rewrite-cps-cont cont
94 (($ $cont sym ($ $kargs names syms body))
95 (sym ($kargs names syms ,(visit-term body))))
96 (($ $cont sym ($ $kfun src meta self tail clause))
97 (sym ($kfun src meta self ,tail ,(and clause (visit-cont clause)))))
98 (($ $cont sym ($ $kclause arity body alternate))
99 (sym ($kclause ,arity ,(visit-cont body)
100 ,(and alternate (visit-cont alternate)))))
101 (($ $cont sym ($ $kreceive))
102 ,cont)))
103 (define (visit-term term)
104 (rewrite-cps-term term
105 (($ $letk conts body)
106 ($letk ,(map visit-cont conts) ,(visit-term body)))
107 (($ $continue k src
108 (and ($ $primcall 'cache-current-module! (module scope))
109 (? (lambda _
110 (not (hashq-ref scope-var->used? scope))))))
111 ($continue k src ($primcall 'values ())))
112 (($ $continue)
113 ,term)))
114 (visit-cont fun)))