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 (reify-clause ktail)
54 (let-gensyms (kclause kbody wna false str eol kthrow throw)
56 (kclause #f ($kclause ('() '() #f '() #f)
60 ($letconst (('wna wna 'wrong-number-of-args)
62 ('str str "Wrong number of arguments")
66 ($kargs ('throw) (throw)
69 (wna false str eol false))))))
70 ,(primitive-ref 'throw kthrow))))))))))
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)
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))))
92 (define (visit-term term)
93 (rewrite-cps-term term
95 ($letk ,(map visit-cont conts) ,(visit-term body)))
99 (match (lookup-cont k conts)
100 (($ $kargs (_)) (primitive-ref name k))
101 (_ (build-cps-term ($continue k ($void))))))
103 (build-cps-term ($continue k ,(visit-fun exp))))
104 (($ $primcall 'call-thunk/no-inline (proc))
106 ($continue k ($call proc ()))))
107 (($ $primcall name args)
109 ((or (prim-rtl-instruction name) (branching-primitive? name))
110 ;; Assume arities are correct.
115 ($letk ((k* #f ($kargs (v) (v)
116 ($continue k ($call v args)))))
117 ,(primitive-ref name k*)))))))