1 ;;;; rtl-compilation.test --- test suite for compiling via bytecode -*- scheme -*-
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
19 (define-module (test-suite bytecode-compilation)
20 #:use-module (test-suite lib)
21 #:use-module (system base compile)
22 #:use-module (system vm loader))
24 (define* (compile-via-bytecode exp #:key peval? cse? (env (make-fresh-user-module)))
25 (load-thunk-from-memory
26 (compile exp #:env env #:to 'bytecode
27 #:opts `(#:partial-eval? ,peval? #:cse? ,cse?))))
29 (define* (run-bytecode exp #:key (env (make-fresh-user-module)))
30 (let ((thunk (compile-via-bytecode exp #:env env)))
31 (save-module-excursion
33 (set-current-module env)
36 (with-test-prefix "tail context"
38 (run-bytecode '(let ((x 1)) x)))
43 (pass-if-equal (if #f #f)
44 (run-bytecode '(if #f #f)))
46 (pass-if-equal "top-level define"
48 (let ((mod (make-fresh-user-module)))
49 (let ((result (run-bytecode '(define v 1) #:env mod)))
50 (list result (module-ref mod 'v)))))
52 (pass-if-equal "top-level set!"
54 (let ((mod (make-fresh-user-module)))
55 (module-define! mod 'v #f)
56 (let ((result (run-bytecode '(set! v 1) #:env mod)))
57 (list result (module-ref mod 'v)))))
59 (pass-if-equal "top-level apply [single value]"
61 (let ((mod (make-fresh-user-module)))
62 (module-define! mod 'args '(2 3))
63 (run-bytecode '(apply expt args) #:env mod)))
65 (pass-if-equal "top-level apply [zero values]"
67 (let ((mod (make-fresh-user-module)))
68 (module-define! mod 'proc (lambda () (values)))
69 (module-define! mod 'args '())
71 (lambda () (run-bytecode '(apply proc args) #:env mod))
74 (pass-if-equal "top-level apply [two values]"
76 (let ((mod (make-fresh-user-module)))
77 (module-define! mod 'proc (lambda (n d) (floor/ n d)))
78 (module-define! mod 'args '(5 3))
80 (lambda () (run-bytecode '(apply proc args) #:env mod))
83 (pass-if-equal "call-with-values"
85 ((run-bytecode '(lambda (n d)
86 (call-with-values (lambda () (floor/ n d))
87 (lambda (q r) (list q r (+ q r))))))
94 ((run-bytecode '(lambda () 1))))
97 ((run-bytecode '(lambda (x) 1)) 2))
100 ((run-bytecode '(lambda (x) x)) 1))
103 ((((run-bytecode '(lambda (x)
106 (+ x y z))))) 1) 2) 3))
109 (run-bytecode '(identity 1)))
111 (pass-if-equal '(1 . 2)
112 (run-bytecode '(cons 1 2)))
114 (pass-if-equal '(1 2)
115 (call-with-values (lambda () (run-bytecode '(values 1 2))) list))
118 ((run-bytecode '(lambda (x y z rest) (apply + x y z rest)))
124 (with-test-prefix "value context"
128 (with-test-prefix "drop context"
132 (with-test-prefix "test context"
136 (with-test-prefix "values context"
137 (pass-if-equal '(3 . 1)
139 '(let ((rat (lambda (n d)
141 (lambda () (floor/ n d))
146 (with-test-prefix "contification"
147 (pass-if ((run-bytecode '(lambda (x)
149 (if (null? x) #t (odd? (cdr x))))
151 (if (null? x) #f (even? (cdr x))))
155 (pass-if (not ((run-bytecode '(lambda (x)
157 (if (null? x) #t (odd? (cdr x))))
159 (if (null? x) #f (even? (cdr x))))
164 ((run-bytecode '(lambda (x)
166 (if (null? x) #t (odd? (cdr x))))
168 (if (null? x) #f (even? (cdr x))))
172 ;; An irreducible loop between even? and odd?.
174 ((run-bytecode '(lambda (x do-even?)
176 (if (null? x) #t (odd? (cdr x))))
178 (if (null? x) #f (even? (cdr x))))
179 (if do-even? (even? x) (odd? x))))
183 (with-test-prefix "case-lambda"
184 (pass-if-equal "simple"
186 (let ((proc (run-bytecode '(case-lambda
190 ((x y z . rest) (apply + x y z rest))))))
191 (map (lambda (args) (apply proc args))
192 '(() (3) (2 7) (2 3 5 7 11)))))
194 (pass-if-exception "no match"
195 exception:wrong-num-args
196 ((run-bytecode '(case-lambda ((x) x) ((x y) (+ x y))))
199 (pass-if-exception "zero clauses called with no args"
200 exception:wrong-num-args
201 ((run-bytecode '(case-lambda))))
203 (pass-if-exception "zero clauses called with args"
204 exception:wrong-num-args
205 ((run-bytecode '(case-lambda)) 1)))
207 (with-test-prefix "mixed contexts"
208 (pass-if-equal "sequences" '(3 4 5)
209 (let* ((pair (cons 1 2))
210 (result ((run-bytecode '(lambda (pair)
219 (pass-if-equal "mutable lexicals" 2
220 (run-bytecode '(let ((n 1)) (set! n 2) n))))