1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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
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.
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))
35 (define (module-box src module name public? bound? val-proc)
36 (let-gensyms (module-sym name-sym public?-sym bound?-sym kbox box)
38 ($letconst (('module module-sym module)
40 ('public? public?-sym public?)
41 ('bound? bound?-sym bound?))
42 ($letk ((kbox src ($kargs ('box) (box) ,(val-proc box))))
44 ($primcall 'cached-module-box
45 (module-sym name-sym public?-sym bound?-sym))))))))
47 (define (primitive-ref name k)
48 (module-box #f '(guile) name #f #t
51 ($continue k ($primcall 'box-ref (box)))))))
53 (define (builtin-ref idx k)
54 (let-gensyms (idx-sym)
56 ($letconst (('idx idx-sym idx))
58 ($primcall 'builtin-ref (idx-sym)))))))
60 (define (reify-clause ktail)
61 (let-gensyms (kclause kbody wna false str eol kthrow throw)
63 (kclause #f ($kclause ('() '() #f '() #f)
67 ($letconst (('wna wna 'wrong-number-of-args)
69 ('str str "Wrong number of arguments")
73 ($kargs ('throw) (throw)
76 (wna false str eol false))))))
77 ,(primitive-ref 'throw kthrow))))))))))
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)
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))))
99 (define (visit-term term)
100 (rewrite-cps-term term
101 (($ $letk conts body)
102 ($letk ,(map visit-cont conts) ,(visit-term body)))
106 (match (lookup-cont k conts)
109 ((builtin-name->index name)
111 (builtin-ref idx k)))
112 (else (primitive-ref name k))))
113 (_ (build-cps-term ($continue k ($void))))))
115 (build-cps-term ($continue k ,(visit-fun exp))))
116 (($ $primcall 'call-thunk/no-inline (proc))
118 ($continue k ($call proc ()))))
119 (($ $primcall name args)
121 ((or (prim-rtl-instruction name) (branching-primitive? name))
122 ;; Assume arities are correct.
127 ($letk ((k* #f ($kargs (v) (v)
128 ($continue k ($call v args)))))
130 ((builtin-name->index name)
132 (builtin-ref idx k*)))
133 (else (primitive-ref name k*)))))))))