summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-04 11:21:58 +0000
committerDamien George <damien.p.george@gmail.com>2014-04-04 11:21:58 +0000
commitaf27259439b6a48ef2d71cc94ca8d3004ac54d62 (patch)
tree57198bf59b70dba218c25edbcd2dec92a2ae9f86 /py
parent28390340e5986a33ddb64e1e7af6266c20b009ca (diff)
downloadmicropython-af27259439b6a48ef2d71cc94ca8d3004ac54d62.tar.gz
micropython-af27259439b6a48ef2d71cc94ca8d3004ac54d62.zip
py: Enable optimisation of multiplying 2 small ints in compiler.
Diffstat (limited to 'py')
-rw-r--r--py/compile.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/py/compile.c b/py/compile.c
index 44b37b9342..6ede842397 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -115,31 +115,31 @@ mp_parse_node_t fold_constants(mp_parse_node_t pn) {
if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
- machine_int_t res;
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
- res = arg0 + arg1;
+ arg0 += arg1;
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
- res = arg0 - arg1;
+ arg0 -= arg1;
} else {
// shouldn't happen
assert(0);
- res = 0;
}
- if (MP_PARSE_FITS_SMALL_INT(res)) {
- pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
+ if (MP_PARSE_FITS_SMALL_INT(arg0)) {
+ pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
}
}
break;
case PN_term:
if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
- int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
- int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
+ machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
+ machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
-#if MICROPY_EMIT_CPYTHON
- // can overflow; enabled only to compare with CPython
- pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
-#endif
+ if (!mp_small_int_mul_overflow(arg0, arg1)) {
+ arg0 *= arg1;
+ if (MP_PARSE_FITS_SMALL_INT(arg0)) {
+ pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
+ }
+ }
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
; // pass
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {