1 ;;; Tree-il canonicalizer
3 ;; Copyright (C) 2011, 2012 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 (define-module (language tree-il canonicalize)
22 #:use-module (language tree-il)
23 #:use-module (ice-9 match)
24 #:use-module (srfi srfi-1)
25 #:export (canonicalize!))
27 (define (tree-il-any proc exp)
28 (tree-il-fold (lambda (exp res)
32 (lambda (exp res) res)
35 (define (canonicalize! x)
39 (($ <let> src () () () body)
41 (($ <letrec> src _ () () () body)
43 (($ <fix> src () () () body)
45 (($ <dynlet> src () () body)
47 (($ <prompt> src tag body handler)
48 (define (escape-only? handler)
50 (($ <lambda-case> _ (_ . _) _ _ _ _ (cont . _) body #f)
51 (not (tree-il-any (lambda (x)
53 (eq? (lexical-ref-gensym x) cont)))
56 (define (thunk-application? x)
59 ($ <lambda> _ _ ($ <lambda-case> _ () #f #f #f))
62 (define (make-thunk-application body)
65 (make-lambda-case #f '() #f #f #f '() '() body #f)))
66 (make-call #f thunk '()))
68 ;; This code has a nasty job to do: to ensure that either the
69 ;; handler is escape-only, or the body is the application of a
70 ;; thunk. Sad but true.
71 (if (or (escape-only? handler)
72 (thunk-application? body))
74 (make-prompt src tag (make-thunk-application body) handler)))