Skip to content

Commit 7e12a60

Browse files
committed
py/compile: Fix edge case when constant-folding negation of integer.
Also adds tests specifically for testing constant folding.
1 parent 2a8d7ee commit 7e12a60

3 files changed

Lines changed: 44 additions & 7 deletions

File tree

py/compile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
315315
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
316316
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
317317
// -int
318-
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
318+
arg = -arg;
319+
if (MP_SMALL_INT_FITS(arg)) {
320+
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
321+
}
319322
} else {
320323
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
321324
// ~int

tests/basics/int_constfolding.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# tests int constant folding in compiler
2+
3+
# positive
4+
print(+1)
5+
print(+100)
6+
7+
# negation
8+
print(-1)
9+
print(-(-1))
10+
print(-0x3fffffff) # 32-bit edge case
11+
print(-0x3fffffffffffffff) # 64-bit edge case
12+
print(-(-0x3fffffff - 1)) # 32-bit edge case
13+
print(-(-0x3fffffffffffffff - 1)) # 64-bit edge case
14+
15+
# 1's complement
16+
print(~0)
17+
print(~1)
18+
print(~-1)
19+
print(~0x3fffffff) # 32-bit edge case
20+
print(~0x3fffffffffffffff) # 64-bit edge case
21+
print(~(-0x3fffffff - 1)) # 32-bit edge case
22+
print(~(-0x3fffffffffffffff - 1)) # 64-bit edge case
23+
24+
# addition
25+
print(1 + 2)
26+
27+
# subtraction
28+
print(1 - 2)
29+
print(2 - 1)
30+
31+
# multiplication
32+
print(1 * 2)
33+
print(123 * 456)
34+
35+
# floor div and modulo
36+
print(123 // 7, 123 % 7)
37+
print(-123 // 7, -123 % 7)
38+
print(123 // -7, 123 % -7)
39+
print(-123 // -7, -123 % -7)
40+

tests/basics/int_divmod.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
if j != 0:
77
print(i, j, i // j, i % j, divmod(i, j))
88

9-
# this tests compiler constant folding
10-
print(123 // 7, 123 % 7)
11-
print(-123 // 7, -123 % 7)
12-
print(123 // -7, 123 % -7)
13-
print(-123 // -7, -123 % -7)
14-
159
# this tests bignum modulo
1610
a = 987654321987987987987987987987
1711
b = 19

0 commit comments

Comments
 (0)