Fix source-line-for-user for unknown line
[bpt/guile.git] / module / language / cps / arities.scm
CommitLineData
026b5611
AW
1;;; Continuation-passing style (CPS) intermediate language (IL)
2
36527695 3;; Copyright (C) 2013, 2014 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 () ()
64 ($continue kunspec src ($void)))))
65 ($continue kvoid src ,exp)))))
36527695 66 (($ $kreceive arity kargs)
7bbfc029
AW
67 ,(match arity
68 (($ $arity () () rest () #f)
69 (if rest
828ed944 70 (let-fresh (knil) ()
7bbfc029
AW
71 (build-cps-term
72 ($letk ((knil ($kargs () ()
73 ($continue kargs src ($const '())))))
74 ($continue knil src ,exp))))
75 (build-cps-term
76 ($continue kargs src ,exp))))
4fc6b4d2 77 (_
828ed944 78 (let-fresh (kvoid kvalues) (void)
7bbfc029
AW
79 (build-cps-term
80 ($letk* ((kvalues ($kargs ('void) (void)
81 ($continue k src
82 ($primcall 'values (void)))))
83 (kvoid ($kargs () ()
84 ($continue kvalues src
85 ($void)))))
86 ($continue kvoid src ,exp)))))))
026b5611 87 (($ $kargs () () _)
6e422a35 88 ($continue k src ,exp))
026b5611 89 (_
828ed944 90 ,(let-fresh (k*) ()
026b5611 91 (build-cps-term
6e422a35
AW
92 ($letk ((k* ($kargs () () ($continue k src ($void)))))
93 ($continue k* src ,exp)))))))
026b5611 94 (1
fbdb69b2 95 (rewrite-cps-term (lookup-cont k dfg)
4fc6b4d2
AW
96 (($ $ktail)
97 ,(rewrite-cps-term exp
13085a82 98 (($values (sym))
6e422a35 99 ($continue ktail src ($primcall 'return (sym))))
4fc6b4d2 100 (_
828ed944 101 ,(let-fresh (k*) (v)
4fc6b4d2 102 (build-cps-term
6e422a35
AW
103 ($letk ((k* ($kargs (v) (v)
104 ($continue k src
105 ($primcall 'return (v))))))
106 ($continue k* src ,exp)))))))
36527695 107 (($ $kreceive arity kargs)
7bbfc029
AW
108 ,(match arity
109 (($ $arity (_) () rest () #f)
110 (if rest
828ed944 111 (let-fresh (kval) (val nil)
7bbfc029
AW
112 (build-cps-term
113 ($letk ((kval ($kargs ('val) (val)
114 ($letconst (('nil nil '()))
115 ($continue kargs src
116 ($values (val nil)))))))
117 ($continue kval src ,exp))))
118 (build-cps-term ($continue kargs src ,exp))))
4fc6b4d2 119 (_
828ed944 120 (let-fresh (kvalues) (value)
7bbfc029
AW
121 (build-cps-term
122 ($letk ((kvalues ($kargs ('value) (value)
123 ($continue k src
124 ($primcall 'values (value))))))
125 ($continue kvalues src ,exp)))))))
4fc6b4d2 126 (($ $kargs () () _)
828ed944 127 ,(let-fresh (k*) (drop)
4fc6b4d2 128 (build-cps-term
6e422a35
AW
129 ($letk ((k* ($kargs ('drop) (drop)
130 ($continue k src ($values ())))))
131 ($continue k* src ,exp)))))
4fc6b4d2 132 (_
6e422a35 133 ($continue k src ,exp))))))
026b5611 134
6e422a35 135 (define (visit-exp k src exp)
026b5611
AW
136 (rewrite-cps-term exp
137 ((or ($ $void)
138 ($ $const)
139 ($ $prim)
13085a82 140 ($ $values (_)))
6e422a35 141 ,(adapt-exp 1 k src exp))
a0329d01
AW
142 (($ $fun free body)
143 ,(adapt-exp 1 k src (build-cps-exp
144 ($fun free ,(fix-arities* body dfg)))))
b3ae2b50 145 ((or ($ $call) ($ $callk))
026b5611 146 ;; In general, calls have unknown return arity. For that
b3ae2b50
AW
147 ;; reason every non-tail call has a $kreceive continuation to
148 ;; adapt the return to the target continuation, and we don't
149 ;; need to do any adapting here.
6e422a35 150 ($continue k src ,exp))
026b5611
AW
151 (($ $primcall 'return (arg))
152 ;; Primcalls to return are in tail position.
6e422a35 153 ($continue ktail src ,exp))
026b5611 154 (($ $primcall (? (lambda (name)
691697de 155 (and (not (prim-instruction name))
026b5611 156 (not (branching-primitive? name))))))
6e422a35 157 ($continue k src ,exp))
026b5611
AW
158 (($ $primcall name args)
159 ,(match (prim-arity name)
160 ((out . in)
161 (if (= in (length args))
6e422a35 162 (adapt-exp out k src
691697de 163 (let ((inst (prim-instruction name)))
6165d812
AW
164 (if (and inst (not (eq? inst name)))
165 (build-cps-exp ($primcall inst args))
166 exp)))
828ed944 167 (let-fresh (k*) (p*)
026b5611 168 (build-cps-term
6e422a35
AW
169 ($letk ((k* ($kargs ('prim) (p*)
170 ($continue k src ($call p* args)))))
171 ($continue k* src ($prim name)))))))))
026b5611 172 (($ $values)
13085a82
AW
173 ;; Non-unary values nodes are inserted by CPS optimization
174 ;; passes, so we assume they are correct.
6e422a35 175 ($continue k src ,exp))
026b5611 176 (($ $prompt)
6e422a35 177 ($continue k src ,exp))))
026b5611
AW
178
179 (define (visit-cont cont)
180 (rewrite-cps-cont cont
6e422a35
AW
181 (($ $cont sym ($ $kargs names syms body))
182 (sym ($kargs names syms ,(visit-term body))))
90dce16d
AW
183 (($ $cont sym ($ $kclause arity body alternate))
184 (sym ($kclause ,arity ,(visit-cont body)
185 ,(and alternate (visit-cont alternate)))))
026b5611
AW
186 (($ $cont)
187 ,cont)))
188
189 (rewrite-cps-cont clause
8320f504
AW
190 (($ $cont sym ($ $kfun src meta self tail clause))
191 (sym ($kfun src meta self ,tail ,(and clause (visit-cont clause))))))))
026b5611 192
828ed944 193(define (fix-arities fun)
a0329d01 194 (let ((dfg (compute-dfg fun)))
3e1b97c1
AW
195 (with-fresh-name-state-from-dfg dfg
196 (fix-arities* fun dfg))))