Remove $void CPS expression type
[bpt/guile.git] / module / language / cps / arities.scm
CommitLineData
026b5611
AW
1;;; Continuation-passing style (CPS) intermediate language (IL)
2
a9ec16f9 3;; Copyright (C) 2013, 2014, 2015 Free Software Foundation, Inc.
026b5611
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;;; A pass to adapt expressions to the arities of their continuations,
22;;; and to rewrite some tail expressions as primcalls to "return".
23;;;
24;;; Code:
25
26(define-module (language cps arities)
27 #:use-module (ice-9 match)
28 #:use-module ((srfi srfi-1) #:select (fold))
29 #:use-module (srfi srfi-26)
30 #:use-module (language cps)
31 #:use-module (language cps dfg)
32 #:use-module (language cps primitives)
33 #:export (fix-arities))
34
a0329d01 35(define (fix-arities* clause dfg)
a6f823bd 36 (let ((ktail (match clause
24b611e8 37 (($ $cont _
8320f504 38 ($ $kfun src meta _ ($ $cont ktail) _)) ktail))))
026b5611
AW
39 (define (visit-term term)
40 (rewrite-cps-term term
41 (($ $letk conts body)
42 ($letk ,(map visit-cont conts) ,(visit-term body)))
43 (($ $letrec names syms funs body)
a0329d01
AW
44 ($letrec names syms (map (lambda (fun)
45 (rewrite-cps-exp fun
46 (($ $fun free body)
47 ($fun free ,(fix-arities* body dfg)))))
48 funs)
49 ,(visit-term body)))
6e422a35
AW
50 (($ $continue k src exp)
51 ,(visit-exp k src exp))))
026b5611 52
6e422a35 53 (define (adapt-exp nvals k src exp)
026b5611
AW
54 (match nvals
55 (0
fbdb69b2 56 (rewrite-cps-term (lookup-cont k dfg)
026b5611 57 (($ $ktail)
828ed944 58 ,(let-fresh (kvoid kunspec) (unspec)
026b5611 59 (build-cps-term
6e422a35
AW
60 ($letk* ((kunspec ($kargs (unspec) (unspec)
61 ($continue k src
62 ($primcall 'return (unspec)))))
63 (kvoid ($kargs () ()
a9ec16f9
AW
64 ($continue kunspec src
65 ($const *unspecified*)))))
6e422a35 66 ($continue kvoid src ,exp)))))
36527695 67 (($ $kreceive arity kargs)
7bbfc029
AW
68 ,(match arity
69 (($ $arity () () rest () #f)
70 (if rest
828ed944 71 (let-fresh (knil) ()
7bbfc029
AW
72 (build-cps-term
73 ($letk ((knil ($kargs () ()
74 ($continue kargs src ($const '())))))
75 ($continue knil src ,exp))))
76 (build-cps-term
77 ($continue kargs src ,exp))))
4fc6b4d2 78 (_
828ed944 79 (let-fresh (kvoid kvalues) (void)
7bbfc029
AW
80 (build-cps-term
81 ($letk* ((kvalues ($kargs ('void) (void)
82 ($continue k src
83 ($primcall 'values (void)))))
84 (kvoid ($kargs () ()
85 ($continue kvalues src
a9ec16f9 86 ($const *unspecified*)))))
7bbfc029 87 ($continue kvoid src ,exp)))))))
026b5611 88 (($ $kargs () () _)
6e422a35 89 ($continue k src ,exp))
026b5611 90 (_
828ed944 91 ,(let-fresh (k*) ()
026b5611 92 (build-cps-term
a9ec16f9
AW
93 ($letk ((k* ($kargs () () ($continue k src
94 ($const *unspecified*)))))
6e422a35 95 ($continue k* src ,exp)))))))
026b5611 96 (1
fbdb69b2 97 (rewrite-cps-term (lookup-cont k dfg)
4fc6b4d2
AW
98 (($ $ktail)
99 ,(rewrite-cps-term exp
30411abf 100 (($ $values (sym))
6e422a35 101 ($continue ktail src ($primcall 'return (sym))))
4fc6b4d2 102 (_
828ed944 103 ,(let-fresh (k*) (v)
4fc6b4d2 104 (build-cps-term
6e422a35
AW
105 ($letk ((k* ($kargs (v) (v)
106 ($continue k src
107 ($primcall 'return (v))))))
108 ($continue k* src ,exp)))))))
36527695 109 (($ $kreceive arity kargs)
7bbfc029
AW
110 ,(match arity
111 (($ $arity (_) () rest () #f)
112 (if rest
828ed944 113 (let-fresh (kval) (val nil)
7bbfc029
AW
114 (build-cps-term
115 ($letk ((kval ($kargs ('val) (val)
116 ($letconst (('nil nil '()))
117 ($continue kargs src
118 ($values (val nil)))))))
119 ($continue kval src ,exp))))
120 (build-cps-term ($continue kargs src ,exp))))
4fc6b4d2 121 (_
828ed944 122 (let-fresh (kvalues) (value)
7bbfc029
AW
123 (build-cps-term
124 ($letk ((kvalues ($kargs ('value) (value)
125 ($continue k src
126 ($primcall 'values (value))))))
127 ($continue kvalues src ,exp)))))))
4fc6b4d2 128 (($ $kargs () () _)
828ed944 129 ,(let-fresh (k*) (drop)
4fc6b4d2 130 (build-cps-term
6e422a35
AW
131 ($letk ((k* ($kargs ('drop) (drop)
132 ($continue k src ($values ())))))
133 ($continue k* src ,exp)))))
4fc6b4d2 134 (_
6e422a35 135 ($continue k src ,exp))))))
026b5611 136
6e422a35 137 (define (visit-exp k src exp)
026b5611 138 (rewrite-cps-term exp
a9ec16f9 139 ((or ($ $const)
026b5611 140 ($ $prim)
13085a82 141 ($ $values (_)))
6e422a35 142 ,(adapt-exp 1 k src exp))
a0329d01
AW
143 (($ $fun free body)
144 ,(adapt-exp 1 k src (build-cps-exp
145 ($fun free ,(fix-arities* body dfg)))))
b3ae2b50 146 ((or ($ $call) ($ $callk))
026b5611 147 ;; In general, calls have unknown return arity. For that
b3ae2b50
AW
148 ;; reason every non-tail call has a $kreceive continuation to
149 ;; adapt the return to the target continuation, and we don't
150 ;; need to do any adapting here.
6e422a35 151 ($continue k src ,exp))
92805e21
AW
152 (($ $branch)
153 ;; Assume branching primcalls have the correct arity.
154 ($continue k src ,exp))
026b5611
AW
155 (($ $primcall 'return (arg))
156 ;; Primcalls to return are in tail position.
6e422a35 157 ($continue ktail src ,exp))
026b5611 158 (($ $primcall (? (lambda (name)
691697de 159 (and (not (prim-instruction name))
026b5611 160 (not (branching-primitive? name))))))
6e422a35 161 ($continue k src ,exp))
026b5611
AW
162 (($ $primcall name args)
163 ,(match (prim-arity name)
164 ((out . in)
165 (if (= in (length args))
6e422a35 166 (adapt-exp out k src
691697de 167 (let ((inst (prim-instruction name)))
6165d812
AW
168 (if (and inst (not (eq? inst name)))
169 (build-cps-exp ($primcall inst args))
170 exp)))
828ed944 171 (let-fresh (k*) (p*)
026b5611 172 (build-cps-term
6e422a35
AW
173 ($letk ((k* ($kargs ('prim) (p*)
174 ($continue k src ($call p* args)))))
175 ($continue k* src ($prim name)))))))))
026b5611 176 (($ $values)
13085a82
AW
177 ;; Non-unary values nodes are inserted by CPS optimization
178 ;; passes, so we assume they are correct.
6e422a35 179 ($continue k src ,exp))
026b5611 180 (($ $prompt)
6e422a35 181 ($continue k src ,exp))))
026b5611
AW
182
183 (define (visit-cont cont)
184 (rewrite-cps-cont cont
6e422a35
AW
185 (($ $cont sym ($ $kargs names syms body))
186 (sym ($kargs names syms ,(visit-term body))))
90dce16d
AW
187 (($ $cont sym ($ $kclause arity body alternate))
188 (sym ($kclause ,arity ,(visit-cont body)
189 ,(and alternate (visit-cont alternate)))))
026b5611
AW
190 (($ $cont)
191 ,cont)))
192
193 (rewrite-cps-cont clause
8320f504
AW
194 (($ $cont sym ($ $kfun src meta self tail clause))
195 (sym ($kfun src meta self ,tail ,(and clause (visit-cont clause))))))))
026b5611 196
828ed944 197(define (fix-arities fun)
a0329d01 198 (let ((dfg (compute-dfg fun)))
3e1b97c1
AW
199 (with-fresh-name-state-from-dfg dfg
200 (fix-arities* fun dfg))))