Fix primitive reification for class-of, bytevector-u8-ref, etc
[bpt/guile.git] / module / language / cps / reify-primitives.scm
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 ;;; 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 rtl)
33 #:export (reify-primitives))
34
35 (define (module-box src module name public? bound? val-proc)
36 (let-gensyms (module-sym name-sym public?-sym bound?-sym kbox box)
37 (build-cps-term
38 ($letconst (('module module-sym module)
39 ('name name-sym name)
40 ('public? public?-sym public?)
41 ('bound? bound?-sym bound?))
42 ($letk ((kbox src ($kargs ('box) (box) ,(val-proc box))))
43 ($continue kbox
44 ($primcall 'cached-module-box
45 (module-sym name-sym public?-sym bound?-sym))))))))
46
47 (define (primitive-module name)
48 (case name
49 ((bytevector-u8-ref bytevector-u8-set!
50 bytevector-s8-ref bytevector-s8-set!
51
52 bytevector-u16-ref bytevector-u16-set!
53 bytevector-u16-native-ref bytevector-u16-native-set!
54 bytevector-s16-ref bytevector-s16-set!
55 bytevector-s16-native-ref bytevector-s16-native-set!
56
57 bytevector-u32-ref bytevector-u32-set!
58 bytevector-u32-native-ref bytevector-u32-native-set!
59 bytevector-s32-ref bytevector-s32-set!
60 bytevector-s32-native-ref bytevector-s32-native-set!
61
62 bytevector-u64-ref bytevector-u64-set!
63 bytevector-u64-native-ref bytevector-u64-native-set!
64 bytevector-s64-ref bytevector-s64-set!
65 bytevector-s64-native-ref bytevector-s64-native-set!
66
67 bytevector-ieee-single-ref bytevector-ieee-single-set!
68 bytevector-ieee-single-native-ref bytevector-ieee-single-native-set!
69 bytevector-ieee-double-ref bytevector-ieee-double-set!
70 bytevector-ieee-double-native-ref bytevector-ieee-double-native-set!)
71 '(rnrs bytevectors))
72 ((class-of @slot-ref @slot-set!) '(oop goops))
73 (else '(guile))))
74
75 (define (primitive-ref name k)
76 (module-box #f (primitive-module name) name #f #t
77 (lambda (box)
78 (build-cps-term
79 ($continue k ($primcall 'box-ref (box)))))))
80
81 (define (builtin-ref idx k)
82 (let-gensyms (idx-sym)
83 (build-cps-term
84 ($letconst (('idx idx-sym idx))
85 ($continue k
86 ($primcall 'builtin-ref (idx-sym)))))))
87
88 (define (reify-clause ktail)
89 (let-gensyms (kclause kbody wna false str eol kthrow throw)
90 (build-cps-cont
91 (kclause #f ($kclause ('() '() #f '() #f)
92 (kbody
93 #f
94 ($kargs () ()
95 ($letconst (('wna wna 'wrong-number-of-args)
96 ('false false #f)
97 ('str str "Wrong number of arguments")
98 ('eol eol '()))
99 ($letk ((kthrow
100 #f
101 ($kargs ('throw) (throw)
102 ($continue ktail
103 ($call throw
104 (wna false str eol false))))))
105 ,(primitive-ref 'throw kthrow))))))))))
106
107 ;; FIXME: Operate on one function at a time, for efficiency.
108 (define (reify-primitives fun)
109 (let ((conts (build-cont-table fun)))
110 (define (visit-fun term)
111 (rewrite-cps-exp term
112 (($ $fun meta free body)
113 ($fun meta free ,(visit-cont body)))))
114 (define (visit-cont cont)
115 (rewrite-cps-cont cont
116 (($ $cont sym src ($ $kargs names syms body))
117 (sym src ($kargs names syms ,(visit-term body))))
118 (($ $cont sym src ($ $kentry self (and tail ($ $cont ktail)) ()))
119 ;; A case-lambda with no clauses. Reify a clause.
120 (sym src ($kentry self ,tail (,(reify-clause ktail)))))
121 (($ $cont sym src ($ $kentry self tail clauses))
122 (sym src ($kentry self ,tail ,(map visit-cont clauses))))
123 (($ $cont sym src ($ $kclause arity body))
124 (sym src ($kclause ,arity ,(visit-cont body))))
125 (($ $cont)
126 ,cont)))
127 (define (visit-term term)
128 (rewrite-cps-term term
129 (($ $letk conts body)
130 ($letk ,(map visit-cont conts) ,(visit-term body)))
131 (($ $continue k exp)
132 ,(match exp
133 (($ $prim name)
134 (match (lookup-cont k conts)
135 (($ $kargs (_))
136 (cond
137 ((builtin-name->index name)
138 => (lambda (idx)
139 (builtin-ref idx k)))
140 (else (primitive-ref name k))))
141 (_ (build-cps-term ($continue k ($void))))))
142 (($ $fun)
143 (build-cps-term ($continue k ,(visit-fun exp))))
144 (($ $primcall 'call-thunk/no-inline (proc))
145 (build-cps-term
146 ($continue k ($call proc ()))))
147 (($ $primcall name args)
148 (cond
149 ((or (prim-rtl-instruction name) (branching-primitive? name))
150 ;; Assume arities are correct.
151 term)
152 (else
153 (let-gensyms (k* v)
154 (build-cps-term
155 ($letk ((k* #f ($kargs (v) (v)
156 ($continue k ($call v args)))))
157 ,(cond
158 ((builtin-name->index name)
159 => (lambda (idx)
160 (builtin-ref idx k*)))
161 (else (primitive-ref name k*)))))))))
162 (_ term)))))
163
164 (visit-fun fun)))