1 ;;; transformation of letrec into simpler forms
3 ;; Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
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.
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.
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
19 (define-module (language tree-il fix-letrec)
20 #:use-module (system base syntax)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-11)
23 #:use-module (language tree-il)
24 #:use-module (language tree-il effects)
25 #:export (fix-letrec!))
27 ;; For a detailed discussion, see "Fixing Letrec: A Faithful Yet
28 ;; Efficient Implementation of Scheme's Recursive Binding Construct", by
29 ;; Oscar Waddell, Dipanwita Sarkar, and R. Kent Dybvig.
32 (make-tree-il-folder unref ref set simple lambda complex))
34 (define (simple-expression? x bound-vars simple-primcall?)
38 ((<lexical-ref> gensym)
39 (not (memq gensym bound-vars)))
40 ((<conditional> test consequent alternate)
41 (and (simple-expression? test bound-vars simple-primcall?)
42 (simple-expression? consequent bound-vars simple-primcall?)
43 (simple-expression? alternate bound-vars simple-primcall?)))
45 (and-map (lambda (x) (simple-expression? x bound-vars simple-primcall?))
47 ((<application> proc args)
48 (and (primitive-ref? proc)
51 (simple-expression? x bound-vars simple-primcall?))
55 (define (partition-vars x)
57 (((unref ref set simple lambda* complex)
59 (lambda (x unref ref set simple lambda* complex)
61 ((<lexical-ref> gensym)
62 (values (delq gensym unref)
63 (lset-adjoin eq? ref gensym)
68 ((<lexical-set> gensym)
71 (lset-adjoin eq? set gensym)
76 (values (append gensyms unref)
83 (values (append gensyms unref)
90 (values unref ref set simple lambda* complex))))
91 (lambda (x unref ref set simple lambda* complex)
93 ((<letrec> in-order? (orig-gensyms gensyms) vals)
94 (define compute-effects
95 (make-effects-analyzer (lambda (x) (memq x set))))
96 (define (effect-free-primcall? x)
97 (let ((effects (compute-effects x)))
99 (exclude-effects effects (logior &allocation
101 (define (effect+exception-free-primcall? x)
102 (let ((effects (compute-effects x)))
104 (exclude-effects effects &allocation))))
105 (let lp ((gensyms orig-gensyms) (vals vals)
106 (s '()) (l '()) (c '()))
109 ;; Unreferenced complex vars are still
110 ;; complex for letrec*. We need to update
111 ;; our algorithm to "Fixing letrec reloaded"
113 (values (if in-order?
114 (lset-difference eq? unref c)
121 ((memq (car gensyms) unref)
122 ;; See above note about unref and letrec*.
124 (not (lambda? (car vals)))
125 (not (simple-expression?
126 (car vals) orig-gensyms
127 effect+exception-free-primcall?)))
128 (lp (cdr gensyms) (cdr vals)
129 s l (cons (car gensyms) c))
130 (lp (cdr gensyms) (cdr vals)
132 ((memq (car gensyms) set)
133 (lp (cdr gensyms) (cdr vals)
134 s l (cons (car gensyms) c)))
135 ((lambda? (car vals))
136 (lp (cdr gensyms) (cdr vals)
137 s (cons (car gensyms) l) c))
139 (car vals) orig-gensyms
141 effect+exception-free-primcall?
142 effect-free-primcall?))
143 ;; For letrec*, we can't consider e.g. `car' to be
144 ;; "simple", as it could raise an exception. Hence
145 ;; effect+exception-free-primitive? above.
146 (lp (cdr gensyms) (cdr vals)
147 (cons (car gensyms) s) l c))
149 (lp (cdr gensyms) (cdr vals)
150 s l (cons (car gensyms) c))))))
151 ((<let> (orig-gensyms gensyms) vals)
152 ;; The point is to compile let-bound lambdas as
153 ;; efficiently as we do letrec-bound lambdas, so
154 ;; we use the same algorithm for analyzing the
155 ;; gensyms. There is no problem recursing into the
156 ;; bindings after the let, because all variables
157 ;; have been renamed.
158 (let lp ((gensyms orig-gensyms) (vals vals)
159 (s '()) (l '()) (c '()))
168 ((memq (car gensyms) unref)
169 (lp (cdr gensyms) (cdr vals)
171 ((memq (car gensyms) set)
172 (lp (cdr gensyms) (cdr vals)
173 s l (cons (car gensyms) c)))
174 ((and (lambda? (car vals))
175 (not (memq (car gensyms) set)))
176 (lp (cdr gensyms) (cdr vals)
177 s (cons (car gensyms) l) c))
178 ;; There is no difference between simple and
179 ;; complex, for the purposes of let. Just lump
180 ;; them all into complex.
182 (lp (cdr gensyms) (cdr vals)
183 s l (cons (car gensyms) c))))))
185 (values unref ref set simple lambda* complex))))
192 (values unref simple lambda* complex)))
194 (define (make-sequence* src exps)
195 (let lp ((in exps) (out '()))
199 (make-sequence src (reverse (cons (car in) out))))
200 (let ((head (car in)))
202 ((<lambda>) (lp (cdr in) out))
203 ((<const>) (lp (cdr in) out))
204 ((<lexical-ref>) (lp (cdr in) out))
205 ((<void>) (lp (cdr in) out))
206 (else (lp (cdr in) (cons head out))))))))
208 (define (fix-letrec! x)
209 (let-values (((unref simple lambda* complex) (partition-vars x)))
214 ;; Sets to unreferenced variables may be replaced by their
215 ;; expression, called for effect.
216 ((<lexical-set> gensym exp)
217 (if (memq gensym unref)
218 (make-sequence* #f (list exp (make-void #f)))
221 ((<letrec> src in-order? names gensyms vals body)
222 (let ((binds (map list gensyms names vals)))
223 ;; The bindings returned by this function need to appear in the same
224 ;; order that they appear in the letrec.
226 (let lp ((binds binds))
229 ((memq (caar binds) set)
230 (cons (car binds) (lp (cdr binds))))
231 (else (lp (cdr binds))))))
232 (let ((u (lookup unref))
235 (c (lookup complex)))
236 ;; Bind "simple" bindings, and locations for complex
240 (append (map cadr s) (map cadr c))
241 (append (map car s) (map car c))
242 (append (map caddr s) (map (lambda (x) (make-void #f)) c))
243 ;; Bind lambdas using the fixpoint operator.
245 src (map cadr l) (map car l) (map caddr l)
249 ;; The right-hand-sides of the unreferenced
250 ;; bindings, for effect.
254 ;; No complex bindings, just emit the body.
257 ;; For letrec*, assign complex bindings in order, then the
261 (make-lexical-set #f (cadr c) (car c)
266 ;; Otherwise for plain letrec, evaluate the "complex"
267 ;; bindings, in a `let' to indicate that order doesn't
268 ;; matter, and bind to their variables.
270 (let ((tmps (map (lambda (x) (gensym)) c)))
272 #f (map cadr c) tmps (map caddr c)
278 (make-lexical-ref #f (cadr x) tmp)))
282 ((<let> src names gensyms vals body)
283 (let ((binds (map list gensyms names vals)))
285 (map (lambda (v) (assq v binds))
286 (lset-intersection eq? gensyms set)))
287 (let ((u (lookup unref))
289 (c (lookup complex)))
293 ;; unreferenced bindings, called for effect.
296 ;; unassigned lambdas use fix.
297 (make-fix src (map cadr l) (map car l) (map caddr l)
298 ;; and the "complex" bindings.
299 (make-let src (map cadr c) (map car c) (map caddr c)
306 ;;; eval: (put 'record-case 'scheme-indent-function 1)