1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013, 2014 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
21 ;;; This pass converts a CPS term in such a way that no function has any
22 ;;; free variables. Instead, closures are built explicitly with
23 ;;; make-closure primcalls, and free variables are referenced through
26 ;;; Closure conversion also removes any $letrec forms that contification
27 ;;; did not handle. See (language cps) for a further discussion of
32 (define-module (language cps closure-conversion)
33 #:use-module (ice-9 match)
34 #:use-module ((srfi srfi-1) #:select (fold
35 lset-union lset-difference
37 #:use-module (ice-9 receive)
38 #:use-module (srfi srfi-26)
39 #:use-module (language cps)
40 #:export (convert-closures))
43 (lset-union eq? s1 s2))
45 (define (difference s1 s2)
46 (lset-difference eq? s1 s2))
51 (define (convert-free-var sym self bound k)
52 "Convert one possibly free variable reference to a bound reference.
54 If @var{sym} is free (i.e., not present in @var{bound},), it is replaced
55 by a closure reference via a @code{free-ref} primcall, and @var{k} is
56 called with the new var. Otherwise @var{sym} is bound, so @var{k} is
57 called with @var{sym}.
59 @var{k} should return two values: a term and a list of additional free
63 (let-fresh (k*) (sym*)
64 (receive (exp free) (k sym*)
65 (values (build-cps-term
66 ($letk ((k* ($kargs (sym*) (sym*) ,exp)))
67 ($continue k* #f ($primcall 'free-ref (self sym)))))
70 (define (convert-free-vars syms self bound k)
71 "Convert a number of possibly free references to bound references.
72 @var{k} is called with the bound references, and should return two
73 values: the term and a list of additional free variables in the term."
77 (convert-free-var sym self bound
79 (convert-free-vars syms self bound
81 (k (cons sym syms)))))))))
83 (define (init-closure src v free outer-self outer-bound body)
84 "Initialize the free variables @var{free} in a closure bound to
85 @var{v}, and continue with @var{body}. @var{outer-self} must be the
86 label of the outer procedure, where the initialization will be
87 performed, and @var{outer-bound} is the list of bound variables there."
88 (fold (lambda (free idx body)
89 (let-fresh (k) (idxsym)
91 ($letk ((k ($kargs () () ,body)))
93 free outer-self outer-bound
95 (values (build-cps-term
96 ($letconst (('idx idxsym idx))
98 ($primcall 'free-set! (v idxsym free)))))
102 (iota (length free))))
104 (define (cc* exps self bound)
105 "Convert all free references in the list of expressions @var{exps} to
106 bound references, and convert functions to flat closures. Returns two
107 values: the transformed list, and a cumulative set of free variables."
108 (let lp ((exps exps) (exps* '()) (free '()))
110 (() (values (reverse exps*) free))
112 (receive (exp* free*) (cc exp self bound)
113 (lp exps (cons exp* exps*) (union free free*)))))))
115 ;; Closure conversion.
116 (define (cc exp self bound)
117 "Convert all free references in @var{exp} to bound references, and
118 convert functions to flat closures."
120 (($ $letk conts body)
121 (receive (conts free) (cc* conts self bound)
122 (receive (body free*) (cc body self bound)
123 (values (build-cps-term ($letk ,conts ,body))
124 (union free free*)))))
126 (($ $cont sym ($ $kargs names syms body))
127 (receive (body free) (cc body self (append syms bound))
128 (values (build-cps-cont (sym ($kargs names syms ,body)))
131 (($ $cont sym ($ $kentry self tail clauses))
132 (receive (clauses free) (cc* clauses self (list self))
133 (values (build-cps-cont (sym ($kentry self ,tail ,clauses)))
136 (($ $cont sym ($ $kclause arity body))
137 (receive (body free) (cc body self bound)
138 (values (build-cps-cont (sym ($kclause ,arity ,body)))
142 ;; Other kinds of continuations don't bind values and don't have
147 (($ $letrec names syms funs body)
148 (let ((bound (append bound syms)))
149 (receive (body free) (cc body self bound)
150 (let lp ((in (map list names syms funs))
151 (bindings (lambda (body) body))
155 (() (values (bindings body) free))
156 (((name sym ($ $fun src meta () fun-body)) . in)
157 (receive (fun-body fun-free) (cc fun-body #f '())
162 ($letk ((k ($kargs (name) (sym) ,(bindings body))))
164 ($fun src meta fun-free ,fun-body))))))
165 (init-closure src sym fun-free self bound body)
166 (union free (difference fun-free bound))))))))))
174 (($ $continue k src ($ $fun src* meta () body))
175 (receive (body free) (cc body #f '())
178 (values (build-cps-term
179 ($continue k src ($fun src* meta free ,body)))
183 (let-fresh (kinit) (v)
185 ($letk ((kinit ($kargs (v) (v)
187 src v free self bound
189 ($continue k src ($values (v))))))))
190 ($continue kinit src ($fun src* meta free ,body)))))
191 (difference free bound))))))
193 (($ $continue k src ($ $call proc args))
194 (convert-free-vars (cons proc args) self bound
197 (values (build-cps-term
198 ($continue k src ($call proc args)))
201 (($ $continue k src ($ $callk k* proc args))
202 (convert-free-vars (cons proc args) self bound
205 (values (build-cps-term
206 ($continue k src ($callk k* proc args)))
209 (($ $continue k src ($ $primcall name args))
210 (convert-free-vars args self bound
212 (values (build-cps-term
213 ($continue k src ($primcall name args)))
216 (($ $continue k src ($ $values args))
217 (convert-free-vars args self bound
219 (values (build-cps-term
220 ($continue k src ($values args)))
223 (($ $continue k src ($ $prompt escape? tag handler))
227 (values (build-cps-term
228 ($continue k src ($prompt escape? tag handler)))
231 (_ (error "what" exp))))
233 ;; Convert the slot arguments of 'free-ref' primcalls from symbols to
235 (define (convert-to-indices body free)
236 (define (free-index sym)
237 (or (list-index (cut eq? <> sym) free)
238 (error "free variable not found!" sym free)))
239 (define (visit-term term)
240 (rewrite-cps-term term
241 (($ $letk conts body)
242 ($letk ,(map visit-cont conts) ,(visit-term body)))
243 (($ $continue k src ($ $primcall 'free-ref (closure sym)))
246 ($letconst (('idx idx (free-index sym)))
247 ($continue k src ($primcall 'free-ref (closure idx)))))))
248 (($ $continue k src ($ $fun src* meta free body))
250 ($fun src* meta free ,(convert-to-indices body free))))
253 (define (visit-cont cont)
254 (rewrite-cps-cont cont
255 (($ $cont sym ($ $kargs names syms body))
256 (sym ($kargs names syms ,(visit-term body))))
257 (($ $cont sym ($ $kclause arity body))
258 (sym ($kclause ,arity ,(visit-cont body))))
259 ;; Other kinds of continuations don't bind values and don't have
264 (rewrite-cps-cont body
265 (($ $cont sym ($ $kentry self tail clauses))
266 (sym ($kentry self ,tail ,(map visit-cont clauses))))))
268 (define (convert-closures exp)
269 "Convert free reference in @var{exp} to primcalls to @code{free-ref},
270 and allocate and initialize flat closures."
271 (with-fresh-name-state exp
273 (($ $fun src meta () body)
274 (receive (body free) (cc body #f '())
276 (error "Expected no free vars in toplevel thunk" exp body free))
278 ($fun src meta free ,(convert-to-indices body free))))))))