Limit impact of O(n^2) type analysis by imposing limit
[bpt/guile.git] / module / language / cps.scm
CommitLineData
80b01fd0
AW
1;;; Continuation-passing style (CPS) intermediate language (IL)
2
7ab76a83 3;; Copyright (C) 2013, 2014 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:
30;;; $kif for test continuations, $kargs for continuations that bind
31;;; values, 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,
95;;; $kif, etc).
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.
116 $letk $continue $letrec
117
118 ;; Continuations.
119 $cont
120
121 ;; Continuation bodies.
8320f504 122 $kif $kreceive $kargs $kfun $ktail $kclause
80b01fd0
AW
123
124 ;; Expressions.
cf8bb037
AW
125 $void $const $prim $fun $closure
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)
cf8bb037 180(define-cps-type $letrec names syms funs body) ; Higher-order.
80b01fd0
AW
181
182;; Continuations
6e422a35 183(define-cps-type $cont k cont)
80b01fd0 184(define-cps-type $kif kt kf)
36527695 185(define-cps-type $kreceive arity k)
80b01fd0 186(define-cps-type $kargs names syms body)
8320f504 187(define-cps-type $kfun src meta self tail clause)
80b01fd0 188(define-cps-type $ktail)
90dce16d 189(define-cps-type $kclause arity cont alternate)
80b01fd0
AW
190
191;; Expressions.
80b01fd0
AW
192(define-cps-type $void)
193(define-cps-type $const val)
194(define-cps-type $prim name)
cf8bb037
AW
195(define-cps-type $fun free body) ; Higher-order.
196(define-cps-type $closure label nfree) ; First-order.
80b01fd0 197(define-cps-type $call proc args)
cf8bb037 198(define-cps-type $callk k proc args) ; First-order.
80b01fd0
AW
199(define-cps-type $primcall name args)
200(define-cps-type $values args)
7ab76a83 201(define-cps-type $prompt escape? tag handler)
80b01fd0 202
cf8bb037
AW
203;; The root of a higher-order CPS term is $cont containing a $kfun. The
204;; root of a first-order CPS term is a $program.
205(define-cps-type $program funs)
206
9a1dfb7d
AW
207(define label-counter (make-parameter #f))
208(define var-counter (make-parameter #f))
209
210(define (fresh-label)
828ed944
AW
211 (let ((count (or (label-counter)
212 (error "fresh-label outside with-fresh-name-state"))))
9a1dfb7d
AW
213 (label-counter (1+ count))
214 count))
215
9a1dfb7d 216(define (fresh-var)
1eda52c8 217 (let ((count (or (var-counter)
828ed944 218 (error "fresh-var outside with-fresh-name-state"))))
1eda52c8 219 (var-counter (1+ count))
9a1dfb7d
AW
220 count))
221
222(define-syntax-rule (let-fresh (label ...) (var ...) body ...)
223 (let ((label (fresh-label)) ...
224 (var (fresh-var)) ...)
225 body ...))
226
828ed944 227(define-syntax-rule (with-fresh-name-state fun body ...)
d3dbf75a 228 (call-with-values (lambda () (compute-max-label-and-var fun))
3e1b97c1
AW
229 (lambda (max-label max-var)
230 (parameterize ((label-counter (1+ max-label))
231 (var-counter (1+ max-var)))
232 body ...))))
80b01fd0
AW
233
234(define-syntax build-arity
235 (syntax-rules (unquote)
236 ((_ (unquote exp)) exp)
237 ((_ (req opt rest kw allow-other-keys?))
238 (make-$arity req opt rest kw allow-other-keys?))))
239
240(define-syntax build-cont-body
8320f504 241 (syntax-rules (unquote $kif $kreceive $kargs $kfun $ktail $kclause)
80b01fd0
AW
242 ((_ (unquote exp))
243 exp)
244 ((_ ($kif kt kf))
245 (make-$kif kt kf))
36527695
AW
246 ((_ ($kreceive req rest kargs))
247 (make-$kreceive (make-$arity req '() rest '() #f) kargs))
f5fcd7f2
AW
248 ((_ ($kargs (name ...) (unquote syms) body))
249 (make-$kargs (list name ...) syms (build-cps-term body)))
80b01fd0
AW
250 ((_ ($kargs (name ...) (sym ...) body))
251 (make-$kargs (list name ...) (list sym ...) (build-cps-term body)))
252 ((_ ($kargs names syms body))
253 (make-$kargs names syms (build-cps-term body)))
8320f504
AW
254 ((_ ($kfun src meta self tail clause))
255 (make-$kfun src meta self (build-cps-cont tail) (build-cps-cont clause)))
80b01fd0
AW
256 ((_ ($ktail))
257 (make-$ktail))
90dce16d
AW
258 ((_ ($kclause arity cont alternate))
259 (make-$kclause (build-arity arity) (build-cps-cont cont)
260 (build-cps-cont alternate)))))
80b01fd0
AW
261
262(define-syntax build-cps-cont
263 (syntax-rules (unquote)
264 ((_ (unquote exp)) exp)
6e422a35 265 ((_ (k cont)) (make-$cont k (build-cont-body cont)))))
80b01fd0
AW
266
267(define-syntax build-cps-exp
268 (syntax-rules (unquote
cf8bb037
AW
269 $void $const $prim $fun $closure
270 $call $callk $primcall $values $prompt)
80b01fd0 271 ((_ (unquote exp)) exp)
80b01fd0
AW
272 ((_ ($void)) (make-$void))
273 ((_ ($const val)) (make-$const val))
274 ((_ ($prim name)) (make-$prim name))
cf8bb037
AW
275 ((_ ($fun free body)) (make-$fun free (build-cps-cont body)))
276 ((_ ($closure k nfree)) (make-$closure k nfree))
f5fcd7f2 277 ((_ ($call proc (unquote args))) (make-$call proc args))
80b01fd0
AW
278 ((_ ($call proc (arg ...))) (make-$call proc (list arg ...)))
279 ((_ ($call proc args)) (make-$call proc args))
f5fcd7f2 280 ((_ ($callk k proc (unquote args))) (make-$callk k proc args))
b3ae2b50
AW
281 ((_ ($callk k proc (arg ...))) (make-$callk k proc (list arg ...)))
282 ((_ ($callk k proc args)) (make-$callk k proc args))
f5fcd7f2 283 ((_ ($primcall name (unquote args))) (make-$primcall name args))
80b01fd0
AW
284 ((_ ($primcall name (arg ...))) (make-$primcall name (list arg ...)))
285 ((_ ($primcall name args)) (make-$primcall name args))
f5fcd7f2 286 ((_ ($values (unquote args))) (make-$values args))
80b01fd0
AW
287 ((_ ($values (arg ...))) (make-$values (list arg ...)))
288 ((_ ($values args)) (make-$values args))
7ab76a83
AW
289 ((_ ($prompt escape? tag handler))
290 (make-$prompt escape? tag handler))))
80b01fd0
AW
291
292(define-syntax build-cps-term
cf8bb037 293 (syntax-rules (unquote $letk $letk* $letconst $letrec $program $continue)
80b01fd0
AW
294 ((_ (unquote exp))
295 exp)
296 ((_ ($letk (unquote conts) body))
297 (make-$letk conts (build-cps-term body)))
298 ((_ ($letk (cont ...) body))
299 (make-$letk (list (build-cps-cont cont) ...)
300 (build-cps-term body)))
301 ((_ ($letk* () body))
302 (build-cps-term body))
303 ((_ ($letk* (cont conts ...) body))
304 (build-cps-term ($letk (cont) ($letk* (conts ...) body))))
305 ((_ ($letconst () body))
306 (build-cps-term body))
307 ((_ ($letconst ((name sym val) tail ...) body))
9a1dfb7d 308 (let-fresh (kconst) ()
80b01fd0 309 (build-cps-term
6e422a35
AW
310 ($letk ((kconst ($kargs (name) (sym) ($letconst (tail ...) body))))
311 ($continue kconst (let ((props (source-properties val)))
312 (and (pair? props) props))
313 ($const val))))))
80b01fd0
AW
314 ((_ ($letrec names gensyms funs body))
315 (make-$letrec names gensyms funs (build-cps-term body)))
cf8bb037
AW
316 ((_ ($program (unquote conts)))
317 (make-$program conts))
318 ((_ ($program (cont ...)))
319 (make-$program (list (build-cps-cont cont) ...)))
320 ((_ ($program conts))
321 (make-$program conts))
6e422a35
AW
322 ((_ ($continue k src exp))
323 (make-$continue k src (build-cps-exp exp)))))
80b01fd0
AW
324
325(define-syntax-rule (rewrite-cps-term x (pat body) ...)
326 (match x
327 (pat (build-cps-term body)) ...))
328(define-syntax-rule (rewrite-cps-cont x (pat body) ...)
329 (match x
330 (pat (build-cps-cont body)) ...))
331(define-syntax-rule (rewrite-cps-exp x (pat body) ...)
332 (match x
333 (pat (build-cps-exp body)) ...))
334
335(define (parse-cps exp)
336 (define (src exp)
337 (let ((props (source-properties exp)))
338 (and (pair? props) props)))
339 (match exp
340 ;; Continuations.
341 (('letconst k (name sym c) body)
342 (build-cps-term
6e422a35
AW
343 ($letk ((k ($kargs (name) (sym)
344 ,(parse-cps body))))
345 ($continue k (src exp) ($const c)))))
80b01fd0
AW
346 (('let k (name sym val) body)
347 (build-cps-term
6e422a35
AW
348 ($letk ((k ($kargs (name) (sym)
349 ,(parse-cps body))))
80b01fd0
AW
350 ,(parse-cps val))))
351 (('letk (cont ...) body)
352 (build-cps-term
353 ($letk ,(map parse-cps cont) ,(parse-cps body))))
354 (('k sym body)
355 (build-cps-cont
6e422a35 356 (sym ,(parse-cps body))))
80b01fd0
AW
357 (('kif kt kf)
358 (build-cont-body ($kif kt kf)))
36527695
AW
359 (('kreceive req rest k)
360 (build-cont-body ($kreceive req rest k)))
80b01fd0
AW
361 (('kargs names syms body)
362 (build-cont-body ($kargs names syms ,(parse-cps body))))
8320f504 363 (('kfun src meta self tail clause)
80b01fd0 364 (build-cont-body
8320f504 365 ($kfun (src exp) meta self ,(parse-cps tail)
24b611e8 366 ,(and=> clause parse-cps))))
80b01fd0
AW
367 (('ktail)
368 (build-cont-body
369 ($ktail)))
370 (('kclause (req opt rest kw allow-other-keys?) body)
371 (build-cont-body
372 ($kclause (req opt rest kw allow-other-keys?)
90dce16d
AW
373 ,(parse-cps body)
374 ,#f)))
375 (('kclause (req opt rest kw allow-other-keys?) body alternate)
376 (build-cont-body
377 ($kclause (req opt rest kw allow-other-keys?)
378 ,(parse-cps body)
379 ,(parse-cps alternate))))
80b01fd0
AW
380 (('kseq body)
381 (build-cont-body ($kargs () () ,(parse-cps body))))
382
383 ;; Calls.
384 (('continue k exp)
6e422a35 385 (build-cps-term ($continue k (src exp) ,(parse-cps exp))))
80b01fd0
AW
386 (('void)
387 (build-cps-exp ($void)))
388 (('const exp)
389 (build-cps-exp ($const exp)))
390 (('prim name)
391 (build-cps-exp ($prim name)))
24b611e8
AW
392 (('fun free body)
393 (build-cps-exp ($fun free ,(parse-cps body))))
cf8bb037
AW
394 (('closure k nfree)
395 (build-cps-exp ($closure k nfree)))
80b01fd0
AW
396 (('letrec ((name sym fun) ...) body)
397 (build-cps-term
398 ($letrec name sym (map parse-cps fun) ,(parse-cps body))))
cf8bb037
AW
399 (('program (cont ...))
400 (build-cps-term ($program ,(map parse-cps cont))))
80b01fd0
AW
401 (('call proc arg ...)
402 (build-cps-exp ($call proc arg)))
b3ae2b50
AW
403 (('callk k proc arg ...)
404 (build-cps-exp ($callk k proc arg)))
80b01fd0
AW
405 (('primcall name arg ...)
406 (build-cps-exp ($primcall name arg)))
407 (('values arg ...)
408 (build-cps-exp ($values arg)))
7ab76a83
AW
409 (('prompt escape? tag handler)
410 (build-cps-exp ($prompt escape? tag handler)))
80b01fd0
AW
411 (_
412 (error "unexpected cps" exp))))
413
414(define (unparse-cps exp)
415 (match exp
416 ;; Continuations.
6e422a35
AW
417 (($ $letk (($ $cont k ($ $kargs (name) (sym) body)))
418 ($ $continue k src ($ $const c)))
80b01fd0
AW
419 `(letconst ,k (,name ,sym ,c)
420 ,(unparse-cps body)))
6e422a35 421 (($ $letk (($ $cont k ($ $kargs (name) (sym) body))) val)
80b01fd0
AW
422 `(let ,k (,name ,sym ,(unparse-cps val))
423 ,(unparse-cps body)))
424 (($ $letk conts body)
425 `(letk ,(map unparse-cps conts) ,(unparse-cps body)))
6e422a35 426 (($ $cont sym body)
80b01fd0
AW
427 `(k ,sym ,(unparse-cps body)))
428 (($ $kif kt kf)
429 `(kif ,kt ,kf))
36527695
AW
430 (($ $kreceive ($ $arity req () rest '() #f) k)
431 `(kreceive ,req ,rest ,k))
80b01fd0
AW
432 (($ $kargs () () body)
433 `(kseq ,(unparse-cps body)))
434 (($ $kargs names syms body)
435 `(kargs ,names ,syms ,(unparse-cps body)))
8320f504
AW
436 (($ $kfun src meta self tail clause)
437 `(kfun ,meta ,self ,(unparse-cps tail) ,(unparse-cps clause)))
80b01fd0
AW
438 (($ $ktail)
439 `(ktail))
90dce16d
AW
440 (($ $kclause ($ $arity req opt rest kw allow-other-keys?) body alternate)
441 `(kclause (,req ,opt ,rest ,kw ,allow-other-keys?) ,(unparse-cps body)
442 . ,(if alternate (list (unparse-cps alternate)) '())))
80b01fd0
AW
443
444 ;; Calls.
6e422a35 445 (($ $continue k src exp)
80b01fd0 446 `(continue ,k ,(unparse-cps exp)))
80b01fd0
AW
447 (($ $void)
448 `(void))
449 (($ $const val)
450 `(const ,val))
451 (($ $prim name)
452 `(prim ,name))
24b611e8
AW
453 (($ $fun free body)
454 `(fun ,free ,(unparse-cps body)))
cf8bb037
AW
455 (($ $closure k nfree)
456 `(closure ,k ,nfree))
80b01fd0
AW
457 (($ $letrec names syms funs body)
458 `(letrec ,(map (lambda (name sym fun)
459 (list name sym (unparse-cps fun)))
460 names syms funs)
461 ,(unparse-cps body)))
cf8bb037
AW
462 (($ $program conts)
463 `(program ,(map unparse-cps conts)))
80b01fd0
AW
464 (($ $call proc args)
465 `(call ,proc ,@args))
b3ae2b50
AW
466 (($ $callk k proc args)
467 `(callk ,k ,proc ,@args))
80b01fd0
AW
468 (($ $primcall name args)
469 `(primcall ,name ,@args))
470 (($ $values args)
471 `(values ,@args))
7ab76a83
AW
472 (($ $prompt escape? tag handler)
473 `(prompt ,escape? ,tag ,handler))
80b01fd0
AW
474 (_
475 (error "unexpected cps" exp))))
476
405805fb 477(define-syntax-rule (make-global-cont-folder seed ...)
686a6490 478 (lambda (proc cont seed ...)
828ed944
AW
479 (define (cont-folder cont seed ...)
480 (match cont
481 (($ $cont k cont)
482 (let-values (((seed ...) (proc k cont seed ...)))
483 (match cont
484 (($ $kargs names syms body)
485 (term-folder body seed ...))
486
8320f504 487 (($ $kfun src meta self tail clause)
828ed944 488 (let-values (((seed ...) (cont-folder tail seed ...)))
90dce16d
AW
489 (if clause
490 (cont-folder clause seed ...)
491 (values seed ...))))
492
493 (($ $kclause arity body alternate)
494 (let-values (((seed ...) (cont-folder body seed ...)))
495 (if alternate
496 (cont-folder alternate seed ...)
497 (values seed ...))))
828ed944
AW
498
499 (_ (values seed ...)))))))
500
501 (define (fun-folder fun seed ...)
502 (match fun
24b611e8 503 (($ $fun free body)
828ed944
AW
504 (cont-folder body seed ...))))
505
506 (define (term-folder term seed ...)
507 (match term
508 (($ $letk conts body)
509 (let-values (((seed ...) (term-folder body seed ...)))
2ad91e6b
AW
510 (let lp ((conts conts) (seed seed) ...)
511 (if (null? conts)
512 (values seed ...)
513 (let-values (((seed ...) (cont-folder (car conts) seed ...)))
514 (lp (cdr conts) seed ...))))))
828ed944
AW
515
516 (($ $continue k src exp)
517 (match exp
405805fb 518 (($ $fun) (fun-folder exp seed ...))
828ed944
AW
519 (_ (values seed ...))))
520
521 (($ $letrec names syms funs body)
522 (let-values (((seed ...) (term-folder body seed ...)))
2ad91e6b
AW
523 (let lp ((funs funs) (seed seed) ...)
524 (if (null? funs)
525 (values seed ...)
526 (let-values (((seed ...) (fun-folder (car funs) seed ...)))
527 (lp (cdr funs) seed ...))))))))
828ed944 528
686a6490 529 (cont-folder cont seed ...)))
828ed944 530
405805fb
AW
531(define-syntax-rule (make-local-cont-folder seed ...)
532 (lambda (proc cont seed ...)
533 (define (cont-folder cont seed ...)
534 (match cont
535 (($ $cont k (and cont ($ $kargs names syms body)))
536 (let-values (((seed ...) (proc k cont seed ...)))
537 (term-folder body seed ...)))
538 (($ $cont k cont)
539 (proc k cont seed ...))))
540 (define (term-folder term seed ...)
541 (match term
542 (($ $letk conts body)
543 (let-values (((seed ...) (term-folder body seed ...)))
544 (let lp ((conts conts) (seed seed) ...)
545 (match conts
546 (() (values seed ...))
547 ((cont) (cont-folder cont seed ...))
548 ((cont . conts)
549 (let-values (((seed ...) (cont-folder cont seed ...)))
550 (lp conts seed ...)))))))
551 (($ $letrec names syms funs body) (term-folder body seed ...))
552 (_ (values seed ...))))
553 (define (clause-folder clause seed ...)
554 (match clause
555 (($ $cont k (and cont ($ $kclause arity body alternate)))
556 (let-values (((seed ...) (proc k cont seed ...)))
557 (if alternate
558 (let-values (((seed ...) (cont-folder body seed ...)))
559 (clause-folder alternate seed ...))
560 (cont-folder body seed ...))))))
561 (match cont
562 (($ $cont k (and cont ($ $kfun src meta self tail clause)))
563 (let*-values (((seed ...) (proc k cont seed ...))
564 ((seed ...) (if clause
565 (clause-folder clause seed ...)
566 (values seed ...))))
567 (cont-folder tail seed ...))))))
568
828ed944 569(define (compute-max-label-and-var fun)
cf8bb037
AW
570 (match fun
571 (($ $cont)
572 ((make-global-cont-folder max-label max-var)
573 (lambda (label cont max-label max-var)
574 (values (max label max-label)
575 (match cont
576 (($ $kargs names vars body)
577 (let lp ((body body) (max-var (fold max max-var vars)))
578 (match body
579 (($ $letk conts body) (lp body max-var))
580 (($ $letrec names vars funs body)
581 (lp body (fold max max-var vars)))
582 (_ max-var))))
583 (($ $kfun src meta self)
584 (max self max-var))
585 (_ max-var))))
586 fun -1 -1))
587 (($ $program conts)
588 (define (fold/2 proc in s0 s1)
589 (if (null? in)
590 (values s0 s1)
591 (let-values (((s0 s1) (proc (car in) s0 s1)))
592 (fold/2 proc (cdr in) s0 s1))))
593 (let lp ((conts conts) (max-label -1) (max-var -1))
594 (if (null? conts)
595 (values max-label max-var)
596 (call-with-values (lambda ()
597 ((make-local-cont-folder max-label max-var)
598 (lambda (label cont max-label max-var)
599 (values (max label max-label)
600 (match cont
601 (($ $kargs names vars body)
602 (fold max max-var vars))
603 (($ $kfun src meta self)
604 (max self max-var))
605 (_ max-var))))
606 (car conts) max-label max-var))
607 (lambda (max-label max-var)
608 (lp (cdr conts) max-label max-var))))))))
80b01fd0 609
828ed944 610(define (fold-conts proc seed fun)
405805fb 611 ((make-global-cont-folder seed) proc fun seed))
80b01fd0 612
a6f823bd 613(define (fold-local-conts proc seed fun)
405805fb 614 ((make-local-cont-folder seed) proc fun seed))
2c3c086e
AW
615
616(define (visit-cont-successors proc cont)
617 (match cont
618 (($ $kargs names syms body)
619 (let lp ((body body))
620 (match body
621 (($ $letk conts body) (lp body))
622 (($ $letrec names vars funs body) (lp body))
623 (($ $continue k src exp)
624 (match exp
625 (($ $prompt escape? tag handler) (proc k handler))
626 (_ (proc k)))))))
627
628 (($ $kif kt kf) (proc kt kf))
629
630 (($ $kreceive arity k) (proc k))
631
632 (($ $kclause arity ($ $cont kbody) #f) (proc kbody))
633
634 (($ $kclause arity ($ $cont kbody) ($ $cont kalt)) (proc kbody kalt))
635
8320f504 636 (($ $kfun src meta self tail ($ $cont clause)) (proc clause))
2c3c086e 637
8320f504 638 (($ $kfun src meta self tail #f) (proc))
2c3c086e
AW
639
640 (($ $ktail) (proc))))