compiler.test 14 KB

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