Debian Codegraph @

test-suite/tests/compiler.test

raw
1 ;;;; compiler.test --- tests for the compiler -*- scheme -*-
2 ;;;; Copyright (C) 2008-2014, 2018, 2021-2022, 2024 Free Software Foundation, Inc.
3 ;;;;
4 ;;;; This library is free software; you can redistribute it and/or
5 ;;;; modify it under the terms of the GNU Lesser General Public
6 ;;;; License as published by the Free Software Foundation; either
7 ;;;; version 3 of the License, or (at your option) any later version.
8 ;;;;
9 ;;;; This library is distributed in the hope that it will be useful,
10 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;;; Lesser General Public License for more details.
13 ;;;;
14 ;;;; You should have received a copy of the GNU Lesser General Public
15 ;;;; License along with this library; if not, write to the Free Software
16 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 (define-module (tests compiler)
19 #:use-module (test-suite lib)
20 #:use-module (test-suite guile-test)
21 #:use-module (system base compile)
22 #:use-module ((language tree-il)
23 #:select (tree-il-src call-args))
24 #:use-module ((system vm loader) #:select (load-thunk-from-memory))
25 #:use-module ((system vm program) #:select (program-sources source:addr)))
26
27 (define read-and-compile
28 (@@ (system base compile) read-and-compile))
29
30
31
32 (with-test-prefix "basic"
33
34 (pass-if "compile to value"
35 (equal? (compile 1) 1)))
36
37
38 (with-test-prefix "psyntax"
39
40 (pass-if "compile uses a fresh module by default"
41 (begin
42 (compile '(define + -))
43 (eq? (compile '+) +)))
44
45 (pass-if "compile-time definitions are isolated"
46 (begin
47 (compile '(define foo-bar #t))
48 (not (module-variable (current-module) 'foo-bar))))
49
50 (pass-if "compile in current module"
51 (let ((o (begin
52 (compile '(define-macro (foo) 'bar)
53 #:env (current-module))
54 (compile '(let ((bar 'ok)) (foo))
55 #:env (current-module)))))
56 (and (macro? (module-ref (current-module) 'foo))
57 (eq? o 'ok))))
58
59 (pass-if "compile in fresh module"
60 (let* ((m (let ((m (make-module)))
61 (beautify-user-module! m)
62 m))
63 (o (begin
64 (compile '(define-macro (foo) 'bar) #:env m)
65 (compile '(let ((bar 'ok)) (foo)) #:env m))))
66 (and (module-ref m 'foo)
67 (eq? o 'ok))))
68
69 (pass-if "redefinition"
70 ;; In this case the locally-bound `round' must have the same value as the
71 ;; imported `round'. See the same test in `syntax.test' for details.
72 (let ((m (make-module)))
73 (beautify-user-module! m)
74 (compile '(define round round) #:env m)
75 (eq? round (module-ref m 'round))))
76
77 (pass-if-equal "syntax-source with read-hash-extend"
78 '((filename . "sample.scm") (line . 2) (column . 5))
79 ;; In Guile 3.0.8, psyntax would dismiss source properties added by
80 ;; read hash extensions on data they return.
81 ;; See <https://issues.guix.gnu.org/54003>
82 (with-fluids ((%read-hash-procedures
83 (fluid-ref %read-hash-procedures)))
84 (read-hash-extend #\~ (lambda (chr port)
85 (list 'magic (read port))))
86 (tree-il-src
87 (car
88 (call-args
89 (call-with-input-string "\
90 ;; first line
91 ;; second line
92 #~(this is a magic expression)"
93 (lambda (port)
94 (set-port-filename! port "sample.scm")
95 (compile (read-syntax port) #:to 'tree-il)))))))))
96
97
98 (with-test-prefix "current-reader"
99
100 (pass-if "default compile-time current-reader differs"
101 (not (eq? (compile 'current-reader)
102 current-reader)))
103
104 (pass-if "compile-time changes are honored and isolated"
105 ;; Make sure changing `current-reader' as the side-effect of a defmacro
106 ;; actually works.
107 (let ((r (fluid-ref current-reader))
108 (input (open-input-string
109 "(define-macro (install-reader!)
110 ;;(format #t \"current-reader = ~A~%\" current-reader)
111 (fluid-set! current-reader
112 (let ((first? #t))
113 (lambda args
114 (if first?
115 (begin
116 (set! first? #f)
117 ''ok)
118 (read (open-input-string \"\"))))))
119 #f)
120 (install-reader!)
121 this-should-be-ignored")))
122 (and (eq? ((load-thunk-from-memory (read-and-compile input)))
123 'ok)
124 (eq? r (fluid-ref current-reader)))))
125
126 (pass-if "with eval-when"
127 (let ((r (fluid-ref current-reader)))
128 (compile '(eval-when (compile eval)
129 (fluid-set! current-reader (lambda args 'chbouib))))
130 (eq? (fluid-ref current-reader) r))))
131
132
133 (with-test-prefix "procedure-name"
134
135 (pass-if "program"
136 (let ((m (make-module)))
137 (beautify-user-module! m)
138 (compile '(define (foo x) x) #:env m)
139 (eq? (procedure-name (module-ref m 'foo)) 'foo)))
140
141 (pass-if "program with lambda"
142 (let ((m (make-module)))
143 (beautify-user-module! m)
144 (compile '(define foo (lambda (x) x)) #:env m)
145 (eq? (procedure-name (module-ref m 'foo)) 'foo)))
146
147 (pass-if "subr"
148 (eq? (procedure-name waitpid) 'waitpid)))
149
150
151 (with-test-prefix "program-sources"
152
153 (with-test-prefix "source info associated with IP 0"
154
155 ;; Tools like `(system vm coverage)' like it when source info is associated
156 ;; with IP 0 of a VM program, which corresponds to the entry point. See
157 ;; also <http://savannah.gnu.org/bugs/?29817> for details.
158
159 (pass-if "lambda"
160 (let ((s (program-sources (compile '(lambda (x) x)))))
161 (not (not (memv 0 (map source:addr s))))))
162
163 (pass-if "lambda*"
164 (let ((s (program-sources
165 (compile '(lambda* (x #:optional y) x)))))
166 (not (not (memv 0 (map source:addr s))))))
167
168 (pass-if "case-lambda"
169 (let ((s (program-sources
170 (compile '(case-lambda (() #t)
171 ((y) y)
172 ((y z) (list y z)))))))
173 (not (not (memv 0 (map source:addr s))))))))
174
175 (with-test-prefix "case-lambda"
176 (pass-if "self recursion to different clause"
177 (equal? (with-output-to-string
178 (lambda ()
179 (let ()
180 (define t
181 (case-lambda
182 ((x)
183 (t x 'y))
184 ((x y)
185 (display (list x y))
186 (list x y))))
187 (display (t 'x)))))
188 "(x y)(x y)")))
189
190 (with-test-prefix "limits"
191 (define (arg n)
192 (string->symbol (format #f "arg~a" n)))
193
194 ;; Cons and vector-set! take uint8 arguments, so this triggers the
195 ;; shuffling case. Also there is the case where more than 252
196 ;; arguments causes shuffling.
197
198 (pass-if "300 arguments"
199 (equal? (apply (compile `(lambda ,(map arg (iota 300))
200 'foo))
201 (iota 300))
202 'foo))
203
204 (pass-if "300 arguments with list"
205 (equal? (apply (compile `(lambda ,(map arg (iota 300))
206 (list ,@(reverse (map arg (iota 300))))))
207 (iota 300))
208 (reverse (iota 300))))
209
210 (pass-if "300 arguments with vector"
211 (equal? (apply (compile `(lambda ,(map arg (iota 300))
212 (vector ,@(reverse (map arg (iota 300))))))
213 (iota 300))
214 (list->vector (reverse (iota 300)))))
215
216 (pass-if "0 arguments with list of 300 elements"
217 (equal? ((compile `(lambda ()
218 (list ,@(map (lambda (n) `(identity ,n))
219 (iota 300))))))
220 (iota 300)))
221
222 (pass-if "0 arguments with vector of 300 elements"
223 (equal? ((compile `(lambda ()
224 (vector ,@(map (lambda (n) `(identity ,n))
225 (iota 300))))))
226 (list->vector (iota 300)))))
227
228 (with-test-prefix "regression tests"
229 (pass-if-equal "#18583" 1
230 (compile
231 '(begin
232 (define x (list 1))
233 (define x (car x))
234 x)))
235
236 (pass-if "Chained comparisons"
237 (not (compile
238 '(false-if-exception (< 'not-a-number)))))
239
240 (pass-if-equal "(not (list 1 2))" ;https://bugs.gnu.org/58217
241 '(#f #f)
242 ;; The baseline compiler (-O0 and -O1) in 3.0.8 would crash.
243 (list (compile '(not (list 1 2)) #:optimization-level 2)
244 (compile '(not (list 1 2)) #:optimization-level 0))))
245
246 (with-test-prefix "prompt body slot allocation"
247 (define test-code
248 '(begin
249 (use-modules (ice-9 control))
250
251 (define (foo k) (k))
252 (define (qux k) 42)
253
254 (define (test)
255 (let lp ((i 0))
256 (when (< i 5)
257 (let/ec cancel (let lp () (qux cancel) (foo cancel) (lp)))
258 (lp (1+ i)))))
259 test))
260 (define test-proc #f)
261 (pass-if "compiling test works"
262 (begin
263 (set! test-proc (compile test-code))
264 (procedure? test-proc)))
265
266 (pass-if "test terminates without error"
267 (begin
268 (test-proc)
269 #t)))
270
271 (with-test-prefix "flonum inference"
272 (define test-code
273 '(lambda (x) (let ((y (if x 0.0 0.0+0.0i))) (+ y 0.0))))
274 (define test-proc #f)
275 (pass-if "compiling test works"
276 (begin
277 (set! test-proc (compile test-code))
278 (procedure? test-proc)))
279
280 (pass-if-equal "test flonum" 0.0 (test-proc #t))
281 (pass-if-equal "test complex" 0.0+0.0i (test-proc #f)))
282
283 (with-test-prefix "null? and nil? inference"
284 (pass-if-equal "nil? after null?"
285 '((f . f) ; 3
286 (f . f) ; #t
287 (f . t) ; #f
288 (t . t) ; #nil
289 (t . t)) ; ()
290 (map (compile '(lambda (x)
291 (if (null? x)
292 (cons 't (if (nil? x) 't 'f))
293 (cons 'f (if (nil? x) 't 'f)))))
294 '(3 #t #f #nil ())))
295
296 (pass-if-equal "nil? after truth test"
297 '((t . f) ; 3
298 (t . f) ; #t
299 (f . t) ; #f
300 (f . t) ; #nil
301 (t . t)) ; ()
302 (map (compile '(lambda (x)
303 (if x
304 (cons 't (if (nil? x) 't 'f))
305 (cons 'f (if (nil? x) 't 'f)))))
306 '(3 #t #f #nil ())))
307
308 (pass-if-equal "null? after nil?"
309 '((f . f) ; 3
310 (f . f) ; #t
311 (t . f) ; #f
312 (t . t) ; #nil
313 (t . t)) ; ()
314 (map (compile '(lambda (x)
315 (if (nil? x)
316 (cons 't (if (null? x) 't 'f))
317 (cons 'f (if (null? x) 't 'f)))))
318 '(3 #t #f #nil ())))
319
320 (pass-if-equal "truth test after nil?"
321 '((f . t) ; 3
322 (f . t) ; #t
323 (t . f) ; #f
324 (t . f) ; #nil
325 (t . t)) ; ()
326 (map (compile '(lambda (x)
327 (if (nil? x)
328 (cons 't (if x 't 'f))
329 (cons 'f (if x 't 'f)))))
330 '(3 #t #f #nil ()))))
331
332 (with-test-prefix "cse auxiliary definitions"
333 (define test-proc
334 (compile
335 '(begin
336 (define count 1)
337 (set! count count) ;; Avoid inlining
338
339 (define (main)
340 (define (trampoline thunk)
341 (let loop ((i 0) (result #f))
342 (cond
343 ((< i 1)
344 (loop (+ i 1) (thunk)))
345 (else
346 (unless (= result 42) (error "bad result" result))
347 result))))
348 (define (test n)
349 (let ((matrix (make-vector n)))
350 (let loop ((i (- n 1)))
351 (when (>= i 0)
352 (vector-set! matrix i (make-vector n 42))
353 (loop (- i 1))))
354 (vector-ref (vector-ref matrix 0) 0)))
355
356 (trampoline (lambda () (test count))))
357 main)))
358
359 (pass-if-equal "running test" 42 (test-proc))
360
361 (define test2
362 (compile '(lambda (x)
363 (define pair (cons 42 69))
364 (when x (set-car! pair 100))
365 (car pair))))
366 (pass-if-equal "clobbering" 100 (test2 #t)))
367
368 (with-test-prefix "closure conversion"
369 (define test-proc
370 (compile
371 '(lambda (arg)
372 (define (A a)
373 (let loop ((ls a))
374 (cond ((null? ls)
375 (B a))
376 ((pair? ls)
377 (if (list? (car ls))
378 (loop (cdr ls))
379 #t))
380 (else #t))))
381 (define (B b)
382 (let loop ((ls b))
383 (cond ((null? ls)
384 (map A b))
385 ((pair? ls)
386 (if (list? (car ls))
387 (loop (cdr ls))
388 (error "bad" b)))
389 (else
390 (error "bad" b)))))
391 (B arg))))
392
393 (pass-if-equal "running test" '(#t #t)
394 (test-proc '((V X) (Y Z)))))
395
396 (with-test-prefix "constant propagation"
397 (define test-proc
398 (compile
399 '(lambda (a b)
400 (let ((c (if (and (eq? a 'foo)
401 (eq? b 'bar))
402 'qux
403 a)))
404 c))))
405
406 (pass-if-equal "one two" 'one (test-proc 'one 'two))
407 (pass-if-equal "one bar" 'one (test-proc 'one 'bar))
408 (pass-if-equal "foo bar" 'qux (test-proc 'foo 'bar))
409 (pass-if-equal "foo two" 'foo (test-proc 'foo 'two)))
410
411 (with-test-prefix "size effects in multi-arg eq / <"
412 (pass-if-equal "eq?" 42
413 (compile '(catch 'foo
414 (lambda () (= 0 1 (throw 'foo)))
415 (lambda (k) 42))))
416
417 (pass-if-equal "<" 42
418 (compile '(catch 'foo
419 (lambda () (< 0 -1 (throw 'foo)))
420 (lambda (k) 42)))))
421
422 (with-test-prefix "read-and-compile tree-il"
423 (let ((code
424 "\
425 (seq
426 (define forty-two
427 (lambda ((name . forty-two))
428 (lambda-case ((() #f #f #f () ()) (const 42)))))
429 (toplevel forty-two))")
430 (bytecode #f)
431 (proc #f))
432 (pass-if "compiling tree-il works"
433 (begin
434 (set! bytecode
435 (call-with-input-string code
436 (lambda (port)
437 (read-and-compile port #:from 'tree-il))))
438 #t))
439 (pass-if "bytecode can be read"
440 (begin
441 (set! proc ((load-thunk-from-memory bytecode)))
442 (procedure? proc)))
443 (pass-if-equal "proc executes" 42 (proc))))