Debian Codegraph @

test-suite/tests/peval.test

raw
1 ;;;; tree-il.test --- test suite for compiling tree-il -*- scheme -*-
2 ;;;; Andy Wingo <[email protected]> --- May 2009
3 ;;;;
4 ;;;; Copyright (C) 2009-2014, 2017, 2020, 2022-2024 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-suite tree-il)
21 #:use-module (test-suite lib)
22 #:use-module (system base compile)
23 #:use-module (system base pmatch)
24 #:use-module (system base message)
25 #:use-module (language tree-il)
26 #:use-module (language tree-il fix-letrec)
27 #:use-module (language tree-il peval)
28 #:use-module (language tree-il primitives)
29 #:use-module (rnrs bytevectors) ;; for the bytevector primitives
30 #:use-module (srfi srfi-13))
31
32 (define-syntax pass-if-peval
33 (syntax-rules ()
34 ((_ in pat)
35 (pass-if-peval in pat
36 (fix-letrec
37 (expand-primitives
38 (resolve-primitives
39 (compile 'in #:from 'scheme #:to 'tree-il)
40 (current-module))))))
41 ((_ in pat code)
42 (pass-if 'in
43 (let ((evaled (unparse-tree-il (peval code))))
44 (pmatch evaled
45 (pat #t)
46 (_ (pk 'peval-mismatch)
47 ((@ (ice-9 pretty-print) pretty-print)
48 'in)
49 (newline)
50 ((@ (ice-9 pretty-print) pretty-print)
51 evaled)
52 (newline)
53 ((@ (ice-9 pretty-print) pretty-print)
54 'pat)
55 (newline)
56 #f)))))))
57
58
59 (with-test-prefix "partial evaluation"
60
61 (pass-if-peval
62 ;; First order, primitive.
63 (let ((x 1) (y 2)) (+ x y))
64 (const 3))
65
66 (pass-if-peval
67 ;; First order, thunk.
68 (let ((x 1) (y 2))
69 (let ((f (lambda () (+ x y))))
70 (f)))
71 (const 3))
72
73 (pass-if-peval
74 ;; First order, let-values (requires primitive expansion for
75 ;; `call-with-values'.)
76 (let ((x 0))
77 (call-with-values
78 (lambda () (if (zero? x) (values 1 2) (values 3 4)))
79 (lambda (a b)
80 (+ a b))))
81 (const 3))
82
83 (pass-if-peval
84 ;; First order, multiple values.
85 (let ((x 1) (y 2))
86 (values x y))
87 (primcall values (const 1) (const 2)))
88
89 (pass-if-peval
90 ;; First order, multiple values truncated.
91 (let ((x (values 1 'a)) (y 2))
92 (values x y))
93 (primcall values (const 1) (const 2)))
94
95 (pass-if-peval
96 ;; First order, multiple values truncated.
97 (or (values 1 2) 3)
98 (const 1))
99
100 (pass-if-peval
101 ;; First order, coalesced, mutability preserved.
102 (cons 0 (cons 1 (cons 2 (list 3 4 5))))
103 (primcall list
104 (const 0) (const 1) (const 2) (const 3) (const 4) (const 5)))
105
106 (pass-if-peval
107 ;; First order, coalesced, immutability preserved.
108 (cons 0 (cons 1 (cons 2 '(3 4 5))))
109 (primcall cons (const 0)
110 (primcall cons (const 1)
111 (primcall cons (const 2)
112 (const (3 4 5))))))
113
114 ;; These two tests doesn't work any more because we changed the way we
115 ;; deal with constants -- now the algorithm will see a construction as
116 ;; being bound to the lexical, so it won't propagate it. It can't
117 ;; even propagate it in the case that it is only referenced once,
118 ;; because:
119 ;;
120 ;; (let ((x (cons 1 2))) (lambda () x))
121 ;;
122 ;; is not the same as
123 ;;
124 ;; (lambda () (cons 1 2))
125 ;;
126 ;; Perhaps if we determined that not only was it only referenced once,
127 ;; it was not closed over by a lambda, then we could propagate it, and
128 ;; re-enable these two tests.
129 ;;
130 #;
131 (pass-if-peval
132 ;; First order, mutability preserved.
133 (let loop ((i 3) (r '()))
134 (if (zero? i)
135 r
136 (loop (1- i) (cons (cons i i) r))))
137 (primcall list
138 (primcall cons (const 1) (const 1))
139 (primcall cons (const 2) (const 2))
140 (primcall cons (const 3) (const 3))))
141 ;;
142 ;; See above.
143 #;
144 (pass-if-peval
145 ;; First order, evaluated.
146 (let loop ((i 7)
147 (r '()))
148 (if (<= i 0)
149 (car r)
150 (loop (1- i) (cons i r))))
151 (const 1))
152
153 ;; Instead here are tests for what happens for the above cases: they
154 ;; unroll but they don't fold.
155 (pass-if-peval
156 (let loop ((i 3) (r '()))
157 (if (zero? i)
158 r
159 (loop (1- i) (cons (cons i i) r))))
160 (let (r) (_)
161 ((primcall list
162 (primcall cons (const 3) (const 3))))
163 (let (r) (_)
164 ((primcall cons
165 (primcall cons (const 2) (const 2))
166 (lexical r _)))
167 (primcall cons
168 (primcall cons (const 1) (const 1))
169 (lexical r _)))))
170
171 ;; See above.
172 (pass-if-peval
173 (let loop ((i 4)
174 (r '()))
175 (if (<= i 0)
176 (car r)
177 (loop (1- i) (cons i r))))
178 (let (r) (_)
179 ((primcall list (const 4)))
180 (let (r) (_)
181 ((primcall cons
182 (const 3)
183 (lexical r _)))
184 (let (r) (_)
185 ((primcall cons
186 (const 2)
187 (lexical r _)))
188 (let (r) (_)
189 ((primcall cons
190 (const 1)
191 (lexical r _)))
192 (primcall car
193 (lexical r _)))))))
194
195 ;; Static sums.
196 (pass-if-peval
197 (let loop ((l '(1 2 3 4)) (sum 0))
198 (if (null? l)
199 sum
200 (loop (cdr l) (+ sum (car l)))))
201 (const 10))
202
203 (pass-if-peval
204 (let ((string->chars
205 (lambda (s)
206 (define (char-at n)
207 (string-ref s n))
208 (define (len)
209 (string-length s))
210 (let loop ((i 0))
211 (if (< i (len))
212 (cons (char-at i)
213 (loop (1+ i)))
214 '())))))
215 (string->chars "yo"))
216 (primcall list (const #\y) (const #\o)))
217
218 (pass-if-peval
219 ;; Primitives in module-refs are resolved (the expansion of `pmatch'
220 ;; below leads to calls to (@@ (system base pmatch) car) and
221 ;; similar, which is what we want to be inlined.)
222 (begin
223 (use-modules (system base pmatch))
224 (pmatch '(a b c d)
225 ((a b . _)
226 #t)))
227 (seq (call . _)
228 (const #t)))
229
230 (pass-if-peval
231 ;; Mutability preserved.
232 ((lambda (x y z) (list x y z)) 1 2 3)
233 (primcall list (const 1) (const 2) (const 3)))
234
235 (pass-if-peval
236 ;; Don't propagate effect-free expressions that operate on mutable
237 ;; objects.
238 (let* ((x (list 1))
239 (y (car x)))
240 (set-car! x 0)
241 y)
242 (let (x) (_) ((primcall list (const 1)))
243 (let (y) (_) ((primcall car (lexical x _)))
244 (seq
245 (primcall set-car! (lexical x _) (const 0))
246 (lexical y _)))))
247
248 (pass-if-peval
249 ;; Don't propagate effect-free expressions that operate on objects we
250 ;; don't know about.
251 (let ((y (car x)))
252 (set-car! x 0)
253 y)
254 (let (y) (_) ((primcall car (toplevel x)))
255 (seq
256 (primcall set-car! (toplevel x) (const 0))
257 (lexical y _))))
258
259 (pass-if-peval
260 ;; Infinite recursion
261 ((lambda (x) (x x)) (lambda (x) (x x)))
262 (let (x) (_)
263 ((lambda _
264 (lambda-case
265 (((x) _ _ _ _ _)
266 (call (lexical x _) (lexical x _))))))
267 (call (lexical x _) (lexical x _))))
268
269 (pass-if-peval
270 ;; First order, aliased primitive.
271 (let* ((x *) (y (x 1 2))) y)
272 (const 2))
273
274 (pass-if-peval
275 ;; First order, shadowed primitive.
276 (begin
277 (define (+ x y) (pk x y))
278 (+ 1 2))
279 (seq
280 (define +
281 (lambda (_)
282 (lambda-case
283 (((x y) #f #f #f () (_ _))
284 (call (toplevel pk) (lexical x _) (lexical y _))))))
285 (call (toplevel +) (const 1) (const 2))))
286
287 (pass-if-peval
288 ;; First-order, effects preserved.
289 (let ((x 2))
290 (do-something!)
291 x)
292 (seq
293 (call (toplevel do-something!))
294 (const 2)))
295
296 (pass-if-peval
297 ;; First order, residual bindings removed.
298 (let ((x 2) (y 3))
299 (* (+ x y) z))
300 (primcall * (const 5) (toplevel z)))
301
302 (pass-if-peval
303 ;; First order, with lambda.
304 (define (foo x)
305 (define (bar z) (* z z))
306 (+ x (bar 3)))
307 (define foo
308 (lambda (_)
309 (lambda-case
310 (((x) #f #f #f () (_))
311 (primcall + (lexical x _) (const 9)))))))
312
313 (pass-if-peval
314 ;; First order, with lambda inlined & specialized twice.
315 (let ((f (lambda (x y)
316 (+ (* x top) y)))
317 (x 2)
318 (y 3))
319 (+ (* x (f x y))
320 (f something x)))
321 (primcall +
322 (primcall *
323 (const 2)
324 (primcall + ; (f 2 3)
325 (primcall *
326 (const 2)
327 (toplevel top))
328 (const 3)))
329 (let (x) (_) ((toplevel something)) ; (f something 2)
330 ;; `something' is not const, so preserve order of
331 ;; effects with a lexical binding.
332 (primcall +
333 (primcall *
334 (lexical x _)
335 (toplevel top))
336 (const 2)))))
337
338 (pass-if-peval
339 ;; First order, with lambda inlined & specialized 3 times.
340 (let ((f (lambda (x y) (if (> x 0) y x))))
341 (+ (f -1 0)
342 (f 1 0)
343 (f -1 y)
344 (f 2 y)
345 (f z y)))
346 (primcall
347 +
348 (primcall
349 +
350 (primcall
351 +
352 (const -1) ; (f -1 0)
353 (seq (toplevel y) (const -1))) ; (f -1 y)
354 (toplevel y)) ; (f 2 y)
355 (let (x y) (_ _) ((toplevel z) (toplevel y)) ; (f z y)
356 (if (primcall > (lexical x _) (const 0))
357 (lexical y _)
358 (lexical x _)))))
359
360 (pass-if-peval
361 ;; First order, conditional.
362 (let ((y 2))
363 (lambda (x)
364 (if (> y 0)
365 (display x)
366 'never-reached)))
367 (lambda ()
368 (lambda-case
369 (((x) #f #f #f () (_))
370 (call (toplevel display) (lexical x _))))))
371
372 (pass-if-peval
373 ;; First order, recursive procedure.
374 (letrec ((fibo (lambda (n)
375 (if (<= n 1)
376 n
377 (+ (fibo (- n 1))
378 (fibo (- n 2)))))))
379 (fibo 4))
380 (const 3))
381
382 (pass-if-peval
383 ;; Don't propagate toplevel references, as intervening expressions
384 ;; could alter their bindings.
385 (let ((x top))
386 (foo)
387 x)
388 (let (x) (_) ((toplevel top))
389 (seq
390 (call (toplevel foo))
391 (lexical x _))))
392
393 (pass-if-peval
394 ;; Higher order.
395 ((lambda (f x)
396 (f (* (car x) (cadr x))))
397 (lambda (x)
398 (+ x 1))
399 '(2 3))
400 (const 7))
401
402 (pass-if-peval
403 ;; Higher order with optional argument (default value).
404 ((lambda* (f x #:optional (y 0))
405 (+ y (f (* (car x) (cadr x)))))
406 (lambda (x)
407 (+ x 1))
408 '(2 3))
409 (const 7))
410
411 (pass-if-peval
412 ;; Higher order with optional argument (default uses earlier argument).
413 ;; <http://bugs.gnu.org/17634>
414 ((lambda* (f x #:optional (y (+ 3 (car x))))
415 (+ y (f (* (car x) (cadr x)))))
416 (lambda (x)
417 (+ x 1))
418 '(2 3))
419 (const 12))
420
421 (pass-if-peval
422 ;; Higher order with optional arguments
423 ;; (default uses earlier optional argument).
424 ((lambda* (f x #:optional (y (+ 3 (car x))) (z (+ (cadr x) y)))
425 (+ y z (f (* (car x) (cadr x)))))
426 (lambda (x)
427 (+ x 1))
428 '(2 3))
429 (const 20))
430
431 (pass-if-peval
432 ;; Higher order with optional arguments (one caller-supplied value,
433 ;; one default that uses earlier optional argument).
434 ((lambda* (f x #:optional (y (+ 3 (car x))) (z (+ (cadr x) y)))
435 (+ y z (f (* (car x) (cadr x)))))
436 (lambda (x)
437 (+ x 1))
438 '(2 3)
439 -3)
440 (const 4))
441
442 (pass-if-peval
443 ;; Higher order with optional arguments (caller-supplied values).
444 ((lambda* (f x #:optional (y (+ 3 (car x))) (z (+ (cadr x) y)))
445 (+ y z (f (* (car x) (cadr x)))))
446 (lambda (x)
447 (+ x 1))
448 '(2 3)
449 -3
450 17)
451 (const 21))
452
453 (pass-if-peval
454 ;; Higher order with optional and rest arguments (one
455 ;; caller-supplied value, one default that uses earlier optional
456 ;; argument).
457 ((lambda* (f x #:optional (y (+ 3 (car x))) (z (+ (cadr x) y))
458 #:rest r)
459 (list r (+ y z (f (* (car x) (cadr x))))))
460 (lambda (x)
461 (+ x 1))
462 '(2 3)
463 -3)
464 (primcall list (const ()) (const 4)))
465
466 (pass-if-peval
467 ;; Higher order with optional and rest arguments
468 ;; (caller-supplied values for optionals).
469 ((lambda* (f x #:optional (y (+ 3 (car x))) (z (+ (cadr x) y))
470 #:rest r)
471 (list r (+ y z (f (* (car x) (cadr x))))))
472 (lambda (x)
473 (+ x 1))
474 '(2 3)
475 -3
476 17)
477 (primcall list (const ()) (const 21)))
478
479 (pass-if-peval
480 ;; Higher order with optional and rest arguments
481 ;; (caller-supplied values for optionals and rest).
482 ((lambda* (f x #:optional (y (+ 3 (car x))) (z (+ (cadr x) y))
483 #:rest r)
484 (list r (+ y z (f (* (car x) (cadr x))))))
485 (lambda (x)
486 (+ x 1))
487 '(2 3)
488 -3
489 17
490 8
491 3)
492 (let (r) (_) ((primcall list (const 8) (const 3)))
493 (primcall list (lexical r _) (const 21))))
494
495 (pass-if-peval
496 ;; Higher order with optional argument (caller-supplied value).
497 ((lambda* (f x #:optional (y 0))
498 (+ y (f (* (car x) (cadr x)))))
499 (lambda (x)
500 (+ x 1))
501 '(2 3)
502 35)
503 (const 42))
504
505 (pass-if-peval
506 ;; Higher order with optional argument (side-effecting default
507 ;; value).
508 ((lambda* (f x #:optional (y (foo)))
509 (+ y (f (* (car x) (cadr x)))))
510 (lambda (x)
511 (+ x 1))
512 '(2 3))
513 (let (y) (_) ((call (toplevel foo)))
514 (primcall + (lexical y _) (const 7))))
515
516 (pass-if-peval
517 ;; Higher order with optional argument (caller-supplied value).
518 ((lambda* (f x #:optional (y (foo)))
519 (+ y (f (* (car x) (cadr x)))))
520 (lambda (x)
521 (+ x 1))
522 '(2 3)
523 35)
524 (const 42))
525
526 (pass-if-peval
527 ;; Higher order.
528 ((lambda (f) (f x)) (lambda (x) x))
529 (toplevel x))
530
531 (pass-if-peval
532 ;; Bug reported at
533 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html>.
534 (let ((fold (lambda (f g) (f (g top)))))
535 (fold 1+ (lambda (x) x)))
536 (primcall + (toplevel top) (const 1)))
537
538 (pass-if-peval
539 ;; Procedure not inlined when residual code contains recursive calls.
540 ;; <http://debbugs.gnu.org/9542>
541 (letrec ((fold (lambda (f x3 b null? car cdr)
542 (if (null? x3)
543 b
544 (f (car x3) (fold f (cdr x3) b null? car cdr))))))
545 (fold * x 1 zero? (lambda (x1) x1) (lambda (x2) (- x2 1))))
546 (fix (fold) (_) (_)
547 (call (lexical fold _)
548 (primitive *)
549 (toplevel x)
550 (const 1)
551 (primitive zero?)
552 (lambda ()
553 (lambda-case
554 (((x1) #f #f #f () (_))
555 (lexical x1 _))))
556 (lambda ()
557 (lambda-case
558 (((x2) #f #f #f () (_))
559 (primcall - (lexical x2 _) (const 1))))))))
560
561 (pass-if "inlined lambdas are alpha-renamed"
562 ;; In this example, `make-adder' is inlined more than once; thus,
563 ;; they should use different gensyms for their arguments, because
564 ;; the various optimization passes assume uniquely-named variables.
565 ;;
566 ;; Bug reported at
567 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html> and
568 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00029.html>.
569 (pmatch (unparse-tree-il
570 (peval (expand-primitives
571 (resolve-primitives
572 (compile
573 '(let ((make-adder
574 (lambda (x) (lambda (y) (+ x y)))))
575 (cons (make-adder 1) (make-adder 2)))
576 #:to 'tree-il)
577 (current-module)))))
578 ((primcall cons
579 (lambda ()
580 (lambda-case
581 (((y) #f #f #f () (,gensym1))
582 (primcall +
583 (const 1)
584 (lexical y ,ref1)))))
585 (lambda ()
586 (lambda-case
587 (((y) #f #f #f () (,gensym2))
588 (primcall +
589 (const 2)
590 (lexical y ,ref2))))))
591 (and (eq? gensym1 ref1)
592 (eq? gensym2 ref2)
593 (not (eq? gensym1 gensym2))))
594 (_ #f)))
595
596 (pass-if-peval
597 ;; Unused letrec bindings are pruned.
598 (letrec ((a (lambda () (b)))
599 (b (lambda () (a)))
600 (c (lambda (x) x)))
601 (c 10))
602 (const 10))
603
604 (pass-if-peval
605 ;; Unused letrec bindings are pruned.
606 (letrec ((a (foo!))
607 (b (lambda () (a)))
608 (c (lambda (x) x)))
609 (c 10))
610 (seq (call (toplevel foo!))
611 (const 10)))
612
613 (pass-if-peval
614 ;; Higher order, mutually recursive procedures.
615 (letrec ((even? (lambda (x)
616 (or (= 0 x)
617 (odd? (- x 1)))))
618 (odd? (lambda (x)
619 (not (even? x)))))
620 (and (even? 4) (odd? 7)))
621 (const #t))
622
623 (pass-if-peval
624 ;; Memv with constants.
625 (memv 1 '(3 2 1))
626 (const '(1)))
627
628 (pass-if-peval
629 ;; Memv with non-constant list. It could fold but doesn't
630 ;; currently.
631 (memv 1 (list 3 2 1))
632 (primcall memv
633 (const 1)
634 (primcall list (const 3) (const 2) (const 1))))
635
636 (pass-if-peval
637 ;; Memv with non-constant key, constant list, test context
638 (case foo
639 ((3 2 1) 'a)
640 (else 'b))
641 (let (key) (_) ((toplevel foo))
642 (if (if (primcall eq? (lexical key _) (const 3))
643 (const #t)
644 (if (primcall eq? (lexical key _) (const 2))
645 (const #t)
646 (primcall eq? (lexical key _) (const 1))))
647 (const a)
648 (const b))))
649
650 (pass-if-peval
651 ;; Memv with non-constant key, empty list, test context.
652 (case foo
653 (() 'a)
654 (else 'b))
655 (seq (toplevel foo) (const 'b)))
656
657 ;;
658 ;; Below are cases where constant propagation should bail out.
659 ;;
660
661 (pass-if-peval
662 ;; Non-constant lexical is not propagated.
663 (let ((v (make-vector 6 #f)))
664 (lambda (n)
665 (vector-set! v n n)))
666 (let (v) (_)
667 ((primcall make-vector (const 6) (const #f)))
668 (lambda ()
669 (lambda-case
670 (((n) #f #f #f () (_))
671 (primcall vector-set!
672 (lexical v _) (lexical n _) (lexical n _)))))))
673
674 (pass-if-peval
675 ;; Mutable lexical is not propagated.
676 (let ((v (vector 1 2 3)))
677 (lambda ()
678 v))
679 (let (v) (_)
680 ((primcall vector (const 1) (const 2) (const 3)))
681 (lambda ()
682 (lambda-case
683 ((() #f #f #f () ())
684 (lexical v _))))))
685
686 (pass-if-peval
687 ;; Lexical that is not provably pure is not inlined nor propagated.
688 (let* ((x (if (> p q) (frob!) (display 'chbouib)))
689 (y (* x 2)))
690 (+ x x y))
691 (let (x) (_) ((if (primcall > (toplevel p) (toplevel q))
692 (call (toplevel frob!))
693 (call (toplevel display) (const chbouib))))
694 (let (y) (_) ((primcall * (lexical x _) (const 2)))
695 (primcall +
696 (primcall + (lexical x _) (lexical x _))
697 (lexical y _)))))
698
699 (pass-if-peval
700 ;; Non-constant arguments not propagated to lambdas.
701 ((lambda (x y z)
702 (vector-set! x 0 0)
703 (set-car! y 0)
704 (set-cdr! z '()))
705 (vector 1 2 3)
706 (make-list 10)
707 (list 1 2 3))
708 (let (x y z) (_ _ _)
709 ((primcall vector (const 1) (const 2) (const 3))
710 (call (toplevel make-list) (const 10))
711 (primcall list (const 1) (const 2) (const 3)))
712 (seq
713 (primcall vector-set!
714 (lexical x _) (const 0) (const 0))
715 (seq (primcall set-car!
716 (lexical y _) (const 0))
717 (primcall set-cdr!
718 (lexical z _) (const ()))))))
719
720 (pass-if-peval
721 (let ((foo top-foo) (bar top-bar))
722 (let* ((g (lambda (x y) (+ x y)))
723 (f (lambda (g x) (g x x))))
724 (+ (f g foo) (f g bar))))
725 (let (foo bar) (_ _) ((toplevel top-foo) (toplevel top-bar))
726 (primcall +
727 (primcall + (lexical foo _) (lexical foo _))
728 (primcall + (lexical bar _) (lexical bar _)))))
729
730 (pass-if-peval
731 ;; Fresh objects are not turned into constants, nor are constants
732 ;; turned into fresh objects.
733 (let* ((c '(2 3))
734 (x (cons 1 c))
735 (y (cons 0 x)))
736 y)
737 (let (x) (_) ((primcall cons (const 1) (const (2 3))))
738 (primcall cons (const 0) (lexical x _))))
739
740 (pass-if-peval
741 ;; Bindings mutated.
742 (let ((x 2))
743 (set! x 3)
744 x)
745 (let (x) (_) ((const 2))
746 (seq
747 (set! (lexical x _) (const 3))
748 (lexical x _))))
749
750 (pass-if-peval
751 ;; Bindings mutated.
752 (letrec ((x 0)
753 (f (lambda ()
754 (set! x (+ 1 x))
755 x)))
756 (frob f) ; may mutate `x'
757 x)
758 (let (x) (_) ((const 0))
759 (seq
760 (call (toplevel frob) (lambda _ _))
761 (lexical x _))))
762
763 (pass-if-peval
764 ;; Bindings mutated.
765 (letrec ((f (lambda (x)
766 (set! f (lambda (_) x))
767 x)))
768 (f 2))
769 (let (f) (_) ((void)) (seq _ (call . _))))
770
771 (pass-if-peval
772 ;; Bindings possibly mutated.
773 (let ((x (make-foo)))
774 (frob! x) ; may mutate `x'
775 x)
776 (let (x) (_) ((call (toplevel make-foo)))
777 (seq
778 (call (toplevel frob!) (lexical x _))
779 (lexical x _))))
780
781 (pass-if-peval
782 ;; Inlining stops at recursive calls with dynamic arguments.
783 (let loop ((x x))
784 (if (< x 0) x (loop (1- x))))
785 (fix (loop) (_) ((lambda (_)
786 (lambda-case
787 (((x) #f #f #f () (_))
788 (if _ _
789 (call (lexical loop _)
790 (primcall - (lexical x _)
791 (const 1))))))))
792 (call (lexical loop _) (toplevel x))))
793
794 (pass-if-peval
795 ;; Recursion on the 2nd argument is fully evaluated.
796 (let ((x (top)))
797 (let loop ((x x) (y 10))
798 (if (> y 0)
799 (loop x (1- y))
800 (foo x y))))
801 (let (x) (_) ((call (toplevel top)))
802 (call (toplevel foo) (lexical x _) (const 0))))
803
804 (pass-if-peval
805 ;; Inlining aborted when residual code contains recursive calls.
806 ;;
807 ;; <http://debbugs.gnu.org/9542>
808 (let loop ((x x) (y 0))
809 (if (> y 0)
810 (loop (1- x) (1- y))
811 (if (< x 0)
812 x
813 (loop (1+ x) (1+ y)))))
814 (fix (loop) (_) ((lambda (_)
815 (lambda-case
816 (((x y) #f #f #f () (_ _))
817 (if (primcall >
818 (lexical y _) (const 0))
819 _ _)))))
820 (call (lexical loop _) (toplevel x) (const 0))))
821
822 (pass-if-peval
823 ;; Infinite recursion: `peval' can inline some but eventually gives up.
824 (letrec ((f (lambda (x) (g (1- x))))
825 (g (lambda (x) (h (1+ x))))
826 (h (lambda (x) (f x))))
827 (f 0))
828 (fix (f) (_) (_) (call . _)))
829
830 (pass-if-peval
831 ;; Infinite recursion: all the arguments to `loop' are static, but
832 ;; unrolling it would lead `peval' to enter an infinite loop.
833 (let loop ((x 0))
834 (and (< x top)
835 (loop (1+ x))))
836 (fix (loop) (_) ((lambda . _))
837 (call (lexical loop _) (const 0))))
838
839 (pass-if-peval
840 ;; This test checks that the `start' binding is indeed residualized.
841 ;; See the `referenced?' procedure in peval's `prune-bindings'.
842 (let ((pos 0))
843 (let ((here (let ((start pos)) (lambda () start))))
844 (set! pos 1) ;; Cause references to `pos' to residualize.
845 (here)))
846 (let (pos) (_) ((const 0))
847 (let (here) (_) (_)
848 (seq
849 (set! (lexical pos _) (const 1))
850 (call (lexical here _))))))
851
852 (pass-if-peval
853 ;; FIXME: Signal an error?
854 (letrec ((a a))
855 1)
856 (let (a) (_) ((void)) (seq (set! . _) (const 1))))
857
858 (pass-if-peval
859 ;; This is a fun one for peval to handle.
860 (letrec ((a a))
861 a)
862 (let (a) (_) ((void)) (seq (set! . _) (lexical a _))))
863
864 (pass-if-peval
865 ;; Another interesting recursive case.
866 (letrec ((a b) (b a))
867 a)
868 (let (a b) (_ _) ((void) (void))
869 (seq (set! . _) (seq (set! . _) (lexical a _)))))
870
871 (pass-if-peval
872 ;; Another pruning case, that `a' is residualized.
873 (letrec ((a (lambda () (a)))
874 (b (lambda () (a)))
875 (c (lambda (x) x)))
876 (let ((d (foo b)))
877 (c d)))
878
879 ;; "b c a" is the current order that we get with unordered letrec,
880 ;; but it's not important to this test, so if it changes, just adapt
881 ;; the test.
882 (fix (a) (_)
883 ((lambda _
884 (lambda-case
885 ((() #f #f #f () ())
886 (call (lexical a _))))))
887 (fix (b) (_)
888 ((lambda _
889 (lambda-case
890 ((() #f #f #f () ())
891 (call (lexical a _))))))
892 (call (toplevel foo) (lexical b _)))))
893
894 (pass-if-peval
895 ;; In this case, we can prune the bindings. `a' ends up being copied
896 ;; because it is only referenced once in the source program. Oh
897 ;; well.
898 (letrec* ((a (lambda (x) (top x)))
899 (b (lambda () a)))
900 (foo (b) (b)))
901 (call (toplevel foo)
902 (lambda _
903 (lambda-case
904 (((x) #f #f #f () (_))
905 (call (toplevel top) (lexical x _)))))
906 (lambda _
907 (lambda-case
908 (((x) #f #f #f () (_))
909 (call (toplevel top) (lexical x _)))))))
910
911 (pass-if-peval
912 ;; The inliner sees through a `let'.
913 ((let ((a 10)) (lambda (b) (* b 2))) 30)
914 (const 60))
915
916 (pass-if-peval
917 ((lambda ()
918 (define (const x) (lambda (_) x))
919 (let ((v #f))
920 ((const #t) v))))
921 (const #t))
922
923 (pass-if-peval
924 ;; Applications of procedures with rest arguments can get inlined.
925 ((lambda (x y . z)
926 (list x y z))
927 1 2 3 4)
928 (let (z) (_) ((primcall list (const 3) (const 4)))
929 (primcall list (const 1) (const 2) (lexical z _))))
930
931 (pass-if-peval
932 ;; Unmutated lists can get inlined.
933 (let ((args (list 2 3)))
934 (apply (lambda (x y z w)
935 (list x y z w))
936 0 1 args))
937 (primcall list (const 0) (const 1) (const 2) (const 3)))
938
939 (pass-if-peval
940 ;; However if the list might have been mutated, it doesn't propagate.
941 (let ((args (list 2 3)))
942 (foo! args)
943 (apply (lambda (x y z w)
944 (list x y z w))
945 0 1 args))
946 (let (args) (_) ((primcall list (const 2) (const 3)))
947 (seq
948 (call (toplevel foo!) (lexical args _))
949 (primcall apply
950 (lambda ()
951 (lambda-case
952 (((x y z w) #f #f #f () (_ _ _ _))
953 (primcall list
954 (lexical x _) (lexical y _)
955 (lexical z _) (lexical w _)))))
956 (const 0)
957 (const 1)
958 (lexical args _)))))
959
960 (pass-if-peval
961 ;; Here the `args' that gets built by the application of the lambda
962 ;; takes more than effort "10" to visit. Test that we fall back to
963 ;; the source expression of the operand, which is still a call to
964 ;; `list', so the inlining still happens.
965 (lambda (bv offset n)
966 (let ((x (bytevector-ieee-single-native-ref
967 bv
968 (+ offset 0)))
969 (y (bytevector-ieee-single-native-ref
970 bv
971 (+ offset 4))))
972 (let ((args (list x y)))
973 (apply
974 (lambda (bv offset x y)
975 (bytevector-ieee-single-native-set!
976 bv
977 (+ offset 0)
978 x)
979 (bytevector-ieee-single-native-set!
980 bv
981 (+ offset 4)
982 y))
983 bv
984 offset
985 args))))
986 (lambda ()
987 (lambda-case
988 (((bv offset n) #f #f #f () (_ _ _))
989 (let (x y) (_ _) ((primcall bytevector-ieee-single-native-ref
990 (lexical bv _)
991 (primcall +
992 (lexical offset _) (const 0)))
993 (primcall bytevector-ieee-single-native-ref
994 (lexical bv _)
995 (primcall +
996 (lexical offset _) (const 4))))
997 (seq
998 (primcall bytevector-ieee-single-native-set!
999 (lexical bv _)
1000 (primcall +
1001 (lexical offset _) (const 0))
1002 (lexical x _))
1003 (primcall bytevector-ieee-single-native-set!
1004 (lexical bv _)
1005 (primcall +
1006 (lexical offset _) (const 4))
1007 (lexical y _))))))))
1008
1009 (pass-if-peval
1010 ;; Here we ensure that non-constant expressions are not copied.
1011 (lambda ()
1012 (let ((args (list (foo!))))
1013 (apply
1014 (lambda (z x)
1015 (list z x))
1016 ;; This toplevel ref might raise an unbound variable exception.
1017 ;; The effects of `(foo!)' must be visible before this effect.
1018 z
1019 args)))
1020 (lambda ()
1021 (lambda-case
1022 ((() #f #f #f () ())
1023 (let (_) (_) ((call (toplevel foo!)))
1024 (let (z) (_) ((toplevel z))
1025 (primcall 'list
1026 (lexical z _)
1027 (lexical _ _))))))))
1028
1029 (pass-if-peval
1030 ;; Rest args referenced more than once are not destructured.
1031 (lambda ()
1032 (let ((args (list 'foo)))
1033 (set-car! args 'bar)
1034 (apply
1035 (lambda (z x)
1036 (list z x))
1037 z
1038 args)))
1039 (lambda ()
1040 (lambda-case
1041 ((() #f #f #f () ())
1042 (let (args) (_)
1043 ((primcall list (const foo)))
1044 (seq
1045 (primcall set-car! (lexical args _) (const bar))
1046 (primcall apply
1047 (lambda . _)
1048 (toplevel z)
1049 (lexical args _))))))))
1050
1051 (pass-if-peval
1052 ;; Let-values inlining, even with consumers with rest args.
1053 (call-with-values (lambda () (values 1 2))
1054 (lambda args
1055 (apply list args)))
1056 (primcall list (const 1) (const 2)))
1057
1058 (pass-if-peval
1059 ;; When we can't inline let-values but can prove that the producer
1060 ;; has just one value, reduce to "let" (which can then fold
1061 ;; further).
1062 (call-with-values (lambda () (if foo 1 2))
1063 (lambda args
1064 (apply values args)))
1065 (if (toplevel foo) (const 1) (const 2)))
1066
1067 (pass-if-peval
1068 ;; Constant folding: cons of #nil does not make list
1069 (cons 1 #nil)
1070 (primcall cons (const 1) (const '#nil)))
1071
1072 (pass-if-peval
1073 ;; Constant folding: cons
1074 (begin (cons 1 2) #f)
1075 (const #f))
1076
1077 (pass-if-peval
1078 ;; Constant folding: cons
1079 (begin (cons (foo) 2) #f)
1080 (seq (call (toplevel foo)) (const #f)))
1081
1082 (pass-if-peval
1083 ;; Constant folding: cons
1084 (if (cons 0 0) 1 2)
1085 (const 1))
1086
1087 (pass-if-peval
1088 ;; Constant folding: car+cons
1089 (car (cons 1 0))
1090 (const 1))
1091
1092 (pass-if-peval
1093 ;; Constant folding: cdr+cons
1094 (cdr (cons 1 0))
1095 (const 0))
1096
1097 (pass-if-peval
1098 ;; Constant folding: car+cons, impure
1099 (car (cons 1 (bar)))
1100 (seq (call (toplevel bar)) (const 1)))
1101
1102 (pass-if-peval
1103 ;; Constant folding: cdr+cons, impure
1104 (cdr (cons (bar) 0))
1105 (seq (call (toplevel bar)) (const 0)))
1106
1107 (pass-if-peval
1108 ;; Constant folding: car+list
1109 (car (list 1 0))
1110 (const 1))
1111
1112 (pass-if-peval
1113 ;; Constant folding: cdr+list
1114 (cdr (list 1 0))
1115 (primcall list (const 0)))
1116
1117 (pass-if-peval
1118 ;; Constant folding: car+list, impure
1119 (car (list 1 (bar)))
1120 (seq (call (toplevel bar)) (const 1)))
1121
1122 (pass-if-peval
1123 ;; Constant folding: cdr+list, impure
1124 (cdr (list (bar) 0))
1125 (seq (call (toplevel bar)) (primcall list (const 0))))
1126
1127 (pass-if-peval
1128 ;; Equality primitive: same lexical
1129 (let ((x (random))) (eq? x x))
1130 (seq (call (toplevel random)) (const #t)))
1131
1132 (pass-if-peval
1133 ;; Equality primitive: merge lexical identities
1134 (let* ((x (random)) (y x)) (eq? x y))
1135 (seq (call (toplevel random)) (const #t)))
1136
1137 (pass-if-peval
1138 ;; Non-constant guards get lexical bindings, invocation of winder and
1139 ;; unwinder lifted out. Unfortunately both have the generic variable
1140 ;; name "tmp", so we can't distinguish them in this test, and they
1141 ;; also collide in generic names with the single-value result from
1142 ;; the dynwind; alack.
1143 (dynamic-wind foo (lambda () bar) baz)
1144 (let (tmp tmp) (_ _) ((toplevel foo) (toplevel baz))
1145 (seq (seq (if (primcall thunk? (lexical tmp _))
1146 (call (lexical tmp _))
1147 (primcall raise-type-error . _))
1148 (primcall wind (lexical tmp _) (lexical tmp _)))
1149 (let (tmp) (_) ((toplevel bar))
1150 (seq (seq (primcall unwind)
1151 (call (lexical tmp _)))
1152 (lexical tmp _))))))
1153
1154 (pass-if-peval
1155 ;; Constant guards don't need lexical bindings or thunk? checks.
1156 (dynamic-wind (lambda () foo) (lambda () bar) (lambda () baz))
1157 (seq (seq (toplevel foo)
1158 (primcall wind
1159 (lambda ()
1160 (lambda-case
1161 ((() #f #f #f () ()) (toplevel foo))))
1162 (lambda ()
1163 (lambda-case
1164 ((() #f #f #f () ()) (toplevel baz))))))
1165 (let (tmp) (_) ((toplevel bar))
1166 (seq (seq (primcall unwind)
1167 (toplevel baz))
1168 (lexical tmp _)))))
1169
1170 (pass-if-peval
1171 ;; Dynwind bodies that return an unknown number of values need a
1172 ;; let-values.
1173 (dynamic-wind (lambda () foo) (lambda () (bar)) (lambda () baz))
1174 (seq (seq (toplevel foo)
1175 (primcall wind
1176 (lambda ()
1177 (lambda-case
1178 ((() #f #f #f () ()) (toplevel foo))))
1179 (lambda ()
1180 (lambda-case
1181 ((() #f #f #f () ()) (toplevel baz))))))
1182 (let-values (call (toplevel bar))
1183 (lambda-case
1184 ((() #f vals #f () (_))
1185 (seq (seq (primcall unwind)
1186 (toplevel baz))
1187 (primcall apply (primitive values) (lexical vals _))))))))
1188
1189 (pass-if-peval
1190 ;; Prompt is removed if tag is unreferenced
1191 (let ((tag (make-prompt-tag)))
1192 (call-with-prompt tag
1193 (lambda () 1)
1194 (lambda args args)))
1195 (const 1))
1196
1197 (pass-if-peval
1198 ;; Prompt is removed if tag is unreferenced, with explicit stem
1199 (let ((tag (make-prompt-tag "foo")))
1200 (call-with-prompt tag
1201 (lambda () 1)
1202 (lambda args args)))
1203 (const 1))
1204
1205 ;; Handler lambda inlined
1206 (pass-if-peval
1207 (call-with-prompt tag
1208 (lambda () 1)
1209 (lambda (k x) x))
1210 (prompt #t
1211 (toplevel tag)
1212 (const 1)
1213 (lambda _
1214 (lambda-case
1215 (((k x) #f #f #f () (_ _))
1216 (lexical x _))))))
1217
1218 ;; Handler toplevel not inlined
1219 (pass-if-peval
1220 (call-with-prompt tag
1221 (lambda () 1)
1222 handler)
1223 (let (handler) (_) ((toplevel handler))
1224 (if (primcall procedure? (lexical handler _))
1225 (prompt #f
1226 (toplevel tag)
1227 (lambda _
1228 (lambda-case
1229 ((() #f #f #f () ())
1230 (const 1))))
1231 (lambda _
1232 (lambda-case
1233 ((() #f args #f () (_))
1234 (primcall apply
1235 (lexical handler _)
1236 (lexical args _))))))
1237 (primcall raise-type-error . _))))
1238
1239 (pass-if-peval
1240 ;; `while' without `break' or `continue' has no prompts and gets its
1241 ;; condition folded. Unfortunately the outer `lp' does not yet get
1242 ;; elided, and the continuation tag stays around. (The continue tag
1243 ;; stays around because although it is not referenced, recursively
1244 ;; visiting the loop in the continue handler manages to visit the tag
1245 ;; twice before aborting. The abort doesn't unroll the recursive
1246 ;; reference.)
1247 (while #t #t)
1248 (let (_) (_) ((primcall make-prompt-tag . _))
1249 (fix (lp) (_)
1250 ((lambda _
1251 (lambda-case
1252 ((() #f #f #f () ())
1253 (fix (loop) (_)
1254 ((lambda _
1255 (lambda-case
1256 ((() #f #f #f () ())
1257 (call (lexical loop _))))))
1258 (call (lexical loop _)))))))
1259 (call (lexical lp _)))))
1260
1261 (pass-if-peval
1262 (lambda (a . rest)
1263 (apply (lambda (x y) (+ x y))
1264 a rest))
1265 (lambda _
1266 (lambda-case
1267 (((x y) #f #f #f () (_ _))
1268 _))))
1269
1270 (pass-if-peval
1271 (car '(1 2))
1272 (const 1))
1273
1274 ;; If we bail out when inlining an identifier because it's too big,
1275 ;; but the identifier simply aliases some other identifier, then avoid
1276 ;; residualizing a reference to the leaf identifier. The bailout is
1277 ;; driven by the recursive-effort-limit, which is currently 100. We
1278 ;; make sure to trip it with this recursive sum thing.
1279 (pass-if-peval
1280 (let ((x (let sum ((n 0) (out 0))
1281 (if (< n 10000)
1282 (sum (1+ n) (+ out n))
1283 out))))
1284 ((lambda (y) (list y)) x))
1285 (let (x) (_) (_)
1286 (primcall list (lexical x _))))
1287
1288 ;; Here we test that a common test in a chain of ifs gets lifted.
1289 (pass-if-peval
1290 (if (and (struct? x) (eq? (struct-vtable x) A))
1291 (foo x)
1292 (if (and (struct? x) (eq? (struct-vtable x) B))
1293 (bar x)
1294 (if (and (struct? x) (eq? (struct-vtable x) C))
1295 (baz x)
1296 (qux x))))
1297 (let (failure) (_) ((lambda _
1298 (lambda-case
1299 ((() #f #f #f () ())
1300 (call (toplevel qux) (toplevel x))))))
1301 (if (primcall struct? (toplevel x))
1302 (if (primcall eq?
1303 (primcall struct-vtable (toplevel x))
1304 (toplevel A))
1305 (call (toplevel foo) (toplevel x))
1306 (if (primcall eq?
1307 (primcall struct-vtable (toplevel x))
1308 (toplevel B))
1309 (call (toplevel bar) (toplevel x))
1310 (if (primcall eq?
1311 (primcall struct-vtable (toplevel x))
1312 (toplevel C))
1313 (call (toplevel baz) (toplevel x))
1314 (call (lexical failure _)))))
1315 (call (lexical failure _)))))
1316
1317 ;; Multiple common tests should get lifted as well.
1318 (pass-if-peval
1319 (if (and (struct? x) (eq? (struct-vtable x) A) B)
1320 (foo x)
1321 (if (and (struct? x) (eq? (struct-vtable x) A) C)
1322 (bar x)
1323 (if (and (struct? x) (eq? (struct-vtable x) A) D)
1324 (baz x)
1325 (qux x))))
1326 (let (failure) (_) ((lambda _
1327 (lambda-case
1328 ((() #f #f #f () ())
1329 (call (toplevel qux) (toplevel x))))))
1330 (if (primcall struct? (toplevel x))
1331 (if (primcall eq?
1332 (primcall struct-vtable (toplevel x))
1333 (toplevel A))
1334 (if (toplevel B)
1335 (call (toplevel foo) (toplevel x))
1336 (if (toplevel C)
1337 (call (toplevel bar) (toplevel x))
1338 (if (toplevel D)
1339 (call (toplevel baz) (toplevel x))
1340 (call (lexical failure _)))))
1341 (call (lexical failure _)))
1342 (call (lexical failure _)))))
1343
1344 (pass-if-peval
1345 (apply (lambda (x y) (cons x y)) '(1 2))
1346 (primcall cons (const 1) (const 2)))
1347
1348 (pass-if-peval
1349 (apply (lambda (x y) (cons x y)) (list 1 2))
1350 (primcall cons (const 1) (const 2)))
1351
1352 (pass-if-peval
1353 (apply = (list 0 0 0))
1354 (const #t))
1355
1356 (pass-if-peval
1357 (apply char<? (list #\a #\b #\c))
1358 (const #t))
1359
1360 ;; Disable after removal of abort-in-tail-position optimization, in
1361 ;; hopes that CPS does a uniformly better job.
1362 #;
1363 (pass-if-peval
1364 (let ((t (make-prompt-tag)))
1365 (call-with-prompt t
1366 (lambda () (abort-to-prompt t 1 2 3))
1367 (lambda (k x y z) (list x y z))))
1368 (primcall list (const 1) (const 2) (const 3)))
1369
1370 (pass-if-peval
1371 (call-with-values foo (lambda (x) (bar x)))
1372 (let-values (call (toplevel foo))
1373 (lambda-case
1374 (((x) #f #f #f () (_))
1375 (call (toplevel bar) (lexical x _))))))
1376
1377 (pass-if-peval
1378 (eq? '(a b) '(a b))
1379 (const #t))
1380
1381 (pass-if-peval
1382 (eqv? '(a b) '(a b))
1383 (const #t))
1384
1385 (pass-if-peval
1386 ((lambda (foo)
1387 (define* (bar a #:optional (b (1+ a)))
1388 (list a b))
1389 (bar 1))
1390 1)
1391 (primcall list (const 1) (const 2)))
1392
1393 (pass-if-peval
1394 ;; Should not inline tail list to apply if it is mutable.
1395 ;; <http://debbugs.gnu.org/15533>
1396 (let ((l '()))
1397 (if (pair? arg)
1398 (set! l arg))
1399 (apply f l))
1400 (let (l) (_) ((const ()))
1401 (seq
1402 (if (primcall pair? (toplevel arg))
1403 (set! (lexical l _) (toplevel arg))
1404 (void))
1405 (primcall apply (toplevel f) (lexical l _)))))
1406
1407 (pass-if-peval
1408 (lambda (f x)
1409 (let lp ((x x))
1410 (let ((x* (f x)))
1411 (if (eq? x x*) x* (lp x*)))))
1412 (lambda ()
1413 (lambda-case
1414 (((f x) #f #f #f () (_ _))
1415 (fix (lp)
1416 (_)
1417 ((lambda ((name . lp))
1418 (lambda-case
1419 (((x) #f #f #f () (_))
1420 (let (x*)
1421 (_)
1422 ((call (lexical f _) (lexical x _)))
1423 (if (primcall
1424 eq?
1425 (lexical x _)
1426 (lexical x* _))
1427 (lexical x* _)
1428 (call (lexical lp _)
1429 (lexical x* _))))))))
1430 (call (lexical lp _)
1431 (lexical x _)))))))
1432
1433 (pass-if-peval
1434 (lambda ()
1435 (define (add1 n) (+ 1 n))
1436 (add1 1 2))
1437 (lambda ()
1438 (lambda-case
1439 ((() #f #f #f () ())
1440 (fix (add1)
1441 (_)
1442 ((lambda ((name . add1))
1443 (lambda-case
1444 (((n) #f #f #f () (_))
1445 (primcall + (const 1) (lexical n _))))))
1446 (call (lexical add1 _)
1447 (const 1)
1448 (const 2)))))))
1449
1450 (pass-if-peval (make-vector 123 x)
1451 (primcall make-vector (const 123) (toplevel x)))
1452
1453 (pass-if-peval (make-vector)
1454 ;; This used to trigger an infinite loop between
1455 ;; 'resolve-primitives' and 'expand-primcall':
1456 ;; <https://bugs.gnu.org/60522>.
1457 (primcall make-vector)))
1458
1459 (with-test-prefix "case-lambda"
1460 ;; one case
1461 (pass-if-peval
1462 ((case-lambda (() 0)))
1463 (const 0))
1464
1465 ;; middle
1466 (pass-if-peval
1467 ((case-lambda (() 0) ((a b) 1) ((a) 2)) 1 2)
1468 (const 1))
1469
1470 ;; last
1471 (pass-if-peval
1472 ((case-lambda ((a b) 0) ((a) 1) (() 2)))
1473 (const 2))
1474
1475 ;; first
1476 (pass-if-peval
1477 ((case-lambda ((a) 0) (() 1) ((a b) 2)) 1)
1478 (const 0))
1479
1480 ;; rest arg
1481 (pass-if-peval
1482 ((case-lambda (args 0) ((a b) 1) ((a) 2)) 1 2)
1483 (const 0))
1484
1485 ;; req before rest I
1486 (pass-if-peval
1487 ((case-lambda ((a b) 0) (args 1) ((a) 1)) 1 2)
1488 (const 0))
1489
1490 ;; req before rest II
1491 (pass-if-peval
1492 ((case-lambda ((a) 0) (args 1) ((a b) 2)) 1 2)
1493 (const 1))
1494
1495 ;; optional
1496 (pass-if-peval
1497 ((case-lambda* ((a #:optional x) 0) (args 1) ((a) 2)) 1 2)
1498 (const 0))
1499
1500 ;; optional and rest, no match I
1501 (pass-if-peval
1502 ((case-lambda* ((a #:optional x . rest) 0) (args 1) ((a) 2)))
1503 (const 1))
1504
1505 ;; optional and rest, match I
1506 (pass-if-peval
1507 ((case-lambda* (() 0) ((a #:optional x . rest) 1) ((a) 2)) 1)
1508 (const 1))
1509
1510 ;; optional and rest, match II
1511 (pass-if-peval
1512 ((case-lambda* ((a #:optional x . rest) 0) (args 1) ((a) 2)) 1)
1513 (const 0))
1514
1515 ;; optional and rest, match III
1516 (pass-if-peval
1517 ((case-lambda* ((a #:optional x . rest) 0) (args 1) ((a) 2)) 1 2)
1518 (const 0))
1519
1520 ;; optional and rest, match IV
1521 (pass-if-peval
1522 ((case-lambda* ((a #:optional x . rest) 0) (args 1) ((a) 2)) 1 2 3)
1523 (const 0))
1524
1525 ;; keyword cases survive
1526 (pass-if-peval ((case-lambda* ((a b) 0) ((a #:key x) 1)) 0 #:x 1)
1527 (const 1))
1528 (pass-if-peval ((case-lambda* ((a b c) 0) ((a #:key x) 1)) 0 #:x 1)
1529 (const 0))
1530 (pass-if-peval ((case-lambda* ((a #:key x) 0) ((a b) 0)) 0 #:x 1)
1531 (const 0))
1532 (pass-if-peval ((case-lambda* ((a #:key x) 0) ((a b c) 1)) 0 1 2)
1533 (const 1)))
1534
1535 (with-test-prefix "eqv?"
1536 (pass-if-peval (eqv? x #f)
1537 (primcall eq? (toplevel x) (const #f)))
1538
1539 (pass-if-peval (eqv? x '())
1540 (primcall eq? (toplevel x) (const ())))
1541
1542 (pass-if-peval (eqv? x #t)
1543 (primcall eq? (toplevel x) (const #t)))
1544
1545 (pass-if-peval (eqv? x 'sym)
1546 (primcall eq? (toplevel x) (const sym)))
1547
1548 (pass-if-peval (eqv? x 42)
1549 (primcall eq? (toplevel x) (const 42)))
1550
1551 (pass-if-peval (eqv? x #\a)
1552 (primcall eq? (toplevel x) (const #\a)))
1553
1554 (pass-if-peval (eqv? x 42.0)
1555 (primcall eqv? (toplevel x) (const '42.0)))
1556
1557 (pass-if-peval (eqv? x #nil)
1558 (primcall eq? (toplevel x) (const #nil)))
1559
1560 (pass-if-peval (eqv? x '(a . b))
1561 (primcall eq? (toplevel x) (const (a . b)))))
1562
1563 (with-test-prefix "equal?"
1564 (pass-if-peval (equal? x #f)
1565 (primcall eq? (toplevel x) (const #f)))
1566
1567 (pass-if-peval (equal? x '())
1568 (primcall eq? (toplevel x) (const ())))
1569
1570 (pass-if-peval (equal? x #t)
1571 (primcall eq? (toplevel x) (const #t)))
1572
1573 (pass-if-peval (equal? x 'sym)
1574 (primcall eq? (toplevel x) (const sym)))
1575
1576 (pass-if-peval (equal? x 42)
1577 (primcall eq? (toplevel x) (const 42)))
1578
1579 (pass-if-peval (equal? x #\a)
1580 (primcall eq? (toplevel x) (const #\a)))
1581
1582 (pass-if-peval (equal? x 42.0)
1583 (primcall eqv? (toplevel x) (const '42.0)))
1584
1585 (pass-if-peval (equal? x #nil)
1586 (primcall eq? (toplevel x) (const #nil)))
1587
1588 (pass-if-peval (equal? x '(a . b))
1589 (primcall equal? (toplevel x) (const (a . b)))))
1590
1591 (with-test-prefix "values"
1592 (pass-if-peval (begin (cons 1 (values)) #f)
1593 (seq (primcall values (primcall values))
1594 (const #f)))
1595
1596 (pass-if-peval (begin 1 (values) #f)
1597 (const #f)))
1598
1599 (with-test-prefix "append"
1600 (pass-if-peval (append '() 42)
1601 (const 42))
1602
1603 (pass-if-peval (append '(1 2) 42)
1604 (primcall cons (const 1)
1605 (primcall cons (const 2) (const 42))))
1606
1607 (pass-if-peval (append (list 1 2) 42)
1608 (primcall cons (const 1)
1609 (primcall cons (const 2) (const 42))))
1610
1611 (pass-if-peval (append (cons* 1 2 '()) 42)
1612 (primcall cons (const 1)
1613 (primcall cons (const 2) (const 42))))
1614
1615 (pass-if-peval (append (cons 1 2) 42)
1616 (primcall cons (const 1)
1617 (primcall append (const 2) (const 42)))))