1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013 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 ;;; Contification is a pass that turns $fun instances into $cont
22 ;;; instances if all calls to the $fun return to the same continuation.
23 ;;; This is a more rigorous variant of our old "fixpoint labels
24 ;;; allocation" optimization.
26 ;;; See Kennedy's "Compiling with Continuations, Continued", and Fluet
27 ;;; and Weeks's "Contification using Dominators".
31 (define-module (language cps contification)
32 #:use-module (ice-9 match)
33 #:use-module ((srfi srfi-1) #:select (concatenate))
34 #:use-module (srfi srfi-26)
35 #:use-module (language cps)
36 #:use-module (language cps dfg)
37 #:use-module (language cps primitives)
38 #:use-module (language rtl)
42 (let* ((dfg (compute-dfg fun))
43 (cont-table (dfg-cont-table dfg))
46 (define (subst-call! sym arities body-ks)
47 (set! call-substs (acons sym (map cons arities body-ks) call-substs)))
48 (define (subst-return! old-tail new-tail)
49 (set! cont-substs (acons old-tail new-tail cont-substs)))
50 (define (lookup-return-cont k)
51 (or (assq-ref cont-substs k) k))
53 (define (contify-call proc args)
54 (and=> (assq-ref call-substs proc)
56 (let lp ((clauses clauses))
58 (() (error "invalid contification"))
59 (((($ $arity req () #f () #f) . k) . clauses)
60 (if (= (length req) (length args))
62 ($continue k ($values args)))
64 ((_ . clauses) (lp clauses)))))))
66 ;; If K is a continuation that binds one variable, and it has only
67 ;; one predecessor, return that variable.
68 (define (bound-symbol k)
69 (match (lookup-cont k cont-table)
71 (match (lookup-uses k dfg)
73 ;; K has one predecessor, the one that defined SYM.
78 (define (contify-fun term-k sym self tail arities bodies)
80 (list sym) (list self) (list tail)
81 (list arities) (list bodies)))
83 (define (contify-funs term-k syms selfs tails arities bodies)
84 ;; Are the given args compatible with any of the arities?
85 (define (applicable? proc args)
87 (($ $arity req () #f () #f)
88 (= (length args) (length req)))
90 (assq-ref (map cons syms arities) proc)))
92 ;; If the use of PROC in continuation USE is a call to PROC that
93 ;; is compatible with one of the procedure's arities, return the
94 ;; target continuation. Otherwise return #f.
95 (define (call-target use proc)
96 (match (find-call (lookup-cont use cont-table))
97 (($ $continue k ($ $call proc* args))
98 (and (eq? proc proc*) (not (memq proc args)) (applicable? proc args)
103 (and-map null? (map (cut lookup-uses <> dfg) selfs))
104 (and=> (let visit-syms ((syms syms) (k #f))
108 (let visit-uses ((uses (lookup-uses sym dfg)) (k k))
110 (() (visit-syms syms k))
112 (and=> (call-target use sym)
115 ((memq k* tails) (visit-uses uses k))
116 ((not k) (visit-uses uses k*))
117 ((eq? k k*) (visit-uses uses k))
120 ;; We have a common continuation, so we contify: mark
121 ;; all SYMs for replacement in calls, and mark the tail
122 ;; continuations for replacement by K.
123 (for-each (lambda (sym tail arities bodies)
124 (for-each (cut lift-definition! <> term-k dfg)
126 (subst-call! sym arities bodies)
127 (subst-return! tail k))
128 syms tails arities bodies)
131 ;; This is a first cut at a contification algorithm. It contifies
132 ;; non-recursive functions that only have positional arguments.
133 (define (visit-fun term)
134 (rewrite-cps-exp term
135 (($ $fun meta free body)
136 ($fun meta free ,(visit-cont body)))))
137 (define (visit-cont cont)
138 (rewrite-cps-cont cont
140 ($ $kargs (name) (and sym (? (cut assq <> call-substs)))
142 (sym src ($kargs () () ,(visit-term body sym))))
143 (($ $cont sym src ($ $kargs names syms body))
144 (sym src ($kargs names syms ,(visit-term body sym))))
145 (($ $cont sym src ($ $kentry self tail clauses))
146 (sym src ($kentry self ,tail ,(map visit-cont clauses))))
147 (($ $cont sym src ($ $kclause arity body))
148 (sym src ($kclause ,arity ,(visit-cont body))))
151 (define (visit-term term term-k)
153 (($ $letk conts body)
154 ;; Visit the body first, so we visit depth-first.
155 (let ((body (visit-term body term-k)))
157 ($letk ,(map visit-cont conts) ,body))))
158 (($ $letrec names syms funs body)
159 (define (split-components nsf)
160 ;; FIXME: Compute strongly-connected components. Currently
161 ;; we just put non-recursive functions in their own
162 ;; components, and lump everything else in the remaining
164 (define (recursive? k)
165 (or-map (cut variable-used-in? <> k dfg) syms))
166 (let lp ((nsf nsf) (rec '()))
172 (((and elt (n s ($ $fun meta free ($ $cont kentry))))
174 (if (recursive? kentry)
175 (lp nsf (cons elt rec))
176 (cons (list elt) (lp nsf rec)))))))
177 (define (visit-components components)
179 (() (visit-term body term-k))
180 ((((name sym fun) ...) . components)
185 ($ $cont tail-k _ ($ $ktail))
186 (($ $cont _ _ ($ $kclause arity
187 (and body ($ $cont body-k))))
190 (if (contify-funs term-k sym self tail-k arity body-k)
191 (let ((body* (visit-components components)))
193 ($letk ,(map visit-cont (concatenate body))
197 ($letrec name sym (map visit-fun fun)
198 ,(visit-components components))))))))))
199 (visit-components (split-components (map list names syms funs))))
201 (let ((k* (lookup-return-cont k)))
203 (rewrite-cps-term exp
204 (($ $fun) ($continue k* ,(visit-fun exp)))
205 (($ $primcall 'return (val))
207 (build-cps-term ($continue k* ,exp))
208 (build-cps-term ($continue k* ($values (val))))))
209 (($ $primcall 'return-values vals)
211 (build-cps-term ($continue k* ,exp))
212 (build-cps-term ($continue k* ($values vals)))))
213 (_ ($continue k* ,exp))))
218 ($ $cont tail-k _ ($ $ktail))
219 (($ $cont _ _ ($ $kclause arity
220 (and body ($ $cont body-k))))
222 (if (and=> (bound-symbol k*)
224 (contify-fun term-k sym self tail-k arity body-k)))
226 ($letk ,(map visit-cont body)
227 ($continue k* ($values ()))))
230 (or (contify-call proc args)
234 (let ((fun (visit-fun fun)))
235 (if (null? call-substs)
237 ;; Iterate to fixed point.