Debian Codegraph @

test-suite/tests/rtl.test

raw
1 ;;;; Low-level tests of the bytecode assembler -*- mode: scheme; coding: utf-8; -*-
2 ;;;;
3 ;;;; Copyright (C) 2010-2015, 2017-2019 Free Software Foundation, Inc.
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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
18
19 (define-module (tests bytecode)
20 #:use-module (test-suite lib)
21 #:use-module (system vm assembler)
22 #:use-module (system vm program)
23 #:use-module (system vm loader)
24 #:use-module (system vm linker)
25 #:use-module (system vm debug))
26
27 (define (assemble-program instructions)
28 "Take the sequence of instructions @var{instructions}, assemble them
29 into bytecode, link an image, and load that image from memory. Returns
30 a procedure."
31 (let ((asm (make-assembler)))
32 (emit-text asm instructions)
33 (load-thunk-from-memory (link-assembly asm #:page-aligned? #f))))
34
35 (define-syntax-rule (assert-equal val expr)
36 (let ((x val))
37 (pass-if (object->string x) (equal? expr x))))
38
39 (define (return-constant val)
40 (assemble-program `((begin-program foo
41 ((name . foo)))
42 (begin-standard-arity #t () 1 #f)
43 (load-constant 0 ,val)
44 (return-values)
45 (end-arity)
46 (end-program))))
47
48 (define-syntax-rule (assert-constants val ...)
49 (begin
50 (assert-equal val ((return-constant val)))
51 ...))
52
53 (with-test-prefix "load-constant"
54 (assert-constants
55 1
56 -1
57 0
58 most-positive-fixnum
59 most-negative-fixnum
60 #t
61 #\c
62 (integer->char 16000)
63 3.14
64 "foo"
65 'foo
66 #:foo
67 "æ" ;; a non-ASCII Latin-1 string
68 "λ" ;; non-ascii, non-latin-1
69 '(1 . 2)
70 '(1 2 3 4)
71 #(1 2 3)
72 #("foo" "bar" 'baz)
73 #vu8()
74 #vu8(1 2 3 4 128 129 130)
75 #u32()
76 #u32(1 2 3 4 128 129 130 255 1000)
77 ;; FIXME: Add more tests for arrays (uniform and otherwise)
78 ))
79
80 (define-syntax-rule (assert-bad-constants val ...)
81 (begin
82 (pass-if-exception (object->string val) exception:miscellaneous-error
83 (return-constant val))
84 ...))
85
86 (with-test-prefix "bad constants"
87 (assert-bad-constants (make-symbol "foo")
88 (lambda () 100)))
89
90 (with-test-prefix "static procedure"
91 (assert-equal 42
92 (((assemble-program `((begin-program foo
93 ((name . foo)))
94 (begin-standard-arity #t () 1 #f)
95 (load-static-procedure 0 bar)
96 (return-values)
97 (end-arity)
98 (end-program)
99 (begin-program bar
100 ((name . bar)))
101 (begin-standard-arity #t () 1 #f)
102 (load-constant 0 42)
103 (return-values)
104 (end-arity)
105 (end-program)))))))
106
107 (with-test-prefix "loop"
108 (assert-equal (* 999 500)
109 (let ((sumto
110 (assemble-program
111 ;; 0: limit
112 ;; 1: n
113 ;; 2: accum
114 '((begin-program countdown
115 ((name . countdown)))
116 (begin-standard-arity #t (x) 4 #f)
117 (definition closure 0 scm)
118 (definition x 1 scm)
119 (j fix-body)
120 (label loop-head)
121 (=? 1 2)
122 (je out)
123 (add 0 1 0)
124 (add/immediate 1 1 1)
125 (j loop-head)
126 (label fix-body)
127 (load-constant 1 0)
128 (load-constant 0 0)
129 (j loop-head)
130 (label out)
131 (mov 3 0)
132 (reset-frame 1)
133 (return-values)
134 (end-arity)
135 (end-program)))))
136 (sumto 1000))))
137
138 (with-test-prefix "call"
139 (assert-equal 42
140 (let ((call ;; (lambda (x) (x))
141 (assemble-program
142 '((begin-program call
143 ((name . call)))
144 (begin-standard-arity #t (f) 7 #f)
145 (definition closure 0 scm)
146 (definition f 1 scm)
147 (mov 1 5)
148 (call 5 1)
149 (receive 0 5 7)
150 (reset-frame 1)
151 (return-values)
152 (end-arity)
153 (end-program)))))
154 (call (lambda () 42))))
155
156 (assert-equal 6
157 (let ((call-with-3 ;; (lambda (x) (x 3))
158 (assemble-program
159 '((begin-program call-with-3
160 ((name . call-with-3)))
161 (begin-standard-arity #t (f) 7 #f)
162 (definition closure 0 scm)
163 (definition f 1 scm)
164 (mov 1 5)
165 (load-constant 0 3)
166 (call 5 2)
167 (receive 0 5 7)
168 (reset-frame 1)
169 (return-values)
170 (end-arity)
171 (end-program)))))
172 (call-with-3 (lambda (x) (* x 2))))))
173
174 (with-test-prefix "tail-call"
175 (assert-equal 3
176 (let ((call ;; (lambda (x) (x))
177 (assemble-program
178 '((begin-program call
179 ((name . call)))
180 (begin-standard-arity #t (f) 2 #f)
181 (definition closure 0 scm)
182 (definition f 1 scm)
183 (mov 1 0)
184 (reset-frame 1)
185 (tail-call)
186 (end-arity)
187 (end-program)))))
188 (call (lambda () 3))))
189
190 (assert-equal 6
191 (let ((call-with-3 ;; (lambda (x) (x 3))
192 (assemble-program
193 '((begin-program call-with-3
194 ((name . call-with-3)))
195 (begin-standard-arity #t (f) 2 #f)
196 (definition closure 0 scm)
197 (definition f 1 scm)
198 (mov 1 0) ;; R0 <- R1
199 (load-constant 0 3) ;; R1 <- 3
200 (tail-call)
201 (end-arity)
202 (end-program)))))
203 (call-with-3 (lambda (x) (* x 2))))))
204
205 (with-test-prefix "debug contexts"
206 (let ((return-3 (assemble-program
207 '((begin-program return-3 ((name . return-3)))
208 (begin-standard-arity #t () 1 #f)
209 (load-constant 0 3)
210 (return-values)
211 (end-arity)
212 (end-program)))))
213 (pass-if "program name"
214 (and=> (find-program-debug-info (program-code return-3))
215 (lambda (pdi)
216 (equal? (program-debug-info-name pdi)
217 'return-3))))
218
219 (pass-if "program address"
220 (and=> (find-program-debug-info (program-code return-3))
221 (lambda (pdi)
222 (equal? (program-debug-info-addr pdi)
223 (program-code return-3)))))))
224
225 (with-test-prefix "procedure name"
226 (pass-if-equal 'foo
227 (procedure-name
228 (assemble-program
229 '((begin-program foo ((name . foo)))
230 (begin-standard-arity #t () 1 #f)
231 (load-constant 0 42)
232 (return-values)
233 (end-arity)
234 (end-program))))))
235
236 (with-test-prefix "simple procedure arity"
237 (pass-if-equal "#<procedure foo ()>"
238 (object->string
239 (assemble-program
240 '((begin-program foo ((name . foo)))
241 (begin-standard-arity #t () 1 #f)
242 (definition closure 0 scm)
243 (load-constant 0 42)
244 (return-values)
245 (end-arity)
246 (end-program)))))
247 (pass-if-equal "#<procedure foo (x y)>"
248 (object->string
249 (assemble-program
250 '((begin-program foo ((name . foo)))
251 (begin-standard-arity #t (x y) 3 #f)
252 (definition closure 0 scm)
253 (definition x 1 scm)
254 (definition y 2 scm)
255 (load-constant 2 42)
256 (reset-frame 1)
257 (return-values)
258 (end-arity)
259 (end-program)))))
260
261 (pass-if-equal "#<procedure foo (x #:optional y . z)>"
262 (object->string
263 (assemble-program
264 '((begin-program foo ((name . foo)))
265 (begin-opt-arity #t (x) (y) z 4 #f)
266 (definition closure 0 scm)
267 (definition x 1 scm)
268 (definition y 2 scm)
269 (definition z 3 scm)
270 (load-constant 3 42)
271 (reset-frame 1)
272 (return-values)
273 (end-arity)
274 (end-program))))))
275
276 (with-test-prefix "procedure docstrings"
277 (pass-if-equal "qux qux"
278 (procedure-documentation
279 (assemble-program
280 '((begin-program foo ((name . foo) (documentation . "qux qux")))
281 (begin-standard-arity #t () 1 #f)
282 (load-constant 0 42)
283 (return-values)
284 (end-arity)
285 (end-program))))))
286
287 (with-test-prefix "procedure properties"
288 ;; No properties.
289 (pass-if-equal '()
290 (procedure-properties
291 (assemble-program
292 '((begin-program foo ())
293 (begin-standard-arity #t () 1 #f)
294 (load-constant 0 42)
295 (return-values)
296 (end-arity)
297 (end-program)))))
298
299 ;; Name and docstring (which actually don't go out to procprops).
300 (pass-if-equal '((name . foo)
301 (documentation . "qux qux"))
302 (procedure-properties
303 (assemble-program
304 '((begin-program foo ((name . foo) (documentation . "qux qux")))
305 (begin-standard-arity #t () 1 #f)
306 (load-constant 0 42)
307 (return-values)
308 (end-arity)
309 (end-program)))))
310
311 ;; A property that actually needs serialization.
312 (pass-if-equal '((name . foo)
313 (documentation . "qux qux")
314 (moo . "mooooooooooooo"))
315 (procedure-properties
316 (assemble-program
317 '((begin-program foo ((name . foo)
318 (documentation . "qux qux")
319 (moo . "mooooooooooooo")))
320 (begin-standard-arity #t () 1 #f)
321 (load-constant 0 42)
322 (return-values)
323 (end-arity)
324 (end-program)))))
325
326 ;; Procedure-name still works in this case.
327 (pass-if-equal 'foo
328 (procedure-name
329 (assemble-program
330 '((begin-program foo ((name . foo)
331 (documentation . "qux qux")
332 (moo . "mooooooooooooo")))
333 (begin-standard-arity #t () 1 #f)
334 (load-constant 0 42)
335 (return-values)
336 (end-arity)
337 (end-program))))))