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 (srfi srfi-9)
38 #:use-module (srfi srfi-26)
39 #:use-module (language cps)
40 #:use-module (language cps dfg)
41 #:export (convert-closures))
45 (define (convert-free-var var self self-known? free k)
46 "Convert one possibly free variable reference to a bound reference.
48 If @var{var} is free (i.e., present in @var{free},), it is replaced
49 by a closure reference via a @code{free-ref} primcall, and @var{k} is
50 called with the new var. Otherwise @var{var} is bound, so @var{k} is
51 called with @var{var}."
53 ((list-index (cut eq? <> var) free)
55 (match (cons self-known? free)
56 ;; A reference to the one free var of a well-known function.
58 ;; A reference to one of the two free vars in a well-known
61 (let-fresh (k*) (var*)
63 ($letk ((k* ($kargs (var*) (var*) ,(k var*))))
65 ($primcall (match free-idx (0 'car) (1 'cdr)) (self)))))))
67 (let-fresh (k* kidx) (idx var*)
69 ($letk ((kidx ($kargs ('idx) (idx)
70 ($letk ((k* ($kargs (var*) (var*) ,(k var*))))
74 ((not self-known?) 'free-ref)
75 ((<= free-idx #xff) 'vector-ref/immediate)
78 ($continue kidx #f ($const free-idx)))))))))
81 (define (convert-free-vars vars self self-known? free k)
82 "Convert a number of possibly free references to bound references.
83 @var{k} is called with the bound references, and should return the
88 (convert-free-var var self self-known? free
90 (convert-free-vars vars self self-known? free
92 (k (cons var vars)))))))))
94 (define (allocate-closure src name var label known? free body)
95 "Allocate a new closure."
96 (match (cons known? free)
100 ($letk ((k* ($kargs (name) (var) ,body)))
102 ($closure label (length free)))))))
104 ;; Well-known closure with no free variables; elide the
108 ;; Well-known closure with one free variable; the free var is the
109 ;; closure, and no new binding need be made.
112 ;; Well-known closure with two free variables; the closure is a
114 (let-fresh (kinit kfalse) (false)
116 ($letk ((kinit ($kargs (name) (var)
118 (kfalse ($kargs ('false) (false)
120 ($primcall 'cons (false false))))))
121 ($continue kfalse src ($const #f))))))
122 ;; Well-known callee with more than two free variables; the closure
125 (let ((nfree (length free)))
126 (let-fresh (kinit klen kfalse) (false len-var)
128 ($letk ((kinit ($kargs (name) (var) ,body))
129 (kfalse ($kargs ('false) (false)
131 ($kargs ('len) (len-var)
133 ($primcall (if (<= nfree #xff)
134 'make-vector/immediate
137 ($continue klen src ($const nfree))))))
138 ($continue kfalse src ($const #f)))))))))
140 (define (init-closure src var known? free
141 outer-self outer-known? outer-free body)
142 "Initialize the free variables @var{free} in a closure bound to
143 @var{var}, and continue with @var{body}. @var{outer-self} must be the
144 label of the outer procedure, where the initialization will be
145 performed, and @var{outer-free} is the list of free variables there."
146 (match (cons known? free)
147 ;; Well-known callee with no free variables; no initialization
150 ;; Well-known callee with one free variable; no initialization
153 ;; Well-known callee with two free variables; do a set-car! and
156 (let-fresh (kcar kcdr) ()
158 v0 outer-self outer-known? outer-free
161 ($letk ((kcar ($kargs () ()
163 v1 outer-self outer-known? outer-free
166 ($letk ((kcdr ($kargs () () ,body)))
168 ($primcall 'set-cdr! (var v1))))))))))
170 ($primcall 'set-car! (var v0)))))))))
171 ;; Otherwise residualize a sequence of vector-set! or free-set!,
172 ;; depending on whether the callee is well-known or not.
174 (fold (lambda (free idx body)
175 (let-fresh (k) (idxvar)
177 ($letk ((k ($kargs () () ,body)))
179 free outer-self outer-known? outer-free
182 ($letconst (('idx idxvar idx))
185 ((not known?) 'free-set!)
186 ((<= idx #xff) 'vector-set!/immediate)
188 (var idxvar free)))))))))))
191 (iota (length free))))))
193 (define (analyze-closures exp dfg)
194 "Compute the set of free variables for all $fun instances in
196 (let ((free-vars (make-hash-table))
197 (named-funs (make-hash-table))
198 (well-known-vars (make-bitvector (var-counter) #t)))
199 (define (add-named-fun! var cont)
200 (hashq-set! named-funs var cont))
201 (define (clear-well-known! var)
202 (bitvector-set! well-known-vars var #f))
203 (define (compute-well-known-labels)
204 (let ((bv (make-bitvector (label-counter) #f)))
208 (($ $cont label ($ $kfun src meta self))
209 (unless (equal? var self)
210 (bitvector-set! bv label
211 (and (bitvector-ref well-known-vars var)
212 (bitvector-ref well-known-vars self)))))))
216 (lset-union eq? a b))
217 (define (difference a b)
218 (lset-difference eq? a b))
219 (define (visit-cont cont bound)
221 (($ $cont label ($ $kargs names vars body))
222 (visit-term body (append vars bound)))
223 (($ $cont label ($ $kfun src meta self tail clause))
224 (add-named-fun! self cont)
225 (let ((free (if clause
226 (visit-cont clause (list self))
228 (hashq-set! free-vars label free)
229 (difference free bound)))
230 (($ $cont label ($ $kclause arity body alternate))
231 (let ((free (visit-cont body bound)))
233 (union (visit-cont alternate bound) free)
236 (define (visit-term term bound)
238 (($ $letk conts body)
239 (fold (lambda (cont free)
240 (union (visit-cont cont bound) free))
241 (visit-term body bound)
243 (($ $letrec names vars (($ $fun () cont) ...) body)
244 (let ((bound (append vars bound)))
245 (for-each add-named-fun! vars cont)
246 (fold (lambda (cont free)
247 (union (visit-cont cont bound) free))
248 (visit-term body bound)
250 (($ $continue k src ($ $fun () body))
251 (match (lookup-predecessors k dfg)
252 ((_) (match (lookup-cont k dfg)
253 (($ $kargs (name) (var))
254 (add-named-fun! var body))))
256 (visit-cont body bound))
257 (($ $continue k src exp)
258 (visit-exp exp bound))))
259 (define (visit-exp exp bound)
260 (define (adjoin var free)
261 (if (or (memq var bound) (memq var free))
265 ((or ($ $void) ($ $const) ($ $prim)) '())
267 (for-each clear-well-known! args)
268 (fold adjoin (adjoin proc '()) args))
269 (($ $primcall name args)
270 (for-each clear-well-known! args)
271 (fold adjoin '() args))
273 (for-each clear-well-known! args)
274 (fold adjoin '() args))
275 (($ $prompt escape? tag handler)
276 (clear-well-known! tag)
279 (let ((free (visit-cont exp '())))
281 (error "Expected no free vars in toplevel thunk" free exp))
282 (values free-vars named-funs (compute-well-known-labels)))))
284 (define (prune-free-vars free-vars named-funs well-known var-aliases)
285 (define (well-known? label)
286 (bitvector-ref well-known label))
287 (let ((eliminated (make-bitvector (label-counter) #f))
288 (label-aliases (make-vector (label-counter) #f)))
290 (let ((label (bit-position #t well-known label)))
292 (match (hashq-ref free-vars label)
293 ;; Mark all well-known closures that have no free variables
295 (() (bitvector-set! eliminated label #t))
296 ;; Replace well-known closures that have just one free
297 ;; variable by references to that free variable.
299 (vector-set! label-aliases label var))
302 ;; Iterative free variable elimination.
305 (define (adjoin elt list)
306 ;; Normally you wouldn't see duplicates in a free variable
307 ;; list, but with aliases that is possible.
308 (if (memq elt list) list (cons elt list)))
309 (define (filter-out-eliminated free)
313 (let lp ((var var) (alias-stack '()))
314 (match (hashq-ref named-funs var)
317 ((bitvector-ref eliminated label)
318 (filter-out-eliminated free))
319 ((vector-ref label-aliases label)
322 ((memq label alias-stack)
323 ;; We have found a set of mutually recursive
324 ;; well-known procedures, each of which only
325 ;; closes over one of the others. Mark them
326 ;; all for elimination.
327 (for-each (lambda (label)
328 (bitvector-set! eliminated label #t)
331 (filter-out-eliminated free))
333 (lp var (cons label alias-stack))))))
335 (adjoin var (filter-out-eliminated free)))))
336 (_ (adjoin var (filter-out-eliminated free))))))))
337 (hash-for-each-handle
342 (let ((orig-nfree (length free))
343 (free (filter-out-eliminated free)))
345 ;; If we managed to eliminate one or more free variables
346 ;; from a well-known function, it could be that we can
347 ;; eliminate or alias this function as well.
348 (when (and (well-known? label)
349 (< (length free) orig-nfree))
352 (bitvector-set! eliminated label #t)
355 (vector-set! label-aliases label var)
359 ;; Iterate to fixed point.
360 (when recurse? (lp))))
361 ;; Populate var-aliases from label-aliases.
362 (hash-for-each (lambda (var cont)
365 (let ((alias (vector-ref label-aliases label)))
367 (vector-set! var-aliases var alias))))))
370 (define (convert-one label fun free-vars named-funs well-known aliases)
371 (define (well-known? label)
372 (bitvector-ref well-known label))
374 ;; Load the closure for a known call. The callee may or may not be
375 ;; known at all call sites.
376 (define (convert-known-proc-call var label self self-known? free k)
377 ;; Well-known closures with one free variable are replaced at their
378 ;; use sites by uses of the one free variable. The use sites of a
379 ;; well-known closures are only in well-known proc calls, and in
380 ;; free lists of other closures. Here we handle the call case; the
381 ;; free list case is handled by prune-free-vars.
383 (let ((var* (vector-ref aliases var)))
387 (match (cons (well-known? label)
388 (hashq-ref free-vars label))
390 ;; Calling a well-known procedure with no free variables; pass #f
394 ($letk ((k* ($kargs (v*) (v*) ,(k v*))))
395 ($continue k* #f ($const #f))))))
397 ;; Calling a well-known procedure with one free variable; pass
398 ;; the free variable as the closure.
399 (convert-free-var (rename var) self self-known? free k))
401 (convert-free-var var self self-known? free k))))
403 (let ((free (hashq-ref free-vars label))
404 (self-known? (well-known? label))
405 (self (match fun (($ $kfun _ _ self) self))))
406 (define (visit-cont cont)
407 (rewrite-cps-cont cont
408 (($ $cont label ($ $kargs names vars body))
409 (label ($kargs names vars ,(visit-term body))))
410 (($ $cont label ($ $kfun src meta self tail clause))
411 (label ($kfun src meta self ,tail
412 ,(and clause (visit-cont clause)))))
413 (($ $cont label ($ $kclause arity body alternate))
414 (label ($kclause ,arity ,(visit-cont body)
415 ,(and alternate (visit-cont alternate)))))
417 (define (visit-term term)
419 (($ $letk conts body)
421 ($letk ,(map visit-cont conts) ,(visit-term body))))
424 (($ $letrec names vars funs body)
425 (let lp ((in (map list names vars funs))
426 (bindings (lambda (body) body))
427 (body (visit-term body)))
430 (((name var ($ $fun ()
432 ($ $cont kfun ($ $kfun src))))) . in)
433 (let ((fun-free (hashq-ref free-vars kfun)))
437 src name var kfun (well-known? kfun) fun-free
441 (well-known? kfun) fun-free self self-known? free
444 (($ $continue k src (or ($ $void) ($ $const) ($ $prim)))
447 (($ $continue k src ($ $fun () ($ $cont kfun)))
448 (let ((fun-free (hashq-ref free-vars kfun)))
449 (match (cons (well-known? kfun) fun-free)
452 ($continue k src ,(if known?
453 (build-cps-exp ($const #f))
454 (build-cps-exp ($closure kfun 0))))))
456 ;; A well-known closure of one free variable is replaced
457 ;; at each use with the free variable itself, so we don't
458 ;; need a binding at all; and yet, the continuation
459 ;; expects one value, so give it something. DCE should
462 ($continue k src ,(build-cps-exp ($const #f)))))
466 src #f var kfun (well-known? kfun) fun-free
469 (well-known? kfun) fun-free self self-known? free
470 (build-cps-term ($continue k src ($values (var)))))))))))
472 (($ $continue k src ($ $call proc args))
473 (match (hashq-ref named-funs proc)
475 (convert-known-proc-call
476 proc kfun self self-known? free
478 (convert-free-vars args self self-known? free
482 ($callk kfun proc args))))))))
484 (convert-free-vars (cons proc args) self self-known? free
489 ($call proc args)))))))))
491 (($ $continue k src ($ $primcall name args))
492 (convert-free-vars args self self-known? free
495 ($continue k src ($primcall name args))))))
497 (($ $continue k src ($ $values args))
498 (convert-free-vars args self self-known? free
501 ($continue k src ($values args))))))
503 (($ $continue k src ($ $prompt escape? tag handler))
504 (convert-free-var tag self self-known? free
508 ($prompt escape? tag handler))))))))
509 (visit-cont (build-cps-cont (label ,fun)))))
511 (define (convert-closures fun)
512 "Convert free reference in @var{exp} to primcalls to @code{free-ref},
513 and allocate and initialize flat closures."
514 (let ((dfg (compute-dfg fun)))
515 (with-fresh-name-state-from-dfg dfg
516 (call-with-values (lambda () (analyze-closures fun dfg))
517 (lambda (free-vars named-funs well-known)
518 (let ((labels (sort (hash-map->list (lambda (k v) k) free-vars) <))
519 (aliases (make-vector (var-counter) #f)))
520 (prune-free-vars free-vars named-funs well-known aliases)
523 ,(map (lambda (label)
524 (convert-one label (lookup-cont label dfg)
525 free-vars named-funs well-known aliases))