Commit | Line | Data |
---|---|---|
80b01fd0 AW |
1 | ;;; Continuation-passing style (CPS) intermediate language (IL) |
2 | ||
a9ec16f9 | 3 | ;; Copyright (C) 2013, 2014, 2015 Free Software Foundation, Inc. |
80b01fd0 AW |
4 | |
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. | |
9 | ;;;; | |
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. | |
14 | ;;;; | |
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 | |
18 | ||
19 | ;;; Commentary: | |
20 | ;;; | |
21 | ;;; This is the continuation-passing style (CPS) intermediate language | |
22 | ;;; (IL) for Guile. | |
23 | ;;; | |
24 | ;;; There are two kinds of terms in CPS: terms that bind continuations, | |
25 | ;;; and terms that call continuations. | |
26 | ;;; | |
27 | ;;; $letk binds a set of mutually recursive continuations, each one an | |
6e422a35 AW |
28 | ;;; instance of $cont. A $cont declares the name of a continuation, and |
29 | ;;; then contains as a subterm the particular continuation instance: | |
59258f7c AW |
30 | ;;; $kargs for continuations that bind values, $ktail for the tail |
31 | ;;; continuation, etc. | |
80b01fd0 AW |
32 | ;;; |
33 | ;;; $continue nodes call continuations. The expression contained in the | |
34 | ;;; $continue node determines the value or values that are passed to the | |
35 | ;;; target continuation: $const to pass a constant value, $values to | |
6e422a35 | 36 | ;;; pass multiple named values, etc. $continue nodes also record the source at which |
80b01fd0 AW |
37 | ;;; |
38 | ;;; Additionally there is $letrec, a term that binds mutually recursive | |
39 | ;;; functions. The contification pass will turn $letrec into $letk if | |
40 | ;;; it can do so. Otherwise, the closure conversion pass will desugar | |
41 | ;;; $letrec into an equivalent sequence of make-closure primcalls and | |
42 | ;;; subsequent initializations of the captured variables of the | |
43 | ;;; closures. You can think of $letrec as pertaining to "high CPS", | |
44 | ;;; whereas later passes will only see "low CPS", which does not have | |
45 | ;;; $letrec. | |
46 | ;;; | |
47 | ;;; This particular formulation of CPS was inspired by Andrew Kennedy's | |
48 | ;;; 2007 paper, "Compiling with Continuations, Continued". All Guile | |
49 | ;;; hackers should read that excellent paper! As in Kennedy's paper, | |
50 | ;;; continuations are second-class, and may be thought of as basic block | |
51 | ;;; labels. All values are bound to variables using continuation calls: | |
52 | ;;; even constants! | |
53 | ;;; | |
54 | ;;; There are some Guile-specific quirks as well: | |
55 | ;;; | |
36527695 | 56 | ;;; - $kreceive represents a continuation that receives multiple values, |
80b01fd0 AW |
57 | ;;; but which truncates them to some number of required values, |
58 | ;;; possibly with a rest list. | |
59 | ;;; | |
8320f504 | 60 | ;;; - $kfun labels an entry point for a $fun (a function), and |
80b01fd0 AW |
61 | ;;; contains a $ktail representing the formal argument which is the |
62 | ;;; function's continuation. | |
63 | ;;; | |
8320f504 | 64 | ;;; - $kfun also contain a $kclause continuation, corresponding to |
90dce16d AW |
65 | ;;; the first case-lambda clause of the function. $kclause actually |
66 | ;;; contains the clause body, and the subsequent clause (if any). | |
67 | ;;; This is because the $kclause logically matches or doesn't match | |
68 | ;;; a given set of actual arguments against a formal arity, then | |
69 | ;;; proceeds to a "body" continuation (which is a $kargs). | |
80b01fd0 AW |
70 | ;;; |
71 | ;;; That's to say that a $fun can be matched like this: | |
72 | ;;; | |
73 | ;;; (match f | |
24b611e8 | 74 | ;;; (($ $fun free |
8320f504 AW |
75 | ;;; ($ $cont kfun |
76 | ;;; ($ $kfun src meta self ($ $cont ktail ($ $ktail)) | |
90dce16d | 77 | ;;; ($ $kclause arity |
24b611e8 | 78 | ;;; ($ $cont kbody ($ $kargs names syms body)) |
90dce16d | 79 | ;;; alternate)))) |
80b01fd0 AW |
80 | ;;; #t)) |
81 | ;;; | |
8320f504 | 82 | ;;; A $continue to ktail is in tail position. $kfun, $kclause, |
80b01fd0 AW |
83 | ;;; and $ktail will never be seen elsewhere in a CPS term. |
84 | ;;; | |
85 | ;;; - $prompt continues to the body of the prompt, having pushed on a | |
86 | ;;; prompt whose handler will continue at its "handler" | |
87 | ;;; continuation. The continuation of the prompt is responsible for | |
7ab76a83 | 88 | ;;; popping the prompt. |
80b01fd0 AW |
89 | ;;; |
90 | ;;; In summary: | |
91 | ;;; | |
92 | ;;; - $letk, $letrec, and $continue are terms. | |
93 | ;;; | |
94 | ;;; - $cont is a continuation, containing a continuation body ($kargs, | |
59258f7c | 95 | ;;; $ktail, etc). |
80b01fd0 AW |
96 | ;;; |
97 | ;;; - $continue terms contain an expression ($call, $const, $fun, | |
98 | ;;; etc). | |
99 | ;;; | |
100 | ;;; See (language tree-il compile-cps) for details on how Tree-IL | |
101 | ;;; converts to CPS. | |
102 | ;;; | |
103 | ;;; Code: | |
104 | ||
105 | (define-module (language cps) | |
106 | #:use-module (ice-9 match) | |
107 | #:use-module ((srfi srfi-1) #:select (fold)) | |
108 | #:use-module (srfi srfi-9) | |
109 | #:use-module (srfi srfi-9 gnu) | |
828ed944 | 110 | #:use-module (srfi srfi-11) |
80b01fd0 AW |
111 | #:export (;; Helper. |
112 | $arity | |
113 | make-$arity | |
114 | ||
115 | ;; Terms. | |
34ff3af9 | 116 | $letk $continue |
80b01fd0 AW |
117 | |
118 | ;; Continuations. | |
119 | $cont | |
120 | ||
121 | ;; Continuation bodies. | |
59258f7c | 122 | $kreceive $kargs $kfun $ktail $kclause |
80b01fd0 AW |
123 | |
124 | ;; Expressions. | |
34ff3af9 | 125 | $const $prim $fun $rec $closure $branch |
cf8bb037 AW |
126 | $call $callk $primcall $values $prompt |
127 | ||
128 | ;; First-order CPS root. | |
129 | $program | |
80b01fd0 | 130 | |
9a1dfb7d AW |
131 | ;; Fresh names. |
132 | label-counter var-counter | |
133 | fresh-label fresh-var | |
828ed944 AW |
134 | with-fresh-name-state compute-max-label-and-var |
135 | let-fresh | |
9a1dfb7d | 136 | |
80b01fd0 | 137 | ;; Building macros. |
80b01fd0 AW |
138 | build-cps-term build-cps-cont build-cps-exp |
139 | rewrite-cps-term rewrite-cps-cont rewrite-cps-exp | |
140 | ||
141 | ;; Misc. | |
142 | parse-cps unparse-cps | |
405805fb AW |
143 | make-global-cont-folder make-local-cont-folder |
144 | fold-conts fold-local-conts | |
2c3c086e | 145 | visit-cont-successors)) |
80b01fd0 AW |
146 | |
147 | ;; FIXME: Use SRFI-99, when Guile adds it. | |
148 | (define-syntax define-record-type* | |
149 | (lambda (x) | |
150 | (define (id-append ctx . syms) | |
151 | (datum->syntax ctx (apply symbol-append (map syntax->datum syms)))) | |
152 | (syntax-case x () | |
153 | ((_ name field ...) | |
154 | (and (identifier? #'name) (and-map identifier? #'(field ...))) | |
155 | (with-syntax ((cons (id-append #'name #'make- #'name)) | |
156 | (pred (id-append #'name #'name #'?)) | |
157 | ((getter ...) (map (lambda (f) | |
158 | (id-append f #'name #'- f)) | |
159 | #'(field ...)))) | |
160 | #'(define-record-type name | |
161 | (cons field ...) | |
162 | pred | |
163 | (field getter) | |
164 | ...)))))) | |
165 | ||
166 | (define-syntax-rule (define-cps-type name field ...) | |
167 | (begin | |
168 | (define-record-type* name field ...) | |
169 | (set-record-type-printer! name print-cps))) | |
170 | ||
171 | (define (print-cps exp port) | |
172 | (format port "#<cps ~S>" (unparse-cps exp))) | |
173 | ||
174 | ;; Helper. | |
175 | (define-record-type* $arity req opt rest kw allow-other-keys?) | |
176 | ||
177 | ;; Terms. | |
178 | (define-cps-type $letk conts body) | |
6e422a35 | 179 | (define-cps-type $continue k src exp) |
80b01fd0 AW |
180 | |
181 | ;; Continuations | |
6e422a35 | 182 | (define-cps-type $cont k cont) |
36527695 | 183 | (define-cps-type $kreceive arity k) |
80b01fd0 | 184 | (define-cps-type $kargs names syms body) |
8320f504 | 185 | (define-cps-type $kfun src meta self tail clause) |
80b01fd0 | 186 | (define-cps-type $ktail) |
90dce16d | 187 | (define-cps-type $kclause arity cont alternate) |
80b01fd0 AW |
188 | |
189 | ;; Expressions. | |
80b01fd0 AW |
190 | (define-cps-type $const val) |
191 | (define-cps-type $prim name) | |
cf8bb037 | 192 | (define-cps-type $fun free body) ; Higher-order. |
34ff3af9 | 193 | (define-cps-type $rec names syms funs) ; Higher-order. |
cf8bb037 | 194 | (define-cps-type $closure label nfree) ; First-order. |
92805e21 | 195 | (define-cps-type $branch k exp) |
80b01fd0 | 196 | (define-cps-type $call proc args) |
cf8bb037 | 197 | (define-cps-type $callk k proc args) ; First-order. |
80b01fd0 AW |
198 | (define-cps-type $primcall name args) |
199 | (define-cps-type $values args) | |
7ab76a83 | 200 | (define-cps-type $prompt escape? tag handler) |
80b01fd0 | 201 | |
cf8bb037 AW |
202 | ;; The root of a higher-order CPS term is $cont containing a $kfun. The |
203 | ;; root of a first-order CPS term is a $program. | |
204 | (define-cps-type $program funs) | |
205 | ||
9a1dfb7d AW |
206 | (define label-counter (make-parameter #f)) |
207 | (define var-counter (make-parameter #f)) | |
208 | ||
209 | (define (fresh-label) | |
828ed944 AW |
210 | (let ((count (or (label-counter) |
211 | (error "fresh-label outside with-fresh-name-state")))) | |
9a1dfb7d AW |
212 | (label-counter (1+ count)) |
213 | count)) | |
214 | ||
9a1dfb7d | 215 | (define (fresh-var) |
1eda52c8 | 216 | (let ((count (or (var-counter) |
828ed944 | 217 | (error "fresh-var outside with-fresh-name-state")))) |
1eda52c8 | 218 | (var-counter (1+ count)) |
9a1dfb7d AW |
219 | count)) |
220 | ||
221 | (define-syntax-rule (let-fresh (label ...) (var ...) body ...) | |
222 | (let ((label (fresh-label)) ... | |
223 | (var (fresh-var)) ...) | |
224 | body ...)) | |
225 | ||
828ed944 | 226 | (define-syntax-rule (with-fresh-name-state fun body ...) |
d3dbf75a | 227 | (call-with-values (lambda () (compute-max-label-and-var fun)) |
3e1b97c1 AW |
228 | (lambda (max-label max-var) |
229 | (parameterize ((label-counter (1+ max-label)) | |
230 | (var-counter (1+ max-var))) | |
231 | body ...)))) | |
80b01fd0 AW |
232 | |
233 | (define-syntax build-arity | |
234 | (syntax-rules (unquote) | |
235 | ((_ (unquote exp)) exp) | |
236 | ((_ (req opt rest kw allow-other-keys?)) | |
237 | (make-$arity req opt rest kw allow-other-keys?)))) | |
238 | ||
239 | (define-syntax build-cont-body | |
59258f7c | 240 | (syntax-rules (unquote $kreceive $kargs $kfun $ktail $kclause) |
80b01fd0 AW |
241 | ((_ (unquote exp)) |
242 | exp) | |
36527695 AW |
243 | ((_ ($kreceive req rest kargs)) |
244 | (make-$kreceive (make-$arity req '() rest '() #f) kargs)) | |
f5fcd7f2 AW |
245 | ((_ ($kargs (name ...) (unquote syms) body)) |
246 | (make-$kargs (list name ...) syms (build-cps-term body))) | |
80b01fd0 AW |
247 | ((_ ($kargs (name ...) (sym ...) body)) |
248 | (make-$kargs (list name ...) (list sym ...) (build-cps-term body))) | |
249 | ((_ ($kargs names syms body)) | |
250 | (make-$kargs names syms (build-cps-term body))) | |
8320f504 AW |
251 | ((_ ($kfun src meta self tail clause)) |
252 | (make-$kfun src meta self (build-cps-cont tail) (build-cps-cont clause))) | |
80b01fd0 AW |
253 | ((_ ($ktail)) |
254 | (make-$ktail)) | |
90dce16d AW |
255 | ((_ ($kclause arity cont alternate)) |
256 | (make-$kclause (build-arity arity) (build-cps-cont cont) | |
257 | (build-cps-cont alternate))))) | |
80b01fd0 AW |
258 | |
259 | (define-syntax build-cps-cont | |
260 | (syntax-rules (unquote) | |
261 | ((_ (unquote exp)) exp) | |
6e422a35 | 262 | ((_ (k cont)) (make-$cont k (build-cont-body cont))))) |
80b01fd0 AW |
263 | |
264 | (define-syntax build-cps-exp | |
265 | (syntax-rules (unquote | |
34ff3af9 | 266 | $const $prim $fun $rec $closure $branch |
cf8bb037 | 267 | $call $callk $primcall $values $prompt) |
80b01fd0 | 268 | ((_ (unquote exp)) exp) |
80b01fd0 AW |
269 | ((_ ($const val)) (make-$const val)) |
270 | ((_ ($prim name)) (make-$prim name)) | |
cf8bb037 | 271 | ((_ ($fun free body)) (make-$fun free (build-cps-cont body))) |
34ff3af9 | 272 | ((_ ($rec names gensyms funs)) (make-$rec names gensyms funs)) |
cf8bb037 | 273 | ((_ ($closure k nfree)) (make-$closure k nfree)) |
f5fcd7f2 | 274 | ((_ ($call proc (unquote args))) (make-$call proc args)) |
80b01fd0 AW |
275 | ((_ ($call proc (arg ...))) (make-$call proc (list arg ...))) |
276 | ((_ ($call proc args)) (make-$call proc args)) | |
f5fcd7f2 | 277 | ((_ ($callk k proc (unquote args))) (make-$callk k proc args)) |
b3ae2b50 AW |
278 | ((_ ($callk k proc (arg ...))) (make-$callk k proc (list arg ...))) |
279 | ((_ ($callk k proc args)) (make-$callk k proc args)) | |
f5fcd7f2 | 280 | ((_ ($primcall name (unquote args))) (make-$primcall name args)) |
80b01fd0 AW |
281 | ((_ ($primcall name (arg ...))) (make-$primcall name (list arg ...))) |
282 | ((_ ($primcall name args)) (make-$primcall name args)) | |
f5fcd7f2 | 283 | ((_ ($values (unquote args))) (make-$values args)) |
80b01fd0 AW |
284 | ((_ ($values (arg ...))) (make-$values (list arg ...))) |
285 | ((_ ($values args)) (make-$values args)) | |
92805e21 | 286 | ((_ ($branch k exp)) (make-$branch k (build-cps-exp exp))) |
7ab76a83 AW |
287 | ((_ ($prompt escape? tag handler)) |
288 | (make-$prompt escape? tag handler)))) | |
80b01fd0 AW |
289 | |
290 | (define-syntax build-cps-term | |
34ff3af9 | 291 | (syntax-rules (unquote $letk $letk* $letconst $program $continue) |
80b01fd0 AW |
292 | ((_ (unquote exp)) |
293 | exp) | |
294 | ((_ ($letk (unquote conts) body)) | |
295 | (make-$letk conts (build-cps-term body))) | |
296 | ((_ ($letk (cont ...) body)) | |
297 | (make-$letk (list (build-cps-cont cont) ...) | |
298 | (build-cps-term body))) | |
299 | ((_ ($letk* () body)) | |
300 | (build-cps-term body)) | |
301 | ((_ ($letk* (cont conts ...) body)) | |
302 | (build-cps-term ($letk (cont) ($letk* (conts ...) body)))) | |
303 | ((_ ($letconst () body)) | |
304 | (build-cps-term body)) | |
305 | ((_ ($letconst ((name sym val) tail ...) body)) | |
9a1dfb7d | 306 | (let-fresh (kconst) () |
80b01fd0 | 307 | (build-cps-term |
6e422a35 AW |
308 | ($letk ((kconst ($kargs (name) (sym) ($letconst (tail ...) body)))) |
309 | ($continue kconst (let ((props (source-properties val))) | |
310 | (and (pair? props) props)) | |
311 | ($const val)))))) | |
cf8bb037 AW |
312 | ((_ ($program (unquote conts))) |
313 | (make-$program conts)) | |
314 | ((_ ($program (cont ...))) | |
315 | (make-$program (list (build-cps-cont cont) ...))) | |
316 | ((_ ($program conts)) | |
317 | (make-$program conts)) | |
6e422a35 AW |
318 | ((_ ($continue k src exp)) |
319 | (make-$continue k src (build-cps-exp exp))))) | |
80b01fd0 AW |
320 | |
321 | (define-syntax-rule (rewrite-cps-term x (pat body) ...) | |
322 | (match x | |
323 | (pat (build-cps-term body)) ...)) | |
324 | (define-syntax-rule (rewrite-cps-cont x (pat body) ...) | |
325 | (match x | |
326 | (pat (build-cps-cont body)) ...)) | |
327 | (define-syntax-rule (rewrite-cps-exp x (pat body) ...) | |
328 | (match x | |
329 | (pat (build-cps-exp body)) ...)) | |
330 | ||
331 | (define (parse-cps exp) | |
332 | (define (src exp) | |
333 | (let ((props (source-properties exp))) | |
334 | (and (pair? props) props))) | |
335 | (match exp | |
336 | ;; Continuations. | |
337 | (('letconst k (name sym c) body) | |
338 | (build-cps-term | |
6e422a35 AW |
339 | ($letk ((k ($kargs (name) (sym) |
340 | ,(parse-cps body)))) | |
341 | ($continue k (src exp) ($const c))))) | |
80b01fd0 AW |
342 | (('let k (name sym val) body) |
343 | (build-cps-term | |
6e422a35 AW |
344 | ($letk ((k ($kargs (name) (sym) |
345 | ,(parse-cps body)))) | |
80b01fd0 AW |
346 | ,(parse-cps val)))) |
347 | (('letk (cont ...) body) | |
348 | (build-cps-term | |
349 | ($letk ,(map parse-cps cont) ,(parse-cps body)))) | |
350 | (('k sym body) | |
351 | (build-cps-cont | |
6e422a35 | 352 | (sym ,(parse-cps body)))) |
36527695 AW |
353 | (('kreceive req rest k) |
354 | (build-cont-body ($kreceive req rest k))) | |
80b01fd0 AW |
355 | (('kargs names syms body) |
356 | (build-cont-body ($kargs names syms ,(parse-cps body)))) | |
8320f504 | 357 | (('kfun src meta self tail clause) |
80b01fd0 | 358 | (build-cont-body |
8320f504 | 359 | ($kfun (src exp) meta self ,(parse-cps tail) |
24b611e8 | 360 | ,(and=> clause parse-cps)))) |
80b01fd0 AW |
361 | (('ktail) |
362 | (build-cont-body | |
363 | ($ktail))) | |
364 | (('kclause (req opt rest kw allow-other-keys?) body) | |
365 | (build-cont-body | |
366 | ($kclause (req opt rest kw allow-other-keys?) | |
90dce16d AW |
367 | ,(parse-cps body) |
368 | ,#f))) | |
369 | (('kclause (req opt rest kw allow-other-keys?) body alternate) | |
370 | (build-cont-body | |
371 | ($kclause (req opt rest kw allow-other-keys?) | |
372 | ,(parse-cps body) | |
373 | ,(parse-cps alternate)))) | |
80b01fd0 AW |
374 | (('kseq body) |
375 | (build-cont-body ($kargs () () ,(parse-cps body)))) | |
376 | ||
377 | ;; Calls. | |
378 | (('continue k exp) | |
6e422a35 | 379 | (build-cps-term ($continue k (src exp) ,(parse-cps exp)))) |
80b01fd0 AW |
380 | (('const exp) |
381 | (build-cps-exp ($const exp))) | |
382 | (('prim name) | |
383 | (build-cps-exp ($prim name))) | |
24b611e8 AW |
384 | (('fun free body) |
385 | (build-cps-exp ($fun free ,(parse-cps body)))) | |
cf8bb037 AW |
386 | (('closure k nfree) |
387 | (build-cps-exp ($closure k nfree))) | |
34ff3af9 AW |
388 | (('rec (name sym fun) ...) |
389 | (build-cps-exp ($rec name sym (map parse-cps fun)))) | |
cf8bb037 AW |
390 | (('program (cont ...)) |
391 | (build-cps-term ($program ,(map parse-cps cont)))) | |
80b01fd0 AW |
392 | (('call proc arg ...) |
393 | (build-cps-exp ($call proc arg))) | |
b3ae2b50 AW |
394 | (('callk k proc arg ...) |
395 | (build-cps-exp ($callk k proc arg))) | |
80b01fd0 AW |
396 | (('primcall name arg ...) |
397 | (build-cps-exp ($primcall name arg))) | |
92805e21 AW |
398 | (('branch k exp) |
399 | (build-cps-exp ($branch k ,(parse-cps exp)))) | |
80b01fd0 AW |
400 | (('values arg ...) |
401 | (build-cps-exp ($values arg))) | |
7ab76a83 AW |
402 | (('prompt escape? tag handler) |
403 | (build-cps-exp ($prompt escape? tag handler))) | |
80b01fd0 AW |
404 | (_ |
405 | (error "unexpected cps" exp)))) | |
406 | ||
407 | (define (unparse-cps exp) | |
408 | (match exp | |
409 | ;; Continuations. | |
6e422a35 AW |
410 | (($ $letk (($ $cont k ($ $kargs (name) (sym) body))) |
411 | ($ $continue k src ($ $const c))) | |
80b01fd0 AW |
412 | `(letconst ,k (,name ,sym ,c) |
413 | ,(unparse-cps body))) | |
6e422a35 | 414 | (($ $letk (($ $cont k ($ $kargs (name) (sym) body))) val) |
80b01fd0 AW |
415 | `(let ,k (,name ,sym ,(unparse-cps val)) |
416 | ,(unparse-cps body))) | |
417 | (($ $letk conts body) | |
418 | `(letk ,(map unparse-cps conts) ,(unparse-cps body))) | |
6e422a35 | 419 | (($ $cont sym body) |
80b01fd0 | 420 | `(k ,sym ,(unparse-cps body))) |
36527695 AW |
421 | (($ $kreceive ($ $arity req () rest '() #f) k) |
422 | `(kreceive ,req ,rest ,k)) | |
80b01fd0 AW |
423 | (($ $kargs () () body) |
424 | `(kseq ,(unparse-cps body))) | |
425 | (($ $kargs names syms body) | |
426 | `(kargs ,names ,syms ,(unparse-cps body))) | |
8320f504 AW |
427 | (($ $kfun src meta self tail clause) |
428 | `(kfun ,meta ,self ,(unparse-cps tail) ,(unparse-cps clause))) | |
80b01fd0 AW |
429 | (($ $ktail) |
430 | `(ktail)) | |
90dce16d AW |
431 | (($ $kclause ($ $arity req opt rest kw allow-other-keys?) body alternate) |
432 | `(kclause (,req ,opt ,rest ,kw ,allow-other-keys?) ,(unparse-cps body) | |
433 | . ,(if alternate (list (unparse-cps alternate)) '()))) | |
80b01fd0 AW |
434 | |
435 | ;; Calls. | |
6e422a35 | 436 | (($ $continue k src exp) |
80b01fd0 | 437 | `(continue ,k ,(unparse-cps exp))) |
80b01fd0 AW |
438 | (($ $const val) |
439 | `(const ,val)) | |
440 | (($ $prim name) | |
441 | `(prim ,name)) | |
24b611e8 AW |
442 | (($ $fun free body) |
443 | `(fun ,free ,(unparse-cps body))) | |
cf8bb037 AW |
444 | (($ $closure k nfree) |
445 | `(closure ,k ,nfree)) | |
34ff3af9 AW |
446 | (($ $rec names syms funs) |
447 | `(rec ,@(map (lambda (name sym fun) | |
448 | (list name sym (unparse-cps fun))) | |
449 | names syms funs))) | |
cf8bb037 AW |
450 | (($ $program conts) |
451 | `(program ,(map unparse-cps conts))) | |
80b01fd0 AW |
452 | (($ $call proc args) |
453 | `(call ,proc ,@args)) | |
b3ae2b50 AW |
454 | (($ $callk k proc args) |
455 | `(callk ,k ,proc ,@args)) | |
80b01fd0 AW |
456 | (($ $primcall name args) |
457 | `(primcall ,name ,@args)) | |
92805e21 AW |
458 | (($ $branch k exp) |
459 | `(branch ,k ,(unparse-cps exp))) | |
80b01fd0 AW |
460 | (($ $values args) |
461 | `(values ,@args)) | |
7ab76a83 AW |
462 | (($ $prompt escape? tag handler) |
463 | `(prompt ,escape? ,tag ,handler)) | |
80b01fd0 AW |
464 | (_ |
465 | (error "unexpected cps" exp)))) | |
466 | ||
405805fb | 467 | (define-syntax-rule (make-global-cont-folder seed ...) |
686a6490 | 468 | (lambda (proc cont seed ...) |
828ed944 AW |
469 | (define (cont-folder cont seed ...) |
470 | (match cont | |
471 | (($ $cont k cont) | |
472 | (let-values (((seed ...) (proc k cont seed ...))) | |
473 | (match cont | |
474 | (($ $kargs names syms body) | |
475 | (term-folder body seed ...)) | |
476 | ||
8320f504 | 477 | (($ $kfun src meta self tail clause) |
828ed944 | 478 | (let-values (((seed ...) (cont-folder tail seed ...))) |
90dce16d AW |
479 | (if clause |
480 | (cont-folder clause seed ...) | |
481 | (values seed ...)))) | |
482 | ||
483 | (($ $kclause arity body alternate) | |
484 | (let-values (((seed ...) (cont-folder body seed ...))) | |
485 | (if alternate | |
486 | (cont-folder alternate seed ...) | |
487 | (values seed ...)))) | |
828ed944 AW |
488 | |
489 | (_ (values seed ...))))))) | |
490 | ||
491 | (define (fun-folder fun seed ...) | |
492 | (match fun | |
24b611e8 | 493 | (($ $fun free body) |
828ed944 AW |
494 | (cont-folder body seed ...)))) |
495 | ||
496 | (define (term-folder term seed ...) | |
497 | (match term | |
498 | (($ $letk conts body) | |
499 | (let-values (((seed ...) (term-folder body seed ...))) | |
2ad91e6b AW |
500 | (let lp ((conts conts) (seed seed) ...) |
501 | (if (null? conts) | |
502 | (values seed ...) | |
503 | (let-values (((seed ...) (cont-folder (car conts) seed ...))) | |
504 | (lp (cdr conts) seed ...)))))) | |
828ed944 AW |
505 | |
506 | (($ $continue k src exp) | |
507 | (match exp | |
405805fb | 508 | (($ $fun) (fun-folder exp seed ...)) |
34ff3af9 AW |
509 | (($ $rec names syms funs) |
510 | (let lp ((funs funs) (seed seed) ...) | |
511 | (if (null? funs) | |
512 | (values seed ...) | |
513 | (let-values (((seed ...) (fun-folder (car funs) seed ...))) | |
514 | (lp (cdr funs) seed ...))))) | |
515 | (_ (values seed ...)))))) | |
828ed944 | 516 | |
686a6490 | 517 | (cont-folder cont seed ...))) |
828ed944 | 518 | |
405805fb AW |
519 | (define-syntax-rule (make-local-cont-folder seed ...) |
520 | (lambda (proc cont seed ...) | |
521 | (define (cont-folder cont seed ...) | |
522 | (match cont | |
523 | (($ $cont k (and cont ($ $kargs names syms body))) | |
524 | (let-values (((seed ...) (proc k cont seed ...))) | |
525 | (term-folder body seed ...))) | |
526 | (($ $cont k cont) | |
527 | (proc k cont seed ...)))) | |
528 | (define (term-folder term seed ...) | |
529 | (match term | |
530 | (($ $letk conts body) | |
531 | (let-values (((seed ...) (term-folder body seed ...))) | |
532 | (let lp ((conts conts) (seed seed) ...) | |
533 | (match conts | |
534 | (() (values seed ...)) | |
535 | ((cont) (cont-folder cont seed ...)) | |
536 | ((cont . conts) | |
537 | (let-values (((seed ...) (cont-folder cont seed ...))) | |
538 | (lp conts seed ...))))))) | |
405805fb AW |
539 | (_ (values seed ...)))) |
540 | (define (clause-folder clause seed ...) | |
541 | (match clause | |
542 | (($ $cont k (and cont ($ $kclause arity body alternate))) | |
543 | (let-values (((seed ...) (proc k cont seed ...))) | |
544 | (if alternate | |
545 | (let-values (((seed ...) (cont-folder body seed ...))) | |
546 | (clause-folder alternate seed ...)) | |
547 | (cont-folder body seed ...)))))) | |
548 | (match cont | |
549 | (($ $cont k (and cont ($ $kfun src meta self tail clause))) | |
550 | (let*-values (((seed ...) (proc k cont seed ...)) | |
551 | ((seed ...) (if clause | |
552 | (clause-folder clause seed ...) | |
553 | (values seed ...)))) | |
554 | (cont-folder tail seed ...)))))) | |
555 | ||
828ed944 | 556 | (define (compute-max-label-and-var fun) |
cf8bb037 AW |
557 | (match fun |
558 | (($ $cont) | |
559 | ((make-global-cont-folder max-label max-var) | |
560 | (lambda (label cont max-label max-var) | |
561 | (values (max label max-label) | |
562 | (match cont | |
563 | (($ $kargs names vars body) | |
34ff3af9 | 564 | (fold max max-var vars)) |
cf8bb037 AW |
565 | (($ $kfun src meta self) |
566 | (max self max-var)) | |
567 | (_ max-var)))) | |
568 | fun -1 -1)) | |
569 | (($ $program conts) | |
570 | (define (fold/2 proc in s0 s1) | |
571 | (if (null? in) | |
572 | (values s0 s1) | |
573 | (let-values (((s0 s1) (proc (car in) s0 s1))) | |
574 | (fold/2 proc (cdr in) s0 s1)))) | |
575 | (let lp ((conts conts) (max-label -1) (max-var -1)) | |
576 | (if (null? conts) | |
577 | (values max-label max-var) | |
578 | (call-with-values (lambda () | |
579 | ((make-local-cont-folder max-label max-var) | |
580 | (lambda (label cont max-label max-var) | |
581 | (values (max label max-label) | |
582 | (match cont | |
583 | (($ $kargs names vars body) | |
584 | (fold max max-var vars)) | |
585 | (($ $kfun src meta self) | |
586 | (max self max-var)) | |
587 | (_ max-var)))) | |
588 | (car conts) max-label max-var)) | |
589 | (lambda (max-label max-var) | |
590 | (lp (cdr conts) max-label max-var)))))))) | |
80b01fd0 | 591 | |
828ed944 | 592 | (define (fold-conts proc seed fun) |
405805fb | 593 | ((make-global-cont-folder seed) proc fun seed)) |
80b01fd0 | 594 | |
a6f823bd | 595 | (define (fold-local-conts proc seed fun) |
405805fb | 596 | ((make-local-cont-folder seed) proc fun seed)) |
2c3c086e AW |
597 | |
598 | (define (visit-cont-successors proc cont) | |
599 | (match cont | |
600 | (($ $kargs names syms body) | |
601 | (let lp ((body body)) | |
602 | (match body | |
603 | (($ $letk conts body) (lp body)) | |
2c3c086e AW |
604 | (($ $continue k src exp) |
605 | (match exp | |
606 | (($ $prompt escape? tag handler) (proc k handler)) | |
92805e21 | 607 | (($ $branch kt) (proc k kt)) |
2c3c086e AW |
608 | (_ (proc k))))))) |
609 | ||
2c3c086e AW |
610 | (($ $kreceive arity k) (proc k)) |
611 | ||
612 | (($ $kclause arity ($ $cont kbody) #f) (proc kbody)) | |
613 | ||
614 | (($ $kclause arity ($ $cont kbody) ($ $cont kalt)) (proc kbody kalt)) | |
615 | ||
8320f504 | 616 | (($ $kfun src meta self tail ($ $cont clause)) (proc clause)) |
2c3c086e | 617 | |
8320f504 | 618 | (($ $kfun src meta self tail #f) (proc)) |
2c3c086e AW |
619 | |
620 | (($ $ktail) (proc)))) |