irbuild-i32.test 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 :: int32
  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 :: int32
  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 :: int32
  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 :: int32
  88. r2, r3, r4 :: bit
  89. r5 :: int32
  90. r6 :: bit
  91. r7, r8, r9 :: int32
  92. r10, r11, r12 :: bit
  93. r13 :: int32
  94. r14 :: bit
  95. r15 :: int32
  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 :: int32
  137. r2, r3, r4, r5 :: bit
  138. r6, r7, r8 :: int32
  139. r9, r10, r11, r12 :: bit
  140. r13 :: int32
  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 :: int32
  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 :: int32
  194. r1 :: object
  195. L0:
  196. r0 = unbox(int32, x)
  197. y = r0
  198. r1 = box(int32, 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 :: int32
  208. r0 :: native_int
  209. r1, r2, r3 :: bit
  210. r4 :: native_int
  211. r5, r6 :: int32
  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 int32
  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 :: int32
  241. y :: int
  242. r0 :: native_int
  243. r1, r2, r3 :: bit
  244. r4 :: native_int
  245. r5, r6 :: int32
  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 int32
  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 :: int32
  276. r0 :: native_int
  277. r1 :: bit
  278. r2, r3 :: int32
  279. r4 :: ptr
  280. r5 :: c_ptr
  281. r6 :: int32
  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 :: int32
  307. r0 :: native_int
  308. r1 :: int
  309. L0:
  310. r0 = extend signed a: int32 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 :: int32
  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 :: int32
  345. x :: int
  346. r0 :: native_int
  347. r1, r2, r3 :: bit
  348. r4 :: native_int
  349. r5, r6, r7 :: int32
  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 int32
  366. r6 = r5
  367. goto L5
  368. L4:
  369. CPyInt32_Overflow()
  370. unreachable
  371. L5:
  372. r7 = r6 + a
  373. r8 = extend signed r7: int32 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 :: int32
  386. L0:
  387. x = 0
  388. y = -127
  389. z = 12
  390. return 1
  391. [case testI32ExplicitConversionFromNativeInt]
  392. from mypy_extensions import i64, i32
  393. def from_i32(x: i32) -> i32:
  394. return i32(x)
  395. def from_i64(x: i64) -> i32:
  396. return i32(x)
  397. [out]
  398. def from_i32(x):
  399. x :: int32
  400. L0:
  401. return x
  402. def from_i64(x):
  403. x :: int64
  404. r0 :: int32
  405. L0:
  406. r0 = truncate x: int64 to int32
  407. return r0
  408. [case testI32ExplicitConversionFromInt_64bit]
  409. from mypy_extensions import i32
  410. def f(x: int) -> i32:
  411. return i32(x)
  412. [out]
  413. def f(x):
  414. x :: int
  415. r0 :: native_int
  416. r1, r2, r3 :: bit
  417. r4 :: native_int
  418. r5, r6 :: int32
  419. L0:
  420. r0 = x & 1
  421. r1 = r0 == 0
  422. if r1 goto L1 else goto L4 :: bool
  423. L1:
  424. r2 = x < 4294967296 :: signed
  425. if r2 goto L2 else goto L4 :: bool
  426. L2:
  427. r3 = x >= -4294967296 :: signed
  428. if r3 goto L3 else goto L4 :: bool
  429. L3:
  430. r4 = x >> 1
  431. r5 = truncate r4: native_int to int32
  432. r6 = r5
  433. goto L5
  434. L4:
  435. CPyInt32_Overflow()
  436. unreachable
  437. L5:
  438. return r6
  439. [case testI32ExplicitConversionFromLiteral]
  440. from mypy_extensions import i32
  441. def f() -> None:
  442. x = i32(0)
  443. y = i32(11)
  444. z = i32(-3)
  445. [out]
  446. def f():
  447. x, y, z :: int32
  448. L0:
  449. x = 0
  450. y = 11
  451. z = -3
  452. return 1
  453. [case testI32ExplicitConversionFromVariousTypes_64bit]
  454. from mypy_extensions import i32
  455. def bool_to_i32(b: bool) -> i32:
  456. return i32(b)
  457. def str_to_i32(s: str) -> i32:
  458. return i32(s)
  459. class C:
  460. def __int__(self) -> i32:
  461. return 5
  462. def instance_to_i32(c: C) -> i32:
  463. return i32(c)
  464. def float_to_i32(x: float) -> i32:
  465. return i32(x)
  466. [out]
  467. def bool_to_i32(b):
  468. b :: bool
  469. r0 :: int32
  470. L0:
  471. r0 = extend b: builtins.bool to int32
  472. return r0
  473. def str_to_i32(s):
  474. s :: str
  475. r0 :: object
  476. r1 :: int32
  477. L0:
  478. r0 = CPyLong_FromStr(s)
  479. r1 = unbox(int32, r0)
  480. return r1
  481. def C.__int__(self):
  482. self :: __main__.C
  483. L0:
  484. return 5
  485. def instance_to_i32(c):
  486. c :: __main__.C
  487. r0 :: int32
  488. L0:
  489. r0 = c.__int__()
  490. return r0
  491. def float_to_i32(x):
  492. x :: float
  493. r0 :: int
  494. r1 :: native_int
  495. r2, r3, r4 :: bit
  496. r5 :: native_int
  497. r6, r7 :: int32
  498. L0:
  499. r0 = CPyTagged_FromFloat(x)
  500. r1 = r0 & 1
  501. r2 = r1 == 0
  502. if r2 goto L1 else goto L4 :: bool
  503. L1:
  504. r3 = r0 < 4294967296 :: signed
  505. if r3 goto L2 else goto L4 :: bool
  506. L2:
  507. r4 = r0 >= -4294967296 :: signed
  508. if r4 goto L3 else goto L4 :: bool
  509. L3:
  510. r5 = r0 >> 1
  511. r6 = truncate r5: native_int to int32
  512. r7 = r6
  513. goto L5
  514. L4:
  515. CPyInt32_Overflow()
  516. unreachable
  517. L5:
  518. return r7
  519. [case testI32ExplicitConversionFromFloat_32bit]
  520. from mypy_extensions import i32
  521. def float_to_i32(x: float) -> i32:
  522. return i32(x)
  523. [out]
  524. def float_to_i32(x):
  525. x :: float
  526. r0 :: int
  527. r1 :: native_int
  528. r2 :: bit
  529. r3, r4 :: int32
  530. r5 :: ptr
  531. r6 :: c_ptr
  532. r7 :: int32
  533. L0:
  534. r0 = CPyTagged_FromFloat(x)
  535. r1 = r0 & 1
  536. r2 = r1 == 0
  537. if r2 goto L1 else goto L2 :: bool
  538. L1:
  539. r3 = r0 >> 1
  540. r4 = r3
  541. goto L3
  542. L2:
  543. r5 = r0 ^ 1
  544. r6 = r5
  545. r7 = CPyLong_AsInt32(r6)
  546. r4 = r7
  547. keep_alive r0
  548. L3:
  549. return r4