1 ;;; TREE-IL -> GLIL compiler
3 ;; Copyright (C) 2001,2008,2009 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program 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
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 (define-module (language tree-il analyze)
23 #:use-module (system base syntax)
24 #:use-module (language tree-il)
25 #:export (analyze-lexicals))
27 ;; allocation: the process of assigning a type and index to each var
28 ;; a var is external if it is heaps; assigning index is easy
29 ;; args are assigned in order
30 ;; locals are indexed as their linear position in the binding path
37 ;; This algorithm has the problem that variables are only allocated
38 ;; indices at the end of the binding path. If variables bound early in
39 ;; the path are not used in later portions of the path, their indices
40 ;; will not be recycled. This problem is particularly egregious in the
44 ;; -> (let ((a x)) (if a a (let ((b y)) (if b b z))))
46 ;; As you can see, the `a' binding is only used in the ephemeral `then'
47 ;; clause of the first `if', but its index would be reserved for the
48 ;; whole of the `or' expansion. So we have a hack for this specific
49 ;; case. A proper solution would be some sort of liveness analysis, and
50 ;; not our linear allocation algorithm.
53 ;; sym -> (local . index) | (heap level . index)
54 ;; lambda -> (nlocs . nexts)
56 (define (analyze-lexicals x)
57 ;; parents: lambda -> parent
58 ;; useful when we see a closed-over var, so we can calculate its
59 ;; coordinates (depth and index).
60 ;; bindings: lambda -> (sym ...)
61 ;; useful for two reasons: one, so we know how much space to allocate
62 ;; when we go into a lambda; and two, so that we know when to stop,
63 ;; when looking for closed-over vars.
64 ;; heaps: sym -> lambda
65 ;; allows us to heapify vars in an O(1) fashion
66 ;; refcounts: sym -> count
67 ;; allows us to detect the or-expansion an O(1) time
69 (define (find-heap sym parent)
70 ;; fixme: check displaced lexicals here?
71 (if (memq sym (hashq-ref bindings parent))
73 (find-heap sym (hashq-ref parents parent))))
75 (define (analyze! x parent level)
76 (define (step y) (analyze! y parent level))
77 (define (recur x parent) (analyze! x parent (1+ level)))
79 ((<application> proc args)
80 (step proc) (for-each step args))
82 ((<conditional> test then else)
83 (step test) (step then) (step else))
85 ((<lexical-ref> name gensym)
86 (hashq-set! refcounts gensym (1+ (hashq-ref refcounts gensym 0)))
87 (if (and (not (memq gensym (hashq-ref bindings parent)))
88 (not (hashq-ref heaps gensym)))
89 (hashq-set! heaps gensym (find-heap gensym parent))))
91 ((<lexical-set> name gensym exp)
93 (if (not (hashq-ref heaps gensym))
94 (hashq-set! heaps gensym (find-heap gensym parent))))
96 ((<module-set> mod name public? exp)
99 ((<toplevel-set> name exp)
102 ((<toplevel-define> name exp)
106 (for-each step exps))
108 ((<lambda> vars meta body)
109 (hashq-set! parents x parent)
110 (hashq-set! bindings x
111 (let rev* ((vars vars) (out '()))
112 (cond ((null? vars) out)
113 ((pair? vars) (rev* (cdr vars)
114 (cons (car vars) out)))
115 (else (cons vars out)))))
117 (hashq-set! bindings x (reverse! (hashq-ref bindings x))))
119 ((<let> vars vals body)
121 (hashq-set! bindings parent
122 (append (reverse vars) (hashq-ref bindings parent)))
125 ((<letrec> vars vals body)
126 (hashq-set! bindings parent
127 (append (reverse vars) (hashq-ref bindings parent)))
131 ((<let-values> vars exp body)
132 (hashq-set! bindings parent
133 (let lp ((out (hashq-ref bindings parent)) (in vars))
135 (lp (cons (car in) out) (cdr in))
136 (if (null? in) out (cons in out)))))
142 (define (allocate-heap! binder)
143 (hashq-set! heap-indexes binder
144 (1+ (hashq-ref heap-indexes binder -1))))
146 (define (allocate! x level n)
147 (define (recur y) (allocate! y level n))
149 ((<application> proc args)
150 (apply max (recur proc) (map recur args)))
152 ((<conditional> test then else)
153 (max (recur test) (recur then) (recur else)))
155 ((<lexical-set> name gensym exp)
158 ((<module-set> mod name public? exp)
161 ((<toplevel-set> name exp)
164 ((<toplevel-define> name exp)
168 (apply max (map recur exps)))
170 ((<lambda> vars meta body)
171 (let lp ((vars vars) (n 0))
173 (hashq-set! allocation x
174 (let ((nlocs (- (allocate! body (1+ level) n) n)))
175 (cons nlocs (1+ (hashq-ref heap-indexes x -1)))))
176 (let ((v (if (pair? vars) (car vars) vars)))
177 (let ((binder (hashq-ref heaps v)))
181 (cons* 'heap (1+ level) (allocate-heap! binder))
183 (lp (if (pair? vars) (cdr vars) '()) (1+ n)))))
186 ((<let> vars vals body)
187 (let ((nmax (apply max (map recur vals))))
190 ((and (conditional? body)
192 (let ((v (car vars)))
193 (and (not (hashq-ref heaps v))
194 (= (hashq-ref refcounts v 0) 2)
195 (lexical-ref? (conditional-test body))
196 (eq? (lexical-ref-gensym (conditional-test body)) v)
197 (lexical-ref? (conditional-then body))
198 (eq? (lexical-ref-gensym (conditional-then body)) v))))
199 (hashq-set! allocation (car vars) (cons 'stack n))
200 ;; the 1+ for this var
201 (max nmax (1+ n) (allocate! (conditional-else body) level n)))
203 (let lp ((vars vars) (n n))
205 (max nmax (allocate! body level n))
206 (let ((v (car vars)))
207 (let ((binder (hashq-ref heaps v)))
211 (cons* 'heap level (allocate-heap! binder))
213 (lp (cdr vars) (if binder n (1+ n)))))))))))
215 ((<letrec> vars vals body)
216 (let lp ((vars vars) (n n))
218 (let ((nmax (apply max
220 (allocate! x level n))
222 (max nmax (allocate! body level n)))
223 (let ((v (car vars)))
224 (let ((binder (hashq-ref heaps v)))
228 (cons* 'heap level (allocate-heap! binder))
230 (lp (cdr vars) (if binder n (1+ n))))))))
232 ((<let-values> vars exp body)
233 (let ((nmax (recur exp)))
234 (let lp ((vars vars) (n n))
236 (max nmax (allocate! body level n))
237 (let ((v (if (pair? vars) (car vars) vars)))
238 (let ((binder (hashq-ref heaps v)))
242 (cons* 'heap level (allocate-heap! binder))
244 (lp (if (pair? vars) (cdr vars) '())
245 (if binder n (1+ n)))))))))
249 (define parents (make-hash-table))
250 (define bindings (make-hash-table))
251 (define heaps (make-hash-table))
252 (define refcounts (make-hash-table))
253 (define allocation (make-hash-table))
254 (define heap-indexes (make-hash-table))