inlining-test.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. (module-static #t)
  2. (define-namespace Repl "class:kawa.repl")
  3. (define-namespace Repl2 <kawa.repl>)
  4. (define (set-home1 (x :: <String>)) (set! (<kawa.repl>:.homeDirectory) x))
  5. (define (set-home2 (x :: <String>)) (set! <kawa.repl>:homeDirectory x))
  6. (define (set-home3 (x :: <String>)) (set! (Repl:.homeDirectory) x))
  7. (define (set-home4 (x :: <String>)) (set! Repl:homeDirectory x))
  8. (define (set-home5 (x :: <String>)) (set! (kawa.repl:.homeDirectory) x))
  9. (define (set-home6 (x :: <String>)) (set! kawa.repl:homeDirectory x))
  10. (define (set-home7 (x :: <String>)) (set! (Repl2:.homeDirectory) x))
  11. (define (set-home8 (x :: <String>)) (set! Repl2:homeDirectory x))
  12. (define (get-home1) (<kawa.repl>:.homeDirectory))
  13. (define (get-home2) <kawa.repl>:homeDirectory)
  14. (define (get-home3) (Repl:.homeDirectory))
  15. (define (get-home4) Repl:homeDirectory)
  16. (define (get-home5) (kawa.repl:.homeDirectory))
  17. (define (get-home6) kawa.repl:homeDirectory)
  18. (define (get-home7) (Repl2:.homeDirectory))
  19. (define (get-home8) Repl2:homeDirectory)
  20. (define-namespace Pair1 <pair>)
  21. (define-namespace Pair2 <gnu.lists.Pair>)
  22. (define-namespace Pair3 "class:gnu.lists.Pair")
  23. (define (set-car1 (p ::<pair>) x) (set! (*:.car p) x))
  24. (define (set-car2 (p ::<pair>) x) (set! (<pair>:.car p) x))
  25. (define (set-car3 (p ::<pair>) x) (set! (gnu.lists.Pair:.car p) x))
  26. (define (set-car4 (p ::<pair>) x) (set! (<gnu.lists.Pair>:.car p) x))
  27. (define (set-car5 (p ::<pair>) x) (set! (Pair1:.car p) x))
  28. (define (set-car6 (p ::<pair>) x) (set! (Pair2:.car p) x))
  29. (define (set-car7 (p ::<pair>) x) (set! (Pair3:.car p) x))
  30. (define (set-car8 (p ::<pair>) x) (set! p:car x))
  31. (define (set-car9 p x) (set! (Pair1:.car p) x))
  32. (define (get-car1 (p ::<pair>)) (*:.car p))
  33. (define (get-car2 (p ::<pair>)) (<pair>:.car p))
  34. (define (get-car3 (p ::<pair>)) (gnu.lists.Pair:.car p))
  35. (define (get-car4 (p ::<pair>)) (<gnu.lists.Pair>:.car p))
  36. (define (get-car5 (p ::<pair>)) (Pair1:.car p))
  37. (define (get-car6 (p ::<pair>)) (Pair2:.car p))
  38. (define (get-car7 (p ::<pair>)) (Pair3:.car p))
  39. (define (get-car8 (p ::<pair>)) p:car)
  40. (define (get-car9 p) (Pair3:.car p))
  41. (define (get-car10 p) (<pair>:.car p))
  42. (define (get-car11 p::pair) (car p))
  43. (define (is-pair1 x) (<pair>:instance? x))
  44. (define (is-pair2 x) (gnu.lists.Pair:instance? x))
  45. (define (is-pair3 x) (<gnu.lists.Pair>:instance? x))
  46. (define (is-pair4 x) (Pair1:instance? x))
  47. (define (is-pair5 x) (Pair2:instance? x))
  48. (define (is-pair6 x) (Pair3:instance? x))
  49. (define (is-pair7 x) (instance? x <pair>))
  50. (define (is-pair9 x) (instance? x <gnu.lists.Pair>))
  51. (define (is-pair10 x) (instance? x Pair1:<>))
  52. (define (is-pair11 x) (instance? x Pair2:<>))
  53. (define (is-pair12 x) (instance? x Pair3:<>))
  54. (define (is-pair13 x) (gnu.lists.Pair? x))
  55. (define (is-pair14 x) (Pair1? x))
  56. (define (is-pair15 x) (Pair2? x))
  57. (define (is-pair16 x) (Pair3? x))
  58. (define (cast-to-pair1 x) (<pair>:@ x))
  59. (define (cast-to-pair2 x) (->gnu.lists.Pair x))
  60. (define (cast-to-pair3 x) (<gnu.lists.Pair>:@ x))
  61. (define (cast-to-pair4 x) (Pair1:@ x))
  62. (define (cast-to-pair5 x) (Pair2:@ x))
  63. (define (cast-to-pair6 x) (->Pair3 x))
  64. (define (cast-to-pair7 x) (as <pair> x))
  65. (define (cast-to-pair9 x) (as <gnu.lists.Pair> x))
  66. (define (cast-to-pair10 x) (as Pair1:<> x))
  67. (define (cast-to-pair11 x) (as Pair2:<> x))
  68. (define (cast-to-pair12 x) (as Pair3:<> x))
  69. (define (new-pair1 x y) (<pair>:new x y))
  70. (define (new-pair2 x y) (gnu.lists.Pair:new x y))
  71. (define (new-pair3 x y) (<gnu.lists.Pair>:new x y))
  72. (define (new-pair4 x y) (Pair1:new x y))
  73. (define (new-pair5 x y) (Pair2:new x y))
  74. (define (new-pair6 x y) (Pair3:new x y))
  75. (define (new-pair7 x y) (make <pair> x y))
  76. (define (new-pair9 x y) (make <gnu.lists.Pair> x y))
  77. (define (new-pair10 x y) (make Pair1:<> x y))
  78. (define (new-pair11 x y) (make Pair2:<> x y))
  79. (define (new-pair12 x y) (make Pair3:<> x y))
  80. (define (is-empty1 (p::<pair>)) ;; OK
  81. (*:isEmpty p))
  82. (define (make-iarr1 (n :: <int>)) (make <int[]> size: n))
  83. (define (make-iarr2 (n :: <int>)) (<int[]> size: n))
  84. (define (make-iarr3 (n :: <int>)) (<int[]> size: n 3 4 5))
  85. (define (length-iarr1 (arr :: <int[]>)) :: <int>
  86. (field arr 'length))
  87. (define (length-iarr2(arr :: <int[]>)) :: <int>
  88. (*:.length arr))
  89. (define (length-iarr3 (arr :: <int[]>)) :: <int>
  90. arr:length)
  91. (define (get-iarr1 (arr :: <int[]>) (i :: <int>)) :: <int>
  92. (arr i))
  93. (define (set-iarr1 (arr :: <int[]>) (i :: <int>) (val :: <int>)) :: <void>
  94. (set! (arr i) val))
  95. #|
  96. (define (car1 (x :: <pair>)) ;; OK
  97. (*:.car x))
  98. (define (get-ns str)
  99. (<gnu.mapping.Namespace>:getInstance str))
  100. (define (xcarx (p <pair>))
  101. (p:isEmpty))
  102. ; (*:isEmpty p))
  103. ; (*:.car p))
  104. ;(define-alias xx #,(namespace "XX"))
  105. (define TWO xx:TWO)
  106. |#
  107. (define-simple-class <Int> ()
  108. (value :: <int>)
  109. ((toHex)
  110. (<java.lang.Integer>:toHexString value))
  111. ((toHex x) allocation: 'static
  112. (<java.lang.Integer>:toHexString x))
  113. ((toHex x) allocation: 'static
  114. (<java.lang.Integer>:toHexString x)))
  115. (define (tohex1 x)
  116. (<Int>:toHex x))
  117. (define (tohex2 (x :: <Int>))
  118. (invoke x 'toHex))
  119. (define (tohex3 (x :: <Int>))
  120. (x:toHex))
  121. (define (varargs1)
  122. (invoke gnu.math.IntNum 'getMethod "valueOf"
  123. java.lang.String java.lang.Integer:TYPE))
  124. (define (varargs2 (argtypes :: java.lang.Class[]))
  125. (invoke gnu.math.IntNum 'getMethod "valueOf" @argtypes))
  126. (define (varargs3 argtypes)
  127. (invoke gnu.math.IntNum 'getMethod "valueOf" @argtypes))
  128. (define (top-level-recurse1 x::pair)
  129. (set-car! x 123)
  130. (top-level-recurse1 x))
  131. (define (top-level-recurse2 a b)
  132. (top-level-recurse2 b a))
  133. (define-namespace xx "XX")
  134. (define xx:two 222)
  135. (define list-two (list 'xx:Two))
  136. (define (factoriali1 x :: int) :: int
  137. (if (< x 1) 1
  138. (* x (factoriali1 (- x 1)))))
  139. (define (factoriali2 x :: <int>) :: <int>
  140. (if (< x 1) 1
  141. (* x (factoriali2 (- x 1)))))
  142. (define (factoriall1 x :: long) :: long
  143. (if (< x 1) 1
  144. (* x (factoriall1 (- x 1)))))
  145. (define (factorialI1 x :: <integer>) :: <integer>
  146. (if (< x 1) 1
  147. (* x (factorialI1 (- x 1)))))
  148. (define (plus-lambda1) :: int
  149. ((lambda (x y) (+ x y)) 3 4))
  150. (define (first-negative (vals :: double[])) :: double
  151. (let ((count vals:length))
  152. (call-with-current-continuation
  153. (lambda (exit)
  154. (do ((i :: int 0 (+ i 1)))
  155. ((= i count)
  156. 0)
  157. (let ((x (vals i)))
  158. (if (< x 0)
  159. (exit x))))))))
  160. (define (inline-two-calls (x :: int)) :: int
  161. (define (f (w :: int)) (+ w 10))
  162. (if (> x 0)
  163. (let ((y1 (+ x 1)))
  164. (f y1))
  165. (let ((y2 (+ x 2)))
  166. (f y2))))
  167. (define (inline-two-functions x)
  168. (letrec ((f (lambda ()
  169. (if x (f) (g))))
  170. (g (lambda ()
  171. (if x (g) (f)))))
  172. (f)))
  173. (define (check-even (x :: int)) ::boolean
  174. (letrec ((even?
  175. (lambda ((n1 :: int))
  176. (if (= n1 0)
  177. #t
  178. (odd? (- n1 1)))))
  179. (odd?
  180. (lambda ((n2 :: int))
  181. (if (= n2 0)
  182. #f
  183. (even? (- n2 1))))))
  184. (even? x)))
  185. ;; Same as check-even, but without return-type specifier
  186. (define (check-even-unspec-return (x :: int))
  187. (letrec ((even?
  188. (lambda ((n1 :: int))
  189. (if (= n1 0)
  190. #t
  191. (odd? (- n1 1)))))
  192. (odd?
  193. (lambda ((n2 :: int))
  194. (if (= n2 0)
  195. #f
  196. (even? (- n2 1))))))
  197. (even? x)))
  198. (define (constant-propagation1)
  199. (define x :: int 6)
  200. (define x2 (* x 2))
  201. (+ x x2))
  202. (define (constant-propagation2)
  203. (let ((cont2 (lambda (j::int) (+ 10 j))))
  204. (cont2 3)))
  205. ;; FIXME constant-folding is not done as well as we'd like.
  206. ;; Partly caused by setting dval=null in InlineCalls:visitReferenceExp.
  207. ;; The other problem is we visit a called Lambda (here cont2) before
  208. ;; visiting the argument (here i). That also means we visit the
  209. ;; arguments without using the required parameter type.
  210. (define (constant-propagation3)
  211. (let* ((i::int 2)
  212. (cont2 (lambda (j::int) (+ i j))))
  213. (cont2 i)))
  214. (define (factorial-infer1 (x ::int))
  215. ;; The type of r should be inferred as integer.
  216. (define r 1)
  217. (do ((i ::int 1 (+ i 1)))
  218. ((> i x) r)
  219. (set! r (* r i))))
  220. ;; FUTURE - would like to infer type of r as integer
  221. (define (factorial-infer2 (x ::int))
  222. (do ((i ::int 1 (+ i 1)) (r 1 (* r i)))
  223. ((> i x) r)))
  224. (define (get-from-vector1 x::gnu.lists.FVector[java.lang.Integer] i::int)
  225. (x:get i))
  226. (define (get-from-vector2 x::gnu.lists.FVector[java.lang.Integer] i::int)
  227. (x i))
  228. (define (sum1 n::integer)
  229. (let loop ((i 0) (sum 0))
  230. (if (< i n)
  231. sum
  232. (loop (+ i 1) (+ i sum)))))
  233. (define (sum2 n::double) ::double
  234. (let loop ((i 0.0d0) (sum 0))
  235. (if (< i n)
  236. sum
  237. (loop (+ i 1) (+ i sum)))))
  238. (define (numcomp1 x y) ::int
  239. (if (< x y) 5 6))
  240. (define(numcomp2 x y) ::int
  241. (let ((b (<= x y)))
  242. (if b 4 5)))
  243. (define (numcomp3 x y z) ::int
  244. (if (> x y z) 3 2))
  245. (define (numcomp4 x y z) ::int
  246. (if (> x 10 y 5 z) 6 3))
  247. (define (numcomp5 x y z) ::int
  248. (let ((b (> x 10 y 5 z)))
  249. (if b 4 3)))
  250. (define (eqv1 x y)
  251. (eqv? y x))
  252. (define (raise1 x::int y)
  253. (if (< x 0) (raise y) (* x 2)))
  254. (define (read1 p::input-port) ::int
  255. (let ((ch (read-char p)))
  256. (cond ((eof-object? ch) 1)
  257. ((char=? ch #\space) 2)
  258. ((and (char-ci>=? ch #\A) (char-ci<=? ch #\Z)) 3)
  259. (else 4))))
  260. (define (handle-char ch::character)::void
  261. (format #t "{~w}" ch))
  262. (define (string-for-each1 str::string)::void
  263. (string-for-each (lambda (x) (if (char>? x #\Space) (handle-char x))) str))
  264. (define (string-for-each2 str::string)::void
  265. (string-for-each handle-char str))
  266. (import (kawa string-cursors))
  267. (define (string-for-each3 str::string)::void
  268. (string-cursor-for-each (lambda (x) (if (char>? x #\Space) (handle-char x)))
  269. str))
  270. (define (string-for-each4 str::string
  271. start::string-cursor end::string-cursor)::void
  272. (string-cursor-for-each handle-char str start end))
  273. (define (string-for-each5 str::string
  274. start::int end::int)::void
  275. (srfi-13-string-for-each handle-char str start end))
  276. (define (string-for-each6 str::string)::void
  277. (string-for-each
  278. (lambda (x y z) (handle-char x) (handle-char y) (handle-char z))
  279. str "BCDE" str))
  280. (define (string-append1 (str::gnu.lists.FString) (ch::char))
  281. (string-append! str ch))
  282. (define (string-append2 (str::gnu.lists.FString) (ch::character))
  283. (string-append! str ch))
  284. (define (string-append3 (str::gnu.lists.FString) (ch::gnu.lists.FString))
  285. (string-append! str ch))
  286. (define (string-append4 (str::gnu.lists.FString) (ch::gnu.text.Char))
  287. (string-append! str ch))
  288. (define (string-append5 (str::gnu.lists.FString) (ch::java.lang.Character))
  289. (string-append! str ch))
  290. (define (string-append6 (str::gnu.lists.FString) ch)
  291. (string-append! str ch))
  292. (define (string-append7 (str::gnu.lists.FString) ch1 (ch2::character))
  293. (string-append! str ch1 ch2))
  294. (define (translate-space-to-newline str::string)::string
  295. (let ((result (make-string 0)))
  296. (string-for-each
  297. (lambda (ch)
  298. (string-append! result
  299. (if (char=? ch #\Space) #\Newline ch)))
  300. str)
  301. result))
  302. (define (case01)
  303. (let ((key 5))
  304. (case key
  305. ((1 2 3 4) '1to4)
  306. ((5 6 7 8) '5to8))))
  307. (define (case02)
  308. (let ((key (* 2 3)))
  309. (case key
  310. ((1 2 3 4) '1to4)
  311. ((5 6 7 8) '5to8))))
  312. (define (case03)
  313. (let ((key 'five))
  314. (case key
  315. ((one two three four) '1to4)
  316. ((five six seven eight) '5to8))))
  317. (define (case04 key)
  318. (case key
  319. ((1 2 3 4) (+ 5 (* 2 3)))
  320. ((5 6 7 8) (* 2 (+ 3 4)))
  321. (else (+ (* 3 2) 6))))
  322. (define (case05 key::int)
  323. (case key
  324. ((1 2 3 4) (+ 5 (* 2 3)))
  325. ((5 6 7 8) (* 2 (+ 3 4)))
  326. (else (+ (* 3 2) 6))))
  327. (define (case06 key::long)
  328. (case key
  329. ((1 2 3 4) (+ 5 (* 2 3)))
  330. ((5 6 7 8) (* 2 (+ 3 4)))
  331. (else (+ (* 3 2) 6))))
  332. (define (case07 key)
  333. (case key
  334. ((1 2 3 4) 1)
  335. ((5 6 7 8) 2)
  336. (else 3)))
  337. (define (case08 key::int)
  338. (case key
  339. ((1 2 3 4) 1)
  340. ((5 6 7 8) 2)
  341. (else 3)))
  342. (define (case09 key::long)
  343. (case key
  344. ((1 2 3 4) 1)
  345. ((5 6 7 8) 2)
  346. (else 3)))
  347. (define (case10 key)
  348. (case key
  349. ((1 2 3 4) '1to4)
  350. ((5 6 7 8) '5to8)
  351. (else 3)))
  352. (define (case11 key::int)
  353. (case key
  354. ((1 2 3 4) '1to4)
  355. ((5 6 7 8) '5to8)
  356. (else 3)))
  357. (define (case12 key::long)
  358. (case key
  359. ((1 2 3 4) '1to4)
  360. ((5 6 7 8) '5to8)
  361. (else 3)))
  362. (define (case13 key::integer)
  363. (case key
  364. ((1 2 3 4) 1)
  365. ((5 6 7 8) 2)
  366. (else 3)))
  367. (define (case14 key::char)
  368. (case key
  369. ((#\a #\b #\c #\d) 1)
  370. ((#\e #\f #\g #\h) 2)
  371. (else 3)))
  372. (define (callWithValues1 x::integer y::integer)
  373. (call-with-values (lambda () (floor/ x y))
  374. (lambda (a b) (list b a))))
  375. (define (callWithValues2 x::integer y::integer)
  376. (call-with-values (lambda () (values (+ x 1) (- y 1)))
  377. list))
  378. (define (callWithValues3 x::integer y::integer)
  379. (call-with-values (lambda () (floor/ x y))
  380. list))
  381. (define (mmemq x list)
  382. (let lp ((lst list))
  383. (and (? p::pair lst)
  384. (if (eq? x p:car) lst
  385. (lp p:cdr)))))
  386. (define (greater-equal x y)::boolean
  387. (>= x y))
  388. (define (greater-equal-u32-s32 x::uint y::int)
  389. (>= x y))
  390. (define (greater-equal-u32-s32-generic x::uint y::int)
  391. (greater-equal x y))
  392. (define (greater-equal-u64-s32 x::ulong y::int)
  393. (>= x y))
  394. (define (greater-equal-u64-u64 x::ulong y::ulong)
  395. (>= x y))
  396. (define (greater-equal-u64-u64-generic x::ulong y::ulong)
  397. (greater-equal x y))
  398. (define s8a::byte 123)
  399. (define (increment-s8a) (set! s8a (+ s8a 1)))
  400. (define (increment-arr-s8 arr::byte[] i::int) (set! (arr i) (+ (arr i) 1)))
  401. (define u8a::ubyte 253)
  402. (define u16a::ushort #xff35)
  403. (define (increment-u8a) (set! u8a (+ u8a 1)))
  404. (define (set-u16a val::int) (set! u16a val))
  405. (define (add-u8a-u16a)
  406. (+ u8a u16a))
  407. (define (rshift-integer x::integer y::int)
  408. (bitwise-arithmetic-shift-right x y))
  409. (define (rshift-s16 x::short y::int)
  410. (bitwise-arithmetic-shift-right x y))
  411. (define (rshift-s32 x::int y::int)
  412. (bitwise-arithmetic-shift-right x y))
  413. (define (rshift-s64 x::long y::int)
  414. (bitwise-arithmetic-shift-right x y))
  415. (define (rshift-u32 x::uint y::int)
  416. (bitwise-arithmetic-shift-right x y))
  417. (define (rshift-u64 x::ulong y::int)
  418. (bitwise-arithmetic-shift-right x y))
  419. (define (lshift-integer x::integer y::int)
  420. (bitwise-arithmetic-shift-left x y))
  421. (define (lshift-s16 x::short y::int)
  422. (bitwise-arithmetic-shift-left x y))
  423. (define (lshift-s32 x::int y::int)
  424. (bitwise-arithmetic-shift-left x y))
  425. (define (lshift-s64 x::long y::int)
  426. (bitwise-arithmetic-shift-left x y))
  427. (define (lshift-u32 x::uint y::int)
  428. (bitwise-arithmetic-shift-left x y))
  429. (define (lshift-u64 x::ulong y::int)
  430. (bitwise-arithmetic-shift-left x y))
  431. (define (index-s16 i::int)
  432. (let ((v #s16(3 5 -12 2)))
  433. (v i)))
  434. (define (make-u8v1 x::int)
  435. (u8vector 5 253 x))
  436. (define (index-u8v1 i::int)
  437. (let ((v (make-u8v1 3)))
  438. (v i)))
  439. (define (index-u8i1 i::int) ::int
  440. (let ((v (make-u8v1 3)))
  441. (v i)))
  442. (define (index-u8i2 i::int) ::int
  443. (index-u8v1 i))
  444. (define (index-f32 i::int)
  445. (let ((v #f32(3.4 1/2 55)))
  446. (f32vector-ref v i)))
  447. (define (set-u8vector1 v::u8vector i::int x::long)
  448. (u8vector-set! v i x))
  449. (define (set-u8vector2 v::u8vector i::int x::long)
  450. (set! (v i) x))
  451. (define (index-seq q::sequence i::int)
  452. (q i))
  453. (define (index-str1 x::string i::int)
  454. (x i))
  455. (define (index-str2 x::string i::int)
  456. ((dynamic x) i))
  457. (define (index-str3 x::string i::int)
  458. (index-seq x i))
  459. (define (index-garr1 x::array2 i::int j::int)
  460. (x i j))
  461. (define (index-garr2 x::array2 i::int j::int)
  462. (+ (x j i) 100))
  463. (define (index-garr3 x::array2[double] i::int j::int)
  464. (x i j))
  465. (define (index-garr4 x::array2[long] i::int j::int)
  466. (+ (x j i) 100))
  467. (define (index-garr5 x::array[double] i::int j::int)
  468. (x i j))
  469. (define (index-garr6 x::array i::int j::int)
  470. (x i j))
  471. (define (index-garr7 x::array[int] i::int j::int)
  472. (x i j))
  473. ;; From GitLab issue #32 "Imprecise infered return type".
  474. (define (list-cond x) (if x '() (list 1 2)))