VM has "builtins": primitives addressable by emitted RTL code
[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-ref name k)
48 (module-box #f '(guile) name #f #t
49 (lambda (box)
50 (build-cps-term
51 ($continue k ($primcall 'box-ref (box)))))))
52
53 (define (builtin-ref idx k)
54 (let-gensyms (idx-sym)
55 (build-cps-term
56 ($letconst (('idx idx-sym idx))
57 ($continue k
58 ($primcall 'builtin-ref (idx-sym)))))))
59
60 (define (reify-clause ktail)
61 (let-gensyms (kclause kbody wna false str eol kthrow throw)
62 (build-cps-cont
63 (kclause #f ($kclause ('() '() #f '() #f)
64 (kbody
65 #f
66 ($kargs () ()
67 ($letconst (('wna wna 'wrong-number-of-args)
68 ('false false #f)
69 ('str str "Wrong number of arguments")
70 ('eol eol '()))
71 ($letk ((kthrow
72 #f
73 ($kargs ('throw) (throw)
74 ($continue ktail
75 ($call throw
76 (wna false str eol false))))))
77 ,(primitive-ref 'throw kthrow))))))))))
78
79 ;; FIXME: Operate on one function at a time, for efficiency.
80 (define (reify-primitives fun)
81 (let ((conts (build-cont-table fun)))
82 (define (visit-fun term)
83 (rewrite-cps-exp term
84 (($ $fun meta free body)
85 ($fun meta free ,(visit-cont body)))))
86 (define (visit-cont cont)
87 (rewrite-cps-cont cont
88 (($ $cont sym src ($ $kargs names syms body))
89 (sym src ($kargs names syms ,(visit-term body))))
90 (($ $cont sym src ($ $kentry self (and tail ($ $cont ktail)) ()))
91 ;; A case-lambda with no clauses. Reify a clause.
92 (sym src ($kentry self ,tail (,(reify-clause ktail)))))
93 (($ $cont sym src ($ $kentry self tail clauses))
94 (sym src ($kentry self ,tail ,(map visit-cont clauses))))
95 (($ $cont sym src ($ $kclause arity body))
96 (sym src ($kclause ,arity ,(visit-cont body))))
97 (($ $cont)
98 ,cont)))
99 (define (visit-term term)
100 (rewrite-cps-term term
101 (($ $letk conts body)
102 ($letk ,(map visit-cont conts) ,(visit-term body)))
103 (($ $continue k exp)
104 ,(match exp
105 (($ $prim name)
106 (match (lookup-cont k conts)
107 (($ $kargs (_))
108 (cond
109 ((builtin-name->index name)
110 => (lambda (idx)
111 (builtin-ref idx k)))
112 (else (primitive-ref name k))))
113 (_ (build-cps-term ($continue k ($void))))))
114 (($ $fun)
115 (build-cps-term ($continue k ,(visit-fun exp))))
116 (($ $primcall 'call-thunk/no-inline (proc))
117 (build-cps-term
118 ($continue k ($call proc ()))))
119 (($ $primcall name args)
120 (cond
121 ((or (prim-rtl-instruction name) (branching-primitive? name))
122 ;; Assume arities are correct.
123 term)
124 (else
125 (let-gensyms (k* v)
126 (build-cps-term
127 ($letk ((k* #f ($kargs (v) (v)
128 ($continue k ($call v args)))))
129 ,(cond
130 ((builtin-name->index name)
131 => (lambda (idx)
132 (builtin-ref idx k*)))
133 (else (primitive-ref name k*)))))))))
134 (_ term)))))
135
136 (visit-fun fun)))