1 ;;;; tree-il.test --- test suite for compiling tree-il -*- scheme -*-
2 ;;;; Andy Wingo <wingo@pobox.com> --- May 2009
4 ;;;; Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (define-module (test-suite tree-il)
21 #:use-module (test-suite lib)
22 #:use-module (system base compile)
23 #:use-module (system base pmatch)
24 #:use-module (system base message)
25 #:use-module (language tree-il)
26 #:use-module (language tree-il primitives)
27 #:use-module (language tree-il cse)
28 #:use-module (language tree-il peval)
29 #:use-module (language glil)
30 #:use-module (srfi srfi-13))
32 (define-syntax pass-if-cse
36 (let ((evaled (unparse-tree-il
41 (compile 'in #:from 'scheme #:to 'tree-il)
42 (current-module))))))))
46 ((@ (ice-9 pretty-print) pretty-print)
49 ((@ (ice-9 pretty-print) pretty-print)
52 ((@ (ice-9 pretty-print) pretty-print)
58 (with-test-prefix "cse"
60 ;; The eq? propagates, and (if TEST #t #f) folds to TEST if TEST is
68 (((x y) #f #f #f () (_ _))
69 (apply (primitive eq?) (lexical x _) (lexical y _))))))
71 ;; The eq? propagates, and (if TEST #f #t) folds to (not TEST).
77 (((x y) #f #f #f () (_ _))
78 (apply (primitive not)
79 (apply (primitive eq?) (lexical x _) (lexical y _)))))))
81 ;; (if TEST (not TEST) #f)
87 (and (eq? x y) (not (eq? x y))))
90 (((x y) #f #f #f () (_ _))
93 ;; (if TEST #f TEST) => (if TEST #f #f) => ...
96 (if (eq? x y) #f (eq? x y)))
99 (((x y) #f #f #f () (_ _))
102 ;; The same, but side-effecting primitives do not propagate.
105 (and (set-car! x y) (not (set-car! x y))))
108 (((x y) #f #f #f () (_ _))
109 (if (apply (primitive set-car!)
112 (apply (primitive not)
113 (apply (primitive set-car!)
118 ;; Primitives that access mutable memory can propagate, as long as
119 ;; there is no intervening mutation.
122 (and (string-ref x y)
125 (not (string-ref x y)))))
128 (((x y) #f #f #f () (_ _))
130 (apply (primitive string-ref)
135 ;; However, expressions with dependencies on effects do not propagate
139 (and (string-ref x y)
141 (and (string-ref x y) #t))))
144 (((x y) #f #f #f () (_ _))
145 (if (apply (primitive string-ref)
151 (if (apply (primitive string-ref)
158 ;; A mutation stops the propagation.
161 (and (string-ref x y)
164 (not (string-ref x y)))))
167 (((x y) #f #f #f () (_ _))
168 (if (apply (primitive string-ref)
172 (apply (primitive string-set!)
175 (apply (primitive not)
176 (apply (primitive string-ref)
181 ;; Predicates are only added to the database if they are in a
182 ;; predicate context.
185 (begin (eq? x y) (eq? x y)))
188 (((x y) #f #f #f () (_ _))
189 (apply (primitive eq?) (lexical x _) (lexical y _))))))
191 ;; Conditional bailouts do cause primitives to be added to the DB.
194 (begin (unless (eq? x y) (throw 'foo)) (eq? x y)))
197 (((x y) #f #f #f () (_ _))
199 (if (apply (primitive eq?)
200 (lexical x _) (lexical y _))
202 (apply (primitive 'throw) (const 'foo)))
205 ;; A chain of tests in a conditional bailout add data to the DB
210 (unless (and (struct? x) (eq? (struct-vtable x) x-vtable))
212 (if (and (struct? x) (eq? (struct-vtable x) x-vtable))
217 (((x y) #f #f #f () (_ _))
219 (if (if (apply (primitive struct?) (lexical x _))
220 (apply (primitive eq?)
221 (apply (primitive struct-vtable)
226 (apply (primitive 'throw) (const 'foo)))
227 (apply (primitive struct-ref) (lexical x _) (lexical y _)))))))
229 ;; Strict argument evaluation also adds info to the DB.
233 (+ z (if (and (struct? x) (eq? (struct-vtable x) x-vtable))
236 (if (and (struct? x) (eq? (struct-vtable x) x-vtable))
242 (((x) #f #f #f () (_))
243 (let (z) (_) ((if (if (apply (primitive struct?) (lexical x _))
244 (apply (primitive eq?)
245 (apply (primitive struct-vtable)
249 (apply (primitive struct-ref) (lexical x _) (const 1))
250 (apply (primitive 'throw) (const 'foo))))
251 (apply (primitive +) (lexical z _)
252 (apply (primitive struct-ref) (lexical x _) (const 2)))))))))