1 ;;; Tree-il canonicalizer
3 ;; Copyright (C) 2011, 2012, 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 (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 (($ <sequence> src (tail))
41 (($ <sequence> src exps)
42 (and (any sequence? exps)
44 (append-map (lambda (x)
49 (($ <let> src () () () body)
51 (($ <letrec> src _ () () () body)
53 (($ <fix> src () () () body)
55 (($ <dynlet> src () () body)
57 (($ <lambda> src meta #f)
58 ;; Give a body to case-lambda with no clauses.
62 #f '() #f #f #f '() '()
65 (make-primitive-ref #f 'throw)
66 (list (make-const #f 'wrong-number-of-args)
68 (make-const #f "Wrong number of arguments")
72 (($ <prompt> src tag body handler)
73 (define (escape-only? handler)
75 (($ <lambda-case> _ (_ . _) _ _ _ _ (cont . _) body #f)
76 (not (tree-il-any (lambda (x)
78 (eq? (lexical-ref-gensym x) cont)))
81 (define (thunk-application? x)
84 ($ <lambda> _ _ ($ <lambda-case> _ () #f #f #f))
87 (define (make-thunk-application body)
90 (make-lambda-case #f '() #f #f #f '() '() body #f)))
91 (make-application #f thunk '()))
93 ;; This code has a nasty job to do: to ensure that either the
94 ;; handler is escape-only, or the body is the application of a
95 ;; thunk. Sad but true.
96 (if (or (escape-only? handler)
97 (thunk-application? body))
99 (make-prompt src tag (make-thunk-application body) handler)))