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 primitives)
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-primitive?)
38 ((<lexical-ref> gensym)
39 (not (memq gensym bound-vars)))
40 ((<conditional> test consequent alternate)
41 (and (simple-expression? test bound-vars simple-primitive?)
42 (simple-expression? consequent bound-vars simple-primitive?)
43 (simple-expression? alternate bound-vars simple-primitive?)))
45 (and (simple-expression? head bound-vars simple-primitive?)
46 (simple-expression? tail bound-vars simple-primitive?)))
47 ((<primcall> name args)
48 (and (simple-primitive? name)
49 ;; FIXME: check arity?
51 (simple-expression? x bound-vars simple-primitive?))
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 (let lp ((gensyms orig-gensyms) (vals vals)
95 (s '()) (l '()) (c '()))
98 ;; Unreferenced complex vars are still
99 ;; complex for letrec*. We need to update
100 ;; our algorithm to "Fixing letrec reloaded"
102 (values (if in-order?
103 (lset-difference eq? unref c)
110 ((memq (car gensyms) unref)
111 ;; See above note about unref and letrec*.
113 (not (lambda? (car vals)))
114 (not (simple-expression?
115 (car vals) orig-gensyms
116 effect+exception-free-primitive?)))
117 (lp (cdr gensyms) (cdr vals)
118 s l (cons (car gensyms) c))
119 (lp (cdr gensyms) (cdr vals)
121 ((memq (car gensyms) set)
122 (lp (cdr gensyms) (cdr vals)
123 s l (cons (car gensyms) c)))
124 ((lambda? (car vals))
125 (lp (cdr gensyms) (cdr vals)
126 s (cons (car gensyms) l) c))
128 (car vals) orig-gensyms
130 effect+exception-free-primitive?
131 effect-free-primitive?))
132 ;; For letrec*, we can't consider e.g. `car' to be
133 ;; "simple", as it could raise an exception. Hence
134 ;; effect+exception-free-primitive? above.
135 (lp (cdr gensyms) (cdr vals)
136 (cons (car gensyms) s) l c))
138 (lp (cdr gensyms) (cdr vals)
139 s l (cons (car gensyms) c))))))
140 ((<let> (orig-gensyms gensyms) vals)
141 ;; The point is to compile let-bound lambdas as
142 ;; efficiently as we do letrec-bound lambdas, so
143 ;; we use the same algorithm for analyzing the
144 ;; gensyms. There is no problem recursing into the
145 ;; bindings after the let, because all variables
146 ;; have been renamed.
147 (let lp ((gensyms orig-gensyms) (vals vals)
148 (s '()) (l '()) (c '()))
157 ((memq (car gensyms) unref)
158 (lp (cdr gensyms) (cdr vals)
160 ((memq (car gensyms) set)
161 (lp (cdr gensyms) (cdr vals)
162 s l (cons (car gensyms) c)))
163 ((and (lambda? (car vals))
164 (not (memq (car gensyms) set)))
165 (lp (cdr gensyms) (cdr vals)
166 s (cons (car gensyms) l) c))
167 ;; There is no difference between simple and
168 ;; complex, for the purposes of let. Just lump
169 ;; them all into complex.
171 (lp (cdr gensyms) (cdr vals)
172 s l (cons (car gensyms) c))))))
174 (values unref ref set simple lambda* complex))))
181 (values unref simple lambda* complex)))
183 (define (make-seq* src head tail)
187 ((<lexical-ref>) tail)
189 (else (make-seq src head tail))))
191 (define (list->seq* loc exps)
192 (if (null? (cdr exps))
194 (let lp ((exps (cdr exps)) (effects (list (car exps))))
195 (if (null? (cdr exps))
197 (fold (lambda (exp tail) (make-seq* #f exp tail))
201 (lp (cdr exps) (cons (car exps) effects))))))
203 (define (fix-letrec! x)
204 (let-values (((unref simple lambda* complex) (partition-vars x)))
209 ;; Sets to unreferenced variables may be replaced by their
210 ;; expression, called for effect.
211 ((<lexical-set> gensym exp)
212 (if (memq gensym unref)
213 (make-seq* #f exp (make-void #f))
216 ((<letrec> src in-order? names gensyms vals body)
217 (let ((binds (map list gensyms names vals)))
218 ;; The bindings returned by this function need to appear in the same
219 ;; order that they appear in the letrec.
221 (let lp ((binds binds))
224 ((memq (caar binds) set)
225 (cons (car binds) (lp (cdr binds))))
226 (else (lp (cdr binds))))))
227 (let ((u (lookup unref))
230 (c (lookup complex)))
231 ;; Bind "simple" bindings, and locations for complex
235 (append (map cadr s) (map cadr c))
236 (append (map car s) (map car c))
237 (append (map caddr s) (map (lambda (x) (make-void #f)) c))
238 ;; Bind lambdas using the fixpoint operator.
240 src (map cadr l) (map car l) (map caddr l)
244 ;; The right-hand-sides of the unreferenced
245 ;; bindings, for effect.
249 ;; No complex bindings, just emit the body.
252 ;; For letrec*, assign complex bindings in order, then the
256 (make-lexical-set #f (cadr c) (car c)
261 ;; Otherwise for plain letrec, evaluate the "complex"
262 ;; bindings, in a `let' to indicate that order doesn't
263 ;; matter, and bind to their variables.
265 (let ((tmps (map (lambda (x) (gensym)) c)))
267 #f (map cadr c) tmps (map caddr c)
273 (make-lexical-ref #f (cadr x) tmp)))
277 ((<let> src names gensyms vals body)
278 (let ((binds (map list gensyms names vals)))
280 (map (lambda (v) (assq v binds))
281 (lset-intersection eq? gensyms set)))
282 (let ((u (lookup unref))
284 (c (lookup complex)))
288 ;; unreferenced bindings, called for effect.
291 ;; unassigned lambdas use fix.
292 (make-fix src (map cadr l) (map car l) (map caddr l)
293 ;; and the "complex" bindings.
294 (make-let src (map cadr c) (map car c) (map caddr c)
301 ;;; eval: (put 'record-case 'scheme-indent-function 1)