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 (assert-nargs-ee/locals 0 1)
33 (load-constant 0 ,val)
37 (define-syntax-rule (assert-constants val ...)
39 (assert-equal val ((return-constant val)))
42 (with-test-prefix "load-constant"
56 "æ" ;; a non-ASCII Latin-1 string
57 "λ" ;; non-ascii, non-latin-1
62 ;; FIXME: Add tests for arrays (uniform and otherwise)
65 (with-test-prefix "static procedure"
67 (((assemble-program `((begin-program foo
69 (assert-nargs-ee/locals 0 1)
70 (load-static-procedure 0 bar)
75 (assert-nargs-ee/locals 0 1)
80 (with-test-prefix "loop"
81 (assert-equal (* 999 500)
87 '((begin-program countdown
89 (assert-nargs-ee/locals 1 2)
105 (with-test-prefix "accum"
106 (assert-equal (+ 1 2 3)
112 '((begin-program make-accum
113 ((name . make-accum)))
114 (assert-nargs-ee/locals 0 2)
117 (make-closure 1 accum (0))
122 (assert-nargs-ee/locals 1 2)
129 (let ((accum (make-accum)))
134 (with-test-prefix "call"
136 (let ((call ;; (lambda (x) (x))
138 '((begin-program call
140 (assert-nargs-ee/locals 1 0)
142 (return 1) ;; MVRA from call
143 (return 1) ;; RA from call
145 (call (lambda () 42))))
148 (let ((call-with-3 ;; (lambda (x) (x 3))
150 '((begin-program call-with-3
151 ((name . call-with-3)))
152 (assert-nargs-ee/locals 1 1)
155 (return 2) ;; MVRA from call
156 (return 2) ;; RA from call
158 (call-with-3 (lambda (x) (* x 2))))))
160 (with-test-prefix "tail-call"
162 (let ((call ;; (lambda (x) (x))
164 '((begin-program call
166 (assert-nargs-ee/locals 1 0)
169 (call (lambda () 3))))
172 (let ((call-with-3 ;; (lambda (x) (x 3))
174 '((begin-program call-with-3
175 ((name . call-with-3)))
176 (assert-nargs-ee/locals 1 1)
177 (mov 1 0) ;; R1 <- R0
178 (load-constant 0 3) ;; R0 <- 3
181 (call-with-3 (lambda (x) (* x 2))))))
183 (with-test-prefix "cached-toplevel-ref"
185 (let ((get-sqrt-trampoline
187 '((begin-program get-sqrt-trampoline
188 ((name . get-sqrt-trampoline)))
189 (assert-nargs-ee/locals 0 1)
190 (cache-current-module! 0 sqrt-scope)
191 (load-static-procedure 0 sqrt-trampoline)
195 (begin-program sqrt-trampoline
196 ((name . sqrt-trampoline)))
197 (assert-nargs-ee/locals 1 1)
198 (cached-toplevel-ref 1 sqrt-scope sqrt)
201 ((get-sqrt-trampoline) 25.0))))
205 (with-test-prefix "cached-toplevel-set!"
206 (let ((prev *top-val*))
207 (assert-equal (1+ prev)
208 (let ((make-top-incrementor
210 '((begin-program make-top-incrementor
211 ((name . make-top-incrementor)))
212 (assert-nargs-ee/locals 0 1)
213 (cache-current-module! 0 top-incrementor)
214 (load-static-procedure 0 top-incrementor)
218 (begin-program top-incrementor
219 ((name . top-incrementor)))
220 (assert-nargs-ee/locals 0 1)
221 (cached-toplevel-ref 0 top-incrementor *top-val*)
223 (cached-toplevel-set! 0 top-incrementor *top-val*)
226 ((make-top-incrementor))
229 (with-test-prefix "cached-module-ref"
231 (let ((get-sqrt-trampoline
233 '((begin-program get-sqrt-trampoline
234 ((name . get-sqrt-trampoline)))
235 (assert-nargs-ee/locals 0 1)
236 (load-static-procedure 0 sqrt-trampoline)
240 (begin-program sqrt-trampoline
241 ((name . sqrt-trampoline)))
242 (assert-nargs-ee/locals 1 1)
243 (cached-module-ref 1 (guile) #t sqrt)
246 ((get-sqrt-trampoline) 25.0))))
248 (with-test-prefix "cached-module-set!"
249 (let ((prev *top-val*))
250 (assert-equal (1+ prev)
251 (let ((make-top-incrementor
253 '((begin-program make-top-incrementor
254 ((name . make-top-incrementor)))
255 (assert-nargs-ee/locals 0 1)
256 (load-static-procedure 0 top-incrementor)
260 (begin-program top-incrementor
261 ((name . top-incrementor)))
262 (assert-nargs-ee/locals 0 1)
263 (cached-module-ref 0 (tests rtl) #f *top-val*)
265 (cached-module-set! 0 (tests rtl) #f *top-val*)
268 ((make-top-incrementor))
271 (with-test-prefix "debug contexts"
272 (let ((return-3 (assemble-program
273 '((begin-program return-3 ((name . return-3)))
274 (assert-nargs-ee/locals 0 1)
278 (pass-if "program name"
279 (and=> (find-program-debug-info (rtl-program-code return-3))
281 (equal? (program-debug-info-name pdi)
284 (pass-if "program address"
285 (and=> (find-program-debug-info (rtl-program-code return-3))
287 (equal? (program-debug-info-addr pdi)
288 (rtl-program-code return-3)))))))
290 (with-test-prefix "procedure name"
294 '((begin-program foo ((name . foo)))
295 (assert-nargs-ee/locals 0 1)