irbuild-i32.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. # Test cases for i32 native ints. Focus on things that are different from i64; no need to
  2. # duplicate all i64 test cases here.
  3. [case testI32BinaryOp]
  4. from mypy_extensions import i32
  5. def add_op(x: i32, y: i32) -> i32:
  6. x = y + x
  7. y = x + 5
  8. y += x
  9. y += 7
  10. x = 5 + y
  11. return x
  12. def compare(x: i32, y: i32) -> None:
  13. a = x == y
  14. b = x == -5
  15. c = x < y
  16. d = x < -5
  17. e = -5 == x
  18. f = -5 < x
  19. [out]
  20. def add_op(x, y):
  21. x, y, r0, r1, r2, r3, r4 :: i32
  22. L0:
  23. r0 = y + x
  24. x = r0
  25. r1 = x + 5
  26. y = r1
  27. r2 = y + x
  28. y = r2
  29. r3 = y + 7
  30. y = r3
  31. r4 = 5 + y
  32. x = r4
  33. return x
  34. def compare(x, y):
  35. x, y :: i32
  36. r0 :: bit
  37. a :: bool
  38. r1 :: bit
  39. b :: bool
  40. r2 :: bit
  41. c :: bool
  42. r3 :: bit
  43. d :: bool
  44. r4 :: bit
  45. e :: bool
  46. r5 :: bit
  47. f :: bool
  48. L0:
  49. r0 = x == y
  50. a = r0
  51. r1 = x == -5
  52. b = r1
  53. r2 = x < y :: signed
  54. c = r2
  55. r3 = x < -5 :: signed
  56. d = r3
  57. r4 = -5 == x
  58. e = r4
  59. r5 = -5 < x :: signed
  60. f = r5
  61. return 1
  62. [case testI32UnaryOp]
  63. from mypy_extensions import i32
  64. def unary(x: i32) -> i32:
  65. y = -x
  66. x = ~y
  67. y = +x
  68. return y
  69. [out]
  70. def unary(x):
  71. x, r0, y, r1 :: i32
  72. L0:
  73. r0 = 0 - x
  74. y = r0
  75. r1 = y ^ -1
  76. x = r1
  77. y = x
  78. return y
  79. [case testI32DivisionByConstant]
  80. from mypy_extensions import i32
  81. def div_by_constant(x: i32) -> i32:
  82. x = x // 5
  83. x //= 17
  84. return x
  85. [out]
  86. def div_by_constant(x):
  87. x, r0, r1 :: i32
  88. r2, r3, r4 :: bit
  89. r5 :: i32
  90. r6 :: bit
  91. r7, r8, r9 :: i32
  92. r10, r11, r12 :: bit
  93. r13 :: i32
  94. r14 :: bit
  95. r15 :: i32
  96. L0:
  97. r0 = x / 5
  98. r1 = r0
  99. r2 = x < 0 :: signed
  100. r3 = 5 < 0 :: signed
  101. r4 = r2 == r3
  102. if r4 goto L3 else goto L1 :: bool
  103. L1:
  104. r5 = r1 * 5
  105. r6 = r5 == x
  106. if r6 goto L3 else goto L2 :: bool
  107. L2:
  108. r7 = r1 - 1
  109. r1 = r7
  110. L3:
  111. x = r1
  112. r8 = x / 17
  113. r9 = r8
  114. r10 = x < 0 :: signed
  115. r11 = 17 < 0 :: signed
  116. r12 = r10 == r11
  117. if r12 goto L6 else goto L4 :: bool
  118. L4:
  119. r13 = r9 * 17
  120. r14 = r13 == x
  121. if r14 goto L6 else goto L5 :: bool
  122. L5:
  123. r15 = r9 - 1
  124. r9 = r15
  125. L6:
  126. x = r9
  127. return x
  128. [case testI32ModByConstant]
  129. from mypy_extensions import i32
  130. def mod_by_constant(x: i32) -> i32:
  131. x = x % 5
  132. x %= 17
  133. return x
  134. [out]
  135. def mod_by_constant(x):
  136. x, r0, r1 :: i32
  137. r2, r3, r4, r5 :: bit
  138. r6, r7, r8 :: i32
  139. r9, r10, r11, r12 :: bit
  140. r13 :: i32
  141. L0:
  142. r0 = x % 5
  143. r1 = r0
  144. r2 = x < 0 :: signed
  145. r3 = 5 < 0 :: signed
  146. r4 = r2 == r3
  147. if r4 goto L3 else goto L1 :: bool
  148. L1:
  149. r5 = r1 == 0
  150. if r5 goto L3 else goto L2 :: bool
  151. L2:
  152. r6 = r1 + 5
  153. r1 = r6
  154. L3:
  155. x = r1
  156. r7 = x % 17
  157. r8 = r7
  158. r9 = x < 0 :: signed
  159. r10 = 17 < 0 :: signed
  160. r11 = r9 == r10
  161. if r11 goto L6 else goto L4 :: bool
  162. L4:
  163. r12 = r8 == 0
  164. if r12 goto L6 else goto L5 :: bool
  165. L5:
  166. r13 = r8 + 17
  167. r8 = r13
  168. L6:
  169. x = r8
  170. return x
  171. [case testI32DivModByVariable]
  172. from mypy_extensions import i32
  173. def divmod(x: i32, y: i32) -> i32:
  174. a = x // y
  175. return a % y
  176. [out]
  177. def divmod(x, y):
  178. x, y, r0, a, r1 :: i32
  179. L0:
  180. r0 = CPyInt32_Divide(x, y)
  181. a = r0
  182. r1 = CPyInt32_Remainder(a, y)
  183. return r1
  184. [case testI32BoxAndUnbox]
  185. from typing import Any
  186. from mypy_extensions import i32
  187. def f(x: Any) -> Any:
  188. y: i32 = x
  189. return y
  190. [out]
  191. def f(x):
  192. x :: object
  193. r0, y :: i32
  194. r1 :: object
  195. L0:
  196. r0 = unbox(i32, x)
  197. y = r0
  198. r1 = box(i32, y)
  199. return r1
  200. [case testI32MixedCompare1_64bit]
  201. from mypy_extensions import i32
  202. def f(x: int, y: i32) -> bool:
  203. return x == y
  204. [out]
  205. def f(x, y):
  206. x :: int
  207. y :: i32
  208. r0 :: native_int
  209. r1, r2, r3 :: bit
  210. r4 :: native_int
  211. r5, r6 :: i32
  212. r7 :: bit
  213. L0:
  214. r0 = x & 1
  215. r1 = r0 == 0
  216. if r1 goto L1 else goto L4 :: bool
  217. L1:
  218. r2 = x < 4294967296 :: signed
  219. if r2 goto L2 else goto L4 :: bool
  220. L2:
  221. r3 = x >= -4294967296 :: signed
  222. if r3 goto L3 else goto L4 :: bool
  223. L3:
  224. r4 = x >> 1
  225. r5 = truncate r4: native_int to i32
  226. r6 = r5
  227. goto L5
  228. L4:
  229. CPyInt32_Overflow()
  230. unreachable
  231. L5:
  232. r7 = r6 == y
  233. return r7
  234. [case testI32MixedCompare2_64bit]
  235. from mypy_extensions import i32
  236. def f(x: i32, y: int) -> bool:
  237. return x == y
  238. [out]
  239. def f(x, y):
  240. x :: i32
  241. y :: int
  242. r0 :: native_int
  243. r1, r2, r3 :: bit
  244. r4 :: native_int
  245. r5, r6 :: i32
  246. r7 :: bit
  247. L0:
  248. r0 = y & 1
  249. r1 = r0 == 0
  250. if r1 goto L1 else goto L4 :: bool
  251. L1:
  252. r2 = y < 4294967296 :: signed
  253. if r2 goto L2 else goto L4 :: bool
  254. L2:
  255. r3 = y >= -4294967296 :: signed
  256. if r3 goto L3 else goto L4 :: bool
  257. L3:
  258. r4 = y >> 1
  259. r5 = truncate r4: native_int to i32
  260. r6 = r5
  261. goto L5
  262. L4:
  263. CPyInt32_Overflow()
  264. unreachable
  265. L5:
  266. r7 = x == r6
  267. return r7
  268. [case testI32MixedCompare_32bit]
  269. from mypy_extensions import i32
  270. def f(x: int, y: i32) -> bool:
  271. return x == y
  272. [out]
  273. def f(x, y):
  274. x :: int
  275. y :: i32
  276. r0 :: native_int
  277. r1 :: bit
  278. r2, r3 :: i32
  279. r4 :: ptr
  280. r5 :: c_ptr
  281. r6 :: i32
  282. r7 :: bit
  283. L0:
  284. r0 = x & 1
  285. r1 = r0 == 0
  286. if r1 goto L1 else goto L2 :: bool
  287. L1:
  288. r2 = x >> 1
  289. r3 = r2
  290. goto L3
  291. L2:
  292. r4 = x ^ 1
  293. r5 = r4
  294. r6 = CPyLong_AsInt32(r5)
  295. r3 = r6
  296. keep_alive x
  297. L3:
  298. r7 = r3 == y
  299. return r7
  300. [case testI32ConvertToInt_64bit]
  301. from mypy_extensions import i32
  302. def i32_to_int(a: i32) -> int:
  303. return a
  304. [out]
  305. def i32_to_int(a):
  306. a :: i32
  307. r0 :: native_int
  308. r1 :: int
  309. L0:
  310. r0 = extend signed a: i32 to native_int
  311. r1 = r0 << 1
  312. return r1
  313. [case testI32ConvertToInt_32bit]
  314. from mypy_extensions import i32
  315. def i32_to_int(a: i32) -> int:
  316. return a
  317. [out]
  318. def i32_to_int(a):
  319. a :: i32
  320. r0, r1 :: bit
  321. r2, r3, r4 :: int
  322. L0:
  323. r0 = a <= 1073741823 :: signed
  324. if r0 goto L1 else goto L2 :: bool
  325. L1:
  326. r1 = a >= -1073741824 :: signed
  327. if r1 goto L3 else goto L2 :: bool
  328. L2:
  329. r2 = CPyTagged_FromSsize_t(a)
  330. r3 = r2
  331. goto L4
  332. L3:
  333. r4 = a << 1
  334. r3 = r4
  335. L4:
  336. return r3
  337. [case testI32OperatorAssignmentMixed_64bit]
  338. from mypy_extensions import i32
  339. def f(a: i32) -> None:
  340. x = 0
  341. x += a
  342. [out]
  343. def f(a):
  344. a :: i32
  345. x :: int
  346. r0 :: native_int
  347. r1, r2, r3 :: bit
  348. r4 :: native_int
  349. r5, r6, r7 :: i32
  350. r8 :: native_int
  351. r9 :: int
  352. L0:
  353. x = 0
  354. r0 = x & 1
  355. r1 = r0 == 0
  356. if r1 goto L1 else goto L4 :: bool
  357. L1:
  358. r2 = x < 4294967296 :: signed
  359. if r2 goto L2 else goto L4 :: bool
  360. L2:
  361. r3 = x >= -4294967296 :: signed
  362. if r3 goto L3 else goto L4 :: bool
  363. L3:
  364. r4 = x >> 1
  365. r5 = truncate r4: native_int to i32
  366. r6 = r5
  367. goto L5
  368. L4:
  369. CPyInt32_Overflow()
  370. unreachable
  371. L5:
  372. r7 = r6 + a
  373. r8 = extend signed r7: i32 to native_int
  374. r9 = r8 << 1
  375. x = r9
  376. return 1
  377. [case testI32InitializeFromLiteral]
  378. from mypy_extensions import i32, i64
  379. def f() -> None:
  380. x: i32 = 0
  381. y: i32 = -127
  382. z: i32 = 5 + 7
  383. [out]
  384. def f():
  385. x, y, z :: i32
  386. L0:
  387. x = 0
  388. y = -127
  389. z = 12
  390. return 1
  391. [case testI32ExplicitConversionFromNativeInt]
  392. from mypy_extensions import i64, i32, i16
  393. def from_i16(x: i16) -> i32:
  394. return i32(x)
  395. def from_i32(x: i32) -> i32:
  396. return i32(x)
  397. def from_i64(x: i64) -> i32:
  398. return i32(x)
  399. [out]
  400. def from_i16(x):
  401. x :: i16
  402. r0 :: i32
  403. L0:
  404. r0 = extend signed x: i16 to i32
  405. return r0
  406. def from_i32(x):
  407. x :: i32
  408. L0:
  409. return x
  410. def from_i64(x):
  411. x :: i64
  412. r0 :: i32
  413. L0:
  414. r0 = truncate x: i64 to i32
  415. return r0
  416. [case testI32ExplicitConversionFromInt_64bit]
  417. from mypy_extensions import i32
  418. def f(x: int) -> i32:
  419. return i32(x)
  420. [out]
  421. def f(x):
  422. x :: int
  423. r0 :: native_int
  424. r1, r2, r3 :: bit
  425. r4 :: native_int
  426. r5, r6 :: i32
  427. L0:
  428. r0 = x & 1
  429. r1 = r0 == 0
  430. if r1 goto L1 else goto L4 :: bool
  431. L1:
  432. r2 = x < 4294967296 :: signed
  433. if r2 goto L2 else goto L4 :: bool
  434. L2:
  435. r3 = x >= -4294967296 :: signed
  436. if r3 goto L3 else goto L4 :: bool
  437. L3:
  438. r4 = x >> 1
  439. r5 = truncate r4: native_int to i32
  440. r6 = r5
  441. goto L5
  442. L4:
  443. CPyInt32_Overflow()
  444. unreachable
  445. L5:
  446. return r6
  447. [case testI32ExplicitConversionFromLiteral_64bit]
  448. from mypy_extensions import i32
  449. def f() -> None:
  450. x = i32(0)
  451. y = i32(11)
  452. z = i32(-3)
  453. a = i32(2**31)
  454. [out]
  455. def f():
  456. x, y, z, a :: i32
  457. L0:
  458. x = 0
  459. y = 11
  460. z = -3
  461. a = -2147483648
  462. return 1
  463. [case testI32ExplicitConversionFromVariousTypes_64bit]
  464. from mypy_extensions import i32
  465. def bool_to_i32(b: bool) -> i32:
  466. return i32(b)
  467. def str_to_i32(s: str) -> i32:
  468. return i32(s)
  469. class C:
  470. def __int__(self) -> i32:
  471. return 5
  472. def instance_to_i32(c: C) -> i32:
  473. return i32(c)
  474. def float_to_i32(x: float) -> i32:
  475. return i32(x)
  476. [out]
  477. def bool_to_i32(b):
  478. b :: bool
  479. r0 :: i32
  480. L0:
  481. r0 = extend b: builtins.bool to i32
  482. return r0
  483. def str_to_i32(s):
  484. s :: str
  485. r0 :: object
  486. r1 :: i32
  487. L0:
  488. r0 = CPyLong_FromStr(s)
  489. r1 = unbox(i32, r0)
  490. return r1
  491. def C.__int__(self):
  492. self :: __main__.C
  493. L0:
  494. return 5
  495. def instance_to_i32(c):
  496. c :: __main__.C
  497. r0 :: i32
  498. L0:
  499. r0 = c.__int__()
  500. return r0
  501. def float_to_i32(x):
  502. x :: float
  503. r0 :: int
  504. r1 :: native_int
  505. r2, r3, r4 :: bit
  506. r5 :: native_int
  507. r6, r7 :: i32
  508. L0:
  509. r0 = CPyTagged_FromFloat(x)
  510. r1 = r0 & 1
  511. r2 = r1 == 0
  512. if r2 goto L1 else goto L4 :: bool
  513. L1:
  514. r3 = r0 < 4294967296 :: signed
  515. if r3 goto L2 else goto L4 :: bool
  516. L2:
  517. r4 = r0 >= -4294967296 :: signed
  518. if r4 goto L3 else goto L4 :: bool
  519. L3:
  520. r5 = r0 >> 1
  521. r6 = truncate r5: native_int to i32
  522. r7 = r6
  523. goto L5
  524. L4:
  525. CPyInt32_Overflow()
  526. unreachable
  527. L5:
  528. return r7
  529. [case testI32ExplicitConversionFromFloat_32bit]
  530. from mypy_extensions import i32
  531. def float_to_i32(x: float) -> i32:
  532. return i32(x)
  533. [out]
  534. def float_to_i32(x):
  535. x :: float
  536. r0 :: int
  537. r1 :: native_int
  538. r2 :: bit
  539. r3, r4 :: i32
  540. r5 :: ptr
  541. r6 :: c_ptr
  542. r7 :: i32
  543. L0:
  544. r0 = CPyTagged_FromFloat(x)
  545. r1 = r0 & 1
  546. r2 = r1 == 0
  547. if r2 goto L1 else goto L2 :: bool
  548. L1:
  549. r3 = r0 >> 1
  550. r4 = r3
  551. goto L3
  552. L2:
  553. r5 = r0 ^ 1
  554. r6 = r5
  555. r7 = CPyLong_AsInt32(r6)
  556. r4 = r7
  557. keep_alive r0
  558. L3:
  559. return r4