1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013, 2014 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 ;;; Constructor inlining turns "list" primcalls into a series of conses,
22 ;;; and does similar transformations for "vector".
26 (define-module (language cps constructors)
27 #:use-module (ice-9 match)
28 #:use-module (srfi srfi-26)
29 #:use-module (language cps)
30 #:export (inline-constructors))
32 (define (inline-constructors* fun)
33 (define (visit-cont cont)
34 (rewrite-cps-cont cont
35 (($ $cont sym ($ $kargs names syms body))
36 (sym ($kargs names syms ,(visit-term body))))
37 (($ $cont sym ($ $kfun src meta self tail clause))
38 (sym ($kfun src meta self ,tail ,(and clause (visit-cont clause)))))
39 (($ $cont sym ($ $kclause arity body alternate))
40 (sym ($kclause ,arity ,(visit-cont body)
41 ,(and alternate (visit-cont alternate)))))
44 (define (visit-term term)
45 (rewrite-cps-term term
47 ($letk ,(map visit-cont conts)
49 (($ $letrec names syms funs body)
50 ($letrec names syms (map visit-fun funs)
52 (($ $continue k src ($ $primcall 'list args))
53 ,(let-fresh (kvalues) (val)
55 ($letk ((kvalues ($kargs ('val) (val)
57 ($primcall 'values (val))))))
58 ,(let lp ((args args) (k kvalues))
62 ($continue k src ($const '()))))
64 (let-fresh (ktail) (tail)
66 ($letk ((ktail ($kargs ('tail) (tail)
68 ($primcall 'cons (arg tail))))))
69 ,(lp args ktail)))))))))))
70 (($ $continue k src ($ $primcall 'vector args))
71 ,(let-fresh (kalloc) (vec len init)
72 (define (initialize args n)
76 ($continue k src ($primcall 'values (vec)))))
78 (let-fresh (knext) (idx)
80 ($letk ((knext ($kargs () ()
81 ,(initialize args (1+ n)))))
82 ($letconst (('idx idx n))
84 ($primcall 'vector-set! (vec idx arg))))))))))
86 ($letk ((kalloc ($kargs ('vec) (vec)
87 ,(initialize args 0))))
88 ($letconst (('len len (length args))
91 ($primcall 'make-vector (len init))))))))
92 (($ $continue k src (and fun ($ $fun)))
93 ($continue k src ,(visit-fun fun)))
96 (define (visit-fun fun)
99 ($fun free ,(inline-constructors* body)))))
103 (define (inline-constructors fun)
104 (with-fresh-name-state fun
105 (inline-constructors* fun)))