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