Rename $ktrunc to $kreceive
[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;;;
60;;; - $kentry labels an entry point for a $fun (a function), and
61;;; contains a $ktail representing the formal argument which is the
62;;; function's continuation.
63;;;
64;;; - $kentry also contains $kclause continuations, corresponding to
65;;; the case-lambda clauses of the function. $kclause actually
66;;; contains the clause body. This is because the $kclause
67;;; logically matches or doesn't match a given set of actual
68;;; arguments against a formal arity, then proceeds to a "body"
69;;; continuation (which is a $kargs).
70;;;
71;;; That's to say that a $fun can be matched like this:
72;;;
73;;; (match f
6e422a35
AW
74;;; (($ $fun src meta free
75;;; ($ $cont kentry
80b01fd0
AW
76;;; ($ $kentry self ($ $cont ktail _ ($ $ktail))
77;;; (($ $kclause arity
78;;; ($ $cont kbody _ ($ $kargs names syms body)))
79;;; ...))))
80;;; #t))
81;;;
82;;; A $continue to ktail is in tail position. $kentry, $kclause,
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)
110 #:export (;; Helper.
111 $arity
112 make-$arity
113
114 ;; Terms.
115 $letk $continue $letrec
116
117 ;; Continuations.
118 $cont
119
120 ;; Continuation bodies.
36527695 121 $kif $kreceive $kargs $kentry $ktail $kclause
80b01fd0
AW
122
123 ;; Expressions.
13085a82 124 $void $const $prim $fun $call $primcall $values $prompt
80b01fd0
AW
125
126 ;; Building macros.
127 let-gensyms
128 build-cps-term build-cps-cont build-cps-exp
129 rewrite-cps-term rewrite-cps-cont rewrite-cps-exp
130
131 ;; Misc.
132 parse-cps unparse-cps
133 fold-conts fold-local-conts))
134
135;; FIXME: Use SRFI-99, when Guile adds it.
136(define-syntax define-record-type*
137 (lambda (x)
138 (define (id-append ctx . syms)
139 (datum->syntax ctx (apply symbol-append (map syntax->datum syms))))
140 (syntax-case x ()
141 ((_ name field ...)
142 (and (identifier? #'name) (and-map identifier? #'(field ...)))
143 (with-syntax ((cons (id-append #'name #'make- #'name))
144 (pred (id-append #'name #'name #'?))
145 ((getter ...) (map (lambda (f)
146 (id-append f #'name #'- f))
147 #'(field ...))))
148 #'(define-record-type name
149 (cons field ...)
150 pred
151 (field getter)
152 ...))))))
153
154(define-syntax-rule (define-cps-type name field ...)
155 (begin
156 (define-record-type* name field ...)
157 (set-record-type-printer! name print-cps)))
158
159(define (print-cps exp port)
160 (format port "#<cps ~S>" (unparse-cps exp)))
161
162;; Helper.
163(define-record-type* $arity req opt rest kw allow-other-keys?)
164
165;; Terms.
166(define-cps-type $letk conts body)
6e422a35 167(define-cps-type $continue k src exp)
80b01fd0
AW
168(define-cps-type $letrec names syms funs body)
169
170;; Continuations
6e422a35 171(define-cps-type $cont k cont)
80b01fd0 172(define-cps-type $kif kt kf)
36527695 173(define-cps-type $kreceive arity k)
80b01fd0
AW
174(define-cps-type $kargs names syms body)
175(define-cps-type $kentry self tail clauses)
176(define-cps-type $ktail)
177(define-cps-type $kclause arity cont)
178
179;; Expressions.
80b01fd0
AW
180(define-cps-type $void)
181(define-cps-type $const val)
182(define-cps-type $prim name)
6e422a35 183(define-cps-type $fun src meta free body)
80b01fd0
AW
184(define-cps-type $call proc args)
185(define-cps-type $primcall name args)
186(define-cps-type $values args)
7ab76a83 187(define-cps-type $prompt escape? tag handler)
80b01fd0
AW
188
189(define-syntax let-gensyms
190 (syntax-rules ()
191 ((_ (sym ...) body body* ...)
192 (let ((sym (gensym (symbol->string 'sym))) ...)
193 body body* ...))))
194
195(define-syntax build-arity
196 (syntax-rules (unquote)
197 ((_ (unquote exp)) exp)
198 ((_ (req opt rest kw allow-other-keys?))
199 (make-$arity req opt rest kw allow-other-keys?))))
200
201(define-syntax build-cont-body
36527695 202 (syntax-rules (unquote $kif $kreceive $kargs $kentry $ktail $kclause)
80b01fd0
AW
203 ((_ (unquote exp))
204 exp)
205 ((_ ($kif kt kf))
206 (make-$kif kt kf))
36527695
AW
207 ((_ ($kreceive req rest kargs))
208 (make-$kreceive (make-$arity req '() rest '() #f) kargs))
80b01fd0
AW
209 ((_ ($kargs (name ...) (sym ...) body))
210 (make-$kargs (list name ...) (list sym ...) (build-cps-term body)))
211 ((_ ($kargs names syms body))
212 (make-$kargs names syms (build-cps-term body)))
213 ((_ ($kentry self tail (unquote clauses)))
214 (make-$kentry self (build-cps-cont tail) clauses))
215 ((_ ($kentry self tail (clause ...)))
216 (make-$kentry self (build-cps-cont tail) (list (build-cps-cont clause) ...)))
217 ((_ ($ktail))
218 (make-$ktail))
219 ((_ ($kclause arity cont))
220 (make-$kclause (build-arity arity) (build-cps-cont cont)))))
221
222(define-syntax build-cps-cont
223 (syntax-rules (unquote)
224 ((_ (unquote exp)) exp)
6e422a35 225 ((_ (k cont)) (make-$cont k (build-cont-body cont)))))
80b01fd0
AW
226
227(define-syntax build-cps-exp
228 (syntax-rules (unquote
13085a82 229 $void $const $prim $fun $call $primcall $values $prompt)
80b01fd0 230 ((_ (unquote exp)) exp)
80b01fd0
AW
231 ((_ ($void)) (make-$void))
232 ((_ ($const val)) (make-$const val))
233 ((_ ($prim name)) (make-$prim name))
6e422a35
AW
234 ((_ ($fun src meta free body))
235 (make-$fun src meta free (build-cps-cont body)))
80b01fd0
AW
236 ((_ ($call proc (arg ...))) (make-$call proc (list arg ...)))
237 ((_ ($call proc args)) (make-$call proc args))
238 ((_ ($primcall name (arg ...))) (make-$primcall name (list arg ...)))
239 ((_ ($primcall name args)) (make-$primcall name args))
240 ((_ ($values (arg ...))) (make-$values (list arg ...)))
241 ((_ ($values args)) (make-$values args))
7ab76a83
AW
242 ((_ ($prompt escape? tag handler))
243 (make-$prompt escape? tag handler))))
80b01fd0
AW
244
245(define-syntax build-cps-term
246 (syntax-rules (unquote $letk $letk* $letconst $letrec $continue)
247 ((_ (unquote exp))
248 exp)
249 ((_ ($letk (unquote conts) body))
250 (make-$letk conts (build-cps-term body)))
251 ((_ ($letk (cont ...) body))
252 (make-$letk (list (build-cps-cont cont) ...)
253 (build-cps-term body)))
254 ((_ ($letk* () body))
255 (build-cps-term body))
256 ((_ ($letk* (cont conts ...) body))
257 (build-cps-term ($letk (cont) ($letk* (conts ...) body))))
258 ((_ ($letconst () body))
259 (build-cps-term body))
260 ((_ ($letconst ((name sym val) tail ...) body))
261 (let-gensyms (kconst)
262 (build-cps-term
6e422a35
AW
263 ($letk ((kconst ($kargs (name) (sym) ($letconst (tail ...) body))))
264 ($continue kconst (let ((props (source-properties val)))
265 (and (pair? props) props))
266 ($const val))))))
80b01fd0
AW
267 ((_ ($letrec names gensyms funs body))
268 (make-$letrec names gensyms funs (build-cps-term body)))
6e422a35
AW
269 ((_ ($continue k src exp))
270 (make-$continue k src (build-cps-exp exp)))))
80b01fd0
AW
271
272(define-syntax-rule (rewrite-cps-term x (pat body) ...)
273 (match x
274 (pat (build-cps-term body)) ...))
275(define-syntax-rule (rewrite-cps-cont x (pat body) ...)
276 (match x
277 (pat (build-cps-cont body)) ...))
278(define-syntax-rule (rewrite-cps-exp x (pat body) ...)
279 (match x
280 (pat (build-cps-exp body)) ...))
281
282(define (parse-cps exp)
283 (define (src exp)
284 (let ((props (source-properties exp)))
285 (and (pair? props) props)))
286 (match exp
287 ;; Continuations.
288 (('letconst k (name sym c) body)
289 (build-cps-term
6e422a35
AW
290 ($letk ((k ($kargs (name) (sym)
291 ,(parse-cps body))))
292 ($continue k (src exp) ($const c)))))
80b01fd0
AW
293 (('let k (name sym val) body)
294 (build-cps-term
6e422a35
AW
295 ($letk ((k ($kargs (name) (sym)
296 ,(parse-cps body))))
80b01fd0
AW
297 ,(parse-cps val))))
298 (('letk (cont ...) body)
299 (build-cps-term
300 ($letk ,(map parse-cps cont) ,(parse-cps body))))
301 (('k sym body)
302 (build-cps-cont
6e422a35 303 (sym ,(parse-cps body))))
80b01fd0
AW
304 (('kif kt kf)
305 (build-cont-body ($kif kt kf)))
36527695
AW
306 (('kreceive req rest k)
307 (build-cont-body ($kreceive req rest k)))
80b01fd0
AW
308 (('kargs names syms body)
309 (build-cont-body ($kargs names syms ,(parse-cps body))))
310 (('kentry self tail clauses)
311 (build-cont-body
312 ($kentry self ,(parse-cps tail) ,(map parse-cps clauses))))
313 (('ktail)
314 (build-cont-body
315 ($ktail)))
316 (('kclause (req opt rest kw allow-other-keys?) body)
317 (build-cont-body
318 ($kclause (req opt rest kw allow-other-keys?)
319 ,(parse-cps body))))
320 (('kseq body)
321 (build-cont-body ($kargs () () ,(parse-cps body))))
322
323 ;; Calls.
324 (('continue k exp)
6e422a35 325 (build-cps-term ($continue k (src exp) ,(parse-cps exp))))
80b01fd0
AW
326 (('void)
327 (build-cps-exp ($void)))
328 (('const exp)
329 (build-cps-exp ($const exp)))
330 (('prim name)
331 (build-cps-exp ($prim name)))
332 (('fun meta free body)
6e422a35 333 (build-cps-exp ($fun (src exp) meta free ,(parse-cps body))))
80b01fd0
AW
334 (('letrec ((name sym fun) ...) body)
335 (build-cps-term
336 ($letrec name sym (map parse-cps fun) ,(parse-cps body))))
337 (('call proc arg ...)
338 (build-cps-exp ($call proc arg)))
339 (('primcall name arg ...)
340 (build-cps-exp ($primcall name arg)))
341 (('values arg ...)
342 (build-cps-exp ($values arg)))
7ab76a83
AW
343 (('prompt escape? tag handler)
344 (build-cps-exp ($prompt escape? tag handler)))
80b01fd0
AW
345 (_
346 (error "unexpected cps" exp))))
347
348(define (unparse-cps exp)
349 (match exp
350 ;; Continuations.
6e422a35
AW
351 (($ $letk (($ $cont k ($ $kargs (name) (sym) body)))
352 ($ $continue k src ($ $const c)))
80b01fd0
AW
353 `(letconst ,k (,name ,sym ,c)
354 ,(unparse-cps body)))
6e422a35 355 (($ $letk (($ $cont k ($ $kargs (name) (sym) body))) val)
80b01fd0
AW
356 `(let ,k (,name ,sym ,(unparse-cps val))
357 ,(unparse-cps body)))
358 (($ $letk conts body)
359 `(letk ,(map unparse-cps conts) ,(unparse-cps body)))
6e422a35 360 (($ $cont sym body)
80b01fd0
AW
361 `(k ,sym ,(unparse-cps body)))
362 (($ $kif kt kf)
363 `(kif ,kt ,kf))
36527695
AW
364 (($ $kreceive ($ $arity req () rest '() #f) k)
365 `(kreceive ,req ,rest ,k))
80b01fd0
AW
366 (($ $kargs () () body)
367 `(kseq ,(unparse-cps body)))
368 (($ $kargs names syms body)
369 `(kargs ,names ,syms ,(unparse-cps body)))
370 (($ $kentry self tail clauses)
371 `(kentry ,self ,(unparse-cps tail) ,(map unparse-cps clauses)))
372 (($ $ktail)
373 `(ktail))
374 (($ $kclause ($ $arity req opt rest kw allow-other-keys?) body)
375 `(kclause (,req ,opt ,rest ,kw ,allow-other-keys?) ,(unparse-cps body)))
376
377 ;; Calls.
6e422a35 378 (($ $continue k src exp)
80b01fd0 379 `(continue ,k ,(unparse-cps exp)))
80b01fd0
AW
380 (($ $void)
381 `(void))
382 (($ $const val)
383 `(const ,val))
384 (($ $prim name)
385 `(prim ,name))
6e422a35 386 (($ $fun src meta free body)
80b01fd0
AW
387 `(fun ,meta ,free ,(unparse-cps body)))
388 (($ $letrec names syms funs body)
389 `(letrec ,(map (lambda (name sym fun)
390 (list name sym (unparse-cps fun)))
391 names syms funs)
392 ,(unparse-cps body)))
393 (($ $call proc args)
394 `(call ,proc ,@args))
395 (($ $primcall name args)
396 `(primcall ,name ,@args))
397 (($ $values args)
398 `(values ,@args))
7ab76a83
AW
399 (($ $prompt escape? tag handler)
400 `(prompt ,escape? ,tag ,handler))
80b01fd0
AW
401 (_
402 (error "unexpected cps" exp))))
403
404(define (fold-conts proc seed fun)
405 (define (cont-folder cont seed)
406 (match cont
6e422a35
AW
407 (($ $cont k cont)
408 (let ((seed (proc k cont seed)))
80b01fd0
AW
409 (match cont
410 (($ $kargs names syms body)
411 (term-folder body seed))
412
413 (($ $kentry self tail clauses)
414 (fold cont-folder (cont-folder tail seed) clauses))
415
416 (($ $kclause arity body)
417 (cont-folder body seed))
418
419 (_ seed))))))
420
421 (define (fun-folder fun seed)
422 (match fun
6e422a35 423 (($ $fun src meta free body)
80b01fd0
AW
424 (cont-folder body seed))))
425
426 (define (term-folder term seed)
427 (match term
428 (($ $letk conts body)
429 (fold cont-folder (term-folder body seed) conts))
430
6e422a35 431 (($ $continue k src exp)
80b01fd0
AW
432 (match exp
433 (($ $fun) (fun-folder exp seed))
434 (_ seed)))
435
436 (($ $letrec names syms funs body)
437 (fold fun-folder (term-folder body seed) funs))))
438
439 (fun-folder fun seed))
440
441(define (fold-local-conts proc seed cont)
442 (define (cont-folder cont seed)
443 (match cont
6e422a35
AW
444 (($ $cont k cont)
445 (let ((seed (proc k cont seed)))
80b01fd0
AW
446 (match cont
447 (($ $kargs names syms body)
448 (term-folder body seed))
449
450 (($ $kentry self tail clauses)
451 (fold cont-folder (cont-folder tail seed) clauses))
452
453 (($ $kclause arity body)
454 (cont-folder body seed))
455
456 (_ seed))))))
457
458 (define (term-folder term seed)
459 (match term
460 (($ $letk conts body)
461 (fold cont-folder (term-folder body seed) conts))
462
463 (($ $continue) seed)
464
465 (($ $letrec names syms funs body) (term-folder body seed))))
466
467 (cont-folder cont seed))