closure-conversion.scm 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013-2021 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 as heap
  20. ;;; objects, and free variables are referenced through the closure.
  21. ;;;
  22. ;;; Closure conversion also removes any $rec expressions that
  23. ;;; contification did not handle. See (language cps) for a further
  24. ;;; discussion of $rec.
  25. ;;;
  26. ;;; Before closure conversion, function self variables are always bound.
  27. ;;; After closure conversion, well-known functions with no free
  28. ;;; variables may have no self reference.
  29. ;;;
  30. ;;; Code:
  31. (define-module (language cps closure-conversion)
  32. #:use-module (ice-9 match)
  33. #:use-module ((srfi srfi-1) #:select (fold
  34. filter-map
  35. ))
  36. #:use-module (srfi srfi-11)
  37. #:use-module (system base types internal)
  38. #:use-module (language cps)
  39. #:use-module (language cps utils)
  40. #:use-module (language cps with-cps)
  41. #:use-module (language cps intmap)
  42. #:use-module (language cps intset)
  43. #:export (convert-closures))
  44. (define (compute-program-body functions)
  45. (intmap-fold (lambda (label body out) (intset-union body out))
  46. functions
  47. empty-intset))
  48. (define (filter-reachable conts functions)
  49. (intmap-select conts (compute-program-body functions)))
  50. (define (compute-non-operator-uses conts)
  51. (persistent-intset
  52. (intmap-fold
  53. (lambda (label cont uses)
  54. (define (add-use var uses) (intset-add! uses var))
  55. (define (add-uses vars uses)
  56. (match vars
  57. (() uses)
  58. ((var . vars) (add-uses vars (add-use var uses)))))
  59. (match cont
  60. (($ $kargs _ _ ($ $continue _ _ exp))
  61. (match exp
  62. ((or ($ $const) ($ $prim) ($ $fun) ($ $rec)) uses)
  63. (($ $values args)
  64. (add-uses args uses))
  65. (($ $call proc args)
  66. (add-uses args uses))
  67. (($ $callk label proc args)
  68. (let ((uses (add-uses args uses)))
  69. (if proc
  70. (add-use proc uses)
  71. uses)))
  72. (($ $primcall name param args)
  73. (add-uses args uses))))
  74. (($ $kargs _ _ ($ $branch kf kt src op param args))
  75. (add-uses args uses))
  76. (($ $kargs _ _ ($ $switch kf kt* src arg))
  77. (add-use arg uses))
  78. (($ $kargs _ _ ($ $prompt k kh src escape? tag))
  79. (add-use tag uses))
  80. (($ $kargs _ _ ($ $throw src op param args))
  81. (add-uses args uses))
  82. (_ uses)))
  83. conts
  84. empty-intset)))
  85. (define (compute-function-names conts functions)
  86. "Compute a map of FUN-LABEL->BOUND-VAR... for each labelled function
  87. whose bound vars we know."
  88. (define (add-named-fun var kfun out)
  89. (let ((self (match (intmap-ref conts kfun)
  90. (($ $kfun src meta self) self))))
  91. (intmap-add out kfun (intset var self))))
  92. (intmap-fold
  93. (lambda (label body out)
  94. (let* ((conts (intmap-select conts body))
  95. (single (compute-singly-referenced-labels conts)))
  96. (intmap-fold
  97. (lambda (label cont out)
  98. (match cont
  99. (($ $kargs _ _ ($ $continue k _ ($ $fun kfun)))
  100. (if (intset-ref single k)
  101. (match (intmap-ref conts k)
  102. (($ $kargs (_) (var)) (add-named-fun var kfun out))
  103. (_ out))
  104. out))
  105. (($ $kargs _ _ ($ $continue k _ ($ $rec _ vars (($ $fun kfun) ...))))
  106. (unless (intset-ref single k)
  107. (error "$rec continuation has multiple predecessors??"))
  108. (fold add-named-fun out vars kfun))
  109. (_ out)))
  110. conts
  111. out)))
  112. functions
  113. empty-intmap))
  114. (define (compute-well-known-functions conts bound->label)
  115. "Compute a set of labels indicating the well-known functions in
  116. @var{conts}. A well-known function is a function whose bound names we
  117. know and which is never used in a non-operator position."
  118. (intset-subtract
  119. (persistent-intset
  120. (intmap-fold (lambda (bound label candidates)
  121. (intset-add! candidates label))
  122. bound->label
  123. empty-intset))
  124. (persistent-intset
  125. (intset-fold (lambda (var not-well-known)
  126. (match (intmap-ref bound->label var (lambda (_) #f))
  127. (#f not-well-known)
  128. (label (intset-add! not-well-known label))))
  129. (compute-non-operator-uses conts)
  130. empty-intset))))
  131. (define (intset-cons i set)
  132. (intset-add set i))
  133. (define (compute-shared-closures conts well-known)
  134. "Compute a map LABEL->VAR indicating the sets of functions that will
  135. share a closure. If a functions's label is in the map, it is shared.
  136. The entries indicate the var of the shared closure, which will be one of
  137. the bound vars of the closure."
  138. (intmap-fold
  139. (lambda (label cont out)
  140. (match cont
  141. (($ $kargs _ _
  142. ($ $continue _ _ ($ $rec names vars (($ $fun kfuns) ...))))
  143. ;; The split-rec pass should have ensured that this $rec forms a
  144. ;; strongly-connected component, so the free variables from all of
  145. ;; the functions will be alive as long as one of the closures is
  146. ;; alive. For that reason we can consider storing all free
  147. ;; variables in one closure and sharing it.
  148. (let* ((kfuns-set (fold intset-cons empty-intset kfuns))
  149. (unknown-kfuns (intset-subtract kfuns-set well-known)))
  150. (cond
  151. ((or (eq? empty-intset kfuns-set) (trivial-intset kfuns-set))
  152. ;; There is only zero or one function bound here. Trivially
  153. ;; shared already.
  154. out)
  155. ((eq? empty-intset unknown-kfuns)
  156. ;; All functions are well-known; we can share a closure. Use
  157. ;; the first bound variable.
  158. (let ((closure (car vars)))
  159. (intset-fold (lambda (kfun out)
  160. (intmap-add out kfun closure))
  161. kfuns-set out)))
  162. ((trivial-intset unknown-kfuns)
  163. => (lambda (unknown-kfun)
  164. ;; Only one function is not-well-known. Use that
  165. ;; function's closure as the shared closure.
  166. (let ((closure (assq-ref (map cons kfuns vars) unknown-kfun)))
  167. (intset-fold (lambda (kfun out)
  168. (intmap-add out kfun closure))
  169. kfuns-set out))))
  170. (else
  171. ;; More than one not-well-known function means we need more
  172. ;; than one proper closure, so we can't share.
  173. out))))
  174. (_ out)))
  175. conts
  176. empty-intmap))
  177. (define* (rewrite-shared-closure-calls cps functions label->bound shared kfun)
  178. "Rewrite CPS such that every call to a function with a shared closure
  179. instead is a $callk to that label, but passing the shared closure as the
  180. proc argument. For recursive calls, use the appropriate 'self'
  181. variable, if possible. Also rewrite uses of the non-well-known but
  182. shared closures to use the appropriate 'self' variable, if possible."
  183. ;; env := var -> (var . label)
  184. (define (visit-fun kfun cps env)
  185. (define (subst var)
  186. (match (intmap-ref env var (lambda (_) #f))
  187. (#f var)
  188. ((var . label) var)))
  189. (define (visit-exp exp)
  190. (rewrite-exp exp
  191. ((or ($ $const) ($ $prim)) ,exp)
  192. (($ $call proc args)
  193. ,(let ((args (map subst args)))
  194. (rewrite-exp (intmap-ref env proc (lambda (_) #f))
  195. (#f ($call proc ,args))
  196. ((closure . label) ($callk label closure ,args)))))
  197. (($ $callk label proc args)
  198. ($callk label (and proc (subst proc)) ,(map subst args)))
  199. (($ $primcall name param args)
  200. ($primcall name param ,(map subst args)))
  201. (($ $values args)
  202. ($values ,(map subst args)))))
  203. (define (visit-term term)
  204. (rewrite-term term
  205. (($ $continue k src exp)
  206. ($continue k src ,(visit-exp exp)))
  207. (($ $branch kf kt src op param args)
  208. ($branch kf kt src op param ,(map subst args)))
  209. (($ $switch kf kt* src arg)
  210. ($switch kf kt* src (subst arg)))
  211. (($ $prompt k kh src escape? tag)
  212. ($prompt k kh src escape? (subst tag)))
  213. (($ $throw src op param args)
  214. ($throw src op param ,(map subst args)))))
  215. (define (visit-rec labels vars cps)
  216. (define (compute-env label bound self rec-bound rec-labels env)
  217. (define (add-bound-var bound label env)
  218. (intmap-add env bound (cons self label) (lambda (old new) new)))
  219. (if (intmap-ref shared label (lambda (_) #f))
  220. ;; Within a function with a shared closure, rewrite
  221. ;; references to bound vars to use the "self" var.
  222. (fold add-bound-var env rec-bound rec-labels)
  223. ;; Otherwise be sure to use "self" references in any
  224. ;; closure.
  225. (add-bound-var bound label env)))
  226. (fold (lambda (label var cps)
  227. (match (intmap-ref cps label)
  228. (($ $kfun src meta self)
  229. (visit-fun label cps
  230. (compute-env label var self vars labels env)))))
  231. cps labels vars))
  232. (define (visit-cont label cps)
  233. (match (intmap-ref cps label)
  234. (($ $kargs names vars
  235. ($ $continue k src ($ $fun label)))
  236. (visit-fun label cps env))
  237. (($ $kargs _ _
  238. ($ $continue k src ($ $rec names vars (($ $fun labels) ...))))
  239. (visit-rec labels vars cps))
  240. (($ $kargs names vars term)
  241. (with-cps cps
  242. (setk label ($kargs names vars ,(visit-term term)))))
  243. (_ cps)))
  244. (intset-fold visit-cont (intmap-ref functions kfun) cps))
  245. ;; Initial environment is bound-var -> (shared-var . label) map for
  246. ;; functions with shared closures.
  247. (let ((env (intmap-fold (lambda (label shared env)
  248. (intset-fold (lambda (bound env)
  249. (intmap-add env bound
  250. (cons shared label)))
  251. (intset-remove
  252. (intmap-ref label->bound label)
  253. (match (intmap-ref cps label)
  254. (($ $kfun src meta self) self)))
  255. env))
  256. shared
  257. empty-intmap)))
  258. (persistent-intmap (visit-fun kfun cps env))))
  259. (define (compute-free-vars conts kfun shared)
  260. "Compute a FUN-LABEL->FREE-VAR... map describing all free variable
  261. references."
  262. (define (add-def var defs) (intset-add! defs var))
  263. (define (add-defs vars defs)
  264. (match vars
  265. (() defs)
  266. ((var . vars) (add-defs vars (add-def var defs)))))
  267. (define (add-use var uses)
  268. (intset-add! uses var))
  269. (define (add-uses vars uses)
  270. (match vars
  271. (() uses)
  272. ((var . vars) (add-uses vars (add-use var uses)))))
  273. (define (visit-nested-funs body)
  274. (intset-fold
  275. (lambda (label out)
  276. (match (intmap-ref conts label)
  277. (($ $kargs _ _ ($ $continue _ _ ($ $fun kfun)))
  278. (intmap-union out (visit-fun kfun)))
  279. ;; Convention is that functions not bound by $fun / $rec and
  280. ;; thus reachable only via $callk and such have no free
  281. ;; variables.
  282. (($ $kargs _ _ ($ $continue _ _
  283. ($ $rec _ _ (($ $fun labels) ...))))
  284. (let* ((out (fold (lambda (kfun out)
  285. (intmap-union out (visit-fun kfun)))
  286. out labels))
  287. (free (fold (lambda (kfun free)
  288. (intset-union free (intmap-ref out kfun)))
  289. empty-intset labels)))
  290. (fold (lambda (kfun out)
  291. ;; For functions that share a closure, the free
  292. ;; variables for one will be the union of the free
  293. ;; variables for all.
  294. (if (intmap-ref shared kfun (lambda (_) #f))
  295. (intmap-replace out kfun free)
  296. out))
  297. out
  298. labels)))
  299. (_ out)))
  300. body
  301. empty-intmap))
  302. (define (visit-fun kfun)
  303. (let* ((body (compute-function-body conts kfun))
  304. (free (visit-nested-funs body)))
  305. (call-with-values
  306. (lambda ()
  307. (intset-fold
  308. (lambda (label defs uses)
  309. (match (intmap-ref conts label)
  310. (($ $kargs names vars term)
  311. (values
  312. (add-defs vars defs)
  313. (match term
  314. (($ $continue k src exp)
  315. (match exp
  316. ((or ($ $const) ($ $prim)) uses)
  317. (($ $fun kfun)
  318. (intset-union (persistent-intset uses)
  319. (intmap-ref free kfun)))
  320. (($ $rec names vars (($ $fun kfun) ...))
  321. (fold (lambda (kfun uses)
  322. (intset-union (persistent-intset uses)
  323. (intmap-ref free kfun)))
  324. uses kfun))
  325. (($ $values args)
  326. (add-uses args uses))
  327. (($ $call proc args)
  328. (add-use proc (add-uses args uses)))
  329. (($ $callk label proc args)
  330. (let ((uses (add-uses args uses)))
  331. (if proc
  332. (add-use proc uses)
  333. uses)))
  334. (($ $primcall name param args)
  335. (add-uses args uses))))
  336. (($ $branch kf kt src op param args)
  337. (add-uses args uses))
  338. (($ $switch kf kt* src arg)
  339. (add-use arg uses))
  340. (($ $prompt k kh src escape? tag)
  341. (add-use tag uses))
  342. (($ $throw src op param args)
  343. (add-uses args uses)))))
  344. (($ $kfun src meta self)
  345. (values (if self (add-def self defs) defs) uses))
  346. (_ (values defs uses))))
  347. body empty-intset empty-intset))
  348. (lambda (defs uses)
  349. (intmap-add free kfun (intset-subtract
  350. (persistent-intset uses)
  351. (persistent-intset defs)))))))
  352. ;; Ensure that functions only reachable by $callk are present in the
  353. ;; free-vars map, albeit with empty-intset. Note that if front-ends
  354. ;; start emitting $callk to targets with free variables, we will need
  355. ;; to do a better job here!
  356. (define (ensure-all-functions-have-free-vars free-vars)
  357. (intmap-fold
  358. (lambda (label cont out)
  359. (match cont
  360. (($ $kfun)
  361. (intmap-add out label empty-intset intset-union))
  362. (_ out)))
  363. conts
  364. free-vars))
  365. (ensure-all-functions-have-free-vars (visit-fun kfun)))
  366. (define (eliminate-closure? label free-vars)
  367. (eq? (intmap-ref free-vars label) empty-intset))
  368. (define (closure-label label shared bound->label)
  369. (cond
  370. ((intmap-ref shared label (lambda (_) #f))
  371. => (lambda (closure)
  372. (intmap-ref bound->label closure)))
  373. (else label)))
  374. (define (closure-alias label well-known free-vars)
  375. (and (intset-ref well-known label)
  376. (trivial-intset (intmap-ref free-vars label))))
  377. (define (prune-free-vars free-vars bound->label well-known shared)
  378. "Given the label->bound-var map @var{free-vars}, remove free variables
  379. that are known functions with zero free variables, and replace
  380. references to well-known functions with one free variable with that free
  381. variable, until we reach a fixed point on the free-vars map."
  382. (define (prune-free in-label free free-vars)
  383. (intset-fold (lambda (var free)
  384. (match (intmap-ref bound->label var (lambda (_) #f))
  385. (#f free)
  386. (label
  387. (cond
  388. ((eliminate-closure? label free-vars)
  389. (intset-remove free var))
  390. ((closure-alias (closure-label label shared bound->label)
  391. well-known free-vars)
  392. => (lambda (alias)
  393. ;; If VAR is free in LABEL, then ALIAS must
  394. ;; also be free because its definition must
  395. ;; precede VAR's definition.
  396. (intset-add (intset-remove free var) alias)))
  397. (else free)))))
  398. free free))
  399. (fixpoint (lambda (free-vars)
  400. (intmap-fold (lambda (label free free-vars)
  401. (intmap-replace free-vars label
  402. (prune-free label free free-vars)))
  403. free-vars
  404. free-vars))
  405. free-vars))
  406. (define (intset-find set i)
  407. (let lp ((idx 0) (start #f))
  408. (let ((start (intset-next set start)))
  409. (cond
  410. ((not start) (error "not found" set i))
  411. ((= start i) idx)
  412. (else (lp (1+ idx) (1+ start)))))))
  413. (define (intset-count set)
  414. (intset-fold (lambda (_ count) (1+ count)) set 0))
  415. (define (compute-elidable-closures cps well-known shared free-vars)
  416. "Compute the set of well-known callees with no free variables. Calls
  417. to these functions can avoid passing a closure parameter. Note however
  418. that we have to exclude well-known callees that are part of a shared
  419. closure that contains any not-well-known member."
  420. (define (intset-map f set)
  421. (persistent-intset
  422. (intset-fold (lambda (i out) (if (f i) (intset-add! out i) out))
  423. set
  424. empty-intset)))
  425. (let ((no-free-vars (persistent-intset
  426. (intmap-fold (lambda (label free out)
  427. (if (eq? empty-intset free)
  428. (intset-add! out label)
  429. out))
  430. free-vars empty-intset)))
  431. (shared
  432. (intmap-fold
  433. (lambda (label cont out)
  434. (match cont
  435. (($ $kargs _ _
  436. ($ $continue _ _ ($ $rec _ _ (($ $fun kfuns) ...))))
  437. ;; Either all of these functions share a closure, in
  438. ;; which all or all except one of them are well-known, or
  439. ;; none of the functions share a closure.
  440. (if (intmap-ref shared (car kfuns) (lambda (_) #f))
  441. (let* ((scc (fold intset-cons empty-intset kfuns)))
  442. (intset-fold (lambda (label out)
  443. (intmap-add out label scc))
  444. scc out))
  445. out))
  446. (_ out)))
  447. cps
  448. empty-intmap)))
  449. (intmap-fold (lambda (label labels elidable)
  450. (if (eq? labels (intset-intersect labels well-known))
  451. elidable
  452. (intset-subtract elidable labels)))
  453. shared
  454. (intset-intersect well-known no-free-vars))))
  455. (define (convert-one cps label body free-vars bound->label well-known shared
  456. elidable)
  457. (define (well-known? label)
  458. (intset-ref well-known label))
  459. (let* ((free (intmap-ref free-vars label))
  460. (nfree (intset-count free))
  461. (self-known? (well-known? (closure-label label shared bound->label)))
  462. (self (match (intmap-ref cps label) (($ $kfun _ _ self) self))))
  463. (define (convert-arg cps var k)
  464. "Convert one possibly free variable reference to a bound reference.
  465. If @var{var} is free, it is replaced by a closure reference via a
  466. @code{free-ref} primcall, and @var{k} is called with the new var.
  467. Otherwise @var{var} is bound, so @var{k} is called with @var{var}."
  468. ;; We know that var is not the name of a well-known function.
  469. (cond
  470. ((and=> (intmap-ref bound->label var (lambda (_) #f))
  471. (lambda (kfun)
  472. (and (eq? empty-intset (intmap-ref free-vars kfun))
  473. kfun)))
  474. ;; A not-well-known function with zero free vars. Copy as a
  475. ;; constant, relying on the linker to reify just one copy.
  476. => (lambda (kfun)
  477. ;; It may well be that "var" is the "self" of another
  478. ;; member of an SCC containing just one not-well-known
  479. ;; function. But here we're asking for the value of the
  480. ;; closure, which is the $const-fun of the non-well-known
  481. ;; member.
  482. (let ((kfun (closure-label kfun shared bound->label)))
  483. (with-cps cps
  484. (letv var*)
  485. (let$ body (k var*))
  486. (letk k* ($kargs (#f) (var*) ,body))
  487. (build-term ($continue k* #f ($const-fun kfun)))))))
  488. ((intset-ref free var)
  489. (if (and self-known? (eqv? 1 nfree))
  490. ;; A reference to the one free var of a well-known function.
  491. (with-cps cps
  492. ($ (k self)))
  493. (let* ((idx (intset-find free var))
  494. (param (cond
  495. ((not self-known?) (cons 'closure (+ idx 2)))
  496. ((= nfree 2) (cons 'pair idx))
  497. (else (cons 'vector (+ idx 1))))))
  498. (with-cps cps
  499. (letv var*)
  500. (let$ body (k var*))
  501. (letk k* ($kargs (#f) (var*) ,body))
  502. (build-term
  503. ($continue k* #f
  504. ($primcall 'scm-ref/immediate param (self))))))))
  505. (else
  506. (with-cps cps
  507. ($ (k var))))))
  508. (define (convert-args cps vars k)
  509. "Convert a number of possibly free references to bound references.
  510. @var{k} is called with the bound references, and should return the
  511. term."
  512. (match vars
  513. (()
  514. (with-cps cps
  515. ($ (k '()))))
  516. ((var . vars)
  517. (convert-arg cps var
  518. (lambda (cps var)
  519. (convert-args cps vars
  520. (lambda (cps vars)
  521. (with-cps cps
  522. ($ (k (cons var vars)))))))))))
  523. (define (allocate-closure cps k src label known? nfree)
  524. "Allocate a new closure, and pass it to $var{k}."
  525. (match (vector known? nfree)
  526. (#(#f 0)
  527. ;; The call sites cannot be enumerated, but the closure has no
  528. ;; identity; statically allocate it.
  529. (with-cps cps
  530. (build-term ($continue k src ($const-fun label)))))
  531. (#(#f nfree)
  532. ;; The call sites cannot be enumerated; allocate a closure.
  533. (with-cps cps
  534. (letv closure tag code)
  535. (letk k* ($kargs () ()
  536. ($continue k src ($values (closure)))))
  537. (letk kinit ($kargs ('code) (code)
  538. ($continue k* src
  539. ($primcall 'word-set!/immediate '(closure . 1)
  540. (closure code)))))
  541. (letk kcode ($kargs () ()
  542. ($continue kinit src ($code label))))
  543. (letk ktag1
  544. ($kargs ('tag) (tag)
  545. ($continue kcode src
  546. ($primcall 'word-set!/immediate '(closure . 0)
  547. (closure tag)))))
  548. (letk ktag0
  549. ($kargs ('closure) (closure)
  550. ($continue ktag1 src
  551. ($primcall 'load-u64 (+ %tc7-program (ash nfree 16)) ()))))
  552. (build-term
  553. ($continue ktag0 src
  554. ($primcall 'allocate-words/immediate `(closure . ,(+ nfree 2))
  555. ())))))
  556. (#(#t 0)
  557. (with-cps cps
  558. (build-term ($continue k src ($const #f)))))
  559. (#(#t 1)
  560. ;; A well-known closure of one free variable is replaced
  561. ;; at each use with the free variable itself, so we don't
  562. ;; need a binding at all; and yet, the continuation
  563. ;; expects one value, so give it something. DCE should
  564. ;; clean up later.
  565. (with-cps cps
  566. (build-term ($continue k src ($const #f)))))
  567. (#(#t 2)
  568. ;; Well-known closure with two free variables; the closure is a
  569. ;; pair.
  570. (with-cps cps
  571. (build-term
  572. ($continue k src
  573. ($primcall 'allocate-words/immediate `(pair . 2) ())))))
  574. ;; Well-known callee with more than two free variables; the closure
  575. ;; is a vector.
  576. (#(#t nfree)
  577. (unless (> nfree 2)
  578. (error "unexpected well-known nullary, unary, or binary closure"))
  579. (with-cps cps
  580. (letv v w0)
  581. (letk k* ($kargs () () ($continue k src ($values (v)))))
  582. (letk ktag1
  583. ($kargs ('w0) (w0)
  584. ($continue k* src
  585. ($primcall 'word-set!/immediate '(vector . 0) (v w0)))))
  586. (letk ktag0
  587. ($kargs ('v) (v)
  588. ($continue ktag1 src
  589. ($primcall 'load-u64 (+ %tc7-vector (ash nfree 8)) ()))))
  590. (build-term
  591. ($continue ktag0 src
  592. ($primcall 'allocate-words/immediate `(vector . ,(1+ nfree))
  593. ())))))))
  594. (define (init-closure cps k src var known? free)
  595. "Initialize the free variables @var{closure-free} in a closure
  596. bound to @var{var}, and continue to @var{k}."
  597. (let ((count (intset-count free)))
  598. (cond
  599. ((and known? (<= count 1))
  600. ;; Well-known callee with zero or one free variables; no
  601. ;; initialization necessary.
  602. (with-cps cps
  603. (build-term ($continue k src ($values ())))))
  604. (else
  605. ;; Otherwise residualize a sequence of scm-set!.
  606. (let-values (((kind offset)
  607. ;; What are we initializing? A closure if the
  608. ;; procedure is not well-known; a pair if it has
  609. ;; only 2 free variables; otherwise, a vector.
  610. (cond
  611. ((not known?) (values 'closure 2))
  612. ((= count 2) (values 'pair 0))
  613. (else (values 'vector 1)))))
  614. (let lp ((cps cps) (prev #f) (idx 0))
  615. (match (intset-next free prev)
  616. (#f (with-cps cps
  617. (build-term ($continue k src ($values ())))))
  618. (v (with-cps cps
  619. (let$ body (lp (1+ v) (1+ idx)))
  620. (letk k ($kargs () () ,body))
  621. ($ (convert-arg v
  622. (lambda (cps v)
  623. (with-cps cps
  624. (build-term
  625. ($continue k src
  626. ($primcall 'scm-set!/immediate
  627. (cons kind (+ offset idx))
  628. (var v)))))))))))))))))
  629. (define (make-single-closure cps k src kfun)
  630. (let ((free (intmap-ref free-vars kfun)))
  631. (match (vector (well-known? kfun) (intset-count free))
  632. (#(#f 0)
  633. (with-cps cps
  634. (build-term ($continue k src ($const-fun kfun)))))
  635. (#(well-known? nfree)
  636. ;; A bit of a mess, but beta conversion should remove the
  637. ;; final $values if possible.
  638. (with-cps cps
  639. (letv closure)
  640. (letk k* ($kargs () () ($continue k src ($values (closure)))))
  641. (let$ init (init-closure k* src closure well-known? free))
  642. (letk knew ($kargs (#f) (closure) ,init))
  643. ($ (allocate-closure knew src kfun well-known? nfree)))))))
  644. ;; The callee is known, but not necessarily well-known.
  645. (define (convert-known-proc-call cps k src label closure args)
  646. (define (have-closure cps closure)
  647. (convert-args cps args
  648. (lambda (cps args)
  649. (with-cps cps
  650. (build-term
  651. ($continue k src ($callk label closure args)))))))
  652. (cond
  653. ((not closure)
  654. ;; No closure to begin with; done.
  655. (have-closure cps #f))
  656. ((eq? (intmap-ref free-vars label) empty-intset)
  657. ;; Known call, no free variables; no closure needed. If the
  658. ;; callee is well-known, elide the closure argument entirely.
  659. ;; Otherwise pass #f.
  660. (if (intset-ref elidable label)
  661. (have-closure cps #f)
  662. (with-cps cps
  663. ($ (with-cps-constants ((false #f))
  664. ($ (have-closure false)))))))
  665. ((and (well-known? (closure-label label shared bound->label))
  666. (trivial-intset (intmap-ref free-vars label)))
  667. ;; Well-known closures with one free variable are
  668. ;; replaced at their use sites by uses of the one free
  669. ;; variable.
  670. => (lambda (var)
  671. (convert-arg cps var have-closure)))
  672. (else
  673. ;; Otherwise just load the proc.
  674. (convert-arg cps closure have-closure))))
  675. (define (visit-term cps term)
  676. (match term
  677. (($ $continue k src (or ($ $const) ($ $prim)))
  678. (with-cps cps
  679. term))
  680. (($ $continue k src ($ $fun kfun))
  681. (with-cps cps
  682. ($ (make-single-closure k src kfun))))
  683. ;; Remove letrec.
  684. (($ $continue k src ($ $rec names vars (($ $fun kfuns) ...)))
  685. (match (vector names vars kfuns)
  686. (#(() () ())
  687. ;; Trivial empty case.
  688. (with-cps cps
  689. (build-term ($continue k src ($values ())))))
  690. (#((name) (var) (kfun))
  691. ;; Trivial single case. We have already proven that K has
  692. ;; only LABEL as its predecessor, so we have been able
  693. ;; already to rewrite free references to the bound name with
  694. ;; the self name.
  695. (with-cps cps
  696. ($ (make-single-closure k src kfun))))
  697. (#(_ _ (kfun0 . _))
  698. ;; A non-trivial strongly-connected component. Does it have
  699. ;; a shared closure?
  700. (match (intmap-ref shared kfun0 (lambda (_) #f))
  701. (#f
  702. ;; Nope. Allocate closures for each function.
  703. (let lp ((cps (match (intmap-ref cps k)
  704. ;; Steal declarations from the continuation.
  705. (($ $kargs names vals body)
  706. (intmap-replace cps k
  707. (build-cont
  708. ($kargs () () ,body))))))
  709. (in (map vector names vars kfuns))
  710. (init (lambda (cps)
  711. (with-cps cps
  712. (build-term
  713. ($continue k src ($values ())))))))
  714. (match in
  715. (() (init cps))
  716. ((#(name var kfun) . in)
  717. (let* ((known? (well-known? kfun))
  718. (free (intmap-ref free-vars kfun))
  719. (nfree (intset-count free)))
  720. (define (next-init cps)
  721. (with-cps cps
  722. (let$ body (init))
  723. (letk k ($kargs () () ,body))
  724. ($ (init-closure k src var known? free))))
  725. (with-cps cps
  726. (let$ body (lp in next-init))
  727. (letk k ($kargs (name) (var) ,body))
  728. ($ (allocate-closure k src kfun known? nfree))))))))
  729. (shared
  730. ;; If shared is in the bound->var map, that means one of
  731. ;; the functions is not well-known. Otherwise use kfun0
  732. ;; as the function label, but just so make-single-closure
  733. ;; can find the free vars, not for embedding in the
  734. ;; closure.
  735. (let* ((kfun (intmap-ref bound->label shared (lambda (_) kfun0)))
  736. (cps (match (intmap-ref cps k)
  737. ;; Make continuation declare only the shared
  738. ;; closure.
  739. (($ $kargs names vals body)
  740. (intmap-replace cps k
  741. (build-cont
  742. ($kargs (#f) (shared) ,body)))))))
  743. (with-cps cps
  744. ($ (make-single-closure k src kfun)))))))))
  745. (($ $continue k src ($ $call proc args))
  746. (match (intmap-ref bound->label proc (lambda (_) #f))
  747. (#f
  748. (convert-arg cps proc
  749. (lambda (cps proc)
  750. (convert-args cps args
  751. (lambda (cps args)
  752. (with-cps cps
  753. (build-term
  754. ($continue k src ($call proc args)))))))))
  755. (label
  756. (convert-known-proc-call cps k src label proc args))))
  757. (($ $continue k src ($ $callk label proc args))
  758. (convert-known-proc-call cps k src label proc args))
  759. (($ $continue k src ($ $primcall name param args))
  760. (convert-args cps args
  761. (lambda (cps args)
  762. (with-cps cps
  763. (build-term
  764. ($continue k src ($primcall name param args)))))))
  765. (($ $continue k src ($ $values args))
  766. (convert-args cps args
  767. (lambda (cps args)
  768. (with-cps cps
  769. (build-term
  770. ($continue k src ($values args)))))))
  771. (($ $branch kf kt src op param args)
  772. (convert-args cps args
  773. (lambda (cps args)
  774. (with-cps cps
  775. (build-term
  776. ($branch kf kt src op param args))))))
  777. (($ $switch kf kt* src arg)
  778. (convert-arg cps arg
  779. (lambda (cps arg)
  780. (with-cps cps
  781. (build-term
  782. ($switch kf kt* src arg))))))
  783. (($ $prompt k kh src escape? tag)
  784. (convert-arg cps tag
  785. (lambda (cps tag)
  786. (with-cps cps
  787. (build-term
  788. ($prompt k kh src escape? tag))))))
  789. (($ $throw src op param args)
  790. (convert-args cps args
  791. (lambda (cps args)
  792. (with-cps cps
  793. (build-term
  794. ($throw src op param args))))))))
  795. (intset-fold (lambda (label cps)
  796. (match (intmap-ref cps label (lambda (_) #f))
  797. (($ $kargs names vars term)
  798. (with-cps cps
  799. (let$ term (visit-term term))
  800. (setk label ($kargs names vars ,term))))
  801. (($ $kfun src meta self ktail kclause)
  802. (if (intset-ref elidable label)
  803. (with-cps cps
  804. (setk label ($kfun src meta #f ktail kclause)))
  805. cps))
  806. (_ cps)))
  807. body
  808. cps)))
  809. (define (convert-closures cps)
  810. "Convert free reference in @var{cps} to primcalls to @code{free-ref},
  811. and allocate and initialize flat closures."
  812. (let* ((kfun 0) ;; Ass-u-me.
  813. ;; label -> body-label...
  814. (functions (compute-reachable-functions cps kfun))
  815. (cps (filter-reachable cps functions))
  816. ;; label -> bound-var...
  817. (label->bound (compute-function-names cps functions))
  818. ;; bound-var -> label
  819. (bound->label (invert-partition label->bound))
  820. ;; label...
  821. (well-known (compute-well-known-functions cps bound->label))
  822. ;; label -> closure-var
  823. (shared (compute-shared-closures cps well-known))
  824. (cps (rewrite-shared-closure-calls cps functions label->bound shared
  825. kfun))
  826. ;; label -> free-var...
  827. (free-vars (compute-free-vars cps kfun shared))
  828. (free-vars (prune-free-vars free-vars bound->label well-known shared))
  829. ;; label...
  830. (elidable (compute-elidable-closures cps well-known shared free-vars)))
  831. (let ((free-in-program (intmap-ref free-vars kfun)))
  832. (unless (eq? empty-intset free-in-program)
  833. (error "Expected no free vars in program" free-in-program)))
  834. (with-fresh-name-state cps
  835. (persistent-intmap
  836. (intmap-fold
  837. (lambda (label body cps)
  838. (convert-one cps label body free-vars bound->label well-known shared
  839. elidable))
  840. functions
  841. cps)))))
  842. ;;; Local Variables:
  843. ;;; eval: (put 'convert-arg 'scheme-indent-function 2)
  844. ;;; eval: (put 'convert-args 'scheme-indent-function 2)
  845. ;;; End: