Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
7e12a601
Commit
7e12a601
authored
Oct 08, 2015
by
Damien George
Browse files
py/compile: Fix edge case when constant-folding negation of integer.
Also adds tests specifically for testing constant folding.
parent
2a8d7ee0
Changes
3
Hide whitespace changes
Inline
Side-by-side
py/compile.c
View file @
7e12a601
...
...
@@ -315,7 +315,10 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
pn
=
mp_parse_node_new_leaf
(
MP_PARSE_NODE_SMALL_INT
,
arg
);
}
else
if
(
MP_PARSE_NODE_IS_TOKEN_KIND
(
pns
->
nodes
[
0
],
MP_TOKEN_OP_MINUS
))
{
// -int
pn
=
mp_parse_node_new_leaf
(
MP_PARSE_NODE_SMALL_INT
,
-
arg
);
arg
=
-
arg
;
if
(
MP_SMALL_INT_FITS
(
arg
))
{
pn
=
mp_parse_node_new_leaf
(
MP_PARSE_NODE_SMALL_INT
,
arg
);
}
}
else
{
assert
(
MP_PARSE_NODE_IS_TOKEN_KIND
(
pns
->
nodes
[
0
],
MP_TOKEN_OP_TILDE
));
// should be
// ~int
...
...
tests/basics/int_constfolding.py
0 → 100644
View file @
7e12a601
# tests int constant folding in compiler
# positive
print
(
+
1
)
print
(
+
100
)
# negation
print
(
-
1
)
print
(
-
(
-
1
))
print
(
-
0x3fffffff
)
# 32-bit edge case
print
(
-
0x3fffffffffffffff
)
# 64-bit edge case
print
(
-
(
-
0x3fffffff
-
1
))
# 32-bit edge case
print
(
-
(
-
0x3fffffffffffffff
-
1
))
# 64-bit edge case
# 1's complement
print
(
~
0
)
print
(
~
1
)
print
(
~-
1
)
print
(
~
0x3fffffff
)
# 32-bit edge case
print
(
~
0x3fffffffffffffff
)
# 64-bit edge case
print
(
~
(
-
0x3fffffff
-
1
))
# 32-bit edge case
print
(
~
(
-
0x3fffffffffffffff
-
1
))
# 64-bit edge case
# addition
print
(
1
+
2
)
# subtraction
print
(
1
-
2
)
print
(
2
-
1
)
# multiplication
print
(
1
*
2
)
print
(
123
*
456
)
# floor div and modulo
print
(
123
//
7
,
123
%
7
)
print
(
-
123
//
7
,
-
123
%
7
)
print
(
123
//
-
7
,
123
%
-
7
)
print
(
-
123
//
-
7
,
-
123
%
-
7
)
tests/basics/int_divmod.py
View file @
7e12a601
...
...
@@ -6,12 +6,6 @@ for i in range(-2, 3):
if
j
!=
0
:
print
(
i
,
j
,
i
//
j
,
i
%
j
,
divmod
(
i
,
j
))
# this tests compiler constant folding
print
(
123
//
7
,
123
%
7
)
print
(
-
123
//
7
,
-
123
%
7
)
print
(
123
//
-
7
,
123
%
-
7
)
print
(
-
123
//
-
7
,
-
123
%
-
7
)
# this tests bignum modulo
a
=
987654321987987987987987987987
b
=
19
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment