Commit | Line | Data |
---|---|---|
80b01fd0 AW |
1 | ;;; Continuation-passing style (CPS) intermediate language (IL) |
2 | ||
3 | ;; Copyright (C) 2013 Free Software Foundation, Inc. | |
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 | ;;; | |
56 | ;;; - $ktrunc represents a continuation that receives multiple values, | |
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 | |
96af4a18 AW |
88 | ;;; popping the prompt. A $prompt also records the continuation |
89 | ;;; that pops the prompt, to make various static analyses easier. | |
80b01fd0 AW |
90 | ;;; |
91 | ;;; In summary: | |
92 | ;;; | |
93 | ;;; - $letk, $letrec, and $continue are terms. | |
94 | ;;; | |
95 | ;;; - $cont is a continuation, containing a continuation body ($kargs, | |
96 | ;;; $kif, etc). | |
97 | ;;; | |
98 | ;;; - $continue terms contain an expression ($call, $const, $fun, | |
99 | ;;; etc). | |
100 | ;;; | |
101 | ;;; See (language tree-il compile-cps) for details on how Tree-IL | |
102 | ;;; converts to CPS. | |
103 | ;;; | |
104 | ;;; Code: | |
105 | ||
106 | (define-module (language cps) | |
107 | #:use-module (ice-9 match) | |
108 | #:use-module ((srfi srfi-1) #:select (fold)) | |
109 | #:use-module (srfi srfi-9) | |
110 | #:use-module (srfi srfi-9 gnu) | |
111 | #:export (;; Helper. | |
112 | $arity | |
113 | make-$arity | |
114 | ||
115 | ;; Terms. | |
116 | $letk $continue $letrec | |
117 | ||
118 | ;; Continuations. | |
119 | $cont | |
120 | ||
121 | ;; Continuation bodies. | |
122 | $kif $ktrunc $kargs $kentry $ktail $kclause | |
123 | ||
124 | ;; Expressions. | |
13085a82 | 125 | $void $const $prim $fun $call $primcall $values $prompt |
80b01fd0 AW |
126 | |
127 | ;; Building macros. | |
128 | let-gensyms | |
129 | build-cps-term build-cps-cont build-cps-exp | |
130 | rewrite-cps-term rewrite-cps-cont rewrite-cps-exp | |
131 | ||
132 | ;; Misc. | |
133 | parse-cps unparse-cps | |
134 | fold-conts fold-local-conts)) | |
135 | ||
136 | ;; FIXME: Use SRFI-99, when Guile adds it. | |
137 | (define-syntax define-record-type* | |
138 | (lambda (x) | |
139 | (define (id-append ctx . syms) | |
140 | (datum->syntax ctx (apply symbol-append (map syntax->datum syms)))) | |
141 | (syntax-case x () | |
142 | ((_ name field ...) | |
143 | (and (identifier? #'name) (and-map identifier? #'(field ...))) | |
144 | (with-syntax ((cons (id-append #'name #'make- #'name)) | |
145 | (pred (id-append #'name #'name #'?)) | |
146 | ((getter ...) (map (lambda (f) | |
147 | (id-append f #'name #'- f)) | |
148 | #'(field ...)))) | |
149 | #'(define-record-type name | |
150 | (cons field ...) | |
151 | pred | |
152 | (field getter) | |
153 | ...)))))) | |
154 | ||
155 | (define-syntax-rule (define-cps-type name field ...) | |
156 | (begin | |
157 | (define-record-type* name field ...) | |
158 | (set-record-type-printer! name print-cps))) | |
159 | ||
160 | (define (print-cps exp port) | |
161 | (format port "#<cps ~S>" (unparse-cps exp))) | |
162 | ||
163 | ;; Helper. | |
164 | (define-record-type* $arity req opt rest kw allow-other-keys?) | |
165 | ||
166 | ;; Terms. | |
167 | (define-cps-type $letk conts body) | |
6e422a35 | 168 | (define-cps-type $continue k src exp) |
80b01fd0 AW |
169 | (define-cps-type $letrec names syms funs body) |
170 | ||
171 | ;; Continuations | |
6e422a35 | 172 | (define-cps-type $cont k cont) |
80b01fd0 AW |
173 | (define-cps-type $kif kt kf) |
174 | (define-cps-type $ktrunc arity k) | |
175 | (define-cps-type $kargs names syms body) | |
176 | (define-cps-type $kentry self tail clauses) | |
177 | (define-cps-type $ktail) | |
178 | (define-cps-type $kclause arity cont) | |
179 | ||
180 | ;; Expressions. | |
80b01fd0 AW |
181 | (define-cps-type $void) |
182 | (define-cps-type $const val) | |
183 | (define-cps-type $prim name) | |
6e422a35 | 184 | (define-cps-type $fun src meta free body) |
80b01fd0 AW |
185 | (define-cps-type $call proc args) |
186 | (define-cps-type $primcall name args) | |
187 | (define-cps-type $values args) | |
96af4a18 | 188 | (define-cps-type $prompt escape? tag handler pop) |
80b01fd0 AW |
189 | |
190 | (define-syntax let-gensyms | |
191 | (syntax-rules () | |
192 | ((_ (sym ...) body body* ...) | |
193 | (let ((sym (gensym (symbol->string 'sym))) ...) | |
194 | body body* ...)))) | |
195 | ||
196 | (define-syntax build-arity | |
197 | (syntax-rules (unquote) | |
198 | ((_ (unquote exp)) exp) | |
199 | ((_ (req opt rest kw allow-other-keys?)) | |
200 | (make-$arity req opt rest kw allow-other-keys?)))) | |
201 | ||
202 | (define-syntax build-cont-body | |
203 | (syntax-rules (unquote $kif $ktrunc $kargs $kentry $ktail $kclause) | |
204 | ((_ (unquote exp)) | |
205 | exp) | |
206 | ((_ ($kif kt kf)) | |
207 | (make-$kif kt kf)) | |
208 | ((_ ($ktrunc req rest kargs)) | |
209 | (make-$ktrunc (make-$arity req '() rest '() #f) kargs)) | |
210 | ((_ ($kargs (name ...) (sym ...) body)) | |
211 | (make-$kargs (list name ...) (list sym ...) (build-cps-term body))) | |
212 | ((_ ($kargs names syms body)) | |
213 | (make-$kargs names syms (build-cps-term body))) | |
214 | ((_ ($kentry self tail (unquote clauses))) | |
215 | (make-$kentry self (build-cps-cont tail) clauses)) | |
216 | ((_ ($kentry self tail (clause ...))) | |
217 | (make-$kentry self (build-cps-cont tail) (list (build-cps-cont clause) ...))) | |
218 | ((_ ($ktail)) | |
219 | (make-$ktail)) | |
220 | ((_ ($kclause arity cont)) | |
221 | (make-$kclause (build-arity arity) (build-cps-cont cont))))) | |
222 | ||
223 | (define-syntax build-cps-cont | |
224 | (syntax-rules (unquote) | |
225 | ((_ (unquote exp)) exp) | |
6e422a35 | 226 | ((_ (k cont)) (make-$cont k (build-cont-body cont))))) |
80b01fd0 AW |
227 | |
228 | (define-syntax build-cps-exp | |
229 | (syntax-rules (unquote | |
13085a82 | 230 | $void $const $prim $fun $call $primcall $values $prompt) |
80b01fd0 | 231 | ((_ (unquote exp)) exp) |
80b01fd0 AW |
232 | ((_ ($void)) (make-$void)) |
233 | ((_ ($const val)) (make-$const val)) | |
234 | ((_ ($prim name)) (make-$prim name)) | |
6e422a35 AW |
235 | ((_ ($fun src meta free body)) |
236 | (make-$fun src meta free (build-cps-cont body))) | |
80b01fd0 AW |
237 | ((_ ($call proc (arg ...))) (make-$call proc (list arg ...))) |
238 | ((_ ($call proc args)) (make-$call proc args)) | |
239 | ((_ ($primcall name (arg ...))) (make-$primcall name (list arg ...))) | |
240 | ((_ ($primcall name args)) (make-$primcall name args)) | |
241 | ((_ ($values (arg ...))) (make-$values (list arg ...))) | |
242 | ((_ ($values args)) (make-$values args)) | |
96af4a18 AW |
243 | ((_ ($prompt escape? tag handler pop)) |
244 | (make-$prompt escape? tag handler pop)))) | |
80b01fd0 AW |
245 | |
246 | (define-syntax build-cps-term | |
247 | (syntax-rules (unquote $letk $letk* $letconst $letrec $continue) | |
248 | ((_ (unquote exp)) | |
249 | exp) | |
250 | ((_ ($letk (unquote conts) body)) | |
251 | (make-$letk conts (build-cps-term body))) | |
252 | ((_ ($letk (cont ...) body)) | |
253 | (make-$letk (list (build-cps-cont cont) ...) | |
254 | (build-cps-term body))) | |
255 | ((_ ($letk* () body)) | |
256 | (build-cps-term body)) | |
257 | ((_ ($letk* (cont conts ...) body)) | |
258 | (build-cps-term ($letk (cont) ($letk* (conts ...) body)))) | |
259 | ((_ ($letconst () body)) | |
260 | (build-cps-term body)) | |
261 | ((_ ($letconst ((name sym val) tail ...) body)) | |
262 | (let-gensyms (kconst) | |
263 | (build-cps-term | |
6e422a35 AW |
264 | ($letk ((kconst ($kargs (name) (sym) ($letconst (tail ...) body)))) |
265 | ($continue kconst (let ((props (source-properties val))) | |
266 | (and (pair? props) props)) | |
267 | ($const val)))))) | |
80b01fd0 AW |
268 | ((_ ($letrec names gensyms funs body)) |
269 | (make-$letrec names gensyms funs (build-cps-term body))) | |
6e422a35 AW |
270 | ((_ ($continue k src exp)) |
271 | (make-$continue k src (build-cps-exp exp))))) | |
80b01fd0 AW |
272 | |
273 | (define-syntax-rule (rewrite-cps-term x (pat body) ...) | |
274 | (match x | |
275 | (pat (build-cps-term body)) ...)) | |
276 | (define-syntax-rule (rewrite-cps-cont x (pat body) ...) | |
277 | (match x | |
278 | (pat (build-cps-cont body)) ...)) | |
279 | (define-syntax-rule (rewrite-cps-exp x (pat body) ...) | |
280 | (match x | |
281 | (pat (build-cps-exp body)) ...)) | |
282 | ||
283 | (define (parse-cps exp) | |
284 | (define (src exp) | |
285 | (let ((props (source-properties exp))) | |
286 | (and (pair? props) props))) | |
287 | (match exp | |
288 | ;; Continuations. | |
289 | (('letconst k (name sym c) body) | |
290 | (build-cps-term | |
6e422a35 AW |
291 | ($letk ((k ($kargs (name) (sym) |
292 | ,(parse-cps body)))) | |
293 | ($continue k (src exp) ($const c))))) | |
80b01fd0 AW |
294 | (('let k (name sym val) body) |
295 | (build-cps-term | |
6e422a35 AW |
296 | ($letk ((k ($kargs (name) (sym) |
297 | ,(parse-cps body)))) | |
80b01fd0 AW |
298 | ,(parse-cps val)))) |
299 | (('letk (cont ...) body) | |
300 | (build-cps-term | |
301 | ($letk ,(map parse-cps cont) ,(parse-cps body)))) | |
302 | (('k sym body) | |
303 | (build-cps-cont | |
6e422a35 | 304 | (sym ,(parse-cps body)))) |
80b01fd0 AW |
305 | (('kif kt kf) |
306 | (build-cont-body ($kif kt kf))) | |
307 | (('ktrunc req rest k) | |
308 | (build-cont-body ($ktrunc req rest k))) | |
309 | (('kargs names syms body) | |
310 | (build-cont-body ($kargs names syms ,(parse-cps body)))) | |
311 | (('kentry self tail clauses) | |
312 | (build-cont-body | |
313 | ($kentry self ,(parse-cps tail) ,(map parse-cps clauses)))) | |
314 | (('ktail) | |
315 | (build-cont-body | |
316 | ($ktail))) | |
317 | (('kclause (req opt rest kw allow-other-keys?) body) | |
318 | (build-cont-body | |
319 | ($kclause (req opt rest kw allow-other-keys?) | |
320 | ,(parse-cps body)))) | |
321 | (('kseq body) | |
322 | (build-cont-body ($kargs () () ,(parse-cps body)))) | |
323 | ||
324 | ;; Calls. | |
325 | (('continue k exp) | |
6e422a35 | 326 | (build-cps-term ($continue k (src exp) ,(parse-cps exp)))) |
80b01fd0 AW |
327 | (('void) |
328 | (build-cps-exp ($void))) | |
329 | (('const exp) | |
330 | (build-cps-exp ($const exp))) | |
331 | (('prim name) | |
332 | (build-cps-exp ($prim name))) | |
333 | (('fun meta free body) | |
6e422a35 | 334 | (build-cps-exp ($fun (src exp) meta free ,(parse-cps body)))) |
80b01fd0 AW |
335 | (('letrec ((name sym fun) ...) body) |
336 | (build-cps-term | |
337 | ($letrec name sym (map parse-cps fun) ,(parse-cps body)))) | |
338 | (('call proc arg ...) | |
339 | (build-cps-exp ($call proc arg))) | |
340 | (('primcall name arg ...) | |
341 | (build-cps-exp ($primcall name arg))) | |
342 | (('values arg ...) | |
343 | (build-cps-exp ($values arg))) | |
96af4a18 AW |
344 | (('prompt escape? tag handler pop) |
345 | (build-cps-exp ($prompt escape? tag handler pop))) | |
80b01fd0 AW |
346 | (_ |
347 | (error "unexpected cps" exp)))) | |
348 | ||
349 | (define (unparse-cps exp) | |
350 | (match exp | |
351 | ;; Continuations. | |
6e422a35 AW |
352 | (($ $letk (($ $cont k ($ $kargs (name) (sym) body))) |
353 | ($ $continue k src ($ $const c))) | |
80b01fd0 AW |
354 | `(letconst ,k (,name ,sym ,c) |
355 | ,(unparse-cps body))) | |
6e422a35 | 356 | (($ $letk (($ $cont k ($ $kargs (name) (sym) body))) val) |
80b01fd0 AW |
357 | `(let ,k (,name ,sym ,(unparse-cps val)) |
358 | ,(unparse-cps body))) | |
359 | (($ $letk conts body) | |
360 | `(letk ,(map unparse-cps conts) ,(unparse-cps body))) | |
6e422a35 | 361 | (($ $cont sym body) |
80b01fd0 AW |
362 | `(k ,sym ,(unparse-cps body))) |
363 | (($ $kif kt kf) | |
364 | `(kif ,kt ,kf)) | |
365 | (($ $ktrunc ($ $arity req () rest '() #f) k) | |
366 | `(ktrunc ,req ,rest ,k)) | |
367 | (($ $kargs () () body) | |
368 | `(kseq ,(unparse-cps body))) | |
369 | (($ $kargs names syms body) | |
370 | `(kargs ,names ,syms ,(unparse-cps body))) | |
371 | (($ $kentry self tail clauses) | |
372 | `(kentry ,self ,(unparse-cps tail) ,(map unparse-cps clauses))) | |
373 | (($ $ktail) | |
374 | `(ktail)) | |
375 | (($ $kclause ($ $arity req opt rest kw allow-other-keys?) body) | |
376 | `(kclause (,req ,opt ,rest ,kw ,allow-other-keys?) ,(unparse-cps body))) | |
377 | ||
378 | ;; Calls. | |
6e422a35 | 379 | (($ $continue k src exp) |
80b01fd0 | 380 | `(continue ,k ,(unparse-cps exp))) |
80b01fd0 AW |
381 | (($ $void) |
382 | `(void)) | |
383 | (($ $const val) | |
384 | `(const ,val)) | |
385 | (($ $prim name) | |
386 | `(prim ,name)) | |
6e422a35 | 387 | (($ $fun src meta free body) |
80b01fd0 AW |
388 | `(fun ,meta ,free ,(unparse-cps body))) |
389 | (($ $letrec names syms funs body) | |
390 | `(letrec ,(map (lambda (name sym fun) | |
391 | (list name sym (unparse-cps fun))) | |
392 | names syms funs) | |
393 | ,(unparse-cps body))) | |
394 | (($ $call proc args) | |
395 | `(call ,proc ,@args)) | |
396 | (($ $primcall name args) | |
397 | `(primcall ,name ,@args)) | |
398 | (($ $values args) | |
399 | `(values ,@args)) | |
96af4a18 AW |
400 | (($ $prompt escape? tag handler pop) |
401 | `(prompt ,escape? ,tag ,handler ,pop)) | |
80b01fd0 AW |
402 | (_ |
403 | (error "unexpected cps" exp)))) | |
404 | ||
405 | (define (fold-conts proc seed fun) | |
406 | (define (cont-folder cont seed) | |
407 | (match cont | |
6e422a35 AW |
408 | (($ $cont k cont) |
409 | (let ((seed (proc k cont seed))) | |
80b01fd0 AW |
410 | (match cont |
411 | (($ $kargs names syms body) | |
412 | (term-folder body seed)) | |
413 | ||
414 | (($ $kentry self tail clauses) | |
415 | (fold cont-folder (cont-folder tail seed) clauses)) | |
416 | ||
417 | (($ $kclause arity body) | |
418 | (cont-folder body seed)) | |
419 | ||
420 | (_ seed)))))) | |
421 | ||
422 | (define (fun-folder fun seed) | |
423 | (match fun | |
6e422a35 | 424 | (($ $fun src meta free body) |
80b01fd0 AW |
425 | (cont-folder body seed)))) |
426 | ||
427 | (define (term-folder term seed) | |
428 | (match term | |
429 | (($ $letk conts body) | |
430 | (fold cont-folder (term-folder body seed) conts)) | |
431 | ||
6e422a35 | 432 | (($ $continue k src exp) |
80b01fd0 AW |
433 | (match exp |
434 | (($ $fun) (fun-folder exp seed)) | |
435 | (_ seed))) | |
436 | ||
437 | (($ $letrec names syms funs body) | |
438 | (fold fun-folder (term-folder body seed) funs)))) | |
439 | ||
440 | (fun-folder fun seed)) | |
441 | ||
442 | (define (fold-local-conts proc seed cont) | |
443 | (define (cont-folder cont seed) | |
444 | (match cont | |
6e422a35 AW |
445 | (($ $cont k cont) |
446 | (let ((seed (proc k cont seed))) | |
80b01fd0 AW |
447 | (match cont |
448 | (($ $kargs names syms body) | |
449 | (term-folder body seed)) | |
450 | ||
451 | (($ $kentry self tail clauses) | |
452 | (fold cont-folder (cont-folder tail seed) clauses)) | |
453 | ||
454 | (($ $kclause arity body) | |
455 | (cont-folder body seed)) | |
456 | ||
457 | (_ seed)))))) | |
458 | ||
459 | (define (term-folder term seed) | |
460 | (match term | |
461 | (($ $letk conts body) | |
462 | (fold cont-folder (term-folder body seed) conts)) | |
463 | ||
464 | (($ $continue) seed) | |
465 | ||
466 | (($ $letrec names syms funs body) (term-folder body seed)))) | |
467 | ||
468 | (cont-folder cont seed)) |