Debian Codegraph @

mypyc/test-data/irbuild-i32.test

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