1 ;;;; Low-level tests of the RTL assembler -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2010, 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
19 (define-module (tests rtl)
20 #:use-module (test-suite lib)
21 #:use-module (system vm assembler)
22 #:use-module (system vm program)
23 #:use-module (system vm debug))
25 (define-syntax-rule (assert-equal val expr)
27 (pass-if (object->string x) (equal? expr x))))
29 (define (return-constant val)
30 (assemble-program `((begin-program foo
32 (begin-standard-arity () 2 #f)
33 (load-constant 1 ,val)
38 (define-syntax-rule (assert-constants val ...)
40 (assert-equal val ((return-constant val)))
43 (with-test-prefix "load-constant"
57 "æ" ;; a non-ASCII Latin-1 string
58 "λ" ;; non-ascii, non-latin-1
63 ;; FIXME: Add tests for arrays (uniform and otherwise)
66 (with-test-prefix "static procedure"
68 (((assemble-program `((begin-program foo
70 (begin-standard-arity () 2 #f)
71 (load-static-procedure 1 bar)
77 (begin-standard-arity () 2 #f)
83 (with-test-prefix "loop"
84 (assert-equal (* 999 500)
90 '((begin-program countdown
92 (begin-standard-arity (x) 4 #f)
109 (with-test-prefix "accum"
110 (assert-equal (+ 1 2 3)
116 '((begin-program make-accum
117 ((name . make-accum)))
118 (begin-standard-arity () 3 #f)
121 (make-closure 2 accum 1)
128 (begin-standard-arity (x) 4 #f)
136 (let ((accum (make-accum)))
141 (with-test-prefix "call"
143 (let ((call ;; (lambda (x) (x))
145 '((begin-program call
147 (begin-standard-arity (f) 7 #f)
154 (call (lambda () 42))))
157 (let ((call-with-3 ;; (lambda (x) (x 3))
159 '((begin-program call-with-3
160 ((name . call-with-3)))
161 (begin-standard-arity (f) 7 #f)
169 (call-with-3 (lambda (x) (* x 2))))))
171 (with-test-prefix "tail-call"
173 (let ((call ;; (lambda (x) (x))
175 '((begin-program call
177 (begin-standard-arity (f) 2 #f)
182 (call (lambda () 3))))
185 (let ((call-with-3 ;; (lambda (x) (x 3))
187 '((begin-program call-with-3
188 ((name . call-with-3)))
189 (begin-standard-arity (f) 2 #f)
190 (mov 0 1) ;; R0 <- R1
191 (load-constant 1 3) ;; R1 <- 3
195 (call-with-3 (lambda (x) (* x 2))))))
197 (with-test-prefix "cached-toplevel-ref"
199 (let ((get-sqrt-trampoline
201 '((begin-program get-sqrt-trampoline
202 ((name . get-sqrt-trampoline)))
203 (begin-standard-arity () 2 #f)
205 (cache-current-module! 1 sqrt-scope)
206 (load-static-procedure 1 sqrt-trampoline)
211 (begin-program sqrt-trampoline
212 ((name . sqrt-trampoline)))
213 (begin-standard-arity (x) 3 #f)
214 (cached-toplevel-box 2 sqrt-scope sqrt #t)
219 ((get-sqrt-trampoline) 25.0))))
223 (with-test-prefix "cached-toplevel-set!"
224 (let ((prev *top-val*))
225 (assert-equal (1+ prev)
226 (let ((make-top-incrementor
228 '((begin-program make-top-incrementor
229 ((name . make-top-incrementor)))
230 (begin-standard-arity () 2 #f)
232 (cache-current-module! 1 top-incrementor)
233 (load-static-procedure 1 top-incrementor)
238 (begin-program top-incrementor
239 ((name . top-incrementor)))
240 (begin-standard-arity () 3 #f)
241 (cached-toplevel-box 1 top-incrementor *top-val* #t)
248 ((make-top-incrementor))
251 (with-test-prefix "cached-module-ref"
253 (let ((get-sqrt-trampoline
255 '((begin-program get-sqrt-trampoline
256 ((name . get-sqrt-trampoline)))
257 (begin-standard-arity () 2 #f)
258 (load-static-procedure 1 sqrt-trampoline)
263 (begin-program sqrt-trampoline
264 ((name . sqrt-trampoline)))
265 (begin-standard-arity (x) 3 #f)
266 (cached-module-box 2 (guile) sqrt #t #t)
271 ((get-sqrt-trampoline) 25.0))))
273 (with-test-prefix "cached-module-set!"
274 (let ((prev *top-val*))
275 (assert-equal (1+ prev)
276 (let ((make-top-incrementor
278 '((begin-program make-top-incrementor
279 ((name . make-top-incrementor)))
280 (begin-standard-arity () 2 #f)
281 (load-static-procedure 1 top-incrementor)
286 (begin-program top-incrementor
287 ((name . top-incrementor)))
288 (begin-standard-arity () 3 #f)
289 (cached-module-box 1 (tests rtl) *top-val* #f #t)
296 ((make-top-incrementor))
299 (with-test-prefix "debug contexts"
300 (let ((return-3 (assemble-program
301 '((begin-program return-3 ((name . return-3)))
302 (begin-standard-arity () 2 #f)
307 (pass-if "program name"
308 (and=> (find-program-debug-info (rtl-program-code return-3))
310 (equal? (program-debug-info-name pdi)
313 (pass-if "program address"
314 (and=> (find-program-debug-info (rtl-program-code return-3))
316 (equal? (program-debug-info-addr pdi)
317 (rtl-program-code return-3)))))))
319 (with-test-prefix "procedure name"
323 '((begin-program foo ((name . foo)))
324 (begin-standard-arity () 2 #f)
330 (with-test-prefix "simply procedure arity"
331 (pass-if-equal "#<procedure foo ()>"
334 '((begin-program foo ((name . foo)))
335 (begin-standard-arity () 2 #f)
340 (pass-if-equal "#<procedure foo (x y)>"
343 '((begin-program foo ((name . foo)))
344 (begin-standard-arity (x y) 3 #f)
350 (pass-if-equal "#<procedure foo (x #:optional y . z)>"
353 '((begin-program foo ((name . foo)))
354 (begin-opt-arity (x) (y) z 4 #f)
360 (with-test-prefix "procedure docstrings"
361 (pass-if-equal "qux qux"
362 (procedure-documentation
364 '((begin-program foo ((name . foo) (documentation . "qux qux")))
365 (begin-standard-arity () 2 #f)
371 (with-test-prefix "procedure properties"
374 (procedure-properties
376 '((begin-program foo ())
377 (begin-standard-arity () 2 #f)
383 ;; Name and docstring (which actually don't go out to procprops).
384 (pass-if-equal '((name . foo)
385 (documentation . "qux qux"))
386 (procedure-properties
388 '((begin-program foo ((name . foo) (documentation . "qux qux")))
389 (begin-standard-arity () 2 #f)
395 ;; A property that actually needs serialization.
396 (pass-if-equal '((name . foo)
397 (documentation . "qux qux")
398 (moo . "mooooooooooooo"))
399 (procedure-properties
401 '((begin-program foo ((name . foo)
402 (documentation . "qux qux")
403 (moo . "mooooooooooooo")))
404 (begin-standard-arity () 2 #f)
410 ;; Procedure-name still works in this case.
414 '((begin-program foo ((name . foo)
415 (documentation . "qux qux")
416 (moo . "mooooooooooooo")))
417 (begin-standard-arity () 2 #f)