First-order CPS has $program and $closure forms
[bpt/guile.git] / module / language / cps / reify-primitives.scm
1 ;;; Continuation-passing style (CPS) intermediate language (IL)
2
3 ;; Copyright (C) 2013, 2014 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 ;;; A pass to reify lone $prim's that were never folded into a
22 ;;; $primcall, and $primcall's to primitives that don't have a
23 ;;; corresponding VM op.
24 ;;;
25 ;;; Code:
26
27 (define-module (language cps reify-primitives)
28 #:use-module (ice-9 match)
29 #:use-module (language cps)
30 #:use-module (language cps dfg)
31 #:use-module (language cps primitives)
32 #:use-module (language bytecode)
33 #:export (reify-primitives))
34
35 (define (module-box src module name public? bound? val-proc)
36 (let-fresh (kbox) (module-var name-var public?-var bound?-var box)
37 (build-cps-term
38 ($letconst (('module module-var module)
39 ('name name-var name)
40 ('public? public?-var public?)
41 ('bound? bound?-var bound?))
42 ($letk ((kbox ($kargs ('box) (box) ,(val-proc box))))
43 ($continue kbox src
44 ($primcall 'cached-module-box
45 (module-var name-var public?-var bound?-var))))))))
46
47 (define (primitive-module name)
48 (case name
49 ((bytevector-length
50
51 bytevector-u8-ref bytevector-u8-set!
52 bytevector-s8-ref bytevector-s8-set!
53
54 bytevector-u16-ref bytevector-u16-set!
55 bytevector-u16-native-ref bytevector-u16-native-set!
56 bytevector-s16-ref bytevector-s16-set!
57 bytevector-s16-native-ref bytevector-s16-native-set!
58
59 bytevector-u32-ref bytevector-u32-set!
60 bytevector-u32-native-ref bytevector-u32-native-set!
61 bytevector-s32-ref bytevector-s32-set!
62 bytevector-s32-native-ref bytevector-s32-native-set!
63
64 bytevector-u64-ref bytevector-u64-set!
65 bytevector-u64-native-ref bytevector-u64-native-set!
66 bytevector-s64-ref bytevector-s64-set!
67 bytevector-s64-native-ref bytevector-s64-native-set!
68
69 bytevector-ieee-single-ref bytevector-ieee-single-set!
70 bytevector-ieee-single-native-ref bytevector-ieee-single-native-set!
71 bytevector-ieee-double-ref bytevector-ieee-double-set!
72 bytevector-ieee-double-native-ref bytevector-ieee-double-native-set!)
73 '(rnrs bytevectors))
74 ((class-of) '(oop goops))
75 (else '(guile))))
76
77 (define (primitive-ref name k src)
78 (module-box #f (primitive-module name) name #f #t
79 (lambda (box)
80 (build-cps-term
81 ($continue k src ($primcall 'box-ref (box)))))))
82
83 (define (builtin-ref idx k src)
84 (let-fresh () (idx-var)
85 (build-cps-term
86 ($letconst (('idx idx-var idx))
87 ($continue k src
88 ($primcall 'builtin-ref (idx-var)))))))
89
90 (define (reify-clause ktail)
91 (let-fresh (kclause kbody kthrow) (wna false str eol throw)
92 (build-cps-cont
93 (kclause ($kclause ('() '() #f '() #f)
94 (kbody
95 ($kargs () ()
96 ($letconst (('wna wna 'wrong-number-of-args)
97 ('false false #f)
98 ('str str "Wrong number of arguments")
99 ('eol eol '()))
100 ($letk ((kthrow
101 ($kargs ('throw) (throw)
102 ($continue ktail #f
103 ($call throw
104 (wna false str eol false))))))
105 ,(primitive-ref 'throw kthrow #f)))))
106 ,#f)))))
107
108 (define (reify-primitives/1 fun single-value-conts)
109 (define (visit-clause cont)
110 (rewrite-cps-cont cont
111 (($ $cont label ($ $kclause arity body alternate))
112 (label ($kclause ,arity ,(visit-cont body)
113 ,(and alternate (visit-clause alternate)))))))
114 (define (visit-cont cont)
115 (rewrite-cps-cont cont
116 (($ $cont label ($ $kargs (name) (var) body))
117 ,(begin
118 (bitvector-set! single-value-conts label #t)
119 (build-cps-cont
120 (label ($kargs (name) (var) ,(visit-term body))))))
121 (($ $cont label ($ $kargs names vars body))
122 (label ($kargs names vars ,(visit-term body))))
123 (($ $cont)
124 ,cont)))
125 (define (visit-term term)
126 (match term
127 (($ $letk conts body)
128 ;; Visit continuations before their uses.
129 (let ((conts (map visit-cont conts)))
130 (build-cps-term
131 ($letk ,conts ,(visit-term body)))))
132 (($ $continue k src exp)
133 (match exp
134 (($ $prim name)
135 (if (bitvector-ref single-value-conts k)
136 (cond
137 ((builtin-name->index name)
138 => (lambda (idx)
139 (builtin-ref idx k src)))
140 (else (primitive-ref name k src)))
141 (build-cps-term ($continue k src ($void)))))
142 (($ $primcall 'call-thunk/no-inline (proc))
143 (build-cps-term
144 ($continue k src ($call proc ()))))
145 (($ $primcall name args)
146 (cond
147 ((or (prim-instruction name) (branching-primitive? name))
148 ;; Assume arities are correct.
149 term)
150 (else
151 (let-fresh (k*) (v)
152 (build-cps-term
153 ($letk ((k* ($kargs (v) (v)
154 ($continue k src ($call v args)))))
155 ,(cond
156 ((builtin-name->index name)
157 => (lambda (idx)
158 (builtin-ref idx k* src)))
159 (else (primitive-ref name k* src)))))))))
160 (_ term)))))
161
162 (rewrite-cps-cont fun
163 (($ $cont label ($ $kfun src meta self (and tail ($ $cont ktail)) #f))
164 ;; A case-lambda with no clauses. Reify a clause.
165 (label ($kfun src meta self ,tail ,(reify-clause ktail))))
166 (($ $cont label ($ $kfun src meta self tail clause))
167 (label ($kfun src meta self ,tail ,(visit-clause clause))))))
168
169 (define (reify-primitives term)
170 (with-fresh-name-state term
171 (let ((single-value-conts (make-bitvector (label-counter) #f)))
172 (rewrite-cps-term term
173 (($ $program procs)
174 ($program ,(map (lambda (cont)
175 (reify-primitives/1 cont single-value-conts))
176 procs)))))))