summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-04-07 23:53:37 +1000
committerDamien George <damien@micropython.org>2022-04-14 23:52:14 +1000
commit9c8a56343fea4e3603dbeeabd1b3a2194f9dca91 (patch)
treee25fdb654eb13f70b137ac6ca8a34831aa5e01fe
parentabdc4ec08d742642598555ba8e1854fe1445645e (diff)
tools/mpy-tool.py: Optimise freezing of ints that can fit a small int.
Signed-off-by: Damien George <damien@micropython.org>
-rwxr-xr-xtools/mpy-tool.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
index 000b22578..17dd8704a 100755
--- a/tools/mpy-tool.py
+++ b/tools/mpy-tool.py
@@ -327,6 +327,13 @@ class Opcodes:
mapping[MP_BC_BINARY_OP_MULTI + i] = "BINARY_OP %d %s" % (i, mp_binary_op_method_name[i])
+# This definition of a small int covers all possible targets, in the sense that every
+# target can encode as a small int, an integer that passes this test. The minimum is set
+# by MICROPY_OBJ_REPR_B on a 16-bit machine, where there are 14 bits for the small int.
+def mp_small_int_fits(i):
+ return -0x2000 <= i <= 0x1FFF
+
+
# this function mirrors that in py/bc.c
def mp_opcode_format(bytecode, ip, count_var_uint):
opcode = bytecode[ip]
@@ -633,8 +640,10 @@ class CompiledModule:
const_obj_content += 4 * 4
return "MP_ROM_PTR(&%s)" % obj_name
elif is_int_type(obj):
- if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
- # TODO check if we can actually fit this long-int into a small-int
+ if mp_small_int_fits(obj):
+ # Encode directly as a small integer object.
+ return "MP_ROM_INT(%d)" % obj
+ elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_NONE:
raise FreezeError(self, "target does not support long int")
elif config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_LONGLONG:
# TODO