1 ;;; transformation of letrec into simpler forms
3 ;; Copyright (C) 2009 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)
38 ((<lexical-ref> gensym)
39 (not (memq gensym bound-vars)))
40 ((<conditional> test then else)
41 (and (simple-expression? test bound-vars)
42 (simple-expression? then bound-vars)
43 (simple-expression? else bound-vars)))
45 (and-map (lambda (x) (simple-expression? x bound-vars))
47 ((<application> proc args)
48 (and (primitive-ref? proc)
49 (effect-free-primitive? (primitive-ref-name proc))
50 (and-map (lambda (x) (simple-expression? x bound-vars))
54 (define (partition-vars x)
56 (((unref ref set simple lambda* complex)
58 (lambda (x unref ref set simple lambda* complex)
60 ((<lexical-ref> gensym)
61 (values (delq gensym unref)
62 (lset-adjoin eq? ref gensym)
67 ((<lexical-set> gensym)
70 (lset-adjoin eq? set gensym)
75 (values (append vars unref)
82 (values (append vars unref)
89 (values unref ref set simple lambda* complex))))
90 (lambda (x unref ref set simple lambda* complex)
92 ((<letrec> (orig-vars vars) vals)
93 (let lp ((vars orig-vars) (vals vals)
94 (s '()) (l '()) (c '()))
103 ((memq (car vars) unref)
104 (lp (cdr vars) (cdr vals)
106 ((memq (car vars) set)
107 (lp (cdr vars) (cdr vals)
108 s l (cons (car vars) c)))
109 ((lambda? (car vals))
110 (lp (cdr vars) (cdr vals)
111 s (cons (car vars) l) c))
112 ((simple-expression? (car vals) orig-vars)
113 (lp (cdr vars) (cdr vals)
114 (cons (car vars) s) l c))
116 (lp (cdr vars) (cdr vals)
117 s l (cons (car vars) c))))))
118 ((<let> (orig-vars vars) vals)
119 ;; The point is to compile let-bound lambdas as
120 ;; efficiently as we do letrec-bound lambdas, so
121 ;; we use the same algorithm for analyzing the
122 ;; vars. There is no problem recursing into the
123 ;; bindings after the let, because all variables
124 ;; have been renamed.
125 (let lp ((vars orig-vars) (vals vals)
126 (s '()) (l '()) (c '()))
135 ((memq (car vars) unref)
136 (lp (cdr vars) (cdr vals)
138 ((memq (car vars) set)
139 (lp (cdr vars) (cdr vals)
140 s l (cons (car vars) c)))
141 ((and (lambda? (car vals))
142 (not (memq (car vars) set)))
143 (lp (cdr vars) (cdr vals)
144 s (cons (car vars) l) c))
145 ;; There is no difference between simple and
146 ;; complex, for the purposes of let. Just lump
147 ;; them all into complex.
149 (lp (cdr vars) (cdr vals)
150 s l (cons (car vars) c))))))
152 (values unref ref set simple lambda* complex))))
159 (values unref simple lambda* complex)))
161 (define (fix-letrec! x)
162 (let-values (((unref simple lambda* complex) (partition-vars x)))
167 ;; Sets to unreferenced variables may be replaced by their
168 ;; expression, called for effect.
169 ((<lexical-set> gensym exp)
170 (if (memq gensym unref)
171 (make-sequence #f (list exp (make-void #f)))
174 ((<letrec> src names vars vals body)
175 (let ((binds (map list vars names vals)))
177 (map (lambda (v) (assq v binds))
178 (lset-intersection eq? vars set)))
179 (let ((u (lookup unref))
182 (c (lookup complex)))
183 ;; Bind "simple" bindings, and locations for complex
187 (append (map cadr s) (map cadr c))
188 (append (map car s) (map car c))
189 (append (map caddr s) (map (lambda (x) (make-void #f)) c))
190 ;; Bind lambdas using the fixpoint operator.
192 src (map cadr l) (map car l) (map caddr l)
196 ;; The right-hand-sides of the unreferenced
197 ;; bindings, for effect.
200 ;; No complex bindings, just emit the body.
203 ;; Evaluate the the "complex" bindings, in a `let' to
204 ;; indicate that order doesn't matter, and bind to
206 (let ((tmps (map (lambda (x) (gensym)) c)))
208 #f (map cadr c) tmps (map caddr c)
214 (make-lexical-ref #f (cadr x) tmp)))
216 ;; Finally, the body.
219 ((<let> src names vars vals body)
220 (let ((binds (map list vars names vals)))
222 (map (lambda (v) (assq v binds))
223 (lset-intersection eq? vars set)))
224 (let ((u (lookup unref))
226 (c (lookup complex)))
230 ;; unreferenced bindings, called for effect.
233 ;; unassigned lambdas use fix.
234 (make-fix src (map cadr l) (map car l) (map caddr l)
235 ;; and the "complex" bindings.
236 (make-let src (map cadr c) (map car c) (map caddr c)