closure-conversion.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013, 2014 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Commentary:
  17. ;;;
  18. ;;; This pass converts a CPS term in such a way that no function has any
  19. ;;; free variables. Instead, closures are built explicitly with
  20. ;;; make-closure primcalls, and free variables are referenced through
  21. ;;; the closure.
  22. ;;;
  23. ;;; Closure conversion also removes any $letrec forms that contification
  24. ;;; did not handle. See (language cps) for a further discussion of
  25. ;;; $letrec.
  26. ;;;
  27. ;;; Code:
  28. (define-module (language cps closure-conversion)
  29. #:use-module (ice-9 match)
  30. #:use-module ((srfi srfi-1) #:select (fold
  31. lset-union lset-difference
  32. list-index))
  33. #:use-module (ice-9 receive)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (language cps)
  36. #:export (convert-closures))
  37. (define (union s1 s2)
  38. (lset-union eq? s1 s2))
  39. (define (difference s1 s2)
  40. (lset-difference eq? s1 s2))
  41. ;; bound := sym ...
  42. ;; free := sym ...
  43. (define (convert-free-var sym self bound k)
  44. "Convert one possibly free variable reference to a bound reference.
  45. If @var{sym} is free (i.e., not present in @var{bound},), it is replaced
  46. by a closure reference via a @code{free-ref} primcall, and @var{k} is
  47. called with the new var. Otherwise @var{sym} is bound, so @var{k} is
  48. called with @var{sym}.
  49. @var{k} should return two values: a term and a list of additional free
  50. values in the term."
  51. (if (memq sym bound)
  52. (k sym)
  53. (let-gensyms (k* sym*)
  54. (receive (exp free) (k sym*)
  55. (values (build-cps-term
  56. ($letk ((k* ($kargs (sym*) (sym*) ,exp)))
  57. ($continue k* #f ($primcall 'free-ref (self sym)))))
  58. (cons sym free))))))
  59. (define (convert-free-vars syms self bound k)
  60. "Convert a number of possibly free references to bound references.
  61. @var{k} is called with the bound references, and should return two
  62. values: the term and a list of additional free variables in the term."
  63. (match syms
  64. (() (k '()))
  65. ((sym . syms)
  66. (convert-free-var sym self bound
  67. (lambda (sym)
  68. (convert-free-vars syms self bound
  69. (lambda (syms)
  70. (k (cons sym syms)))))))))
  71. (define (init-closure src v free outer-self outer-bound body)
  72. "Initialize the free variables @var{free} in a closure bound to
  73. @var{v}, and continue with @var{body}. @var{outer-self} must be the
  74. label of the outer procedure, where the initialization will be
  75. performed, and @var{outer-bound} is the list of bound variables there."
  76. (fold (lambda (free idx body)
  77. (let-gensyms (k idxsym)
  78. (build-cps-term
  79. ($letk ((k ($kargs () () ,body)))
  80. ,(convert-free-var
  81. free outer-self outer-bound
  82. (lambda (free)
  83. (values (build-cps-term
  84. ($letconst (('idx idxsym idx))
  85. ($continue k src
  86. ($primcall 'free-set! (v idxsym free)))))
  87. '())))))))
  88. body
  89. free
  90. (iota (length free))))
  91. (define (cc* exps self bound)
  92. "Convert all free references in the list of expressions @var{exps} to
  93. bound references, and convert functions to flat closures. Returns two
  94. values: the transformed list, and a cumulative set of free variables."
  95. (let lp ((exps exps) (exps* '()) (free '()))
  96. (match exps
  97. (() (values (reverse exps*) free))
  98. ((exp . exps)
  99. (receive (exp* free*) (cc exp self bound)
  100. (lp exps (cons exp* exps*) (union free free*)))))))
  101. ;; Closure conversion.
  102. (define (cc exp self bound)
  103. "Convert all free references in @var{exp} to bound references, and
  104. convert functions to flat closures."
  105. (match exp
  106. (($ $letk conts body)
  107. (receive (conts free) (cc* conts self bound)
  108. (receive (body free*) (cc body self bound)
  109. (values (build-cps-term ($letk ,conts ,body))
  110. (union free free*)))))
  111. (($ $cont sym ($ $kargs names syms body))
  112. (receive (body free) (cc body self (append syms bound))
  113. (values (build-cps-cont (sym ($kargs names syms ,body)))
  114. free)))
  115. (($ $cont sym ($ $kentry self tail clauses))
  116. (receive (clauses free) (cc* clauses self (list self))
  117. (values (build-cps-cont (sym ($kentry self ,tail ,clauses)))
  118. free)))
  119. (($ $cont sym ($ $kclause arity body))
  120. (receive (body free) (cc body self bound)
  121. (values (build-cps-cont (sym ($kclause ,arity ,body)))
  122. free)))
  123. (($ $cont)
  124. ;; Other kinds of continuations don't bind values and don't have
  125. ;; bodies.
  126. (values exp '()))
  127. ;; Remove letrec.
  128. (($ $letrec names syms funs body)
  129. (let ((bound (append bound syms)))
  130. (receive (body free) (cc body self bound)
  131. (let lp ((in (map list names syms funs))
  132. (bindings (lambda (body) body))
  133. (body body)
  134. (free free))
  135. (match in
  136. (() (values (bindings body) free))
  137. (((name sym ($ $fun src meta () fun-body)) . in)
  138. (receive (fun-body fun-free) (cc fun-body #f '())
  139. (lp in
  140. (lambda (body)
  141. (let-gensyms (k)
  142. (build-cps-term
  143. ($letk ((k ($kargs (name) (sym) ,(bindings body))))
  144. ($continue k src
  145. ($fun src meta fun-free ,fun-body))))))
  146. (init-closure src sym fun-free self bound body)
  147. (union free (difference fun-free bound))))))))))
  148. (($ $continue k src
  149. (or ($ $void)
  150. ($ $const)
  151. ($ $prim)))
  152. (values exp '()))
  153. (($ $continue k src ($ $fun src* meta () body))
  154. (receive (body free) (cc body #f '())
  155. (match free
  156. (()
  157. (values (build-cps-term
  158. ($continue k src ($fun src* meta free ,body)))
  159. free))
  160. (_
  161. (values
  162. (let-gensyms (kinit v)
  163. (build-cps-term
  164. ($letk ((kinit ($kargs (v) (v)
  165. ,(init-closure
  166. src v free self bound
  167. (build-cps-term
  168. ($continue k src ($values (v))))))))
  169. ($continue kinit src ($fun src* meta free ,body)))))
  170. (difference free bound))))))
  171. (($ $continue k src ($ $call proc args))
  172. (convert-free-vars (cons proc args) self bound
  173. (match-lambda
  174. ((proc . args)
  175. (values (build-cps-term
  176. ($continue k src ($call proc args)))
  177. '())))))
  178. (($ $continue k src ($ $primcall name args))
  179. (convert-free-vars args self bound
  180. (lambda (args)
  181. (values (build-cps-term
  182. ($continue k src ($primcall name args)))
  183. '()))))
  184. (($ $continue k src ($ $values args))
  185. (convert-free-vars args self bound
  186. (lambda (args)
  187. (values (build-cps-term
  188. ($continue k src ($values args)))
  189. '()))))
  190. (($ $continue k src ($ $prompt escape? tag handler))
  191. (convert-free-var
  192. tag self bound
  193. (lambda (tag)
  194. (values (build-cps-term
  195. ($continue k src ($prompt escape? tag handler)))
  196. '()))))
  197. (_ (error "what" exp))))
  198. ;; Convert the slot arguments of 'free-ref' primcalls from symbols to
  199. ;; indices.
  200. (define (convert-to-indices body free)
  201. (define (free-index sym)
  202. (or (list-index (cut eq? <> sym) free)
  203. (error "free variable not found!" sym free)))
  204. (define (visit-term term)
  205. (rewrite-cps-term term
  206. (($ $letk conts body)
  207. ($letk ,(map visit-cont conts) ,(visit-term body)))
  208. (($ $continue k src ($ $primcall 'free-ref (closure sym)))
  209. ,(let-gensyms (idx)
  210. (build-cps-term
  211. ($letconst (('idx idx (free-index sym)))
  212. ($continue k src ($primcall 'free-ref (closure idx)))))))
  213. (($ $continue k src ($ $fun src* meta free body))
  214. ($continue k src
  215. ($fun src* meta free ,(convert-to-indices body free))))
  216. (($ $continue)
  217. ,term)))
  218. (define (visit-cont cont)
  219. (rewrite-cps-cont cont
  220. (($ $cont sym ($ $kargs names syms body))
  221. (sym ($kargs names syms ,(visit-term body))))
  222. (($ $cont sym ($ $kclause arity body))
  223. (sym ($kclause ,arity ,(visit-cont body))))
  224. ;; Other kinds of continuations don't bind values and don't have
  225. ;; bodies.
  226. (($ $cont)
  227. ,cont)))
  228. (rewrite-cps-cont body
  229. (($ $cont sym ($ $kentry self tail clauses))
  230. (sym ($kentry self ,tail ,(map visit-cont clauses))))))
  231. (define (convert-closures exp)
  232. "Convert free reference in @var{exp} to primcalls to @code{free-ref},
  233. and allocate and initialize flat closures."
  234. (match exp
  235. (($ $fun src meta () body)
  236. (receive (body free) (cc body #f '())
  237. (unless (null? free)
  238. (error "Expected no free vars in toplevel thunk" exp body free))
  239. (build-cps-exp
  240. ($fun src meta free ,(convert-to-indices body free)))))))